109467b48Spatrick //===-- MCJIT.h - Class definition for the MCJIT ----------------*- C++ -*-===// 209467b48Spatrick // 309467b48Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 409467b48Spatrick // See https://llvm.org/LICENSE.txt for license information. 509467b48Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 609467b48Spatrick // 709467b48Spatrick //===----------------------------------------------------------------------===// 809467b48Spatrick 909467b48Spatrick #ifndef LLVM_LIB_EXECUTIONENGINE_MCJIT_MCJIT_H 1009467b48Spatrick #define LLVM_LIB_EXECUTIONENGINE_MCJIT_MCJIT_H 1109467b48Spatrick 1209467b48Spatrick #include "llvm/ADT/SmallPtrSet.h" 1309467b48Spatrick #include "llvm/ADT/SmallVector.h" 1409467b48Spatrick #include "llvm/ExecutionEngine/ExecutionEngine.h" 1509467b48Spatrick #include "llvm/ExecutionEngine/RTDyldMemoryManager.h" 1609467b48Spatrick #include "llvm/ExecutionEngine/RuntimeDyld.h" 1709467b48Spatrick 1809467b48Spatrick namespace llvm { 1909467b48Spatrick class MCJIT; 20097a140dSpatrick class Module; 21097a140dSpatrick class ObjectCache; 2209467b48Spatrick 2309467b48Spatrick // This is a helper class that the MCJIT execution engine uses for linking 2409467b48Spatrick // functions across modules that it owns. It aggregates the memory manager 2509467b48Spatrick // that is passed in to the MCJIT constructor and defers most functionality 2609467b48Spatrick // to that object. 2709467b48Spatrick class LinkingSymbolResolver : public LegacyJITSymbolResolver { 2809467b48Spatrick public: LinkingSymbolResolver(MCJIT & Parent,std::shared_ptr<LegacyJITSymbolResolver> Resolver)2909467b48Spatrick LinkingSymbolResolver(MCJIT &Parent, 3009467b48Spatrick std::shared_ptr<LegacyJITSymbolResolver> Resolver) 3109467b48Spatrick : ParentEngine(Parent), ClientResolver(std::move(Resolver)) {} 3209467b48Spatrick 3309467b48Spatrick JITSymbol findSymbol(const std::string &Name) override; 3409467b48Spatrick 3509467b48Spatrick // MCJIT doesn't support logical dylibs. findSymbolInLogicalDylib(const std::string & Name)3609467b48Spatrick JITSymbol findSymbolInLogicalDylib(const std::string &Name) override { 3709467b48Spatrick return nullptr; 3809467b48Spatrick } 3909467b48Spatrick 4009467b48Spatrick private: 4109467b48Spatrick MCJIT &ParentEngine; 4209467b48Spatrick std::shared_ptr<LegacyJITSymbolResolver> ClientResolver; 4309467b48Spatrick void anchor() override; 4409467b48Spatrick }; 4509467b48Spatrick 4609467b48Spatrick // About Module states: added->loaded->finalized. 4709467b48Spatrick // 4809467b48Spatrick // The purpose of the "added" state is having modules in standby. (added=known 4909467b48Spatrick // but not compiled). The idea is that you can add a module to provide function 5009467b48Spatrick // definitions but if nothing in that module is referenced by a module in which 5109467b48Spatrick // a function is executed (note the wording here because it's not exactly the 5209467b48Spatrick // ideal case) then the module never gets compiled. This is sort of lazy 5309467b48Spatrick // compilation. 5409467b48Spatrick // 5509467b48Spatrick // The purpose of the "loaded" state (loaded=compiled and required sections 5609467b48Spatrick // copied into local memory but not yet ready for execution) is to have an 5709467b48Spatrick // intermediate state wherein clients can remap the addresses of sections, using 5809467b48Spatrick // MCJIT::mapSectionAddress, (in preparation for later copying to a new location 5909467b48Spatrick // or an external process) before relocations and page permissions are applied. 6009467b48Spatrick // 6109467b48Spatrick // It might not be obvious at first glance, but the "remote-mcjit" case in the 6209467b48Spatrick // lli tool does this. In that case, the intermediate action is taken by the 6309467b48Spatrick // RemoteMemoryManager in response to the notifyObjectLoaded function being 6409467b48Spatrick // called. 6509467b48Spatrick 6609467b48Spatrick class MCJIT : public ExecutionEngine { 6709467b48Spatrick MCJIT(std::unique_ptr<Module> M, std::unique_ptr<TargetMachine> tm, 6809467b48Spatrick std::shared_ptr<MCJITMemoryManager> MemMgr, 6909467b48Spatrick std::shared_ptr<LegacyJITSymbolResolver> Resolver); 7009467b48Spatrick 7109467b48Spatrick typedef llvm::SmallPtrSet<Module *, 4> ModulePtrSet; 7209467b48Spatrick 7309467b48Spatrick class OwningModuleContainer { 7409467b48Spatrick public: 75*d415bd75Srobert OwningModuleContainer() = default; ~OwningModuleContainer()7609467b48Spatrick ~OwningModuleContainer() { 7709467b48Spatrick freeModulePtrSet(AddedModules); 7809467b48Spatrick freeModulePtrSet(LoadedModules); 7909467b48Spatrick freeModulePtrSet(FinalizedModules); 8009467b48Spatrick } 8109467b48Spatrick begin_added()8209467b48Spatrick ModulePtrSet::iterator begin_added() { return AddedModules.begin(); } end_added()8309467b48Spatrick ModulePtrSet::iterator end_added() { return AddedModules.end(); } added()8409467b48Spatrick iterator_range<ModulePtrSet::iterator> added() { 8509467b48Spatrick return make_range(begin_added(), end_added()); 8609467b48Spatrick } 8709467b48Spatrick begin_loaded()8809467b48Spatrick ModulePtrSet::iterator begin_loaded() { return LoadedModules.begin(); } end_loaded()8909467b48Spatrick ModulePtrSet::iterator end_loaded() { return LoadedModules.end(); } 9009467b48Spatrick begin_finalized()9109467b48Spatrick ModulePtrSet::iterator begin_finalized() { return FinalizedModules.begin(); } end_finalized()9209467b48Spatrick ModulePtrSet::iterator end_finalized() { return FinalizedModules.end(); } 9309467b48Spatrick addModule(std::unique_ptr<Module> M)9409467b48Spatrick void addModule(std::unique_ptr<Module> M) { 9509467b48Spatrick AddedModules.insert(M.release()); 9609467b48Spatrick } 9709467b48Spatrick removeModule(Module * M)9809467b48Spatrick bool removeModule(Module *M) { 9909467b48Spatrick return AddedModules.erase(M) || LoadedModules.erase(M) || 10009467b48Spatrick FinalizedModules.erase(M); 10109467b48Spatrick } 10209467b48Spatrick hasModuleBeenAddedButNotLoaded(Module * M)10309467b48Spatrick bool hasModuleBeenAddedButNotLoaded(Module *M) { 10473471bf0Spatrick return AddedModules.contains(M); 10509467b48Spatrick } 10609467b48Spatrick hasModuleBeenLoaded(Module * M)10709467b48Spatrick bool hasModuleBeenLoaded(Module *M) { 10809467b48Spatrick // If the module is in either the "loaded" or "finalized" sections it 10909467b48Spatrick // has been loaded. 11073471bf0Spatrick return LoadedModules.contains(M) || FinalizedModules.contains(M); 11109467b48Spatrick } 11209467b48Spatrick hasModuleBeenFinalized(Module * M)11309467b48Spatrick bool hasModuleBeenFinalized(Module *M) { 11473471bf0Spatrick return FinalizedModules.contains(M); 11509467b48Spatrick } 11609467b48Spatrick ownsModule(Module * M)11709467b48Spatrick bool ownsModule(Module* M) { 11873471bf0Spatrick return AddedModules.contains(M) || LoadedModules.contains(M) || 11973471bf0Spatrick FinalizedModules.contains(M); 12009467b48Spatrick } 12109467b48Spatrick markModuleAsLoaded(Module * M)12209467b48Spatrick void markModuleAsLoaded(Module *M) { 12309467b48Spatrick // This checks against logic errors in the MCJIT implementation. 12409467b48Spatrick // This function should never be called with either a Module that MCJIT 12509467b48Spatrick // does not own or a Module that has already been loaded and/or finalized. 12609467b48Spatrick assert(AddedModules.count(M) && 12709467b48Spatrick "markModuleAsLoaded: Module not found in AddedModules"); 12809467b48Spatrick 12909467b48Spatrick // Remove the module from the "Added" set. 13009467b48Spatrick AddedModules.erase(M); 13109467b48Spatrick 13209467b48Spatrick // Add the Module to the "Loaded" set. 13309467b48Spatrick LoadedModules.insert(M); 13409467b48Spatrick } 13509467b48Spatrick markModuleAsFinalized(Module * M)13609467b48Spatrick void markModuleAsFinalized(Module *M) { 13709467b48Spatrick // This checks against logic errors in the MCJIT implementation. 13809467b48Spatrick // This function should never be called with either a Module that MCJIT 13909467b48Spatrick // does not own, a Module that has not been loaded or a Module that has 14009467b48Spatrick // already been finalized. 14109467b48Spatrick assert(LoadedModules.count(M) && 14209467b48Spatrick "markModuleAsFinalized: Module not found in LoadedModules"); 14309467b48Spatrick 14409467b48Spatrick // Remove the module from the "Loaded" section of the list. 14509467b48Spatrick LoadedModules.erase(M); 14609467b48Spatrick 14709467b48Spatrick // Add the Module to the "Finalized" section of the list by inserting it 14809467b48Spatrick // before the 'end' iterator. 14909467b48Spatrick FinalizedModules.insert(M); 15009467b48Spatrick } 15109467b48Spatrick markAllLoadedModulesAsFinalized()15209467b48Spatrick void markAllLoadedModulesAsFinalized() { 153*d415bd75Srobert for (Module *M : LoadedModules) 15409467b48Spatrick FinalizedModules.insert(M); 15509467b48Spatrick LoadedModules.clear(); 15609467b48Spatrick } 15709467b48Spatrick 15809467b48Spatrick private: 15909467b48Spatrick ModulePtrSet AddedModules; 16009467b48Spatrick ModulePtrSet LoadedModules; 16109467b48Spatrick ModulePtrSet FinalizedModules; 16209467b48Spatrick freeModulePtrSet(ModulePtrSet & MPS)16309467b48Spatrick void freeModulePtrSet(ModulePtrSet& MPS) { 16409467b48Spatrick // Go through the module set and delete everything. 165*d415bd75Srobert for (Module *M : MPS) 16609467b48Spatrick delete M; 16709467b48Spatrick MPS.clear(); 16809467b48Spatrick } 16909467b48Spatrick }; 17009467b48Spatrick 17109467b48Spatrick std::unique_ptr<TargetMachine> TM; 17209467b48Spatrick MCContext *Ctx; 17309467b48Spatrick std::shared_ptr<MCJITMemoryManager> MemMgr; 17409467b48Spatrick LinkingSymbolResolver Resolver; 17509467b48Spatrick RuntimeDyld Dyld; 17609467b48Spatrick std::vector<JITEventListener*> EventListeners; 17709467b48Spatrick 17809467b48Spatrick OwningModuleContainer OwnedModules; 17909467b48Spatrick 18009467b48Spatrick SmallVector<object::OwningBinary<object::Archive>, 2> Archives; 18109467b48Spatrick SmallVector<std::unique_ptr<MemoryBuffer>, 2> Buffers; 18209467b48Spatrick 18309467b48Spatrick SmallVector<std::unique_ptr<object::ObjectFile>, 2> LoadedObjects; 18409467b48Spatrick 18509467b48Spatrick // An optional ObjectCache to be notified of compiled objects and used to 18609467b48Spatrick // perform lookup of pre-compiled code to avoid re-compilation. 18709467b48Spatrick ObjectCache *ObjCache; 18809467b48Spatrick 18909467b48Spatrick Function *FindFunctionNamedInModulePtrSet(StringRef FnName, 19009467b48Spatrick ModulePtrSet::iterator I, 19109467b48Spatrick ModulePtrSet::iterator E); 19209467b48Spatrick 19309467b48Spatrick GlobalVariable *FindGlobalVariableNamedInModulePtrSet(StringRef Name, 19409467b48Spatrick bool AllowInternal, 19509467b48Spatrick ModulePtrSet::iterator I, 19609467b48Spatrick ModulePtrSet::iterator E); 19709467b48Spatrick 19809467b48Spatrick void runStaticConstructorsDestructorsInModulePtrSet(bool isDtors, 19909467b48Spatrick ModulePtrSet::iterator I, 20009467b48Spatrick ModulePtrSet::iterator E); 20109467b48Spatrick 20209467b48Spatrick public: 20309467b48Spatrick ~MCJIT() override; 20409467b48Spatrick 20509467b48Spatrick /// @name ExecutionEngine interface implementation 20609467b48Spatrick /// @{ 20709467b48Spatrick void addModule(std::unique_ptr<Module> M) override; 20809467b48Spatrick void addObjectFile(std::unique_ptr<object::ObjectFile> O) override; 20909467b48Spatrick void addObjectFile(object::OwningBinary<object::ObjectFile> O) override; 21009467b48Spatrick void addArchive(object::OwningBinary<object::Archive> O) override; 21109467b48Spatrick bool removeModule(Module *M) override; 21209467b48Spatrick 21309467b48Spatrick /// FindFunctionNamed - Search all of the active modules to find the function that 21409467b48Spatrick /// defines FnName. This is very slow operation and shouldn't be used for 21509467b48Spatrick /// general code. 21609467b48Spatrick Function *FindFunctionNamed(StringRef FnName) override; 21709467b48Spatrick 21809467b48Spatrick /// FindGlobalVariableNamed - Search all of the active modules to find the 21909467b48Spatrick /// global variable that defines Name. This is very slow operation and 22009467b48Spatrick /// shouldn't be used for general code. 22109467b48Spatrick GlobalVariable *FindGlobalVariableNamed(StringRef Name, 22209467b48Spatrick bool AllowInternal = false) override; 22309467b48Spatrick 22409467b48Spatrick /// Sets the object manager that MCJIT should use to avoid compilation. 22509467b48Spatrick void setObjectCache(ObjectCache *manager) override; 22609467b48Spatrick setProcessAllSections(bool ProcessAllSections)22709467b48Spatrick void setProcessAllSections(bool ProcessAllSections) override { 22809467b48Spatrick Dyld.setProcessAllSections(ProcessAllSections); 22909467b48Spatrick } 23009467b48Spatrick 23109467b48Spatrick void generateCodeForModule(Module *M) override; 23209467b48Spatrick 23309467b48Spatrick /// finalizeObject - ensure the module is fully processed and is usable. 23409467b48Spatrick /// 23509467b48Spatrick /// It is the user-level function for completing the process of making the 23609467b48Spatrick /// object usable for execution. It should be called after sections within an 23709467b48Spatrick /// object have been relocated using mapSectionAddress. When this method is 23809467b48Spatrick /// called the MCJIT execution engine will reapply relocations for a loaded 23909467b48Spatrick /// object. 24009467b48Spatrick /// Is it OK to finalize a set of modules, add modules and finalize again. 24109467b48Spatrick // FIXME: Do we really need both of these? 24209467b48Spatrick void finalizeObject() override; 24309467b48Spatrick virtual void finalizeModule(Module *); 24409467b48Spatrick void finalizeLoadedModules(); 24509467b48Spatrick 24609467b48Spatrick /// runStaticConstructorsDestructors - This method is used to execute all of 24709467b48Spatrick /// the static constructors or destructors for a program. 24809467b48Spatrick /// 24909467b48Spatrick /// \param isDtors - Run the destructors instead of constructors. 25009467b48Spatrick void runStaticConstructorsDestructors(bool isDtors) override; 25109467b48Spatrick 25209467b48Spatrick void *getPointerToFunction(Function *F) override; 25309467b48Spatrick 25409467b48Spatrick GenericValue runFunction(Function *F, 25509467b48Spatrick ArrayRef<GenericValue> ArgValues) override; 25609467b48Spatrick 25709467b48Spatrick /// getPointerToNamedFunction - This method returns the address of the 25809467b48Spatrick /// specified function by using the dlsym function call. As such it is only 25909467b48Spatrick /// useful for resolving library symbols, not code generated symbols. 26009467b48Spatrick /// 26109467b48Spatrick /// If AbortOnFailure is false and no function with the given name is 26209467b48Spatrick /// found, this function silently returns a null pointer. Otherwise, 26309467b48Spatrick /// it prints a message to stderr and aborts. 26409467b48Spatrick /// 26509467b48Spatrick void *getPointerToNamedFunction(StringRef Name, 26609467b48Spatrick bool AbortOnFailure = true) override; 26709467b48Spatrick 26809467b48Spatrick /// mapSectionAddress - map a section to its target address space value. 26909467b48Spatrick /// Map the address of a JIT section as returned from the memory manager 27009467b48Spatrick /// to the address in the target process as the running code will see it. 27109467b48Spatrick /// This is the address which will be used for relocation resolution. mapSectionAddress(const void * LocalAddress,uint64_t TargetAddress)27209467b48Spatrick void mapSectionAddress(const void *LocalAddress, 27309467b48Spatrick uint64_t TargetAddress) override { 27409467b48Spatrick Dyld.mapSectionAddress(LocalAddress, TargetAddress); 27509467b48Spatrick } 27609467b48Spatrick void RegisterJITEventListener(JITEventListener *L) override; 27709467b48Spatrick void UnregisterJITEventListener(JITEventListener *L) override; 27809467b48Spatrick 27909467b48Spatrick // If successful, these function will implicitly finalize all loaded objects. 28009467b48Spatrick // To get a function address within MCJIT without causing a finalize, use 28109467b48Spatrick // getSymbolAddress. 28209467b48Spatrick uint64_t getGlobalValueAddress(const std::string &Name) override; 28309467b48Spatrick uint64_t getFunctionAddress(const std::string &Name) override; 28409467b48Spatrick getTargetMachine()28509467b48Spatrick TargetMachine *getTargetMachine() override { return TM.get(); } 28609467b48Spatrick 28709467b48Spatrick /// @} 28809467b48Spatrick /// @name (Private) Registration Interfaces 28909467b48Spatrick /// @{ 29009467b48Spatrick Register()29109467b48Spatrick static void Register() { 29209467b48Spatrick MCJITCtor = createJIT; 29309467b48Spatrick } 29409467b48Spatrick 29509467b48Spatrick static ExecutionEngine * 29609467b48Spatrick createJIT(std::unique_ptr<Module> M, std::string *ErrorStr, 29709467b48Spatrick std::shared_ptr<MCJITMemoryManager> MemMgr, 29809467b48Spatrick std::shared_ptr<LegacyJITSymbolResolver> Resolver, 29909467b48Spatrick std::unique_ptr<TargetMachine> TM); 30009467b48Spatrick 30109467b48Spatrick // @} 30209467b48Spatrick 30309467b48Spatrick // Takes a mangled name and returns the corresponding JITSymbol (if a 30409467b48Spatrick // definition of that mangled name has been added to the JIT). 30509467b48Spatrick JITSymbol findSymbol(const std::string &Name, bool CheckFunctionsOnly); 30609467b48Spatrick 30709467b48Spatrick // DEPRECATED - Please use findSymbol instead. 30809467b48Spatrick // 30909467b48Spatrick // This is not directly exposed via the ExecutionEngine API, but it is 31009467b48Spatrick // used by the LinkingMemoryManager. 31109467b48Spatrick // 31209467b48Spatrick // getSymbolAddress takes an unmangled name and returns the corresponding 31309467b48Spatrick // JITSymbol if a definition of the name has been added to the JIT. 31409467b48Spatrick uint64_t getSymbolAddress(const std::string &Name, 31509467b48Spatrick bool CheckFunctionsOnly); 31609467b48Spatrick 31709467b48Spatrick protected: 31809467b48Spatrick /// emitObject -- Generate a JITed object in memory from the specified module 31909467b48Spatrick /// Currently, MCJIT only supports a single module and the module passed to 32009467b48Spatrick /// this function call is expected to be the contained module. The module 32109467b48Spatrick /// is passed as a parameter here to prepare for multiple module support in 32209467b48Spatrick /// the future. 32309467b48Spatrick std::unique_ptr<MemoryBuffer> emitObject(Module *M); 32409467b48Spatrick 32509467b48Spatrick void notifyObjectLoaded(const object::ObjectFile &Obj, 32609467b48Spatrick const RuntimeDyld::LoadedObjectInfo &L); 32709467b48Spatrick void notifyFreeingObject(const object::ObjectFile &Obj); 32809467b48Spatrick 32909467b48Spatrick JITSymbol findExistingSymbol(const std::string &Name); 33009467b48Spatrick Module *findModuleForSymbol(const std::string &Name, bool CheckFunctionsOnly); 33109467b48Spatrick }; 33209467b48Spatrick 33309467b48Spatrick } // end llvm namespace 33409467b48Spatrick 33509467b48Spatrick #endif // LLVM_LIB_EXECUTIONENGINE_MCJIT_MCJIT_H 336