Rizin
unix-like reverse engineering framework and cli tools
bin_omf.c
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: 2015-2019 ampotos <mercie_i@epitech.eu>
2 // SPDX-FileCopyrightText: 2015-2019 pancake <pancake@nopcode.org>
3 // SPDX-License-Identifier: LGPL-3.0-only
4 
5 #include <rz_types.h>
6 #include <rz_util.h>
7 #include <rz_lib.h>
8 #include <rz_bin.h>
9 #include "omf/omf.h"
10 
11 static bool load_buffer(RzBinFile *bf, RzBinObject *obj, RzBuffer *b, Sdb *sdb) {
12  ut64 size;
13  const ut8 *buf = rz_buf_data(b, &size);
14  rz_return_val_if_fail(buf, false);
16  return obj->bin_obj;
17 }
18 
19 static void destroy(RzBinFile *bf) {
21  bf->o->bin_obj = NULL;
22 }
23 
24 static bool check_buffer(RzBuffer *b) {
25  int i;
26  ut8 ch;
27  if (rz_buf_read_at(b, 0, &ch, 1) != 1) {
28  return false;
29  }
30  if (ch != 0x80 && ch != 0x82) {
31  return false;
32  }
33 
34  ut16 rec_size;
35  if (!rz_buf_read_le16_at(b, 1, &rec_size)) {
36  return false;
37  }
38 
39  ut8 str_size;
40  (void)rz_buf_read_at(b, 3, &str_size, 1);
42  if (str_size + 2 != rec_size || length < rec_size + 3) {
43  return false;
44  }
45  // check that the string is ASCII
46  for (i = 4; i < str_size + 4; i++) {
47  if (rz_buf_read_at(b, i, &ch, 1) != 1) {
48  break;
49  }
50  if (ch > 0x7f) {
51  return false;
52  }
53  }
54  ut64 size;
55  const ut8 *buf = rz_buf_data(b, &size);
56  if (buf == NULL) {
57  // hackaround until we make this plugin not use RBuf.data
58  ut8 buf[1024] = { 0 };
59  rz_buf_read_at(b, 0, buf, sizeof(buf));
60  return rz_bin_checksum_omf_ok(buf, sizeof(buf));
61  }
62  rz_return_val_if_fail(buf, false);
64 }
65 
66 static ut64 baddr(RzBinFile *bf) {
67  return OMF_BASE_ADDR;
68 }
69 
70 static RzList *entries(RzBinFile *bf) {
71  RzList *ret;
72  RzBinAddr *addr;
73 
74  if (!(ret = rz_list_newf(free))) {
75  return NULL;
76  }
77  if (!(addr = RZ_NEW0(RzBinAddr))) {
78  rz_list_free(ret);
79  return NULL;
80  }
81  if (!rz_bin_omf_get_entry(bf->o->bin_obj, addr)) {
82  RZ_FREE(addr);
83  } else {
84  rz_list_append(ret, addr);
85  }
86  return ret;
87 }
88 
89 static RzList *sections(RzBinFile *bf) {
90  RzList *ret;
91  ut32 ct_omf_sect = 0;
92 
93  if (!bf || !bf->o || !bf->o->bin_obj) {
94  return NULL;
95  }
96  rz_bin_omf_obj *obj = bf->o->bin_obj;
97 
98  if (!(ret = rz_list_new())) {
99  return NULL;
100  }
101 
102  while (ct_omf_sect < obj->nb_section) {
103  if (!rz_bin_omf_send_sections(ret,
104  obj->sections[ct_omf_sect++], bf->o->bin_obj)) {
105  return ret;
106  }
107  }
108  return ret;
109 }
110 
111 static RzList *symbols(RzBinFile *bf) {
112  RzList *ret;
113  RzBinSymbol *sym;
114  OMF_symbol *sym_omf;
115  int ct_sym = 0;
116  if (!bf || !bf->o || !bf->o->bin_obj) {
117  return NULL;
118  }
119  if (!(ret = rz_list_newf((RzListFree)rz_bin_symbol_free))) {
120  return NULL;
121  }
122 
123  while (ct_sym < ((rz_bin_omf_obj *)bf->o->bin_obj)->nb_symbol) {
124  if (!(sym = RZ_NEW0(RzBinSymbol))) {
125  return ret;
126  }
127  sym_omf = ((rz_bin_omf_obj *)bf->o->bin_obj)->symbols[ct_sym++];
128  sym->name = strdup(sym_omf->name);
129  sym->forwarder = "NONE";
130  sym->paddr = rz_bin_omf_get_paddr_sym(bf->o->bin_obj, sym_omf);
131  sym->vaddr = rz_bin_omf_get_vaddr_sym(bf->o->bin_obj, sym_omf);
132  sym->ordinal = ct_sym;
133  sym->size = 0;
134  rz_list_append(ret, sym);
135  }
136  return ret;
137 }
138 
139 static RzBinInfo *info(RzBinFile *bf) {
140  RzBinInfo *ret;
141 
142  if (!(ret = RZ_NEW0(RzBinInfo))) {
143  return NULL;
144  }
145  ret->file = strdup(bf->file);
146  ret->bclass = strdup("OMF");
147  ret->rclass = strdup("omf");
148  // the "E" is here to made rva return the same value for 16 bit en 32 bits files
149  ret->type = strdup("E OMF (Relocatable Object Module Format)");
150  ret->os = strdup("any");
151  ret->machine = strdup("i386");
152  ret->arch = strdup("x86");
153  ret->big_endian = false;
154  ret->has_va = true;
155  ret->bits = rz_bin_omf_get_bits(bf->o->bin_obj);
156  ret->dbg_info = 0;
157  ret->has_nx = false;
158  return ret;
159 }
160 
161 static ut64 get_vaddr(RzBinFile *bf, ut64 baddr, ut64 paddr, ut64 vaddr) {
162  return vaddr;
163 }
164 
166  .name = "omf",
167  .desc = "omf bin plugin",
168  .license = "LGPL3",
169  .load_buffer = &load_buffer,
170  .destroy = &destroy,
171  .check_buffer = &check_buffer,
172  .baddr = &baddr,
173  .entries = &entries,
175  .sections = &sections,
176  .symbols = &symbols,
177  .info = &info,
178  .get_vaddr = &get_vaddr,
179 };
180 
181 #ifndef RZ_PLUGIN_INCORE
184  .data = &rz_bin_plugin_omf,
186 };
187 #endif
lzma_index ** i
Definition: index.h:629
RZ_API void rz_bin_symbol_free(RzBinSymbol *sym)
Definition: bin.c:175
RZ_API RZ_OWN RzList * rz_bin_maps_of_file_sections(RZ_NONNULL RzBinFile *binfile)
Create a list of RzBinMap from RzBinSections queried from the given file.
Definition: bin.c:1040
static bool load_buffer(RzBinFile *bf, RzBinObject *obj, RzBuffer *b, Sdb *sdb)
Definition: bin_omf.c:11
static RzList * symbols(RzBinFile *bf)
Definition: bin_omf.c:111
static void destroy(RzBinFile *bf)
Definition: bin_omf.c:19
RZ_API RzLibStruct rizin_plugin
Definition: bin_omf.c:182
static bool check_buffer(RzBuffer *b)
Definition: bin_omf.c:24
static RzBinInfo * info(RzBinFile *bf)
Definition: bin_omf.c:139
static ut64 baddr(RzBinFile *bf)
Definition: bin_omf.c:66
RzBinPlugin rz_bin_plugin_omf
Definition: bin_omf.c:165
static RzList * entries(RzBinFile *bf)
Definition: bin_omf.c:70
static RzList * sections(RzBinFile *bf)
Definition: bin_omf.c:89
static ut64 get_vaddr(RzBinFile *bf, ut64 baddr, ut64 paddr, ut64 vaddr)
Definition: bin_omf.c:161
#define RZ_API
#define NULL
Definition: cris-opc.c:27
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 static offset struct stat static buf void long static basep static whence static length const void static len static semflg const void static shmflg const struct timespec struct timespec static rem const char static group const void length
Definition: sflib.h:133
uint16_t ut16
uint32_t ut32
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
voidpf void uLong size
Definition: ioapi.h:138
voidpf void * buf
Definition: ioapi.h:138
uint8_t ut8
Definition: lh5801.h:11
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_OWN RzList * rz_list_new(void)
Returns a new initialized RzList pointer (free method is not initialized)
Definition: list.c:235
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
RZ_API void rz_list_free(RZ_NONNULL RzList *list)
Empties the list and frees the list pointer.
Definition: list.c:137
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")
bool rz_bin_omf_get_entry(rz_bin_omf_obj *obj, RzBinAddr *addr)
Definition: omf.c:701
ut64 rz_bin_omf_get_vaddr_sym(rz_bin_omf_obj *obj, OMF_symbol *sym)
Definition: omf.c:797
int rz_bin_omf_send_sections(RzList *list, OMF_segment *section, rz_bin_omf_obj *obj)
Definition: omf.c:746
void rz_bin_free_all_omf_obj(rz_bin_omf_obj *obj)
Definition: omf.c:665
int rz_bin_omf_get_bits(rz_bin_omf_obj *obj)
Definition: omf.c:731
bool rz_bin_checksum_omf_ok(const ut8 *buf, ut64 buf_size)
Definition: omf.c:30
ut64 rz_bin_omf_get_paddr_sym(rz_bin_omf_obj *obj, OMF_symbol *sym)
Definition: omf.c:774
rz_bin_omf_obj * rz_bin_internal_omf_load(const ut8 *buf, ut64 size)
Definition: omf.c:683
#define OMF_BASE_ADDR
Definition: omf.h:61
#define rz_return_val_if_fail(expr, val)
Definition: rz_assert.h:108
#define rz_buf_read_le16_at(b, addr, result)
Definition: rz_buf.h:270
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_DEPRECATE RZ_API RZ_BORROW ut8 * rz_buf_data(RZ_NONNULL RzBuffer *b, RZ_NONNULL RZ_OUT ut64 *size)
Return a borrowed array of bytes representing the buffer data.
Definition: buf.c:1287
RZ_API ut64 rz_buf_size(RZ_NONNULL RzBuffer *b)
Return the size of the buffer.
Definition: buf.c:1225
@ RZ_LIB_TYPE_BIN
Definition: rz_lib.h:75
void(* RzListFree)(void *ptr)
Definition: rz_list.h:11
#define RZ_NEW0(x)
Definition: rz_types.h:284
#define RZ_FREE(x)
Definition: rz_types.h:369
#define RZ_VERSION
Definition: rz_version.h:8
#define b(i)
Definition: sha256.c:42
Definition: omf.h:42
char * name
Definition: omf.h:43
XX curplugin == o->plugin.
Definition: rz_bin.h:298
RzBinObject * o
Definition: rz_bin.h:305
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
int has_nx
Definition: rz_bin.h:234
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 * rclass
Definition: rz_bin.h:213
char * arch
Definition: rz_bin.h:214
int big_endian
Definition: rz_bin.h:235
void * bin_obj
Definition: rz_bin.h:293
OMF_segment ** sections
Definition: omf.h:52
char * name
Definition: rz_bin.h:509
char * version
Definition: rz_bin.h:512
char * name
Definition: rz_bin.h:675
ut32 ordinal
Definition: rz_bin.h:692
const char * forwarder
Definition: rz_bin.h:680
Definition: sdb.h:63
ut64(WINAPI *w32_GetEnabledXStateFeatures)()
static int addr
Definition: z80asm.c:58