Rizin
unix-like reverse engineering framework and cli tools
bin_xbe.c File Reference
#include <rz_types.h>
#include <rz_util.h>
#include <rz_lib.h>
#include <rz_bin.h>
#include <string.h>
#include "../format/xbe/xbe.h"
#include "../format/xbe/kernel.h"

Go to the source code of this file.

Functions

static bool read_xbe_header (xbe_header *hdr, RzBuffer *b, ut64 off)
 
static bool read_xbe_section (xbe_section *sect, RzBuffer *b, ut64 off)
 
static bool read_xbe_lib (xbe_lib *lib, RzBuffer *b, ut64 off)
 
static bool check_buffer (RzBuffer *b)
 
static bool load_buffer (RzBinFile *bf, RzBinObject *o, RzBuffer *buf, Sdb *sdb)
 
static void destroy (RzBinFile *bf)
 
static RzBinAddrbinsym (RzBinFile *bf, RzBinSpecialSymbol type)
 
static RzListentries (RzBinFile *bf)
 
static RzListsections (RzBinFile *bf)
 
static char * describe_xbe_lib_at (RzBuffer *b, ut64 off, ut64 filesz)
 
static RzListlibs (RzBinFile *bf)
 
static RzListsymbols (RzBinFile *bf)
 
static RzBinInfoinfo (RzBinFile *bf)
 
static ut64 baddr (RzBinFile *bf)
 

Variables

static const char * kt_name []
 
RzBinPlugin rz_bin_plugin_xbe
 
RZ_API RzLibStruct rizin_plugin
 

Function Documentation

◆ baddr()

static ut64 baddr ( RzBinFile bf)
static

Definition at line 366 of file bin_xbe.c.

366  {
367  rz_bin_xbe_obj_t *obj = bf->o->bin_obj;
368  return obj->header.base;
369 }
RzBinObject * o
Definition: rz_bin.h:305
void * bin_obj
Definition: rz_bin.h:293
xbe_header header
Definition: xbe.h:69
ut32 base
Definition: xbe.h:22

References xbe_header::base, rz_bin_object_t::bin_obj, rz_bin_xbe_obj_t::header, and rz_bin_file_t::o.

◆ binsym()

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

Definition at line 113 of file bin_xbe.c.

113  {
114  if (!bf || !bf->buf || type != RZ_BIN_SPECIAL_SYMBOL_MAIN) {
115  return NULL;
116  }
117  rz_bin_xbe_obj_t *obj = bf->o->bin_obj;
118  RzBinAddr *ret = RZ_NEW0(RzBinAddr);
119  if (!ret) {
120  return NULL;
121  }
122  ret->vaddr = obj->header.ep ^ obj->ep_key;
123  ret->paddr = ret->vaddr - obj->header.base;
124  return ret;
125 }
#define NULL
Definition: cris-opc.c:27
int type
Definition: mipsasm.c:17
@ RZ_BIN_SPECIAL_SYMBOL_MAIN
Definition: rz_bin.h:139
#define RZ_NEW0(x)
Definition: rz_types.h:284
ut64 vaddr
Definition: rz_bin.h:186
ut64 paddr
Definition: rz_bin.h:187
RzBuffer * buf
Definition: rz_bin.h:303
int ep_key
Definition: xbe.h:71
ut32 ep
Definition: xbe.h:31

References xbe_header::base, rz_bin_object_t::bin_obj, rz_bin_file_t::buf, xbe_header::ep, rz_bin_xbe_obj_t::ep_key, rz_bin_xbe_obj_t::header, NULL, rz_bin_file_t::o, rz_bin_addr_t::paddr, RZ_BIN_SPECIAL_SYMBOL_MAIN, RZ_NEW0, type, and rz_bin_addr_t::vaddr.

◆ check_buffer()

static bool check_buffer ( RzBuffer b)
static

Definition at line 74 of file bin_xbe.c.

74  {
75  ut8 magic[4];
76  if (rz_buf_read_at(b, 0, magic, sizeof(magic)) == 4) {
77  return !memcmp(magic, "XBEH", 4);
78  }
79  return false;
80 }
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
#define b(i)
Definition: sha256.c:42

References b, and rz_buf_read_at().

