1 //===- RuntimeDyld.h - Run-time dynamic linker for MC-JIT -------*- C++ -*-===//
2 //
3 // 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
6 //
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/FunctionExtras.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"
23 #include <algorithm>
24 #include <cassert>
25 #include <cstddef>
26 #include <cstdint>
27 #include <map>
28 #include <memory>
29 #include <string>
30 #include <system_error>
31 
32 namespace llvm {
33 
34 namespace object {
35 
36 template <typename T> class OwningBinary;
37 
38 } // end namespace object
39 
40 /// Base class for errors originating in RuntimeDyld, e.g. missing relocation
41 /// support.
42 class RuntimeDyldError : public ErrorInfo<RuntimeDyldError> {
43 public:
44   static char ID;
45 
46   RuntimeDyldError(std::string ErrMsg) : ErrMsg(std::move(ErrMsg)) {}
47 
48   void log(raw_ostream &OS) const override;
49   const std::string &getErrorMessage() const { return ErrMsg; }
50   std::error_code convertToErrorCode() const override;
51 
52 private:
53   std::string ErrMsg;
54 };
55 
56 class RuntimeDyldImpl;
57 
58 class RuntimeDyld {
59 public:
60   // Change the address associated with a section when resolving relocations.
61   // Any relocations already associated with the symbol will be re-resolved.
62   void reassignSectionAddress(unsigned SectionID, uint64_t Addr);
63 
64   using NotifyStubEmittedFunction = std::function<void(
65       StringRef FileName, StringRef SectionName, StringRef SymbolName,
66       unsigned SectionID, uint32_t StubOffset)>;
67 
68   /// Information about the loaded object.
69   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 
91   /// Memory Management.
92   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     /// Override to return false to tell LLVM no stub space will be needed.
133     /// This requires some guarantees depending on architecuture, but when
134     /// you know what you are doing it saves allocated space.
135     virtual bool allowStubAllocation() const { return true; }
136 
137     /// Register the EH frames with the runtime so that c++ exceptions work.
138     ///
139     /// \p Addr parameter provides the local address of the EH frame section
140     /// data, while \p LoadAddr provides the address of the data in the target
141     /// address space.  If the section has not been remapped (which will usually
142     /// be the case for local execution) these two values will be the same.
143     virtual void registerEHFrames(uint8_t *Addr, uint64_t LoadAddr,
144                                   size_t Size) = 0;
145     virtual void deregisterEHFrames() = 0;
146 
147     /// This method is called when object loading is complete and section page
148     /// permissions can be applied.  It is up to the memory manager implementation
149     /// to decide whether or not to act on this method.  The memory manager will
150     /// typically allocate all sections as read-write and then apply specific
151     /// permissions when this method is called.  Code sections cannot be executed
152     /// until this function has been called.  In addition, any cache coherency
153     /// operations needed to reliably use the memory are also performed.
154     ///
155     /// Returns true if an error occurred, false otherwise.
156     virtual bool finalizeMemory(std::string *ErrMsg = nullptr) = 0;
157 
158     /// This method is called after an object has been loaded into memory but
159     /// before relocations are applied to the loaded sections.
160     ///
161     /// Memory managers which are preparing code for execution in an external
162     /// address space can use this call to remap the section addresses for the
163     /// newly loaded object.
164     ///
165     /// For clients that do not need access to an ExecutionEngine instance this
166     /// method should be preferred to its cousin
167     /// MCJITMemoryManager::notifyObjectLoaded as this method is compatible with
168     /// ORC JIT stacks.
169     virtual void notifyObjectLoaded(RuntimeDyld &RTDyld,
170                                     const object::ObjectFile &Obj) {}
171 
172   private:
173     virtual void anchor();
174 
175     bool FinalizationLocked = false;
176   };
177 
178   /// Construct a RuntimeDyld instance.
179   RuntimeDyld(MemoryManager &MemMgr, JITSymbolResolver &Resolver);
180   RuntimeDyld(const RuntimeDyld &) = delete;
181   RuntimeDyld &operator=(const RuntimeDyld &) = delete;
182   ~RuntimeDyld();
183 
184   /// Add the referenced object file to the list of objects to be loaded and
185   /// relocated.
186   std::unique_ptr<LoadedObjectInfo> loadObject(const object::ObjectFile &O);
187 
188   /// Get the address of our local copy of the symbol. This may or may not
189   /// be the address used for relocation (clients can copy the data around
190   /// and resolve relocatons based on where they put it).
191   void *getSymbolLocalAddress(StringRef Name) const;
192 
193   /// Get the section ID for the section containing the given symbol.
194   unsigned getSymbolSectionID(StringRef Name) const;
195 
196   /// Get the target address and flags for the named symbol.
197   /// This address is the one used for relocation.
198   JITEvaluatedSymbol getSymbol(StringRef Name) const;
199 
200   /// Returns a copy of the symbol table. This can be used by on-finalized
201   /// callbacks to extract the symbol table before throwing away the
202   /// RuntimeDyld instance. Because the map keys (StringRefs) are backed by
203   /// strings inside the RuntimeDyld instance, the map should be processed
204   /// before the RuntimeDyld instance is discarded.
205   std::map<StringRef, JITEvaluatedSymbol> getSymbolTable() const;
206 
207   /// Resolve the relocations for all symbols we currently know about.
208   void resolveRelocations();
209 
210   /// Map a section to its target address space value.
211   /// Map the address of a JIT section as returned from the memory manager
212   /// to the address in the target process as the running code will see it.
213   /// This is the address which will be used for relocation resolution.
214   void mapSectionAddress(const void *LocalAddress, uint64_t TargetAddress);
215 
216   /// Returns the section's working memory.
217   StringRef getSectionContent(unsigned SectionID) const;
218 
219   /// If the section was loaded, return the section's load address,
220   /// otherwise return None.
221   uint64_t getSectionLoadAddress(unsigned SectionID) const;
222 
223   /// Set the NotifyStubEmitted callback. This is used for debugging
224   /// purposes. A callback is made for each stub that is generated.
225   void setNotifyStubEmitted(NotifyStubEmittedFunction NotifyStubEmitted) {
226     this->NotifyStubEmitted = std::move(NotifyStubEmitted);
227   }
228 
229   /// Register any EH frame sections that have been loaded but not previously
230   /// registered with the memory manager.  Note, RuntimeDyld is responsible
231   /// for identifying the EH frame and calling the memory manager with the
232   /// EH frame section data.  However, the memory manager itself will handle
233   /// the actual target-specific EH frame registration.
234   void registerEHFrames();
235 
236   void deregisterEHFrames();
237 
238   bool hasError();
239   StringRef getErrorString();
240 
241   /// By default, only sections that are "required for execution" are passed to
242   /// the RTDyldMemoryManager, and other sections are discarded. Passing 'true'
243   /// to this method will cause RuntimeDyld to pass all sections to its
244   /// memory manager regardless of whether they are "required to execute" in the
245   /// usual sense. This is useful for inspecting metadata sections that may not
246   /// contain relocations, E.g. Debug info, stackmaps.
247   ///
248   /// Must be called before the first object file is loaded.
249   void setProcessAllSections(bool ProcessAllSections) {
250     assert(!Dyld && "setProcessAllSections must be called before loadObject.");
251     this->ProcessAllSections = ProcessAllSections;
252   }
253 
254   /// Perform all actions needed to make the code owned by this RuntimeDyld
255   /// instance executable:
256   ///
257   /// 1) Apply relocations.
258   /// 2) Register EH frames.
259   /// 3) Update memory permissions*.
260   ///
261   /// * Finalization is potentially recursive**, and the 3rd step will only be
262   ///   applied by the outermost call to finalize. This allows different
263   ///   RuntimeDyld instances to share a memory manager without the innermost
264   ///   finalization locking the memory and causing relocation fixup errors in
265   ///   outer instances.
266   ///
267   /// ** Recursive finalization occurs when one RuntimeDyld instances needs the
268   ///   address of a symbol owned by some other instance in order to apply
269   ///   relocations.
270   ///
271   void finalizeWithMemoryManagerLocking();
272 
273 private:
274   friend void jitLinkForORC(
275       object::OwningBinary<object::ObjectFile> O,
276       RuntimeDyld::MemoryManager &MemMgr, JITSymbolResolver &Resolver,
277       bool ProcessAllSections,
278       unique_function<Error(const object::ObjectFile &Obj, LoadedObjectInfo &,
279                             std::map<StringRef, JITEvaluatedSymbol>)>
280           OnLoaded,
281       unique_function<void(object::OwningBinary<object::ObjectFile> O,
282                            std::unique_ptr<LoadedObjectInfo>, Error)>
283           OnEmitted);
284 
285   // RuntimeDyldImpl is the actual class. RuntimeDyld is just the public
286   // interface.
287   std::unique_ptr<RuntimeDyldImpl> Dyld;
288   MemoryManager &MemMgr;
289   JITSymbolResolver &Resolver;
290   bool ProcessAllSections;
291   NotifyStubEmittedFunction NotifyStubEmitted;
292 };
293 
294 // Asynchronous JIT link for ORC.
295 //
296 // Warning: This API is experimental and probably should not be used by anyone
297 // but ORC's RTDyldObjectLinkingLayer2. Internally it constructs a RuntimeDyld
298 // instance and uses continuation passing to perform the fix-up and finalize
299 // steps asynchronously.
300 void jitLinkForORC(
301     object::OwningBinary<object::ObjectFile> O,
302     RuntimeDyld::MemoryManager &MemMgr, JITSymbolResolver &Resolver,
303     bool ProcessAllSections,
304     unique_function<Error(const object::ObjectFile &Obj,
305                           RuntimeDyld::LoadedObjectInfo &,
306                           std::map<StringRef, JITEvaluatedSymbol>)>
307         OnLoaded,
308     unique_function<void(object::OwningBinary<object::ObjectFile>,
309                          std::unique_ptr<RuntimeDyld::LoadedObjectInfo>, Error)>
310         OnEmitted);
311 
312 } // end namespace llvm
313 
314 #endif // LLVM_EXECUTIONENGINE_RUNTIMEDYLD_H
315