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 "JITRegistrar.h"
17 #include "ObjectImageCommon.h"
18 #include "RuntimeDyldELF.h"
19 #include "RuntimeDyldImpl.h"
20 #include "RuntimeDyldMachO.h"
21 #include "llvm/Object/ELF.h"
22 #include "llvm/Support/MathExtras.h"
23 #include "llvm/Support/MutexGuard.h"
26 using namespace llvm::object;
28 // Empty out-of-line virtual destructor as the key function.
29 RuntimeDyldImpl::~RuntimeDyldImpl() {}
31 // Pin the JITRegistrar's and ObjectImage*'s vtables to this file.
32 void JITRegistrar::anchor() {}
33 void ObjectImage::anchor() {}
34 void ObjectImageCommon::anchor() {}
38 void RuntimeDyldImpl::registerEHFrames() {}
40 void RuntimeDyldImpl::deregisterEHFrames() {}
42 // Resolve the relocations for all symbols we currently know about.
43 void RuntimeDyldImpl::resolveRelocations() {
44 MutexGuard locked(lock);
46 // First, resolve relocations associated with external symbols.
47 resolveExternalSymbols();
49 // Just iterate over the sections we have and resolve all the relocations
50 // in them. Gross overkill, but it gets the job done.
51 for (int i = 0, e = Sections.size(); i != e; ++i) {
52 // The Section here (Sections[i]) refers to the section in which the
53 // symbol for the relocation is located. The SectionID in the relocation
54 // entry provides the section to which the relocation will be applied.
55 uint64_t Addr = Sections[i].LoadAddress;
56 DEBUG(dbgs() << "Resolving relocations Section #" << i << "\t"
57 << format("%p", (uint8_t *)Addr) << "\n");
58 resolveRelocationList(Relocations[i], Addr);
63 void RuntimeDyldImpl::mapSectionAddress(const void *LocalAddress,
64 uint64_t TargetAddress) {
65 MutexGuard locked(lock);
66 for (unsigned i = 0, e = Sections.size(); i != e; ++i) {
67 if (Sections[i].Address == LocalAddress) {
68 reassignSectionAddress(i, TargetAddress);
72 llvm_unreachable("Attempting to remap address of unknown section!");
75 ObjectImage *RuntimeDyldImpl::loadObject(ObjectImage *InputObject) {
76 MutexGuard locked(lock);
78 std::unique_ptr<ObjectImage> Obj(InputObject);
82 // Save information about our target
83 Arch = (Triple::ArchType)Obj->getArch();
84 IsTargetLittleEndian = Obj->getObjectFile()->isLittleEndian();
86 // Compute the memory size required to load all sections to be loaded
87 // and pass this information to the memory manager
88 if (MemMgr->needsToReserveAllocationSpace()) {
89 uint64_t CodeSize = 0, DataSizeRO = 0, DataSizeRW = 0;
90 computeTotalAllocSize(*Obj, CodeSize, DataSizeRO, DataSizeRW);
91 MemMgr->reserveAllocationSpace(CodeSize, DataSizeRO, DataSizeRW);
94 // Symbols found in this object
95 StringMap<SymbolLoc> LocalSymbols;
96 // Used sections from the object file
97 ObjSectionToIDMap LocalSections;
99 // Common symbols requiring allocation, with their sizes and alignments
100 CommonSymbolMap CommonSymbols;
101 // Maximum required total memory to allocate all common symbols
102 uint64_t CommonSize = 0;
105 DEBUG(dbgs() << "Parse symbols:\n");
106 for (symbol_iterator I = Obj->begin_symbols(), E = Obj->end_symbols(); I != E;
108 object::SymbolRef::Type SymType;
110 Check(I->getType(SymType));
111 Check(I->getName(Name));
113 uint32_t Flags = I->getFlags();
115 bool IsCommon = Flags & SymbolRef::SF_Common;
117 // Add the common symbols to a list. We'll allocate them all below.
119 Check(I->getAlignment(Align));
121 Check(I->getSize(Size));
122 CommonSize += Size + Align;
123 CommonSymbols[*I] = CommonSymbolInfo(Size, Align);
125 if (SymType == object::SymbolRef::ST_Function ||
126 SymType == object::SymbolRef::ST_Data ||
127 SymType == object::SymbolRef::ST_Unknown) {
129 StringRef SectionData;
131 section_iterator SI = Obj->end_sections();
132 Check(I->getFileOffset(FileOffset));
133 Check(I->getSection(SI));
134 if (SI == Obj->end_sections())
136 Check(SI->getContents(SectionData));
137 Check(SI->isText(IsCode));
138 const uint8_t *SymPtr =
139 (const uint8_t *)Obj->getData().data() + (uintptr_t)FileOffset;
140 uintptr_t SectOffset =
141 (uintptr_t)(SymPtr - (const uint8_t *)SectionData.begin());
143 findOrEmitSection(*Obj, *SI, IsCode, LocalSections);
144 LocalSymbols[Name.data()] = SymbolLoc(SectionID, SectOffset);
145 DEBUG(dbgs() << "\tFileOffset: " << format("%p", (uintptr_t)FileOffset)
146 << " flags: " << Flags << " SID: " << SectionID
147 << " Offset: " << format("%p", SectOffset));
148 GlobalSymbolTable[Name] = SymbolLoc(SectionID, SectOffset);
151 DEBUG(dbgs() << "\tType: " << SymType << " Name: " << Name << "\n");
154 // Allocate common symbols
156 emitCommonSymbols(*Obj, CommonSymbols, CommonSize, LocalSymbols);
158 // Parse and process relocations
159 DEBUG(dbgs() << "Parse relocations:\n");
160 for (section_iterator SI = Obj->begin_sections(), SE = Obj->end_sections();
162 unsigned SectionID = 0;
164 section_iterator RelocatedSection = SI->getRelocatedSection();
166 if (SI->relocation_empty() && !ProcessAllSections)
170 Check(RelocatedSection->isText(IsCode));
172 findOrEmitSection(*Obj, *RelocatedSection, IsCode, LocalSections);
173 DEBUG(dbgs() << "\tSectionID: " << SectionID << "\n");
175 for (relocation_iterator I = SI->relocation_begin(),
176 E = SI->relocation_end(); I != E;)
177 I = processRelocationRef(SectionID, I, *Obj, LocalSections, LocalSymbols,
181 // Give the subclasses a chance to tie-up any loose ends.
182 finalizeLoad(LocalSections);
184 return Obj.release();
187 // A helper method for computeTotalAllocSize.
188 // Computes the memory size required to allocate sections with the given sizes,
189 // assuming that all sections are allocated with the given alignment
191 computeAllocationSizeForSections(std::vector<uint64_t> &SectionSizes,
192 uint64_t Alignment) {
193 uint64_t TotalSize = 0;
194 for (size_t Idx = 0, Cnt = SectionSizes.size(); Idx < Cnt; Idx++) {
195 uint64_t AlignedSize =
196 (SectionSizes[Idx] + Alignment - 1) / Alignment * Alignment;
197 TotalSize += AlignedSize;
202 // Compute an upper bound of the memory size that is required to load all
204 void RuntimeDyldImpl::computeTotalAllocSize(ObjectImage &Obj,
206 uint64_t &DataSizeRO,
207 uint64_t &DataSizeRW) {
208 // Compute the size of all sections required for execution
209 std::vector<uint64_t> CodeSectionSizes;
210 std::vector<uint64_t> ROSectionSizes;
211 std::vector<uint64_t> RWSectionSizes;
212 uint64_t MaxAlignment = sizeof(void *);
214 // Collect sizes of all sections to be loaded;
215 // also determine the max alignment of all sections
216 for (section_iterator SI = Obj.begin_sections(), SE = Obj.end_sections();
218 const SectionRef &Section = *SI;
221 Check(Section.isRequiredForExecution(IsRequired));
223 // Consider only the sections that are required to be loaded for execution
225 uint64_t DataSize = 0;
226 uint64_t Alignment64 = 0;
228 bool IsReadOnly = false;
230 Check(Section.getSize(DataSize));
231 Check(Section.getAlignment(Alignment64));
232 Check(Section.isText(IsCode));
233 Check(Section.isReadOnlyData(IsReadOnly));
234 Check(Section.getName(Name));
235 unsigned Alignment = (unsigned)Alignment64 & 0xffffffffL;
237 uint64_t StubBufSize = computeSectionStubBufSize(Obj, Section);
238 uint64_t SectionSize = DataSize + StubBufSize;
240 // The .eh_frame section (at least on Linux) needs an extra four bytes
242 // with zeroes added at the end. For MachO objects, this section has a
243 // slightly different name, so this won't have any effect for MachO
245 if (Name == ".eh_frame")
248 if (SectionSize > 0) {
249 // save the total size of the section
251 CodeSectionSizes.push_back(SectionSize);
252 } else if (IsReadOnly) {
253 ROSectionSizes.push_back(SectionSize);
255 RWSectionSizes.push_back(SectionSize);
257 // update the max alignment
258 if (Alignment > MaxAlignment) {
259 MaxAlignment = Alignment;
265 // Compute the size of all common symbols
266 uint64_t CommonSize = 0;
267 for (symbol_iterator I = Obj.begin_symbols(), E = Obj.end_symbols(); I != E;
269 uint32_t Flags = I->getFlags();
270 if (Flags & SymbolRef::SF_Common) {
271 // Add the common symbols to a list. We'll allocate them all below.
273 Check(I->getSize(Size));
277 if (CommonSize != 0) {
278 RWSectionSizes.push_back(CommonSize);
281 // Compute the required allocation space for each different type of sections
282 // (code, read-only data, read-write data) assuming that all sections are
283 // allocated with the max alignment. Note that we cannot compute with the
284 // individual alignments of the sections, because then the required size
285 // depends on the order, in which the sections are allocated.
286 CodeSize = computeAllocationSizeForSections(CodeSectionSizes, MaxAlignment);
287 DataSizeRO = computeAllocationSizeForSections(ROSectionSizes, MaxAlignment);
288 DataSizeRW = computeAllocationSizeForSections(RWSectionSizes, MaxAlignment);
291 // compute stub buffer size for the given section
292 unsigned RuntimeDyldImpl::computeSectionStubBufSize(ObjectImage &Obj,
293 const SectionRef &Section) {
294 unsigned StubSize = getMaxStubSize();
298 // FIXME: this is an inefficient way to handle this. We should computed the
299 // necessary section allocation size in loadObject by walking all the sections
301 unsigned StubBufSize = 0;
302 for (section_iterator SI = Obj.begin_sections(), SE = Obj.end_sections();
304 section_iterator RelSecI = SI->getRelocatedSection();
305 if (!(RelSecI == Section))
308 for (const RelocationRef &Reloc : SI->relocations()) {
310 StubBufSize += StubSize;
314 // Get section data size and alignment
315 uint64_t Alignment64;
317 Check(Section.getSize(DataSize));
318 Check(Section.getAlignment(Alignment64));
320 // Add stubbuf size alignment
321 unsigned Alignment = (unsigned)Alignment64 & 0xffffffffL;
322 unsigned StubAlignment = getStubAlignment();
323 unsigned EndAlignment = (DataSize | Alignment) & -(DataSize | Alignment);
324 if (StubAlignment > EndAlignment)
325 StubBufSize += StubAlignment - EndAlignment;
329 void RuntimeDyldImpl::emitCommonSymbols(ObjectImage &Obj,
330 const CommonSymbolMap &CommonSymbols,
332 SymbolTableMap &SymbolTable) {
333 // Allocate memory for the section
334 unsigned SectionID = Sections.size();
335 uint8_t *Addr = MemMgr->allocateDataSection(TotalSize, sizeof(void *),
336 SectionID, StringRef(), false);
338 report_fatal_error("Unable to allocate memory for common symbols!");
340 Sections.push_back(SectionEntry(StringRef(), Addr, TotalSize, 0));
341 memset(Addr, 0, TotalSize);
343 DEBUG(dbgs() << "emitCommonSection SectionID: " << SectionID << " new addr: "
344 << format("%p", Addr) << " DataSize: " << TotalSize << "\n");
346 // Assign the address of each symbol
347 for (CommonSymbolMap::const_iterator it = CommonSymbols.begin(),
348 itEnd = CommonSymbols.end(); it != itEnd; ++it) {
349 uint64_t Size = it->second.first;
350 uint64_t Align = it->second.second;
352 it->first.getName(Name);
354 // This symbol has an alignment requirement.
355 uint64_t AlignOffset = OffsetToAlignment((uint64_t)Addr, Align);
357 Offset += AlignOffset;
358 DEBUG(dbgs() << "Allocating common symbol " << Name << " address "
359 << format("%p\n", Addr));
361 Obj.updateSymbolAddress(it->first, (uint64_t)Addr);
362 SymbolTable[Name.data()] = SymbolLoc(SectionID, Offset);
368 unsigned RuntimeDyldImpl::emitSection(ObjectImage &Obj,
369 const SectionRef &Section, bool IsCode) {
372 uint64_t Alignment64;
373 Check(Section.getContents(data));
374 Check(Section.getAlignment(Alignment64));
376 unsigned Alignment = (unsigned)Alignment64 & 0xffffffffL;
382 unsigned PaddingSize = 0;
383 unsigned StubBufSize = 0;
385 Check(Section.isRequiredForExecution(IsRequired));
386 Check(Section.isVirtual(IsVirtual));
387 Check(Section.isZeroInit(IsZeroInit));
388 Check(Section.isReadOnlyData(IsReadOnly));
389 Check(Section.getSize(DataSize));
390 Check(Section.getName(Name));
392 StubBufSize = computeSectionStubBufSize(Obj, Section);
394 // The .eh_frame section (at least on Linux) needs an extra four bytes padded
395 // with zeroes added at the end. For MachO objects, this section has a
396 // slightly different name, so this won't have any effect for MachO objects.
397 if (Name == ".eh_frame")
401 unsigned SectionID = Sections.size();
403 const char *pData = 0;
405 // Some sections, such as debug info, don't need to be loaded for execution.
406 // Leave those where they are.
408 Allocate = DataSize + PaddingSize + StubBufSize;
409 Addr = IsCode ? MemMgr->allocateCodeSection(Allocate, Alignment, SectionID,
411 : MemMgr->allocateDataSection(Allocate, Alignment, SectionID,
414 report_fatal_error("Unable to allocate section memory!");
416 // Virtual sections have no data in the object image, so leave pData = 0
420 // Zero-initialize or copy the data from the image
421 if (IsZeroInit || IsVirtual)
422 memset(Addr, 0, DataSize);
424 memcpy(Addr, pData, DataSize);
426 // Fill in any extra bytes we allocated for padding
427 if (PaddingSize != 0) {
428 memset(Addr + DataSize, 0, PaddingSize);
429 // Update the DataSize variable so that the stub offset is set correctly.
430 DataSize += PaddingSize;
433 DEBUG(dbgs() << "emitSection SectionID: " << SectionID << " Name: " << Name
434 << " obj addr: " << format("%p", pData)
435 << " new addr: " << format("%p", Addr)
436 << " DataSize: " << DataSize << " StubBufSize: " << StubBufSize
437 << " Allocate: " << Allocate << "\n");
438 Obj.updateSectionAddress(Section, (uint64_t)Addr);
440 // Even if we didn't load the section, we need to record an entry for it
441 // to handle later processing (and by 'handle' I mean don't do anything
442 // with these sections).
445 DEBUG(dbgs() << "emitSection SectionID: " << SectionID << " Name: " << Name
446 << " obj addr: " << format("%p", data.data()) << " new addr: 0"
447 << " DataSize: " << DataSize << " StubBufSize: " << StubBufSize
448 << " Allocate: " << Allocate << "\n");
451 Sections.push_back(SectionEntry(Name, Addr, DataSize, (uintptr_t)pData));
455 unsigned RuntimeDyldImpl::findOrEmitSection(ObjectImage &Obj,
456 const SectionRef &Section,
458 ObjSectionToIDMap &LocalSections) {
460 unsigned SectionID = 0;
461 ObjSectionToIDMap::iterator i = LocalSections.find(Section);
462 if (i != LocalSections.end())
463 SectionID = i->second;
465 SectionID = emitSection(Obj, Section, IsCode);
466 LocalSections[Section] = SectionID;
471 void RuntimeDyldImpl::addRelocationForSection(const RelocationEntry &RE,
472 unsigned SectionID) {
473 Relocations[SectionID].push_back(RE);
476 void RuntimeDyldImpl::addRelocationForSymbol(const RelocationEntry &RE,
477 StringRef SymbolName) {
478 // Relocation by symbol. If the symbol is found in the global symbol table,
479 // create an appropriate section relocation. Otherwise, add it to
480 // ExternalSymbolRelocations.
481 SymbolTableMap::const_iterator Loc = GlobalSymbolTable.find(SymbolName);
482 if (Loc == GlobalSymbolTable.end()) {
483 ExternalSymbolRelocations[SymbolName].push_back(RE);
485 // Copy the RE since we want to modify its addend.
486 RelocationEntry RECopy = RE;
487 RECopy.Addend += Loc->second.second;
488 Relocations[Loc->second.first].push_back(RECopy);
492 uint8_t *RuntimeDyldImpl::createStubFunction(uint8_t *Addr) {
493 if (Arch == Triple::aarch64 || Arch == Triple::aarch64_be) {
494 // This stub has to be able to access the full address space,
495 // since symbol lookup won't necessarily find a handy, in-range,
496 // PLT stub for functions which could be anywhere.
497 uint32_t *StubAddr = (uint32_t *)Addr;
499 // Stub can use ip0 (== x16) to calculate address
500 *StubAddr = 0xd2e00010; // movz ip0, #:abs_g3:<addr>
502 *StubAddr = 0xf2c00010; // movk ip0, #:abs_g2_nc:<addr>
504 *StubAddr = 0xf2a00010; // movk ip0, #:abs_g1_nc:<addr>
506 *StubAddr = 0xf2800010; // movk ip0, #:abs_g0_nc:<addr>
508 *StubAddr = 0xd61f0200; // br ip0
511 } else if (Arch == Triple::arm) {
512 // TODO: There is only ARM far stub now. We should add the Thumb stub,
513 // and stubs for branches Thumb - ARM and ARM - Thumb.
514 uint32_t *StubAddr = (uint32_t *)Addr;
515 *StubAddr = 0xe51ff004; // ldr pc,<label>
516 return (uint8_t *)++StubAddr;
517 } else if (Arch == Triple::mipsel || Arch == Triple::mips) {
518 uint32_t *StubAddr = (uint32_t *)Addr;
519 // 0: 3c190000 lui t9,%hi(addr).
520 // 4: 27390000 addiu t9,t9,%lo(addr).
521 // 8: 03200008 jr t9.
523 const unsigned LuiT9Instr = 0x3c190000, AdduiT9Instr = 0x27390000;
524 const unsigned JrT9Instr = 0x03200008, NopInstr = 0x0;
526 *StubAddr = LuiT9Instr;
528 *StubAddr = AdduiT9Instr;
530 *StubAddr = JrT9Instr;
532 *StubAddr = NopInstr;
534 } else if (Arch == Triple::ppc64 || Arch == Triple::ppc64le) {
535 // PowerPC64 stub: the address points to a function descriptor
536 // instead of the function itself. Load the function address
537 // on r11 and sets it to control register. Also loads the function
538 // TOC in r2 and environment pointer to r11.
539 writeInt32BE(Addr, 0x3D800000); // lis r12, highest(addr)
540 writeInt32BE(Addr+4, 0x618C0000); // ori r12, higher(addr)
541 writeInt32BE(Addr+8, 0x798C07C6); // sldi r12, r12, 32
542 writeInt32BE(Addr+12, 0x658C0000); // oris r12, r12, h(addr)
543 writeInt32BE(Addr+16, 0x618C0000); // ori r12, r12, l(addr)
544 writeInt32BE(Addr+20, 0xF8410028); // std r2, 40(r1)
545 writeInt32BE(Addr+24, 0xE96C0000); // ld r11, 0(r12)
546 writeInt32BE(Addr+28, 0xE84C0008); // ld r2, 0(r12)
547 writeInt32BE(Addr+32, 0x7D6903A6); // mtctr r11
548 writeInt32BE(Addr+36, 0xE96C0010); // ld r11, 16(r2)
549 writeInt32BE(Addr+40, 0x4E800420); // bctr
552 } else if (Arch == Triple::systemz) {
553 writeInt16BE(Addr, 0xC418); // lgrl %r1,.+8
554 writeInt16BE(Addr+2, 0x0000);
555 writeInt16BE(Addr+4, 0x0004);
556 writeInt16BE(Addr+6, 0x07F1); // brc 15,%r1
557 // 8-byte address stored at Addr + 8
559 } else if (Arch == Triple::x86_64) {
561 *(Addr+1) = 0x25; // rip
562 // 32-bit PC-relative address of the GOT entry will be stored at Addr+2
567 // Assign an address to a symbol name and resolve all the relocations
568 // associated with it.
569 void RuntimeDyldImpl::reassignSectionAddress(unsigned SectionID,
571 // The address to use for relocation resolution is not
572 // the address of the local section buffer. We must be doing
573 // a remote execution environment of some sort. Relocations can't
574 // be applied until all the sections have been moved. The client must
575 // trigger this with a call to MCJIT::finalize() or
576 // RuntimeDyld::resolveRelocations().
578 // Addr is a uint64_t because we can't assume the pointer width
579 // of the target is the same as that of the host. Just use a generic
580 // "big enough" type.
581 Sections[SectionID].LoadAddress = Addr;
584 void RuntimeDyldImpl::resolveRelocationList(const RelocationList &Relocs,
586 for (unsigned i = 0, e = Relocs.size(); i != e; ++i) {
587 const RelocationEntry &RE = Relocs[i];
588 // Ignore relocations for sections that were not loaded
589 if (Sections[RE.SectionID].Address == 0)
591 resolveRelocation(RE, Value);
595 void RuntimeDyldImpl::resolveExternalSymbols() {
596 while (!ExternalSymbolRelocations.empty()) {
597 StringMap<RelocationList>::iterator i = ExternalSymbolRelocations.begin();
599 StringRef Name = i->first();
600 if (Name.size() == 0) {
601 // This is an absolute symbol, use an address of zero.
602 DEBUG(dbgs() << "Resolving absolute relocations."
604 RelocationList &Relocs = i->second;
605 resolveRelocationList(Relocs, 0);
608 SymbolTableMap::const_iterator Loc = GlobalSymbolTable.find(Name);
609 if (Loc == GlobalSymbolTable.end()) {
610 // This is an external symbol, try to get its address from
612 Addr = MemMgr->getSymbolAddress(Name.data());
613 // The call to getSymbolAddress may have caused additional modules to
614 // be loaded, which may have added new entries to the
615 // ExternalSymbolRelocations map. Consquently, we need to update our
616 // iterator. This is also why retrieval of the relocation list
617 // associated with this symbol is deferred until below this point.
618 // New entries may have been added to the relocation list.
619 i = ExternalSymbolRelocations.find(Name);
621 // We found the symbol in our global table. It was probably in a
622 // Module that we loaded previously.
623 SymbolLoc SymLoc = Loc->second;
624 Addr = getSectionLoadAddress(SymLoc.first) + SymLoc.second;
627 // FIXME: Implement error handling that doesn't kill the host program!
629 report_fatal_error("Program used external function '" + Name +
630 "' which could not be resolved!");
632 updateGOTEntries(Name, Addr);
633 DEBUG(dbgs() << "Resolving relocations Name: " << Name << "\t"
634 << format("0x%lx", Addr) << "\n");
635 // This list may have been updated when we called getSymbolAddress, so
636 // don't change this code to get the list earlier.
637 RelocationList &Relocs = i->second;
638 resolveRelocationList(Relocs, Addr);
641 ExternalSymbolRelocations.erase(i);
645 //===----------------------------------------------------------------------===//
646 // RuntimeDyld class implementation
647 RuntimeDyld::RuntimeDyld(RTDyldMemoryManager *mm) {
648 // FIXME: There's a potential issue lurking here if a single instance of
649 // RuntimeDyld is used to load multiple objects. The current implementation
650 // associates a single memory manager with a RuntimeDyld instance. Even
651 // though the public class spawns a new 'impl' instance for each load,
652 // they share a single memory manager. This can become a problem when page
653 // permissions are applied.
656 ProcessAllSections = false;
659 RuntimeDyld::~RuntimeDyld() { delete Dyld; }
661 static std::unique_ptr<RuntimeDyldELF>
662 createRuntimeDyldELF(RTDyldMemoryManager *MM, bool ProcessAllSections) {
663 std::unique_ptr<RuntimeDyldELF> Dyld(new RuntimeDyldELF(MM));
664 Dyld->setProcessAllSections(ProcessAllSections);
668 static std::unique_ptr<RuntimeDyldMachO>
669 createRuntimeDyldMachO(RTDyldMemoryManager *MM, bool ProcessAllSections) {
670 std::unique_ptr<RuntimeDyldMachO> Dyld(new RuntimeDyldMachO(MM));
671 Dyld->setProcessAllSections(ProcessAllSections);
675 ObjectImage *RuntimeDyld::loadObject(ObjectFile *InputObject) {
676 std::unique_ptr<ObjectImage> InputImage;
678 if (InputObject->isELF()) {
679 InputImage.reset(RuntimeDyldELF::createObjectImageFromFile(InputObject));
681 Dyld = createRuntimeDyldELF(MM, ProcessAllSections).release();
682 } else if (InputObject->isMachO()) {
683 InputImage.reset(RuntimeDyldMachO::createObjectImageFromFile(InputObject));
685 Dyld = createRuntimeDyldMachO(MM, ProcessAllSections).release();
687 report_fatal_error("Incompatible object format!");
689 if (!Dyld->isCompatibleFile(InputObject))
690 report_fatal_error("Incompatible object format!");
692 Dyld->loadObject(InputImage.get());
693 return InputImage.release();
696 ObjectImage *RuntimeDyld::loadObject(ObjectBuffer *InputBuffer) {
697 std::unique_ptr<ObjectImage> InputImage;
698 sys::fs::file_magic Type = sys::fs::identify_magic(InputBuffer->getBuffer());
701 case sys::fs::file_magic::elf_relocatable:
702 case sys::fs::file_magic::elf_executable:
703 case sys::fs::file_magic::elf_shared_object:
704 case sys::fs::file_magic::elf_core:
705 InputImage.reset(RuntimeDyldELF::createObjectImage(InputBuffer));
707 Dyld = createRuntimeDyldELF(MM, ProcessAllSections).release();
709 case sys::fs::file_magic::macho_object:
710 case sys::fs::file_magic::macho_executable:
711 case sys::fs::file_magic::macho_fixed_virtual_memory_shared_lib:
712 case sys::fs::file_magic::macho_core:
713 case sys::fs::file_magic::macho_preload_executable:
714 case sys::fs::file_magic::macho_dynamically_linked_shared_lib:
715 case sys::fs::file_magic::macho_dynamic_linker:
716 case sys::fs::file_magic::macho_bundle:
717 case sys::fs::file_magic::macho_dynamically_linked_shared_lib_stub:
718 case sys::fs::file_magic::macho_dsym_companion:
719 InputImage.reset(RuntimeDyldMachO::createObjectImage(InputBuffer));
721 Dyld = createRuntimeDyldMachO(MM, ProcessAllSections).release();
723 case sys::fs::file_magic::unknown:
724 case sys::fs::file_magic::bitcode:
725 case sys::fs::file_magic::archive:
726 case sys::fs::file_magic::coff_object:
727 case sys::fs::file_magic::coff_import_library:
728 case sys::fs::file_magic::pecoff_executable:
729 case sys::fs::file_magic::macho_universal_binary:
730 case sys::fs::file_magic::windows_resource:
731 report_fatal_error("Incompatible object format!");
734 if (!Dyld->isCompatibleFormat(InputBuffer))
735 report_fatal_error("Incompatible object format!");
737 Dyld->loadObject(InputImage.get());
738 return InputImage.release();
741 void *RuntimeDyld::getSymbolAddress(StringRef Name) {
744 return Dyld->getSymbolAddress(Name);
747 uint64_t RuntimeDyld::getSymbolLoadAddress(StringRef Name) {
750 return Dyld->getSymbolLoadAddress(Name);
753 void RuntimeDyld::resolveRelocations() { Dyld->resolveRelocations(); }
755 void RuntimeDyld::reassignSectionAddress(unsigned SectionID, uint64_t Addr) {
756 Dyld->reassignSectionAddress(SectionID, Addr);
759 void RuntimeDyld::mapSectionAddress(const void *LocalAddress,
760 uint64_t TargetAddress) {
761 Dyld->mapSectionAddress(LocalAddress, TargetAddress);
764 bool RuntimeDyld::hasError() { return Dyld->hasError(); }
766 StringRef RuntimeDyld::getErrorString() { return Dyld->getErrorString(); }
768 void RuntimeDyld::registerEHFrames() {
770 Dyld->registerEHFrames();
773 void RuntimeDyld::deregisterEHFrames() {
775 Dyld->deregisterEHFrames();
778 } // end namespace llvm