blob: 82702dcf251637b764aec9eb07e811a5c99c5ecd [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
25#ifdef ART_ENABLE_CODEGEN_x86
Vladimir Markob163bb72015-03-31 21:49:49 +010026#include "linker/x86/relative_patcher_x86.h"
Alex Light50fa9932015-08-10 15:30:07 -070027#endif
28#ifdef ART_ENABLE_CODEGEN_x86_64
Vladimir Markob163bb72015-03-31 21:49:49 +010029#include "linker/x86_64/relative_patcher_x86_64.h"
Alex Light50fa9932015-08-10 15:30:07 -070030#endif
Vladimir Markob163bb72015-03-31 21:49:49 +010031#include "output_stream.h"
32
33namespace art {
34namespace linker {
35
36std::unique_ptr<RelativePatcher> RelativePatcher::Create(
37 InstructionSet instruction_set, const InstructionSetFeatures* features,
38 RelativePatcherTargetProvider* provider) {
39 class RelativePatcherNone FINAL : public RelativePatcher {
40 public:
41 RelativePatcherNone() { }
42
43 uint32_t ReserveSpace(uint32_t offset,
Vladimir Marko4d23c9d2015-04-01 23:03:09 +010044 const CompiledMethod* compiled_method ATTRIBUTE_UNUSED,
45 MethodReference method_ref ATTRIBUTE_UNUSED) OVERRIDE {
Vladimir Markob163bb72015-03-31 21:49:49 +010046 return offset; // No space reserved; no patches expected.
47 }
48
Vladimir Marko71b0ddf2015-04-02 19:45:06 +010049 uint32_t ReserveSpaceEnd(uint32_t offset) OVERRIDE {
50 return offset; // No space reserved; no patches expected.
51 }
52
Vladimir Markob163bb72015-03-31 21:49:49 +010053 uint32_t WriteThunks(OutputStream* out ATTRIBUTE_UNUSED, uint32_t offset) OVERRIDE {
54 return offset; // No thunks added; no patches expected.
55 }
56
57 void PatchCall(std::vector<uint8_t>* code ATTRIBUTE_UNUSED,
58 uint32_t literal_offset ATTRIBUTE_UNUSED,
59 uint32_t patch_offset ATTRIBUTE_UNUSED,
60 uint32_t target_offset ATTRIBUTE_UNUSED) OVERRIDE {
61 LOG(FATAL) << "Unexpected relative call patch.";
62 }
63
64 virtual void PatchDexCacheReference(std::vector<uint8_t>* code ATTRIBUTE_UNUSED,
65 const LinkerPatch& patch ATTRIBUTE_UNUSED,
66 uint32_t patch_offset ATTRIBUTE_UNUSED,
67 uint32_t target_offset ATTRIBUTE_UNUSED) {
68 LOG(FATAL) << "Unexpected relative dex cache array patch.";
69 }
70
71 private:
72 DISALLOW_COPY_AND_ASSIGN(RelativePatcherNone);
73 };
74
Alex Light50fa9932015-08-10 15:30:07 -070075 UNUSED(features);
76 UNUSED(provider);
Vladimir Markob163bb72015-03-31 21:49:49 +010077 switch (instruction_set) {
Alex Light50fa9932015-08-10 15:30:07 -070078#ifdef ART_ENABLE_CODEGEN_x86
Vladimir Markob163bb72015-03-31 21:49:49 +010079 case kX86:
80 return std::unique_ptr<RelativePatcher>(new X86RelativePatcher());
Alex Light50fa9932015-08-10 15:30:07 -070081#endif
82#ifdef ART_ENABLE_CODEGEN_x86_64
Vladimir Markob163bb72015-03-31 21:49:49 +010083 case kX86_64:
84 return std::unique_ptr<RelativePatcher>(new X86_64RelativePatcher());
Alex Light50fa9932015-08-10 15:30:07 -070085#endif
86#ifdef ART_ENABLE_CODEGEN_arm
Vladimir Markob163bb72015-03-31 21:49:49 +010087 case kArm:
88 // Fall through: we generate Thumb2 code for "arm".
89 case kThumb2:
90 return std::unique_ptr<RelativePatcher>(new Thumb2RelativePatcher(provider));
Alex Light50fa9932015-08-10 15:30:07 -070091#endif
92#ifdef ART_ENABLE_CODEGEN_arm64
Vladimir Markob163bb72015-03-31 21:49:49 +010093 case kArm64:
94 return std::unique_ptr<RelativePatcher>(
95 new Arm64RelativePatcher(provider, features->AsArm64InstructionSetFeatures()));
Alex Light50fa9932015-08-10 15:30:07 -070096#endif
Vladimir Markob163bb72015-03-31 21:49:49 +010097 default:
98 return std::unique_ptr<RelativePatcher>(new RelativePatcherNone);
Vladimir Markob163bb72015-03-31 21:49:49 +010099 }
100}
101
102bool RelativePatcher::WriteCodeAlignment(OutputStream* out, uint32_t aligned_code_delta) {
103 static const uint8_t kPadding[] = {
104 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u
105 };
106 DCHECK_LE(aligned_code_delta, sizeof(kPadding));
107 if (UNLIKELY(!out->WriteFully(kPadding, aligned_code_delta))) {
108 return false;
109 }
110 size_code_alignment_ += aligned_code_delta;
111 return true;
112}
113
114bool RelativePatcher::WriteRelCallThunk(OutputStream* out, const ArrayRef<const uint8_t>& thunk) {
115 if (UNLIKELY(!out->WriteFully(thunk.data(), thunk.size()))) {
116 return false;
117 }
118 size_relative_call_thunks_ += thunk.size();
119 return true;
120}
121
122bool RelativePatcher::WriteMiscThunk(OutputStream* out, const ArrayRef<const uint8_t>& thunk) {
123 if (UNLIKELY(!out->WriteFully(thunk.data(), thunk.size()))) {
124 return false;
125 }
126 size_misc_thunks_ += thunk.size();
127 return true;
128}
129
130} // namespace linker
131} // namespace art