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. 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.
69 /// Returns true if an error occurred, false otherwise.
70 virtual bool finalizeMemory(std::string *ErrMsg = 0) = 0;
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);
79 RuntimeDyld(const RuntimeDyld &) LLVM_DELETED_FUNCTION;
80 void operator=(const RuntimeDyld &) LLVM_DELETED_FUNCTION;
82 // RuntimeDyldImpl is the actual class. RuntimeDyld is just the public
84 RuntimeDyldImpl *Dyld;
85 RTDyldMemoryManager *MM;
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);
91 RuntimeDyld(RTDyldMemoryManager *);
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);
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);
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);
109 /// Resolve the relocations for all symbols we currently know about.
110 void resolveRelocations();
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);
118 StringRef getErrorString();
120 StringRef getEHFrameSection();
123 } // end namespace llvm