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