home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 10 / ioProg_10.iso / soft / optima / samples.z / ReadExe.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1996-03-22  |  5.6 KB  |  185 lines

  1. #include "wpch.hpp"
  2.  
  3. typedef unsigned char unsigned_8;
  4. typedef unsigned short unsigned_16;
  5. typedef unsigned long unsigned_32;
  6. #include <unistd.h>
  7. #include <stdio.h>
  8. #include <fcntl.h>
  9. #include <malloc.h>
  10. #include <stddef.h>
  11.  
  12. #pragma pack(push,1);
  13.  
  14. /* DOS EXE file header */
  15. /* =================== */
  16.  
  17. typedef struct dos_exe_header {
  18.     unsigned_16         signature;      /* signature to mark valid EXE file */
  19.     unsigned_16         mod_size;       /* length of image mod 512          */
  20.     unsigned_16         file_size;      /* number of 512 byte pages         */
  21.     unsigned_16         num_relocs;     /* number of relocation items       */
  22.     unsigned_16         hdr_size;       /* size of header (in paragraphs)   */
  23.     unsigned_16         min_16;         /* minimum # of paragraphs          */
  24.     unsigned_16         max_16;         /* maximum # of paragraphs          */
  25.     unsigned_16         SS_offset;      /* offset of SS within load module  */
  26.     unsigned_16         SP;             /* value for SP                     */
  27.     unsigned_16         chk_sum;        /* check sum                        */
  28.     unsigned_16         IP;             /* value for IP                     */
  29.     unsigned_16         CS_offset;      /* offset of CS within load module  */
  30.     unsigned_16         reloc_offset;   /* offset to 1st relocation item    */
  31.     unsigned_16         overlay_num;    /* overlay number (0 if resident)   */
  32. } dos_exe_header;
  33.  
  34.  
  35. /* PE executable format structures */
  36.  
  37. /* type of a [relative] virtual address */
  38. typedef    unsigned_32    pe_va;
  39.  
  40. /* PE header table types */
  41. enum {
  42.     PE_TBL_EXPORT,
  43.     PE_TBL_IMPORT,
  44.     PE_TBL_RESOURCE,
  45.     PE_TBL_EXCEPTION,
  46.     PE_TBL_SECURITY,
  47.     PE_TBL_FIXUP,
  48.     PE_TBL_DEBUG,
  49.     PE_TBL_DESCRIPTION,
  50.     PE_TBL_MACHINE,
  51.     PE_TBL_THREAD,
  52.     PE_TBL_CALLBACKS,
  53.     PE_TBL_NUMBER = 16
  54. };
  55.  
  56. #define OLD_PE_TBL_NUMBER 9
  57.  
  58. typedef struct {
  59.         pe_va        rva;
  60.     unsigned_32    size;
  61. } pe_hdr_table_entry;
  62.  
  63. /* PE header structure */
  64. typedef struct {
  65.     unsigned_32        signature;
  66.     unsigned_16        cpu_type;
  67.     unsigned_16        num_objects;
  68.     unsigned_32        time_stamp;
  69.     unsigned_32        sym_table;
  70.     unsigned_32        num_syms;
  71.     unsigned_16        nt_hdr_size;    /* # of bytes after the flags field */
  72.     unsigned_16        flags;
  73.     unsigned_16        magic;        /* currently 0x10b */
  74.     unsigned_8        lnk_major;
  75.     unsigned_8        lnk_minor;
  76.     unsigned_32        code_size;
  77.     unsigned_32        init_data_size;
  78.     unsigned_32        uninit_data_size;
  79.     pe_va        entry_rva;
  80.     unsigned_32        code_base;
  81.     unsigned_32        data_base;
  82.     unsigned_32        image_base;
  83.     unsigned_32        object_align;
  84.     unsigned_32        file_align;
  85.     unsigned_16        os_major;
  86.     unsigned_16        os_minor;
  87.     unsigned_16        user_major;
  88.     unsigned_16        user_minor;
  89.     unsigned_16        subsys_major;
  90.     unsigned_16        subsys_minor;
  91.     unsigned_32        rsvd1;
  92.     unsigned_32        image_size;
  93.     unsigned_32        header_size;  //size of dos hdr, nt hdr, obj table & pad
  94.     unsigned_32        file_checksum;
  95.     unsigned_16        subsystem;
  96.     unsigned_16        dll_flags;
  97.     unsigned_32        stack_reserve_size;
  98.     unsigned_32        stack_commit_size;
  99.     unsigned_32        heap_reserve_size;
  100.     unsigned_32        heap_commit_size;
  101.     unsigned_32        tls_idx_addr;
  102.     unsigned_32        num_tables;
  103.     pe_hdr_table_entry    table[PE_TBL_NUMBER];
  104. } pe_header;
  105.  
  106.  
  107. /* PE object table structure */
  108. #define PE_OBJ_NAME_LEN 8
  109. typedef struct {
  110.     unsigned_8        name[PE_OBJ_NAME_LEN];
  111.     unsigned_32        virtual_size;
  112.     pe_va        rva;
  113.     unsigned_32        physical_size;
  114.     unsigned_32        physical_offset;
  115.     unsigned_32        relocs_rva;
  116.     unsigned_32        linnum_rva;
  117.     unsigned_16        num_relocs;
  118.     unsigned_16        num_linnums;
  119.     unsigned_32        flags;
  120. } pe_object;
  121.  
  122. #pragma pack(pop);
  123.  
  124. int Handle;
  125.  
  126. #define DOS_SIGNATURE     0x5a4d
  127. #define OS2_EXE_HEADER_FOLLOWS    0x0040    /* reloc table offset 0x40 */
  128. #define NH_OFFSET    0x003c
  129. /*
  130.  * Dump the Object Table.
  131.  */
  132.  
  133. bool FindObject( WString const &exe, WString &object, DWORD start, DWORD size )
  134. {
  135.     unsigned_32        new_exe_off;    /* offset of new exe head  */
  136.     pe_header        pe_head;    /* the pe_header        */
  137.     unsigned_16        i;
  138.     pe_object        *pe_obj;
  139.     struct dos_exe_header    dos_head;
  140.     char            name[PE_OBJ_NAME_LEN+1];
  141.  
  142.     class TheFile {
  143.     public:
  144.         TheFile( const char *name ) {
  145.         handle = open( name, O_RDONLY | O_BINARY, 0 );
  146.         }
  147.         ~TheFile()
  148.         {
  149.         if( handle != -1 ) close( handle );
  150.         }
  151.         int handle;
  152.     };
  153.  
  154.     TheFile file( exe );
  155.     if( file.handle == -1 ) return( false );
  156.     read( file.handle, &dos_head, sizeof( dos_head ) );
  157.     if( dos_head.signature != DOS_SIGNATURE ) {
  158.         return( false );
  159.     }
  160.     if( dos_head.reloc_offset != OS2_EXE_HEADER_FOLLOWS ) {
  161.         return( false );
  162.     }
  163.     lseek( file.handle, NH_OFFSET, SEEK_SET );
  164.     read( file.handle, &new_exe_off, sizeof( new_exe_off ) );
  165.     lseek( file.handle, new_exe_off, SEEK_SET );
  166.     read( file.handle, &pe_head, sizeof( pe_header ) );
  167.     lseek( file.handle, new_exe_off + offsetof( pe_header, magic ) + pe_head.nt_hdr_size, SEEK_SET );
  168.  
  169.     if( pe_head.num_objects == 0 ) return( false );
  170.     pe_obj = (pe_object*)malloc( pe_head.num_objects * sizeof(pe_object) );
  171.     read( file.handle, pe_obj, pe_head.num_objects * sizeof(pe_object) );
  172.     for( i = 0; i < pe_head.num_objects; i++ ) {
  173.         if( pe_obj[i].rva >= start && pe_obj[i].rva < start+size ) {
  174.             object += " ";
  175.             memset( name, 0, sizeof( name ) );
  176.             memcpy( name, pe_obj[i].name, sizeof( pe_obj[i].name ) );
  177.             object += name;
  178.         }
  179.  
  180.     }
  181.     free( pe_obj );
  182.     return( true );
  183. }
  184.  
  185.