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