blob: fb07cb902180454cf92a427e18e5bd092ec42b8d [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"
Tim Northoverf00677d2012-10-29 10:47:04 +000020#include "llvm/Support/MathExtras.h"
Eli Bendersky76463fd2012-01-22 07:05:02 +000021
Jim Grosbach6e563312011-03-21 22:15:52 +000022using namespace llvm;
23using namespace llvm::object;
24
Chandler Carruth53c5e7b2011-04-05 23:54:31 +000025// Empty out-of-line virtual destructor as the key function.
26RTDyldMemoryManager::~RTDyldMemoryManager() {}
Danil Malyshevcf852dc2011-07-13 07:57:58 +000027RuntimeDyldImpl::~RuntimeDyldImpl() {}
Chandler Carruth53c5e7b2011-04-05 23:54:31 +000028
Jim Grosbach6e563312011-03-21 22:15:52 +000029namespace llvm {
Jim Grosbach6e563312011-03-21 22:15:52 +000030
Jim Grosbachf8c1c842011-04-12 21:20:41 +000031// Resolve the relocations for all symbols we currently know about.
32void RuntimeDyldImpl::resolveRelocations() {
Preston Gurdc68dda82012-04-12 20:13:57 +000033 // First, resolve relocations associated with external symbols.
Eli Bendersky37bc5a22012-04-30 12:15:58 +000034 resolveExternalSymbols();
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +000035
Jim Grosbach61425c02012-01-16 22:26:39 +000036 // Just iterate over the sections we have and resolve all the relocations
37 // in them. Gross overkill, but it gets the job done.
38 for (int i = 0, e = Sections.size(); i != e; ++i) {
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +000039 reassignSectionAddress(i, Sections[i].LoadAddress);
Jim Grosbach61425c02012-01-16 22:26:39 +000040 }
Jim Grosbachf8c1c842011-04-12 21:20:41 +000041}
42
Jim Grosbache940c1b2012-09-13 21:50:06 +000043void RuntimeDyldImpl::mapSectionAddress(const void *LocalAddress,
Jim Grosbach020f4e82012-01-16 23:50:55 +000044 uint64_t TargetAddress) {
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +000045 for (unsigned i = 0, e = Sections.size(); i != e; ++i) {
46 if (Sections[i].Address == LocalAddress) {
47 reassignSectionAddress(i, TargetAddress);
48 return;
49 }
50 }
51 llvm_unreachable("Attempting to remap address of unknown section!");
Jim Grosbach020f4e82012-01-16 23:50:55 +000052}
53
Eli Bendersky5fe01982012-04-29 12:40:47 +000054// Subclasses can implement this method to create specialized image instances.
Sylvestre Ledruc8e41c52012-07-23 08:51:15 +000055// The caller owns the pointer that is returned.
Andrew Kaylor3f23cef2012-10-02 21:18:39 +000056ObjectImage *RuntimeDyldImpl::createObjectImage(ObjectBuffer *InputBuffer) {
57 return new ObjectImageCommon(InputBuffer);
Preston Gurd689ff9c2012-04-16 22:12:58 +000058}
59
Andrew Kaylor3f23cef2012-10-02 21:18:39 +000060ObjectImage *RuntimeDyldImpl::loadObject(ObjectBuffer *InputBuffer) {
Preston Gurd689ff9c2012-04-16 22:12:58 +000061 OwningPtr<ObjectImage> obj(createObjectImage(InputBuffer));
Preston Gurdc68dda82012-04-12 20:13:57 +000062 if (!obj)
63 report_fatal_error("Unable to create object image from memory buffer!");
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +000064
65 Arch = (Triple::ArchType)obj->getArch();
66
Eli Benderskyd98c9e92012-05-01 06:58:59 +000067 // Symbols found in this object
68 StringMap<SymbolLoc> LocalSymbols;
69 // Used sections from the object file
70 ObjSectionToIDMap LocalSections;
71
Tim Northoverf00677d2012-10-29 10:47:04 +000072 // Common symbols requiring allocation, with their sizes and alignments
Eli Benderskyd98c9e92012-05-01 06:58:59 +000073 CommonSymbolMap CommonSymbols;
Tim Northoverf00677d2012-10-29 10:47:04 +000074 // Maximum required total memory to allocate all common symbols
Eli Benderskyd98c9e92012-05-01 06:58:59 +000075 uint64_t CommonSize = 0;
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +000076
77 error_code err;
78 // Parse symbols
79 DEBUG(dbgs() << "Parse symbols:\n");
80 for (symbol_iterator i = obj->begin_symbols(), e = obj->end_symbols();
81 i != e; i.increment(err)) {
82 Check(err);
83 object::SymbolRef::Type SymType;
84 StringRef Name;
85 Check(i->getType(SymType));
86 Check(i->getName(Name));
87
Preston Gurdc68dda82012-04-12 20:13:57 +000088 uint32_t flags;
89 Check(i->getFlags(flags));
90
91 bool isCommon = flags & SymbolRef::SF_Common;
92 if (isCommon) {
93 // Add the common symbols to a list. We'll allocate them all below.
Tim Northoverf00677d2012-10-29 10:47:04 +000094 uint64_t Align = getCommonSymbolAlignment(*i);
Preston Gurdc68dda82012-04-12 20:13:57 +000095 uint64_t Size = 0;
96 Check(i->getSize(Size));
Tim Northoverf00677d2012-10-29 10:47:04 +000097 CommonSize += Size + Align;
98 CommonSymbols[*i] = CommonSymbolInfo(Size, Align);
Preston Gurdc68dda82012-04-12 20:13:57 +000099 } else {
100 if (SymType == object::SymbolRef::ST_Function ||
Akira Hatanakab889e0c2012-08-17 21:28:04 +0000101 SymType == object::SymbolRef::ST_Data ||
102 SymType == object::SymbolRef::ST_Unknown) {
Preston Gurdc68dda82012-04-12 20:13:57 +0000103 uint64_t FileOffset;
Eli Bendersky5fe01982012-04-29 12:40:47 +0000104 StringRef SectionData;
Preston Gurdc68dda82012-04-12 20:13:57 +0000105 section_iterator si = obj->end_sections();
106 Check(i->getFileOffset(FileOffset));
107 Check(i->getSection(si));
108 if (si == obj->end_sections()) continue;
Eli Bendersky5fe01982012-04-29 12:40:47 +0000109 Check(si->getContents(SectionData));
Preston Gurdc68dda82012-04-12 20:13:57 +0000110 const uint8_t* SymPtr = (const uint8_t*)InputBuffer->getBufferStart() +
111 (uintptr_t)FileOffset;
Eli Bendersky5fe01982012-04-29 12:40:47 +0000112 uintptr_t SectOffset = (uintptr_t)(SymPtr -
113 (const uint8_t*)SectionData.begin());
Preston Gurdc68dda82012-04-12 20:13:57 +0000114 unsigned SectionID =
Preston Gurd689ff9c2012-04-16 22:12:58 +0000115 findOrEmitSection(*obj,
116 *si,
Preston Gurdc68dda82012-04-12 20:13:57 +0000117 SymType == object::SymbolRef::ST_Function,
118 LocalSections);
Preston Gurdc68dda82012-04-12 20:13:57 +0000119 LocalSymbols[Name.data()] = SymbolLoc(SectionID, SectOffset);
120 DEBUG(dbgs() << "\tFileOffset: " << format("%p", (uintptr_t)FileOffset)
121 << " flags: " << flags
122 << " SID: " << SectionID
123 << " Offset: " << format("%p", SectOffset));
Eli Bendersky5fe01982012-04-29 12:40:47 +0000124 bool isGlobal = flags & SymbolRef::SF_Global;
Preston Gurdc68dda82012-04-12 20:13:57 +0000125 if (isGlobal)
Eli Benderskyd98c9e92012-05-01 06:58:59 +0000126 GlobalSymbolTable[Name] = SymbolLoc(SectionID, SectOffset);
Preston Gurdc68dda82012-04-12 20:13:57 +0000127 }
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000128 }
129 DEBUG(dbgs() << "\tType: " << SymType << " Name: " << Name << "\n");
130 }
131
Preston Gurdc68dda82012-04-12 20:13:57 +0000132 // Allocate common symbols
133 if (CommonSize != 0)
Preston Gurd689ff9c2012-04-16 22:12:58 +0000134 emitCommonSymbols(*obj, CommonSymbols, CommonSize, LocalSymbols);
Preston Gurdc68dda82012-04-12 20:13:57 +0000135
Eli Bendersky5fe01982012-04-29 12:40:47 +0000136 // Parse and process relocations
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000137 DEBUG(dbgs() << "Parse relocations:\n");
138 for (section_iterator si = obj->begin_sections(),
139 se = obj->end_sections(); si != se; si.increment(err)) {
140 Check(err);
141 bool isFirstRelocation = true;
142 unsigned SectionID = 0;
143 StubMap Stubs;
144
145 for (relocation_iterator i = si->begin_relocations(),
146 e = si->end_relocations(); i != e; i.increment(err)) {
147 Check(err);
148
Eli Bendersky5fe01982012-04-29 12:40:47 +0000149 // If it's the first relocation in this section, find its SectionID
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000150 if (isFirstRelocation) {
Preston Gurd689ff9c2012-04-16 22:12:58 +0000151 SectionID = findOrEmitSection(*obj, *si, true, LocalSections);
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000152 DEBUG(dbgs() << "\tSectionID: " << SectionID << "\n");
153 isFirstRelocation = false;
154 }
155
156 ObjRelocationInfo RI;
157 RI.SectionID = SectionID;
158 Check(i->getAdditionalInfo(RI.AdditionalInfo));
159 Check(i->getOffset(RI.Offset));
160 Check(i->getSymbol(RI.Symbol));
161 Check(i->getType(RI.Type));
162
163 DEBUG(dbgs() << "\t\tAddend: " << RI.AdditionalInfo
164 << " Offset: " << format("%p", (uintptr_t)RI.Offset)
165 << " Type: " << (uint32_t)(RI.Type & 0xffffffffL)
166 << "\n");
167 processRelocationRef(RI, *obj, LocalSections, LocalSymbols, Stubs);
168 }
169 }
Preston Gurd689ff9c2012-04-16 22:12:58 +0000170
Andrew Kaylor3f23cef2012-10-02 21:18:39 +0000171 return obj.take();
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000172}
173
Eli Benderskyc201e6e2012-05-01 10:41:12 +0000174void RuntimeDyldImpl::emitCommonSymbols(ObjectImage &Obj,
175 const CommonSymbolMap &CommonSymbols,
176 uint64_t TotalSize,
177 SymbolTableMap &SymbolTable) {
Preston Gurdc68dda82012-04-12 20:13:57 +0000178 // Allocate memory for the section
179 unsigned SectionID = Sections.size();
180 uint8_t *Addr = MemMgr->allocateDataSection(TotalSize, sizeof(void*),
181 SectionID);
182 if (!Addr)
183 report_fatal_error("Unable to allocate memory for common symbols!");
184 uint64_t Offset = 0;
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +0000185 Sections.push_back(SectionEntry(StringRef(), Addr, TotalSize, TotalSize, 0));
Preston Gurdc68dda82012-04-12 20:13:57 +0000186 memset(Addr, 0, TotalSize);
187
188 DEBUG(dbgs() << "emitCommonSection SectionID: " << SectionID
189 << " new addr: " << format("%p", Addr)
190 << " DataSize: " << TotalSize
191 << "\n");
192
193 // Assign the address of each symbol
Eli Benderskyc201e6e2012-05-01 10:41:12 +0000194 for (CommonSymbolMap::const_iterator it = CommonSymbols.begin(),
195 itEnd = CommonSymbols.end(); it != itEnd; it++) {
Tim Northoverf00677d2012-10-29 10:47:04 +0000196 uint64_t Size = it->second.first;
197 uint64_t Align = it->second.second;
Preston Gurdc68dda82012-04-12 20:13:57 +0000198 StringRef Name;
199 it->first.getName(Name);
Tim Northoverf00677d2012-10-29 10:47:04 +0000200 if (Align) {
201 // This symbol has an alignment requirement.
202 uint64_t AlignOffset = OffsetToAlignment((uint64_t)Addr, Align);
203 Addr += AlignOffset;
204 Offset += AlignOffset;
205 DEBUG(dbgs() << "Allocating common symbol " << Name << " address " <<
206 format("0x%x\n", Addr));
207 }
Preston Gurd689ff9c2012-04-16 22:12:58 +0000208 Obj.updateSymbolAddress(it->first, (uint64_t)Addr);
Eli Benderskyc201e6e2012-05-01 10:41:12 +0000209 SymbolTable[Name.data()] = SymbolLoc(SectionID, Offset);
Preston Gurdc68dda82012-04-12 20:13:57 +0000210 Offset += Size;
211 Addr += Size;
212 }
Preston Gurdc68dda82012-04-12 20:13:57 +0000213}
214
Preston Gurd689ff9c2012-04-16 22:12:58 +0000215unsigned RuntimeDyldImpl::emitSection(ObjectImage &Obj,
216 const SectionRef &Section,
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000217 bool IsCode) {
218
219 unsigned StubBufSize = 0,
220 StubSize = getMaxStubSize();
221 error_code err;
222 if (StubSize > 0) {
223 for (relocation_iterator i = Section.begin_relocations(),
Preston Gurdc68dda82012-04-12 20:13:57 +0000224 e = Section.end_relocations(); i != e; i.increment(err), Check(err))
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000225 StubBufSize += StubSize;
226 }
227 StringRef data;
228 uint64_t Alignment64;
229 Check(Section.getContents(data));
230 Check(Section.getAlignment(Alignment64));
231
232 unsigned Alignment = (unsigned)Alignment64 & 0xffffffffL;
Preston Gurdc68dda82012-04-12 20:13:57 +0000233 bool IsRequired;
234 bool IsVirtual;
235 bool IsZeroInit;
236 uint64_t DataSize;
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +0000237 StringRef Name;
Preston Gurdc68dda82012-04-12 20:13:57 +0000238 Check(Section.isRequiredForExecution(IsRequired));
239 Check(Section.isVirtual(IsVirtual));
240 Check(Section.isZeroInit(IsZeroInit));
241 Check(Section.getSize(DataSize));
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +0000242 Check(Section.getName(Name));
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
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +0000270 << " Name: " << Name
Preston Gurdc68dda82012-04-12 20:13:57 +0000271 << " obj addr: " << format("%p", pData)
272 << " new addr: " << format("%p", Addr)
273 << " DataSize: " << DataSize
274 << " StubBufSize: " << StubBufSize
275 << " Allocate: " << Allocate
276 << "\n");
Preston Gurd689ff9c2012-04-16 22:12:58 +0000277 Obj.updateSectionAddress(Section, (uint64_t)Addr);
Preston Gurdc68dda82012-04-12 20:13:57 +0000278 }
279 else {
280 // Even if we didn't load the section, we need to record an entry for it
Eli Bendersky5fe01982012-04-29 12:40:47 +0000281 // to handle later processing (and by 'handle' I mean don't do anything
282 // with these sections).
Preston Gurdc68dda82012-04-12 20:13:57 +0000283 Allocate = 0;
284 Addr = 0;
285 DEBUG(dbgs() << "emitSection SectionID: " << SectionID
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +0000286 << " Name: " << Name
Preston Gurdc68dda82012-04-12 20:13:57 +0000287 << " obj addr: " << format("%p", data.data())
288 << " new addr: 0"
289 << " DataSize: " << DataSize
290 << " StubBufSize: " << StubBufSize
291 << " Allocate: " << Allocate
292 << "\n");
293 }
294
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +0000295 Sections.push_back(SectionEntry(Name, Addr, Allocate, DataSize,
296 (uintptr_t)pData));
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000297 return SectionID;
298}
299
Preston Gurd689ff9c2012-04-16 22:12:58 +0000300unsigned RuntimeDyldImpl::findOrEmitSection(ObjectImage &Obj,
301 const SectionRef &Section,
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000302 bool IsCode,
303 ObjSectionToIDMap &LocalSections) {
304
305 unsigned SectionID = 0;
306 ObjSectionToIDMap::iterator i = LocalSections.find(Section);
307 if (i != LocalSections.end())
308 SectionID = i->second;
309 else {
Preston Gurd689ff9c2012-04-16 22:12:58 +0000310 SectionID = emitSection(Obj, Section, IsCode);
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000311 LocalSections[Section] = SectionID;
312 }
313 return SectionID;
314}
315
Eli Benderskyc201e6e2012-05-01 10:41:12 +0000316void RuntimeDyldImpl::addRelocationForSection(const RelocationEntry &RE,
317 unsigned SectionID) {
318 Relocations[SectionID].push_back(RE);
319}
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000320
Eli Benderskyc201e6e2012-05-01 10:41:12 +0000321void RuntimeDyldImpl::addRelocationForSymbol(const RelocationEntry &RE,
322 StringRef SymbolName) {
323 // Relocation by symbol. If the symbol is found in the global symbol table,
324 // create an appropriate section relocation. Otherwise, add it to
325 // ExternalSymbolRelocations.
326 SymbolTableMap::const_iterator Loc =
327 GlobalSymbolTable.find(SymbolName);
328 if (Loc == GlobalSymbolTable.end()) {
329 ExternalSymbolRelocations[SymbolName].push_back(RE);
Eli Bendersky37bc5a22012-04-30 12:15:58 +0000330 } else {
Eli Benderskyc201e6e2012-05-01 10:41:12 +0000331 // Copy the RE since we want to modify its addend.
332 RelocationEntry RECopy = RE;
333 RECopy.Addend += Loc->second.second;
334 Relocations[Loc->second.first].push_back(RECopy);
Eli Bendersky37bc5a22012-04-30 12:15:58 +0000335 }
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000336}
337
338uint8_t *RuntimeDyldImpl::createStubFunction(uint8_t *Addr) {
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000339 if (Arch == Triple::arm) {
Akira Hatanakab889e0c2012-08-17 21:28:04 +0000340 // TODO: There is only ARM far stub now. We should add the Thumb stub,
341 // and stubs for branches Thumb - ARM and ARM - Thumb.
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000342 uint32_t *StubAddr = (uint32_t*)Addr;
343 *StubAddr = 0xe51ff004; // ldr pc,<label>
344 return (uint8_t*)++StubAddr;
Akira Hatanakab889e0c2012-08-17 21:28:04 +0000345 } else if (Arch == Triple::mipsel) {
346 uint32_t *StubAddr = (uint32_t*)Addr;
347 // 0: 3c190000 lui t9,%hi(addr).
348 // 4: 27390000 addiu t9,t9,%lo(addr).
349 // 8: 03200008 jr t9.
350 // c: 00000000 nop.
351 const unsigned LuiT9Instr = 0x3c190000, AdduiT9Instr = 0x27390000;
352 const unsigned JrT9Instr = 0x03200008, NopInstr = 0x0;
353
354 *StubAddr = LuiT9Instr;
355 StubAddr++;
356 *StubAddr = AdduiT9Instr;
357 StubAddr++;
358 *StubAddr = JrT9Instr;
359 StubAddr++;
360 *StubAddr = NopInstr;
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000361 return Addr;
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +0000362 } else if (Arch == Triple::ppc64) {
363 // PowerPC64 stub: the address points to a function descriptor
364 // instead of the function itself. Load the function address
365 // on r11 and sets it to control register. Also loads the function
366 // TOC in r2 and environment pointer to r11.
367 writeInt32BE(Addr, 0x3D800000); // lis r12, highest(addr)
368 writeInt32BE(Addr+4, 0x618C0000); // ori r12, higher(addr)
369 writeInt32BE(Addr+8, 0x798C07C6); // sldi r12, r12, 32
370 writeInt32BE(Addr+12, 0x658C0000); // oris r12, r12, h(addr)
371 writeInt32BE(Addr+16, 0x618C0000); // ori r12, r12, l(addr)
372 writeInt32BE(Addr+20, 0xF8410028); // std r2, 40(r1)
373 writeInt32BE(Addr+24, 0xE96C0000); // ld r11, 0(r12)
374 writeInt32BE(Addr+28, 0xE84C0008); // ld r2, 0(r12)
375 writeInt32BE(Addr+32, 0x7D6903A6); // mtctr r11
376 writeInt32BE(Addr+36, 0xE96C0010); // ld r11, 16(r2)
377 writeInt32BE(Addr+40, 0x4E800420); // bctr
378
379 return Addr;
Akira Hatanakab889e0c2012-08-17 21:28:04 +0000380 }
381 return Addr;
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000382}
383
384// Assign an address to a symbol name and resolve all the relocations
385// associated with it.
386void RuntimeDyldImpl::reassignSectionAddress(unsigned SectionID,
387 uint64_t Addr) {
388 // The address to use for relocation resolution is not
389 // the address of the local section buffer. We must be doing
390 // a remote execution environment of some sort. Re-apply any
391 // relocations referencing this section with the given address.
392 //
393 // Addr is a uint64_t because we can't assume the pointer width
394 // of the target is the same as that of the host. Just use a generic
395 // "big enough" type.
396 Sections[SectionID].LoadAddress = Addr;
397 DEBUG(dbgs() << "Resolving relocations Section #" << SectionID
398 << "\t" << format("%p", (uint8_t *)Addr)
399 << "\n");
400 resolveRelocationList(Relocations[SectionID], Addr);
401}
402
403void RuntimeDyldImpl::resolveRelocationEntry(const RelocationEntry &RE,
404 uint64_t Value) {
Eric Christophera7ca3c22012-10-12 02:04:47 +0000405 // Ignore relocations for sections that were not loaded
406 if (Sections[RE.SectionID].Address != 0) {
407 uint8_t *Target = Sections[RE.SectionID].Address + RE.Offset;
408 DEBUG(dbgs() << "\tSectionID: " << RE.SectionID
409 << " + " << RE.Offset << " (" << format("%p", Target) << ")"
410 << " RelType: " << RE.RelType
411 << " Addend: " << RE.Addend
412 << "\n");
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000413
Eric Christophera7ca3c22012-10-12 02:04:47 +0000414 resolveRelocation(Target, Sections[RE.SectionID].LoadAddress + RE.Offset,
415 Value, RE.RelType, RE.Addend);
Preston Gurdc68dda82012-04-12 20:13:57 +0000416 }
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000417}
418
419void RuntimeDyldImpl::resolveRelocationList(const RelocationList &Relocs,
420 uint64_t Value) {
421 for (unsigned i = 0, e = Relocs.size(); i != e; ++i) {
422 resolveRelocationEntry(Relocs[i], Value);
423 }
424}
425
Eli Bendersky37bc5a22012-04-30 12:15:58 +0000426void RuntimeDyldImpl::resolveExternalSymbols() {
427 StringMap<RelocationList>::iterator i = ExternalSymbolRelocations.begin(),
428 e = ExternalSymbolRelocations.end();
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000429 for (; i != e; i++) {
430 StringRef Name = i->first();
431 RelocationList &Relocs = i->second;
Eli Benderskyd98c9e92012-05-01 06:58:59 +0000432 SymbolTableMap::const_iterator Loc = GlobalSymbolTable.find(Name);
433 if (Loc == GlobalSymbolTable.end()) {
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000434 // This is an external symbol, try to get it address from
435 // MemoryManager.
436 uint8_t *Addr = (uint8_t*) MemMgr->getPointerToNamedFunction(Name.data(),
437 true);
438 DEBUG(dbgs() << "Resolving relocations Name: " << Name
439 << "\t" << format("%p", Addr)
440 << "\n");
441 resolveRelocationList(Relocs, (uintptr_t)Addr);
442 } else {
Eli Bendersky37bc5a22012-04-30 12:15:58 +0000443 report_fatal_error("Expected external symbol");
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000444 }
445 }
446}
447
448
Jim Grosbach6e563312011-03-21 22:15:52 +0000449//===----------------------------------------------------------------------===//
450// RuntimeDyld class implementation
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000451RuntimeDyld::RuntimeDyld(RTDyldMemoryManager *mm) {
452 Dyld = 0;
453 MM = mm;
Jim Grosbach6e563312011-03-21 22:15:52 +0000454}
455
456RuntimeDyld::~RuntimeDyld() {
457 delete Dyld;
458}
459
Andrew Kaylor3f23cef2012-10-02 21:18:39 +0000460ObjectImage *RuntimeDyld::loadObject(ObjectBuffer *InputBuffer) {
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000461 if (!Dyld) {
Eli Benderskya66a1852012-01-16 08:56:09 +0000462 sys::LLVMFileType type = sys::IdentifyFileType(
463 InputBuffer->getBufferStart(),
464 static_cast<unsigned>(InputBuffer->getBufferSize()));
465 switch (type) {
466 case sys::ELF_Relocatable_FileType:
467 case sys::ELF_Executable_FileType:
468 case sys::ELF_SharedObject_FileType:
469 case sys::ELF_Core_FileType:
470 Dyld = new RuntimeDyldELF(MM);
471 break;
472 case sys::Mach_O_Object_FileType:
473 case sys::Mach_O_Executable_FileType:
474 case sys::Mach_O_FixedVirtualMemorySharedLib_FileType:
475 case sys::Mach_O_Core_FileType:
476 case sys::Mach_O_PreloadExecutable_FileType:
477 case sys::Mach_O_DynamicallyLinkedSharedLib_FileType:
478 case sys::Mach_O_DynamicLinker_FileType:
479 case sys::Mach_O_Bundle_FileType:
480 case sys::Mach_O_DynamicallyLinkedSharedLibStub_FileType:
481 case sys::Mach_O_DSYMCompanion_FileType:
482 Dyld = new RuntimeDyldMachO(MM);
483 break;
484 case sys::Unknown_FileType:
485 case sys::Bitcode_FileType:
486 case sys::Archive_FileType:
487 case sys::COFF_FileType:
488 report_fatal_error("Incompatible object format!");
489 }
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000490 } else {
Eli Benderskya66a1852012-01-16 08:56:09 +0000491 if (!Dyld->isCompatibleFormat(InputBuffer))
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000492 report_fatal_error("Incompatible object format!");
493 }
494
Jim Grosbach6e563312011-03-21 22:15:52 +0000495 return Dyld->loadObject(InputBuffer);
496}
497
Jim Grosbachb0271052011-04-08 17:31:24 +0000498void *RuntimeDyld::getSymbolAddress(StringRef Name) {
Jim Grosbach6e563312011-03-21 22:15:52 +0000499 return Dyld->getSymbolAddress(Name);
500}
501
Jim Grosbach35ed8422012-09-05 16:50:40 +0000502uint64_t RuntimeDyld::getSymbolLoadAddress(StringRef Name) {
503 return Dyld->getSymbolLoadAddress(Name);
504}
505
Jim Grosbachf8c1c842011-04-12 21:20:41 +0000506void RuntimeDyld::resolveRelocations() {
507 Dyld->resolveRelocations();
508}
509
Jim Grosbach61425c02012-01-16 22:26:39 +0000510void RuntimeDyld::reassignSectionAddress(unsigned SectionID,
511 uint64_t Addr) {
512 Dyld->reassignSectionAddress(SectionID, Addr);
Jim Grosbachf8c1c842011-04-12 21:20:41 +0000513}
514
Jim Grosbache940c1b2012-09-13 21:50:06 +0000515void RuntimeDyld::mapSectionAddress(const void *LocalAddress,
Jim Grosbach020f4e82012-01-16 23:50:55 +0000516 uint64_t TargetAddress) {
517 Dyld->mapSectionAddress(LocalAddress, TargetAddress);
518}
519
Jim Grosbach91dde152011-03-22 18:22:27 +0000520StringRef RuntimeDyld::getErrorString() {
Jim Grosbachb3eecaf2011-03-22 18:19:42 +0000521 return Dyld->getErrorString();
522}
523
Jim Grosbach6e563312011-03-21 22:15:52 +0000524} // end namespace llvm