1 //===- X86.cpp ------------------------------------------------------------===//
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 #include "InputFiles.h"
14 #include "SyntheticSections.h"
16 #include "llvm/Support/Endian.h"
19 using namespace llvm::support::endian;
20 using namespace llvm::ELF;
22 using namespace lld::elf;
25 class X86 final : public TargetInfo {
28 RelExpr getRelExpr(uint32_t Type, const SymbolBody &S,
29 const uint8_t *Loc) const override;
30 int64_t getImplicitAddend(const uint8_t *Buf, uint32_t Type) const override;
31 void writeGotPltHeader(uint8_t *Buf) const override;
32 uint32_t getDynRel(uint32_t Type) const override;
33 void writeGotPlt(uint8_t *Buf, const SymbolBody &S) const override;
34 void writeIgotPlt(uint8_t *Buf, const SymbolBody &S) const override;
35 void writePltHeader(uint8_t *Buf) const override;
36 void writePlt(uint8_t *Buf, uint64_t GotPltEntryAddr, uint64_t PltEntryAddr,
37 int32_t Index, unsigned RelOff) const override;
38 void relocateOne(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
40 RelExpr adjustRelaxExpr(uint32_t Type, const uint8_t *Data,
41 RelExpr Expr) const override;
42 void relaxTlsGdToIe(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
43 void relaxTlsGdToLe(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
44 void relaxTlsIeToLe(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
45 void relaxTlsLdToLe(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
51 GotRel = R_386_GLOB_DAT;
52 PltRel = R_386_JUMP_SLOT;
53 IRelativeRel = R_386_IRELATIVE;
54 RelativeRel = R_386_RELATIVE;
55 TlsGotRel = R_386_TLS_TPOFF;
56 TlsModuleIndexRel = R_386_TLS_DTPMOD32;
57 TlsOffsetRel = R_386_TLS_DTPOFF32;
64 // 0xCC is the "int3" (call debug exception handler) instruction.
65 TrapInstr = 0xcccccccc;
68 RelExpr X86::getRelExpr(uint32_t Type, const SymbolBody &S,
69 const uint8_t *Loc) const {
74 case R_386_TLS_LDO_32:
87 return R_GOTONLY_PC_FROM_END;
92 // These relocations can be calculated in two different ways.
93 // Usual calculation is G + A - GOT what means an offset in GOT table
94 // (R_GOT_FROM_END). When instruction pointed by relocation has no base
95 // register, then relocations can be used when PIC code is disabled. In that
96 // case calculation is G + A, it resolves to an address of entry in GOT
97 // (R_GOT) and not an offset.
99 // To check that instruction has no base register we scan ModR/M byte.
100 // See "Table 2-2. 32-Bit Addressing Forms with the ModR/M Byte"
101 // (http://www.intel.com/content/dam/www/public/us/en/documents/manuals/
102 // 64-ia-32-architectures-software-developer-instruction-set-reference-manual-325383.pdf)
103 if ((Loc[-1] & 0xc7) != 0x5)
104 return R_GOT_FROM_END;
106 error(toString(S.File) + ": relocation " + toString(Type) + " against '" +
108 "' without base register can not be used when PIC enabled");
110 case R_386_TLS_GOTIE:
111 return R_GOT_FROM_END;
113 return R_GOTREL_FROM_END;
116 case R_386_TLS_LE_32:
121 error(toString(S.File) + ": unknown relocation type: " + toString(Type));
126 RelExpr X86::adjustRelaxExpr(uint32_t Type, const uint8_t *Data,
127 RelExpr Expr) const {
131 case R_RELAX_TLS_GD_TO_IE:
132 return R_RELAX_TLS_GD_TO_IE_END;
133 case R_RELAX_TLS_GD_TO_LE:
134 return R_RELAX_TLS_GD_TO_LE_NEG;
138 void X86::writeGotPltHeader(uint8_t *Buf) const {
139 write32le(Buf, InX::Dynamic->getVA());
142 void X86::writeGotPlt(uint8_t *Buf, const SymbolBody &S) const {
143 // Entries in .got.plt initially points back to the corresponding
144 // PLT entries with a fixed offset to skip the first instruction.
145 write32le(Buf, S.getPltVA() + 6);
148 void X86::writeIgotPlt(uint8_t *Buf, const SymbolBody &S) const {
149 // An x86 entry is the address of the ifunc resolver function.
150 write32le(Buf, S.getVA());
153 uint32_t X86::getDynRel(uint32_t Type) const {
154 if (Type == R_386_TLS_LE)
155 return R_386_TLS_TPOFF;
156 if (Type == R_386_TLS_LE_32)
157 return R_386_TLS_TPOFF32;
161 void X86::writePltHeader(uint8_t *Buf) const {
163 const uint8_t V[] = {
164 0xff, 0xb3, 0x04, 0x00, 0x00, 0x00, // pushl GOTPLT+4(%ebx)
165 0xff, 0xa3, 0x08, 0x00, 0x00, 0x00, // jmp *GOTPLT+8(%ebx)
166 0x90, 0x90, 0x90, 0x90 // nop
168 memcpy(Buf, V, sizeof(V));
170 uint32_t Ebx = InX::Got->getVA() + InX::Got->getSize();
171 uint32_t GotPlt = InX::GotPlt->getVA() - Ebx;
172 write32le(Buf + 2, GotPlt + 4);
173 write32le(Buf + 8, GotPlt + 8);
177 const uint8_t PltData[] = {
178 0xff, 0x35, 0x00, 0x00, 0x00, 0x00, // pushl (GOTPLT+4)
179 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // jmp *(GOTPLT+8)
180 0x90, 0x90, 0x90, 0x90 // nop
182 memcpy(Buf, PltData, sizeof(PltData));
183 uint32_t GotPlt = InX::GotPlt->getVA();
184 write32le(Buf + 2, GotPlt + 4);
185 write32le(Buf + 8, GotPlt + 8);
188 void X86::writePlt(uint8_t *Buf, uint64_t GotPltEntryAddr,
189 uint64_t PltEntryAddr, int32_t Index,
190 unsigned RelOff) const {
191 const uint8_t Inst[] = {
192 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, // jmp *foo_in_GOT|*foo@GOT(%ebx)
193 0x68, 0x00, 0x00, 0x00, 0x00, // pushl $reloc_offset
194 0xe9, 0x00, 0x00, 0x00, 0x00 // jmp .PLT0@PC
196 memcpy(Buf, Inst, sizeof(Inst));
199 // jmp *foo@GOT(%ebx)
200 uint32_t Ebx = InX::Got->getVA() + InX::Got->getSize();
202 write32le(Buf + 2, GotPltEntryAddr - Ebx);
206 write32le(Buf + 2, GotPltEntryAddr);
209 write32le(Buf + 7, RelOff);
210 write32le(Buf + 12, -Index * PltEntrySize - PltHeaderSize - 16);
213 int64_t X86::getImplicitAddend(const uint8_t *Buf, uint32_t Type) const {
219 return SignExtend64<8>(*Buf);
222 return SignExtend64<16>(read16le(Buf));
230 case R_386_TLS_LDO_32:
232 return SignExtend64<32>(read32le(Buf));
236 void X86::relocateOne(uint8_t *Loc, uint32_t Type, uint64_t Val) const {
237 // R_386_{PC,}{8,16} are not part of the i386 psABI, but they are
238 // being used for some 16-bit programs such as boot loaders, so
239 // we want to support them.
242 checkUInt<8>(Loc, Val, Type);
246 checkInt<8>(Loc, Val, Type);
250 checkUInt<16>(Loc, Val, Type);
254 // R_386_PC16 is normally used with 16 bit code. In that situation
255 // the PC is 16 bits, just like the addend. This means that it can
256 // point from any 16 bit address to any other if the possibility
257 // of wrapping is included.
258 // The only restriction we have to check then is that the destination
259 // address fits in 16 bits. That is impossible to do here. The problem is
260 // that we are passed the final value, which already had the
261 // current location subtracted from it.
262 // We just check that Val fits in 17 bits. This misses some cases, but
263 // should have no false positives.
264 checkInt<17>(Loc, Val, Type);
268 checkInt<32>(Loc, Val, Type);
273 void X86::relaxTlsGdToLe(uint8_t *Loc, uint32_t Type, uint64_t Val) const {
275 // leal x@tlsgd(, %ebx, 1),
276 // call __tls_get_addr@plt
279 // subl $x@ntpoff,%eax
280 const uint8_t Inst[] = {
281 0x65, 0xa1, 0x00, 0x00, 0x00, 0x00, // movl %gs:0, %eax
282 0x81, 0xe8, 0x00, 0x00, 0x00, 0x00 // subl 0(%ebx), %eax
284 memcpy(Loc - 3, Inst, sizeof(Inst));
285 write32le(Loc + 5, Val);
288 void X86::relaxTlsGdToIe(uint8_t *Loc, uint32_t Type, uint64_t Val) const {
290 // leal x@tlsgd(, %ebx, 1),
291 // call __tls_get_addr@plt
294 // addl x@gotntpoff(%ebx), %eax
295 const uint8_t Inst[] = {
296 0x65, 0xa1, 0x00, 0x00, 0x00, 0x00, // movl %gs:0, %eax
297 0x03, 0x83, 0x00, 0x00, 0x00, 0x00 // addl 0(%ebx), %eax
299 memcpy(Loc - 3, Inst, sizeof(Inst));
300 write32le(Loc + 5, Val);
303 // In some conditions, relocations can be optimized to avoid using GOT.
304 // This function does that for Initial Exec to Local Exec case.
305 void X86::relaxTlsIeToLe(uint8_t *Loc, uint32_t Type, uint64_t Val) const {
306 // Ulrich's document section 6.2 says that @gotntpoff can
307 // be used with MOVL or ADDL instructions.
308 // @indntpoff is similar to @gotntpoff, but for use in
309 // position dependent code.
310 uint8_t Reg = (Loc[-1] >> 3) & 7;
312 if (Type == R_386_TLS_IE) {
313 if (Loc[-1] == 0xa1) {
314 // "movl foo@indntpoff,%eax" -> "movl $foo,%eax"
315 // This case is different from the generic case below because
316 // this is a 5 byte instruction while below is 6 bytes.
318 } else if (Loc[-2] == 0x8b) {
319 // "movl foo@indntpoff,%reg" -> "movl $foo,%reg"
321 Loc[-1] = 0xc0 | Reg;
323 // "addl foo@indntpoff,%reg" -> "addl $foo,%reg"
325 Loc[-1] = 0xc0 | Reg;
328 assert(Type == R_386_TLS_GOTIE);
329 if (Loc[-2] == 0x8b) {
330 // "movl foo@gottpoff(%rip),%reg" -> "movl $foo,%reg"
332 Loc[-1] = 0xc0 | Reg;
334 // "addl foo@gotntpoff(%rip),%reg" -> "leal foo(%reg),%reg"
336 Loc[-1] = 0x80 | (Reg << 3) | Reg;
342 void X86::relaxTlsLdToLe(uint8_t *Loc, uint32_t Type, uint64_t Val) const {
343 if (Type == R_386_TLS_LDO_32) {
349 // leal foo(%reg),%eax
350 // call ___tls_get_addr
354 // leal 0(%esi,1),%esi
355 const uint8_t Inst[] = {
356 0x65, 0xa1, 0x00, 0x00, 0x00, 0x00, // movl %gs:0,%eax
358 0x8d, 0x74, 0x26, 0x00 // leal 0(%esi,1),%esi
360 memcpy(Loc - 2, Inst, sizeof(Inst));
363 TargetInfo *elf::createX86TargetInfo() { return make<X86>(); }