16e56331ed99e5b96de940dfdc53e438eef521a2eJim Grosbach//===-- RuntimeDyld.h - Run-time dynamic linker for MC-JIT ------*- C++ -*-===//
26e56331ed99e5b96de940dfdc53e438eef521a2eJim Grosbach//
36e56331ed99e5b96de940dfdc53e438eef521a2eJim Grosbach//                     The LLVM Compiler Infrastructure
46e56331ed99e5b96de940dfdc53e438eef521a2eJim Grosbach//
56e56331ed99e5b96de940dfdc53e438eef521a2eJim Grosbach// This file is distributed under the University of Illinois Open Source
66e56331ed99e5b96de940dfdc53e438eef521a2eJim Grosbach// License. See LICENSE.TXT for details.
76e56331ed99e5b96de940dfdc53e438eef521a2eJim Grosbach//
86e56331ed99e5b96de940dfdc53e438eef521a2eJim Grosbach//===----------------------------------------------------------------------===//
96e56331ed99e5b96de940dfdc53e438eef521a2eJim Grosbach//
106e56331ed99e5b96de940dfdc53e438eef521a2eJim Grosbach// Interface for the runtime dynamic linker facilities of the MC-JIT.
116e56331ed99e5b96de940dfdc53e438eef521a2eJim Grosbach//
126e56331ed99e5b96de940dfdc53e438eef521a2eJim Grosbach//===----------------------------------------------------------------------===//
136e56331ed99e5b96de940dfdc53e438eef521a2eJim Grosbach
14674be02d525d4e24bc6943ed9274958c580bcfbcJakub Staszak#ifndef LLVM_EXECUTIONENGINE_RUNTIMEDYLD_H
15674be02d525d4e24bc6943ed9274958c580bcfbcJakub Staszak#define LLVM_EXECUTIONENGINE_RUNTIMEDYLD_H
166e56331ed99e5b96de940dfdc53e438eef521a2eJim Grosbach
174c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar#include "JITSymbolFlags.h"
186948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar#include "llvm/ADT/STLExtras.h"
19de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar#include "llvm/DebugInfo/DIContext.h"
20f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar#include "llvm/Object/ObjectFile.h"
216e56331ed99e5b96de940dfdc53e438eef521a2eJim Grosbach#include "llvm/Support/Memory.h"
22f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar#include <map>
23ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines#include <memory>
24de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar#include <utility>
256e56331ed99e5b96de940dfdc53e438eef521a2eJim Grosbach
266e56331ed99e5b96de940dfdc53e438eef521a2eJim Grosbachnamespace llvm {
276e56331ed99e5b96de940dfdc53e438eef521a2eJim Grosbach
28de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainarclass StringRef;
29de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar
3036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hinesnamespace object {
3136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  class ObjectFile;
32ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  template <typename T> class OwningBinary;
3336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines}
3436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines
35de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar/// Base class for errors originating in RuntimeDyld, e.g. missing relocation
36de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar/// support.
37de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainarclass RuntimeDyldError : public ErrorInfo<RuntimeDyldError> {
38de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainarpublic:
39de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  static char ID;
40de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  RuntimeDyldError(std::string ErrMsg) : ErrMsg(std::move(ErrMsg)) {}
41de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  void log(raw_ostream &OS) const override;
42de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  const std::string &getErrorMessage() const { return ErrMsg; }
43de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  std::error_code convertToErrorCode() const override;
44de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainarprivate:
45de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  std::string ErrMsg;
46de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar};
47de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar
486e56331ed99e5b96de940dfdc53e438eef521a2eJim Grosbachclass RuntimeDyldImpl;
4937ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hinesclass RuntimeDyldCheckerImpl;
500c7f116bb6950ef819323d855415b2f2b0aad987Pirama Arumuga Nainar
516e56331ed99e5b96de940dfdc53e438eef521a2eJim Grosbachclass RuntimeDyld {
5237ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  friend class RuntimeDyldCheckerImpl;
53c6a4f5e819217e1e12c458aed8e7b122e23a3a58Stephen Hines
54ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  RuntimeDyld(const RuntimeDyld &) = delete;
55ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  void operator=(const RuntimeDyld &) = delete;
566e56331ed99e5b96de940dfdc53e438eef521a2eJim Grosbach
57020f4e861a9a32059f76377e787703c92ba55a98Jim Grosbachprotected:
58020f4e861a9a32059f76377e787703c92ba55a98Jim Grosbach  // Change the address associated with a section when resolving relocations.
59020f4e861a9a32059f76377e787703c92ba55a98Jim Grosbach  // Any relocations already associated with the symbol will be re-resolved.
60020f4e861a9a32059f76377e787703c92ba55a98Jim Grosbach  void reassignSectionAddress(unsigned SectionID, uint64_t Addr);
616e56331ed99e5b96de940dfdc53e438eef521a2eJim Grosbachpublic:
62ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines
634c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar  /// \brief Information about a named symbol.
644c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar  class SymbolInfo : public JITSymbolBase {
654c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar  public:
664c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar    SymbolInfo(std::nullptr_t) : JITSymbolBase(JITSymbolFlags::None), Address(0) {}
674c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar    SymbolInfo(uint64_t Address, JITSymbolFlags Flags)
684c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar      : JITSymbolBase(Flags), Address(Address) {}
694c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar    explicit operator bool() const { return Address != 0; }
704c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar    uint64_t getAddress() const { return Address; }
714c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar  private:
724c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar    uint64_t Address;
734c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar  };
744c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar
75ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  /// \brief Information about the loaded object.
766948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar  class LoadedObjectInfo : public llvm::LoadedObjectInfo {
77ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines    friend class RuntimeDyldImpl;
78ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  public:
79f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar    typedef std::map<object::SectionRef, unsigned> ObjSectionToIDMap;
80f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar
81f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar    LoadedObjectInfo(RuntimeDyldImpl &RTDyld, ObjSectionToIDMap ObjSecToIDMap)
82de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar        : RTDyld(RTDyld), ObjSecToIDMap(std::move(ObjSecToIDMap)) {}
83ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines
84ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines    virtual object::OwningBinary<object::ObjectFile>
85ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines    getObjectForDebug(const object::ObjectFile &Obj) const = 0;
86ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines
87f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar    uint64_t
88f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar    getSectionLoadAddress(const object::SectionRef &Sec) const override;
89ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines
90ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  protected:
91ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines    virtual void anchor();
92ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines
93ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines    RuntimeDyldImpl &RTDyld;
94f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar    ObjSectionToIDMap ObjSecToIDMap;
95ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  };
96ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines
976948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar  template <typename Derived> struct LoadedObjectInfoHelper : LoadedObjectInfo {
98f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  protected:
99f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar    LoadedObjectInfoHelper(const LoadedObjectInfoHelper &) = default;
100f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar    LoadedObjectInfoHelper() = default;
101f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar
102f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  public:
103f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar    LoadedObjectInfoHelper(RuntimeDyldImpl &RTDyld,
104f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar                           LoadedObjectInfo::ObjSectionToIDMap ObjSecToIDMap)
105f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar        : LoadedObjectInfo(RTDyld, std::move(ObjSecToIDMap)) {}
1066948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar    std::unique_ptr<llvm::LoadedObjectInfo> clone() const override {
1076948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar      return llvm::make_unique<Derived>(static_cast<const Derived &>(*this));
1086948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar    }
1096948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar  };
1106948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar
1110c7f116bb6950ef819323d855415b2f2b0aad987Pirama Arumuga Nainar  /// \brief Memory Management.
1120c7f116bb6950ef819323d855415b2f2b0aad987Pirama Arumuga Nainar  class MemoryManager {
113de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar    friend class RuntimeDyld;
1140c7f116bb6950ef819323d855415b2f2b0aad987Pirama Arumuga Nainar  public:
115de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar    MemoryManager() : FinalizationLocked(false) {}
116f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar    virtual ~MemoryManager() {}
1170c7f116bb6950ef819323d855415b2f2b0aad987Pirama Arumuga Nainar
1180c7f116bb6950ef819323d855415b2f2b0aad987Pirama Arumuga Nainar    /// Allocate a memory block of (at least) the given size suitable for
1190c7f116bb6950ef819323d855415b2f2b0aad987Pirama Arumuga Nainar    /// executable code. The SectionID is a unique identifier assigned by the
1200c7f116bb6950ef819323d855415b2f2b0aad987Pirama Arumuga Nainar    /// RuntimeDyld instance, and optionally recorded by the memory manager to
1210c7f116bb6950ef819323d855415b2f2b0aad987Pirama Arumuga Nainar    /// access a loaded section.
1220c7f116bb6950ef819323d855415b2f2b0aad987Pirama Arumuga Nainar    virtual uint8_t *allocateCodeSection(uintptr_t Size, unsigned Alignment,
1230c7f116bb6950ef819323d855415b2f2b0aad987Pirama Arumuga Nainar                                         unsigned SectionID,
1240c7f116bb6950ef819323d855415b2f2b0aad987Pirama Arumuga Nainar                                         StringRef SectionName) = 0;
1250c7f116bb6950ef819323d855415b2f2b0aad987Pirama Arumuga Nainar
1260c7f116bb6950ef819323d855415b2f2b0aad987Pirama Arumuga Nainar    /// Allocate a memory block of (at least) the given size suitable for data.
1270c7f116bb6950ef819323d855415b2f2b0aad987Pirama Arumuga Nainar    /// The SectionID is a unique identifier assigned by the JIT engine, and
1280c7f116bb6950ef819323d855415b2f2b0aad987Pirama Arumuga Nainar    /// optionally recorded by the memory manager to access a loaded section.
1290c7f116bb6950ef819323d855415b2f2b0aad987Pirama Arumuga Nainar    virtual uint8_t *allocateDataSection(uintptr_t Size, unsigned Alignment,
1300c7f116bb6950ef819323d855415b2f2b0aad987Pirama Arumuga Nainar                                         unsigned SectionID,
1310c7f116bb6950ef819323d855415b2f2b0aad987Pirama Arumuga Nainar                                         StringRef SectionName,
1320c7f116bb6950ef819323d855415b2f2b0aad987Pirama Arumuga Nainar                                         bool IsReadOnly) = 0;
1330c7f116bb6950ef819323d855415b2f2b0aad987Pirama Arumuga Nainar
1340c7f116bb6950ef819323d855415b2f2b0aad987Pirama Arumuga Nainar    /// Inform the memory manager about the total amount of memory required to
1350c7f116bb6950ef819323d855415b2f2b0aad987Pirama Arumuga Nainar    /// allocate all sections to be loaded:
1360c7f116bb6950ef819323d855415b2f2b0aad987Pirama Arumuga Nainar    /// \p CodeSize - the total size of all code sections
1370c7f116bb6950ef819323d855415b2f2b0aad987Pirama Arumuga Nainar    /// \p DataSizeRO - the total size of all read-only data sections
1380c7f116bb6950ef819323d855415b2f2b0aad987Pirama Arumuga Nainar    /// \p DataSizeRW - the total size of all read-write data sections
1390c7f116bb6950ef819323d855415b2f2b0aad987Pirama Arumuga Nainar    ///
1400c7f116bb6950ef819323d855415b2f2b0aad987Pirama Arumuga Nainar    /// Note that by default the callback is disabled. To enable it
1410c7f116bb6950ef819323d855415b2f2b0aad987Pirama Arumuga Nainar    /// redefine the method needsToReserveAllocationSpace to return true.
142de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar    virtual void reserveAllocationSpace(uintptr_t CodeSize, uint32_t CodeAlign,
143de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar                                        uintptr_t RODataSize,
144de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar                                        uint32_t RODataAlign,
145de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar                                        uintptr_t RWDataSize,
146de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar                                        uint32_t RWDataAlign) {}
1470c7f116bb6950ef819323d855415b2f2b0aad987Pirama Arumuga Nainar
1480c7f116bb6950ef819323d855415b2f2b0aad987Pirama Arumuga Nainar    /// Override to return true to enable the reserveAllocationSpace callback.
1490c7f116bb6950ef819323d855415b2f2b0aad987Pirama Arumuga Nainar    virtual bool needsToReserveAllocationSpace() { return false; }
1500c7f116bb6950ef819323d855415b2f2b0aad987Pirama Arumuga Nainar
1510c7f116bb6950ef819323d855415b2f2b0aad987Pirama Arumuga Nainar    /// Register the EH frames with the runtime so that c++ exceptions work.
1520c7f116bb6950ef819323d855415b2f2b0aad987Pirama Arumuga Nainar    ///
1530c7f116bb6950ef819323d855415b2f2b0aad987Pirama Arumuga Nainar    /// \p Addr parameter provides the local address of the EH frame section
1540c7f116bb6950ef819323d855415b2f2b0aad987Pirama Arumuga Nainar    /// data, while \p LoadAddr provides the address of the data in the target
1550c7f116bb6950ef819323d855415b2f2b0aad987Pirama Arumuga Nainar    /// address space.  If the section has not been remapped (which will usually
1560c7f116bb6950ef819323d855415b2f2b0aad987Pirama Arumuga Nainar    /// be the case for local execution) these two values will be the same.
1570c7f116bb6950ef819323d855415b2f2b0aad987Pirama Arumuga Nainar    virtual void registerEHFrames(uint8_t *Addr, uint64_t LoadAddr,
1580c7f116bb6950ef819323d855415b2f2b0aad987Pirama Arumuga Nainar                                  size_t Size) = 0;
1590c7f116bb6950ef819323d855415b2f2b0aad987Pirama Arumuga Nainar    virtual void deregisterEHFrames(uint8_t *addr, uint64_t LoadAddr,
1600c7f116bb6950ef819323d855415b2f2b0aad987Pirama Arumuga Nainar                                    size_t Size) = 0;
1610c7f116bb6950ef819323d855415b2f2b0aad987Pirama Arumuga Nainar
1620c7f116bb6950ef819323d855415b2f2b0aad987Pirama Arumuga Nainar    /// This method is called when object loading is complete and section page
1630c7f116bb6950ef819323d855415b2f2b0aad987Pirama Arumuga Nainar    /// permissions can be applied.  It is up to the memory manager implementation
1640c7f116bb6950ef819323d855415b2f2b0aad987Pirama Arumuga Nainar    /// to decide whether or not to act on this method.  The memory manager will
1650c7f116bb6950ef819323d855415b2f2b0aad987Pirama Arumuga Nainar    /// typically allocate all sections as read-write and then apply specific
1660c7f116bb6950ef819323d855415b2f2b0aad987Pirama Arumuga Nainar    /// permissions when this method is called.  Code sections cannot be executed
1670c7f116bb6950ef819323d855415b2f2b0aad987Pirama Arumuga Nainar    /// until this function has been called.  In addition, any cache coherency
1680c7f116bb6950ef819323d855415b2f2b0aad987Pirama Arumuga Nainar    /// operations needed to reliably use the memory are also performed.
1690c7f116bb6950ef819323d855415b2f2b0aad987Pirama Arumuga Nainar    ///
1700c7f116bb6950ef819323d855415b2f2b0aad987Pirama Arumuga Nainar    /// Returns true if an error occurred, false otherwise.
1710c7f116bb6950ef819323d855415b2f2b0aad987Pirama Arumuga Nainar    virtual bool finalizeMemory(std::string *ErrMsg = nullptr) = 0;
1720c7f116bb6950ef819323d855415b2f2b0aad987Pirama Arumuga Nainar
173de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar    /// This method is called after an object has been loaded into memory but
174de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar    /// before relocations are applied to the loaded sections.
175de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar    ///
176de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar    /// Memory managers which are preparing code for execution in an external
177de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar    /// address space can use this call to remap the section addresses for the
178de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar    /// newly loaded object.
179de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar    ///
180de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar    /// For clients that do not need access to an ExecutionEngine instance this
181de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar    /// method should be preferred to its cousin
182de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar    /// MCJITMemoryManager::notifyObjectLoaded as this method is compatible with
183de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar    /// ORC JIT stacks.
184de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar    virtual void notifyObjectLoaded(RuntimeDyld &RTDyld,
185de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar                                    const object::ObjectFile &Obj) {}
186de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar
1870c7f116bb6950ef819323d855415b2f2b0aad987Pirama Arumuga Nainar  private:
1880c7f116bb6950ef819323d855415b2f2b0aad987Pirama Arumuga Nainar    virtual void anchor();
189de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar    bool FinalizationLocked;
1900c7f116bb6950ef819323d855415b2f2b0aad987Pirama Arumuga Nainar  };
1910c7f116bb6950ef819323d855415b2f2b0aad987Pirama Arumuga Nainar
1920c7f116bb6950ef819323d855415b2f2b0aad987Pirama Arumuga Nainar  /// \brief Symbol resolution.
1930c7f116bb6950ef819323d855415b2f2b0aad987Pirama Arumuga Nainar  class SymbolResolver {
1940c7f116bb6950ef819323d855415b2f2b0aad987Pirama Arumuga Nainar  public:
195f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar    virtual ~SymbolResolver() {}
1960c7f116bb6950ef819323d855415b2f2b0aad987Pirama Arumuga Nainar
1970c7f116bb6950ef819323d855415b2f2b0aad987Pirama Arumuga Nainar    /// This method returns the address of the specified symbol if it exists
1980c7f116bb6950ef819323d855415b2f2b0aad987Pirama Arumuga Nainar    /// within the logical dynamic library represented by this
199de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar    /// RTDyldMemoryManager. Unlike findSymbol, queries through this
2000c7f116bb6950ef819323d855415b2f2b0aad987Pirama Arumuga Nainar    /// interface should return addresses for hidden symbols.
2010c7f116bb6950ef819323d855415b2f2b0aad987Pirama Arumuga Nainar    ///
2020c7f116bb6950ef819323d855415b2f2b0aad987Pirama Arumuga Nainar    /// This is of particular importance for the Orc JIT APIs, which support lazy
2030c7f116bb6950ef819323d855415b2f2b0aad987Pirama Arumuga Nainar    /// compilation by breaking up modules: Each of those broken out modules
2040c7f116bb6950ef819323d855415b2f2b0aad987Pirama Arumuga Nainar    /// must be able to resolve hidden symbols provided by the others. Clients
2050c7f116bb6950ef819323d855415b2f2b0aad987Pirama Arumuga Nainar    /// writing memory managers for MCJIT can usually ignore this method.
2060c7f116bb6950ef819323d855415b2f2b0aad987Pirama Arumuga Nainar    ///
2070c7f116bb6950ef819323d855415b2f2b0aad987Pirama Arumuga Nainar    /// This method will be queried by RuntimeDyld when checking for previous
208de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar    /// definitions of common symbols.
2090c7f116bb6950ef819323d855415b2f2b0aad987Pirama Arumuga Nainar    virtual SymbolInfo findSymbolInLogicalDylib(const std::string &Name) = 0;
210de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar
211de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar    /// This method returns the address of the specified function or variable.
212de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar    /// It is used to resolve symbols during module linking.
213de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar    ///
214de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar    /// If the returned symbol's address is equal to ~0ULL then RuntimeDyld will
215de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar    /// skip all relocations for that symbol, and the client will be responsible
216de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar    /// for handling them manually.
217de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar    virtual SymbolInfo findSymbol(const std::string &Name) = 0;
218de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar
2190c7f116bb6950ef819323d855415b2f2b0aad987Pirama Arumuga Nainar  private:
2200c7f116bb6950ef819323d855415b2f2b0aad987Pirama Arumuga Nainar    virtual void anchor();
2210c7f116bb6950ef819323d855415b2f2b0aad987Pirama Arumuga Nainar  };
2220c7f116bb6950ef819323d855415b2f2b0aad987Pirama Arumuga Nainar
2230c7f116bb6950ef819323d855415b2f2b0aad987Pirama Arumuga Nainar  /// \brief Construct a RuntimeDyld instance.
2240c7f116bb6950ef819323d855415b2f2b0aad987Pirama Arumuga Nainar  RuntimeDyld(MemoryManager &MemMgr, SymbolResolver &Resolver);
2256e56331ed99e5b96de940dfdc53e438eef521a2eJim Grosbach  ~RuntimeDyld();
2266e56331ed99e5b96de940dfdc53e438eef521a2eJim Grosbach
227ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  /// Add the referenced object file to the list of objects to be loaded and
228ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  /// relocated.
229ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  std::unique_ptr<LoadedObjectInfo> loadObject(const object::ObjectFile &O);
23036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines
2315fe019835c269ccbfe185276269bc53b3f9a7a86Eli Bendersky  /// Get the address of our local copy of the symbol. This may or may not
2325fe019835c269ccbfe185276269bc53b3f9a7a86Eli Bendersky  /// be the address used for relocation (clients can copy the data around
2335fe019835c269ccbfe185276269bc53b3f9a7a86Eli Bendersky  /// and resolve relocatons based on where they put it).
2344c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar  void *getSymbolLocalAddress(StringRef Name) const;
23535ed842773da41779d57d3ed23f440202d0be198Jim Grosbach
2364c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar  /// Get the target address and flags for the named symbol.
2374c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar  /// This address is the one used for relocation.
2384c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar  SymbolInfo getSymbol(StringRef Name) const;
239ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines
2405fe019835c269ccbfe185276269bc53b3f9a7a86Eli Bendersky  /// Resolve the relocations for all symbols we currently know about.
241f8c1c8465ff097ad5b87331b6d9a2576167f1402Jim Grosbach  void resolveRelocations();
242020f4e861a9a32059f76377e787703c92ba55a98Jim Grosbach
2438213068f3401848d86027e61f74f41c54c4805c5Andrew Kaylor  /// Map a section to its target address space value.
244020f4e861a9a32059f76377e787703c92ba55a98Jim Grosbach  /// Map the address of a JIT section as returned from the memory manager
245020f4e861a9a32059f76377e787703c92ba55a98Jim Grosbach  /// to the address in the target process as the running code will see it.
246020f4e861a9a32059f76377e787703c92ba55a98Jim Grosbach  /// This is the address which will be used for relocation resolution.
247e940c1bb6c976539f07d6f440aeaacf7c25e1ddcJim Grosbach  void mapSectionAddress(const void *LocalAddress, uint64_t TargetAddress);
248020f4e861a9a32059f76377e787703c92ba55a98Jim Grosbach
249528f6d787b1a847e61eb2f1114559f423fdeb68cAndrew Kaylor  /// Register any EH frame sections that have been loaded but not previously
250528f6d787b1a847e61eb2f1114559f423fdeb68cAndrew Kaylor  /// registered with the memory manager.  Note, RuntimeDyld is responsible
251528f6d787b1a847e61eb2f1114559f423fdeb68cAndrew Kaylor  /// for identifying the EH frame and calling the memory manager with the
252528f6d787b1a847e61eb2f1114559f423fdeb68cAndrew Kaylor  /// EH frame section data.  However, the memory manager itself will handle
253528f6d787b1a847e61eb2f1114559f423fdeb68cAndrew Kaylor  /// the actual target-specific EH frame registration.
254528f6d787b1a847e61eb2f1114559f423fdeb68cAndrew Kaylor  void registerEHFrames();
255a2e40fbd624916c187a95ed76939ca7f02ed3e53Rafael Espindola
25643507d026bef31100cb0c35614bcf419029a265bAndrew Kaylor  void deregisterEHFrames();
25743507d026bef31100cb0c35614bcf419029a265bAndrew Kaylor
25836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  bool hasError();
259528f6d787b1a847e61eb2f1114559f423fdeb68cAndrew Kaylor  StringRef getErrorString();
26036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines
26136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  /// By default, only sections that are "required for execution" are passed to
26236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  /// the RTDyldMemoryManager, and other sections are discarded. Passing 'true'
26336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  /// to this method will cause RuntimeDyld to pass all sections to its
26436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  /// memory manager regardless of whether they are "required to execute" in the
26536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  /// usual sense. This is useful for inspecting metadata sections that may not
26636b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  /// contain relocations, E.g. Debug info, stackmaps.
26736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  ///
26836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  /// Must be called before the first object file is loaded.
26936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  void setProcessAllSections(bool ProcessAllSections) {
27036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    assert(!Dyld && "setProcessAllSections must be called before loadObject.");
27136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    this->ProcessAllSections = ProcessAllSections;
27236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  }
2730c7f116bb6950ef819323d855415b2f2b0aad987Pirama Arumuga Nainar
274de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  /// Perform all actions needed to make the code owned by this RuntimeDyld
275de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  /// instance executable:
276de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  ///
277de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  /// 1) Apply relocations.
278de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  /// 2) Register EH frames.
279de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  /// 3) Update memory permissions*.
280de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  ///
281de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  /// * Finalization is potentially recursive**, and the 3rd step will only be
282de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  ///   applied by the outermost call to finalize. This allows different
283de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  ///   RuntimeDyld instances to share a memory manager without the innermost
284de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  ///   finalization locking the memory and causing relocation fixup errors in
285de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  ///   outer instances.
286de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  ///
287de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  /// ** Recursive finalization occurs when one RuntimeDyld instances needs the
288de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  ///   address of a symbol owned by some other instance in order to apply
289de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  ///   relocations.
290de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  ///
291de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  void finalizeWithMemoryManagerLocking();
292de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar
2930c7f116bb6950ef819323d855415b2f2b0aad987Pirama Arumuga Nainarprivate:
2940c7f116bb6950ef819323d855415b2f2b0aad987Pirama Arumuga Nainar  // RuntimeDyldImpl is the actual class. RuntimeDyld is just the public
2950c7f116bb6950ef819323d855415b2f2b0aad987Pirama Arumuga Nainar  // interface.
2960c7f116bb6950ef819323d855415b2f2b0aad987Pirama Arumuga Nainar  std::unique_ptr<RuntimeDyldImpl> Dyld;
2970c7f116bb6950ef819323d855415b2f2b0aad987Pirama Arumuga Nainar  MemoryManager &MemMgr;
2980c7f116bb6950ef819323d855415b2f2b0aad987Pirama Arumuga Nainar  SymbolResolver &Resolver;
2990c7f116bb6950ef819323d855415b2f2b0aad987Pirama Arumuga Nainar  bool ProcessAllSections;
3000c7f116bb6950ef819323d855415b2f2b0aad987Pirama Arumuga Nainar  RuntimeDyldCheckerImpl *Checker;
3016e56331ed99e5b96de940dfdc53e438eef521a2eJim Grosbach};
3026e56331ed99e5b96de940dfdc53e438eef521a2eJim Grosbach
3036e56331ed99e5b96de940dfdc53e438eef521a2eJim Grosbach} // end namespace llvm
3046e56331ed99e5b96de940dfdc53e438eef521a2eJim Grosbach
305f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar#endif // LLVM_EXECUTIONENGINE_RUNTIMEDYLD_H
306