fe0ccdaaa1af9c3bb1f83a967ff1f2d0bc5b3731
[oota-llvm.git] / include / llvm / ExecutionEngine / RuntimeDyld.h
1 //===-- RuntimeDyld.h - Run-time dynamic linker for MC-JIT ------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // Interface for the runtime dynamic linker facilities of the MC-JIT.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef LLVM_EXECUTIONENGINE_RUNTIMEDYLD_H
15 #define LLVM_EXECUTIONENGINE_RUNTIMEDYLD_H
16
17 #include "JITSymbolFlags.h"
18 #include "llvm/ADT/StringRef.h"
19 #include "llvm/ExecutionEngine/RTDyldMemoryManager.h"
20 #include "llvm/Support/Memory.h"
21 #include <memory>
22
23 namespace llvm {
24
25 namespace object {
26   class ObjectFile;
27   template <typename T> class OwningBinary;
28 }
29
30 class RuntimeDyldImpl;
31 class RuntimeDyldCheckerImpl;
32  
33 class RuntimeDyld {
34   friend class RuntimeDyldCheckerImpl;
35
36   RuntimeDyld(const RuntimeDyld &) = delete;
37   void operator=(const RuntimeDyld &) = delete;
38
39   // RuntimeDyldImpl is the actual class. RuntimeDyld is just the public
40   // interface.
41   std::unique_ptr<RuntimeDyldImpl> Dyld;
42   RTDyldMemoryManager *MM;
43   bool ProcessAllSections;
44   RuntimeDyldCheckerImpl *Checker;
45 protected:
46   // Change the address associated with a section when resolving relocations.
47   // Any relocations already associated with the symbol will be re-resolved.
48   void reassignSectionAddress(unsigned SectionID, uint64_t Addr);
49 public:
50
51   /// \brief Information about a named symbol.
52   class SymbolInfo : public JITSymbolBase {
53   public:
54     SymbolInfo(std::nullptr_t) : JITSymbolBase(JITSymbolFlags::None), Address(0) {}
55     SymbolInfo(uint64_t Address, JITSymbolFlags Flags)
56       : JITSymbolBase(Flags), Address(Address) {}
57     explicit operator bool() const { return Address != 0; }
58     uint64_t getAddress() const { return Address; }
59   private:
60     uint64_t Address;
61   };
62
63   /// \brief Information about the loaded object.
64   class LoadedObjectInfo {
65     friend class RuntimeDyldImpl;
66   public:
67     LoadedObjectInfo(RuntimeDyldImpl &RTDyld, unsigned BeginIdx,
68                      unsigned EndIdx)
69       : RTDyld(RTDyld), BeginIdx(BeginIdx), EndIdx(EndIdx) { }
70
71     virtual ~LoadedObjectInfo() {}
72
73     virtual object::OwningBinary<object::ObjectFile>
74     getObjectForDebug(const object::ObjectFile &Obj) const = 0;
75
76     uint64_t getSectionLoadAddress(StringRef Name) const;
77
78   protected:
79     virtual void anchor();
80
81     RuntimeDyldImpl &RTDyld;
82     unsigned BeginIdx, EndIdx;
83   };
84
85   RuntimeDyld(RTDyldMemoryManager *);
86   ~RuntimeDyld();
87
88   /// Add the referenced object file to the list of objects to be loaded and
89   /// relocated.
90   std::unique_ptr<LoadedObjectInfo> loadObject(const object::ObjectFile &O);
91
92   /// Get the address of our local copy of the symbol. This may or may not
93   /// be the address used for relocation (clients can copy the data around
94   /// and resolve relocatons based on where they put it).
95   void *getSymbolLocalAddress(StringRef Name) const;
96
97   /// Get the target address and flags for the named symbol.
98   /// This address is the one used for relocation.
99   SymbolInfo getSymbol(StringRef Name) const;
100
101   /// Resolve the relocations for all symbols we currently know about.
102   void resolveRelocations();
103
104   /// Map a section to its target address space value.
105   /// Map the address of a JIT section as returned from the memory manager
106   /// to the address in the target process as the running code will see it.
107   /// This is the address which will be used for relocation resolution.
108   void mapSectionAddress(const void *LocalAddress, uint64_t TargetAddress);
109
110   /// Register any EH frame sections that have been loaded but not previously
111   /// registered with the memory manager.  Note, RuntimeDyld is responsible
112   /// for identifying the EH frame and calling the memory manager with the
113   /// EH frame section data.  However, the memory manager itself will handle
114   /// the actual target-specific EH frame registration.
115   void registerEHFrames();
116
117   void deregisterEHFrames();
118
119   bool hasError();
120   StringRef getErrorString();
121
122   /// By default, only sections that are "required for execution" are passed to
123   /// the RTDyldMemoryManager, and other sections are discarded. Passing 'true'
124   /// to this method will cause RuntimeDyld to pass all sections to its
125   /// memory manager regardless of whether they are "required to execute" in the
126   /// usual sense. This is useful for inspecting metadata sections that may not
127   /// contain relocations, E.g. Debug info, stackmaps.
128   ///
129   /// Must be called before the first object file is loaded.
130   void setProcessAllSections(bool ProcessAllSections) {
131     assert(!Dyld && "setProcessAllSections must be called before loadObject.");
132     this->ProcessAllSections = ProcessAllSections;
133   }
134 };
135
136 } // end namespace llvm
137
138 #endif