blob: dbb18cc9192f789409e6a5373bd61a9c7536b51c [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#ifndef ART_COMPILER_LINKER_RELATIVE_PATCHER_H_
18#define ART_COMPILER_LINKER_RELATIVE_PATCHER_H_
19
20#include <vector>
21
22#include "arch/instruction_set.h"
23#include "arch/instruction_set_features.h"
24#include "base/macros.h"
25#include "method_reference.h"
26#include "utils/array_ref.h"
27
28namespace art {
29
30class CompiledMethod;
31class LinkerPatch;
32class OutputStream;
33
34namespace linker {
35
36/**
37 * @class RelativePatcherTargetProvider
38 * @brief Interface for providing method offsets for relative call targets.
39 */
40class RelativePatcherTargetProvider {
41 public:
42 /**
43 * Find the offset of the target method of a relative call if known.
44 *
45 * The process of assigning target method offsets includes calls to the relative patcher's
46 * ReserveSpace() which in turn can use FindMethodOffset() to determine if a method already
47 * has an offset assigned and, if so, what's that offset. If the offset has not yet been
48 * assigned or if it's too far for the particular architecture's relative call,
49 * ReserveSpace() may need to allocate space for a special dispatch thunk.
50 *
51 * @param ref the target method of the relative call.
52 * @return true in the first element of the pair if the method was found, false otherwise;
53 * if found, the second element specifies the offset.
54 */
55 virtual std::pair<bool, uint32_t> FindMethodOffset(MethodReference ref) = 0;
56
57 protected:
58 virtual ~RelativePatcherTargetProvider() { }
59};
60
61/**
62 * @class RelativePatcher
63 * @brief Interface for architecture-specific link-time patching of PC-relative references.
64 */
65class RelativePatcher {
66 public:
67 static std::unique_ptr<RelativePatcher> Create(
68 InstructionSet instruction_set, const InstructionSetFeatures* features,
69 RelativePatcherTargetProvider* provider);
70
71 virtual ~RelativePatcher() { }
72
73 uint32_t CodeAlignmentSize() const {
74 return size_code_alignment_;
75 }
76
77 uint32_t RelativeCallThunksSize() const {
78 return size_relative_call_thunks_;
79 }
80
81 uint32_t MiscThunksSize() const {
82 return size_misc_thunks_;
83 }
84
85 // Reserve space for relative call thunks if needed, return adjusted offset. After all methods
86 // of a class have been processed it's called one last time with compiled_method == nullptr.
87 virtual uint32_t ReserveSpace(uint32_t offset, const CompiledMethod* compiled_method) = 0;
88
89 // Write relative call thunks if needed, return adjusted offset.
90 virtual uint32_t WriteThunks(OutputStream* out, uint32_t offset) = 0;
91
92 // Patch method code. The input displacement is relative to the patched location,
93 // the patcher may need to adjust it if the correct base is different.
94 virtual void PatchCall(std::vector<uint8_t>* code, uint32_t literal_offset,
95 uint32_t patch_offset, uint32_t target_offset) = 0;
96
97 // Patch a reference to a dex cache location.
98 virtual void PatchDexCacheReference(std::vector<uint8_t>* code, const LinkerPatch& patch,
99 uint32_t patch_offset, uint32_t target_offset) = 0;
100
101 protected:
102 RelativePatcher()
103 : size_code_alignment_(0u),
104 size_relative_call_thunks_(0u),
105 size_misc_thunks_(0u) {
106 }
107
108 bool WriteCodeAlignment(OutputStream* out, uint32_t aligned_code_delta);
109 bool WriteRelCallThunk(OutputStream* out, const ArrayRef<const uint8_t>& thunk);
110 bool WriteMiscThunk(OutputStream* out, const ArrayRef<const uint8_t>& thunk);
111
112 private:
113 uint32_t size_code_alignment_;
114 uint32_t size_relative_call_thunks_;
115 uint32_t size_misc_thunks_;
116
117 DISALLOW_COPY_AND_ASSIGN(RelativePatcher);
118};
119
120} // namespace linker
121} // namespace art
122
123#endif // ART_COMPILER_LINKER_RELATIVE_PATCHER_H_