c3d160f0125b858a3a91af71c24fc52c9c5203da
[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_RUNTIME_DYLD_H
15 #define LLVM_RUNTIME_DYLD_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   /// allocateCodeSection - Allocate a memory block of (at least) the given
40   /// size suitable for executable code. The SectionID is a unique identifier
41   /// assigned by the JIT engine, and optionally recorded by the memory manager
42   /// to access a loaded section.
43   virtual uint8_t *allocateCodeSection(uintptr_t Size, unsigned Alignment,
44                                        unsigned SectionID) = 0;
45
46   /// allocateDataSection - Allocate a memory block of (at least) the given
47   /// size suitable for data. The SectionID is a unique identifier
48   /// assigned by the JIT engine, and optionally recorded by the memory manager
49   /// to access a loaded section.
50   virtual uint8_t *allocateDataSection(uintptr_t Size, unsigned Alignment,
51                                        unsigned SectionID, bool IsReadOnly) = 0;
52
53   /// getPointerToNamedFunction - This method returns the address of the
54   /// specified function. As such it is only useful for resolving library
55   /// symbols, not code generated symbols.
56   ///
57   /// If AbortOnFailure is false and no function with the given name is
58   /// found, this function returns a null pointer. Otherwise, it prints a
59   /// message to stderr and aborts.
60   virtual void *getPointerToNamedFunction(const std::string &Name,
61                                           bool AbortOnFailure = true) = 0;
62
63   /// applyPermissions - This method is called when object loading is
64   /// complete and section page permissions can be applied.  It is up to
65   /// the memory manager implementation to decide whether or not to act
66   /// on this method.  The memory manager will typically allocate all 
67   /// sections as read-write and then apply specific permissions when
68   /// this method is called.  Returns true if an error occurred, false
69   /// otherwise.
70   virtual bool applyPermissions(std::string *ErrMsg = 0) = 0;
71 };
72
73 class RuntimeDyld {
74   RuntimeDyld(const RuntimeDyld &) LLVM_DELETED_FUNCTION;
75   void operator=(const RuntimeDyld &) LLVM_DELETED_FUNCTION;
76
77   // RuntimeDyldImpl is the actual class. RuntimeDyld is just the public
78   // interface.
79   RuntimeDyldImpl *Dyld;
80   RTDyldMemoryManager *MM;
81 protected:
82   // Change the address associated with a section when resolving relocations.
83   // Any relocations already associated with the symbol will be re-resolved.
84   void reassignSectionAddress(unsigned SectionID, uint64_t Addr);
85 public:
86   RuntimeDyld(RTDyldMemoryManager *);
87   ~RuntimeDyld();
88
89   /// loadObject - prepare the object contained in the input buffer for
90   /// execution.  Ownership of the input buffer is transferred to the
91   /// ObjectImage instance returned from this function if successful.
92   /// In the case of load failure, the input buffer will be deleted.
93   ObjectImage *loadObject(ObjectBuffer *InputBuffer);
94
95   /// Get the address of our local copy of the symbol. This may or may not
96   /// be the address used for relocation (clients can copy the data around
97   /// and resolve relocatons based on where they put it).
98   void *getSymbolAddress(StringRef Name);
99
100   /// Get the address of the target copy of the symbol. This is the address
101   /// used for relocation.
102   uint64_t getSymbolLoadAddress(StringRef Name);
103
104   /// Resolve the relocations for all symbols we currently know about.
105   void resolveRelocations();
106
107   /// mapSectionAddress - map a section to its target address space value.
108   /// Map the address of a JIT section as returned from the memory manager
109   /// to the address in the target process as the running code will see it.
110   /// This is the address which will be used for relocation resolution.
111   void mapSectionAddress(const void *LocalAddress, uint64_t TargetAddress);
112
113   StringRef getErrorString();
114 };
115
116 } // end namespace llvm
117
118 #endif