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