Rizin
unix-like reverse engineering framework and cli tools
luac_common.h File Reference
#include <rz_bin.h>
#include <rz_lib.h>
#include <rz_list.h>

Go to the source code of this file.

Classes

struct  lua_proto_ex
 Store valuable info when parsing. Treat luac file body as a main function. More...
 
struct  lua_constant_entry
 Store constant type, data, and offset of this constant in luac file. More...
 
struct  lua_upvalue_entry
 Store upvalue attributes. More...
 
struct  lua_lineinfo_entry
 Store line info attributes. More...
 
struct  lua_abs_lineinfo_entry
 Store line info attributes. More...
 
struct  lua_local_var_entry
 Store local var names and other info. More...
 
struct  lua_dbg_upvalue_entry
 Store upvalue's debug info. More...
 
struct  luac_bin_info
 

Macros

#define LUAC_MAGIC_OFFSET   0x00
 
#define LUAC_MAGIC_SIZE   4
 
#define LUAC_VERSION_OFFSET   0x04
 
#define LUAC_VERSION_SIZE   1
 
#define LUAC_MAGIC   "\x1b\x4c\x75\x61"
 
#define makevariant(t, v)   ((t) | ((v) << 4))
 
#define LUA_TNIL   0
 
#define LUA_TBOOLEAN   1
 
#define LUA_TNUMBER   3
 
#define LUA_TSTRING   4
 
#define LUA_VNIL   makevariant(LUA_TNIL, 0)
 
#define LUA_VFALSE   makevariant(LUA_TBOOLEAN, 0)
 
#define LUA_VTRUE   makevariant(LUA_TBOOLEAN, 1)
 
#define LUA_VNUMINT   makevariant(LUA_TNUMBER, 0) /* integer numbers */
 
#define LUA_VNUMFLT   makevariant(LUA_TNUMBER, 1) /* float numbers */
 
#define LUA_VSHRSTR   makevariant(LUA_TSTRING, 0) /* short strings */
 
#define LUA_VLNGSTR   makevariant(LUA_TSTRING, 1) /* long strings */
 
#define lua_check_error_offset(offset)
 
#define lua_check_error_offset_proto(offset, proto)
 
#define lua_return_if_null(proto)
 

Typedefs

typedef ut32 LUA_INSTRUCTION
 
typedef struct lua_proto_ex LuaProtoHeavy
 
typedef LuaProtoHeavy LuaProto
 
typedef struct lua_constant_entry LuaConstEntry
 
typedef struct lua_upvalue_entry LuaUpvalueEntry
 
typedef struct LuaProto LuaProtoEntry
 
typedef struct lua_lineinfo_entry LuaLineinfoEntry
 
typedef struct lua_abs_lineinfo_entry LuaAbsLineinfoEntry
 
typedef struct lua_local_var_entry LuaLocalVarEntry
 
typedef struct lua_dbg_upvalue_entry LuaDbgUpvalueEntry
 
typedef struct luac_bin_info LuacBinInfo
 

Functions

LuaDbgUpvalueEntrylua_new_dbg_upvalue_entry ()
 
LuaLocalVarEntrylua_new_local_var_entry ()
 
LuaAbsLineinfoEntrylua_new_abs_lineinfo_entry ()
 
LuaLineinfoEntrylua_new_lineinfo_entry ()
 
LuaUpvalueEntrylua_new_upvalue_entry ()
 
LuaConstEntrylua_new_const_entry ()
 
LuaProtolua_new_proto_entry ()
 
void lua_free_dbg_upvalue_entry (LuaDbgUpvalueEntry *)
 
void lua_free_local_var_entry (LuaLocalVarEntry *)
 
void lua_free_const_entry (LuaConstEntry *)
 
void lua_free_proto_entry (LuaProto *)
 
void luac_add_section (RzList *section_list, char *name, ut64 offset, ut32 size, bool is_func)
 
void luac_add_symbol (RzList *symbol_list, char *name, ut64 offset, ut64 size, const char *type)
 
void luac_add_entry (RzList *entry_list, ut64 offset, int entry_type)
 
void luac_add_string (RzList *string_list, char *string, ut64 offset, ut64 size)
 
LuacBinInfoluac_build_info (LuaProto *proto)
 
void _luac_build_info (LuaProto *proto, LuacBinInfo *info)
 
RzBinInfolua_parse_header_54 (RzBinFile *bf, st32 major, st32 minor)
 
LuaProtolua_parse_body_54 (RzBuffer *buffer, ut64 offset, ut64 data_size)
 
RzBinInfolua_parse_header_53 (RzBinFile *bf, st32 major, st32 minor)
 
LuaProtolua_parse_body_53 (RzBuffer *buffer, ut64 offset, ut64 data_size)
 

Macro Definition Documentation

◆ lua_check_error_offset

#define lua_check_error_offset (   offset)
Value:
if ((offset) == 0) { \
return 0; \
}
voidpf uLong offset
Definition: ioapi.h:144

Definition at line 208 of file luac_common.h.

◆ lua_check_error_offset_proto

#define lua_check_error_offset_proto (   offset,
  proto 
)
Value:
if ((offset) == 0) { \
lua_free_proto_entry((proto)); \
return NULL; \
}
#define NULL
Definition: cris-opc.c:27

Definition at line 212 of file luac_common.h.

◆ lua_return_if_null

#define lua_return_if_null (   proto)
Value:
if ((proto) == NULL) { \
return 0; \
}

Definition at line 217 of file luac_common.h.

◆ LUA_TBOOLEAN

#define LUA_TBOOLEAN   1

Definition at line 27 of file luac_common.h.

◆ LUA_TNIL

#define LUA_TNIL   0

Definition at line 26 of file luac_common.h.

◆ LUA_TNUMBER

#define LUA_TNUMBER   3

Definition at line 28 of file luac_common.h.

◆ LUA_TSTRING

#define LUA_TSTRING   4

Definition at line 29 of file luac_common.h.

◆ LUA_VFALSE

#define LUA_VFALSE   makevariant(LUA_TBOOLEAN, 0)

Definition at line 32 of file luac_common.h.

◆ LUA_VLNGSTR

#define LUA_VLNGSTR   makevariant(LUA_TSTRING, 1) /* long strings */

Definition at line 37 of file luac_common.h.

◆ LUA_VNIL

#define LUA_VNIL   makevariant(LUA_TNIL, 0)

Definition at line 31 of file luac_common.h.

◆ LUA_VNUMFLT

#define LUA_VNUMFLT   makevariant(LUA_TNUMBER, 1) /* float numbers */

Definition at line 35 of file luac_common.h.

◆ LUA_VNUMINT

#define LUA_VNUMINT   makevariant(LUA_TNUMBER, 0) /* integer numbers */

Definition at line 34 of file luac_common.h.

◆ LUA_VSHRSTR

#define LUA_VSHRSTR   makevariant(LUA_TSTRING, 0) /* short strings */

Definition at line 36 of file luac_common.h.

◆ LUA_VTRUE

#define LUA_VTRUE   makevariant(LUA_TBOOLEAN, 1)

Definition at line 33 of file luac_common.h.

◆ LUAC_MAGIC

#define LUAC_MAGIC   "\x1b\x4c\x75\x61"

Definition at line 21 of file luac_common.h.

◆ LUAC_MAGIC_OFFSET

