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_EXECUTIONENGINE_RUNTIMEDYLD_H
15 #define LLVM_EXECUTIONENGINE_RUNTIMEDYLD_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 /// 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
43 virtual uint8_t *allocateCodeSection(uintptr_t Size, unsigned Alignment,
44 unsigned SectionID) = 0;
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;
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.
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;
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.
67 /// Returns true if an error occurred, false otherwise.
68 virtual bool applyPermissions(std::string *ErrMsg = 0) = 0;
72 RuntimeDyld(const RuntimeDyld &) LLVM_DELETED_FUNCTION;
73 void operator=(const RuntimeDyld &) LLVM_DELETED_FUNCTION;
75 // RuntimeDyldImpl is the actual class. RuntimeDyld is just the public
77 RuntimeDyldImpl *Dyld;
78 RTDyldMemoryManager *MM;
80 // Change the address associated with a section when resolving relocations.
81 // Any relocations already associated with the symbol will be re-resolved.
82 void reassignSectionAddress(unsigned SectionID, uint64_t Addr);
84 RuntimeDyld(RTDyldMemoryManager *);
87 /// Prepare the object contained in the input buffer for execution.
88 /// Ownership of the input buffer is transferred to the ObjectImage
89 /// instance returned from this function if successful. In the case of load
90 /// failure, the input buffer will be deleted.
91 ObjectImage *loadObject(ObjectBuffer *InputBuffer);
93 /// Get the address of our local copy of the symbol. This may or may not
94 /// be the address used for relocation (clients can copy the data around
95 /// and resolve relocatons based on where they put it).
96 void *getSymbolAddress(StringRef Name);
98 /// Get the address of the target copy of the symbol. This is the address
99 /// used for relocation.
100 uint64_t getSymbolLoadAddress(StringRef Name);
102 /// Resolve the relocations for all symbols we currently know about.
103 void resolveRelocations();
105 /// Map a section to its target address space value.
106 /// Map the address of a JIT section as returned from the memory manager
107 /// to the address in the target process as the running code will see it.
108 /// This is the address which will be used for relocation resolution.
109 void mapSectionAddress(const void *LocalAddress, uint64_t TargetAddress);
111 StringRef getErrorString();
114 } // end namespace llvm