1 //===--- LogicalDylib.h - Simulates dylib-style symbol lookup ---*- 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 // Simulates symbol resolution inside a dylib. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #ifndef LLVM_EXECUTIONENGINE_ORC_LOGICALDYLIB_H 15 #define LLVM_EXECUTIONENGINE_ORC_LOGICALDYLIB_H 16 17 namespace llvm { 18 namespace orc { 19 20 template <typename BaseLayerT, 21 typename LogicalModuleResources, 22 typename LogicalDylibResources> 23 class LogicalDylib { 24 public: 25 typedef typename BaseLayerT::ModuleSetHandleT BaseLayerModuleSetHandleT; 26 private: 27 28 typedef std::vector<BaseLayerModuleSetHandleT> BaseLayerHandleList; 29 30 struct LogicalModule { 31 LogicalModuleResources Resources; 32 BaseLayerHandleList BaseLayerHandles; 33 }; 34 typedef std::vector<LogicalModule> LogicalModuleList; 35 36 public: 37 38 typedef typename BaseLayerHandleList::iterator BaseLayerHandleIterator; 39 typedef typename LogicalModuleList::iterator LogicalModuleHandle; 40 LogicalDylib(BaseLayerT & BaseLayer)41 LogicalDylib(BaseLayerT &BaseLayer) : BaseLayer(BaseLayer) {} 42 ~LogicalDylib()43 ~LogicalDylib() { 44 for (auto &LM : LogicalModules) 45 for (auto BLH : LM.BaseLayerHandles) 46 BaseLayer.removeModuleSet(BLH); 47 } 48 createLogicalModule()49 LogicalModuleHandle createLogicalModule() { 50 LogicalModules.push_back(LogicalModule()); 51 return std::prev(LogicalModules.end()); 52 } 53 addToLogicalModule(LogicalModuleHandle LMH,BaseLayerModuleSetHandleT BaseLayerHandle)54 void addToLogicalModule(LogicalModuleHandle LMH, 55 BaseLayerModuleSetHandleT BaseLayerHandle) { 56 LMH->BaseLayerHandles.push_back(BaseLayerHandle); 57 } 58 getLogicalModuleResources(LogicalModuleHandle LMH)59 LogicalModuleResources& getLogicalModuleResources(LogicalModuleHandle LMH) { 60 return LMH->Resources; 61 } 62 moduleHandlesBegin(LogicalModuleHandle LMH)63 BaseLayerHandleIterator moduleHandlesBegin(LogicalModuleHandle LMH) { 64 return LMH->BaseLayerHandles.begin(); 65 } 66 moduleHandlesEnd(LogicalModuleHandle LMH)67 BaseLayerHandleIterator moduleHandlesEnd(LogicalModuleHandle LMH) { 68 return LMH->BaseLayerHandles.end(); 69 } 70 findSymbolInLogicalModule(LogicalModuleHandle LMH,const std::string & Name)71 JITSymbol findSymbolInLogicalModule(LogicalModuleHandle LMH, 72 const std::string &Name) { 73 for (auto BLH : LMH->BaseLayerHandles) 74 if (auto Symbol = BaseLayer.findSymbolIn(BLH, Name, false)) 75 return Symbol; 76 return nullptr; 77 } 78 findSymbolInternally(LogicalModuleHandle LMH,const std::string & Name)79 JITSymbol findSymbolInternally(LogicalModuleHandle LMH, 80 const std::string &Name) { 81 if (auto Symbol = findSymbolInLogicalModule(LMH, Name)) 82 return Symbol; 83 84 for (auto LMI = LogicalModules.begin(), LME = LogicalModules.end(); 85 LMI != LME; ++LMI) { 86 if (LMI != LMH) 87 if (auto Symbol = findSymbolInLogicalModule(LMI, Name)) 88 return Symbol; 89 } 90 91 return nullptr; 92 } 93 findSymbol(const std::string & Name,bool ExportedSymbolsOnly)94 JITSymbol findSymbol(const std::string &Name, bool ExportedSymbolsOnly) { 95 for (auto &LM : LogicalModules) 96 for (auto BLH : LM.BaseLayerHandles) 97 if (auto Symbol = 98 BaseLayer.findSymbolIn(BLH, Name, ExportedSymbolsOnly)) 99 return Symbol; 100 return nullptr; 101 } 102 getDylibResources()103 LogicalDylibResources& getDylibResources() { return DylibResources; } 104 105 protected: 106 BaseLayerT BaseLayer; 107 LogicalModuleList LogicalModules; 108 LogicalDylibResources DylibResources; 109 110 }; 111 112 } // End namespace orc. 113 } // End namespace llvm. 114 115 #endif // LLVM_EXECUTIONENGINE_ORC_LOGICALDYLIB_H 116