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