metasm 1.0.1 → 1.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +1 -0
- data/.hgtags +3 -0
- data/Gemfile +1 -0
- data/INSTALL +61 -0
- data/LICENCE +458 -0
- data/README +29 -21
- data/Rakefile +10 -0
- data/TODO +10 -12
- data/doc/code_organisation.txt +2 -0
- data/doc/core/DynLdr.txt +247 -0
- data/doc/core/ExeFormat.txt +43 -0
- data/doc/core/Expression.txt +220 -0
- data/doc/core/GNUExports.txt +27 -0
- data/doc/core/Ia32.txt +236 -0
- data/doc/core/SerialStruct.txt +108 -0
- data/doc/core/VirtualString.txt +145 -0
- data/doc/core/WindowsExports.txt +61 -0
- data/doc/core/index.txt +1 -0
- data/doc/style.css +6 -3
- data/doc/usage/debugger.txt +327 -0
- data/doc/usage/index.txt +1 -0
- data/doc/use_cases.txt +2 -2
- data/metasm.gemspec +22 -0
- data/{lib/metasm.rb → metasm.rb} +11 -3
- data/{lib/metasm → metasm}/compile_c.rb +13 -7
- data/metasm/cpu/arc.rb +8 -0
- data/metasm/cpu/arc/decode.rb +425 -0
- data/metasm/cpu/arc/main.rb +191 -0
- data/metasm/cpu/arc/opcodes.rb +588 -0
- data/{lib/metasm → metasm/cpu}/arm.rb +7 -5
- data/{lib/metasm → metasm/cpu}/arm/debug.rb +2 -2
- data/{lib/metasm → metasm/cpu}/arm/decode.rb +13 -12
- data/{lib/metasm → metasm/cpu}/arm/encode.rb +23 -8
- data/{lib/metasm → metasm/cpu}/arm/main.rb +0 -3
- data/metasm/cpu/arm/opcodes.rb +324 -0
- data/{lib/metasm → metasm/cpu}/arm/parse.rb +25 -13
- data/{lib/metasm → metasm/cpu}/arm/render.rb +2 -2
- data/metasm/cpu/arm64.rb +15 -0
- data/metasm/cpu/arm64/debug.rb +38 -0
- data/metasm/cpu/arm64/decode.rb +289 -0
- data/metasm/cpu/arm64/encode.rb +41 -0
- data/metasm/cpu/arm64/main.rb +105 -0
- data/metasm/cpu/arm64/opcodes.rb +232 -0
- data/metasm/cpu/arm64/parse.rb +20 -0
- data/metasm/cpu/arm64/render.rb +95 -0
- data/{lib/metasm/ppc.rb → metasm/cpu/bpf.rb} +2 -4
- data/metasm/cpu/bpf/decode.rb +142 -0
- data/metasm/cpu/bpf/main.rb +60 -0
- data/metasm/cpu/bpf/opcodes.rb +81 -0
- data/metasm/cpu/bpf/render.rb +41 -0
- data/metasm/cpu/cy16.rb +9 -0
- data/metasm/cpu/cy16/decode.rb +253 -0
- data/metasm/cpu/cy16/main.rb +63 -0
- data/metasm/cpu/cy16/opcodes.rb +78 -0
- data/metasm/cpu/cy16/render.rb +41 -0
- data/metasm/cpu/dalvik.rb +11 -0
- data/{lib/metasm → metasm/cpu}/dalvik/decode.rb +35 -13
- data/{lib/metasm → metasm/cpu}/dalvik/main.rb +51 -2
- data/{lib/metasm → metasm/cpu}/dalvik/opcodes.rb +19 -11
- data/metasm/cpu/ia32.rb +17 -0
- data/{lib/metasm → metasm/cpu}/ia32/compile_c.rb +5 -7
- data/{lib/metasm → metasm/cpu}/ia32/debug.rb +5 -5
- data/{lib/metasm → metasm/cpu}/ia32/decode.rb +246 -59
- data/{lib/metasm → metasm/cpu}/ia32/decompile.rb +7 -7
- data/{lib/metasm → metasm/cpu}/ia32/encode.rb +19 -13
- data/{lib/metasm → metasm/cpu}/ia32/main.rb +51 -8
- data/metasm/cpu/ia32/opcodes.rb +1424 -0
- data/{lib/metasm → metasm/cpu}/ia32/parse.rb +47 -16
- data/{lib/metasm → metasm/cpu}/ia32/render.rb +31 -4
- data/metasm/cpu/mips.rb +14 -0
- data/{lib/metasm → metasm/cpu}/mips/compile_c.rb +1 -1
- data/metasm/cpu/mips/debug.rb +42 -0
- data/{lib/metasm → metasm/cpu}/mips/decode.rb +46 -16
- data/{lib/metasm → metasm/cpu}/mips/encode.rb +4 -3
- data/{lib/metasm → metasm/cpu}/mips/main.rb +11 -4
- data/{lib/metasm → metasm/cpu}/mips/opcodes.rb +86 -17
- data/{lib/metasm → metasm/cpu}/mips/parse.rb +1 -1
- data/{lib/metasm → metasm/cpu}/mips/render.rb +1 -1
- data/{lib/metasm/dalvik.rb → metasm/cpu/msp430.rb} +1 -1
- data/metasm/cpu/msp430/decode.rb +247 -0
- data/metasm/cpu/msp430/main.rb +62 -0
- data/metasm/cpu/msp430/opcodes.rb +101 -0
- data/{lib/metasm → metasm/cpu}/pic16c/decode.rb +6 -7
- data/{lib/metasm → metasm/cpu}/pic16c/main.rb +0 -0
- data/{lib/metasm → metasm/cpu}/pic16c/opcodes.rb +1 -1
- data/{lib/metasm/mips.rb → metasm/cpu/ppc.rb} +4 -4
- data/{lib/metasm → metasm/cpu}/ppc/decode.rb +18 -12
- data/{lib/metasm → metasm/cpu}/ppc/decompile.rb +3 -3
- data/{lib/metasm → metasm/cpu}/ppc/encode.rb +2 -2
- data/{lib/metasm → metasm/cpu}/ppc/main.rb +17 -12
- data/{lib/metasm → metasm/cpu}/ppc/opcodes.rb +11 -5
- data/metasm/cpu/ppc/parse.rb +55 -0
- data/metasm/cpu/python.rb +8 -0
- data/metasm/cpu/python/decode.rb +136 -0
- data/metasm/cpu/python/main.rb +36 -0
- data/metasm/cpu/python/opcodes.rb +180 -0
- data/{lib/metasm → metasm/cpu}/sh4.rb +1 -1
- data/{lib/metasm → metasm/cpu}/sh4/decode.rb +48 -17
- data/{lib/metasm → metasm/cpu}/sh4/main.rb +13 -4
- data/{lib/metasm → metasm/cpu}/sh4/opcodes.rb +7 -8
- data/metasm/cpu/x86_64.rb +15 -0
- data/{lib/metasm → metasm/cpu}/x86_64/compile_c.rb +28 -17
- data/{lib/metasm → metasm/cpu}/x86_64/debug.rb +4 -4
- data/{lib/metasm → metasm/cpu}/x86_64/decode.rb +57 -15
- data/{lib/metasm → metasm/cpu}/x86_64/encode.rb +55 -26
- data/{lib/metasm → metasm/cpu}/x86_64/main.rb +14 -6
- data/metasm/cpu/x86_64/opcodes.rb +136 -0
- data/{lib/metasm → metasm/cpu}/x86_64/parse.rb +10 -2
- data/metasm/cpu/x86_64/render.rb +35 -0
- data/metasm/cpu/z80.rb +9 -0
- data/metasm/cpu/z80/decode.rb +313 -0
- data/metasm/cpu/z80/main.rb +67 -0
- data/metasm/cpu/z80/opcodes.rb +224 -0
- data/metasm/cpu/z80/render.rb +59 -0
- data/{lib/metasm/os/main.rb → metasm/debug.rb} +160 -401
- data/{lib/metasm → metasm}/decode.rb +35 -4
- data/{lib/metasm → metasm}/decompile.rb +15 -16
- data/{lib/metasm → metasm}/disassemble.rb +201 -45
- data/{lib/metasm → metasm}/disassemble_api.rb +651 -87
- data/{lib/metasm → metasm}/dynldr.rb +220 -133
- data/{lib/metasm → metasm}/encode.rb +10 -1
- data/{lib/metasm → metasm}/exe_format/a_out.rb +9 -6
- data/{lib/metasm → metasm}/exe_format/autoexe.rb +1 -0
- data/{lib/metasm → metasm}/exe_format/bflt.rb +57 -27
- data/{lib/metasm → metasm}/exe_format/coff.rb +11 -3
- data/{lib/metasm → metasm}/exe_format/coff_decode.rb +53 -20
- data/{lib/metasm → metasm}/exe_format/coff_encode.rb +11 -13
- data/{lib/metasm → metasm}/exe_format/dex.rb +13 -5
- data/{lib/metasm → metasm}/exe_format/dol.rb +1 -0
- data/{lib/metasm → metasm}/exe_format/elf.rb +93 -57
- data/{lib/metasm → metasm}/exe_format/elf_decode.rb +143 -34
- data/{lib/metasm → metasm}/exe_format/elf_encode.rb +122 -31
- data/metasm/exe_format/gb.rb +65 -0
- data/metasm/exe_format/javaclass.rb +424 -0
- data/{lib/metasm → metasm}/exe_format/macho.rb +204 -16
- data/{lib/metasm → metasm}/exe_format/main.rb +26 -3
- data/{lib/metasm → metasm}/exe_format/mz.rb +1 -0
- data/{lib/metasm → metasm}/exe_format/nds.rb +7 -4
- data/{lib/metasm → metasm}/exe_format/pe.rb +71 -8
- data/metasm/exe_format/pyc.rb +167 -0
- data/{lib/metasm → metasm}/exe_format/serialstruct.rb +67 -14
- data/{lib/metasm → metasm}/exe_format/shellcode.rb +7 -3
- data/metasm/exe_format/shellcode_rwx.rb +114 -0
- data/metasm/exe_format/swf.rb +205 -0
- data/{lib/metasm → metasm}/exe_format/xcoff.rb +7 -7
- data/metasm/exe_format/zip.rb +335 -0
- data/metasm/gui.rb +13 -0
- data/{lib/metasm → metasm}/gui/cstruct.rb +35 -41
- data/{lib/metasm → metasm}/gui/dasm_coverage.rb +11 -11
- data/{lib/metasm → metasm}/gui/dasm_decomp.rb +7 -20
- data/{lib/metasm → metasm}/gui/dasm_funcgraph.rb +0 -0
- data/metasm/gui/dasm_graph.rb +1695 -0
- data/{lib/metasm → metasm}/gui/dasm_hex.rb +12 -8
- data/{lib/metasm → metasm}/gui/dasm_listing.rb +43 -28
- data/{lib/metasm → metasm}/gui/dasm_main.rb +310 -53
- data/{lib/metasm → metasm}/gui/dasm_opcodes.rb +5 -19
- data/{lib/metasm → metasm}/gui/debug.rb +93 -27
- data/{lib/metasm → metasm}/gui/gtk.rb +162 -40
- data/{lib/metasm → metasm}/gui/qt.rb +12 -2
- data/{lib/metasm → metasm}/gui/win32.rb +179 -42
- data/{lib/metasm → metasm}/gui/x11.rb +59 -59
- data/{lib/metasm → metasm}/main.rb +389 -264
- data/{lib/metasm/os/remote.rb → metasm/os/gdbremote.rb} +146 -54
- data/{lib/metasm → metasm}/os/gnu_exports.rb +1 -1
- data/{lib/metasm → metasm}/os/linux.rb +628 -151
- data/metasm/os/main.rb +330 -0
- data/{lib/metasm → metasm}/os/windows.rb +132 -42
- data/{lib/metasm → metasm}/os/windows_exports.rb +141 -0
- data/{lib/metasm → metasm}/parse.rb +26 -24
- data/{lib/metasm → metasm}/parse_c.rb +221 -116
- data/{lib/metasm → metasm}/preprocessor.rb +55 -40
- data/{lib/metasm → metasm}/render.rb +14 -38
- data/misc/hexdump.rb +2 -1
- data/misc/lint.rb +58 -0
- data/misc/txt2html.rb +9 -7
- data/samples/bindiff.rb +3 -4
- data/samples/dasm-plugins/bindiff.rb +15 -0
- data/samples/dasm-plugins/bookmark.rb +133 -0
- data/samples/dasm-plugins/c_constants.rb +57 -0
- data/samples/dasm-plugins/colortheme_solarized.rb +125 -0
- data/samples/dasm-plugins/cppobj_funcall.rb +60 -0
- data/samples/dasm-plugins/dasm_all.rb +70 -0
- data/samples/dasm-plugins/demangle_cpp.rb +31 -0
- data/samples/dasm-plugins/deobfuscate.rb +251 -0
- data/samples/dasm-plugins/dump_text.rb +35 -0
- data/samples/dasm-plugins/export_graph_svg.rb +86 -0
- data/samples/dasm-plugins/findgadget.rb +75 -0
- data/samples/dasm-plugins/hl_opcode.rb +32 -0
- data/samples/dasm-plugins/hotfix_gtk_dbg.rb +19 -0
- data/samples/dasm-plugins/imm2off.rb +34 -0
- data/samples/dasm-plugins/match_libsigs.rb +93 -0
- data/samples/dasm-plugins/patch_file.rb +95 -0
- data/samples/dasm-plugins/scanfuncstart.rb +36 -0
- data/samples/dasm-plugins/scanxrefs.rb +26 -0
- data/samples/dasm-plugins/selfmodify.rb +197 -0
- data/samples/dasm-plugins/stringsxrefs.rb +28 -0
- data/samples/dasmnavig.rb +1 -1
- data/samples/dbg-apihook.rb +24 -9
- data/samples/dbg-plugins/heapscan.rb +283 -0
- data/samples/dbg-plugins/heapscan/compiled_heapscan_lin.c +155 -0
- data/samples/dbg-plugins/heapscan/compiled_heapscan_win.c +128 -0
- data/samples/dbg-plugins/heapscan/graphheap.rb +616 -0
- data/samples/dbg-plugins/heapscan/heapscan.rb +709 -0
- data/samples/dbg-plugins/heapscan/winheap.h +174 -0
- data/samples/dbg-plugins/heapscan/winheap7.h +307 -0
- data/samples/dbg-plugins/trace_func.rb +214 -0
- data/samples/disassemble-gui.rb +35 -5
- data/samples/disassemble.rb +31 -6
- data/samples/dump_upx.rb +24 -12
- data/samples/dynamic_ruby.rb +12 -3
- data/samples/exeencode.rb +6 -5
- data/samples/factorize-headers-peimports.rb +1 -1
- data/samples/lindebug.rb +175 -381
- data/samples/metasm-shell.rb +1 -2
- data/samples/peldr.rb +2 -2
- data/tests/all.rb +1 -1
- data/tests/arc.rb +26 -0
- data/tests/dynldr.rb +22 -4
- data/tests/expression.rb +55 -0
- data/tests/graph_layout.rb +285 -0
- data/tests/ia32.rb +79 -26
- data/tests/mips.rb +9 -2
- data/tests/x86_64.rb +66 -18
- metadata +330 -218
- data/lib/metasm/arm/opcodes.rb +0 -177
- data/lib/metasm/gui.rb +0 -23
- data/lib/metasm/gui/dasm_graph.rb +0 -1354
- data/lib/metasm/ia32.rb +0 -14
- data/lib/metasm/ia32/opcodes.rb +0 -873
- data/lib/metasm/ppc/parse.rb +0 -52
- data/lib/metasm/x86_64.rb +0 -12
- data/lib/metasm/x86_64/opcodes.rb +0 -118
- data/samples/gdbclient.rb +0 -583
- data/samples/rubstop.rb +0 -399
@@ -0,0 +1,41 @@
|
|
1
|
+
# This file is part of Metasm, the Ruby assembly manipulation suite
|
2
|
+
# Copyright (C) 2006-2009 Yoann GUILLOT
|
3
|
+
#
|
4
|
+
# Licence is LGPL, see LICENCE in the top-level directory
|
5
|
+
|
6
|
+
|
7
|
+
require 'metasm/cpu/arm64/opcodes'
|
8
|
+
require 'metasm/encode'
|
9
|
+
|
10
|
+
module Metasm
|
11
|
+
class ARM64
|
12
|
+
def encode_instr_op(program, instr, op)
|
13
|
+
base = op.bin
|
14
|
+
set_field = lambda { |f, v|
|
15
|
+
v = v.reduce if v.kind_of?(Expression)
|
16
|
+
case f
|
17
|
+
when :i8_12
|
18
|
+
base = Expression[base, :|, [[v, :&, 0xf], :|, [[v, :<<, 4], :&, 0xf00]]]
|
19
|
+
next
|
20
|
+
when :stype; v = [:lsl, :lsr, :asr, :ror].index(v)
|
21
|
+
when :u; v = [:-, :+].index(v)
|
22
|
+
end
|
23
|
+
base = Expression[base, :|, [[v, :&, @fields_mask[f]], :<<, @fields_shift[f]]]
|
24
|
+
}
|
25
|
+
|
26
|
+
op.args.zip(instr.args).each { |sym, arg|
|
27
|
+
case sym
|
28
|
+
when :rd, :rs, :rn, :rm, :rt
|
29
|
+
if arg.sz == 32
|
30
|
+
set_field[:sf, 0]
|
31
|
+
elsif op.field[:sf]
|
32
|
+
set_field[:sf, 1]
|
33
|
+
end
|
34
|
+
set_field[sym, arg.i]
|
35
|
+
end
|
36
|
+
}
|
37
|
+
|
38
|
+
Expression[base].encode(:u32, @endianness)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,105 @@
|
|
1
|
+
# This file is part of Metasm, the Ruby assembly manipulation suite
|
2
|
+
# Copyright (C) 2006-2009 Yoann GUILLOT
|
3
|
+
#
|
4
|
+
# Licence is LGPL, see LICENCE in the top-level directory
|
5
|
+
|
6
|
+
|
7
|
+
require 'metasm/main'
|
8
|
+
|
9
|
+
module Metasm
|
10
|
+
class ARM64 < CPU
|
11
|
+
class Reg
|
12
|
+
class << self
|
13
|
+
attr_accessor :s_to_i, :i_to_s
|
14
|
+
end
|
15
|
+
@i_to_s = { 32 => (0..30).inject({}) { |h, i| h.update i => "w#{i}" }.merge(31 => 'wsp', 32 => 'wzr'),
|
16
|
+
64 => (0..30).inject({}) { |h, i| h.update i => "x#{i}" }.merge(31 => 'sp', 32 => 'xzr', 33 => 'pc')
|
17
|
+
}
|
18
|
+
|
19
|
+
attr_accessor :i, :sz
|
20
|
+
def initialize(i, sz)
|
21
|
+
@i = i
|
22
|
+
@sz = sz
|
23
|
+
end
|
24
|
+
|
25
|
+
Sym = @i_to_s[64].inject({}) { |h, (k, v)| h.update k => v.to_sym }
|
26
|
+
|
27
|
+
def symbolic
|
28
|
+
if @sz == 64
|
29
|
+
Sym[@i]
|
30
|
+
else
|
31
|
+
Expression[Sym[@i], :&, 0xffffffff]
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
class RegShift
|
37
|
+
attr_accessor :reg, :mode, :shift
|
38
|
+
def initialize(reg, mode, shift)
|
39
|
+
@reg = reg
|
40
|
+
@mode = mode
|
41
|
+
@shift = shift
|
42
|
+
end
|
43
|
+
|
44
|
+
def symbolic
|
45
|
+
sym = @reg.symbolic
|
46
|
+
if shift != 0
|
47
|
+
case @mode
|
48
|
+
when :lsl; Expression[sym, :<<, shift]
|
49
|
+
when :lsr; Expression[sym, :>>, shift]
|
50
|
+
when :asr; Expression[sym, :>>, shift] # signextend
|
51
|
+
end
|
52
|
+
end
|
53
|
+
sym
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
class RegCC
|
58
|
+
attr_accessor :cc
|
59
|
+
def initialize(cc)
|
60
|
+
@cc = cc
|
61
|
+
end
|
62
|
+
def symbolic
|
63
|
+
0
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
class Memref
|
68
|
+
attr_accessor :base, :index, :scale, :offset, :sz, :incr
|
69
|
+
def initialize(base, index, scale, offset, sz, incr=nil)
|
70
|
+
@base, @index, @scale, @offset, @sz, @incr = base, index, scale, offset, sz, incr
|
71
|
+
end
|
72
|
+
|
73
|
+
def symbolic(orig=nil)
|
74
|
+
o = Expression[@base.symbolic]
|
75
|
+
if @index
|
76
|
+
si = @index.symbolic
|
77
|
+
si = Expression[@scale, :*, @index] if @scale != 1
|
78
|
+
o = Expression[o, :+, si]
|
79
|
+
end
|
80
|
+
o = Expression[o, :+, @offset] if @offset and @incr != :post
|
81
|
+
Indirection[o.reduce, @sz, orig]
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
class RegList
|
86
|
+
attr_accessor :list
|
87
|
+
|
88
|
+
def initialize(l=[])
|
89
|
+
@list = l
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
def initialize(endianness = :little)
|
94
|
+
super()
|
95
|
+
@endianness = endianness
|
96
|
+
@size = 64
|
97
|
+
end
|
98
|
+
|
99
|
+
def init_opcode_list
|
100
|
+
init_latest
|
101
|
+
@opcode_list
|
102
|
+
end
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
@@ -0,0 +1,232 @@
|
|
1
|
+
# This file is part of Metasm, the Ruby assembly manipulation suite
|
2
|
+
# Copyright (C) 2006-2009 Yoann GUILLOT
|
3
|
+
#
|
4
|
+
# Licence is LGPL, see LICENCE in the top-level directory
|
5
|
+
|
6
|
+
|
7
|
+
require 'metasm/cpu/arm64/main'
|
8
|
+
|
9
|
+
module Metasm
|
10
|
+
class ARM64
|
11
|
+
private
|
12
|
+
|
13
|
+
def addop(name, bin, *args)
|
14
|
+
o = Opcode.new name, bin
|
15
|
+
args.each { |a|
|
16
|
+
o.args << a if @valid_args[a]
|
17
|
+
o.props[a] = true if @valid_props[a]
|
18
|
+
o.props.update a if a.kind_of?(::Hash)
|
19
|
+
}
|
20
|
+
|
21
|
+
args.each { |a| o.fields[a] = [@fields_mask[a], @fields_shift[a]] if @fields_mask[a] }
|
22
|
+
|
23
|
+
@opcode_list << o
|
24
|
+
end
|
25
|
+
|
26
|
+
def addop_s31(n, bin, *args)
|
27
|
+
addop n, bin, :r_32, *args
|
28
|
+
addop n, (1 << 31) | bin, *args
|
29
|
+
end
|
30
|
+
|
31
|
+
def addop_s30(n, bin, *args)
|
32
|
+
addop n, bin, :r_32, *args
|
33
|
+
addop n, (1 << 30) | bin, *args
|
34
|
+
end
|
35
|
+
|
36
|
+
def addop_data_shifted(n, bin, *args)
|
37
|
+
addop n, bin | (0b00 << 22), :rt, :rn, :rm_lsl_i6, :r_32, *args
|
38
|
+
addop n, bin | (0b01 << 22), :rt, :rn, :rm_lsr_i6, :r_32, *args
|
39
|
+
addop n, bin | (0b10 << 22), :rt, :rn, :rm_asr_i6, :r_32, *args
|
40
|
+
addop n, bin | (0b00 << 22) | (1 << 31), :rt, :rn, :rm_lsl_i5, *args
|
41
|
+
addop n, bin | (0b01 << 22) | (1 << 31), :rt, :rn, :rm_lsr_i5, *args
|
42
|
+
addop n, bin | (0b10 << 22) | (1 << 31), :rt, :rn, :rm_asr_i5, *args
|
43
|
+
end
|
44
|
+
|
45
|
+
def addop_data_imm(n, bin, *args)
|
46
|
+
addop n, bin, :rt, :rn, :bitmask_imm, :bitmask_s, :bitmask_r, *args
|
47
|
+
addop n, bin | (1 << 31), :rt, :rn, :bitmask_imm, :bitmask_n, :bitmask_s, :bitmask_r, *args
|
48
|
+
end
|
49
|
+
|
50
|
+
def addop_bitfield(n, bin, *args)
|
51
|
+
addop n, bin, :rt, :rn, :bitmask, :bitmask_s, :bitmask_r, *args
|
52
|
+
addop n, bin | (1 << 31) | (1 << 22), :rt, :rn, :bitmask, :bitmask_s, :bitmask_r, *args
|
53
|
+
end
|
54
|
+
|
55
|
+
# official name => usual name
|
56
|
+
OP_DATA_ALIAS = { 'bic' => 'andn', 'orr' => 'or', 'eor' => 'xor' }
|
57
|
+
def addop_data_shifted_alias(n, bin, *args)
|
58
|
+
if a = OP_DATA_ALIAS[n]
|
59
|
+
addop_data_shifted a, bin, *args
|
60
|
+
end
|
61
|
+
addop_data_shifted n, bin, *args
|
62
|
+
end
|
63
|
+
|
64
|
+
def addop_data_imm_alias(n, bin, *args)
|
65
|
+
if a = OP_DATA_ALIAS[n]
|
66
|
+
addop_data_imm a, bin, *args
|
67
|
+
end
|
68
|
+
addop_data_imm n, bin, *args
|
69
|
+
end
|
70
|
+
|
71
|
+
def addop_store(n, bin, *args)
|
72
|
+
addop_s30 n, bin | (0b01 << 10), :rt, :m_rn_s9, :mem_incr => :post
|
73
|
+
addop_s30 n, bin | (0b11 << 10), :rt, :m_rn_s9, :mem_incr => :pre
|
74
|
+
addop_s30 n, bin | (1 << 21) | (0b10 << 10) | (1 << 14), :rt, :m_rm_extend
|
75
|
+
addop_s30 n, bin | (1 << 24), :rt, :m_rn_u12
|
76
|
+
end
|
77
|
+
|
78
|
+
OP_CC = %w[eq ne cs cc mi pl vs vc hi ls ge lt gt le al al2]
|
79
|
+
def addop_cc(n, bin, *args)
|
80
|
+
OP_CC.each_with_index { |e, i|
|
81
|
+
args << :stopexec if e == 'al' and args.include?(:setip)
|
82
|
+
addop n+e, bin | i, *args
|
83
|
+
}
|
84
|
+
end
|
85
|
+
|
86
|
+
public
|
87
|
+
# ARMv8 64-bits instruction set, aka AArch64
|
88
|
+
def init_arm_v8
|
89
|
+
@opcode_list = []
|
90
|
+
|
91
|
+
[:stopexec, :setip, :saveip,
|
92
|
+
:r_z, # reg nr31 = flag ? zero : sp
|
93
|
+
:r_32, # reg size == 32bit
|
94
|
+
:mem_incr, # mem dereference is pre/post-increment
|
95
|
+
:mem_sz, # point to uint32 => 4
|
96
|
+
:pcrel, # immediate value is pc-relative
|
97
|
+
:pcrel_page, # immediate value is a page offset, pc-relative
|
98
|
+
].each { |p| @valid_props[p] = true }
|
99
|
+
|
100
|
+
[:rn, :rt, :rt2, :rm,
|
101
|
+
:rm_lsl_i6, :rm_lsr_i6, :rm_asr_i6,
|
102
|
+
:rm_lsl_i5, :rm_lsr_i5, :rm_asr_i5,
|
103
|
+
:m_rm_extend, :rm_extend_i3,
|
104
|
+
:i14_5, :i16_5, :il18_5, :i19_5, :i26_0, :i12_10_s1,
|
105
|
+
:i19_5_2_29,
|
106
|
+
:m_rn_s7, :m_rn_s9, :m_rn_u12,
|
107
|
+
:bitmask, :bitmask_imm, :cond_12,
|
108
|
+
].each { |p| @valid_args[p] = true }
|
109
|
+
|
110
|
+
@fields_mask.update :rn => 0x1f, :rt => 0x1f, :rt2 => 0x1f, :rm => 0x1f,
|
111
|
+
:rm_lsl_i6 => 0x7ff, :rm_lsr_i6 => 0x7ff, :rm_asr_i6 => 0x7ff,
|
112
|
+
:rm_lsl_i5 => 0x7df, :rm_lsr_i5 => 0x7df, :rm_asr_i5 => 0x7df,
|
113
|
+
:m_rm_extend => ((0x1f << 11) | (0xb << 7) | 0x1f), :rm_extend_i3 => 0x7ff,
|
114
|
+
:i14_5 => 0x3fff, :i16_5 => 0xffff, :il18_5 => 0x3ffff, :i26_0 => 0x3ffffff,
|
115
|
+
:i12_10_s1 => 0x3fff, :i6_10 => 0x3f,
|
116
|
+
:s7_15 => 0x7f, :s9_12 => 0x1ff, :u12_10 => 0xfff,
|
117
|
+
:i19_5 => 0x7ffff, :i2_29 => 3,
|
118
|
+
:i19_5_2_29 => 0x60ffffe0, :cond_12 => 0xf,
|
119
|
+
:bitmask_n => 1, :bitmask_s => 0x3f, :bitmask_r => 0x3f,
|
120
|
+
:regextend_13 => 7, :i1_12 => 1, :i3_10 => 7,
|
121
|
+
:m_rn_s7 => ((0x7f << 10) | 0x1f),
|
122
|
+
:m_rn_s9 => ((0x1ff << 7) | 0x1f),
|
123
|
+
:m_rn_u12 => ((0xfff << 5) | 0x1f)
|
124
|
+
|
125
|
+
@fields_shift.update :rn => 5, :rt => 0, :rt2 => 10, :rm => 16,
|
126
|
+
:rm_lsl_i6 => 10, :rm_lsr_i6 => 10, :rm_asr_i6 => 10,
|
127
|
+
:rm_lsl_i5 => 10, :rm_lsr_i5 => 10, :rm_asr_i5 => 10,
|
128
|
+
:m_rm_extend => 5, :rm_extend_i3 => 10,
|
129
|
+
:i14_5 => 5, :i16_5 => 5, :il18_5 => 5, :i26_0 => 0,
|
130
|
+
:i12_10_s1 => 10, :i6_10 => 10,
|
131
|
+
:s7_15 => 15, :s9_12 => 12, :u12_10 => 10,
|
132
|
+
:i19_5 => 5, :i2_29 => 29,
|
133
|
+
:i19_5_2_29 => 0, :cond_12 => 12,
|
134
|
+
:bitmask_n => 22, :bitmask_s => 10, :bitmask_r => 16,
|
135
|
+
:regextend_13 => 13, :i1_12 => 12, :i3_10 => 10,
|
136
|
+
:m_rn_s7 => 5, :m_rn_s9 => 5, :m_rn_u12 => 5
|
137
|
+
|
138
|
+
addop 'adr', 1 << 28, :rt, :i19_5_2_29, :pcrel
|
139
|
+
addop 'adrp',(1 << 28) | (1 << 31), :rt, :i19_5_2_29, :pcrel_page
|
140
|
+
|
141
|
+
addop_s31 'cbz', 0b0110100 << 24, :rt, :i19_5, :setip
|
142
|
+
addop_s31 'cbnz', 0b0110101 << 24, :rt, :i19_5, :setip
|
143
|
+
addop_cc 'b', 0b0101010 << 25, :i19_5, :setip
|
144
|
+
|
145
|
+
addop_s31 'mov', (0b01_01010_00_0 << 21) | (0b11111 << 5), :rt, :rm, :r_z # alias for orr rt, 0, rm
|
146
|
+
addop_data_shifted_alias 'and', 0b00_01010_00_0 << 21
|
147
|
+
addop_data_shifted_alias 'bic', 0b00_01010_00_1 << 21 # and not
|
148
|
+
addop_data_shifted_alias 'orr', 0b01_01010_00_0 << 21
|
149
|
+
addop_data_shifted_alias 'orn', 0b01_01010_00_1 << 21 # or not
|
150
|
+
addop_data_shifted_alias 'eor', 0b10_01010_00_0 << 21
|
151
|
+
addop_data_shifted_alias 'eorn', 0b10_01010_00_1 << 21
|
152
|
+
addop_data_shifted_alias 'ands', 0b11_01010_00_0 << 21, :r_z # same as and + set flags
|
153
|
+
addop_data_shifted_alias 'bics', 0b11_01010_00_1 << 21, :r_z # same as bic + set flags
|
154
|
+
|
155
|
+
addop 'cmp', (0b11_01011_00_0 << 21) | (0b11111 << 0) | (0b00 << 22), :rn, :rm_lsl_i6, :r_32, :r_z # alias for subs 0, rn, rm
|
156
|
+
addop 'cmp', (0b11_01011_00_0 << 21) | (0b11111 << 0) | (0b01 << 22), :rn, :rm_lsr_i6, :r_32, :r_z
|
157
|
+
addop 'cmp', (0b11_01011_00_0 << 21) | (0b11111 << 0) | (0b10 << 22), :rn, :rm_asr_i6, :r_32, :r_z
|
158
|
+
addop 'cmp', (0b11_01011_00_0 << 21) | (0b11111 << 0) | (0b00 << 22) | (1 << 31), :rn, :rm_lsl_i5, :r_z
|
159
|
+
addop 'cmp', (0b11_01011_00_0 << 21) | (0b11111 << 0) | (0b01 << 22) | (1 << 31), :rn, :rm_lsr_i5, :r_z
|
160
|
+
addop 'cmp', (0b11_01011_00_0 << 21) | (0b11111 << 0) | (0b10 << 22) | (1 << 31), :rn, :rm_asr_i5, :r_z
|
161
|
+
addop_s31 'negs', (0b11_01011_00_0 << 21) | (0b11111 << 5), :rt, :rm, :r_z # alias for subs rt, 0, rm
|
162
|
+
addop_data_shifted_alias 'add', 0b00_01011_00_0 << 21
|
163
|
+
addop_data_shifted_alias 'adds',0b01_01011_00_0 << 21, :r_z
|
164
|
+
addop_data_shifted_alias 'sub', 0b10_01011_00_0 << 21
|
165
|
+
addop_data_shifted_alias 'subs',0b11_01011_00_0 << 21, :r_z
|
166
|
+
|
167
|
+
addop_s31 'add', 0b00_01011_00_1 << 21, :rt, :rn, :rm_extend_i3
|
168
|
+
addop_s31 'adds',0b01_01011_00_1 << 21, :rt, :rn, :rm_extend_i3
|
169
|
+
addop_s31 'sub', 0b10_01011_00_1 << 21, :rt, :rn, :rm_extend_i3
|
170
|
+
addop_s31 'subs',0b11_01011_00_1 << 21, :rt, :rn, :rm_extend_i3
|
171
|
+
|
172
|
+
addop_data_imm_alias 'and', 0b00_100100 << 23
|
173
|
+
addop_data_imm_alias 'orr', 0b01_100100 << 23
|
174
|
+
addop_data_imm_alias 'eor', 0b10_100100 << 23
|
175
|
+
addop_data_imm_alias 'ands',0b11_100100 << 23, :r_z
|
176
|
+
|
177
|
+
addop 'svc', (0b11010100 << 24) | (0b000 << 21) | (0b00001), :i16_5
|
178
|
+
addop 'hvc', (0b11010100 << 24) | (0b000 << 21) | (0b00010), :i16_5, :stopexec
|
179
|
+
addop 'smc', (0b11010100 << 24) | (0b000 << 21) | (0b00011), :i16_5, :stopexec
|
180
|
+
addop 'brk', (0b11010100 << 24) | (0b001 << 21) | (0b00000), :i16_5, :stopexec
|
181
|
+
addop 'hlt', (0b11010100 << 24) | (0b010 << 21) | (0b00000), :i16_5, :stopexec
|
182
|
+
addop 'dcps1', (0b11010100 << 24) | (0b101 << 21) | (0b00001), :i16_5, :stopexec
|
183
|
+
addop 'dcps2', (0b11010100 << 24) | (0b101 << 21) | (0b00010), :i16_5, :stopexec
|
184
|
+
addop 'dcps3', (0b11010100 << 24) | (0b101 << 21) | (0b00011), :i16_5, :stopexec
|
185
|
+
|
186
|
+
addop_s31 'tbz', (0b0110110 << 24), :rt, :i14_5
|
187
|
+
|
188
|
+
addop 'b', (0b000101 << 26), :i26_0, :setip, :stopexec
|
189
|
+
addop 'bl', (0b100101 << 26), :i26_0, :setip, :stopexec, :saveip
|
190
|
+
addop 'br', (0b1101011 << 25) | (0b0000 << 21) | (0b11111 << 16), :rn, :setip, :stopexec
|
191
|
+
addop 'blr', (0b1101011 << 25) | (0b0001 << 21) | (0b11111 << 16), :rn, :setip, :stopexec, :saveip
|
192
|
+
addop 'ret', (0b1101011 << 25) | (0b0010 << 21) | (0b11111 << 16) | (0b11110 << 5), :setip, :stopexec
|
193
|
+
addop 'ret', (0b1101011 << 25) | (0b0010 << 21) | (0b11111 << 16), :rn, :setip, :stopexec
|
194
|
+
addop 'eret',(0b1101011 << 25) | (0b0100 << 21) | (0b11111 << 16) | (0b11111 << 5), :setip, :stopexec
|
195
|
+
addop 'drps',(0b1101011 << 25) | (0b0101 << 21) | (0b11111 << 16) | (0b11111 << 5), :setip, :stopexec
|
196
|
+
|
197
|
+
addop_s31 'mov', (0b0010001 << 24), :rt, :rn # alias for add rt, rn, 0
|
198
|
+
addop_s31 'add', (0b0010001 << 24), :rt, :rn, :i12_10_s1
|
199
|
+
addop_s31 'adds', (0b0110001 << 24), :rt, :rn, :i12_10_s1
|
200
|
+
addop_s31 'sub', (0b1010001 << 24), :rt, :rn, :i12_10_s1
|
201
|
+
addop_s31 'subs', (0b1110001 << 24), :rt, :rn, :i12_10_s1
|
202
|
+
|
203
|
+
addop_s31 'movn', (0b00100101 << 23), :rt, :il18_5
|
204
|
+
addop_s31 'mov', (0b10100101 << 23), :rt, :i16_5 # alias movz rt, i16 LSL 0
|
205
|
+
addop_s31 'movz', (0b10100101 << 23), :rt, :il18_5
|
206
|
+
addop_s31 'movk', (0b11100101 << 23), :rt, :il18_5
|
207
|
+
|
208
|
+
addop_store 'str', (0b10_111_0_00_00 << 22)
|
209
|
+
addop_store 'ldr', (0b10_111_0_00_01 << 22)
|
210
|
+
addop_store 'ldrsw', (0b10_111_0_00_10 << 22)
|
211
|
+
addop_store 'strb', (0b00_111_0_00_00 << 22)
|
212
|
+
addop_store 'ldrb', (0b00_111_0_00_01 << 22)
|
213
|
+
addop_s31 'stp', 0b00_101_0_001_0 << 22, :rt, :rt2, :m_rn_s7, :mem_incr => :post
|
214
|
+
addop_s31 'stp', 0b00_101_0_011_0 << 22, :rt, :rt2, :m_rn_s7, :mem_incr => :pre
|
215
|
+
addop_s31 'stp', 0b00_101_0_010_0 << 22, :rt, :rt2, :m_rn_s7
|
216
|
+
addop_s31 'ldp', 0b00_101_0_001_1 << 22, :rt, :rt2, :m_rn_s7, :mem_incr => :post
|
217
|
+
addop_s31 'ldp', 0b00_101_0_011_1 << 22, :rt, :rt2, :m_rn_s7, :mem_incr => :pre
|
218
|
+
addop_s31 'ldp', 0b00_101_0_010_1 << 22, :rt, :rt2, :m_rn_s7
|
219
|
+
|
220
|
+
addop_s31 'csel', (0b0011010100 << 21) | (0b00 << 10), :rt, :rn, :rm, :cond_12, :r_z
|
221
|
+
addop_s31 'csinc', (0b0011010100 << 21) | (0b01 << 10), :rt, :rn, :rm, :cond_12, :r_z
|
222
|
+
addop_s31 'csinv', (0b1011010100 << 21) | (0b00 << 10), :rt, :rn, :rm, :cond_12, :r_z
|
223
|
+
addop_s31 'csneg', (0b1011010100 << 21) | (0b01 << 10), :rt, :rn, :rm, :cond_12, :r_z
|
224
|
+
|
225
|
+
addop_bitfield 'sbfm', 0b00_100110 << 23
|
226
|
+
addop_bitfield 'bfm', 0b01_100110 << 23
|
227
|
+
addop_bitfield 'ubfm', 0b10_100110 << 23
|
228
|
+
end
|
229
|
+
|
230
|
+
alias init_latest init_arm_v8
|
231
|
+
end
|
232
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
# This file is part of Metasm, the Ruby assembly manipulation suite
|
2
|
+
# Copyright (C) 2006-2009 Yoann GUILLOT
|
3
|
+
#
|
4
|
+
# Licence is LGPL, see LICENCE in the top-level directory
|
5
|
+
|
6
|
+
|
7
|
+
require 'metasm/cpu/arm64/opcodes'
|
8
|
+
require 'metasm/parse'
|
9
|
+
|
10
|
+
module Metasm
|
11
|
+
class ARM64
|
12
|
+
def parse_arg_valid?(op, sym, arg)
|
13
|
+
false
|
14
|
+
end
|
15
|
+
|
16
|
+
def parse_argument(lexer)
|
17
|
+
raise lexer, 'fu'
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,95 @@
|
|
1
|
+
# This file is part of Metasm, the Ruby assembly manipulation suite
|
2
|
+
# Copyright (C) 2006-2009 Yoann GUILLOT
|
3
|
+
#
|
4
|
+
# Licence is LGPL, see LICENCE in the top-level directory
|
5
|
+
|
6
|
+
require 'metasm/render'
|
7
|
+
require 'metasm/cpu/arm64/opcodes'
|
8
|
+
|
9
|
+
module Metasm
|
10
|
+
class ARM64
|
11
|
+
class Reg
|
12
|
+
include Renderable
|
13
|
+
def render
|
14
|
+
[self.class.i_to_s[@sz][@i]]
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
class RegShift
|
19
|
+
include Renderable
|
20
|
+
def render
|
21
|
+
case @mode
|
22
|
+
when :lsl, :lsr, :asr
|
23
|
+
if shift == 0
|
24
|
+
[@reg]
|
25
|
+
else
|
26
|
+
case @mode
|
27
|
+
when :lsl; [@reg, ' LSL ', Expression[shift]]
|
28
|
+
when :lsr; [@reg, ' LSR ', Expression[shift]]
|
29
|
+
when :asr; [@reg, ' ASR ', Expression[shift]]
|
30
|
+
end
|
31
|
+
end
|
32
|
+
else
|
33
|
+
sh = []
|
34
|
+
sh << ' LSL ' << Expression[shift] if shift != 0
|
35
|
+
[ @mode.to_s, '( ', @reg, ' )', *sh ]
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
class RegCC
|
41
|
+
include Renderable
|
42
|
+
def render
|
43
|
+
[@cc]
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
class Memref
|
48
|
+
include Renderable
|
49
|
+
def render
|
50
|
+
o = Expression[@base]
|
51
|
+
if @index
|
52
|
+
i = @index
|
53
|
+
i = Expression[@scale, :*, @index] if @scale != 1
|
54
|
+
o = Expression[o, :+, i]
|
55
|
+
end
|
56
|
+
case @incr
|
57
|
+
when nil
|
58
|
+
o = Expression[o, :+, @offset] if @offset and @offset != Expression[0]
|
59
|
+
['[', o, ']']
|
60
|
+
when :pre
|
61
|
+
o = Expression[o, :+, @offset]
|
62
|
+
['[', o, ']!']
|
63
|
+
when :post
|
64
|
+
['[', o, '], ', @offset]
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
class RegList
|
70
|
+
include Renderable
|
71
|
+
def render
|
72
|
+
r = ['{']
|
73
|
+
@list.each { |l| r << l << ', ' }
|
74
|
+
r[-1] = '}'
|
75
|
+
r << '^' if usermoderegs
|
76
|
+
r
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
def gui_hilight_word_regexp_init
|
81
|
+
ret = {}
|
82
|
+
(0..30).each { |i|
|
83
|
+
ret["w#{i}"] = ret["x#{i}"] = "[wx]#{i}"
|
84
|
+
}
|
85
|
+
ret["sp"] = ret["wsp"] = "w?sp"
|
86
|
+
ret["zr"] = ret["wzr"] = "w?zr"
|
87
|
+
ret
|
88
|
+
end
|
89
|
+
|
90
|
+
def gui_hilight_word_regexp(word)
|
91
|
+
@gui_hilight_word_hash ||= gui_hilight_word_regexp_init
|
92
|
+
@gui_hilight_word_hash[word] or super(word)
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|