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

Go to the source code of this file.

Macros

#define VFILE_NAME_PATCHED   "patched"
 

Functions

static bool load_buffer (RzBinFile *bf, RzBinObject *obj, RzBuffer *buf, Sdb *sdb)
 
static RzListentries (RzBinFile *bf)
 
static RzListmaps (RzBinFile *bf)
 
static RzListsections (RzBinFile *bf)
 
static RzListvirtual_files (RzBinFile *bf)
 
static void convert_relocs (RzBfltObj *bin, RzList *out, RzVector *relocs)
 
static RzListrelocs (RzBinFile *bf)
 
static RzBinInfoinfo (RzBinFile *bf)
 
static bool check_buffer (RzBuffer *buf)
 
static void destroy (RzBinFile *bf)
 
static RzListstrings (RzBinFile *bf)
 

Variables

RzBinPlugin rz_bin_plugin_bflt
 
RZ_API RzLibStruct rizin_plugin
 

Macro Definition Documentation

◆ VFILE_NAME_PATCHED

#define VFILE_NAME_PATCHED   "patched"

Definition at line 12 of file bin_bflt.c.

Function Documentation

◆ check_buffer()

static bool check_buffer ( RzBuffer buf)
static

Definition at line 241 of file bin_bflt.c.

241  {
242  ut8 tmp[4];
243  int r = rz_buf_read_at(buf, 0, tmp, sizeof(tmp));
244  return r == sizeof(tmp) && !memcmp(tmp, "bFLT", 4);
245 }
#define r
Definition: crypto_rc6.c:12
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

References r, rz_buf_read_at(), and autogen_x86imm::tmp.

◆ convert_relocs()

static void convert_relocs ( RzBfltObj bin,
RzList out,
RzVector relocs 
)
static

Definition at line 185 of file bin_bflt.c.

185  {
186  RzBfltReloc *br;
189  if (!r) {
190  return;
191  }
192  r->type = RZ_BIN_RELOC_32;
193  r->paddr = br->reloc_paddr;
194  r->vaddr = rz_bflt_paddr_to_vaddr(bin, r->paddr);
195 
196  // 0 preserved, see also patching in bflt.c
197  r->target_vaddr = br->value_orig ? rz_bflt_paddr_to_vaddr(bin, br->value_orig) : 0;
198 
199  rz_list_push(out, r);
200  }
201 }
ut64 rz_bflt_paddr_to_vaddr(RzBfltObj *bin, ut32 paddr)
Definition: bflt.c:208
static RzList * relocs(RzBinFile *bf)
Definition: bin_bflt.c:203
const lzma_allocator const uint8_t size_t uint8_t * out
Definition: block.h:528
RZ_API RZ_BORROW RzListIter * rz_list_push(RZ_NONNULL RzList *list, void *item)
Alias for rz_list_append.
Definition: list.c:60
@ RZ_BIN_RELOC_32
Definition: rz_bin.h:176
#define RZ_NEW0(x)
Definition: rz_types.h:284
#define rz_vector_foreach(vec, it)
Definition: rz_vector.h:169
#define br(opcode, mask, lose, flags)
Definition: malloc.c:26

References br, out, r, relocs(), rz_bflt_paddr_to_vaddr(), RZ_BIN_RELOC_32, rz_list_push(), RZ_NEW0, and rz_vector_foreach.

Referenced by relocs().

◆ destroy()

static void destroy ( RzBinFile bf)
static

Definition at line 247 of file bin_bflt.c.

247  {
248  rz_bflt_free(bf->o->bin_obj);
249 }
void rz_bflt_free(RzBfltObj *obj)
Definition: bflt.c:173
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_bflt_free().

◆ entries()

static RzList* entries ( RzBinFile bf)
static

Definition at line 19 of file bin_bflt.c.

