173
|
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 "Symbols.h"
|
|
10 #include "SyntheticSections.h"
|
|
11 #include "Target.h"
|
|
12
|
|
13 #include "lld/Common/ErrorHandler.h"
|
|
14 #include "llvm/BinaryFormat/MachO.h"
|
|
15 #include "llvm/Support/Endian.h"
|
|
16
|
|
17 using namespace llvm::MachO;
|
|
18 using namespace llvm::support::endian;
|
|
19 using namespace lld;
|
|
20 using namespace lld::macho;
|
|
21
|
|
22 namespace {
|
|
23
|
|
24 struct X86_64 : TargetInfo {
|
|
25 X86_64();
|
|
26
|
|
27 uint64_t getImplicitAddend(const uint8_t *loc, uint8_t type) const override;
|
|
28 void relocateOne(uint8_t *loc, uint8_t type, uint64_t val) const override;
|
|
29
|
|
30 void writeStub(uint8_t *buf, const DylibSymbol &) const override;
|
|
31 void writeStubHelperHeader(uint8_t *buf) const override;
|
|
32 void writeStubHelperEntry(uint8_t *buf, const DylibSymbol &,
|
|
33 uint64_t entryAddr) const override;
|
|
34
|
|
35 void prepareDylibSymbolRelocation(DylibSymbol &, uint8_t type) override;
|
|
36 uint64_t getDylibSymbolVA(const DylibSymbol &, uint8_t type) const override;
|
|
37 };
|
|
38
|
|
39 } // namespace
|
|
40
|
|
41 uint64_t X86_64::getImplicitAddend(const uint8_t *loc, uint8_t type) const {
|
|
42 switch (type) {
|
|
43 case X86_64_RELOC_BRANCH:
|
|
44 case X86_64_RELOC_SIGNED:
|
|
45 case X86_64_RELOC_SIGNED_1:
|
|
46 case X86_64_RELOC_SIGNED_2:
|
|
47 case X86_64_RELOC_SIGNED_4:
|
|
48 case X86_64_RELOC_GOT_LOAD:
|
|
49 return read32le(loc);
|
|
50 case X86_64_RELOC_UNSIGNED:
|
|
51 return read64le(loc);
|
|
52 default:
|
|
53 error("TODO: Unhandled relocation type " + std::to_string(type));
|
|
54 return 0;
|
|
55 }
|
|
56 }
|
|
57
|
|
58 void X86_64::relocateOne(uint8_t *loc, uint8_t type, uint64_t val) const {
|
|
59 switch (type) {
|
|
60 case X86_64_RELOC_BRANCH:
|
|
61 case X86_64_RELOC_SIGNED:
|
|
62 case X86_64_RELOC_SIGNED_1:
|
|
63 case X86_64_RELOC_SIGNED_2:
|
|
64 case X86_64_RELOC_SIGNED_4:
|
|
65 case X86_64_RELOC_GOT_LOAD:
|
|
66 // These types are only used for pc-relative relocations, so offset by 4
|
|
67 // since the RIP has advanced by 4 at this point.
|
|
68 write32le(loc, val - 4);
|
|
69 break;
|
|
70 case X86_64_RELOC_UNSIGNED:
|
|
71 write64le(loc, val);
|
|
72 break;
|
|
73 default:
|
|
74 llvm_unreachable(
|
|
75 "getImplicitAddend should have flagged all unhandled relocation types");
|
|
76 }
|
|
77 }
|
|
78
|
|
79 // The following methods emit a number of assembly sequences with RIP-relative
|
|
80 // addressing. Note that RIP-relative addressing on X86-64 has the RIP pointing
|
|
81 // to the next instruction, not the current instruction, so we always have to
|
|
82 // account for the current instruction's size when calculating offsets.
|
|
83 // writeRipRelative helps with that.
|
|
84 //
|
|
85 // bufAddr: The virtual address corresponding to buf[0].
|
|
86 // bufOff: The offset within buf of the next instruction.
|
|
87 // destAddr: The destination address that the current instruction references.
|
|
88 static void writeRipRelative(uint8_t *buf, uint64_t bufAddr, uint64_t bufOff,
|
|
89 uint64_t destAddr) {
|
|
90 uint64_t rip = bufAddr + bufOff;
|
|
91 // For the instructions we care about, the RIP-relative address is always
|
|
92 // stored in the last 4 bytes of the instruction.
|
|
93 write32le(buf + bufOff - 4, destAddr - rip);
|
|
94 }
|
|
95
|
|
96 static constexpr uint8_t stub[] = {
|
|
97 0xff, 0x25, 0, 0, 0, 0, // jmpq *__la_symbol_ptr(%rip)
|
|
98 };
|
|
99
|
|
100 void X86_64::writeStub(uint8_t *buf, const DylibSymbol &sym) const {
|
|
101 memcpy(buf, stub, 2); // just copy the two nonzero bytes
|
|
102 uint64_t stubAddr = in.stubs->addr + sym.stubsIndex * sizeof(stub);
|
|
103 writeRipRelative(buf, stubAddr, sizeof(stub),
|
|
104 in.lazyPointers->addr + sym.stubsIndex * WordSize);
|
|
105 }
|
|
106
|
|
107 static constexpr uint8_t stubHelperHeader[] = {
|
|
108 0x4c, 0x8d, 0x1d, 0, 0, 0, 0, // 0x0: leaq ImageLoaderCache(%rip), %r11
|
|
109 0x41, 0x53, // 0x7: pushq %r11
|
|
110 0xff, 0x25, 0, 0, 0, 0, // 0x9: jmpq *dyld_stub_binder@GOT(%rip)
|
|
111 0x90, // 0xf: nop
|
|
112 };
|
|
113
|
|
114 static constexpr uint8_t stubHelperEntry[] = {
|
|
115 0x68, 0, 0, 0, 0, // 0x0: pushq <bind offset>
|
|
116 0xe9, 0, 0, 0, 0, // 0x5: jmp <__stub_helper>
|
|
117 };
|
|
118
|
|
119 void X86_64::writeStubHelperHeader(uint8_t *buf) const {
|
|
120 memcpy(buf, stubHelperHeader, sizeof(stubHelperHeader));
|
|
121 writeRipRelative(buf, in.stubHelper->addr, 7, in.imageLoaderCache->getVA());
|
|
122 writeRipRelative(buf, in.stubHelper->addr, 0xf,
|
|
123 in.got->addr +
|
|
124 in.stubHelper->stubBinder->gotIndex * WordSize);
|
|
125 }
|
|
126
|
|
127 void X86_64::writeStubHelperEntry(uint8_t *buf, const DylibSymbol &sym,
|
|
128 uint64_t entryAddr) const {
|
|
129 memcpy(buf, stubHelperEntry, sizeof(stubHelperEntry));
|
|
130 write32le(buf + 1, sym.lazyBindOffset);
|
|
131 writeRipRelative(buf, entryAddr, sizeof(stubHelperEntry),
|
|
132 in.stubHelper->addr);
|
|
133 }
|
|
134
|
|
135 void X86_64::prepareDylibSymbolRelocation(DylibSymbol &sym, uint8_t type) {
|
|
136 switch (type) {
|
|
137 case X86_64_RELOC_GOT_LOAD:
|
|
138 in.got->addEntry(sym);
|
|
139 break;
|
|
140 case X86_64_RELOC_BRANCH:
|
|
141 in.stubs->addEntry(sym);
|
|
142 break;
|
|
143 case X86_64_RELOC_GOT:
|
|
144 fatal("TODO: Unhandled dylib symbol relocation X86_64_RELOC_GOT");
|
|
145 default:
|
|
146 llvm_unreachable("Unexpected dylib relocation type");
|
|
147 }
|
|
148 }
|
|
149
|
|
150 uint64_t X86_64::getDylibSymbolVA(const DylibSymbol &sym, uint8_t type) const {
|
|
151 switch (type) {
|
|
152 case X86_64_RELOC_GOT_LOAD:
|
|
153 return in.got->addr + sym.gotIndex * WordSize;
|
|
154 case X86_64_RELOC_BRANCH:
|
|
155 return in.stubs->addr + sym.stubsIndex * sizeof(stub);
|
|
156 case X86_64_RELOC_GOT:
|
|
157 fatal("TODO: Unhandled dylib symbol relocation X86_64_RELOC_GOT");
|
|
158 default:
|
|
159 llvm_unreachable("Unexpected dylib relocation type");
|
|
160 }
|
|
161 }
|
|
162
|
|
163 X86_64::X86_64() {
|
|
164 cpuType = CPU_TYPE_X86_64;
|
|
165 cpuSubtype = CPU_SUBTYPE_X86_64_ALL;
|
|
166
|
|
167 stubSize = sizeof(stub);
|
|
168 stubHelperHeaderSize = sizeof(stubHelperHeader);
|
|
169 stubHelperEntrySize = sizeof(stubHelperEntry);
|
|
170 }
|
|
171
|
|
172 TargetInfo *macho::createX86_64TargetInfo() {
|
|
173 static X86_64 t;
|
|
174 return &t;
|
|
175 }
|