Rizin
unix-like reverse engineering framework and cli tools
coff.h File Reference
#include <rz_types.h>
#include <rz_util.h>
#include <rz_lib.h>
#include <rz_bin.h>
#include <ht_up.h>
#include <ht_uu.h>
#include "coff_specs.h"

Go to the source code of this file.

Classes

struct  rz_bin_coff_obj
 

Macros

#define COFF_IS_BIG_ENDIAN   1
 
#define COFF_IS_LITTLE_ENDIAN   0
 
#define RZ_COFF_RELOC_TARGET_SIZE   8
 

Functions

RZ_API bool rz_coff_supported_arch (const ut8 *buf)
 
RZ_API ut64 rz_coff_perms_from_section_flags (ut32 flags)
 
RZ_API struct rz_bin_coff_objrz_bin_coff_new_buf (RzBuffer *buf, bool verbose)
 
RZ_API void rz_bin_coff_free (struct rz_bin_coff_obj *obj)
 
RZ_API RzBinAddrrz_coff_get_entry (struct rz_bin_coff_obj *obj)
 
RZ_API char * rz_coff_symbol_name (struct rz_bin_coff_obj *obj, void *ptr)
 
RZ_API ut64 rz_coff_import_index_addr (struct rz_bin_coff_obj *obj, ut64 imp_index)
 
RZ_API ut64 rz_coff_get_reloc_targets_map_base (struct rz_bin_coff_obj *obj)
 base vaddr where to map the artificial reloc target vfile More...
 
RZ_API RzListrz_coff_get_relocs (struct rz_bin_coff_obj *bin)
 
RZ_API ut64 rz_coff_get_reloc_targets_vfile_size (struct rz_bin_coff_obj *obj)
 size of the artificial reloc target vfile More...
 
RZ_API RZ_BORROW RzBufferrz_coff_get_patched_buf (struct rz_bin_coff_obj *bin)
 

Macro Definition Documentation

◆ COFF_IS_BIG_ENDIAN

#define COFF_IS_BIG_ENDIAN   1

Definition at line 14 of file coff.h.

◆ COFF_IS_LITTLE_ENDIAN

#define COFF_IS_LITTLE_ENDIAN   0

Definition at line 15 of file coff.h.

◆ RZ_COFF_RELOC_TARGET_SIZE

#define RZ_COFF_RELOC_TARGET_SIZE   8

Definition at line 55 of file coff.h.

Function Documentation

◆ rz_bin_coff_free()

RZ_API void rz_bin_coff_free ( struct rz_bin_coff_obj obj)

Definition at line 266 of file coff.c.

266  {
267  ht_up_free(obj->sym_ht);
268  ht_up_free(obj->imp_ht);
269  ht_uu_free(obj->imp_index);
270  free(obj->scn_va);
271  free(obj->scn_hdrs);
272  free(obj->symbols);
273  rz_buf_free(obj->buf_patched);
274  rz_buf_free(obj->b);
275  free(obj);
276 }
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
RZ_API void rz_buf_free(RzBuffer *b)
Free all internal data hold by the buffer and the buffer.
Definition: buf.c:1253
RzBuffer * buf_patched
overlay over the original file with relocs patched
Definition: coff.h:38
ut64 * scn_va
Definition: coff.h:35
HtUP * sym_ht
Definition: coff.h:32
HtUP * imp_ht
Definition: coff.h:33
struct coff_symbol * symbols
Definition: coff.h:23
struct coff_scn_hdr * scn_hdrs
Definition: coff.h:22
HtUU * imp_index
locally-generated indices for imports, in particular for deterministically assigning reloc targets
Definition: coff.h:34
RzBuffer * b
Definition: coff.h:27

References rz_bin_coff_obj::b, rz_bin_coff_obj::buf_patched, free(), rz_bin_coff_obj::imp_ht, rz_bin_coff_obj::imp_index, rz_buf_free(), rz_bin_coff_obj::scn_hdrs, rz_bin_coff_obj::scn_va, rz_bin_coff_obj::sym_ht, and rz_bin_coff_obj::symbols.

Referenced by destroy().

◆ rz_bin_coff_new_buf()

RZ_API struct rz_bin_coff_obj* rz_bin_coff_new_buf ( RzBuffer buf,
bool  verbose 
)

Definition at line 278 of file coff.c.

