a71b1411c8aed38914ac0e6cf8450230cc6e3b1a
[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 an abstraction layer
28 // over the JITMemoryManager, which references objects by their source
29 // representations in LLVM IR.
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.
41   virtual uint8_t *allocateCodeSection(uintptr_t Size, unsigned Alignment,
42                                        unsigned SectionID) = 0;
43
44   /// allocateDataSection - Allocate a memory block of (at least) the given
45   /// size suitable for data.
46   virtual uint8_t *allocateDataSection(uintptr_t Size, unsigned Alignment,
47                                        unsigned SectionID) = 0;
48
49   virtual void *getPointerToNamedFunction(const std::string &Name,
50                                           bool AbortOnFailure = true) = 0;
51 };
52
53 class RuntimeDyld {
54   RuntimeDyld(const RuntimeDyld &) LLVM_DELETED_FUNCTION;
55   void operator=(const RuntimeDyld &) LLVM_DELETED_FUNCTION;
56
57   // RuntimeDyldImpl is the actual class. RuntimeDyld is just the public
58   // interface.
59   RuntimeDyldImpl *Dyld;
60   RTDyldMemoryManager *MM;
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 public:
66   RuntimeDyld(RTDyldMemoryManager *);
67   ~RuntimeDyld();
68
69   /// loadObject - prepare the object contained in the input buffer for
70   /// execution.  Ownership of the input buffer is transferred to the
71   /// ObjectImage instance returned from this function if successful.
72   /// In the case of load failure, the input buffer will be deleted.
73   ObjectImage *loadObject(ObjectBuffer *InputBuffer);
74
75   /// Get the address of our local copy of the symbol. This may or may not
76   /// be the address used for relocation (clients can copy the data around
77   /// and resolve relocatons based on where they put it).
78   void *getSymbolAddress(StringRef Name);
79
80   /// Get the address of the target copy of the symbol. This is the address
81   /// used for relocation.
82   uint64_t getSymbolLoadAddress(StringRef Name);
83
84   /// Resolve the relocations for all symbols we currently know about.
85   void resolveRelocations();
86
87   /// mapSectionAddress - map a section to its target address space value.
88   /// Map the address of a JIT section as returned from the memory manager
89   /// to the address in the target process as the running code will see it.
90   /// This is the address which will be used for relocation resolution.
91   void mapSectionAddress(const void *LocalAddress, uint64_t TargetAddress);
92
93   StringRef getErrorString();
94 };
95
96 } // end namespace llvm
97
98 #endif