19  {
20  RzBfltObj *obj = bf->o->bin_obj;
21  RzList *ret;
22  RzBinAddr *ptr;
23 
24  if (!(ret = rz_list_newf(free))) {
25  return NULL;
26  }
27  ptr = rz_bflt_get_entry(obj);
28  if (!ptr) {
29  rz_list_free(ret);
30  return NULL;
31  }
32  rz_list_append(ret, ptr);
33  return ret;
34 }
RzBinAddr * rz_bflt_get_entry(RzBfltObj *bin)
Definition: bflt.c:183
#define NULL
Definition: cris-opc.c:27
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
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
RZ_API void rz_list_free(RZ_NONNULL RzList *list)
Empties the list and frees the list pointer.
Definition: list.c:137

References rz_bin_object_t::bin_obj, free(), NULL, rz_bin_file_t::o, rz_bflt_get_entry(), rz_list_append(), rz_list_free(), and rz_list_newf().

◆ info()

static RzBinInfo* info ( RzBinFile bf)
static

Definition at line 215 of file bin_bflt.c.

215  {
216  RzBfltObj *obj = NULL;
217  RzBinInfo *info = NULL;
218  if (!bf || !bf->o || !bf->o->bin_obj) {
219  return NULL;
220  }
221  obj = (RzBfltObj *)bf->o->bin_obj;
222  if (!(info = RZ_NEW0(RzBinInfo))) {
223  return NULL;
224  }
225  info->file = bf->file ? strdup(bf->file) : NULL;
226  info->rclass = strdup("bflt");
227  info->bclass = strdup("bflt");
228  info->type = strdup("bFLT (Executable file)");
229  info->os = strdup("Linux");
230  info->subsystem = strdup("uClinux");
231  info->arch = strdup("arm"); // this is a wild guess, the format does not specify any arch, but arm is probably the most popular
232  info->big_endian = obj->big_endian;
233  info->bits = 32;
234  info->has_va = true;
235  info->dbg_info = 0;
236  info->machine = strdup("unknown");
237  info->has_pi = true;
238  return info;
239 }
static RzBinInfo * info(RzBinFile *bf)
Definition: bin_bflt.c:215
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 big_endian
Definition: bflt.h:48
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
int has_pi
Definition: rz_bin.h:229
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
if(dbg->bits==RZ_SYS_BITS_64)
Definition: windows-arm64.h:4

References rz_bin_info_t::arch, rz_bin_info_t::bclass, rz_bflt_obj_t::big_endian, rz_bin_info_t::big_endian, rz_bin_object_t::bin_obj, rz_bin_info_t::bits, rz_bin_info_t::dbg_info, rz_bin_info_t::file, rz_bin_file_t::file, rz_bin_info_t::has_pi, rz_bin_info_t::has_va, if(), rz_bin_info_t::machine, NULL, rz_bin_file_t::o, rz_bin_info_t::os, rz_bin_info_t::rclass, RZ_NEW0, strdup(), rz_bin_info_t::subsystem, and rz_bin_info_t::type.

◆ load_buffer()

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

Definition at line 14 of file bin_bflt.c.

14  {
16  return obj->bin_obj;
17 }
RzBfltObj * rz_bflt_new_buf(RzBuffer *buf, ut64 baddr, bool big_endian, bool patch_relocs)
Definition: bflt.c:164
bool patch_relocs
ask the bin plugin to fill relocs with valid contents for analysis
Definition: rz_bin.h:250
ut64 baseaddr
where the linker maps the binary in memory
Definition: rz_bin.h:248
bool big_endian
only used for binary formats that do not specify the endian in the file, but need it to load,...
Definition: rz_bin.h:251
RzBinObjectLoadOptions opts
Definition: rz_bin.h:260

References rz_bin_file_load_options_t::baseaddr, rz_bin_file_load_options_t::big_endian, rz_bin_object_t::bin_obj, rz_bin_object_t::opts, rz_bin_file_load_options_t::patch_relocs, and rz_bflt_new_buf().

◆ maps()

static RzList* maps ( RzBinFile bf)
static

