Rizin
unix-like reverse engineering framework and cli tools
pe.c File Reference
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <rz_types.h>
#include <rz_util.h>
#include <rz_hash.h>
#include "pe.h"
#include <time.h>

Go to the source code of this file.

Functions

PE_DWord PE_() bin_pe_rva_to_paddr (RzBinPEObj *bin, PE_DWord rva)
 
PE_DWord PE_() bin_pe_rva_to_va (RzBinPEObj *bin, PE_DWord rva)
 
PE_DWord PE_() bin_pe_va_to_rva (RzBinPEObj *bin, PE_DWord va)
 
static int bin_pe_init (RzBinPEObj *bin)
 
void *PE_() rz_bin_pe_free (RzBinPEObj *bin)
 
RzBinPEObj *PE_() rz_bin_pe_new_buf (RzBuffer *buf, bool verbose)
 

Function Documentation

◆ bin_pe_init()

static int bin_pe_init ( RzBinPEObj bin)
static

Definition at line 40 of file pe.c.

40  {
41  bin->dos_header = NULL;
42  bin->nt_headers = NULL;
43  bin->section_header = NULL;
44  bin->export_directory = NULL;
45  bin->import_directory = NULL;
46  bin->resource_directory = NULL;
47  bin->security_directory = NULL;
48  bin->delay_import_directory = NULL;
49  bin->optional_header = NULL;
50  bin->data_directory = NULL;
51  bin->big_endian = 0;
52  bin->cms = NULL;
53  bin->spcinfo = NULL;
54  if (!PE_(bin_pe_init_hdr)(bin)) {
55  RZ_LOG_ERROR("File is not PE\n");
56  return false;
57  }
58  if (!PE_(bin_pe_init_sections)(bin)) {
59  RZ_LOG_ERROR("Cannot initialize sections\n");
60  return false;
61  }
62  bin->sections = PE_(rz_bin_pe_get_sections)(bin);
64  (bin);
66  (bin);
68  (bin);
70  (bin);
71 
72  bin->big_endian = PE_(rz_bin_pe_is_big_endian)(bin);
73 
75  (bin);
77  (bin);
79  (bin);
81  (bin);
83  (bin);
84  bin->relocs = NULL;
85  return true;
86 }
#define NULL
Definition: cris-opc.c:27
int PE_() rz_bin_pe_is_big_endian(RzBinPEObj *bin)
Definition: pe_info.c:343
int PE_() bin_pe_init_tls(RzBinPEObj *bin)
Definition: pe_tls.c:71
int PE_() bin_pe_init_overlay(RzBinPEObj *bin)
Definition: pe_overlay.c:67
int PE_() bin_pe_init_exports(RzBinPEObj *bin)
Definition: pe_exports.c:140
int PE_() bin_pe_init_security(RzBinPEObj *bin)
Definition: pe_security.c:71
int PE_() bin_pe_init_imports(RzBinPEObj *bin)
Definition: pe_imports.c:325
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
int PE_() bin_pe_init_clr(RzBinPEObj *bin)
Definition: pe_clr.c:122
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
int PE_() bin_pe_init_resource(RzBinPEObj *bin)
Definition: pe_rsrc.c:50
struct rz_bin_pe_section_t *PE_() rz_bin_pe_get_sections(RzBinPEObj *bin)
Definition: pe_section.c:243
#define PE_(name)
Definition: pe_specs.h:23
#define RZ_LOG_ERROR(fmtstr,...)
Definition: rz_log.h:58
Definition: malloc.c:26

References bin_pe_init_clr(), bin_pe_init_exports(), bin_pe_init_hdr(), bin_pe_init_imports(), bin_pe_init_overlay(), bin_pe_init_resource(), bin_pe_init_rich_info(), bin_pe_init_sections(), bin_pe_init_security(), bin_pe_init_tls(), bin_pe_parse_resource(), NULL, PE_, rz_bin_pe_get_sections(), rz_bin_pe_is_big_endian(), and RZ_LOG_ERROR.

Referenced by rz_bin_pe_new_buf().

◆ bin_pe_rva_to_paddr()

PE_DWord PE_() bin_pe_rva_to_paddr ( RzBinPEObj bin,
PE_DWord  rva 
)

Definition at line 15 of file pe.c.

15  {
16  PE_DWord section_base;
17  int i, section_size;
18  for (i = 0; i < bin->num_sections; i++) {
19  section_base = bin->sections[i].vaddr;
20  section_size = bin->sections[i].vsize;
21  if (rva >= section_base && rva < section_base + section_size) {
22  return bin->sections[i].paddr + (rva - section_base);
23  }
24  }
25  return rva;
26 }
lzma_index ** i
Definition: index.h:629
static ut64 rva(RzBinObject *o, ut64 paddr, ut64 vaddr, int va)
Definition: cbin.c:77
#define PE_DWord
Definition: pe_specs.h:27

References i, PE_DWord, and rva().

