blob: b28a312f74e8f728ddcdf3a53ec239ce8dc7a191 [file] [log] [blame]
Jim Grosbach06594e12012-01-16 23:50:58 +00001//===-- RuntimeDyld.cpp - Run-time dynamic linker for MC-JIT ----*- C++ -*-===//
Jim Grosbachf016b0a2011-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 Grosbach6a85a052011-03-23 19:52:00 +000014#define DEBUG_TYPE "dyld"
Chandler Carruthed0881b2012-12-03 16:50:05 +000015#include "llvm/ExecutionEngine/RuntimeDyld.h"
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +000016#include "JITRegistrar.h"
Andrew Kayloradc70562012-10-02 21:18:39 +000017#include "ObjectImageCommon.h"
Eli Bendersky058d6472012-01-22 07:05:02 +000018#include "RuntimeDyldELF.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000019#include "RuntimeDyldImpl.h"
Eli Bendersky058d6472012-01-22 07:05:02 +000020#include "RuntimeDyldMachO.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000021#include "llvm/Object/ELF.h"
Tim Northover94bc73d2012-10-29 10:47:04 +000022#include "llvm/Support/MathExtras.h"
Andrew Kaylor4fba0492013-10-21 17:42:06 +000023#include "llvm/Support/MutexGuard.h"
Eli Bendersky058d6472012-01-22 07:05:02 +000024
Jim Grosbachf016b0a2011-03-21 22:15:52 +000025using namespace llvm;
26using namespace llvm::object;
27
Chandler Carruth086f7082011-04-05 23:54:31 +000028// Empty out-of-line virtual destructor as the key function.
Danil Malyshev72510f22011-07-13 07:57:58 +000029RuntimeDyldImpl::~RuntimeDyldImpl() {}
Chandler Carruth086f7082011-04-05 23:54:31 +000030
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +000031// Pin the JITRegistrar's and ObjectImage*'s vtables to this file.
32void JITRegistrar::anchor() {}
33void ObjectImage::anchor() {}
34void ObjectImageCommon::anchor() {}
35
Jim Grosbachf016b0a2011-03-21 22:15:52 +000036namespace llvm {
Jim Grosbachf016b0a2011-03-21 22:15:52 +000037
Juergen Ributzka7608dc02014-03-21 20:28:42 +000038void RuntimeDyldImpl::registerEHFrames() {}
Rafael Espindolafa5942b2013-05-05 20:43:10 +000039
Juergen Ributzka7608dc02014-03-21 20:28:42 +000040void RuntimeDyldImpl::deregisterEHFrames() {}
Andrew Kaylorc442a762013-10-16 00:14:21 +000041
Jim Grosbach733d3052011-04-12 21:20:41 +000042// Resolve the relocations for all symbols we currently know about.
43void RuntimeDyldImpl::resolveRelocations() {
Andrew Kaylor4fba0492013-10-21 17:42:06 +000044 MutexGuard locked(lock);
45
Preston Gurd2138ef62012-04-12 20:13:57 +000046 // First, resolve relocations associated with external symbols.
Eli Benderskyb92e1cf2012-04-30 12:15:58 +000047 resolveExternalSymbols();
Danil Malyshev70d22cc2012-03-30 16:45:19 +000048
Jim Grosbacheff0a402012-01-16 22:26:39 +000049 // Just iterate over the sections we have and resolve all the relocations
50 // in them. Gross overkill, but it gets the job done.
51 for (int i = 0, e = Sections.size(); i != e; ++i) {
Andrew Kaylor5f3a9982013-08-19 19:38:06 +000052 // The Section here (Sections[i]) refers to the section in which the
53 // symbol for the relocation is located. The SectionID in the relocation
54 // entry provides the section to which the relocation will be applied.
Andrew Kaylora714efc2012-11-05 20:57:16 +000055 uint64_t Addr = Sections[i].LoadAddress;
Juergen Ributzka7608dc02014-03-21 20:28:42 +000056 DEBUG(dbgs() << "Resolving relocations Section #" << i << "\t"
57 << format("%p", (uint8_t *)Addr) << "\n");
Andrew Kaylora714efc2012-11-05 20:57:16 +000058 resolveRelocationList(Relocations[i], Addr);
Andrew Kaylorea395922013-10-01 01:47:35 +000059 Relocations.erase(i);
Jim Grosbacheff0a402012-01-16 22:26:39 +000060 }
Jim Grosbach733d3052011-04-12 21:20:41 +000061}
62
Jim Grosbach6d613972012-09-13 21:50:06 +000063void RuntimeDyldImpl::mapSectionAddress(const void *LocalAddress,
Jim Grosbach0ddb3a42012-01-16 23:50:55 +000064 uint64_t TargetAddress) {
Andrew Kaylor4fba0492013-10-21 17:42:06 +000065 MutexGuard locked(lock);
Danil Malyshev70d22cc2012-03-30 16:45:19 +000066 for (unsigned i = 0, e = Sections.size(); i != e; ++i) {
67 if (Sections[i].Address == LocalAddress) {
68 reassignSectionAddress(i, TargetAddress);
69 return;
70 }
71 }
72 llvm_unreachable("Attempting to remap address of unknown section!");
Jim Grosbach0ddb3a42012-01-16 23:50:55 +000073}
74
Juergen Ributzka7608dc02014-03-21 20:28:42 +000075ObjectImage *RuntimeDyldImpl::loadObject(ObjectImage *InputObject) {
Andrew Kaylor4fba0492013-10-21 17:42:06 +000076 MutexGuard locked(lock);
77
Ahmed Charles56440fd2014-03-06 05:51:42 +000078 std::unique_ptr<ObjectImage> Obj(InputObject);
Lang Hames937ec542014-02-12 21:30:07 +000079 if (!Obj)
Lang Hames173c69f2014-01-08 04:09:09 +000080 return NULL;
Danil Malyshev70d22cc2012-03-30 16:45:19 +000081
Andrew Kaylor33c5b1b2013-10-15 20:44:55 +000082 // Save information about our target
Lang Hames937ec542014-02-12 21:30:07 +000083 Arch = (Triple::ArchType)Obj->getArch();
84 IsTargetLittleEndian = Obj->getObjectFile()->isLittleEndian();
Juergen Ributzka7608dc02014-03-21 20:28:42 +000085
Lang Hames937ec542014-02-12 21:30:07 +000086 // Compute the memory size required to load all sections to be loaded
87 // and pass this information to the memory manager
88 if (MemMgr->needsToReserveAllocationSpace()) {
89 uint64_t CodeSize = 0, DataSizeRO = 0, DataSizeRW = 0;
90 computeTotalAllocSize(*Obj, CodeSize, DataSizeRO, DataSizeRW);
91 MemMgr->reserveAllocationSpace(CodeSize, DataSizeRO, DataSizeRW);
92 }
Juergen Ributzka7608dc02014-03-21 20:28:42 +000093
Eli Benderskyfc079082012-05-01 06:58:59 +000094 // Symbols found in this object
95 StringMap<SymbolLoc> LocalSymbols;
96 // Used sections from the object file
97 ObjSectionToIDMap LocalSections;
98
Tim Northover94bc73d2012-10-29 10:47:04 +000099 // Common symbols requiring allocation, with their sizes and alignments
Eli Benderskyfc079082012-05-01 06:58:59 +0000100 CommonSymbolMap CommonSymbols;
Tim Northover94bc73d2012-10-29 10:47:04 +0000101 // Maximum required total memory to allocate all common symbols
Eli Benderskyfc079082012-05-01 06:58:59 +0000102 uint64_t CommonSize = 0;
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000103
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000104 // Parse symbols
105 DEBUG(dbgs() << "Parse symbols:\n");
Lang Hames937ec542014-02-12 21:30:07 +0000106 for (symbol_iterator I = Obj->begin_symbols(), E = Obj->end_symbols(); I != E;
107 ++I) {
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000108 object::SymbolRef::Type SymType;
109 StringRef Name;
Lang Hames937ec542014-02-12 21:30:07 +0000110 Check(I->getType(SymType));
111 Check(I->getName(Name));
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000112
Lang Hames937ec542014-02-12 21:30:07 +0000113 uint32_t Flags = I->getFlags();
Preston Gurd2138ef62012-04-12 20:13:57 +0000114
Lang Hames937ec542014-02-12 21:30:07 +0000115 bool IsCommon = Flags & SymbolRef::SF_Common;
116 if (IsCommon) {
Preston Gurd2138ef62012-04-12 20:13:57 +0000117 // Add the common symbols to a list. We'll allocate them all below.
Rafael Espindolae4dd2e02013-04-29 22:24:22 +0000118 uint32_t Align;
Lang Hames937ec542014-02-12 21:30:07 +0000119 Check(I->getAlignment(Align));
Preston Gurd2138ef62012-04-12 20:13:57 +0000120 uint64_t Size = 0;
Lang Hames937ec542014-02-12 21:30:07 +0000121 Check(I->getSize(Size));
Tim Northover94bc73d2012-10-29 10:47:04 +0000122 CommonSize += Size + Align;
Lang Hames937ec542014-02-12 21:30:07 +0000123 CommonSymbols[*I] = CommonSymbolInfo(Size, Align);
Preston Gurd2138ef62012-04-12 20:13:57 +0000124 } else {
125 if (SymType == object::SymbolRef::ST_Function ||
Akira Hatanaka111174b2012-08-17 21:28:04 +0000126 SymType == object::SymbolRef::ST_Data ||
127 SymType == object::SymbolRef::ST_Unknown) {
Preston Gurd2138ef62012-04-12 20:13:57 +0000128 uint64_t FileOffset;
Eli Bendersky0e2ac5b2012-04-29 12:40:47 +0000129 StringRef SectionData;
Tim Northoverd05e6b52012-12-17 17:59:35 +0000130 bool IsCode;
Lang Hames937ec542014-02-12 21:30:07 +0000131 section_iterator SI = Obj->end_sections();
132 Check(I->getFileOffset(FileOffset));
133 Check(I->getSection(SI));
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000134 if (SI == Obj->end_sections())
135 continue;
Lang Hames937ec542014-02-12 21:30:07 +0000136 Check(SI->getContents(SectionData));
137 Check(SI->isText(IsCode));
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000138 const uint8_t *SymPtr =
139 (const uint8_t *)Obj->getData().data() + (uintptr_t)FileOffset;
140 uintptr_t SectOffset =
141 (uintptr_t)(SymPtr - (const uint8_t *)SectionData.begin());
142 unsigned SectionID =
143 findOrEmitSection(*Obj, *SI, IsCode, LocalSections);
Preston Gurd2138ef62012-04-12 20:13:57 +0000144 LocalSymbols[Name.data()] = SymbolLoc(SectionID, SectOffset);
145 DEBUG(dbgs() << "\tFileOffset: " << format("%p", (uintptr_t)FileOffset)
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000146 << " flags: " << Flags << " SID: " << SectionID
Preston Gurd2138ef62012-04-12 20:13:57 +0000147 << " Offset: " << format("%p", SectOffset));
Amara Emersonc958bf32012-11-16 11:11:59 +0000148 GlobalSymbolTable[Name] = SymbolLoc(SectionID, SectOffset);
Preston Gurd2138ef62012-04-12 20:13:57 +0000149 }
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000150 }
151 DEBUG(dbgs() << "\tType: " << SymType << " Name: " << Name << "\n");
152 }
153
Preston Gurd2138ef62012-04-12 20:13:57 +0000154 // Allocate common symbols
155 if (CommonSize != 0)
Lang Hames937ec542014-02-12 21:30:07 +0000156 emitCommonSymbols(*Obj, CommonSymbols, CommonSize, LocalSymbols);
Preston Gurd2138ef62012-04-12 20:13:57 +0000157
Eli Bendersky0e2ac5b2012-04-29 12:40:47 +0000158 // Parse and process relocations
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000159 DEBUG(dbgs() << "Parse relocations:\n");
Lang Hames937ec542014-02-12 21:30:07 +0000160 for (section_iterator SI = Obj->begin_sections(), SE = Obj->end_sections();
161 SI != SE; ++SI) {
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000162 unsigned SectionID = 0;
163 StubMap Stubs;
Lang Hames937ec542014-02-12 21:30:07 +0000164 section_iterator RelocatedSection = SI->getRelocatedSection();
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000165
Juergen Ributzka046709f2014-03-21 07:26:41 +0000166 if (SI->relocation_empty() && !ProcessAllSections)
167 continue;
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000168
Juergen Ributzka046709f2014-03-21 07:26:41 +0000169 bool IsCode = false;
170 Check(RelocatedSection->isText(IsCode));
171 SectionID =
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000172 findOrEmitSection(*Obj, *RelocatedSection, IsCode, LocalSections);
Juergen Ributzka046709f2014-03-21 07:26:41 +0000173 DEBUG(dbgs() << "\tSectionID: " << SectionID << "\n");
174
175 for (relocation_iterator I = SI->relocation_begin(),
176 E = SI->relocation_end(); I != E;)
177 I = processRelocationRef(SectionID, I, *Obj, LocalSections, LocalSymbols,
178 Stubs);
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000179 }
Preston Gurdcc31af92012-04-16 22:12:58 +0000180
Andrew Kaylor4612fed2013-08-19 23:27:43 +0000181 // Give the subclasses a chance to tie-up any loose ends.
Andrew Kaylor7bb13442013-10-11 21:25:48 +0000182 finalizeLoad(LocalSections);
Andrew Kaylor4612fed2013-08-19 23:27:43 +0000183
Ahmed Charles96c9d952014-03-05 10:19:29 +0000184 return Obj.release();
Lang Hames937ec542014-02-12 21:30:07 +0000185}
186
187// A helper method for computeTotalAllocSize.
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000188// Computes the memory size required to allocate sections with the given sizes,
Lang Hames937ec542014-02-12 21:30:07 +0000189// assuming that all sections are allocated with the given alignment
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000190static uint64_t
191computeAllocationSizeForSections(std::vector<uint64_t> &SectionSizes,
192 uint64_t Alignment) {
Lang Hames937ec542014-02-12 21:30:07 +0000193 uint64_t TotalSize = 0;
194 for (size_t Idx = 0, Cnt = SectionSizes.size(); Idx < Cnt; Idx++) {
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000195 uint64_t AlignedSize =
196 (SectionSizes[Idx] + Alignment - 1) / Alignment * Alignment;
Lang Hames937ec542014-02-12 21:30:07 +0000197 TotalSize += AlignedSize;
198 }
199 return TotalSize;
200}
201
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000202// Compute an upper bound of the memory size that is required to load all
203// sections
204void RuntimeDyldImpl::computeTotalAllocSize(ObjectImage &Obj,
205 uint64_t &CodeSize,
206 uint64_t &DataSizeRO,
207 uint64_t &DataSizeRW) {
Lang Hames937ec542014-02-12 21:30:07 +0000208 // Compute the size of all sections required for execution
209 std::vector<uint64_t> CodeSectionSizes;
210 std::vector<uint64_t> ROSectionSizes;
211 std::vector<uint64_t> RWSectionSizes;
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000212 uint64_t MaxAlignment = sizeof(void *);
Lang Hames937ec542014-02-12 21:30:07 +0000213
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000214 // Collect sizes of all sections to be loaded;
Lang Hames937ec542014-02-12 21:30:07 +0000215 // also determine the max alignment of all sections
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000216 for (section_iterator SI = Obj.begin_sections(), SE = Obj.end_sections();
Lang Hames937ec542014-02-12 21:30:07 +0000217 SI != SE; ++SI) {
218 const SectionRef &Section = *SI;
219
220 bool IsRequired;
221 Check(Section.isRequiredForExecution(IsRequired));
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000222
Lang Hames937ec542014-02-12 21:30:07 +0000223 // Consider only the sections that are required to be loaded for execution
224 if (IsRequired) {
225 uint64_t DataSize = 0;
226 uint64_t Alignment64 = 0;
227 bool IsCode = false;
228 bool IsReadOnly = false;
229 StringRef Name;
230 Check(Section.getSize(DataSize));
231 Check(Section.getAlignment(Alignment64));
232 Check(Section.isText(IsCode));
233 Check(Section.isReadOnlyData(IsReadOnly));
234 Check(Section.getName(Name));
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000235 unsigned Alignment = (unsigned)Alignment64 & 0xffffffffL;
236
Lang Hames937ec542014-02-12 21:30:07 +0000237 uint64_t StubBufSize = computeSectionStubBufSize(Obj, Section);
238 uint64_t SectionSize = DataSize + StubBufSize;
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000239
240 // The .eh_frame section (at least on Linux) needs an extra four bytes
241 // padded
Lang Hames937ec542014-02-12 21:30:07 +0000242 // with zeroes added at the end. For MachO objects, this section has a
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000243 // slightly different name, so this won't have any effect for MachO
244 // objects.
Lang Hames937ec542014-02-12 21:30:07 +0000245 if (Name == ".eh_frame")
246 SectionSize += 4;
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000247
Lang Hames937ec542014-02-12 21:30:07 +0000248 if (SectionSize > 0) {
249 // save the total size of the section
250 if (IsCode) {
251 CodeSectionSizes.push_back(SectionSize);
252 } else if (IsReadOnly) {
253 ROSectionSizes.push_back(SectionSize);
254 } else {
255 RWSectionSizes.push_back(SectionSize);
256 }
257 // update the max alignment
258 if (Alignment > MaxAlignment) {
259 MaxAlignment = Alignment;
260 }
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000261 }
Lang Hames937ec542014-02-12 21:30:07 +0000262 }
263 }
264
265 // Compute the size of all common symbols
266 uint64_t CommonSize = 0;
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000267 for (symbol_iterator I = Obj.begin_symbols(), E = Obj.end_symbols(); I != E;
268 ++I) {
Lang Hames937ec542014-02-12 21:30:07 +0000269 uint32_t Flags = I->getFlags();
270 if (Flags & SymbolRef::SF_Common) {
271 // Add the common symbols to a list. We'll allocate them all below.
272 uint64_t Size = 0;
273 Check(I->getSize(Size));
274 CommonSize += Size;
275 }
276 }
277 if (CommonSize != 0) {
278 RWSectionSizes.push_back(CommonSize);
279 }
280
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000281 // Compute the required allocation space for each different type of sections
282 // (code, read-only data, read-write data) assuming that all sections are
Lang Hames937ec542014-02-12 21:30:07 +0000283 // allocated with the max alignment. Note that we cannot compute with the
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000284 // individual alignments of the sections, because then the required size
Lang Hames937ec542014-02-12 21:30:07 +0000285 // depends on the order, in which the sections are allocated.
286 CodeSize = computeAllocationSizeForSections(CodeSectionSizes, MaxAlignment);
287 DataSizeRO = computeAllocationSizeForSections(ROSectionSizes, MaxAlignment);
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000288 DataSizeRW = computeAllocationSizeForSections(RWSectionSizes, MaxAlignment);
Lang Hames937ec542014-02-12 21:30:07 +0000289}
290
291// compute stub buffer size for the given section
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000292unsigned RuntimeDyldImpl::computeSectionStubBufSize(ObjectImage &Obj,
Lang Hames937ec542014-02-12 21:30:07 +0000293 const SectionRef &Section) {
294 unsigned StubSize = getMaxStubSize();
295 if (StubSize == 0) {
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000296 return 0;
Lang Hames937ec542014-02-12 21:30:07 +0000297 }
298 // FIXME: this is an inefficient way to handle this. We should computed the
299 // necessary section allocation size in loadObject by walking all the sections
300 // once.
301 unsigned StubBufSize = 0;
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000302 for (section_iterator SI = Obj.begin_sections(), SE = Obj.end_sections();
Lang Hames937ec542014-02-12 21:30:07 +0000303 SI != SE; ++SI) {
304 section_iterator RelSecI = SI->getRelocatedSection();
305 if (!(RelSecI == Section))
306 continue;
307
Alexey Samsonovaa4d2952014-03-14 14:22:49 +0000308 for (const RelocationRef &Reloc : SI->relocations()) {
309 (void)Reloc;
Lang Hames937ec542014-02-12 21:30:07 +0000310 StubBufSize += StubSize;
311 }
312 }
Alexey Samsonovaa4d2952014-03-14 14:22:49 +0000313
Lang Hames937ec542014-02-12 21:30:07 +0000314 // Get section data size and alignment
315 uint64_t Alignment64;
316 uint64_t DataSize;
317 Check(Section.getSize(DataSize));
318 Check(Section.getAlignment(Alignment64));
319
320 // Add stubbuf size alignment
321 unsigned Alignment = (unsigned)Alignment64 & 0xffffffffL;
322 unsigned StubAlignment = getStubAlignment();
323 unsigned EndAlignment = (DataSize | Alignment) & -(DataSize | Alignment);
324 if (StubAlignment > EndAlignment)
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000325 StubBufSize += StubAlignment - EndAlignment;
Lang Hames937ec542014-02-12 21:30:07 +0000326 return StubBufSize;
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000327}
328
Eli Bendersky667b8792012-05-01 10:41:12 +0000329void RuntimeDyldImpl::emitCommonSymbols(ObjectImage &Obj,
330 const CommonSymbolMap &CommonSymbols,
331 uint64_t TotalSize,
332 SymbolTableMap &SymbolTable) {
Preston Gurd2138ef62012-04-12 20:13:57 +0000333 // Allocate memory for the section
334 unsigned SectionID = Sections.size();
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000335 uint8_t *Addr = MemMgr->allocateDataSection(TotalSize, sizeof(void *),
336 SectionID, StringRef(), false);
Preston Gurd2138ef62012-04-12 20:13:57 +0000337 if (!Addr)
338 report_fatal_error("Unable to allocate memory for common symbols!");
339 uint64_t Offset = 0;
Rafael Espindolafa5942b2013-05-05 20:43:10 +0000340 Sections.push_back(SectionEntry(StringRef(), Addr, TotalSize, 0));
Preston Gurd2138ef62012-04-12 20:13:57 +0000341 memset(Addr, 0, TotalSize);
342
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000343 DEBUG(dbgs() << "emitCommonSection SectionID: " << SectionID << " new addr: "
344 << format("%p", Addr) << " DataSize: " << TotalSize << "\n");
Preston Gurd2138ef62012-04-12 20:13:57 +0000345
346 // Assign the address of each symbol
Eli Bendersky667b8792012-05-01 10:41:12 +0000347 for (CommonSymbolMap::const_iterator it = CommonSymbols.begin(),
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000348 itEnd = CommonSymbols.end(); it != itEnd; ++it) {
Tim Northover94bc73d2012-10-29 10:47:04 +0000349 uint64_t Size = it->second.first;
350 uint64_t Align = it->second.second;
Preston Gurd2138ef62012-04-12 20:13:57 +0000351 StringRef Name;
352 it->first.getName(Name);
Tim Northover94bc73d2012-10-29 10:47:04 +0000353 if (Align) {
354 // This symbol has an alignment requirement.
355 uint64_t AlignOffset = OffsetToAlignment((uint64_t)Addr, Align);
356 Addr += AlignOffset;
357 Offset += AlignOffset;
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000358 DEBUG(dbgs() << "Allocating common symbol " << Name << " address "
359 << format("%p\n", Addr));
Tim Northover94bc73d2012-10-29 10:47:04 +0000360 }
Preston Gurdcc31af92012-04-16 22:12:58 +0000361 Obj.updateSymbolAddress(it->first, (uint64_t)Addr);
Eli Bendersky667b8792012-05-01 10:41:12 +0000362 SymbolTable[Name.data()] = SymbolLoc(SectionID, Offset);
Preston Gurd2138ef62012-04-12 20:13:57 +0000363 Offset += Size;
364 Addr += Size;
365 }
Preston Gurd2138ef62012-04-12 20:13:57 +0000366}
367
Preston Gurdcc31af92012-04-16 22:12:58 +0000368unsigned RuntimeDyldImpl::emitSection(ObjectImage &Obj,
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000369 const SectionRef &Section, bool IsCode) {
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000370
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000371 StringRef data;
372 uint64_t Alignment64;
373 Check(Section.getContents(data));
374 Check(Section.getAlignment(Alignment64));
375
376 unsigned Alignment = (unsigned)Alignment64 & 0xffffffffL;
Preston Gurd2138ef62012-04-12 20:13:57 +0000377 bool IsRequired;
378 bool IsVirtual;
379 bool IsZeroInit;
Andrew Kaylora342cb92012-11-15 23:50:01 +0000380 bool IsReadOnly;
Preston Gurd2138ef62012-04-12 20:13:57 +0000381 uint64_t DataSize;
Andrew Kaylor877b9312013-10-16 00:32:24 +0000382 unsigned PaddingSize = 0;
Lang Hames937ec542014-02-12 21:30:07 +0000383 unsigned StubBufSize = 0;
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000384 StringRef Name;
Preston Gurd2138ef62012-04-12 20:13:57 +0000385 Check(Section.isRequiredForExecution(IsRequired));
386 Check(Section.isVirtual(IsVirtual));
387 Check(Section.isZeroInit(IsZeroInit));
Andrew Kaylora342cb92012-11-15 23:50:01 +0000388 Check(Section.isReadOnlyData(IsReadOnly));
Preston Gurd2138ef62012-04-12 20:13:57 +0000389 Check(Section.getSize(DataSize));
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000390 Check(Section.getName(Name));
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000391
392 StubBufSize = computeSectionStubBufSize(Obj, Section);
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000393
Andrew Kaylor877b9312013-10-16 00:32:24 +0000394 // The .eh_frame section (at least on Linux) needs an extra four bytes padded
395 // with zeroes added at the end. For MachO objects, this section has a
396 // slightly different name, so this won't have any effect for MachO objects.
397 if (Name == ".eh_frame")
398 PaddingSize = 4;
399
Lang Hamesd4100172014-02-11 05:28:24 +0000400 uintptr_t Allocate;
Preston Gurd2138ef62012-04-12 20:13:57 +0000401 unsigned SectionID = Sections.size();
402 uint8_t *Addr;
403 const char *pData = 0;
404
405 // Some sections, such as debug info, don't need to be loaded for execution.
406 // Leave those where they are.
407 if (IsRequired) {
Andrew Kaylor877b9312013-10-16 00:32:24 +0000408 Allocate = DataSize + PaddingSize + StubBufSize;
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000409 Addr = IsCode ? MemMgr->allocateCodeSection(Allocate, Alignment, SectionID,
410 Name)
411 : MemMgr->allocateDataSection(Allocate, Alignment, SectionID,
412 Name, IsReadOnly);
Preston Gurd2138ef62012-04-12 20:13:57 +0000413 if (!Addr)
414 report_fatal_error("Unable to allocate section memory!");
415
416 // Virtual sections have no data in the object image, so leave pData = 0
417 if (!IsVirtual)
418 pData = data.data();
419
420 // Zero-initialize or copy the data from the image
421 if (IsZeroInit || IsVirtual)
422 memset(Addr, 0, DataSize);
423 else
424 memcpy(Addr, pData, DataSize);
425
Andrew Kaylor877b9312013-10-16 00:32:24 +0000426 // Fill in any extra bytes we allocated for padding
427 if (PaddingSize != 0) {
428 memset(Addr + DataSize, 0, PaddingSize);
429 // Update the DataSize variable so that the stub offset is set correctly.
430 DataSize += PaddingSize;
431 }
432
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000433 DEBUG(dbgs() << "emitSection SectionID: " << SectionID << " Name: " << Name
Preston Gurd2138ef62012-04-12 20:13:57 +0000434 << " obj addr: " << format("%p", pData)
435 << " new addr: " << format("%p", Addr)
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000436 << " DataSize: " << DataSize << " StubBufSize: " << StubBufSize
437 << " Allocate: " << Allocate << "\n");
Preston Gurdcc31af92012-04-16 22:12:58 +0000438 Obj.updateSectionAddress(Section, (uint64_t)Addr);
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000439 } else {
Preston Gurd2138ef62012-04-12 20:13:57 +0000440 // Even if we didn't load the section, we need to record an entry for it
Eli Bendersky0e2ac5b2012-04-29 12:40:47 +0000441 // to handle later processing (and by 'handle' I mean don't do anything
442 // with these sections).
Preston Gurd2138ef62012-04-12 20:13:57 +0000443 Allocate = 0;
444 Addr = 0;
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000445 DEBUG(dbgs() << "emitSection SectionID: " << SectionID << " Name: " << Name
446 << " obj addr: " << format("%p", data.data()) << " new addr: 0"
447 << " DataSize: " << DataSize << " StubBufSize: " << StubBufSize
448 << " Allocate: " << Allocate << "\n");
Preston Gurd2138ef62012-04-12 20:13:57 +0000449 }
450
Rafael Espindolafa5942b2013-05-05 20:43:10 +0000451 Sections.push_back(SectionEntry(Name, Addr, DataSize, (uintptr_t)pData));
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000452 return SectionID;
453}
454
Preston Gurdcc31af92012-04-16 22:12:58 +0000455unsigned RuntimeDyldImpl::findOrEmitSection(ObjectImage &Obj,
456 const SectionRef &Section,
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000457 bool IsCode,
458 ObjSectionToIDMap &LocalSections) {
459
460 unsigned SectionID = 0;
461 ObjSectionToIDMap::iterator i = LocalSections.find(Section);
462 if (i != LocalSections.end())
463 SectionID = i->second;
464 else {
Preston Gurdcc31af92012-04-16 22:12:58 +0000465 SectionID = emitSection(Obj, Section, IsCode);
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000466 LocalSections[Section] = SectionID;
467 }
468 return SectionID;
469}
470
Eli Bendersky667b8792012-05-01 10:41:12 +0000471void RuntimeDyldImpl::addRelocationForSection(const RelocationEntry &RE,
472 unsigned SectionID) {
473 Relocations[SectionID].push_back(RE);
474}
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000475
Eli Bendersky667b8792012-05-01 10:41:12 +0000476void RuntimeDyldImpl::addRelocationForSymbol(const RelocationEntry &RE,
477 StringRef SymbolName) {
478 // Relocation by symbol. If the symbol is found in the global symbol table,
479 // create an appropriate section relocation. Otherwise, add it to
480 // ExternalSymbolRelocations.
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000481 SymbolTableMap::const_iterator Loc = GlobalSymbolTable.find(SymbolName);
Eli Bendersky667b8792012-05-01 10:41:12 +0000482 if (Loc == GlobalSymbolTable.end()) {
483 ExternalSymbolRelocations[SymbolName].push_back(RE);
Eli Benderskyb92e1cf2012-04-30 12:15:58 +0000484 } else {
Eli Bendersky667b8792012-05-01 10:41:12 +0000485 // Copy the RE since we want to modify its addend.
486 RelocationEntry RECopy = RE;
487 RECopy.Addend += Loc->second.second;
488 Relocations[Loc->second.first].push_back(RECopy);
Eli Benderskyb92e1cf2012-04-30 12:15:58 +0000489 }
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000490}
491
492uint8_t *RuntimeDyldImpl::createStubFunction(uint8_t *Addr) {
Tim Northover37cde972013-05-04 20:14:09 +0000493 if (Arch == Triple::aarch64) {
494 // This stub has to be able to access the full address space,
495 // since symbol lookup won't necessarily find a handy, in-range,
496 // PLT stub for functions which could be anywhere.
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000497 uint32_t *StubAddr = (uint32_t *)Addr;
Tim Northover37cde972013-05-04 20:14:09 +0000498
499 // Stub can use ip0 (== x16) to calculate address
500 *StubAddr = 0xd2e00010; // movz ip0, #:abs_g3:<addr>
501 StubAddr++;
502 *StubAddr = 0xf2c00010; // movk ip0, #:abs_g2_nc:<addr>
503 StubAddr++;
504 *StubAddr = 0xf2a00010; // movk ip0, #:abs_g1_nc:<addr>
505 StubAddr++;
506 *StubAddr = 0xf2800010; // movk ip0, #:abs_g0_nc:<addr>
507 StubAddr++;
508 *StubAddr = 0xd61f0200; // br ip0
509
510 return Addr;
511 } else if (Arch == Triple::arm) {
Akira Hatanaka111174b2012-08-17 21:28:04 +0000512 // TODO: There is only ARM far stub now. We should add the Thumb stub,
513 // and stubs for branches Thumb - ARM and ARM - Thumb.
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000514 uint32_t *StubAddr = (uint32_t *)Addr;
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000515 *StubAddr = 0xe51ff004; // ldr pc,<label>
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000516 return (uint8_t *)++StubAddr;
NAKAMURA Takumif97efd92012-12-04 00:08:14 +0000517 } else if (Arch == Triple::mipsel || Arch == Triple::mips) {
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000518 uint32_t *StubAddr = (uint32_t *)Addr;
Akira Hatanaka111174b2012-08-17 21:28:04 +0000519 // 0: 3c190000 lui t9,%hi(addr).
520 // 4: 27390000 addiu t9,t9,%lo(addr).
521 // 8: 03200008 jr t9.
522 // c: 00000000 nop.
523 const unsigned LuiT9Instr = 0x3c190000, AdduiT9Instr = 0x27390000;
524 const unsigned JrT9Instr = 0x03200008, NopInstr = 0x0;
525
526 *StubAddr = LuiT9Instr;
527 StubAddr++;
528 *StubAddr = AdduiT9Instr;
529 StubAddr++;
530 *StubAddr = JrT9Instr;
531 StubAddr++;
532 *StubAddr = NopInstr;
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000533 return Addr;
Bill Schmidt0a9170d2013-07-26 01:35:43 +0000534 } else if (Arch == Triple::ppc64 || Arch == Triple::ppc64le) {
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000535 // PowerPC64 stub: the address points to a function descriptor
536 // instead of the function itself. Load the function address
537 // on r11 and sets it to control register. Also loads the function
538 // TOC in r2 and environment pointer to r11.
539 writeInt32BE(Addr, 0x3D800000); // lis r12, highest(addr)
540 writeInt32BE(Addr+4, 0x618C0000); // ori r12, higher(addr)
541 writeInt32BE(Addr+8, 0x798C07C6); // sldi r12, r12, 32
542 writeInt32BE(Addr+12, 0x658C0000); // oris r12, r12, h(addr)
543 writeInt32BE(Addr+16, 0x618C0000); // ori r12, r12, l(addr)
544 writeInt32BE(Addr+20, 0xF8410028); // std r2, 40(r1)
545 writeInt32BE(Addr+24, 0xE96C0000); // ld r11, 0(r12)
546 writeInt32BE(Addr+28, 0xE84C0008); // ld r2, 0(r12)
547 writeInt32BE(Addr+32, 0x7D6903A6); // mtctr r11
548 writeInt32BE(Addr+36, 0xE96C0010); // ld r11, 16(r2)
549 writeInt32BE(Addr+40, 0x4E800420); // bctr
Andrew Kaylor0eece8d2012-11-01 19:49:21 +0000550
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000551 return Addr;
Richard Sandifordca044082013-05-03 14:15:35 +0000552 } else if (Arch == Triple::systemz) {
553 writeInt16BE(Addr, 0xC418); // lgrl %r1,.+8
554 writeInt16BE(Addr+2, 0x0000);
555 writeInt16BE(Addr+4, 0x0004);
556 writeInt16BE(Addr+6, 0x07F1); // brc 15,%r1
557 // 8-byte address stored at Addr + 8
558 return Addr;
Andrew Kaylor4612fed2013-08-19 23:27:43 +0000559 } else if (Arch == Triple::x86_64) {
560 *Addr = 0xFF; // jmp
561 *(Addr+1) = 0x25; // rip
562 // 32-bit PC-relative address of the GOT entry will be stored at Addr+2
Akira Hatanaka111174b2012-08-17 21:28:04 +0000563 }
564 return Addr;
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000565}
566
567// Assign an address to a symbol name and resolve all the relocations
568// associated with it.
569void RuntimeDyldImpl::reassignSectionAddress(unsigned SectionID,
570 uint64_t Addr) {
571 // The address to use for relocation resolution is not
572 // the address of the local section buffer. We must be doing
Andrew Kaylora714efc2012-11-05 20:57:16 +0000573 // a remote execution environment of some sort. Relocations can't
574 // be applied until all the sections have been moved. The client must
575 // trigger this with a call to MCJIT::finalize() or
576 // RuntimeDyld::resolveRelocations().
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000577 //
578 // Addr is a uint64_t because we can't assume the pointer width
579 // of the target is the same as that of the host. Just use a generic
580 // "big enough" type.
581 Sections[SectionID].LoadAddress = Addr;
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000582}
583
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000584void RuntimeDyldImpl::resolveRelocationList(const RelocationList &Relocs,
585 uint64_t Value) {
586 for (unsigned i = 0, e = Relocs.size(); i != e; ++i) {
Rafael Espindolaf1f1c622013-04-29 17:24:34 +0000587 const RelocationEntry &RE = Relocs[i];
588 // Ignore relocations for sections that were not loaded
589 if (Sections[RE.SectionID].Address == 0)
590 continue;
591 resolveRelocation(RE, Value);
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000592 }
593}
594
Eli Benderskyb92e1cf2012-04-30 12:15:58 +0000595void RuntimeDyldImpl::resolveExternalSymbols() {
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000596 while (!ExternalSymbolRelocations.empty()) {
Andrew Kaylorea395922013-10-01 01:47:35 +0000597 StringMap<RelocationList>::iterator i = ExternalSymbolRelocations.begin();
598
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000599 StringRef Name = i->first();
Andrew Kaylorea395922013-10-01 01:47:35 +0000600 if (Name.size() == 0) {
601 // This is an absolute symbol, use an address of zero.
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000602 DEBUG(dbgs() << "Resolving absolute relocations."
603 << "\n");
Andrew Kaylorcfb4a992013-11-11 19:55:10 +0000604 RelocationList &Relocs = i->second;
Andrew Kaylorea395922013-10-01 01:47:35 +0000605 resolveRelocationList(Relocs, 0);
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000606 } else {
Andrew Kaylorea395922013-10-01 01:47:35 +0000607 uint64_t Addr = 0;
608 SymbolTableMap::const_iterator Loc = GlobalSymbolTable.find(Name);
609 if (Loc == GlobalSymbolTable.end()) {
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000610 // This is an external symbol, try to get its address from
611 // MemoryManager.
612 Addr = MemMgr->getSymbolAddress(Name.data());
613 // The call to getSymbolAddress may have caused additional modules to
614 // be loaded, which may have added new entries to the
615 // ExternalSymbolRelocations map. Consquently, we need to update our
616 // iterator. This is also why retrieval of the relocation list
617 // associated with this symbol is deferred until below this point.
618 // New entries may have been added to the relocation list.
619 i = ExternalSymbolRelocations.find(Name);
Andrew Kaylorea395922013-10-01 01:47:35 +0000620 } else {
621 // We found the symbol in our global table. It was probably in a
622 // Module that we loaded previously.
Yaron Kerenc9802882013-10-19 09:04:26 +0000623 SymbolLoc SymLoc = Loc->second;
Andrew Kaylorea395922013-10-01 01:47:35 +0000624 Addr = getSectionLoadAddress(SymLoc.first) + SymLoc.second;
625 }
626
627 // FIXME: Implement error handling that doesn't kill the host program!
628 if (!Addr)
629 report_fatal_error("Program used external function '" + Name +
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000630 "' which could not be resolved!");
Andrew Kaylorea395922013-10-01 01:47:35 +0000631
632 updateGOTEntries(Name, Addr);
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000633 DEBUG(dbgs() << "Resolving relocations Name: " << Name << "\t"
634 << format("0x%lx", Addr) << "\n");
Andrew Kaylorcfb4a992013-11-11 19:55:10 +0000635 // This list may have been updated when we called getSymbolAddress, so
636 // don't change this code to get the list earlier.
637 RelocationList &Relocs = i->second;
Andrew Kaylorea395922013-10-01 01:47:35 +0000638 resolveRelocationList(Relocs, Addr);
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000639 }
Andrew Kaylorea395922013-10-01 01:47:35 +0000640
Andrew Kaylorcfb4a992013-11-11 19:55:10 +0000641 ExternalSymbolRelocations.erase(i);
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000642 }
643}
644
Jim Grosbachf016b0a2011-03-21 22:15:52 +0000645//===----------------------------------------------------------------------===//
646// RuntimeDyld class implementation
Danil Malyshev72510f22011-07-13 07:57:58 +0000647RuntimeDyld::RuntimeDyld(RTDyldMemoryManager *mm) {
Andrew Kaylora342cb92012-11-15 23:50:01 +0000648 // FIXME: There's a potential issue lurking here if a single instance of
649 // RuntimeDyld is used to load multiple objects. The current implementation
650 // associates a single memory manager with a RuntimeDyld instance. Even
651 // though the public class spawns a new 'impl' instance for each load,
652 // they share a single memory manager. This can become a problem when page
653 // permissions are applied.
Danil Malyshev72510f22011-07-13 07:57:58 +0000654 Dyld = 0;
655 MM = mm;
Lang Hames868d4b32014-03-20 21:06:46 +0000656 ProcessAllSections = false;
Jim Grosbachf016b0a2011-03-21 22:15:52 +0000657}
658
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000659RuntimeDyld::~RuntimeDyld() { delete Dyld; }
Jim Grosbachf016b0a2011-03-21 22:15:52 +0000660
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000661static std::unique_ptr<RuntimeDyldELF>
662createRuntimeDyldELF(RTDyldMemoryManager *MM, bool ProcessAllSections) {
Lang Hames868d4b32014-03-20 21:06:46 +0000663 std::unique_ptr<RuntimeDyldELF> Dyld(new RuntimeDyldELF(MM));
664 Dyld->setProcessAllSections(ProcessAllSections);
665 return Dyld;
666}
667
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000668static std::unique_ptr<RuntimeDyldMachO>
669createRuntimeDyldMachO(RTDyldMemoryManager *MM, bool ProcessAllSections) {
Lang Hames868d4b32014-03-20 21:06:46 +0000670 std::unique_ptr<RuntimeDyldMachO> Dyld(new RuntimeDyldMachO(MM));
671 Dyld->setProcessAllSections(ProcessAllSections);
672 return Dyld;
673}
674
Lang Hames173c69f2014-01-08 04:09:09 +0000675ObjectImage *RuntimeDyld::loadObject(ObjectFile *InputObject) {
Lang Hames951b2352014-03-08 18:45:12 +0000676 std::unique_ptr<ObjectImage> InputImage;
Lang Hames173c69f2014-01-08 04:09:09 +0000677
Lang Hames951b2352014-03-08 18:45:12 +0000678 if (InputObject->isELF()) {
679 InputImage.reset(RuntimeDyldELF::createObjectImageFromFile(InputObject));
680 if (!Dyld)
Lang Hames868d4b32014-03-20 21:06:46 +0000681 Dyld = createRuntimeDyldELF(MM, ProcessAllSections).release();
Lang Hames951b2352014-03-08 18:45:12 +0000682 } else if (InputObject->isMachO()) {
683 InputImage.reset(RuntimeDyldMachO::createObjectImageFromFile(InputObject));
684 if (!Dyld)
Lang Hames868d4b32014-03-20 21:06:46 +0000685 Dyld = createRuntimeDyldMachO(MM, ProcessAllSections).release();
Lang Hames951b2352014-03-08 18:45:12 +0000686 } else
687 report_fatal_error("Incompatible object format!");
688
689 if (!Dyld->isCompatibleFile(InputObject))
690 report_fatal_error("Incompatible object format!");
691
692 Dyld->loadObject(InputImage.get());
693 return InputImage.release();
Lang Hames173c69f2014-01-08 04:09:09 +0000694}
695
Andrew Kayloradc70562012-10-02 21:18:39 +0000696ObjectImage *RuntimeDyld::loadObject(ObjectBuffer *InputBuffer) {
Lang Hames951b2352014-03-08 18:45:12 +0000697 std::unique_ptr<ObjectImage> InputImage;
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000698 sys::fs::file_magic Type = sys::fs::identify_magic(InputBuffer->getBuffer());
Lang Hames951b2352014-03-08 18:45:12 +0000699
700 switch (Type) {
701 case sys::fs::file_magic::elf_relocatable:
702 case sys::fs::file_magic::elf_executable:
703 case sys::fs::file_magic::elf_shared_object:
704 case sys::fs::file_magic::elf_core:
705 InputImage.reset(RuntimeDyldELF::createObjectImage(InputBuffer));
706 if (!Dyld)
Lang Hames868d4b32014-03-20 21:06:46 +0000707 Dyld = createRuntimeDyldELF(MM, ProcessAllSections).release();
Lang Hames951b2352014-03-08 18:45:12 +0000708 break;
709 case sys::fs::file_magic::macho_object:
710 case sys::fs::file_magic::macho_executable:
711 case sys::fs::file_magic::macho_fixed_virtual_memory_shared_lib:
712 case sys::fs::file_magic::macho_core:
713 case sys::fs::file_magic::macho_preload_executable:
714 case sys::fs::file_magic::macho_dynamically_linked_shared_lib:
715 case sys::fs::file_magic::macho_dynamic_linker:
716 case sys::fs::file_magic::macho_bundle:
717 case sys::fs::file_magic::macho_dynamically_linked_shared_lib_stub:
718 case sys::fs::file_magic::macho_dsym_companion:
719 InputImage.reset(RuntimeDyldMachO::createObjectImage(InputBuffer));
720 if (!Dyld)
Lang Hames868d4b32014-03-20 21:06:46 +0000721 Dyld = createRuntimeDyldMachO(MM, ProcessAllSections).release();
Lang Hames951b2352014-03-08 18:45:12 +0000722 break;
723 case sys::fs::file_magic::unknown:
724 case sys::fs::file_magic::bitcode:
725 case sys::fs::file_magic::archive:
726 case sys::fs::file_magic::coff_object:
727 case sys::fs::file_magic::coff_import_library:
728 case sys::fs::file_magic::pecoff_executable:
729 case sys::fs::file_magic::macho_universal_binary:
730 case sys::fs::file_magic::windows_resource:
731 report_fatal_error("Incompatible object format!");
Danil Malyshev72510f22011-07-13 07:57:58 +0000732 }
733
Lang Hames951b2352014-03-08 18:45:12 +0000734 if (!Dyld->isCompatibleFormat(InputBuffer))
735 report_fatal_error("Incompatible object format!");
736
737 Dyld->loadObject(InputImage.get());
738 return InputImage.release();
Jim Grosbachf016b0a2011-03-21 22:15:52 +0000739}
740
Jim Grosbach18b81c52011-04-08 17:31:24 +0000741void *RuntimeDyld::getSymbolAddress(StringRef Name) {
Andrew Kaylorea395922013-10-01 01:47:35 +0000742 if (!Dyld)
743 return NULL;
Jim Grosbachf016b0a2011-03-21 22:15:52 +0000744 return Dyld->getSymbolAddress(Name);
745}
746
Jim Grosbachdc1123f2012-09-05 16:50:40 +0000747uint64_t RuntimeDyld::getSymbolLoadAddress(StringRef Name) {
Andrew Kaylorea395922013-10-01 01:47:35 +0000748 if (!Dyld)
749 return 0;
Jim Grosbachdc1123f2012-09-05 16:50:40 +0000750 return Dyld->getSymbolLoadAddress(Name);
751}
752
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000753void RuntimeDyld::resolveRelocations() { Dyld->resolveRelocations(); }
Jim Grosbach733d3052011-04-12 21:20:41 +0000754
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000755void RuntimeDyld::reassignSectionAddress(unsigned SectionID, uint64_t Addr) {
Jim Grosbacheff0a402012-01-16 22:26:39 +0000756 Dyld->reassignSectionAddress(SectionID, Addr);
Jim Grosbach733d3052011-04-12 21:20:41 +0000757}
758
Jim Grosbach6d613972012-09-13 21:50:06 +0000759void RuntimeDyld::mapSectionAddress(const void *LocalAddress,
Jim Grosbach0ddb3a42012-01-16 23:50:55 +0000760 uint64_t TargetAddress) {
761 Dyld->mapSectionAddress(LocalAddress, TargetAddress);
762}
763
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000764StringRef RuntimeDyld::getErrorString() { return Dyld->getErrorString(); }
Jim Grosbach40411cc2011-03-22 18:19:42 +0000765
Andrew Kaylor7bb13442013-10-11 21:25:48 +0000766void RuntimeDyld::registerEHFrames() {
Andrew Kaylorc442a762013-10-16 00:14:21 +0000767 if (Dyld)
768 Dyld->registerEHFrames();
769}
770
771void RuntimeDyld::deregisterEHFrames() {
772 if (Dyld)
773 Dyld->deregisterEHFrames();
Rafael Espindolafa5942b2013-05-05 20:43:10 +0000774}
775
Jim Grosbachf016b0a2011-03-21 22:15:52 +0000776} // end namespace llvm