278  {
279  struct rz_bin_coff_obj *bin = RZ_NEW0(struct rz_bin_coff_obj);
281  return bin;
282 }
static int rz_bin_coff_init(struct rz_bin_coff_obj *obj, RzBuffer *buf, bool verbose)
Definition: coff.c:239
voidpf void * buf
Definition: ioapi.h:138
#define RZ_NEW0(x)
Definition: rz_types.h:284
Definition: malloc.c:26
static int verbose
Definition: z80asm.c:73

References rz_bin_coff_init(), RZ_NEW0, and verbose.

Referenced by load_buffer().

◆ rz_coff_get_entry()

RZ_API RzBinAddr* rz_coff_get_entry ( struct rz_bin_coff_obj obj)

Definition at line 89 of file coff.c.

89  {
91  int i;
92  if (!addr) {
93  return NULL;
94  }
95  /* Simplest case, the header provides the entrypoint address */
96  if (obj->hdr.f_opthdr) {
97  addr->paddr = obj->opt_hdr.entry;
98  return addr;
99  }
100  /* No help from the header eh? Use the address of the symbols '_start'
101  * or 'main' if present */
102  if (obj->symbols) {
103  for (i = 0; i < obj->hdr.f_nsyms; i++) {
104  if ((!strcmp(obj->symbols[i].n_name, "_start") ||
105  !strcmp(obj->symbols[i].n_name, "start")) &&
106  rz_coff_rebase_sym(obj, addr, &obj->symbols[i])) {
107  return addr;
108  }
109  }
110  for (i = 0; i < obj->hdr.f_nsyms; i++) {
111  if ((!strcmp(obj->symbols[i].n_name, "_main") ||
112  !strcmp(obj->symbols[i].n_name, "main")) &&
113  rz_coff_rebase_sym(obj, addr, &obj->symbols[i])) {
114  return addr;
115  }
116  }
117  }
118 #if 0
119  /* Still clueless ? Let's just use the address of .text */
120  if (obj->scn_hdrs) {
121  for (i = 0; i < obj->hdr.f_nscns; i++) {
122  // avoid doing string matching and use x bit from the section
123  if (obj->scn_hdrs[i].s_flags & COFF_SCN_MEM_EXECUTE) {
124  addr->paddr = obj->scn_hdrs[i].s_scnptr;
125  return addr;
126  }
127  }
128  }
129 #else
130  free(addr);
131  return NULL;
132 #endif
133  return addr;
134 }
lzma_index ** i
Definition: index.h:629
static int rz_coff_rebase_sym(struct rz_bin_coff_obj *obj, RzBinAddr *addr, struct coff_symbol *sym)
Definition: coff.c:79
#define COFF_SCN_MEM_EXECUTE
Definition: coff_specs.h:81
#define NULL
Definition: cris-opc.c:27
struct coff_hdr hdr
Definition: coff.h:20
struct coff_opt_hdr opt_hdr
Definition: coff.h:21
static int addr
Definition: z80asm.c:58

References addr, COFF_SCN_MEM_EXECUTE, free(), rz_bin_coff_obj::hdr, i, NULL, rz_bin_coff_obj::opt_hdr, rz_coff_rebase_sym(), RZ_NEW0, rz_bin_coff_obj::scn_hdrs, and rz_bin_coff_obj::symbols.

Referenced by entries().

◆ rz_coff_get_patched_buf()

RZ_API RZ_BORROW RzBuffer* rz_coff_get_patched_buf ( struct rz_bin_coff_obj bin)

Definition at line 219 of file coff_reloc.c.

219  {
221  if (bin->buf_patched) {
222  return bin->buf_patched;
223  }
225  if (!bin->buf_patched) {
226  return NULL;
227  }
228  relocs_foreach(bin, patch_reloc_cb, bin->buf_patched);
230  return bin->buf_patched;
231 }
static void patch_reloc_cb(RZ_BORROW RzBinReloc *reloc, ut8 *patch_buf, size_t patch_buf_sz, void *user)
Definition: coff_reloc.c:212
static void relocs_foreach(struct rz_bin_coff_obj *bin, RelocsForeachCb cb, void *user)
Definition: coff_reloc.c:38
#define rz_return_val_if_fail(expr, val)
Definition: rz_assert.h:108
RZ_API void rz_buf_sparse_set_write_mode(RzBuffer *b, RzBufferSparseWriteMode mode)
Only for sparse RzBuffers.
Definition: buf_sparse.c:325
@ RZ_BUF_SPARSE_WRITE_MODE_SPARSE
all writes are performed in the sparse overlay
Definition: rz_buf.h:60
@ RZ_BUF_SPARSE_WRITE_MODE_THROUGH
all writes are performed in the underlying base buffer
Definition: rz_buf.h:61
RZ_API RZ_OWN RzBuffer * rz_buf_new_sparse_overlay(RzBuffer *b, RzBufferSparseWriteMode write_mode)
Creates a sparse buffer from a already populated buffer.
Definition: buf.c:426

