Rizin
unix-like reverse engineering framework and cli tools
lua_arch.h File Reference
#include <rz_types.h>
#include <rz_asm.h>

Go to the source code of this file.

Macros

#define LUA_REG_PREF   "R"
 
#define LUA_CONST_PREF   "CONST"
 
#define LUA_KR_PREF   "R/CONST"
 
#define LUA_UPVALUE_PREF   "UPV"
 
#define LUA_EVENT_PREF   "EVENT"
 
#define LUA_KPROTO_PREF   "KPROTO"
 
#define LUA_NO_PREFIX   ""
 
#define LUA_EXTRAARG_MARK   "#Ex"
 
#define LUA_KFLAG_MARK   "#Kflag"
 
#define LUA_JMP_MARK   "#To"
 
#define LUA_CLOSURE_MARK   "#CLOSURE"
 
#define LUA_KX_MARK   " CONST[#Ex]"
 

Typedefs

typedef ut32 LuaInstruction
 
typedef char ** LuaOpNameList
 

Functions

LuaInstruction lua_build_instruction (const ut8 *buf)
 
void lua_set_instruction (LuaInstruction instruction, ut8 *data)
 
int lua_load_next_arg_start (const char *raw_string, char *recv_buf)
 
bool lua_is_valid_num_value_string (const char *str)
 
int lua_convert_str_to_num (const char *str)
 
char * luaop_new_str_3arg (char *opname, int a, int b, int c)
 
char * luaop_new_str_2arg (char *opname, int a, int b)
 
char * luaop_new_str_1arg (char *opname, int a)
 
char * luaop_new_str_3arg_ex (char *opname, int a, int b, int c, int isk)
 
char * luaop_new_str_2arg_ex (char *opname, int a, int b, int isk)
 
char * luaop_new_str_1arg_ex (char *opname, int a, int isk)
 
bool free_lua_opnames (LuaOpNameList list)
 
int lua54_disasm (RzAsmOp *op, const ut8 *buf, int len, LuaOpNameList oplist)
 
int lua54_anal_op (RzAnalysis *analysis, RzAnalysisOp *op, ut64 addr, const ut8 *data, int len)
 
bool lua54_assembly (const char *input, st32 input_size, LuaInstruction *instruction)
 
LuaOpNameList get_lua54_opnames (void)
 
ut8 get_lua54_opcode_by_name (const char *name, int len)
 
int lua53_disasm (RzAsmOp *op, const ut8 *buf, int len, LuaOpNameList oplist)
 
int lua53_anal_op (RzAnalysis *analysis, RzAnalysisOp *op, ut64 addr, const ut8 *data, int len)
 
bool lua53_assembly (const char *input, st32 input_size, LuaInstruction *instruction)
 
LuaOpNameList get_lua53_opnames (void)
 
ut8 get_lua53_opcode_by_name (const char *name, int len)
 

Macro Definition Documentation

◆ LUA_CLOSURE_MARK

#define LUA_CLOSURE_MARK   "#CLOSURE"

Definition at line 23 of file lua_arch.h.

◆ LUA_CONST_PREF

#define LUA_CONST_PREF   "CONST"

Definition at line 12 of file lua_arch.h.

◆ LUA_EVENT_PREF

#define LUA_EVENT_PREF   "EVENT"

Definition at line 15 of file lua_arch.h.

◆ LUA_EXTRAARG_MARK

#define LUA_EXTRAARG_MARK   "#Ex"

Definition at line 20 of file lua_arch.h.

◆ LUA_JMP_MARK

#define LUA_JMP_MARK   "#To"

Definition at line 22 of file lua_arch.h.

◆ LUA_KFLAG_MARK

#define LUA_KFLAG_MARK   "#Kflag"

Definition at line 21 of file lua_arch.h.

◆ LUA_KPROTO_PREF

#define LUA_KPROTO_PREF   "KPROTO"

Definition at line 16 of file lua_arch.h.

◆ LUA_KR_PREF

#define LUA_KR_PREF   "R/CONST"

Definition at line 13 of file lua_arch.h.

◆ LUA_KX_MARK

#define LUA_KX_MARK   " CONST[#Ex]"

Definition at line 24 of file lua_arch.h.

◆ LUA_NO_PREFIX

#define LUA_NO_PREFIX   ""

Definition at line 17 of file lua_arch.h.

◆ LUA_REG_PREF

#define LUA_REG_PREF   "R"

Definition at line 11 of file lua_arch.h.

◆ LUA_UPVALUE_PREF

#define LUA_UPVALUE_PREF   "UPV"

Definition at line 14 of file lua_arch.h.

Typedef Documentation

◆ LuaInstruction

Definition at line 27 of file lua_arch.h.

◆ LuaOpNameList

typedef char** LuaOpNameList

Definition at line 30 of file lua_arch.h.

Function Documentation

◆ free_lua_opnames()

bool free_lua_opnames ( LuaOpNameList  list)

Definition at line 22 of file lua_arch.c.

22  {
23  if (list != NULL) {
24  RZ_FREE(list);
25  return true;
26  }
27  return false;
28 }
#define NULL
Definition: cris-opc.c:27
static void list(RzEgg *egg)
Definition: rz-gg.c:52
#define RZ_FREE(x)
Definition: rz_types.h:369

References list(), NULL, and RZ_FREE.

Referenced by rz_luac_disasm().

◆ get_lua53_opcode_by_name()

ut8 get_lua53_opcode_by_name ( const char *  name,
int  len 
)

Definition at line 69 of file opcode_53.c.

69  {
70  lua_strcase("move") return OP_MOVE;
71  lua_strcase("loadk") return OP_LOADK;
72  lua_strcase("loadkx") return OP_LOADKX;
73  lua_strcase("loadbool") return OP_LOADBOOL;
74  lua_strcase("loadnil") return OP_LOADNIL;
75  lua_strcase("getupval") return OP_GETUPVAL;
76  lua_strcase("gettabup") return OP_GETTABUP;
77  lua_strcase("gettable") return OP_GETTABLE;
78  lua_strcase("settabup") return OP_SETTABUP;
79  lua_strcase("setupval") return OP_SETUPVAL;
80  lua_strcase("settable") return OP_SETTABLE;
81  lua_strcase("newtable") return OP_NEWTABLE;
82 
83  lua_strcase("self") return OP_SELF;
84  lua_strcase("add") return OP_ADD;
85  lua_strcase("sub") return OP_SUB;
86  lua_strcase("mul") return OP_MUL;
87  lua_strcase("mod") return OP_MOD;
88  lua_strcase("pow") return OP_POW;
89  lua_strcase("div") return OP_DIV;
90  lua_strcase("idiv") return OP_IDIV;
91  lua_strcase("band") return OP_BAND;
92  lua_strcase("bor") return OP_BOR;
93  lua_strcase("bxor") return OP_BXOR;
94  lua_strcase("shl") return OP_SHL;
95  lua_strcase("shr") return OP_SHR;
96  lua_strcase("unm") return OP_UNM;
97  lua_strcase("bnot") return OP_BNOT;
98  lua_strcase("not") return OP_NOT;
99 
100  lua_strcase("len") return OP_LEN;
101  lua_strcase("concat") return OP_CONCAT;
102  lua_strcase("jmp") return OP_JMP;
103  lua_strcase("eq") return OP_EQ;
104  lua_strcase("lt") return OP_LT;
105  lua_strcase("le") return OP_LE;
106  lua_strcase("test") return OP_TEST;
107  lua_strcase("testset") return OP_TESTSET;
108 
109  lua_strcase("call") return OP_CALL;
110  lua_strcase("tailcall") return OP_TAILCALL;
111  lua_strcase("return") return OP_RETURN;
112  lua_strcase("forloop") return OP_FORLOOP;
113  lua_strcase("forprep") return OP_FORPREP;
114  lua_strcase("tforcall") return OP_TFORCALL;
115  lua_strcase("tforloop") return OP_TFORLOOP;
116  lua_strcase("setlist") return OP_SETLIST;
117  lua_strcase("closure") return OP_CLOSURE;
118  lua_strcase("vararg") return OP_VARARG;
119  lua_strcase("extraarg") return OP_EXTRAARG;
120 
121  return OP_EXTRAARG + 1; // invalid
122 }
size_t len
Definition: 6502dis.c:15
@ OP_DIV
Definition: 8051_ops.h:51
@ OP_ADD
Definition: 8051_ops.h:42
@ OP_MUL
Definition: 8051_ops.h:63
@ OP_JMP
Definition: 8051_ops.h:57
#define jmp
@ OP_SETLIST
Definition: arch_53.h:122
@ OP_EQ
Definition: arch_53.h:104
@ OP_VARARG
Definition: arch_53.h:126
@ OP_CONCAT
Definition: arch_53.h:101
@ OP_BOR
Definition: arch_53.h:92
@ OP_SETTABLE
Definition: arch_53.h:78
@ OP_POW
Definition: arch_53.h:88
@ OP_NOT
Definition: arch_53.h:98
@ OP_TESTSET
Definition: arch_53.h:109
@ OP_MOD
Definition: arch_53.h:87
@ OP_CLOSURE
Definition: arch_53.h:124
@ OP_SETUPVAL
Definition: arch_53.h:77
@ OP_FORPREP
Definition: arch_53.h:117
@ OP_LEN
Definition: arch_53.h:99
@ OP_LOADNIL
Definition: arch_53.h:70
@ OP_BAND
Definition: arch_53.h:91
@ OP_SELF
Definition: arch_53.h:82
@ OP_SUB
Definition: arch_53.h:85
@ OP_SHR
Definition: arch_53.h:95
@ OP_LT
Definition: arch_53.h:105
@ OP_TFORLOOP
Definition: arch_53.h:120
@ OP_SHL
Definition: arch_53.h:94
@ OP_TEST
Definition: arch_53.h:108
@ OP_TFORCALL
Definition: arch_53.h:119
@ OP_FORLOOP
Definition: arch_53.h:115
@ OP_GETTABLE
Definition: arch_53.h:74
@ OP_LOADK
Definition: arch_53.h:67
@ OP_GETUPVAL
Definition: arch_53.h:71
@ OP_SETTABUP
Definition: arch_53.h:76
@ OP_IDIV
Definition: arch_53.h:90
@ OP_GETTABUP
Definition: arch_53.h:73
@ OP_LE
Definition: arch_53.h:106
@ OP_RETURN
Definition: arch_53.h:113
@ OP_BNOT
Definition: arch_53.h:97
@ OP_MOVE
Definition: arch_53.h:66
@ OP_UNM
Definition: arch_53.h:96
@ OP_EXTRAARG
Definition: arch_53.h:128
@ OP_LOADKX
Definition: arch_53.h:68
@ OP_NEWTABLE
Definition: arch_53.h:80
@ OP_LOADBOOL
Definition: arch_53.h:69
@ OP_BXOR
Definition: arch_53.h:93
@ OP_TAILCALL
Definition: arch_53.h:112
static RzILOpEffect * mul(cs_insn *insn, bool is_thumb)
Definition: arm_il32.c:539
int call(int a, int b)
Definition: bcj_test.c:25
int mod(int a, int b)
Definition: crypto_rot.c:8
#define concat(x)
Definition: fmt.c:11
-lz4-versions
#define OP_CALL
Definition: nios2.h:263
#define lua_strcase(case_str)
Definition: opcode_53.c:6
static int add(char *argv[])
Definition: ziptool.c:84

References lua_strcase, OP_ADD, OP_BAND, OP_BNOT, OP_BOR, OP_BXOR, OP_CALL, OP_CLOSURE, OP_CONCAT, OP_DIV, OP_EQ, OP_EXTRAARG, OP_FORLOOP, OP_FORPREP, OP_GETTABLE, OP_GETTABUP, OP_GETUPVAL, OP_IDIV, OP_JMP, OP_LE, OP_LEN, OP_LOADBOOL, OP_LOADK, OP_LOADKX, OP_LOADNIL, OP_LT, OP_MOD, OP_MOVE, OP_MUL, OP_NEWTABLE, OP_NOT, OP_POW, OP_RETURN, OP_SELF, OP_SETLIST, OP_SETTABLE, OP_SETTABUP, OP_SETUPVAL, OP_SHL, OP_SHR, OP_SUB, OP_TAILCALL, OP_TEST, OP_TESTSET, OP_TFORCALL, OP_TFORLOOP, OP_UNM, and OP_VARARG.

Referenced by lua53_assembly().

◆ get_lua53_opnames()

LuaOpNameList get_lua53_opnames ( void  )

