1 //===-- RuntimeDyldImpl.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 implementations of runtime dynamic linker facilities.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_RUNTIME_DYLD_IMPL_H
15 #define LLVM_RUNTIME_DYLD_IMPL_H
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"
33 class RuntimeDyldImpl {
38 // The MemoryManager to load objects into.
39 RTDyldMemoryManager *MemMgr;
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;
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;
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;
61 // Set the error state and record an error string.
62 bool Error(const Twine &Msg) {
68 uint8_t *getSectionAddress(unsigned SectionID) {
69 return (uint8_t*)Sections[SectionID].base();
71 void extractFunction(StringRef Name, uint8_t *StartAddress,
75 RuntimeDyldImpl(RTDyldMemoryManager *mm) : MemMgr(mm), HasError(false) {}
77 virtual ~RuntimeDyldImpl();
79 virtual bool loadObject(MemoryBuffer *InputBuffer) = 0;
81 void *getSymbolAddress(StringRef Name) {
82 // FIXME: Just look up as a function for now. Overly simple of course.
84 if (SymbolTable.find(Name) == SymbolTable.end())
86 SymbolLoc Loc = SymbolTable.lookup(Name);
87 return getSectionAddress(Loc.first) + Loc.second;
90 virtual void resolveRelocations();
92 virtual void reassignSectionAddress(unsigned SectionID, uint64_t Addr) = 0;
94 void mapSectionAddress(void *LocalAddress, uint64_t TargetAddress);
96 // Is the linker in an error state?
97 bool hasError() { return HasError; }
99 // Mark the error condition as handled and continue.
100 void clearError() { HasError = false; }
102 // Get the error message.
103 StringRef getErrorString() { return ErrorStr; }
105 virtual bool isCompatibleFormat(const MemoryBuffer *InputBuffer) const = 0;
108 } // end namespace llvm