Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:43:54

0001 //===- RTDyldObjectLinkingLayer.h - RTDyld-based jit linking  ---*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 //
0009 // Contains the definition for an RTDyld-based, in-process object linking layer.
0010 //
0011 //===----------------------------------------------------------------------===//
0012 
0013 #ifndef LLVM_EXECUTIONENGINE_ORC_RTDYLDOBJECTLINKINGLAYER_H
0014 #define LLVM_EXECUTIONENGINE_ORC_RTDYLDOBJECTLINKINGLAYER_H
0015 
0016 #include "llvm/ADT/STLExtras.h"
0017 #include "llvm/ADT/StringRef.h"
0018 #include "llvm/ExecutionEngine/JITEventListener.h"
0019 #include "llvm/ExecutionEngine/JITSymbol.h"
0020 #include "llvm/ExecutionEngine/Orc/Core.h"
0021 #include "llvm/ExecutionEngine/Orc/Layer.h"
0022 #include "llvm/ExecutionEngine/RuntimeDyld.h"
0023 #include "llvm/Object/ObjectFile.h"
0024 #include "llvm/Support/Error.h"
0025 #include <algorithm>
0026 #include <cassert>
0027 #include <functional>
0028 #include <list>
0029 #include <memory>
0030 #include <utility>
0031 #include <vector>
0032 
0033 namespace llvm {
0034 namespace orc {
0035 
0036 class RTDyldObjectLinkingLayer
0037     : public RTTIExtends<RTDyldObjectLinkingLayer, ObjectLayer>,
0038       private ResourceManager {
0039 public:
0040   static char ID;
0041 
0042   /// Functor for receiving object-loaded notifications.
0043   using NotifyLoadedFunction = std::function<void(
0044       MaterializationResponsibility &R, const object::ObjectFile &Obj,
0045       const RuntimeDyld::LoadedObjectInfo &)>;
0046 
0047   /// Functor for receiving finalization notifications.
0048   using NotifyEmittedFunction = std::function<void(
0049       MaterializationResponsibility &R, std::unique_ptr<MemoryBuffer>)>;
0050 
0051   using GetMemoryManagerFunction =
0052       unique_function<std::unique_ptr<RuntimeDyld::MemoryManager>()>;
0053 
0054   /// Construct an ObjectLinkingLayer with the given NotifyLoaded,
0055   ///        and NotifyEmitted functors.
0056   RTDyldObjectLinkingLayer(ExecutionSession &ES,
0057                            GetMemoryManagerFunction GetMemoryManager);
0058 
0059   ~RTDyldObjectLinkingLayer();
0060 
0061   /// Emit the object.
0062   void emit(std::unique_ptr<MaterializationResponsibility> R,
0063             std::unique_ptr<MemoryBuffer> O) override;
0064 
0065   /// Set the NotifyLoaded callback.
0066   RTDyldObjectLinkingLayer &setNotifyLoaded(NotifyLoadedFunction NotifyLoaded) {
0067     this->NotifyLoaded = std::move(NotifyLoaded);
0068     return *this;
0069   }
0070 
0071   /// Set the NotifyEmitted callback.
0072   RTDyldObjectLinkingLayer &
0073   setNotifyEmitted(NotifyEmittedFunction NotifyEmitted) {
0074     this->NotifyEmitted = std::move(NotifyEmitted);
0075     return *this;
0076   }
0077 
0078   /// Set the 'ProcessAllSections' flag.
0079   ///
0080   /// If set to true, all sections in each object file will be allocated using
0081   /// the memory manager, rather than just the sections required for execution.
0082   ///
0083   /// This is kludgy, and may be removed in the future.
0084   RTDyldObjectLinkingLayer &setProcessAllSections(bool ProcessAllSections) {
0085     this->ProcessAllSections = ProcessAllSections;
0086     return *this;
0087   }
0088 
0089   /// Instructs this RTDyldLinkingLayer2 instance to override the symbol flags
0090   /// returned by RuntimeDyld for any given object file with the flags supplied
0091   /// by the MaterializationResponsibility instance. This is a workaround to
0092   /// support symbol visibility in COFF, which does not use the libObject's
0093   /// SF_Exported flag. Use only when generating / adding COFF object files.
0094   ///
0095   /// FIXME: We should be able to remove this if/when COFF properly tracks
0096   /// exported symbols.
0097   RTDyldObjectLinkingLayer &
0098   setOverrideObjectFlagsWithResponsibilityFlags(bool OverrideObjectFlags) {
0099     this->OverrideObjectFlags = OverrideObjectFlags;
0100     return *this;
0101   }
0102 
0103   /// If set, this RTDyldObjectLinkingLayer instance will claim responsibility
0104   /// for any symbols provided by a given object file that were not already in
0105   /// the MaterializationResponsibility instance. Setting this flag allows
0106   /// higher-level program representations (e.g. LLVM IR) to be added based on
0107   /// only a subset of the symbols they provide, without having to write
0108   /// intervening layers to scan and add the additional symbols. This trades
0109   /// diagnostic quality for convenience however: If all symbols are enumerated
0110   /// up-front then clashes can be detected and reported early (and usually
0111   /// deterministically). If this option is set, clashes for the additional
0112   /// symbols may not be detected until late, and detection may depend on
0113   /// the flow of control through JIT'd code. Use with care.
0114   RTDyldObjectLinkingLayer &
0115   setAutoClaimResponsibilityForObjectSymbols(bool AutoClaimObjectSymbols) {
0116     this->AutoClaimObjectSymbols = AutoClaimObjectSymbols;
0117     return *this;
0118   }
0119 
0120   /// Register a JITEventListener.
0121   void registerJITEventListener(JITEventListener &L);
0122 
0123   /// Unregister a JITEventListener.
0124   void unregisterJITEventListener(JITEventListener &L);
0125 
0126 private:
0127   using MemoryManagerUP = std::unique_ptr<RuntimeDyld::MemoryManager>;
0128 
0129   Error onObjLoad(MaterializationResponsibility &R,
0130                   const object::ObjectFile &Obj,
0131                   RuntimeDyld::MemoryManager &MemMgr,
0132                   RuntimeDyld::LoadedObjectInfo &LoadedObjInfo,
0133                   std::map<StringRef, JITEvaluatedSymbol> Resolved,
0134                   std::set<StringRef> &InternalSymbols);
0135 
0136   void onObjEmit(MaterializationResponsibility &R,
0137                  object::OwningBinary<object::ObjectFile> O,
0138                  std::unique_ptr<RuntimeDyld::MemoryManager> MemMgr,
0139                  std::unique_ptr<RuntimeDyld::LoadedObjectInfo> LoadedObjInfo,
0140                  std::unique_ptr<SymbolDependenceMap> Deps, Error Err);
0141 
0142   Error handleRemoveResources(JITDylib &JD, ResourceKey K) override;
0143   void handleTransferResources(JITDylib &JD, ResourceKey DstKey,
0144                                ResourceKey SrcKey) override;
0145 
0146   mutable std::mutex RTDyldLayerMutex;
0147   GetMemoryManagerFunction GetMemoryManager;
0148   NotifyLoadedFunction NotifyLoaded;
0149   NotifyEmittedFunction NotifyEmitted;
0150   bool ProcessAllSections = false;
0151   bool OverrideObjectFlags = false;
0152   bool AutoClaimObjectSymbols = false;
0153   DenseMap<ResourceKey, std::vector<MemoryManagerUP>> MemMgrs;
0154   std::vector<JITEventListener *> EventListeners;
0155 };
0156 
0157 } // end namespace orc
0158 } // end namespace llvm
0159 
0160 #endif // LLVM_EXECUTIONENGINE_ORC_RTDYLDOBJECTLINKINGLAYER_H