LLVM API Documentation
00001 //===-- RuntimeDyld.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 the MC-JIT runtime dynamic linker. 00011 // 00012 //===----------------------------------------------------------------------===// 00013 00014 #define DEBUG_TYPE "dyld" 00015 #include "llvm/ExecutionEngine/RuntimeDyld.h" 00016 #include "ObjectImageCommon.h" 00017 #include "RuntimeDyldELF.h" 00018 #include "RuntimeDyldImpl.h" 00019 #include "RuntimeDyldMachO.h" 00020 #include "llvm/Support/MathExtras.h" 00021 #include "llvm/Support/Path.h" 00022 00023 using namespace llvm; 00024 using namespace llvm::object; 00025 00026 // Empty out-of-line virtual destructor as the key function. 00027 RuntimeDyldImpl::~RuntimeDyldImpl() {} 00028 00029 namespace llvm { 00030 00031 StringRef RuntimeDyldImpl::getEHFrameSection() { 00032 return StringRef(); 00033 } 00034 00035 // Resolve the relocations for all symbols we currently know about. 00036 void RuntimeDyldImpl::resolveRelocations() { 00037 // First, resolve relocations associated with external symbols. 00038 resolveExternalSymbols(); 00039 00040 // Just iterate over the sections we have and resolve all the relocations 00041 // in them. Gross overkill, but it gets the job done. 00042 for (int i = 0, e = Sections.size(); i != e; ++i) { 00043 uint64_t Addr = Sections[i].LoadAddress; 00044 DEBUG(dbgs() << "Resolving relocations Section #" << i 00045 << "\t" << format("%p", (uint8_t *)Addr) 00046 << "\n"); 00047 resolveRelocationList(Relocations[i], Addr); 00048 } 00049 } 00050 00051 void RuntimeDyldImpl::mapSectionAddress(const void *LocalAddress, 00052 uint64_t TargetAddress) { 00053 for (unsigned i = 0, e = Sections.size(); i != e; ++i) { 00054 if (Sections[i].Address == LocalAddress) { 00055 reassignSectionAddress(i, TargetAddress); 00056 return; 00057 } 00058 } 00059 llvm_unreachable("Attempting to remap address of unknown section!"); 00060 } 00061 00062 // Subclasses can implement this method to create specialized image instances. 00063 // The caller owns the pointer that is returned. 00064 ObjectImage *RuntimeDyldImpl::createObjectImage(ObjectBuffer *InputBuffer) { 00065 return new ObjectImageCommon(InputBuffer); 00066 } 00067 00068 ObjectImage *RuntimeDyldImpl::loadObject(ObjectBuffer *InputBuffer) { 00069 OwningPtr<ObjectImage> obj(createObjectImage(InputBuffer)); 00070 if (!obj) 00071 report_fatal_error("Unable to create object image from memory buffer!"); 00072 00073 Arch = (Triple::ArchType)obj->getArch(); 00074 00075 // Symbols found in this object 00076 StringMap<SymbolLoc> LocalSymbols; 00077 // Used sections from the object file 00078 ObjSectionToIDMap LocalSections; 00079 00080 // Common symbols requiring allocation, with their sizes and alignments 00081 CommonSymbolMap CommonSymbols; 00082 // Maximum required total memory to allocate all common symbols 00083 uint64_t CommonSize = 0; 00084 00085 error_code err; 00086 // Parse symbols 00087 DEBUG(dbgs() << "Parse symbols:\n"); 00088 for (symbol_iterator i = obj->begin_symbols(), e = obj->end_symbols(); 00089 i != e; i.increment(err)) { 00090 Check(err); 00091 object::SymbolRef::Type SymType; 00092 StringRef Name; 00093 Check(i->getType(SymType)); 00094 Check(i->getName(Name)); 00095 00096 uint32_t flags; 00097 Check(i->getFlags(flags)); 00098 00099 bool isCommon = flags & SymbolRef::SF_Common; 00100 if (isCommon) { 00101 // Add the common symbols to a list. We'll allocate them all below. 00102 uint32_t Align; 00103 Check(i->getAlignment(Align)); 00104 uint64_t Size = 0; 00105 Check(i->getSize(Size)); 00106 CommonSize += Size + Align; 00107 CommonSymbols[*i] = CommonSymbolInfo(Size, Align); 00108 } else { 00109 if (SymType == object::SymbolRef::ST_Function || 00110 SymType == object::SymbolRef::ST_Data || 00111 SymType == object::SymbolRef::ST_Unknown) { 00112 uint64_t FileOffset; 00113 StringRef SectionData; 00114 bool IsCode; 00115 section_iterator si = obj->end_sections(); 00116 Check(i->getFileOffset(FileOffset)); 00117 Check(i->getSection(si)); 00118 if (si == obj->end_sections()) continue; 00119 Check(si->getContents(SectionData)); 00120 Check(si->isText(IsCode)); 00121 const uint8_t* SymPtr = (const uint8_t*)InputBuffer->getBufferStart() + 00122 (uintptr_t)FileOffset; 00123 uintptr_t SectOffset = (uintptr_t)(SymPtr - 00124 (const uint8_t*)SectionData.begin()); 00125 unsigned SectionID = findOrEmitSection(*obj, *si, IsCode, LocalSections); 00126 LocalSymbols[Name.data()] = SymbolLoc(SectionID, SectOffset); 00127 DEBUG(dbgs() << "\tFileOffset: " << format("%p", (uintptr_t)FileOffset) 00128 << " flags: " << flags 00129 << " SID: " << SectionID 00130 << " Offset: " << format("%p", SectOffset)); 00131 GlobalSymbolTable[Name] = SymbolLoc(SectionID, SectOffset); 00132 } 00133 } 00134 DEBUG(dbgs() << "\tType: " << SymType << " Name: " << Name << "\n"); 00135 } 00136 00137 // Allocate common symbols 00138 if (CommonSize != 0) 00139 emitCommonSymbols(*obj, CommonSymbols, CommonSize, LocalSymbols); 00140 00141 // Parse and process relocations 00142 DEBUG(dbgs() << "Parse relocations:\n"); 00143 for (section_iterator si = obj->begin_sections(), 00144 se = obj->end_sections(); si != se; si.increment(err)) { 00145 Check(err); 00146 bool isFirstRelocation = true; 00147 unsigned SectionID = 0; 00148 StubMap Stubs; 00149 00150 for (relocation_iterator i = si->begin_relocations(), 00151 e = si->end_relocations(); i != e; i.increment(err)) { 00152 Check(err); 00153 00154 // If it's the first relocation in this section, find its SectionID 00155 if (isFirstRelocation) { 00156 SectionID = findOrEmitSection(*obj, *si, true, LocalSections); 00157 DEBUG(dbgs() << "\tSectionID: " << SectionID << "\n"); 00158 isFirstRelocation = false; 00159 } 00160 00161 processRelocationRef(SectionID, *i, *obj, LocalSections, LocalSymbols, 00162 Stubs); 00163 } 00164 } 00165 00166 return obj.take(); 00167 } 00168 00169 void RuntimeDyldImpl::emitCommonSymbols(ObjectImage &Obj, 00170 const CommonSymbolMap &CommonSymbols, 00171 uint64_t TotalSize, 00172 SymbolTableMap &SymbolTable) { 00173 // Allocate memory for the section 00174 unsigned SectionID = Sections.size(); 00175 uint8_t *Addr = MemMgr->allocateDataSection(TotalSize, sizeof(void*), 00176 SectionID, false); 00177 if (!Addr) 00178 report_fatal_error("Unable to allocate memory for common symbols!"); 00179 uint64_t Offset = 0; 00180 Sections.push_back(SectionEntry(StringRef(), Addr, TotalSize, 0)); 00181 memset(Addr, 0, TotalSize); 00182 00183 DEBUG(dbgs() << "emitCommonSection SectionID: " << SectionID 00184 << " new addr: " << format("%p", Addr) 00185 << " DataSize: " << TotalSize 00186 << "\n"); 00187 00188 // Assign the address of each symbol 00189 for (CommonSymbolMap::const_iterator it = CommonSymbols.begin(), 00190 itEnd = CommonSymbols.end(); it != itEnd; it++) { 00191 uint64_t Size = it->second.first; 00192 uint64_t Align = it->second.second; 00193 StringRef Name; 00194 it->first.getName(Name); 00195 if (Align) { 00196 // This symbol has an alignment requirement. 00197 uint64_t AlignOffset = OffsetToAlignment((uint64_t)Addr, Align); 00198 Addr += AlignOffset; 00199 Offset += AlignOffset; 00200 DEBUG(dbgs() << "Allocating common symbol " << Name << " address " << 00201 format("%p\n", Addr)); 00202 } 00203 Obj.updateSymbolAddress(it->first, (uint64_t)Addr); 00204 SymbolTable[Name.data()] = SymbolLoc(SectionID, Offset); 00205 Offset += Size; 00206 Addr += Size; 00207 } 00208 } 00209 00210 unsigned RuntimeDyldImpl::emitSection(ObjectImage &Obj, 00211 const SectionRef &Section, 00212 bool IsCode) { 00213 00214 unsigned StubBufSize = 0, 00215 StubSize = getMaxStubSize(); 00216 error_code err; 00217 if (StubSize > 0) { 00218 for (relocation_iterator i = Section.begin_relocations(), 00219 e = Section.end_relocations(); i != e; i.increment(err), Check(err)) 00220 StubBufSize += StubSize; 00221 } 00222 StringRef data; 00223 uint64_t Alignment64; 00224 Check(Section.getContents(data)); 00225 Check(Section.getAlignment(Alignment64)); 00226 00227 unsigned Alignment = (unsigned)Alignment64 & 0xffffffffL; 00228 bool IsRequired; 00229 bool IsVirtual; 00230 bool IsZeroInit; 00231 bool IsReadOnly; 00232 uint64_t DataSize; 00233 StringRef Name; 00234 Check(Section.isRequiredForExecution(IsRequired)); 00235 Check(Section.isVirtual(IsVirtual)); 00236 Check(Section.isZeroInit(IsZeroInit)); 00237 Check(Section.isReadOnlyData(IsReadOnly)); 00238 Check(Section.getSize(DataSize)); 00239 Check(Section.getName(Name)); 00240 if (StubSize > 0) { 00241 unsigned StubAlignment = getStubAlignment(); 00242 unsigned EndAlignment = (DataSize | Alignment) & -(DataSize | Alignment); 00243 if (StubAlignment > EndAlignment) 00244 StubBufSize += StubAlignment - EndAlignment; 00245 } 00246 00247 unsigned Allocate; 00248 unsigned SectionID = Sections.size(); 00249 uint8_t *Addr; 00250 const char *pData = 0; 00251 00252 // Some sections, such as debug info, don't need to be loaded for execution. 00253 // Leave those where they are. 00254 if (IsRequired) { 00255 Allocate = DataSize + StubBufSize; 00256 Addr = IsCode 00257 ? MemMgr->allocateCodeSection(Allocate, Alignment, SectionID) 00258 : MemMgr->allocateDataSection(Allocate, Alignment, SectionID, IsReadOnly); 00259 if (!Addr) 00260 report_fatal_error("Unable to allocate section memory!"); 00261 00262 // Virtual sections have no data in the object image, so leave pData = 0 00263 if (!IsVirtual) 00264 pData = data.data(); 00265 00266 // Zero-initialize or copy the data from the image 00267 if (IsZeroInit || IsVirtual) 00268 memset(Addr, 0, DataSize); 00269 else 00270 memcpy(Addr, pData, DataSize); 00271 00272 DEBUG(dbgs() << "emitSection SectionID: " << SectionID 00273 << " Name: " << Name 00274 << " obj addr: " << format("%p", pData) 00275 << " new addr: " << format("%p", Addr) 00276 << " DataSize: " << DataSize 00277 << " StubBufSize: " << StubBufSize 00278 << " Allocate: " << Allocate 00279 << "\n"); 00280 Obj.updateSectionAddress(Section, (uint64_t)Addr); 00281 } 00282 else { 00283 // Even if we didn't load the section, we need to record an entry for it 00284 // to handle later processing (and by 'handle' I mean don't do anything 00285 // with these sections). 00286 Allocate = 0; 00287 Addr = 0; 00288 DEBUG(dbgs() << "emitSection SectionID: " << SectionID 00289 << " Name: " << Name 00290 << " obj addr: " << format("%p", data.data()) 00291 << " new addr: 0" 00292 << " DataSize: " << DataSize 00293 << " StubBufSize: " << StubBufSize 00294 << " Allocate: " << Allocate 00295 << "\n"); 00296 } 00297 00298 Sections.push_back(SectionEntry(Name, Addr, DataSize, (uintptr_t)pData)); 00299 return SectionID; 00300 } 00301 00302 unsigned RuntimeDyldImpl::findOrEmitSection(ObjectImage &Obj, 00303 const SectionRef &Section, 00304 bool IsCode, 00305 ObjSectionToIDMap &LocalSections) { 00306 00307 unsigned SectionID = 0; 00308 ObjSectionToIDMap::iterator i = LocalSections.find(Section); 00309 if (i != LocalSections.end()) 00310 SectionID = i->second; 00311 else { 00312 SectionID = emitSection(Obj, Section, IsCode); 00313 LocalSections[Section] = SectionID; 00314 } 00315 return SectionID; 00316 } 00317 00318 void RuntimeDyldImpl::addRelocationForSection(const RelocationEntry &RE, 00319 unsigned SectionID) { 00320 Relocations[SectionID].push_back(RE); 00321 } 00322 00323 void RuntimeDyldImpl::addRelocationForSymbol(const RelocationEntry &RE, 00324 StringRef SymbolName) { 00325 // Relocation by symbol. If the symbol is found in the global symbol table, 00326 // create an appropriate section relocation. Otherwise, add it to 00327 // ExternalSymbolRelocations. 00328 SymbolTableMap::const_iterator Loc = 00329 GlobalSymbolTable.find(SymbolName); 00330 if (Loc == GlobalSymbolTable.end()) { 00331 ExternalSymbolRelocations[SymbolName].push_back(RE); 00332 } else { 00333 // Copy the RE since we want to modify its addend. 00334 RelocationEntry RECopy = RE; 00335 RECopy.Addend += Loc->second.second; 00336 Relocations[Loc->second.first].push_back(RECopy); 00337 } 00338 } 00339 00340 uint8_t *RuntimeDyldImpl::createStubFunction(uint8_t *Addr) { 00341 if (Arch == Triple::aarch64) { 00342 // This stub has to be able to access the full address space, 00343 // since symbol lookup won't necessarily find a handy, in-range, 00344 // PLT stub for functions which could be anywhere. 00345 uint32_t *StubAddr = (uint32_t*)Addr; 00346 00347 // Stub can use ip0 (== x16) to calculate address 00348 *StubAddr = 0xd2e00010; // movz ip0, #:abs_g3:<addr> 00349 StubAddr++; 00350 *StubAddr = 0xf2c00010; // movk ip0, #:abs_g2_nc:<addr> 00351 StubAddr++; 00352 *StubAddr = 0xf2a00010; // movk ip0, #:abs_g1_nc:<addr> 00353 StubAddr++; 00354 *StubAddr = 0xf2800010; // movk ip0, #:abs_g0_nc:<addr> 00355 StubAddr++; 00356 *StubAddr = 0xd61f0200; // br ip0 00357 00358 return Addr; 00359 } else if (Arch == Triple::arm) { 00360 // TODO: There is only ARM far stub now. We should add the Thumb stub, 00361 // and stubs for branches Thumb - ARM and ARM - Thumb. 00362 uint32_t *StubAddr = (uint32_t*)Addr; 00363 *StubAddr = 0xe51ff004; // ldr pc,<label> 00364 return (uint8_t*)++StubAddr; 00365 } else if (Arch == Triple::mipsel || Arch == Triple::mips) { 00366 uint32_t *StubAddr = (uint32_t*)Addr; 00367 // 0: 3c190000 lui t9,%hi(addr). 00368 // 4: 27390000 addiu t9,t9,%lo(addr). 00369 // 8: 03200008 jr t9. 00370 // c: 00000000 nop. 00371 const unsigned LuiT9Instr = 0x3c190000, AdduiT9Instr = 0x27390000; 00372 const unsigned JrT9Instr = 0x03200008, NopInstr = 0x0; 00373 00374 *StubAddr = LuiT9Instr; 00375 StubAddr++; 00376 *StubAddr = AdduiT9Instr; 00377 StubAddr++; 00378 *StubAddr = JrT9Instr; 00379 StubAddr++; 00380 *StubAddr = NopInstr; 00381 return Addr; 00382 } else if (Arch == Triple::ppc64) { 00383 // PowerPC64 stub: the address points to a function descriptor 00384 // instead of the function itself. Load the function address 00385 // on r11 and sets it to control register. Also loads the function 00386 // TOC in r2 and environment pointer to r11. 00387 writeInt32BE(Addr, 0x3D800000); // lis r12, highest(addr) 00388 writeInt32BE(Addr+4, 0x618C0000); // ori r12, higher(addr) 00389 writeInt32BE(Addr+8, 0x798C07C6); // sldi r12, r12, 32 00390 writeInt32BE(Addr+12, 0x658C0000); // oris r12, r12, h(addr) 00391 writeInt32BE(Addr+16, 0x618C0000); // ori r12, r12, l(addr) 00392 writeInt32BE(Addr+20, 0xF8410028); // std r2, 40(r1) 00393 writeInt32BE(Addr+24, 0xE96C0000); // ld r11, 0(r12) 00394 writeInt32BE(Addr+28, 0xE84C0008); // ld r2, 0(r12) 00395 writeInt32BE(Addr+32, 0x7D6903A6); // mtctr r11 00396 writeInt32BE(Addr+36, 0xE96C0010); // ld r11, 16(r2) 00397 writeInt32BE(Addr+40, 0x4E800420); // bctr 00398 00399 return Addr; 00400 } else if (Arch == Triple::systemz) { 00401 writeInt16BE(Addr, 0xC418); // lgrl %r1,.+8 00402 writeInt16BE(Addr+2, 0x0000); 00403 writeInt16BE(Addr+4, 0x0004); 00404 writeInt16BE(Addr+6, 0x07F1); // brc 15,%r1 00405 // 8-byte address stored at Addr + 8 00406 return Addr; 00407 } 00408 return Addr; 00409 } 00410 00411 // Assign an address to a symbol name and resolve all the relocations 00412 // associated with it. 00413 void RuntimeDyldImpl::reassignSectionAddress(unsigned SectionID, 00414 uint64_t Addr) { 00415 // The address to use for relocation resolution is not 00416 // the address of the local section buffer. We must be doing 00417 // a remote execution environment of some sort. Relocations can't 00418 // be applied until all the sections have been moved. The client must 00419 // trigger this with a call to MCJIT::finalize() or 00420 // RuntimeDyld::resolveRelocations(). 00421 // 00422 // Addr is a uint64_t because we can't assume the pointer width 00423 // of the target is the same as that of the host. Just use a generic 00424 // "big enough" type. 00425 Sections[SectionID].LoadAddress = Addr; 00426 } 00427 00428 void RuntimeDyldImpl::resolveRelocationList(const RelocationList &Relocs, 00429 uint64_t Value) { 00430 for (unsigned i = 0, e = Relocs.size(); i != e; ++i) { 00431 const RelocationEntry &RE = Relocs[i]; 00432 // Ignore relocations for sections that were not loaded 00433 if (Sections[RE.SectionID].Address == 0) 00434 continue; 00435 resolveRelocation(RE, Value); 00436 } 00437 } 00438 00439 void RuntimeDyldImpl::resolveExternalSymbols() { 00440 StringMap<RelocationList>::iterator i = ExternalSymbolRelocations.begin(), 00441 e = ExternalSymbolRelocations.end(); 00442 for (; i != e; i++) { 00443 StringRef Name = i->first(); 00444 RelocationList &Relocs = i->second; 00445 SymbolTableMap::const_iterator Loc = GlobalSymbolTable.find(Name); 00446 if (Loc == GlobalSymbolTable.end()) { 00447 if (Name.size() == 0) { 00448 // This is an absolute symbol, use an address of zero. 00449 DEBUG(dbgs() << "Resolving absolute relocations." << "\n"); 00450 resolveRelocationList(Relocs, 0); 00451 } else { 00452 // This is an external symbol, try to get its address from 00453 // MemoryManager. 00454 uint8_t *Addr = (uint8_t*) MemMgr->getPointerToNamedFunction(Name.data(), 00455 true); 00456 DEBUG(dbgs() << "Resolving relocations Name: " << Name 00457 << "\t" << format("%p", Addr) 00458 << "\n"); 00459 resolveRelocationList(Relocs, (uintptr_t)Addr); 00460 } 00461 } else { 00462 report_fatal_error("Expected external symbol"); 00463 } 00464 } 00465 } 00466 00467 00468 //===----------------------------------------------------------------------===// 00469 // RuntimeDyld class implementation 00470 RuntimeDyld::RuntimeDyld(RTDyldMemoryManager *mm) { 00471 // FIXME: There's a potential issue lurking here if a single instance of 00472 // RuntimeDyld is used to load multiple objects. The current implementation 00473 // associates a single memory manager with a RuntimeDyld instance. Even 00474 // though the public class spawns a new 'impl' instance for each load, 00475 // they share a single memory manager. This can become a problem when page 00476 // permissions are applied. 00477 Dyld = 0; 00478 MM = mm; 00479 } 00480 00481 RuntimeDyld::~RuntimeDyld() { 00482 delete Dyld; 00483 } 00484 00485 ObjectImage *RuntimeDyld::loadObject(ObjectBuffer *InputBuffer) { 00486 if (!Dyld) { 00487 sys::LLVMFileType type = sys::IdentifyFileType( 00488 InputBuffer->getBufferStart(), 00489 static_cast<unsigned>(InputBuffer->getBufferSize())); 00490 switch (type) { 00491 case sys::ELF_Relocatable_FileType: 00492 case sys::ELF_Executable_FileType: 00493 case sys::ELF_SharedObject_FileType: 00494 case sys::ELF_Core_FileType: 00495 Dyld = new RuntimeDyldELF(MM); 00496 break; 00497 case sys::Mach_O_Object_FileType: 00498 case sys::Mach_O_Executable_FileType: 00499 case sys::Mach_O_FixedVirtualMemorySharedLib_FileType: 00500 case sys::Mach_O_Core_FileType: 00501 case sys::Mach_O_PreloadExecutable_FileType: 00502 case sys::Mach_O_DynamicallyLinkedSharedLib_FileType: 00503 case sys::Mach_O_DynamicLinker_FileType: 00504 case sys::Mach_O_Bundle_FileType: 00505 case sys::Mach_O_DynamicallyLinkedSharedLibStub_FileType: 00506 case sys::Mach_O_DSYMCompanion_FileType: 00507 Dyld = new RuntimeDyldMachO(MM); 00508 break; 00509 case sys::Unknown_FileType: 00510 case sys::Bitcode_FileType: 00511 case sys::Archive_FileType: 00512 case sys::COFF_FileType: 00513 report_fatal_error("Incompatible object format!"); 00514 } 00515 } else { 00516 if (!Dyld->isCompatibleFormat(InputBuffer)) 00517 report_fatal_error("Incompatible object format!"); 00518 } 00519 00520 return Dyld->loadObject(InputBuffer); 00521 } 00522 00523 void *RuntimeDyld::getSymbolAddress(StringRef Name) { 00524 return Dyld->getSymbolAddress(Name); 00525 } 00526 00527 uint64_t RuntimeDyld::getSymbolLoadAddress(StringRef Name) { 00528 return Dyld->getSymbolLoadAddress(Name); 00529 } 00530 00531 void RuntimeDyld::resolveRelocations() { 00532 Dyld->resolveRelocations(); 00533 } 00534 00535 void RuntimeDyld::reassignSectionAddress(unsigned SectionID, 00536 uint64_t Addr) { 00537 Dyld->reassignSectionAddress(SectionID, Addr); 00538 } 00539 00540 void RuntimeDyld::mapSectionAddress(const void *LocalAddress, 00541 uint64_t TargetAddress) { 00542 Dyld->mapSectionAddress(LocalAddress, TargetAddress); 00543 } 00544 00545 StringRef RuntimeDyld::getErrorString() { 00546 return Dyld->getErrorString(); 00547 } 00548 00549 StringRef RuntimeDyld::getEHFrameSection() { 00550 return Dyld->getEHFrameSection(); 00551 } 00552 00553 } // end namespace llvm