debug.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717
  1. /*
  2. * Made after:
  3. * CVDump - Parses through a Visual Studio .DBG file in CodeView 4 format
  4. * and dumps the info to STDOUT in a human-readable format
  5. *
  6. * Copyright 2000 John R. Sheets
  7. *
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with this library; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
  21. */
  22. #include "config.h"
  23. #include <stdlib.h>
  24. #include <stdarg.h>
  25. #include <stdio.h>
  26. #include <time.h>
  27. #include "../tools.h"
  28. #include "windef.h"
  29. #include "winbase.h"
  30. #include "winedump.h"
  31. #include "wine/mscvpdb.h"
  32. /*
  33. * .DBG File Layout:
  34. *
  35. * IMAGE_SEPARATE_DEBUG_HEADER
  36. * IMAGE_SECTION_HEADER[]
  37. * IMAGE_DEBUG_DIRECTORY[]
  38. * OMFSignature
  39. * debug data (typical example)
  40. * - IMAGE_DEBUG_TYPE_MISC
  41. * - IMAGE_DEBUG_TYPE_FPO
  42. * - IMAGE_DEBUG_TYPE_CODEVIEW
  43. * OMFDirHeader
  44. * OMFDirEntry[]
  45. */
  46. /*
  47. * Descriptions:
  48. *
  49. * (hdr) IMAGE_SEPARATE_DEBUG_HEADER - .DBG-specific file header; holds info that
  50. * applies to the file as a whole, including # of COFF sections, file offsets, etc.
  51. * (hdr) IMAGE_SECTION_HEADER - list of COFF sections copied verbatim from .EXE;
  52. * although this directory contains file offsets, these offsets are meaningless
  53. * in the context of the .DBG file, because only the section headers are copied
  54. * to the .DBG file... not the binary data it points to.
  55. * (hdr) IMAGE_DEBUG_DIRECTORY - list of different formats of debug info contained in file
  56. * (see IMAGE_DEBUG_TYPE_* descriptions below); tells where each section starts
  57. * (hdr) OMFSignature (CV) - Contains "NBxx" signature, plus file offset telling how far
  58. * into the IMAGE_DEBUG_TYPE_CODEVIEW section the OMFDirHeader and OMFDirEntry's sit
  59. * (data) IMAGE_DEBUG_TYPE_MISC - usually holds name of original .EXE file
  60. * (data) IMAGE_DEBUG_TYPE_FPO - Frame Pointer Optimization data; used for dealing with
  61. * optimized stack frames (optional)
  62. * (data) IMAGE_DEBUG_TYPE_CODEVIEW - *** THE GOOD STUFF ***
  63. * This block of data contains all the symbol tables, line number info, etc.,
  64. * that the Visual C++ debugger needs.
  65. * (hdr) OMFDirHeader (CV) -
  66. * (hdr) OMFDirEntry (CV) - list of subsections within CodeView debug data section
  67. */
  68. /*
  69. * The .DBG file typically has three arrays of directory entries, which tell
  70. * the OS or debugger where in the file to look for the actual data
  71. *
  72. * IMAGE_SECTION_HEADER - number of entries determined by:
  73. * (IMAGE_SEPARATE_DEBUG_HEADER.NumberOfSections)
  74. *
  75. * IMAGE_DEBUG_DIRECTORY - number of entries determined by:
  76. * (IMAGE_SEPARATE_DEBUG_HEADER.DebugDirectorySize / sizeof (IMAGE_DEBUG_DIRECTORY))
  77. *
  78. * OMFDirEntry - number of entries determined by:
  79. * (OMFDirHeader.cDir)
  80. */
  81. extern const IMAGE_NT_HEADERS* PE_nt_headers;
  82. static const void* cv_base /* = 0 */;
  83. static BOOL dump_cv_sst_module(const OMFDirEntry* omfde)
  84. {
  85. const OMFModule* module;
  86. const OMFSegDesc* segDesc;
  87. int i;
  88. module = PRD(Offset(cv_base) + omfde->lfo, sizeof(OMFModule));
  89. if (!module) {printf("Can't get the OMF-Module, aborting\n"); return FALSE;}
  90. printf(" olvNumber: %u\n", module->ovlNumber);
  91. printf(" iLib: %u\n", module->iLib);
  92. printf(" cSeg: %u\n", module->cSeg);
  93. printf(" Style: %c%c\n", module->Style[0], module->Style[1]);
  94. printf(" Name: %.*s\n",
  95. *(const BYTE*)((const char*)(module + 1) + sizeof(OMFSegDesc) * module->cSeg),
  96. (const char*)(module + 1) + sizeof(OMFSegDesc) * module->cSeg + 1);
  97. segDesc = PRD(Offset(module + 1), sizeof(OMFSegDesc) * module->cSeg);
  98. if (!segDesc) {printf("Can't get the OMF-SegDesc, aborting\n"); return FALSE;}
  99. for (i = 0; i < module->cSeg; i++)
  100. {
  101. printf (" segment #%2d: offset = [0x%8x], size = [0x%8x]\n",
  102. segDesc->Seg, segDesc->Off, segDesc->cbSeg);
  103. segDesc++;
  104. }
  105. return TRUE;
  106. }
  107. static BOOL dump_cv_sst_global_pub(const OMFDirEntry* omfde)
  108. {
  109. long fileoffset;
  110. const OMFSymHash* header;
  111. const BYTE* symbols;
  112. fileoffset = Offset(cv_base) + omfde->lfo;
  113. printf (" GlobalPub section starts at file offset 0x%lx\n", fileoffset);
  114. printf (" Symbol table starts at 0x%lx\n", fileoffset + sizeof (OMFSymHash));
  115. printf ("\n ----- Begin Symbol Table -----\n");
  116. header = PRD(fileoffset, sizeof(OMFSymHash));
  117. if (!header) {printf("Can't get OMF-SymHash, aborting\n");return FALSE;}
  118. symbols = PRD(fileoffset + sizeof(OMFSymHash), header->cbSymbol);
  119. if (!symbols) {printf("Can't OMF-SymHash details, aborting\n"); return FALSE;}
  120. codeview_dump_symbols(symbols, 0, header->cbSymbol);
  121. return TRUE;
  122. }
  123. static BOOL dump_cv_sst_global_sym(const OMFDirEntry* omfde)
  124. {
  125. /*** NOT YET IMPLEMENTED ***/
  126. return TRUE;
  127. }
  128. static BOOL dump_cv_sst_static_sym(const OMFDirEntry* omfde)
  129. {
  130. /*** NOT YET IMPLEMENTED ***/
  131. return TRUE;
  132. }
  133. static BOOL dump_cv_sst_libraries(const OMFDirEntry* omfde)
  134. {
  135. /*** NOT YET IMPLEMENTED ***/
  136. return TRUE;
  137. }
  138. static BOOL dump_cv_sst_global_types(const OMFDirEntry* omfde)
  139. {
  140. long fileoffset;
  141. const OMFGlobalTypes*types;
  142. const BYTE* data;
  143. unsigned sz;
  144. fileoffset = Offset(cv_base) + omfde->lfo;
  145. printf (" GlobalTypes section starts at file offset 0x%lx\n", fileoffset);
  146. printf ("\n ----- Begin Global Types Table -----\n");
  147. types = PRD(fileoffset, sizeof(OMFGlobalTypes));
  148. if (!types) {printf("Can't get OMF-GlobalTypes, aborting\n");return FALSE;}
  149. sz = omfde->cb - sizeof(OMFGlobalTypes) - sizeof(DWORD) * types->cTypes;
  150. data = PRD(fileoffset + sizeof(OMFGlobalTypes) + sizeof(DWORD) * types->cTypes, sz);
  151. if (!data) {printf("Can't OMF-SymHash details, aborting\n"); return FALSE;}
  152. /* doc says:
  153. * - for NB07 & NB08 (that we don't support yet), offsets are from types
  154. * - for NB09, offsets are from data
  155. * For now, we only support the latter
  156. */
  157. codeview_dump_types_from_offsets(data, (const DWORD*)(types + 1), types->cTypes);
  158. return TRUE;
  159. }
  160. static BOOL dump_cv_sst_seg_map(const OMFDirEntry* omfde)
  161. {
  162. const OMFSegMap* segMap;
  163. const OMFSegMapDesc* segMapDesc;
  164. int i;
  165. segMap = PRD(Offset(cv_base) + omfde->lfo, sizeof(OMFSegMap));
  166. if (!segMap) {printf("Can't get SegMap, aborting\n");return FALSE;}
  167. printf(" cSeg: %u\n", segMap->cSeg);
  168. printf(" cSegLog: %u\n", segMap->cSegLog);
  169. segMapDesc = PRD(Offset(segMap + 1), segMap->cSeg * sizeof(OMFSegDesc));
  170. if (!segMapDesc) {printf("Can't get SegDescr array, aborting\n");return FALSE;}
  171. for (i = 0; i < segMap->cSeg; i++)
  172. {
  173. printf(" SegDescr #%2d\n", i + 1);
  174. printf(" flags: %04X\n", segMapDesc[i].flags);
  175. printf(" ovl: %u\n", segMapDesc[i].ovl);
  176. printf(" group: %u\n", segMapDesc[i].group);
  177. printf(" frame: %u\n", segMapDesc[i].frame);
  178. printf(" iSegName: %u\n", segMapDesc[i].iSegName);
  179. printf(" iClassName: %u\n", segMapDesc[i].iClassName);
  180. printf(" offset: %lu\n", segMapDesc[i].offset);
  181. printf(" cbSeg: %lu\n", segMapDesc[i].cbSeg);
  182. }
  183. return TRUE;
  184. }
  185. static BOOL dump_cv_sst_file_index(const OMFDirEntry* omfde)
  186. {
  187. /*** NOT YET IMPLEMENTED ***/
  188. return TRUE;
  189. }
  190. static BOOL dump_cv_sst_src_module(const OMFDirEntry* omfde)
  191. {
  192. int i, j;
  193. const BYTE* rawdata;
  194. const unsigned long* seg_info_dw;
  195. const unsigned short* seg_info_w;
  196. unsigned ofs;
  197. const OMFSourceModule* sourceModule;
  198. const OMFSourceFile* sourceFile;
  199. const OMFSourceLine* sourceLine;
  200. rawdata = PRD(Offset(cv_base) + omfde->lfo, omfde->cb);
  201. if (!rawdata) {printf("Can't get srcModule subsection details, aborting\n");return FALSE;}
  202. /* FIXME: check ptr validity */
  203. sourceModule = (const void*)rawdata;
  204. printf (" Module table: Found %d file(s) and %d segment(s)\n",
  205. sourceModule->cFile, sourceModule->cSeg);
  206. for (i = 0; i < sourceModule->cFile; i++)
  207. {
  208. printf (" File #%2d begins at an offset of 0x%lx in this section\n",
  209. i + 1, sourceModule->baseSrcFile[i]);
  210. }
  211. /* FIXME: check ptr validity */
  212. seg_info_dw = (const void*)((const char*)(sourceModule + 1) +
  213. sizeof(unsigned long) * (sourceModule->cFile - 1));
  214. seg_info_w = (const unsigned short*)(&seg_info_dw[sourceModule->cSeg * 2]);
  215. for (i = 0; i < sourceModule->cSeg; i++)
  216. {
  217. printf (" Segment #%2d start = 0x%lx, end = 0x%lx, seg index = %u\n",
  218. i + 1, seg_info_dw[i * 2], seg_info_dw[(i * 2) + 1],
  219. seg_info_w[i]);
  220. }
  221. ofs = sizeof(OMFSourceModule) + sizeof(unsigned long) * (sourceModule->cFile - 1) +
  222. sourceModule->cSeg * (2 * sizeof(unsigned long) + sizeof(unsigned short));
  223. ofs = (ofs + 3) & ~3;
  224. /* the OMFSourceFile is quite unpleasant to use:
  225. * we have first:
  226. * unsigned short number of segments
  227. * unsigned short reserved
  228. * unsigned long baseSrcLn[# segments]
  229. * unsigned long offset[2 * #segments]
  230. * odd indices are start offsets
  231. * even indices are end offsets
  232. * unsigned char string length for file name
  233. * char file name (length is previous field)
  234. */
  235. /* FIXME: check ptr validity */
  236. sourceFile = (const void*)(rawdata + ofs);
  237. seg_info_dw = (const void*)((const char*)sourceFile + 2 * sizeof(unsigned short) +
  238. sourceFile->cSeg * sizeof(unsigned long));
  239. ofs += 2 * sizeof(unsigned short) + 3 * sourceFile->cSeg * sizeof(unsigned long);
  240. printf(" File table: %.*s\n",
  241. *(const BYTE*)((const char*)sourceModule + ofs), (const char*)sourceModule + ofs + 1);
  242. for (i = 0; i < sourceFile->cSeg; i++)
  243. {
  244. printf (" Segment #%2d start = 0x%lx, end = 0x%lx, offset = 0x%lx\n",
  245. i + 1, seg_info_dw[i * 2], seg_info_dw[(i * 2) + 1], sourceFile->baseSrcLn[i]);
  246. }
  247. /* add file name length */
  248. ofs += *(const BYTE*)((const char*)sourceModule + ofs) + 1;
  249. ofs = (ofs + 3) & ~3;
  250. for (i = 0; i < sourceModule->cSeg; i++)
  251. {
  252. sourceLine = (const void*)(rawdata + ofs);
  253. seg_info_dw = (const void*)((const char*)sourceLine + 2 * sizeof(unsigned short));
  254. seg_info_w = (const void*)(&seg_info_dw[sourceLine->cLnOff]);
  255. printf (" Line table #%2d: Found %d line numbers for segment index %d\n",
  256. i, sourceLine->cLnOff, sourceLine->Seg);
  257. for (j = 0; j < sourceLine->cLnOff; j++)
  258. {
  259. printf (" Pair #%2d: offset = [0x%8lx], linenumber = %d\n",
  260. j + 1, seg_info_dw[j], seg_info_w[j]);
  261. }
  262. ofs += 2 * sizeof(unsigned short) +
  263. sourceLine->cLnOff * (sizeof(unsigned long) + sizeof(unsigned short));
  264. ofs = (ofs + 3) & ~3;
  265. }
  266. return TRUE;
  267. }
  268. static BOOL dump_cv_sst_align_sym(const OMFDirEntry* omfde)
  269. {
  270. const char* rawdata = PRD(Offset(cv_base) + omfde->lfo, omfde->cb);
  271. if (!rawdata) {printf("Can't get srcAlignSym subsection details, aborting\n");return FALSE;}
  272. if (omfde->cb < sizeof(DWORD)) return TRUE;
  273. codeview_dump_symbols(rawdata, sizeof(DWORD), omfde->cb);
  274. return TRUE;
  275. }
  276. static void dump_codeview_all_modules(const OMFDirHeader *omfdh)
  277. {
  278. unsigned i;
  279. const OMFDirEntry* dirEntry;
  280. const char* str;
  281. if (!omfdh || !omfdh->cDir) return;
  282. dirEntry = PRD(Offset(omfdh + 1), omfdh->cDir * sizeof(OMFDirEntry));
  283. if (!dirEntry) {printf("Can't read DirEntry array, aborting\n"); return;}
  284. for (i = 0; i < omfdh->cDir; i++)
  285. {
  286. switch (dirEntry[i].SubSection)
  287. {
  288. case sstModule: str = "sstModule"; break;
  289. case sstAlignSym: str = "sstAlignSym"; break;
  290. case sstSrcModule: str = "sstSrcModule"; break;
  291. case sstLibraries: str = "sstLibraries"; break;
  292. case sstGlobalSym: str = "sstGlobalSym"; break;
  293. case sstGlobalPub: str = "sstGlobalPub"; break;
  294. case sstGlobalTypes: str = "sstGlobalTypes"; break;
  295. case sstSegMap: str = "sstSegMap"; break;
  296. case sstFileIndex: str = "sstFileIndex"; break;
  297. case sstStaticSym: str = "sstStaticSym"; break;
  298. default: str = "<undefined>"; break;
  299. }
  300. printf("Module #%2d (%p)\n", i + 1, &dirEntry[i]);
  301. printf(" SubSection: %04X (%s)\n", dirEntry[i].SubSection, str);
  302. printf(" iMod: %d\n", dirEntry[i].iMod);
  303. printf(" lfo: %d\n", dirEntry[i].lfo);
  304. printf(" cb: %u\n", dirEntry[i].cb);
  305. switch (dirEntry[i].SubSection)
  306. {
  307. case sstModule: dump_cv_sst_module(&dirEntry[i]); break;
  308. case sstAlignSym: dump_cv_sst_align_sym(&dirEntry[i]); break;
  309. case sstSrcModule: dump_cv_sst_src_module(&dirEntry[i]); break;
  310. case sstLibraries: dump_cv_sst_libraries(&dirEntry[i]); break;
  311. case sstGlobalSym: dump_cv_sst_global_sym(&dirEntry[i]); break;
  312. case sstGlobalPub: dump_cv_sst_global_pub(&dirEntry[i]); break;
  313. case sstGlobalTypes: dump_cv_sst_global_types(&dirEntry[i]); break;
  314. case sstSegMap: dump_cv_sst_seg_map(&dirEntry[i]); break;
  315. case sstFileIndex: dump_cv_sst_file_index(&dirEntry[i]); break;
  316. case sstStaticSym: dump_cv_sst_static_sym(&dirEntry[i]); break;
  317. default: printf("unsupported type %x\n", dirEntry[i].SubSection); break;
  318. }
  319. printf("\n");
  320. }
  321. return;
  322. }
  323. static void dump_codeview_headers(unsigned long base, unsigned long len)
  324. {
  325. const OMFDirHeader* dirHeader;
  326. const char* signature;
  327. const OMFDirEntry* dirEntry;
  328. const OMFSignature* sig;
  329. unsigned i;
  330. int modulecount = 0, alignsymcount = 0, srcmodulecount = 0, librariescount = 0;
  331. int globalsymcount = 0, globalpubcount = 0, globaltypescount = 0;
  332. int segmapcount = 0, fileindexcount = 0, staticsymcount = 0;
  333. cv_base = PRD(base, len);
  334. if (!cv_base) {printf("Can't get full debug content, aborting\n");return;}
  335. signature = cv_base;
  336. printf(" CodeView Data\n");
  337. printf(" Signature: %.4s\n", signature);
  338. if (memcmp(signature, "NB10", 4) == 0)
  339. {
  340. const CODEVIEW_PDB_DATA* pdb_data;
  341. pdb_data = cv_base;
  342. printf(" Filepos: 0x%08lX\n", pdb_data->filepos);
  343. printf(" TimeStamp: %08X (%s)\n",
  344. pdb_data->timestamp, get_time_str(pdb_data->timestamp));
  345. printf(" Age: %08X\n", pdb_data->age);
  346. printf(" Filename: %s\n", pdb_data->name);
  347. return;
  348. }
  349. if (memcmp(signature, "RSDS", 4) == 0)
  350. {
  351. const OMFSignatureRSDS* rsds_data;
  352. rsds_data = cv_base;
  353. printf(" Guid: %s\n", get_guid_str(&rsds_data->guid));
  354. printf(" Age: %08X\n", rsds_data->age);
  355. printf(" Filename: %s\n", rsds_data->name);
  356. return;
  357. }
  358. if (memcmp(signature, "NB09", 4) != 0 && memcmp(signature, "NB11", 4) != 0)
  359. {
  360. printf("Unsupported signature (%.4s), aborting\n", signature);
  361. return;
  362. }
  363. sig = cv_base;
  364. printf(" Filepos: 0x%08lX\n", sig->filepos);
  365. dirHeader = PRD(Offset(cv_base) + sig->filepos, sizeof(OMFDirHeader));
  366. if (!dirHeader) {printf("Can't get debug header, aborting\n"); return;}
  367. printf(" Size of header: 0x%4X\n", dirHeader->cbDirHeader);
  368. printf(" Size per entry: 0x%4X\n", dirHeader->cbDirEntry);
  369. printf(" # of entries: 0x%8X (%d)\n", dirHeader->cDir, dirHeader->cDir);
  370. printf(" Offset to NextDir: 0x%8X\n", dirHeader->lfoNextDir);
  371. printf(" Flags: 0x%8X\n", dirHeader->flags);
  372. if (!dirHeader->cDir) return;
  373. dirEntry = PRD(Offset(dirHeader + 1), sizeof(OMFDirEntry) * dirHeader->cDir);
  374. if (!dirEntry) {printf("Can't get DirEntry array, aborting\n");return;}
  375. for (i = 0; i < dirHeader->cDir; i++)
  376. {
  377. switch (dirEntry[i].SubSection)
  378. {
  379. case sstModule: modulecount++; break;
  380. case sstAlignSym: alignsymcount++; break;
  381. case sstSrcModule: srcmodulecount++; break;
  382. case sstLibraries: librariescount++; break;
  383. case sstGlobalSym: globalsymcount++; break;
  384. case sstGlobalPub: globalpubcount++; break;
  385. case sstGlobalTypes: globaltypescount++; break;
  386. case sstSegMap: segmapcount++; break;
  387. case sstFileIndex: fileindexcount++; break;
  388. case sstStaticSym: staticsymcount++; break;
  389. }
  390. }
  391. /* This one has to be > 0
  392. */
  393. printf ("\nFound: %d sstModule subsections\n", modulecount);
  394. if (alignsymcount > 0) printf (" %d sstAlignSym subsections\n", alignsymcount);
  395. if (srcmodulecount > 0) printf (" %d sstSrcModule subsections\n", srcmodulecount);
  396. if (librariescount > 0) printf (" %d sstLibraries subsections\n", librariescount);
  397. if (globalsymcount > 0) printf (" %d sstGlobalSym subsections\n", globalsymcount);
  398. if (globalpubcount > 0) printf (" %d sstGlobalPub subsections\n", globalpubcount);
  399. if (globaltypescount > 0) printf (" %d sstGlobalTypes subsections\n", globaltypescount);
  400. if (segmapcount > 0) printf (" %d sstSegMap subsections\n", segmapcount);
  401. if (fileindexcount > 0) printf (" %d sstFileIndex subsections\n", fileindexcount);
  402. if (staticsymcount > 0) printf (" %d sstStaticSym subsections\n", staticsymcount);
  403. dump_codeview_all_modules(dirHeader);
  404. }
  405. static const char *get_coff_name( const IMAGE_SYMBOL *coff_sym, const char *coff_strtab )
  406. {
  407. static char namebuff[9];
  408. const char* nampnt;
  409. if( coff_sym->N.Name.Short )
  410. {
  411. memcpy(namebuff, coff_sym->N.ShortName, 8);
  412. namebuff[8] = '\0';
  413. nampnt = namebuff;
  414. }
  415. else
  416. {
  417. nampnt = coff_strtab + coff_sym->N.Name.Long;
  418. }
  419. if( nampnt[0] == '_' )
  420. nampnt++;
  421. return nampnt;
  422. }
  423. static const char* storage_class(BYTE sc)
  424. {
  425. static char tmp[7];
  426. switch (sc)
  427. {
  428. case IMAGE_SYM_CLASS_STATIC: return "static";
  429. case IMAGE_SYM_CLASS_EXTERNAL: return "extrnl";
  430. case IMAGE_SYM_CLASS_LABEL: return "label ";
  431. }
  432. sprintf(tmp, "#%d", sc);
  433. return tmp;
  434. }
  435. void dump_coff_symbol_table(const IMAGE_SYMBOL *coff_symbols, unsigned num_sym,
  436. const IMAGE_SECTION_HEADER *sectHead)
  437. {
  438. const IMAGE_SYMBOL *coff_sym;
  439. const char *coff_strtab = (const char *) (coff_symbols + num_sym);
  440. unsigned int i;
  441. const char *nampnt;
  442. int naux;
  443. printf("\nDebug table: COFF format.\n");
  444. printf(" ID | seg:offs [ abs ] | Kind | symbol/function name\n");
  445. for (i=0; i < num_sym; i++)
  446. {
  447. coff_sym = coff_symbols + i;
  448. naux = coff_sym->NumberOfAuxSymbols;
  449. switch (coff_sym->StorageClass)
  450. {
  451. case IMAGE_SYM_CLASS_FILE:
  452. printf("file %s\n", (const char *) (coff_sym + 1));
  453. break;
  454. case IMAGE_SYM_CLASS_STATIC:
  455. case IMAGE_SYM_CLASS_EXTERNAL:
  456. case IMAGE_SYM_CLASS_LABEL:
  457. if (coff_sym->SectionNumber > 0)
  458. {
  459. DWORD base = sectHead[coff_sym->SectionNumber - 1].VirtualAddress;
  460. nampnt = get_coff_name( coff_sym, coff_strtab );
  461. printf("%05d | %02d:%08x [%08x] | %s | %s\n",
  462. i, coff_sym->SectionNumber - 1, coff_sym->Value,
  463. base + coff_sym->Value,
  464. storage_class(coff_sym->StorageClass), nampnt);
  465. }
  466. break;
  467. default:
  468. printf("%05d | %s\n", i, storage_class(coff_sym->StorageClass));
  469. }
  470. /*
  471. * For now, skip past the aux entries.
  472. */
  473. i += naux;
  474. }
  475. }
  476. void dump_coff(unsigned long coffbase, unsigned long len, const IMAGE_SECTION_HEADER* sectHead)
  477. {
  478. const IMAGE_COFF_SYMBOLS_HEADER *coff = PRD(coffbase, len);
  479. const IMAGE_SYMBOL *coff_symbols =
  480. (const IMAGE_SYMBOL *) ((const char *)coff + coff->LvaToFirstSymbol);
  481. dump_coff_symbol_table(coff_symbols, coff->NumberOfSymbols, sectHead);
  482. }
  483. void dump_codeview(unsigned long base, unsigned long len)
  484. {
  485. dump_codeview_headers(base, len);
  486. }
  487. void dump_frame_pointer_omission(unsigned long base, unsigned long len)
  488. {
  489. const FPO_DATA* fpo;
  490. const FPO_DATA* last;
  491. const char* x;
  492. /* FPO is used to describe nonstandard stack frames */
  493. printf("Range #loc #pmt Prlg #reg Info\n"
  494. "-----------------+----+----+----+----+------------\n");
  495. fpo = PRD(base, len);
  496. if (!fpo) {printf("Couldn't get FPO blob\n"); return;}
  497. last = (const FPO_DATA*)((const char*)fpo + len);
  498. while (fpo < last && fpo->ulOffStart)
  499. {
  500. switch (fpo->cbFrame)
  501. {
  502. case FRAME_FPO: x = "FRAME_FPO"; break;
  503. case FRAME_NONFPO: x = "FRAME_NONFPO"; break;
  504. case FRAME_TRAP: x = "FRAME_TRAP"; break;
  505. case FRAME_TSS: x = "case FRAME_TSS"; break;
  506. default: x = NULL; break;
  507. }
  508. printf("%08x-%08x %4u %4u %4u %4u %s%s%s\n",
  509. fpo->ulOffStart, fpo->ulOffStart + fpo->cbProcSize,
  510. fpo->cdwLocals, fpo->cdwParams, fpo->cbProlog, fpo->cbRegs,
  511. x, fpo->fHasSEH ? " SEH" : "", fpo->fUseBP ? " UseBP" : "");
  512. fpo++;
  513. }
  514. }
  515. struct stab_nlist
  516. {
  517. union
  518. {
  519. char* n_name;
  520. struct stab_nlist* n_next;
  521. long n_strx;
  522. } n_un;
  523. unsigned char n_type;
  524. char n_other;
  525. short n_desc;
  526. unsigned long n_value;
  527. };
  528. static const char * const stabs_defs[] = {
  529. NULL,NULL,NULL,NULL, /* 00 */
  530. NULL,NULL,NULL,NULL, /* 08 */
  531. NULL,NULL,NULL,NULL, /* 10 */
  532. NULL,NULL,NULL,NULL, /* 18 */
  533. "GSYM","FNAME","FUN","STSYM", /* 20 */
  534. "LCSYM","MAIN","ROSYM","PC", /* 28 */
  535. NULL,"NSYMS","NOMAP",NULL, /* 30 */
  536. "OBJ",NULL,"OPT",NULL, /* 38 */
  537. "RSYM","M2C","SLINE","DSLINE", /* 40 */
  538. "BSLINE","DEFD","FLINE",NULL, /* 48 */
  539. "EHDECL",NULL,"CATCH",NULL, /* 50 */
  540. NULL,NULL,NULL,NULL, /* 58 */
  541. "SSYM","ENDM","SO",NULL, /* 60 */
  542. NULL,NULL,NULL,NULL, /* 68 */
  543. NULL,NULL,NULL,NULL, /* 70 */
  544. NULL,NULL,NULL,NULL, /* 78 */
  545. "LSYM","BINCL","SOL",NULL, /* 80 */
  546. NULL,NULL,NULL,NULL, /* 88 */
  547. NULL,NULL,NULL,NULL, /* 90 */
  548. NULL,NULL,NULL,NULL, /* 98 */
  549. "PSYM","EINCL","ENTRY",NULL, /* a0 */
  550. NULL,NULL,NULL,NULL, /* a8 */
  551. NULL,NULL,NULL,NULL, /* b0 */
  552. NULL,NULL,NULL,NULL, /* b8 */
  553. "LBRAC","EXCL","SCOPE",NULL, /* c0 */
  554. NULL,NULL,NULL,NULL, /* c8 */
  555. NULL,NULL,NULL,NULL, /* d0 */
  556. NULL,NULL,NULL,NULL, /* d8 */
  557. "RBRAC","BCOMM","ECOMM",NULL, /* e0 */
  558. "ECOML","WITH",NULL,NULL, /* e8 */
  559. "NBTEXT","NBDATA","NBBSS","NBSTS", /* f0 */
  560. "NBLCS",NULL,NULL,NULL /* f8 */
  561. };
  562. void dump_stabs(const void* pv_stabs, unsigned szstabs, const char* stabstr, unsigned szstr)
  563. {
  564. int i;
  565. int nstab;
  566. const char* ptr;
  567. char* stabbuff;
  568. unsigned int stabbufflen;
  569. const struct stab_nlist* stab_ptr = pv_stabs;
  570. const char* strs_end;
  571. char n_buffer[16];
  572. nstab = szstabs / sizeof(struct stab_nlist);
  573. strs_end = stabstr + szstr;
  574. /*
  575. * Allocate a buffer into which we can build stab strings for cases
  576. * where the stab is continued over multiple lines.
  577. */
  578. stabbufflen = 65536;
  579. stabbuff = xmalloc(stabbufflen);
  580. stabbuff[0] = '\0';
  581. printf("#Sym n_type n_othr n_desc n_value n_strx String\n");
  582. for (i = 0; i < nstab; i++, stab_ptr++)
  583. {
  584. ptr = stabstr + stab_ptr->n_un.n_strx;
  585. if ((ptr > strs_end) || (ptr + strlen(ptr) > strs_end))
  586. {
  587. ptr = "[[*** bad string ***]]";
  588. }
  589. else if (ptr[strlen(ptr) - 1] == '\\')
  590. {
  591. /*
  592. * Indicates continuation. Append this to the buffer, and go onto the
  593. * next record. Repeat the process until we find a stab without the
  594. * '/' character, as this indicates we have the whole thing.
  595. */
  596. unsigned len = strlen(ptr);
  597. if (strlen(stabbuff) + len > stabbufflen)
  598. {
  599. stabbufflen += 65536;
  600. stabbuff = xrealloc(stabbuff, stabbufflen);
  601. }
  602. strcat(stabbuff, ptr);
  603. continue;
  604. }
  605. else if (stabbuff[0] != '\0')
  606. {
  607. strcat(stabbuff, ptr);
  608. ptr = stabbuff;
  609. }
  610. if ((stab_ptr->n_type & 1) || !stabs_defs[stab_ptr->n_type / 2])
  611. sprintf(n_buffer, "<0x%02x>", stab_ptr->n_type);
  612. else
  613. sprintf(n_buffer, "%-6s", stabs_defs[stab_ptr->n_type / 2]);
  614. printf("%4d %s %-8x % 6d %-8lx %-6lx %s\n",
  615. i, n_buffer, stab_ptr->n_other, stab_ptr->n_desc, stab_ptr->n_value,
  616. stab_ptr->n_un.n_strx, ptr);
  617. }
  618. free(stabbuff);
  619. }