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 "llvm/ADT/STLExtras.h"
18 #include "llvm/ADT/StringRef.h"
19 #include "llvm/DebugInfo/DIContext.h"
20 #include "llvm/ExecutionEngine/JITSymbol.h"
21 #include "llvm/Object/ObjectFile.h"
22 #include "llvm/Support/Error.h"
30 #include <system_error>
36 template <typename T> class OwningBinary;
38 } // end namespace object
40 /// Base class for errors originating in RuntimeDyld, e.g. missing relocation
42 class RuntimeDyldError : public ErrorInfo<RuntimeDyldError> {
46 RuntimeDyldError(std::string ErrMsg) : ErrMsg(std::move(ErrMsg)) {}
48 void log(raw_ostream &OS) const override;
49 const std::string &getErrorMessage() const { return ErrMsg; }
50 std::error_code convertToErrorCode() const override;
56 class RuntimeDyldCheckerImpl;
57 class RuntimeDyldImpl;
60 friend class RuntimeDyldCheckerImpl;
63 // Change the address associated with a section when resolving relocations.
64 // Any relocations already associated with the symbol will be re-resolved.
65 void reassignSectionAddress(unsigned SectionID, uint64_t Addr);
68 /// \brief Information about the loaded object.
69 class LoadedObjectInfo : public llvm::LoadedObjectInfo {
70 friend class RuntimeDyldImpl;
73 using ObjSectionToIDMap = std::map<object::SectionRef, unsigned>;
75 LoadedObjectInfo(RuntimeDyldImpl &RTDyld, ObjSectionToIDMap ObjSecToIDMap)
76 : RTDyld(RTDyld), ObjSecToIDMap(std::move(ObjSecToIDMap)) {}
78 virtual object::OwningBinary<object::ObjectFile>
79 getObjectForDebug(const object::ObjectFile &Obj) const = 0;
82 getSectionLoadAddress(const object::SectionRef &Sec) const override;
85 virtual void anchor();
87 RuntimeDyldImpl &RTDyld;
88 ObjSectionToIDMap ObjSecToIDMap;
91 /// \brief Memory Management.
93 friend class RuntimeDyld;
96 MemoryManager() = default;
97 virtual ~MemoryManager() = default;
99 /// Allocate a memory block of (at least) the given size suitable for
100 /// executable code. The SectionID is a unique identifier assigned by the
101 /// RuntimeDyld instance, and optionally recorded by the memory manager to
102 /// access a loaded section.
103 virtual uint8_t *allocateCodeSection(uintptr_t Size, unsigned Alignment,
105 StringRef SectionName) = 0;
107 /// Allocate a memory block of (at least) the given size suitable for data.
108 /// The SectionID is a unique identifier assigned by the JIT engine, and
109 /// optionally recorded by the memory manager to access a loaded section.
110 virtual uint8_t *allocateDataSection(uintptr_t Size, unsigned Alignment,
112 StringRef SectionName,
113 bool IsReadOnly) = 0;
115 /// Inform the memory manager about the total amount of memory required to
116 /// allocate all sections to be loaded:
117 /// \p CodeSize - the total size of all code sections
118 /// \p DataSizeRO - the total size of all read-only data sections
119 /// \p DataSizeRW - the total size of all read-write data sections
121 /// Note that by default the callback is disabled. To enable it
122 /// redefine the method needsToReserveAllocationSpace to return true.
123 virtual void reserveAllocationSpace(uintptr_t CodeSize, uint32_t CodeAlign,
124 uintptr_t RODataSize,
125 uint32_t RODataAlign,
126 uintptr_t RWDataSize,
127 uint32_t RWDataAlign) {}
129 /// Override to return true to enable the reserveAllocationSpace callback.
130 virtual bool needsToReserveAllocationSpace() { return false; }
132 /// Register the EH frames with the runtime so that c++ exceptions work.
134 /// \p Addr parameter provides the local address of the EH frame section
135 /// data, while \p LoadAddr provides the address of the data in the target
136 /// address space. If the section has not been remapped (which will usually
137 /// be the case for local execution) these two values will be the same.
138 virtual void registerEHFrames(uint8_t *Addr, uint64_t LoadAddr,
140 virtual void deregisterEHFrames() = 0;
142 /// This method is called when object loading is complete and section page
143 /// permissions can be applied. It is up to the memory manager implementation
144 /// to decide whether or not to act on this method. The memory manager will
145 /// typically allocate all sections as read-write and then apply specific
146 /// permissions when this method is called. Code sections cannot be executed
147 /// until this function has been called. In addition, any cache coherency
148 /// operations needed to reliably use the memory are also performed.
150 /// Returns true if an error occurred, false otherwise.
151 virtual bool finalizeMemory(std::string *ErrMsg = nullptr) = 0;
153 /// This method is called after an object has been loaded into memory but
154 /// before relocations are applied to the loaded sections.
156 /// Memory managers which are preparing code for execution in an external
157 /// address space can use this call to remap the section addresses for the
158 /// newly loaded object.
160 /// For clients that do not need access to an ExecutionEngine instance this
161 /// method should be preferred to its cousin
162 /// MCJITMemoryManager::notifyObjectLoaded as this method is compatible with
164 virtual void notifyObjectLoaded(RuntimeDyld &RTDyld,
165 const object::ObjectFile &Obj) {}
168 virtual void anchor();
170 bool FinalizationLocked = false;
173 /// \brief Construct a RuntimeDyld instance.
174 RuntimeDyld(MemoryManager &MemMgr, JITSymbolResolver &Resolver);
175 RuntimeDyld(const RuntimeDyld &) = delete;
176 RuntimeDyld &operator=(const RuntimeDyld &) = delete;
179 /// Add the referenced object file to the list of objects to be loaded and
181 std::unique_ptr<LoadedObjectInfo> loadObject(const object::ObjectFile &O);
183 /// Get the address of our local copy of the symbol. This may or may not
184 /// be the address used for relocation (clients can copy the data around
185 /// and resolve relocatons based on where they put it).
186 void *getSymbolLocalAddress(StringRef Name) const;
188 /// Get the target address and flags for the named symbol.
189 /// This address is the one used for relocation.
190 JITEvaluatedSymbol getSymbol(StringRef Name) const;
192 /// Resolve the relocations for all symbols we currently know about.
193 void resolveRelocations();
195 /// Map a section to its target address space value.
196 /// Map the address of a JIT section as returned from the memory manager
197 /// to the address in the target process as the running code will see it.
198 /// This is the address which will be used for relocation resolution.
199 void mapSectionAddress(const void *LocalAddress, uint64_t TargetAddress);
201 /// Register any EH frame sections that have been loaded but not previously
202 /// registered with the memory manager. Note, RuntimeDyld is responsible
203 /// for identifying the EH frame and calling the memory manager with the
204 /// EH frame section data. However, the memory manager itself will handle
205 /// the actual target-specific EH frame registration.
206 void registerEHFrames();
208 void deregisterEHFrames();
211 StringRef getErrorString();
213 /// By default, only sections that are "required for execution" are passed to
214 /// the RTDyldMemoryManager, and other sections are discarded. Passing 'true'
215 /// to this method will cause RuntimeDyld to pass all sections to its
216 /// memory manager regardless of whether they are "required to execute" in the
217 /// usual sense. This is useful for inspecting metadata sections that may not
218 /// contain relocations, E.g. Debug info, stackmaps.
220 /// Must be called before the first object file is loaded.
221 void setProcessAllSections(bool ProcessAllSections) {
222 assert(!Dyld && "setProcessAllSections must be called before loadObject.");
223 this->ProcessAllSections = ProcessAllSections;
226 /// Perform all actions needed to make the code owned by this RuntimeDyld
227 /// instance executable:
229 /// 1) Apply relocations.
230 /// 2) Register EH frames.
231 /// 3) Update memory permissions*.
233 /// * Finalization is potentially recursive**, and the 3rd step will only be
234 /// applied by the outermost call to finalize. This allows different
235 /// RuntimeDyld instances to share a memory manager without the innermost
236 /// finalization locking the memory and causing relocation fixup errors in
239 /// ** Recursive finalization occurs when one RuntimeDyld instances needs the
240 /// address of a symbol owned by some other instance in order to apply
243 void finalizeWithMemoryManagerLocking();
246 // RuntimeDyldImpl is the actual class. RuntimeDyld is just the public
248 std::unique_ptr<RuntimeDyldImpl> Dyld;
249 MemoryManager &MemMgr;
250 JITSymbolResolver &Resolver;
251 bool ProcessAllSections;
252 RuntimeDyldCheckerImpl *Checker;
255 } // end namespace llvm
257 #endif // LLVM_EXECUTIONENGINE_RUNTIMEDYLD_H