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/StringMap.h"
20 #include "llvm/ADT/Twine.h"
21 #include "llvm/ADT/SmallVector.h"
22 #include "llvm/ExecutionEngine/ExecutionEngine.h"
23 #include "llvm/Support/Format.h"
24 #include "llvm/Support/Memory.h"
25 #include "llvm/Support/MemoryBuffer.h"
26 #include "llvm/Support/system_error.h"
27 #include "llvm/Support/raw_ostream.h"
28 #include "llvm/Support/Debug.h"
29 #include "llvm/Support/ErrorHandling.h"
32 using namespace llvm::object;
35 class RuntimeDyldImpl {
40 // The MemoryManager to load objects into.
41 RTDyldMemoryManager *MemMgr;
43 // FIXME: This all assumes we're dealing with external symbols for anything
44 // explicitly referenced. I.e., we can index by name and things
45 // will work out. In practice, this may not be the case, so we
46 // should find a way to effectively generalize.
48 // For each function, we have a MemoryBlock of it's instruction data.
49 StringMap<sys::MemoryBlock> Functions;
51 // Master symbol table. As modules are loaded and external symbols are
52 // resolved, their addresses are stored here.
53 StringMap<uint8_t*> SymbolTable;
58 // Set the error state and record an error string.
59 bool Error(const Twine &Msg) {
65 void extractFunction(StringRef Name, uint8_t *StartAddress,
69 RuntimeDyldImpl(RTDyldMemoryManager *mm) : MemMgr(mm), HasError(false) {}
71 virtual ~RuntimeDyldImpl();
73 virtual bool loadObject(MemoryBuffer *InputBuffer) = 0;
75 void *getSymbolAddress(StringRef Name) {
76 // FIXME: Just look up as a function for now. Overly simple of course.
78 return SymbolTable.lookup(Name);
81 void resolveRelocations();
83 virtual void reassignSymbolAddress(StringRef Name, uint8_t *Addr) = 0;
85 // Is the linker in an error state?
86 bool hasError() { return HasError; }
88 // Mark the error condition as handled and continue.
89 void clearError() { HasError = false; }
91 // Get the error message.
92 StringRef getErrorString() { return ErrorStr; }
94 virtual bool isCompatibleFormat(const MemoryBuffer *InputBuffer) const = 0;
97 class RuntimeDyldELF : public RuntimeDyldImpl {
98 // For each symbol, keep a list of relocations based on it. Anytime
99 // its address is reassigned (the JIT re-compiled the function, e.g.),
100 // the relocations get re-resolved.
101 struct RelocationEntry {
102 // Function or section this relocation is contained in.
104 // Offset into the target function or section for the relocation.
108 // Addend encoded in the instruction itself, if any.
110 // Has the relocation been recalcuated as an offset within a function?
111 bool IsFunctionRelative;
112 // Has this relocation been resolved previously?
115 RelocationEntry(StringRef t,
119 bool isFunctionRelative)
124 , IsFunctionRelative(isFunctionRelative)
125 , isResolved(false) { }
127 typedef SmallVector<RelocationEntry, 4> RelocationList;
128 StringMap<RelocationList> Relocations;
131 void resolveX86_64Relocation(StringRef Name,
133 const RelocationEntry &RE);
135 void resolveX86Relocation(StringRef Name,
137 const RelocationEntry &RE);
139 void resolveArmRelocation(StringRef Name,
141 const RelocationEntry &RE);
143 void resolveRelocation(StringRef Name,
145 const RelocationEntry &RE);
148 RuntimeDyldELF(RTDyldMemoryManager *mm) : RuntimeDyldImpl(mm) {}
150 bool loadObject(MemoryBuffer *InputBuffer);
152 void reassignSymbolAddress(StringRef Name, uint8_t *Addr);
154 bool isCompatibleFormat(const MemoryBuffer *InputBuffer) const;
158 class RuntimeDyldMachO : public RuntimeDyldImpl {
160 // For each symbol, keep a list of relocations based on it. Anytime
161 // its address is reassigned (the JIT re-compiled the function, e.g.),
162 // the relocations get re-resolved.
163 struct RelocationEntry {
164 std::string Target; // Object this relocation is contained in.
165 uint64_t Offset; // Offset into the object for the relocation.
166 uint32_t Data; // Second word of the raw macho relocation entry.
167 int64_t Addend; // Addend encoded in the instruction itself, if any.
168 bool isResolved; // Has this relocation been resolved previously?
170 RelocationEntry(StringRef t, uint64_t offset, uint32_t data, int64_t addend)
171 : Target(t), Offset(offset), Data(data), Addend(addend),
174 typedef SmallVector<RelocationEntry, 4> RelocationList;
175 StringMap<RelocationList> Relocations;
177 // FIXME: Also keep a map of all the relocations contained in an object. Use
178 // this to dynamically answer whether all of the relocations in it have
179 // been resolved or not.
181 bool resolveRelocation(uint8_t *Address, uint8_t *Value, bool isPCRel,
182 unsigned Type, unsigned Size);
183 bool resolveX86_64Relocation(uintptr_t Address, uintptr_t Value, bool isPCRel,
184 unsigned Type, unsigned Size);
185 bool resolveARMRelocation(uintptr_t Address, uintptr_t Value, bool isPCRel,
186 unsigned Type, unsigned Size);
188 bool loadSegment32(const MachOObject *Obj,
189 const MachOObject::LoadCommandInfo *SegmentLCI,
190 const InMemoryStruct<macho::SymtabLoadCommand> &SymtabLC);
191 bool loadSegment64(const MachOObject *Obj,
192 const MachOObject::LoadCommandInfo *SegmentLCI,
193 const InMemoryStruct<macho::SymtabLoadCommand> &SymtabLC);
196 RuntimeDyldMachO(RTDyldMemoryManager *mm) : RuntimeDyldImpl(mm) {}
198 bool loadObject(MemoryBuffer *InputBuffer);
200 void reassignSymbolAddress(StringRef Name, uint8_t *Addr);
202 static bool isKnownFormat(const MemoryBuffer *InputBuffer);
204 bool isCompatibleFormat(const MemoryBuffer *InputBuffer) const {
205 return isKnownFormat(InputBuffer);
209 } // end namespace llvm