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