From a18996b41f05c3d3dc271d10b0bb394e7d74de47 Mon Sep 17 00:00:00 2001 From: Nibble Date: Thu, 19 Mar 2009 23:22:14 +0100 Subject: [PATCH] * r_bin_pe - Fixed exports bug - Little refactoring --- libr/bin/format/pe/pe.c | 18 +++--- libr/bin/format/pe/pe.h | 2 +- libr/bin/format/pe/pe_specs.h | 103 ++++------------------------------ 3 files changed, 21 insertions(+), 102 deletions(-) diff --git a/libr/bin/format/pe/pe.c b/libr/bin/format/pe/pe.c index 2afc65d9fe..e1aaa5939e 100644 --- a/libr/bin/format/pe/pe.c +++ b/libr/bin/format/pe/pe.c @@ -29,6 +29,7 @@ static PE_DWord PE_(r_bin_pe_aux_rva_to_offset)(PE_(r_bin_pe_obj) *bin, PE_DWord return 0; } +#if 0 static PE_DWord PE_(r_bin_pe_aux_offset_to_rva)(PE_(r_bin_pe_obj) *bin, PE_DWord offset) { PE_(image_section_header) *shdrp; @@ -45,6 +46,7 @@ static PE_DWord PE_(r_bin_pe_aux_offset_to_rva)(PE_(r_bin_pe_obj) *bin, PE_DWord return 0; } +#endif static int PE_(r_bin_pe_do_checks)(PE_(r_bin_pe_obj) *bin) { @@ -240,7 +242,7 @@ int PE_(r_bin_pe_get_entrypoint)(PE_(r_bin_pe_obj) *bin, PE_(r_bin_pe_entrypoint int PE_(r_bin_pe_get_exports)(PE_(r_bin_pe_obj) *bin, PE_(r_bin_pe_export) *export) { - PE_DWord functions_offset, names_offset, ordinals_offset, function_rva, name_rva, name_offset; + PE_CWord functions_offset, names_offset, ordinals_offset, function_rva, name_rva, name_offset; PE_Word function_ordinal; PE_(r_bin_pe_export) *exportp; char function_name[PE_NAME_LENGTH], forwarder_name[PE_NAME_LENGTH]; @@ -248,7 +250,7 @@ int PE_(r_bin_pe_get_exports)(PE_(r_bin_pe_obj) *bin, PE_(r_bin_pe_export) *expo int i; PE_(image_data_directory) *data_dir_export = &bin->nt_headers->optional_header.DataDirectory[PE_IMAGE_DIRECTORY_ENTRY_EXPORT]; - PE_DWord export_dir_rva = data_dir_export->VirtualAddress; + PE_CWord export_dir_rva = data_dir_export->VirtualAddress; int export_dir_size = data_dir_export->Size; if (PE_(r_bin_pe_init_exports)(bin) == -1) @@ -263,12 +265,12 @@ int PE_(r_bin_pe_get_exports)(PE_(r_bin_pe_obj) *bin, PE_(r_bin_pe_export) *expo exportp = export; for (i = 0; i < bin->export_directory->NumberOfNames; i++, exportp++) { - lseek(bin->fd, functions_offset + i * sizeof(PE_DWord), SEEK_SET); - read(bin->fd, &function_rva, sizeof(PE_DWord)); + lseek(bin->fd, functions_offset + i * sizeof(PE_CWord), SEEK_SET); + read(bin->fd, &function_rva, sizeof(PE_CWord)); lseek(bin->fd, ordinals_offset + i * sizeof(PE_Word), SEEK_SET); read(bin->fd, &function_ordinal, sizeof(PE_Word)); - lseek(bin->fd, names_offset + i * sizeof(PE_DWord), SEEK_SET); - read(bin->fd, &name_rva, sizeof(PE_DWord)); + lseek(bin->fd, names_offset + i * sizeof(PE_CWord), SEEK_SET); + read(bin->fd, &name_rva, sizeof(PE_CWord)); name_offset = PE_(r_bin_pe_aux_rva_to_offset)(bin, name_rva); if (name_offset) { @@ -312,9 +314,9 @@ int PE_(r_bin_pe_get_file_alignment)(PE_(r_bin_pe_obj) *bin) return bin->nt_headers->optional_header.FileAlignment; } -PE_DWord PE_(r_bin_pe_get_image_base)(PE_(r_bin_pe_obj) *bin) +u64 PE_(r_bin_pe_get_image_base)(PE_(r_bin_pe_obj) *bin) { - return bin->nt_headers->optional_header.ImageBase; + return(u64)bin->nt_headers->optional_header.ImageBase; } int PE_(r_bin_pe_get_imports)(PE_(r_bin_pe_obj) *bin, PE_(r_bin_pe_import) *import) diff --git a/libr/bin/format/pe/pe.h b/libr/bin/format/pe/pe.h index d55ea5d7a0..6451b69be4 100644 --- a/libr/bin/format/pe/pe.h +++ b/libr/bin/format/pe/pe.h @@ -70,7 +70,7 @@ int PE_(r_bin_pe_get_entrypoint)(PE_(r_bin_pe_obj)*, PE_(r_bin_pe_entrypoint)*); int PE_(r_bin_pe_get_exports)(PE_(r_bin_pe_obj)*, PE_(r_bin_pe_export)*); int PE_(r_bin_pe_get_exports_count)(PE_(r_bin_pe_obj)*); int PE_(r_bin_pe_get_file_alignment)(PE_(r_bin_pe_obj)*); -PE_DWord PE_(r_bin_pe_get_image_base)(PE_(r_bin_pe_obj)*); +u64 PE_(r_bin_pe_get_image_base)(PE_(r_bin_pe_obj)*); int PE_(r_bin_pe_get_image_size)(PE_(r_bin_pe_obj)*); int PE_(r_bin_pe_get_imports)(PE_(r_bin_pe_obj)*, PE_(r_bin_pe_import)*); int PE_(r_bin_pe_get_imports_count)(PE_(r_bin_pe_obj)*); diff --git a/libr/bin/format/pe/pe_specs.h b/libr/bin/format/pe/pe_specs.h index 5dcc176bea..04278b6ccd 100644 --- a/libr/bin/format/pe/pe_specs.h +++ b/libr/bin/format/pe/pe_specs.h @@ -5,6 +5,7 @@ #undef ILT_MASK2 #undef PE_Word #undef PE_DWord +#undef PE_CWord #ifdef R_BIN_PE64 #define PE_(name) Pe64_##name @@ -12,12 +13,14 @@ #define ILT_MASK2 0x7fffffffffffffffLL #define PE_Word u16 #define PE_DWord u64 +#define PE_CWord u32 #else #define PE_(name) Pe32_##name #define ILT_MASK1 0x80000000 #define ILT_MASK2 0x7fffffff #define PE_Word u16 #define PE_DWord u32 +#define PE_CWord u32 #endif #ifndef _INCLUDE_R_BIN_PE_SPECS_H_ @@ -46,29 +49,7 @@ typedef struct { u16 e_oeminfo; /* 26: OEM information; e_oemid specific */ u16 e_res2[10]; /* 28: Reserved words */ u32 e_lfanew; /* 3c: Offset to extended header */ -} Pe32_image_dos_header; - -typedef struct { - u16 e_magic; /* 00: MZ Header signature */ - u16 e_cblp; /* 02: Bytes on last page of file */ - u16 e_cp; /* 04: Pages in file */ - u16 e_crlc; /* 06: Relocations */ - u16 e_cparhdr; /* 08: Size of header in paragraphs */ - u16 e_minalloc; /* 0a: Minimum extra paragraphs needed */ - u16 e_maxalloc; /* 0c: Maximum extra paragraphs needed */ - u16 e_ss; /* 0e: Initial (relative) SS value */ - u16 e_sp; /* 10: Initial SP value */ - u16 e_csum; /* 12: Checksum */ - u16 e_ip; /* 14: Initial IP value */ - u16 e_cs; /* 16: Initial (relative) CS value */ - u16 e_lfarlc; /* 18: File address of relocation table */ - u16 e_ovno; /* 1a: Overlay number */ - u16 e_res[4]; /* 1c: Reserved words */ - u16 e_oemid; /* 24: OEM identifier (for e_oeminfo) */ - u16 e_oeminfo; /* 26: OEM information; e_oemid specific */ - u16 e_res2[10]; /* 28: Reserved words */ - u32 e_lfanew; /* 3c: Offset to extended header */ -} Pe64_image_dos_header; +} Pe32_image_dos_header, Pe64_image_dos_header; #define PE_IMAGE_FILE_TYPE_PE32 0x10b #define PE_IMAGE_FILE_TYPE_PE32PLUS 0x20b @@ -129,17 +110,7 @@ typedef struct { u32 NumberOfSymbols; u16 SizeOfOptionalHeader; u16 Characteristics; -} Pe32_image_file_header; - -typedef struct { - u16 Machine; - u16 NumberOfSections; - u32 TimeDateStamp; - u32 PointerToSymbolTable; - u32 NumberOfSymbols; - u16 SizeOfOptionalHeader; - u16 Characteristics; -} Pe64_image_file_header; +} Pe32_image_file_header, Pe64_image_file_header; #define PE_IMAGE_DIRECTORY_ENTRIES 16 #define PE_IMAGE_DIRECTORY_ENTRY_EXPORT 0 @@ -174,12 +145,7 @@ typedef struct { typedef struct { u32 VirtualAddress; u32 Size; -} Pe32_image_data_directory; - -typedef struct { - u32 VirtualAddress; - u32 Size; -} Pe64_image_data_directory; +} Pe32_image_data_directory, Pe64_image_data_directory; typedef struct { /* Standard fields */ @@ -273,23 +239,7 @@ typedef struct { u16 NumberOfRelocations; u16 NumberOfLinenumbers; u32 Characteristics; -} Pe32_image_section_header; - -typedef struct { - u8 Name[PE_IMAGE_SIZEOF_SHORT_NAME]; - union { - u32 PhysicalAddress; - u32 VirtualSize; - } Misc; - u32 VirtualAddress; - u32 SizeOfRawData; - u32 PointerToRawData; - u32 PointerToRelocations; - u32 PointerToLinenumbers; - u16 NumberOfRelocations; - u16 NumberOfLinenumbers; - u32 Characteristics; -} Pe64_image_section_header; +} Pe32_image_section_header, Pe64_image_section_header; typedef struct { u32 Characteristics; @@ -303,21 +253,7 @@ typedef struct { u32 AddressOfFunctions; u32 AddressOfNames; u32 AddressOfOrdinals; -} Pe32_image_export_directory; - -typedef struct { - u32 Characteristics; - u32 TimeDateStamp; - u16 MajorVersion; - u16 MinorVersion; - u32 Name; - u32 Base; - u32 NumberOfFunctions; - u32 NumberOfNames; - u32 AddressOfFunctions; - u32 AddressOfNames; - u32 AddressOfOrdinals; -} Pe64_image_export_directory; +} Pe32_image_export_directory, Pe64_image_export_directory; typedef struct { u32 Characteristics; @@ -325,15 +261,7 @@ typedef struct { u32 ForwarderChain; u32 Name; u32 FirstThunk; -} Pe32_image_import_directory; - -typedef struct { - u32 Characteristics; - u32 TimeDateStamp; - u32 ForwarderChain; - u32 Name; - u32 FirstThunk; -} Pe64_image_import_directory; +} Pe32_image_import_directory, Pe64_image_import_directory; typedef struct { u32 Attributes; @@ -344,18 +272,7 @@ typedef struct { u32 BoundDelayImportTable; u32 UnloadDelayImportTable; u32 TimeStamp; -} Pe32_image_delay_import_directory; - -typedef struct { - u32 Attributes; - u32 Name; - u32 ModuleHandle; - u32 DelayImportAddressTable; - u32 DelayImportNameTable; - u32 BoundDelayImportTable; - u32 UnloadDelayImportTable; - u32 TimeStamp; -} Pe64_image_delay_import_directory; +} Pe32_image_delay_import_directory, Pe64_image_delay_import_directory; typedef struct { u32 Signature;