1 //===-- RuntimeDyldMachO.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 // MachO support for MC-JIT runtime dynamic linker.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_RUNTIME_DYLD_MACHO_H
15 #define LLVM_RUNTIME_DYLD_MACHO_H
17 #include "llvm/ADT/IndexedMap.h"
18 #include "llvm/Object/MachOObject.h"
19 #include "llvm/Support/Format.h"
20 #include "RuntimeDyldImpl.h"
23 using namespace llvm::object;
27 class RuntimeDyldMachO : public RuntimeDyldImpl {
29 // For each symbol, keep a list of relocations based on it. Anytime
30 // its address is reassigned (the JIT re-compiled the function, e.g.),
31 // the relocations get re-resolved.
32 // The symbol (or section) the relocation is sourced from is the Key
33 // in the relocation list where it's stored.
34 struct RelocationEntry {
35 unsigned SectionID; // Section the relocation is contained in.
36 uint64_t Offset; // Offset into the section for the relocation.
37 uint32_t Data; // Second word of the raw macho relocation entry.
38 int64_t Addend; // Addend encoded in the instruction itself, if any,
39 // plus the offset into the source section for
40 // the symbol once the relocation is resolvable.
42 RelocationEntry(unsigned id, uint64_t offset, uint32_t data, int64_t addend)
43 : SectionID(id), Offset(offset), Data(data), Addend(addend) {}
45 typedef SmallVector<RelocationEntry, 4> RelocationList;
46 // Relocations to sections already loaded. Indexed by SectionID which is the
47 // source of the address. The target where the address will be writen is
48 // SectionID/Offset in the relocation itself.
49 IndexedMap<RelocationList> Relocations;
50 // Relocations to symbols that are not yet resolved. Must be external
51 // relocations by definition. Indexed by symbol name.
52 StringMap<RelocationList> UnresolvedRelocations;
54 bool resolveRelocation(uint8_t *LocalAddress,
55 uint64_t FinalAddress,
61 bool resolveX86_64Relocation(uint8_t *LocalAddress,
62 uint64_t FinalAddress,
68 bool resolveARMRelocation(uint8_t *LocalAddress,
69 uint64_t FinalAddress,
76 bool loadSegment32(const MachOObject *Obj,
77 const MachOObject::LoadCommandInfo *SegmentLCI,
78 const InMemoryStruct<macho::SymtabLoadCommand> &SymtabLC);
79 bool loadSegment64(const MachOObject *Obj,
80 const MachOObject::LoadCommandInfo *SegmentLCI,
81 const InMemoryStruct<macho::SymtabLoadCommand> &SymtabLC);
82 bool processSymbols32(const MachOObject *Obj,
83 SmallVectorImpl<unsigned> &SectionMap,
84 SmallVectorImpl<StringRef> &SymbolNames,
85 const InMemoryStruct<macho::SymtabLoadCommand> &SymtabLC);
86 bool processSymbols64(const MachOObject *Obj,
87 SmallVectorImpl<unsigned> &SectionMap,
88 SmallVectorImpl<StringRef> &SymbolNames,
89 const InMemoryStruct<macho::SymtabLoadCommand> &SymtabLC);
91 void resolveSymbol(StringRef Name);
94 RuntimeDyldMachO(RTDyldMemoryManager *mm) : RuntimeDyldImpl(mm) {}
96 bool loadObject(MemoryBuffer *InputBuffer);
98 void reassignSectionAddress(unsigned SectionID, uint64_t Addr);
100 static bool isKnownFormat(const MemoryBuffer *InputBuffer);
102 bool isCompatibleFormat(const MemoryBuffer *InputBuffer) const {
103 return isKnownFormat(InputBuffer);
107 } // end namespace llvm