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

Go to the source code of this file.

Macros

#define VFILE_NAME_RELOC_TARGETS   "reloc-targets"
 
#define VFILE_NAME_PATCHED   "patched"
 
#define DTYPE_IS_FUNCTION(type)   (COFF_SYM_GET_DTYPE(type) == COFF_SYM_DTYPE_FUNCTION)
 

Functions

static void populate_symbols (RzBinFile *bf)
 
static Sdbget_sdb (RzBinFile *bf)
 
static bool rz_coff_is_stripped (struct rz_bin_coff_obj *obj)
 
static bool load_buffer (RzBinFile *bf, RzBinObject *obj, RzBuffer *buf, Sdb *sdb)
 
static void destroy (RzBinFile *bf)
 
static ut64 baddr (RzBinFile *bf)
 
static RzBinAddrbinsym (RzBinFile *bf, RzBinSpecialSymbol sym)
 
static bool is_imported_symbol (struct coff_symbol *s)
 
static bool _fill_bin_symbol (RzBin *rbin, struct rz_bin_coff_obj *bin, int idx, RzBinSymbol **sym)
 
static RzBinImport_fill_bin_import (struct rz_bin_coff_obj *bin, int idx)
 
static RzListentries (RzBinFile *bf)
 
static RzListvirtual_files (RzBinFile *bf)
 
static RzListmaps (RzBinFile *bf)
 
static RzListsections (RzBinFile *bf)
 
static void populate_imports (struct rz_bin_coff_obj *obj)
 
static RzListsymbols (RzBinFile *bf)
 
static RzListimports (RzBinFile *bf)
 
static RzListlibs (RzBinFile *bf)
 
static RzListrelocs (RzBinFile *bf)
 
static RzBinInfoinfo (RzBinFile *bf)
 
static RzListfields (RzBinFile *bf)
 
static ut64 size (RzBinFile *bf)
 
static bool check_buffer (RzBuffer *buf)
 

Variables

RzBinPlugin rz_bin_plugin_coff
 
RZ_API RzLibStruct rizin_plugin
 

Macro Definition Documentation

◆ DTYPE_IS_FUNCTION

#define DTYPE_IS_FUNCTION (   type)    (COFF_SYM_GET_DTYPE(type) == COFF_SYM_DTYPE_FUNCTION)

Definition at line 51 of file bin_coff.c.

◆ VFILE_NAME_PATCHED

#define VFILE_NAME_PATCHED   "patched"

Definition at line 14 of file bin_coff.c.

◆ VFILE_NAME_RELOC_TARGETS

#define VFILE_NAME_RELOC_TARGETS   "reloc-targets"

Definition at line 13 of file bin_coff.c.

Function Documentation

◆ _fill_bin_import()

static RzBinImport* _fill_bin_import ( struct rz_bin_coff_obj bin,
int  idx 
)
static

Definition at line 146 of file bin_coff.c.

146  {
148  if (!ptr || idx < 0 || idx > bin->hdr.f_nsyms) {
149  free(ptr);
150  return NULL;
151  }
152  struct coff_symbol *s = &bin->symbols[idx];
153  if (!is_imported_symbol(s)) {
154  free(ptr);
155  return NULL;
156  }
157  char *coffname = rz_coff_symbol_name(bin, s);
158  if (!coffname) {
159  free(ptr);
160  return NULL;
161  }
162  ptr->name = coffname;
163  ptr->bind = "NONE";
164  ptr->type = DTYPE_IS_FUNCTION(s->n_type)
167  return ptr;
168 }
static bool is_imported_symbol(struct coff_symbol *s)
Definition: bin_coff.c:53
#define DTYPE_IS_FUNCTION(type)
Definition: bin_coff.c:51
RZ_API char * rz_coff_symbol_name(struct rz_bin_coff_obj *obj, void *ptr)
Definition: coff.c:50
#define NULL
Definition: cris-opc.c:27
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
int idx
Definition: setup.py:197
static RzSocket * s
Definition: rtr.c:28
#define RZ_BIN_TYPE_FUNC_STR
Definition: rz_bin.h:119
#define RZ_BIN_TYPE_UNKNOWN_STR
Definition: rz_bin.h:134
#define RZ_NEW0(x)
Definition: rz_types.h:284
Definition: malloc.c:26
const char * type
Definition: rz_bin.h:704
const char * bind
Definition: rz_bin.h:703
char * name
Definition: rz_bin.h:701

References rz_bin_import_t::bind, DTYPE_IS_FUNCTION, free(), setup::idx, is_imported_symbol(), rz_bin_import_t::name, NULL, RZ_BIN_TYPE_FUNC_STR, RZ_BIN_TYPE_UNKNOWN_STR, rz_coff_symbol_name(), RZ_NEW0, s, and rz_bin_import_t::type.

Referenced by populate_imports().

◆ _fill_bin_symbol()

static bool _fill_bin_symbol ( RzBin rbin,
struct rz_bin_coff_obj bin,
int  idx,
RzBinSymbol **  sym 
)
static

Definition at line 57 of file bin_coff.c.

