blob: b017ebb2dcbe8bc843802b274840c2fbb6fb8a01 [file] [log] [blame]
Danil Malyshevcf852dc2011-07-13 07:57:58 +00001//===-- RuntimeDyld.cpp - Run-time dynamic linker for MC-JIT ------*- C++ -*-===//
Jim Grosbach6e563312011-03-21 22:15:52 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// Implementation of the MC-JIT runtime dynamic linker.
11//
12//===----------------------------------------------------------------------===//
13
Jim Grosbach8b54dca2011-03-23 19:52:00 +000014#define DEBUG_TYPE "dyld"
Danil Malyshevcf852dc2011-07-13 07:57:58 +000015#include "RuntimeDyldImpl.h"
Eli Benderskya66a1852012-01-16 08:56:09 +000016#include "llvm/Support/Path.h"
Jim Grosbach6e563312011-03-21 22:15:52 +000017using namespace llvm;
18using namespace llvm::object;
19
Chandler Carruth53c5e7b2011-04-05 23:54:31 +000020// Empty out-of-line virtual destructor as the key function.
21RTDyldMemoryManager::~RTDyldMemoryManager() {}
Danil Malyshevcf852dc2011-07-13 07:57:58 +000022RuntimeDyldImpl::~RuntimeDyldImpl() {}
Chandler Carruth53c5e7b2011-04-05 23:54:31 +000023
Jim Grosbach6e563312011-03-21 22:15:52 +000024namespace llvm {
Jim Grosbach6e563312011-03-21 22:15:52 +000025
Jim Grosbachc41ab782011-04-06 01:11:05 +000026void RuntimeDyldImpl::extractFunction(StringRef Name, uint8_t *StartAddress,
Jim Grosbach01ccab42011-04-06 22:13:52 +000027 uint8_t *EndAddress) {
Jim Grosbachc41ab782011-04-06 01:11:05 +000028 // Allocate memory for the function via the memory manager.
29 uintptr_t Size = EndAddress - StartAddress + 1;
Jim Grosbachffa62502011-05-13 20:12:14 +000030 uintptr_t AllocSize = Size;
31 uint8_t *Mem = MemMgr->startFunctionBody(Name.data(), AllocSize);
Jim Grosbachc41ab782011-04-06 01:11:05 +000032 assert(Size >= (uint64_t)(EndAddress - StartAddress + 1) &&
33 "Memory manager failed to allocate enough memory!");
34 // Copy the function payload into the memory block.
Jim Grosbachffa62502011-05-13 20:12:14 +000035 memcpy(Mem, StartAddress, Size);
Jim Grosbachc41ab782011-04-06 01:11:05 +000036 MemMgr->endFunctionBody(Name.data(), Mem, Mem + Size);
37 // Remember where we put it.
38 Functions[Name] = sys::MemoryBlock(Mem, Size);
Jim Grosbachf8c1c842011-04-12 21:20:41 +000039 // Default the assigned address for this symbol to wherever this
40 // allocated it.
41 SymbolTable[Name] = Mem;
Jim Grosbachffa62502011-05-13 20:12:14 +000042 DEBUG(dbgs() << " allocated to [" << Mem << ", " << Mem + Size << "]\n");
Jim Grosbachc41ab782011-04-06 01:11:05 +000043}
44
Jim Grosbachf8c1c842011-04-12 21:20:41 +000045// Resolve the relocations for all symbols we currently know about.
46void RuntimeDyldImpl::resolveRelocations() {
47 // Just iterate over the symbols in our symbol table and assign their
48 // addresses.
49 StringMap<uint8_t*>::iterator i = SymbolTable.begin();
50 StringMap<uint8_t*>::iterator e = SymbolTable.end();
51 for (;i != e; ++i)
52 reassignSymbolAddress(i->getKey(), i->getValue());
53}
54
Jim Grosbach6e563312011-03-21 22:15:52 +000055//===----------------------------------------------------------------------===//
56// RuntimeDyld class implementation
Danil Malyshevcf852dc2011-07-13 07:57:58 +000057RuntimeDyld::RuntimeDyld(RTDyldMemoryManager *mm) {
58 Dyld = 0;
59 MM = mm;
Jim Grosbach6e563312011-03-21 22:15:52 +000060}
61
62RuntimeDyld::~RuntimeDyld() {
63 delete Dyld;
64}
65
66bool RuntimeDyld::loadObject(MemoryBuffer *InputBuffer) {
Danil Malyshevcf852dc2011-07-13 07:57:58 +000067 if (!Dyld) {
Eli Benderskya66a1852012-01-16 08:56:09 +000068 sys::LLVMFileType type = sys::IdentifyFileType(
69 InputBuffer->getBufferStart(),
70 static_cast<unsigned>(InputBuffer->getBufferSize()));
71 switch (type) {
72 case sys::ELF_Relocatable_FileType:
73 case sys::ELF_Executable_FileType:
74 case sys::ELF_SharedObject_FileType:
75 case sys::ELF_Core_FileType:
76 Dyld = new RuntimeDyldELF(MM);
77 break;
78 case sys::Mach_O_Object_FileType:
79 case sys::Mach_O_Executable_FileType:
80 case sys::Mach_O_FixedVirtualMemorySharedLib_FileType:
81 case sys::Mach_O_Core_FileType:
82 case sys::Mach_O_PreloadExecutable_FileType:
83 case sys::Mach_O_DynamicallyLinkedSharedLib_FileType:
84 case sys::Mach_O_DynamicLinker_FileType:
85 case sys::Mach_O_Bundle_FileType:
86 case sys::Mach_O_DynamicallyLinkedSharedLibStub_FileType:
87 case sys::Mach_O_DSYMCompanion_FileType:
88 Dyld = new RuntimeDyldMachO(MM);
89 break;
90 case sys::Unknown_FileType:
91 case sys::Bitcode_FileType:
92 case sys::Archive_FileType:
93 case sys::COFF_FileType:
94 report_fatal_error("Incompatible object format!");
95 }
Danil Malyshevcf852dc2011-07-13 07:57:58 +000096 } else {
Eli Benderskya66a1852012-01-16 08:56:09 +000097 if (!Dyld->isCompatibleFormat(InputBuffer))
Danil Malyshevcf852dc2011-07-13 07:57:58 +000098 report_fatal_error("Incompatible object format!");
99 }
100
Jim Grosbach6e563312011-03-21 22:15:52 +0000101 return Dyld->loadObject(InputBuffer);
102}
103
Jim Grosbachb0271052011-04-08 17:31:24 +0000104void *RuntimeDyld::getSymbolAddress(StringRef Name) {
Jim Grosbach6e563312011-03-21 22:15:52 +0000105 return Dyld->getSymbolAddress(Name);
106}
107
Jim Grosbachf8c1c842011-04-12 21:20:41 +0000108void RuntimeDyld::resolveRelocations() {
109 Dyld->resolveRelocations();
110}
111
112void RuntimeDyld::reassignSymbolAddress(StringRef Name, uint8_t *Addr) {
113 Dyld->reassignSymbolAddress(Name, Addr);
114}
115
Jim Grosbach91dde152011-03-22 18:22:27 +0000116StringRef RuntimeDyld::getErrorString() {
Jim Grosbachb3eecaf2011-03-22 18:19:42 +0000117 return Dyld->getErrorString();
118}
119
Jim Grosbach6e563312011-03-21 22:15:52 +0000120} // end namespace llvm