Definition at line 10 of file opcode_53.c.

10  {
12  if (list == NULL) {
13  RZ_LOG_ERROR("Cannot allocate lua53 opcode list.\n");
14  return NULL;
15  }
16 
17  // Do not free the const string
18  list[OP_MOVE] = "move";
19  list[OP_LOADK] = "loadk";
20  list[OP_LOADKX] = "loadkx";
21  list[OP_LOADBOOL] = "loadbool";
22  list[OP_LOADNIL] = "loadnil";
23  list[OP_GETUPVAL] = "getupval";
24  list[OP_GETTABUP] = "gettabup";
25  list[OP_GETTABLE] = "gettable";
26  list[OP_SETTABUP] = "settabup";
27  list[OP_SETUPVAL] = "setupval";
28  list[OP_SETTABLE] = "settable";
29  list[OP_NEWTABLE] = "newtable";
30  list[OP_SELF] = "self";
31  list[OP_ADD] = "add";
32  list[OP_SUB] = "sub";
33  list[OP_MUL] = "mul";
34  list[OP_MOD] = "mod";
35  list[OP_POW] = "pow";
36  list[OP_DIV] = "div";
37  list[OP_IDIV] = "idiv";
38  list[OP_BAND] = "band";
39  list[OP_BOR] = "bor";
40  list[OP_BXOR] = "bxor";
41  list[OP_SHL] = "shl";
42  list[OP_SHR] = "shr";
43  list[OP_UNM] = "unm";
44  list[OP_BNOT] = "bnot";
45  list[OP_NOT] = "not";
46  list[OP_LEN] = "len";
47  list[OP_CONCAT] = "concat";
48  list[OP_JMP] = "jmp";
49  list[OP_EQ] = "eq";
50  list[OP_LT] = "lt";
51  list[OP_LE] = "le";
52  list[OP_TEST] = "test";
53  list[OP_TESTSET] = "testset";
54  list[OP_CALL] = "call";
55  list[OP_TAILCALL] = "tailcall";
56  list[OP_RETURN] = "return";
57  list[OP_FORLOOP] = "forloop";
58  list[OP_FORPREP] = "forprep";
59  list[OP_TFORCALL] = "tforcall";
60  list[OP_TFORLOOP] = "tforloop";
61  list[OP_SETLIST] = "setlist";
62  list[OP_CLOSURE] = "closure";
63  list[OP_VARARG] = "vararg";
64  list[OP_EXTRAARG] = "extraarg";
65 
66  return list;
67 }
#define LUA_NUM_OPCODES
Definition: arch_53.h:131
char ** LuaOpNameList
Definition: lua_arch.h:30
#define RZ_LOG_ERROR(fmtstr,...)
Definition: rz_log.h:58
#define RZ_NEWS(x, y)
Definition: rz_types.h:283

References list(), LUA_NUM_OPCODES, NULL, OP_ADD, OP_BAND, OP_BNOT, OP_BOR, OP_BXOR, OP_CALL, OP_CLOSURE, OP_CONCAT, OP_DIV, OP_EQ, OP_EXTRAARG, OP_FORLOOP, OP_FORPREP, OP_GETTABLE, OP_GETTABUP, OP_GETUPVAL, OP_IDIV, OP_JMP, OP_LE, OP_LEN, OP_LOADBOOL, OP_LOADK, OP_LOADKX, OP_LOADNIL, OP_LT, OP_MOD, OP_MOVE, OP_MUL, OP_NEWTABLE, OP_NOT, OP_POW, OP_RETURN, OP_SELF, OP_SETLIST, OP_SETTABLE, OP_SETTABUP, OP_SETUPVAL, OP_SHL, OP_SHR, OP_SUB, OP_TAILCALL, OP_TEST, OP_TESTSET, OP_TFORCALL, OP_TFORLOOP, OP_UNM, OP_VARARG, RZ_LOG_ERROR, and RZ_NEWS.

Referenced by rz_luac_disasm().

◆ get_lua54_opcode_by_name()

ut8 get_lua54_opcode_by_name ( const char *  name,
int  len 
)

Definition at line 105 of file opcode_54.c.

105  {
106  lua_strcase("move") return OP_MOVE;
107  lua_strcase("loadi") return OP_LOADI;
108  lua_strcase("loadf") return OP_LOADF;
109  lua_strcase("loadk") return OP_LOADK;
110  lua_strcase("loadkx") return OP_LOADKX;
111  lua_strcase("loadfalse") return OP_LOADFALSE;
112  lua_strcase("lfalseskip") return OP_LFALSESKIP;
113  lua_strcase("loadtrue") return OP_LOADTRUE;
114  lua_strcase("loadnil") return OP_LOADNIL;
115  lua_strcase("getupval") return OP_GETUPVAL;
116  lua_strcase("setupval") return OP_SETUPVAL;
117 
118  lua_strcase("gettabup") return OP_GETTABUP;
119  lua_strcase("gettable") return OP_GETTABLE;
120  lua_strcase("geti") return OP_GETI;
121  lua_strcase("getfield") return OP_GETFIELD;
122 
123  lua_strcase("settabup") return OP_SETTABUP;
124  lua_strcase("settable") return OP_SETTABLE;
125  lua_strcase("seti") return OP_SETI;
126  lua_strcase("setfield") return OP_SETFIELD;
127 
128  lua_strcase("newtable") return OP_NEWTABLE;
129 
130  lua_strcase("self") return OP_SELF;
131 
132  lua_strcase("addi") return OP_ADDI;
133 
134  lua_strcase("addk") return OP_ADDK;
135  lua_strcase("subk") return OP_SUBK;
136  lua_strcase("mulk") return OP_MULK;
137  lua_strcase("modk") return OP_MODK;
138  lua_strcase("powk") return OP_POWK;
139  lua_strcase("divk") return OP_DIVK;
140  lua_strcase("idivk") return OP_IDIVK;
141 
142  lua_strcase("bandk") return OP_BANDK;
143  lua_strcase("bork") return OP_BORK;
144  lua_strcase("bxork") return OP_BXORK;
145 
146  lua_strcase("shri") return OP_SHRI;
147  lua_strcase("shli") return OP_SHLI;
148 
149  lua_strcase("add") return OP_ADD;
150  lua_strcase("sub") return OP_SUB;
151  lua_strcase("mul") return OP_MUL;
152  lua_strcase("mod") return OP_MOD;
153  lua_strcase("pow") return OP_POW;
154  lua_strcase("div") return OP_DIV;
155  lua_strcase("idiv") return OP_IDIV;
156 
157  lua_strcase("band") return OP_BAND;
158  lua_strcase("bor") return OP_BOR;
159  lua_strcase("bxor") return OP_BXOR;
160  lua_strcase("shl") return OP_SHL;
161  lua_strcase("shr") return OP_SHR;
162 
163  lua_strcase("mmbin") return OP_MMBIN;
164  lua_strcase("mmbini") return OP_MMBINI;
165  lua_strcase("mmbink") return OP_MMBINK;
166 
167  lua_strcase("unm") return OP_UNM;
168  lua_strcase("bnot") return OP_BNOT;
169  lua_strcase("not") return OP_NOT;
170  lua_strcase("len") return OP_LEN;
171  lua_strcase("concat") return OP_CONCAT;
172 
173  lua_strcase("close") return OP_CLOSE;
174  lua_strcase("tbc") return OP_TBC;
175  lua_strcase("jmp") return OP_JMP;
176  lua_strcase("eq") return OP_EQ;
177  lua_strcase("lt") return OP_LT;
178  lua_strcase("le") return OP_LE;
179 
180  lua_strcase("eqk") return OP_EQK;
181  lua_strcase("eqi") return OP_EQI;
182  lua_strcase("lti") return OP_LTI;
183  lua_strcase("lei") return OP_LEI;
184  lua_strcase("gti") return OP_GTI;
185  lua_strcase("gei") return OP_GEI;
186 
187  lua_strcase("test") return OP_TEST;
188  lua_strcase("testset") return OP_TESTSET;
189 
190  lua_strcase("call") return OP_CALL;
191  lua_strcase("tailcall") return OP_TAILCALL;
192 
193  lua_strcase("return") return OP_RETURN;
194  lua_strcase("return0") return OP_RETURN0;
195  lua_strcase("return1") return OP_RETURN1;
196 
197  lua_strcase("forloop") return OP_FORLOOP;
198  lua_strcase("forprep") return OP_FORPREP;
199 
200  lua_strcase("tforprep") return OP_TFORPREP;
201  lua_strcase("tforcall") return OP_TFORCALL;
202  lua_strcase("tforloop") return OP_TFORLOOP;
203 
204  lua_strcase("setlist") return OP_SETLIST;
205 
206  lua_strcase("closure") return OP_CLOSURE;
207 
208  lua_strcase("vararg") return OP_VARARG;
209 
210  lua_strcase("varargprep") return OP_VARARGPREP;
211 
212  lua_strcase("extraarg") return OP_EXTRAARG;
213 
214  return OP_EXTRAARG + 1; // invalid
215 }
@ OP_MMBINK
Definition: arch_54.h:131
@ OP_EQI
Definition: arch_54.h:148
@ OP_VARARGPREP
Definition: arch_54.h:178
@ OP_LFALSESKIP
Definition: arch_54.h:78
@ OP_MMBINI
Definition: arch_54.h:130
@ OP_POWK
Definition: arch_54.h:104
@ OP_BANDK
Definition: arch_54.h:108
@ OP_MMBIN
Definition: arch_54.h:129
@ OP_MULK
Definition: arch_54.h:102
@ OP_SETFIELD
Definition: arch_54.h:92
@ OP_TBC
Definition: arch_54.h:141
@ OP_GTI
Definition: arch_54.h:151
@ OP_SETI
Definition: arch_54.h:91
@ OP_BORK
Definition: arch_54.h:109
@ OP_LOADI
Definition: arch_54.h:73
@ OP_LTI
Definition: arch_54.h:149
@ OP_LOADF
Definition: arch_54.h:74
@ OP_RETURN1
Definition: arch_54.h:162
@ OP_LOADFALSE
Definition: arch_54.h:77
@ OP_DIVK
Definition: arch_54.h:105
@ OP_SHRI
Definition: arch_54.h:112
@ OP_GETI
Definition: arch_54.h:86
@ OP_IDIVK
Definition: arch_54.h:106
@ OP_CLOSE
Definition: arch_54.h:140
@ OP_ADDK
Definition: arch_54.h:100
@ OP_TFORPREP
Definition: arch_54.h:168
@ OP_SHLI
Definition: arch_54.h:113
@ OP_GETFIELD
Definition: arch_54.h:87
@ OP_LOADTRUE
Definition: arch_54.h:79
@ OP_EQK
Definition: arch_54.h:147
@ OP_MODK
Definition: arch_54.h:103
@ OP_GEI
Definition: arch_54.h:152
@ OP_RETURN0
Definition: arch_54.h:161
@ OP_LEI
Definition: arch_54.h:150
@ OP_BXORK
Definition: arch_54.h:110
@ OP_SUBK
Definition: arch_54.h:101
static static fork const void static count close
Definition: sflib.h:33
#define OP_ADDI
Definition: nios2.h:253
#define lua_strcase(case_str)
Definition: opcode_54.c:5

References lua_strcase, OP_ADD, OP_ADDI, OP_ADDK, OP_BAND, OP_BANDK, OP_BNOT, OP_BOR, OP_BORK, OP_BXOR, OP_BXORK, OP_CALL, OP_CLOSE, OP_CLOSURE, OP_CONCAT, OP_DIV, OP_DIVK, OP_EQ, OP_EQI, OP_EQK, OP_EXTRAARG, OP_FORLOOP, OP_FORPREP, OP_GEI, OP_GETFIELD, OP_GETI, OP_GETTABLE, OP_GETTABUP, OP_GETUPVAL, OP_GTI, OP_IDIV, OP_IDIVK, OP_JMP, OP_LE, OP_LEI, OP_LEN, OP_LFALSESKIP, OP_LOADF, OP_LOADFALSE, OP_LOADI, OP_LOADK, OP_LOADKX, OP_LOADNIL, OP_LOADTRUE, OP_LT, OP_LTI, OP_MMBIN, OP_MMBINI, OP_MMBINK, OP_MOD, OP_MODK, OP_MOVE, OP_MUL, OP_MULK, OP_NEWTABLE, OP_NOT, OP_POW, OP_POWK, OP_RETURN, OP_RETURN0, OP_RETURN1, OP_SELF, OP_SETFIELD, OP_SETI, OP_SETLIST, OP_SETTABLE, OP_SETTABUP, OP_SETUPVAL, OP_SHL, OP_SHLI, OP_SHR, OP_SHRI, OP_SUB, OP_SUBK, OP_TAILCALL, OP_TBC, OP_TEST, OP_TESTSET, OP_TFORCALL, OP_TFORLOOP, OP_TFORPREP, OP_UNM, OP_VARARG, and OP_VARARGPREP.

