1 //===-- RuntimeDyldImpl.h - Run-time dynamic linker for MC-JIT ------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Interface for the implementations of runtime dynamic linker facilities.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_RUNTIME_DYLD_IMPL_H
15 #define LLVM_RUNTIME_DYLD_IMPL_H
17 #include "llvm/ExecutionEngine/RuntimeDyld.h"
18 #include "llvm/Object/MachOObject.h"
19 #include "llvm/ADT/DenseMap.h"
20 #include "llvm/ADT/IndexedMap.h"
21 #include "llvm/ADT/StringMap.h"
22 #include "llvm/ADT/Twine.h"
23 #include "llvm/ADT/SmallVector.h"
24 #include "llvm/ExecutionEngine/ExecutionEngine.h"
25 #include "llvm/Support/Format.h"
26 #include "llvm/Support/Memory.h"
27 #include "llvm/Support/MemoryBuffer.h"
28 #include "llvm/Support/system_error.h"
29 #include "llvm/Support/raw_ostream.h"
30 #include "llvm/Support/Debug.h"
31 #include "llvm/Support/ErrorHandling.h"
34 using namespace llvm::object;
37 class RuntimeDyldImpl {
42 // The MemoryManager to load objects into.
43 RTDyldMemoryManager *MemMgr;
45 // For each section, we have a MemoryBlock of it's data.
46 // Indexed by SectionID.
47 SmallVector<sys::MemoryBlock, 32> Sections;
48 // For each section, the address it will be considered to live at for
49 // relocations. The same as the pointer to the above memory block for hosted
50 // JITs. Indexed by SectionID.
51 SmallVector<uint64_t, 32> SectionLoadAddress;
53 // Keep a map of starting local address to the SectionID which references it.
54 // Lookup function for when we assign virtual addresses.
55 DenseMap<void *, unsigned> SectionLocalMemToID;
57 // Master symbol table. As modules are loaded and external symbols are
58 // resolved, their addresses are stored here as a SectionID/Offset pair.
59 typedef std::pair<unsigned, uint64_t> SymbolLoc;
60 StringMap<SymbolLoc> SymbolTable;
65 // Set the error state and record an error string.
66 bool Error(const Twine &Msg) {
72 uint8_t *getSectionAddress(unsigned SectionID) {
73 return (uint8_t*)Sections[SectionID].base();
75 void extractFunction(StringRef Name, uint8_t *StartAddress,
79 RuntimeDyldImpl(RTDyldMemoryManager *mm) : MemMgr(mm), HasError(false) {}
81 virtual ~RuntimeDyldImpl();
83 virtual bool loadObject(MemoryBuffer *InputBuffer) = 0;
85 void *getSymbolAddress(StringRef Name) {
86 // FIXME: Just look up as a function for now. Overly simple of course.
88 if (SymbolTable.find(Name) == SymbolTable.end())
90 SymbolLoc Loc = SymbolTable.lookup(Name);
91 return getSectionAddress(Loc.first) + Loc.second;
94 virtual void resolveRelocations();
96 virtual void reassignSectionAddress(unsigned SectionID, uint64_t Addr) = 0;
98 void mapSectionAddress(void *LocalAddress, uint64_t TargetAddress);
100 // Is the linker in an error state?
101 bool hasError() { return HasError; }
103 // Mark the error condition as handled and continue.
104 void clearError() { HasError = false; }
106 // Get the error message.
107 StringRef getErrorString() { return ErrorStr; }
109 virtual bool isCompatibleFormat(const MemoryBuffer *InputBuffer) const = 0;
112 class RuntimeDyldELF : public RuntimeDyldImpl {
113 // For each symbol, keep a list of relocations based on it. Anytime
114 // its address is reassigned (the JIT re-compiled the function, e.g.),
115 // the relocations get re-resolved.
116 struct RelocationEntry {
117 // Function or section this relocation is contained in.
119 // Offset into the target function or section for the relocation.
123 // Addend encoded in the instruction itself, if any.
125 // Has the relocation been recalcuated as an offset within a function?
126 bool IsFunctionRelative;
127 // Has this relocation been resolved previously?
130 RelocationEntry(StringRef t,
134 bool isFunctionRelative)
139 , IsFunctionRelative(isFunctionRelative)
140 , isResolved(false) { }
142 typedef SmallVector<RelocationEntry, 4> RelocationList;
143 StringMap<RelocationList> Relocations;
146 void resolveRelocations();
148 void resolveX86_64Relocation(StringRef Name,
150 const RelocationEntry &RE);
152 void resolveX86Relocation(StringRef Name,
154 const RelocationEntry &RE);
156 void resolveArmRelocation(StringRef Name,
158 const RelocationEntry &RE);
160 void resolveRelocation(StringRef Name,
162 const RelocationEntry &RE);
165 RuntimeDyldELF(RTDyldMemoryManager *mm) : RuntimeDyldImpl(mm) {}
167 bool loadObject(MemoryBuffer *InputBuffer);
169 void reassignSymbolAddress(StringRef Name, uint8_t *Addr);
170 void reassignSectionAddress(unsigned SectionID, uint64_t Addr);
172 bool isCompatibleFormat(const MemoryBuffer *InputBuffer) const;
176 class RuntimeDyldMachO : public RuntimeDyldImpl {
178 // For each symbol, keep a list of relocations based on it. Anytime
179 // its address is reassigned (the JIT re-compiled the function, e.g.),
180 // the relocations get re-resolved.
181 // The symbol (or section) the relocation is sourced from is the Key
182 // in the relocation list where it's stored.
183 struct RelocationEntry {
184 unsigned SectionID; // Section the relocation is contained in.
185 uint64_t Offset; // Offset into the section for the relocation.
186 uint32_t Data; // Second word of the raw macho relocation entry.
187 int64_t Addend; // Addend encoded in the instruction itself, if any,
188 // plus the offset into the source section for
189 // the symbol once the relocation is resolvable.
191 RelocationEntry(unsigned id, uint64_t offset, uint32_t data, int64_t addend)
192 : SectionID(id), Offset(offset), Data(data), Addend(addend) {}
194 typedef SmallVector<RelocationEntry, 4> RelocationList;
195 // Relocations to sections already loaded. Indexed by SectionID which is the
196 // source of the address. The target where the address will be writen is
197 // SectionID/Offset in the relocation itself.
198 IndexedMap<RelocationList> Relocations;
199 // Relocations to symbols that are not yet resolved. Must be external
200 // relocations by definition. Indexed by symbol name.
201 StringMap<RelocationList> UnresolvedRelocations;
203 bool resolveRelocation(uint8_t *Address, uint64_t Value, bool isPCRel,
204 unsigned Type, unsigned Size, int64_t Addend);
205 bool resolveX86_64Relocation(uintptr_t Address, uintptr_t Value, bool isPCRel,
206 unsigned Type, unsigned Size, int64_t Addend);
207 bool resolveARMRelocation(uintptr_t Address, uintptr_t Value, bool isPCRel,
208 unsigned Type, unsigned Size, int64_t Addend);
210 bool loadSegment32(const MachOObject *Obj,
211 const MachOObject::LoadCommandInfo *SegmentLCI,
212 const InMemoryStruct<macho::SymtabLoadCommand> &SymtabLC);
213 bool loadSegment64(const MachOObject *Obj,
214 const MachOObject::LoadCommandInfo *SegmentLCI,
215 const InMemoryStruct<macho::SymtabLoadCommand> &SymtabLC);
216 bool processSymbols32(const MachOObject *Obj,
217 SmallVectorImpl<unsigned> &SectionMap,
218 SmallVectorImpl<StringRef> &SymbolNames,
219 const InMemoryStruct<macho::SymtabLoadCommand> &SymtabLC);
220 bool processSymbols64(const MachOObject *Obj,
221 SmallVectorImpl<unsigned> &SectionMap,
222 SmallVectorImpl<StringRef> &SymbolNames,
223 const InMemoryStruct<macho::SymtabLoadCommand> &SymtabLC);
225 void resolveSymbol(StringRef Name);
228 RuntimeDyldMachO(RTDyldMemoryManager *mm) : RuntimeDyldImpl(mm) {}
230 bool loadObject(MemoryBuffer *InputBuffer);
232 void reassignSectionAddress(unsigned SectionID, uint64_t Addr);
234 static bool isKnownFormat(const MemoryBuffer *InputBuffer);
236 bool isCompatibleFormat(const MemoryBuffer *InputBuffer) const {
237 return isKnownFormat(InputBuffer);
241 } // end namespace llvm