Rizin
unix-like reverse engineering framework and cli tools
luac_common.h
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 // put common definition of luac
5 
6 #ifndef BUILD_LUAC_COMMON_H
7 #define BUILD_LUAC_COMMON_H
8 
9 #include <rz_bin.h>
10 #include <rz_lib.h>
11 #include <rz_list.h>
12 
14 
15 /* Macros About Luac Format */
16 #define LUAC_MAGIC_OFFSET 0x00
17 #define LUAC_MAGIC_SIZE 4
18 #define LUAC_VERSION_OFFSET 0x04
19 #define LUAC_VERSION_SIZE 1
20 
21 #define LUAC_MAGIC "\x1b\x4c\x75\x61"
22 
23 /* Lua Constant Tag */
24 #define makevariant(t, v) ((t) | ((v) << 4))
25 
26 #define LUA_TNIL 0
27 #define LUA_TBOOLEAN 1
28 #define LUA_TNUMBER 3
29 #define LUA_TSTRING 4
30 
31 #define LUA_VNIL makevariant(LUA_TNIL, 0)
32 #define LUA_VFALSE makevariant(LUA_TBOOLEAN, 0)
33 #define LUA_VTRUE makevariant(LUA_TBOOLEAN, 1)
34 #define LUA_VNUMINT makevariant(LUA_TNUMBER, 0) /* integer numbers */
35 #define LUA_VNUMFLT makevariant(LUA_TNUMBER, 1) /* float numbers */
36 #define LUA_VSHRSTR makevariant(LUA_TSTRING, 0) /* short strings */
37 #define LUA_VLNGSTR makevariant(LUA_TSTRING, 1) /* long strings */
38 
43 typedef struct lua_proto_ex {
46 
48  int name_size;
49 
52 
56 
57  /* Code of this proto */
61 
62  /* store constant entries */
66 
67  /* store upvalue entries */
71 
72  /* store protos defined in this proto */
76 
77  /* store Debug info */
84 
86 
88 
93 typedef struct lua_constant_entry {
94  ut8 tag;
95  void *data;
96  int data_len;
99 
104 typedef struct lua_upvalue_entry {
105  /* attributes of upvalue */
111 
112 typedef struct LuaProto LuaProtoEntry;
113 
118 typedef struct lua_lineinfo_entry {
122 
127 typedef struct lua_abs_lineinfo_entry {
128  int pc;
129  int line;
132 
137 typedef struct lua_local_var_entry {
140  int start_pc;
141  int end_pc;
144 
149 typedef struct lua_dbg_upvalue_entry {
151  int name_len;
154 
159 typedef struct luac_bin_info {
168 
169 /* ========================================================
170  * Common Operation to Lua structures
171  * Implemented in 'bin/format/luac/luac_common.c'
172  * ======================================================== */
180 
185 
186 /* ========================================================
187  * Common Operation to RzBinInfo
188  * Implemented in 'bin/format/luac/luac_bin.c'
189  * ======================================================== */
190 void luac_add_section(RzList *section_list, char *name, ut64 offset, ut32 size, bool is_func);
191 void luac_add_symbol(RzList *symbol_list, char *name, ut64 offset, ut64 size, const char *type);
192 void luac_add_entry(RzList *entry_list, ut64 offset, int entry_type);
193 void luac_add_string(RzList *string_list, char *string, ut64 offset, ut64 size);
194 
197 
198 /* ========================================================
199  * Export version specified Api to bin_luac.c
200  * Implemented in 'bin/format/luac/v[version]/bin_[version]
201  * ======================================================== */
204 
207 
208 #define lua_check_error_offset(offset) \
209  if ((offset) == 0) { \
210  return 0; \
211  }
212 #define lua_check_error_offset_proto(offset, proto) \
213  if ((offset) == 0) { \
214  lua_free_proto_entry((proto)); \
215  return NULL; \
216  }
217 #define lua_return_if_null(proto) \
218  if ((proto) == NULL) { \
219  return 0; \
220  }
221 
222 #endif // BUILD_LUAC_COMMON_H
RzBinInfo * info(RzBinFile *bf)
Definition: bin_ne.c:86
uint32_t ut32
#define minor(dev)
Definition: fsmagic.c:57
#define major(dev)
Definition: fsmagic.c:56
voidpf void uLong size
Definition: ioapi.h:138
voidpf uLong offset
Definition: ioapi.h:144
uint8_t ut8
Definition: lh5801.h:11
struct lua_proto_ex LuaProtoHeavy
void lua_free_local_var_entry(LuaLocalVarEntry *)
Definition: luac_common.c:103
LuaLineinfoEntry * lua_new_lineinfo_entry()
Definition: luac_common.c:23
LuacBinInfo * luac_build_info(LuaProto *proto)
Definition: luac_bin.c:114
void luac_add_entry(RzList *entry_list, ut64 offset, int entry_type)
Definition: luac_bin.c:44
void luac_add_symbol(RzList *symbol_list, char *name, ut64 offset, ut64 size, const char *type)
Definition: luac_bin.c:30
struct lua_abs_lineinfo_entry LuaAbsLineinfoEntry
struct lua_local_var_entry LuaLocalVarEntry
LuaConstEntry * lua_new_const_entry()
Definition: luac_common.c:33
RzBinInfo * lua_parse_header_54(RzBinFile *bf, st32 major, st32 minor)
Definition: parse_54.c:484
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
ut32 LUA_INSTRUCTION
Definition: luac_common.h:13
RzBinInfo * lua_parse_header_53(RzBinFile *bf, st32 major, st32 minor)
Definition: parse_53.c:483
void lua_free_const_entry(LuaConstEntry *)
Definition: luac_common.c:111
LuaProto * lua_parse_body_54(RzBuffer *buffer, ut64 offset, ut64 data_size)
Definition: parse_54.c:410
LuaProto * lua_new_proto_entry()
Definition: luac_common.c:38
struct lua_upvalue_entry LuaUpvalueEntry
struct lua_lineinfo_entry LuaLineinfoEntry
LuaProtoHeavy LuaProto
Definition: luac_common.h:87
LuaDbgUpvalueEntry * lua_new_dbg_upvalue_entry()
Definition: luac_common.c:8
LuaLocalVarEntry * lua_new_local_var_entry()
Definition: luac_common.c:13
LuaUpvalueEntry * lua_new_upvalue_entry()
Definition: luac_common.c:28
void lua_free_proto_entry(LuaProto *)
Definition: luac_common.c:119
struct lua_constant_entry LuaConstEntry
struct luac_bin_info LuacBinInfo
LuaAbsLineinfoEntry * lua_new_abs_lineinfo_entry()
Definition: luac_common.c:18
void lua_free_dbg_upvalue_entry(LuaDbgUpvalueEntry *)
Definition: luac_common.c:94
void luac_add_section(RzList *section_list, char *name, ut64 offset, ut32 size, bool is_func)
Definition: luac_bin.c:6
LuaProto * lua_parse_body_53(RzBuffer *buffer, ut64 offset, ut64 data_size)
Definition: parse_53.c:407
struct lua_dbg_upvalue_entry LuaDbgUpvalueEntry
int type
Definition: mipsasm.c:17
#define st32
Definition: rz_types_base.h:12
Definition: buffer.h:15
Store line info attributes.
Definition: luac_common.h:127
int line
line number in source file
Definition: luac_common.h:129
ut64 offset
Definition: luac_common.h:130
int pc
pc value of lua
Definition: luac_common.h:128
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 line info attributes.
Definition: luac_common.h:118
ut32 info_data
Definition: luac_common.h:119
ut64 offset
Definition: luac_common.h:120
Store local var names and other info.
Definition: luac_common.h:137
int varname_len
length of name
Definition: luac_common.h:139
int end_pc
first deactive position
Definition: luac_common.h:141
ut64 offset
offset of this entry
Definition: luac_common.h:142
int start_pc
first active position
Definition: luac_common.h:140
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
ut64 lastline_defined
line number of function end
Definition: luac_common.h:51
RzList * dbg_upvalue_entries
A list to store upvalue names.
Definition: luac_common.h:83
ut64 line_defined
line number of function start
Definition: luac_common.h:50
RzList * upvalue_entries
A list to store upvalue entries.
Definition: luac_common.h:68
ut64 size
current proto size
Definition: luac_common.h:45
ut8 is_vararg
is variable arg?
Definition: luac_common.h:54
ut64 inner_proto_offset
sub proto section offset
Definition: luac_common.h:74
RzList * abs_line_info_entries
A list to store absolutely line info entries.
Definition: luac_common.h:81
ut64 code_skipped
opcode data offset to code_offset.
Definition: luac_common.h:60
ut64 const_offset
const section offset
Definition: luac_common.h:64
ut8 num_params
number of parameters of this proto
Definition: luac_common.h:53
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
RzList * line_info_entries
A list to store line info entries.
Definition: luac_common.h:80
ut8 max_stack_size
max stack size
Definition: luac_common.h:55
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
ut8 idx
index
Definition: luac_common.h:107
ut8 kind
kind
Definition: luac_common.h:108
ut8 instack
is in stack
Definition: luac_common.h:106
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
Definition: z80asm.h:102
XX curplugin == o->plugin.
Definition: rz_bin.h:298
ut64(WINAPI *w32_GetEnabledXStateFeatures)()