blob: d5c683c579e9c53e0899cd240a688ad2ef254ed4 [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
Lang Hamesf4b3b672014-08-25 22:19:14 +000044static void dumpSectionMemory(const SectionEntry &S, StringRef State) {
45 dbgs() << "----- Contents of section " << S.Name << " " << State << " -----";
46
47 const unsigned ColsPerRow = 16;
Lang Hames86b08f02014-08-25 18:37:38 +000048
49 uint8_t *DataAddr = S.Address;
50 uint64_t LoadAddr = S.LoadAddress;
51
52 unsigned StartPadding = LoadAddr & 7;
53 unsigned BytesRemaining = S.Size;
54
55 if (StartPadding) {
Lang Hamesf4b3b672014-08-25 22:19:14 +000056 dbgs() << "\n" << format("0x%08x", LoadAddr & ~(ColsPerRow - 1)) << ":";
Lang Hames86b08f02014-08-25 18:37:38 +000057 while (StartPadding--)
58 dbgs() << " ";
59 }
60
61 while (BytesRemaining > 0) {
Lang Hamesf4b3b672014-08-25 22:19:14 +000062 if ((LoadAddr & (ColsPerRow - 1)) == 0)
Lang Hames86b08f02014-08-25 18:37:38 +000063 dbgs() << "\n" << format("0x%08x", LoadAddr) << ":";
64
65 dbgs() << " " << format("%02x", *DataAddr);
66
67 ++DataAddr;
68 ++LoadAddr;
69 --BytesRemaining;
70 }
71
72 dbgs() << "\n";
73}
74
Jim Grosbach733d3052011-04-12 21:20:41 +000075// Resolve the relocations for all symbols we currently know about.
76void RuntimeDyldImpl::resolveRelocations() {
Andrew Kaylor4fba0492013-10-21 17:42:06 +000077 MutexGuard locked(lock);
78
Preston Gurd2138ef62012-04-12 20:13:57 +000079 // First, resolve relocations associated with external symbols.
Eli Benderskyb92e1cf2012-04-30 12:15:58 +000080 resolveExternalSymbols();
Danil Malyshev70d22cc2012-03-30 16:45:19 +000081
Jim Grosbacheff0a402012-01-16 22:26:39 +000082 // Just iterate over the sections we have and resolve all the relocations
83 // in them. Gross overkill, but it gets the job done.
84 for (int i = 0, e = Sections.size(); i != e; ++i) {
Andrew Kaylor5f3a9982013-08-19 19:38:06 +000085 // The Section here (Sections[i]) refers to the section in which the
86 // symbol for the relocation is located. The SectionID in the relocation
87 // entry provides the section to which the relocation will be applied.
Andrew Kaylora714efc2012-11-05 20:57:16 +000088 uint64_t Addr = Sections[i].LoadAddress;
Jim Grosbach36f025e2014-04-21 19:23:59 +000089 DEBUG(dbgs() << "Resolving relocations Section #" << i << "\t"
Lang Hames86b08f02014-08-25 18:37:38 +000090 << format("0x%x", Addr) << "\n");
Lang Hamesf4b3b672014-08-25 22:19:14 +000091 DEBUG(dumpSectionMemory(Sections[i], "before relocations"));
Andrew Kaylora714efc2012-11-05 20:57:16 +000092 resolveRelocationList(Relocations[i], Addr);
Lang Hamesf4b3b672014-08-25 22:19:14 +000093 DEBUG(dumpSectionMemory(Sections[i], "after relocations"));
Andrew Kaylorea395922013-10-01 01:47:35 +000094 Relocations.erase(i);
Jim Grosbacheff0a402012-01-16 22:26:39 +000095 }
Jim Grosbach733d3052011-04-12 21:20:41 +000096}
97
Jim Grosbach6d613972012-09-13 21:50:06 +000098void RuntimeDyldImpl::mapSectionAddress(const void *LocalAddress,
Jim Grosbach0ddb3a42012-01-16 23:50:55 +000099 uint64_t TargetAddress) {
Andrew Kaylor4fba0492013-10-21 17:42:06 +0000100 MutexGuard locked(lock);
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000101 for (unsigned i = 0, e = Sections.size(); i != e; ++i) {
102 if (Sections[i].Address == LocalAddress) {
103 reassignSectionAddress(i, TargetAddress);
104 return;
105 }
106 }
107 llvm_unreachable("Attempting to remap address of unknown section!");
Jim Grosbach0ddb3a42012-01-16 23:50:55 +0000108}
109
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000110static std::error_code getOffset(const SymbolRef &Sym, uint64_t &Result) {
Rafael Espindola6956b1a2014-04-21 13:45:32 +0000111 uint64_t Address;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000112 if (std::error_code EC = Sym.getAddress(Address))
Rafael Espindola6956b1a2014-04-21 13:45:32 +0000113 return EC;
114
115 if (Address == UnknownAddressOrSize) {
116 Result = UnknownAddressOrSize;
117 return object_error::success;
118 }
119
120 const ObjectFile *Obj = Sym.getObject();
121 section_iterator SecI(Obj->section_begin());
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000122 if (std::error_code EC = Sym.getSection(SecI))
Rafael Espindola6956b1a2014-04-21 13:45:32 +0000123 return EC;
124
125 if (SecI == Obj->section_end()) {
126 Result = UnknownAddressOrSize;
127 return object_error::success;
128 }
129
130 uint64_t SectionAddress;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000131 if (std::error_code EC = SecI->getAddress(SectionAddress))
Rafael Espindola6956b1a2014-04-21 13:45:32 +0000132 return EC;
133
134 Result = Address - SectionAddress;
135 return object_error::success;
136}
137
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000138ObjectImage *RuntimeDyldImpl::loadObject(ObjectImage *InputObject) {
Andrew Kaylor4fba0492013-10-21 17:42:06 +0000139 MutexGuard locked(lock);
140
Ahmed Charles56440fd2014-03-06 05:51:42 +0000141 std::unique_ptr<ObjectImage> Obj(InputObject);
Lang Hames937ec542014-02-12 21:30:07 +0000142 if (!Obj)
Craig Topper353eda42014-04-24 06:44:33 +0000143 return nullptr;
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000144
Andrew Kaylor33c5b1b2013-10-15 20:44:55 +0000145 // Save information about our target
Lang Hames937ec542014-02-12 21:30:07 +0000146 Arch = (Triple::ArchType)Obj->getArch();
147 IsTargetLittleEndian = Obj->getObjectFile()->isLittleEndian();
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000148
Lang Hames937ec542014-02-12 21:30:07 +0000149 // Compute the memory size required to load all sections to be loaded
150 // and pass this information to the memory manager
151 if (MemMgr->needsToReserveAllocationSpace()) {
152 uint64_t CodeSize = 0, DataSizeRO = 0, DataSizeRW = 0;
153 computeTotalAllocSize(*Obj, CodeSize, DataSizeRO, DataSizeRW);
154 MemMgr->reserveAllocationSpace(CodeSize, DataSizeRO, DataSizeRW);
155 }
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000156
Eli Benderskyfc079082012-05-01 06:58:59 +0000157 // Symbols found in this object
158 StringMap<SymbolLoc> LocalSymbols;
159 // Used sections from the object file
160 ObjSectionToIDMap LocalSections;
161
Tim Northover94bc73d2012-10-29 10:47:04 +0000162 // Common symbols requiring allocation, with their sizes and alignments
Eli Benderskyfc079082012-05-01 06:58:59 +0000163 CommonSymbolMap CommonSymbols;
Tim Northover94bc73d2012-10-29 10:47:04 +0000164 // Maximum required total memory to allocate all common symbols
Eli Benderskyfc079082012-05-01 06:58:59 +0000165 uint64_t CommonSize = 0;
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000166
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000167 // Parse symbols
168 DEBUG(dbgs() << "Parse symbols:\n");
Jim Grosbach36f025e2014-04-21 19:23:59 +0000169 for (symbol_iterator I = Obj->begin_symbols(), E = Obj->end_symbols(); I != E;
170 ++I) {
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000171 object::SymbolRef::Type SymType;
172 StringRef Name;
Jim Grosbach36f025e2014-04-21 19:23:59 +0000173 Check(I->getType(SymType));
174 Check(I->getName(Name));
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000175
Jim Grosbach36f025e2014-04-21 19:23:59 +0000176 uint32_t Flags = I->getFlags();
Preston Gurd2138ef62012-04-12 20:13:57 +0000177
Lang Hames937ec542014-02-12 21:30:07 +0000178 bool IsCommon = Flags & SymbolRef::SF_Common;
179 if (IsCommon) {
Preston Gurd2138ef62012-04-12 20:13:57 +0000180 // Add the common symbols to a list. We'll allocate them all below.
Lang Hames36072da2014-05-12 21:39:59 +0000181 if (!GlobalSymbolTable.count(Name)) {
182 uint32_t Align;
183 Check(I->getAlignment(Align));
184 uint64_t Size = 0;
185 Check(I->getSize(Size));
186 CommonSize += Size + Align;
187 CommonSymbols[*I] = CommonSymbolInfo(Size, Align);
188 }
Preston Gurd2138ef62012-04-12 20:13:57 +0000189 } else {
190 if (SymType == object::SymbolRef::ST_Function ||
Akira Hatanaka111174b2012-08-17 21:28:04 +0000191 SymType == object::SymbolRef::ST_Data ||
192 SymType == object::SymbolRef::ST_Unknown) {
Rafael Espindola6956b1a2014-04-21 13:45:32 +0000193 uint64_t SectOffset;
Eli Bendersky0e2ac5b2012-04-29 12:40:47 +0000194 StringRef SectionData;
Tim Northoverd05e6b52012-12-17 17:59:35 +0000195 bool IsCode;
Lang Hames937ec542014-02-12 21:30:07 +0000196 section_iterator SI = Obj->end_sections();
Jim Grosbach36f025e2014-04-21 19:23:59 +0000197 Check(getOffset(*I, SectOffset));
198 Check(I->getSection(SI));
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000199 if (SI == Obj->end_sections())
200 continue;
Lang Hames937ec542014-02-12 21:30:07 +0000201 Check(SI->getContents(SectionData));
202 Check(SI->isText(IsCode));
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000203 unsigned SectionID =
204 findOrEmitSection(*Obj, *SI, IsCode, LocalSections);
Preston Gurd2138ef62012-04-12 20:13:57 +0000205 LocalSymbols[Name.data()] = SymbolLoc(SectionID, SectOffset);
Rafael Espindola6956b1a2014-04-21 13:45:32 +0000206 DEBUG(dbgs() << "\tOffset: " << format("%p", (uintptr_t)SectOffset)
207 << " flags: " << Flags << " SID: " << SectionID);
Amara Emersonc958bf32012-11-16 11:11:59 +0000208 GlobalSymbolTable[Name] = SymbolLoc(SectionID, SectOffset);
Preston Gurd2138ef62012-04-12 20:13:57 +0000209 }
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000210 }
211 DEBUG(dbgs() << "\tType: " << SymType << " Name: " << Name << "\n");
212 }
213
Preston Gurd2138ef62012-04-12 20:13:57 +0000214 // Allocate common symbols
215 if (CommonSize != 0)
Lang Hames36072da2014-05-12 21:39:59 +0000216 emitCommonSymbols(*Obj, CommonSymbols, CommonSize, GlobalSymbolTable);
Preston Gurd2138ef62012-04-12 20:13:57 +0000217
Eli Bendersky0e2ac5b2012-04-29 12:40:47 +0000218 // Parse and process relocations
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000219 DEBUG(dbgs() << "Parse relocations:\n");
Jim Grosbach36f025e2014-04-21 19:23:59 +0000220 for (section_iterator SI = Obj->begin_sections(), SE = Obj->end_sections();
221 SI != SE; ++SI) {
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000222 unsigned SectionID = 0;
223 StubMap Stubs;
Jim Grosbach36f025e2014-04-21 19:23:59 +0000224 section_iterator RelocatedSection = SI->getRelocatedSection();
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000225
Jim Grosbach36f025e2014-04-21 19:23:59 +0000226 relocation_iterator I = SI->relocation_begin();
227 relocation_iterator E = SI->relocation_end();
Rafael Espindola77314aa2014-04-03 22:42:22 +0000228
229 if (I == E && !ProcessAllSections)
Juergen Ributzka046709f2014-03-21 07:26:41 +0000230 continue;
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000231
Juergen Ributzka046709f2014-03-21 07:26:41 +0000232 bool IsCode = false;
233 Check(RelocatedSection->isText(IsCode));
234 SectionID =
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000235 findOrEmitSection(*Obj, *RelocatedSection, IsCode, LocalSections);
Juergen Ributzka046709f2014-03-21 07:26:41 +0000236 DEBUG(dbgs() << "\tSectionID: " << SectionID << "\n");
237
Rafael Espindola77314aa2014-04-03 22:42:22 +0000238 for (; I != E;)
Juergen Ributzka046709f2014-03-21 07:26:41 +0000239 I = processRelocationRef(SectionID, I, *Obj, LocalSections, LocalSymbols,
240 Stubs);
Lang Hamesf7acddd2014-07-22 22:47:39 +0000241
Lang Hamesf7acddd2014-07-22 22:47:39 +0000242 // If there is an attached checker, notify it about the stubs for this
243 // section so that they can be verified.
244 if (Checker)
245 Checker->registerStubMap(Obj->getImageName(), SectionID, Stubs);
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000246 }
Preston Gurdcc31af92012-04-16 22:12:58 +0000247
Andrew Kaylor4612fed2013-08-19 23:27:43 +0000248 // Give the subclasses a chance to tie-up any loose ends.
Lang Hames36072da2014-05-12 21:39:59 +0000249 finalizeLoad(*Obj, LocalSections);
Andrew Kaylor4612fed2013-08-19 23:27:43 +0000250
Ahmed Charles96c9d952014-03-05 10:19:29 +0000251 return Obj.release();
Lang Hames937ec542014-02-12 21:30:07 +0000252}
253
254// A helper method for computeTotalAllocSize.
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000255// Computes the memory size required to allocate sections with the given sizes,
Lang Hames937ec542014-02-12 21:30:07 +0000256// assuming that all sections are allocated with the given alignment
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000257static uint64_t
258computeAllocationSizeForSections(std::vector<uint64_t> &SectionSizes,
259 uint64_t Alignment) {
Lang Hames937ec542014-02-12 21:30:07 +0000260 uint64_t TotalSize = 0;
Jim Grosbach36f025e2014-04-21 19:23:59 +0000261 for (size_t Idx = 0, Cnt = SectionSizes.size(); Idx < Cnt; Idx++) {
262 uint64_t AlignedSize =
263 (SectionSizes[Idx] + Alignment - 1) / Alignment * Alignment;
Lang Hames937ec542014-02-12 21:30:07 +0000264 TotalSize += AlignedSize;
265 }
266 return TotalSize;
267}
268
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000269// Compute an upper bound of the memory size that is required to load all
270// sections
271void RuntimeDyldImpl::computeTotalAllocSize(ObjectImage &Obj,
272 uint64_t &CodeSize,
273 uint64_t &DataSizeRO,
274 uint64_t &DataSizeRW) {
Lang Hames937ec542014-02-12 21:30:07 +0000275 // Compute the size of all sections required for execution
276 std::vector<uint64_t> CodeSectionSizes;
277 std::vector<uint64_t> ROSectionSizes;
278 std::vector<uint64_t> RWSectionSizes;
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000279 uint64_t MaxAlignment = sizeof(void *);
Lang Hames937ec542014-02-12 21:30:07 +0000280
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000281 // Collect sizes of all sections to be loaded;
Lang Hames937ec542014-02-12 21:30:07 +0000282 // also determine the max alignment of all sections
Jim Grosbach36f025e2014-04-21 19:23:59 +0000283 for (section_iterator SI = Obj.begin_sections(), SE = Obj.end_sections();
284 SI != SE; ++SI) {
285 const SectionRef &Section = *SI;
286
Lang Hames937ec542014-02-12 21:30:07 +0000287 bool IsRequired;
288 Check(Section.isRequiredForExecution(IsRequired));
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000289
Lang Hames937ec542014-02-12 21:30:07 +0000290 // Consider only the sections that are required to be loaded for execution
291 if (IsRequired) {
292 uint64_t DataSize = 0;
293 uint64_t Alignment64 = 0;
294 bool IsCode = false;
295 bool IsReadOnly = false;
296 StringRef Name;
297 Check(Section.getSize(DataSize));
298 Check(Section.getAlignment(Alignment64));
299 Check(Section.isText(IsCode));
300 Check(Section.isReadOnlyData(IsReadOnly));
301 Check(Section.getName(Name));
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000302 unsigned Alignment = (unsigned)Alignment64 & 0xffffffffL;
303
Lang Hames937ec542014-02-12 21:30:07 +0000304 uint64_t StubBufSize = computeSectionStubBufSize(Obj, Section);
305 uint64_t SectionSize = DataSize + StubBufSize;
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000306
307 // The .eh_frame section (at least on Linux) needs an extra four bytes
308 // padded
Lang Hames937ec542014-02-12 21:30:07 +0000309 // with zeroes added at the end. For MachO objects, this section has a
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000310 // slightly different name, so this won't have any effect for MachO
311 // objects.
Lang Hames937ec542014-02-12 21:30:07 +0000312 if (Name == ".eh_frame")
313 SectionSize += 4;
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000314
Lang Hames937ec542014-02-12 21:30:07 +0000315 if (SectionSize > 0) {
316 // save the total size of the section
317 if (IsCode) {
318 CodeSectionSizes.push_back(SectionSize);
319 } else if (IsReadOnly) {
320 ROSectionSizes.push_back(SectionSize);
321 } else {
322 RWSectionSizes.push_back(SectionSize);
323 }
324 // update the max alignment
325 if (Alignment > MaxAlignment) {
326 MaxAlignment = Alignment;
327 }
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000328 }
Lang Hames937ec542014-02-12 21:30:07 +0000329 }
330 }
331
332 // Compute the size of all common symbols
333 uint64_t CommonSize = 0;
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000334 for (symbol_iterator I = Obj.begin_symbols(), E = Obj.end_symbols(); I != E;
335 ++I) {
Lang Hames937ec542014-02-12 21:30:07 +0000336 uint32_t Flags = I->getFlags();
337 if (Flags & SymbolRef::SF_Common) {
338 // Add the common symbols to a list. We'll allocate them all below.
339 uint64_t Size = 0;
340 Check(I->getSize(Size));
341 CommonSize += Size;
342 }
343 }
344 if (CommonSize != 0) {
345 RWSectionSizes.push_back(CommonSize);
346 }
347
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000348 // Compute the required allocation space for each different type of sections
349 // (code, read-only data, read-write data) assuming that all sections are
Lang Hames937ec542014-02-12 21:30:07 +0000350 // allocated with the max alignment. Note that we cannot compute with the
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000351 // individual alignments of the sections, because then the required size
Lang Hames937ec542014-02-12 21:30:07 +0000352 // depends on the order, in which the sections are allocated.
353 CodeSize = computeAllocationSizeForSections(CodeSectionSizes, MaxAlignment);
354 DataSizeRO = computeAllocationSizeForSections(ROSectionSizes, MaxAlignment);
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000355 DataSizeRW = computeAllocationSizeForSections(RWSectionSizes, MaxAlignment);
Lang Hames937ec542014-02-12 21:30:07 +0000356}
357
358// compute stub buffer size for the given section
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000359unsigned RuntimeDyldImpl::computeSectionStubBufSize(ObjectImage &Obj,
Lang Hames937ec542014-02-12 21:30:07 +0000360 const SectionRef &Section) {
361 unsigned StubSize = getMaxStubSize();
362 if (StubSize == 0) {
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000363 return 0;
Lang Hames937ec542014-02-12 21:30:07 +0000364 }
365 // FIXME: this is an inefficient way to handle this. We should computed the
366 // necessary section allocation size in loadObject by walking all the sections
367 // once.
368 unsigned StubBufSize = 0;
Jim Grosbach36f025e2014-04-21 19:23:59 +0000369 for (section_iterator SI = Obj.begin_sections(), SE = Obj.end_sections();
370 SI != SE; ++SI) {
371 section_iterator RelSecI = SI->getRelocatedSection();
Lang Hames937ec542014-02-12 21:30:07 +0000372 if (!(RelSecI == Section))
373 continue;
374
Jim Grosbach36f025e2014-04-21 19:23:59 +0000375 for (const RelocationRef &Reloc : SI->relocations()) {
Alexey Samsonovaa4d2952014-03-14 14:22:49 +0000376 (void)Reloc;
Lang Hames937ec542014-02-12 21:30:07 +0000377 StubBufSize += StubSize;
378 }
379 }
Alexey Samsonovaa4d2952014-03-14 14:22:49 +0000380
Lang Hames937ec542014-02-12 21:30:07 +0000381 // Get section data size and alignment
382 uint64_t Alignment64;
383 uint64_t DataSize;
384 Check(Section.getSize(DataSize));
385 Check(Section.getAlignment(Alignment64));
386
387 // Add stubbuf size alignment
388 unsigned Alignment = (unsigned)Alignment64 & 0xffffffffL;
389 unsigned StubAlignment = getStubAlignment();
390 unsigned EndAlignment = (DataSize | Alignment) & -(DataSize | Alignment);
391 if (StubAlignment > EndAlignment)
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000392 StubBufSize += StubAlignment - EndAlignment;
Lang Hames937ec542014-02-12 21:30:07 +0000393 return StubBufSize;
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000394}
395
Eli Bendersky667b8792012-05-01 10:41:12 +0000396void RuntimeDyldImpl::emitCommonSymbols(ObjectImage &Obj,
397 const CommonSymbolMap &CommonSymbols,
398 uint64_t TotalSize,
399 SymbolTableMap &SymbolTable) {
Preston Gurd2138ef62012-04-12 20:13:57 +0000400 // Allocate memory for the section
401 unsigned SectionID = Sections.size();
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000402 uint8_t *Addr = MemMgr->allocateDataSection(TotalSize, sizeof(void *),
403 SectionID, StringRef(), false);
Preston Gurd2138ef62012-04-12 20:13:57 +0000404 if (!Addr)
405 report_fatal_error("Unable to allocate memory for common symbols!");
406 uint64_t Offset = 0;
Lang Hames86b08f02014-08-25 18:37:38 +0000407 Sections.push_back(SectionEntry("<common symbols>", Addr, TotalSize, 0));
Preston Gurd2138ef62012-04-12 20:13:57 +0000408 memset(Addr, 0, TotalSize);
409
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000410 DEBUG(dbgs() << "emitCommonSection SectionID: " << SectionID << " new addr: "
411 << format("%p", Addr) << " DataSize: " << TotalSize << "\n");
Preston Gurd2138ef62012-04-12 20:13:57 +0000412
413 // Assign the address of each symbol
Jim Grosbach36f025e2014-04-21 19:23:59 +0000414 for (CommonSymbolMap::const_iterator it = CommonSymbols.begin(),
415 itEnd = CommonSymbols.end(); it != itEnd; ++it) {
416 uint64_t Size = it->second.first;
417 uint64_t Align = it->second.second;
Preston Gurd2138ef62012-04-12 20:13:57 +0000418 StringRef Name;
Jim Grosbach36f025e2014-04-21 19:23:59 +0000419 it->first.getName(Name);
Tim Northover94bc73d2012-10-29 10:47:04 +0000420 if (Align) {
421 // This symbol has an alignment requirement.
422 uint64_t AlignOffset = OffsetToAlignment((uint64_t)Addr, Align);
423 Addr += AlignOffset;
424 Offset += AlignOffset;
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000425 DEBUG(dbgs() << "Allocating common symbol " << Name << " address "
426 << format("%p\n", Addr));
Tim Northover94bc73d2012-10-29 10:47:04 +0000427 }
Jim Grosbach36f025e2014-04-21 19:23:59 +0000428 Obj.updateSymbolAddress(it->first, (uint64_t)Addr);
Eli Bendersky667b8792012-05-01 10:41:12 +0000429 SymbolTable[Name.data()] = SymbolLoc(SectionID, Offset);
Preston Gurd2138ef62012-04-12 20:13:57 +0000430 Offset += Size;
431 Addr += Size;
432 }
Preston Gurd2138ef62012-04-12 20:13:57 +0000433}
434
Preston Gurdcc31af92012-04-16 22:12:58 +0000435unsigned RuntimeDyldImpl::emitSection(ObjectImage &Obj,
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000436 const SectionRef &Section, bool IsCode) {
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000437
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000438 StringRef data;
439 uint64_t Alignment64;
440 Check(Section.getContents(data));
441 Check(Section.getAlignment(Alignment64));
442
443 unsigned Alignment = (unsigned)Alignment64 & 0xffffffffL;
Preston Gurd2138ef62012-04-12 20:13:57 +0000444 bool IsRequired;
445 bool IsVirtual;
446 bool IsZeroInit;
Andrew Kaylora342cb92012-11-15 23:50:01 +0000447 bool IsReadOnly;
Preston Gurd2138ef62012-04-12 20:13:57 +0000448 uint64_t DataSize;
Andrew Kaylor877b9312013-10-16 00:32:24 +0000449 unsigned PaddingSize = 0;
Lang Hames937ec542014-02-12 21:30:07 +0000450 unsigned StubBufSize = 0;
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000451 StringRef Name;
Preston Gurd2138ef62012-04-12 20:13:57 +0000452 Check(Section.isRequiredForExecution(IsRequired));
453 Check(Section.isVirtual(IsVirtual));
454 Check(Section.isZeroInit(IsZeroInit));
Andrew Kaylora342cb92012-11-15 23:50:01 +0000455 Check(Section.isReadOnlyData(IsReadOnly));
Preston Gurd2138ef62012-04-12 20:13:57 +0000456 Check(Section.getSize(DataSize));
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000457 Check(Section.getName(Name));
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000458
459 StubBufSize = computeSectionStubBufSize(Obj, Section);
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000460
Andrew Kaylor877b9312013-10-16 00:32:24 +0000461 // The .eh_frame section (at least on Linux) needs an extra four bytes padded
462 // with zeroes added at the end. For MachO objects, this section has a
463 // slightly different name, so this won't have any effect for MachO objects.
464 if (Name == ".eh_frame")
465 PaddingSize = 4;
466
Lang Hamesd4100172014-02-11 05:28:24 +0000467 uintptr_t Allocate;
Preston Gurd2138ef62012-04-12 20:13:57 +0000468 unsigned SectionID = Sections.size();
469 uint8_t *Addr;
Craig Topper353eda42014-04-24 06:44:33 +0000470 const char *pData = nullptr;
Preston Gurd2138ef62012-04-12 20:13:57 +0000471
472 // Some sections, such as debug info, don't need to be loaded for execution.
473 // Leave those where they are.
474 if (IsRequired) {
Andrew Kaylor877b9312013-10-16 00:32:24 +0000475 Allocate = DataSize + PaddingSize + StubBufSize;
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000476 Addr = IsCode ? MemMgr->allocateCodeSection(Allocate, Alignment, SectionID,
477 Name)
478 : MemMgr->allocateDataSection(Allocate, Alignment, SectionID,
479 Name, IsReadOnly);
Preston Gurd2138ef62012-04-12 20:13:57 +0000480 if (!Addr)
481 report_fatal_error("Unable to allocate section memory!");
482
483 // Virtual sections have no data in the object image, so leave pData = 0
484 if (!IsVirtual)
485 pData = data.data();
486
487 // Zero-initialize or copy the data from the image
488 if (IsZeroInit || IsVirtual)
489 memset(Addr, 0, DataSize);
490 else
491 memcpy(Addr, pData, DataSize);
492
Andrew Kaylor877b9312013-10-16 00:32:24 +0000493 // Fill in any extra bytes we allocated for padding
494 if (PaddingSize != 0) {
495 memset(Addr + DataSize, 0, PaddingSize);
496 // Update the DataSize variable so that the stub offset is set correctly.
497 DataSize += PaddingSize;
498 }
499
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000500 DEBUG(dbgs() << "emitSection SectionID: " << SectionID << " Name: " << Name
Preston Gurd2138ef62012-04-12 20:13:57 +0000501 << " obj addr: " << format("%p", pData)
502 << " new addr: " << format("%p", Addr)
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000503 << " DataSize: " << DataSize << " StubBufSize: " << StubBufSize
504 << " Allocate: " << Allocate << "\n");
Preston Gurdcc31af92012-04-16 22:12:58 +0000505 Obj.updateSectionAddress(Section, (uint64_t)Addr);
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000506 } else {
Preston Gurd2138ef62012-04-12 20:13:57 +0000507 // Even if we didn't load the section, we need to record an entry for it
Eli Bendersky0e2ac5b2012-04-29 12:40:47 +0000508 // to handle later processing (and by 'handle' I mean don't do anything
509 // with these sections).
Preston Gurd2138ef62012-04-12 20:13:57 +0000510 Allocate = 0;
Craig Topper353eda42014-04-24 06:44:33 +0000511 Addr = nullptr;
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000512 DEBUG(dbgs() << "emitSection SectionID: " << SectionID << " Name: " << Name
513 << " obj addr: " << format("%p", data.data()) << " new addr: 0"
514 << " DataSize: " << DataSize << " StubBufSize: " << StubBufSize
515 << " Allocate: " << Allocate << "\n");
Preston Gurd2138ef62012-04-12 20:13:57 +0000516 }
517
Rafael Espindolafa5942b2013-05-05 20:43:10 +0000518 Sections.push_back(SectionEntry(Name, Addr, DataSize, (uintptr_t)pData));
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000519 return SectionID;
520}
521
Preston Gurdcc31af92012-04-16 22:12:58 +0000522unsigned RuntimeDyldImpl::findOrEmitSection(ObjectImage &Obj,
523 const SectionRef &Section,
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000524 bool IsCode,
525 ObjSectionToIDMap &LocalSections) {
526
527 unsigned SectionID = 0;
528 ObjSectionToIDMap::iterator i = LocalSections.find(Section);
529 if (i != LocalSections.end())
530 SectionID = i->second;
531 else {
Preston Gurdcc31af92012-04-16 22:12:58 +0000532 SectionID = emitSection(Obj, Section, IsCode);
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000533 LocalSections[Section] = SectionID;
534 }
535 return SectionID;
536}
537
Eli Bendersky667b8792012-05-01 10:41:12 +0000538void RuntimeDyldImpl::addRelocationForSection(const RelocationEntry &RE,
539 unsigned SectionID) {
540 Relocations[SectionID].push_back(RE);
541}
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000542
Eli Bendersky667b8792012-05-01 10:41:12 +0000543void RuntimeDyldImpl::addRelocationForSymbol(const RelocationEntry &RE,
544 StringRef SymbolName) {
545 // Relocation by symbol. If the symbol is found in the global symbol table,
546 // create an appropriate section relocation. Otherwise, add it to
547 // ExternalSymbolRelocations.
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000548 SymbolTableMap::const_iterator Loc = GlobalSymbolTable.find(SymbolName);
Eli Bendersky667b8792012-05-01 10:41:12 +0000549 if (Loc == GlobalSymbolTable.end()) {
550 ExternalSymbolRelocations[SymbolName].push_back(RE);
Eli Benderskyb92e1cf2012-04-30 12:15:58 +0000551 } else {
Eli Bendersky667b8792012-05-01 10:41:12 +0000552 // Copy the RE since we want to modify its addend.
553 RelocationEntry RECopy = RE;
554 RECopy.Addend += Loc->second.second;
555 Relocations[Loc->second.first].push_back(RECopy);
Eli Benderskyb92e1cf2012-04-30 12:15:58 +0000556 }
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000557}
558
Ulrich Weigand752b5c92014-07-20 23:53:14 +0000559uint8_t *RuntimeDyldImpl::createStubFunction(uint8_t *Addr,
560 unsigned AbiVariant) {
Tim Northovere19bed72014-07-23 12:32:47 +0000561 if (Arch == Triple::aarch64 || Arch == Triple::aarch64_be) {
Tim Northover37cde972013-05-04 20:14:09 +0000562 // This stub has to be able to access the full address space,
563 // since symbol lookup won't necessarily find a handy, in-range,
564 // PLT stub for functions which could be anywhere.
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000565 uint32_t *StubAddr = (uint32_t *)Addr;
Tim Northover37cde972013-05-04 20:14:09 +0000566
567 // Stub can use ip0 (== x16) to calculate address
568 *StubAddr = 0xd2e00010; // movz ip0, #:abs_g3:<addr>
569 StubAddr++;
570 *StubAddr = 0xf2c00010; // movk ip0, #:abs_g2_nc:<addr>
571 StubAddr++;
572 *StubAddr = 0xf2a00010; // movk ip0, #:abs_g1_nc:<addr>
573 StubAddr++;
574 *StubAddr = 0xf2800010; // movk ip0, #:abs_g0_nc:<addr>
575 StubAddr++;
576 *StubAddr = 0xd61f0200; // br ip0
577
578 return Addr;
Christian Pirker2a111602014-03-28 14:35:30 +0000579 } else if (Arch == Triple::arm || Arch == Triple::armeb) {
Akira Hatanaka111174b2012-08-17 21:28:04 +0000580 // TODO: There is only ARM far stub now. We should add the Thumb stub,
581 // and stubs for branches Thumb - ARM and ARM - Thumb.
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000582 uint32_t *StubAddr = (uint32_t *)Addr;
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000583 *StubAddr = 0xe51ff004; // ldr pc,<label>
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000584 return (uint8_t *)++StubAddr;
NAKAMURA Takumif97efd92012-12-04 00:08:14 +0000585 } else if (Arch == Triple::mipsel || Arch == Triple::mips) {
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000586 uint32_t *StubAddr = (uint32_t *)Addr;
Akira Hatanaka111174b2012-08-17 21:28:04 +0000587 // 0: 3c190000 lui t9,%hi(addr).
588 // 4: 27390000 addiu t9,t9,%lo(addr).
589 // 8: 03200008 jr t9.
590 // c: 00000000 nop.
591 const unsigned LuiT9Instr = 0x3c190000, AdduiT9Instr = 0x27390000;
592 const unsigned JrT9Instr = 0x03200008, NopInstr = 0x0;
593
594 *StubAddr = LuiT9Instr;
595 StubAddr++;
596 *StubAddr = AdduiT9Instr;
597 StubAddr++;
598 *StubAddr = JrT9Instr;
599 StubAddr++;
600 *StubAddr = NopInstr;
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000601 return Addr;
Bill Schmidt0a9170d2013-07-26 01:35:43 +0000602 } else if (Arch == Triple::ppc64 || Arch == Triple::ppc64le) {
Ulrich Weigand752b5c92014-07-20 23:53:14 +0000603 // Depending on which version of the ELF ABI is in use, we need to
604 // generate one of two variants of the stub. They both start with
605 // the same sequence to load the target address into r12.
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000606 writeInt32BE(Addr, 0x3D800000); // lis r12, highest(addr)
607 writeInt32BE(Addr+4, 0x618C0000); // ori r12, higher(addr)
608 writeInt32BE(Addr+8, 0x798C07C6); // sldi r12, r12, 32
609 writeInt32BE(Addr+12, 0x658C0000); // oris r12, r12, h(addr)
610 writeInt32BE(Addr+16, 0x618C0000); // ori r12, r12, l(addr)
Ulrich Weigand752b5c92014-07-20 23:53:14 +0000611 if (AbiVariant == 2) {
612 // PowerPC64 stub ELFv2 ABI: The address points to the function itself.
613 // The address is already in r12 as required by the ABI. Branch to it.
614 writeInt32BE(Addr+20, 0xF8410018); // std r2, 24(r1)
615 writeInt32BE(Addr+24, 0x7D8903A6); // mtctr r12
616 writeInt32BE(Addr+28, 0x4E800420); // bctr
617 } else {
618 // PowerPC64 stub ELFv1 ABI: The address points to a function descriptor.
619 // Load the function address on r11 and sets it to control register. Also
620 // loads the function TOC in r2 and environment pointer to r11.
621 writeInt32BE(Addr+20, 0xF8410028); // std r2, 40(r1)
622 writeInt32BE(Addr+24, 0xE96C0000); // ld r11, 0(r12)
623 writeInt32BE(Addr+28, 0xE84C0008); // ld r2, 0(r12)
624 writeInt32BE(Addr+32, 0x7D6903A6); // mtctr r11
625 writeInt32BE(Addr+36, 0xE96C0010); // ld r11, 16(r2)
626 writeInt32BE(Addr+40, 0x4E800420); // bctr
627 }
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000628 return Addr;
Richard Sandifordca044082013-05-03 14:15:35 +0000629 } else if (Arch == Triple::systemz) {
630 writeInt16BE(Addr, 0xC418); // lgrl %r1,.+8
631 writeInt16BE(Addr+2, 0x0000);
632 writeInt16BE(Addr+4, 0x0004);
633 writeInt16BE(Addr+6, 0x07F1); // brc 15,%r1
634 // 8-byte address stored at Addr + 8
635 return Addr;
Andrew Kaylor4612fed2013-08-19 23:27:43 +0000636 } else if (Arch == Triple::x86_64) {
637 *Addr = 0xFF; // jmp
638 *(Addr+1) = 0x25; // rip
639 // 32-bit PC-relative address of the GOT entry will be stored at Addr+2
Lang Hames36072da2014-05-12 21:39:59 +0000640 } else if (Arch == Triple::x86) {
641 *Addr = 0xE9; // 32-bit pc-relative jump.
Akira Hatanaka111174b2012-08-17 21:28:04 +0000642 }
643 return Addr;
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000644}
645
646// Assign an address to a symbol name and resolve all the relocations
647// associated with it.
648void RuntimeDyldImpl::reassignSectionAddress(unsigned SectionID,
649 uint64_t Addr) {
650 // The address to use for relocation resolution is not
651 // the address of the local section buffer. We must be doing
Andrew Kaylora714efc2012-11-05 20:57:16 +0000652 // a remote execution environment of some sort. Relocations can't
653 // be applied until all the sections have been moved. The client must
654 // trigger this with a call to MCJIT::finalize() or
655 // RuntimeDyld::resolveRelocations().
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000656 //
657 // Addr is a uint64_t because we can't assume the pointer width
658 // of the target is the same as that of the host. Just use a generic
659 // "big enough" type.
660 Sections[SectionID].LoadAddress = Addr;
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000661}
662
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000663void RuntimeDyldImpl::resolveRelocationList(const RelocationList &Relocs,
664 uint64_t Value) {
665 for (unsigned i = 0, e = Relocs.size(); i != e; ++i) {
Rafael Espindolaf1f1c622013-04-29 17:24:34 +0000666 const RelocationEntry &RE = Relocs[i];
667 // Ignore relocations for sections that were not loaded
Craig Topper353eda42014-04-24 06:44:33 +0000668 if (Sections[RE.SectionID].Address == nullptr)
Rafael Espindolaf1f1c622013-04-29 17:24:34 +0000669 continue;
670 resolveRelocation(RE, Value);
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000671 }
672}
673
Eli Benderskyb92e1cf2012-04-30 12:15:58 +0000674void RuntimeDyldImpl::resolveExternalSymbols() {
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000675 while (!ExternalSymbolRelocations.empty()) {
Andrew Kaylorea395922013-10-01 01:47:35 +0000676 StringMap<RelocationList>::iterator i = ExternalSymbolRelocations.begin();
677
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000678 StringRef Name = i->first();
Andrew Kaylorea395922013-10-01 01:47:35 +0000679 if (Name.size() == 0) {
680 // This is an absolute symbol, use an address of zero.
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000681 DEBUG(dbgs() << "Resolving absolute relocations."
682 << "\n");
Andrew Kaylorcfb4a992013-11-11 19:55:10 +0000683 RelocationList &Relocs = i->second;
Andrew Kaylorea395922013-10-01 01:47:35 +0000684 resolveRelocationList(Relocs, 0);
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000685 } else {
Andrew Kaylorea395922013-10-01 01:47:35 +0000686 uint64_t Addr = 0;
687 SymbolTableMap::const_iterator Loc = GlobalSymbolTable.find(Name);
688 if (Loc == GlobalSymbolTable.end()) {
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000689 // This is an external symbol, try to get its address from
690 // MemoryManager.
691 Addr = MemMgr->getSymbolAddress(Name.data());
692 // The call to getSymbolAddress may have caused additional modules to
693 // be loaded, which may have added new entries to the
694 // ExternalSymbolRelocations map. Consquently, we need to update our
695 // iterator. This is also why retrieval of the relocation list
696 // associated with this symbol is deferred until below this point.
697 // New entries may have been added to the relocation list.
698 i = ExternalSymbolRelocations.find(Name);
Andrew Kaylorea395922013-10-01 01:47:35 +0000699 } else {
700 // We found the symbol in our global table. It was probably in a
701 // Module that we loaded previously.
Yaron Kerenc9802882013-10-19 09:04:26 +0000702 SymbolLoc SymLoc = Loc->second;
Andrew Kaylorea395922013-10-01 01:47:35 +0000703 Addr = getSectionLoadAddress(SymLoc.first) + SymLoc.second;
704 }
705
706 // FIXME: Implement error handling that doesn't kill the host program!
707 if (!Addr)
708 report_fatal_error("Program used external function '" + Name +
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000709 "' which could not be resolved!");
Andrew Kaylorea395922013-10-01 01:47:35 +0000710
711 updateGOTEntries(Name, Addr);
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000712 DEBUG(dbgs() << "Resolving relocations Name: " << Name << "\t"
713 << format("0x%lx", Addr) << "\n");
Andrew Kaylorcfb4a992013-11-11 19:55:10 +0000714 // This list may have been updated when we called getSymbolAddress, so
715 // don't change this code to get the list earlier.
716 RelocationList &Relocs = i->second;
Andrew Kaylorea395922013-10-01 01:47:35 +0000717 resolveRelocationList(Relocs, Addr);
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000718 }
Andrew Kaylorea395922013-10-01 01:47:35 +0000719
Andrew Kaylorcfb4a992013-11-11 19:55:10 +0000720 ExternalSymbolRelocations.erase(i);
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000721 }
722}
723
Jim Grosbachf016b0a2011-03-21 22:15:52 +0000724//===----------------------------------------------------------------------===//
725// RuntimeDyld class implementation
Danil Malyshev72510f22011-07-13 07:57:58 +0000726RuntimeDyld::RuntimeDyld(RTDyldMemoryManager *mm) {
Andrew Kaylora342cb92012-11-15 23:50:01 +0000727 // FIXME: There's a potential issue lurking here if a single instance of
728 // RuntimeDyld is used to load multiple objects. The current implementation
729 // associates a single memory manager with a RuntimeDyld instance. Even
730 // though the public class spawns a new 'impl' instance for each load,
731 // they share a single memory manager. This can become a problem when page
732 // permissions are applied.
Craig Topper353eda42014-04-24 06:44:33 +0000733 Dyld = nullptr;
Danil Malyshev72510f22011-07-13 07:57:58 +0000734 MM = mm;
Lang Hames868d4b32014-03-20 21:06:46 +0000735 ProcessAllSections = false;
Lang Hamesf7acddd2014-07-22 22:47:39 +0000736 Checker = nullptr;
Jim Grosbachf016b0a2011-03-21 22:15:52 +0000737}
738
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000739RuntimeDyld::~RuntimeDyld() { delete Dyld; }
Jim Grosbachf016b0a2011-03-21 22:15:52 +0000740
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000741static std::unique_ptr<RuntimeDyldELF>
Lang Hamesf7acddd2014-07-22 22:47:39 +0000742createRuntimeDyldELF(RTDyldMemoryManager *MM, bool ProcessAllSections,
743 RuntimeDyldCheckerImpl *Checker) {
Lang Hames868d4b32014-03-20 21:06:46 +0000744 std::unique_ptr<RuntimeDyldELF> Dyld(new RuntimeDyldELF(MM));
745 Dyld->setProcessAllSections(ProcessAllSections);
Lang Hamesf7acddd2014-07-22 22:47:39 +0000746 Dyld->setRuntimeDyldChecker(Checker);
Lang Hames868d4b32014-03-20 21:06:46 +0000747 return Dyld;
748}
749
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000750static std::unique_ptr<RuntimeDyldMachO>
Lang Hamesa5216882014-07-17 18:54:50 +0000751createRuntimeDyldMachO(Triple::ArchType Arch, RTDyldMemoryManager *MM,
Lang Hamesf7acddd2014-07-22 22:47:39 +0000752 bool ProcessAllSections, RuntimeDyldCheckerImpl *Checker) {
Lang Hamesa5216882014-07-17 18:54:50 +0000753 std::unique_ptr<RuntimeDyldMachO> Dyld(RuntimeDyldMachO::create(Arch, MM));
Lang Hames868d4b32014-03-20 21:06:46 +0000754 Dyld->setProcessAllSections(ProcessAllSections);
Lang Hamesf7acddd2014-07-22 22:47:39 +0000755 Dyld->setRuntimeDyldChecker(Checker);
Lang Hames868d4b32014-03-20 21:06:46 +0000756 return Dyld;
757}
758
David Blaikie7a1e7752014-04-29 21:52:46 +0000759ObjectImage *RuntimeDyld::loadObject(std::unique_ptr<ObjectFile> InputObject) {
Lang Hames951b2352014-03-08 18:45:12 +0000760 std::unique_ptr<ObjectImage> InputImage;
Lang Hames173c69f2014-01-08 04:09:09 +0000761
David Blaikie7a1e7752014-04-29 21:52:46 +0000762 ObjectFile &Obj = *InputObject;
763
Lang Hames951b2352014-03-08 18:45:12 +0000764 if (InputObject->isELF()) {
David Blaikie7a1e7752014-04-29 21:52:46 +0000765 InputImage.reset(RuntimeDyldELF::createObjectImageFromFile(std::move(InputObject)));
Lang Hames951b2352014-03-08 18:45:12 +0000766 if (!Dyld)
Lang Hamesf7acddd2014-07-22 22:47:39 +0000767 Dyld = createRuntimeDyldELF(MM, ProcessAllSections, Checker).release();
Lang Hames951b2352014-03-08 18:45:12 +0000768 } else if (InputObject->isMachO()) {
David Blaikie7a1e7752014-04-29 21:52:46 +0000769 InputImage.reset(RuntimeDyldMachO::createObjectImageFromFile(std::move(InputObject)));
Lang Hames951b2352014-03-08 18:45:12 +0000770 if (!Dyld)
Lang Hamesa5216882014-07-17 18:54:50 +0000771 Dyld = createRuntimeDyldMachO(
772 static_cast<Triple::ArchType>(InputImage->getArch()),
Lang Hamesf7acddd2014-07-22 22:47:39 +0000773 MM, ProcessAllSections, Checker).release();
Lang Hames951b2352014-03-08 18:45:12 +0000774 } else
775 report_fatal_error("Incompatible object format!");
776
David Blaikie7a1e7752014-04-29 21:52:46 +0000777 if (!Dyld->isCompatibleFile(&Obj))
Lang Hames951b2352014-03-08 18:45:12 +0000778 report_fatal_error("Incompatible object format!");
779
780 Dyld->loadObject(InputImage.get());
781 return InputImage.release();
Lang Hames173c69f2014-01-08 04:09:09 +0000782}
783
Andrew Kayloradc70562012-10-02 21:18:39 +0000784ObjectImage *RuntimeDyld::loadObject(ObjectBuffer *InputBuffer) {
Lang Hames951b2352014-03-08 18:45:12 +0000785 std::unique_ptr<ObjectImage> InputImage;
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000786 sys::fs::file_magic Type = sys::fs::identify_magic(InputBuffer->getBuffer());
Lang Hames951b2352014-03-08 18:45:12 +0000787
788 switch (Type) {
789 case sys::fs::file_magic::elf_relocatable:
790 case sys::fs::file_magic::elf_executable:
791 case sys::fs::file_magic::elf_shared_object:
792 case sys::fs::file_magic::elf_core:
793 InputImage.reset(RuntimeDyldELF::createObjectImage(InputBuffer));
794 if (!Dyld)
Lang Hamesf7acddd2014-07-22 22:47:39 +0000795 Dyld = createRuntimeDyldELF(MM, ProcessAllSections, Checker).release();
Lang Hames951b2352014-03-08 18:45:12 +0000796 break;
797 case sys::fs::file_magic::macho_object:
798 case sys::fs::file_magic::macho_executable:
799 case sys::fs::file_magic::macho_fixed_virtual_memory_shared_lib:
800 case sys::fs::file_magic::macho_core:
801 case sys::fs::file_magic::macho_preload_executable:
802 case sys::fs::file_magic::macho_dynamically_linked_shared_lib:
803 case sys::fs::file_magic::macho_dynamic_linker:
804 case sys::fs::file_magic::macho_bundle:
805 case sys::fs::file_magic::macho_dynamically_linked_shared_lib_stub:
806 case sys::fs::file_magic::macho_dsym_companion:
807 InputImage.reset(RuntimeDyldMachO::createObjectImage(InputBuffer));
808 if (!Dyld)
Lang Hamesa5216882014-07-17 18:54:50 +0000809 Dyld = createRuntimeDyldMachO(
810 static_cast<Triple::ArchType>(InputImage->getArch()),
Lang Hamesf7acddd2014-07-22 22:47:39 +0000811 MM, ProcessAllSections, Checker).release();
Lang Hames951b2352014-03-08 18:45:12 +0000812 break;
813 case sys::fs::file_magic::unknown:
814 case sys::fs::file_magic::bitcode:
815 case sys::fs::file_magic::archive:
816 case sys::fs::file_magic::coff_object:
817 case sys::fs::file_magic::coff_import_library:
818 case sys::fs::file_magic::pecoff_executable:
819 case sys::fs::file_magic::macho_universal_binary:
820 case sys::fs::file_magic::windows_resource:
821 report_fatal_error("Incompatible object format!");
Danil Malyshev72510f22011-07-13 07:57:58 +0000822 }
823
Lang Hames951b2352014-03-08 18:45:12 +0000824 if (!Dyld->isCompatibleFormat(InputBuffer))
825 report_fatal_error("Incompatible object format!");
826
827 Dyld->loadObject(InputImage.get());
828 return InputImage.release();
Jim Grosbachf016b0a2011-03-21 22:15:52 +0000829}
830
Jim Grosbach18b81c52011-04-08 17:31:24 +0000831void *RuntimeDyld::getSymbolAddress(StringRef Name) {
Andrew Kaylorea395922013-10-01 01:47:35 +0000832 if (!Dyld)
Craig Topper353eda42014-04-24 06:44:33 +0000833 return nullptr;
Jim Grosbachf016b0a2011-03-21 22:15:52 +0000834 return Dyld->getSymbolAddress(Name);
835}
836
Jim Grosbachdc1123f2012-09-05 16:50:40 +0000837uint64_t RuntimeDyld::getSymbolLoadAddress(StringRef Name) {
Andrew Kaylorea395922013-10-01 01:47:35 +0000838 if (!Dyld)
839 return 0;
Jim Grosbachdc1123f2012-09-05 16:50:40 +0000840 return Dyld->getSymbolLoadAddress(Name);
841}
842
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000843void RuntimeDyld::resolveRelocations() { Dyld->resolveRelocations(); }
Jim Grosbach733d3052011-04-12 21:20:41 +0000844
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000845void RuntimeDyld::reassignSectionAddress(unsigned SectionID, uint64_t Addr) {
Jim Grosbacheff0a402012-01-16 22:26:39 +0000846 Dyld->reassignSectionAddress(SectionID, Addr);
Jim Grosbach733d3052011-04-12 21:20:41 +0000847}
848
Jim Grosbach6d613972012-09-13 21:50:06 +0000849void RuntimeDyld::mapSectionAddress(const void *LocalAddress,
Jim Grosbach0ddb3a42012-01-16 23:50:55 +0000850 uint64_t TargetAddress) {
851 Dyld->mapSectionAddress(LocalAddress, TargetAddress);
852}
853
Juergen Ributzka6ff29a72014-03-26 18:19:27 +0000854bool RuntimeDyld::hasError() { return Dyld->hasError(); }
855
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000856StringRef RuntimeDyld::getErrorString() { return Dyld->getErrorString(); }
Jim Grosbach40411cc2011-03-22 18:19:42 +0000857
Andrew Kaylor7bb13442013-10-11 21:25:48 +0000858void RuntimeDyld::registerEHFrames() {
Andrew Kaylorc442a762013-10-16 00:14:21 +0000859 if (Dyld)
860 Dyld->registerEHFrames();
861}
862
863void RuntimeDyld::deregisterEHFrames() {
864 if (Dyld)
865 Dyld->deregisterEHFrames();
Rafael Espindolafa5942b2013-05-05 20:43:10 +0000866}
867
Jim Grosbachf016b0a2011-03-21 22:15:52 +0000868} // end namespace llvm