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 #include "llvm/ExecutionEngine/RuntimeDyld.h"
15 #include "JITRegistrar.h"
16 #include "ObjectImageCommon.h"
17 #include "RuntimeDyldCheckerImpl.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 #define DEBUG_TYPE "dyld"
30 // Empty out-of-line virtual destructor as the key function.
31 RuntimeDyldImpl::~RuntimeDyldImpl() {}
33 // Pin the JITRegistrar's and ObjectImage*'s vtables to this file.
34 void JITRegistrar::anchor() {}
35 void ObjectImage::anchor() {}
36 void ObjectImageCommon::anchor() {}
40 void RuntimeDyldImpl::registerEHFrames() {}
42 void RuntimeDyldImpl::deregisterEHFrames() {}
44 // Resolve the relocations for all symbols we currently know about.
45 void RuntimeDyldImpl::resolveRelocations() {
46 MutexGuard locked(lock);
48 // First, resolve relocations associated with external symbols.
49 resolveExternalSymbols();
51 // Just iterate over the sections we have and resolve all the relocations
52 // in them. Gross overkill, but it gets the job done.
53 for (int i = 0, e = Sections.size(); i != e; ++i) {
54 // The Section here (Sections[i]) refers to the section in which the
55 // symbol for the relocation is located. The SectionID in the relocation
56 // entry provides the section to which the relocation will be applied.
57 uint64_t Addr = Sections[i].LoadAddress;
58 DEBUG(dbgs() << "Resolving relocations Section #" << i << "\t"
59 << format("%p", (uint8_t *)Addr) << "\n");
60 resolveRelocationList(Relocations[i], Addr);
65 void RuntimeDyldImpl::mapSectionAddress(const void *LocalAddress,
66 uint64_t TargetAddress) {
67 MutexGuard locked(lock);
68 for (unsigned i = 0, e = Sections.size(); i != e; ++i) {
69 if (Sections[i].Address == LocalAddress) {
70 reassignSectionAddress(i, TargetAddress);
74 llvm_unreachable("Attempting to remap address of unknown section!");
77 static std::error_code getOffset(const SymbolRef &Sym, uint64_t &Result) {
79 if (std::error_code EC = Sym.getAddress(Address))
82 if (Address == UnknownAddressOrSize) {
83 Result = UnknownAddressOrSize;
84 return object_error::success;
87 const ObjectFile *Obj = Sym.getObject();
88 section_iterator SecI(Obj->section_begin());
89 if (std::error_code EC = Sym.getSection(SecI))
92 if (SecI == Obj->section_end()) {
93 Result = UnknownAddressOrSize;
94 return object_error::success;
97 uint64_t SectionAddress;
98 if (std::error_code EC = SecI->getAddress(SectionAddress))
101 Result = Address - SectionAddress;
102 return object_error::success;
105 ObjectImage *RuntimeDyldImpl::loadObject(ObjectImage *InputObject) {
106 MutexGuard locked(lock);
108 std::unique_ptr<ObjectImage> Obj(InputObject);
112 // Save information about our target
113 Arch = (Triple::ArchType)Obj->getArch();
114 IsTargetLittleEndian = Obj->getObjectFile()->isLittleEndian();
116 // Compute the memory size required to load all sections to be loaded
117 // and pass this information to the memory manager
118 if (MemMgr->needsToReserveAllocationSpace()) {
119 uint64_t CodeSize = 0, DataSizeRO = 0, DataSizeRW = 0;
120 computeTotalAllocSize(*Obj, CodeSize, DataSizeRO, DataSizeRW);
121 MemMgr->reserveAllocationSpace(CodeSize, DataSizeRO, DataSizeRW);
124 // Symbols found in this object
125 StringMap<SymbolLoc> LocalSymbols;
126 // Used sections from the object file
127 ObjSectionToIDMap LocalSections;
129 // Common symbols requiring allocation, with their sizes and alignments
130 CommonSymbolMap CommonSymbols;
131 // Maximum required total memory to allocate all common symbols
132 uint64_t CommonSize = 0;
135 DEBUG(dbgs() << "Parse symbols:\n");
136 for (symbol_iterator I = Obj->begin_symbols(), E = Obj->end_symbols(); I != E;
138 object::SymbolRef::Type SymType;
140 Check(I->getType(SymType));
141 Check(I->getName(Name));
143 uint32_t Flags = I->getFlags();
145 bool IsCommon = Flags & SymbolRef::SF_Common;
147 // Add the common symbols to a list. We'll allocate them all below.
148 if (!GlobalSymbolTable.count(Name)) {
150 Check(I->getAlignment(Align));
152 Check(I->getSize(Size));
153 CommonSize += Size + Align;
154 CommonSymbols[*I] = CommonSymbolInfo(Size, Align);
157 if (SymType == object::SymbolRef::ST_Function ||
158 SymType == object::SymbolRef::ST_Data ||
159 SymType == object::SymbolRef::ST_Unknown) {
161 StringRef SectionData;
163 section_iterator SI = Obj->end_sections();
164 Check(getOffset(*I, SectOffset));
165 Check(I->getSection(SI));
166 if (SI == Obj->end_sections())
168 Check(SI->getContents(SectionData));
169 Check(SI->isText(IsCode));
171 findOrEmitSection(*Obj, *SI, IsCode, LocalSections);
172 LocalSymbols[Name.data()] = SymbolLoc(SectionID, SectOffset);
173 DEBUG(dbgs() << "\tOffset: " << format("%p", (uintptr_t)SectOffset)
174 << " flags: " << Flags << " SID: " << SectionID);
175 GlobalSymbolTable[Name] = SymbolLoc(SectionID, SectOffset);
178 DEBUG(dbgs() << "\tType: " << SymType << " Name: " << Name << "\n");
181 // Allocate common symbols
183 emitCommonSymbols(*Obj, CommonSymbols, CommonSize, GlobalSymbolTable);
185 // Parse and process relocations
186 DEBUG(dbgs() << "Parse relocations:\n");
187 for (section_iterator SI = Obj->begin_sections(), SE = Obj->end_sections();
189 unsigned SectionID = 0;
191 section_iterator RelocatedSection = SI->getRelocatedSection();
193 relocation_iterator I = SI->relocation_begin();
194 relocation_iterator E = SI->relocation_end();
196 if (I == E && !ProcessAllSections)
200 Check(RelocatedSection->isText(IsCode));
202 findOrEmitSection(*Obj, *RelocatedSection, IsCode, LocalSections);
203 DEBUG(dbgs() << "\tSectionID: " << SectionID << "\n");
206 I = processRelocationRef(SectionID, I, *Obj, LocalSections, LocalSymbols,
209 // If there is an attached checker, notify it about the stubs for this
210 // section so that they can be verified.
212 Checker->registerStubMap(Obj->getImageName(), SectionID, Stubs);
215 // Give the subclasses a chance to tie-up any loose ends.
216 finalizeLoad(*Obj, LocalSections);
218 return Obj.release();
221 // A helper method for computeTotalAllocSize.
222 // Computes the memory size required to allocate sections with the given sizes,
223 // assuming that all sections are allocated with the given alignment
225 computeAllocationSizeForSections(std::vector<uint64_t> &SectionSizes,
226 uint64_t Alignment) {
227 uint64_t TotalSize = 0;
228 for (size_t Idx = 0, Cnt = SectionSizes.size(); Idx < Cnt; Idx++) {
229 uint64_t AlignedSize =
230 (SectionSizes[Idx] + Alignment - 1) / Alignment * Alignment;
231 TotalSize += AlignedSize;
236 // Compute an upper bound of the memory size that is required to load all
238 void RuntimeDyldImpl::computeTotalAllocSize(ObjectImage &Obj,
240 uint64_t &DataSizeRO,
241 uint64_t &DataSizeRW) {
242 // Compute the size of all sections required for execution
243 std::vector<uint64_t> CodeSectionSizes;
244 std::vector<uint64_t> ROSectionSizes;
245 std::vector<uint64_t> RWSectionSizes;
246 uint64_t MaxAlignment = sizeof(void *);
248 // Collect sizes of all sections to be loaded;
249 // also determine the max alignment of all sections
250 for (section_iterator SI = Obj.begin_sections(), SE = Obj.end_sections();
252 const SectionRef &Section = *SI;
255 Check(Section.isRequiredForExecution(IsRequired));
257 // Consider only the sections that are required to be loaded for execution
259 uint64_t DataSize = 0;
260 uint64_t Alignment64 = 0;
262 bool IsReadOnly = false;
264 Check(Section.getSize(DataSize));
265 Check(Section.getAlignment(Alignment64));
266 Check(Section.isText(IsCode));
267 Check(Section.isReadOnlyData(IsReadOnly));
268 Check(Section.getName(Name));
269 unsigned Alignment = (unsigned)Alignment64 & 0xffffffffL;
271 uint64_t StubBufSize = computeSectionStubBufSize(Obj, Section);
272 uint64_t SectionSize = DataSize + StubBufSize;
274 // The .eh_frame section (at least on Linux) needs an extra four bytes
276 // with zeroes added at the end. For MachO objects, this section has a
277 // slightly different name, so this won't have any effect for MachO
279 if (Name == ".eh_frame")
282 if (SectionSize > 0) {
283 // save the total size of the section
285 CodeSectionSizes.push_back(SectionSize);
286 } else if (IsReadOnly) {
287 ROSectionSizes.push_back(SectionSize);
289 RWSectionSizes.push_back(SectionSize);
291 // update the max alignment
292 if (Alignment > MaxAlignment) {
293 MaxAlignment = Alignment;
299 // Compute the size of all common symbols
300 uint64_t CommonSize = 0;
301 for (symbol_iterator I = Obj.begin_symbols(), E = Obj.end_symbols(); I != E;
303 uint32_t Flags = I->getFlags();
304 if (Flags & SymbolRef::SF_Common) {
305 // Add the common symbols to a list. We'll allocate them all below.
307 Check(I->getSize(Size));
311 if (CommonSize != 0) {
312 RWSectionSizes.push_back(CommonSize);
315 // Compute the required allocation space for each different type of sections
316 // (code, read-only data, read-write data) assuming that all sections are
317 // allocated with the max alignment. Note that we cannot compute with the
318 // individual alignments of the sections, because then the required size
319 // depends on the order, in which the sections are allocated.
320 CodeSize = computeAllocationSizeForSections(CodeSectionSizes, MaxAlignment);
321 DataSizeRO = computeAllocationSizeForSections(ROSectionSizes, MaxAlignment);
322 DataSizeRW = computeAllocationSizeForSections(RWSectionSizes, MaxAlignment);
325 // compute stub buffer size for the given section
326 unsigned RuntimeDyldImpl::computeSectionStubBufSize(ObjectImage &Obj,
327 const SectionRef &Section) {
328 unsigned StubSize = getMaxStubSize();
332 // FIXME: this is an inefficient way to handle this. We should computed the
333 // necessary section allocation size in loadObject by walking all the sections
335 unsigned StubBufSize = 0;
336 for (section_iterator SI = Obj.begin_sections(), SE = Obj.end_sections();
338 section_iterator RelSecI = SI->getRelocatedSection();
339 if (!(RelSecI == Section))
342 for (const RelocationRef &Reloc : SI->relocations()) {
344 StubBufSize += StubSize;
348 // Get section data size and alignment
349 uint64_t Alignment64;
351 Check(Section.getSize(DataSize));
352 Check(Section.getAlignment(Alignment64));
354 // Add stubbuf size alignment
355 unsigned Alignment = (unsigned)Alignment64 & 0xffffffffL;
356 unsigned StubAlignment = getStubAlignment();
357 unsigned EndAlignment = (DataSize | Alignment) & -(DataSize | Alignment);
358 if (StubAlignment > EndAlignment)
359 StubBufSize += StubAlignment - EndAlignment;
363 void RuntimeDyldImpl::emitCommonSymbols(ObjectImage &Obj,
364 const CommonSymbolMap &CommonSymbols,
366 SymbolTableMap &SymbolTable) {
367 // Allocate memory for the section
368 unsigned SectionID = Sections.size();
369 uint8_t *Addr = MemMgr->allocateDataSection(TotalSize, sizeof(void *),
370 SectionID, StringRef(), false);
372 report_fatal_error("Unable to allocate memory for common symbols!");
374 Sections.push_back(SectionEntry(StringRef(), Addr, TotalSize, 0));
375 memset(Addr, 0, TotalSize);
377 DEBUG(dbgs() << "emitCommonSection SectionID: " << SectionID << " new addr: "
378 << format("%p", Addr) << " DataSize: " << TotalSize << "\n");
380 // Assign the address of each symbol
381 for (CommonSymbolMap::const_iterator it = CommonSymbols.begin(),
382 itEnd = CommonSymbols.end(); it != itEnd; ++it) {
383 uint64_t Size = it->second.first;
384 uint64_t Align = it->second.second;
386 it->first.getName(Name);
388 // This symbol has an alignment requirement.
389 uint64_t AlignOffset = OffsetToAlignment((uint64_t)Addr, Align);
391 Offset += AlignOffset;
392 DEBUG(dbgs() << "Allocating common symbol " << Name << " address "
393 << format("%p\n", Addr));
395 Obj.updateSymbolAddress(it->first, (uint64_t)Addr);
396 SymbolTable[Name.data()] = SymbolLoc(SectionID, Offset);
402 unsigned RuntimeDyldImpl::emitSection(ObjectImage &Obj,
403 const SectionRef &Section, bool IsCode) {
406 uint64_t Alignment64;
407 Check(Section.getContents(data));
408 Check(Section.getAlignment(Alignment64));
410 unsigned Alignment = (unsigned)Alignment64 & 0xffffffffL;
416 unsigned PaddingSize = 0;
417 unsigned StubBufSize = 0;
419 Check(Section.isRequiredForExecution(IsRequired));
420 Check(Section.isVirtual(IsVirtual));
421 Check(Section.isZeroInit(IsZeroInit));
422 Check(Section.isReadOnlyData(IsReadOnly));
423 Check(Section.getSize(DataSize));
424 Check(Section.getName(Name));
426 StubBufSize = computeSectionStubBufSize(Obj, Section);
428 // The .eh_frame section (at least on Linux) needs an extra four bytes padded
429 // with zeroes added at the end. For MachO objects, this section has a
430 // slightly different name, so this won't have any effect for MachO objects.
431 if (Name == ".eh_frame")
435 unsigned SectionID = Sections.size();
437 const char *pData = nullptr;
439 // Some sections, such as debug info, don't need to be loaded for execution.
440 // Leave those where they are.
442 Allocate = DataSize + PaddingSize + StubBufSize;
443 Addr = IsCode ? MemMgr->allocateCodeSection(Allocate, Alignment, SectionID,
445 : MemMgr->allocateDataSection(Allocate, Alignment, SectionID,
448 report_fatal_error("Unable to allocate section memory!");
450 // Virtual sections have no data in the object image, so leave pData = 0
454 // Zero-initialize or copy the data from the image
455 if (IsZeroInit || IsVirtual)
456 memset(Addr, 0, DataSize);
458 memcpy(Addr, pData, DataSize);
460 // Fill in any extra bytes we allocated for padding
461 if (PaddingSize != 0) {
462 memset(Addr + DataSize, 0, PaddingSize);
463 // Update the DataSize variable so that the stub offset is set correctly.
464 DataSize += PaddingSize;
467 DEBUG(dbgs() << "emitSection SectionID: " << SectionID << " Name: " << Name
468 << " obj addr: " << format("%p", pData)
469 << " new addr: " << format("%p", Addr)
470 << " DataSize: " << DataSize << " StubBufSize: " << StubBufSize
471 << " Allocate: " << Allocate << "\n");
472 Obj.updateSectionAddress(Section, (uint64_t)Addr);
474 // Even if we didn't load the section, we need to record an entry for it
475 // to handle later processing (and by 'handle' I mean don't do anything
476 // with these sections).
479 DEBUG(dbgs() << "emitSection SectionID: " << SectionID << " Name: " << Name
480 << " obj addr: " << format("%p", data.data()) << " new addr: 0"
481 << " DataSize: " << DataSize << " StubBufSize: " << StubBufSize
482 << " Allocate: " << Allocate << "\n");
485 Sections.push_back(SectionEntry(Name, Addr, DataSize, (uintptr_t)pData));
489 unsigned RuntimeDyldImpl::findOrEmitSection(ObjectImage &Obj,
490 const SectionRef &Section,
492 ObjSectionToIDMap &LocalSections) {
494 unsigned SectionID = 0;
495 ObjSectionToIDMap::iterator i = LocalSections.find(Section);
496 if (i != LocalSections.end())
497 SectionID = i->second;
499 SectionID = emitSection(Obj, Section, IsCode);
500 LocalSections[Section] = SectionID;
505 void RuntimeDyldImpl::addRelocationForSection(const RelocationEntry &RE,
506 unsigned SectionID) {
507 Relocations[SectionID].push_back(RE);
510 void RuntimeDyldImpl::addRelocationForSymbol(const RelocationEntry &RE,
511 StringRef SymbolName) {
512 // Relocation by symbol. If the symbol is found in the global symbol table,
513 // create an appropriate section relocation. Otherwise, add it to
514 // ExternalSymbolRelocations.
515 SymbolTableMap::const_iterator Loc = GlobalSymbolTable.find(SymbolName);
516 if (Loc == GlobalSymbolTable.end()) {
517 ExternalSymbolRelocations[SymbolName].push_back(RE);
519 // Copy the RE since we want to modify its addend.
520 RelocationEntry RECopy = RE;
521 RECopy.Addend += Loc->second.second;
522 Relocations[Loc->second.first].push_back(RECopy);
526 uint8_t *RuntimeDyldImpl::createStubFunction(uint8_t *Addr,
527 unsigned AbiVariant) {
528 if (Arch == Triple::aarch64 || Arch == Triple::aarch64_be) {
529 // This stub has to be able to access the full address space,
530 // since symbol lookup won't necessarily find a handy, in-range,
531 // PLT stub for functions which could be anywhere.
532 uint32_t *StubAddr = (uint32_t *)Addr;
534 // Stub can use ip0 (== x16) to calculate address
535 *StubAddr = 0xd2e00010; // movz ip0, #:abs_g3:<addr>
537 *StubAddr = 0xf2c00010; // movk ip0, #:abs_g2_nc:<addr>
539 *StubAddr = 0xf2a00010; // movk ip0, #:abs_g1_nc:<addr>
541 *StubAddr = 0xf2800010; // movk ip0, #:abs_g0_nc:<addr>
543 *StubAddr = 0xd61f0200; // br ip0
546 } else if (Arch == Triple::arm || Arch == Triple::armeb) {
547 // TODO: There is only ARM far stub now. We should add the Thumb stub,
548 // and stubs for branches Thumb - ARM and ARM - Thumb.
549 uint32_t *StubAddr = (uint32_t *)Addr;
550 *StubAddr = 0xe51ff004; // ldr pc,<label>
551 return (uint8_t *)++StubAddr;
552 } else if (Arch == Triple::mipsel || Arch == Triple::mips) {
553 uint32_t *StubAddr = (uint32_t *)Addr;
554 // 0: 3c190000 lui t9,%hi(addr).
555 // 4: 27390000 addiu t9,t9,%lo(addr).
556 // 8: 03200008 jr t9.
558 const unsigned LuiT9Instr = 0x3c190000, AdduiT9Instr = 0x27390000;
559 const unsigned JrT9Instr = 0x03200008, NopInstr = 0x0;
561 *StubAddr = LuiT9Instr;
563 *StubAddr = AdduiT9Instr;
565 *StubAddr = JrT9Instr;
567 *StubAddr = NopInstr;
569 } else if (Arch == Triple::ppc64 || Arch == Triple::ppc64le) {
570 // Depending on which version of the ELF ABI is in use, we need to
571 // generate one of two variants of the stub. They both start with
572 // the same sequence to load the target address into r12.
573 writeInt32BE(Addr, 0x3D800000); // lis r12, highest(addr)
574 writeInt32BE(Addr+4, 0x618C0000); // ori r12, higher(addr)
575 writeInt32BE(Addr+8, 0x798C07C6); // sldi r12, r12, 32
576 writeInt32BE(Addr+12, 0x658C0000); // oris r12, r12, h(addr)
577 writeInt32BE(Addr+16, 0x618C0000); // ori r12, r12, l(addr)
578 if (AbiVariant == 2) {
579 // PowerPC64 stub ELFv2 ABI: The address points to the function itself.
580 // The address is already in r12 as required by the ABI. Branch to it.
581 writeInt32BE(Addr+20, 0xF8410018); // std r2, 24(r1)
582 writeInt32BE(Addr+24, 0x7D8903A6); // mtctr r12
583 writeInt32BE(Addr+28, 0x4E800420); // bctr
585 // PowerPC64 stub ELFv1 ABI: The address points to a function descriptor.
586 // Load the function address on r11 and sets it to control register. Also
587 // loads the function TOC in r2 and environment pointer to r11.
588 writeInt32BE(Addr+20, 0xF8410028); // std r2, 40(r1)
589 writeInt32BE(Addr+24, 0xE96C0000); // ld r11, 0(r12)
590 writeInt32BE(Addr+28, 0xE84C0008); // ld r2, 0(r12)
591 writeInt32BE(Addr+32, 0x7D6903A6); // mtctr r11
592 writeInt32BE(Addr+36, 0xE96C0010); // ld r11, 16(r2)
593 writeInt32BE(Addr+40, 0x4E800420); // bctr
596 } else if (Arch == Triple::systemz) {
597 writeInt16BE(Addr, 0xC418); // lgrl %r1,.+8
598 writeInt16BE(Addr+2, 0x0000);
599 writeInt16BE(Addr+4, 0x0004);
600 writeInt16BE(Addr+6, 0x07F1); // brc 15,%r1
601 // 8-byte address stored at Addr + 8
603 } else if (Arch == Triple::x86_64) {
605 *(Addr+1) = 0x25; // rip
606 // 32-bit PC-relative address of the GOT entry will be stored at Addr+2
607 } else if (Arch == Triple::x86) {
608 *Addr = 0xE9; // 32-bit pc-relative jump.
613 // Assign an address to a symbol name and resolve all the relocations
614 // associated with it.
615 void RuntimeDyldImpl::reassignSectionAddress(unsigned SectionID,
617 // The address to use for relocation resolution is not
618 // the address of the local section buffer. We must be doing
619 // a remote execution environment of some sort. Relocations can't
620 // be applied until all the sections have been moved. The client must
621 // trigger this with a call to MCJIT::finalize() or
622 // RuntimeDyld::resolveRelocations().
624 // Addr is a uint64_t because we can't assume the pointer width
625 // of the target is the same as that of the host. Just use a generic
626 // "big enough" type.
627 Sections[SectionID].LoadAddress = Addr;
630 void RuntimeDyldImpl::resolveRelocationList(const RelocationList &Relocs,
632 for (unsigned i = 0, e = Relocs.size(); i != e; ++i) {
633 const RelocationEntry &RE = Relocs[i];
634 // Ignore relocations for sections that were not loaded
635 if (Sections[RE.SectionID].Address == nullptr)
637 resolveRelocation(RE, Value);
641 void RuntimeDyldImpl::resolveExternalSymbols() {
642 while (!ExternalSymbolRelocations.empty()) {
643 StringMap<RelocationList>::iterator i = ExternalSymbolRelocations.begin();
645 StringRef Name = i->first();
646 if (Name.size() == 0) {
647 // This is an absolute symbol, use an address of zero.
648 DEBUG(dbgs() << "Resolving absolute relocations."
650 RelocationList &Relocs = i->second;
651 resolveRelocationList(Relocs, 0);
654 SymbolTableMap::const_iterator Loc = GlobalSymbolTable.find(Name);
655 if (Loc == GlobalSymbolTable.end()) {
656 // This is an external symbol, try to get its address from
658 Addr = MemMgr->getSymbolAddress(Name.data());
659 // The call to getSymbolAddress may have caused additional modules to
660 // be loaded, which may have added new entries to the
661 // ExternalSymbolRelocations map. Consquently, we need to update our
662 // iterator. This is also why retrieval of the relocation list
663 // associated with this symbol is deferred until below this point.
664 // New entries may have been added to the relocation list.
665 i = ExternalSymbolRelocations.find(Name);
667 // We found the symbol in our global table. It was probably in a
668 // Module that we loaded previously.
669 SymbolLoc SymLoc = Loc->second;
670 Addr = getSectionLoadAddress(SymLoc.first) + SymLoc.second;
673 // FIXME: Implement error handling that doesn't kill the host program!
675 report_fatal_error("Program used external function '" + Name +
676 "' which could not be resolved!");
678 updateGOTEntries(Name, Addr);
679 DEBUG(dbgs() << "Resolving relocations Name: " << Name << "\t"
680 << format("0x%lx", Addr) << "\n");
681 // This list may have been updated when we called getSymbolAddress, so
682 // don't change this code to get the list earlier.
683 RelocationList &Relocs = i->second;
684 resolveRelocationList(Relocs, Addr);
687 ExternalSymbolRelocations.erase(i);
691 //===----------------------------------------------------------------------===//
692 // RuntimeDyld class implementation
693 RuntimeDyld::RuntimeDyld(RTDyldMemoryManager *mm) {
694 // FIXME: There's a potential issue lurking here if a single instance of
695 // RuntimeDyld is used to load multiple objects. The current implementation
696 // associates a single memory manager with a RuntimeDyld instance. Even
697 // though the public class spawns a new 'impl' instance for each load,
698 // they share a single memory manager. This can become a problem when page
699 // permissions are applied.
702 ProcessAllSections = false;
706 RuntimeDyld::~RuntimeDyld() { delete Dyld; }
708 static std::unique_ptr<RuntimeDyldELF>
709 createRuntimeDyldELF(RTDyldMemoryManager *MM, bool ProcessAllSections,
710 RuntimeDyldCheckerImpl *Checker) {
711 std::unique_ptr<RuntimeDyldELF> Dyld(new RuntimeDyldELF(MM));
712 Dyld->setProcessAllSections(ProcessAllSections);
713 Dyld->setRuntimeDyldChecker(Checker);
717 static std::unique_ptr<RuntimeDyldMachO>
718 createRuntimeDyldMachO(Triple::ArchType Arch, RTDyldMemoryManager *MM,
719 bool ProcessAllSections, RuntimeDyldCheckerImpl *Checker) {
720 std::unique_ptr<RuntimeDyldMachO> Dyld(RuntimeDyldMachO::create(Arch, MM));
721 Dyld->setProcessAllSections(ProcessAllSections);
722 Dyld->setRuntimeDyldChecker(Checker);
726 ObjectImage *RuntimeDyld::loadObject(std::unique_ptr<ObjectFile> InputObject) {
727 std::unique_ptr<ObjectImage> InputImage;
729 ObjectFile &Obj = *InputObject;
731 if (InputObject->isELF()) {
732 InputImage.reset(RuntimeDyldELF::createObjectImageFromFile(std::move(InputObject)));
734 Dyld = createRuntimeDyldELF(MM, ProcessAllSections, Checker).release();
735 } else if (InputObject->isMachO()) {
736 InputImage.reset(RuntimeDyldMachO::createObjectImageFromFile(std::move(InputObject)));
738 Dyld = createRuntimeDyldMachO(
739 static_cast<Triple::ArchType>(InputImage->getArch()),
740 MM, ProcessAllSections, Checker).release();
742 report_fatal_error("Incompatible object format!");
744 if (!Dyld->isCompatibleFile(&Obj))
745 report_fatal_error("Incompatible object format!");
747 Dyld->loadObject(InputImage.get());
748 return InputImage.release();
751 ObjectImage *RuntimeDyld::loadObject(ObjectBuffer *InputBuffer) {
752 std::unique_ptr<ObjectImage> InputImage;
753 sys::fs::file_magic Type = sys::fs::identify_magic(InputBuffer->getBuffer());
756 case sys::fs::file_magic::elf_relocatable:
757 case sys::fs::file_magic::elf_executable:
758 case sys::fs::file_magic::elf_shared_object:
759 case sys::fs::file_magic::elf_core:
760 InputImage.reset(RuntimeDyldELF::createObjectImage(InputBuffer));
762 Dyld = createRuntimeDyldELF(MM, ProcessAllSections, Checker).release();
764 case sys::fs::file_magic::macho_object:
765 case sys::fs::file_magic::macho_executable:
766 case sys::fs::file_magic::macho_fixed_virtual_memory_shared_lib:
767 case sys::fs::file_magic::macho_core:
768 case sys::fs::file_magic::macho_preload_executable:
769 case sys::fs::file_magic::macho_dynamically_linked_shared_lib:
770 case sys::fs::file_magic::macho_dynamic_linker:
771 case sys::fs::file_magic::macho_bundle:
772 case sys::fs::file_magic::macho_dynamically_linked_shared_lib_stub:
773 case sys::fs::file_magic::macho_dsym_companion:
774 InputImage.reset(RuntimeDyldMachO::createObjectImage(InputBuffer));
776 Dyld = createRuntimeDyldMachO(
777 static_cast<Triple::ArchType>(InputImage->getArch()),
778 MM, ProcessAllSections, Checker).release();
780 case sys::fs::file_magic::unknown:
781 case sys::fs::file_magic::bitcode:
782 case sys::fs::file_magic::archive:
783 case sys::fs::file_magic::coff_object:
784 case sys::fs::file_magic::coff_import_library:
785 case sys::fs::file_magic::pecoff_executable:
786 case sys::fs::file_magic::macho_universal_binary:
787 case sys::fs::file_magic::windows_resource:
788 report_fatal_error("Incompatible object format!");
791 if (!Dyld->isCompatibleFormat(InputBuffer))
792 report_fatal_error("Incompatible object format!");
794 Dyld->loadObject(InputImage.get());
795 return InputImage.release();
798 void *RuntimeDyld::getSymbolAddress(StringRef Name) {
801 return Dyld->getSymbolAddress(Name);
804 uint64_t RuntimeDyld::getSymbolLoadAddress(StringRef Name) {
807 return Dyld->getSymbolLoadAddress(Name);
810 void RuntimeDyld::resolveRelocations() { Dyld->resolveRelocations(); }
812 void RuntimeDyld::reassignSectionAddress(unsigned SectionID, uint64_t Addr) {
813 Dyld->reassignSectionAddress(SectionID, Addr);
816 void RuntimeDyld::mapSectionAddress(const void *LocalAddress,
817 uint64_t TargetAddress) {
818 Dyld->mapSectionAddress(LocalAddress, TargetAddress);
821 bool RuntimeDyld::hasError() { return Dyld->hasError(); }
823 StringRef RuntimeDyld::getErrorString() { return Dyld->getErrorString(); }
825 void RuntimeDyld::registerEHFrames() {
827 Dyld->registerEHFrames();
830 void RuntimeDyld::deregisterEHFrames() {
832 Dyld->deregisterEHFrames();
835 } // end namespace llvm