Definition at line 36 of file bin_bflt.c.

36  {
37  RzBfltObj *obj = bf->o->bin_obj;
39  if (!ret) {
40  return NULL;
41  }
42 
44  if (!map) {
45  rz_list_free(ret);
46  return NULL;
47  }
48  map->paddr = 0;
49  map->vaddr = rz_bflt_get_text_base(obj);
50  map->psize = obj->hdr.data_start;
51  map->vsize = obj->hdr.data_start;
52  map->perm = RZ_PERM_RWX;
53  map->name = strdup("hdr+text");
54  map->vfile_name = obj->buf_patched ? strdup(VFILE_NAME_PATCHED) : NULL;
55  rz_list_append(ret, map);
56 
57  map = RZ_NEW0(RzBinMap);
58  if (!map) {
59  rz_list_free(ret);
60  return NULL;
61  }
62  map->paddr = obj->hdr.data_start;
63  map->vaddr = rz_bflt_get_data_base(obj);
64  map->psize = obj->hdr.data_end - obj->hdr.data_start;
65  map->vsize = rz_bflt_get_data_vsize(obj);
66  map->perm = RZ_PERM_RWX;
67  map->name = strdup("data+bss");
68  map->vfile_name = obj->buf_patched ? strdup(VFILE_NAME_PATCHED) : NULL;
69  rz_list_append(ret, map);
70 
71  return ret;
72 }
ut64 rz_bflt_get_data_base(RzBfltObj *bin)
Address to map data+bss segment to.
Definition: bflt.c:198
ut64 rz_bflt_get_data_vsize(RzBfltObj *bin)
Total size of data+bss.
Definition: bflt.c:204
ut64 rz_bflt_get_text_base(RzBfltObj *bin)
Address to map text segment to.
Definition: bflt.c:193
RZ_API void rz_bin_map_free(RzBinMap *map)
Definition: bin.c:1023
#define VFILE_NAME_PATCHED
Definition: bin_bflt.c:12
size_t map(int syms, int left, int len)
Definition: enough.c:237
void(* RzListFree)(void *ptr)
Definition: rz_list.h:11
#define RZ_PERM_RWX
Definition: rz_types.h:98
ut32 data_start
Definition: bflt.h:25
ut32 data_end
Definition: bflt.h:26
RzBfltHdr hdr
Definition: bflt.h:42
RzBuffer * buf_patched
overlay over the original file with relocs patched
Definition: bflt.h:46
Description of a single memory mapping into virtual memory from a binary.
Definition: rz_bin.h:602

References rz_bin_object_t::bin_obj, rz_bflt_obj_t::buf_patched, rz_bflt_hdr_t::data_end, rz_bflt_hdr_t::data_start, rz_bflt_obj_t::hdr, map(), NULL, rz_bin_file_t::o, rz_bflt_get_data_base(), rz_bflt_get_data_vsize(), rz_bflt_get_text_base(), rz_bin_map_free(), rz_list_append(), rz_list_free(), rz_list_newf(), RZ_NEW0, RZ_PERM_RWX, strdup(), and VFILE_NAME_PATCHED.

◆ relocs()

static RzList* relocs ( RzBinFile bf)
static

Definition at line 203 of file bin_bflt.c.

203  {
204  RzBfltObj *obj = (RzBfltObj *)bf->o->bin_obj;
206  if (!list || !obj) {
208  return NULL;
209  }
210  convert_relocs(obj, list, &obj->got_relocs);
211  convert_relocs(obj, list, &obj->relocs);
212  return list;
213 }
static void convert_relocs(RzBfltObj *bin, RzList *out, RzVector *relocs)
Definition: bin_bflt.c:185
static void list(RzEgg *egg)
Definition: rz-gg.c:52
RzVector got_relocs
Definition: bflt.h:44
RzVector relocs
Definition: bflt.h:43

