1//===- ARM64Common.cpp ----------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include "Arch/ARM64Common.h"
10
11#include "lld/Common/ErrorHandler.h"
12#include "llvm/Support/Endian.h"
13
14using namespace llvm::MachO;
15using namespace llvm::support::endian;
16using namespace lld;
17using namespace lld::macho;
18
19int64_t ARM64Common::getEmbeddedAddend(MemoryBufferRef mb, uint64_t offset,
20 const relocation_info rel) const {
21 if (rel.r_type != ARM64_RELOC_UNSIGNED &&
22 rel.r_type != ARM64_RELOC_SUBTRACTOR &&
23 rel.r_type != ARM64_RELOC_AUTHENTICATED_POINTER) {
24 // All other reloc types should use the ADDEND relocation to store their
25 // addends.
26 // TODO(gkm): extract embedded addend just so we can assert that it is 0
27 return 0;
28 }
29
30 const auto *buf = reinterpret_cast<const uint8_t *>(mb.getBufferStart());
31 const uint8_t *loc = buf + offset + rel.r_address;
32 if (rel.r_type == ARM64_RELOC_AUTHENTICATED_POINTER)
33 return static_cast<int32_t>(read32le(P: loc));
34 switch (rel.r_length) {
35 case 2:
36 return static_cast<int32_t>(read32le(P: loc));
37 case 3:
38 return read64le(P: loc);
39 default:
40 llvm_unreachable("invalid r_length");
41 }
42}
43
44static void writeValue(uint8_t *loc, const Relocation &r, uint64_t value) {
45 switch (r.length) {
46 case 2:
47 checkInt(loc, d: r, v: value, bits: 32);
48 write32le(P: loc, V: value);
49 break;
50 case 3:
51 write64le(P: loc, V: value);
52 break;
53 default:
54 llvm_unreachable("invalid r_length");
55 }
56}
57
58// For instruction relocations (load, store, add), the base
59// instruction is pre-populated in the text section. A pre-populated
60// instruction has opcode & register-operand bits set, with immediate
61// operands zeroed. We read it from text, OR-in the immediate
62// operands, then write-back the completed instruction.
63void ARM64Common::relocateOne(uint8_t *loc, const Relocation &r, uint64_t value,
64 uint64_t pc) const {
65 auto loc32 = reinterpret_cast<uint32_t *>(loc);
66 uint32_t base = ((r.length == 2) ? read32le(P: loc) : 0);
67 switch (r.type) {
68 case ARM64_RELOC_BRANCH26:
69 encodeBranch26(loc: loc32, r, base, va: value - pc);
70 break;
71 case ARM64_RELOC_SUBTRACTOR:
72 case ARM64_RELOC_UNSIGNED:
73 writeValue(loc, r, value);
74 break;
75 case ARM64_RELOC_POINTER_TO_GOT:
76 if (r.pcrel)
77 value -= pc;
78 writeValue(loc, r, value);
79 break;
80 case ARM64_RELOC_PAGE21:
81 case ARM64_RELOC_GOT_LOAD_PAGE21:
82 case ARM64_RELOC_TLVP_LOAD_PAGE21:
83 assert(r.pcrel);
84 encodePage21(loc: loc32, r, base, va: pageBits(address: value) - pageBits(address: pc));
85 break;
86 case ARM64_RELOC_PAGEOFF12:
87 case ARM64_RELOC_GOT_LOAD_PAGEOFF12:
88 case ARM64_RELOC_TLVP_LOAD_PAGEOFF12:
89 assert(!r.pcrel);
90 encodePageOff12(loc: loc32, t: r, base, va: value);
91 break;
92 default:
93 llvm_unreachable("unexpected relocation type");
94 }
95}
96
97void ARM64Common::relaxGotLoad(uint8_t *loc, uint8_t type) const {
98 // The instruction format comments below are quoted from
99 // ArmĀ® Architecture Reference Manual
100 // Armv8, for Armv8-A architecture profile
101 // ARM DDI 0487G.a (ID011921)
102 uint32_t instruction = read32le(P: loc);
103 // C6.2.132 LDR (immediate)
104 // This matches both the 64- and 32-bit variants:
105 // LDR <(X|W)t>, [<Xn|SP>{, #<pimm>}]
106 if ((instruction & 0xbfc00000) != 0xb9400000)
107 error(msg: getRelocAttrs(type).name + " reloc requires LDR instruction");
108 assert(((instruction >> 10) & 0xfff) == 0 &&
109 "non-zero embedded LDR immediate");
110 // C6.2.4 ADD (immediate)
111 // ADD <Xd|SP>, <Xn|SP>, #<imm>{, <shift>}
112 instruction = ((instruction & 0x001fffff) | 0x91000000);
113 write32le(P: loc, V: instruction);
114}
115
116void ARM64Common::handleDtraceReloc(const Symbol *sym, const Relocation &r,
117 uint8_t *loc) const {
118 assert(r.type == ARM64_RELOC_BRANCH26);
119
120 if (config->outputType == MH_OBJECT)
121 return;
122
123 if (sym->getName().starts_with(Prefix: "___dtrace_probe")) {
124 // change call site to a NOP
125 write32le(P: loc, V: 0xD503201F);
126 } else if (sym->getName().starts_with(Prefix: "___dtrace_isenabled")) {
127 // change call site to 'MOVZ X0,0'
128 write32le(P: loc, V: 0xD2800000);
129 } else {
130 error(msg: "Unrecognized dtrace symbol prefix: " + toString(*sym));
131 }
132}
133
134static void reportUnalignedLdrStr(Twine loc, uint64_t va, int align,
135 const Symbol *sym) {
136 std::string symbolHint;
137 if (sym)
138 symbolHint = " (" + toString(*sym) + ")";
139 error(msg: loc + ": " + Twine(8 * align) + "-bit LDR/STR to 0x" +
140 llvm::utohexstr(X: va) + symbolHint + " is not " + Twine(align) +
141 "-byte aligned");
142}
143
144void macho::reportUnalignedLdrStr(void *loc, const Relocation &r, uint64_t va,
145 int align) {
146 uint64_t off = reinterpret_cast<const uint8_t *>(loc) - in.bufferStart;
147 const InputSection *isec = offsetToInputSection(&off);
148 std::string locStr = isec ? isec->getLocation(off) : "(invalid location)";
149 ::reportUnalignedLdrStr(loc: locStr, va, align, sym: dyn_cast<Symbol *>(Val: r.referent));
150}
151
152void macho::reportUnalignedLdrStr(void *loc, lld::macho::SymbolDiagnostic d,
153 uint64_t va, int align) {
154 ::reportUnalignedLdrStr(loc: d.reason, va, align, sym: d.symbol);
155}
156