24 #define DEBUG_TYPE "dyld"
28 class LoadedMachOObjectInfo final
30 RuntimeDyld::LoadedObjectInfo> {
33 ObjSectionToIDMap ObjSecToIDMap)
37 getObjectForDebug(
const ObjectFile &Obj)
const override {
47 unsigned NumBytes = 1 << RE.
Size;
50 return static_cast<int64_t
>(readBytesUnaligned(Src, NumBytes));
58 bool TargetIsLocalThumbFunc) {
69 uint8_t *LocalAddress = Section.getAddressWithOffset(Offset);
70 unsigned NumBytes = 1 << Size;
71 int64_t Addend = readBytesUnaligned(LocalAddress, NumBytes);
76 uint64_t SectionBaseAddr = TargetSI->getAddress();
78 bool IsCode = TargetSection.
isText();
80 if (
auto TargetSectionIDOrErr =
81 findOrEmitSection(Obj, TargetSection, IsCode, ObjSectionToID))
82 TargetSectionID = *TargetSectionIDOrErr;
84 return TargetSectionIDOrErr.takeError();
86 Addend -= SectionBaseAddr;
88 R.IsTargetThumbFunc = TargetIsLocalThumbFunc;
90 addRelocationForSection(R, TargetSectionID);
111 if (
auto TargetNameOrErr = Symbol->getName())
112 TargetName = *TargetNameOrErr;
114 return TargetNameOrErr.takeError();
116 GlobalSymbolTable.find(TargetName.
data());
117 if (
SI != GlobalSymbolTable.end()) {
127 bool IsCode = Sec.
isText();
128 if (
auto SectionIDOrErr = findOrEmitSection(Obj, Sec, IsCode,
130 Value.SectionID = *SectionIDOrErr;
132 return SectionIDOrErr.takeError();
142 unsigned OffsetToNextPC) {
143 auto &
O = *cast<MachOObjectFile>(RI->getObject());
145 Value.Offset += RI->getOffset() + OffsetToNextPC + SecI->getAddress();
151 uint8_t *LocalAddress = Section.getAddress() + RE.
Offset;
155 <<
" LocalAddress: " <<
format(
"%p", LocalAddress)
156 <<
" FinalAddress: " <<
format(
"0x%016" PRIx64, FinalAddress)
159 <<
" Size: " << (1 << RE.
Size) <<
"\n";
168 for (;
SI != SE; ++
SI) {
171 if ((
Addr >= SAddr) && (
Addr < SAddr + SSize))
183 unsigned PTSectionID) {
185 "Pointer table section not supported in 64-bit MachO.");
190 unsigned FirstIndirectSymbol = Sec32.
reserved1;
191 const unsigned PTEntrySize = 4;
192 unsigned NumPTEntries = PTSectionSize / PTEntrySize;
193 unsigned PTEntryOffset = 0;
195 assert((PTSectionSize % PTEntrySize) == 0 &&
196 "Pointers section does not contain a whole number of stubs?");
199 << Sections[PTSectionID].
getName() <<
", Section ID "
200 << PTSectionID <<
", " << NumPTEntries <<
" entries, "
201 << PTEntrySize <<
" bytes each:\n");
203 for (
unsigned i = 0;
i < NumPTEntries; ++
i) {
204 unsigned SymbolIndex =
208 if (
auto IndirectSymbolNameOrErr =
SI->getName())
209 IndirectSymbolName = *IndirectSymbolNameOrErr;
211 return IndirectSymbolNameOrErr.takeError();
212 LLVM_DEBUG(
dbgs() <<
" " << IndirectSymbolName <<
": index " << SymbolIndex
213 <<
", PT offset: " << PTEntryOffset <<
"\n");
216 addRelocationForSymbol(RE, IndirectSymbolName);
217 PTEntryOffset += PTEntrySize;
226 template <
typename Impl>
234 for (
const auto &Section : Obj.
sections()) {
244 if (Name ==
"__text") {
245 if (
auto TextSIDOrErr = findOrEmitSection(Obj, Section,
true, SectionMap))
246 TextSID = *TextSIDOrErr;
248 return TextSIDOrErr.takeError();
249 }
else if (Name ==
"__eh_frame") {
250 if (
auto EHFrameSIDOrErr = findOrEmitSection(Obj, Section,
false,
252 EHFrameSID = *EHFrameSIDOrErr;
254 return EHFrameSIDOrErr.takeError();
255 }
else if (Name ==
"__gcc_except_tab") {
256 if (
auto ExceptTabSIDOrErr = findOrEmitSection(Obj, Section,
true,
258 ExceptTabSID = *ExceptTabSIDOrErr;
260 return ExceptTabSIDOrErr.takeError();
262 auto I = SectionMap.find(Section);
263 if (
I != SectionMap.end())
264 if (
auto Err =
impl().finalizeSection(Obj,
I->second, Section))
268 UnregisteredEHFrameSections.push_back(
274 template <
typename Impl>
276 int64_t DeltaForText,
277 int64_t DeltaForEH) {
278 typedef typename Impl::TargetPtrT TargetPtrT;
280 LLVM_DEBUG(
dbgs() <<
"Processing FDE: Delta for text: " << DeltaForText
281 <<
", Delta for EH: " << DeltaForEH <<
"\n");
285 uint32_t Offset = readBytesUnaligned(
P, 4);
290 TargetPtrT FDELocation = readBytesUnaligned(
P,
sizeof(TargetPtrT));
291 TargetPtrT NewLocation = FDELocation - DeltaForText;
292 writeBytesUnaligned(NewLocation,
P,
sizeof(TargetPtrT));
294 P +=
sizeof(TargetPtrT);
297 P +=
sizeof(TargetPtrT);
299 uint8_t Augmentationsize = *
P;
301 if (Augmentationsize != 0) {
302 TargetPtrT LSDA = readBytesUnaligned(
P,
sizeof(TargetPtrT));
303 TargetPtrT NewLSDA = LSDA - DeltaForEH;
304 writeBytesUnaligned(NewLSDA,
P,
sizeof(TargetPtrT));
311 int64_t ObjDistance =
static_cast<int64_t
>(A->getObjAddress()) -
312 static_cast<int64_t
>(
B->getObjAddress());
313 int64_t MemDistance = A->getLoadAddress() -
B->getLoadAddress();
314 return ObjDistance - MemDistance;
317 template <
typename Impl>
320 for (
int i = 0,
e = UnregisteredEHFrameSections.size();
i !=
e; ++
i) {
332 int64_t DeltaForEH = 0;
337 uint8_t *End =
P + EHFrame->
getSize();
339 P = processFDE(
P, DeltaForText, DeltaForEH);
345 UnregisteredEHFrameSections.clear();
348 std::unique_ptr<RuntimeDyldMachO>
357 return std::make_unique<RuntimeDyldMachOARM>(MemMgr,
Resolver);
359 return std::make_unique<RuntimeDyldMachOAArch64>(MemMgr,
Resolver);
361 return std::make_unique<RuntimeDyldMachOAArch64>(MemMgr,
Resolver);
363 return std::make_unique<RuntimeDyldMachOI386>(MemMgr,
Resolver);
365 return std::make_unique<RuntimeDyldMachOX86_64>(MemMgr,
Resolver);
369 std::unique_ptr<RuntimeDyld::LoadedObjectInfo>
371 if (
auto ObjSectionToIDOrErr = loadObjectImpl(
O))
372 return std::make_unique<LoadedMachOObjectInfo>(*
this,
373 *ObjSectionToIDOrErr);