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/FileSystem.h"
21 #include "llvm/Support/MathExtras.h"
22 #include "llvm/Object/ELF.h"
25 using namespace llvm::object;
27 // Empty out-of-line virtual destructor as the key function.
28 RuntimeDyldImpl::~RuntimeDyldImpl() {}
32 StringRef RuntimeDyldImpl::getEHFrameSection() {
36 // Resolve the relocations for all symbols we currently know about.
37 void RuntimeDyldImpl::resolveRelocations() {
38 // First, resolve relocations associated with external symbols.
39 resolveExternalSymbols();
41 // Just iterate over the sections we have and resolve all the relocations
42 // in them. Gross overkill, but it gets the job done.
43 for (int i = 0, e = Sections.size(); i != e; ++i) {
44 // The Section here (Sections[i]) refers to the section in which the
45 // symbol for the relocation is located. The SectionID in the relocation
46 // entry provides the section to which the relocation will be applied.
47 uint64_t Addr = Sections[i].LoadAddress;
48 DEBUG(dbgs() << "Resolving relocations Section #" << i
49 << "\t" << format("%p", (uint8_t *)Addr)
51 resolveRelocationList(Relocations[i], Addr);
56 void RuntimeDyldImpl::mapSectionAddress(const void *LocalAddress,
57 uint64_t TargetAddress) {
58 for (unsigned i = 0, e = Sections.size(); i != e; ++i) {
59 if (Sections[i].Address == LocalAddress) {
60 reassignSectionAddress(i, TargetAddress);
64 llvm_unreachable("Attempting to remap address of unknown section!");
67 // Subclasses can implement this method to create specialized image instances.
68 // The caller owns the pointer that is returned.
69 ObjectImage *RuntimeDyldImpl::createObjectImage(ObjectBuffer *InputBuffer) {
70 return new ObjectImageCommon(InputBuffer);
73 ObjectImage *RuntimeDyldImpl::loadObject(ObjectBuffer *InputBuffer) {
74 OwningPtr<ObjectImage> obj(createObjectImage(InputBuffer));
76 report_fatal_error("Unable to create object image from memory buffer!");
78 Arch = (Triple::ArchType)obj->getArch();
80 // Symbols found in this object
81 StringMap<SymbolLoc> LocalSymbols;
82 // Used sections from the object file
83 ObjSectionToIDMap LocalSections;
85 // Common symbols requiring allocation, with their sizes and alignments
86 CommonSymbolMap CommonSymbols;
87 // Maximum required total memory to allocate all common symbols
88 uint64_t CommonSize = 0;
92 DEBUG(dbgs() << "Parse symbols:\n");
93 for (symbol_iterator i = obj->begin_symbols(), e = obj->end_symbols();
94 i != e; i.increment(err)) {
96 object::SymbolRef::Type SymType;
98 Check(i->getType(SymType));
99 Check(i->getName(Name));
102 Check(i->getFlags(flags));
104 bool isCommon = flags & SymbolRef::SF_Common;
106 // Add the common symbols to a list. We'll allocate them all below.
108 Check(i->getAlignment(Align));
110 Check(i->getSize(Size));
111 CommonSize += Size + Align;
112 CommonSymbols[*i] = CommonSymbolInfo(Size, Align);
114 if (SymType == object::SymbolRef::ST_Function ||
115 SymType == object::SymbolRef::ST_Data ||
116 SymType == object::SymbolRef::ST_Unknown) {
118 StringRef SectionData;
120 section_iterator si = obj->end_sections();
121 Check(i->getFileOffset(FileOffset));
122 Check(i->getSection(si));
123 if (si == obj->end_sections()) continue;
124 Check(si->getContents(SectionData));
125 Check(si->isText(IsCode));
126 const uint8_t* SymPtr = (const uint8_t*)InputBuffer->getBufferStart() +
127 (uintptr_t)FileOffset;
128 uintptr_t SectOffset = (uintptr_t)(SymPtr -
129 (const uint8_t*)SectionData.begin());
130 unsigned SectionID = findOrEmitSection(*obj, *si, IsCode, LocalSections);
131 LocalSymbols[Name.data()] = SymbolLoc(SectionID, SectOffset);
132 DEBUG(dbgs() << "\tFileOffset: " << format("%p", (uintptr_t)FileOffset)
133 << " flags: " << flags
134 << " SID: " << SectionID
135 << " Offset: " << format("%p", SectOffset));
136 GlobalSymbolTable[Name] = SymbolLoc(SectionID, SectOffset);
139 DEBUG(dbgs() << "\tType: " << SymType << " Name: " << Name << "\n");
142 // Allocate common symbols
144 emitCommonSymbols(*obj, CommonSymbols, CommonSize, LocalSymbols);
146 // Parse and process relocations
147 DEBUG(dbgs() << "Parse relocations:\n");
148 for (section_iterator si = obj->begin_sections(),
149 se = obj->end_sections(); si != se; si.increment(err)) {
151 bool isFirstRelocation = true;
152 unsigned SectionID = 0;
154 section_iterator RelocatedSection = si->getRelocatedSection();
156 for (relocation_iterator i = si->begin_relocations(),
157 e = si->end_relocations(); i != e; i.increment(err)) {
160 // If it's the first relocation in this section, find its SectionID
161 if (isFirstRelocation) {
163 findOrEmitSection(*obj, *RelocatedSection, true, LocalSections);
164 DEBUG(dbgs() << "\tSectionID: " << SectionID << "\n");
165 isFirstRelocation = false;
168 processRelocationRef(SectionID, *i, *obj, LocalSections, LocalSymbols,
173 // Give the subclasses a chance to tie-up any loose ends.
179 void RuntimeDyldImpl::emitCommonSymbols(ObjectImage &Obj,
180 const CommonSymbolMap &CommonSymbols,
182 SymbolTableMap &SymbolTable) {
183 // Allocate memory for the section
184 unsigned SectionID = Sections.size();
185 uint8_t *Addr = MemMgr->allocateDataSection(
186 TotalSize, sizeof(void*), SectionID, StringRef(), false);
188 report_fatal_error("Unable to allocate memory for common symbols!");
190 Sections.push_back(SectionEntry(StringRef(), Addr, TotalSize, 0));
191 memset(Addr, 0, TotalSize);
193 DEBUG(dbgs() << "emitCommonSection SectionID: " << SectionID
194 << " new addr: " << format("%p", Addr)
195 << " DataSize: " << TotalSize
198 // Assign the address of each symbol
199 for (CommonSymbolMap::const_iterator it = CommonSymbols.begin(),
200 itEnd = CommonSymbols.end(); it != itEnd; it++) {
201 uint64_t Size = it->second.first;
202 uint64_t Align = it->second.second;
204 it->first.getName(Name);
206 // This symbol has an alignment requirement.
207 uint64_t AlignOffset = OffsetToAlignment((uint64_t)Addr, Align);
209 Offset += AlignOffset;
210 DEBUG(dbgs() << "Allocating common symbol " << Name << " address " <<
211 format("%p\n", Addr));
213 Obj.updateSymbolAddress(it->first, (uint64_t)Addr);
214 SymbolTable[Name.data()] = SymbolLoc(SectionID, Offset);
220 unsigned RuntimeDyldImpl::emitSection(ObjectImage &Obj,
221 const SectionRef &Section,
224 unsigned StubBufSize = 0,
225 StubSize = getMaxStubSize();
227 const ObjectFile *ObjFile = Obj.getObjectFile();
228 // FIXME: this is an inefficient way to handle this. We should computed the
229 // necessary section allocation size in loadObject by walking all the sections
232 for (section_iterator SI = ObjFile->begin_sections(),
233 SE = ObjFile->end_sections();
234 SI != SE; SI.increment(err), Check(err)) {
235 section_iterator RelSecI = SI->getRelocatedSection();
236 if (!(RelSecI == Section))
239 for (relocation_iterator I = SI->begin_relocations(),
240 E = SI->end_relocations(); I != E; I.increment(err), Check(err)) {
241 StubBufSize += StubSize;
247 uint64_t Alignment64;
248 Check(Section.getContents(data));
249 Check(Section.getAlignment(Alignment64));
251 unsigned Alignment = (unsigned)Alignment64 & 0xffffffffL;
258 Check(Section.isRequiredForExecution(IsRequired));
259 Check(Section.isVirtual(IsVirtual));
260 Check(Section.isZeroInit(IsZeroInit));
261 Check(Section.isReadOnlyData(IsReadOnly));
262 Check(Section.getSize(DataSize));
263 Check(Section.getName(Name));
265 unsigned StubAlignment = getStubAlignment();
266 unsigned EndAlignment = (DataSize | Alignment) & -(DataSize | Alignment);
267 if (StubAlignment > EndAlignment)
268 StubBufSize += StubAlignment - EndAlignment;
272 unsigned SectionID = Sections.size();
274 const char *pData = 0;
276 // Some sections, such as debug info, don't need to be loaded for execution.
277 // Leave those where they are.
279 Allocate = DataSize + StubBufSize;
281 ? MemMgr->allocateCodeSection(Allocate, Alignment, SectionID, Name)
282 : MemMgr->allocateDataSection(Allocate, Alignment, SectionID, Name,
285 report_fatal_error("Unable to allocate section memory!");
287 // Virtual sections have no data in the object image, so leave pData = 0
291 // Zero-initialize or copy the data from the image
292 if (IsZeroInit || IsVirtual)
293 memset(Addr, 0, DataSize);
295 memcpy(Addr, pData, DataSize);
297 DEBUG(dbgs() << "emitSection SectionID: " << SectionID
299 << " obj addr: " << format("%p", pData)
300 << " new addr: " << format("%p", Addr)
301 << " DataSize: " << DataSize
302 << " StubBufSize: " << StubBufSize
303 << " Allocate: " << Allocate
305 Obj.updateSectionAddress(Section, (uint64_t)Addr);
308 // Even if we didn't load the section, we need to record an entry for it
309 // to handle later processing (and by 'handle' I mean don't do anything
310 // with these sections).
313 DEBUG(dbgs() << "emitSection SectionID: " << SectionID
315 << " obj addr: " << format("%p", data.data())
317 << " DataSize: " << DataSize
318 << " StubBufSize: " << StubBufSize
319 << " Allocate: " << Allocate
323 Sections.push_back(SectionEntry(Name, Addr, DataSize, (uintptr_t)pData));
327 unsigned RuntimeDyldImpl::findOrEmitSection(ObjectImage &Obj,
328 const SectionRef &Section,
330 ObjSectionToIDMap &LocalSections) {
332 unsigned SectionID = 0;
333 ObjSectionToIDMap::iterator i = LocalSections.find(Section);
334 if (i != LocalSections.end())
335 SectionID = i->second;
337 SectionID = emitSection(Obj, Section, IsCode);
338 LocalSections[Section] = SectionID;
343 void RuntimeDyldImpl::addRelocationForSection(const RelocationEntry &RE,
344 unsigned SectionID) {
345 Relocations[SectionID].push_back(RE);
348 void RuntimeDyldImpl::addRelocationForSymbol(const RelocationEntry &RE,
349 StringRef SymbolName) {
350 // Relocation by symbol. If the symbol is found in the global symbol table,
351 // create an appropriate section relocation. Otherwise, add it to
352 // ExternalSymbolRelocations.
353 SymbolTableMap::const_iterator Loc =
354 GlobalSymbolTable.find(SymbolName);
355 if (Loc == GlobalSymbolTable.end()) {
356 ExternalSymbolRelocations[SymbolName].push_back(RE);
358 // Copy the RE since we want to modify its addend.
359 RelocationEntry RECopy = RE;
360 RECopy.Addend += Loc->second.second;
361 Relocations[Loc->second.first].push_back(RECopy);
365 uint8_t *RuntimeDyldImpl::createStubFunction(uint8_t *Addr) {
366 if (Arch == Triple::aarch64) {
367 // This stub has to be able to access the full address space,
368 // since symbol lookup won't necessarily find a handy, in-range,
369 // PLT stub for functions which could be anywhere.
370 uint32_t *StubAddr = (uint32_t*)Addr;
372 // Stub can use ip0 (== x16) to calculate address
373 *StubAddr = 0xd2e00010; // movz ip0, #:abs_g3:<addr>
375 *StubAddr = 0xf2c00010; // movk ip0, #:abs_g2_nc:<addr>
377 *StubAddr = 0xf2a00010; // movk ip0, #:abs_g1_nc:<addr>
379 *StubAddr = 0xf2800010; // movk ip0, #:abs_g0_nc:<addr>
381 *StubAddr = 0xd61f0200; // br ip0
384 } else if (Arch == Triple::arm) {
385 // TODO: There is only ARM far stub now. We should add the Thumb stub,
386 // and stubs for branches Thumb - ARM and ARM - Thumb.
387 uint32_t *StubAddr = (uint32_t*)Addr;
388 *StubAddr = 0xe51ff004; // ldr pc,<label>
389 return (uint8_t*)++StubAddr;
390 } else if (Arch == Triple::mipsel || Arch == Triple::mips) {
391 uint32_t *StubAddr = (uint32_t*)Addr;
392 // 0: 3c190000 lui t9,%hi(addr).
393 // 4: 27390000 addiu t9,t9,%lo(addr).
394 // 8: 03200008 jr t9.
396 const unsigned LuiT9Instr = 0x3c190000, AdduiT9Instr = 0x27390000;
397 const unsigned JrT9Instr = 0x03200008, NopInstr = 0x0;
399 *StubAddr = LuiT9Instr;
401 *StubAddr = AdduiT9Instr;
403 *StubAddr = JrT9Instr;
405 *StubAddr = NopInstr;
407 } else if (Arch == Triple::ppc64 || Arch == Triple::ppc64le) {
408 // PowerPC64 stub: the address points to a function descriptor
409 // instead of the function itself. Load the function address
410 // on r11 and sets it to control register. Also loads the function
411 // TOC in r2 and environment pointer to r11.
412 writeInt32BE(Addr, 0x3D800000); // lis r12, highest(addr)
413 writeInt32BE(Addr+4, 0x618C0000); // ori r12, higher(addr)
414 writeInt32BE(Addr+8, 0x798C07C6); // sldi r12, r12, 32
415 writeInt32BE(Addr+12, 0x658C0000); // oris r12, r12, h(addr)
416 writeInt32BE(Addr+16, 0x618C0000); // ori r12, r12, l(addr)
417 writeInt32BE(Addr+20, 0xF8410028); // std r2, 40(r1)
418 writeInt32BE(Addr+24, 0xE96C0000); // ld r11, 0(r12)
419 writeInt32BE(Addr+28, 0xE84C0008); // ld r2, 0(r12)
420 writeInt32BE(Addr+32, 0x7D6903A6); // mtctr r11
421 writeInt32BE(Addr+36, 0xE96C0010); // ld r11, 16(r2)
422 writeInt32BE(Addr+40, 0x4E800420); // bctr
425 } else if (Arch == Triple::systemz) {
426 writeInt16BE(Addr, 0xC418); // lgrl %r1,.+8
427 writeInt16BE(Addr+2, 0x0000);
428 writeInt16BE(Addr+4, 0x0004);
429 writeInt16BE(Addr+6, 0x07F1); // brc 15,%r1
430 // 8-byte address stored at Addr + 8
432 } else if (Arch == Triple::x86_64) {
434 *(Addr+1) = 0x25; // rip
435 // 32-bit PC-relative address of the GOT entry will be stored at Addr+2
440 // Assign an address to a symbol name and resolve all the relocations
441 // associated with it.
442 void RuntimeDyldImpl::reassignSectionAddress(unsigned SectionID,
444 // The address to use for relocation resolution is not
445 // the address of the local section buffer. We must be doing
446 // a remote execution environment of some sort. Relocations can't
447 // be applied until all the sections have been moved. The client must
448 // trigger this with a call to MCJIT::finalize() or
449 // RuntimeDyld::resolveRelocations().
451 // Addr is a uint64_t because we can't assume the pointer width
452 // of the target is the same as that of the host. Just use a generic
453 // "big enough" type.
454 Sections[SectionID].LoadAddress = Addr;
457 void RuntimeDyldImpl::resolveRelocationList(const RelocationList &Relocs,
459 for (unsigned i = 0, e = Relocs.size(); i != e; ++i) {
460 const RelocationEntry &RE = Relocs[i];
461 // Ignore relocations for sections that were not loaded
462 if (Sections[RE.SectionID].Address == 0)
464 resolveRelocation(RE, Value);
468 void RuntimeDyldImpl::resolveExternalSymbols() {
469 while(!ExternalSymbolRelocations.empty()) {
470 StringMap<RelocationList>::iterator i = ExternalSymbolRelocations.begin();
472 StringRef Name = i->first();
473 RelocationList &Relocs = i->second;
474 if (Name.size() == 0) {
475 // This is an absolute symbol, use an address of zero.
476 DEBUG(dbgs() << "Resolving absolute relocations." << "\n");
477 resolveRelocationList(Relocs, 0);
480 SymbolTableMap::const_iterator Loc = GlobalSymbolTable.find(Name);
481 if (Loc == GlobalSymbolTable.end()) {
482 // This is an external symbol, try to get its address from
484 Addr = MemMgr->getSymbolAddress(Name.data());
486 // We found the symbol in our global table. It was probably in a
487 // Module that we loaded previously.
488 SymbolLoc SymLoc = GlobalSymbolTable.lookup(Name);
489 Addr = getSectionLoadAddress(SymLoc.first) + SymLoc.second;
492 // FIXME: Implement error handling that doesn't kill the host program!
494 report_fatal_error("Program used external function '" + Name +
495 "' which could not be resolved!");
497 updateGOTEntries(Name, Addr);
498 DEBUG(dbgs() << "Resolving relocations Name: " << Name
499 << "\t" << format("0x%lx", Addr)
501 resolveRelocationList(Relocs, Addr);
504 ExternalSymbolRelocations.erase(i->first());
509 //===----------------------------------------------------------------------===//
510 // RuntimeDyld class implementation
511 RuntimeDyld::RuntimeDyld(RTDyldMemoryManager *mm) {
512 // FIXME: There's a potential issue lurking here if a single instance of
513 // RuntimeDyld is used to load multiple objects. The current implementation
514 // associates a single memory manager with a RuntimeDyld instance. Even
515 // though the public class spawns a new 'impl' instance for each load,
516 // they share a single memory manager. This can become a problem when page
517 // permissions are applied.
522 RuntimeDyld::~RuntimeDyld() {
526 ObjectImage *RuntimeDyld::loadObject(ObjectBuffer *InputBuffer) {
528 sys::fs::file_magic Type =
529 sys::fs::identify_magic(InputBuffer->getBuffer());
531 case sys::fs::file_magic::elf_relocatable:
532 case sys::fs::file_magic::elf_executable:
533 case sys::fs::file_magic::elf_shared_object:
534 case sys::fs::file_magic::elf_core:
535 Dyld = new RuntimeDyldELF(MM);
537 case sys::fs::file_magic::macho_object:
538 case sys::fs::file_magic::macho_executable:
539 case sys::fs::file_magic::macho_fixed_virtual_memory_shared_lib:
540 case sys::fs::file_magic::macho_core:
541 case sys::fs::file_magic::macho_preload_executable:
542 case sys::fs::file_magic::macho_dynamically_linked_shared_lib:
543 case sys::fs::file_magic::macho_dynamic_linker:
544 case sys::fs::file_magic::macho_bundle:
545 case sys::fs::file_magic::macho_dynamically_linked_shared_lib_stub:
546 case sys::fs::file_magic::macho_dsym_companion:
547 Dyld = new RuntimeDyldMachO(MM);
549 case sys::fs::file_magic::unknown:
550 case sys::fs::file_magic::bitcode:
551 case sys::fs::file_magic::archive:
552 case sys::fs::file_magic::coff_object:
553 case sys::fs::file_magic::pecoff_executable:
554 case sys::fs::file_magic::macho_universal_binary:
555 report_fatal_error("Incompatible object format!");
558 if (!Dyld->isCompatibleFormat(InputBuffer))
559 report_fatal_error("Incompatible object format!");
562 return Dyld->loadObject(InputBuffer);
565 void *RuntimeDyld::getSymbolAddress(StringRef Name) {
568 return Dyld->getSymbolAddress(Name);
571 uint64_t RuntimeDyld::getSymbolLoadAddress(StringRef Name) {
574 return Dyld->getSymbolLoadAddress(Name);
577 void RuntimeDyld::resolveRelocations() {
578 Dyld->resolveRelocations();
581 void RuntimeDyld::reassignSectionAddress(unsigned SectionID,
583 Dyld->reassignSectionAddress(SectionID, Addr);
586 void RuntimeDyld::mapSectionAddress(const void *LocalAddress,
587 uint64_t TargetAddress) {
588 Dyld->mapSectionAddress(LocalAddress, TargetAddress);
591 StringRef RuntimeDyld::getErrorString() {
592 return Dyld->getErrorString();
595 StringRef RuntimeDyld::getEHFrameSection() {
596 return Dyld->getEHFrameSection();
599 } // end namespace llvm