blob: 7884f46a3781fbd517dc1d1308324b2d1d4da808 [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"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000015#include "llvm/ExecutionEngine/RuntimeDyld.h"
Andrew Kaylor3f23cef2012-10-02 21:18:39 +000016#include "ObjectImageCommon.h"
Eli Bendersky76463fd2012-01-22 07:05:02 +000017#include "RuntimeDyldELF.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000018#include "RuntimeDyldImpl.h"
Eli Bendersky76463fd2012-01-22 07:05:02 +000019#include "RuntimeDyldMachO.h"
Tim Northoverf00677d2012-10-29 10:47:04 +000020#include "llvm/Support/MathExtras.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000021#include "llvm/Support/Path.h"
Eli Bendersky76463fd2012-01-22 07:05:02 +000022
Jim Grosbach6e563312011-03-21 22:15:52 +000023using namespace llvm;
24using namespace llvm::object;
25
Chandler Carruth53c5e7b2011-04-05 23:54:31 +000026// Empty out-of-line virtual destructor as the key function.
27RTDyldMemoryManager::~RTDyldMemoryManager() {}
Danil Malyshevcf852dc2011-07-13 07:57:58 +000028RuntimeDyldImpl::~RuntimeDyldImpl() {}
Chandler Carruth53c5e7b2011-04-05 23:54:31 +000029
Jim Grosbach6e563312011-03-21 22:15:52 +000030namespace llvm {
Jim Grosbach6e563312011-03-21 22:15:52 +000031
Jim Grosbachf8c1c842011-04-12 21:20:41 +000032// Resolve the relocations for all symbols we currently know about.
33void RuntimeDyldImpl::resolveRelocations() {
Preston Gurdc68dda82012-04-12 20:13:57 +000034 // First, resolve relocations associated with external symbols.
Eli Bendersky37bc5a22012-04-30 12:15:58 +000035 resolveExternalSymbols();
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +000036
Jim Grosbach61425c02012-01-16 22:26:39 +000037 // Just iterate over the sections we have and resolve all the relocations
38 // in them. Gross overkill, but it gets the job done.
39 for (int i = 0, e = Sections.size(); i != e; ++i) {
Andrew Kaylor28989882012-11-05 20:57:16 +000040 uint64_t Addr = Sections[i].LoadAddress;
41 DEBUG(dbgs() << "Resolving relocations Section #" << i
42 << "\t" << format("%p", (uint8_t *)Addr)
43 << "\n");
44 resolveRelocationList(Relocations[i], Addr);
Jim Grosbach61425c02012-01-16 22:26:39 +000045 }
Jim Grosbachf8c1c842011-04-12 21:20:41 +000046}
47
Jim Grosbache940c1b2012-09-13 21:50:06 +000048void RuntimeDyldImpl::mapSectionAddress(const void *LocalAddress,
Jim Grosbach020f4e82012-01-16 23:50:55 +000049 uint64_t TargetAddress) {
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +000050 for (unsigned i = 0, e = Sections.size(); i != e; ++i) {
51 if (Sections[i].Address == LocalAddress) {
52 reassignSectionAddress(i, TargetAddress);
53 return;
54 }
55 }
56 llvm_unreachable("Attempting to remap address of unknown section!");
Jim Grosbach020f4e82012-01-16 23:50:55 +000057}
58
Eli Bendersky5fe01982012-04-29 12:40:47 +000059// Subclasses can implement this method to create specialized image instances.
Sylvestre Ledruc8e41c52012-07-23 08:51:15 +000060// The caller owns the pointer that is returned.
Andrew Kaylor3f23cef2012-10-02 21:18:39 +000061ObjectImage *RuntimeDyldImpl::createObjectImage(ObjectBuffer *InputBuffer) {
62 return new ObjectImageCommon(InputBuffer);
Preston Gurd689ff9c2012-04-16 22:12:58 +000063}
64
Andrew Kaylor3f23cef2012-10-02 21:18:39 +000065ObjectImage *RuntimeDyldImpl::loadObject(ObjectBuffer *InputBuffer) {
Preston Gurd689ff9c2012-04-16 22:12:58 +000066 OwningPtr<ObjectImage> obj(createObjectImage(InputBuffer));
Preston Gurdc68dda82012-04-12 20:13:57 +000067 if (!obj)
68 report_fatal_error("Unable to create object image from memory buffer!");
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +000069
70 Arch = (Triple::ArchType)obj->getArch();
71
Eli Benderskyd98c9e92012-05-01 06:58:59 +000072 // Symbols found in this object
73 StringMap<SymbolLoc> LocalSymbols;
74 // Used sections from the object file
75 ObjSectionToIDMap LocalSections;
76
Tim Northoverf00677d2012-10-29 10:47:04 +000077 // Common symbols requiring allocation, with their sizes and alignments
Eli Benderskyd98c9e92012-05-01 06:58:59 +000078 CommonSymbolMap CommonSymbols;
Tim Northoverf00677d2012-10-29 10:47:04 +000079 // Maximum required total memory to allocate all common symbols
Eli Benderskyd98c9e92012-05-01 06:58:59 +000080 uint64_t CommonSize = 0;
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +000081
82 error_code err;
83 // Parse symbols
84 DEBUG(dbgs() << "Parse symbols:\n");
85 for (symbol_iterator i = obj->begin_symbols(), e = obj->end_symbols();
86 i != e; i.increment(err)) {
87 Check(err);
88 object::SymbolRef::Type SymType;
89 StringRef Name;
90 Check(i->getType(SymType));
91 Check(i->getName(Name));
92
Preston Gurdc68dda82012-04-12 20:13:57 +000093 uint32_t flags;
94 Check(i->getFlags(flags));
95
96 bool isCommon = flags & SymbolRef::SF_Common;
97 if (isCommon) {
98 // Add the common symbols to a list. We'll allocate them all below.
Tim Northoverf00677d2012-10-29 10:47:04 +000099 uint64_t Align = getCommonSymbolAlignment(*i);
Preston Gurdc68dda82012-04-12 20:13:57 +0000100 uint64_t Size = 0;
101 Check(i->getSize(Size));
Tim Northoverf00677d2012-10-29 10:47:04 +0000102 CommonSize += Size + Align;
103 CommonSymbols[*i] = CommonSymbolInfo(Size, Align);
Preston Gurdc68dda82012-04-12 20:13:57 +0000104 } else {
105 if (SymType == object::SymbolRef::ST_Function ||
Akira Hatanakab889e0c2012-08-17 21:28:04 +0000106 SymType == object::SymbolRef::ST_Data ||
107 SymType == object::SymbolRef::ST_Unknown) {
Preston Gurdc68dda82012-04-12 20:13:57 +0000108 uint64_t FileOffset;
Eli Bendersky5fe01982012-04-29 12:40:47 +0000109 StringRef SectionData;
Tim Northover78822132012-12-17 17:59:35 +0000110 bool IsCode;
Preston Gurdc68dda82012-04-12 20:13:57 +0000111 section_iterator si = obj->end_sections();
112 Check(i->getFileOffset(FileOffset));
113 Check(i->getSection(si));
114 if (si == obj->end_sections()) continue;
Eli Bendersky5fe01982012-04-29 12:40:47 +0000115 Check(si->getContents(SectionData));
Tim Northover78822132012-12-17 17:59:35 +0000116 Check(si->isText(IsCode));
Preston Gurdc68dda82012-04-12 20:13:57 +0000117 const uint8_t* SymPtr = (const uint8_t*)InputBuffer->getBufferStart() +
118 (uintptr_t)FileOffset;
Eli Bendersky5fe01982012-04-29 12:40:47 +0000119 uintptr_t SectOffset = (uintptr_t)(SymPtr -
120 (const uint8_t*)SectionData.begin());
Tim Northover78822132012-12-17 17:59:35 +0000121 unsigned SectionID = findOrEmitSection(*obj, *si, IsCode, LocalSections);
Preston Gurdc68dda82012-04-12 20:13:57 +0000122 LocalSymbols[Name.data()] = SymbolLoc(SectionID, SectOffset);
123 DEBUG(dbgs() << "\tFileOffset: " << format("%p", (uintptr_t)FileOffset)
124 << " flags: " << flags
125 << " SID: " << SectionID
126 << " Offset: " << format("%p", SectOffset));
Amara Emerson098d6d52012-11-16 11:11:59 +0000127 GlobalSymbolTable[Name] = SymbolLoc(SectionID, SectOffset);
Preston Gurdc68dda82012-04-12 20:13:57 +0000128 }
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000129 }
130 DEBUG(dbgs() << "\tType: " << SymType << " Name: " << Name << "\n");
131 }
132
Preston Gurdc68dda82012-04-12 20:13:57 +0000133 // Allocate common symbols
134 if (CommonSize != 0)
Preston Gurd689ff9c2012-04-16 22:12:58 +0000135 emitCommonSymbols(*obj, CommonSymbols, CommonSize, LocalSymbols);
Preston Gurdc68dda82012-04-12 20:13:57 +0000136
Eli Bendersky5fe01982012-04-29 12:40:47 +0000137 // Parse and process relocations
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000138 DEBUG(dbgs() << "Parse relocations:\n");
139 for (section_iterator si = obj->begin_sections(),
140 se = obj->end_sections(); si != se; si.increment(err)) {
141 Check(err);
142 bool isFirstRelocation = true;
143 unsigned SectionID = 0;
144 StubMap Stubs;
145
146 for (relocation_iterator i = si->begin_relocations(),
147 e = si->end_relocations(); i != e; i.increment(err)) {
148 Check(err);
149
Eli Bendersky5fe01982012-04-29 12:40:47 +0000150 // If it's the first relocation in this section, find its SectionID
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000151 if (isFirstRelocation) {
Preston Gurd689ff9c2012-04-16 22:12:58 +0000152 SectionID = findOrEmitSection(*obj, *si, true, LocalSections);
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000153 DEBUG(dbgs() << "\tSectionID: " << SectionID << "\n");
154 isFirstRelocation = false;
155 }
156
Rafael Espindolaefa91f62013-04-29 14:44:23 +0000157 processRelocationRef(SectionID, i, *obj, LocalSections, LocalSymbols,
158 Stubs);
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000159 }
160 }
Preston Gurd689ff9c2012-04-16 22:12:58 +0000161
Andrew Kaylor3f23cef2012-10-02 21:18:39 +0000162 return obj.take();
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000163}
164
Eli Benderskyc201e6e2012-05-01 10:41:12 +0000165void RuntimeDyldImpl::emitCommonSymbols(ObjectImage &Obj,
166 const CommonSymbolMap &CommonSymbols,
167 uint64_t TotalSize,
168 SymbolTableMap &SymbolTable) {
Preston Gurdc68dda82012-04-12 20:13:57 +0000169 // Allocate memory for the section
170 unsigned SectionID = Sections.size();
171 uint8_t *Addr = MemMgr->allocateDataSection(TotalSize, sizeof(void*),
Andrew Kaylor53608a32012-11-15 23:50:01 +0000172 SectionID, false);
Preston Gurdc68dda82012-04-12 20:13:57 +0000173 if (!Addr)
174 report_fatal_error("Unable to allocate memory for common symbols!");
175 uint64_t Offset = 0;
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +0000176 Sections.push_back(SectionEntry(StringRef(), Addr, TotalSize, TotalSize, 0));
Preston Gurdc68dda82012-04-12 20:13:57 +0000177 memset(Addr, 0, TotalSize);
178
179 DEBUG(dbgs() << "emitCommonSection SectionID: " << SectionID
180 << " new addr: " << format("%p", Addr)
181 << " DataSize: " << TotalSize
182 << "\n");
183
184 // Assign the address of each symbol
Eli Benderskyc201e6e2012-05-01 10:41:12 +0000185 for (CommonSymbolMap::const_iterator it = CommonSymbols.begin(),
186 itEnd = CommonSymbols.end(); it != itEnd; it++) {
Tim Northoverf00677d2012-10-29 10:47:04 +0000187 uint64_t Size = it->second.first;
188 uint64_t Align = it->second.second;
Preston Gurdc68dda82012-04-12 20:13:57 +0000189 StringRef Name;
190 it->first.getName(Name);
Tim Northoverf00677d2012-10-29 10:47:04 +0000191 if (Align) {
192 // This symbol has an alignment requirement.
193 uint64_t AlignOffset = OffsetToAlignment((uint64_t)Addr, Align);
194 Addr += AlignOffset;
195 Offset += AlignOffset;
196 DEBUG(dbgs() << "Allocating common symbol " << Name << " address " <<
Andrew Kaylor5fc8c7c2012-11-01 19:49:21 +0000197 format("%p\n", Addr));
Tim Northoverf00677d2012-10-29 10:47:04 +0000198 }
Preston Gurd689ff9c2012-04-16 22:12:58 +0000199 Obj.updateSymbolAddress(it->first, (uint64_t)Addr);
Eli Benderskyc201e6e2012-05-01 10:41:12 +0000200 SymbolTable[Name.data()] = SymbolLoc(SectionID, Offset);
Preston Gurdc68dda82012-04-12 20:13:57 +0000201 Offset += Size;
202 Addr += Size;
203 }
Preston Gurdc68dda82012-04-12 20:13:57 +0000204}
205
Preston Gurd689ff9c2012-04-16 22:12:58 +0000206unsigned RuntimeDyldImpl::emitSection(ObjectImage &Obj,
207 const SectionRef &Section,
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000208 bool IsCode) {
209
210 unsigned StubBufSize = 0,
211 StubSize = getMaxStubSize();
212 error_code err;
213 if (StubSize > 0) {
214 for (relocation_iterator i = Section.begin_relocations(),
Preston Gurdc68dda82012-04-12 20:13:57 +0000215 e = Section.end_relocations(); i != e; i.increment(err), Check(err))
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000216 StubBufSize += StubSize;
217 }
218 StringRef data;
219 uint64_t Alignment64;
220 Check(Section.getContents(data));
221 Check(Section.getAlignment(Alignment64));
222
223 unsigned Alignment = (unsigned)Alignment64 & 0xffffffffL;
Preston Gurdc68dda82012-04-12 20:13:57 +0000224 bool IsRequired;
225 bool IsVirtual;
226 bool IsZeroInit;
Andrew Kaylor53608a32012-11-15 23:50:01 +0000227 bool IsReadOnly;
Preston Gurdc68dda82012-04-12 20:13:57 +0000228 uint64_t DataSize;
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +0000229 StringRef Name;
Preston Gurdc68dda82012-04-12 20:13:57 +0000230 Check(Section.isRequiredForExecution(IsRequired));
231 Check(Section.isVirtual(IsVirtual));
232 Check(Section.isZeroInit(IsZeroInit));
Andrew Kaylor53608a32012-11-15 23:50:01 +0000233 Check(Section.isReadOnlyData(IsReadOnly));
Preston Gurdc68dda82012-04-12 20:13:57 +0000234 Check(Section.getSize(DataSize));
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +0000235 Check(Section.getName(Name));
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000236
Preston Gurdc68dda82012-04-12 20:13:57 +0000237 unsigned Allocate;
238 unsigned SectionID = Sections.size();
239 uint8_t *Addr;
240 const char *pData = 0;
241
242 // Some sections, such as debug info, don't need to be loaded for execution.
243 // Leave those where they are.
244 if (IsRequired) {
245 Allocate = DataSize + StubBufSize;
246 Addr = IsCode
247 ? MemMgr->allocateCodeSection(Allocate, Alignment, SectionID)
Andrew Kaylor53608a32012-11-15 23:50:01 +0000248 : MemMgr->allocateDataSection(Allocate, Alignment, SectionID, IsReadOnly);
Preston Gurdc68dda82012-04-12 20:13:57 +0000249 if (!Addr)
250 report_fatal_error("Unable to allocate section memory!");
251
252 // Virtual sections have no data in the object image, so leave pData = 0
253 if (!IsVirtual)
254 pData = data.data();
255
256 // Zero-initialize or copy the data from the image
257 if (IsZeroInit || IsVirtual)
258 memset(Addr, 0, DataSize);
259 else
260 memcpy(Addr, pData, DataSize);
261
262 DEBUG(dbgs() << "emitSection SectionID: " << SectionID
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +0000263 << " Name: " << Name
Preston Gurdc68dda82012-04-12 20:13:57 +0000264 << " obj addr: " << format("%p", pData)
265 << " new addr: " << format("%p", Addr)
266 << " DataSize: " << DataSize
267 << " StubBufSize: " << StubBufSize
268 << " Allocate: " << Allocate
269 << "\n");
Preston Gurd689ff9c2012-04-16 22:12:58 +0000270 Obj.updateSectionAddress(Section, (uint64_t)Addr);
Preston Gurdc68dda82012-04-12 20:13:57 +0000271 }
272 else {
273 // Even if we didn't load the section, we need to record an entry for it
Eli Bendersky5fe01982012-04-29 12:40:47 +0000274 // to handle later processing (and by 'handle' I mean don't do anything
275 // with these sections).
Preston Gurdc68dda82012-04-12 20:13:57 +0000276 Allocate = 0;
277 Addr = 0;
278 DEBUG(dbgs() << "emitSection SectionID: " << SectionID
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +0000279 << " Name: " << Name
Preston Gurdc68dda82012-04-12 20:13:57 +0000280 << " obj addr: " << format("%p", data.data())
281 << " new addr: 0"
282 << " DataSize: " << DataSize
283 << " StubBufSize: " << StubBufSize
284 << " Allocate: " << Allocate
285 << "\n");
286 }
287
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +0000288 Sections.push_back(SectionEntry(Name, Addr, Allocate, DataSize,
289 (uintptr_t)pData));
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000290 return SectionID;
291}
292
Preston Gurd689ff9c2012-04-16 22:12:58 +0000293unsigned RuntimeDyldImpl::findOrEmitSection(ObjectImage &Obj,
294 const SectionRef &Section,
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000295 bool IsCode,
296 ObjSectionToIDMap &LocalSections) {
297
298 unsigned SectionID = 0;
299 ObjSectionToIDMap::iterator i = LocalSections.find(Section);
300 if (i != LocalSections.end())
301 SectionID = i->second;
302 else {
Preston Gurd689ff9c2012-04-16 22:12:58 +0000303 SectionID = emitSection(Obj, Section, IsCode);
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000304 LocalSections[Section] = SectionID;
305 }
306 return SectionID;
307}
308
Eli Benderskyc201e6e2012-05-01 10:41:12 +0000309void RuntimeDyldImpl::addRelocationForSection(const RelocationEntry &RE,
310 unsigned SectionID) {
311 Relocations[SectionID].push_back(RE);
312}
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000313
Eli Benderskyc201e6e2012-05-01 10:41:12 +0000314void RuntimeDyldImpl::addRelocationForSymbol(const RelocationEntry &RE,
315 StringRef SymbolName) {
316 // Relocation by symbol. If the symbol is found in the global symbol table,
317 // create an appropriate section relocation. Otherwise, add it to
318 // ExternalSymbolRelocations.
319 SymbolTableMap::const_iterator Loc =
320 GlobalSymbolTable.find(SymbolName);
321 if (Loc == GlobalSymbolTable.end()) {
322 ExternalSymbolRelocations[SymbolName].push_back(RE);
Eli Bendersky37bc5a22012-04-30 12:15:58 +0000323 } else {
Eli Benderskyc201e6e2012-05-01 10:41:12 +0000324 // Copy the RE since we want to modify its addend.
325 RelocationEntry RECopy = RE;
326 RECopy.Addend += Loc->second.second;
327 Relocations[Loc->second.first].push_back(RECopy);
Eli Bendersky37bc5a22012-04-30 12:15:58 +0000328 }
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000329}
330
331uint8_t *RuntimeDyldImpl::createStubFunction(uint8_t *Addr) {
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000332 if (Arch == Triple::arm) {
Akira Hatanakab889e0c2012-08-17 21:28:04 +0000333 // TODO: There is only ARM far stub now. We should add the Thumb stub,
334 // and stubs for branches Thumb - ARM and ARM - Thumb.
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000335 uint32_t *StubAddr = (uint32_t*)Addr;
336 *StubAddr = 0xe51ff004; // ldr pc,<label>
337 return (uint8_t*)++StubAddr;
NAKAMURA Takumi4af1ca52012-12-04 00:08:14 +0000338 } else if (Arch == Triple::mipsel || Arch == Triple::mips) {
Akira Hatanakab889e0c2012-08-17 21:28:04 +0000339 uint32_t *StubAddr = (uint32_t*)Addr;
340 // 0: 3c190000 lui t9,%hi(addr).
341 // 4: 27390000 addiu t9,t9,%lo(addr).
342 // 8: 03200008 jr t9.
343 // c: 00000000 nop.
344 const unsigned LuiT9Instr = 0x3c190000, AdduiT9Instr = 0x27390000;
345 const unsigned JrT9Instr = 0x03200008, NopInstr = 0x0;
346
347 *StubAddr = LuiT9Instr;
348 StubAddr++;
349 *StubAddr = AdduiT9Instr;
350 StubAddr++;
351 *StubAddr = JrT9Instr;
352 StubAddr++;
353 *StubAddr = NopInstr;
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000354 return Addr;
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +0000355 } else if (Arch == Triple::ppc64) {
356 // PowerPC64 stub: the address points to a function descriptor
357 // instead of the function itself. Load the function address
358 // on r11 and sets it to control register. Also loads the function
359 // TOC in r2 and environment pointer to r11.
360 writeInt32BE(Addr, 0x3D800000); // lis r12, highest(addr)
361 writeInt32BE(Addr+4, 0x618C0000); // ori r12, higher(addr)
362 writeInt32BE(Addr+8, 0x798C07C6); // sldi r12, r12, 32
363 writeInt32BE(Addr+12, 0x658C0000); // oris r12, r12, h(addr)
364 writeInt32BE(Addr+16, 0x618C0000); // ori r12, r12, l(addr)
365 writeInt32BE(Addr+20, 0xF8410028); // std r2, 40(r1)
366 writeInt32BE(Addr+24, 0xE96C0000); // ld r11, 0(r12)
367 writeInt32BE(Addr+28, 0xE84C0008); // ld r2, 0(r12)
368 writeInt32BE(Addr+32, 0x7D6903A6); // mtctr r11
369 writeInt32BE(Addr+36, 0xE96C0010); // ld r11, 16(r2)
370 writeInt32BE(Addr+40, 0x4E800420); // bctr
Andrew Kaylor5fc8c7c2012-11-01 19:49:21 +0000371
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +0000372 return Addr;
Akira Hatanakab889e0c2012-08-17 21:28:04 +0000373 }
374 return Addr;
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000375}
376
377// Assign an address to a symbol name and resolve all the relocations
378// associated with it.
379void RuntimeDyldImpl::reassignSectionAddress(unsigned SectionID,
380 uint64_t Addr) {
381 // The address to use for relocation resolution is not
382 // the address of the local section buffer. We must be doing
Andrew Kaylor28989882012-11-05 20:57:16 +0000383 // a remote execution environment of some sort. Relocations can't
384 // be applied until all the sections have been moved. The client must
385 // trigger this with a call to MCJIT::finalize() or
386 // RuntimeDyld::resolveRelocations().
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000387 //
388 // Addr is a uint64_t because we can't assume the pointer width
389 // of the target is the same as that of the host. Just use a generic
390 // "big enough" type.
391 Sections[SectionID].LoadAddress = Addr;
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000392}
393
394void RuntimeDyldImpl::resolveRelocationEntry(const RelocationEntry &RE,
395 uint64_t Value) {
Eric Christophera7ca3c22012-10-12 02:04:47 +0000396 // Ignore relocations for sections that were not loaded
397 if (Sections[RE.SectionID].Address != 0) {
Eric Christophera7ca3c22012-10-12 02:04:47 +0000398 DEBUG(dbgs() << "\tSectionID: " << RE.SectionID
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000399 << " + " << RE.Offset << " ("
400 << format("%p", Sections[RE.SectionID].Address + RE.Offset) << ")"
Eric Christophera7ca3c22012-10-12 02:04:47 +0000401 << " RelType: " << RE.RelType
402 << " Addend: " << RE.Addend
403 << "\n");
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000404
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000405 resolveRelocation(Sections[RE.SectionID], RE.Offset,
Eric Christophera7ca3c22012-10-12 02:04:47 +0000406 Value, RE.RelType, RE.Addend);
Preston Gurdc68dda82012-04-12 20:13:57 +0000407 }
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000408}
409
410void RuntimeDyldImpl::resolveRelocationList(const RelocationList &Relocs,
411 uint64_t Value) {
412 for (unsigned i = 0, e = Relocs.size(); i != e; ++i) {
413 resolveRelocationEntry(Relocs[i], Value);
414 }
415}
416
Eli Bendersky37bc5a22012-04-30 12:15:58 +0000417void RuntimeDyldImpl::resolveExternalSymbols() {
418 StringMap<RelocationList>::iterator i = ExternalSymbolRelocations.begin(),
419 e = ExternalSymbolRelocations.end();
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000420 for (; i != e; i++) {
421 StringRef Name = i->first();
422 RelocationList &Relocs = i->second;
Eli Benderskyd98c9e92012-05-01 06:58:59 +0000423 SymbolTableMap::const_iterator Loc = GlobalSymbolTable.find(Name);
424 if (Loc == GlobalSymbolTable.end()) {
Andrew Kaylor7b170502013-02-20 18:09:21 +0000425 if (Name.size() == 0) {
426 // This is an absolute symbol, use an address of zero.
427 DEBUG(dbgs() << "Resolving absolute relocations." << "\n");
428 resolveRelocationList(Relocs, 0);
Andrew Kaylor29fe1502013-02-20 18:24:34 +0000429 } else {
430 // This is an external symbol, try to get its address from
Andrew Kaylor7b170502013-02-20 18:09:21 +0000431 // MemoryManager.
432 uint8_t *Addr = (uint8_t*) MemMgr->getPointerToNamedFunction(Name.data(),
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000433 true);
Andrew Kaylor7b170502013-02-20 18:09:21 +0000434 DEBUG(dbgs() << "Resolving relocations Name: " << Name
435 << "\t" << format("%p", Addr)
436 << "\n");
437 resolveRelocationList(Relocs, (uintptr_t)Addr);
438 }
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000439 } else {
Eli Bendersky37bc5a22012-04-30 12:15:58 +0000440 report_fatal_error("Expected external symbol");
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000441 }
442 }
443}
444
445
Jim Grosbach6e563312011-03-21 22:15:52 +0000446//===----------------------------------------------------------------------===//
447// RuntimeDyld class implementation
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000448RuntimeDyld::RuntimeDyld(RTDyldMemoryManager *mm) {
Andrew Kaylor53608a32012-11-15 23:50:01 +0000449 // FIXME: There's a potential issue lurking here if a single instance of
450 // RuntimeDyld is used to load multiple objects. The current implementation
451 // associates a single memory manager with a RuntimeDyld instance. Even
452 // though the public class spawns a new 'impl' instance for each load,
453 // they share a single memory manager. This can become a problem when page
454 // permissions are applied.
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000455 Dyld = 0;
456 MM = mm;
Jim Grosbach6e563312011-03-21 22:15:52 +0000457}
458
459RuntimeDyld::~RuntimeDyld() {
460 delete Dyld;
461}
462
Andrew Kaylor3f23cef2012-10-02 21:18:39 +0000463ObjectImage *RuntimeDyld::loadObject(ObjectBuffer *InputBuffer) {
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000464 if (!Dyld) {
Eli Benderskya66a1852012-01-16 08:56:09 +0000465 sys::LLVMFileType type = sys::IdentifyFileType(
466 InputBuffer->getBufferStart(),
467 static_cast<unsigned>(InputBuffer->getBufferSize()));
468 switch (type) {
469 case sys::ELF_Relocatable_FileType:
470 case sys::ELF_Executable_FileType:
471 case sys::ELF_SharedObject_FileType:
472 case sys::ELF_Core_FileType:
473 Dyld = new RuntimeDyldELF(MM);
474 break;
475 case sys::Mach_O_Object_FileType:
476 case sys::Mach_O_Executable_FileType:
477 case sys::Mach_O_FixedVirtualMemorySharedLib_FileType:
478 case sys::Mach_O_Core_FileType:
479 case sys::Mach_O_PreloadExecutable_FileType:
480 case sys::Mach_O_DynamicallyLinkedSharedLib_FileType:
481 case sys::Mach_O_DynamicLinker_FileType:
482 case sys::Mach_O_Bundle_FileType:
483 case sys::Mach_O_DynamicallyLinkedSharedLibStub_FileType:
484 case sys::Mach_O_DSYMCompanion_FileType:
485 Dyld = new RuntimeDyldMachO(MM);
486 break;
487 case sys::Unknown_FileType:
488 case sys::Bitcode_FileType:
489 case sys::Archive_FileType:
490 case sys::COFF_FileType:
491 report_fatal_error("Incompatible object format!");
492 }
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000493 } else {
Eli Benderskya66a1852012-01-16 08:56:09 +0000494 if (!Dyld->isCompatibleFormat(InputBuffer))
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000495 report_fatal_error("Incompatible object format!");
496 }
497
Jim Grosbach6e563312011-03-21 22:15:52 +0000498 return Dyld->loadObject(InputBuffer);
499}
500
Jim Grosbachb0271052011-04-08 17:31:24 +0000501void *RuntimeDyld::getSymbolAddress(StringRef Name) {
Jim Grosbach6e563312011-03-21 22:15:52 +0000502 return Dyld->getSymbolAddress(Name);
503}
504
Jim Grosbach35ed8422012-09-05 16:50:40 +0000505uint64_t RuntimeDyld::getSymbolLoadAddress(StringRef Name) {
506 return Dyld->getSymbolLoadAddress(Name);
507}
508
Jim Grosbachf8c1c842011-04-12 21:20:41 +0000509void RuntimeDyld::resolveRelocations() {
510 Dyld->resolveRelocations();
511}
512
Jim Grosbach61425c02012-01-16 22:26:39 +0000513void RuntimeDyld::reassignSectionAddress(unsigned SectionID,
514 uint64_t Addr) {
515 Dyld->reassignSectionAddress(SectionID, Addr);
Jim Grosbachf8c1c842011-04-12 21:20:41 +0000516}
517
Jim Grosbache940c1b2012-09-13 21:50:06 +0000518void RuntimeDyld::mapSectionAddress(const void *LocalAddress,
Jim Grosbach020f4e82012-01-16 23:50:55 +0000519 uint64_t TargetAddress) {
520 Dyld->mapSectionAddress(LocalAddress, TargetAddress);
521}
522
Jim Grosbach91dde152011-03-22 18:22:27 +0000523StringRef RuntimeDyld::getErrorString() {
Jim Grosbachb3eecaf2011-03-22 18:19:42 +0000524 return Dyld->getErrorString();
525}
526
Jim Grosbach6e563312011-03-21 22:15:52 +0000527} // end namespace llvm