home *** CD-ROM | disk | FTP | other *** search
- #include "wpch.hpp"
-
- typedef unsigned char unsigned_8;
- typedef unsigned short unsigned_16;
- typedef unsigned long unsigned_32;
- #include <unistd.h>
- #include <stdio.h>
- #include <fcntl.h>
- #include <malloc.h>
- #include <stddef.h>
-
- #pragma pack(push,1);
-
- /* DOS EXE file header */
- /* =================== */
-
- typedef struct dos_exe_header {
- unsigned_16 signature; /* signature to mark valid EXE file */
- unsigned_16 mod_size; /* length of image mod 512 */
- unsigned_16 file_size; /* number of 512 byte pages */
- unsigned_16 num_relocs; /* number of relocation items */
- unsigned_16 hdr_size; /* size of header (in paragraphs) */
- unsigned_16 min_16; /* minimum # of paragraphs */
- unsigned_16 max_16; /* maximum # of paragraphs */
- unsigned_16 SS_offset; /* offset of SS within load module */
- unsigned_16 SP; /* value for SP */
- unsigned_16 chk_sum; /* check sum */
- unsigned_16 IP; /* value for IP */
- unsigned_16 CS_offset; /* offset of CS within load module */
- unsigned_16 reloc_offset; /* offset to 1st relocation item */
- unsigned_16 overlay_num; /* overlay number (0 if resident) */
- } dos_exe_header;
-
-
- /* PE executable format structures */
-
- /* type of a [relative] virtual address */
- typedef unsigned_32 pe_va;
-
- /* PE header table types */
- enum {
- PE_TBL_EXPORT,
- PE_TBL_IMPORT,
- PE_TBL_RESOURCE,
- PE_TBL_EXCEPTION,
- PE_TBL_SECURITY,
- PE_TBL_FIXUP,
- PE_TBL_DEBUG,
- PE_TBL_DESCRIPTION,
- PE_TBL_MACHINE,
- PE_TBL_THREAD,
- PE_TBL_CALLBACKS,
- PE_TBL_NUMBER = 16
- };
-
- #define OLD_PE_TBL_NUMBER 9
-
- typedef struct {
- pe_va rva;
- unsigned_32 size;
- } pe_hdr_table_entry;
-
- /* PE header structure */
- typedef struct {
- unsigned_32 signature;
- unsigned_16 cpu_type;
- unsigned_16 num_objects;
- unsigned_32 time_stamp;
- unsigned_32 sym_table;
- unsigned_32 num_syms;
- unsigned_16 nt_hdr_size; /* # of bytes after the flags field */
- unsigned_16 flags;
- unsigned_16 magic; /* currently 0x10b */
- unsigned_8 lnk_major;
- unsigned_8 lnk_minor;
- unsigned_32 code_size;
- unsigned_32 init_data_size;
- unsigned_32 uninit_data_size;
- pe_va entry_rva;
- unsigned_32 code_base;
- unsigned_32 data_base;
- unsigned_32 image_base;
- unsigned_32 object_align;
- unsigned_32 file_align;
- unsigned_16 os_major;
- unsigned_16 os_minor;
- unsigned_16 user_major;
- unsigned_16 user_minor;
- unsigned_16 subsys_major;
- unsigned_16 subsys_minor;
- unsigned_32 rsvd1;
- unsigned_32 image_size;
- unsigned_32 header_size; //size of dos hdr, nt hdr, obj table & pad
- unsigned_32 file_checksum;
- unsigned_16 subsystem;
- unsigned_16 dll_flags;
- unsigned_32 stack_reserve_size;
- unsigned_32 stack_commit_size;
- unsigned_32 heap_reserve_size;
- unsigned_32 heap_commit_size;
- unsigned_32 tls_idx_addr;
- unsigned_32 num_tables;
- pe_hdr_table_entry table[PE_TBL_NUMBER];
- } pe_header;
-
-
- /* PE object table structure */
- #define PE_OBJ_NAME_LEN 8
- typedef struct {
- unsigned_8 name[PE_OBJ_NAME_LEN];
- unsigned_32 virtual_size;
- pe_va rva;
- unsigned_32 physical_size;
- unsigned_32 physical_offset;
- unsigned_32 relocs_rva;
- unsigned_32 linnum_rva;
- unsigned_16 num_relocs;
- unsigned_16 num_linnums;
- unsigned_32 flags;
- } pe_object;
-
- #pragma pack(pop);
-
- int Handle;
-
- #define DOS_SIGNATURE 0x5a4d
- #define OS2_EXE_HEADER_FOLLOWS 0x0040 /* reloc table offset 0x40 */
- #define NH_OFFSET 0x003c
- /*
- * Dump the Object Table.
- */
-
- bool FindObject( WString const &exe, WString &object, DWORD start, DWORD size )
- {
- unsigned_32 new_exe_off; /* offset of new exe head */
- pe_header pe_head; /* the pe_header */
- unsigned_16 i;
- pe_object *pe_obj;
- struct dos_exe_header dos_head;
- char name[PE_OBJ_NAME_LEN+1];
-
- class TheFile {
- public:
- TheFile( const char *name ) {
- handle = open( name, O_RDONLY | O_BINARY, 0 );
- }
- ~TheFile()
- {
- if( handle != -1 ) close( handle );
- }
- int handle;
- };
-
- TheFile file( exe );
- if( file.handle == -1 ) return( false );
- read( file.handle, &dos_head, sizeof( dos_head ) );
- if( dos_head.signature != DOS_SIGNATURE ) {
- return( false );
- }
- if( dos_head.reloc_offset != OS2_EXE_HEADER_FOLLOWS ) {
- return( false );
- }
- lseek( file.handle, NH_OFFSET, SEEK_SET );
- read( file.handle, &new_exe_off, sizeof( new_exe_off ) );
- lseek( file.handle, new_exe_off, SEEK_SET );
- read( file.handle, &pe_head, sizeof( pe_header ) );
- lseek( file.handle, new_exe_off + offsetof( pe_header, magic ) + pe_head.nt_hdr_size, SEEK_SET );
-
- if( pe_head.num_objects == 0 ) return( false );
- pe_obj = (pe_object*)malloc( pe_head.num_objects * sizeof(pe_object) );
- read( file.handle, pe_obj, pe_head.num_objects * sizeof(pe_object) );
- for( i = 0; i < pe_head.num_objects; i++ ) {
- if( pe_obj[i].rva >= start && pe_obj[i].rva < start+size ) {
- object += " ";
- memset( name, 0, sizeof( name ) );
- memcpy( name, pe_obj[i].name, sizeof( pe_obj[i].name ) );
- object += name;
- }
-
- }
- free( pe_obj );
- return( true );
- }
-
-