blob: 77655947fd3f405d7a9fe0d49ba747f450504a4f [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
Alex Light50fa9932015-08-10 15:30:07 -070028#ifdef ART_ENABLE_CODEGEN_x86
Vladimir Markob163bb72015-03-31 21:49:49 +010029#include "linker/x86/relative_patcher_x86.h"
Alex Light50fa9932015-08-10 15:30:07 -070030#endif
31#ifdef ART_ENABLE_CODEGEN_x86_64
Vladimir Markob163bb72015-03-31 21:49:49 +010032#include "linker/x86_64/relative_patcher_x86_64.h"
Alex Light50fa9932015-08-10 15:30:07 -070033#endif
Vladimir Markob163bb72015-03-31 21:49:49 +010034#include "output_stream.h"
35
36namespace art {
37namespace linker {
38
39std::unique_ptr<RelativePatcher> RelativePatcher::Create(
Vladimir Marko944da602016-02-19 12:27:55 +000040 InstructionSet instruction_set,
41 const InstructionSetFeatures* features,
Vladimir Markob163bb72015-03-31 21:49:49 +010042 RelativePatcherTargetProvider* provider) {
43 class RelativePatcherNone FINAL : public RelativePatcher {
44 public:
45 RelativePatcherNone() { }
46
47 uint32_t ReserveSpace(uint32_t offset,
Vladimir Marko4d23c9d2015-04-01 23:03:09 +010048 const CompiledMethod* compiled_method ATTRIBUTE_UNUSED,
49 MethodReference method_ref ATTRIBUTE_UNUSED) OVERRIDE {
Vladimir Markob163bb72015-03-31 21:49:49 +010050 return offset; // No space reserved; no patches expected.
51 }
52
Vladimir Marko71b0ddf2015-04-02 19:45:06 +010053 uint32_t ReserveSpaceEnd(uint32_t offset) OVERRIDE {
54 return offset; // No space reserved; no patches expected.
55 }
56
Vladimir Markob163bb72015-03-31 21:49:49 +010057 uint32_t WriteThunks(OutputStream* out ATTRIBUTE_UNUSED, uint32_t offset) OVERRIDE {
58 return offset; // No thunks added; no patches expected.
59 }
60
61 void PatchCall(std::vector<uint8_t>* code ATTRIBUTE_UNUSED,
62 uint32_t literal_offset ATTRIBUTE_UNUSED,
63 uint32_t patch_offset ATTRIBUTE_UNUSED,
64 uint32_t target_offset ATTRIBUTE_UNUSED) OVERRIDE {
65 LOG(FATAL) << "Unexpected relative call patch.";
66 }
67
Vladimir Markocac5a7e2016-02-22 10:39:50 +000068 void PatchPcRelativeReference(std::vector<uint8_t>* code ATTRIBUTE_UNUSED,
69 const LinkerPatch& patch ATTRIBUTE_UNUSED,
70 uint32_t patch_offset ATTRIBUTE_UNUSED,
71 uint32_t target_offset ATTRIBUTE_UNUSED) OVERRIDE {
Vladimir Markob163bb72015-03-31 21:49:49 +010072 LOG(FATAL) << "Unexpected relative dex cache array patch.";
73 }
74
75 private:
76 DISALLOW_COPY_AND_ASSIGN(RelativePatcherNone);
77 };
78
Alex Light50fa9932015-08-10 15:30:07 -070079 UNUSED(features);
80 UNUSED(provider);
Vladimir Markob163bb72015-03-31 21:49:49 +010081 switch (instruction_set) {
Alex Light50fa9932015-08-10 15:30:07 -070082#ifdef ART_ENABLE_CODEGEN_x86
Vladimir Markob163bb72015-03-31 21:49:49 +010083 case kX86:
84 return std::unique_ptr<RelativePatcher>(new X86RelativePatcher());
Alex Light50fa9932015-08-10 15:30:07 -070085#endif
86#ifdef ART_ENABLE_CODEGEN_x86_64
Vladimir Markob163bb72015-03-31 21:49:49 +010087 case kX86_64:
88 return std::unique_ptr<RelativePatcher>(new X86_64RelativePatcher());
Alex Light50fa9932015-08-10 15:30:07 -070089#endif
90#ifdef ART_ENABLE_CODEGEN_arm
Vladimir Markob163bb72015-03-31 21:49:49 +010091 case kArm:
92 // Fall through: we generate Thumb2 code for "arm".
93 case kThumb2:
94 return std::unique_ptr<RelativePatcher>(new Thumb2RelativePatcher(provider));
Alex Light50fa9932015-08-10 15:30:07 -070095#endif
96#ifdef ART_ENABLE_CODEGEN_arm64
Vladimir Markob163bb72015-03-31 21:49:49 +010097 case kArm64:
98 return std::unique_ptr<RelativePatcher>(
99 new Arm64RelativePatcher(provider, features->AsArm64InstructionSetFeatures()));
Alex Light50fa9932015-08-10 15:30:07 -0700100#endif
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700101#ifdef ART_ENABLE_CODEGEN_mips
102 case kMips:
103 return std::unique_ptr<RelativePatcher>(
104 new MipsRelativePatcher(features->AsMipsInstructionSetFeatures()));
105#endif
Vladimir Markob163bb72015-03-31 21:49:49 +0100106 default:
107 return std::unique_ptr<RelativePatcher>(new RelativePatcherNone);
Vladimir Markob163bb72015-03-31 21:49:49 +0100108 }
109}
110
111bool RelativePatcher::WriteCodeAlignment(OutputStream* out, uint32_t aligned_code_delta) {
112 static const uint8_t kPadding[] = {
113 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u
114 };
115 DCHECK_LE(aligned_code_delta, sizeof(kPadding));
116 if (UNLIKELY(!out->WriteFully(kPadding, aligned_code_delta))) {
117 return false;
118 }
119 size_code_alignment_ += aligned_code_delta;
120 return true;
121}
122
123bool RelativePatcher::WriteRelCallThunk(OutputStream* out, const ArrayRef<const uint8_t>& thunk) {
124 if (UNLIKELY(!out->WriteFully(thunk.data(), thunk.size()))) {
125 return false;
126 }
127 size_relative_call_thunks_ += thunk.size();
128 return true;
129}
130
131bool RelativePatcher::WriteMiscThunk(OutputStream* out, const ArrayRef<const uint8_t>& thunk) {
132 if (UNLIKELY(!out->WriteFully(thunk.data(), thunk.size()))) {
133 return false;
134 }
135 size_misc_thunks_ += thunk.size();
136 return true;
137}
138
139} // namespace linker
140} // namespace art