Loading...
--- dyld/dyld-1284.13/other-tools/SymbolicatedImage.cpp
+++ dyld/dyld-1335/other-tools/SymbolicatedImage.cpp
@@ -32,7 +32,6 @@
// llvm
#if HAVE_LIBLTO
- #include <llvm-c/Disassembler.h>
extern "C" void lto_initialize_disassembler(); // from libLTO.dylib but not in Disassembler.h
extern "C" int LLVMSetDisasmOptions(LLVMDisasmContextRef context, uint64_t options);\
WEAK_LINK_FORCE_IMPORT(LLVMCreateDisasm);
@@ -71,7 +70,10 @@
{
// build list of sections
_image.header()->forEachSection(^(const Header::SectionInfo& sectInfo, bool& stop) {
- _sectionSymbols.push_back({sectInfo});
+ std::string sectName(sectInfo.segmentName);
+ sectName += ",";
+ sectName += sectInfo.sectionName;
+ _sectionSymbols.push_back({sectName, sectInfo});
});
// check for encrypted range
@@ -96,13 +98,13 @@
// add symbols from nlist
_image.symbolTable().forEachDefinedSymbol(^(const Symbol& symbol, uint32_t symbolIndex, bool& stop) {
uint64_t absAddress;
- if ( !symbol.isAbsolute(absAddress) && (symbol.implOffset() != 0) && symbol.sectionOrdinal() < _sectionSymbols.size() ) {
+ if ( !symbol.isAbsolute(absAddress) && (symbol.implOffset() != 0 || prefLoadAddress() == 0) && (symbol.sectionOrdinal()-1) < _sectionSymbols.size() ) {
const char* symName = symbol.name().c_str();
_symbolsMap[_prefLoadAddress+symbol.implOffset()] = symName;
SectionSymbols& ss = _sectionSymbols[symbol.sectionOrdinal()-1];
uint64_t offsetInSection = _prefLoadAddress+symbol.implOffset()-ss.sectInfo.address;
- ss.symbols.push_back({offsetInSection, symName});
+ ss.symbols.push_back({offsetInSection, symName, symbol.isThumb()});
}
if ( symbol.scope() == Symbol::Scope::translationUnit )
hasLocalSymbols = true;
@@ -297,6 +299,8 @@
// sort symbols within section
for (SectionSymbols& ss : _sectionSymbols) {
std::sort(ss.symbols.begin(), ss.symbols.end(), [](const SectionSymbols::Sym& a, const SectionSymbols::Sym& b) {
+ if ( a.offsetInSection == b.offsetInSection )
+ return CString(a.name) < CString(b.name);
return (a.offsetInSection < b.offsetInSection);
});
}
@@ -344,9 +348,7 @@
uint64_t segOffset = (uint8_t*)fixup.location - (uint8_t*)(fixup.segment->content);
uint64_t runtimeOffset = fixup.segment->runtimeOffset + segOffset;
uint64_t address = _prefLoadAddress + runtimeOffset;
- const char* inSymbolName;
- uint32_t inSymbolOffset;
- this->findClosestSymbol(runtimeOffset, inSymbolName, inSymbolOffset);
+ SymbolLoc symbolLoc = findClosestSymbol(address);
uint32_t sectNum = 1;
for ( const SectionSymbols& ss : _sectionSymbols ) {
if ( ss.sectInfo.segmentName == fixup.segment->segName ) {
@@ -355,7 +357,7 @@
}
sectNum++;
}
- _fixups.push_back({fixup, address, inSymbolName, inSymbolOffset, sectNum});
+ _fixups.push_back({fixup, symbolLoc, address, sectNum});
}
@@ -364,34 +366,40 @@
// FIXME:
}
-void SymbolicatedImage::findClosestSymbol(uint64_t runtimeOffset, const char*& inSymbolName, uint32_t& inSymbolOffset) const
-{
- inSymbolName = "";
- inSymbolOffset = 0;
+SymbolicatedImage::SymbolLoc SymbolicatedImage::findClosestSymbol(uint64_t runtimeOffset) const
+{
+ SymbolLoc loc;
for (const SectionSymbols& ss : _sectionSymbols) {
if ( (runtimeOffset >= ss.sectInfo.address) && (runtimeOffset < ss.sectInfo.address+ss.sectInfo.size) ) {
+ if ( ss.symbols.empty() ) {
+ loc.name = ss.sectStartName.c_str();
+ loc.inSymbolOffset = (uint32_t)(runtimeOffset - ss.sectInfo.address);
+ loc.isThumb = false;
+ continue;
+ }
+
// find largest symbol address that is <= target address
const uint64_t targetSectOffset = runtimeOffset-ss.sectInfo.address;
auto it = std::lower_bound(ss.symbols.begin(), ss.symbols.end(), targetSectOffset, [](const SectionSymbols::Sym& sym, uint64_t sectOffset) -> bool {
return sym.offsetInSection <= sectOffset;
});
// lower_bound returns the symbol after the one we need
- if ( (it != ss.symbols.end()) && (it != ss.symbols.begin()) ) {
+ if ( it != ss.symbols.begin() ) {
--it;
- inSymbolName = it->name;
- inSymbolOffset = (uint32_t)(runtimeOffset - (ss.sectInfo.address+it->offsetInSection));
- }
- else if ( ss.symbols.empty() ) {
- inSymbolName = "";
- inSymbolOffset = 0;
+ loc.name = it->name;
+ loc.inSymbolOffset = (uint32_t)(runtimeOffset - (ss.sectInfo.address+it->offsetInSection));
+ loc.isThumb = it->thumb;
}
else {
- inSymbolName = ss.symbols.front().name;
- inSymbolOffset = (uint32_t)(runtimeOffset - (ss.sectInfo.address + ss.symbols.front().offsetInSection));
+ const SectionSymbols::Sym& sym = ss.symbols.front();
+ loc.name = sym.name;
+ loc.isThumb = sym.thumb;
+ loc.inSymbolOffset = (uint32_t)(runtimeOffset - (ss.sectInfo.address + sym.offsetInSection));
}
break;
}
}
+ return loc;
}
const char* SymbolicatedImage::selectorFromObjCStub(uint64_t sectionVmAdr, const uint8_t* sectionContent, uint32_t& offset) const
@@ -918,18 +926,16 @@
}
else {
if ( symbolic ) {
- const char* inSymbolName;
- uint32_t inSymbolOffset;
- this->findClosestSymbol(fixup.rebase.targetVmOffset, inSymbolName, inSymbolOffset);
- if ( strncmp(inSymbolName, "__TEXT,", 7) == 0 ) {
+ SymbolLoc loc = findClosestSymbol(fixup.rebase.targetVmOffset);
+ if ( loc.name.starts_with("__TEXT,") ) {
const char* str = this->cStringAt(_prefLoadAddress+fixup.rebase.targetVmOffset);
snprintf(buffer, 4096, "\"%s\"%s", str, authInfo);
}
- else if ( inSymbolOffset == 0 ) {
- snprintf(buffer, 4096, "%s%s", inSymbolName, authInfo);
+ else if ( loc.inSymbolOffset == 0 ) {
+ snprintf(buffer, 4096, "%s%s", loc.name.c_str(), authInfo);
}
else {
- snprintf(buffer, 4096, "%s+%u%s", inSymbolName, inSymbolOffset, authInfo);
+ snprintf(buffer, 4096, "%s+%u%s", loc.name.c_str(), loc.inSymbolOffset, authInfo);
}
}
else {
@@ -946,6 +952,10 @@
LLVMDisasmDispose(_llvmRef);
_llvmRef = nullptr;
}
+ if ( _llvmThumbRef != nullptr ) {
+ LLVMDisasmDispose(_llvmThumbRef);
+ _llvmThumbRef = nullptr;
+ }
#endif
}
@@ -967,10 +977,18 @@
const char* SymbolicatedImage::targetTriple() const
{
Architecture arch = _image.header()->arch();
- if ( arch.usesArm64Instructions() )
+ if ( arch.usesArm64AuthPointers() )
return "arm64e-apple-darwin";
+ else if ( arch.usesArm64Instructions() )
+ return "arm64-apple-darwin";
else if ( arch.usesx86_64Instructions() )
return "x86_64h-apple-darwin";
+ else if ( arch.usesThumbInstructions() && !arch.usesArm32Instructions() )
+ return "thumbv7em-apple-darwin";
+ else if ( arch.usesArm32Instructions() )
+ return "armv7-apple-darwin";
+ else if ( arch == Architecture::i386 )
+ return "i386-apple-darwin";
else
return "unknown";
}
@@ -992,6 +1010,13 @@
_llvmRef = LLVMCreateDisasm(targetTriple(), this, 0, &printDumpOpInfoCallback, &printDumpSymbolCallback);
if ( _llvmRef != nullptr )
LLVMSetDisasmOptions(_llvmRef, LLVMDisassembler_Option_PrintImmHex);
+
+ Architecture arch = _image.header()->arch();
+ if ( arch.usesArm32Instructions() && arch.usesThumbInstructions() ) {
+ _llvmThumbRef = LLVMCreateDisasm("thumbv7em-apple-darwin", this, 0, &printDumpOpInfoCallback, &printDumpSymbolCallback);
+ if ( _llvmThumbRef != nullptr )
+ LLVMSetDisasmOptions(_llvmThumbRef, LLVMDisassembler_Option_PrintImmHex);
+ }
}