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,247 @@
|
|
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/msp430/opcodes'
|
7
|
+
require 'metasm/decode'
|
8
|
+
|
9
|
+
module Metasm
|
10
|
+
class MSP430
|
11
|
+
def build_opcode_bin_mask(op)
|
12
|
+
op.bin_mask = 0
|
13
|
+
op.fields.each_key { |f|
|
14
|
+
op.bin_mask |= @fields_mask[f] << @fields_shift[f]
|
15
|
+
}
|
16
|
+
op.bin_mask ^= 0xffff
|
17
|
+
end
|
18
|
+
|
19
|
+
def build_bin_lookaside
|
20
|
+
lookaside = Array.new(256) { [] }
|
21
|
+
opcode_list.each { |op|
|
22
|
+
build_opcode_bin_mask op
|
23
|
+
b = (op.bin >> 8) & 255
|
24
|
+
msk = (op.bin_mask >> 8) & 255
|
25
|
+
|
26
|
+
for i in b..(b | (255^msk))
|
27
|
+
lookaside[i] << op if i & msk == b & msk
|
28
|
+
end
|
29
|
+
}
|
30
|
+
lookaside
|
31
|
+
end
|
32
|
+
|
33
|
+
def decode_findopcode(edata)
|
34
|
+
di = DecodedInstruction.new(self)
|
35
|
+
val = edata.decode_imm(:u16, @endianness)
|
36
|
+
edata.ptr -= 2
|
37
|
+
di.opcode = @bin_lookaside[(val >> 8) & 0xff].find { |opcode| (val & opcode.bin_mask) == opcode.bin }
|
38
|
+
di if di.opcode
|
39
|
+
end
|
40
|
+
|
41
|
+
def decode_instr_op(edata, di)
|
42
|
+
before_ptr = edata.ptr
|
43
|
+
op = di.opcode
|
44
|
+
di.instruction.opname = op.name
|
45
|
+
val = edata.decode_imm(:u16, @endianness)
|
46
|
+
|
47
|
+
field_val = lambda{ |f|
|
48
|
+
(val >> @fields_shift[f]) & @fields_mask[f]
|
49
|
+
}
|
50
|
+
|
51
|
+
# must decode rs first
|
52
|
+
vals = {}
|
53
|
+
([:rs, :rd, :r_pc] & op.args).each { |a|
|
54
|
+
mod = { :rs => :as, :rd => :ad, :r_pc => :ad }[a]
|
55
|
+
mod = :as if mod == :ad and not op.fields[mod] # addop_macro1 -> rs + ad
|
56
|
+
|
57
|
+
if a == :r_pc
|
58
|
+
r = Reg.new(0)
|
59
|
+
else
|
60
|
+
r = Reg.new(field_val[a])
|
61
|
+
end
|
62
|
+
|
63
|
+
w = op.props[:byte] ? 1 : 2
|
64
|
+
|
65
|
+
case field_val[mod]
|
66
|
+
when 0
|
67
|
+
if r.i == 3 and a == :rs
|
68
|
+
vals[a] = Expression[0]
|
69
|
+
else
|
70
|
+
vals[a] = r
|
71
|
+
end
|
72
|
+
when 1
|
73
|
+
if r.i == 3 and a == :rs
|
74
|
+
vals[a] = Expression[1]
|
75
|
+
else
|
76
|
+
imm = edata.decode_imm(:u16, @endianness)
|
77
|
+
r = nil if r.i == 2 # [imm]
|
78
|
+
vals[a] = Memref.new(r, imm, w)
|
79
|
+
end
|
80
|
+
when 2
|
81
|
+
if r.i == 3
|
82
|
+
vals[a] = Expression[2]
|
83
|
+
elsif r.i == 2
|
84
|
+
vals[a] = Expression[4]
|
85
|
+
else
|
86
|
+
vals[a] = Memref.new(r, 0, w)
|
87
|
+
end
|
88
|
+
when 3
|
89
|
+
if r.i == 3
|
90
|
+
vals[a] = Expression[-1]
|
91
|
+
elsif r.i == 2
|
92
|
+
vals[a] = Expression[8]
|
93
|
+
elsif r.i == 0 # pc++
|
94
|
+
# XXX order wrt other edata.decode_imm ?
|
95
|
+
vals[a] = Expression[edata.decode_imm(:u16, @endianness)]
|
96
|
+
else
|
97
|
+
vals[a] = Memref.new(r, 0, w, true)
|
98
|
+
end
|
99
|
+
end
|
100
|
+
}
|
101
|
+
|
102
|
+
op.args.each { |a|
|
103
|
+
di.instruction.args << case a
|
104
|
+
when :joff; Expression[2 * Expression.make_signed(field_val[a], 10)]
|
105
|
+
when :rs, :rd, :r_pc; vals[a]
|
106
|
+
else raise SyntaxError, "Internal error: invalid argument #{a} in #{op.name}"
|
107
|
+
end
|
108
|
+
}
|
109
|
+
|
110
|
+
di.bin_length += edata.ptr - before_ptr
|
111
|
+
|
112
|
+
return if edata.ptr > edata.length
|
113
|
+
|
114
|
+
di
|
115
|
+
end
|
116
|
+
|
117
|
+
def decode_instr_interpret(di, addr)
|
118
|
+
if di.opcode.props[:setip] and di.opcode.name =~ /^j/
|
119
|
+
delta = di.instruction.args.last.reduce
|
120
|
+
arg = Expression[[addr, :+, di.bin_length], :+, delta].reduce
|
121
|
+
di.instruction.args[-1] = Expression[arg]
|
122
|
+
end
|
123
|
+
|
124
|
+
di
|
125
|
+
end
|
126
|
+
|
127
|
+
def backtrace_binding
|
128
|
+
@backtrace_binding ||= init_backtrace_binding
|
129
|
+
end
|
130
|
+
|
131
|
+
def init_backtrace_binding
|
132
|
+
@backtrace_binding ||= {}
|
133
|
+
|
134
|
+
opcode_list.map { |ol| ol.name }.uniq.each { |op|
|
135
|
+
@backtrace_binding[op] ||= case op
|
136
|
+
when 'mov'; lambda { |di, a0, a1| { a0 => Expression[a1] }}
|
137
|
+
when 'cmp', 'test'; lambda { |di, *a| {} } # TODO
|
138
|
+
when 'add', 'adc' ; lambda { |di, a0, a1| { a0 => Expression[a0, :+, a1] } }
|
139
|
+
when 'sub', 'sbc'; lambda { |di, a0, a1| { a0 => Expression[a0, :-, a1] } }
|
140
|
+
when 'and'; lambda { |di, a0, a1| { a0 => Expression[a0, :&, a1] } }
|
141
|
+
when 'or'; lambda { |di, a0, a1| { a0 => Expression[a0, :|, a1] } }
|
142
|
+
when 'xor'; lambda { |di, a0, a1| { a0 => Expression[a0, :^, a1] } }
|
143
|
+
when 'push'; lambda { |di, a0| { Indirection[:sp, 2] => Expression[a0],
|
144
|
+
:sp => Expression[:sp, :-, 2] } }
|
145
|
+
when 'call'; lambda { |di, a0| { Indirection[:sp, 2] => Expression[di.next_addr],
|
146
|
+
:sp => Expression[:sp, :-, 2] } }
|
147
|
+
when 'pop'; lambda { |di, a0| { a0 => Expression[Indirection[:sp, 2]],
|
148
|
+
:sp => Expression[:sp, :+, 2] } }
|
149
|
+
when 'ret'; lambda { |di| { :sp => Expression[:sp, :+, 2] } }
|
150
|
+
when 'reti'; lambda { |di| { :sp => Expression[:sp, :+, 4] } }
|
151
|
+
when /^j/; lambda { |di, a0| {} }
|
152
|
+
end
|
153
|
+
}
|
154
|
+
|
155
|
+
@backtrace_binding
|
156
|
+
end
|
157
|
+
|
158
|
+
def get_backtrace_binding(di)
|
159
|
+
a = di.instruction.args.map { |arg|
|
160
|
+
case arg
|
161
|
+
when Reg; arg.symbolic
|
162
|
+
when Memref; arg.symbolic(di.address)
|
163
|
+
else arg
|
164
|
+
end
|
165
|
+
}
|
166
|
+
|
167
|
+
if binding = backtrace_binding[di.opcode.basename]
|
168
|
+
bd = binding[di, *a] || {}
|
169
|
+
di.instruction.args.grep(Memref).each { |m|
|
170
|
+
next unless r = m.base and m.postincr
|
171
|
+
r = m.base.symbolic
|
172
|
+
bd[r] ||= Expression[r, :+, m.size]
|
173
|
+
}
|
174
|
+
bd
|
175
|
+
else
|
176
|
+
puts "unhandled instruction to backtrace: #{di}" if $VERBOSE
|
177
|
+
{ :incomplete_binding => Expression[1] }
|
178
|
+
end
|
179
|
+
end
|
180
|
+
|
181
|
+
def get_xrefs_x(dasm, di)
|
182
|
+
return [] if not di.opcode.props[:setip]
|
183
|
+
|
184
|
+
case di.instruction.opname
|
185
|
+
when 'ret'
|
186
|
+
return [Indirection[:sp, 2, di.address]]
|
187
|
+
when 'reti'
|
188
|
+
return [Indirection[[:sp, :+, 2], 2, di.address]]
|
189
|
+
end
|
190
|
+
|
191
|
+
# XXX add pc, 42 ?
|
192
|
+
val = di.instruction.args[0]
|
193
|
+
case val
|
194
|
+
when Reg; val = val.symbolic
|
195
|
+
when Memref; val = val.symbolic(di.address)
|
196
|
+
end
|
197
|
+
|
198
|
+
[Expression[val]]
|
199
|
+
end
|
200
|
+
|
201
|
+
def backtrace_is_function_return(expr, di=nil)
|
202
|
+
expr = Expression[expr].reduce_rec
|
203
|
+
expr.kind_of?(Indirection) and expr.len == 2 and expr.target == Expression[:sp]
|
204
|
+
end
|
205
|
+
|
206
|
+
# updates the function backtrace_binding
|
207
|
+
# if the function is big and no specific register is given, do nothing (the binding will be lazily updated later, on demand)
|
208
|
+
def backtrace_update_function_binding(dasm, faddr, f, retaddrlist, *wantregs)
|
209
|
+
b = f.backtrace_binding
|
210
|
+
|
211
|
+
bt_val = lambda { |r|
|
212
|
+
next if not retaddrlist
|
213
|
+
b[r] = Expression::Unknown
|
214
|
+
bt = []
|
215
|
+
retaddrlist.each { |retaddr|
|
216
|
+
bt |= dasm.backtrace(Expression[r], retaddr, :include_start => true,
|
217
|
+
:snapshot_addr => faddr, :origin => retaddr)
|
218
|
+
}
|
219
|
+
if bt.length != 1
|
220
|
+
b[r] = Expression::Unknown
|
221
|
+
else
|
222
|
+
b[r] = bt.first
|
223
|
+
end
|
224
|
+
}
|
225
|
+
|
226
|
+
if not wantregs.empty?
|
227
|
+
wantregs.each(&bt_val)
|
228
|
+
else
|
229
|
+
bt_val[:sp]
|
230
|
+
end
|
231
|
+
|
232
|
+
b
|
233
|
+
end
|
234
|
+
|
235
|
+
def replace_instr_arg_immediate(i, old, new)
|
236
|
+
i.args.map! { |a|
|
237
|
+
case a
|
238
|
+
when Expression; a == old ? new : Expression[a.bind(old => new).reduce]
|
239
|
+
when Memref
|
240
|
+
a.base = (a.base == old ? new : Expression[a.base.bind(old => new).reduce]) if a.base.kind_of?(Expression)
|
241
|
+
a
|
242
|
+
else a
|
243
|
+
end
|
244
|
+
}
|
245
|
+
end
|
246
|
+
end
|
247
|
+
end
|
@@ -0,0 +1,62 @@
|
|
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
|
+
|
10
|
+
class MSP430 < CPU
|
11
|
+
def initialize(e = :little)
|
12
|
+
super()
|
13
|
+
@endianness = e
|
14
|
+
@size = 16
|
15
|
+
end
|
16
|
+
|
17
|
+
class Reg
|
18
|
+
include Renderable
|
19
|
+
Sym = (4..15).inject(0 => :pc, 1 => :sp, 2 => :flags, 3 => :rzero) { |h, i| h.update i => "r#{i}".to_sym }
|
20
|
+
|
21
|
+
attr_accessor :i
|
22
|
+
def initialize(i) ; @i = i end
|
23
|
+
def symbolic ; Sym[@i] end
|
24
|
+
def render ; [Sym[@i].to_s] end
|
25
|
+
def ==(o) ; o.class == self.class and o.i == @i end
|
26
|
+
end
|
27
|
+
|
28
|
+
class Memref
|
29
|
+
attr_accessor :base, :offset, :size, :postincr
|
30
|
+
|
31
|
+
def initialize(base, offset = 0, size = nil, postincr = false)
|
32
|
+
@base = base
|
33
|
+
@offset = Expression[offset]
|
34
|
+
@size = size
|
35
|
+
@postincr = postincr
|
36
|
+
end
|
37
|
+
|
38
|
+
def symbolic(orig=nil)
|
39
|
+
r = @base.symbolic if @base
|
40
|
+
e = Expression[r, :+, @offset].reduce
|
41
|
+
Indirection[e, (@size || 1), orig]
|
42
|
+
end
|
43
|
+
|
44
|
+
include Renderable
|
45
|
+
|
46
|
+
def render
|
47
|
+
b = @base
|
48
|
+
b = @base.to_s + '++' if @base and @postincr
|
49
|
+
p = Expression[b, :+, @offset].reduce
|
50
|
+
Indirection[p, @size].render
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
def init_opcode_list
|
55
|
+
init
|
56
|
+
end
|
57
|
+
|
58
|
+
def dbg_register_list
|
59
|
+
@dbg_register_list ||= Reg::Sym.sort.transpose.last
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
@@ -0,0 +1,101 @@
|
|
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/msp430/main'
|
7
|
+
|
8
|
+
module Metasm
|
9
|
+
class MSP430
|
10
|
+
def addop(name, bin, *args)
|
11
|
+
o = Opcode.new name, bin
|
12
|
+
|
13
|
+
args.each { |a|
|
14
|
+
o.args << a if @valid_args[a]
|
15
|
+
o.props[a] = true if @valid_props[a]
|
16
|
+
o.fields[a] = [@fields_mask[a], @fields_shift[a]] if @fields_mask[a]
|
17
|
+
}
|
18
|
+
|
19
|
+
@opcode_list << o
|
20
|
+
end
|
21
|
+
|
22
|
+
def init
|
23
|
+
@opcode_list = []
|
24
|
+
|
25
|
+
@fields_mask = {
|
26
|
+
:as => 3, # adressing mode
|
27
|
+
:ad => 1, # adressing mode
|
28
|
+
:rd => 0xf,
|
29
|
+
:rs => 0xf,
|
30
|
+
:joff => 0x3ff, # signed offset for jumps
|
31
|
+
}
|
32
|
+
@fields_shift = {
|
33
|
+
:as => 4,
|
34
|
+
:ad => 7,
|
35
|
+
:rd => 0,
|
36
|
+
:rs => 8,
|
37
|
+
:joff => 0,
|
38
|
+
}
|
39
|
+
@valid_args = { :r_pc => true, :rd => true, :rs => true, :joff => true }
|
40
|
+
@valid_props = { :setip => true, :stopexec => true, :saveip => true, :byte => true }
|
41
|
+
|
42
|
+
# https://en.wikipedia.org/wiki/TI_MSP430
|
43
|
+
|
44
|
+
addop_macro1 'rrc', 0, :byte
|
45
|
+
addop_macro1 'swpb', 1
|
46
|
+
addop_macro1 'rra', 2, :byte
|
47
|
+
addop_macro1 'sxt', 3
|
48
|
+
addop_macro1 'push', 4, :byte
|
49
|
+
addop_macro1 'call', 5, :setip, :stopexec, :saveip
|
50
|
+
|
51
|
+
addop 'reti', 0b000100_110_0000000
|
52
|
+
|
53
|
+
addop_macro2 'jnz', 0
|
54
|
+
addop_macro2 'jz', 1
|
55
|
+
addop_macro2 'jnc', 2
|
56
|
+
addop_macro2 'jc', 3
|
57
|
+
addop_macro2 'jb', 4 # 'jn' jump if negative => jl unsigned ?
|
58
|
+
addop_macro2 'jge', 5
|
59
|
+
addop_macro2 'jl', 6
|
60
|
+
addop_macro2 'jmp', 7, :stopexec
|
61
|
+
|
62
|
+
addop 'ret', 0x4130, :setip, :stopexec # mov pc, [sp++]
|
63
|
+
addop 'pop', 0x4130, :rd, :ad # mov rd, [sp++]
|
64
|
+
|
65
|
+
addop_macro3 'mov', 4
|
66
|
+
addop_macro3 'add', 5
|
67
|
+
addop_macro3 'adc', 6 # 'addc'
|
68
|
+
addop_macro3 'sbc', 7
|
69
|
+
addop_macro3 'sub', 8
|
70
|
+
addop_macro3 'cmp', 9
|
71
|
+
addop_macro3 'dadd',10 # decimal add with carry
|
72
|
+
addop_macro3 'test',11 # 'bit'
|
73
|
+
addop_macro3 'andn',12 # 'bic'
|
74
|
+
addop_macro3 'or', 13 # 'bis'
|
75
|
+
addop_macro3 'xor', 14
|
76
|
+
addop_macro3 'and', 15
|
77
|
+
end
|
78
|
+
|
79
|
+
def addop_macro1(name, bin, *props)
|
80
|
+
if props.delete :byte
|
81
|
+
addop_byte name, (0b000100 << 10) | (bin << 7), :as, :rd, *props
|
82
|
+
else
|
83
|
+
addop name, (0b000100 << 10) | (bin << 7), :as, :rd, *props
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
def addop_macro2(name, bin, *props)
|
88
|
+
addop name, (0b001 << 13) | (bin << 10), :joff, :setip, *props
|
89
|
+
end
|
90
|
+
|
91
|
+
def addop_macro3(name, bin, *props)
|
92
|
+
addop_byte name, (bin << 12), :r_pc, :ad, :as, :rs, :setip, :stopexec # dst == pc
|
93
|
+
addop_byte name, (bin << 12), :rd, :ad, :as, :rs
|
94
|
+
end
|
95
|
+
|
96
|
+
def addop_byte(name, bin, *props)
|
97
|
+
addop name, bin, *props
|
98
|
+
addop name + '.b', bin | (1 << 6), :byte, *props
|
99
|
+
end
|
100
|
+
end
|
101
|
+
end
|
@@ -4,7 +4,7 @@
|
|
4
4
|
# Licence is LGPL, see LICENCE in the top-level directory
|
5
5
|
|
6
6
|
|
7
|
-
require 'metasm/pic16c/opcodes'
|
7
|
+
require 'metasm/cpu/pic16c/opcodes'
|
8
8
|
require 'metasm/decode'
|
9
9
|
|
10
10
|
module Metasm
|
@@ -17,22 +17,21 @@ class Pic16c
|
|
17
17
|
}
|
18
18
|
op.bin_mask.map! { |v| 255 ^ v }
|
19
19
|
end
|
20
|
-
|
20
|
+
|
21
21
|
def build_bin_lookaside
|
22
22
|
# sets up a hash byte value => list of opcodes that may match
|
23
23
|
# opcode.bin_mask is built here
|
24
24
|
lookaside = Array.new(256) { [] }
|
25
25
|
@opcode_list.each { |op|
|
26
|
-
|
26
|
+
|
27
27
|
build_opcode_bin_mask op
|
28
|
-
|
28
|
+
|
29
29
|
b = op.bin[0]
|
30
30
|
msk = op.bin_mask[0]
|
31
|
-
|
32
|
-
|
31
|
+
|
33
32
|
for i in b..(b | (255^msk))
|
34
33
|
ext if i & msk != b & msk
|
35
|
-
|
34
|
+
|
36
35
|
lookaside[i] << op
|
37
36
|
end
|
38
37
|
}
|