Referenced by lua54_assembly().

◆ get_lua54_opnames()

LuaOpNameList get_lua54_opnames ( void  )

Definition at line 9 of file opcode_54.c.

9  {
11  if (list == NULL) {
12  RZ_LOG_ERROR("Cannot allocate lua54 opcode list.\n");
13  return NULL;
14  }
15 
16  // Do not free the const string
17  list[OP_MOVE] = "move";
18  list[OP_LOADI] = "loadi",
19  list[OP_LOADF] = "loadf",
20  list[OP_LOADK] = "loadk",
21  list[OP_LOADKX] = "loadkx",
22  list[OP_LOADFALSE] = "loadfalse",
23  list[OP_LFALSESKIP] = "lfalseskip",
24  list[OP_LOADTRUE] = "loadtrue",
25  list[OP_LOADNIL] = "loadnil",
26  list[OP_GETUPVAL] = "getupval",
27  list[OP_SETUPVAL] = "setupval",
28  list[OP_GETTABUP] = "gettabup",
29  list[OP_GETTABLE] = "gettable",
30  list[OP_GETI] = "geti",
31  list[OP_GETFIELD] = "getfield",
32  list[OP_SETTABUP] = "settabup",
33  list[OP_SETTABLE] = "settable",
34  list[OP_SETI] = "seti",
35  list[OP_SETFIELD] = "setfield",
36  list[OP_NEWTABLE] = "newtable",
37  list[OP_SELF] = "self",
38  list[OP_ADDI] = "addi",
39  list[OP_ADDK] = "addk",
40  list[OP_SUBK] = "subk",
41  list[OP_MULK] = "mulk",
42  list[OP_MODK] = "modk",
43  list[OP_POWK] = "powk",
44  list[OP_DIVK] = "divk",
45  list[OP_IDIVK] = "idivk",
46  list[OP_BANDK] = "bandk",
47  list[OP_BORK] = "bork",
48  list[OP_BXORK] = "bxork",
49  list[OP_SHRI] = "shri",
50  list[OP_SHLI] = "shli",
51  list[OP_ADD] = "add",
52  list[OP_SUB] = "sub",
53  list[OP_MUL] = "mul",
54  list[OP_MOD] = "mod",
55  list[OP_POW] = "pow",
56  list[OP_DIV] = "div",
57  list[OP_IDIV] = "idiv",
58  list[OP_BAND] = "band",
59  list[OP_BOR] = "bor",
60  list[OP_BXOR] = "bxor",
61  list[OP_SHL] = "shl",
62  list[OP_SHR] = "shr",
63  list[OP_MMBIN] = "mmbin",
64  list[OP_MMBINI] = "mmbini",
65  list[OP_MMBINK] = "mmbink",
66  list[OP_UNM] = "unm",
67  list[OP_BNOT] = "bnot",
68  list[OP_NOT] = "not",
69  list[OP_LEN] = "len",
70  list[OP_CONCAT] = "concat",
71  list[OP_CLOSE] = "close",
72  list[OP_TBC] = "tbc",
73  list[OP_JMP] = "jmp",
74  list[OP_EQ] = "eq",
75  list[OP_LT] = "lt",
76  list[OP_LE] = "le",
77  list[OP_EQK] = "eqk",
78  list[OP_EQI] = "eqi",
79  list[OP_LTI] = "lti",
80  list[OP_LEI] = "lei",
81  list[OP_GTI] = "gti",
82  list[OP_GEI] = "gei",
83  list[OP_TEST] = "test",
84  list[OP_TESTSET] = "testset",
85  list[OP_CALL] = "call",
86  list[OP_TAILCALL] = "tailcall",
87  list[OP_RETURN] = "return",
88  list[OP_RETURN0] = "return0",
89  list[OP_RETURN1] = "return1",
90  list[OP_FORLOOP] = "forloop",
91  list[OP_FORPREP] = "forprep",
92  list[OP_TFORPREP] = "tforprep",
93  list[OP_TFORCALL] = "tforcall",
94  list[OP_TFORLOOP] = "tforloop",
95  list[OP_SETLIST] = "setlist",
96  list[OP_CLOSURE] = "closure",
97  list[OP_VARARG] = "vararg",
98  list[OP_VARARGPREP] = "varargprep",
99  list[OP_EXTRAARG] = "extraarg",
101 
102  return list;
103 }

References list(), LUA_NUM_OPCODES, NULL, OP_ADD, OP_ADDI, OP_ADDK, OP_BAND, OP_BANDK, OP_BNOT, OP_BOR, OP_BORK, OP_BXOR, OP_BXORK, OP_CALL, OP_CLOSE, OP_CLOSURE, OP_CONCAT, OP_DIV, OP_DIVK, OP_EQ, OP_EQI, OP_EQK, OP_EXTRAARG, OP_FORLOOP, OP_FORPREP, OP_GEI, OP_GETFIELD, OP_GETI, OP_GETTABLE, OP_GETTABUP, OP_GETUPVAL, OP_GTI, OP_IDIV, OP_IDIVK, OP_JMP, OP_LE, OP_LEI, OP_LEN, OP_LFALSESKIP, OP_LOADF, OP_LOADFALSE, OP_LOADI, OP_LOADK, OP_LOADKX, OP_LOADNIL, OP_LOADTRUE, OP_LT, OP_LTI, OP_MMBIN, OP_MMBINI, OP_MMBINK, OP_MOD, OP_MODK, OP_MOVE, OP_MUL, OP_MULK, OP_NEWTABLE, OP_NOT, OP_POW, OP_POWK, OP_RETURN, OP_RETURN0, OP_RETURN1, OP_SELF, OP_SETFIELD, OP_SETI, OP_SETLIST, OP_SETTABLE, OP_SETTABUP, OP_SETUPVAL, OP_SHL, OP_SHLI, OP_SHR, OP_SHRI, OP_SUB, OP_SUBK, OP_TAILCALL, OP_TBC, OP_TEST, OP_TESTSET, OP_TFORCALL, OP_TFORLOOP, OP_TFORPREP, OP_UNM, OP_VARARG, OP_VARARGPREP, RZ_LOG_ERROR, and RZ_NEWS.

Referenced by rz_luac_disasm().

◆ lua53_anal_op()

int lua53_anal_op ( RzAnalysis analysis,
RzAnalysisOp op,
ut64  addr,
const ut8 data,
int  len 
)

Definition at line 7 of file analysis_53.c.

7  {
8  if (!op) {
9  return 0;
10  }
11 
12  memset(op, 0, sizeof(RzAnalysisOp));
14 
15  ut32 extra_arg = 0;
16  op->addr = addr;
17  op->size = 4;
19  op->eob = false;
20 
22  return op->size;
23  }
24  // op->mnemonic = strdup ();
25 
26  switch (GET_OPCODE(instruction)) {
27  case OP_MOVE: /* A B R(A) := R(B) */
29  break;
30  case OP_LOADK: /* A Bx R(A) := Kst(Bx) */
32  break;
33  case OP_LOADKX: /* A R(A) := Kst(extra arg) */
35  extra_arg = lua_build_instruction(data + 4);
36  if (GET_OPCODE(extra_arg) == OP_EXTRAARG) {
37  op->size = 8;
38  }
39  break;
40  case OP_LOADBOOL: /* A B C R(A) := (Bool)B; if (C) pc++ */
42  op->val = !!GETARG_B(instruction);
43  op->jump = op->addr + 8;
44  op->fail = op->addr + 4;
45  break;
46  case OP_LOADNIL: /* A B R(A), R(A+1), ..., R(A+B) := nil */
47  break;
48  case OP_GETUPVAL: /* A B R(A) := UpValue[B] */
49  case OP_GETTABUP: /* A B C R(A) := UpValue[B][RK(C)] */
51  break;
52  case OP_GETTABLE: /* A B C R(A) := R(B)[RK(C)] */
53  break;
54 
55  case OP_SETTABUP: /* A B C UpValue[A][RK(B)] := RK(C) */
56  case OP_SETUPVAL: /* A B UpValue[B] := R(A) */
58  break;
59  case OP_SETTABLE: /* A B C R(A)[RK(B)] := RK(C) */
60  break;
61  case OP_NEWTABLE: /* A B C R(A) := {} (size = B,C) */
63  break;
64  case OP_SELF: /* A B C R(A+1) := R(B); R(A) := R(B)[RK(C)] */
65  break;
66  case OP_ADD: /* A B C R(A) := RK(B) + RK(C) */
68  break;
69  case OP_SUB: /* A B C R(A) := RK(B) - RK(C) */
71  break;
72  case OP_MUL: /* A B C R(A) := RK(B) * RK(C) */
74  break;
75  case OP_MOD: /* A B C R(A) := RK(B) % RK(C) */
77  break;
78  case OP_POW: /* A B C R(A) := RK(B) ^ RK(C) */
79  break;
80  case OP_DIV: /* A B C R(A) := RK(B) / RK(C) */
81  case OP_IDIV: /* A B C R(A) := RK(B) // RK(C) */
83  break;
84  case OP_BAND: /* A B C R(A) := RK(B) & RK(C) */
86  break;
87  case OP_BOR: /* A B C R(A) := RK(B) | RK(C) */
88  op->type = RZ_ANALYSIS_OP_TYPE_OR;
89  break;
90  case OP_BXOR: /* A B C R(A) := RK(B) ~ RK(C) */
92  break;
93  case OP_SHL: /* A B C R(A) := RK(B) << RK(C) */
95  break;
96  case OP_SHR: /* A B C R(A) := RK(B) >> RK(C) */
98  break;
99  case OP_UNM: /* A B R(A) := -R(B) */
100  break;
101  case OP_BNOT: /* A B R(A) := ~R(B) */
102  op->type = RZ_ANALYSIS_OP_TYPE_CPL;
103  break;
104  case OP_NOT: /* A B R(A) := not R(B) */
105  op->type = RZ_ANALYSIS_OP_TYPE_NOT;
106  break;
107  case OP_LEN: /* A B R(A) := length of R(B) */
108  case OP_CONCAT: /* A B C R(A) := R(B).. ... ..R(C) */
109  break;
110  case OP_JMP: /* A sBx pc+=sBx; if (A) close all upvalues >= R(A - 1) */
112  op->jump = op->addr + (st32)(4 * GETARG_sBx(instruction));
113  op->fail = op->addr + 4;
114  break;
115  case OP_EQ: /* A B C if ((RK(B) == RK(C)) ~= A) then pc++ */
116  case OP_LT: /* A B C if ((RK(B) < RK(C)) ~= A) then pc++ */
117  case OP_LE: /* A B C if ((RK(B) <= RK(C)) ~= A) then pc++ */
118  case OP_TEST: /* A C if not (R(A) <=> C) then pc++ */
120  op->jump = op->addr + 8;
121  op->fail = op->addr + 4;
122  break;
123  case OP_TESTSET: /* A B C if (R(B) <=> C) then R(A) := R(B) else pc++ */
125  op->jump = op->addr + 8;
126  op->fail = op->addr + 4;
127  break;
128  case OP_CALL: /* A B C R(A), ... ,R(A+C-2) := R(A)(R(A+1), ... ,R(A+B-1)) */
130  break;
131  case OP_TAILCALL: /* A B C return R(A)(R(A+1), ... ,R(A+B-1)) */
133  op->type2 = RZ_ANALYSIS_OP_TYPE_RET;
134  op->eob = true;
135  op->stackop = RZ_ANALYSIS_STACK_INC;
136  op->stackptr = -4;
137  break;
138  case OP_RETURN: /* A B return R(A), ... ,R(A+B-2) (see note) */
139  op->type = RZ_ANALYSIS_OP_TYPE_RET;
140  op->eob = true;
141  op->stackop = RZ_ANALYSIS_STACK_INC;
142  op->stackptr = -4;
143  break;
144  case OP_FORLOOP: /* A sBx R(A)+=R(A+2); if R(A) <?= R(A+1) then { pc+=sBx; R(A+3)=R(A) }*/
146  op->jump = op->addr + 4 + 4 * (GETARG_sBx(instruction));
147  op->fail = op->addr + 4;
148  break;
149  case OP_FORPREP: /* A sBx R(A)-=R(A+2); pc+=sBx */
150  op->type = RZ_ANALYSIS_OP_TYPE_JMP;
151  op->jump = op->addr + 4 + 4 * (GETARG_sBx(instruction));
152  op->fail = op->addr + 4;
153  break;
154  case OP_TFORCALL: /* A C R(A+3), ... ,R(A+2+C) := R(A)(R(A+1), R(A+2)); */
156  break;
157  case OP_TFORLOOP: /* A sBx if R(A+1) ~= nil then { R(A)=R(A+1); pc += sBx }*/
159  op->jump = op->addr + 4 + 4 * (GETARG_sBx(instruction));
160  op->fail = op->addr + 4;
161  break;
162  case OP_SETLIST: /* A B C R(A)[(C-1)*FPF+i] := R(A+i), 1 <= i <= B */
164  break;
165  case OP_CLOSURE: /* A Bx R(A) := closure(KPROTO[Bx]) */
166  case OP_VARARG: /* A B R(A), R(A+1), ..., R(A+B-2) = vararg */
167  case OP_EXTRAARG: /* Ax extra (larger) argument for previous opcode */
168  break;
169  }
170  return op->size;
171 }
#define GETARG_sBx(i)
Definition: arch_53.h:191
#define GETARG_B(i)
Definition: arch_53.h:179
#define GET_OPCODE(i)
Definition: arch_53.h:168
uint32_t ut32
return memset(p, 0, total)
LuaInstruction lua_build_instruction(const ut8 *buf)
Definition: lua_arch.c:6
@ RZ_ANALYSIS_STACK_INC
Definition: rz_analysis.h:457
@ RZ_ANALYSIS_OP_TYPE_SUB
Definition: rz_analysis.h:402
@ RZ_ANALYSIS_OP_TYPE_LOAD
Definition: rz_analysis.h:416
@ RZ_ANALYSIS_OP_TYPE_UNK
Definition: rz_analysis.h:388
@ RZ_ANALYSIS_OP_TYPE_MUL
Definition: rz_analysis.h:404
@ RZ_ANALYSIS_OP_TYPE_JMP
Definition: rz_analysis.h:368
@ RZ_ANALYSIS_OP_TYPE_AND
Definition: rz_analysis.h:411
@ RZ_ANALYSIS_OP_TYPE_MOD
Definition: rz_analysis.h:422
@ RZ_ANALYSIS_OP_TYPE_CMOV
Definition: rz_analysis.h:391
@ RZ_ANALYSIS_OP_TYPE_ADD
Definition: rz_analysis.h:401
@ RZ_ANALYSIS_OP_TYPE_OR
Definition: rz_analysis.h:410
@ RZ_ANALYSIS_OP_TYPE_STORE
Definition: rz_analysis.h:415
@ RZ_ANALYSIS_OP_TYPE_CPL
Definition: rz_analysis.h:429
@ RZ_ANALYSIS_OP_TYPE_SHR
Definition: rz_analysis.h:406
@ RZ_ANALYSIS_OP_TYPE_CJMP
Definition: rz_analysis.h:373
@ RZ_ANALYSIS_OP_TYPE_DIV
Definition: rz_analysis.h:405
@ RZ_ANALYSIS_OP_TYPE_MOV
Definition: rz_analysis.h:390
@ RZ_ANALYSIS_OP_TYPE_SHL
Definition: rz_analysis.h:407
@ RZ_ANALYSIS_OP_TYPE_NOT
Definition: rz_analysis.h:414
@ RZ_ANALYSIS_OP_TYPE_RET
Definition: rz_analysis.h:385
@ RZ_ANALYSIS_OP_TYPE_RCALL
Definition: rz_analysis.h:380
@ RZ_ANALYSIS_OP_TYPE_XOR
Definition: rz_analysis.h:412
@ RZ_ANALYSIS_OP_TYPE_NEW
Definition: rz_analysis.h:427
#define st32
Definition: rz_types_base.h:12
Definition: dis.c:32
static int addr
Definition: z80asm.c:58

