X-Git-Url: https://git.saurik.com/apple/ld64.git/blobdiff_plain/f80fe69f3f29962e8aa43a99f8ed9201548f3d78..eaf282aaf65b222563e6b3db98e12d720fb161bf:/src/other/unwinddump.cpp?ds=inline diff --git a/src/other/unwinddump.cpp b/src/other/unwinddump.cpp index 3da01c5..f91ece3 100644 --- a/src/other/unwinddump.cpp +++ b/src/other/unwinddump.cpp @@ -163,6 +163,25 @@ bool UnwindPrinter::validFile(const uint8_t* fileContent) } #endif +template <> +bool UnwindPrinter::validFile(const uint8_t* fileContent) +{ + const macho_header

* header = (const macho_header

*)fileContent; + if ( header->magic() != MH_MAGIC ) + return false; + if ( header->cputype() != CPU_TYPE_ARM ) + return false; + switch (header->filetype()) { + case MH_EXECUTE: + case MH_DYLIB: + case MH_BUNDLE: + case MH_DYLINKER: + case MH_OBJECT: + return true; + } + return false; +} + template UnwindPrinter::UnwindPrinter(const uint8_t* fileContent, uint32_t fileLength, const char* path, bool showFunctionNames) : fHeader(NULL), fLength(fileLength), fUnwindSection(NULL), @@ -221,7 +240,14 @@ const char* UnwindPrinter::functionName(pint_t addr, uint32_t* offset) for (uint32_t i=0; i < fSymbolCount; ++i) { uint8_t type = fSymbols[i].n_type(); if ( ((type & N_STAB) == 0) && ((type & N_TYPE) == N_SECT) ) { - if ( fSymbols[i].n_value() == addr ) { + uint32_t value = fSymbols[i].n_value(); + if ( value == addr ) { + const char* r = &fStrings[fSymbols[i].n_strx()]; + return r; + } + if ( fSymbols[i].n_desc() & N_ARM_THUMB_DEF ) + value |= 1; + if ( value == addr ) { const char* r = &fStrings[fSymbols[i].n_strx()]; //fprintf(stderr, "addr=0x%08llX, i=%u, n_type=0x%0X, r=%s\n", (long long)(fSymbols[i].n_value()), i, fSymbols[i].n_type(), r); return r; @@ -730,6 +756,93 @@ void UnwindPrinter::decode(uint32_t encoding, const uint8_t* funcStart, c } #endif +template <> +void UnwindPrinter::decode(uint32_t encoding, const uint8_t* funcStart, char* str) +{ + *str = '\0'; + switch ( encoding & UNWIND_ARM_MODE_MASK ) { + case UNWIND_ARM_MODE_DWARF: + sprintf(str, "dwarf offset 0x%08X, ", encoding & UNWIND_ARM_DWARF_SECTION_OFFSET); + break; + case UNWIND_ARM_MODE_FRAME: + case UNWIND_ARM_MODE_FRAME_D: + switch ( encoding & UNWIND_ARM_FRAME_STACK_ADJUST_MASK ) { + case 0x00000000: + strcpy(str, "std frame: "); + break; + case 0x00400000: + strcat(str, "std frame(sp adj 4): "); + break; + case 0x00800000: + strcat(str, "std frame(sp adj 8): "); + break; + case 0x00C00000: + strcat(str, "std frame(sp adj 12): "); + break; + } + if ( encoding & UNWIND_ARM_FRAME_FIRST_PUSH_R4 ) + strcat(str, "r4 "); + if ( encoding & UNWIND_ARM_FRAME_FIRST_PUSH_R5 ) + strcat(str, "r5 "); + if ( encoding & UNWIND_ARM_FRAME_FIRST_PUSH_R6 ) + strcat(str, "r6 "); + + if ( encoding & 0x000000F8) + strcat(str, " / "); + if ( encoding & UNWIND_ARM_FRAME_SECOND_PUSH_R8 ) + strcat(str, "r8 "); + if ( encoding & UNWIND_ARM_FRAME_SECOND_PUSH_R9 ) + strcat(str, "r9 "); + if ( encoding & UNWIND_ARM_FRAME_SECOND_PUSH_R10 ) + strcat(str, "r10 "); + if ( encoding & UNWIND_ARM_FRAME_SECOND_PUSH_R11 ) + strcat(str, "r11 "); + if ( encoding & UNWIND_ARM_FRAME_SECOND_PUSH_R12 ) + strcat(str, "r12 "); + + if ( (encoding & UNWIND_ARM_MODE_MASK) == UNWIND_ARM_MODE_FRAME_D ) { + switch ( encoding & UNWIND_ARM_FRAME_D_REG_COUNT_MASK ) { + case 0x00000000: + strcat(str, " / d8 "); + break; + case 0x00000100: + strcat(str, " / d8,d10 "); + break; + case 0x00000200: + strcat(str, " / d8,d10,d12 "); + break; + case 0x00000300: + strcat(str, " / d8,d10,d12,d14 "); + break; + case 0x00000400: + strcat(str, " / d12,d14 / d8,d9,d10 "); + break; + case 0x00000500: + strcat(str, " / d14 / d8,d9,d10,d11,d12"); + break; + case 0x00000600: + strcat(str, " / d8,d9,d10,d11,d12,d13,d14 "); + break; + case 0x00000700: + strcat(str, " / d8,d9,d10,d11,d12,d13,d14 "); + break; + default: + strcat(str, " / unknown D register usage "); + break; + } + } + + break; + default: + if ( encoding == 0 ) + strcpy(str, "no unwind information"); + else + strcpy(str, "unsupported compact unwind"); + break; + } +} + + template <> const char* UnwindPrinter::personalityName(const macho_relocation_info* reloc) { @@ -759,6 +872,15 @@ const char* UnwindPrinter::personalityName(const macho_relocation_info +const char* UnwindPrinter::personalityName(const macho_relocation_info* reloc) +{ + //assert(reloc->r_extern() && "reloc not extern on personality column in __compact_unwind section"); + //assert((reloc->r_type() == GENERIC_RELOC_VANILLA) && "wrong reloc type on personality column in __compact_unwind section"); + const macho_nlist

& sym = fSymbols[reloc->r_symbolnum()]; + return &fStrings[sym.n_strx()]; +} + template bool UnwindPrinter::hasExernReloc(uint64_t sectionOffset, const char** personalityStr, pint_t* addr) { @@ -795,6 +917,7 @@ void UnwindPrinter::printObjectUnwindSection(bool showFunctionNames) } else { functionNameStr = this->functionName(entry->codeStart(), &offsetInFunction); + funcAddress = entry->codeStart(); } if ( offsetInFunction == 0 ) printf(" start: 0x%08llX %s\n", (uint64_t)funcAddress, functionNameStr); @@ -990,9 +1113,15 @@ static void dump(const char* path, const std::set& onlyArchs, bool s if ( UnwindPrinter::validFile(p + offset) ) UnwindPrinter::make(p + offset, size, path, showFunctionNames); else - throw "in universal file, arm64 slice does not contain arm mach-o"; + throw "in universal file, arm64 slice does not contain arm64 mach-o"; break; #endif + case CPU_TYPE_ARM: + if ( UnwindPrinter::validFile(p + offset) ) + UnwindPrinter::make(p + offset, size, path, showFunctionNames); + else + throw "in universal file, arm slice does not contain arm mach-o"; + break; default: throwf("in universal file, unknown architecture slice 0x%x\n", cputype); } @@ -1010,6 +1139,9 @@ static void dump(const char* path, const std::set& onlyArchs, bool s UnwindPrinter::make(p, length, path, showFunctionNames); } #endif + else if ( UnwindPrinter::validFile(p) && onlyArchs.count(CPU_TYPE_ARM) ) { + UnwindPrinter::make(p, length, path, showFunctionNames); + } else { throw "not a known file type"; } @@ -1040,6 +1172,8 @@ int main(int argc, const char* argv[]) else if ( strcmp(arch, "arm64") == 0 ) onlyArchs.insert(CPU_TYPE_ARM64); #endif + else if ( strcmp(arch, "armv7k") == 0 ) + onlyArchs.insert(CPU_TYPE_ARM); else throwf("unknown architecture %s", arch); } @@ -1062,6 +1196,7 @@ int main(int argc, const char* argv[]) #if SUPPORT_ARCH_arm64 onlyArchs.insert(CPU_TYPE_ARM64); #endif + onlyArchs.insert(CPU_TYPE_ARM); } // process each file