Eli Bendersky | 058d647 | 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 | |
Lang Hames | 951b235 | 2014-03-08 18:45:12 +0000 | [diff] [blame] | 17 | #include "ObjectImageCommon.h" |
Chandler Carruth | 802d755 | 2012-12-04 07:12:27 +0000 | [diff] [blame] | 18 | #include "RuntimeDyldImpl.h" |
Rafael Espindola | 6e040c0 | 2013-04-26 20:07:33 +0000 | [diff] [blame] | 19 | #include "llvm/Object/MachO.h" |
Eli Bendersky | 058d647 | 2012-01-22 07:05:02 +0000 | [diff] [blame] | 20 | #include "llvm/Support/Format.h" |
Eli Bendersky | 058d647 | 2012-01-22 07:05:02 +0000 | [diff] [blame] | 21 | |
Lang Hames | a521688 | 2014-07-17 18:54:50 +0000 | [diff] [blame^] | 22 | #define DEBUG_TYPE "dyld" |
| 23 | |
Eli Bendersky | 058d647 | 2012-01-22 07:05:02 +0000 | [diff] [blame] | 24 | using namespace llvm; |
| 25 | using namespace llvm::object; |
| 26 | |
Eli Bendersky | 058d647 | 2012-01-22 07:05:02 +0000 | [diff] [blame] | 27 | namespace llvm { |
| 28 | class RuntimeDyldMachO : public RuntimeDyldImpl { |
Lang Hames | a521688 | 2014-07-17 18:54:50 +0000 | [diff] [blame^] | 29 | protected: |
| 30 | struct SectionOffsetPair { |
| 31 | unsigned SectionID; |
| 32 | uint64_t Offset; |
| 33 | }; |
Lang Hames | 7f9fc2b | 2014-05-22 22:30:13 +0000 | [diff] [blame] | 34 | |
Andrew Kaylor | 7bb1344 | 2013-10-11 21:25:48 +0000 | [diff] [blame] | 35 | struct EHFrameRelatedSections { |
Juergen Ributzka | 7608dc0 | 2014-03-21 20:28:42 +0000 | [diff] [blame] | 36 | EHFrameRelatedSections() |
| 37 | : EHFrameSID(RTDYLD_INVALID_SECTION_ID), |
| 38 | TextSID(RTDYLD_INVALID_SECTION_ID), |
| 39 | ExceptTabSID(RTDYLD_INVALID_SECTION_ID) {} |
NAKAMURA Takumi | 87e0880 | 2013-12-07 11:21:42 +0000 | [diff] [blame] | 40 | EHFrameRelatedSections(SID EH, SID T, SID Ex) |
Juergen Ributzka | 7608dc0 | 2014-03-21 20:28:42 +0000 | [diff] [blame] | 41 | : EHFrameSID(EH), TextSID(T), ExceptTabSID(Ex) {} |
Andrew Kaylor | 7bb1344 | 2013-10-11 21:25:48 +0000 | [diff] [blame] | 42 | SID EHFrameSID; |
| 43 | SID TextSID; |
| 44 | SID ExceptTabSID; |
| 45 | }; |
| 46 | |
| 47 | // When a module is loaded we save the SectionID of the EH frame section |
| 48 | // in a table until we receive a request to register all unregistered |
| 49 | // EH frame sections with the memory manager. |
| 50 | SmallVector<EHFrameRelatedSections, 2> UnregisteredEHFrameSections; |
Juergen Ributzka | 7608dc0 | 2014-03-21 20:28:42 +0000 | [diff] [blame] | 51 | |
Eli Bendersky | 058d647 | 2012-01-22 07:05:02 +0000 | [diff] [blame] | 52 | RuntimeDyldMachO(RTDyldMemoryManager *mm) : RuntimeDyldImpl(mm) {} |
| 53 | |
Lang Hames | a521688 | 2014-07-17 18:54:50 +0000 | [diff] [blame^] | 54 | /// Parse the given relocation, which must be a non-scattered, and |
| 55 | /// return a RelocationEntry representing the information. The 'Addend' field |
| 56 | /// will contain the unmodified instruction immediate. |
| 57 | RelocationEntry getBasicRelocationEntry(unsigned SectionID, ObjectImage &Obj, |
| 58 | const relocation_iterator &RI) const; |
Lang Hames | 951b235 | 2014-03-08 18:45:12 +0000 | [diff] [blame] | 59 | |
Lang Hames | a521688 | 2014-07-17 18:54:50 +0000 | [diff] [blame^] | 60 | /// Construct a RelocationValueRef representing the relocation target. |
| 61 | /// For Symbols in known sections, this will return a RelocationValueRef |
| 62 | /// representing a (SectionID, Offset) pair. |
| 63 | /// For Symbols whose section is not known, this will return a |
| 64 | /// (SymbolName, Offset) pair, where the Offset is taken from the instruction |
| 65 | /// immediate (held in RE.Addend). |
| 66 | /// In both cases the Addend field is *NOT* fixed up to be PC-relative. That |
| 67 | /// should be done by the caller where appropriate by calling makePCRel on |
| 68 | /// the RelocationValueRef. |
| 69 | RelocationValueRef getRelocationValueRef(ObjectImage &ObjImg, |
| 70 | const relocation_iterator &RI, |
| 71 | const RelocationEntry &RE, |
| 72 | ObjSectionToIDMap &ObjSectionToID, |
| 73 | const SymbolTableMap &Symbols); |
| 74 | |
| 75 | /// Make the RelocationValueRef addend PC-relative. |
| 76 | void makeValueAddendPCRel(RelocationValueRef &Value, ObjectImage &ObjImg, |
| 77 | const relocation_iterator &RI); |
| 78 | |
| 79 | /// Dump information about the relocation entry (RE) and resolved value. |
| 80 | void dumpRelocationToResolve(const RelocationEntry &RE, uint64_t Value) const; |
| 81 | |
| 82 | public: |
| 83 | /// Create an ObjectImage from the given ObjectBuffer. |
Lang Hames | 84bc818 | 2014-07-15 19:35:22 +0000 | [diff] [blame] | 84 | static ObjectImage *createObjectImage(ObjectBuffer *InputBuffer) { |
| 85 | return new ObjectImageCommon(InputBuffer); |
| 86 | } |
| 87 | |
Lang Hames | a521688 | 2014-07-17 18:54:50 +0000 | [diff] [blame^] | 88 | /// Create an ObjectImage from the given ObjectFile. |
Juergen Ributzka | 7608dc0 | 2014-03-21 20:28:42 +0000 | [diff] [blame] | 89 | static ObjectImage * |
Lang Hames | 84bc818 | 2014-07-15 19:35:22 +0000 | [diff] [blame] | 90 | createObjectImageFromFile(std::unique_ptr<object::ObjectFile> InputObject) { |
| 91 | return new ObjectImageCommon(std::move(InputObject)); |
| 92 | } |
Lang Hames | a521688 | 2014-07-17 18:54:50 +0000 | [diff] [blame^] | 93 | |
| 94 | /// Create a RuntimeDyldMachO instance for the given target architecture. |
| 95 | static std::unique_ptr<RuntimeDyldMachO> create(Triple::ArchType Arch, |
| 96 | RTDyldMemoryManager *mm); |
| 97 | |
| 98 | /// Write the least significant 'Size' bytes in 'Value' out at the address |
| 99 | /// pointed to by Addr. Check for overflow. |
| 100 | bool writeBytesUnaligned(uint8_t *Addr, uint64_t Value, unsigned Size); |
| 101 | |
| 102 | SectionEntry &getSection(unsigned SectionID) { return Sections[SectionID]; } |
| 103 | |
| 104 | bool isCompatibleFormat(const ObjectBuffer *Buffer) const override; |
| 105 | bool isCompatibleFile(const object::ObjectFile *Obj) const override; |
| 106 | void registerEHFrames() override; |
| 107 | }; |
| 108 | |
| 109 | /// RuntimeDyldMachOTarget - Templated base class for generic MachO linker |
| 110 | /// algorithms and data structures. |
| 111 | /// |
| 112 | /// Concrete, target specific sub-classes can be accessed via the impl() |
| 113 | /// methods. (i.e. the RuntimeDyldMachO hierarchy uses the Curiously |
| 114 | /// Recurring Template Idiom). Concrete subclasses for each target |
| 115 | /// can be found in ./Targets. |
| 116 | template <typename Impl> |
| 117 | class RuntimeDyldMachOCRTPBase : public RuntimeDyldMachO { |
| 118 | private: |
| 119 | Impl &impl() { return static_cast<Impl &>(*this); } |
| 120 | const Impl &impl() const { return static_cast<Impl &>(*this); } |
| 121 | |
| 122 | public: |
| 123 | RuntimeDyldMachOCRTPBase(RTDyldMemoryManager *mm) : RuntimeDyldMachO(mm) {} |
| 124 | |
| 125 | void finalizeLoad(ObjectImage &ObjImg, ObjSectionToIDMap &SectionMap) { |
| 126 | unsigned EHFrameSID = RTDYLD_INVALID_SECTION_ID; |
| 127 | unsigned TextSID = RTDYLD_INVALID_SECTION_ID; |
| 128 | unsigned ExceptTabSID = RTDYLD_INVALID_SECTION_ID; |
| 129 | ObjSectionToIDMap::iterator i, e; |
| 130 | |
| 131 | for (i = SectionMap.begin(), e = SectionMap.end(); i != e; ++i) { |
| 132 | const SectionRef &Section = i->first; |
| 133 | StringRef Name; |
| 134 | Section.getName(Name); |
| 135 | if (Name == "__eh_frame") |
| 136 | EHFrameSID = i->second; |
| 137 | else if (Name == "__text") |
| 138 | TextSID = i->second; |
| 139 | else if (Name == "__gcc_except_tab") |
| 140 | ExceptTabSID = i->second; |
| 141 | else |
| 142 | impl().finalizeSection(ObjImg, i->second, Section); |
| 143 | } |
| 144 | UnregisteredEHFrameSections.push_back( |
| 145 | EHFrameRelatedSections(EHFrameSID, TextSID, ExceptTabSID)); |
| 146 | } |
Eli Bendersky | 058d647 | 2012-01-22 07:05:02 +0000 | [diff] [blame] | 147 | }; |
| 148 | |
| 149 | } // end namespace llvm |
| 150 | |
Lang Hames | a521688 | 2014-07-17 18:54:50 +0000 | [diff] [blame^] | 151 | #undef DEBUG_TYPE |
| 152 | |
Eli Bendersky | 058d647 | 2012-01-22 07:05:02 +0000 | [diff] [blame] | 153 | #endif |