metasm 1.0.0 → 1.0.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (276) hide show
  1. checksums.yaml +7 -0
  2. checksums.yaml.gz.sig +0 -0
  3. data.tar.gz.sig +3 -0
  4. data/.gitignore +3 -0
  5. data/.hgtags +3 -0
  6. data/Gemfile +3 -0
  7. data/INSTALL +61 -0
  8. data/LICENCE +458 -0
  9. data/README +29 -21
  10. data/Rakefile +10 -0
  11. data/TODO +10 -12
  12. data/doc/code_organisation.txt +3 -1
  13. data/doc/core/DynLdr.txt +247 -0
  14. data/doc/core/ExeFormat.txt +43 -0
  15. data/doc/core/Expression.txt +220 -0
  16. data/doc/core/GNUExports.txt +27 -0
  17. data/doc/core/Ia32.txt +236 -0
  18. data/doc/core/SerialStruct.txt +108 -0
  19. data/doc/core/VirtualString.txt +145 -0
  20. data/doc/core/WindowsExports.txt +61 -0
  21. data/doc/core/index.txt +1 -0
  22. data/doc/style.css +6 -3
  23. data/doc/usage/debugger.txt +327 -0
  24. data/doc/usage/index.txt +1 -0
  25. data/doc/use_cases.txt +2 -2
  26. data/metasm.gemspec +23 -0
  27. data/{lib/metasm.rb → metasm.rb} +15 -3
  28. data/{lib/metasm → metasm}/compile_c.rb +15 -9
  29. data/metasm/cpu/arc.rb +8 -0
  30. data/metasm/cpu/arc/decode.rb +404 -0
  31. data/metasm/cpu/arc/main.rb +191 -0
  32. data/metasm/cpu/arc/opcodes.rb +588 -0
  33. data/metasm/cpu/arm.rb +14 -0
  34. data/{lib/metasm → metasm/cpu}/arm/debug.rb +2 -2
  35. data/{lib/metasm → metasm/cpu}/arm/decode.rb +15 -18
  36. data/{lib/metasm → metasm/cpu}/arm/encode.rb +23 -8
  37. data/{lib/metasm → metasm/cpu}/arm/main.rb +3 -6
  38. data/metasm/cpu/arm/opcodes.rb +324 -0
  39. data/{lib/metasm → metasm/cpu}/arm/parse.rb +25 -13
  40. data/{lib/metasm → metasm/cpu}/arm/render.rb +2 -2
  41. data/metasm/cpu/arm64.rb +15 -0
  42. data/metasm/cpu/arm64/debug.rb +38 -0
  43. data/metasm/cpu/arm64/decode.rb +285 -0
  44. data/metasm/cpu/arm64/encode.rb +41 -0
  45. data/metasm/cpu/arm64/main.rb +105 -0
  46. data/metasm/cpu/arm64/opcodes.rb +232 -0
  47. data/metasm/cpu/arm64/parse.rb +20 -0
  48. data/metasm/cpu/arm64/render.rb +95 -0
  49. data/{lib/metasm/mips/compile_c.rb → metasm/cpu/bpf.rb} +4 -2
  50. data/metasm/cpu/bpf/decode.rb +110 -0
  51. data/metasm/cpu/bpf/main.rb +60 -0
  52. data/metasm/cpu/bpf/opcodes.rb +81 -0
  53. data/metasm/cpu/bpf/render.rb +30 -0
  54. data/{lib/metasm/ppc.rb → metasm/cpu/cy16.rb} +2 -4
  55. data/metasm/cpu/cy16/decode.rb +247 -0
  56. data/metasm/cpu/cy16/main.rb +63 -0
  57. data/metasm/cpu/cy16/opcodes.rb +78 -0
  58. data/metasm/cpu/cy16/render.rb +30 -0
  59. data/metasm/cpu/dalvik.rb +11 -0
  60. data/{lib/metasm → metasm/cpu}/dalvik/decode.rb +34 -34
  61. data/{lib/metasm → metasm/cpu}/dalvik/main.rb +71 -4
  62. data/{lib/metasm → metasm/cpu}/dalvik/opcodes.rb +21 -12
  63. data/{lib/metasm/mips.rb → metasm/cpu/ebpf.rb} +3 -4
  64. data/metasm/cpu/ebpf/debug.rb +61 -0
  65. data/metasm/cpu/ebpf/decode.rb +142 -0
  66. data/metasm/cpu/ebpf/main.rb +58 -0
  67. data/metasm/cpu/ebpf/opcodes.rb +97 -0
  68. data/metasm/cpu/ebpf/render.rb +36 -0
  69. data/metasm/cpu/ia32.rb +17 -0
  70. data/{lib/metasm → metasm/cpu}/ia32/compile_c.rb +23 -9
  71. data/{lib/metasm → metasm/cpu}/ia32/debug.rb +44 -6
  72. data/{lib/metasm → metasm/cpu}/ia32/decode.rb +342 -128
  73. data/{lib/metasm → metasm/cpu}/ia32/decompile.rb +75 -53
  74. data/{lib/metasm → metasm/cpu}/ia32/encode.rb +19 -13
  75. data/{lib/metasm → metasm/cpu}/ia32/main.rb +66 -8
  76. data/metasm/cpu/ia32/opcodes.rb +1424 -0
  77. data/{lib/metasm → metasm/cpu}/ia32/parse.rb +55 -17
  78. data/{lib/metasm → metasm/cpu}/ia32/render.rb +32 -5
  79. data/metasm/cpu/mcs51.rb +8 -0
  80. data/metasm/cpu/mcs51/decode.rb +99 -0
  81. data/metasm/cpu/mcs51/main.rb +87 -0
  82. data/metasm/cpu/mcs51/opcodes.rb +120 -0
  83. data/metasm/cpu/mips.rb +14 -0
  84. data/metasm/cpu/mips/debug.rb +42 -0
  85. data/{lib/metasm → metasm/cpu}/mips/decode.rb +59 -38
  86. data/{lib/metasm → metasm/cpu}/mips/encode.rb +4 -3
  87. data/{lib/metasm → metasm/cpu}/mips/main.rb +13 -6
  88. data/{lib/metasm → metasm/cpu}/mips/opcodes.rb +87 -18
  89. data/{lib/metasm → metasm/cpu}/mips/parse.rb +1 -1
  90. data/{lib/metasm → metasm/cpu}/mips/render.rb +1 -1
  91. data/{lib/metasm/dalvik.rb → metasm/cpu/msp430.rb} +1 -1
  92. data/metasm/cpu/msp430/decode.rb +243 -0
  93. data/metasm/cpu/msp430/main.rb +62 -0
  94. data/metasm/cpu/msp430/opcodes.rb +101 -0
  95. data/metasm/cpu/openrisc.rb +11 -0
  96. data/metasm/cpu/openrisc/debug.rb +106 -0
  97. data/metasm/cpu/openrisc/decode.rb +182 -0
  98. data/metasm/cpu/openrisc/decompile.rb +350 -0
  99. data/metasm/cpu/openrisc/main.rb +70 -0
  100. data/metasm/cpu/openrisc/opcodes.rb +109 -0
  101. data/metasm/cpu/openrisc/render.rb +37 -0
  102. data/{lib/metasm → metasm/cpu}/pic16c/decode.rb +6 -7
  103. data/{lib/metasm → metasm/cpu}/pic16c/main.rb +0 -0
  104. data/{lib/metasm → metasm/cpu}/pic16c/opcodes.rb +1 -1
  105. data/metasm/cpu/ppc.rb +11 -0
  106. data/{lib/metasm → metasm/cpu}/ppc/decode.rb +18 -37
  107. data/{lib/metasm → metasm/cpu}/ppc/decompile.rb +3 -3
  108. data/{lib/metasm → metasm/cpu}/ppc/encode.rb +2 -2
  109. data/{lib/metasm → metasm/cpu}/ppc/main.rb +23 -18
  110. data/{lib/metasm → metasm/cpu}/ppc/opcodes.rb +11 -6
  111. data/metasm/cpu/ppc/parse.rb +55 -0
  112. data/metasm/cpu/python.rb +8 -0
  113. data/metasm/cpu/python/decode.rb +116 -0
  114. data/metasm/cpu/python/main.rb +36 -0
  115. data/metasm/cpu/python/opcodes.rb +180 -0
  116. data/{lib/metasm → metasm/cpu}/sh4.rb +1 -1
  117. data/{lib/metasm → metasm/cpu}/sh4/decode.rb +50 -23
  118. data/{lib/metasm → metasm/cpu}/sh4/main.rb +38 -27
  119. data/{lib/metasm → metasm/cpu}/sh4/opcodes.rb +7 -8
  120. data/metasm/cpu/st20.rb +9 -0
  121. data/metasm/cpu/st20/decode.rb +173 -0
  122. data/metasm/cpu/st20/decompile.rb +283 -0
  123. data/metasm/cpu/st20/main.rb +37 -0
  124. data/metasm/cpu/st20/opcodes.rb +140 -0
  125. data/{lib/metasm/arm.rb → metasm/cpu/webasm.rb} +4 -5
  126. data/metasm/cpu/webasm/debug.rb +31 -0
  127. data/metasm/cpu/webasm/decode.rb +321 -0
  128. data/metasm/cpu/webasm/decompile.rb +386 -0
  129. data/metasm/cpu/webasm/encode.rb +104 -0
  130. data/metasm/cpu/webasm/main.rb +81 -0
  131. data/metasm/cpu/webasm/opcodes.rb +214 -0
  132. data/metasm/cpu/x86_64.rb +15 -0
  133. data/{lib/metasm → metasm/cpu}/x86_64/compile_c.rb +40 -25
  134. data/{lib/metasm → metasm/cpu}/x86_64/debug.rb +4 -4
  135. data/{lib/metasm → metasm/cpu}/x86_64/decode.rb +58 -15
  136. data/{lib/metasm → metasm/cpu}/x86_64/encode.rb +59 -28
  137. data/{lib/metasm → metasm/cpu}/x86_64/main.rb +18 -6
  138. data/metasm/cpu/x86_64/opcodes.rb +138 -0
  139. data/{lib/metasm → metasm/cpu}/x86_64/parse.rb +12 -4
  140. data/metasm/cpu/x86_64/render.rb +35 -0
  141. data/metasm/cpu/z80.rb +9 -0
  142. data/metasm/cpu/z80/decode.rb +286 -0
  143. data/metasm/cpu/z80/main.rb +67 -0
  144. data/metasm/cpu/z80/opcodes.rb +224 -0
  145. data/metasm/cpu/z80/render.rb +48 -0
  146. data/{lib/metasm/os/main.rb → metasm/debug.rb} +201 -407
  147. data/{lib/metasm → metasm}/decode.rb +104 -24
  148. data/{lib/metasm → metasm}/decompile.rb +804 -478
  149. data/{lib/metasm → metasm}/disassemble.rb +385 -170
  150. data/{lib/metasm → metasm}/disassemble_api.rb +684 -105
  151. data/{lib/metasm → metasm}/dynldr.rb +231 -138
  152. data/{lib/metasm → metasm}/encode.rb +20 -5
  153. data/{lib/metasm → metasm}/exe_format/a_out.rb +9 -6
  154. data/{lib/metasm → metasm}/exe_format/autoexe.rb +3 -0
  155. data/{lib/metasm → metasm}/exe_format/bflt.rb +57 -27
  156. data/{lib/metasm → metasm}/exe_format/coff.rb +35 -7
  157. data/{lib/metasm → metasm}/exe_format/coff_decode.rb +70 -23
  158. data/{lib/metasm → metasm}/exe_format/coff_encode.rb +24 -22
  159. data/{lib/metasm → metasm}/exe_format/dex.rb +26 -8
  160. data/{lib/metasm → metasm}/exe_format/dol.rb +1 -0
  161. data/{lib/metasm → metasm}/exe_format/elf.rb +108 -58
  162. data/{lib/metasm → metasm}/exe_format/elf_decode.rb +202 -36
  163. data/{lib/metasm → metasm}/exe_format/elf_encode.rb +126 -32
  164. data/metasm/exe_format/gb.rb +65 -0
  165. data/metasm/exe_format/javaclass.rb +424 -0
  166. data/{lib/metasm → metasm}/exe_format/macho.rb +218 -16
  167. data/{lib/metasm → metasm}/exe_format/main.rb +28 -3
  168. data/{lib/metasm → metasm}/exe_format/mz.rb +2 -0
  169. data/{lib/metasm → metasm}/exe_format/nds.rb +7 -4
  170. data/{lib/metasm → metasm}/exe_format/pe.rb +96 -11
  171. data/metasm/exe_format/pyc.rb +167 -0
  172. data/{lib/metasm → metasm}/exe_format/serialstruct.rb +67 -14
  173. data/{lib/metasm → metasm}/exe_format/shellcode.rb +7 -3
  174. data/metasm/exe_format/shellcode_rwx.rb +114 -0
  175. data/metasm/exe_format/swf.rb +205 -0
  176. data/metasm/exe_format/wasm.rb +402 -0
  177. data/{lib/metasm → metasm}/exe_format/xcoff.rb +7 -7
  178. data/metasm/exe_format/zip.rb +335 -0
  179. data/metasm/gui.rb +13 -0
  180. data/{lib/metasm → metasm}/gui/cstruct.rb +35 -41
  181. data/{lib/metasm → metasm}/gui/dasm_coverage.rb +11 -11
  182. data/{lib/metasm → metasm}/gui/dasm_decomp.rb +177 -114
  183. data/{lib/metasm → metasm}/gui/dasm_funcgraph.rb +0 -0
  184. data/metasm/gui/dasm_graph.rb +1754 -0
  185. data/{lib/metasm → metasm}/gui/dasm_hex.rb +16 -12
  186. data/{lib/metasm → metasm}/gui/dasm_listing.rb +43 -28
  187. data/{lib/metasm → metasm}/gui/dasm_main.rb +360 -77
  188. data/{lib/metasm → metasm}/gui/dasm_opcodes.rb +5 -19
  189. data/{lib/metasm → metasm}/gui/debug.rb +109 -34
  190. data/{lib/metasm → metasm}/gui/gtk.rb +174 -44
  191. data/{lib/metasm → metasm}/gui/qt.rb +14 -4
  192. data/{lib/metasm → metasm}/gui/win32.rb +180 -43
  193. data/{lib/metasm → metasm}/gui/x11.rb +59 -59
  194. data/{lib/metasm → metasm}/main.rb +421 -286
  195. data/metasm/os/emulator.rb +175 -0
  196. data/{lib/metasm/os/remote.rb → metasm/os/gdbremote.rb} +146 -54
  197. data/{lib/metasm → metasm}/os/gnu_exports.rb +1 -1
  198. data/{lib/metasm → metasm}/os/linux.rb +628 -151
  199. data/metasm/os/main.rb +335 -0
  200. data/{lib/metasm → metasm}/os/windows.rb +151 -58
  201. data/{lib/metasm → metasm}/os/windows_exports.rb +141 -0
  202. data/{lib/metasm → metasm}/parse.rb +49 -36
  203. data/{lib/metasm → metasm}/parse_c.rb +405 -246
  204. data/{lib/metasm → metasm}/preprocessor.rb +71 -41
  205. data/{lib/metasm → metasm}/render.rb +14 -38
  206. data/misc/hexdump.rb +4 -3
  207. data/misc/lint.rb +58 -0
  208. data/misc/objdiff.rb +4 -1
  209. data/misc/objscan.rb +1 -1
  210. data/misc/openrisc-parser.rb +79 -0
  211. data/misc/txt2html.rb +9 -7
  212. data/samples/bindiff.rb +3 -4
  213. data/samples/dasm-plugins/bindiff.rb +15 -0
  214. data/samples/dasm-plugins/bookmark.rb +133 -0
  215. data/samples/dasm-plugins/c_constants.rb +57 -0
  216. data/samples/dasm-plugins/colortheme_solarized.rb +125 -0
  217. data/samples/dasm-plugins/cppobj_funcall.rb +60 -0
  218. data/samples/dasm-plugins/dasm_all.rb +70 -0
  219. data/samples/dasm-plugins/demangle_cpp.rb +31 -0
  220. data/samples/dasm-plugins/deobfuscate.rb +251 -0
  221. data/samples/dasm-plugins/dump_text.rb +35 -0
  222. data/samples/dasm-plugins/export_graph_svg.rb +86 -0
  223. data/samples/dasm-plugins/findgadget.rb +75 -0
  224. data/samples/dasm-plugins/hl_opcode.rb +32 -0
  225. data/samples/dasm-plugins/hotfix_gtk_dbg.rb +19 -0
  226. data/samples/dasm-plugins/imm2off.rb +34 -0
  227. data/samples/dasm-plugins/match_libsigs.rb +93 -0
  228. data/samples/dasm-plugins/patch_file.rb +95 -0
  229. data/samples/dasm-plugins/scanfuncstart.rb +36 -0
  230. data/samples/dasm-plugins/scanxrefs.rb +29 -0
  231. data/samples/dasm-plugins/selfmodify.rb +197 -0
  232. data/samples/dasm-plugins/stringsxrefs.rb +28 -0
  233. data/samples/dasmnavig.rb +1 -1
  234. data/samples/dbg-apihook.rb +24 -9
  235. data/samples/dbg-plugins/heapscan.rb +283 -0
  236. data/samples/dbg-plugins/heapscan/compiled_heapscan_lin.c +155 -0
  237. data/samples/dbg-plugins/heapscan/compiled_heapscan_win.c +128 -0
  238. data/samples/dbg-plugins/heapscan/graphheap.rb +616 -0
  239. data/samples/dbg-plugins/heapscan/heapscan.rb +709 -0
  240. data/samples/dbg-plugins/heapscan/winheap.h +174 -0
  241. data/samples/dbg-plugins/heapscan/winheap7.h +307 -0
  242. data/samples/dbg-plugins/trace_func.rb +214 -0
  243. data/samples/disassemble-gui.rb +48 -7
  244. data/samples/disassemble.rb +31 -6
  245. data/samples/dump_upx.rb +24 -12
  246. data/samples/dynamic_ruby.rb +35 -27
  247. data/samples/elfencode.rb +15 -0
  248. data/samples/emubios.rb +251 -0
  249. data/samples/emudbg.rb +127 -0
  250. data/samples/exeencode.rb +6 -5
  251. data/samples/factorize-headers-peimports.rb +1 -1
  252. data/samples/lindebug.rb +186 -391
  253. data/samples/metasm-shell.rb +68 -57
  254. data/samples/peldr.rb +2 -2
  255. data/tests/all.rb +1 -1
  256. data/tests/arc.rb +26 -0
  257. data/tests/dynldr.rb +22 -4
  258. data/tests/expression.rb +57 -0
  259. data/tests/graph_layout.rb +285 -0
  260. data/tests/ia32.rb +80 -26
  261. data/tests/mcs51.rb +27 -0
  262. data/tests/mips.rb +10 -3
  263. data/tests/preprocessor.rb +18 -0
  264. data/tests/x86_64.rb +66 -18
  265. metadata +465 -219
  266. metadata.gz.sig +2 -0
  267. data/lib/metasm/arm/opcodes.rb +0 -177
  268. data/lib/metasm/gui.rb +0 -23
  269. data/lib/metasm/gui/dasm_graph.rb +0 -1354
  270. data/lib/metasm/ia32.rb +0 -14
  271. data/lib/metasm/ia32/opcodes.rb +0 -872
  272. data/lib/metasm/ppc/parse.rb +0 -52
  273. data/lib/metasm/x86_64.rb +0 -12
  274. data/lib/metasm/x86_64/opcodes.rb +0 -118
  275. data/samples/gdbclient.rb +0 -583
  276. data/samples/rubstop.rb +0 -399
