blob: bda32d4154c317081a6c3134d0e8c4f9ea758876 [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
Andrew Kaylor528f6d72013-10-11 21:25:48 +000032void RuntimeDyldImpl::registerEHFrames() {
Rafael Espindolaa2e40fb2013-05-05 20:43:10 +000033}
34
Jim Grosbachf8c1c842011-04-12 21:20:41 +000035// Resolve the relocations for all symbols we currently know about.
36void RuntimeDyldImpl::resolveRelocations() {
Preston Gurdc68dda82012-04-12 20:13:57 +000037 // First, resolve relocations associated with external symbols.
Eli Bendersky37bc5a22012-04-30 12:15:58 +000038 resolveExternalSymbols();
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +000039
Jim Grosbach61425c02012-01-16 22:26:39 +000040 // Just iterate over the sections we have and resolve all the relocations
41 // in them. Gross overkill, but it gets the job done.
42 for (int i = 0, e = Sections.size(); i != e; ++i) {
Andrew Kaylor32bd10b2013-08-19 19:38:06 +000043 // The Section here (Sections[i]) refers to the section in which the
44 // symbol for the relocation is located. The SectionID in the relocation
45 // entry provides the section to which the relocation will be applied.
Andrew Kaylor28989882012-11-05 20:57:16 +000046 uint64_t Addr = Sections[i].LoadAddress;
47 DEBUG(dbgs() << "Resolving relocations Section #" << i
48 << "\t" << format("%p", (uint8_t *)Addr)
49 << "\n");
50 resolveRelocationList(Relocations[i], Addr);
Andrew Kaylor8e9ec012013-10-01 01:47:35 +000051 Relocations.erase(i);
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
Andrew Kaylorab950f52013-10-15 20:44:55 +000077 // Save information about our target
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +000078 Arch = (Triple::ArchType)obj->getArch();
Andrew Kaylorab950f52013-10-15 20:44:55 +000079 IsTargetLittleEndian = obj->getObjectFile()->isLittleEndian();
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +000080
Eli Benderskyd98c9e92012-05-01 06:58:59 +000081 // Symbols found in this object
82 StringMap<SymbolLoc> LocalSymbols;
83 // Used sections from the object file
84 ObjSectionToIDMap LocalSections;
85
Tim Northoverf00677d2012-10-29 10:47:04 +000086 // Common symbols requiring allocation, with their sizes and alignments
Eli Benderskyd98c9e92012-05-01 06:58:59 +000087 CommonSymbolMap CommonSymbols;
Tim Northoverf00677d2012-10-29 10:47:04 +000088 // Maximum required total memory to allocate all common symbols
Eli Benderskyd98c9e92012-05-01 06:58:59 +000089 uint64_t CommonSize = 0;
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +000090
91 error_code err;
92 // Parse symbols
93 DEBUG(dbgs() << "Parse symbols:\n");
94 for (symbol_iterator i = obj->begin_symbols(), e = obj->end_symbols();
95 i != e; i.increment(err)) {
96 Check(err);
97 object::SymbolRef::Type SymType;
98 StringRef Name;
99 Check(i->getType(SymType));
100 Check(i->getName(Name));
101
Preston Gurdc68dda82012-04-12 20:13:57 +0000102 uint32_t flags;
103 Check(i->getFlags(flags));
104
105 bool isCommon = flags & SymbolRef::SF_Common;
106 if (isCommon) {
107 // Add the common symbols to a list. We'll allocate them all below.
Rafael Espindola59a0e792013-04-29 22:24:22 +0000108 uint32_t Align;
109 Check(i->getAlignment(Align));
Preston Gurdc68dda82012-04-12 20:13:57 +0000110 uint64_t Size = 0;
111 Check(i->getSize(Size));
Tim Northoverf00677d2012-10-29 10:47:04 +0000112 CommonSize += Size + Align;
113 CommonSymbols[*i] = CommonSymbolInfo(Size, Align);
Preston Gurdc68dda82012-04-12 20:13:57 +0000114 } else {
115 if (SymType == object::SymbolRef::ST_Function ||
Akira Hatanakab889e0c2012-08-17 21:28:04 +0000116 SymType == object::SymbolRef::ST_Data ||
117 SymType == object::SymbolRef::ST_Unknown) {
Preston Gurdc68dda82012-04-12 20:13:57 +0000118 uint64_t FileOffset;
Eli Bendersky5fe01982012-04-29 12:40:47 +0000119 StringRef SectionData;
Tim Northover78822132012-12-17 17:59:35 +0000120 bool IsCode;
Preston Gurdc68dda82012-04-12 20:13:57 +0000121 section_iterator si = obj->end_sections();
122 Check(i->getFileOffset(FileOffset));
123 Check(i->getSection(si));
124 if (si == obj->end_sections()) continue;
Eli Bendersky5fe01982012-04-29 12:40:47 +0000125 Check(si->getContents(SectionData));
Tim Northover78822132012-12-17 17:59:35 +0000126 Check(si->isText(IsCode));
Preston Gurdc68dda82012-04-12 20:13:57 +0000127 const uint8_t* SymPtr = (const uint8_t*)InputBuffer->getBufferStart() +
128 (uintptr_t)FileOffset;
Eli Bendersky5fe01982012-04-29 12:40:47 +0000129 uintptr_t SectOffset = (uintptr_t)(SymPtr -
130 (const uint8_t*)SectionData.begin());
Tim Northover78822132012-12-17 17:59:35 +0000131 unsigned SectionID = findOrEmitSection(*obj, *si, IsCode, LocalSections);
Preston Gurdc68dda82012-04-12 20:13:57 +0000132 LocalSymbols[Name.data()] = SymbolLoc(SectionID, SectOffset);
133 DEBUG(dbgs() << "\tFileOffset: " << format("%p", (uintptr_t)FileOffset)
134 << " flags: " << flags
135 << " SID: " << SectionID
136 << " Offset: " << format("%p", SectOffset));
Amara Emerson098d6d52012-11-16 11:11:59 +0000137 GlobalSymbolTable[Name] = SymbolLoc(SectionID, SectOffset);
Preston Gurdc68dda82012-04-12 20:13:57 +0000138 }
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000139 }
140 DEBUG(dbgs() << "\tType: " << SymType << " Name: " << Name << "\n");
141 }
142
Preston Gurdc68dda82012-04-12 20:13:57 +0000143 // Allocate common symbols
144 if (CommonSize != 0)
Preston Gurd689ff9c2012-04-16 22:12:58 +0000145 emitCommonSymbols(*obj, CommonSymbols, CommonSize, LocalSymbols);
Preston Gurdc68dda82012-04-12 20:13:57 +0000146
Eli Bendersky5fe01982012-04-29 12:40:47 +0000147 // Parse and process relocations
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000148 DEBUG(dbgs() << "Parse relocations:\n");
149 for (section_iterator si = obj->begin_sections(),
150 se = obj->end_sections(); si != se; si.increment(err)) {
151 Check(err);
152 bool isFirstRelocation = true;
153 unsigned SectionID = 0;
154 StubMap Stubs;
Rafael Espindola7486d922013-05-30 03:05:14 +0000155 section_iterator RelocatedSection = si->getRelocatedSection();
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000156
157 for (relocation_iterator i = si->begin_relocations(),
158 e = si->end_relocations(); i != e; i.increment(err)) {
159 Check(err);
160
Eli Bendersky5fe01982012-04-29 12:40:47 +0000161 // If it's the first relocation in this section, find its SectionID
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000162 if (isFirstRelocation) {
Rafael Espindola7486d922013-05-30 03:05:14 +0000163 SectionID =
164 findOrEmitSection(*obj, *RelocatedSection, true, LocalSections);
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000165 DEBUG(dbgs() << "\tSectionID: " << SectionID << "\n");
166 isFirstRelocation = false;
167 }
168
Rafael Espindolaca0e7362013-04-29 19:03:21 +0000169 processRelocationRef(SectionID, *i, *obj, LocalSections, LocalSymbols,
Rafael Espindolaefa91f62013-04-29 14:44:23 +0000170 Stubs);
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000171 }
172 }
Preston Gurd689ff9c2012-04-16 22:12:58 +0000173
Andrew Kaylorff9fa052013-08-19 23:27:43 +0000174 // Give the subclasses a chance to tie-up any loose ends.
Andrew Kaylor528f6d72013-10-11 21:25:48 +0000175 finalizeLoad(LocalSections);
Andrew Kaylorff9fa052013-08-19 23:27:43 +0000176
Andrew Kaylor3f23cef2012-10-02 21:18:39 +0000177 return obj.take();
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000178}
179
Eli Benderskyc201e6e2012-05-01 10:41:12 +0000180void RuntimeDyldImpl::emitCommonSymbols(ObjectImage &Obj,
181 const CommonSymbolMap &CommonSymbols,
182 uint64_t TotalSize,
183 SymbolTableMap &SymbolTable) {
Preston Gurdc68dda82012-04-12 20:13:57 +0000184 // Allocate memory for the section
185 unsigned SectionID = Sections.size();
Filip Pizlo6eb43d22013-10-02 00:59:25 +0000186 uint8_t *Addr = MemMgr->allocateDataSection(
187 TotalSize, sizeof(void*), SectionID, StringRef(), false);
Preston Gurdc68dda82012-04-12 20:13:57 +0000188 if (!Addr)
189 report_fatal_error("Unable to allocate memory for common symbols!");
190 uint64_t Offset = 0;
Rafael Espindolaa2e40fb2013-05-05 20:43:10 +0000191 Sections.push_back(SectionEntry(StringRef(), Addr, TotalSize, 0));
Preston Gurdc68dda82012-04-12 20:13:57 +0000192 memset(Addr, 0, TotalSize);
193
194 DEBUG(dbgs() << "emitCommonSection SectionID: " << SectionID
195 << " new addr: " << format("%p", Addr)
196 << " DataSize: " << TotalSize
197 << "\n");
198
199 // Assign the address of each symbol
Eli Benderskyc201e6e2012-05-01 10:41:12 +0000200 for (CommonSymbolMap::const_iterator it = CommonSymbols.begin(),
201 itEnd = CommonSymbols.end(); it != itEnd; it++) {
Tim Northoverf00677d2012-10-29 10:47:04 +0000202 uint64_t Size = it->second.first;
203 uint64_t Align = it->second.second;
Preston Gurdc68dda82012-04-12 20:13:57 +0000204 StringRef Name;
205 it->first.getName(Name);
Tim Northoverf00677d2012-10-29 10:47:04 +0000206 if (Align) {
207 // This symbol has an alignment requirement.
208 uint64_t AlignOffset = OffsetToAlignment((uint64_t)Addr, Align);
209 Addr += AlignOffset;
210 Offset += AlignOffset;
211 DEBUG(dbgs() << "Allocating common symbol " << Name << " address " <<
Andrew Kaylor5fc8c7c2012-11-01 19:49:21 +0000212 format("%p\n", Addr));
Tim Northoverf00677d2012-10-29 10:47:04 +0000213 }
Preston Gurd689ff9c2012-04-16 22:12:58 +0000214 Obj.updateSymbolAddress(it->first, (uint64_t)Addr);
Eli Benderskyc201e6e2012-05-01 10:41:12 +0000215 SymbolTable[Name.data()] = SymbolLoc(SectionID, Offset);
Preston Gurdc68dda82012-04-12 20:13:57 +0000216 Offset += Size;
217 Addr += Size;
218 }
Preston Gurdc68dda82012-04-12 20:13:57 +0000219}
220
Preston Gurd689ff9c2012-04-16 22:12:58 +0000221unsigned RuntimeDyldImpl::emitSection(ObjectImage &Obj,
222 const SectionRef &Section,
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000223 bool IsCode) {
224
225 unsigned StubBufSize = 0,
226 StubSize = getMaxStubSize();
227 error_code err;
Rafael Espindola7486d922013-05-30 03:05:14 +0000228 const ObjectFile *ObjFile = Obj.getObjectFile();
229 // FIXME: this is an inefficient way to handle this. We should computed the
230 // necessary section allocation size in loadObject by walking all the sections
231 // once.
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000232 if (StubSize > 0) {
Rafael Espindola7486d922013-05-30 03:05:14 +0000233 for (section_iterator SI = ObjFile->begin_sections(),
234 SE = ObjFile->end_sections();
235 SI != SE; SI.increment(err), Check(err)) {
236 section_iterator RelSecI = SI->getRelocatedSection();
237 if (!(RelSecI == Section))
238 continue;
239
240 for (relocation_iterator I = SI->begin_relocations(),
241 E = SI->end_relocations(); I != E; I.increment(err), Check(err)) {
242 StubBufSize += StubSize;
243 }
244 }
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000245 }
Rafael Espindola7486d922013-05-30 03:05:14 +0000246
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000247 StringRef data;
248 uint64_t Alignment64;
249 Check(Section.getContents(data));
250 Check(Section.getAlignment(Alignment64));
251
252 unsigned Alignment = (unsigned)Alignment64 & 0xffffffffL;
Preston Gurdc68dda82012-04-12 20:13:57 +0000253 bool IsRequired;
254 bool IsVirtual;
255 bool IsZeroInit;
Andrew Kaylor53608a32012-11-15 23:50:01 +0000256 bool IsReadOnly;
Preston Gurdc68dda82012-04-12 20:13:57 +0000257 uint64_t DataSize;
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +0000258 StringRef Name;
Preston Gurdc68dda82012-04-12 20:13:57 +0000259 Check(Section.isRequiredForExecution(IsRequired));
260 Check(Section.isVirtual(IsVirtual));
261 Check(Section.isZeroInit(IsZeroInit));
Andrew Kaylor53608a32012-11-15 23:50:01 +0000262 Check(Section.isReadOnlyData(IsReadOnly));
Preston Gurdc68dda82012-04-12 20:13:57 +0000263 Check(Section.getSize(DataSize));
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +0000264 Check(Section.getName(Name));
Richard Sandiford6fc2ad62013-05-03 14:15:35 +0000265 if (StubSize > 0) {
266 unsigned StubAlignment = getStubAlignment();
267 unsigned EndAlignment = (DataSize | Alignment) & -(DataSize | Alignment);
268 if (StubAlignment > EndAlignment)
269 StubBufSize += StubAlignment - EndAlignment;
270 }
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000271
Preston Gurdc68dda82012-04-12 20:13:57 +0000272 unsigned Allocate;
273 unsigned SectionID = Sections.size();
274 uint8_t *Addr;
275 const char *pData = 0;
276
277 // Some sections, such as debug info, don't need to be loaded for execution.
278 // Leave those where they are.
279 if (IsRequired) {
280 Allocate = DataSize + StubBufSize;
281 Addr = IsCode
Filip Pizlo6eb43d22013-10-02 00:59:25 +0000282 ? MemMgr->allocateCodeSection(Allocate, Alignment, SectionID, Name)
283 : MemMgr->allocateDataSection(Allocate, Alignment, SectionID, Name,
284 IsReadOnly);
Preston Gurdc68dda82012-04-12 20:13:57 +0000285 if (!Addr)
286 report_fatal_error("Unable to allocate section memory!");
287
288 // Virtual sections have no data in the object image, so leave pData = 0
289 if (!IsVirtual)
290 pData = data.data();
291
292 // Zero-initialize or copy the data from the image
293 if (IsZeroInit || IsVirtual)
294 memset(Addr, 0, DataSize);
295 else
296 memcpy(Addr, pData, DataSize);
297
298 DEBUG(dbgs() << "emitSection SectionID: " << SectionID
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +0000299 << " Name: " << Name
Preston Gurdc68dda82012-04-12 20:13:57 +0000300 << " obj addr: " << format("%p", pData)
301 << " new addr: " << format("%p", Addr)
302 << " DataSize: " << DataSize
303 << " StubBufSize: " << StubBufSize
304 << " Allocate: " << Allocate
305 << "\n");
Preston Gurd689ff9c2012-04-16 22:12:58 +0000306 Obj.updateSectionAddress(Section, (uint64_t)Addr);
Preston Gurdc68dda82012-04-12 20:13:57 +0000307 }
308 else {
309 // Even if we didn't load the section, we need to record an entry for it
Eli Bendersky5fe01982012-04-29 12:40:47 +0000310 // to handle later processing (and by 'handle' I mean don't do anything
311 // with these sections).
Preston Gurdc68dda82012-04-12 20:13:57 +0000312 Allocate = 0;
313 Addr = 0;
314 DEBUG(dbgs() << "emitSection SectionID: " << SectionID
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +0000315 << " Name: " << Name
Preston Gurdc68dda82012-04-12 20:13:57 +0000316 << " obj addr: " << format("%p", data.data())
317 << " new addr: 0"
318 << " DataSize: " << DataSize
319 << " StubBufSize: " << StubBufSize
320 << " Allocate: " << Allocate
321 << "\n");
322 }
323
Rafael Espindolaa2e40fb2013-05-05 20:43:10 +0000324 Sections.push_back(SectionEntry(Name, Addr, DataSize, (uintptr_t)pData));
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000325 return SectionID;
326}
327
Preston Gurd689ff9c2012-04-16 22:12:58 +0000328unsigned RuntimeDyldImpl::findOrEmitSection(ObjectImage &Obj,
329 const SectionRef &Section,
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000330 bool IsCode,
331 ObjSectionToIDMap &LocalSections) {
332
333 unsigned SectionID = 0;
334 ObjSectionToIDMap::iterator i = LocalSections.find(Section);
335 if (i != LocalSections.end())
336 SectionID = i->second;
337 else {
Preston Gurd689ff9c2012-04-16 22:12:58 +0000338 SectionID = emitSection(Obj, Section, IsCode);
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000339 LocalSections[Section] = SectionID;
340 }
341 return SectionID;
342}
343
Eli Benderskyc201e6e2012-05-01 10:41:12 +0000344void RuntimeDyldImpl::addRelocationForSection(const RelocationEntry &RE,
345 unsigned SectionID) {
346 Relocations[SectionID].push_back(RE);
347}
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000348
Eli Benderskyc201e6e2012-05-01 10:41:12 +0000349void RuntimeDyldImpl::addRelocationForSymbol(const RelocationEntry &RE,
350 StringRef SymbolName) {
351 // Relocation by symbol. If the symbol is found in the global symbol table,
352 // create an appropriate section relocation. Otherwise, add it to
353 // ExternalSymbolRelocations.
354 SymbolTableMap::const_iterator Loc =
355 GlobalSymbolTable.find(SymbolName);
356 if (Loc == GlobalSymbolTable.end()) {
357 ExternalSymbolRelocations[SymbolName].push_back(RE);
Eli Bendersky37bc5a22012-04-30 12:15:58 +0000358 } else {
Eli Benderskyc201e6e2012-05-01 10:41:12 +0000359 // Copy the RE since we want to modify its addend.
360 RelocationEntry RECopy = RE;
361 RECopy.Addend += Loc->second.second;
362 Relocations[Loc->second.first].push_back(RECopy);
Eli Bendersky37bc5a22012-04-30 12:15:58 +0000363 }
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000364}
365
366uint8_t *RuntimeDyldImpl::createStubFunction(uint8_t *Addr) {
Tim Northover4a9b6b72013-05-04 20:14:09 +0000367 if (Arch == Triple::aarch64) {
368 // This stub has to be able to access the full address space,
369 // since symbol lookup won't necessarily find a handy, in-range,
370 // PLT stub for functions which could be anywhere.
371 uint32_t *StubAddr = (uint32_t*)Addr;
372
373 // Stub can use ip0 (== x16) to calculate address
374 *StubAddr = 0xd2e00010; // movz ip0, #:abs_g3:<addr>
375 StubAddr++;
376 *StubAddr = 0xf2c00010; // movk ip0, #:abs_g2_nc:<addr>
377 StubAddr++;
378 *StubAddr = 0xf2a00010; // movk ip0, #:abs_g1_nc:<addr>
379 StubAddr++;
380 *StubAddr = 0xf2800010; // movk ip0, #:abs_g0_nc:<addr>
381 StubAddr++;
382 *StubAddr = 0xd61f0200; // br ip0
383
384 return Addr;
385 } else if (Arch == Triple::arm) {
Akira Hatanakab889e0c2012-08-17 21:28:04 +0000386 // TODO: There is only ARM far stub now. We should add the Thumb stub,
387 // and stubs for branches Thumb - ARM and ARM - Thumb.
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000388 uint32_t *StubAddr = (uint32_t*)Addr;
389 *StubAddr = 0xe51ff004; // ldr pc,<label>
390 return (uint8_t*)++StubAddr;
NAKAMURA Takumi4af1ca52012-12-04 00:08:14 +0000391 } else if (Arch == Triple::mipsel || Arch == Triple::mips) {
Akira Hatanakab889e0c2012-08-17 21:28:04 +0000392 uint32_t *StubAddr = (uint32_t*)Addr;
393 // 0: 3c190000 lui t9,%hi(addr).
394 // 4: 27390000 addiu t9,t9,%lo(addr).
395 // 8: 03200008 jr t9.
396 // c: 00000000 nop.
397 const unsigned LuiT9Instr = 0x3c190000, AdduiT9Instr = 0x27390000;
398 const unsigned JrT9Instr = 0x03200008, NopInstr = 0x0;
399
400 *StubAddr = LuiT9Instr;
401 StubAddr++;
402 *StubAddr = AdduiT9Instr;
403 StubAddr++;
404 *StubAddr = JrT9Instr;
405 StubAddr++;
406 *StubAddr = NopInstr;
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000407 return Addr;
Bill Schmidtf38cc382013-07-26 01:35:43 +0000408 } else if (Arch == Triple::ppc64 || Arch == Triple::ppc64le) {
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +0000409 // PowerPC64 stub: the address points to a function descriptor
410 // instead of the function itself. Load the function address
411 // on r11 and sets it to control register. Also loads the function
412 // TOC in r2 and environment pointer to r11.
413 writeInt32BE(Addr, 0x3D800000); // lis r12, highest(addr)
414 writeInt32BE(Addr+4, 0x618C0000); // ori r12, higher(addr)
415 writeInt32BE(Addr+8, 0x798C07C6); // sldi r12, r12, 32
416 writeInt32BE(Addr+12, 0x658C0000); // oris r12, r12, h(addr)
417 writeInt32BE(Addr+16, 0x618C0000); // ori r12, r12, l(addr)
418 writeInt32BE(Addr+20, 0xF8410028); // std r2, 40(r1)
419 writeInt32BE(Addr+24, 0xE96C0000); // ld r11, 0(r12)
420 writeInt32BE(Addr+28, 0xE84C0008); // ld r2, 0(r12)
421 writeInt32BE(Addr+32, 0x7D6903A6); // mtctr r11
422 writeInt32BE(Addr+36, 0xE96C0010); // ld r11, 16(r2)
423 writeInt32BE(Addr+40, 0x4E800420); // bctr
Andrew Kaylor5fc8c7c2012-11-01 19:49:21 +0000424
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +0000425 return Addr;
Richard Sandiford6fc2ad62013-05-03 14:15:35 +0000426 } else if (Arch == Triple::systemz) {
427 writeInt16BE(Addr, 0xC418); // lgrl %r1,.+8
428 writeInt16BE(Addr+2, 0x0000);
429 writeInt16BE(Addr+4, 0x0004);
430 writeInt16BE(Addr+6, 0x07F1); // brc 15,%r1
431 // 8-byte address stored at Addr + 8
432 return Addr;
Andrew Kaylorff9fa052013-08-19 23:27:43 +0000433 } else if (Arch == Triple::x86_64) {
434 *Addr = 0xFF; // jmp
435 *(Addr+1) = 0x25; // rip
436 // 32-bit PC-relative address of the GOT entry will be stored at Addr+2
Akira Hatanakab889e0c2012-08-17 21:28:04 +0000437 }
438 return Addr;
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000439}
440
441// Assign an address to a symbol name and resolve all the relocations
442// associated with it.
443void RuntimeDyldImpl::reassignSectionAddress(unsigned SectionID,
444 uint64_t Addr) {
445 // The address to use for relocation resolution is not
446 // the address of the local section buffer. We must be doing
Andrew Kaylor28989882012-11-05 20:57:16 +0000447 // a remote execution environment of some sort. Relocations can't
448 // be applied until all the sections have been moved. The client must
449 // trigger this with a call to MCJIT::finalize() or
450 // RuntimeDyld::resolveRelocations().
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000451 //
452 // Addr is a uint64_t because we can't assume the pointer width
453 // of the target is the same as that of the host. Just use a generic
454 // "big enough" type.
455 Sections[SectionID].LoadAddress = Addr;
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000456}
457
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000458void RuntimeDyldImpl::resolveRelocationList(const RelocationList &Relocs,
459 uint64_t Value) {
460 for (unsigned i = 0, e = Relocs.size(); i != e; ++i) {
Rafael Espindola87b50172013-04-29 17:24:34 +0000461 const RelocationEntry &RE = Relocs[i];
462 // Ignore relocations for sections that were not loaded
463 if (Sections[RE.SectionID].Address == 0)
464 continue;
465 resolveRelocation(RE, Value);
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000466 }
467}
468
Eli Bendersky37bc5a22012-04-30 12:15:58 +0000469void RuntimeDyldImpl::resolveExternalSymbols() {
Andrew Kaylor8e9ec012013-10-01 01:47:35 +0000470 while(!ExternalSymbolRelocations.empty()) {
471 StringMap<RelocationList>::iterator i = ExternalSymbolRelocations.begin();
472
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000473 StringRef Name = i->first();
474 RelocationList &Relocs = i->second;
Andrew Kaylor8e9ec012013-10-01 01:47:35 +0000475 if (Name.size() == 0) {
476 // This is an absolute symbol, use an address of zero.
477 DEBUG(dbgs() << "Resolving absolute relocations." << "\n");
478 resolveRelocationList(Relocs, 0);
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000479 } else {
Andrew Kaylor8e9ec012013-10-01 01:47:35 +0000480 uint64_t Addr = 0;
481 SymbolTableMap::const_iterator Loc = GlobalSymbolTable.find(Name);
482 if (Loc == GlobalSymbolTable.end()) {
483 // This is an external symbol, try to get its address from
484 // MemoryManager.
485 Addr = MemMgr->getSymbolAddress(Name.data());
486 } else {
487 // We found the symbol in our global table. It was probably in a
488 // Module that we loaded previously.
489 SymbolLoc SymLoc = GlobalSymbolTable.lookup(Name);
490 Addr = getSectionLoadAddress(SymLoc.first) + SymLoc.second;
491 }
492
493 // FIXME: Implement error handling that doesn't kill the host program!
494 if (!Addr)
495 report_fatal_error("Program used external function '" + Name +
496 "' which could not be resolved!");
497
498 updateGOTEntries(Name, Addr);
499 DEBUG(dbgs() << "Resolving relocations Name: " << Name
500 << "\t" << format("0x%lx", Addr)
501 << "\n");
502 resolveRelocationList(Relocs, Addr);
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000503 }
Andrew Kaylor8e9ec012013-10-01 01:47:35 +0000504
505 ExternalSymbolRelocations.erase(i->first());
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000506 }
507}
508
509
Jim Grosbach6e563312011-03-21 22:15:52 +0000510//===----------------------------------------------------------------------===//
511// RuntimeDyld class implementation
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000512RuntimeDyld::RuntimeDyld(RTDyldMemoryManager *mm) {
Andrew Kaylor53608a32012-11-15 23:50:01 +0000513 // FIXME: There's a potential issue lurking here if a single instance of
514 // RuntimeDyld is used to load multiple objects. The current implementation
515 // associates a single memory manager with a RuntimeDyld instance. Even
516 // though the public class spawns a new 'impl' instance for each load,
517 // they share a single memory manager. This can become a problem when page
518 // permissions are applied.
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000519 Dyld = 0;
520 MM = mm;
Jim Grosbach6e563312011-03-21 22:15:52 +0000521}
522
523RuntimeDyld::~RuntimeDyld() {
524 delete Dyld;
525}
526
Andrew Kaylor3f23cef2012-10-02 21:18:39 +0000527ObjectImage *RuntimeDyld::loadObject(ObjectBuffer *InputBuffer) {
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000528 if (!Dyld) {
Rafael Espindola3ecfcc22013-06-11 18:01:14 +0000529 sys::fs::file_magic Type =
530 sys::fs::identify_magic(InputBuffer->getBuffer());
531 switch (Type) {
532 case sys::fs::file_magic::elf_relocatable:
533 case sys::fs::file_magic::elf_executable:
534 case sys::fs::file_magic::elf_shared_object:
535 case sys::fs::file_magic::elf_core:
536 Dyld = new RuntimeDyldELF(MM);
537 break;
538 case sys::fs::file_magic::macho_object:
539 case sys::fs::file_magic::macho_executable:
540 case sys::fs::file_magic::macho_fixed_virtual_memory_shared_lib:
541 case sys::fs::file_magic::macho_core:
542 case sys::fs::file_magic::macho_preload_executable:
543 case sys::fs::file_magic::macho_dynamically_linked_shared_lib:
544 case sys::fs::file_magic::macho_dynamic_linker:
545 case sys::fs::file_magic::macho_bundle:
546 case sys::fs::file_magic::macho_dynamically_linked_shared_lib_stub:
547 case sys::fs::file_magic::macho_dsym_companion:
548 Dyld = new RuntimeDyldMachO(MM);
549 break;
550 case sys::fs::file_magic::unknown:
551 case sys::fs::file_magic::bitcode:
552 case sys::fs::file_magic::archive:
553 case sys::fs::file_magic::coff_object:
554 case sys::fs::file_magic::pecoff_executable:
Alexey Samsonov9c22f872013-06-18 15:03:28 +0000555 case sys::fs::file_magic::macho_universal_binary:
Rui Ueyamab32b0372013-10-15 22:45:38 +0000556 case sys::fs::file_magic::windows_resource:
Rafael Espindola3ecfcc22013-06-11 18:01:14 +0000557 report_fatal_error("Incompatible object format!");
Eli Benderskya66a1852012-01-16 08:56:09 +0000558 }
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000559 } else {
Eli Benderskya66a1852012-01-16 08:56:09 +0000560 if (!Dyld->isCompatibleFormat(InputBuffer))
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000561 report_fatal_error("Incompatible object format!");
562 }
563
Jim Grosbach6e563312011-03-21 22:15:52 +0000564 return Dyld->loadObject(InputBuffer);
565}
566
Jim Grosbachb0271052011-04-08 17:31:24 +0000567void *RuntimeDyld::getSymbolAddress(StringRef Name) {
Andrew Kaylor8e9ec012013-10-01 01:47:35 +0000568 if (!Dyld)
569 return NULL;
Jim Grosbach6e563312011-03-21 22:15:52 +0000570 return Dyld->getSymbolAddress(Name);
571}
572
Jim Grosbach35ed8422012-09-05 16:50:40 +0000573uint64_t RuntimeDyld::getSymbolLoadAddress(StringRef Name) {
Andrew Kaylor8e9ec012013-10-01 01:47:35 +0000574 if (!Dyld)
575 return 0;
Jim Grosbach35ed8422012-09-05 16:50:40 +0000576 return Dyld->getSymbolLoadAddress(Name);
577}
578
Jim Grosbachf8c1c842011-04-12 21:20:41 +0000579void RuntimeDyld::resolveRelocations() {
580 Dyld->resolveRelocations();
581}
582
Jim Grosbach61425c02012-01-16 22:26:39 +0000583void RuntimeDyld::reassignSectionAddress(unsigned SectionID,
584 uint64_t Addr) {
585 Dyld->reassignSectionAddress(SectionID, Addr);
Jim Grosbachf8c1c842011-04-12 21:20:41 +0000586}
587
Jim Grosbache940c1b2012-09-13 21:50:06 +0000588void RuntimeDyld::mapSectionAddress(const void *LocalAddress,
Jim Grosbach020f4e82012-01-16 23:50:55 +0000589 uint64_t TargetAddress) {
590 Dyld->mapSectionAddress(LocalAddress, TargetAddress);
591}
592
Jim Grosbach91dde152011-03-22 18:22:27 +0000593StringRef RuntimeDyld::getErrorString() {
Jim Grosbachb3eecaf2011-03-22 18:19:42 +0000594 return Dyld->getErrorString();
595}
596
Andrew Kaylor528f6d72013-10-11 21:25:48 +0000597void RuntimeDyld::registerEHFrames() {
598 return Dyld->registerEHFrames();
Rafael Espindolaa2e40fb2013-05-05 20:43:10 +0000599}
600
Jim Grosbach6e563312011-03-21 22:15:52 +0000601} // end namespace llvm