blob: b2b4eba47074cca43758b1c89baebcfddb6fbbae [file] [log] [blame]
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001//===- RuntimeDyld.h - Run-time dynamic linker for MC-JIT -------*- C++ -*-===//
2//
Andrew Walbran16937d02019-10-22 13:54:20 +01003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01006//
7//===----------------------------------------------------------------------===//
8//
9// Interface for the runtime dynamic linker facilities of the MC-JIT.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef LLVM_EXECUTIONENGINE_RUNTIMEDYLD_H
14#define LLVM_EXECUTIONENGINE_RUNTIMEDYLD_H
15
16#include "llvm/ADT/STLExtras.h"
17#include "llvm/ADT/StringRef.h"
18#include "llvm/DebugInfo/DIContext.h"
19#include "llvm/ExecutionEngine/JITSymbol.h"
20#include "llvm/Object/ObjectFile.h"
21#include "llvm/Support/Error.h"
22#include <algorithm>
23#include <cassert>
24#include <cstddef>
25#include <cstdint>
26#include <map>
27#include <memory>
28#include <string>
29#include <system_error>
30
31namespace llvm {
32
33namespace object {
34
35template <typename T> class OwningBinary;
36
37} // end namespace object
38
39/// Base class for errors originating in RuntimeDyld, e.g. missing relocation
40/// support.
41class RuntimeDyldError : public ErrorInfo<RuntimeDyldError> {
42public:
43 static char ID;
44
45 RuntimeDyldError(std::string ErrMsg) : ErrMsg(std::move(ErrMsg)) {}
46
47 void log(raw_ostream &OS) const override;
48 const std::string &getErrorMessage() const { return ErrMsg; }
49 std::error_code convertToErrorCode() const override;
50
51private:
52 std::string ErrMsg;
53};
54
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010055class RuntimeDyldImpl;
56
57class RuntimeDyld {
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010058protected:
59 // Change the address associated with a section when resolving relocations.
60 // Any relocations already associated with the symbol will be re-resolved.
61 void reassignSectionAddress(unsigned SectionID, uint64_t Addr);
62
63public:
Andrew Walbran3d2c1972020-04-07 12:24:26 +010064 using NotifyStubEmittedFunction = std::function<void(
65 StringRef FileName, StringRef SectionName, StringRef SymbolName,
66 unsigned SectionID, uint32_t StubOffset)>;
67
Andrew Scullcdfcccc2018-10-05 20:58:37 +010068 /// Information about the loaded object.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010069 class LoadedObjectInfo : public llvm::LoadedObjectInfo {
70 friend class RuntimeDyldImpl;
71
72 public:
73 using ObjSectionToIDMap = std::map<object::SectionRef, unsigned>;
74
75 LoadedObjectInfo(RuntimeDyldImpl &RTDyld, ObjSectionToIDMap ObjSecToIDMap)
76 : RTDyld(RTDyld), ObjSecToIDMap(std::move(ObjSecToIDMap)) {}
77
78 virtual object::OwningBinary<object::ObjectFile>
79 getObjectForDebug(const object::ObjectFile &Obj) const = 0;
80
81 uint64_t
82 getSectionLoadAddress(const object::SectionRef &Sec) const override;
83
84 protected:
85 virtual void anchor();
86
87 RuntimeDyldImpl &RTDyld;
88 ObjSectionToIDMap ObjSecToIDMap;
89 };
90
Andrew Scullcdfcccc2018-10-05 20:58:37 +010091 /// Memory Management.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010092 class MemoryManager {
93 friend class RuntimeDyld;
94
95 public:
96 MemoryManager() = default;
97 virtual ~MemoryManager() = default;
98
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,
104 unsigned SectionID,
105 StringRef SectionName) = 0;
106
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,
111 unsigned SectionID,
112 StringRef SectionName,
113 bool IsReadOnly) = 0;
114
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
120 ///
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) {}
128
129 /// Override to return true to enable the reserveAllocationSpace callback.
130 virtual bool needsToReserveAllocationSpace() { return false; }
131
132 /// Register the EH frames with the runtime so that c++ exceptions work.
133 ///
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,
139 size_t Size) = 0;
140 virtual void deregisterEHFrames() = 0;
141
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.
149 ///
150 /// Returns true if an error occurred, false otherwise.
151 virtual bool finalizeMemory(std::string *ErrMsg = nullptr) = 0;
152
153 /// This method is called after an object has been loaded into memory but
154 /// before relocations are applied to the loaded sections.
155 ///
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.
159 ///
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
163 /// ORC JIT stacks.
164 virtual void notifyObjectLoaded(RuntimeDyld &RTDyld,
165 const object::ObjectFile &Obj) {}
166
167 private:
168 virtual void anchor();
169
170 bool FinalizationLocked = false;
171 };
172
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100173 /// Construct a RuntimeDyld instance.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100174 RuntimeDyld(MemoryManager &MemMgr, JITSymbolResolver &Resolver);
175 RuntimeDyld(const RuntimeDyld &) = delete;
176 RuntimeDyld &operator=(const RuntimeDyld &) = delete;
177 ~RuntimeDyld();
178
179 /// Add the referenced object file to the list of objects to be loaded and
180 /// relocated.
181 std::unique_ptr<LoadedObjectInfo> loadObject(const object::ObjectFile &O);
182
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;
187
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100188 /// Get the section ID for the section containing the given symbol.
189 unsigned getSymbolSectionID(StringRef Name) const;
190
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100191 /// Get the target address and flags for the named symbol.
192 /// This address is the one used for relocation.
193 JITEvaluatedSymbol getSymbol(StringRef Name) const;
194
195 /// Returns a copy of the symbol table. This can be used by on-finalized
196 /// callbacks to extract the symbol table before throwing away the
197 /// RuntimeDyld instance. Because the map keys (StringRefs) are backed by
198 /// strings inside the RuntimeDyld instance, the map should be processed
199 /// before the RuntimeDyld instance is discarded.
200 std::map<StringRef, JITEvaluatedSymbol> getSymbolTable() const;
201
202 /// Resolve the relocations for all symbols we currently know about.
203 void resolveRelocations();
204
205 /// Map a section to its target address space value.
206 /// Map the address of a JIT section as returned from the memory manager
207 /// to the address in the target process as the running code will see it.
208 /// This is the address which will be used for relocation resolution.
209 void mapSectionAddress(const void *LocalAddress, uint64_t TargetAddress);
210
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100211 /// Returns the section's working memory.
212 StringRef getSectionContent(unsigned SectionID) const;
213
214 /// If the section was loaded, return the section's load address,
215 /// otherwise return None.
216 uint64_t getSectionLoadAddress(unsigned SectionID) const;
217
218 /// Set the NotifyStubEmitted callback. This is used for debugging
219 /// purposes. A callback is made for each stub that is generated.
220 void setNotifyStubEmitted(NotifyStubEmittedFunction NotifyStubEmitted) {
221 this->NotifyStubEmitted = std::move(NotifyStubEmitted);
222 }
223
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100224 /// Register any EH frame sections that have been loaded but not previously
225 /// registered with the memory manager. Note, RuntimeDyld is responsible
226 /// for identifying the EH frame and calling the memory manager with the
227 /// EH frame section data. However, the memory manager itself will handle
228 /// the actual target-specific EH frame registration.
229 void registerEHFrames();
230
231 void deregisterEHFrames();
232
233 bool hasError();
234 StringRef getErrorString();
235
236 /// By default, only sections that are "required for execution" are passed to
237 /// the RTDyldMemoryManager, and other sections are discarded. Passing 'true'
238 /// to this method will cause RuntimeDyld to pass all sections to its
239 /// memory manager regardless of whether they are "required to execute" in the
240 /// usual sense. This is useful for inspecting metadata sections that may not
241 /// contain relocations, E.g. Debug info, stackmaps.
242 ///
243 /// Must be called before the first object file is loaded.
244 void setProcessAllSections(bool ProcessAllSections) {
245 assert(!Dyld && "setProcessAllSections must be called before loadObject.");
246 this->ProcessAllSections = ProcessAllSections;
247 }
248
249 /// Perform all actions needed to make the code owned by this RuntimeDyld
250 /// instance executable:
251 ///
252 /// 1) Apply relocations.
253 /// 2) Register EH frames.
254 /// 3) Update memory permissions*.
255 ///
256 /// * Finalization is potentially recursive**, and the 3rd step will only be
257 /// applied by the outermost call to finalize. This allows different
258 /// RuntimeDyld instances to share a memory manager without the innermost
259 /// finalization locking the memory and causing relocation fixup errors in
260 /// outer instances.
261 ///
262 /// ** Recursive finalization occurs when one RuntimeDyld instances needs the
263 /// address of a symbol owned by some other instance in order to apply
264 /// relocations.
265 ///
266 void finalizeWithMemoryManagerLocking();
267
268private:
Andrew Scull0372a572018-11-16 15:47:06 +0000269 friend void
270 jitLinkForORC(object::ObjectFile &Obj,
271 std::unique_ptr<MemoryBuffer> UnderlyingBuffer,
272 RuntimeDyld::MemoryManager &MemMgr, JITSymbolResolver &Resolver,
273 bool ProcessAllSections,
274 std::function<Error(std::unique_ptr<LoadedObjectInfo>,
275 std::map<StringRef, JITEvaluatedSymbol>)>
276 OnLoaded,
277 std::function<void(Error)> OnEmitted);
278
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100279 // RuntimeDyldImpl is the actual class. RuntimeDyld is just the public
280 // interface.
281 std::unique_ptr<RuntimeDyldImpl> Dyld;
282 MemoryManager &MemMgr;
283 JITSymbolResolver &Resolver;
284 bool ProcessAllSections;
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100285 NotifyStubEmittedFunction NotifyStubEmitted;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100286};
287
Andrew Scull0372a572018-11-16 15:47:06 +0000288// Asynchronous JIT link for ORC.
289//
290// Warning: This API is experimental and probably should not be used by anyone
291// but ORC's RTDyldObjectLinkingLayer2. Internally it constructs a RuntimeDyld
292// instance and uses continuation passing to perform the fix-up and finalize
293// steps asynchronously.
294void jitLinkForORC(object::ObjectFile &Obj,
295 std::unique_ptr<MemoryBuffer> UnderlyingBuffer,
296 RuntimeDyld::MemoryManager &MemMgr,
297 JITSymbolResolver &Resolver, bool ProcessAllSections,
298 std::function<Error(std::unique_ptr<LoadedObjectInfo>,
299 std::map<StringRef, JITEvaluatedSymbol>)>
300 OnLoaded,
301 std::function<void(Error)> OnEmitted);
302
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100303} // end namespace llvm
304
305#endif // LLVM_EXECUTIONENGINE_RUNTIMEDYLD_H