@@ -0,0 +1 @@
1
+ See <use_cases.txt>
data/doc/use_cases.txt CHANGED
@@ -8,11 +8,11 @@ There are quite a lot of possible usages that can be derived from the
8
8
  The major would be related to:
9
9
 
10
10
  * the scriptable <usage/debugger.txt>
11
- * the <usage/disassembler.txt> (with the optionnal <usage/disassembler_gui.txt>)
11
+ * the <usage/disassembler.txt> (with the optional <usage/disassembler_gui.txt>)
12
12
  * the <usage/assembler.txt>
13
13
  * the <usage/C_parser.txt>
14
14
  * the <usage/C_compiler.txt>
15
15
  * the <usage/exe_manipulation.txt> facilities
16
16
 
17
- and various interaction between those.
17
+ and various interactions among them.
18
18
 
data/metasm.gemspec ADDED
@@ -0,0 +1,23 @@
1
+ lib = File.expand_path('.', File.dirname(__FILE__))
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require 'metasm'
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = 'metasm'
7
+ s.version = '1.0.5'
8
+ s.summary =
9
+ "Metasm is a cross-architecture assembler, disassembler, linker, and debugger."
10
+ s.description = ""
11
+ s.authors = ["Yoann Guillot"]
12
+ s.email = ['john at ofjj.net']
13
+ s.files = `git ls-files -z`.split("\x0")
14
+ s.test_files = s.files.grep(%r{^tests/})
15
+ s.require_paths = ["."]
16
+ s.homepage = 'http://metasm.cr0.org'
17
+ s.license = 'LGPL-2.1'
18
+
19
+ s.add_development_dependency "bundler", "~> 1.7"
20
+ s.add_development_dependency "rake"
21
+ s.add_development_dependency "test-unit"
22
+ end
23
+
@@ -15,6 +15,8 @@ module Metasm
15
15
  Const_autorequire_equiv = {
16
16
  'X86' => 'Ia32', 'PPC' => 'PowerPC',
17
17
  'X64' => 'X86_64', 'AMD64' => 'X86_64',
18
+ 'MIPS64' => 'MIPS', 'AArch64' => 'ARM64',
19
+ 'TransPuter' => 'ST20',
18
20
  'UniversalBinary' => 'MachO', 'COFFArchive' => 'COFF',
19
21
  'DEY' => 'DEX',
20
22
  'PTrace' => 'LinOS', 'FatELF' => 'ELF',
@@ -32,8 +34,11 @@ module Metasm
32
34
 
33
35
  # files to require to get the definition of those constants
34
36
  Const_autorequire = {
35
- 'Ia32' => 'ia32', 'MIPS' => 'mips', 'PowerPC' => 'ppc', 'ARM' => 'arm',
36
- 'X86_64' => 'x86_64', 'Sh4' => 'sh4', 'Dalvik' => 'dalvik',
37
+ 'Ia32' => 'cpu/ia32', 'MIPS' => 'cpu/mips', 'PowerPC' => 'cpu/ppc', 'ARM' => 'cpu/arm',
38
+ 'X86_64' => 'cpu/x86_64', 'Sh4' => 'cpu/sh4', 'Dalvik' => 'cpu/dalvik', 'ARC' => 'cpu/arc',
39
+ 'Python' => 'cpu/python', 'Z80' => 'cpu/z80', 'CY16' => 'cpu/cy16', 'BPF' => 'cpu/bpf', 'EBPF' => 'cpu/ebpf',
40
+ 'MSP430' => 'cpu/msp430', 'ARM64' => 'cpu/arm64', 'ST20' => 'cpu/st20', 'MCS51' => 'cpu/mcs51',
41
+ 'OpenRisc' => 'cpu/openrisc', 'WebAsm' => 'cpu/webasm',
37
42
  'C' => 'compile_c',
38
43
  'MZ' => 'exe_format/mz', 'PE' => 'exe_format/pe',
39
44
  'ELF' => 'exe_format/elf', 'COFF' => 'exe_format/coff',
@@ -41,12 +46,19 @@ module Metasm
41
46
  'AOut' => 'exe_format/a_out', 'MachO' => 'exe_format/macho',
42
47
  'DEX' => 'exe_format/dex',
43
48
  'NDS' => 'exe_format/nds', 'XCoff' => 'exe_format/xcoff',
49
+ 'GameBoyRom' => 'exe_format/gb',
44
50
  'Bflt' => 'exe_format/bflt', 'Dol' => 'exe_format/dol',
51
+ 'PYC' => 'exe_format/pyc', 'JavaClass' => 'exe_format/javaclass',
52
+ 'SWF' => 'exe_format/swf', 'ZIP' => 'exe_format/zip',
53
+ 'WasmFile' => 'exe_format/wasm',
54
+ 'Shellcode_RWX' => 'exe_format/shellcode_rwx',
45
55
  'Gui' => 'gui',
46
56
  'WindowsExports' => 'os/windows_exports',
47
57
  'GNUExports' => 'os/gnu_exports',
58
+ 'Debugger' => 'debug',
48
59
  'LinOS' => 'os/linux', 'WinOS' => 'os/windows',
49
- 'GdbClient' => 'os/remote',
60
+ 'GdbClient' => 'os/gdbremote',
61
+ 'EmuDebugger' => 'os/emulator',
50
62
  'Disassembler' => 'disassemble',
51
63
  'Decompiler' => 'decompile',
52
64
  'DynLdr' => 'dynldr',
@@ -11,14 +11,14 @@ module Metasm
11
11
  module C
12
12
  class Parser
13
13
  def precompile
14
- @toplevel.precompile(Compiler.new(self))
14
+ @toplevel.precompile(Compiler.new(self, @program))
15
15
  self
16
16
  end
17
17
  end
18
18
 
19
19
  # each CPU defines a subclass of this one
20
20
  class Compiler
21
- # an ExeFormat (mostly used for unique label creation)
21
+ # an ExeFormat (mostly used for unique label creation, and cpu.check_reserved_name)
22
22
  attr_accessor :exeformat
23
23
  # the C Parser (destroyed by compilation)
24
24
  attr_accessor :parser
@@ -36,7 +36,8 @@ module C
36
36
  end
37
37
 
38
38
  # creates a new CCompiler from an ExeFormat and a C Parser
39
- def initialize(parser, exeformat=ExeFormat.new, source=[])
39
+ def initialize(parser, exeformat=nil, source=[])
40
+ exeformat ||= ExeFormat.new
40
41
  @parser, @exeformat, @source = parser, exeformat, source
41
42
  @auto_label_list = {}
42
43
  @label_oldname = {}
@@ -280,7 +281,7 @@ module C
280
281
  when :ptr; " d#{%w[x b w x d x x x q][@parser.typesize[type.name]]} "
281
282
  when :float; ' db ' + [value].pack(@parser.endianness == :little ? 'e' : 'g').unpack('C*').join(', ') + ' // '
282
283
  when :double; ' db ' + [value].pack(@parser.endianness == :little ? 'E' : 'G').unpack('C*').join(', ') + ' // '
283
- when :longdouble; ' db ' + [value].pack(@parser.endianness == :little ? 'E' : 'G').unpack('C*').join(', ') + ' // ' # XXX same as :double
284
+ when :longdouble; ' db ' + [value].pack(@parser.endianness == :little ? 'E' : 'G').unpack('C*').join(', ') + ' // ' # XXX same as :double
284
285
  else raise "unknown idata type #{type.inspect} #{value.inspect}"
285
286
  end
286
287
 
@@ -428,6 +429,7 @@ module C
428
429
  # return non-nil if the variable name is unsuitable to appear as is in the asm listing
429
430
  # eg filter out asm instruction names
430
431
  def check_reserved_name(var)
432
+ return true if @exeformat.cpu and @exeformat.cpu.check_reserved_name(var.name)
431
433
  %w[db dw dd dq].include?(var.name)
432
434
  end
433
435
  end
@@ -555,8 +557,12 @@ module C
555
557
  # reuse same name as predeclarations
556
558
  @var.name = n
557
559
  else
558
- @var.name = compiler.new_label @var.name until @var.name != old
559
- compiler.label_oldname[@var.name] = old
560
+ newname = old
561
+ newname = compiler.new_label newname until newname != old
562
+ if not compiler.check_reserved_name(@var)
563
+ compiler.label_oldname[newname] = old
564
+ end
565
+ @var.name = newname
560
566
  end
561
567
  ref ||= scope.symbol[@var.name] || @var
562
568
  # append only one actual declaration for all predecls (the one with init, or the last uninit)
@@ -684,7 +690,7 @@ module C
684
690
  if not init.op and init.rexpr.kind_of? ::String
685
691
  v = Variable.new
686
692
  v.storage = :static
687
- v.name = 'char_' + init.rexpr.tr('^a-zA-Z', '')[0, 8]
693
+ v.name = 'char_' + init.rexpr.gsub(/[^a-zA-Z]/, '')[0, 8]
688
694
  v.type = Array.new(type.type)
689
695
  v.type.length = init.rexpr.length + 1
690
696
  v.type.type.qualifier = [:const]
@@ -858,7 +864,7 @@ module C
858
864
  blk.statements.each_with_index { |s, i|
859
865
  case s
860
866
  when Case
861
- label = compiler.new_label('case')
867
+ label = compiler.new_label("case_#{s.expr}")
862
868
  if s.expr == 'default'
863
869
  default = label
864
870
  elsif s.exprup
@@ -1057,7 +1063,7 @@ module C
1057
1063
  @lexpr = nil
1058
1064
  precompile_inner(compiler, scope)
1059
1065
  when :'?:'
1060
- # cannot precompile in place, a conditionnal expression may have a coma: must turn into If
1066
+ # cannot precompile in place, a conditional expression may have a coma: must turn into If
1061
1067
  if @lexpr.kind_of? CExpression
1062
1068
  @lexpr = @lexpr.precompile_inner(compiler, scope)
1063
1069
  if not @lexpr.lexpr and not @lexpr.op and @lexpr.rexpr.kind_of? ::Numeric
data/metasm/cpu/arc.rb ADDED
@@ -0,0 +1,8 @@
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
+
7
+ require 'metasm/main'
8
+ require 'metasm/cpu/arc/decode'
@@ -0,0 +1,404 @@
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/arc/opcodes'
7
+ require 'metasm/decode'
8
+
9
+ module Metasm
10
+ class ARC
11
+ def major_opcode(val, sz = 16)
12
+ return val >> (sz == 16 ? 0xB : 0x1B)
13
+ end
14
+
15
+ def sub_opcode(val)
16
+ return ((val >> 16) & 0x3f)
17
+ end
18
+
19
+ def build_opcode_bin_mask(op, sz)
20
+ op.bin_mask = 0
21
+ op.args.each { |f| op.bin_mask |= @fields_mask[f] << @fields_shift[f]}
22
+ op.bin_mask = ((1 << sz)-1) ^ op.bin_mask
23
+ end
24
+
25
+ def build_bin_lookaside
26
+ bin_lookaside = {}
27
+ opcode_list.each{|mode,oplist|
28
+ lookaside = {}
29
+ # 2nd level to speed up lookaside for major 5
30
+ lookaside[5] = {}
31
+ oplist.each { |op|
32
+ next if not op.bin.kind_of? Integer
33
+ build_opcode_bin_mask(op, mode)
34
+ mj = major_opcode(op.bin, mode)
35
+ if mode == 32 and mj == 5
36
+ (lookaside[mj][sub_opcode(op.bin)] ||= []) << op
37
+ else
38
+ (lookaside[mj] ||= []) << op
39
+ end
40
+ }
41
+ bin_lookaside[mode] = lookaside
42
+ }
43
+ bin_lookaside
44
+ end
45
+
46
+ def instruction_size(edata)
47
+ val = major_opcode(edata.decode_imm(:u16, @endianness))
48
+ edata.ptr -= 2
49
+ (val >= 0xC) ? 16 : 32
50
+ end
51
+
52
+ def memref_size(di)
53
+ case di.opcode.name
54
+ when 'ldb_s', 'stb_s', 'extb_s', 'sexb_s'; 1
55
+ when 'ldw_s', 'stw_s', 'extw_s', 'sexw_s'; 2
56
+ else 4
57
+ end
58
+ end
59
+
60
+ def decode_bin(edata, sz)
61
+ case sz
62
+ when 16; edata.decode_imm(:u16, @endianness)
63
+ when 32
64
+ # wordswap
65
+ val = edata.decode_imm(:u32, :little)
66
+ ((val >> 16) & 0xffff) | ((val & 0xffff) << 16)
67
+ end
68
+ end
69
+
70
+ def decode_findopcode(edata)
71
+ di = DecodedInstruction.new(self)
72
+
73
+ @instrlength = instruction_size(edata)
74
+ val = decode_bin(edata, @instrlength)
75
+ edata.ptr -= @instrlength/8
76
+
77
+ maj = major_opcode(val, @instrlength)
78
+ lookaside = @bin_lookaside[@instrlength][maj]
79
+ lookaside = lookaside[sub_opcode(val)] if @instrlength == 32 and maj == 5
80
+
81
+ op = lookaside.select { |opcode|
82
+ if false and (val & opcode.bin_mask) == opcode.bin
83
+ puts "#{opcode.bin_mask.to_s(16)} - #{opcode.bin.to_s(16)} - #{(val & opcode.bin_mask).to_s(16)} - #{opcode.name} - #{opcode.args}"
84
+ end
85
+ (val & opcode.bin_mask) == opcode.bin
86
+ }
87
+
88
+ if op.size == 2 and op.first.name == 'mov' and op.last.name == 'nop'
89
+ op = op.last
90
+ elsif op == nil or op.size != 1
91
+ puts "[> I sense a disturbance in the force <]"
92
+ op.to_a.each { |opcode| puts "#{opcode.name} - #{opcode.args} - #{Expression[opcode.bin]} - #{Expression[opcode.bin_mask]}" }
93
+ puts "current value: #{Expression[val]}"
94
+ puts "current value: 0b#{val.to_s(2)}"
95
+ op = nil
96
+ else
97
+ op = op.first
98
+ end
99
+
100
+ di if di.opcode = op
101
+ end
102
+
103
+ Reduced_reg = [0, 1, 2, 3, 12, 13, 14, 15]
104
+ def reduced_reg_set(i)
105
+ Reduced_reg[i]
106
+ end
107
+
108
+ def decode_instr_op(edata, di)
109
+ before_ptr = edata.ptr
110
+ op = di.opcode
111
+ di.instruction.opname = op.name
112
+ val = decode_bin(edata, @instrlength)
113
+
114
+ field_val = lambda { |f|
115
+ r = (val >> @fields_shift[f]) & @fields_mask[f]
116
+ case f
117
+
118
+ # 16-bits instruction operands ------------------------------------------"
119
+ when :ca, :cb, :cb2, :cb3, :cc; r = reduced_reg_set(r)
120
+ when :ch
121
+ r = (((r & 7) << 3) | (r >> 5))
122
+ when :@cbu7, :@cbu6, :@cbu5
123
+ r = r & 0b11111
124
+ r = (f == :@cbu7) ? r << 2 : ( (f == :@cbu6) ? r << 1 : r)
125
+ when :cu5ee; r = r << 2
126
+ when :cdisps13
127
+ r = (Expression.make_signed(r,11) << 2) + ((di.address >> 2) << 2)
128
+ when :cdisps10
129
+ r = (Expression.make_signed(r, 9) << 1) + ((di.address >> 2) << 2)
130
+ when :cdisps8
131
+ r = (Expression.make_signed(r, 7) << 1) + ((di.address >> 2) << 2)
132
+ when :cdisps7
133
+ r = (Expression.make_signed(r, 6) << 1) + ((di.address >> 2) << 2)
134
+ when :cs9, :cs10, :cs11
135
+ r = Expression.make_signed(r, ((f== :cs11 ? 11 : (f == :cs10 ? 10 : 9) )))
136
+ r = (f == :cs11) ? r << 2 : ((f == :cs10) ? r << 1 : r)
137
+ when :@cspu7;
138
+ r = r << 2
139
+
140
+ # 32-bits instruction operands ------------------------------------------"
141
+ when :b
142
+ r = (r >> 12) | ((r & 0x7) << 3)
143
+ when :s8e
144
+ r = ((r & 0x1) << 7) | (r >> 2)
145
+ r = (Expression.make_signed(r, 8) << 1) + ((di.address >> 2) << 2)
146
+
147
+ when :u6e
148
+ r = (r << 1) + ((di.address >> 2) << 2)
149
+ when :s9
150
+ r = (Expression.make_signed(r, 7) << 1) + ((di.address >> 2) << 2)
151
+
152
+ when :s12
153
+ r = (r >> 6) | ((r & 0x3f) << 6)
154
+ r = Expression.make_signed(r, 12)
155
+
156
+ when :s12e
157
+ r = (r >> 6) | ((r & 0x3f) << 6)
158
+ r = (Expression.make_signed(r, 12) <<1 ) + ((di.address >> 2) << 2)
159
+
160
+ when :s21e
161
+ r = ((r & 0x3ff) << 10) | (r >> 11)
162
+ r = (Expression.make_signed(r, 20) << 1) + ((di.address >> 2) << 2)
163
+
164
+ when :s21ee # pc-relative
165
+ r = ((r & 0x3ff) << 9) | (r >> 12)
166
+ r = (Expression.make_signed(r, 19) << 2) + ((di.address >> 2) << 2)
167
+
168
+ when :s25e # pc-relative
169
+ r = ((r & 0xf) << 20) | (((r >> 6) & 0x3ff) << 10) | (r >> 17)
170
+ r = (Expression.make_signed(r, 24) << 1) + ((di.address >> 2) << 2)
171
+
172
+ when :s25ee # pc-relative
173
+ r = ((r & 0xf) << 19) | (((r >> 6) & 0x3ff) << 9) | (r >> 18)
174
+ r = (Expression.make_signed(r, 23) << 2) + ((di.address >> 2) << 2)
175
+
176
+ when :@bs9
177
+ r = r >> 3
178
+ s9 = ((r & 1) << 8) | ((r >> 1) & 0xff)
179
+ r = Expression.make_signed(s9, 9)
180
+
181
+ when :bext, :cext, :@cext
182
+ if ((r = field_val[(f == :bext) ? :b : :c]) == 0x3E)
183
+ tmp = edata.decode_imm(:u32, :little)
184
+ r = Expression[(tmp >> 16) | ((tmp & 0xffff) << 16)]
185
+ else
186
+ r = GPR.new(r)
187
+ end
188
+
189
+ else r
190
+ end
191
+ r
192
+ }
193
+
194
+ # decode properties fields
195
+ op.args.each { |a|
196
+ case a
197
+ when :flags15, :flags16
198
+ di.instruction.opname += '.f' if field_val[a] != 0
199
+ when :ccond
200
+ di.instruction.opname += ('.' + @cond_suffix[field_val[a]]) if field_val[a] != 0
201
+ when :delay5, :delay16
202
+ di.instruction.opname += '.d' if field_val[a] != 0
203
+ when :cache5, :cache11, :cache16
204
+ di.instruction.opname +='.di' if field_val[a] != 0
205
+ when :signext6, :signext16
206
+ di.instruction.opname += '.x' if field_val[a] != 0
207
+ when :wb3, :wb9, :wb22
208
+ case field_val[a]
209
+ when 1; di.instruction.opname += ((memref_size(di) == 2) ? '.ab' : '.a')
210
+ when 2; di.instruction.opname += '.ab'
211
+ when 3; di.instruction.opname += '.as'
212
+ end
213
+ when :sz1, :sz7, :sz16, :sz17
214
+ case field_val[a]
215
+ when 1; di.instruction.opname += 'b'
216
+ when 2; di.instruction.opname += 'w'
217
+ end
218
+ else
219
+ di.instruction.args << case a
220
+
221
+ # 16-bits instruction operands ------------------------------------------"
222
+ when :cr0; GPR.new 0
223
+ when :ca, :cb, :cb2, :cb3, :cc; GPR.new(field_val[a])
224
+ when :ch
225
+ if ((r = field_val[a]) == 0x3E)
226
+ tmp = edata.decode_imm(:u32, :little)
227
+ Expression[(tmp >> 16) | ((tmp & 0xffff) << 16)]
228
+ else
229
+ GPR.new(r)
230
+ end
231
+
232
+ when :@gps9, :@gps10, :@gps11
233
+ imm = (a == :@gps11) ? :cs11 : (a == :@gps10) ? :cs10 : :cs9
234
+ Memref.new(GPR.new(26), Expression[field_val[imm]], memref_size(di))
235
+
236
+ when :cu3, :cu5, :cu5ee, :cu6, :cu7, :cu7l, :cu8; Expression[field_val[a]]
237
+ when :cs9, :cs10, :cs11; Expression[field_val[a]]
238
+ when :cdisps7, :cdisps8, :cdisps10, :cdisps13; Expression[field_val[a]]
239
+ when :@cb; Memref.new(GPR.new(field_val[:cb]), nil, memref_size(di))
240
+ when :@cbu7, :@cbu6, :@cbu5; Memref.new(GPR.new(field_val[:cb]), Expression[field_val[a]], memref_size(di))
241
+ when :@cspu7; Memref.new(GPR.new(28), field_val[a], memref_size(di))
242
+ when :@cbcc; Memref.new(field_val[:cb], field_val[:cc], memref_size(di))
243
+
244
+ # 32-bits instruction operands ------------------------------------------"
245
+ when :a, :b
246
+ ((r = field_val[a]) == 0x3E) ? :zero : GPR.new(r)
247
+ when :b2; GPR.new field_val[:b]
248
+ when :c; GPR.new field_val[a]
249
+ when :bext, :cext; field_val[a]
250
+ when :@cext
251
+ target = field_val[a]
252
+ (di.opcode.props[:setip] and target.kind_of? GPR) ? Memref.new(target, nil, memref_size(di)) : target
253
+
254
+ when :@bextcext
255
+ tmp = field_val[a]
256
+ #c = tmp & 0x3F
257
+ tmp = tmp >> 6
258
+ #b = (tmp >> 12) | ((tmp & 0x7) << 3)
259
+ Memref.new(field_val[:bext], field_val[:cext], memref_size(di))
260
+
261
+ when :u6, :u6e, :s8e, :s9, :s12; Expression[field_val[a]]
262
+ when :s12e, :s21e, :s21ee, :s25e, :s25ee; Expression[field_val[a]]
263
+ when :auxs12; AUX.new field_val[:s12]
264
+ when :@c; Memref.new(GPR.new(field_val[a]), nil, memref_size(di))
265
+ when :@bcext; Memref.new(field_val[a], nil, memref_size(di))
266
+ #when :@bcext; Memref.new(field_val[:b], field_val[:cext], memref_size(di))
267
+ when :@bs9
268
+ # [b,s9] or [limm] if b = 0x3E
269
+ base = field_val[:bext]
270
+ Memref.new(base, (base.kind_of? GPR) ? Expression[field_val[a]] : nil, memref_size(di))
271
+
272
+ # common instruction operands ------------------------------------------"
273
+ when :zero; Expression[0]
274
+ when :gp; GPR.new(26)
275
+ when :sp, :sp2; GPR.new(28)
276
+ when :blink; GPR.new(31)
277
+ when :@ilink1; Memref.new(GPR.new(29), nil, memref_size(di))
278
+ when :@ilink2; Memref.new(GPR.new(30), nil, memref_size(di))
279
+ when :@blink; Memref.new(GPR.new(31), nil, memref_size(di))
280
+
281
+ else raise SyntaxError, "Internal error: invalid argument #{a} in #{op.name}"
282
+ end
283
+ end
284
+ }
285
+
286
+ di.bin_length += edata.ptr - before_ptr
287
+
288
+ return if edata.ptr > edata.virtsize
289
+
290
+ di
291
+ end
292
+
293
+ def disassembler_default_func
294
+ df = DecodedFunction.new
295
+ df.backtrace_binding = {}
296
+ 15.times { |i|
297
+ df.backtrace_binding["r#{i}".to_sym] = Expression::Unknown
298
+ }
299
+ df.backtracked_for = []
300
+ df.btfor_callback = lambda { |dasm, btfor, funcaddr, calladdr|
301
+ if funcaddr != :default
302
+ btfor
303
+ elsif di = dasm.decoded[calladdr] and di.opcode.props[:saveip]
304
+ btfor
305
+ else []
306
+ end
307
+ }
308
+ df
309
+ end
310
+
311
+ REG_SYMS = [:r26, :r27, :r28, :r29, :r30, :r31, :r60]
312
+ def register_symbols
313
+ REG_SYMS
314
+ end
315
+
316
+ def opshift(op)
317
+ op[/\d/].to_i
318
+ end
319
+
320
+ def with_res(arg)
321
+ arg != :zero
322
+ end
323
+
324
+ def init_backtrace_binding
325
+ sp = :r28
326
+ blink = :r31
327
+
328
+ @backtrace_binding ||= {}
329
+
330
+ mask = lambda { |sz| (1 << sz)-1 } # 32bits => 0xffff_ffff
331
+
332
+ opcode_list.each{|mode, oplist|
333
+ oplist.map { |ol| ol.name }.uniq.each { |op|
334
+ binding = case op
335
+ when /^add/, /^sub/
336
+ lambda { |di, a0, a1, a2|
337
+ if (shift = opshift(op)) == 0
338
+ { a0 => Expression[[a1, :+, a2], :&, mask[32]] }
339
+ else
340
+ { a0 => Expression[[a1, :+, [a2, :<<, shift]], :&, mask[32]] }
341
+ end
342
+ }
343
+ when /^and/
344
+ lambda { |di, a0, a1, a2| { a0 => Expression[a1, :&, a2] } }
345
+ when /^asl/
346
+ lambda { |di, *a| { a[0] => Expression[[a[1], :<<, (a[2] ? a[2]:1)], :&, mask[32]] } }
347
+ when /^bxor/
348
+ lambda { |di, a0, a1, a2| { a0 => Expression[a1, :^, [1, :<<, a2]] }}
349
+ when /^bclr/; lambda { |di, a0, a1, a2| { a0 => Expression[a1, :&, Expression[mask[32], :^, Expression[1, :<<, a2]]] } }
350
+ when /^bset/; lambda { |di, a0, a1, a2| { a0 => Expression[a1, :|, Expression[1, :<<, a2]] } }
351
+ when /^jl/; lambda { |di, a0| { blink => Expression[di.next_addr] } }
352
+ when 'bl', 'bl_s', /^bl\./
353
+ # FIXME handle delay slot
354
+ # "This address is taken either from the first instruction following the branch (current PC) or the
355
+ # instruction after that (next PC) according to the delay slot mode (.d)."
356
+ lambda { |di, a0| { blink => Expression[di.next_addr] } }
357
+ when /^mov/, /^lr/, /^ld/; lambda { |di, a0, a1| { a0 => a1 } }
358
+ when /^neg/; lambda { |di, a0, a1| { a0 => Expression[[0, :-, a1], :&, mask[32]] } }
359
+ when /^not/; lambda { |di, a0, a1| { a0 => Expression[[:~, a1], :&, mask[32]] } }
360
+ when /^or/; lambda { |di, a0, a1, a2| { a0 => Expression[a1, :|, a2] } }
361
+ when /^st/, /^sr/; lambda { |di, a0, a1| { a1 => a0 } }
362
+ when /^ex/; lambda { |di, a0, a1| { a1 => a0 , a0 => a1 } }
363
+ when 'push_s'
364
+ lambda { |di, a0| {
365
+ sp => Expression[sp, :-, 4],
366
+ Indirection[sp, @size/8, di.address] => Expression[a0]
367
+ } }
368
+ when 'pop_s'
369
+ lambda { |di, a0| {
370
+ a0 => Indirection[sp, @size/8, di.address],
371
+ sp => Expression[sp, :+, 4]
372
+ } }
373
+ end
374
+ @backtrace_binding[op] ||= binding if binding
375
+ }
376
+ }
377
+
378
+ @backtrace_binding
379
+ end
380
+
381
+ def get_xrefs_x(dasm, di)
382
+ return [] if not di.opcode.props[:setip]
383
+
384
+ arg = case di.opcode.name
385
+ when 'b', 'b_s', /^j/, /^bl/, /^br/, 'lp'
386
+ expr = di.instruction.args.last
387
+ expr.kind_of?(Memref) ? expr.base : expr
388
+ else di.instruction.args.last
389
+ end
390
+
391
+ [Expression[(arg.kind_of?(Reg) ? arg.symbolic : arg)]]
392
+ end
393
+
394
+ def backtrace_is_function_return(expr, di=nil)
395
+ Expression[expr].reduce == Expression[register_symbols[5]]
396
+ end
397
+
398
+ def delay_slot(di=nil)
399
+ return 0 if (not di) or (not di.opcode.props[:setip])
400
+ return 1 if di.opcode.props[:delay_slot]
401
+ (di.instruction.opname =~ /\.d/) ? 0 : 1
402
+ end
403
+ end
404
+ end