1 /*
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
19 #include "debug/method_debug_info.h"
20 #ifdef ART_ENABLE_CODEGEN_arm
21 #include "linker/arm/relative_patcher_thumb2.h"
22 #endif
23 #ifdef ART_ENABLE_CODEGEN_arm64
24 #include "linker/arm64/relative_patcher_arm64.h"
25 #endif
26 #ifdef ART_ENABLE_CODEGEN_riscv64
27 #include "linker/riscv64/relative_patcher_riscv64.h"
28 #endif
29 #ifdef ART_ENABLE_CODEGEN_x86
30 #include "linker/x86/relative_patcher_x86.h"
31 #endif
32 #ifdef ART_ENABLE_CODEGEN_x86_64
33 #include "linker/x86_64/relative_patcher_x86_64.h"
34 #endif
35 #include "stream/output_stream.h"
36
37 namespace art {
38 namespace linker {
39
Create(InstructionSet instruction_set,const InstructionSetFeatures * features,RelativePatcherThunkProvider * thunk_provider,RelativePatcherTargetProvider * target_provider)40 std::unique_ptr<RelativePatcher> RelativePatcher::Create(
41 InstructionSet instruction_set,
42 const InstructionSetFeatures* features,
43 RelativePatcherThunkProvider* thunk_provider,
44 RelativePatcherTargetProvider* target_provider) {
45 class RelativePatcherNone final : public RelativePatcher {
46 public:
47 RelativePatcherNone() { }
48
49 uint32_t ReserveSpace(uint32_t offset,
50 [[maybe_unused]] const CompiledMethod* compiled_method,
51 [[maybe_unused]] MethodReference method_ref) override {
52 return offset; // No space reserved; no patches expected.
53 }
54
55 uint32_t ReserveSpaceEnd(uint32_t offset) override {
56 return offset; // No space reserved; no patches expected.
57 }
58
59 uint32_t WriteThunks([[maybe_unused]] OutputStream* out, uint32_t offset) override {
60 return offset; // No thunks added; no patches expected.
61 }
62
63 void PatchCall([[maybe_unused]] std::vector<uint8_t>* code,
64 [[maybe_unused]] uint32_t literal_offset,
65 [[maybe_unused]] uint32_t patch_offset,
66 [[maybe_unused]] uint32_t target_offset) override {
67 LOG(FATAL) << "Unexpected relative call patch.";
68 }
69
70 void PatchPcRelativeReference([[maybe_unused]] std::vector<uint8_t>* code,
71 [[maybe_unused]] const LinkerPatch& patch,
72 [[maybe_unused]] uint32_t patch_offset,
73 [[maybe_unused]] uint32_t target_offset) override {
74 LOG(FATAL) << "Unexpected relative dex cache array patch.";
75 }
76
77 void PatchEntrypointCall([[maybe_unused]] std::vector<uint8_t>* code,
78 [[maybe_unused]] const LinkerPatch& patch,
79 [[maybe_unused]] uint32_t patch_offset) override {
80 LOG(FATAL) << "Unexpected entrypoint call patch.";
81 }
82
83 void PatchBakerReadBarrierBranch([[maybe_unused]] std::vector<uint8_t>* code,
84 [[maybe_unused]] const LinkerPatch& patch,
85 [[maybe_unused]] uint32_t patch_offset) override {
86 LOG(FATAL) << "Unexpected baker read barrier branch patch.";
87 }
88
89 std::vector<debug::MethodDebugInfo> GenerateThunkDebugInfo(
90 [[maybe_unused]] uint32_t executable_offset) override {
91 return std::vector<debug::MethodDebugInfo>(); // No thunks added.
92 }
93
94 private:
95 DISALLOW_COPY_AND_ASSIGN(RelativePatcherNone);
96 };
97
98 UNUSED(features);
99 UNUSED(thunk_provider);
100 UNUSED(target_provider);
101 switch (instruction_set) {
102 #ifdef ART_ENABLE_CODEGEN_x86
103 case InstructionSet::kX86:
104 return std::unique_ptr<RelativePatcher>(new X86RelativePatcher());
105 #endif
106 #ifdef ART_ENABLE_CODEGEN_x86_64
107 case InstructionSet::kX86_64:
108 return std::unique_ptr<RelativePatcher>(new X86_64RelativePatcher());
109 #endif
110 #ifdef ART_ENABLE_CODEGEN_arm
111 case InstructionSet::kArm:
112 // Fall through: we generate Thumb2 code for "arm".
113 case InstructionSet::kThumb2:
114 return std::unique_ptr<RelativePatcher>(
115 new Thumb2RelativePatcher(thunk_provider, target_provider));
116 #endif
117 #ifdef ART_ENABLE_CODEGEN_arm64
118 case InstructionSet::kArm64:
119 return std::unique_ptr<RelativePatcher>(
120 new Arm64RelativePatcher(thunk_provider,
121 target_provider,
122 features->AsArm64InstructionSetFeatures()));
123 #endif
124 #ifdef ART_ENABLE_CODEGEN_riscv64
125 case InstructionSet::kRiscv64:
126 return std::unique_ptr<RelativePatcher>(
127 new Riscv64RelativePatcher(thunk_provider,
128 target_provider,
129 features->AsRiscv64InstructionSetFeatures()));
130 #endif
131 default:
132 return std::unique_ptr<RelativePatcher>(new RelativePatcherNone);
133 }
134 }
135
WriteCodeAlignment(OutputStream * out,uint32_t aligned_code_delta)136 bool RelativePatcher::WriteCodeAlignment(OutputStream* out, uint32_t aligned_code_delta) {
137 static const uint8_t kPadding[] = {
138 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u
139 };
140 DCHECK_LE(aligned_code_delta, sizeof(kPadding));
141 if (UNLIKELY(!out->WriteFully(kPadding, aligned_code_delta))) {
142 return false;
143 }
144 size_code_alignment_ += aligned_code_delta;
145 return true;
146 }
147
WriteThunk(OutputStream * out,const ArrayRef<const uint8_t> & thunk)148 bool RelativePatcher::WriteThunk(OutputStream* out, const ArrayRef<const uint8_t>& thunk) {
149 if (UNLIKELY(!out->WriteFully(thunk.data(), thunk.size()))) {
150 return false;
151 }
152 size_relative_call_thunks_ += thunk.size();
153 return true;
154 }
155
WriteMiscThunk(OutputStream * out,const ArrayRef<const uint8_t> & thunk)156 bool RelativePatcher::WriteMiscThunk(OutputStream* out, const ArrayRef<const uint8_t>& thunk) {
157 if (UNLIKELY(!out->WriteFully(thunk.data(), thunk.size()))) {
158 return false;
159 }
160 size_misc_thunks_ += thunk.size();
161 return true;
162 }
163
164 } // namespace linker
165 } // namespace art
166