References NULL, patch_reloc_cb(), relocs_foreach(), rz_buf_new_sparse_overlay(), rz_buf_sparse_set_write_mode(), RZ_BUF_SPARSE_WRITE_MODE_SPARSE, RZ_BUF_SPARSE_WRITE_MODE_THROUGH, and rz_return_val_if_fail.

Referenced by virtual_files().

◆ rz_coff_get_reloc_targets_map_base()

RZ_API ut64 rz_coff_get_reloc_targets_map_base ( struct rz_bin_coff_obj obj)

base vaddr where to map the artificial reloc target vfile

Definition at line 9 of file coff_reloc.c.

9  {
10  rz_return_val_if_fail(obj, 0);
12  return obj->reloc_targets_map_base;
13  }
14  if (!obj->scn_va) {
15  return 0;
16  }
17  ut64 max = 0;
18  for (size_t i = 0; i < obj->hdr.f_nscns; i++) {
19  struct coff_scn_hdr *hdr = &obj->scn_hdrs[i];
20  ut64 val = obj->scn_va[i] + hdr->s_size;
21  if (val > max) {
22  max = val;
23  }
24  }
25  max += 8;
29  return obj->reloc_targets_map_base;
30 }
ut16 val
Definition: armass64_const.h:6
#define RZ_COFF_RELOC_TARGET_SIZE
Definition: coff.h:55
int max
Definition: enough.c:225
static ut64 rz_num_align_delta(ut64 v, ut64 alignment)
Padding to align v to the next alignment-boundary.
Definition: rz_num.h:116
ut64 reloc_targets_map_base
Definition: coff.h:36
bool reloc_targets_map_base_calculated
Definition: coff.h:37
ut64(WINAPI *w32_GetEnabledXStateFeatures)()

References rz_bin_coff_obj::hdr, i, max, rz_bin_coff_obj::reloc_targets_map_base, rz_bin_coff_obj::reloc_targets_map_base_calculated, RZ_COFF_RELOC_TARGET_SIZE, rz_num_align_delta(), rz_return_val_if_fail, rz_bin_coff_obj::scn_hdrs, rz_bin_coff_obj::scn_va, ut64(), and val.

Referenced by maps(), and rz_coff_import_index_addr().

◆ rz_coff_get_reloc_targets_vfile_size()

RZ_API ut64 rz_coff_get_reloc_targets_vfile_size ( struct rz_bin_coff_obj obj)

size of the artificial reloc target vfile

Definition at line 206 of file coff_reloc.c.

206  {
207  rz_return_val_if_fail(obj, 0);
208  ut64 count = obj->imp_index ? obj->imp_index->count : 0;
210 }
static static sync static getppid static getegid const char static filename char static len const char char static bufsiz static mask static vfork const void static prot static getpgrp const char static swapflags static arg static fd static protocol static who struct sockaddr static addrlen static backlog struct timeval struct timezone static tz const struct iovec static count static mode const void const struct sockaddr static tolen const char static pathname void count
Definition: sflib.h:98

References count, rz_bin_coff_obj::imp_index, RZ_COFF_RELOC_TARGET_SIZE, rz_return_val_if_fail, and ut64().

Referenced by maps(), and virtual_files().

◆ rz_coff_get_relocs()

RZ_API RzList* rz_coff_get_relocs ( struct rz_bin_coff_obj bin)

Definition at line 195 of file coff_reloc.c.

195  {
198  if (!r) {
199  return NULL;
200  }
202  return r;
203 }
void get_relocs_list_cb(RZ_BORROW RzBinReloc *reloc, ut8 *patch_buf, size_t patch_buf_sz, void *user)
Definition: coff_reloc.c:185
#define r
Definition: crypto_rc6.c:12
RZ_API RZ_OWN RzList * rz_list_newf(RzListFree f)
Returns a new initialized RzList pointer and sets the free method.
Definition: list.c:248

