Rizin
unix-like reverse engineering framework and cli tools
bin_te.c
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: 2013-2019 xvilka <anton.kochkov@gmail.com>
2 // SPDX-License-Identifier: LGPL-3.0-only
3 
4 #include <rz_types.h>
5 #include <rz_util.h>
6 #include <rz_lib.h>
7 #include <rz_bin.h>
8 #include "te/te_specs.h"
9 #include "te/te.h"
10 
11 static Sdb *get_sdb(RzBinFile *bf) {
12  RzBinObject *o = bf->o;
13  if (!o) {
14  return NULL;
15  }
16  struct rz_bin_te_obj_t *bin = (struct rz_bin_te_obj_t *)o->bin_obj;
17  return bin ? bin->kv : NULL;
18 }
19 
20 static bool load_buffer(RzBinFile *bf, RzBinObject *obj, RzBuffer *b, Sdb *sdb) {
21  rz_return_val_if_fail(bf && obj && b, false);
22  ut64 sz = rz_buf_size(b);
23  if (sz == 0 || sz == UT64_MAX) {
24  return false;
25  }
26  struct rz_bin_te_obj_t *res = rz_bin_te_new_buf(b);
27  if (res) {
28  sdb_ns_set(sdb, "info", res->kv);
29  }
30  obj->bin_obj = res;
31  return true;
32 }
33 
34 static void destroy(RzBinFile *bf) {
35  rz_bin_te_free((struct rz_bin_te_obj_t *)bf->o->bin_obj);
36 }
37 
38 static ut64 baddr(RzBinFile *bf) {
39  return rz_bin_te_get_image_base(bf->o->bin_obj);
40 }
41 
43  RzBinAddr *ret = NULL;
44  switch (type) {
46  if (!(ret = RZ_NEW(RzBinAddr))) {
47  return NULL;
48  }
49  ret->paddr = ret->vaddr = rz_bin_te_get_main_paddr(bf->o->bin_obj);
50  break;
51  default:
52  break;
53  }
54  return ret;
55 }
56 
57 static RzList *entries(RzBinFile *bf) {
58  RzList *ret = rz_list_newf(free);
59  if (ret) {
61  if (entry) {
62  RzBinAddr *ptr = RZ_NEW0(RzBinAddr);
63  if (ptr) {
64  ptr->paddr = entry->paddr;
65  ptr->vaddr = entry->vaddr;
66  rz_list_append(ret, ptr);
67  }
68  free(entry);
69  }
70  }
71  return ret;
72 }
73 
74 static RzList *sections(RzBinFile *bf) {
75  RzList *ret = NULL;
76  RzBinSection *ptr = NULL;
78  int i;
79 
80  if (!(ret = rz_list_new())) {
81  return NULL;
82  }
83  ret->free = free;
84  if (!(sections = rz_bin_te_get_sections(bf->o->bin_obj))) {
85  free(ret);
86  return NULL;
87  }
88  for (i = 0; !sections[i].last; i++) {
89  if (!(ptr = RZ_NEW0(RzBinSection))) {
90  break;
91  }
92  ptr->name = strdup((char *)sections[i].name);
93  ptr->size = sections[i].size;
94  ptr->vsize = sections[i].vsize;
95  ptr->paddr = sections[i].paddr;
96  ptr->vaddr = sections[i].vaddr;
97  ptr->perm = 0;
99  ptr->perm |= RZ_PERM_X;
100  }
102  ptr->perm |= RZ_PERM_W;
103  }
105  ptr->perm |= RZ_PERM_R;
106  }
108  ptr->perm |= RZ_PERM_SHAR;
109  }
110  /* All TE files have _TEXT_RE section, which is 16-bit, because of
111  * CPU start in this mode */
112  if (!strncmp(ptr->name, "_TEXT_RE", 8)) {
113  ptr->bits = RZ_SYS_BITS_16;
114  }
115  rz_list_append(ret, ptr);
116  }
117  free(sections);
118  return ret;
119 }
120 
121 static RzBinInfo *info(RzBinFile *bf) {
122  RzBinInfo *ret = RZ_NEW0(RzBinInfo);
123  if (!ret) {
124  return NULL;
125  }
126  ret->file = strdup(bf->file);
127  ret->bclass = strdup("TE");
128  ret->rclass = strdup("te");
129  ret->os = rz_bin_te_get_os(bf->o->bin_obj);
130  ret->arch = rz_bin_te_get_arch(bf->o->bin_obj);
131  ret->machine = rz_bin_te_get_machine(bf->o->bin_obj);
133  ret->type = strdup("EXEC (Executable file)");
134  ret->bits = rz_bin_te_get_bits(bf->o->bin_obj);
135  ret->big_endian = 1;
136  ret->dbg_info = 0;
137  ret->has_va = true;
138 
139  sdb_num_set(bf->sdb, "te.bits", ret->bits, 0);
140 
141  return ret;
142 }
143 
144 static bool check_buffer(RzBuffer *b) {
145  ut8 buf[2];
146  if (rz_buf_read_at(b, 0, buf, 2) == 2) {
147  return !memcmp(buf, "\x56\x5a", 2);
148  }
149  return false;
150 }
151 
153  .name = "te",
154  .desc = "TE bin plugin", // Terse Executable format
155  .license = "LGPL3",
156  .get_sdb = &get_sdb,
157  .load_buffer = &load_buffer,
158  .destroy = &destroy,
159  .check_buffer = &check_buffer,
160  .baddr = &baddr,
161  .binsym = &binsym,
162  .entries = &entries,
164  .sections = &sections,
165  .info = &info,
166  .minstrlen = 4,
167 };
168 
169 #ifndef RZ_PLUGIN_INCORE
172  .data = &rz_bin_plugin_te,
174 };
175 #endif
lzma_index ** i
Definition: index.h:629
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_te.c:20
static Sdb * get_sdb(RzBinFile *bf)
Definition: bin_te.c:11
static RzBinAddr * binsym(RzBinFile *bf, RzBinSpecialSymbol type)
Definition: bin_te.c:42
RzBinPlugin rz_bin_plugin_te
Definition: bin_te.c:152
static void destroy(RzBinFile *bf)
Definition: bin_te.c:34
RZ_API RzLibStruct rizin_plugin
Definition: bin_te.c:170
static bool check_buffer(RzBuffer *b)
Definition: bin_te.c:144
static RzBinInfo * info(RzBinFile *bf)
Definition: bin_te.c:121
static ut64 baddr(RzBinFile *bf)
Definition: bin_te.c:38
static RzList * entries(RzBinFile *bf)
Definition: bin_te.c:57
static RzList * sections(RzBinFile *bf)
Definition: bin_te.c:74
#define RZ_API
#define NULL
Definition: cris-opc.c:27
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
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
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 type
Definition: mipsasm.c:17
RZ_API int sdb_ns_set(Sdb *s, const char *name, Sdb *r)
Definition: ns.c:156
RZ_API int sdb_num_set(Sdb *s, const char *key, ut64 v, ut32 cas)
Definition: num.c:25
#define rz_return_val_if_fail(expr, val)
Definition: rz_assert.h:108
RzBinSpecialSymbol
Definition: rz_bin.h:136
@ RZ_BIN_SPECIAL_SYMBOL_MAIN
Definition: rz_bin.h:139
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 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
@ RZ_SYS_BITS_16
Definition: rz_sys.h:19
#define RZ_PERM_R
Definition: rz_types.h:93
#define RZ_NEW0(x)
Definition: rz_types.h:284
#define RZ_NEW(x)
Definition: rz_types.h:285
#define RZ_PERM_W
Definition: rz_types.h:94
#define RZ_PERM_X
Definition: rz_types.h:95
#define RZ_PERM_SHAR
Definition: rz_types.h:100
#define UT64_MAX
Definition: rz_types_base.h:86
#define RZ_VERSION
Definition: rz_version.h:8
static struct sockaddr static addrlen static backlog const void static flags void flags
Definition: sfsocketcall.h:123
#define b(i)
Definition: sha256.c:42
Definition: malloc.c:26
Definition: zipcmp.c:77
Definition: z80asm.h:102
ut64 vaddr
Definition: rz_bin.h:186
ut64 paddr
Definition: rz_bin.h:187
XX curplugin == o->plugin.
Definition: rz_bin.h:298
RzBinObject * o
Definition: rz_bin.h:305
RZ_DEPRECATE Sdb * sdb
deprecated, put info in C structures instead of this
Definition: rz_bin.h:315
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 * 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
char * name
Definition: rz_bin.h:509
char * version
Definition: rz_bin.h:512
char * name
Definition: rz_bin.h:619
Sdb * kv
Definition: te.h:44
RzListFree free
Definition: rz_list.h:21
Definition: sdb.h:63
ut64 rz_bin_te_get_image_base(struct rz_bin_te_obj_t *bin)
Definition: te.c:222
struct rz_bin_te_section_t * rz_bin_te_get_sections(struct rz_bin_te_obj_t *bin)
Definition: te.c:359
ut64 rz_bin_te_get_main_paddr(struct rz_bin_te_obj_t *bin)
Definition: te.c:95
char * rz_bin_te_get_os(struct rz_bin_te_obj_t *bin)
Definition: te.c:325
char * rz_bin_te_get_arch(struct rz_bin_te_obj_t *bin)
Definition: te.c:167
char * rz_bin_te_get_subsystem(struct rz_bin_te_obj_t *bin)
Definition: te.c:388
struct rz_bin_te_obj_t * rz_bin_te_new_buf(RzBuffer *buf)
Definition: te.c:466
RzBinAddr * rz_bin_te_get_entrypoint(struct rz_bin_te_obj_t *bin)
Definition: te.c:204
char * rz_bin_te_get_machine(struct rz_bin_te_obj_t *bin)
Definition: te.c:229
void * rz_bin_te_free(struct rz_bin_te_obj_t *bin)
Definition: te.c:431
int rz_bin_te_get_bits(struct rz_bin_te_obj_t *bin)
Definition: te.c:200
#define RZ_BIN_TE_SCN_IS_WRITABLE(x)
Definition: te.h:16
#define RZ_BIN_TE_SCN_IS_READABLE(x)
Definition: te.h:15
#define RZ_BIN_TE_SCN_IS_SHAREABLE(x)
Definition: te.h:13
#define RZ_BIN_TE_SCN_IS_EXECUTABLE(x)
Definition: te.h:14
ut64(WINAPI *w32_GetEnabledXStateFeatures)()