References rz_bin_object_t::bin_obj, convert_relocs(), free(), rz_bflt_obj_t::got_relocs, list(), NULL, rz_bin_file_t::o, rz_bflt_obj_t::relocs, rz_list_free(), and rz_list_newf().

Referenced by convert_relocs().

◆ sections()

static RzList* sections ( RzBinFile bf)
static

Definition at line 74 of file bin_bflt.c.

74  {
75  RzBfltObj *obj = bf->o->bin_obj;
77  if (!ret) {
78  return NULL;
79  }
80 
81  // segments
82 
84  if (!sec) {
85  goto beach;
86  }
87  sec->paddr = 0;
88  sec->vaddr = rz_bflt_get_text_base(obj);
89  sec->size = obj->hdr.data_start;
90  sec->vsize = obj->hdr.data_start;
91  sec->perm = RZ_PERM_RWX;
92  sec->name = strdup("TEXT");
93  sec->is_segment = true;
94  rz_list_push(ret, sec);
95 
96  sec = RZ_NEW0(RzBinSection);
97  if (!sec) {
98  goto beach;
99  }
100  sec->paddr = obj->hdr.data_start;
101  sec->vaddr = rz_bflt_get_data_base(obj);
102  sec->size = obj->hdr.data_start;
103  sec->vsize = rz_bflt_get_data_vsize(obj);
104  sec->perm = RZ_PERM_RWX;
105  sec->name = strdup("DATA");
106  sec->is_segment = true;
107  rz_list_push(ret, sec);
108 
109  // sections
110 
111  sec = RZ_NEW0(RzBinSection);
112  if (!sec) {
113  goto beach;
114  }
115  sec->paddr = 0;
116  sec->vaddr = rz_bflt_get_text_base(obj);
117  sec->size = BFLT_HDR_SIZE;
118  sec->vsize = BFLT_HDR_SIZE;
119  sec->perm = RZ_PERM_RWX;
120  sec->name = strdup("header");
121  rz_list_push(ret, sec);
122 
123  sec = RZ_NEW0(RzBinSection);
124  if (!sec) {
125  goto beach;
126  }
127  sec->paddr = BFLT_HDR_SIZE;
129  sec->size = obj->hdr.data_start - BFLT_HDR_SIZE;
130  sec->vsize = obj->hdr.data_start - BFLT_HDR_SIZE;
131  sec->perm = RZ_PERM_RWX;
132  sec->name = strdup("text");
133  rz_list_push(ret, sec);
134 
135  sec = RZ_NEW0(RzBinSection);
136  if (!sec) {
137  goto beach;
138  }
139  sec->paddr = obj->hdr.data_start;
140  sec->vaddr = rz_bflt_get_data_base(obj);
141  sec->size = obj->hdr.data_end - obj->hdr.data_start;
142  sec->vsize = obj->hdr.data_end - obj->hdr.data_start;
143  sec->perm = RZ_PERM_RWX;
144  sec->name = strdup("data");
145  sec->is_data = true;
146  rz_list_push(ret, sec);
147 
148  sec = RZ_NEW0(RzBinSection);
149  if (!sec) {
150  goto beach;
151  }
152  sec->paddr = obj->hdr.data_end;
153  sec->vaddr = rz_bflt_get_data_base(obj) + obj->hdr.data_end - obj->hdr.data_start;
154  sec->size = 0;
155  sec->vsize = obj->hdr.bss_end - obj->hdr.data_end;
156  sec->perm = RZ_PERM_RWX;
157  sec->name = strdup("bss");
158  sec->is_data = true;
159  rz_list_push(ret, sec);
160 
161  return ret;
162 beach:
163  rz_list_free(ret);
164  return NULL;
165 }
#define BFLT_HDR_SIZE
Definition: bflt.h:53
RZ_API void rz_bin_section_free(RzBinSection *bs)
Definition: bin.c:1116
ut32 bss_end
Definition: bflt.h:27
bool is_segment
Definition: rz_bin.h:634
char * name
Definition: rz_bin.h:619

