• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===- X86.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 #include "lld/Common/ErrorHandler.h"
14 #include "llvm/Support/Endian.h"
15 
16 using namespace llvm;
17 using namespace llvm::support::endian;
18 using namespace llvm::ELF;
19 using namespace lld;
20 using namespace lld::elf;
21 
22 namespace {
23 class X86 : public TargetInfo {
24 public:
25   X86();
26   int getTlsGdRelaxSkip(RelType type) const override;
27   RelExpr getRelExpr(RelType type, const Symbol &s,
28                      const uint8_t *loc) const override;
29   int64_t getImplicitAddend(const uint8_t *buf, RelType type) const override;
30   void writeGotPltHeader(uint8_t *buf) const override;
31   RelType getDynRel(RelType type) const override;
32   void writeGotPlt(uint8_t *buf, const Symbol &s) const override;
33   void writeIgotPlt(uint8_t *buf, const Symbol &s) const override;
34   void writePltHeader(uint8_t *buf) const override;
35   void writePlt(uint8_t *buf, const Symbol &sym,
36                 uint64_t pltEntryAddr) const override;
37   void relocate(uint8_t *loc, const Relocation &rel,
38                 uint64_t val) const override;
39 
40   RelExpr adjustTlsExpr(RelType type, RelExpr expr) const override;
41   void relaxTlsGdToIe(uint8_t *loc, const Relocation &rel,
42                       uint64_t val) const override;
43   void relaxTlsGdToLe(uint8_t *loc, const Relocation &rel,
44                       uint64_t val) const override;
45   void relaxTlsIeToLe(uint8_t *loc, const Relocation &rel,
46                       uint64_t val) const override;
47   void relaxTlsLdToLe(uint8_t *loc, const Relocation &rel,
48                       uint64_t val) const override;
49 };
50 } // namespace
51 
X86()52 X86::X86() {
53   copyRel = R_386_COPY;
54   gotRel = R_386_GLOB_DAT;
55   noneRel = R_386_NONE;
56   pltRel = R_386_JUMP_SLOT;
57   iRelativeRel = R_386_IRELATIVE;
58   relativeRel = R_386_RELATIVE;
59   symbolicRel = R_386_32;
60   tlsGotRel = R_386_TLS_TPOFF;
61   tlsModuleIndexRel = R_386_TLS_DTPMOD32;
62   tlsOffsetRel = R_386_TLS_DTPOFF32;
63   pltHeaderSize = 16;
64   pltEntrySize = 16;
65   ipltEntrySize = 16;
66   trapInstr = {0xcc, 0xcc, 0xcc, 0xcc}; // 0xcc = INT3
67 
68   // Align to the non-PAE large page size (known as a superpage or huge page).
69   // FreeBSD automatically promotes large, superpage-aligned allocations.
70   defaultImageBase = 0x400000;
71 }
72 
getTlsGdRelaxSkip(RelType type) const73 int X86::getTlsGdRelaxSkip(RelType type) const {
74   return 2;
75 }
76 
getRelExpr(RelType type,const Symbol & s,const uint8_t * loc) const77 RelExpr X86::getRelExpr(RelType type, const Symbol &s,
78                         const uint8_t *loc) const {
79   // There are 4 different TLS variable models with varying degrees of
80   // flexibility and performance. LocalExec and InitialExec models are fast but
81   // less-flexible models. If they are in use, we set DF_STATIC_TLS flag in the
82   // dynamic section to let runtime know about that.
83   if (type == R_386_TLS_LE || type == R_386_TLS_LE_32 || type == R_386_TLS_IE ||
84       type == R_386_TLS_GOTIE)
85     config->hasStaticTlsModel = true;
86 
87   switch (type) {
88   case R_386_8:
89   case R_386_16:
90   case R_386_32:
91     return R_ABS;
92   case R_386_TLS_LDO_32:
93     return R_DTPREL;
94   case R_386_TLS_GD:
95     return R_TLSGD_GOTPLT;
96   case R_386_TLS_LDM:
97     return R_TLSLD_GOTPLT;
98   case R_386_PLT32:
99     return R_PLT_PC;
100   case R_386_PC8:
101   case R_386_PC16:
102   case R_386_PC32:
103     return R_PC;
104   case R_386_GOTPC:
105     return R_GOTPLTONLY_PC;
106   case R_386_TLS_IE:
107     return R_GOT;
108   case R_386_GOT32:
109   case R_386_GOT32X:
110     // These relocations are arguably mis-designed because their calculations
111     // depend on the instructions they are applied to. This is bad because we
112     // usually don't care about whether the target section contains valid
113     // machine instructions or not. But this is part of the documented ABI, so
114     // we had to implement as the standard requires.
115     //
116     // x86 does not support PC-relative data access. Therefore, in order to
117     // access GOT contents, a GOT address needs to be known at link-time
118     // (which means non-PIC) or compilers have to emit code to get a GOT
119     // address at runtime (which means code is position-independent but
120     // compilers need to emit extra code for each GOT access.) This decision
121     // is made at compile-time. In the latter case, compilers emit code to
122     // load a GOT address to a register, which is usually %ebx.
123     //
124     // So, there are two ways to refer to symbol foo's GOT entry: foo@GOT or
125     // foo@GOT(%ebx).
126     //
127     // foo@GOT is not usable in PIC. If we are creating a PIC output and if we
128     // find such relocation, we should report an error. foo@GOT is resolved to
129     // an *absolute* address of foo's GOT entry, because both GOT address and
130     // foo's offset are known. In other words, it's G + A.
131     //
132     // foo@GOT(%ebx) needs to be resolved to a *relative* offset from a GOT to
133     // foo's GOT entry in the table, because GOT address is not known but foo's
134     // offset in the table is known. It's G + A - GOT.
135     //
136     // It's unfortunate that compilers emit the same relocation for these
137     // different use cases. In order to distinguish them, we have to read a
138     // machine instruction.
139     //
140     // The following code implements it. We assume that Loc[0] is the first byte
141     // of a displacement or an immediate field of a valid machine
142     // instruction. That means a ModRM byte is at Loc[-1]. By taking a look at
143     // the byte, we can determine whether the instruction uses the operand as an
144     // absolute address (R_GOT) or a register-relative address (R_GOTPLT).
145     return (loc[-1] & 0xc7) == 0x5 ? R_GOT : R_GOTPLT;
146   case R_386_TLS_GOTIE:
147     return R_GOTPLT;
148   case R_386_GOTOFF:
149     return R_GOTPLTREL;
150   case R_386_TLS_LE:
151     return R_TLS;
152   case R_386_TLS_LE_32:
153     return R_NEG_TLS;
154   case R_386_NONE:
155     return R_NONE;
156   default:
157     error(getErrorLocation(loc) + "unknown relocation (" + Twine(type) +
158           ") against symbol " + toString(s));
159     return R_NONE;
160   }
161 }
162 
adjustTlsExpr(RelType type,RelExpr expr) const163 RelExpr X86::adjustTlsExpr(RelType type, RelExpr expr) const {
164   switch (expr) {
165   default:
166     return expr;
167   case R_RELAX_TLS_GD_TO_IE:
168     return R_RELAX_TLS_GD_TO_IE_GOTPLT;
169   case R_RELAX_TLS_GD_TO_LE:
170     return R_RELAX_TLS_GD_TO_LE_NEG;
171   }
172 }
173 
writeGotPltHeader(uint8_t * buf) const174 void X86::writeGotPltHeader(uint8_t *buf) const {
175   write32le(buf, mainPart->dynamic->getVA());
176 }
177 
writeGotPlt(uint8_t * buf,const Symbol & s) const178 void X86::writeGotPlt(uint8_t *buf, const Symbol &s) const {
179   // Entries in .got.plt initially points back to the corresponding
180   // PLT entries with a fixed offset to skip the first instruction.
181   write32le(buf, s.getPltVA() + 6);
182 }
183 
writeIgotPlt(uint8_t * buf,const Symbol & s) const184 void X86::writeIgotPlt(uint8_t *buf, const Symbol &s) const {
185   // An x86 entry is the address of the ifunc resolver function.
186   write32le(buf, s.getVA());
187 }
188 
getDynRel(RelType type) const189 RelType X86::getDynRel(RelType type) const {
190   if (type == R_386_TLS_LE)
191     return R_386_TLS_TPOFF;
192   if (type == R_386_TLS_LE_32)
193     return R_386_TLS_TPOFF32;
194   return type;
195 }
196 
writePltHeader(uint8_t * buf) const197 void X86::writePltHeader(uint8_t *buf) const {
198   if (config->isPic) {
199     const uint8_t v[] = {
200         0xff, 0xb3, 0x04, 0x00, 0x00, 0x00, // pushl 4(%ebx)
201         0xff, 0xa3, 0x08, 0x00, 0x00, 0x00, // jmp *8(%ebx)
202         0x90, 0x90, 0x90, 0x90              // nop
203     };
204     memcpy(buf, v, sizeof(v));
205     return;
206   }
207 
208   const uint8_t pltData[] = {
209       0xff, 0x35, 0, 0, 0, 0, // pushl (GOTPLT+4)
210       0xff, 0x25, 0, 0, 0, 0, // jmp *(GOTPLT+8)
211       0x90, 0x90, 0x90, 0x90, // nop
212   };
213   memcpy(buf, pltData, sizeof(pltData));
214   uint32_t gotPlt = in.gotPlt->getVA();
215   write32le(buf + 2, gotPlt + 4);
216   write32le(buf + 8, gotPlt + 8);
217 }
218 
writePlt(uint8_t * buf,const Symbol & sym,uint64_t pltEntryAddr) const219 void X86::writePlt(uint8_t *buf, const Symbol &sym,
220                    uint64_t pltEntryAddr) const {
221   unsigned relOff = in.relaPlt->entsize * sym.pltIndex;
222   if (config->isPic) {
223     const uint8_t inst[] = {
224         0xff, 0xa3, 0, 0, 0, 0, // jmp *foo@GOT(%ebx)
225         0x68, 0,    0, 0, 0,    // pushl $reloc_offset
226         0xe9, 0,    0, 0, 0,    // jmp .PLT0@PC
227     };
228     memcpy(buf, inst, sizeof(inst));
229     write32le(buf + 2, sym.getGotPltVA() - in.gotPlt->getVA());
230   } else {
231     const uint8_t inst[] = {
232         0xff, 0x25, 0, 0, 0, 0, // jmp *foo@GOT
233         0x68, 0,    0, 0, 0,    // pushl $reloc_offset
234         0xe9, 0,    0, 0, 0,    // jmp .PLT0@PC
235     };
236     memcpy(buf, inst, sizeof(inst));
237     write32le(buf + 2, sym.getGotPltVA());
238   }
239 
240   write32le(buf + 7, relOff);
241   write32le(buf + 12, in.plt->getVA() - pltEntryAddr - 16);
242 }
243 
getImplicitAddend(const uint8_t * buf,RelType type) const244 int64_t X86::getImplicitAddend(const uint8_t *buf, RelType type) const {
245   switch (type) {
246   case R_386_8:
247   case R_386_PC8:
248     return SignExtend64<8>(*buf);
249   case R_386_16:
250   case R_386_PC16:
251     return SignExtend64<16>(read16le(buf));
252   case R_386_32:
253   case R_386_GOT32:
254   case R_386_GOT32X:
255   case R_386_GOTOFF:
256   case R_386_GOTPC:
257   case R_386_PC32:
258   case R_386_PLT32:
259   case R_386_TLS_LDO_32:
260   case R_386_TLS_LE:
261     return SignExtend64<32>(read32le(buf));
262   default:
263     return 0;
264   }
265 }
266 
relocate(uint8_t * loc,const Relocation & rel,uint64_t val) const267 void X86::relocate(uint8_t *loc, const Relocation &rel, uint64_t val) const {
268   switch (rel.type) {
269   case R_386_8:
270     // R_386_{PC,}{8,16} are not part of the i386 psABI, but they are
271     // being used for some 16-bit programs such as boot loaders, so
272     // we want to support them.
273     checkIntUInt(loc, val, 8, rel);
274     *loc = val;
275     break;
276   case R_386_PC8:
277     checkInt(loc, val, 8, rel);
278     *loc = val;
279     break;
280   case R_386_16:
281     checkIntUInt(loc, val, 16, rel);
282     write16le(loc, val);
283     break;
284   case R_386_PC16:
285     // R_386_PC16 is normally used with 16 bit code. In that situation
286     // the PC is 16 bits, just like the addend. This means that it can
287     // point from any 16 bit address to any other if the possibility
288     // of wrapping is included.
289     // The only restriction we have to check then is that the destination
290     // address fits in 16 bits. That is impossible to do here. The problem is
291     // that we are passed the final value, which already had the
292     // current location subtracted from it.
293     // We just check that Val fits in 17 bits. This misses some cases, but
294     // should have no false positives.
295     checkInt(loc, val, 17, rel);
296     write16le(loc, val);
297     break;
298   case R_386_32:
299   case R_386_GOT32:
300   case R_386_GOT32X:
301   case R_386_GOTOFF:
302   case R_386_GOTPC:
303   case R_386_PC32:
304   case R_386_PLT32:
305   case R_386_RELATIVE:
306   case R_386_TLS_DTPMOD32:
307   case R_386_TLS_DTPOFF32:
308   case R_386_TLS_GD:
309   case R_386_TLS_GOTIE:
310   case R_386_TLS_IE:
311   case R_386_TLS_LDM:
312   case R_386_TLS_LDO_32:
313   case R_386_TLS_LE:
314   case R_386_TLS_LE_32:
315   case R_386_TLS_TPOFF:
316   case R_386_TLS_TPOFF32:
317     checkInt(loc, val, 32, rel);
318     write32le(loc, val);
319     break;
320   default:
321     llvm_unreachable("unknown relocation");
322   }
323 }
324 
relaxTlsGdToLe(uint8_t * loc,const Relocation &,uint64_t val) const325 void X86::relaxTlsGdToLe(uint8_t *loc, const Relocation &, uint64_t val) const {
326   // Convert
327   //   leal x@tlsgd(, %ebx, 1),
328   //   call __tls_get_addr@plt
329   // to
330   //   movl %gs:0,%eax
331   //   subl $x@ntpoff,%eax
332   const uint8_t inst[] = {
333       0x65, 0xa1, 0x00, 0x00, 0x00, 0x00, // movl %gs:0, %eax
334       0x81, 0xe8, 0, 0, 0, 0,             // subl Val(%ebx), %eax
335   };
336   memcpy(loc - 3, inst, sizeof(inst));
337   write32le(loc + 5, val);
338 }
339 
relaxTlsGdToIe(uint8_t * loc,const Relocation &,uint64_t val) const340 void X86::relaxTlsGdToIe(uint8_t *loc, const Relocation &, uint64_t val) const {
341   // Convert
342   //   leal x@tlsgd(, %ebx, 1),
343   //   call __tls_get_addr@plt
344   // to
345   //   movl %gs:0, %eax
346   //   addl x@gotntpoff(%ebx), %eax
347   const uint8_t inst[] = {
348       0x65, 0xa1, 0x00, 0x00, 0x00, 0x00, // movl %gs:0, %eax
349       0x03, 0x83, 0, 0, 0, 0,             // addl Val(%ebx), %eax
350   };
351   memcpy(loc - 3, inst, sizeof(inst));
352   write32le(loc + 5, val);
353 }
354 
355 // In some conditions, relocations can be optimized to avoid using GOT.
356 // This function does that for Initial Exec to Local Exec case.
relaxTlsIeToLe(uint8_t * loc,const Relocation & rel,uint64_t val) const357 void X86::relaxTlsIeToLe(uint8_t *loc, const Relocation &rel,
358                          uint64_t val) const {
359   // Ulrich's document section 6.2 says that @gotntpoff can
360   // be used with MOVL or ADDL instructions.
361   // @indntpoff is similar to @gotntpoff, but for use in
362   // position dependent code.
363   uint8_t reg = (loc[-1] >> 3) & 7;
364 
365   if (rel.type == R_386_TLS_IE) {
366     if (loc[-1] == 0xa1) {
367       // "movl foo@indntpoff,%eax" -> "movl $foo,%eax"
368       // This case is different from the generic case below because
369       // this is a 5 byte instruction while below is 6 bytes.
370       loc[-1] = 0xb8;
371     } else if (loc[-2] == 0x8b) {
372       // "movl foo@indntpoff,%reg" -> "movl $foo,%reg"
373       loc[-2] = 0xc7;
374       loc[-1] = 0xc0 | reg;
375     } else {
376       // "addl foo@indntpoff,%reg" -> "addl $foo,%reg"
377       loc[-2] = 0x81;
378       loc[-1] = 0xc0 | reg;
379     }
380   } else {
381     assert(rel.type == R_386_TLS_GOTIE);
382     if (loc[-2] == 0x8b) {
383       // "movl foo@gottpoff(%rip),%reg" -> "movl $foo,%reg"
384       loc[-2] = 0xc7;
385       loc[-1] = 0xc0 | reg;
386     } else {
387       // "addl foo@gotntpoff(%rip),%reg" -> "leal foo(%reg),%reg"
388       loc[-2] = 0x8d;
389       loc[-1] = 0x80 | (reg << 3) | reg;
390     }
391   }
392   write32le(loc, val);
393 }
394 
relaxTlsLdToLe(uint8_t * loc,const Relocation & rel,uint64_t val) const395 void X86::relaxTlsLdToLe(uint8_t *loc, const Relocation &rel,
396                          uint64_t val) const {
397   if (rel.type == R_386_TLS_LDO_32) {
398     write32le(loc, val);
399     return;
400   }
401 
402   // Convert
403   //   leal foo(%reg),%eax
404   //   call ___tls_get_addr
405   // to
406   //   movl %gs:0,%eax
407   //   nop
408   //   leal 0(%esi,1),%esi
409   const uint8_t inst[] = {
410       0x65, 0xa1, 0x00, 0x00, 0x00, 0x00, // movl %gs:0,%eax
411       0x90,                               // nop
412       0x8d, 0x74, 0x26, 0x00,             // leal 0(%esi,1),%esi
413   };
414   memcpy(loc - 2, inst, sizeof(inst));
415 }
416 
417 // If Intel Indirect Branch Tracking is enabled, we have to emit special PLT
418 // entries containing endbr32 instructions. A PLT entry will be split into two
419 // parts, one in .plt.sec (writePlt), and the other in .plt (writeIBTPlt).
420 namespace {
421 class IntelIBT : public X86 {
422 public:
423   IntelIBT();
424   void writeGotPlt(uint8_t *buf, const Symbol &s) const override;
425   void writePlt(uint8_t *buf, const Symbol &sym,
426                 uint64_t pltEntryAddr) const override;
427   void writeIBTPlt(uint8_t *buf, size_t numEntries) const override;
428 
429   static const unsigned IBTPltHeaderSize = 16;
430 };
431 } // namespace
432 
IntelIBT()433 IntelIBT::IntelIBT() { pltHeaderSize = 0; }
434 
writeGotPlt(uint8_t * buf,const Symbol & s) const435 void IntelIBT::writeGotPlt(uint8_t *buf, const Symbol &s) const {
436   uint64_t va =
437       in.ibtPlt->getVA() + IBTPltHeaderSize + s.pltIndex * pltEntrySize;
438   write32le(buf, va);
439 }
440 
writePlt(uint8_t * buf,const Symbol & sym,uint64_t) const441 void IntelIBT::writePlt(uint8_t *buf, const Symbol &sym,
442                         uint64_t /*pltEntryAddr*/) const {
443   if (config->isPic) {
444     const uint8_t inst[] = {
445         0xf3, 0x0f, 0x1e, 0xfb,       // endbr32
446         0xff, 0xa3, 0,    0,    0, 0, // jmp *name@GOT(%ebx)
447         0x66, 0x0f, 0x1f, 0x44, 0, 0, // nop
448     };
449     memcpy(buf, inst, sizeof(inst));
450     write32le(buf + 6, sym.getGotPltVA() - in.gotPlt->getVA());
451     return;
452   }
453 
454   const uint8_t inst[] = {
455       0xf3, 0x0f, 0x1e, 0xfb,       // endbr32
456       0xff, 0x25, 0,    0,    0, 0, // jmp *foo@GOT
457       0x66, 0x0f, 0x1f, 0x44, 0, 0, // nop
458   };
459   memcpy(buf, inst, sizeof(inst));
460   write32le(buf + 6, sym.getGotPltVA());
461 }
462 
writeIBTPlt(uint8_t * buf,size_t numEntries) const463 void IntelIBT::writeIBTPlt(uint8_t *buf, size_t numEntries) const {
464   writePltHeader(buf);
465   buf += IBTPltHeaderSize;
466 
467   const uint8_t inst[] = {
468       0xf3, 0x0f, 0x1e, 0xfb,    // endbr32
469       0x68, 0,    0,    0,    0, // pushl $reloc_offset
470       0xe9, 0,    0,    0,    0, // jmpq .PLT0@PC
471       0x66, 0x90,                // nop
472   };
473 
474   for (size_t i = 0; i < numEntries; ++i) {
475     memcpy(buf, inst, sizeof(inst));
476     write32le(buf + 5, i * sizeof(object::ELF32LE::Rel));
477     write32le(buf + 10, -pltHeaderSize - sizeof(inst) * i - 30);
478     buf += sizeof(inst);
479   }
480 }
481 
482 namespace {
483 class RetpolinePic : public X86 {
484 public:
485   RetpolinePic();
486   void writeGotPlt(uint8_t *buf, const Symbol &s) const override;
487   void writePltHeader(uint8_t *buf) const override;
488   void writePlt(uint8_t *buf, const Symbol &sym,
489                 uint64_t pltEntryAddr) const override;
490 };
491 
492 class RetpolineNoPic : public X86 {
493 public:
494   RetpolineNoPic();
495   void writeGotPlt(uint8_t *buf, const Symbol &s) const override;
496   void writePltHeader(uint8_t *buf) const override;
497   void writePlt(uint8_t *buf, const Symbol &sym,
498                 uint64_t pltEntryAddr) const override;
499 };
500 } // namespace
501 
RetpolinePic()502 RetpolinePic::RetpolinePic() {
503   pltHeaderSize = 48;
504   pltEntrySize = 32;
505   ipltEntrySize = 32;
506 }
507 
writeGotPlt(uint8_t * buf,const Symbol & s) const508 void RetpolinePic::writeGotPlt(uint8_t *buf, const Symbol &s) const {
509   write32le(buf, s.getPltVA() + 17);
510 }
511 
writePltHeader(uint8_t * buf) const512 void RetpolinePic::writePltHeader(uint8_t *buf) const {
513   const uint8_t insn[] = {
514       0xff, 0xb3, 4,    0,    0,    0,          // 0:    pushl 4(%ebx)
515       0x50,                                     // 6:    pushl %eax
516       0x8b, 0x83, 8,    0,    0,    0,          // 7:    mov 8(%ebx), %eax
517       0xe8, 0x0e, 0x00, 0x00, 0x00,             // d:    call next
518       0xf3, 0x90,                               // 12: loop: pause
519       0x0f, 0xae, 0xe8,                         // 14:   lfence
520       0xeb, 0xf9,                               // 17:   jmp loop
521       0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, // 19:   int3; .align 16
522       0x89, 0x0c, 0x24,                         // 20: next: mov %ecx, (%esp)
523       0x8b, 0x4c, 0x24, 0x04,                   // 23:   mov 0x4(%esp), %ecx
524       0x89, 0x44, 0x24, 0x04,                   // 27:   mov %eax ,0x4(%esp)
525       0x89, 0xc8,                               // 2b:   mov %ecx, %eax
526       0x59,                                     // 2d:   pop %ecx
527       0xc3,                                     // 2e:   ret
528       0xcc,                                     // 2f:   int3; padding
529   };
530   memcpy(buf, insn, sizeof(insn));
531 }
532 
writePlt(uint8_t * buf,const Symbol & sym,uint64_t pltEntryAddr) const533 void RetpolinePic::writePlt(uint8_t *buf, const Symbol &sym,
534                             uint64_t pltEntryAddr) const {
535   unsigned relOff = in.relaPlt->entsize * sym.pltIndex;
536   const uint8_t insn[] = {
537       0x50,                            // pushl %eax
538       0x8b, 0x83, 0,    0,    0,    0, // mov foo@GOT(%ebx), %eax
539       0xe8, 0,    0,    0,    0,       // call plt+0x20
540       0xe9, 0,    0,    0,    0,       // jmp plt+0x12
541       0x68, 0,    0,    0,    0,       // pushl $reloc_offset
542       0xe9, 0,    0,    0,    0,       // jmp plt+0
543       0xcc, 0xcc, 0xcc, 0xcc, 0xcc,    // int3; padding
544   };
545   memcpy(buf, insn, sizeof(insn));
546 
547   uint32_t ebx = in.gotPlt->getVA();
548   unsigned off = pltEntryAddr - in.plt->getVA();
549   write32le(buf + 3, sym.getGotPltVA() - ebx);
550   write32le(buf + 8, -off - 12 + 32);
551   write32le(buf + 13, -off - 17 + 18);
552   write32le(buf + 18, relOff);
553   write32le(buf + 23, -off - 27);
554 }
555 
RetpolineNoPic()556 RetpolineNoPic::RetpolineNoPic() {
557   pltHeaderSize = 48;
558   pltEntrySize = 32;
559   ipltEntrySize = 32;
560 }
561 
writeGotPlt(uint8_t * buf,const Symbol & s) const562 void RetpolineNoPic::writeGotPlt(uint8_t *buf, const Symbol &s) const {
563   write32le(buf, s.getPltVA() + 16);
564 }
565 
writePltHeader(uint8_t * buf) const566 void RetpolineNoPic::writePltHeader(uint8_t *buf) const {
567   const uint8_t insn[] = {
568       0xff, 0x35, 0,    0,    0,    0, // 0:    pushl GOTPLT+4
569       0x50,                            // 6:    pushl %eax
570       0xa1, 0,    0,    0,    0,       // 7:    mov GOTPLT+8, %eax
571       0xe8, 0x0f, 0x00, 0x00, 0x00,    // c:    call next
572       0xf3, 0x90,                      // 11: loop: pause
573       0x0f, 0xae, 0xe8,                // 13:   lfence
574       0xeb, 0xf9,                      // 16:   jmp loop
575       0xcc, 0xcc, 0xcc, 0xcc, 0xcc,    // 18:   int3
576       0xcc, 0xcc, 0xcc,                // 1f:   int3; .align 16
577       0x89, 0x0c, 0x24,                // 20: next: mov %ecx, (%esp)
578       0x8b, 0x4c, 0x24, 0x04,          // 23:   mov 0x4(%esp), %ecx
579       0x89, 0x44, 0x24, 0x04,          // 27:   mov %eax ,0x4(%esp)
580       0x89, 0xc8,                      // 2b:   mov %ecx, %eax
581       0x59,                            // 2d:   pop %ecx
582       0xc3,                            // 2e:   ret
583       0xcc,                            // 2f:   int3; padding
584   };
585   memcpy(buf, insn, sizeof(insn));
586 
587   uint32_t gotPlt = in.gotPlt->getVA();
588   write32le(buf + 2, gotPlt + 4);
589   write32le(buf + 8, gotPlt + 8);
590 }
591 
writePlt(uint8_t * buf,const Symbol & sym,uint64_t pltEntryAddr) const592 void RetpolineNoPic::writePlt(uint8_t *buf, const Symbol &sym,
593                               uint64_t pltEntryAddr) const {
594   unsigned relOff = in.relaPlt->entsize * sym.pltIndex;
595   const uint8_t insn[] = {
596       0x50,                         // 0:  pushl %eax
597       0xa1, 0,    0,    0,    0,    // 1:  mov foo_in_GOT, %eax
598       0xe8, 0,    0,    0,    0,    // 6:  call plt+0x20
599       0xe9, 0,    0,    0,    0,    // b:  jmp plt+0x11
600       0x68, 0,    0,    0,    0,    // 10: pushl $reloc_offset
601       0xe9, 0,    0,    0,    0,    // 15: jmp plt+0
602       0xcc, 0xcc, 0xcc, 0xcc, 0xcc, // 1a: int3; padding
603       0xcc,                         // 1f: int3; padding
604   };
605   memcpy(buf, insn, sizeof(insn));
606 
607   unsigned off = pltEntryAddr - in.plt->getVA();
608   write32le(buf + 2, sym.getGotPltVA());
609   write32le(buf + 7, -off - 11 + 32);
610   write32le(buf + 12, -off - 16 + 17);
611   write32le(buf + 17, relOff);
612   write32le(buf + 22, -off - 26);
613 }
614 
getX86TargetInfo()615 TargetInfo *elf::getX86TargetInfo() {
616   if (config->zRetpolineplt) {
617     if (config->isPic) {
618       static RetpolinePic t;
619       return &t;
620     }
621     static RetpolineNoPic t;
622     return &t;
623   }
624 
625   if (config->andFeatures & GNU_PROPERTY_X86_FEATURE_1_IBT) {
626     static IntelIBT t;
627     return &t;
628   }
629 
630   static X86 t;
631   return &t;
632 }
633