blob: cff7cbdf28ad996fc77e3035f2705535d024a1c4 [file] [log] [blame]
Danil Malyshevcf852dc2011-07-13 07:57:58 +00001//===-- RuntimeDyldImpl.h - Run-time dynamic linker for MC-JIT ------*- C++ -*-===//
2//
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"
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"
30
31using namespace llvm;
32using namespace llvm::object;
33
34namespace llvm {
35class RuntimeDyldImpl {
36protected:
37 unsigned CPUType;
38 unsigned CPUSubtype;
39
40 // The MemoryManager to load objects into.
41 RTDyldMemoryManager *MemMgr;
42
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.
47
48 // For each function, we have a MemoryBlock of it's instruction data.
49 StringMap<sys::MemoryBlock> Functions;
50
51 // Master symbol table. As modules are loaded and external symbols are
52 // resolved, their addresses are stored here.
53 StringMap<uint8_t*> SymbolTable;
54
55 bool HasError;
56 std::string ErrorStr;
57
58 // Set the error state and record an error string.
59 bool Error(const Twine &Msg) {
60 ErrorStr = Msg.str();
61 HasError = true;
62 return true;
63 }
64
65 void extractFunction(StringRef Name, uint8_t *StartAddress,
66 uint8_t *EndAddress);
67
68public:
69 RuntimeDyldImpl(RTDyldMemoryManager *mm) : MemMgr(mm), HasError(false) {}
70
71 virtual ~RuntimeDyldImpl();
72
73 virtual bool loadObject(MemoryBuffer *InputBuffer) = 0;
74
75 void *getSymbolAddress(StringRef Name) {
76 // FIXME: Just look up as a function for now. Overly simple of course.
77 // Work in progress.
78 return SymbolTable.lookup(Name);
79 }
80
81 void resolveRelocations();
82
83 virtual void reassignSymbolAddress(StringRef Name, uint8_t *Addr) = 0;
84
85 // Is the linker in an error state?
86 bool hasError() { return HasError; }
87
88 // Mark the error condition as handled and continue.
89 void clearError() { HasError = false; }
90
91 // Get the error message.
92 StringRef getErrorString() { return ErrorStr; }
93
94 virtual bool isCompatibleFormat(const MemoryBuffer *InputBuffer) const = 0;
95};
96
Eli Benderskya66a1852012-01-16 08:56:09 +000097class 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.
103 std::string Target;
104 // Offset into the target function or section for the relocation.
105 uint32_t Offset;
106 // Relocation type
107 uint32_t Type;
108 // Addend encoded in the instruction itself, if any.
109 int32_t Addend;
110 // Has the relocation been recalcuated as an offset within a function?
111 bool IsFunctionRelative;
112 // Has this relocation been resolved previously?
113 bool isResolved;
114
115 RelocationEntry(StringRef t,
116 uint32_t offset,
117 uint32_t type,
118 int32_t addend,
119 bool isFunctionRelative)
120 : Target(t)
121 , Offset(offset)
122 , Type(type)
123 , Addend(addend)
124 , IsFunctionRelative(isFunctionRelative)
125 , isResolved(false) { }
126 };
127 typedef SmallVector<RelocationEntry, 4> RelocationList;
128 StringMap<RelocationList> Relocations;
129 unsigned Arch;
130
131 void resolveX86_64Relocation(StringRef Name,
132 uint8_t *Addr,
133 const RelocationEntry &RE);
134
135 void resolveX86Relocation(StringRef Name,
136 uint8_t *Addr,
137 const RelocationEntry &RE);
138
139 void resolveArmRelocation(StringRef Name,
140 uint8_t *Addr,
141 const RelocationEntry &RE);
142
143 void resolveRelocation(StringRef Name,
144 uint8_t *Addr,
145 const RelocationEntry &RE);
146
147public:
148 RuntimeDyldELF(RTDyldMemoryManager *mm) : RuntimeDyldImpl(mm) {}
149
150 bool loadObject(MemoryBuffer *InputBuffer);
151
152 void reassignSymbolAddress(StringRef Name, uint8_t *Addr);
153
154 bool isCompatibleFormat(const MemoryBuffer *InputBuffer) const;
155};
156
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000157
158class RuntimeDyldMachO : public RuntimeDyldImpl {
159
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?
169
170 RelocationEntry(StringRef t, uint64_t offset, uint32_t data, int64_t addend)
171 : Target(t), Offset(offset), Data(data), Addend(addend),
172 isResolved(false) {}
173 };
174 typedef SmallVector<RelocationEntry, 4> RelocationList;
175 StringMap<RelocationList> Relocations;
176
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.
180
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);
187
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);
194
195public:
196 RuntimeDyldMachO(RTDyldMemoryManager *mm) : RuntimeDyldImpl(mm) {}
197
198 bool loadObject(MemoryBuffer *InputBuffer);
199
200 void reassignSymbolAddress(StringRef Name, uint8_t *Addr);
201
202 static bool isKnownFormat(const MemoryBuffer *InputBuffer);
203
204 bool isCompatibleFormat(const MemoryBuffer *InputBuffer) const {
205 return isKnownFormat(InputBuffer);
Eric Christophercf1e9672011-07-20 02:44:39 +0000206 }
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000207};
208
209} // end namespace llvm
210
211
212#endif