1 //===-- RuntimeDyld.cpp - Run-time dynamic linker for MC-JIT ----*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Implementation of the MC-JIT runtime dynamic linker.
12 //===----------------------------------------------------------------------===//
14 #define DEBUG_TYPE "dyld"
15 #include "llvm/ExecutionEngine/RuntimeDyld.h"
16 #include "ObjectImageCommon.h"
17 #include "RuntimeDyldELF.h"
18 #include "RuntimeDyldImpl.h"
19 #include "RuntimeDyldMachO.h"
20 #include "llvm/Support/MathExtras.h"
21 #include "llvm/Support/Path.h"
24 using namespace llvm::object;
26 // Empty out-of-line virtual destructor as the key function.
27 RTDyldMemoryManager::~RTDyldMemoryManager() {}
28 RuntimeDyldImpl::~RuntimeDyldImpl() {}
32 // Resolve the relocations for all symbols we currently know about.
33 void RuntimeDyldImpl::resolveRelocations() {
34 // First, resolve relocations associated with external symbols.
35 resolveExternalSymbols();
37 // Just iterate over the sections we have and resolve all the relocations
38 // in them. Gross overkill, but it gets the job done.
39 for (int i = 0, e = Sections.size(); i != e; ++i) {
40 uint64_t Addr = Sections[i].LoadAddress;
41 DEBUG(dbgs() << "Resolving relocations Section #" << i
42 << "\t" << format("%p", (uint8_t *)Addr)
44 resolveRelocationList(Relocations[i], Addr);
48 void RuntimeDyldImpl::mapSectionAddress(const void *LocalAddress,
49 uint64_t TargetAddress) {
50 for (unsigned i = 0, e = Sections.size(); i != e; ++i) {
51 if (Sections[i].Address == LocalAddress) {
52 reassignSectionAddress(i, TargetAddress);
56 llvm_unreachable("Attempting to remap address of unknown section!");
59 // Subclasses can implement this method to create specialized image instances.
60 // The caller owns the pointer that is returned.
61 ObjectImage *RuntimeDyldImpl::createObjectImage(ObjectBuffer *InputBuffer) {
62 return new ObjectImageCommon(InputBuffer);
65 ObjectImage *RuntimeDyldImpl::loadObject(ObjectBuffer *InputBuffer) {
66 OwningPtr<ObjectImage> obj(createObjectImage(InputBuffer));
68 report_fatal_error("Unable to create object image from memory buffer!");
70 Arch = (Triple::ArchType)obj->getArch();
72 // Symbols found in this object
73 StringMap<SymbolLoc> LocalSymbols;
74 // Used sections from the object file
75 ObjSectionToIDMap LocalSections;
77 // Common symbols requiring allocation, with their sizes and alignments
78 CommonSymbolMap CommonSymbols;
79 // Maximum required total memory to allocate all common symbols
80 uint64_t CommonSize = 0;
84 DEBUG(dbgs() << "Parse symbols:\n");
85 for (symbol_iterator i = obj->begin_symbols(), e = obj->end_symbols();
86 i != e; i.increment(err)) {
88 object::SymbolRef::Type SymType;
90 Check(i->getType(SymType));
91 Check(i->getName(Name));
94 Check(i->getFlags(flags));
96 bool isCommon = flags & SymbolRef::SF_Common;
98 // Add the common symbols to a list. We'll allocate them all below.
99 uint64_t Align = getCommonSymbolAlignment(*i);
101 Check(i->getSize(Size));
102 CommonSize += Size + Align;
103 CommonSymbols[*i] = CommonSymbolInfo(Size, Align);
105 if (SymType == object::SymbolRef::ST_Function ||
106 SymType == object::SymbolRef::ST_Data ||
107 SymType == object::SymbolRef::ST_Unknown) {
109 StringRef SectionData;
111 section_iterator si = obj->end_sections();
112 Check(i->getFileOffset(FileOffset));
113 Check(i->getSection(si));
114 if (si == obj->end_sections()) continue;
115 Check(si->getContents(SectionData));
116 Check(si->isText(IsCode));
117 const uint8_t* SymPtr = (const uint8_t*)InputBuffer->getBufferStart() +
118 (uintptr_t)FileOffset;
119 uintptr_t SectOffset = (uintptr_t)(SymPtr -
120 (const uint8_t*)SectionData.begin());
121 unsigned SectionID = findOrEmitSection(*obj, *si, IsCode, LocalSections);
122 LocalSymbols[Name.data()] = SymbolLoc(SectionID, SectOffset);
123 DEBUG(dbgs() << "\tFileOffset: " << format("%p", (uintptr_t)FileOffset)
124 << " flags: " << flags
125 << " SID: " << SectionID
126 << " Offset: " << format("%p", SectOffset));
127 GlobalSymbolTable[Name] = SymbolLoc(SectionID, SectOffset);
130 DEBUG(dbgs() << "\tType: " << SymType << " Name: " << Name << "\n");
133 // Allocate common symbols
135 emitCommonSymbols(*obj, CommonSymbols, CommonSize, LocalSymbols);
137 // Parse and process relocations
138 DEBUG(dbgs() << "Parse relocations:\n");
139 for (section_iterator si = obj->begin_sections(),
140 se = obj->end_sections(); si != se; si.increment(err)) {
142 bool isFirstRelocation = true;
143 unsigned SectionID = 0;
146 for (relocation_iterator i = si->begin_relocations(),
147 e = si->end_relocations(); i != e; i.increment(err)) {
150 // If it's the first relocation in this section, find its SectionID
151 if (isFirstRelocation) {
152 SectionID = findOrEmitSection(*obj, *si, true, LocalSections);
153 DEBUG(dbgs() << "\tSectionID: " << SectionID << "\n");
154 isFirstRelocation = false;
157 ObjRelocationInfo RI;
158 RI.SectionID = SectionID;
159 Check(i->getAdditionalInfo(RI.AdditionalInfo));
160 Check(i->getOffset(RI.Offset));
161 Check(i->getSymbol(RI.Symbol));
162 Check(i->getType(RI.Type));
164 DEBUG(dbgs() << "\t\tAddend: " << RI.AdditionalInfo
165 << " Offset: " << format("%p", (uintptr_t)RI.Offset)
166 << " Type: " << (uint32_t)(RI.Type & 0xffffffffL)
168 processRelocationRef(RI, *obj, LocalSections, LocalSymbols, Stubs);
175 void RuntimeDyldImpl::emitCommonSymbols(ObjectImage &Obj,
176 const CommonSymbolMap &CommonSymbols,
178 SymbolTableMap &SymbolTable) {
179 // Allocate memory for the section
180 unsigned SectionID = Sections.size();
181 uint8_t *Addr = MemMgr->allocateDataSection(TotalSize, sizeof(void*),
184 report_fatal_error("Unable to allocate memory for common symbols!");
186 Sections.push_back(SectionEntry(StringRef(), Addr, TotalSize, TotalSize, 0));
187 memset(Addr, 0, TotalSize);
189 DEBUG(dbgs() << "emitCommonSection SectionID: " << SectionID
190 << " new addr: " << format("%p", Addr)
191 << " DataSize: " << TotalSize
194 // Assign the address of each symbol
195 for (CommonSymbolMap::const_iterator it = CommonSymbols.begin(),
196 itEnd = CommonSymbols.end(); it != itEnd; it++) {
197 uint64_t Size = it->second.first;
198 uint64_t Align = it->second.second;
200 it->first.getName(Name);
202 // This symbol has an alignment requirement.
203 uint64_t AlignOffset = OffsetToAlignment((uint64_t)Addr, Align);
205 Offset += AlignOffset;
206 DEBUG(dbgs() << "Allocating common symbol " << Name << " address " <<
207 format("%p\n", Addr));
209 Obj.updateSymbolAddress(it->first, (uint64_t)Addr);
210 SymbolTable[Name.data()] = SymbolLoc(SectionID, Offset);
216 unsigned RuntimeDyldImpl::emitSection(ObjectImage &Obj,
217 const SectionRef &Section,
220 unsigned StubBufSize = 0,
221 StubSize = getMaxStubSize();
224 for (relocation_iterator i = Section.begin_relocations(),
225 e = Section.end_relocations(); i != e; i.increment(err), Check(err))
226 StubBufSize += StubSize;
229 uint64_t Alignment64;
230 Check(Section.getContents(data));
231 Check(Section.getAlignment(Alignment64));
233 unsigned Alignment = (unsigned)Alignment64 & 0xffffffffL;
240 Check(Section.isRequiredForExecution(IsRequired));
241 Check(Section.isVirtual(IsVirtual));
242 Check(Section.isZeroInit(IsZeroInit));
243 Check(Section.isReadOnlyData(IsReadOnly));
244 Check(Section.getSize(DataSize));
245 Check(Section.getName(Name));
248 unsigned SectionID = Sections.size();
250 const char *pData = 0;
252 // Some sections, such as debug info, don't need to be loaded for execution.
253 // Leave those where they are.
255 Allocate = DataSize + StubBufSize;
257 ? MemMgr->allocateCodeSection(Allocate, Alignment, SectionID)
258 : MemMgr->allocateDataSection(Allocate, Alignment, SectionID, IsReadOnly);
260 report_fatal_error("Unable to allocate section memory!");
262 // Virtual sections have no data in the object image, so leave pData = 0
266 // Zero-initialize or copy the data from the image
267 if (IsZeroInit || IsVirtual)
268 memset(Addr, 0, DataSize);
270 memcpy(Addr, pData, DataSize);
272 DEBUG(dbgs() << "emitSection SectionID: " << SectionID
274 << " obj addr: " << format("%p", pData)
275 << " new addr: " << format("%p", Addr)
276 << " DataSize: " << DataSize
277 << " StubBufSize: " << StubBufSize
278 << " Allocate: " << Allocate
280 Obj.updateSectionAddress(Section, (uint64_t)Addr);
283 // Even if we didn't load the section, we need to record an entry for it
284 // to handle later processing (and by 'handle' I mean don't do anything
285 // with these sections).
288 DEBUG(dbgs() << "emitSection SectionID: " << SectionID
290 << " obj addr: " << format("%p", data.data())
292 << " DataSize: " << DataSize
293 << " StubBufSize: " << StubBufSize
294 << " Allocate: " << Allocate
298 Sections.push_back(SectionEntry(Name, Addr, Allocate, DataSize,
303 unsigned RuntimeDyldImpl::findOrEmitSection(ObjectImage &Obj,
304 const SectionRef &Section,
306 ObjSectionToIDMap &LocalSections) {
308 unsigned SectionID = 0;
309 ObjSectionToIDMap::iterator i = LocalSections.find(Section);
310 if (i != LocalSections.end())
311 SectionID = i->second;
313 SectionID = emitSection(Obj, Section, IsCode);
314 LocalSections[Section] = SectionID;
319 void RuntimeDyldImpl::addRelocationForSection(const RelocationEntry &RE,
320 unsigned SectionID) {
321 Relocations[SectionID].push_back(RE);
324 void RuntimeDyldImpl::addRelocationForSymbol(const RelocationEntry &RE,
325 StringRef SymbolName) {
326 // Relocation by symbol. If the symbol is found in the global symbol table,
327 // create an appropriate section relocation. Otherwise, add it to
328 // ExternalSymbolRelocations.
329 SymbolTableMap::const_iterator Loc =
330 GlobalSymbolTable.find(SymbolName);
331 if (Loc == GlobalSymbolTable.end()) {
332 ExternalSymbolRelocations[SymbolName].push_back(RE);
334 // Copy the RE since we want to modify its addend.
335 RelocationEntry RECopy = RE;
336 RECopy.Addend += Loc->second.second;
337 Relocations[Loc->second.first].push_back(RECopy);
341 uint8_t *RuntimeDyldImpl::createStubFunction(uint8_t *Addr) {
342 if (Arch == Triple::arm) {
343 // TODO: There is only ARM far stub now. We should add the Thumb stub,
344 // and stubs for branches Thumb - ARM and ARM - Thumb.
345 uint32_t *StubAddr = (uint32_t*)Addr;
346 *StubAddr = 0xe51ff004; // ldr pc,<label>
347 return (uint8_t*)++StubAddr;
348 } else if (Arch == Triple::mipsel || Arch == Triple::mips) {
349 uint32_t *StubAddr = (uint32_t*)Addr;
350 // 0: 3c190000 lui t9,%hi(addr).
351 // 4: 27390000 addiu t9,t9,%lo(addr).
352 // 8: 03200008 jr t9.
354 const unsigned LuiT9Instr = 0x3c190000, AdduiT9Instr = 0x27390000;
355 const unsigned JrT9Instr = 0x03200008, NopInstr = 0x0;
357 *StubAddr = LuiT9Instr;
359 *StubAddr = AdduiT9Instr;
361 *StubAddr = JrT9Instr;
363 *StubAddr = NopInstr;
365 } else if (Arch == Triple::ppc64) {
366 // PowerPC64 stub: the address points to a function descriptor
367 // instead of the function itself. Load the function address
368 // on r11 and sets it to control register. Also loads the function
369 // TOC in r2 and environment pointer to r11.
370 writeInt32BE(Addr, 0x3D800000); // lis r12, highest(addr)
371 writeInt32BE(Addr+4, 0x618C0000); // ori r12, higher(addr)
372 writeInt32BE(Addr+8, 0x798C07C6); // sldi r12, r12, 32
373 writeInt32BE(Addr+12, 0x658C0000); // oris r12, r12, h(addr)
374 writeInt32BE(Addr+16, 0x618C0000); // ori r12, r12, l(addr)
375 writeInt32BE(Addr+20, 0xF8410028); // std r2, 40(r1)
376 writeInt32BE(Addr+24, 0xE96C0000); // ld r11, 0(r12)
377 writeInt32BE(Addr+28, 0xE84C0008); // ld r2, 0(r12)
378 writeInt32BE(Addr+32, 0x7D6903A6); // mtctr r11
379 writeInt32BE(Addr+36, 0xE96C0010); // ld r11, 16(r2)
380 writeInt32BE(Addr+40, 0x4E800420); // bctr
387 // Assign an address to a symbol name and resolve all the relocations
388 // associated with it.
389 void RuntimeDyldImpl::reassignSectionAddress(unsigned SectionID,
391 // The address to use for relocation resolution is not
392 // the address of the local section buffer. We must be doing
393 // a remote execution environment of some sort. Relocations can't
394 // be applied until all the sections have been moved. The client must
395 // trigger this with a call to MCJIT::finalize() or
396 // RuntimeDyld::resolveRelocations().
398 // Addr is a uint64_t because we can't assume the pointer width
399 // of the target is the same as that of the host. Just use a generic
400 // "big enough" type.
401 Sections[SectionID].LoadAddress = Addr;
404 void RuntimeDyldImpl::resolveRelocationEntry(const RelocationEntry &RE,
406 // Ignore relocations for sections that were not loaded
407 if (Sections[RE.SectionID].Address != 0) {
408 DEBUG(dbgs() << "\tSectionID: " << RE.SectionID
409 << " + " << RE.Offset << " ("
410 << format("%p", Sections[RE.SectionID].Address + RE.Offset) << ")"
411 << " RelType: " << RE.RelType
412 << " Addend: " << RE.Addend
415 resolveRelocation(Sections[RE.SectionID], RE.Offset,
416 Value, RE.RelType, RE.Addend);
420 void RuntimeDyldImpl::resolveRelocationList(const RelocationList &Relocs,
422 for (unsigned i = 0, e = Relocs.size(); i != e; ++i) {
423 resolveRelocationEntry(Relocs[i], Value);
427 void RuntimeDyldImpl::resolveExternalSymbols() {
428 StringMap<RelocationList>::iterator i = ExternalSymbolRelocations.begin(),
429 e = ExternalSymbolRelocations.end();
430 for (; i != e; i++) {
431 StringRef Name = i->first();
432 RelocationList &Relocs = i->second;
433 SymbolTableMap::const_iterator Loc = GlobalSymbolTable.find(Name);
434 if (Loc == GlobalSymbolTable.end()) {
435 if (Name.size() == 0) {
436 // This is an absolute symbol, use an address of zero.
437 DEBUG(dbgs() << "Resolving absolute relocations." << "\n");
438 resolveRelocationList(Relocs, 0);
441 // This is an external symbol, try to get it address from
443 uint8_t *Addr = (uint8_t*) MemMgr->getPointerToNamedFunction(Name.data(),
445 DEBUG(dbgs() << "Resolving relocations Name: " << Name
446 << "\t" << format("%p", Addr)
448 resolveRelocationList(Relocs, (uintptr_t)Addr);
451 report_fatal_error("Expected external symbol");
457 //===----------------------------------------------------------------------===//
458 // RuntimeDyld class implementation
459 RuntimeDyld::RuntimeDyld(RTDyldMemoryManager *mm) {
460 // FIXME: There's a potential issue lurking here if a single instance of
461 // RuntimeDyld is used to load multiple objects. The current implementation
462 // associates a single memory manager with a RuntimeDyld instance. Even
463 // though the public class spawns a new 'impl' instance for each load,
464 // they share a single memory manager. This can become a problem when page
465 // permissions are applied.
470 RuntimeDyld::~RuntimeDyld() {
474 ObjectImage *RuntimeDyld::loadObject(ObjectBuffer *InputBuffer) {
476 sys::LLVMFileType type = sys::IdentifyFileType(
477 InputBuffer->getBufferStart(),
478 static_cast<unsigned>(InputBuffer->getBufferSize()));
480 case sys::ELF_Relocatable_FileType:
481 case sys::ELF_Executable_FileType:
482 case sys::ELF_SharedObject_FileType:
483 case sys::ELF_Core_FileType:
484 Dyld = new RuntimeDyldELF(MM);
486 case sys::Mach_O_Object_FileType:
487 case sys::Mach_O_Executable_FileType:
488 case sys::Mach_O_FixedVirtualMemorySharedLib_FileType:
489 case sys::Mach_O_Core_FileType:
490 case sys::Mach_O_PreloadExecutable_FileType:
491 case sys::Mach_O_DynamicallyLinkedSharedLib_FileType:
492 case sys::Mach_O_DynamicLinker_FileType:
493 case sys::Mach_O_Bundle_FileType:
494 case sys::Mach_O_DynamicallyLinkedSharedLibStub_FileType:
495 case sys::Mach_O_DSYMCompanion_FileType:
496 Dyld = new RuntimeDyldMachO(MM);
498 case sys::Unknown_FileType:
499 case sys::Bitcode_FileType:
500 case sys::Archive_FileType:
501 case sys::COFF_FileType:
502 report_fatal_error("Incompatible object format!");
505 if (!Dyld->isCompatibleFormat(InputBuffer))
506 report_fatal_error("Incompatible object format!");
509 return Dyld->loadObject(InputBuffer);
512 void *RuntimeDyld::getSymbolAddress(StringRef Name) {
513 return Dyld->getSymbolAddress(Name);
516 uint64_t RuntimeDyld::getSymbolLoadAddress(StringRef Name) {
517 return Dyld->getSymbolLoadAddress(Name);
520 void RuntimeDyld::resolveRelocations() {
521 Dyld->resolveRelocations();
524 void RuntimeDyld::reassignSectionAddress(unsigned SectionID,
526 Dyld->reassignSectionAddress(SectionID, Addr);
529 void RuntimeDyld::mapSectionAddress(const void *LocalAddress,
530 uint64_t TargetAddress) {
531 Dyld->mapSectionAddress(LocalAddress, TargetAddress);
534 StringRef RuntimeDyld::getErrorString() {
535 return Dyld->getErrorString();
538 } // end namespace llvm