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" |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 19 | #include "llvm/ExecutionEngine/JITEventListener.h" |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 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/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 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 38 | class RTDyldObjectLinkingLayer : public ObjectLayer, private ResourceManager { |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 39 | public: |
| 40 | /// Functor for receiving object-loaded notifications. |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 41 | using NotifyLoadedFunction = std::function<void( |
| 42 | MaterializationResponsibility &R, const object::ObjectFile &Obj, |
| 43 | const RuntimeDyld::LoadedObjectInfo &)>; |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 44 | |
| 45 | /// Functor for receiving finalization notifications. |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 46 | using NotifyEmittedFunction = std::function<void( |
| 47 | MaterializationResponsibility &R, std::unique_ptr<MemoryBuffer>)>; |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 48 | |
| 49 | using GetMemoryManagerFunction = |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame] | 50 | std::function<std::unique_ptr<RuntimeDyld::MemoryManager>()>; |
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 Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 54 | RTDyldObjectLinkingLayer(ExecutionSession &ES, |
| 55 | GetMemoryManagerFunction GetMemoryManager); |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 56 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 57 | ~RTDyldObjectLinkingLayer(); |
| 58 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 59 | /// Emit the object. |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 60 | void emit(std::unique_ptr<MaterializationResponsibility> R, |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 61 | std::unique_ptr<MemoryBuffer> O) override; |
| 62 | |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 63 | /// Set the NotifyLoaded callback. |
| 64 | RTDyldObjectLinkingLayer &setNotifyLoaded(NotifyLoadedFunction NotifyLoaded) { |
| 65 | this->NotifyLoaded = std::move(NotifyLoaded); |
| 66 | return *this; |
| 67 | } |
| 68 | |
| 69 | /// Set the NotifyEmitted callback. |
| 70 | RTDyldObjectLinkingLayer & |
| 71 | setNotifyEmitted(NotifyEmittedFunction NotifyEmitted) { |
| 72 | this->NotifyEmitted = std::move(NotifyEmitted); |
| 73 | return *this; |
| 74 | } |
| 75 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 76 | /// Set the 'ProcessAllSections' flag. |
| 77 | /// |
| 78 | /// If set to true, all sections in each object file will be allocated using |
| 79 | /// the memory manager, rather than just the sections required for execution. |
| 80 | /// |
| 81 | /// This is kludgy, and may be removed in the future. |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame] | 82 | RTDyldObjectLinkingLayer &setProcessAllSections(bool ProcessAllSections) { |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 83 | this->ProcessAllSections = ProcessAllSections; |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame] | 84 | return *this; |
| 85 | } |
| 86 | |
| 87 | /// Instructs this RTDyldLinkingLayer2 instance to override the symbol flags |
| 88 | /// returned by RuntimeDyld for any given object file with the flags supplied |
| 89 | /// by the MaterializationResponsibility instance. This is a workaround to |
| 90 | /// support symbol visibility in COFF, which does not use the libObject's |
| 91 | /// SF_Exported flag. Use only when generating / adding COFF object files. |
| 92 | /// |
| 93 | /// FIXME: We should be able to remove this if/when COFF properly tracks |
| 94 | /// exported symbols. |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame] | 95 | RTDyldObjectLinkingLayer & |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame] | 96 | setOverrideObjectFlagsWithResponsibilityFlags(bool OverrideObjectFlags) { |
| 97 | this->OverrideObjectFlags = OverrideObjectFlags; |
| 98 | return *this; |
| 99 | } |
| 100 | |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame] | 101 | /// If set, this RTDyldObjectLinkingLayer instance will claim responsibility |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame] | 102 | /// for any symbols provided by a given object file that were not already in |
| 103 | /// the MaterializationResponsibility instance. Setting this flag allows |
| 104 | /// higher-level program representations (e.g. LLVM IR) to be added based on |
| 105 | /// only a subset of the symbols they provide, without having to write |
| 106 | /// intervening layers to scan and add the additional symbols. This trades |
| 107 | /// diagnostic quality for convenience however: If all symbols are enumerated |
| 108 | /// up-front then clashes can be detected and reported early (and usually |
| 109 | /// deterministically). If this option is set, clashes for the additional |
| 110 | /// symbols may not be detected until late, and detection may depend on |
| 111 | /// the flow of control through JIT'd code. Use with care. |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame] | 112 | RTDyldObjectLinkingLayer & |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame] | 113 | setAutoClaimResponsibilityForObjectSymbols(bool AutoClaimObjectSymbols) { |
| 114 | this->AutoClaimObjectSymbols = AutoClaimObjectSymbols; |
| 115 | return *this; |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 116 | } |
| 117 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 118 | /// Register a JITEventListener. |
| 119 | void registerJITEventListener(JITEventListener &L); |
| 120 | |
| 121 | /// Unregister a JITEventListener. |
| 122 | void unregisterJITEventListener(JITEventListener &L); |
| 123 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 124 | private: |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 125 | using MemoryManagerUP = std::unique_ptr<RuntimeDyld::MemoryManager>; |
| 126 | |
| 127 | Error onObjLoad(MaterializationResponsibility &R, |
| 128 | const object::ObjectFile &Obj, |
| 129 | RuntimeDyld::MemoryManager &MemMgr, |
| 130 | RuntimeDyld::LoadedObjectInfo &LoadedObjInfo, |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame] | 131 | std::map<StringRef, JITEvaluatedSymbol> Resolved, |
| 132 | std::set<StringRef> &InternalSymbols); |
| 133 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 134 | void onObjEmit(MaterializationResponsibility &R, |
| 135 | object::OwningBinary<object::ObjectFile> O, |
| 136 | std::unique_ptr<RuntimeDyld::MemoryManager> MemMgr, |
| 137 | std::unique_ptr<RuntimeDyld::LoadedObjectInfo> LoadedObjInfo, |
| 138 | Error Err); |
| 139 | |
| 140 | Error handleRemoveResources(ResourceKey K) override; |
| 141 | void handleTransferResources(ResourceKey DstKey, ResourceKey SrcKey) override; |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame] | 142 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 143 | mutable std::mutex RTDyldLayerMutex; |
| 144 | GetMemoryManagerFunction GetMemoryManager; |
| 145 | NotifyLoadedFunction NotifyLoaded; |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame] | 146 | NotifyEmittedFunction NotifyEmitted; |
| 147 | bool ProcessAllSections = false; |
| 148 | bool OverrideObjectFlags = false; |
| 149 | bool AutoClaimObjectSymbols = false; |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 150 | DenseMap<ResourceKey, std::vector<MemoryManagerUP>> MemMgrs; |
| 151 | std::vector<JITEventListener *> EventListeners; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 152 | }; |
| 153 | |
| 154 | } // end namespace orc |
| 155 | } // end namespace llvm |
| 156 | |
| 157 | #endif // LLVM_EXECUTIONENGINE_ORC_RTDYLDOBJECTLINKINGLAYER_H |