References free(), get_relocs_list_cb(), NULL, r, relocs_foreach(), rz_list_newf(), and rz_return_val_if_fail.

Referenced by relocs().

◆ rz_coff_import_index_addr()

RZ_API ut64 rz_coff_import_index_addr ( struct rz_bin_coff_obj obj,
ut64  imp_index 
)

Definition at line 32 of file coff_reloc.c.

32  {
34 }
RZ_API ut64 rz_coff_get_reloc_targets_map_base(struct rz_bin_coff_obj *obj)
base vaddr where to map the artificial reloc target vfile
Definition: coff_reloc.c:9

References rz_coff_get_reloc_targets_map_base(), and RZ_COFF_RELOC_TARGET_SIZE.

Referenced by _fill_bin_symbol(), and relocs_foreach().

◆ rz_coff_perms_from_section_flags()

RZ_API ut64 rz_coff_perms_from_section_flags ( ut32  flags)

Definition at line 36 of file coff.c.

36  {
37  ut32 r = 0;
38  if (flags & COFF_SCN_MEM_READ) {
39  r |= RZ_PERM_R;
40  }
41  if (flags & COFF_SCN_MEM_WRITE) {
42  r |= RZ_PERM_W;
43  }
45  r |= RZ_PERM_X;
46  }
47  return r;
48 }
#define COFF_SCN_MEM_READ
Definition: coff_specs.h:82
#define COFF_SCN_MEM_WRITE
Definition: coff_specs.h:83
uint32_t ut32
#define RZ_PERM_R
Definition: rz_types.h:93
#define RZ_PERM_W
Definition: rz_types.h:94
#define RZ_PERM_X
Definition: rz_types.h:95
static struct sockaddr static addrlen static backlog const void static flags void flags
Definition: sfsocketcall.h:123

References COFF_SCN_MEM_EXECUTE, COFF_SCN_MEM_READ, COFF_SCN_MEM_WRITE, flags, r, RZ_PERM_R, RZ_PERM_W, and RZ_PERM_X.

Referenced by maps(), and sections().

◆ rz_coff_supported_arch()

RZ_API bool rz_coff_supported_arch ( const ut8 buf)

Definition at line 9 of file coff.c.

9  {
11  switch (arch) {
18  case COFF_FILE_TI_COFF:
30  return true;
31  default:
32  return false;
33  }
34 }
#define COFF_FILE_MACHINE_SH3DSP
Definition: coff_specs.h:27
#define COFF_FILE_MACHINE_SH4
Definition: coff_specs.h:28
#define COFF_FILE_MACHINE_AMD29KBE
Definition: coff_specs.h:21
#define COFF_FILE_MACHINE_ARM64
Definition: coff_specs.h:13
#define COFF_FILE_MACHINE_AMD64
Definition: coff_specs.h:10
#define COFF_FILE_MACHINE_ARM
Definition: coff_specs.h:11
#define COFF_FILE_TI_COFF
Definition: coff_specs.h:34
#define COFF_FILE_MACHINE_MIPSFPU
Definition: coff_specs.h:19
#define COFF_FILE_MACHINE_SH3
Definition: coff_specs.h:26
#define COFF_FILE_MACHINE_MIPS16
Definition: coff_specs.h:18
#define COFF_FILE_MACHINE_MIPSFPU16
Definition: coff_specs.h:20
#define COFF_FILE_MACHINE_I386
Definition: coff_specs.h:15
#define COFF_FILE_MACHINE_SH5
Definition: coff_specs.h:29
#define COFF_FILE_MACHINE_AMD29KLE
Definition: coff_specs.h:22
#define COFF_FILE_MACHINE_THUMB
Definition: coff_specs.h:30
#define COFF_FILE_MACHINE_R4000
Definition: coff_specs.h:25
#define COFF_FILE_MACHINE_H8300
Definition: coff_specs.h:32
#define COFF_FILE_MACHINE_ARMNT
Definition: coff_specs.h:12
cs_arch arch
Definition: cstool.c:13
uint16_t ut16
static ut16 rz_read_le16(const void *src)
Definition: rz_endian.h:206

