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 { |
Chandler Carruth | 3e29671 | 2012-03-22 05:44:06 +0000 | [diff] [blame] | 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 | |
| 47 | // For each section, keep a list of referrers in that section that are clients |
| 48 | // of relocations in other sections. Whenever a relocation gets created, |
| 49 | // create a corresponding referrer. Whenever relocations are re-resolved, |
| 50 | // re-resolve the referrers' relocations as well. |
| 51 | struct Referrer { |
| 52 | unsigned SectionID; // Section whose RelocationList contains the relocation. |
| 53 | uint32_t Index; // Index of the RelocatonEntry in that RelocationList. |
| 54 | |
| 55 | Referrer(unsigned id, uint32_t index) |
| 56 | : SectionID(id), Index(index) {} |
| 57 | }; |
| 58 | typedef SmallVector<Referrer, 4> ReferrerList; |
| 59 | |
| 60 | // Relocations to sections already loaded. Indexed by SectionID which is the |
| 61 | // source of the address. The target where the address will be writen is |
| 62 | // SectionID/Offset in the relocation itself. |
| 63 | IndexedMap<RelocationList> Relocations; |
| 64 | // Referrers corresponding to Relocations. |
| 65 | IndexedMap<ReferrerList> Referrers; |
| 66 | // Relocations to symbols that are not yet resolved. Must be external |
| 67 | // relocations by definition. Indexed by symbol name. |
| 68 | StringMap<RelocationList> UnresolvedRelocations; |
| 69 | |
| 70 | bool resolveRelocation(uint8_t *LocalAddress, |
| 71 | uint64_t FinalAddress, |
| 72 | uint64_t Value, |
| 73 | bool isPCRel, |
| 74 | unsigned Type, |
| 75 | unsigned Size, |
| 76 | int64_t Addend); |
Sean Callanan | 61dfa77 | 2012-03-07 23:05:25 +0000 | [diff] [blame] | 77 | bool resolveX86_64Relocation(uint8_t *LocalAddress, |
| 78 | uint64_t FinalAddress, |
| 79 | uint64_t Value, |
| 80 | bool isPCRel, |
| 81 | unsigned Type, |
| 82 | unsigned Size, |
| 83 | int64_t Addend); |
| 84 | bool resolveARMRelocation(uint8_t *LocalAddress, |
| 85 | uint64_t FinalAddress, |
| 86 | uint64_t Value, |
| 87 | bool isPCRel, |
| 88 | unsigned Type, |
| 89 | unsigned Size, |
| 90 | int64_t Addend); |
Eli Bendersky | 76463fd | 2012-01-22 07:05:02 +0000 | [diff] [blame] | 91 | |
Chandler Carruth | 3e29671 | 2012-03-22 05:44:06 +0000 | [diff] [blame] | 92 | bool loadSegment32(const MachOObject *Obj, |
| 93 | const MachOObject::LoadCommandInfo *SegmentLCI, |
| 94 | const InMemoryStruct<macho::SymtabLoadCommand> &SymtabLC); |
| 95 | bool loadSegment64(const MachOObject *Obj, |
| 96 | const MachOObject::LoadCommandInfo *SegmentLCI, |
| 97 | const InMemoryStruct<macho::SymtabLoadCommand> &SymtabLC); |
| 98 | bool processSymbols32(const MachOObject *Obj, |
| 99 | SmallVectorImpl<unsigned> &SectionMap, |
| 100 | SmallVectorImpl<StringRef> &SymbolNames, |
| 101 | const InMemoryStruct<macho::SymtabLoadCommand> &SymtabLC); |
| 102 | bool processSymbols64(const MachOObject *Obj, |
| 103 | SmallVectorImpl<unsigned> &SectionMap, |
| 104 | SmallVectorImpl<StringRef> &SymbolNames, |
| 105 | const InMemoryStruct<macho::SymtabLoadCommand> &SymtabLC); |
| 106 | |
| 107 | void resolveSymbol(StringRef Name); |
Eli Bendersky | 76463fd | 2012-01-22 07:05:02 +0000 | [diff] [blame] | 108 | |
| 109 | public: |
| 110 | RuntimeDyldMachO(RTDyldMemoryManager *mm) : RuntimeDyldImpl(mm) {} |
| 111 | |
Chandler Carruth | 3e29671 | 2012-03-22 05:44:06 +0000 | [diff] [blame] | 112 | bool loadObject(MemoryBuffer *InputBuffer); |
| 113 | |
| 114 | void reassignSectionAddress(unsigned SectionID, uint64_t Addr); |
| 115 | |
| 116 | static bool isKnownFormat(const MemoryBuffer *InputBuffer); |
| 117 | |
| 118 | bool isCompatibleFormat(const MemoryBuffer *InputBuffer) const { |
| 119 | return isKnownFormat(InputBuffer); |
| 120 | } |
Eli Bendersky | 76463fd | 2012-01-22 07:05:02 +0000 | [diff] [blame] | 121 | }; |
| 122 | |
| 123 | } // end namespace llvm |
| 124 | |
| 125 | #endif |