1 //===-- RuntimeDyldELF.cpp - Run-time dynamic linker for MC-JIT -*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // Implementation of ELF support for the MC-JIT runtime dynamic linker. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "RuntimeDyldELF.h" 15 #include "RuntimeDyldCheckerImpl.h" 16 #include "llvm/ADT/IntervalMap.h" 17 #include "llvm/ADT/STLExtras.h" 18 #include "llvm/ADT/StringRef.h" 19 #include "llvm/ADT/Triple.h" 20 #include "llvm/MC/MCStreamer.h" 21 #include "llvm/Object/ELFObjectFile.h" 22 #include "llvm/Object/ObjectFile.h" 23 #include "llvm/Support/ELF.h" 24 #include "llvm/Support/Endian.h" 25 #include "llvm/Support/MemoryBuffer.h" 26 #include "llvm/Support/TargetRegistry.h" 27 28 using namespace llvm; 29 using namespace llvm::object; 30 31 #define DEBUG_TYPE "dyld" 32 33 namespace { 34 35 template <class ELFT> class DyldELFObject : public ELFObjectFile<ELFT> { 36 LLVM_ELF_IMPORT_TYPES_ELFT(ELFT) 37 38 typedef Elf_Shdr_Impl<ELFT> Elf_Shdr; 39 typedef Elf_Sym_Impl<ELFT> Elf_Sym; 40 typedef Elf_Rel_Impl<ELFT, false> Elf_Rel; 41 typedef Elf_Rel_Impl<ELFT, true> Elf_Rela; 42 43 typedef Elf_Ehdr_Impl<ELFT> Elf_Ehdr; 44 45 typedef typename ELFDataTypeTypedefHelper<ELFT>::value_type addr_type; 46 47 public: 48 DyldELFObject(MemoryBufferRef Wrapper, std::error_code &ec); 49 50 void updateSectionAddress(const SectionRef &Sec, uint64_t Addr); 51 52 void updateSymbolAddress(const SymbolRef &SymRef, uint64_t Addr); 53 54 // Methods for type inquiry through isa, cast and dyn_cast 55 static inline bool classof(const Binary *v) { 56 return (isa<ELFObjectFile<ELFT>>(v) && 57 classof(cast<ELFObjectFile<ELFT>>(v))); 58 } 59 static inline bool classof(const ELFObjectFile<ELFT> *v) { 60 return v->isDyldType(); 61 } 62 }; 63 64 65 66 // The MemoryBuffer passed into this constructor is just a wrapper around the 67 // actual memory. Ultimately, the Binary parent class will take ownership of 68 // this MemoryBuffer object but not the underlying memory. 69 template <class ELFT> 70 DyldELFObject<ELFT>::DyldELFObject(MemoryBufferRef Wrapper, std::error_code &EC) 71 : ELFObjectFile<ELFT>(Wrapper, EC) { 72 this->isDyldELFObject = true; 73 } 74 75 template <class ELFT> 76 void DyldELFObject<ELFT>::updateSectionAddress(const SectionRef &Sec, 77 uint64_t Addr) { 78 DataRefImpl ShdrRef = Sec.getRawDataRefImpl(); 79 Elf_Shdr *shdr = 80 const_cast<Elf_Shdr *>(reinterpret_cast<const Elf_Shdr *>(ShdrRef.p)); 81 82 // This assumes the address passed in matches the target address bitness 83 // The template-based type cast handles everything else. 84 shdr->sh_addr = static_cast<addr_type>(Addr); 85 } 86 87 template <class ELFT> 88 void DyldELFObject<ELFT>::updateSymbolAddress(const SymbolRef &SymRef, 89 uint64_t Addr) { 90 91 Elf_Sym *sym = const_cast<Elf_Sym *>( 92 ELFObjectFile<ELFT>::getSymbol(SymRef.getRawDataRefImpl())); 93 94 // This assumes the address passed in matches the target address bitness 95 // The template-based type cast handles everything else. 96 sym->st_value = static_cast<addr_type>(Addr); 97 } 98 99 class LoadedELFObjectInfo final 100 : public RuntimeDyld::LoadedObjectInfoHelper<LoadedELFObjectInfo> { 101 public: 102 LoadedELFObjectInfo(RuntimeDyldImpl &RTDyld, ObjSectionToIDMap ObjSecToIDMap) 103 : LoadedObjectInfoHelper(RTDyld, std::move(ObjSecToIDMap)) {} 104 105 OwningBinary<ObjectFile> 106 getObjectForDebug(const ObjectFile &Obj) const override; 107 }; 108 109 template <typename ELFT> 110 std::unique_ptr<DyldELFObject<ELFT>> 111 createRTDyldELFObject(MemoryBufferRef Buffer, 112 const ObjectFile &SourceObject, 113 const LoadedELFObjectInfo &L, 114 std::error_code &ec) { 115 typedef typename ELFFile<ELFT>::Elf_Shdr Elf_Shdr; 116 typedef typename ELFDataTypeTypedefHelper<ELFT>::value_type addr_type; 117 118 std::unique_ptr<DyldELFObject<ELFT>> Obj = 119 llvm::make_unique<DyldELFObject<ELFT>>(Buffer, ec); 120 121 // Iterate over all sections in the object. 122 auto SI = SourceObject.section_begin(); 123 for (const auto &Sec : Obj->sections()) { 124 StringRef SectionName; 125 Sec.getName(SectionName); 126 if (SectionName != "") { 127 DataRefImpl ShdrRef = Sec.getRawDataRefImpl(); 128 Elf_Shdr *shdr = const_cast<Elf_Shdr *>( 129 reinterpret_cast<const Elf_Shdr *>(ShdrRef.p)); 130 131 if (uint64_t SecLoadAddr = L.getSectionLoadAddress(*SI)) { 132 // This assumes that the address passed in matches the target address 133 // bitness. The template-based type cast handles everything else. 134 shdr->sh_addr = static_cast<addr_type>(SecLoadAddr); 135 } 136 } 137 ++SI; 138 } 139 140 return Obj; 141 } 142 143 OwningBinary<ObjectFile> createELFDebugObject(const ObjectFile &Obj, 144 const LoadedELFObjectInfo &L) { 145 assert(Obj.isELF() && "Not an ELF object file."); 146 147 std::unique_ptr<MemoryBuffer> Buffer = 148 MemoryBuffer::getMemBufferCopy(Obj.getData(), Obj.getFileName()); 149 150 std::error_code ec; 151 152 std::unique_ptr<ObjectFile> DebugObj; 153 if (Obj.getBytesInAddress() == 4 && Obj.isLittleEndian()) { 154 typedef ELFType<support::little, false> ELF32LE; 155 DebugObj = createRTDyldELFObject<ELF32LE>(Buffer->getMemBufferRef(), Obj, L, 156 ec); 157 } else if (Obj.getBytesInAddress() == 4 && !Obj.isLittleEndian()) { 158 typedef ELFType<support::big, false> ELF32BE; 159 DebugObj = createRTDyldELFObject<ELF32BE>(Buffer->getMemBufferRef(), Obj, L, 160 ec); 161 } else if (Obj.getBytesInAddress() == 8 && !Obj.isLittleEndian()) { 162 typedef ELFType<support::big, true> ELF64BE; 163 DebugObj = createRTDyldELFObject<ELF64BE>(Buffer->getMemBufferRef(), Obj, L, 164 ec); 165 } else if (Obj.getBytesInAddress() == 8 && Obj.isLittleEndian()) { 166 typedef ELFType<support::little, true> ELF64LE; 167 DebugObj = createRTDyldELFObject<ELF64LE>(Buffer->getMemBufferRef(), Obj, L, 168 ec); 169 } else 170 llvm_unreachable("Unexpected ELF format"); 171 172 assert(!ec && "Could not construct copy ELF object file"); 173 174 return OwningBinary<ObjectFile>(std::move(DebugObj), std::move(Buffer)); 175 } 176 177 OwningBinary<ObjectFile> 178 LoadedELFObjectInfo::getObjectForDebug(const ObjectFile &Obj) const { 179 return createELFDebugObject(Obj, *this); 180 } 181 182 } // anonymous namespace 183 184 namespace llvm { 185 186 RuntimeDyldELF::RuntimeDyldELF(RuntimeDyld::MemoryManager &MemMgr, 187 JITSymbolResolver &Resolver) 188 : RuntimeDyldImpl(MemMgr, Resolver), GOTSectionID(0), CurrentGOTIndex(0) {} 189 RuntimeDyldELF::~RuntimeDyldELF() {} 190 191 void RuntimeDyldELF::registerEHFrames() { 192 for (int i = 0, e = UnregisteredEHFrameSections.size(); i != e; ++i) { 193 SID EHFrameSID = UnregisteredEHFrameSections[i]; 194 uint8_t *EHFrameAddr = Sections[EHFrameSID].getAddress(); 195 uint64_t EHFrameLoadAddr = Sections[EHFrameSID].getLoadAddress(); 196 size_t EHFrameSize = Sections[EHFrameSID].getSize(); 197 MemMgr.registerEHFrames(EHFrameAddr, EHFrameLoadAddr, EHFrameSize); 198 RegisteredEHFrameSections.push_back(EHFrameSID); 199 } 200 UnregisteredEHFrameSections.clear(); 201 } 202 203 void RuntimeDyldELF::deregisterEHFrames() { 204 for (int i = 0, e = RegisteredEHFrameSections.size(); i != e; ++i) { 205 SID EHFrameSID = RegisteredEHFrameSections[i]; 206 uint8_t *EHFrameAddr = Sections[EHFrameSID].getAddress(); 207 uint64_t EHFrameLoadAddr = Sections[EHFrameSID].getLoadAddress(); 208 size_t EHFrameSize = Sections[EHFrameSID].getSize(); 209 MemMgr.deregisterEHFrames(EHFrameAddr, EHFrameLoadAddr, EHFrameSize); 210 } 211 RegisteredEHFrameSections.clear(); 212 } 213 214 std::unique_ptr<RuntimeDyld::LoadedObjectInfo> 215 RuntimeDyldELF::loadObject(const object::ObjectFile &O) { 216 if (auto ObjSectionToIDOrErr = loadObjectImpl(O)) 217 return llvm::make_unique<LoadedELFObjectInfo>(*this, *ObjSectionToIDOrErr); 218 else { 219 HasError = true; 220 raw_string_ostream ErrStream(ErrorStr); 221 logAllUnhandledErrors(ObjSectionToIDOrErr.takeError(), ErrStream, ""); 222 return nullptr; 223 } 224 } 225 226 void RuntimeDyldELF::resolveX86_64Relocation(const SectionEntry &Section, 227 uint64_t Offset, uint64_t Value, 228 uint32_t Type, int64_t Addend, 229 uint64_t SymOffset) { 230 switch (Type) { 231 default: 232 llvm_unreachable("Relocation type not implemented yet!"); 233 break; 234 case ELF::R_X86_64_64: { 235 support::ulittle64_t::ref(Section.getAddressWithOffset(Offset)) = 236 Value + Addend; 237 DEBUG(dbgs() << "Writing " << format("%p", (Value + Addend)) << " at " 238 << format("%p\n", Section.getAddressWithOffset(Offset))); 239 break; 240 } 241 case ELF::R_X86_64_32: 242 case ELF::R_X86_64_32S: { 243 Value += Addend; 244 assert((Type == ELF::R_X86_64_32 && (Value <= UINT32_MAX)) || 245 (Type == ELF::R_X86_64_32S && 246 ((int64_t)Value <= INT32_MAX && (int64_t)Value >= INT32_MIN))); 247 uint32_t TruncatedAddr = (Value & 0xFFFFFFFF); 248 support::ulittle32_t::ref(Section.getAddressWithOffset(Offset)) = 249 TruncatedAddr; 250 DEBUG(dbgs() << "Writing " << format("%p", TruncatedAddr) << " at " 251 << format("%p\n", Section.getAddressWithOffset(Offset))); 252 break; 253 } 254 case ELF::R_X86_64_PC8: { 255 uint64_t FinalAddress = Section.getLoadAddressWithOffset(Offset); 256 int64_t RealOffset = Value + Addend - FinalAddress; 257 assert(isInt<8>(RealOffset)); 258 int8_t TruncOffset = (RealOffset & 0xFF); 259 Section.getAddress()[Offset] = TruncOffset; 260 break; 261 } 262 case ELF::R_X86_64_PC32: { 263 uint64_t FinalAddress = Section.getLoadAddressWithOffset(Offset); 264 int64_t RealOffset = Value + Addend - FinalAddress; 265 assert(isInt<32>(RealOffset)); 266 int32_t TruncOffset = (RealOffset & 0xFFFFFFFF); 267 support::ulittle32_t::ref(Section.getAddressWithOffset(Offset)) = 268 TruncOffset; 269 break; 270 } 271 case ELF::R_X86_64_PC64: { 272 uint64_t FinalAddress = Section.getLoadAddressWithOffset(Offset); 273 int64_t RealOffset = Value + Addend - FinalAddress; 274 support::ulittle64_t::ref(Section.getAddressWithOffset(Offset)) = 275 RealOffset; 276 break; 277 } 278 } 279 } 280 281 void RuntimeDyldELF::resolveX86Relocation(const SectionEntry &Section, 282 uint64_t Offset, uint32_t Value, 283 uint32_t Type, int32_t Addend) { 284 switch (Type) { 285 case ELF::R_386_32: { 286 support::ulittle32_t::ref(Section.getAddressWithOffset(Offset)) = 287 Value + Addend; 288 break; 289 } 290 case ELF::R_386_PC32: { 291 uint32_t FinalAddress = 292 Section.getLoadAddressWithOffset(Offset) & 0xFFFFFFFF; 293 uint32_t RealOffset = Value + Addend - FinalAddress; 294 support::ulittle32_t::ref(Section.getAddressWithOffset(Offset)) = 295 RealOffset; 296 break; 297 } 298 default: 299 // There are other relocation types, but it appears these are the 300 // only ones currently used by the LLVM ELF object writer 301 llvm_unreachable("Relocation type not implemented yet!"); 302 break; 303 } 304 } 305 306 void RuntimeDyldELF::resolveAArch64Relocation(const SectionEntry &Section, 307 uint64_t Offset, uint64_t Value, 308 uint32_t Type, int64_t Addend) { 309 uint32_t *TargetPtr = 310 reinterpret_cast<uint32_t *>(Section.getAddressWithOffset(Offset)); 311 uint64_t FinalAddress = Section.getLoadAddressWithOffset(Offset); 312 313 DEBUG(dbgs() << "resolveAArch64Relocation, LocalAddress: 0x" 314 << format("%llx", Section.getAddressWithOffset(Offset)) 315 << " FinalAddress: 0x" << format("%llx", FinalAddress) 316 << " Value: 0x" << format("%llx", Value) << " Type: 0x" 317 << format("%x", Type) << " Addend: 0x" << format("%llx", Addend) 318 << "\n"); 319 320 switch (Type) { 321 default: 322 llvm_unreachable("Relocation type not implemented yet!"); 323 break; 324 case ELF::R_AARCH64_ABS64: { 325 uint64_t *TargetPtr = 326 reinterpret_cast<uint64_t *>(Section.getAddressWithOffset(Offset)); 327 *TargetPtr = Value + Addend; 328 break; 329 } 330 case ELF::R_AARCH64_PREL32: { 331 uint64_t Result = Value + Addend - FinalAddress; 332 assert(static_cast<int64_t>(Result) >= INT32_MIN && 333 static_cast<int64_t>(Result) <= UINT32_MAX); 334 *TargetPtr = static_cast<uint32_t>(Result & 0xffffffffU); 335 break; 336 } 337 case ELF::R_AARCH64_CALL26: // fallthrough 338 case ELF::R_AARCH64_JUMP26: { 339 // Operation: S+A-P. Set Call or B immediate value to bits fff_fffc of the 340 // calculation. 341 uint64_t BranchImm = Value + Addend - FinalAddress; 342 343 // "Check that -2^27 <= result < 2^27". 344 assert(isInt<28>(BranchImm)); 345 346 // AArch64 code is emitted with .rela relocations. The data already in any 347 // bits affected by the relocation on entry is garbage. 348 *TargetPtr &= 0xfc000000U; 349 // Immediate goes in bits 25:0 of B and BL. 350 *TargetPtr |= static_cast<uint32_t>(BranchImm & 0xffffffcU) >> 2; 351 break; 352 } 353 case ELF::R_AARCH64_MOVW_UABS_G3: { 354 uint64_t Result = Value + Addend; 355 356 // AArch64 code is emitted with .rela relocations. The data already in any 357 // bits affected by the relocation on entry is garbage. 358 *TargetPtr &= 0xffe0001fU; 359 // Immediate goes in bits 20:5 of MOVZ/MOVK instruction 360 *TargetPtr |= Result >> (48 - 5); 361 // Shift must be "lsl #48", in bits 22:21 362 assert((*TargetPtr >> 21 & 0x3) == 3 && "invalid shift for relocation"); 363 break; 364 } 365 case ELF::R_AARCH64_MOVW_UABS_G2_NC: { 366 uint64_t Result = Value + Addend; 367 368 // AArch64 code is emitted with .rela relocations. The data already in any 369 // bits affected by the relocation on entry is garbage. 370 *TargetPtr &= 0xffe0001fU; 371 // Immediate goes in bits 20:5 of MOVZ/MOVK instruction 372 *TargetPtr |= ((Result & 0xffff00000000ULL) >> (32 - 5)); 373 // Shift must be "lsl #32", in bits 22:21 374 assert((*TargetPtr >> 21 & 0x3) == 2 && "invalid shift for relocation"); 375 break; 376 } 377 case ELF::R_AARCH64_MOVW_UABS_G1_NC: { 378 uint64_t Result = Value + Addend; 379 380 // AArch64 code is emitted with .rela relocations. The data already in any 381 // bits affected by the relocation on entry is garbage. 382 *TargetPtr &= 0xffe0001fU; 383 // Immediate goes in bits 20:5 of MOVZ/MOVK instruction 384 *TargetPtr |= ((Result & 0xffff0000U) >> (16 - 5)); 385 // Shift must be "lsl #16", in bits 22:2 386 assert((*TargetPtr >> 21 & 0x3) == 1 && "invalid shift for relocation"); 387 break; 388 } 389 case ELF::R_AARCH64_MOVW_UABS_G0_NC: { 390 uint64_t Result = Value + Addend; 391 392 // AArch64 code is emitted with .rela relocations. The data already in any 393 // bits affected by the relocation on entry is garbage. 394 *TargetPtr &= 0xffe0001fU; 395 // Immediate goes in bits 20:5 of MOVZ/MOVK instruction 396 *TargetPtr |= ((Result & 0xffffU) << 5); 397 // Shift must be "lsl #0", in bits 22:21. 398 assert((*TargetPtr >> 21 & 0x3) == 0 && "invalid shift for relocation"); 399 break; 400 } 401 case ELF::R_AARCH64_ADR_PREL_PG_HI21: { 402 // Operation: Page(S+A) - Page(P) 403 uint64_t Result = 404 ((Value + Addend) & ~0xfffULL) - (FinalAddress & ~0xfffULL); 405 406 // Check that -2^32 <= X < 2^32 407 assert(isInt<33>(Result) && "overflow check failed for relocation"); 408 409 // AArch64 code is emitted with .rela relocations. The data already in any 410 // bits affected by the relocation on entry is garbage. 411 *TargetPtr &= 0x9f00001fU; 412 // Immediate goes in bits 30:29 + 5:23 of ADRP instruction, taken 413 // from bits 32:12 of X. 414 *TargetPtr |= ((Result & 0x3000U) << (29 - 12)); 415 *TargetPtr |= ((Result & 0x1ffffc000ULL) >> (14 - 5)); 416 break; 417 } 418 case ELF::R_AARCH64_LDST32_ABS_LO12_NC: { 419 // Operation: S + A 420 uint64_t Result = Value + Addend; 421 422 // AArch64 code is emitted with .rela relocations. The data already in any 423 // bits affected by the relocation on entry is garbage. 424 *TargetPtr &= 0xffc003ffU; 425 // Immediate goes in bits 21:10 of LD/ST instruction, taken 426 // from bits 11:2 of X 427 *TargetPtr |= ((Result & 0xffc) << (10 - 2)); 428 break; 429 } 430 case ELF::R_AARCH64_LDST64_ABS_LO12_NC: { 431 // Operation: S + A 432 uint64_t Result = Value + Addend; 433 434 // AArch64 code is emitted with .rela relocations. The data already in any 435 // bits affected by the relocation on entry is garbage. 436 *TargetPtr &= 0xffc003ffU; 437 // Immediate goes in bits 21:10 of LD/ST instruction, taken 438 // from bits 11:3 of X 439 *TargetPtr |= ((Result & 0xff8) << (10 - 3)); 440 break; 441 } 442 } 443 } 444 445 void RuntimeDyldELF::resolveARMRelocation(const SectionEntry &Section, 446 uint64_t Offset, uint32_t Value, 447 uint32_t Type, int32_t Addend) { 448 // TODO: Add Thumb relocations. 449 uint32_t *TargetPtr = 450 reinterpret_cast<uint32_t *>(Section.getAddressWithOffset(Offset)); 451 uint32_t FinalAddress = Section.getLoadAddressWithOffset(Offset) & 0xFFFFFFFF; 452 Value += Addend; 453 454 DEBUG(dbgs() << "resolveARMRelocation, LocalAddress: " 455 << Section.getAddressWithOffset(Offset) 456 << " FinalAddress: " << format("%p", FinalAddress) << " Value: " 457 << format("%x", Value) << " Type: " << format("%x", Type) 458 << " Addend: " << format("%x", Addend) << "\n"); 459 460 switch (Type) { 461 default: 462 llvm_unreachable("Not implemented relocation type!"); 463 464 case ELF::R_ARM_NONE: 465 break; 466 // Write a 31bit signed offset 467 case ELF::R_ARM_PREL31: 468 support::ulittle32_t::ref{TargetPtr} = 469 (support::ulittle32_t::ref{TargetPtr} & 0x80000000) | 470 ((Value - FinalAddress) & ~0x80000000); 471 break; 472 case ELF::R_ARM_TARGET1: 473 case ELF::R_ARM_ABS32: 474 support::ulittle32_t::ref{TargetPtr} = Value; 475 break; 476 // Write first 16 bit of 32 bit value to the mov instruction. 477 // Last 4 bit should be shifted. 478 case ELF::R_ARM_MOVW_ABS_NC: 479 case ELF::R_ARM_MOVT_ABS: 480 if (Type == ELF::R_ARM_MOVW_ABS_NC) 481 Value = Value & 0xFFFF; 482 else if (Type == ELF::R_ARM_MOVT_ABS) 483 Value = (Value >> 16) & 0xFFFF; 484 support::ulittle32_t::ref{TargetPtr} = 485 (support::ulittle32_t::ref{TargetPtr} & ~0x000F0FFF) | (Value & 0xFFF) | 486 (((Value >> 12) & 0xF) << 16); 487 break; 488 // Write 24 bit relative value to the branch instruction. 489 case ELF::R_ARM_PC24: // Fall through. 490 case ELF::R_ARM_CALL: // Fall through. 491 case ELF::R_ARM_JUMP24: 492 int32_t RelValue = static_cast<int32_t>(Value - FinalAddress - 8); 493 RelValue = (RelValue & 0x03FFFFFC) >> 2; 494 assert((support::ulittle32_t::ref{TargetPtr} & 0xFFFFFF) == 0xFFFFFE); 495 support::ulittle32_t::ref{TargetPtr} = 496 (support::ulittle32_t::ref{TargetPtr} & 0xFF000000) | RelValue; 497 break; 498 } 499 } 500 501 void RuntimeDyldELF::resolveMIPSRelocation(const SectionEntry &Section, 502 uint64_t Offset, uint32_t Value, 503 uint32_t Type, int32_t Addend) { 504 uint8_t *TargetPtr = Section.getAddressWithOffset(Offset); 505 Value += Addend; 506 507 DEBUG(dbgs() << "resolveMIPSRelocation, LocalAddress: " 508 << Section.getAddressWithOffset(Offset) << " FinalAddress: " 509 << format("%p", Section.getLoadAddressWithOffset(Offset)) 510 << " Value: " << format("%x", Value) 511 << " Type: " << format("%x", Type) 512 << " Addend: " << format("%x", Addend) << "\n"); 513 514 Value = evaluateMIPS32Relocation(Section, Offset, Value, Type); 515 516 applyMIPSRelocation(TargetPtr, Value, Type); 517 } 518 519 void RuntimeDyldELF::setMipsABI(const ObjectFile &Obj) { 520 if (Arch == Triple::UnknownArch || 521 !StringRef(Triple::getArchTypePrefix(Arch)).equals("mips")) { 522 IsMipsO32ABI = false; 523 IsMipsN32ABI = false; 524 IsMipsN64ABI = false; 525 return; 526 } 527 unsigned AbiVariant; 528 Obj.getPlatformFlags(AbiVariant); 529 IsMipsO32ABI = AbiVariant & ELF::EF_MIPS_ABI_O32; 530 IsMipsN32ABI = AbiVariant & ELF::EF_MIPS_ABI2; 531 IsMipsN64ABI = Obj.getFileFormatName().equals("ELF64-mips"); 532 } 533 534 void RuntimeDyldELF::resolveMIPSN32Relocation(const SectionEntry &Section, 535 uint64_t Offset, uint64_t Value, 536 uint32_t Type, int64_t Addend, 537 uint64_t SymOffset, 538 SID SectionID) { 539 int64_t CalculatedValue = evaluateMIPS64Relocation( 540 Section, Offset, Value, Type, Addend, SymOffset, SectionID); 541 applyMIPSRelocation(Section.getAddressWithOffset(Offset), CalculatedValue, 542 Type); 543 } 544 545 void RuntimeDyldELF::resolveMIPSN64Relocation(const SectionEntry &Section, 546 uint64_t Offset, uint64_t Value, 547 uint32_t Type, int64_t Addend, 548 uint64_t SymOffset, 549 SID SectionID) { 550 uint32_t r_type = Type & 0xff; 551 uint32_t r_type2 = (Type >> 8) & 0xff; 552 uint32_t r_type3 = (Type >> 16) & 0xff; 553 554 // RelType is used to keep information for which relocation type we are 555 // applying relocation. 556 uint32_t RelType = r_type; 557 int64_t CalculatedValue = evaluateMIPS64Relocation(Section, Offset, Value, 558 RelType, Addend, 559 SymOffset, SectionID); 560 if (r_type2 != ELF::R_MIPS_NONE) { 561 RelType = r_type2; 562 CalculatedValue = evaluateMIPS64Relocation(Section, Offset, 0, RelType, 563 CalculatedValue, SymOffset, 564 SectionID); 565 } 566 if (r_type3 != ELF::R_MIPS_NONE) { 567 RelType = r_type3; 568 CalculatedValue = evaluateMIPS64Relocation(Section, Offset, 0, RelType, 569 CalculatedValue, SymOffset, 570 SectionID); 571 } 572 applyMIPSRelocation(Section.getAddressWithOffset(Offset), CalculatedValue, 573 RelType); 574 } 575 576 int64_t RuntimeDyldELF::evaluateMIPS32Relocation(const SectionEntry &Section, 577 uint64_t Offset, 578 uint64_t Value, 579 uint32_t Type) { 580 581 DEBUG(dbgs() << "evaluateMIPS32Relocation, LocalAddress: 0x" 582 << format("%llx", Section.getAddressWithOffset(Offset)) 583 << " FinalAddress: 0x" 584 << format("%llx", Section.getLoadAddressWithOffset(Offset)) 585 << " Value: 0x" << format("%llx", Value) << " Type: 0x" 586 << format("%x", Type) << "\n"); 587 588 switch (Type) { 589 default: 590 llvm_unreachable("Unknown relocation type!"); 591 return Value; 592 case ELF::R_MIPS_32: 593 return Value; 594 case ELF::R_MIPS_26: 595 return Value >> 2; 596 case ELF::R_MIPS_HI16: 597 // Get the higher 16-bits. Also add 1 if bit 15 is 1. 598 return (Value + 0x8000) >> 16; 599 case ELF::R_MIPS_LO16: 600 return Value; 601 case ELF::R_MIPS_PC32: { 602 uint32_t FinalAddress = Section.getLoadAddressWithOffset(Offset); 603 return Value - FinalAddress; 604 } 605 case ELF::R_MIPS_PC16: { 606 uint32_t FinalAddress = Section.getLoadAddressWithOffset(Offset); 607 return (Value - FinalAddress) >> 2; 608 } 609 case ELF::R_MIPS_PC19_S2: { 610 uint32_t FinalAddress = Section.getLoadAddressWithOffset(Offset); 611 return (Value - (FinalAddress & ~0x3)) >> 2; 612 } 613 case ELF::R_MIPS_PC21_S2: { 614 uint32_t FinalAddress = Section.getLoadAddressWithOffset(Offset); 615 return (Value - FinalAddress) >> 2; 616 } 617 case ELF::R_MIPS_PC26_S2: { 618 uint32_t FinalAddress = Section.getLoadAddressWithOffset(Offset); 619 return (Value - FinalAddress) >> 2; 620 } 621 case ELF::R_MIPS_PCHI16: { 622 uint32_t FinalAddress = Section.getLoadAddressWithOffset(Offset); 623 return (Value - FinalAddress + 0x8000) >> 16; 624 } 625 case ELF::R_MIPS_PCLO16: { 626 uint32_t FinalAddress = Section.getLoadAddressWithOffset(Offset); 627 return Value - FinalAddress; 628 } 629 } 630 } 631 632 int64_t 633 RuntimeDyldELF::evaluateMIPS64Relocation(const SectionEntry &Section, 634 uint64_t Offset, uint64_t Value, 635 uint32_t Type, int64_t Addend, 636 uint64_t SymOffset, SID SectionID) { 637 638 DEBUG(dbgs() << "evaluateMIPS64Relocation, LocalAddress: 0x" 639 << format("%llx", Section.getAddressWithOffset(Offset)) 640 << " FinalAddress: 0x" 641 << format("%llx", Section.getLoadAddressWithOffset(Offset)) 642 << " Value: 0x" << format("%llx", Value) << " Type: 0x" 643 << format("%x", Type) << " Addend: 0x" << format("%llx", Addend) 644 << " SymOffset: " << format("%x", SymOffset) << "\n"); 645 646 switch (Type) { 647 default: 648 llvm_unreachable("Not implemented relocation type!"); 649 break; 650 case ELF::R_MIPS_JALR: 651 case ELF::R_MIPS_NONE: 652 break; 653 case ELF::R_MIPS_32: 654 case ELF::R_MIPS_64: 655 return Value + Addend; 656 case ELF::R_MIPS_26: 657 return ((Value + Addend) >> 2) & 0x3ffffff; 658 case ELF::R_MIPS_GPREL16: { 659 uint64_t GOTAddr = getSectionLoadAddress(SectionToGOTMap[SectionID]); 660 return Value + Addend - (GOTAddr + 0x7ff0); 661 } 662 case ELF::R_MIPS_SUB: 663 return Value - Addend; 664 case ELF::R_MIPS_HI16: 665 // Get the higher 16-bits. Also add 1 if bit 15 is 1. 666 return ((Value + Addend + 0x8000) >> 16) & 0xffff; 667 case ELF::R_MIPS_LO16: 668 return (Value + Addend) & 0xffff; 669 case ELF::R_MIPS_CALL16: 670 case ELF::R_MIPS_GOT_DISP: 671 case ELF::R_MIPS_GOT_PAGE: { 672 uint8_t *LocalGOTAddr = 673 getSectionAddress(SectionToGOTMap[SectionID]) + SymOffset; 674 uint64_t GOTEntry = readBytesUnaligned(LocalGOTAddr, getGOTEntrySize()); 675 676 Value += Addend; 677 if (Type == ELF::R_MIPS_GOT_PAGE) 678 Value = (Value + 0x8000) & ~0xffff; 679 680 if (GOTEntry) 681 assert(GOTEntry == Value && 682 "GOT entry has two different addresses."); 683 else 684 writeBytesUnaligned(Value, LocalGOTAddr, getGOTEntrySize()); 685 686 return (SymOffset - 0x7ff0) & 0xffff; 687 } 688 case ELF::R_MIPS_GOT_OFST: { 689 int64_t page = (Value + Addend + 0x8000) & ~0xffff; 690 return (Value + Addend - page) & 0xffff; 691 } 692 case ELF::R_MIPS_GPREL32: { 693 uint64_t GOTAddr = getSectionLoadAddress(SectionToGOTMap[SectionID]); 694 return Value + Addend - (GOTAddr + 0x7ff0); 695 } 696 case ELF::R_MIPS_PC16: { 697 uint64_t FinalAddress = Section.getLoadAddressWithOffset(Offset); 698 return ((Value + Addend - FinalAddress) >> 2) & 0xffff; 699 } 700 case ELF::R_MIPS_PC32: { 701 uint64_t FinalAddress = Section.getLoadAddressWithOffset(Offset); 702 return Value + Addend - FinalAddress; 703 } 704 case ELF::R_MIPS_PC18_S3: { 705 uint64_t FinalAddress = Section.getLoadAddressWithOffset(Offset); 706 return ((Value + Addend - (FinalAddress & ~0x7)) >> 3) & 0x3ffff; 707 } 708 case ELF::R_MIPS_PC19_S2: { 709 uint64_t FinalAddress = Section.getLoadAddressWithOffset(Offset); 710 return ((Value + Addend - (FinalAddress & ~0x3)) >> 2) & 0x7ffff; 711 } 712 case ELF::R_MIPS_PC21_S2: { 713 uint64_t FinalAddress = Section.getLoadAddressWithOffset(Offset); 714 return ((Value + Addend - FinalAddress) >> 2) & 0x1fffff; 715 } 716 case ELF::R_MIPS_PC26_S2: { 717 uint64_t FinalAddress = Section.getLoadAddressWithOffset(Offset); 718 return ((Value + Addend - FinalAddress) >> 2) & 0x3ffffff; 719 } 720 case ELF::R_MIPS_PCHI16: { 721 uint64_t FinalAddress = Section.getLoadAddressWithOffset(Offset); 722 return ((Value + Addend - FinalAddress + 0x8000) >> 16) & 0xffff; 723 } 724 case ELF::R_MIPS_PCLO16: { 725 uint64_t FinalAddress = Section.getLoadAddressWithOffset(Offset); 726 return (Value + Addend - FinalAddress) & 0xffff; 727 } 728 } 729 return 0; 730 } 731 732 void RuntimeDyldELF::applyMIPSRelocation(uint8_t *TargetPtr, int64_t Value, 733 uint32_t Type) { 734 uint32_t Insn = readBytesUnaligned(TargetPtr, 4); 735 736 switch (Type) { 737 default: 738 llvm_unreachable("Unknown relocation type!"); 739 break; 740 case ELF::R_MIPS_GPREL16: 741 case ELF::R_MIPS_HI16: 742 case ELF::R_MIPS_LO16: 743 case ELF::R_MIPS_PC16: 744 case ELF::R_MIPS_PCHI16: 745 case ELF::R_MIPS_PCLO16: 746 case ELF::R_MIPS_CALL16: 747 case ELF::R_MIPS_GOT_DISP: 748 case ELF::R_MIPS_GOT_PAGE: 749 case ELF::R_MIPS_GOT_OFST: 750 Insn = (Insn & 0xffff0000) | (Value & 0x0000ffff); 751 writeBytesUnaligned(Insn, TargetPtr, 4); 752 break; 753 case ELF::R_MIPS_PC18_S3: 754 Insn = (Insn & 0xfffc0000) | (Value & 0x0003ffff); 755 writeBytesUnaligned(Insn, TargetPtr, 4); 756 break; 757 case ELF::R_MIPS_PC19_S2: 758 Insn = (Insn & 0xfff80000) | (Value & 0x0007ffff); 759 writeBytesUnaligned(Insn, TargetPtr, 4); 760 break; 761 case ELF::R_MIPS_PC21_S2: 762 Insn = (Insn & 0xffe00000) | (Value & 0x001fffff); 763 writeBytesUnaligned(Insn, TargetPtr, 4); 764 break; 765 case ELF::R_MIPS_26: 766 case ELF::R_MIPS_PC26_S2: 767 Insn = (Insn & 0xfc000000) | (Value & 0x03ffffff); 768 writeBytesUnaligned(Insn, TargetPtr, 4); 769 break; 770 case ELF::R_MIPS_32: 771 case ELF::R_MIPS_GPREL32: 772 case ELF::R_MIPS_PC32: 773 writeBytesUnaligned(Value & 0xffffffff, TargetPtr, 4); 774 break; 775 case ELF::R_MIPS_64: 776 case ELF::R_MIPS_SUB: 777 writeBytesUnaligned(Value, TargetPtr, 8); 778 break; 779 } 780 } 781 782 // Return the .TOC. section and offset. 783 Error RuntimeDyldELF::findPPC64TOCSection(const ELFObjectFileBase &Obj, 784 ObjSectionToIDMap &LocalSections, 785 RelocationValueRef &Rel) { 786 // Set a default SectionID in case we do not find a TOC section below. 787 // This may happen for references to TOC base base (sym@toc, .odp 788 // relocation) without a .toc directive. In this case just use the 789 // first section (which is usually the .odp) since the code won't 790 // reference the .toc base directly. 791 Rel.SymbolName = nullptr; 792 Rel.SectionID = 0; 793 794 // The TOC consists of sections .got, .toc, .tocbss, .plt in that 795 // order. The TOC starts where the first of these sections starts. 796 for (auto &Section: Obj.sections()) { 797 StringRef SectionName; 798 if (auto EC = Section.getName(SectionName)) 799 return errorCodeToError(EC); 800 801 if (SectionName == ".got" 802 || SectionName == ".toc" 803 || SectionName == ".tocbss" 804 || SectionName == ".plt") { 805 if (auto SectionIDOrErr = 806 findOrEmitSection(Obj, Section, false, LocalSections)) 807 Rel.SectionID = *SectionIDOrErr; 808 else 809 return SectionIDOrErr.takeError(); 810 break; 811 } 812 } 813 814 // Per the ppc64-elf-linux ABI, The TOC base is TOC value plus 0x8000 815 // thus permitting a full 64 Kbytes segment. 816 Rel.Addend = 0x8000; 817 818 return Error::success(); 819 } 820 821 // Returns the sections and offset associated with the ODP entry referenced 822 // by Symbol. 823 Error RuntimeDyldELF::findOPDEntrySection(const ELFObjectFileBase &Obj, 824 ObjSectionToIDMap &LocalSections, 825 RelocationValueRef &Rel) { 826 // Get the ELF symbol value (st_value) to compare with Relocation offset in 827 // .opd entries 828 for (section_iterator si = Obj.section_begin(), se = Obj.section_end(); 829 si != se; ++si) { 830 section_iterator RelSecI = si->getRelocatedSection(); 831 if (RelSecI == Obj.section_end()) 832 continue; 833 834 StringRef RelSectionName; 835 if (auto EC = RelSecI->getName(RelSectionName)) 836 return errorCodeToError(EC); 837 838 if (RelSectionName != ".opd") 839 continue; 840 841 for (elf_relocation_iterator i = si->relocation_begin(), 842 e = si->relocation_end(); 843 i != e;) { 844 // The R_PPC64_ADDR64 relocation indicates the first field 845 // of a .opd entry 846 uint64_t TypeFunc = i->getType(); 847 if (TypeFunc != ELF::R_PPC64_ADDR64) { 848 ++i; 849 continue; 850 } 851 852 uint64_t TargetSymbolOffset = i->getOffset(); 853 symbol_iterator TargetSymbol = i->getSymbol(); 854 int64_t Addend; 855 if (auto AddendOrErr = i->getAddend()) 856 Addend = *AddendOrErr; 857 else 858 return errorCodeToError(AddendOrErr.getError()); 859 860 ++i; 861 if (i == e) 862 break; 863 864 // Just check if following relocation is a R_PPC64_TOC 865 uint64_t TypeTOC = i->getType(); 866 if (TypeTOC != ELF::R_PPC64_TOC) 867 continue; 868 869 // Finally compares the Symbol value and the target symbol offset 870 // to check if this .opd entry refers to the symbol the relocation 871 // points to. 872 if (Rel.Addend != (int64_t)TargetSymbolOffset) 873 continue; 874 875 section_iterator TSI = Obj.section_end(); 876 if (auto TSIOrErr = TargetSymbol->getSection()) 877 TSI = *TSIOrErr; 878 else 879 return TSIOrErr.takeError(); 880 assert(TSI != Obj.section_end() && "TSI should refer to a valid section"); 881 882 bool IsCode = TSI->isText(); 883 if (auto SectionIDOrErr = findOrEmitSection(Obj, *TSI, IsCode, 884 LocalSections)) 885 Rel.SectionID = *SectionIDOrErr; 886 else 887 return SectionIDOrErr.takeError(); 888 Rel.Addend = (intptr_t)Addend; 889 return Error::success(); 890 } 891 } 892 llvm_unreachable("Attempting to get address of ODP entry!"); 893 } 894 895 // Relocation masks following the #lo(value), #hi(value), #ha(value), 896 // #higher(value), #highera(value), #highest(value), and #highesta(value) 897 // macros defined in section 4.5.1. Relocation Types of the PPC-elf64abi 898 // document. 899 900 static inline uint16_t applyPPClo(uint64_t value) { return value & 0xffff; } 901 902 static inline uint16_t applyPPChi(uint64_t value) { 903 return (value >> 16) & 0xffff; 904 } 905 906 static inline uint16_t applyPPCha (uint64_t value) { 907 return ((value + 0x8000) >> 16) & 0xffff; 908 } 909 910 static inline uint16_t applyPPChigher(uint64_t value) { 911 return (value >> 32) & 0xffff; 912 } 913 914 static inline uint16_t applyPPChighera (uint64_t value) { 915 return ((value + 0x8000) >> 32) & 0xffff; 916 } 917 918 static inline uint16_t applyPPChighest(uint64_t value) { 919 return (value >> 48) & 0xffff; 920 } 921 922 static inline uint16_t applyPPChighesta (uint64_t value) { 923 return ((value + 0x8000) >> 48) & 0xffff; 924 } 925 926 void RuntimeDyldELF::resolvePPC32Relocation(const SectionEntry &Section, 927 uint64_t Offset, uint64_t Value, 928 uint32_t Type, int64_t Addend) { 929 uint8_t *LocalAddress = Section.getAddressWithOffset(Offset); 930 switch (Type) { 931 default: 932 llvm_unreachable("Relocation type not implemented yet!"); 933 break; 934 case ELF::R_PPC_ADDR16_LO: 935 writeInt16BE(LocalAddress, applyPPClo(Value + Addend)); 936 break; 937 case ELF::R_PPC_ADDR16_HI: 938 writeInt16BE(LocalAddress, applyPPChi(Value + Addend)); 939 break; 940 case ELF::R_PPC_ADDR16_HA: 941 writeInt16BE(LocalAddress, applyPPCha(Value + Addend)); 942 break; 943 } 944 } 945 946 void RuntimeDyldELF::resolvePPC64Relocation(const SectionEntry &Section, 947 uint64_t Offset, uint64_t Value, 948 uint32_t Type, int64_t Addend) { 949 uint8_t *LocalAddress = Section.getAddressWithOffset(Offset); 950 switch (Type) { 951 default: 952 llvm_unreachable("Relocation type not implemented yet!"); 953 break; 954 case ELF::R_PPC64_ADDR16: 955 writeInt16BE(LocalAddress, applyPPClo(Value + Addend)); 956 break; 957 case ELF::R_PPC64_ADDR16_DS: 958 writeInt16BE(LocalAddress, applyPPClo(Value + Addend) & ~3); 959 break; 960 case ELF::R_PPC64_ADDR16_LO: 961 writeInt16BE(LocalAddress, applyPPClo(Value + Addend)); 962 break; 963 case ELF::R_PPC64_ADDR16_LO_DS: 964 writeInt16BE(LocalAddress, applyPPClo(Value + Addend) & ~3); 965 break; 966 case ELF::R_PPC64_ADDR16_HI: 967 writeInt16BE(LocalAddress, applyPPChi(Value + Addend)); 968 break; 969 case ELF::R_PPC64_ADDR16_HA: 970 writeInt16BE(LocalAddress, applyPPCha(Value + Addend)); 971 break; 972 case ELF::R_PPC64_ADDR16_HIGHER: 973 writeInt16BE(LocalAddress, applyPPChigher(Value + Addend)); 974 break; 975 case ELF::R_PPC64_ADDR16_HIGHERA: 976 writeInt16BE(LocalAddress, applyPPChighera(Value + Addend)); 977 break; 978 case ELF::R_PPC64_ADDR16_HIGHEST: 979 writeInt16BE(LocalAddress, applyPPChighest(Value + Addend)); 980 break; 981 case ELF::R_PPC64_ADDR16_HIGHESTA: 982 writeInt16BE(LocalAddress, applyPPChighesta(Value + Addend)); 983 break; 984 case ELF::R_PPC64_ADDR14: { 985 assert(((Value + Addend) & 3) == 0); 986 // Preserve the AA/LK bits in the branch instruction 987 uint8_t aalk = *(LocalAddress + 3); 988 writeInt16BE(LocalAddress + 2, (aalk & 3) | ((Value + Addend) & 0xfffc)); 989 } break; 990 case ELF::R_PPC64_REL16_LO: { 991 uint64_t FinalAddress = Section.getLoadAddressWithOffset(Offset); 992 uint64_t Delta = Value - FinalAddress + Addend; 993 writeInt16BE(LocalAddress, applyPPClo(Delta)); 994 } break; 995 case ELF::R_PPC64_REL16_HI: { 996 uint64_t FinalAddress = Section.getLoadAddressWithOffset(Offset); 997 uint64_t Delta = Value - FinalAddress + Addend; 998 writeInt16BE(LocalAddress, applyPPChi(Delta)); 999 } break; 1000 case ELF::R_PPC64_REL16_HA: { 1001 uint64_t FinalAddress = Section.getLoadAddressWithOffset(Offset); 1002 uint64_t Delta = Value - FinalAddress + Addend; 1003 writeInt16BE(LocalAddress, applyPPCha(Delta)); 1004 } break; 1005 case ELF::R_PPC64_ADDR32: { 1006 int32_t Result = static_cast<int32_t>(Value + Addend); 1007 if (SignExtend32<32>(Result) != Result) 1008 llvm_unreachable("Relocation R_PPC64_ADDR32 overflow"); 1009 writeInt32BE(LocalAddress, Result); 1010 } break; 1011 case ELF::R_PPC64_REL24: { 1012 uint64_t FinalAddress = Section.getLoadAddressWithOffset(Offset); 1013 int32_t delta = static_cast<int32_t>(Value - FinalAddress + Addend); 1014 if (SignExtend32<26>(delta) != delta) 1015 llvm_unreachable("Relocation R_PPC64_REL24 overflow"); 1016 // Generates a 'bl <address>' instruction 1017 writeInt32BE(LocalAddress, 0x48000001 | (delta & 0x03FFFFFC)); 1018 } break; 1019 case ELF::R_PPC64_REL32: { 1020 uint64_t FinalAddress = Section.getLoadAddressWithOffset(Offset); 1021 int32_t delta = static_cast<int32_t>(Value - FinalAddress + Addend); 1022 if (SignExtend32<32>(delta) != delta) 1023 llvm_unreachable("Relocation R_PPC64_REL32 overflow"); 1024 writeInt32BE(LocalAddress, delta); 1025 } break; 1026 case ELF::R_PPC64_REL64: { 1027 uint64_t FinalAddress = Section.getLoadAddressWithOffset(Offset); 1028 uint64_t Delta = Value - FinalAddress + Addend; 1029 writeInt64BE(LocalAddress, Delta); 1030 } break; 1031 case ELF::R_PPC64_ADDR64: 1032 writeInt64BE(LocalAddress, Value + Addend); 1033 break; 1034 } 1035 } 1036 1037 void RuntimeDyldELF::resolveSystemZRelocation(const SectionEntry &Section, 1038 uint64_t Offset, uint64_t Value, 1039 uint32_t Type, int64_t Addend) { 1040 uint8_t *LocalAddress = Section.getAddressWithOffset(Offset); 1041 switch (Type) { 1042 default: 1043 llvm_unreachable("Relocation type not implemented yet!"); 1044 break; 1045 case ELF::R_390_PC16DBL: 1046 case ELF::R_390_PLT16DBL: { 1047 int64_t Delta = (Value + Addend) - Section.getLoadAddressWithOffset(Offset); 1048 assert(int16_t(Delta / 2) * 2 == Delta && "R_390_PC16DBL overflow"); 1049 writeInt16BE(LocalAddress, Delta / 2); 1050 break; 1051 } 1052 case ELF::R_390_PC32DBL: 1053 case ELF::R_390_PLT32DBL: { 1054 int64_t Delta = (Value + Addend) - Section.getLoadAddressWithOffset(Offset); 1055 assert(int32_t(Delta / 2) * 2 == Delta && "R_390_PC32DBL overflow"); 1056 writeInt32BE(LocalAddress, Delta / 2); 1057 break; 1058 } 1059 case ELF::R_390_PC32: { 1060 int64_t Delta = (Value + Addend) - Section.getLoadAddressWithOffset(Offset); 1061 assert(int32_t(Delta) == Delta && "R_390_PC32 overflow"); 1062 writeInt32BE(LocalAddress, Delta); 1063 break; 1064 } 1065 case ELF::R_390_64: 1066 writeInt64BE(LocalAddress, Value + Addend); 1067 break; 1068 case ELF::R_390_PC64: { 1069 int64_t Delta = (Value + Addend) - Section.getLoadAddressWithOffset(Offset); 1070 writeInt64BE(LocalAddress, Delta); 1071 break; 1072 } 1073 } 1074 } 1075 1076 // The target location for the relocation is described by RE.SectionID and 1077 // RE.Offset. RE.SectionID can be used to find the SectionEntry. Each 1078 // SectionEntry has three members describing its location. 1079 // SectionEntry::Address is the address at which the section has been loaded 1080 // into memory in the current (host) process. SectionEntry::LoadAddress is the 1081 // address that the section will have in the target process. 1082 // SectionEntry::ObjAddress is the address of the bits for this section in the 1083 // original emitted object image (also in the current address space). 1084 // 1085 // Relocations will be applied as if the section were loaded at 1086 // SectionEntry::LoadAddress, but they will be applied at an address based 1087 // on SectionEntry::Address. SectionEntry::ObjAddress will be used to refer to 1088 // Target memory contents if they are required for value calculations. 1089 // 1090 // The Value parameter here is the load address of the symbol for the 1091 // relocation to be applied. For relocations which refer to symbols in the 1092 // current object Value will be the LoadAddress of the section in which 1093 // the symbol resides (RE.Addend provides additional information about the 1094 // symbol location). For external symbols, Value will be the address of the 1095 // symbol in the target address space. 1096 void RuntimeDyldELF::resolveRelocation(const RelocationEntry &RE, 1097 uint64_t Value) { 1098 const SectionEntry &Section = Sections[RE.SectionID]; 1099 return resolveRelocation(Section, RE.Offset, Value, RE.RelType, RE.Addend, 1100 RE.SymOffset, RE.SectionID); 1101 } 1102 1103 void RuntimeDyldELF::resolveRelocation(const SectionEntry &Section, 1104 uint64_t Offset, uint64_t Value, 1105 uint32_t Type, int64_t Addend, 1106 uint64_t SymOffset, SID SectionID) { 1107 switch (Arch) { 1108 case Triple::x86_64: 1109 resolveX86_64Relocation(Section, Offset, Value, Type, Addend, SymOffset); 1110 break; 1111 case Triple::x86: 1112 resolveX86Relocation(Section, Offset, (uint32_t)(Value & 0xffffffffL), Type, 1113 (uint32_t)(Addend & 0xffffffffL)); 1114 break; 1115 case Triple::aarch64: 1116 case Triple::aarch64_be: 1117 resolveAArch64Relocation(Section, Offset, Value, Type, Addend); 1118 break; 1119 case Triple::arm: // Fall through. 1120 case Triple::armeb: 1121 case Triple::thumb: 1122 case Triple::thumbeb: 1123 resolveARMRelocation(Section, Offset, (uint32_t)(Value & 0xffffffffL), Type, 1124 (uint32_t)(Addend & 0xffffffffL)); 1125 break; 1126 case Triple::mips: // Fall through. 1127 case Triple::mipsel: 1128 case Triple::mips64: 1129 case Triple::mips64el: 1130 if (IsMipsO32ABI) 1131 resolveMIPSRelocation(Section, Offset, (uint32_t)(Value & 0xffffffffL), 1132 Type, (uint32_t)(Addend & 0xffffffffL)); 1133 else if (IsMipsN32ABI) 1134 resolveMIPSN32Relocation(Section, Offset, Value, Type, Addend, SymOffset, 1135 SectionID); 1136 else if (IsMipsN64ABI) 1137 resolveMIPSN64Relocation(Section, Offset, Value, Type, Addend, SymOffset, 1138 SectionID); 1139 else 1140 llvm_unreachable("Mips ABI not handled"); 1141 break; 1142 case Triple::ppc: 1143 resolvePPC32Relocation(Section, Offset, Value, Type, Addend); 1144 break; 1145 case Triple::ppc64: // Fall through. 1146 case Triple::ppc64le: 1147 resolvePPC64Relocation(Section, Offset, Value, Type, Addend); 1148 break; 1149 case Triple::systemz: 1150 resolveSystemZRelocation(Section, Offset, Value, Type, Addend); 1151 break; 1152 default: 1153 llvm_unreachable("Unsupported CPU type!"); 1154 } 1155 } 1156 1157 void *RuntimeDyldELF::computePlaceholderAddress(unsigned SectionID, uint64_t Offset) const { 1158 return (void *)(Sections[SectionID].getObjAddress() + Offset); 1159 } 1160 1161 void RuntimeDyldELF::processSimpleRelocation(unsigned SectionID, uint64_t Offset, unsigned RelType, RelocationValueRef Value) { 1162 RelocationEntry RE(SectionID, Offset, RelType, Value.Addend, Value.Offset); 1163 if (Value.SymbolName) 1164 addRelocationForSymbol(RE, Value.SymbolName); 1165 else 1166 addRelocationForSection(RE, Value.SectionID); 1167 } 1168 1169 uint32_t RuntimeDyldELF::getMatchingLoRelocation(uint32_t RelType, 1170 bool IsLocal) const { 1171 switch (RelType) { 1172 case ELF::R_MICROMIPS_GOT16: 1173 if (IsLocal) 1174 return ELF::R_MICROMIPS_LO16; 1175 break; 1176 case ELF::R_MICROMIPS_HI16: 1177 return ELF::R_MICROMIPS_LO16; 1178 case ELF::R_MIPS_GOT16: 1179 if (IsLocal) 1180 return ELF::R_MIPS_LO16; 1181 break; 1182 case ELF::R_MIPS_HI16: 1183 return ELF::R_MIPS_LO16; 1184 case ELF::R_MIPS_PCHI16: 1185 return ELF::R_MIPS_PCLO16; 1186 default: 1187 break; 1188 } 1189 return ELF::R_MIPS_NONE; 1190 } 1191 1192 Expected<relocation_iterator> 1193 RuntimeDyldELF::processRelocationRef( 1194 unsigned SectionID, relocation_iterator RelI, const ObjectFile &O, 1195 ObjSectionToIDMap &ObjSectionToID, StubMap &Stubs) { 1196 const auto &Obj = cast<ELFObjectFileBase>(O); 1197 uint64_t RelType = RelI->getType(); 1198 ErrorOr<int64_t> AddendOrErr = ELFRelocationRef(*RelI).getAddend(); 1199 int64_t Addend = AddendOrErr ? *AddendOrErr : 0; 1200 elf_symbol_iterator Symbol = RelI->getSymbol(); 1201 1202 // Obtain the symbol name which is referenced in the relocation 1203 StringRef TargetName; 1204 if (Symbol != Obj.symbol_end()) { 1205 if (auto TargetNameOrErr = Symbol->getName()) 1206 TargetName = *TargetNameOrErr; 1207 else 1208 return TargetNameOrErr.takeError(); 1209 } 1210 DEBUG(dbgs() << "\t\tRelType: " << RelType << " Addend: " << Addend 1211 << " TargetName: " << TargetName << "\n"); 1212 RelocationValueRef Value; 1213 // First search for the symbol in the local symbol table 1214 SymbolRef::Type SymType = SymbolRef::ST_Unknown; 1215 1216 // Search for the symbol in the global symbol table 1217 RTDyldSymbolTable::const_iterator gsi = GlobalSymbolTable.end(); 1218 if (Symbol != Obj.symbol_end()) { 1219 gsi = GlobalSymbolTable.find(TargetName.data()); 1220 Expected<SymbolRef::Type> SymTypeOrErr = Symbol->getType(); 1221 if (!SymTypeOrErr) { 1222 std::string Buf; 1223 raw_string_ostream OS(Buf); 1224 logAllUnhandledErrors(SymTypeOrErr.takeError(), OS, ""); 1225 OS.flush(); 1226 report_fatal_error(Buf); 1227 } 1228 SymType = *SymTypeOrErr; 1229 } 1230 if (gsi != GlobalSymbolTable.end()) { 1231 const auto &SymInfo = gsi->second; 1232 Value.SectionID = SymInfo.getSectionID(); 1233 Value.Offset = SymInfo.getOffset(); 1234 Value.Addend = SymInfo.getOffset() + Addend; 1235 } else { 1236 switch (SymType) { 1237 case SymbolRef::ST_Debug: { 1238 // TODO: Now ELF SymbolRef::ST_Debug = STT_SECTION, it's not obviously 1239 // and can be changed by another developers. Maybe best way is add 1240 // a new symbol type ST_Section to SymbolRef and use it. 1241 auto SectionOrErr = Symbol->getSection(); 1242 if (!SectionOrErr) { 1243 std::string Buf; 1244 raw_string_ostream OS(Buf); 1245 logAllUnhandledErrors(SectionOrErr.takeError(), OS, ""); 1246 OS.flush(); 1247 report_fatal_error(Buf); 1248 } 1249 section_iterator si = *SectionOrErr; 1250 if (si == Obj.section_end()) 1251 llvm_unreachable("Symbol section not found, bad object file format!"); 1252 DEBUG(dbgs() << "\t\tThis is section symbol\n"); 1253 bool isCode = si->isText(); 1254 if (auto SectionIDOrErr = findOrEmitSection(Obj, (*si), isCode, 1255 ObjSectionToID)) 1256 Value.SectionID = *SectionIDOrErr; 1257 else 1258 return SectionIDOrErr.takeError(); 1259 Value.Addend = Addend; 1260 break; 1261 } 1262 case SymbolRef::ST_Data: 1263 case SymbolRef::ST_Function: 1264 case SymbolRef::ST_Unknown: { 1265 Value.SymbolName = TargetName.data(); 1266 Value.Addend = Addend; 1267 1268 // Absolute relocations will have a zero symbol ID (STN_UNDEF), which 1269 // will manifest here as a NULL symbol name. 1270 // We can set this as a valid (but empty) symbol name, and rely 1271 // on addRelocationForSymbol to handle this. 1272 if (!Value.SymbolName) 1273 Value.SymbolName = ""; 1274 break; 1275 } 1276 default: 1277 llvm_unreachable("Unresolved symbol type!"); 1278 break; 1279 } 1280 } 1281 1282 uint64_t Offset = RelI->getOffset(); 1283 1284 DEBUG(dbgs() << "\t\tSectionID: " << SectionID << " Offset: " << Offset 1285 << "\n"); 1286 if ((Arch == Triple::aarch64 || Arch == Triple::aarch64_be) && 1287 (RelType == ELF::R_AARCH64_CALL26 || RelType == ELF::R_AARCH64_JUMP26)) { 1288 // This is an AArch64 branch relocation, need to use a stub function. 1289 DEBUG(dbgs() << "\t\tThis is an AArch64 branch relocation."); 1290 SectionEntry &Section = Sections[SectionID]; 1291 1292 // Look for an existing stub. 1293 StubMap::const_iterator i = Stubs.find(Value); 1294 if (i != Stubs.end()) { 1295 resolveRelocation(Section, Offset, 1296 (uint64_t)Section.getAddressWithOffset(i->second), 1297 RelType, 0); 1298 DEBUG(dbgs() << " Stub function found\n"); 1299 } else { 1300 // Create a new stub function. 1301 DEBUG(dbgs() << " Create a new stub function\n"); 1302 Stubs[Value] = Section.getStubOffset(); 1303 uint8_t *StubTargetAddr = createStubFunction( 1304 Section.getAddressWithOffset(Section.getStubOffset())); 1305 1306 RelocationEntry REmovz_g3(SectionID, 1307 StubTargetAddr - Section.getAddress(), 1308 ELF::R_AARCH64_MOVW_UABS_G3, Value.Addend); 1309 RelocationEntry REmovk_g2(SectionID, StubTargetAddr - 1310 Section.getAddress() + 4, 1311 ELF::R_AARCH64_MOVW_UABS_G2_NC, Value.Addend); 1312 RelocationEntry REmovk_g1(SectionID, StubTargetAddr - 1313 Section.getAddress() + 8, 1314 ELF::R_AARCH64_MOVW_UABS_G1_NC, Value.Addend); 1315 RelocationEntry REmovk_g0(SectionID, StubTargetAddr - 1316 Section.getAddress() + 12, 1317 ELF::R_AARCH64_MOVW_UABS_G0_NC, Value.Addend); 1318 1319 if (Value.SymbolName) { 1320 addRelocationForSymbol(REmovz_g3, Value.SymbolName); 1321 addRelocationForSymbol(REmovk_g2, Value.SymbolName); 1322 addRelocationForSymbol(REmovk_g1, Value.SymbolName); 1323 addRelocationForSymbol(REmovk_g0, Value.SymbolName); 1324 } else { 1325 addRelocationForSection(REmovz_g3, Value.SectionID); 1326 addRelocationForSection(REmovk_g2, Value.SectionID); 1327 addRelocationForSection(REmovk_g1, Value.SectionID); 1328 addRelocationForSection(REmovk_g0, Value.SectionID); 1329 } 1330 resolveRelocation(Section, Offset, 1331 reinterpret_cast<uint64_t>(Section.getAddressWithOffset( 1332 Section.getStubOffset())), 1333 RelType, 0); 1334 Section.advanceStubOffset(getMaxStubSize()); 1335 } 1336 } else if (Arch == Triple::arm) { 1337 if (RelType == ELF::R_ARM_PC24 || RelType == ELF::R_ARM_CALL || 1338 RelType == ELF::R_ARM_JUMP24) { 1339 // This is an ARM branch relocation, need to use a stub function. 1340 DEBUG(dbgs() << "\t\tThis is an ARM branch relocation.\n"); 1341 SectionEntry &Section = Sections[SectionID]; 1342 1343 // Look for an existing stub. 1344 StubMap::const_iterator i = Stubs.find(Value); 1345 if (i != Stubs.end()) { 1346 resolveRelocation( 1347 Section, Offset, 1348 reinterpret_cast<uint64_t>(Section.getAddressWithOffset(i->second)), 1349 RelType, 0); 1350 DEBUG(dbgs() << " Stub function found\n"); 1351 } else { 1352 // Create a new stub function. 1353 DEBUG(dbgs() << " Create a new stub function\n"); 1354 Stubs[Value] = Section.getStubOffset(); 1355 uint8_t *StubTargetAddr = createStubFunction( 1356 Section.getAddressWithOffset(Section.getStubOffset())); 1357 RelocationEntry RE(SectionID, StubTargetAddr - Section.getAddress(), 1358 ELF::R_ARM_ABS32, Value.Addend); 1359 if (Value.SymbolName) 1360 addRelocationForSymbol(RE, Value.SymbolName); 1361 else 1362 addRelocationForSection(RE, Value.SectionID); 1363 1364 resolveRelocation(Section, Offset, reinterpret_cast<uint64_t>( 1365 Section.getAddressWithOffset( 1366 Section.getStubOffset())), 1367 RelType, 0); 1368 Section.advanceStubOffset(getMaxStubSize()); 1369 } 1370 } else { 1371 uint32_t *Placeholder = 1372 reinterpret_cast<uint32_t*>(computePlaceholderAddress(SectionID, Offset)); 1373 if (RelType == ELF::R_ARM_PREL31 || RelType == ELF::R_ARM_TARGET1 || 1374 RelType == ELF::R_ARM_ABS32) { 1375 Value.Addend += *Placeholder; 1376 } else if (RelType == ELF::R_ARM_MOVW_ABS_NC || RelType == ELF::R_ARM_MOVT_ABS) { 1377 // See ELF for ARM documentation 1378 Value.Addend += (int16_t)((*Placeholder & 0xFFF) | (((*Placeholder >> 16) & 0xF) << 12)); 1379 } 1380 processSimpleRelocation(SectionID, Offset, RelType, Value); 1381 } 1382 } else if (IsMipsO32ABI) { 1383 uint8_t *Placeholder = reinterpret_cast<uint8_t *>( 1384 computePlaceholderAddress(SectionID, Offset)); 1385 uint32_t Opcode = readBytesUnaligned(Placeholder, 4); 1386 if (RelType == ELF::R_MIPS_26) { 1387 // This is an Mips branch relocation, need to use a stub function. 1388 DEBUG(dbgs() << "\t\tThis is a Mips branch relocation."); 1389 SectionEntry &Section = Sections[SectionID]; 1390 1391 // Extract the addend from the instruction. 1392 // We shift up by two since the Value will be down shifted again 1393 // when applying the relocation. 1394 uint32_t Addend = (Opcode & 0x03ffffff) << 2; 1395 1396 Value.Addend += Addend; 1397 1398 // Look up for existing stub. 1399 StubMap::const_iterator i = Stubs.find(Value); 1400 if (i != Stubs.end()) { 1401 RelocationEntry RE(SectionID, Offset, RelType, i->second); 1402 addRelocationForSection(RE, SectionID); 1403 DEBUG(dbgs() << " Stub function found\n"); 1404 } else { 1405 // Create a new stub function. 1406 DEBUG(dbgs() << " Create a new stub function\n"); 1407 Stubs[Value] = Section.getStubOffset(); 1408 1409 unsigned AbiVariant; 1410 O.getPlatformFlags(AbiVariant); 1411 1412 uint8_t *StubTargetAddr = createStubFunction( 1413 Section.getAddressWithOffset(Section.getStubOffset()), AbiVariant); 1414 1415 // Creating Hi and Lo relocations for the filled stub instructions. 1416 RelocationEntry REHi(SectionID, StubTargetAddr - Section.getAddress(), 1417 ELF::R_MIPS_HI16, Value.Addend); 1418 RelocationEntry RELo(SectionID, 1419 StubTargetAddr - Section.getAddress() + 4, 1420 ELF::R_MIPS_LO16, Value.Addend); 1421 1422 if (Value.SymbolName) { 1423 addRelocationForSymbol(REHi, Value.SymbolName); 1424 addRelocationForSymbol(RELo, Value.SymbolName); 1425 } 1426 else { 1427 addRelocationForSection(REHi, Value.SectionID); 1428 addRelocationForSection(RELo, Value.SectionID); 1429 } 1430 1431 RelocationEntry RE(SectionID, Offset, RelType, Section.getStubOffset()); 1432 addRelocationForSection(RE, SectionID); 1433 Section.advanceStubOffset(getMaxStubSize()); 1434 } 1435 } else if (RelType == ELF::R_MIPS_HI16 || RelType == ELF::R_MIPS_PCHI16) { 1436 int64_t Addend = (Opcode & 0x0000ffff) << 16; 1437 RelocationEntry RE(SectionID, Offset, RelType, Addend); 1438 PendingRelocs.push_back(std::make_pair(Value, RE)); 1439 } else if (RelType == ELF::R_MIPS_LO16 || RelType == ELF::R_MIPS_PCLO16) { 1440 int64_t Addend = Value.Addend + SignExtend32<16>(Opcode & 0x0000ffff); 1441 for (auto I = PendingRelocs.begin(); I != PendingRelocs.end();) { 1442 const RelocationValueRef &MatchingValue = I->first; 1443 RelocationEntry &Reloc = I->second; 1444 if (MatchingValue == Value && 1445 RelType == getMatchingLoRelocation(Reloc.RelType) && 1446 SectionID == Reloc.SectionID) { 1447 Reloc.Addend += Addend; 1448 if (Value.SymbolName) 1449 addRelocationForSymbol(Reloc, Value.SymbolName); 1450 else 1451 addRelocationForSection(Reloc, Value.SectionID); 1452 I = PendingRelocs.erase(I); 1453 } else 1454 ++I; 1455 } 1456 RelocationEntry RE(SectionID, Offset, RelType, Addend); 1457 if (Value.SymbolName) 1458 addRelocationForSymbol(RE, Value.SymbolName); 1459 else 1460 addRelocationForSection(RE, Value.SectionID); 1461 } else { 1462 if (RelType == ELF::R_MIPS_32) 1463 Value.Addend += Opcode; 1464 else if (RelType == ELF::R_MIPS_PC16) 1465 Value.Addend += SignExtend32<18>((Opcode & 0x0000ffff) << 2); 1466 else if (RelType == ELF::R_MIPS_PC19_S2) 1467 Value.Addend += SignExtend32<21>((Opcode & 0x0007ffff) << 2); 1468 else if (RelType == ELF::R_MIPS_PC21_S2) 1469 Value.Addend += SignExtend32<23>((Opcode & 0x001fffff) << 2); 1470 else if (RelType == ELF::R_MIPS_PC26_S2) 1471 Value.Addend += SignExtend32<28>((Opcode & 0x03ffffff) << 2); 1472 processSimpleRelocation(SectionID, Offset, RelType, Value); 1473 } 1474 } else if (IsMipsN32ABI || IsMipsN64ABI) { 1475 uint32_t r_type = RelType & 0xff; 1476 RelocationEntry RE(SectionID, Offset, RelType, Value.Addend); 1477 if (r_type == ELF::R_MIPS_CALL16 || r_type == ELF::R_MIPS_GOT_PAGE 1478 || r_type == ELF::R_MIPS_GOT_DISP) { 1479 StringMap<uint64_t>::iterator i = GOTSymbolOffsets.find(TargetName); 1480 if (i != GOTSymbolOffsets.end()) 1481 RE.SymOffset = i->second; 1482 else { 1483 RE.SymOffset = allocateGOTEntries(SectionID, 1); 1484 GOTSymbolOffsets[TargetName] = RE.SymOffset; 1485 } 1486 } 1487 if (Value.SymbolName) 1488 addRelocationForSymbol(RE, Value.SymbolName); 1489 else 1490 addRelocationForSection(RE, Value.SectionID); 1491 } else if (Arch == Triple::ppc64 || Arch == Triple::ppc64le) { 1492 if (RelType == ELF::R_PPC64_REL24) { 1493 // Determine ABI variant in use for this object. 1494 unsigned AbiVariant; 1495 Obj.getPlatformFlags(AbiVariant); 1496 AbiVariant &= ELF::EF_PPC64_ABI; 1497 // A PPC branch relocation will need a stub function if the target is 1498 // an external symbol (Symbol::ST_Unknown) or if the target address 1499 // is not within the signed 24-bits branch address. 1500 SectionEntry &Section = Sections[SectionID]; 1501 uint8_t *Target = Section.getAddressWithOffset(Offset); 1502 bool RangeOverflow = false; 1503 if (SymType != SymbolRef::ST_Unknown) { 1504 if (AbiVariant != 2) { 1505 // In the ELFv1 ABI, a function call may point to the .opd entry, 1506 // so the final symbol value is calculated based on the relocation 1507 // values in the .opd section. 1508 if (auto Err = findOPDEntrySection(Obj, ObjSectionToID, Value)) 1509 return std::move(Err); 1510 } else { 1511 // In the ELFv2 ABI, a function symbol may provide a local entry 1512 // point, which must be used for direct calls. 1513 uint8_t SymOther = Symbol->getOther(); 1514 Value.Addend += ELF::decodePPC64LocalEntryOffset(SymOther); 1515 } 1516 uint8_t *RelocTarget = 1517 Sections[Value.SectionID].getAddressWithOffset(Value.Addend); 1518 int32_t delta = static_cast<int32_t>(Target - RelocTarget); 1519 // If it is within 26-bits branch range, just set the branch target 1520 if (SignExtend32<26>(delta) == delta) { 1521 RelocationEntry RE(SectionID, Offset, RelType, Value.Addend); 1522 if (Value.SymbolName) 1523 addRelocationForSymbol(RE, Value.SymbolName); 1524 else 1525 addRelocationForSection(RE, Value.SectionID); 1526 } else { 1527 RangeOverflow = true; 1528 } 1529 } 1530 if (SymType == SymbolRef::ST_Unknown || RangeOverflow) { 1531 // It is an external symbol (SymbolRef::ST_Unknown) or within a range 1532 // larger than 24-bits. 1533 StubMap::const_iterator i = Stubs.find(Value); 1534 if (i != Stubs.end()) { 1535 // Symbol function stub already created, just relocate to it 1536 resolveRelocation(Section, Offset, 1537 reinterpret_cast<uint64_t>( 1538 Section.getAddressWithOffset(i->second)), 1539 RelType, 0); 1540 DEBUG(dbgs() << " Stub function found\n"); 1541 } else { 1542 // Create a new stub function. 1543 DEBUG(dbgs() << " Create a new stub function\n"); 1544 Stubs[Value] = Section.getStubOffset(); 1545 uint8_t *StubTargetAddr = createStubFunction( 1546 Section.getAddressWithOffset(Section.getStubOffset()), 1547 AbiVariant); 1548 RelocationEntry RE(SectionID, StubTargetAddr - Section.getAddress(), 1549 ELF::R_PPC64_ADDR64, Value.Addend); 1550 1551 // Generates the 64-bits address loads as exemplified in section 1552 // 4.5.1 in PPC64 ELF ABI. Note that the relocations need to 1553 // apply to the low part of the instructions, so we have to update 1554 // the offset according to the target endianness. 1555 uint64_t StubRelocOffset = StubTargetAddr - Section.getAddress(); 1556 if (!IsTargetLittleEndian) 1557 StubRelocOffset += 2; 1558 1559 RelocationEntry REhst(SectionID, StubRelocOffset + 0, 1560 ELF::R_PPC64_ADDR16_HIGHEST, Value.Addend); 1561 RelocationEntry REhr(SectionID, StubRelocOffset + 4, 1562 ELF::R_PPC64_ADDR16_HIGHER, Value.Addend); 1563 RelocationEntry REh(SectionID, StubRelocOffset + 12, 1564 ELF::R_PPC64_ADDR16_HI, Value.Addend); 1565 RelocationEntry REl(SectionID, StubRelocOffset + 16, 1566 ELF::R_PPC64_ADDR16_LO, Value.Addend); 1567 1568 if (Value.SymbolName) { 1569 addRelocationForSymbol(REhst, Value.SymbolName); 1570 addRelocationForSymbol(REhr, Value.SymbolName); 1571 addRelocationForSymbol(REh, Value.SymbolName); 1572 addRelocationForSymbol(REl, Value.SymbolName); 1573 } else { 1574 addRelocationForSection(REhst, Value.SectionID); 1575 addRelocationForSection(REhr, Value.SectionID); 1576 addRelocationForSection(REh, Value.SectionID); 1577 addRelocationForSection(REl, Value.SectionID); 1578 } 1579 1580 resolveRelocation(Section, Offset, reinterpret_cast<uint64_t>( 1581 Section.getAddressWithOffset( 1582 Section.getStubOffset())), 1583 RelType, 0); 1584 Section.advanceStubOffset(getMaxStubSize()); 1585 } 1586 if (SymType == SymbolRef::ST_Unknown) { 1587 // Restore the TOC for external calls 1588 if (AbiVariant == 2) 1589 writeInt32BE(Target + 4, 0xE8410018); // ld r2,28(r1) 1590 else 1591 writeInt32BE(Target + 4, 0xE8410028); // ld r2,40(r1) 1592 } 1593 } 1594 } else if (RelType == ELF::R_PPC64_TOC16 || 1595 RelType == ELF::R_PPC64_TOC16_DS || 1596 RelType == ELF::R_PPC64_TOC16_LO || 1597 RelType == ELF::R_PPC64_TOC16_LO_DS || 1598 RelType == ELF::R_PPC64_TOC16_HI || 1599 RelType == ELF::R_PPC64_TOC16_HA) { 1600 // These relocations are supposed to subtract the TOC address from 1601 // the final value. This does not fit cleanly into the RuntimeDyld 1602 // scheme, since there may be *two* sections involved in determining 1603 // the relocation value (the section of the symbol referred to by the 1604 // relocation, and the TOC section associated with the current module). 1605 // 1606 // Fortunately, these relocations are currently only ever generated 1607 // referring to symbols that themselves reside in the TOC, which means 1608 // that the two sections are actually the same. Thus they cancel out 1609 // and we can immediately resolve the relocation right now. 1610 switch (RelType) { 1611 case ELF::R_PPC64_TOC16: RelType = ELF::R_PPC64_ADDR16; break; 1612 case ELF::R_PPC64_TOC16_DS: RelType = ELF::R_PPC64_ADDR16_DS; break; 1613 case ELF::R_PPC64_TOC16_LO: RelType = ELF::R_PPC64_ADDR16_LO; break; 1614 case ELF::R_PPC64_TOC16_LO_DS: RelType = ELF::R_PPC64_ADDR16_LO_DS; break; 1615 case ELF::R_PPC64_TOC16_HI: RelType = ELF::R_PPC64_ADDR16_HI; break; 1616 case ELF::R_PPC64_TOC16_HA: RelType = ELF::R_PPC64_ADDR16_HA; break; 1617 default: llvm_unreachable("Wrong relocation type."); 1618 } 1619 1620 RelocationValueRef TOCValue; 1621 if (auto Err = findPPC64TOCSection(Obj, ObjSectionToID, TOCValue)) 1622 return std::move(Err); 1623 if (Value.SymbolName || Value.SectionID != TOCValue.SectionID) 1624 llvm_unreachable("Unsupported TOC relocation."); 1625 Value.Addend -= TOCValue.Addend; 1626 resolveRelocation(Sections[SectionID], Offset, Value.Addend, RelType, 0); 1627 } else { 1628 // There are two ways to refer to the TOC address directly: either 1629 // via a ELF::R_PPC64_TOC relocation (where both symbol and addend are 1630 // ignored), or via any relocation that refers to the magic ".TOC." 1631 // symbols (in which case the addend is respected). 1632 if (RelType == ELF::R_PPC64_TOC) { 1633 RelType = ELF::R_PPC64_ADDR64; 1634 if (auto Err = findPPC64TOCSection(Obj, ObjSectionToID, Value)) 1635 return std::move(Err); 1636 } else if (TargetName == ".TOC.") { 1637 if (auto Err = findPPC64TOCSection(Obj, ObjSectionToID, Value)) 1638 return std::move(Err); 1639 Value.Addend += Addend; 1640 } 1641 1642 RelocationEntry RE(SectionID, Offset, RelType, Value.Addend); 1643 1644 if (Value.SymbolName) 1645 addRelocationForSymbol(RE, Value.SymbolName); 1646 else 1647 addRelocationForSection(RE, Value.SectionID); 1648 } 1649 } else if (Arch == Triple::systemz && 1650 (RelType == ELF::R_390_PLT32DBL || RelType == ELF::R_390_GOTENT)) { 1651 // Create function stubs for both PLT and GOT references, regardless of 1652 // whether the GOT reference is to data or code. The stub contains the 1653 // full address of the symbol, as needed by GOT references, and the 1654 // executable part only adds an overhead of 8 bytes. 1655 // 1656 // We could try to conserve space by allocating the code and data 1657 // parts of the stub separately. However, as things stand, we allocate 1658 // a stub for every relocation, so using a GOT in JIT code should be 1659 // no less space efficient than using an explicit constant pool. 1660 DEBUG(dbgs() << "\t\tThis is a SystemZ indirect relocation."); 1661 SectionEntry &Section = Sections[SectionID]; 1662 1663 // Look for an existing stub. 1664 StubMap::const_iterator i = Stubs.find(Value); 1665 uintptr_t StubAddress; 1666 if (i != Stubs.end()) { 1667 StubAddress = uintptr_t(Section.getAddressWithOffset(i->second)); 1668 DEBUG(dbgs() << " Stub function found\n"); 1669 } else { 1670 // Create a new stub function. 1671 DEBUG(dbgs() << " Create a new stub function\n"); 1672 1673 uintptr_t BaseAddress = uintptr_t(Section.getAddress()); 1674 uintptr_t StubAlignment = getStubAlignment(); 1675 StubAddress = 1676 (BaseAddress + Section.getStubOffset() + StubAlignment - 1) & 1677 -StubAlignment; 1678 unsigned StubOffset = StubAddress - BaseAddress; 1679 1680 Stubs[Value] = StubOffset; 1681 createStubFunction((uint8_t *)StubAddress); 1682 RelocationEntry RE(SectionID, StubOffset + 8, ELF::R_390_64, 1683 Value.Offset); 1684 if (Value.SymbolName) 1685 addRelocationForSymbol(RE, Value.SymbolName); 1686 else 1687 addRelocationForSection(RE, Value.SectionID); 1688 Section.advanceStubOffset(getMaxStubSize()); 1689 } 1690 1691 if (RelType == ELF::R_390_GOTENT) 1692 resolveRelocation(Section, Offset, StubAddress + 8, ELF::R_390_PC32DBL, 1693 Addend); 1694 else 1695 resolveRelocation(Section, Offset, StubAddress, RelType, Addend); 1696 } else if (Arch == Triple::x86_64) { 1697 if (RelType == ELF::R_X86_64_PLT32) { 1698 // The way the PLT relocations normally work is that the linker allocates 1699 // the 1700 // PLT and this relocation makes a PC-relative call into the PLT. The PLT 1701 // entry will then jump to an address provided by the GOT. On first call, 1702 // the 1703 // GOT address will point back into PLT code that resolves the symbol. After 1704 // the first call, the GOT entry points to the actual function. 1705 // 1706 // For local functions we're ignoring all of that here and just replacing 1707 // the PLT32 relocation type with PC32, which will translate the relocation 1708 // into a PC-relative call directly to the function. For external symbols we 1709 // can't be sure the function will be within 2^32 bytes of the call site, so 1710 // we need to create a stub, which calls into the GOT. This case is 1711 // equivalent to the usual PLT implementation except that we use the stub 1712 // mechanism in RuntimeDyld (which puts stubs at the end of the section) 1713 // rather than allocating a PLT section. 1714 if (Value.SymbolName) { 1715 // This is a call to an external function. 1716 // Look for an existing stub. 1717 SectionEntry &Section = Sections[SectionID]; 1718 StubMap::const_iterator i = Stubs.find(Value); 1719 uintptr_t StubAddress; 1720 if (i != Stubs.end()) { 1721 StubAddress = uintptr_t(Section.getAddress()) + i->second; 1722 DEBUG(dbgs() << " Stub function found\n"); 1723 } else { 1724 // Create a new stub function (equivalent to a PLT entry). 1725 DEBUG(dbgs() << " Create a new stub function\n"); 1726 1727 uintptr_t BaseAddress = uintptr_t(Section.getAddress()); 1728 uintptr_t StubAlignment = getStubAlignment(); 1729 StubAddress = 1730 (BaseAddress + Section.getStubOffset() + StubAlignment - 1) & 1731 -StubAlignment; 1732 unsigned StubOffset = StubAddress - BaseAddress; 1733 Stubs[Value] = StubOffset; 1734 createStubFunction((uint8_t *)StubAddress); 1735 1736 // Bump our stub offset counter 1737 Section.advanceStubOffset(getMaxStubSize()); 1738 1739 // Allocate a GOT Entry 1740 uint64_t GOTOffset = allocateGOTEntries(SectionID, 1); 1741 1742 // The load of the GOT address has an addend of -4 1743 resolveGOTOffsetRelocation(SectionID, StubOffset + 2, GOTOffset - 4); 1744 1745 // Fill in the value of the symbol we're targeting into the GOT 1746 addRelocationForSymbol( 1747 computeGOTOffsetRE(SectionID, GOTOffset, 0, ELF::R_X86_64_64), 1748 Value.SymbolName); 1749 } 1750 1751 // Make the target call a call into the stub table. 1752 resolveRelocation(Section, Offset, StubAddress, ELF::R_X86_64_PC32, 1753 Addend); 1754 } else { 1755 RelocationEntry RE(SectionID, Offset, ELF::R_X86_64_PC32, Value.Addend, 1756 Value.Offset); 1757 addRelocationForSection(RE, Value.SectionID); 1758 } 1759 } else if (RelType == ELF::R_X86_64_GOTPCREL || 1760 RelType == ELF::R_X86_64_GOTPCRELX || 1761 RelType == ELF::R_X86_64_REX_GOTPCRELX) { 1762 uint64_t GOTOffset = allocateGOTEntries(SectionID, 1); 1763 resolveGOTOffsetRelocation(SectionID, Offset, GOTOffset + Addend); 1764 1765 // Fill in the value of the symbol we're targeting into the GOT 1766 RelocationEntry RE = computeGOTOffsetRE(SectionID, GOTOffset, Value.Offset, ELF::R_X86_64_64); 1767 if (Value.SymbolName) 1768 addRelocationForSymbol(RE, Value.SymbolName); 1769 else 1770 addRelocationForSection(RE, Value.SectionID); 1771 } else if (RelType == ELF::R_X86_64_PC32) { 1772 Value.Addend += support::ulittle32_t::ref(computePlaceholderAddress(SectionID, Offset)); 1773 processSimpleRelocation(SectionID, Offset, RelType, Value); 1774 } else if (RelType == ELF::R_X86_64_PC64) { 1775 Value.Addend += support::ulittle64_t::ref(computePlaceholderAddress(SectionID, Offset)); 1776 processSimpleRelocation(SectionID, Offset, RelType, Value); 1777 } else { 1778 processSimpleRelocation(SectionID, Offset, RelType, Value); 1779 } 1780 } else { 1781 if (Arch == Triple::x86) { 1782 Value.Addend += support::ulittle32_t::ref(computePlaceholderAddress(SectionID, Offset)); 1783 } 1784 processSimpleRelocation(SectionID, Offset, RelType, Value); 1785 } 1786 return ++RelI; 1787 } 1788 1789 size_t RuntimeDyldELF::getGOTEntrySize() { 1790 // We don't use the GOT in all of these cases, but it's essentially free 1791 // to put them all here. 1792 size_t Result = 0; 1793 switch (Arch) { 1794 case Triple::x86_64: 1795 case Triple::aarch64: 1796 case Triple::aarch64_be: 1797 case Triple::ppc64: 1798 case Triple::ppc64le: 1799 case Triple::systemz: 1800 Result = sizeof(uint64_t); 1801 break; 1802 case Triple::x86: 1803 case Triple::arm: 1804 case Triple::thumb: 1805 Result = sizeof(uint32_t); 1806 break; 1807 case Triple::mips: 1808 case Triple::mipsel: 1809 case Triple::mips64: 1810 case Triple::mips64el: 1811 if (IsMipsO32ABI || IsMipsN32ABI) 1812 Result = sizeof(uint32_t); 1813 else if (IsMipsN64ABI) 1814 Result = sizeof(uint64_t); 1815 else 1816 llvm_unreachable("Mips ABI not handled"); 1817 break; 1818 default: 1819 llvm_unreachable("Unsupported CPU type!"); 1820 } 1821 return Result; 1822 } 1823 1824 uint64_t RuntimeDyldELF::allocateGOTEntries(unsigned SectionID, unsigned no) 1825 { 1826 (void)SectionID; // The GOT Section is the same for all section in the object file 1827 if (GOTSectionID == 0) { 1828 GOTSectionID = Sections.size(); 1829 // Reserve a section id. We'll allocate the section later 1830 // once we know the total size 1831 Sections.push_back(SectionEntry(".got", nullptr, 0, 0, 0)); 1832 } 1833 uint64_t StartOffset = CurrentGOTIndex * getGOTEntrySize(); 1834 CurrentGOTIndex += no; 1835 return StartOffset; 1836 } 1837 1838 void RuntimeDyldELF::resolveGOTOffsetRelocation(unsigned SectionID, uint64_t Offset, uint64_t GOTOffset) 1839 { 1840 // Fill in the relative address of the GOT Entry into the stub 1841 RelocationEntry GOTRE(SectionID, Offset, ELF::R_X86_64_PC32, GOTOffset); 1842 addRelocationForSection(GOTRE, GOTSectionID); 1843 } 1844 1845 RelocationEntry RuntimeDyldELF::computeGOTOffsetRE(unsigned SectionID, uint64_t GOTOffset, uint64_t SymbolOffset, 1846 uint32_t Type) 1847 { 1848 (void)SectionID; // The GOT Section is the same for all section in the object file 1849 return RelocationEntry(GOTSectionID, GOTOffset, Type, SymbolOffset); 1850 } 1851 1852 Error RuntimeDyldELF::finalizeLoad(const ObjectFile &Obj, 1853 ObjSectionToIDMap &SectionMap) { 1854 if (IsMipsO32ABI) 1855 if (!PendingRelocs.empty()) 1856 return make_error<RuntimeDyldError>("Can't find matching LO16 reloc"); 1857 1858 // If necessary, allocate the global offset table 1859 if (GOTSectionID != 0) { 1860 // Allocate memory for the section 1861 size_t TotalSize = CurrentGOTIndex * getGOTEntrySize(); 1862 uint8_t *Addr = MemMgr.allocateDataSection(TotalSize, getGOTEntrySize(), 1863 GOTSectionID, ".got", false); 1864 if (!Addr) 1865 return make_error<RuntimeDyldError>("Unable to allocate memory for GOT!"); 1866 1867 Sections[GOTSectionID] = 1868 SectionEntry(".got", Addr, TotalSize, TotalSize, 0); 1869 1870 if (Checker) 1871 Checker->registerSection(Obj.getFileName(), GOTSectionID); 1872 1873 // For now, initialize all GOT entries to zero. We'll fill them in as 1874 // needed when GOT-based relocations are applied. 1875 memset(Addr, 0, TotalSize); 1876 if (IsMipsN32ABI || IsMipsN64ABI) { 1877 // To correctly resolve Mips GOT relocations, we need a mapping from 1878 // object's sections to GOTs. 1879 for (section_iterator SI = Obj.section_begin(), SE = Obj.section_end(); 1880 SI != SE; ++SI) { 1881 if (SI->relocation_begin() != SI->relocation_end()) { 1882 section_iterator RelocatedSection = SI->getRelocatedSection(); 1883 ObjSectionToIDMap::iterator i = SectionMap.find(*RelocatedSection); 1884 assert (i != SectionMap.end()); 1885 SectionToGOTMap[i->second] = GOTSectionID; 1886 } 1887 } 1888 GOTSymbolOffsets.clear(); 1889 } 1890 } 1891 1892 // Look for and record the EH frame section. 1893 ObjSectionToIDMap::iterator i, e; 1894 for (i = SectionMap.begin(), e = SectionMap.end(); i != e; ++i) { 1895 const SectionRef &Section = i->first; 1896 StringRef Name; 1897 Section.getName(Name); 1898 if (Name == ".eh_frame") { 1899 UnregisteredEHFrameSections.push_back(i->second); 1900 break; 1901 } 1902 } 1903 1904 GOTSectionID = 0; 1905 CurrentGOTIndex = 0; 1906 1907 return Error::success(); 1908 } 1909 1910 bool RuntimeDyldELF::isCompatibleFile(const object::ObjectFile &Obj) const { 1911 return Obj.isELF(); 1912 } 1913 1914 bool RuntimeDyldELF::relocationNeedsStub(const RelocationRef &R) const { 1915 if (Arch != Triple::x86_64) 1916 return true; // Conservative answer 1917 1918 switch (R.getType()) { 1919 default: 1920 return true; // Conservative answer 1921 1922 1923 case ELF::R_X86_64_GOTPCREL: 1924 case ELF::R_X86_64_GOTPCRELX: 1925 case ELF::R_X86_64_REX_GOTPCRELX: 1926 case ELF::R_X86_64_PC32: 1927 case ELF::R_X86_64_PC64: 1928 case ELF::R_X86_64_64: 1929 // We know that these reloation types won't need a stub function. This list 1930 // can be extended as needed. 1931 return false; 1932 } 1933 } 1934 1935 } // namespace llvm 1936