1/*
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 thunks if needed before a method, return adjusted offset.
86  virtual uint32_t ReserveSpace(uint32_t offset,
87                                const CompiledMethod* compiled_method,
88                                MethodReference method_ref) = 0;
89
90  // Reserve space for thunks if needed after the last method, return adjusted offset.
91  // The caller may use this method to preemptively force thunk space reservation and
92  // then resume reservation for more methods. This is useful when there is a gap in
93  // the .text segment, for example when going to the next oat file for multi-image.
94  virtual uint32_t ReserveSpaceEnd(uint32_t offset) = 0;
95
96  // Write relative call thunks if needed, return adjusted offset. Returns 0 on write failure.
97  virtual uint32_t WriteThunks(OutputStream* out, uint32_t offset) = 0;
98
99  // Patch method code. The input displacement is relative to the patched location,
100  // the patcher may need to adjust it if the correct base is different.
101  virtual void PatchCall(std::vector<uint8_t>* code,
102                         uint32_t literal_offset,
103                         uint32_t patch_offset,
104                         uint32_t target_offset) = 0;
105
106  // Patch a reference to a dex cache location.
107  virtual void PatchPcRelativeReference(std::vector<uint8_t>* code,
108                                        const LinkerPatch& patch,
109                                        uint32_t patch_offset,
110                                        uint32_t target_offset) = 0;
111
112 protected:
113  RelativePatcher()
114      : size_code_alignment_(0u),
115        size_relative_call_thunks_(0u),
116        size_misc_thunks_(0u) {
117  }
118
119  bool WriteCodeAlignment(OutputStream* out, uint32_t aligned_code_delta);
120  bool WriteRelCallThunk(OutputStream* out, const ArrayRef<const uint8_t>& thunk);
121  bool WriteMiscThunk(OutputStream* out, const ArrayRef<const uint8_t>& thunk);
122
123 private:
124  uint32_t size_code_alignment_;
125  uint32_t size_relative_call_thunks_;
126  uint32_t size_misc_thunks_;
127
128  DISALLOW_COPY_AND_ASSIGN(RelativePatcher);
129};
130
131}  // namespace linker
132}  // namespace art
133
134#endif  // ART_COMPILER_LINKER_RELATIVE_PATCHER_H_
135