blob: 0628fa71802c55bf0028573b2e02a22c44ee4611 [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
14#ifndef LLVM_RUNTIME_DYLD_IMPL_H
15#define LLVM_RUNTIME_DYLD_IMPL_H
16
Jim Grosbach0ddb3a42012-01-16 23:50:55 +000017#include "llvm/ADT/DenseMap.h"
Danil Malyshev72510f22011-07-13 07:57:58 +000018#include "llvm/ADT/SmallVector.h"
Eli Bendersky32d54882012-04-30 10:06:27 +000019#include "llvm/ADT/StringMap.h"
20#include "llvm/ADT/Triple.h"
Chandler Carruth802d7552012-12-04 07:12:27 +000021#include "llvm/ExecutionEngine/ObjectImage.h"
22#include "llvm/ExecutionEngine/RuntimeDyld.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"
28#include "llvm/Support/SwapByteOrder.h"
Eli Bendersky32d54882012-04-30 10:06:27 +000029#include "llvm/Support/raw_ostream.h"
30#include "llvm/Support/system_error.h"
31#include <map>
Danil Malyshev72510f22011-07-13 07:57:58 +000032
33using namespace llvm;
Danil Malyshev70d22cc2012-03-30 16:45:19 +000034using namespace llvm::object;
Danil Malyshev72510f22011-07-13 07:57:58 +000035
36namespace llvm {
Danil Malyshev70d22cc2012-03-30 16:45:19 +000037
Andrew Kayloradc70562012-10-02 21:18:39 +000038class ObjectBuffer;
Eli Bendersky32d54882012-04-30 10:06:27 +000039class Twine;
40
41
42/// SectionEntry - represents a section emitted into memory by the dynamic
43/// linker.
Danil Malyshev70d22cc2012-03-30 16:45:19 +000044class SectionEntry {
45public:
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +000046 /// Name - section name.
47 StringRef Name;
48
Eli Bendersky32d54882012-04-30 10:06:27 +000049 /// Address - address in the linker's memory where the section resides.
50 uint8_t *Address;
51
Rafael Espindolafa5942b2013-05-05 20:43:10 +000052 /// Size - section size. Doesn't include the stubs.
Eli Bendersky32d54882012-04-30 10:06:27 +000053 size_t Size;
54
55 /// LoadAddress - the address of the section in the target process's memory.
56 /// Used for situations in which JIT-ed code is being executed in the address
57 /// space of a separate process. If the code executes in the same address
58 /// space where it was JIT-ed, this just equals Address.
59 uint64_t LoadAddress;
60
61 /// StubOffset - used for architectures with stub functions for far
62 /// relocations (like ARM).
63 uintptr_t StubOffset;
64
65 /// ObjAddress - address of the section in the in-memory object file. Used
66 /// for calculating relocations in some object formats (like MachO).
67 uintptr_t ObjAddress;
68
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +000069 SectionEntry(StringRef name, uint8_t *address, size_t size,
Rafael Espindolafa5942b2013-05-05 20:43:10 +000070 uintptr_t objAddress)
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +000071 : Name(name), Address(address), Size(size), LoadAddress((uintptr_t)address),
Rafael Espindolafa5942b2013-05-05 20:43:10 +000072 StubOffset(size), ObjAddress(objAddress) {}
Danil Malyshev70d22cc2012-03-30 16:45:19 +000073};
74
Eli Bendersky32d54882012-04-30 10:06:27 +000075/// RelocationEntry - used to represent relocations internally in the dynamic
76/// linker.
Danil Malyshev70d22cc2012-03-30 16:45:19 +000077class RelocationEntry {
78public:
Eli Bendersky32d54882012-04-30 10:06:27 +000079 /// SectionID - the section this relocation points to.
80 unsigned SectionID;
81
82 /// Offset - offset into the section.
Andrew Kaylor4612fed2013-08-19 23:27:43 +000083 uint64_t Offset;
Eli Bendersky32d54882012-04-30 10:06:27 +000084
85 /// RelType - relocation type.
86 uint32_t RelType;
87
88 /// Addend - the relocation addend encoded in the instruction itself. Also
89 /// used to make a relocation section relative instead of symbol relative.
Andrew Kaylor4612fed2013-08-19 23:27:43 +000090 int64_t Addend;
91
92 /// SymOffset - Section offset of the relocation entry's symbol (used for GOT
93 /// lookup).
94 uint64_t SymOffset;
Eli Bendersky32d54882012-04-30 10:06:27 +000095
Rafael Espindolaf1f1c622013-04-29 17:24:34 +000096 /// True if this is a PCRel relocation (MachO specific).
97 bool IsPCRel;
98
99 /// The size of this relocation (MachO specific).
100 unsigned Size;
101
Eli Bendersky32d54882012-04-30 10:06:27 +0000102 RelocationEntry(unsigned id, uint64_t offset, uint32_t type, int64_t addend)
Rafael Espindolaf1f1c622013-04-29 17:24:34 +0000103 : SectionID(id), Offset(offset), RelType(type), Addend(addend),
Andrew Kaylor4612fed2013-08-19 23:27:43 +0000104 SymOffset(0), IsPCRel(false), Size(0) {}
105
106 RelocationEntry(unsigned id, uint64_t offset, uint32_t type, int64_t addend,
107 uint64_t symoffset)
108 : SectionID(id), Offset(offset), RelType(type), Addend(addend),
109 SymOffset(symoffset), IsPCRel(false), Size(0) {}
Rafael Espindolaf1f1c622013-04-29 17:24:34 +0000110
111 RelocationEntry(unsigned id, uint64_t offset, uint32_t type, int64_t addend,
112 bool IsPCRel, unsigned Size)
113 : SectionID(id), Offset(offset), RelType(type), Addend(addend),
Andrew Kaylor4612fed2013-08-19 23:27:43 +0000114 SymOffset(0), IsPCRel(IsPCRel), Size(Size) {}
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000115};
116
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000117class RelocationValueRef {
118public:
119 unsigned SectionID;
Andrew Kaylor4612fed2013-08-19 23:27:43 +0000120 uint64_t Offset;
121 int64_t Addend;
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000122 const char *SymbolName;
Andrew Kaylor4612fed2013-08-19 23:27:43 +0000123 RelocationValueRef(): SectionID(0), Offset(0), Addend(0), SymbolName(0) {}
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000124
125 inline bool operator==(const RelocationValueRef &Other) const {
Benjamin Kramer5a712502013-08-20 09:27:31 +0000126 return SectionID == Other.SectionID && Offset == Other.Offset &&
127 Addend == Other.Addend && SymbolName == Other.SymbolName;
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000128 }
129 inline bool operator <(const RelocationValueRef &Other) const {
Benjamin Kramer5a712502013-08-20 09:27:31 +0000130 if (SectionID != Other.SectionID)
131 return SectionID < Other.SectionID;
132 if (Offset != Other.Offset)
133 return Offset < Other.Offset;
134 if (Addend != Other.Addend)
135 return Addend < Other.Addend;
136 return SymbolName < Other.SymbolName;
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000137 }
138};
139
Danil Malyshev72510f22011-07-13 07:57:58 +0000140class RuntimeDyldImpl {
141protected:
Danil Malyshev72510f22011-07-13 07:57:58 +0000142 // The MemoryManager to load objects into.
143 RTDyldMemoryManager *MemMgr;
144
Eli Bendersky32d54882012-04-30 10:06:27 +0000145 // A list of all sections emitted by the dynamic linker. These sections are
146 // referenced in the code by means of their index in this list - SectionID.
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000147 typedef SmallVector<SectionEntry, 64> SectionList;
148 SectionList Sections;
Danil Malyshev72510f22011-07-13 07:57:58 +0000149
Andrew Kaylor7bb13442013-10-11 21:25:48 +0000150 typedef unsigned SID; // Type for SectionIDs
151 #define RTDYLD_INVALID_SECTION_ID ((SID)(-1))
152
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000153 // Keep a map of sections from object file to the SectionID which
154 // references it.
155 typedef std::map<SectionRef, unsigned> ObjSectionToIDMap;
Jim Grosbach0ddb3a42012-01-16 23:50:55 +0000156
Eli Benderskyfc079082012-05-01 06:58:59 +0000157 // A global symbol table for symbols from all loaded modules. Maps the
158 // symbol name to a (SectionID, offset in section) pair.
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000159 typedef std::pair<unsigned, uintptr_t> SymbolLoc;
Eli Benderskyfc079082012-05-01 06:58:59 +0000160 typedef StringMap<SymbolLoc> SymbolTableMap;
161 SymbolTableMap GlobalSymbolTable;
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000162
Tim Northover94bc73d2012-10-29 10:47:04 +0000163 // Pair representing the size and alignment requirement for a common symbol.
164 typedef std::pair<unsigned, unsigned> CommonSymbolInfo;
165 // Keep a map of common symbols to their info pairs
166 typedef std::map<SymbolRef, CommonSymbolInfo> CommonSymbolMap;
Preston Gurd2138ef62012-04-12 20:13:57 +0000167
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000168 // For each symbol, keep a list of relocations based on it. Anytime
169 // its address is reassigned (the JIT re-compiled the function, e.g.),
170 // the relocations get re-resolved.
171 // The symbol (or section) the relocation is sourced from is the Key
172 // in the relocation list where it's stored.
173 typedef SmallVector<RelocationEntry, 64> RelocationList;
174 // Relocations to sections already loaded. Indexed by SectionID which is the
Benjamin Kramerbde91762012-06-02 10:20:22 +0000175 // source of the address. The target where the address will be written is
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000176 // SectionID/Offset in the relocation itself.
177 DenseMap<unsigned, RelocationList> Relocations;
Eli Benderskyb92e1cf2012-04-30 12:15:58 +0000178
179 // Relocations to external symbols that are not yet resolved. Symbols are
180 // external when they aren't found in the global symbol table of all loaded
181 // modules. This map is indexed by symbol name.
182 StringMap<RelocationList> ExternalSymbolRelocations;
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000183
184 typedef std::map<RelocationValueRef, uintptr_t> StubMap;
185
186 Triple::ArchType Arch;
Andrew Kaylor33c5b1b2013-10-15 20:44:55 +0000187 bool IsTargetLittleEndian;
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000188
Andrew Kaylor2ba21c52013-10-15 21:32:56 +0000189 virtual unsigned getMaxStubSize() = 0;
190 virtual unsigned getStubAlignment() = 0;
Richard Sandifordca044082013-05-03 14:15:35 +0000191
Danil Malyshev72510f22011-07-13 07:57:58 +0000192 bool HasError;
193 std::string ErrorStr;
194
195 // Set the error state and record an error string.
196 bool Error(const Twine &Msg) {
197 ErrorStr = Msg.str();
198 HasError = true;
199 return true;
200 }
201
Jim Grosbachdc1123f2012-09-05 16:50:40 +0000202 uint64_t getSectionLoadAddress(unsigned SectionID) {
203 return Sections[SectionID].LoadAddress;
204 }
205
Jim Grosbacheff0a402012-01-16 22:26:39 +0000206 uint8_t *getSectionAddress(unsigned SectionID) {
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000207 return (uint8_t*)Sections[SectionID].Address;
Jim Grosbacheff0a402012-01-16 22:26:39 +0000208 }
Danil Malyshev72510f22011-07-13 07:57:58 +0000209
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000210 void writeInt16BE(uint8_t *Addr, uint16_t Value) {
Andrew Kaylor33c5b1b2013-10-15 20:44:55 +0000211 if (IsTargetLittleEndian)
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000212 Value = sys::SwapByteOrder(Value);
213 *Addr = (Value >> 8) & 0xFF;
214 *(Addr+1) = Value & 0xFF;
215 }
216
217 void writeInt32BE(uint8_t *Addr, uint32_t Value) {
Andrew Kaylor33c5b1b2013-10-15 20:44:55 +0000218 if (IsTargetLittleEndian)
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000219 Value = sys::SwapByteOrder(Value);
220 *Addr = (Value >> 24) & 0xFF;
221 *(Addr+1) = (Value >> 16) & 0xFF;
222 *(Addr+2) = (Value >> 8) & 0xFF;
223 *(Addr+3) = Value & 0xFF;
224 }
225
226 void writeInt64BE(uint8_t *Addr, uint64_t Value) {
Andrew Kaylor33c5b1b2013-10-15 20:44:55 +0000227 if (IsTargetLittleEndian)
Adhemerval Zanella5fc11b32012-10-25 13:13:48 +0000228 Value = sys::SwapByteOrder(Value);
229 *Addr = (Value >> 56) & 0xFF;
230 *(Addr+1) = (Value >> 48) & 0xFF;
231 *(Addr+2) = (Value >> 40) & 0xFF;
232 *(Addr+3) = (Value >> 32) & 0xFF;
233 *(Addr+4) = (Value >> 24) & 0xFF;
234 *(Addr+5) = (Value >> 16) & 0xFF;
235 *(Addr+6) = (Value >> 8) & 0xFF;
236 *(Addr+7) = Value & 0xFF;
237 }
238
Eli Bendersky667b8792012-05-01 10:41:12 +0000239 /// \brief Given the common symbols discovered in the object file, emit a
240 /// new section for them and update the symbol mappings in the object and
241 /// symbol table.
242 void emitCommonSymbols(ObjectImage &Obj,
243 const CommonSymbolMap &CommonSymbols,
244 uint64_t TotalSize,
245 SymbolTableMap &SymbolTable);
Preston Gurd2138ef62012-04-12 20:13:57 +0000246
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000247 /// \brief Emits section data from the object file to the MemoryManager.
248 /// \param IsCode if it's true then allocateCodeSection() will be
Eli Bendersky0e2ac5b2012-04-29 12:40:47 +0000249 /// used for emits, else allocateDataSection() will be used.
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000250 /// \return SectionID.
Preston Gurdcc31af92012-04-16 22:12:58 +0000251 unsigned emitSection(ObjectImage &Obj,
252 const SectionRef &Section,
253 bool IsCode);
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000254
255 /// \brief Find Section in LocalSections. If the secton is not found - emit
256 /// it and store in LocalSections.
257 /// \param IsCode if it's true then allocateCodeSection() will be
258 /// used for emmits, else allocateDataSection() will be used.
259 /// \return SectionID.
Preston Gurdcc31af92012-04-16 22:12:58 +0000260 unsigned findOrEmitSection(ObjectImage &Obj,
261 const SectionRef &Section,
262 bool IsCode,
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000263 ObjSectionToIDMap &LocalSections);
264
Eli Bendersky667b8792012-05-01 10:41:12 +0000265 // \brief Add a relocation entry that uses the given section.
266 void addRelocationForSection(const RelocationEntry &RE, unsigned SectionID);
267
268 // \brief Add a relocation entry that uses the given symbol. This symbol may
269 // be found in the global symbol table, or it may be external.
270 void addRelocationForSymbol(const RelocationEntry &RE, StringRef SymbolName);
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000271
272 /// \brief Emits long jump instruction to Addr.
273 /// \return Pointer to the memory area for emitting target address.
274 uint8_t* createStubFunction(uint8_t *Addr);
275
276 /// \brief Resolves relocations from Relocs list with address from Value.
277 void resolveRelocationList(const RelocationList &Relocs, uint64_t Value);
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000278
279 /// \brief A object file specific relocation resolver
Rafael Espindolab39478e2013-04-29 19:33:51 +0000280 /// \param RE The relocation to be resolved
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000281 /// \param Value Target symbol address to apply the relocation action
Rafael Espindolaf1f1c622013-04-29 17:24:34 +0000282 virtual void resolveRelocation(const RelocationEntry &RE, uint64_t Value) = 0;
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000283
Eli Bendersky0e2ac5b2012-04-29 12:40:47 +0000284 /// \brief Parses the object file relocation and stores it to Relocations
285 /// or SymbolRelocations (this depends on the object file type).
Rafael Espindola4d4a48d2013-04-29 14:44:23 +0000286 virtual void processRelocationRef(unsigned SectionID,
Rafael Espindola37008942013-04-29 19:03:21 +0000287 RelocationRef RelI,
Preston Gurdcc31af92012-04-16 22:12:58 +0000288 ObjectImage &Obj,
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000289 ObjSectionToIDMap &ObjSectionToID,
Eli Benderskyfc079082012-05-01 06:58:59 +0000290 const SymbolTableMap &Symbols,
Eli Bendersky0e2ac5b2012-04-29 12:40:47 +0000291 StubMap &Stubs) = 0;
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000292
Eli Benderskyb92e1cf2012-04-30 12:15:58 +0000293 /// \brief Resolve relocations to external symbols.
294 void resolveExternalSymbols();
Andrew Kaylor4612fed2013-08-19 23:27:43 +0000295
296 /// \brief Update GOT entries for external symbols.
297 // The base class does nothing. ELF overrides this.
298 virtual void updateGOTEntries(StringRef Name, uint64_t Addr) {}
299
Andrew Kayloradc70562012-10-02 21:18:39 +0000300 virtual ObjectImage *createObjectImage(ObjectBuffer *InputBuffer);
Danil Malyshev72510f22011-07-13 07:57:58 +0000301public:
302 RuntimeDyldImpl(RTDyldMemoryManager *mm) : MemMgr(mm), HasError(false) {}
303
304 virtual ~RuntimeDyldImpl();
305
Andrew Kayloradc70562012-10-02 21:18:39 +0000306 ObjectImage *loadObject(ObjectBuffer *InputBuffer);
Danil Malyshev72510f22011-07-13 07:57:58 +0000307
308 void *getSymbolAddress(StringRef Name) {
309 // FIXME: Just look up as a function for now. Overly simple of course.
310 // Work in progress.
Eli Benderskyfc079082012-05-01 06:58:59 +0000311 if (GlobalSymbolTable.find(Name) == GlobalSymbolTable.end())
Jim Grosbacheff0a402012-01-16 22:26:39 +0000312 return 0;
Eli Benderskyfc079082012-05-01 06:58:59 +0000313 SymbolLoc Loc = GlobalSymbolTable.lookup(Name);
Jim Grosbacheff0a402012-01-16 22:26:39 +0000314 return getSectionAddress(Loc.first) + Loc.second;
Danil Malyshev72510f22011-07-13 07:57:58 +0000315 }
316
Jim Grosbachdc1123f2012-09-05 16:50:40 +0000317 uint64_t getSymbolLoadAddress(StringRef Name) {
318 // FIXME: Just look up as a function for now. Overly simple of course.
319 // Work in progress.
320 if (GlobalSymbolTable.find(Name) == GlobalSymbolTable.end())
321 return 0;
322 SymbolLoc Loc = GlobalSymbolTable.lookup(Name);
323 return getSectionLoadAddress(Loc.first) + Loc.second;
324 }
325
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000326 void resolveRelocations();
Danil Malyshev72510f22011-07-13 07:57:58 +0000327
Danil Malyshev70d22cc2012-03-30 16:45:19 +0000328 void reassignSectionAddress(unsigned SectionID, uint64_t Addr);
Danil Malyshev72510f22011-07-13 07:57:58 +0000329
Jim Grosbach6d613972012-09-13 21:50:06 +0000330 void mapSectionAddress(const void *LocalAddress, uint64_t TargetAddress);
Jim Grosbach0ddb3a42012-01-16 23:50:55 +0000331
Danil Malyshev72510f22011-07-13 07:57:58 +0000332 // Is the linker in an error state?
333 bool hasError() { return HasError; }
334
335 // Mark the error condition as handled and continue.
336 void clearError() { HasError = false; }
337
338 // Get the error message.
339 StringRef getErrorString() { return ErrorStr; }
340
Andrew Kayloradc70562012-10-02 21:18:39 +0000341 virtual bool isCompatibleFormat(const ObjectBuffer *Buffer) const = 0;
Rafael Espindolafa5942b2013-05-05 20:43:10 +0000342
Andrew Kaylor7bb13442013-10-11 21:25:48 +0000343 virtual void registerEHFrames();
Andrew Kaylor4612fed2013-08-19 23:27:43 +0000344
Andrew Kaylorc442a762013-10-16 00:14:21 +0000345 virtual void deregisterEHFrames();
346
Andrew Kaylor7bb13442013-10-11 21:25:48 +0000347 virtual void finalizeLoad(ObjSectionToIDMap &SectionMap) {}
Danil Malyshev72510f22011-07-13 07:57:58 +0000348};
349
Danil Malyshev72510f22011-07-13 07:57:58 +0000350} // end namespace llvm
351
352
353#endif