1 /*
2 * Copyright (C) 2023 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 "relative_patcher_riscv64.h"
18
19 #include "base/bit_utils.h"
20 #include "debug/method_debug_info.h"
21 #include "linker/linker_patch.h"
22
23 namespace art {
24 namespace linker {
25
Riscv64RelativePatcher(RelativePatcherThunkProvider * thunk_provider,RelativePatcherTargetProvider * target_provider,const Riscv64InstructionSetFeatures * features)26 Riscv64RelativePatcher::Riscv64RelativePatcher(
27 [[maybe_unused]] RelativePatcherThunkProvider* thunk_provider,
28 [[maybe_unused]] RelativePatcherTargetProvider* target_provider,
29 [[maybe_unused]] const Riscv64InstructionSetFeatures* features)
30 : RelativePatcher() {
31 }
32
ReserveSpace(uint32_t offset,const CompiledMethod * compiled_method,MethodReference method_ref)33 uint32_t Riscv64RelativePatcher::ReserveSpace(
34 uint32_t offset,
35 [[maybe_unused]] const CompiledMethod* compiled_method,
36 [[maybe_unused]] MethodReference method_ref) {
37 // TODO(riscv64): Reduce code size for AOT by using shared trampolines for slow path
38 // runtime calls across the entire oat file. These need space reserved here.
39 return offset;
40 }
41
ReserveSpaceEnd(uint32_t offset)42 uint32_t Riscv64RelativePatcher::ReserveSpaceEnd(uint32_t offset) {
43 // TODO(riscv64): Reduce code size for AOT by using shared trampolines for slow path
44 // runtime calls across the entire oat file. These need space reserved here.
45 return offset;
46 }
47
WriteThunks(OutputStream * out,uint32_t offset)48 uint32_t Riscv64RelativePatcher::WriteThunks([[maybe_unused]] OutputStream* out, uint32_t offset) {
49 // TODO(riscv64): Reduce code size for AOT by using shared trampolines for slow path
50 // runtime calls across the entire oat file. These need to be written here.
51 return offset;
52 }
53
PatchCall(std::vector<uint8_t> * code,uint32_t literal_offset,uint32_t patch_offset,uint32_t target_offset)54 void Riscv64RelativePatcher::PatchCall([[maybe_unused]] std::vector<uint8_t>* code,
55 [[maybe_unused]] uint32_t literal_offset,
56 [[maybe_unused]] uint32_t patch_offset,
57 [[maybe_unused]] uint32_t target_offset) {
58 // Direct calls are currently not used on any architecture.
59 UNIMPLEMENTED(FATAL) << "Unsupported direct call.";
60 }
61
PatchPcRelativeReference(std::vector<uint8_t> * code,const LinkerPatch & patch,uint32_t patch_offset,uint32_t target_offset)62 void Riscv64RelativePatcher::PatchPcRelativeReference(std::vector<uint8_t>* code,
63 const LinkerPatch& patch,
64 uint32_t patch_offset,
65 uint32_t target_offset) {
66 DCHECK_ALIGNED(patch_offset, 2u);
67 DCHECK_ALIGNED(target_offset, 2u);
68 uint32_t literal_offset = patch.LiteralOffset();
69 uint32_t insn = GetInsn(code, literal_offset);
70 uint32_t pc_insn_offset = patch.PcInsnOffset();
71 uint32_t disp = target_offset - (patch_offset - literal_offset + pc_insn_offset);
72 if (literal_offset == pc_insn_offset) {
73 // Check it's an AUIPC with imm == 0x12345 (unset).
74 DCHECK_EQ((insn & 0xfffff07fu), 0x12345017u)
75 << literal_offset << ", " << pc_insn_offset << ", 0x" << std::hex << insn;
76 insn = PatchAuipc(insn, disp);
77 } else {
78 DCHECK_EQ((insn & 0xfff00000u), 0x67800000u);
79 CHECK((insn & 0x0000707fu) == 0x00000013u || // ADD
80 (insn & 0x0000707fu) == 0x00006003u || // LWU
81 (insn & 0x0000707fu) == 0x00003003u) // LD
82 << "insn: 0x" << std::hex << insn << ", type: " << patch.GetType();
83 // Check that pc_insn_offset points to AUIPC with matching register.
84 DCHECK_EQ(GetInsn(code, pc_insn_offset) & 0x00000fffu,
85 0x00000017 | (((insn >> 15) & 0x1fu) << 7));
86 uint32_t imm12 = disp & 0xfffu; // The instruction shall sign-extend this immediate.
87 insn = (insn & ~(0xfffu << 20)) | (imm12 << 20);
88 }
89 SetInsn(code, literal_offset, insn);
90 }
91
PatchEntrypointCall(std::vector<uint8_t> * code,const LinkerPatch & patch,uint32_t patch_offset)92 void Riscv64RelativePatcher::PatchEntrypointCall([[maybe_unused]] std::vector<uint8_t>* code,
93 [[maybe_unused]] const LinkerPatch& patch,
94 [[maybe_unused]] uint32_t patch_offset) {
95 // TODO(riscv64): Reduce code size for AOT by using shared trampolines for slow path
96 // runtime calls across the entire oat file. Calls to these trapolines need to be patched here.
97 UNIMPLEMENTED(FATAL) << "Shared entrypoint trampolines are not implemented.";
98 }
99
PatchBakerReadBarrierBranch(std::vector<uint8_t> * code,const LinkerPatch & patch,uint32_t patch_offset)100 void Riscv64RelativePatcher::PatchBakerReadBarrierBranch(
101 [[maybe_unused]] std::vector<uint8_t>* code,
102 [[maybe_unused]] const LinkerPatch& patch,
103 [[maybe_unused]] uint32_t patch_offset) {
104 // Baker read barrier with introspection is not implemented.
105 // Such implementation is impractical given the short reach of conditional branches.
106 UNIMPLEMENTED(FATAL) << "Baker read barrier branches are not used on riscv64.";
107 }
108
GenerateThunkDebugInfo(uint32_t executable_offset)109 std::vector<debug::MethodDebugInfo> Riscv64RelativePatcher::GenerateThunkDebugInfo(
110 [[maybe_unused]] uint32_t executable_offset) {
111 // TODO(riscv64): Reduce code size for AOT by using shared trampolines for slow path
112 // runtime calls across the entire oat file. These need debug info generated here.
113 return {};
114 }
115
PatchAuipc(uint32_t auipc,int32_t offset)116 uint32_t Riscv64RelativePatcher::PatchAuipc(uint32_t auipc, int32_t offset) {
117 // The highest 0x800 values are out of range.
118 DCHECK_LT(offset, 0x7ffff800);
119 // Round `offset` to nearest 4KiB offset because short offset has range [-0x800, 0x800).
120 int32_t near_offset = (offset + 0x800) & ~0xfff;
121 // Extract the `imm20`.
122 uint32_t imm20 = static_cast<uint32_t>(near_offset) >> 12;
123 return (auipc & 0x00000fffu) | // Clear offset bits, keep AUIPC with destination reg.
124 (imm20 << 12); // Encode the immediate.
125 }
126
SetInsn(std::vector<uint8_t> * code,uint32_t offset,uint32_t value)127 void Riscv64RelativePatcher::SetInsn(std::vector<uint8_t>* code, uint32_t offset, uint32_t value) {
128 DCHECK_LE(offset + 4u, code->size());
129 DCHECK_ALIGNED(offset, 2u);
130 uint8_t* addr = &(*code)[offset];
131 addr[0] = (value >> 0) & 0xff;
132 addr[1] = (value >> 8) & 0xff;
133 addr[2] = (value >> 16) & 0xff;
134 addr[3] = (value >> 24) & 0xff;
135 }
136
GetInsn(ArrayRef<const uint8_t> code,uint32_t offset)137 uint32_t Riscv64RelativePatcher::GetInsn(ArrayRef<const uint8_t> code, uint32_t offset) {
138 DCHECK_LE(offset + 4u, code.size());
139 DCHECK_ALIGNED(offset, 2u);
140 const uint8_t* addr = &code[offset];
141 return
142 (static_cast<uint32_t>(addr[0]) << 0) +
143 (static_cast<uint32_t>(addr[1]) << 8) +
144 (static_cast<uint32_t>(addr[2]) << 16)+
145 (static_cast<uint32_t>(addr[3]) << 24);
146 }
147
148 template <typename Alloc>
GetInsn(std::vector<uint8_t,Alloc> * code,uint32_t offset)149 uint32_t Riscv64RelativePatcher::GetInsn(std::vector<uint8_t, Alloc>* code, uint32_t offset) {
150 return GetInsn(ArrayRef<const uint8_t>(*code), offset);
151 }
152
153 } // namespace linker
154 } // namespace art
155