MCJIT support for non-function sections.
[oota-llvm.git] / lib / ExecutionEngine / RuntimeDyld / RuntimeDyld.cpp
1 //===-- RuntimeDyld.cpp - Run-time dynamic linker for MC-JIT ------*- C++ -*-===//
2 //
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
14 #define DEBUG_TYPE "dyld"
15 #include "RuntimeDyldImpl.h"
16 #include "llvm/Support/Path.h"
17 using namespace llvm;
18 using namespace llvm::object;
19
20 // Empty out-of-line virtual destructor as the key function.
21 RTDyldMemoryManager::~RTDyldMemoryManager() {}
22 RuntimeDyldImpl::~RuntimeDyldImpl() {}
23
24 namespace llvm {
25
26 void RuntimeDyldImpl::extractFunction(StringRef Name, uint8_t *StartAddress,
27                                       uint8_t *EndAddress) {
28   // FIXME: DEPRECATED in favor of by-section allocation.
29   // Allocate memory for the function via the memory manager.
30   uintptr_t Size = EndAddress - StartAddress + 1;
31   uintptr_t AllocSize = Size;
32   uint8_t *Mem = MemMgr->startFunctionBody(Name.data(), AllocSize);
33   assert(Size >= (uint64_t)(EndAddress - StartAddress + 1) &&
34          "Memory manager failed to allocate enough memory!");
35   // Copy the function payload into the memory block.
36   memcpy(Mem, StartAddress, Size);
37   MemMgr->endFunctionBody(Name.data(), Mem, Mem + Size);
38   // Remember where we put it.
39   unsigned SectionID = Sections.size();
40   Sections.push_back(sys::MemoryBlock(Mem, Size));
41
42   // Default the assigned address for this symbol to wherever this
43   // allocated it.
44   SymbolTable[Name] = SymbolLoc(SectionID, 0); 
45   DEBUG(dbgs() << "    allocated to [" << Mem << ", " << Mem + Size << "]\n");
46 }
47
48 // Resolve the relocations for all symbols we currently know about.
49 void RuntimeDyldImpl::resolveRelocations() {
50   // Just iterate over the sections we have and resolve all the relocations
51   // in them. Gross overkill, but it gets the job done.
52   for (int i = 0, e = Sections.size(); i != e; ++i) {
53     reassignSectionAddress(i, SectionLoadAddress[i]);
54   }
55 }
56
57 //===----------------------------------------------------------------------===//
58 // RuntimeDyld class implementation
59 RuntimeDyld::RuntimeDyld(RTDyldMemoryManager *mm) {
60   Dyld = 0;
61   MM = mm;
62 }
63
64 RuntimeDyld::~RuntimeDyld() {
65   delete Dyld;
66 }
67
68 bool RuntimeDyld::loadObject(MemoryBuffer *InputBuffer) {
69   if (!Dyld) {
70     sys::LLVMFileType type = sys::IdentifyFileType(
71             InputBuffer->getBufferStart(),
72             static_cast<unsigned>(InputBuffer->getBufferSize()));
73     switch (type) {
74       case sys::ELF_Relocatable_FileType:
75       case sys::ELF_Executable_FileType:
76       case sys::ELF_SharedObject_FileType:
77       case sys::ELF_Core_FileType:
78         Dyld = new RuntimeDyldELF(MM);
79         break;
80       case sys::Mach_O_Object_FileType:
81       case sys::Mach_O_Executable_FileType:
82       case sys::Mach_O_FixedVirtualMemorySharedLib_FileType:
83       case sys::Mach_O_Core_FileType:
84       case sys::Mach_O_PreloadExecutable_FileType:
85       case sys::Mach_O_DynamicallyLinkedSharedLib_FileType:
86       case sys::Mach_O_DynamicLinker_FileType:
87       case sys::Mach_O_Bundle_FileType:
88       case sys::Mach_O_DynamicallyLinkedSharedLibStub_FileType:
89       case sys::Mach_O_DSYMCompanion_FileType:
90         Dyld = new RuntimeDyldMachO(MM);
91         break;
92       case sys::Unknown_FileType:
93       case sys::Bitcode_FileType:
94       case sys::Archive_FileType:
95       case sys::COFF_FileType:
96         report_fatal_error("Incompatible object format!");
97     }
98   } else {
99     if (!Dyld->isCompatibleFormat(InputBuffer))
100       report_fatal_error("Incompatible object format!");
101   }
102
103   return Dyld->loadObject(InputBuffer);
104 }
105
106 void *RuntimeDyld::getSymbolAddress(StringRef Name) {
107   return Dyld->getSymbolAddress(Name);
108 }
109
110 void RuntimeDyld::resolveRelocations() {
111   Dyld->resolveRelocations();
112 }
113
114 void RuntimeDyld::reassignSectionAddress(unsigned SectionID,
115                                          uint64_t Addr) {
116   Dyld->reassignSectionAddress(SectionID, Addr);
117 }
118
119 StringRef RuntimeDyld::getErrorString() {
120   return Dyld->getErrorString();
121 }
122
123 } // end namespace llvm