Skip to content

Ghidra is a software reverse engineering (SRE) framework

Remove almost all manal memory mangement. Fix all known leaks including COM object leaks. Use wstring everywhere and as early as possible.

Paul Moranb9a7de52931f
diff --git a/Ghidra/Features/PDB/src/pdb/cpp/find.cpp b/Ghidra/Features/PDB/src/pdb/cpp/find.cpp
index 3cdb3d59ec..abc0f8c352 100644
--- a/Ghidra/Features/PDB/src/pdb/cpp/find.cpp
+++ b/Ghidra/Features/PDB/src/pdb/cpp/find.cpp
@@ -15,97 +15,113 @@
* limitations under the License.
*/
#include "find.h"
+#include <atlcomcli.h>
+#include <comutil.h>
-std::wstring findMangledName(IDiaSymbol * pFunction) {
- DWORD rva = getRVA(pFunction);
- IDiaSymbol * pSymbol = NULL;
- HRESULT hr = pSession->findSymbolByRVA(rva, SymTagPublicSymbol, &pSymbol);
+std::wstring findMangledName(IDiaSymbol& pFunction) {
+ const DWORD rva = getRVA(pFunction);
+ CComPtr<IDiaSymbol> pSymbol;
+ const HRESULT hr = pSession->findSymbolByRVA(rva, SymTagPublicSymbol, &pSymbol);
if (hr == S_OK) {
- DWORD tag = getTag(pSymbol);
+ DWORD tag = getTag(*pSymbol);
if (tag == SymTagPublicSymbol) {//do not delete
- DWORD address = getRVA(pSymbol);
+ DWORD address = getRVA(*pSymbol);
if (address == rva) {
- return getName(pSymbol);
+ return getName(*pSymbol);
}
}
}
return getName(pFunction);
}
-void findNameInNamespace( wchar_t* name, IDiaSymbol* pnamespace )
+void findNameInNamespace( const std::wstring& name, IDiaSymbol& pnamespace )
{
- wchar_t * buf = NULL;
- BSTR szNamespace;
- pnamespace->get_name( &szNamespace );
- buf = new wchar_t[ wcslen( name ) + wcslen( szNamespace ) + 3];
- wsprintfW( buf, L"%s::%s", szNamespace, name );
- IDiaEnumSymbols * pEnum;
- if ( FAILED( pGlobal->findChildren( SymTagNull, name, nsCaseSensitive, &pEnum ) ) ) {
+ bstr_t szNamespace;
+ pnamespace.get_name( szNamespace.GetAddress() );
+
+ std::wstring fullName = name + +L"::" + std::wstring(szNamespace.GetBSTR(), szNamespace.length());
+
+ CComPtr<IDiaEnumSymbols> pEnum;
+ if ( FAILED( pGlobal->findChildren( SymTagNull, fullName.c_str(), nsCaseSensitive, &pEnum ) ) ) {
fatal( "Namespace findChildren failed" );
}
+
long cnt = 0;
if ( pEnum != NULL && SUCCEEDED( pEnum->get_Count(&cnt) ) && cnt > 0 ) { // Found a name.
- printNameFromScope( name, pGlobal, pEnum );
+ printNameFromScope( name.c_str(), *pGlobal, *pEnum );
}
- delete [] buf;
}
-void findNameInEnum( wchar_t* name, IDiaSymbol* penumeration )
+void findNameInEnum( const std::wstring& name, IDiaSymbol& penumeration )
{
- IDiaEnumSymbols * pEnum;
- if ( FAILED( penumeration->findChildren( SymTagData, name, nsRegularExpression, &pEnum ) ) ) {
+ CComPtr<IDiaEnumSymbols> pEnum;
+ if ( FAILED( penumeration.findChildren( SymTagData, name.c_str(), nsRegularExpression, &pEnum ) ) ) {
fatal( "Enumeration findChildren failed" );
}
long cnt = 0;
if ( pEnum != NULL && SUCCEEDED( pEnum->get_Count(&cnt) ) && cnt > 0 ) { // Found a name.
- printNameFromScope( name, penumeration, pEnum );
+ printNameFromScope( name, penumeration, *pEnum );
}
}
-void findNameInClass( wchar_t* name, IDiaSymbol* pclass )
+void findNameInClass( const std::wstring& name, IDiaSymbol& pclass )
{
- IDiaEnumSymbols * pEnum;
- if ( FAILED( pclass->findChildren( SymTagNull, name, nsCaseSensitive, &pEnum ) ) ) {
- fatal( "Class findChildren failed" );
- }
- long cnt = 0;
- if ( pEnum != NULL && SUCCEEDED( pEnum->get_Count(&cnt) ) && cnt > 0 ) { // Found a name.
- printNameFromScope( name, pclass, pEnum );
- }
- pEnum = 0;
- // Check out the enumerations.
- IDiaSymbol * pSym;
- if ( FAILED( pclass->findChildren( SymTagEnum, NULL, nsNone, &pEnum ) ) ) {
- fatal( "Class findChildren for enums failed" );
- }
- if ( pEnum != NULL && SUCCEEDED( pEnum->get_Count(&cnt) ) && cnt > 0 ) { // Found an enum.
- DWORD celt;
- while ( SUCCEEDED( pEnum->Next( 1, &pSym, &celt ) ) && celt == 1 ) {
- findNameInEnum( name, pSym );
- pSym = 0;
- }
- }
- pEnum = 0;
- // Check out the base classes.
- if ( FAILED( pclass->findChildren( SymTagBaseClass, NULL, nsNone, &pEnum ) ) ) {
- fatal( "Class findChildren for base classes failed" );
- }
- if ( pEnum != NULL && SUCCEEDED( pEnum->get_Count(&cnt) ) && cnt > 0 ) { // Found a base class.
- DWORD celt;
- while ( SUCCEEDED( pEnum->Next( 1, &pSym, &celt ) ) && celt == 1 ) {
- IDiaSymbol * pClass;
- if ( pSym->get_type( &pClass ) == S_OK ) {
- fatal( "Getting class for a base type failed" );
- }
- if ( pClass ) {
- findNameInClass( name, pClass );
- }
- pSym = 0;
- }
- }
+ {
+ CComPtr<IDiaEnumSymbols> pEnum;
+ if (FAILED(pclass.findChildren(SymTagNull, name.c_str(), nsCaseSensitive, &pEnum))) {
+ fatal("Class findChildren failed");
+ }
+ long cnt = 0;
+ if (pEnum != NULL && SUCCEEDED(pEnum->get_Count(&cnt)) && cnt > 0) { // Found a name.
+ printNameFromScope(name, pclass, *pEnum);
+ }
+ }
+
+ {
+ // Check out the enumerations.
+ CComPtr<IDiaEnumSymbols> pEnum;
+ CComPtr<IDiaSymbol> pSym;
+ if (FAILED(pclass.findChildren(SymTagEnum, NULL, nsNone, &pEnum))) {
+ fatal("Class findChildren for enums failed");
+ }
+
+ long cnt = 0;
+ if (pEnum != NULL && SUCCEEDED(pEnum->get_Count(&cnt)) && cnt > 0) { // Found an enum.
+ DWORD celt;
+ while (SUCCEEDED(pEnum->Next(1, &pSym, &celt)) && celt == 1) {
+ findNameInEnum(name, *pSym);
+ pSym = 0;
+ }
+ }
+ }
+
+ {
+ // Check out the base classes.
+ CComPtr<IDiaEnumSymbols> pEnum;
+
+ if (FAILED(pclass.findChildren(SymTagBaseClass, NULL, nsNone, &pEnum))) {
+ fatal("Class findChildren for base classes failed");
+ }
+
+ long cnt = 0;
+ if (pEnum != NULL && SUCCEEDED(pEnum->get_Count(&cnt)) && cnt > 0) { // Found a base class.
+ DWORD celt;
+ CComPtr<IDiaSymbol> pSym;
+ while (SUCCEEDED(pEnum->Next(1, &pSym, &celt)) && celt == 1) {
+ CComPtr<IDiaSymbol> pClass;
+ if (pSym->get_type(&pClass) == S_OK) {
+ fatal("Getting class for a base type failed");
+ }
+ if (pClass) {
+ findNameInClass(name, *pClass);
+ }
+ pSym = 0;
+ }
+ }
+ }
}
-void findCppNameInScope( wchar_t* name, IDiaSymbol* pScope )
+void findCppNameInScope( const std::wstring& name, IDiaSymbol& pScope )
{
// while ( scope ) {
// Scan the scope for a symbol.
@@ -115,23 +131,23 @@ void findCppNameInScope( wchar_t* name, IDiaSymbol* pScope )
// scope = scope.parent;
// }
- wprintf( L"Finding name \"%ws\" in ", name );
+ wprintf( L"Finding name \"%ws\" in ", name.c_str() );
printScopeName( pScope );
wprintf( L"\n" );
DWORD celt;
long cnt = 0;
- IDiaSymbol * pSym;
- IDiaSymbol * pParent;
- IDiaSymbol * pscope;
- for ( pscope = pScope; pscope != NULL; ) {
+ CComPtr<IDiaSymbol> pSym;
+ CComPtr<IDiaSymbol> pParent;
+ CComPtr<IDiaSymbol> pscope;
+ for ( pscope = &pScope; pscope != NULL; ) {
IDiaEnumSymbols * pEnum;
// Local data search
- if ( FAILED( pscope->findChildren( SymTagNull, name, nsCaseSensitive, &pEnum ) ) ) {
+ if ( FAILED( pscope->findChildren( SymTagNull, name.c_str(), nsCaseSensitive, &pEnum ) ) ) {
fatal( "Local scope findChildren failed" );
}
if ( pEnum != NULL && SUCCEEDED( pEnum->get_Count(&cnt) ) && cnt > 0 ) { // Found a name.
- printNameFromScope( name, pscope, pEnum );
+ printNameFromScope( name, *pscope, *pEnum );
}
pEnum = 0;
// Look into any namespaces.
@@ -140,7 +156,7 @@ void findCppNameInScope( wchar_t* name, IDiaSymbol* pScope )
}
if ( pEnum != NULL && SUCCEEDED( pEnum->get_Count(&cnt) ) && cnt > 0 ) { // Found a namespace.
while ( SUCCEEDED( pEnum->Next( 1, &pSym, &celt ) ) && celt == 1 ) {
- findNameInNamespace( name, pSym );
+ findNameInNamespace( name, *pSym );
pSym = 0;
}
}
@@ -148,7 +164,7 @@ void findCppNameInScope( wchar_t* name, IDiaSymbol* pScope )
// Check if this is a member function.
DWORD tag = SymTagNull;
if ( SUCCEEDED( pscope->get_symTag( &tag ) ) && tag == SymTagFunction && SUCCEEDED( pscope->get_classParent( &pParent ) ) && pParent != NULL ) {
- findNameInClass( name, pParent );
+ findNameInClass( name, *pParent );
}
pParent = NULL;
// Move to lexical parent.
diff --git a/Ghidra/Features/PDB/src/pdb/cpp/iterate.cpp b/Ghidra/Features/PDB/src/pdb/cpp/iterate.cpp
index 0fd3493c18..ada39d60b6 100644
--- a/Ghidra/Features/PDB/src/pdb/cpp/iterate.cpp
+++ b/Ghidra/Features/PDB/src/pdb/cpp/iterate.cpp
@@ -16,11 +16,11 @@
*/
#include "iterate.h"
-void iterateEnumMembers(IDiaSymbol * pSymbol) {
+static void iterateEnumMembers(IDiaSymbol& pSymbol) {
DWORD celt;
IDiaEnumSymbols * pEnum;
IDiaSymbol * pMember;
- pSymbol->findChildren(SymTagNull, NULL, nsNone, &pEnum);
+ pSymbol.findChildren(SymTagNull, NULL, nsNone, &pEnum);
if (pEnum == NULL) {
return;
}
@@ -31,17 +31,17 @@ void iterateEnumMembers(IDiaSymbol * pSymbol) {
if (celt != 1) {
break;
}
- std::wstring name = getName(pMember);
- std::wstring value = getValue(pMember);
+ std::wstring name = getName(*pMember);
+ std::wstring value = getValue(*pMember);
printf("%S<member name=\"%S\" value=\"%S\" />\n", indent(12).c_str(), name.c_str(), value.c_str());
pMember = 0;
}
}
void iterateEnums() {
- DWORD celt;
- IDiaEnumSymbols * pEnum;
- IDiaSymbol * pSymbol;
+ DWORD celt = 0;
+ CComPtr<IDiaEnumSymbols> pEnum;
+ CComPtr<IDiaSymbol> pSymbol;
pGlobal->findChildren(SymTagEnum, NULL, nsNone, &pEnum);
if (pEnum == NULL) {
return;
@@ -55,18 +55,14 @@ void iterateEnums() {
break;
}
- DWORD tag = getTag(pSymbol);
+ const DWORD tag = getTag(*pSymbol);
if (tag != SymTagEnum) {//do not delete
continue;
}
- std::wstring name = getName(pSymbol);
- std::wstring type = getTypeAsString(pSymbol);
- ULONGLONG len = getLength(pSymbol);
-
- printf("%S<enum name=\"%ws\" type=\"%ws\" length=\"0x%I64x\" >\n", indent(8).c_str(), name.c_str(), type.c_str(), len);
+ printf("%S<enum name=\"%ws\" type=\"%S\" length=\"0x%I64x\" >\n", indent(8).c_str(), getName(*pSymbol).c_str(), getTypeAsString(*pSymbol).c_str(), getLength(*pSymbol));
- iterateEnumMembers(pSymbol);
+ iterateEnumMembers(*pSymbol);
printf("%S</enum>\n", indent(8).c_str());
pSymbol = 0;
@@ -74,11 +70,11 @@ void iterateEnums() {
printf("%S</enums>\n", indent(4).c_str());
}
-void iterateMembers(IDiaSymbol * pSymbol) {
- DWORD celt;
- IDiaEnumSymbols * pEnum;
- IDiaSymbol * pMember;
- pSymbol->findChildren(SymTagNull, NULL, nsNone, &pEnum);
+static void iterateMembers(IDiaSymbol& pSymbol) {
+ DWORD celt = 0;
+ CComPtr<IDiaEnumSymbols> pEnum;
+ CComPtr<IDiaSymbol> pMember;
+ pSymbol.findChildren(SymTagNull, NULL, nsNone, &pEnum);
if (pEnum == NULL) {
return;
}
@@ -90,21 +86,20 @@ void iterateMembers(IDiaSymbol * pSymbol) {
break;
}
- std::wstring name = getName(pMember);
- printf("%S<member name=\"%ws\" datatype=\"%ws\" offset=\"0x%x\" kind=\"%ws\" length=\"0x%I64x\" />\n",
+ printf("%S<member name=\"%S\" datatype=\"%S\" offset=\"0x%x\" kind=\"%S\" length=\"0x%I64x\" />\n",
indent(12).c_str(),
- name.c_str(),
- getTypeAsString(pMember).c_str(),
- getOffset(pMember),
- getKindAsString(pMember).c_str(),
- getLength(pMember));
+ getName(*pMember).c_str(),
+ getTypeAsString(*pMember).c_str(),
+ getOffset(*pMember),
+ getKindAsString(*pMember).c_str(),
+ getLength(*pMember));
}
}
void iterateDataTypes() {
- DWORD celt;
- IDiaEnumSymbols * pEnum;
- IDiaSymbol * pSymbol;
+ DWORD celt = 0;
+ CComPtr<IDiaEnumSymbols> pEnum;
+ CComPtr<IDiaSymbol> pSymbol;
pGlobal->findChildren(SymTagUDT, NULL, nsNone/*nsfCaseInsensitive|nsfUndecoratedName*/, &pEnum);
if (pEnum == NULL) {
return;
@@ -118,26 +113,23 @@ void iterateDataTypes() {
break;
}
- DWORD tag = getTag(pSymbol);
+ const DWORD tag = getTag(*pSymbol);
if (tag != SymTagUDT) {//do not delete
continue;
}
- if (getUdtKind(pSymbol) == UdtClass) {
+ if (getUdtKind(*pSymbol) == UdtClass) {
continue;
}
- std::wstring name = getName(pSymbol);
- std::wstring kind = getKindAsString(pSymbol);
- ULONGLONG len = getLength(pSymbol);
-
+ const ULONGLONG len = getLength(*pSymbol);
if ( len == 0 ) {
continue;
}
- printf("%S<datatype name=\"%ws\" kind=\"%ws\" length=\"0x%I64x\" >\n", indent(8).c_str(), name.c_str(), kind.c_str(), len);
+ printf("%S<datatype name=\"%S\" kind=\"%S\" length=\"0x%I64x\" >\n", indent(8).c_str(), getName(*pSymbol).c_str(), getKindAsString(*pSymbol).c_str(), len);
- iterateMembers(pSymbol);
+ iterateMembers(*pSymbol);
printf("%S</datatype>\n", indent(8).c_str());
pSymbol = 0;
@@ -146,9 +138,9 @@ void iterateDataTypes() {
}
void iterateTypedefs() {
- DWORD celt;
- IDiaEnumSymbols * pEnum;
- IDiaSymbol * pSymbol;
+ DWORD celt = 0;
+ CComPtr<IDiaEnumSymbols> pEnum;
+ CComPtr<IDiaSymbol> pSymbol;
pGlobal->findChildren(SymTagTypedef, NULL, nsNone/*nsfCaseInsensitive|nsfUndecoratedName*/, &pEnum);
if (pEnum == NULL) {
return;
@@ -162,14 +154,12 @@ void iterateTypedefs() {
break;
}
- DWORD tag = getTag(pSymbol);
+ const DWORD tag = getTag(*pSymbol);
if (tag != SymTagTypedef) {//do not delete
continue;
}
- std::wstring name = getName(pSymbol);
- std::wstring type = getTypeAsString(pSymbol);
- printf("%S<typedef name=\"%ws\" basetype=\"%ws\" />\n", indent(8).c_str(), name.c_str(), type.c_str());
+ printf("%S<typedef name=\"%S\" basetype=\"%S\" />\n", indent(8).c_str(), getName(*pSymbol).c_str(), getTypeAsString(*pSymbol).c_str());
pSymbol = 0;
}
@@ -177,9 +167,9 @@ void iterateTypedefs() {
}
void iterateClasses() {
- DWORD celt;
- IDiaEnumSymbols * pEnum;
- IDiaSymbol * pSymbol;
+ DWORD celt = 0;
+ CComPtr<IDiaEnumSymbols> pEnum;
+ CComPtr<IDiaSymbol> pSymbol;
pGlobal->findChildren(SymTagUDT, NULL, nsNone/*nsfCaseInsensitive|nsfUndecoratedName*/, &pEnum);
if (pEnum == NULL) {
return;
@@ -193,25 +183,23 @@ void iterateClasses() {
break;
}
- DWORD tag = getTag(pSymbol);
+ const DWORD tag = getTag(*pSymbol);
if (tag != SymTagUDT) {//do not delete
continue;
}
- if (getUdtKind(pSymbol) != UdtClass) {
+ if (getUdtKind(*pSymbol) != UdtClass) {
continue;
}
- std::wstring name = getName(pSymbol);
- ULONGLONG len = getLength(pSymbol);
-
+ const ULONGLONG len = getLength(*pSymbol);
if ( len == 0 ) {
continue;
}
- printf("%S<class name=\"%ws\" length=\"0x%I64x\" >\n", indent(8).c_str(), name.c_str(), len);
+ printf("%S<class name=\"%S\" length=\"0x%I64x\" >\n", indent(8).c_str(), getName(*pSymbol).c_str(), len);
- iterateMembers(pSymbol);
+ iterateMembers(*pSymbol);
printf("%S</class>\n", indent(8).c_str());
pSymbol = 0;
@@ -221,29 +209,29 @@ void iterateClasses() {
void dumpFunctionStackVariables( DWORD rva )
{
- IDiaSymbol * pBlock;
+ CComPtr<IDiaSymbol> pBlock;
if ( FAILED( pSession->findSymbolByRVA( rva, SymTagBlock, &pBlock ) ) ) {
fatal( "Failed to find symbols by RVA" );
}
for ( ; pBlock != NULL; ) {
- IDiaEnumSymbols * pEnum;
+ CComPtr<IDiaEnumSymbols> pEnum;
// Local data search
if ( FAILED( pBlock->findChildren( SymTagNull, NULL, nsNone, &pEnum ) ) ) {
fatal( "Local scope findChildren failed" );
}
- IDiaSymbol * pSymbol;
+ CComPtr<IDiaSymbol> pSymbol;
DWORD tag;
DWORD celt;
while ( pEnum != NULL && SUCCEEDED( pEnum->Next( 1, &pSymbol, &celt ) ) && celt == 1 ) {
pSymbol->get_symTag( &tag );
if ( tag == SymTagData ) {
- printf("%S<stack_variable name=\"%ws\" kind=\"%ws\" offset=\"0x%x\" datatype=\"%ws\" length=\"0x%I64x\" />\n",
+ printf("%S<stack_variable name=\"%S\" kind=\"%S\" offset=\"0x%x\" datatype=\"%S\" length=\"0x%I64x\" />\n",
indent(12).c_str(),
- getName(pSymbol).c_str(),
- getKindAsString(pSymbol).c_str(),
- getOffset(pSymbol),
- getTypeAsString(pSymbol).c_str(),
- getLength(pSymbol));
+ getName(*pSymbol).c_str(),
+ getKindAsString(*pSymbol).c_str(),
+ getOffset(*pSymbol),
+ getTypeAsString(*pSymbol).c_str(),
+ getLength(*pSymbol));
}
else if ( tag == SymTagAnnotation ) {
/*
@@ -272,7 +260,7 @@ void dumpFunctionStackVariables( DWORD rva )
break;
}
// Move to lexical parent.
- IDiaSymbol * pParent;
+ CComPtr<IDiaSymbol> pParent;
if ( SUCCEEDED( pBlock->get_lexicalParent( &pParent ) ) && pParent != NULL ) {
pBlock = pParent;
}
@@ -293,11 +281,11 @@ void dumpFunctionLines( IDiaSymbol* pSymbol, IDiaSession* pSession )
if ( isect == 0 || length <= 0 ) {
return;
}
- IDiaEnumLineNumbers * pLines;
+ CComPtr<IDiaEnumLineNumbers> pLines;
if (pSession->findLinesByAddr( isect, offset, static_cast<DWORD>( length ), &pLines ) < 0) {
return;
}
- IDiaLineNumber * pLine;
+ CComPtr<IDiaLineNumber> pLine;
DWORD celt;
while ( 1 ) {
if (pLines->Next( 1, &pLine, &celt ) < 0) {
@@ -307,14 +295,14 @@ void dumpFunctionLines( IDiaSymbol* pSymbol, IDiaSession* pSession )
break;
}
- IDiaSymbol * pComp;
+ CComPtr<IDiaSymbol> pComp;
pLine->get_compiland( &pComp );
- IDiaSourceFile * pSrc;
+ CComPtr<IDiaSourceFile> pSrc;
pLine->get_sourceFile( &pSrc );
- BSTR sourceFileName = NULL;
- pSrc->get_fileName( &sourceFileName );
+ bstr_t sourceFileName ;
+ pSrc->get_fileName( sourceFileName.GetAddress() );
DWORD addr;
pLine->get_relativeVirtualAddress( &addr );
@@ -324,17 +312,17 @@ void dumpFunctionLines( IDiaSymbol* pSymbol, IDiaSession* pSession )
DWORD end;
pLine->get_lineNumberEnd( &end );
- printf("%S<line_number source_file=\"%ws\" start=\"0x%x\" end=\"0x%x\" addr=\"0x%x\" /> \n",
- indent(12).c_str(), sourceFileName, start, end, addr);
+ printf("%S<line_number source_file=\"%S\" start=\"0x%x\" end=\"0x%x\" addr=\"0x%x\" /> \n",
+ indent(12).c_str(), sourceFileName.GetBSTR(), start, end, addr);
pLine = NULL;
}
}
void iterateFunctions() {
- DWORD celt;
- IDiaEnumSymbols * pEnum;
- IDiaSymbol * pSymbol;
+ DWORD celt = 0;
+ CComPtr<IDiaEnumSymbols> pEnum;
+ CComPtr<IDiaSymbol> pSymbol;
pGlobal->findChildren(SymTagFunction, NULL, nsNone, &pEnum);
if (pEnum == NULL) {
return;
@@ -348,16 +336,14 @@ void iterateFunctions() {
break;
}
- DWORD tag = getTag(pSymbol);
+ const DWORD tag = getTag(*pSymbol);
if (tag != SymTagFunction) {//do not delete
continue;
}
- std::wstring name = findMangledName(pSymbol);
- DWORD address = getRVA(pSymbol);
- ULONGLONG len = getLength(pSymbol);
+ const DWORD address = getRVA(*pSymbol);
- printf("%S<function name=\"%ws\" address=\"0x%x\" length=\"0x%I64x\">\n", indent(8).c_str(), name.c_str(), address, len);
+ printf("%S<function name=\"%S\" address=\"0x%x\" length=\"0x%I64x\">\n", indent(8).c_str(), findMangledName(*pSymbol).c_str(), address, getLength(*pSymbol));
dumpFunctionStackVariables(address);
dumpFunctionLines(pSymbol, pSession);
@@ -370,8 +356,8 @@ void iterateFunctions() {
}
void iterateSymbolTable(IDiaEnumSymbols * pSymbols) {
- DWORD celt;
- IDiaSymbol * pSymbol;
+ DWORD celt = 0;
+ CComPtr<IDiaSymbol> pSymbol;
while ( 1 ) {
if (pSymbols->Next( 1, &pSymbol, &celt ) < 0) {
@@ -381,21 +367,21 @@ void iterateSymbolTable(IDiaEnumSymbols * pSymbols) {
break;
}
- std::wstring name = getName(pSymbol);
+ const std::wstring name = getName(*pSymbol);
if (name.empty()) {
continue;
}
printf("%S", indent(12).c_str());
- printf("<symbol name=\"%ws\" ", name.c_str());
- printf("address=\"0x%x\" ", getRVA(pSymbol));
- printf("length=\"0x%I64x\" ", getLength(pSymbol));
- printf("tag=\"%ws\" ", getTagAsString(pSymbol));
- printf("kind=\"%ws\" ", getKindAsString(pSymbol).c_str());
- printf("index=\"0x%x\" ", getIndex(pSymbol));
- printf("undecorated=\"%ws\" ", getUndecoratedName(pSymbol).c_str());
- printf("value=\"%ws\" ", getValue(pSymbol).c_str());
- printf("datatype=\"%ws\" ", getTypeAsString(pSymbol).c_str());
+ printf("<symbol name=\"%S\" ", name.c_str());
+ printf("address=\"0x%x\" ", getRVA(*pSymbol));
+ printf("length=\"0x%I64x\" ", getLength(*pSymbol));
+ printf("tag=\"%S\" ", getTagAsString(*pSymbol).c_str());
+ printf("kind=\"%S\" ", getKindAsString(*pSymbol).c_str());
+ printf("index=\"0x%x\" ", getIndex(*pSymbol));
+ printf("undecorated=\"%ws\" ", getUndecoratedName(*pSymbol).c_str());
+ printf("value=\"%S\" ", getValue(*pSymbol).c_str());
+ printf("datatype=\"%S\" ", getTypeAsString(*pSymbol).c_str());
printf(" />\n");
pSymbol = NULL;
@@ -403,16 +389,16 @@ void iterateSymbolTable(IDiaEnumSymbols * pSymbols) {
}
void iterateSourceFiles(IDiaEnumSourceFiles * pSourceFiles) {
- HRESULT hr;
- DWORD celt;
- IDiaSourceFile * pSourceFile;
+ HRESULT hr = S_OK;
+ DWORD celt = 0;
+ CComPtr<IDiaSourceFile> pSourceFile;
while ( SUCCEEDED( hr = pSourceFiles->Next( 1, &pSourceFile, &celt ) ) && celt == 1 ) {
- BSTR name;
- pSourceFile->get_fileName( &name );
- DWORD id;
+ bstr_t name;
+ pSourceFile->get_fileName( name.GetAddress() );
+ DWORD id = 0;
pSourceFile->get_uniqueId( &id );
- if ( name != NULL ) {
- printf("%S<source_file name=\"%ws\" id=\"0x%x\" /> \n", indent(12).c_str(), name, id);
+ if ( name.GetAddress() != NULL ) {
+ printf("%S<source_file name=\"%ws\" id=\"0x%x\" /> \n", indent(12).c_str(), name.GetBSTR(), id);
}
pSourceFile = NULL;
}
@@ -426,12 +412,12 @@ void iterateSourceFiles(IDiaEnumSourceFiles * pSourceFiles) {
* information in the segment map.
*/
void iterateSegments(IDiaEnumSegments * pSegments) {
- HRESULT hr;
- DWORD celt;
- IDiaSegment * pSegment;
+ HRESULT hr = S_OK;
+ DWORD celt = 0;
+ CComPtr<IDiaSegment> pSegment;
while ( SUCCEEDED( hr = pSegments->Next( 1, &pSegment, &celt ) ) && celt == 1 ) {
- DWORD rva;
- DWORD seg;
+ DWORD rva = 0;
+ DWORD seg = 0;
pSegment->get_addressSection( &seg );
pSegment->get_relativeVirtualAddress( &rva );
printf("%S<segment number=\"%i\" address=\"0x%x\" /> \n", indent(12).c_str(), seg, rva);
@@ -445,9 +431,9 @@ void iterateSegments(IDiaEnumSegments * pSegments) {
* to the image by a compiland.
*/
void iterateSections(IDiaEnumSectionContribs * pSecContribs) {
- DWORD celt;
- IDiaSymbol * pSym = NULL;
- IDiaSectionContrib * pSecContrib;
+ DWORD celt = 0;
+ CComPtr<IDiaSymbol> pSym ;
+ CComPtr<IDiaSectionContrib> pSecContrib;
while ( 1 ) {
if (pSecContribs->Next( 1, &pSecContrib, &celt ) < 0 ) {
@@ -456,15 +442,15 @@ void iterateSections(IDiaEnumSectionContribs * pSecContribs) {
if (celt != 1) {
break;
}
- DWORD rva;
+ DWORD rva = 0;
if ( pSecContrib->get_relativeVirtualAddress( &rva ) == S_OK ) {
if ( pSession->findSymbolByRVA( rva, SymTagNull, &pSym ) != S_OK ) {
pSym = NULL;
}
}
else {
- DWORD isect;
- DWORD offset;
+ DWORD isect = 0;
+ DWORD offset = 0;
pSecContrib->get_addressSection( &isect );
pSecContrib->get_addressOffset( &offset );
pSecContrib = NULL;
@@ -476,10 +462,10 @@ void iterateSections(IDiaEnumSectionContribs * pSecContribs) {
printf("%S<section_contrib address=\"0x%x\" /> \n", indent(12).c_str(), rva);
}
else {
- std::wstring name = getName(pSym);
- std::wstring tag = getTagAsString(pSym);
+ std::wstring name = getName(*pSym);
+ std::wstring tag = getTagAsString(*pSym);
- printf("%S<section_contrib address=\"0x%x\" name=\"%ws\" tag=\"%ws\" /> \n",
+ printf("%S<section_contrib address=\"0x%x\" name=\"%S\" tag=\"%S\" /> \n",
indent(12).c_str(), rva, name.c_str(), tag.c_str());
}
}
@@ -489,11 +475,11 @@ void iterateSections(IDiaEnumSectionContribs * pSecContribs) {
* Accesses the program source code stored in the DIA data source.
*/
void iterateInjectedSource(IDiaEnumInjectedSources * pInjectedSrcs) {
- DWORD celt;
- IDiaInjectedSource* pInjectedSrc;
+ DWORD celt = 0;
+ CComPtr<IDiaInjectedSource> pInjectedSrc;
while ( 1 ) {
- HRESULT hr = pInjectedSrcs->Next( 1, &pInjectedSrc, &celt );
+ const HRESULT hr = pInjectedSrcs->Next( 1, &pInjectedSrc, &celt );
if (hr < 0) {
break;
}
@@ -501,11 +487,11 @@ void iterateInjectedSource(IDiaEnumInjectedSources * pInjectedSrcs) {
break;
}
- BSTR filename;
- pInjectedSrc->get_filename(&filename);
+ bstr_t filename;
+ pInjectedSrc->get_filename(filename.GetAddress());
- BSTR objectname;
- pInjectedSrc->get_objectFilename(&objectname);
+ bstr_t objectname;
+ pInjectedSrc->get_objectFilename(objectname.GetAddress());
DWORD crc;
pInjectedSrc->get_crc(&crc);
@@ -515,8 +501,8 @@ void iterateInjectedSource(IDiaEnumInjectedSources * pInjectedSrcs) {
printf("%S<injected_source filename=\"%ws\" objectname=\"%ws\" crc=\"0x%x\" length=\"0x%I64x\" />\n",
indent(8).c_str(),
- filename,
- objectname,
+ filename.GetBSTR(),
+ objectname.GetBSTR(),
crc,
length);
}
@@ -528,8 +514,8 @@ void iterateInjectedSource(IDiaEnumInjectedSources * pInjectedSrcs) {
* address and block length.
*/
void iterateFrameData(IDiaEnumFrameData * pEnumFrameData) {
- DWORD celt;
- IDiaFrameData * pFrameData;
+ DWORD celt = 0;
+ CComPtr<IDiaFrameData> pFrameData;
while ( 1 ) {
HRESULT hr = pEnumFrameData->Next( 1, &pFrameData, &celt );
@@ -543,12 +529,12 @@ void iterateFrameData(IDiaEnumFrameData * pEnumFrameData) {
}
}
-int iterateTables(const bool printAll) {
+int iterateTables(bool printAll) {
printf("%S<tables>\n", indent(4).c_str());
- HRESULT hr;
- DWORD celt;
+ HRESULT hr = S_OK;
+ DWORD celt = 0;
- IDiaEnumTables * pTables;
+ CComPtr<IDiaEnumTables> pTables;
hr = pSession->getEnumTables( &pTables );
if ( hr < 0) {
@@ -564,36 +550,36 @@ int iterateTables(const bool printAll) {
if (celt != 1) {
break;
}
- BSTR name;
- pTable->get_name( &name );
+ bstr_t name;
+ pTable->get_name( name.GetAddress() );
- printf("%S<table name=\"%ws\">\n", indent(8).c_str(), name );
+ printf("%S<table name=\"%ws\">\n", indent(8).c_str(), name.GetBSTR() );
- IDiaEnumSymbols * pSymbols;
- IDiaEnumSourceFiles * pSourceFiles;
- IDiaEnumSegments * pSegments;
- IDiaEnumSectionContribs * pSecContribs;
- IDiaEnumInjectedSources * pInjectedSrcs;
- IDiaEnumFrameData * pEnumFrameData;
+ CComPtr<IDiaEnumSymbols> pSymbols;
+ CComPtr<IDiaEnumSourceFiles> pSourceFiles;
+ CComPtr<IDiaEnumSegments> pSegments;
+ CComPtr<IDiaEnumSectionContribs> pSecContribs;
+ CComPtr<IDiaEnumInjectedSources> pInjectedSrcs;
+ CComPtr<IDiaEnumFrameData> pEnumFrameData;
- if ( SUCCEEDED( pTable->QueryInterface( _uuidof( IDiaEnumSymbols ), (void**)&pSymbols ) ) ) {
+ if ( SUCCEEDED( pTable->QueryInterface(IID_PPV_ARGS(&pSymbols) ) ) ) {
iterateSymbolTable(pSymbols);
}
- else if ( SUCCEEDED( pTable->QueryInterface( _uuidof( IDiaEnumSourceFiles ), (void**)&pSourceFiles ) ) ) {
+ else if ( SUCCEEDED( pTable->QueryInterface(IID_PPV_ARGS(&pSourceFiles) ) ) ) {
iterateSourceFiles(pSourceFiles);
}
- else if ( SUCCEEDED( pTable->QueryInterface( _uuidof( IDiaEnumSegments ), (void**)&pSegments ) ) ) {
+ else if ( SUCCEEDED( pTable->QueryInterface(IID_PPV_ARGS(&pSegments)) ) ) {
iterateSegments(pSegments);
}
- else if ( SUCCEEDED( pTable->QueryInterface( _uuidof( IDiaEnumSectionContribs ), (void**)&pSecContribs ) ) ) {
+ else if ( SUCCEEDED( pTable->QueryInterface(IID_PPV_ARGS(&pSecContribs) ) ) ) {
if (printAll) {
iterateSections(pSecContribs);
}
}
- else if ( SUCCEEDED( pTable->QueryInterface( _uuidof( IDiaEnumInjectedSources ), (void**)&pInjectedSrcs ) ) ) {
+ else if ( SUCCEEDED( pTable->QueryInterface(IID_PPV_ARGS(&pInjectedSrcs) ) ) ) {
iterateInjectedSource(pInjectedSrcs);
}
- else if ( SUCCEEDED( pTable->QueryInterface( _uuidof( IDiaEnumFrameData ), (void**)&pEnumFrameData ) ) ) {
+ else if ( SUCCEEDED( pTable->QueryInterface(IID_PPV_ARGS(&pEnumFrameData) ) ) ) {
iterateFrameData(pEnumFrameData);
}
diff --git a/Ghidra/Features/PDB/src/pdb/cpp/pdb.cpp b/Ghidra/Features/PDB/src/pdb/cpp/pdb.cpp
index 90384dc40c..4b07a46923 100644
--- a/Ghidra/Features/PDB/src/pdb/cpp/pdb.cpp
+++ b/Ghidra/Features/PDB/src/pdb/cpp/pdb.cpp
@@ -86,10 +86,10 @@ int init(const char * szFilename, const char * szSignature, const char * szAge)
checkErr(hr);
delete [] wszFilename;
- if (pSource->openSession( &pSession ) < 0) {
+ if (FAILED(pSource->openSession( &pSession ))) {
fatal("Unable to open session\n");
}
- if (pSession->get_globalScope( &pGlobal ) < 0) {
+ if (FAILED(pSession->get_globalScope( &pGlobal ))) {
fatal("Unable to get global scope\n");
}
@@ -99,7 +99,7 @@ int init(const char * szFilename, const char * szSignature, const char * szAge)
fatal("Unable to get global symbol index\n");
}
- std::wstring exename = getName(pGlobal);
+ std::wstring exename = getName(*pGlobal);
GUID currGUID;
BSTR guidString;
diff --git a/Ghidra/Features/PDB/src/pdb/cpp/print.cpp b/Ghidra/Features/PDB/src/pdb/cpp/print.cpp
index f27a2e9f1e..8546c5bf4e 100644
--- a/Ghidra/Features/PDB/src/pdb/cpp/print.cpp
+++ b/Ghidra/Features/PDB/src/pdb/cpp/print.cpp
@@ -17,6 +17,9 @@
#include "find.h"
#include "print.h"
#include "symbol.h"
+#include <atlcomcli.h>
+#include <comutil.h>
+#include <vector>
// Returns a mixture of static strings and allocated strings.
// Abandon all hope of memory management, ye who enter here
@@ -103,42 +106,35 @@ std::wstring printVariant( VARIANT & v ) {
}
}
-void printBound( IDiaSymbol* pBound ) {
+void printBound( IDiaSymbol& pBound ) {
DWORD tag = 0;
- BSTR name;
- DWORD kind;
- pBound->get_symTag( &tag );
- pBound->get_locationType( &kind );
+ bstr_t name;
+ DWORD kind = 0;
+ pBound.get_symTag( &tag );
+ pBound.get_locationType( &kind );
if ( tag == SymTagData && kind == LocIsConstant ) {
//TODO
//CComVariant v;
//pBound->get_value( &v );
//printVariant( v );
}
- else if ( pBound->get_name( &name ) == S_OK ) {
- printf( "%ws", name );
+ else if ( pBound.get_name( name.GetAddress() ) == S_OK ) {
+ printf( "%ws", name.GetBSTR() );
}
}
-std::wstring printType( IDiaSymbol * pType, BSTR suffix ) {
+std::wstring printType( IDiaSymbol * pType, const std::wstring& suffix ) {
if (pType == NULL) {
return L"";
}
- std::wstring name = getName(pType);
- DWORD tag = getTag(pType);
+ std::wstring name = getName(*pType);
+ DWORD tag = getTag(*pType);
if ( tag == SymTagPointerType ) {
- IDiaSymbol * pBaseType;
+ CComPtr<IDiaSymbol> pBaseType;
if ( pType->get_type( &pBaseType ) == S_OK ) {
- size_t length = wcslen(suffix) + 3; // length of: suffix + " *\0"
- wchar_t * str = (wchar_t *)calloc(length, sizeof(wchar_t));
- if (str != NULL) {
- swprintf_s(str, length, L"%ws *", suffix);
- std::wstring ret = printType(pBaseType, (BSTR)str);
- free(str);
- return ret;
- }
+ return printType(pBaseType, suffix + L" *");
}
else {
return L"";
@@ -146,35 +142,23 @@ std::wstring printType( IDiaSymbol * pType, BSTR suffix ) {
}
if ( tag == SymTagBaseType ) {
- std::wstring bt = getBaseTypeAsString( pType );
- size_t length = bt.length() + wcslen(suffix) + 1; // length of: bt + suffix + "\0"
- wchar_t * str = (wchar_t *)calloc(length, sizeof(wchar_t));
- if (str != NULL) {
- swprintf_s(str, length, L"%ws%ws", bt.c_str(), suffix);
- std::wstring ret(str);
- free(str);
- return ret;
- }
+ return getBaseTypeAsString( *pType ) + suffix;
}
if ( tag == SymTagArrayType ) {
- IDiaSymbol * pBaseType = getType( pType );
+ CComPtr<IDiaSymbol> pBaseType = getType( *pType );
if ( pBaseType == NULL ) {
return L"";
}
- ULONGLONG lenArray = getLength( pType );
- ULONGLONG lenElem = getLength( pBaseType );
+ ULONGLONG lenArray = getLength( *pType );
+ ULONGLONG lenElem = getLength( *pBaseType );
if (lenElem == 0) {//prevent divide by zero...
lenElem = lenArray;
}
- size_t strLen = wcslen(suffix) + 64 + 3; // length of suffix + wag_for_numeric_value + "[]\0"
- wchar_t * str = (wchar_t *)calloc(strLen, sizeof(wchar_t));
- if (str != NULL) {
- swprintf_s(str, strLen, L"%ws[%I64d]", suffix, lenArray / lenElem);
- std::wstring ret = printType(pBaseType, (BSTR)str);
- free(str);
- return ret;
- }
+ size_t strLen = suffix.length() + 64 + 3; // length of suffix + wag_for_numeric_value + "[]\0"
+ std::vector<wchar_t> str(strLen);
+ swprintf_s(str.data(), strLen, L"%ws[%I64d]", suffix.c_str(), lenArray / lenElem);
+ return printType(pBaseType, str.data());
}
if ( tag == SymTagFunctionType ) {
@@ -182,58 +166,45 @@ std::wstring printType( IDiaSymbol * pType, BSTR suffix ) {
}
if ( tag == SymTagCustomType ) {
- DWORD id;
- DWORD rec;
- GUID guid;
+ DWORD id = 0;
+ DWORD rec = 0;
+ GUID guid = {};
if (pType->get_guid(&guid) == S_OK) {
- int maxGUIDStrLen = 64;
- wchar_t* guidStr = (wchar_t*)calloc((size_t)maxGUIDStrLen, sizeof(wchar_t));
- if (guidStr != NULL) {
- if (StringFromGUID2(guid, guidStr, maxGUIDStrLen) > 0) {
- std::wstring ret(guidStr);
- free(guidStr);
- return ret;
- }
+ const int maxGUIDStrLen = 64 + 1;
+ std::vector<wchar_t> guidStr(maxGUIDStrLen);
+ if (StringFromGUID2(guid, guidStr.data(), maxGUIDStrLen) > 0) {
+ return guidStr.data();
}
}
else if ( pType->get_oemId( &id ) == S_OK && pType->get_oemSymbolId( &rec ) == S_OK ) {
- size_t strLen = 256; // wag_for_2_hex_numbers "0xNNNNN:0xNNNNN"
- wchar_t * str = (wchar_t *)calloc(strLen, sizeof(wchar_t));
+ const size_t strLen = 256; // wag_for_2_hex_numbers "0xNNNNN:0xNNNNN"
+ wchar_t str[strLen] = {};
if (str != NULL) {
- swprintf_s(str, strLen, L"0x%x:0x%x", id, rec);
- std::wstring ret(str);
- free(str);
- return ret;
+ swprintf_s(str, L"0x%x:0x%x", id, rec);
+ return str;
}
}
return L"";
}
if ( !name.empty() ) {
- size_t length = name.length() + wcslen(suffix) + 1; // length of name + suffix + "\0"
- wchar_t * str = (wchar_t *)calloc(length, sizeof(wchar_t));
- if (str != NULL) {
- swprintf_s(str, length, L"%ws%ws", name.c_str(), suffix);
- std::wstring ret(str);
- free(str);
- return ret;
- }
+ return name + suffix;
}
return L"Undefined";
}
-void printScopeName( IDiaSymbol* pscope ) {
- printf("<scope name=\"%ws\" tag=\"%ws\" />\n", getName( pscope ).c_str(), getTagAsString( pscope ));
+void printScopeName( IDiaSymbol& pscope ) {
+ printf("<scope name=\"%ws\" tag=\"%ws\" />\n", getName( pscope ).c_str(), getTagAsString( pscope ).c_str());
}
-void printNameFromScope( wchar_t* name, IDiaSymbol* pscope, IDiaEnumSymbols* pEnum ) {
+void printNameFromScope( const std::wstring& /*name*/, IDiaSymbol& pscope, IDiaEnumSymbols& pEnum ) {
- IDiaSymbol * pSym;
- DWORD celt;
- while ( SUCCEEDED( pEnum->Next( 1, &pSym, &celt ) ) && celt == 1 ) {
- std::wstring name = getName( pSym );
- std::wstring tag = getTagAsString( pSym );
+ CComPtr<IDiaSymbol> pSym;
+ DWORD celt = 0;
+ while ( SUCCEEDED( pEnum.Next( 1, &pSym, &celt ) ) && celt == 1 ) {
+ std::wstring name = getName( *pSym );
+ std::wstring tag = getTagAsString( *pSym );
wprintf( L"\t%ws %ws found in ", tag.c_str(), name.c_str() );
printScopeName( pscope );
wprintf( L"\n" );
diff --git a/Ghidra/Features/PDB/src/pdb/cpp/symbol.cpp b/Ghidra/Features/PDB/src/pdb/cpp/symbol.cpp
index ef66f26007..4cf94302a9 100644
--- a/Ghidra/Features/PDB/src/pdb/cpp/symbol.cpp
+++ b/Ghidra/Features/PDB/src/pdb/cpp/symbol.cpp
@@ -17,8 +17,13 @@
#include "xml.h"
#include "print.h"
#include "pdb.h"
+#include <vector>
+#include <comutil.h>
+#include <atlcomcli.h>
-static wchar_t* SYMBOL_TAG_STRINGS [] = {
+#pragma comment(lib, "comsuppw.lib") // bstr_t
+
+const static std::wstring SYMBOL_TAG_STRINGS [] = {
L"",
L"Executable",
L"Compiland",
@@ -112,14 +117,14 @@ static bool fTagScopes [] = {
false
};
-static wchar_t * UDT_KIND_STRINGS [] = {
+const static std::wstring UDT_KIND_STRINGS [] = {
L"Structure",
L"Class",
L"Union",
L"Interface"
};
-static wchar_t* DATA_KIND_STRINGS [] = {
+const static std::wstring DATA_KIND_STRINGS [] = {
L"Unknown",
L"Local",
L"StaticLocal",
@@ -132,7 +137,7 @@ static wchar_t* DATA_KIND_STRINGS [] = {
L"Constant"
};
-static wchar_t* BASIC_TYPE_STRINGS [] = {
+const static std::wstring BASIC_TYPE_STRINGS [] = {
L"<NoType>",
L"void",
L"char",
@@ -169,120 +174,102 @@ static wchar_t* BASIC_TYPE_STRINGS [] = {
L"char32_t"
};
-std::wstring getName(IDiaSymbol * pSymbol) {
- BSTR name;
- std::wstring escapedName;
- DWORD symIndexId, locType;
- ULONGLONG len;
- if (pSymbol->get_name( &name ) == 0) {
- if (wcscmp(name, L"") == 0) {
- size_t length = 7; // length of: "NONAME\0"
- wchar_t * str = (wchar_t *)calloc(length, sizeof(wchar_t));
- if (str != NULL) {
- swprintf_s(str, length, L"NONAME");
- escapedName = escapeXmlEntities(str);
- free(str);
- }
- } else
- if(wcsstr(name, L"unnamed-tag") != NULL){
- if(pSymbol->get_symIndexId(&symIndexId) != 0){
- symIndexId = 0;
- }
- size_t length = 16; // length of: "<unnamed_NNNN>\0" + 1 extra
- wchar_t * str = (wchar_t *)calloc(length, sizeof(wchar_t));
- if (str != NULL) {
- swprintf_s(str, length, L"<unnamed_%04x>", symIndexId);
- escapedName = escapeXmlEntities(str);
- free(str);
- }
- } else
- if(pSymbol->get_locationType(&locType) == 0 &&
- locType == LocIsBitField &&
- pSymbol->get_length(&len) == 0){
- size_t length = wcslen(name) + 4 + 32; // length of: name + ":0x\0" + wag_hex_numeric_str_len
- wchar_t * str = (wchar_t *)calloc(length, sizeof(wchar_t));
- if (str != NULL) {
- swprintf_s(str, length, L"%ws:0x%I64x", name, len);
- escapedName = escapeXmlEntities(str);
- free(str);
- }
- } else {
- escapedName = escapeXmlEntities(name);
- }
- SysFreeString(name);
- return escapedName;
- }
+std::wstring getName(IDiaSymbol& pSymbol) {
+ bstr_t name;
+ if (SUCCEEDED(pSymbol.get_name(name.GetAddress()))) {
+ const std::wstring wstrName = std::wstring(name.GetBSTR(), name.length());
+ if (wstrName.empty()) {
+ return escapeXmlEntities(L"NONAME");
+ }
+
+ if (wstrName.find(L"unnamed-tag") != std::wstring::npos) {
+ DWORD symIndexId = 0;
+ if (FAILED(pSymbol.get_symIndexId(&symIndexId))) {
+ symIndexId = 0;
+ }
+ const size_t length = 16; // length of: "<unnamed_NNNN>\0" + 1 extra
+ std::vector<wchar_t> str(16);
+ swprintf_s(str.data(), length, L"<unnamed_%04x>", symIndexId);
+ return escapeXmlEntities(str.data());
+ }
+
+ DWORD locType = 0;
+ ULONGLONG len = 0;
+ if (SUCCEEDED(pSymbol.get_locationType(&locType)) &&
+ locType == LocIsBitField &&
+ SUCCEEDED(pSymbol.get_length(&len))) {
+ const size_t length = wstrName.length() + 4 + 32; // length of: name + ":0x\0" + wag_hex_numeric_str_len
+ std::vector<wchar_t> str(length);
+ swprintf_s(str.data(), length, L"%ws:0x%I64x", wstrName.c_str(), len);
+ return escapeXmlEntities(str.data());
+ }
+
+ return escapeXmlEntities(wstrName);
+ }
return std::wstring();
}
-std::wstring getUndecoratedName(IDiaSymbol * pSymbol) {
- BSTR name;
- if (pSymbol->get_undecoratedName( &name ) == 0) {
- std::wstring escapedName = escapeXmlEntities(name);
- SysFreeString(name);
- return escapedName;
+
+std::wstring getUndecoratedName(IDiaSymbol& pSymbol) {
+ bstr_t name;
+ if (SUCCEEDED(pSymbol.get_undecoratedName( name.GetAddress() ))) {
+ return escapeXmlEntities(std::wstring(name.GetBSTR(), name.length()));
}
return L"";
}
-DWORD getRVA(IDiaSymbol * pSymbol) {
- DWORD rva;
- pSymbol->get_relativeVirtualAddress( &rva );
+DWORD getRVA(IDiaSymbol& pSymbol) {
+ DWORD rva = 0;
+ pSymbol.get_relativeVirtualAddress( &rva );
return rva;
}
-ULONGLONG getLength(IDiaSymbol * pSymbol) {
+ULONGLONG getLength(IDiaSymbol& pSymbol) {
ULONGLONG len = 0;
- pSymbol->get_length( &len );
+ pSymbol.get_length( &len );
return len;
}
-DWORD getTag(IDiaSymbol * pSymbol) {
- DWORD tag;
- pSymbol->get_symTag( &tag );
+DWORD getTag(IDiaSymbol& pSymbol) {
+ DWORD tag = 0;
+ pSymbol.get_symTag( &tag );
return tag;
}
-const wchar_t* getTagAsString(IDiaSymbol * pSymbol) {
+
+std::wstring getTagAsString(IDiaSymbol& pSymbol) {
return SYMBOL_TAG_STRINGS[getTag(pSymbol)];
}
-DWORD getKind(IDiaSymbol * pSymbol) {
- DWORD kind;
- pSymbol->get_dataKind( &kind );
+DWORD getKind(IDiaSymbol& pSymbol) {
+ DWORD kind = 0;
+ pSymbol.get_dataKind( &kind );
return kind;
}
-DWORD getUdtKind(IDiaSymbol * pSymbol) {
- DWORD kind;
- pSymbol->get_udtKind( &kind );
+DWORD getUdtKind(IDiaSymbol& pSymbol) {
+ DWORD kind = 0;
+ pSymbol.get_udtKind( &kind );
return kind;
}
-std::wstring getKindAsString(IDiaSymbol * pSymbol) {
- DWORD tag = getTag(pSymbol);
+std::wstring getKindAsString(IDiaSymbol& pSymbol) {
+ const DWORD tag = getTag(pSymbol);
if (tag == SymTagUDT) {
return UDT_KIND_STRINGS[getUdtKind(pSymbol)];
}
return DATA_KIND_STRINGS[getKind(pSymbol)];
}
-/*
-DWORD getSection(IDiaSymbol * pSymbol) {
- DWORD section;
- pSymbol->get_addressSection( &section );
- return section;
-}
-*/
-LONG getOffset(IDiaSymbol * pSymbol) {
- LONG offset;
- pSymbol->get_offset( &offset );
+
+LONG getOffset(IDiaSymbol& pSymbol) {
+ LONG offset = 0;
+ pSymbol.get_offset( &offset );
return offset;
}
-DWORD getIndex(IDiaSymbol * pSymbol) {
- DWORD index;
- pSymbol->get_symIndexId( &index );
+DWORD getIndex(IDiaSymbol& pSymbol) {
+ DWORD index = 0;
+ pSymbol.get_symIndexId( &index );
return index;
}
-std::wstring getValue(IDiaSymbol * pSymbol) {
+std::wstring getValue(IDiaSymbol& pSymbol) {
if (getKind(pSymbol) == DataIsConstant) {
VARIANT value;
- HRESULT hr = pSymbol->get_value( &value );
+ HRESULT hr = pSymbol.get_value( &value );
if (hr == S_OK) {
return printVariant( value );
- //return value.bstrVal;
}
}
return L"";
@@ -305,17 +292,17 @@ BSTR getBaseTypeName(IDiaSymbol * pSymbol) {
}
*/
-IDiaSymbol * getType(IDiaSymbol * pSymbol) {
- IDiaSymbol * pBaseType;
- if (pSymbol->get_type( &pBaseType ) == 0) {
+CComPtr<IDiaSymbol> getType(IDiaSymbol& pSymbol) {
+ CComPtr<IDiaSymbol> pBaseType;
+ if (SUCCEEDED(pSymbol.get_type( &pBaseType ))) {
return pBaseType;
}
return NULL;
}
-std::wstring getTypeAsString(IDiaSymbol * pSymbol) {
+std::wstring getTypeAsString(IDiaSymbol& pSymbol) {
std::wstring typeStr ;
- IDiaSymbol * pType;
- if (pSymbol->get_type( &pType ) == 0) {
+ CComPtr<IDiaSymbol> pType;
+ if (SUCCEEDED(pSymbol.get_type( &pType ))) {
typeStr = printType( pType, L"" );
}
else {
@@ -324,17 +311,17 @@ std::wstring getTypeAsString(IDiaSymbol * pSymbol) {
return typeStr;
}
-DWORD getBaseType(IDiaSymbol * pSymbol) {
+DWORD getBaseType(IDiaSymbol& pSymbol) {
if (getTag(pSymbol) == SymTagBaseType) {
DWORD baseType;
- pSymbol->get_baseType( &baseType );
+ pSymbol.get_baseType( &baseType );
return baseType;
}
return -1;
}
-std::wstring getBaseTypeAsString(IDiaSymbol * pSymbol) {
- ULONGLONG len = getLength(pSymbol);
- DWORD bt = getBaseType(pSymbol);
+std::wstring getBaseTypeAsString(IDiaSymbol& pSymbol) {
+ const ULONGLONG len = getLength(pSymbol);
+ const DWORD bt = getBaseType(pSymbol);
switch(bt) {
case 6 :
switch(len) {
diff --git a/Ghidra/Features/PDB/src/pdb/cpp/xml.cpp b/Ghidra/Features/PDB/src/pdb/cpp/xml.cpp
index ce4220bf22..33e455dc03 100644
--- a/Ghidra/Features/PDB/src/pdb/cpp/xml.cpp
+++ b/Ghidra/Features/PDB/src/pdb/cpp/xml.cpp
@@ -20,13 +20,13 @@ std::wstring indent(size_t nSpaces) {
return std::wstring(nSpaces, ' ');
}
-std::wstring escapeXmlEntities(BSTR bstr) {
+std::wstring escapeXmlEntities(const std::wstring& bstr) {
// Scan source str for problematic characters that need escaping.
// Calculate how many characters we will need in new string.
// The cases in this switch() statement need to match the cases in the following switch()
// statement.
- const size_t len = SysStringLen(bstr);
+ const size_t len = bstr.length();
size_t destLen = 0;
for (int i = 0; i < len; ++i) {
switch (bstr[i]) {
diff --git a/Ghidra/Features/PDB/src/pdb/headers/find.h b/Ghidra/Features/PDB/src/pdb/headers/find.h
index 6300d40c93..be95940fc1 100644
--- a/Ghidra/Features/PDB/src/pdb/headers/find.h
+++ b/Ghidra/Features/PDB/src/pdb/headers/find.h
@@ -25,15 +25,17 @@
#include "pdb.h"
#include "print.h"
#include "symbol.h"
+#include <string>
+
//
//The name of SymTagFunction symbol are demangled.
//This method locates the mangled
//function name, which is stored in a SymTagPublicSymbol symbol.
//
-std::wstring findMangledName(IDiaSymbol * pFunction);
-void findNameInNamespace( wchar_t* name, IDiaSymbol* pnamespace );
-void findNameInEnum( wchar_t* name, IDiaSymbol* penumeration );
-void findNameInClass( wchar_t* name, IDiaSymbol* pclass );
-void findCppNameInScope( wchar_t* name, IDiaSymbol* pScope );
+std::wstring findMangledName(IDiaSymbol& pFunction);
+void findNameInNamespace( const std::wstring& name, IDiaSymbol& pnamespace );
+void findNameInEnum( const std::wstring& name, IDiaSymbol& penumeration );
+void findNameInClass( const std::wstring& name, IDiaSymbol& pclass );
+void findCppNameInScope( const std::wstring& name, IDiaSymbol& pScope );
#endif
diff --git a/Ghidra/Features/PDB/src/pdb/headers/iterate.h b/Ghidra/Features/PDB/src/pdb/headers/iterate.h
index d0d18e6238..b523548bdc 100644
--- a/Ghidra/Features/PDB/src/pdb/headers/iterate.h
+++ b/Ghidra/Features/PDB/src/pdb/headers/iterate.h
@@ -28,6 +28,6 @@ void iterateEnums();
void iterateTypedefs();
void iterateClasses();
void iterateFunctions();
-int iterateTables(const bool printAll);
+int iterateTables(bool printAll);
#endif
diff --git a/Ghidra/Features/PDB/src/pdb/headers/print.h b/Ghidra/Features/PDB/src/pdb/headers/print.h
index 30fc3a18c6..5b68e25e82 100644
--- a/Ghidra/Features/PDB/src/pdb/headers/print.h
+++ b/Ghidra/Features/PDB/src/pdb/headers/print.h
@@ -25,10 +25,10 @@
#include <string>
std::wstring printVariant( VARIANT & v );
-std::wstring printType( IDiaSymbol* pType, BSTR prefix );
+std::wstring printType( IDiaSymbol* pType, const std::wstring& prefix );
-void printBound( IDiaSymbol* pBound );
-void printScopeName( IDiaSymbol* pscope );
-void printNameFromScope( wchar_t* name, IDiaSymbol* pscope, IDiaEnumSymbols* pEnum );
+void printBound( IDiaSymbol& pBound );
+void printScopeName( IDiaSymbol& pscope );
+void printNameFromScope( const std::wstring& name, IDiaSymbol& pscope, IDiaEnumSymbols& pEnum );
#endif
diff --git a/Ghidra/Features/PDB/src/pdb/headers/symbol.h b/Ghidra/Features/PDB/src/pdb/headers/symbol.h
index 1d81ddad92..f3be4aa862 100644
--- a/Ghidra/Features/PDB/src/pdb/headers/symbol.h
+++ b/Ghidra/Features/PDB/src/pdb/headers/symbol.h
@@ -21,23 +21,24 @@
#include "diacreate.h"
#include "cvconst.h"
#include <string>
+#include <atlcomcli.h>
-std::wstring getName(IDiaSymbol * pSymbol);
-std::wstring getUndecoratedName(IDiaSymbol * pSymbol);
-DWORD getRVA(IDiaSymbol * pSymbol);
-ULONGLONG getLength(IDiaSymbol * pSymbol);
-DWORD getTag(IDiaSymbol * pSymbol);
-const wchar_t* getTagAsString(IDiaSymbol * pSymbol);
-DWORD getKind(IDiaSymbol * pSymbol);
-std::wstring getKindAsString(IDiaSymbol * pSymbol);
-DWORD getUdtKind(IDiaSymbol * pSymbol);
-LONG getOffset(IDiaSymbol * pSymbol);
-DWORD getIndex(IDiaSymbol * pSymbol);
-std::wstring getValue(IDiaSymbol * pSymbol);
-DWORD getBaseType(IDiaSymbol * pSymbol);
-std::wstring getBaseTypeAsString(IDiaSymbol * pSymbol);
-IDiaSymbol * getType(IDiaSymbol * pSymbol);
-std::wstring getTypeAsString(IDiaSymbol * pSymbol);
+std::wstring getName(IDiaSymbol& pSymbol);
+std::wstring getUndecoratedName(IDiaSymbol& pSymbol);
+DWORD getRVA(IDiaSymbol& pSymbol);
+ULONGLONG getLength(IDiaSymbol& pSymbol);
+DWORD getTag(IDiaSymbol& pSymbol);
+std::wstring getTagAsString(IDiaSymbol& pSymbol);
+DWORD getKind(IDiaSymbol& pSymbol);
+std::wstring getKindAsString(IDiaSymbol& pSymbol);
+DWORD getUdtKind(IDiaSymbol& pSymbol);
+LONG getOffset(IDiaSymbol& pSymbol);
+DWORD getIndex(IDiaSymbol& pSymbol);
+std::wstring getValue(IDiaSymbol& pSymbol);
+DWORD getBaseType(IDiaSymbol& pSymbol);
+std::wstring getBaseTypeAsString(IDiaSymbol& pSymbol);
+CComPtr<IDiaSymbol> getType(IDiaSymbol& pSymbol);
+std::wstring getTypeAsString(IDiaSymbol& pSymbol);
bool isScopeSym( DWORD tag );
diff --git a/Ghidra/Features/PDB/src/pdb/headers/xml.h b/Ghidra/Features/PDB/src/pdb/headers/xml.h
index 4313d4397d..b63401dfa3 100644
--- a/Ghidra/Features/PDB/src/pdb/headers/xml.h
+++ b/Ghidra/Features/PDB/src/pdb/headers/xml.h
@@ -21,6 +21,6 @@
#include <comutil.h>
std::wstring indent(size_t nSpaces);
-std::wstring escapeXmlEntities(BSTR bstr);
+std::wstring escapeXmlEntities(const std::wstring& bstr);
#endif