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 "RuntimeDyldImpl.h" 00016 #include "RuntimeDyldELF.h" 00017 #include "RuntimeDyldMachO.h" 00018 #include "llvm/Support/Path.h" 00019 00020 using namespace llvm; 00021 using namespace llvm::object; 00022 00023 // Empty out-of-line virtual destructor as the key function. 00024 RTDyldMemoryManager::~RTDyldMemoryManager() {} 00025 RuntimeDyldImpl::~RuntimeDyldImpl() {} 00026 00027 namespace llvm { 00028 00029 namespace { 00030 // Helper for extensive error checking in debug builds. 00031 error_code Check(error_code Err) { 00032 if (Err) { 00033 report_fatal_error(Err.message()); 00034 } 00035 return Err; 00036 } 00037 } // end anonymous namespace 00038 00039 // Resolve the relocations for all symbols we currently know about. 00040 void RuntimeDyldImpl::resolveRelocations() { 00041 // First, resolve relocations associated with external symbols. 00042 resolveExternalSymbols(); 00043 00044 // Just iterate over the sections we have and resolve all the relocations 00045 // in them. Gross overkill, but it gets the job done. 00046 for (int i = 0, e = Sections.size(); i != e; ++i) { 00047 reassignSectionAddress(i, Sections[i].LoadAddress); 00048 } 00049 } 00050 00051 void RuntimeDyldImpl::mapSectionAddress(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 the pointer that is returned. 00064 ObjectImage *RuntimeDyldImpl::createObjectImage(const MemoryBuffer *InputBuffer) { 00065 ObjectFile *ObjFile = ObjectFile::createObjectFile(const_cast<MemoryBuffer*> 00066 (InputBuffer)); 00067 ObjectImage *Obj = new ObjectImage(ObjFile); 00068 return Obj; 00069 } 00070 00071 bool RuntimeDyldImpl::loadObject(const MemoryBuffer *InputBuffer) { 00072 OwningPtr<ObjectImage> obj(createObjectImage(InputBuffer)); 00073 if (!obj) 00074 report_fatal_error("Unable to create object image from memory buffer!"); 00075 00076 Arch = (Triple::ArchType)obj->getArch(); 00077 00078 // Symbols found in this object 00079 StringMap<SymbolLoc> LocalSymbols; 00080 // Used sections from the object file 00081 ObjSectionToIDMap LocalSections; 00082 00083 // Common symbols requiring allocation, and the total size required to 00084 // allocate all common symbols. 00085 CommonSymbolMap CommonSymbols; 00086 uint64_t CommonSize = 0; 00087 00088 error_code err; 00089 // Parse symbols 00090 DEBUG(dbgs() << "Parse symbols:\n"); 00091 for (symbol_iterator i = obj->begin_symbols(), e = obj->end_symbols(); 00092 i != e; i.increment(err)) { 00093 Check(err); 00094 object::SymbolRef::Type SymType; 00095 StringRef Name; 00096 Check(i->getType(SymType)); 00097 Check(i->getName(Name)); 00098 00099 uint32_t flags; 00100 Check(i->getFlags(flags)); 00101 00102 bool isCommon = flags & SymbolRef::SF_Common; 00103 if (isCommon) { 00104 // Add the common symbols to a list. We'll allocate them all below. 00105 uint64_t Size = 0; 00106 Check(i->getSize(Size)); 00107 CommonSize += Size; 00108 CommonSymbols[*i] = Size; 00109 } else { 00110 if (SymType == object::SymbolRef::ST_Function || 00111 SymType == object::SymbolRef::ST_Data) { 00112 uint64_t FileOffset; 00113 StringRef SectionData; 00114 section_iterator si = obj->end_sections(); 00115 Check(i->getFileOffset(FileOffset)); 00116 Check(i->getSection(si)); 00117 if (si == obj->end_sections()) continue; 00118 Check(si->getContents(SectionData)); 00119 const uint8_t* SymPtr = (const uint8_t*)InputBuffer->getBufferStart() + 00120 (uintptr_t)FileOffset; 00121 uintptr_t SectOffset = (uintptr_t)(SymPtr - 00122 (const uint8_t*)SectionData.begin()); 00123 unsigned SectionID = 00124 findOrEmitSection(*obj, 00125 *si, 00126 SymType == object::SymbolRef::ST_Function, 00127 LocalSections); 00128 LocalSymbols[Name.data()] = SymbolLoc(SectionID, SectOffset); 00129 DEBUG(dbgs() << "\tFileOffset: " << format("%p", (uintptr_t)FileOffset) 00130 << " flags: " << flags 00131 << " SID: " << SectionID 00132 << " Offset: " << format("%p", SectOffset)); 00133 bool isGlobal = flags & SymbolRef::SF_Global; 00134 if (isGlobal) 00135 GlobalSymbolTable[Name] = SymbolLoc(SectionID, SectOffset); 00136 } 00137 } 00138 DEBUG(dbgs() << "\tType: " << SymType << " Name: " << Name << "\n"); 00139 } 00140 00141 // Allocate common symbols 00142 if (CommonSize != 0) 00143 emitCommonSymbols(*obj, CommonSymbols, CommonSize, LocalSymbols); 00144 00145 // Parse and process relocations 00146 DEBUG(dbgs() << "Parse relocations:\n"); 00147 for (section_iterator si = obj->begin_sections(), 00148 se = obj->end_sections(); si != se; si.increment(err)) { 00149 Check(err); 00150 bool isFirstRelocation = true; 00151 unsigned SectionID = 0; 00152 StubMap Stubs; 00153 00154 for (relocation_iterator i = si->begin_relocations(), 00155 e = si->end_relocations(); i != e; i.increment(err)) { 00156 Check(err); 00157 00158 // If it's the first relocation in this section, find its SectionID 00159 if (isFirstRelocation) { 00160 SectionID = findOrEmitSection(*obj, *si, true, LocalSections); 00161 DEBUG(dbgs() << "\tSectionID: " << SectionID << "\n"); 00162 isFirstRelocation = false; 00163 } 00164 00165 ObjRelocationInfo RI; 00166 RI.SectionID = SectionID; 00167 Check(i->getAdditionalInfo(RI.AdditionalInfo)); 00168 Check(i->getOffset(RI.Offset)); 00169 Check(i->getSymbol(RI.Symbol)); 00170 Check(i->getType(RI.Type)); 00171 00172 DEBUG(dbgs() << "\t\tAddend: " << RI.AdditionalInfo 00173 << " Offset: " << format("%p", (uintptr_t)RI.Offset) 00174 << " Type: " << (uint32_t)(RI.Type & 0xffffffffL) 00175 << "\n"); 00176 processRelocationRef(RI, *obj, LocalSections, LocalSymbols, Stubs); 00177 } 00178 } 00179 00180 handleObjectLoaded(obj.take()); 00181 00182 return false; 00183 } 00184 00185 void RuntimeDyldImpl::emitCommonSymbols(ObjectImage &Obj, 00186 const CommonSymbolMap &CommonSymbols, 00187 uint64_t TotalSize, 00188 SymbolTableMap &SymbolTable) { 00189 // Allocate memory for the section 00190 unsigned SectionID = Sections.size(); 00191 uint8_t *Addr = MemMgr->allocateDataSection(TotalSize, sizeof(void*), 00192 SectionID); 00193 if (!Addr) 00194 report_fatal_error("Unable to allocate memory for common symbols!"); 00195 uint64_t Offset = 0; 00196 Sections.push_back(SectionEntry(Addr, TotalSize, TotalSize, 0)); 00197 memset(Addr, 0, TotalSize); 00198 00199 DEBUG(dbgs() << "emitCommonSection SectionID: " << SectionID 00200 << " new addr: " << format("%p", Addr) 00201 << " DataSize: " << TotalSize 00202 << "\n"); 00203 00204 // Assign the address of each symbol 00205 for (CommonSymbolMap::const_iterator it = CommonSymbols.begin(), 00206 itEnd = CommonSymbols.end(); it != itEnd; it++) { 00207 StringRef Name; 00208 it->first.getName(Name); 00209 Obj.updateSymbolAddress(it->first, (uint64_t)Addr); 00210 SymbolTable[Name.data()] = SymbolLoc(SectionID, Offset); 00211 uint64_t Size = it->second; 00212 Offset += Size; 00213 Addr += Size; 00214 } 00215 } 00216 00217 unsigned RuntimeDyldImpl::emitSection(ObjectImage &Obj, 00218 const SectionRef &Section, 00219 bool IsCode) { 00220 00221 unsigned StubBufSize = 0, 00222 StubSize = getMaxStubSize(); 00223 error_code err; 00224 if (StubSize > 0) { 00225 for (relocation_iterator i = Section.begin_relocations(), 00226 e = Section.end_relocations(); i != e; i.increment(err), Check(err)) 00227 StubBufSize += StubSize; 00228 } 00229 StringRef data; 00230 uint64_t Alignment64; 00231 Check(Section.getContents(data)); 00232 Check(Section.getAlignment(Alignment64)); 00233 00234 unsigned Alignment = (unsigned)Alignment64 & 0xffffffffL; 00235 bool IsRequired; 00236 bool IsVirtual; 00237 bool IsZeroInit; 00238 uint64_t DataSize; 00239 Check(Section.isRequiredForExecution(IsRequired)); 00240 Check(Section.isVirtual(IsVirtual)); 00241 Check(Section.isZeroInit(IsZeroInit)); 00242 Check(Section.getSize(DataSize)); 00243 00244 unsigned Allocate; 00245 unsigned SectionID = Sections.size(); 00246 uint8_t *Addr; 00247 const char *pData = 0; 00248 00249 // Some sections, such as debug info, don't need to be loaded for execution. 00250 // Leave those where they are. 00251 if (IsRequired) { 00252 Allocate = DataSize + StubBufSize; 00253 Addr = IsCode 00254 ? MemMgr->allocateCodeSection(Allocate, Alignment, SectionID) 00255 : MemMgr->allocateDataSection(Allocate, Alignment, SectionID); 00256 if (!Addr) 00257 report_fatal_error("Unable to allocate section memory!"); 00258 00259 // Virtual sections have no data in the object image, so leave pData = 0 00260 if (!IsVirtual) 00261 pData = data.data(); 00262 00263 // Zero-initialize or copy the data from the image 00264 if (IsZeroInit || IsVirtual) 00265 memset(Addr, 0, DataSize); 00266 else 00267 memcpy(Addr, pData, DataSize); 00268 00269 DEBUG(dbgs() << "emitSection SectionID: " << SectionID 00270 << " obj addr: " << format("%p", pData) 00271 << " new addr: " << format("%p", Addr) 00272 << " DataSize: " << DataSize 00273 << " StubBufSize: " << StubBufSize 00274 << " Allocate: " << Allocate 00275 << "\n"); 00276 Obj.updateSectionAddress(Section, (uint64_t)Addr); 00277 } 00278 else { 00279 // Even if we didn't load the section, we need to record an entry for it 00280 // to handle later processing (and by 'handle' I mean don't do anything 00281 // with these sections). 00282 Allocate = 0; 00283 Addr = 0; 00284 DEBUG(dbgs() << "emitSection SectionID: " << SectionID 00285 << " obj addr: " << format("%p", data.data()) 00286 << " new addr: 0" 00287 << " DataSize: " << DataSize 00288 << " StubBufSize: " << StubBufSize 00289 << " Allocate: " << Allocate 00290 << "\n"); 00291 } 00292 00293 Sections.push_back(SectionEntry(Addr, Allocate, DataSize,(uintptr_t)pData)); 00294 return SectionID; 00295 } 00296 00297 unsigned RuntimeDyldImpl::findOrEmitSection(ObjectImage &Obj, 00298 const SectionRef &Section, 00299 bool IsCode, 00300 ObjSectionToIDMap &LocalSections) { 00301 00302 unsigned SectionID = 0; 00303 ObjSectionToIDMap::iterator i = LocalSections.find(Section); 00304 if (i != LocalSections.end()) 00305 SectionID = i->second; 00306 else { 00307 SectionID = emitSection(Obj, Section, IsCode); 00308 LocalSections[Section] = SectionID; 00309 } 00310 return SectionID; 00311 } 00312 00313 void RuntimeDyldImpl::addRelocationForSection(const RelocationEntry &RE, 00314 unsigned SectionID) { 00315 Relocations[SectionID].push_back(RE); 00316 } 00317 00318 void RuntimeDyldImpl::addRelocationForSymbol(const RelocationEntry &RE, 00319 StringRef SymbolName) { 00320 // Relocation by symbol. If the symbol is found in the global symbol table, 00321 // create an appropriate section relocation. Otherwise, add it to 00322 // ExternalSymbolRelocations. 00323 SymbolTableMap::const_iterator Loc = 00324 GlobalSymbolTable.find(SymbolName); 00325 if (Loc == GlobalSymbolTable.end()) { 00326 ExternalSymbolRelocations[SymbolName].push_back(RE); 00327 } else { 00328 // Copy the RE since we want to modify its addend. 00329 RelocationEntry RECopy = RE; 00330 RECopy.Addend += Loc->second.second; 00331 Relocations[Loc->second.first].push_back(RECopy); 00332 } 00333 } 00334 00335 uint8_t *RuntimeDyldImpl::createStubFunction(uint8_t *Addr) { 00336 // TODO: There is only ARM far stub now. We should add the Thumb stub, 00337 // and stubs for branches Thumb - ARM and ARM - Thumb. 00338 if (Arch == Triple::arm) { 00339 uint32_t *StubAddr = (uint32_t*)Addr; 00340 *StubAddr = 0xe51ff004; // ldr pc,<label> 00341 return (uint8_t*)++StubAddr; 00342 } 00343 else 00344 return Addr; 00345 } 00346 00347 // Assign an address to a symbol name and resolve all the relocations 00348 // associated with it. 00349 void RuntimeDyldImpl::reassignSectionAddress(unsigned SectionID, 00350 uint64_t Addr) { 00351 // The address to use for relocation resolution is not 00352 // the address of the local section buffer. We must be doing 00353 // a remote execution environment of some sort. Re-apply any 00354 // relocations referencing this section with the given address. 00355 // 00356 // Addr is a uint64_t because we can't assume the pointer width 00357 // of the target is the same as that of the host. Just use a generic 00358 // "big enough" type. 00359 Sections[SectionID].LoadAddress = Addr; 00360 DEBUG(dbgs() << "Resolving relocations Section #" << SectionID 00361 << "\t" << format("%p", (uint8_t *)Addr) 00362 << "\n"); 00363 resolveRelocationList(Relocations[SectionID], Addr); 00364 } 00365 00366 void RuntimeDyldImpl::resolveRelocationEntry(const RelocationEntry &RE, 00367 uint64_t Value) { 00368 // Ignore relocations for sections that were not loaded 00369 if (Sections[RE.SectionID].Address != 0) { 00370 uint8_t *Target = Sections[RE.SectionID].Address + RE.Offset; 00371 DEBUG(dbgs() << "\tSectionID: " << RE.SectionID 00372 << " + " << RE.Offset << " (" << format("%p", Target) << ")" 00373 << " RelType: " << RE.RelType 00374 << " Addend: " << RE.Addend 00375 << "\n"); 00376 00377 resolveRelocation(Target, Sections[RE.SectionID].LoadAddress + RE.Offset, 00378 Value, RE.RelType, RE.Addend); 00379 } 00380 } 00381 00382 void RuntimeDyldImpl::resolveRelocationList(const RelocationList &Relocs, 00383 uint64_t Value) { 00384 for (unsigned i = 0, e = Relocs.size(); i != e; ++i) { 00385 resolveRelocationEntry(Relocs[i], Value); 00386 } 00387 } 00388 00389 void RuntimeDyldImpl::resolveExternalSymbols() { 00390 StringMap<RelocationList>::iterator i = ExternalSymbolRelocations.begin(), 00391 e = ExternalSymbolRelocations.end(); 00392 for (; i != e; i++) { 00393 StringRef Name = i->first(); 00394 RelocationList &Relocs = i->second; 00395 SymbolTableMap::const_iterator Loc = GlobalSymbolTable.find(Name); 00396 if (Loc == GlobalSymbolTable.end()) { 00397 // This is an external symbol, try to get it address from 00398 // MemoryManager. 00399 uint8_t *Addr = (uint8_t*) MemMgr->getPointerToNamedFunction(Name.data(), 00400 true); 00401 DEBUG(dbgs() << "Resolving relocations Name: " << Name 00402 << "\t" << format("%p", Addr) 00403 << "\n"); 00404 resolveRelocationList(Relocs, (uintptr_t)Addr); 00405 } else { 00406 report_fatal_error("Expected external symbol"); 00407 } 00408 } 00409 } 00410 00411 00412 //===----------------------------------------------------------------------===// 00413 // RuntimeDyld class implementation 00414 RuntimeDyld::RuntimeDyld(RTDyldMemoryManager *mm) { 00415 Dyld = 0; 00416 MM = mm; 00417 } 00418 00419 RuntimeDyld::~RuntimeDyld() { 00420 delete Dyld; 00421 } 00422 00423 bool RuntimeDyld::loadObject(MemoryBuffer *InputBuffer) { 00424 if (!Dyld) { 00425 sys::LLVMFileType type = sys::IdentifyFileType( 00426 InputBuffer->getBufferStart(), 00427 static_cast<unsigned>(InputBuffer->getBufferSize())); 00428 switch (type) { 00429 case sys::ELF_Relocatable_FileType: 00430 case sys::ELF_Executable_FileType: 00431 case sys::ELF_SharedObject_FileType: 00432 case sys::ELF_Core_FileType: 00433 Dyld = new RuntimeDyldELF(MM); 00434 break; 00435 case sys::Mach_O_Object_FileType: 00436 case sys::Mach_O_Executable_FileType: 00437 case sys::Mach_O_FixedVirtualMemorySharedLib_FileType: 00438 case sys::Mach_O_Core_FileType: 00439 case sys::Mach_O_PreloadExecutable_FileType: 00440 case sys::Mach_O_DynamicallyLinkedSharedLib_FileType: 00441 case sys::Mach_O_DynamicLinker_FileType: 00442 case sys::Mach_O_Bundle_FileType: 00443 case sys::Mach_O_DynamicallyLinkedSharedLibStub_FileType: 00444 case sys::Mach_O_DSYMCompanion_FileType: 00445 Dyld = new RuntimeDyldMachO(MM); 00446 break; 00447 case sys::Unknown_FileType: 00448 case sys::Bitcode_FileType: 00449 case sys::Archive_FileType: 00450 case sys::COFF_FileType: 00451 report_fatal_error("Incompatible object format!"); 00452 } 00453 } else { 00454 if (!Dyld->isCompatibleFormat(InputBuffer)) 00455 report_fatal_error("Incompatible object format!"); 00456 } 00457 00458 return Dyld->loadObject(InputBuffer); 00459 } 00460 00461 void *RuntimeDyld::getSymbolAddress(StringRef Name) { 00462 return Dyld->getSymbolAddress(Name); 00463 } 00464 00465 void RuntimeDyld::resolveRelocations() { 00466 Dyld->resolveRelocations(); 00467 } 00468 00469 void RuntimeDyld::reassignSectionAddress(unsigned SectionID, 00470 uint64_t Addr) { 00471 Dyld->reassignSectionAddress(SectionID, Addr); 00472 } 00473 00474 void RuntimeDyld::mapSectionAddress(void *LocalAddress, 00475 uint64_t TargetAddress) { 00476 Dyld->mapSectionAddress(LocalAddress, TargetAddress); 00477 } 00478 00479 StringRef RuntimeDyld::getErrorString() { 00480 return Dyld->getErrorString(); 00481 } 00482 00483 } // end namespace llvm