1 //===- Target.cpp ---------------------------------------------------------===// 2 // 3 // The LLVM Linker 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // Machine-specific things, such as applying relocations, creation of 11 // GOT or PLT entries, etc., are handled in this file. 12 // 13 // Refer the ELF spec for the single letter varaibles, S, A or P, used 14 // in this file. SA is S+A. 15 // 16 //===----------------------------------------------------------------------===// 17 18 #include "Target.h" 19 #include "Error.h" 20 #include "OutputSections.h" 21 #include "Symbols.h" 22 23 #include "llvm/ADT/ArrayRef.h" 24 #include "llvm/Object/ELF.h" 25 #include "llvm/Support/Endian.h" 26 #include "llvm/Support/ELF.h" 27 28 using namespace llvm; 29 using namespace llvm::object; 30 using namespace llvm::support::endian; 31 using namespace llvm::ELF; 32 33 namespace lld { 34 namespace elf2 { 35 36 std::unique_ptr<TargetInfo> Target; 37 38 template <endianness E> static void add32(void *P, int32_t V) { 39 write32<E>(P, read32<E>(P) + V); 40 } 41 42 static void add32le(uint8_t *P, int32_t V) { add32<support::little>(P, V); } 43 static void or32le(uint8_t *P, int32_t V) { write32le(P, read32le(P) | V); } 44 45 namespace { 46 class X86TargetInfo final : public TargetInfo { 47 public: 48 X86TargetInfo(); 49 void writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const override; 50 void writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr, 51 uint64_t PltEntryAddr) const override; 52 void writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr, 53 uint64_t PltEntryAddr, int32_t Index) const override; 54 bool relocNeedsGot(uint32_t Type, const SymbolBody &S) const override; 55 bool relocPointsToGot(uint32_t Type) const override; 56 bool relocNeedsPlt(uint32_t Type, const SymbolBody &S) const override; 57 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P, 58 uint64_t SA) const override; 59 }; 60 61 class X86_64TargetInfo final : public TargetInfo { 62 public: 63 X86_64TargetInfo(); 64 unsigned getPLTRefReloc(unsigned Type) const override; 65 void writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const override; 66 void writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr, 67 uint64_t PltEntryAddr) const override; 68 void writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr, 69 uint64_t PltEntryAddr, int32_t Index) const override; 70 bool relocNeedsCopy(uint32_t Type, const SymbolBody &S) const override; 71 bool relocNeedsGot(uint32_t Type, const SymbolBody &S) const override; 72 bool relocNeedsPlt(uint32_t Type, const SymbolBody &S) const override; 73 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P, 74 uint64_t SA) const override; 75 bool isRelRelative(uint32_t Type) const override; 76 }; 77 78 class PPC64TargetInfo final : public TargetInfo { 79 public: 80 PPC64TargetInfo(); 81 void writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const override; 82 void writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr, 83 uint64_t PltEntryAddr) const override; 84 void writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr, 85 uint64_t PltEntryAddr, int32_t Index) const override; 86 bool relocNeedsGot(uint32_t Type, const SymbolBody &S) const override; 87 bool relocNeedsPlt(uint32_t Type, const SymbolBody &S) const override; 88 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P, 89 uint64_t SA) const override; 90 bool isRelRelative(uint32_t Type) const override; 91 }; 92 93 class AArch64TargetInfo final : public TargetInfo { 94 public: 95 AArch64TargetInfo(); 96 void writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const override; 97 void writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr, 98 uint64_t PltEntryAddr) const override; 99 void writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr, 100 uint64_t PltEntryAddr, int32_t Index) const override; 101 bool relocNeedsGot(uint32_t Type, const SymbolBody &S) const override; 102 bool relocNeedsPlt(uint32_t Type, const SymbolBody &S) const override; 103 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P, 104 uint64_t SA) const override; 105 }; 106 107 template <class ELFT> class MipsTargetInfo final : public TargetInfo { 108 public: 109 MipsTargetInfo(); 110 void writeGotHeaderEntries(uint8_t *Buf) const override; 111 void writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const override; 112 void writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr, 113 uint64_t PltEntryAddr) const override; 114 void writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr, 115 uint64_t PltEntryAddr, int32_t Index) const override; 116 bool relocNeedsGot(uint32_t Type, const SymbolBody &S) const override; 117 bool relocNeedsPlt(uint32_t Type, const SymbolBody &S) const override; 118 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P, 119 uint64_t SA) const override; 120 }; 121 } // anonymous namespace 122 123 TargetInfo *createTarget() { 124 switch (Config->EMachine) { 125 case EM_386: 126 return new X86TargetInfo(); 127 case EM_AARCH64: 128 return new AArch64TargetInfo(); 129 case EM_MIPS: 130 switch (Config->EKind) { 131 case ELF32LEKind: 132 return new MipsTargetInfo<ELF32LE>(); 133 case ELF32BEKind: 134 return new MipsTargetInfo<ELF32BE>(); 135 default: 136 error("Unsupported MIPS target"); 137 } 138 case EM_PPC64: 139 return new PPC64TargetInfo(); 140 case EM_X86_64: 141 return new X86_64TargetInfo(); 142 } 143 error("Unknown target machine"); 144 } 145 146 TargetInfo::~TargetInfo() {} 147 148 uint64_t TargetInfo::getVAStart() const { return Config->Shared ? 0 : VAStart; } 149 150 bool TargetInfo::relocNeedsCopy(uint32_t Type, const SymbolBody &S) const { 151 return false; 152 } 153 154 unsigned TargetInfo::getPLTRefReloc(unsigned Type) const { return PCRelReloc; } 155 156 bool TargetInfo::relocPointsToGot(uint32_t Type) const { return false; } 157 158 bool TargetInfo::isRelRelative(uint32_t Type) const { return true; } 159 160 void TargetInfo::writeGotHeaderEntries(uint8_t *Buf) const {} 161 162 X86TargetInfo::X86TargetInfo() { 163 PCRelReloc = R_386_PC32; 164 GotReloc = R_386_GLOB_DAT; 165 GotRefReloc = R_386_GOT32; 166 PltReloc = R_386_JUMP_SLOT; 167 } 168 169 void X86TargetInfo::writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const {} 170 void X86TargetInfo::writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr, 171 uint64_t PltEntryAddr) const {} 172 173 void X86TargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr, 174 uint64_t PltEntryAddr, int32_t Index) const { 175 // jmpl *val; nop; nop 176 const uint8_t Inst[] = {0xff, 0x25, 0, 0, 0, 0, 0x90, 0x90}; 177 memcpy(Buf, Inst, sizeof(Inst)); 178 assert(isUInt<32>(GotEntryAddr)); 179 write32le(Buf + 2, GotEntryAddr); 180 } 181 182 bool X86TargetInfo::relocNeedsGot(uint32_t Type, const SymbolBody &S) const { 183 return Type == R_386_GOT32 || relocNeedsPlt(Type, S); 184 } 185 186 bool X86TargetInfo::relocPointsToGot(uint32_t Type) const { 187 return Type == R_386_GOTPC; 188 } 189 190 bool X86TargetInfo::relocNeedsPlt(uint32_t Type, const SymbolBody &S) const { 191 return Type == R_386_PLT32 || (Type == R_386_PC32 && S.isShared()); 192 } 193 194 void X86TargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, 195 uint64_t P, uint64_t SA) const { 196 switch (Type) { 197 case R_386_GOT32: 198 add32le(Loc, SA - Out<ELF32LE>::Got->getVA()); 199 break; 200 case R_386_PC32: 201 add32le(Loc, SA - P); 202 break; 203 case R_386_32: 204 add32le(Loc, SA); 205 break; 206 default: 207 error("unrecognized reloc " + Twine(Type)); 208 } 209 } 210 211 X86_64TargetInfo::X86_64TargetInfo() { 212 CopyReloc = R_X86_64_COPY; 213 PCRelReloc = R_X86_64_PC32; 214 GotReloc = R_X86_64_GLOB_DAT; 215 GotRefReloc = R_X86_64_PC32; 216 PltReloc = R_X86_64_JUMP_SLOT; 217 RelativeReloc = R_X86_64_RELATIVE; 218 LazyRelocations = true; 219 PltEntrySize = 16; 220 PltZeroEntrySize = 16; 221 } 222 223 void X86_64TargetInfo::writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const { 224 // Skip 6 bytes of "jmpq *got(%rip)" 225 write32le(Buf, Plt + 6); 226 } 227 228 void X86_64TargetInfo::writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr, 229 uint64_t PltEntryAddr) const { 230 const uint8_t PltData[] = { 231 0xff, 0x35, 0x00, 0x00, 0x00, 0x00, // pushq GOT+8(%rip) 232 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // jmp *GOT+16(%rip) 233 0x0f, 0x1f, 0x40, 0x00 // nopl 0x0(rax) 234 }; 235 memcpy(Buf, PltData, sizeof(PltData)); 236 write32le(Buf + 2, GotEntryAddr - PltEntryAddr + 2); // GOT+8 237 write32le(Buf + 8, GotEntryAddr - PltEntryAddr + 4); // GOT+16 238 } 239 240 void X86_64TargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr, 241 uint64_t PltEntryAddr, 242 int32_t Index) const { 243 const uint8_t Inst[] = { 244 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // jmpq *got(%rip) 245 0x68, 0x00, 0x00, 0x00, 0x00, // pushq <relocation index> 246 0xe9, 0x00, 0x00, 0x00, 0x00 // jmpq plt[0] 247 }; 248 memcpy(Buf, Inst, sizeof(Inst)); 249 250 write32le(Buf + 2, GotEntryAddr - PltEntryAddr - 6); 251 write32le(Buf + 7, Index); 252 write32le(Buf + 12, -Index * PltEntrySize - PltZeroEntrySize - 16); 253 } 254 255 bool X86_64TargetInfo::relocNeedsCopy(uint32_t Type, 256 const SymbolBody &S) const { 257 if (Type == R_X86_64_32S || Type == R_X86_64_32 || Type == R_X86_64_PC32 || 258 Type == R_X86_64_64) 259 if (auto *SS = dyn_cast<SharedSymbol<ELF64LE>>(&S)) 260 return SS->Sym.getType() == STT_OBJECT; 261 return false; 262 } 263 264 bool X86_64TargetInfo::relocNeedsGot(uint32_t Type, const SymbolBody &S) const { 265 return Type == R_X86_64_GOTPCREL || relocNeedsPlt(Type, S); 266 } 267 268 unsigned X86_64TargetInfo::getPLTRefReloc(unsigned Type) const { 269 if (Type == R_X86_64_PLT32) 270 return R_X86_64_PC32; 271 return Type; 272 } 273 274 bool X86_64TargetInfo::relocNeedsPlt(uint32_t Type, const SymbolBody &S) const { 275 if (relocNeedsCopy(Type, S)) 276 return false; 277 278 switch (Type) { 279 default: 280 return false; 281 case R_X86_64_32: 282 case R_X86_64_64: 283 case R_X86_64_PC32: 284 // This relocation is defined to have a value of (S + A - P). 285 // The problems start when a non PIC program calls a function in a shared 286 // library. 287 // In an ideal world, we could just report an error saying the relocation 288 // can overflow at runtime. 289 // In the real world with glibc, crt1.o has a R_X86_64_PC32 pointing to 290 // libc.so. 291 // 292 // The general idea on how to handle such cases is to create a PLT entry 293 // and use that as the function value. 294 // 295 // For the static linking part, we just return true and everything else 296 // will use the the PLT entry as the address. 297 // 298 // The remaining (unimplemented) problem is making sure pointer equality 299 // still works. We need the help of the dynamic linker for that. We 300 // let it know that we have a direct reference to a so symbol by creating 301 // an undefined symbol with a non zero st_value. Seeing that, the 302 // dynamic linker resolves the symbol to the value of the symbol we created. 303 // This is true even for got entries, so pointer equality is maintained. 304 // To avoid an infinite loop, the only entry that points to the 305 // real function is a dedicated got entry used by the plt. That is 306 // identified by special relocation types (R_X86_64_JUMP_SLOT, 307 // R_386_JMP_SLOT, etc). 308 return S.isShared(); 309 case R_X86_64_PLT32: 310 return canBePreempted(&S, true); 311 } 312 } 313 314 bool X86_64TargetInfo::isRelRelative(uint32_t Type) const { 315 switch (Type) { 316 default: 317 return false; 318 case R_X86_64_PC64: 319 case R_X86_64_PC32: 320 case R_X86_64_PC16: 321 case R_X86_64_PC8: 322 case R_X86_64_PLT32: 323 return true; 324 } 325 } 326 327 void X86_64TargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, 328 uint64_t P, uint64_t SA) const { 329 switch (Type) { 330 case R_X86_64_PC32: 331 case R_X86_64_GOTPCREL: 332 case R_X86_64_PLT32: 333 write32le(Loc, SA - P); 334 break; 335 case R_X86_64_64: 336 write64le(Loc, SA); 337 break; 338 case R_X86_64_32: 339 case R_X86_64_32S: 340 if (Type == R_X86_64_32 && !isUInt<32>(SA)) 341 error("R_X86_64_32 out of range"); 342 else if (!isInt<32>(SA)) 343 error("R_X86_64_32S out of range"); 344 write32le(Loc, SA); 345 break; 346 case R_X86_64_TPOFF32: { 347 uint64_t Val = SA - Out<ELF64LE>::TlsPhdr->p_memsz; 348 if (!isInt<32>(Val)) 349 error("R_X86_64_TPOFF32 out of range"); 350 write32le(Loc, Val); 351 break; 352 } 353 default: 354 error("unrecognized reloc " + Twine(Type)); 355 } 356 } 357 358 // Relocation masks following the #lo(value), #hi(value), #ha(value), 359 // #higher(value), #highera(value), #highest(value), and #highesta(value) 360 // macros defined in section 4.5.1. Relocation Types of the PPC-elf64abi 361 // document. 362 static uint16_t applyPPCLo(uint64_t V) { return V; } 363 static uint16_t applyPPCHi(uint64_t V) { return V >> 16; } 364 static uint16_t applyPPCHa(uint64_t V) { return (V + 0x8000) >> 16; } 365 static uint16_t applyPPCHigher(uint64_t V) { return V >> 32; } 366 static uint16_t applyPPCHighera(uint64_t V) { return (V + 0x8000) >> 32; } 367 static uint16_t applyPPCHighest(uint64_t V) { return V >> 48; } 368 static uint16_t applyPPCHighesta(uint64_t V) { return (V + 0x8000) >> 48; } 369 370 PPC64TargetInfo::PPC64TargetInfo() { 371 PCRelReloc = R_PPC64_REL24; 372 GotReloc = R_PPC64_GLOB_DAT; 373 GotRefReloc = R_PPC64_REL64; 374 RelativeReloc = R_PPC64_RELATIVE; 375 PltEntrySize = 32; 376 377 // We need 64K pages (at least under glibc/Linux, the loader won't 378 // set different permissions on a finer granularity than that). 379 PageSize = 65536; 380 381 // The PPC64 ELF ABI v1 spec, says: 382 // 383 // It is normally desirable to put segments with different characteristics 384 // in separate 256 Mbyte portions of the address space, to give the 385 // operating system full paging flexibility in the 64-bit address space. 386 // 387 // And because the lowest non-zero 256M boundary is 0x10000000, PPC64 linkers 388 // use 0x10000000 as the starting address. 389 VAStart = 0x10000000; 390 } 391 392 uint64_t getPPC64TocBase() { 393 // The TOC consists of sections .got, .toc, .tocbss, .plt in that 394 // order. The TOC starts where the first of these sections starts. 395 396 // FIXME: This obviously does not do the right thing when there is no .got 397 // section, but there is a .toc or .tocbss section. 398 uint64_t TocVA = Out<ELF64BE>::Got->getVA(); 399 if (!TocVA) 400 TocVA = Out<ELF64BE>::Plt->getVA(); 401 402 // Per the ppc64-elf-linux ABI, The TOC base is TOC value plus 0x8000 403 // thus permitting a full 64 Kbytes segment. Note that the glibc startup 404 // code (crt1.o) assumes that you can get from the TOC base to the 405 // start of the .toc section with only a single (signed) 16-bit relocation. 406 return TocVA + 0x8000; 407 } 408 409 void PPC64TargetInfo::writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const {} 410 void PPC64TargetInfo::writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr, 411 uint64_t PltEntryAddr) const {} 412 void PPC64TargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr, 413 uint64_t PltEntryAddr, int32_t Index) const { 414 uint64_t Off = GotEntryAddr - getPPC64TocBase(); 415 416 // FIXME: What we should do, in theory, is get the offset of the function 417 // descriptor in the .opd section, and use that as the offset from %r2 (the 418 // TOC-base pointer). Instead, we have the GOT-entry offset, and that will 419 // be a pointer to the function descriptor in the .opd section. Using 420 // this scheme is simpler, but requires an extra indirection per PLT dispatch. 421 422 write32be(Buf, 0xf8410028); // std %r2, 40(%r1) 423 write32be(Buf + 4, 0x3d620000 | applyPPCHa(Off)); // addis %r11, %r2, X@ha 424 write32be(Buf + 8, 0xe98b0000 | applyPPCLo(Off)); // ld %r12, X@l(%r11) 425 write32be(Buf + 12, 0xe96c0000); // ld %r11,0(%r12) 426 write32be(Buf + 16, 0x7d6903a6); // mtctr %r11 427 write32be(Buf + 20, 0xe84c0008); // ld %r2,8(%r12) 428 write32be(Buf + 24, 0xe96c0010); // ld %r11,16(%r12) 429 write32be(Buf + 28, 0x4e800420); // bctr 430 } 431 432 bool PPC64TargetInfo::relocNeedsGot(uint32_t Type, const SymbolBody &S) const { 433 if (relocNeedsPlt(Type, S)) 434 return true; 435 436 switch (Type) { 437 default: return false; 438 case R_PPC64_GOT16: 439 case R_PPC64_GOT16_LO: 440 case R_PPC64_GOT16_HI: 441 case R_PPC64_GOT16_HA: 442 case R_PPC64_GOT16_DS: 443 case R_PPC64_GOT16_LO_DS: 444 return true; 445 } 446 } 447 448 bool PPC64TargetInfo::relocNeedsPlt(uint32_t Type, const SymbolBody &S) const { 449 // These are function calls that need to be redirected through a PLT stub. 450 return Type == R_PPC64_REL24 && canBePreempted(&S, false); 451 } 452 453 bool PPC64TargetInfo::isRelRelative(uint32_t Type) const { 454 switch (Type) { 455 default: 456 return true; 457 case R_PPC64_TOC: 458 case R_PPC64_ADDR64: 459 return false; 460 } 461 } 462 463 void PPC64TargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, 464 uint64_t P, uint64_t SA) const { 465 uint64_t TB = getPPC64TocBase(); 466 467 // For a TOC-relative relocation, adjust the addend and proceed in terms of 468 // the corresponding ADDR16 relocation type. 469 switch (Type) { 470 case R_PPC64_TOC16: Type = R_PPC64_ADDR16; SA -= TB; break; 471 case R_PPC64_TOC16_DS: Type = R_PPC64_ADDR16_DS; SA -= TB; break; 472 case R_PPC64_TOC16_LO: Type = R_PPC64_ADDR16_LO; SA -= TB; break; 473 case R_PPC64_TOC16_LO_DS: Type = R_PPC64_ADDR16_LO_DS; SA -= TB; break; 474 case R_PPC64_TOC16_HI: Type = R_PPC64_ADDR16_HI; SA -= TB; break; 475 case R_PPC64_TOC16_HA: Type = R_PPC64_ADDR16_HA; SA -= TB; break; 476 default: break; 477 } 478 479 switch (Type) { 480 case R_PPC64_ADDR16: 481 if (!isInt<16>(SA)) 482 error("Relocation R_PPC64_ADDR16 overflow"); 483 write16be(Loc, SA); 484 break; 485 case R_PPC64_ADDR16_DS: 486 if (!isInt<16>(SA)) 487 error("Relocation R_PPC64_ADDR16_DS overflow"); 488 write16be(Loc, (read16be(Loc) & 3) | (SA & ~3)); 489 break; 490 case R_PPC64_ADDR16_LO: 491 write16be(Loc, applyPPCLo(SA)); 492 break; 493 case R_PPC64_ADDR16_LO_DS: 494 write16be(Loc, (read16be(Loc) & 3) | (applyPPCLo(SA) & ~3)); 495 break; 496 case R_PPC64_ADDR16_HI: 497 write16be(Loc, applyPPCHi(SA)); 498 break; 499 case R_PPC64_ADDR16_HA: 500 write16be(Loc, applyPPCHa(SA)); 501 break; 502 case R_PPC64_ADDR16_HIGHER: 503 write16be(Loc, applyPPCHigher(SA)); 504 break; 505 case R_PPC64_ADDR16_HIGHERA: 506 write16be(Loc, applyPPCHighera(SA)); 507 break; 508 case R_PPC64_ADDR16_HIGHEST: 509 write16be(Loc, applyPPCHighest(SA)); 510 break; 511 case R_PPC64_ADDR16_HIGHESTA: 512 write16be(Loc, applyPPCHighesta(SA)); 513 break; 514 case R_PPC64_ADDR14: { 515 if ((SA & 3) != 0) 516 error("Improper alignment for relocation R_PPC64_ADDR14"); 517 518 // Preserve the AA/LK bits in the branch instruction 519 uint8_t AALK = Loc[3]; 520 write16be(Loc + 2, (AALK & 3) | (SA & 0xfffc)); 521 break; 522 } 523 case R_PPC64_REL16_LO: 524 write16be(Loc, applyPPCLo(SA - P)); 525 break; 526 case R_PPC64_REL16_HI: 527 write16be(Loc, applyPPCHi(SA - P)); 528 break; 529 case R_PPC64_REL16_HA: 530 write16be(Loc, applyPPCHa(SA - P)); 531 break; 532 case R_PPC64_ADDR32: 533 if (!isInt<32>(SA)) 534 error("Relocation R_PPC64_ADDR32 overflow"); 535 write32be(Loc, SA); 536 break; 537 case R_PPC64_REL24: { 538 // If we have an undefined weak symbol, we might get here with a symbol 539 // address of zero. That could overflow, but the code must be unreachable, 540 // so don't bother doing anything at all. 541 if (!SA) 542 break; 543 544 uint64_t PltStart = Out<ELF64BE>::Plt->getVA(); 545 uint64_t PltEnd = PltStart + Out<ELF64BE>::Plt->getSize(); 546 bool InPlt = PltStart <= SA && SA < PltEnd; 547 548 if (!InPlt && Out<ELF64BE>::Opd) { 549 // If this is a local call, and we currently have the address of a 550 // function-descriptor, get the underlying code address instead. 551 uint64_t OpdStart = Out<ELF64BE>::Opd->getVA(); 552 uint64_t OpdEnd = OpdStart + Out<ELF64BE>::Opd->getSize(); 553 bool InOpd = OpdStart <= SA && SA < OpdEnd; 554 555 if (InOpd) 556 SA = read64be(&Out<ELF64BE>::OpdBuf[SA - OpdStart]); 557 } 558 559 uint32_t Mask = 0x03FFFFFC; 560 if (!isInt<24>(SA - P)) 561 error("Relocation R_PPC64_REL24 overflow"); 562 write32be(Loc, (read32be(Loc) & ~Mask) | ((SA - P) & Mask)); 563 564 uint32_t Nop = 0x60000000; 565 if (InPlt && Loc + 8 <= BufEnd && read32be(Loc + 4) == Nop) 566 write32be(Loc + 4, 0xe8410028); // ld %r2, 40(%r1) 567 break; 568 } 569 case R_PPC64_REL32: 570 if (!isInt<32>(SA - P)) 571 error("Relocation R_PPC64_REL32 overflow"); 572 write32be(Loc, SA - P); 573 break; 574 case R_PPC64_REL64: 575 write64be(Loc, SA - P); 576 break; 577 case R_PPC64_ADDR64: 578 case R_PPC64_TOC: 579 write64be(Loc, SA); 580 break; 581 default: 582 error("unrecognized reloc " + Twine(Type)); 583 } 584 } 585 586 AArch64TargetInfo::AArch64TargetInfo() {} 587 588 void AArch64TargetInfo::writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const {} 589 void AArch64TargetInfo::writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr, 590 uint64_t PltEntryAddr) const {} 591 void AArch64TargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr, 592 uint64_t PltEntryAddr, int32_t Index) const {} 593 bool AArch64TargetInfo::relocNeedsGot(uint32_t Type, 594 const SymbolBody &S) const { 595 return false; 596 } 597 bool AArch64TargetInfo::relocNeedsPlt(uint32_t Type, 598 const SymbolBody &S) const { 599 return false; 600 } 601 602 static void updateAArch64Adr(uint8_t *L, uint64_t Imm) { 603 uint32_t ImmLo = (Imm & 0x3) << 29; 604 uint32_t ImmHi = ((Imm & 0x1FFFFC) >> 2) << 5; 605 uint64_t Mask = (0x3 << 29) | (0x7FFFF << 5); 606 write32le(L, (read32le(L) & ~Mask) | ImmLo | ImmHi); 607 } 608 609 // Page(Expr) is the page address of the expression Expr, defined 610 // as (Expr & ~0xFFF). (This applies even if the machine page size 611 // supported by the platform has a different value.) 612 static uint64_t getAArch64Page(uint64_t Expr) { 613 return Expr & (~static_cast<uint64_t>(0xFFF)); 614 } 615 616 void AArch64TargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd, 617 uint32_t Type, uint64_t P, 618 uint64_t SA) const { 619 switch (Type) { 620 case R_AARCH64_ABS16: 621 if (!isInt<16>(SA)) 622 error("Relocation R_AARCH64_ABS16 out of range"); 623 write16le(Loc, SA); 624 break; 625 case R_AARCH64_ABS32: 626 if (!isInt<32>(SA)) 627 error("Relocation R_AARCH64_ABS32 out of range"); 628 write32le(Loc, SA); 629 break; 630 case R_AARCH64_ABS64: 631 // No overflow check needed. 632 write64le(Loc, SA); 633 break; 634 case R_AARCH64_ADD_ABS_LO12_NC: 635 // No overflow check needed. 636 // This relocation stores 12 bits and there's no instruction 637 // to do it. Instead, we do a 32 bits store of the value 638 // of r_addend bitwise-or'ed Loc. This assumes that the addend 639 // bits in Loc are zero. 640 or32le(Loc, (SA & 0xFFF) << 10); 641 break; 642 case R_AARCH64_ADR_PREL_LO21: { 643 uint64_t X = SA - P; 644 if (!isInt<21>(X)) 645 error("Relocation R_AARCH64_ADR_PREL_LO21 out of range"); 646 updateAArch64Adr(Loc, X & 0x1FFFFF); 647 break; 648 } 649 case R_AARCH64_ADR_PREL_PG_HI21: { 650 uint64_t X = getAArch64Page(SA) - getAArch64Page(P); 651 if (!isInt<33>(X)) 652 error("Relocation R_AARCH64_ADR_PREL_PG_HI21 out of range"); 653 updateAArch64Adr(Loc, (X >> 12) & 0x1FFFFF); // X[32:12] 654 break; 655 } 656 case R_AARCH64_LDST64_ABS_LO12_NC: 657 // No overflow check needed. 658 or32le(Loc, (SA & 0xFF8) << 7); 659 break; 660 case R_AARCH64_PREL16: 661 if (!isInt<16>(SA)) 662 error("Relocation R_AARCH64_PREL16 out of range"); 663 write16le(Loc, SA - P); 664 break; 665 case R_AARCH64_PREL32: 666 if (!isInt<32>(SA)) 667 error("Relocation R_AARCH64_PREL32 out of range"); 668 write32le(Loc, SA - P); 669 break; 670 case R_AARCH64_PREL64: 671 // No overflow check needed. 672 write64le(Loc, SA - P); 673 break; 674 default: 675 error("unrecognized reloc " + Twine(Type)); 676 } 677 } 678 679 template <class ELFT> MipsTargetInfo<ELFT>::MipsTargetInfo() { 680 PageSize = 65536; 681 GotRefReloc = R_MIPS_GOT16; 682 GotHeaderEntriesNum = 2; 683 } 684 685 template <class ELFT> 686 void MipsTargetInfo<ELFT>::writeGotHeaderEntries(uint8_t *Buf) const { 687 typedef typename llvm::object::ELFFile<ELFT>::Elf_Off Elf_Off; 688 auto *P = reinterpret_cast<Elf_Off *>(Buf); 689 // Module pointer 690 P[1] = ELFT::Is64Bits ? 0x8000000000000000 : 0x80000000; 691 } 692 693 template <class ELFT> 694 void MipsTargetInfo<ELFT>::writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const {} 695 template <class ELFT> 696 void MipsTargetInfo<ELFT>::writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr, 697 uint64_t PltEntryAddr) const {} 698 template <class ELFT> 699 void MipsTargetInfo<ELFT>::writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr, 700 uint64_t PltEntryAddr, int32_t Index) const {} 701 702 template <class ELFT> 703 bool MipsTargetInfo<ELFT>::relocNeedsGot(uint32_t Type, 704 const SymbolBody &S) const { 705 return Type == R_MIPS_GOT16; 706 } 707 708 template <class ELFT> 709 bool MipsTargetInfo<ELFT>::relocNeedsPlt(uint32_t Type, 710 const SymbolBody &S) const { 711 return false; 712 } 713 714 template <class ELFT> 715 void MipsTargetInfo<ELFT>::relocateOne(uint8_t *Loc, uint8_t *BufEnd, 716 uint32_t Type, uint64_t P, 717 uint64_t SA) const { 718 const endianness E = ELFT::TargetEndianness; 719 switch (Type) { 720 case R_MIPS_32: 721 add32<E>(Loc, SA); 722 break; 723 case R_MIPS_GOT16: { 724 int64_t V = SA - getMipsGpAddr<ELFT>(); 725 if (!isInt<16>(V)) 726 error("Relocation R_MIPS_GOT16 out of range"); 727 write32<E>(Loc, (read32<E>(Loc) & 0xffff0000) | (V & 0xffff)); 728 break; 729 } 730 default: 731 error("unrecognized reloc " + Twine(Type)); 732 } 733 } 734 735 template <class ELFT> 736 typename llvm::object::ELFFile<ELFT>::uintX_t getMipsGpAddr() { 737 const unsigned GPOffset = 0x7ff0; 738 return Out<ELFT>::Got->getVA() ? (Out<ELFT>::Got->getVA() + GPOffset) : 0; 739 } 740 741 template uint32_t getMipsGpAddr<ELF32LE>(); 742 template uint32_t getMipsGpAddr<ELF32BE>(); 743 template uint64_t getMipsGpAddr<ELF64LE>(); 744 template uint64_t getMipsGpAddr<ELF64BE>(); 745 } 746 } 747