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"
13 #include "SyntheticSections.h"
15 #include "llvm/Support/Endian.h"
18 using namespace llvm::support::endian;
19 using namespace llvm::ELF;
21 using namespace lld::elf;
24 class X86 final : public TargetInfo {
27 RelExpr getRelExpr(uint32_t Type, const SymbolBody &S,
28 const uint8_t *Loc) const override;
29 int64_t getImplicitAddend(const uint8_t *Buf, uint32_t Type) const override;
30 void writeGotPltHeader(uint8_t *Buf) const override;
31 uint32_t getDynRel(uint32_t Type) const override;
32 void writeGotPlt(uint8_t *Buf, const SymbolBody &S) const override;
33 void writeIgotPlt(uint8_t *Buf, const SymbolBody &S) const override;
34 void writePltHeader(uint8_t *Buf) const override;
35 void writePlt(uint8_t *Buf, uint64_t GotPltEntryAddr, uint64_t PltEntryAddr,
36 int32_t Index, unsigned RelOff) const override;
37 void relocateOne(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
39 RelExpr adjustRelaxExpr(uint32_t Type, const uint8_t *Data,
40 RelExpr Expr) const override;
41 void relaxTlsGdToIe(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
42 void relaxTlsGdToLe(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
43 void relaxTlsIeToLe(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
44 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;
63 TrapInstr = 0xcccccccc; // 0xcc = INT3
66 RelExpr X86::getRelExpr(uint32_t Type, const SymbolBody &S,
67 const uint8_t *Loc) const {
72 case R_386_TLS_LDO_32:
85 return R_GOTONLY_PC_FROM_END;
90 // These relocations can be calculated in two different ways.
91 // Usual calculation is G + A - GOT what means an offset in GOT table
92 // (R_GOT_FROM_END). When instruction pointed by relocation has no base
93 // register, then relocations can be used when PIC code is disabled. In that
94 // case calculation is G + A, it resolves to an address of entry in GOT
95 // (R_GOT) and not an offset.
97 // To check that instruction has no base register we scan ModR/M byte.
98 // See "Table 2-2. 32-Bit Addressing Forms with the ModR/M Byte"
99 // (http://www.intel.com/content/dam/www/public/us/en/documents/manuals/
100 // 64-ia-32-architectures-software-developer-instruction-set-reference-manual-325383.pdf)
101 if ((Loc[-1] & 0xc7) != 0x5)
102 return R_GOT_FROM_END;
104 error(toString(S.File) + ": relocation " + toString(Type) + " against '" +
106 "' without base register can not be used when PIC enabled");
108 case R_386_TLS_GOTIE:
109 return R_GOT_FROM_END;
111 return R_GOTREL_FROM_END;
114 case R_386_TLS_LE_32:
119 error(toString(S.File) + ": unknown relocation type: " + toString(Type));
124 RelExpr X86::adjustRelaxExpr(uint32_t Type, const uint8_t *Data,
125 RelExpr Expr) const {
129 case R_RELAX_TLS_GD_TO_IE:
130 return R_RELAX_TLS_GD_TO_IE_END;
131 case R_RELAX_TLS_GD_TO_LE:
132 return R_RELAX_TLS_GD_TO_LE_NEG;
136 void X86::writeGotPltHeader(uint8_t *Buf) const {
137 write32le(Buf, InX::Dynamic->getVA());
140 void X86::writeGotPlt(uint8_t *Buf, const SymbolBody &S) const {
141 // Entries in .got.plt initially points back to the corresponding
142 // PLT entries with a fixed offset to skip the first instruction.
143 write32le(Buf, S.getPltVA() + 6);
146 void X86::writeIgotPlt(uint8_t *Buf, const SymbolBody &S) const {
147 // An x86 entry is the address of the ifunc resolver function.
148 write32le(Buf, S.getVA());
151 uint32_t X86::getDynRel(uint32_t Type) const {
152 if (Type == R_386_TLS_LE)
153 return R_386_TLS_TPOFF;
154 if (Type == R_386_TLS_LE_32)
155 return R_386_TLS_TPOFF32;
159 void X86::writePltHeader(uint8_t *Buf) const {
161 const uint8_t V[] = {
162 0xff, 0xb3, 0x04, 0x00, 0x00, 0x00, // pushl GOTPLT+4(%ebx)
163 0xff, 0xa3, 0x08, 0x00, 0x00, 0x00, // jmp *GOTPLT+8(%ebx)
164 0x90, 0x90, 0x90, 0x90 // nop
166 memcpy(Buf, V, sizeof(V));
168 uint32_t Ebx = InX::Got->getVA() + InX::Got->getSize();
169 uint32_t GotPlt = InX::GotPlt->getVA() - Ebx;
170 write32le(Buf + 2, GotPlt + 4);
171 write32le(Buf + 8, GotPlt + 8);
175 const uint8_t PltData[] = {
176 0xff, 0x35, 0x00, 0x00, 0x00, 0x00, // pushl (GOTPLT+4)
177 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // jmp *(GOTPLT+8)
178 0x90, 0x90, 0x90, 0x90 // nop
180 memcpy(Buf, PltData, sizeof(PltData));
181 uint32_t GotPlt = InX::GotPlt->getVA();
182 write32le(Buf + 2, GotPlt + 4);
183 write32le(Buf + 8, GotPlt + 8);
186 void X86::writePlt(uint8_t *Buf, uint64_t GotPltEntryAddr,
187 uint64_t PltEntryAddr, int32_t Index,
188 unsigned RelOff) const {
189 const uint8_t Inst[] = {
190 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, // jmp *foo_in_GOT|*foo@GOT(%ebx)
191 0x68, 0x00, 0x00, 0x00, 0x00, // pushl $reloc_offset
192 0xe9, 0x00, 0x00, 0x00, 0x00 // jmp .PLT0@PC
194 memcpy(Buf, Inst, sizeof(Inst));
197 // jmp *foo@GOT(%ebx)
198 uint32_t Ebx = InX::Got->getVA() + InX::Got->getSize();
200 write32le(Buf + 2, GotPltEntryAddr - Ebx);
204 write32le(Buf + 2, GotPltEntryAddr);
207 write32le(Buf + 7, RelOff);
208 write32le(Buf + 12, -Index * PltEntrySize - PltHeaderSize - 16);
211 int64_t X86::getImplicitAddend(const uint8_t *Buf, uint32_t Type) const {
217 return SignExtend64<8>(*Buf);
220 return SignExtend64<16>(read16le(Buf));
228 case R_386_TLS_LDO_32:
230 return SignExtend64<32>(read32le(Buf));
234 void X86::relocateOne(uint8_t *Loc, uint32_t Type, uint64_t Val) const {
235 // R_386_{PC,}{8,16} are not part of the i386 psABI, but they are
236 // being used for some 16-bit programs such as boot loaders, so
237 // we want to support them.
240 checkUInt<8>(Loc, Val, Type);
244 checkInt<8>(Loc, Val, Type);
248 checkUInt<16>(Loc, Val, Type);
252 // R_386_PC16 is normally used with 16 bit code. In that situation
253 // the PC is 16 bits, just like the addend. This means that it can
254 // point from any 16 bit address to any other if the possibility
255 // of wrapping is included.
256 // The only restriction we have to check then is that the destination
257 // address fits in 16 bits. That is impossible to do here. The problem is
258 // that we are passed the final value, which already had the
259 // current location subtracted from it.
260 // We just check that Val fits in 17 bits. This misses some cases, but
261 // should have no false positives.
262 checkInt<17>(Loc, Val, Type);
266 checkInt<32>(Loc, Val, Type);
271 void X86::relaxTlsGdToLe(uint8_t *Loc, uint32_t Type, uint64_t Val) const {
273 // leal x@tlsgd(, %ebx, 1),
274 // call __tls_get_addr@plt
277 // subl $x@ntpoff,%eax
278 const uint8_t Inst[] = {
279 0x65, 0xa1, 0x00, 0x00, 0x00, 0x00, // movl %gs:0, %eax
280 0x81, 0xe8, 0x00, 0x00, 0x00, 0x00 // subl 0(%ebx), %eax
282 memcpy(Loc - 3, Inst, sizeof(Inst));
283 write32le(Loc + 5, Val);
286 void X86::relaxTlsGdToIe(uint8_t *Loc, uint32_t Type, uint64_t Val) const {
288 // leal x@tlsgd(, %ebx, 1),
289 // call __tls_get_addr@plt
292 // addl x@gotntpoff(%ebx), %eax
293 const uint8_t Inst[] = {
294 0x65, 0xa1, 0x00, 0x00, 0x00, 0x00, // movl %gs:0, %eax
295 0x03, 0x83, 0x00, 0x00, 0x00, 0x00 // addl 0(%ebx), %eax
297 memcpy(Loc - 3, Inst, sizeof(Inst));
298 write32le(Loc + 5, Val);
301 // In some conditions, relocations can be optimized to avoid using GOT.
302 // This function does that for Initial Exec to Local Exec case.
303 void X86::relaxTlsIeToLe(uint8_t *Loc, uint32_t Type, uint64_t Val) const {
304 // Ulrich's document section 6.2 says that @gotntpoff can
305 // be used with MOVL or ADDL instructions.
306 // @indntpoff is similar to @gotntpoff, but for use in
307 // position dependent code.
308 uint8_t Reg = (Loc[-1] >> 3) & 7;
310 if (Type == R_386_TLS_IE) {
311 if (Loc[-1] == 0xa1) {
312 // "movl foo@indntpoff,%eax" -> "movl $foo,%eax"
313 // This case is different from the generic case below because
314 // this is a 5 byte instruction while below is 6 bytes.
316 } else if (Loc[-2] == 0x8b) {
317 // "movl foo@indntpoff,%reg" -> "movl $foo,%reg"
319 Loc[-1] = 0xc0 | Reg;
321 // "addl foo@indntpoff,%reg" -> "addl $foo,%reg"
323 Loc[-1] = 0xc0 | Reg;
326 assert(Type == R_386_TLS_GOTIE);
327 if (Loc[-2] == 0x8b) {
328 // "movl foo@gottpoff(%rip),%reg" -> "movl $foo,%reg"
330 Loc[-1] = 0xc0 | Reg;
332 // "addl foo@gotntpoff(%rip),%reg" -> "leal foo(%reg),%reg"
334 Loc[-1] = 0x80 | (Reg << 3) | Reg;
340 void X86::relaxTlsLdToLe(uint8_t *Loc, uint32_t Type, uint64_t Val) const {
341 if (Type == R_386_TLS_LDO_32) {
347 // leal foo(%reg),%eax
348 // call ___tls_get_addr
352 // leal 0(%esi,1),%esi
353 const uint8_t Inst[] = {
354 0x65, 0xa1, 0x00, 0x00, 0x00, 0x00, // movl %gs:0,%eax
356 0x8d, 0x74, 0x26, 0x00 // leal 0(%esi,1),%esi
358 memcpy(Loc - 2, Inst, sizeof(Inst));
361 TargetInfo *elf::getX86TargetInfo() {