blob: 6911f2f07aaa4395d9be3b2737ad3577d20359b5 [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"
Eli Bendersky058d6472012-01-22 07:05:02 +000019#include "llvm/ADT/IndexedMap.h"
Rafael Espindola6e040c02013-04-26 20:07:33 +000020#include "llvm/Object/MachO.h"
Eli Bendersky058d6472012-01-22 07:05:02 +000021#include "llvm/Support/Format.h"
Eli Bendersky058d6472012-01-22 07:05:02 +000022
23using namespace llvm;
24using namespace llvm::object;
25
Eli Bendersky058d6472012-01-22 07:05:02 +000026namespace llvm {
27class RuntimeDyldMachO : public RuntimeDyldImpl {
Lang Hamesf35553e2014-05-09 00:11:18 +000028private:
Eli Bendersky058d6472012-01-22 07:05:02 +000029
Lang Hamesf35553e2014-05-09 00:11:18 +000030 /// 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 Kaylor7bb13442013-10-11 21:25:48 +000048
Lang Hames36072da2014-05-12 21:39:59 +000049 // Populate stubs in __jump_table section.
50 void populateJumpTable(MachOObjectFile &Obj, const SectionRef &JTSection,
51 unsigned JTSectionID);
52
53 // Populate __pointers section.
54 void populatePointersSection(MachOObjectFile &Obj, const SectionRef &PTSection,
55 unsigned PTSectionID);
56
Craig Topperb51ff602014-03-08 07:51:20 +000057 unsigned getMaxStubSize() override {
Andrew Kaylor2ba21c52013-10-15 21:32:56 +000058 if (Arch == Triple::arm || Arch == Triple::thumb)
59 return 8; // 32-bit instruction and 32-bit address
60 else if (Arch == Triple::x86_64)
61 return 8; // GOT entry
62 else
63 return 0;
64 }
65
Juergen Ributzka7608dc02014-03-21 20:28:42 +000066 unsigned getStubAlignment() override { return 1; }
Andrew Kaylor2ba21c52013-10-15 21:32:56 +000067
Lang Hames36072da2014-05-12 21:39:59 +000068 relocation_iterator processSECTDIFFRelocation(
69 unsigned SectionID,
70 relocation_iterator RelI,
71 ObjectImage &ObjImg,
72 ObjSectionToIDMap &ObjSectionToID);
73
Lang Hames7f9fc2b2014-05-22 22:30:13 +000074 relocation_iterator processI386ScatteredVANILLA(
75 unsigned SectionID,
76 relocation_iterator RelI,
77 ObjectImage &ObjImg,
78 ObjSectionToIDMap &ObjSectionToID);
79
Andrew Kaylor7bb13442013-10-11 21:25:48 +000080 struct EHFrameRelatedSections {
Juergen Ributzka7608dc02014-03-21 20:28:42 +000081 EHFrameRelatedSections()
82 : EHFrameSID(RTDYLD_INVALID_SECTION_ID),
83 TextSID(RTDYLD_INVALID_SECTION_ID),
84 ExceptTabSID(RTDYLD_INVALID_SECTION_ID) {}
NAKAMURA Takumi87e08802013-12-07 11:21:42 +000085 EHFrameRelatedSections(SID EH, SID T, SID Ex)
Juergen Ributzka7608dc02014-03-21 20:28:42 +000086 : EHFrameSID(EH), TextSID(T), ExceptTabSID(Ex) {}
Andrew Kaylor7bb13442013-10-11 21:25:48 +000087 SID EHFrameSID;
88 SID TextSID;
89 SID ExceptTabSID;
90 };
91
92 // When a module is loaded we save the SectionID of the EH frame section
93 // in a table until we receive a request to register all unregistered
94 // EH frame sections with the memory manager.
95 SmallVector<EHFrameRelatedSections, 2> UnregisteredEHFrameSections;
Juergen Ributzka7608dc02014-03-21 20:28:42 +000096
Eli Bendersky058d6472012-01-22 07:05:02 +000097public:
98 RuntimeDyldMachO(RTDyldMemoryManager *mm) : RuntimeDyldImpl(mm) {}
99
Craig Topperb51ff602014-03-08 07:51:20 +0000100 void resolveRelocation(const RelocationEntry &RE, uint64_t Value) override;
Juergen Ributzka046709f2014-03-21 07:26:41 +0000101 relocation_iterator
102 processRelocationRef(unsigned SectionID, relocation_iterator RelI,
103 ObjectImage &Obj, ObjSectionToIDMap &ObjSectionToID,
104 const SymbolTableMap &Symbols, StubMap &Stubs) override;
Craig Topperb51ff602014-03-08 07:51:20 +0000105 bool isCompatibleFormat(const ObjectBuffer *Buffer) const override;
106 bool isCompatibleFile(const object::ObjectFile *Obj) const override;
107 void registerEHFrames() override;
Lang Hames36072da2014-05-12 21:39:59 +0000108 void finalizeLoad(ObjectImage &ObjImg,
109 ObjSectionToIDMap &SectionMap) override;
Lang Hames951b2352014-03-08 18:45:12 +0000110
Lang Hames951b2352014-03-08 18:45:12 +0000111 static ObjectImage *createObjectImage(ObjectBuffer *InputBuffer) {
112 return new ObjectImageCommon(InputBuffer);
113 }
114
Juergen Ributzka7608dc02014-03-21 20:28:42 +0000115 static ObjectImage *
David Blaikie7a1e7752014-04-29 21:52:46 +0000116 createObjectImageFromFile(std::unique_ptr<object::ObjectFile> InputObject) {
117 return new ObjectImageCommon(std::move(InputObject));
Lang Hames951b2352014-03-08 18:45:12 +0000118 }
Eli Bendersky058d6472012-01-22 07:05:02 +0000119};
120
121} // end namespace llvm
122
123#endif