Add EH support to the MCJIT.
[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 "llvm/ADT/StringRef.h"
18 #include "llvm/ExecutionEngine/ObjectBuffer.h"
19 #include "llvm/Support/Memory.h"
20
21 namespace llvm {
22
23 class RuntimeDyldImpl;
24 class ObjectImage;
25
26 // RuntimeDyld clients often want to handle the memory management of
27 // what gets placed where. For JIT clients, this is the subset of
28 // JITMemoryManager required for dynamic loading of binaries.
29 //
30 // FIXME: As the RuntimeDyld fills out, additional routines will be needed
31 //        for the varying types of objects to be allocated.
32 class RTDyldMemoryManager {
33   RTDyldMemoryManager(const RTDyldMemoryManager&) LLVM_DELETED_FUNCTION;
34   void operator=(const RTDyldMemoryManager&) LLVM_DELETED_FUNCTION;
35 public:
36   RTDyldMemoryManager() {}
37   virtual ~RTDyldMemoryManager();
38
39   /// Allocate a memory block of (at least) the given size suitable for
40   /// executable code. The SectionID is a unique identifier assigned by the JIT
41   /// engine, and optionally recorded by the memory manager to access a loaded
42   /// section.
43   virtual uint8_t *allocateCodeSection(uintptr_t Size, unsigned Alignment,
44                                        unsigned SectionID) = 0;
45
46   /// Allocate a memory block of (at least) the given size suitable for data.
47   /// The SectionID is a unique identifier assigned by the JIT engine, and
48   /// optionally recorded by the memory manager to access a loaded section.
49   virtual uint8_t *allocateDataSection(uintptr_t Size, unsigned Alignment,
50                                        unsigned SectionID, bool IsReadOnly) = 0;
51
52   /// This method returns the address of the specified function. As such it is
53   /// only useful for resolving library symbols, not code generated symbols.
54   ///
55   /// If AbortOnFailure is false and no function with the given name is
56   /// found, this function returns a null pointer. Otherwise, it prints a
57   /// message to stderr and aborts.
58   virtual void *getPointerToNamedFunction(const std::string &Name,
59                                           bool AbortOnFailure = true) = 0;
60
61   /// This method is called when object loading is complete and section page
62   /// permissions can be applied.  It is up to the memory manager implementation
63   /// to decide whether or not to act on this method.  The memory manager will
64   /// typically allocate all sections as read-write and then apply specific
65   /// permissions when this method is called.
66   ///
67   /// Returns true if an error occurred, false otherwise.
68   virtual bool applyPermissions(std::string *ErrMsg = 0) = 0;
69
70   /// Register the EH frames with the runtime so that c++ exceptions work. The
71   /// default implementation does nothing. Look at SectionMemoryManager for one
72   /// that uses __register_frame.
73   virtual void registerEHFrames(StringRef SectionData);
74 };
75
76 class RuntimeDyld {
77   RuntimeDyld(const RuntimeDyld &) LLVM_DELETED_FUNCTION;
78   void operator=(const RuntimeDyld &) LLVM_DELETED_FUNCTION;
79
80   // RuntimeDyldImpl is the actual class. RuntimeDyld is just the public
81   // interface.
82   RuntimeDyldImpl *Dyld;
83   RTDyldMemoryManager *MM;
84 protected:
85   // Change the address associated with a section when resolving relocations.
86   // Any relocations already associated with the symbol will be re-resolved.
87   void reassignSectionAddress(unsigned SectionID, uint64_t Addr);
88 public:
89   RuntimeDyld(RTDyldMemoryManager *);
90   ~RuntimeDyld();
91
92   /// Prepare the object contained in the input buffer for execution.
93   /// Ownership of the input buffer is transferred to the ObjectImage
94   /// instance returned from this function if successful. In the case of load
95   /// failure, the input buffer will be deleted.
96   ObjectImage *loadObject(ObjectBuffer *InputBuffer);
97
98   /// Get the address of our local copy of the symbol. This may or may not
99   /// be the address used for relocation (clients can copy the data around
100   /// and resolve relocatons based on where they put it).
101   void *getSymbolAddress(StringRef Name);
102
103   /// Get the address of the target copy of the symbol. This is the address
104   /// used for relocation.
105   uint64_t getSymbolLoadAddress(StringRef Name);
106
107   /// Resolve the relocations for all symbols we currently know about.
108   void resolveRelocations();
109
110   /// Map a section to its target address space value.
111   /// Map the address of a JIT section as returned from the memory manager
112   /// to the address in the target process as the running code will see it.
113   /// This is the address which will be used for relocation resolution.
114   void mapSectionAddress(const void *LocalAddress, uint64_t TargetAddress);
115
116   StringRef getErrorString();
117
118   StringRef getEHFrameSection();
119 };
120
121 } // end namespace llvm
122
123 #endif