blob: 844f1c2931cb8cc20dca89a2c02626ee34cc7153 [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.
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
Rafael Espindolaa2e40fb2013-05-05 20:43:10 +000031StringRef RuntimeDyldImpl::getEHFrameSection() {
32 return StringRef();
33}
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 Kaylor28989882012-11-05 20:57:16 +000043 uint64_t Addr = Sections[i].LoadAddress;
44 DEBUG(dbgs() << "Resolving relocations Section #" << i
45 << "\t" << format("%p", (uint8_t *)Addr)
46 << "\n");
47 resolveRelocationList(Relocations[i], Addr);
Jim Grosbach61425c02012-01-16 22:26:39 +000048 }
Jim Grosbachf8c1c842011-04-12 21:20:41 +000049}
50
Jim Grosbache940c1b2012-09-13 21:50:06 +000051void RuntimeDyldImpl::mapSectionAddress(const void *LocalAddress,
Jim Grosbach020f4e82012-01-16 23:50:55 +000052 uint64_t TargetAddress) {
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +000053 for (unsigned i = 0, e = Sections.size(); i != e; ++i) {
54 if (Sections[i].Address == LocalAddress) {
55 reassignSectionAddress(i, TargetAddress);
56 return;
57 }
58 }
59 llvm_unreachable("Attempting to remap address of unknown section!");
Jim Grosbach020f4e82012-01-16 23:50:55 +000060}
61
Eli Bendersky5fe01982012-04-29 12:40:47 +000062// Subclasses can implement this method to create specialized image instances.
Sylvestre Ledruc8e41c52012-07-23 08:51:15 +000063// The caller owns the pointer that is returned.
Andrew Kaylor3f23cef2012-10-02 21:18:39 +000064ObjectImage *RuntimeDyldImpl::createObjectImage(ObjectBuffer *InputBuffer) {
65 return new ObjectImageCommon(InputBuffer);
Preston Gurd689ff9c2012-04-16 22:12:58 +000066}
67
Andrew Kaylor3f23cef2012-10-02 21:18:39 +000068ObjectImage *RuntimeDyldImpl::loadObject(ObjectBuffer *InputBuffer) {
Preston Gurd689ff9c2012-04-16 22:12:58 +000069 OwningPtr<ObjectImage> obj(createObjectImage(InputBuffer));
Preston Gurdc68dda82012-04-12 20:13:57 +000070 if (!obj)
71 report_fatal_error("Unable to create object image from memory buffer!");
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +000072
73 Arch = (Triple::ArchType)obj->getArch();
74
Eli Benderskyd98c9e92012-05-01 06:58:59 +000075 // Symbols found in this object
76 StringMap<SymbolLoc> LocalSymbols;
77 // Used sections from the object file
78 ObjSectionToIDMap LocalSections;
79
Tim Northoverf00677d2012-10-29 10:47:04 +000080 // Common symbols requiring allocation, with their sizes and alignments
Eli Benderskyd98c9e92012-05-01 06:58:59 +000081 CommonSymbolMap CommonSymbols;
Tim Northoverf00677d2012-10-29 10:47:04 +000082 // Maximum required total memory to allocate all common symbols
Eli Benderskyd98c9e92012-05-01 06:58:59 +000083 uint64_t CommonSize = 0;
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +000084
85 error_code err;
86 // Parse symbols
87 DEBUG(dbgs() << "Parse symbols:\n");
88 for (symbol_iterator i = obj->begin_symbols(), e = obj->end_symbols();
89 i != e; i.increment(err)) {
90 Check(err);
91 object::SymbolRef::Type SymType;
92 StringRef Name;
93 Check(i->getType(SymType));
94 Check(i->getName(Name));
95
Preston Gurdc68dda82012-04-12 20:13:57 +000096 uint32_t flags;
97 Check(i->getFlags(flags));
98
99 bool isCommon = flags & SymbolRef::SF_Common;
100 if (isCommon) {
101 // Add the common symbols to a list. We'll allocate them all below.
Rafael Espindola59a0e792013-04-29 22:24:22 +0000102 uint32_t Align;
103 Check(i->getAlignment(Align));
Preston Gurdc68dda82012-04-12 20:13:57 +0000104 uint64_t Size = 0;
105 Check(i->getSize(Size));
Tim Northoverf00677d2012-10-29 10:47:04 +0000106 CommonSize += Size + Align;
107 CommonSymbols[*i] = CommonSymbolInfo(Size, Align);
Preston Gurdc68dda82012-04-12 20:13:57 +0000108 } else {
109 if (SymType == object::SymbolRef::ST_Function ||
Akira Hatanakab889e0c2012-08-17 21:28:04 +0000110 SymType == object::SymbolRef::ST_Data ||
111 SymType == object::SymbolRef::ST_Unknown) {
Preston Gurdc68dda82012-04-12 20:13:57 +0000112 uint64_t FileOffset;
Eli Bendersky5fe01982012-04-29 12:40:47 +0000113 StringRef SectionData;
Tim Northover78822132012-12-17 17:59:35 +0000114 bool IsCode;
Preston Gurdc68dda82012-04-12 20:13:57 +0000115 section_iterator si = obj->end_sections();
116 Check(i->getFileOffset(FileOffset));
117 Check(i->getSection(si));
118 if (si == obj->end_sections()) continue;
Eli Bendersky5fe01982012-04-29 12:40:47 +0000119 Check(si->getContents(SectionData));
Tim Northover78822132012-12-17 17:59:35 +0000120 Check(si->isText(IsCode));
Preston Gurdc68dda82012-04-12 20:13:57 +0000121 const uint8_t* SymPtr = (const uint8_t*)InputBuffer->getBufferStart() +
122 (uintptr_t)FileOffset;
Eli Bendersky5fe01982012-04-29 12:40:47 +0000123 uintptr_t SectOffset = (uintptr_t)(SymPtr -
124 (const uint8_t*)SectionData.begin());
Tim Northover78822132012-12-17 17:59:35 +0000125 unsigned SectionID = findOrEmitSection(*obj, *si, IsCode, LocalSections);
Preston Gurdc68dda82012-04-12 20:13:57 +0000126 LocalSymbols[Name.data()] = SymbolLoc(SectionID, SectOffset);
127 DEBUG(dbgs() << "\tFileOffset: " << format("%p", (uintptr_t)FileOffset)
128 << " flags: " << flags
129 << " SID: " << SectionID
130 << " Offset: " << format("%p", SectOffset));
Amara Emerson098d6d52012-11-16 11:11:59 +0000131 GlobalSymbolTable[Name] = SymbolLoc(SectionID, SectOffset);
Preston Gurdc68dda82012-04-12 20:13:57 +0000132 }
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000133 }
134 DEBUG(dbgs() << "\tType: " << SymType << " Name: " << Name << "\n");
135 }
136
Preston Gurdc68dda82012-04-12 20:13:57 +0000137 // Allocate common symbols
138 if (CommonSize != 0)
Preston Gurd689ff9c2012-04-16 22:12:58 +0000139 emitCommonSymbols(*obj, CommonSymbols, CommonSize, LocalSymbols);
Preston Gurdc68dda82012-04-12 20:13:57 +0000140
Eli Bendersky5fe01982012-04-29 12:40:47 +0000141 // Parse and process relocations
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000142 DEBUG(dbgs() << "Parse relocations:\n");
143 for (section_iterator si = obj->begin_sections(),
144 se = obj->end_sections(); si != se; si.increment(err)) {
145 Check(err);
146 bool isFirstRelocation = true;
147 unsigned SectionID = 0;
148 StubMap Stubs;
149
150 for (relocation_iterator i = si->begin_relocations(),
151 e = si->end_relocations(); i != e; i.increment(err)) {
152 Check(err);
153
Eli Bendersky5fe01982012-04-29 12:40:47 +0000154 // If it's the first relocation in this section, find its SectionID
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000155 if (isFirstRelocation) {
Preston Gurd689ff9c2012-04-16 22:12:58 +0000156 SectionID = findOrEmitSection(*obj, *si, true, LocalSections);
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000157 DEBUG(dbgs() << "\tSectionID: " << SectionID << "\n");
158 isFirstRelocation = false;
159 }
160
Rafael Espindolaca0e7362013-04-29 19:03:21 +0000161 processRelocationRef(SectionID, *i, *obj, LocalSections, LocalSymbols,
Rafael Espindolaefa91f62013-04-29 14:44:23 +0000162 Stubs);
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000163 }
164 }
Preston Gurd689ff9c2012-04-16 22:12:58 +0000165
Andrew Kaylor3f23cef2012-10-02 21:18:39 +0000166 return obj.take();
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000167}
168
Eli Benderskyc201e6e2012-05-01 10:41:12 +0000169void RuntimeDyldImpl::emitCommonSymbols(ObjectImage &Obj,
170 const CommonSymbolMap &CommonSymbols,
171 uint64_t TotalSize,
172 SymbolTableMap &SymbolTable) {
Preston Gurdc68dda82012-04-12 20:13:57 +0000173 // Allocate memory for the section
174 unsigned SectionID = Sections.size();
175 uint8_t *Addr = MemMgr->allocateDataSection(TotalSize, sizeof(void*),
Andrew Kaylor53608a32012-11-15 23:50:01 +0000176 SectionID, false);
Preston Gurdc68dda82012-04-12 20:13:57 +0000177 if (!Addr)
178 report_fatal_error("Unable to allocate memory for common symbols!");
179 uint64_t Offset = 0;
Rafael Espindolaa2e40fb2013-05-05 20:43:10 +0000180 Sections.push_back(SectionEntry(StringRef(), Addr, TotalSize, 0));
Preston Gurdc68dda82012-04-12 20:13:57 +0000181 memset(Addr, 0, TotalSize);
182
183 DEBUG(dbgs() << "emitCommonSection SectionID: " << SectionID
184 << " new addr: " << format("%p", Addr)
185 << " DataSize: " << TotalSize
186 << "\n");
187
188 // Assign the address of each symbol
Eli Benderskyc201e6e2012-05-01 10:41:12 +0000189 for (CommonSymbolMap::const_iterator it = CommonSymbols.begin(),
190 itEnd = CommonSymbols.end(); it != itEnd; it++) {
Tim Northoverf00677d2012-10-29 10:47:04 +0000191 uint64_t Size = it->second.first;
192 uint64_t Align = it->second.second;
Preston Gurdc68dda82012-04-12 20:13:57 +0000193 StringRef Name;
194 it->first.getName(Name);
Tim Northoverf00677d2012-10-29 10:47:04 +0000195 if (Align) {
196 // This symbol has an alignment requirement.
197 uint64_t AlignOffset = OffsetToAlignment((uint64_t)Addr, Align);
198 Addr += AlignOffset;
199 Offset += AlignOffset;
200 DEBUG(dbgs() << "Allocating common symbol " << Name << " address " <<
Andrew Kaylor5fc8c7c2012-11-01 19:49:21 +0000201 format("%p\n", Addr));
Tim Northoverf00677d2012-10-29 10:47:04 +0000202 }
Preston Gurd689ff9c2012-04-16 22:12:58 +0000203 Obj.updateSymbolAddress(it->first, (uint64_t)Addr);
Eli Benderskyc201e6e2012-05-01 10:41:12 +0000204 SymbolTable[Name.data()] = SymbolLoc(SectionID, Offset);
Preston Gurdc68dda82012-04-12 20:13:57 +0000205 Offset += Size;
206 Addr += Size;
207 }
Preston Gurdc68dda82012-04-12 20:13:57 +0000208}
209
Preston Gurd689ff9c2012-04-16 22:12:58 +0000210unsigned RuntimeDyldImpl::emitSection(ObjectImage &Obj,
211 const SectionRef &Section,
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000212 bool IsCode) {
213
214 unsigned StubBufSize = 0,
215 StubSize = getMaxStubSize();
216 error_code err;
217 if (StubSize > 0) {
218 for (relocation_iterator i = Section.begin_relocations(),
Preston Gurdc68dda82012-04-12 20:13:57 +0000219 e = Section.end_relocations(); i != e; i.increment(err), Check(err))
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000220 StubBufSize += StubSize;
221 }
222 StringRef data;
223 uint64_t Alignment64;
224 Check(Section.getContents(data));
225 Check(Section.getAlignment(Alignment64));
226
227 unsigned Alignment = (unsigned)Alignment64 & 0xffffffffL;
Preston Gurdc68dda82012-04-12 20:13:57 +0000228 bool IsRequired;
229 bool IsVirtual;
230 bool IsZeroInit;
Andrew Kaylor53608a32012-11-15 23:50:01 +0000231 bool IsReadOnly;
Preston Gurdc68dda82012-04-12 20:13:57 +0000232 uint64_t DataSize;
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +0000233 StringRef Name;
Preston Gurdc68dda82012-04-12 20:13:57 +0000234 Check(Section.isRequiredForExecution(IsRequired));
235 Check(Section.isVirtual(IsVirtual));
236 Check(Section.isZeroInit(IsZeroInit));
Andrew Kaylor53608a32012-11-15 23:50:01 +0000237 Check(Section.isReadOnlyData(IsReadOnly));
Preston Gurdc68dda82012-04-12 20:13:57 +0000238 Check(Section.getSize(DataSize));
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +0000239 Check(Section.getName(Name));
Richard Sandiford6fc2ad62013-05-03 14:15:35 +0000240 if (StubSize > 0) {
241 unsigned StubAlignment = getStubAlignment();
242 unsigned EndAlignment = (DataSize | Alignment) & -(DataSize | Alignment);
243 if (StubAlignment > EndAlignment)
244 StubBufSize += StubAlignment - EndAlignment;
245 }
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000246
Preston Gurdc68dda82012-04-12 20:13:57 +0000247 unsigned Allocate;
248 unsigned SectionID = Sections.size();
249 uint8_t *Addr;
250 const char *pData = 0;
251
252 // Some sections, such as debug info, don't need to be loaded for execution.
253 // Leave those where they are.
254 if (IsRequired) {
255 Allocate = DataSize + StubBufSize;
256 Addr = IsCode
257 ? MemMgr->allocateCodeSection(Allocate, Alignment, SectionID)
Andrew Kaylor53608a32012-11-15 23:50:01 +0000258 : MemMgr->allocateDataSection(Allocate, Alignment, SectionID, IsReadOnly);
Preston Gurdc68dda82012-04-12 20:13:57 +0000259 if (!Addr)
260 report_fatal_error("Unable to allocate section memory!");
261
262 // Virtual sections have no data in the object image, so leave pData = 0
263 if (!IsVirtual)
264 pData = data.data();
265
266 // Zero-initialize or copy the data from the image
267 if (IsZeroInit || IsVirtual)
268 memset(Addr, 0, DataSize);
269 else
270 memcpy(Addr, pData, DataSize);
271
272 DEBUG(dbgs() << "emitSection SectionID: " << SectionID
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +0000273 << " Name: " << Name
Preston Gurdc68dda82012-04-12 20:13:57 +0000274 << " obj addr: " << format("%p", pData)
275 << " new addr: " << format("%p", Addr)
276 << " DataSize: " << DataSize
277 << " StubBufSize: " << StubBufSize
278 << " Allocate: " << Allocate
279 << "\n");
Preston Gurd689ff9c2012-04-16 22:12:58 +0000280 Obj.updateSectionAddress(Section, (uint64_t)Addr);
Preston Gurdc68dda82012-04-12 20:13:57 +0000281 }
282 else {
283 // Even if we didn't load the section, we need to record an entry for it
Eli Bendersky5fe01982012-04-29 12:40:47 +0000284 // to handle later processing (and by 'handle' I mean don't do anything
285 // with these sections).
Preston Gurdc68dda82012-04-12 20:13:57 +0000286 Allocate = 0;
287 Addr = 0;
288 DEBUG(dbgs() << "emitSection SectionID: " << SectionID
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +0000289 << " Name: " << Name
Preston Gurdc68dda82012-04-12 20:13:57 +0000290 << " obj addr: " << format("%p", data.data())
291 << " new addr: 0"
292 << " DataSize: " << DataSize
293 << " StubBufSize: " << StubBufSize
294 << " Allocate: " << Allocate
295 << "\n");
296 }
297
Rafael Espindolaa2e40fb2013-05-05 20:43:10 +0000298 Sections.push_back(SectionEntry(Name, Addr, DataSize, (uintptr_t)pData));
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000299 return SectionID;
300}
301
Preston Gurd689ff9c2012-04-16 22:12:58 +0000302unsigned RuntimeDyldImpl::findOrEmitSection(ObjectImage &Obj,
303 const SectionRef &Section,
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000304 bool IsCode,
305 ObjSectionToIDMap &LocalSections) {
306
307 unsigned SectionID = 0;
308 ObjSectionToIDMap::iterator i = LocalSections.find(Section);
309 if (i != LocalSections.end())
310 SectionID = i->second;
311 else {
Preston Gurd689ff9c2012-04-16 22:12:58 +0000312 SectionID = emitSection(Obj, Section, IsCode);
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000313 LocalSections[Section] = SectionID;
314 }
315 return SectionID;
316}
317
Eli Benderskyc201e6e2012-05-01 10:41:12 +0000318void RuntimeDyldImpl::addRelocationForSection(const RelocationEntry &RE,
319 unsigned SectionID) {
320 Relocations[SectionID].push_back(RE);
321}
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000322
Eli Benderskyc201e6e2012-05-01 10:41:12 +0000323void RuntimeDyldImpl::addRelocationForSymbol(const RelocationEntry &RE,
324 StringRef SymbolName) {
325 // Relocation by symbol. If the symbol is found in the global symbol table,
326 // create an appropriate section relocation. Otherwise, add it to
327 // ExternalSymbolRelocations.
328 SymbolTableMap::const_iterator Loc =
329 GlobalSymbolTable.find(SymbolName);
330 if (Loc == GlobalSymbolTable.end()) {
331 ExternalSymbolRelocations[SymbolName].push_back(RE);
Eli Bendersky37bc5a22012-04-30 12:15:58 +0000332 } else {
Eli Benderskyc201e6e2012-05-01 10:41:12 +0000333 // Copy the RE since we want to modify its addend.
334 RelocationEntry RECopy = RE;
335 RECopy.Addend += Loc->second.second;
336 Relocations[Loc->second.first].push_back(RECopy);
Eli Bendersky37bc5a22012-04-30 12:15:58 +0000337 }
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000338}
339
340uint8_t *RuntimeDyldImpl::createStubFunction(uint8_t *Addr) {
Tim Northover4a9b6b72013-05-04 20:14:09 +0000341 if (Arch == Triple::aarch64) {
342 // This stub has to be able to access the full address space,
343 // since symbol lookup won't necessarily find a handy, in-range,
344 // PLT stub for functions which could be anywhere.
345 uint32_t *StubAddr = (uint32_t*)Addr;
346
347 // Stub can use ip0 (== x16) to calculate address
348 *StubAddr = 0xd2e00010; // movz ip0, #:abs_g3:<addr>
349 StubAddr++;
350 *StubAddr = 0xf2c00010; // movk ip0, #:abs_g2_nc:<addr>
351 StubAddr++;
352 *StubAddr = 0xf2a00010; // movk ip0, #:abs_g1_nc:<addr>
353 StubAddr++;
354 *StubAddr = 0xf2800010; // movk ip0, #:abs_g0_nc:<addr>
355 StubAddr++;
356 *StubAddr = 0xd61f0200; // br ip0
357
358 return Addr;
359 } else if (Arch == Triple::arm) {
Akira Hatanakab889e0c2012-08-17 21:28:04 +0000360 // TODO: There is only ARM far stub now. We should add the Thumb stub,
361 // and stubs for branches Thumb - ARM and ARM - Thumb.
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000362 uint32_t *StubAddr = (uint32_t*)Addr;
363 *StubAddr = 0xe51ff004; // ldr pc,<label>
364 return (uint8_t*)++StubAddr;
NAKAMURA Takumi4af1ca52012-12-04 00:08:14 +0000365 } else if (Arch == Triple::mipsel || Arch == Triple::mips) {
Akira Hatanakab889e0c2012-08-17 21:28:04 +0000366 uint32_t *StubAddr = (uint32_t*)Addr;
367 // 0: 3c190000 lui t9,%hi(addr).
368 // 4: 27390000 addiu t9,t9,%lo(addr).
369 // 8: 03200008 jr t9.
370 // c: 00000000 nop.
371 const unsigned LuiT9Instr = 0x3c190000, AdduiT9Instr = 0x27390000;
372 const unsigned JrT9Instr = 0x03200008, NopInstr = 0x0;
373
374 *StubAddr = LuiT9Instr;
375 StubAddr++;
376 *StubAddr = AdduiT9Instr;
377 StubAddr++;
378 *StubAddr = JrT9Instr;
379 StubAddr++;
380 *StubAddr = NopInstr;
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000381 return Addr;
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +0000382 } else if (Arch == Triple::ppc64) {
383 // PowerPC64 stub: the address points to a function descriptor
384 // instead of the function itself. Load the function address
385 // on r11 and sets it to control register. Also loads the function
386 // TOC in r2 and environment pointer to r11.
387 writeInt32BE(Addr, 0x3D800000); // lis r12, highest(addr)
388 writeInt32BE(Addr+4, 0x618C0000); // ori r12, higher(addr)
389 writeInt32BE(Addr+8, 0x798C07C6); // sldi r12, r12, 32
390 writeInt32BE(Addr+12, 0x658C0000); // oris r12, r12, h(addr)
391 writeInt32BE(Addr+16, 0x618C0000); // ori r12, r12, l(addr)
392 writeInt32BE(Addr+20, 0xF8410028); // std r2, 40(r1)
393 writeInt32BE(Addr+24, 0xE96C0000); // ld r11, 0(r12)
394 writeInt32BE(Addr+28, 0xE84C0008); // ld r2, 0(r12)
395 writeInt32BE(Addr+32, 0x7D6903A6); // mtctr r11
396 writeInt32BE(Addr+36, 0xE96C0010); // ld r11, 16(r2)
397 writeInt32BE(Addr+40, 0x4E800420); // bctr
Andrew Kaylor5fc8c7c2012-11-01 19:49:21 +0000398
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +0000399 return Addr;
Richard Sandiford6fc2ad62013-05-03 14:15:35 +0000400 } else if (Arch == Triple::systemz) {
401 writeInt16BE(Addr, 0xC418); // lgrl %r1,.+8
402 writeInt16BE(Addr+2, 0x0000);
403 writeInt16BE(Addr+4, 0x0004);
404 writeInt16BE(Addr+6, 0x07F1); // brc 15,%r1
405 // 8-byte address stored at Addr + 8
406 return Addr;
Akira Hatanakab889e0c2012-08-17 21:28:04 +0000407 }
408 return Addr;
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000409}
410
411// Assign an address to a symbol name and resolve all the relocations
412// associated with it.
413void RuntimeDyldImpl::reassignSectionAddress(unsigned SectionID,
414 uint64_t Addr) {
415 // The address to use for relocation resolution is not
416 // the address of the local section buffer. We must be doing
Andrew Kaylor28989882012-11-05 20:57:16 +0000417 // a remote execution environment of some sort. Relocations can't
418 // be applied until all the sections have been moved. The client must
419 // trigger this with a call to MCJIT::finalize() or
420 // RuntimeDyld::resolveRelocations().
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000421 //
422 // Addr is a uint64_t because we can't assume the pointer width
423 // of the target is the same as that of the host. Just use a generic
424 // "big enough" type.
425 Sections[SectionID].LoadAddress = Addr;
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000426}
427
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000428void RuntimeDyldImpl::resolveRelocationList(const RelocationList &Relocs,
429 uint64_t Value) {
430 for (unsigned i = 0, e = Relocs.size(); i != e; ++i) {
Rafael Espindola87b50172013-04-29 17:24:34 +0000431 const RelocationEntry &RE = Relocs[i];
432 // Ignore relocations for sections that were not loaded
433 if (Sections[RE.SectionID].Address == 0)
434 continue;
435 resolveRelocation(RE, Value);
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000436 }
437}
438
Eli Bendersky37bc5a22012-04-30 12:15:58 +0000439void RuntimeDyldImpl::resolveExternalSymbols() {
440 StringMap<RelocationList>::iterator i = ExternalSymbolRelocations.begin(),
441 e = ExternalSymbolRelocations.end();
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000442 for (; i != e; i++) {
443 StringRef Name = i->first();
444 RelocationList &Relocs = i->second;
Eli Benderskyd98c9e92012-05-01 06:58:59 +0000445 SymbolTableMap::const_iterator Loc = GlobalSymbolTable.find(Name);
446 if (Loc == GlobalSymbolTable.end()) {
Andrew Kaylor7b170502013-02-20 18:09:21 +0000447 if (Name.size() == 0) {
448 // This is an absolute symbol, use an address of zero.
449 DEBUG(dbgs() << "Resolving absolute relocations." << "\n");
450 resolveRelocationList(Relocs, 0);
Andrew Kaylor29fe1502013-02-20 18:24:34 +0000451 } else {
452 // This is an external symbol, try to get its address from
Andrew Kaylor7b170502013-02-20 18:09:21 +0000453 // MemoryManager.
454 uint8_t *Addr = (uint8_t*) MemMgr->getPointerToNamedFunction(Name.data(),
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000455 true);
Andrew Kaylor7b170502013-02-20 18:09:21 +0000456 DEBUG(dbgs() << "Resolving relocations Name: " << Name
457 << "\t" << format("%p", Addr)
458 << "\n");
459 resolveRelocationList(Relocs, (uintptr_t)Addr);
460 }
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000461 } else {
Eli Bendersky37bc5a22012-04-30 12:15:58 +0000462 report_fatal_error("Expected external symbol");
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000463 }
464 }
465}
466
467
Jim Grosbach6e563312011-03-21 22:15:52 +0000468//===----------------------------------------------------------------------===//
469// RuntimeDyld class implementation
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000470RuntimeDyld::RuntimeDyld(RTDyldMemoryManager *mm) {
Andrew Kaylor53608a32012-11-15 23:50:01 +0000471 // FIXME: There's a potential issue lurking here if a single instance of
472 // RuntimeDyld is used to load multiple objects. The current implementation
473 // associates a single memory manager with a RuntimeDyld instance. Even
474 // though the public class spawns a new 'impl' instance for each load,
475 // they share a single memory manager. This can become a problem when page
476 // permissions are applied.
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000477 Dyld = 0;
478 MM = mm;
Jim Grosbach6e563312011-03-21 22:15:52 +0000479}
480
481RuntimeDyld::~RuntimeDyld() {
482 delete Dyld;
483}
484
Andrew Kaylor3f23cef2012-10-02 21:18:39 +0000485ObjectImage *RuntimeDyld::loadObject(ObjectBuffer *InputBuffer) {
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000486 if (!Dyld) {
Eli Benderskya66a1852012-01-16 08:56:09 +0000487 sys::LLVMFileType type = sys::IdentifyFileType(
488 InputBuffer->getBufferStart(),
489 static_cast<unsigned>(InputBuffer->getBufferSize()));
490 switch (type) {
491 case sys::ELF_Relocatable_FileType:
492 case sys::ELF_Executable_FileType:
493 case sys::ELF_SharedObject_FileType:
494 case sys::ELF_Core_FileType:
495 Dyld = new RuntimeDyldELF(MM);
496 break;
497 case sys::Mach_O_Object_FileType:
498 case sys::Mach_O_Executable_FileType:
499 case sys::Mach_O_FixedVirtualMemorySharedLib_FileType:
500 case sys::Mach_O_Core_FileType:
501 case sys::Mach_O_PreloadExecutable_FileType:
502 case sys::Mach_O_DynamicallyLinkedSharedLib_FileType:
503 case sys::Mach_O_DynamicLinker_FileType:
504 case sys::Mach_O_Bundle_FileType:
505 case sys::Mach_O_DynamicallyLinkedSharedLibStub_FileType:
506 case sys::Mach_O_DSYMCompanion_FileType:
507 Dyld = new RuntimeDyldMachO(MM);
508 break;
509 case sys::Unknown_FileType:
510 case sys::Bitcode_FileType:
511 case sys::Archive_FileType:
512 case sys::COFF_FileType:
513 report_fatal_error("Incompatible object format!");
514 }
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000515 } else {
Eli Benderskya66a1852012-01-16 08:56:09 +0000516 if (!Dyld->isCompatibleFormat(InputBuffer))
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000517 report_fatal_error("Incompatible object format!");
518 }
519
Jim Grosbach6e563312011-03-21 22:15:52 +0000520 return Dyld->loadObject(InputBuffer);
521}
522
Jim Grosbachb0271052011-04-08 17:31:24 +0000523void *RuntimeDyld::getSymbolAddress(StringRef Name) {
Jim Grosbach6e563312011-03-21 22:15:52 +0000524 return Dyld->getSymbolAddress(Name);
525}
526
Jim Grosbach35ed8422012-09-05 16:50:40 +0000527uint64_t RuntimeDyld::getSymbolLoadAddress(StringRef Name) {
528 return Dyld->getSymbolLoadAddress(Name);
529}
530
Jim Grosbachf8c1c842011-04-12 21:20:41 +0000531void RuntimeDyld::resolveRelocations() {
532 Dyld->resolveRelocations();
533}
534
Jim Grosbach61425c02012-01-16 22:26:39 +0000535void RuntimeDyld::reassignSectionAddress(unsigned SectionID,
536 uint64_t Addr) {
537 Dyld->reassignSectionAddress(SectionID, Addr);
Jim Grosbachf8c1c842011-04-12 21:20:41 +0000538}
539
Jim Grosbache940c1b2012-09-13 21:50:06 +0000540void RuntimeDyld::mapSectionAddress(const void *LocalAddress,
Jim Grosbach020f4e82012-01-16 23:50:55 +0000541 uint64_t TargetAddress) {
542 Dyld->mapSectionAddress(LocalAddress, TargetAddress);
543}
544
Jim Grosbach91dde152011-03-22 18:22:27 +0000545StringRef RuntimeDyld::getErrorString() {
Jim Grosbachb3eecaf2011-03-22 18:19:42 +0000546 return Dyld->getErrorString();
547}
548
Rafael Espindolaa2e40fb2013-05-05 20:43:10 +0000549StringRef RuntimeDyld::getEHFrameSection() {
550 return Dyld->getEHFrameSection();
551}
552
Jim Grosbach6e563312011-03-21 22:15:52 +0000553} // end namespace llvm