1 //===-- RuntimeDyldELF.cpp - Run-time dynamic linker for MC-JIT -*- C++ -*-===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // Implementation of ELF support for the MC-JIT runtime dynamic linker. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #include "RuntimeDyldELF.h" 14 #include "RuntimeDyldCheckerImpl.h" 15 #include "Targets/RuntimeDyldELFMips.h" 16 #include "llvm/ADT/STLExtras.h" 17 #include "llvm/ADT/StringRef.h" 18 #include "llvm/BinaryFormat/ELF.h" 19 #include "llvm/Object/ELFObjectFile.h" 20 #include "llvm/Object/ObjectFile.h" 21 #include "llvm/Support/Endian.h" 22 #include "llvm/Support/MemoryBuffer.h" 23 #include "llvm/TargetParser/Triple.h" 24 25 using namespace llvm; 26 using namespace llvm::object; 27 using namespace llvm::support::endian; 28 29 #define DEBUG_TYPE "dyld" 30 31 static void or32le(void *P, int32_t V) { write32le(P, read32le(P) | V); } 32 33 static void or32AArch64Imm(void *L, uint64_t Imm) { 34 or32le(L, (Imm & 0xFFF) << 10); 35 } 36 37 template <class T> static void write(bool isBE, void *P, T V) { 38 isBE ? write<T, llvm::endianness::big>(P, V) 39 : write<T, llvm::endianness::little>(P, V); 40 } 41 42 static void write32AArch64Addr(void *L, uint64_t Imm) { 43 uint32_t ImmLo = (Imm & 0x3) << 29; 44 uint32_t ImmHi = (Imm & 0x1FFFFC) << 3; 45 uint64_t Mask = (0x3 << 29) | (0x1FFFFC << 3); 46 write32le(L, (read32le(L) & ~Mask) | ImmLo | ImmHi); 47 } 48 49 // Return the bits [Start, End] from Val shifted Start bits. 50 // For instance, getBits(0xF0, 4, 8) returns 0xF. 51 static uint64_t getBits(uint64_t Val, int Start, int End) { 52 uint64_t Mask = ((uint64_t)1 << (End + 1 - Start)) - 1; 53 return (Val >> Start) & Mask; 54 } 55 56 namespace { 57 58 template <class ELFT> class DyldELFObject : public ELFObjectFile<ELFT> { 59 LLVM_ELF_IMPORT_TYPES_ELFT(ELFT) 60 61 typedef typename ELFT::uint addr_type; 62 63 DyldELFObject(ELFObjectFile<ELFT> &&Obj); 64 65 public: 66 static Expected<std::unique_ptr<DyldELFObject>> 67 create(MemoryBufferRef Wrapper); 68 69 void updateSectionAddress(const SectionRef &Sec, uint64_t Addr); 70 71 void updateSymbolAddress(const SymbolRef &SymRef, uint64_t Addr); 72 73 // Methods for type inquiry through isa, cast and dyn_cast 74 static bool classof(const Binary *v) { 75 return (isa<ELFObjectFile<ELFT>>(v) && 76 classof(cast<ELFObjectFile<ELFT>>(v))); 77 } 78 static bool classof(const ELFObjectFile<ELFT> *v) { 79 return v->isDyldType(); 80 } 81 }; 82 83 84 85 // The MemoryBuffer passed into this constructor is just a wrapper around the 86 // actual memory. Ultimately, the Binary parent class will take ownership of 87 // this MemoryBuffer object but not the underlying memory. 88 template <class ELFT> 89 DyldELFObject<ELFT>::DyldELFObject(ELFObjectFile<ELFT> &&Obj) 90 : ELFObjectFile<ELFT>(std::move(Obj)) { 91 this->isDyldELFObject = true; 92 } 93 94 template <class ELFT> 95 Expected<std::unique_ptr<DyldELFObject<ELFT>>> 96 DyldELFObject<ELFT>::create(MemoryBufferRef Wrapper) { 97 auto Obj = ELFObjectFile<ELFT>::create(Wrapper); 98 if (auto E = Obj.takeError()) 99 return std::move(E); 100 std::unique_ptr<DyldELFObject<ELFT>> Ret( 101 new DyldELFObject<ELFT>(std::move(*Obj))); 102 return std::move(Ret); 103 } 104 105 template <class ELFT> 106 void DyldELFObject<ELFT>::updateSectionAddress(const SectionRef &Sec, 107 uint64_t Addr) { 108 DataRefImpl ShdrRef = Sec.getRawDataRefImpl(); 109 Elf_Shdr *shdr = 110 const_cast<Elf_Shdr *>(reinterpret_cast<const Elf_Shdr *>(ShdrRef.p)); 111 112 // This assumes the address passed in matches the target address bitness 113 // The template-based type cast handles everything else. 114 shdr->sh_addr = static_cast<addr_type>(Addr); 115 } 116 117 template <class ELFT> 118 void DyldELFObject<ELFT>::updateSymbolAddress(const SymbolRef &SymRef, 119 uint64_t Addr) { 120 121 Elf_Sym *sym = const_cast<Elf_Sym *>( 122 ELFObjectFile<ELFT>::getSymbol(SymRef.getRawDataRefImpl())); 123 124 // This assumes the address passed in matches the target address bitness 125 // The template-based type cast handles everything else. 126 sym->st_value = static_cast<addr_type>(Addr); 127 } 128 129 class LoadedELFObjectInfo final 130 : public LoadedObjectInfoHelper<LoadedELFObjectInfo, 131 RuntimeDyld::LoadedObjectInfo> { 132 public: 133 LoadedELFObjectInfo(RuntimeDyldImpl &RTDyld, ObjSectionToIDMap ObjSecToIDMap) 134 : LoadedObjectInfoHelper(RTDyld, std::move(ObjSecToIDMap)) {} 135 136 OwningBinary<ObjectFile> 137 getObjectForDebug(const ObjectFile &Obj) const override; 138 }; 139 140 template <typename ELFT> 141 static Expected<std::unique_ptr<DyldELFObject<ELFT>>> 142 createRTDyldELFObject(MemoryBufferRef Buffer, const ObjectFile &SourceObject, 143 const LoadedELFObjectInfo &L) { 144 typedef typename ELFT::Shdr Elf_Shdr; 145 typedef typename ELFT::uint addr_type; 146 147 Expected<std::unique_ptr<DyldELFObject<ELFT>>> ObjOrErr = 148 DyldELFObject<ELFT>::create(Buffer); 149 if (Error E = ObjOrErr.takeError()) 150 return std::move(E); 151 152 std::unique_ptr<DyldELFObject<ELFT>> Obj = std::move(*ObjOrErr); 153 154 // Iterate over all sections in the object. 155 auto SI = SourceObject.section_begin(); 156 for (const auto &Sec : Obj->sections()) { 157 Expected<StringRef> NameOrErr = Sec.getName(); 158 if (!NameOrErr) { 159 consumeError(NameOrErr.takeError()); 160 continue; 161 } 162 163 if (*NameOrErr != "") { 164 DataRefImpl ShdrRef = Sec.getRawDataRefImpl(); 165 Elf_Shdr *shdr = const_cast<Elf_Shdr *>( 166 reinterpret_cast<const Elf_Shdr *>(ShdrRef.p)); 167 168 if (uint64_t SecLoadAddr = L.getSectionLoadAddress(*SI)) { 169 // This assumes that the address passed in matches the target address 170 // bitness. The template-based type cast handles everything else. 171 shdr->sh_addr = static_cast<addr_type>(SecLoadAddr); 172 } 173 } 174 ++SI; 175 } 176 177 return std::move(Obj); 178 } 179 180 static OwningBinary<ObjectFile> 181 createELFDebugObject(const ObjectFile &Obj, const LoadedELFObjectInfo &L) { 182 assert(Obj.isELF() && "Not an ELF object file."); 183 184 std::unique_ptr<MemoryBuffer> Buffer = 185 MemoryBuffer::getMemBufferCopy(Obj.getData(), Obj.getFileName()); 186 187 Expected<std::unique_ptr<ObjectFile>> DebugObj(nullptr); 188 handleAllErrors(DebugObj.takeError()); 189 if (Obj.getBytesInAddress() == 4 && Obj.isLittleEndian()) 190 DebugObj = 191 createRTDyldELFObject<ELF32LE>(Buffer->getMemBufferRef(), Obj, L); 192 else if (Obj.getBytesInAddress() == 4 && !Obj.isLittleEndian()) 193 DebugObj = 194 createRTDyldELFObject<ELF32BE>(Buffer->getMemBufferRef(), Obj, L); 195 else if (Obj.getBytesInAddress() == 8 && !Obj.isLittleEndian()) 196 DebugObj = 197 createRTDyldELFObject<ELF64BE>(Buffer->getMemBufferRef(), Obj, L); 198 else if (Obj.getBytesInAddress() == 8 && Obj.isLittleEndian()) 199 DebugObj = 200 createRTDyldELFObject<ELF64LE>(Buffer->getMemBufferRef(), Obj, L); 201 else 202 llvm_unreachable("Unexpected ELF format"); 203 204 handleAllErrors(DebugObj.takeError()); 205 return OwningBinary<ObjectFile>(std::move(*DebugObj), std::move(Buffer)); 206 } 207 208 OwningBinary<ObjectFile> 209 LoadedELFObjectInfo::getObjectForDebug(const ObjectFile &Obj) const { 210 return createELFDebugObject(Obj, *this); 211 } 212 213 } // anonymous namespace 214 215 namespace llvm { 216 217 RuntimeDyldELF::RuntimeDyldELF(RuntimeDyld::MemoryManager &MemMgr, 218 JITSymbolResolver &Resolver) 219 : RuntimeDyldImpl(MemMgr, Resolver), GOTSectionID(0), CurrentGOTIndex(0) {} 220 RuntimeDyldELF::~RuntimeDyldELF() = default; 221 222 void RuntimeDyldELF::registerEHFrames() { 223 for (int i = 0, e = UnregisteredEHFrameSections.size(); i != e; ++i) { 224 SID EHFrameSID = UnregisteredEHFrameSections[i]; 225 uint8_t *EHFrameAddr = Sections[EHFrameSID].getAddress(); 226 uint64_t EHFrameLoadAddr = Sections[EHFrameSID].getLoadAddress(); 227 size_t EHFrameSize = Sections[EHFrameSID].getSize(); 228 MemMgr.registerEHFrames(EHFrameAddr, EHFrameLoadAddr, EHFrameSize); 229 } 230 UnregisteredEHFrameSections.clear(); 231 } 232 233 std::unique_ptr<RuntimeDyldELF> 234 llvm::RuntimeDyldELF::create(Triple::ArchType Arch, 235 RuntimeDyld::MemoryManager &MemMgr, 236 JITSymbolResolver &Resolver) { 237 switch (Arch) { 238 default: 239 return std::make_unique<RuntimeDyldELF>(MemMgr, Resolver); 240 case Triple::mips: 241 case Triple::mipsel: 242 case Triple::mips64: 243 case Triple::mips64el: 244 return std::make_unique<RuntimeDyldELFMips>(MemMgr, Resolver); 245 } 246 } 247 248 std::unique_ptr<RuntimeDyld::LoadedObjectInfo> 249 RuntimeDyldELF::loadObject(const object::ObjectFile &O) { 250 if (auto ObjSectionToIDOrErr = loadObjectImpl(O)) 251 return std::make_unique<LoadedELFObjectInfo>(*this, *ObjSectionToIDOrErr); 252 else { 253 HasError = true; 254 raw_string_ostream ErrStream(ErrorStr); 255 logAllUnhandledErrors(ObjSectionToIDOrErr.takeError(), ErrStream); 256 return nullptr; 257 } 258 } 259 260 void RuntimeDyldELF::resolveX86_64Relocation(const SectionEntry &Section, 261 uint64_t Offset, uint64_t Value, 262 uint32_t Type, int64_t Addend, 263 uint64_t SymOffset) { 264 switch (Type) { 265 default: 266 report_fatal_error("Relocation type not implemented yet!"); 267 break; 268 case ELF::R_X86_64_NONE: 269 break; 270 case ELF::R_X86_64_8: { 271 Value += Addend; 272 assert((int64_t)Value <= INT8_MAX && (int64_t)Value >= INT8_MIN); 273 uint8_t TruncatedAddr = (Value & 0xFF); 274 *Section.getAddressWithOffset(Offset) = TruncatedAddr; 275 LLVM_DEBUG(dbgs() << "Writing " << format("%p", TruncatedAddr) << " at " 276 << format("%p\n", Section.getAddressWithOffset(Offset))); 277 break; 278 } 279 case ELF::R_X86_64_16: { 280 Value += Addend; 281 assert((int64_t)Value <= INT16_MAX && (int64_t)Value >= INT16_MIN); 282 uint16_t TruncatedAddr = (Value & 0xFFFF); 283 support::ulittle16_t::ref(Section.getAddressWithOffset(Offset)) = 284 TruncatedAddr; 285 LLVM_DEBUG(dbgs() << "Writing " << format("%p", TruncatedAddr) << " at " 286 << format("%p\n", Section.getAddressWithOffset(Offset))); 287 break; 288 } 289 case ELF::R_X86_64_64: { 290 support::ulittle64_t::ref(Section.getAddressWithOffset(Offset)) = 291 Value + Addend; 292 LLVM_DEBUG(dbgs() << "Writing " << format("%p", (Value + Addend)) << " at " 293 << format("%p\n", Section.getAddressWithOffset(Offset))); 294 break; 295 } 296 case ELF::R_X86_64_32: 297 case ELF::R_X86_64_32S: { 298 Value += Addend; 299 assert((Type == ELF::R_X86_64_32 && (Value <= UINT32_MAX)) || 300 (Type == ELF::R_X86_64_32S && 301 ((int64_t)Value <= INT32_MAX && (int64_t)Value >= INT32_MIN))); 302 uint32_t TruncatedAddr = (Value & 0xFFFFFFFF); 303 support::ulittle32_t::ref(Section.getAddressWithOffset(Offset)) = 304 TruncatedAddr; 305 LLVM_DEBUG(dbgs() << "Writing " << format("%p", TruncatedAddr) << " at " 306 << format("%p\n", Section.getAddressWithOffset(Offset))); 307 break; 308 } 309 case ELF::R_X86_64_PC8: { 310 uint64_t FinalAddress = Section.getLoadAddressWithOffset(Offset); 311 int64_t RealOffset = Value + Addend - FinalAddress; 312 assert(isInt<8>(RealOffset)); 313 int8_t TruncOffset = (RealOffset & 0xFF); 314 Section.getAddress()[Offset] = TruncOffset; 315 break; 316 } 317 case ELF::R_X86_64_PC32: { 318 uint64_t FinalAddress = Section.getLoadAddressWithOffset(Offset); 319 int64_t RealOffset = Value + Addend - FinalAddress; 320 assert(isInt<32>(RealOffset)); 321 int32_t TruncOffset = (RealOffset & 0xFFFFFFFF); 322 support::ulittle32_t::ref(Section.getAddressWithOffset(Offset)) = 323 TruncOffset; 324 break; 325 } 326 case ELF::R_X86_64_PC64: { 327 uint64_t FinalAddress = Section.getLoadAddressWithOffset(Offset); 328 int64_t RealOffset = Value + Addend - FinalAddress; 329 support::ulittle64_t::ref(Section.getAddressWithOffset(Offset)) = 330 RealOffset; 331 LLVM_DEBUG(dbgs() << "Writing " << format("%p", RealOffset) << " at " 332 << format("%p\n", FinalAddress)); 333 break; 334 } 335 case ELF::R_X86_64_GOTOFF64: { 336 // Compute Value - GOTBase. 337 uint64_t GOTBase = 0; 338 for (const auto &Section : Sections) { 339 if (Section.getName() == ".got") { 340 GOTBase = Section.getLoadAddressWithOffset(0); 341 break; 342 } 343 } 344 assert(GOTBase != 0 && "missing GOT"); 345 int64_t GOTOffset = Value - GOTBase + Addend; 346 support::ulittle64_t::ref(Section.getAddressWithOffset(Offset)) = GOTOffset; 347 break; 348 } 349 case ELF::R_X86_64_DTPMOD64: { 350 // We only have one DSO, so the module id is always 1. 351 support::ulittle64_t::ref(Section.getAddressWithOffset(Offset)) = 1; 352 break; 353 } 354 case ELF::R_X86_64_DTPOFF64: 355 case ELF::R_X86_64_TPOFF64: { 356 // DTPOFF64 should resolve to the offset in the TLS block, TPOFF64 to the 357 // offset in the *initial* TLS block. Since we are statically linking, all 358 // TLS blocks already exist in the initial block, so resolve both 359 // relocations equally. 360 support::ulittle64_t::ref(Section.getAddressWithOffset(Offset)) = 361 Value + Addend; 362 break; 363 } 364 case ELF::R_X86_64_DTPOFF32: 365 case ELF::R_X86_64_TPOFF32: { 366 // As for the (D)TPOFF64 relocations above, both DTPOFF32 and TPOFF32 can 367 // be resolved equally. 368 int64_t RealValue = Value + Addend; 369 assert(RealValue >= INT32_MIN && RealValue <= INT32_MAX); 370 int32_t TruncValue = RealValue; 371 support::ulittle32_t::ref(Section.getAddressWithOffset(Offset)) = 372 TruncValue; 373 break; 374 } 375 } 376 } 377 378 void RuntimeDyldELF::resolveX86Relocation(const SectionEntry &Section, 379 uint64_t Offset, uint32_t Value, 380 uint32_t Type, int32_t Addend) { 381 switch (Type) { 382 case ELF::R_386_32: { 383 support::ulittle32_t::ref(Section.getAddressWithOffset(Offset)) = 384 Value + Addend; 385 break; 386 } 387 // Handle R_386_PLT32 like R_386_PC32 since it should be able to 388 // reach any 32 bit address. 389 case ELF::R_386_PLT32: 390 case ELF::R_386_PC32: { 391 uint32_t FinalAddress = 392 Section.getLoadAddressWithOffset(Offset) & 0xFFFFFFFF; 393 uint32_t RealOffset = Value + Addend - FinalAddress; 394 support::ulittle32_t::ref(Section.getAddressWithOffset(Offset)) = 395 RealOffset; 396 break; 397 } 398 default: 399 // There are other relocation types, but it appears these are the 400 // only ones currently used by the LLVM ELF object writer 401 report_fatal_error("Relocation type not implemented yet!"); 402 break; 403 } 404 } 405 406 void RuntimeDyldELF::resolveAArch64Relocation(const SectionEntry &Section, 407 uint64_t Offset, uint64_t Value, 408 uint32_t Type, int64_t Addend) { 409 uint32_t *TargetPtr = 410 reinterpret_cast<uint32_t *>(Section.getAddressWithOffset(Offset)); 411 uint64_t FinalAddress = Section.getLoadAddressWithOffset(Offset); 412 // Data should use target endian. Code should always use little endian. 413 bool isBE = Arch == Triple::aarch64_be; 414 415 LLVM_DEBUG(dbgs() << "resolveAArch64Relocation, LocalAddress: 0x" 416 << format("%llx", Section.getAddressWithOffset(Offset)) 417 << " FinalAddress: 0x" << format("%llx", FinalAddress) 418 << " Value: 0x" << format("%llx", Value) << " Type: 0x" 419 << format("%x", Type) << " Addend: 0x" 420 << format("%llx", Addend) << "\n"); 421 422 switch (Type) { 423 default: 424 report_fatal_error("Relocation type not implemented yet!"); 425 break; 426 case ELF::R_AARCH64_NONE: 427 break; 428 case ELF::R_AARCH64_ABS16: { 429 uint64_t Result = Value + Addend; 430 assert(Result == static_cast<uint64_t>(llvm::SignExtend64(Result, 16)) || 431 (Result >> 16) == 0); 432 write(isBE, TargetPtr, static_cast<uint16_t>(Result & 0xffffU)); 433 break; 434 } 435 case ELF::R_AARCH64_ABS32: { 436 uint64_t Result = Value + Addend; 437 assert(Result == static_cast<uint64_t>(llvm::SignExtend64(Result, 32)) || 438 (Result >> 32) == 0); 439 write(isBE, TargetPtr, static_cast<uint32_t>(Result & 0xffffffffU)); 440 break; 441 } 442 case ELF::R_AARCH64_ABS64: 443 write(isBE, TargetPtr, Value + Addend); 444 break; 445 case ELF::R_AARCH64_PLT32: { 446 uint64_t Result = Value + Addend - FinalAddress; 447 assert(static_cast<int64_t>(Result) >= INT32_MIN && 448 static_cast<int64_t>(Result) <= INT32_MAX); 449 write(isBE, TargetPtr, static_cast<uint32_t>(Result)); 450 break; 451 } 452 case ELF::R_AARCH64_PREL16: { 453 uint64_t Result = Value + Addend - FinalAddress; 454 assert(static_cast<int64_t>(Result) >= INT16_MIN && 455 static_cast<int64_t>(Result) <= UINT16_MAX); 456 write(isBE, TargetPtr, static_cast<uint16_t>(Result & 0xffffU)); 457 break; 458 } 459 case ELF::R_AARCH64_PREL32: { 460 uint64_t Result = Value + Addend - FinalAddress; 461 assert(static_cast<int64_t>(Result) >= INT32_MIN && 462 static_cast<int64_t>(Result) <= UINT32_MAX); 463 write(isBE, TargetPtr, static_cast<uint32_t>(Result & 0xffffffffU)); 464 break; 465 } 466 case ELF::R_AARCH64_PREL64: 467 write(isBE, TargetPtr, Value + Addend - FinalAddress); 468 break; 469 case ELF::R_AARCH64_CONDBR19: { 470 uint64_t BranchImm = Value + Addend - FinalAddress; 471 472 assert(isInt<21>(BranchImm)); 473 *TargetPtr &= 0xff00001fU; 474 // Immediate:20:2 goes in bits 23:5 of Bcc, CBZ, CBNZ 475 or32le(TargetPtr, (BranchImm & 0x001FFFFC) << 3); 476 break; 477 } 478 case ELF::R_AARCH64_TSTBR14: { 479 uint64_t BranchImm = Value + Addend - FinalAddress; 480 481 assert(isInt<16>(BranchImm)); 482 483 uint32_t RawInstr = *(support::little32_t *)TargetPtr; 484 *(support::little32_t *)TargetPtr = RawInstr & 0xfff8001fU; 485 486 // Immediate:15:2 goes in bits 18:5 of TBZ, TBNZ 487 or32le(TargetPtr, (BranchImm & 0x0000FFFC) << 3); 488 break; 489 } 490 case ELF::R_AARCH64_CALL26: // fallthrough 491 case ELF::R_AARCH64_JUMP26: { 492 // Operation: S+A-P. Set Call or B immediate value to bits fff_fffc of the 493 // calculation. 494 uint64_t BranchImm = Value + Addend - FinalAddress; 495 496 // "Check that -2^27 <= result < 2^27". 497 assert(isInt<28>(BranchImm)); 498 or32le(TargetPtr, (BranchImm & 0x0FFFFFFC) >> 2); 499 break; 500 } 501 case ELF::R_AARCH64_MOVW_UABS_G3: 502 or32le(TargetPtr, ((Value + Addend) & 0xFFFF000000000000) >> 43); 503 break; 504 case ELF::R_AARCH64_MOVW_UABS_G2_NC: 505 or32le(TargetPtr, ((Value + Addend) & 0xFFFF00000000) >> 27); 506 break; 507 case ELF::R_AARCH64_MOVW_UABS_G1_NC: 508 or32le(TargetPtr, ((Value + Addend) & 0xFFFF0000) >> 11); 509 break; 510 case ELF::R_AARCH64_MOVW_UABS_G0_NC: 511 or32le(TargetPtr, ((Value + Addend) & 0xFFFF) << 5); 512 break; 513 case ELF::R_AARCH64_ADR_PREL_PG_HI21: { 514 // Operation: Page(S+A) - Page(P) 515 uint64_t Result = 516 ((Value + Addend) & ~0xfffULL) - (FinalAddress & ~0xfffULL); 517 518 // Check that -2^32 <= X < 2^32 519 assert(isInt<33>(Result) && "overflow check failed for relocation"); 520 521 // Immediate goes in bits 30:29 + 5:23 of ADRP instruction, taken 522 // from bits 32:12 of X. 523 write32AArch64Addr(TargetPtr, Result >> 12); 524 break; 525 } 526 case ELF::R_AARCH64_ADD_ABS_LO12_NC: 527 // Operation: S + A 528 // Immediate goes in bits 21:10 of LD/ST instruction, taken 529 // from bits 11:0 of X 530 or32AArch64Imm(TargetPtr, Value + Addend); 531 break; 532 case ELF::R_AARCH64_LDST8_ABS_LO12_NC: 533 // Operation: S + A 534 // Immediate goes in bits 21:10 of LD/ST instruction, taken 535 // from bits 11:0 of X 536 or32AArch64Imm(TargetPtr, getBits(Value + Addend, 0, 11)); 537 break; 538 case ELF::R_AARCH64_LDST16_ABS_LO12_NC: 539 // Operation: S + A 540 // Immediate goes in bits 21:10 of LD/ST instruction, taken 541 // from bits 11:1 of X 542 or32AArch64Imm(TargetPtr, getBits(Value + Addend, 1, 11)); 543 break; 544 case ELF::R_AARCH64_LDST32_ABS_LO12_NC: 545 // Operation: S + A 546 // Immediate goes in bits 21:10 of LD/ST instruction, taken 547 // from bits 11:2 of X 548 or32AArch64Imm(TargetPtr, getBits(Value + Addend, 2, 11)); 549 break; 550 case ELF::R_AARCH64_LDST64_ABS_LO12_NC: 551 // Operation: S + A 552 // Immediate goes in bits 21:10 of LD/ST instruction, taken 553 // from bits 11:3 of X 554 or32AArch64Imm(TargetPtr, getBits(Value + Addend, 3, 11)); 555 break; 556 case ELF::R_AARCH64_LDST128_ABS_LO12_NC: 557 // Operation: S + A 558 // Immediate goes in bits 21:10 of LD/ST instruction, taken 559 // from bits 11:4 of X 560 or32AArch64Imm(TargetPtr, getBits(Value + Addend, 4, 11)); 561 break; 562 case ELF::R_AARCH64_LD_PREL_LO19: { 563 // Operation: S + A - P 564 uint64_t Result = Value + Addend - FinalAddress; 565 566 // "Check that -2^20 <= result < 2^20". 567 assert(isInt<21>(Result)); 568 569 *TargetPtr &= 0xff00001fU; 570 // Immediate goes in bits 23:5 of LD imm instruction, taken 571 // from bits 20:2 of X 572 *TargetPtr |= ((Result & 0xffc) << (5 - 2)); 573 break; 574 } 575 case ELF::R_AARCH64_ADR_PREL_LO21: { 576 // Operation: S + A - P 577 uint64_t Result = Value + Addend - FinalAddress; 578 579 // "Check that -2^20 <= result < 2^20". 580 assert(isInt<21>(Result)); 581 582 *TargetPtr &= 0x9f00001fU; 583 // Immediate goes in bits 23:5, 30:29 of ADR imm instruction, taken 584 // from bits 20:0 of X 585 *TargetPtr |= ((Result & 0xffc) << (5 - 2)); 586 *TargetPtr |= (Result & 0x3) << 29; 587 break; 588 } 589 } 590 } 591 592 void RuntimeDyldELF::resolveARMRelocation(const SectionEntry &Section, 593 uint64_t Offset, uint32_t Value, 594 uint32_t Type, int32_t Addend) { 595 // TODO: Add Thumb relocations. 596 uint32_t *TargetPtr = 597 reinterpret_cast<uint32_t *>(Section.getAddressWithOffset(Offset)); 598 uint32_t FinalAddress = Section.getLoadAddressWithOffset(Offset) & 0xFFFFFFFF; 599 Value += Addend; 600 601 LLVM_DEBUG(dbgs() << "resolveARMRelocation, LocalAddress: " 602 << Section.getAddressWithOffset(Offset) 603 << " FinalAddress: " << format("%p", FinalAddress) 604 << " Value: " << format("%x", Value) 605 << " Type: " << format("%x", Type) 606 << " Addend: " << format("%x", Addend) << "\n"); 607 608 switch (Type) { 609 default: 610 llvm_unreachable("Not implemented relocation type!"); 611 612 case ELF::R_ARM_NONE: 613 break; 614 // Write a 31bit signed offset 615 case ELF::R_ARM_PREL31: 616 support::ulittle32_t::ref{TargetPtr} = 617 (support::ulittle32_t::ref{TargetPtr} & 0x80000000) | 618 ((Value - FinalAddress) & ~0x80000000); 619 break; 620 case ELF::R_ARM_TARGET1: 621 case ELF::R_ARM_ABS32: 622 support::ulittle32_t::ref{TargetPtr} = Value; 623 break; 624 // Write first 16 bit of 32 bit value to the mov instruction. 625 // Last 4 bit should be shifted. 626 case ELF::R_ARM_MOVW_ABS_NC: 627 case ELF::R_ARM_MOVT_ABS: 628 if (Type == ELF::R_ARM_MOVW_ABS_NC) 629 Value = Value & 0xFFFF; 630 else if (Type == ELF::R_ARM_MOVT_ABS) 631 Value = (Value >> 16) & 0xFFFF; 632 support::ulittle32_t::ref{TargetPtr} = 633 (support::ulittle32_t::ref{TargetPtr} & ~0x000F0FFF) | (Value & 0xFFF) | 634 (((Value >> 12) & 0xF) << 16); 635 break; 636 // Write 24 bit relative value to the branch instruction. 637 case ELF::R_ARM_PC24: // Fall through. 638 case ELF::R_ARM_CALL: // Fall through. 639 case ELF::R_ARM_JUMP24: 640 int32_t RelValue = static_cast<int32_t>(Value - FinalAddress - 8); 641 RelValue = (RelValue & 0x03FFFFFC) >> 2; 642 assert((support::ulittle32_t::ref{TargetPtr} & 0xFFFFFF) == 0xFFFFFE); 643 support::ulittle32_t::ref{TargetPtr} = 644 (support::ulittle32_t::ref{TargetPtr} & 0xFF000000) | RelValue; 645 break; 646 } 647 } 648 649 void RuntimeDyldELF::setMipsABI(const ObjectFile &Obj) { 650 if (Arch == Triple::UnknownArch || 651 !StringRef(Triple::getArchTypePrefix(Arch)).equals("mips")) { 652 IsMipsO32ABI = false; 653 IsMipsN32ABI = false; 654 IsMipsN64ABI = false; 655 return; 656 } 657 if (auto *E = dyn_cast<ELFObjectFileBase>(&Obj)) { 658 unsigned AbiVariant = E->getPlatformFlags(); 659 IsMipsO32ABI = AbiVariant & ELF::EF_MIPS_ABI_O32; 660 IsMipsN32ABI = AbiVariant & ELF::EF_MIPS_ABI2; 661 } 662 IsMipsN64ABI = Obj.getFileFormatName().equals("elf64-mips"); 663 } 664 665 // Return the .TOC. section and offset. 666 Error RuntimeDyldELF::findPPC64TOCSection(const ELFObjectFileBase &Obj, 667 ObjSectionToIDMap &LocalSections, 668 RelocationValueRef &Rel) { 669 // Set a default SectionID in case we do not find a TOC section below. 670 // This may happen for references to TOC base base (sym@toc, .odp 671 // relocation) without a .toc directive. In this case just use the 672 // first section (which is usually the .odp) since the code won't 673 // reference the .toc base directly. 674 Rel.SymbolName = nullptr; 675 Rel.SectionID = 0; 676 677 // The TOC consists of sections .got, .toc, .tocbss, .plt in that 678 // order. The TOC starts where the first of these sections starts. 679 for (auto &Section : Obj.sections()) { 680 Expected<StringRef> NameOrErr = Section.getName(); 681 if (!NameOrErr) 682 return NameOrErr.takeError(); 683 StringRef SectionName = *NameOrErr; 684 685 if (SectionName == ".got" 686 || SectionName == ".toc" 687 || SectionName == ".tocbss" 688 || SectionName == ".plt") { 689 if (auto SectionIDOrErr = 690 findOrEmitSection(Obj, Section, false, LocalSections)) 691 Rel.SectionID = *SectionIDOrErr; 692 else 693 return SectionIDOrErr.takeError(); 694 break; 695 } 696 } 697 698 // Per the ppc64-elf-linux ABI, The TOC base is TOC value plus 0x8000 699 // thus permitting a full 64 Kbytes segment. 700 Rel.Addend = 0x8000; 701 702 return Error::success(); 703 } 704 705 // Returns the sections and offset associated with the ODP entry referenced 706 // by Symbol. 707 Error RuntimeDyldELF::findOPDEntrySection(const ELFObjectFileBase &Obj, 708 ObjSectionToIDMap &LocalSections, 709 RelocationValueRef &Rel) { 710 // Get the ELF symbol value (st_value) to compare with Relocation offset in 711 // .opd entries 712 for (section_iterator si = Obj.section_begin(), se = Obj.section_end(); 713 si != se; ++si) { 714 715 Expected<section_iterator> RelSecOrErr = si->getRelocatedSection(); 716 if (!RelSecOrErr) 717 report_fatal_error(Twine(toString(RelSecOrErr.takeError()))); 718 719 section_iterator RelSecI = *RelSecOrErr; 720 if (RelSecI == Obj.section_end()) 721 continue; 722 723 Expected<StringRef> NameOrErr = RelSecI->getName(); 724 if (!NameOrErr) 725 return NameOrErr.takeError(); 726 StringRef RelSectionName = *NameOrErr; 727 728 if (RelSectionName != ".opd") 729 continue; 730 731 for (elf_relocation_iterator i = si->relocation_begin(), 732 e = si->relocation_end(); 733 i != e;) { 734 // The R_PPC64_ADDR64 relocation indicates the first field 735 // of a .opd entry 736 uint64_t TypeFunc = i->getType(); 737 if (TypeFunc != ELF::R_PPC64_ADDR64) { 738 ++i; 739 continue; 740 } 741 742 uint64_t TargetSymbolOffset = i->getOffset(); 743 symbol_iterator TargetSymbol = i->getSymbol(); 744 int64_t Addend; 745 if (auto AddendOrErr = i->getAddend()) 746 Addend = *AddendOrErr; 747 else 748 return AddendOrErr.takeError(); 749 750 ++i; 751 if (i == e) 752 break; 753 754 // Just check if following relocation is a R_PPC64_TOC 755 uint64_t TypeTOC = i->getType(); 756 if (TypeTOC != ELF::R_PPC64_TOC) 757 continue; 758 759 // Finally compares the Symbol value and the target symbol offset 760 // to check if this .opd entry refers to the symbol the relocation 761 // points to. 762 if (Rel.Addend != (int64_t)TargetSymbolOffset) 763 continue; 764 765 section_iterator TSI = Obj.section_end(); 766 if (auto TSIOrErr = TargetSymbol->getSection()) 767 TSI = *TSIOrErr; 768 else 769 return TSIOrErr.takeError(); 770 assert(TSI != Obj.section_end() && "TSI should refer to a valid section"); 771 772 bool IsCode = TSI->isText(); 773 if (auto SectionIDOrErr = findOrEmitSection(Obj, *TSI, IsCode, 774 LocalSections)) 775 Rel.SectionID = *SectionIDOrErr; 776 else 777 return SectionIDOrErr.takeError(); 778 Rel.Addend = (intptr_t)Addend; 779 return Error::success(); 780 } 781 } 782 llvm_unreachable("Attempting to get address of ODP entry!"); 783 } 784 785 // Relocation masks following the #lo(value), #hi(value), #ha(value), 786 // #higher(value), #highera(value), #highest(value), and #highesta(value) 787 // macros defined in section 4.5.1. Relocation Types of the PPC-elf64abi 788 // document. 789 790 static inline uint16_t applyPPClo(uint64_t value) { return value & 0xffff; } 791 792 static inline uint16_t applyPPChi(uint64_t value) { 793 return (value >> 16) & 0xffff; 794 } 795 796 static inline uint16_t applyPPCha (uint64_t value) { 797 return ((value + 0x8000) >> 16) & 0xffff; 798 } 799 800 static inline uint16_t applyPPChigher(uint64_t value) { 801 return (value >> 32) & 0xffff; 802 } 803 804 static inline uint16_t applyPPChighera (uint64_t value) { 805 return ((value + 0x8000) >> 32) & 0xffff; 806 } 807 808 static inline uint16_t applyPPChighest(uint64_t value) { 809 return (value >> 48) & 0xffff; 810 } 811 812 static inline uint16_t applyPPChighesta (uint64_t value) { 813 return ((value + 0x8000) >> 48) & 0xffff; 814 } 815 816 void RuntimeDyldELF::resolvePPC32Relocation(const SectionEntry &Section, 817 uint64_t Offset, uint64_t Value, 818 uint32_t Type, int64_t Addend) { 819 uint8_t *LocalAddress = Section.getAddressWithOffset(Offset); 820 switch (Type) { 821 default: 822 report_fatal_error("Relocation type not implemented yet!"); 823 break; 824 case ELF::R_PPC_ADDR16_LO: 825 writeInt16BE(LocalAddress, applyPPClo(Value + Addend)); 826 break; 827 case ELF::R_PPC_ADDR16_HI: 828 writeInt16BE(LocalAddress, applyPPChi(Value + Addend)); 829 break; 830 case ELF::R_PPC_ADDR16_HA: 831 writeInt16BE(LocalAddress, applyPPCha(Value + Addend)); 832 break; 833 } 834 } 835 836 void RuntimeDyldELF::resolvePPC64Relocation(const SectionEntry &Section, 837 uint64_t Offset, uint64_t Value, 838 uint32_t Type, int64_t Addend) { 839 uint8_t *LocalAddress = Section.getAddressWithOffset(Offset); 840 switch (Type) { 841 default: 842 report_fatal_error("Relocation type not implemented yet!"); 843 break; 844 case ELF::R_PPC64_ADDR16: 845 writeInt16BE(LocalAddress, applyPPClo(Value + Addend)); 846 break; 847 case ELF::R_PPC64_ADDR16_DS: 848 writeInt16BE(LocalAddress, applyPPClo(Value + Addend) & ~3); 849 break; 850 case ELF::R_PPC64_ADDR16_LO: 851 writeInt16BE(LocalAddress, applyPPClo(Value + Addend)); 852 break; 853 case ELF::R_PPC64_ADDR16_LO_DS: 854 writeInt16BE(LocalAddress, applyPPClo(Value + Addend) & ~3); 855 break; 856 case ELF::R_PPC64_ADDR16_HI: 857 case ELF::R_PPC64_ADDR16_HIGH: 858 writeInt16BE(LocalAddress, applyPPChi(Value + Addend)); 859 break; 860 case ELF::R_PPC64_ADDR16_HA: 861 case ELF::R_PPC64_ADDR16_HIGHA: 862 writeInt16BE(LocalAddress, applyPPCha(Value + Addend)); 863 break; 864 case ELF::R_PPC64_ADDR16_HIGHER: 865 writeInt16BE(LocalAddress, applyPPChigher(Value + Addend)); 866 break; 867 case ELF::R_PPC64_ADDR16_HIGHERA: 868 writeInt16BE(LocalAddress, applyPPChighera(Value + Addend)); 869 break; 870 case ELF::R_PPC64_ADDR16_HIGHEST: 871 writeInt16BE(LocalAddress, applyPPChighest(Value + Addend)); 872 break; 873 case ELF::R_PPC64_ADDR16_HIGHESTA: 874 writeInt16BE(LocalAddress, applyPPChighesta(Value + Addend)); 875 break; 876 case ELF::R_PPC64_ADDR14: { 877 assert(((Value + Addend) & 3) == 0); 878 // Preserve the AA/LK bits in the branch instruction 879 uint8_t aalk = *(LocalAddress + 3); 880 writeInt16BE(LocalAddress + 2, (aalk & 3) | ((Value + Addend) & 0xfffc)); 881 } break; 882 case ELF::R_PPC64_REL16_LO: { 883 uint64_t FinalAddress = Section.getLoadAddressWithOffset(Offset); 884 uint64_t Delta = Value - FinalAddress + Addend; 885 writeInt16BE(LocalAddress, applyPPClo(Delta)); 886 } break; 887 case ELF::R_PPC64_REL16_HI: { 888 uint64_t FinalAddress = Section.getLoadAddressWithOffset(Offset); 889 uint64_t Delta = Value - FinalAddress + Addend; 890 writeInt16BE(LocalAddress, applyPPChi(Delta)); 891 } break; 892 case ELF::R_PPC64_REL16_HA: { 893 uint64_t FinalAddress = Section.getLoadAddressWithOffset(Offset); 894 uint64_t Delta = Value - FinalAddress + Addend; 895 writeInt16BE(LocalAddress, applyPPCha(Delta)); 896 } break; 897 case ELF::R_PPC64_ADDR32: { 898 int64_t Result = static_cast<int64_t>(Value + Addend); 899 if (SignExtend64<32>(Result) != Result) 900 llvm_unreachable("Relocation R_PPC64_ADDR32 overflow"); 901 writeInt32BE(LocalAddress, Result); 902 } break; 903 case ELF::R_PPC64_REL24: { 904 uint64_t FinalAddress = Section.getLoadAddressWithOffset(Offset); 905 int64_t delta = static_cast<int64_t>(Value - FinalAddress + Addend); 906 if (SignExtend64<26>(delta) != delta) 907 llvm_unreachable("Relocation R_PPC64_REL24 overflow"); 908 // We preserve bits other than LI field, i.e. PO and AA/LK fields. 909 uint32_t Inst = readBytesUnaligned(LocalAddress, 4); 910 writeInt32BE(LocalAddress, (Inst & 0xFC000003) | (delta & 0x03FFFFFC)); 911 } break; 912 case ELF::R_PPC64_REL32: { 913 uint64_t FinalAddress = Section.getLoadAddressWithOffset(Offset); 914 int64_t delta = static_cast<int64_t>(Value - FinalAddress + Addend); 915 if (SignExtend64<32>(delta) != delta) 916 llvm_unreachable("Relocation R_PPC64_REL32 overflow"); 917 writeInt32BE(LocalAddress, delta); 918 } break; 919 case ELF::R_PPC64_REL64: { 920 uint64_t FinalAddress = Section.getLoadAddressWithOffset(Offset); 921 uint64_t Delta = Value - FinalAddress + Addend; 922 writeInt64BE(LocalAddress, Delta); 923 } break; 924 case ELF::R_PPC64_ADDR64: 925 writeInt64BE(LocalAddress, Value + Addend); 926 break; 927 } 928 } 929 930 void RuntimeDyldELF::resolveSystemZRelocation(const SectionEntry &Section, 931 uint64_t Offset, uint64_t Value, 932 uint32_t Type, int64_t Addend) { 933 uint8_t *LocalAddress = Section.getAddressWithOffset(Offset); 934 switch (Type) { 935 default: 936 report_fatal_error("Relocation type not implemented yet!"); 937 break; 938 case ELF::R_390_PC16DBL: 939 case ELF::R_390_PLT16DBL: { 940 int64_t Delta = (Value + Addend) - Section.getLoadAddressWithOffset(Offset); 941 assert(int16_t(Delta / 2) * 2 == Delta && "R_390_PC16DBL overflow"); 942 writeInt16BE(LocalAddress, Delta / 2); 943 break; 944 } 945 case ELF::R_390_PC32DBL: 946 case ELF::R_390_PLT32DBL: { 947 int64_t Delta = (Value + Addend) - Section.getLoadAddressWithOffset(Offset); 948 assert(int32_t(Delta / 2) * 2 == Delta && "R_390_PC32DBL overflow"); 949 writeInt32BE(LocalAddress, Delta / 2); 950 break; 951 } 952 case ELF::R_390_PC16: { 953 int64_t Delta = (Value + Addend) - Section.getLoadAddressWithOffset(Offset); 954 assert(int16_t(Delta) == Delta && "R_390_PC16 overflow"); 955 writeInt16BE(LocalAddress, Delta); 956 break; 957 } 958 case ELF::R_390_PC32: { 959 int64_t Delta = (Value + Addend) - Section.getLoadAddressWithOffset(Offset); 960 assert(int32_t(Delta) == Delta && "R_390_PC32 overflow"); 961 writeInt32BE(LocalAddress, Delta); 962 break; 963 } 964 case ELF::R_390_PC64: { 965 int64_t Delta = (Value + Addend) - Section.getLoadAddressWithOffset(Offset); 966 writeInt64BE(LocalAddress, Delta); 967 break; 968 } 969 case ELF::R_390_8: 970 *LocalAddress = (uint8_t)(Value + Addend); 971 break; 972 case ELF::R_390_16: 973 writeInt16BE(LocalAddress, Value + Addend); 974 break; 975 case ELF::R_390_32: 976 writeInt32BE(LocalAddress, Value + Addend); 977 break; 978 case ELF::R_390_64: 979 writeInt64BE(LocalAddress, Value + Addend); 980 break; 981 } 982 } 983 984 void RuntimeDyldELF::resolveBPFRelocation(const SectionEntry &Section, 985 uint64_t Offset, uint64_t Value, 986 uint32_t Type, int64_t Addend) { 987 bool isBE = Arch == Triple::bpfeb; 988 989 switch (Type) { 990 default: 991 report_fatal_error("Relocation type not implemented yet!"); 992 break; 993 case ELF::R_BPF_NONE: 994 case ELF::R_BPF_64_64: 995 case ELF::R_BPF_64_32: 996 case ELF::R_BPF_64_NODYLD32: 997 break; 998 case ELF::R_BPF_64_ABS64: { 999 write(isBE, Section.getAddressWithOffset(Offset), Value + Addend); 1000 LLVM_DEBUG(dbgs() << "Writing " << format("%p", (Value + Addend)) << " at " 1001 << format("%p\n", Section.getAddressWithOffset(Offset))); 1002 break; 1003 } 1004 case ELF::R_BPF_64_ABS32: { 1005 Value += Addend; 1006 assert(Value <= UINT32_MAX); 1007 write(isBE, Section.getAddressWithOffset(Offset), static_cast<uint32_t>(Value)); 1008 LLVM_DEBUG(dbgs() << "Writing " << format("%p", Value) << " at " 1009 << format("%p\n", Section.getAddressWithOffset(Offset))); 1010 break; 1011 } 1012 } 1013 } 1014 1015 // The target location for the relocation is described by RE.SectionID and 1016 // RE.Offset. RE.SectionID can be used to find the SectionEntry. Each 1017 // SectionEntry has three members describing its location. 1018 // SectionEntry::Address is the address at which the section has been loaded 1019 // into memory in the current (host) process. SectionEntry::LoadAddress is the 1020 // address that the section will have in the target process. 1021 // SectionEntry::ObjAddress is the address of the bits for this section in the 1022 // original emitted object image (also in the current address space). 1023 // 1024 // Relocations will be applied as if the section were loaded at 1025 // SectionEntry::LoadAddress, but they will be applied at an address based 1026 // on SectionEntry::Address. SectionEntry::ObjAddress will be used to refer to 1027 // Target memory contents if they are required for value calculations. 1028 // 1029 // The Value parameter here is the load address of the symbol for the 1030 // relocation to be applied. For relocations which refer to symbols in the 1031 // current object Value will be the LoadAddress of the section in which 1032 // the symbol resides (RE.Addend provides additional information about the 1033 // symbol location). For external symbols, Value will be the address of the 1034 // symbol in the target address space. 1035 void RuntimeDyldELF::resolveRelocation(const RelocationEntry &RE, 1036 uint64_t Value) { 1037 const SectionEntry &Section = Sections[RE.SectionID]; 1038 return resolveRelocation(Section, RE.Offset, Value, RE.RelType, RE.Addend, 1039 RE.SymOffset, RE.SectionID); 1040 } 1041 1042 void RuntimeDyldELF::resolveRelocation(const SectionEntry &Section, 1043 uint64_t Offset, uint64_t Value, 1044 uint32_t Type, int64_t Addend, 1045 uint64_t SymOffset, SID SectionID) { 1046 switch (Arch) { 1047 case Triple::x86_64: 1048 resolveX86_64Relocation(Section, Offset, Value, Type, Addend, SymOffset); 1049 break; 1050 case Triple::x86: 1051 resolveX86Relocation(Section, Offset, (uint32_t)(Value & 0xffffffffL), Type, 1052 (uint32_t)(Addend & 0xffffffffL)); 1053 break; 1054 case Triple::aarch64: 1055 case Triple::aarch64_be: 1056 resolveAArch64Relocation(Section, Offset, Value, Type, Addend); 1057 break; 1058 case Triple::arm: // Fall through. 1059 case Triple::armeb: 1060 case Triple::thumb: 1061 case Triple::thumbeb: 1062 resolveARMRelocation(Section, Offset, (uint32_t)(Value & 0xffffffffL), Type, 1063 (uint32_t)(Addend & 0xffffffffL)); 1064 break; 1065 case Triple::ppc: // Fall through. 1066 case Triple::ppcle: 1067 resolvePPC32Relocation(Section, Offset, Value, Type, Addend); 1068 break; 1069 case Triple::ppc64: // Fall through. 1070 case Triple::ppc64le: 1071 resolvePPC64Relocation(Section, Offset, Value, Type, Addend); 1072 break; 1073 case Triple::systemz: 1074 resolveSystemZRelocation(Section, Offset, Value, Type, Addend); 1075 break; 1076 case Triple::bpfel: 1077 case Triple::bpfeb: 1078 resolveBPFRelocation(Section, Offset, Value, Type, Addend); 1079 break; 1080 default: 1081 llvm_unreachable("Unsupported CPU type!"); 1082 } 1083 } 1084 1085 void *RuntimeDyldELF::computePlaceholderAddress(unsigned SectionID, uint64_t Offset) const { 1086 return (void *)(Sections[SectionID].getObjAddress() + Offset); 1087 } 1088 1089 void RuntimeDyldELF::processSimpleRelocation(unsigned SectionID, uint64_t Offset, unsigned RelType, RelocationValueRef Value) { 1090 RelocationEntry RE(SectionID, Offset, RelType, Value.Addend, Value.Offset); 1091 if (Value.SymbolName) 1092 addRelocationForSymbol(RE, Value.SymbolName); 1093 else 1094 addRelocationForSection(RE, Value.SectionID); 1095 } 1096 1097 uint32_t RuntimeDyldELF::getMatchingLoRelocation(uint32_t RelType, 1098 bool IsLocal) const { 1099 switch (RelType) { 1100 case ELF::R_MICROMIPS_GOT16: 1101 if (IsLocal) 1102 return ELF::R_MICROMIPS_LO16; 1103 break; 1104 case ELF::R_MICROMIPS_HI16: 1105 return ELF::R_MICROMIPS_LO16; 1106 case ELF::R_MIPS_GOT16: 1107 if (IsLocal) 1108 return ELF::R_MIPS_LO16; 1109 break; 1110 case ELF::R_MIPS_HI16: 1111 return ELF::R_MIPS_LO16; 1112 case ELF::R_MIPS_PCHI16: 1113 return ELF::R_MIPS_PCLO16; 1114 default: 1115 break; 1116 } 1117 return ELF::R_MIPS_NONE; 1118 } 1119 1120 // Sometimes we don't need to create thunk for a branch. 1121 // This typically happens when branch target is located 1122 // in the same object file. In such case target is either 1123 // a weak symbol or symbol in a different executable section. 1124 // This function checks if branch target is located in the 1125 // same object file and if distance between source and target 1126 // fits R_AARCH64_CALL26 relocation. If both conditions are 1127 // met, it emits direct jump to the target and returns true. 1128 // Otherwise false is returned and thunk is created. 1129 bool RuntimeDyldELF::resolveAArch64ShortBranch( 1130 unsigned SectionID, relocation_iterator RelI, 1131 const RelocationValueRef &Value) { 1132 uint64_t Address; 1133 if (Value.SymbolName) { 1134 auto Loc = GlobalSymbolTable.find(Value.SymbolName); 1135 1136 // Don't create direct branch for external symbols. 1137 if (Loc == GlobalSymbolTable.end()) 1138 return false; 1139 1140 const auto &SymInfo = Loc->second; 1141 Address = 1142 uint64_t(Sections[SymInfo.getSectionID()].getLoadAddressWithOffset( 1143 SymInfo.getOffset())); 1144 } else { 1145 Address = uint64_t(Sections[Value.SectionID].getLoadAddress()); 1146 } 1147 uint64_t Offset = RelI->getOffset(); 1148 uint64_t SourceAddress = Sections[SectionID].getLoadAddressWithOffset(Offset); 1149 1150 // R_AARCH64_CALL26 requires immediate to be in range -2^27 <= imm < 2^27 1151 // If distance between source and target is out of range then we should 1152 // create thunk. 1153 if (!isInt<28>(Address + Value.Addend - SourceAddress)) 1154 return false; 1155 1156 resolveRelocation(Sections[SectionID], Offset, Address, RelI->getType(), 1157 Value.Addend); 1158 1159 return true; 1160 } 1161 1162 void RuntimeDyldELF::resolveAArch64Branch(unsigned SectionID, 1163 const RelocationValueRef &Value, 1164 relocation_iterator RelI, 1165 StubMap &Stubs) { 1166 1167 LLVM_DEBUG(dbgs() << "\t\tThis is an AArch64 branch relocation."); 1168 SectionEntry &Section = Sections[SectionID]; 1169 1170 uint64_t Offset = RelI->getOffset(); 1171 unsigned RelType = RelI->getType(); 1172 // Look for an existing stub. 1173 StubMap::const_iterator i = Stubs.find(Value); 1174 if (i != Stubs.end()) { 1175 resolveRelocation(Section, Offset, 1176 (uint64_t)Section.getAddressWithOffset(i->second), 1177 RelType, 0); 1178 LLVM_DEBUG(dbgs() << " Stub function found\n"); 1179 } else if (!resolveAArch64ShortBranch(SectionID, RelI, Value)) { 1180 // Create a new stub function. 1181 LLVM_DEBUG(dbgs() << " Create a new stub function\n"); 1182 Stubs[Value] = Section.getStubOffset(); 1183 uint8_t *StubTargetAddr = createStubFunction( 1184 Section.getAddressWithOffset(Section.getStubOffset())); 1185 1186 RelocationEntry REmovz_g3(SectionID, StubTargetAddr - Section.getAddress(), 1187 ELF::R_AARCH64_MOVW_UABS_G3, Value.Addend); 1188 RelocationEntry REmovk_g2(SectionID, 1189 StubTargetAddr - Section.getAddress() + 4, 1190 ELF::R_AARCH64_MOVW_UABS_G2_NC, Value.Addend); 1191 RelocationEntry REmovk_g1(SectionID, 1192 StubTargetAddr - Section.getAddress() + 8, 1193 ELF::R_AARCH64_MOVW_UABS_G1_NC, Value.Addend); 1194 RelocationEntry REmovk_g0(SectionID, 1195 StubTargetAddr - Section.getAddress() + 12, 1196 ELF::R_AARCH64_MOVW_UABS_G0_NC, Value.Addend); 1197 1198 if (Value.SymbolName) { 1199 addRelocationForSymbol(REmovz_g3, Value.SymbolName); 1200 addRelocationForSymbol(REmovk_g2, Value.SymbolName); 1201 addRelocationForSymbol(REmovk_g1, Value.SymbolName); 1202 addRelocationForSymbol(REmovk_g0, Value.SymbolName); 1203 } else { 1204 addRelocationForSection(REmovz_g3, Value.SectionID); 1205 addRelocationForSection(REmovk_g2, Value.SectionID); 1206 addRelocationForSection(REmovk_g1, Value.SectionID); 1207 addRelocationForSection(REmovk_g0, Value.SectionID); 1208 } 1209 resolveRelocation(Section, Offset, 1210 reinterpret_cast<uint64_t>(Section.getAddressWithOffset( 1211 Section.getStubOffset())), 1212 RelType, 0); 1213 Section.advanceStubOffset(getMaxStubSize()); 1214 } 1215 } 1216 1217 Expected<relocation_iterator> 1218 RuntimeDyldELF::processRelocationRef( 1219 unsigned SectionID, relocation_iterator RelI, const ObjectFile &O, 1220 ObjSectionToIDMap &ObjSectionToID, StubMap &Stubs) { 1221 const auto &Obj = cast<ELFObjectFileBase>(O); 1222 uint64_t RelType = RelI->getType(); 1223 int64_t Addend = 0; 1224 if (Expected<int64_t> AddendOrErr = ELFRelocationRef(*RelI).getAddend()) 1225 Addend = *AddendOrErr; 1226 else 1227 consumeError(AddendOrErr.takeError()); 1228 elf_symbol_iterator Symbol = RelI->getSymbol(); 1229 1230 // Obtain the symbol name which is referenced in the relocation 1231 StringRef TargetName; 1232 if (Symbol != Obj.symbol_end()) { 1233 if (auto TargetNameOrErr = Symbol->getName()) 1234 TargetName = *TargetNameOrErr; 1235 else 1236 return TargetNameOrErr.takeError(); 1237 } 1238 LLVM_DEBUG(dbgs() << "\t\tRelType: " << RelType << " Addend: " << Addend 1239 << " TargetName: " << TargetName << "\n"); 1240 RelocationValueRef Value; 1241 // First search for the symbol in the local symbol table 1242 SymbolRef::Type SymType = SymbolRef::ST_Unknown; 1243 1244 // Search for the symbol in the global symbol table 1245 RTDyldSymbolTable::const_iterator gsi = GlobalSymbolTable.end(); 1246 if (Symbol != Obj.symbol_end()) { 1247 gsi = GlobalSymbolTable.find(TargetName.data()); 1248 Expected<SymbolRef::Type> SymTypeOrErr = Symbol->getType(); 1249 if (!SymTypeOrErr) { 1250 std::string Buf; 1251 raw_string_ostream OS(Buf); 1252 logAllUnhandledErrors(SymTypeOrErr.takeError(), OS); 1253 report_fatal_error(Twine(OS.str())); 1254 } 1255 SymType = *SymTypeOrErr; 1256 } 1257 if (gsi != GlobalSymbolTable.end()) { 1258 const auto &SymInfo = gsi->second; 1259 Value.SectionID = SymInfo.getSectionID(); 1260 Value.Offset = SymInfo.getOffset(); 1261 Value.Addend = SymInfo.getOffset() + Addend; 1262 } else { 1263 switch (SymType) { 1264 case SymbolRef::ST_Debug: { 1265 // TODO: Now ELF SymbolRef::ST_Debug = STT_SECTION, it's not obviously 1266 // and can be changed by another developers. Maybe best way is add 1267 // a new symbol type ST_Section to SymbolRef and use it. 1268 auto SectionOrErr = Symbol->getSection(); 1269 if (!SectionOrErr) { 1270 std::string Buf; 1271 raw_string_ostream OS(Buf); 1272 logAllUnhandledErrors(SectionOrErr.takeError(), OS); 1273 report_fatal_error(Twine(OS.str())); 1274 } 1275 section_iterator si = *SectionOrErr; 1276 if (si == Obj.section_end()) 1277 llvm_unreachable("Symbol section not found, bad object file format!"); 1278 LLVM_DEBUG(dbgs() << "\t\tThis is section symbol\n"); 1279 bool isCode = si->isText(); 1280 if (auto SectionIDOrErr = findOrEmitSection(Obj, (*si), isCode, 1281 ObjSectionToID)) 1282 Value.SectionID = *SectionIDOrErr; 1283 else 1284 return SectionIDOrErr.takeError(); 1285 Value.Addend = Addend; 1286 break; 1287 } 1288 case SymbolRef::ST_Data: 1289 case SymbolRef::ST_Function: 1290 case SymbolRef::ST_Other: 1291 case SymbolRef::ST_Unknown: { 1292 Value.SymbolName = TargetName.data(); 1293 Value.Addend = Addend; 1294 1295 // Absolute relocations will have a zero symbol ID (STN_UNDEF), which 1296 // will manifest here as a NULL symbol name. 1297 // We can set this as a valid (but empty) symbol name, and rely 1298 // on addRelocationForSymbol to handle this. 1299 if (!Value.SymbolName) 1300 Value.SymbolName = ""; 1301 break; 1302 } 1303 default: 1304 llvm_unreachable("Unresolved symbol type!"); 1305 break; 1306 } 1307 } 1308 1309 uint64_t Offset = RelI->getOffset(); 1310 1311 LLVM_DEBUG(dbgs() << "\t\tSectionID: " << SectionID << " Offset: " << Offset 1312 << "\n"); 1313 if ((Arch == Triple::aarch64 || Arch == Triple::aarch64_be)) { 1314 if ((RelType == ELF::R_AARCH64_CALL26 || 1315 RelType == ELF::R_AARCH64_JUMP26) && 1316 MemMgr.allowStubAllocation()) { 1317 resolveAArch64Branch(SectionID, Value, RelI, Stubs); 1318 } else if (RelType == ELF::R_AARCH64_ADR_GOT_PAGE) { 1319 // Create new GOT entry or find existing one. If GOT entry is 1320 // to be created, then we also emit ABS64 relocation for it. 1321 uint64_t GOTOffset = findOrAllocGOTEntry(Value, ELF::R_AARCH64_ABS64); 1322 resolveGOTOffsetRelocation(SectionID, Offset, GOTOffset + Addend, 1323 ELF::R_AARCH64_ADR_PREL_PG_HI21); 1324 1325 } else if (RelType == ELF::R_AARCH64_LD64_GOT_LO12_NC) { 1326 uint64_t GOTOffset = findOrAllocGOTEntry(Value, ELF::R_AARCH64_ABS64); 1327 resolveGOTOffsetRelocation(SectionID, Offset, GOTOffset + Addend, 1328 ELF::R_AARCH64_LDST64_ABS_LO12_NC); 1329 } else { 1330 processSimpleRelocation(SectionID, Offset, RelType, Value); 1331 } 1332 } else if (Arch == Triple::arm) { 1333 if (RelType == ELF::R_ARM_PC24 || RelType == ELF::R_ARM_CALL || 1334 RelType == ELF::R_ARM_JUMP24) { 1335 // This is an ARM branch relocation, need to use a stub function. 1336 LLVM_DEBUG(dbgs() << "\t\tThis is an ARM branch relocation.\n"); 1337 SectionEntry &Section = Sections[SectionID]; 1338 1339 // Look for an existing stub. 1340 StubMap::const_iterator i = Stubs.find(Value); 1341 if (i != Stubs.end()) { 1342 resolveRelocation( 1343 Section, Offset, 1344 reinterpret_cast<uint64_t>(Section.getAddressWithOffset(i->second)), 1345 RelType, 0); 1346 LLVM_DEBUG(dbgs() << " Stub function found\n"); 1347 } else { 1348 // Create a new stub function. 1349 LLVM_DEBUG(dbgs() << " Create a new stub function\n"); 1350 Stubs[Value] = Section.getStubOffset(); 1351 uint8_t *StubTargetAddr = createStubFunction( 1352 Section.getAddressWithOffset(Section.getStubOffset())); 1353 RelocationEntry RE(SectionID, StubTargetAddr - Section.getAddress(), 1354 ELF::R_ARM_ABS32, Value.Addend); 1355 if (Value.SymbolName) 1356 addRelocationForSymbol(RE, Value.SymbolName); 1357 else 1358 addRelocationForSection(RE, Value.SectionID); 1359 1360 resolveRelocation(Section, Offset, reinterpret_cast<uint64_t>( 1361 Section.getAddressWithOffset( 1362 Section.getStubOffset())), 1363 RelType, 0); 1364 Section.advanceStubOffset(getMaxStubSize()); 1365 } 1366 } else { 1367 uint32_t *Placeholder = 1368 reinterpret_cast<uint32_t*>(computePlaceholderAddress(SectionID, Offset)); 1369 if (RelType == ELF::R_ARM_PREL31 || RelType == ELF::R_ARM_TARGET1 || 1370 RelType == ELF::R_ARM_ABS32) { 1371 Value.Addend += *Placeholder; 1372 } else if (RelType == ELF::R_ARM_MOVW_ABS_NC || RelType == ELF::R_ARM_MOVT_ABS) { 1373 // See ELF for ARM documentation 1374 Value.Addend += (int16_t)((*Placeholder & 0xFFF) | (((*Placeholder >> 16) & 0xF) << 12)); 1375 } 1376 processSimpleRelocation(SectionID, Offset, RelType, Value); 1377 } 1378 } else if (IsMipsO32ABI) { 1379 uint8_t *Placeholder = reinterpret_cast<uint8_t *>( 1380 computePlaceholderAddress(SectionID, Offset)); 1381 uint32_t Opcode = readBytesUnaligned(Placeholder, 4); 1382 if (RelType == ELF::R_MIPS_26) { 1383 // This is an Mips branch relocation, need to use a stub function. 1384 LLVM_DEBUG(dbgs() << "\t\tThis is a Mips branch relocation."); 1385 SectionEntry &Section = Sections[SectionID]; 1386 1387 // Extract the addend from the instruction. 1388 // We shift up by two since the Value will be down shifted again 1389 // when applying the relocation. 1390 uint32_t Addend = (Opcode & 0x03ffffff) << 2; 1391 1392 Value.Addend += Addend; 1393 1394 // Look up for existing stub. 1395 StubMap::const_iterator i = Stubs.find(Value); 1396 if (i != Stubs.end()) { 1397 RelocationEntry RE(SectionID, Offset, RelType, i->second); 1398 addRelocationForSection(RE, SectionID); 1399 LLVM_DEBUG(dbgs() << " Stub function found\n"); 1400 } else { 1401 // Create a new stub function. 1402 LLVM_DEBUG(dbgs() << " Create a new stub function\n"); 1403 Stubs[Value] = Section.getStubOffset(); 1404 1405 unsigned AbiVariant = Obj.getPlatformFlags(); 1406 1407 uint8_t *StubTargetAddr = createStubFunction( 1408 Section.getAddressWithOffset(Section.getStubOffset()), AbiVariant); 1409 1410 // Creating Hi and Lo relocations for the filled stub instructions. 1411 RelocationEntry REHi(SectionID, StubTargetAddr - Section.getAddress(), 1412 ELF::R_MIPS_HI16, Value.Addend); 1413 RelocationEntry RELo(SectionID, 1414 StubTargetAddr - Section.getAddress() + 4, 1415 ELF::R_MIPS_LO16, Value.Addend); 1416 1417 if (Value.SymbolName) { 1418 addRelocationForSymbol(REHi, Value.SymbolName); 1419 addRelocationForSymbol(RELo, Value.SymbolName); 1420 } else { 1421 addRelocationForSection(REHi, Value.SectionID); 1422 addRelocationForSection(RELo, Value.SectionID); 1423 } 1424 1425 RelocationEntry RE(SectionID, Offset, RelType, Section.getStubOffset()); 1426 addRelocationForSection(RE, SectionID); 1427 Section.advanceStubOffset(getMaxStubSize()); 1428 } 1429 } else if (RelType == ELF::R_MIPS_HI16 || RelType == ELF::R_MIPS_PCHI16) { 1430 int64_t Addend = (Opcode & 0x0000ffff) << 16; 1431 RelocationEntry RE(SectionID, Offset, RelType, Addend); 1432 PendingRelocs.push_back(std::make_pair(Value, RE)); 1433 } else if (RelType == ELF::R_MIPS_LO16 || RelType == ELF::R_MIPS_PCLO16) { 1434 int64_t Addend = Value.Addend + SignExtend32<16>(Opcode & 0x0000ffff); 1435 for (auto I = PendingRelocs.begin(); I != PendingRelocs.end();) { 1436 const RelocationValueRef &MatchingValue = I->first; 1437 RelocationEntry &Reloc = I->second; 1438 if (MatchingValue == Value && 1439 RelType == getMatchingLoRelocation(Reloc.RelType) && 1440 SectionID == Reloc.SectionID) { 1441 Reloc.Addend += Addend; 1442 if (Value.SymbolName) 1443 addRelocationForSymbol(Reloc, Value.SymbolName); 1444 else 1445 addRelocationForSection(Reloc, Value.SectionID); 1446 I = PendingRelocs.erase(I); 1447 } else 1448 ++I; 1449 } 1450 RelocationEntry RE(SectionID, Offset, RelType, Addend); 1451 if (Value.SymbolName) 1452 addRelocationForSymbol(RE, Value.SymbolName); 1453 else 1454 addRelocationForSection(RE, Value.SectionID); 1455 } else { 1456 if (RelType == ELF::R_MIPS_32) 1457 Value.Addend += Opcode; 1458 else if (RelType == ELF::R_MIPS_PC16) 1459 Value.Addend += SignExtend32<18>((Opcode & 0x0000ffff) << 2); 1460 else if (RelType == ELF::R_MIPS_PC19_S2) 1461 Value.Addend += SignExtend32<21>((Opcode & 0x0007ffff) << 2); 1462 else if (RelType == ELF::R_MIPS_PC21_S2) 1463 Value.Addend += SignExtend32<23>((Opcode & 0x001fffff) << 2); 1464 else if (RelType == ELF::R_MIPS_PC26_S2) 1465 Value.Addend += SignExtend32<28>((Opcode & 0x03ffffff) << 2); 1466 processSimpleRelocation(SectionID, Offset, RelType, Value); 1467 } 1468 } else if (IsMipsN32ABI || IsMipsN64ABI) { 1469 uint32_t r_type = RelType & 0xff; 1470 RelocationEntry RE(SectionID, Offset, RelType, Value.Addend); 1471 if (r_type == ELF::R_MIPS_CALL16 || r_type == ELF::R_MIPS_GOT_PAGE 1472 || r_type == ELF::R_MIPS_GOT_DISP) { 1473 StringMap<uint64_t>::iterator i = GOTSymbolOffsets.find(TargetName); 1474 if (i != GOTSymbolOffsets.end()) 1475 RE.SymOffset = i->second; 1476 else { 1477 RE.SymOffset = allocateGOTEntries(1); 1478 GOTSymbolOffsets[TargetName] = RE.SymOffset; 1479 } 1480 if (Value.SymbolName) 1481 addRelocationForSymbol(RE, Value.SymbolName); 1482 else 1483 addRelocationForSection(RE, Value.SectionID); 1484 } else if (RelType == ELF::R_MIPS_26) { 1485 // This is an Mips branch relocation, need to use a stub function. 1486 LLVM_DEBUG(dbgs() << "\t\tThis is a Mips branch relocation."); 1487 SectionEntry &Section = Sections[SectionID]; 1488 1489 // Look up for existing stub. 1490 StubMap::const_iterator i = Stubs.find(Value); 1491 if (i != Stubs.end()) { 1492 RelocationEntry RE(SectionID, Offset, RelType, i->second); 1493 addRelocationForSection(RE, SectionID); 1494 LLVM_DEBUG(dbgs() << " Stub function found\n"); 1495 } else { 1496 // Create a new stub function. 1497 LLVM_DEBUG(dbgs() << " Create a new stub function\n"); 1498 Stubs[Value] = Section.getStubOffset(); 1499 1500 unsigned AbiVariant = Obj.getPlatformFlags(); 1501 1502 uint8_t *StubTargetAddr = createStubFunction( 1503 Section.getAddressWithOffset(Section.getStubOffset()), AbiVariant); 1504 1505 if (IsMipsN32ABI) { 1506 // Creating Hi and Lo relocations for the filled stub instructions. 1507 RelocationEntry REHi(SectionID, StubTargetAddr - Section.getAddress(), 1508 ELF::R_MIPS_HI16, Value.Addend); 1509 RelocationEntry RELo(SectionID, 1510 StubTargetAddr - Section.getAddress() + 4, 1511 ELF::R_MIPS_LO16, Value.Addend); 1512 if (Value.SymbolName) { 1513 addRelocationForSymbol(REHi, Value.SymbolName); 1514 addRelocationForSymbol(RELo, Value.SymbolName); 1515 } else { 1516 addRelocationForSection(REHi, Value.SectionID); 1517 addRelocationForSection(RELo, Value.SectionID); 1518 } 1519 } else { 1520 // Creating Highest, Higher, Hi and Lo relocations for the filled stub 1521 // instructions. 1522 RelocationEntry REHighest(SectionID, 1523 StubTargetAddr - Section.getAddress(), 1524 ELF::R_MIPS_HIGHEST, Value.Addend); 1525 RelocationEntry REHigher(SectionID, 1526 StubTargetAddr - Section.getAddress() + 4, 1527 ELF::R_MIPS_HIGHER, Value.Addend); 1528 RelocationEntry REHi(SectionID, 1529 StubTargetAddr - Section.getAddress() + 12, 1530 ELF::R_MIPS_HI16, Value.Addend); 1531 RelocationEntry RELo(SectionID, 1532 StubTargetAddr - Section.getAddress() + 20, 1533 ELF::R_MIPS_LO16, Value.Addend); 1534 if (Value.SymbolName) { 1535 addRelocationForSymbol(REHighest, Value.SymbolName); 1536 addRelocationForSymbol(REHigher, Value.SymbolName); 1537 addRelocationForSymbol(REHi, Value.SymbolName); 1538 addRelocationForSymbol(RELo, Value.SymbolName); 1539 } else { 1540 addRelocationForSection(REHighest, Value.SectionID); 1541 addRelocationForSection(REHigher, Value.SectionID); 1542 addRelocationForSection(REHi, Value.SectionID); 1543 addRelocationForSection(RELo, Value.SectionID); 1544 } 1545 } 1546 RelocationEntry RE(SectionID, Offset, RelType, Section.getStubOffset()); 1547 addRelocationForSection(RE, SectionID); 1548 Section.advanceStubOffset(getMaxStubSize()); 1549 } 1550 } else { 1551 processSimpleRelocation(SectionID, Offset, RelType, Value); 1552 } 1553 1554 } else if (Arch == Triple::ppc64 || Arch == Triple::ppc64le) { 1555 if (RelType == ELF::R_PPC64_REL24) { 1556 // Determine ABI variant in use for this object. 1557 unsigned AbiVariant = Obj.getPlatformFlags(); 1558 AbiVariant &= ELF::EF_PPC64_ABI; 1559 // A PPC branch relocation will need a stub function if the target is 1560 // an external symbol (either Value.SymbolName is set, or SymType is 1561 // Symbol::ST_Unknown) or if the target address is not within the 1562 // signed 24-bits branch address. 1563 SectionEntry &Section = Sections[SectionID]; 1564 uint8_t *Target = Section.getAddressWithOffset(Offset); 1565 bool RangeOverflow = false; 1566 bool IsExtern = Value.SymbolName || SymType == SymbolRef::ST_Unknown; 1567 if (!IsExtern) { 1568 if (AbiVariant != 2) { 1569 // In the ELFv1 ABI, a function call may point to the .opd entry, 1570 // so the final symbol value is calculated based on the relocation 1571 // values in the .opd section. 1572 if (auto Err = findOPDEntrySection(Obj, ObjSectionToID, Value)) 1573 return std::move(Err); 1574 } else { 1575 // In the ELFv2 ABI, a function symbol may provide a local entry 1576 // point, which must be used for direct calls. 1577 if (Value.SectionID == SectionID){ 1578 uint8_t SymOther = Symbol->getOther(); 1579 Value.Addend += ELF::decodePPC64LocalEntryOffset(SymOther); 1580 } 1581 } 1582 uint8_t *RelocTarget = 1583 Sections[Value.SectionID].getAddressWithOffset(Value.Addend); 1584 int64_t delta = static_cast<int64_t>(Target - RelocTarget); 1585 // If it is within 26-bits branch range, just set the branch target 1586 if (SignExtend64<26>(delta) != delta) { 1587 RangeOverflow = true; 1588 } else if ((AbiVariant != 2) || 1589 (AbiVariant == 2 && Value.SectionID == SectionID)) { 1590 RelocationEntry RE(SectionID, Offset, RelType, Value.Addend); 1591 addRelocationForSection(RE, Value.SectionID); 1592 } 1593 } 1594 if (IsExtern || (AbiVariant == 2 && Value.SectionID != SectionID) || 1595 RangeOverflow) { 1596 // It is an external symbol (either Value.SymbolName is set, or 1597 // SymType is SymbolRef::ST_Unknown) or out of range. 1598 StubMap::const_iterator i = Stubs.find(Value); 1599 if (i != Stubs.end()) { 1600 // Symbol function stub already created, just relocate to it 1601 resolveRelocation(Section, Offset, 1602 reinterpret_cast<uint64_t>( 1603 Section.getAddressWithOffset(i->second)), 1604 RelType, 0); 1605 LLVM_DEBUG(dbgs() << " Stub function found\n"); 1606 } else { 1607 // Create a new stub function. 1608 LLVM_DEBUG(dbgs() << " Create a new stub function\n"); 1609 Stubs[Value] = Section.getStubOffset(); 1610 uint8_t *StubTargetAddr = createStubFunction( 1611 Section.getAddressWithOffset(Section.getStubOffset()), 1612 AbiVariant); 1613 RelocationEntry RE(SectionID, StubTargetAddr - Section.getAddress(), 1614 ELF::R_PPC64_ADDR64, Value.Addend); 1615 1616 // Generates the 64-bits address loads as exemplified in section 1617 // 4.5.1 in PPC64 ELF ABI. Note that the relocations need to 1618 // apply to the low part of the instructions, so we have to update 1619 // the offset according to the target endianness. 1620 uint64_t StubRelocOffset = StubTargetAddr - Section.getAddress(); 1621 if (!IsTargetLittleEndian) 1622 StubRelocOffset += 2; 1623 1624 RelocationEntry REhst(SectionID, StubRelocOffset + 0, 1625 ELF::R_PPC64_ADDR16_HIGHEST, Value.Addend); 1626 RelocationEntry REhr(SectionID, StubRelocOffset + 4, 1627 ELF::R_PPC64_ADDR16_HIGHER, Value.Addend); 1628 RelocationEntry REh(SectionID, StubRelocOffset + 12, 1629 ELF::R_PPC64_ADDR16_HI, Value.Addend); 1630 RelocationEntry REl(SectionID, StubRelocOffset + 16, 1631 ELF::R_PPC64_ADDR16_LO, Value.Addend); 1632 1633 if (Value.SymbolName) { 1634 addRelocationForSymbol(REhst, Value.SymbolName); 1635 addRelocationForSymbol(REhr, Value.SymbolName); 1636 addRelocationForSymbol(REh, Value.SymbolName); 1637 addRelocationForSymbol(REl, Value.SymbolName); 1638 } else { 1639 addRelocationForSection(REhst, Value.SectionID); 1640 addRelocationForSection(REhr, Value.SectionID); 1641 addRelocationForSection(REh, Value.SectionID); 1642 addRelocationForSection(REl, Value.SectionID); 1643 } 1644 1645 resolveRelocation(Section, Offset, reinterpret_cast<uint64_t>( 1646 Section.getAddressWithOffset( 1647 Section.getStubOffset())), 1648 RelType, 0); 1649 Section.advanceStubOffset(getMaxStubSize()); 1650 } 1651 if (IsExtern || (AbiVariant == 2 && Value.SectionID != SectionID)) { 1652 // Restore the TOC for external calls 1653 if (AbiVariant == 2) 1654 writeInt32BE(Target + 4, 0xE8410018); // ld r2,24(r1) 1655 else 1656 writeInt32BE(Target + 4, 0xE8410028); // ld r2,40(r1) 1657 } 1658 } 1659 } else if (RelType == ELF::R_PPC64_TOC16 || 1660 RelType == ELF::R_PPC64_TOC16_DS || 1661 RelType == ELF::R_PPC64_TOC16_LO || 1662 RelType == ELF::R_PPC64_TOC16_LO_DS || 1663 RelType == ELF::R_PPC64_TOC16_HI || 1664 RelType == ELF::R_PPC64_TOC16_HA) { 1665 // These relocations are supposed to subtract the TOC address from 1666 // the final value. This does not fit cleanly into the RuntimeDyld 1667 // scheme, since there may be *two* sections involved in determining 1668 // the relocation value (the section of the symbol referred to by the 1669 // relocation, and the TOC section associated with the current module). 1670 // 1671 // Fortunately, these relocations are currently only ever generated 1672 // referring to symbols that themselves reside in the TOC, which means 1673 // that the two sections are actually the same. Thus they cancel out 1674 // and we can immediately resolve the relocation right now. 1675 switch (RelType) { 1676 case ELF::R_PPC64_TOC16: RelType = ELF::R_PPC64_ADDR16; break; 1677 case ELF::R_PPC64_TOC16_DS: RelType = ELF::R_PPC64_ADDR16_DS; break; 1678 case ELF::R_PPC64_TOC16_LO: RelType = ELF::R_PPC64_ADDR16_LO; break; 1679 case ELF::R_PPC64_TOC16_LO_DS: RelType = ELF::R_PPC64_ADDR16_LO_DS; break; 1680 case ELF::R_PPC64_TOC16_HI: RelType = ELF::R_PPC64_ADDR16_HI; break; 1681 case ELF::R_PPC64_TOC16_HA: RelType = ELF::R_PPC64_ADDR16_HA; break; 1682 default: llvm_unreachable("Wrong relocation type."); 1683 } 1684 1685 RelocationValueRef TOCValue; 1686 if (auto Err = findPPC64TOCSection(Obj, ObjSectionToID, TOCValue)) 1687 return std::move(Err); 1688 if (Value.SymbolName || Value.SectionID != TOCValue.SectionID) 1689 llvm_unreachable("Unsupported TOC relocation."); 1690 Value.Addend -= TOCValue.Addend; 1691 resolveRelocation(Sections[SectionID], Offset, Value.Addend, RelType, 0); 1692 } else { 1693 // There are two ways to refer to the TOC address directly: either 1694 // via a ELF::R_PPC64_TOC relocation (where both symbol and addend are 1695 // ignored), or via any relocation that refers to the magic ".TOC." 1696 // symbols (in which case the addend is respected). 1697 if (RelType == ELF::R_PPC64_TOC) { 1698 RelType = ELF::R_PPC64_ADDR64; 1699 if (auto Err = findPPC64TOCSection(Obj, ObjSectionToID, Value)) 1700 return std::move(Err); 1701 } else if (TargetName == ".TOC.") { 1702 if (auto Err = findPPC64TOCSection(Obj, ObjSectionToID, Value)) 1703 return std::move(Err); 1704 Value.Addend += Addend; 1705 } 1706 1707 RelocationEntry RE(SectionID, Offset, RelType, Value.Addend); 1708 1709 if (Value.SymbolName) 1710 addRelocationForSymbol(RE, Value.SymbolName); 1711 else 1712 addRelocationForSection(RE, Value.SectionID); 1713 } 1714 } else if (Arch == Triple::systemz && 1715 (RelType == ELF::R_390_PLT32DBL || RelType == ELF::R_390_GOTENT)) { 1716 // Create function stubs for both PLT and GOT references, regardless of 1717 // whether the GOT reference is to data or code. The stub contains the 1718 // full address of the symbol, as needed by GOT references, and the 1719 // executable part only adds an overhead of 8 bytes. 1720 // 1721 // We could try to conserve space by allocating the code and data 1722 // parts of the stub separately. However, as things stand, we allocate 1723 // a stub for every relocation, so using a GOT in JIT code should be 1724 // no less space efficient than using an explicit constant pool. 1725 LLVM_DEBUG(dbgs() << "\t\tThis is a SystemZ indirect relocation."); 1726 SectionEntry &Section = Sections[SectionID]; 1727 1728 // Look for an existing stub. 1729 StubMap::const_iterator i = Stubs.find(Value); 1730 uintptr_t StubAddress; 1731 if (i != Stubs.end()) { 1732 StubAddress = uintptr_t(Section.getAddressWithOffset(i->second)); 1733 LLVM_DEBUG(dbgs() << " Stub function found\n"); 1734 } else { 1735 // Create a new stub function. 1736 LLVM_DEBUG(dbgs() << " Create a new stub function\n"); 1737 1738 uintptr_t BaseAddress = uintptr_t(Section.getAddress()); 1739 StubAddress = 1740 alignTo(BaseAddress + Section.getStubOffset(), getStubAlignment()); 1741 unsigned StubOffset = StubAddress - BaseAddress; 1742 1743 Stubs[Value] = StubOffset; 1744 createStubFunction((uint8_t *)StubAddress); 1745 RelocationEntry RE(SectionID, StubOffset + 8, ELF::R_390_64, 1746 Value.Offset); 1747 if (Value.SymbolName) 1748 addRelocationForSymbol(RE, Value.SymbolName); 1749 else 1750 addRelocationForSection(RE, Value.SectionID); 1751 Section.advanceStubOffset(getMaxStubSize()); 1752 } 1753 1754 if (RelType == ELF::R_390_GOTENT) 1755 resolveRelocation(Section, Offset, StubAddress + 8, ELF::R_390_PC32DBL, 1756 Addend); 1757 else 1758 resolveRelocation(Section, Offset, StubAddress, RelType, Addend); 1759 } else if (Arch == Triple::x86_64) { 1760 if (RelType == ELF::R_X86_64_PLT32) { 1761 // The way the PLT relocations normally work is that the linker allocates 1762 // the 1763 // PLT and this relocation makes a PC-relative call into the PLT. The PLT 1764 // entry will then jump to an address provided by the GOT. On first call, 1765 // the 1766 // GOT address will point back into PLT code that resolves the symbol. After 1767 // the first call, the GOT entry points to the actual function. 1768 // 1769 // For local functions we're ignoring all of that here and just replacing 1770 // the PLT32 relocation type with PC32, which will translate the relocation 1771 // into a PC-relative call directly to the function. For external symbols we 1772 // can't be sure the function will be within 2^32 bytes of the call site, so 1773 // we need to create a stub, which calls into the GOT. This case is 1774 // equivalent to the usual PLT implementation except that we use the stub 1775 // mechanism in RuntimeDyld (which puts stubs at the end of the section) 1776 // rather than allocating a PLT section. 1777 if (Value.SymbolName && MemMgr.allowStubAllocation()) { 1778 // This is a call to an external function. 1779 // Look for an existing stub. 1780 SectionEntry *Section = &Sections[SectionID]; 1781 StubMap::const_iterator i = Stubs.find(Value); 1782 uintptr_t StubAddress; 1783 if (i != Stubs.end()) { 1784 StubAddress = uintptr_t(Section->getAddress()) + i->second; 1785 LLVM_DEBUG(dbgs() << " Stub function found\n"); 1786 } else { 1787 // Create a new stub function (equivalent to a PLT entry). 1788 LLVM_DEBUG(dbgs() << " Create a new stub function\n"); 1789 1790 uintptr_t BaseAddress = uintptr_t(Section->getAddress()); 1791 StubAddress = alignTo(BaseAddress + Section->getStubOffset(), 1792 getStubAlignment()); 1793 unsigned StubOffset = StubAddress - BaseAddress; 1794 Stubs[Value] = StubOffset; 1795 createStubFunction((uint8_t *)StubAddress); 1796 1797 // Bump our stub offset counter 1798 Section->advanceStubOffset(getMaxStubSize()); 1799 1800 // Allocate a GOT Entry 1801 uint64_t GOTOffset = allocateGOTEntries(1); 1802 // This potentially creates a new Section which potentially 1803 // invalidates the Section pointer, so reload it. 1804 Section = &Sections[SectionID]; 1805 1806 // The load of the GOT address has an addend of -4 1807 resolveGOTOffsetRelocation(SectionID, StubOffset + 2, GOTOffset - 4, 1808 ELF::R_X86_64_PC32); 1809 1810 // Fill in the value of the symbol we're targeting into the GOT 1811 addRelocationForSymbol( 1812 computeGOTOffsetRE(GOTOffset, 0, ELF::R_X86_64_64), 1813 Value.SymbolName); 1814 } 1815 1816 // Make the target call a call into the stub table. 1817 resolveRelocation(*Section, Offset, StubAddress, ELF::R_X86_64_PC32, 1818 Addend); 1819 } else { 1820 Value.Addend += support::ulittle32_t::ref( 1821 computePlaceholderAddress(SectionID, Offset)); 1822 processSimpleRelocation(SectionID, Offset, ELF::R_X86_64_PC32, Value); 1823 } 1824 } else if (RelType == ELF::R_X86_64_GOTPCREL || 1825 RelType == ELF::R_X86_64_GOTPCRELX || 1826 RelType == ELF::R_X86_64_REX_GOTPCRELX) { 1827 uint64_t GOTOffset = allocateGOTEntries(1); 1828 resolveGOTOffsetRelocation(SectionID, Offset, GOTOffset + Addend, 1829 ELF::R_X86_64_PC32); 1830 1831 // Fill in the value of the symbol we're targeting into the GOT 1832 RelocationEntry RE = 1833 computeGOTOffsetRE(GOTOffset, Value.Offset, ELF::R_X86_64_64); 1834 if (Value.SymbolName) 1835 addRelocationForSymbol(RE, Value.SymbolName); 1836 else 1837 addRelocationForSection(RE, Value.SectionID); 1838 } else if (RelType == ELF::R_X86_64_GOT64) { 1839 // Fill in a 64-bit GOT offset. 1840 uint64_t GOTOffset = allocateGOTEntries(1); 1841 resolveRelocation(Sections[SectionID], Offset, GOTOffset, 1842 ELF::R_X86_64_64, 0); 1843 1844 // Fill in the value of the symbol we're targeting into the GOT 1845 RelocationEntry RE = 1846 computeGOTOffsetRE(GOTOffset, Value.Offset, ELF::R_X86_64_64); 1847 if (Value.SymbolName) 1848 addRelocationForSymbol(RE, Value.SymbolName); 1849 else 1850 addRelocationForSection(RE, Value.SectionID); 1851 } else if (RelType == ELF::R_X86_64_GOTPC32) { 1852 // Materialize the address of the base of the GOT relative to the PC. 1853 // This doesn't create a GOT entry, but it does mean we need a GOT 1854 // section. 1855 (void)allocateGOTEntries(0); 1856 resolveGOTOffsetRelocation(SectionID, Offset, Addend, ELF::R_X86_64_PC32); 1857 } else if (RelType == ELF::R_X86_64_GOTPC64) { 1858 (void)allocateGOTEntries(0); 1859 resolveGOTOffsetRelocation(SectionID, Offset, Addend, ELF::R_X86_64_PC64); 1860 } else if (RelType == ELF::R_X86_64_GOTOFF64) { 1861 // GOTOFF relocations ultimately require a section difference relocation. 1862 (void)allocateGOTEntries(0); 1863 processSimpleRelocation(SectionID, Offset, RelType, Value); 1864 } else if (RelType == ELF::R_X86_64_PC32) { 1865 Value.Addend += support::ulittle32_t::ref(computePlaceholderAddress(SectionID, Offset)); 1866 processSimpleRelocation(SectionID, Offset, RelType, Value); 1867 } else if (RelType == ELF::R_X86_64_PC64) { 1868 Value.Addend += support::ulittle64_t::ref(computePlaceholderAddress(SectionID, Offset)); 1869 processSimpleRelocation(SectionID, Offset, RelType, Value); 1870 } else if (RelType == ELF::R_X86_64_GOTTPOFF) { 1871 processX86_64GOTTPOFFRelocation(SectionID, Offset, Value, Addend); 1872 } else if (RelType == ELF::R_X86_64_TLSGD || 1873 RelType == ELF::R_X86_64_TLSLD) { 1874 // The next relocation must be the relocation for __tls_get_addr. 1875 ++RelI; 1876 auto &GetAddrRelocation = *RelI; 1877 processX86_64TLSRelocation(SectionID, Offset, RelType, Value, Addend, 1878 GetAddrRelocation); 1879 } else { 1880 processSimpleRelocation(SectionID, Offset, RelType, Value); 1881 } 1882 } else { 1883 if (Arch == Triple::x86) { 1884 Value.Addend += support::ulittle32_t::ref(computePlaceholderAddress(SectionID, Offset)); 1885 } 1886 processSimpleRelocation(SectionID, Offset, RelType, Value); 1887 } 1888 return ++RelI; 1889 } 1890 1891 void RuntimeDyldELF::processX86_64GOTTPOFFRelocation(unsigned SectionID, 1892 uint64_t Offset, 1893 RelocationValueRef Value, 1894 int64_t Addend) { 1895 // Use the approach from "x86-64 Linker Optimizations" from the TLS spec 1896 // to replace the GOTTPOFF relocation with a TPOFF relocation. The spec 1897 // only mentions one optimization even though there are two different 1898 // code sequences for the Initial Exec TLS Model. We match the code to 1899 // find out which one was used. 1900 1901 // A possible TLS code sequence and its replacement 1902 struct CodeSequence { 1903 // The expected code sequence 1904 ArrayRef<uint8_t> ExpectedCodeSequence; 1905 // The negative offset of the GOTTPOFF relocation to the beginning of 1906 // the sequence 1907 uint64_t TLSSequenceOffset; 1908 // The new code sequence 1909 ArrayRef<uint8_t> NewCodeSequence; 1910 // The offset of the new TPOFF relocation 1911 uint64_t TpoffRelocationOffset; 1912 }; 1913 1914 std::array<CodeSequence, 2> CodeSequences; 1915 1916 // Initial Exec Code Model Sequence 1917 { 1918 static const std::initializer_list<uint8_t> ExpectedCodeSequenceList = { 1919 0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00, 1920 0x00, // mov %fs:0, %rax 1921 0x48, 0x03, 0x05, 0x00, 0x00, 0x00, 0x00 // add x@gotpoff(%rip), 1922 // %rax 1923 }; 1924 CodeSequences[0].ExpectedCodeSequence = 1925 ArrayRef<uint8_t>(ExpectedCodeSequenceList); 1926 CodeSequences[0].TLSSequenceOffset = 12; 1927 1928 static const std::initializer_list<uint8_t> NewCodeSequenceList = { 1929 0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00, 0x00, // mov %fs:0, %rax 1930 0x48, 0x8d, 0x80, 0x00, 0x00, 0x00, 0x00 // lea x@tpoff(%rax), %rax 1931 }; 1932 CodeSequences[0].NewCodeSequence = ArrayRef<uint8_t>(NewCodeSequenceList); 1933 CodeSequences[0].TpoffRelocationOffset = 12; 1934 } 1935 1936 // Initial Exec Code Model Sequence, II 1937 { 1938 static const std::initializer_list<uint8_t> ExpectedCodeSequenceList = { 1939 0x48, 0x8b, 0x05, 0x00, 0x00, 0x00, 0x00, // mov x@gotpoff(%rip), %rax 1940 0x64, 0x48, 0x8b, 0x00, 0x00, 0x00, 0x00 // mov %fs:(%rax), %rax 1941 }; 1942 CodeSequences[1].ExpectedCodeSequence = 1943 ArrayRef<uint8_t>(ExpectedCodeSequenceList); 1944 CodeSequences[1].TLSSequenceOffset = 3; 1945 1946 static const std::initializer_list<uint8_t> NewCodeSequenceList = { 1947 0x66, 0x0f, 0x1f, 0x44, 0x00, 0x00, // 6 byte nop 1948 0x64, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00, 0x00, // mov %fs:x@tpoff, %rax 1949 }; 1950 CodeSequences[1].NewCodeSequence = ArrayRef<uint8_t>(NewCodeSequenceList); 1951 CodeSequences[1].TpoffRelocationOffset = 10; 1952 } 1953 1954 bool Resolved = false; 1955 auto &Section = Sections[SectionID]; 1956 for (const auto &C : CodeSequences) { 1957 assert(C.ExpectedCodeSequence.size() == C.NewCodeSequence.size() && 1958 "Old and new code sequences must have the same size"); 1959 1960 if (Offset < C.TLSSequenceOffset || 1961 (Offset - C.TLSSequenceOffset + C.NewCodeSequence.size()) > 1962 Section.getSize()) { 1963 // This can't be a matching sequence as it doesn't fit in the current 1964 // section 1965 continue; 1966 } 1967 1968 auto TLSSequenceStartOffset = Offset - C.TLSSequenceOffset; 1969 auto *TLSSequence = Section.getAddressWithOffset(TLSSequenceStartOffset); 1970 if (ArrayRef<uint8_t>(TLSSequence, C.ExpectedCodeSequence.size()) != 1971 C.ExpectedCodeSequence) { 1972 continue; 1973 } 1974 1975 memcpy(TLSSequence, C.NewCodeSequence.data(), C.NewCodeSequence.size()); 1976 1977 // The original GOTTPOFF relocation has an addend as it is PC relative, 1978 // so it needs to be corrected. The TPOFF32 relocation is used as an 1979 // absolute value (which is an offset from %fs:0), so remove the addend 1980 // again. 1981 RelocationEntry RE(SectionID, 1982 TLSSequenceStartOffset + C.TpoffRelocationOffset, 1983 ELF::R_X86_64_TPOFF32, Value.Addend - Addend); 1984 1985 if (Value.SymbolName) 1986 addRelocationForSymbol(RE, Value.SymbolName); 1987 else 1988 addRelocationForSection(RE, Value.SectionID); 1989 1990 Resolved = true; 1991 break; 1992 } 1993 1994 if (!Resolved) { 1995 // The GOTTPOFF relocation was not used in one of the sequences 1996 // described in the spec, so we can't optimize it to a TPOFF 1997 // relocation. 1998 uint64_t GOTOffset = allocateGOTEntries(1); 1999 resolveGOTOffsetRelocation(SectionID, Offset, GOTOffset + Addend, 2000 ELF::R_X86_64_PC32); 2001 RelocationEntry RE = 2002 computeGOTOffsetRE(GOTOffset, Value.Offset, ELF::R_X86_64_TPOFF64); 2003 if (Value.SymbolName) 2004 addRelocationForSymbol(RE, Value.SymbolName); 2005 else 2006 addRelocationForSection(RE, Value.SectionID); 2007 } 2008 } 2009 2010 void RuntimeDyldELF::processX86_64TLSRelocation( 2011 unsigned SectionID, uint64_t Offset, uint64_t RelType, 2012 RelocationValueRef Value, int64_t Addend, 2013 const RelocationRef &GetAddrRelocation) { 2014 // Since we are statically linking and have no additional DSOs, we can resolve 2015 // the relocation directly without using __tls_get_addr. 2016 // Use the approach from "x86-64 Linker Optimizations" from the TLS spec 2017 // to replace it with the Local Exec relocation variant. 2018 2019 // Find out whether the code was compiled with the large or small memory 2020 // model. For this we look at the next relocation which is the relocation 2021 // for the __tls_get_addr function. If it's a 32 bit relocation, it's the 2022 // small code model, with a 64 bit relocation it's the large code model. 2023 bool IsSmallCodeModel; 2024 // Is the relocation for the __tls_get_addr a PC-relative GOT relocation? 2025 bool IsGOTPCRel = false; 2026 2027 switch (GetAddrRelocation.getType()) { 2028 case ELF::R_X86_64_GOTPCREL: 2029 case ELF::R_X86_64_REX_GOTPCRELX: 2030 case ELF::R_X86_64_GOTPCRELX: 2031 IsGOTPCRel = true; 2032 [[fallthrough]]; 2033 case ELF::R_X86_64_PLT32: 2034 IsSmallCodeModel = true; 2035 break; 2036 case ELF::R_X86_64_PLTOFF64: 2037 IsSmallCodeModel = false; 2038 break; 2039 default: 2040 report_fatal_error( 2041 "invalid TLS relocations for General/Local Dynamic TLS Model: " 2042 "expected PLT or GOT relocation for __tls_get_addr function"); 2043 } 2044 2045 // The negative offset to the start of the TLS code sequence relative to 2046 // the offset of the TLSGD/TLSLD relocation 2047 uint64_t TLSSequenceOffset; 2048 // The expected start of the code sequence 2049 ArrayRef<uint8_t> ExpectedCodeSequence; 2050 // The new TLS code sequence that will replace the existing code 2051 ArrayRef<uint8_t> NewCodeSequence; 2052 2053 if (RelType == ELF::R_X86_64_TLSGD) { 2054 // The offset of the new TPOFF32 relocation (offset starting from the 2055 // beginning of the whole TLS sequence) 2056 uint64_t TpoffRelocOffset; 2057 2058 if (IsSmallCodeModel) { 2059 if (!IsGOTPCRel) { 2060 static const std::initializer_list<uint8_t> CodeSequence = { 2061 0x66, // data16 (no-op prefix) 2062 0x48, 0x8d, 0x3d, 0x00, 0x00, 2063 0x00, 0x00, // lea <disp32>(%rip), %rdi 2064 0x66, 0x66, // two data16 prefixes 2065 0x48, // rex64 (no-op prefix) 2066 0xe8, 0x00, 0x00, 0x00, 0x00 // call __tls_get_addr@plt 2067 }; 2068 ExpectedCodeSequence = ArrayRef<uint8_t>(CodeSequence); 2069 TLSSequenceOffset = 4; 2070 } else { 2071 // This code sequence is not described in the TLS spec but gcc 2072 // generates it sometimes. 2073 static const std::initializer_list<uint8_t> CodeSequence = { 2074 0x66, // data16 (no-op prefix) 2075 0x48, 0x8d, 0x3d, 0x00, 0x00, 2076 0x00, 0x00, // lea <disp32>(%rip), %rdi 2077 0x66, // data16 prefix (no-op prefix) 2078 0x48, // rex64 (no-op prefix) 2079 0xff, 0x15, 0x00, 0x00, 0x00, 2080 0x00 // call *__tls_get_addr@gotpcrel(%rip) 2081 }; 2082 ExpectedCodeSequence = ArrayRef<uint8_t>(CodeSequence); 2083 TLSSequenceOffset = 4; 2084 } 2085 2086 // The replacement code for the small code model. It's the same for 2087 // both sequences. 2088 static const std::initializer_list<uint8_t> SmallSequence = { 2089 0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00, 2090 0x00, // mov %fs:0, %rax 2091 0x48, 0x8d, 0x80, 0x00, 0x00, 0x00, 0x00 // lea x@tpoff(%rax), 2092 // %rax 2093 }; 2094 NewCodeSequence = ArrayRef<uint8_t>(SmallSequence); 2095 TpoffRelocOffset = 12; 2096 } else { 2097 static const std::initializer_list<uint8_t> CodeSequence = { 2098 0x48, 0x8d, 0x3d, 0x00, 0x00, 0x00, 0x00, // lea <disp32>(%rip), 2099 // %rdi 2100 0x48, 0xb8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 2101 0x00, // movabs $__tls_get_addr@pltoff, %rax 2102 0x48, 0x01, 0xd8, // add %rbx, %rax 2103 0xff, 0xd0 // call *%rax 2104 }; 2105 ExpectedCodeSequence = ArrayRef<uint8_t>(CodeSequence); 2106 TLSSequenceOffset = 3; 2107 2108 // The replacement code for the large code model 2109 static const std::initializer_list<uint8_t> LargeSequence = { 2110 0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00, 2111 0x00, // mov %fs:0, %rax 2112 0x48, 0x8d, 0x80, 0x00, 0x00, 0x00, 0x00, // lea x@tpoff(%rax), 2113 // %rax 2114 0x66, 0x0f, 0x1f, 0x44, 0x00, 0x00 // nopw 0x0(%rax,%rax,1) 2115 }; 2116 NewCodeSequence = ArrayRef<uint8_t>(LargeSequence); 2117 TpoffRelocOffset = 12; 2118 } 2119 2120 // The TLSGD/TLSLD relocations are PC-relative, so they have an addend. 2121 // The new TPOFF32 relocations is used as an absolute offset from 2122 // %fs:0, so remove the TLSGD/TLSLD addend again. 2123 RelocationEntry RE(SectionID, Offset - TLSSequenceOffset + TpoffRelocOffset, 2124 ELF::R_X86_64_TPOFF32, Value.Addend - Addend); 2125 if (Value.SymbolName) 2126 addRelocationForSymbol(RE, Value.SymbolName); 2127 else 2128 addRelocationForSection(RE, Value.SectionID); 2129 } else if (RelType == ELF::R_X86_64_TLSLD) { 2130 if (IsSmallCodeModel) { 2131 if (!IsGOTPCRel) { 2132 static const std::initializer_list<uint8_t> CodeSequence = { 2133 0x48, 0x8d, 0x3d, 0x00, 0x00, 0x00, // leaq <disp32>(%rip), %rdi 2134 0x00, 0xe8, 0x00, 0x00, 0x00, 0x00 // call __tls_get_addr@plt 2135 }; 2136 ExpectedCodeSequence = ArrayRef<uint8_t>(CodeSequence); 2137 TLSSequenceOffset = 3; 2138 2139 // The replacement code for the small code model 2140 static const std::initializer_list<uint8_t> SmallSequence = { 2141 0x66, 0x66, 0x66, // three data16 prefixes (no-op) 2142 0x64, 0x48, 0x8b, 0x04, 0x25, 2143 0x00, 0x00, 0x00, 0x00 // mov %fs:0, %rax 2144 }; 2145 NewCodeSequence = ArrayRef<uint8_t>(SmallSequence); 2146 } else { 2147 // This code sequence is not described in the TLS spec but gcc 2148 // generates it sometimes. 2149 static const std::initializer_list<uint8_t> CodeSequence = { 2150 0x48, 0x8d, 0x3d, 0x00, 2151 0x00, 0x00, 0x00, // leaq <disp32>(%rip), %rdi 2152 0xff, 0x15, 0x00, 0x00, 2153 0x00, 0x00 // call 2154 // *__tls_get_addr@gotpcrel(%rip) 2155 }; 2156 ExpectedCodeSequence = ArrayRef<uint8_t>(CodeSequence); 2157 TLSSequenceOffset = 3; 2158 2159 // The replacement is code is just like above but it needs to be 2160 // one byte longer. 2161 static const std::initializer_list<uint8_t> SmallSequence = { 2162 0x0f, 0x1f, 0x40, 0x00, // 4 byte nop 2163 0x64, 0x48, 0x8b, 0x04, 0x25, 2164 0x00, 0x00, 0x00, 0x00 // mov %fs:0, %rax 2165 }; 2166 NewCodeSequence = ArrayRef<uint8_t>(SmallSequence); 2167 } 2168 } else { 2169 // This is the same sequence as for the TLSGD sequence with the large 2170 // memory model above 2171 static const std::initializer_list<uint8_t> CodeSequence = { 2172 0x48, 0x8d, 0x3d, 0x00, 0x00, 0x00, 0x00, // lea <disp32>(%rip), 2173 // %rdi 2174 0x48, 0xb8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 2175 0x48, // movabs $__tls_get_addr@pltoff, %rax 2176 0x01, 0xd8, // add %rbx, %rax 2177 0xff, 0xd0 // call *%rax 2178 }; 2179 ExpectedCodeSequence = ArrayRef<uint8_t>(CodeSequence); 2180 TLSSequenceOffset = 3; 2181 2182 // The replacement code for the large code model 2183 static const std::initializer_list<uint8_t> LargeSequence = { 2184 0x66, 0x66, 0x66, // three data16 prefixes (no-op) 2185 0x66, 0x66, 0x0f, 0x1f, 0x84, 0x00, 0x00, 0x00, 0x00, 2186 0x00, // 10 byte nop 2187 0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00, 0x00 // mov %fs:0,%rax 2188 }; 2189 NewCodeSequence = ArrayRef<uint8_t>(LargeSequence); 2190 } 2191 } else { 2192 llvm_unreachable("both TLS relocations handled above"); 2193 } 2194 2195 assert(ExpectedCodeSequence.size() == NewCodeSequence.size() && 2196 "Old and new code sequences must have the same size"); 2197 2198 auto &Section = Sections[SectionID]; 2199 if (Offset < TLSSequenceOffset || 2200 (Offset - TLSSequenceOffset + NewCodeSequence.size()) > 2201 Section.getSize()) { 2202 report_fatal_error("unexpected end of section in TLS sequence"); 2203 } 2204 2205 auto *TLSSequence = Section.getAddressWithOffset(Offset - TLSSequenceOffset); 2206 if (ArrayRef<uint8_t>(TLSSequence, ExpectedCodeSequence.size()) != 2207 ExpectedCodeSequence) { 2208 report_fatal_error( 2209 "invalid TLS sequence for Global/Local Dynamic TLS Model"); 2210 } 2211 2212 memcpy(TLSSequence, NewCodeSequence.data(), NewCodeSequence.size()); 2213 } 2214 2215 size_t RuntimeDyldELF::getGOTEntrySize() { 2216 // We don't use the GOT in all of these cases, but it's essentially free 2217 // to put them all here. 2218 size_t Result = 0; 2219 switch (Arch) { 2220 case Triple::x86_64: 2221 case Triple::aarch64: 2222 case Triple::aarch64_be: 2223 case Triple::ppc64: 2224 case Triple::ppc64le: 2225 case Triple::systemz: 2226 Result = sizeof(uint64_t); 2227 break; 2228 case Triple::x86: 2229 case Triple::arm: 2230 case Triple::thumb: 2231 Result = sizeof(uint32_t); 2232 break; 2233 case Triple::mips: 2234 case Triple::mipsel: 2235 case Triple::mips64: 2236 case Triple::mips64el: 2237 if (IsMipsO32ABI || IsMipsN32ABI) 2238 Result = sizeof(uint32_t); 2239 else if (IsMipsN64ABI) 2240 Result = sizeof(uint64_t); 2241 else 2242 llvm_unreachable("Mips ABI not handled"); 2243 break; 2244 default: 2245 llvm_unreachable("Unsupported CPU type!"); 2246 } 2247 return Result; 2248 } 2249 2250 uint64_t RuntimeDyldELF::allocateGOTEntries(unsigned no) { 2251 if (GOTSectionID == 0) { 2252 GOTSectionID = Sections.size(); 2253 // Reserve a section id. We'll allocate the section later 2254 // once we know the total size 2255 Sections.push_back(SectionEntry(".got", nullptr, 0, 0, 0)); 2256 } 2257 uint64_t StartOffset = CurrentGOTIndex * getGOTEntrySize(); 2258 CurrentGOTIndex += no; 2259 return StartOffset; 2260 } 2261 2262 uint64_t RuntimeDyldELF::findOrAllocGOTEntry(const RelocationValueRef &Value, 2263 unsigned GOTRelType) { 2264 auto E = GOTOffsetMap.insert({Value, 0}); 2265 if (E.second) { 2266 uint64_t GOTOffset = allocateGOTEntries(1); 2267 2268 // Create relocation for newly created GOT entry 2269 RelocationEntry RE = 2270 computeGOTOffsetRE(GOTOffset, Value.Offset, GOTRelType); 2271 if (Value.SymbolName) 2272 addRelocationForSymbol(RE, Value.SymbolName); 2273 else 2274 addRelocationForSection(RE, Value.SectionID); 2275 2276 E.first->second = GOTOffset; 2277 } 2278 2279 return E.first->second; 2280 } 2281 2282 void RuntimeDyldELF::resolveGOTOffsetRelocation(unsigned SectionID, 2283 uint64_t Offset, 2284 uint64_t GOTOffset, 2285 uint32_t Type) { 2286 // Fill in the relative address of the GOT Entry into the stub 2287 RelocationEntry GOTRE(SectionID, Offset, Type, GOTOffset); 2288 addRelocationForSection(GOTRE, GOTSectionID); 2289 } 2290 2291 RelocationEntry RuntimeDyldELF::computeGOTOffsetRE(uint64_t GOTOffset, 2292 uint64_t SymbolOffset, 2293 uint32_t Type) { 2294 return RelocationEntry(GOTSectionID, GOTOffset, Type, SymbolOffset); 2295 } 2296 2297 void RuntimeDyldELF::processNewSymbol(const SymbolRef &ObjSymbol, SymbolTableEntry& Symbol) { 2298 // This should never return an error as `processNewSymbol` wouldn't have been 2299 // called if getFlags() returned an error before. 2300 auto ObjSymbolFlags = cantFail(ObjSymbol.getFlags()); 2301 2302 if (ObjSymbolFlags & SymbolRef::SF_Indirect) { 2303 if (IFuncStubSectionID == 0) { 2304 // Create a dummy section for the ifunc stubs. It will be actually 2305 // allocated in finalizeLoad() below. 2306 IFuncStubSectionID = Sections.size(); 2307 Sections.push_back( 2308 SectionEntry(".text.__llvm_IFuncStubs", nullptr, 0, 0, 0)); 2309 // First 64B are reserverd for the IFunc resolver 2310 IFuncStubOffset = 64; 2311 } 2312 2313 IFuncStubs.push_back(IFuncStub{IFuncStubOffset, Symbol}); 2314 // Modify the symbol so that it points to the ifunc stub instead of to the 2315 // resolver function. 2316 Symbol = SymbolTableEntry(IFuncStubSectionID, IFuncStubOffset, 2317 Symbol.getFlags()); 2318 IFuncStubOffset += getMaxIFuncStubSize(); 2319 } 2320 } 2321 2322 Error RuntimeDyldELF::finalizeLoad(const ObjectFile &Obj, 2323 ObjSectionToIDMap &SectionMap) { 2324 if (IsMipsO32ABI) 2325 if (!PendingRelocs.empty()) 2326 return make_error<RuntimeDyldError>("Can't find matching LO16 reloc"); 2327 2328 // Create the IFunc stubs if necessary. This must be done before processing 2329 // the GOT entries, as the IFunc stubs may create some. 2330 if (IFuncStubSectionID != 0) { 2331 uint8_t *IFuncStubsAddr = MemMgr.allocateCodeSection( 2332 IFuncStubOffset, 1, IFuncStubSectionID, ".text.__llvm_IFuncStubs"); 2333 if (!IFuncStubsAddr) 2334 return make_error<RuntimeDyldError>( 2335 "Unable to allocate memory for IFunc stubs!"); 2336 Sections[IFuncStubSectionID] = 2337 SectionEntry(".text.__llvm_IFuncStubs", IFuncStubsAddr, IFuncStubOffset, 2338 IFuncStubOffset, 0); 2339 2340 createIFuncResolver(IFuncStubsAddr); 2341 2342 LLVM_DEBUG(dbgs() << "Creating IFunc stubs SectionID: " 2343 << IFuncStubSectionID << " Addr: " 2344 << Sections[IFuncStubSectionID].getAddress() << '\n'); 2345 for (auto &IFuncStub : IFuncStubs) { 2346 auto &Symbol = IFuncStub.OriginalSymbol; 2347 LLVM_DEBUG(dbgs() << "\tSectionID: " << Symbol.getSectionID() 2348 << " Offset: " << format("%p", Symbol.getOffset()) 2349 << " IFuncStubOffset: " 2350 << format("%p\n", IFuncStub.StubOffset)); 2351 createIFuncStub(IFuncStubSectionID, 0, IFuncStub.StubOffset, 2352 Symbol.getSectionID(), Symbol.getOffset()); 2353 } 2354 2355 IFuncStubSectionID = 0; 2356 IFuncStubOffset = 0; 2357 IFuncStubs.clear(); 2358 } 2359 2360 // If necessary, allocate the global offset table 2361 if (GOTSectionID != 0) { 2362 // Allocate memory for the section 2363 size_t TotalSize = CurrentGOTIndex * getGOTEntrySize(); 2364 uint8_t *Addr = MemMgr.allocateDataSection(TotalSize, getGOTEntrySize(), 2365 GOTSectionID, ".got", false); 2366 if (!Addr) 2367 return make_error<RuntimeDyldError>("Unable to allocate memory for GOT!"); 2368 2369 Sections[GOTSectionID] = 2370 SectionEntry(".got", Addr, TotalSize, TotalSize, 0); 2371 2372 // For now, initialize all GOT entries to zero. We'll fill them in as 2373 // needed when GOT-based relocations are applied. 2374 memset(Addr, 0, TotalSize); 2375 if (IsMipsN32ABI || IsMipsN64ABI) { 2376 // To correctly resolve Mips GOT relocations, we need a mapping from 2377 // object's sections to GOTs. 2378 for (section_iterator SI = Obj.section_begin(), SE = Obj.section_end(); 2379 SI != SE; ++SI) { 2380 if (SI->relocation_begin() != SI->relocation_end()) { 2381 Expected<section_iterator> RelSecOrErr = SI->getRelocatedSection(); 2382 if (!RelSecOrErr) 2383 return make_error<RuntimeDyldError>( 2384 toString(RelSecOrErr.takeError())); 2385 2386 section_iterator RelocatedSection = *RelSecOrErr; 2387 ObjSectionToIDMap::iterator i = SectionMap.find(*RelocatedSection); 2388 assert(i != SectionMap.end()); 2389 SectionToGOTMap[i->second] = GOTSectionID; 2390 } 2391 } 2392 GOTSymbolOffsets.clear(); 2393 } 2394 } 2395 2396 // Look for and record the EH frame section. 2397 ObjSectionToIDMap::iterator i, e; 2398 for (i = SectionMap.begin(), e = SectionMap.end(); i != e; ++i) { 2399 const SectionRef &Section = i->first; 2400 2401 StringRef Name; 2402 Expected<StringRef> NameOrErr = Section.getName(); 2403 if (NameOrErr) 2404 Name = *NameOrErr; 2405 else 2406 consumeError(NameOrErr.takeError()); 2407 2408 if (Name == ".eh_frame") { 2409 UnregisteredEHFrameSections.push_back(i->second); 2410 break; 2411 } 2412 } 2413 2414 GOTOffsetMap.clear(); 2415 GOTSectionID = 0; 2416 CurrentGOTIndex = 0; 2417 2418 return Error::success(); 2419 } 2420 2421 bool RuntimeDyldELF::isCompatibleFile(const object::ObjectFile &Obj) const { 2422 return Obj.isELF(); 2423 } 2424 2425 void RuntimeDyldELF::createIFuncResolver(uint8_t *Addr) const { 2426 if (Arch == Triple::x86_64) { 2427 // The adddres of the GOT1 entry is in %r11, the GOT2 entry is in %r11+8 2428 // (see createIFuncStub() for details) 2429 // The following code first saves all registers that contain the original 2430 // function arguments as those registers are not saved by the resolver 2431 // function. %r11 is saved as well so that the GOT2 entry can be updated 2432 // afterwards. Then it calls the actual IFunc resolver function whose 2433 // address is stored in GOT2. After the resolver function returns, all 2434 // saved registers are restored and the return value is written to GOT1. 2435 // Finally, jump to the now resolved function. 2436 // clang-format off 2437 const uint8_t StubCode[] = { 2438 0x57, // push %rdi 2439 0x56, // push %rsi 2440 0x52, // push %rdx 2441 0x51, // push %rcx 2442 0x41, 0x50, // push %r8 2443 0x41, 0x51, // push %r9 2444 0x41, 0x53, // push %r11 2445 0x41, 0xff, 0x53, 0x08, // call *0x8(%r11) 2446 0x41, 0x5b, // pop %r11 2447 0x41, 0x59, // pop %r9 2448 0x41, 0x58, // pop %r8 2449 0x59, // pop %rcx 2450 0x5a, // pop %rdx 2451 0x5e, // pop %rsi 2452 0x5f, // pop %rdi 2453 0x49, 0x89, 0x03, // mov %rax,(%r11) 2454 0xff, 0xe0 // jmp *%rax 2455 }; 2456 // clang-format on 2457 static_assert(sizeof(StubCode) <= 64, 2458 "maximum size of the IFunc resolver is 64B"); 2459 memcpy(Addr, StubCode, sizeof(StubCode)); 2460 } else { 2461 report_fatal_error( 2462 "IFunc resolver is not supported for target architecture"); 2463 } 2464 } 2465 2466 void RuntimeDyldELF::createIFuncStub(unsigned IFuncStubSectionID, 2467 uint64_t IFuncResolverOffset, 2468 uint64_t IFuncStubOffset, 2469 unsigned IFuncSectionID, 2470 uint64_t IFuncOffset) { 2471 auto &IFuncStubSection = Sections[IFuncStubSectionID]; 2472 auto *Addr = IFuncStubSection.getAddressWithOffset(IFuncStubOffset); 2473 2474 if (Arch == Triple::x86_64) { 2475 // The first instruction loads a PC-relative address into %r11 which is a 2476 // GOT entry for this stub. This initially contains the address to the 2477 // IFunc resolver. We can use %r11 here as it's caller saved but not used 2478 // to pass any arguments. In fact, x86_64 ABI even suggests using %r11 for 2479 // code in the PLT. The IFunc resolver will use %r11 to update the GOT 2480 // entry. 2481 // 2482 // The next instruction just jumps to the address contained in the GOT 2483 // entry. As mentioned above, we do this two-step jump by first setting 2484 // %r11 so that the IFunc resolver has access to it. 2485 // 2486 // The IFunc resolver of course also needs to know the actual address of 2487 // the actual IFunc resolver function. This will be stored in a GOT entry 2488 // right next to the first one for this stub. So, the IFunc resolver will 2489 // be able to call it with %r11+8. 2490 // 2491 // In total, two adjacent GOT entries (+relocation) and one additional 2492 // relocation are required: 2493 // GOT1: Address of the IFunc resolver. 2494 // GOT2: Address of the IFunc resolver function. 2495 // IFuncStubOffset+3: 32-bit PC-relative address of GOT1. 2496 uint64_t GOT1 = allocateGOTEntries(2); 2497 uint64_t GOT2 = GOT1 + getGOTEntrySize(); 2498 2499 RelocationEntry RE1(GOTSectionID, GOT1, ELF::R_X86_64_64, 2500 IFuncResolverOffset, {}); 2501 addRelocationForSection(RE1, IFuncStubSectionID); 2502 RelocationEntry RE2(GOTSectionID, GOT2, ELF::R_X86_64_64, IFuncOffset, {}); 2503 addRelocationForSection(RE2, IFuncSectionID); 2504 2505 const uint8_t StubCode[] = { 2506 0x4c, 0x8d, 0x1d, 0x00, 0x00, 0x00, 0x00, // leaq 0x0(%rip),%r11 2507 0x41, 0xff, 0x23 // jmpq *(%r11) 2508 }; 2509 assert(sizeof(StubCode) <= getMaxIFuncStubSize() && 2510 "IFunc stub size must not exceed getMaxIFuncStubSize()"); 2511 memcpy(Addr, StubCode, sizeof(StubCode)); 2512 2513 // The PC-relative value starts 4 bytes from the end of the leaq 2514 // instruction, so the addend is -4. 2515 resolveGOTOffsetRelocation(IFuncStubSectionID, IFuncStubOffset + 3, 2516 GOT1 - 4, ELF::R_X86_64_PC32); 2517 } else { 2518 report_fatal_error("IFunc stub is not supported for target architecture"); 2519 } 2520 } 2521 2522 unsigned RuntimeDyldELF::getMaxIFuncStubSize() const { 2523 if (Arch == Triple::x86_64) { 2524 return 10; 2525 } 2526 return 0; 2527 } 2528 2529 bool RuntimeDyldELF::relocationNeedsGot(const RelocationRef &R) const { 2530 unsigned RelTy = R.getType(); 2531 if (Arch == Triple::aarch64 || Arch == Triple::aarch64_be) 2532 return RelTy == ELF::R_AARCH64_ADR_GOT_PAGE || 2533 RelTy == ELF::R_AARCH64_LD64_GOT_LO12_NC; 2534 2535 if (Arch == Triple::x86_64) 2536 return RelTy == ELF::R_X86_64_GOTPCREL || 2537 RelTy == ELF::R_X86_64_GOTPCRELX || 2538 RelTy == ELF::R_X86_64_GOT64 || 2539 RelTy == ELF::R_X86_64_REX_GOTPCRELX; 2540 return false; 2541 } 2542 2543 bool RuntimeDyldELF::relocationNeedsStub(const RelocationRef &R) const { 2544 if (Arch != Triple::x86_64) 2545 return true; // Conservative answer 2546 2547 switch (R.getType()) { 2548 default: 2549 return true; // Conservative answer 2550 2551 2552 case ELF::R_X86_64_GOTPCREL: 2553 case ELF::R_X86_64_GOTPCRELX: 2554 case ELF::R_X86_64_REX_GOTPCRELX: 2555 case ELF::R_X86_64_GOTPC64: 2556 case ELF::R_X86_64_GOT64: 2557 case ELF::R_X86_64_GOTOFF64: 2558 case ELF::R_X86_64_PC32: 2559 case ELF::R_X86_64_PC64: 2560 case ELF::R_X86_64_64: 2561 // We know that these reloation types won't need a stub function. This list 2562 // can be extended as needed. 2563 return false; 2564 } 2565 } 2566 2567 } // namespace llvm 2568