1 //===-- RuntimeDyld.h - Run-time dynamic linker for MC-JIT ------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Interface for the runtime dynamic linker facilities of the MC-JIT.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_RUNTIME_DYLD_H
15 #define LLVM_RUNTIME_DYLD_H
17 #include "llvm/ADT/StringRef.h"
18 #include "llvm/ExecutionEngine/ObjectBuffer.h"
19 #include "llvm/Support/Memory.h"
23 class RuntimeDyldImpl;
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.
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;
36 RTDyldMemoryManager() {}
37 virtual ~RTDyldMemoryManager();
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;
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;
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.
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;
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
70 virtual bool applyPermissions(std::string *ErrMsg = 0) = 0;
74 RuntimeDyld(const RuntimeDyld &) LLVM_DELETED_FUNCTION;
75 void operator=(const RuntimeDyld &) LLVM_DELETED_FUNCTION;
77 // RuntimeDyldImpl is the actual class. RuntimeDyld is just the public
79 RuntimeDyldImpl *Dyld;
80 RTDyldMemoryManager *MM;
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);
86 RuntimeDyld(RTDyldMemoryManager *);
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);
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);
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);
104 /// Resolve the relocations for all symbols we currently know about.
105 void resolveRelocations();
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);
113 StringRef getErrorString();
116 } // end namespace llvm