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 RuntimeDyldImpl::~RuntimeDyldImpl() {}
31 StringRef RuntimeDyldImpl::getEHFrameSection() {
35 // Resolve the relocations for all symbols we currently know about.
36 void RuntimeDyldImpl::resolveRelocations() {
37 // First, resolve relocations associated with external symbols.
38 resolveExternalSymbols();
40 // Just iterate over the sections we have and resolve all the relocations
41 // in them. Gross overkill, but it gets the job done.
42 for (int i = 0, e = Sections.size(); i != e; ++i) {
43 uint64_t Addr = Sections[i].LoadAddress;
44 DEBUG(dbgs() << "Resolving relocations Section #" << i
45 << "\t" << format("%p", (uint8_t *)Addr)
47 resolveRelocationList(Relocations[i], Addr);
51 void RuntimeDyldImpl::mapSectionAddress(const void *LocalAddress,
52 uint64_t TargetAddress) {
53 for (unsigned i = 0, e = Sections.size(); i != e; ++i) {
54 if (Sections[i].Address == LocalAddress) {
55 reassignSectionAddress(i, TargetAddress);
59 llvm_unreachable("Attempting to remap address of unknown section!");
62 // Subclasses can implement this method to create specialized image instances.
63 // The caller owns the pointer that is returned.
64 ObjectImage *RuntimeDyldImpl::createObjectImage(ObjectBuffer *InputBuffer) {
65 return new ObjectImageCommon(InputBuffer);
68 ObjectImage *RuntimeDyldImpl::loadObject(ObjectBuffer *InputBuffer) {
69 OwningPtr<ObjectImage> obj(createObjectImage(InputBuffer));
71 report_fatal_error("Unable to create object image from memory buffer!");
73 Arch = (Triple::ArchType)obj->getArch();
75 // Symbols found in this object
76 StringMap<SymbolLoc> LocalSymbols;
77 // Used sections from the object file
78 ObjSectionToIDMap LocalSections;
80 // Common symbols requiring allocation, with their sizes and alignments
81 CommonSymbolMap CommonSymbols;
82 // Maximum required total memory to allocate all common symbols
83 uint64_t CommonSize = 0;
87 DEBUG(dbgs() << "Parse symbols:\n");
88 for (symbol_iterator i = obj->begin_symbols(), e = obj->end_symbols();
89 i != e; i.increment(err)) {
91 object::SymbolRef::Type SymType;
93 Check(i->getType(SymType));
94 Check(i->getName(Name));
97 Check(i->getFlags(flags));
99 bool isCommon = flags & SymbolRef::SF_Common;
101 // Add the common symbols to a list. We'll allocate them all below.
103 Check(i->getAlignment(Align));
105 Check(i->getSize(Size));
106 CommonSize += Size + Align;
107 CommonSymbols[*i] = CommonSymbolInfo(Size, Align);
109 if (SymType == object::SymbolRef::ST_Function ||
110 SymType == object::SymbolRef::ST_Data ||
111 SymType == object::SymbolRef::ST_Unknown) {
113 StringRef SectionData;
115 section_iterator si = obj->end_sections();
116 Check(i->getFileOffset(FileOffset));
117 Check(i->getSection(si));
118 if (si == obj->end_sections()) continue;
119 Check(si->getContents(SectionData));
120 Check(si->isText(IsCode));
121 const uint8_t* SymPtr = (const uint8_t*)InputBuffer->getBufferStart() +
122 (uintptr_t)FileOffset;
123 uintptr_t SectOffset = (uintptr_t)(SymPtr -
124 (const uint8_t*)SectionData.begin());
125 unsigned SectionID = findOrEmitSection(*obj, *si, IsCode, LocalSections);
126 LocalSymbols[Name.data()] = SymbolLoc(SectionID, SectOffset);
127 DEBUG(dbgs() << "\tFileOffset: " << format("%p", (uintptr_t)FileOffset)
128 << " flags: " << flags
129 << " SID: " << SectionID
130 << " Offset: " << format("%p", SectOffset));
131 GlobalSymbolTable[Name] = SymbolLoc(SectionID, SectOffset);
134 DEBUG(dbgs() << "\tType: " << SymType << " Name: " << Name << "\n");
137 // Allocate common symbols
139 emitCommonSymbols(*obj, CommonSymbols, CommonSize, LocalSymbols);
141 // Parse and process relocations
142 DEBUG(dbgs() << "Parse relocations:\n");
143 for (section_iterator si = obj->begin_sections(),
144 se = obj->end_sections(); si != se; si.increment(err)) {
146 bool isFirstRelocation = true;
147 unsigned SectionID = 0;
150 for (relocation_iterator i = si->begin_relocations(),
151 e = si->end_relocations(); i != e; i.increment(err)) {
154 // If it's the first relocation in this section, find its SectionID
155 if (isFirstRelocation) {
156 SectionID = findOrEmitSection(*obj, *si, true, LocalSections);
157 DEBUG(dbgs() << "\tSectionID: " << SectionID << "\n");
158 isFirstRelocation = false;
161 processRelocationRef(SectionID, *i, *obj, LocalSections, LocalSymbols,
169 void RuntimeDyldImpl::emitCommonSymbols(ObjectImage &Obj,
170 const CommonSymbolMap &CommonSymbols,
172 SymbolTableMap &SymbolTable) {
173 // Allocate memory for the section
174 unsigned SectionID = Sections.size();
175 uint8_t *Addr = MemMgr->allocateDataSection(TotalSize, sizeof(void*),
178 report_fatal_error("Unable to allocate memory for common symbols!");
180 Sections.push_back(SectionEntry(StringRef(), Addr, TotalSize, 0));
181 memset(Addr, 0, TotalSize);
183 DEBUG(dbgs() << "emitCommonSection SectionID: " << SectionID
184 << " new addr: " << format("%p", Addr)
185 << " DataSize: " << TotalSize
188 // Assign the address of each symbol
189 for (CommonSymbolMap::const_iterator it = CommonSymbols.begin(),
190 itEnd = CommonSymbols.end(); it != itEnd; it++) {
191 uint64_t Size = it->second.first;
192 uint64_t Align = it->second.second;
194 it->first.getName(Name);
196 // This symbol has an alignment requirement.
197 uint64_t AlignOffset = OffsetToAlignment((uint64_t)Addr, Align);
199 Offset += AlignOffset;
200 DEBUG(dbgs() << "Allocating common symbol " << Name << " address " <<
201 format("%p\n", Addr));
203 Obj.updateSymbolAddress(it->first, (uint64_t)Addr);
204 SymbolTable[Name.data()] = SymbolLoc(SectionID, Offset);
210 unsigned RuntimeDyldImpl::emitSection(ObjectImage &Obj,
211 const SectionRef &Section,
214 unsigned StubBufSize = 0,
215 StubSize = getMaxStubSize();
218 for (relocation_iterator i = Section.begin_relocations(),
219 e = Section.end_relocations(); i != e; i.increment(err), Check(err))
220 StubBufSize += StubSize;
223 uint64_t Alignment64;
224 Check(Section.getContents(data));
225 Check(Section.getAlignment(Alignment64));
227 unsigned Alignment = (unsigned)Alignment64 & 0xffffffffL;
234 Check(Section.isRequiredForExecution(IsRequired));
235 Check(Section.isVirtual(IsVirtual));
236 Check(Section.isZeroInit(IsZeroInit));
237 Check(Section.isReadOnlyData(IsReadOnly));
238 Check(Section.getSize(DataSize));
239 Check(Section.getName(Name));
241 unsigned StubAlignment = getStubAlignment();
242 unsigned EndAlignment = (DataSize | Alignment) & -(DataSize | Alignment);
243 if (StubAlignment > EndAlignment)
244 StubBufSize += StubAlignment - EndAlignment;
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, DataSize, (uintptr_t)pData));
302 unsigned RuntimeDyldImpl::findOrEmitSection(ObjectImage &Obj,
303 const SectionRef &Section,
305 ObjSectionToIDMap &LocalSections) {
307 unsigned SectionID = 0;
308 ObjSectionToIDMap::iterator i = LocalSections.find(Section);
309 if (i != LocalSections.end())
310 SectionID = i->second;
312 SectionID = emitSection(Obj, Section, IsCode);
313 LocalSections[Section] = SectionID;
318 void RuntimeDyldImpl::addRelocationForSection(const RelocationEntry &RE,
319 unsigned SectionID) {
320 Relocations[SectionID].push_back(RE);
323 void RuntimeDyldImpl::addRelocationForSymbol(const RelocationEntry &RE,
324 StringRef SymbolName) {
325 // Relocation by symbol. If the symbol is found in the global symbol table,
326 // create an appropriate section relocation. Otherwise, add it to
327 // ExternalSymbolRelocations.
328 SymbolTableMap::const_iterator Loc =
329 GlobalSymbolTable.find(SymbolName);
330 if (Loc == GlobalSymbolTable.end()) {
331 ExternalSymbolRelocations[SymbolName].push_back(RE);
333 // Copy the RE since we want to modify its addend.
334 RelocationEntry RECopy = RE;
335 RECopy.Addend += Loc->second.second;
336 Relocations[Loc->second.first].push_back(RECopy);
340 uint8_t *RuntimeDyldImpl::createStubFunction(uint8_t *Addr) {
341 if (Arch == Triple::aarch64) {
342 // This stub has to be able to access the full address space,
343 // since symbol lookup won't necessarily find a handy, in-range,
344 // PLT stub for functions which could be anywhere.
345 uint32_t *StubAddr = (uint32_t*)Addr;
347 // Stub can use ip0 (== x16) to calculate address
348 *StubAddr = 0xd2e00010; // movz ip0, #:abs_g3:<addr>
350 *StubAddr = 0xf2c00010; // movk ip0, #:abs_g2_nc:<addr>
352 *StubAddr = 0xf2a00010; // movk ip0, #:abs_g1_nc:<addr>
354 *StubAddr = 0xf2800010; // movk ip0, #:abs_g0_nc:<addr>
356 *StubAddr = 0xd61f0200; // br ip0
359 } else if (Arch == Triple::arm) {
360 // TODO: There is only ARM far stub now. We should add the Thumb stub,
361 // and stubs for branches Thumb - ARM and ARM - Thumb.
362 uint32_t *StubAddr = (uint32_t*)Addr;
363 *StubAddr = 0xe51ff004; // ldr pc,<label>
364 return (uint8_t*)++StubAddr;
365 } else if (Arch == Triple::mipsel || Arch == Triple::mips) {
366 uint32_t *StubAddr = (uint32_t*)Addr;
367 // 0: 3c190000 lui t9,%hi(addr).
368 // 4: 27390000 addiu t9,t9,%lo(addr).
369 // 8: 03200008 jr t9.
371 const unsigned LuiT9Instr = 0x3c190000, AdduiT9Instr = 0x27390000;
372 const unsigned JrT9Instr = 0x03200008, NopInstr = 0x0;
374 *StubAddr = LuiT9Instr;
376 *StubAddr = AdduiT9Instr;
378 *StubAddr = JrT9Instr;
380 *StubAddr = NopInstr;
382 } else if (Arch == Triple::ppc64) {
383 // PowerPC64 stub: the address points to a function descriptor
384 // instead of the function itself. Load the function address
385 // on r11 and sets it to control register. Also loads the function
386 // TOC in r2 and environment pointer to r11.
387 writeInt32BE(Addr, 0x3D800000); // lis r12, highest(addr)
388 writeInt32BE(Addr+4, 0x618C0000); // ori r12, higher(addr)
389 writeInt32BE(Addr+8, 0x798C07C6); // sldi r12, r12, 32
390 writeInt32BE(Addr+12, 0x658C0000); // oris r12, r12, h(addr)
391 writeInt32BE(Addr+16, 0x618C0000); // ori r12, r12, l(addr)
392 writeInt32BE(Addr+20, 0xF8410028); // std r2, 40(r1)
393 writeInt32BE(Addr+24, 0xE96C0000); // ld r11, 0(r12)
394 writeInt32BE(Addr+28, 0xE84C0008); // ld r2, 0(r12)
395 writeInt32BE(Addr+32, 0x7D6903A6); // mtctr r11
396 writeInt32BE(Addr+36, 0xE96C0010); // ld r11, 16(r2)
397 writeInt32BE(Addr+40, 0x4E800420); // bctr
400 } else if (Arch == Triple::systemz) {
401 writeInt16BE(Addr, 0xC418); // lgrl %r1,.+8
402 writeInt16BE(Addr+2, 0x0000);
403 writeInt16BE(Addr+4, 0x0004);
404 writeInt16BE(Addr+6, 0x07F1); // brc 15,%r1
405 // 8-byte address stored at Addr + 8
411 // Assign an address to a symbol name and resolve all the relocations
412 // associated with it.
413 void RuntimeDyldImpl::reassignSectionAddress(unsigned SectionID,
415 // The address to use for relocation resolution is not
416 // the address of the local section buffer. We must be doing
417 // a remote execution environment of some sort. Relocations can't
418 // be applied until all the sections have been moved. The client must
419 // trigger this with a call to MCJIT::finalize() or
420 // RuntimeDyld::resolveRelocations().
422 // Addr is a uint64_t because we can't assume the pointer width
423 // of the target is the same as that of the host. Just use a generic
424 // "big enough" type.
425 Sections[SectionID].LoadAddress = Addr;
428 void RuntimeDyldImpl::resolveRelocationList(const RelocationList &Relocs,
430 for (unsigned i = 0, e = Relocs.size(); i != e; ++i) {
431 const RelocationEntry &RE = Relocs[i];
432 // Ignore relocations for sections that were not loaded
433 if (Sections[RE.SectionID].Address == 0)
435 resolveRelocation(RE, Value);
439 void RuntimeDyldImpl::resolveExternalSymbols() {
440 StringMap<RelocationList>::iterator i = ExternalSymbolRelocations.begin(),
441 e = ExternalSymbolRelocations.end();
442 for (; i != e; i++) {
443 StringRef Name = i->first();
444 RelocationList &Relocs = i->second;
445 SymbolTableMap::const_iterator Loc = GlobalSymbolTable.find(Name);
446 if (Loc == GlobalSymbolTable.end()) {
447 if (Name.size() == 0) {
448 // This is an absolute symbol, use an address of zero.
449 DEBUG(dbgs() << "Resolving absolute relocations." << "\n");
450 resolveRelocationList(Relocs, 0);
452 // This is an external symbol, try to get its address from
454 uint8_t *Addr = (uint8_t*) MemMgr->getPointerToNamedFunction(Name.data(),
456 DEBUG(dbgs() << "Resolving relocations Name: " << Name
457 << "\t" << format("%p", Addr)
459 resolveRelocationList(Relocs, (uintptr_t)Addr);
462 report_fatal_error("Expected external symbol");
468 //===----------------------------------------------------------------------===//
469 // RuntimeDyld class implementation
470 RuntimeDyld::RuntimeDyld(RTDyldMemoryManager *mm) {
471 // FIXME: There's a potential issue lurking here if a single instance of
472 // RuntimeDyld is used to load multiple objects. The current implementation
473 // associates a single memory manager with a RuntimeDyld instance. Even
474 // though the public class spawns a new 'impl' instance for each load,
475 // they share a single memory manager. This can become a problem when page
476 // permissions are applied.
481 RuntimeDyld::~RuntimeDyld() {
485 ObjectImage *RuntimeDyld::loadObject(ObjectBuffer *InputBuffer) {
487 sys::LLVMFileType type = sys::IdentifyFileType(
488 InputBuffer->getBufferStart(),
489 static_cast<unsigned>(InputBuffer->getBufferSize()));
491 case sys::ELF_Relocatable_FileType:
492 case sys::ELF_Executable_FileType:
493 case sys::ELF_SharedObject_FileType:
494 case sys::ELF_Core_FileType:
495 Dyld = new RuntimeDyldELF(MM);
497 case sys::Mach_O_Object_FileType:
498 case sys::Mach_O_Executable_FileType:
499 case sys::Mach_O_FixedVirtualMemorySharedLib_FileType:
500 case sys::Mach_O_Core_FileType:
501 case sys::Mach_O_PreloadExecutable_FileType:
502 case sys::Mach_O_DynamicallyLinkedSharedLib_FileType:
503 case sys::Mach_O_DynamicLinker_FileType:
504 case sys::Mach_O_Bundle_FileType:
505 case sys::Mach_O_DynamicallyLinkedSharedLibStub_FileType:
506 case sys::Mach_O_DSYMCompanion_FileType:
507 Dyld = new RuntimeDyldMachO(MM);
509 case sys::Unknown_FileType:
510 case sys::Bitcode_FileType:
511 case sys::Archive_FileType:
512 case sys::COFF_FileType:
513 report_fatal_error("Incompatible object format!");
516 if (!Dyld->isCompatibleFormat(InputBuffer))
517 report_fatal_error("Incompatible object format!");
520 return Dyld->loadObject(InputBuffer);
523 void *RuntimeDyld::getSymbolAddress(StringRef Name) {
524 return Dyld->getSymbolAddress(Name);
527 uint64_t RuntimeDyld::getSymbolLoadAddress(StringRef Name) {
528 return Dyld->getSymbolLoadAddress(Name);
531 void RuntimeDyld::resolveRelocations() {
532 Dyld->resolveRelocations();
535 void RuntimeDyld::reassignSectionAddress(unsigned SectionID,
537 Dyld->reassignSectionAddress(SectionID, Addr);
540 void RuntimeDyld::mapSectionAddress(const void *LocalAddress,
541 uint64_t TargetAddress) {
542 Dyld->mapSectionAddress(LocalAddress, TargetAddress);
545 StringRef RuntimeDyld::getErrorString() {
546 return Dyld->getErrorString();
549 StringRef RuntimeDyld::getEHFrameSection() {
550 return Dyld->getEHFrameSection();
553 } // end namespace llvm