Minor changes to the MCJITTest unittests to use the correct API for finalizing
[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.  Code sections cannot be executed
66   /// until this function has been called.  In addition, any cache coherency
67   /// operations needed to reliably use the memory are also performed.
68   ///
69   /// Returns true if an error occurred, false otherwise.
70   virtual bool finalizeMemory(std::string *ErrMsg = 0) = 0;
71
72   /// Register the EH frames with the runtime so that c++ exceptions work. The
73   /// default implementation does nothing. Look at SectionMemoryManager for one
74   /// that uses __register_frame.
75   virtual void registerEHFrames(StringRef SectionData);
76 };
77
78 class RuntimeDyld {
79   RuntimeDyld(const RuntimeDyld &) LLVM_DELETED_FUNCTION;
80   void operator=(const RuntimeDyld &) LLVM_DELETED_FUNCTION;
81
82   // RuntimeDyldImpl is the actual class. RuntimeDyld is just the public
83   // interface.
84   RuntimeDyldImpl *Dyld;
85   RTDyldMemoryManager *MM;
86 protected:
87   // Change the address associated with a section when resolving relocations.
88   // Any relocations already associated with the symbol will be re-resolved.
89   void reassignSectionAddress(unsigned SectionID, uint64_t Addr);
90 public:
91   RuntimeDyld(RTDyldMemoryManager *);
92   ~RuntimeDyld();
93
94   /// Prepare the object contained in the input buffer for execution.
95   /// Ownership of the input buffer is transferred to the ObjectImage
96   /// instance returned from this function if successful. In the case of load
97   /// failure, the input buffer will be deleted.
98   ObjectImage *loadObject(ObjectBuffer *InputBuffer);
99
100   /// Get the address of our local copy of the symbol. This may or may not
101   /// be the address used for relocation (clients can copy the data around
102   /// and resolve relocatons based on where they put it).
103   void *getSymbolAddress(StringRef Name);
104
105   /// Get the address of the target copy of the symbol. This is the address
106   /// used for relocation.
107   uint64_t getSymbolLoadAddress(StringRef Name);
108
109   /// Resolve the relocations for all symbols we currently know about.
110   void resolveRelocations();
111
112   /// Map a section to its target address space value.
113   /// Map the address of a JIT section as returned from the memory manager
114   /// to the address in the target process as the running code will see it.
115   /// This is the address which will be used for relocation resolution.
116   void mapSectionAddress(const void *LocalAddress, uint64_t TargetAddress);
117
118   StringRef getErrorString();
119
120   StringRef getEHFrameSection();
121 };
122
123 } // end namespace llvm
124
125 #endif