blob: ee494539385c4761cf9cd79b71ddd5949fcc5fe1 [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
Vladimir Markof4f2daa2017-03-20 18:26:59 +000078 void PatchBakerReadBarrierBranch(std::vector<uint8_t>* code ATTRIBUTE_UNUSED,
79 const LinkerPatch& patch ATTRIBUTE_UNUSED,
80 uint32_t patch_offset ATTRIBUTE_UNUSED) {
81 LOG(FATAL) << "Unexpected baker read barrier branch patch.";
82 }
83
Vladimir Markob163bb72015-03-31 21:49:49 +010084 private:
85 DISALLOW_COPY_AND_ASSIGN(RelativePatcherNone);
86 };
87
Alex Light50fa9932015-08-10 15:30:07 -070088 UNUSED(features);
89 UNUSED(provider);
Vladimir Markob163bb72015-03-31 21:49:49 +010090 switch (instruction_set) {
Alex Light50fa9932015-08-10 15:30:07 -070091#ifdef ART_ENABLE_CODEGEN_x86
Vladimir Markob163bb72015-03-31 21:49:49 +010092 case kX86:
93 return std::unique_ptr<RelativePatcher>(new X86RelativePatcher());
Alex Light50fa9932015-08-10 15:30:07 -070094#endif
95#ifdef ART_ENABLE_CODEGEN_x86_64
Vladimir Markob163bb72015-03-31 21:49:49 +010096 case kX86_64:
97 return std::unique_ptr<RelativePatcher>(new X86_64RelativePatcher());
Alex Light50fa9932015-08-10 15:30:07 -070098#endif
99#ifdef ART_ENABLE_CODEGEN_arm
Vladimir Markob163bb72015-03-31 21:49:49 +0100100 case kArm:
101 // Fall through: we generate Thumb2 code for "arm".
102 case kThumb2:
103 return std::unique_ptr<RelativePatcher>(new Thumb2RelativePatcher(provider));
Alex Light50fa9932015-08-10 15:30:07 -0700104#endif
105#ifdef ART_ENABLE_CODEGEN_arm64
Vladimir Markob163bb72015-03-31 21:49:49 +0100106 case kArm64:
107 return std::unique_ptr<RelativePatcher>(
108 new Arm64RelativePatcher(provider, features->AsArm64InstructionSetFeatures()));
Alex Light50fa9932015-08-10 15:30:07 -0700109#endif
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700110#ifdef ART_ENABLE_CODEGEN_mips
111 case kMips:
112 return std::unique_ptr<RelativePatcher>(
113 new MipsRelativePatcher(features->AsMipsInstructionSetFeatures()));
114#endif
Alexey Frunze19f6c692016-11-30 19:19:55 -0800115#ifdef ART_ENABLE_CODEGEN_mips64
116 case kMips64:
117 return std::unique_ptr<RelativePatcher>(new Mips64RelativePatcher());
118#endif
Vladimir Markob163bb72015-03-31 21:49:49 +0100119 default:
120 return std::unique_ptr<RelativePatcher>(new RelativePatcherNone);
Vladimir Markob163bb72015-03-31 21:49:49 +0100121 }
122}
123
124bool RelativePatcher::WriteCodeAlignment(OutputStream* out, uint32_t aligned_code_delta) {
125 static const uint8_t kPadding[] = {
126 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u
127 };
128 DCHECK_LE(aligned_code_delta, sizeof(kPadding));
129 if (UNLIKELY(!out->WriteFully(kPadding, aligned_code_delta))) {
130 return false;
131 }
132 size_code_alignment_ += aligned_code_delta;
133 return true;
134}
135
Vladimir Markof4f2daa2017-03-20 18:26:59 +0000136bool RelativePatcher::WriteThunk(OutputStream* out, const ArrayRef<const uint8_t>& thunk) {
Vladimir Markob163bb72015-03-31 21:49:49 +0100137 if (UNLIKELY(!out->WriteFully(thunk.data(), thunk.size()))) {
138 return false;
139 }
140 size_relative_call_thunks_ += thunk.size();
141 return true;
142}
143
144bool RelativePatcher::WriteMiscThunk(OutputStream* out, const ArrayRef<const uint8_t>& thunk) {
145 if (UNLIKELY(!out->WriteFully(thunk.data(), thunk.size()))) {
146 return false;
147 }
148 size_misc_thunks_ += thunk.size();
149 return true;
150}
151
152} // namespace linker
153} // namespace art