blob: 066ecebaa259326d2356a9d80f706c927bee2117 [file] [log] [blame]
Daniel Dunbar6aec2982010-11-17 16:06:43 +00001//===-- MCJIT.h - Class definition for the MCJIT ----------------*- 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#ifndef LLVM_LIB_EXECUTIONENGINE_MCJIT_H
11#define LLVM_LIB_EXECUTIONENGINE_MCJIT_H
12
Andrew Kaylor8e9ec012013-10-01 01:47:35 +000013#include "llvm/ADT/DenseMap.h"
Andrew Kaylor2ad18ef2013-10-24 00:19:14 +000014#include "llvm/ADT/SmallPtrSet.h"
Andrew Kaylor776054d2012-11-06 18:51:59 +000015#include "llvm/ADT/SmallVector.h"
Daniel Dunbar6aec2982010-11-17 16:06:43 +000016#include "llvm/ExecutionEngine/ExecutionEngine.h"
Andrew Kaylor1c489452013-04-25 21:02:36 +000017#include "llvm/ExecutionEngine/ObjectCache.h"
Andrew Kaylor8e9ec012013-10-01 01:47:35 +000018#include "llvm/ExecutionEngine/ObjectImage.h"
Jim Grosbachf9229102011-03-22 01:06:42 +000019#include "llvm/ExecutionEngine/RuntimeDyld.h"
Andrew Kaylor2ad18ef2013-10-24 00:19:14 +000020#include "llvm/IR/Module.h"
Daniel Dunbar6aec2982010-11-17 16:06:43 +000021
22namespace llvm {
Andrew Kaylor8e9ec012013-10-01 01:47:35 +000023class MCJIT;
24
25// This is a helper class that the MCJIT execution engine uses for linking
26// functions across modules that it owns. It aggregates the memory manager
27// that is passed in to the MCJIT constructor and defers most functionality
28// to that object.
29class LinkingMemoryManager : public RTDyldMemoryManager {
30public:
31 LinkingMemoryManager(MCJIT *Parent, RTDyldMemoryManager *MM)
32 : ParentEngine(Parent), ClientMM(MM) {}
33
Stephen Hines36b56882014-04-23 16:57:46 -070034 uint64_t getSymbolAddress(const std::string &Name) override;
Andrew Kaylor8e9ec012013-10-01 01:47:35 +000035
36 // Functions deferred to client memory manager
Stephen Hines36b56882014-04-23 16:57:46 -070037 uint8_t *allocateCodeSection(uintptr_t Size, unsigned Alignment,
38 unsigned SectionID,
39 StringRef SectionName) override {
Filip Pizlo6eb43d22013-10-02 00:59:25 +000040 return ClientMM->allocateCodeSection(Size, Alignment, SectionID, SectionName);
Andrew Kaylor8e9ec012013-10-01 01:47:35 +000041 }
42
Stephen Hines36b56882014-04-23 16:57:46 -070043 uint8_t *allocateDataSection(uintptr_t Size, unsigned Alignment,
44 unsigned SectionID, StringRef SectionName,
45 bool IsReadOnly) override {
Andrew Kaylor8e9ec012013-10-01 01:47:35 +000046 return ClientMM->allocateDataSection(Size, Alignment,
Filip Pizlo6eb43d22013-10-02 00:59:25 +000047 SectionID, SectionName, IsReadOnly);
Andrew Kaylor8e9ec012013-10-01 01:47:35 +000048 }
49
Stephen Hines36b56882014-04-23 16:57:46 -070050 void reserveAllocationSpace(uintptr_t CodeSize, uintptr_t DataSizeRO,
51 uintptr_t DataSizeRW) override {
52 return ClientMM->reserveAllocationSpace(CodeSize, DataSizeRO, DataSizeRW);
53 }
54
55 bool needsToReserveAllocationSpace() override {
56 return ClientMM->needsToReserveAllocationSpace();
57 }
58
59 void notifyObjectLoaded(ExecutionEngine *EE,
60 const ObjectImage *Obj) override {
Andrew Kaylorb868e912013-10-04 00:49:38 +000061 ClientMM->notifyObjectLoaded(EE, Obj);
62 }
63
Stephen Hines36b56882014-04-23 16:57:46 -070064 void registerEHFrames(uint8_t *Addr, uint64_t LoadAddr,
65 size_t Size) override {
Andrew Kaylor528f6d72013-10-11 21:25:48 +000066 ClientMM->registerEHFrames(Addr, LoadAddr, Size);
Andrew Kaylor8e9ec012013-10-01 01:47:35 +000067 }
68
Stephen Hines36b56882014-04-23 16:57:46 -070069 void deregisterEHFrames(uint8_t *Addr, uint64_t LoadAddr,
70 size_t Size) override {
Andrew Kaylor43507d02013-10-16 00:14:21 +000071 ClientMM->deregisterEHFrames(Addr, LoadAddr, Size);
72 }
73
Stephen Hines36b56882014-04-23 16:57:46 -070074 bool finalizeMemory(std::string *ErrMsg = 0) override {
Andrew Kaylor8e9ec012013-10-01 01:47:35 +000075 return ClientMM->finalizeMemory(ErrMsg);
76 }
77
78private:
79 MCJIT *ParentEngine;
Stephen Hines36b56882014-04-23 16:57:46 -070080 std::unique_ptr<RTDyldMemoryManager> ClientMM;
Andrew Kaylor8e9ec012013-10-01 01:47:35 +000081};
Andrew Kaylor3f23cef2012-10-02 21:18:39 +000082
Andrew Kaylor2ad18ef2013-10-24 00:19:14 +000083// About Module states: added->loaded->finalized.
Yaron Kerenf14bb8f2013-10-19 09:03:20 +000084//
85// The purpose of the "added" state is having modules in standby. (added=known
86// but not compiled). The idea is that you can add a module to provide function
87// definitions but if nothing in that module is referenced by a module in which
Yaron Keren3985f6a2013-10-24 10:04:47 +000088// a function is executed (note the wording here because it's not exactly the
Yaron Kerenf14bb8f2013-10-19 09:03:20 +000089// ideal case) then the module never gets compiled. This is sort of lazy
90// compilation.
91//
92// The purpose of the "loaded" state (loaded=compiled and required sections
93// copied into local memory but not yet ready for execution) is to have an
94// intermediate state wherein clients can remap the addresses of sections, using
95// MCJIT::mapSectionAddress, (in preparation for later copying to a new location
96// or an external process) before relocations and page permissions are applied.
97//
98// It might not be obvious at first glance, but the "remote-mcjit" case in the
99// lli tool does this. In that case, the intermediate action is taken by the
100// RemoteMemoryManager in response to the notifyObjectLoaded function being
101// called.
102
Daniel Dunbar6aec2982010-11-17 16:06:43 +0000103class MCJIT : public ExecutionEngine {
Jim Grosbach8005bcd2012-08-21 15:42:49 +0000104 MCJIT(Module *M, TargetMachine *tm, RTDyldMemoryManager *MemMgr,
105 bool AllocateGVsWithCode);
Jim Grosbach31649e62011-03-18 22:48:41 +0000106
Andrew Kaylor2ad18ef2013-10-24 00:19:14 +0000107 typedef llvm::SmallPtrSet<Module *, 4> ModulePtrSet;
Andrew Kaylor8e9ec012013-10-01 01:47:35 +0000108
Andrew Kaylor2ad18ef2013-10-24 00:19:14 +0000109 class OwningModuleContainer {
Andrew Kaylor8e9ec012013-10-01 01:47:35 +0000110 public:
Andrew Kaylor2ad18ef2013-10-24 00:19:14 +0000111 OwningModuleContainer() {
112 }
113 ~OwningModuleContainer() {
114 freeModulePtrSet(AddedModules);
115 freeModulePtrSet(LoadedModules);
116 freeModulePtrSet(FinalizedModules);
117 }
Andrew Kaylor8e9ec012013-10-01 01:47:35 +0000118
Andrew Kaylor2ad18ef2013-10-24 00:19:14 +0000119 ModulePtrSet::iterator begin_added() { return AddedModules.begin(); }
120 ModulePtrSet::iterator end_added() { return AddedModules.end(); }
121
122 ModulePtrSet::iterator begin_loaded() { return LoadedModules.begin(); }
123 ModulePtrSet::iterator end_loaded() { return LoadedModules.end(); }
124
125 ModulePtrSet::iterator begin_finalized() { return FinalizedModules.begin(); }
126 ModulePtrSet::iterator end_finalized() { return FinalizedModules.end(); }
127
128 void addModule(Module *M) {
129 AddedModules.insert(M);
130 }
131
132 bool removeModule(Module *M) {
133 return AddedModules.erase(M) || LoadedModules.erase(M) ||
134 FinalizedModules.erase(M);
135 }
136
137 bool hasModuleBeenAddedButNotLoaded(Module *M) {
138 return AddedModules.count(M) != 0;
139 }
140
141 bool hasModuleBeenLoaded(Module *M) {
142 // If the module is in either the "loaded" or "finalized" sections it
143 // has been loaded.
144 return (LoadedModules.count(M) != 0 ) || (FinalizedModules.count(M) != 0);
145 }
146
147 bool hasModuleBeenFinalized(Module *M) {
148 return FinalizedModules.count(M) != 0;
149 }
150
151 bool ownsModule(Module* M) {
152 return (AddedModules.count(M) != 0) || (LoadedModules.count(M) != 0) ||
153 (FinalizedModules.count(M) != 0);
154 }
155
156 void markModuleAsLoaded(Module *M) {
157 // This checks against logic errors in the MCJIT implementation.
158 // This function should never be called with either a Module that MCJIT
159 // does not own or a Module that has already been loaded and/or finalized.
160 assert(AddedModules.count(M) &&
161 "markModuleAsLoaded: Module not found in AddedModules");
162
163 // Remove the module from the "Added" set.
164 AddedModules.erase(M);
165
166 // Add the Module to the "Loaded" set.
167 LoadedModules.insert(M);
168 }
169
170 void markModuleAsFinalized(Module *M) {
171 // This checks against logic errors in the MCJIT implementation.
172 // This function should never be called with either a Module that MCJIT
173 // does not own, a Module that has not been loaded or a Module that has
174 // already been finalized.
175 assert(LoadedModules.count(M) &&
176 "markModuleAsFinalized: Module not found in LoadedModules");
177
178 // Remove the module from the "Loaded" section of the list.
179 LoadedModules.erase(M);
180
181 // Add the Module to the "Finalized" section of the list by inserting it
182 // before the 'end' iterator.
183 FinalizedModules.insert(M);
184 }
185
186 void markAllLoadedModulesAsFinalized() {
187 for (ModulePtrSet::iterator I = LoadedModules.begin(),
188 E = LoadedModules.end();
189 I != E; ++I) {
190 Module *M = *I;
191 FinalizedModules.insert(M);
192 }
193 LoadedModules.clear();
194 }
Andrew Kaylor8e9ec012013-10-01 01:47:35 +0000195
196 private:
Andrew Kaylor2ad18ef2013-10-24 00:19:14 +0000197 ModulePtrSet AddedModules;
198 ModulePtrSet LoadedModules;
199 ModulePtrSet FinalizedModules;
200
201 void freeModulePtrSet(ModulePtrSet& MPS) {
202 // Go through the module set and delete everything.
203 for (ModulePtrSet::iterator I = MPS.begin(), E = MPS.end(); I != E; ++I) {
204 Module *M = *I;
205 delete M;
206 }
207 MPS.clear();
208 }
Andrew Kaylor8e9ec012013-10-01 01:47:35 +0000209 };
210
Jim Grosbach31649e62011-03-18 22:48:41 +0000211 TargetMachine *TM;
212 MCContext *Ctx;
Andrew Kaylor8e9ec012013-10-01 01:47:35 +0000213 LinkingMemoryManager MemMgr;
Andrew Kaylorea708d12012-08-07 18:33:00 +0000214 RuntimeDyld Dyld;
Andrew Kaylor776054d2012-11-06 18:51:59 +0000215 SmallVector<JITEventListener*, 2> EventListeners;
Jim Grosbach31649e62011-03-18 22:48:41 +0000216
Andrew Kaylor2ad18ef2013-10-24 00:19:14 +0000217 OwningModuleContainer OwnedModules;
Andrew Kaylor8e9ec012013-10-01 01:47:35 +0000218
Stephen Hines36b56882014-04-23 16:57:46 -0700219 SmallVector<object::Archive*, 2> Archives;
220
221 typedef SmallVector<ObjectImage *, 2> LoadedObjectList;
222 LoadedObjectList LoadedObjects;
Jim Grosbach31649e62011-03-18 22:48:41 +0000223
Andrew Kaylor1c489452013-04-25 21:02:36 +0000224 // An optional ObjectCache to be notified of compiled objects and used to
225 // perform lookup of pre-compiled code to avoid re-compilation.
226 ObjectCache *ObjCache;
227
Andrew Kaylor2ad18ef2013-10-24 00:19:14 +0000228 Function *FindFunctionNamedInModulePtrSet(const char *FnName,
229 ModulePtrSet::iterator I,
230 ModulePtrSet::iterator E);
231
232 void runStaticConstructorsDestructorsInModulePtrSet(bool isDtors,
233 ModulePtrSet::iterator I,
234 ModulePtrSet::iterator E);
235
Daniel Dunbar6aec2982010-11-17 16:06:43 +0000236public:
237 ~MCJIT();
238
239 /// @name ExecutionEngine interface implementation
240 /// @{
Stephen Hines36b56882014-04-23 16:57:46 -0700241 void addModule(Module *M) override;
242 void addObjectFile(object::ObjectFile *O) override;
243 void addArchive(object::Archive *O) override;
244 bool removeModule(Module *M) override;
Andrew Kaylor2ad18ef2013-10-24 00:19:14 +0000245
246 /// FindFunctionNamed - Search all of the active modules to find the one that
247 /// defines FnName. This is very slow operation and shouldn't be used for
248 /// general code.
Stephen Hines36b56882014-04-23 16:57:46 -0700249 Function *FindFunctionNamed(const char *FnName) override;
Daniel Dunbar6aec2982010-11-17 16:06:43 +0000250
Andrew Kaylor1c489452013-04-25 21:02:36 +0000251 /// Sets the object manager that MCJIT should use to avoid compilation.
Stephen Hines36b56882014-04-23 16:57:46 -0700252 void setObjectCache(ObjectCache *manager) override;
Andrew Kaylor1c489452013-04-25 21:02:36 +0000253
Stephen Hines36b56882014-04-23 16:57:46 -0700254 void setProcessAllSections(bool ProcessAllSections) override {
255 Dyld.setProcessAllSections(ProcessAllSections);
256 }
257
258 void generateCodeForModule(Module *M) override;
Andrew Kaylor8e9ec012013-10-01 01:47:35 +0000259
David Tweedabb38fe2013-05-17 10:01:46 +0000260 /// finalizeObject - ensure the module is fully processed and is usable.
261 ///
262 /// It is the user-level function for completing the process of making the
263 /// object usable for execution. It should be called after sections within an
264 /// object have been relocated using mapSectionAddress. When this method is
265 /// called the MCJIT execution engine will reapply relocations for a loaded
266 /// object.
Yaron Kerenf14bb8f2013-10-19 09:03:20 +0000267 /// Is it OK to finalize a set of modules, add modules and finalize again.
Andrew Kaylor5e206fc2013-10-21 23:27:02 +0000268 // FIXME: Do we really need both of these?
Stephen Hines36b56882014-04-23 16:57:46 -0700269 void finalizeObject() override;
Andrew Kaylor8e9ec012013-10-01 01:47:35 +0000270 virtual void finalizeModule(Module *);
271 void finalizeLoadedModules();
Andrew Kaylor28989882012-11-05 20:57:16 +0000272
Andrew Kaylor2ad18ef2013-10-24 00:19:14 +0000273 /// runStaticConstructorsDestructors - This method is used to execute all of
274 /// the static constructors or destructors for a program.
275 ///
276 /// \param isDtors - Run the destructors instead of constructors.
Stephen Hines36b56882014-04-23 16:57:46 -0700277 void runStaticConstructorsDestructors(bool isDtors) override;
Andrew Kaylor2ad18ef2013-10-24 00:19:14 +0000278
Stephen Hines36b56882014-04-23 16:57:46 -0700279 void *getPointerToBasicBlock(BasicBlock *BB) override;
Daniel Dunbar6aec2982010-11-17 16:06:43 +0000280
Stephen Hines36b56882014-04-23 16:57:46 -0700281 void *getPointerToFunction(Function *F) override;
Daniel Dunbar6aec2982010-11-17 16:06:43 +0000282
Stephen Hines36b56882014-04-23 16:57:46 -0700283 void *recompileAndRelinkFunction(Function *F) override;
Daniel Dunbar6aec2982010-11-17 16:06:43 +0000284
Stephen Hines36b56882014-04-23 16:57:46 -0700285 void freeMachineCodeForFunction(Function *F) override;
Daniel Dunbar6aec2982010-11-17 16:06:43 +0000286
Stephen Hines36b56882014-04-23 16:57:46 -0700287 GenericValue runFunction(Function *F,
288 const std::vector<GenericValue> &ArgValues) override;
Daniel Dunbar6aec2982010-11-17 16:06:43 +0000289
Jim Grosbach34714a02011-03-22 18:05:27 +0000290 /// getPointerToNamedFunction - This method returns the address of the
291 /// specified function by using the dlsym function call. As such it is only
292 /// useful for resolving library symbols, not code generated symbols.
293 ///
294 /// If AbortOnFailure is false and no function with the given name is
295 /// found, this function silently returns a null pointer. Otherwise,
296 /// it prints a message to stderr and aborts.
297 ///
Stephen Hines36b56882014-04-23 16:57:46 -0700298 void *getPointerToNamedFunction(const std::string &Name,
299 bool AbortOnFailure = true) override;
Danil Malyshev30b9e322012-03-28 21:46:36 +0000300
Jim Grosbach020f4e82012-01-16 23:50:55 +0000301 /// mapSectionAddress - map a section to its target address space value.
302 /// Map the address of a JIT section as returned from the memory manager
303 /// to the address in the target process as the running code will see it.
304 /// This is the address which will be used for relocation resolution.
Stephen Hines36b56882014-04-23 16:57:46 -0700305 void mapSectionAddress(const void *LocalAddress,
306 uint64_t TargetAddress) override {
Jim Grosbach020f4e82012-01-16 23:50:55 +0000307 Dyld.mapSectionAddress(LocalAddress, TargetAddress);
308 }
Stephen Hines36b56882014-04-23 16:57:46 -0700309 void RegisterJITEventListener(JITEventListener *L) override;
310 void UnregisterJITEventListener(JITEventListener *L) override;
Andrew Kaylor776054d2012-11-06 18:51:59 +0000311
Andrew Kaylor8e9ec012013-10-01 01:47:35 +0000312 // If successful, these function will implicitly finalize all loaded objects.
313 // To get a function address within MCJIT without causing a finalize, use
314 // getSymbolAddress.
Stephen Hines36b56882014-04-23 16:57:46 -0700315 uint64_t getGlobalValueAddress(const std::string &Name) override;
316 uint64_t getFunctionAddress(const std::string &Name) override;
317
318 TargetMachine *getTargetMachine() override { return TM; }
Andrew Kaylor8e9ec012013-10-01 01:47:35 +0000319
Daniel Dunbar6aec2982010-11-17 16:06:43 +0000320 /// @}
321 /// @name (Private) Registration Interfaces
322 /// @{
323
324 static void Register() {
325 MCJITCtor = createJIT;
326 }
327
Daniel Dunbar6aec2982010-11-17 16:06:43 +0000328 static ExecutionEngine *createJIT(Module *M,
329 std::string *ErrorStr,
Filip Pizlo13a3cf12013-05-14 19:29:00 +0000330 RTDyldMemoryManager *MemMgr,
Daniel Dunbar6aec2982010-11-17 16:06:43 +0000331 bool GVsWithCode,
Dylan Noblesmithc5b28582011-05-13 21:51:29 +0000332 TargetMachine *TM);
Daniel Dunbar6aec2982010-11-17 16:06:43 +0000333
334 // @}
Andrew Kaylorea708d12012-08-07 18:33:00 +0000335
Andrew Kaylor8e9ec012013-10-01 01:47:35 +0000336 // This is not directly exposed via the ExecutionEngine API, but it is
337 // used by the LinkingMemoryManager.
338 uint64_t getSymbolAddress(const std::string &Name,
339 bool CheckFunctionsOnly);
340
Andrew Kaylorea708d12012-08-07 18:33:00 +0000341protected:
342 /// emitObject -- Generate a JITed object in memory from the specified module
343 /// Currently, MCJIT only supports a single module and the module passed to
344 /// this function call is expected to be the contained module. The module
Stephen Hines36b56882014-04-23 16:57:46 -0700345 /// is passed as a parameter here to prepare for multiple module support in
Andrew Kaylorea708d12012-08-07 18:33:00 +0000346 /// the future.
Andrew Kaylor1c489452013-04-25 21:02:36 +0000347 ObjectBufferStream* emitObject(Module *M);
348
Andrew Kaylor776054d2012-11-06 18:51:59 +0000349 void NotifyObjectEmitted(const ObjectImage& Obj);
350 void NotifyFreeingObject(const ObjectImage& Obj);
Andrew Kaylor8e9ec012013-10-01 01:47:35 +0000351
352 uint64_t getExistingSymbolAddress(const std::string &Name);
353 Module *findModuleForSymbol(const std::string &Name,
354 bool CheckFunctionsOnly);
Daniel Dunbar6aec2982010-11-17 16:06:43 +0000355};
356
357} // End llvm namespace
358
359#endif