blob: bf678af6ece787fdebe80767b2fa9e1fce700419 [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
17#include "llvm/ExecutionEngine/RuntimeDyld.h"
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +000018#include "llvm/Object/ObjectFile.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/StringMap.h"
21#include "llvm/ADT/Twine.h"
22#include "llvm/ADT/SmallVector.h"
Danil Malyshevcf852dc2011-07-13 07:57:58 +000023#include "llvm/Support/Memory.h"
24#include "llvm/Support/MemoryBuffer.h"
25#include "llvm/Support/system_error.h"
26#include "llvm/Support/raw_ostream.h"
27#include "llvm/Support/Debug.h"
28#include "llvm/Support/ErrorHandling.h"
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +000029#include "llvm/ADT/Triple.h"
30#include <map>
31#include "llvm/Support/Format.h"
Danil Malyshevcf852dc2011-07-13 07:57:58 +000032
33using namespace llvm;
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +000034using namespace llvm::object;
Danil Malyshevcf852dc2011-07-13 07:57:58 +000035
36namespace llvm {
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +000037
38class SectionEntry {
39public:
40 uint8_t* Address;
41 size_t Size;
42 uint64_t LoadAddress; // For each section, the address it will be
43 // considered to live at for relocations. The same
44 // as the pointer to the above memory block for
45 // hosted JITs.
46 uintptr_t StubOffset; // It's used for architecturies with stub
47 // functions for far relocations like ARM.
48 uintptr_t ObjAddress; // Section address in object file. It's use for
49 // calculate MachO relocation addend
50 SectionEntry(uint8_t* address, size_t size, uintptr_t stubOffset,
51 uintptr_t objAddress)
52 : Address(address), Size(size), LoadAddress((uintptr_t)address),
53 StubOffset(stubOffset), ObjAddress(objAddress) {}
54};
55
56class RelocationEntry {
57public:
58 unsigned SectionID; // Section the relocation is contained in.
59 uintptr_t Offset; // Offset into the section for the relocation.
60 uint32_t Data; // Relocatino data. Including type of relocation
61 // and another flags and parameners from
62 intptr_t Addend; // Addend encoded in the instruction itself, if any,
63 // plus the offset into the source section for
64 // the symbol once the relocation is resolvable.
65 RelocationEntry(unsigned id, uint64_t offset, uint32_t data, int64_t addend)
66 : SectionID(id), Offset(offset), Data(data), Addend(addend) {}
67};
68
69// Raw relocation data from object file
70class ObjRelocationInfo {
71public:
72 unsigned SectionID;
73 uint64_t Offset;
74 SymbolRef Symbol;
75 uint64_t Type;
76 int64_t AdditionalInfo;
77};
78
79class RelocationValueRef {
80public:
81 unsigned SectionID;
82 intptr_t Addend;
83 const char *SymbolName;
84 RelocationValueRef(): SectionID(0), Addend(0), SymbolName(0) {}
85
86 inline bool operator==(const RelocationValueRef &Other) const {
87 return std::memcmp(this, &Other, sizeof(RelocationValueRef)) == 0;
88 }
89 inline bool operator <(const RelocationValueRef &Other) const {
90 return std::memcmp(this, &Other, sizeof(RelocationValueRef)) < 0;
91 }
92};
93
Danil Malyshevcf852dc2011-07-13 07:57:58 +000094class RuntimeDyldImpl {
95protected:
Danil Malyshevcf852dc2011-07-13 07:57:58 +000096 // The MemoryManager to load objects into.
97 RTDyldMemoryManager *MemMgr;
98
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +000099 // A list of emmitted sections.
100 typedef SmallVector<SectionEntry, 64> SectionList;
101 SectionList Sections;
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000102
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000103 // Keep a map of sections from object file to the SectionID which
104 // references it.
105 typedef std::map<SectionRef, unsigned> ObjSectionToIDMap;
Jim Grosbach020f4e82012-01-16 23:50:55 +0000106
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000107 // Master symbol table. As modules are loaded and external symbols are
Jim Grosbach61425c02012-01-16 22:26:39 +0000108 // resolved, their addresses are stored here as a SectionID/Offset pair.
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000109 typedef std::pair<unsigned, uintptr_t> SymbolLoc;
Jim Grosbach61425c02012-01-16 22:26:39 +0000110 StringMap<SymbolLoc> SymbolTable;
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000111 typedef DenseMap<const char*, SymbolLoc> LocalSymbolMap;
112
Preston Gurdc68dda82012-04-12 20:13:57 +0000113 // Keep a map of common symbols to their sizes
114 typedef std::map<SymbolRef, unsigned> CommonSymbolMap;
115
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000116 // For each symbol, keep a list of relocations based on it. Anytime
117 // its address is reassigned (the JIT re-compiled the function, e.g.),
118 // the relocations get re-resolved.
119 // The symbol (or section) the relocation is sourced from is the Key
120 // in the relocation list where it's stored.
121 typedef SmallVector<RelocationEntry, 64> RelocationList;
122 // Relocations to sections already loaded. Indexed by SectionID which is the
123 // source of the address. The target where the address will be writen is
124 // SectionID/Offset in the relocation itself.
125 DenseMap<unsigned, RelocationList> Relocations;
126 // Relocations to external symbols that are not yet resolved.
127 // Indexed by symbol name.
128 StringMap<RelocationList> SymbolRelocations;
129
130 typedef std::map<RelocationValueRef, uintptr_t> StubMap;
131
132 Triple::ArchType Arch;
133
134 inline unsigned getMaxStubSize() {
135 if (Arch == Triple::arm || Arch == Triple::thumb)
136 return 8; // 32-bit instruction and 32-bit address
137 else
138 return 0;
139 }
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000140
141 bool HasError;
142 std::string ErrorStr;
143
144 // Set the error state and record an error string.
145 bool Error(const Twine &Msg) {
146 ErrorStr = Msg.str();
147 HasError = true;
148 return true;
149 }
150
Jim Grosbach61425c02012-01-16 22:26:39 +0000151 uint8_t *getSectionAddress(unsigned SectionID) {
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000152 return (uint8_t*)Sections[SectionID].Address;
Jim Grosbach61425c02012-01-16 22:26:39 +0000153 }
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000154
Preston Gurdc68dda82012-04-12 20:13:57 +0000155 /// \brief Emits a section containing common symbols.
156 /// \return SectionID.
157 unsigned emitCommonSymbols(const CommonSymbolMap &Map,
158 uint64_t TotalSize,
159 LocalSymbolMap &Symbols);
160
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000161 /// \brief Emits section data from the object file to the MemoryManager.
162 /// \param IsCode if it's true then allocateCodeSection() will be
163 /// used for emmits, else allocateDataSection() will be used.
164 /// \return SectionID.
165 unsigned emitSection(const SectionRef &Section, bool IsCode);
166
167 /// \brief Find Section in LocalSections. If the secton is not found - emit
168 /// it and store in LocalSections.
169 /// \param IsCode if it's true then allocateCodeSection() will be
170 /// used for emmits, else allocateDataSection() will be used.
171 /// \return SectionID.
172 unsigned findOrEmitSection(const SectionRef &Section, bool IsCode,
173 ObjSectionToIDMap &LocalSections);
174
175 /// \brief If Value.SymbolName is NULL then store relocation to the
176 /// Relocations, else store it in the SymbolRelocations.
177 void AddRelocation(const RelocationValueRef &Value, unsigned SectionID,
178 uintptr_t Offset, uint32_t RelType);
179
180 /// \brief Emits long jump instruction to Addr.
181 /// \return Pointer to the memory area for emitting target address.
182 uint8_t* createStubFunction(uint8_t *Addr);
183
184 /// \brief Resolves relocations from Relocs list with address from Value.
185 void resolveRelocationList(const RelocationList &Relocs, uint64_t Value);
186 void resolveRelocationEntry(const RelocationEntry &RE, uint64_t Value);
187
188 /// \brief A object file specific relocation resolver
189 /// \param Address Address to apply the relocation action
190 /// \param Value Target symbol address to apply the relocation action
191 /// \param Type object file specific relocation type
192 /// \param Addend A constant addend used to compute the value to be stored
193 /// into the relocatable field
194 virtual void resolveRelocation(uint8_t *LocalAddress,
195 uint64_t FinalAddress,
196 uint64_t Value,
197 uint32_t Type,
198 int64_t Addend) = 0;
199
200 /// \brief Parses the object file relocation and store it to Relocations
201 /// or SymbolRelocations. Its depend from object file type.
202 virtual void processRelocationRef(const ObjRelocationInfo &Rel,
203 const ObjectFile &Obj,
204 ObjSectionToIDMap &ObjSectionToID,
205 LocalSymbolMap &Symbols, StubMap &Stubs) = 0;
206
207 void resolveSymbols();
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000208public:
209 RuntimeDyldImpl(RTDyldMemoryManager *mm) : MemMgr(mm), HasError(false) {}
210
211 virtual ~RuntimeDyldImpl();
212
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000213 bool loadObject(const MemoryBuffer *InputBuffer);
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000214
215 void *getSymbolAddress(StringRef Name) {
216 // FIXME: Just look up as a function for now. Overly simple of course.
217 // Work in progress.
Jim Grosbach61425c02012-01-16 22:26:39 +0000218 if (SymbolTable.find(Name) == SymbolTable.end())
219 return 0;
220 SymbolLoc Loc = SymbolTable.lookup(Name);
221 return getSectionAddress(Loc.first) + Loc.second;
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000222 }
223
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000224 void resolveRelocations();
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000225
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000226 void reassignSectionAddress(unsigned SectionID, uint64_t Addr);
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000227
Jim Grosbach020f4e82012-01-16 23:50:55 +0000228 void mapSectionAddress(void *LocalAddress, uint64_t TargetAddress);
229
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000230 // Is the linker in an error state?
231 bool hasError() { return HasError; }
232
233 // Mark the error condition as handled and continue.
234 void clearError() { HasError = false; }
235
236 // Get the error message.
237 StringRef getErrorString() { return ErrorStr; }
238
239 virtual bool isCompatibleFormat(const MemoryBuffer *InputBuffer) const = 0;
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000240
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000241};
242
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000243} // end namespace llvm
244
245
246#endif