1 //===-- RuntimeDyldMachO.h - Run-time dynamic linker for MC-JIT ---*- C++ -*-=//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // MachO support for MC-JIT runtime dynamic linker.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_LIB_EXECUTIONENGINE_RUNTIMEDYLD_RUNTIMEDYLDMACHO_H
15 #define LLVM_LIB_EXECUTIONENGINE_RUNTIMEDYLD_RUNTIMEDYLDMACHO_H
17 #include "ObjectImageCommon.h"
18 #include "RuntimeDyldImpl.h"
19 #include "llvm/Object/MachO.h"
20 #include "llvm/Support/Format.h"
22 #define DEBUG_TYPE "dyld"
25 using namespace llvm::object;
28 class RuntimeDyldMachO : public RuntimeDyldImpl {
30 struct SectionOffsetPair {
35 struct EHFrameRelatedSections {
36 EHFrameRelatedSections()
37 : EHFrameSID(RTDYLD_INVALID_SECTION_ID),
38 TextSID(RTDYLD_INVALID_SECTION_ID),
39 ExceptTabSID(RTDYLD_INVALID_SECTION_ID) {}
41 EHFrameRelatedSections(SID EH, SID T, SID Ex)
42 : EHFrameSID(EH), TextSID(T), ExceptTabSID(Ex) {}
48 // When a module is loaded we save the SectionID of the EH frame section
49 // in a table until we receive a request to register all unregistered
50 // EH frame sections with the memory manager.
51 SmallVector<EHFrameRelatedSections, 2> UnregisteredEHFrameSections;
53 RuntimeDyldMachO(RTDyldMemoryManager *mm) : RuntimeDyldImpl(mm) {}
55 /// This convenience method uses memcpy to extract a contiguous addend (the
56 /// addend size and offset are taken from the corresponding fields of the RE).
57 int64_t memcpyAddend(const RelocationEntry &RE) const;
59 /// Given a relocation_iterator for a non-scattered relocation, construct a
60 /// RelocationEntry and fill in the common fields. The 'Addend' field is *not*
61 /// filled in, since immediate encodings are highly target/opcode specific.
62 /// For targets/opcodes with simple, contiguous immediates (e.g. X86) the
63 /// memcpyAddend method can be used to read the immediate.
64 RelocationEntry getRelocationEntry(unsigned SectionID, ObjectImage &ObjImg,
65 const relocation_iterator &RI) const {
66 const MachOObjectFile &Obj =
67 static_cast<const MachOObjectFile &>(*ObjImg.getObjectFile());
68 MachO::any_relocation_info RelInfo =
69 Obj.getRelocation(RI->getRawDataRefImpl());
71 bool IsPCRel = Obj.getAnyRelocationPCRel(RelInfo);
72 unsigned Size = Obj.getAnyRelocationLength(RelInfo);
74 RI->getOffset(Offset);
75 MachO::RelocationInfoType RelType =
76 static_cast<MachO::RelocationInfoType>(Obj.getAnyRelocationType(RelInfo));
78 return RelocationEntry(SectionID, Offset, RelType, 0, IsPCRel, Size);
81 /// 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.
90 RelocationValueRef getRelocationValueRef(ObjectImage &ObjImg,
91 const relocation_iterator &RI,
92 const RelocationEntry &RE,
93 ObjSectionToIDMap &ObjSectionToID,
94 const SymbolTableMap &Symbols);
96 /// Make the RelocationValueRef addend PC-relative.
97 void makeValueAddendPCRel(RelocationValueRef &Value, ObjectImage &ObjImg,
98 const relocation_iterator &RI,
99 unsigned OffsetToNextPC);
101 /// Dump information about the relocation entry (RE) and resolved value.
102 void dumpRelocationToResolve(const RelocationEntry &RE, uint64_t Value) const;
105 /// Create an ObjectImage from the given ObjectBuffer.
106 static ObjectImage *createObjectImage(ObjectBuffer *InputBuffer) {
107 return new ObjectImageCommon(InputBuffer);
110 /// Create an ObjectImage from the given ObjectFile.
112 createObjectImageFromFile(std::unique_ptr<object::ObjectFile> InputObject) {
113 return new ObjectImageCommon(std::move(InputObject));
116 /// Create a RuntimeDyldMachO instance for the given target architecture.
117 static std::unique_ptr<RuntimeDyldMachO> create(Triple::ArchType Arch,
118 RTDyldMemoryManager *mm);
120 /// Write the least significant 'Size' bytes in 'Value' out at the address
121 /// pointed to by Addr. Check for overflow.
122 bool writeBytesUnaligned(uint8_t *Dst, uint64_t Value, unsigned Size);
124 SectionEntry &getSection(unsigned SectionID) { return Sections[SectionID]; }
126 bool isCompatibleFormat(const ObjectBuffer *Buffer) const override;
127 bool isCompatibleFile(const object::ObjectFile *Obj) const override;
128 void registerEHFrames() override;
131 /// RuntimeDyldMachOTarget - Templated base class for generic MachO linker
132 /// algorithms and data structures.
134 /// Concrete, target specific sub-classes can be accessed via the impl()
135 /// methods. (i.e. the RuntimeDyldMachO hierarchy uses the Curiously
136 /// Recurring Template Idiom). Concrete subclasses for each target
137 /// can be found in ./Targets.
138 template <typename Impl>
139 class RuntimeDyldMachOCRTPBase : public RuntimeDyldMachO {
141 Impl &impl() { return static_cast<Impl &>(*this); }
142 const Impl &impl() const { return static_cast<const Impl &>(*this); }
145 RuntimeDyldMachOCRTPBase(RTDyldMemoryManager *mm) : RuntimeDyldMachO(mm) {}
147 void finalizeLoad(ObjectImage &ObjImg, ObjSectionToIDMap &SectionMap) {
148 unsigned EHFrameSID = RTDYLD_INVALID_SECTION_ID;
149 unsigned TextSID = RTDYLD_INVALID_SECTION_ID;
150 unsigned ExceptTabSID = RTDYLD_INVALID_SECTION_ID;
151 ObjSectionToIDMap::iterator i, e;
153 for (i = SectionMap.begin(), e = SectionMap.end(); i != e; ++i) {
154 const SectionRef &Section = i->first;
156 Section.getName(Name);
157 if (Name == "__eh_frame")
158 EHFrameSID = i->second;
159 else if (Name == "__text")
161 else if (Name == "__gcc_except_tab")
162 ExceptTabSID = i->second;
164 impl().finalizeSection(ObjImg, i->second, Section);
166 UnregisteredEHFrameSections.push_back(
167 EHFrameRelatedSections(EHFrameSID, TextSID, ExceptTabSID));
171 } // end namespace llvm