1 //===-- RuntimeDyld.h - Run-time dynamic linker for MC-JIT ------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Interface for the runtime dynamic linker facilities of the MC-JIT.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_EXECUTIONENGINE_RUNTIMEDYLD_H
15 #define LLVM_EXECUTIONENGINE_RUNTIMEDYLD_H
17 #include "JITSymbolFlags.h"
18 #include "llvm/ADT/StringRef.h"
19 #include "llvm/Support/Memory.h"
20 #include "llvm/DebugInfo/DIContext.h"
27 template <typename T> class OwningBinary;
30 class RuntimeDyldImpl;
31 class RuntimeDyldCheckerImpl;
34 friend class RuntimeDyldCheckerImpl;
36 RuntimeDyld(const RuntimeDyld &) = delete;
37 void operator=(const RuntimeDyld &) = delete;
40 // Change the address associated with a section when resolving relocations.
41 // Any relocations already associated with the symbol will be re-resolved.
42 void reassignSectionAddress(unsigned SectionID, uint64_t Addr);
45 /// \brief Information about a named symbol.
46 class SymbolInfo : public JITSymbolBase {
48 SymbolInfo(std::nullptr_t) : JITSymbolBase(JITSymbolFlags::None), Address(0) {}
49 SymbolInfo(uint64_t Address, JITSymbolFlags Flags)
50 : JITSymbolBase(Flags), Address(Address) {}
51 explicit operator bool() const { return Address != 0; }
52 uint64_t getAddress() const { return Address; }
57 /// \brief Information about the loaded object.
58 class LoadedObjectInfo : public llvm::LoadedObjectInfo {
59 friend class RuntimeDyldImpl;
61 LoadedObjectInfo(RuntimeDyldImpl &RTDyld, unsigned BeginIdx,
63 : RTDyld(RTDyld), BeginIdx(BeginIdx), EndIdx(EndIdx) { }
65 virtual ~LoadedObjectInfo() {}
67 virtual object::OwningBinary<object::ObjectFile>
68 getObjectForDebug(const object::ObjectFile &Obj) const = 0;
70 uint64_t getSectionLoadAddress(StringRef Name) const;
73 virtual void anchor();
75 RuntimeDyldImpl &RTDyld;
76 unsigned BeginIdx, EndIdx;
79 /// \brief Memory Management.
82 virtual ~MemoryManager() {};
84 /// Allocate a memory block of (at least) the given size suitable for
85 /// executable code. The SectionID is a unique identifier assigned by the
86 /// RuntimeDyld instance, and optionally recorded by the memory manager to
87 /// access a loaded section.
88 virtual uint8_t *allocateCodeSection(uintptr_t Size, unsigned Alignment,
90 StringRef SectionName) = 0;
92 /// Allocate a memory block of (at least) the given size suitable for data.
93 /// The SectionID is a unique identifier assigned by the JIT engine, and
94 /// optionally recorded by the memory manager to access a loaded section.
95 virtual uint8_t *allocateDataSection(uintptr_t Size, unsigned Alignment,
97 StringRef SectionName,
100 /// Inform the memory manager about the total amount of memory required to
101 /// allocate all sections to be loaded:
102 /// \p CodeSize - the total size of all code sections
103 /// \p DataSizeRO - the total size of all read-only data sections
104 /// \p DataSizeRW - the total size of all read-write data sections
106 /// Note that by default the callback is disabled. To enable it
107 /// redefine the method needsToReserveAllocationSpace to return true.
108 virtual void reserveAllocationSpace(uintptr_t CodeSize,
109 uintptr_t DataSizeRO,
110 uintptr_t DataSizeRW) {}
112 /// Override to return true to enable the reserveAllocationSpace callback.
113 virtual bool needsToReserveAllocationSpace() { return false; }
115 /// Register the EH frames with the runtime so that c++ exceptions work.
117 /// \p Addr parameter provides the local address of the EH frame section
118 /// data, while \p LoadAddr provides the address of the data in the target
119 /// address space. If the section has not been remapped (which will usually
120 /// be the case for local execution) these two values will be the same.
121 virtual void registerEHFrames(uint8_t *Addr, uint64_t LoadAddr,
123 virtual void deregisterEHFrames(uint8_t *addr, uint64_t LoadAddr,
126 /// This method is called when object loading is complete and section page
127 /// permissions can be applied. It is up to the memory manager implementation
128 /// to decide whether or not to act on this method. The memory manager will
129 /// typically allocate all sections as read-write and then apply specific
130 /// permissions when this method is called. Code sections cannot be executed
131 /// until this function has been called. In addition, any cache coherency
132 /// operations needed to reliably use the memory are also performed.
134 /// Returns true if an error occurred, false otherwise.
135 virtual bool finalizeMemory(std::string *ErrMsg = nullptr) = 0;
138 virtual void anchor();
141 /// \brief Symbol resolution.
142 class SymbolResolver {
144 virtual ~SymbolResolver() {};
146 /// This method returns the address of the specified function or variable.
147 /// It is used to resolve symbols during module linking.
148 virtual SymbolInfo findSymbol(const std::string &Name) = 0;
150 /// This method returns the address of the specified symbol if it exists
151 /// within the logical dynamic library represented by this
152 /// RTDyldMemoryManager. Unlike getSymbolAddress, queries through this
153 /// interface should return addresses for hidden symbols.
155 /// This is of particular importance for the Orc JIT APIs, which support lazy
156 /// compilation by breaking up modules: Each of those broken out modules
157 /// must be able to resolve hidden symbols provided by the others. Clients
158 /// writing memory managers for MCJIT can usually ignore this method.
160 /// This method will be queried by RuntimeDyld when checking for previous
161 /// definitions of common symbols. It will *not* be queried by default when
162 /// resolving external symbols (this minimises the link-time overhead for
163 /// MCJIT clients who don't care about Orc features). If you are writing a
164 /// RTDyldMemoryManager for Orc and want "external" symbol resolution to
165 /// search the logical dylib, you should override your getSymbolAddress
166 /// method call this method directly.
167 virtual SymbolInfo findSymbolInLogicalDylib(const std::string &Name) = 0;
169 virtual void anchor();
172 /// \brief Construct a RuntimeDyld instance.
173 RuntimeDyld(MemoryManager &MemMgr, SymbolResolver &Resolver);
176 /// Add the referenced object file to the list of objects to be loaded and
178 std::unique_ptr<LoadedObjectInfo> loadObject(const object::ObjectFile &O);
180 /// Get the address of our local copy of the symbol. This may or may not
181 /// be the address used for relocation (clients can copy the data around
182 /// and resolve relocatons based on where they put it).
183 void *getSymbolLocalAddress(StringRef Name) const;
185 /// Get the target address and flags for the named symbol.
186 /// This address is the one used for relocation.
187 SymbolInfo getSymbol(StringRef Name) const;
189 /// Resolve the relocations for all symbols we currently know about.
190 void resolveRelocations();
192 /// Map a section to its target address space value.
193 /// Map the address of a JIT section as returned from the memory manager
194 /// to the address in the target process as the running code will see it.
195 /// This is the address which will be used for relocation resolution.
196 void mapSectionAddress(const void *LocalAddress, uint64_t TargetAddress);
198 /// Register any EH frame sections that have been loaded but not previously
199 /// registered with the memory manager. Note, RuntimeDyld is responsible
200 /// for identifying the EH frame and calling the memory manager with the
201 /// EH frame section data. However, the memory manager itself will handle
202 /// the actual target-specific EH frame registration.
203 void registerEHFrames();
205 void deregisterEHFrames();
208 StringRef getErrorString();
210 /// By default, only sections that are "required for execution" are passed to
211 /// the RTDyldMemoryManager, and other sections are discarded. Passing 'true'
212 /// to this method will cause RuntimeDyld to pass all sections to its
213 /// memory manager regardless of whether they are "required to execute" in the
214 /// usual sense. This is useful for inspecting metadata sections that may not
215 /// contain relocations, E.g. Debug info, stackmaps.
217 /// Must be called before the first object file is loaded.
218 void setProcessAllSections(bool ProcessAllSections) {
219 assert(!Dyld && "setProcessAllSections must be called before loadObject.");
220 this->ProcessAllSections = ProcessAllSections;
224 // RuntimeDyldImpl is the actual class. RuntimeDyld is just the public
226 std::unique_ptr<RuntimeDyldImpl> Dyld;
227 MemoryManager &MemMgr;
228 SymbolResolver &Resolver;
229 bool ProcessAllSections;
230 RuntimeDyldCheckerImpl *Checker;
233 } // end namespace llvm