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