Referenced by _parse_resource_directory(), bin_pe_get_overlay(), bin_pe_init_clr(), bin_pe_init_exports(), bin_pe_init_imports(), bin_pe_init_resource(), bin_pe_init_tls(), bin_pe_parse_imports(), bin_pe_store_tls_callbacks(), rz_bin_pe_get_clr_methoddef_offset(), rz_bin_pe_get_clr_symbols(), rz_bin_pe_get_debug_data(), rz_bin_pe_get_entrypoint(), rz_bin_pe_get_exports(), rz_bin_pe_get_imports(), and rz_bin_pe_get_libs().

◆ bin_pe_rva_to_va()

PE_DWord PE_() bin_pe_rva_to_va ( RzBinPEObj bin,
PE_DWord  rva 
)

◆ bin_pe_va_to_rva()

PE_DWord PE_() bin_pe_va_to_rva ( RzBinPEObj bin,
PE_DWord  va 
)

Definition at line 32 of file pe.c.

32  {
33  ut64 imageBase = PE_(rz_bin_pe_get_image_base)(bin);
34  if (va < imageBase) {
35  return va;
36  }
37  return va - imageBase;
38 }
ut64(WINAPI *w32_GetEnabledXStateFeatures)()

References PE_, rz_bin_pe_get_image_base(), and ut64().

Referenced by bin_pe_init_tls(), and bin_pe_store_tls_callbacks().

◆ rz_bin_pe_free()

void* PE_() rz_bin_pe_free ( RzBinPEObj bin)

Definition at line 88 of file pe.c.

88  {
89  if (!bin) {
90  return NULL;
91  }
92  free(bin->dos_header);
93  free(bin->nt_headers);
94  free(bin->section_header);
95  free(bin->export_directory);
96  free(bin->import_directory);
97  free(bin->resource_directory);
99  (bin->security_directory);
100  free(bin->delay_import_directory);
102  free(bin->tls_directory);
103  free(bin->sections);
104  free(bin->authentihash);
105  rz_list_free(bin->rich_entries);
106  rz_list_free(bin->resources);
107  rz_pkcs7_free_cms(bin->cms);
108  rz_pkcs7_free_spcinfo(bin->spcinfo);
109  rz_hash_free(bin->hash);
110  rz_buf_free(bin->b);
111  bin->b = NULL;
112  free(bin);
113  return NULL;
114 }
void bin_pe_dotnet_destroy_clr(Pe_image_clr *clr)
Definition: dotnet.c:968
RZ_API void rz_hash_free(RzHash *rh)
Definition: hash.c:597
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
RZ_API void rz_list_free(RZ_NONNULL RzList *list)
Empties the list and frees the list pointer.
Definition: list.c:137
void PE_() free_security_directory(Pe_image_security_directory *security_directory)
Definition: pe_security.c:156
RZ_API void rz_buf_free(RzBuffer *b)
Free all internal data hold by the buffer and the buffer.
Definition: buf.c:1253
RZ_API void rz_pkcs7_free_cms(RCMS *container)
Definition: pkcs7.c:341
RZ_API void rz_pkcs7_free_spcinfo(SpcIndirectDataContent *spcinfo)
Definition: pkcs7.c:729

References bin_pe_dotnet_destroy_clr(), free(), free_security_directory(), NULL, PE_, rz_buf_free(), rz_hash_free(), rz_list_free(), rz_pkcs7_free_cms(), and rz_pkcs7_free_spcinfo().

Referenced by rz_bin_pe_new_buf(), rz_bin_pemixed_free(), rz_bin_pemixed_from_bytes_new(), and rz_bin_pemixed_init_dos().

◆ rz_bin_pe_new_buf()

RzBinPEObj* PE_() rz_bin_pe_new_buf ( RzBuffer buf,
bool  verbose 
)

Definition at line 116 of file pe.c.

116  {
118  if (!bin) {
119  return NULL;
120  }
121  bin->kv = sdb_new0();
122  bin->b = rz_buf_ref(buf);
123  bin->verbose = verbose;
124  bin->size = rz_buf_size(buf);
125  bin->hash = rz_hash_new();
126  if (!bin_pe_init(bin)) {
127  return PE_(rz_bin_pe_free)(bin);
128  }
129  return bin;
130 }
RZ_API RzHash * rz_hash_new(void)
Definition: hash.c:585
voidpf void * buf
Definition: ioapi.h:138
static int bin_pe_init(RzBinPEObj *bin)
Definition: pe.c:40
void *PE_() rz_bin_pe_free(RzBinPEObj *bin)
Definition: pe.c:88
#define RzBinPEObj
Definition: pe.h:126
RZ_API RzBuffer * rz_buf_ref(RzBuffer *b)
Increment the reference count of the buffer.
Definition: buf.c:668
RZ_API ut64 rz_buf_size(RZ_NONNULL RzBuffer *b)
Return the size of the buffer.
Definition: buf.c:1225
#define RZ_NEW0(x)
Definition: rz_types.h:284
RZ_API Sdb * sdb_new0(void)
Definition: sdb.c:43
static int verbose
Definition: z80asm.c:73

References bin_pe_init(), NULL, PE_, rz_bin_pe_free(), rz_buf_ref(), rz_buf_size(), rz_hash_new(), RZ_NEW0, RzBinPEObj, sdb_new0(), and verbose.

Referenced by rz_bin_pemixed_from_bytes_new().