blob: ff102ffea6a90ba6c0e8bc8b986107ede517ade4 [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
Benjamin Kramera7c40ef2014-08-13 16:26:38 +000014#ifndef LLVM_LIB_EXECUTIONENGINE_RUNTIMEDYLD_RUNTIMEDYLDMACHO_H
15#define LLVM_LIB_EXECUTIONENGINE_RUNTIMEDYLD_RUNTIMEDYLDMACHO_H
Eli Bendersky058d6472012-01-22 07:05:02 +000016
Chandler Carruth802d7552012-12-04 07:12:27 +000017#include "RuntimeDyldImpl.h"
Rafael Espindola6e040c02013-04-26 20:07:33 +000018#include "llvm/Object/MachO.h"
Eli Bendersky058d6472012-01-22 07:05:02 +000019#include "llvm/Support/Format.h"
Eli Bendersky058d6472012-01-22 07:05:02 +000020
Lang Hamesa5216882014-07-17 18:54:50 +000021#define DEBUG_TYPE "dyld"
22
Eli Bendersky058d6472012-01-22 07:05:02 +000023using namespace llvm;
24using namespace llvm::object;
25
Eli Bendersky058d6472012-01-22 07:05:02 +000026namespace llvm {
27class RuntimeDyldMachO : public RuntimeDyldImpl {
Lang Hamesa5216882014-07-17 18:54:50 +000028protected:
29 struct SectionOffsetPair {
30 unsigned SectionID;
31 uint64_t Offset;
32 };
Lang Hames7f9fc2b2014-05-22 22:30:13 +000033
Andrew Kaylor7bb13442013-10-11 21:25:48 +000034 struct EHFrameRelatedSections {
Juergen Ributzka7608dc02014-03-21 20:28:42 +000035 EHFrameRelatedSections()
36 : EHFrameSID(RTDYLD_INVALID_SECTION_ID),
37 TextSID(RTDYLD_INVALID_SECTION_ID),
38 ExceptTabSID(RTDYLD_INVALID_SECTION_ID) {}
Lang Hamesf7acddd2014-07-22 22:47:39 +000039
NAKAMURA Takumi87e08802013-12-07 11:21:42 +000040 EHFrameRelatedSections(SID EH, SID T, SID Ex)
Juergen Ributzka7608dc02014-03-21 20:28:42 +000041 : EHFrameSID(EH), TextSID(T), ExceptTabSID(Ex) {}
Andrew Kaylor7bb13442013-10-11 21:25:48 +000042 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 Ributzka7608dc02014-03-21 20:28:42 +000051
Eli Bendersky058d6472012-01-22 07:05:02 +000052 RuntimeDyldMachO(RTDyldMemoryManager *mm) : RuntimeDyldImpl(mm) {}
53
Lang Hames25d93092014-08-08 23:12:22 +000054 /// This convenience method uses memcpy to extract a contiguous addend (the
55 /// addend size and offset are taken from the corresponding fields of the RE).
56 int64_t memcpyAddend(const RelocationEntry &RE) const;
57
58 /// Given a relocation_iterator for a non-scattered relocation, construct a
59 /// RelocationEntry and fill in the common fields. The 'Addend' field is *not*
60 /// filled in, since immediate encodings are highly target/opcode specific.
61 /// For targets/opcodes with simple, contiguous immediates (e.g. X86) the
62 /// memcpyAddend method can be used to read the immediate.
Lang Hames829a19a2014-11-26 06:53:26 +000063 RelocationEntry getRelocationEntry(unsigned SectionID,
64 const ObjectFile &BaseTObj,
Lang Hames25d93092014-08-08 23:12:22 +000065 const relocation_iterator &RI) const {
66 const MachOObjectFile &Obj =
Lang Hames829a19a2014-11-26 06:53:26 +000067 static_cast<const MachOObjectFile &>(BaseTObj);
Lang Hames25d93092014-08-08 23:12:22 +000068 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 Hames951b2352014-03-08 18:45:12 +000080
Lang Hamesa5216882014-07-17 18:54:50 +000081 /// 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.
Lang Hames829a19a2014-11-26 06:53:26 +000090 RelocationValueRef getRelocationValueRef(const ObjectFile &BaseTObj,
Lang Hamesa5216882014-07-17 18:54:50 +000091 const relocation_iterator &RI,
92 const RelocationEntry &RE,
93 ObjSectionToIDMap &ObjSectionToID,
94 const SymbolTableMap &Symbols);
95
96 /// Make the RelocationValueRef addend PC-relative.
Lang Hames829a19a2014-11-26 06:53:26 +000097 void makeValueAddendPCRel(RelocationValueRef &Value,
98 const ObjectFile &BaseTObj,
Lang Hames13163652014-07-30 03:35:05 +000099 const relocation_iterator &RI,
100 unsigned OffsetToNextPC);
Lang Hamesa5216882014-07-17 18:54:50 +0000101
102 /// Dump information about the relocation entry (RE) and resolved value.
103 void dumpRelocationToResolve(const RelocationEntry &RE, uint64_t Value) const;
104
Lang Hames6f1048f2014-09-11 19:21:14 +0000105 // Return a section iterator for the section containing the given address.
106 static section_iterator getSectionByAddress(const MachOObjectFile &Obj,
107 uint64_t Addr);
108
109
110 // Populate __pointers section.
Lang Hames829a19a2014-11-26 06:53:26 +0000111 void populateIndirectSymbolPointersSection(const MachOObjectFile &Obj,
Lang Hames6f1048f2014-09-11 19:21:14 +0000112 const SectionRef &PTSection,
113 unsigned PTSectionID);
114
Lang Hamesa5216882014-07-17 18:54:50 +0000115public:
Lang Hamesa5216882014-07-17 18:54:50 +0000116
117 /// Create a RuntimeDyldMachO instance for the given target architecture.
118 static std::unique_ptr<RuntimeDyldMachO> create(Triple::ArchType Arch,
119 RTDyldMemoryManager *mm);
120
Lang Hames829a19a2014-11-26 06:53:26 +0000121 std::unique_ptr<RuntimeDyld::LoadedObjectInfo>
Evgeniy Stepanov2a783392014-11-26 12:26:03 +0000122 loadObject(const object::ObjectFile &O) override;
Lang Hames829a19a2014-11-26 06:53:26 +0000123
Lang Hamesa5216882014-07-17 18:54:50 +0000124 SectionEntry &getSection(unsigned SectionID) { return Sections[SectionID]; }
125
Lang Hames829a19a2014-11-26 06:53:26 +0000126 bool isCompatibleFile(const object::ObjectFile &Obj) const override;
Lang Hamesa5216882014-07-17 18:54:50 +0000127};
128
129/// RuntimeDyldMachOTarget - Templated base class for generic MachO linker
130/// algorithms and data structures.
131///
132/// Concrete, target specific sub-classes can be accessed via the impl()
133/// methods. (i.e. the RuntimeDyldMachO hierarchy uses the Curiously
134/// Recurring Template Idiom). Concrete subclasses for each target
135/// can be found in ./Targets.
136template <typename Impl>
137class RuntimeDyldMachOCRTPBase : public RuntimeDyldMachO {
138private:
139 Impl &impl() { return static_cast<Impl &>(*this); }
Lang Hames3fda7d82014-07-19 00:19:17 +0000140 const Impl &impl() const { return static_cast<const Impl &>(*this); }
141
Lang Hameseb195f02014-09-04 04:53:03 +0000142 unsigned char *processFDE(unsigned char *P, int64_t DeltaForText,
143 int64_t DeltaForEH);
144
Lang Hamesa5216882014-07-17 18:54:50 +0000145public:
146 RuntimeDyldMachOCRTPBase(RTDyldMemoryManager *mm) : RuntimeDyldMachO(mm) {}
147
Lang Hames829a19a2014-11-26 06:53:26 +0000148 void finalizeLoad(const ObjectFile &Obj,
Lang Hameseb195f02014-09-04 04:53:03 +0000149 ObjSectionToIDMap &SectionMap) override;
150 void registerEHFrames() override;
Eli Bendersky058d6472012-01-22 07:05:02 +0000151};
152
153} // end namespace llvm
154
Lang Hamesa5216882014-07-17 18:54:50 +0000155#undef DEBUG_TYPE
156
Eli Bendersky058d6472012-01-22 07:05:02 +0000157#endif