blob: e97e5859e0118e3c0ccd87e58014e47c95695420 [file] [log] [blame]
Jim Grosbache0934be2012-01-16 23:50:58 +00001//===-- RuntimeDyldImpl.h - Run-time dynamic linker for MC-JIT --*- C++ -*-===//
Danil Malyshevcf852dc2011-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
Eli Bendersky6d15e872012-04-30 10:06:27 +000017#include "ObjectImage.h"
Danil Malyshevcf852dc2011-07-13 07:57:58 +000018#include "llvm/ExecutionEngine/RuntimeDyld.h"
Jim Grosbach020f4e82012-01-16 23:50:55 +000019#include "llvm/ADT/DenseMap.h"
Danil Malyshevcf852dc2011-07-13 07:57:58 +000020#include "llvm/ADT/SmallVector.h"
Eli Bendersky6d15e872012-04-30 10:06:27 +000021#include "llvm/ADT/StringMap.h"
22#include "llvm/ADT/Triple.h"
23#include "llvm/Object/ObjectFile.h"
Danil Malyshevcf852dc2011-07-13 07:57:58 +000024#include "llvm/Support/Debug.h"
25#include "llvm/Support/ErrorHandling.h"
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +000026#include "llvm/Support/Format.h"
Eli Bendersky6d15e872012-04-30 10:06:27 +000027#include "llvm/Support/raw_ostream.h"
28#include "llvm/Support/system_error.h"
29#include <map>
Danil Malyshevcf852dc2011-07-13 07:57:58 +000030
31using namespace llvm;
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +000032using namespace llvm::object;
Danil Malyshevcf852dc2011-07-13 07:57:58 +000033
34namespace llvm {
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +000035
Eli Bendersky6d15e872012-04-30 10:06:27 +000036class MemoryBuffer;
37class Twine;
38
39
40/// SectionEntry - represents a section emitted into memory by the dynamic
41/// linker.
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +000042class SectionEntry {
43public:
Eli Bendersky6d15e872012-04-30 10:06:27 +000044 /// Address - address in the linker's memory where the section resides.
45 uint8_t *Address;
46
47 /// Size - section size.
48 size_t Size;
49
50 /// LoadAddress - the address of the section in the target process's memory.
51 /// Used for situations in which JIT-ed code is being executed in the address
52 /// space of a separate process. If the code executes in the same address
53 /// space where it was JIT-ed, this just equals Address.
54 uint64_t LoadAddress;
55
56 /// StubOffset - used for architectures with stub functions for far
57 /// relocations (like ARM).
58 uintptr_t StubOffset;
59
60 /// ObjAddress - address of the section in the in-memory object file. Used
61 /// for calculating relocations in some object formats (like MachO).
62 uintptr_t ObjAddress;
63
Eli Bendersky5fe01982012-04-29 12:40:47 +000064 SectionEntry(uint8_t *address, size_t size, uintptr_t stubOffset,
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +000065 uintptr_t objAddress)
66 : Address(address), Size(size), LoadAddress((uintptr_t)address),
67 StubOffset(stubOffset), ObjAddress(objAddress) {}
68};
69
Eli Bendersky6d15e872012-04-30 10:06:27 +000070/// RelocationEntry - used to represent relocations internally in the dynamic
71/// linker.
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +000072class RelocationEntry {
73public:
Eli Bendersky6d15e872012-04-30 10:06:27 +000074 /// SectionID - the section this relocation points to.
75 unsigned SectionID;
76
77 /// Offset - offset into the section.
78 uintptr_t Offset;
79
80 /// RelType - relocation type.
81 uint32_t RelType;
82
83 /// Addend - the relocation addend encoded in the instruction itself. Also
84 /// used to make a relocation section relative instead of symbol relative.
85 intptr_t Addend;
86
87 RelocationEntry(unsigned id, uint64_t offset, uint32_t type, int64_t addend)
88 : SectionID(id), Offset(offset), RelType(type), Addend(addend) {}
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +000089};
90
Eli Bendersky6d15e872012-04-30 10:06:27 +000091/// ObjRelocationInfo - relocation information as read from the object file.
92/// Used to pass around data taken from object::RelocationRef, together with
93/// the section to which the relocation points (represented by a SectionID).
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +000094class ObjRelocationInfo {
95public:
96 unsigned SectionID;
97 uint64_t Offset;
98 SymbolRef Symbol;
99 uint64_t Type;
100 int64_t AdditionalInfo;
101};
102
103class RelocationValueRef {
104public:
105 unsigned SectionID;
106 intptr_t Addend;
107 const char *SymbolName;
108 RelocationValueRef(): SectionID(0), Addend(0), SymbolName(0) {}
109
110 inline bool operator==(const RelocationValueRef &Other) const {
111 return std::memcmp(this, &Other, sizeof(RelocationValueRef)) == 0;
112 }
113 inline bool operator <(const RelocationValueRef &Other) const {
114 return std::memcmp(this, &Other, sizeof(RelocationValueRef)) < 0;
115 }
116};
117
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000118class RuntimeDyldImpl {
119protected:
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000120 // The MemoryManager to load objects into.
121 RTDyldMemoryManager *MemMgr;
122
Eli Bendersky6d15e872012-04-30 10:06:27 +0000123 // A list of all sections emitted by the dynamic linker. These sections are
124 // referenced in the code by means of their index in this list - SectionID.
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000125 typedef SmallVector<SectionEntry, 64> SectionList;
126 SectionList Sections;
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000127
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000128 // Keep a map of sections from object file to the SectionID which
129 // references it.
130 typedef std::map<SectionRef, unsigned> ObjSectionToIDMap;
Jim Grosbach020f4e82012-01-16 23:50:55 +0000131
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000132 // Master symbol table. As modules are loaded and external symbols are
Jim Grosbach61425c02012-01-16 22:26:39 +0000133 // resolved, their addresses are stored here as a SectionID/Offset pair.
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000134 typedef std::pair<unsigned, uintptr_t> SymbolLoc;
Jim Grosbach61425c02012-01-16 22:26:39 +0000135 StringMap<SymbolLoc> SymbolTable;
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000136 typedef DenseMap<const char*, SymbolLoc> LocalSymbolMap;
137
Preston Gurdc68dda82012-04-12 20:13:57 +0000138 // Keep a map of common symbols to their sizes
139 typedef std::map<SymbolRef, unsigned> CommonSymbolMap;
140
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000141 // For each symbol, keep a list of relocations based on it. Anytime
142 // its address is reassigned (the JIT re-compiled the function, e.g.),
143 // the relocations get re-resolved.
144 // The symbol (or section) the relocation is sourced from is the Key
145 // in the relocation list where it's stored.
146 typedef SmallVector<RelocationEntry, 64> RelocationList;
147 // Relocations to sections already loaded. Indexed by SectionID which is the
148 // source of the address. The target where the address will be writen is
149 // SectionID/Offset in the relocation itself.
150 DenseMap<unsigned, RelocationList> Relocations;
151 // Relocations to external symbols that are not yet resolved.
152 // Indexed by symbol name.
153 StringMap<RelocationList> SymbolRelocations;
154
155 typedef std::map<RelocationValueRef, uintptr_t> StubMap;
156
157 Triple::ArchType Arch;
158
159 inline unsigned getMaxStubSize() {
160 if (Arch == Triple::arm || Arch == Triple::thumb)
161 return 8; // 32-bit instruction and 32-bit address
162 else
163 return 0;
164 }
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000165
166 bool HasError;
167 std::string ErrorStr;
168
169 // Set the error state and record an error string.
170 bool Error(const Twine &Msg) {
171 ErrorStr = Msg.str();
172 HasError = true;
173 return true;
174 }
175
Jim Grosbach61425c02012-01-16 22:26:39 +0000176 uint8_t *getSectionAddress(unsigned SectionID) {
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000177 return (uint8_t*)Sections[SectionID].Address;
Jim Grosbach61425c02012-01-16 22:26:39 +0000178 }
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000179
Preston Gurdc68dda82012-04-12 20:13:57 +0000180 /// \brief Emits a section containing common symbols.
181 /// \return SectionID.
Preston Gurd689ff9c2012-04-16 22:12:58 +0000182 unsigned emitCommonSymbols(ObjectImage &Obj,
183 const CommonSymbolMap &Map,
Preston Gurdc68dda82012-04-12 20:13:57 +0000184 uint64_t TotalSize,
185 LocalSymbolMap &Symbols);
186
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000187 /// \brief Emits section data from the object file to the MemoryManager.
188 /// \param IsCode if it's true then allocateCodeSection() will be
Eli Bendersky5fe01982012-04-29 12:40:47 +0000189 /// used for emits, else allocateDataSection() will be used.
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000190 /// \return SectionID.
Preston Gurd689ff9c2012-04-16 22:12:58 +0000191 unsigned emitSection(ObjectImage &Obj,
192 const SectionRef &Section,
193 bool IsCode);
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000194
195 /// \brief Find Section in LocalSections. If the secton is not found - emit
196 /// it and store in LocalSections.
197 /// \param IsCode if it's true then allocateCodeSection() will be
198 /// used for emmits, else allocateDataSection() will be used.
199 /// \return SectionID.
Preston Gurd689ff9c2012-04-16 22:12:58 +0000200 unsigned findOrEmitSection(ObjectImage &Obj,
201 const SectionRef &Section,
202 bool IsCode,
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000203 ObjSectionToIDMap &LocalSections);
204
205 /// \brief If Value.SymbolName is NULL then store relocation to the
206 /// Relocations, else store it in the SymbolRelocations.
Eli Bendersky6d15e872012-04-30 10:06:27 +0000207 void addRelocation(const RelocationValueRef &Value, unsigned SectionID,
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000208 uintptr_t Offset, uint32_t RelType);
209
210 /// \brief Emits long jump instruction to Addr.
211 /// \return Pointer to the memory area for emitting target address.
212 uint8_t* createStubFunction(uint8_t *Addr);
213
214 /// \brief Resolves relocations from Relocs list with address from Value.
215 void resolveRelocationList(const RelocationList &Relocs, uint64_t Value);
216 void resolveRelocationEntry(const RelocationEntry &RE, uint64_t Value);
217
218 /// \brief A object file specific relocation resolver
219 /// \param Address Address to apply the relocation action
220 /// \param Value Target symbol address to apply the relocation action
221 /// \param Type object file specific relocation type
222 /// \param Addend A constant addend used to compute the value to be stored
223 /// into the relocatable field
224 virtual void resolveRelocation(uint8_t *LocalAddress,
225 uint64_t FinalAddress,
226 uint64_t Value,
227 uint32_t Type,
228 int64_t Addend) = 0;
229
Eli Bendersky5fe01982012-04-29 12:40:47 +0000230 /// \brief Parses the object file relocation and stores it to Relocations
231 /// or SymbolRelocations (this depends on the object file type).
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000232 virtual void processRelocationRef(const ObjRelocationInfo &Rel,
Preston Gurd689ff9c2012-04-16 22:12:58 +0000233 ObjectImage &Obj,
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000234 ObjSectionToIDMap &ObjSectionToID,
Eli Bendersky5fe01982012-04-29 12:40:47 +0000235 LocalSymbolMap &Symbols,
236 StubMap &Stubs) = 0;
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000237
238 void resolveSymbols();
Preston Gurd689ff9c2012-04-16 22:12:58 +0000239 virtual ObjectImage *createObjectImage(const MemoryBuffer *InputBuffer);
240 virtual void handleObjectLoaded(ObjectImage *Obj)
241 {
242 // Subclasses may choose to retain this image if they have a use for it
243 delete Obj;
244 }
245
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000246public:
247 RuntimeDyldImpl(RTDyldMemoryManager *mm) : MemMgr(mm), HasError(false) {}
248
249 virtual ~RuntimeDyldImpl();
250
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000251 bool loadObject(const MemoryBuffer *InputBuffer);
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000252
253 void *getSymbolAddress(StringRef Name) {
254 // FIXME: Just look up as a function for now. Overly simple of course.
255 // Work in progress.
Jim Grosbach61425c02012-01-16 22:26:39 +0000256 if (SymbolTable.find(Name) == SymbolTable.end())
257 return 0;
258 SymbolLoc Loc = SymbolTable.lookup(Name);
259 return getSectionAddress(Loc.first) + Loc.second;
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000260 }
261
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000262 void resolveRelocations();
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000263
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000264 void reassignSectionAddress(unsigned SectionID, uint64_t Addr);
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000265
Jim Grosbach020f4e82012-01-16 23:50:55 +0000266 void mapSectionAddress(void *LocalAddress, uint64_t TargetAddress);
267
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000268 // Is the linker in an error state?
269 bool hasError() { return HasError; }
270
271 // Mark the error condition as handled and continue.
272 void clearError() { HasError = false; }
273
274 // Get the error message.
275 StringRef getErrorString() { return ErrorStr; }
276
277 virtual bool isCompatibleFormat(const MemoryBuffer *InputBuffer) const = 0;
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000278
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000279};
280
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000281} // end namespace llvm
282
283
284#endif