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;
98 class RuntimeDyldMachO : public RuntimeDyldImpl {
100 // For each symbol, keep a list of relocations based on it. Anytime
101 // its address is reassigned (the JIT re-compiled the function, e.g.),
102 // the relocations get re-resolved.
103 struct RelocationEntry {
104 std::string Target; // Object this relocation is contained in.
105 uint64_t Offset; // Offset into the object for the relocation.
106 uint32_t Data; // Second word of the raw macho relocation entry.
107 int64_t Addend; // Addend encoded in the instruction itself, if any.
108 bool isResolved; // Has this relocation been resolved previously?
110 RelocationEntry(StringRef t, uint64_t offset, uint32_t data, int64_t addend)
111 : Target(t), Offset(offset), Data(data), Addend(addend),
114 typedef SmallVector<RelocationEntry, 4> RelocationList;
115 StringMap<RelocationList> Relocations;
117 // FIXME: Also keep a map of all the relocations contained in an object. Use
118 // this to dynamically answer whether all of the relocations in it have
119 // been resolved or not.
121 bool resolveRelocation(uint8_t *Address, uint8_t *Value, bool isPCRel,
122 unsigned Type, unsigned Size);
123 bool resolveX86_64Relocation(uintptr_t Address, uintptr_t Value, bool isPCRel,
124 unsigned Type, unsigned Size);
125 bool resolveARMRelocation(uintptr_t Address, uintptr_t Value, bool isPCRel,
126 unsigned Type, unsigned Size);
128 bool loadSegment32(const MachOObject *Obj,
129 const MachOObject::LoadCommandInfo *SegmentLCI,
130 const InMemoryStruct<macho::SymtabLoadCommand> &SymtabLC);
131 bool loadSegment64(const MachOObject *Obj,
132 const MachOObject::LoadCommandInfo *SegmentLCI,
133 const InMemoryStruct<macho::SymtabLoadCommand> &SymtabLC);
136 RuntimeDyldMachO(RTDyldMemoryManager *mm) : RuntimeDyldImpl(mm) {}
138 bool loadObject(MemoryBuffer *InputBuffer);
140 void reassignSymbolAddress(StringRef Name, uint8_t *Addr);
142 static bool isKnownFormat(const MemoryBuffer *InputBuffer);
144 bool isCompatibleFormat(const MemoryBuffer *InputBuffer) const {
145 return isKnownFormat(InputBuffer);
149 } // end namespace llvm