1 ----------------------------------------------------------------------------
2 -- LuaJIT ARM disassembler module.
4 -- Copyright (C) 2005-2012 Mike Pall. All rights reserved.
5 -- Released under the MIT license. See Copyright Notice in luajit.h
6 ----------------------------------------------------------------------------
7 -- This is a helper module used by the LuaJIT machine code dumper module.
9 -- It disassembles most user-mode ARMv7 instructions
10 -- NYI: Advanced SIMD and VFP instructions.
11 ------------------------------------------------------------------------------
14 local sub
, byte
, format = string.sub
, string.byte
, string.format
15 local match
, gmatch
, gsub = string.match
, string.gmatch
, string.gsub
16 local concat
= table.concat
17 local bit
= require("bit")
18 local band
, bor
, ror
, tohex
= bit
.band
, bit
.bor
, bit
.ror
, bit
.tohex
19 local lshift
, rshift
, arshift
= bit
.lshift
, bit
.rshift
, bit
.arshift
21 ------------------------------------------------------------------------------
23 ------------------------------------------------------------------------------
28 shift
= 0, mask
= 0 -- NYI VFP load/store.
31 shift
= 0, mask
= 0 -- NYI ldc, mcrr, mrrc.
40 shift
= 0, mask
= 0 -- NYI VFP data.
43 shift
= 0, mask
= 0 -- NYI cdp, mcr, mrc.
50 shift
= 0, mask
= 0, -- NYI unconditional CP load/store.
54 shift
= 0, mask
= 0, -- NYI unconditional CP data.
57 local map_simddata
= {
58 shift
= 0, mask
= 0, -- NYI SIMD data.
61 local map_simdload
= {
62 shift
= 0, mask
= 0, -- NYI SIMD load/store, preload.
66 shift
= 0, mask
= 0, -- NYI preload.
70 shift
= 20, mask
= 31,
74 [0] = "sadd16DNM", "sasxDNM", "ssaxDNM", "ssub16DNM",
75 "sadd8DNM", false, false, "ssub8DNM",
79 [0] = "qadd16DNM", "qasxDNM", "qsaxDNM", "qsub16DNM",
80 "qadd8DNM", false, false, "qsub8DNM",
84 [0] = "shadd16DNM", "shasxDNM", "shsaxDNM", "shsub16DNM",
85 "shadd8DNM", false, false, "shsub8DNM",
90 [0] = "uadd16DNM", "uasxDNM", "usaxDNM", "usub16DNM",
91 "uadd8DNM", false, false, "usub8DNM",
95 [0] = "uqadd16DNM", "uqasxDNM", "uqsaxDNM", "uqsub16DNM",
96 "uqadd8DNM", false, false, "uqsub8DNM",
100 [0] = "uhadd16DNM", "uhasxDNM", "uhsaxDNM", "uhsub16DNM",
101 "uhadd8DNM", false, false, "uhsub8DNM",
105 [0] = "pkhbtDNMU", false, "pkhtbDNMU",
106 { shift
= 16, mask
= 15, [15] = "sxtb16DMU", _
= "sxtab16DNMU", },
107 "pkhbtDNMU", "selDNM", "pkhtbDNMU",
112 [0] = "ssatDxMu", "ssat16DxM", "ssatDxMu",
113 { shift
= 16, mask
= 15, [15] = "sxtbDMU", _
= "sxtabDNMU", },
114 "ssatDxMu", false, "ssatDxMu",
118 [0] = "ssatDxMu", "revDM", "ssatDxMu",
119 { shift
= 16, mask
= 15, [15] = "sxthDMU", _
= "sxtahDNMU", },
120 "ssatDxMu", "rev16DM", "ssatDxMu",
124 [3] = { shift
= 16, mask
= 15, [15] = "uxtb16DMU", _
= "uxtab16DNMU", },
129 [0] = "usatDwMu", "usat16DwM", "usatDwMu",
130 { shift
= 16, mask
= 15, [15] = "uxtbDMU", _
= "uxtabDNMU", },
131 "usatDwMu", false, "usatDwMu",
135 [0] = "usatDwMu", "rbitDM", "usatDwMu",
136 { shift
= 16, mask
= 15, [15] = "uxthDMU", _
= "uxtahDNMU", },
137 "usatDwMu", "revshDM", "usatDwMu",
140 shift
= 12, mask
= 15,
143 "smuadNMS", "smuadxNMS", "smusdNMS", "smusdxNMS",
147 [0] = "smladNMSD", "smladxNMSD", "smlsdNMSD", "smlsdxNMSD",
153 [0] = "smlaldDNMS", "smlaldxDNMS", "smlsldDNMS", "smlsldxDNMS",
157 [0] = { shift
= 12, mask
= 15, [15] = "smmulNMS", _
= "smmlaNMSD", },
158 { shift
= 12, mask
= 15, [15] = "smmulrNMS", _
= "smmlarNMSD", },
159 false, false, false, false,
160 "smmlsNMSD", "smmlsrNMSD",
165 [0] = { shift
= 12, mask
= 15, [15] = "usad8NMS", _
= "usada8NMSD", },
169 shift
= 5, mask
= 3, [2] = "sbfxDMvw",
172 shift
= 5, mask
= 3, [2] = "sbfxDMvw",
176 [0] = { shift
= 0, mask
= 15, [15] = "bfcDvX", _
= "bfiDMvX", },
180 [0] = { shift
= 0, mask
= 15, [15] = "bfcDvX", _
= "bfiDMvX", },
183 shift
= 5, mask
= 3, [2] = "ubfxDMvw",
186 shift
= 5, mask
= 3, [2] = "ubfxDMvw",
191 shift
= 21, mask
= 9,
193 shift
= 20, mask
= 5,
194 [0] = "strtDL", "ldrtDL", [4] = "strbtDL", [5] = "ldrbtDL",
197 shift
= 20, mask
= 5,
198 [0] = "strDL", "ldrDL", [4] = "strbDL", [5] = "ldrbDL",
204 [0] = map_load
, map_media
,
208 shift
= 20, mask
= 1,
210 shift
= 23, mask
= 3,
211 [0] = "stmdaNR", "stmNR",
212 { shift
= 16, mask
= 63, [45] = "pushR", _
= "stmdbNR", }, "stmibNR",
215 shift
= 23, mask
= 3,
216 [0] = "ldmdaNR", { shift
= 16, mask
= 63, [61] = "popR", _
= "ldmNR", },
217 "ldmdbNR", "ldmibNR",
222 shift
= 21, mask
= 15,
223 [0] = "andDNPs", "eorDNPs", "subDNPs", "rsbDNPs",
224 "addDNPs", "adcDNPs", "sbcDNPs", "rscDNPs",
225 "tstNP", "teqNP", "cmpNP", "cmnNP",
226 "orrDNPs", "movDPs", "bicDNPs", "mvnDPs",
230 shift
= 21, mask
= 7,
231 [0] = "mulNMSs", "mlaNMSDs", "umaalDNMS", "mlsDNMS",
232 "umullDNMSs", "umlalDNMSs", "smullDNMSs", "smlalDNMSs",
236 shift
= 20, mask
= 15, -- NYI: brackets around N. R(D+1) for ldrexd/strexd.
237 [0] = "swpDMN", false, false, false,
238 "swpbDMN", false, false, false,
239 "strexDMN", "ldrexDN", "strexdDN", "ldrexdDN",
240 "strexbDMN", "ldrexbDN", "strexhDN", "ldrexhDN",
244 shift
= 21, mask
= 3,
245 [0] = { shift
= 5, mask
= 3,
246 [0] = "smlabbNMSD", "smlatbNMSD", "smlabtNMSD", "smlattNMSD", },
247 { shift
= 5, mask
= 3,
248 [0] = "smlawbNMSD", "smulwbNMS", "smlawtNMSD", "smulwtNMS", },
249 { shift
= 5, mask
= 3,
250 [0] = "smlalbbDNMS", "smlaltbDNMS", "smlalbtDNMS", "smlalttDNMS", },
251 { shift
= 5, mask
= 3,
252 [0] = "smulbbNMS", "smultbNMS", "smulbtNMS", "smulttNMS", },
257 -- NYI: decode PSR bits of msr.
258 [0] = { shift
= 21, mask
= 1, [0] = "mrsD", "msrM", },
259 { shift
= 21, mask
= 3, "bxM", false, "clzDM", },
260 { shift
= 21, mask
= 3, "bxjM", },
261 { shift
= 21, mask
= 3, "blxM", },
263 { shift
= 21, mask
= 3, [0] = "qaddDMN", "qsubDMN", "qdaddDMN", "qdsubDMN", },
265 { shift
= 21, mask
= 3, "bkptK", },
272 [0] = { shift
= 24, mask
= 1, [0] = map_mul
, map_sync
, },
273 { shift
= 20, mask
= 1, [0] = "strhDL", "ldrhDL", },
274 { shift
= 20, mask
= 1, [0] = "ldrdDL", "ldrsbDL", },
275 { shift
= 20, mask
= 1, [0] = "strdDL", "ldrshDL", },
278 shift
= 20, mask
= 25,
279 [16] = { shift
= 7, mask
= 1, [0] = map_misc
, map_mulh
, },
281 shift
= 0, mask
= 0xffffffff,
282 [bor(0xe1a00000)] = "nop",
289 shift
= 20, mask
= 31, -- NYI: decode PSR bits of msr. Decode imm12.
290 [16] = "movwDW", [20] = "movtDW",
291 [18] = { shift
= 0, mask
= 0xf00ff, [0] = "nopv6", _
= "msrNW", },
297 shift
= 24, mask
= 1,
301 local map_condins
= {
302 [0] = map_datar
, map_datai
, map_load
, map_load1
,
303 map_loadm
, map_branch
, map_loadc
, map_datac
307 local map_uncondins
= {
308 [0] = false, map_simddata
, map_simdload
, map_preload
,
309 false, "blxB", map_loadcu
, map_datacu
,
312 ------------------------------------------------------------------------------
315 [0] = "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
316 "r8", "r9", "r10", "r11", "r12", "sp", "lr", "pc",
320 [0] = "eq", "ne", "hs", "lo", "mi", "pl", "vs", "vc",
321 "hi", "ls", "ge", "lt", "gt", "le", "al",
324 local map_shift
= { [0] = "lsl", "lsr", "asr", "ror", }
326 ------------------------------------------------------------------------------
328 -- Output a nicely formatted line with an opcode and operands.
329 local function putop(ctx
, text
, operands
)
333 local sym
= ctx
.symtab
[ctx
.rel
]
336 elseif band(ctx
.op
, 0x0e000000) ~= 0x0a000000 then
337 extra
= "\t; 0x"..tohex(ctx
.rel
)
340 if ctx
.hexdump
> 0 then
341 ctx
.out(format("%08x %s %-5s %s%s\n",
342 ctx
.addr
+pos
, tohex(ctx
.op
), text
, concat(operands
, ", "), extra
))
344 ctx
.out(format("%08x %-5s %s%s\n",
345 ctx
.addr
+pos
, text
, concat(operands
, ", "), extra
))
350 -- Fallback for unknown opcodes.
351 local function unknown(ctx
)
352 return putop(ctx
, ".long", { "0x"..tohex(ctx
.op
) })
355 -- Format operand 2 of load/store opcodes.
356 local function fmtload(ctx
, op
, pos
)
357 local base
= map_gpr
[band(rshift(op
, 16), 15)]
359 local ext
= (band(op
, 0x04000000) == 0)
360 if not ext
and band(op
, 0x02000000) == 0 then
362 if band(op
, 0x00800000) == 0 then ofs
= -ofs
end
363 if base
== "pc" then ctx
.rel
= ctx
.addr
+ pos
+ 8 + ofs
end
365 elseif ext
and band(op
, 0x00400000) ~= 0 then
366 ofs
= band(op
, 15) + band(rshift(op
, 4), 0xf0)
367 if band(op
, 0x00800000) == 0 then ofs
= -ofs
end
368 if base
== "pc" then ctx
.rel
= ctx
.addr
+ pos
+ 8 + ofs
end
371 ofs
= map_gpr
[band(op
, 15)]
372 if ext
or band(op
, 0xfe0) == 0 then
373 elseif band(op
, 0xfe0) == 0x60 then
374 ofs
= format("%s, rrx", ofs
)
376 local sh
= band(rshift(op
, 7), 31)
377 if sh
== 0 then sh
= 32 end
378 ofs
= format("%s, %s #%d", ofs
, map_shift
[band(rshift(op
, 5), 3)], sh
)
380 if band(op
, 0x00800000) == 0 then ofs
= "-"..ofs
end
383 x
= format("[%s]", base
)
384 elseif band(op
, 0x01000000) == 0 then
385 x
= format("[%s], %s", base
, ofs
)
387 x
= format("[%s, %s]", base
, ofs
)
389 if band(op
, 0x01200000) == 0x01200000 then x
= x
.."!" end
393 -- Disassemble a single instruction.
394 local function disass_ins(ctx
)
396 local b0
, b1
, b2
, b3
= byte(ctx
.code
, pos
+1, pos
+4)
397 local op
= bor(lshift(b3
, 24), lshift(b2
, 16), lshift(b1
, 8), b0
)
400 local last
, name
, pat
404 local cond
= rshift(op
, 28)
407 opat
= map_uncondins
[band(rshift(op
, 25), 7)]
409 if cond
~= 14 then suffix
= map_cond
[cond
] end
410 opat
= map_condins
[band(rshift(op
, 25), 7)]
412 while type(opat
) ~= "string" do
413 if not opat
then return unknown(ctx
) end
414 opat
= opat
[band(rshift(op
, opat
.shift
), opat
.mask
)] or opat
._
416 name
, pat
= match(opat
, "^([a-z0-9]*)(.*)")
418 for p
in gmatch(pat
, ".") do
421 x
= map_gpr
[band(rshift(op
, 12), 15)]
423 x
= map_gpr
[band(rshift(op
, 16), 15)]
425 x
= map_gpr
[band(rshift(op
, 8), 15)]
427 x
= map_gpr
[band(op
, 15)]
429 if band(op
, 0x02000000) ~= 0 then
430 x
= ror(band(op
, 255), 2*band(rshift(op
, 8), 15))
432 x
= map_gpr
[band(op
, 15)]
433 if band(op
, 0xff0) ~= 0 then
434 operands
[#operands
+1] = x
435 local s
= map_shift
[band(rshift(op
, 5), 3)]
437 if band(op
, 0xf90) == 0 then
438 if s
== "ror" then s
= "rrx" else r
= "#32" end
439 elseif band(op
, 0x10) == 0 then
440 r
= "#"..band(rshift(op
, 7), 31)
442 r
= map_gpr
[band(rshift(op
, 8), 15)]
444 if name
== "mov" then name
= s
; x
= r
445 elseif r
then x
= format("%s %s", s
, r
)
450 x
= fmtload(ctx
, op
, pos
, false)
452 local addr
= ctx
.addr
+ pos
+ 8 + arshift(lshift(op
, 8), 6)
453 if cond
== 15 then addr
= addr
+ band(rshift(op
, 23), 2) end
455 x
= "0x"..tohex(addr
)
457 if band(op
, 0x00200000) ~= 0 and #operands
== 1 then
458 operands
[1] = operands
[1].."!"
462 if band(rshift(op
, i
), 1) == 1 then t
[#t
+1] = map_gpr
[i
] end
464 x
= "{"..concat(t
, ", ").."}"
466 x
= band(op
, 0x0fff) + band(rshift(op
, 4), 0xf000)
468 x
= "#0x"..tohex(band(op
, 0x00ffffff), 6)
470 x
= band(rshift(op
, 7), 31)
471 if x
== 0 then x
= nil end
473 x
= band(rshift(op
, 7), 31)
474 if band(op
, 0x40) == 0 then
475 if x
== 0 then x
= nil else x
= "lsl #"..x
end
477 if x
== 0 then x
= "asr #32" else x
= "asr #"..x
end
480 x
= band(rshift(op
, 7), 31)
482 x
= band(rshift(op
, 16), 31)
484 x
= band(rshift(op
, 16), 31) + 1
486 x
= band(rshift(op
, 16), 31) - last
+ 1
488 x
= "#0x"..tohex(band(rshift(op
, 4), 0x0000fff0) + band(op
, 15), 4)
490 if band(op
, 0x00100000) ~= 0 then suffix
= "s"..suffix
end
496 if type(x
) == "number" then x
= "#"..x
end
497 operands
[#operands
+1] = x
501 return putop(ctx
, name
..suffix
, operands
)
504 ------------------------------------------------------------------------------
506 -- Disassemble a block of code.
507 local function disass_block(ctx
, ofs
, len
)
508 if not ofs
then ofs
= 0 end
509 local stop
= len
and ofs
+len
or #ctx
.code
512 while ctx
.pos
< stop
do disass_ins(ctx
) end
515 -- Extended API: create a disassembler context. Then call ctx:disass(ofs, len).
516 local function create_(code
, addr
, out
)
520 ctx
.out
= out
or io
.write
522 ctx
.disass
= disass_block
527 -- Simple API: disassemble code (a string) at address and output via out.
528 local function disass_(code
, addr
, out
)
529 create_(code
, addr
, out
):disass()
532 -- Return register name for RID.
533 local function regname_(r
)
537 -- Public module functions.