blob: 7025e229e5bb790ddd84c2fd4046d64663c74b94 [file] [log] [blame]
Eli Bendersky058d6472012-01-22 07:05:02 +00001//===-- 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 Hames951b2352014-03-08 18:45:12 +000017#include "ObjectImageCommon.h"
Chandler Carruth802d7552012-12-04 07:12:27 +000018#include "RuntimeDyldImpl.h"
Rafael Espindola6e040c02013-04-26 20:07:33 +000019#include "llvm/Object/MachO.h"
Eli Bendersky058d6472012-01-22 07:05:02 +000020#include "llvm/Support/Format.h"
Eli Bendersky058d6472012-01-22 07:05:02 +000021
22using namespace llvm;
23using namespace llvm::object;
24
Eli Bendersky058d6472012-01-22 07:05:02 +000025namespace llvm {
26class RuntimeDyldMachO : public RuntimeDyldImpl {
Lang Hamesf35553e2014-05-09 00:11:18 +000027private:
Eli Bendersky058d6472012-01-22 07:05:02 +000028
Lang Hamesf35553e2014-05-09 00:11:18 +000029 /// Write the least significant 'Size' bytes in 'Value' out at the address
Lang Hames8e30e4b2014-05-23 18:35:44 +000030 /// pointed to by Addr.
Lang Hamesf35553e2014-05-09 00:11:18 +000031 bool applyRelocationValue(uint8_t *Addr, uint64_t Value, unsigned Size) {
32 for (unsigned i = 0; i < Size; ++i) {
33 *Addr++ = (uint8_t)Value;
34 Value >>= 8;
35 }
36
Lang Hamesf35553e2014-05-09 00:11:18 +000037 return false;
38 }
39
40 bool resolveI386Relocation(const RelocationEntry &RE, uint64_t Value);
41 bool resolveX86_64Relocation(const RelocationEntry &RE, uint64_t Value);
42 bool resolveARMRelocation(const RelocationEntry &RE, uint64_t Value);
Tim Northover3b0846e2014-05-24 12:50:23 +000043 bool resolveAArch64Relocation(const RelocationEntry &RE, uint64_t Value);
Andrew Kaylor7bb13442013-10-11 21:25:48 +000044
Lang Hames36072da2014-05-12 21:39:59 +000045 // Populate stubs in __jump_table section.
46 void populateJumpTable(MachOObjectFile &Obj, const SectionRef &JTSection,
47 unsigned JTSectionID);
48
49 // Populate __pointers section.
50 void populatePointersSection(MachOObjectFile &Obj, const SectionRef &PTSection,
51 unsigned PTSectionID);
52
Craig Topperb51ff602014-03-08 07:51:20 +000053 unsigned getMaxStubSize() override {
Andrew Kaylor2ba21c52013-10-15 21:32:56 +000054 if (Arch == Triple::arm || Arch == Triple::thumb)
55 return 8; // 32-bit instruction and 32-bit address
56 else if (Arch == Triple::x86_64)
57 return 8; // GOT entry
Lang Hames5d102842014-07-12 00:16:47 +000058 else if (Arch == Triple::arm64)
59 return 8; // GOT entry
Andrew Kaylor2ba21c52013-10-15 21:32:56 +000060 else
61 return 0;
62 }
63
Lang Hames5d102842014-07-12 00:16:47 +000064 unsigned getStubAlignment() override {
65 if (Arch == Triple::arm || Arch == Triple::thumb)
66 return 4;
67 else if (Arch == Triple::arm64)
68 return 8;
69 else
70 return 1;
71 }
Andrew Kaylor2ba21c52013-10-15 21:32:56 +000072
Lang Hames36072da2014-05-12 21:39:59 +000073 relocation_iterator processSECTDIFFRelocation(
74 unsigned SectionID,
75 relocation_iterator RelI,
76 ObjectImage &ObjImg,
77 ObjSectionToIDMap &ObjSectionToID);
78
Lang Hames7f9fc2b2014-05-22 22:30:13 +000079 relocation_iterator processI386ScatteredVANILLA(
80 unsigned SectionID,
81 relocation_iterator RelI,
82 ObjectImage &ObjImg,
83 ObjSectionToIDMap &ObjSectionToID);
84
Andrew Kaylor7bb13442013-10-11 21:25:48 +000085 struct EHFrameRelatedSections {
Juergen Ributzka7608dc02014-03-21 20:28:42 +000086 EHFrameRelatedSections()
87 : EHFrameSID(RTDYLD_INVALID_SECTION_ID),
88 TextSID(RTDYLD_INVALID_SECTION_ID),
89 ExceptTabSID(RTDYLD_INVALID_SECTION_ID) {}
NAKAMURA Takumi87e08802013-12-07 11:21:42 +000090 EHFrameRelatedSections(SID EH, SID T, SID Ex)
Juergen Ributzka7608dc02014-03-21 20:28:42 +000091 : EHFrameSID(EH), TextSID(T), ExceptTabSID(Ex) {}
Andrew Kaylor7bb13442013-10-11 21:25:48 +000092 SID EHFrameSID;
93 SID TextSID;
94 SID ExceptTabSID;
95 };
96
97 // When a module is loaded we save the SectionID of the EH frame section
98 // in a table until we receive a request to register all unregistered
99 // EH frame sections with the memory manager.
100 SmallVector<EHFrameRelatedSections, 2> UnregisteredEHFrameSections;
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000101
Eli Bendersky058d6472012-01-22 07:05:02 +0000102public:
103 RuntimeDyldMachO(RTDyldMemoryManager *mm) : RuntimeDyldImpl(mm) {}
104
Craig Topperb51ff602014-03-08 07:51:20 +0000105 void resolveRelocation(const RelocationEntry &RE, uint64_t Value) override;
Juergen Ributzka046709f2014-03-21 07:26:41 +0000106 relocation_iterator
107 processRelocationRef(unsigned SectionID, relocation_iterator RelI,
108 ObjectImage &Obj, ObjSectionToIDMap &ObjSectionToID,
109 const SymbolTableMap &Symbols, StubMap &Stubs) override;
Craig Topperb51ff602014-03-08 07:51:20 +0000110 bool isCompatibleFormat(const ObjectBuffer *Buffer) const override;
111 bool isCompatibleFile(const object::ObjectFile *Obj) const override;
112 void registerEHFrames() override;
Lang Hames36072da2014-05-12 21:39:59 +0000113 void finalizeLoad(ObjectImage &ObjImg,
114 ObjSectionToIDMap &SectionMap) override;
Lang Hames951b2352014-03-08 18:45:12 +0000115
Lang Hames84bc8182014-07-15 19:35:22 +0000116 static ObjectImage *createObjectImage(ObjectBuffer *InputBuffer) {
117 return new ObjectImageCommon(InputBuffer);
118 }
119
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000120 static ObjectImage *
Lang Hames84bc8182014-07-15 19:35:22 +0000121 createObjectImageFromFile(std::unique_ptr<object::ObjectFile> InputObject) {
122 return new ObjectImageCommon(std::move(InputObject));
123 }
Eli Bendersky058d6472012-01-22 07:05:02 +0000124};
125
126} // end namespace llvm
127
128#endif