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 switch (Type) { 266 case R_X86_64_32: 267 return R_X86_64_32; 268 case R_X86_64_PC32: 269 case R_X86_64_PLT32: 270 return R_X86_64_PC32; 271 } 272 llvm_unreachable("Unexpected relocation"); 273 } 274 275 bool X86_64TargetInfo::relocNeedsPlt(uint32_t Type, const SymbolBody &S) const { 276 if (relocNeedsCopy(Type, S)) 277 return false; 278 279 switch (Type) { 280 default: 281 return false; 282 case R_X86_64_32: 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 default: 347 error("unrecognized reloc " + Twine(Type)); 348 } 349 } 350 351 // Relocation masks following the #lo(value), #hi(value), #ha(value), 352 // #higher(value), #highera(value), #highest(value), and #highesta(value) 353 // macros defined in section 4.5.1. Relocation Types of the PPC-elf64abi 354 // document. 355 static uint16_t applyPPCLo(uint64_t V) { return V; } 356 static uint16_t applyPPCHi(uint64_t V) { return V >> 16; } 357 static uint16_t applyPPCHa(uint64_t V) { return (V + 0x8000) >> 16; } 358 static uint16_t applyPPCHigher(uint64_t V) { return V >> 32; } 359 static uint16_t applyPPCHighera(uint64_t V) { return (V + 0x8000) >> 32; } 360 static uint16_t applyPPCHighest(uint64_t V) { return V >> 48; } 361 static uint16_t applyPPCHighesta(uint64_t V) { return (V + 0x8000) >> 48; } 362 363 PPC64TargetInfo::PPC64TargetInfo() { 364 PCRelReloc = R_PPC64_REL24; 365 GotReloc = R_PPC64_GLOB_DAT; 366 GotRefReloc = R_PPC64_REL64; 367 RelativeReloc = R_PPC64_RELATIVE; 368 PltEntrySize = 32; 369 370 // We need 64K pages (at least under glibc/Linux, the loader won't 371 // set different permissions on a finer granularity than that). 372 PageSize = 65536; 373 374 // The PPC64 ELF ABI v1 spec, says: 375 // 376 // It is normally desirable to put segments with different characteristics 377 // in separate 256 Mbyte portions of the address space, to give the 378 // operating system full paging flexibility in the 64-bit address space. 379 // 380 // And because the lowest non-zero 256M boundary is 0x10000000, PPC64 linkers 381 // use 0x10000000 as the starting address. 382 VAStart = 0x10000000; 383 } 384 385 uint64_t getPPC64TocBase() { 386 // The TOC consists of sections .got, .toc, .tocbss, .plt in that 387 // order. The TOC starts where the first of these sections starts. 388 389 // FIXME: This obviously does not do the right thing when there is no .got 390 // section, but there is a .toc or .tocbss section. 391 uint64_t TocVA = Out<ELF64BE>::Got->getVA(); 392 if (!TocVA) 393 TocVA = Out<ELF64BE>::Plt->getVA(); 394 395 // Per the ppc64-elf-linux ABI, The TOC base is TOC value plus 0x8000 396 // thus permitting a full 64 Kbytes segment. Note that the glibc startup 397 // code (crt1.o) assumes that you can get from the TOC base to the 398 // start of the .toc section with only a single (signed) 16-bit relocation. 399 return TocVA + 0x8000; 400 } 401 402 void PPC64TargetInfo::writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const {} 403 void PPC64TargetInfo::writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr, 404 uint64_t PltEntryAddr) const {} 405 void PPC64TargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr, 406 uint64_t PltEntryAddr, int32_t Index) const { 407 uint64_t Off = GotEntryAddr - getPPC64TocBase(); 408 409 // FIXME: What we should do, in theory, is get the offset of the function 410 // descriptor in the .opd section, and use that as the offset from %r2 (the 411 // TOC-base pointer). Instead, we have the GOT-entry offset, and that will 412 // be a pointer to the function descriptor in the .opd section. Using 413 // this scheme is simpler, but requires an extra indirection per PLT dispatch. 414 415 write32be(Buf, 0xf8410028); // std %r2, 40(%r1) 416 write32be(Buf + 4, 0x3d620000 | applyPPCHa(Off)); // addis %r11, %r2, X@ha 417 write32be(Buf + 8, 0xe98b0000 | applyPPCLo(Off)); // ld %r12, X@l(%r11) 418 write32be(Buf + 12, 0xe96c0000); // ld %r11,0(%r12) 419 write32be(Buf + 16, 0x7d6903a6); // mtctr %r11 420 write32be(Buf + 20, 0xe84c0008); // ld %r2,8(%r12) 421 write32be(Buf + 24, 0xe96c0010); // ld %r11,16(%r12) 422 write32be(Buf + 28, 0x4e800420); // bctr 423 } 424 425 bool PPC64TargetInfo::relocNeedsGot(uint32_t Type, const SymbolBody &S) const { 426 if (relocNeedsPlt(Type, S)) 427 return true; 428 429 switch (Type) { 430 default: return false; 431 case R_PPC64_GOT16: 432 case R_PPC64_GOT16_LO: 433 case R_PPC64_GOT16_HI: 434 case R_PPC64_GOT16_HA: 435 case R_PPC64_GOT16_DS: 436 case R_PPC64_GOT16_LO_DS: 437 return true; 438 } 439 } 440 441 bool PPC64TargetInfo::relocNeedsPlt(uint32_t Type, const SymbolBody &S) const { 442 // These are function calls that need to be redirected through a PLT stub. 443 return Type == R_PPC64_REL24 && canBePreempted(&S, false); 444 } 445 446 bool PPC64TargetInfo::isRelRelative(uint32_t Type) const { 447 switch (Type) { 448 default: 449 return true; 450 case R_PPC64_TOC: 451 case R_PPC64_ADDR64: 452 return false; 453 } 454 } 455 456 void PPC64TargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, 457 uint64_t P, uint64_t SA) const { 458 uint64_t TB = getPPC64TocBase(); 459 460 // For a TOC-relative relocation, adjust the addend and proceed in terms of 461 // the corresponding ADDR16 relocation type. 462 switch (Type) { 463 case R_PPC64_TOC16: Type = R_PPC64_ADDR16; SA -= TB; break; 464 case R_PPC64_TOC16_DS: Type = R_PPC64_ADDR16_DS; SA -= TB; break; 465 case R_PPC64_TOC16_LO: Type = R_PPC64_ADDR16_LO; SA -= TB; break; 466 case R_PPC64_TOC16_LO_DS: Type = R_PPC64_ADDR16_LO_DS; SA -= TB; break; 467 case R_PPC64_TOC16_HI: Type = R_PPC64_ADDR16_HI; SA -= TB; break; 468 case R_PPC64_TOC16_HA: Type = R_PPC64_ADDR16_HA; SA -= TB; break; 469 default: break; 470 } 471 472 switch (Type) { 473 case R_PPC64_ADDR16: 474 if (!isInt<16>(SA)) 475 error("Relocation R_PPC64_ADDR16 overflow"); 476 write16be(Loc, SA); 477 break; 478 case R_PPC64_ADDR16_DS: 479 if (!isInt<16>(SA)) 480 error("Relocation R_PPC64_ADDR16_DS overflow"); 481 write16be(Loc, (read16be(Loc) & 3) | (SA & ~3)); 482 break; 483 case R_PPC64_ADDR16_LO: 484 write16be(Loc, applyPPCLo(SA)); 485 break; 486 case R_PPC64_ADDR16_LO_DS: 487 write16be(Loc, (read16be(Loc) & 3) | (applyPPCLo(SA) & ~3)); 488 break; 489 case R_PPC64_ADDR16_HI: 490 write16be(Loc, applyPPCHi(SA)); 491 break; 492 case R_PPC64_ADDR16_HA: 493 write16be(Loc, applyPPCHa(SA)); 494 break; 495 case R_PPC64_ADDR16_HIGHER: 496 write16be(Loc, applyPPCHigher(SA)); 497 break; 498 case R_PPC64_ADDR16_HIGHERA: 499 write16be(Loc, applyPPCHighera(SA)); 500 break; 501 case R_PPC64_ADDR16_HIGHEST: 502 write16be(Loc, applyPPCHighest(SA)); 503 break; 504 case R_PPC64_ADDR16_HIGHESTA: 505 write16be(Loc, applyPPCHighesta(SA)); 506 break; 507 case R_PPC64_ADDR14: { 508 if ((SA & 3) != 0) 509 error("Improper alignment for relocation R_PPC64_ADDR14"); 510 511 // Preserve the AA/LK bits in the branch instruction 512 uint8_t AALK = Loc[3]; 513 write16be(Loc + 2, (AALK & 3) | (SA & 0xfffc)); 514 break; 515 } 516 case R_PPC64_REL16_LO: 517 write16be(Loc, applyPPCLo(SA - P)); 518 break; 519 case R_PPC64_REL16_HI: 520 write16be(Loc, applyPPCHi(SA - P)); 521 break; 522 case R_PPC64_REL16_HA: 523 write16be(Loc, applyPPCHa(SA - P)); 524 break; 525 case R_PPC64_ADDR32: 526 if (!isInt<32>(SA)) 527 error("Relocation R_PPC64_ADDR32 overflow"); 528 write32be(Loc, SA); 529 break; 530 case R_PPC64_REL24: { 531 // If we have an undefined weak symbol, we might get here with a symbol 532 // address of zero. That could overflow, but the code must be unreachable, 533 // so don't bother doing anything at all. 534 if (!SA) 535 break; 536 537 uint64_t PltStart = Out<ELF64BE>::Plt->getVA(); 538 uint64_t PltEnd = PltStart + Out<ELF64BE>::Plt->getSize(); 539 bool InPlt = PltStart <= SA && SA < PltEnd; 540 541 if (!InPlt && Out<ELF64BE>::Opd) { 542 // If this is a local call, and we currently have the address of a 543 // function-descriptor, get the underlying code address instead. 544 uint64_t OpdStart = Out<ELF64BE>::Opd->getVA(); 545 uint64_t OpdEnd = OpdStart + Out<ELF64BE>::Opd->getSize(); 546 bool InOpd = OpdStart <= SA && SA < OpdEnd; 547 548 if (InOpd) 549 SA = read64be(&Out<ELF64BE>::OpdBuf[SA - OpdStart]); 550 } 551 552 uint32_t Mask = 0x03FFFFFC; 553 if (!isInt<24>(SA - P)) 554 error("Relocation R_PPC64_REL24 overflow"); 555 write32be(Loc, (read32be(Loc) & ~Mask) | ((SA - P) & Mask)); 556 557 uint32_t Nop = 0x60000000; 558 if (InPlt && Loc + 8 <= BufEnd && read32be(Loc + 4) == Nop) 559 write32be(Loc + 4, 0xe8410028); // ld %r2, 40(%r1) 560 break; 561 } 562 case R_PPC64_REL32: 563 if (!isInt<32>(SA - P)) 564 error("Relocation R_PPC64_REL32 overflow"); 565 write32be(Loc, SA - P); 566 break; 567 case R_PPC64_REL64: 568 write64be(Loc, SA - P); 569 break; 570 case R_PPC64_ADDR64: 571 case R_PPC64_TOC: 572 write64be(Loc, SA); 573 break; 574 default: 575 error("unrecognized reloc " + Twine(Type)); 576 } 577 } 578 579 AArch64TargetInfo::AArch64TargetInfo() {} 580 581 void AArch64TargetInfo::writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const {} 582 void AArch64TargetInfo::writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr, 583 uint64_t PltEntryAddr) const {} 584 void AArch64TargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr, 585 uint64_t PltEntryAddr, int32_t Index) const {} 586 bool AArch64TargetInfo::relocNeedsGot(uint32_t Type, 587 const SymbolBody &S) const { 588 return false; 589 } 590 bool AArch64TargetInfo::relocNeedsPlt(uint32_t Type, 591 const SymbolBody &S) const { 592 return false; 593 } 594 595 static void updateAArch64Adr(uint8_t *L, uint64_t Imm) { 596 uint32_t ImmLo = (Imm & 0x3) << 29; 597 uint32_t ImmHi = ((Imm & 0x1FFFFC) >> 2) << 5; 598 uint64_t Mask = (0x3 << 29) | (0x7FFFF << 5); 599 write32le(L, (read32le(L) & ~Mask) | ImmLo | ImmHi); 600 } 601 602 // Page(Expr) is the page address of the expression Expr, defined 603 // as (Expr & ~0xFFF). (This applies even if the machine page size 604 // supported by the platform has a different value.) 605 static uint64_t getAArch64Page(uint64_t Expr) { 606 return Expr & (~static_cast<uint64_t>(0xFFF)); 607 } 608 609 void AArch64TargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd, 610 uint32_t Type, uint64_t P, 611 uint64_t SA) const { 612 switch (Type) { 613 case R_AARCH64_ABS16: 614 if (!isInt<16>(SA)) 615 error("Relocation R_AARCH64_ABS16 out of range"); 616 write16le(Loc, SA); 617 break; 618 case R_AARCH64_ABS32: 619 if (!isInt<32>(SA)) 620 error("Relocation R_AARCH64_ABS32 out of range"); 621 write32le(Loc, SA); 622 break; 623 case R_AARCH64_ABS64: 624 // No overflow check needed. 625 write64le(Loc, SA); 626 break; 627 case R_AARCH64_ADD_ABS_LO12_NC: 628 // No overflow check needed. 629 // This relocation stores 12 bits and there's no instruction 630 // to do it. Instead, we do a 32 bits store of the value 631 // of r_addend bitwise-or'ed Loc. This assumes that the addend 632 // bits in Loc are zero. 633 or32le(Loc, (SA & 0xFFF) << 10); 634 break; 635 case R_AARCH64_ADR_PREL_LO21: { 636 uint64_t X = SA - P; 637 if (!isInt<21>(X)) 638 error("Relocation R_AARCH64_ADR_PREL_LO21 out of range"); 639 updateAArch64Adr(Loc, X & 0x1FFFFF); 640 break; 641 } 642 case R_AARCH64_ADR_PREL_PG_HI21: { 643 uint64_t X = getAArch64Page(SA) - getAArch64Page(P); 644 if (!isInt<33>(X)) 645 error("Relocation R_AARCH64_ADR_PREL_PG_HI21 out of range"); 646 updateAArch64Adr(Loc, (X >> 12) & 0x1FFFFF); // X[32:12] 647 break; 648 } 649 case R_AARCH64_PREL64: 650 // No overflow check needed. 651 write64le(Loc, SA - P); 652 break; 653 default: 654 error("unrecognized reloc " + Twine(Type)); 655 } 656 } 657 658 template <class ELFT> MipsTargetInfo<ELFT>::MipsTargetInfo() { 659 PageSize = 65536; 660 } 661 662 template <class ELFT> 663 void MipsTargetInfo<ELFT>::writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const {} 664 template <class ELFT> 665 void MipsTargetInfo<ELFT>::writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr, 666 uint64_t PltEntryAddr) const {} 667 template <class ELFT> 668 void MipsTargetInfo<ELFT>::writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr, 669 uint64_t PltEntryAddr, int32_t Index) const {} 670 671 template <class ELFT> 672 bool MipsTargetInfo<ELFT>::relocNeedsGot(uint32_t Type, 673 const SymbolBody &S) const { 674 return false; 675 } 676 677 template <class ELFT> 678 bool MipsTargetInfo<ELFT>::relocNeedsPlt(uint32_t Type, 679 const SymbolBody &S) const { 680 return false; 681 } 682 683 template <class ELFT> 684 void MipsTargetInfo<ELFT>::relocateOne(uint8_t *Loc, uint8_t *BufEnd, 685 uint32_t Type, uint64_t P, 686 uint64_t SA) const { 687 const bool IsLE = ELFT::TargetEndianness == support::little; 688 switch (Type) { 689 case R_MIPS_32: 690 add32<IsLE>(Loc, SA); 691 break; 692 default: 693 error("unrecognized reloc " + Twine(Type)); 694 } 695 } 696 } 697 } 698