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_EXECUTIONENGINE_RUNTIMEDYLD_H
15#define LLVM_EXECUTIONENGINE_RUNTIMEDYLD_H
16
17#include "llvm/ADT/StringRef.h"
18#include "llvm/ExecutionEngine/ObjectBuffer.h"
19#include "llvm/Support/Memory.h"
20
21namespace llvm {
22
23class RuntimeDyldImpl;
24class 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.
32class RTDyldMemoryManager {
33  RTDyldMemoryManager(const RTDyldMemoryManager&) LLVM_DELETED_FUNCTION;
34  void operator=(const RTDyldMemoryManager&) LLVM_DELETED_FUNCTION;
35public:
36  RTDyldMemoryManager() {}
37  virtual ~RTDyldMemoryManager();
38
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
42  /// section.
43  virtual uint8_t *allocateCodeSection(uintptr_t Size, unsigned Alignment,
44                                       unsigned SectionID) = 0;
45
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;
51
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.
54  ///
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;
60
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.
66  ///
67  /// Returns true if an error occurred, false otherwise.
68  virtual bool applyPermissions(std::string *ErrMsg = 0) = 0;
69};
70
71class RuntimeDyld {
72  RuntimeDyld(const RuntimeDyld &) LLVM_DELETED_FUNCTION;
73  void operator=(const RuntimeDyld &) LLVM_DELETED_FUNCTION;
74
75  // RuntimeDyldImpl is the actual class. RuntimeDyld is just the public
76  // interface.
77  RuntimeDyldImpl *Dyld;
78  RTDyldMemoryManager *MM;
79protected:
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);
83public:
84  RuntimeDyld(RTDyldMemoryManager *);
85  ~RuntimeDyld();
86
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);
92
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);
97
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);
101
102  /// Resolve the relocations for all symbols we currently know about.
103  void resolveRelocations();
104
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);
110
111  StringRef getErrorString();
112};
113
114} // end namespace llvm
115
116#endif
117