summaryrefslogtreecommitdiff
path: root/dex2oat/linker/relative_patcher.cc
blob: 231c4a590ce4fad9a3b9fa330720a0b3eaea50b2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/*
 * Copyright (C) 2015 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "linker/relative_patcher.h"

#include "debug/method_debug_info.h"
#ifdef ART_ENABLE_CODEGEN_arm
#include "linker/arm/relative_patcher_thumb2.h"
#endif
#ifdef ART_ENABLE_CODEGEN_arm64
#include "linker/arm64/relative_patcher_arm64.h"
#endif
#ifdef ART_ENABLE_CODEGEN_riscv64
#include "linker/riscv64/relative_patcher_riscv64.h"
#endif
#ifdef ART_ENABLE_CODEGEN_x86
#include "linker/x86/relative_patcher_x86.h"
#endif
#ifdef ART_ENABLE_CODEGEN_x86_64
#include "linker/x86_64/relative_patcher_x86_64.h"
#endif
#include "stream/output_stream.h"

namespace art {
namespace linker {

std::unique_ptr<RelativePatcher> RelativePatcher::Create(
    InstructionSet instruction_set,
    const InstructionSetFeatures* features,
    RelativePatcherThunkProvider* thunk_provider,
    RelativePatcherTargetProvider* target_provider) {
  class RelativePatcherNone final : public RelativePatcher {
   public:
    RelativePatcherNone() { }

    uint32_t ReserveSpace(uint32_t offset,
                          [[maybe_unused]] const CompiledMethod* compiled_method,
                          [[maybe_unused]] MethodReference method_ref) override {
      return offset;  // No space reserved; no patches expected.
    }

    uint32_t ReserveSpaceEnd(uint32_t offset) override {
      return offset;  // No space reserved; no patches expected.
    }

    uint32_t WriteThunks([[maybe_unused]] OutputStream* out, uint32_t offset) override {
      return offset;  // No thunks added; no patches expected.
    }

    void PatchCall([[maybe_unused]] std::vector<uint8_t>* code,
                   [[maybe_unused]] uint32_t literal_offset,
                   [[maybe_unused]] uint32_t patch_offset,
                   [[maybe_unused]] uint32_t target_offset) override {
      LOG(FATAL) << "Unexpected relative call patch.";
    }

    void PatchPcRelativeReference([[maybe_unused]] std::vector<uint8_t>* code,
                                  [[maybe_unused]] const LinkerPatch& patch,
                                  [[maybe_unused]] uint32_t patch_offset,
                                  [[maybe_unused]] uint32_t target_offset) override {
      LOG(FATAL) << "Unexpected relative dex cache array patch.";
    }

    void PatchEntrypointCall([[maybe_unused]] std::vector<uint8_t>* code,
                             [[maybe_unused]] const LinkerPatch& patch,
                             [[maybe_unused]] uint32_t patch_offset) override {
      LOG(FATAL) << "Unexpected entrypoint call patch.";
    }

    void PatchBakerReadBarrierBranch([[maybe_unused]] std::vector<uint8_t>* code,
                                     [[maybe_unused]] const LinkerPatch& patch,
                                     [[maybe_unused]] uint32_t patch_offset) override {
      LOG(FATAL) << "Unexpected baker read barrier branch patch.";
    }

    std::vector<debug::MethodDebugInfo> GenerateThunkDebugInfo(
        [[maybe_unused]] uint32_t executable_offset) override {
      return std::vector<debug::MethodDebugInfo>();  // No thunks added.
    }

   private:
    DISALLOW_COPY_AND_ASSIGN(RelativePatcherNone);
  };

  UNUSED(features);
  UNUSED(thunk_provider);
  UNUSED(target_provider);
  switch (instruction_set) {
#ifdef ART_ENABLE_CODEGEN_x86
    case InstructionSet::kX86:
      return std::unique_ptr<RelativePatcher>(new X86RelativePatcher());
#endif
#ifdef ART_ENABLE_CODEGEN_x86_64
    case InstructionSet::kX86_64:
      return std::unique_ptr<RelativePatcher>(new X86_64RelativePatcher());
#endif
#ifdef ART_ENABLE_CODEGEN_arm
    case InstructionSet::kArm:
      // Fall through: we generate Thumb2 code for "arm".
    case InstructionSet::kThumb2:
      return std::unique_ptr<RelativePatcher>(
          new Thumb2RelativePatcher(thunk_provider, target_provider));
#endif
#ifdef ART_ENABLE_CODEGEN_arm64
    case InstructionSet::kArm64:
      return std::unique_ptr<RelativePatcher>(
          new Arm64RelativePatcher(thunk_provider,
                                   target_provider,
                                   features->AsArm64InstructionSetFeatures()));
#endif
#ifdef ART_ENABLE_CODEGEN_riscv64
    case InstructionSet::kRiscv64:
      return std::unique_ptr<RelativePatcher>(
          new Riscv64RelativePatcher(thunk_provider,
                                     target_provider,
                                     features->AsRiscv64InstructionSetFeatures()));
#endif
    default:
      return std::unique_ptr<RelativePatcher>(new RelativePatcherNone);
  }
}

bool RelativePatcher::WriteCodeAlignment(OutputStream* out, uint32_t aligned_code_delta) {
  static const uint8_t kPadding[] = {
      0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u
  };
  DCHECK_LE(aligned_code_delta, sizeof(kPadding));
  if (UNLIKELY(!out->WriteFully(kPadding, aligned_code_delta))) {
    return false;
  }
  size_code_alignment_ += aligned_code_delta;
  return true;
}

bool RelativePatcher::WriteThunk(OutputStream* out, const ArrayRef<const uint8_t>& thunk) {
  if (UNLIKELY(!out->WriteFully(thunk.data(), thunk.size()))) {
    return false;
  }
  size_relative_call_thunks_ += thunk.size();
  return true;
}

bool RelativePatcher::WriteMiscThunk(OutputStream* out, const ArrayRef<const uint8_t>& thunk) {
  if (UNLIKELY(!out->WriteFully(thunk.data(), thunk.size()))) {
    return false;
  }
  size_misc_thunks_ += thunk.size();
  return true;
}

}  // namespace linker
}  // namespace art