blob: a14a6bbd1e51d00b62f1c6e4da14001a5147c788 [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"
Lang Hamesf7acddd2014-07-22 22:47:39 +000015#include "RuntimeDyldCheckerImpl.h"
David Majnemer1a666e02015-03-07 20:21:27 +000016#include "RuntimeDyldCOFF.h"
Eli Bendersky058d6472012-01-22 07:05:02 +000017#include "RuntimeDyldELF.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000018#include "RuntimeDyldImpl.h"
Eli Bendersky058d6472012-01-22 07:05:02 +000019#include "RuntimeDyldMachO.h"
Rafael Espindola0e77a942014-12-10 20:46:55 +000020#include "llvm/Object/ELFObjectFile.h"
David Majnemer1a666e02015-03-07 20:21:27 +000021#include "llvm/Object/COFF.h"
Lang Hames89595312016-04-27 20:24:48 +000022#include "llvm/Support/ManagedStatic.h"
Tim Northover94bc73d2012-10-29 10:47:04 +000023#include "llvm/Support/MathExtras.h"
Andrew Kaylor4fba0492013-10-21 17:42:06 +000024#include "llvm/Support/MutexGuard.h"
Eli Bendersky058d6472012-01-22 07:05:02 +000025
Jim Grosbachf016b0a2011-03-21 22:15:52 +000026using namespace llvm;
27using namespace llvm::object;
28
Chandler Carruthf58e3762014-04-22 03:04:17 +000029#define DEBUG_TYPE "dyld"
30
Lang Hames89595312016-04-27 20:24:48 +000031namespace {
32
33enum RuntimeDyldErrorCode {
34 GenericRTDyldError = 1
35};
36
37class RuntimeDyldErrorCategory : public std::error_category {
38public:
39 const char *name() const LLVM_NOEXCEPT override { return "runtimedyld"; }
40
41 std::string message(int Condition) const override {
42 switch (static_cast<RuntimeDyldErrorCode>(Condition)) {
43 case GenericRTDyldError: return "Generic RuntimeDyld error";
44 }
45 llvm_unreachable("Unrecognized RuntimeDyldErrorCode");
46 }
47};
48
49static ManagedStatic<RuntimeDyldErrorCategory> RTDyldErrorCategory;
50
51}
52
53char RuntimeDyldError::ID = 0;
54
55void RuntimeDyldError::log(raw_ostream &OS) const {
56 OS << ErrMsg << "\n";
57}
58
59std::error_code RuntimeDyldError::convertToErrorCode() const {
60 return std::error_code(GenericRTDyldError, *RTDyldErrorCategory);
61}
62
Chandler Carruth086f7082011-04-05 23:54:31 +000063// Empty out-of-line virtual destructor as the key function.
Danil Malyshev72510f22011-07-13 07:57:58 +000064RuntimeDyldImpl::~RuntimeDyldImpl() {}
Chandler Carruth086f7082011-04-05 23:54:31 +000065
Lang Hamesb5c7b1f2014-11-26 16:54:40 +000066// Pin LoadedObjectInfo's vtables to this file.
67void RuntimeDyld::LoadedObjectInfo::anchor() {}
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +000068
Jim Grosbachf016b0a2011-03-21 22:15:52 +000069namespace llvm {
Jim Grosbachf016b0a2011-03-21 22:15:52 +000070
Juergen Ributzka7608dc02014-03-21 20:28:42 +000071void RuntimeDyldImpl::registerEHFrames() {}
Rafael Espindolafa5942b2013-05-05 20:43:10 +000072
Juergen Ributzka7608dc02014-03-21 20:28:42 +000073void RuntimeDyldImpl::deregisterEHFrames() {}
Andrew Kaylorc442a762013-10-16 00:14:21 +000074
Benjamin Kramer01e17892014-08-26 14:22:05 +000075#ifndef NDEBUG
Lang Hamesf4b3b672014-08-25 22:19:14 +000076static void dumpSectionMemory(const SectionEntry &S, StringRef State) {
Sanjoy Das277776a2015-11-23 21:47:41 +000077 dbgs() << "----- Contents of section " << S.getName() << " " << State
78 << " -----";
Lang Hamesf4b3b672014-08-25 22:19:14 +000079
Sanjoy Das277776a2015-11-23 21:47:41 +000080 if (S.getAddress() == nullptr) {
Lang Hames24f0c242014-10-01 21:57:47 +000081 dbgs() << "\n <section not emitted>\n";
82 return;
83 }
84
Lang Hamesf4b3b672014-08-25 22:19:14 +000085 const unsigned ColsPerRow = 16;
Lang Hames86b08f02014-08-25 18:37:38 +000086
Sanjoy Das277776a2015-11-23 21:47:41 +000087 uint8_t *DataAddr = S.getAddress();
88 uint64_t LoadAddr = S.getLoadAddress();
Lang Hames86b08f02014-08-25 18:37:38 +000089
Lang Hames8d4d0262014-09-18 16:43:24 +000090 unsigned StartPadding = LoadAddr & (ColsPerRow - 1);
Sanjoy Das277776a2015-11-23 21:47:41 +000091 unsigned BytesRemaining = S.getSize();
Lang Hames86b08f02014-08-25 18:37:38 +000092
93 if (StartPadding) {
Alexei Starovoitov36df1ca2015-03-30 05:15:57 +000094 dbgs() << "\n" << format("0x%016" PRIx64,
95 LoadAddr & ~(uint64_t)(ColsPerRow - 1)) << ":";
Lang Hames86b08f02014-08-25 18:37:38 +000096 while (StartPadding--)
97 dbgs() << " ";
98 }
99
100 while (BytesRemaining > 0) {
Lang Hamesf4b3b672014-08-25 22:19:14 +0000101 if ((LoadAddr & (ColsPerRow - 1)) == 0)
Lang Hamesc5cafbb2014-08-28 04:25:17 +0000102 dbgs() << "\n" << format("0x%016" PRIx64, LoadAddr) << ":";
Lang Hames86b08f02014-08-25 18:37:38 +0000103
104 dbgs() << " " << format("%02x", *DataAddr);
105
106 ++DataAddr;
107 ++LoadAddr;
108 --BytesRemaining;
109 }
110
111 dbgs() << "\n";
112}
Benjamin Kramer01e17892014-08-26 14:22:05 +0000113#endif
Lang Hames86b08f02014-08-25 18:37:38 +0000114
Jim Grosbach733d3052011-04-12 21:20:41 +0000115// Resolve the relocations for all symbols we currently know about.
116void RuntimeDyldImpl::resolveRelocations() {
Andrew Kaylor4fba0492013-10-21 17:42:06 +0000117 MutexGuard locked(lock);
118
Lang Hames79fce472015-09-10 20:44:36 +0000119 // Print out the sections prior to relocation.
120 DEBUG(
121 for (int i = 0, e = Sections.size(); i != e; ++i)
122 dumpSectionMemory(Sections[i], "before relocations");
123 );
124
Preston Gurd2138ef62012-04-12 20:13:57 +0000125 // First, resolve relocations associated with external symbols.
Eli Benderskyb92e1cf2012-04-30 12:15:58 +0000126 resolveExternalSymbols();
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000127
Keno Fischer2cd66e92015-10-10 05:37:02 +0000128 // Iterate over all outstanding relocations
129 for (auto it = Relocations.begin(), e = Relocations.end(); it != e; ++it) {
Andrew Kaylor5f3a9982013-08-19 19:38:06 +0000130 // The Section here (Sections[i]) refers to the section in which the
131 // symbol for the relocation is located. The SectionID in the relocation
132 // entry provides the section to which the relocation will be applied.
Keno Fischereb59d462015-12-03 21:27:59 +0000133 int Idx = it->first;
Sanjoy Das277776a2015-11-23 21:47:41 +0000134 uint64_t Addr = Sections[Idx].getLoadAddress();
Keno Fischer2cd66e92015-10-10 05:37:02 +0000135 DEBUG(dbgs() << "Resolving relocations Section #" << Idx << "\t"
Alexei Starovoitov36df1ca2015-03-30 05:15:57 +0000136 << format("%p", (uintptr_t)Addr) << "\n");
Keno Fischereb59d462015-12-03 21:27:59 +0000137 resolveRelocationList(it->second, Addr);
Jim Grosbacheff0a402012-01-16 22:26:39 +0000138 }
Keno Fischer2cd66e92015-10-10 05:37:02 +0000139 Relocations.clear();
Lang Hames79fce472015-09-10 20:44:36 +0000140
141 // Print out sections after relocation.
142 DEBUG(
143 for (int i = 0, e = Sections.size(); i != e; ++i)
144 dumpSectionMemory(Sections[i], "after relocations");
145 );
146
Jim Grosbach733d3052011-04-12 21:20:41 +0000147}
148
Jim Grosbach6d613972012-09-13 21:50:06 +0000149void RuntimeDyldImpl::mapSectionAddress(const void *LocalAddress,
Jim Grosbach0ddb3a42012-01-16 23:50:55 +0000150 uint64_t TargetAddress) {
Andrew Kaylor4fba0492013-10-21 17:42:06 +0000151 MutexGuard locked(lock);
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000152 for (unsigned i = 0, e = Sections.size(); i != e; ++i) {
Sanjoy Das277776a2015-11-23 21:47:41 +0000153 if (Sections[i].getAddress() == LocalAddress) {
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000154 reassignSectionAddress(i, TargetAddress);
155 return;
156 }
157 }
158 llvm_unreachable("Attempting to remap address of unknown section!");
Jim Grosbach0ddb3a42012-01-16 23:50:55 +0000159}
160
Lang Hames89595312016-04-27 20:24:48 +0000161static Error getOffset(const SymbolRef &Sym, SectionRef Sec,
162 uint64_t &Result) {
Rafael Espindolaed067c42015-07-03 18:19:00 +0000163 ErrorOr<uint64_t> AddressOrErr = Sym.getAddress();
164 if (std::error_code EC = AddressOrErr.getError())
Lang Hames89595312016-04-27 20:24:48 +0000165 return errorCodeToError(EC);
Rafael Espindolaef888a42015-07-07 16:45:55 +0000166 Result = *AddressOrErr - Sec.getAddress();
Lang Hames89595312016-04-27 20:24:48 +0000167 return Error::success();
Rafael Espindola6956b1a2014-04-21 13:45:32 +0000168}
169
Lang Hames89595312016-04-27 20:24:48 +0000170Expected<RuntimeDyldImpl::ObjSectionToIDMap>
Lang Hamesb5c7b1f2014-11-26 16:54:40 +0000171RuntimeDyldImpl::loadObjectImpl(const object::ObjectFile &Obj) {
Andrew Kaylor4fba0492013-10-21 17:42:06 +0000172 MutexGuard locked(lock);
173
Andrew Kaylor33c5b1b2013-10-15 20:44:55 +0000174 // Save information about our target
Lang Hamesb5c7b1f2014-11-26 16:54:40 +0000175 Arch = (Triple::ArchType)Obj.getArch();
176 IsTargetLittleEndian = Obj.isLittleEndian();
Petar Jovanovic97202832015-05-28 13:48:41 +0000177 setMipsABI(Obj);
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000178
Lang Hames937ec542014-02-12 21:30:07 +0000179 // Compute the memory size required to load all sections to be loaded
180 // and pass this information to the memory manager
Lang Hames633fe142015-03-30 03:37:06 +0000181 if (MemMgr.needsToReserveAllocationSpace()) {
Lang Hamesb2b7a3c2016-01-10 18:51:50 +0000182 uint64_t CodeSize = 0, RODataSize = 0, RWDataSize = 0;
183 uint32_t CodeAlign = 1, RODataAlign = 1, RWDataAlign = 1;
Lang Hames89595312016-04-27 20:24:48 +0000184 if (auto Err = computeTotalAllocSize(Obj,
185 CodeSize, CodeAlign,
186 RODataSize, RODataAlign,
187 RWDataSize, RWDataAlign))
188 return std::move(Err);
Lang Hamesb2b7a3c2016-01-10 18:51:50 +0000189 MemMgr.reserveAllocationSpace(CodeSize, CodeAlign, RODataSize, RODataAlign,
190 RWDataSize, RWDataAlign);
Lang Hames937ec542014-02-12 21:30:07 +0000191 }
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000192
Eli Benderskyfc079082012-05-01 06:58:59 +0000193 // Used sections from the object file
194 ObjSectionToIDMap LocalSections;
195
Tim Northover94bc73d2012-10-29 10:47:04 +0000196 // Common symbols requiring allocation, with their sizes and alignments
Lang Hames29968952015-01-17 00:55:05 +0000197 CommonSymbolList CommonSymbols;
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000198
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000199 // Parse symbols
200 DEBUG(dbgs() << "Parse symbols:\n");
Lang Hamesb5c7b1f2014-11-26 16:54:40 +0000201 for (symbol_iterator I = Obj.symbol_begin(), E = Obj.symbol_end(); I != E;
Jim Grosbach36f025e2014-04-21 19:23:59 +0000202 ++I) {
Jim Grosbach36f025e2014-04-21 19:23:59 +0000203 uint32_t Flags = I->getFlags();
Preston Gurd2138ef62012-04-12 20:13:57 +0000204
Lang Hamesa32d71b2015-10-18 01:41:37 +0000205 if (Flags & SymbolRef::SF_Common)
Lang Hames29968952015-01-17 00:55:05 +0000206 CommonSymbols.push_back(*I);
207 else {
Lang Hames89595312016-04-27 20:24:48 +0000208
209 // Get the symbol type.
210 object::SymbolRef::Type SymType;
211 if (auto SymTypeOrErr = I->getType())
212 SymType = *SymTypeOrErr;
213 else
Kevin Enderby7bd8d992016-05-02 20:28:12 +0000214 return SymTypeOrErr.takeError();
Lang Hames29968952015-01-17 00:55:05 +0000215
Lang Hamesa32d71b2015-10-18 01:41:37 +0000216 // Get symbol name.
Lang Hames89595312016-04-27 20:24:48 +0000217 StringRef Name;
218 if (auto NameOrErr = I->getName())
219 Name = *NameOrErr;
220 else
221 return NameOrErr.takeError();
222
Lang Hamesa32d71b2015-10-18 01:41:37 +0000223 // Compute JIT symbol flags.
224 JITSymbolFlags RTDyldSymFlags = JITSymbolFlags::None;
225 if (Flags & SymbolRef::SF_Weak)
226 RTDyldSymFlags |= JITSymbolFlags::Weak;
227 if (Flags & SymbolRef::SF_Exported)
228 RTDyldSymFlags |= JITSymbolFlags::Exported;
Lang Hames29968952015-01-17 00:55:05 +0000229
Keno Fischerddad1872015-10-21 20:22:04 +0000230 if (Flags & SymbolRef::SF_Absolute &&
231 SymType != object::SymbolRef::ST_File) {
Lang Hames89595312016-04-27 20:24:48 +0000232 uint64_t Addr = 0;
233 if (auto AddrOrErr = I->getAddress())
234 Addr = *AddrOrErr;
235 else
236 return errorCodeToError(AddrOrErr.getError());
237
Lang Hamesa32d71b2015-10-18 01:41:37 +0000238 unsigned SectionID = AbsoluteSymbolSection;
239
240 DEBUG(dbgs() << "\tType: " << SymType << " (absolute) Name: " << Name
241 << " SID: " << SectionID << " Offset: "
Lang Hames89595312016-04-27 20:24:48 +0000242 << format("%p", (uintptr_t)Addr)
Lang Hamesa32d71b2015-10-18 01:41:37 +0000243 << " flags: " << Flags << "\n");
244 GlobalSymbolTable[Name] =
Lang Hames89595312016-04-27 20:24:48 +0000245 SymbolTableEntry(SectionID, Addr, RTDyldSymFlags);
Lang Hamesa32d71b2015-10-18 01:41:37 +0000246 } else if (SymType == object::SymbolRef::ST_Function ||
247 SymType == object::SymbolRef::ST_Data ||
248 SymType == object::SymbolRef::ST_Unknown ||
249 SymType == object::SymbolRef::ST_Other) {
250
Lang Hames89595312016-04-27 20:24:48 +0000251 section_iterator SI = Obj.section_end();
252 if (auto SIOrErr = I->getSection())
253 SI = *SIOrErr;
254 else
Kevin Enderby7bd8d992016-05-02 20:28:12 +0000255 return SIOrErr.takeError();
Lang Hames89595312016-04-27 20:24:48 +0000256
Lang Hamesb5c7b1f2014-11-26 16:54:40 +0000257 if (SI == Obj.section_end())
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000258 continue;
Lang Hames89595312016-04-27 20:24:48 +0000259
Lang Hamesa32d71b2015-10-18 01:41:37 +0000260 // Get symbol offset.
Rafael Espindolaef888a42015-07-07 16:45:55 +0000261 uint64_t SectOffset;
Lang Hames89595312016-04-27 20:24:48 +0000262 if (auto Err = getOffset(*I, *SI, SectOffset))
263 return std::move(Err);
264
Rafael Espindola80291272014-10-08 15:28:58 +0000265 bool IsCode = SI->isText();
Lang Hames89595312016-04-27 20:24:48 +0000266 unsigned SectionID;
267 if (auto SectionIDOrErr = findOrEmitSection(Obj, *SI, IsCode,
268 LocalSections))
269 SectionID = *SectionIDOrErr;
270 else
271 return SectionIDOrErr.takeError();
Lang Hamesa32d71b2015-10-18 01:41:37 +0000272
Lang Hames6bfd3982015-01-16 23:13:56 +0000273 DEBUG(dbgs() << "\tType: " << SymType << " Name: " << Name
274 << " SID: " << SectionID << " Offset: "
275 << format("%p", (uintptr_t)SectOffset)
276 << " flags: " << Flags << "\n");
Lang Hamesb1186032015-03-11 00:43:26 +0000277 GlobalSymbolTable[Name] =
278 SymbolTableEntry(SectionID, SectOffset, RTDyldSymFlags);
Preston Gurd2138ef62012-04-12 20:13:57 +0000279 }
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000280 }
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000281 }
282
Preston Gurd2138ef62012-04-12 20:13:57 +0000283 // Allocate common symbols
Lang Hames89595312016-04-27 20:24:48 +0000284 if (auto Err = emitCommonSymbols(Obj, CommonSymbols))
285 return std::move(Err);
Preston Gurd2138ef62012-04-12 20:13:57 +0000286
Eli Bendersky0e2ac5b2012-04-29 12:40:47 +0000287 // Parse and process relocations
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000288 DEBUG(dbgs() << "Parse relocations:\n");
Lang Hamesb5c7b1f2014-11-26 16:54:40 +0000289 for (section_iterator SI = Obj.section_begin(), SE = Obj.section_end();
Jim Grosbach36f025e2014-04-21 19:23:59 +0000290 SI != SE; ++SI) {
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000291 StubMap Stubs;
Jim Grosbach36f025e2014-04-21 19:23:59 +0000292 section_iterator RelocatedSection = SI->getRelocatedSection();
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000293
Rafael Espindola127b6c32015-02-17 20:07:28 +0000294 if (RelocatedSection == SE)
295 continue;
296
Jim Grosbach36f025e2014-04-21 19:23:59 +0000297 relocation_iterator I = SI->relocation_begin();
298 relocation_iterator E = SI->relocation_end();
Rafael Espindola77314aa2014-04-03 22:42:22 +0000299
300 if (I == E && !ProcessAllSections)
Juergen Ributzka046709f2014-03-21 07:26:41 +0000301 continue;
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000302
Rafael Espindola80291272014-10-08 15:28:58 +0000303 bool IsCode = RelocatedSection->isText();
Lang Hames89595312016-04-27 20:24:48 +0000304 unsigned SectionID = 0;
305 if (auto SectionIDOrErr = findOrEmitSection(Obj, *RelocatedSection, IsCode,
306 LocalSections))
307 SectionID = *SectionIDOrErr;
308 else
309 return SectionIDOrErr.takeError();
310
Juergen Ributzka046709f2014-03-21 07:26:41 +0000311 DEBUG(dbgs() << "\tSectionID: " << SectionID << "\n");
312
Rafael Espindola77314aa2014-04-03 22:42:22 +0000313 for (; I != E;)
Lang Hames89595312016-04-27 20:24:48 +0000314 if (auto IOrErr = processRelocationRef(SectionID, I, Obj, LocalSections, Stubs))
315 I = *IOrErr;
316 else
317 return IOrErr.takeError();
Lang Hamesf7acddd2014-07-22 22:47:39 +0000318
Lang Hamesf7acddd2014-07-22 22:47:39 +0000319 // If there is an attached checker, notify it about the stubs for this
320 // section so that they can be verified.
321 if (Checker)
Lang Hamesb5c7b1f2014-11-26 16:54:40 +0000322 Checker->registerStubMap(Obj.getFileName(), SectionID, Stubs);
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000323 }
Preston Gurdcc31af92012-04-16 22:12:58 +0000324
Andrew Kaylor4612fed2013-08-19 23:27:43 +0000325 // Give the subclasses a chance to tie-up any loose ends.
Lang Hames89595312016-04-27 20:24:48 +0000326 if (auto Err = finalizeLoad(Obj, LocalSections))
327 return std::move(Err);
Andrew Kaylor4612fed2013-08-19 23:27:43 +0000328
Lang Hames2e88f4f2015-07-28 17:52:11 +0000329// for (auto E : LocalSections)
330// llvm::dbgs() << "Added: " << E.first.getRawDataRefImpl() << " -> " << E.second << "\n";
Lang Hamesb5c7b1f2014-11-26 16:54:40 +0000331
Lang Hames2e88f4f2015-07-28 17:52:11 +0000332 return LocalSections;
Lang Hames937ec542014-02-12 21:30:07 +0000333}
334
335// A helper method for computeTotalAllocSize.
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000336// Computes the memory size required to allocate sections with the given sizes,
Lang Hames937ec542014-02-12 21:30:07 +0000337// assuming that all sections are allocated with the given alignment
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000338static uint64_t
339computeAllocationSizeForSections(std::vector<uint64_t> &SectionSizes,
340 uint64_t Alignment) {
Lang Hames937ec542014-02-12 21:30:07 +0000341 uint64_t TotalSize = 0;
Jim Grosbach36f025e2014-04-21 19:23:59 +0000342 for (size_t Idx = 0, Cnt = SectionSizes.size(); Idx < Cnt; Idx++) {
343 uint64_t AlignedSize =
344 (SectionSizes[Idx] + Alignment - 1) / Alignment * Alignment;
Lang Hames937ec542014-02-12 21:30:07 +0000345 TotalSize += AlignedSize;
346 }
347 return TotalSize;
348}
349
Rafael Espindolaedd5f842015-06-26 12:44:10 +0000350static bool isRequiredForExecution(const SectionRef Section) {
Rafael Espindola0e77a942014-12-10 20:46:55 +0000351 const ObjectFile *Obj = Section.getObject();
Rafael Espindolaedd5f842015-06-26 12:44:10 +0000352 if (isa<object::ELFObjectFileBase>(Obj))
353 return ELFSectionRef(Section).getFlags() & ELF::SHF_ALLOC;
David Majnemer1a666e02015-03-07 20:21:27 +0000354 if (auto *COFFObj = dyn_cast<object::COFFObjectFile>(Obj)) {
355 const coff_section *CoffSection = COFFObj->getCOFFSection(Section);
356 // Avoid loading zero-sized COFF sections.
357 // In PE files, VirtualSize gives the section size, and SizeOfRawData
358 // may be zero for sections with content. In Obj files, SizeOfRawData
359 // gives the section size, and VirtualSize is always zero. Hence
360 // the need to check for both cases below.
361 bool HasContent = (CoffSection->VirtualSize > 0)
362 || (CoffSection->SizeOfRawData > 0);
363 bool IsDiscardable = CoffSection->Characteristics &
364 (COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_LNK_INFO);
365 return HasContent && !IsDiscardable;
366 }
367
Rafael Espindola0e77a942014-12-10 20:46:55 +0000368 assert(isa<MachOObjectFile>(Obj));
369 return true;
Rafael Espindolaedd5f842015-06-26 12:44:10 +0000370}
Rafael Espindola0e77a942014-12-10 20:46:55 +0000371
Rafael Espindolaedd5f842015-06-26 12:44:10 +0000372static bool isReadOnlyData(const SectionRef Section) {
Rafael Espindola0e77a942014-12-10 20:46:55 +0000373 const ObjectFile *Obj = Section.getObject();
Rafael Espindolaedd5f842015-06-26 12:44:10 +0000374 if (isa<object::ELFObjectFileBase>(Obj))
375 return !(ELFSectionRef(Section).getFlags() &
Rafael Espindola0e77a942014-12-10 20:46:55 +0000376 (ELF::SHF_WRITE | ELF::SHF_EXECINSTR));
David Majnemer1a666e02015-03-07 20:21:27 +0000377 if (auto *COFFObj = dyn_cast<object::COFFObjectFile>(Obj))
378 return ((COFFObj->getCOFFSection(Section)->Characteristics &
379 (COFF::IMAGE_SCN_CNT_INITIALIZED_DATA
380 | COFF::IMAGE_SCN_MEM_READ
381 | COFF::IMAGE_SCN_MEM_WRITE))
382 ==
383 (COFF::IMAGE_SCN_CNT_INITIALIZED_DATA
384 | COFF::IMAGE_SCN_MEM_READ));
385
Rafael Espindola0e77a942014-12-10 20:46:55 +0000386 assert(isa<MachOObjectFile>(Obj));
387 return false;
388}
389
Rafael Espindola41401e92015-06-26 12:33:37 +0000390static bool isZeroInit(const SectionRef Section) {
Rafael Espindola0e77a942014-12-10 20:46:55 +0000391 const ObjectFile *Obj = Section.getObject();
Rafael Espindola41401e92015-06-26 12:33:37 +0000392 if (isa<object::ELFObjectFileBase>(Obj))
393 return ELFSectionRef(Section).getType() == ELF::SHT_NOBITS;
David Majnemer1a666e02015-03-07 20:21:27 +0000394 if (auto *COFFObj = dyn_cast<object::COFFObjectFile>(Obj))
395 return COFFObj->getCOFFSection(Section)->Characteristics &
396 COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA;
Rafael Espindola0e77a942014-12-10 20:46:55 +0000397
398 auto *MachO = cast<MachOObjectFile>(Obj);
399 unsigned SectionType = MachO->getSectionType(Section);
400 return SectionType == MachO::S_ZEROFILL ||
401 SectionType == MachO::S_GB_ZEROFILL;
402}
403
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000404// Compute an upper bound of the memory size that is required to load all
405// sections
Lang Hames89595312016-04-27 20:24:48 +0000406Error RuntimeDyldImpl::computeTotalAllocSize(const ObjectFile &Obj,
407 uint64_t &CodeSize,
408 uint32_t &CodeAlign,
409 uint64_t &RODataSize,
410 uint32_t &RODataAlign,
411 uint64_t &RWDataSize,
412 uint32_t &RWDataAlign) {
Lang Hames937ec542014-02-12 21:30:07 +0000413 // Compute the size of all sections required for execution
414 std::vector<uint64_t> CodeSectionSizes;
415 std::vector<uint64_t> ROSectionSizes;
416 std::vector<uint64_t> RWSectionSizes;
Lang Hames937ec542014-02-12 21:30:07 +0000417
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000418 // Collect sizes of all sections to be loaded;
Lang Hames937ec542014-02-12 21:30:07 +0000419 // also determine the max alignment of all sections
Lang Hamesb5c7b1f2014-11-26 16:54:40 +0000420 for (section_iterator SI = Obj.section_begin(), SE = Obj.section_end();
Jim Grosbach36f025e2014-04-21 19:23:59 +0000421 SI != SE; ++SI) {
422 const SectionRef &Section = *SI;
423
Rafael Espindola0e77a942014-12-10 20:46:55 +0000424 bool IsRequired = isRequiredForExecution(Section);
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000425
Lang Hames937ec542014-02-12 21:30:07 +0000426 // Consider only the sections that are required to be loaded for execution
427 if (IsRequired) {
Rafael Espindola80291272014-10-08 15:28:58 +0000428 uint64_t DataSize = Section.getSize();
429 uint64_t Alignment64 = Section.getAlignment();
Lang Hames89595312016-04-27 20:24:48 +0000430 unsigned Alignment = (unsigned)Alignment64 & 0xffffffffL;
Rafael Espindola80291272014-10-08 15:28:58 +0000431 bool IsCode = Section.isText();
Rafael Espindola0e77a942014-12-10 20:46:55 +0000432 bool IsReadOnly = isReadOnlyData(Section);
Lang Hames89595312016-04-27 20:24:48 +0000433
434 StringRef Name;
435 if (auto EC = Section.getName(Name))
436 return errorCodeToError(EC);
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000437
Lang Hames937ec542014-02-12 21:30:07 +0000438 uint64_t StubBufSize = computeSectionStubBufSize(Obj, Section);
439 uint64_t SectionSize = DataSize + StubBufSize;
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000440
441 // The .eh_frame section (at least on Linux) needs an extra four bytes
442 // padded
Lang Hames937ec542014-02-12 21:30:07 +0000443 // with zeroes added at the end. For MachO objects, this section has a
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000444 // slightly different name, so this won't have any effect for MachO
445 // objects.
Lang Hames937ec542014-02-12 21:30:07 +0000446 if (Name == ".eh_frame")
447 SectionSize += 4;
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000448
Lang Hames2be1bbe2015-04-07 06:27:56 +0000449 if (!SectionSize)
450 SectionSize = 1;
451
452 if (IsCode) {
Lang Hamesb2b7a3c2016-01-10 18:51:50 +0000453 CodeAlign = std::max(CodeAlign, Alignment);
Lang Hames2be1bbe2015-04-07 06:27:56 +0000454 CodeSectionSizes.push_back(SectionSize);
455 } else if (IsReadOnly) {
Lang Hamesb2b7a3c2016-01-10 18:51:50 +0000456 RODataAlign = std::max(RODataAlign, Alignment);
Lang Hames2be1bbe2015-04-07 06:27:56 +0000457 ROSectionSizes.push_back(SectionSize);
458 } else {
Lang Hamesb2b7a3c2016-01-10 18:51:50 +0000459 RWDataAlign = std::max(RWDataAlign, Alignment);
Lang Hames2be1bbe2015-04-07 06:27:56 +0000460 RWSectionSizes.push_back(SectionSize);
461 }
Lang Hames937ec542014-02-12 21:30:07 +0000462 }
463 }
464
465 // Compute the size of all common symbols
466 uint64_t CommonSize = 0;
Lang Hames543e0dc2016-04-21 20:08:06 +0000467 uint32_t CommonAlign = 1;
Lang Hamesb5c7b1f2014-11-26 16:54:40 +0000468 for (symbol_iterator I = Obj.symbol_begin(), E = Obj.symbol_end(); I != E;
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000469 ++I) {
Lang Hames937ec542014-02-12 21:30:07 +0000470 uint32_t Flags = I->getFlags();
471 if (Flags & SymbolRef::SF_Common) {
472 // Add the common symbols to a list. We'll allocate them all below.
Rafael Espindolad7a32ea2015-06-24 10:20:30 +0000473 uint64_t Size = I->getCommonSize();
Lang Hames543e0dc2016-04-21 20:08:06 +0000474 uint32_t Align = I->getAlignment();
475 // If this is the first common symbol, use its alignment as the alignment
476 // for the common symbols section.
477 if (CommonSize == 0)
478 CommonAlign = Align;
479 CommonSize = alignTo(CommonSize, Align) + Size;
Lang Hames937ec542014-02-12 21:30:07 +0000480 }
481 }
482 if (CommonSize != 0) {
483 RWSectionSizes.push_back(CommonSize);
Lang Hames543e0dc2016-04-21 20:08:06 +0000484 RWDataAlign = std::max(RWDataAlign, CommonAlign);
Lang Hames937ec542014-02-12 21:30:07 +0000485 }
486
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000487 // Compute the required allocation space for each different type of sections
488 // (code, read-only data, read-write data) assuming that all sections are
Lang Hames937ec542014-02-12 21:30:07 +0000489 // allocated with the max alignment. Note that we cannot compute with the
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000490 // individual alignments of the sections, because then the required size
Lang Hames937ec542014-02-12 21:30:07 +0000491 // depends on the order, in which the sections are allocated.
Lang Hamesb2b7a3c2016-01-10 18:51:50 +0000492 CodeSize = computeAllocationSizeForSections(CodeSectionSizes, CodeAlign);
493 RODataSize = computeAllocationSizeForSections(ROSectionSizes, RODataAlign);
494 RWDataSize = computeAllocationSizeForSections(RWSectionSizes, RWDataAlign);
Lang Hames89595312016-04-27 20:24:48 +0000495
496 return Error::success();
Lang Hames937ec542014-02-12 21:30:07 +0000497}
498
499// compute stub buffer size for the given section
Lang Hamesb5c7b1f2014-11-26 16:54:40 +0000500unsigned RuntimeDyldImpl::computeSectionStubBufSize(const ObjectFile &Obj,
Lang Hames937ec542014-02-12 21:30:07 +0000501 const SectionRef &Section) {
502 unsigned StubSize = getMaxStubSize();
503 if (StubSize == 0) {
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000504 return 0;
Lang Hames937ec542014-02-12 21:30:07 +0000505 }
506 // FIXME: this is an inefficient way to handle this. We should computed the
507 // necessary section allocation size in loadObject by walking all the sections
508 // once.
509 unsigned StubBufSize = 0;
Lang Hamesb5c7b1f2014-11-26 16:54:40 +0000510 for (section_iterator SI = Obj.section_begin(), SE = Obj.section_end();
Jim Grosbach36f025e2014-04-21 19:23:59 +0000511 SI != SE; ++SI) {
512 section_iterator RelSecI = SI->getRelocatedSection();
Lang Hames937ec542014-02-12 21:30:07 +0000513 if (!(RelSecI == Section))
514 continue;
515
Sanjoy Dasd5658b02015-11-23 21:47:51 +0000516 for (const RelocationRef &Reloc : SI->relocations())
517 if (relocationNeedsStub(Reloc))
518 StubBufSize += StubSize;
Lang Hames937ec542014-02-12 21:30:07 +0000519 }
Alexey Samsonovaa4d2952014-03-14 14:22:49 +0000520
Lang Hames937ec542014-02-12 21:30:07 +0000521 // Get section data size and alignment
Rafael Espindola80291272014-10-08 15:28:58 +0000522 uint64_t DataSize = Section.getSize();
523 uint64_t Alignment64 = Section.getAlignment();
Lang Hames937ec542014-02-12 21:30:07 +0000524
525 // Add stubbuf size alignment
526 unsigned Alignment = (unsigned)Alignment64 & 0xffffffffL;
527 unsigned StubAlignment = getStubAlignment();
528 unsigned EndAlignment = (DataSize | Alignment) & -(DataSize | Alignment);
529 if (StubAlignment > EndAlignment)
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000530 StubBufSize += StubAlignment - EndAlignment;
Lang Hames937ec542014-02-12 21:30:07 +0000531 return StubBufSize;
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000532}
533
Lang Hamese1287c02014-08-29 23:17:47 +0000534uint64_t RuntimeDyldImpl::readBytesUnaligned(uint8_t *Src,
535 unsigned Size) const {
536 uint64_t Result = 0;
Lang Hames69abd722014-09-07 02:05:26 +0000537 if (IsTargetLittleEndian) {
538 Src += Size - 1;
539 while (Size--)
540 Result = (Result << 8) | *Src--;
541 } else
542 while (Size--)
543 Result = (Result << 8) | *Src++;
Lang Hamese1287c02014-08-29 23:17:47 +0000544
545 return Result;
546}
547
548void RuntimeDyldImpl::writeBytesUnaligned(uint64_t Value, uint8_t *Dst,
549 unsigned Size) const {
Lang Hames69abd722014-09-07 02:05:26 +0000550 if (IsTargetLittleEndian) {
551 while (Size--) {
552 *Dst++ = Value & 0xFF;
553 Value >>= 8;
554 }
Lang Hamese1287c02014-08-29 23:17:47 +0000555 } else {
Lang Hames69abd722014-09-07 02:05:26 +0000556 Dst += Size - 1;
557 while (Size--) {
558 *Dst-- = Value & 0xFF;
559 Value >>= 8;
560 }
Lang Hamese1287c02014-08-29 23:17:47 +0000561 }
562}
563
Lang Hames89595312016-04-27 20:24:48 +0000564Error RuntimeDyldImpl::emitCommonSymbols(const ObjectFile &Obj,
565 CommonSymbolList &CommonSymbols) {
Lang Hames29968952015-01-17 00:55:05 +0000566 if (CommonSymbols.empty())
Lang Hames89595312016-04-27 20:24:48 +0000567 return Error::success();
Lang Hames29968952015-01-17 00:55:05 +0000568
569 uint64_t CommonSize = 0;
Lang Hames543e0dc2016-04-21 20:08:06 +0000570 uint32_t CommonAlign = CommonSymbols.begin()->getAlignment();
Lang Hames29968952015-01-17 00:55:05 +0000571 CommonSymbolList SymbolsToAllocate;
572
573 DEBUG(dbgs() << "Processing common symbols...\n");
574
575 for (const auto &Sym : CommonSymbols) {
Lang Hames89595312016-04-27 20:24:48 +0000576 StringRef Name;
577 if (auto NameOrErr = Sym.getName())
578 Name = *NameOrErr;
579 else
580 return NameOrErr.takeError();
Lang Hames29968952015-01-17 00:55:05 +0000581
Lang Hames29968952015-01-17 00:55:05 +0000582 // Skip common symbols already elsewhere.
Lang Hames93de2a12015-01-23 21:25:00 +0000583 if (GlobalSymbolTable.count(Name) ||
Lang Hames633fe142015-03-30 03:37:06 +0000584 Resolver.findSymbolInLogicalDylib(Name)) {
Lang Hames29968952015-01-17 00:55:05 +0000585 DEBUG(dbgs() << "\tSkipping already emitted common symbol '" << Name
586 << "'\n");
587 continue;
588 }
589
Rafael Espindolaa4d224722015-05-31 23:52:50 +0000590 uint32_t Align = Sym.getAlignment();
Rafael Espindolad7a32ea2015-06-24 10:20:30 +0000591 uint64_t Size = Sym.getCommonSize();
Lang Hames29968952015-01-17 00:55:05 +0000592
Lang Hames543e0dc2016-04-21 20:08:06 +0000593 CommonSize = alignTo(CommonSize, Align) + Size;
594
Lang Hames29968952015-01-17 00:55:05 +0000595 SymbolsToAllocate.push_back(Sym);
596 }
597
Preston Gurd2138ef62012-04-12 20:13:57 +0000598 // Allocate memory for the section
599 unsigned SectionID = Sections.size();
Lang Hames543e0dc2016-04-21 20:08:06 +0000600 uint8_t *Addr = MemMgr.allocateDataSection(CommonSize, CommonAlign,
601 SectionID, "<common symbols>",
602 false);
Preston Gurd2138ef62012-04-12 20:13:57 +0000603 if (!Addr)
604 report_fatal_error("Unable to allocate memory for common symbols!");
605 uint64_t Offset = 0;
Sanjoy Das80825922015-11-23 21:47:46 +0000606 Sections.push_back(
607 SectionEntry("<common symbols>", Addr, CommonSize, CommonSize, 0));
Lang Hames29968952015-01-17 00:55:05 +0000608 memset(Addr, 0, CommonSize);
Preston Gurd2138ef62012-04-12 20:13:57 +0000609
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000610 DEBUG(dbgs() << "emitCommonSection SectionID: " << SectionID << " new addr: "
Lang Hames29968952015-01-17 00:55:05 +0000611 << format("%p", Addr) << " DataSize: " << CommonSize << "\n");
Preston Gurd2138ef62012-04-12 20:13:57 +0000612
613 // Assign the address of each symbol
Lang Hames29968952015-01-17 00:55:05 +0000614 for (auto &Sym : SymbolsToAllocate) {
Rafael Espindolaa4d224722015-05-31 23:52:50 +0000615 uint32_t Align = Sym.getAlignment();
Rafael Espindolad7a32ea2015-06-24 10:20:30 +0000616 uint64_t Size = Sym.getCommonSize();
Lang Hames89595312016-04-27 20:24:48 +0000617 StringRef Name;
618 if (auto NameOrErr = Sym.getName())
619 Name = *NameOrErr;
620 else
621 return NameOrErr.takeError();
Tim Northover94bc73d2012-10-29 10:47:04 +0000622 if (Align) {
623 // This symbol has an alignment requirement.
624 uint64_t AlignOffset = OffsetToAlignment((uint64_t)Addr, Align);
625 Addr += AlignOffset;
626 Offset += AlignOffset;
Tim Northover94bc73d2012-10-29 10:47:04 +0000627 }
Lang Hames29968952015-01-17 00:55:05 +0000628 uint32_t Flags = Sym.getFlags();
Lang Hamesb1186032015-03-11 00:43:26 +0000629 JITSymbolFlags RTDyldSymFlags = JITSymbolFlags::None;
630 if (Flags & SymbolRef::SF_Weak)
631 RTDyldSymFlags |= JITSymbolFlags::Weak;
632 if (Flags & SymbolRef::SF_Exported)
633 RTDyldSymFlags |= JITSymbolFlags::Exported;
Lang Hames29968952015-01-17 00:55:05 +0000634 DEBUG(dbgs() << "Allocating common symbol " << Name << " address "
635 << format("%p", Addr) << "\n");
Lang Hamesb1186032015-03-11 00:43:26 +0000636 GlobalSymbolTable[Name] =
637 SymbolTableEntry(SectionID, Offset, RTDyldSymFlags);
Preston Gurd2138ef62012-04-12 20:13:57 +0000638 Offset += Size;
639 Addr += Size;
640 }
Petar Jovanovicd22164d2015-08-13 15:12:49 +0000641
642 if (Checker)
643 Checker->registerSection(Obj.getFileName(), SectionID);
Lang Hames89595312016-04-27 20:24:48 +0000644
645 return Error::success();
Preston Gurd2138ef62012-04-12 20:13:57 +0000646}
647
Lang Hames89595312016-04-27 20:24:48 +0000648Expected<unsigned>
649RuntimeDyldImpl::emitSection(const ObjectFile &Obj,
650 const SectionRef &Section,
651 bool IsCode) {
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000652 StringRef data;
Rafael Espindola80291272014-10-08 15:28:58 +0000653 uint64_t Alignment64 = Section.getAlignment();
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000654
655 unsigned Alignment = (unsigned)Alignment64 & 0xffffffffL;
Andrew Kaylor877b9312013-10-16 00:32:24 +0000656 unsigned PaddingSize = 0;
Lang Hames937ec542014-02-12 21:30:07 +0000657 unsigned StubBufSize = 0;
Rafael Espindola0e77a942014-12-10 20:46:55 +0000658 bool IsRequired = isRequiredForExecution(Section);
Rafael Espindola80291272014-10-08 15:28:58 +0000659 bool IsVirtual = Section.isVirtual();
Rafael Espindola0e77a942014-12-10 20:46:55 +0000660 bool IsZeroInit = isZeroInit(Section);
661 bool IsReadOnly = isReadOnlyData(Section);
Rafael Espindola80291272014-10-08 15:28:58 +0000662 uint64_t DataSize = Section.getSize();
Lang Hames89595312016-04-27 20:24:48 +0000663
664 StringRef Name;
665 if (auto EC = Section.getName(Name))
666 return errorCodeToError(EC);
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000667
668 StubBufSize = computeSectionStubBufSize(Obj, Section);
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000669
Andrew Kaylor877b9312013-10-16 00:32:24 +0000670 // The .eh_frame section (at least on Linux) needs an extra four bytes padded
671 // with zeroes added at the end. For MachO objects, this section has a
672 // slightly different name, so this won't have any effect for MachO objects.
673 if (Name == ".eh_frame")
674 PaddingSize = 4;
675
Lang Hamesd4100172014-02-11 05:28:24 +0000676 uintptr_t Allocate;
Preston Gurd2138ef62012-04-12 20:13:57 +0000677 unsigned SectionID = Sections.size();
678 uint8_t *Addr;
Craig Topper353eda42014-04-24 06:44:33 +0000679 const char *pData = nullptr;
Preston Gurd2138ef62012-04-12 20:13:57 +0000680
Lang Hames86a45932015-10-15 06:41:45 +0000681 // If this section contains any bits (i.e. isn't a virtual or bss section),
682 // grab a reference to them.
683 if (!IsVirtual && !IsZeroInit) {
684 // In either case, set the location of the unrelocated section in memory,
685 // since we still process relocations for it even if we're not applying them.
Lang Hames89595312016-04-27 20:24:48 +0000686 if (auto EC = Section.getContents(data))
687 return errorCodeToError(EC);
Keno Fischere6892c82015-05-01 20:21:45 +0000688 pData = data.data();
Lang Hames86a45932015-10-15 06:41:45 +0000689 }
Keno Fischere6892c82015-05-01 20:21:45 +0000690
Lang Hames7e66c6c2015-08-14 06:26:42 +0000691 // Code section alignment needs to be at least as high as stub alignment or
692 // padding calculations may by incorrect when the section is remapped to a
693 // higher alignment.
694 if (IsCode)
695 Alignment = std::max(Alignment, getStubAlignment());
696
Preston Gurd2138ef62012-04-12 20:13:57 +0000697 // Some sections, such as debug info, don't need to be loaded for execution.
698 // Leave those where they are.
699 if (IsRequired) {
Andrew Kaylor877b9312013-10-16 00:32:24 +0000700 Allocate = DataSize + PaddingSize + StubBufSize;
Lang Hames2be1bbe2015-04-07 06:27:56 +0000701 if (!Allocate)
702 Allocate = 1;
Lang Hames633fe142015-03-30 03:37:06 +0000703 Addr = IsCode ? MemMgr.allocateCodeSection(Allocate, Alignment, SectionID,
704 Name)
705 : MemMgr.allocateDataSection(Allocate, Alignment, SectionID,
706 Name, IsReadOnly);
Preston Gurd2138ef62012-04-12 20:13:57 +0000707 if (!Addr)
708 report_fatal_error("Unable to allocate section memory!");
709
Preston Gurd2138ef62012-04-12 20:13:57 +0000710 // Zero-initialize or copy the data from the image
711 if (IsZeroInit || IsVirtual)
712 memset(Addr, 0, DataSize);
713 else
714 memcpy(Addr, pData, DataSize);
715
Andrew Kaylor877b9312013-10-16 00:32:24 +0000716 // Fill in any extra bytes we allocated for padding
717 if (PaddingSize != 0) {
718 memset(Addr + DataSize, 0, PaddingSize);
719 // Update the DataSize variable so that the stub offset is set correctly.
720 DataSize += PaddingSize;
721 }
722
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000723 DEBUG(dbgs() << "emitSection SectionID: " << SectionID << " Name: " << Name
Preston Gurd2138ef62012-04-12 20:13:57 +0000724 << " obj addr: " << format("%p", pData)
725 << " new addr: " << format("%p", Addr)
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000726 << " DataSize: " << DataSize << " StubBufSize: " << StubBufSize
727 << " Allocate: " << Allocate << "\n");
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000728 } else {
Preston Gurd2138ef62012-04-12 20:13:57 +0000729 // Even if we didn't load the section, we need to record an entry for it
Eli Bendersky0e2ac5b2012-04-29 12:40:47 +0000730 // to handle later processing (and by 'handle' I mean don't do anything
731 // with these sections).
Preston Gurd2138ef62012-04-12 20:13:57 +0000732 Allocate = 0;
Craig Topper353eda42014-04-24 06:44:33 +0000733 Addr = nullptr;
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000734 DEBUG(dbgs() << "emitSection SectionID: " << SectionID << " Name: " << Name
735 << " obj addr: " << format("%p", data.data()) << " new addr: 0"
736 << " DataSize: " << DataSize << " StubBufSize: " << StubBufSize
737 << " Allocate: " << Allocate << "\n");
Preston Gurd2138ef62012-04-12 20:13:57 +0000738 }
739
Sanjoy Das80825922015-11-23 21:47:46 +0000740 Sections.push_back(
741 SectionEntry(Name, Addr, DataSize, Allocate, (uintptr_t)pData));
Lang Hames587ee6a2014-09-03 05:01:46 +0000742
743 if (Checker)
Lang Hamesb5c7b1f2014-11-26 16:54:40 +0000744 Checker->registerSection(Obj.getFileName(), SectionID);
Lang Hames587ee6a2014-09-03 05:01:46 +0000745
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000746 return SectionID;
747}
748
Lang Hames89595312016-04-27 20:24:48 +0000749Expected<unsigned>
750RuntimeDyldImpl::findOrEmitSection(const ObjectFile &Obj,
751 const SectionRef &Section,
752 bool IsCode,
753 ObjSectionToIDMap &LocalSections) {
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000754
755 unsigned SectionID = 0;
756 ObjSectionToIDMap::iterator i = LocalSections.find(Section);
757 if (i != LocalSections.end())
758 SectionID = i->second;
759 else {
Lang Hames89595312016-04-27 20:24:48 +0000760 if (auto SectionIDOrErr = emitSection(Obj, Section, IsCode))
761 SectionID = *SectionIDOrErr;
762 else
763 return SectionIDOrErr.takeError();
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000764 LocalSections[Section] = SectionID;
765 }
766 return SectionID;
767}
768
Eli Bendersky667b8792012-05-01 10:41:12 +0000769void RuntimeDyldImpl::addRelocationForSection(const RelocationEntry &RE,
770 unsigned SectionID) {
771 Relocations[SectionID].push_back(RE);
772}
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000773
Eli Bendersky667b8792012-05-01 10:41:12 +0000774void RuntimeDyldImpl::addRelocationForSymbol(const RelocationEntry &RE,
775 StringRef SymbolName) {
776 // Relocation by symbol. If the symbol is found in the global symbol table,
777 // create an appropriate section relocation. Otherwise, add it to
778 // ExternalSymbolRelocations.
Lang Hames6bfd3982015-01-16 23:13:56 +0000779 RTDyldSymbolTable::const_iterator Loc = GlobalSymbolTable.find(SymbolName);
Eli Bendersky667b8792012-05-01 10:41:12 +0000780 if (Loc == GlobalSymbolTable.end()) {
781 ExternalSymbolRelocations[SymbolName].push_back(RE);
Eli Benderskyb92e1cf2012-04-30 12:15:58 +0000782 } else {
Eli Bendersky667b8792012-05-01 10:41:12 +0000783 // Copy the RE since we want to modify its addend.
784 RelocationEntry RECopy = RE;
Lang Hames6bfd3982015-01-16 23:13:56 +0000785 const auto &SymInfo = Loc->second;
786 RECopy.Addend += SymInfo.getOffset();
787 Relocations[SymInfo.getSectionID()].push_back(RECopy);
Eli Benderskyb92e1cf2012-04-30 12:15:58 +0000788 }
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000789}
790
Ulrich Weigand752b5c92014-07-20 23:53:14 +0000791uint8_t *RuntimeDyldImpl::createStubFunction(uint8_t *Addr,
792 unsigned AbiVariant) {
Tim Northovere19bed72014-07-23 12:32:47 +0000793 if (Arch == Triple::aarch64 || Arch == Triple::aarch64_be) {
Tim Northover37cde972013-05-04 20:14:09 +0000794 // This stub has to be able to access the full address space,
795 // since symbol lookup won't necessarily find a handy, in-range,
796 // PLT stub for functions which could be anywhere.
Tim Northover37cde972013-05-04 20:14:09 +0000797 // Stub can use ip0 (== x16) to calculate address
Daniel Sanders66e799f2014-11-06 09:53:05 +0000798 writeBytesUnaligned(0xd2e00010, Addr, 4); // movz ip0, #:abs_g3:<addr>
799 writeBytesUnaligned(0xf2c00010, Addr+4, 4); // movk ip0, #:abs_g2_nc:<addr>
800 writeBytesUnaligned(0xf2a00010, Addr+8, 4); // movk ip0, #:abs_g1_nc:<addr>
801 writeBytesUnaligned(0xf2800010, Addr+12, 4); // movk ip0, #:abs_g0_nc:<addr>
802 writeBytesUnaligned(0xd61f0200, Addr+16, 4); // br ip0
Tim Northover37cde972013-05-04 20:14:09 +0000803
804 return Addr;
Christian Pirker2a111602014-03-28 14:35:30 +0000805 } else if (Arch == Triple::arm || Arch == Triple::armeb) {
Akira Hatanaka111174b2012-08-17 21:28:04 +0000806 // TODO: There is only ARM far stub now. We should add the Thumb stub,
807 // and stubs for branches Thumb - ARM and ARM - Thumb.
Daniel Sanders66e799f2014-11-06 09:53:05 +0000808 writeBytesUnaligned(0xe51ff004, Addr, 4); // ldr pc,<label>
809 return Addr + 4;
Petar Jovanovic97202832015-05-28 13:48:41 +0000810 } else if (IsMipsO32ABI) {
Akira Hatanaka111174b2012-08-17 21:28:04 +0000811 // 0: 3c190000 lui t9,%hi(addr).
812 // 4: 27390000 addiu t9,t9,%lo(addr).
813 // 8: 03200008 jr t9.
814 // c: 00000000 nop.
815 const unsigned LuiT9Instr = 0x3c190000, AdduiT9Instr = 0x27390000;
816 const unsigned JrT9Instr = 0x03200008, NopInstr = 0x0;
817
Daniel Sanders66e799f2014-11-06 09:53:05 +0000818 writeBytesUnaligned(LuiT9Instr, Addr, 4);
819 writeBytesUnaligned(AdduiT9Instr, Addr+4, 4);
820 writeBytesUnaligned(JrT9Instr, Addr+8, 4);
821 writeBytesUnaligned(NopInstr, Addr+12, 4);
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000822 return Addr;
Bill Schmidt0a9170d2013-07-26 01:35:43 +0000823 } else if (Arch == Triple::ppc64 || Arch == Triple::ppc64le) {
Ulrich Weigand752b5c92014-07-20 23:53:14 +0000824 // Depending on which version of the ELF ABI is in use, we need to
825 // generate one of two variants of the stub. They both start with
826 // the same sequence to load the target address into r12.
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000827 writeInt32BE(Addr, 0x3D800000); // lis r12, highest(addr)
828 writeInt32BE(Addr+4, 0x618C0000); // ori r12, higher(addr)
829 writeInt32BE(Addr+8, 0x798C07C6); // sldi r12, r12, 32
830 writeInt32BE(Addr+12, 0x658C0000); // oris r12, r12, h(addr)
831 writeInt32BE(Addr+16, 0x618C0000); // ori r12, r12, l(addr)
Ulrich Weigand752b5c92014-07-20 23:53:14 +0000832 if (AbiVariant == 2) {
833 // PowerPC64 stub ELFv2 ABI: The address points to the function itself.
834 // The address is already in r12 as required by the ABI. Branch to it.
835 writeInt32BE(Addr+20, 0xF8410018); // std r2, 24(r1)
836 writeInt32BE(Addr+24, 0x7D8903A6); // mtctr r12
837 writeInt32BE(Addr+28, 0x4E800420); // bctr
838 } else {
839 // PowerPC64 stub ELFv1 ABI: The address points to a function descriptor.
840 // Load the function address on r11 and sets it to control register. Also
841 // loads the function TOC in r2 and environment pointer to r11.
842 writeInt32BE(Addr+20, 0xF8410028); // std r2, 40(r1)
843 writeInt32BE(Addr+24, 0xE96C0000); // ld r11, 0(r12)
844 writeInt32BE(Addr+28, 0xE84C0008); // ld r2, 0(r12)
845 writeInt32BE(Addr+32, 0x7D6903A6); // mtctr r11
846 writeInt32BE(Addr+36, 0xE96C0010); // ld r11, 16(r2)
847 writeInt32BE(Addr+40, 0x4E800420); // bctr
848 }
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000849 return Addr;
Richard Sandifordca044082013-05-03 14:15:35 +0000850 } else if (Arch == Triple::systemz) {
851 writeInt16BE(Addr, 0xC418); // lgrl %r1,.+8
852 writeInt16BE(Addr+2, 0x0000);
853 writeInt16BE(Addr+4, 0x0004);
854 writeInt16BE(Addr+6, 0x07F1); // brc 15,%r1
855 // 8-byte address stored at Addr + 8
856 return Addr;
Andrew Kaylor4612fed2013-08-19 23:27:43 +0000857 } else if (Arch == Triple::x86_64) {
858 *Addr = 0xFF; // jmp
859 *(Addr+1) = 0x25; // rip
860 // 32-bit PC-relative address of the GOT entry will be stored at Addr+2
Lang Hames36072da2014-05-12 21:39:59 +0000861 } else if (Arch == Triple::x86) {
862 *Addr = 0xE9; // 32-bit pc-relative jump.
Akira Hatanaka111174b2012-08-17 21:28:04 +0000863 }
864 return Addr;
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000865}
866
867// Assign an address to a symbol name and resolve all the relocations
868// associated with it.
869void RuntimeDyldImpl::reassignSectionAddress(unsigned SectionID,
870 uint64_t Addr) {
871 // The address to use for relocation resolution is not
872 // the address of the local section buffer. We must be doing
Andrew Kaylora714efc2012-11-05 20:57:16 +0000873 // a remote execution environment of some sort. Relocations can't
874 // be applied until all the sections have been moved. The client must
875 // trigger this with a call to MCJIT::finalize() or
876 // RuntimeDyld::resolveRelocations().
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000877 //
878 // Addr is a uint64_t because we can't assume the pointer width
879 // of the target is the same as that of the host. Just use a generic
880 // "big enough" type.
Sanjoy Das277776a2015-11-23 21:47:41 +0000881 DEBUG(dbgs() << "Reassigning address for section " << SectionID << " ("
882 << Sections[SectionID].getName() << "): "
883 << format("0x%016" PRIx64, Sections[SectionID].getLoadAddress())
884 << " -> " << format("0x%016" PRIx64, Addr) << "\n");
885 Sections[SectionID].setLoadAddress(Addr);
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000886}
887
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000888void RuntimeDyldImpl::resolveRelocationList(const RelocationList &Relocs,
889 uint64_t Value) {
890 for (unsigned i = 0, e = Relocs.size(); i != e; ++i) {
Rafael Espindolaf1f1c622013-04-29 17:24:34 +0000891 const RelocationEntry &RE = Relocs[i];
892 // Ignore relocations for sections that were not loaded
Sanjoy Das277776a2015-11-23 21:47:41 +0000893 if (Sections[RE.SectionID].getAddress() == nullptr)
Rafael Espindolaf1f1c622013-04-29 17:24:34 +0000894 continue;
895 resolveRelocation(RE, Value);
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000896 }
897}
898
Eli Benderskyb92e1cf2012-04-30 12:15:58 +0000899void RuntimeDyldImpl::resolveExternalSymbols() {
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000900 while (!ExternalSymbolRelocations.empty()) {
Andrew Kaylorea395922013-10-01 01:47:35 +0000901 StringMap<RelocationList>::iterator i = ExternalSymbolRelocations.begin();
902
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000903 StringRef Name = i->first();
Andrew Kaylorea395922013-10-01 01:47:35 +0000904 if (Name.size() == 0) {
905 // This is an absolute symbol, use an address of zero.
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000906 DEBUG(dbgs() << "Resolving absolute relocations."
907 << "\n");
Andrew Kaylorcfb4a992013-11-11 19:55:10 +0000908 RelocationList &Relocs = i->second;
Andrew Kaylorea395922013-10-01 01:47:35 +0000909 resolveRelocationList(Relocs, 0);
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000910 } else {
Andrew Kaylorea395922013-10-01 01:47:35 +0000911 uint64_t Addr = 0;
Lang Hames6bfd3982015-01-16 23:13:56 +0000912 RTDyldSymbolTable::const_iterator Loc = GlobalSymbolTable.find(Name);
Andrew Kaylorea395922013-10-01 01:47:35 +0000913 if (Loc == GlobalSymbolTable.end()) {
Lang Hames633fe142015-03-30 03:37:06 +0000914 // This is an external symbol, try to get its address from the symbol
915 // resolver.
916 Addr = Resolver.findSymbol(Name.data()).getAddress();
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000917 // The call to getSymbolAddress may have caused additional modules to
918 // be loaded, which may have added new entries to the
919 // ExternalSymbolRelocations map. Consquently, we need to update our
920 // iterator. This is also why retrieval of the relocation list
921 // associated with this symbol is deferred until below this point.
922 // New entries may have been added to the relocation list.
923 i = ExternalSymbolRelocations.find(Name);
Andrew Kaylorea395922013-10-01 01:47:35 +0000924 } else {
925 // We found the symbol in our global table. It was probably in a
926 // Module that we loaded previously.
Lang Hames6bfd3982015-01-16 23:13:56 +0000927 const auto &SymInfo = Loc->second;
928 Addr = getSectionLoadAddress(SymInfo.getSectionID()) +
929 SymInfo.getOffset();
Andrew Kaylorea395922013-10-01 01:47:35 +0000930 }
931
932 // FIXME: Implement error handling that doesn't kill the host program!
933 if (!Addr)
934 report_fatal_error("Program used external function '" + Name +
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000935 "' which could not be resolved!");
Andrew Kaylorea395922013-10-01 01:47:35 +0000936
Lang Hames78937c22015-07-04 01:35:26 +0000937 // If Resolver returned UINT64_MAX, the client wants to handle this symbol
938 // manually and we shouldn't resolve its relocations.
939 if (Addr != UINT64_MAX) {
940 DEBUG(dbgs() << "Resolving relocations Name: " << Name << "\t"
941 << format("0x%lx", Addr) << "\n");
942 // This list may have been updated when we called getSymbolAddress, so
943 // don't change this code to get the list earlier.
944 RelocationList &Relocs = i->second;
945 resolveRelocationList(Relocs, Addr);
946 }
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000947 }
Andrew Kaylorea395922013-10-01 01:47:35 +0000948
Andrew Kaylorcfb4a992013-11-11 19:55:10 +0000949 ExternalSymbolRelocations.erase(i);
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000950 }
951}
952
Jim Grosbachf016b0a2011-03-21 22:15:52 +0000953//===----------------------------------------------------------------------===//
954// RuntimeDyld class implementation
Lang Hamesb5c7b1f2014-11-26 16:54:40 +0000955
956uint64_t RuntimeDyld::LoadedObjectInfo::getSectionLoadAddress(
Lang Hames2e88f4f2015-07-28 17:52:11 +0000957 const object::SectionRef &Sec) const {
958
Lang Hames2e88f4f2015-07-28 17:52:11 +0000959 auto I = ObjSecToIDMap.find(Sec);
Sanjoy Das277776a2015-11-23 21:47:41 +0000960 if (I != ObjSecToIDMap.end())
961 return RTDyld.Sections[I->second].getLoadAddress();
Lang Hamesb5c7b1f2014-11-26 16:54:40 +0000962
963 return 0;
964}
965
Lang Hames633fe142015-03-30 03:37:06 +0000966void RuntimeDyld::MemoryManager::anchor() {}
967void RuntimeDyld::SymbolResolver::anchor() {}
968
969RuntimeDyld::RuntimeDyld(RuntimeDyld::MemoryManager &MemMgr,
970 RuntimeDyld::SymbolResolver &Resolver)
971 : MemMgr(MemMgr), Resolver(Resolver) {
Andrew Kaylora342cb92012-11-15 23:50:01 +0000972 // FIXME: There's a potential issue lurking here if a single instance of
973 // RuntimeDyld is used to load multiple objects. The current implementation
974 // associates a single memory manager with a RuntimeDyld instance. Even
975 // though the public class spawns a new 'impl' instance for each load,
976 // they share a single memory manager. This can become a problem when page
977 // permissions are applied.
Craig Topper353eda42014-04-24 06:44:33 +0000978 Dyld = nullptr;
Lang Hames868d4b32014-03-20 21:06:46 +0000979 ProcessAllSections = false;
Lang Hamesf7acddd2014-07-22 22:47:39 +0000980 Checker = nullptr;
Jim Grosbachf016b0a2011-03-21 22:15:52 +0000981}
982
David Blaikie847d2a02014-09-04 18:37:29 +0000983RuntimeDyld::~RuntimeDyld() {}
Jim Grosbachf016b0a2011-03-21 22:15:52 +0000984
David Majnemer1a666e02015-03-07 20:21:27 +0000985static std::unique_ptr<RuntimeDyldCOFF>
Lang Hames633fe142015-03-30 03:37:06 +0000986createRuntimeDyldCOFF(Triple::ArchType Arch, RuntimeDyld::MemoryManager &MM,
987 RuntimeDyld::SymbolResolver &Resolver,
David Majnemer1a666e02015-03-07 20:21:27 +0000988 bool ProcessAllSections, RuntimeDyldCheckerImpl *Checker) {
Lang Hames633fe142015-03-30 03:37:06 +0000989 std::unique_ptr<RuntimeDyldCOFF> Dyld =
990 RuntimeDyldCOFF::create(Arch, MM, Resolver);
David Majnemer1a666e02015-03-07 20:21:27 +0000991 Dyld->setProcessAllSections(ProcessAllSections);
992 Dyld->setRuntimeDyldChecker(Checker);
993 return Dyld;
994}
995
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000996static std::unique_ptr<RuntimeDyldELF>
Lang Hames633fe142015-03-30 03:37:06 +0000997createRuntimeDyldELF(RuntimeDyld::MemoryManager &MM,
998 RuntimeDyld::SymbolResolver &Resolver,
999 bool ProcessAllSections, RuntimeDyldCheckerImpl *Checker) {
1000 std::unique_ptr<RuntimeDyldELF> Dyld(new RuntimeDyldELF(MM, Resolver));
Lang Hames868d4b32014-03-20 21:06:46 +00001001 Dyld->setProcessAllSections(ProcessAllSections);
Lang Hamesf7acddd2014-07-22 22:47:39 +00001002 Dyld->setRuntimeDyldChecker(Checker);
Lang Hames868d4b32014-03-20 21:06:46 +00001003 return Dyld;
1004}
1005
Juergen Ributzka7608dc02014-03-21 20:28:42 +00001006static std::unique_ptr<RuntimeDyldMachO>
Lang Hames633fe142015-03-30 03:37:06 +00001007createRuntimeDyldMachO(Triple::ArchType Arch, RuntimeDyld::MemoryManager &MM,
1008 RuntimeDyld::SymbolResolver &Resolver,
1009 bool ProcessAllSections,
1010 RuntimeDyldCheckerImpl *Checker) {
1011 std::unique_ptr<RuntimeDyldMachO> Dyld =
1012 RuntimeDyldMachO::create(Arch, MM, Resolver);
Lang Hames868d4b32014-03-20 21:06:46 +00001013 Dyld->setProcessAllSections(ProcessAllSections);
Lang Hamesf7acddd2014-07-22 22:47:39 +00001014 Dyld->setRuntimeDyldChecker(Checker);
Lang Hames868d4b32014-03-20 21:06:46 +00001015 return Dyld;
1016}
1017
Lang Hamesb5c7b1f2014-11-26 16:54:40 +00001018std::unique_ptr<RuntimeDyld::LoadedObjectInfo>
1019RuntimeDyld::loadObject(const ObjectFile &Obj) {
1020 if (!Dyld) {
1021 if (Obj.isELF())
Lang Hames633fe142015-03-30 03:37:06 +00001022 Dyld = createRuntimeDyldELF(MemMgr, Resolver, ProcessAllSections, Checker);
Lang Hamesb5c7b1f2014-11-26 16:54:40 +00001023 else if (Obj.isMachO())
Aaron Ballman9fb41142014-11-26 15:27:39 +00001024 Dyld = createRuntimeDyldMachO(
Lang Hames633fe142015-03-30 03:37:06 +00001025 static_cast<Triple::ArchType>(Obj.getArch()), MemMgr, Resolver,
Lang Hamesb5c7b1f2014-11-26 16:54:40 +00001026 ProcessAllSections, Checker);
David Majnemer1a666e02015-03-07 20:21:27 +00001027 else if (Obj.isCOFF())
1028 Dyld = createRuntimeDyldCOFF(
Lang Hames633fe142015-03-30 03:37:06 +00001029 static_cast<Triple::ArchType>(Obj.getArch()), MemMgr, Resolver,
David Majnemer1a666e02015-03-07 20:21:27 +00001030 ProcessAllSections, Checker);
Lang Hamesb5c7b1f2014-11-26 16:54:40 +00001031 else
1032 report_fatal_error("Incompatible object format!");
Aaron Ballman9fb41142014-11-26 15:27:39 +00001033 }
1034
Lang Hamesb5c7b1f2014-11-26 16:54:40 +00001035 if (!Dyld->isCompatibleFile(Obj))
Aaron Ballman9fb41142014-11-26 15:27:39 +00001036 report_fatal_error("Incompatible object format!");
1037
Lang Hamesb0934292016-01-10 23:59:41 +00001038 auto LoadedObjInfo = Dyld->loadObject(Obj);
1039 MemMgr.notifyObjectLoaded(*this, Obj);
1040 return LoadedObjInfo;
Jim Grosbachf016b0a2011-03-21 22:15:52 +00001041}
1042
Lang Hamesb1186032015-03-11 00:43:26 +00001043void *RuntimeDyld::getSymbolLocalAddress(StringRef Name) const {
Andrew Kaylorea395922013-10-01 01:47:35 +00001044 if (!Dyld)
Craig Topper353eda42014-04-24 06:44:33 +00001045 return nullptr;
Lang Hamesb1186032015-03-11 00:43:26 +00001046 return Dyld->getSymbolLocalAddress(Name);
Jim Grosbachf016b0a2011-03-21 22:15:52 +00001047}
1048
Lang Hamesb1186032015-03-11 00:43:26 +00001049RuntimeDyld::SymbolInfo RuntimeDyld::getSymbol(StringRef Name) const {
Andrew Kaylorea395922013-10-01 01:47:35 +00001050 if (!Dyld)
Lang Hamesb1186032015-03-11 00:43:26 +00001051 return nullptr;
1052 return Dyld->getSymbol(Name);
Lang Hames6bfd3982015-01-16 23:13:56 +00001053}
1054
Juergen Ributzka7608dc02014-03-21 20:28:42 +00001055void RuntimeDyld::resolveRelocations() { Dyld->resolveRelocations(); }
Jim Grosbach733d3052011-04-12 21:20:41 +00001056
Juergen Ributzka7608dc02014-03-21 20:28:42 +00001057void RuntimeDyld::reassignSectionAddress(unsigned SectionID, uint64_t Addr) {
Jim Grosbacheff0a402012-01-16 22:26:39 +00001058 Dyld->reassignSectionAddress(SectionID, Addr);
Jim Grosbach733d3052011-04-12 21:20:41 +00001059}
1060
Jim Grosbach6d613972012-09-13 21:50:06 +00001061void RuntimeDyld::mapSectionAddress(const void *LocalAddress,
Jim Grosbach0ddb3a42012-01-16 23:50:55 +00001062 uint64_t TargetAddress) {
1063 Dyld->mapSectionAddress(LocalAddress, TargetAddress);
1064}
1065
Juergen Ributzka6ff29a72014-03-26 18:19:27 +00001066bool RuntimeDyld::hasError() { return Dyld->hasError(); }
1067
Juergen Ributzka7608dc02014-03-21 20:28:42 +00001068StringRef RuntimeDyld::getErrorString() { return Dyld->getErrorString(); }
Jim Grosbach40411cc2011-03-22 18:19:42 +00001069
Lang Hames859d73c2016-01-09 19:50:40 +00001070void RuntimeDyld::finalizeWithMemoryManagerLocking() {
1071 bool MemoryFinalizationLocked = MemMgr.FinalizationLocked;
1072 MemMgr.FinalizationLocked = true;
1073 resolveRelocations();
1074 registerEHFrames();
1075 if (!MemoryFinalizationLocked) {
1076 MemMgr.finalizeMemory();
1077 MemMgr.FinalizationLocked = false;
1078 }
1079}
1080
Andrew Kaylor7bb13442013-10-11 21:25:48 +00001081void RuntimeDyld::registerEHFrames() {
Andrew Kaylorc442a762013-10-16 00:14:21 +00001082 if (Dyld)
1083 Dyld->registerEHFrames();
1084}
1085
1086void RuntimeDyld::deregisterEHFrames() {
1087 if (Dyld)
1088 Dyld->deregisterEHFrames();
Rafael Espindolafa5942b2013-05-05 20:43:10 +00001089}
1090
Jim Grosbachf016b0a2011-03-21 22:15:52 +00001091} // end namespace llvm