Rizin
unix-like reverse engineering framework and cli tools
pe.h
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: 2008 nibble <nibble.ds@gmail.com>
2 // SPDX-License-Identifier: LGPL-3.0-only
3 
4 #include <rz_types.h>
5 #include <rz_util.h>
6 #include <rz_lib.h>
7 #include <rz_bin.h>
8 
9 #include "pe_specs.h"
10 #include "dotnet.h"
11 
12 #ifndef _INCLUDE_RZ_BIN_PE_H_
13 #define _INCLUDE_RZ_BIN_PE_H_
14 
15 #define PE_READ_STRUCT_FIELD(var, struct_type, field, size) var->field = rz_read_le##size(buf + offsetof(struct_type, field))
16 
17 #define RZ_BIN_PE_SCN_IS_SHAREABLE(x) x &PE_IMAGE_SCN_MEM_SHARED
18 #define RZ_BIN_PE_SCN_IS_EXECUTABLE(x) x &PE_IMAGE_SCN_MEM_EXECUTE
19 #define RZ_BIN_PE_SCN_IS_READABLE(x) x &PE_IMAGE_SCN_MEM_READ
20 #define RZ_BIN_PE_SCN_IS_WRITABLE(x) x &PE_IMAGE_SCN_MEM_WRITE
21 
22 // SECTION FLAGS FOR EXE/PE/DLL START
23 #define IMAGE_SCN_TYPE_NO_PAD 0x00000008 // The section should not be padded to the next boundary. This flag is obsolete and is replaced by #define IMAGE_SCN_ALIGN_1BYTES. This is valid only for object files.
24 #define IMAGE_SCN_CNT_CODE 0x00000020 // The section contains executable code.
25 #define IMAGE_SCN_CNT_INITIALIZED_DATA 0x00000040 // The section contains initialized data.
26 #define IMAGE_SCN_CNT_UNINITIALIZED_DATA 0x00000080 // The section contains uninitialized data.
27 #define IMAGE_SCN_LNK_OTHER 0x00000100 // Reserved for future use.
28 #define IMAGE_SCN_LNK_INFO 0x00000200 // The section contains comments or other information. The .drectve section has this type. This is valid for object files only.
29 #define IMAGE_SCN_LNK_REMOVE 0x00000800 // The section will not become part of the image. This is valid only for object files.
30 #define IMAGE_SCN_LNK_COMDAT 0x00001000 // The section contains COMDAT data. For more information, see COMDAT Sections (Object Only). This is valid only for object files.
31 #define IMAGE_SCN_GPREL 0x00008000 // The section contains data referenced through the global pointer (GP).
32 #define IMAGE_SCN_MEM_PURGEABLE 0x00020000 // Reserved for future use.
33 #define IMAGE_SCN_MEM_16BIT 0x00020000 // Reserved for future use.
34 #define IMAGE_SCN_MEM_LOCKED 0x00040000 // Reserved for future use.
35 #define IMAGE_SCN_MEM_PRELOAD 0x00080000 // Reserved for future use.
36 #define IMAGE_SCN_ALIGN_1BYTES 0x00100000 // Align data on a 1-byte boundary. Valid only for object files.
37 #define IMAGE_SCN_ALIGN_2BYTES 0x00200000 // Align data on a 2-byte boundary. Valid only for object files.
38 #define IMAGE_SCN_ALIGN_4BYTES 0x00300000 // Align data on a 4-byte boundary. Valid only for object files.
39 #define IMAGE_SCN_ALIGN_8BYTES 0x00400000 // Align data on an 8-byte boundary. Valid only for object files.
40 #define IMAGE_SCN_ALIGN_16BYTES 0x00500000 // Align data on a 16-byte boundary. Valid only for object files.
41 #define IMAGE_SCN_ALIGN_32BYTES 0x00600000 // Align data on a 32-byte boundary. Valid only for object files.
42 #define IMAGE_SCN_ALIGN_64BYTES 0x00700000 // Align data on a 64-byte boundary. Valid only for object files.
43 #define IMAGE_SCN_ALIGN_128BYTES 0x00800000 // Align data on a 128-byte boundary. Valid only for object files.
44 #define IMAGE_SCN_ALIGN_256BYTES 0x00900000 // Align data on a 256-byte boundary. Valid only for object files.
45 #define IMAGE_SCN_ALIGN_512BYTES 0x00A00000 // Align data on a 512-byte boundary. Valid only for object files.
46 #define IMAGE_SCN_ALIGN_1024BYTES 0x00B00000 // Align data on a 1024-byte boundary. Valid only for object files.
47 #define IMAGE_SCN_ALIGN_2048BYTES 0x00C00000 // Align data on a 2048-byte boundary. Valid only for object files.
48 #define IMAGE_SCN_ALIGN_4096BYTES 0x00D00000 // Align data on a 4096-byte boundary. Valid only for object files.
49 #define IMAGE_SCN_ALIGN_8192BYTES 0x00E00000 // Align data on an 8192-byte boundary. Valid only for object files.
50 #define IMAGE_SCN_LNK_NRELOC_OVFL 0x01000000 // The section contains extended relocations.
51 #define IMAGE_SCN_MEM_DISCARDABLE 0x02000000 // The section can be discarded as needed.
52 #define IMAGE_SCN_MEM_NOT_CACHED 0x04000000 // The section cannot be cached.
53 #define IMAGE_SCN_MEM_NOT_PAGED 0x08000000 // The section is not pageable.
54 
55 #define PE_SCN_ALIGN_MASK 0x00F00000
56 
61 };
62 
71  int last;
72 };
73 
81  int last;
82 };
83 
91  int last;
92 };
93 
95  char string[PE_STRING_LENGTH];
99  char type;
100  int last;
101 };
102 
105  int last;
106 };
107 
108 typedef struct _PE_RESOURCE {
109  char *timestr;
110  char *type;
111  char *language;
112  char *name;
115 
116 #define GUIDSTR_LEN 41
117 #define DBG_FILE_NAME_LEN 255
118 
119 typedef struct SDebugInfo {
123 
124 #endif
125 
126 #define RzBinPEObj struct PE_(rz_bin_pe_obj_t)
127 struct PE_(rz_bin_pe_obj_t) {
128  // these pointers contain a copy of the headers and sections!
129  PE_(image_dos_header) * dos_header;
130  PE_(image_nt_headers) * nt_headers;
131  PE_(image_optional_header) * optional_header; // not free this just pointer into nt_headers
132  PE_(image_data_directory) * data_directory; // not free this just pointer into nt_headers
133  PE_(image_section_header) * section_header;
134  PE_(image_export_directory) * export_directory;
135  PE_(image_import_directory) * import_directory;
136  PE_(image_tls_directory) * tls_directory;
137  Pe_image_resource_directory *resource_directory;
138  PE_(image_delay_import_directory) * delay_import_directory;
139  Pe_image_security_directory *security_directory;
140 
141  Pe_image_clr *clr; // dotnet information
142 
143  /* store the section information for future use */
145 
146  // these values define the real offset into the untouched binary
147  ut64 rich_header_offset;
148  ut64 nt_header_offset;
149  ut64 section_header_offset;
150  ut64 import_directory_offset;
151  ut64 export_directory_offset;
152  ut64 resource_directory_offset;
153  ut64 delay_import_directory_offset;
154 
155  int import_directory_size;
156  ut64 size;
157  int num_sections;
158  int endian;
159  bool verbose;
160  int big_endian;
161  RzList *rich_entries;
162  RzList *relocs;
163  RzList *resources; // RzList of rz_pe_resources
164  const char *file;
165  RzBuffer *b;
166  Sdb *kv;
167  RCMS *cms;
168  SpcIndirectDataContent *spcinfo;
169  char *authentihash;
170  bool is_authhash_valid;
171  bool is_signed;
172  RzHash *hash;
173 };
174 
175 #define MAX_METADATA_STRING_LENGTH 256
176 #define COFF_SYMBOL_SIZE 18
177 #define PE_READ_STRUCT_FIELD(var, struct_type, field, size) var->field = rz_read_le##size(buf + offsetof(struct_type, field))
178 
179 // pe_clr.c
183 
184 // pe_debug.c
186 
187 // pe_exports.c
190 
191 // pe_hdr.c
193 
194 // pe_imports.c
196 int PE_(read_image_import_directory)(RzBuffer *b, ut64 addr, PE_(image_import_directory) * import_dir);
197 int PE_(read_image_delay_import_directory)(RzBuffer *b, ut64 addr, PE_(image_delay_import_directory) * directory);
199 
200 // pe_info.c
224 
225 // pe_overlay.c
228 
229 // pe_rsrc.c
233 
234 // pe_section.c
239 
240 // pe_security.c
245 void PE_(free_security_directory)(Pe_image_security_directory *security_directory);
246 
247 // pe_tls.c
249 
250 // pe.c
255 RzBinPEObj *PE_(rz_bin_pe_new)(const char *file, bool verbose);
RzList * sections(RzBinFile *bf)
Definition: bin_ne.c:110
RzList * relocs(RzBinFile *bf)
Definition: bin_ne.c:114
static ut64 rva(RzBinObject *o, ut64 paddr, ut64 vaddr, int va)
Definition: cbin.c:77
#define RZ_API
voidpf void uLong size
Definition: ioapi.h:138
voidpf void * buf
Definition: ioapi.h:138
uint8_t ut8
Definition: lh5801.h:11
directory
Definition: regress.py:17
int PE_() rz_bin_pe_is_stripped_debug(RzBinPEObj *bin)
Definition: pe_info.c:377
struct rz_bin_pe_addr_t *PE_() check_unknow(RzBinPEObj *bin)
Definition: pe_info.c:879
int PE_() rz_bin_pe_get_image_size(RzBinPEObj *bin)
Definition: pe_info.c:505
int PE_() rz_bin_pe_get_debug_data(RzBinPEObj *bin, SDebugInfo *res)
Definition: pe_debug.c:158
PE_DWord PE_() bin_pe_va_to_rva(RzBinPEObj *bin, PE_DWord va)
Definition: pe.c:32
struct rz_bin_pe_export_t *PE_() rz_bin_pe_get_exports(RzBinPEObj *bin)
Definition: pe_exports.c:162
ut64 PE_() rz_bin_pe_get_clr_methoddef_offset(RzBinPEObj *bin, Pe_image_metadata_methoddef *methoddef)
Definition: pe_clr.c:104
int PE_() rz_bin_pe_is_big_endian(RzBinPEObj *bin)
Definition: pe_info.c:343
struct rz_bin_pe_addr_t *PE_() rz_bin_pe_get_main_vaddr(RzBinPEObj *bin)
Definition: pe_info.c:917
void PE_() rz_bin_pe_check_sections(RzBinPEObj *bin, struct rz_bin_pe_section_t **sects)
Definition: pe_section.c:10
int PE_() bin_pe_init_tls(RzBinPEObj *bin)
Definition: pe_tls.c:71
RzList *PE_() section_flag_to_rzlist(ut64 flag)
Definition: mach0.c:2378
int PE_() bin_pe_init_overlay(RzBinPEObj *bin)
Definition: pe_overlay.c:67
struct rz_bin_pe_addr_t *PE_() rz_bin_pe_get_entrypoint(RzBinPEObj *bin)
Definition: pe_info.c:509
int PE_() rz_bin_pe_is_stripped_local_syms(RzBinPEObj *bin)
Definition: pe_info.c:370
char *PE_() bin_pe_compute_authentihash(RzBinPEObj *bin)
Definition: pe_security.c:21
int PE_() read_image_delay_import_directory(RzBuffer *b, ut64 addr, PE_(image_delay_import_directory) *directory)
Definition: pe_imports.c:24
int PE_() bin_pe_init_exports(RzBinPEObj *bin)
Definition: pe_exports.c:140
char *PE_() rz_bin_pe_get_subsystem(RzBinPEObj *bin)
Definition: pe_info.c:159
char *PE_() rz_bin_pe_get_machine(RzBinPEObj *bin)
Definition: pe_info.c:24
char *PE_() rz_bin_pe_get_arch(RzBinPEObj *bin)
Definition: pe_info.c:114
struct PE_(rz_bin_pe_obj_t)
Definition: pe.h:127
void *PE_() rz_bin_pe_free(RzBinPEObj *bin)
Definition: pe.c:88
#define DBG_FILE_NAME_LEN
Definition: pe.h:117
int PE_() read_image_import_directory(RzBuffer *b, ut64 addr, PE_(image_import_directory) *import_dir)
Definition: pe_imports.c:8
int PE_() bin_pe_get_claimed_checksum(RzBinPEObj *bin)
Definition: pe_info.c:221
int PE_() bin_pe_init_security(RzBinPEObj *bin)
Definition: pe_security.c:71
struct rz_bin_pe_import_t *PE_() rz_bin_pe_get_imports(RzBinPEObj *bin)
Definition: pe_imports.c:198
int PE_() bin_pe_init_imports(RzBinPEObj *bin)
Definition: pe_imports.c:325
struct rz_bin_pe_lib_t *PE_() rz_bin_pe_get_libs(RzBinPEObj *bin)
Definition: pe_info.c:384
struct SDebugInfo SDebugInfo
int PE_() bin_pe_get_actual_checksum(RzBinPEObj *bin)
Definition: pe_info.c:249
#define RzBinPEObj
Definition: pe.h:126
void PE_() bin_pe_init_rich_info(RzBinPEObj *bin)
Definition: pe_rsrc.c:1101
RZ_API void PE_() bin_pe_parse_resource(RzBinPEObj *bin)
Definition: pe_rsrc.c:1523
PE_DWord PE_() bin_pe_rva_to_paddr(RzBinPEObj *bin, PE_DWord rva)
Definition: pe.c:15
RZ_OWN RzList *PE_() rz_bin_pe_get_clr_symbols(RzBinPEObj *bin)
Definition: pe_clr.c:8
ut64 PE_() rz_bin_pe_get_image_base(RzBinPEObj *bin)
Definition: pe_info.c:588
char *PE_() rz_bin_pe_get_cc(RzBinPEObj *bin)
Definition: pe_info.c:201
int PE_() bin_pe_init_clr(RzBinPEObj *bin)
Definition: pe_clr.c:122
void PE_() free_security_directory(Pe_image_security_directory *security_directory)
Definition: pe_security.c:156
int PE_() bin_pe_init_hdr(RzBinPEObj *bin)
Definition: pe_hdr.c:124
int PE_() bin_pe_init_sections(RzBinPEObj *bin)
Definition: pe_section.c:333
RzBinPEObj *PE_() rz_bin_pe_new_buf(RzBuffer *buf, bool verbose)
Definition: pe.c:116
struct _PE_RESOURCE rz_pe_resource
int PE_() rz_bin_pe_is_stripped_relocs(RzBinPEObj *bin)
Definition: pe_info.c:356
char *PE_() rz_bin_pe_get_os(RzBinPEObj *bin)
Definition: pe_info.c:69
char *PE_() rz_bin_pe_get_class(RzBinPEObj *bin)
Definition: pe_info.c:103
RzBinPEObj *PE_() rz_bin_pe_new(const char *file, bool verbose)
int PE_() rz_bin_pe_is_dll(RzBinPEObj *bin)
Definition: pe_info.c:324
struct rz_bin_pe_addr_t *PE_() check_msvcseh(RzBinPEObj *bin)
Definition: pe_info.c:623
int PE_() bin_pe_init_resource(RzBinPEObj *bin)
Definition: pe_rsrc.c:50
PE_DWord PE_() bin_pe_rva_to_va(RzBinPEObj *bin, PE_DWord rva)
Definition: pe.c:28
int PE_() rz_bin_pe_get_bits(RzBinPEObj *bin)
Definition: pe_info.c:306
#define GUIDSTR_LEN
Definition: pe.h:116
int PE_() bin_pe_is_authhash_valid(RzBinPEObj *bin)
Definition: pe_security.c:67
int PE_() rz_bin_pe_is_stripped_line_nums(RzBinPEObj *bin)
Definition: pe_info.c:363
const char *PE_() bin_pe_get_authentihash(RzBinPEObj *bin)
Definition: pe_security.c:60
int PE_() bin_pe_get_overlay(RzBinPEObj *bin, ut64 *size)
Definition: pe_overlay.c:16
struct rz_bin_pe_section_t *PE_() rz_bin_pe_get_sections(RzBinPEObj *bin)
Definition: pe_section.c:243
struct rz_bin_pe_addr_t *PE_() check_mingw(RzBinPEObj *bin)
Definition: pe_info.c:822
#define PE_IMAGE_SIZEOF_SHORT_NAME
Definition: pe_specs.h:352
#define PE_NAME_LENGTH
Definition: pe_specs.h:36
#define PE_STRING_LENGTH
Definition: pe_specs.h:37
#define PE_DWord
Definition: pe_specs.h:27
#define RZ_OWN
Definition: rz_types.h:62
#define b(i)
Definition: sha256.c:42
Definition: pe_specs.h:501
Definition: pe.h:119
char file_name[DBG_FILE_NAME_LEN]
Definition: pe.h:121
char guidstr[GUIDSTR_LEN]
Definition: pe.h:120
char * language
Definition: pe.h:111
char * timestr
Definition: pe.h:109
Pe_image_resource_data_entry * data
Definition: pe.h:113
char * type
Definition: pe.h:110
char * name
Definition: pe.h:112
Definition: malloc.c:26
Definition: z80asm.h:102
ut64 vaddr
Definition: pe.h:58
ut64 paddr
Definition: pe.h:59
ut64 haddr
Definition: pe.h:60
ut8 forwarder[PE_NAME_LENGTH+1]
Definition: pe.h:87
ut64 vaddr
Definition: pe.h:88
ut8 libname[PE_NAME_LENGTH+1]
Definition: pe.h:86
ut64 paddr
Definition: pe.h:89
ut64 ordinal
Definition: pe.h:90
ut64 ordinal
Definition: pe.h:80
ut8 libname[PE_NAME_LENGTH+1]
Definition: pe.h:76
ut64 paddr
Definition: pe.h:78
ut64 vaddr
Definition: pe.h:77
ut64 hint
Definition: pe.h:79
int last
Definition: pe.h:105
ut64 vaddr
Definition: pe.h:67
ut64 vsize
Definition: pe.h:66
ut64 paddr
Definition: pe.h:68
ut64 flags
Definition: pe.h:69
ut64 paddr
Definition: pe.h:97
char type
Definition: pe.h:99
ut64 vaddr
Definition: pe.h:96
ut64 size
Definition: pe.h:98
Definition: sdb.h:63
ut64(WINAPI *w32_GetEnabledXStateFeatures)()
static int verbose
Definition: z80asm.c:73
static int file
Definition: z80asm.c:58
static int addr
Definition: z80asm.c:58