blob: 4be16915cbcfd91260c837535bfca9c2ed8638fa [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
Chandler Carruthed0881b2012-12-03 16:50:05 +000014#include "llvm/ExecutionEngine/RuntimeDyld.h"
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +000015#include "JITRegistrar.h"
Andrew Kayloradc70562012-10-02 21:18:39 +000016#include "ObjectImageCommon.h"
Lang Hamesf7acddd2014-07-22 22:47:39 +000017#include "RuntimeDyldCheckerImpl.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 Carruthf58e3762014-04-22 03:04:17 +000028#define DEBUG_TYPE "dyld"
29
Chandler Carruth086f7082011-04-05 23:54:31 +000030// Empty out-of-line virtual destructor as the key function.
Danil Malyshev72510f22011-07-13 07:57:58 +000031RuntimeDyldImpl::~RuntimeDyldImpl() {}
Chandler Carruth086f7082011-04-05 23:54:31 +000032
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +000033// Pin the JITRegistrar's and ObjectImage*'s vtables to this file.
34void JITRegistrar::anchor() {}
35void ObjectImage::anchor() {}
36void ObjectImageCommon::anchor() {}
37
Jim Grosbachf016b0a2011-03-21 22:15:52 +000038namespace llvm {
Jim Grosbachf016b0a2011-03-21 22:15:52 +000039
Juergen Ributzka7608dc02014-03-21 20:28:42 +000040void RuntimeDyldImpl::registerEHFrames() {}
Rafael Espindolafa5942b2013-05-05 20:43:10 +000041
Juergen Ributzka7608dc02014-03-21 20:28:42 +000042void RuntimeDyldImpl::deregisterEHFrames() {}
Andrew Kaylorc442a762013-10-16 00:14:21 +000043
Jim Grosbach733d3052011-04-12 21:20:41 +000044// Resolve the relocations for all symbols we currently know about.
45void RuntimeDyldImpl::resolveRelocations() {
Andrew Kaylor4fba0492013-10-21 17:42:06 +000046 MutexGuard locked(lock);
47
Preston Gurd2138ef62012-04-12 20:13:57 +000048 // First, resolve relocations associated with external symbols.
Eli Benderskyb92e1cf2012-04-30 12:15:58 +000049 resolveExternalSymbols();
Danil Malyshev70d22cc2012-03-30 16:45:19 +000050
Jim Grosbacheff0a402012-01-16 22:26:39 +000051 // Just iterate over the sections we have and resolve all the relocations
52 // in them. Gross overkill, but it gets the job done.
53 for (int i = 0, e = Sections.size(); i != e; ++i) {
Andrew Kaylor5f3a9982013-08-19 19:38:06 +000054 // The Section here (Sections[i]) refers to the section in which the
55 // symbol for the relocation is located. The SectionID in the relocation
56 // entry provides the section to which the relocation will be applied.
Andrew Kaylora714efc2012-11-05 20:57:16 +000057 uint64_t Addr = Sections[i].LoadAddress;
Jim Grosbach36f025e2014-04-21 19:23:59 +000058 DEBUG(dbgs() << "Resolving relocations Section #" << i << "\t"
Juergen Ributzka7608dc02014-03-21 20:28:42 +000059 << format("%p", (uint8_t *)Addr) << "\n");
Andrew Kaylora714efc2012-11-05 20:57:16 +000060 resolveRelocationList(Relocations[i], Addr);
Andrew Kaylorea395922013-10-01 01:47:35 +000061 Relocations.erase(i);
Jim Grosbacheff0a402012-01-16 22:26:39 +000062 }
Jim Grosbach733d3052011-04-12 21:20:41 +000063}
64
Jim Grosbach6d613972012-09-13 21:50:06 +000065void RuntimeDyldImpl::mapSectionAddress(const void *LocalAddress,
Jim Grosbach0ddb3a42012-01-16 23:50:55 +000066 uint64_t TargetAddress) {
Andrew Kaylor4fba0492013-10-21 17:42:06 +000067 MutexGuard locked(lock);
Danil Malyshev70d22cc2012-03-30 16:45:19 +000068 for (unsigned i = 0, e = Sections.size(); i != e; ++i) {
69 if (Sections[i].Address == LocalAddress) {
70 reassignSectionAddress(i, TargetAddress);
71 return;
72 }
73 }
74 llvm_unreachable("Attempting to remap address of unknown section!");
Jim Grosbach0ddb3a42012-01-16 23:50:55 +000075}
76
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +000077static std::error_code getOffset(const SymbolRef &Sym, uint64_t &Result) {
Rafael Espindola6956b1a2014-04-21 13:45:32 +000078 uint64_t Address;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +000079 if (std::error_code EC = Sym.getAddress(Address))
Rafael Espindola6956b1a2014-04-21 13:45:32 +000080 return EC;
81
82 if (Address == UnknownAddressOrSize) {
83 Result = UnknownAddressOrSize;
84 return object_error::success;
85 }
86
87 const ObjectFile *Obj = Sym.getObject();
88 section_iterator SecI(Obj->section_begin());
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +000089 if (std::error_code EC = Sym.getSection(SecI))
Rafael Espindola6956b1a2014-04-21 13:45:32 +000090 return EC;
91
92 if (SecI == Obj->section_end()) {
93 Result = UnknownAddressOrSize;
94 return object_error::success;
95 }
96
97 uint64_t SectionAddress;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +000098 if (std::error_code EC = SecI->getAddress(SectionAddress))
Rafael Espindola6956b1a2014-04-21 13:45:32 +000099 return EC;
100
101 Result = Address - SectionAddress;
102 return object_error::success;
103}
104
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000105ObjectImage *RuntimeDyldImpl::loadObject(ObjectImage *InputObject) {
Andrew Kaylor4fba0492013-10-21 17:42:06 +0000106 MutexGuard locked(lock);
107
Ahmed Charles56440fd2014-03-06 05:51:42 +0000108 std::unique_ptr<ObjectImage> Obj(InputObject);
Lang Hames937ec542014-02-12 21:30:07 +0000109 if (!Obj)
Craig Topper353eda42014-04-24 06:44:33 +0000110 return nullptr;
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000111
Andrew Kaylor33c5b1b2013-10-15 20:44:55 +0000112 // Save information about our target
Lang Hames937ec542014-02-12 21:30:07 +0000113 Arch = (Triple::ArchType)Obj->getArch();
114 IsTargetLittleEndian = Obj->getObjectFile()->isLittleEndian();
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000115
Lang Hames937ec542014-02-12 21:30:07 +0000116 // Compute the memory size required to load all sections to be loaded
117 // and pass this information to the memory manager
118 if (MemMgr->needsToReserveAllocationSpace()) {
119 uint64_t CodeSize = 0, DataSizeRO = 0, DataSizeRW = 0;
120 computeTotalAllocSize(*Obj, CodeSize, DataSizeRO, DataSizeRW);
121 MemMgr->reserveAllocationSpace(CodeSize, DataSizeRO, DataSizeRW);
122 }
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000123
Eli Benderskyfc079082012-05-01 06:58:59 +0000124 // Symbols found in this object
125 StringMap<SymbolLoc> LocalSymbols;
126 // Used sections from the object file
127 ObjSectionToIDMap LocalSections;
128
Tim Northover94bc73d2012-10-29 10:47:04 +0000129 // Common symbols requiring allocation, with their sizes and alignments
Eli Benderskyfc079082012-05-01 06:58:59 +0000130 CommonSymbolMap CommonSymbols;
Tim Northover94bc73d2012-10-29 10:47:04 +0000131 // Maximum required total memory to allocate all common symbols
Eli Benderskyfc079082012-05-01 06:58:59 +0000132 uint64_t CommonSize = 0;
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000133
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000134 // Parse symbols
135 DEBUG(dbgs() << "Parse symbols:\n");
Jim Grosbach36f025e2014-04-21 19:23:59 +0000136 for (symbol_iterator I = Obj->begin_symbols(), E = Obj->end_symbols(); I != E;
137 ++I) {
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000138 object::SymbolRef::Type SymType;
139 StringRef Name;
Jim Grosbach36f025e2014-04-21 19:23:59 +0000140 Check(I->getType(SymType));
141 Check(I->getName(Name));
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000142
Jim Grosbach36f025e2014-04-21 19:23:59 +0000143 uint32_t Flags = I->getFlags();
Preston Gurd2138ef62012-04-12 20:13:57 +0000144
Lang Hames937ec542014-02-12 21:30:07 +0000145 bool IsCommon = Flags & SymbolRef::SF_Common;
146 if (IsCommon) {
Preston Gurd2138ef62012-04-12 20:13:57 +0000147 // Add the common symbols to a list. We'll allocate them all below.
Lang Hames36072da2014-05-12 21:39:59 +0000148 if (!GlobalSymbolTable.count(Name)) {
149 uint32_t Align;
150 Check(I->getAlignment(Align));
151 uint64_t Size = 0;
152 Check(I->getSize(Size));
153 CommonSize += Size + Align;
154 CommonSymbols[*I] = CommonSymbolInfo(Size, Align);
155 }
Preston Gurd2138ef62012-04-12 20:13:57 +0000156 } else {
157 if (SymType == object::SymbolRef::ST_Function ||
Akira Hatanaka111174b2012-08-17 21:28:04 +0000158 SymType == object::SymbolRef::ST_Data ||
159 SymType == object::SymbolRef::ST_Unknown) {
Rafael Espindola6956b1a2014-04-21 13:45:32 +0000160 uint64_t SectOffset;
Eli Bendersky0e2ac5b2012-04-29 12:40:47 +0000161 StringRef SectionData;
Tim Northoverd05e6b52012-12-17 17:59:35 +0000162 bool IsCode;
Lang Hames937ec542014-02-12 21:30:07 +0000163 section_iterator SI = Obj->end_sections();
Jim Grosbach36f025e2014-04-21 19:23:59 +0000164 Check(getOffset(*I, SectOffset));
165 Check(I->getSection(SI));
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000166 if (SI == Obj->end_sections())
167 continue;
Lang Hames937ec542014-02-12 21:30:07 +0000168 Check(SI->getContents(SectionData));
169 Check(SI->isText(IsCode));
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000170 unsigned SectionID =
171 findOrEmitSection(*Obj, *SI, IsCode, LocalSections);
Preston Gurd2138ef62012-04-12 20:13:57 +0000172 LocalSymbols[Name.data()] = SymbolLoc(SectionID, SectOffset);
Rafael Espindola6956b1a2014-04-21 13:45:32 +0000173 DEBUG(dbgs() << "\tOffset: " << format("%p", (uintptr_t)SectOffset)
174 << " flags: " << Flags << " SID: " << SectionID);
Amara Emersonc958bf32012-11-16 11:11:59 +0000175 GlobalSymbolTable[Name] = SymbolLoc(SectionID, SectOffset);
Preston Gurd2138ef62012-04-12 20:13:57 +0000176 }
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000177 }
178 DEBUG(dbgs() << "\tType: " << SymType << " Name: " << Name << "\n");
179 }
180
Preston Gurd2138ef62012-04-12 20:13:57 +0000181 // Allocate common symbols
182 if (CommonSize != 0)
Lang Hames36072da2014-05-12 21:39:59 +0000183 emitCommonSymbols(*Obj, CommonSymbols, CommonSize, GlobalSymbolTable);
Preston Gurd2138ef62012-04-12 20:13:57 +0000184
Eli Bendersky0e2ac5b2012-04-29 12:40:47 +0000185 // Parse and process relocations
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000186 DEBUG(dbgs() << "Parse relocations:\n");
Jim Grosbach36f025e2014-04-21 19:23:59 +0000187 for (section_iterator SI = Obj->begin_sections(), SE = Obj->end_sections();
188 SI != SE; ++SI) {
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000189 unsigned SectionID = 0;
190 StubMap Stubs;
Jim Grosbach36f025e2014-04-21 19:23:59 +0000191 section_iterator RelocatedSection = SI->getRelocatedSection();
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000192
Jim Grosbach36f025e2014-04-21 19:23:59 +0000193 relocation_iterator I = SI->relocation_begin();
194 relocation_iterator E = SI->relocation_end();
Rafael Espindola77314aa2014-04-03 22:42:22 +0000195
196 if (I == E && !ProcessAllSections)
Juergen Ributzka046709f2014-03-21 07:26:41 +0000197 continue;
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000198
Juergen Ributzka046709f2014-03-21 07:26:41 +0000199 bool IsCode = false;
200 Check(RelocatedSection->isText(IsCode));
201 SectionID =
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000202 findOrEmitSection(*Obj, *RelocatedSection, IsCode, LocalSections);
Juergen Ributzka046709f2014-03-21 07:26:41 +0000203 DEBUG(dbgs() << "\tSectionID: " << SectionID << "\n");
204
Rafael Espindola77314aa2014-04-03 22:42:22 +0000205 for (; I != E;)
Juergen Ributzka046709f2014-03-21 07:26:41 +0000206 I = processRelocationRef(SectionID, I, *Obj, LocalSections, LocalSymbols,
207 Stubs);
Lang Hamesf7acddd2014-07-22 22:47:39 +0000208
Lang Hamesf7acddd2014-07-22 22:47:39 +0000209 // If there is an attached checker, notify it about the stubs for this
210 // section so that they can be verified.
211 if (Checker)
212 Checker->registerStubMap(Obj->getImageName(), SectionID, Stubs);
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000213 }
Preston Gurdcc31af92012-04-16 22:12:58 +0000214
Andrew Kaylor4612fed2013-08-19 23:27:43 +0000215 // Give the subclasses a chance to tie-up any loose ends.
Lang Hames36072da2014-05-12 21:39:59 +0000216 finalizeLoad(*Obj, LocalSections);
Andrew Kaylor4612fed2013-08-19 23:27:43 +0000217
Ahmed Charles96c9d952014-03-05 10:19:29 +0000218 return Obj.release();
Lang Hames937ec542014-02-12 21:30:07 +0000219}
220
221// A helper method for computeTotalAllocSize.
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000222// Computes the memory size required to allocate sections with the given sizes,
Lang Hames937ec542014-02-12 21:30:07 +0000223// assuming that all sections are allocated with the given alignment
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000224static uint64_t
225computeAllocationSizeForSections(std::vector<uint64_t> &SectionSizes,
226 uint64_t Alignment) {
Lang Hames937ec542014-02-12 21:30:07 +0000227 uint64_t TotalSize = 0;
Jim Grosbach36f025e2014-04-21 19:23:59 +0000228 for (size_t Idx = 0, Cnt = SectionSizes.size(); Idx < Cnt; Idx++) {
229 uint64_t AlignedSize =
230 (SectionSizes[Idx] + Alignment - 1) / Alignment * Alignment;
Lang Hames937ec542014-02-12 21:30:07 +0000231 TotalSize += AlignedSize;
232 }
233 return TotalSize;
234}
235
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000236// Compute an upper bound of the memory size that is required to load all
237// sections
238void RuntimeDyldImpl::computeTotalAllocSize(ObjectImage &Obj,
239 uint64_t &CodeSize,
240 uint64_t &DataSizeRO,
241 uint64_t &DataSizeRW) {
Lang Hames937ec542014-02-12 21:30:07 +0000242 // Compute the size of all sections required for execution
243 std::vector<uint64_t> CodeSectionSizes;
244 std::vector<uint64_t> ROSectionSizes;
245 std::vector<uint64_t> RWSectionSizes;
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000246 uint64_t MaxAlignment = sizeof(void *);
Lang Hames937ec542014-02-12 21:30:07 +0000247
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000248 // Collect sizes of all sections to be loaded;
Lang Hames937ec542014-02-12 21:30:07 +0000249 // also determine the max alignment of all sections
Jim Grosbach36f025e2014-04-21 19:23:59 +0000250 for (section_iterator SI = Obj.begin_sections(), SE = Obj.end_sections();
251 SI != SE; ++SI) {
252 const SectionRef &Section = *SI;
253
Lang Hames937ec542014-02-12 21:30:07 +0000254 bool IsRequired;
255 Check(Section.isRequiredForExecution(IsRequired));
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000256
Lang Hames937ec542014-02-12 21:30:07 +0000257 // Consider only the sections that are required to be loaded for execution
258 if (IsRequired) {
259 uint64_t DataSize = 0;
260 uint64_t Alignment64 = 0;
261 bool IsCode = false;
262 bool IsReadOnly = false;
263 StringRef Name;
264 Check(Section.getSize(DataSize));
265 Check(Section.getAlignment(Alignment64));
266 Check(Section.isText(IsCode));
267 Check(Section.isReadOnlyData(IsReadOnly));
268 Check(Section.getName(Name));
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000269 unsigned Alignment = (unsigned)Alignment64 & 0xffffffffL;
270
Lang Hames937ec542014-02-12 21:30:07 +0000271 uint64_t StubBufSize = computeSectionStubBufSize(Obj, Section);
272 uint64_t SectionSize = DataSize + StubBufSize;
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000273
274 // The .eh_frame section (at least on Linux) needs an extra four bytes
275 // padded
Lang Hames937ec542014-02-12 21:30:07 +0000276 // with zeroes added at the end. For MachO objects, this section has a
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000277 // slightly different name, so this won't have any effect for MachO
278 // objects.
Lang Hames937ec542014-02-12 21:30:07 +0000279 if (Name == ".eh_frame")
280 SectionSize += 4;
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000281
Lang Hames937ec542014-02-12 21:30:07 +0000282 if (SectionSize > 0) {
283 // save the total size of the section
284 if (IsCode) {
285 CodeSectionSizes.push_back(SectionSize);
286 } else if (IsReadOnly) {
287 ROSectionSizes.push_back(SectionSize);
288 } else {
289 RWSectionSizes.push_back(SectionSize);
290 }
291 // update the max alignment
292 if (Alignment > MaxAlignment) {
293 MaxAlignment = Alignment;
294 }
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000295 }
Lang Hames937ec542014-02-12 21:30:07 +0000296 }
297 }
298
299 // Compute the size of all common symbols
300 uint64_t CommonSize = 0;
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000301 for (symbol_iterator I = Obj.begin_symbols(), E = Obj.end_symbols(); I != E;
302 ++I) {
Lang Hames937ec542014-02-12 21:30:07 +0000303 uint32_t Flags = I->getFlags();
304 if (Flags & SymbolRef::SF_Common) {
305 // Add the common symbols to a list. We'll allocate them all below.
306 uint64_t Size = 0;
307 Check(I->getSize(Size));
308 CommonSize += Size;
309 }
310 }
311 if (CommonSize != 0) {
312 RWSectionSizes.push_back(CommonSize);
313 }
314
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000315 // Compute the required allocation space for each different type of sections
316 // (code, read-only data, read-write data) assuming that all sections are
Lang Hames937ec542014-02-12 21:30:07 +0000317 // allocated with the max alignment. Note that we cannot compute with the
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000318 // individual alignments of the sections, because then the required size
Lang Hames937ec542014-02-12 21:30:07 +0000319 // depends on the order, in which the sections are allocated.
320 CodeSize = computeAllocationSizeForSections(CodeSectionSizes, MaxAlignment);
321 DataSizeRO = computeAllocationSizeForSections(ROSectionSizes, MaxAlignment);
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000322 DataSizeRW = computeAllocationSizeForSections(RWSectionSizes, MaxAlignment);
Lang Hames937ec542014-02-12 21:30:07 +0000323}
324
325// compute stub buffer size for the given section
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000326unsigned RuntimeDyldImpl::computeSectionStubBufSize(ObjectImage &Obj,
Lang Hames937ec542014-02-12 21:30:07 +0000327 const SectionRef &Section) {
328 unsigned StubSize = getMaxStubSize();
329 if (StubSize == 0) {
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000330 return 0;
Lang Hames937ec542014-02-12 21:30:07 +0000331 }
332 // FIXME: this is an inefficient way to handle this. We should computed the
333 // necessary section allocation size in loadObject by walking all the sections
334 // once.
335 unsigned StubBufSize = 0;
Jim Grosbach36f025e2014-04-21 19:23:59 +0000336 for (section_iterator SI = Obj.begin_sections(), SE = Obj.end_sections();
337 SI != SE; ++SI) {
338 section_iterator RelSecI = SI->getRelocatedSection();
Lang Hames937ec542014-02-12 21:30:07 +0000339 if (!(RelSecI == Section))
340 continue;
341
Jim Grosbach36f025e2014-04-21 19:23:59 +0000342 for (const RelocationRef &Reloc : SI->relocations()) {
Alexey Samsonovaa4d2952014-03-14 14:22:49 +0000343 (void)Reloc;
Lang Hames937ec542014-02-12 21:30:07 +0000344 StubBufSize += StubSize;
345 }
346 }
Alexey Samsonovaa4d2952014-03-14 14:22:49 +0000347
Lang Hames937ec542014-02-12 21:30:07 +0000348 // Get section data size and alignment
349 uint64_t Alignment64;
350 uint64_t DataSize;
351 Check(Section.getSize(DataSize));
352 Check(Section.getAlignment(Alignment64));
353
354 // Add stubbuf size alignment
355 unsigned Alignment = (unsigned)Alignment64 & 0xffffffffL;
356 unsigned StubAlignment = getStubAlignment();
357 unsigned EndAlignment = (DataSize | Alignment) & -(DataSize | Alignment);
358 if (StubAlignment > EndAlignment)
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000359 StubBufSize += StubAlignment - EndAlignment;
Lang Hames937ec542014-02-12 21:30:07 +0000360 return StubBufSize;
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000361}
362
Eli Bendersky667b8792012-05-01 10:41:12 +0000363void RuntimeDyldImpl::emitCommonSymbols(ObjectImage &Obj,
364 const CommonSymbolMap &CommonSymbols,
365 uint64_t TotalSize,
366 SymbolTableMap &SymbolTable) {
Preston Gurd2138ef62012-04-12 20:13:57 +0000367 // Allocate memory for the section
368 unsigned SectionID = Sections.size();
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000369 uint8_t *Addr = MemMgr->allocateDataSection(TotalSize, sizeof(void *),
370 SectionID, StringRef(), false);
Preston Gurd2138ef62012-04-12 20:13:57 +0000371 if (!Addr)
372 report_fatal_error("Unable to allocate memory for common symbols!");
373 uint64_t Offset = 0;
Rafael Espindolafa5942b2013-05-05 20:43:10 +0000374 Sections.push_back(SectionEntry(StringRef(), Addr, TotalSize, 0));
Preston Gurd2138ef62012-04-12 20:13:57 +0000375 memset(Addr, 0, TotalSize);
376
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000377 DEBUG(dbgs() << "emitCommonSection SectionID: " << SectionID << " new addr: "
378 << format("%p", Addr) << " DataSize: " << TotalSize << "\n");
Preston Gurd2138ef62012-04-12 20:13:57 +0000379
380 // Assign the address of each symbol
Jim Grosbach36f025e2014-04-21 19:23:59 +0000381 for (CommonSymbolMap::const_iterator it = CommonSymbols.begin(),
382 itEnd = CommonSymbols.end(); it != itEnd; ++it) {
383 uint64_t Size = it->second.first;
384 uint64_t Align = it->second.second;
Preston Gurd2138ef62012-04-12 20:13:57 +0000385 StringRef Name;
Jim Grosbach36f025e2014-04-21 19:23:59 +0000386 it->first.getName(Name);
Tim Northover94bc73d2012-10-29 10:47:04 +0000387 if (Align) {
388 // This symbol has an alignment requirement.
389 uint64_t AlignOffset = OffsetToAlignment((uint64_t)Addr, Align);
390 Addr += AlignOffset;
391 Offset += AlignOffset;
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000392 DEBUG(dbgs() << "Allocating common symbol " << Name << " address "
393 << format("%p\n", Addr));
Tim Northover94bc73d2012-10-29 10:47:04 +0000394 }
Jim Grosbach36f025e2014-04-21 19:23:59 +0000395 Obj.updateSymbolAddress(it->first, (uint64_t)Addr);
Eli Bendersky667b8792012-05-01 10:41:12 +0000396 SymbolTable[Name.data()] = SymbolLoc(SectionID, Offset);
Preston Gurd2138ef62012-04-12 20:13:57 +0000397 Offset += Size;
398 Addr += Size;
399 }
Preston Gurd2138ef62012-04-12 20:13:57 +0000400}
401
Preston Gurdcc31af92012-04-16 22:12:58 +0000402unsigned RuntimeDyldImpl::emitSection(ObjectImage &Obj,
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000403 const SectionRef &Section, bool IsCode) {
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000404
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000405 StringRef data;
406 uint64_t Alignment64;
407 Check(Section.getContents(data));
408 Check(Section.getAlignment(Alignment64));
409
410 unsigned Alignment = (unsigned)Alignment64 & 0xffffffffL;
Preston Gurd2138ef62012-04-12 20:13:57 +0000411 bool IsRequired;
412 bool IsVirtual;
413 bool IsZeroInit;
Andrew Kaylora342cb92012-11-15 23:50:01 +0000414 bool IsReadOnly;
Preston Gurd2138ef62012-04-12 20:13:57 +0000415 uint64_t DataSize;
Andrew Kaylor877b9312013-10-16 00:32:24 +0000416 unsigned PaddingSize = 0;
Lang Hames937ec542014-02-12 21:30:07 +0000417 unsigned StubBufSize = 0;
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000418 StringRef Name;
Preston Gurd2138ef62012-04-12 20:13:57 +0000419 Check(Section.isRequiredForExecution(IsRequired));
420 Check(Section.isVirtual(IsVirtual));
421 Check(Section.isZeroInit(IsZeroInit));
Andrew Kaylora342cb92012-11-15 23:50:01 +0000422 Check(Section.isReadOnlyData(IsReadOnly));
Preston Gurd2138ef62012-04-12 20:13:57 +0000423 Check(Section.getSize(DataSize));
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000424 Check(Section.getName(Name));
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000425
426 StubBufSize = computeSectionStubBufSize(Obj, Section);
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000427
Andrew Kaylor877b9312013-10-16 00:32:24 +0000428 // The .eh_frame section (at least on Linux) needs an extra four bytes padded
429 // with zeroes added at the end. For MachO objects, this section has a
430 // slightly different name, so this won't have any effect for MachO objects.
431 if (Name == ".eh_frame")
432 PaddingSize = 4;
433
Lang Hamesd4100172014-02-11 05:28:24 +0000434 uintptr_t Allocate;
Preston Gurd2138ef62012-04-12 20:13:57 +0000435 unsigned SectionID = Sections.size();
436 uint8_t *Addr;
Craig Topper353eda42014-04-24 06:44:33 +0000437 const char *pData = nullptr;
Preston Gurd2138ef62012-04-12 20:13:57 +0000438
439 // Some sections, such as debug info, don't need to be loaded for execution.
440 // Leave those where they are.
441 if (IsRequired) {
Andrew Kaylor877b9312013-10-16 00:32:24 +0000442 Allocate = DataSize + PaddingSize + StubBufSize;
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000443 Addr = IsCode ? MemMgr->allocateCodeSection(Allocate, Alignment, SectionID,
444 Name)
445 : MemMgr->allocateDataSection(Allocate, Alignment, SectionID,
446 Name, IsReadOnly);
Preston Gurd2138ef62012-04-12 20:13:57 +0000447 if (!Addr)
448 report_fatal_error("Unable to allocate section memory!");
449
450 // Virtual sections have no data in the object image, so leave pData = 0
451 if (!IsVirtual)
452 pData = data.data();
453
454 // Zero-initialize or copy the data from the image
455 if (IsZeroInit || IsVirtual)
456 memset(Addr, 0, DataSize);
457 else
458 memcpy(Addr, pData, DataSize);
459
Andrew Kaylor877b9312013-10-16 00:32:24 +0000460 // Fill in any extra bytes we allocated for padding
461 if (PaddingSize != 0) {
462 memset(Addr + DataSize, 0, PaddingSize);
463 // Update the DataSize variable so that the stub offset is set correctly.
464 DataSize += PaddingSize;
465 }
466
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000467 DEBUG(dbgs() << "emitSection SectionID: " << SectionID << " Name: " << Name
Preston Gurd2138ef62012-04-12 20:13:57 +0000468 << " obj addr: " << format("%p", pData)
469 << " new addr: " << format("%p", Addr)
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000470 << " DataSize: " << DataSize << " StubBufSize: " << StubBufSize
471 << " Allocate: " << Allocate << "\n");
Preston Gurdcc31af92012-04-16 22:12:58 +0000472 Obj.updateSectionAddress(Section, (uint64_t)Addr);
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000473 } else {
Preston Gurd2138ef62012-04-12 20:13:57 +0000474 // Even if we didn't load the section, we need to record an entry for it
Eli Bendersky0e2ac5b2012-04-29 12:40:47 +0000475 // to handle later processing (and by 'handle' I mean don't do anything
476 // with these sections).
Preston Gurd2138ef62012-04-12 20:13:57 +0000477 Allocate = 0;
Craig Topper353eda42014-04-24 06:44:33 +0000478 Addr = nullptr;
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000479 DEBUG(dbgs() << "emitSection SectionID: " << SectionID << " Name: " << Name
480 << " obj addr: " << format("%p", data.data()) << " new addr: 0"
481 << " DataSize: " << DataSize << " StubBufSize: " << StubBufSize
482 << " Allocate: " << Allocate << "\n");
Preston Gurd2138ef62012-04-12 20:13:57 +0000483 }
484
Rafael Espindolafa5942b2013-05-05 20:43:10 +0000485 Sections.push_back(SectionEntry(Name, Addr, DataSize, (uintptr_t)pData));
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000486 return SectionID;
487}
488
Preston Gurdcc31af92012-04-16 22:12:58 +0000489unsigned RuntimeDyldImpl::findOrEmitSection(ObjectImage &Obj,
490 const SectionRef &Section,
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000491 bool IsCode,
492 ObjSectionToIDMap &LocalSections) {
493
494 unsigned SectionID = 0;
495 ObjSectionToIDMap::iterator i = LocalSections.find(Section);
496 if (i != LocalSections.end())
497 SectionID = i->second;
498 else {
Preston Gurdcc31af92012-04-16 22:12:58 +0000499 SectionID = emitSection(Obj, Section, IsCode);
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000500 LocalSections[Section] = SectionID;
501 }
502 return SectionID;
503}
504
Eli Bendersky667b8792012-05-01 10:41:12 +0000505void RuntimeDyldImpl::addRelocationForSection(const RelocationEntry &RE,
506 unsigned SectionID) {
507 Relocations[SectionID].push_back(RE);
508}
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000509
Eli Bendersky667b8792012-05-01 10:41:12 +0000510void RuntimeDyldImpl::addRelocationForSymbol(const RelocationEntry &RE,
511 StringRef SymbolName) {
512 // Relocation by symbol. If the symbol is found in the global symbol table,
513 // create an appropriate section relocation. Otherwise, add it to
514 // ExternalSymbolRelocations.
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000515 SymbolTableMap::const_iterator Loc = GlobalSymbolTable.find(SymbolName);
Eli Bendersky667b8792012-05-01 10:41:12 +0000516 if (Loc == GlobalSymbolTable.end()) {
517 ExternalSymbolRelocations[SymbolName].push_back(RE);
Eli Benderskyb92e1cf2012-04-30 12:15:58 +0000518 } else {
Eli Bendersky667b8792012-05-01 10:41:12 +0000519 // Copy the RE since we want to modify its addend.
520 RelocationEntry RECopy = RE;
521 RECopy.Addend += Loc->second.second;
522 Relocations[Loc->second.first].push_back(RECopy);
Eli Benderskyb92e1cf2012-04-30 12:15:58 +0000523 }
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000524}
525
Ulrich Weigand752b5c92014-07-20 23:53:14 +0000526uint8_t *RuntimeDyldImpl::createStubFunction(uint8_t *Addr,
527 unsigned AbiVariant) {
Tim Northovere19bed72014-07-23 12:32:47 +0000528 if (Arch == Triple::aarch64 || Arch == Triple::aarch64_be) {
Tim Northover37cde972013-05-04 20:14:09 +0000529 // This stub has to be able to access the full address space,
530 // since symbol lookup won't necessarily find a handy, in-range,
531 // PLT stub for functions which could be anywhere.
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000532 uint32_t *StubAddr = (uint32_t *)Addr;
Tim Northover37cde972013-05-04 20:14:09 +0000533
534 // Stub can use ip0 (== x16) to calculate address
535 *StubAddr = 0xd2e00010; // movz ip0, #:abs_g3:<addr>
536 StubAddr++;
537 *StubAddr = 0xf2c00010; // movk ip0, #:abs_g2_nc:<addr>
538 StubAddr++;
539 *StubAddr = 0xf2a00010; // movk ip0, #:abs_g1_nc:<addr>
540 StubAddr++;
541 *StubAddr = 0xf2800010; // movk ip0, #:abs_g0_nc:<addr>
542 StubAddr++;
543 *StubAddr = 0xd61f0200; // br ip0
544
545 return Addr;
Christian Pirker2a111602014-03-28 14:35:30 +0000546 } else if (Arch == Triple::arm || Arch == Triple::armeb) {
Akira Hatanaka111174b2012-08-17 21:28:04 +0000547 // TODO: There is only ARM far stub now. We should add the Thumb stub,
548 // and stubs for branches Thumb - ARM and ARM - Thumb.
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000549 uint32_t *StubAddr = (uint32_t *)Addr;
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000550 *StubAddr = 0xe51ff004; // ldr pc,<label>
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000551 return (uint8_t *)++StubAddr;
NAKAMURA Takumif97efd92012-12-04 00:08:14 +0000552 } else if (Arch == Triple::mipsel || Arch == Triple::mips) {
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000553 uint32_t *StubAddr = (uint32_t *)Addr;
Akira Hatanaka111174b2012-08-17 21:28:04 +0000554 // 0: 3c190000 lui t9,%hi(addr).
555 // 4: 27390000 addiu t9,t9,%lo(addr).
556 // 8: 03200008 jr t9.
557 // c: 00000000 nop.
558 const unsigned LuiT9Instr = 0x3c190000, AdduiT9Instr = 0x27390000;
559 const unsigned JrT9Instr = 0x03200008, NopInstr = 0x0;
560
561 *StubAddr = LuiT9Instr;
562 StubAddr++;
563 *StubAddr = AdduiT9Instr;
564 StubAddr++;
565 *StubAddr = JrT9Instr;
566 StubAddr++;
567 *StubAddr = NopInstr;
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000568 return Addr;
Bill Schmidt0a9170d2013-07-26 01:35:43 +0000569 } else if (Arch == Triple::ppc64 || Arch == Triple::ppc64le) {
Ulrich Weigand752b5c92014-07-20 23:53:14 +0000570 // Depending on which version of the ELF ABI is in use, we need to
571 // generate one of two variants of the stub. They both start with
572 // the same sequence to load the target address into r12.
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000573 writeInt32BE(Addr, 0x3D800000); // lis r12, highest(addr)
574 writeInt32BE(Addr+4, 0x618C0000); // ori r12, higher(addr)
575 writeInt32BE(Addr+8, 0x798C07C6); // sldi r12, r12, 32
576 writeInt32BE(Addr+12, 0x658C0000); // oris r12, r12, h(addr)
577 writeInt32BE(Addr+16, 0x618C0000); // ori r12, r12, l(addr)
Ulrich Weigand752b5c92014-07-20 23:53:14 +0000578 if (AbiVariant == 2) {
579 // PowerPC64 stub ELFv2 ABI: The address points to the function itself.
580 // The address is already in r12 as required by the ABI. Branch to it.
581 writeInt32BE(Addr+20, 0xF8410018); // std r2, 24(r1)
582 writeInt32BE(Addr+24, 0x7D8903A6); // mtctr r12
583 writeInt32BE(Addr+28, 0x4E800420); // bctr
584 } else {
585 // PowerPC64 stub ELFv1 ABI: The address points to a function descriptor.
586 // Load the function address on r11 and sets it to control register. Also
587 // loads the function TOC in r2 and environment pointer to r11.
588 writeInt32BE(Addr+20, 0xF8410028); // std r2, 40(r1)
589 writeInt32BE(Addr+24, 0xE96C0000); // ld r11, 0(r12)
590 writeInt32BE(Addr+28, 0xE84C0008); // ld r2, 0(r12)
591 writeInt32BE(Addr+32, 0x7D6903A6); // mtctr r11
592 writeInt32BE(Addr+36, 0xE96C0010); // ld r11, 16(r2)
593 writeInt32BE(Addr+40, 0x4E800420); // bctr
594 }
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000595 return Addr;
Richard Sandifordca044082013-05-03 14:15:35 +0000596 } else if (Arch == Triple::systemz) {
597 writeInt16BE(Addr, 0xC418); // lgrl %r1,.+8
598 writeInt16BE(Addr+2, 0x0000);
599 writeInt16BE(Addr+4, 0x0004);
600 writeInt16BE(Addr+6, 0x07F1); // brc 15,%r1
601 // 8-byte address stored at Addr + 8
602 return Addr;
Andrew Kaylor4612fed2013-08-19 23:27:43 +0000603 } else if (Arch == Triple::x86_64) {
604 *Addr = 0xFF; // jmp
605 *(Addr+1) = 0x25; // rip
606 // 32-bit PC-relative address of the GOT entry will be stored at Addr+2
Lang Hames36072da2014-05-12 21:39:59 +0000607 } else if (Arch == Triple::x86) {
608 *Addr = 0xE9; // 32-bit pc-relative jump.
Akira Hatanaka111174b2012-08-17 21:28:04 +0000609 }
610 return Addr;
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000611}
612
613// Assign an address to a symbol name and resolve all the relocations
614// associated with it.
615void RuntimeDyldImpl::reassignSectionAddress(unsigned SectionID,
616 uint64_t Addr) {
617 // The address to use for relocation resolution is not
618 // the address of the local section buffer. We must be doing
Andrew Kaylora714efc2012-11-05 20:57:16 +0000619 // a remote execution environment of some sort. Relocations can't
620 // be applied until all the sections have been moved. The client must
621 // trigger this with a call to MCJIT::finalize() or
622 // RuntimeDyld::resolveRelocations().
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000623 //
624 // Addr is a uint64_t because we can't assume the pointer width
625 // of the target is the same as that of the host. Just use a generic
626 // "big enough" type.
627 Sections[SectionID].LoadAddress = Addr;
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000628}
629
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000630void RuntimeDyldImpl::resolveRelocationList(const RelocationList &Relocs,
631 uint64_t Value) {
632 for (unsigned i = 0, e = Relocs.size(); i != e; ++i) {
Rafael Espindolaf1f1c622013-04-29 17:24:34 +0000633 const RelocationEntry &RE = Relocs[i];
634 // Ignore relocations for sections that were not loaded
Craig Topper353eda42014-04-24 06:44:33 +0000635 if (Sections[RE.SectionID].Address == nullptr)
Rafael Espindolaf1f1c622013-04-29 17:24:34 +0000636 continue;
637 resolveRelocation(RE, Value);
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000638 }
639}
640
Eli Benderskyb92e1cf2012-04-30 12:15:58 +0000641void RuntimeDyldImpl::resolveExternalSymbols() {
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000642 while (!ExternalSymbolRelocations.empty()) {
Andrew Kaylorea395922013-10-01 01:47:35 +0000643 StringMap<RelocationList>::iterator i = ExternalSymbolRelocations.begin();
644
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000645 StringRef Name = i->first();
Andrew Kaylorea395922013-10-01 01:47:35 +0000646 if (Name.size() == 0) {
647 // This is an absolute symbol, use an address of zero.
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000648 DEBUG(dbgs() << "Resolving absolute relocations."
649 << "\n");
Andrew Kaylorcfb4a992013-11-11 19:55:10 +0000650 RelocationList &Relocs = i->second;
Andrew Kaylorea395922013-10-01 01:47:35 +0000651 resolveRelocationList(Relocs, 0);
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000652 } else {
Andrew Kaylorea395922013-10-01 01:47:35 +0000653 uint64_t Addr = 0;
654 SymbolTableMap::const_iterator Loc = GlobalSymbolTable.find(Name);
655 if (Loc == GlobalSymbolTable.end()) {
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000656 // This is an external symbol, try to get its address from
657 // MemoryManager.
658 Addr = MemMgr->getSymbolAddress(Name.data());
659 // The call to getSymbolAddress may have caused additional modules to
660 // be loaded, which may have added new entries to the
661 // ExternalSymbolRelocations map. Consquently, we need to update our
662 // iterator. This is also why retrieval of the relocation list
663 // associated with this symbol is deferred until below this point.
664 // New entries may have been added to the relocation list.
665 i = ExternalSymbolRelocations.find(Name);
Andrew Kaylorea395922013-10-01 01:47:35 +0000666 } else {
667 // We found the symbol in our global table. It was probably in a
668 // Module that we loaded previously.
Yaron Kerenc9802882013-10-19 09:04:26 +0000669 SymbolLoc SymLoc = Loc->second;
Andrew Kaylorea395922013-10-01 01:47:35 +0000670 Addr = getSectionLoadAddress(SymLoc.first) + SymLoc.second;
671 }
672
673 // FIXME: Implement error handling that doesn't kill the host program!
674 if (!Addr)
675 report_fatal_error("Program used external function '" + Name +
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000676 "' which could not be resolved!");
Andrew Kaylorea395922013-10-01 01:47:35 +0000677
678 updateGOTEntries(Name, Addr);
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000679 DEBUG(dbgs() << "Resolving relocations Name: " << Name << "\t"
680 << format("0x%lx", Addr) << "\n");
Andrew Kaylorcfb4a992013-11-11 19:55:10 +0000681 // This list may have been updated when we called getSymbolAddress, so
682 // don't change this code to get the list earlier.
683 RelocationList &Relocs = i->second;
Andrew Kaylorea395922013-10-01 01:47:35 +0000684 resolveRelocationList(Relocs, Addr);
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000685 }
Andrew Kaylorea395922013-10-01 01:47:35 +0000686
Andrew Kaylorcfb4a992013-11-11 19:55:10 +0000687 ExternalSymbolRelocations.erase(i);
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000688 }
689}
690
Jim Grosbachf016b0a2011-03-21 22:15:52 +0000691//===----------------------------------------------------------------------===//
692// RuntimeDyld class implementation
Danil Malyshev72510f22011-07-13 07:57:58 +0000693RuntimeDyld::RuntimeDyld(RTDyldMemoryManager *mm) {
Andrew Kaylora342cb92012-11-15 23:50:01 +0000694 // FIXME: There's a potential issue lurking here if a single instance of
695 // RuntimeDyld is used to load multiple objects. The current implementation
696 // associates a single memory manager with a RuntimeDyld instance. Even
697 // though the public class spawns a new 'impl' instance for each load,
698 // they share a single memory manager. This can become a problem when page
699 // permissions are applied.
Craig Topper353eda42014-04-24 06:44:33 +0000700 Dyld = nullptr;
Danil Malyshev72510f22011-07-13 07:57:58 +0000701 MM = mm;
Lang Hames868d4b32014-03-20 21:06:46 +0000702 ProcessAllSections = false;
Lang Hamesf7acddd2014-07-22 22:47:39 +0000703 Checker = nullptr;
Jim Grosbachf016b0a2011-03-21 22:15:52 +0000704}
705
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000706RuntimeDyld::~RuntimeDyld() { delete Dyld; }
Jim Grosbachf016b0a2011-03-21 22:15:52 +0000707
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000708static std::unique_ptr<RuntimeDyldELF>
Lang Hamesf7acddd2014-07-22 22:47:39 +0000709createRuntimeDyldELF(RTDyldMemoryManager *MM, bool ProcessAllSections,
710 RuntimeDyldCheckerImpl *Checker) {
Lang Hames868d4b32014-03-20 21:06:46 +0000711 std::unique_ptr<RuntimeDyldELF> Dyld(new RuntimeDyldELF(MM));
712 Dyld->setProcessAllSections(ProcessAllSections);
Lang Hamesf7acddd2014-07-22 22:47:39 +0000713 Dyld->setRuntimeDyldChecker(Checker);
Lang Hames868d4b32014-03-20 21:06:46 +0000714 return Dyld;
715}
716
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000717static std::unique_ptr<RuntimeDyldMachO>
Lang Hamesa5216882014-07-17 18:54:50 +0000718createRuntimeDyldMachO(Triple::ArchType Arch, RTDyldMemoryManager *MM,
Lang Hamesf7acddd2014-07-22 22:47:39 +0000719 bool ProcessAllSections, RuntimeDyldCheckerImpl *Checker) {
Lang Hamesa5216882014-07-17 18:54:50 +0000720 std::unique_ptr<RuntimeDyldMachO> Dyld(RuntimeDyldMachO::create(Arch, MM));
Lang Hames868d4b32014-03-20 21:06:46 +0000721 Dyld->setProcessAllSections(ProcessAllSections);
Lang Hamesf7acddd2014-07-22 22:47:39 +0000722 Dyld->setRuntimeDyldChecker(Checker);
Lang Hames868d4b32014-03-20 21:06:46 +0000723 return Dyld;
724}
725
David Blaikie7a1e7752014-04-29 21:52:46 +0000726ObjectImage *RuntimeDyld::loadObject(std::unique_ptr<ObjectFile> InputObject) {
Lang Hames951b2352014-03-08 18:45:12 +0000727 std::unique_ptr<ObjectImage> InputImage;
Lang Hames173c69f2014-01-08 04:09:09 +0000728
David Blaikie7a1e7752014-04-29 21:52:46 +0000729 ObjectFile &Obj = *InputObject;
730
Lang Hames951b2352014-03-08 18:45:12 +0000731 if (InputObject->isELF()) {
David Blaikie7a1e7752014-04-29 21:52:46 +0000732 InputImage.reset(RuntimeDyldELF::createObjectImageFromFile(std::move(InputObject)));
Lang Hames951b2352014-03-08 18:45:12 +0000733 if (!Dyld)
Lang Hamesf7acddd2014-07-22 22:47:39 +0000734 Dyld = createRuntimeDyldELF(MM, ProcessAllSections, Checker).release();
Lang Hames951b2352014-03-08 18:45:12 +0000735 } else if (InputObject->isMachO()) {
David Blaikie7a1e7752014-04-29 21:52:46 +0000736 InputImage.reset(RuntimeDyldMachO::createObjectImageFromFile(std::move(InputObject)));
Lang Hames951b2352014-03-08 18:45:12 +0000737 if (!Dyld)
Lang Hamesa5216882014-07-17 18:54:50 +0000738 Dyld = createRuntimeDyldMachO(
739 static_cast<Triple::ArchType>(InputImage->getArch()),
Lang Hamesf7acddd2014-07-22 22:47:39 +0000740 MM, ProcessAllSections, Checker).release();
Lang Hames951b2352014-03-08 18:45:12 +0000741 } else
742 report_fatal_error("Incompatible object format!");
743
David Blaikie7a1e7752014-04-29 21:52:46 +0000744 if (!Dyld->isCompatibleFile(&Obj))
Lang Hames951b2352014-03-08 18:45:12 +0000745 report_fatal_error("Incompatible object format!");
746
747 Dyld->loadObject(InputImage.get());
748 return InputImage.release();
Lang Hames173c69f2014-01-08 04:09:09 +0000749}
750
Andrew Kayloradc70562012-10-02 21:18:39 +0000751ObjectImage *RuntimeDyld::loadObject(ObjectBuffer *InputBuffer) {
Lang Hames951b2352014-03-08 18:45:12 +0000752 std::unique_ptr<ObjectImage> InputImage;
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000753 sys::fs::file_magic Type = sys::fs::identify_magic(InputBuffer->getBuffer());
Lang Hames951b2352014-03-08 18:45:12 +0000754
755 switch (Type) {
756 case sys::fs::file_magic::elf_relocatable:
757 case sys::fs::file_magic::elf_executable:
758 case sys::fs::file_magic::elf_shared_object:
759 case sys::fs::file_magic::elf_core:
760 InputImage.reset(RuntimeDyldELF::createObjectImage(InputBuffer));
761 if (!Dyld)
Lang Hamesf7acddd2014-07-22 22:47:39 +0000762 Dyld = createRuntimeDyldELF(MM, ProcessAllSections, Checker).release();
Lang Hames951b2352014-03-08 18:45:12 +0000763 break;
764 case sys::fs::file_magic::macho_object:
765 case sys::fs::file_magic::macho_executable:
766 case sys::fs::file_magic::macho_fixed_virtual_memory_shared_lib:
767 case sys::fs::file_magic::macho_core:
768 case sys::fs::file_magic::macho_preload_executable:
769 case sys::fs::file_magic::macho_dynamically_linked_shared_lib:
770 case sys::fs::file_magic::macho_dynamic_linker:
771 case sys::fs::file_magic::macho_bundle:
772 case sys::fs::file_magic::macho_dynamically_linked_shared_lib_stub:
773 case sys::fs::file_magic::macho_dsym_companion:
774 InputImage.reset(RuntimeDyldMachO::createObjectImage(InputBuffer));
775 if (!Dyld)
Lang Hamesa5216882014-07-17 18:54:50 +0000776 Dyld = createRuntimeDyldMachO(
777 static_cast<Triple::ArchType>(InputImage->getArch()),
Lang Hamesf7acddd2014-07-22 22:47:39 +0000778 MM, ProcessAllSections, Checker).release();
Lang Hames951b2352014-03-08 18:45:12 +0000779 break;
780 case sys::fs::file_magic::unknown:
781 case sys::fs::file_magic::bitcode:
782 case sys::fs::file_magic::archive:
783 case sys::fs::file_magic::coff_object:
784 case sys::fs::file_magic::coff_import_library:
785 case sys::fs::file_magic::pecoff_executable:
786 case sys::fs::file_magic::macho_universal_binary:
787 case sys::fs::file_magic::windows_resource:
788 report_fatal_error("Incompatible object format!");
Danil Malyshev72510f22011-07-13 07:57:58 +0000789 }
790
Lang Hames951b2352014-03-08 18:45:12 +0000791 if (!Dyld->isCompatibleFormat(InputBuffer))
792 report_fatal_error("Incompatible object format!");
793
794 Dyld->loadObject(InputImage.get());
795 return InputImage.release();
Jim Grosbachf016b0a2011-03-21 22:15:52 +0000796}
797
Jim Grosbach18b81c52011-04-08 17:31:24 +0000798void *RuntimeDyld::getSymbolAddress(StringRef Name) {
Andrew Kaylorea395922013-10-01 01:47:35 +0000799 if (!Dyld)
Craig Topper353eda42014-04-24 06:44:33 +0000800 return nullptr;
Jim Grosbachf016b0a2011-03-21 22:15:52 +0000801 return Dyld->getSymbolAddress(Name);
802}
803
Jim Grosbachdc1123f2012-09-05 16:50:40 +0000804uint64_t RuntimeDyld::getSymbolLoadAddress(StringRef Name) {
Andrew Kaylorea395922013-10-01 01:47:35 +0000805 if (!Dyld)
806 return 0;
Jim Grosbachdc1123f2012-09-05 16:50:40 +0000807 return Dyld->getSymbolLoadAddress(Name);
808}
809
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000810void RuntimeDyld::resolveRelocations() { Dyld->resolveRelocations(); }
Jim Grosbach733d3052011-04-12 21:20:41 +0000811
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000812void RuntimeDyld::reassignSectionAddress(unsigned SectionID, uint64_t Addr) {
Jim Grosbacheff0a402012-01-16 22:26:39 +0000813 Dyld->reassignSectionAddress(SectionID, Addr);
Jim Grosbach733d3052011-04-12 21:20:41 +0000814}
815
Jim Grosbach6d613972012-09-13 21:50:06 +0000816void RuntimeDyld::mapSectionAddress(const void *LocalAddress,
Jim Grosbach0ddb3a42012-01-16 23:50:55 +0000817 uint64_t TargetAddress) {
818 Dyld->mapSectionAddress(LocalAddress, TargetAddress);
819}
820
Juergen Ributzka6ff29a72014-03-26 18:19:27 +0000821bool RuntimeDyld::hasError() { return Dyld->hasError(); }
822
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000823StringRef RuntimeDyld::getErrorString() { return Dyld->getErrorString(); }
Jim Grosbach40411cc2011-03-22 18:19:42 +0000824
Andrew Kaylor7bb13442013-10-11 21:25:48 +0000825void RuntimeDyld::registerEHFrames() {
Andrew Kaylorc442a762013-10-16 00:14:21 +0000826 if (Dyld)
827 Dyld->registerEHFrames();
828}
829
830void RuntimeDyld::deregisterEHFrames() {
831 if (Dyld)
832 Dyld->deregisterEHFrames();
Rafael Espindolafa5942b2013-05-05 20:43:10 +0000833}
834
Jim Grosbachf016b0a2011-03-21 22:15:52 +0000835} // end namespace llvm