blob: f1538b10ccdc70a703698cf3148dc8c2004285cb [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
Alex Light50fa9932015-08-10 15:30:07 -070019#ifdef ART_ENABLE_CODEGEN_arm
Vladimir Markob163bb72015-03-31 21:49:49 +010020#include "linker/arm/relative_patcher_thumb2.h"
Alex Light50fa9932015-08-10 15:30:07 -070021#endif
22#ifdef ART_ENABLE_CODEGEN_arm64
Vladimir Markob163bb72015-03-31 21:49:49 +010023#include "linker/arm64/relative_patcher_arm64.h"
Alex Light50fa9932015-08-10 15:30:07 -070024#endif
Alexey Frunzee3fb2452016-05-10 16:08:05 -070025#ifdef ART_ENABLE_CODEGEN_mips
26#include "linker/mips/relative_patcher_mips.h"
27#endif
Alexey Frunze19f6c692016-11-30 19:19:55 -080028#ifdef ART_ENABLE_CODEGEN_mips64
29#include "linker/mips64/relative_patcher_mips64.h"
30#endif
Alex Light50fa9932015-08-10 15:30:07 -070031#ifdef ART_ENABLE_CODEGEN_x86
Vladimir Markob163bb72015-03-31 21:49:49 +010032#include "linker/x86/relative_patcher_x86.h"
Alex Light50fa9932015-08-10 15:30:07 -070033#endif
34#ifdef ART_ENABLE_CODEGEN_x86_64
Vladimir Markob163bb72015-03-31 21:49:49 +010035#include "linker/x86_64/relative_patcher_x86_64.h"
Alex Light50fa9932015-08-10 15:30:07 -070036#endif
Vladimir Markob163bb72015-03-31 21:49:49 +010037#include "output_stream.h"
38
39namespace art {
40namespace linker {
41
42std::unique_ptr<RelativePatcher> RelativePatcher::Create(
Vladimir Marko944da602016-02-19 12:27:55 +000043 InstructionSet instruction_set,
44 const InstructionSetFeatures* features,
Vladimir Markob163bb72015-03-31 21:49:49 +010045 RelativePatcherTargetProvider* provider) {
46 class RelativePatcherNone FINAL : public RelativePatcher {
47 public:
48 RelativePatcherNone() { }
49
50 uint32_t ReserveSpace(uint32_t offset,
Vladimir Marko4d23c9d2015-04-01 23:03:09 +010051 const CompiledMethod* compiled_method ATTRIBUTE_UNUSED,
52 MethodReference method_ref ATTRIBUTE_UNUSED) OVERRIDE {
Vladimir Markob163bb72015-03-31 21:49:49 +010053 return offset; // No space reserved; no patches expected.
54 }
55
Vladimir Marko71b0ddf2015-04-02 19:45:06 +010056 uint32_t ReserveSpaceEnd(uint32_t offset) OVERRIDE {
57 return offset; // No space reserved; no patches expected.
58 }
59
Vladimir Markob163bb72015-03-31 21:49:49 +010060 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
Vladimir Markocac5a7e2016-02-22 10:39:50 +000071 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 {
Vladimir Markob163bb72015-03-31 21:49:49 +010075 LOG(FATAL) << "Unexpected relative dex cache array patch.";
76 }
77
78 private:
79 DISALLOW_COPY_AND_ASSIGN(RelativePatcherNone);
80 };
81
Alex Light50fa9932015-08-10 15:30:07 -070082 UNUSED(features);
83 UNUSED(provider);
Vladimir Markob163bb72015-03-31 21:49:49 +010084 switch (instruction_set) {
Alex Light50fa9932015-08-10 15:30:07 -070085#ifdef ART_ENABLE_CODEGEN_x86
Vladimir Markob163bb72015-03-31 21:49:49 +010086 case kX86:
87 return std::unique_ptr<RelativePatcher>(new X86RelativePatcher());
Alex Light50fa9932015-08-10 15:30:07 -070088#endif
89#ifdef ART_ENABLE_CODEGEN_x86_64
Vladimir Markob163bb72015-03-31 21:49:49 +010090 case kX86_64:
91 return std::unique_ptr<RelativePatcher>(new X86_64RelativePatcher());
Alex Light50fa9932015-08-10 15:30:07 -070092#endif
93#ifdef ART_ENABLE_CODEGEN_arm
Vladimir Markob163bb72015-03-31 21:49:49 +010094 case kArm:
95 // Fall through: we generate Thumb2 code for "arm".
96 case kThumb2:
97 return std::unique_ptr<RelativePatcher>(new Thumb2RelativePatcher(provider));
Alex Light50fa9932015-08-10 15:30:07 -070098#endif
99#ifdef ART_ENABLE_CODEGEN_arm64
Vladimir Markob163bb72015-03-31 21:49:49 +0100100 case kArm64:
101 return std::unique_ptr<RelativePatcher>(
102 new Arm64RelativePatcher(provider, features->AsArm64InstructionSetFeatures()));
Alex Light50fa9932015-08-10 15:30:07 -0700103#endif
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700104#ifdef ART_ENABLE_CODEGEN_mips
105 case kMips:
106 return std::unique_ptr<RelativePatcher>(
107 new MipsRelativePatcher(features->AsMipsInstructionSetFeatures()));
108#endif
Alexey Frunze19f6c692016-11-30 19:19:55 -0800109#ifdef ART_ENABLE_CODEGEN_mips64
110 case kMips64:
111 return std::unique_ptr<RelativePatcher>(new Mips64RelativePatcher());
112#endif
Vladimir Markob163bb72015-03-31 21:49:49 +0100113 default:
114 return std::unique_ptr<RelativePatcher>(new RelativePatcherNone);
Vladimir Markob163bb72015-03-31 21:49:49 +0100115 }
116}
117
118bool RelativePatcher::WriteCodeAlignment(OutputStream* out, uint32_t aligned_code_delta) {
119 static const uint8_t kPadding[] = {
120 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u
121 };
122 DCHECK_LE(aligned_code_delta, sizeof(kPadding));
123 if (UNLIKELY(!out->WriteFully(kPadding, aligned_code_delta))) {
124 return false;
125 }
126 size_code_alignment_ += aligned_code_delta;
127 return true;
128}
129
130bool RelativePatcher::WriteRelCallThunk(OutputStream* out, const ArrayRef<const uint8_t>& thunk) {
131 if (UNLIKELY(!out->WriteFully(thunk.data(), thunk.size()))) {
132 return false;
133 }
134 size_relative_call_thunks_ += thunk.size();
135 return true;
136}
137
138bool RelativePatcher::WriteMiscThunk(OutputStream* out, const ArrayRef<const uint8_t>& thunk) {
139 if (UNLIKELY(!out->WriteFully(thunk.data(), thunk.size()))) {
140 return false;
141 }
142 size_misc_thunks_ += thunk.size();
143 return true;
144}
145
146} // namespace linker
147} // namespace art