Rizin
unix-like reverse engineering framework and cli tools
disassembly_54.c File Reference
#include "arch_54.h"

Go to the source code of this file.

Functions

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

Function Documentation

◆ lua54_disasm()

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

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 }
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
LuaOpCode
Definition: arch_53.h:62
@ 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_BXOR
Definition: arch_53.h:93
@ OP_TAILCALL
Definition: arch_53.h:112
#define LUA_GET_OPCODE(i)
Definition: arch_54.h:198
#define LUA_GETARG_B(i)
Definition: arch_54.h:208
#define LUA_GETARG_sC(i)
Definition: arch_54.h:214
@ 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
#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_sJ(i)
Definition: arch_54.h:213
#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
#define LUA_GETARG_Bx(i)
Definition: arch_54.h:210
static SblHeader sb
Definition: bin_mbn.c:26
static char sc[]
Definition: egg_cb.c:6
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_2arg_ex(char *opname, int a, int b, int isk)
Definition: lua_arch.c:76
char * luaop_new_str_3arg(char *opname, int a, int b, int c)
Definition: lua_arch.c:31
char * luaop_new_str_3arg_ex(char *opname, int a, int b, int c, int isk)
Definition: lua_arch.c:65
LuaInstruction lua_build_instruction(const ut8 *buf)
Definition: lua_arch.c:6
char * luaop_new_str_1arg(char *opname, int a)
Definition: lua_arch.c:53
char * luaop_new_str_1arg_ex(char *opname, int a, int isk)
Definition: lua_arch.c:87
ut32 LuaInstruction
Definition: lua_arch.h:27
#define OP_CALL
Definition: nios2.h:263
#define OP_ADDI
Definition: nios2.h:253
#define RZ_LOG_DEBUG(fmtstr,...)
Definition: rz_log.h:49
RZ_API char * rz_str_newf(const char *fmt,...) RZ_PRINTF_CHECK(1
RZ_API bool rz_strbuf_append(RzStrBuf *sb, const char *s)
Definition: strbuf.c:222
#define RZ_FREE(x)
Definition: rz_types.h:369
#define b(i)
Definition: sha256.c:42
#define c(i)
Definition: sha256.c:43
#define a(i)
Definition: sha256.c:41
Definition: dis.c:32

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().