57  {
58  RzBinSymbol *ptr = *sym;
59  struct coff_symbol *s = NULL;
60  struct coff_scn_hdr *sc_hdr = NULL;
61  if (idx < 0 || idx > bin->hdr.f_nsyms) {
62  return false;
63  }
64  if (!bin->symbols) {
65  return false;
66  }
67  s = &bin->symbols[idx];
68  char *coffname = rz_coff_symbol_name(bin, s);
69  if (!coffname) {
70  return false;
71  }
72  ptr->size = 4;
73  ptr->ordinal = 0;
74  ptr->name = coffname;
75  ptr->forwarder = "NONE";
78  ptr->vaddr = UT64_MAX;
79  if (s->n_scnum < bin->hdr.f_nscns + 1 && s->n_scnum > 0) {
80  // first index is 0 that is why -1
81  sc_hdr = &bin->scn_hdrs[s->n_scnum - 1];
82  ptr->paddr = sc_hdr->s_scnptr + s->n_value;
83  if (bin->scn_va) {
84  ptr->vaddr = bin->scn_va[s->n_scnum - 1] + s->n_value;
85  }
86  }
87  if (ptr->is_imported) {
88  // if the symbol is an import and it will be assigned an artificial target,
89  // assign this target as the vaddr of the symbol.
90  bool found;
91  ut64 imp_idx = ht_uu_find(bin->imp_index, idx, &found);
92  if (found) {
93  ptr->vaddr = rz_coff_import_index_addr(bin, imp_idx);
94  }
95  }
96 
97  switch (s->n_sclass) {
100  break;
101  case COFF_SYM_CLASS_FILE:
102  ptr->type = RZ_BIN_TYPE_FILE_STR;
103  break;
106  break;
108  if (s->n_scnum == COFF_SYM_SCNUM_UNDEF) {
109  ptr->paddr = UT64_MAX;
110  ptr->bind = "NONE";
111  } else {
113  }
114  ptr->type = (DTYPE_IS_FUNCTION(s->n_type) || !strcmp(coffname, "main"))
117  break;
119  if (s->n_scnum == COFF_SYM_SCNUM_ABS) {
120  ptr->type = "ABS";
121  ptr->paddr = UT64_MAX;
122  char *newname = rz_str_newf("%s-0x%08x", coffname, s->n_value);
123  if (newname) {
124  free(ptr->name);
125  ptr->name = newname;
126  }
127  } else if (sc_hdr && !memcmp(sc_hdr->s_name, s->n_name, 8)) {
129  } else {
130  ptr->type = DTYPE_IS_FUNCTION(s->n_type)
133  }
134  break;
136  ptr->type = "LABEL";
137  ptr->size = 0;
138  break;
139  default:
140  ptr->type = rz_str_constpool_get(&rbin->constpool, sdb_fmt("%i", s->n_sclass));
141  break;
142  }
143  return true;
144 }
RZ_API ut64 rz_coff_import_index_addr(struct rz_bin_coff_obj *obj, ut64 imp_index)
Definition: coff_reloc.c:32
#define COFF_SYM_CLASS_FILE
Definition: coff_specs.h:134
#define COFF_SYM_CLASS_LABEL
Definition: coff_specs.h:118
#define COFF_SYM_CLASS_EXTERNAL
Definition: coff_specs.h:114
#define COFF_SYM_SCNUM_UNDEF
Definition: coff_specs.h:85
#define COFF_SYM_CLASS_STATIC
Definition: coff_specs.h:115
#define COFF_SYM_CLASS_FUNCTION
Definition: coff_specs.h:132
#define COFF_SYM_SCNUM_ABS
Definition: coff_specs.h:86
#define COFF_SYM_CLASS_SECTION
Definition: coff_specs.h:135
RZ_API char * sdb_fmt(const char *fmt,...)
Definition: fmt.c:26
RZ_API const KEY_TYPE bool * found
Definition: ht_inc.h:130
#define RZ_BIN_BIND_LOCAL_STR
Definition: rz_bin.h:106
#define RZ_BIN_BIND_GLOBAL_STR
Definition: rz_bin.h:107
#define RZ_BIN_TYPE_FILE_STR
Definition: rz_bin.h:125
#define RZ_BIN_TYPE_SECTION_STR
Definition: rz_bin.h:124
RZ_API char * rz_str_newf(const char *fmt,...) RZ_PRINTF_CHECK(1
RZ_API const char * rz_str_constpool_get(RzStrConstPool *pool, const char *str)
Definition: str_constpool.c:19
#define UT64_MAX
Definition: rz_types_base.h:86
const char * bind
Definition: rz_bin.h:681
bool is_imported
Definition: rz_bin.h:684
const char * type
Definition: rz_bin.h:682
char * name
Definition: rz_bin.h:675
ut32 ordinal
Definition: rz_bin.h:692
const char * forwarder
Definition: rz_bin.h:680
RzStrConstPool constpool
Definition: rz_bin.h:362
ut64(WINAPI *w32_GetEnabledXStateFeatures)()

References rz_bin_symbol_t::bind, COFF_SYM_CLASS_EXTERNAL, COFF_SYM_CLASS_FILE, COFF_SYM_CLASS_FUNCTION, COFF_SYM_CLASS_LABEL, COFF_SYM_CLASS_SECTION, COFF_SYM_CLASS_STATIC, COFF_SYM_SCNUM_ABS, COFF_SYM_SCNUM_UNDEF, rz_bin_t::constpool, DTYPE_IS_FUNCTION, rz_bin_symbol_t::forwarder, found, free(), setup::idx, rz_bin_symbol_t::is_imported, is_imported_symbol(), rz_bin_symbol_t::name, NULL, rz_bin_symbol_t::ordinal, rz_bin_symbol_t::paddr, RZ_BIN_BIND_GLOBAL_STR, RZ_BIN_BIND_LOCAL_STR, RZ_BIN_TYPE_FILE_STR, RZ_BIN_TYPE_FUNC_STR, RZ_BIN_TYPE_SECTION_STR, RZ_BIN_TYPE_UNKNOWN_STR, rz_coff_import_index_addr(), rz_coff_symbol_name(), rz_str_constpool_get(), rz_str_newf(), s, sdb_fmt(), rz_bin_symbol_t::size, rz_bin_symbol_t::type, ut64(), UT64_MAX, and rz_bin_symbol_t::vaddr.

Referenced by populate_symbols().

◆ baddr()

static ut64 baddr ( RzBinFile bf)
static

Definition at line 43 of file bin_coff.c.

43  {
44  return 0;
45 }

◆ binsym()

static RzBinAddr* binsym ( RzBinFile bf,
RzBinSpecialSymbol  sym 
)
static

Definition at line 47 of file bin_coff.c.

47  {
48  return NULL;
49 }

References NULL.

◆ check_buffer()

static bool check_buffer ( RzBuffer buf)
static

Definition at line 505 of file bin_coff.c.

505  {
506 #if 0
507 TODO: do more checks here to avoid false positives
508 
509 ut16 MACHINE
510 ut16 NSECTIONS
511 ut32 DATE
512 ut32 PTRTOSYMTABLE
513 ut32 NUMOFSYMS
514 ut16 OPTHDRSIZE
515 ut16 CHARACTERISTICS
516 #endif
517 
518  ut8 tmp[20];
519  int r = rz_buf_read_at(buf, 0, tmp, sizeof(tmp));
520  return r >= 20 && rz_coff_supported_arch(tmp);
521 }
RZ_API bool rz_coff_supported_arch(const ut8 *buf)
Definition: coff.c:9
#define r
Definition: crypto_rc6.c:12
uint16_t ut16
uint32_t ut32
voidpf void * buf
Definition: ioapi.h:138
uint8_t ut8
Definition: lh5801.h:11
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
static struct sockaddr static addrlen static backlog const void static flags void struct sockaddr socklen_t static fromlen const void const struct sockaddr to
Definition: sfsocketcall.h:125
uint32_t checks
Definition: list.c:109

References checks, r, rz_buf_read_at(), rz_coff_supported_arch(), autogen_x86imm::tmp, to, and objdump-m68k::TODO.

◆ destroy()

static void destroy ( RzBinFile bf)
static

Definition at line 39 of file bin_coff.c.

39  {
40  rz_bin_coff_free((struct rz_bin_coff_obj *)bf->o->bin_obj);
41 }
RZ_API void rz_bin_coff_free(struct rz_bin_coff_obj *obj)
Definition: coff.c:266
RzBinObject * o
Definition: rz_bin.h:305
void * bin_obj
Definition: rz_bin.h:293

References rz_bin_object_t::bin_obj, rz_bin_file_t::o, and rz_bin_coff_free().

◆ entries()

static RzList* entries ( RzBinFile bf)
static

Definition at line 170 of file bin_coff.c.

170  {
171  struct rz_bin_coff_obj *obj = (struct rz_bin_coff_obj *)bf->o->bin_obj;
172  RzList *ret;
173  if (!(ret = rz_list_newf(free))) {
174  return NULL;
175  }
176  RzBinAddr *ptr = rz_coff_get_entry(obj);
177  if (ptr) {
178  rz_list_append(ret, ptr);
179  }
180  return ret;
181 }
RZ_API RzBinAddr * rz_coff_get_entry(struct rz_bin_coff_obj *obj)
Definition: coff.c:89
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
RZ_API RZ_BORROW RzListIter * rz_list_append(RZ_NONNULL RzList *list, void *data)
Appends at the end of the list a new element.
Definition: list.c:288
if(dbg->bits==RZ_SYS_BITS_64)
Definition: windows-arm64.h:4

References rz_bin_object_t::bin_obj, free(), if(), NULL, rz_bin_file_t::o, rz_coff_get_entry(), rz_list_append(), and rz_list_newf().

◆ fields()

static RzList* fields ( RzBinFile bf)
static

Definition at line 497 of file bin_coff.c.

497  {
498  return NULL;
499 }

References NULL.

◆ get_sdb()

static Sdb* get_sdb ( RzBinFile bf)
static

Definition at line 18 of file bin_coff.c.

18  {
19  RzBinObject *o = bf->o;
20  if (!o) {
21  return NULL;
22  }
23  struct rz_bin_coff_obj *bin = (struct rz_bin_coff_obj *)o->bin_obj;
24  if (bin->kv) {
25  return bin->kv;
26  }
27  return NULL;
28 }

References rz_bin_object_t::bin_obj, if(), NULL, and rz_bin_file_t::o.

◆ imports()

static RzList* imports ( RzBinFile bf)
static

Definition at line 359 of file bin_coff.c.

359  {
360  int i;
361  struct rz_bin_coff_obj *obj = (struct rz_bin_coff_obj *)bf->o->bin_obj;
363  if (!ret) {
364  return NULL;
365  }
366  if (obj->symbols) {
367  populate_imports(obj);
368  for (i = 0; i < obj->hdr.f_nsyms; i++) {
369  RzBinImport *ptr = ht_up_find(obj->imp_ht, i, NULL);
370  if (ptr) {
371  rz_list_append(ret, ptr);
372  }
373  i += obj->symbols[i].n_numaux;
374  }
375  }
376  return ret;
377 }
lzma_index ** i
Definition: index.h:629
RZ_API void rz_bin_import_free(RzBinImport *imp)
Definition: bin.c:137
static void populate_imports(struct rz_bin_coff_obj *obj)
Definition: bin_coff.c:303
void(* RzListFree)(void *ptr)
Definition: rz_list.h:11
HtUP * imp_ht
Definition: coff.h:33
struct coff_symbol * symbols
Definition: coff.h:23
struct coff_hdr hdr
Definition: coff.h:20

References rz_bin_object_t::bin_obj, rz_bin_coff_obj::hdr, i, rz_bin_coff_obj::imp_ht, NULL, rz_bin_file_t::o, populate_imports(), rz_bin_import_free(), rz_list_append(), rz_list_newf(), and rz_bin_coff_obj::symbols.

◆ info()

static RzBinInfo* info ( RzBinFile bf)
static

Definition at line 388 of file bin_coff.c.

388  {
389  RzBinInfo *ret = RZ_NEW0(RzBinInfo);
390  struct rz_bin_coff_obj *obj = (struct rz_bin_coff_obj *)bf->o->bin_obj;
391 
392  ret->file = bf->file ? strdup(bf->file) : NULL;
393  ret->rclass = strdup("coff");
394  ret->bclass = strdup("coff");
395  ret->type = strdup("COFF (Executable file)");
396  ret->os = strdup("any");
397  ret->subsystem = strdup("any");
398  ret->big_endian = obj->endian;
399  ret->has_va = true;
400  ret->dbg_info = 0;
401 
402  if (rz_coff_is_stripped(obj)) {
404  } else {
405  if (!(obj->hdr.f_flags & COFF_FLAGS_TI_F_RELFLG)) {
406  ret->dbg_info |= RZ_BIN_DBG_RELOCS;
407  }
408  if (!(obj->hdr.f_flags & COFF_FLAGS_TI_F_LNNO)) {
410  }
411  if (!(obj->hdr.f_flags & COFF_FLAGS_TI_F_EXEC)) {
412  ret->dbg_info |= RZ_BIN_DBG_SYMS;
413  }
414  }
415 
416  switch (obj->hdr.f_magic) {
421  ret->machine = strdup("mips");
422  ret->arch = strdup("mips");
423  ret->bits = 32;
424  break;
426  ret->machine = strdup("i386");
427  ret->arch = strdup("x86");
428  ret->bits = 32;
429  break;
431  ret->machine = strdup("AMD64");
432  ret->arch = strdup("x86");
433  ret->bits = 64;
434  break;
436  ret->machine = strdup("H8300");
437  ret->arch = strdup("h8300");
438  ret->bits = 16;
439  break;
442  ret->cpu = strdup("29000");
443  ret->machine = strdup("amd29k");
444  ret->arch = strdup("amd29k");
445  ret->bits = 32;
446  break;
448  ret->machine = strdup("arm");
449  ret->arch = strdup("arm");
450  ret->bits = 16;
451  break;
454  ret->machine = strdup("arm");
455  ret->arch = strdup("arm");
456  ret->bits = 32;
457  break;
459  ret->machine = strdup("arm");
460  ret->arch = strdup("arm");
461  ret->bits = 64;
462  break;
467  ret->machine = strdup("sh");
468  ret->arch = strdup("sh");
469  ret->bits = 32;
470  break;
471  case COFF_FILE_TI_COFF:
472  switch (obj->target_id) {
474  ret->machine = strdup("c54x");
475  ret->arch = strdup("tms320");
476  ret->bits = 32;
477  break;
479  ret->machine = strdup("c55x");
480  ret->arch = strdup("tms320");
481  ret->bits = 32;
482  break;
484  ret->machine = strdup("c55x+");
485  ret->arch = strdup("tms320");
486  ret->bits = 32;
487  break;
488  }
489  break;
490  default:
491  ret->machine = strdup("unknown");
492  }
493 
494  return ret;
495 }
static bool rz_coff_is_stripped(struct rz_bin_coff_obj *obj)
Definition: bin_coff.c:30
#define COFF_FILE_MACHINE_TMS320C55PLUS
Definition: coff_specs.h:41
#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_MACHINE_TMS320C54
Definition: coff_specs.h:36
#define COFF_FILE_TI_COFF
Definition: coff_specs.h:34
#define COFF_FLAGS_TI_F_EXEC
Definition: coff_specs.h:44
#define COFF_FLAGS_TI_F_LNNO
Definition: coff_specs.h:45
#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_FLAGS_TI_F_RELFLG
Definition: coff_specs.h:43
#define COFF_FILE_MACHINE_MIPSFPU16
Definition: coff_specs.h:20
#define COFF_FILE_MACHINE_I386
Definition: coff_specs.h:15
#define COFF_FILE_MACHINE_TMS320C55
Definition: coff_specs.h:38
#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
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")
#define RZ_BIN_DBG_RELOCS
Definition: rz_bin.h:31
#define RZ_BIN_DBG_STRIPPED
Definition: rz_bin.h:27
#define RZ_BIN_DBG_SYMS
Definition: rz_bin.h:30
#define RZ_BIN_DBG_LINENUMS
Definition: rz_bin.h:29
ut8 endian
Definition: coff.h:29
ut16 target_id
Definition: coff.h:25
char * file
Definition: rz_bin.h:299
int has_va
Definition: rz_bin.h:228
char * type
Definition: rz_bin.h:211
char * os
Definition: rz_bin.h:219
char * subsystem
Definition: rz_bin.h:220
char * machine
Definition: rz_bin.h:216
char * bclass
Definition: rz_bin.h:212
char * file
Definition: rz_bin.h:210
ut64 dbg_info
Definition: rz_bin.h:240
char * cpu
Definition: rz_bin.h:215
char * rclass
Definition: rz_bin.h:213
char * arch
Definition: rz_bin.h:214
int big_endian
Definition: rz_bin.h:235

References rz_bin_info_t::arch, rz_bin_info_t::bclass, rz_bin_info_t::big_endian, rz_bin_object_t::bin_obj, rz_bin_info_t::bits, 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_MACHINE_TMS320C54, COFF_FILE_MACHINE_TMS320C55, COFF_FILE_MACHINE_TMS320C55PLUS, COFF_FILE_TI_COFF, COFF_FLAGS_TI_F_EXEC, COFF_FLAGS_TI_F_LNNO, COFF_FLAGS_TI_F_RELFLG, rz_bin_info_t::cpu, rz_bin_info_t::dbg_info, rz_bin_coff_obj::endian, rz_bin_info_t::file, rz_bin_file_t::file, rz_bin_info_t::has_va, rz_bin_coff_obj::hdr, rz_bin_info_t::machine, NULL, rz_bin_file_t::o, rz_bin_info_t::os, rz_bin_info_t::rclass, RZ_BIN_DBG_LINENUMS, RZ_BIN_DBG_RELOCS, RZ_BIN_DBG_STRIPPED, RZ_BIN_DBG_SYMS, rz_coff_is_stripped(), RZ_NEW0, strdup(), rz_bin_info_t::subsystem, rz_bin_coff_obj::target_id, and rz_bin_info_t::type.

◆ is_imported_symbol()

static bool is_imported_symbol ( struct coff_symbol *  s)
static

Definition at line 53 of file bin_coff.c.

53  {
54  return s->n_scnum == COFF_SYM_SCNUM_UNDEF && s->n_sclass == COFF_SYM_CLASS_EXTERNAL;
55 }

References COFF_SYM_CLASS_EXTERNAL, COFF_SYM_SCNUM_UNDEF, and s.

Referenced by _fill_bin_import(), and _fill_bin_symbol().

◆ libs()

static RzList* libs ( RzBinFile bf)
static

Definition at line 379 of file bin_coff.c.

379  {
380  return NULL;
381 }

References NULL.

Referenced by get_libs(), rz_bin_demangle(), rz_bin_pe_get_libs(), rz_core_bin_libs_print(), rz_core_bin_load(), and rz_diff_libraries_new().

◆ load_buffer()

static bool load_buffer ( RzBinFile bf,
RzBinObject obj,
RzBuffer buf,
Sdb sdb 
)
static

Definition at line 34 of file bin_coff.c.

34  {
36  return obj->bin_obj != NULL;
37 }
RZ_API struct rz_bin_coff_obj * rz_bin_coff_new_buf(RzBuffer *buf, bool verbose)
Definition: coff.c:278
struct rz_bin_t * rbin
Definition: rz_bin.h:316
bool verbose
Definition: rz_bin.h:359

References rz_bin_object_t::bin_obj, NULL, rz_bin_file_t::rbin, rz_bin_coff_new_buf(), and rz_bin_t::verbose.

◆ maps()

static RzList* maps ( RzBinFile bf)
static

Definition at line 224 of file bin_coff.c.

224  {
226  if (!ret) {
227  return NULL;
228  }
229  RzBinObject *o = bf->o;
230  struct rz_bin_coff_obj *obj = o ? o->bin_obj : NULL;
231  if (!obj || !obj->scn_hdrs) {
232  return ret;
233  }
234  populate_symbols(bf);
235  for (size_t i = 0; i < obj->hdr.f_nscns; i++) {
236  RzBinMap *ptr = RZ_NEW0(RzBinMap);
237  if (!ptr) {
238  return ret;
239  }
240  struct coff_scn_hdr *hdr = &obj->scn_hdrs[i];
241  ptr->name = rz_coff_symbol_name(obj, hdr);
242  ptr->psize = hdr->s_size;
243  ptr->vsize = hdr->s_size;
244  ptr->paddr = hdr->s_scnptr;
245  if (obj->scn_va) {
246  ptr->vaddr = obj->scn_va[i];
247  }
248  ptr->perm = rz_coff_perms_from_section_flags(hdr->s_flags);
249  if (hdr->s_nreloc) {
251  }
252  rz_list_append(ret, ptr);
253  }
255  if (rtmsz) {
256  // virtual file for reloc targets (where the relocs will point into)
258  if (!map) {
259  return ret;
260  }
261  map->name = strdup("reloc-targets");
262  map->paddr = 0;
263  map->psize = rtmsz;
265  map->vsize = rtmsz;
266  map->perm = RZ_PERM_R;
267  map->vfile_name = strdup(VFILE_NAME_RELOC_TARGETS);
268  rz_list_prepend(ret, map);
269  }
270  return ret;
271 }
RZ_API void rz_bin_map_free(RzBinMap *map)
Definition: bin.c:1023
static void populate_symbols(RzBinFile *bf)
Definition: bin_coff.c:320
#define VFILE_NAME_PATCHED
Definition: bin_coff.c:14
#define VFILE_NAME_RELOC_TARGETS
Definition: bin_coff.c:13
RZ_API ut64 rz_coff_perms_from_section_flags(ut32 flags)
Definition: coff.c:36
RZ_API ut64 rz_coff_get_reloc_targets_vfile_size(struct rz_bin_coff_obj *obj)
size of the artificial reloc target vfile
Definition: coff_reloc.c:206
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
size_t map(int syms, int left, int len)
Definition: enough.c:237
RZ_API RZ_BORROW RzListIter * rz_list_prepend(RZ_NONNULL RzList *list, void *data)
Appends at the beginning of the list a new element.
Definition: list.c:316
#define RZ_PERM_R
Definition: rz_types.h:93
ut64 * scn_va
Definition: coff.h:35
struct coff_scn_hdr * scn_hdrs
Definition: coff.h:22
Description of a single memory mapping into virtual memory from a binary.
Definition: rz_bin.h:602
ut64 vsize
size to map in the destination address space. If vsize > psize, excessive bytes are meant to be fille...
Definition: rz_bin.h:606
ut32 perm
Definition: rz_bin.h:608
ut64 paddr
address of the map inside the file
Definition: rz_bin.h:603
ut64 psize
size of the data inside the file
Definition: rz_bin.h:604
ut64 vaddr
address in the destination address space to map to
Definition: rz_bin.h:605
RZ_NULLABLE char * name
Definition: rz_bin.h:607
RZ_NULLABLE char * vfile_name
Definition: rz_bin.h:615

References rz_bin_object_t::bin_obj, rz_bin_coff_obj::hdr, i, map(), rz_bin_map_t::name, NULL, rz_bin_file_t::o, rz_bin_map_t::paddr, rz_bin_map_t::perm, populate_symbols(), rz_bin_map_t::psize, rz_bin_map_free(), rz_coff_get_reloc_targets_map_base(), rz_coff_get_reloc_targets_vfile_size(), rz_coff_perms_from_section_flags(), rz_coff_symbol_name(), rz_list_append(), rz_list_newf(), rz_list_prepend(), RZ_NEW0, RZ_PERM_R, rz_bin_coff_obj::scn_hdrs, rz_bin_coff_obj::scn_va, strdup(), ut64(), rz_bin_map_t::vaddr, rz_bin_map_t::vfile_name, VFILE_NAME_PATCHED, VFILE_NAME_RELOC_TARGETS, and rz_bin_map_t::vsize.

◆ populate_imports()

static void populate_imports ( struct rz_bin_coff_obj obj)
static

Definition at line 303 of file bin_coff.c.

303  {
304  if (obj->imp_index->count || !obj->symbols) {
305  return;
306  }
307  int ord = 0;
308  ut64 imp_idx = 0;
309  for (size_t i = 0; i < obj->hdr.f_nsyms; i++) {
310  RzBinImport *ptr = _fill_bin_import(obj, i);
311  if (ptr) {
312  ptr->ordinal = ord++;
313  ht_up_insert(obj->imp_ht, (ut64)i, ptr);
314  ht_uu_insert(obj->imp_index, (ut64)i, imp_idx++);
315  }
316  i += obj->symbols[i].n_numaux;
317  }
318 }
static RzBinImport * _fill_bin_import(struct rz_bin_coff_obj *bin, int idx)
Definition: bin_coff.c:146
HtUU * imp_index
locally-generated indices for imports, in particular for deterministically assigning reloc targets
Definition: coff.h:34
ut32 ordinal
Definition: rz_bin.h:707

References _fill_bin_import(), rz_bin_coff_obj::hdr, i, rz_bin_coff_obj::imp_ht, rz_bin_coff_obj::imp_index, rz_bin_import_t::ordinal, rz_bin_coff_obj::symbols, and ut64().

Referenced by imports(), and populate_symbols().

◆ populate_symbols()

static void populate_symbols ( RzBinFile bf)
static

Definition at line 320 of file bin_coff.c.

320  {
321  struct rz_bin_coff_obj *obj = (struct rz_bin_coff_obj *)bf->o->bin_obj;
322  if (obj->sym_ht->count || !obj->symbols) {
323  return;
324  }
325  populate_imports(obj);
326  for (size_t i = 0; i < obj->hdr.f_nsyms; i++) {
328  if (!ptr) {
329  break;
330  }
331  if (_fill_bin_symbol(bf->rbin, obj, i, &ptr)) {
332  ht_up_insert(obj->sym_ht, (ut64)i, ptr);
333  } else {
334  free(ptr);
335  }
336  i += obj->symbols[i].n_numaux;
337  }
338 }
static bool _fill_bin_symbol(RzBin *rbin, struct rz_bin_coff_obj *bin, int idx, RzBinSymbol **sym)
Definition: bin_coff.c:57
HtUP * sym_ht
Definition: coff.h:32

References _fill_bin_symbol(), rz_bin_object_t::bin_obj, free(), rz_bin_coff_obj::hdr, i, if(), rz_bin_file_t::o, populate_imports(), rz_bin_file_t::rbin, RZ_NEW0, rz_bin_coff_obj::sym_ht, rz_bin_coff_obj::symbols, and ut64().

Referenced by maps(), relocs(), symbols(), and virtual_files().

◆ relocs()

static RzList* relocs ( RzBinFile bf)
static

Definition at line 383 of file bin_coff.c.

383  {
384  populate_symbols(bf);
385  return rz_coff_get_relocs(bf->o->bin_obj);
386 }
RZ_API RzList * rz_coff_get_relocs(struct rz_bin_coff_obj *bin)
Definition: coff_reloc.c:195

References rz_bin_object_t::bin_obj, rz_bin_file_t::o, populate_symbols(), and rz_coff_get_relocs().

◆ rz_coff_is_stripped()

static bool rz_coff_is_stripped ( struct rz_bin_coff_obj obj)
static

Definition at line 30 of file bin_coff.c.

30  {
32 }
#define COFF_FLAGS_TI_F_LSYMS
Definition: coff_specs.h:46

References COFF_FLAGS_TI_F_LNNO, COFF_FLAGS_TI_F_LSYMS, COFF_FLAGS_TI_F_RELFLG, and rz_bin_coff_obj::hdr.

Referenced by info().

◆ sections()

static RzList* sections ( RzBinFile bf)
static

Definition at line 273 of file bin_coff.c.

273  {
274  struct rz_bin_coff_obj *obj = (struct rz_bin_coff_obj *)bf->o->bin_obj;
276  if (!ret) {
277  return NULL;
278  }
279  if (!obj || !obj->scn_hdrs) {
280  return ret;
281  }
282  for (size_t i = 0; i < obj->hdr.f_nscns; i++) {
284  if (!ptr) {
285  return ret;
286  }
287  ptr->name = rz_coff_symbol_name(obj, &obj->scn_hdrs[i]);
288  if (strstr(ptr->name, "data")) {
289  ptr->is_data = true;
290  }
291  ptr->size = obj->scn_hdrs[i].s_size;
292  ptr->vsize = obj->scn_hdrs[i].s_size;
293  ptr->paddr = obj->scn_hdrs[i].s_scnptr;
294  if (obj->scn_va) {
295  ptr->vaddr = obj->scn_va[i];
296  }
297  ptr->perm = rz_coff_perms_from_section_flags(obj->scn_hdrs[i].s_flags);
298  rz_list_append(ret, ptr);
299  }
300  return ret;
301 }
RZ_API void rz_bin_section_free(RzBinSection *bs)
Definition: bin.c:1116
char * name
Definition: rz_bin.h:619

References rz_bin_object_t::bin_obj, rz_bin_coff_obj::hdr, i, rz_bin_section_t::is_data, rz_bin_section_t::name, NULL, rz_bin_file_t::o, rz_bin_section_t::paddr, rz_bin_section_t::perm, rz_bin_section_free(), rz_coff_perms_from_section_flags(), rz_coff_symbol_name(), rz_list_append(), rz_list_newf(), RZ_NEW0, rz_bin_coff_obj::scn_hdrs, rz_bin_coff_obj::scn_va, rz_bin_section_t::size, rz_bin_section_t::vaddr, and rz_bin_section_t::vsize.

◆ size()

static ut64 size ( RzBinFile bf)
static

Definition at line 501 of file bin_coff.c.

501  {
502  return 0;
503 }

◆ symbols()

static RzList* symbols ( RzBinFile bf)
static

Definition at line 340 of file bin_coff.c.

340  {
341  struct rz_bin_coff_obj *obj = (struct rz_bin_coff_obj *)bf->o->bin_obj;
343  if (!ret) {
344  return NULL;
345  }
346  if (obj->symbols) {
347  populate_symbols(bf);
348  for (size_t i = 0; i < obj->hdr.f_nsyms; i++) {
349  RzBinSymbol *ptr = ht_up_find(obj->sym_ht, i, NULL);
350  if (ptr) {
351  rz_list_append(ret, ptr);
352  }
353  i += obj->symbols[i].n_numaux;
354  }
355  }
356  return ret;
357 }
RZ_API void rz_bin_symbol_free(RzBinSymbol *sym)
Definition: bin.c:175

References rz_bin_object_t::bin_obj, rz_bin_coff_obj::hdr, i, NULL, rz_bin_file_t::o, populate_symbols(), rz_bin_symbol_free(), rz_list_append(), rz_list_newf(), rz_bin_coff_obj::sym_ht, and rz_bin_coff_obj::symbols.

◆ virtual_files()

static RzList* virtual_files ( RzBinFile bf)
static

Definition at line 183 of file bin_coff.c.

183  {
185  if (!r) {
186  return NULL;
187  }
188  RzBinObject *o = bf->o;
189  struct rz_bin_coff_obj *obj = o ? o->bin_obj : NULL;
190  if (!obj) {
191  return r;
192  }
193  populate_symbols(bf); // the patching depends on symbols to be available
194  // virtual file for reloc targets (where the relocs will point into)
196  if (rtmsz) {
197  RzBuffer *buf = rz_buf_new_empty(rtmsz);
198  if (!buf) {
199  return r;
200  }
202  if (!vf) {
203  return r;
204  }
205  vf->buf = buf;
206  vf->buf_owned = true;
208  rz_list_push(r, vf);
209  }
210  // virtual file mirroring the raw file, but with relocs patched
212  if (buf_patched) {
214  if (!vf) {
215  return r;
216  }
217  vf->buf = buf_patched;
219  rz_list_push(r, vf);
220  }
221  return r;
222 }
RZ_API void rz_bin_virtual_file_free(RzBinVirtualFile *vfile)
Definition: bin.c:1012
RZ_API RZ_BORROW RzBuffer * rz_coff_get_patched_buf(struct rz_bin_coff_obj *bin)
Definition: coff_reloc.c:219
RZ_API RZ_BORROW RzListIter * rz_list_push(RZ_NONNULL RzList *list, void *item)
Alias for rz_list_append.
Definition: list.c:60
RZ_API RZ_OWN RzBuffer * rz_buf_new_empty(ut64 len)
Creates a new empty buffer with a predefined size;.
Definition: buf.c:285
RzBuffer * buf_patched
overlay over the original file with relocs patched
Definition: coff.h:38
RZ_NONNULL RzBuffer * buf
Definition: rz_bin.h:597
bool buf_owned
whether buf is owned and freed by this RzBinVirtualFile
Definition: rz_bin.h:598
RZ_OWN RZ_NONNULL char * name
Definition: rz_bin.h:596

References rz_bin_object_t::bin_obj, rz_bin_virtual_file_t::buf, rz_bin_virtual_file_t::buf_owned, rz_bin_coff_obj::buf_patched, rz_bin_virtual_file_t::name, NULL, rz_bin_file_t::o, populate_symbols(), r, rz_bin_virtual_file_free(), rz_buf_new_empty(), rz_coff_get_patched_buf(), rz_coff_get_reloc_targets_vfile_size(), rz_list_newf(), rz_list_push(), RZ_NEW0, strdup(), ut64(), VFILE_NAME_PATCHED, and VFILE_NAME_RELOC_TARGETS.

Variable Documentation

◆ rizin_plugin

RZ_API RzLibStruct rizin_plugin
Initial value:
= {
.type = RZ_LIB_TYPE_BIN,
}
RzBinPlugin rz_bin_plugin_coff
Definition: bin_coff.c:523
@ RZ_LIB_TYPE_BIN
Definition: rz_lib.h:75
#define RZ_VERSION
Definition: rz_version.h:8
char * version
Definition: rz_bin.h:512

Definition at line 547 of file bin_coff.c.

◆ rz_bin_plugin_coff

RzBinPlugin rz_bin_plugin_coff
Initial value:
= {
.name = "coff",
.desc = "COFF format rz_bin plugin",
.license = "LGPL3",
.get_sdb = &get_sdb,
.load_buffer = &load_buffer,
.destroy = &destroy,
.check_buffer = &check_buffer,
.baddr = &baddr,
.binsym = &binsym,
.entries = &entries,
.virtual_files = &virtual_files,
.maps = &maps,
.sections = &sections,
.symbols = &symbols,
.imports = &imports,
.info = &info,
.fields = &fields,
.size = &size,
.libs = &libs,
.relocs = &relocs
}
static RzBinAddr * binsym(RzBinFile *bf, RzBinSpecialSymbol sym)
Definition: bin_coff.c:47
static Sdb * get_sdb(RzBinFile *bf)
Definition: bin_coff.c:18
static bool check_buffer(RzBuffer *buf)
Definition: bin_coff.c:505
static bool load_buffer(RzBinFile *bf, RzBinObject *obj, RzBuffer *buf, Sdb *sdb)
Definition: bin_coff.c:34
static RzList * symbols(RzBinFile *bf)
Definition: bin_coff.c:340
static ut64 size(RzBinFile *bf)
Definition: bin_coff.c:501
static RzList * libs(RzBinFile *bf)
Definition: bin_coff.c:379
static RzList * fields(RzBinFile *bf)
Definition: bin_coff.c:497
static void destroy(RzBinFile *bf)
Definition: bin_coff.c:39
static RzList * virtual_files(RzBinFile *bf)
Definition: bin_coff.c:183
static RzBinInfo * info(RzBinFile *bf)
Definition: bin_coff.c:388
static ut64 baddr(RzBinFile *bf)
Definition: bin_coff.c:43
static RzList * entries(RzBinFile *bf)
Definition: bin_coff.c:170
static RzList * maps(RzBinFile *bf)
Definition: bin_coff.c:224
static RzList * sections(RzBinFile *bf)
Definition: bin_coff.c:273
static RzList * imports(RzBinFile *bf)
Definition: bin_coff.c:359
static RzList * relocs(RzBinFile *bf)
Definition: bin_coff.c:383

Definition at line 523 of file bin_coff.c.