Eli Bendersky | 76463fd | 2012-01-22 07:05:02 +0000 | [diff] [blame^] | 1 | //===-- RuntimeDyldMachO.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 | // MachO support for MC-JIT runtime dynamic linker. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #ifndef LLVM_RUNTIME_DYLD_MACHO_H |
| 15 | #define LLVM_RUNTIME_DYLD_MACHO_H |
| 16 | |
| 17 | #include "llvm/ADT/IndexedMap.h" |
| 18 | #include "llvm/Object/MachOObject.h" |
| 19 | #include "llvm/Support/Format.h" |
| 20 | #include "RuntimeDyldImpl.h" |
| 21 | |
| 22 | using namespace llvm; |
| 23 | using namespace llvm::object; |
| 24 | |
| 25 | |
| 26 | namespace llvm { |
| 27 | class RuntimeDyldMachO : public RuntimeDyldImpl { |
| 28 | |
| 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. |
| 41 | |
| 42 | RelocationEntry(unsigned id, uint64_t offset, uint32_t data, int64_t addend) |
| 43 | : SectionID(id), Offset(offset), Data(data), Addend(addend) {} |
| 44 | }; |
| 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; |
| 53 | |
| 54 | bool resolveRelocation(uint8_t *Address, uint64_t Value, bool isPCRel, |
| 55 | unsigned Type, unsigned Size, int64_t Addend); |
| 56 | bool resolveX86_64Relocation(uintptr_t Address, uintptr_t Value, bool isPCRel, |
| 57 | unsigned Type, unsigned Size, int64_t Addend); |
| 58 | bool resolveARMRelocation(uintptr_t Address, uintptr_t Value, bool isPCRel, |
| 59 | unsigned Type, unsigned Size, int64_t Addend); |
| 60 | |
| 61 | bool loadSegment32(const MachOObject *Obj, |
| 62 | const MachOObject::LoadCommandInfo *SegmentLCI, |
| 63 | const InMemoryStruct<macho::SymtabLoadCommand> &SymtabLC); |
| 64 | bool loadSegment64(const MachOObject *Obj, |
| 65 | const MachOObject::LoadCommandInfo *SegmentLCI, |
| 66 | const InMemoryStruct<macho::SymtabLoadCommand> &SymtabLC); |
| 67 | bool processSymbols32(const MachOObject *Obj, |
| 68 | SmallVectorImpl<unsigned> &SectionMap, |
| 69 | SmallVectorImpl<StringRef> &SymbolNames, |
| 70 | const InMemoryStruct<macho::SymtabLoadCommand> &SymtabLC); |
| 71 | bool processSymbols64(const MachOObject *Obj, |
| 72 | SmallVectorImpl<unsigned> &SectionMap, |
| 73 | SmallVectorImpl<StringRef> &SymbolNames, |
| 74 | const InMemoryStruct<macho::SymtabLoadCommand> &SymtabLC); |
| 75 | |
| 76 | void resolveSymbol(StringRef Name); |
| 77 | |
| 78 | public: |
| 79 | RuntimeDyldMachO(RTDyldMemoryManager *mm) : RuntimeDyldImpl(mm) {} |
| 80 | |
| 81 | bool loadObject(MemoryBuffer *InputBuffer); |
| 82 | |
| 83 | void reassignSectionAddress(unsigned SectionID, uint64_t Addr); |
| 84 | |
| 85 | static bool isKnownFormat(const MemoryBuffer *InputBuffer); |
| 86 | |
| 87 | bool isCompatibleFormat(const MemoryBuffer *InputBuffer) const { |
| 88 | return isKnownFormat(InputBuffer); |
| 89 | } |
| 90 | }; |
| 91 | |
| 92 | } // end namespace llvm |
| 93 | |
| 94 | #endif |
| 95 | |
| 96 | //===-- RuntimeDyldMachO.h - Run-time dynamic linker for MC-JIT ---*- C++ -*-=// |
| 97 | // |
| 98 | // The LLVM Compiler Infrastructure |
| 99 | // |
| 100 | // This file is distributed under the University of Illinois Open Source |
| 101 | // License. See LICENSE.TXT for details. |
| 102 | // |
| 103 | //===----------------------------------------------------------------------===// |
| 104 | // |
| 105 | // MachO support for MC-JIT runtime dynamic linker. |
| 106 | // |
| 107 | //===----------------------------------------------------------------------===// |
| 108 | |
| 109 | #ifndef LLVM_RUNTIME_DYLD_MACHO_H |
| 110 | #define LLVM_RUNTIME_DYLD_MACHO_H |
| 111 | |
| 112 | #include "llvm/ADT/IndexedMap.h" |
| 113 | #include "llvm/Object/MachOObject.h" |
| 114 | #include "llvm/Support/Format.h" |
| 115 | #include "RuntimeDyldImpl.h" |
| 116 | |
| 117 | using namespace llvm; |
| 118 | using namespace llvm::object; |
| 119 | |
| 120 | |
| 121 | namespace llvm { |
| 122 | class RuntimeDyldMachO : public RuntimeDyldImpl { |
| 123 | |
| 124 | // For each symbol, keep a list of relocations based on it. Anytime |
| 125 | // its address is reassigned (the JIT re-compiled the function, e.g.), |
| 126 | // the relocations get re-resolved. |
| 127 | // The symbol (or section) the relocation is sourced from is the Key |
| 128 | // in the relocation list where it's stored. |
| 129 | struct RelocationEntry { |
| 130 | unsigned SectionID; // Section the relocation is contained in. |
| 131 | uint64_t Offset; // Offset into the section for the relocation. |
| 132 | uint32_t Data; // Second word of the raw macho relocation entry. |
| 133 | int64_t Addend; // Addend encoded in the instruction itself, if any, |
| 134 | // plus the offset into the source section for |
| 135 | // the symbol once the relocation is resolvable. |
| 136 | |
| 137 | RelocationEntry(unsigned id, uint64_t offset, uint32_t data, int64_t addend) |
| 138 | : SectionID(id), Offset(offset), Data(data), Addend(addend) {} |
| 139 | }; |
| 140 | typedef SmallVector<RelocationEntry, 4> RelocationList; |
| 141 | // Relocations to sections already loaded. Indexed by SectionID which is the |
| 142 | // source of the address. The target where the address will be writen is |
| 143 | // SectionID/Offset in the relocation itself. |
| 144 | IndexedMap<RelocationList> Relocations; |
| 145 | // Relocations to symbols that are not yet resolved. Must be external |
| 146 | // relocations by definition. Indexed by symbol name. |
| 147 | StringMap<RelocationList> UnresolvedRelocations; |
| 148 | |
| 149 | bool resolveRelocation(uint8_t *Address, uint64_t Value, bool isPCRel, |
| 150 | unsigned Type, unsigned Size, int64_t Addend); |
| 151 | bool resolveX86_64Relocation(uintptr_t Address, uintptr_t Value, bool isPCRel, |
| 152 | unsigned Type, unsigned Size, int64_t Addend); |
| 153 | bool resolveARMRelocation(uintptr_t Address, uintptr_t Value, bool isPCRel, |
| 154 | unsigned Type, unsigned Size, int64_t Addend); |
| 155 | |
| 156 | bool loadSegment32(const MachOObject *Obj, |
| 157 | const MachOObject::LoadCommandInfo *SegmentLCI, |
| 158 | const InMemoryStruct<macho::SymtabLoadCommand> &SymtabLC); |
| 159 | bool loadSegment64(const MachOObject *Obj, |
| 160 | const MachOObject::LoadCommandInfo *SegmentLCI, |
| 161 | const InMemoryStruct<macho::SymtabLoadCommand> &SymtabLC); |
| 162 | bool processSymbols32(const MachOObject *Obj, |
| 163 | SmallVectorImpl<unsigned> &SectionMap, |
| 164 | SmallVectorImpl<StringRef> &SymbolNames, |
| 165 | const InMemoryStruct<macho::SymtabLoadCommand> &SymtabLC); |
| 166 | bool processSymbols64(const MachOObject *Obj, |
| 167 | SmallVectorImpl<unsigned> &SectionMap, |
| 168 | SmallVectorImpl<StringRef> &SymbolNames, |
| 169 | const InMemoryStruct<macho::SymtabLoadCommand> &SymtabLC); |
| 170 | |
| 171 | void resolveSymbol(StringRef Name); |
| 172 | |
| 173 | public: |
| 174 | RuntimeDyldMachO(RTDyldMemoryManager *mm) : RuntimeDyldImpl(mm) {} |
| 175 | |
| 176 | bool loadObject(MemoryBuffer *InputBuffer); |
| 177 | |
| 178 | void reassignSectionAddress(unsigned SectionID, uint64_t Addr); |
| 179 | |
| 180 | static bool isKnownFormat(const MemoryBuffer *InputBuffer); |
| 181 | |
| 182 | bool isCompatibleFormat(const MemoryBuffer *InputBuffer) const { |
| 183 | return isKnownFormat(InputBuffer); |
| 184 | } |
| 185 | }; |
| 186 | |
| 187 | } // end namespace llvm |
| 188 | |
| 189 | #endif |
| 190 | |