References BFLT_HDR_SIZE, rz_bin_object_t::bin_obj, rz_bflt_hdr_t::bss_end, rz_bflt_hdr_t::data_end, rz_bflt_hdr_t::data_start, rz_bflt_obj_t::hdr, rz_bin_section_t::is_data, rz_bin_section_t::is_segment, rz_bin_section_t::name, NULL, rz_bin_file_t::o, rz_bin_section_t::paddr, rz_bin_section_t::perm, rz_bflt_get_data_base(), rz_bflt_get_data_vsize(), rz_bflt_get_text_base(), rz_bin_section_free(), rz_list_free(), rz_list_newf(), rz_list_push(), RZ_NEW0, RZ_PERM_RWX, rz_bin_section_t::size, strdup(), rz_bin_section_t::vaddr, and rz_bin_section_t::vsize.

◆ strings()

static RzList* strings ( RzBinFile bf)
static

Definition at line 251 of file bin_bflt.c.

251  {
252  return rz_bin_file_strings(bf, 0, false);
253 }
RZ_API RZ_OWN RzList * rz_bin_file_strings(RZ_NONNULL RzBinFile *bf, size_t min_length, bool raw_strings)
Generates a RzList struct containing RzBinString from a given RzBinFile.
Definition: bfile_string.c:325

References rz_bin_file_strings().

◆ virtual_files()

static RzList* virtual_files ( RzBinFile bf)
static

Definition at line 167 of file bin_bflt.c.

167  {
168  RzBfltObj *obj = bf->o->bin_obj;
170  if (!r) {
171  return NULL;
172  }
173  if (obj->buf_patched) {
175  if (!vf) {
176  return r;
177  }
178  vf->buf = obj->buf_patched;
180  rz_list_push(r, vf);
181  }
182  return r;
183 }
RZ_API void rz_bin_virtual_file_free(RzBinVirtualFile *vfile)
Definition: bin.c:1012
RZ_NONNULL RzBuffer * buf
Definition: rz_bin.h:597
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_bflt_obj_t::buf_patched, rz_bin_virtual_file_t::name, NULL, rz_bin_file_t::o, r, rz_bin_virtual_file_free(), rz_list_newf(), rz_list_push(), RZ_NEW0, strdup(), and VFILE_NAME_PATCHED.

Variable Documentation

◆ rizin_plugin

RZ_API RzLibStruct rizin_plugin
Initial value:
= {
.type = RZ_LIB_TYPE_BIN,
}
RzBinPlugin rz_bin_plugin_bflt
Definition: bin_bflt.c:255
@ 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 272 of file bin_bflt.c.

◆ rz_bin_plugin_bflt

RzBinPlugin rz_bin_plugin_bflt
Initial value:
= {
.name = "bflt",
.desc = "bFLT uClinux executable",
.license = "LGPL3",
.load_buffer = &load_buffer,
.destroy = &destroy,
.check_buffer = &check_buffer,
.virtual_files = &virtual_files,
.maps = &maps,
.entries = &entries,
.sections = &sections,
.strings = &strings,
.info = &info,
.relocs = &relocs
}
static bool check_buffer(RzBuffer *buf)
Definition: bin_bflt.c:241
static bool load_buffer(RzBinFile *bf, RzBinObject *obj, RzBuffer *buf, Sdb *sdb)
Definition: bin_bflt.c:14
static RzList * strings(RzBinFile *bf)
Definition: bin_bflt.c:251
static void destroy(RzBinFile *bf)
Definition: bin_bflt.c:247
static RzList * virtual_files(RzBinFile *bf)
Definition: bin_bflt.c:167
static RzList * entries(RzBinFile *bf)
Definition: bin_bflt.c:19
static RzList * maps(RzBinFile *bf)
Definition: bin_bflt.c:36
static RzList * sections(RzBinFile *bf)
Definition: bin_bflt.c:74

Definition at line 255 of file bin_bflt.c.