1 //===-- RuntimeDyldMachO.cpp - 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 // Implementation of the MC-JIT runtime dynamic linker.
12 //===----------------------------------------------------------------------===//
14 #include "RuntimeDyldMachO.h"
15 #include "llvm/ADT/STLExtras.h"
16 #include "llvm/ADT/StringRef.h"
18 #include "Targets/RuntimeDyldMachOARM.h"
19 #include "Targets/RuntimeDyldMachOAArch64.h"
20 #include "Targets/RuntimeDyldMachOI386.h"
21 #include "Targets/RuntimeDyldMachOX86_64.h"
24 using namespace llvm::object;
26 #define DEBUG_TYPE "dyld"
30 class LoadedMachOObjectInfo : public RuntimeDyld::LoadedObjectInfo {
32 LoadedMachOObjectInfo(RuntimeDyldImpl &RTDyld, unsigned BeginIdx,
34 : RuntimeDyld::LoadedObjectInfo(RTDyld, BeginIdx, EndIdx) {}
36 OwningBinary<ObjectFile>
37 getObjectForDebug(const ObjectFile &Obj) const override {
38 return OwningBinary<ObjectFile>();
46 int64_t RuntimeDyldMachO::memcpyAddend(const RelocationEntry &RE) const {
47 unsigned NumBytes = 1 << RE.Size;
48 uint8_t *Src = Sections[RE.SectionID].Address + RE.Offset;
50 return static_cast<int64_t>(readBytesUnaligned(Src, NumBytes));
53 RelocationValueRef RuntimeDyldMachO::getRelocationValueRef(
54 const ObjectFile &BaseTObj, const relocation_iterator &RI,
55 const RelocationEntry &RE, ObjSectionToIDMap &ObjSectionToID,
56 const SymbolTableMap &Symbols) {
58 const MachOObjectFile &Obj =
59 static_cast<const MachOObjectFile &>(BaseTObj);
60 MachO::any_relocation_info RelInfo =
61 Obj.getRelocation(RI->getRawDataRefImpl());
62 RelocationValueRef Value;
64 bool IsExternal = Obj.getPlainRelocationExternal(RelInfo);
66 symbol_iterator Symbol = RI->getSymbol();
68 Symbol->getName(TargetName);
69 SymbolTableMap::const_iterator SI = Symbols.find(TargetName.data());
70 if (SI != Symbols.end()) {
71 Value.SectionID = SI->second.first;
72 Value.Offset = SI->second.second + RE.Addend;
74 SI = GlobalSymbolTable.find(TargetName.data());
75 if (SI != GlobalSymbolTable.end()) {
76 Value.SectionID = SI->second.first;
77 Value.Offset = SI->second.second + RE.Addend;
79 Value.SymbolName = TargetName.data();
80 Value.Offset = RE.Addend;
84 SectionRef Sec = Obj.getRelocationSection(RelInfo);
85 bool IsCode = Sec.isText();
86 Value.SectionID = findOrEmitSection(Obj, Sec, IsCode, ObjSectionToID);
87 uint64_t Addr = Sec.getAddress();
88 Value.Offset = RE.Addend - Addr;
94 void RuntimeDyldMachO::makeValueAddendPCRel(RelocationValueRef &Value,
95 const ObjectFile &BaseTObj,
96 const relocation_iterator &RI,
97 unsigned OffsetToNextPC) {
98 const MachOObjectFile &Obj =
99 static_cast<const MachOObjectFile &>(BaseTObj);
100 MachO::any_relocation_info RelInfo =
101 Obj.getRelocation(RI->getRawDataRefImpl());
103 bool IsPCRel = Obj.getAnyRelocationPCRel(RelInfo);
105 uint64_t RelocAddr = 0;
106 RI->getAddress(RelocAddr);
107 Value.Offset += RelocAddr + OffsetToNextPC;
111 void RuntimeDyldMachO::dumpRelocationToResolve(const RelocationEntry &RE,
112 uint64_t Value) const {
113 const SectionEntry &Section = Sections[RE.SectionID];
114 uint8_t *LocalAddress = Section.Address + RE.Offset;
115 uint64_t FinalAddress = Section.LoadAddress + RE.Offset;
117 dbgs() << "resolveRelocation Section: " << RE.SectionID
118 << " LocalAddress: " << format("%p", LocalAddress)
119 << " FinalAddress: " << format("0x%016" PRIx64, FinalAddress)
120 << " Value: " << format("0x%016" PRIx64, Value) << " Addend: " << RE.Addend
121 << " isPCRel: " << RE.IsPCRel << " MachoType: " << RE.RelType
122 << " Size: " << (1 << RE.Size) << "\n";
126 RuntimeDyldMachO::getSectionByAddress(const MachOObjectFile &Obj,
128 section_iterator SI = Obj.section_begin();
129 section_iterator SE = Obj.section_end();
131 for (; SI != SE; ++SI) {
132 uint64_t SAddr = SI->getAddress();
133 uint64_t SSize = SI->getSize();
134 if ((Addr >= SAddr) && (Addr < SAddr + SSize))
142 // Populate __pointers section.
143 void RuntimeDyldMachO::populateIndirectSymbolPointersSection(
144 const MachOObjectFile &Obj,
145 const SectionRef &PTSection,
146 unsigned PTSectionID) {
147 assert(!Obj.is64Bit() &&
148 "Pointer table section not supported in 64-bit MachO.");
150 MachO::dysymtab_command DySymTabCmd = Obj.getDysymtabLoadCommand();
151 MachO::section Sec32 = Obj.getSection(PTSection.getRawDataRefImpl());
152 uint32_t PTSectionSize = Sec32.size;
153 unsigned FirstIndirectSymbol = Sec32.reserved1;
154 const unsigned PTEntrySize = 4;
155 unsigned NumPTEntries = PTSectionSize / PTEntrySize;
156 unsigned PTEntryOffset = 0;
158 assert((PTSectionSize % PTEntrySize) == 0 &&
159 "Pointers section does not contain a whole number of stubs?");
161 DEBUG(dbgs() << "Populating pointer table section "
162 << Sections[PTSectionID].Name
163 << ", Section ID " << PTSectionID << ", "
164 << NumPTEntries << " entries, " << PTEntrySize
165 << " bytes each:\n");
167 for (unsigned i = 0; i < NumPTEntries; ++i) {
168 unsigned SymbolIndex =
169 Obj.getIndirectSymbolTableEntry(DySymTabCmd, FirstIndirectSymbol + i);
170 symbol_iterator SI = Obj.getSymbolByIndex(SymbolIndex);
171 StringRef IndirectSymbolName;
172 SI->getName(IndirectSymbolName);
173 DEBUG(dbgs() << " " << IndirectSymbolName << ": index " << SymbolIndex
174 << ", PT offset: " << PTEntryOffset << "\n");
175 RelocationEntry RE(PTSectionID, PTEntryOffset,
176 MachO::GENERIC_RELOC_VANILLA, 0, false, 2);
177 addRelocationForSymbol(RE, IndirectSymbolName);
178 PTEntryOffset += PTEntrySize;
182 bool RuntimeDyldMachO::isCompatibleFile(const object::ObjectFile &Obj) const {
183 return Obj.isMachO();
186 template <typename Impl>
187 void RuntimeDyldMachOCRTPBase<Impl>::finalizeLoad(const ObjectFile &ObjImg,
188 ObjSectionToIDMap &SectionMap) {
189 unsigned EHFrameSID = RTDYLD_INVALID_SECTION_ID;
190 unsigned TextSID = RTDYLD_INVALID_SECTION_ID;
191 unsigned ExceptTabSID = RTDYLD_INVALID_SECTION_ID;
192 ObjSectionToIDMap::iterator i, e;
194 for (i = SectionMap.begin(), e = SectionMap.end(); i != e; ++i) {
195 const SectionRef &Section = i->first;
197 Section.getName(Name);
198 if (Name == "__eh_frame")
199 EHFrameSID = i->second;
200 else if (Name == "__text")
202 else if (Name == "__gcc_except_tab")
203 ExceptTabSID = i->second;
205 impl().finalizeSection(ObjImg, i->second, Section);
207 UnregisteredEHFrameSections.push_back(
208 EHFrameRelatedSections(EHFrameSID, TextSID, ExceptTabSID));
211 template <typename Impl>
212 unsigned char *RuntimeDyldMachOCRTPBase<Impl>::processFDE(unsigned char *P,
213 int64_t DeltaForText,
214 int64_t DeltaForEH) {
215 typedef typename Impl::TargetPtrT TargetPtrT;
217 DEBUG(dbgs() << "Processing FDE: Delta for text: " << DeltaForText
218 << ", Delta for EH: " << DeltaForEH << "\n");
219 uint32_t Length = readBytesUnaligned(P, 4);
221 unsigned char *Ret = P + Length;
222 uint32_t Offset = readBytesUnaligned(P, 4);
223 if (Offset == 0) // is a CIE
227 TargetPtrT FDELocation = readBytesUnaligned(P, sizeof(TargetPtrT));
228 TargetPtrT NewLocation = FDELocation - DeltaForText;
229 writeBytesUnaligned(NewLocation, P, sizeof(TargetPtrT));
231 P += sizeof(TargetPtrT);
233 // Skip the FDE address range
234 P += sizeof(TargetPtrT);
236 uint8_t Augmentationsize = *P;
238 if (Augmentationsize != 0) {
239 TargetPtrT LSDA = readBytesUnaligned(P, sizeof(TargetPtrT));
240 TargetPtrT NewLSDA = LSDA - DeltaForEH;
241 writeBytesUnaligned(NewLSDA, P, sizeof(TargetPtrT));
247 static int64_t computeDelta(SectionEntry *A, SectionEntry *B) {
248 int64_t ObjDistance = A->ObjAddress - B->ObjAddress;
249 int64_t MemDistance = A->LoadAddress - B->LoadAddress;
250 return ObjDistance - MemDistance;
253 template <typename Impl>
254 void RuntimeDyldMachOCRTPBase<Impl>::registerEHFrames() {
258 for (int i = 0, e = UnregisteredEHFrameSections.size(); i != e; ++i) {
259 EHFrameRelatedSections &SectionInfo = UnregisteredEHFrameSections[i];
260 if (SectionInfo.EHFrameSID == RTDYLD_INVALID_SECTION_ID ||
261 SectionInfo.TextSID == RTDYLD_INVALID_SECTION_ID)
263 SectionEntry *Text = &Sections[SectionInfo.TextSID];
264 SectionEntry *EHFrame = &Sections[SectionInfo.EHFrameSID];
265 SectionEntry *ExceptTab = nullptr;
266 if (SectionInfo.ExceptTabSID != RTDYLD_INVALID_SECTION_ID)
267 ExceptTab = &Sections[SectionInfo.ExceptTabSID];
269 int64_t DeltaForText = computeDelta(Text, EHFrame);
270 int64_t DeltaForEH = 0;
272 DeltaForEH = computeDelta(ExceptTab, EHFrame);
274 unsigned char *P = EHFrame->Address;
275 unsigned char *End = P + EHFrame->Size;
277 P = processFDE(P, DeltaForText, DeltaForEH);
280 MemMgr->registerEHFrames(EHFrame->Address, EHFrame->LoadAddress,
283 UnregisteredEHFrameSections.clear();
286 std::unique_ptr<RuntimeDyldMachO>
287 RuntimeDyldMachO::create(Triple::ArchType Arch, RTDyldMemoryManager *MM) {
290 llvm_unreachable("Unsupported target for RuntimeDyldMachO.");
292 case Triple::arm: return make_unique<RuntimeDyldMachOARM>(MM);
293 case Triple::aarch64: return make_unique<RuntimeDyldMachOAArch64>(MM);
294 case Triple::x86: return make_unique<RuntimeDyldMachOI386>(MM);
295 case Triple::x86_64: return make_unique<RuntimeDyldMachOX86_64>(MM);
299 std::unique_ptr<RuntimeDyld::LoadedObjectInfo>
300 RuntimeDyldMachO::loadObject(const object::ObjectFile &O) {
301 unsigned SectionStartIdx, SectionEndIdx;
302 std::tie(SectionStartIdx, SectionEndIdx) = loadObjectImpl(O);
303 return llvm::make_unique<LoadedMachOObjectInfo>(*this, SectionStartIdx,
307 } // end namespace llvm