Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 1 | //===- RTDyldObjectLinkingLayer.h - RTDyld-based jit linking ---*- 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 | // Contains the definition for an RTDyld-based, in-process object linking layer. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #ifndef LLVM_EXECUTIONENGINE_ORC_RTDYLDOBJECTLINKINGLAYER_H |
| 15 | #define LLVM_EXECUTIONENGINE_ORC_RTDYLDOBJECTLINKINGLAYER_H |
| 16 | |
| 17 | #include "llvm/ADT/STLExtras.h" |
| 18 | #include "llvm/ADT/StringMap.h" |
| 19 | #include "llvm/ADT/StringRef.h" |
| 20 | #include "llvm/ExecutionEngine/JITSymbol.h" |
| 21 | #include "llvm/ExecutionEngine/Orc/Core.h" |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 22 | #include "llvm/ExecutionEngine/Orc/Layer.h" |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 23 | #include "llvm/ExecutionEngine/Orc/Legacy.h" |
| 24 | #include "llvm/ExecutionEngine/RuntimeDyld.h" |
| 25 | #include "llvm/Object/ObjectFile.h" |
| 26 | #include "llvm/Support/Error.h" |
| 27 | #include <algorithm> |
| 28 | #include <cassert> |
| 29 | #include <functional> |
| 30 | #include <list> |
| 31 | #include <memory> |
| 32 | #include <string> |
| 33 | #include <utility> |
| 34 | #include <vector> |
| 35 | |
| 36 | namespace llvm { |
| 37 | namespace orc { |
| 38 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 39 | class RTDyldObjectLinkingLayer2 : public ObjectLayer { |
| 40 | public: |
| 41 | /// Functor for receiving object-loaded notifications. |
| 42 | using NotifyLoadedFunction = |
| 43 | std::function<void(VModuleKey, const object::ObjectFile &Obj, |
| 44 | const RuntimeDyld::LoadedObjectInfo &)>; |
| 45 | |
| 46 | /// Functor for receiving finalization notifications. |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame^] | 47 | using NotifyEmittedFunction = std::function<void(VModuleKey)>; |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 48 | |
| 49 | using GetMemoryManagerFunction = |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame^] | 50 | std::function<std::unique_ptr<RuntimeDyld::MemoryManager>(VModuleKey)>; |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 51 | |
| 52 | /// Construct an ObjectLinkingLayer with the given NotifyLoaded, |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame^] | 53 | /// and NotifyEmitted functors. |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 54 | RTDyldObjectLinkingLayer2( |
| 55 | ExecutionSession &ES, GetMemoryManagerFunction GetMemoryManager, |
| 56 | NotifyLoadedFunction NotifyLoaded = NotifyLoadedFunction(), |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame^] | 57 | NotifyEmittedFunction NotifyEmitted = NotifyEmittedFunction()); |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 58 | |
| 59 | /// Emit the object. |
| 60 | void emit(MaterializationResponsibility R, VModuleKey K, |
| 61 | std::unique_ptr<MemoryBuffer> O) override; |
| 62 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 63 | /// Set the 'ProcessAllSections' flag. |
| 64 | /// |
| 65 | /// If set to true, all sections in each object file will be allocated using |
| 66 | /// the memory manager, rather than just the sections required for execution. |
| 67 | /// |
| 68 | /// This is kludgy, and may be removed in the future. |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame^] | 69 | RTDyldObjectLinkingLayer2 &setProcessAllSections(bool ProcessAllSections) { |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 70 | this->ProcessAllSections = ProcessAllSections; |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame^] | 71 | return *this; |
| 72 | } |
| 73 | |
| 74 | /// Instructs this RTDyldLinkingLayer2 instance to override the symbol flags |
| 75 | /// returned by RuntimeDyld for any given object file with the flags supplied |
| 76 | /// by the MaterializationResponsibility instance. This is a workaround to |
| 77 | /// support symbol visibility in COFF, which does not use the libObject's |
| 78 | /// SF_Exported flag. Use only when generating / adding COFF object files. |
| 79 | /// |
| 80 | /// FIXME: We should be able to remove this if/when COFF properly tracks |
| 81 | /// exported symbols. |
| 82 | RTDyldObjectLinkingLayer2 & |
| 83 | setOverrideObjectFlagsWithResponsibilityFlags(bool OverrideObjectFlags) { |
| 84 | this->OverrideObjectFlags = OverrideObjectFlags; |
| 85 | return *this; |
| 86 | } |
| 87 | |
| 88 | /// If set, this RTDyldObjectLinkingLayer2 instance will claim responsibility |
| 89 | /// for any symbols provided by a given object file that were not already in |
| 90 | /// the MaterializationResponsibility instance. Setting this flag allows |
| 91 | /// higher-level program representations (e.g. LLVM IR) to be added based on |
| 92 | /// only a subset of the symbols they provide, without having to write |
| 93 | /// intervening layers to scan and add the additional symbols. This trades |
| 94 | /// diagnostic quality for convenience however: If all symbols are enumerated |
| 95 | /// up-front then clashes can be detected and reported early (and usually |
| 96 | /// deterministically). If this option is set, clashes for the additional |
| 97 | /// symbols may not be detected until late, and detection may depend on |
| 98 | /// the flow of control through JIT'd code. Use with care. |
| 99 | RTDyldObjectLinkingLayer2 & |
| 100 | setAutoClaimResponsibilityForObjectSymbols(bool AutoClaimObjectSymbols) { |
| 101 | this->AutoClaimObjectSymbols = AutoClaimObjectSymbols; |
| 102 | return *this; |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | private: |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame^] | 106 | Error onObjLoad(VModuleKey K, MaterializationResponsibility &R, |
| 107 | object::ObjectFile &Obj, |
| 108 | std::unique_ptr<RuntimeDyld::LoadedObjectInfo> LoadedObjInfo, |
| 109 | std::map<StringRef, JITEvaluatedSymbol> Resolved, |
| 110 | std::set<StringRef> &InternalSymbols); |
| 111 | |
| 112 | void onObjEmit(VModuleKey K, MaterializationResponsibility &R, Error Err); |
| 113 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 114 | mutable std::mutex RTDyldLayerMutex; |
| 115 | GetMemoryManagerFunction GetMemoryManager; |
| 116 | NotifyLoadedFunction NotifyLoaded; |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame^] | 117 | NotifyEmittedFunction NotifyEmitted; |
| 118 | bool ProcessAllSections = false; |
| 119 | bool OverrideObjectFlags = false; |
| 120 | bool AutoClaimObjectSymbols = false; |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 121 | std::map<VModuleKey, std::shared_ptr<RuntimeDyld::MemoryManager>> MemMgrs; |
| 122 | }; |
| 123 | |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 124 | class RTDyldObjectLinkingLayerBase { |
| 125 | public: |
| 126 | using ObjectPtr = std::unique_ptr<MemoryBuffer>; |
| 127 | |
| 128 | protected: |
| 129 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 130 | /// Holds an object to be allocated/linked as a unit in the JIT. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 131 | /// |
| 132 | /// An instance of this class will be created for each object added |
| 133 | /// via JITObjectLayer::addObject. Deleting the instance (via |
| 134 | /// removeObject) frees its memory, removing all symbol definitions that |
| 135 | /// had been provided by this instance. Higher level layers are responsible |
| 136 | /// for taking any action required to handle the missing symbols. |
| 137 | class LinkedObject { |
| 138 | public: |
| 139 | LinkedObject() = default; |
| 140 | LinkedObject(const LinkedObject&) = delete; |
| 141 | void operator=(const LinkedObject&) = delete; |
| 142 | virtual ~LinkedObject() = default; |
| 143 | |
| 144 | virtual Error finalize() = 0; |
| 145 | |
| 146 | virtual JITSymbol::GetAddressFtor |
| 147 | getSymbolMaterializer(std::string Name) = 0; |
| 148 | |
| 149 | virtual void mapSectionAddress(const void *LocalAddress, |
| 150 | JITTargetAddress TargetAddr) const = 0; |
| 151 | |
| 152 | JITSymbol getSymbol(StringRef Name, bool ExportedSymbolsOnly) { |
| 153 | auto SymEntry = SymbolTable.find(Name); |
| 154 | if (SymEntry == SymbolTable.end()) |
| 155 | return nullptr; |
| 156 | if (!SymEntry->second.getFlags().isExported() && ExportedSymbolsOnly) |
| 157 | return nullptr; |
| 158 | if (!Finalized) |
| 159 | return JITSymbol(getSymbolMaterializer(Name), |
| 160 | SymEntry->second.getFlags()); |
| 161 | return JITSymbol(SymEntry->second); |
| 162 | } |
| 163 | |
| 164 | protected: |
| 165 | StringMap<JITEvaluatedSymbol> SymbolTable; |
| 166 | bool Finalized = false; |
| 167 | }; |
| 168 | }; |
| 169 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 170 | /// Bare bones object linking layer. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 171 | /// |
| 172 | /// This class is intended to be used as the base layer for a JIT. It allows |
| 173 | /// object files to be loaded into memory, linked, and the addresses of their |
| 174 | /// symbols queried. All objects added to this layer can see each other's |
| 175 | /// symbols. |
| 176 | class RTDyldObjectLinkingLayer : public RTDyldObjectLinkingLayerBase { |
| 177 | public: |
| 178 | |
| 179 | using RTDyldObjectLinkingLayerBase::ObjectPtr; |
| 180 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 181 | /// Functor for receiving object-loaded notifications. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 182 | using NotifyLoadedFtor = |
| 183 | std::function<void(VModuleKey, const object::ObjectFile &Obj, |
| 184 | const RuntimeDyld::LoadedObjectInfo &)>; |
| 185 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 186 | /// Functor for receiving finalization notifications. |
| 187 | using NotifyFinalizedFtor = |
| 188 | std::function<void(VModuleKey, const object::ObjectFile &Obj, |
| 189 | const RuntimeDyld::LoadedObjectInfo &)>; |
| 190 | |
| 191 | /// Functor for receiving deallocation notifications. |
| 192 | using NotifyFreedFtor = std::function<void(VModuleKey, const object::ObjectFile &Obj)>; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 193 | |
| 194 | private: |
| 195 | using OwnedObject = object::OwningBinary<object::ObjectFile>; |
| 196 | |
| 197 | template <typename MemoryManagerPtrT> |
| 198 | class ConcreteLinkedObject : public LinkedObject { |
| 199 | public: |
| 200 | ConcreteLinkedObject(RTDyldObjectLinkingLayer &Parent, VModuleKey K, |
| 201 | OwnedObject Obj, MemoryManagerPtrT MemMgr, |
| 202 | std::shared_ptr<SymbolResolver> Resolver, |
| 203 | bool ProcessAllSections) |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 204 | : K(std::move(K)), |
| 205 | Parent(Parent), |
| 206 | MemMgr(std::move(MemMgr)), |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 207 | PFC(llvm::make_unique<PreFinalizeContents>( |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 208 | std::move(Obj), std::move(Resolver), |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 209 | ProcessAllSections)) { |
| 210 | buildInitialSymbolTable(PFC->Obj); |
| 211 | } |
| 212 | |
| 213 | ~ConcreteLinkedObject() override { |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame^] | 214 | if (this->Parent.NotifyFreed && ObjForNotify.getBinary()) |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 215 | this->Parent.NotifyFreed(K, *ObjForNotify.getBinary()); |
| 216 | |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 217 | MemMgr->deregisterEHFrames(); |
| 218 | } |
| 219 | |
| 220 | Error finalize() override { |
| 221 | assert(PFC && "mapSectionAddress called on finalized LinkedObject"); |
| 222 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 223 | JITSymbolResolverAdapter ResolverAdapter(Parent.ES, *PFC->Resolver, |
| 224 | nullptr); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 225 | PFC->RTDyld = llvm::make_unique<RuntimeDyld>(*MemMgr, ResolverAdapter); |
| 226 | PFC->RTDyld->setProcessAllSections(PFC->ProcessAllSections); |
| 227 | |
| 228 | Finalized = true; |
| 229 | |
| 230 | std::unique_ptr<RuntimeDyld::LoadedObjectInfo> Info = |
| 231 | PFC->RTDyld->loadObject(*PFC->Obj.getBinary()); |
| 232 | |
| 233 | // Copy the symbol table out of the RuntimeDyld instance. |
| 234 | { |
| 235 | auto SymTab = PFC->RTDyld->getSymbolTable(); |
| 236 | for (auto &KV : SymTab) |
| 237 | SymbolTable[KV.first] = KV.second; |
| 238 | } |
| 239 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 240 | if (Parent.NotifyLoaded) |
| 241 | Parent.NotifyLoaded(K, *PFC->Obj.getBinary(), *Info); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 242 | |
| 243 | PFC->RTDyld->finalizeWithMemoryManagerLocking(); |
| 244 | |
| 245 | if (PFC->RTDyld->hasError()) |
| 246 | return make_error<StringError>(PFC->RTDyld->getErrorString(), |
| 247 | inconvertibleErrorCode()); |
| 248 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 249 | if (Parent.NotifyFinalized) |
| 250 | Parent.NotifyFinalized(K, *PFC->Obj.getBinary(), *Info); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 251 | |
| 252 | // Release resources. |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 253 | if (this->Parent.NotifyFreed) |
| 254 | ObjForNotify = std::move(PFC->Obj); // needed for callback |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 255 | PFC = nullptr; |
| 256 | return Error::success(); |
| 257 | } |
| 258 | |
| 259 | JITSymbol::GetAddressFtor getSymbolMaterializer(std::string Name) override { |
| 260 | return [this, Name]() -> Expected<JITTargetAddress> { |
| 261 | // The symbol may be materialized between the creation of this lambda |
| 262 | // and its execution, so we need to double check. |
| 263 | if (!this->Finalized) |
| 264 | if (auto Err = this->finalize()) |
| 265 | return std::move(Err); |
| 266 | return this->getSymbol(Name, false).getAddress(); |
| 267 | }; |
| 268 | } |
| 269 | |
| 270 | void mapSectionAddress(const void *LocalAddress, |
| 271 | JITTargetAddress TargetAddr) const override { |
| 272 | assert(PFC && "mapSectionAddress called on finalized LinkedObject"); |
| 273 | assert(PFC->RTDyld && "mapSectionAddress called on raw LinkedObject"); |
| 274 | PFC->RTDyld->mapSectionAddress(LocalAddress, TargetAddr); |
| 275 | } |
| 276 | |
| 277 | private: |
| 278 | void buildInitialSymbolTable(const OwnedObject &Obj) { |
| 279 | for (auto &Symbol : Obj.getBinary()->symbols()) { |
| 280 | if (Symbol.getFlags() & object::SymbolRef::SF_Undefined) |
| 281 | continue; |
| 282 | Expected<StringRef> SymbolName = Symbol.getName(); |
| 283 | // FIXME: Raise an error for bad symbols. |
| 284 | if (!SymbolName) { |
| 285 | consumeError(SymbolName.takeError()); |
| 286 | continue; |
| 287 | } |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 288 | // FIXME: Raise an error for bad symbols. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 289 | auto Flags = JITSymbolFlags::fromObjectSymbol(Symbol); |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 290 | if (!Flags) { |
| 291 | consumeError(Flags.takeError()); |
| 292 | continue; |
| 293 | } |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 294 | SymbolTable.insert( |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 295 | std::make_pair(*SymbolName, JITEvaluatedSymbol(0, *Flags))); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 296 | } |
| 297 | } |
| 298 | |
| 299 | // Contains the information needed prior to finalization: the object files, |
| 300 | // memory manager, resolver, and flags needed for RuntimeDyld. |
| 301 | struct PreFinalizeContents { |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 302 | PreFinalizeContents(OwnedObject Obj, |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 303 | std::shared_ptr<SymbolResolver> Resolver, |
| 304 | bool ProcessAllSections) |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 305 | : Obj(std::move(Obj)), |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 306 | Resolver(std::move(Resolver)), |
| 307 | ProcessAllSections(ProcessAllSections) {} |
| 308 | |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 309 | OwnedObject Obj; |
| 310 | std::shared_ptr<SymbolResolver> Resolver; |
| 311 | bool ProcessAllSections; |
| 312 | std::unique_ptr<RuntimeDyld> RTDyld; |
| 313 | }; |
| 314 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 315 | VModuleKey K; |
| 316 | RTDyldObjectLinkingLayer &Parent; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 317 | MemoryManagerPtrT MemMgr; |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 318 | OwnedObject ObjForNotify; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 319 | std::unique_ptr<PreFinalizeContents> PFC; |
| 320 | }; |
| 321 | |
| 322 | template <typename MemoryManagerPtrT> |
| 323 | std::unique_ptr<ConcreteLinkedObject<MemoryManagerPtrT>> |
| 324 | createLinkedObject(RTDyldObjectLinkingLayer &Parent, VModuleKey K, |
| 325 | OwnedObject Obj, MemoryManagerPtrT MemMgr, |
| 326 | std::shared_ptr<SymbolResolver> Resolver, |
| 327 | bool ProcessAllSections) { |
| 328 | using LOS = ConcreteLinkedObject<MemoryManagerPtrT>; |
| 329 | return llvm::make_unique<LOS>(Parent, std::move(K), std::move(Obj), |
| 330 | std::move(MemMgr), std::move(Resolver), |
| 331 | ProcessAllSections); |
| 332 | } |
| 333 | |
| 334 | public: |
| 335 | struct Resources { |
| 336 | std::shared_ptr<RuntimeDyld::MemoryManager> MemMgr; |
| 337 | std::shared_ptr<SymbolResolver> Resolver; |
| 338 | }; |
| 339 | |
| 340 | using ResourcesGetter = std::function<Resources(VModuleKey)>; |
| 341 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 342 | /// Construct an ObjectLinkingLayer with the given NotifyLoaded, |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 343 | /// and NotifyFinalized functors. |
| 344 | RTDyldObjectLinkingLayer( |
| 345 | ExecutionSession &ES, ResourcesGetter GetResources, |
| 346 | NotifyLoadedFtor NotifyLoaded = NotifyLoadedFtor(), |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 347 | NotifyFinalizedFtor NotifyFinalized = NotifyFinalizedFtor(), |
| 348 | NotifyFreedFtor NotifyFreed = NotifyFreedFtor()) |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 349 | : ES(ES), GetResources(std::move(GetResources)), |
| 350 | NotifyLoaded(std::move(NotifyLoaded)), |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 351 | NotifyFinalized(std::move(NotifyFinalized)), |
| 352 | NotifyFreed(std::move(NotifyFreed)), |
| 353 | ProcessAllSections(false) { |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 354 | } |
| 355 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 356 | /// Set the 'ProcessAllSections' flag. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 357 | /// |
| 358 | /// If set to true, all sections in each object file will be allocated using |
| 359 | /// the memory manager, rather than just the sections required for execution. |
| 360 | /// |
| 361 | /// This is kludgy, and may be removed in the future. |
| 362 | void setProcessAllSections(bool ProcessAllSections) { |
| 363 | this->ProcessAllSections = ProcessAllSections; |
| 364 | } |
| 365 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 366 | /// Add an object to the JIT. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 367 | Error addObject(VModuleKey K, ObjectPtr ObjBuffer) { |
| 368 | |
| 369 | auto Obj = |
| 370 | object::ObjectFile::createObjectFile(ObjBuffer->getMemBufferRef()); |
| 371 | if (!Obj) |
| 372 | return Obj.takeError(); |
| 373 | |
| 374 | assert(!LinkedObjects.count(K) && "VModuleKey already in use"); |
| 375 | |
| 376 | auto R = GetResources(K); |
| 377 | |
| 378 | LinkedObjects[K] = createLinkedObject( |
| 379 | *this, K, OwnedObject(std::move(*Obj), std::move(ObjBuffer)), |
| 380 | std::move(R.MemMgr), std::move(R.Resolver), ProcessAllSections); |
| 381 | |
| 382 | return Error::success(); |
| 383 | } |
| 384 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 385 | /// Remove the object associated with VModuleKey K. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 386 | /// |
| 387 | /// All memory allocated for the object will be freed, and the sections and |
| 388 | /// symbols it provided will no longer be available. No attempt is made to |
| 389 | /// re-emit the missing symbols, and any use of these symbols (directly or |
| 390 | /// indirectly) will result in undefined behavior. If dependence tracking is |
| 391 | /// required to detect or resolve such issues it should be added at a higher |
| 392 | /// layer. |
| 393 | Error removeObject(VModuleKey K) { |
| 394 | assert(LinkedObjects.count(K) && "VModuleKey not associated with object"); |
| 395 | // How do we invalidate the symbols in H? |
| 396 | LinkedObjects.erase(K); |
| 397 | return Error::success(); |
| 398 | } |
| 399 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 400 | /// Search for the given named symbol. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 401 | /// @param Name The name of the symbol to search for. |
| 402 | /// @param ExportedSymbolsOnly If true, search only for exported symbols. |
| 403 | /// @return A handle for the given named symbol, if it exists. |
| 404 | JITSymbol findSymbol(StringRef Name, bool ExportedSymbolsOnly) { |
| 405 | for (auto &KV : LinkedObjects) |
| 406 | if (auto Sym = KV.second->getSymbol(Name, ExportedSymbolsOnly)) |
| 407 | return Sym; |
| 408 | else if (auto Err = Sym.takeError()) |
| 409 | return std::move(Err); |
| 410 | |
| 411 | return nullptr; |
| 412 | } |
| 413 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 414 | /// Search for the given named symbol in the context of the loaded |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 415 | /// object represented by the VModuleKey K. |
| 416 | /// @param K The VModuleKey for the object to search in. |
| 417 | /// @param Name The name of the symbol to search for. |
| 418 | /// @param ExportedSymbolsOnly If true, search only for exported symbols. |
| 419 | /// @return A handle for the given named symbol, if it is found in the |
| 420 | /// given object. |
| 421 | JITSymbol findSymbolIn(VModuleKey K, StringRef Name, |
| 422 | bool ExportedSymbolsOnly) { |
| 423 | assert(LinkedObjects.count(K) && "VModuleKey not associated with object"); |
| 424 | return LinkedObjects[K]->getSymbol(Name, ExportedSymbolsOnly); |
| 425 | } |
| 426 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 427 | /// Map section addresses for the object associated with the |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 428 | /// VModuleKey K. |
| 429 | void mapSectionAddress(VModuleKey K, const void *LocalAddress, |
| 430 | JITTargetAddress TargetAddr) { |
| 431 | assert(LinkedObjects.count(K) && "VModuleKey not associated with object"); |
| 432 | LinkedObjects[K]->mapSectionAddress(LocalAddress, TargetAddr); |
| 433 | } |
| 434 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 435 | /// Immediately emit and finalize the object represented by the given |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 436 | /// VModuleKey. |
| 437 | /// @param K VModuleKey for object to emit/finalize. |
| 438 | Error emitAndFinalize(VModuleKey K) { |
| 439 | assert(LinkedObjects.count(K) && "VModuleKey not associated with object"); |
| 440 | return LinkedObjects[K]->finalize(); |
| 441 | } |
| 442 | |
| 443 | private: |
| 444 | ExecutionSession &ES; |
| 445 | |
| 446 | std::map<VModuleKey, std::unique_ptr<LinkedObject>> LinkedObjects; |
| 447 | ResourcesGetter GetResources; |
| 448 | NotifyLoadedFtor NotifyLoaded; |
| 449 | NotifyFinalizedFtor NotifyFinalized; |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 450 | NotifyFreedFtor NotifyFreed; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 451 | bool ProcessAllSections = false; |
| 452 | }; |
| 453 | |
| 454 | } // end namespace orc |
| 455 | } // end namespace llvm |
| 456 | |
| 457 | #endif // LLVM_EXECUTIONENGINE_ORC_RTDYLDOBJECTLINKINGLAYER_H |