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