blob: 4118e8ad50954788374ab23b9e4490d1ec548a6f [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) {
Andrew Kaylor28989882012-11-05 20:57:16 +000039 uint64_t Addr = Sections[i].LoadAddress;
40 DEBUG(dbgs() << "Resolving relocations Section #" << i
41 << "\t" << format("%p", (uint8_t *)Addr)
42 << "\n");
43 resolveRelocationList(Relocations[i], Addr);
Jim Grosbach61425c02012-01-16 22:26:39 +000044 }
Jim Grosbachf8c1c842011-04-12 21:20:41 +000045}
46
Jim Grosbache940c1b2012-09-13 21:50:06 +000047void RuntimeDyldImpl::mapSectionAddress(const void *LocalAddress,
Jim Grosbach020f4e82012-01-16 23:50:55 +000048 uint64_t TargetAddress) {
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +000049 for (unsigned i = 0, e = Sections.size(); i != e; ++i) {
50 if (Sections[i].Address == LocalAddress) {
51 reassignSectionAddress(i, TargetAddress);
52 return;
53 }
54 }
55 llvm_unreachable("Attempting to remap address of unknown section!");
Jim Grosbach020f4e82012-01-16 23:50:55 +000056}
57
Eli Bendersky5fe01982012-04-29 12:40:47 +000058// Subclasses can implement this method to create specialized image instances.
Sylvestre Ledruc8e41c52012-07-23 08:51:15 +000059// The caller owns the pointer that is returned.
Andrew Kaylor3f23cef2012-10-02 21:18:39 +000060ObjectImage *RuntimeDyldImpl::createObjectImage(ObjectBuffer *InputBuffer) {
61 return new ObjectImageCommon(InputBuffer);
Preston Gurd689ff9c2012-04-16 22:12:58 +000062}
63
Andrew Kaylor3f23cef2012-10-02 21:18:39 +000064ObjectImage *RuntimeDyldImpl::loadObject(ObjectBuffer *InputBuffer) {
Preston Gurd689ff9c2012-04-16 22:12:58 +000065 OwningPtr<ObjectImage> obj(createObjectImage(InputBuffer));
Preston Gurdc68dda82012-04-12 20:13:57 +000066 if (!obj)
67 report_fatal_error("Unable to create object image from memory buffer!");
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +000068
69 Arch = (Triple::ArchType)obj->getArch();
70
Eli Benderskyd98c9e92012-05-01 06:58:59 +000071 // Symbols found in this object
72 StringMap<SymbolLoc> LocalSymbols;
73 // Used sections from the object file
74 ObjSectionToIDMap LocalSections;
75
Tim Northoverf00677d2012-10-29 10:47:04 +000076 // Common symbols requiring allocation, with their sizes and alignments
Eli Benderskyd98c9e92012-05-01 06:58:59 +000077 CommonSymbolMap CommonSymbols;
Tim Northoverf00677d2012-10-29 10:47:04 +000078 // Maximum required total memory to allocate all common symbols
Eli Benderskyd98c9e92012-05-01 06:58:59 +000079 uint64_t CommonSize = 0;
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +000080
81 error_code err;
82 // Parse symbols
83 DEBUG(dbgs() << "Parse symbols:\n");
84 for (symbol_iterator i = obj->begin_symbols(), e = obj->end_symbols();
85 i != e; i.increment(err)) {
86 Check(err);
87 object::SymbolRef::Type SymType;
88 StringRef Name;
89 Check(i->getType(SymType));
90 Check(i->getName(Name));
91
Preston Gurdc68dda82012-04-12 20:13:57 +000092 uint32_t flags;
93 Check(i->getFlags(flags));
94
95 bool isCommon = flags & SymbolRef::SF_Common;
96 if (isCommon) {
97 // Add the common symbols to a list. We'll allocate them all below.
Tim Northoverf00677d2012-10-29 10:47:04 +000098 uint64_t Align = getCommonSymbolAlignment(*i);
Preston Gurdc68dda82012-04-12 20:13:57 +000099 uint64_t Size = 0;
100 Check(i->getSize(Size));
Tim Northoverf00677d2012-10-29 10:47:04 +0000101 CommonSize += Size + Align;
102 CommonSymbols[*i] = CommonSymbolInfo(Size, Align);
Preston Gurdc68dda82012-04-12 20:13:57 +0000103 } else {
104 if (SymType == object::SymbolRef::ST_Function ||
Akira Hatanakab889e0c2012-08-17 21:28:04 +0000105 SymType == object::SymbolRef::ST_Data ||
106 SymType == object::SymbolRef::ST_Unknown) {
Preston Gurdc68dda82012-04-12 20:13:57 +0000107 uint64_t FileOffset;
Eli Bendersky5fe01982012-04-29 12:40:47 +0000108 StringRef SectionData;
Preston Gurdc68dda82012-04-12 20:13:57 +0000109 section_iterator si = obj->end_sections();
110 Check(i->getFileOffset(FileOffset));
111 Check(i->getSection(si));
112 if (si == obj->end_sections()) continue;
Eli Bendersky5fe01982012-04-29 12:40:47 +0000113 Check(si->getContents(SectionData));
Preston Gurdc68dda82012-04-12 20:13:57 +0000114 const uint8_t* SymPtr = (const uint8_t*)InputBuffer->getBufferStart() +
115 (uintptr_t)FileOffset;
Eli Bendersky5fe01982012-04-29 12:40:47 +0000116 uintptr_t SectOffset = (uintptr_t)(SymPtr -
117 (const uint8_t*)SectionData.begin());
Preston Gurdc68dda82012-04-12 20:13:57 +0000118 unsigned SectionID =
Preston Gurd689ff9c2012-04-16 22:12:58 +0000119 findOrEmitSection(*obj,
120 *si,
Preston Gurdc68dda82012-04-12 20:13:57 +0000121 SymType == object::SymbolRef::ST_Function,
122 LocalSections);
Preston Gurdc68dda82012-04-12 20:13:57 +0000123 LocalSymbols[Name.data()] = SymbolLoc(SectionID, SectOffset);
124 DEBUG(dbgs() << "\tFileOffset: " << format("%p", (uintptr_t)FileOffset)
125 << " flags: " << flags
126 << " SID: " << SectionID
127 << " Offset: " << format("%p", SectOffset));
Eli Bendersky5fe01982012-04-29 12:40:47 +0000128 bool isGlobal = flags & SymbolRef::SF_Global;
Preston Gurdc68dda82012-04-12 20:13:57 +0000129 if (isGlobal)
Eli Benderskyd98c9e92012-05-01 06:58:59 +0000130 GlobalSymbolTable[Name] = SymbolLoc(SectionID, SectOffset);
Preston Gurdc68dda82012-04-12 20:13:57 +0000131 }
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000132 }
133 DEBUG(dbgs() << "\tType: " << SymType << " Name: " << Name << "\n");
134 }
135
Preston Gurdc68dda82012-04-12 20:13:57 +0000136 // Allocate common symbols
137 if (CommonSize != 0)
Preston Gurd689ff9c2012-04-16 22:12:58 +0000138 emitCommonSymbols(*obj, CommonSymbols, CommonSize, LocalSymbols);
Preston Gurdc68dda82012-04-12 20:13:57 +0000139
Eli Bendersky5fe01982012-04-29 12:40:47 +0000140 // Parse and process relocations
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000141 DEBUG(dbgs() << "Parse relocations:\n");
142 for (section_iterator si = obj->begin_sections(),
143 se = obj->end_sections(); si != se; si.increment(err)) {
144 Check(err);
145 bool isFirstRelocation = true;
146 unsigned SectionID = 0;
147 StubMap Stubs;
148
149 for (relocation_iterator i = si->begin_relocations(),
150 e = si->end_relocations(); i != e; i.increment(err)) {
151 Check(err);
152
Eli Bendersky5fe01982012-04-29 12:40:47 +0000153 // If it's the first relocation in this section, find its SectionID
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000154 if (isFirstRelocation) {
Preston Gurd689ff9c2012-04-16 22:12:58 +0000155 SectionID = findOrEmitSection(*obj, *si, true, LocalSections);
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000156 DEBUG(dbgs() << "\tSectionID: " << SectionID << "\n");
157 isFirstRelocation = false;
158 }
159
160 ObjRelocationInfo RI;
161 RI.SectionID = SectionID;
162 Check(i->getAdditionalInfo(RI.AdditionalInfo));
163 Check(i->getOffset(RI.Offset));
164 Check(i->getSymbol(RI.Symbol));
165 Check(i->getType(RI.Type));
166
167 DEBUG(dbgs() << "\t\tAddend: " << RI.AdditionalInfo
168 << " Offset: " << format("%p", (uintptr_t)RI.Offset)
169 << " Type: " << (uint32_t)(RI.Type & 0xffffffffL)
170 << "\n");
171 processRelocationRef(RI, *obj, LocalSections, LocalSymbols, Stubs);
172 }
173 }
Preston Gurd689ff9c2012-04-16 22:12:58 +0000174
Andrew Kaylor3f23cef2012-10-02 21:18:39 +0000175 return obj.take();
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000176}
177
Eli Benderskyc201e6e2012-05-01 10:41:12 +0000178void RuntimeDyldImpl::emitCommonSymbols(ObjectImage &Obj,
179 const CommonSymbolMap &CommonSymbols,
180 uint64_t TotalSize,
181 SymbolTableMap &SymbolTable) {
Preston Gurdc68dda82012-04-12 20:13:57 +0000182 // Allocate memory for the section
183 unsigned SectionID = Sections.size();
184 uint8_t *Addr = MemMgr->allocateDataSection(TotalSize, sizeof(void*),
Andrew Kaylor53608a32012-11-15 23:50:01 +0000185 SectionID, false);
Preston Gurdc68dda82012-04-12 20:13:57 +0000186 if (!Addr)
187 report_fatal_error("Unable to allocate memory for common symbols!");
188 uint64_t Offset = 0;
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +0000189 Sections.push_back(SectionEntry(StringRef(), Addr, TotalSize, TotalSize, 0));
Preston Gurdc68dda82012-04-12 20:13:57 +0000190 memset(Addr, 0, TotalSize);
191
192 DEBUG(dbgs() << "emitCommonSection SectionID: " << SectionID
193 << " new addr: " << format("%p", Addr)
194 << " DataSize: " << TotalSize
195 << "\n");
196
197 // Assign the address of each symbol
Eli Benderskyc201e6e2012-05-01 10:41:12 +0000198 for (CommonSymbolMap::const_iterator it = CommonSymbols.begin(),
199 itEnd = CommonSymbols.end(); it != itEnd; it++) {
Tim Northoverf00677d2012-10-29 10:47:04 +0000200 uint64_t Size = it->second.first;
201 uint64_t Align = it->second.second;
Preston Gurdc68dda82012-04-12 20:13:57 +0000202 StringRef Name;
203 it->first.getName(Name);
Tim Northoverf00677d2012-10-29 10:47:04 +0000204 if (Align) {
205 // This symbol has an alignment requirement.
206 uint64_t AlignOffset = OffsetToAlignment((uint64_t)Addr, Align);
207 Addr += AlignOffset;
208 Offset += AlignOffset;
209 DEBUG(dbgs() << "Allocating common symbol " << Name << " address " <<
Andrew Kaylor5fc8c7c2012-11-01 19:49:21 +0000210 format("%p\n", Addr));
Tim Northoverf00677d2012-10-29 10:47:04 +0000211 }
Preston Gurd689ff9c2012-04-16 22:12:58 +0000212 Obj.updateSymbolAddress(it->first, (uint64_t)Addr);
Eli Benderskyc201e6e2012-05-01 10:41:12 +0000213 SymbolTable[Name.data()] = SymbolLoc(SectionID, Offset);
Preston Gurdc68dda82012-04-12 20:13:57 +0000214 Offset += Size;
215 Addr += Size;
216 }
Preston Gurdc68dda82012-04-12 20:13:57 +0000217}
218
Preston Gurd689ff9c2012-04-16 22:12:58 +0000219unsigned RuntimeDyldImpl::emitSection(ObjectImage &Obj,
220 const SectionRef &Section,
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000221 bool IsCode) {
222
223 unsigned StubBufSize = 0,
224 StubSize = getMaxStubSize();
225 error_code err;
226 if (StubSize > 0) {
227 for (relocation_iterator i = Section.begin_relocations(),
Preston Gurdc68dda82012-04-12 20:13:57 +0000228 e = Section.end_relocations(); i != e; i.increment(err), Check(err))
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000229 StubBufSize += StubSize;
230 }
231 StringRef data;
232 uint64_t Alignment64;
233 Check(Section.getContents(data));
234 Check(Section.getAlignment(Alignment64));
235
236 unsigned Alignment = (unsigned)Alignment64 & 0xffffffffL;
Preston Gurdc68dda82012-04-12 20:13:57 +0000237 bool IsRequired;
238 bool IsVirtual;
239 bool IsZeroInit;
Andrew Kaylor53608a32012-11-15 23:50:01 +0000240 bool IsReadOnly;
Preston Gurdc68dda82012-04-12 20:13:57 +0000241 uint64_t DataSize;
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +0000242 StringRef Name;
Preston Gurdc68dda82012-04-12 20:13:57 +0000243 Check(Section.isRequiredForExecution(IsRequired));
244 Check(Section.isVirtual(IsVirtual));
245 Check(Section.isZeroInit(IsZeroInit));
Andrew Kaylor53608a32012-11-15 23:50:01 +0000246 Check(Section.isReadOnlyData(IsReadOnly));
Preston Gurdc68dda82012-04-12 20:13:57 +0000247 Check(Section.getSize(DataSize));
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +0000248 Check(Section.getName(Name));
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000249
Preston Gurdc68dda82012-04-12 20:13:57 +0000250 unsigned Allocate;
251 unsigned SectionID = Sections.size();
252 uint8_t *Addr;
253 const char *pData = 0;
254
255 // Some sections, such as debug info, don't need to be loaded for execution.
256 // Leave those where they are.
257 if (IsRequired) {
258 Allocate = DataSize + StubBufSize;
259 Addr = IsCode
260 ? MemMgr->allocateCodeSection(Allocate, Alignment, SectionID)
Andrew Kaylor53608a32012-11-15 23:50:01 +0000261 : MemMgr->allocateDataSection(Allocate, Alignment, SectionID, IsReadOnly);
Preston Gurdc68dda82012-04-12 20:13:57 +0000262 if (!Addr)
263 report_fatal_error("Unable to allocate section memory!");
264
265 // Virtual sections have no data in the object image, so leave pData = 0
266 if (!IsVirtual)
267 pData = data.data();
268
269 // Zero-initialize or copy the data from the image
270 if (IsZeroInit || IsVirtual)
271 memset(Addr, 0, DataSize);
272 else
273 memcpy(Addr, pData, DataSize);
274
275 DEBUG(dbgs() << "emitSection SectionID: " << SectionID
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +0000276 << " Name: " << Name
Preston Gurdc68dda82012-04-12 20:13:57 +0000277 << " obj addr: " << format("%p", pData)
278 << " new addr: " << format("%p", Addr)
279 << " DataSize: " << DataSize
280 << " StubBufSize: " << StubBufSize
281 << " Allocate: " << Allocate
282 << "\n");
Preston Gurd689ff9c2012-04-16 22:12:58 +0000283 Obj.updateSectionAddress(Section, (uint64_t)Addr);
Preston Gurdc68dda82012-04-12 20:13:57 +0000284 }
285 else {
286 // Even if we didn't load the section, we need to record an entry for it
Eli Bendersky5fe01982012-04-29 12:40:47 +0000287 // to handle later processing (and by 'handle' I mean don't do anything
288 // with these sections).
Preston Gurdc68dda82012-04-12 20:13:57 +0000289 Allocate = 0;
290 Addr = 0;
291 DEBUG(dbgs() << "emitSection SectionID: " << SectionID
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +0000292 << " Name: " << Name
Preston Gurdc68dda82012-04-12 20:13:57 +0000293 << " obj addr: " << format("%p", data.data())
294 << " new addr: 0"
295 << " DataSize: " << DataSize
296 << " StubBufSize: " << StubBufSize
297 << " Allocate: " << Allocate
298 << "\n");
299 }
300
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +0000301 Sections.push_back(SectionEntry(Name, Addr, Allocate, DataSize,
302 (uintptr_t)pData));
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000303 return SectionID;
304}
305
Preston Gurd689ff9c2012-04-16 22:12:58 +0000306unsigned RuntimeDyldImpl::findOrEmitSection(ObjectImage &Obj,
307 const SectionRef &Section,
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000308 bool IsCode,
309 ObjSectionToIDMap &LocalSections) {
310
311 unsigned SectionID = 0;
312 ObjSectionToIDMap::iterator i = LocalSections.find(Section);
313 if (i != LocalSections.end())
314 SectionID = i->second;
315 else {
Preston Gurd689ff9c2012-04-16 22:12:58 +0000316 SectionID = emitSection(Obj, Section, IsCode);
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000317 LocalSections[Section] = SectionID;
318 }
319 return SectionID;
320}
321
Eli Benderskyc201e6e2012-05-01 10:41:12 +0000322void RuntimeDyldImpl::addRelocationForSection(const RelocationEntry &RE,
323 unsigned SectionID) {
324 Relocations[SectionID].push_back(RE);
325}
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000326
Eli Benderskyc201e6e2012-05-01 10:41:12 +0000327void RuntimeDyldImpl::addRelocationForSymbol(const RelocationEntry &RE,
328 StringRef SymbolName) {
329 // Relocation by symbol. If the symbol is found in the global symbol table,
330 // create an appropriate section relocation. Otherwise, add it to
331 // ExternalSymbolRelocations.
332 SymbolTableMap::const_iterator Loc =
333 GlobalSymbolTable.find(SymbolName);
334 if (Loc == GlobalSymbolTable.end()) {
335 ExternalSymbolRelocations[SymbolName].push_back(RE);
Eli Bendersky37bc5a22012-04-30 12:15:58 +0000336 } else {
Eli Benderskyc201e6e2012-05-01 10:41:12 +0000337 // Copy the RE since we want to modify its addend.
338 RelocationEntry RECopy = RE;
339 RECopy.Addend += Loc->second.second;
340 Relocations[Loc->second.first].push_back(RECopy);
Eli Bendersky37bc5a22012-04-30 12:15:58 +0000341 }
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000342}
343
344uint8_t *RuntimeDyldImpl::createStubFunction(uint8_t *Addr) {
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000345 if (Arch == Triple::arm) {
Akira Hatanakab889e0c2012-08-17 21:28:04 +0000346 // TODO: There is only ARM far stub now. We should add the Thumb stub,
347 // and stubs for branches Thumb - ARM and ARM - Thumb.
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000348 uint32_t *StubAddr = (uint32_t*)Addr;
349 *StubAddr = 0xe51ff004; // ldr pc,<label>
350 return (uint8_t*)++StubAddr;
Akira Hatanakab889e0c2012-08-17 21:28:04 +0000351 } else if (Arch == Triple::mipsel) {
352 uint32_t *StubAddr = (uint32_t*)Addr;
353 // 0: 3c190000 lui t9,%hi(addr).
354 // 4: 27390000 addiu t9,t9,%lo(addr).
355 // 8: 03200008 jr t9.
356 // c: 00000000 nop.
357 const unsigned LuiT9Instr = 0x3c190000, AdduiT9Instr = 0x27390000;
358 const unsigned JrT9Instr = 0x03200008, NopInstr = 0x0;
359
360 *StubAddr = LuiT9Instr;
361 StubAddr++;
362 *StubAddr = AdduiT9Instr;
363 StubAddr++;
364 *StubAddr = JrT9Instr;
365 StubAddr++;
366 *StubAddr = NopInstr;
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000367 return Addr;
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +0000368 } else if (Arch == Triple::ppc64) {
369 // PowerPC64 stub: the address points to a function descriptor
370 // instead of the function itself. Load the function address
371 // on r11 and sets it to control register. Also loads the function
372 // TOC in r2 and environment pointer to r11.
373 writeInt32BE(Addr, 0x3D800000); // lis r12, highest(addr)
374 writeInt32BE(Addr+4, 0x618C0000); // ori r12, higher(addr)
375 writeInt32BE(Addr+8, 0x798C07C6); // sldi r12, r12, 32
376 writeInt32BE(Addr+12, 0x658C0000); // oris r12, r12, h(addr)
377 writeInt32BE(Addr+16, 0x618C0000); // ori r12, r12, l(addr)
378 writeInt32BE(Addr+20, 0xF8410028); // std r2, 40(r1)
379 writeInt32BE(Addr+24, 0xE96C0000); // ld r11, 0(r12)
380 writeInt32BE(Addr+28, 0xE84C0008); // ld r2, 0(r12)
381 writeInt32BE(Addr+32, 0x7D6903A6); // mtctr r11
382 writeInt32BE(Addr+36, 0xE96C0010); // ld r11, 16(r2)
383 writeInt32BE(Addr+40, 0x4E800420); // bctr
Andrew Kaylor5fc8c7c2012-11-01 19:49:21 +0000384
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +0000385 return Addr;
Akira Hatanakab889e0c2012-08-17 21:28:04 +0000386 }
387 return Addr;
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000388}
389
390// Assign an address to a symbol name and resolve all the relocations
391// associated with it.
392void RuntimeDyldImpl::reassignSectionAddress(unsigned SectionID,
393 uint64_t Addr) {
394 // The address to use for relocation resolution is not
395 // the address of the local section buffer. We must be doing
Andrew Kaylor28989882012-11-05 20:57:16 +0000396 // a remote execution environment of some sort. Relocations can't
397 // be applied until all the sections have been moved. The client must
398 // trigger this with a call to MCJIT::finalize() or
399 // RuntimeDyld::resolveRelocations().
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000400 //
401 // Addr is a uint64_t because we can't assume the pointer width
402 // of the target is the same as that of the host. Just use a generic
403 // "big enough" type.
404 Sections[SectionID].LoadAddress = Addr;
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000405}
406
407void RuntimeDyldImpl::resolveRelocationEntry(const RelocationEntry &RE,
408 uint64_t Value) {
Eric Christophera7ca3c22012-10-12 02:04:47 +0000409 // Ignore relocations for sections that were not loaded
410 if (Sections[RE.SectionID].Address != 0) {
Eric Christophera7ca3c22012-10-12 02:04:47 +0000411 DEBUG(dbgs() << "\tSectionID: " << RE.SectionID
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000412 << " + " << RE.Offset << " ("
413 << format("%p", Sections[RE.SectionID].Address + RE.Offset) << ")"
Eric Christophera7ca3c22012-10-12 02:04:47 +0000414 << " RelType: " << RE.RelType
415 << " Addend: " << RE.Addend
416 << "\n");
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000417
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000418 resolveRelocation(Sections[RE.SectionID], RE.Offset,
Eric Christophera7ca3c22012-10-12 02:04:47 +0000419 Value, RE.RelType, RE.Addend);
Preston Gurdc68dda82012-04-12 20:13:57 +0000420 }
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000421}
422
423void RuntimeDyldImpl::resolveRelocationList(const RelocationList &Relocs,
424 uint64_t Value) {
425 for (unsigned i = 0, e = Relocs.size(); i != e; ++i) {
426 resolveRelocationEntry(Relocs[i], Value);
427 }
428}
429
Eli Bendersky37bc5a22012-04-30 12:15:58 +0000430void RuntimeDyldImpl::resolveExternalSymbols() {
431 StringMap<RelocationList>::iterator i = ExternalSymbolRelocations.begin(),
432 e = ExternalSymbolRelocations.end();
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000433 for (; i != e; i++) {
434 StringRef Name = i->first();
435 RelocationList &Relocs = i->second;
Eli Benderskyd98c9e92012-05-01 06:58:59 +0000436 SymbolTableMap::const_iterator Loc = GlobalSymbolTable.find(Name);
437 if (Loc == GlobalSymbolTable.end()) {
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000438 // This is an external symbol, try to get it address from
439 // MemoryManager.
440 uint8_t *Addr = (uint8_t*) MemMgr->getPointerToNamedFunction(Name.data(),
441 true);
442 DEBUG(dbgs() << "Resolving relocations Name: " << Name
443 << "\t" << format("%p", Addr)
444 << "\n");
445 resolveRelocationList(Relocs, (uintptr_t)Addr);
446 } else {
Eli Bendersky37bc5a22012-04-30 12:15:58 +0000447 report_fatal_error("Expected external symbol");
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000448 }
449 }
450}
451
452
Jim Grosbach6e563312011-03-21 22:15:52 +0000453//===----------------------------------------------------------------------===//
454// RuntimeDyld class implementation
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000455RuntimeDyld::RuntimeDyld(RTDyldMemoryManager *mm) {
Andrew Kaylor53608a32012-11-15 23:50:01 +0000456 // FIXME: There's a potential issue lurking here if a single instance of
457 // RuntimeDyld is used to load multiple objects. The current implementation
458 // associates a single memory manager with a RuntimeDyld instance. Even
459 // though the public class spawns a new 'impl' instance for each load,
460 // they share a single memory manager. This can become a problem when page
461 // permissions are applied.
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000462 Dyld = 0;
463 MM = mm;
Jim Grosbach6e563312011-03-21 22:15:52 +0000464}
465
466RuntimeDyld::~RuntimeDyld() {
467 delete Dyld;
468}
469
Andrew Kaylor3f23cef2012-10-02 21:18:39 +0000470ObjectImage *RuntimeDyld::loadObject(ObjectBuffer *InputBuffer) {
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000471 if (!Dyld) {
Eli Benderskya66a1852012-01-16 08:56:09 +0000472 sys::LLVMFileType type = sys::IdentifyFileType(
473 InputBuffer->getBufferStart(),
474 static_cast<unsigned>(InputBuffer->getBufferSize()));
475 switch (type) {
476 case sys::ELF_Relocatable_FileType:
477 case sys::ELF_Executable_FileType:
478 case sys::ELF_SharedObject_FileType:
479 case sys::ELF_Core_FileType:
480 Dyld = new RuntimeDyldELF(MM);
481 break;
482 case sys::Mach_O_Object_FileType:
483 case sys::Mach_O_Executable_FileType:
484 case sys::Mach_O_FixedVirtualMemorySharedLib_FileType:
485 case sys::Mach_O_Core_FileType:
486 case sys::Mach_O_PreloadExecutable_FileType:
487 case sys::Mach_O_DynamicallyLinkedSharedLib_FileType:
488 case sys::Mach_O_DynamicLinker_FileType:
489 case sys::Mach_O_Bundle_FileType:
490 case sys::Mach_O_DynamicallyLinkedSharedLibStub_FileType:
491 case sys::Mach_O_DSYMCompanion_FileType:
492 Dyld = new RuntimeDyldMachO(MM);
493 break;
494 case sys::Unknown_FileType:
495 case sys::Bitcode_FileType:
496 case sys::Archive_FileType:
497 case sys::COFF_FileType:
498 report_fatal_error("Incompatible object format!");
499 }
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000500 } else {
Eli Benderskya66a1852012-01-16 08:56:09 +0000501 if (!Dyld->isCompatibleFormat(InputBuffer))
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000502 report_fatal_error("Incompatible object format!");
503 }
504
Jim Grosbach6e563312011-03-21 22:15:52 +0000505 return Dyld->loadObject(InputBuffer);
506}
507
Jim Grosbachb0271052011-04-08 17:31:24 +0000508void *RuntimeDyld::getSymbolAddress(StringRef Name) {
Jim Grosbach6e563312011-03-21 22:15:52 +0000509 return Dyld->getSymbolAddress(Name);
510}
511
Jim Grosbach35ed8422012-09-05 16:50:40 +0000512uint64_t RuntimeDyld::getSymbolLoadAddress(StringRef Name) {
513 return Dyld->getSymbolLoadAddress(Name);
514}
515
Jim Grosbachf8c1c842011-04-12 21:20:41 +0000516void RuntimeDyld::resolveRelocations() {
517 Dyld->resolveRelocations();
518}
519
Jim Grosbach61425c02012-01-16 22:26:39 +0000520void RuntimeDyld::reassignSectionAddress(unsigned SectionID,
521 uint64_t Addr) {
522 Dyld->reassignSectionAddress(SectionID, Addr);
Jim Grosbachf8c1c842011-04-12 21:20:41 +0000523}
524
Jim Grosbache940c1b2012-09-13 21:50:06 +0000525void RuntimeDyld::mapSectionAddress(const void *LocalAddress,
Jim Grosbach020f4e82012-01-16 23:50:55 +0000526 uint64_t TargetAddress) {
527 Dyld->mapSectionAddress(LocalAddress, TargetAddress);
528}
529
Jim Grosbach91dde152011-03-22 18:22:27 +0000530StringRef RuntimeDyld::getErrorString() {
Jim Grosbachb3eecaf2011-03-22 18:19:42 +0000531 return Dyld->getErrorString();
532}
533
Jim Grosbach6e563312011-03-21 22:15:52 +0000534} // end namespace llvm