| 1 | //===-- MCJIT.h - Class definition for the MCJIT ----------------*- C++ -*-===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #ifndef LLVM_LIB_EXECUTIONENGINE_MCJIT_MCJIT_H |
| 10 | #define LLVM_LIB_EXECUTIONENGINE_MCJIT_MCJIT_H |
| 11 | |
| 12 | #include "llvm/ADT/SmallPtrSet.h" |
| 13 | #include "llvm/ADT/SmallVector.h" |
| 14 | #include "llvm/ExecutionEngine/ExecutionEngine.h" |
| 15 | #include "llvm/ExecutionEngine/RTDyldMemoryManager.h" |
| 16 | #include "llvm/ExecutionEngine/RuntimeDyld.h" |
| 17 | |
| 18 | namespace llvm { |
| 19 | class MCJIT; |
| 20 | class Module; |
| 21 | class ObjectCache; |
| 22 | |
| 23 | // This is a helper class that the MCJIT execution engine uses for linking |
| 24 | // functions across modules that it owns. It aggregates the memory manager |
| 25 | // that is passed in to the MCJIT constructor and defers most functionality |
| 26 | // to that object. |
| 27 | class LinkingSymbolResolver : public LegacyJITSymbolResolver { |
| 28 | public: |
| 29 | LinkingSymbolResolver(MCJIT &Parent, |
| 30 | std::shared_ptr<LegacyJITSymbolResolver> Resolver) |
| 31 | : ParentEngine(Parent), ClientResolver(std::move(Resolver)) {} |
| 32 | |
| 33 | JITSymbol findSymbol(const std::string &Name) override; |
| 34 | |
| 35 | // MCJIT doesn't support logical dylibs. |
| 36 | JITSymbol findSymbolInLogicalDylib(const std::string &Name) override { |
| 37 | return nullptr; |
| 38 | } |
| 39 | |
| 40 | private: |
| 41 | MCJIT &ParentEngine; |
| 42 | std::shared_ptr<LegacyJITSymbolResolver> ClientResolver; |
| 43 | void anchor() override; |
| 44 | }; |
| 45 | |
| 46 | // About Module states: added->loaded->finalized. |
| 47 | // |
| 48 | // The purpose of the "added" state is having modules in standby. (added=known |
| 49 | // but not compiled). The idea is that you can add a module to provide function |
| 50 | // definitions but if nothing in that module is referenced by a module in which |
| 51 | // a function is executed (note the wording here because it's not exactly the |
| 52 | // ideal case) then the module never gets compiled. This is sort of lazy |
| 53 | // compilation. |
| 54 | // |
| 55 | // The purpose of the "loaded" state (loaded=compiled and required sections |
| 56 | // copied into local memory but not yet ready for execution) is to have an |
| 57 | // intermediate state wherein clients can remap the addresses of sections, using |
| 58 | // MCJIT::mapSectionAddress, (in preparation for later copying to a new location |
| 59 | // or an external process) before relocations and page permissions are applied. |
| 60 | // |
| 61 | // It might not be obvious at first glance, but the "remote-mcjit" case in the |
| 62 | // lli tool does this. In that case, the intermediate action is taken by the |
| 63 | // RemoteMemoryManager in response to the notifyObjectLoaded function being |
| 64 | // called. |
| 65 | |
| 66 | class MCJIT : public ExecutionEngine { |
| 67 | MCJIT(std::unique_ptr<Module> M, std::unique_ptr<TargetMachine> tm, |
| 68 | std::shared_ptr<MCJITMemoryManager> MemMgr, |
| 69 | std::shared_ptr<LegacyJITSymbolResolver> Resolver); |
| 70 | |
| 71 | typedef llvm::SmallPtrSet<Module *, 4> ModulePtrSet; |
| 72 | |
| 73 | class OwningModuleContainer { |
| 74 | public: |
| 75 | OwningModuleContainer() = default; |
| 76 | ~OwningModuleContainer() { |
| 77 | freeModulePtrSet(MPS&: AddedModules); |
| 78 | freeModulePtrSet(MPS&: LoadedModules); |
| 79 | freeModulePtrSet(MPS&: FinalizedModules); |
| 80 | } |
| 81 | |
| 82 | ModulePtrSet::iterator begin_added() { return AddedModules.begin(); } |
| 83 | ModulePtrSet::iterator end_added() { return AddedModules.end(); } |
| 84 | iterator_range<ModulePtrSet::iterator> added() { |
| 85 | return make_range(x: begin_added(), y: end_added()); |
| 86 | } |
| 87 | |
| 88 | ModulePtrSet::iterator begin_loaded() { return LoadedModules.begin(); } |
| 89 | ModulePtrSet::iterator end_loaded() { return LoadedModules.end(); } |
| 90 | |
| 91 | ModulePtrSet::iterator begin_finalized() { return FinalizedModules.begin(); } |
| 92 | ModulePtrSet::iterator end_finalized() { return FinalizedModules.end(); } |
| 93 | |
| 94 | void addModule(std::unique_ptr<Module> M) { |
| 95 | AddedModules.insert(Ptr: M.release()); |
| 96 | } |
| 97 | |
| 98 | bool removeModule(Module *M) { |
| 99 | return AddedModules.erase(Ptr: M) || LoadedModules.erase(Ptr: M) || |
| 100 | FinalizedModules.erase(Ptr: M); |
| 101 | } |
| 102 | |
| 103 | bool hasModuleBeenAddedButNotLoaded(Module *M) { |
| 104 | return AddedModules.contains(Ptr: M); |
| 105 | } |
| 106 | |
| 107 | bool hasModuleBeenLoaded(Module *M) { |
| 108 | // If the module is in either the "loaded" or "finalized" sections it |
| 109 | // has been loaded. |
| 110 | return LoadedModules.contains(Ptr: M) || FinalizedModules.contains(Ptr: M); |
| 111 | } |
| 112 | |
| 113 | bool hasModuleBeenFinalized(Module *M) { |
| 114 | return FinalizedModules.contains(Ptr: M); |
| 115 | } |
| 116 | |
| 117 | bool ownsModule(Module* M) { |
| 118 | return AddedModules.contains(Ptr: M) || LoadedModules.contains(Ptr: M) || |
| 119 | FinalizedModules.contains(Ptr: M); |
| 120 | } |
| 121 | |
| 122 | void markModuleAsLoaded(Module *M) { |
| 123 | // This checks against logic errors in the MCJIT implementation. |
| 124 | // This function should never be called with either a Module that MCJIT |
| 125 | // does not own or a Module that has already been loaded and/or finalized. |
| 126 | assert(AddedModules.count(M) && |
| 127 | "markModuleAsLoaded: Module not found in AddedModules" ); |
| 128 | |
| 129 | // Remove the module from the "Added" set. |
| 130 | AddedModules.erase(Ptr: M); |
| 131 | |
| 132 | // Add the Module to the "Loaded" set. |
| 133 | LoadedModules.insert(Ptr: M); |
| 134 | } |
| 135 | |
| 136 | void markModuleAsFinalized(Module *M) { |
| 137 | // This checks against logic errors in the MCJIT implementation. |
| 138 | // This function should never be called with either a Module that MCJIT |
| 139 | // does not own, a Module that has not been loaded or a Module that has |
| 140 | // already been finalized. |
| 141 | assert(LoadedModules.count(M) && |
| 142 | "markModuleAsFinalized: Module not found in LoadedModules" ); |
| 143 | |
| 144 | // Remove the module from the "Loaded" section of the list. |
| 145 | LoadedModules.erase(Ptr: M); |
| 146 | |
| 147 | // Add the Module to the "Finalized" section of the list by inserting it |
| 148 | // before the 'end' iterator. |
| 149 | FinalizedModules.insert(Ptr: M); |
| 150 | } |
| 151 | |
| 152 | void markAllLoadedModulesAsFinalized() { |
| 153 | FinalizedModules.insert_range(R&: LoadedModules); |
| 154 | LoadedModules.clear(); |
| 155 | } |
| 156 | |
| 157 | private: |
| 158 | ModulePtrSet AddedModules; |
| 159 | ModulePtrSet LoadedModules; |
| 160 | ModulePtrSet FinalizedModules; |
| 161 | |
| 162 | void freeModulePtrSet(ModulePtrSet& MPS) { |
| 163 | // Go through the module set and delete everything. |
| 164 | for (Module *M : MPS) |
| 165 | delete M; |
| 166 | MPS.clear(); |
| 167 | } |
| 168 | }; |
| 169 | |
| 170 | std::unique_ptr<TargetMachine> TM; |
| 171 | MCContext *Ctx; |
| 172 | std::shared_ptr<MCJITMemoryManager> MemMgr; |
| 173 | LinkingSymbolResolver Resolver; |
| 174 | RuntimeDyld Dyld; |
| 175 | std::vector<JITEventListener*> EventListeners; |
| 176 | |
| 177 | OwningModuleContainer OwnedModules; |
| 178 | |
| 179 | SmallVector<object::OwningBinary<object::Archive>, 2> Archives; |
| 180 | SmallVector<std::unique_ptr<MemoryBuffer>, 2> Buffers; |
| 181 | |
| 182 | SmallVector<std::unique_ptr<object::ObjectFile>, 2> LoadedObjects; |
| 183 | |
| 184 | // An optional ObjectCache to be notified of compiled objects and used to |
| 185 | // perform lookup of pre-compiled code to avoid re-compilation. |
| 186 | ObjectCache *ObjCache; |
| 187 | |
| 188 | Function *FindFunctionNamedInModulePtrSet(StringRef FnName, |
| 189 | ModulePtrSet::iterator I, |
| 190 | ModulePtrSet::iterator E); |
| 191 | |
| 192 | GlobalVariable *FindGlobalVariableNamedInModulePtrSet(StringRef Name, |
| 193 | bool AllowInternal, |
| 194 | ModulePtrSet::iterator I, |
| 195 | ModulePtrSet::iterator E); |
| 196 | |
| 197 | void runStaticConstructorsDestructorsInModulePtrSet(bool isDtors, |
| 198 | ModulePtrSet::iterator I, |
| 199 | ModulePtrSet::iterator E); |
| 200 | |
| 201 | public: |
| 202 | ~MCJIT() override; |
| 203 | |
| 204 | /// @name ExecutionEngine interface implementation |
| 205 | /// @{ |
| 206 | void addModule(std::unique_ptr<Module> M) override; |
| 207 | void addObjectFile(std::unique_ptr<object::ObjectFile> O) override; |
| 208 | void addObjectFile(object::OwningBinary<object::ObjectFile> O) override; |
| 209 | void addArchive(object::OwningBinary<object::Archive> O) override; |
| 210 | bool removeModule(Module *M) override; |
| 211 | |
| 212 | /// FindFunctionNamed - Search all of the active modules to find the function that |
| 213 | /// defines FnName. This is very slow operation and shouldn't be used for |
| 214 | /// general code. |
| 215 | Function *FindFunctionNamed(StringRef FnName) override; |
| 216 | |
| 217 | /// FindGlobalVariableNamed - Search all of the active modules to find the |
| 218 | /// global variable that defines Name. This is very slow operation and |
| 219 | /// shouldn't be used for general code. |
| 220 | GlobalVariable *FindGlobalVariableNamed(StringRef Name, |
| 221 | bool AllowInternal = false) override; |
| 222 | |
| 223 | /// Sets the object manager that MCJIT should use to avoid compilation. |
| 224 | void setObjectCache(ObjectCache *manager) override; |
| 225 | |
| 226 | void setProcessAllSections(bool ProcessAllSections) override { |
| 227 | Dyld.setProcessAllSections(ProcessAllSections); |
| 228 | } |
| 229 | |
| 230 | void generateCodeForModule(Module *M) override; |
| 231 | |
| 232 | /// finalizeObject - ensure the module is fully processed and is usable. |
| 233 | /// |
| 234 | /// It is the user-level function for completing the process of making the |
| 235 | /// object usable for execution. It should be called after sections within an |
| 236 | /// object have been relocated using mapSectionAddress. When this method is |
| 237 | /// called the MCJIT execution engine will reapply relocations for a loaded |
| 238 | /// object. |
| 239 | /// Is it OK to finalize a set of modules, add modules and finalize again. |
| 240 | // FIXME: Do we really need both of these? |
| 241 | void finalizeObject() override; |
| 242 | virtual void finalizeModule(Module *); |
| 243 | void finalizeLoadedModules(); |
| 244 | |
| 245 | /// runStaticConstructorsDestructors - This method is used to execute all of |
| 246 | /// the static constructors or destructors for a program. |
| 247 | /// |
| 248 | /// \param isDtors - Run the destructors instead of constructors. |
| 249 | void runStaticConstructorsDestructors(bool isDtors) override; |
| 250 | |
| 251 | void *getPointerToFunction(Function *F) override; |
| 252 | |
| 253 | GenericValue runFunction(Function *F, |
| 254 | ArrayRef<GenericValue> ArgValues) override; |
| 255 | |
| 256 | /// getPointerToNamedFunction - This method returns the address of the |
| 257 | /// specified function by using the dlsym function call. As such it is only |
| 258 | /// useful for resolving library symbols, not code generated symbols. |
| 259 | /// |
| 260 | /// If AbortOnFailure is false and no function with the given name is |
| 261 | /// found, this function silently returns a null pointer. Otherwise, |
| 262 | /// it prints a message to stderr and aborts. |
| 263 | /// |
| 264 | void *getPointerToNamedFunction(StringRef Name, |
| 265 | bool AbortOnFailure = true) override; |
| 266 | |
| 267 | /// mapSectionAddress - map a section to its target address space value. |
| 268 | /// Map the address of a JIT section as returned from the memory manager |
| 269 | /// to the address in the target process as the running code will see it. |
| 270 | /// This is the address which will be used for relocation resolution. |
| 271 | void mapSectionAddress(const void *LocalAddress, |
| 272 | uint64_t TargetAddress) override { |
| 273 | Dyld.mapSectionAddress(LocalAddress, TargetAddress); |
| 274 | } |
| 275 | void RegisterJITEventListener(JITEventListener *L) override; |
| 276 | void UnregisterJITEventListener(JITEventListener *L) override; |
| 277 | |
| 278 | // If successful, these function will implicitly finalize all loaded objects. |
| 279 | // To get a function address within MCJIT without causing a finalize, use |
| 280 | // getSymbolAddress. |
| 281 | uint64_t getGlobalValueAddress(const std::string &Name) override; |
| 282 | uint64_t getFunctionAddress(const std::string &Name) override; |
| 283 | |
| 284 | TargetMachine *getTargetMachine() override { return TM.get(); } |
| 285 | |
| 286 | /// @} |
| 287 | /// @name (Private) Registration Interfaces |
| 288 | /// @{ |
| 289 | |
| 290 | static void Register() { |
| 291 | MCJITCtor = createJIT; |
| 292 | } |
| 293 | |
| 294 | static ExecutionEngine * |
| 295 | createJIT(std::unique_ptr<Module> M, std::string *ErrorStr, |
| 296 | std::shared_ptr<MCJITMemoryManager> MemMgr, |
| 297 | std::shared_ptr<LegacyJITSymbolResolver> Resolver, |
| 298 | std::unique_ptr<TargetMachine> TM); |
| 299 | |
| 300 | // @} |
| 301 | |
| 302 | // Takes a mangled name and returns the corresponding JITSymbol (if a |
| 303 | // definition of that mangled name has been added to the JIT). |
| 304 | JITSymbol findSymbol(const std::string &Name, bool CheckFunctionsOnly); |
| 305 | |
| 306 | // DEPRECATED - Please use findSymbol instead. |
| 307 | // |
| 308 | // This is not directly exposed via the ExecutionEngine API, but it is |
| 309 | // used by the LinkingMemoryManager. |
| 310 | // |
| 311 | // getSymbolAddress takes an unmangled name and returns the corresponding |
| 312 | // JITSymbol if a definition of the name has been added to the JIT. |
| 313 | uint64_t getSymbolAddress(const std::string &Name, |
| 314 | bool CheckFunctionsOnly); |
| 315 | |
| 316 | protected: |
| 317 | /// emitObject -- Generate a JITed object in memory from the specified module |
| 318 | /// Currently, MCJIT only supports a single module and the module passed to |
| 319 | /// this function call is expected to be the contained module. The module |
| 320 | /// is passed as a parameter here to prepare for multiple module support in |
| 321 | /// the future. |
| 322 | std::unique_ptr<MemoryBuffer> emitObject(Module *M); |
| 323 | |
| 324 | void notifyObjectLoaded(const object::ObjectFile &Obj, |
| 325 | const RuntimeDyld::LoadedObjectInfo &L); |
| 326 | void notifyFreeingObject(const object::ObjectFile &Obj); |
| 327 | |
| 328 | JITSymbol findExistingSymbol(const std::string &Name); |
| 329 | Module *findModuleForSymbol(const std::string &Name, bool CheckFunctionsOnly); |
| 330 | }; |
| 331 | |
| 332 | } // end llvm namespace |
| 333 | |
| 334 | #endif // LLVM_LIB_EXECUTIONENGINE_MCJIT_MCJIT_H |
| 335 | |