1 //===---- OrcMCJITReplacement.h - Orc based MCJIT replacement ---*- 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 // Orc based MCJIT replacement. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #ifndef LLVM_LIB_EXECUTIONENGINE_ORC_ORCMCJITREPLACEMENT_H 15 #define LLVM_LIB_EXECUTIONENGINE_ORC_ORCMCJITREPLACEMENT_H 16 17 #include "llvm/ExecutionEngine/ExecutionEngine.h" 18 #include "llvm/ExecutionEngine/Orc/CompileUtils.h" 19 #include "llvm/ExecutionEngine/Orc/IRCompileLayer.h" 20 #include "llvm/ExecutionEngine/Orc/LazyEmittingLayer.h" 21 #include "llvm/ExecutionEngine/Orc/ObjectLinkingLayer.h" 22 #include "llvm/Object/Archive.h" 23 24 namespace llvm { 25 namespace orc { 26 27 class OrcMCJITReplacement : public ExecutionEngine { 28 29 // OrcMCJITReplacement needs to do a little extra book-keeping to ensure that 30 // Orc's automatic finalization doesn't kick in earlier than MCJIT clients are 31 // expecting - see finalizeMemory. 32 class MCJITReplacementMemMgr : public MCJITMemoryManager { 33 public: MCJITReplacementMemMgr(OrcMCJITReplacement & M,std::shared_ptr<MCJITMemoryManager> ClientMM)34 MCJITReplacementMemMgr(OrcMCJITReplacement &M, 35 std::shared_ptr<MCJITMemoryManager> ClientMM) 36 : M(M), ClientMM(std::move(ClientMM)) {} 37 allocateCodeSection(uintptr_t Size,unsigned Alignment,unsigned SectionID,StringRef SectionName)38 uint8_t *allocateCodeSection(uintptr_t Size, unsigned Alignment, 39 unsigned SectionID, 40 StringRef SectionName) override { 41 uint8_t *Addr = 42 ClientMM->allocateCodeSection(Size, Alignment, SectionID, 43 SectionName); 44 M.SectionsAllocatedSinceLastLoad.insert(Addr); 45 return Addr; 46 } 47 allocateDataSection(uintptr_t Size,unsigned Alignment,unsigned SectionID,StringRef SectionName,bool IsReadOnly)48 uint8_t *allocateDataSection(uintptr_t Size, unsigned Alignment, 49 unsigned SectionID, StringRef SectionName, 50 bool IsReadOnly) override { 51 uint8_t *Addr = ClientMM->allocateDataSection(Size, Alignment, SectionID, 52 SectionName, IsReadOnly); 53 M.SectionsAllocatedSinceLastLoad.insert(Addr); 54 return Addr; 55 } 56 reserveAllocationSpace(uintptr_t CodeSize,uintptr_t DataSizeRO,uintptr_t DataSizeRW)57 void reserveAllocationSpace(uintptr_t CodeSize, uintptr_t DataSizeRO, 58 uintptr_t DataSizeRW) override { 59 return ClientMM->reserveAllocationSpace(CodeSize, DataSizeRO, 60 DataSizeRW); 61 } 62 needsToReserveAllocationSpace()63 bool needsToReserveAllocationSpace() override { 64 return ClientMM->needsToReserveAllocationSpace(); 65 } 66 registerEHFrames(uint8_t * Addr,uint64_t LoadAddr,size_t Size)67 void registerEHFrames(uint8_t *Addr, uint64_t LoadAddr, 68 size_t Size) override { 69 return ClientMM->registerEHFrames(Addr, LoadAddr, Size); 70 } 71 deregisterEHFrames(uint8_t * Addr,uint64_t LoadAddr,size_t Size)72 void deregisterEHFrames(uint8_t *Addr, uint64_t LoadAddr, 73 size_t Size) override { 74 return ClientMM->deregisterEHFrames(Addr, LoadAddr, Size); 75 } 76 notifyObjectLoaded(ExecutionEngine * EE,const object::ObjectFile & O)77 void notifyObjectLoaded(ExecutionEngine *EE, 78 const object::ObjectFile &O) override { 79 return ClientMM->notifyObjectLoaded(EE, O); 80 } 81 82 bool finalizeMemory(std::string *ErrMsg = nullptr) override { 83 // Each set of objects loaded will be finalized exactly once, but since 84 // symbol lookup during relocation may recursively trigger the 85 // loading/relocation of other modules, and since we're forwarding all 86 // finalizeMemory calls to a single underlying memory manager, we need to 87 // defer forwarding the call on until all necessary objects have been 88 // loaded. Otherwise, during the relocation of a leaf object, we will end 89 // up finalizing memory, causing a crash further up the stack when we 90 // attempt to apply relocations to finalized memory. 91 // To avoid finalizing too early, look at how many objects have been 92 // loaded but not yet finalized. This is a bit of a hack that relies on 93 // the fact that we're lazily emitting object files: The only way you can 94 // get more than one set of objects loaded but not yet finalized is if 95 // they were loaded during relocation of another set. 96 if (M.UnfinalizedSections.size() == 1) 97 return ClientMM->finalizeMemory(ErrMsg); 98 return false; 99 } 100 101 private: 102 OrcMCJITReplacement &M; 103 std::shared_ptr<MCJITMemoryManager> ClientMM; 104 }; 105 106 class LinkingResolver : public RuntimeDyld::SymbolResolver { 107 public: LinkingResolver(OrcMCJITReplacement & M)108 LinkingResolver(OrcMCJITReplacement &M) : M(M) {} 109 findSymbol(const std::string & Name)110 RuntimeDyld::SymbolInfo findSymbol(const std::string &Name) override { 111 return M.findMangledSymbol(Name); 112 } 113 114 RuntimeDyld::SymbolInfo findSymbolInLogicalDylib(const std::string & Name)115 findSymbolInLogicalDylib(const std::string &Name) override { 116 return M.ClientResolver->findSymbolInLogicalDylib(Name); 117 } 118 119 private: 120 OrcMCJITReplacement &M; 121 }; 122 123 private: 124 125 static ExecutionEngine * createOrcMCJITReplacement(std::string * ErrorMsg,std::shared_ptr<MCJITMemoryManager> MemMgr,std::shared_ptr<RuntimeDyld::SymbolResolver> Resolver,std::unique_ptr<TargetMachine> TM)126 createOrcMCJITReplacement(std::string *ErrorMsg, 127 std::shared_ptr<MCJITMemoryManager> MemMgr, 128 std::shared_ptr<RuntimeDyld::SymbolResolver> Resolver, 129 std::unique_ptr<TargetMachine> TM) { 130 return new OrcMCJITReplacement(std::move(MemMgr), std::move(Resolver), 131 std::move(TM)); 132 } 133 134 public: Register()135 static void Register() { 136 OrcMCJITReplacementCtor = createOrcMCJITReplacement; 137 } 138 OrcMCJITReplacement(std::shared_ptr<MCJITMemoryManager> MemMgr,std::shared_ptr<RuntimeDyld::SymbolResolver> ClientResolver,std::unique_ptr<TargetMachine> TM)139 OrcMCJITReplacement( 140 std::shared_ptr<MCJITMemoryManager> MemMgr, 141 std::shared_ptr<RuntimeDyld::SymbolResolver> ClientResolver, 142 std::unique_ptr<TargetMachine> TM) 143 : TM(std::move(TM)), MemMgr(*this, std::move(MemMgr)), 144 Resolver(*this), ClientResolver(std::move(ClientResolver)), 145 Mang(this->TM->getDataLayout()), 146 NotifyObjectLoaded(*this), NotifyFinalized(*this), 147 ObjectLayer(NotifyObjectLoaded, NotifyFinalized), 148 CompileLayer(ObjectLayer, SimpleCompiler(*this->TM)), 149 LazyEmitLayer(CompileLayer) { 150 setDataLayout(this->TM->getDataLayout()); 151 } 152 addModule(std::unique_ptr<Module> M)153 void addModule(std::unique_ptr<Module> M) override { 154 155 // If this module doesn't have a DataLayout attached then attach the 156 // default. 157 if (M->getDataLayout().isDefault()) 158 M->setDataLayout(*getDataLayout()); 159 160 Modules.push_back(std::move(M)); 161 std::vector<Module *> Ms; 162 Ms.push_back(&*Modules.back()); 163 LazyEmitLayer.addModuleSet(std::move(Ms), &MemMgr, &Resolver); 164 } 165 addObjectFile(std::unique_ptr<object::ObjectFile> O)166 void addObjectFile(std::unique_ptr<object::ObjectFile> O) override { 167 std::vector<std::unique_ptr<object::ObjectFile>> Objs; 168 Objs.push_back(std::move(O)); 169 ObjectLayer.addObjectSet(std::move(Objs), &MemMgr, &Resolver); 170 } 171 addObjectFile(object::OwningBinary<object::ObjectFile> O)172 void addObjectFile(object::OwningBinary<object::ObjectFile> O) override { 173 std::unique_ptr<object::ObjectFile> Obj; 174 std::unique_ptr<MemoryBuffer> Buf; 175 std::tie(Obj, Buf) = O.takeBinary(); 176 std::vector<std::unique_ptr<object::ObjectFile>> Objs; 177 Objs.push_back(std::move(Obj)); 178 auto H = 179 ObjectLayer.addObjectSet(std::move(Objs), &MemMgr, &Resolver); 180 181 std::vector<std::unique_ptr<MemoryBuffer>> Bufs; 182 Bufs.push_back(std::move(Buf)); 183 ObjectLayer.takeOwnershipOfBuffers(H, std::move(Bufs)); 184 } 185 addArchive(object::OwningBinary<object::Archive> A)186 void addArchive(object::OwningBinary<object::Archive> A) override { 187 Archives.push_back(std::move(A)); 188 } 189 getSymbolAddress(StringRef Name)190 uint64_t getSymbolAddress(StringRef Name) { 191 return findSymbol(Name).getAddress(); 192 } 193 findSymbol(StringRef Name)194 RuntimeDyld::SymbolInfo findSymbol(StringRef Name) { 195 return findMangledSymbol(Mangle(Name)); 196 } 197 finalizeObject()198 void finalizeObject() override { 199 // This is deprecated - Aim to remove in ExecutionEngine. 200 // REMOVE IF POSSIBLE - Doesn't make sense for New JIT. 201 } 202 mapSectionAddress(const void * LocalAddress,uint64_t TargetAddress)203 void mapSectionAddress(const void *LocalAddress, 204 uint64_t TargetAddress) override { 205 for (auto &P : UnfinalizedSections) 206 if (P.second.count(LocalAddress)) 207 ObjectLayer.mapSectionAddress(P.first, LocalAddress, TargetAddress); 208 } 209 getGlobalValueAddress(const std::string & Name)210 uint64_t getGlobalValueAddress(const std::string &Name) override { 211 return getSymbolAddress(Name); 212 } 213 getFunctionAddress(const std::string & Name)214 uint64_t getFunctionAddress(const std::string &Name) override { 215 return getSymbolAddress(Name); 216 } 217 getPointerToFunction(Function * F)218 void *getPointerToFunction(Function *F) override { 219 uint64_t FAddr = getSymbolAddress(F->getName()); 220 return reinterpret_cast<void *>(static_cast<uintptr_t>(FAddr)); 221 } 222 223 void *getPointerToNamedFunction(StringRef Name, 224 bool AbortOnFailure = true) override { 225 uint64_t Addr = getSymbolAddress(Name); 226 if (!Addr && AbortOnFailure) 227 llvm_unreachable("Missing symbol!"); 228 return reinterpret_cast<void *>(static_cast<uintptr_t>(Addr)); 229 } 230 231 GenericValue runFunction(Function *F, 232 const std::vector<GenericValue> &ArgValues) override; 233 setObjectCache(ObjectCache * NewCache)234 void setObjectCache(ObjectCache *NewCache) override { 235 CompileLayer.setObjectCache(NewCache); 236 } 237 238 private: 239 findMangledSymbol(StringRef Name)240 RuntimeDyld::SymbolInfo findMangledSymbol(StringRef Name) { 241 if (auto Sym = LazyEmitLayer.findSymbol(Name, false)) 242 return RuntimeDyld::SymbolInfo(Sym.getAddress(), Sym.getFlags()); 243 if (auto Sym = ClientResolver->findSymbol(Name)) 244 return RuntimeDyld::SymbolInfo(Sym.getAddress(), Sym.getFlags()); 245 if (auto Sym = scanArchives(Name)) 246 return RuntimeDyld::SymbolInfo(Sym.getAddress(), Sym.getFlags()); 247 248 return nullptr; 249 } 250 scanArchives(StringRef Name)251 JITSymbol scanArchives(StringRef Name) { 252 for (object::OwningBinary<object::Archive> &OB : Archives) { 253 object::Archive *A = OB.getBinary(); 254 // Look for our symbols in each Archive 255 object::Archive::child_iterator ChildIt = A->findSym(Name); 256 if (ChildIt != A->child_end()) { 257 // FIXME: Support nested archives? 258 ErrorOr<std::unique_ptr<object::Binary>> ChildBinOrErr = 259 ChildIt->getAsBinary(); 260 if (ChildBinOrErr.getError()) 261 continue; 262 std::unique_ptr<object::Binary> &ChildBin = ChildBinOrErr.get(); 263 if (ChildBin->isObject()) { 264 std::vector<std::unique_ptr<object::ObjectFile>> ObjSet; 265 ObjSet.push_back(std::unique_ptr<object::ObjectFile>( 266 static_cast<object::ObjectFile *>(ChildBin.release()))); 267 ObjectLayer.addObjectSet(std::move(ObjSet), &MemMgr, &Resolver); 268 if (auto Sym = ObjectLayer.findSymbol(Name, true)) 269 return Sym; 270 } 271 } 272 } 273 return nullptr; 274 } 275 276 class NotifyObjectLoadedT { 277 public: 278 typedef std::vector<std::unique_ptr<object::ObjectFile>> ObjListT; 279 typedef std::vector<std::unique_ptr<RuntimeDyld::LoadedObjectInfo>> 280 LoadedObjInfoListT; 281 NotifyObjectLoadedT(OrcMCJITReplacement & M)282 NotifyObjectLoadedT(OrcMCJITReplacement &M) : M(M) {} 283 operator()284 void operator()(ObjectLinkingLayerBase::ObjSetHandleT H, 285 const ObjListT &Objects, 286 const LoadedObjInfoListT &Infos) const { 287 M.UnfinalizedSections[H] = std::move(M.SectionsAllocatedSinceLastLoad); 288 M.SectionsAllocatedSinceLastLoad = SectionAddrSet(); 289 assert(Objects.size() == Infos.size() && 290 "Incorrect number of Infos for Objects."); 291 for (unsigned I = 0; I < Objects.size(); ++I) 292 M.MemMgr.notifyObjectLoaded(&M, *Objects[I]); 293 }; 294 295 private: 296 OrcMCJITReplacement &M; 297 }; 298 299 class NotifyFinalizedT { 300 public: NotifyFinalizedT(OrcMCJITReplacement & M)301 NotifyFinalizedT(OrcMCJITReplacement &M) : M(M) {} operator()302 void operator()(ObjectLinkingLayerBase::ObjSetHandleT H) { 303 M.UnfinalizedSections.erase(H); 304 } 305 306 private: 307 OrcMCJITReplacement &M; 308 }; 309 Mangle(StringRef Name)310 std::string Mangle(StringRef Name) { 311 std::string MangledName; 312 { 313 raw_string_ostream MangledNameStream(MangledName); 314 Mang.getNameWithPrefix(MangledNameStream, Name); 315 } 316 return MangledName; 317 } 318 319 typedef ObjectLinkingLayer<NotifyObjectLoadedT> ObjectLayerT; 320 typedef IRCompileLayer<ObjectLayerT> CompileLayerT; 321 typedef LazyEmittingLayer<CompileLayerT> LazyEmitLayerT; 322 323 std::unique_ptr<TargetMachine> TM; 324 MCJITReplacementMemMgr MemMgr; 325 LinkingResolver Resolver; 326 std::shared_ptr<RuntimeDyld::SymbolResolver> ClientResolver; 327 Mangler Mang; 328 329 NotifyObjectLoadedT NotifyObjectLoaded; 330 NotifyFinalizedT NotifyFinalized; 331 332 ObjectLayerT ObjectLayer; 333 CompileLayerT CompileLayer; 334 LazyEmitLayerT LazyEmitLayer; 335 336 // We need to store ObjLayerT::ObjSetHandles for each of the object sets 337 // that have been emitted but not yet finalized so that we can forward the 338 // mapSectionAddress calls appropriately. 339 typedef std::set<const void *> SectionAddrSet; 340 struct ObjSetHandleCompare { operatorObjSetHandleCompare341 bool operator()(ObjectLayerT::ObjSetHandleT H1, 342 ObjectLayerT::ObjSetHandleT H2) const { 343 return &*H1 < &*H2; 344 } 345 }; 346 SectionAddrSet SectionsAllocatedSinceLastLoad; 347 std::map<ObjectLayerT::ObjSetHandleT, SectionAddrSet, ObjSetHandleCompare> 348 UnfinalizedSections; 349 350 std::vector<object::OwningBinary<object::Archive>> Archives; 351 }; 352 353 } // End namespace orc. 354 } // End namespace llvm. 355 356 #endif // LLVM_LIB_EXECUTIONENGINE_ORC_MCJITREPLACEMENT_H 357