References addr, GET_OPCODE, GETARG_B, GETARG_sBx, lua_build_instruction(), memset(), OP_ADD, OP_BAND, OP_BNOT, OP_BOR, OP_BXOR, OP_CALL, OP_CLOSURE, OP_CONCAT, OP_DIV, OP_EQ, OP_EXTRAARG, OP_FORLOOP, OP_FORPREP, OP_GETTABLE, OP_GETTABUP, OP_GETUPVAL, OP_IDIV, OP_JMP, OP_LE, OP_LEN, OP_LOADBOOL, OP_LOADK, OP_LOADKX, OP_LOADNIL, OP_LT, OP_MOD, OP_MOVE, OP_MUL, OP_NEWTABLE, OP_NOT, OP_POW, OP_RETURN, OP_SELF, OP_SETLIST, OP_SETTABLE, OP_SETTABUP, OP_SETUPVAL, OP_SHL, OP_SHR, OP_SUB, OP_TAILCALL, OP_TEST, OP_TESTSET, OP_TFORCALL, OP_TFORLOOP, OP_UNM, OP_VARARG, RZ_ANALYSIS_OP_TYPE_ADD, RZ_ANALYSIS_OP_TYPE_AND, RZ_ANALYSIS_OP_TYPE_CJMP, RZ_ANALYSIS_OP_TYPE_CMOV, RZ_ANALYSIS_OP_TYPE_CPL, RZ_ANALYSIS_OP_TYPE_DIV, RZ_ANALYSIS_OP_TYPE_JMP, RZ_ANALYSIS_OP_TYPE_LOAD, RZ_ANALYSIS_OP_TYPE_MOD, RZ_ANALYSIS_OP_TYPE_MOV, RZ_ANALYSIS_OP_TYPE_MUL, RZ_ANALYSIS_OP_TYPE_NEW, RZ_ANALYSIS_OP_TYPE_NOT, RZ_ANALYSIS_OP_TYPE_OR, RZ_ANALYSIS_OP_TYPE_RCALL, RZ_ANALYSIS_OP_TYPE_RET, RZ_ANALYSIS_OP_TYPE_SHL, RZ_ANALYSIS_OP_TYPE_SHR, RZ_ANALYSIS_OP_TYPE_STORE, RZ_ANALYSIS_OP_TYPE_SUB, RZ_ANALYSIS_OP_TYPE_UNK, RZ_ANALYSIS_OP_TYPE_XOR, RZ_ANALYSIS_STACK_INC, and st32.

Referenced by rz_lua_analysis_op().

◆ lua53_assembly()

bool lua53_assembly ( const char *  input,
st32  input_size,
LuaInstruction instruction 
)

Definition at line 60 of file assembly_53.c.

60  {
61  const char *opcode_start; // point to the header
62  const char *opcode_end; // point to the first white space
63  int opcode_len;
64 
65  const char *arg_start;
66 
67  ut8 opcode;
69 
70  /* Find the opcode */
71  opcode_start = input;
72  opcode_end = strchr(input, ' ');
73  if (opcode_end == NULL) {
74  opcode_end = input + input_size;
75  }
76 
77  opcode_len = opcode_end - opcode_start;
78  opcode = get_lua53_opcode_by_name(opcode_start, opcode_len);
79 
80  /* Find the arguments */
81  arg_start = rz_str_trim_head_ro(opcode_end);
82 
83  /* Encode opcode and args */
84  switch (opcode) {
85  case OP_LOADKX:
86  instruction = encode_instruction(opcode, arg_start, PARAM_A, 1);
87  break;
88  case OP_MOVE:
89  case OP_SETUPVAL:
90  case OP_UNM:
91  case OP_BNOT:
92  case OP_NOT:
93  case OP_LEN:
94  case OP_LOADNIL:
95  case OP_RETURN:
96  case OP_VARARG:
97  case OP_GETUPVAL:
98  instruction = encode_instruction(opcode, arg_start, PARAM_A | PARAM_B, 2);
99  break;
100  case OP_TEST:
101  case OP_TFORCALL:
102  instruction = encode_instruction(opcode, arg_start, PARAM_A | PARAM_C, 2);
103  break;
104  case OP_LOADK:
105  case OP_CLOSURE:
106  instruction = encode_instruction(opcode, arg_start, PARAM_A | PARAM_Bx, 2);
107  break;
108  case OP_CONCAT:
109  case OP_TESTSET:
110  case OP_CALL:
111  case OP_TAILCALL:
112  case OP_NEWTABLE:
113  case OP_SETLIST:
114  case OP_LOADBOOL:
115  case OP_SELF:
116  case OP_GETTABUP:
117  case OP_GETTABLE:
118  case OP_SETTABUP:
119  case OP_SETTABLE:
120  case OP_ADD:
121  case OP_SUB:
122  case OP_MUL:
123  case OP_MOD:
124  case OP_POW:
125  case OP_DIV:
126  case OP_IDIV:
127  case OP_BAND:
128  case OP_BOR:
129  case OP_BXOR:
130  case OP_SHL:
131  case OP_SHR:
132  case OP_EQ:
133  case OP_LT:
134  case OP_LE:
135  instruction = encode_instruction(opcode, arg_start,
136  PARAM_A | PARAM_B | PARAM_C,
137  3);
138  break;
139  case OP_JMP:
140  case OP_FORLOOP:
141  case OP_FORPREP:
142  case OP_TFORLOOP:
143  instruction = encode_instruction(opcode, arg_start, PARAM_A | PARAM_sBx, 2);
144  break;
145  case OP_EXTRAARG:
146  instruction = encode_instruction(opcode, arg_start, PARAM_Ax, 1);
147  break;
148  default:
149  return false;
150  }
151 
152  if (instruction == -1) {
153  return false;
154  }
155 
156  *instruction_p = instruction;
157  return true;
158 }
#define PARAM_Ax
Definition: arch_53.h:41
#define PARAM_Bx
Definition: arch_53.h:42
#define PARAM_C
Definition: arch_53.h:40
#define PARAM_sBx
Definition: arch_53.h:43
#define PARAM_B
Definition: arch_53.h:39
#define PARAM_A
Definition: arch_53.h:38
static LuaInstruction encode_instruction(ut8 opcode, const char *arg_start, ut16 flag, ut8 arg_num)
Definition: assembly_53.c:7
uint8_t ut8
Definition: lh5801.h:11
ut32 LuaInstruction
Definition: lua_arch.h:27
ut8 get_lua53_opcode_by_name(const char *name, int len)
Definition: opcode_53.c:69
RZ_API const char * rz_str_trim_head_ro(const char *str)
Definition: str_trim.c:86
static bool input(void *ud, zip_uint8_t *data, zip_uint64_t length)

References encode_instruction(), get_lua53_opcode_by_name(), input(), NULL, OP_ADD, OP_BAND, OP_BNOT, OP_BOR, OP_BXOR, OP_CALL, OP_CLOSURE, OP_CONCAT, OP_DIV, OP_EQ, OP_EXTRAARG, OP_FORLOOP, OP_FORPREP, OP_GETTABLE, OP_GETTABUP, OP_GETUPVAL, OP_IDIV, OP_JMP, OP_LE, OP_LEN, OP_LOADBOOL, OP_LOADK, OP_LOADKX, OP_LOADNIL, OP_LT, OP_MOD, OP_MOVE, OP_MUL, OP_NEWTABLE, OP_NOT, OP_POW, OP_RETURN, OP_SELF, OP_SETLIST, OP_SETTABLE, OP_SETTABUP, OP_SETUPVAL, OP_SHL, OP_SHR, OP_SUB, OP_TAILCALL, OP_TEST, OP_TESTSET, OP_TFORCALL, OP_TFORLOOP, OP_UNM, OP_VARARG, PARAM_A, PARAM_Ax, PARAM_B, PARAM_Bx, PARAM_C, PARAM_sBx, and rz_str_trim_head_ro().

Referenced by rz_luac_asm().

◆ lua53_disasm()

int lua53_disasm ( RzAsmOp op,
const ut8 buf,
int  len,
LuaOpNameList  oplist 
)

Definition at line 7 of file disassembly_53.c.

