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