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" |
Eli Bendersky | 058d647 | 2012-01-22 07:05:02 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/IndexedMap.h" |
Rafael Espindola | 6e040c0 | 2013-04-26 20:07:33 +0000 | [diff] [blame] | 20 | #include "llvm/Object/MachO.h" |
Eli Bendersky | 058d647 | 2012-01-22 07:05:02 +0000 | [diff] [blame] | 21 | #include "llvm/Support/Format.h" |
Eli Bendersky | 058d647 | 2012-01-22 07:05:02 +0000 | [diff] [blame] | 22 | |
| 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 | f35553e | 2014-05-09 00:11:18 +0000 | [diff] [blame^] | 28 | private: |
Eli Bendersky | 058d647 | 2012-01-22 07:05:02 +0000 | [diff] [blame] | 29 | |
Lang Hames | f35553e | 2014-05-09 00:11:18 +0000 | [diff] [blame^] | 30 | /// Write the least significant 'Size' bytes in 'Value' out at the address |
| 31 | /// pointed to by Addr. Check for overflow. |
| 32 | bool applyRelocationValue(uint8_t *Addr, uint64_t Value, unsigned Size) { |
| 33 | for (unsigned i = 0; i < Size; ++i) { |
| 34 | *Addr++ = (uint8_t)Value; |
| 35 | Value >>= 8; |
| 36 | } |
| 37 | |
| 38 | if (Value) // Catch overflow |
| 39 | return Error("Relocation out of range."); |
| 40 | |
| 41 | return false; |
| 42 | } |
| 43 | |
| 44 | bool resolveI386Relocation(const RelocationEntry &RE, uint64_t Value); |
| 45 | bool resolveX86_64Relocation(const RelocationEntry &RE, uint64_t Value); |
| 46 | bool resolveARMRelocation(const RelocationEntry &RE, uint64_t Value); |
| 47 | bool resolveARM64Relocation(const RelocationEntry &RE, uint64_t Value); |
Andrew Kaylor | 7bb1344 | 2013-10-11 21:25:48 +0000 | [diff] [blame] | 48 | |
Craig Topper | b51ff60 | 2014-03-08 07:51:20 +0000 | [diff] [blame] | 49 | unsigned getMaxStubSize() override { |
Andrew Kaylor | 2ba21c5 | 2013-10-15 21:32:56 +0000 | [diff] [blame] | 50 | if (Arch == Triple::arm || Arch == Triple::thumb) |
| 51 | return 8; // 32-bit instruction and 32-bit address |
| 52 | else if (Arch == Triple::x86_64) |
| 53 | return 8; // GOT entry |
| 54 | else |
| 55 | return 0; |
| 56 | } |
| 57 | |
Juergen Ributzka | 7608dc0 | 2014-03-21 20:28:42 +0000 | [diff] [blame] | 58 | unsigned getStubAlignment() override { return 1; } |
Andrew Kaylor | 2ba21c5 | 2013-10-15 21:32:56 +0000 | [diff] [blame] | 59 | |
Andrew Kaylor | 7bb1344 | 2013-10-11 21:25:48 +0000 | [diff] [blame] | 60 | struct EHFrameRelatedSections { |
Juergen Ributzka | 7608dc0 | 2014-03-21 20:28:42 +0000 | [diff] [blame] | 61 | EHFrameRelatedSections() |
| 62 | : EHFrameSID(RTDYLD_INVALID_SECTION_ID), |
| 63 | TextSID(RTDYLD_INVALID_SECTION_ID), |
| 64 | ExceptTabSID(RTDYLD_INVALID_SECTION_ID) {} |
NAKAMURA Takumi | 87e0880 | 2013-12-07 11:21:42 +0000 | [diff] [blame] | 65 | EHFrameRelatedSections(SID EH, SID T, SID Ex) |
Juergen Ributzka | 7608dc0 | 2014-03-21 20:28:42 +0000 | [diff] [blame] | 66 | : EHFrameSID(EH), TextSID(T), ExceptTabSID(Ex) {} |
Andrew Kaylor | 7bb1344 | 2013-10-11 21:25:48 +0000 | [diff] [blame] | 67 | SID EHFrameSID; |
| 68 | SID TextSID; |
| 69 | SID ExceptTabSID; |
| 70 | }; |
| 71 | |
| 72 | // When a module is loaded we save the SectionID of the EH frame section |
| 73 | // in a table until we receive a request to register all unregistered |
| 74 | // EH frame sections with the memory manager. |
| 75 | SmallVector<EHFrameRelatedSections, 2> UnregisteredEHFrameSections; |
Juergen Ributzka | 7608dc0 | 2014-03-21 20:28:42 +0000 | [diff] [blame] | 76 | |
Eli Bendersky | 058d647 | 2012-01-22 07:05:02 +0000 | [diff] [blame] | 77 | public: |
| 78 | RuntimeDyldMachO(RTDyldMemoryManager *mm) : RuntimeDyldImpl(mm) {} |
| 79 | |
Craig Topper | b51ff60 | 2014-03-08 07:51:20 +0000 | [diff] [blame] | 80 | void resolveRelocation(const RelocationEntry &RE, uint64_t Value) override; |
Juergen Ributzka | 046709f | 2014-03-21 07:26:41 +0000 | [diff] [blame] | 81 | relocation_iterator |
| 82 | processRelocationRef(unsigned SectionID, relocation_iterator RelI, |
| 83 | ObjectImage &Obj, ObjSectionToIDMap &ObjSectionToID, |
| 84 | const SymbolTableMap &Symbols, StubMap &Stubs) override; |
Craig Topper | b51ff60 | 2014-03-08 07:51:20 +0000 | [diff] [blame] | 85 | bool isCompatibleFormat(const ObjectBuffer *Buffer) const override; |
| 86 | bool isCompatibleFile(const object::ObjectFile *Obj) const override; |
| 87 | void registerEHFrames() override; |
| 88 | void finalizeLoad(ObjSectionToIDMap &SectionMap) override; |
Lang Hames | 951b235 | 2014-03-08 18:45:12 +0000 | [diff] [blame] | 89 | |
Lang Hames | 951b235 | 2014-03-08 18:45:12 +0000 | [diff] [blame] | 90 | static ObjectImage *createObjectImage(ObjectBuffer *InputBuffer) { |
| 91 | return new ObjectImageCommon(InputBuffer); |
| 92 | } |
| 93 | |
Juergen Ributzka | 7608dc0 | 2014-03-21 20:28:42 +0000 | [diff] [blame] | 94 | static ObjectImage * |
David Blaikie | 7a1e775 | 2014-04-29 21:52:46 +0000 | [diff] [blame] | 95 | createObjectImageFromFile(std::unique_ptr<object::ObjectFile> InputObject) { |
| 96 | return new ObjectImageCommon(std::move(InputObject)); |
Lang Hames | 951b235 | 2014-03-08 18:45:12 +0000 | [diff] [blame] | 97 | } |
Eli Bendersky | 058d647 | 2012-01-22 07:05:02 +0000 | [diff] [blame] | 98 | }; |
| 99 | |
| 100 | } // end namespace llvm |
| 101 | |
| 102 | #endif |