7  {
8  if (len < 4) {
9  RZ_LOG_DEBUG("Cannot disassemble lua53 opcode (truncated).\n");
10  return 0;
11  }
14 
15  /* Pre fetch some args */
16  int a = GETARG_A(instruction);
17  int b = GETARG_B(instruction);
18  int c = GETARG_C(instruction);
19  int ax = GETARG_Ax(instruction);
20  int bx = GETARG_Bx(instruction);
21  int sbx = GETARG_sBx(instruction);
22 
23  // simplify test flag
24  int is_special_B = b & 0x100;
25  int is_special_C = c & 0x100;
26 
27  int special_c = 0xFF - c;
28  int special_b = 0xFF - b;
29 
30  char *asm_string;
31 
32  switch (opcode) {
33  case OP_LOADKX: /* A R(A) := Kst(extra arg) */
34  asm_string = luaop_new_str_1arg(opnames[opcode], a);
35  break;
36  case OP_MOVE: /* A B R(A) := R(B) */
37  case OP_SETUPVAL: /* A B UpValue[B] := R(A) */
38  case OP_UNM: /* A B R(A) := -R(B) */
39  case OP_BNOT: /* A B R(A) := ~R(B) */
40  case OP_NOT: /* A B R(A) := not R(B) */
41  case OP_LEN: /* A B R(A) := length of R(B) */
42  case OP_LOADNIL: /* A B R(A), R(A+1), ..., R(A+B) := nil */
43  case OP_RETURN: /* A B return R(A), ... ,R(A+B-2) (see note) */
44  case OP_VARARG: /* A B R(A), R(A+1), ..., R(A+B-2) = vararg */
45  case OP_GETUPVAL: /* A B R(A) := UpValue[B] */
46  asm_string = luaop_new_str_2arg(opnames[opcode], a, b);
47  break;
48  case OP_TEST: /* A C if not (R(A) <=> C) then pc++ */
49  case OP_TFORCALL: /* A C R(A+3), ... ,R(A+2+C) := R(A)(R(A+1), R(A+2)); */
50  asm_string = luaop_new_str_2arg(opnames[opcode], a, c);
51  break;
52  case OP_LOADK: /* A Bx R(A) := Kst(Bx) */
53  case OP_CLOSURE: /* A Bx R(A) := closure(KPROTO[Bx]) */
54  asm_string = luaop_new_str_2arg(opnames[opcode], a, bx);
55  break;
56  case OP_CONCAT: /* A B C R(A) := R(B).. ... ..R(C) */
57  case OP_TESTSET: /* A B C if (R(B) <=> C) then R(A) := R(B) else pc++ */
58  case OP_CALL: /* A B C R(A), ... ,R(A+C-2) := R(A)(R(A+1), ... ,R(A+B-1)) */
59  case OP_TAILCALL: /* A B C return R(A)(R(A+1), ... ,R(A+B-1)) */
60  case OP_NEWTABLE: /* A B C R(A) := {} (size = B,C) */
61  case OP_SETLIST: /* A B C R(A)[(C-1)*FPF+i] := R(A+i), 1 <= i <= B */
62  case OP_LOADBOOL: /* A B C R(A) := (Bool)B; if (C) pc++ */
63  case OP_SELF: /* A B C R(A+1) := R(B); R(A) := R(B)[RK(C)] */
64  asm_string = luaop_new_str_3arg(opnames[opcode], a, b, c);
65  break;
66  case OP_GETTABUP: /* A B C R(A) := UpValue[B][RK(C)] */
67  case OP_GETTABLE: /* A B C R(A) := R(B)[RK(C)] */
68  if (is_special_C) {
69  asm_string = luaop_new_str_3arg(opnames[opcode], a, b, special_c);
70  } else {
71  asm_string = luaop_new_str_3arg(opnames[opcode], a, b, c);
72  }
73  break;
74  case OP_SETTABUP: /* A B C UpValue[A][RK(B)] := RK(C) */
75  case OP_SETTABLE: /* A B C R(A)[RK(B)] := RK(C) */
76  case OP_ADD: /* A B C R(A) := RK(B) + RK(C) */
77  case OP_SUB: /* A B C R(A) := RK(B) - RK(C) */
78  case OP_MUL: /* A B C R(A) := RK(B) * RK(C) */
79  case OP_MOD: /* A B C R(A) := RK(B) % RK(C) */
80  case OP_POW: /* A B C R(A) := RK(B) ^ RK(C) */
81  case OP_DIV: /* A B C R(A) := RK(B) / RK(C) */
82  case OP_IDIV: /* A B C R(A) := RK(B) // RK(C) */
83  case OP_BAND: /* A B C R(A) := RK(B) & RK(C) */
84  case OP_BOR: /* A B C R(A) := RK(B) | RK(C) */
85  case OP_BXOR: /* A B C R(A) := RK(B) ~ RK(C) */
86  case OP_SHL: /* A B C R(A) := RK(B) << RK(C) */
87  case OP_SHR: /* A B C R(A) := RK(B) >> RK(C) */
88  case OP_EQ: /* A B C if ((RK(B) == RK(C)) ~= A) then pc++ */
89  case OP_LT: /* A B C if ((RK(B) < RK(C)) ~= A) then pc++ */
90  case OP_LE: /* A B C if ((RK(B) <= RK(C)) ~= A) then pc++ */
91  if (is_special_B) {
92  if (is_special_C) {
93  asm_string = luaop_new_str_3arg(
94  opnames[opcode],
95  a, special_b, special_c);
96  } else {
97  asm_string = luaop_new_str_3arg(
98  opnames[opcode],
99  a, special_b, c);
100  }
101  } else {
102  if (is_special_C) {
103  asm_string = luaop_new_str_3arg(
104  opnames[opcode],
105  a, b, special_c);
106  } else {
107  asm_string = luaop_new_str_3arg(
108  opnames[opcode],
109  a, b, c);
110  }
111  }
112  break;
113  case OP_JMP: /* A sBx pc+=sBx; if (A) close all upvalues >= R(A - 1) */
114  case OP_FORLOOP: /* A sBx R(A)+=R(A+2);if R(A) <?= R(A+1) then { pc+=sBx; R(A+3)=R(A) }*/
115  case OP_FORPREP: /* A sBx R(A)-=R(A+2); pc+=sBx */
116  case OP_TFORLOOP: /* A sBx if R(A+1) ~= nil then { R(A)=R(A+1); pc += sBx }*/
117  asm_string = luaop_new_str_2arg(opnames[opcode], a, sbx);
118  break;
119  case OP_EXTRAARG: /* Ax extra (larger) argument for previous opcode */
120  asm_string = luaop_new_str_1arg(opnames[opcode], ax);
121  break;
122  default:
123  asm_string = rz_str_new("invalid");
124  break;
125  }
126 
127  rz_strbuf_append(&op->buf_asm, asm_string);
128  op->size = 4;
129  RZ_FREE(asm_string);
130  return 4;
131 }
#define GETARG_A(i)
Definition: arch_53.h:176
LuaOpCode
Definition: arch_53.h:62
#define GETARG_Bx(i)
Definition: arch_53.h:185
#define GETARG_Ax(i)
Definition: arch_53.h:188
#define GETARG_C(i)
Definition: arch_53.h:182
voidpf void * buf
Definition: ioapi.h:138
char * luaop_new_str_2arg(char *opname, int a, int b)
Definition: lua_arch.c:42
char * luaop_new_str_3arg(char *opname, int a, int b, int c)
Definition: lua_arch.c:31
char * luaop_new_str_1arg(char *opname, int a)
Definition: lua_arch.c:53
#define RZ_LOG_DEBUG(fmtstr,...)
Definition: rz_log.h:49
RZ_API char * rz_str_new(const char *str)
Definition: str.c:865
RZ_API bool rz_strbuf_append(RzStrBuf *sb, const char *s)
Definition: strbuf.c:222
#define b(i)
Definition: sha256.c:42
#define c(i)
Definition: sha256.c:43
#define a(i)
Definition: sha256.c:41

References a, ax, b, c, GET_OPCODE, GETARG_A, GETARG_Ax, GETARG_B, GETARG_Bx, GETARG_C, GETARG_sBx, len, lua_build_instruction(), luaop_new_str_1arg(), luaop_new_str_2arg(), luaop_new_str_3arg(), OP_ADD, OP_BAND, OP_BNOT, OP_BOR, OP_BXOR, OP_CALL, OP_CLOSURE, OP_CONCAT, OP_DIV, OP_EQ, OP_EXTRAARG, OP_FORLOOP, OP_FORPREP, OP_GETTABLE, OP_GETTABUP, OP_GETUPVAL, OP_IDIV, OP_JMP, OP_LE, OP_LEN, OP_LOADBOOL, OP_LOADK, OP_LOADKX, OP_LOADNIL, OP_LT, OP_MOD, OP_MOVE, OP_MUL, OP_NEWTABLE, OP_NOT, OP_POW, OP_RETURN, OP_SELF, OP_SETLIST, OP_SETTABLE, OP_SETTABUP, OP_SETUPVAL, OP_SHL, OP_SHR, OP_SUB, OP_TAILCALL, OP_TEST, OP_TESTSET, OP_TFORCALL, OP_TFORLOOP, OP_UNM, OP_VARARG, RZ_FREE, RZ_LOG_DEBUG, rz_str_new(), and rz_strbuf_append().

Referenced by rz_luac_disasm().

◆ lua54_anal_op()

int lua54_anal_op ( RzAnalysis analysis,
RzAnalysisOp op,
ut64  addr,
const ut8 data,
int  len 
)

Definition at line 6 of file analysis_54.c.