References arch, COFF_FILE_MACHINE_AMD29KBE, COFF_FILE_MACHINE_AMD29KLE, COFF_FILE_MACHINE_AMD64, COFF_FILE_MACHINE_ARM, COFF_FILE_MACHINE_ARM64, COFF_FILE_MACHINE_ARMNT, COFF_FILE_MACHINE_H8300, COFF_FILE_MACHINE_I386, COFF_FILE_MACHINE_MIPS16, COFF_FILE_MACHINE_MIPSFPU, COFF_FILE_MACHINE_MIPSFPU16, COFF_FILE_MACHINE_R4000, COFF_FILE_MACHINE_SH3, COFF_FILE_MACHINE_SH3DSP, COFF_FILE_MACHINE_SH4, COFF_FILE_MACHINE_SH5, COFF_FILE_MACHINE_THUMB, COFF_FILE_TI_COFF, and rz_read_le16().

Referenced by check_buffer().

◆ rz_coff_symbol_name()

RZ_API char* rz_coff_symbol_name ( struct rz_bin_coff_obj obj,
void *  ptr 
)

Definition at line 50 of file coff.c.

50  {
51  char n[256] = { 0 };
52  int len = 0, offset = 0;
53  union {
54  char name[8];
55  struct {
56  ut32 zero;
57  ut32 offset;
58  };
59  } *p = ptr;
60  if (!ptr) {
61  return strdup("");
62  }
63  if (p->zero) {
64  return rz_str_ndup(p->name, 8);
65  }
66  offset = obj->hdr.f_symptr + obj->hdr.f_nsyms * sizeof(struct coff_symbol) + p->offset;
67  if (offset > obj->size) {
68  return strdup("");
69  }
70  len = rz_buf_read_at(obj->b, offset, (ut8 *)n, sizeof(n));
71  if (len < 1) {
72  return strdup("");
73  }
74  /* ensure null terminated string */
75  n[sizeof(n) - 1] = 0;
76  return strdup(n);
77 }
size_t len
Definition: 6502dis.c:15
voidpf uLong offset
Definition: ioapi.h:144
uint8_t ut8
Definition: lh5801.h:11
void * p
Definition: libc.cpp:67
return strdup("=SP r13\n" "=LR r14\n" "=PC r15\n" "=A0 r0\n" "=A1 r1\n" "=A2 r2\n" "=A3 r3\n" "=ZF zf\n" "=SF nf\n" "=OF vf\n" "=CF cf\n" "=SN or0\n" "gpr lr .32 56 0\n" "gpr pc .32 60 0\n" "gpr cpsr .32 64 0 ____tfiae_________________qvczn\n" "gpr or0 .32 68 0\n" "gpr tf .1 64.5 0 thumb\n" "gpr ef .1 64.9 0 endian\n" "gpr jf .1 64.24 0 java\n" "gpr qf .1 64.27 0 sticky_overflow\n" "gpr vf .1 64.28 0 overflow\n" "gpr cf .1 64.29 0 carry\n" "gpr zf .1 64.30 0 zero\n" "gpr nf .1 64.31 0 negative\n" "gpr itc .4 64.10 0 if_then_count\n" "gpr gef .4 64.16 0 great_or_equal\n" "gpr r0 .32 0 0\n" "gpr r1 .32 4 0\n" "gpr r2 .32 8 0\n" "gpr r3 .32 12 0\n" "gpr r4 .32 16 0\n" "gpr r5 .32 20 0\n" "gpr r6 .32 24 0\n" "gpr r7 .32 28 0\n" "gpr r8 .32 32 0\n" "gpr r9 .32 36 0\n" "gpr r10 .32 40 0\n" "gpr r11 .32 44 0\n" "gpr r12 .32 48 0\n" "gpr r13 .32 52 0\n" "gpr r14 .32 56 0\n" "gpr r15 .32 60 0\n" "gpr r16 .32 64 0\n" "gpr r17 .32 68 0\n")
int n
Definition: mipsasm.c:19
RZ_API st64 rz_buf_read_at(RZ_NONNULL RzBuffer *b, ut64 addr, RZ_NONNULL RZ_OUT ut8 *buf, ut64 len)
Read len bytes of the buffer at the specified address.
Definition: buf.c:1136
RZ_API char * rz_str_ndup(RZ_NULLABLE const char *ptr, int len)
Create new copy of string ptr limited to size len.
Definition: str.c:1006
Definition: z80asm.h:102
size_t size
Definition: coff.h:28

References rz_bin_coff_obj::b, rz_bin_coff_obj::hdr, len, n, p, rz_buf_read_at(), rz_str_ndup(), rz_bin_coff_obj::size, and strdup().

Referenced by _fill_bin_import(), _fill_bin_symbol(), maps(), and sections().