From 2ed439677ede3d870357133c39e3d903358c77e7 Mon Sep 17 00:00:00 2001 From: Duncan Ogilvie Date: Sat, 8 May 2021 14:06:57 +0200 Subject: [PATCH] Show ordinal names in the undecorated symbol column Thanks to @dmex for the suggestion --- src/dbg/module.cpp | 2 +- src/gui/Src/Gui/ZehSymbolTable.cpp | 136 ++++++++++++++++++++--------- src/gui/Src/Gui/ZehSymbolTable.h | 2 + 3 files changed, 97 insertions(+), 43 deletions(-) diff --git a/src/dbg/module.cpp b/src/dbg/module.cpp index 6ebc6c12..a776814f 100644 --- a/src/dbg/module.cpp +++ b/src/dbg/module.cpp @@ -283,7 +283,7 @@ static void ReadImportDirectory(MODINFO & Info, ULONG_PTR FileMapVA) // Import by ordinal entry.ordinal = THUNK_VAL(Info.headers, thunkData, u1.Ordinal) & 0xffff; char buf[18]; - sprintf_s(buf, "Ordinal%u", (ULONG)entry.ordinal); + sprintf_s(buf, "Ordinal#%u", (ULONG)entry.ordinal); entry.name = String((const char*)buf); } } diff --git a/src/gui/Src/Gui/ZehSymbolTable.cpp b/src/gui/Src/Gui/ZehSymbolTable.cpp index 23dc92ae..8d5f5fc9 100644 --- a/src/gui/Src/Gui/ZehSymbolTable.cpp +++ b/src/gui/Src/Gui/ZehSymbolTable.cpp @@ -52,10 +52,69 @@ QString ZehSymbolTable::getCellContent(int r, int c) return QString(); SymbolInfoWrapper info; DbgGetSymbolInfo(&mData.at(r), &info); + return symbolInfoString(&info, c); +} + +bool ZehSymbolTable::isValidIndex(int r, int c) +{ + QMutexLocker lock(&mMutex); + return r >= 0 && r < mData.size() && c >= 0 && c <= ColUndecorated; +} + +void ZehSymbolTable::sortRows(int column, bool ascending) +{ + QMutexLocker lock(&mMutex); + std::stable_sort(mData.begin(), mData.end(), [this, column, ascending](const SYMBOLPTR & a, const SYMBOLPTR & b) + { + SymbolInfoWrapper ainfo, binfo; + DbgGetSymbolInfo(&a, &ainfo); + DbgGetSymbolInfo(&b, &binfo); + switch(column) + { + case ColAddr: + return ascending ? ainfo->addr < binfo->addr : ainfo->addr > binfo->addr; + + case ColType: + return ascending ? ainfo->type < binfo->type : ainfo->type > binfo->type; + + case ColOrdinal: + // If we are sorting by ordinal make the exports the first entries + if(ainfo->type == sym_export && binfo->type != sym_export) + return ascending; + else if(ainfo->type != sym_export && binfo->type == sym_export) + return !ascending; + else + return ascending ? ainfo->ordinal < binfo->ordinal : ainfo->ordinal > binfo->ordinal; + + case ColDecorated: + { + auto acell = symbolInfoString(&ainfo, ColDecorated); + auto bcell = symbolInfoString(&binfo, ColDecorated); + int result = QString::compare(acell, bcell); + return ascending ? result < 0 : result > 0; + } + + case ColUndecorated: + { + auto acell = symbolInfoString(&ainfo, ColUndecorated); + auto bcell = symbolInfoString(&binfo, ColUndecorated); + int result = QString::compare(acell, bcell); + return ascending ? result < 0 : result > 0; + } + + default: + return false; + } + }); +} + +QString ZehSymbolTable::symbolInfoString(const SYMBOLINFO* info, int c) +{ switch(c) { case ColAddr: return ToPtrString(info->addr); + case ColType: switch(info->type) { @@ -68,12 +127,15 @@ QString ZehSymbolTable::getCellContent(int r, int c) default: __debugbreak(); } + case ColOrdinal: if(info->type == sym_export) return QString::number(info->ordinal); else return QString(); + case ColDecorated: + { char modname[MAX_MODULE_SIZE]; // Get module name for import symbols if(info->type == sym_import) @@ -84,51 +146,41 @@ QString ZehSymbolTable::getCellContent(int r, int c) return QString(modname).append('.').append(info->decoratedSymbol); } return info->decoratedSymbol; + } + case ColUndecorated: + { + if(*info->undecoratedSymbol == '\0' && strstr(info->decoratedSymbol, "Ordinal") == info->decoratedSymbol) + { + char label[MAX_LABEL_SIZE] = ""; + switch(info->type) + { + case sym_import: + { + duint wVA; + if(DbgMemRead(info->addr, &wVA, sizeof(duint))) + { + DbgGetLabelAt(wVA, SEG_DEFAULT, label); + return label; + } + } + break; + + case sym_export: + { + DbgGetLabelAt(info->addr, SEG_DEFAULT, label); + return label; + } + break; + + default: + break; + } + } return info->undecoratedSymbol; + } + default: return QString(); } } - -bool ZehSymbolTable::isValidIndex(int r, int c) -{ - QMutexLocker lock(&mMutex); - return r >= 0 && r < mData.size() && c >= 0 && c <= ColUndecorated; -} - -void ZehSymbolTable::sortRows(int column, bool ascending) -{ - QMutexLocker lock(&mMutex); - std::stable_sort(mData.begin(), mData.end(), [column, ascending](const SYMBOLPTR & a, const SYMBOLPTR & b) - { - SymbolInfoWrapper ainfo, binfo; - DbgGetSymbolInfo(&a, &ainfo); - DbgGetSymbolInfo(&b, &binfo); - switch(column) - { - case ColAddr: - return ascending ? ainfo->addr < binfo->addr : ainfo->addr > binfo->addr; - case ColType: - return ascending ? ainfo->type < binfo->type : ainfo->type > binfo->type; - case ColOrdinal: - // If we are sorting by ordinal make the exports the first entries - if(ainfo->type == sym_export && binfo->type != sym_export) - return ascending; - else if(ainfo->type != sym_export && binfo->type == sym_export) - return !ascending; - else - return ascending ? ainfo->ordinal < binfo->ordinal : ainfo->ordinal > binfo->ordinal; - case ColDecorated: - { - int result = strcmp(ainfo->decoratedSymbol, binfo->decoratedSymbol); - return ascending ? result < 0 : result > 0; - } - case ColUndecorated: - { - int result = strcmp(ainfo->undecoratedSymbol, binfo->undecoratedSymbol); - return ascending ? result < 0 : result > 0; - } - } - }); -} diff --git a/src/gui/Src/Gui/ZehSymbolTable.h b/src/gui/Src/Gui/ZehSymbolTable.h index 6e067e5c..cbddd6f2 100644 --- a/src/gui/Src/Gui/ZehSymbolTable.h +++ b/src/gui/Src/Gui/ZehSymbolTable.h @@ -35,4 +35,6 @@ private: ColDecorated, ColUndecorated }; + + QString symbolInfoString(const SYMBOLINFO* info, int c); };