RuntimeDyldImpl.h revision 76463fdeb603e1d89b05f094bfd6fe73b90d0b61
1//===-- RuntimeDyldImpl.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 implementations of runtime dynamic linker facilities.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_RUNTIME_DYLD_IMPL_H
15#define LLVM_RUNTIME_DYLD_IMPL_H
16
17#include "llvm/ExecutionEngine/RuntimeDyld.h"
18#include "llvm/ADT/DenseMap.h"
19#include "llvm/ADT/StringMap.h"
20#include "llvm/ADT/Twine.h"
21#include "llvm/ADT/SmallVector.h"
22#include "llvm/ExecutionEngine/ExecutionEngine.h"
23#include "llvm/Support/Memory.h"
24#include "llvm/Support/MemoryBuffer.h"
25#include "llvm/Support/system_error.h"
26#include "llvm/Support/raw_ostream.h"
27#include "llvm/Support/Debug.h"
28#include "llvm/Support/ErrorHandling.h"
29
30using namespace llvm;
31
32namespace llvm {
33class RuntimeDyldImpl {
34protected:
35  unsigned CPUType;
36  unsigned CPUSubtype;
37
38  // The MemoryManager to load objects into.
39  RTDyldMemoryManager *MemMgr;
40
41  // For each section, we have a MemoryBlock of it's data.
42  // Indexed by SectionID.
43  SmallVector<sys::MemoryBlock, 32> Sections;
44  // For each section, the address it will be considered to live at for
45  // relocations. The same as the pointer to the above memory block for hosted
46  // JITs. Indexed by SectionID.
47  SmallVector<uint64_t, 32> SectionLoadAddress;
48
49  // Keep a map of starting local address to the SectionID which references it.
50  // Lookup function for when we assign virtual addresses.
51  DenseMap<void *, unsigned> SectionLocalMemToID;
52
53  // Master symbol table. As modules are loaded and external symbols are
54  // resolved, their addresses are stored here as a SectionID/Offset pair.
55  typedef std::pair<unsigned, uint64_t> SymbolLoc;
56  StringMap<SymbolLoc> SymbolTable;
57
58  bool HasError;
59  std::string ErrorStr;
60
61  // Set the error state and record an error string.
62  bool Error(const Twine &Msg) {
63    ErrorStr = Msg.str();
64    HasError = true;
65    return true;
66  }
67
68  uint8_t *getSectionAddress(unsigned SectionID) {
69    return (uint8_t*)Sections[SectionID].base();
70  }
71  void extractFunction(StringRef Name, uint8_t *StartAddress,
72                       uint8_t *EndAddress);
73
74public:
75  RuntimeDyldImpl(RTDyldMemoryManager *mm) : MemMgr(mm), HasError(false) {}
76
77  virtual ~RuntimeDyldImpl();
78
79  virtual bool loadObject(MemoryBuffer *InputBuffer) = 0;
80
81  void *getSymbolAddress(StringRef Name) {
82    // FIXME: Just look up as a function for now. Overly simple of course.
83    // Work in progress.
84    if (SymbolTable.find(Name) == SymbolTable.end())
85      return 0;
86    SymbolLoc Loc = SymbolTable.lookup(Name);
87    return getSectionAddress(Loc.first) + Loc.second;
88  }
89
90  virtual void resolveRelocations();
91
92  virtual void reassignSectionAddress(unsigned SectionID, uint64_t Addr) = 0;
93
94  void mapSectionAddress(void *LocalAddress, uint64_t TargetAddress);
95
96  // Is the linker in an error state?
97  bool hasError() { return HasError; }
98
99  // Mark the error condition as handled and continue.
100  void clearError() { HasError = false; }
101
102  // Get the error message.
103  StringRef getErrorString() { return ErrorStr; }
104
105  virtual bool isCompatibleFormat(const MemoryBuffer *InputBuffer) const = 0;
106};
107
108} // end namespace llvm
109
110
111#endif
112