-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathsymboltablesupport.cpp
170 lines (137 loc) · 4.69 KB
/
symboltablesupport.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
//==================================
// PEDUMP - Matt Pietrek 1997
// FILE: COMMON.C
//==================================
#include <windows.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include "common.h"
#include "COFFSymbolTable.h"
#include "extrnvar.h"
#include "cv_dbg.h"
//
// Dump the COFF debug information header
//
void DumpCOFFHeader(PIMAGE_COFF_SYMBOLS_HEADER pDbgInfo)
{
printf("COFF Debug Info Header\n");
printf(" NumberOfSymbols: %08X\n", pDbgInfo->NumberOfSymbols);
printf(" LvaToFirstSymbol: %08X\n", pDbgInfo->LvaToFirstSymbol);
printf(" NumberOfLinenumbers: %08X\n", pDbgInfo->NumberOfLinenumbers);
printf(" LvaToFirstLinenumber: %08X\n", pDbgInfo->LvaToFirstLinenumber);
printf(" RvaToFirstByteOfCode: %08X\n", pDbgInfo->RvaToFirstByteOfCode);
printf(" RvaToLastByteOfCode: %08X\n", pDbgInfo->RvaToLastByteOfCode);
printf(" RvaToFirstByteOfData: %08X\n", pDbgInfo->RvaToFirstByteOfData);
printf(" RvaToLastByteOfData: %08X\n", pDbgInfo->RvaToLastByteOfData);
}
//
// Given a COFF symbol table index, look up its name. This function assumes
// that the COFFSymbolCount and PCOFFSymbolTable variables have been already
// set up.
//
BOOL LookupSymbolName(DWORD index, PSTR buffer, UINT length)
{
if ( !g_pCOFFSymbolTable )
return FALSE;
PCOFFSymbol pSymbol = g_pCOFFSymbolTable->GetSymbolFromIndex( index );
if ( !pSymbol )
return FALSE;
lstrcpyn( buffer, pSymbol->GetName(), length );
delete pSymbol;
return TRUE;
}
//
// Dump a range of line numbers from the COFF debug information
//
void DumpLineNumbers(PIMAGE_LINENUMBER pln, DWORD count)
{
char buffer[64];
DWORD i;
printf("Line Numbers\n");
for (i=0; i < count; i++)
{
if ( pln->Linenumber == 0 ) // A symbol table index
{
buffer[0] = 0;
LookupSymbolName(pln->Type.SymbolTableIndex, buffer,
sizeof(buffer));
printf("SymIndex: %X (%s)\n", pln->Type.SymbolTableIndex,
buffer);
}
else // A regular line number
printf(" Addr: %05X Line: %04u\n",
pln->Type.VirtualAddress, pln->Linenumber);
pln++;
}
}
//
// Used by the DumpSymbolTable() routine. It purpose is to filter out
// the non-normal section numbers and give them meaningful names.
//
void GetSectionName(WORD section, PSTR buffer, unsigned cbBuffer)
{
char tempbuffer[10];
switch ( (SHORT)section )
{
case IMAGE_SYM_UNDEFINED: strcpy(tempbuffer, "UNDEF"); break;
case IMAGE_SYM_ABSOLUTE: strcpy(tempbuffer, "ABS"); break;
case IMAGE_SYM_DEBUG: strcpy(tempbuffer, "DEBUG"); break;
default: sprintf(tempbuffer, "%X", section);
}
strncpy(buffer, tempbuffer, cbBuffer-1);
}
//
// Dumps a COFF symbol table from an EXE or OBJ
//
void DumpSymbolTable( PCOFFSymbolTable pSymTab )
{
printf( "Symbol Table - %X entries (* = auxillary symbol)\n",
pSymTab->GetNumberOfSymbols() );
printf(
"Indx Sectn Value Type Storage Name\n"
"---- ----- -------- ----- ------- --------\n");
PCOFFSymbol pSymbol = pSymTab->GetNextSymbol( 0 );
while ( pSymbol )
{
char szSection[10];
GetSectionName(pSymbol->GetSectionNumber(),szSection,sizeof(szSection));
printf( "%04X %5s %08X %s %-8s %s\n",
pSymbol->GetIndex(), szSection, pSymbol->GetValue(),
pSymbol->GetTypeName(), pSymbol->GetStorageClassName(),
pSymbol->GetName() );
if ( pSymbol->GetNumberOfAuxSymbols() )
{
char szAuxSymbol[1024];
if (pSymbol->GetAuxSymbolAsString(szAuxSymbol,sizeof(szAuxSymbol)))
printf( " * %s\n", szAuxSymbol );
}
pSymbol = pSymTab->GetNextSymbol( pSymbol );
}
}
void DumpMiscDebugInfo( PIMAGE_DEBUG_MISC pMiscDebugInfo )
{
if ( IMAGE_DEBUG_MISC_EXENAME != pMiscDebugInfo->DataType )
{
printf( "Unknown Miscellaneous Debug Information type: %u\n",
pMiscDebugInfo->DataType );
return;
}
printf( "Miscellaneous Debug Information\n" );
printf( " %s\n", pMiscDebugInfo->Data );
}
void DumpCVDebugInfo( PDWORD pCVHeader )
{
PPDB_INFO pPDBInfo;
printf( "CodeView Signature: %08X\n", *pCVHeader );
if ( '01BN' != *pCVHeader )
{
printf( "Unhandled CodeView Information format %.4s\n", pCVHeader );
return;
}
pPDBInfo = (PPDB_INFO)pCVHeader;
printf( " Offset: %08X Signature: %08X Age: %08X\n",
pPDBInfo->Offset, pPDBInfo->sig, pPDBInfo->age );
printf( " File: %s\n", pPDBInfo->PdbName );
}