6  {
7  if (!op || len < 4) {
8  return 0;
9  }
10 
11  memset(op, 0, sizeof(RzAnalysisOp));
13 
14  op->size = 4;
15  op->addr = addr;
16 
18  return op->size;
19  }
20 
21  switch (LUA_GET_OPCODE(instruction)) {
22  case OP_MOVE: /* A B R[A] := R[B] */
24  break;
25  case OP_LOADI: /* A sBx R[A] := sBx */
26  case OP_LOADF: /* A sBx R[A] := (lua_Number)sBx */
27  case OP_LOADK: /* A Bx R[A] := K[Bx] */
28  case OP_LOADTRUE: /* A R[A] := true */
29  case OP_LOADNIL: /* A B R[A], R[A+1], ..., R[A+B] := nil */
30  case OP_LOADFALSE: /* A R[A] := false */
32  break;
33  case OP_LOADKX: /* A R[A] := K[extra arg] */
35  op->size = 4;
36  break;
37  case OP_LFALSESKIP: /*A R[A] := false; pc++ */
39  op->size = 8;
40  break;
41  case OP_GETTABUP: /* A B C R[A] := UpValue[B][K[C]:string] */
42  case OP_GETUPVAL: /* A B R[A] := UpValue[B] */
43  case OP_GETI: /* A B C R[A] := R[B][C] */
44  case OP_GETFIELD: /* A B C R[A] := R[B][K[C]:string] */
45  case OP_GETTABLE: /* A B C R[A] := R[B][R[C]] */
46  case OP_SETTABLE: /* A B C R[A][R[B]] := RK(C) */
48  break;
49  case OP_SETTABUP: /* A B C UpValue[A][K[B]:string] := RK(C) */
50  case OP_SETUPVAL: /* A B UpValue[B] := R[A] */
51  case OP_SETI: /* A B C R[A][B] := RK(C) */
52  case OP_SETFIELD: /* A B C R[A][K[B]:string] := RK(C) */
54  break;
55  case OP_NEWTABLE: /* A B C k R[A] := {} */
57  op->size = 4;
58  break;
59  case OP_SELF: /* A B C R[A+1] := R[B]; R[A] := R[B][RK(C):string] */
60  break;
61  case OP_ADDI: /* A B sC R[A] := R[B] + sC */
62  case OP_ADDK: /* A B C R[A] := R[B] + K[C] */
63  case OP_ADD: /* A B C R[A] := R[B] + R[C] */
65  break;
66  case OP_SUBK: /* A B C R[A] := R[B] - K[C] */
67  case OP_SUB: /* A B C R[A] := R[B] - R[C] */
69  break;
70  case OP_MULK: /* A B C R[A] := R[B] * K[C] */
71  case OP_MUL: /* A B C R[A] := R[B] * R[C] */
73  break;
74  case OP_MOD: /* A B C R[A] := R[B] % R[C] */
75  case OP_MODK: /* A B C R[A] := R[B] % K[C] */
77  break;
78  case OP_POW: /* A B C R[A] := R[B] ^ R[C] */
79  case OP_POWK: /* A B C R[A] := R[B] ^ K[C] */
80  break;
81  case OP_DIVK: /* A B C R[A] := R[B] / K[C] */
82  case OP_IDIVK: /* A B C R[A] := R[B] // K[C] */
83  case OP_DIV: /* A B C R[A] := R[B] / R[C] */
84  case OP_IDIV: /* A B C R[A] := R[B] // R[C] */
86  break;
87  case OP_BANDK: /* A B C R[A] := R[B] & K[C]:integer */
88  case OP_BAND: /* A B C R[A] := R[B] & R[C] */
90  break;
91  case OP_BOR: /* A B C R[A] := R[B] | R[C] */
92  case OP_BORK: /* A B C R[A] := R[B] | K[C]:integer */
93  op->type = RZ_ANALYSIS_OP_TYPE_OR;
94  break;
95  case OP_BXOR: /* A B C R[A] := R[B] ~ R[C] */
96  case OP_BXORK: /* A B C R[A] := R[B] ~ K[C]:integer */
98  break;
99  case OP_NOT: /* A B R[A] := not R[B] */
100  op->type = RZ_ANALYSIS_OP_TYPE_NOT;
101  break;
102  case OP_BNOT: /* A B R[A] := ~R[B] */
103  op->type = RZ_ANALYSIS_OP_TYPE_CPL;
104  break;
105  case OP_SHRI: /* A B sC R[A] := R[B] >> sC */
106  case OP_SHR: /* A B C R[A] := R[B] >> R[C] */
107  op->type = RZ_ANALYSIS_OP_TYPE_SHR;
108  break;
109  case OP_SHLI: /* A B sC R[A] := sC << R[B] */
110  case OP_SHL: /* A B C R[A] := R[B] << R[C] */
111  op->type = RZ_ANALYSIS_OP_TYPE_SHL;
112  break;
113  case OP_MMBIN: /* A B C call C metamethod over R[A] and R[B] */
114  case OP_MMBINI: /* A sB C k call C metamethod over R[A] and sB */
115  case OP_MMBINK: /* A B C k call C metamethod over R[A] and K[B] */
117  break;
118  case OP_UNM: /* A B R[A] := -R[B] */
119  case OP_LEN: /* A B R[A] := #R[B] (length operator) */
120  case OP_CONCAT: /* A B R[A] := R[A].. ... ..R[A + B - 1] */
121  case OP_CLOSE: /* A close all upvalues >= R[A] */
122  case OP_TBC: /* A mark variable A "to be closed" */
123  break;
124  case OP_JMP: /* sJ pc += sJ */
126  op->jump = op->addr + (st32)(4 * (LUA_GETARG_sJ(instruction)));
127  op->fail = op->addr + 4;
128  break;
129  case OP_EQ: /* A B k if ((R[A] == R[B]) ~= k) then pc++ */
130  case OP_LT: /* A B k if ((R[A] < R[B]) ~= k) then pc++ */
131  case OP_LE: /* A B k if ((R[A] <= R[B]) ~= k) then pc++ */
132  case OP_EQK: /* A B k if ((R[A] == K[B]) ~= k) then pc++ */
133  case OP_EQI: /* A sB k if ((R[A] == sB) ~= k) then pc++ */
134  case OP_LTI: /* A sB k if ((R[A] < sB) ~= k) then pc++ */
135  case OP_LEI: /* A sB k if ((R[A] <= sB) ~= k) then pc++ */
136  case OP_GTI: /* A sB k if ((R[A] > sB) ~= k) then pc++ */
137  case OP_GEI: /* A sB k if ((R[A] >= sB) ~= k) then pc++ */
138  case OP_TEST: /* A k if (not R[A] == k) then pc++ */
140  op->jump = op->addr + 8;
141  op->fail = op->addr + 4;
142  break;
143  case OP_TESTSET: /* A B k if (not R[B] == k) then pc++ else R[A] := R[B] */
145  op->jump = op->addr + 8;
146  op->fail = op->addr + 4;
147  break;
148  case OP_CALL: /* A B C R[A], ... ,R[A+C-2] := R[A](R[A+1], ... ,R[A+B-1]) */
150  break;
151  case OP_TAILCALL: /* A B C k return R[A](R[A+1], ... ,R[A+B-1]) */
154  op->eob = true;
155  op->stackop = RZ_ANALYSIS_STACK_INC;
156  op->stackptr = -4;
157  break;
158  case OP_RETURN: /* A B C k return R[A], ... ,R[A+B-2] (see note) */
159  case OP_RETURN1: /* A return R[A] */
160  case OP_RETURN0: /* return */
161  op->type = RZ_ANALYSIS_OP_TYPE_RET;
162  op->eob = true;
163  op->stackop = RZ_ANALYSIS_STACK_INC;
164  op->stackptr = -4;
165  break;
166  case OP_FORLOOP: /* A Bx update counters; if loop continues then pc-=Bx; */
167  op->type = RZ_ANALYSIS_OP_TYPE_JMP;
168  op->jump = op->addr + 4 - 4 * (LUA_GETARG_Bx(instruction));
169  op->fail = op->addr + 4;
170  break;
171  case OP_FORPREP: /* A Bx <check values and prepare counters>;
172  if not to run then pc+=Bx+1; */
173  op->type = RZ_ANALYSIS_OP_TYPE_JMP;
174  op->jump = op->addr + 4 + 4 * (LUA_GETARG_Bx(instruction) + 1);
175  op->fail = op->addr + 4;
176  break;
177  case OP_TFORPREP: /* A Bx create upvalue for R[A + 3]; pc+=Bx */
178  op->type = RZ_ANALYSIS_OP_TYPE_JMP;
179  op->jump = op->addr + 4 + 4 * (LUA_GETARG_Bx(instruction));
180  op->fail = op->addr + 4;
181  break;
182  case OP_TFORCALL: /* A C R[A+4], ... ,R[A+3+C] := R[A](R[A+1], R[A+2]); */
184  break;
185  case OP_TFORLOOP: /* A Bx if R[A+2] ~= nil then { R[A]=R[A+2]; pc -= Bx } */
187  op->jump = op->addr + 4 - 4 * (LUA_GETARG_Bx(instruction));
188  op->fail = op->addr + 4;
189  break;
190  case OP_SETLIST: /* A B C k R[A][C+i] := R[A+i], 1 <= i <= B */
192  break;
193  case OP_CLOSURE: /* A Bx R[A] := closure(KPROTO[Bx]) */
194  case OP_VARARG: /* A C R[A], R[A+1], ..., R[A+C-2] = vararg */
195  case OP_VARARGPREP: /*A (adjust vararg parameters) */
196  case OP_EXTRAARG: /* Ax extra (larger) argument for previous opcode */
197  op->size = 4;
198  break;
199  }
200  return op->size;
201 }
#define LUA_GET_OPCODE(i)
Definition: arch_54.h:198
#define LUA_GETARG_sJ(i)
Definition: arch_54.h:213
#define LUA_GETARG_Bx(i)
Definition: arch_54.h:210
@ RZ_ANALYSIS_ADDR_HINT_TYPE_RET
Definition: rz_analysis.h:636
@ RZ_ANALYSIS_OP_TYPE_CALL
Definition: rz_analysis.h:378

References addr, len, lua_build_instruction(), LUA_GET_OPCODE, LUA_GETARG_Bx, LUA_GETARG_sJ, memset(), OP_ADD, OP_ADDI, OP_ADDK, OP_BAND, OP_BANDK, OP_BNOT, OP_BOR, OP_BORK, OP_BXOR, OP_BXORK, OP_CALL, OP_CLOSE, OP_CLOSURE, OP_CONCAT, OP_DIV, OP_DIVK, OP_EQ, OP_EQI, OP_EQK, OP_EXTRAARG, OP_FORLOOP, OP_FORPREP, OP_GEI, OP_GETFIELD, OP_GETI, OP_GETTABLE, OP_GETTABUP, OP_GETUPVAL, OP_GTI, OP_IDIV, OP_IDIVK, OP_JMP, OP_LE, OP_LEI, OP_LEN, OP_LFALSESKIP, OP_LOADF, OP_LOADFALSE, OP_LOADI, OP_LOADK, OP_LOADKX, OP_LOADNIL, OP_LOADTRUE, OP_LT, OP_LTI, OP_MMBIN, OP_MMBINI, OP_MMBINK, OP_MOD, OP_MODK, OP_MOVE, OP_MUL, OP_MULK, OP_NEWTABLE, OP_NOT, OP_POW, OP_POWK, OP_RETURN, OP_RETURN0, OP_RETURN1, OP_SELF, OP_SETFIELD, OP_SETI, OP_SETLIST, OP_SETTABLE, OP_SETTABUP, OP_SETUPVAL, OP_SHL, OP_SHLI, OP_SHR, OP_SHRI, OP_SUB, OP_SUBK, OP_TAILCALL, OP_TBC, OP_TEST, OP_TESTSET, OP_TFORCALL, OP_TFORLOOP, OP_TFORPREP, OP_UNM, OP_VARARG, OP_VARARGPREP, RZ_ANALYSIS_ADDR_HINT_TYPE_RET, RZ_ANALYSIS_OP_TYPE_ADD, RZ_ANALYSIS_OP_TYPE_AND, RZ_ANALYSIS_OP_TYPE_CALL, RZ_ANALYSIS_OP_TYPE_CJMP, RZ_ANALYSIS_OP_TYPE_CMOV, RZ_ANALYSIS_OP_TYPE_CPL, RZ_ANALYSIS_OP_TYPE_DIV, RZ_ANALYSIS_OP_TYPE_JMP, RZ_ANALYSIS_OP_TYPE_LOAD, RZ_ANALYSIS_OP_TYPE_MOD, RZ_ANALYSIS_OP_TYPE_MOV, RZ_ANALYSIS_OP_TYPE_MUL, RZ_ANALYSIS_OP_TYPE_NEW, RZ_ANALYSIS_OP_TYPE_NOT, RZ_ANALYSIS_OP_TYPE_OR, RZ_ANALYSIS_OP_TYPE_RCALL, RZ_ANALYSIS_OP_TYPE_RET, RZ_ANALYSIS_OP_TYPE_SHL, RZ_ANALYSIS_OP_TYPE_SHR, RZ_ANALYSIS_OP_TYPE_STORE, RZ_ANALYSIS_OP_TYPE_SUB, RZ_ANALYSIS_OP_TYPE_XOR, RZ_ANALYSIS_STACK_INC, and st32.

Referenced by rz_lua_analysis_op().

◆ lua54_assembly()

bool lua54_assembly ( const char *  input,
st32  input_size,
LuaInstruction instruction 
)

Definition at line 96 of file assembly_54.c.

