blob: 28e99be9ab5ad02d93ec51a1b74ee5a15b1c1889 [file] [log] [blame]
Jim Grosbache0934be2012-01-16 23:50:58 +00001//===-- RuntimeDyldImpl.h - Run-time dynamic linker for MC-JIT --*- C++ -*-===//
Danil Malyshevcf852dc2011-07-13 07:57:58 +00002//
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"
Jim Grosbach020f4e82012-01-16 23:50:55 +000018#include "llvm/ADT/DenseMap.h"
Danil Malyshevcf852dc2011-07-13 07:57:58 +000019#include "llvm/ADT/StringMap.h"
20#include "llvm/ADT/Twine.h"
21#include "llvm/ADT/SmallVector.h"
Bill Wendling288967d2012-03-29 23:23:59 +000022#include "llvm/ExecutionEngine/ExecutionEngine.h"
Danil Malyshevcf852dc2011-07-13 07:57:58 +000023#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;
Danil Malyshevcf852dc2011-07-13 07:57:58 +000031
32namespace llvm {
33class RuntimeDyldImpl {
34protected:
Bill Wendling288967d2012-03-29 23:23:59 +000035 unsigned CPUType;
36 unsigned CPUSubtype;
37
Danil Malyshevcf852dc2011-07-13 07:57:58 +000038 // The MemoryManager to load objects into.
39 RTDyldMemoryManager *MemMgr;
40
Bill Wendling288967d2012-03-29 23:23:59 +000041 // 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;
Danil Malyshevcf852dc2011-07-13 07:57:58 +000048
Bill Wendling288967d2012-03-29 23:23:59 +000049 // 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;
Jim Grosbach020f4e82012-01-16 23:50:55 +000052
Danil Malyshevcf852dc2011-07-13 07:57:58 +000053 // Master symbol table. As modules are loaded and external symbols are
Jim Grosbach61425c02012-01-16 22:26:39 +000054 // resolved, their addresses are stored here as a SectionID/Offset pair.
Bill Wendling288967d2012-03-29 23:23:59 +000055 typedef std::pair<unsigned, uint64_t> SymbolLoc;
Jim Grosbach61425c02012-01-16 22:26:39 +000056 StringMap<SymbolLoc> SymbolTable;
Danil Malyshevcf852dc2011-07-13 07:57:58 +000057
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
Jim Grosbach61425c02012-01-16 22:26:39 +000068 uint8_t *getSectionAddress(unsigned SectionID) {
Bill Wendling288967d2012-03-29 23:23:59 +000069 return (uint8_t*)Sections[SectionID].base();
Jim Grosbach61425c02012-01-16 22:26:39 +000070 }
Bill Wendling288967d2012-03-29 23:23:59 +000071 void extractFunction(StringRef Name, uint8_t *StartAddress,
72 uint8_t *EndAddress);
Danil Malyshevcf852dc2011-07-13 07:57:58 +000073
74public:
75 RuntimeDyldImpl(RTDyldMemoryManager *mm) : MemMgr(mm), HasError(false) {}
76
77 virtual ~RuntimeDyldImpl();
78
Bill Wendling288967d2012-03-29 23:23:59 +000079 virtual bool loadObject(MemoryBuffer *InputBuffer) = 0;
Danil Malyshevcf852dc2011-07-13 07:57:58 +000080
81 void *getSymbolAddress(StringRef Name) {
82 // FIXME: Just look up as a function for now. Overly simple of course.
83 // Work in progress.
Jim Grosbach61425c02012-01-16 22:26:39 +000084 if (SymbolTable.find(Name) == SymbolTable.end())
85 return 0;
86 SymbolLoc Loc = SymbolTable.lookup(Name);
87 return getSectionAddress(Loc.first) + Loc.second;
Danil Malyshevcf852dc2011-07-13 07:57:58 +000088 }
89
Bill Wendling288967d2012-03-29 23:23:59 +000090 virtual void resolveRelocations();
Danil Malyshevcf852dc2011-07-13 07:57:58 +000091
Bill Wendling288967d2012-03-29 23:23:59 +000092 virtual void reassignSectionAddress(unsigned SectionID, uint64_t Addr) = 0;
Danil Malyshevcf852dc2011-07-13 07:57:58 +000093
Jim Grosbach020f4e82012-01-16 23:50:55 +000094 void mapSectionAddress(void *LocalAddress, uint64_t TargetAddress);
95
Danil Malyshevcf852dc2011-07-13 07:57:58 +000096 // 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
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000108} // end namespace llvm
109
110
111#endif