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 | |
Chandler Carruth | 802d755 | 2012-12-04 07:12:27 +0000 | [diff] [blame] | 17 | #include "RuntimeDyldImpl.h" |
Rafael Espindola | 6e040c0 | 2013-04-26 20:07:33 +0000 | [diff] [blame] | 18 | #include "llvm/Object/MachO.h" |
Eli Bendersky | 058d647 | 2012-01-22 07:05:02 +0000 | [diff] [blame] | 19 | #include "llvm/Support/Format.h" |
Eli Bendersky | 058d647 | 2012-01-22 07:05:02 +0000 | [diff] [blame] | 20 | |
Lang Hames | a521688 | 2014-07-17 18:54:50 +0000 | [diff] [blame] | 21 | #define DEBUG_TYPE "dyld" |
| 22 | |
Eli Bendersky | 058d647 | 2012-01-22 07:05:02 +0000 | [diff] [blame] | 23 | using namespace llvm; |
| 24 | using namespace llvm::object; |
| 25 | |
Eli Bendersky | 058d647 | 2012-01-22 07:05:02 +0000 | [diff] [blame] | 26 | namespace llvm { |
| 27 | class RuntimeDyldMachO : public RuntimeDyldImpl { |
Lang Hames | a521688 | 2014-07-17 18:54:50 +0000 | [diff] [blame] | 28 | protected: |
| 29 | struct SectionOffsetPair { |
| 30 | unsigned SectionID; |
| 31 | uint64_t Offset; |
| 32 | }; |
Lang Hames | 7f9fc2b | 2014-05-22 22:30:13 +0000 | [diff] [blame] | 33 | |
Andrew Kaylor | 7bb1344 | 2013-10-11 21:25:48 +0000 | [diff] [blame] | 34 | struct EHFrameRelatedSections { |
Juergen Ributzka | 7608dc0 | 2014-03-21 20:28:42 +0000 | [diff] [blame] | 35 | EHFrameRelatedSections() |
| 36 | : EHFrameSID(RTDYLD_INVALID_SECTION_ID), |
| 37 | TextSID(RTDYLD_INVALID_SECTION_ID), |
| 38 | ExceptTabSID(RTDYLD_INVALID_SECTION_ID) {} |
Lang Hames | f7acddd | 2014-07-22 22:47:39 +0000 | [diff] [blame] | 39 | |
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 | |
Lang Hames | 633fe14 | 2015-03-30 03:37:06 +0000 | [diff] [blame] | 52 | RuntimeDyldMachO(RuntimeDyld::MemoryManager &MemMgr, |
Lang Hames | ad4a911 | 2016-08-01 20:49:11 +0000 | [diff] [blame] | 53 | JITSymbolResolver &Resolver) |
Lang Hames | 633fe14 | 2015-03-30 03:37:06 +0000 | [diff] [blame] | 54 | : RuntimeDyldImpl(MemMgr, Resolver) {} |
Eli Bendersky | 058d647 | 2012-01-22 07:05:02 +0000 | [diff] [blame] | 55 | |
Lang Hames | 25d9309 | 2014-08-08 23:12:22 +0000 | [diff] [blame] | 56 | /// This convenience method uses memcpy to extract a contiguous addend (the |
| 57 | /// addend size and offset are taken from the corresponding fields of the RE). |
| 58 | int64_t memcpyAddend(const RelocationEntry &RE) const; |
| 59 | |
| 60 | /// Given a relocation_iterator for a non-scattered relocation, construct a |
| 61 | /// RelocationEntry and fill in the common fields. The 'Addend' field is *not* |
| 62 | /// filled in, since immediate encodings are highly target/opcode specific. |
| 63 | /// For targets/opcodes with simple, contiguous immediates (e.g. X86) the |
| 64 | /// memcpyAddend method can be used to read the immediate. |
Lang Hames | b5c7b1f | 2014-11-26 16:54:40 +0000 | [diff] [blame] | 65 | RelocationEntry getRelocationEntry(unsigned SectionID, |
| 66 | const ObjectFile &BaseTObj, |
Lang Hames | 25d9309 | 2014-08-08 23:12:22 +0000 | [diff] [blame] | 67 | const relocation_iterator &RI) const { |
| 68 | const MachOObjectFile &Obj = |
Lang Hames | b5c7b1f | 2014-11-26 16:54:40 +0000 | [diff] [blame] | 69 | static_cast<const MachOObjectFile &>(BaseTObj); |
Lang Hames | 25d9309 | 2014-08-08 23:12:22 +0000 | [diff] [blame] | 70 | MachO::any_relocation_info RelInfo = |
| 71 | Obj.getRelocation(RI->getRawDataRefImpl()); |
| 72 | |
| 73 | bool IsPCRel = Obj.getAnyRelocationPCRel(RelInfo); |
| 74 | unsigned Size = Obj.getAnyRelocationLength(RelInfo); |
Rafael Espindola | 96d071c | 2015-06-29 23:29:12 +0000 | [diff] [blame] | 75 | uint64_t Offset = RI->getOffset(); |
Lang Hames | 25d9309 | 2014-08-08 23:12:22 +0000 | [diff] [blame] | 76 | MachO::RelocationInfoType RelType = |
| 77 | static_cast<MachO::RelocationInfoType>(Obj.getAnyRelocationType(RelInfo)); |
| 78 | |
| 79 | return RelocationEntry(SectionID, Offset, RelType, 0, IsPCRel, Size); |
| 80 | } |
Lang Hames | 951b235 | 2014-03-08 18:45:12 +0000 | [diff] [blame] | 81 | |
Lang Hames | a8183e5 | 2015-07-24 17:40:04 +0000 | [diff] [blame] | 82 | /// Process a scattered vanilla relocation. |
Lang Hames | 8959531 | 2016-04-27 20:24:48 +0000 | [diff] [blame] | 83 | Expected<relocation_iterator> |
| 84 | processScatteredVANILLA(unsigned SectionID, relocation_iterator RelI, |
| 85 | const ObjectFile &BaseObjT, |
| 86 | RuntimeDyldMachO::ObjSectionToIDMap &ObjSectionToID); |
Lang Hames | a8183e5 | 2015-07-24 17:40:04 +0000 | [diff] [blame] | 87 | |
Lang Hames | a521688 | 2014-07-17 18:54:50 +0000 | [diff] [blame] | 88 | /// Construct a RelocationValueRef representing the relocation target. |
| 89 | /// For Symbols in known sections, this will return a RelocationValueRef |
| 90 | /// representing a (SectionID, Offset) pair. |
| 91 | /// For Symbols whose section is not known, this will return a |
| 92 | /// (SymbolName, Offset) pair, where the Offset is taken from the instruction |
| 93 | /// immediate (held in RE.Addend). |
| 94 | /// In both cases the Addend field is *NOT* fixed up to be PC-relative. That |
| 95 | /// should be done by the caller where appropriate by calling makePCRel on |
| 96 | /// the RelocationValueRef. |
Lang Hames | 8959531 | 2016-04-27 20:24:48 +0000 | [diff] [blame] | 97 | Expected<RelocationValueRef> |
| 98 | getRelocationValueRef(const ObjectFile &BaseTObj, |
| 99 | const relocation_iterator &RI, |
| 100 | const RelocationEntry &RE, |
| 101 | ObjSectionToIDMap &ObjSectionToID); |
Lang Hames | a521688 | 2014-07-17 18:54:50 +0000 | [diff] [blame] | 102 | |
| 103 | /// Make the RelocationValueRef addend PC-relative. |
Lang Hames | b5c7b1f | 2014-11-26 16:54:40 +0000 | [diff] [blame] | 104 | void makeValueAddendPCRel(RelocationValueRef &Value, |
Lang Hames | 1316365 | 2014-07-30 03:35:05 +0000 | [diff] [blame] | 105 | const relocation_iterator &RI, |
| 106 | unsigned OffsetToNextPC); |
Lang Hames | a521688 | 2014-07-17 18:54:50 +0000 | [diff] [blame] | 107 | |
| 108 | /// Dump information about the relocation entry (RE) and resolved value. |
| 109 | void dumpRelocationToResolve(const RelocationEntry &RE, uint64_t Value) const; |
| 110 | |
Lang Hames | 6f1048f | 2014-09-11 19:21:14 +0000 | [diff] [blame] | 111 | // Return a section iterator for the section containing the given address. |
| 112 | static section_iterator getSectionByAddress(const MachOObjectFile &Obj, |
| 113 | uint64_t Addr); |
| 114 | |
| 115 | |
| 116 | // Populate __pointers section. |
Lang Hames | 8959531 | 2016-04-27 20:24:48 +0000 | [diff] [blame] | 117 | Error populateIndirectSymbolPointersSection(const MachOObjectFile &Obj, |
| 118 | const SectionRef &PTSection, |
| 119 | unsigned PTSectionID); |
Lang Hames | 6f1048f | 2014-09-11 19:21:14 +0000 | [diff] [blame] | 120 | |
Lang Hames | a521688 | 2014-07-17 18:54:50 +0000 | [diff] [blame] | 121 | public: |
Lang Hames | a521688 | 2014-07-17 18:54:50 +0000 | [diff] [blame] | 122 | |
| 123 | /// Create a RuntimeDyldMachO instance for the given target architecture. |
Lang Hames | 633fe14 | 2015-03-30 03:37:06 +0000 | [diff] [blame] | 124 | static std::unique_ptr<RuntimeDyldMachO> |
| 125 | create(Triple::ArchType Arch, |
| 126 | RuntimeDyld::MemoryManager &MemMgr, |
Lang Hames | ad4a911 | 2016-08-01 20:49:11 +0000 | [diff] [blame] | 127 | JITSymbolResolver &Resolver); |
Lang Hames | a521688 | 2014-07-17 18:54:50 +0000 | [diff] [blame] | 128 | |
Lang Hames | b5c7b1f | 2014-11-26 16:54:40 +0000 | [diff] [blame] | 129 | std::unique_ptr<RuntimeDyld::LoadedObjectInfo> |
| 130 | loadObject(const object::ObjectFile &O) override; |
| 131 | |
Lang Hames | a521688 | 2014-07-17 18:54:50 +0000 | [diff] [blame] | 132 | SectionEntry &getSection(unsigned SectionID) { return Sections[SectionID]; } |
| 133 | |
Lang Hames | b5c7b1f | 2014-11-26 16:54:40 +0000 | [diff] [blame] | 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 | |
Sanjoy Das | 277776a | 2015-11-23 21:47:41 +0000 | [diff] [blame] | 150 | unsigned char *processFDE(uint8_t *P, int64_t DeltaForText, |
Lang Hames | eb195f0 | 2014-09-04 04:53:03 +0000 | [diff] [blame] | 151 | int64_t DeltaForEH); |
| 152 | |
Lang Hames | a521688 | 2014-07-17 18:54:50 +0000 | [diff] [blame] | 153 | public: |
Lang Hames | 633fe14 | 2015-03-30 03:37:06 +0000 | [diff] [blame] | 154 | RuntimeDyldMachOCRTPBase(RuntimeDyld::MemoryManager &MemMgr, |
Lang Hames | ad4a911 | 2016-08-01 20:49:11 +0000 | [diff] [blame] | 155 | JITSymbolResolver &Resolver) |
Lang Hames | 633fe14 | 2015-03-30 03:37:06 +0000 | [diff] [blame] | 156 | : RuntimeDyldMachO(MemMgr, Resolver) {} |
Lang Hames | a521688 | 2014-07-17 18:54:50 +0000 | [diff] [blame] | 157 | |
Lang Hames | 8959531 | 2016-04-27 20:24:48 +0000 | [diff] [blame] | 158 | Error finalizeLoad(const ObjectFile &Obj, |
| 159 | ObjSectionToIDMap &SectionMap) override; |
Lang Hames | eb195f0 | 2014-09-04 04:53:03 +0000 | [diff] [blame] | 160 | void registerEHFrames() override; |
Eli Bendersky | 058d647 | 2012-01-22 07:05:02 +0000 | [diff] [blame] | 161 | }; |
| 162 | |
| 163 | } // end namespace llvm |
| 164 | |
Lang Hames | a521688 | 2014-07-17 18:54:50 +0000 | [diff] [blame] | 165 | #undef DEBUG_TYPE |
| 166 | |
Eli Bendersky | 058d647 | 2012-01-22 07:05:02 +0000 | [diff] [blame] | 167 | #endif |