1 //===- X86_64.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 "InputFiles.h"
10 #include "Symbols.h"
11 #include "SyntheticSections.h"
12 #include "Target.h"
13 
14 #include "lld/Common/ErrorHandler.h"
15 #include "mach-o/compact_unwind_encoding.h"
16 #include "llvm/BinaryFormat/MachO.h"
17 #include "llvm/Support/Endian.h"
18 
19 using namespace llvm::MachO;
20 using namespace llvm::support::endian;
21 using namespace lld;
22 using namespace lld::macho;
23 
24 namespace {
25 
26 struct X86_64 : TargetInfo {
27   X86_64();
28 
29   int64_t getEmbeddedAddend(MemoryBufferRef, uint64_t offset,
30                             const relocation_info) const override;
31   void relocateOne(uint8_t *loc, const Reloc &, uint64_t va,
32                    uint64_t relocVA) const override;
33 
34   void writeStub(uint8_t *buf, const Symbol &) const override;
35   void writeStubHelperHeader(uint8_t *buf) const override;
36   void writeStubHelperEntry(uint8_t *buf, const Symbol &,
37                             uint64_t entryAddr) const override;
38 
39   void relaxGotLoad(uint8_t *loc, uint8_t type) const override;
40   uint64_t getPageSize() const override { return 4 * 1024; }
41 
42   void handleDtraceReloc(const Symbol *sym, const Reloc &r,
43                          uint8_t *loc) const override;
44 };
45 } // namespace
46 
47 static constexpr std::array<RelocAttrs, 10> relocAttrsArray{{
48 #define B(x) RelocAttrBits::x
49     {"UNSIGNED",
50      B(UNSIGNED) | B(ABSOLUTE) | B(EXTERN) | B(LOCAL) | B(BYTE4) | B(BYTE8)},
51     {"SIGNED", B(PCREL) | B(EXTERN) | B(LOCAL) | B(BYTE4)},
52     {"BRANCH", B(PCREL) | B(EXTERN) | B(BRANCH) | B(BYTE4)},
53     {"GOT_LOAD", B(PCREL) | B(EXTERN) | B(GOT) | B(LOAD) | B(BYTE4)},
54     {"GOT", B(PCREL) | B(EXTERN) | B(GOT) | B(POINTER) | B(BYTE4)},
55     {"SUBTRACTOR", B(SUBTRAHEND) | B(EXTERN) | B(BYTE4) | B(BYTE8)},
56     {"SIGNED_1", B(PCREL) | B(EXTERN) | B(LOCAL) | B(BYTE4)},
57     {"SIGNED_2", B(PCREL) | B(EXTERN) | B(LOCAL) | B(BYTE4)},
58     {"SIGNED_4", B(PCREL) | B(EXTERN) | B(LOCAL) | B(BYTE4)},
59     {"TLV", B(PCREL) | B(EXTERN) | B(TLV) | B(LOAD) | B(BYTE4)},
60 #undef B
61 }};
62 
63 static int pcrelOffset(uint8_t type) {
64   switch (type) {
65   case X86_64_RELOC_SIGNED_1:
66     return 1;
67   case X86_64_RELOC_SIGNED_2:
68     return 2;
69   case X86_64_RELOC_SIGNED_4:
70     return 4;
71   default:
72     return 0;
73   }
74 }
75 
76 int64_t X86_64::getEmbeddedAddend(MemoryBufferRef mb, uint64_t offset,
77                                   relocation_info rel) const {
78   auto *buf = reinterpret_cast<const uint8_t *>(mb.getBufferStart());
79   const uint8_t *loc = buf + offset + rel.r_address;
80 
81   switch (rel.r_length) {
82   case 2:
83     return static_cast<int32_t>(read32le(loc)) + pcrelOffset(rel.r_type);
84   case 3:
85     return read64le(loc) + pcrelOffset(rel.r_type);
86   default:
87     llvm_unreachable("invalid r_length");
88   }
89 }
90 
91 void X86_64::relocateOne(uint8_t *loc, const Reloc &r, uint64_t value,
92                          uint64_t relocVA) const {
93   if (r.pcrel) {
94     uint64_t pc = relocVA + 4 + pcrelOffset(r.type);
95     value -= pc;
96   }
97 
98   switch (r.length) {
99   case 2:
100     if (r.type == X86_64_RELOC_UNSIGNED)
101       checkUInt(loc, r, value, 32);
102     else
103       checkInt(loc, r, value, 32);
104     write32le(loc, value);
105     break;
106   case 3:
107     write64le(loc, value);
108     break;
109   default:
110     llvm_unreachable("invalid r_length");
111   }
112 }
113 
114 // The following methods emit a number of assembly sequences with RIP-relative
115 // addressing. Note that RIP-relative addressing on X86-64 has the RIP pointing
116 // to the next instruction, not the current instruction, so we always have to
117 // account for the current instruction's size when calculating offsets.
118 // writeRipRelative helps with that.
119 //
120 // bufAddr:  The virtual address corresponding to buf[0].
121 // bufOff:   The offset within buf of the next instruction.
122 // destAddr: The destination address that the current instruction references.
123 static void writeRipRelative(SymbolDiagnostic d, uint8_t *buf, uint64_t bufAddr,
124                              uint64_t bufOff, uint64_t destAddr) {
125   uint64_t rip = bufAddr + bufOff;
126   checkInt(buf, d, destAddr - rip, 32);
127   // For the instructions we care about, the RIP-relative address is always
128   // stored in the last 4 bytes of the instruction.
129   write32le(buf + bufOff - 4, destAddr - rip);
130 }
131 
132 static constexpr uint8_t stub[] = {
133     0xff, 0x25, 0, 0, 0, 0, // jmpq *__la_symbol_ptr(%rip)
134 };
135 
136 void X86_64::writeStub(uint8_t *buf, const Symbol &sym) const {
137   memcpy(buf, stub, 2); // just copy the two nonzero bytes
138   uint64_t stubAddr = in.stubs->addr + sym.stubsIndex * sizeof(stub);
139   writeRipRelative({&sym, "stub"}, buf, stubAddr, sizeof(stub),
140                    in.lazyPointers->addr + sym.stubsIndex * LP64::wordSize);
141 }
142 
143 static constexpr uint8_t stubHelperHeader[] = {
144     0x4c, 0x8d, 0x1d, 0, 0, 0, 0, // 0x0: leaq ImageLoaderCache(%rip), %r11
145     0x41, 0x53,                   // 0x7: pushq %r11
146     0xff, 0x25, 0,    0, 0, 0,    // 0x9: jmpq *dyld_stub_binder@GOT(%rip)
147     0x90,                         // 0xf: nop
148 };
149 
150 void X86_64::writeStubHelperHeader(uint8_t *buf) const {
151   memcpy(buf, stubHelperHeader, sizeof(stubHelperHeader));
152   SymbolDiagnostic d = {nullptr, "stub helper header"};
153   writeRipRelative(d, buf, in.stubHelper->addr, 7,
154                    in.imageLoaderCache->getVA());
155   writeRipRelative(d, buf, in.stubHelper->addr, 0xf,
156                    in.got->addr +
157                        in.stubHelper->stubBinder->gotIndex * LP64::wordSize);
158 }
159 
160 static constexpr uint8_t stubHelperEntry[] = {
161     0x68, 0, 0, 0, 0, // 0x0: pushq <bind offset>
162     0xe9, 0, 0, 0, 0, // 0x5: jmp <__stub_helper>
163 };
164 
165 void X86_64::writeStubHelperEntry(uint8_t *buf, const Symbol &sym,
166                                   uint64_t entryAddr) const {
167   memcpy(buf, stubHelperEntry, sizeof(stubHelperEntry));
168   write32le(buf + 1, sym.lazyBindOffset);
169   writeRipRelative({&sym, "stub helper"}, buf, entryAddr,
170                    sizeof(stubHelperEntry), in.stubHelper->addr);
171 }
172 
173 void X86_64::relaxGotLoad(uint8_t *loc, uint8_t type) const {
174   // Convert MOVQ to LEAQ
175   if (loc[-2] != 0x8b)
176     error(getRelocAttrs(type).name + " reloc requires MOVQ instruction");
177   loc[-2] = 0x8d;
178 }
179 
180 X86_64::X86_64() : TargetInfo(LP64()) {
181   cpuType = CPU_TYPE_X86_64;
182   cpuSubtype = CPU_SUBTYPE_X86_64_ALL;
183 
184   modeDwarfEncoding = UNWIND_X86_MODE_DWARF;
185   subtractorRelocType = X86_64_RELOC_SUBTRACTOR;
186   unsignedRelocType = X86_64_RELOC_UNSIGNED;
187 
188   stubSize = sizeof(stub);
189   stubHelperHeaderSize = sizeof(stubHelperHeader);
190   stubHelperEntrySize = sizeof(stubHelperEntry);
191 
192   relocAttrs = {relocAttrsArray.data(), relocAttrsArray.size()};
193 }
194 
195 TargetInfo *macho::createX86_64TargetInfo() {
196   static X86_64 t;
197   return &t;
198 }
199 
200 void X86_64::handleDtraceReloc(const Symbol *sym, const Reloc &r,
201                                uint8_t *loc) const {
202   assert(r.type == X86_64_RELOC_BRANCH);
203 
204   if (config->outputType == MH_OBJECT)
205     return;
206 
207   if (sym->getName().startswith("___dtrace_probe")) {
208     // change call site to a NOP
209     loc[-1] = 0x90;
210     write32le(loc, 0x00401F0F);
211   } else if (sym->getName().startswith("___dtrace_isenabled")) {
212     // change call site to a clear eax
213     loc[-1] = 0x33;
214     write32le(loc, 0x909090C0);
215   } else {
216     error("Unrecognized dtrace symbol prefix: " + toString(*sym));
217   }
218 }
219