#define LUAC_MAGIC_OFFSET   0x00

Definition at line 16 of file luac_common.h.

◆ LUAC_MAGIC_SIZE

#define LUAC_MAGIC_SIZE   4

Definition at line 17 of file luac_common.h.

◆ LUAC_VERSION_OFFSET

#define LUAC_VERSION_OFFSET   0x04

Definition at line 18 of file luac_common.h.

◆ LUAC_VERSION_SIZE

#define LUAC_VERSION_SIZE   1

Definition at line 19 of file luac_common.h.

◆ makevariant

#define makevariant (   t,
  v 
)    ((t) | ((v) << 4))

Definition at line 24 of file luac_common.h.

Typedef Documentation

◆ LUA_INSTRUCTION

Definition at line 13 of file luac_common.h.

◆ LuaAbsLineinfoEntry

◆ LuacBinInfo

typedef struct luac_bin_info LuacBinInfo

◆ LuaConstEntry

◆ LuaDbgUpvalueEntry

◆ LuaLineinfoEntry

◆ LuaLocalVarEntry

◆ LuaProto

Definition at line 87 of file luac_common.h.

◆ LuaProtoEntry

typedef struct LuaProto LuaProtoEntry

Definition at line 87 of file luac_common.h.

◆ LuaProtoHeavy

typedef struct lua_proto_ex LuaProtoHeavy

◆ LuaUpvalueEntry

Function Documentation

◆ _luac_build_info()

void _luac_build_info ( LuaProto proto,
LuacBinInfo info 
)

Definition at line 225 of file luac_bin.c.

