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/FileSystem.h"
23 #include "llvm/Support/MathExtras.h"
24 #include "llvm/Support/MutexGuard.h"
27 using namespace llvm::object;
29 // Empty out-of-line virtual destructor as the key function.
30 RuntimeDyldImpl::~RuntimeDyldImpl() {}
32 // Pin the JITRegistrar's and ObjectImage*'s vtables to this file.
33 void JITRegistrar::anchor() {}
34 void ObjectImage::anchor() {}
35 void ObjectImageCommon::anchor() {}
39 void RuntimeDyldImpl::registerEHFrames() {
42 void RuntimeDyldImpl::deregisterEHFrames() {
45 // Resolve the relocations for all symbols we currently know about.
46 void RuntimeDyldImpl::resolveRelocations() {
47 MutexGuard locked(lock);
49 // First, resolve relocations associated with external symbols.
50 resolveExternalSymbols();
52 // Just iterate over the sections we have and resolve all the relocations
53 // in them. Gross overkill, but it gets the job done.
54 for (int i = 0, e = Sections.size(); i != e; ++i) {
55 // The Section here (Sections[i]) refers to the section in which the
56 // symbol for the relocation is located. The SectionID in the relocation
57 // entry provides the section to which the relocation will be applied.
58 uint64_t Addr = Sections[i].LoadAddress;
59 DEBUG(dbgs() << "Resolving relocations Section #" << i
60 << "\t" << format("%p", (uint8_t *)Addr)
62 resolveRelocationList(Relocations[i], Addr);
67 void RuntimeDyldImpl::mapSectionAddress(const void *LocalAddress,
68 uint64_t TargetAddress) {
69 MutexGuard locked(lock);
70 for (unsigned i = 0, e = Sections.size(); i != e; ++i) {
71 if (Sections[i].Address == LocalAddress) {
72 reassignSectionAddress(i, TargetAddress);
76 llvm_unreachable("Attempting to remap address of unknown section!");
79 // Subclasses can implement this method to create specialized image instances.
80 // The caller owns the pointer that is returned.
81 ObjectImage *RuntimeDyldImpl::createObjectImage(ObjectBuffer *InputBuffer) {
82 return new ObjectImageCommon(InputBuffer);
85 ObjectImage *RuntimeDyldImpl::createObjectImageFromFile(ObjectFile *InputObject) {
86 return new ObjectImageCommon(InputObject);
89 ObjectImage *RuntimeDyldImpl::loadObject(ObjectFile *InputObject) {
90 return loadObject(createObjectImageFromFile(InputObject));
93 ObjectImage *RuntimeDyldImpl::loadObject(ObjectBuffer *InputBuffer) {
94 return loadObject(createObjectImage(InputBuffer));
97 ObjectImage *RuntimeDyldImpl::loadObject(ObjectImage *InputObject) {
98 MutexGuard locked(lock);
100 OwningPtr<ObjectImage> obj(InputObject);
104 // Save information about our target
105 Arch = (Triple::ArchType)obj->getArch();
106 IsTargetLittleEndian = obj->getObjectFile()->isLittleEndian();
108 // Symbols found in this object
109 StringMap<SymbolLoc> LocalSymbols;
110 // Used sections from the object file
111 ObjSectionToIDMap LocalSections;
113 // Common symbols requiring allocation, with their sizes and alignments
114 CommonSymbolMap CommonSymbols;
115 // Maximum required total memory to allocate all common symbols
116 uint64_t CommonSize = 0;
119 DEBUG(dbgs() << "Parse symbols:\n");
120 for (symbol_iterator i = obj->begin_symbols(), e = obj->end_symbols(); i != e;
122 object::SymbolRef::Type SymType;
124 Check(i->getType(SymType));
125 Check(i->getName(Name));
127 uint32_t flags = i->getFlags();
129 bool isCommon = flags & SymbolRef::SF_Common;
131 // Add the common symbols to a list. We'll allocate them all below.
133 Check(i->getAlignment(Align));
135 Check(i->getSize(Size));
136 CommonSize += Size + Align;
137 CommonSymbols[*i] = CommonSymbolInfo(Size, Align);
139 if (SymType == object::SymbolRef::ST_Function ||
140 SymType == object::SymbolRef::ST_Data ||
141 SymType == object::SymbolRef::ST_Unknown) {
143 StringRef SectionData;
145 section_iterator si = obj->end_sections();
146 Check(i->getFileOffset(FileOffset));
147 Check(i->getSection(si));
148 if (si == obj->end_sections()) continue;
149 Check(si->getContents(SectionData));
150 Check(si->isText(IsCode));
151 const uint8_t* SymPtr = (const uint8_t*)InputObject->getData().data() +
152 (uintptr_t)FileOffset;
153 uintptr_t SectOffset = (uintptr_t)(SymPtr -
154 (const uint8_t*)SectionData.begin());
155 unsigned SectionID = findOrEmitSection(*obj, *si, IsCode, LocalSections);
156 LocalSymbols[Name.data()] = SymbolLoc(SectionID, SectOffset);
157 DEBUG(dbgs() << "\tFileOffset: " << format("%p", (uintptr_t)FileOffset)
158 << " flags: " << flags
159 << " SID: " << SectionID
160 << " Offset: " << format("%p", SectOffset));
161 GlobalSymbolTable[Name] = SymbolLoc(SectionID, SectOffset);
164 DEBUG(dbgs() << "\tType: " << SymType << " Name: " << Name << "\n");
167 // Allocate common symbols
169 emitCommonSymbols(*obj, CommonSymbols, CommonSize, LocalSymbols);
171 // Parse and process relocations
172 DEBUG(dbgs() << "Parse relocations:\n");
173 for (section_iterator si = obj->begin_sections(), se = obj->end_sections();
175 bool isFirstRelocation = true;
176 unsigned SectionID = 0;
178 section_iterator RelocatedSection = si->getRelocatedSection();
180 for (relocation_iterator i = si->relocation_begin(),
181 e = si->relocation_end();
183 // If it's the first relocation in this section, find its SectionID
184 if (isFirstRelocation) {
186 findOrEmitSection(*obj, *RelocatedSection, true, LocalSections);
187 DEBUG(dbgs() << "\tSectionID: " << SectionID << "\n");
188 isFirstRelocation = false;
191 processRelocationRef(SectionID, *i, *obj, LocalSections, LocalSymbols,
196 // Give the subclasses a chance to tie-up any loose ends.
197 finalizeLoad(LocalSections);
202 void RuntimeDyldImpl::emitCommonSymbols(ObjectImage &Obj,
203 const CommonSymbolMap &CommonSymbols,
205 SymbolTableMap &SymbolTable) {
206 // Allocate memory for the section
207 unsigned SectionID = Sections.size();
208 uint8_t *Addr = MemMgr->allocateDataSection(
209 TotalSize, sizeof(void*), SectionID, StringRef(), false);
211 report_fatal_error("Unable to allocate memory for common symbols!");
213 Sections.push_back(SectionEntry(StringRef(), Addr, TotalSize, 0));
214 memset(Addr, 0, TotalSize);
216 DEBUG(dbgs() << "emitCommonSection SectionID: " << SectionID
217 << " new addr: " << format("%p", Addr)
218 << " DataSize: " << TotalSize
221 // Assign the address of each symbol
222 for (CommonSymbolMap::const_iterator it = CommonSymbols.begin(),
223 itEnd = CommonSymbols.end(); it != itEnd; it++) {
224 uint64_t Size = it->second.first;
225 uint64_t Align = it->second.second;
227 it->first.getName(Name);
229 // This symbol has an alignment requirement.
230 uint64_t AlignOffset = OffsetToAlignment((uint64_t)Addr, Align);
232 Offset += AlignOffset;
233 DEBUG(dbgs() << "Allocating common symbol " << Name << " address " <<
234 format("%p\n", Addr));
236 Obj.updateSymbolAddress(it->first, (uint64_t)Addr);
237 SymbolTable[Name.data()] = SymbolLoc(SectionID, Offset);
243 unsigned RuntimeDyldImpl::emitSection(ObjectImage &Obj,
244 const SectionRef &Section,
247 unsigned StubBufSize = 0,
248 StubSize = getMaxStubSize();
249 const ObjectFile *ObjFile = Obj.getObjectFile();
250 // FIXME: this is an inefficient way to handle this. We should computed the
251 // necessary section allocation size in loadObject by walking all the sections
254 for (section_iterator SI = ObjFile->section_begin(),
255 SE = ObjFile->section_end();
257 section_iterator RelSecI = SI->getRelocatedSection();
258 if (!(RelSecI == Section))
261 for (relocation_iterator I = SI->relocation_begin(),
262 E = SI->relocation_end();
264 StubBufSize += StubSize;
270 uint64_t Alignment64;
271 Check(Section.getContents(data));
272 Check(Section.getAlignment(Alignment64));
274 unsigned Alignment = (unsigned)Alignment64 & 0xffffffffL;
280 unsigned PaddingSize = 0;
282 Check(Section.isRequiredForExecution(IsRequired));
283 Check(Section.isVirtual(IsVirtual));
284 Check(Section.isZeroInit(IsZeroInit));
285 Check(Section.isReadOnlyData(IsReadOnly));
286 Check(Section.getSize(DataSize));
287 Check(Section.getName(Name));
289 unsigned StubAlignment = getStubAlignment();
290 unsigned EndAlignment = (DataSize | Alignment) & -(DataSize | Alignment);
291 if (StubAlignment > EndAlignment)
292 StubBufSize += StubAlignment - EndAlignment;
295 // The .eh_frame section (at least on Linux) needs an extra four bytes padded
296 // with zeroes added at the end. For MachO objects, this section has a
297 // slightly different name, so this won't have any effect for MachO objects.
298 if (Name == ".eh_frame")
302 unsigned SectionID = Sections.size();
304 const char *pData = 0;
306 // Some sections, such as debug info, don't need to be loaded for execution.
307 // Leave those where they are.
309 Allocate = DataSize + PaddingSize + StubBufSize;
311 ? MemMgr->allocateCodeSection(Allocate, Alignment, SectionID, Name)
312 : MemMgr->allocateDataSection(Allocate, Alignment, SectionID, Name,
315 report_fatal_error("Unable to allocate section memory!");
317 // Virtual sections have no data in the object image, so leave pData = 0
321 // Zero-initialize or copy the data from the image
322 if (IsZeroInit || IsVirtual)
323 memset(Addr, 0, DataSize);
325 memcpy(Addr, pData, DataSize);
327 // Fill in any extra bytes we allocated for padding
328 if (PaddingSize != 0) {
329 memset(Addr + DataSize, 0, PaddingSize);
330 // Update the DataSize variable so that the stub offset is set correctly.
331 DataSize += PaddingSize;
334 DEBUG(dbgs() << "emitSection SectionID: " << SectionID
336 << " obj addr: " << format("%p", pData)
337 << " new addr: " << format("%p", Addr)
338 << " DataSize: " << DataSize
339 << " StubBufSize: " << StubBufSize
340 << " Allocate: " << Allocate
342 Obj.updateSectionAddress(Section, (uint64_t)Addr);
345 // Even if we didn't load the section, we need to record an entry for it
346 // to handle later processing (and by 'handle' I mean don't do anything
347 // with these sections).
350 DEBUG(dbgs() << "emitSection SectionID: " << SectionID
352 << " obj addr: " << format("%p", data.data())
354 << " DataSize: " << DataSize
355 << " StubBufSize: " << StubBufSize
356 << " Allocate: " << Allocate
360 Sections.push_back(SectionEntry(Name, Addr, DataSize, (uintptr_t)pData));
364 unsigned RuntimeDyldImpl::findOrEmitSection(ObjectImage &Obj,
365 const SectionRef &Section,
367 ObjSectionToIDMap &LocalSections) {
369 unsigned SectionID = 0;
370 ObjSectionToIDMap::iterator i = LocalSections.find(Section);
371 if (i != LocalSections.end())
372 SectionID = i->second;
374 SectionID = emitSection(Obj, Section, IsCode);
375 LocalSections[Section] = SectionID;
380 void RuntimeDyldImpl::addRelocationForSection(const RelocationEntry &RE,
381 unsigned SectionID) {
382 Relocations[SectionID].push_back(RE);
385 void RuntimeDyldImpl::addRelocationForSymbol(const RelocationEntry &RE,
386 StringRef SymbolName) {
387 // Relocation by symbol. If the symbol is found in the global symbol table,
388 // create an appropriate section relocation. Otherwise, add it to
389 // ExternalSymbolRelocations.
390 SymbolTableMap::const_iterator Loc =
391 GlobalSymbolTable.find(SymbolName);
392 if (Loc == GlobalSymbolTable.end()) {
393 ExternalSymbolRelocations[SymbolName].push_back(RE);
395 // Copy the RE since we want to modify its addend.
396 RelocationEntry RECopy = RE;
397 RECopy.Addend += Loc->second.second;
398 Relocations[Loc->second.first].push_back(RECopy);
402 uint8_t *RuntimeDyldImpl::createStubFunction(uint8_t *Addr) {
403 if (Arch == Triple::aarch64) {
404 // This stub has to be able to access the full address space,
405 // since symbol lookup won't necessarily find a handy, in-range,
406 // PLT stub for functions which could be anywhere.
407 uint32_t *StubAddr = (uint32_t*)Addr;
409 // Stub can use ip0 (== x16) to calculate address
410 *StubAddr = 0xd2e00010; // movz ip0, #:abs_g3:<addr>
412 *StubAddr = 0xf2c00010; // movk ip0, #:abs_g2_nc:<addr>
414 *StubAddr = 0xf2a00010; // movk ip0, #:abs_g1_nc:<addr>
416 *StubAddr = 0xf2800010; // movk ip0, #:abs_g0_nc:<addr>
418 *StubAddr = 0xd61f0200; // br ip0
421 } else if (Arch == Triple::arm) {
422 // TODO: There is only ARM far stub now. We should add the Thumb stub,
423 // and stubs for branches Thumb - ARM and ARM - Thumb.
424 uint32_t *StubAddr = (uint32_t*)Addr;
425 *StubAddr = 0xe51ff004; // ldr pc,<label>
426 return (uint8_t*)++StubAddr;
427 } else if (Arch == Triple::mipsel || Arch == Triple::mips) {
428 uint32_t *StubAddr = (uint32_t*)Addr;
429 // 0: 3c190000 lui t9,%hi(addr).
430 // 4: 27390000 addiu t9,t9,%lo(addr).
431 // 8: 03200008 jr t9.
433 const unsigned LuiT9Instr = 0x3c190000, AdduiT9Instr = 0x27390000;
434 const unsigned JrT9Instr = 0x03200008, NopInstr = 0x0;
436 *StubAddr = LuiT9Instr;
438 *StubAddr = AdduiT9Instr;
440 *StubAddr = JrT9Instr;
442 *StubAddr = NopInstr;
444 } else if (Arch == Triple::ppc64 || Arch == Triple::ppc64le) {
445 // PowerPC64 stub: the address points to a function descriptor
446 // instead of the function itself. Load the function address
447 // on r11 and sets it to control register. Also loads the function
448 // TOC in r2 and environment pointer to r11.
449 writeInt32BE(Addr, 0x3D800000); // lis r12, highest(addr)
450 writeInt32BE(Addr+4, 0x618C0000); // ori r12, higher(addr)
451 writeInt32BE(Addr+8, 0x798C07C6); // sldi r12, r12, 32
452 writeInt32BE(Addr+12, 0x658C0000); // oris r12, r12, h(addr)
453 writeInt32BE(Addr+16, 0x618C0000); // ori r12, r12, l(addr)
454 writeInt32BE(Addr+20, 0xF8410028); // std r2, 40(r1)
455 writeInt32BE(Addr+24, 0xE96C0000); // ld r11, 0(r12)
456 writeInt32BE(Addr+28, 0xE84C0008); // ld r2, 0(r12)
457 writeInt32BE(Addr+32, 0x7D6903A6); // mtctr r11
458 writeInt32BE(Addr+36, 0xE96C0010); // ld r11, 16(r2)
459 writeInt32BE(Addr+40, 0x4E800420); // bctr
462 } else if (Arch == Triple::systemz) {
463 writeInt16BE(Addr, 0xC418); // lgrl %r1,.+8
464 writeInt16BE(Addr+2, 0x0000);
465 writeInt16BE(Addr+4, 0x0004);
466 writeInt16BE(Addr+6, 0x07F1); // brc 15,%r1
467 // 8-byte address stored at Addr + 8
469 } else if (Arch == Triple::x86_64) {
471 *(Addr+1) = 0x25; // rip
472 // 32-bit PC-relative address of the GOT entry will be stored at Addr+2
477 // Assign an address to a symbol name and resolve all the relocations
478 // associated with it.
479 void RuntimeDyldImpl::reassignSectionAddress(unsigned SectionID,
481 // The address to use for relocation resolution is not
482 // the address of the local section buffer. We must be doing
483 // a remote execution environment of some sort. Relocations can't
484 // be applied until all the sections have been moved. The client must
485 // trigger this with a call to MCJIT::finalize() or
486 // RuntimeDyld::resolveRelocations().
488 // Addr is a uint64_t because we can't assume the pointer width
489 // of the target is the same as that of the host. Just use a generic
490 // "big enough" type.
491 Sections[SectionID].LoadAddress = Addr;
494 void RuntimeDyldImpl::resolveRelocationList(const RelocationList &Relocs,
496 for (unsigned i = 0, e = Relocs.size(); i != e; ++i) {
497 const RelocationEntry &RE = Relocs[i];
498 // Ignore relocations for sections that were not loaded
499 if (Sections[RE.SectionID].Address == 0)
501 resolveRelocation(RE, Value);
505 void RuntimeDyldImpl::resolveExternalSymbols() {
506 while(!ExternalSymbolRelocations.empty()) {
507 StringMap<RelocationList>::iterator i = ExternalSymbolRelocations.begin();
509 StringRef Name = i->first();
510 if (Name.size() == 0) {
511 // This is an absolute symbol, use an address of zero.
512 DEBUG(dbgs() << "Resolving absolute relocations." << "\n");
513 RelocationList &Relocs = i->second;
514 resolveRelocationList(Relocs, 0);
517 SymbolTableMap::const_iterator Loc = GlobalSymbolTable.find(Name);
518 if (Loc == GlobalSymbolTable.end()) {
519 // This is an external symbol, try to get its address from
521 Addr = MemMgr->getSymbolAddress(Name.data());
522 // The call to getSymbolAddress may have caused additional modules to
523 // be loaded, which may have added new entries to the
524 // ExternalSymbolRelocations map. Consquently, we need to update our
525 // iterator. This is also why retrieval of the relocation list
526 // associated with this symbol is deferred until below this point.
527 // New entries may have been added to the relocation list.
528 i = ExternalSymbolRelocations.find(Name);
530 // We found the symbol in our global table. It was probably in a
531 // Module that we loaded previously.
532 SymbolLoc SymLoc = Loc->second;
533 Addr = getSectionLoadAddress(SymLoc.first) + SymLoc.second;
536 // FIXME: Implement error handling that doesn't kill the host program!
538 report_fatal_error("Program used external function '" + Name +
539 "' which could not be resolved!");
541 updateGOTEntries(Name, Addr);
542 DEBUG(dbgs() << "Resolving relocations Name: " << Name
543 << "\t" << format("0x%lx", Addr)
545 // This list may have been updated when we called getSymbolAddress, so
546 // don't change this code to get the list earlier.
547 RelocationList &Relocs = i->second;
548 resolveRelocationList(Relocs, Addr);
551 ExternalSymbolRelocations.erase(i);
556 //===----------------------------------------------------------------------===//
557 // RuntimeDyld class implementation
558 RuntimeDyld::RuntimeDyld(RTDyldMemoryManager *mm) {
559 // FIXME: There's a potential issue lurking here if a single instance of
560 // RuntimeDyld is used to load multiple objects. The current implementation
561 // associates a single memory manager with a RuntimeDyld instance. Even
562 // though the public class spawns a new 'impl' instance for each load,
563 // they share a single memory manager. This can become a problem when page
564 // permissions are applied.
569 RuntimeDyld::~RuntimeDyld() {
573 ObjectImage *RuntimeDyld::loadObject(ObjectFile *InputObject) {
575 if (InputObject->isELF())
576 Dyld = new RuntimeDyldELF(MM);
577 else if (InputObject->isMachO())
578 Dyld = new RuntimeDyldMachO(MM);
580 report_fatal_error("Incompatible object format!");
582 if (!Dyld->isCompatibleFile(InputObject))
583 report_fatal_error("Incompatible object format!");
586 return Dyld->loadObject(InputObject);
589 ObjectImage *RuntimeDyld::loadObject(ObjectBuffer *InputBuffer) {
591 sys::fs::file_magic Type =
592 sys::fs::identify_magic(InputBuffer->getBuffer());
594 case sys::fs::file_magic::elf_relocatable:
595 case sys::fs::file_magic::elf_executable:
596 case sys::fs::file_magic::elf_shared_object:
597 case sys::fs::file_magic::elf_core:
598 Dyld = new RuntimeDyldELF(MM);
600 case sys::fs::file_magic::macho_object:
601 case sys::fs::file_magic::macho_executable:
602 case sys::fs::file_magic::macho_fixed_virtual_memory_shared_lib:
603 case sys::fs::file_magic::macho_core:
604 case sys::fs::file_magic::macho_preload_executable:
605 case sys::fs::file_magic::macho_dynamically_linked_shared_lib:
606 case sys::fs::file_magic::macho_dynamic_linker:
607 case sys::fs::file_magic::macho_bundle:
608 case sys::fs::file_magic::macho_dynamically_linked_shared_lib_stub:
609 case sys::fs::file_magic::macho_dsym_companion:
610 Dyld = new RuntimeDyldMachO(MM);
612 case sys::fs::file_magic::unknown:
613 case sys::fs::file_magic::bitcode:
614 case sys::fs::file_magic::archive:
615 case sys::fs::file_magic::coff_object:
616 case sys::fs::file_magic::coff_import_library:
617 case sys::fs::file_magic::pecoff_executable:
618 case sys::fs::file_magic::macho_universal_binary:
619 case sys::fs::file_magic::windows_resource:
620 report_fatal_error("Incompatible object format!");
623 if (!Dyld->isCompatibleFormat(InputBuffer))
624 report_fatal_error("Incompatible object format!");
627 return Dyld->loadObject(InputBuffer);
630 void *RuntimeDyld::getSymbolAddress(StringRef Name) {
633 return Dyld->getSymbolAddress(Name);
636 uint64_t RuntimeDyld::getSymbolLoadAddress(StringRef Name) {
639 return Dyld->getSymbolLoadAddress(Name);
642 void RuntimeDyld::resolveRelocations() {
643 Dyld->resolveRelocations();
646 void RuntimeDyld::reassignSectionAddress(unsigned SectionID,
648 Dyld->reassignSectionAddress(SectionID, Addr);
651 void RuntimeDyld::mapSectionAddress(const void *LocalAddress,
652 uint64_t TargetAddress) {
653 Dyld->mapSectionAddress(LocalAddress, TargetAddress);
656 StringRef RuntimeDyld::getErrorString() {
657 return Dyld->getErrorString();
660 void RuntimeDyld::registerEHFrames() {
662 Dyld->registerEHFrames();
665 void RuntimeDyld::deregisterEHFrames() {
667 Dyld->deregisterEHFrames();
670 } // end namespace llvm