From 71170a4e9c65cad27984db86c57397a6be370012 Mon Sep 17 00:00:00 2001 From: Anton Kochkov Date: Wed, 17 Oct 2018 17:18:18 +0800 Subject: [PATCH] PDB switch to R_PACKED and utN types (#11830) Fix typo, use R_PACKED and use r2 types in the PDB parser --- libr/bin/pdb/tpi.c | 7 +- libr/bin/pdb/types.h | 527 +++++++++++++++++++++++-------------------- 2 files changed, 282 insertions(+), 252 deletions(-) diff --git a/libr/bin/pdb/tpi.c b/libr/bin/pdb/tpi.c index 9b83a82b04..f9bc7b5df6 100644 --- a/libr/bin/pdb/tpi.c +++ b/libr/bin/pdb/tpi.c @@ -438,7 +438,7 @@ static int get_procedure_arglist(void *type, void **ret_type) { static int get_member_index(void *type, void **ret_type) { STypeInfo *t = (STypeInfo *) type; SLF_MEMBER *lf = (SLF_MEMBER *) t->type_info; - int curr_idx = lf->inedex; + int curr_idx = lf->index; if (curr_idx < base_idx) { *ret_type = 0; @@ -635,7 +635,7 @@ static void get_sval_val(SVal *val, int *res) { // STypeInfo *t = (STypeInfo *) type; // SLF_MEMBER *lf_member = (SLF_MEMBER *)t->type_info; -// *indx_val = lf_member->inedex; +// *indx_val = lf_member->index; //} static void get_onemethod_name_len(void *type, int *res_len) { @@ -1526,6 +1526,7 @@ void deinit_scstring(SCString *cstr) { int parse_sctring(SCString *sctr, unsigned char *leaf_data, unsigned int *read_bytes, unsigned int len) { unsigned int c = 0; sctr->name = NULL; + sctr->size = 0; while (*leaf_data) { CAN_READ((*read_bytes + c), 1, len); c++; @@ -1726,7 +1727,7 @@ static int parse_lf_member(SLF_MEMBER *lf_member, unsigned char *leaf_data, unsi lf_member->offset.name_or_val = 0; READ2(*read_bytes, len, lf_member->fldattr.fldattr, leaf_data, ut16); - READ4(*read_bytes, len, lf_member->inedex, leaf_data, ut32); + READ4(*read_bytes, len, lf_member->index, leaf_data, ut32); tmp_read_bytes_before = *read_bytes; parse_sval(&lf_member->offset, leaf_data, read_bytes, len); diff --git a/libr/bin/pdb/types.h b/libr/bin/pdb/types.h index 73de274047..7834f2cd79 100644 --- a/libr/bin/pdb/types.h +++ b/libr/bin/pdb/types.h @@ -6,7 +6,8 @@ #define READ_PAGE_FAIL 0x01 -//TODO: MOVE TO GENERAL MACROSE +// TODO: Move to a general macroses in r_util/r_types + /////////////////////////////////////////////////////////////////////////////// #define GET_PAGE(pn, off, pos, page_size) { \ (pn) = (pos) / (page_size); \ @@ -465,18 +466,18 @@ typedef enum { typedef union { struct { - unsigned char scoped : 1; - unsigned char reserved : 7; // swapped - unsigned char packed : 1; - unsigned char ctor : 1; - unsigned char ovlops : 1; - unsigned char isnested : 1; - unsigned char cnested : 1; - unsigned char opassign : 1; - unsigned char opcast : 1; - unsigned char fwdref : 1; + ut8 scoped : 1; + ut8 reserved : 7; // swapped + ut8 packed : 1; + ut8 ctor : 1; + ut8 ovlops : 1; + ut8 isnested : 1; + ut8 cnested : 1; + ut8 opassign : 1; + ut8 opcast : 1; + ut8 fwdref : 1; } bits; - unsigned short cv_property; + ut16 cv_property; } UCV_PROPERTY; typedef enum { @@ -502,58 +503,62 @@ typedef enum { //# ordering for BitStructs typedef union { struct { - unsigned char access : 2; - unsigned char mprop : 3; - unsigned char pseudo : 1; - unsigned char noinherit : 1; - unsigned char noconstruct : 1; - unsigned char padding : 7; - unsigned char compgenx : 1; + ut8 access : 2; + ut8 mprop : 3; + ut8 pseudo : 1; + ut8 noinherit : 1; + ut8 noconstruct : 1; + ut8 padding : 7; + ut8 compgenx : 1; } bits; - unsigned short fldattr; + ut16 fldattr; } UCV_fldattr; +R_PACKED( typedef struct { - unsigned int return_type; + ut32 return_type; ECV_CALL call_conv; - unsigned char reserved; - unsigned short parm_count; - unsigned int arg_list; - unsigned char pad; -} SLF_PROCEDURE; + ut8 reserved; + ut16 parm_count; + ut32 arg_list; + ut8 pad; +}) SLF_PROCEDURE; +R_PACKED( typedef struct { - unsigned int return_type; - unsigned int class_type; - unsigned int this_type; + ut32 return_type; + ut32 class_type; + ut32 this_type; ECV_CALL call_conv; // 1 byte - unsigned char reserved; - unsigned short parm_count; - unsigned int arglist; - int this_adjust; - unsigned char pad; -} SLF_MFUNCTION; + ut8 reserved; + ut16 parm_count; + ut32 arglist; + st32 this_adjust; + ut8 pad; +}) SLF_MFUNCTION; +R_PACKED( typedef struct { - unsigned int count; - unsigned int *arg_type; - unsigned char pad; -} SLF_ARGLIST; + ut32 count; + ut32 *arg_type; + ut8 pad; +}) SLF_ARGLIST; +R_PACKED( typedef struct { - unsigned int modified_type; + ut32 modified_type; union { struct { - unsigned char pad2 : 8; - unsigned char const_ : 1; - unsigned char volatile_ : 1; - unsigned char unaligned : 1; - unsigned char pad1 : 5; + ut8 pad2 : 8; + ut8 const_ : 1; + ut8 volatile_ : 1; + ut8 unaligned : 1; + ut8 pad1 : 5; } bits; - unsigned short modifier; + ut16 modifier; } umodifier; - unsigned char pad; -} SLF_MODIFIER; + ut8 pad; +}) SLF_MODIFIER; typedef enum { ePTR_MODE_PTR = 0x00000000, @@ -582,32 +587,35 @@ typedef enum { eTypeMax } EType; +R_PACKED( typedef union { struct { - unsigned char pad[2]; - unsigned char flat32 : 1; - unsigned char volatile_ : 1; - unsigned char const_ : 1; - unsigned char unaligned : 1; - unsigned char restrict_ : 1; - unsigned char pad1 : 3; - unsigned char type : 5; - unsigned char mode : 3; + ut8 pad[2]; + ut8 flat32 : 1; + ut8 volatile_ : 1; + ut8 const_ : 1; + ut8 unaligned : 1; + ut8 restrict_ : 1; + ut8 pad1 : 3; + ut8 type : 5; + ut8 mode : 3; } bits; - unsigned int ptr_attr; -} UPTR_ATTR; + ut32 ptr_attr; +}) UPTR_ATTR; +R_PACKED( typedef struct { - unsigned int utype; + ut32 utype; UPTR_ATTR ptr_attr; - unsigned char pad; -} SLF_POINTER; + ut8 pad; +}) SLF_POINTER; +R_PACKED( typedef struct { - int stream_size; - int num_pages; - char *stream_pages; -} SPage; + st32 stream_size; + st32 num_pages; + ut8 *stream_pages; +}) SPage; typedef struct { // FILE *fp; @@ -656,148 +664,166 @@ typedef struct { f_load load; } SParsedPDBStream; +R_PACKED( typedef struct { char *name; - unsigned int size; -} SCString; + ut32 size; +}) SCString; +R_PACKED( typedef struct { SCString name; -} SNoVal; +}) SNoVal; +R_PACKED( typedef struct { char value; SCString name; -} SVal_LF_CHAR; +}) SVal_LF_CHAR; +R_PACKED( typedef struct { - short value; + st16 value; SCString name; -} SVal_LF_SHORT; +}) SVal_LF_SHORT; +R_PACKED( typedef struct { - unsigned short value; + ut16 value; SCString name; -} SVal_LF_USHORT; +}) SVal_LF_USHORT; typedef struct { - long value; + st32 value; SCString name; } SVal_LF_LONG; typedef struct { - unsigned long value; + ut32 value; SCString name; } SVal_LF_ULONG; typedef struct { - int64_t value; + st64 value; SCString name; } SVal_LF_QUADWORD; typedef struct { - uint64_t value; + ut64 value; SCString name; } SVal_LF_UQUADWORD; +R_PACKED( typedef struct { - unsigned short value_or_type; + ut16 value_or_type; void *name_or_val; -} SVal; +}) SVal; +R_PACKED( typedef struct { - unsigned int element_type; - unsigned int index_type; + ut32 element_type; + ut32 index_type; SVal size; - unsigned char pad; -} SLF_ARRAY; + ut8 pad; +}) SLF_ARRAY; +R_PACKED( typedef struct { - unsigned short count; + ut16 count; UCV_PROPERTY prop; - unsigned int field_list; - unsigned int derived; - unsigned int vshape; + ut32 field_list; + ut32 derived; + ut32 vshape; SVal size; - unsigned char pad; -} SLF_STRUCTURE, SLF_CLASS; + ut8 pad; +}) SLF_STRUCTURE, SLF_CLASS; +R_PACKED( typedef struct { - unsigned short count; + ut16 count; UCV_PROPERTY prop; - unsigned int field_list; + ut32 field_list; SVal size; - unsigned pad; -} SLF_UNION; + ut32 pad; +}) SLF_UNION; +R_PACKED( typedef struct { - unsigned int base_type; - unsigned char length; - unsigned char position; - unsigned char pad; -} SLF_BITFIELD; + ut32 base_type; + ut8 length; + ut8 position; + ut8 pad; +}) SLF_BITFIELD; +R_PACKED( typedef struct { - unsigned short count; + ut16 count; char *vt_descriptors; - unsigned char pad; -} SLF_VTSHAPE; + ut8 pad; +}) SLF_VTSHAPE; +R_PACKED( typedef struct { - unsigned short count; + ut16 count; UCV_PROPERTY prop; - unsigned int utype; - unsigned int field_list; + ut32 utype; + ut32 field_list; SCString name; - unsigned char pad; -} SLF_ENUM; + ut8 pad; +}) SLF_ENUM; +R_PACKED( typedef struct { UCV_fldattr fldattr; SVal enum_value; - unsigned char pad; + ut8 pad; free_func free_; -} SLF_ENUMERATE; +}) SLF_ENUMERATE; +R_PACKED( typedef struct { - unsigned short pad; - unsigned int index; + ut16 pad; + ut32 index; SCString name; free_func free_; -} SLF_NESTTYPE; +}) SLF_NESTTYPE; +R_PACKED( typedef struct { - unsigned short count; - unsigned int mlist; + ut16 count; + ut32 mlist; SCString name; - unsigned char pad; + ut8 pad; free_func free_; -} SLF_METHOD; +}) SLF_METHOD; +R_PACKED( typedef struct { UCV_fldattr fldattr; - unsigned int inedex; + ut32 index; SVal offset; - unsigned char pad; + ut8 pad; // TODO: remove free_ free_func free_; -} SLF_MEMBER; +}) SLF_MEMBER; +R_PACKED( typedef struct { - unsigned int val; + ut32 val; SCString str_data; -} SLF_ONEMETHOD_VAL; +}) SLF_ONEMETHOD_VAL; +R_PACKED( typedef struct { UCV_fldattr fldattr; - unsigned int index; + ut32 index; SLF_ONEMETHOD_VAL val; - unsigned char pad; -} SLF_ONEMETHOD; + ut8 pad; +}) SLF_ONEMETHOD; typedef struct { // ELeafType leaf_type; @@ -805,26 +831,26 @@ typedef struct { } SLF_FIELDLIST; typedef struct { - int off; - int cb; + st32 off; + st32 cb; } SOffCb; typedef struct { - short sn; - short padding; - int hash_key; - int buckets; + st16 sn; + st16 padding; + st32 hash_key; + st32 buckets; SOffCb hash_vals; SOffCb ti_off; SOffCb hash_adj; } STPI; typedef struct { - unsigned int version; - int hdr_size; - unsigned int ti_min; - unsigned int ti_max; - unsigned int follow_size; + ut32 version; + st32 hdr_size; + ut32 ti_min; + ut32 ti_max; + ut32 follow_size; STPI tpi; } STPIHeader; @@ -986,6 +1012,7 @@ typedef enum { eLF_MAX = 0xFFFFFFFF } ELeafType; +R_PACKED( typedef struct { ELeafType leaf_type; void *type_info; @@ -1012,15 +1039,16 @@ typedef struct { get_value is_fwdref; get_print_type_ get_print_type; -} STypeInfo; +}) STypeInfo; +R_PACKED( typedef struct { - unsigned short length; - unsigned int tpi_idx; + ut16 length; + ut32 tpi_idx; STypeInfo type_data; // free_func free_; -} SType; +}) SType; typedef struct { STPIHeader header; @@ -1030,18 +1058,18 @@ typedef struct { } STpiStream; typedef struct { - unsigned int data1; - unsigned short data2; - unsigned short data3; - char data4[8]; + ut32 data1; + ut16 data2; + ut16 data3; + ut8 data4[8]; } SGUID; typedef struct { - unsigned int version; - unsigned int time_date_stamp; - unsigned int age; + ut32 version; + ut32 time_date_stamp; + ut32 age; SGUID guid; - unsigned int cb_names; + ut32 cb_names; char *names; free_func free_; @@ -1057,79 +1085,75 @@ typedef enum { eMaxMachine } EMachine; -#pragma pack(push, 1) +R_PACKED( typedef struct { - short section; - short padding1; - int offset; - int size; - unsigned int flags; - int module; - short padding2; - unsigned int data_crc; - unsigned int reloc_crc; -} SSymbolRange; -#pragma pack(pop) + ut16 section; + ut16 padding1; + st32 offset; + st32 size; + ut32 flags; + st32 module; + st16 padding2; + ut32 data_crc; + ut32 reloc_crc; +}) SSymbolRange; -// #pragma pack(push, 1) +R_PACKED( typedef struct { - unsigned int opened; + ut32 opened; SSymbolRange range; - unsigned short flags; - short stream; - unsigned int symSize; - unsigned int oldLineSize; - unsigned int lineSize; - short nSrcFiles; - short padding1; - unsigned int offsets; - unsigned int niSource; - unsigned int niCompiler; + ut16 flags; + st16 stream; + ut32 symSize; + ut32 oldLineSize; + ut32 lineSize; + st16 nSrcFiles; + st16 padding1; + ut32 offsets; + ut32 niSource; + ut32 niCompiler; SCString modName; SCString objName; -} SDBIExHeader; -// #pragma pack(pop) +}) SDBIExHeader; -#pragma pack(push, 1) +R_PACKED( typedef struct { - short sn_fpo; - short sn_exception; - short sn_fixup; - short sn_omap_to_src; - short sn_omap_from_src; - short sn_section_hdr; - short sn_token_rid_map; - short sn_xdata; - short sn_pdata; - short sn_new_fpo; - short sn_section_hdr_orig; -} SDbiDbgHeader; -#pragma pack(pop) + st16 sn_fpo; + st16 sn_exception; + st16 sn_fixup; + st16 sn_omap_to_src; + st16 sn_omap_from_src; + st16 sn_section_hdr; + st16 sn_token_rid_map; + st16 sn_xdata; + st16 sn_pdata; + st16 sn_new_fpo; + st16 sn_section_hdr_orig; +}) SDbiDbgHeader; -#pragma pack(push, 1) +R_PACKED( typedef struct { - unsigned int magic; - unsigned int version; - unsigned int age; - short gssymStream; - unsigned short vers; - short pssymStream; - unsigned short pdbver; - short symrecStream; - unsigned short pdbver2; - unsigned int module_size; - unsigned int seccon_size; - unsigned int secmap_size; - unsigned int filinf_size; - unsigned int tsmap_size; - unsigned int mfc_index; - unsigned int dbghdr_size; - unsigned int ecinfo_size; - unsigned short flags; + ut32 magic; + ut32 version; + ut32 age; + st16 gssymStream; + ut16 vers; + st16 pssymStream; + ut16 pdbver; + st16 symrecStream; + ut16 pdbver2; + ut32 module_size; + ut32 seccon_size; + ut32 secmap_size; + ut32 filinf_size; + ut32 tsmap_size; + ut32 mfc_index; + ut32 dbghdr_size; + ut32 ecinfo_size; + ut16 flags; EMachine machine; // read just 2 bytes - unsigned int resvd; -} SDBIHeader; -#pragma pack(pop) + ut32 resvd; +}) SDBIHeader; typedef struct { SDBIHeader dbi_header; @@ -1143,23 +1167,24 @@ typedef struct { // start of FPO stream structures typedef union { struct { - unsigned char cbRegs : 3; - unsigned char fHashSEH : 1; - unsigned char fUseBp : 1; - unsigned char reserved : 1; - unsigned char cbFrame : 2; - unsigned char cbProlog : 8; + ut8 cbRegs : 3; + ut8 fHashSEH : 1; + ut8 fUseBp : 1; + ut8 reserved : 1; + ut8 cbFrame : 2; + ut8 cbProlog : 8; } bits; - unsigned short bit_values; + ut16 bit_values; } UBit_values; +R_PACKED( typedef struct { - unsigned int ul_off_start; - unsigned int cb_proc_size; - unsigned int cdw_locals; - unsigned short cdw_params; + ut32 ul_off_start; + ut32 cb_proc_size; + ut32 cdw_locals; + ut16 cdw_params; UBit_values bit_values; -} SFPO_DATA; +}) SFPO_DATA; typedef struct { RList *fpo_data_list; @@ -1172,17 +1197,18 @@ typedef enum { eFPO_DATA_FLAGS_MAX } EFPO_DATA_FLAGS; +R_PACKED( typedef struct { - unsigned int ul_off_start; - unsigned int cb_proc_size; - unsigned int cdw_locals; - unsigned int cdw_params; - unsigned int max_stack; - unsigned int programm_string_offset; - unsigned short cb_prolog; - unsigned short cb_save_regs; + ut32 ul_off_start; + ut32 cb_proc_size; + ut32 cdw_locals; + ut32 cdw_params; + ut32 max_stack; + ut32 programm_string_offset; + ut16 cb_prolog; + ut16 cb_save_regs; EFPO_DATA_FLAGS flags; -} SFPO_DATA_V2; +}) SFPO_DATA_V2; typedef struct { RList *fpo_data_list; @@ -1194,35 +1220,38 @@ typedef struct { RList *globals_list; } SGDATAStream; +R_PACKED ( typedef struct { - unsigned short leaf_type; - unsigned int symtype; - unsigned int offset; - unsigned short segment; + ut16 leaf_type; + ut32 symtype; + ut32 offset; + ut16 segment; SCString name; -} SGlobal; +}) SGlobal; // end GDATA structures // PE stream structures +// TODO: Support 64bit addressing! typedef union { - unsigned int physical_address; - unsigned int virtual_address; + ut32 physical_address; + ut32 virtual_address; } UMISC; #define PDB_SIZEOF_SECTION_NAME 8 +R_PACKED( typedef struct { char name[PDB_SIZEOF_SECTION_NAME]; UMISC misc; - unsigned int virtual_address; - unsigned int size_of_raw_data; - unsigned int pointer_to_raw_data; - unsigned int pointer_to_relocations; - unsigned int pointer_to_line_numbers; - unsigned short number_of_relocations; - unsigned short number_of_line_numbers; - unsigned int charactestics; -} SIMAGE_SECTION_HEADER; + ut32 virtual_address; + ut32 size_of_raw_data; + ut32 pointer_to_raw_data; + ut32 pointer_to_relocations; + ut32 pointer_to_line_numbers; + ut16 number_of_relocations; + ut16 number_of_line_numbers; + ut32 charactestics; +}) SIMAGE_SECTION_HEADER; typedef struct { RList *sections_hdrs; @@ -1231,13 +1260,13 @@ typedef struct { // omap structures typedef struct { - unsigned int from; - unsigned int to; + ut32 from; + ut32 to; } SOmapEntry; typedef struct { RList *omap_entries; - unsigned int *froms; + ut32 *froms; } SOmapStream; // end of omap structures