◆ describe_xbe_lib_at()

static char* describe_xbe_lib_at ( RzBuffer b,
ut64  off,
ut64  filesz 
)
static

Generate a string like "<LIBNAME> <MAJOR>.<MINOR>.<BUILD>" by reading an xbe_lib at the given offset.

Definition at line 219 of file bin_xbe.c.

219  {
220  if (off + sizeof(xbe_lib) > filesz) {
221  return NULL;
222  }
223  xbe_lib lib;
224  if (!read_xbe_lib(&lib, b, off)) {
225  return NULL;
226  }
227  // lib.name may not be 0-terminated
228  char name[9];
229  RZ_STATIC_ASSERT(sizeof(name) == sizeof(lib.name) + 1);
230  memcpy(name, lib.name, sizeof(lib.name));
231  name[sizeof(lib.name)] = 0;
232  return rz_str_newf("%s %i.%i.%i", name, lib.major, lib.minor, lib.build);
233 }
static bool read_xbe_lib(xbe_lib *lib, RzBuffer *b, ut64 off)
Definition: bin_xbe.c:66
memcpy(mem, inblock.get(), min(CONTAINING_RECORD(inblock.get(), MEMBLOCK, data) ->size, size))
int off
Definition: pal.c:13
#define RZ_STATIC_ASSERT(x)
Definition: rz_assert.h:10
RZ_API char * rz_str_newf(const char *fmt,...) RZ_PRINTF_CHECK(1
Definition: z80asm.h:102
Definition: xbe.h:60
ut16 build
Definition: xbe.h:64
ut8 name[8]
Definition: xbe.h:61
ut16 major
Definition: xbe.h:62
ut16 minor
Definition: xbe.h:63

References b, xbe_lib::build, xbe_lib::major, memcpy(), xbe_lib::minor, xbe_lib::name, NULL, off, read_xbe_lib(), RZ_STATIC_ASSERT, and rz_str_newf().

Referenced by libs().

◆ destroy()

static void destroy ( RzBinFile bf)
static

Definition at line 109 of file bin_xbe.c.

109  {
110  RZ_FREE(bf->o->bin_obj);
111 }
#define RZ_FREE(x)
Definition: rz_types.h:369

References rz_bin_object_t::bin_obj, rz_bin_file_t::o, and RZ_FREE.

◆ entries()

static RzList* entries ( RzBinFile bf)
static

Definition at line 127 of file bin_xbe.c.

127  {
128  const rz_bin_xbe_obj_t *obj;
129  RzList *ret;
130  RzBinAddr *ptr = RZ_NEW0(RzBinAddr);
131  if (!bf || !bf->buf || !bf->o->bin_obj || !ptr) {
132  free(ptr);
133  return NULL;
134  }
135  ret = rz_list_new();
136  if (!ret) {
137  free(ptr);
138  return NULL;
139  }
140  ret->free = free;
141  obj = bf->o->bin_obj;
142  ptr->vaddr = obj->header.ep ^ obj->ep_key;
143  ptr->paddr = ptr->vaddr - obj->header.base;
144  rz_list_append(ret, ptr);
145  return ret;
146 }
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
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
RzListFree free
Definition: rz_list.h:21

References xbe_header::base, rz_bin_object_t::bin_obj, rz_bin_file_t::buf, xbe_header::ep, rz_bin_xbe_obj_t::ep_key, rz_list_t::free, free(), rz_bin_xbe_obj_t::header, NULL, rz_bin_file_t::o, rz_bin_addr_t::paddr, rz_list_append(), rz_list_new(), RZ_NEW0, and rz_bin_addr_t::vaddr.

◆ info()

static RzBinInfo* info ( RzBinFile bf)
static

Definition at line 333 of file bin_xbe.c.

333  {
334  rz_bin_xbe_obj_t *obj;
335  RzBinInfo *ret;
336  ut8 dbg_name[256];
337 
338  if (!bf || !bf->buf) {
339  return NULL;
340  }
341 
342  ret = RZ_NEW0(RzBinInfo);
343  if (!ret) {
344  return NULL;
345  }
346 
347  obj = bf->o->bin_obj;
348 
349  memset(dbg_name, 0, sizeof(dbg_name));
350  rz_buf_read_at(bf->buf, obj->header.debug_name_addr - obj->header.base, dbg_name, sizeof(dbg_name));
351  dbg_name[sizeof(dbg_name) - 1] = 0;
352  ret->file = strdup((char *)dbg_name);
353  ret->bclass = strdup("program");
354  ret->machine = strdup("Microsoft Xbox");
355  ret->os = strdup("xbox");
356  ret->type = strdup("Microsoft Xbox executable");
357  ret->arch = strdup("x86");
358  ret->has_va = 1;
359  ret->bits = 32;
360  ret->big_endian = 0;
361  ret->dbg_info = 0;
362  ret->lang = NULL;
363  return ret;
364 }
return memset(p, 0, total)
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 has_va
Definition: rz_bin.h:228
char * type
Definition: rz_bin.h:211
char * os
Definition: rz_bin.h:219
char * machine
Definition: rz_bin.h:216
const char * lang
Definition: rz_bin.h:224
char * bclass
Definition: rz_bin.h:212
char * file
Definition: rz_bin.h:210
ut64 dbg_info
Definition: rz_bin.h:240
char * arch
Definition: rz_bin.h:214
int big_endian
Definition: rz_bin.h:235
ut32 debug_name_addr
Definition: xbe.h:35

References rz_bin_info_t::arch, xbe_header::base, rz_bin_info_t::bclass, rz_bin_info_t::big_endian, rz_bin_object_t::bin_obj, rz_bin_info_t::bits, rz_bin_file_t::buf, rz_bin_info_t::dbg_info, xbe_header::debug_name_addr, rz_bin_info_t::file, rz_bin_info_t::has_va, rz_bin_xbe_obj_t::header, rz_bin_info_t::lang, rz_bin_info_t::machine, memset(), NULL, rz_bin_file_t::o, rz_bin_info_t::os, rz_buf_read_at(), RZ_NEW0, strdup(), and rz_bin_info_t::type.

◆ libs()

static RzList* libs ( RzBinFile bf)
static

Definition at line 235 of file bin_xbe.c.

235  {
236  if (!bf || !bf->o || !bf->o->bin_obj) {
237  return NULL;
238  }
239  rz_bin_xbe_obj_t *obj = bf->o->bin_obj;
240  xbe_header *h = &obj->header;
241  RzList *ret = rz_list_newf(free);
242  if (!ret) {
243  return NULL;
244  }
245 
246  // Hint: h->kernel_lib_addr and h->xapi_lib_addr also point to xbe_lib structs,
247  // but in our known samples, they just point into the array below, so no need
248  // to check them explicitly.
249 
250  for (ut32 i = 0; i < h->lib_versions; i++) {
251  ut64 addr = h->lib_versions_addr - h->base + (i * sizeof(xbe_lib));
252  char *lib = describe_xbe_lib_at(bf->buf, addr, bf->size);
253  if (!lib) {
254  break;
255  }
256  rz_list_push(ret, lib);
257  }
258 
259  return ret;
260 }
lzma_index ** i
Definition: index.h:629
static char * describe_xbe_lib_at(RzBuffer *b, ut64 off, ut64 filesz)
Definition: bin_xbe.c:219
uint32_t ut32
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_push(RZ_NONNULL RzList *list, void *item)
Alias for rz_list_append.
Definition: list.c:60
#define h(i)
Definition: sha256.c:48
Definition: xbe.h:19
ut64(WINAPI *w32_GetEnabledXStateFeatures)()
static int addr
Definition: z80asm.c:58

References addr, rz_bin_object_t::bin_obj, rz_bin_file_t::buf, describe_xbe_lib_at(), free(), h, rz_bin_xbe_obj_t::header, i, NULL, rz_bin_file_t::o, rz_list_newf(), rz_list_push(), rz_bin_file_t::size, and ut64().

◆ load_buffer()

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

Definition at line 82 of file bin_xbe.c.

82  {
84  if (!obj) {
85  return false;
86  }
87  if (!read_xbe_header(&obj->header, buf, 0)) {
88  RZ_FREE(obj);
89  return false;
90  }
91 
92  if ((obj->header.ep & 0xf0000000) == 0x40000000) {
93  // Sega Chihiro xbe
94  obj->ep_key = XBE_EP_CHIHIRO;
95  obj->kt_key = XBE_KP_CHIHIRO;
96  } else if ((obj->header.ep ^ XBE_EP_RETAIL) > 0x1000000) {
97  // Debug xbe
98  obj->ep_key = XBE_EP_DEBUG;
99  obj->kt_key = XBE_KP_DEBUG;
100  } else {
101  // Retail xbe
102  obj->ep_key = XBE_EP_RETAIL;
103  obj->kt_key = XBE_KP_RETAIL;
104  }
105  o->bin_obj = obj;
106  return true;
107 }
static bool read_xbe_header(xbe_header *hdr, RzBuffer *b, ut64 off)
Definition: bin_xbe.c:17
voidpf void * buf
Definition: ioapi.h:138
#define RZ_NEW(x)
Definition: rz_types.h:285
int kt_key
Definition: xbe.h:70
#define XBE_EP_CHIHIRO
Definition: xbe.h:15
#define XBE_EP_DEBUG
Definition: xbe.h:10
#define XBE_KP_CHIHIRO
Definition: xbe.h:16
#define XBE_KP_RETAIL
Definition: xbe.h:12
#define XBE_EP_RETAIL
Definition: xbe.h:9
#define XBE_KP_DEBUG
Definition: xbe.h:13

References rz_bin_object_t::bin_obj, xbe_header::ep, rz_bin_xbe_obj_t::ep_key, rz_bin_xbe_obj_t::header, rz_bin_xbe_obj_t::kt_key, read_xbe_header(), RZ_FREE, RZ_NEW, XBE_EP_CHIHIRO, XBE_EP_DEBUG, XBE_EP_RETAIL, XBE_KP_CHIHIRO, XBE_KP_DEBUG, and XBE_KP_RETAIL.

◆ read_xbe_header()

static bool read_xbe_header ( xbe_header hdr,
RzBuffer b,
ut64  off 
)
static

Definition at line 17 of file bin_xbe.c.

17  {
18  if (rz_buf_read_at(b, off, hdr->magic, sizeof(hdr->magic)) != sizeof(hdr->magic) ||
19  rz_buf_read_at(b, off + 4, hdr->signature, sizeof(hdr->signature)) != sizeof(hdr->signature) ||
20  !rz_buf_read_le32_at(b, off + 0x104, &hdr->base) ||
21  !rz_buf_read_le32_at(b, off + 0x104 + sizeof(ut32), &hdr->headers_size) ||
22  !rz_buf_read_le32_at(b, off + 0x104 + sizeof(ut32) * 2, &hdr->image_size) ||
23  !rz_buf_read_le32_at(b, off + 0x104 + sizeof(ut32) * 3, &hdr->image_header_size) ||
24  !rz_buf_read_le32_at(b, off + 0x104 + sizeof(ut32) * 4, &hdr->timestamp) ||
25  !rz_buf_read_le32_at(b, off + 0x104 + sizeof(ut32) * 5, &hdr->cert_addr) ||
26  !rz_buf_read_le32_at(b, off + 0x104 + sizeof(ut32) * 6, &hdr->sections) ||
27  !rz_buf_read_le32_at(b, off + 0x104 + sizeof(ut32) * 7, &hdr->sechdr_addr) ||
28  !rz_buf_read_le32_at(b, off + 0x104 + sizeof(ut32) * 8, &hdr->init_flags) ||
29  !rz_buf_read_le32_at(b, off + 0x104 + sizeof(ut32) * 9, &hdr->ep) ||
30  !rz_buf_read_le32_at(b, off + 0x104 + sizeof(ut32) * 10, &hdr->tls_addr)) {
31  return false;
32  }
33  off += 0x104 + sizeof(ut32) * 11;
34  for (size_t i = 0; i < RZ_ARRAY_SIZE(hdr->pe_data); i++) {
35  if (!rz_buf_read_le32_at(b, off, &hdr->pe_data[i])) {
36  return false;
37  }
38  off += sizeof(ut32);
39  }
40  return rz_buf_read_le32_at(b, off, &hdr->debug_path_addr) &&
41  rz_buf_read_le32_at(b, off + sizeof(ut32), &hdr->debug_name_addr) &&
42  rz_buf_read_le32_at(b, off + sizeof(ut32) * 2, &hdr->debug_uname_addr) &&
43  rz_buf_read_le32_at(b, off + sizeof(ut32) * 3, &hdr->kernel_thunk_addr) &&
44  rz_buf_read_le32_at(b, off + sizeof(ut32) * 4, &hdr->nonkernel_import_dir_addr) &&
45  rz_buf_read_le32_at(b, off + sizeof(ut32) * 5, &hdr->lib_versions) &&
46  rz_buf_read_le32_at(b, off + sizeof(ut32) * 6, &hdr->lib_versions_addr) &&
47  rz_buf_read_le32_at(b, off + sizeof(ut32) * 7, &hdr->kernel_lib_addr) &&
48  rz_buf_read_le32_at(b, off + sizeof(ut32) * 8, &hdr->xapi_lib_addr) &&
49  rz_buf_read_le32_at(b, off + sizeof(ut32) * 9, &hdr->padding[0]) &&
50  rz_buf_read_le32_at(b, off + sizeof(ut32) * 10, &hdr->padding[1]);
51 }
#define rz_buf_read_le32_at(b, addr, result)
Definition: rz_buf.h:271
#define RZ_ARRAY_SIZE(x)
Definition: rz_types.h:300
ut32 padding[2]
Definition: xbe.h:43
ut32 lib_versions_addr
Definition: xbe.h:40
ut32 headers_size
Definition: xbe.h:23
ut8 magic[4]
Definition: xbe.h:20
ut32 cert_addr
Definition: xbe.h:27
ut32 pe_data[7]
Definition: xbe.h:33
ut32 xapi_lib_addr
Definition: xbe.h:42
ut32 tls_addr
Definition: xbe.h:32
ut32 debug_uname_addr
Definition: xbe.h:36
ut32 debug_path_addr
Definition: xbe.h:34
ut8 signature[0x100]
Definition: xbe.h:21
ut32 sections
Definition: xbe.h:28
ut32 init_flags
Definition: xbe.h:30
ut32 kernel_lib_addr
Definition: xbe.h:41
ut32 image_header_size
Definition: xbe.h:25
ut32 lib_versions
Definition: xbe.h:39
ut32 image_size
Definition: xbe.h:24
ut32 nonkernel_import_dir_addr
Definition: xbe.h:38
ut32 sechdr_addr
Definition: xbe.h:29
ut32 kernel_thunk_addr
Definition: xbe.h:37
ut32 timestamp
Definition: xbe.h:26

References b, xbe_header::base, xbe_header::cert_addr, xbe_header::debug_name_addr, xbe_header::debug_path_addr, xbe_header::debug_uname_addr, xbe_header::ep, xbe_header::headers_size, i, xbe_header::image_header_size, xbe_header::image_size, xbe_header::init_flags, xbe_header::kernel_lib_addr, xbe_header::kernel_thunk_addr, xbe_header::lib_versions, xbe_header::lib_versions_addr, xbe_header::magic, xbe_header::nonkernel_import_dir_addr, off, xbe_header::padding, xbe_header::pe_data, RZ_ARRAY_SIZE, rz_buf_read_at(), rz_buf_read_le32_at, xbe_header::sechdr_addr, xbe_header::sections, xbe_header::signature, xbe_header::timestamp, xbe_header::tls_addr, and xbe_header::xapi_lib_addr.

Referenced by load_buffer().

◆ read_xbe_lib()

static bool read_xbe_lib ( xbe_lib lib,
RzBuffer b,
ut64  off 
)
static

Definition at line 66 of file bin_xbe.c.

66  {
67  return rz_buf_read_at(b, off, lib->name, sizeof(lib->name)) == sizeof(lib->name) &&
68  rz_buf_read_le16_at(b, off + 8, &lib->major) &&
69  rz_buf_read_le16_at(b, off + 8 + sizeof(ut16), &lib->minor) &&
70  rz_buf_read_le16_at(b, off + 8 + sizeof(ut16) * 2, &lib->build) &&
71  rz_buf_read_le16_at(b, off + 8 + sizeof(ut16) * 3, &lib->flags);
72 }
uint16_t ut16
#define rz_buf_read_le16_at(b, addr, result)
Definition: rz_buf.h:270
ut16 flags
Definition: xbe.h:65

References b, xbe_lib::build, xbe_lib::flags, xbe_lib::major, xbe_lib::minor, xbe_lib::name, off, rz_buf_read_at(), and rz_buf_read_le16_at.

Referenced by describe_xbe_lib_at().

◆ read_xbe_section()

static bool read_xbe_section ( xbe_section sect,
RzBuffer b,
ut64  off 
)
static

Definition at line 53 of file bin_xbe.c.

53  {
54  return rz_buf_read_le32_at(b, off, &sect->flags) &&
55  rz_buf_read_le32_at(b, off + sizeof(ut32), &sect->vaddr) &&
56  rz_buf_read_le32_at(b, off + sizeof(ut32) * 2, &sect->vsize) &&
57  rz_buf_read_le32_at(b, off + sizeof(ut32) * 3, &sect->offset) &&
58  rz_buf_read_le32_at(b, off + sizeof(ut32) * 4, &sect->size) &&
59  rz_buf_read_le32_at(b, off + sizeof(ut32) * 5, &sect->name_addr) &&
60  rz_buf_read_le32_at(b, off + sizeof(ut32) * 6, &sect->refcount) &&
61  rz_buf_read_le32_at(b, off + sizeof(ut32) * 7, &sect->padding[0]) &&
62  rz_buf_read_le32_at(b, off + sizeof(ut32) * 8, &sect->padding[1]) &&
63  rz_buf_read_at(b, off + sizeof(ut32) * 9, sect->digest, sizeof(sect->digest)) == sizeof(sect->digest);
64 }
ut32 vaddr
Definition: xbe.h:50
ut32 offset
Definition: xbe.h:52
ut32 name_addr
Definition: xbe.h:54
ut32 size
Definition: xbe.h:53
ut8 digest[20]
Definition: xbe.h:57
ut32 vsize
Definition: xbe.h:51
ut32 padding[2]
Definition: xbe.h:56
ut32 flags
Definition: xbe.h:49
ut32 refcount
Definition: xbe.h:55

References b, xbe_section::digest, xbe_section::flags, xbe_section::name_addr, off, xbe_section::offset, xbe_section::padding, xbe_section::refcount, rz_buf_read_at(), rz_buf_read_le32_at, xbe_section::size, xbe_section::vaddr, and xbe_section::vsize.

Referenced by sections(), and symbols().

◆ sections()

static RzList* sections ( RzBinFile bf)
static

Definition at line 148 of file bin_xbe.c.

148  {
149  rz_bin_xbe_obj_t *obj = NULL;
150  xbe_header *h = NULL;
151  RzList *ret = NULL;
152  char tmp[0x100];
153  int i, r;
154  ut32 addr;
155 
156  if (!bf || !bf->o || !bf->o->bin_obj || !bf->buf) {
157  return NULL;
158  }
159  obj = bf->o->bin_obj;
160  h = &obj->header;
161  if (h->sections < 1) {
162  return NULL;
163  }
164  ret = rz_list_new();
165  if (!ret) {
166  return NULL;
167  }
168  ret->free = free;
169  if (h->sections < 1 || h->sections > 255) {
170  goto out_error;
171  }
172  addr = h->sechdr_addr - h->base;
173  if (addr > bf->size || addr + (sizeof(xbe_section) * h->sections) > bf->size) {
174  goto out_error;
175  }
176  for (i = 0; i < h->sections; i++) {
177  xbe_section sect;
178  if (!read_xbe_section(&sect, bf->buf, addr + i * sizeof(xbe_section))) {
179  goto out_error;
180  }
182  ut32 name_addr = sect.name_addr - h->base;
183  tmp[0] = 0;
184  if (name_addr > bf->size || name_addr + sizeof(tmp) > bf->size) {
185  free(item);
186  goto out_error;
187  }
188  r = rz_buf_read_at(bf->buf, name_addr, (ut8 *)tmp, sizeof(tmp));
189  if (r < 1) {
190  free(item);
191  goto out_error;
192  }
193  tmp[sizeof(tmp) - 1] = 0;
194  item->name = rz_str_newf("%s.%i", tmp, i);
195  item->paddr = sect.offset;
196  item->vaddr = sect.vaddr;
197  item->size = sect.size;
198  item->vsize = sect.vsize;
199 
200  item->perm = RZ_PERM_R;
201  if (sect.flags & SECT_FLAG_X) {
202  item->perm |= RZ_PERM_X;
203  }
204  if (sect.flags & SECT_FLAG_W) {
205  item->perm |= RZ_PERM_W;
206  }
207  rz_list_append(ret, item);
208  }
209  return ret;
210 out_error:
211  rz_list_free(ret);
212  return NULL;
213 }
static bool read_xbe_section(xbe_section *sect, RzBuffer *b, ut64 off)
Definition: bin_xbe.c:53
#define r
Definition: crypto_rc6.c:12
RZ_API void rz_list_free(RZ_NONNULL RzList *list)
Empties the list and frees the list pointer.
Definition: list.c:137
#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
char * name
Definition: rz_bin.h:619
#define SECT_FLAG_X
Definition: xbe.h:46
#define SECT_FLAG_W
Definition: xbe.h:47

References addr, rz_bin_object_t::bin_obj, rz_bin_file_t::buf, xbe_section::flags, rz_list_t::free, free(), h, rz_bin_xbe_obj_t::header, i, rz_bin_section_t::name, xbe_section::name_addr, NULL, rz_bin_file_t::o, xbe_section::offset, rz_bin_section_t::paddr, rz_bin_section_t::perm, r, read_xbe_section(), rz_buf_read_at(), rz_list_append(), rz_list_free(), rz_list_new(), RZ_NEW0, RZ_PERM_R, RZ_PERM_W, RZ_PERM_X, rz_str_newf(), SECT_FLAG_W, SECT_FLAG_X, xbe_section::size, rz_bin_file_t::size, rz_bin_section_t::size, autogen_x86imm::tmp, xbe_section::vaddr, rz_bin_section_t::vaddr, xbe_section::vsize, and rz_bin_section_t::vsize.

◆ symbols()

static RzList* symbols ( RzBinFile bf)
static

Definition at line 262 of file bin_xbe.c.

262  {
263  rz_bin_xbe_obj_t *obj;
264  xbe_header *h;
265  RzList *ret;
266  ut32 kt_addr;
267  ut32 addr;
268 
269  if (!bf || !bf->o || !bf->o->bin_obj) {
270  return NULL;
271  }
272 
273  obj = bf->o->bin_obj;
274  h = &obj->header;
275  kt_addr = h->kernel_thunk_addr ^ obj->kt_key;
277  if (!ret) {
278  return NULL;
279  }
280  int limit = h->sections;
281  if (limit * (sizeof(xbe_section)) >= bf->size - h->sechdr_addr) {
282  goto out_error;
283  }
284  xbe_section sect;
285  bool found = false;
286  for (size_t i = 0; found == false && i < limit; i++) {
287  addr = h->sechdr_addr - h->base + (sizeof(xbe_section) * i);
288  if (addr > bf->size || addr + sizeof(sect) > bf->size ||
289  !read_xbe_section(&sect, bf->buf, addr)) {
290  goto out_error;
291  }
292  if (kt_addr >= sect.vaddr && kt_addr < sect.vaddr + sect.vsize) {
293  found = true;
294  }
295  }
296  if (!found) {
297  goto out_error;
298  }
299  addr = sect.offset + (kt_addr - sect.vaddr);
300  ut32 thunk_addr[XBE_MAX_THUNK];
301  if (addr > bf->size || addr + sizeof(thunk_addr) > bf->size) {
302  goto out_error;
303  }
304  for (size_t i = 0; i < RZ_ARRAY_SIZE(thunk_addr); i++) {
305  if (!rz_buf_read_le32_at(bf->buf, addr + i * sizeof(ut32), &thunk_addr[i])) {
306  goto out_error;
307  }
308  }
309  for (size_t i = 0; i < XBE_MAX_THUNK && thunk_addr[i]; i++) {
311  if (!sym) {
312  goto out_error;
313  }
314  const ut32 thunk_index = thunk_addr[i] ^ 0x80000000;
315  // Basic sanity checks
316  if (thunk_addr[i] & 0x80000000 && thunk_index > 0 && thunk_index <= XBE_MAX_THUNK) {
317  sym->name = rz_str_newf("kt.%s", kt_name[thunk_index - 1]);
318  sym->vaddr = (h->kernel_thunk_addr ^ obj->kt_key) + (4 * i);
319  sym->paddr = sym->vaddr - h->base;
320  sym->size = 4;
321  sym->ordinal = i;
322  rz_list_append(ret, sym);
323  } else {
324  free(sym);
325  }
326  }
327  return ret;
328 out_error:
329  rz_list_free(ret);
330  return NULL;
331 }
RZ_API void rz_bin_symbol_free(RzBinSymbol *sym)
Definition: bin.c:175
static const char * kt_name[]
Definition: bin_xbe.c:13
RZ_API const KEY_TYPE bool * found
Definition: ht_inc.h:130
static uint32_t const uint8_t uint32_t uint32_t limit
Definition: memcmplen.h:45
void(* RzListFree)(void *ptr)
Definition: rz_list.h:11
char * name
Definition: rz_bin.h:675
ut32 ordinal
Definition: rz_bin.h:692
#define XBE_MAX_THUNK
Definition: xbe.h:18

References addr, rz_bin_object_t::bin_obj, rz_bin_file_t::buf, found, free(), h, rz_bin_xbe_obj_t::header, i, rz_bin_xbe_obj_t::kt_key, kt_name, limit, rz_bin_symbol_t::name, NULL, rz_bin_file_t::o, xbe_section::offset, rz_bin_symbol_t::ordinal, rz_bin_symbol_t::paddr, read_xbe_section(), RZ_ARRAY_SIZE, rz_bin_symbol_free(), rz_buf_read_le32_at, rz_list_append(), rz_list_free(), rz_list_newf(), RZ_NEW0, rz_str_newf(), rz_bin_file_t::size, rz_bin_symbol_t::size, xbe_section::vaddr, rz_bin_symbol_t::vaddr, xbe_section::vsize, and XBE_MAX_THUNK.

Variable Documentation

◆ kt_name

const char* kt_name[]
static
Initial value:
= {
}

Definition at line 13 of file bin_xbe.c.

Referenced by symbols().

◆ rizin_plugin

RZ_API RzLibStruct rizin_plugin
Initial value:
= {
.type = RZ_LIB_TYPE_BIN,
}
RzBinPlugin rz_bin_plugin_xbe
Definition: bin_xbe.c:371
@ 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 389 of file bin_xbe.c.

◆ rz_bin_plugin_xbe

RzBinPlugin rz_bin_plugin_xbe
Initial value:
= {
.name = "xbe",
.desc = "Microsoft Xbox xbe format rz_bin plugin",
.license = "LGPL3",
.load_buffer = &load_buffer,
.destroy = &destroy,
.check_buffer = &check_buffer,
.baddr = &baddr,
.binsym = &binsym,
.entries = &entries,
.sections = &sections,
.symbols = &symbols,
.info = &info,
.libs = &libs,
}
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 RzList * symbols(RzBinFile *bf)
Definition: bin_xbe.c:262
static RzBinAddr * binsym(RzBinFile *bf, RzBinSpecialSymbol type)
Definition: bin_xbe.c:113
static RzList * libs(RzBinFile *bf)
Definition: bin_xbe.c:235
static void destroy(RzBinFile *bf)
Definition: bin_xbe.c:109
static bool check_buffer(RzBuffer *b)
Definition: bin_xbe.c:74
static RzBinInfo * info(RzBinFile *bf)
Definition: bin_xbe.c:333
static ut64 baddr(RzBinFile *bf)
Definition: bin_xbe.c:366
static RzList * entries(RzBinFile *bf)
Definition: bin_xbe.c:127
static bool load_buffer(RzBinFile *bf, RzBinObject *o, RzBuffer *buf, Sdb *sdb)
Definition: bin_xbe.c:82
static RzList * sections(RzBinFile *bf)
Definition: bin_xbe.c:148

Definition at line 371 of file bin_xbe.c.