home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / dos_ency / app_n / objdump.c
Encoding:
C/C++ Source or Header  |  1988-08-11  |  5.2 KB  |  140 lines

  1. /*****************************************************************************
  2. *                                                                            *
  3. * OBJDUMP.C -- display contents of an object file                            *
  4. *                                                                            *
  5. *                                                                            *
  6. *     Compile:  msc objdump;   (Microsoft C version 4.0 or later)            *
  7. *     Link:     link objdump;                                                *
  8. *     Execute:  objdump <filename>                                           *
  9. *                                                                            *
  10. *****************************************************************************/
  11.  
  12. #include        <fcntl.h>
  13.  
  14. #define         TRUE    1
  15. #define         FALSE   0
  16.  
  17. main( argc, argv )
  18. int        argc;
  19. char       **argv;
  20. {
  21.         unsigned char        CurrentByte;
  22.         int     ObjFileHandle;
  23.         int     CurrentLineLength;                 /* length of output line */
  24.         int     ObjRecordNumber = 0;
  25.         int     ObjRecordLength;
  26.         int     ObjRecordOffset = 0;   /* offset into current object record */
  27.         char    ASCIIEquiv[17];
  28.         char    FormatString[24];
  29.         char    *ObjRecordName();
  30.         char    *memset();
  31.  
  32.  
  33. /* open the object file */
  34.  
  35.         ObjFileHandle = open( argv[1],O_BINARY );
  36.  
  37.         if( ObjFileHandle == -1 )
  38.         {
  39.           printf( "\nCan't open object file\n" );
  40.           exit( 1 );
  41.         }
  42.         
  43. /* process the object file character by character */
  44.  
  45.         while( read( ObjFileHandle, &CurrentByte, 1 ) )
  46.         {
  47.           switch( ObjRecordOffset ) /* action depends on offset into record */
  48.           {
  49.             case(0):                              /* start of object record */
  50.               printf( "\n\nRecord %d:  %02Xh %s",
  51.                 ++ObjRecordNumber, CurrentByte, ObjRecordName(CurrentByte) );
  52.               printf( "\n%02X ", CurrentByte );
  53.               ++ObjRecordOffset;
  54.               break;
  55.  
  56.             case(1):                          /* first byte of length field */
  57.               ObjRecordLength = CurrentByte;
  58.               ++ObjRecordOffset;
  59.               break;
  60.  
  61.             case(2):                         /* second byte of length field */
  62.               ObjRecordLength += CurrentByte << 8; /* compute record length */
  63.               printf( "%04Xh ", ObjRecordLength );           /* show length */
  64.               CurrentLineLength = 0;
  65.               memset( ASCIIEquiv,'\0', 17 );            /* zero this string */
  66.               ++ObjRecordOffset;
  67.               break;
  68.  
  69.             default:                    /* remaining bytes in object record */
  70.               printf( "%02X ", CurrentByte );                        /* hex */
  71.  
  72.               if( CurrentByte < 0x20 || CurrentByte > 0x7F )       /* ASCII */
  73.                 CurrentByte = '.';
  74.               ASCIIEquiv[CurrentLineLength++] = CurrentByte;
  75.  
  76.               if( CurrentLineLength == 16 ||   /* if end of output line ... */
  77.                  ObjRecordOffset == ObjRecordLength+2 )
  78.               {                                           /* ... display it */
  79.                 sprintf( FormatString, "%%%ds%%s\n         ",
  80.                   3*(16-CurrentLineLength)+2 );
  81.                 printf( FormatString, " ", ASCIIEquiv );
  82.                 memset( ASCIIEquiv, '\0', 17 );
  83.                 CurrentLineLength = 0;
  84.               }
  85.  
  86.               if( ++ObjRecordOffset == ObjRecordLength+3 )  /* if done .. */
  87.                 ObjRecordOffset = 0;          /* .. process another record */
  88.               break;
  89.           }
  90.         }
  91.  
  92.         if( CurrentLineLength )    /* display remainder of last output line */
  93.           printf( "  %s", ASCIIEquiv );
  94.  
  95.         close( ObjFileHandle );
  96.  
  97.         printf( "\n%d object records\n", ObjRecordNumber );
  98.  
  99.         return( 0 );
  100. }
  101.  
  102.  
  103. char *ObjRecordName( n )                       /* return object record name */
  104. int        n;                                            /* n = record type */
  105. {
  106.         int        i;
  107.  
  108.         static     struct
  109.         {
  110.           int        RecordNumber;
  111.           char       *RecordName;
  112.         }                RecordStruct[] =
  113.                          {
  114.                           0x80,"THEADR",
  115.                           0x88,"COMENT",
  116.                           0x8A,"MODEND",
  117.                           0x8C,"EXTDEF",
  118.                           0x8E,"TYPDEF",
  119.                           0x90,"PUBDEF",
  120.                           0x94,"LINNUM",
  121.                           0x96,"LNAMES",
  122.                           0x98,"SEGDEF",
  123.                           0x9A,"GRPDEF",
  124.                           0x9C,"FIXUPP",
  125.                           0xA0,"LEDATA",
  126.                           0xA2,"LIDATA",
  127.                           0xB0,"COMDEF",
  128.                           0x00,"******"
  129.                          };
  130.  
  131.         int      RecordTableSize = sizeof(RecordStruct)/sizeof(RecordStruct[0]);
  132.  
  133.  
  134.         for( i=0; i<RecordTableSize-1; i++ )         /* scan table for name */
  135.           if ( RecordStruct[i].RecordNumber == n )
  136.             break;
  137.  
  138.         return( RecordStruct[i].RecordName );
  139. }
  140.