225  {
226  /* process proto header info */
227  char *section_name;
228  char *symbol_name;
229  char *proto_name;
230  RzListIter *iter;
231 
232  ut64 current_offset;
233  ut64 current_size;
234 
235  int i = 0; // iter
236 
237  // 0. check if stripped (proto name is lost)
238  if (proto->name_size == 0 || proto->proto_name == NULL) {
239  // replace name with current offset
240  proto_name = rz_str_newf("fcn.%08llx", proto->offset);
241  } else {
242  proto_name = rz_str_new((char *)proto->proto_name);
243  }
244 
245  // 1.1 set section name as function_name.header
246  current_offset = proto->offset;
247  current_size = proto->size;
248  section_name = rz_str_newf("%s.header", proto_name);
249  luac_add_section(info->section_list, section_name, current_offset, current_size, false);
250  RZ_FREE(section_name);
251 
252  // 1.2 set section name as function_name.code
253  current_offset = proto->code_offset;
254  current_size = proto->code_size;
255  section_name = rz_str_newf("%s.code", proto_name);
256  luac_add_section(info->section_list, section_name, current_offset, current_size, true);
257  RZ_FREE(section_name);
258 
259  // 1.3 set const section
260  current_offset = proto->const_offset;
261  current_size = proto->const_size;
262  section_name = rz_str_newf("%s.const", proto_name);
263  luac_add_section(info->section_list, section_name, current_offset, current_size, false);
264  RZ_FREE(section_name);
265 
266  // 1.4 upvalue section
267  current_offset = proto->upvalue_offset;
268  current_size = proto->upvalue_size;
269  section_name = rz_str_newf("%s.upvalues", proto_name);
270  luac_add_section(info->section_list, section_name, current_offset, current_size, false);
271  RZ_FREE(section_name);
272 
273  // 1.5 inner protos section
274  current_offset = proto->inner_proto_offset;
275  current_size = proto->inner_proto_size;
276  section_name = rz_str_newf("%s.protos", proto_name);
277  luac_add_section(info->section_list, section_name, current_offset, current_size, false);
278  RZ_FREE(section_name);
279 
280  // 1.6 debug section
281  current_offset = proto->debug_offset;
282  current_size = proto->debug_size;
283  section_name = rz_str_newf("%s.debug", proto_name);
284  luac_add_section(info->section_list, section_name, current_offset, current_size, false);
285  RZ_FREE(section_name);
286 
287  // 2.1 parse local var info
288  LuaLocalVarEntry *local_var_entry;
289  rz_list_foreach (proto->local_var_info_entries, iter, local_var_entry) {
291  info->string_list,
292  (char *)local_var_entry->varname,
293  local_var_entry->offset,
294  local_var_entry->varname_len);
295  }
296 
297  // 2.2 parse debug_upvalues
298  char **upvalue_names;
299  int real_upvalue_cnt;
300  LuaDbgUpvalueEntry *debug_upv_entry;
301  real_upvalue_cnt = rz_list_length(proto->upvalue_entries);
302  upvalue_names = RZ_NEWS0(char *, real_upvalue_cnt);
303  if (!upvalue_names) {
304  return;
305  }
306  rz_list_foreach (proto->dbg_upvalue_entries, iter, debug_upv_entry) {
307  upvalue_names[i] = (char *)debug_upv_entry->upvalue_name;
309  info->string_list,
310  upvalue_names[i],
311  debug_upv_entry->offset,
312  debug_upv_entry->name_len);
313  }
314 
315  // 3.1 construct constant symbols
316  LuaConstEntry *const_entry;
317  rz_list_foreach (proto->const_entries, iter, const_entry) {
318  symbol_name = get_constant_symbol_name(proto_name, const_entry);
320  info->symbol_list,
321  symbol_name,
322  const_entry->offset,
323  const_entry->data_len,
324  get_tag_string(const_entry->tag));
325  if (const_entry->tag == LUA_VLNGSTR || const_entry->tag == LUA_VSHRSTR) {
327  info->string_list,
328  (char *)const_entry->data,
329  const_entry->offset,
330  const_entry->data_len);
331  }
332  RZ_FREE(symbol_name);
333  }
334 
335  // 3.2 construct upvalue symbols
336  LuaUpvalueEntry *upvalue_entry;
337  i = 0;
338  rz_list_foreach (proto->upvalue_entries, iter, upvalue_entry) {
339  symbol_name = get_upvalue_symbol_name(proto_name, upvalue_entry, upvalue_names[i++]);
341  info->symbol_list,
342  symbol_name,
343  upvalue_entry->offset,
344  3,
345  "UPVALUE");
346  RZ_FREE(symbol_name);
347  }
348 
349  // 4. parse sub proto
350  LuaProto *sub_proto;
351  rz_list_foreach (proto->proto_entries, iter, sub_proto) {
352  _luac_build_info(sub_proto, info);
353  }
354 
355  RZ_FREE(proto_name);
356 }
lzma_index ** i
Definition: index.h:629
RzBinInfo * info(RzBinFile *bf)
Definition: bin_ne.c:86
RZ_API ut32 rz_list_length(RZ_NONNULL const RzList *list)
Returns the length of the list.
Definition: list.c:109
void luac_add_symbol(RzList *symbol_list, char *name, ut64 offset, ut64 size, const char *type)
Definition: luac_bin.c:30
void luac_add_string(RzList *string_list, char *string, ut64 offset, ut64 size)
Definition: luac_bin.c:57
void _luac_build_info(LuaProto *proto, LuacBinInfo *info)
Definition: luac_bin.c:225
static const char * get_tag_string(ut8 tag)
Definition: luac_bin.c:147
static char * get_constant_symbol_name(char *proto_name, LuaConstEntry *entry)
Definition: luac_bin.c:166
void luac_add_section(RzList *section_list, char *name, ut64 offset, ut32 size, bool is_func)
Definition: luac_bin.c:6
static char * get_upvalue_symbol_name(char *proto_name, LuaUpvalueEntry *entry, char *debug_name)
Definition: luac_bin.c:216
#define LUA_VLNGSTR
Definition: luac_common.h:37
#define LUA_VSHRSTR
Definition: luac_common.h:36
RZ_API char * rz_str_newf(const char *fmt,...) RZ_PRINTF_CHECK(1
RZ_API char * rz_str_new(const char *str)
Definition: str.c:865
#define RZ_NEWS0(x, y)
Definition: rz_types.h:282
#define RZ_FREE(x)
Definition: rz_types.h:369
Store constant type, data, and offset of this constant in luac file.
Definition: luac_common.h:93
ut64 offset
addr of this constant
Definition: luac_common.h:97
int data_len
len of data
Definition: luac_common.h:96
void * data
can be Number/Integer/String
Definition: luac_common.h:95
ut8 tag
type of this constant, see LUA_V* macros in luac_common.h
Definition: luac_common.h:94
Store upvalue's debug info.
Definition: luac_common.h:149
int name_len
length of name
Definition: luac_common.h:151
ut8 * upvalue_name
upvalue name
Definition: luac_common.h:150
ut64 offset
Definition: luac_common.h:152
Store local var names and other info.
Definition: luac_common.h:137
int varname_len
length of name
Definition: luac_common.h:139
ut64 offset
offset of this entry
Definition: luac_common.h:142
ut8 * varname
name of this variable
Definition: luac_common.h:138
Store valuable info when parsing. Treat luac file body as a main function.
Definition: luac_common.h:43
int name_size
size of proto name
Definition: luac_common.h:48
RzList * dbg_upvalue_entries
A list to store upvalue names.
Definition: luac_common.h:83
RzList * upvalue_entries
A list to store upvalue entries.
Definition: luac_common.h:68
ut64 size
current proto size
Definition: luac_common.h:45
ut64 inner_proto_offset
sub proto section offset
Definition: luac_common.h:74
ut64 const_offset
const section offset
Definition: luac_common.h:64
ut64 upvalue_size
upvalue section size
Definition: luac_common.h:70
RzList * local_var_info_entries
A list to store local var entries.
Definition: luac_common.h:82
RzList * const_entries
A list to store constant entries.
Definition: luac_common.h:63
ut8 * proto_name
current proto name
Definition: luac_common.h:47
ut64 code_offset
code section offset
Definition: luac_common.h:58
RzList * proto_entries
A list to store sub proto entries.
Definition: luac_common.h:73
ut64 upvalue_offset
upvalue section offset
Definition: luac_common.h:69
ut64 offset
proto offset in bytes
Definition: luac_common.h:44
ut64 debug_size
debug section size
Definition: luac_common.h:79
ut64 debug_offset
debug section offset
Definition: luac_common.h:78
ut64 const_size
const section size
Definition: luac_common.h:65
ut64 inner_proto_size
sub proto section size
Definition: luac_common.h:75
ut64 code_size
code section size
Definition: luac_common.h:59
Store upvalue attributes.
Definition: luac_common.h:104
ut64 offset
offset of this upvalue
Definition: luac_common.h:109
ut64(WINAPI *w32_GetEnabledXStateFeatures)()

References lua_proto_ex::code_offset, lua_proto_ex::code_size, lua_proto_ex::const_entries, lua_proto_ex::const_offset, lua_proto_ex::const_size, lua_constant_entry::data, lua_constant_entry::data_len, lua_proto_ex::dbg_upvalue_entries, lua_proto_ex::debug_offset, lua_proto_ex::debug_size, get_constant_symbol_name(), get_tag_string(), get_upvalue_symbol_name(), i, info(), lua_proto_ex::inner_proto_offset, lua_proto_ex::inner_proto_size, lua_proto_ex::local_var_info_entries, LUA_VLNGSTR, LUA_VSHRSTR, luac_add_section(), luac_add_string(), luac_add_symbol(), lua_dbg_upvalue_entry::name_len, lua_proto_ex::name_size, NULL, lua_proto_ex::offset, lua_constant_entry::offset, lua_upvalue_entry::offset, lua_local_var_entry::offset, lua_dbg_upvalue_entry::offset, lua_proto_ex::proto_entries, lua_proto_ex::proto_name, RZ_FREE, rz_list_length(), RZ_NEWS0, rz_str_new(), rz_str_newf(), lua_proto_ex::size, lua_constant_entry::tag, lua_proto_ex::upvalue_entries, lua_dbg_upvalue_entry::upvalue_name, lua_proto_ex::upvalue_offset, lua_proto_ex::upvalue_size, ut64(), lua_local_var_entry::varname, and lua_local_var_entry::varname_len.

Referenced by luac_build_info().

◆ lua_free_const_entry()

void lua_free_const_entry ( LuaConstEntry entry)

Definition at line 111 of file luac_common.c.

111  {
112  if (!entry) {
113  return;
114  }
115  free(entry->data);
116  free(entry);
117 }
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
Definition: zipcmp.c:77

References free().

Referenced by lua_new_proto_entry().

◆ lua_free_dbg_upvalue_entry()

void lua_free_dbg_upvalue_entry ( LuaDbgUpvalueEntry entry)

Definition at line 94 of file luac_common.c.

94  {
95  if (!entry) {
96  return;
97  }
98  free(entry->upvalue_name);
99  // leave entry to rz_list_free
100  free(entry);
101 }

References free().

Referenced by lua_new_proto_entry().

◆ lua_free_local_var_entry()

void lua_free_local_var_entry ( LuaLocalVarEntry entry)

Definition at line 103 of file luac_common.c.

103  {
104  if (!entry) {
105  return;
106  }
107  free(entry->varname);
108  free(entry);
109 }

References free().

Referenced by lua_new_proto_entry().

◆ lua_free_proto_entry()

void lua_free_proto_entry ( LuaProto proto)

Definition at line 119 of file luac_common.c.

119  {
120  if (!proto) {
121  return;
122  }
123 
124  /* free constants entries */
125  rz_list_free(proto->const_entries);
126 
127  /* free upvalue entries */
129 
130  /* free debug */
135 
136  /* recursively free protos */
137  rz_list_free(proto->proto_entries);
138 
139  free(proto->proto_name);
140  free(proto);
141 }
RZ_API void rz_list_free(RZ_NONNULL RzList *list)
Empties the list and frees the list pointer.
Definition: list.c:137
RzList * abs_line_info_entries
A list to store absolutely line info entries.
Definition: luac_common.h:81
RzList * line_info_entries
A list to store line info entries.
Definition: luac_common.h:80

References lua_proto_ex::abs_line_info_entries, lua_proto_ex::const_entries, lua_proto_ex::dbg_upvalue_entries, free(), lua_proto_ex::line_info_entries, lua_proto_ex::local_var_info_entries, lua_proto_ex::proto_entries, lua_proto_ex::proto_name, rz_list_free(), and lua_proto_ex::upvalue_entries.

Referenced by load_buffer(), lua_new_proto_entry(), lua_parse_body_53(), and lua_parse_body_54().

◆ lua_new_abs_lineinfo_entry()

LuaAbsLineinfoEntry* lua_new_abs_lineinfo_entry ( )

Definition at line 18 of file luac_common.c.

18  {
20  return entry;
21 }
#define RZ_NEW0(x)
Definition: rz_types.h:284
Store line info attributes.
Definition: luac_common.h:127

References RZ_NEW0.

Referenced by lua_parse_debug().

◆ lua_new_const_entry()

LuaConstEntry* lua_new_const_entry ( )

Definition at line 33 of file luac_common.c.

33  {
35  return entry;
36 }

References RZ_NEW0.

Referenced by lua_parse_const_entry().

◆ lua_new_dbg_upvalue_entry()

LuaDbgUpvalueEntry* lua_new_dbg_upvalue_entry ( )

Definition at line 8 of file luac_common.c.

8  {
10  return entry;
11 }

References RZ_NEW0.

Referenced by lua_parse_debug().

◆ lua_new_lineinfo_entry()

LuaLineinfoEntry* lua_new_lineinfo_entry ( )

Definition at line 23 of file luac_common.c.

23  {
25  return entry;
26 }
Store line info attributes.
Definition: luac_common.h:118

References RZ_NEW0.

Referenced by lua_parse_debug().

◆ lua_new_local_var_entry()

LuaLocalVarEntry* lua_new_local_var_entry ( )

Definition at line 13 of file luac_common.c.

13  {
15  return entry;
16 }

References RZ_NEW0.

Referenced by lua_parse_debug().

◆ lua_new_proto_entry()

LuaProto* lua_new_proto_entry ( )

Definition at line 38 of file luac_common.c.

38  {
39  LuaProto *proto = RZ_NEW0(LuaProto);
40  if (!proto) {
41  RZ_LOG_ERROR("Cannot allocate LuaProto\n");
42  return NULL;
43  }
44 
46  if (!proto->const_entries) {
47  RZ_LOG_ERROR("Cannot allocate Const Entry List\n");
48  goto fail;
49  }
50 
52  if (!proto->upvalue_entries) {
53  RZ_LOG_ERROR("Cannot allocate Upvalue Entry List\n");
54  goto fail;
55  }
56 
58  if (!proto->proto_entries) {
59  RZ_LOG_ERROR("Cannot allocate Proto Entry List\n");
60  goto fail;
61  }
62 
64  if (!proto->line_info_entries) {
65  RZ_LOG_ERROR("Cannot allocate Debug Line Info\n");
66  goto fail;
67  }
68 
70  if (!proto->abs_line_info_entries) {
71  RZ_LOG_ERROR("Cannot allocate Abs Line Info\n");
72  goto fail;
73  }
74 
76  if (!proto->local_var_info_entries) {
77  RZ_LOG_ERROR("Cannot allocate Local Var\n");
78  goto fail;
79  }
80 
82  if (!proto->dbg_upvalue_entries) {
83  RZ_LOG_ERROR("Cannot allocate Debug Upvalues\n");
84  goto fail;
85  }
86 
87  return proto;
88 
89 fail:
90  lua_free_proto_entry(proto);
91  return NULL;
92 }
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
void lua_free_const_entry(LuaConstEntry *entry)
Definition: luac_common.c:111
void lua_free_local_var_entry(LuaLocalVarEntry *entry)
Definition: luac_common.c:103
void lua_free_dbg_upvalue_entry(LuaDbgUpvalueEntry *entry)
Definition: luac_common.c:94
void lua_free_proto_entry(LuaProto *proto)
Definition: luac_common.c:119
void(* RzListFree)(void *ptr)
Definition: rz_list.h:11
#define RZ_LOG_ERROR(fmtstr,...)
Definition: rz_log.h:58
#define fail(test)
Definition: tests.h:29

References lua_proto_ex::abs_line_info_entries, lua_proto_ex::const_entries, lua_proto_ex::dbg_upvalue_entries, fail, free(), lua_proto_ex::line_info_entries, lua_proto_ex::local_var_info_entries, lua_free_const_entry(), lua_free_dbg_upvalue_entry(), lua_free_local_var_entry(), lua_free_proto_entry(), NULL, lua_proto_ex::proto_entries, rz_list_newf(), RZ_LOG_ERROR, RZ_NEW0, and lua_proto_ex::upvalue_entries.

Referenced by lua_parse_body_53(), and lua_parse_body_54().

◆ lua_new_upvalue_entry()

LuaUpvalueEntry* lua_new_upvalue_entry ( )

Definition at line 28 of file luac_common.c.

28  {
30  return entry;
31 }

References RZ_NEW0.

Referenced by lua_parse_upvalue_entry().

◆ lua_parse_body_53()

LuaProto* lua_parse_body_53 ( RzBuffer buffer,
ut64  offset,
ut64  data_size 
)

Definition at line 407 of file parse_53.c.

407  {
408  ut64 offset;
409  ut64 delta_offset;
410 
411  LuaProto *ret_proto = lua_new_proto_entry();
412  if (!ret_proto) {
413  return NULL;
414  }
415 
416  // start parsing
417  offset = base_offset;
418 
419  // record offset of main proto
420  ret_proto->offset = offset;
421 
422  // parse proto name
423  delta_offset = lua_parse_name(ret_proto, buffer, offset, data_size);
424  lua_check_error_offset_proto(delta_offset, ret_proto);
425  offset += delta_offset;
426 
427  /* parse line defined info */
428  delta_offset = lua_parse_line_defined(ret_proto, buffer, offset, data_size);
429  lua_check_error_offset_proto(delta_offset, ret_proto);
430  offset += delta_offset;
431 
432  /* parse num params max_stack_size */
433  if (offset + 3 > data_size) {
434  lua_free_proto_entry(ret_proto);
435  return NULL;
436  }
437  if (!rz_buf_read8_at(buffer, offset + 0, &ret_proto->num_params) ||
438  !rz_buf_read8_at(buffer, offset + 1, &ret_proto->is_vararg) ||
439  !rz_buf_read8_at(buffer, offset + 2, &ret_proto->max_stack_size)) {
440  lua_free_proto_entry(ret_proto);
441  return NULL;
442  }
443  offset += 3;
444 
445  /* parse code */
446  ret_proto->code_offset = offset;
447  delta_offset = lua_parse_code(ret_proto, buffer, offset, data_size);
448  lua_check_error_offset_proto(delta_offset, ret_proto);
449  offset += delta_offset;
450 
451  /* parse constants */
452  ret_proto->const_offset = offset;
453  delta_offset = lua_parse_consts(ret_proto, buffer, offset, data_size);
454  lua_check_error_offset_proto(delta_offset, ret_proto);
455  offset += delta_offset;
456 
457  /* parse upvalues */
458  ret_proto->upvalue_offset = offset;
459  delta_offset = lua_parse_upvalues(ret_proto, buffer, offset, data_size);
460  lua_check_error_offset_proto(delta_offset, ret_proto);
461  offset += delta_offset;
462 
463  /* parse inner protos */
464  ret_proto->inner_proto_offset = offset;
465  delta_offset = lua_parse_protos(ret_proto, buffer, offset, data_size);
466  lua_check_error_offset_proto(delta_offset, ret_proto);
467  offset += delta_offset;
468 
469  /* specially handle recursive protos size */
470  ret_proto->inner_proto_size = offset - ret_proto->inner_proto_offset;
471 
472  /* parse debug */
473  ret_proto->debug_offset = offset;
474  delta_offset = lua_parse_debug(ret_proto, buffer, offset, data_size);
475  lua_check_error_offset_proto(delta_offset, ret_proto);
476  offset += delta_offset;
477 
478  ret_proto->size = offset - base_offset + 1;
479 
480  return ret_proto;
481 }
LuaProto * lua_new_proto_entry()
Definition: luac_common.c:38
#define lua_check_error_offset_proto(offset, proto)
Definition: luac_common.h:212
static ut64 lua_parse_code(LuaProto *proto, RzBuffer *buffer, ut64 offset, ut64 data_size)
Definition: parse_53.c:105
static ut64 lua_parse_name(LuaProto *proto, RzBuffer *buffer, ut64 offset, ut64 data_size)
Definition: parse_53.c:101
static ut64 lua_parse_debug(LuaProto *proto, RzBuffer *buffer, ut64 offset, ut64 data_size)
Definition: parse_53.c:295
static ut64 lua_parse_upvalues(LuaProto *proto, RzBuffer *buffer, ut64 offset, ut64 data_size)
Definition: parse_53.c:268
static ut64 lua_parse_line_defined(LuaProto *proto, RzBuffer *buffer, ut64 offset, ut64 data_size)
Definition: parse_53.c:33
static ut64 lua_parse_consts(LuaProto *proto, RzBuffer *buffer, ut64 offset, ut64 data_size)
Definition: parse_53.c:213
static ut64 lua_parse_protos(LuaProto *proto, RzBuffer *buffer, ut64 offset, ut64 data_size)
Definition: parse_53.c:378
RZ_API bool rz_buf_read8_at(RzBuffer *b, ut64 addr, RZ_NONNULL RZ_OUT ut8 *result)
Read a byte at the specified address in the buffer.
Definition: buf.c:876
Definition: buffer.h:15
ut8 is_vararg
is variable arg?
Definition: luac_common.h:54
ut8 num_params
number of parameters of this proto
Definition: luac_common.h:53
ut8 max_stack_size
max stack size
Definition: luac_common.h:55

References lua_proto_ex::code_offset, lua_proto_ex::const_offset, lua_proto_ex::debug_offset, lua_proto_ex::inner_proto_offset, lua_proto_ex::inner_proto_size, lua_proto_ex::is_vararg, lua_check_error_offset_proto, lua_free_proto_entry(), lua_new_proto_entry(), lua_parse_code(), lua_parse_consts(), lua_parse_debug(), lua_parse_line_defined(), lua_parse_name(), lua_parse_protos(), lua_parse_upvalues(), lua_proto_ex::max_stack_size, NULL, lua_proto_ex::num_params, lua_proto_ex::offset, rz_buf_read8_at(), lua_proto_ex::size, lua_proto_ex::upvalue_offset, and ut64().

Referenced by load_buffer(), and lua_parse_protos().

◆ lua_parse_body_54()

LuaProto* lua_parse_body_54 ( RzBuffer buffer,
ut64  offset,
ut64  data_size 
)

Definition at line 410 of file parse_54.c.

410  {
411  LuaProto *ret_proto; /* construted proto for return */
412  ut64 offset; /* record offset */
413  ut64 delta_offset;
415 
416  // start parsing
417  offset = base_offset;
418 
419  /* record offset of main proto */
420  ret_proto->offset = offset;
421 
422  /* parse proto name of main proto */
423  delta_offset = lua_parse_name(ret_proto, buffer, offset, data_size);
424  lua_check_error_offset_proto(delta_offset, ret_proto);
425  offset += delta_offset;
426 
427  /* parse line defined info */
428  delta_offset = lua_parse_line_defined(ret_proto, buffer, offset, data_size);
429  lua_check_error_offset_proto(delta_offset, ret_proto);
430  offset += delta_offset;
431 
432  /* parse num params max_stack_size */
433  if (offset + 3 > data_size) {
434  lua_free_proto_entry(ret_proto);
435  return NULL;
436  }
437 
438  if (!rz_buf_read8_at(buffer, offset + 0, &ret_proto->num_params) ||
439  !rz_buf_read8_at(buffer, offset + 1, &ret_proto->is_vararg) ||
440  !rz_buf_read8_at(buffer, offset + 2, &ret_proto->max_stack_size)) {
441  lua_free_proto_entry(ret_proto);
442  return NULL;
443  }
444  offset += 3;
445 
446  /* parse code */
447  ret_proto->code_offset = offset;
448  delta_offset = lua_parse_code(ret_proto, buffer, offset, data_size);
449  lua_check_error_offset_proto(delta_offset, ret_proto);
450  offset += delta_offset;
451 
452  /* parse constants */
453  ret_proto->const_offset = offset;
454  delta_offset = lua_parse_consts(ret_proto, buffer, offset, data_size);
455  lua_check_error_offset_proto(delta_offset, ret_proto);
456  offset += delta_offset;
457 
458  /* parse upvalues */
459  ret_proto->upvalue_offset = offset;
460  delta_offset = lua_parse_upvalues(ret_proto, buffer, offset, data_size);
461  lua_check_error_offset_proto(delta_offset, ret_proto);
462  offset += delta_offset;
463 
464  /* parse inner protos */
465  ret_proto->inner_proto_offset = offset;
466  delta_offset = lua_parse_protos(ret_proto, buffer, offset, data_size);
467  lua_check_error_offset_proto(delta_offset, ret_proto);
468  offset += delta_offset;
469 
470  /* specially handle recursive protos size */
471  ret_proto->inner_proto_size = offset - ret_proto->inner_proto_offset;
472 
473  /* parse debug */
474  ret_proto->debug_offset = offset;
475  delta_offset = lua_parse_debug(ret_proto, buffer, offset, data_size);
476  lua_check_error_offset_proto(delta_offset, ret_proto);
477  offset += delta_offset;
478 
479  ret_proto->size = offset - base_offset + 1;
480 
481  return ret_proto;
482 }
static ut64 lua_parse_code(LuaProto *proto, RzBuffer *buffer, ut64 offset, ut64 data_size)
Definition: parse_54.c:123
static ut64 lua_parse_name(LuaProto *proto, RzBuffer *buffer, ut64 offset, ut64 data_size)
Definition: parse_54.c:98
static ut64 lua_parse_debug(LuaProto *proto, RzBuffer *buffer, ut64 offset, ut64 data_size)
Definition: parse_54.c:286
static ut64 lua_parse_upvalues(LuaProto *proto, RzBuffer *buffer, ut64 offset, ut64 data_size)
Definition: parse_54.c:263
static ut64 lua_parse_line_defined(LuaProto *proto, RzBuffer *buffer, ut64 offset, ut64 data_size)
Definition: parse_54.c:102
static ut64 lua_parse_consts(LuaProto *proto, RzBuffer *buffer, ut64 offset, ut64 data_size)
Definition: parse_54.c:206
static ut64 lua_parse_protos(LuaProto *proto, RzBuffer *buffer, ut64 offset, ut64 data_size)
Definition: parse_54.c:385
#define rz_return_val_if_fail(expr, val)
Definition: rz_assert.h:108

References lua_proto_ex::code_offset, lua_proto_ex::const_offset, lua_proto_ex::debug_offset, lua_proto_ex::inner_proto_offset, lua_proto_ex::inner_proto_size, lua_proto_ex::is_vararg, lua_check_error_offset_proto, lua_free_proto_entry(), lua_new_proto_entry(), lua_parse_code(), lua_parse_consts(), lua_parse_debug(), lua_parse_line_defined(), lua_parse_name(), lua_parse_protos(), lua_parse_upvalues(), lua_proto_ex::max_stack_size, NULL, lua_proto_ex::num_params, lua_proto_ex::offset, rz_buf_read8_at(), rz_return_val_if_fail, lua_proto_ex::size, lua_proto_ex::upvalue_offset, and ut64().

Referenced by load_buffer(), and lua_parse_protos().

◆ lua_parse_header_53()

RzBinInfo* lua_parse_header_53 ( RzBinFile bf,
st32  major,
st32  minor 
)

Definition at line 483 of file parse_53.c.

483  {
484  RzBinInfo *ret = NULL;
485  RzBuffer *buffer;
486 
487  st64 reat = bf->size;
488  if (reat < LUAC_53_HDRSIZE) {
489  RZ_LOG_ERROR("Truncated header\n");
490  return NULL;
491  }
492  buffer = bf->buf;
493 
494  /* read header members from work buffer */
495  ut8 luac_format;
496  if (!rz_buf_read8_at(buffer, LUAC_53_FORMAT_OFFSET, &luac_format)) {
497  return NULL;
498  }
499  ut8 int_size;
500  if (!rz_buf_read8_at(buffer, LUAC_53_INT_SIZE_OFFSET, &int_size)) {
501  return NULL;
502  }
503  ut8 sizet_size;
504  if (!rz_buf_read8_at(buffer, LUAC_53_SIZET_SIZE_OFFSET, &sizet_size)) {
505  return NULL;
506  }
507  ut8 instruction_size;
508  if (!rz_buf_read8_at(buffer, LUAC_53_INSTRUCTION_SIZE_OFFSET, &instruction_size)) {
509  return NULL;
510  }
511  ut8 integer_size;
512  if (!rz_buf_read8_at(buffer, LUAC_53_INTEGER_SIZE_OFFSET, &integer_size)) {
513  return NULL;
514  }
515  ut8 number_size;
516  if (!rz_buf_read8_at(buffer, LUAC_53_NUMBER_SIZE_OFFSET, &number_size)) {
517  return NULL;
518  }
520  double number_valid = lua_load_number(buffer, LUAC_53_NUMBER_VALID_OFFSET);
521 
522  /* Common Ret */
523  if (!(ret = RZ_NEW0(RzBinInfo))) {
524  return NULL;
525  }
526  ret->file = rz_str_new(bf->file);
527  ret->type = rz_str_newf("Lua %c.%c compiled file", major + '0', minor + '0');
528  ret->bclass = rz_str_new("Lua compiled file");
529  ret->rclass = rz_str_new("luac");
530  ret->arch = rz_str_new("luac");
531  ret->machine = rz_str_newf("Lua %c.%c VM", major + '0', minor + '0');
532  ret->os = rz_str_newf("%c.%c", major + '0', minor + '0');
533  ret->cpu = rz_str_newf("%c.%c", major + '0', minor + '0');
534  ret->bits = 8;
535 
536  /* official format ? */
537  if (luac_format != LUAC_54_FORMAT) {
538  ret->compiler = rz_str_new("Unofficial Lua Compiler");
539  return ret;
540  }
541  ret->compiler = rz_str_new("Official Lua Compiler");
542 
543  /* Check Size */
544  // TODO : remove this check and process different compiler options
545  if ((instruction_size != sizeof(LUA_INSTRUCTION)) ||
546  (integer_size != sizeof(LUA_INTEGER)) ||
547  (number_size != sizeof(LUA_NUMBER)) ||
548  (int_size != sizeof(LUA_INT)) ||
549  (sizet_size != sizeof(size_t))) {
550  RZ_LOG_ERROR("Size definition does not match with the expected size\n");
551  return ret;
552  }
553 
554  /* Check endian */
555  if (integer_valid != LUAC_53_INT_VALIDATION) {
556  RZ_LOG_ERROR("Integer format does not match with the expected integer\n");
557  return ret;
558  } else if (number_valid != LUAC_53_NUMBER_VALIDATION) {
559  RZ_LOG_ERROR("Number format does not match with the expected number\n");
560  return ret;
561  }
562 
563  /* parse source file name */
564  char *src_file_name = NULL;
565  int name_len;
566  lua_parse_string(buffer, ((ut8 **)&(src_file_name)), &name_len, LUAC_FILENAME_OFFSET, bf->size);
567 
568  /* put source file info into GUID */
569  ret->guid = rz_str_new(src_file_name ? src_file_name : "stripped");
570  free(src_file_name);
571 
572  return ret;
573 }
struct buffer buffer
#define minor(dev)
Definition: fsmagic.c:57
#define major(dev)
Definition: fsmagic.c:56
uint8_t ut8
Definition: lh5801.h:11
ut32 LUA_INSTRUCTION
Definition: luac_common.h:13
ut64 LUA_INTEGER
Definition: luac_specs_53.h:11
#define LUAC_53_HDRSIZE
Definition: luac_specs_53.h:34
#define LUAC_53_INSTRUCTION_SIZE_OFFSET
Definition: luac_specs_53.h:27
#define LUAC_53_INT_SIZE_OFFSET
Definition: luac_specs_53.h:25
#define LUAC_53_NUMBER_VALIDATION
Definition: luac_specs_53.h:20
#define LUAC_54_FORMAT
Definition: luac_specs_53.h:17
double LUA_NUMBER
Definition: luac_specs_53.h:10
#define LUAC_FILENAME_OFFSET
Definition: luac_specs_53.h:37
#define LUAC_53_NUMBER_VALID_OFFSET
Definition: luac_specs_53.h:31
#define LUAC_53_NUMBER_SIZE_OFFSET
Definition: luac_specs_53.h:29
ut32 LUA_INT
Definition: luac_specs_53.h:12
#define LUAC_53_FORMAT_OFFSET
Definition: luac_specs_53.h:23
#define LUAC_53_INT_VALIDATION
Definition: luac_specs_53.h:19
#define LUAC_53_SIZET_SIZE_OFFSET
Definition: luac_specs_53.h:26
#define LUAC_53_INTEGER_VALID_OFFSET
Definition: luac_specs_53.h:30
#define LUAC_53_INTEGER_SIZE_OFFSET
Definition: luac_specs_53.h:28
static ut64 lua_parse_string(RzBuffer *buffer, ut8 **dest, int *str_len, ut64 offset, ut64 data_size)
Definition: parse_53.c:54
static double lua_load_number(RzBuffer *buffer, ut64 offset)
Definition: parse_53.c:21
static ut64 lua_load_integer(RzBuffer *buffer, ut64 offset)
Definition: parse_53.c:15
#define st64
Definition: rz_types_base.h:10
char * file
Definition: rz_bin.h:299
RzBuffer * buf
Definition: rz_bin.h:303
char * type
Definition: rz_bin.h:211
char * os
Definition: rz_bin.h:219
char * machine
Definition: rz_bin.h:216
char * bclass
Definition: rz_bin.h:212
char * guid
Definition: rz_bin.h:222
char * file
Definition: rz_bin.h:210
char * cpu
Definition: rz_bin.h:215
char * rclass
Definition: rz_bin.h:213
char * arch
Definition: rz_bin.h:214
char * compiler
Definition: rz_bin.h:244

References rz_bin_info_t::arch, rz_bin_info_t::bclass, rz_bin_info_t::bits, rz_bin_file_t::buf, rz_bin_info_t::compiler, rz_bin_info_t::cpu, rz_bin_info_t::file, rz_bin_file_t::file, free(), rz_bin_info_t::guid, lua_load_integer(), lua_load_number(), lua_parse_string(), LUAC_53_FORMAT_OFFSET, LUAC_53_HDRSIZE, LUAC_53_INSTRUCTION_SIZE_OFFSET, LUAC_53_INT_SIZE_OFFSET, LUAC_53_INT_VALIDATION, LUAC_53_INTEGER_SIZE_OFFSET, LUAC_53_INTEGER_VALID_OFFSET, LUAC_53_NUMBER_SIZE_OFFSET, LUAC_53_NUMBER_VALID_OFFSET, LUAC_53_NUMBER_VALIDATION, LUAC_53_SIZET_SIZE_OFFSET, LUAC_54_FORMAT, LUAC_FILENAME_OFFSET, rz_bin_info_t::machine, major, minor, NULL, rz_bin_info_t::os, rz_bin_info_t::rclass, rz_buf_read8_at(), RZ_LOG_ERROR, RZ_NEW0, rz_str_new(), rz_str_newf(), rz_bin_file_t::size, st64, rz_bin_info_t::type, and ut64().

Referenced by load_buffer().

◆ lua_parse_header_54()

RzBinInfo* lua_parse_header_54 ( RzBinFile bf,
st32  major,
st32  minor 
)

Definition at line 484 of file parse_54.c.

484  {
485  RzBinInfo *ret = NULL;
486  RzBuffer *buffer;
487 
488  st64 reat = bf->size;
489  if (reat < LUAC_54_HDRSIZE) {
490  RZ_LOG_ERROR("Truncated Header\n");
491  return NULL;
492  }
493  buffer = bf->buf;
494 
495  /* read header members from work buffer */
496  ut8 luac_format;
497  if (!rz_buf_read8_at(buffer, LUAC_54_FORMAT_OFFSET, &luac_format)) {
498  return NULL;
499  }
500 
501  ut8 instruction_size;
502  if (!rz_buf_read8_at(buffer, LUAC_54_INSTRUCTION_SIZE_OFFSET, &instruction_size)) {
503  return NULL;
504  }
505 
506  ut8 integer_size;
507  if (!rz_buf_read8_at(buffer, LUAC_54_INTEGER_SIZE_OFFSET, &integer_size)) {
508  return NULL;
509  }
510 
511  ut8 number_size;
512  if (!rz_buf_read8_at(buffer, LUAC_54_NUMBER_SIZE_OFFSET, &number_size)) {
513  return NULL;
514  }
515 
517  double number_valid = lua_load_number(buffer, LUAC_54_NUMBER_VALID_OFFSET);
518 
519  /* Common Ret */
520  if (!(ret = RZ_NEW0(RzBinInfo))) {
521  return NULL;
522  }
523  ret->file = rz_str_new(bf->file);
524  ret->type = rz_str_newf("Lua %c.%c compiled file", major + '0', minor + '0');
525  ret->bclass = rz_str_new("Lua compiled file");
526  ret->rclass = rz_str_new("luac");
527  ret->arch = rz_str_new("luac");
528  ret->machine = rz_str_newf("Lua %c.%c VM", major + '0', minor + '0');
529  ret->os = rz_str_newf("%c.%c", major + '0', minor + '0');
530  ret->cpu = rz_str_newf("%c.%c", major + '0', minor + '0');
531  ret->bits = 8;
532 
533  /* official format ? */
534  if (luac_format != LUAC_54_FORMAT) {
535  ret->compiler = rz_str_new("Unofficial Lua Compiler");
536  return ret;
537  }
538  ret->compiler = rz_str_new("Official Lua Compiler");
539 
540  /* Check Size */
541  if ((instruction_size != sizeof(LUA_INSTRUCTION)) ||
542  (integer_size != sizeof(LUA_INTEGER)) ||
543  (number_size != sizeof(LUA_NUMBER))) {
544  RZ_LOG_ERROR("Size definition does not match with the expected size\n");
545  return ret;
546  }
547 
548  /* Check endian */
549  if (int_valid != LUAC_54_INT_VALIDATION) {
550  RZ_LOG_ERROR("Integer format does not match with the expected integer\n");
551  return ret;
552  } else if (number_valid != LUAC_54_NUMBER_VALIDATION) {
553  RZ_LOG_ERROR("Number format does not match with the expected number\n");
554  return ret;
555  }
556 
557  /* parse source file name */
558  char *src_file_name = NULL;
559  int name_len;
560  lua_parse_string(buffer, ((ut8 **)&(src_file_name)), &name_len, LUAC_FILENAME_OFFSET, bf->size);
561 
562  /* put source file info into GUID */
563  ret->guid = rz_str_new(src_file_name ? src_file_name : "stripped");
564  free(src_file_name);
565 
566  return ret;
567 }
#define LUAC_54_INTEGER_SIZE_OFFSET
Definition: luac_specs_54.h:25
#define LUAC_54_HDRSIZE
Definition: luac_specs_54.h:47
#define LUAC_54_NUMBER_SIZE_OFFSET
Definition: luac_specs_54.h:26
#define LUAC_54_INT_VALIDATION
Definition: luac_specs_54.h:44
#define LUAC_54_FORMAT_OFFSET
Definition: luac_specs_54.h:22
#define LUAC_54_NUMBER_VALIDATION
Definition: luac_specs_54.h:45
#define LUAC_54_INTEGER_VALID_OFFSET
Definition: luac_specs_54.h:27
#define LUAC_54_NUMBER_VALID_OFFSET
Definition: luac_specs_54.h:28
#define LUAC_54_INSTRUCTION_SIZE_OFFSET
Definition: luac_specs_54.h:24
static ut64 lua_parse_string(RzBuffer *buffer, ut8 **dest, int *str_len, ut64 offset, ut64 data_size)
Definition: parse_54.c:61
static double lua_load_number(RzBuffer *buffer, ut64 offset)
Definition: parse_54.c:20
static ut64 lua_load_integer(RzBuffer *buffer, ut64 offset)
Definition: parse_54.c:14

References rz_bin_info_t::arch, rz_bin_info_t::bclass, rz_bin_info_t::bits, rz_bin_file_t::buf, rz_bin_info_t::compiler, rz_bin_info_t::cpu, rz_bin_info_t::file, rz_bin_file_t::file, free(), rz_bin_info_t::guid, lua_load_integer(), lua_load_number(), lua_parse_string(), LUAC_54_FORMAT, LUAC_54_FORMAT_OFFSET, LUAC_54_HDRSIZE, LUAC_54_INSTRUCTION_SIZE_OFFSET, LUAC_54_INT_VALIDATION, LUAC_54_INTEGER_SIZE_OFFSET, LUAC_54_INTEGER_VALID_OFFSET, LUAC_54_NUMBER_SIZE_OFFSET, LUAC_54_NUMBER_VALID_OFFSET, LUAC_54_NUMBER_VALIDATION, LUAC_FILENAME_OFFSET, rz_bin_info_t::machine, major, minor, NULL, rz_bin_info_t::os, rz_bin_info_t::rclass, rz_buf_read8_at(), RZ_LOG_ERROR, RZ_NEW0, rz_str_new(), rz_str_newf(), rz_bin_file_t::size, st64, rz_bin_info_t::type, and ut64().

Referenced by load_buffer().

◆ luac_add_entry()

void luac_add_entry ( RzList entry_list,
ut64  offset,
int  entry_type 
)

Definition at line 44 of file luac_bin.c.

44  {
46  if (!entry) {
47  return;
48  }
49 
50  entry->vaddr = offset;
51  entry->paddr = offset;
52  entry->type = entry_type;
53 
54  rz_list_append(entry_list, entry);
55 }
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

References rz_list_append(), and RZ_NEW0.

Referenced by luac_build_info().

◆ luac_add_section()

void luac_add_section ( RzList section_list,
char *  name,
ut64  offset,
ut32  size,
bool  is_func 
)

Definition at line 6 of file luac_bin.c.

6  {
8  if (!bin_sec) {
9  return;
10  }
11 
12  bin_sec->name = rz_str_new(name);
13  bin_sec->vaddr = bin_sec->paddr = offset;
14  bin_sec->size = bin_sec->vsize = size;
15  bin_sec->is_data = false;
16  bin_sec->bits = is_func ? sizeof(LUA_INSTRUCTION) * 8 : 8;
17  // bin_sec->has_strings = !is_func;
18  bin_sec->has_strings = false;
19  bin_sec->arch = rz_str_new("luac");
20 
21  if (is_func) {
22  bin_sec->perm = RZ_PERM_R | RZ_PERM_X;
23  } else {
24  bin_sec->perm = RZ_PERM_R;
25  }
26 
27  rz_list_append(section_list, bin_sec);
28 }
voidpf void uLong size
Definition: ioapi.h:138
#define RZ_PERM_R
Definition: rz_types.h:93
#define RZ_PERM_X
Definition: rz_types.h:95
Definition: z80asm.h:102
const char * arch
Definition: rz_bin.h:627
char * name
Definition: rz_bin.h:619
bool has_strings
Definition: rz_bin.h:632

References rz_bin_section_t::arch, rz_bin_section_t::bits, rz_bin_section_t::has_strings, rz_bin_section_t::is_data, rz_bin_section_t::name, rz_bin_section_t::paddr, rz_bin_section_t::perm, rz_list_append(), RZ_NEW0, RZ_PERM_R, RZ_PERM_X, rz_str_new(), rz_bin_section_t::size, rz_bin_section_t::vaddr, and rz_bin_section_t::vsize.

Referenced by _luac_build_info().

◆ luac_add_string()

void luac_add_string ( RzList string_list,
char *  string,
ut64  offset,
ut64  size 
)

Definition at line 57 of file luac_bin.c.

57  {
58  RzBinString *bin_string = RZ_NEW0(RzBinString);
59  if (!bin_string) {
60  return;
61  }
62 
63  bin_string->paddr = offset;
64  bin_string->vaddr = offset;
65  bin_string->size = size;
66  bin_string->length = size;
67  bin_string->string = rz_str_new(string);
68  bin_string->type = RZ_STRING_ENC_UTF8;
69 
70  rz_list_append(string_list, bin_string);
71 }
@ RZ_STRING_ENC_UTF8
Definition: rz_str.h:21
char * string
Definition: rz_bin.h:752

References rz_bin_string_t::length, rz_bin_string_t::paddr, rz_list_append(), RZ_NEW0, rz_str_new(), RZ_STRING_ENC_UTF8, rz_bin_string_t::size, rz_bin_string_t::string, rz_bin_string_t::type, and rz_bin_string_t::vaddr.

Referenced by _luac_build_info().

◆ luac_add_symbol()

void luac_add_symbol ( RzList symbol_list,
char *  name,
ut64  offset,
ut64  size,
const char *  type 
)

Definition at line 30 of file luac_bin.c.

30  {
31  RzBinSymbol *bin_sym = RZ_NEW0(RzBinSymbol);
32  if (!bin_sym) {
33  return;
34  }
35 
36  bin_sym->name = rz_str_new(name);
37  bin_sym->vaddr = bin_sym->paddr = offset;
38  bin_sym->size = size;
39  bin_sym->type = type;
40 
41  rz_list_append(symbol_list, bin_sym);
42 }
int type
Definition: mipsasm.c:17
const char * type
Definition: rz_bin.h:682
char * name
Definition: rz_bin.h:675

References rz_bin_symbol_t::name, rz_bin_symbol_t::paddr, rz_list_append(), RZ_NEW0, rz_str_new(), rz_bin_symbol_t::size, type, rz_bin_symbol_t::type, and rz_bin_symbol_t::vaddr.

Referenced by _luac_build_info().

◆ luac_build_info()

LuacBinInfo* luac_build_info ( LuaProto proto)

Definition at line 114 of file luac_bin.c.

114  {
115  if (!proto) {
116  RZ_LOG_ERROR("Invalid luac file\n");
117  return NULL;
118  }
119 
121  if (!ret) {
122  return NULL;
123  }
124 
129 
130  if (!(ret->entry_list && ret->symbol_list && ret->section_list && ret->string_list)) {
135  }
136 
137  _luac_build_info(proto, ret);
138 
139  // add entry of main
140  ut64 main_entry_offset;
141  main_entry_offset = proto->code_offset + proto->code_skipped;
142  luac_add_entry(ret->entry_list, main_entry_offset, RZ_BIN_ENTRY_TYPE_PROGRAM);
143 
144  return ret;
145 }
RZ_API void rz_bin_symbol_free(RzBinSymbol *sym)
Definition: bin.c:175
void luac_add_entry(RzList *entry_list, ut64 offset, int entry_type)
Definition: luac_bin.c:44
static void try_free_empty_list(RzList *list)
Definition: luac_bin.c:73
static void free_rz_section(RzBinSection *section)
Definition: luac_bin.c:79
static void free_rz_addr(RzBinAddr *addr)
Definition: luac_bin.c:107
static void free_rz_string(RzBinString *string)
Definition: luac_bin.c:95
#define RZ_BIN_ENTRY_TYPE_PROGRAM
Definition: rz_bin.h:33
ut64 code_skipped
opcode data offset to code_offset.
Definition: luac_common.h:60
RzList * entry_list
list of entries
Definition: luac_common.h:164
RzList * section_list
list of sections
Definition: luac_common.h:162
RzList * symbol_list
list of symbols
Definition: luac_common.h:163
RzList * string_list
list of strings
Definition: luac_common.h:165

References _luac_build_info(), lua_proto_ex::code_offset, lua_proto_ex::code_skipped, luac_bin_info::entry_list, free_rz_addr(), free_rz_section(), free_rz_string(), luac_add_entry(), NULL, RZ_BIN_ENTRY_TYPE_PROGRAM, rz_bin_symbol_free(), rz_list_newf(), RZ_LOG_ERROR, RZ_NEW0, luac_bin_info::section_list, luac_bin_info::string_list, luac_bin_info::symbol_list, try_free_empty_list(), and ut64().

Referenced by load_buffer().