metasm 1.0.1 → 1.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (235) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +1 -0
  3. data/.hgtags +3 -0
  4. data/Gemfile +1 -0
  5. data/INSTALL +61 -0
  6. data/LICENCE +458 -0
  7. data/README +29 -21
  8. data/Rakefile +10 -0
  9. data/TODO +10 -12
  10. data/doc/code_organisation.txt +2 -0
  11. data/doc/core/DynLdr.txt +247 -0
  12. data/doc/core/ExeFormat.txt +43 -0
  13. data/doc/core/Expression.txt +220 -0
  14. data/doc/core/GNUExports.txt +27 -0
  15. data/doc/core/Ia32.txt +236 -0
  16. data/doc/core/SerialStruct.txt +108 -0
  17. data/doc/core/VirtualString.txt +145 -0
  18. data/doc/core/WindowsExports.txt +61 -0
  19. data/doc/core/index.txt +1 -0
  20. data/doc/style.css +6 -3
  21. data/doc/usage/debugger.txt +327 -0
  22. data/doc/usage/index.txt +1 -0
  23. data/doc/use_cases.txt +2 -2
  24. data/metasm.gemspec +22 -0
  25. data/{lib/metasm.rb → metasm.rb} +11 -3
  26. data/{lib/metasm → metasm}/compile_c.rb +13 -7
  27. data/metasm/cpu/arc.rb +8 -0
  28. data/metasm/cpu/arc/decode.rb +425 -0
  29. data/metasm/cpu/arc/main.rb +191 -0
  30. data/metasm/cpu/arc/opcodes.rb +588 -0
  31. data/{lib/metasm → metasm/cpu}/arm.rb +7 -5
  32. data/{lib/metasm → metasm/cpu}/arm/debug.rb +2 -2
  33. data/{lib/metasm → metasm/cpu}/arm/decode.rb +13 -12
  34. data/{lib/metasm → metasm/cpu}/arm/encode.rb +23 -8
  35. data/{lib/metasm → metasm/cpu}/arm/main.rb +0 -3
  36. data/metasm/cpu/arm/opcodes.rb +324 -0
  37. data/{lib/metasm → metasm/cpu}/arm/parse.rb +25 -13
  38. data/{lib/metasm → metasm/cpu}/arm/render.rb +2 -2
  39. data/metasm/cpu/arm64.rb +15 -0
  40. data/metasm/cpu/arm64/debug.rb +38 -0
  41. data/metasm/cpu/arm64/decode.rb +289 -0
  42. data/metasm/cpu/arm64/encode.rb +41 -0
  43. data/metasm/cpu/arm64/main.rb +105 -0
  44. data/metasm/cpu/arm64/opcodes.rb +232 -0
  45. data/metasm/cpu/arm64/parse.rb +20 -0
  46. data/metasm/cpu/arm64/render.rb +95 -0
  47. data/{lib/metasm/ppc.rb → metasm/cpu/bpf.rb} +2 -4
  48. data/metasm/cpu/bpf/decode.rb +142 -0
  49. data/metasm/cpu/bpf/main.rb +60 -0
  50. data/metasm/cpu/bpf/opcodes.rb +81 -0
  51. data/metasm/cpu/bpf/render.rb +41 -0
  52. data/metasm/cpu/cy16.rb +9 -0
  53. data/metasm/cpu/cy16/decode.rb +253 -0
  54. data/metasm/cpu/cy16/main.rb +63 -0
  55. data/metasm/cpu/cy16/opcodes.rb +78 -0
  56. data/metasm/cpu/cy16/render.rb +41 -0
  57. data/metasm/cpu/dalvik.rb +11 -0
  58. data/{lib/metasm → metasm/cpu}/dalvik/decode.rb +35 -13
  59. data/{lib/metasm → metasm/cpu}/dalvik/main.rb +51 -2
  60. data/{lib/metasm → metasm/cpu}/dalvik/opcodes.rb +19 -11
  61. data/metasm/cpu/ia32.rb +17 -0
  62. data/{lib/metasm → metasm/cpu}/ia32/compile_c.rb +5 -7
  63. data/{lib/metasm → metasm/cpu}/ia32/debug.rb +5 -5
  64. data/{lib/metasm → metasm/cpu}/ia32/decode.rb +246 -59
  65. data/{lib/metasm → metasm/cpu}/ia32/decompile.rb +7 -7
  66. data/{lib/metasm → metasm/cpu}/ia32/encode.rb +19 -13
  67. data/{lib/metasm → metasm/cpu}/ia32/main.rb +51 -8
  68. data/metasm/cpu/ia32/opcodes.rb +1424 -0
  69. data/{lib/metasm → metasm/cpu}/ia32/parse.rb +47 -16
  70. data/{lib/metasm → metasm/cpu}/ia32/render.rb +31 -4
  71. data/metasm/cpu/mips.rb +14 -0
  72. data/{lib/metasm → metasm/cpu}/mips/compile_c.rb +1 -1
  73. data/metasm/cpu/mips/debug.rb +42 -0
  74. data/{lib/metasm → metasm/cpu}/mips/decode.rb +46 -16
  75. data/{lib/metasm → metasm/cpu}/mips/encode.rb +4 -3
  76. data/{lib/metasm → metasm/cpu}/mips/main.rb +11 -4
  77. data/{lib/metasm → metasm/cpu}/mips/opcodes.rb +86 -17
  78. data/{lib/metasm → metasm/cpu}/mips/parse.rb +1 -1
  79. data/{lib/metasm → metasm/cpu}/mips/render.rb +1 -1
  80. data/{lib/metasm/dalvik.rb → metasm/cpu/msp430.rb} +1 -1
  81. data/metasm/cpu/msp430/decode.rb +247 -0
  82. data/metasm/cpu/msp430/main.rb +62 -0
  83. data/metasm/cpu/msp430/opcodes.rb +101 -0
  84. data/{lib/metasm → metasm/cpu}/pic16c/decode.rb +6 -7
  85. data/{lib/metasm → metasm/cpu}/pic16c/main.rb +0 -0
  86. data/{lib/metasm → metasm/cpu}/pic16c/opcodes.rb +1 -1
  87. data/{lib/metasm/mips.rb → metasm/cpu/ppc.rb} +4 -4
  88. data/{lib/metasm → metasm/cpu}/ppc/decode.rb +18 -12
  89. data/{lib/metasm → metasm/cpu}/ppc/decompile.rb +3 -3
  90. data/{lib/metasm → metasm/cpu}/ppc/encode.rb +2 -2
  91. data/{lib/metasm → metasm/cpu}/ppc/main.rb +17 -12
  92. data/{lib/metasm → metasm/cpu}/ppc/opcodes.rb +11 -5
  93. data/metasm/cpu/ppc/parse.rb +55 -0
  94. data/metasm/cpu/python.rb +8 -0
  95. data/metasm/cpu/python/decode.rb +136 -0
  96. data/metasm/cpu/python/main.rb +36 -0
  97. data/metasm/cpu/python/opcodes.rb +180 -0
  98. data/{lib/metasm → metasm/cpu}/sh4.rb +1 -1
  99. data/{lib/metasm → metasm/cpu}/sh4/decode.rb +48 -17
  100. data/{lib/metasm → metasm/cpu}/sh4/main.rb +13 -4
  101. data/{lib/metasm → metasm/cpu}/sh4/opcodes.rb +7 -8
  102. data/metasm/cpu/x86_64.rb +15 -0
  103. data/{lib/metasm → metasm/cpu}/x86_64/compile_c.rb +28 -17
  104. data/{lib/metasm → metasm/cpu}/x86_64/debug.rb +4 -4
  105. data/{lib/metasm → metasm/cpu}/x86_64/decode.rb +57 -15
  106. data/{lib/metasm → metasm/cpu}/x86_64/encode.rb +55 -26
  107. data/{lib/metasm → metasm/cpu}/x86_64/main.rb +14 -6
  108. data/metasm/cpu/x86_64/opcodes.rb +136 -0
  109. data/{lib/metasm → metasm/cpu}/x86_64/parse.rb +10 -2
  110. data/metasm/cpu/x86_64/render.rb +35 -0
  111. data/metasm/cpu/z80.rb +9 -0
  112. data/metasm/cpu/z80/decode.rb +313 -0
  113. data/metasm/cpu/z80/main.rb +67 -0
  114. data/metasm/cpu/z80/opcodes.rb +224 -0
  115. data/metasm/cpu/z80/render.rb +59 -0
  116. data/{lib/metasm/os/main.rb → metasm/debug.rb} +160 -401
  117. data/{lib/metasm → metasm}/decode.rb +35 -4
  118. data/{lib/metasm → metasm}/decompile.rb +15 -16
  119. data/{lib/metasm → metasm}/disassemble.rb +201 -45
  120. data/{lib/metasm → metasm}/disassemble_api.rb +651 -87
  121. data/{lib/metasm → metasm}/dynldr.rb +220 -133
  122. data/{lib/metasm → metasm}/encode.rb +10 -1
  123. data/{lib/metasm → metasm}/exe_format/a_out.rb +9 -6
  124. data/{lib/metasm → metasm}/exe_format/autoexe.rb +1 -0
  125. data/{lib/metasm → metasm}/exe_format/bflt.rb +57 -27
  126. data/{lib/metasm → metasm}/exe_format/coff.rb +11 -3
  127. data/{lib/metasm → metasm}/exe_format/coff_decode.rb +53 -20
  128. data/{lib/metasm → metasm}/exe_format/coff_encode.rb +11 -13
  129. data/{lib/metasm → metasm}/exe_format/dex.rb +13 -5
  130. data/{lib/metasm → metasm}/exe_format/dol.rb +1 -0
  131. data/{lib/metasm → metasm}/exe_format/elf.rb +93 -57
  132. data/{lib/metasm → metasm}/exe_format/elf_decode.rb +143 -34
  133. data/{lib/metasm → metasm}/exe_format/elf_encode.rb +122 -31
  134. data/metasm/exe_format/gb.rb +65 -0
  135. data/metasm/exe_format/javaclass.rb +424 -0
  136. data/{lib/metasm → metasm}/exe_format/macho.rb +204 -16
  137. data/{lib/metasm → metasm}/exe_format/main.rb +26 -3
  138. data/{lib/metasm → metasm}/exe_format/mz.rb +1 -0
  139. data/{lib/metasm → metasm}/exe_format/nds.rb +7 -4
  140. data/{lib/metasm → metasm}/exe_format/pe.rb +71 -8
  141. data/metasm/exe_format/pyc.rb +167 -0
  142. data/{lib/metasm → metasm}/exe_format/serialstruct.rb +67 -14
  143. data/{lib/metasm → metasm}/exe_format/shellcode.rb +7 -3
  144. data/metasm/exe_format/shellcode_rwx.rb +114 -0
  145. data/metasm/exe_format/swf.rb +205 -0
  146. data/{lib/metasm → metasm}/exe_format/xcoff.rb +7 -7
  147. data/metasm/exe_format/zip.rb +335 -0
  148. data/metasm/gui.rb +13 -0
  149. data/{lib/metasm → metasm}/gui/cstruct.rb +35 -41
  150. data/{lib/metasm → metasm}/gui/dasm_coverage.rb +11 -11
  151. data/{lib/metasm → metasm}/gui/dasm_decomp.rb +7 -20
  152. data/{lib/metasm → metasm}/gui/dasm_funcgraph.rb +0 -0
  153. data/metasm/gui/dasm_graph.rb +1695 -0
  154. data/{lib/metasm → metasm}/gui/dasm_hex.rb +12 -8
  155. data/{lib/metasm → metasm}/gui/dasm_listing.rb +43 -28
  156. data/{lib/metasm → metasm}/gui/dasm_main.rb +310 -53
  157. data/{lib/metasm → metasm}/gui/dasm_opcodes.rb +5 -19
  158. data/{lib/metasm → metasm}/gui/debug.rb +93 -27
  159. data/{lib/metasm → metasm}/gui/gtk.rb +162 -40
  160. data/{lib/metasm → metasm}/gui/qt.rb +12 -2
  161. data/{lib/metasm → metasm}/gui/win32.rb +179 -42
  162. data/{lib/metasm → metasm}/gui/x11.rb +59 -59
  163. data/{lib/metasm → metasm}/main.rb +389 -264
  164. data/{lib/metasm/os/remote.rb → metasm/os/gdbremote.rb} +146 -54
  165. data/{lib/metasm → metasm}/os/gnu_exports.rb +1 -1
  166. data/{lib/metasm → metasm}/os/linux.rb +628 -151
  167. data/metasm/os/main.rb +330 -0
  168. data/{lib/metasm → metasm}/os/windows.rb +132 -42
  169. data/{lib/metasm → metasm}/os/windows_exports.rb +141 -0
  170. data/{lib/metasm → metasm}/parse.rb +26 -24
  171. data/{lib/metasm → metasm}/parse_c.rb +221 -116
  172. data/{lib/metasm → metasm}/preprocessor.rb +55 -40
  173. data/{lib/metasm → metasm}/render.rb +14 -38
  174. data/misc/hexdump.rb +2 -1
  175. data/misc/lint.rb +58 -0
  176. data/misc/txt2html.rb +9 -7
  177. data/samples/bindiff.rb +3 -4
  178. data/samples/dasm-plugins/bindiff.rb +15 -0
  179. data/samples/dasm-plugins/bookmark.rb +133 -0
  180. data/samples/dasm-plugins/c_constants.rb +57 -0
  181. data/samples/dasm-plugins/colortheme_solarized.rb +125 -0
  182. data/samples/dasm-plugins/cppobj_funcall.rb +60 -0
  183. data/samples/dasm-plugins/dasm_all.rb +70 -0
  184. data/samples/dasm-plugins/demangle_cpp.rb +31 -0
  185. data/samples/dasm-plugins/deobfuscate.rb +251 -0
  186. data/samples/dasm-plugins/dump_text.rb +35 -0
  187. data/samples/dasm-plugins/export_graph_svg.rb +86 -0
  188. data/samples/dasm-plugins/findgadget.rb +75 -0
  189. data/samples/dasm-plugins/hl_opcode.rb +32 -0
  190. data/samples/dasm-plugins/hotfix_gtk_dbg.rb +19 -0
  191. data/samples/dasm-plugins/imm2off.rb +34 -0
  192. data/samples/dasm-plugins/match_libsigs.rb +93 -0
  193. data/samples/dasm-plugins/patch_file.rb +95 -0
  194. data/samples/dasm-plugins/scanfuncstart.rb +36 -0
  195. data/samples/dasm-plugins/scanxrefs.rb +26 -0
  196. data/samples/dasm-plugins/selfmodify.rb +197 -0
  197. data/samples/dasm-plugins/stringsxrefs.rb +28 -0
  198. data/samples/dasmnavig.rb +1 -1
  199. data/samples/dbg-apihook.rb +24 -9
  200. data/samples/dbg-plugins/heapscan.rb +283 -0
  201. data/samples/dbg-plugins/heapscan/compiled_heapscan_lin.c +155 -0
  202. data/samples/dbg-plugins/heapscan/compiled_heapscan_win.c +128 -0
  203. data/samples/dbg-plugins/heapscan/graphheap.rb +616 -0
  204. data/samples/dbg-plugins/heapscan/heapscan.rb +709 -0
  205. data/samples/dbg-plugins/heapscan/winheap.h +174 -0
  206. data/samples/dbg-plugins/heapscan/winheap7.h +307 -0
  207. data/samples/dbg-plugins/trace_func.rb +214 -0
  208. data/samples/disassemble-gui.rb +35 -5
  209. data/samples/disassemble.rb +31 -6
  210. data/samples/dump_upx.rb +24 -12
  211. data/samples/dynamic_ruby.rb +12 -3
  212. data/samples/exeencode.rb +6 -5
  213. data/samples/factorize-headers-peimports.rb +1 -1
  214. data/samples/lindebug.rb +175 -381
  215. data/samples/metasm-shell.rb +1 -2
  216. data/samples/peldr.rb +2 -2
  217. data/tests/all.rb +1 -1
  218. data/tests/arc.rb +26 -0
  219. data/tests/dynldr.rb +22 -4
  220. data/tests/expression.rb +55 -0
  221. data/tests/graph_layout.rb +285 -0
  222. data/tests/ia32.rb +79 -26
  223. data/tests/mips.rb +9 -2
  224. data/tests/x86_64.rb +66 -18
  225. metadata +330 -218
  226. data/lib/metasm/arm/opcodes.rb +0 -177
  227. data/lib/metasm/gui.rb +0 -23
  228. data/lib/metasm/gui/dasm_graph.rb +0 -1354
  229. data/lib/metasm/ia32.rb +0 -14
  230. data/lib/metasm/ia32/opcodes.rb +0 -873
  231. data/lib/metasm/ppc/parse.rb +0 -52
  232. data/lib/metasm/x86_64.rb +0 -12
  233. data/lib/metasm/x86_64/opcodes.rb +0 -118
  234. data/samples/gdbclient.rb +0 -583
  235. 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