1*9880d681SAndroid Build Coastguard Worker //===-- RuntimeDyldMachO.cpp - Run-time dynamic linker for MC-JIT -*- C++ -*-=//
2*9880d681SAndroid Build Coastguard Worker //
3*9880d681SAndroid Build Coastguard Worker // The LLVM Compiler Infrastructure
4*9880d681SAndroid Build Coastguard Worker //
5*9880d681SAndroid Build Coastguard Worker // This file is distributed under the University of Illinois Open Source
6*9880d681SAndroid Build Coastguard Worker // License. See LICENSE.TXT for details.
7*9880d681SAndroid Build Coastguard Worker //
8*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
9*9880d681SAndroid Build Coastguard Worker //
10*9880d681SAndroid Build Coastguard Worker // Implementation of the MC-JIT runtime dynamic linker.
11*9880d681SAndroid Build Coastguard Worker //
12*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
13*9880d681SAndroid Build Coastguard Worker
14*9880d681SAndroid Build Coastguard Worker #include "RuntimeDyldMachO.h"
15*9880d681SAndroid Build Coastguard Worker #include "Targets/RuntimeDyldMachOAArch64.h"
16*9880d681SAndroid Build Coastguard Worker #include "Targets/RuntimeDyldMachOARM.h"
17*9880d681SAndroid Build Coastguard Worker #include "Targets/RuntimeDyldMachOI386.h"
18*9880d681SAndroid Build Coastguard Worker #include "Targets/RuntimeDyldMachOX86_64.h"
19*9880d681SAndroid Build Coastguard Worker #include "llvm/ADT/STLExtras.h"
20*9880d681SAndroid Build Coastguard Worker #include "llvm/ADT/StringRef.h"
21*9880d681SAndroid Build Coastguard Worker
22*9880d681SAndroid Build Coastguard Worker using namespace llvm;
23*9880d681SAndroid Build Coastguard Worker using namespace llvm::object;
24*9880d681SAndroid Build Coastguard Worker
25*9880d681SAndroid Build Coastguard Worker #define DEBUG_TYPE "dyld"
26*9880d681SAndroid Build Coastguard Worker
27*9880d681SAndroid Build Coastguard Worker namespace {
28*9880d681SAndroid Build Coastguard Worker
29*9880d681SAndroid Build Coastguard Worker class LoadedMachOObjectInfo final
30*9880d681SAndroid Build Coastguard Worker : public RuntimeDyld::LoadedObjectInfoHelper<LoadedMachOObjectInfo> {
31*9880d681SAndroid Build Coastguard Worker public:
LoadedMachOObjectInfo(RuntimeDyldImpl & RTDyld,ObjSectionToIDMap ObjSecToIDMap)32*9880d681SAndroid Build Coastguard Worker LoadedMachOObjectInfo(RuntimeDyldImpl &RTDyld,
33*9880d681SAndroid Build Coastguard Worker ObjSectionToIDMap ObjSecToIDMap)
34*9880d681SAndroid Build Coastguard Worker : LoadedObjectInfoHelper(RTDyld, std::move(ObjSecToIDMap)) {}
35*9880d681SAndroid Build Coastguard Worker
36*9880d681SAndroid Build Coastguard Worker OwningBinary<ObjectFile>
getObjectForDebug(const ObjectFile & Obj) const37*9880d681SAndroid Build Coastguard Worker getObjectForDebug(const ObjectFile &Obj) const override {
38*9880d681SAndroid Build Coastguard Worker return OwningBinary<ObjectFile>();
39*9880d681SAndroid Build Coastguard Worker }
40*9880d681SAndroid Build Coastguard Worker };
41*9880d681SAndroid Build Coastguard Worker
42*9880d681SAndroid Build Coastguard Worker }
43*9880d681SAndroid Build Coastguard Worker
44*9880d681SAndroid Build Coastguard Worker namespace llvm {
45*9880d681SAndroid Build Coastguard Worker
memcpyAddend(const RelocationEntry & RE) const46*9880d681SAndroid Build Coastguard Worker int64_t RuntimeDyldMachO::memcpyAddend(const RelocationEntry &RE) const {
47*9880d681SAndroid Build Coastguard Worker unsigned NumBytes = 1 << RE.Size;
48*9880d681SAndroid Build Coastguard Worker uint8_t *Src = Sections[RE.SectionID].getAddress() + RE.Offset;
49*9880d681SAndroid Build Coastguard Worker
50*9880d681SAndroid Build Coastguard Worker return static_cast<int64_t>(readBytesUnaligned(Src, NumBytes));
51*9880d681SAndroid Build Coastguard Worker }
52*9880d681SAndroid Build Coastguard Worker
53*9880d681SAndroid Build Coastguard Worker Expected<relocation_iterator>
processScatteredVANILLA(unsigned SectionID,relocation_iterator RelI,const ObjectFile & BaseObjT,RuntimeDyldMachO::ObjSectionToIDMap & ObjSectionToID)54*9880d681SAndroid Build Coastguard Worker RuntimeDyldMachO::processScatteredVANILLA(
55*9880d681SAndroid Build Coastguard Worker unsigned SectionID, relocation_iterator RelI,
56*9880d681SAndroid Build Coastguard Worker const ObjectFile &BaseObjT,
57*9880d681SAndroid Build Coastguard Worker RuntimeDyldMachO::ObjSectionToIDMap &ObjSectionToID) {
58*9880d681SAndroid Build Coastguard Worker const MachOObjectFile &Obj =
59*9880d681SAndroid Build Coastguard Worker static_cast<const MachOObjectFile&>(BaseObjT);
60*9880d681SAndroid Build Coastguard Worker MachO::any_relocation_info RE =
61*9880d681SAndroid Build Coastguard Worker Obj.getRelocation(RelI->getRawDataRefImpl());
62*9880d681SAndroid Build Coastguard Worker
63*9880d681SAndroid Build Coastguard Worker SectionEntry &Section = Sections[SectionID];
64*9880d681SAndroid Build Coastguard Worker uint32_t RelocType = Obj.getAnyRelocationType(RE);
65*9880d681SAndroid Build Coastguard Worker bool IsPCRel = Obj.getAnyRelocationPCRel(RE);
66*9880d681SAndroid Build Coastguard Worker unsigned Size = Obj.getAnyRelocationLength(RE);
67*9880d681SAndroid Build Coastguard Worker uint64_t Offset = RelI->getOffset();
68*9880d681SAndroid Build Coastguard Worker uint8_t *LocalAddress = Section.getAddressWithOffset(Offset);
69*9880d681SAndroid Build Coastguard Worker unsigned NumBytes = 1 << Size;
70*9880d681SAndroid Build Coastguard Worker int64_t Addend = readBytesUnaligned(LocalAddress, NumBytes);
71*9880d681SAndroid Build Coastguard Worker
72*9880d681SAndroid Build Coastguard Worker unsigned SymbolBaseAddr = Obj.getScatteredRelocationValue(RE);
73*9880d681SAndroid Build Coastguard Worker section_iterator TargetSI = getSectionByAddress(Obj, SymbolBaseAddr);
74*9880d681SAndroid Build Coastguard Worker assert(TargetSI != Obj.section_end() && "Can't find section for symbol");
75*9880d681SAndroid Build Coastguard Worker uint64_t SectionBaseAddr = TargetSI->getAddress();
76*9880d681SAndroid Build Coastguard Worker SectionRef TargetSection = *TargetSI;
77*9880d681SAndroid Build Coastguard Worker bool IsCode = TargetSection.isText();
78*9880d681SAndroid Build Coastguard Worker uint32_t TargetSectionID = ~0U;
79*9880d681SAndroid Build Coastguard Worker if (auto TargetSectionIDOrErr =
80*9880d681SAndroid Build Coastguard Worker findOrEmitSection(Obj, TargetSection, IsCode, ObjSectionToID))
81*9880d681SAndroid Build Coastguard Worker TargetSectionID = *TargetSectionIDOrErr;
82*9880d681SAndroid Build Coastguard Worker else
83*9880d681SAndroid Build Coastguard Worker return TargetSectionIDOrErr.takeError();
84*9880d681SAndroid Build Coastguard Worker
85*9880d681SAndroid Build Coastguard Worker Addend -= SectionBaseAddr;
86*9880d681SAndroid Build Coastguard Worker RelocationEntry R(SectionID, Offset, RelocType, Addend, IsPCRel, Size);
87*9880d681SAndroid Build Coastguard Worker
88*9880d681SAndroid Build Coastguard Worker addRelocationForSection(R, TargetSectionID);
89*9880d681SAndroid Build Coastguard Worker
90*9880d681SAndroid Build Coastguard Worker return ++RelI;
91*9880d681SAndroid Build Coastguard Worker }
92*9880d681SAndroid Build Coastguard Worker
93*9880d681SAndroid Build Coastguard Worker
94*9880d681SAndroid Build Coastguard Worker Expected<RelocationValueRef>
getRelocationValueRef(const ObjectFile & BaseTObj,const relocation_iterator & RI,const RelocationEntry & RE,ObjSectionToIDMap & ObjSectionToID)95*9880d681SAndroid Build Coastguard Worker RuntimeDyldMachO::getRelocationValueRef(
96*9880d681SAndroid Build Coastguard Worker const ObjectFile &BaseTObj, const relocation_iterator &RI,
97*9880d681SAndroid Build Coastguard Worker const RelocationEntry &RE, ObjSectionToIDMap &ObjSectionToID) {
98*9880d681SAndroid Build Coastguard Worker
99*9880d681SAndroid Build Coastguard Worker const MachOObjectFile &Obj =
100*9880d681SAndroid Build Coastguard Worker static_cast<const MachOObjectFile &>(BaseTObj);
101*9880d681SAndroid Build Coastguard Worker MachO::any_relocation_info RelInfo =
102*9880d681SAndroid Build Coastguard Worker Obj.getRelocation(RI->getRawDataRefImpl());
103*9880d681SAndroid Build Coastguard Worker RelocationValueRef Value;
104*9880d681SAndroid Build Coastguard Worker
105*9880d681SAndroid Build Coastguard Worker bool IsExternal = Obj.getPlainRelocationExternal(RelInfo);
106*9880d681SAndroid Build Coastguard Worker if (IsExternal) {
107*9880d681SAndroid Build Coastguard Worker symbol_iterator Symbol = RI->getSymbol();
108*9880d681SAndroid Build Coastguard Worker StringRef TargetName;
109*9880d681SAndroid Build Coastguard Worker if (auto TargetNameOrErr = Symbol->getName())
110*9880d681SAndroid Build Coastguard Worker TargetName = *TargetNameOrErr;
111*9880d681SAndroid Build Coastguard Worker else
112*9880d681SAndroid Build Coastguard Worker return TargetNameOrErr.takeError();
113*9880d681SAndroid Build Coastguard Worker RTDyldSymbolTable::const_iterator SI =
114*9880d681SAndroid Build Coastguard Worker GlobalSymbolTable.find(TargetName.data());
115*9880d681SAndroid Build Coastguard Worker if (SI != GlobalSymbolTable.end()) {
116*9880d681SAndroid Build Coastguard Worker const auto &SymInfo = SI->second;
117*9880d681SAndroid Build Coastguard Worker Value.SectionID = SymInfo.getSectionID();
118*9880d681SAndroid Build Coastguard Worker Value.Offset = SymInfo.getOffset() + RE.Addend;
119*9880d681SAndroid Build Coastguard Worker } else {
120*9880d681SAndroid Build Coastguard Worker Value.SymbolName = TargetName.data();
121*9880d681SAndroid Build Coastguard Worker Value.Offset = RE.Addend;
122*9880d681SAndroid Build Coastguard Worker }
123*9880d681SAndroid Build Coastguard Worker } else {
124*9880d681SAndroid Build Coastguard Worker SectionRef Sec = Obj.getAnyRelocationSection(RelInfo);
125*9880d681SAndroid Build Coastguard Worker bool IsCode = Sec.isText();
126*9880d681SAndroid Build Coastguard Worker if (auto SectionIDOrErr = findOrEmitSection(Obj, Sec, IsCode,
127*9880d681SAndroid Build Coastguard Worker ObjSectionToID))
128*9880d681SAndroid Build Coastguard Worker Value.SectionID = *SectionIDOrErr;
129*9880d681SAndroid Build Coastguard Worker else
130*9880d681SAndroid Build Coastguard Worker return SectionIDOrErr.takeError();
131*9880d681SAndroid Build Coastguard Worker uint64_t Addr = Sec.getAddress();
132*9880d681SAndroid Build Coastguard Worker Value.Offset = RE.Addend - Addr;
133*9880d681SAndroid Build Coastguard Worker }
134*9880d681SAndroid Build Coastguard Worker
135*9880d681SAndroid Build Coastguard Worker return Value;
136*9880d681SAndroid Build Coastguard Worker }
137*9880d681SAndroid Build Coastguard Worker
makeValueAddendPCRel(RelocationValueRef & Value,const relocation_iterator & RI,unsigned OffsetToNextPC)138*9880d681SAndroid Build Coastguard Worker void RuntimeDyldMachO::makeValueAddendPCRel(RelocationValueRef &Value,
139*9880d681SAndroid Build Coastguard Worker const relocation_iterator &RI,
140*9880d681SAndroid Build Coastguard Worker unsigned OffsetToNextPC) {
141*9880d681SAndroid Build Coastguard Worker auto &O = *cast<MachOObjectFile>(RI->getObject());
142*9880d681SAndroid Build Coastguard Worker section_iterator SecI = O.getRelocationRelocatedSection(RI);
143*9880d681SAndroid Build Coastguard Worker Value.Offset += RI->getOffset() + OffsetToNextPC + SecI->getAddress();
144*9880d681SAndroid Build Coastguard Worker }
145*9880d681SAndroid Build Coastguard Worker
dumpRelocationToResolve(const RelocationEntry & RE,uint64_t Value) const146*9880d681SAndroid Build Coastguard Worker void RuntimeDyldMachO::dumpRelocationToResolve(const RelocationEntry &RE,
147*9880d681SAndroid Build Coastguard Worker uint64_t Value) const {
148*9880d681SAndroid Build Coastguard Worker const SectionEntry &Section = Sections[RE.SectionID];
149*9880d681SAndroid Build Coastguard Worker uint8_t *LocalAddress = Section.getAddress() + RE.Offset;
150*9880d681SAndroid Build Coastguard Worker uint64_t FinalAddress = Section.getLoadAddress() + RE.Offset;
151*9880d681SAndroid Build Coastguard Worker
152*9880d681SAndroid Build Coastguard Worker dbgs() << "resolveRelocation Section: " << RE.SectionID
153*9880d681SAndroid Build Coastguard Worker << " LocalAddress: " << format("%p", LocalAddress)
154*9880d681SAndroid Build Coastguard Worker << " FinalAddress: " << format("0x%016" PRIx64, FinalAddress)
155*9880d681SAndroid Build Coastguard Worker << " Value: " << format("0x%016" PRIx64, Value) << " Addend: " << RE.Addend
156*9880d681SAndroid Build Coastguard Worker << " isPCRel: " << RE.IsPCRel << " MachoType: " << RE.RelType
157*9880d681SAndroid Build Coastguard Worker << " Size: " << (1 << RE.Size) << "\n";
158*9880d681SAndroid Build Coastguard Worker }
159*9880d681SAndroid Build Coastguard Worker
160*9880d681SAndroid Build Coastguard Worker section_iterator
getSectionByAddress(const MachOObjectFile & Obj,uint64_t Addr)161*9880d681SAndroid Build Coastguard Worker RuntimeDyldMachO::getSectionByAddress(const MachOObjectFile &Obj,
162*9880d681SAndroid Build Coastguard Worker uint64_t Addr) {
163*9880d681SAndroid Build Coastguard Worker section_iterator SI = Obj.section_begin();
164*9880d681SAndroid Build Coastguard Worker section_iterator SE = Obj.section_end();
165*9880d681SAndroid Build Coastguard Worker
166*9880d681SAndroid Build Coastguard Worker for (; SI != SE; ++SI) {
167*9880d681SAndroid Build Coastguard Worker uint64_t SAddr = SI->getAddress();
168*9880d681SAndroid Build Coastguard Worker uint64_t SSize = SI->getSize();
169*9880d681SAndroid Build Coastguard Worker if ((Addr >= SAddr) && (Addr < SAddr + SSize))
170*9880d681SAndroid Build Coastguard Worker return SI;
171*9880d681SAndroid Build Coastguard Worker }
172*9880d681SAndroid Build Coastguard Worker
173*9880d681SAndroid Build Coastguard Worker return SE;
174*9880d681SAndroid Build Coastguard Worker }
175*9880d681SAndroid Build Coastguard Worker
176*9880d681SAndroid Build Coastguard Worker
177*9880d681SAndroid Build Coastguard Worker // Populate __pointers section.
populateIndirectSymbolPointersSection(const MachOObjectFile & Obj,const SectionRef & PTSection,unsigned PTSectionID)178*9880d681SAndroid Build Coastguard Worker Error RuntimeDyldMachO::populateIndirectSymbolPointersSection(
179*9880d681SAndroid Build Coastguard Worker const MachOObjectFile &Obj,
180*9880d681SAndroid Build Coastguard Worker const SectionRef &PTSection,
181*9880d681SAndroid Build Coastguard Worker unsigned PTSectionID) {
182*9880d681SAndroid Build Coastguard Worker assert(!Obj.is64Bit() &&
183*9880d681SAndroid Build Coastguard Worker "Pointer table section not supported in 64-bit MachO.");
184*9880d681SAndroid Build Coastguard Worker
185*9880d681SAndroid Build Coastguard Worker MachO::dysymtab_command DySymTabCmd = Obj.getDysymtabLoadCommand();
186*9880d681SAndroid Build Coastguard Worker MachO::section Sec32 = Obj.getSection(PTSection.getRawDataRefImpl());
187*9880d681SAndroid Build Coastguard Worker uint32_t PTSectionSize = Sec32.size;
188*9880d681SAndroid Build Coastguard Worker unsigned FirstIndirectSymbol = Sec32.reserved1;
189*9880d681SAndroid Build Coastguard Worker const unsigned PTEntrySize = 4;
190*9880d681SAndroid Build Coastguard Worker unsigned NumPTEntries = PTSectionSize / PTEntrySize;
191*9880d681SAndroid Build Coastguard Worker unsigned PTEntryOffset = 0;
192*9880d681SAndroid Build Coastguard Worker
193*9880d681SAndroid Build Coastguard Worker assert((PTSectionSize % PTEntrySize) == 0 &&
194*9880d681SAndroid Build Coastguard Worker "Pointers section does not contain a whole number of stubs?");
195*9880d681SAndroid Build Coastguard Worker
196*9880d681SAndroid Build Coastguard Worker DEBUG(dbgs() << "Populating pointer table section "
197*9880d681SAndroid Build Coastguard Worker << Sections[PTSectionID].getName() << ", Section ID "
198*9880d681SAndroid Build Coastguard Worker << PTSectionID << ", " << NumPTEntries << " entries, "
199*9880d681SAndroid Build Coastguard Worker << PTEntrySize << " bytes each:\n");
200*9880d681SAndroid Build Coastguard Worker
201*9880d681SAndroid Build Coastguard Worker for (unsigned i = 0; i < NumPTEntries; ++i) {
202*9880d681SAndroid Build Coastguard Worker unsigned SymbolIndex =
203*9880d681SAndroid Build Coastguard Worker Obj.getIndirectSymbolTableEntry(DySymTabCmd, FirstIndirectSymbol + i);
204*9880d681SAndroid Build Coastguard Worker symbol_iterator SI = Obj.getSymbolByIndex(SymbolIndex);
205*9880d681SAndroid Build Coastguard Worker StringRef IndirectSymbolName;
206*9880d681SAndroid Build Coastguard Worker if (auto IndirectSymbolNameOrErr = SI->getName())
207*9880d681SAndroid Build Coastguard Worker IndirectSymbolName = *IndirectSymbolNameOrErr;
208*9880d681SAndroid Build Coastguard Worker else
209*9880d681SAndroid Build Coastguard Worker return IndirectSymbolNameOrErr.takeError();
210*9880d681SAndroid Build Coastguard Worker DEBUG(dbgs() << " " << IndirectSymbolName << ": index " << SymbolIndex
211*9880d681SAndroid Build Coastguard Worker << ", PT offset: " << PTEntryOffset << "\n");
212*9880d681SAndroid Build Coastguard Worker RelocationEntry RE(PTSectionID, PTEntryOffset,
213*9880d681SAndroid Build Coastguard Worker MachO::GENERIC_RELOC_VANILLA, 0, false, 2);
214*9880d681SAndroid Build Coastguard Worker addRelocationForSymbol(RE, IndirectSymbolName);
215*9880d681SAndroid Build Coastguard Worker PTEntryOffset += PTEntrySize;
216*9880d681SAndroid Build Coastguard Worker }
217*9880d681SAndroid Build Coastguard Worker return Error::success();
218*9880d681SAndroid Build Coastguard Worker }
219*9880d681SAndroid Build Coastguard Worker
isCompatibleFile(const object::ObjectFile & Obj) const220*9880d681SAndroid Build Coastguard Worker bool RuntimeDyldMachO::isCompatibleFile(const object::ObjectFile &Obj) const {
221*9880d681SAndroid Build Coastguard Worker return Obj.isMachO();
222*9880d681SAndroid Build Coastguard Worker }
223*9880d681SAndroid Build Coastguard Worker
224*9880d681SAndroid Build Coastguard Worker template <typename Impl>
225*9880d681SAndroid Build Coastguard Worker Error
finalizeLoad(const ObjectFile & Obj,ObjSectionToIDMap & SectionMap)226*9880d681SAndroid Build Coastguard Worker RuntimeDyldMachOCRTPBase<Impl>::finalizeLoad(const ObjectFile &Obj,
227*9880d681SAndroid Build Coastguard Worker ObjSectionToIDMap &SectionMap) {
228*9880d681SAndroid Build Coastguard Worker unsigned EHFrameSID = RTDYLD_INVALID_SECTION_ID;
229*9880d681SAndroid Build Coastguard Worker unsigned TextSID = RTDYLD_INVALID_SECTION_ID;
230*9880d681SAndroid Build Coastguard Worker unsigned ExceptTabSID = RTDYLD_INVALID_SECTION_ID;
231*9880d681SAndroid Build Coastguard Worker
232*9880d681SAndroid Build Coastguard Worker for (const auto &Section : Obj.sections()) {
233*9880d681SAndroid Build Coastguard Worker StringRef Name;
234*9880d681SAndroid Build Coastguard Worker Section.getName(Name);
235*9880d681SAndroid Build Coastguard Worker
236*9880d681SAndroid Build Coastguard Worker // Force emission of the __text, __eh_frame, and __gcc_except_tab sections
237*9880d681SAndroid Build Coastguard Worker // if they're present. Otherwise call down to the impl to handle other
238*9880d681SAndroid Build Coastguard Worker // sections that have already been emitted.
239*9880d681SAndroid Build Coastguard Worker if (Name == "__text") {
240*9880d681SAndroid Build Coastguard Worker if (auto TextSIDOrErr = findOrEmitSection(Obj, Section, true, SectionMap))
241*9880d681SAndroid Build Coastguard Worker TextSID = *TextSIDOrErr;
242*9880d681SAndroid Build Coastguard Worker else
243*9880d681SAndroid Build Coastguard Worker return TextSIDOrErr.takeError();
244*9880d681SAndroid Build Coastguard Worker } else if (Name == "__eh_frame") {
245*9880d681SAndroid Build Coastguard Worker if (auto EHFrameSIDOrErr = findOrEmitSection(Obj, Section, false,
246*9880d681SAndroid Build Coastguard Worker SectionMap))
247*9880d681SAndroid Build Coastguard Worker EHFrameSID = *EHFrameSIDOrErr;
248*9880d681SAndroid Build Coastguard Worker else
249*9880d681SAndroid Build Coastguard Worker return EHFrameSIDOrErr.takeError();
250*9880d681SAndroid Build Coastguard Worker } else if (Name == "__gcc_except_tab") {
251*9880d681SAndroid Build Coastguard Worker if (auto ExceptTabSIDOrErr = findOrEmitSection(Obj, Section, true,
252*9880d681SAndroid Build Coastguard Worker SectionMap))
253*9880d681SAndroid Build Coastguard Worker ExceptTabSID = *ExceptTabSIDOrErr;
254*9880d681SAndroid Build Coastguard Worker else
255*9880d681SAndroid Build Coastguard Worker return ExceptTabSIDOrErr.takeError();
256*9880d681SAndroid Build Coastguard Worker } else {
257*9880d681SAndroid Build Coastguard Worker auto I = SectionMap.find(Section);
258*9880d681SAndroid Build Coastguard Worker if (I != SectionMap.end())
259*9880d681SAndroid Build Coastguard Worker if (auto Err = impl().finalizeSection(Obj, I->second, Section))
260*9880d681SAndroid Build Coastguard Worker return Err;
261*9880d681SAndroid Build Coastguard Worker }
262*9880d681SAndroid Build Coastguard Worker }
263*9880d681SAndroid Build Coastguard Worker UnregisteredEHFrameSections.push_back(
264*9880d681SAndroid Build Coastguard Worker EHFrameRelatedSections(EHFrameSID, TextSID, ExceptTabSID));
265*9880d681SAndroid Build Coastguard Worker
266*9880d681SAndroid Build Coastguard Worker return Error::success();
267*9880d681SAndroid Build Coastguard Worker }
268*9880d681SAndroid Build Coastguard Worker
269*9880d681SAndroid Build Coastguard Worker template <typename Impl>
processFDE(uint8_t * P,int64_t DeltaForText,int64_t DeltaForEH)270*9880d681SAndroid Build Coastguard Worker unsigned char *RuntimeDyldMachOCRTPBase<Impl>::processFDE(uint8_t *P,
271*9880d681SAndroid Build Coastguard Worker int64_t DeltaForText,
272*9880d681SAndroid Build Coastguard Worker int64_t DeltaForEH) {
273*9880d681SAndroid Build Coastguard Worker typedef typename Impl::TargetPtrT TargetPtrT;
274*9880d681SAndroid Build Coastguard Worker
275*9880d681SAndroid Build Coastguard Worker DEBUG(dbgs() << "Processing FDE: Delta for text: " << DeltaForText
276*9880d681SAndroid Build Coastguard Worker << ", Delta for EH: " << DeltaForEH << "\n");
277*9880d681SAndroid Build Coastguard Worker uint32_t Length = readBytesUnaligned(P, 4);
278*9880d681SAndroid Build Coastguard Worker P += 4;
279*9880d681SAndroid Build Coastguard Worker uint8_t *Ret = P + Length;
280*9880d681SAndroid Build Coastguard Worker uint32_t Offset = readBytesUnaligned(P, 4);
281*9880d681SAndroid Build Coastguard Worker if (Offset == 0) // is a CIE
282*9880d681SAndroid Build Coastguard Worker return Ret;
283*9880d681SAndroid Build Coastguard Worker
284*9880d681SAndroid Build Coastguard Worker P += 4;
285*9880d681SAndroid Build Coastguard Worker TargetPtrT FDELocation = readBytesUnaligned(P, sizeof(TargetPtrT));
286*9880d681SAndroid Build Coastguard Worker TargetPtrT NewLocation = FDELocation - DeltaForText;
287*9880d681SAndroid Build Coastguard Worker writeBytesUnaligned(NewLocation, P, sizeof(TargetPtrT));
288*9880d681SAndroid Build Coastguard Worker
289*9880d681SAndroid Build Coastguard Worker P += sizeof(TargetPtrT);
290*9880d681SAndroid Build Coastguard Worker
291*9880d681SAndroid Build Coastguard Worker // Skip the FDE address range
292*9880d681SAndroid Build Coastguard Worker P += sizeof(TargetPtrT);
293*9880d681SAndroid Build Coastguard Worker
294*9880d681SAndroid Build Coastguard Worker uint8_t Augmentationsize = *P;
295*9880d681SAndroid Build Coastguard Worker P += 1;
296*9880d681SAndroid Build Coastguard Worker if (Augmentationsize != 0) {
297*9880d681SAndroid Build Coastguard Worker TargetPtrT LSDA = readBytesUnaligned(P, sizeof(TargetPtrT));
298*9880d681SAndroid Build Coastguard Worker TargetPtrT NewLSDA = LSDA - DeltaForEH;
299*9880d681SAndroid Build Coastguard Worker writeBytesUnaligned(NewLSDA, P, sizeof(TargetPtrT));
300*9880d681SAndroid Build Coastguard Worker }
301*9880d681SAndroid Build Coastguard Worker
302*9880d681SAndroid Build Coastguard Worker return Ret;
303*9880d681SAndroid Build Coastguard Worker }
304*9880d681SAndroid Build Coastguard Worker
computeDelta(SectionEntry * A,SectionEntry * B)305*9880d681SAndroid Build Coastguard Worker static int64_t computeDelta(SectionEntry *A, SectionEntry *B) {
306*9880d681SAndroid Build Coastguard Worker int64_t ObjDistance = static_cast<int64_t>(A->getObjAddress()) -
307*9880d681SAndroid Build Coastguard Worker static_cast<int64_t>(B->getObjAddress());
308*9880d681SAndroid Build Coastguard Worker int64_t MemDistance = A->getLoadAddress() - B->getLoadAddress();
309*9880d681SAndroid Build Coastguard Worker return ObjDistance - MemDistance;
310*9880d681SAndroid Build Coastguard Worker }
311*9880d681SAndroid Build Coastguard Worker
312*9880d681SAndroid Build Coastguard Worker template <typename Impl>
registerEHFrames()313*9880d681SAndroid Build Coastguard Worker void RuntimeDyldMachOCRTPBase<Impl>::registerEHFrames() {
314*9880d681SAndroid Build Coastguard Worker
315*9880d681SAndroid Build Coastguard Worker for (int i = 0, e = UnregisteredEHFrameSections.size(); i != e; ++i) {
316*9880d681SAndroid Build Coastguard Worker EHFrameRelatedSections &SectionInfo = UnregisteredEHFrameSections[i];
317*9880d681SAndroid Build Coastguard Worker if (SectionInfo.EHFrameSID == RTDYLD_INVALID_SECTION_ID ||
318*9880d681SAndroid Build Coastguard Worker SectionInfo.TextSID == RTDYLD_INVALID_SECTION_ID)
319*9880d681SAndroid Build Coastguard Worker continue;
320*9880d681SAndroid Build Coastguard Worker SectionEntry *Text = &Sections[SectionInfo.TextSID];
321*9880d681SAndroid Build Coastguard Worker SectionEntry *EHFrame = &Sections[SectionInfo.EHFrameSID];
322*9880d681SAndroid Build Coastguard Worker SectionEntry *ExceptTab = nullptr;
323*9880d681SAndroid Build Coastguard Worker if (SectionInfo.ExceptTabSID != RTDYLD_INVALID_SECTION_ID)
324*9880d681SAndroid Build Coastguard Worker ExceptTab = &Sections[SectionInfo.ExceptTabSID];
325*9880d681SAndroid Build Coastguard Worker
326*9880d681SAndroid Build Coastguard Worker int64_t DeltaForText = computeDelta(Text, EHFrame);
327*9880d681SAndroid Build Coastguard Worker int64_t DeltaForEH = 0;
328*9880d681SAndroid Build Coastguard Worker if (ExceptTab)
329*9880d681SAndroid Build Coastguard Worker DeltaForEH = computeDelta(ExceptTab, EHFrame);
330*9880d681SAndroid Build Coastguard Worker
331*9880d681SAndroid Build Coastguard Worker uint8_t *P = EHFrame->getAddress();
332*9880d681SAndroid Build Coastguard Worker uint8_t *End = P + EHFrame->getSize();
333*9880d681SAndroid Build Coastguard Worker while (P != End) {
334*9880d681SAndroid Build Coastguard Worker P = processFDE(P, DeltaForText, DeltaForEH);
335*9880d681SAndroid Build Coastguard Worker }
336*9880d681SAndroid Build Coastguard Worker
337*9880d681SAndroid Build Coastguard Worker MemMgr.registerEHFrames(EHFrame->getAddress(), EHFrame->getLoadAddress(),
338*9880d681SAndroid Build Coastguard Worker EHFrame->getSize());
339*9880d681SAndroid Build Coastguard Worker }
340*9880d681SAndroid Build Coastguard Worker UnregisteredEHFrameSections.clear();
341*9880d681SAndroid Build Coastguard Worker }
342*9880d681SAndroid Build Coastguard Worker
343*9880d681SAndroid Build Coastguard Worker std::unique_ptr<RuntimeDyldMachO>
create(Triple::ArchType Arch,RuntimeDyld::MemoryManager & MemMgr,RuntimeDyld::SymbolResolver & Resolver)344*9880d681SAndroid Build Coastguard Worker RuntimeDyldMachO::create(Triple::ArchType Arch,
345*9880d681SAndroid Build Coastguard Worker RuntimeDyld::MemoryManager &MemMgr,
346*9880d681SAndroid Build Coastguard Worker RuntimeDyld::SymbolResolver &Resolver) {
347*9880d681SAndroid Build Coastguard Worker switch (Arch) {
348*9880d681SAndroid Build Coastguard Worker default:
349*9880d681SAndroid Build Coastguard Worker llvm_unreachable("Unsupported target for RuntimeDyldMachO.");
350*9880d681SAndroid Build Coastguard Worker break;
351*9880d681SAndroid Build Coastguard Worker case Triple::arm:
352*9880d681SAndroid Build Coastguard Worker return make_unique<RuntimeDyldMachOARM>(MemMgr, Resolver);
353*9880d681SAndroid Build Coastguard Worker case Triple::aarch64:
354*9880d681SAndroid Build Coastguard Worker return make_unique<RuntimeDyldMachOAArch64>(MemMgr, Resolver);
355*9880d681SAndroid Build Coastguard Worker case Triple::x86:
356*9880d681SAndroid Build Coastguard Worker return make_unique<RuntimeDyldMachOI386>(MemMgr, Resolver);
357*9880d681SAndroid Build Coastguard Worker case Triple::x86_64:
358*9880d681SAndroid Build Coastguard Worker return make_unique<RuntimeDyldMachOX86_64>(MemMgr, Resolver);
359*9880d681SAndroid Build Coastguard Worker }
360*9880d681SAndroid Build Coastguard Worker }
361*9880d681SAndroid Build Coastguard Worker
362*9880d681SAndroid Build Coastguard Worker std::unique_ptr<RuntimeDyld::LoadedObjectInfo>
loadObject(const object::ObjectFile & O)363*9880d681SAndroid Build Coastguard Worker RuntimeDyldMachO::loadObject(const object::ObjectFile &O) {
364*9880d681SAndroid Build Coastguard Worker if (auto ObjSectionToIDOrErr = loadObjectImpl(O))
365*9880d681SAndroid Build Coastguard Worker return llvm::make_unique<LoadedMachOObjectInfo>(*this,
366*9880d681SAndroid Build Coastguard Worker *ObjSectionToIDOrErr);
367*9880d681SAndroid Build Coastguard Worker else {
368*9880d681SAndroid Build Coastguard Worker HasError = true;
369*9880d681SAndroid Build Coastguard Worker raw_string_ostream ErrStream(ErrorStr);
370*9880d681SAndroid Build Coastguard Worker logAllUnhandledErrors(ObjSectionToIDOrErr.takeError(), ErrStream, "");
371*9880d681SAndroid Build Coastguard Worker return nullptr;
372*9880d681SAndroid Build Coastguard Worker }
373*9880d681SAndroid Build Coastguard Worker }
374*9880d681SAndroid Build Coastguard Worker
375*9880d681SAndroid Build Coastguard Worker } // end namespace llvm
376