blob: d36126a9e2db776b979e1da7496477a5a57be90e [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"
Rafael Espindola3ecfcc22013-06-11 18:01:14 +000020#include "llvm/Support/FileSystem.h"
Tim Northoverf00677d2012-10-29 10:47:04 +000021#include "llvm/Support/MathExtras.h"
Rafael Espindola7486d922013-05-30 03:05:14 +000022#include "llvm/Object/ELF.h"
Eli Bendersky76463fd2012-01-22 07:05:02 +000023
Jim Grosbach6e563312011-03-21 22:15:52 +000024using namespace llvm;
25using namespace llvm::object;
26
Chandler Carruth53c5e7b2011-04-05 23:54:31 +000027// Empty out-of-line virtual destructor as the key function.
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
Rafael Espindolaa2e40fb2013-05-05 20:43:10 +000032StringRef RuntimeDyldImpl::getEHFrameSection() {
33 return StringRef();
34}
35
Jim Grosbachf8c1c842011-04-12 21:20:41 +000036// Resolve the relocations for all symbols we currently know about.
37void RuntimeDyldImpl::resolveRelocations() {
Preston Gurdc68dda82012-04-12 20:13:57 +000038 // First, resolve relocations associated with external symbols.
Eli Bendersky37bc5a22012-04-30 12:15:58 +000039 resolveExternalSymbols();
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +000040
Jim Grosbach61425c02012-01-16 22:26:39 +000041 // Just iterate over the sections we have and resolve all the relocations
42 // in them. Gross overkill, but it gets the job done.
43 for (int i = 0, e = Sections.size(); i != e; ++i) {
Andrew Kaylor32bd10b2013-08-19 19:38:06 +000044 // The Section here (Sections[i]) refers to the section in which the
45 // symbol for the relocation is located. The SectionID in the relocation
46 // entry provides the section to which the relocation will be applied.
Andrew Kaylor28989882012-11-05 20:57:16 +000047 uint64_t Addr = Sections[i].LoadAddress;
48 DEBUG(dbgs() << "Resolving relocations Section #" << i
49 << "\t" << format("%p", (uint8_t *)Addr)
50 << "\n");
51 resolveRelocationList(Relocations[i], Addr);
Jim Grosbach61425c02012-01-16 22:26:39 +000052 }
Jim Grosbachf8c1c842011-04-12 21:20:41 +000053}
54
Jim Grosbache940c1b2012-09-13 21:50:06 +000055void RuntimeDyldImpl::mapSectionAddress(const void *LocalAddress,
Jim Grosbach020f4e82012-01-16 23:50:55 +000056 uint64_t TargetAddress) {
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +000057 for (unsigned i = 0, e = Sections.size(); i != e; ++i) {
58 if (Sections[i].Address == LocalAddress) {
59 reassignSectionAddress(i, TargetAddress);
60 return;
61 }
62 }
63 llvm_unreachable("Attempting to remap address of unknown section!");
Jim Grosbach020f4e82012-01-16 23:50:55 +000064}
65
Eli Bendersky5fe01982012-04-29 12:40:47 +000066// Subclasses can implement this method to create specialized image instances.
Sylvestre Ledruc8e41c52012-07-23 08:51:15 +000067// The caller owns the pointer that is returned.
Andrew Kaylor3f23cef2012-10-02 21:18:39 +000068ObjectImage *RuntimeDyldImpl::createObjectImage(ObjectBuffer *InputBuffer) {
69 return new ObjectImageCommon(InputBuffer);
Preston Gurd689ff9c2012-04-16 22:12:58 +000070}
71
Andrew Kaylor3f23cef2012-10-02 21:18:39 +000072ObjectImage *RuntimeDyldImpl::loadObject(ObjectBuffer *InputBuffer) {
Preston Gurd689ff9c2012-04-16 22:12:58 +000073 OwningPtr<ObjectImage> obj(createObjectImage(InputBuffer));
Preston Gurdc68dda82012-04-12 20:13:57 +000074 if (!obj)
75 report_fatal_error("Unable to create object image from memory buffer!");
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +000076
77 Arch = (Triple::ArchType)obj->getArch();
78
Eli Benderskyd98c9e92012-05-01 06:58:59 +000079 // Symbols found in this object
80 StringMap<SymbolLoc> LocalSymbols;
81 // Used sections from the object file
82 ObjSectionToIDMap LocalSections;
83
Tim Northoverf00677d2012-10-29 10:47:04 +000084 // Common symbols requiring allocation, with their sizes and alignments
Eli Benderskyd98c9e92012-05-01 06:58:59 +000085 CommonSymbolMap CommonSymbols;
Tim Northoverf00677d2012-10-29 10:47:04 +000086 // Maximum required total memory to allocate all common symbols
Eli Benderskyd98c9e92012-05-01 06:58:59 +000087 uint64_t CommonSize = 0;
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +000088
89 error_code err;
90 // Parse symbols
91 DEBUG(dbgs() << "Parse symbols:\n");
92 for (symbol_iterator i = obj->begin_symbols(), e = obj->end_symbols();
93 i != e; i.increment(err)) {
94 Check(err);
95 object::SymbolRef::Type SymType;
96 StringRef Name;
97 Check(i->getType(SymType));
98 Check(i->getName(Name));
99
Preston Gurdc68dda82012-04-12 20:13:57 +0000100 uint32_t flags;
101 Check(i->getFlags(flags));
102
103 bool isCommon = flags & SymbolRef::SF_Common;
104 if (isCommon) {
105 // Add the common symbols to a list. We'll allocate them all below.
Rafael Espindola59a0e792013-04-29 22:24:22 +0000106 uint32_t Align;
107 Check(i->getAlignment(Align));
Preston Gurdc68dda82012-04-12 20:13:57 +0000108 uint64_t Size = 0;
109 Check(i->getSize(Size));
Tim Northoverf00677d2012-10-29 10:47:04 +0000110 CommonSize += Size + Align;
111 CommonSymbols[*i] = CommonSymbolInfo(Size, Align);
Preston Gurdc68dda82012-04-12 20:13:57 +0000112 } else {
113 if (SymType == object::SymbolRef::ST_Function ||
Akira Hatanakab889e0c2012-08-17 21:28:04 +0000114 SymType == object::SymbolRef::ST_Data ||
115 SymType == object::SymbolRef::ST_Unknown) {
Preston Gurdc68dda82012-04-12 20:13:57 +0000116 uint64_t FileOffset;
Eli Bendersky5fe01982012-04-29 12:40:47 +0000117 StringRef SectionData;
Tim Northover78822132012-12-17 17:59:35 +0000118 bool IsCode;
Preston Gurdc68dda82012-04-12 20:13:57 +0000119 section_iterator si = obj->end_sections();
120 Check(i->getFileOffset(FileOffset));
121 Check(i->getSection(si));
122 if (si == obj->end_sections()) continue;
Eli Bendersky5fe01982012-04-29 12:40:47 +0000123 Check(si->getContents(SectionData));
Tim Northover78822132012-12-17 17:59:35 +0000124 Check(si->isText(IsCode));
Preston Gurdc68dda82012-04-12 20:13:57 +0000125 const uint8_t* SymPtr = (const uint8_t*)InputBuffer->getBufferStart() +
126 (uintptr_t)FileOffset;
Eli Bendersky5fe01982012-04-29 12:40:47 +0000127 uintptr_t SectOffset = (uintptr_t)(SymPtr -
128 (const uint8_t*)SectionData.begin());
Tim Northover78822132012-12-17 17:59:35 +0000129 unsigned SectionID = findOrEmitSection(*obj, *si, IsCode, LocalSections);
Preston Gurdc68dda82012-04-12 20:13:57 +0000130 LocalSymbols[Name.data()] = SymbolLoc(SectionID, SectOffset);
131 DEBUG(dbgs() << "\tFileOffset: " << format("%p", (uintptr_t)FileOffset)
132 << " flags: " << flags
133 << " SID: " << SectionID
134 << " Offset: " << format("%p", SectOffset));
Amara Emerson098d6d52012-11-16 11:11:59 +0000135 GlobalSymbolTable[Name] = SymbolLoc(SectionID, SectOffset);
Preston Gurdc68dda82012-04-12 20:13:57 +0000136 }
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000137 }
138 DEBUG(dbgs() << "\tType: " << SymType << " Name: " << Name << "\n");
139 }
140
Preston Gurdc68dda82012-04-12 20:13:57 +0000141 // Allocate common symbols
142 if (CommonSize != 0)
Preston Gurd689ff9c2012-04-16 22:12:58 +0000143 emitCommonSymbols(*obj, CommonSymbols, CommonSize, LocalSymbols);
Preston Gurdc68dda82012-04-12 20:13:57 +0000144
Eli Bendersky5fe01982012-04-29 12:40:47 +0000145 // Parse and process relocations
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000146 DEBUG(dbgs() << "Parse relocations:\n");
147 for (section_iterator si = obj->begin_sections(),
148 se = obj->end_sections(); si != se; si.increment(err)) {
149 Check(err);
150 bool isFirstRelocation = true;
151 unsigned SectionID = 0;
152 StubMap Stubs;
Rafael Espindola7486d922013-05-30 03:05:14 +0000153 section_iterator RelocatedSection = si->getRelocatedSection();
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000154
155 for (relocation_iterator i = si->begin_relocations(),
156 e = si->end_relocations(); i != e; i.increment(err)) {
157 Check(err);
158
Eli Bendersky5fe01982012-04-29 12:40:47 +0000159 // If it's the first relocation in this section, find its SectionID
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000160 if (isFirstRelocation) {
Rafael Espindola7486d922013-05-30 03:05:14 +0000161 SectionID =
162 findOrEmitSection(*obj, *RelocatedSection, true, LocalSections);
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000163 DEBUG(dbgs() << "\tSectionID: " << SectionID << "\n");
164 isFirstRelocation = false;
165 }
166
Rafael Espindolaca0e7362013-04-29 19:03:21 +0000167 processRelocationRef(SectionID, *i, *obj, LocalSections, LocalSymbols,
Rafael Espindolaefa91f62013-04-29 14:44:23 +0000168 Stubs);
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000169 }
170 }
Preston Gurd689ff9c2012-04-16 22:12:58 +0000171
Andrew Kaylorff9fa052013-08-19 23:27:43 +0000172 // Give the subclasses a chance to tie-up any loose ends.
173 finalizeLoad();
174
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;
Rafael Espindolaa2e40fb2013-05-05 20:43:10 +0000189 Sections.push_back(SectionEntry(StringRef(), Addr, 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;
Rafael Espindola7486d922013-05-30 03:05:14 +0000226 const ObjectFile *ObjFile = Obj.getObjectFile();
227 // FIXME: this is an inefficient way to handle this. We should computed the
228 // necessary section allocation size in loadObject by walking all the sections
229 // once.
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000230 if (StubSize > 0) {
Rafael Espindola7486d922013-05-30 03:05:14 +0000231 for (section_iterator SI = ObjFile->begin_sections(),
232 SE = ObjFile->end_sections();
233 SI != SE; SI.increment(err), Check(err)) {
234 section_iterator RelSecI = SI->getRelocatedSection();
235 if (!(RelSecI == Section))
236 continue;
237
238 for (relocation_iterator I = SI->begin_relocations(),
239 E = SI->end_relocations(); I != E; I.increment(err), Check(err)) {
240 StubBufSize += StubSize;
241 }
242 }
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000243 }
Rafael Espindola7486d922013-05-30 03:05:14 +0000244
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000245 StringRef data;
246 uint64_t Alignment64;
247 Check(Section.getContents(data));
248 Check(Section.getAlignment(Alignment64));
249
250 unsigned Alignment = (unsigned)Alignment64 & 0xffffffffL;
Preston Gurdc68dda82012-04-12 20:13:57 +0000251 bool IsRequired;
252 bool IsVirtual;
253 bool IsZeroInit;
Andrew Kaylor53608a32012-11-15 23:50:01 +0000254 bool IsReadOnly;
Preston Gurdc68dda82012-04-12 20:13:57 +0000255 uint64_t DataSize;
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +0000256 StringRef Name;
Preston Gurdc68dda82012-04-12 20:13:57 +0000257 Check(Section.isRequiredForExecution(IsRequired));
258 Check(Section.isVirtual(IsVirtual));
259 Check(Section.isZeroInit(IsZeroInit));
Andrew Kaylor53608a32012-11-15 23:50:01 +0000260 Check(Section.isReadOnlyData(IsReadOnly));
Preston Gurdc68dda82012-04-12 20:13:57 +0000261 Check(Section.getSize(DataSize));
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +0000262 Check(Section.getName(Name));
Richard Sandiford6fc2ad62013-05-03 14:15:35 +0000263 if (StubSize > 0) {
264 unsigned StubAlignment = getStubAlignment();
265 unsigned EndAlignment = (DataSize | Alignment) & -(DataSize | Alignment);
266 if (StubAlignment > EndAlignment)
267 StubBufSize += StubAlignment - EndAlignment;
268 }
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000269
Preston Gurdc68dda82012-04-12 20:13:57 +0000270 unsigned Allocate;
271 unsigned SectionID = Sections.size();
272 uint8_t *Addr;
273 const char *pData = 0;
274
275 // Some sections, such as debug info, don't need to be loaded for execution.
276 // Leave those where they are.
277 if (IsRequired) {
278 Allocate = DataSize + StubBufSize;
279 Addr = IsCode
280 ? MemMgr->allocateCodeSection(Allocate, Alignment, SectionID)
Andrew Kaylor53608a32012-11-15 23:50:01 +0000281 : MemMgr->allocateDataSection(Allocate, Alignment, SectionID, IsReadOnly);
Preston Gurdc68dda82012-04-12 20:13:57 +0000282 if (!Addr)
283 report_fatal_error("Unable to allocate section memory!");
284
285 // Virtual sections have no data in the object image, so leave pData = 0
286 if (!IsVirtual)
287 pData = data.data();
288
289 // Zero-initialize or copy the data from the image
290 if (IsZeroInit || IsVirtual)
291 memset(Addr, 0, DataSize);
292 else
293 memcpy(Addr, pData, DataSize);
294
295 DEBUG(dbgs() << "emitSection SectionID: " << SectionID
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +0000296 << " Name: " << Name
Preston Gurdc68dda82012-04-12 20:13:57 +0000297 << " obj addr: " << format("%p", pData)
298 << " new addr: " << format("%p", Addr)
299 << " DataSize: " << DataSize
300 << " StubBufSize: " << StubBufSize
301 << " Allocate: " << Allocate
302 << "\n");
Preston Gurd689ff9c2012-04-16 22:12:58 +0000303 Obj.updateSectionAddress(Section, (uint64_t)Addr);
Preston Gurdc68dda82012-04-12 20:13:57 +0000304 }
305 else {
306 // Even if we didn't load the section, we need to record an entry for it
Eli Bendersky5fe01982012-04-29 12:40:47 +0000307 // to handle later processing (and by 'handle' I mean don't do anything
308 // with these sections).
Preston Gurdc68dda82012-04-12 20:13:57 +0000309 Allocate = 0;
310 Addr = 0;
311 DEBUG(dbgs() << "emitSection SectionID: " << SectionID
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +0000312 << " Name: " << Name
Preston Gurdc68dda82012-04-12 20:13:57 +0000313 << " obj addr: " << format("%p", data.data())
314 << " new addr: 0"
315 << " DataSize: " << DataSize
316 << " StubBufSize: " << StubBufSize
317 << " Allocate: " << Allocate
318 << "\n");
319 }
320
Rafael Espindolaa2e40fb2013-05-05 20:43:10 +0000321 Sections.push_back(SectionEntry(Name, Addr, DataSize, (uintptr_t)pData));
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000322 return SectionID;
323}
324
Preston Gurd689ff9c2012-04-16 22:12:58 +0000325unsigned RuntimeDyldImpl::findOrEmitSection(ObjectImage &Obj,
326 const SectionRef &Section,
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000327 bool IsCode,
328 ObjSectionToIDMap &LocalSections) {
329
330 unsigned SectionID = 0;
331 ObjSectionToIDMap::iterator i = LocalSections.find(Section);
332 if (i != LocalSections.end())
333 SectionID = i->second;
334 else {
Preston Gurd689ff9c2012-04-16 22:12:58 +0000335 SectionID = emitSection(Obj, Section, IsCode);
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000336 LocalSections[Section] = SectionID;
337 }
338 return SectionID;
339}
340
Eli Benderskyc201e6e2012-05-01 10:41:12 +0000341void RuntimeDyldImpl::addRelocationForSection(const RelocationEntry &RE,
342 unsigned SectionID) {
343 Relocations[SectionID].push_back(RE);
344}
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000345
Eli Benderskyc201e6e2012-05-01 10:41:12 +0000346void RuntimeDyldImpl::addRelocationForSymbol(const RelocationEntry &RE,
347 StringRef SymbolName) {
348 // Relocation by symbol. If the symbol is found in the global symbol table,
349 // create an appropriate section relocation. Otherwise, add it to
350 // ExternalSymbolRelocations.
351 SymbolTableMap::const_iterator Loc =
352 GlobalSymbolTable.find(SymbolName);
353 if (Loc == GlobalSymbolTable.end()) {
354 ExternalSymbolRelocations[SymbolName].push_back(RE);
Eli Bendersky37bc5a22012-04-30 12:15:58 +0000355 } else {
Eli Benderskyc201e6e2012-05-01 10:41:12 +0000356 // Copy the RE since we want to modify its addend.
357 RelocationEntry RECopy = RE;
358 RECopy.Addend += Loc->second.second;
359 Relocations[Loc->second.first].push_back(RECopy);
Eli Bendersky37bc5a22012-04-30 12:15:58 +0000360 }
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000361}
362
363uint8_t *RuntimeDyldImpl::createStubFunction(uint8_t *Addr) {
Tim Northover4a9b6b72013-05-04 20:14:09 +0000364 if (Arch == Triple::aarch64) {
365 // This stub has to be able to access the full address space,
366 // since symbol lookup won't necessarily find a handy, in-range,
367 // PLT stub for functions which could be anywhere.
368 uint32_t *StubAddr = (uint32_t*)Addr;
369
370 // Stub can use ip0 (== x16) to calculate address
371 *StubAddr = 0xd2e00010; // movz ip0, #:abs_g3:<addr>
372 StubAddr++;
373 *StubAddr = 0xf2c00010; // movk ip0, #:abs_g2_nc:<addr>
374 StubAddr++;
375 *StubAddr = 0xf2a00010; // movk ip0, #:abs_g1_nc:<addr>
376 StubAddr++;
377 *StubAddr = 0xf2800010; // movk ip0, #:abs_g0_nc:<addr>
378 StubAddr++;
379 *StubAddr = 0xd61f0200; // br ip0
380
381 return Addr;
382 } else if (Arch == Triple::arm) {
Akira Hatanakab889e0c2012-08-17 21:28:04 +0000383 // TODO: There is only ARM far stub now. We should add the Thumb stub,
384 // and stubs for branches Thumb - ARM and ARM - Thumb.
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000385 uint32_t *StubAddr = (uint32_t*)Addr;
386 *StubAddr = 0xe51ff004; // ldr pc,<label>
387 return (uint8_t*)++StubAddr;
NAKAMURA Takumi4af1ca52012-12-04 00:08:14 +0000388 } else if (Arch == Triple::mipsel || Arch == Triple::mips) {
Akira Hatanakab889e0c2012-08-17 21:28:04 +0000389 uint32_t *StubAddr = (uint32_t*)Addr;
390 // 0: 3c190000 lui t9,%hi(addr).
391 // 4: 27390000 addiu t9,t9,%lo(addr).
392 // 8: 03200008 jr t9.
393 // c: 00000000 nop.
394 const unsigned LuiT9Instr = 0x3c190000, AdduiT9Instr = 0x27390000;
395 const unsigned JrT9Instr = 0x03200008, NopInstr = 0x0;
396
397 *StubAddr = LuiT9Instr;
398 StubAddr++;
399 *StubAddr = AdduiT9Instr;
400 StubAddr++;
401 *StubAddr = JrT9Instr;
402 StubAddr++;
403 *StubAddr = NopInstr;
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000404 return Addr;
Bill Schmidtf38cc382013-07-26 01:35:43 +0000405 } else if (Arch == Triple::ppc64 || Arch == Triple::ppc64le) {
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +0000406 // PowerPC64 stub: the address points to a function descriptor
407 // instead of the function itself. Load the function address
408 // on r11 and sets it to control register. Also loads the function
409 // TOC in r2 and environment pointer to r11.
410 writeInt32BE(Addr, 0x3D800000); // lis r12, highest(addr)
411 writeInt32BE(Addr+4, 0x618C0000); // ori r12, higher(addr)
412 writeInt32BE(Addr+8, 0x798C07C6); // sldi r12, r12, 32
413 writeInt32BE(Addr+12, 0x658C0000); // oris r12, r12, h(addr)
414 writeInt32BE(Addr+16, 0x618C0000); // ori r12, r12, l(addr)
415 writeInt32BE(Addr+20, 0xF8410028); // std r2, 40(r1)
416 writeInt32BE(Addr+24, 0xE96C0000); // ld r11, 0(r12)
417 writeInt32BE(Addr+28, 0xE84C0008); // ld r2, 0(r12)
418 writeInt32BE(Addr+32, 0x7D6903A6); // mtctr r11
419 writeInt32BE(Addr+36, 0xE96C0010); // ld r11, 16(r2)
420 writeInt32BE(Addr+40, 0x4E800420); // bctr
Andrew Kaylor5fc8c7c2012-11-01 19:49:21 +0000421
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +0000422 return Addr;
Richard Sandiford6fc2ad62013-05-03 14:15:35 +0000423 } else if (Arch == Triple::systemz) {
424 writeInt16BE(Addr, 0xC418); // lgrl %r1,.+8
425 writeInt16BE(Addr+2, 0x0000);
426 writeInt16BE(Addr+4, 0x0004);
427 writeInt16BE(Addr+6, 0x07F1); // brc 15,%r1
428 // 8-byte address stored at Addr + 8
429 return Addr;
Andrew Kaylorff9fa052013-08-19 23:27:43 +0000430 } else if (Arch == Triple::x86_64) {
431 *Addr = 0xFF; // jmp
432 *(Addr+1) = 0x25; // rip
433 // 32-bit PC-relative address of the GOT entry will be stored at Addr+2
Akira Hatanakab889e0c2012-08-17 21:28:04 +0000434 }
435 return Addr;
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000436}
437
438// Assign an address to a symbol name and resolve all the relocations
439// associated with it.
440void RuntimeDyldImpl::reassignSectionAddress(unsigned SectionID,
441 uint64_t Addr) {
442 // The address to use for relocation resolution is not
443 // the address of the local section buffer. We must be doing
Andrew Kaylor28989882012-11-05 20:57:16 +0000444 // a remote execution environment of some sort. Relocations can't
445 // be applied until all the sections have been moved. The client must
446 // trigger this with a call to MCJIT::finalize() or
447 // RuntimeDyld::resolveRelocations().
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000448 //
449 // Addr is a uint64_t because we can't assume the pointer width
450 // of the target is the same as that of the host. Just use a generic
451 // "big enough" type.
452 Sections[SectionID].LoadAddress = Addr;
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000453}
454
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000455void RuntimeDyldImpl::resolveRelocationList(const RelocationList &Relocs,
456 uint64_t Value) {
457 for (unsigned i = 0, e = Relocs.size(); i != e; ++i) {
Rafael Espindola87b50172013-04-29 17:24:34 +0000458 const RelocationEntry &RE = Relocs[i];
459 // Ignore relocations for sections that were not loaded
460 if (Sections[RE.SectionID].Address == 0)
461 continue;
462 resolveRelocation(RE, Value);
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000463 }
464}
465
Eli Bendersky37bc5a22012-04-30 12:15:58 +0000466void RuntimeDyldImpl::resolveExternalSymbols() {
467 StringMap<RelocationList>::iterator i = ExternalSymbolRelocations.begin(),
468 e = ExternalSymbolRelocations.end();
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000469 for (; i != e; i++) {
470 StringRef Name = i->first();
471 RelocationList &Relocs = i->second;
Eli Benderskyd98c9e92012-05-01 06:58:59 +0000472 SymbolTableMap::const_iterator Loc = GlobalSymbolTable.find(Name);
473 if (Loc == GlobalSymbolTable.end()) {
Andrew Kaylor7b170502013-02-20 18:09:21 +0000474 if (Name.size() == 0) {
475 // This is an absolute symbol, use an address of zero.
476 DEBUG(dbgs() << "Resolving absolute relocations." << "\n");
477 resolveRelocationList(Relocs, 0);
Andrew Kaylor29fe1502013-02-20 18:24:34 +0000478 } else {
479 // This is an external symbol, try to get its address from
Andrew Kaylor7b170502013-02-20 18:09:21 +0000480 // MemoryManager.
481 uint8_t *Addr = (uint8_t*) MemMgr->getPointerToNamedFunction(Name.data(),
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000482 true);
Andrew Kaylorff9fa052013-08-19 23:27:43 +0000483 updateGOTEntries(Name, (uint64_t)Addr);
Andrew Kaylor7b170502013-02-20 18:09:21 +0000484 DEBUG(dbgs() << "Resolving relocations Name: " << Name
485 << "\t" << format("%p", Addr)
486 << "\n");
487 resolveRelocationList(Relocs, (uintptr_t)Addr);
488 }
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000489 } else {
Eli Bendersky37bc5a22012-04-30 12:15:58 +0000490 report_fatal_error("Expected external symbol");
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000491 }
492 }
493}
494
495
Jim Grosbach6e563312011-03-21 22:15:52 +0000496//===----------------------------------------------------------------------===//
497// RuntimeDyld class implementation
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000498RuntimeDyld::RuntimeDyld(RTDyldMemoryManager *mm) {
Andrew Kaylor53608a32012-11-15 23:50:01 +0000499 // FIXME: There's a potential issue lurking here if a single instance of
500 // RuntimeDyld is used to load multiple objects. The current implementation
501 // associates a single memory manager with a RuntimeDyld instance. Even
502 // though the public class spawns a new 'impl' instance for each load,
503 // they share a single memory manager. This can become a problem when page
504 // permissions are applied.
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000505 Dyld = 0;
506 MM = mm;
Jim Grosbach6e563312011-03-21 22:15:52 +0000507}
508
509RuntimeDyld::~RuntimeDyld() {
510 delete Dyld;
511}
512
Andrew Kaylor3f23cef2012-10-02 21:18:39 +0000513ObjectImage *RuntimeDyld::loadObject(ObjectBuffer *InputBuffer) {
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000514 if (!Dyld) {
Rafael Espindola3ecfcc22013-06-11 18:01:14 +0000515 sys::fs::file_magic Type =
516 sys::fs::identify_magic(InputBuffer->getBuffer());
517 switch (Type) {
518 case sys::fs::file_magic::elf_relocatable:
519 case sys::fs::file_magic::elf_executable:
520 case sys::fs::file_magic::elf_shared_object:
521 case sys::fs::file_magic::elf_core:
522 Dyld = new RuntimeDyldELF(MM);
523 break;
524 case sys::fs::file_magic::macho_object:
525 case sys::fs::file_magic::macho_executable:
526 case sys::fs::file_magic::macho_fixed_virtual_memory_shared_lib:
527 case sys::fs::file_magic::macho_core:
528 case sys::fs::file_magic::macho_preload_executable:
529 case sys::fs::file_magic::macho_dynamically_linked_shared_lib:
530 case sys::fs::file_magic::macho_dynamic_linker:
531 case sys::fs::file_magic::macho_bundle:
532 case sys::fs::file_magic::macho_dynamically_linked_shared_lib_stub:
533 case sys::fs::file_magic::macho_dsym_companion:
534 Dyld = new RuntimeDyldMachO(MM);
535 break;
536 case sys::fs::file_magic::unknown:
537 case sys::fs::file_magic::bitcode:
538 case sys::fs::file_magic::archive:
539 case sys::fs::file_magic::coff_object:
540 case sys::fs::file_magic::pecoff_executable:
Alexey Samsonov9c22f872013-06-18 15:03:28 +0000541 case sys::fs::file_magic::macho_universal_binary:
Rafael Espindola3ecfcc22013-06-11 18:01:14 +0000542 report_fatal_error("Incompatible object format!");
Eli Benderskya66a1852012-01-16 08:56:09 +0000543 }
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000544 } else {
Eli Benderskya66a1852012-01-16 08:56:09 +0000545 if (!Dyld->isCompatibleFormat(InputBuffer))
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000546 report_fatal_error("Incompatible object format!");
547 }
548
Jim Grosbach6e563312011-03-21 22:15:52 +0000549 return Dyld->loadObject(InputBuffer);
550}
551
Jim Grosbachb0271052011-04-08 17:31:24 +0000552void *RuntimeDyld::getSymbolAddress(StringRef Name) {
Jim Grosbach6e563312011-03-21 22:15:52 +0000553 return Dyld->getSymbolAddress(Name);
554}
555
Jim Grosbach35ed8422012-09-05 16:50:40 +0000556uint64_t RuntimeDyld::getSymbolLoadAddress(StringRef Name) {
557 return Dyld->getSymbolLoadAddress(Name);
558}
559
Jim Grosbachf8c1c842011-04-12 21:20:41 +0000560void RuntimeDyld::resolveRelocations() {
561 Dyld->resolveRelocations();
562}
563
Jim Grosbach61425c02012-01-16 22:26:39 +0000564void RuntimeDyld::reassignSectionAddress(unsigned SectionID,
565 uint64_t Addr) {
566 Dyld->reassignSectionAddress(SectionID, Addr);
Jim Grosbachf8c1c842011-04-12 21:20:41 +0000567}
568
Jim Grosbache940c1b2012-09-13 21:50:06 +0000569void RuntimeDyld::mapSectionAddress(const void *LocalAddress,
Jim Grosbach020f4e82012-01-16 23:50:55 +0000570 uint64_t TargetAddress) {
571 Dyld->mapSectionAddress(LocalAddress, TargetAddress);
572}
573
Jim Grosbach91dde152011-03-22 18:22:27 +0000574StringRef RuntimeDyld::getErrorString() {
Jim Grosbachb3eecaf2011-03-22 18:19:42 +0000575 return Dyld->getErrorString();
576}
577
Rafael Espindolaa2e40fb2013-05-05 20:43:10 +0000578StringRef RuntimeDyld::getEHFrameSection() {
579 return Dyld->getEHFrameSection();
580}
581
Jim Grosbach6e563312011-03-21 22:15:52 +0000582} // end namespace llvm