metasm 1.0.0 → 1.0.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- checksums.yaml.gz.sig +0 -0
- data.tar.gz.sig +3 -0
- data/.gitignore +3 -0
- data/.hgtags +3 -0
- data/Gemfile +3 -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 +3 -1
- 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 +23 -0
- data/{lib/metasm.rb → metasm.rb} +15 -3
- data/{lib/metasm → metasm}/compile_c.rb +15 -9
- data/metasm/cpu/arc.rb +8 -0
- data/metasm/cpu/arc/decode.rb +404 -0
- data/metasm/cpu/arc/main.rb +191 -0
- data/metasm/cpu/arc/opcodes.rb +588 -0
- data/metasm/cpu/arm.rb +14 -0
- data/{lib/metasm → metasm/cpu}/arm/debug.rb +2 -2
- data/{lib/metasm → metasm/cpu}/arm/decode.rb +15 -18
- data/{lib/metasm → metasm/cpu}/arm/encode.rb +23 -8
- data/{lib/metasm → metasm/cpu}/arm/main.rb +3 -6
- 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 +285 -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/mips/compile_c.rb → metasm/cpu/bpf.rb} +4 -2
- data/metasm/cpu/bpf/decode.rb +110 -0
- data/metasm/cpu/bpf/main.rb +60 -0
- data/metasm/cpu/bpf/opcodes.rb +81 -0
- data/metasm/cpu/bpf/render.rb +30 -0
- data/{lib/metasm/ppc.rb → metasm/cpu/cy16.rb} +2 -4
- data/metasm/cpu/cy16/decode.rb +247 -0
- data/metasm/cpu/cy16/main.rb +63 -0
- data/metasm/cpu/cy16/opcodes.rb +78 -0
- data/metasm/cpu/cy16/render.rb +30 -0
- data/metasm/cpu/dalvik.rb +11 -0
- data/{lib/metasm → metasm/cpu}/dalvik/decode.rb +34 -34
- data/{lib/metasm → metasm/cpu}/dalvik/main.rb +71 -4
- data/{lib/metasm → metasm/cpu}/dalvik/opcodes.rb +21 -12
- data/{lib/metasm/mips.rb → metasm/cpu/ebpf.rb} +3 -4
- data/metasm/cpu/ebpf/debug.rb +61 -0
- data/metasm/cpu/ebpf/decode.rb +142 -0
- data/metasm/cpu/ebpf/main.rb +58 -0
- data/metasm/cpu/ebpf/opcodes.rb +97 -0
- data/metasm/cpu/ebpf/render.rb +36 -0
- data/metasm/cpu/ia32.rb +17 -0
- data/{lib/metasm → metasm/cpu}/ia32/compile_c.rb +23 -9
- data/{lib/metasm → metasm/cpu}/ia32/debug.rb +44 -6
- data/{lib/metasm → metasm/cpu}/ia32/decode.rb +342 -128
- data/{lib/metasm → metasm/cpu}/ia32/decompile.rb +75 -53
- data/{lib/metasm → metasm/cpu}/ia32/encode.rb +19 -13
- data/{lib/metasm → metasm/cpu}/ia32/main.rb +66 -8
- data/metasm/cpu/ia32/opcodes.rb +1424 -0
- data/{lib/metasm → metasm/cpu}/ia32/parse.rb +55 -17
- data/{lib/metasm → metasm/cpu}/ia32/render.rb +32 -5
- data/metasm/cpu/mcs51.rb +8 -0
- data/metasm/cpu/mcs51/decode.rb +99 -0
- data/metasm/cpu/mcs51/main.rb +87 -0
- data/metasm/cpu/mcs51/opcodes.rb +120 -0
- data/metasm/cpu/mips.rb +14 -0
- data/metasm/cpu/mips/debug.rb +42 -0
- data/{lib/metasm → metasm/cpu}/mips/decode.rb +59 -38
- data/{lib/metasm → metasm/cpu}/mips/encode.rb +4 -3
- data/{lib/metasm → metasm/cpu}/mips/main.rb +13 -6
- data/{lib/metasm → metasm/cpu}/mips/opcodes.rb +87 -18
- 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 +243 -0
- data/metasm/cpu/msp430/main.rb +62 -0
- data/metasm/cpu/msp430/opcodes.rb +101 -0
- data/metasm/cpu/openrisc.rb +11 -0
- data/metasm/cpu/openrisc/debug.rb +106 -0
- data/metasm/cpu/openrisc/decode.rb +182 -0
- data/metasm/cpu/openrisc/decompile.rb +350 -0
- data/metasm/cpu/openrisc/main.rb +70 -0
- data/metasm/cpu/openrisc/opcodes.rb +109 -0
- data/metasm/cpu/openrisc/render.rb +37 -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/metasm/cpu/ppc.rb +11 -0
- data/{lib/metasm → metasm/cpu}/ppc/decode.rb +18 -37
- 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 +23 -18
- data/{lib/metasm → metasm/cpu}/ppc/opcodes.rb +11 -6
- data/metasm/cpu/ppc/parse.rb +55 -0
- data/metasm/cpu/python.rb +8 -0
- data/metasm/cpu/python/decode.rb +116 -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 +50 -23
- data/{lib/metasm → metasm/cpu}/sh4/main.rb +38 -27
- data/{lib/metasm → metasm/cpu}/sh4/opcodes.rb +7 -8
- data/metasm/cpu/st20.rb +9 -0
- data/metasm/cpu/st20/decode.rb +173 -0
- data/metasm/cpu/st20/decompile.rb +283 -0
- data/metasm/cpu/st20/main.rb +37 -0
- data/metasm/cpu/st20/opcodes.rb +140 -0
- data/{lib/metasm/arm.rb → metasm/cpu/webasm.rb} +4 -5
- data/metasm/cpu/webasm/debug.rb +31 -0
- data/metasm/cpu/webasm/decode.rb +321 -0
- data/metasm/cpu/webasm/decompile.rb +386 -0
- data/metasm/cpu/webasm/encode.rb +104 -0
- data/metasm/cpu/webasm/main.rb +81 -0
- data/metasm/cpu/webasm/opcodes.rb +214 -0
- data/metasm/cpu/x86_64.rb +15 -0
- data/{lib/metasm → metasm/cpu}/x86_64/compile_c.rb +40 -25
- data/{lib/metasm → metasm/cpu}/x86_64/debug.rb +4 -4
- data/{lib/metasm → metasm/cpu}/x86_64/decode.rb +58 -15
- data/{lib/metasm → metasm/cpu}/x86_64/encode.rb +59 -28
- data/{lib/metasm → metasm/cpu}/x86_64/main.rb +18 -6
- data/metasm/cpu/x86_64/opcodes.rb +138 -0
- data/{lib/metasm → metasm/cpu}/x86_64/parse.rb +12 -4
- data/metasm/cpu/x86_64/render.rb +35 -0
- data/metasm/cpu/z80.rb +9 -0
- data/metasm/cpu/z80/decode.rb +286 -0
- data/metasm/cpu/z80/main.rb +67 -0
- data/metasm/cpu/z80/opcodes.rb +224 -0
- data/metasm/cpu/z80/render.rb +48 -0
- data/{lib/metasm/os/main.rb → metasm/debug.rb} +201 -407
- data/{lib/metasm → metasm}/decode.rb +104 -24
- data/{lib/metasm → metasm}/decompile.rb +804 -478
- data/{lib/metasm → metasm}/disassemble.rb +385 -170
- data/{lib/metasm → metasm}/disassemble_api.rb +684 -105
- data/{lib/metasm → metasm}/dynldr.rb +231 -138
- data/{lib/metasm → metasm}/encode.rb +20 -5
- data/{lib/metasm → metasm}/exe_format/a_out.rb +9 -6
- data/{lib/metasm → metasm}/exe_format/autoexe.rb +3 -0
- data/{lib/metasm → metasm}/exe_format/bflt.rb +57 -27
- data/{lib/metasm → metasm}/exe_format/coff.rb +35 -7
- data/{lib/metasm → metasm}/exe_format/coff_decode.rb +70 -23
- data/{lib/metasm → metasm}/exe_format/coff_encode.rb +24 -22
- data/{lib/metasm → metasm}/exe_format/dex.rb +26 -8
- data/{lib/metasm → metasm}/exe_format/dol.rb +1 -0
- data/{lib/metasm → metasm}/exe_format/elf.rb +108 -58
- data/{lib/metasm → metasm}/exe_format/elf_decode.rb +202 -36
- data/{lib/metasm → metasm}/exe_format/elf_encode.rb +126 -32
- data/metasm/exe_format/gb.rb +65 -0
- data/metasm/exe_format/javaclass.rb +424 -0
- data/{lib/metasm → metasm}/exe_format/macho.rb +218 -16
- data/{lib/metasm → metasm}/exe_format/main.rb +28 -3
- data/{lib/metasm → metasm}/exe_format/mz.rb +2 -0
- data/{lib/metasm → metasm}/exe_format/nds.rb +7 -4
- data/{lib/metasm → metasm}/exe_format/pe.rb +96 -11
- 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/metasm/exe_format/wasm.rb +402 -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 +177 -114
- data/{lib/metasm → metasm}/gui/dasm_funcgraph.rb +0 -0
- data/metasm/gui/dasm_graph.rb +1754 -0
- data/{lib/metasm → metasm}/gui/dasm_hex.rb +16 -12
- data/{lib/metasm → metasm}/gui/dasm_listing.rb +43 -28
- data/{lib/metasm → metasm}/gui/dasm_main.rb +360 -77
- data/{lib/metasm → metasm}/gui/dasm_opcodes.rb +5 -19
- data/{lib/metasm → metasm}/gui/debug.rb +109 -34
- data/{lib/metasm → metasm}/gui/gtk.rb +174 -44
- data/{lib/metasm → metasm}/gui/qt.rb +14 -4
- data/{lib/metasm → metasm}/gui/win32.rb +180 -43
- data/{lib/metasm → metasm}/gui/x11.rb +59 -59
- data/{lib/metasm → metasm}/main.rb +421 -286
- data/metasm/os/emulator.rb +175 -0
- 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 +335 -0
- data/{lib/metasm → metasm}/os/windows.rb +151 -58
- data/{lib/metasm → metasm}/os/windows_exports.rb +141 -0
- data/{lib/metasm → metasm}/parse.rb +49 -36
- data/{lib/metasm → metasm}/parse_c.rb +405 -246
- data/{lib/metasm → metasm}/preprocessor.rb +71 -41
- data/{lib/metasm → metasm}/render.rb +14 -38
- data/misc/hexdump.rb +4 -3
- data/misc/lint.rb +58 -0
- data/misc/objdiff.rb +4 -1
- data/misc/objscan.rb +1 -1
- data/misc/openrisc-parser.rb +79 -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 +29 -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 +48 -7
- data/samples/disassemble.rb +31 -6
- data/samples/dump_upx.rb +24 -12
- data/samples/dynamic_ruby.rb +35 -27
- data/samples/elfencode.rb +15 -0
- data/samples/emubios.rb +251 -0
- data/samples/emudbg.rb +127 -0
- data/samples/exeencode.rb +6 -5
- data/samples/factorize-headers-peimports.rb +1 -1
- data/samples/lindebug.rb +186 -391
- data/samples/metasm-shell.rb +68 -57
- 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 +57 -0
- data/tests/graph_layout.rb +285 -0
- data/tests/ia32.rb +80 -26
- data/tests/mcs51.rb +27 -0
- data/tests/mips.rb +10 -3
- data/tests/preprocessor.rb +18 -0
- data/tests/x86_64.rb +66 -18
- metadata +465 -219
- metadata.gz.sig +2 -0
- 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 -872
- 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
@@ -11,10 +11,15 @@ module Metasm
|
|
11
11
|
class PowerPC < CPU
|
12
12
|
class Reg
|
13
13
|
include Renderable
|
14
|
+
class << self
|
15
|
+
attr_accessor :s_to_i, :i_to_s
|
16
|
+
end
|
14
17
|
|
15
18
|
def ==(o)
|
16
19
|
o.class == self.class and (not respond_to?(:i) or o.i == i)
|
17
20
|
end
|
21
|
+
|
22
|
+
def render ; [self.class.i_to_s[@i]] ; end
|
18
23
|
end
|
19
24
|
|
20
25
|
# general purpose reg
|
@@ -24,17 +29,14 @@ class PowerPC < CPU
|
|
24
29
|
@i = i
|
25
30
|
end
|
26
31
|
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
def
|
32
|
+
@s_to_i = (0..31).inject({}) { |h, i| h.update((i == 1 ? 'sp' : "r#{i}") => i) }
|
33
|
+
@i_to_s = @s_to_i.invert
|
34
|
+
Sym = @s_to_i.sort.transpose.last
|
35
|
+
def symbolic(di=nil) ; Sym[@i] end
|
31
36
|
end
|
32
37
|
|
33
38
|
# special purpose reg
|
34
39
|
class SPR < Reg
|
35
|
-
class << self
|
36
|
-
attr_accessor :s_to_i, :i_to_s
|
37
|
-
end
|
38
40
|
@s_to_i = {'xer' => 1, 'lr' => 8, 'ctr' => 9, 'dec' => 22, 'srr0' => 26, 'srr1' => 27,
|
39
41
|
'sprg0' => 272, 'sprg1' => 273, 'sprg2' => 274, 'sprg3' => 275, 'pvr' => 287}
|
40
42
|
@i_to_s = @s_to_i.invert
|
@@ -45,24 +47,25 @@ class PowerPC < CPU
|
|
45
47
|
end
|
46
48
|
|
47
49
|
Sym = @i_to_s.sort.inject({}) { |h, (k, v)| h.update k => v.to_sym }
|
48
|
-
def symbolic ; Sym[@i] end
|
50
|
+
def symbolic(di=nil) ; Sym[@i] end
|
49
51
|
def render ; [self.class.i_to_s[@i] || "spr#@i"] end
|
50
52
|
end
|
51
53
|
|
52
54
|
# floating point
|
53
|
-
class FPR
|
55
|
+
class FPR < Reg
|
54
56
|
attr_accessor :i
|
55
57
|
def initialize(i)
|
56
58
|
@i = i
|
57
59
|
end
|
58
60
|
|
59
|
-
|
60
|
-
|
61
|
+
@s_to_i = (0..31).inject({}) { |h, i| h.update "fp#{i}" => i }
|
62
|
+
@i_to_s = @s_to_i.invert
|
63
|
+
Sym = @s_to_i.sort.transpose.last
|
61
64
|
end
|
62
65
|
|
63
66
|
# machine state reg
|
64
67
|
class MSR < Reg
|
65
|
-
def symbolic ; :msr end
|
68
|
+
def symbolic(di=nil) ; :msr end
|
66
69
|
def render ; ['msr'] end
|
67
70
|
end
|
68
71
|
|
@@ -73,8 +76,10 @@ class PowerPC < CPU
|
|
73
76
|
@i = i
|
74
77
|
end
|
75
78
|
|
76
|
-
|
77
|
-
|
79
|
+
@s_to_i = (0..31).inject({}) { |h, i| h.update "cr#{i}" => i }
|
80
|
+
@i_to_s = @s_to_i.invert
|
81
|
+
Sym = @s_to_i.sort.transpose.last
|
82
|
+
def symbolic(di=nil) ; "cr#@i".to_sym end
|
78
83
|
end
|
79
84
|
|
80
85
|
# indirection : reg+reg or reg+16b_off
|
@@ -85,17 +90,17 @@ class PowerPC < CPU
|
|
85
90
|
@base, @offset = base, offset
|
86
91
|
end
|
87
92
|
|
88
|
-
def symbolic(
|
93
|
+
def symbolic(di=nil)
|
89
94
|
b = @base.symbolic
|
90
95
|
b = nil if b == :r0 # XXX is it true ?
|
91
96
|
o = @offset
|
92
|
-
o = o.symbolic if o.kind_of?
|
93
|
-
Indirection[Expression[b, :+, o].reduce, 4,
|
97
|
+
o = o.symbolic if o.kind_of?(Reg)
|
98
|
+
Indirection[Expression[b, :+, o].reduce, 4, (di.address if di)]
|
94
99
|
end
|
95
100
|
|
96
101
|
include Renderable
|
97
102
|
def render
|
98
|
-
if @offset.kind_of?
|
103
|
+
if @offset.kind_of?(Reg)
|
99
104
|
['(', @base, ' + ', @offset, ')']
|
100
105
|
else
|
101
106
|
[@offset, '(', @base, ')']
|
@@ -4,14 +4,17 @@
|
|
4
4
|
# Licence is LGPL, see LICENCE in the top-level directory
|
5
5
|
|
6
6
|
|
7
|
-
require 'metasm/ppc/main'
|
7
|
+
require 'metasm/cpu/ppc/main'
|
8
8
|
|
9
9
|
module Metasm
|
10
10
|
class PowerPC
|
11
11
|
def addop(name, bin, *argprops)
|
12
|
-
o = Opcode.new name, bin
|
13
|
-
|
14
|
-
|
12
|
+
o = Opcode.new name, bin
|
13
|
+
argprops.each { |a|
|
14
|
+
o.args << a if @valid_args[a]
|
15
|
+
o.fields[a] = [@fields_mask[a], @fields_shift[a]] if @fields_mask[a]
|
16
|
+
o.props[a] = true if @valid_props[a]
|
17
|
+
}
|
15
18
|
@opcode_list << o
|
16
19
|
end
|
17
20
|
|
@@ -36,7 +39,7 @@ class PowerPC
|
|
36
39
|
addop_branch(nbase+'dz', bin|(0b10010<<21), :ign_bo_at2, :stopexec, *argprops) if not argprops.include? :ctr
|
37
40
|
addop_branch(nbase+'dnz', bin|(0b10000<<21), :ign_bo_at2, :stopexec, *argprops) if not argprops.include? :ctr
|
38
41
|
|
39
|
-
#
|
42
|
+
# conditional
|
40
43
|
%w[lt gt eq so].each_with_index { |cd, i|
|
41
44
|
ncd = {'lt' => 'gte', 'gt' => 'lte', 'eq' => 'ne', 'so' => 'nso'}[cd]
|
42
45
|
addop_branch(nbase+cd, bin|(0b1100<<21)|(i<<16), :ign_bo_at, *argprops)
|
@@ -118,12 +121,14 @@ class PowerPC
|
|
118
121
|
@fields_mask[:ra_i16s] = (@fields_mask[:ds] << @fields_shift[:d]) | (@fields_mask[:ra] << @fields_shift[:ra])
|
119
122
|
@fields_mask[:ra_i16q] = (@fields_mask[:dq] << @fields_shift[:d]) | (@fields_mask[:ra] << @fields_shift[:ra])
|
120
123
|
|
124
|
+
@valid_args = @fields_mask.dup
|
125
|
+
[:ign_bo_zzz, :ign_bo_z, :ign_bo_at, :ign_bo_at2, :aa, :lk, :oe, :rc, :l].each { |k| @valid_args.delete k }
|
121
126
|
|
122
127
|
addop_branch 'b', 0x48000000, :li, :stopexec
|
123
128
|
addop_branchcond 'b', 0x40000000, :bd
|
124
129
|
addop_branchcond 'b', 0x4C000020, :lr
|
125
130
|
addop_branchcond 'b', 0x4C000420, :ctr
|
126
|
-
|
131
|
+
|
127
132
|
addop 'sc', 0x44000002, :lev
|
128
133
|
addop 'crand', 0x4C000202, :bt, :ba, :bb
|
129
134
|
addop 'crxor', 0x4C000182, :bt, :ba, :bb
|
@@ -0,0 +1,55 @@
|
|
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/ppc/opcodes'
|
8
|
+
require 'metasm/parse'
|
9
|
+
|
10
|
+
module Metasm
|
11
|
+
class PowerPC
|
12
|
+
# TODO
|
13
|
+
def parse_arg_valid?(op, sym, arg)
|
14
|
+
case sym
|
15
|
+
when :ra, :rb, :rs, :rt; arg.kind_of?(GPR)
|
16
|
+
when :fra, :frb, :frc, :frs, :frt; arg.kind_of?(FPR)
|
17
|
+
when :ra_i16, :ra_i16s, :ra_i16q; arg.kind_of?(Memref)
|
18
|
+
when :bd, :d, :ds, :dq, :si, :ui, :li, :sh, :mb, :me, :mb_, :me_, :u; arg.kind_of?(Expression)
|
19
|
+
when :ba, :bf, :bfa, :bt; arg.kind_of?(CR)
|
20
|
+
when :ign_bo_zzz, :ign_bo_z, :ign_bo_at, :ign_bo_at2, :aa, :lk, :oe, :rc, :l; # ?
|
21
|
+
when :bb, :bh, :flm, :fxm, :l_, :l__, :lev, :nb, :sh_, :spr, :sr, :tbr, :th, :to
|
22
|
+
# TODO
|
23
|
+
else raise "internal error: mips arg #{sym.inspect}"
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def parse_argument(pgm)
|
28
|
+
pgm.skip_space
|
29
|
+
return if not tok = pgm.readtok
|
30
|
+
if tok.type == :string
|
31
|
+
return GPR.new(GPR.s_to_i[tok.raw]) if GPR.s_to_i[tok.raw]
|
32
|
+
return SPR.new(SPR.s_to_i[tok.raw]) if SPR.s_to_i[tok.raw]
|
33
|
+
return FPR.new(FPR.s_to_i[tok.raw]) if FPR.s_to_i[tok.raw]
|
34
|
+
return CR.new(CR.s_to_i[tok.raw]) if CR.s_to_i[tok.raw]
|
35
|
+
return MSR.new if tok.raw == 'msr'
|
36
|
+
end
|
37
|
+
pgm.unreadtok tok
|
38
|
+
arg = Expression.parse pgm
|
39
|
+
pgm.skip_space
|
40
|
+
# check memory indirection: 'off(base reg)' # XXX scaled index ?
|
41
|
+
if arg and pgm.nexttok and pgm.nexttok.type == :punct and pgm.nexttok.raw == '('
|
42
|
+
pgm.readtok
|
43
|
+
pgm.skip_space_eol
|
44
|
+
ntok = pgm.readtok
|
45
|
+
raise tok, "Invalid base #{ntok}" unless ntok and ntok.type == :string and GPR.s_to_i[ntok.raw]
|
46
|
+
base = GPR.new GPR.s_to_i[ntok.raw]
|
47
|
+
pgm.skip_space_eol
|
48
|
+
ntok = pgm.readtok
|
49
|
+
raise tok, "Invalid memory reference, ')' expected" if not ntok or ntok.type != :punct or ntok.raw != ')'
|
50
|
+
arg = Memref.new base, arg
|
51
|
+
end
|
52
|
+
arg
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
@@ -0,0 +1,116 @@
|
|
1
|
+
# This file is part of Metasm, the Ruby assembly manipulation suite
|
2
|
+
# Copyright (C) 2006-2010 Yoann GUILLOT
|
3
|
+
#
|
4
|
+
# Licence is LGPL, see LICENCE in the top-level directory
|
5
|
+
|
6
|
+
require 'metasm/cpu/python/opcodes'
|
7
|
+
require 'metasm/decode'
|
8
|
+
|
9
|
+
module Metasm
|
10
|
+
class Python
|
11
|
+
def build_bin_lookaside
|
12
|
+
opcode_list.inject({}) { |la, op| la.update op.bin => op }
|
13
|
+
end
|
14
|
+
|
15
|
+
def decode_findopcode(edata)
|
16
|
+
di = DecodedInstruction.new(self)
|
17
|
+
|
18
|
+
byte = edata.decode_imm(:u8, :little)
|
19
|
+
|
20
|
+
di if di.opcode = @bin_lookaside[byte]
|
21
|
+
end
|
22
|
+
|
23
|
+
def decode_instr_op(edata, di)
|
24
|
+
di.bin_length = 1
|
25
|
+
|
26
|
+
di.instruction.opname = di.opcode.name
|
27
|
+
|
28
|
+
di.opcode.args.each { |a|
|
29
|
+
case a
|
30
|
+
when :cmp
|
31
|
+
di.bin_length += 2
|
32
|
+
v = edata.decode_imm(:i16, @endianness)
|
33
|
+
di.instruction.args << (CMP_OP[v] || Expression[v])
|
34
|
+
when :i16
|
35
|
+
di.bin_length += 2
|
36
|
+
di.instruction.args << Expression[edata.decode_imm(:i16, @endianness)]
|
37
|
+
when :u8
|
38
|
+
di.bin_length += 1
|
39
|
+
di.instruction.args << Expression[edata.decode_imm(:u8, @endianness)]
|
40
|
+
else
|
41
|
+
raise "unsupported arg #{a.inspect}"
|
42
|
+
end
|
43
|
+
}
|
44
|
+
|
45
|
+
return if edata.ptr > edata.length
|
46
|
+
|
47
|
+
di
|
48
|
+
end
|
49
|
+
|
50
|
+
def decode_instr_interpret(di, addr)
|
51
|
+
case di.opcode.name
|
52
|
+
when 'LOAD_CONST'
|
53
|
+
if c = prog_code(addr)
|
54
|
+
cst = c[:consts][di.instruction.args.first.reduce]
|
55
|
+
if cst.kind_of? Hash and cst[:type] == :code
|
56
|
+
di.add_comment "lambda #{Expression[cst[:fileoff]]}"
|
57
|
+
else
|
58
|
+
di.add_comment cst.inspect
|
59
|
+
end
|
60
|
+
end
|
61
|
+
when 'LOAD_NAME', 'LOAD_ATTR', 'LOAD_GLOBAL', 'STORE_NAME', 'IMPORT_NAME', 'LOAD_FAST'
|
62
|
+
if c = prog_code(addr)
|
63
|
+
di.add_comment c[:names][di.instruction.args.first.reduce].inspect
|
64
|
+
end
|
65
|
+
end
|
66
|
+
di
|
67
|
+
end
|
68
|
+
|
69
|
+
def init_backtrace_binding
|
70
|
+
@backtrace_binding ||= {}
|
71
|
+
|
72
|
+
opcode_list.each { |op|
|
73
|
+
binding = case op
|
74
|
+
when 'nop'; lambda { |*a| {} }
|
75
|
+
end
|
76
|
+
@backtrace_binding[op] ||= binding if binding
|
77
|
+
}
|
78
|
+
|
79
|
+
@backtrace_binding
|
80
|
+
end
|
81
|
+
|
82
|
+
def get_xrefs_x(dasm, di)
|
83
|
+
return [] if not di.opcode.props[:setip]
|
84
|
+
|
85
|
+
arg = case di.opcode.name
|
86
|
+
when 'JUMP_FORWARD', 'FOR_ITER'
|
87
|
+
# relative offset
|
88
|
+
di.instruction.args.last.reduce + di.next_addr
|
89
|
+
when 'CALL_FUNCTION_VAR'
|
90
|
+
'lol'
|
91
|
+
when /CALL/
|
92
|
+
:unknown
|
93
|
+
else
|
94
|
+
# absolute offset from :code start
|
95
|
+
off = di.instruction.args.last.reduce
|
96
|
+
if c = prog_code(di)
|
97
|
+
off += c[:fileoff]
|
98
|
+
end
|
99
|
+
off
|
100
|
+
end
|
101
|
+
|
102
|
+
[Expression[(arg.kind_of?(Var) ? arg.symbolic : arg)]]
|
103
|
+
end
|
104
|
+
|
105
|
+
def prog_code(addr)
|
106
|
+
addr = addr.address if addr.kind_of? DecodedInstruction
|
107
|
+
@last_prog_code ||= nil
|
108
|
+
return @last_prog_code if @last_prog_code and @last_prog_code[:fileoff] <= addr and @last_prog_code[:fileoff] + @last_prog_code[:code].length > addr
|
109
|
+
@last_prog_code = @program.code_at_off(addr) if @program
|
110
|
+
end
|
111
|
+
|
112
|
+
def backtrace_is_function_return(expr, di=nil)
|
113
|
+
#Expression[expr].reduce == Expression['wtf']
|
114
|
+
end
|
115
|
+
end
|
116
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
# This file is part of Metasm, the Ruby assembly manipulation suite
|
2
|
+
# Copyright (C) 2006-2010 Yoann GUILLOT
|
3
|
+
#
|
4
|
+
# Licence is LGPL, see LICENCE in the top-level directory
|
5
|
+
|
6
|
+
require 'metasm/main'
|
7
|
+
|
8
|
+
module Metasm
|
9
|
+
class Python < CPU
|
10
|
+
def initialize(prog = nil)
|
11
|
+
super()
|
12
|
+
@program = prog
|
13
|
+
@endianness = (prog.respond_to?(:endianness) ? prog.endianness : :little)
|
14
|
+
@size = (prog.respond_to?(:size) ? prog.size : 32)
|
15
|
+
end
|
16
|
+
|
17
|
+
class Var
|
18
|
+
include Renderable
|
19
|
+
|
20
|
+
attr_accessor :i
|
21
|
+
|
22
|
+
def initialize(i); @i = i end
|
23
|
+
|
24
|
+
def ==(o)
|
25
|
+
o.class == self.class and o.i == i
|
26
|
+
end
|
27
|
+
|
28
|
+
def symbolic(di=nil); "var_#{@i}".to_sym end
|
29
|
+
|
30
|
+
def render
|
31
|
+
["var_#@i"]
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,180 @@
|
|
1
|
+
# This file is part of Metasm, the Ruby assembly manipulation suite
|
2
|
+
# Copyright (C) 2006-2010 Yoann GUILLOT
|
3
|
+
#
|
4
|
+
# Licence is LGPL, see LICENCE in the top-level directory
|
5
|
+
|
6
|
+
require 'metasm/cpu/python/main'
|
7
|
+
|
8
|
+
module Metasm
|
9
|
+
class Python
|
10
|
+
CMP_OP = %w[< <= == != > >= in not_in is is_not exch]
|
11
|
+
|
12
|
+
def addop(name, bin, *args)
|
13
|
+
o = Opcode.new(name)
|
14
|
+
o.bin = bin
|
15
|
+
|
16
|
+
args.each { |a|
|
17
|
+
o.args << a if @valid_args[a]
|
18
|
+
o.props[a] = true if @valid_props[a]
|
19
|
+
}
|
20
|
+
o.args << :i16 if o.bin >= 90 and o.props.empty? # HAVE_ARGUMENT
|
21
|
+
|
22
|
+
@opcode_list << o
|
23
|
+
end
|
24
|
+
|
25
|
+
def init_opcode_list
|
26
|
+
@opcode_list = []
|
27
|
+
|
28
|
+
@valid_args[:u8] = true
|
29
|
+
@valid_args[:i16] = true
|
30
|
+
@valid_args[:cmp] = true
|
31
|
+
|
32
|
+
addop 'STOP_CODE', 0, :stopexec
|
33
|
+
addop 'POP_TOP', 1
|
34
|
+
addop 'ROT_TWO', 2
|
35
|
+
addop 'ROT_THREE', 3
|
36
|
+
addop 'DUP_TOP', 4
|
37
|
+
addop 'ROT_FOUR', 5
|
38
|
+
addop 'NOP', 9
|
39
|
+
|
40
|
+
addop 'UNARY_POSITIVE', 10
|
41
|
+
addop 'UNARY_NEGATIVE', 11
|
42
|
+
addop 'UNARY_NOT', 12
|
43
|
+
addop 'UNARY_CONVERT', 13
|
44
|
+
|
45
|
+
addop 'UNARY_INVERT', 15
|
46
|
+
|
47
|
+
addop 'BINARY_POWER', 19
|
48
|
+
|
49
|
+
addop 'BINARY_MULTIPLY', 20
|
50
|
+
addop 'BINARY_DIVIDE', 21
|
51
|
+
addop 'BINARY_MODULO', 22
|
52
|
+
addop 'BINARY_ADD', 23
|
53
|
+
addop 'BINARY_SUBTRACT', 24
|
54
|
+
addop 'BINARY_SUBSCR', 25
|
55
|
+
addop 'BINARY_FLOOR_DIVIDE', 26
|
56
|
+
addop 'BINARY_TRUE_DIVIDE', 27
|
57
|
+
addop 'INPLACE_FLOOR_DIVIDE', 28
|
58
|
+
addop 'INPLACE_TRUE_DIVIDE', 29
|
59
|
+
|
60
|
+
addop 'SLICE', 30
|
61
|
+
addop 'SLICE_1', 31
|
62
|
+
addop 'SLICE_2', 32
|
63
|
+
addop 'SLICE_3', 33
|
64
|
+
|
65
|
+
addop 'STORE_SLICE', 40
|
66
|
+
addop 'STORE_SLICE_1', 41
|
67
|
+
addop 'STORE_SLICE_2', 42
|
68
|
+
addop 'STORE_SLICE_3', 43
|
69
|
+
|
70
|
+
addop 'DELETE_SLICE', 50
|
71
|
+
addop 'DELETE_SLICE_1', 51
|
72
|
+
addop 'DELETE_SLICE_2', 52
|
73
|
+
addop 'DELETE_SLICE_3', 53
|
74
|
+
|
75
|
+
addop 'STORE_MAP', 54
|
76
|
+
addop 'INPLACE_ADD', 55
|
77
|
+
addop 'INPLACE_SUBTRACT', 56
|
78
|
+
addop 'INPLACE_MULTIPLY', 57
|
79
|
+
addop 'INPLACE_DIVIDE', 58
|
80
|
+
addop 'INPLACE_MODULO', 59
|
81
|
+
addop 'STORE_SUBSCR', 60
|
82
|
+
addop 'DELETE_SUBSCR', 61
|
83
|
+
|
84
|
+
addop 'BINARY_LSHIFT', 62
|
85
|
+
addop 'BINARY_RSHIFT', 63
|
86
|
+
addop 'BINARY_AND', 64
|
87
|
+
addop 'BINARY_XOR', 65
|
88
|
+
addop 'BINARY_OR', 66
|
89
|
+
addop 'INPLACE_POWER', 67
|
90
|
+
addop 'GET_ITER', 68
|
91
|
+
|
92
|
+
addop 'PRINT_EXPR', 70
|
93
|
+
addop 'PRINT_ITEM', 71
|
94
|
+
addop 'PRINT_NEWLINE', 72
|
95
|
+
addop 'PRINT_ITEM_TO', 73
|
96
|
+
addop 'PRINT_NEWLINE_TO', 74
|
97
|
+
addop 'INPLACE_LSHIFT', 75
|
98
|
+
addop 'INPLACE_RSHIFT', 76
|
99
|
+
addop 'INPLACE_AND', 77
|
100
|
+
addop 'INPLACE_XOR', 78
|
101
|
+
addop 'INPLACE_OR', 79
|
102
|
+
addop 'BREAK_LOOP', 80
|
103
|
+
addop 'WITH_CLEANUP', 81
|
104
|
+
addop 'LOAD_LOCALS', 82
|
105
|
+
addop 'RETURN_VALUE', 83
|
106
|
+
addop 'IMPORT_STAR', 84
|
107
|
+
addop 'EXEC_STMT', 85
|
108
|
+
addop 'YIELD_VALUE', 86
|
109
|
+
addop 'POP_BLOCK', 87
|
110
|
+
addop 'END_FINALLY', 88
|
111
|
+
addop 'BUILD_CLASS', 89
|
112
|
+
|
113
|
+
#addop 'HAVE_ARGUMENT', 90 #/* Opcodes from here have an argument: */
|
114
|
+
|
115
|
+
addop 'STORE_NAME', 90 #/* Index in name list */
|
116
|
+
addop 'DELETE_NAME', 91 #/* "" */
|
117
|
+
addop 'UNPACK_SEQUENCE', 92 #/* Number of sequence items */
|
118
|
+
addop 'FOR_ITER', 93, :setip
|
119
|
+
addop 'LIST_APPEND', 94
|
120
|
+
|
121
|
+
addop 'STORE_ATTR', 95 #/* Index in name list */
|
122
|
+
addop 'DELETE_ATTR', 96 #/* "" */
|
123
|
+
addop 'STORE_GLOBAL', 97 #/* "" */
|
124
|
+
addop 'DELETE_GLOBAL', 98 #/* "" */
|
125
|
+
addop 'DUP_TOPX', 99 #/* number of items to duplicate */
|
126
|
+
addop 'LOAD_CONST', 100 #/* Index in const list */
|
127
|
+
addop 'LOAD_NAME', 101 #/* Index in name list */
|
128
|
+
addop 'BUILD_TUPLE', 102 #/* Number of tuple items */
|
129
|
+
addop 'BUILD_LIST', 103 #/* Number of list items */
|
130
|
+
addop 'BUILD_SET', 104 #/* Number of set items */
|
131
|
+
addop 'BUILD_MAP', 105 #/* Always zero for now */
|
132
|
+
addop 'LOAD_ATTR', 106 #/* Index in name list */
|
133
|
+
addop 'COMPARE_OP', 107, :cmp #/* Comparison operator */
|
134
|
+
addop 'IMPORT_NAME', 108 #/* Index in name list */
|
135
|
+
addop 'IMPORT_FROM', 109 #/* Index in name list */
|
136
|
+
addop 'JUMP_FORWARD', 110, :setip, :stopexec #/* Number of bytes to skip */
|
137
|
+
|
138
|
+
addop 'JUMP_IF_FALSE_OR_POP', 111, :setip #/* Target byte offset from beginning of code */
|
139
|
+
addop 'JUMP_IF_TRUE_OR_POP', 112, :setip #/* "" */
|
140
|
+
addop 'JUMP_ABSOLUTE', 113, :setip, :stopexec #/* "" */
|
141
|
+
addop 'POP_JUMP_IF_FALSE', 114, :setip #/* "" */
|
142
|
+
addop 'POP_JUMP_IF_TRUE', 115, :setip #/* "" */
|
143
|
+
|
144
|
+
addop 'LOAD_GLOBAL', 116 #/* Index in name list */
|
145
|
+
|
146
|
+
addop 'CONTINUE_LOOP', 119 #/* Start of loop (absolute) */
|
147
|
+
addop 'SETUP_LOOP', 120 #/* Target address (relative) */
|
148
|
+
addop 'SETUP_EXCEPT', 121 #/* "" */
|
149
|
+
addop 'SETUP_FINALLY', 122 #/* "" */
|
150
|
+
|
151
|
+
addop 'LOAD_FAST', 124 #/* Local variable number */
|
152
|
+
addop 'STORE_FAST', 125 #/* Local variable number */
|
153
|
+
addop 'DELETE_FAST', 126 #/* Local variable number */
|
154
|
+
|
155
|
+
addop 'RAISE_VARARGS', 130 #/* Number of raise arguments (1, 2 or 3) */
|
156
|
+
#/* CALL_FUNCTION_XXX opcodes defined below depend on this definition */
|
157
|
+
addop 'CALL_FUNCTION', 131, :u8, :u8, :setip #/* #args + (#kwargs<<8) */
|
158
|
+
addop 'MAKE_FUNCTION', 132 #/* #defaults */
|
159
|
+
addop 'BUILD_SLICE', 133 #/* Number of items */
|
160
|
+
|
161
|
+
addop 'MAKE_CLOSURE', 134 #/* #free vars */
|
162
|
+
addop 'LOAD_CLOSURE', 135 #/* Load free variable from closure */
|
163
|
+
addop 'LOAD_DEREF', 136 #/* Load and dereference from closure cell */
|
164
|
+
addop 'STORE_DEREF', 137 #/* Store into cell */
|
165
|
+
|
166
|
+
#/* The next 3 opcodes must be contiguous and satisfy (CALL_FUNCTION_VAR - CALL_FUNCTION) & 3 == 1 */
|
167
|
+
addop 'CALL_FUNCTION_VAR', 140, :u8, :u8, :setip #/* #args + (#kwargs<<8) */
|
168
|
+
addop 'CALL_FUNCTION_KW', 141, :u8, :u8, :setip #/* #args + (#kwargs<<8) */
|
169
|
+
addop 'CALL_FUNCTION_VAR_KW', 142, :u8, :u8, :setip #/* #args + (#kwargs<<8) */
|
170
|
+
|
171
|
+
addop 'SETUP_WITH', 143
|
172
|
+
|
173
|
+
#/* Support for opargs more than 16 bits long */
|
174
|
+
addop 'EXTENDED_ARG', 145
|
175
|
+
|
176
|
+
addop 'SET_ADD', 146
|
177
|
+
addop 'MAP_ADD', 147
|
178
|
+
end
|
179
|
+
end
|
180
|
+
end
|