Back to home page

EIC code displayed by LXR

 
 

    


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

0001 //===- RuntimeDyld.h - Run-time dynamic linker for MC-JIT -------*- 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 // Interface for the runtime dynamic linker facilities of the MC-JIT.
0010 //
0011 //===----------------------------------------------------------------------===//
0012 
0013 #ifndef LLVM_EXECUTIONENGINE_RUNTIMEDYLD_H
0014 #define LLVM_EXECUTIONENGINE_RUNTIMEDYLD_H
0015 
0016 #include "llvm/ADT/FunctionExtras.h"
0017 #include "llvm/ADT/STLExtras.h"
0018 #include "llvm/ADT/StringRef.h"
0019 #include "llvm/DebugInfo/DIContext.h"
0020 #include "llvm/ExecutionEngine/JITSymbol.h"
0021 #include "llvm/Object/ObjectFile.h"
0022 #include "llvm/Support/Error.h"
0023 #include <algorithm>
0024 #include <cassert>
0025 #include <cstddef>
0026 #include <cstdint>
0027 #include <map>
0028 #include <memory>
0029 #include <string>
0030 #include <system_error>
0031 
0032 namespace llvm {
0033 
0034 namespace object {
0035 
0036 template <typename T> class OwningBinary;
0037 
0038 } // end namespace object
0039 
0040 /// Base class for errors originating in RuntimeDyld, e.g. missing relocation
0041 /// support.
0042 class RuntimeDyldError : public ErrorInfo<RuntimeDyldError> {
0043 public:
0044   static char ID;
0045 
0046   RuntimeDyldError(std::string ErrMsg) : ErrMsg(std::move(ErrMsg)) {}
0047 
0048   void log(raw_ostream &OS) const override;
0049   const std::string &getErrorMessage() const { return ErrMsg; }
0050   std::error_code convertToErrorCode() const override;
0051 
0052 private:
0053   std::string ErrMsg;
0054 };
0055 
0056 class RuntimeDyldImpl;
0057 
0058 class RuntimeDyld {
0059 public:
0060   // Change the address associated with a section when resolving relocations.
0061   // Any relocations already associated with the symbol will be re-resolved.
0062   void reassignSectionAddress(unsigned SectionID, uint64_t Addr);
0063 
0064   using NotifyStubEmittedFunction = std::function<void(
0065       StringRef FileName, StringRef SectionName, StringRef SymbolName,
0066       unsigned SectionID, uint32_t StubOffset)>;
0067 
0068   /// Information about the loaded object.
0069   class LoadedObjectInfo : public llvm::LoadedObjectInfo {
0070     friend class RuntimeDyldImpl;
0071 
0072   public:
0073     using ObjSectionToIDMap = std::map<object::SectionRef, unsigned>;
0074 
0075     LoadedObjectInfo(RuntimeDyldImpl &RTDyld, ObjSectionToIDMap ObjSecToIDMap)
0076         : RTDyld(RTDyld), ObjSecToIDMap(std::move(ObjSecToIDMap)) {}
0077 
0078     virtual object::OwningBinary<object::ObjectFile>
0079     getObjectForDebug(const object::ObjectFile &Obj) const = 0;
0080 
0081     uint64_t
0082     getSectionLoadAddress(const object::SectionRef &Sec) const override;
0083 
0084   protected:
0085     virtual void anchor();
0086 
0087     RuntimeDyldImpl &RTDyld;
0088     ObjSectionToIDMap ObjSecToIDMap;
0089   };
0090 
0091   /// Memory Management.
0092   class MemoryManager {
0093     friend class RuntimeDyld;
0094 
0095   public:
0096     MemoryManager() = default;
0097     virtual ~MemoryManager() = default;
0098 
0099     /// Allocate a memory block of (at least) the given size suitable for
0100     /// executable code. The SectionID is a unique identifier assigned by the
0101     /// RuntimeDyld instance, and optionally recorded by the memory manager to
0102     /// access a loaded section.
0103     virtual uint8_t *allocateCodeSection(uintptr_t Size, unsigned Alignment,
0104                                          unsigned SectionID,
0105                                          StringRef SectionName) = 0;
0106 
0107     /// Allocate a memory block of (at least) the given size suitable for data.
0108     /// The SectionID is a unique identifier assigned by the JIT engine, and
0109     /// optionally recorded by the memory manager to access a loaded section.
0110     virtual uint8_t *allocateDataSection(uintptr_t Size, unsigned Alignment,
0111                                          unsigned SectionID,
0112                                          StringRef SectionName,
0113                                          bool IsReadOnly) = 0;
0114 
0115     /// An allocated TLS section
0116     struct TLSSection {
0117       /// The pointer to the initialization image
0118       uint8_t *InitializationImage;
0119       /// The TLS offset
0120       intptr_t Offset;
0121     };
0122 
0123     /// Allocate a memory block of (at least) the given size to be used for
0124     /// thread-local storage (TLS).
0125     virtual TLSSection allocateTLSSection(uintptr_t Size, unsigned Alignment,
0126                                           unsigned SectionID,
0127                                           StringRef SectionName);
0128 
0129     /// Inform the memory manager about the total amount of memory required to
0130     /// allocate all sections to be loaded:
0131     /// \p CodeSize - the total size of all code sections
0132     /// \p DataSizeRO - the total size of all read-only data sections
0133     /// \p DataSizeRW - the total size of all read-write data sections
0134     ///
0135     /// Note that by default the callback is disabled. To enable it
0136     /// redefine the method needsToReserveAllocationSpace to return true.
0137     virtual void reserveAllocationSpace(uintptr_t CodeSize, Align CodeAlign,
0138                                         uintptr_t RODataSize, Align RODataAlign,
0139                                         uintptr_t RWDataSize,
0140                                         Align RWDataAlign) {}
0141 
0142     /// Override to return true to enable the reserveAllocationSpace callback.
0143     virtual bool needsToReserveAllocationSpace() { return false; }
0144 
0145     /// Override to return false to tell LLVM no stub space will be needed.
0146     /// This requires some guarantees depending on architecuture, but when
0147     /// you know what you are doing it saves allocated space.
0148     virtual bool allowStubAllocation() const { return true; }
0149 
0150     /// Register the EH frames with the runtime so that c++ exceptions work.
0151     ///
0152     /// \p Addr parameter provides the local address of the EH frame section
0153     /// data, while \p LoadAddr provides the address of the data in the target
0154     /// address space.  If the section has not been remapped (which will usually
0155     /// be the case for local execution) these two values will be the same.
0156     virtual void registerEHFrames(uint8_t *Addr, uint64_t LoadAddr,
0157                                   size_t Size) = 0;
0158     virtual void deregisterEHFrames() = 0;
0159 
0160     /// This method is called when object loading is complete and section page
0161     /// permissions can be applied.  It is up to the memory manager implementation
0162     /// to decide whether or not to act on this method.  The memory manager will
0163     /// typically allocate all sections as read-write and then apply specific
0164     /// permissions when this method is called.  Code sections cannot be executed
0165     /// until this function has been called.  In addition, any cache coherency
0166     /// operations needed to reliably use the memory are also performed.
0167     ///
0168     /// Returns true if an error occurred, false otherwise.
0169     virtual bool finalizeMemory(std::string *ErrMsg = nullptr) = 0;
0170 
0171     /// This method is called after an object has been loaded into memory but
0172     /// before relocations are applied to the loaded sections.
0173     ///
0174     /// Memory managers which are preparing code for execution in an external
0175     /// address space can use this call to remap the section addresses for the
0176     /// newly loaded object.
0177     ///
0178     /// For clients that do not need access to an ExecutionEngine instance this
0179     /// method should be preferred to its cousin
0180     /// MCJITMemoryManager::notifyObjectLoaded as this method is compatible with
0181     /// ORC JIT stacks.
0182     virtual void notifyObjectLoaded(RuntimeDyld &RTDyld,
0183                                     const object::ObjectFile &Obj) {}
0184 
0185   private:
0186     virtual void anchor();
0187 
0188     bool FinalizationLocked = false;
0189   };
0190 
0191   /// Construct a RuntimeDyld instance.
0192   RuntimeDyld(MemoryManager &MemMgr, JITSymbolResolver &Resolver);
0193   RuntimeDyld(const RuntimeDyld &) = delete;
0194   RuntimeDyld &operator=(const RuntimeDyld &) = delete;
0195   ~RuntimeDyld();
0196 
0197   /// Add the referenced object file to the list of objects to be loaded and
0198   /// relocated.
0199   std::unique_ptr<LoadedObjectInfo> loadObject(const object::ObjectFile &O);
0200 
0201   /// Get the address of our local copy of the symbol. This may or may not
0202   /// be the address used for relocation (clients can copy the data around
0203   /// and resolve relocatons based on where they put it).
0204   void *getSymbolLocalAddress(StringRef Name) const;
0205 
0206   /// Get the section ID for the section containing the given symbol.
0207   unsigned getSymbolSectionID(StringRef Name) const;
0208 
0209   /// Get the target address and flags for the named symbol.
0210   /// This address is the one used for relocation.
0211   JITEvaluatedSymbol getSymbol(StringRef Name) const;
0212 
0213   /// Returns a copy of the symbol table. This can be used by on-finalized
0214   /// callbacks to extract the symbol table before throwing away the
0215   /// RuntimeDyld instance. Because the map keys (StringRefs) are backed by
0216   /// strings inside the RuntimeDyld instance, the map should be processed
0217   /// before the RuntimeDyld instance is discarded.
0218   std::map<StringRef, JITEvaluatedSymbol> getSymbolTable() const;
0219 
0220   /// Resolve the relocations for all symbols we currently know about.
0221   void resolveRelocations();
0222 
0223   /// Map a section to its target address space value.
0224   /// Map the address of a JIT section as returned from the memory manager
0225   /// to the address in the target process as the running code will see it.
0226   /// This is the address which will be used for relocation resolution.
0227   void mapSectionAddress(const void *LocalAddress, uint64_t TargetAddress);
0228 
0229   /// Returns the section's working memory.
0230   StringRef getSectionContent(unsigned SectionID) const;
0231 
0232   /// If the section was loaded, return the section's load address,
0233   /// otherwise return std::nullopt.
0234   uint64_t getSectionLoadAddress(unsigned SectionID) const;
0235 
0236   /// Set the NotifyStubEmitted callback. This is used for debugging
0237   /// purposes. A callback is made for each stub that is generated.
0238   void setNotifyStubEmitted(NotifyStubEmittedFunction NotifyStubEmitted) {
0239     this->NotifyStubEmitted = std::move(NotifyStubEmitted);
0240   }
0241 
0242   /// Register any EH frame sections that have been loaded but not previously
0243   /// registered with the memory manager.  Note, RuntimeDyld is responsible
0244   /// for identifying the EH frame and calling the memory manager with the
0245   /// EH frame section data.  However, the memory manager itself will handle
0246   /// the actual target-specific EH frame registration.
0247   void registerEHFrames();
0248 
0249   void deregisterEHFrames();
0250 
0251   bool hasError();
0252   StringRef getErrorString();
0253 
0254   /// By default, only sections that are "required for execution" are passed to
0255   /// the RTDyldMemoryManager, and other sections are discarded. Passing 'true'
0256   /// to this method will cause RuntimeDyld to pass all sections to its
0257   /// memory manager regardless of whether they are "required to execute" in the
0258   /// usual sense. This is useful for inspecting metadata sections that may not
0259   /// contain relocations, E.g. Debug info, stackmaps.
0260   ///
0261   /// Must be called before the first object file is loaded.
0262   void setProcessAllSections(bool ProcessAllSections) {
0263     assert(!Dyld && "setProcessAllSections must be called before loadObject.");
0264     this->ProcessAllSections = ProcessAllSections;
0265   }
0266 
0267   /// Perform all actions needed to make the code owned by this RuntimeDyld
0268   /// instance executable:
0269   ///
0270   /// 1) Apply relocations.
0271   /// 2) Register EH frames.
0272   /// 3) Update memory permissions*.
0273   ///
0274   /// * Finalization is potentially recursive**, and the 3rd step will only be
0275   ///   applied by the outermost call to finalize. This allows different
0276   ///   RuntimeDyld instances to share a memory manager without the innermost
0277   ///   finalization locking the memory and causing relocation fixup errors in
0278   ///   outer instances.
0279   ///
0280   /// ** Recursive finalization occurs when one RuntimeDyld instances needs the
0281   ///   address of a symbol owned by some other instance in order to apply
0282   ///   relocations.
0283   ///
0284   void finalizeWithMemoryManagerLocking();
0285 
0286 private:
0287   friend void jitLinkForORC(
0288       object::OwningBinary<object::ObjectFile> O,
0289       RuntimeDyld::MemoryManager &MemMgr, JITSymbolResolver &Resolver,
0290       bool ProcessAllSections,
0291       unique_function<Error(const object::ObjectFile &Obj, LoadedObjectInfo &,
0292                             std::map<StringRef, JITEvaluatedSymbol>)>
0293           OnLoaded,
0294       unique_function<void(object::OwningBinary<object::ObjectFile> O,
0295                            std::unique_ptr<LoadedObjectInfo>, Error)>
0296           OnEmitted);
0297 
0298   // RuntimeDyldImpl is the actual class. RuntimeDyld is just the public
0299   // interface.
0300   std::unique_ptr<RuntimeDyldImpl> Dyld;
0301   MemoryManager &MemMgr;
0302   JITSymbolResolver &Resolver;
0303   bool ProcessAllSections;
0304   NotifyStubEmittedFunction NotifyStubEmitted;
0305 };
0306 
0307 // Asynchronous JIT link for ORC.
0308 //
0309 // Warning: This API is experimental and probably should not be used by anyone
0310 // but ORC's RTDyldObjectLinkingLayer2. Internally it constructs a RuntimeDyld
0311 // instance and uses continuation passing to perform the fix-up and finalize
0312 // steps asynchronously.
0313 void jitLinkForORC(
0314     object::OwningBinary<object::ObjectFile> O,
0315     RuntimeDyld::MemoryManager &MemMgr, JITSymbolResolver &Resolver,
0316     bool ProcessAllSections,
0317     unique_function<Error(const object::ObjectFile &Obj,
0318                           RuntimeDyld::LoadedObjectInfo &,
0319                           std::map<StringRef, JITEvaluatedSymbol>)>
0320         OnLoaded,
0321     unique_function<void(object::OwningBinary<object::ObjectFile>,
0322                          std::unique_ptr<RuntimeDyld::LoadedObjectInfo>, Error)>
0323         OnEmitted);
0324 
0325 } // end namespace llvm
0326 
0327 #endif // LLVM_EXECUTIONENGINE_RUNTIMEDYLD_H