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 "linker/arm/relative_patcher_thumb2.h"
20 #include "linker/arm64/relative_patcher_arm64.h"
21 #include "linker/x86/relative_patcher_x86.h"
22 #include "linker/x86_64/relative_patcher_x86_64.h"
23 #include "output_stream.h"
24
25 namespace art {
26 namespace linker {
27
Create(InstructionSet instruction_set,const InstructionSetFeatures * features,RelativePatcherTargetProvider * provider)28 std::unique_ptr<RelativePatcher> RelativePatcher::Create(
29 InstructionSet instruction_set, const InstructionSetFeatures* features,
30 RelativePatcherTargetProvider* provider) {
31 class RelativePatcherNone FINAL : public RelativePatcher {
32 public:
33 RelativePatcherNone() { }
34
35 uint32_t ReserveSpace(uint32_t offset,
36 const CompiledMethod* compiled_method ATTRIBUTE_UNUSED,
37 MethodReference method_ref ATTRIBUTE_UNUSED) OVERRIDE {
38 return offset; // No space reserved; no patches expected.
39 }
40
41 uint32_t ReserveSpaceEnd(uint32_t offset) OVERRIDE {
42 return offset; // No space reserved; no patches expected.
43 }
44
45 uint32_t WriteThunks(OutputStream* out ATTRIBUTE_UNUSED, uint32_t offset) OVERRIDE {
46 return offset; // No thunks added; no patches expected.
47 }
48
49 void PatchCall(std::vector<uint8_t>* code ATTRIBUTE_UNUSED,
50 uint32_t literal_offset ATTRIBUTE_UNUSED,
51 uint32_t patch_offset ATTRIBUTE_UNUSED,
52 uint32_t target_offset ATTRIBUTE_UNUSED) OVERRIDE {
53 LOG(FATAL) << "Unexpected relative call patch.";
54 }
55
56 virtual void PatchDexCacheReference(std::vector<uint8_t>* code ATTRIBUTE_UNUSED,
57 const LinkerPatch& patch ATTRIBUTE_UNUSED,
58 uint32_t patch_offset ATTRIBUTE_UNUSED,
59 uint32_t target_offset ATTRIBUTE_UNUSED) {
60 LOG(FATAL) << "Unexpected relative dex cache array patch.";
61 }
62
63 private:
64 DISALLOW_COPY_AND_ASSIGN(RelativePatcherNone);
65 };
66
67 switch (instruction_set) {
68 case kX86:
69 return std::unique_ptr<RelativePatcher>(new X86RelativePatcher());
70 case kX86_64:
71 return std::unique_ptr<RelativePatcher>(new X86_64RelativePatcher());
72 case kArm:
73 // Fall through: we generate Thumb2 code for "arm".
74 case kThumb2:
75 return std::unique_ptr<RelativePatcher>(new Thumb2RelativePatcher(provider));
76 case kArm64:
77 return std::unique_ptr<RelativePatcher>(
78 new Arm64RelativePatcher(provider, features->AsArm64InstructionSetFeatures()));
79 default:
80 return std::unique_ptr<RelativePatcher>(new RelativePatcherNone);
81 }
82 }
83
WriteCodeAlignment(OutputStream * out,uint32_t aligned_code_delta)84 bool RelativePatcher::WriteCodeAlignment(OutputStream* out, uint32_t aligned_code_delta) {
85 static const uint8_t kPadding[] = {
86 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u
87 };
88 DCHECK_LE(aligned_code_delta, sizeof(kPadding));
89 if (UNLIKELY(!out->WriteFully(kPadding, aligned_code_delta))) {
90 return false;
91 }
92 size_code_alignment_ += aligned_code_delta;
93 return true;
94 }
95
WriteRelCallThunk(OutputStream * out,const ArrayRef<const uint8_t> & thunk)96 bool RelativePatcher::WriteRelCallThunk(OutputStream* out, const ArrayRef<const uint8_t>& thunk) {
97 if (UNLIKELY(!out->WriteFully(thunk.data(), thunk.size()))) {
98 return false;
99 }
100 size_relative_call_thunks_ += thunk.size();
101 return true;
102 }
103
WriteMiscThunk(OutputStream * out,const ArrayRef<const uint8_t> & thunk)104 bool RelativePatcher::WriteMiscThunk(OutputStream* out, const ArrayRef<const uint8_t>& thunk) {
105 if (UNLIKELY(!out->WriteFully(thunk.data(), thunk.size()))) {
106 return false;
107 }
108 size_misc_thunks_ += thunk.size();
109 return true;
110 }
111
112 } // namespace linker
113 } // namespace art
114