Rizin
unix-like reverse engineering framework and cli tools
pe.c
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: 2008-2019 nibble <nibble.ds@gmail.com>
2 // SPDX-FileCopyrightText: 2008-2019 pancake <pancake@nopcode.org>
3 // SPDX-FileCopyrightText: 2008-2019 inisider <inisider@gmail.com>
4 // SPDX-License-Identifier: LGPL-3.0-only
5 
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <string.h>
9 #include <rz_types.h>
10 #include <rz_util.h>
11 #include <rz_hash.h>
12 #include "pe.h"
13 #include <time.h>
14 
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 }
27 
30 }
31 
33  ut64 imageBase = PE_(rz_bin_pe_get_image_base)(bin);
34  if (va < imageBase) {
35  return va;
36  }
37  return va - imageBase;
38 }
39 
40 static int bin_pe_init(RzBinPEObj *bin) {
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 }
87 
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 }
115 
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 }
lzma_index ** i
Definition: index.h:629
static ut64 rva(RzBinObject *o, ut64 paddr, ut64 vaddr, int va)
Definition: cbin.c:77
#define NULL
Definition: cris-opc.c:27
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 RzHash * rz_hash_new(void)
Definition: hash.c:585
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
voidpf void * buf
Definition: ioapi.h:138
RZ_API void rz_list_free(RZ_NONNULL RzList *list)
Empties the list and frees the list pointer.
Definition: list.c:137
static int bin_pe_init(RzBinPEObj *bin)
Definition: pe.c:40
PE_DWord PE_() bin_pe_va_to_rva(RzBinPEObj *bin, PE_DWord va)
Definition: pe.c:32
void *PE_() rz_bin_pe_free(RzBinPEObj *bin)
Definition: pe.c:88
PE_DWord PE_() bin_pe_rva_to_paddr(RzBinPEObj *bin, PE_DWord rva)
Definition: pe.c:15
RzBinPEObj *PE_() rz_bin_pe_new_buf(RzBuffer *buf, bool verbose)
Definition: pe.c:116
PE_DWord PE_() bin_pe_rva_to_va(RzBinPEObj *bin, PE_DWord rva)
Definition: pe.c:28
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
#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
ut64 PE_() rz_bin_pe_get_image_base(RzBinPEObj *bin)
Definition: pe_info.c:588
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
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 PE_DWord
Definition: pe_specs.h:27
RZ_API RzBuffer * rz_buf_ref(RzBuffer *b)
Increment the reference count of the buffer.
Definition: buf.c:668
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 ut64 rz_buf_size(RZ_NONNULL RzBuffer *b)
Return the size of the buffer.
Definition: buf.c:1225
#define RZ_LOG_ERROR(fmtstr,...)
Definition: rz_log.h:58
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
#define RZ_NEW0(x)
Definition: rz_types.h:284
RZ_API Sdb * sdb_new0(void)
Definition: sdb.c:43
Definition: malloc.c:26
ut64(WINAPI *w32_GetEnabledXStateFeatures)()
static int verbose
Definition: z80asm.c:73