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 | |
Benjamin Kramer | a7c40ef | 2014-08-13 16:26:38 +0000 | [diff] [blame] | 14 | #ifndef LLVM_LIB_EXECUTIONENGINE_RUNTIMEDYLD_RUNTIMEDYLDMACHO_H |
| 15 | #define LLVM_LIB_EXECUTIONENGINE_RUNTIMEDYLD_RUNTIMEDYLDMACHO_H |
Eli Bendersky | 058d647 | 2012-01-22 07:05:02 +0000 | [diff] [blame] | 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) {} |
Lang Hames | f7acddd | 2014-07-22 22:47:39 +0000 | [diff] [blame] | 40 | |
NAKAMURA Takumi | 87e0880 | 2013-12-07 11:21:42 +0000 | [diff] [blame] | 41 | EHFrameRelatedSections(SID EH, SID T, SID Ex) |
Juergen Ributzka | 7608dc0 | 2014-03-21 20:28:42 +0000 | [diff] [blame] | 42 | : EHFrameSID(EH), TextSID(T), ExceptTabSID(Ex) {} |
Andrew Kaylor | 7bb1344 | 2013-10-11 21:25:48 +0000 | [diff] [blame] | 43 | SID EHFrameSID; |
| 44 | SID TextSID; |
| 45 | SID ExceptTabSID; |
| 46 | }; |
| 47 | |
| 48 | // When a module is loaded we save the SectionID of the EH frame section |
| 49 | // in a table until we receive a request to register all unregistered |
| 50 | // EH frame sections with the memory manager. |
| 51 | SmallVector<EHFrameRelatedSections, 2> UnregisteredEHFrameSections; |
Juergen Ributzka | 7608dc0 | 2014-03-21 20:28:42 +0000 | [diff] [blame] | 52 | |
Eli Bendersky | 058d647 | 2012-01-22 07:05:02 +0000 | [diff] [blame] | 53 | RuntimeDyldMachO(RTDyldMemoryManager *mm) : RuntimeDyldImpl(mm) {} |
| 54 | |
Lang Hames | 25d9309 | 2014-08-08 23:12:22 +0000 | [diff] [blame] | 55 | /// This convenience method uses memcpy to extract a contiguous addend (the |
| 56 | /// addend size and offset are taken from the corresponding fields of the RE). |
| 57 | int64_t memcpyAddend(const RelocationEntry &RE) const; |
| 58 | |
| 59 | /// Given a relocation_iterator for a non-scattered relocation, construct a |
| 60 | /// RelocationEntry and fill in the common fields. The 'Addend' field is *not* |
| 61 | /// filled in, since immediate encodings are highly target/opcode specific. |
| 62 | /// For targets/opcodes with simple, contiguous immediates (e.g. X86) the |
| 63 | /// memcpyAddend method can be used to read the immediate. |
| 64 | RelocationEntry getRelocationEntry(unsigned SectionID, ObjectImage &ObjImg, |
| 65 | const relocation_iterator &RI) const { |
| 66 | const MachOObjectFile &Obj = |
| 67 | static_cast<const MachOObjectFile &>(*ObjImg.getObjectFile()); |
| 68 | MachO::any_relocation_info RelInfo = |
| 69 | Obj.getRelocation(RI->getRawDataRefImpl()); |
| 70 | |
| 71 | bool IsPCRel = Obj.getAnyRelocationPCRel(RelInfo); |
| 72 | unsigned Size = Obj.getAnyRelocationLength(RelInfo); |
| 73 | uint64_t Offset; |
| 74 | RI->getOffset(Offset); |
| 75 | MachO::RelocationInfoType RelType = |
| 76 | static_cast<MachO::RelocationInfoType>(Obj.getAnyRelocationType(RelInfo)); |
| 77 | |
| 78 | return RelocationEntry(SectionID, Offset, RelType, 0, IsPCRel, Size); |
| 79 | } |
Lang Hames | 951b235 | 2014-03-08 18:45:12 +0000 | [diff] [blame] | 80 | |
Lang Hames | a521688 | 2014-07-17 18:54:50 +0000 | [diff] [blame] | 81 | /// Construct a RelocationValueRef representing the relocation target. |
| 82 | /// For Symbols in known sections, this will return a RelocationValueRef |
| 83 | /// representing a (SectionID, Offset) pair. |
| 84 | /// For Symbols whose section is not known, this will return a |
| 85 | /// (SymbolName, Offset) pair, where the Offset is taken from the instruction |
| 86 | /// immediate (held in RE.Addend). |
| 87 | /// In both cases the Addend field is *NOT* fixed up to be PC-relative. That |
| 88 | /// should be done by the caller where appropriate by calling makePCRel on |
| 89 | /// the RelocationValueRef. |
| 90 | RelocationValueRef getRelocationValueRef(ObjectImage &ObjImg, |
| 91 | const relocation_iterator &RI, |
| 92 | const RelocationEntry &RE, |
| 93 | ObjSectionToIDMap &ObjSectionToID, |
| 94 | const SymbolTableMap &Symbols); |
| 95 | |
| 96 | /// Make the RelocationValueRef addend PC-relative. |
| 97 | void makeValueAddendPCRel(RelocationValueRef &Value, ObjectImage &ObjImg, |
Lang Hames | 1316365 | 2014-07-30 03:35:05 +0000 | [diff] [blame] | 98 | const relocation_iterator &RI, |
| 99 | unsigned OffsetToNextPC); |
Lang Hames | a521688 | 2014-07-17 18:54:50 +0000 | [diff] [blame] | 100 | |
| 101 | /// Dump information about the relocation entry (RE) and resolved value. |
| 102 | void dumpRelocationToResolve(const RelocationEntry &RE, uint64_t Value) const; |
| 103 | |
Lang Hames | 6f1048f | 2014-09-11 19:21:14 +0000 | [diff] [blame^] | 104 | // Return a section iterator for the section containing the given address. |
| 105 | static section_iterator getSectionByAddress(const MachOObjectFile &Obj, |
| 106 | uint64_t Addr); |
| 107 | |
| 108 | |
| 109 | // Populate __pointers section. |
| 110 | void populateIndirectSymbolPointersSection(MachOObjectFile &Obj, |
| 111 | const SectionRef &PTSection, |
| 112 | unsigned PTSectionID); |
| 113 | |
Lang Hames | a521688 | 2014-07-17 18:54:50 +0000 | [diff] [blame] | 114 | public: |
| 115 | /// Create an ObjectImage from the given ObjectBuffer. |
David Blaikie | ed9709d | 2014-09-03 19:48:09 +0000 | [diff] [blame] | 116 | static std::unique_ptr<ObjectImage> |
| 117 | createObjectImage(std::unique_ptr<ObjectBuffer> InputBuffer) { |
| 118 | return llvm::make_unique<ObjectImageCommon>(std::move(InputBuffer)); |
Lang Hames | 84bc818 | 2014-07-15 19:35:22 +0000 | [diff] [blame] | 119 | } |
| 120 | |
Lang Hames | a521688 | 2014-07-17 18:54:50 +0000 | [diff] [blame] | 121 | /// Create an ObjectImage from the given ObjectFile. |
Juergen Ributzka | 7608dc0 | 2014-03-21 20:28:42 +0000 | [diff] [blame] | 122 | static ObjectImage * |
Lang Hames | 84bc818 | 2014-07-15 19:35:22 +0000 | [diff] [blame] | 123 | createObjectImageFromFile(std::unique_ptr<object::ObjectFile> InputObject) { |
| 124 | return new ObjectImageCommon(std::move(InputObject)); |
| 125 | } |
Lang Hames | a521688 | 2014-07-17 18:54:50 +0000 | [diff] [blame] | 126 | |
| 127 | /// Create a RuntimeDyldMachO instance for the given target architecture. |
| 128 | static std::unique_ptr<RuntimeDyldMachO> create(Triple::ArchType Arch, |
| 129 | RTDyldMemoryManager *mm); |
| 130 | |
Lang Hames | a521688 | 2014-07-17 18:54:50 +0000 | [diff] [blame] | 131 | SectionEntry &getSection(unsigned SectionID) { return Sections[SectionID]; } |
| 132 | |
| 133 | bool isCompatibleFormat(const ObjectBuffer *Buffer) const override; |
| 134 | bool isCompatibleFile(const object::ObjectFile *Obj) const override; |
Lang Hames | a521688 | 2014-07-17 18:54:50 +0000 | [diff] [blame] | 135 | }; |
| 136 | |
| 137 | /// RuntimeDyldMachOTarget - Templated base class for generic MachO linker |
| 138 | /// algorithms and data structures. |
| 139 | /// |
| 140 | /// Concrete, target specific sub-classes can be accessed via the impl() |
| 141 | /// methods. (i.e. the RuntimeDyldMachO hierarchy uses the Curiously |
| 142 | /// Recurring Template Idiom). Concrete subclasses for each target |
| 143 | /// can be found in ./Targets. |
| 144 | template <typename Impl> |
| 145 | class RuntimeDyldMachOCRTPBase : public RuntimeDyldMachO { |
| 146 | private: |
| 147 | Impl &impl() { return static_cast<Impl &>(*this); } |
Lang Hames | 3fda7d8 | 2014-07-19 00:19:17 +0000 | [diff] [blame] | 148 | const Impl &impl() const { return static_cast<const Impl &>(*this); } |
| 149 | |
Lang Hames | eb195f0 | 2014-09-04 04:53:03 +0000 | [diff] [blame] | 150 | unsigned char *processFDE(unsigned char *P, int64_t DeltaForText, |
| 151 | int64_t DeltaForEH); |
| 152 | |
Lang Hames | a521688 | 2014-07-17 18:54:50 +0000 | [diff] [blame] | 153 | public: |
| 154 | RuntimeDyldMachOCRTPBase(RTDyldMemoryManager *mm) : RuntimeDyldMachO(mm) {} |
| 155 | |
Benjamin Kramer | 8c90fd7 | 2014-09-03 11:41:21 +0000 | [diff] [blame] | 156 | void finalizeLoad(ObjectImage &ObjImg, |
Lang Hames | eb195f0 | 2014-09-04 04:53:03 +0000 | [diff] [blame] | 157 | ObjSectionToIDMap &SectionMap) override; |
| 158 | void registerEHFrames() override; |
Eli Bendersky | 058d647 | 2012-01-22 07:05:02 +0000 | [diff] [blame] | 159 | }; |
| 160 | |
| 161 | } // end namespace llvm |
| 162 | |
Lang Hames | a521688 | 2014-07-17 18:54:50 +0000 | [diff] [blame] | 163 | #undef DEBUG_TYPE |
| 164 | |
Eli Bendersky | 058d647 | 2012-01-22 07:05:02 +0000 | [diff] [blame] | 165 | #endif |