blob: 18560c8edb08864ed474d0e9aff08c6138d8a6fa [file] [log] [blame]
Jim Grosbach06594e12012-01-16 23:50:58 +00001//===-- RuntimeDyldImpl.h - Run-time dynamic linker for MC-JIT --*- C++ -*-===//
Danil Malyshev72510f22011-07-13 07:57:58 +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// Interface for the implementations of runtime dynamic linker facilities.
11//
12//===----------------------------------------------------------------------===//
13
Benjamin Kramera7c40ef2014-08-13 16:26:38 +000014#ifndef LLVM_LIB_EXECUTIONENGINE_RUNTIMEDYLD_RUNTIMEDYLDIMPL_H
15#define LLVM_LIB_EXECUTIONENGINE_RUNTIMEDYLD_RUNTIMEDYLDIMPL_H
Danil Malyshev72510f22011-07-13 07:57:58 +000016
Danil Malyshev72510f22011-07-13 07:57:58 +000017#include "llvm/ADT/SmallVector.h"
Eli Bendersky32d54882012-04-30 10:06:27 +000018#include "llvm/ADT/StringMap.h"
19#include "llvm/ADT/Triple.h"
Lang Hames633fe142015-03-30 03:37:06 +000020#include "llvm/ExecutionEngine/RTDyldMemoryManager.h"
Chandler Carruth802d7552012-12-04 07:12:27 +000021#include "llvm/ExecutionEngine/RuntimeDyld.h"
Lang Hamese1c11382014-06-27 20:20:57 +000022#include "llvm/ExecutionEngine/RuntimeDyldChecker.h"
Eli Bendersky32d54882012-04-30 10:06:27 +000023#include "llvm/Object/ObjectFile.h"
Danil Malyshev72510f22011-07-13 07:57:58 +000024#include "llvm/Support/Debug.h"
25#include "llvm/Support/ErrorHandling.h"
Danil Malyshev70d22cc2012-03-30 16:45:19 +000026#include "llvm/Support/Format.h"
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +000027#include "llvm/Support/Host.h"
Andrew Kaylor4fba0492013-10-21 17:42:06 +000028#include "llvm/Support/Mutex.h"
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +000029#include "llvm/Support/SwapByteOrder.h"
Eli Bendersky32d54882012-04-30 10:06:27 +000030#include <map>
Keno Fischereb59d462015-12-03 21:27:59 +000031#include <unordered_map>
Rafael Espindolaa6e9c3e2014-06-12 17:38:55 +000032#include <system_error>
Danil Malyshev72510f22011-07-13 07:57:58 +000033
34using namespace llvm;
Danil Malyshev70d22cc2012-03-30 16:45:19 +000035using namespace llvm::object;
Danil Malyshev72510f22011-07-13 07:57:58 +000036
37namespace llvm {
Danil Malyshev70d22cc2012-03-30 16:45:19 +000038
David Majnemer1a666e02015-03-07 20:21:27 +000039 // Helper for extensive error checking in debug builds.
David Majnemerb654b552015-03-07 20:56:50 +000040inline std::error_code Check(std::error_code Err) {
41 if (Err) {
42 report_fatal_error(Err.message());
David Majnemer1a666e02015-03-07 20:21:27 +000043 }
David Majnemerb654b552015-03-07 20:56:50 +000044 return Err;
45}
Kevin Enderby81e8b7d2016-04-20 21:24:34 +000046inline void Check(llvm::Error Err) {
47 if (Err) {
48 std::string Buf;
49 raw_string_ostream OS(Buf);
50 logAllUnhandledErrors(std::move(Err), OS, "");
51 OS.flush();
52 report_fatal_error(Buf);
53 }
54}
David Majnemer1a666e02015-03-07 20:21:27 +000055
Eli Bendersky32d54882012-04-30 10:06:27 +000056class Twine;
57
Eli Bendersky32d54882012-04-30 10:06:27 +000058/// SectionEntry - represents a section emitted into memory by the dynamic
59/// linker.
Danil Malyshev70d22cc2012-03-30 16:45:19 +000060class SectionEntry {
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +000061 /// Name - section name.
Lang Hamesccc588e2015-04-14 17:13:10 +000062 std::string Name;
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +000063
Eli Bendersky32d54882012-04-30 10:06:27 +000064 /// Address - address in the linker's memory where the section resides.
65 uint8_t *Address;
66
Rafael Espindolafa5942b2013-05-05 20:43:10 +000067 /// Size - section size. Doesn't include the stubs.
Eli Bendersky32d54882012-04-30 10:06:27 +000068 size_t Size;
69
70 /// LoadAddress - the address of the section in the target process's memory.
71 /// Used for situations in which JIT-ed code is being executed in the address
72 /// space of a separate process. If the code executes in the same address
73 /// space where it was JIT-ed, this just equals Address.
74 uint64_t LoadAddress;
75
76 /// StubOffset - used for architectures with stub functions for far
77 /// relocations (like ARM).
78 uintptr_t StubOffset;
79
Sanjoy Das80825922015-11-23 21:47:46 +000080 /// The total amount of space allocated for this section. This includes the
81 /// section size and the maximum amount of space that the stubs can occupy.
82 size_t AllocationSize;
83
Eli Bendersky32d54882012-04-30 10:06:27 +000084 /// ObjAddress - address of the section in the in-memory object file. Used
85 /// for calculating relocations in some object formats (like MachO).
86 uintptr_t ObjAddress;
87
Sanjoy Das277776a2015-11-23 21:47:41 +000088public:
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +000089 SectionEntry(StringRef name, uint8_t *address, size_t size,
Sanjoy Das80825922015-11-23 21:47:46 +000090 size_t allocationSize, uintptr_t objAddress)
Juergen Ributzka7608dc02014-03-21 20:28:42 +000091 : Name(name), Address(address), Size(size),
Lang Hames0717c3de2014-08-27 17:48:07 +000092 LoadAddress(reinterpret_cast<uintptr_t>(address)), StubOffset(size),
Sanjoy Das5abfbb92015-11-23 22:59:36 +000093 AllocationSize(allocationSize), ObjAddress(objAddress) {
94 // AllocationSize is used only in asserts, prevent an "unused private field"
95 // warning:
96 (void)AllocationSize;
97 }
Sanjoy Das277776a2015-11-23 21:47:41 +000098
99 StringRef getName() const { return Name; }
100
101 uint8_t *getAddress() const { return Address; }
102
103 /// \brief Return the address of this section with an offset.
104 uint8_t *getAddressWithOffset(unsigned OffsetBytes) const {
Sanjoy Das80825922015-11-23 21:47:46 +0000105 assert(OffsetBytes <= AllocationSize && "Offset out of bounds!");
Sanjoy Das277776a2015-11-23 21:47:41 +0000106 return Address + OffsetBytes;
107 }
108
109 size_t getSize() const { return Size; }
110
111 uint64_t getLoadAddress() const { return LoadAddress; }
112 void setLoadAddress(uint64_t LA) { LoadAddress = LA; }
113
114 /// \brief Return the load address of this section with an offset.
115 uint64_t getLoadAddressWithOffset(unsigned OffsetBytes) const {
Sanjoy Das80825922015-11-23 21:47:46 +0000116 assert(OffsetBytes <= AllocationSize && "Offset out of bounds!");
Sanjoy Das277776a2015-11-23 21:47:41 +0000117 return LoadAddress + OffsetBytes;
118 }
119
120 uintptr_t getStubOffset() const { return StubOffset; }
121
Sanjoy Das80825922015-11-23 21:47:46 +0000122 void advanceStubOffset(unsigned StubSize) {
123 StubOffset += StubSize;
124 assert(StubOffset <= AllocationSize && "Not enough space allocated!");
125 }
Sanjoy Das277776a2015-11-23 21:47:41 +0000126
127 uintptr_t getObjAddress() const { return ObjAddress; }
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000128};
129
Eli Bendersky32d54882012-04-30 10:06:27 +0000130/// RelocationEntry - used to represent relocations internally in the dynamic
131/// linker.
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000132class RelocationEntry {
133public:
Eli Bendersky32d54882012-04-30 10:06:27 +0000134 /// SectionID - the section this relocation points to.
135 unsigned SectionID;
136
137 /// Offset - offset into the section.
Andrew Kaylor4612fed2013-08-19 23:27:43 +0000138 uint64_t Offset;
Eli Bendersky32d54882012-04-30 10:06:27 +0000139
140 /// RelType - relocation type.
141 uint32_t RelType;
142
143 /// Addend - the relocation addend encoded in the instruction itself. Also
144 /// used to make a relocation section relative instead of symbol relative.
Andrew Kaylor4612fed2013-08-19 23:27:43 +0000145 int64_t Addend;
146
Lang Hames36072da2014-05-12 21:39:59 +0000147 struct SectionPair {
148 uint32_t SectionA;
149 uint32_t SectionB;
150 };
151
Andrew Kaylor4612fed2013-08-19 23:27:43 +0000152 /// SymOffset - Section offset of the relocation entry's symbol (used for GOT
153 /// lookup).
Lang Hames36072da2014-05-12 21:39:59 +0000154 union {
155 uint64_t SymOffset;
156 SectionPair Sections;
157 };
Eli Bendersky32d54882012-04-30 10:06:27 +0000158
Rafael Espindolaf1f1c622013-04-29 17:24:34 +0000159 /// True if this is a PCRel relocation (MachO specific).
160 bool IsPCRel;
161
162 /// The size of this relocation (MachO specific).
163 unsigned Size;
164
Eli Bendersky32d54882012-04-30 10:06:27 +0000165 RelocationEntry(unsigned id, uint64_t offset, uint32_t type, int64_t addend)
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000166 : SectionID(id), Offset(offset), RelType(type), Addend(addend),
167 SymOffset(0), IsPCRel(false), Size(0) {}
Andrew Kaylor4612fed2013-08-19 23:27:43 +0000168
169 RelocationEntry(unsigned id, uint64_t offset, uint32_t type, int64_t addend,
170 uint64_t symoffset)
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000171 : SectionID(id), Offset(offset), RelType(type), Addend(addend),
172 SymOffset(symoffset), IsPCRel(false), Size(0) {}
Rafael Espindolaf1f1c622013-04-29 17:24:34 +0000173
174 RelocationEntry(unsigned id, uint64_t offset, uint32_t type, int64_t addend,
175 bool IsPCRel, unsigned Size)
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000176 : SectionID(id), Offset(offset), RelType(type), Addend(addend),
177 SymOffset(0), IsPCRel(IsPCRel), Size(Size) {}
Lang Hames36072da2014-05-12 21:39:59 +0000178
179 RelocationEntry(unsigned id, uint64_t offset, uint32_t type, int64_t addend,
180 unsigned SectionA, uint64_t SectionAOffset, unsigned SectionB,
181 uint64_t SectionBOffset, bool IsPCRel, unsigned Size)
182 : SectionID(id), Offset(offset), RelType(type),
183 Addend(SectionAOffset - SectionBOffset + addend), IsPCRel(IsPCRel),
184 Size(Size) {
185 Sections.SectionA = SectionA;
186 Sections.SectionB = SectionB;
187 }
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000188};
189
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000190class RelocationValueRef {
191public:
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000192 unsigned SectionID;
193 uint64_t Offset;
194 int64_t Addend;
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000195 const char *SymbolName;
Craig Toppere73658d2014-04-28 04:05:08 +0000196 RelocationValueRef() : SectionID(0), Offset(0), Addend(0),
197 SymbolName(nullptr) {}
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000198
199 inline bool operator==(const RelocationValueRef &Other) const {
Benjamin Kramer5a712502013-08-20 09:27:31 +0000200 return SectionID == Other.SectionID && Offset == Other.Offset &&
201 Addend == Other.Addend && SymbolName == Other.SymbolName;
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000202 }
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000203 inline bool operator<(const RelocationValueRef &Other) const {
Benjamin Kramer5a712502013-08-20 09:27:31 +0000204 if (SectionID != Other.SectionID)
205 return SectionID < Other.SectionID;
206 if (Offset != Other.Offset)
207 return Offset < Other.Offset;
208 if (Addend != Other.Addend)
209 return Addend < Other.Addend;
210 return SymbolName < Other.SymbolName;
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000211 }
212};
213
Lang Hamesb1186032015-03-11 00:43:26 +0000214/// @brief Symbol info for RuntimeDyld.
215class SymbolTableEntry : public JITSymbolBase {
Lang Hames6bfd3982015-01-16 23:13:56 +0000216public:
Lang Hamesb1186032015-03-11 00:43:26 +0000217 SymbolTableEntry()
218 : JITSymbolBase(JITSymbolFlags::None), Offset(0), SectionID(0) {}
Lang Hames6bfd3982015-01-16 23:13:56 +0000219
Lang Hamesb1186032015-03-11 00:43:26 +0000220 SymbolTableEntry(unsigned SectionID, uint64_t Offset, JITSymbolFlags Flags)
221 : JITSymbolBase(Flags), Offset(Offset), SectionID(SectionID) {}
Lang Hames6bfd3982015-01-16 23:13:56 +0000222
223 unsigned getSectionID() const { return SectionID; }
224 uint64_t getOffset() const { return Offset; }
Lang Hames6bfd3982015-01-16 23:13:56 +0000225
226private:
227 uint64_t Offset;
Lang Hamesb1186032015-03-11 00:43:26 +0000228 unsigned SectionID;
Lang Hames6bfd3982015-01-16 23:13:56 +0000229};
230
Lang Hamesb1186032015-03-11 00:43:26 +0000231typedef StringMap<SymbolTableEntry> RTDyldSymbolTable;
Lang Hames6bfd3982015-01-16 23:13:56 +0000232
Danil Malyshev72510f22011-07-13 07:57:58 +0000233class RuntimeDyldImpl {
Lang Hamesb5c7b1f2014-11-26 16:54:40 +0000234 friend class RuntimeDyld::LoadedObjectInfo;
Lang Hamesf7acddd2014-07-22 22:47:39 +0000235 friend class RuntimeDyldCheckerImpl;
Danil Malyshev72510f22011-07-13 07:57:58 +0000236protected:
Lang Hamesa32d71b2015-10-18 01:41:37 +0000237 static const unsigned AbsoluteSymbolSection = ~0U;
238
Danil Malyshev72510f22011-07-13 07:57:58 +0000239 // The MemoryManager to load objects into.
Lang Hames633fe142015-03-30 03:37:06 +0000240 RuntimeDyld::MemoryManager &MemMgr;
241
242 // The symbol resolver to use for external symbols.
243 RuntimeDyld::SymbolResolver &Resolver;
Danil Malyshev72510f22011-07-13 07:57:58 +0000244
Lang Hamesf7acddd2014-07-22 22:47:39 +0000245 // Attached RuntimeDyldChecker instance. Null if no instance attached.
246 RuntimeDyldCheckerImpl *Checker;
247
Eli Bendersky32d54882012-04-30 10:06:27 +0000248 // A list of all sections emitted by the dynamic linker. These sections are
249 // referenced in the code by means of their index in this list - SectionID.
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000250 typedef SmallVector<SectionEntry, 64> SectionList;
251 SectionList Sections;
Danil Malyshev72510f22011-07-13 07:57:58 +0000252
Andrew Kaylor7bb13442013-10-11 21:25:48 +0000253 typedef unsigned SID; // Type for SectionIDs
Keno Fischerc780e8e2015-05-21 21:24:32 +0000254#define RTDYLD_INVALID_SECTION_ID ((RuntimeDyldImpl::SID)(-1))
Andrew Kaylor7bb13442013-10-11 21:25:48 +0000255
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000256 // Keep a map of sections from object file to the SectionID which
257 // references it.
258 typedef std::map<SectionRef, unsigned> ObjSectionToIDMap;
Jim Grosbach0ddb3a42012-01-16 23:50:55 +0000259
Lang Hames6bfd3982015-01-16 23:13:56 +0000260 // A global symbol table for symbols from all loaded modules.
261 RTDyldSymbolTable GlobalSymbolTable;
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000262
Tim Northover94bc73d2012-10-29 10:47:04 +0000263 // Keep a map of common symbols to their info pairs
Lang Hames29968952015-01-17 00:55:05 +0000264 typedef std::vector<SymbolRef> CommonSymbolList;
Preston Gurd2138ef62012-04-12 20:13:57 +0000265
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000266 // For each symbol, keep a list of relocations based on it. Anytime
267 // its address is reassigned (the JIT re-compiled the function, e.g.),
268 // the relocations get re-resolved.
269 // The symbol (or section) the relocation is sourced from is the Key
270 // in the relocation list where it's stored.
271 typedef SmallVector<RelocationEntry, 64> RelocationList;
272 // Relocations to sections already loaded. Indexed by SectionID which is the
Benjamin Kramerbde91762012-06-02 10:20:22 +0000273 // source of the address. The target where the address will be written is
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000274 // SectionID/Offset in the relocation itself.
Keno Fischereb59d462015-12-03 21:27:59 +0000275 std::unordered_map<unsigned, RelocationList> Relocations;
Eli Benderskyb92e1cf2012-04-30 12:15:58 +0000276
277 // Relocations to external symbols that are not yet resolved. Symbols are
278 // external when they aren't found in the global symbol table of all loaded
279 // modules. This map is indexed by symbol name.
280 StringMap<RelocationList> ExternalSymbolRelocations;
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000281
Lang Hamesf7acddd2014-07-22 22:47:39 +0000282
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000283 typedef std::map<RelocationValueRef, uintptr_t> StubMap;
284
285 Triple::ArchType Arch;
Andrew Kaylor33c5b1b2013-10-15 20:44:55 +0000286 bool IsTargetLittleEndian;
Petar Jovanovic97202832015-05-28 13:48:41 +0000287 bool IsMipsO32ABI;
288 bool IsMipsN64ABI;
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000289
Lang Hames868d4b32014-03-20 21:06:46 +0000290 // True if all sections should be passed to the memory manager, false if only
291 // sections containing relocations should be. Defaults to 'false'.
292 bool ProcessAllSections;
293
Andrew Kaylor4fba0492013-10-21 17:42:06 +0000294 // This mutex prevents simultaneously loading objects from two different
295 // threads. This keeps us from having to protect individual data structures
296 // and guarantees that section allocation requests to the memory manager
297 // won't be interleaved between modules. It is also used in mapSectionAddress
298 // and resolveRelocations to protect write access to internal data structures.
299 //
300 // loadObject may be called on the same thread during the handling of of
301 // processRelocations, and that's OK. The handling of the relocation lists
302 // is written in such a way as to work correctly if new elements are added to
303 // the end of the list while the list is being processed.
304 sys::Mutex lock;
305
Andrew Kaylor2ba21c52013-10-15 21:32:56 +0000306 virtual unsigned getMaxStubSize() = 0;
307 virtual unsigned getStubAlignment() = 0;
Richard Sandifordca044082013-05-03 14:15:35 +0000308
Danil Malyshev72510f22011-07-13 07:57:58 +0000309 bool HasError;
310 std::string ErrorStr;
311
312 // Set the error state and record an error string.
313 bool Error(const Twine &Msg) {
314 ErrorStr = Msg.str();
315 HasError = true;
316 return true;
317 }
318
Lang Hames3e930a32014-09-05 18:00:16 +0000319 uint64_t getSectionLoadAddress(unsigned SectionID) const {
Sanjoy Das277776a2015-11-23 21:47:41 +0000320 return Sections[SectionID].getLoadAddress();
Jim Grosbachdc1123f2012-09-05 16:50:40 +0000321 }
322
Lang Hames3e930a32014-09-05 18:00:16 +0000323 uint8_t *getSectionAddress(unsigned SectionID) const {
Sanjoy Das277776a2015-11-23 21:47:41 +0000324 return Sections[SectionID].getAddress();
Jim Grosbacheff0a402012-01-16 22:26:39 +0000325 }
Danil Malyshev72510f22011-07-13 07:57:58 +0000326
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000327 void writeInt16BE(uint8_t *Addr, uint16_t Value) {
Andrew Kaylor33c5b1b2013-10-15 20:44:55 +0000328 if (IsTargetLittleEndian)
Artyom Skrobov9aea8432014-06-14 13:18:07 +0000329 sys::swapByteOrder(Value);
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000330 *Addr = (Value >> 8) & 0xFF;
331 *(Addr + 1) = Value & 0xFF;
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000332 }
333
334 void writeInt32BE(uint8_t *Addr, uint32_t Value) {
Andrew Kaylor33c5b1b2013-10-15 20:44:55 +0000335 if (IsTargetLittleEndian)
Artyom Skrobov9aea8432014-06-14 13:18:07 +0000336 sys::swapByteOrder(Value);
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000337 *Addr = (Value >> 24) & 0xFF;
338 *(Addr + 1) = (Value >> 16) & 0xFF;
339 *(Addr + 2) = (Value >> 8) & 0xFF;
340 *(Addr + 3) = Value & 0xFF;
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000341 }
342
343 void writeInt64BE(uint8_t *Addr, uint64_t Value) {
Andrew Kaylor33c5b1b2013-10-15 20:44:55 +0000344 if (IsTargetLittleEndian)
Artyom Skrobov9aea8432014-06-14 13:18:07 +0000345 sys::swapByteOrder(Value);
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000346 *Addr = (Value >> 56) & 0xFF;
347 *(Addr + 1) = (Value >> 48) & 0xFF;
348 *(Addr + 2) = (Value >> 40) & 0xFF;
349 *(Addr + 3) = (Value >> 32) & 0xFF;
350 *(Addr + 4) = (Value >> 24) & 0xFF;
351 *(Addr + 5) = (Value >> 16) & 0xFF;
352 *(Addr + 6) = (Value >> 8) & 0xFF;
353 *(Addr + 7) = Value & 0xFF;
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000354 }
355
Petar Jovanovic97202832015-05-28 13:48:41 +0000356 virtual void setMipsABI(const ObjectFile &Obj) {
357 IsMipsO32ABI = false;
358 IsMipsN64ABI = false;
359 }
360
Lang Hamese1287c02014-08-29 23:17:47 +0000361 /// Endian-aware read Read the least significant Size bytes from Src.
362 uint64_t readBytesUnaligned(uint8_t *Src, unsigned Size) const;
363
364 /// Endian-aware write. Write the least significant Size bytes from Value to
365 /// Dst.
366 void writeBytesUnaligned(uint64_t Value, uint8_t *Dst, unsigned Size) const;
367
Eli Bendersky667b8792012-05-01 10:41:12 +0000368 /// \brief Given the common symbols discovered in the object file, emit a
369 /// new section for them and update the symbol mappings in the object and
370 /// symbol table.
Lang Hames29968952015-01-17 00:55:05 +0000371 void emitCommonSymbols(const ObjectFile &Obj, CommonSymbolList &CommonSymbols);
Preston Gurd2138ef62012-04-12 20:13:57 +0000372
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000373 /// \brief Emits section data from the object file to the MemoryManager.
374 /// \param IsCode if it's true then allocateCodeSection() will be
Eli Bendersky0e2ac5b2012-04-29 12:40:47 +0000375 /// used for emits, else allocateDataSection() will be used.
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000376 /// \return SectionID.
Lang Hamesb5c7b1f2014-11-26 16:54:40 +0000377 unsigned emitSection(const ObjectFile &Obj, const SectionRef &Section,
Preston Gurdcc31af92012-04-16 22:12:58 +0000378 bool IsCode);
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000379
380 /// \brief Find Section in LocalSections. If the secton is not found - emit
381 /// it and store in LocalSections.
382 /// \param IsCode if it's true then allocateCodeSection() will be
383 /// used for emmits, else allocateDataSection() will be used.
384 /// \return SectionID.
Lang Hamesb5c7b1f2014-11-26 16:54:40 +0000385 unsigned findOrEmitSection(const ObjectFile &Obj, const SectionRef &Section,
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000386 bool IsCode, ObjSectionToIDMap &LocalSections);
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000387
Eli Bendersky667b8792012-05-01 10:41:12 +0000388 // \brief Add a relocation entry that uses the given section.
389 void addRelocationForSection(const RelocationEntry &RE, unsigned SectionID);
390
391 // \brief Add a relocation entry that uses the given symbol. This symbol may
392 // be found in the global symbol table, or it may be external.
393 void addRelocationForSymbol(const RelocationEntry &RE, StringRef SymbolName);
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000394
395 /// \brief Emits long jump instruction to Addr.
396 /// \return Pointer to the memory area for emitting target address.
Ulrich Weigand752b5c92014-07-20 23:53:14 +0000397 uint8_t *createStubFunction(uint8_t *Addr, unsigned AbiVariant = 0);
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000398
399 /// \brief Resolves relocations from Relocs list with address from Value.
400 void resolveRelocationList(const RelocationList &Relocs, uint64_t Value);
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000401
402 /// \brief A object file specific relocation resolver
Rafael Espindolab39478e2013-04-29 19:33:51 +0000403 /// \param RE The relocation to be resolved
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000404 /// \param Value Target symbol address to apply the relocation action
Rafael Espindolaf1f1c622013-04-29 17:24:34 +0000405 virtual void resolveRelocation(const RelocationEntry &RE, uint64_t Value) = 0;
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000406
Juergen Ributzka838282e2014-03-21 20:38:46 +0000407 /// \brief Parses one or more object file relocations (some object files use
408 /// relocation pairs) and stores it to Relocations or SymbolRelocations
409 /// (this depends on the object file type).
410 /// \return Iterator to the next relocation that needs to be parsed.
Juergen Ributzka046709f2014-03-21 07:26:41 +0000411 virtual relocation_iterator
412 processRelocationRef(unsigned SectionID, relocation_iterator RelI,
Lang Hamesb5c7b1f2014-11-26 16:54:40 +0000413 const ObjectFile &Obj, ObjSectionToIDMap &ObjSectionToID,
Lang Hamesa5cd9502014-11-27 05:40:13 +0000414 StubMap &Stubs) = 0;
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000415
Eli Benderskyb92e1cf2012-04-30 12:15:58 +0000416 /// \brief Resolve relocations to external symbols.
417 void resolveExternalSymbols();
Andrew Kaylor4612fed2013-08-19 23:27:43 +0000418
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000419 // \brief Compute an upper bound of the memory that is required to load all
420 // sections
Lang Hamesb2b7a3c2016-01-10 18:51:50 +0000421 void computeTotalAllocSize(const ObjectFile &Obj,
422 uint64_t &CodeSize, uint32_t &CodeAlign,
423 uint64_t &RODataSize, uint32_t &RODataAlign,
424 uint64_t &RWDataSize, uint32_t &RWDataAlign);
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000425
Lang Hames937ec542014-02-12 21:30:07 +0000426 // \brief Compute the stub buffer size required for a section
Lang Hamesb5c7b1f2014-11-26 16:54:40 +0000427 unsigned computeSectionStubBufSize(const ObjectFile &Obj,
Juergen Ributzka046709f2014-03-21 07:26:41 +0000428 const SectionRef &Section);
Lang Hames937ec542014-02-12 21:30:07 +0000429
Lang Hamesb5c7b1f2014-11-26 16:54:40 +0000430 // \brief Implementation of the generic part of the loadObject algorithm.
Lang Hames2e88f4f2015-07-28 17:52:11 +0000431 ObjSectionToIDMap loadObjectImpl(const object::ObjectFile &Obj);
Lang Hamesb5c7b1f2014-11-26 16:54:40 +0000432
Sanjoy Dasd5658b02015-11-23 21:47:51 +0000433 // \brief Return true if the relocation R may require allocating a stub.
434 virtual bool relocationNeedsStub(const RelocationRef &R) const {
435 return true; // Conservative answer
436 }
437
Danil Malyshev72510f22011-07-13 07:57:58 +0000438public:
Lang Hames633fe142015-03-30 03:37:06 +0000439 RuntimeDyldImpl(RuntimeDyld::MemoryManager &MemMgr,
440 RuntimeDyld::SymbolResolver &Resolver)
441 : MemMgr(MemMgr), Resolver(Resolver), Checker(nullptr),
442 ProcessAllSections(false), HasError(false) {
Lang Hamese1c11382014-06-27 20:20:57 +0000443 }
Danil Malyshev72510f22011-07-13 07:57:58 +0000444
445 virtual ~RuntimeDyldImpl();
446
Lang Hames868d4b32014-03-20 21:06:46 +0000447 void setProcessAllSections(bool ProcessAllSections) {
448 this->ProcessAllSections = ProcessAllSections;
449 }
450
Lang Hamesf7acddd2014-07-22 22:47:39 +0000451 void setRuntimeDyldChecker(RuntimeDyldCheckerImpl *Checker) {
452 this->Checker = Checker;
453 }
454
Lang Hamesb5c7b1f2014-11-26 16:54:40 +0000455 virtual std::unique_ptr<RuntimeDyld::LoadedObjectInfo>
456 loadObject(const object::ObjectFile &Obj) = 0;
Danil Malyshev72510f22011-07-13 07:57:58 +0000457
Lang Hamesb1186032015-03-11 00:43:26 +0000458 uint8_t* getSymbolLocalAddress(StringRef Name) const {
Danil Malyshev72510f22011-07-13 07:57:58 +0000459 // FIXME: Just look up as a function for now. Overly simple of course.
460 // Work in progress.
Lang Hames6bfd3982015-01-16 23:13:56 +0000461 RTDyldSymbolTable::const_iterator pos = GlobalSymbolTable.find(Name);
Yaron Kerenc9802882013-10-19 09:04:26 +0000462 if (pos == GlobalSymbolTable.end())
Craig Toppere73658d2014-04-28 04:05:08 +0000463 return nullptr;
Lang Hames6bfd3982015-01-16 23:13:56 +0000464 const auto &SymInfo = pos->second;
Lang Hamesa32d71b2015-10-18 01:41:37 +0000465 // Absolute symbols do not have a local address.
466 if (SymInfo.getSectionID() == AbsoluteSymbolSection)
467 return nullptr;
Lang Hames6bfd3982015-01-16 23:13:56 +0000468 return getSectionAddress(SymInfo.getSectionID()) + SymInfo.getOffset();
Danil Malyshev72510f22011-07-13 07:57:58 +0000469 }
470
Lang Hamesb1186032015-03-11 00:43:26 +0000471 RuntimeDyld::SymbolInfo getSymbol(StringRef Name) const {
Jim Grosbachdc1123f2012-09-05 16:50:40 +0000472 // FIXME: Just look up as a function for now. Overly simple of course.
473 // Work in progress.
Lang Hames6bfd3982015-01-16 23:13:56 +0000474 RTDyldSymbolTable::const_iterator pos = GlobalSymbolTable.find(Name);
Yaron Kerenc9802882013-10-19 09:04:26 +0000475 if (pos == GlobalSymbolTable.end())
Lang Hamesb1186032015-03-11 00:43:26 +0000476 return nullptr;
477 const auto &SymEntry = pos->second;
Lang Hamesa32d71b2015-10-18 01:41:37 +0000478 uint64_t SectionAddr = 0;
479 if (SymEntry.getSectionID() != AbsoluteSymbolSection)
480 SectionAddr = getSectionLoadAddress(SymEntry.getSectionID());
481 uint64_t TargetAddr = SectionAddr + SymEntry.getOffset();
Lang Hamesb1186032015-03-11 00:43:26 +0000482 return RuntimeDyld::SymbolInfo(TargetAddr, SymEntry.getFlags());
Jim Grosbachdc1123f2012-09-05 16:50:40 +0000483 }
484
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000485 void resolveRelocations();
Danil Malyshev72510f22011-07-13 07:57:58 +0000486
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000487 void reassignSectionAddress(unsigned SectionID, uint64_t Addr);
Danil Malyshev72510f22011-07-13 07:57:58 +0000488
Jim Grosbach6d613972012-09-13 21:50:06 +0000489 void mapSectionAddress(const void *LocalAddress, uint64_t TargetAddress);
Jim Grosbach0ddb3a42012-01-16 23:50:55 +0000490
Danil Malyshev72510f22011-07-13 07:57:58 +0000491 // Is the linker in an error state?
492 bool hasError() { return HasError; }
493
494 // Mark the error condition as handled and continue.
495 void clearError() { HasError = false; }
496
497 // Get the error message.
498 StringRef getErrorString() { return ErrorStr; }
499
Lang Hamesb5c7b1f2014-11-26 16:54:40 +0000500 virtual bool isCompatibleFile(const ObjectFile &Obj) const = 0;
Rafael Espindolafa5942b2013-05-05 20:43:10 +0000501
Andrew Kaylor7bb13442013-10-11 21:25:48 +0000502 virtual void registerEHFrames();
Andrew Kaylor4612fed2013-08-19 23:27:43 +0000503
Andrew Kaylorc442a762013-10-16 00:14:21 +0000504 virtual void deregisterEHFrames();
505
Lang Hamesb5c7b1f2014-11-26 16:54:40 +0000506 virtual void finalizeLoad(const ObjectFile &ObjImg,
507 ObjSectionToIDMap &SectionMap) {}
Danil Malyshev72510f22011-07-13 07:57:58 +0000508};
509
Danil Malyshev72510f22011-07-13 07:57:58 +0000510} // end namespace llvm
511
Danil Malyshev72510f22011-07-13 07:57:58 +0000512#endif