LLVM  9.0.0svn
RuntimeDyld.h
Go to the documentation of this file.
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/STLExtras.h"
17 #include "llvm/ADT/StringRef.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 
31 namespace llvm {
32 
33 namespace object {
34 
35 template <typename T> class OwningBinary;
36 
37 } // end namespace object
38 
39 /// Base class for errors originating in RuntimeDyld, e.g. missing relocation
40 /// support.
41 class RuntimeDyldError : public ErrorInfo<RuntimeDyldError> {
42 public:
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 
51 private:
52  std::string ErrMsg;
53 };
54 
56 class RuntimeDyldImpl;
57 
58 class RuntimeDyld {
59  friend class RuntimeDyldCheckerImpl;
60 
61 protected:
62  // Change the address associated with a section when resolving relocations.
63  // Any relocations already associated with the symbol will be re-resolved.
64  void reassignSectionAddress(unsigned SectionID, uint64_t Addr);
65 
66 public:
67  /// Information about the loaded object.
69  friend class RuntimeDyldImpl;
70 
71  public:
72  using ObjSectionToIDMap = std::map<object::SectionRef, unsigned>;
73 
75  : RTDyld(RTDyld), ObjSecToIDMap(std::move(ObjSecToIDMap)) {}
76 
78  getObjectForDebug(const object::ObjectFile &Obj) const = 0;
79 
80  uint64_t
81  getSectionLoadAddress(const object::SectionRef &Sec) const override;
82 
83  protected:
84  virtual void anchor();
85 
88  };
89 
90  /// Memory Management.
91  class MemoryManager {
92  friend class RuntimeDyld;
93 
94  public:
95  MemoryManager() = default;
96  virtual ~MemoryManager() = default;
97 
98  /// Allocate a memory block of (at least) the given size suitable for
99  /// executable code. The SectionID is a unique identifier assigned by the
100  /// RuntimeDyld instance, and optionally recorded by the memory manager to
101  /// access a loaded section.
102  virtual uint8_t *allocateCodeSection(uintptr_t Size, unsigned Alignment,
103  unsigned SectionID,
104  StringRef SectionName) = 0;
105 
106  /// Allocate a memory block of (at least) the given size suitable for data.
107  /// The SectionID is a unique identifier assigned by the JIT engine, and
108  /// optionally recorded by the memory manager to access a loaded section.
109  virtual uint8_t *allocateDataSection(uintptr_t Size, unsigned Alignment,
110  unsigned SectionID,
112  bool IsReadOnly) = 0;
113 
114  /// Inform the memory manager about the total amount of memory required to
115  /// allocate all sections to be loaded:
116  /// \p CodeSize - the total size of all code sections
117  /// \p DataSizeRO - the total size of all read-only data sections
118  /// \p DataSizeRW - the total size of all read-write data sections
119  ///
120  /// Note that by default the callback is disabled. To enable it
121  /// redefine the method needsToReserveAllocationSpace to return true.
122  virtual void reserveAllocationSpace(uintptr_t CodeSize, uint32_t CodeAlign,
123  uintptr_t RODataSize,
124  uint32_t RODataAlign,
125  uintptr_t RWDataSize,
126  uint32_t RWDataAlign) {}
127 
128  /// Override to return true to enable the reserveAllocationSpace callback.
129  virtual bool needsToReserveAllocationSpace() { return false; }
130 
131  /// Register the EH frames with the runtime so that c++ exceptions work.
132  ///
133  /// \p Addr parameter provides the local address of the EH frame section
134  /// data, while \p LoadAddr provides the address of the data in the target
135  /// address space. If the section has not been remapped (which will usually
136  /// be the case for local execution) these two values will be the same.
137  virtual void registerEHFrames(uint8_t *Addr, uint64_t LoadAddr,
138  size_t Size) = 0;
139  virtual void deregisterEHFrames() = 0;
140 
141  /// This method is called when object loading is complete and section page
142  /// permissions can be applied. It is up to the memory manager implementation
143  /// to decide whether or not to act on this method. The memory manager will
144  /// typically allocate all sections as read-write and then apply specific
145  /// permissions when this method is called. Code sections cannot be executed
146  /// until this function has been called. In addition, any cache coherency
147  /// operations needed to reliably use the memory are also performed.
148  ///
149  /// Returns true if an error occurred, false otherwise.
150  virtual bool finalizeMemory(std::string *ErrMsg = nullptr) = 0;
151 
152  /// This method is called after an object has been loaded into memory but
153  /// before relocations are applied to the loaded sections.
154  ///
155  /// Memory managers which are preparing code for execution in an external
156  /// address space can use this call to remap the section addresses for the
157  /// newly loaded object.
158  ///
159  /// For clients that do not need access to an ExecutionEngine instance this
160  /// method should be preferred to its cousin
161  /// MCJITMemoryManager::notifyObjectLoaded as this method is compatible with
162  /// ORC JIT stacks.
163  virtual void notifyObjectLoaded(RuntimeDyld &RTDyld,
164  const object::ObjectFile &Obj) {}
165 
166  private:
167  virtual void anchor();
168 
169  bool FinalizationLocked = false;
170  };
171 
172  /// Construct a RuntimeDyld instance.
174  RuntimeDyld(const RuntimeDyld &) = delete;
175  RuntimeDyld &operator=(const RuntimeDyld &) = delete;
176  ~RuntimeDyld();
177 
178  /// Add the referenced object file to the list of objects to be loaded and
179  /// relocated.
180  std::unique_ptr<LoadedObjectInfo> loadObject(const object::ObjectFile &O);
181 
182  /// Get the address of our local copy of the symbol. This may or may not
183  /// be the address used for relocation (clients can copy the data around
184  /// and resolve relocatons based on where they put it).
185  void *getSymbolLocalAddress(StringRef Name) const;
186 
187  /// Get the target address and flags for the named symbol.
188  /// This address is the one used for relocation.
190 
191  /// Returns a copy of the symbol table. This can be used by on-finalized
192  /// callbacks to extract the symbol table before throwing away the
193  /// RuntimeDyld instance. Because the map keys (StringRefs) are backed by
194  /// strings inside the RuntimeDyld instance, the map should be processed
195  /// before the RuntimeDyld instance is discarded.
196  std::map<StringRef, JITEvaluatedSymbol> getSymbolTable() const;
197 
198  /// Resolve the relocations for all symbols we currently know about.
199  void resolveRelocations();
200 
201  /// Map a section to its target address space value.
202  /// Map the address of a JIT section as returned from the memory manager
203  /// to the address in the target process as the running code will see it.
204  /// This is the address which will be used for relocation resolution.
205  void mapSectionAddress(const void *LocalAddress, uint64_t TargetAddress);
206 
207  /// Register any EH frame sections that have been loaded but not previously
208  /// registered with the memory manager. Note, RuntimeDyld is responsible
209  /// for identifying the EH frame and calling the memory manager with the
210  /// EH frame section data. However, the memory manager itself will handle
211  /// the actual target-specific EH frame registration.
212  void registerEHFrames();
213 
214  void deregisterEHFrames();
215 
216  bool hasError();
217  StringRef getErrorString();
218 
219  /// By default, only sections that are "required for execution" are passed to
220  /// the RTDyldMemoryManager, and other sections are discarded. Passing 'true'
221  /// to this method will cause RuntimeDyld to pass all sections to its
222  /// memory manager regardless of whether they are "required to execute" in the
223  /// usual sense. This is useful for inspecting metadata sections that may not
224  /// contain relocations, E.g. Debug info, stackmaps.
225  ///
226  /// Must be called before the first object file is loaded.
227  void setProcessAllSections(bool ProcessAllSections) {
228  assert(!Dyld && "setProcessAllSections must be called before loadObject.");
229  this->ProcessAllSections = ProcessAllSections;
230  }
231 
232  /// Perform all actions needed to make the code owned by this RuntimeDyld
233  /// instance executable:
234  ///
235  /// 1) Apply relocations.
236  /// 2) Register EH frames.
237  /// 3) Update memory permissions*.
238  ///
239  /// * Finalization is potentially recursive**, and the 3rd step will only be
240  /// applied by the outermost call to finalize. This allows different
241  /// RuntimeDyld instances to share a memory manager without the innermost
242  /// finalization locking the memory and causing relocation fixup errors in
243  /// outer instances.
244  ///
245  /// ** Recursive finalization occurs when one RuntimeDyld instances needs the
246  /// address of a symbol owned by some other instance in order to apply
247  /// relocations.
248  ///
249  void finalizeWithMemoryManagerLocking();
250 
251 private:
252  friend void
254  std::unique_ptr<MemoryBuffer> UnderlyingBuffer,
256  bool ProcessAllSections,
257  std::function<Error(std::unique_ptr<LoadedObjectInfo>,
258  std::map<StringRef, JITEvaluatedSymbol>)>
259  OnLoaded,
260  std::function<void(Error)> OnEmitted);
261 
262  // RuntimeDyldImpl is the actual class. RuntimeDyld is just the public
263  // interface.
264  std::unique_ptr<RuntimeDyldImpl> Dyld;
265  MemoryManager &MemMgr;
266  JITSymbolResolver &Resolver;
267  bool ProcessAllSections;
268  RuntimeDyldCheckerImpl *Checker;
269 };
270 
271 // Asynchronous JIT link for ORC.
272 //
273 // Warning: This API is experimental and probably should not be used by anyone
274 // but ORC's RTDyldObjectLinkingLayer2. Internally it constructs a RuntimeDyld
275 // instance and uses continuation passing to perform the fix-up and finalize
276 // steps asynchronously.
278  std::unique_ptr<MemoryBuffer> UnderlyingBuffer,
280  JITSymbolResolver &Resolver, bool ProcessAllSections,
281  std::function<Error(std::unique_ptr<LoadedObjectInfo>,
282  std::map<StringRef, JITEvaluatedSymbol>)>
283  OnLoaded,
284  std::function<void(Error)> OnEmitted);
285 
286 } // end namespace llvm
287 
288 #endif // LLVM_EXECUTIONENGINE_RUNTIMEDYLD_H
virtual void reserveAllocationSpace(uintptr_t CodeSize, uint32_t CodeAlign, uintptr_t RODataSize, uint32_t RODataAlign, uintptr_t RWDataSize, uint32_t RWDataAlign)
Inform the memory manager about the total amount of memory required to allocate all sections to be lo...
Definition: RuntimeDyld.h:122
Information about the loaded object.
Definition: RuntimeDyld.h:68
This class represents lattice values for constants.
Definition: AllocatorList.h:23
amdgpu Simplify well known AMD library false FunctionCallee Value const Twine & Name
This class is the base class for all object file types.
Definition: ObjectFile.h:201
void jitLinkForORC(object::ObjectFile &Obj, std::unique_ptr< MemoryBuffer > UnderlyingBuffer, RuntimeDyld::MemoryManager &MemMgr, JITSymbolResolver &Resolver, bool ProcessAllSections, std::function< Error(std::unique_ptr< LoadedObjectInfo >, std::map< StringRef, JITEvaluatedSymbol >)> OnLoaded, std::function< void(Error)> OnEmitted)
Definition: BitVector.h:937
virtual bool needsToReserveAllocationSpace()
Override to return true to enable the reserveAllocationSpace callback.
Definition: RuntimeDyld.h:129
RuntimeDyldError(std::string ErrMsg)
Definition: RuntimeDyld.h:45
Expected< const typename ELFT::Sym * > getSymbol(typename ELFT::SymRange Symbols, uint32_t Index)
Definition: ELF.h:336
void setProcessAllSections(bool ProcessAllSections)
By default, only sections that are "required for execution" are passed to the RTDyldMemoryManager, and other sections are discarded.
Definition: RuntimeDyld.h:227
virtual void notifyObjectLoaded(RuntimeDyld &RTDyld, const object::ObjectFile &Obj)
This method is called after an object has been loaded into memory but before relocations are applied ...
Definition: RuntimeDyld.h:163
Interface for looking up the initializer for a variable name, used by Init::resolveReferences.
Definition: Record.h:1851
Symbol resolution interface.
Definition: JITSymbol.h:343
An inferface for inquiring the load address of a loaded object file to be used by the DIContext imple...
Definition: DIContext.h:220
Represents a symbol that has been evaluated to an address already.
Definition: JITSymbol.h:208
Base class for user error types.
Definition: Error.h:344
Base class for errors originating in RuntimeDyld, e.g.
Definition: RuntimeDyld.h:41
uint32_t Size
Definition: Profile.cpp:46
const std::string & getErrorMessage() const
Definition: RuntimeDyld.h:48
std::map< object::SectionRef, unsigned > ObjSectionToIDMap
Definition: RuntimeDyld.h:72
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
Lightweight error class with error context and mandatory checking.
Definition: Error.h:157
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:45
print Print MemDeps of function
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:48
LoadedObjectInfo(RuntimeDyldImpl &RTDyld, ObjSectionToIDMap ObjSecToIDMap)
Definition: RuntimeDyld.h:74
This is a value type class that represents a single section in the list of sections in the object fil...
Definition: ObjectFile.h:80