blob: a45173c2da8d284c379332d160edfca1e0c3598e [file] [log] [blame]
Daniel Dunbar7e5d8a72010-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
Benjamin Kramera7c40ef2014-08-13 16:26:38 +000010#ifndef LLVM_LIB_EXECUTIONENGINE_MCJIT_MCJIT_H
11#define LLVM_LIB_EXECUTIONENGINE_MCJIT_MCJIT_H
Daniel Dunbar7e5d8a72010-11-17 16:06:43 +000012
Andrew Kaylorea395922013-10-01 01:47:35 +000013#include "llvm/ADT/DenseMap.h"
Andrew Kaylorc89fc822013-10-24 00:19:14 +000014#include "llvm/ADT/SmallPtrSet.h"
Andrew Kaylord8ffd9c2012-11-06 18:51:59 +000015#include "llvm/ADT/SmallVector.h"
Daniel Dunbar7e5d8a72010-11-17 16:06:43 +000016#include "llvm/ExecutionEngine/ExecutionEngine.h"
Andrew Kaylorced4e8f2013-04-25 21:02:36 +000017#include "llvm/ExecutionEngine/ObjectCache.h"
Lang Hames93de2a12015-01-23 21:25:00 +000018#include "llvm/ExecutionEngine/ObjectMemoryBuffer.h"
Lang Hames633fe142015-03-30 03:37:06 +000019#include "llvm/ExecutionEngine/RTDyldMemoryManager.h"
Jim Grosbach348a5482011-03-22 01:06:42 +000020#include "llvm/ExecutionEngine/RuntimeDyld.h"
Andrew Kaylorc89fc822013-10-24 00:19:14 +000021#include "llvm/IR/Module.h"
Daniel Dunbar7e5d8a72010-11-17 16:06:43 +000022
23namespace llvm {
Andrew Kaylorea395922013-10-01 01:47:35 +000024class MCJIT;
25
26// This is a helper class that the MCJIT execution engine uses for linking
27// functions across modules that it owns. It aggregates the memory manager
28// that is passed in to the MCJIT constructor and defers most functionality
29// to that object.
Lang Hames633fe142015-03-30 03:37:06 +000030class LinkingSymbolResolver : public RuntimeDyld::SymbolResolver {
Andrew Kaylorea395922013-10-01 01:47:35 +000031public:
Lang Hames633fe142015-03-30 03:37:06 +000032 LinkingSymbolResolver(MCJIT &Parent,
33 std::shared_ptr<RuntimeDyld::SymbolResolver> Resolver)
34 : ParentEngine(Parent), ClientResolver(std::move(Resolver)) {}
Andrew Kaylorea395922013-10-01 01:47:35 +000035
Lang Hames633fe142015-03-30 03:37:06 +000036 RuntimeDyld::SymbolInfo findSymbol(const std::string &Name) override;
Andrew Kaylorea395922013-10-01 01:47:35 +000037
Lang Hames633fe142015-03-30 03:37:06 +000038 // MCJIT doesn't support logical dylibs.
39 RuntimeDyld::SymbolInfo
40 findSymbolInLogicalDylib(const std::string &Name) override {
41 return nullptr;
Andrew Kaylorea395922013-10-01 01:47:35 +000042 }
43
44private:
Lang Hames633fe142015-03-30 03:37:06 +000045 MCJIT &ParentEngine;
46 std::shared_ptr<RuntimeDyld::SymbolResolver> ClientResolver;
Andrew Kaylorea395922013-10-01 01:47:35 +000047};
Andrew Kayloradc70562012-10-02 21:18:39 +000048
Andrew Kaylorc89fc822013-10-24 00:19:14 +000049// About Module states: added->loaded->finalized.
Yaron Kerenfb955822013-10-19 09:03:20 +000050//
51// The purpose of the "added" state is having modules in standby. (added=known
52// but not compiled). The idea is that you can add a module to provide function
53// definitions but if nothing in that module is referenced by a module in which
Yaron Keren1ec9df32013-10-24 10:04:47 +000054// a function is executed (note the wording here because it's not exactly the
Yaron Kerenfb955822013-10-19 09:03:20 +000055// ideal case) then the module never gets compiled. This is sort of lazy
56// compilation.
57//
58// The purpose of the "loaded" state (loaded=compiled and required sections
59// copied into local memory but not yet ready for execution) is to have an
60// intermediate state wherein clients can remap the addresses of sections, using
61// MCJIT::mapSectionAddress, (in preparation for later copying to a new location
62// or an external process) before relocations and page permissions are applied.
63//
64// It might not be obvious at first glance, but the "remote-mcjit" case in the
65// lli tool does this. In that case, the intermediate action is taken by the
66// RemoteMemoryManager in response to the notifyObjectLoaded function being
67// called.
68
Daniel Dunbar7e5d8a72010-11-17 16:06:43 +000069class MCJIT : public ExecutionEngine {
David Blaikie196e3232014-09-02 22:41:07 +000070 MCJIT(std::unique_ptr<Module> M, std::unique_ptr<TargetMachine> tm,
Lang Hames633fe142015-03-30 03:37:06 +000071 std::shared_ptr<MCJITMemoryManager> MemMgr,
72 std::shared_ptr<RuntimeDyld::SymbolResolver> Resolver);
Jim Grosbach7b162492011-03-18 22:48:41 +000073
Andrew Kaylorc89fc822013-10-24 00:19:14 +000074 typedef llvm::SmallPtrSet<Module *, 4> ModulePtrSet;
Andrew Kaylorea395922013-10-01 01:47:35 +000075
Andrew Kaylorc89fc822013-10-24 00:19:14 +000076 class OwningModuleContainer {
Andrew Kaylorea395922013-10-01 01:47:35 +000077 public:
Andrew Kaylorc89fc822013-10-24 00:19:14 +000078 OwningModuleContainer() {
79 }
80 ~OwningModuleContainer() {
81 freeModulePtrSet(AddedModules);
82 freeModulePtrSet(LoadedModules);
83 freeModulePtrSet(FinalizedModules);
84 }
Andrew Kaylorea395922013-10-01 01:47:35 +000085
Andrew Kaylorc89fc822013-10-24 00:19:14 +000086 ModulePtrSet::iterator begin_added() { return AddedModules.begin(); }
87 ModulePtrSet::iterator end_added() { return AddedModules.end(); }
Lang Hames018452e2014-09-05 23:38:35 +000088 iterator_range<ModulePtrSet::iterator> added() {
89 return iterator_range<ModulePtrSet::iterator>(begin_added(), end_added());
90 }
Andrew Kaylorc89fc822013-10-24 00:19:14 +000091
92 ModulePtrSet::iterator begin_loaded() { return LoadedModules.begin(); }
93 ModulePtrSet::iterator end_loaded() { return LoadedModules.end(); }
94
95 ModulePtrSet::iterator begin_finalized() { return FinalizedModules.begin(); }
96 ModulePtrSet::iterator end_finalized() { return FinalizedModules.end(); }
97
Rafael Espindola2a8a2792014-08-19 04:04:25 +000098 void addModule(std::unique_ptr<Module> M) {
99 AddedModules.insert(M.release());
Andrew Kaylorc89fc822013-10-24 00:19:14 +0000100 }
101
102 bool removeModule(Module *M) {
103 return AddedModules.erase(M) || LoadedModules.erase(M) ||
104 FinalizedModules.erase(M);
105 }
106
107 bool hasModuleBeenAddedButNotLoaded(Module *M) {
108 return AddedModules.count(M) != 0;
109 }
110
111 bool hasModuleBeenLoaded(Module *M) {
112 // If the module is in either the "loaded" or "finalized" sections it
113 // has been loaded.
114 return (LoadedModules.count(M) != 0 ) || (FinalizedModules.count(M) != 0);
115 }
116
117 bool hasModuleBeenFinalized(Module *M) {
118 return FinalizedModules.count(M) != 0;
119 }
120
121 bool ownsModule(Module* M) {
122 return (AddedModules.count(M) != 0) || (LoadedModules.count(M) != 0) ||
123 (FinalizedModules.count(M) != 0);
124 }
125
126 void markModuleAsLoaded(Module *M) {
127 // This checks against logic errors in the MCJIT implementation.
128 // This function should never be called with either a Module that MCJIT
129 // does not own or a Module that has already been loaded and/or finalized.
130 assert(AddedModules.count(M) &&
131 "markModuleAsLoaded: Module not found in AddedModules");
132
133 // Remove the module from the "Added" set.
134 AddedModules.erase(M);
135
136 // Add the Module to the "Loaded" set.
137 LoadedModules.insert(M);
138 }
139
140 void markModuleAsFinalized(Module *M) {
141 // This checks against logic errors in the MCJIT implementation.
142 // This function should never be called with either a Module that MCJIT
143 // does not own, a Module that has not been loaded or a Module that has
144 // already been finalized.
145 assert(LoadedModules.count(M) &&
146 "markModuleAsFinalized: Module not found in LoadedModules");
147
148 // Remove the module from the "Loaded" section of the list.
149 LoadedModules.erase(M);
150
151 // Add the Module to the "Finalized" section of the list by inserting it
152 // before the 'end' iterator.
153 FinalizedModules.insert(M);
154 }
155
156 void markAllLoadedModulesAsFinalized() {
157 for (ModulePtrSet::iterator I = LoadedModules.begin(),
158 E = LoadedModules.end();
159 I != E; ++I) {
160 Module *M = *I;
161 FinalizedModules.insert(M);
162 }
163 LoadedModules.clear();
164 }
Andrew Kaylorea395922013-10-01 01:47:35 +0000165
166 private:
Andrew Kaylorc89fc822013-10-24 00:19:14 +0000167 ModulePtrSet AddedModules;
168 ModulePtrSet LoadedModules;
169 ModulePtrSet FinalizedModules;
170
171 void freeModulePtrSet(ModulePtrSet& MPS) {
172 // Go through the module set and delete everything.
173 for (ModulePtrSet::iterator I = MPS.begin(), E = MPS.end(); I != E; ++I) {
174 Module *M = *I;
175 delete M;
176 }
177 MPS.clear();
178 }
Andrew Kaylorea395922013-10-01 01:47:35 +0000179 };
180
David Blaikie196e3232014-09-02 22:41:07 +0000181 std::unique_ptr<TargetMachine> TM;
Jim Grosbach7b162492011-03-18 22:48:41 +0000182 MCContext *Ctx;
Lang Hames633fe142015-03-30 03:37:06 +0000183 std::shared_ptr<MCJITMemoryManager> MemMgr;
184 LinkingSymbolResolver Resolver;
Andrew Kaylor1a568c32012-08-07 18:33:00 +0000185 RuntimeDyld Dyld;
Eric Christopher79cc1e32014-09-02 22:28:02 +0000186 std::vector<JITEventListener*> EventListeners;
Jim Grosbach7b162492011-03-18 22:48:41 +0000187
Andrew Kaylorc89fc822013-10-24 00:19:14 +0000188 OwningModuleContainer OwnedModules;
Andrew Kaylorea395922013-10-01 01:47:35 +0000189
Rafael Espindola48af1c22014-08-19 18:44:46 +0000190 SmallVector<object::OwningBinary<object::Archive>, 2> Archives;
Rafael Espindola7271c192014-08-26 21:04:04 +0000191 SmallVector<std::unique_ptr<MemoryBuffer>, 2> Buffers;
Lang Hames173c69f2014-01-08 04:09:09 +0000192
Lang Hamesb5c7b1f2014-11-26 16:54:40 +0000193 SmallVector<std::unique_ptr<object::ObjectFile>, 2> LoadedObjects;
Jim Grosbach7b162492011-03-18 22:48:41 +0000194
Andrew Kaylorced4e8f2013-04-25 21:02:36 +0000195 // An optional ObjectCache to be notified of compiled objects and used to
196 // perform lookup of pre-compiled code to avoid re-compilation.
197 ObjectCache *ObjCache;
198
Andrew Kaylorc89fc822013-10-24 00:19:14 +0000199 Function *FindFunctionNamedInModulePtrSet(const char *FnName,
200 ModulePtrSet::iterator I,
201 ModulePtrSet::iterator E);
202
Keno Fischer73378eb2015-06-20 00:55:58 +0000203 GlobalVariable *FindGlobalVariableNamedInModulePtrSet(const char *Name,
204 bool AllowInternal,
205 ModulePtrSet::iterator I,
206 ModulePtrSet::iterator E);
207
Andrew Kaylorc89fc822013-10-24 00:19:14 +0000208 void runStaticConstructorsDestructorsInModulePtrSet(bool isDtors,
209 ModulePtrSet::iterator I,
210 ModulePtrSet::iterator E);
211
Daniel Dunbar7e5d8a72010-11-17 16:06:43 +0000212public:
Alexander Kornienkof817c1c2015-04-11 02:11:45 +0000213 ~MCJIT() override;
Daniel Dunbar7e5d8a72010-11-17 16:06:43 +0000214
215 /// @name ExecutionEngine interface implementation
216 /// @{
Rafael Espindola2a8a2792014-08-19 04:04:25 +0000217 void addModule(std::unique_ptr<Module> M) override;
David Blaikie7a1e7752014-04-29 21:52:46 +0000218 void addObjectFile(std::unique_ptr<object::ObjectFile> O) override;
Rafael Espindola7271c192014-08-26 21:04:04 +0000219 void addObjectFile(object::OwningBinary<object::ObjectFile> O) override;
Rafael Espindola48af1c22014-08-19 18:44:46 +0000220 void addArchive(object::OwningBinary<object::Archive> O) override;
Craig Topperb51ff602014-03-08 07:51:20 +0000221 bool removeModule(Module *M) override;
Andrew Kaylorc89fc822013-10-24 00:19:14 +0000222
Keno Fischer73378eb2015-06-20 00:55:58 +0000223 /// FindFunctionNamed - Search all of the active modules to find the function that
Andrew Kaylorc89fc822013-10-24 00:19:14 +0000224 /// defines FnName. This is very slow operation and shouldn't be used for
225 /// general code.
Hans Wennborgd2799a92015-09-10 00:57:26 +0000226 virtual Function *FindFunctionNamed(const char *FnName) override;
Keno Fischer73378eb2015-06-20 00:55:58 +0000227
Hans Wennborgd2799a92015-09-10 00:57:26 +0000228 /// FindGlobalVariableNamed - Search all of the active modules to find the global variable
229 /// that defines Name. This is very slow operation and shouldn't be used for
230 /// general code.
231 virtual GlobalVariable *FindGlobalVariableNamed(const char *Name, bool AllowInternal = false) override;
Daniel Dunbar7e5d8a72010-11-17 16:06:43 +0000232
Andrew Kaylorced4e8f2013-04-25 21:02:36 +0000233 /// Sets the object manager that MCJIT should use to avoid compilation.
Craig Topperb51ff602014-03-08 07:51:20 +0000234 void setObjectCache(ObjectCache *manager) override;
Andrew Kaylorced4e8f2013-04-25 21:02:36 +0000235
Lang Hames868d4b32014-03-20 21:06:46 +0000236 void setProcessAllSections(bool ProcessAllSections) override {
237 Dyld.setProcessAllSections(ProcessAllSections);
238 }
239
Craig Topperb51ff602014-03-08 07:51:20 +0000240 void generateCodeForModule(Module *M) override;
Andrew Kaylorea395922013-10-01 01:47:35 +0000241
David Tweed2e7efed2013-05-17 10:01:46 +0000242 /// finalizeObject - ensure the module is fully processed and is usable.
243 ///
244 /// It is the user-level function for completing the process of making the
245 /// object usable for execution. It should be called after sections within an
246 /// object have been relocated using mapSectionAddress. When this method is
247 /// called the MCJIT execution engine will reapply relocations for a loaded
248 /// object.
Yaron Kerenfb955822013-10-19 09:03:20 +0000249 /// Is it OK to finalize a set of modules, add modules and finalize again.
Andrew Kaylor2fb40ce2013-10-21 23:27:02 +0000250 // FIXME: Do we really need both of these?
Craig Topperb51ff602014-03-08 07:51:20 +0000251 void finalizeObject() override;
Andrew Kaylorea395922013-10-01 01:47:35 +0000252 virtual void finalizeModule(Module *);
253 void finalizeLoadedModules();
Andrew Kaylora714efc2012-11-05 20:57:16 +0000254
Andrew Kaylorc89fc822013-10-24 00:19:14 +0000255 /// runStaticConstructorsDestructors - This method is used to execute all of
256 /// the static constructors or destructors for a program.
257 ///
258 /// \param isDtors - Run the destructors instead of constructors.
Craig Topperb51ff602014-03-08 07:51:20 +0000259 void runStaticConstructorsDestructors(bool isDtors) override;
Andrew Kaylorc89fc822013-10-24 00:19:14 +0000260
Craig Topperb51ff602014-03-08 07:51:20 +0000261 void *getPointerToFunction(Function *F) override;
Daniel Dunbar7e5d8a72010-11-17 16:06:43 +0000262
Craig Topperb51ff602014-03-08 07:51:20 +0000263 GenericValue runFunction(Function *F,
Benjamin Kramerbd7b1c82015-06-13 19:50:29 +0000264 ArrayRef<GenericValue> ArgValues) override;
Daniel Dunbar7e5d8a72010-11-17 16:06:43 +0000265
Jim Grosbachd5274402011-03-22 18:05:27 +0000266 /// getPointerToNamedFunction - This method returns the address of the
267 /// specified function by using the dlsym function call. As such it is only
268 /// useful for resolving library symbols, not code generated symbols.
269 ///
270 /// If AbortOnFailure is false and no function with the given name is
271 /// found, this function silently returns a null pointer. Otherwise,
272 /// it prints a message to stderr and aborts.
273 ///
Lang Hames9a783342014-09-15 17:50:22 +0000274 void *getPointerToNamedFunction(StringRef Name,
Craig Topperb51ff602014-03-08 07:51:20 +0000275 bool AbortOnFailure = true) override;
Danil Malyshevbfee5422012-03-28 21:46:36 +0000276
Jim Grosbach0ddb3a42012-01-16 23:50:55 +0000277 /// mapSectionAddress - map a section to its target address space value.
278 /// Map the address of a JIT section as returned from the memory manager
279 /// to the address in the target process as the running code will see it.
280 /// This is the address which will be used for relocation resolution.
Craig Topperb51ff602014-03-08 07:51:20 +0000281 void mapSectionAddress(const void *LocalAddress,
282 uint64_t TargetAddress) override {
Jim Grosbach0ddb3a42012-01-16 23:50:55 +0000283 Dyld.mapSectionAddress(LocalAddress, TargetAddress);
284 }
Craig Topperb51ff602014-03-08 07:51:20 +0000285 void RegisterJITEventListener(JITEventListener *L) override;
286 void UnregisterJITEventListener(JITEventListener *L) override;
Andrew Kaylord8ffd9c2012-11-06 18:51:59 +0000287
Andrew Kaylorea395922013-10-01 01:47:35 +0000288 // If successful, these function will implicitly finalize all loaded objects.
289 // To get a function address within MCJIT without causing a finalize, use
290 // getSymbolAddress.
Craig Topperb51ff602014-03-08 07:51:20 +0000291 uint64_t getGlobalValueAddress(const std::string &Name) override;
292 uint64_t getFunctionAddress(const std::string &Name) override;
Andrew Kaylorea395922013-10-01 01:47:35 +0000293
David Blaikie196e3232014-09-02 22:41:07 +0000294 TargetMachine *getTargetMachine() override { return TM.get(); }
Juergen Ributzka5fe955c2014-01-23 19:23:28 +0000295
Daniel Dunbar7e5d8a72010-11-17 16:06:43 +0000296 /// @}
297 /// @name (Private) Registration Interfaces
298 /// @{
299
300 static void Register() {
301 MCJITCtor = createJIT;
302 }
303
Lang Hames633fe142015-03-30 03:37:06 +0000304 static ExecutionEngine*
305 createJIT(std::unique_ptr<Module> M,
306 std::string *ErrorStr,
307 std::shared_ptr<MCJITMemoryManager> MemMgr,
308 std::shared_ptr<RuntimeDyld::SymbolResolver> Resolver,
309 std::unique_ptr<TargetMachine> TM);
Daniel Dunbar7e5d8a72010-11-17 16:06:43 +0000310
311 // @}
Andrew Kaylor1a568c32012-08-07 18:33:00 +0000312
Lang Hames633fe142015-03-30 03:37:06 +0000313 RuntimeDyld::SymbolInfo findSymbol(const std::string &Name,
314 bool CheckFunctionsOnly);
315 // DEPRECATED - Please use findSymbol instead.
Andrew Kaylorea395922013-10-01 01:47:35 +0000316 // This is not directly exposed via the ExecutionEngine API, but it is
317 // used by the LinkingMemoryManager.
318 uint64_t getSymbolAddress(const std::string &Name,
Lang Hames633fe142015-03-30 03:37:06 +0000319 bool CheckFunctionsOnly);
Andrew Kaylorea395922013-10-01 01:47:35 +0000320
Andrew Kaylor1a568c32012-08-07 18:33:00 +0000321protected:
322 /// emitObject -- Generate a JITed object in memory from the specified module
323 /// Currently, MCJIT only supports a single module and the module passed to
324 /// this function call is expected to be the contained module. The module
NAKAMURA Takumi87e08802013-12-07 11:21:42 +0000325 /// is passed as a parameter here to prepare for multiple module support in
Andrew Kaylor1a568c32012-08-07 18:33:00 +0000326 /// the future.
Lang Hamesb5c7b1f2014-11-26 16:54:40 +0000327 std::unique_ptr<MemoryBuffer> emitObject(Module *M);
Andrew Kaylorced4e8f2013-04-25 21:02:36 +0000328
Lang Hamesb5c7b1f2014-11-26 16:54:40 +0000329 void NotifyObjectEmitted(const object::ObjectFile& Obj,
330 const RuntimeDyld::LoadedObjectInfo &L);
331 void NotifyFreeingObject(const object::ObjectFile& Obj);
Andrew Kaylorea395922013-10-01 01:47:35 +0000332
Lang Hames633fe142015-03-30 03:37:06 +0000333 RuntimeDyld::SymbolInfo findExistingSymbol(const std::string &Name);
Andrew Kaylorea395922013-10-01 01:47:35 +0000334 Module *findModuleForSymbol(const std::string &Name,
335 bool CheckFunctionsOnly);
Daniel Dunbar7e5d8a72010-11-17 16:06:43 +0000336};
337
Hans Wennborgd2799a92015-09-10 00:57:26 +0000338} // End llvm namespace
Daniel Dunbar7e5d8a72010-11-17 16:06:43 +0000339
Hans Wennborgd2799a92015-09-10 00:57:26 +0000340#endif