LLVM API Documentation
00001 //===-- RuntimeDyldELF.cpp - Run-time dynamic linker for MC-JIT -*- C++ -*-===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // Implementation of ELF support for the MC-JIT runtime dynamic linker. 00011 // 00012 //===----------------------------------------------------------------------===// 00013 00014 #include "RuntimeDyldELF.h" 00015 #include "JITRegistrar.h" 00016 #include "ObjectImageCommon.h" 00017 #include "llvm/ADT/IntervalMap.h" 00018 #include "llvm/ADT/STLExtras.h" 00019 #include "llvm/ADT/StringRef.h" 00020 #include "llvm/ADT/Triple.h" 00021 #include "llvm/ExecutionEngine/ObjectBuffer.h" 00022 #include "llvm/ExecutionEngine/ObjectImage.h" 00023 #include "llvm/Object/ELFObjectFile.h" 00024 #include "llvm/Object/ObjectFile.h" 00025 #include "llvm/Support/ELF.h" 00026 #include "llvm/Support/Endian.h" 00027 #include "llvm/Support/MemoryBuffer.h" 00028 00029 using namespace llvm; 00030 using namespace llvm::object; 00031 00032 #define DEBUG_TYPE "dyld" 00033 00034 namespace { 00035 00036 static inline std::error_code check(std::error_code Err) { 00037 if (Err) { 00038 report_fatal_error(Err.message()); 00039 } 00040 return Err; 00041 } 00042 00043 template <class ELFT> class DyldELFObject : public ELFObjectFile<ELFT> { 00044 LLVM_ELF_IMPORT_TYPES_ELFT(ELFT) 00045 00046 typedef Elf_Shdr_Impl<ELFT> Elf_Shdr; 00047 typedef Elf_Sym_Impl<ELFT> Elf_Sym; 00048 typedef Elf_Rel_Impl<ELFT, false> Elf_Rel; 00049 typedef Elf_Rel_Impl<ELFT, true> Elf_Rela; 00050 00051 typedef Elf_Ehdr_Impl<ELFT> Elf_Ehdr; 00052 00053 typedef typename ELFDataTypeTypedefHelper<ELFT>::value_type addr_type; 00054 00055 std::unique_ptr<ObjectFile> UnderlyingFile; 00056 00057 public: 00058 DyldELFObject(std::unique_ptr<ObjectFile> UnderlyingFile, 00059 MemoryBufferRef Wrapper, std::error_code &ec); 00060 00061 DyldELFObject(MemoryBufferRef Wrapper, std::error_code &ec); 00062 00063 void updateSectionAddress(const SectionRef &Sec, uint64_t Addr); 00064 void updateSymbolAddress(const SymbolRef &Sym, uint64_t Addr); 00065 00066 // Methods for type inquiry through isa, cast and dyn_cast 00067 static inline bool classof(const Binary *v) { 00068 return (isa<ELFObjectFile<ELFT>>(v) && 00069 classof(cast<ELFObjectFile<ELFT>>(v))); 00070 } 00071 static inline bool classof(const ELFObjectFile<ELFT> *v) { 00072 return v->isDyldType(); 00073 } 00074 }; 00075 00076 template <class ELFT> class ELFObjectImage : public ObjectImageCommon { 00077 bool Registered; 00078 00079 public: 00080 ELFObjectImage(std::unique_ptr<ObjectBuffer> Input, 00081 std::unique_ptr<DyldELFObject<ELFT>> Obj) 00082 : ObjectImageCommon(std::move(Input), std::move(Obj)), Registered(false) { 00083 } 00084 00085 virtual ~ELFObjectImage() { 00086 if (Registered) 00087 deregisterWithDebugger(); 00088 } 00089 00090 // Subclasses can override these methods to update the image with loaded 00091 // addresses for sections and common symbols 00092 void updateSectionAddress(const SectionRef &Sec, uint64_t Addr) override { 00093 static_cast<DyldELFObject<ELFT>*>(getObjectFile()) 00094 ->updateSectionAddress(Sec, Addr); 00095 } 00096 00097 void updateSymbolAddress(const SymbolRef &Sym, uint64_t Addr) override { 00098 static_cast<DyldELFObject<ELFT>*>(getObjectFile()) 00099 ->updateSymbolAddress(Sym, Addr); 00100 } 00101 00102 void registerWithDebugger() override { 00103 JITRegistrar::getGDBRegistrar().registerObject(*Buffer); 00104 Registered = true; 00105 } 00106 void deregisterWithDebugger() override { 00107 JITRegistrar::getGDBRegistrar().deregisterObject(*Buffer); 00108 } 00109 }; 00110 00111 // The MemoryBuffer passed into this constructor is just a wrapper around the 00112 // actual memory. Ultimately, the Binary parent class will take ownership of 00113 // this MemoryBuffer object but not the underlying memory. 00114 template <class ELFT> 00115 DyldELFObject<ELFT>::DyldELFObject(MemoryBufferRef Wrapper, std::error_code &EC) 00116 : ELFObjectFile<ELFT>(Wrapper, EC) { 00117 this->isDyldELFObject = true; 00118 } 00119 00120 template <class ELFT> 00121 DyldELFObject<ELFT>::DyldELFObject(std::unique_ptr<ObjectFile> UnderlyingFile, 00122 MemoryBufferRef Wrapper, std::error_code &EC) 00123 : ELFObjectFile<ELFT>(Wrapper, EC), 00124 UnderlyingFile(std::move(UnderlyingFile)) { 00125 this->isDyldELFObject = true; 00126 } 00127 00128 template <class ELFT> 00129 void DyldELFObject<ELFT>::updateSectionAddress(const SectionRef &Sec, 00130 uint64_t Addr) { 00131 DataRefImpl ShdrRef = Sec.getRawDataRefImpl(); 00132 Elf_Shdr *shdr = 00133 const_cast<Elf_Shdr *>(reinterpret_cast<const Elf_Shdr *>(ShdrRef.p)); 00134 00135 // This assumes the address passed in matches the target address bitness 00136 // The template-based type cast handles everything else. 00137 shdr->sh_addr = static_cast<addr_type>(Addr); 00138 } 00139 00140 template <class ELFT> 00141 void DyldELFObject<ELFT>::updateSymbolAddress(const SymbolRef &SymRef, 00142 uint64_t Addr) { 00143 00144 Elf_Sym *sym = const_cast<Elf_Sym *>( 00145 ELFObjectFile<ELFT>::getSymbol(SymRef.getRawDataRefImpl())); 00146 00147 // This assumes the address passed in matches the target address bitness 00148 // The template-based type cast handles everything else. 00149 sym->st_value = static_cast<addr_type>(Addr); 00150 } 00151 00152 } // namespace 00153 00154 namespace llvm { 00155 00156 void RuntimeDyldELF::registerEHFrames() { 00157 if (!MemMgr) 00158 return; 00159 for (int i = 0, e = UnregisteredEHFrameSections.size(); i != e; ++i) { 00160 SID EHFrameSID = UnregisteredEHFrameSections[i]; 00161 uint8_t *EHFrameAddr = Sections[EHFrameSID].Address; 00162 uint64_t EHFrameLoadAddr = Sections[EHFrameSID].LoadAddress; 00163 size_t EHFrameSize = Sections[EHFrameSID].Size; 00164 MemMgr->registerEHFrames(EHFrameAddr, EHFrameLoadAddr, EHFrameSize); 00165 RegisteredEHFrameSections.push_back(EHFrameSID); 00166 } 00167 UnregisteredEHFrameSections.clear(); 00168 } 00169 00170 void RuntimeDyldELF::deregisterEHFrames() { 00171 if (!MemMgr) 00172 return; 00173 for (int i = 0, e = RegisteredEHFrameSections.size(); i != e; ++i) { 00174 SID EHFrameSID = RegisteredEHFrameSections[i]; 00175 uint8_t *EHFrameAddr = Sections[EHFrameSID].Address; 00176 uint64_t EHFrameLoadAddr = Sections[EHFrameSID].LoadAddress; 00177 size_t EHFrameSize = Sections[EHFrameSID].Size; 00178 MemMgr->deregisterEHFrames(EHFrameAddr, EHFrameLoadAddr, EHFrameSize); 00179 } 00180 RegisteredEHFrameSections.clear(); 00181 } 00182 00183 ObjectImage * 00184 RuntimeDyldELF::createObjectImageFromFile(std::unique_ptr<object::ObjectFile> ObjFile) { 00185 if (!ObjFile) 00186 return nullptr; 00187 00188 std::error_code ec; 00189 MemoryBufferRef Buffer = ObjFile->getMemoryBufferRef(); 00190 00191 if (ObjFile->getBytesInAddress() == 4 && ObjFile->isLittleEndian()) { 00192 auto Obj = 00193 llvm::make_unique<DyldELFObject<ELFType<support::little, 2, false>>>( 00194 std::move(ObjFile), Buffer, ec); 00195 return new ELFObjectImage<ELFType<support::little, 2, false>>( 00196 nullptr, std::move(Obj)); 00197 } else if (ObjFile->getBytesInAddress() == 4 && !ObjFile->isLittleEndian()) { 00198 auto Obj = 00199 llvm::make_unique<DyldELFObject<ELFType<support::big, 2, false>>>( 00200 std::move(ObjFile), Buffer, ec); 00201 return new ELFObjectImage<ELFType<support::big, 2, false>>(nullptr, std::move(Obj)); 00202 } else if (ObjFile->getBytesInAddress() == 8 && !ObjFile->isLittleEndian()) { 00203 auto Obj = llvm::make_unique<DyldELFObject<ELFType<support::big, 2, true>>>( 00204 std::move(ObjFile), Buffer, ec); 00205 return new ELFObjectImage<ELFType<support::big, 2, true>>(nullptr, 00206 std::move(Obj)); 00207 } else if (ObjFile->getBytesInAddress() == 8 && ObjFile->isLittleEndian()) { 00208 auto Obj = 00209 llvm::make_unique<DyldELFObject<ELFType<support::little, 2, true>>>( 00210 std::move(ObjFile), Buffer, ec); 00211 return new ELFObjectImage<ELFType<support::little, 2, true>>( 00212 nullptr, std::move(Obj)); 00213 } else 00214 llvm_unreachable("Unexpected ELF format"); 00215 } 00216 00217 std::unique_ptr<ObjectImage> 00218 RuntimeDyldELF::createObjectImage(std::unique_ptr<ObjectBuffer> Buffer) { 00219 if (Buffer->getBufferSize() < ELF::EI_NIDENT) 00220 llvm_unreachable("Unexpected ELF object size"); 00221 std::pair<unsigned char, unsigned char> Ident = 00222 std::make_pair((uint8_t)Buffer->getBufferStart()[ELF::EI_CLASS], 00223 (uint8_t)Buffer->getBufferStart()[ELF::EI_DATA]); 00224 std::error_code ec; 00225 00226 MemoryBufferRef Buf = Buffer->getMemBuffer(); 00227 00228 if (Ident.first == ELF::ELFCLASS32 && Ident.second == ELF::ELFDATA2LSB) { 00229 auto Obj = 00230 llvm::make_unique<DyldELFObject<ELFType<support::little, 4, false>>>( 00231 Buf, ec); 00232 return llvm::make_unique< 00233 ELFObjectImage<ELFType<support::little, 4, false>>>(std::move(Buffer), 00234 std::move(Obj)); 00235 } 00236 if (Ident.first == ELF::ELFCLASS32 && Ident.second == ELF::ELFDATA2MSB) { 00237 auto Obj = 00238 llvm::make_unique<DyldELFObject<ELFType<support::big, 4, false>>>(Buf, 00239 ec); 00240 return llvm::make_unique<ELFObjectImage<ELFType<support::big, 4, false>>>( 00241 std::move(Buffer), std::move(Obj)); 00242 } 00243 if (Ident.first == ELF::ELFCLASS64 && Ident.second == ELF::ELFDATA2MSB) { 00244 auto Obj = llvm::make_unique<DyldELFObject<ELFType<support::big, 8, true>>>( 00245 Buf, ec); 00246 return llvm::make_unique<ELFObjectImage<ELFType<support::big, 8, true>>>( 00247 std::move(Buffer), std::move(Obj)); 00248 } 00249 assert(Ident.first == ELF::ELFCLASS64 && Ident.second == ELF::ELFDATA2LSB && 00250 "Unexpected ELF format"); 00251 auto Obj = 00252 llvm::make_unique<DyldELFObject<ELFType<support::little, 8, true>>>(Buf, 00253 ec); 00254 return llvm::make_unique<ELFObjectImage<ELFType<support::little, 8, true>>>( 00255 std::move(Buffer), std::move(Obj)); 00256 } 00257 00258 RuntimeDyldELF::~RuntimeDyldELF() {} 00259 00260 void RuntimeDyldELF::resolveX86_64Relocation(const SectionEntry &Section, 00261 uint64_t Offset, uint64_t Value, 00262 uint32_t Type, int64_t Addend, 00263 uint64_t SymOffset) { 00264 switch (Type) { 00265 default: 00266 llvm_unreachable("Relocation type not implemented yet!"); 00267 break; 00268 case ELF::R_X86_64_64: { 00269 support::ulittle64_t::ref(Section.Address + Offset) = Value + Addend; 00270 DEBUG(dbgs() << "Writing " << format("%p", (Value + Addend)) << " at " 00271 << format("%p\n", Section.Address + Offset)); 00272 break; 00273 } 00274 case ELF::R_X86_64_32: 00275 case ELF::R_X86_64_32S: { 00276 Value += Addend; 00277 assert((Type == ELF::R_X86_64_32 && (Value <= UINT32_MAX)) || 00278 (Type == ELF::R_X86_64_32S && 00279 ((int64_t)Value <= INT32_MAX && (int64_t)Value >= INT32_MIN))); 00280 uint32_t TruncatedAddr = (Value & 0xFFFFFFFF); 00281 support::ulittle32_t::ref(Section.Address + Offset) = TruncatedAddr; 00282 DEBUG(dbgs() << "Writing " << format("%p", TruncatedAddr) << " at " 00283 << format("%p\n", Section.Address + Offset)); 00284 break; 00285 } 00286 case ELF::R_X86_64_GOTPCREL: { 00287 // findGOTEntry returns the 'G + GOT' part of the relocation calculation 00288 // based on the load/target address of the GOT (not the current/local addr). 00289 uint64_t GOTAddr = findGOTEntry(Value, SymOffset); 00290 uint64_t FinalAddress = Section.LoadAddress + Offset; 00291 // The processRelocationRef method combines the symbol offset and the addend 00292 // and in most cases that's what we want. For this relocation type, we need 00293 // the raw addend, so we subtract the symbol offset to get it. 00294 int64_t RealOffset = GOTAddr + Addend - SymOffset - FinalAddress; 00295 assert(RealOffset <= INT32_MAX && RealOffset >= INT32_MIN); 00296 int32_t TruncOffset = (RealOffset & 0xFFFFFFFF); 00297 support::ulittle32_t::ref(Section.Address + Offset) = TruncOffset; 00298 break; 00299 } 00300 case ELF::R_X86_64_PC32: { 00301 // Get the placeholder value from the generated object since 00302 // a previous relocation attempt may have overwritten the loaded version 00303 support::ulittle32_t::ref Placeholder( 00304 (void *)(Section.ObjAddress + Offset)); 00305 uint64_t FinalAddress = Section.LoadAddress + Offset; 00306 int64_t RealOffset = Placeholder + Value + Addend - FinalAddress; 00307 assert(RealOffset <= INT32_MAX && RealOffset >= INT32_MIN); 00308 int32_t TruncOffset = (RealOffset & 0xFFFFFFFF); 00309 support::ulittle32_t::ref(Section.Address + Offset) = TruncOffset; 00310 break; 00311 } 00312 case ELF::R_X86_64_PC64: { 00313 // Get the placeholder value from the generated object since 00314 // a previous relocation attempt may have overwritten the loaded version 00315 support::ulittle64_t::ref Placeholder( 00316 (void *)(Section.ObjAddress + Offset)); 00317 uint64_t FinalAddress = Section.LoadAddress + Offset; 00318 support::ulittle64_t::ref(Section.Address + Offset) = 00319 Placeholder + Value + Addend - FinalAddress; 00320 break; 00321 } 00322 } 00323 } 00324 00325 void RuntimeDyldELF::resolveX86Relocation(const SectionEntry &Section, 00326 uint64_t Offset, uint32_t Value, 00327 uint32_t Type, int32_t Addend) { 00328 switch (Type) { 00329 case ELF::R_386_32: { 00330 // Get the placeholder value from the generated object since 00331 // a previous relocation attempt may have overwritten the loaded version 00332 support::ulittle32_t::ref Placeholder( 00333 (void *)(Section.ObjAddress + Offset)); 00334 support::ulittle32_t::ref(Section.Address + Offset) = 00335 Placeholder + Value + Addend; 00336 break; 00337 } 00338 case ELF::R_386_PC32: { 00339 // Get the placeholder value from the generated object since 00340 // a previous relocation attempt may have overwritten the loaded version 00341 support::ulittle32_t::ref Placeholder( 00342 (void *)(Section.ObjAddress + Offset)); 00343 uint32_t FinalAddress = ((Section.LoadAddress + Offset) & 0xFFFFFFFF); 00344 uint32_t RealOffset = Placeholder + Value + Addend - FinalAddress; 00345 support::ulittle32_t::ref(Section.Address + Offset) = RealOffset; 00346 break; 00347 } 00348 default: 00349 // There are other relocation types, but it appears these are the 00350 // only ones currently used by the LLVM ELF object writer 00351 llvm_unreachable("Relocation type not implemented yet!"); 00352 break; 00353 } 00354 } 00355 00356 void RuntimeDyldELF::resolveAArch64Relocation(const SectionEntry &Section, 00357 uint64_t Offset, uint64_t Value, 00358 uint32_t Type, int64_t Addend) { 00359 uint32_t *TargetPtr = reinterpret_cast<uint32_t *>(Section.Address + Offset); 00360 uint64_t FinalAddress = Section.LoadAddress + Offset; 00361 00362 DEBUG(dbgs() << "resolveAArch64Relocation, LocalAddress: 0x" 00363 << format("%llx", Section.Address + Offset) 00364 << " FinalAddress: 0x" << format("%llx", FinalAddress) 00365 << " Value: 0x" << format("%llx", Value) << " Type: 0x" 00366 << format("%x", Type) << " Addend: 0x" << format("%llx", Addend) 00367 << "\n"); 00368 00369 switch (Type) { 00370 default: 00371 llvm_unreachable("Relocation type not implemented yet!"); 00372 break; 00373 case ELF::R_AARCH64_ABS64: { 00374 uint64_t *TargetPtr = 00375 reinterpret_cast<uint64_t *>(Section.Address + Offset); 00376 *TargetPtr = Value + Addend; 00377 break; 00378 } 00379 case ELF::R_AARCH64_PREL32: { 00380 uint64_t Result = Value + Addend - FinalAddress; 00381 assert(static_cast<int64_t>(Result) >= INT32_MIN && 00382 static_cast<int64_t>(Result) <= UINT32_MAX); 00383 *TargetPtr = static_cast<uint32_t>(Result & 0xffffffffU); 00384 break; 00385 } 00386 case ELF::R_AARCH64_CALL26: // fallthrough 00387 case ELF::R_AARCH64_JUMP26: { 00388 // Operation: S+A-P. Set Call or B immediate value to bits fff_fffc of the 00389 // calculation. 00390 uint64_t BranchImm = Value + Addend - FinalAddress; 00391 00392 // "Check that -2^27 <= result < 2^27". 00393 assert(-(1LL << 27) <= static_cast<int64_t>(BranchImm) && 00394 static_cast<int64_t>(BranchImm) < (1LL << 27)); 00395 00396 // AArch64 code is emitted with .rela relocations. The data already in any 00397 // bits affected by the relocation on entry is garbage. 00398 *TargetPtr &= 0xfc000000U; 00399 // Immediate goes in bits 25:0 of B and BL. 00400 *TargetPtr |= static_cast<uint32_t>(BranchImm & 0xffffffcU) >> 2; 00401 break; 00402 } 00403 case ELF::R_AARCH64_MOVW_UABS_G3: { 00404 uint64_t Result = Value + Addend; 00405 00406 // AArch64 code is emitted with .rela relocations. The data already in any 00407 // bits affected by the relocation on entry is garbage. 00408 *TargetPtr &= 0xffe0001fU; 00409 // Immediate goes in bits 20:5 of MOVZ/MOVK instruction 00410 *TargetPtr |= Result >> (48 - 5); 00411 // Shift must be "lsl #48", in bits 22:21 00412 assert((*TargetPtr >> 21 & 0x3) == 3 && "invalid shift for relocation"); 00413 break; 00414 } 00415 case ELF::R_AARCH64_MOVW_UABS_G2_NC: { 00416 uint64_t Result = Value + Addend; 00417 00418 // AArch64 code is emitted with .rela relocations. The data already in any 00419 // bits affected by the relocation on entry is garbage. 00420 *TargetPtr &= 0xffe0001fU; 00421 // Immediate goes in bits 20:5 of MOVZ/MOVK instruction 00422 *TargetPtr |= ((Result & 0xffff00000000ULL) >> (32 - 5)); 00423 // Shift must be "lsl #32", in bits 22:21 00424 assert((*TargetPtr >> 21 & 0x3) == 2 && "invalid shift for relocation"); 00425 break; 00426 } 00427 case ELF::R_AARCH64_MOVW_UABS_G1_NC: { 00428 uint64_t Result = Value + Addend; 00429 00430 // AArch64 code is emitted with .rela relocations. The data already in any 00431 // bits affected by the relocation on entry is garbage. 00432 *TargetPtr &= 0xffe0001fU; 00433 // Immediate goes in bits 20:5 of MOVZ/MOVK instruction 00434 *TargetPtr |= ((Result & 0xffff0000U) >> (16 - 5)); 00435 // Shift must be "lsl #16", in bits 22:2 00436 assert((*TargetPtr >> 21 & 0x3) == 1 && "invalid shift for relocation"); 00437 break; 00438 } 00439 case ELF::R_AARCH64_MOVW_UABS_G0_NC: { 00440 uint64_t Result = Value + Addend; 00441 00442 // AArch64 code is emitted with .rela relocations. The data already in any 00443 // bits affected by the relocation on entry is garbage. 00444 *TargetPtr &= 0xffe0001fU; 00445 // Immediate goes in bits 20:5 of MOVZ/MOVK instruction 00446 *TargetPtr |= ((Result & 0xffffU) << 5); 00447 // Shift must be "lsl #0", in bits 22:21. 00448 assert((*TargetPtr >> 21 & 0x3) == 0 && "invalid shift for relocation"); 00449 break; 00450 } 00451 case ELF::R_AARCH64_ADR_PREL_PG_HI21: { 00452 // Operation: Page(S+A) - Page(P) 00453 uint64_t Result = 00454 ((Value + Addend) & ~0xfffULL) - (FinalAddress & ~0xfffULL); 00455 00456 // Check that -2^32 <= X < 2^32 00457 assert(static_cast<int64_t>(Result) >= (-1LL << 32) && 00458 static_cast<int64_t>(Result) < (1LL << 32) && 00459 "overflow check failed for relocation"); 00460 00461 // AArch64 code is emitted with .rela relocations. The data already in any 00462 // bits affected by the relocation on entry is garbage. 00463 *TargetPtr &= 0x9f00001fU; 00464 // Immediate goes in bits 30:29 + 5:23 of ADRP instruction, taken 00465 // from bits 32:12 of X. 00466 *TargetPtr |= ((Result & 0x3000U) << (29 - 12)); 00467 *TargetPtr |= ((Result & 0x1ffffc000ULL) >> (14 - 5)); 00468 break; 00469 } 00470 case ELF::R_AARCH64_LDST32_ABS_LO12_NC: { 00471 // Operation: S + A 00472 uint64_t Result = Value + Addend; 00473 00474 // AArch64 code is emitted with .rela relocations. The data already in any 00475 // bits affected by the relocation on entry is garbage. 00476 *TargetPtr &= 0xffc003ffU; 00477 // Immediate goes in bits 21:10 of LD/ST instruction, taken 00478 // from bits 11:2 of X 00479 *TargetPtr |= ((Result & 0xffc) << (10 - 2)); 00480 break; 00481 } 00482 case ELF::R_AARCH64_LDST64_ABS_LO12_NC: { 00483 // Operation: S + A 00484 uint64_t Result = Value + Addend; 00485 00486 // AArch64 code is emitted with .rela relocations. The data already in any 00487 // bits affected by the relocation on entry is garbage. 00488 *TargetPtr &= 0xffc003ffU; 00489 // Immediate goes in bits 21:10 of LD/ST instruction, taken 00490 // from bits 11:3 of X 00491 *TargetPtr |= ((Result & 0xff8) << (10 - 3)); 00492 break; 00493 } 00494 } 00495 } 00496 00497 void RuntimeDyldELF::resolveARMRelocation(const SectionEntry &Section, 00498 uint64_t Offset, uint32_t Value, 00499 uint32_t Type, int32_t Addend) { 00500 // TODO: Add Thumb relocations. 00501 uint32_t *Placeholder = 00502 reinterpret_cast<uint32_t *>(Section.ObjAddress + Offset); 00503 uint32_t *TargetPtr = (uint32_t *)(Section.Address + Offset); 00504 uint32_t FinalAddress = ((Section.LoadAddress + Offset) & 0xFFFFFFFF); 00505 Value += Addend; 00506 00507 DEBUG(dbgs() << "resolveARMRelocation, LocalAddress: " 00508 << Section.Address + Offset 00509 << " FinalAddress: " << format("%p", FinalAddress) << " Value: " 00510 << format("%x", Value) << " Type: " << format("%x", Type) 00511 << " Addend: " << format("%x", Addend) << "\n"); 00512 00513 switch (Type) { 00514 default: 00515 llvm_unreachable("Not implemented relocation type!"); 00516 00517 case ELF::R_ARM_NONE: 00518 break; 00519 // Write a 32bit value to relocation address, taking into account the 00520 // implicit addend encoded in the target. 00521 case ELF::R_ARM_PREL31: 00522 case ELF::R_ARM_TARGET1: 00523 case ELF::R_ARM_ABS32: 00524 *TargetPtr = *Placeholder + Value; 00525 break; 00526 // Write first 16 bit of 32 bit value to the mov instruction. 00527 // Last 4 bit should be shifted. 00528 case ELF::R_ARM_MOVW_ABS_NC: 00529 // We are not expecting any other addend in the relocation address. 00530 // Using 0x000F0FFF because MOVW has its 16 bit immediate split into 2 00531 // non-contiguous fields. 00532 assert((*Placeholder & 0x000F0FFF) == 0); 00533 Value = Value & 0xFFFF; 00534 *TargetPtr = *Placeholder | (Value & 0xFFF); 00535 *TargetPtr |= ((Value >> 12) & 0xF) << 16; 00536 break; 00537 // Write last 16 bit of 32 bit value to the mov instruction. 00538 // Last 4 bit should be shifted. 00539 case ELF::R_ARM_MOVT_ABS: 00540 // We are not expecting any other addend in the relocation address. 00541 // Use 0x000F0FFF for the same reason as R_ARM_MOVW_ABS_NC. 00542 assert((*Placeholder & 0x000F0FFF) == 0); 00543 00544 Value = (Value >> 16) & 0xFFFF; 00545 *TargetPtr = *Placeholder | (Value & 0xFFF); 00546 *TargetPtr |= ((Value >> 12) & 0xF) << 16; 00547 break; 00548 // Write 24 bit relative value to the branch instruction. 00549 case ELF::R_ARM_PC24: // Fall through. 00550 case ELF::R_ARM_CALL: // Fall through. 00551 case ELF::R_ARM_JUMP24: { 00552 int32_t RelValue = static_cast<int32_t>(Value - FinalAddress - 8); 00553 RelValue = (RelValue & 0x03FFFFFC) >> 2; 00554 assert((*TargetPtr & 0xFFFFFF) == 0xFFFFFE); 00555 *TargetPtr &= 0xFF000000; 00556 *TargetPtr |= RelValue; 00557 break; 00558 } 00559 case ELF::R_ARM_PRIVATE_0: 00560 // This relocation is reserved by the ARM ELF ABI for internal use. We 00561 // appropriate it here to act as an R_ARM_ABS32 without any addend for use 00562 // in the stubs created during JIT (which can't put an addend into the 00563 // original object file). 00564 *TargetPtr = Value; 00565 break; 00566 } 00567 } 00568 00569 void RuntimeDyldELF::resolveMIPSRelocation(const SectionEntry &Section, 00570 uint64_t Offset, uint32_t Value, 00571 uint32_t Type, int32_t Addend) { 00572 uint32_t *Placeholder = 00573 reinterpret_cast<uint32_t *>(Section.ObjAddress + Offset); 00574 uint32_t *TargetPtr = (uint32_t *)(Section.Address + Offset); 00575 Value += Addend; 00576 00577 DEBUG(dbgs() << "resolveMipselocation, LocalAddress: " 00578 << Section.Address + Offset << " FinalAddress: " 00579 << format("%p", Section.LoadAddress + Offset) << " Value: " 00580 << format("%x", Value) << " Type: " << format("%x", Type) 00581 << " Addend: " << format("%x", Addend) << "\n"); 00582 00583 switch (Type) { 00584 default: 00585 llvm_unreachable("Not implemented relocation type!"); 00586 break; 00587 case ELF::R_MIPS_32: 00588 *TargetPtr = Value + (*Placeholder); 00589 break; 00590 case ELF::R_MIPS_26: 00591 *TargetPtr = ((*Placeholder) & 0xfc000000) | ((Value & 0x0fffffff) >> 2); 00592 break; 00593 case ELF::R_MIPS_HI16: 00594 // Get the higher 16-bits. Also add 1 if bit 15 is 1. 00595 Value += ((*Placeholder) & 0x0000ffff) << 16; 00596 *TargetPtr = 00597 ((*Placeholder) & 0xffff0000) | (((Value + 0x8000) >> 16) & 0xffff); 00598 break; 00599 case ELF::R_MIPS_LO16: 00600 Value += ((*Placeholder) & 0x0000ffff); 00601 *TargetPtr = ((*Placeholder) & 0xffff0000) | (Value & 0xffff); 00602 break; 00603 case ELF::R_MIPS_UNUSED1: 00604 // Similar to ELF::R_ARM_PRIVATE_0, R_MIPS_UNUSED1 and R_MIPS_UNUSED2 00605 // are used for internal JIT purpose. These relocations are similar to 00606 // R_MIPS_HI16 and R_MIPS_LO16, but they do not take any addend into 00607 // account. 00608 *TargetPtr = 00609 ((*TargetPtr) & 0xffff0000) | (((Value + 0x8000) >> 16) & 0xffff); 00610 break; 00611 case ELF::R_MIPS_UNUSED2: 00612 *TargetPtr = ((*TargetPtr) & 0xffff0000) | (Value & 0xffff); 00613 break; 00614 } 00615 } 00616 00617 // Return the .TOC. section and offset. 00618 void RuntimeDyldELF::findPPC64TOCSection(ObjectImage &Obj, 00619 ObjSectionToIDMap &LocalSections, 00620 RelocationValueRef &Rel) { 00621 // Set a default SectionID in case we do not find a TOC section below. 00622 // This may happen for references to TOC base base (sym@toc, .odp 00623 // relocation) without a .toc directive. In this case just use the 00624 // first section (which is usually the .odp) since the code won't 00625 // reference the .toc base directly. 00626 Rel.SymbolName = NULL; 00627 Rel.SectionID = 0; 00628 00629 // The TOC consists of sections .got, .toc, .tocbss, .plt in that 00630 // order. The TOC starts where the first of these sections starts. 00631 for (section_iterator si = Obj.begin_sections(), se = Obj.end_sections(); 00632 si != se; ++si) { 00633 00634 StringRef SectionName; 00635 check(si->getName(SectionName)); 00636 00637 if (SectionName == ".got" 00638 || SectionName == ".toc" 00639 || SectionName == ".tocbss" 00640 || SectionName == ".plt") { 00641 Rel.SectionID = findOrEmitSection(Obj, *si, false, LocalSections); 00642 break; 00643 } 00644 } 00645 00646 // Per the ppc64-elf-linux ABI, The TOC base is TOC value plus 0x8000 00647 // thus permitting a full 64 Kbytes segment. 00648 Rel.Addend = 0x8000; 00649 } 00650 00651 // Returns the sections and offset associated with the ODP entry referenced 00652 // by Symbol. 00653 void RuntimeDyldELF::findOPDEntrySection(ObjectImage &Obj, 00654 ObjSectionToIDMap &LocalSections, 00655 RelocationValueRef &Rel) { 00656 // Get the ELF symbol value (st_value) to compare with Relocation offset in 00657 // .opd entries 00658 for (section_iterator si = Obj.begin_sections(), se = Obj.end_sections(); 00659 si != se; ++si) { 00660 section_iterator RelSecI = si->getRelocatedSection(); 00661 if (RelSecI == Obj.end_sections()) 00662 continue; 00663 00664 StringRef RelSectionName; 00665 check(RelSecI->getName(RelSectionName)); 00666 if (RelSectionName != ".opd") 00667 continue; 00668 00669 for (relocation_iterator i = si->relocation_begin(), 00670 e = si->relocation_end(); 00671 i != e;) { 00672 // The R_PPC64_ADDR64 relocation indicates the first field 00673 // of a .opd entry 00674 uint64_t TypeFunc; 00675 check(i->getType(TypeFunc)); 00676 if (TypeFunc != ELF::R_PPC64_ADDR64) { 00677 ++i; 00678 continue; 00679 } 00680 00681 uint64_t TargetSymbolOffset; 00682 symbol_iterator TargetSymbol = i->getSymbol(); 00683 check(i->getOffset(TargetSymbolOffset)); 00684 int64_t Addend; 00685 check(getELFRelocationAddend(*i, Addend)); 00686 00687 ++i; 00688 if (i == e) 00689 break; 00690 00691 // Just check if following relocation is a R_PPC64_TOC 00692 uint64_t TypeTOC; 00693 check(i->getType(TypeTOC)); 00694 if (TypeTOC != ELF::R_PPC64_TOC) 00695 continue; 00696 00697 // Finally compares the Symbol value and the target symbol offset 00698 // to check if this .opd entry refers to the symbol the relocation 00699 // points to. 00700 if (Rel.Addend != (int64_t)TargetSymbolOffset) 00701 continue; 00702 00703 section_iterator tsi(Obj.end_sections()); 00704 check(TargetSymbol->getSection(tsi)); 00705 bool IsCode = false; 00706 tsi->isText(IsCode); 00707 Rel.SectionID = findOrEmitSection(Obj, (*tsi), IsCode, LocalSections); 00708 Rel.Addend = (intptr_t)Addend; 00709 return; 00710 } 00711 } 00712 llvm_unreachable("Attempting to get address of ODP entry!"); 00713 } 00714 00715 // Relocation masks following the #lo(value), #hi(value), #ha(value), 00716 // #higher(value), #highera(value), #highest(value), and #highesta(value) 00717 // macros defined in section 4.5.1. Relocation Types of the PPC-elf64abi 00718 // document. 00719 00720 static inline uint16_t applyPPClo(uint64_t value) { return value & 0xffff; } 00721 00722 static inline uint16_t applyPPChi(uint64_t value) { 00723 return (value >> 16) & 0xffff; 00724 } 00725 00726 static inline uint16_t applyPPCha (uint64_t value) { 00727 return ((value + 0x8000) >> 16) & 0xffff; 00728 } 00729 00730 static inline uint16_t applyPPChigher(uint64_t value) { 00731 return (value >> 32) & 0xffff; 00732 } 00733 00734 static inline uint16_t applyPPChighera (uint64_t value) { 00735 return ((value + 0x8000) >> 32) & 0xffff; 00736 } 00737 00738 static inline uint16_t applyPPChighest(uint64_t value) { 00739 return (value >> 48) & 0xffff; 00740 } 00741 00742 static inline uint16_t applyPPChighesta (uint64_t value) { 00743 return ((value + 0x8000) >> 48) & 0xffff; 00744 } 00745 00746 void RuntimeDyldELF::resolvePPC64Relocation(const SectionEntry &Section, 00747 uint64_t Offset, uint64_t Value, 00748 uint32_t Type, int64_t Addend) { 00749 uint8_t *LocalAddress = Section.Address + Offset; 00750 switch (Type) { 00751 default: 00752 llvm_unreachable("Relocation type not implemented yet!"); 00753 break; 00754 case ELF::R_PPC64_ADDR16: 00755 writeInt16BE(LocalAddress, applyPPClo(Value + Addend)); 00756 break; 00757 case ELF::R_PPC64_ADDR16_DS: 00758 writeInt16BE(LocalAddress, applyPPClo(Value + Addend) & ~3); 00759 break; 00760 case ELF::R_PPC64_ADDR16_LO: 00761 writeInt16BE(LocalAddress, applyPPClo(Value + Addend)); 00762 break; 00763 case ELF::R_PPC64_ADDR16_LO_DS: 00764 writeInt16BE(LocalAddress, applyPPClo(Value + Addend) & ~3); 00765 break; 00766 case ELF::R_PPC64_ADDR16_HI: 00767 writeInt16BE(LocalAddress, applyPPChi(Value + Addend)); 00768 break; 00769 case ELF::R_PPC64_ADDR16_HA: 00770 writeInt16BE(LocalAddress, applyPPCha(Value + Addend)); 00771 break; 00772 case ELF::R_PPC64_ADDR16_HIGHER: 00773 writeInt16BE(LocalAddress, applyPPChigher(Value + Addend)); 00774 break; 00775 case ELF::R_PPC64_ADDR16_HIGHERA: 00776 writeInt16BE(LocalAddress, applyPPChighera(Value + Addend)); 00777 break; 00778 case ELF::R_PPC64_ADDR16_HIGHEST: 00779 writeInt16BE(LocalAddress, applyPPChighest(Value + Addend)); 00780 break; 00781 case ELF::R_PPC64_ADDR16_HIGHESTA: 00782 writeInt16BE(LocalAddress, applyPPChighesta(Value + Addend)); 00783 break; 00784 case ELF::R_PPC64_ADDR14: { 00785 assert(((Value + Addend) & 3) == 0); 00786 // Preserve the AA/LK bits in the branch instruction 00787 uint8_t aalk = *(LocalAddress + 3); 00788 writeInt16BE(LocalAddress + 2, (aalk & 3) | ((Value + Addend) & 0xfffc)); 00789 } break; 00790 case ELF::R_PPC64_REL16_LO: { 00791 uint64_t FinalAddress = (Section.LoadAddress + Offset); 00792 uint64_t Delta = Value - FinalAddress + Addend; 00793 writeInt16BE(LocalAddress, applyPPClo(Delta)); 00794 } break; 00795 case ELF::R_PPC64_REL16_HI: { 00796 uint64_t FinalAddress = (Section.LoadAddress + Offset); 00797 uint64_t Delta = Value - FinalAddress + Addend; 00798 writeInt16BE(LocalAddress, applyPPChi(Delta)); 00799 } break; 00800 case ELF::R_PPC64_REL16_HA: { 00801 uint64_t FinalAddress = (Section.LoadAddress + Offset); 00802 uint64_t Delta = Value - FinalAddress + Addend; 00803 writeInt16BE(LocalAddress, applyPPCha(Delta)); 00804 } break; 00805 case ELF::R_PPC64_ADDR32: { 00806 int32_t Result = static_cast<int32_t>(Value + Addend); 00807 if (SignExtend32<32>(Result) != Result) 00808 llvm_unreachable("Relocation R_PPC64_ADDR32 overflow"); 00809 writeInt32BE(LocalAddress, Result); 00810 } break; 00811 case ELF::R_PPC64_REL24: { 00812 uint64_t FinalAddress = (Section.LoadAddress + Offset); 00813 int32_t delta = static_cast<int32_t>(Value - FinalAddress + Addend); 00814 if (SignExtend32<24>(delta) != delta) 00815 llvm_unreachable("Relocation R_PPC64_REL24 overflow"); 00816 // Generates a 'bl <address>' instruction 00817 writeInt32BE(LocalAddress, 0x48000001 | (delta & 0x03FFFFFC)); 00818 } break; 00819 case ELF::R_PPC64_REL32: { 00820 uint64_t FinalAddress = (Section.LoadAddress + Offset); 00821 int32_t delta = static_cast<int32_t>(Value - FinalAddress + Addend); 00822 if (SignExtend32<32>(delta) != delta) 00823 llvm_unreachable("Relocation R_PPC64_REL32 overflow"); 00824 writeInt32BE(LocalAddress, delta); 00825 } break; 00826 case ELF::R_PPC64_REL64: { 00827 uint64_t FinalAddress = (Section.LoadAddress + Offset); 00828 uint64_t Delta = Value - FinalAddress + Addend; 00829 writeInt64BE(LocalAddress, Delta); 00830 } break; 00831 case ELF::R_PPC64_ADDR64: 00832 writeInt64BE(LocalAddress, Value + Addend); 00833 break; 00834 } 00835 } 00836 00837 void RuntimeDyldELF::resolveSystemZRelocation(const SectionEntry &Section, 00838 uint64_t Offset, uint64_t Value, 00839 uint32_t Type, int64_t Addend) { 00840 uint8_t *LocalAddress = Section.Address + Offset; 00841 switch (Type) { 00842 default: 00843 llvm_unreachable("Relocation type not implemented yet!"); 00844 break; 00845 case ELF::R_390_PC16DBL: 00846 case ELF::R_390_PLT16DBL: { 00847 int64_t Delta = (Value + Addend) - (Section.LoadAddress + Offset); 00848 assert(int16_t(Delta / 2) * 2 == Delta && "R_390_PC16DBL overflow"); 00849 writeInt16BE(LocalAddress, Delta / 2); 00850 break; 00851 } 00852 case ELF::R_390_PC32DBL: 00853 case ELF::R_390_PLT32DBL: { 00854 int64_t Delta = (Value + Addend) - (Section.LoadAddress + Offset); 00855 assert(int32_t(Delta / 2) * 2 == Delta && "R_390_PC32DBL overflow"); 00856 writeInt32BE(LocalAddress, Delta / 2); 00857 break; 00858 } 00859 case ELF::R_390_PC32: { 00860 int64_t Delta = (Value + Addend) - (Section.LoadAddress + Offset); 00861 assert(int32_t(Delta) == Delta && "R_390_PC32 overflow"); 00862 writeInt32BE(LocalAddress, Delta); 00863 break; 00864 } 00865 case ELF::R_390_64: 00866 writeInt64BE(LocalAddress, Value + Addend); 00867 break; 00868 } 00869 } 00870 00871 // The target location for the relocation is described by RE.SectionID and 00872 // RE.Offset. RE.SectionID can be used to find the SectionEntry. Each 00873 // SectionEntry has three members describing its location. 00874 // SectionEntry::Address is the address at which the section has been loaded 00875 // into memory in the current (host) process. SectionEntry::LoadAddress is the 00876 // address that the section will have in the target process. 00877 // SectionEntry::ObjAddress is the address of the bits for this section in the 00878 // original emitted object image (also in the current address space). 00879 // 00880 // Relocations will be applied as if the section were loaded at 00881 // SectionEntry::LoadAddress, but they will be applied at an address based 00882 // on SectionEntry::Address. SectionEntry::ObjAddress will be used to refer to 00883 // Target memory contents if they are required for value calculations. 00884 // 00885 // The Value parameter here is the load address of the symbol for the 00886 // relocation to be applied. For relocations which refer to symbols in the 00887 // current object Value will be the LoadAddress of the section in which 00888 // the symbol resides (RE.Addend provides additional information about the 00889 // symbol location). For external symbols, Value will be the address of the 00890 // symbol in the target address space. 00891 void RuntimeDyldELF::resolveRelocation(const RelocationEntry &RE, 00892 uint64_t Value) { 00893 const SectionEntry &Section = Sections[RE.SectionID]; 00894 return resolveRelocation(Section, RE.Offset, Value, RE.RelType, RE.Addend, 00895 RE.SymOffset); 00896 } 00897 00898 void RuntimeDyldELF::resolveRelocation(const SectionEntry &Section, 00899 uint64_t Offset, uint64_t Value, 00900 uint32_t Type, int64_t Addend, 00901 uint64_t SymOffset) { 00902 switch (Arch) { 00903 case Triple::x86_64: 00904 resolveX86_64Relocation(Section, Offset, Value, Type, Addend, SymOffset); 00905 break; 00906 case Triple::x86: 00907 resolveX86Relocation(Section, Offset, (uint32_t)(Value & 0xffffffffL), Type, 00908 (uint32_t)(Addend & 0xffffffffL)); 00909 break; 00910 case Triple::aarch64: 00911 case Triple::aarch64_be: 00912 resolveAArch64Relocation(Section, Offset, Value, Type, Addend); 00913 break; 00914 case Triple::arm: // Fall through. 00915 case Triple::armeb: 00916 case Triple::thumb: 00917 case Triple::thumbeb: 00918 resolveARMRelocation(Section, Offset, (uint32_t)(Value & 0xffffffffL), Type, 00919 (uint32_t)(Addend & 0xffffffffL)); 00920 break; 00921 case Triple::mips: // Fall through. 00922 case Triple::mipsel: 00923 resolveMIPSRelocation(Section, Offset, (uint32_t)(Value & 0xffffffffL), 00924 Type, (uint32_t)(Addend & 0xffffffffL)); 00925 break; 00926 case Triple::ppc64: // Fall through. 00927 case Triple::ppc64le: 00928 resolvePPC64Relocation(Section, Offset, Value, Type, Addend); 00929 break; 00930 case Triple::systemz: 00931 resolveSystemZRelocation(Section, Offset, Value, Type, Addend); 00932 break; 00933 default: 00934 llvm_unreachable("Unsupported CPU type!"); 00935 } 00936 } 00937 00938 relocation_iterator RuntimeDyldELF::processRelocationRef( 00939 unsigned SectionID, relocation_iterator RelI, ObjectImage &Obj, 00940 ObjSectionToIDMap &ObjSectionToID, const SymbolTableMap &Symbols, 00941 StubMap &Stubs) { 00942 uint64_t RelType; 00943 Check(RelI->getType(RelType)); 00944 int64_t Addend; 00945 Check(getELFRelocationAddend(*RelI, Addend)); 00946 symbol_iterator Symbol = RelI->getSymbol(); 00947 00948 // Obtain the symbol name which is referenced in the relocation 00949 StringRef TargetName; 00950 if (Symbol != Obj.end_symbols()) 00951 Symbol->getName(TargetName); 00952 DEBUG(dbgs() << "\t\tRelType: " << RelType << " Addend: " << Addend 00953 << " TargetName: " << TargetName << "\n"); 00954 RelocationValueRef Value; 00955 // First search for the symbol in the local symbol table 00956 SymbolTableMap::const_iterator lsi = Symbols.end(); 00957 SymbolRef::Type SymType = SymbolRef::ST_Unknown; 00958 if (Symbol != Obj.end_symbols()) { 00959 lsi = Symbols.find(TargetName.data()); 00960 Symbol->getType(SymType); 00961 } 00962 if (lsi != Symbols.end()) { 00963 Value.SectionID = lsi->second.first; 00964 Value.Offset = lsi->second.second; 00965 Value.Addend = lsi->second.second + Addend; 00966 } else { 00967 // Search for the symbol in the global symbol table 00968 SymbolTableMap::const_iterator gsi = GlobalSymbolTable.end(); 00969 if (Symbol != Obj.end_symbols()) 00970 gsi = GlobalSymbolTable.find(TargetName.data()); 00971 if (gsi != GlobalSymbolTable.end()) { 00972 Value.SectionID = gsi->second.first; 00973 Value.Offset = gsi->second.second; 00974 Value.Addend = gsi->second.second + Addend; 00975 } else { 00976 switch (SymType) { 00977 case SymbolRef::ST_Debug: { 00978 // TODO: Now ELF SymbolRef::ST_Debug = STT_SECTION, it's not obviously 00979 // and can be changed by another developers. Maybe best way is add 00980 // a new symbol type ST_Section to SymbolRef and use it. 00981 section_iterator si(Obj.end_sections()); 00982 Symbol->getSection(si); 00983 if (si == Obj.end_sections()) 00984 llvm_unreachable("Symbol section not found, bad object file format!"); 00985 DEBUG(dbgs() << "\t\tThis is section symbol\n"); 00986 // Default to 'true' in case isText fails (though it never does). 00987 bool isCode = true; 00988 si->isText(isCode); 00989 Value.SectionID = findOrEmitSection(Obj, (*si), isCode, ObjSectionToID); 00990 Value.Addend = Addend; 00991 break; 00992 } 00993 case SymbolRef::ST_Data: 00994 case SymbolRef::ST_Unknown: { 00995 Value.SymbolName = TargetName.data(); 00996 Value.Addend = Addend; 00997 00998 // Absolute relocations will have a zero symbol ID (STN_UNDEF), which 00999 // will manifest here as a NULL symbol name. 01000 // We can set this as a valid (but empty) symbol name, and rely 01001 // on addRelocationForSymbol to handle this. 01002 if (!Value.SymbolName) 01003 Value.SymbolName = ""; 01004 break; 01005 } 01006 default: 01007 llvm_unreachable("Unresolved symbol type!"); 01008 break; 01009 } 01010 } 01011 } 01012 uint64_t Offset; 01013 Check(RelI->getOffset(Offset)); 01014 01015 DEBUG(dbgs() << "\t\tSectionID: " << SectionID << " Offset: " << Offset 01016 << "\n"); 01017 if ((Arch == Triple::aarch64 || Arch == Triple::aarch64_be) && 01018 (RelType == ELF::R_AARCH64_CALL26 || RelType == ELF::R_AARCH64_JUMP26)) { 01019 // This is an AArch64 branch relocation, need to use a stub function. 01020 DEBUG(dbgs() << "\t\tThis is an AArch64 branch relocation."); 01021 SectionEntry &Section = Sections[SectionID]; 01022 01023 // Look for an existing stub. 01024 StubMap::const_iterator i = Stubs.find(Value); 01025 if (i != Stubs.end()) { 01026 resolveRelocation(Section, Offset, (uint64_t)Section.Address + i->second, 01027 RelType, 0); 01028 DEBUG(dbgs() << " Stub function found\n"); 01029 } else { 01030 // Create a new stub function. 01031 DEBUG(dbgs() << " Create a new stub function\n"); 01032 Stubs[Value] = Section.StubOffset; 01033 uint8_t *StubTargetAddr = 01034 createStubFunction(Section.Address + Section.StubOffset); 01035 01036 RelocationEntry REmovz_g3(SectionID, StubTargetAddr - Section.Address, 01037 ELF::R_AARCH64_MOVW_UABS_G3, Value.Addend); 01038 RelocationEntry REmovk_g2(SectionID, StubTargetAddr - Section.Address + 4, 01039 ELF::R_AARCH64_MOVW_UABS_G2_NC, Value.Addend); 01040 RelocationEntry REmovk_g1(SectionID, StubTargetAddr - Section.Address + 8, 01041 ELF::R_AARCH64_MOVW_UABS_G1_NC, Value.Addend); 01042 RelocationEntry REmovk_g0(SectionID, 01043 StubTargetAddr - Section.Address + 12, 01044 ELF::R_AARCH64_MOVW_UABS_G0_NC, Value.Addend); 01045 01046 if (Value.SymbolName) { 01047 addRelocationForSymbol(REmovz_g3, Value.SymbolName); 01048 addRelocationForSymbol(REmovk_g2, Value.SymbolName); 01049 addRelocationForSymbol(REmovk_g1, Value.SymbolName); 01050 addRelocationForSymbol(REmovk_g0, Value.SymbolName); 01051 } else { 01052 addRelocationForSection(REmovz_g3, Value.SectionID); 01053 addRelocationForSection(REmovk_g2, Value.SectionID); 01054 addRelocationForSection(REmovk_g1, Value.SectionID); 01055 addRelocationForSection(REmovk_g0, Value.SectionID); 01056 } 01057 resolveRelocation(Section, Offset, 01058 (uint64_t)Section.Address + Section.StubOffset, RelType, 01059 0); 01060 Section.StubOffset += getMaxStubSize(); 01061 } 01062 } else if (Arch == Triple::arm && 01063 (RelType == ELF::R_ARM_PC24 || RelType == ELF::R_ARM_CALL || 01064 RelType == ELF::R_ARM_JUMP24)) { 01065 // This is an ARM branch relocation, need to use a stub function. 01066 DEBUG(dbgs() << "\t\tThis is an ARM branch relocation."); 01067 SectionEntry &Section = Sections[SectionID]; 01068 01069 // Look for an existing stub. 01070 StubMap::const_iterator i = Stubs.find(Value); 01071 if (i != Stubs.end()) { 01072 resolveRelocation(Section, Offset, (uint64_t)Section.Address + i->second, 01073 RelType, 0); 01074 DEBUG(dbgs() << " Stub function found\n"); 01075 } else { 01076 // Create a new stub function. 01077 DEBUG(dbgs() << " Create a new stub function\n"); 01078 Stubs[Value] = Section.StubOffset; 01079 uint8_t *StubTargetAddr = 01080 createStubFunction(Section.Address + Section.StubOffset); 01081 RelocationEntry RE(SectionID, StubTargetAddr - Section.Address, 01082 ELF::R_ARM_PRIVATE_0, Value.Addend); 01083 if (Value.SymbolName) 01084 addRelocationForSymbol(RE, Value.SymbolName); 01085 else 01086 addRelocationForSection(RE, Value.SectionID); 01087 01088 resolveRelocation(Section, Offset, 01089 (uint64_t)Section.Address + Section.StubOffset, RelType, 01090 0); 01091 Section.StubOffset += getMaxStubSize(); 01092 } 01093 } else if ((Arch == Triple::mipsel || Arch == Triple::mips) && 01094 RelType == ELF::R_MIPS_26) { 01095 // This is an Mips branch relocation, need to use a stub function. 01096 DEBUG(dbgs() << "\t\tThis is a Mips branch relocation."); 01097 SectionEntry &Section = Sections[SectionID]; 01098 uint8_t *Target = Section.Address + Offset; 01099 uint32_t *TargetAddress = (uint32_t *)Target; 01100 01101 // Extract the addend from the instruction. 01102 uint32_t Addend = ((*TargetAddress) & 0x03ffffff) << 2; 01103 01104 Value.Addend += Addend; 01105 01106 // Look up for existing stub. 01107 StubMap::const_iterator i = Stubs.find(Value); 01108 if (i != Stubs.end()) { 01109 RelocationEntry RE(SectionID, Offset, RelType, i->second); 01110 addRelocationForSection(RE, SectionID); 01111 DEBUG(dbgs() << " Stub function found\n"); 01112 } else { 01113 // Create a new stub function. 01114 DEBUG(dbgs() << " Create a new stub function\n"); 01115 Stubs[Value] = Section.StubOffset; 01116 uint8_t *StubTargetAddr = 01117 createStubFunction(Section.Address + Section.StubOffset); 01118 01119 // Creating Hi and Lo relocations for the filled stub instructions. 01120 RelocationEntry REHi(SectionID, StubTargetAddr - Section.Address, 01121 ELF::R_MIPS_UNUSED1, Value.Addend); 01122 RelocationEntry RELo(SectionID, StubTargetAddr - Section.Address + 4, 01123 ELF::R_MIPS_UNUSED2, Value.Addend); 01124 01125 if (Value.SymbolName) { 01126 addRelocationForSymbol(REHi, Value.SymbolName); 01127 addRelocationForSymbol(RELo, Value.SymbolName); 01128 } else { 01129 addRelocationForSection(REHi, Value.SectionID); 01130 addRelocationForSection(RELo, Value.SectionID); 01131 } 01132 01133 RelocationEntry RE(SectionID, Offset, RelType, Section.StubOffset); 01134 addRelocationForSection(RE, SectionID); 01135 Section.StubOffset += getMaxStubSize(); 01136 } 01137 } else if (Arch == Triple::ppc64 || Arch == Triple::ppc64le) { 01138 if (RelType == ELF::R_PPC64_REL24) { 01139 // Determine ABI variant in use for this object. 01140 unsigned AbiVariant; 01141 Obj.getObjectFile()->getPlatformFlags(AbiVariant); 01142 AbiVariant &= ELF::EF_PPC64_ABI; 01143 // A PPC branch relocation will need a stub function if the target is 01144 // an external symbol (Symbol::ST_Unknown) or if the target address 01145 // is not within the signed 24-bits branch address. 01146 SectionEntry &Section = Sections[SectionID]; 01147 uint8_t *Target = Section.Address + Offset; 01148 bool RangeOverflow = false; 01149 if (SymType != SymbolRef::ST_Unknown) { 01150 if (AbiVariant != 2) { 01151 // In the ELFv1 ABI, a function call may point to the .opd entry, 01152 // so the final symbol value is calculated based on the relocation 01153 // values in the .opd section. 01154 findOPDEntrySection(Obj, ObjSectionToID, Value); 01155 } else { 01156 // In the ELFv2 ABI, a function symbol may provide a local entry 01157 // point, which must be used for direct calls. 01158 uint8_t SymOther; 01159 Symbol->getOther(SymOther); 01160 Value.Addend += ELF::decodePPC64LocalEntryOffset(SymOther); 01161 } 01162 uint8_t *RelocTarget = Sections[Value.SectionID].Address + Value.Addend; 01163 int32_t delta = static_cast<int32_t>(Target - RelocTarget); 01164 // If it is within 24-bits branch range, just set the branch target 01165 if (SignExtend32<24>(delta) == delta) { 01166 RelocationEntry RE(SectionID, Offset, RelType, Value.Addend); 01167 if (Value.SymbolName) 01168 addRelocationForSymbol(RE, Value.SymbolName); 01169 else 01170 addRelocationForSection(RE, Value.SectionID); 01171 } else { 01172 RangeOverflow = true; 01173 } 01174 } 01175 if (SymType == SymbolRef::ST_Unknown || RangeOverflow == true) { 01176 // It is an external symbol (SymbolRef::ST_Unknown) or within a range 01177 // larger than 24-bits. 01178 StubMap::const_iterator i = Stubs.find(Value); 01179 if (i != Stubs.end()) { 01180 // Symbol function stub already created, just relocate to it 01181 resolveRelocation(Section, Offset, 01182 (uint64_t)Section.Address + i->second, RelType, 0); 01183 DEBUG(dbgs() << " Stub function found\n"); 01184 } else { 01185 // Create a new stub function. 01186 DEBUG(dbgs() << " Create a new stub function\n"); 01187 Stubs[Value] = Section.StubOffset; 01188 uint8_t *StubTargetAddr = 01189 createStubFunction(Section.Address + Section.StubOffset, 01190 AbiVariant); 01191 RelocationEntry RE(SectionID, StubTargetAddr - Section.Address, 01192 ELF::R_PPC64_ADDR64, Value.Addend); 01193 01194 // Generates the 64-bits address loads as exemplified in section 01195 // 4.5.1 in PPC64 ELF ABI. Note that the relocations need to 01196 // apply to the low part of the instructions, so we have to update 01197 // the offset according to the target endianness. 01198 uint64_t StubRelocOffset = StubTargetAddr - Section.Address; 01199 if (!IsTargetLittleEndian) 01200 StubRelocOffset += 2; 01201 01202 RelocationEntry REhst(SectionID, StubRelocOffset + 0, 01203 ELF::R_PPC64_ADDR16_HIGHEST, Value.Addend); 01204 RelocationEntry REhr(SectionID, StubRelocOffset + 4, 01205 ELF::R_PPC64_ADDR16_HIGHER, Value.Addend); 01206 RelocationEntry REh(SectionID, StubRelocOffset + 12, 01207 ELF::R_PPC64_ADDR16_HI, Value.Addend); 01208 RelocationEntry REl(SectionID, StubRelocOffset + 16, 01209 ELF::R_PPC64_ADDR16_LO, Value.Addend); 01210 01211 if (Value.SymbolName) { 01212 addRelocationForSymbol(REhst, Value.SymbolName); 01213 addRelocationForSymbol(REhr, Value.SymbolName); 01214 addRelocationForSymbol(REh, Value.SymbolName); 01215 addRelocationForSymbol(REl, Value.SymbolName); 01216 } else { 01217 addRelocationForSection(REhst, Value.SectionID); 01218 addRelocationForSection(REhr, Value.SectionID); 01219 addRelocationForSection(REh, Value.SectionID); 01220 addRelocationForSection(REl, Value.SectionID); 01221 } 01222 01223 resolveRelocation(Section, Offset, 01224 (uint64_t)Section.Address + Section.StubOffset, 01225 RelType, 0); 01226 Section.StubOffset += getMaxStubSize(); 01227 } 01228 if (SymType == SymbolRef::ST_Unknown) { 01229 // Restore the TOC for external calls 01230 if (AbiVariant == 2) 01231 writeInt32BE(Target + 4, 0xE8410018); // ld r2,28(r1) 01232 else 01233 writeInt32BE(Target + 4, 0xE8410028); // ld r2,40(r1) 01234 } 01235 } 01236 } else if (RelType == ELF::R_PPC64_TOC16 || 01237 RelType == ELF::R_PPC64_TOC16_DS || 01238 RelType == ELF::R_PPC64_TOC16_LO || 01239 RelType == ELF::R_PPC64_TOC16_LO_DS || 01240 RelType == ELF::R_PPC64_TOC16_HI || 01241 RelType == ELF::R_PPC64_TOC16_HA) { 01242 // These relocations are supposed to subtract the TOC address from 01243 // the final value. This does not fit cleanly into the RuntimeDyld 01244 // scheme, since there may be *two* sections involved in determining 01245 // the relocation value (the section of the symbol refered to by the 01246 // relocation, and the TOC section associated with the current module). 01247 // 01248 // Fortunately, these relocations are currently only ever generated 01249 // refering to symbols that themselves reside in the TOC, which means 01250 // that the two sections are actually the same. Thus they cancel out 01251 // and we can immediately resolve the relocation right now. 01252 switch (RelType) { 01253 case ELF::R_PPC64_TOC16: RelType = ELF::R_PPC64_ADDR16; break; 01254 case ELF::R_PPC64_TOC16_DS: RelType = ELF::R_PPC64_ADDR16_DS; break; 01255 case ELF::R_PPC64_TOC16_LO: RelType = ELF::R_PPC64_ADDR16_LO; break; 01256 case ELF::R_PPC64_TOC16_LO_DS: RelType = ELF::R_PPC64_ADDR16_LO_DS; break; 01257 case ELF::R_PPC64_TOC16_HI: RelType = ELF::R_PPC64_ADDR16_HI; break; 01258 case ELF::R_PPC64_TOC16_HA: RelType = ELF::R_PPC64_ADDR16_HA; break; 01259 default: llvm_unreachable("Wrong relocation type."); 01260 } 01261 01262 RelocationValueRef TOCValue; 01263 findPPC64TOCSection(Obj, ObjSectionToID, TOCValue); 01264 if (Value.SymbolName || Value.SectionID != TOCValue.SectionID) 01265 llvm_unreachable("Unsupported TOC relocation."); 01266 Value.Addend -= TOCValue.Addend; 01267 resolveRelocation(Sections[SectionID], Offset, Value.Addend, RelType, 0); 01268 } else { 01269 // There are two ways to refer to the TOC address directly: either 01270 // via a ELF::R_PPC64_TOC relocation (where both symbol and addend are 01271 // ignored), or via any relocation that refers to the magic ".TOC." 01272 // symbols (in which case the addend is respected). 01273 if (RelType == ELF::R_PPC64_TOC) { 01274 RelType = ELF::R_PPC64_ADDR64; 01275 findPPC64TOCSection(Obj, ObjSectionToID, Value); 01276 } else if (TargetName == ".TOC.") { 01277 findPPC64TOCSection(Obj, ObjSectionToID, Value); 01278 Value.Addend += Addend; 01279 } 01280 01281 RelocationEntry RE(SectionID, Offset, RelType, Value.Addend); 01282 01283 if (Value.SymbolName) 01284 addRelocationForSymbol(RE, Value.SymbolName); 01285 else 01286 addRelocationForSection(RE, Value.SectionID); 01287 } 01288 } else if (Arch == Triple::systemz && 01289 (RelType == ELF::R_390_PLT32DBL || RelType == ELF::R_390_GOTENT)) { 01290 // Create function stubs for both PLT and GOT references, regardless of 01291 // whether the GOT reference is to data or code. The stub contains the 01292 // full address of the symbol, as needed by GOT references, and the 01293 // executable part only adds an overhead of 8 bytes. 01294 // 01295 // We could try to conserve space by allocating the code and data 01296 // parts of the stub separately. However, as things stand, we allocate 01297 // a stub for every relocation, so using a GOT in JIT code should be 01298 // no less space efficient than using an explicit constant pool. 01299 DEBUG(dbgs() << "\t\tThis is a SystemZ indirect relocation."); 01300 SectionEntry &Section = Sections[SectionID]; 01301 01302 // Look for an existing stub. 01303 StubMap::const_iterator i = Stubs.find(Value); 01304 uintptr_t StubAddress; 01305 if (i != Stubs.end()) { 01306 StubAddress = uintptr_t(Section.Address) + i->second; 01307 DEBUG(dbgs() << " Stub function found\n"); 01308 } else { 01309 // Create a new stub function. 01310 DEBUG(dbgs() << " Create a new stub function\n"); 01311 01312 uintptr_t BaseAddress = uintptr_t(Section.Address); 01313 uintptr_t StubAlignment = getStubAlignment(); 01314 StubAddress = (BaseAddress + Section.StubOffset + StubAlignment - 1) & 01315 -StubAlignment; 01316 unsigned StubOffset = StubAddress - BaseAddress; 01317 01318 Stubs[Value] = StubOffset; 01319 createStubFunction((uint8_t *)StubAddress); 01320 RelocationEntry RE(SectionID, StubOffset + 8, ELF::R_390_64, 01321 Value.Offset); 01322 if (Value.SymbolName) 01323 addRelocationForSymbol(RE, Value.SymbolName); 01324 else 01325 addRelocationForSection(RE, Value.SectionID); 01326 Section.StubOffset = StubOffset + getMaxStubSize(); 01327 } 01328 01329 if (RelType == ELF::R_390_GOTENT) 01330 resolveRelocation(Section, Offset, StubAddress + 8, ELF::R_390_PC32DBL, 01331 Addend); 01332 else 01333 resolveRelocation(Section, Offset, StubAddress, RelType, Addend); 01334 } else if (Arch == Triple::x86_64 && RelType == ELF::R_X86_64_PLT32) { 01335 // The way the PLT relocations normally work is that the linker allocates 01336 // the 01337 // PLT and this relocation makes a PC-relative call into the PLT. The PLT 01338 // entry will then jump to an address provided by the GOT. On first call, 01339 // the 01340 // GOT address will point back into PLT code that resolves the symbol. After 01341 // the first call, the GOT entry points to the actual function. 01342 // 01343 // For local functions we're ignoring all of that here and just replacing 01344 // the PLT32 relocation type with PC32, which will translate the relocation 01345 // into a PC-relative call directly to the function. For external symbols we 01346 // can't be sure the function will be within 2^32 bytes of the call site, so 01347 // we need to create a stub, which calls into the GOT. This case is 01348 // equivalent to the usual PLT implementation except that we use the stub 01349 // mechanism in RuntimeDyld (which puts stubs at the end of the section) 01350 // rather than allocating a PLT section. 01351 if (Value.SymbolName) { 01352 // This is a call to an external function. 01353 // Look for an existing stub. 01354 SectionEntry &Section = Sections[SectionID]; 01355 StubMap::const_iterator i = Stubs.find(Value); 01356 uintptr_t StubAddress; 01357 if (i != Stubs.end()) { 01358 StubAddress = uintptr_t(Section.Address) + i->second; 01359 DEBUG(dbgs() << " Stub function found\n"); 01360 } else { 01361 // Create a new stub function (equivalent to a PLT entry). 01362 DEBUG(dbgs() << " Create a new stub function\n"); 01363 01364 uintptr_t BaseAddress = uintptr_t(Section.Address); 01365 uintptr_t StubAlignment = getStubAlignment(); 01366 StubAddress = (BaseAddress + Section.StubOffset + StubAlignment - 1) & 01367 -StubAlignment; 01368 unsigned StubOffset = StubAddress - BaseAddress; 01369 Stubs[Value] = StubOffset; 01370 createStubFunction((uint8_t *)StubAddress); 01371 01372 // Create a GOT entry for the external function. 01373 GOTEntries.push_back(Value); 01374 01375 // Make our stub function a relative call to the GOT entry. 01376 RelocationEntry RE(SectionID, StubOffset + 2, ELF::R_X86_64_GOTPCREL, 01377 -4); 01378 addRelocationForSymbol(RE, Value.SymbolName); 01379 01380 // Bump our stub offset counter 01381 Section.StubOffset = StubOffset + getMaxStubSize(); 01382 } 01383 01384 // Make the target call a call into the stub table. 01385 resolveRelocation(Section, Offset, StubAddress, ELF::R_X86_64_PC32, 01386 Addend); 01387 } else { 01388 RelocationEntry RE(SectionID, Offset, ELF::R_X86_64_PC32, Value.Addend, 01389 Value.Offset); 01390 addRelocationForSection(RE, Value.SectionID); 01391 } 01392 } else { 01393 if (Arch == Triple::x86_64 && RelType == ELF::R_X86_64_GOTPCREL) { 01394 GOTEntries.push_back(Value); 01395 } 01396 RelocationEntry RE(SectionID, Offset, RelType, Value.Addend, Value.Offset); 01397 if (Value.SymbolName) 01398 addRelocationForSymbol(RE, Value.SymbolName); 01399 else 01400 addRelocationForSection(RE, Value.SectionID); 01401 } 01402 return ++RelI; 01403 } 01404 01405 void RuntimeDyldELF::updateGOTEntries(StringRef Name, uint64_t Addr) { 01406 01407 SmallVectorImpl<std::pair<SID, GOTRelocations>>::iterator it; 01408 SmallVectorImpl<std::pair<SID, GOTRelocations>>::iterator end = GOTs.end(); 01409 01410 for (it = GOTs.begin(); it != end; ++it) { 01411 GOTRelocations &GOTEntries = it->second; 01412 for (int i = 0, e = GOTEntries.size(); i != e; ++i) { 01413 if (GOTEntries[i].SymbolName != nullptr && 01414 GOTEntries[i].SymbolName == Name) { 01415 GOTEntries[i].Offset = Addr; 01416 } 01417 } 01418 } 01419 } 01420 01421 size_t RuntimeDyldELF::getGOTEntrySize() { 01422 // We don't use the GOT in all of these cases, but it's essentially free 01423 // to put them all here. 01424 size_t Result = 0; 01425 switch (Arch) { 01426 case Triple::x86_64: 01427 case Triple::aarch64: 01428 case Triple::aarch64_be: 01429 case Triple::ppc64: 01430 case Triple::ppc64le: 01431 case Triple::systemz: 01432 Result = sizeof(uint64_t); 01433 break; 01434 case Triple::x86: 01435 case Triple::arm: 01436 case Triple::thumb: 01437 case Triple::mips: 01438 case Triple::mipsel: 01439 Result = sizeof(uint32_t); 01440 break; 01441 default: 01442 llvm_unreachable("Unsupported CPU type!"); 01443 } 01444 return Result; 01445 } 01446 01447 uint64_t RuntimeDyldELF::findGOTEntry(uint64_t LoadAddress, uint64_t Offset) { 01448 01449 const size_t GOTEntrySize = getGOTEntrySize(); 01450 01451 SmallVectorImpl<std::pair<SID, GOTRelocations>>::const_iterator it; 01452 SmallVectorImpl<std::pair<SID, GOTRelocations>>::const_iterator end = 01453 GOTs.end(); 01454 01455 int GOTIndex = -1; 01456 for (it = GOTs.begin(); it != end; ++it) { 01457 SID GOTSectionID = it->first; 01458 const GOTRelocations &GOTEntries = it->second; 01459 01460 // Find the matching entry in our vector. 01461 uint64_t SymbolOffset = 0; 01462 for (int i = 0, e = GOTEntries.size(); i != e; ++i) { 01463 if (!GOTEntries[i].SymbolName) { 01464 if (getSectionLoadAddress(GOTEntries[i].SectionID) == LoadAddress && 01465 GOTEntries[i].Offset == Offset) { 01466 GOTIndex = i; 01467 SymbolOffset = GOTEntries[i].Offset; 01468 break; 01469 } 01470 } else { 01471 // GOT entries for external symbols use the addend as the address when 01472 // the external symbol has been resolved. 01473 if (GOTEntries[i].Offset == LoadAddress) { 01474 GOTIndex = i; 01475 // Don't use the Addend here. The relocation handler will use it. 01476 break; 01477 } 01478 } 01479 } 01480 01481 if (GOTIndex != -1) { 01482 if (GOTEntrySize == sizeof(uint64_t)) { 01483 uint64_t *LocalGOTAddr = (uint64_t *)getSectionAddress(GOTSectionID); 01484 // Fill in this entry with the address of the symbol being referenced. 01485 LocalGOTAddr[GOTIndex] = LoadAddress + SymbolOffset; 01486 } else { 01487 uint32_t *LocalGOTAddr = (uint32_t *)getSectionAddress(GOTSectionID); 01488 // Fill in this entry with the address of the symbol being referenced. 01489 LocalGOTAddr[GOTIndex] = (uint32_t)(LoadAddress + SymbolOffset); 01490 } 01491 01492 // Calculate the load address of this entry 01493 return getSectionLoadAddress(GOTSectionID) + (GOTIndex * GOTEntrySize); 01494 } 01495 } 01496 01497 assert(GOTIndex != -1 && "Unable to find requested GOT entry."); 01498 return 0; 01499 } 01500 01501 void RuntimeDyldELF::finalizeLoad(ObjectImage &ObjImg, 01502 ObjSectionToIDMap &SectionMap) { 01503 // If necessary, allocate the global offset table 01504 if (MemMgr) { 01505 // Allocate the GOT if necessary 01506 size_t numGOTEntries = GOTEntries.size(); 01507 if (numGOTEntries != 0) { 01508 // Allocate memory for the section 01509 unsigned SectionID = Sections.size(); 01510 size_t TotalSize = numGOTEntries * getGOTEntrySize(); 01511 uint8_t *Addr = MemMgr->allocateDataSection(TotalSize, getGOTEntrySize(), 01512 SectionID, ".got", false); 01513 if (!Addr) 01514 report_fatal_error("Unable to allocate memory for GOT!"); 01515 01516 GOTs.push_back(std::make_pair(SectionID, GOTEntries)); 01517 Sections.push_back(SectionEntry(".got", Addr, TotalSize, 0)); 01518 // For now, initialize all GOT entries to zero. We'll fill them in as 01519 // needed when GOT-based relocations are applied. 01520 memset(Addr, 0, TotalSize); 01521 } 01522 } else { 01523 report_fatal_error("Unable to allocate memory for GOT!"); 01524 } 01525 01526 // Look for and record the EH frame section. 01527 ObjSectionToIDMap::iterator i, e; 01528 for (i = SectionMap.begin(), e = SectionMap.end(); i != e; ++i) { 01529 const SectionRef &Section = i->first; 01530 StringRef Name; 01531 Section.getName(Name); 01532 if (Name == ".eh_frame") { 01533 UnregisteredEHFrameSections.push_back(i->second); 01534 break; 01535 } 01536 } 01537 } 01538 01539 bool RuntimeDyldELF::isCompatibleFormat(const ObjectBuffer *Buffer) const { 01540 if (Buffer->getBufferSize() < strlen(ELF::ElfMagic)) 01541 return false; 01542 return (memcmp(Buffer->getBufferStart(), ELF::ElfMagic, 01543 strlen(ELF::ElfMagic))) == 0; 01544 } 01545 01546 bool RuntimeDyldELF::isCompatibleFile(const object::ObjectFile *Obj) const { 01547 return Obj->isELF(); 01548 } 01549 01550 } // namespace llvm