96  {
97  const char *opcode_start; // point to the header
98  const char *opcode_end; // point to the first white space
99  int opcode_len;
100 
101  const char *arg_start;
102 
103  ut8 opcode;
105 
106  /* Find the opcode */
107  opcode_start = input;
108  opcode_end = strchr(input, ' ');
109  if (opcode_end == NULL) {
110  opcode_end = input + input_size;
111  }
112 
113  opcode_len = opcode_end - opcode_start;
114  opcode = get_lua54_opcode_by_name(opcode_start, opcode_len);
115 
116  /* Find the arguments */
117  arg_start = rz_str_trim_head_ro(opcode_end);
118 
119  /* Encode opcode and args */
120  switch (opcode) {
121  case OP_GETI:
122  case OP_MMBIN:
123  case OP_GETTABUP:
124  case OP_CALL:
125  case OP_GETTABLE:
126  case OP_ADD:
127  case OP_SUB:
128  case OP_MUL:
129  case OP_POW:
130  case OP_DIV:
131  case OP_IDIV:
132  case OP_BAND:
133  case OP_BOR:
134  case OP_SHL:
135  case OP_SHR:
136  case OP_ADDK:
137  case OP_SUBK:
138  case OP_MULK:
139  case OP_MODK:
140  case OP_POWK:
141  case OP_DIVK:
142  case OP_IDIVK:
143  case OP_BANDK:
144  case OP_BORK:
145  case OP_BXORK:
146  case OP_GETFIELD:
147  instruction = encode_instruction(opcode, arg_start,
148  PARAM_A | PARAM_B | PARAM_C,
149  3);
150  break;
151  // iABC k instruction
152  case OP_TAILCALL:
153  case OP_RETURN:
154  case OP_NEWTABLE:
155  case OP_SETLIST:
156  case OP_MMBINK:
157  case OP_SETTABUP:
158  case OP_SETTABLE:
159  case OP_SETI:
160  case OP_SETFIELD:
161  case OP_SELF:
162  instruction = encode_instruction(opcode, arg_start,
164  4);
165  break;
166  // AsBC k instruction
167  case OP_MMBINI:
168  instruction = encode_instruction(opcode, arg_start,
170  4);
171  break;
172  // ABsC
173  case OP_ADDI:
174  case OP_SHRI:
175  case OP_SHLI:
176  instruction = encode_instruction(opcode, arg_start,
178  3);
179  break;
180  // AB
181  case OP_MOVE:
182  case OP_UNM:
183  case OP_BNOT:
184  case OP_NOT:
185  case OP_LEN:
186  case OP_CONCAT:
187  case OP_LOADNIL:
188  case OP_GETUPVAL:
189  case OP_SETUPVAL:
190  instruction = encode_instruction(opcode, arg_start,
191  PARAM_A | PARAM_B,
192  2);
193  break;
194  // AB with k
195  case OP_EQ:
196  case OP_LT:
197  case OP_LE:
198  case OP_TESTSET:
199  case OP_EQK:
200  instruction = encode_instruction(opcode, arg_start,
201  PARAM_A | PARAM_B | PARAM_k,
202  3);
203  break;
204  // AsB with k
205  case OP_EQI:
206  case OP_LTI:
207  case OP_LEI:
208  case OP_GTI:
209  case OP_GEI:
210  instruction = encode_instruction(opcode, arg_start,
212  3);
213  break;
214  // AC
215  case OP_TFORCALL:
216  case OP_VARARG:
217  instruction = encode_instruction(opcode, arg_start,
218  PARAM_A | PARAM_C,
219  2);
220  break;
221  // A
222  case OP_LOADKX:
223  case OP_LOADFALSE:
224  case OP_LFALSESKIP:
225  case OP_LOADTRUE:
226  case OP_CLOSE:
227  case OP_TBC:
228  case OP_RETURN1:
229  case OP_VARARGPREP:
230  instruction = encode_instruction(opcode, arg_start,
231  PARAM_A,
232  1);
233  break;
234  // A with k
235  case OP_TEST:
236  instruction = encode_instruction(opcode, arg_start,
237  PARAM_A | PARAM_k,
238  2);
239  break;
240  // no arg
241  case OP_RETURN0:
243  break;
244  // A Bx
245  case OP_LOADK:
246  case OP_FORLOOP:
247  case OP_FORPREP:
248  case OP_TFORLOOP:
249  case OP_TFORPREP:
250  case OP_CLOSURE:
251  instruction = encode_instruction(opcode, arg_start,
252  PARAM_A | PARAM_Bx,
253  2);
254  break;
255  // A sBx
256  case OP_LOADI:
257  case OP_LOADF:
258  instruction = encode_instruction(opcode, arg_start,
259  PARAM_A | PARAM_sBx,
260  2);
261  break;
262  // Ax
263  case OP_EXTRAARG:
264  instruction = encode_instruction(opcode, arg_start,
265  PARAM_Ax,
266  1);
267  break;
268  // isJ
269  case OP_JMP:
270  instruction = encode_instruction(opcode, arg_start,
271  PARAM_sJ,
272  1);
273  break;
274  default:
275  return false;
276  }
277 
278  if (instruction == -1) {
279  return false;
280  }
281 
282  *instruction_p = instruction;
283  return true;
284 }
#define PARAM_k
Definition: arch_54.h:242
#define PARAM_sB
Definition: arch_54.h:241
#define PARAM_sC
Definition: arch_54.h:240
#define LUA_SET_OPCODE(i, o)
Definition: arch_54.h:199
#define PARAM_sJ
Definition: arch_54.h:239
static LuaInstruction encode_instruction(ut8 opcode, const char *arg_start, ut16 flag, ut8 arg_num)
Definition: assembly_54.c:6
ut8 get_lua54_opcode_by_name(const char *name, int len)
Definition: opcode_54.c:105

References encode_instruction(), get_lua54_opcode_by_name(), input(), LUA_SET_OPCODE, NULL, OP_ADD, OP_ADDI, OP_ADDK, OP_BAND, OP_BANDK, OP_BNOT, OP_BOR, OP_BORK, OP_BXORK, OP_CALL, OP_CLOSE, OP_CLOSURE, OP_CONCAT, OP_DIV, OP_DIVK, OP_EQ, OP_EQI, OP_EQK, OP_EXTRAARG, OP_FORLOOP, OP_FORPREP, OP_GEI, OP_GETFIELD, OP_GETI, OP_GETTABLE, OP_GETTABUP, OP_GETUPVAL, OP_GTI, OP_IDIV, OP_IDIVK, OP_JMP, OP_LE, OP_LEI, OP_LEN, OP_LFALSESKIP, OP_LOADF, OP_LOADFALSE, OP_LOADI, OP_LOADK, OP_LOADKX, OP_LOADNIL, OP_LOADTRUE, OP_LT, OP_LTI, OP_MMBIN, OP_MMBINI, OP_MMBINK, OP_MODK, OP_MOVE, OP_MUL, OP_MULK, OP_NEWTABLE, OP_NOT, OP_POW, OP_POWK, OP_RETURN, OP_RETURN0, OP_RETURN1, OP_SELF, OP_SETFIELD, OP_SETI, OP_SETLIST, OP_SETTABLE, OP_SETTABUP, OP_SETUPVAL, OP_SHL, OP_SHLI, OP_SHR, OP_SHRI, OP_SUB, OP_SUBK, OP_TAILCALL, OP_TBC, OP_TEST, OP_TESTSET, OP_TFORCALL, OP_TFORLOOP, OP_TFORPREP, OP_UNM, OP_VARARG, OP_VARARGPREP, PARAM_A, PARAM_Ax, PARAM_B, PARAM_Bx, PARAM_C, PARAM_k, PARAM_sB, PARAM_sBx, PARAM_sC, PARAM_sJ, and rz_str_trim_head_ro().

Referenced by rz_luac_asm().

◆ lua54_disasm()

int lua54_disasm ( RzAsmOp op,
const ut8 buf,
int  len,
LuaOpNameList  oplist 
)

Definition at line 6 of file disassembly_54.c.

