blob: ff05c82aecb3906c0143cc7335405ca8153a6d18 [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"
Andrew Kaylor3f23cef2012-10-02 21:18:39 +000015#include "ObjectImageCommon.h"
Danil Malyshevcf852dc2011-07-13 07:57:58 +000016#include "RuntimeDyldImpl.h"
Eli Bendersky76463fd2012-01-22 07:05:02 +000017#include "RuntimeDyldELF.h"
18#include "RuntimeDyldMachO.h"
Eli Benderskya66a1852012-01-16 08:56:09 +000019#include "llvm/Support/Path.h"
Eli Bendersky76463fd2012-01-22 07:05:02 +000020
Jim Grosbach6e563312011-03-21 22:15:52 +000021using namespace llvm;
22using namespace llvm::object;
23
Chandler Carruth53c5e7b2011-04-05 23:54:31 +000024// Empty out-of-line virtual destructor as the key function.
25RTDyldMemoryManager::~RTDyldMemoryManager() {}
Danil Malyshevcf852dc2011-07-13 07:57:58 +000026RuntimeDyldImpl::~RuntimeDyldImpl() {}
Chandler Carruth53c5e7b2011-04-05 23:54:31 +000027
Jim Grosbach6e563312011-03-21 22:15:52 +000028namespace llvm {
Jim Grosbach6e563312011-03-21 22:15:52 +000029
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +000030namespace {
31 // Helper for extensive error checking in debug builds.
32 error_code Check(error_code Err) {
33 if (Err) {
34 report_fatal_error(Err.message());
35 }
36 return Err;
37 }
38} // end anonymous namespace
Jim Grosbach61425c02012-01-16 22:26:39 +000039
Jim Grosbachf8c1c842011-04-12 21:20:41 +000040// Resolve the relocations for all symbols we currently know about.
41void RuntimeDyldImpl::resolveRelocations() {
Preston Gurdc68dda82012-04-12 20:13:57 +000042 // First, resolve relocations associated with external symbols.
Eli Bendersky37bc5a22012-04-30 12:15:58 +000043 resolveExternalSymbols();
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +000044
Jim Grosbach61425c02012-01-16 22:26:39 +000045 // Just iterate over the sections we have and resolve all the relocations
46 // in them. Gross overkill, but it gets the job done.
47 for (int i = 0, e = Sections.size(); i != e; ++i) {
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +000048 reassignSectionAddress(i, Sections[i].LoadAddress);
Jim Grosbach61425c02012-01-16 22:26:39 +000049 }
Jim Grosbachf8c1c842011-04-12 21:20:41 +000050}
51
Jim Grosbache940c1b2012-09-13 21:50:06 +000052void RuntimeDyldImpl::mapSectionAddress(const void *LocalAddress,
Jim Grosbach020f4e82012-01-16 23:50:55 +000053 uint64_t TargetAddress) {
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +000054 for (unsigned i = 0, e = Sections.size(); i != e; ++i) {
55 if (Sections[i].Address == LocalAddress) {
56 reassignSectionAddress(i, TargetAddress);
57 return;
58 }
59 }
60 llvm_unreachable("Attempting to remap address of unknown section!");
Jim Grosbach020f4e82012-01-16 23:50:55 +000061}
62
Eli Bendersky5fe01982012-04-29 12:40:47 +000063// Subclasses can implement this method to create specialized image instances.
Sylvestre Ledruc8e41c52012-07-23 08:51:15 +000064// The caller owns the pointer that is returned.
Andrew Kaylor3f23cef2012-10-02 21:18:39 +000065ObjectImage *RuntimeDyldImpl::createObjectImage(ObjectBuffer *InputBuffer) {
66 return new ObjectImageCommon(InputBuffer);
Preston Gurd689ff9c2012-04-16 22:12:58 +000067}
68
Andrew Kaylor3f23cef2012-10-02 21:18:39 +000069ObjectImage *RuntimeDyldImpl::loadObject(ObjectBuffer *InputBuffer) {
Preston Gurd689ff9c2012-04-16 22:12:58 +000070 OwningPtr<ObjectImage> obj(createObjectImage(InputBuffer));
Preston Gurdc68dda82012-04-12 20:13:57 +000071 if (!obj)
72 report_fatal_error("Unable to create object image from memory buffer!");
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +000073
74 Arch = (Triple::ArchType)obj->getArch();
75
Eli Benderskyd98c9e92012-05-01 06:58:59 +000076 // Symbols found in this object
77 StringMap<SymbolLoc> LocalSymbols;
78 // Used sections from the object file
79 ObjSectionToIDMap LocalSections;
80
81 // Common symbols requiring allocation, and the total size required to
82 // allocate all common symbols.
83 CommonSymbolMap CommonSymbols;
84 uint64_t CommonSize = 0;
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +000085
86 error_code err;
87 // Parse symbols
88 DEBUG(dbgs() << "Parse symbols:\n");
89 for (symbol_iterator i = obj->begin_symbols(), e = obj->end_symbols();
90 i != e; i.increment(err)) {
91 Check(err);
92 object::SymbolRef::Type SymType;
93 StringRef Name;
94 Check(i->getType(SymType));
95 Check(i->getName(Name));
96
Preston Gurdc68dda82012-04-12 20:13:57 +000097 uint32_t flags;
98 Check(i->getFlags(flags));
99
100 bool isCommon = flags & SymbolRef::SF_Common;
101 if (isCommon) {
102 // Add the common symbols to a list. We'll allocate them all below.
103 uint64_t Size = 0;
104 Check(i->getSize(Size));
105 CommonSize += Size;
106 CommonSymbols[*i] = Size;
107 } else {
108 if (SymType == object::SymbolRef::ST_Function ||
Akira Hatanakab889e0c2012-08-17 21:28:04 +0000109 SymType == object::SymbolRef::ST_Data ||
110 SymType == object::SymbolRef::ST_Unknown) {
Preston Gurdc68dda82012-04-12 20:13:57 +0000111 uint64_t FileOffset;
Eli Bendersky5fe01982012-04-29 12:40:47 +0000112 StringRef SectionData;
Preston Gurdc68dda82012-04-12 20:13:57 +0000113 section_iterator si = obj->end_sections();
114 Check(i->getFileOffset(FileOffset));
115 Check(i->getSection(si));
116 if (si == obj->end_sections()) continue;
Eli Bendersky5fe01982012-04-29 12:40:47 +0000117 Check(si->getContents(SectionData));
Preston Gurdc68dda82012-04-12 20:13:57 +0000118 const uint8_t* SymPtr = (const uint8_t*)InputBuffer->getBufferStart() +
119 (uintptr_t)FileOffset;
Eli Bendersky5fe01982012-04-29 12:40:47 +0000120 uintptr_t SectOffset = (uintptr_t)(SymPtr -
121 (const uint8_t*)SectionData.begin());
Preston Gurdc68dda82012-04-12 20:13:57 +0000122 unsigned SectionID =
Preston Gurd689ff9c2012-04-16 22:12:58 +0000123 findOrEmitSection(*obj,
124 *si,
Preston Gurdc68dda82012-04-12 20:13:57 +0000125 SymType == object::SymbolRef::ST_Function,
126 LocalSections);
Preston Gurdc68dda82012-04-12 20:13:57 +0000127 LocalSymbols[Name.data()] = SymbolLoc(SectionID, SectOffset);
128 DEBUG(dbgs() << "\tFileOffset: " << format("%p", (uintptr_t)FileOffset)
129 << " flags: " << flags
130 << " SID: " << SectionID
131 << " Offset: " << format("%p", SectOffset));
Eli Bendersky5fe01982012-04-29 12:40:47 +0000132 bool isGlobal = flags & SymbolRef::SF_Global;
Preston Gurdc68dda82012-04-12 20:13:57 +0000133 if (isGlobal)
Eli Benderskyd98c9e92012-05-01 06:58:59 +0000134 GlobalSymbolTable[Name] = SymbolLoc(SectionID, SectOffset);
Preston Gurdc68dda82012-04-12 20:13:57 +0000135 }
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000136 }
137 DEBUG(dbgs() << "\tType: " << SymType << " Name: " << Name << "\n");
138 }
139
Preston Gurdc68dda82012-04-12 20:13:57 +0000140 // Allocate common symbols
141 if (CommonSize != 0)
Preston Gurd689ff9c2012-04-16 22:12:58 +0000142 emitCommonSymbols(*obj, CommonSymbols, CommonSize, LocalSymbols);
Preston Gurdc68dda82012-04-12 20:13:57 +0000143
Eli Bendersky5fe01982012-04-29 12:40:47 +0000144 // Parse and process relocations
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000145 DEBUG(dbgs() << "Parse relocations:\n");
146 for (section_iterator si = obj->begin_sections(),
147 se = obj->end_sections(); si != se; si.increment(err)) {
148 Check(err);
149 bool isFirstRelocation = true;
150 unsigned SectionID = 0;
151 StubMap Stubs;
152
153 for (relocation_iterator i = si->begin_relocations(),
154 e = si->end_relocations(); i != e; i.increment(err)) {
155 Check(err);
156
Eli Bendersky5fe01982012-04-29 12:40:47 +0000157 // If it's the first relocation in this section, find its SectionID
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000158 if (isFirstRelocation) {
Preston Gurd689ff9c2012-04-16 22:12:58 +0000159 SectionID = findOrEmitSection(*obj, *si, true, LocalSections);
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000160 DEBUG(dbgs() << "\tSectionID: " << SectionID << "\n");
161 isFirstRelocation = false;
162 }
163
164 ObjRelocationInfo RI;
165 RI.SectionID = SectionID;
166 Check(i->getAdditionalInfo(RI.AdditionalInfo));
167 Check(i->getOffset(RI.Offset));
168 Check(i->getSymbol(RI.Symbol));
169 Check(i->getType(RI.Type));
170
171 DEBUG(dbgs() << "\t\tAddend: " << RI.AdditionalInfo
172 << " Offset: " << format("%p", (uintptr_t)RI.Offset)
173 << " Type: " << (uint32_t)(RI.Type & 0xffffffffL)
174 << "\n");
175 processRelocationRef(RI, *obj, LocalSections, LocalSymbols, Stubs);
176 }
177 }
Preston Gurd689ff9c2012-04-16 22:12:58 +0000178
Andrew Kaylor3f23cef2012-10-02 21:18:39 +0000179 return obj.take();
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000180}
181
Eli Benderskyc201e6e2012-05-01 10:41:12 +0000182void RuntimeDyldImpl::emitCommonSymbols(ObjectImage &Obj,
183 const CommonSymbolMap &CommonSymbols,
184 uint64_t TotalSize,
185 SymbolTableMap &SymbolTable) {
Preston Gurdc68dda82012-04-12 20:13:57 +0000186 // Allocate memory for the section
187 unsigned SectionID = Sections.size();
188 uint8_t *Addr = MemMgr->allocateDataSection(TotalSize, sizeof(void*),
189 SectionID);
190 if (!Addr)
191 report_fatal_error("Unable to allocate memory for common symbols!");
192 uint64_t Offset = 0;
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +0000193 Sections.push_back(SectionEntry(StringRef(), Addr, TotalSize, TotalSize, 0));
Preston Gurdc68dda82012-04-12 20:13:57 +0000194 memset(Addr, 0, TotalSize);
195
196 DEBUG(dbgs() << "emitCommonSection SectionID: " << SectionID
197 << " new addr: " << format("%p", Addr)
198 << " DataSize: " << TotalSize
199 << "\n");
200
201 // Assign the address of each symbol
Eli Benderskyc201e6e2012-05-01 10:41:12 +0000202 for (CommonSymbolMap::const_iterator it = CommonSymbols.begin(),
203 itEnd = CommonSymbols.end(); it != itEnd; it++) {
Preston Gurdc68dda82012-04-12 20:13:57 +0000204 StringRef Name;
205 it->first.getName(Name);
Preston Gurd689ff9c2012-04-16 22:12:58 +0000206 Obj.updateSymbolAddress(it->first, (uint64_t)Addr);
Eli Benderskyc201e6e2012-05-01 10:41:12 +0000207 SymbolTable[Name.data()] = SymbolLoc(SectionID, Offset);
208 uint64_t Size = it->second;
Preston Gurdc68dda82012-04-12 20:13:57 +0000209 Offset += Size;
210 Addr += Size;
211 }
Preston Gurdc68dda82012-04-12 20:13:57 +0000212}
213
Preston Gurd689ff9c2012-04-16 22:12:58 +0000214unsigned RuntimeDyldImpl::emitSection(ObjectImage &Obj,
215 const SectionRef &Section,
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000216 bool IsCode) {
217
218 unsigned StubBufSize = 0,
219 StubSize = getMaxStubSize();
220 error_code err;
221 if (StubSize > 0) {
222 for (relocation_iterator i = Section.begin_relocations(),
Preston Gurdc68dda82012-04-12 20:13:57 +0000223 e = Section.end_relocations(); i != e; i.increment(err), Check(err))
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000224 StubBufSize += StubSize;
225 }
226 StringRef data;
227 uint64_t Alignment64;
228 Check(Section.getContents(data));
229 Check(Section.getAlignment(Alignment64));
230
231 unsigned Alignment = (unsigned)Alignment64 & 0xffffffffL;
Preston Gurdc68dda82012-04-12 20:13:57 +0000232 bool IsRequired;
233 bool IsVirtual;
234 bool IsZeroInit;
235 uint64_t DataSize;
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +0000236 StringRef Name;
Preston Gurdc68dda82012-04-12 20:13:57 +0000237 Check(Section.isRequiredForExecution(IsRequired));
238 Check(Section.isVirtual(IsVirtual));
239 Check(Section.isZeroInit(IsZeroInit));
240 Check(Section.getSize(DataSize));
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +0000241 Check(Section.getName(Name));
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000242
Preston Gurdc68dda82012-04-12 20:13:57 +0000243 unsigned Allocate;
244 unsigned SectionID = Sections.size();
245 uint8_t *Addr;
246 const char *pData = 0;
247
248 // Some sections, such as debug info, don't need to be loaded for execution.
249 // Leave those where they are.
250 if (IsRequired) {
251 Allocate = DataSize + StubBufSize;
252 Addr = IsCode
253 ? MemMgr->allocateCodeSection(Allocate, Alignment, SectionID)
254 : MemMgr->allocateDataSection(Allocate, Alignment, SectionID);
255 if (!Addr)
256 report_fatal_error("Unable to allocate section memory!");
257
258 // Virtual sections have no data in the object image, so leave pData = 0
259 if (!IsVirtual)
260 pData = data.data();
261
262 // Zero-initialize or copy the data from the image
263 if (IsZeroInit || IsVirtual)
264 memset(Addr, 0, DataSize);
265 else
266 memcpy(Addr, pData, DataSize);
267
268 DEBUG(dbgs() << "emitSection SectionID: " << SectionID
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +0000269 << " Name: " << Name
Preston Gurdc68dda82012-04-12 20:13:57 +0000270 << " 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
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +0000285 << " Name: " << Name
Preston Gurdc68dda82012-04-12 20:13:57 +0000286 << " obj addr: " << format("%p", data.data())
287 << " new addr: 0"
288 << " DataSize: " << DataSize
289 << " StubBufSize: " << StubBufSize
290 << " Allocate: " << Allocate
291 << "\n");
292 }
293
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +0000294 Sections.push_back(SectionEntry(Name, Addr, Allocate, DataSize,
295 (uintptr_t)pData));
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000296 return SectionID;
297}
298
Preston Gurd689ff9c2012-04-16 22:12:58 +0000299unsigned RuntimeDyldImpl::findOrEmitSection(ObjectImage &Obj,
300 const SectionRef &Section,
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000301 bool IsCode,
302 ObjSectionToIDMap &LocalSections) {
303
304 unsigned SectionID = 0;
305 ObjSectionToIDMap::iterator i = LocalSections.find(Section);
306 if (i != LocalSections.end())
307 SectionID = i->second;
308 else {
Preston Gurd689ff9c2012-04-16 22:12:58 +0000309 SectionID = emitSection(Obj, Section, IsCode);
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000310 LocalSections[Section] = SectionID;
311 }
312 return SectionID;
313}
314
Eli Benderskyc201e6e2012-05-01 10:41:12 +0000315void RuntimeDyldImpl::addRelocationForSection(const RelocationEntry &RE,
316 unsigned SectionID) {
317 Relocations[SectionID].push_back(RE);
318}
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000319
Eli Benderskyc201e6e2012-05-01 10:41:12 +0000320void RuntimeDyldImpl::addRelocationForSymbol(const RelocationEntry &RE,
321 StringRef SymbolName) {
322 // Relocation by symbol. If the symbol is found in the global symbol table,
323 // create an appropriate section relocation. Otherwise, add it to
324 // ExternalSymbolRelocations.
325 SymbolTableMap::const_iterator Loc =
326 GlobalSymbolTable.find(SymbolName);
327 if (Loc == GlobalSymbolTable.end()) {
328 ExternalSymbolRelocations[SymbolName].push_back(RE);
Eli Bendersky37bc5a22012-04-30 12:15:58 +0000329 } else {
Eli Benderskyc201e6e2012-05-01 10:41:12 +0000330 // Copy the RE since we want to modify its addend.
331 RelocationEntry RECopy = RE;
332 RECopy.Addend += Loc->second.second;
333 Relocations[Loc->second.first].push_back(RECopy);
Eli Bendersky37bc5a22012-04-30 12:15:58 +0000334 }
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000335}
336
337uint8_t *RuntimeDyldImpl::createStubFunction(uint8_t *Addr) {
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000338 if (Arch == Triple::arm) {
Akira Hatanakab889e0c2012-08-17 21:28:04 +0000339 // TODO: There is only ARM far stub now. We should add the Thumb stub,
340 // and stubs for branches Thumb - ARM and ARM - Thumb.
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000341 uint32_t *StubAddr = (uint32_t*)Addr;
342 *StubAddr = 0xe51ff004; // ldr pc,<label>
343 return (uint8_t*)++StubAddr;
Akira Hatanakab889e0c2012-08-17 21:28:04 +0000344 } else if (Arch == Triple::mipsel) {
345 uint32_t *StubAddr = (uint32_t*)Addr;
346 // 0: 3c190000 lui t9,%hi(addr).
347 // 4: 27390000 addiu t9,t9,%lo(addr).
348 // 8: 03200008 jr t9.
349 // c: 00000000 nop.
350 const unsigned LuiT9Instr = 0x3c190000, AdduiT9Instr = 0x27390000;
351 const unsigned JrT9Instr = 0x03200008, NopInstr = 0x0;
352
353 *StubAddr = LuiT9Instr;
354 StubAddr++;
355 *StubAddr = AdduiT9Instr;
356 StubAddr++;
357 *StubAddr = JrT9Instr;
358 StubAddr++;
359 *StubAddr = NopInstr;
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000360 return Addr;
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +0000361 } else if (Arch == Triple::ppc64) {
362 // PowerPC64 stub: the address points to a function descriptor
363 // instead of the function itself. Load the function address
364 // on r11 and sets it to control register. Also loads the function
365 // TOC in r2 and environment pointer to r11.
366 writeInt32BE(Addr, 0x3D800000); // lis r12, highest(addr)
367 writeInt32BE(Addr+4, 0x618C0000); // ori r12, higher(addr)
368 writeInt32BE(Addr+8, 0x798C07C6); // sldi r12, r12, 32
369 writeInt32BE(Addr+12, 0x658C0000); // oris r12, r12, h(addr)
370 writeInt32BE(Addr+16, 0x618C0000); // ori r12, r12, l(addr)
371 writeInt32BE(Addr+20, 0xF8410028); // std r2, 40(r1)
372 writeInt32BE(Addr+24, 0xE96C0000); // ld r11, 0(r12)
373 writeInt32BE(Addr+28, 0xE84C0008); // ld r2, 0(r12)
374 writeInt32BE(Addr+32, 0x7D6903A6); // mtctr r11
375 writeInt32BE(Addr+36, 0xE96C0010); // ld r11, 16(r2)
376 writeInt32BE(Addr+40, 0x4E800420); // bctr
377
378 return Addr;
Akira Hatanakab889e0c2012-08-17 21:28:04 +0000379 }
380 return Addr;
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000381}
382
383// Assign an address to a symbol name and resolve all the relocations
384// associated with it.
385void RuntimeDyldImpl::reassignSectionAddress(unsigned SectionID,
386 uint64_t Addr) {
387 // The address to use for relocation resolution is not
388 // the address of the local section buffer. We must be doing
389 // a remote execution environment of some sort. Re-apply any
390 // relocations referencing this section with the given address.
391 //
392 // Addr is a uint64_t because we can't assume the pointer width
393 // of the target is the same as that of the host. Just use a generic
394 // "big enough" type.
395 Sections[SectionID].LoadAddress = Addr;
396 DEBUG(dbgs() << "Resolving relocations Section #" << SectionID
397 << "\t" << format("%p", (uint8_t *)Addr)
398 << "\n");
399 resolveRelocationList(Relocations[SectionID], Addr);
400}
401
402void RuntimeDyldImpl::resolveRelocationEntry(const RelocationEntry &RE,
403 uint64_t Value) {
Eric Christophera7ca3c22012-10-12 02:04:47 +0000404 // Ignore relocations for sections that were not loaded
405 if (Sections[RE.SectionID].Address != 0) {
406 uint8_t *Target = Sections[RE.SectionID].Address + RE.Offset;
407 DEBUG(dbgs() << "\tSectionID: " << RE.SectionID
408 << " + " << RE.Offset << " (" << format("%p", Target) << ")"
409 << " RelType: " << RE.RelType
410 << " Addend: " << RE.Addend
411 << "\n");
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000412
Eric Christophera7ca3c22012-10-12 02:04:47 +0000413 resolveRelocation(Target, Sections[RE.SectionID].LoadAddress + RE.Offset,
414 Value, RE.RelType, RE.Addend);
Preston Gurdc68dda82012-04-12 20:13:57 +0000415 }
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000416}
417
418void RuntimeDyldImpl::resolveRelocationList(const RelocationList &Relocs,
419 uint64_t Value) {
420 for (unsigned i = 0, e = Relocs.size(); i != e; ++i) {
421 resolveRelocationEntry(Relocs[i], Value);
422 }
423}
424
Eli Bendersky37bc5a22012-04-30 12:15:58 +0000425void RuntimeDyldImpl::resolveExternalSymbols() {
426 StringMap<RelocationList>::iterator i = ExternalSymbolRelocations.begin(),
427 e = ExternalSymbolRelocations.end();
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000428 for (; i != e; i++) {
429 StringRef Name = i->first();
430 RelocationList &Relocs = i->second;
Eli Benderskyd98c9e92012-05-01 06:58:59 +0000431 SymbolTableMap::const_iterator Loc = GlobalSymbolTable.find(Name);
432 if (Loc == GlobalSymbolTable.end()) {
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000433 // This is an external symbol, try to get it address from
434 // MemoryManager.
435 uint8_t *Addr = (uint8_t*) MemMgr->getPointerToNamedFunction(Name.data(),
436 true);
437 DEBUG(dbgs() << "Resolving relocations Name: " << Name
438 << "\t" << format("%p", Addr)
439 << "\n");
440 resolveRelocationList(Relocs, (uintptr_t)Addr);
441 } else {
Eli Bendersky37bc5a22012-04-30 12:15:58 +0000442 report_fatal_error("Expected external symbol");
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000443 }
444 }
445}
446
447
Jim Grosbach6e563312011-03-21 22:15:52 +0000448//===----------------------------------------------------------------------===//
449// RuntimeDyld class implementation
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000450RuntimeDyld::RuntimeDyld(RTDyldMemoryManager *mm) {
451 Dyld = 0;
452 MM = mm;
Jim Grosbach6e563312011-03-21 22:15:52 +0000453}
454
455RuntimeDyld::~RuntimeDyld() {
456 delete Dyld;
457}
458
Andrew Kaylor3f23cef2012-10-02 21:18:39 +0000459ObjectImage *RuntimeDyld::loadObject(ObjectBuffer *InputBuffer) {
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000460 if (!Dyld) {
Eli Benderskya66a1852012-01-16 08:56:09 +0000461 sys::LLVMFileType type = sys::IdentifyFileType(
462 InputBuffer->getBufferStart(),
463 static_cast<unsigned>(InputBuffer->getBufferSize()));
464 switch (type) {
465 case sys::ELF_Relocatable_FileType:
466 case sys::ELF_Executable_FileType:
467 case sys::ELF_SharedObject_FileType:
468 case sys::ELF_Core_FileType:
469 Dyld = new RuntimeDyldELF(MM);
470 break;
471 case sys::Mach_O_Object_FileType:
472 case sys::Mach_O_Executable_FileType:
473 case sys::Mach_O_FixedVirtualMemorySharedLib_FileType:
474 case sys::Mach_O_Core_FileType:
475 case sys::Mach_O_PreloadExecutable_FileType:
476 case sys::Mach_O_DynamicallyLinkedSharedLib_FileType:
477 case sys::Mach_O_DynamicLinker_FileType:
478 case sys::Mach_O_Bundle_FileType:
479 case sys::Mach_O_DynamicallyLinkedSharedLibStub_FileType:
480 case sys::Mach_O_DSYMCompanion_FileType:
481 Dyld = new RuntimeDyldMachO(MM);
482 break;
483 case sys::Unknown_FileType:
484 case sys::Bitcode_FileType:
485 case sys::Archive_FileType:
486 case sys::COFF_FileType:
487 report_fatal_error("Incompatible object format!");
488 }
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000489 } else {
Eli Benderskya66a1852012-01-16 08:56:09 +0000490 if (!Dyld->isCompatibleFormat(InputBuffer))
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000491 report_fatal_error("Incompatible object format!");
492 }
493
Jim Grosbach6e563312011-03-21 22:15:52 +0000494 return Dyld->loadObject(InputBuffer);
495}
496
Jim Grosbachb0271052011-04-08 17:31:24 +0000497void *RuntimeDyld::getSymbolAddress(StringRef Name) {
Jim Grosbach6e563312011-03-21 22:15:52 +0000498 return Dyld->getSymbolAddress(Name);
499}
500
Jim Grosbach35ed8422012-09-05 16:50:40 +0000501uint64_t RuntimeDyld::getSymbolLoadAddress(StringRef Name) {
502 return Dyld->getSymbolLoadAddress(Name);
503}
504
Jim Grosbachf8c1c842011-04-12 21:20:41 +0000505void RuntimeDyld::resolveRelocations() {
506 Dyld->resolveRelocations();
507}
508
Jim Grosbach61425c02012-01-16 22:26:39 +0000509void RuntimeDyld::reassignSectionAddress(unsigned SectionID,
510 uint64_t Addr) {
511 Dyld->reassignSectionAddress(SectionID, Addr);
Jim Grosbachf8c1c842011-04-12 21:20:41 +0000512}
513
Jim Grosbache940c1b2012-09-13 21:50:06 +0000514void RuntimeDyld::mapSectionAddress(const void *LocalAddress,
Jim Grosbach020f4e82012-01-16 23:50:55 +0000515 uint64_t TargetAddress) {
516 Dyld->mapSectionAddress(LocalAddress, TargetAddress);
517}
518
Jim Grosbach91dde152011-03-22 18:22:27 +0000519StringRef RuntimeDyld::getErrorString() {
Jim Grosbachb3eecaf2011-03-22 18:19:42 +0000520 return Dyld->getErrorString();
521}
522
Jim Grosbach6e563312011-03-21 22:15:52 +0000523} // end namespace llvm