blob: b4640404f602ac3fa665a54a945e35b3fac481d8 [file] [log] [blame]
Jim Grosbache0934be2012-01-16 23:50: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 Bendersky76463fd2012-01-22 07:05:02 +000016#include "RuntimeDyldELF.h"
17#include "RuntimeDyldMachO.h"
Eli Benderskya66a1852012-01-16 08:56:09 +000018#include "llvm/Support/Path.h"
Eli Bendersky76463fd2012-01-22 07:05:02 +000019
Jim Grosbach6e563312011-03-21 22:15:52 +000020using namespace llvm;
21using namespace llvm::object;
22
Chandler Carruth53c5e7b2011-04-05 23:54:31 +000023// Empty out-of-line virtual destructor as the key function.
24RTDyldMemoryManager::~RTDyldMemoryManager() {}
Danil Malyshevcf852dc2011-07-13 07:57:58 +000025RuntimeDyldImpl::~RuntimeDyldImpl() {}
Chandler Carruth53c5e7b2011-04-05 23:54:31 +000026
Jim Grosbach6e563312011-03-21 22:15:52 +000027namespace llvm {
Jim Grosbach6e563312011-03-21 22:15:52 +000028
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +000029namespace {
30 // Helper for extensive error checking in debug builds.
31 error_code Check(error_code Err) {
32 if (Err) {
33 report_fatal_error(Err.message());
34 }
35 return Err;
36 }
37} // end anonymous namespace
Jim Grosbach61425c02012-01-16 22:26:39 +000038
Jim Grosbachf8c1c842011-04-12 21:20:41 +000039// Resolve the relocations for all symbols we currently know about.
40void RuntimeDyldImpl::resolveRelocations() {
Preston Gurdc68dda82012-04-12 20:13:57 +000041 // First, resolve relocations associated with external symbols.
Eli Bendersky37bc5a22012-04-30 12:15:58 +000042 resolveExternalSymbols();
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +000043
Jim Grosbach61425c02012-01-16 22:26:39 +000044 // Just iterate over the sections we have and resolve all the relocations
45 // in them. Gross overkill, but it gets the job done.
46 for (int i = 0, e = Sections.size(); i != e; ++i) {
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +000047 reassignSectionAddress(i, Sections[i].LoadAddress);
Jim Grosbach61425c02012-01-16 22:26:39 +000048 }
Jim Grosbachf8c1c842011-04-12 21:20:41 +000049}
50
Jim Grosbach020f4e82012-01-16 23:50:55 +000051void RuntimeDyldImpl::mapSectionAddress(void *LocalAddress,
52 uint64_t TargetAddress) {
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +000053 for (unsigned i = 0, e = Sections.size(); i != e; ++i) {
54 if (Sections[i].Address == LocalAddress) {
55 reassignSectionAddress(i, TargetAddress);
56 return;
57 }
58 }
59 llvm_unreachable("Attempting to remap address of unknown section!");
Jim Grosbach020f4e82012-01-16 23:50:55 +000060}
61
Eli Bendersky5fe01982012-04-29 12:40:47 +000062// Subclasses can implement this method to create specialized image instances.
Sylvestre Ledruc8e41c52012-07-23 08:51:15 +000063// The caller owns the pointer that is returned.
Preston Gurd689ff9c2012-04-16 22:12:58 +000064ObjectImage *RuntimeDyldImpl::createObjectImage(const MemoryBuffer *InputBuffer) {
65 ObjectFile *ObjFile = ObjectFile::createObjectFile(const_cast<MemoryBuffer*>
66 (InputBuffer));
67 ObjectImage *Obj = new ObjectImage(ObjFile);
68 return Obj;
69}
70
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +000071bool RuntimeDyldImpl::loadObject(const MemoryBuffer *InputBuffer) {
Preston Gurd689ff9c2012-04-16 22:12:58 +000072 OwningPtr<ObjectImage> obj(createObjectImage(InputBuffer));
Preston Gurdc68dda82012-04-12 20:13:57 +000073 if (!obj)
74 report_fatal_error("Unable to create object image from memory buffer!");
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +000075
76 Arch = (Triple::ArchType)obj->getArch();
77
Eli Benderskyd98c9e92012-05-01 06:58:59 +000078 // Symbols found in this object
79 StringMap<SymbolLoc> LocalSymbols;
80 // Used sections from the object file
81 ObjSectionToIDMap LocalSections;
82
83 // Common symbols requiring allocation, and the total size required to
84 // allocate all common symbols.
85 CommonSymbolMap CommonSymbols;
86 uint64_t CommonSize = 0;
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +000087
88 error_code err;
89 // Parse symbols
90 DEBUG(dbgs() << "Parse symbols:\n");
91 for (symbol_iterator i = obj->begin_symbols(), e = obj->end_symbols();
92 i != e; i.increment(err)) {
93 Check(err);
94 object::SymbolRef::Type SymType;
95 StringRef Name;
96 Check(i->getType(SymType));
97 Check(i->getName(Name));
98
Preston Gurdc68dda82012-04-12 20:13:57 +000099 uint32_t flags;
100 Check(i->getFlags(flags));
101
102 bool isCommon = flags & SymbolRef::SF_Common;
103 if (isCommon) {
104 // Add the common symbols to a list. We'll allocate them all below.
105 uint64_t Size = 0;
106 Check(i->getSize(Size));
107 CommonSize += Size;
108 CommonSymbols[*i] = Size;
109 } else {
110 if (SymType == object::SymbolRef::ST_Function ||
111 SymType == object::SymbolRef::ST_Data) {
112 uint64_t FileOffset;
Eli Bendersky5fe01982012-04-29 12:40:47 +0000113 StringRef SectionData;
Preston Gurdc68dda82012-04-12 20:13:57 +0000114 section_iterator si = obj->end_sections();
115 Check(i->getFileOffset(FileOffset));
116 Check(i->getSection(si));
117 if (si == obj->end_sections()) continue;
Eli Bendersky5fe01982012-04-29 12:40:47 +0000118 Check(si->getContents(SectionData));
Preston Gurdc68dda82012-04-12 20:13:57 +0000119 const uint8_t* SymPtr = (const uint8_t*)InputBuffer->getBufferStart() +
120 (uintptr_t)FileOffset;
Eli Bendersky5fe01982012-04-29 12:40:47 +0000121 uintptr_t SectOffset = (uintptr_t)(SymPtr -
122 (const uint8_t*)SectionData.begin());
Preston Gurdc68dda82012-04-12 20:13:57 +0000123 unsigned SectionID =
Preston Gurd689ff9c2012-04-16 22:12:58 +0000124 findOrEmitSection(*obj,
125 *si,
Preston Gurdc68dda82012-04-12 20:13:57 +0000126 SymType == object::SymbolRef::ST_Function,
127 LocalSections);
Preston Gurdc68dda82012-04-12 20:13:57 +0000128 LocalSymbols[Name.data()] = SymbolLoc(SectionID, SectOffset);
129 DEBUG(dbgs() << "\tFileOffset: " << format("%p", (uintptr_t)FileOffset)
130 << " flags: " << flags
131 << " SID: " << SectionID
132 << " Offset: " << format("%p", SectOffset));
Eli Bendersky5fe01982012-04-29 12:40:47 +0000133 bool isGlobal = flags & SymbolRef::SF_Global;
Preston Gurdc68dda82012-04-12 20:13:57 +0000134 if (isGlobal)
Eli Benderskyd98c9e92012-05-01 06:58:59 +0000135 GlobalSymbolTable[Name] = SymbolLoc(SectionID, SectOffset);
Preston Gurdc68dda82012-04-12 20:13:57 +0000136 }
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000137 }
138 DEBUG(dbgs() << "\tType: " << SymType << " Name: " << Name << "\n");
139 }
140
Preston Gurdc68dda82012-04-12 20:13:57 +0000141 // Allocate common symbols
142 if (CommonSize != 0)
Preston Gurd689ff9c2012-04-16 22:12:58 +0000143 emitCommonSymbols(*obj, CommonSymbols, CommonSize, LocalSymbols);
Preston Gurdc68dda82012-04-12 20:13:57 +0000144
Eli Bendersky5fe01982012-04-29 12:40:47 +0000145 // Parse and process relocations
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000146 DEBUG(dbgs() << "Parse relocations:\n");
147 for (section_iterator si = obj->begin_sections(),
148 se = obj->end_sections(); si != se; si.increment(err)) {
149 Check(err);
150 bool isFirstRelocation = true;
151 unsigned SectionID = 0;
152 StubMap Stubs;
153
154 for (relocation_iterator i = si->begin_relocations(),
155 e = si->end_relocations(); i != e; i.increment(err)) {
156 Check(err);
157
Eli Bendersky5fe01982012-04-29 12:40:47 +0000158 // If it's the first relocation in this section, find its SectionID
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000159 if (isFirstRelocation) {
Preston Gurd689ff9c2012-04-16 22:12:58 +0000160 SectionID = findOrEmitSection(*obj, *si, true, LocalSections);
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000161 DEBUG(dbgs() << "\tSectionID: " << SectionID << "\n");
162 isFirstRelocation = false;
163 }
164
165 ObjRelocationInfo RI;
166 RI.SectionID = SectionID;
167 Check(i->getAdditionalInfo(RI.AdditionalInfo));
168 Check(i->getOffset(RI.Offset));
169 Check(i->getSymbol(RI.Symbol));
170 Check(i->getType(RI.Type));
171
172 DEBUG(dbgs() << "\t\tAddend: " << RI.AdditionalInfo
173 << " Offset: " << format("%p", (uintptr_t)RI.Offset)
174 << " Type: " << (uint32_t)(RI.Type & 0xffffffffL)
175 << "\n");
176 processRelocationRef(RI, *obj, LocalSections, LocalSymbols, Stubs);
177 }
178 }
Preston Gurd689ff9c2012-04-16 22:12:58 +0000179
180 handleObjectLoaded(obj.take());
181
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000182 return false;
183}
184
Eli Benderskyc201e6e2012-05-01 10:41:12 +0000185void RuntimeDyldImpl::emitCommonSymbols(ObjectImage &Obj,
186 const CommonSymbolMap &CommonSymbols,
187 uint64_t TotalSize,
188 SymbolTableMap &SymbolTable) {
Preston Gurdc68dda82012-04-12 20:13:57 +0000189 // Allocate memory for the section
190 unsigned SectionID = Sections.size();
191 uint8_t *Addr = MemMgr->allocateDataSection(TotalSize, sizeof(void*),
192 SectionID);
193 if (!Addr)
194 report_fatal_error("Unable to allocate memory for common symbols!");
195 uint64_t Offset = 0;
196 Sections.push_back(SectionEntry(Addr, TotalSize, TotalSize, 0));
197 memset(Addr, 0, TotalSize);
198
199 DEBUG(dbgs() << "emitCommonSection SectionID: " << SectionID
200 << " new addr: " << format("%p", Addr)
201 << " DataSize: " << TotalSize
202 << "\n");
203
204 // Assign the address of each symbol
Eli Benderskyc201e6e2012-05-01 10:41:12 +0000205 for (CommonSymbolMap::const_iterator it = CommonSymbols.begin(),
206 itEnd = CommonSymbols.end(); it != itEnd; it++) {
Preston Gurdc68dda82012-04-12 20:13:57 +0000207 StringRef Name;
208 it->first.getName(Name);
Preston Gurd689ff9c2012-04-16 22:12:58 +0000209 Obj.updateSymbolAddress(it->first, (uint64_t)Addr);
Eli Benderskyc201e6e2012-05-01 10:41:12 +0000210 SymbolTable[Name.data()] = SymbolLoc(SectionID, Offset);
211 uint64_t Size = it->second;
Preston Gurdc68dda82012-04-12 20:13:57 +0000212 Offset += Size;
213 Addr += Size;
214 }
Preston Gurdc68dda82012-04-12 20:13:57 +0000215}
216
Preston Gurd689ff9c2012-04-16 22:12:58 +0000217unsigned RuntimeDyldImpl::emitSection(ObjectImage &Obj,
218 const SectionRef &Section,
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000219 bool IsCode) {
220
221 unsigned StubBufSize = 0,
222 StubSize = getMaxStubSize();
223 error_code err;
224 if (StubSize > 0) {
225 for (relocation_iterator i = Section.begin_relocations(),
Preston Gurdc68dda82012-04-12 20:13:57 +0000226 e = Section.end_relocations(); i != e; i.increment(err), Check(err))
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000227 StubBufSize += StubSize;
228 }
229 StringRef data;
230 uint64_t Alignment64;
231 Check(Section.getContents(data));
232 Check(Section.getAlignment(Alignment64));
233
234 unsigned Alignment = (unsigned)Alignment64 & 0xffffffffL;
Preston Gurdc68dda82012-04-12 20:13:57 +0000235 bool IsRequired;
236 bool IsVirtual;
237 bool IsZeroInit;
238 uint64_t DataSize;
239 Check(Section.isRequiredForExecution(IsRequired));
240 Check(Section.isVirtual(IsVirtual));
241 Check(Section.isZeroInit(IsZeroInit));
242 Check(Section.getSize(DataSize));
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000243
Preston Gurdc68dda82012-04-12 20:13:57 +0000244 unsigned Allocate;
245 unsigned SectionID = Sections.size();
246 uint8_t *Addr;
247 const char *pData = 0;
248
249 // Some sections, such as debug info, don't need to be loaded for execution.
250 // Leave those where they are.
251 if (IsRequired) {
252 Allocate = DataSize + StubBufSize;
253 Addr = IsCode
254 ? MemMgr->allocateCodeSection(Allocate, Alignment, SectionID)
255 : MemMgr->allocateDataSection(Allocate, Alignment, SectionID);
256 if (!Addr)
257 report_fatal_error("Unable to allocate section memory!");
258
259 // Virtual sections have no data in the object image, so leave pData = 0
260 if (!IsVirtual)
261 pData = data.data();
262
263 // Zero-initialize or copy the data from the image
264 if (IsZeroInit || IsVirtual)
265 memset(Addr, 0, DataSize);
266 else
267 memcpy(Addr, pData, DataSize);
268
269 DEBUG(dbgs() << "emitSection SectionID: " << SectionID
270 << " obj addr: " << format("%p", pData)
271 << " new addr: " << format("%p", Addr)
272 << " DataSize: " << DataSize
273 << " StubBufSize: " << StubBufSize
274 << " Allocate: " << Allocate
275 << "\n");
Preston Gurd689ff9c2012-04-16 22:12:58 +0000276 Obj.updateSectionAddress(Section, (uint64_t)Addr);
Preston Gurdc68dda82012-04-12 20:13:57 +0000277 }
278 else {
279 // Even if we didn't load the section, we need to record an entry for it
Eli Bendersky5fe01982012-04-29 12:40:47 +0000280 // to handle later processing (and by 'handle' I mean don't do anything
281 // with these sections).
Preston Gurdc68dda82012-04-12 20:13:57 +0000282 Allocate = 0;
283 Addr = 0;
284 DEBUG(dbgs() << "emitSection SectionID: " << SectionID
285 << " obj addr: " << format("%p", data.data())
286 << " new addr: 0"
287 << " DataSize: " << DataSize
288 << " StubBufSize: " << StubBufSize
289 << " Allocate: " << Allocate
290 << "\n");
291 }
292
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000293 Sections.push_back(SectionEntry(Addr, Allocate, DataSize,(uintptr_t)pData));
294 return SectionID;
295}
296
Preston Gurd689ff9c2012-04-16 22:12:58 +0000297unsigned RuntimeDyldImpl::findOrEmitSection(ObjectImage &Obj,
298 const SectionRef &Section,
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000299 bool IsCode,
300 ObjSectionToIDMap &LocalSections) {
301
302 unsigned SectionID = 0;
303 ObjSectionToIDMap::iterator i = LocalSections.find(Section);
304 if (i != LocalSections.end())
305 SectionID = i->second;
306 else {
Preston Gurd689ff9c2012-04-16 22:12:58 +0000307 SectionID = emitSection(Obj, Section, IsCode);
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000308 LocalSections[Section] = SectionID;
309 }
310 return SectionID;
311}
312
Eli Benderskyc201e6e2012-05-01 10:41:12 +0000313void RuntimeDyldImpl::addRelocationForSection(const RelocationEntry &RE,
314 unsigned SectionID) {
315 Relocations[SectionID].push_back(RE);
316}
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000317
Eli Benderskyc201e6e2012-05-01 10:41:12 +0000318void RuntimeDyldImpl::addRelocationForSymbol(const RelocationEntry &RE,
319 StringRef SymbolName) {
320 // Relocation by symbol. If the symbol is found in the global symbol table,
321 // create an appropriate section relocation. Otherwise, add it to
322 // ExternalSymbolRelocations.
323 SymbolTableMap::const_iterator Loc =
324 GlobalSymbolTable.find(SymbolName);
325 if (Loc == GlobalSymbolTable.end()) {
326 ExternalSymbolRelocations[SymbolName].push_back(RE);
Eli Bendersky37bc5a22012-04-30 12:15:58 +0000327 } else {
Eli Benderskyc201e6e2012-05-01 10:41:12 +0000328 // Copy the RE since we want to modify its addend.
329 RelocationEntry RECopy = RE;
330 RECopy.Addend += Loc->second.second;
331 Relocations[Loc->second.first].push_back(RECopy);
Eli Bendersky37bc5a22012-04-30 12:15:58 +0000332 }
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000333}
334
335uint8_t *RuntimeDyldImpl::createStubFunction(uint8_t *Addr) {
336 // TODO: There is only ARM far stub now. We should add the Thumb stub,
337 // and stubs for branches Thumb - ARM and ARM - Thumb.
338 if (Arch == Triple::arm) {
339 uint32_t *StubAddr = (uint32_t*)Addr;
340 *StubAddr = 0xe51ff004; // ldr pc,<label>
341 return (uint8_t*)++StubAddr;
342 }
343 else
344 return Addr;
345}
346
347// Assign an address to a symbol name and resolve all the relocations
348// associated with it.
349void RuntimeDyldImpl::reassignSectionAddress(unsigned SectionID,
350 uint64_t Addr) {
351 // The address to use for relocation resolution is not
352 // the address of the local section buffer. We must be doing
353 // a remote execution environment of some sort. Re-apply any
354 // relocations referencing this section with the given address.
355 //
356 // Addr is a uint64_t because we can't assume the pointer width
357 // of the target is the same as that of the host. Just use a generic
358 // "big enough" type.
359 Sections[SectionID].LoadAddress = Addr;
360 DEBUG(dbgs() << "Resolving relocations Section #" << SectionID
361 << "\t" << format("%p", (uint8_t *)Addr)
362 << "\n");
363 resolveRelocationList(Relocations[SectionID], Addr);
364}
365
366void RuntimeDyldImpl::resolveRelocationEntry(const RelocationEntry &RE,
367 uint64_t Value) {
Preston Gurdc68dda82012-04-12 20:13:57 +0000368 // Ignore relocations for sections that were not loaded
369 if (Sections[RE.SectionID].Address != 0) {
370 uint8_t *Target = Sections[RE.SectionID].Address + RE.Offset;
371 DEBUG(dbgs() << "\tSectionID: " << RE.SectionID
372 << " + " << RE.Offset << " (" << format("%p", Target) << ")"
Eli Bendersky6d15e872012-04-30 10:06:27 +0000373 << " RelType: " << RE.RelType
Preston Gurdc68dda82012-04-12 20:13:57 +0000374 << " Addend: " << RE.Addend
375 << "\n");
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000376
Preston Gurdc68dda82012-04-12 20:13:57 +0000377 resolveRelocation(Target, Sections[RE.SectionID].LoadAddress + RE.Offset,
Eli Bendersky6d15e872012-04-30 10:06:27 +0000378 Value, RE.RelType, RE.Addend);
Preston Gurdc68dda82012-04-12 20:13:57 +0000379 }
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000380}
381
382void RuntimeDyldImpl::resolveRelocationList(const RelocationList &Relocs,
383 uint64_t Value) {
384 for (unsigned i = 0, e = Relocs.size(); i != e; ++i) {
385 resolveRelocationEntry(Relocs[i], Value);
386 }
387}
388
Eli Bendersky37bc5a22012-04-30 12:15:58 +0000389void RuntimeDyldImpl::resolveExternalSymbols() {
390 StringMap<RelocationList>::iterator i = ExternalSymbolRelocations.begin(),
391 e = ExternalSymbolRelocations.end();
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000392 for (; i != e; i++) {
393 StringRef Name = i->first();
394 RelocationList &Relocs = i->second;
Eli Benderskyd98c9e92012-05-01 06:58:59 +0000395 SymbolTableMap::const_iterator Loc = GlobalSymbolTable.find(Name);
396 if (Loc == GlobalSymbolTable.end()) {
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000397 // This is an external symbol, try to get it address from
398 // MemoryManager.
399 uint8_t *Addr = (uint8_t*) MemMgr->getPointerToNamedFunction(Name.data(),
400 true);
401 DEBUG(dbgs() << "Resolving relocations Name: " << Name
402 << "\t" << format("%p", Addr)
403 << "\n");
404 resolveRelocationList(Relocs, (uintptr_t)Addr);
405 } else {
Eli Bendersky37bc5a22012-04-30 12:15:58 +0000406 report_fatal_error("Expected external symbol");
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000407 }
408 }
409}
410
411
Jim Grosbach6e563312011-03-21 22:15:52 +0000412//===----------------------------------------------------------------------===//
413// RuntimeDyld class implementation
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000414RuntimeDyld::RuntimeDyld(RTDyldMemoryManager *mm) {
415 Dyld = 0;
416 MM = mm;
Jim Grosbach6e563312011-03-21 22:15:52 +0000417}
418
419RuntimeDyld::~RuntimeDyld() {
420 delete Dyld;
421}
422
423bool RuntimeDyld::loadObject(MemoryBuffer *InputBuffer) {
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000424 if (!Dyld) {
Eli Benderskya66a1852012-01-16 08:56:09 +0000425 sys::LLVMFileType type = sys::IdentifyFileType(
426 InputBuffer->getBufferStart(),
427 static_cast<unsigned>(InputBuffer->getBufferSize()));
428 switch (type) {
429 case sys::ELF_Relocatable_FileType:
430 case sys::ELF_Executable_FileType:
431 case sys::ELF_SharedObject_FileType:
432 case sys::ELF_Core_FileType:
433 Dyld = new RuntimeDyldELF(MM);
434 break;
435 case sys::Mach_O_Object_FileType:
436 case sys::Mach_O_Executable_FileType:
437 case sys::Mach_O_FixedVirtualMemorySharedLib_FileType:
438 case sys::Mach_O_Core_FileType:
439 case sys::Mach_O_PreloadExecutable_FileType:
440 case sys::Mach_O_DynamicallyLinkedSharedLib_FileType:
441 case sys::Mach_O_DynamicLinker_FileType:
442 case sys::Mach_O_Bundle_FileType:
443 case sys::Mach_O_DynamicallyLinkedSharedLibStub_FileType:
444 case sys::Mach_O_DSYMCompanion_FileType:
445 Dyld = new RuntimeDyldMachO(MM);
446 break;
447 case sys::Unknown_FileType:
448 case sys::Bitcode_FileType:
449 case sys::Archive_FileType:
450 case sys::COFF_FileType:
451 report_fatal_error("Incompatible object format!");
452 }
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000453 } else {
Eli Benderskya66a1852012-01-16 08:56:09 +0000454 if (!Dyld->isCompatibleFormat(InputBuffer))
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000455 report_fatal_error("Incompatible object format!");
456 }
457
Jim Grosbach6e563312011-03-21 22:15:52 +0000458 return Dyld->loadObject(InputBuffer);
459}
460
Jim Grosbachb0271052011-04-08 17:31:24 +0000461void *RuntimeDyld::getSymbolAddress(StringRef Name) {
Jim Grosbach6e563312011-03-21 22:15:52 +0000462 return Dyld->getSymbolAddress(Name);
463}
464
Jim Grosbachf8c1c842011-04-12 21:20:41 +0000465void RuntimeDyld::resolveRelocations() {
466 Dyld->resolveRelocations();
467}
468
Jim Grosbach61425c02012-01-16 22:26:39 +0000469void RuntimeDyld::reassignSectionAddress(unsigned SectionID,
470 uint64_t Addr) {
471 Dyld->reassignSectionAddress(SectionID, Addr);
Jim Grosbachf8c1c842011-04-12 21:20:41 +0000472}
473
Jim Grosbach020f4e82012-01-16 23:50:55 +0000474void RuntimeDyld::mapSectionAddress(void *LocalAddress,
475 uint64_t TargetAddress) {
476 Dyld->mapSectionAddress(LocalAddress, TargetAddress);
477}
478
Jim Grosbach91dde152011-03-22 18:22:27 +0000479StringRef RuntimeDyld::getErrorString() {
Jim Grosbachb3eecaf2011-03-22 18:19:42 +0000480 return Dyld->getErrorString();
481}
482
Jim Grosbach6e563312011-03-21 22:15:52 +0000483} // end namespace llvm