6  {
7  if (len < 4) {
8  RZ_LOG_DEBUG("Cannot disassemble lua54 opcode (truncated).\n");
9  return 0;
10  }
11 
14 
15  /* Pre-fetch arguments */
16  int a = LUA_GETARG_A(instruction);
17  int b = LUA_GETARG_B(instruction);
18  int c = LUA_GETARG_C(instruction);
20  int bx = LUA_GETARG_Bx(instruction);
23  int sbx = LUA_GETARG_sBx(instruction);
24  int isk = LUA_GETARG_k(instruction);
25  int sj = LUA_GETARG_sJ(instruction);
26 
27  char *asm_string;
28 
29  switch (opcode) {
30  /* iABC Instruction */
31  case OP_GETI: /* A B C R[A] := R[B][C] */
32  case OP_MMBIN: /* A B C call C metamethod over R[A] and R[B] */
33  case OP_GETTABUP: /* A B C R[A] := UpValue[B][K[C]:string] */
34  case OP_CALL: /* A B C R[A], ... ,R[A+C-2] := R[A](R[A+1], ... ,R[A+B-1]) */
35  case OP_GETTABLE: /* A B C R[A] := R[B][R[C]] */
36  case OP_ADD: /* A B C R[A] := R[B] + R[C] */
37  case OP_SUB: /* A B C R[A] := R[B] - R[C] */
38  case OP_MUL: /* A B C R[A] := R[B] * R[C] */
39  case OP_MOD: /* A B C R[A] := R[B] % R[C] */
40  case OP_POW: /* A B C R[A] := R[B] ^ R[C] */
41  case OP_DIV: /* A B C R[A] := R[B] / R[C] */
42  case OP_IDIV: /* A B C R[A] := R[B] // R[C] */
43  case OP_BAND: /* A B C R[A] := R[B] & R[C] */
44  case OP_BOR: /* A B C R[A] := R[B] | R[C] */
45  case OP_BXOR: /* A B C R[A] := R[B] ~ R[C] */
46  case OP_SHL: /* A B C R[A] := R[B] << R[C] */
47  case OP_SHR: /* A B C R[A] := R[B] >> R[C] */
48  case OP_ADDK: /* A B C R[A] := R[B] + K[C] */
49  case OP_SUBK: /* A B C R[A] := R[B] - K[C] */
50  case OP_MULK: /* A B C R[A] := R[B] * K[C] */
51  case OP_MODK: /* A B C R[A] := R[B] % K[C] */
52  case OP_POWK: /* A B C R[A] := R[B] ^ K[C] */
53  case OP_DIVK: /* A B C R[A] := R[B] / K[C] */
54  case OP_IDIVK: /* A B C R[A] := R[B] // K[C] */
55  case OP_BANDK: /* A B C R[A] := R[B] & K[C]:integer */
56  case OP_BORK: /* A B C R[A] := R[B] | K[C]:integer */
57  case OP_BXORK: /* A B C R[A] := R[B] ~ K[C]:integer */
58  case OP_GETFIELD: /* A B C R[A] := R[B][K[C]:string] */
59  asm_string = luaop_new_str_3arg(opnames[opcode], a, b, c);
60  break;
61  /* iABC - k instructions */
62  case OP_TAILCALL: /* A B C k return R[A](R[A+1], ... ,R[A+B-1]) */
63  case OP_RETURN: /* A B C k return R[A], ... ,R[A+B-2] (see note) */
64  case OP_NEWTABLE: /* A B C k R[A] := {} */
65  case OP_SETLIST: /* A B C k R[A][C+i] := R[A+i], 1 <= i <= B */
66  case OP_MMBINK: /* A B C k call C metamethod over R[A] and K[B] */
67  case OP_SETTABUP: /* A B C UpValue[A][K[B]:string] := RK(C) */
68  case OP_SETTABLE: /* A B C R[A][R[B]] := RK(C) */
69  case OP_SETI: /* A B C R[A][B] := RK(C) */
70  case OP_SETFIELD: /* A B C R[A][K[B]:string] := RK(C) */
71  case OP_SELF: /* A B C R[A+1] := R[B]; R[A] := R[B][RK(C):string] */
72  asm_string = luaop_new_str_3arg_ex(opnames[opcode], a, b, c, isk);
73  break;
74  /* iABC - signed B with k instruction */
75  case OP_MMBINI: /* A sB C k call C metamethod over R[A] and sB */
76  asm_string = luaop_new_str_3arg_ex(opnames[opcode], a, sb, c, isk);
77  break;
78  /* iABC - c signed instructions */
79  case OP_ADDI: /* A B sC R[A] := R[B] + sC */
80  case OP_SHRI: /* A B sC R[A] := R[B] >> sC */
81  case OP_SHLI: /* A B sC R[A] := sC << R[B] */
82  asm_string = luaop_new_str_3arg(opnames[opcode], a, b, sc);
83  break;
84  /* iABC - A & B instructions */
85  case OP_MOVE: /* A B R[A] := R[B] */
86  case OP_UNM: /* A B R[A] := -R[B] */
87  case OP_BNOT: /* A B R[A] := ~R[B] */
88  case OP_NOT: /* A B R[A] := not R[B] */
89  case OP_LEN: /* A B R[A] := #R[B] (length operator) */
90  case OP_CONCAT: /* A B R[A] := R[A].. ... ..R[A + B - 1] */
91  case OP_LOADNIL: /* A B R[A], R[A+1], ..., R[A+B] := nil */
92  case OP_GETUPVAL: /* A B R[A] := UpValue[B] */
93  case OP_SETUPVAL: /* A B UpValue[B] := R[A] */
94  asm_string = luaop_new_str_2arg(opnames[opcode], a, b);
95  break;
96 
97  /* iABC - A & B with k instructions */
98  case OP_EQ: /* A B k if ((R[A] == R[B]) ~= k) then pc++ */
99  case OP_LT: /* A B k if ((R[A] < R[B]) ~= k) then pc++ */
100  case OP_LE: /* A B k if ((R[A] <= R[B]) ~= k) then pc++ */
101  case OP_TESTSET: /* A B k if (not R[B] == k) then pc++ else R[A] := R[B] */
102  case OP_EQK: /* A B k if ((R[A] == K[B]) ~= k) then pc++ */
103  asm_string = luaop_new_str_2arg_ex(opnames[opcode], a, b, isk);
104  break;
105  /* iABC - A & sB with k instructions */
106  case OP_EQI: /* A sB k if ((R[A] == sB) ~= k) then pc++ */
107  case OP_LTI: /* A sB k if ((R[A] < sB) ~= k) then pc++ */
108  case OP_LEI: /* A sB k if ((R[A] <= sB) ~= k) then pc++ */
109  case OP_GTI: /* A sB k if ((R[A] > sB) ~= k) then pc++ */
110  case OP_GEI: /* A sB k if ((R[A] >= sB) ~= k) then pc++ */
111  asm_string = luaop_new_str_2arg_ex(opnames[opcode], a, sb, isk);
112  break;
113 
114  /* iABC - A & C instructions */
115  case OP_TFORCALL: /* A C R[A+4], ... ,R[A+3+C] := R[A](R[A+1], R[A+2]); */
116  case OP_VARARG: /* A C R[A], R[A+1], ..., R[A+C-2] = vararg */
117  asm_string = luaop_new_str_2arg(opnames[opcode], a, c);
118  break;
119 
120  /* iABC - single A instructions */
121  case OP_LOADKX: /* A R[A] := K[extra arg] */
122  case OP_LOADFALSE: /* A R[A] := false */
123  case OP_LFALSESKIP: /* A R[A] := false; pc++ */
124  case OP_LOADTRUE: /* A R[A] := true */
125  case OP_CLOSE: /* A close all upvalues >= R[A] */
126  case OP_TBC: /* A mark variable A "to be closed" */
127  case OP_RETURN1: /* A return R[A] */
128  case OP_VARARGPREP: /* A (adjust vararg parameters) */
129  asm_string = luaop_new_str_1arg(opnames[opcode], a);
130  break;
131 
132  /* iABC - special instructions */
133  case OP_TEST: /* A k if (not R[A] == k) then pc++ */
134  asm_string = luaop_new_str_1arg_ex(opnames[opcode], a, isk);
135  break;
136 
137  case OP_RETURN0: /* return */
138  asm_string = rz_str_newf("RETURN0");
139  break;
140 
141  /* iABx instructions */
142  case OP_LOADK: /* A Bx R[A] := K[Bx] */
143  case OP_FORLOOP: /* A Bx update counters; if loop continues then pc-=Bx; */
144  case OP_FORPREP: /* A Bx <check values and prepare counters>;
145  if not to run then pc+=Bx+1; */
146  case OP_TFORPREP: /* A Bx create upvalue for R[A + 3]; pc+=Bx */
147  case OP_TFORLOOP: /* A Bx if R[A+2] ~= nil then { R[A]=R[A+2]; pc -= Bx } */
148  case OP_CLOSURE: /* A Bx R[A] := closure(KPROTO[Bx]) */
149  asm_string = luaop_new_str_2arg(opnames[opcode], a, bx);
150  break;
151 
152  /* iAsBx instructions */
153  case OP_LOADI: /* A sBx R[A] := sBx */
154  case OP_LOADF: /* A sBx R[A] := (lua_Number)sBx */
155  asm_string = luaop_new_str_2arg(opnames[opcode], a, sbx);
156  break;
157 
158  /* iAx instructions */
159  case OP_EXTRAARG: /* Ax extra (larger) argument for previous opcode */
160  asm_string = luaop_new_str_1arg(opnames[opcode], ax);
161  break;
162 
163  /* isJ instructions */
164  case OP_JMP: /* sJ pc += sJ */
165  asm_string = luaop_new_str_1arg(opnames[opcode], sj);
166  break;
167 
168  default:
169  asm_string = rz_str_newf("invalid");
170  }
171 
172  rz_strbuf_append(&op->buf_asm, asm_string);
173  op->size = 4;
174  RZ_FREE(asm_string);
175  return 4;
176 }
#define LUA_GETARG_B(i)
Definition: arch_54.h:208
#define LUA_GETARG_sC(i)
Definition: arch_54.h:214
#define LUA_GETARG_A(i)
Definition: arch_54.h:207
#define LUA_GETARG_sBx(i)
Definition: arch_54.h:212
#define LUA_GETARG_Ax(i)
Definition: arch_54.h:211
#define LUA_GETARG_k(i)
Definition: arch_54.h:217
#define LUA_GETARG_sB(i)
Definition: arch_54.h:215
#define LUA_GETARG_C(i)
Definition: arch_54.h:209
static SblHeader sb
Definition: bin_mbn.c:26
static char sc[]
Definition: egg_cb.c:6
char * luaop_new_str_2arg_ex(char *opname, int a, int b, int isk)
Definition: lua_arch.c:76
char * luaop_new_str_3arg_ex(char *opname, int a, int b, int c, int isk)
Definition: lua_arch.c:65
char * luaop_new_str_1arg_ex(char *opname, int a, int isk)
Definition: lua_arch.c:87
RZ_API char * rz_str_newf(const char *fmt,...) RZ_PRINTF_CHECK(1

References a, ax, b, c, len, lua_build_instruction(), LUA_GET_OPCODE, LUA_GETARG_A, LUA_GETARG_Ax, LUA_GETARG_B, LUA_GETARG_Bx, LUA_GETARG_C, LUA_GETARG_k, LUA_GETARG_sB, LUA_GETARG_sBx, LUA_GETARG_sC, LUA_GETARG_sJ, luaop_new_str_1arg(), luaop_new_str_1arg_ex(), luaop_new_str_2arg(), luaop_new_str_2arg_ex(), luaop_new_str_3arg(), luaop_new_str_3arg_ex(), OP_ADD, OP_ADDI, OP_ADDK, OP_BAND, OP_BANDK, OP_BNOT, OP_BOR, OP_BORK, OP_BXOR, OP_BXORK, OP_CALL, OP_CLOSE, OP_CLOSURE, OP_CONCAT, OP_DIV, OP_DIVK, OP_EQ, OP_EQI, OP_EQK, OP_EXTRAARG, OP_FORLOOP, OP_FORPREP, OP_GEI, OP_GETFIELD, OP_GETI, OP_GETTABLE, OP_GETTABUP, OP_GETUPVAL, OP_GTI, OP_IDIV, OP_IDIVK, OP_JMP, OP_LE, OP_LEI, OP_LEN, OP_LFALSESKIP, OP_LOADF, OP_LOADFALSE, OP_LOADI, OP_LOADK, OP_LOADKX, OP_LOADNIL, OP_LOADTRUE, OP_LT, OP_LTI, OP_MMBIN, OP_MMBINI, OP_MMBINK, OP_MOD, OP_MODK, OP_MOVE, OP_MUL, OP_MULK, OP_NEWTABLE, OP_NOT, OP_POW, OP_POWK, OP_RETURN, OP_RETURN0, OP_RETURN1, OP_SELF, OP_SETFIELD, OP_SETI, OP_SETLIST, OP_SETTABLE, OP_SETTABUP, OP_SETUPVAL, OP_SHL, OP_SHLI, OP_SHR, OP_SHRI, OP_SUB, OP_SUBK, OP_TAILCALL, OP_TBC, OP_TEST, OP_TESTSET, OP_TFORCALL, OP_TFORLOOP, OP_TFORPREP, OP_UNM, OP_VARARG, OP_VARARGPREP, RZ_FREE, RZ_LOG_DEBUG, rz_str_newf(), rz_strbuf_append(), sb, and sc.

Referenced by rz_luac_disasm().

◆ lua_build_instruction()

LuaInstruction lua_build_instruction ( const ut8 buf)

Definition at line 6 of file lua_arch.c.

6  {
7  LuaInstruction ret = 0;
8  ret |= buf[3] << 24;
9  ret |= buf[2] << 16;
10  ret |= buf[1] << 8;
11  ret |= buf[0];
12  return ret;
13 }

Referenced by lua53_anal_op(), lua53_disasm(), lua54_anal_op(), and lua54_disasm().

◆ lua_convert_str_to_num()

int lua_convert_str_to_num ( const char *  str)

Definition at line 137 of file lua_arch.c.

137  {
138  return strtoll(str, NULL, 0);
139 }

References NULL, and cmd_descs_generate::str.

Referenced by encode_instruction().

◆ lua_is_valid_num_value_string()

bool lua_is_valid_num_value_string ( const char *  str)

Definition at line 129 of file lua_arch.c.

129  {
131  RZ_LOG_ERROR("assembler: lua: %s is not a valid number argument\n", str);
132  return false;
133  }
134  return true;
135 }
RZ_API bool rz_is_valid_input_num_value(RzNum *num, const char *input_value)
Definition: unum.c:735

References NULL, rz_is_valid_input_num_value(), RZ_LOG_ERROR, and cmd_descs_generate::str.

Referenced by encode_instruction().

◆ lua_load_next_arg_start()

int lua_load_next_arg_start ( const char *  raw_string,
char *  recv_buf 
)

Definition at line 98 of file lua_arch.c.

98  {
99  if (!raw_string) {
100  return 0;
101  }
102 
103  const char *arg_start = NULL;
104  const char *arg_end = NULL;
105  int arg_len = 0;
106 
107  /* locate the start point */
108  arg_start = rz_str_trim_head_ro(raw_string);
109  if (strlen(arg_start) == 0) {
110  return 0;
111  }
112 
113  arg_end = strchr(arg_start, ' ');
114  if (arg_end == NULL) {
115  /* is last arg */
116  arg_len = strlen(arg_start);
117  } else {
118  arg_len = arg_end - arg_start;
119  }
120 
121  /* Set NUL */
122  memcpy(recv_buf, arg_start, arg_len);
123  recv_buf[arg_len] = 0x00;
124 
125  /* Calculate offset */
126  return arg_start - raw_string + arg_len;
127 }
memcpy(mem, inblock.get(), min(CONTAINING_RECORD(inblock.get(), MEMBLOCK, data) ->size, size))

References memcpy(), NULL, and rz_str_trim_head_ro().

Referenced by encode_instruction().

◆ lua_set_instruction()

void lua_set_instruction ( LuaInstruction  instruction,
ut8 data 
)

Definition at line 15 of file lua_arch.c.

15  {
16  data[3] = instruction >> 24;
17  data[2] = instruction >> 16;
18  data[1] = instruction >> 8;
19  data[0] = instruction >> 0;
20 }

Referenced by rz_luac_asm().

◆ luaop_new_str_1arg()

char* luaop_new_str_1arg ( char *  opname,
int  a 
)

Definition at line 53 of file lua_arch.c.

53  {
54  char *asm_string;
55 
56  asm_string = rz_str_newf(
57  "%s %d",
58  opname,
59  a);
60 
61  return asm_string;
62 }

References a, and rz_str_newf().

Referenced by lua53_disasm(), and lua54_disasm().

◆ luaop_new_str_1arg_ex()

char* luaop_new_str_1arg_ex ( char *  opname,
int  a,
int  isk 
)

Definition at line 87 of file lua_arch.c.

87  {
88  char *asm_string;
89 
90  asm_string = rz_str_newf(
91  "%s %d %d",
92  opname,
93  a, isk);
94 
95  return asm_string;
96 }

References a, and rz_str_newf().

Referenced by lua54_disasm().

◆ luaop_new_str_2arg()

char* luaop_new_str_2arg ( char *  opname,
int  a,
int  b 
)

Definition at line 42 of file lua_arch.c.

42  {
43  char *asm_string;
44 
45  asm_string = rz_str_newf(
46  "%s %d %d",
47  opname,
48  a, b);
49 
50  return asm_string;
51 }

References a, b, and rz_str_newf().

Referenced by lua53_disasm(), and lua54_disasm().

◆ luaop_new_str_2arg_ex()

char* luaop_new_str_2arg_ex ( char *  opname,
int  a,
int  b,
int  isk 
)

Definition at line 76 of file lua_arch.c.

76  {
77  char *asm_string;
78 
79  asm_string = rz_str_newf(
80  "%s %d %d %d",
81  opname,
82  a, b, isk);
83 
84  return asm_string;
85 }

References a, b, and rz_str_newf().

Referenced by lua54_disasm().

◆ luaop_new_str_3arg()

char* luaop_new_str_3arg ( char *  opname,
int  a,
int  b,
int  c 
)

Definition at line 31 of file lua_arch.c.

31  {
32  char *asm_string;
33 
34  asm_string = rz_str_newf(
35  "%s %d %d %d",
36  opname,
37  a, b, c);
38 
39  return asm_string;
40 }

References a, b, c, and rz_str_newf().

Referenced by lua53_disasm(), and lua54_disasm().

◆ luaop_new_str_3arg_ex()

char* luaop_new_str_3arg_ex ( char *  opname,
int  a,
int  b,
int  c,
int  isk 
)

Definition at line 65 of file lua_arch.c.

65  {
66  char *asm_string;
67 
68  asm_string = rz_str_newf(
69  "%s %d %d %d %d",
70  opname,
71  a, b, c, isk);
72 
73  return asm_string;
74 }

References a, b, c, and rz_str_newf().

Referenced by lua54_disasm().