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