blob: 6727c1758376c7c228aa504438988c992793923c [file] [log] [blame]
Vladimir Markob163bb72015-03-31 21:49:49 +01001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "linker/relative_patcher.h"
18
Alex Light50fa9932015-08-10 15:30:07 -070019#ifdef ART_ENABLE_CODEGEN_arm
Vladimir Markob163bb72015-03-31 21:49:49 +010020#include "linker/arm/relative_patcher_thumb2.h"
Alex Light50fa9932015-08-10 15:30:07 -070021#endif
22#ifdef ART_ENABLE_CODEGEN_arm64
Vladimir Markob163bb72015-03-31 21:49:49 +010023#include "linker/arm64/relative_patcher_arm64.h"
Alex Light50fa9932015-08-10 15:30:07 -070024#endif
25#ifdef ART_ENABLE_CODEGEN_x86
Vladimir Markob163bb72015-03-31 21:49:49 +010026#include "linker/x86/relative_patcher_x86.h"
Alex Light50fa9932015-08-10 15:30:07 -070027#endif
28#ifdef ART_ENABLE_CODEGEN_x86_64
Vladimir Markob163bb72015-03-31 21:49:49 +010029#include "linker/x86_64/relative_patcher_x86_64.h"
Alex Light50fa9932015-08-10 15:30:07 -070030#endif
Vladimir Markob163bb72015-03-31 21:49:49 +010031#include "output_stream.h"
32
33namespace art {
34namespace linker {
35
36std::unique_ptr<RelativePatcher> RelativePatcher::Create(
Vladimir Marko944da602016-02-19 12:27:55 +000037 InstructionSet instruction_set,
38 const InstructionSetFeatures* features,
Vladimir Markob163bb72015-03-31 21:49:49 +010039 RelativePatcherTargetProvider* provider) {
40 class RelativePatcherNone FINAL : public RelativePatcher {
41 public:
42 RelativePatcherNone() { }
43
44 uint32_t ReserveSpace(uint32_t offset,
Vladimir Marko4d23c9d2015-04-01 23:03:09 +010045 const CompiledMethod* compiled_method ATTRIBUTE_UNUSED,
46 MethodReference method_ref ATTRIBUTE_UNUSED) OVERRIDE {
Vladimir Markob163bb72015-03-31 21:49:49 +010047 return offset; // No space reserved; no patches expected.
48 }
49
Vladimir Marko71b0ddf2015-04-02 19:45:06 +010050 uint32_t ReserveSpaceEnd(uint32_t offset) OVERRIDE {
51 return offset; // No space reserved; no patches expected.
52 }
53
Vladimir Markob163bb72015-03-31 21:49:49 +010054 uint32_t WriteThunks(OutputStream* out ATTRIBUTE_UNUSED, uint32_t offset) OVERRIDE {
55 return offset; // No thunks added; no patches expected.
56 }
57
58 void PatchCall(std::vector<uint8_t>* code ATTRIBUTE_UNUSED,
59 uint32_t literal_offset ATTRIBUTE_UNUSED,
60 uint32_t patch_offset ATTRIBUTE_UNUSED,
61 uint32_t target_offset ATTRIBUTE_UNUSED) OVERRIDE {
62 LOG(FATAL) << "Unexpected relative call patch.";
63 }
64
65 virtual void PatchDexCacheReference(std::vector<uint8_t>* code ATTRIBUTE_UNUSED,
66 const LinkerPatch& patch ATTRIBUTE_UNUSED,
67 uint32_t patch_offset ATTRIBUTE_UNUSED,
68 uint32_t target_offset ATTRIBUTE_UNUSED) {
69 LOG(FATAL) << "Unexpected relative dex cache array patch.";
70 }
71
72 private:
73 DISALLOW_COPY_AND_ASSIGN(RelativePatcherNone);
74 };
75
Alex Light50fa9932015-08-10 15:30:07 -070076 UNUSED(features);
77 UNUSED(provider);
Vladimir Markob163bb72015-03-31 21:49:49 +010078 switch (instruction_set) {
Alex Light50fa9932015-08-10 15:30:07 -070079#ifdef ART_ENABLE_CODEGEN_x86
Vladimir Markob163bb72015-03-31 21:49:49 +010080 case kX86:
81 return std::unique_ptr<RelativePatcher>(new X86RelativePatcher());
Alex Light50fa9932015-08-10 15:30:07 -070082#endif
83#ifdef ART_ENABLE_CODEGEN_x86_64
Vladimir Markob163bb72015-03-31 21:49:49 +010084 case kX86_64:
85 return std::unique_ptr<RelativePatcher>(new X86_64RelativePatcher());
Alex Light50fa9932015-08-10 15:30:07 -070086#endif
87#ifdef ART_ENABLE_CODEGEN_arm
Vladimir Markob163bb72015-03-31 21:49:49 +010088 case kArm:
89 // Fall through: we generate Thumb2 code for "arm".
90 case kThumb2:
91 return std::unique_ptr<RelativePatcher>(new Thumb2RelativePatcher(provider));
Alex Light50fa9932015-08-10 15:30:07 -070092#endif
93#ifdef ART_ENABLE_CODEGEN_arm64
Vladimir Markob163bb72015-03-31 21:49:49 +010094 case kArm64:
95 return std::unique_ptr<RelativePatcher>(
96 new Arm64RelativePatcher(provider, features->AsArm64InstructionSetFeatures()));
Alex Light50fa9932015-08-10 15:30:07 -070097#endif
Vladimir Markob163bb72015-03-31 21:49:49 +010098 default:
99 return std::unique_ptr<RelativePatcher>(new RelativePatcherNone);
Vladimir Markob163bb72015-03-31 21:49:49 +0100100 }
101}
102
103bool RelativePatcher::WriteCodeAlignment(OutputStream* out, uint32_t aligned_code_delta) {
104 static const uint8_t kPadding[] = {
105 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u
106 };
107 DCHECK_LE(aligned_code_delta, sizeof(kPadding));
108 if (UNLIKELY(!out->WriteFully(kPadding, aligned_code_delta))) {
109 return false;
110 }
111 size_code_alignment_ += aligned_code_delta;
112 return true;
113}
114
115bool RelativePatcher::WriteRelCallThunk(OutputStream* out, const ArrayRef<const uint8_t>& thunk) {
116 if (UNLIKELY(!out->WriteFully(thunk.data(), thunk.size()))) {
117 return false;
118 }
119 size_relative_call_thunks_ += thunk.size();
120 return true;
121}
122
123bool RelativePatcher::WriteMiscThunk(OutputStream* out, const ArrayRef<const uint8_t>& thunk) {
124 if (UNLIKELY(!out->WriteFully(thunk.data(), thunk.size()))) {
125 return false;
126 }
127 size_misc_thunks_ += thunk.size();
128 return true;
129}
130
131} // namespace linker
132} // namespace art