Rizin
unix-like reverse engineering framework and cli tools
bin_cgc.c
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: 2009-2019 ret2libc <sirmy15@gmail.com>
2 // SPDX-FileCopyrightText: 2009-2019 pancake <pancake@nopcode.org>
3 // SPDX-License-Identifier: LGPL-3.0-only
4 
5 #define RZ_BIN_CGC 1
6 #include "bin_elf.inc"
7 
8 extern struct rz_bin_dbginfo_t rz_bin_dbginfo_elf;
9 
10 static bool check_buffer(RzBuffer *buf) {
11  ut8 tmp[SCGCMAG + 1];
12  int r = rz_buf_read_at(buf, 0, tmp, sizeof(tmp));
13  return r > SCGCMAG && !memcmp(tmp, CGCMAG, SCGCMAG) && tmp[4] != 2;
14 }
15 
16 static RzBuffer *create(RzBin *bin, const ut8 *code, int codelen, const ut8 *data, int datalen, RzBinArchOptions *opt) {
17  ut32 filesize, code_va, code_pa, phoff;
18  ut32 p_start, p_phoff, p_phdr;
19  ut32 p_ehdrsz, p_phdrsz;
20  ut16 ehdrsz, phdrsz;
21  ut32 p_vaddr, p_paddr, p_fs, p_fs2;
22  ut32 baddr = 0x8048000;
24 
25 #define B(x, y) rz_buf_append_bytes(buf, (const ut8 *)(x), y)
26 #define D(x) rz_buf_append_ut32(buf, x)
27 #define H(x) rz_buf_append_ut16(buf, x)
28 #define Z(x) rz_buf_append_nbytes(buf, x)
29 #define W(x, y, z) rz_buf_write_at(buf, x, (const ut8 *)(y), z)
30 #define WZ(x, y) \
31  p_tmp = rz_buf_size(buf); \
32  Z(x); \
33  W(p_tmp, y, strlen(y))
34 
35  B("\x7F"
36  "CGC"
37  "\x01\x01\x01\x43",
38  8);
39  Z(8);
40  H(2); // ET_EXEC
41  H(3); // e_machne = EM_I386
42 
43  D(1);
44  p_start = rz_buf_size(buf);
45  D(-1); // _start
46  p_phoff = rz_buf_size(buf);
47  D(-1); // phoff -- program headers offset
48  D(0); // shoff -- section headers offset
49  D(0); // flags
50  p_ehdrsz = rz_buf_size(buf);
51  H(-1); // ehdrsz
52  p_phdrsz = rz_buf_size(buf);
53  H(-1); // phdrsz
54  H(1);
55  H(0);
56  H(0);
57  H(0);
58  // phdr:
59  p_phdr = rz_buf_size(buf);
60  D(1);
61  D(0);
62  p_vaddr = rz_buf_size(buf);
63  D(-1); // vaddr = $$
64  p_paddr = rz_buf_size(buf);
65  D(-1); // paddr = $$
66  p_fs = rz_buf_size(buf);
67  D(-1); // filesize
68  p_fs2 = rz_buf_size(buf);
69  D(-1); // filesize
70  D(5); // flags
71  D(0x1000); // align
72 
73  ehdrsz = p_phdr;
74  phdrsz = rz_buf_size(buf) - p_phdr;
75  code_pa = rz_buf_size(buf);
76  code_va = code_pa + baddr;
77  phoff = 0x34; // p_phdr ;
78  filesize = code_pa + codelen + datalen;
79 
80  W(p_start, &code_va, 4);
81  W(p_phoff, &phoff, 4);
82  W(p_ehdrsz, &ehdrsz, 2);
83  W(p_phdrsz, &phdrsz, 2);
84 
85  code_va = baddr; // hack
86  W(p_vaddr, &code_va, 4);
87  code_pa = baddr; // hack
88  W(p_paddr, &code_pa, 4);
89 
90  W(p_fs, &filesize, 4);
91  W(p_fs2, &filesize, 4);
92 
93  B(code, codelen);
94 
95  if (data && datalen > 0) {
96  // ut32 data_section = buf->length;
97  RZ_LOG_WARN("DATA section not support for ELF yet\n");
98  B(data, datalen);
99  }
100  return buf;
101 }
102 
104  .name = "cgc",
105  .desc = "CGC format rz_bin plugin",
106  .license = "LGPL3",
107  .get_sdb = &get_sdb,
108  .load_buffer = load_buffer,
109  .check_buffer = &check_buffer,
110  .baddr = &baddr,
111  .boffset = &boffset,
112  .binsym = &binsym,
113  .entries = &entries,
114  .virtual_files = &virtual_files,
115  .maps = &maps,
116  .sections = &sections,
117  .symbols = &symbols,
118  .minstrlen = 4,
119  .imports = &imports,
120  .strings = &strings,
121  .info = &info,
122  .fields = &fields,
123  .size = &size,
124  .libs = &libs,
125  .relocs = &relocs,
126  .create = &create,
127  .file_type = get_file_type,
128  .regstate = regstate,
129  .section_type_to_string = &Elf_(rz_bin_elf_section_type_to_string),
130  .section_flag_to_rzlist = &Elf_(rz_bin_elf_section_flag_to_rzlist),
131  .destroy = destroy
132 };
const aarch64_field fields[]
Definition: aarch64-opc.c:205
static bool load_buffer(RzBinFile *bf, RzBinObject *obj, RzBuffer *buf, Sdb *sdb)
Definition: bin_any.c:50
static void destroy(RzBinFile *bf)
Definition: bin_any.c:54
static ut64 baddr(RzBinFile *bf)
Definition: bin_any.c:58
static Sdb * get_sdb(RzBinFile *bf)
Definition: bin_art.c:60
static RzList * maps(RzBinFile *bf)
Definition: bin_bf.c:116
static RzList * virtual_files(RzBinFile *bf)
Definition: bin_bflt.c:167
static RzBuffer * create(RzBin *bin, const ut8 *code, int codelen, const ut8 *data, int datalen, RzBinArchOptions *opt)
Definition: bin_cgc.c:16
struct rz_bin_dbginfo_t rz_bin_dbginfo_elf
#define W(x, y, z)
static bool check_buffer(RzBuffer *buf)
Definition: bin_cgc.c:10
#define H(x)
#define Z(x)
#define B(x, y)
RzBinPlugin rz_bin_plugin_cgc
Definition: bin_cgc.c:103
#define D(x)
static RzBinAddr * binsym(RzBinFile *bf, RzBinSpecialSymbol sym)
Definition: bin_coff.c:47
static RzList * libs(RzBinFile *bf)
Definition: bin_coff.c:379
static char * regstate(RzBinFile *bf)
Definition: bin_dmp64.c:252
RzList * entries(RzBinFile *bf)
Definition: bin_ne.c:98
RzBinInfo * info(RzBinFile *bf)
Definition: bin_ne.c:86
RzList * symbols(RzBinFile *bf)
Definition: bin_ne.c:102
RzList * imports(RzBinFile *bf)
Definition: bin_ne.c:106
RzList * sections(RzBinFile *bf)
Definition: bin_ne.c:110
RzList * relocs(RzBinFile *bf)
Definition: bin_ne.c:114
static ut64 boffset(RzBinFile *bf)
Definition: bin_ninds.c:39
#define NULL
Definition: cris-opc.c:27
#define r
Definition: crypto_rc6.c:12
uint16_t ut16
uint32_t ut32
RZ_OWN char *Elf_() rz_bin_elf_section_type_to_string(ut64 type)
Return a string representing the elf type.
Definition: elf_sections.c:422
RZ_OWN RzList *Elf_() rz_bin_elf_section_flag_to_rzlist(ut64 flag)
Return a list of string representing flag options.
Definition: elf_sections.c:298
#define CGCMAG
Definition: elf_specs.h:82
#define Elf_(name)
Definition: elf_specs.h:32
#define SCGCMAG
Definition: elf_specs.h:83
checking print the parsed form of the magic use in n conjunction with m to debug a new magic file n before installing it n output MIME type strings(--mime-type and\n" " --mime-encoding)\n") OPT('s'
voidpf void uLong size
Definition: ioapi.h:138
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
RZ_API RZ_OWN RzBuffer * rz_buf_new_with_bytes(RZ_NULLABLE RZ_BORROW const ut8 *bytes, ut64 len)
Creates a new buffer with a bytes array.
Definition: buf.c:465
RZ_API ut64 rz_buf_size(RZ_NONNULL RzBuffer *b)
Return the size of the buffer.
Definition: buf.c:1225
#define RZ_LOG_WARN(fmtstr,...)
Definition: rz_log.h:56
Definition: malloc.c:26
Definition: inftree9.h:24
char * name
Definition: rz_bin.h:509