Rizin
unix-like reverse engineering framework and cli tools
bin_luac.c
Go to the documentation of this file.
1 // SPDX-License-Identifier: LGPL-3.0-only
2 // SPDX-FileCopyrightText: 2021 Heersin <teablearcher@gmail.com>
3 
4 #include <rz_bin.h>
5 #include <rz_lib.h>
7 
8 #define GET_INTERNAL_BIN_INFO_OBJ(bf) ((LuacBinInfo *)(bf)->o->bin_obj)
9 
10 static bool check_buffer(RzBuffer *buff) {
11  if (rz_buf_size(buff) > 4) {
14  return !memcmp(buf, LUAC_MAGIC, LUAC_MAGIC_SIZE);
15  }
16  return false;
17 }
18 
19 static bool load_buffer(RzBinFile *bf, RzBinObject *obj, RzBuffer *buf, Sdb *sdb) {
20  ut8 MAJOR_MINOR_VERSION;
21  LuacBinInfo *bin_info_obj = NULL;
22  LuaProto *proto = NULL;
23  RzBinInfo *general_info = NULL;
24  st32 major;
25  st32 minor;
26 
27  rz_buf_read_at(buf, LUAC_VERSION_OFFSET, &MAJOR_MINOR_VERSION, sizeof(MAJOR_MINOR_VERSION)); /* 1-byte in fact */
28  if ((bin_info_obj = RZ_NEW(LuacBinInfo)) == NULL) {
29  return false;
30  }
31  major = (MAJOR_MINOR_VERSION & 0xF0) >> 4;
32  minor = (MAJOR_MINOR_VERSION & 0x0F);
33 
34  if (major != 5) {
35  RZ_LOG_ERROR("currently support lua 5.x only\n");
36  return false;
37  }
38 
39  switch (minor) {
40  case 4:
41  proto = lua_parse_body_54(buf, 0x20, bf->size);
42  general_info = lua_parse_header_54(bf, major, minor);
43  break;
44  case 3:
45  proto = lua_parse_body_53(buf, 0x22, bf->size);
46  general_info = lua_parse_header_53(bf, major, minor);
47  break;
48  default:
49  RZ_LOG_ERROR("lua 5.%c not support now\n", minor + '0');
50  return false;
51  }
52 
53  bin_info_obj = luac_build_info(proto);
54  if (bin_info_obj == NULL) {
55  lua_free_proto_entry(proto);
56  rz_bin_info_free(general_info);
57  return false;
58  }
59  bin_info_obj->general_info = general_info;
60  bin_info_obj->major = major;
61  bin_info_obj->minor = minor;
62 
63  lua_free_proto_entry(proto);
64  proto = NULL;
65 
66  obj->bin_obj = bin_info_obj;
67  return true;
68 }
69 
70 static RzBinInfo *info(RzBinFile *bf) {
71  if (!bf) {
72  return NULL;
73  }
74  LuacBinInfo *bin_info_obj = GET_INTERNAL_BIN_INFO_OBJ(bf);
75  if (!bin_info_obj) {
76  return NULL;
77  }
78 
79  return bin_info_obj->general_info;
80 }
81 
82 static RzList *sections(RzBinFile *bf) {
83  if (!bf) {
84  return NULL;
85  }
86  LuacBinInfo *bin_info_obj = GET_INTERNAL_BIN_INFO_OBJ(bf);
87  if (!bin_info_obj) {
88  return NULL;
89  }
90 
91  return rz_list_clone(bin_info_obj->section_list);
92 }
93 
94 static RzList *symbols(RzBinFile *bf) {
95  if (!bf) {
96  return NULL;
97  }
98  LuacBinInfo *bin_info_obj = GET_INTERNAL_BIN_INFO_OBJ(bf);
99  if (!bin_info_obj) {
100  return NULL;
101  }
102 
103  return bin_info_obj->symbol_list;
104 }
105 
106 static RzList *entries(RzBinFile *bf) {
107  if (!bf) {
108  return NULL;
109  }
110  LuacBinInfo *bin_info_obj = GET_INTERNAL_BIN_INFO_OBJ(bf);
111  if (!bin_info_obj) {
112  return NULL;
113  }
114 
115  return bin_info_obj->entry_list;
116 }
117 
118 static RzList *strings(RzBinFile *bf) {
119  if (!bf) {
120  return NULL;
121  }
122  LuacBinInfo *bin_info_obj = GET_INTERNAL_BIN_INFO_OBJ(bf);
123  if (!bin_info_obj) {
124  return NULL;
125  }
126 
127  return bin_info_obj->string_list;
128 }
129 
131  .name = "luac",
132  .desc = "LUA Compiled File",
133  .license = "LGPL3",
134  .get_sdb = NULL,
135  .load_buffer = &load_buffer,
136  .check_buffer = &check_buffer,
137  .baddr = NULL,
138  .entries = &entries,
140  .sections = &sections,
141  .symbols = &symbols,
142  .info = &info,
143  .strings = &strings,
144 };
145 
146 #ifndef RZ_PLUGIN_INCORE
149  .data = &rz_bin_plugin_luac,
151 };
152 #endif
RZ_API void rz_bin_info_free(RzBinInfo *rb)
Definition: bin.c:97
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 *buf, Sdb *sdb)
Definition: bin_luac.c:19
static RzList * symbols(RzBinFile *bf)
Definition: bin_luac.c:94
RzBinPlugin rz_bin_plugin_luac
Definition: bin_luac.c:130
static RzList * strings(RzBinFile *bf)
Definition: bin_luac.c:118
RZ_API RzLibStruct rizin_plugin
Definition: bin_luac.c:147
static RzBinInfo * info(RzBinFile *bf)
Definition: bin_luac.c:70
static bool check_buffer(RzBuffer *buff)
Definition: bin_luac.c:10
static RzList * entries(RzBinFile *bf)
Definition: bin_luac.c:106
static RzList * sections(RzBinFile *bf)
Definition: bin_luac.c:82
#define GET_INTERNAL_BIN_INFO_OBJ(bf)
Definition: bin_luac.c:8
#define RZ_API
#define NULL
Definition: cris-opc.c:27
#define minor(dev)
Definition: fsmagic.c:57
#define major(dev)
Definition: fsmagic.c:56
voidpf void * buf
Definition: ioapi.h:138
uint8_t ut8
Definition: lh5801.h:11
RZ_API RZ_OWN RzList * rz_list_clone(RZ_NONNULL const RzList *list)
Shallow copies of the list (but doesn't free its elements)
Definition: list.c:496
LuacBinInfo * luac_build_info(LuaProto *proto)
Definition: luac_bin.c:114
void lua_free_proto_entry(LuaProto *proto)
Definition: luac_common.c:119
RzBinInfo * lua_parse_header_54(RzBinFile *bf, st32 major, st32 minor)
Definition: parse_54.c:484
RzBinInfo * lua_parse_header_53(RzBinFile *bf, st32 major, st32 minor)
Definition: parse_53.c:483
#define LUAC_MAGIC_OFFSET
Definition: luac_common.h:16
LuaProto * lua_parse_body_54(RzBuffer *buffer, ut64 offset, ut64 data_size)
Definition: parse_54.c:410
#define LUAC_VERSION_OFFSET
Definition: luac_common.h:18
#define LUAC_MAGIC_SIZE
Definition: luac_common.h:17
#define LUAC_MAGIC
Definition: luac_common.h:21
LuaProto * lua_parse_body_53(RzBuffer *buffer, ut64 offset, ut64 data_size)
Definition: parse_53.c:407
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
#define RZ_LOG_ERROR(fmtstr,...)
Definition: rz_log.h:58
#define RZ_NEW(x)
Definition: rz_types.h:285
#define st32
Definition: rz_types_base.h:12
#define RZ_VERSION
Definition: rz_version.h:8
Store valuable info when parsing. Treat luac file body as a main function.
Definition: luac_common.h:43
RzBinInfo * general_info
general binary info from luac header
Definition: luac_common.h:166
RzList * entry_list
list of entries
Definition: luac_common.h:164
st32 minor
minor version
Definition: luac_common.h:161
RzList * section_list
list of sections
Definition: luac_common.h:162
st32 major
major version
Definition: luac_common.h:160
RzList * symbol_list
list of symbols
Definition: luac_common.h:163
RzList * string_list
list of strings
Definition: luac_common.h:165
XX curplugin == o->plugin.
Definition: rz_bin.h:298
void * bin_obj
Definition: rz_bin.h:293
char * name
Definition: rz_bin.h:509
char * version
Definition: rz_bin.h:512
Definition: sdb.h:63