RuntimeDyld.h revision 5fe019835c269ccbfe185276269bc53b3f9a7a86
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/Support/Memory.h"
19
20namespace llvm {
21
22class RuntimeDyldImpl;
23class MemoryBuffer;
24
25// RuntimeDyld clients often want to handle the memory management of
26// what gets placed where. For JIT clients, this is an abstraction layer
27// over the JITMemoryManager, which references objects by their source
28// representations in LLVM IR.
29// FIXME: As the RuntimeDyld fills out, additional routines will be needed
30//        for the varying types of objects to be allocated.
31class RTDyldMemoryManager {
32  RTDyldMemoryManager(const RTDyldMemoryManager&);  // DO NOT IMPLEMENT
33  void operator=(const RTDyldMemoryManager&);       // DO NOT IMPLEMENT
34public:
35  RTDyldMemoryManager() {}
36  virtual ~RTDyldMemoryManager();
37
38  /// allocateCodeSection - Allocate a memory block of (at least) the given
39  /// size suitable for executable code.
40  virtual uint8_t *allocateCodeSection(uintptr_t Size, unsigned Alignment,
41                                       unsigned SectionID) = 0;
42
43  /// allocateDataSection - Allocate a memory block of (at least) the given
44  /// size suitable for data.
45  virtual uint8_t *allocateDataSection(uintptr_t Size, unsigned Alignment,
46                                       unsigned SectionID) = 0;
47
48  virtual void *getPointerToNamedFunction(const std::string &Name,
49                                          bool AbortOnFailure = true) = 0;
50};
51
52class RuntimeDyld {
53  RuntimeDyld(const RuntimeDyld &);     // DO NOT IMPLEMENT
54  void operator=(const RuntimeDyld &);  // DO NOT IMPLEMENT
55
56  // RuntimeDyldImpl is the actual class. RuntimeDyld is just the public
57  // interface.
58  RuntimeDyldImpl *Dyld;
59  RTDyldMemoryManager *MM;
60protected:
61  // Change the address associated with a section when resolving relocations.
62  // Any relocations already associated with the symbol will be re-resolved.
63  void reassignSectionAddress(unsigned SectionID, uint64_t Addr);
64public:
65  RuntimeDyld(RTDyldMemoryManager*);
66  ~RuntimeDyld();
67
68  /// Load an in-memory object file into the dynamic linker.
69  bool loadObject(MemoryBuffer *InputBuffer);
70
71  /// Get the address of our local copy of the symbol. This may or may not
72  /// be the address used for relocation (clients can copy the data around
73  /// and resolve relocatons based on where they put it).
74  void *getSymbolAddress(StringRef Name);
75
76  /// Resolve the relocations for all symbols we currently know about.
77  void resolveRelocations();
78
79  /// mapSectionAddress - map a section to its target address space value.
80  /// Map the address of a JIT section as returned from the memory manager
81  /// to the address in the target process as the running code will see it.
82  /// This is the address which will be used for relocation resolution.
83  void mapSectionAddress(void *LocalAddress, uint64_t TargetAddress);
84
85  StringRef getErrorString();
86};
87
88} // end namespace llvm
89
90#endif
91