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