blob: 714eaf48cf87ab3cece06d86c02ea72e01163810 [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.
Rafael Espindola59a0e792013-04-29 22:24:22 +000099 uint32_t Align;
100 Check(i->getAlignment(Align));
Preston Gurdc68dda82012-04-12 20:13:57 +0000101 uint64_t Size = 0;
102 Check(i->getSize(Size));
Tim Northoverf00677d2012-10-29 10:47:04 +0000103 CommonSize += Size + Align;
104 CommonSymbols[*i] = CommonSymbolInfo(Size, Align);
Preston Gurdc68dda82012-04-12 20:13:57 +0000105 } else {
106 if (SymType == object::SymbolRef::ST_Function ||
Akira Hatanakab889e0c2012-08-17 21:28:04 +0000107 SymType == object::SymbolRef::ST_Data ||
108 SymType == object::SymbolRef::ST_Unknown) {
Preston Gurdc68dda82012-04-12 20:13:57 +0000109 uint64_t FileOffset;
Eli Bendersky5fe01982012-04-29 12:40:47 +0000110 StringRef SectionData;
Tim Northover78822132012-12-17 17:59:35 +0000111 bool IsCode;
Preston Gurdc68dda82012-04-12 20:13:57 +0000112 section_iterator si = obj->end_sections();
113 Check(i->getFileOffset(FileOffset));
114 Check(i->getSection(si));
115 if (si == obj->end_sections()) continue;
Eli Bendersky5fe01982012-04-29 12:40:47 +0000116 Check(si->getContents(SectionData));
Tim Northover78822132012-12-17 17:59:35 +0000117 Check(si->isText(IsCode));
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());
Tim Northover78822132012-12-17 17:59:35 +0000122 unsigned SectionID = findOrEmitSection(*obj, *si, IsCode, 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));
Amara Emerson098d6d52012-11-16 11:11:59 +0000128 GlobalSymbolTable[Name] = SymbolLoc(SectionID, SectOffset);
Preston Gurdc68dda82012-04-12 20:13:57 +0000129 }
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000130 }
131 DEBUG(dbgs() << "\tType: " << SymType << " Name: " << Name << "\n");
132 }
133
Preston Gurdc68dda82012-04-12 20:13:57 +0000134 // Allocate common symbols
135 if (CommonSize != 0)
Preston Gurd689ff9c2012-04-16 22:12:58 +0000136 emitCommonSymbols(*obj, CommonSymbols, CommonSize, LocalSymbols);
Preston Gurdc68dda82012-04-12 20:13:57 +0000137
Eli Bendersky5fe01982012-04-29 12:40:47 +0000138 // Parse and process relocations
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000139 DEBUG(dbgs() << "Parse relocations:\n");
140 for (section_iterator si = obj->begin_sections(),
141 se = obj->end_sections(); si != se; si.increment(err)) {
142 Check(err);
143 bool isFirstRelocation = true;
144 unsigned SectionID = 0;
145 StubMap Stubs;
146
147 for (relocation_iterator i = si->begin_relocations(),
148 e = si->end_relocations(); i != e; i.increment(err)) {
149 Check(err);
150
Eli Bendersky5fe01982012-04-29 12:40:47 +0000151 // If it's the first relocation in this section, find its SectionID
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000152 if (isFirstRelocation) {
Preston Gurd689ff9c2012-04-16 22:12:58 +0000153 SectionID = findOrEmitSection(*obj, *si, true, LocalSections);
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000154 DEBUG(dbgs() << "\tSectionID: " << SectionID << "\n");
155 isFirstRelocation = false;
156 }
157
Rafael Espindolaca0e7362013-04-29 19:03:21 +0000158 processRelocationRef(SectionID, *i, *obj, LocalSections, LocalSymbols,
Rafael Espindolaefa91f62013-04-29 14:44:23 +0000159 Stubs);
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000160 }
161 }
Preston Gurd689ff9c2012-04-16 22:12:58 +0000162
Andrew Kaylor3f23cef2012-10-02 21:18:39 +0000163 return obj.take();
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000164}
165
Eli Benderskyc201e6e2012-05-01 10:41:12 +0000166void RuntimeDyldImpl::emitCommonSymbols(ObjectImage &Obj,
167 const CommonSymbolMap &CommonSymbols,
168 uint64_t TotalSize,
169 SymbolTableMap &SymbolTable) {
Preston Gurdc68dda82012-04-12 20:13:57 +0000170 // Allocate memory for the section
171 unsigned SectionID = Sections.size();
172 uint8_t *Addr = MemMgr->allocateDataSection(TotalSize, sizeof(void*),
Andrew Kaylor53608a32012-11-15 23:50:01 +0000173 SectionID, false);
Preston Gurdc68dda82012-04-12 20:13:57 +0000174 if (!Addr)
175 report_fatal_error("Unable to allocate memory for common symbols!");
176 uint64_t Offset = 0;
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +0000177 Sections.push_back(SectionEntry(StringRef(), Addr, TotalSize, TotalSize, 0));
Preston Gurdc68dda82012-04-12 20:13:57 +0000178 memset(Addr, 0, TotalSize);
179
180 DEBUG(dbgs() << "emitCommonSection SectionID: " << SectionID
181 << " new addr: " << format("%p", Addr)
182 << " DataSize: " << TotalSize
183 << "\n");
184
185 // Assign the address of each symbol
Eli Benderskyc201e6e2012-05-01 10:41:12 +0000186 for (CommonSymbolMap::const_iterator it = CommonSymbols.begin(),
187 itEnd = CommonSymbols.end(); it != itEnd; it++) {
Tim Northoverf00677d2012-10-29 10:47:04 +0000188 uint64_t Size = it->second.first;
189 uint64_t Align = it->second.second;
Preston Gurdc68dda82012-04-12 20:13:57 +0000190 StringRef Name;
191 it->first.getName(Name);
Tim Northoverf00677d2012-10-29 10:47:04 +0000192 if (Align) {
193 // This symbol has an alignment requirement.
194 uint64_t AlignOffset = OffsetToAlignment((uint64_t)Addr, Align);
195 Addr += AlignOffset;
196 Offset += AlignOffset;
197 DEBUG(dbgs() << "Allocating common symbol " << Name << " address " <<
Andrew Kaylor5fc8c7c2012-11-01 19:49:21 +0000198 format("%p\n", Addr));
Tim Northoverf00677d2012-10-29 10:47:04 +0000199 }
Preston Gurd689ff9c2012-04-16 22:12:58 +0000200 Obj.updateSymbolAddress(it->first, (uint64_t)Addr);
Eli Benderskyc201e6e2012-05-01 10:41:12 +0000201 SymbolTable[Name.data()] = SymbolLoc(SectionID, Offset);
Preston Gurdc68dda82012-04-12 20:13:57 +0000202 Offset += Size;
203 Addr += Size;
204 }
Preston Gurdc68dda82012-04-12 20:13:57 +0000205}
206
Preston Gurd689ff9c2012-04-16 22:12:58 +0000207unsigned RuntimeDyldImpl::emitSection(ObjectImage &Obj,
208 const SectionRef &Section,
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000209 bool IsCode) {
210
211 unsigned StubBufSize = 0,
212 StubSize = getMaxStubSize();
213 error_code err;
214 if (StubSize > 0) {
215 for (relocation_iterator i = Section.begin_relocations(),
Preston Gurdc68dda82012-04-12 20:13:57 +0000216 e = Section.end_relocations(); i != e; i.increment(err), Check(err))
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000217 StubBufSize += StubSize;
218 }
219 StringRef data;
220 uint64_t Alignment64;
221 Check(Section.getContents(data));
222 Check(Section.getAlignment(Alignment64));
223
224 unsigned Alignment = (unsigned)Alignment64 & 0xffffffffL;
Preston Gurdc68dda82012-04-12 20:13:57 +0000225 bool IsRequired;
226 bool IsVirtual;
227 bool IsZeroInit;
Andrew Kaylor53608a32012-11-15 23:50:01 +0000228 bool IsReadOnly;
Preston Gurdc68dda82012-04-12 20:13:57 +0000229 uint64_t DataSize;
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +0000230 StringRef Name;
Preston Gurdc68dda82012-04-12 20:13:57 +0000231 Check(Section.isRequiredForExecution(IsRequired));
232 Check(Section.isVirtual(IsVirtual));
233 Check(Section.isZeroInit(IsZeroInit));
Andrew Kaylor53608a32012-11-15 23:50:01 +0000234 Check(Section.isReadOnlyData(IsReadOnly));
Preston Gurdc68dda82012-04-12 20:13:57 +0000235 Check(Section.getSize(DataSize));
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +0000236 Check(Section.getName(Name));
Richard Sandiford6fc2ad62013-05-03 14:15:35 +0000237 if (StubSize > 0) {
238 unsigned StubAlignment = getStubAlignment();
239 unsigned EndAlignment = (DataSize | Alignment) & -(DataSize | Alignment);
240 if (StubAlignment > EndAlignment)
241 StubBufSize += StubAlignment - EndAlignment;
242 }
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)
Andrew Kaylor53608a32012-11-15 23:50:01 +0000255 : MemMgr->allocateDataSection(Allocate, Alignment, SectionID, IsReadOnly);
Preston Gurdc68dda82012-04-12 20:13:57 +0000256 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) {
Tim Northover4a9b6b72013-05-04 20:14:09 +0000339 if (Arch == Triple::aarch64) {
340 // This stub has to be able to access the full address space,
341 // since symbol lookup won't necessarily find a handy, in-range,
342 // PLT stub for functions which could be anywhere.
343 uint32_t *StubAddr = (uint32_t*)Addr;
344
345 // Stub can use ip0 (== x16) to calculate address
346 *StubAddr = 0xd2e00010; // movz ip0, #:abs_g3:<addr>
347 StubAddr++;
348 *StubAddr = 0xf2c00010; // movk ip0, #:abs_g2_nc:<addr>
349 StubAddr++;
350 *StubAddr = 0xf2a00010; // movk ip0, #:abs_g1_nc:<addr>
351 StubAddr++;
352 *StubAddr = 0xf2800010; // movk ip0, #:abs_g0_nc:<addr>
353 StubAddr++;
354 *StubAddr = 0xd61f0200; // br ip0
355
356 return Addr;
357 } else if (Arch == Triple::arm) {
Akira Hatanakab889e0c2012-08-17 21:28:04 +0000358 // TODO: There is only ARM far stub now. We should add the Thumb stub,
359 // and stubs for branches Thumb - ARM and ARM - Thumb.
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000360 uint32_t *StubAddr = (uint32_t*)Addr;
361 *StubAddr = 0xe51ff004; // ldr pc,<label>
362 return (uint8_t*)++StubAddr;
NAKAMURA Takumi4af1ca52012-12-04 00:08:14 +0000363 } else if (Arch == Triple::mipsel || Arch == Triple::mips) {
Akira Hatanakab889e0c2012-08-17 21:28:04 +0000364 uint32_t *StubAddr = (uint32_t*)Addr;
365 // 0: 3c190000 lui t9,%hi(addr).
366 // 4: 27390000 addiu t9,t9,%lo(addr).
367 // 8: 03200008 jr t9.
368 // c: 00000000 nop.
369 const unsigned LuiT9Instr = 0x3c190000, AdduiT9Instr = 0x27390000;
370 const unsigned JrT9Instr = 0x03200008, NopInstr = 0x0;
371
372 *StubAddr = LuiT9Instr;
373 StubAddr++;
374 *StubAddr = AdduiT9Instr;
375 StubAddr++;
376 *StubAddr = JrT9Instr;
377 StubAddr++;
378 *StubAddr = NopInstr;
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000379 return Addr;
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +0000380 } else if (Arch == Triple::ppc64) {
381 // PowerPC64 stub: the address points to a function descriptor
382 // instead of the function itself. Load the function address
383 // on r11 and sets it to control register. Also loads the function
384 // TOC in r2 and environment pointer to r11.
385 writeInt32BE(Addr, 0x3D800000); // lis r12, highest(addr)
386 writeInt32BE(Addr+4, 0x618C0000); // ori r12, higher(addr)
387 writeInt32BE(Addr+8, 0x798C07C6); // sldi r12, r12, 32
388 writeInt32BE(Addr+12, 0x658C0000); // oris r12, r12, h(addr)
389 writeInt32BE(Addr+16, 0x618C0000); // ori r12, r12, l(addr)
390 writeInt32BE(Addr+20, 0xF8410028); // std r2, 40(r1)
391 writeInt32BE(Addr+24, 0xE96C0000); // ld r11, 0(r12)
392 writeInt32BE(Addr+28, 0xE84C0008); // ld r2, 0(r12)
393 writeInt32BE(Addr+32, 0x7D6903A6); // mtctr r11
394 writeInt32BE(Addr+36, 0xE96C0010); // ld r11, 16(r2)
395 writeInt32BE(Addr+40, 0x4E800420); // bctr
Andrew Kaylor5fc8c7c2012-11-01 19:49:21 +0000396
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +0000397 return Addr;
Richard Sandiford6fc2ad62013-05-03 14:15:35 +0000398 } else if (Arch == Triple::systemz) {
399 writeInt16BE(Addr, 0xC418); // lgrl %r1,.+8
400 writeInt16BE(Addr+2, 0x0000);
401 writeInt16BE(Addr+4, 0x0004);
402 writeInt16BE(Addr+6, 0x07F1); // brc 15,%r1
403 // 8-byte address stored at Addr + 8
404 return Addr;
Akira Hatanakab889e0c2012-08-17 21:28:04 +0000405 }
406 return Addr;
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000407}
408
409// Assign an address to a symbol name and resolve all the relocations
410// associated with it.
411void RuntimeDyldImpl::reassignSectionAddress(unsigned SectionID,
412 uint64_t Addr) {
413 // The address to use for relocation resolution is not
414 // the address of the local section buffer. We must be doing
Andrew Kaylor28989882012-11-05 20:57:16 +0000415 // a remote execution environment of some sort. Relocations can't
416 // be applied until all the sections have been moved. The client must
417 // trigger this with a call to MCJIT::finalize() or
418 // RuntimeDyld::resolveRelocations().
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000419 //
420 // Addr is a uint64_t because we can't assume the pointer width
421 // of the target is the same as that of the host. Just use a generic
422 // "big enough" type.
423 Sections[SectionID].LoadAddress = Addr;
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000424}
425
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000426void RuntimeDyldImpl::resolveRelocationList(const RelocationList &Relocs,
427 uint64_t Value) {
428 for (unsigned i = 0, e = Relocs.size(); i != e; ++i) {
Rafael Espindola87b50172013-04-29 17:24:34 +0000429 const RelocationEntry &RE = Relocs[i];
430 // Ignore relocations for sections that were not loaded
431 if (Sections[RE.SectionID].Address == 0)
432 continue;
433 resolveRelocation(RE, Value);
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000434 }
435}
436
Eli Bendersky37bc5a22012-04-30 12:15:58 +0000437void RuntimeDyldImpl::resolveExternalSymbols() {
438 StringMap<RelocationList>::iterator i = ExternalSymbolRelocations.begin(),
439 e = ExternalSymbolRelocations.end();
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000440 for (; i != e; i++) {
441 StringRef Name = i->first();
442 RelocationList &Relocs = i->second;
Eli Benderskyd98c9e92012-05-01 06:58:59 +0000443 SymbolTableMap::const_iterator Loc = GlobalSymbolTable.find(Name);
444 if (Loc == GlobalSymbolTable.end()) {
Andrew Kaylor7b170502013-02-20 18:09:21 +0000445 if (Name.size() == 0) {
446 // This is an absolute symbol, use an address of zero.
447 DEBUG(dbgs() << "Resolving absolute relocations." << "\n");
448 resolveRelocationList(Relocs, 0);
Andrew Kaylor29fe1502013-02-20 18:24:34 +0000449 } else {
450 // This is an external symbol, try to get its address from
Andrew Kaylor7b170502013-02-20 18:09:21 +0000451 // MemoryManager.
452 uint8_t *Addr = (uint8_t*) MemMgr->getPointerToNamedFunction(Name.data(),
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000453 true);
Andrew Kaylor7b170502013-02-20 18:09:21 +0000454 DEBUG(dbgs() << "Resolving relocations Name: " << Name
455 << "\t" << format("%p", Addr)
456 << "\n");
457 resolveRelocationList(Relocs, (uintptr_t)Addr);
458 }
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000459 } else {
Eli Bendersky37bc5a22012-04-30 12:15:58 +0000460 report_fatal_error("Expected external symbol");
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000461 }
462 }
463}
464
465
Jim Grosbach6e563312011-03-21 22:15:52 +0000466//===----------------------------------------------------------------------===//
467// RuntimeDyld class implementation
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000468RuntimeDyld::RuntimeDyld(RTDyldMemoryManager *mm) {
Andrew Kaylor53608a32012-11-15 23:50:01 +0000469 // FIXME: There's a potential issue lurking here if a single instance of
470 // RuntimeDyld is used to load multiple objects. The current implementation
471 // associates a single memory manager with a RuntimeDyld instance. Even
472 // though the public class spawns a new 'impl' instance for each load,
473 // they share a single memory manager. This can become a problem when page
474 // permissions are applied.
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000475 Dyld = 0;
476 MM = mm;
Jim Grosbach6e563312011-03-21 22:15:52 +0000477}
478
479RuntimeDyld::~RuntimeDyld() {
480 delete Dyld;
481}
482
Andrew Kaylor3f23cef2012-10-02 21:18:39 +0000483ObjectImage *RuntimeDyld::loadObject(ObjectBuffer *InputBuffer) {
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000484 if (!Dyld) {
Eli Benderskya66a1852012-01-16 08:56:09 +0000485 sys::LLVMFileType type = sys::IdentifyFileType(
486 InputBuffer->getBufferStart(),
487 static_cast<unsigned>(InputBuffer->getBufferSize()));
488 switch (type) {
489 case sys::ELF_Relocatable_FileType:
490 case sys::ELF_Executable_FileType:
491 case sys::ELF_SharedObject_FileType:
492 case sys::ELF_Core_FileType:
493 Dyld = new RuntimeDyldELF(MM);
494 break;
495 case sys::Mach_O_Object_FileType:
496 case sys::Mach_O_Executable_FileType:
497 case sys::Mach_O_FixedVirtualMemorySharedLib_FileType:
498 case sys::Mach_O_Core_FileType:
499 case sys::Mach_O_PreloadExecutable_FileType:
500 case sys::Mach_O_DynamicallyLinkedSharedLib_FileType:
501 case sys::Mach_O_DynamicLinker_FileType:
502 case sys::Mach_O_Bundle_FileType:
503 case sys::Mach_O_DynamicallyLinkedSharedLibStub_FileType:
504 case sys::Mach_O_DSYMCompanion_FileType:
505 Dyld = new RuntimeDyldMachO(MM);
506 break;
507 case sys::Unknown_FileType:
508 case sys::Bitcode_FileType:
509 case sys::Archive_FileType:
510 case sys::COFF_FileType:
511 report_fatal_error("Incompatible object format!");
512 }
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000513 } else {
Eli Benderskya66a1852012-01-16 08:56:09 +0000514 if (!Dyld->isCompatibleFormat(InputBuffer))
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000515 report_fatal_error("Incompatible object format!");
516 }
517
Jim Grosbach6e563312011-03-21 22:15:52 +0000518 return Dyld->loadObject(InputBuffer);
519}
520
Jim Grosbachb0271052011-04-08 17:31:24 +0000521void *RuntimeDyld::getSymbolAddress(StringRef Name) {
Jim Grosbach6e563312011-03-21 22:15:52 +0000522 return Dyld->getSymbolAddress(Name);
523}
524
Jim Grosbach35ed8422012-09-05 16:50:40 +0000525uint64_t RuntimeDyld::getSymbolLoadAddress(StringRef Name) {
526 return Dyld->getSymbolLoadAddress(Name);
527}
528
Jim Grosbachf8c1c842011-04-12 21:20:41 +0000529void RuntimeDyld::resolveRelocations() {
530 Dyld->resolveRelocations();
531}
532
Jim Grosbach61425c02012-01-16 22:26:39 +0000533void RuntimeDyld::reassignSectionAddress(unsigned SectionID,
534 uint64_t Addr) {
535 Dyld->reassignSectionAddress(SectionID, Addr);
Jim Grosbachf8c1c842011-04-12 21:20:41 +0000536}
537
Jim Grosbache940c1b2012-09-13 21:50:06 +0000538void RuntimeDyld::mapSectionAddress(const void *LocalAddress,
Jim Grosbach020f4e82012-01-16 23:50:55 +0000539 uint64_t TargetAddress) {
540 Dyld->mapSectionAddress(LocalAddress, TargetAddress);
541}
542
Jim Grosbach91dde152011-03-22 18:22:27 +0000543StringRef RuntimeDyld::getErrorString() {
Jim Grosbachb3eecaf2011-03-22 18:19:42 +0000544 return Dyld->getErrorString();
545}
546
Jim Grosbach6e563312011-03-21 22:15:52 +0000547} // end namespace llvm