blob: 18827978d9a33948c0f5d70ec07cd6645fc2ff53 [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 Grosbach61425c02012-01-16 22:26:39 +000028 // FIXME: DEPRECATED in favor of by-section allocation.
Jim Grosbachc41ab782011-04-06 01:11:05 +000029 // Allocate memory for the function via the memory manager.
30 uintptr_t Size = EndAddress - StartAddress + 1;
Jim Grosbachffa62502011-05-13 20:12:14 +000031 uintptr_t AllocSize = Size;
32 uint8_t *Mem = MemMgr->startFunctionBody(Name.data(), AllocSize);
Jim Grosbachc41ab782011-04-06 01:11:05 +000033 assert(Size >= (uint64_t)(EndAddress - StartAddress + 1) &&
34 "Memory manager failed to allocate enough memory!");
35 // Copy the function payload into the memory block.
Jim Grosbachffa62502011-05-13 20:12:14 +000036 memcpy(Mem, StartAddress, Size);
Jim Grosbachc41ab782011-04-06 01:11:05 +000037 MemMgr->endFunctionBody(Name.data(), Mem, Mem + Size);
38 // Remember where we put it.
Jim Grosbach61425c02012-01-16 22:26:39 +000039 unsigned SectionID = Sections.size();
40 Sections.push_back(sys::MemoryBlock(Mem, Size));
41
Jim Grosbachf8c1c842011-04-12 21:20:41 +000042 // Default the assigned address for this symbol to wherever this
43 // allocated it.
Jim Grosbach61425c02012-01-16 22:26:39 +000044 SymbolTable[Name] = SymbolLoc(SectionID, 0);
Jim Grosbachffa62502011-05-13 20:12:14 +000045 DEBUG(dbgs() << " allocated to [" << Mem << ", " << Mem + Size << "]\n");
Jim Grosbachc41ab782011-04-06 01:11:05 +000046}
47
Jim Grosbachf8c1c842011-04-12 21:20:41 +000048// Resolve the relocations for all symbols we currently know about.
49void RuntimeDyldImpl::resolveRelocations() {
Jim Grosbach61425c02012-01-16 22:26:39 +000050 // 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 }
Jim Grosbachf8c1c842011-04-12 21:20:41 +000055}
56
Jim Grosbach6e563312011-03-21 22:15:52 +000057//===----------------------------------------------------------------------===//
58// RuntimeDyld class implementation
Danil Malyshevcf852dc2011-07-13 07:57:58 +000059RuntimeDyld::RuntimeDyld(RTDyldMemoryManager *mm) {
60 Dyld = 0;
61 MM = mm;
Jim Grosbach6e563312011-03-21 22:15:52 +000062}
63
64RuntimeDyld::~RuntimeDyld() {
65 delete Dyld;
66}
67
68bool RuntimeDyld::loadObject(MemoryBuffer *InputBuffer) {
Danil Malyshevcf852dc2011-07-13 07:57:58 +000069 if (!Dyld) {
Eli Benderskya66a1852012-01-16 08:56:09 +000070 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 }
Danil Malyshevcf852dc2011-07-13 07:57:58 +000098 } else {
Eli Benderskya66a1852012-01-16 08:56:09 +000099 if (!Dyld->isCompatibleFormat(InputBuffer))
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000100 report_fatal_error("Incompatible object format!");
101 }
102
Jim Grosbach6e563312011-03-21 22:15:52 +0000103 return Dyld->loadObject(InputBuffer);
104}
105
Jim Grosbachb0271052011-04-08 17:31:24 +0000106void *RuntimeDyld::getSymbolAddress(StringRef Name) {
Jim Grosbach6e563312011-03-21 22:15:52 +0000107 return Dyld->getSymbolAddress(Name);
108}
109
Jim Grosbachf8c1c842011-04-12 21:20:41 +0000110void RuntimeDyld::resolveRelocations() {
111 Dyld->resolveRelocations();
112}
113
Jim Grosbach61425c02012-01-16 22:26:39 +0000114void RuntimeDyld::reassignSectionAddress(unsigned SectionID,
115 uint64_t Addr) {
116 Dyld->reassignSectionAddress(SectionID, Addr);
Jim Grosbachf8c1c842011-04-12 21:20:41 +0000117}
118
Jim Grosbach91dde152011-03-22 18:22:27 +0000119StringRef RuntimeDyld::getErrorString() {
Jim Grosbachb3eecaf2011-03-22 18:19:42 +0000120 return Dyld->getErrorString();
121}
122
Jim Grosbach6e563312011-03-21 22:15:52 +0000123} // end namespace llvm