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