bffDump.cpp

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2001, Sun Microsystems, Inc
00003  * Copyright (C) 2001, The University of Queensland
00004  *
00005  * See the file "LICENSE.TERMS" for information on usage and
00006  * redistribution of this file, and for a DISCLAIMER OF ALL
00007  * WARRANTIES.
00008  *
00009  */
00010 
00011 /* File: bffDump_skel.cc
00012  * Desc: Skeleton driver for a binary-file dumper program. 
00013  * 
00014  * This file is a generic skeleton for a binary-file dumper, 
00015  * it dumps all the information it finds about sections in the
00016  * file, and it displays any code sections in raw hexadecimal 
00017  * notation. 
00018  */
00019 
00020 /*
00021  * $Revision: 1.4 $
00022  *
00023  *    Apr 01 - Cristina: Created
00024  * 11 May 01 - Nathan: Print text section name (suppresses warning)
00025  * 11 May 01 - Mike: use bCode (prints text section of hppa binaries)
00026  * 11 May 01 - Cristina: minor cleanup for generality
00027  * 02 Aug 01 - Brian: Fixed arg mismatch in printf.
00028  */
00029 
00030 // Include all binary file headers for different binary-file formats 
00031 // so that we can support them all. 
00032 #include "BinaryFile.h"
00033 #include "ElfBinaryFile.h"
00034 #include "ExeBinaryFile.h"
00035 #include "HpSomBinaryFile.h"
00036 #include "PalmBinaryFile.h"
00037 #include "Win32BinaryFile.h"
00038 
00039 
00040 int main(int argc, char* argv[])
00041 {
00042     // Usage
00043 
00044     if (argc != 2) {
00045         printf ("Usage: %s <filename>\n", argv[0]);
00046         printf ("%s dumps the contents of the given executable file\n", argv[0]); 
00047         return 1;
00048     }
00049 
00050     // Load the file
00051 
00052     BinaryFile *pbf = NULL;
00053     BinaryFileFactory bff; 
00054     pbf = bff.Load(argv[1]);
00055 
00056     if (pbf == NULL) {
00057     return 2;
00058     }
00059 
00060     // Display program and section information
00061     // If the DisplayDetails() function has not been implemented 
00062     // in the derived class (ElfBinaryFile in this case), then 
00063     // uncomment the commented code below to display section information.
00064 
00065     pbf->DisplayDetails (argv[0]); 
00066 
00067     // This is an alternative way of displaying binary-file information
00068     // by using individual sections.  The above approach is more general. 
00069     /*
00070     printf ("%d sections:\n", pbf->GetNumSections());
00071     for (int i=0; i < pbf->GetNumSections(); i++) 
00072     {
00073         SectionInfo* pSect = pbf->GetSectionInfo(i);
00074         printf("  Section %s at %X\n", pSect->pSectionName, pSect->uNativeAddr);
00075     }
00076     printf("\n");
00077     */
00078 
00079     // Display the code section in raw hexadecimal notation
00080     // Note: this is traditionally the ".text" section in Elf binaries.
00081     // In the case of Prc files (Palm), the code section is named "code0". 
00082 
00083     for (int i=0; i < pbf->GetNumSections(); i++) {
00084         SectionInfo* pSect = pbf->GetSectionInfo(i);
00085         if (pSect->bCode) {
00086             printf("  Code section:\n");
00087             ADDRESS a = pSect->uNativeAddr;
00088             unsigned char* p = (unsigned char*) pSect->uHostAddr;
00089             for (unsigned off = 0; off < pSect->uSectionSize; ) {
00090                 printf("%04X: ", a);
00091                 for (int j=0; (j < 16) && (off < pSect->uSectionSize); j++) {
00092                     printf("%02X ", *p++);
00093                     a++;
00094                     off++;
00095                 }
00096                 printf("\n");
00097             }
00098             printf("\n");
00099         }
00100     }
00101 
00102     // Display the data section(s) in raw hexadecimal notation
00103 
00104     for (int i=0; i < pbf->GetNumSections(); i++) {
00105         SectionInfo* pSect = pbf->GetSectionInfo(i);
00106         if (pSect->bData) {
00107             printf("  Data section: %s\n", pSect->pSectionName);
00108             ADDRESS a = pSect->uNativeAddr;
00109             unsigned char* p = (unsigned char*) pSect->uHostAddr;
00110             for (unsigned off = 0; off < pSect->uSectionSize; ) {
00111                 printf("%04X: ", a);
00112                 for (int j=0; (j < 16) && (off < pSect->uSectionSize); j++) {
00113                     printf("%02X ", *p++);
00114                     a++;
00115                     off++;
00116                 }
00117                 printf("\n");
00118             }
00119             printf("\n");
00120         }
00121     }
00122 
00123     pbf->UnLoad();
00124     return 0;
00125 }
00126 

Generated on Tue Sep 19 21:18:14 2006 for Boomerang by  doxygen 1.4.6