ReactOS Fundraising Campaign 2012
 
€ 4,410 / € 30,000

Information | Donate

Home | Info | Community | Development | myReactOS | Contact Us

  1. Home
  2. Community
  3. Development
  4. myReactOS
  5. Fundraiser 2012

  1. Main Page
  2. Alphabetical List
  3. Data Structures
  4. Directories
  5. File List
  6. Data Fields
  7. Globals
  8. Related Pages

ReactOS Development > Doxygen

dumpstab.c
Go to the documentation of this file.
00001 /*
00002  * Usage: dumpstab input-file
00003  *
00004  * There are two sources of information: the .stab/.stabstr
00005  * sections of the executable and the COFF symbol table. Most
00006  * of the information is in the .stab/.stabstr sections.
00007  * However, most of our asm files don't contain .stab directives,
00008  * so routines implemented in assembler won't show up in the
00009  * .stab section. They are present in the COFF symbol table.
00010  * So, we mostly use the .stab/.stabstr sections, but we augment
00011  * the info there with info from the COFF symbol table when
00012  * possible.
00013  *
00014  * This is a tool and is compiled using the host compiler,
00015  * i.e. on Linux gcc and not mingw-gcc (cross-compiler).
00016  * Therefore we can't include SDK headers and we have to
00017  * duplicate some definitions here.
00018  * Also note that the internal functions are "old C-style",
00019  * returning an int, where a return of 0 means success and
00020  * non-zero is failure.
00021  */
00022 
00023 #include <stdio.h>
00024 #include <string.h>
00025 #include <stdlib.h>
00026 #include <assert.h>
00027 
00028 #include "rsym.h"
00029 
00030 const char*
00031 stab_type_name ( int stab_type )
00032 {
00033     static char buf[32];
00034     switch ( stab_type )
00035     {
00036 #define X(n) case n: return #n;
00037         X(N_GYSM)
00038         X(N_FNAME)
00039         X(N_FUN)
00040         X(N_STSYM)
00041         X(N_LCSYM)
00042         X(N_MAIN)
00043         X(N_PC)
00044         X(N_NSYMS)
00045         X(N_NOMAP)
00046         X(N_RSYM)
00047         X(N_M2C)
00048         X(N_SLINE)
00049         X(N_DSLINE)
00050         X(N_BSLINE)
00051         //X(N_BROWS)
00052         X(N_DEFD)
00053         X(N_EHDECL)
00054         //X(N_MOD2)
00055         X(N_CATCH)
00056         X(N_SSYM)
00057         X(N_SO)
00058         X(N_LSYM)
00059         X(N_BINCL)
00060         X(N_SOL)
00061         X(N_PSYM)
00062         X(N_EINCL)
00063         X(N_ENTRY)
00064         X(N_LBRAC)
00065         X(N_EXCL)
00066         X(N_SCOPE)
00067         X(N_RBRAC)
00068         X(N_BCOMM)
00069         X(N_ECOMM)
00070         X(N_ECOML)
00071         X(N_LENG)
00072     }
00073     sprintf ( buf, "%lu", stab_type );
00074     return buf;
00075 }
00076 
00077 static int
00078 GetStabInfo(void *FileData, PIMAGE_FILE_HEADER PEFileHeader,
00079             PIMAGE_SECTION_HEADER PESectionHeaders,
00080             ULONG *StabSymbolsLength, void **StabSymbolsBase,
00081             ULONG *StabStringsLength, void **StabStringsBase)
00082 {
00083   ULONG Idx;
00084 
00085   /* Load .stab and .stabstr sections if available */
00086   *StabSymbolsBase = NULL;
00087   *StabSymbolsLength = 0;
00088   *StabStringsBase = NULL;
00089   *StabStringsLength = 0;
00090 
00091   for (Idx = 0; Idx < PEFileHeader->NumberOfSections; Idx++)
00092     {
00093       /* printf("section: '%.08s'\n", PESectionHeaders[Idx].Name); */
00094       if ((strncmp((char*)PESectionHeaders[Idx].Name, ".stab", 5) == 0)
00095         && (PESectionHeaders[Idx].Name[5] == 0))
00096         {
00097            /* printf(".stab section found. Size %d\n",
00098                PESectionHeaders[Idx].SizeOfRawData); */
00099 
00100            *StabSymbolsLength = PESectionHeaders[Idx].SizeOfRawData;
00101            *StabSymbolsBase = (void *)((char *) FileData + PESectionHeaders[Idx].PointerToRawData);
00102         }
00103 
00104       if (strncmp((char*)PESectionHeaders[Idx].Name, ".stabstr", 8) == 0)
00105         {
00106            /* printf(".stabstr section found. Size %d\n",
00107                PESectionHeaders[Idx].SizeOfRawData); */
00108 
00109            *StabStringsLength = PESectionHeaders[Idx].SizeOfRawData;
00110            *StabStringsBase = (void *)((char *) FileData + PESectionHeaders[Idx].PointerToRawData);
00111         }
00112     }
00113 
00114   return 0;
00115 }
00116 
00117 static void
00118 IterateStabs(ULONG StabSymbolsLength, void *StabSymbolsBase,
00119              ULONG StabStringsLength, void *StabStringsBase,
00120              ULONG_PTR ImageBase, PIMAGE_FILE_HEADER PEFileHeader,
00121              PIMAGE_SECTION_HEADER PESectionHeaders)
00122 {
00123   PSTAB_ENTRY e;
00124   ULONG Count, i;
00125 
00126   e = StabSymbolsBase;
00127   Count = StabSymbolsLength / sizeof(STAB_ENTRY);
00128   if (Count == 0) /* No symbol info */
00129     return;
00130 
00131   printf ( "type,other,desc,value,str\n" );
00132   for (i = 0; i < Count; i++)
00133     {
00134       printf ( "%s,%lu(0x%x),%lu(0x%x),%lu(0x%x),%s\n",
00135           stab_type_name(e[i].n_type),
00136           e[i].n_other,
00137           e[i].n_other,
00138           e[i].n_desc,
00139           e[i].n_desc,
00140           e[i].n_value,
00141           e[i].n_value,
00142           (char *) StabStringsBase + e[i].n_strx );
00143     }
00144 }
00145 
00146 int main(int argc, char* argv[])
00147 {
00148   PIMAGE_DOS_HEADER PEDosHeader;
00149   PIMAGE_FILE_HEADER PEFileHeader;
00150   PIMAGE_OPTIONAL_HEADER PEOptHeader;
00151   PIMAGE_SECTION_HEADER PESectionHeaders;
00152   ULONG ImageBase;
00153   void *StabBase;
00154   ULONG StabsLength;
00155   void *StabStringBase;
00156   ULONG StabStringsLength;
00157   char* path1;
00158   size_t FileSize;
00159   void *FileData;
00160 
00161   if (2 != argc)
00162     {
00163       fprintf(stderr, "Usage: dumpstabs <exefile>\n");
00164       exit(1);
00165     }
00166 
00167   path1 = convert_path(argv[1]);
00168 
00169   FileData = load_file ( path1, &FileSize );
00170   if ( !FileData )
00171   {
00172     fprintf ( stderr, "An error occured loading '%s'\n", path1 );
00173     exit(1);
00174   }
00175 
00176   /* Check if MZ header exists  */
00177   PEDosHeader = (PIMAGE_DOS_HEADER) FileData;
00178   if (PEDosHeader->e_magic != IMAGE_DOS_MAGIC || PEDosHeader->e_lfanew == 0L)
00179     {
00180       perror("Input file is not a PE image.\n");
00181       free(FileData);
00182       exit(1);
00183     }
00184 
00185   /* Locate PE file header  */
00186   /* sizeof(ULONG) = sizeof(MAGIC) */
00187   PEFileHeader = (PIMAGE_FILE_HEADER)((char *) FileData + PEDosHeader->e_lfanew + sizeof(ULONG));
00188 
00189   /* Locate optional header */
00190   assert(sizeof(ULONG) == 4);
00191   PEOptHeader = (PIMAGE_OPTIONAL_HEADER)(PEFileHeader + 1);
00192   ImageBase = PEOptHeader->ImageBase;
00193 
00194   /* Locate PE section headers  */
00195   PESectionHeaders = (PIMAGE_SECTION_HEADER)((char *) PEOptHeader + PEFileHeader->SizeOfOptionalHeader);
00196 
00197   if (GetStabInfo(FileData, PEFileHeader, PESectionHeaders, &StabsLength, &StabBase,
00198                   &StabStringsLength, &StabStringBase))
00199     {
00200       free(FileData);
00201       exit(1);
00202     }
00203 
00204   IterateStabs( StabsLength, StabBase, StabStringsLength, StabStringBase,
00205                 ImageBase, PEFileHeader, PESectionHeaders);
00206 
00207   free(FileData);
00208 
00209   return 0;
00210 }

Generated on Sat May 26 2012 04:36:35 for ReactOS by doxygen 1.7.6.1

ReactOS is a registered trademark or a trademark of ReactOS Foundation in the United States and other countries.