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
@@ -84,7 +84,7 @@ class Preprocessor
84
84
  # modifies the list, returns an array of list of tokens/nil
85
85
  # handles nesting
86
86
  def self.parse_arglist(lexer, list=nil)
87
- readtok = lambda { list ? list.shift : lexer.readtok(false) }
87
+ readtok = lambda { list ? list.shift : lexer.readtok_nopp }
88
88
  unreadtok = lambda { |t| list ? (list.unshift(t) if t) : lexer.unreadtok(t) }
89
89
  tok = nil
90
90
  unreadlist = []
@@ -378,6 +378,7 @@ class Preprocessor
378
378
  # hash filename => file content
379
379
  attr_accessor :hooked_include
380
380
  attr_accessor :warn_redefinition
381
+ attr_accessor :may_preprocess
381
382
 
382
383
  # global default search directory for #included <files>
383
384
  @@include_search_path = ['/usr/include']
@@ -385,18 +386,14 @@ class Preprocessor
385
386
  def self.include_search_path=(np) @@include_search_path=np end
386
387
 
387
388
  def initialize(text='')
388
- @queue = []
389
389
  @backtrace = []
390
390
  @definition = %w[__FILE__ __LINE__ __COUNTER__ __DATE__ __TIME__].inject({}) { |h, n| h.update n => SpecialMacro.new(n) }
391
391
  @include_search_path = @@include_search_path.dup
392
392
  # stack of :accept/:discard/:discard_all/:testing, represents the current nesting of #if..#endif
393
393
  @ifelse_nesting = []
394
- @text = text
395
- @pos = 0
396
- @filename = 'unknown'
397
- @lineno = 1
398
394
  @warn_redefinition = true
399
395
  @hooked_include = {}
396
+ @may_preprocess = false
400
397
  @pragma_once = {}
401
398
  @pragma_callback = lambda { |otok|
402
399
  tok = otok
@@ -405,6 +402,7 @@ class Preprocessor
405
402
  unreadtok tok
406
403
  puts otok.exception("unhandled pragma #{str.inspect}").message if $VERBOSE
407
404
  }
405
+ feed!(text)
408
406
  define '__METASM__', VERSION
409
407
  end
410
408
 
@@ -493,11 +491,16 @@ class Preprocessor
493
491
  def feed!(text, filename='unknown', lineno=1)
494
492
  raise ArgumentError, 'need something to parse!' if not text
495
493
  @text = text
494
+ if not @may_preprocess and (@text =~ /^\s*(#|\?\?=)/ or (not @definition.empty? and
495
+ @text =~ /#{@definition.keys.map { |k| Regexp.escape(k) }.join('|')}/))
496
+ @may_preprocess = true
497
+ end
496
498
  # @filename[-1] used in trace_macros to distinguish generic/specific files
497
499
  @filename = "\"#{filename}\""
498
500
  @lineno = lineno
499
501
  @pos = 0
500
502
  @queue = []
503
+ @backtrace = []
501
504
  self
502
505
  end
503
506
 
@@ -512,7 +515,7 @@ class Preprocessor
512
515
 
513
516
  # reads one character from self.text
514
517
  # updates self.lineno
515
- # handles trigraphs and \-continued lines
518
+ # handles \-continued lines
516
519
  def getchar
517
520
  @ungetcharpos = @pos
518
521
  @ungetcharlineno = @lineno
@@ -520,11 +523,11 @@ class Preprocessor
520
523
  @pos += 1
521
524
 
522
525
  # check trigraph
523
- if c == ?? and @text[@pos] == ?? and Trigraph[@text[@pos+1]]
524
- puts "can i has trigraf plox ??#{c.chr} (#@filename:#@lineno)" if $VERBOSE
525
- c = Trigraph[@text[@pos+1]]
526
- @pos += 2
527
- end
526
+ #if c == ?? and @text[@pos] == ?? and Trigraph[@text[@pos+1]]
527
+ # puts "can i has trigraf plox ??#{c.chr} (#@filename:#@lineno)" if $VERBOSE
528
+ # c = Trigraph[@text[@pos+1]]
529
+ # @pos += 2
530
+ #end
528
531
 
529
532
  # check line continuation
530
533
  # TODO portability
@@ -567,9 +570,9 @@ class Preprocessor
567
570
  end
568
571
 
569
572
  # calls readtok_nopp and handles preprocessor directives
570
- def readtok(expand_macros = true)
571
- lastpos = @pos
573
+ def readtok
572
574
  tok = readtok_nopp
575
+ return tok if not @may_preprocess # shortcut
573
576
 
574
577
  if not tok
575
578
  # end of file: resume parent
@@ -579,32 +582,41 @@ class Preprocessor
579
582
  tok = readtok
580
583
  end
581
584
 
582
- elsif (tok.type == :eol or lastpos == 0) and @ifelse_nesting.last != :testing
583
- unreadtok tok if lastpos == 0
584
- # detect preprocessor directive
585
- # state = 1 => seen :eol, 2 => seen #
585
+ elsif tok.type == :punct and tok.raw == '#' and not tok.expanded_from and @ifelse_nesting.last != :testing
586
+ # backward check for :eol (skip the '#' itself)
587
+ pos = @pos-2
588
+ while pos >= 0 # if reach start of file, proceed
589
+ case @text[pos, 1]
590
+ when "\n"
591
+ pos -= 1 if pos > 0 and @text[pos-1] == ?\r
592
+ return tok if pos > 0 and @text[pos-1] == ?\\ # check if the newline was a line-continuation
593
+ return tok if pos > 2 and @text[pos-3, 3] == '??/' # trigraph
594
+ break # proceed
595
+ when /\s/ # beware switch order, this matches "\n" too
596
+ else return tok # false alarm
597
+ end
598
+ pos -= 1
599
+ end
586
600
  pretok = []
587
601
  rewind = true
588
- state = 1
589
- loop do
590
- pretok << (ntok = readtok_nopp)
591
- break if not ntok
602
+ while ntok = readtok_nopp
603
+ pretok << ntok
592
604
  if ntok.type == :space # nothing
593
- elsif state == 1 and ntok.type == :punct and ntok.raw == '#' and not ntok.expanded_from
594
- state = 2
595
- elsif state == 2 and ntok.type == :string and not ntok.expanded_from
605
+ next
606
+ elsif ntok.type == :string and not ntok.expanded_from
596
607
  rewind = false if preprocessor_directive(ntok)
597
- break
598
- else break
599
608
  end
609
+ break
600
610
  end
601
611
  if rewind
602
612
  # false alarm: revert
603
613
  pretok.reverse_each { |t| unreadtok t }
614
+ else
615
+ # XXX return :eol ?
616
+ tok = readtok
604
617
  end
605
- tok = readtok if lastpos == 0 # else return the :eol
606
618
 
607
- elsif expand_macros and tok.type == :string and m = @definition[tok.raw] and not tok.expanded_from.to_a.find { |ef| ef.raw == m.name.raw } and
619
+ elsif tok.type == :string and m = @definition[tok.raw] and not tok.expanded_from.to_a.find { |ef| ef.raw == m.name.raw } and
608
620
  ((m.args and margs = Macro.parse_arglist(self)) or not m.args)
609
621
 
610
622
  if defined? @traced_macros and tok.backtrace[-2].to_s[0] == ?" and m.name and m.name.backtrace[-2].to_s[0] == ?<
@@ -637,21 +649,20 @@ class Preprocessor
637
649
  when ?a..?z, ?A..?Z, ?0..?9, ?$, ?_
638
650
  tok.type = :string
639
651
  raw = tok.raw << c
640
- loop do
641
- case c = getchar
642
- when nil; ungetchar; break # avoids 'no method "coerce" for nil' warning
652
+ while c = getchar
653
+ case c
643
654
  when ?a..?z, ?A..?Z, ?0..?9, ?$, ?_
644
- raw << c
645
- else ungetchar; break
655
+ else break
646
656
  end
657
+ raw << c
647
658
  end
659
+ ungetchar
648
660
 
649
661
  when ?\ , ?\t, ?\r, ?\n, ?\f
650
662
  tok.type = ((c == ?\ || c == ?\t) ? :space : :eol)
651
663
  raw = tok.raw << c
652
- loop do
653
- case c = getchar
654
- when nil; break
664
+ while c = getchar
665
+ case c
655
666
  when ?\ , ?\t
656
667
  when ?\n, ?\f, ?\r; tok.type = :eol
657
668
  else break
@@ -676,8 +687,7 @@ class Preprocessor
676
687
  tok.type = :space
677
688
  raw << c
678
689
  seenstar = false
679
- loop do
680
- raise tok, 'unterminated c++ comment' if not c = getchar
690
+ while c = getchar
681
691
  raw << c
682
692
  case c
683
693
  when ?*; seenstar = true
@@ -685,6 +695,7 @@ class Preprocessor
685
695
  else seenstar = false
686
696
  end
687
697
  end
698
+ raise tok, 'unterminated c++ comment' if not c
688
699
  else
689
700
  # just a slash
690
701
  ungetchar
@@ -705,6 +716,7 @@ class Preprocessor
705
716
  tok.type = :quoted
706
717
  tok.raw << delimiter
707
718
  tok.value = ''
719
+ tok.value.force_encoding('binary') if tok.value.respond_to?(:force_encoding)
708
720
  c = nil
709
721
  loop do
710
722
  raise tok, 'unterminated string' if not c = getchar
@@ -767,6 +779,9 @@ class Preprocessor
767
779
  def define(name, value=nil, from=caller.first)
768
780
  from =~ /^(.*?):(\d+)/
769
781
  btfile, btlineno = $1, $2.to_i
782
+ if not @may_preprocess and @text =~ /#{Regexp.escape name}/
783
+ @may_preprocess = true
784
+ end
770
785
  t = Token.new([btfile, btlineno])
771
786
  t.type = :string
772
787
  t.raw = name.dup
@@ -1095,7 +1110,7 @@ class Preprocessor
1095
1110
  nil while dir = readtok and dir.type == :space
1096
1111
  raise cmd, 'qstring expected' if not dir or dir.type != :quoted
1097
1112
  dir = ::File.expand_path dir.value
1098
- raise cmd, 'invalid path' if not ::File.directory? dir
1113
+ raise cmd, "invalid path #{dir.inspect}" if not ::File.directory? dir
1099
1114
  @include_search_path.unshift dir
1100
1115
 
1101
1116
  when 'push_macro', 'pop_macro'
@@ -1161,7 +1176,7 @@ class Preprocessor
1161
1176
  op = op.dup
1162
1177
  op.raw << ntok.raw
1163
1178
  # ok
1164
- when '^', '+', '-', '*', '/', '%', '>>', '<<', '>=', '<=', '||', '&&', '!=', '=='
1179
+ when '^', '+', '-', '*', '/', '%', '>>', '<<', '>=', '<=', '||', '&&', '!=', '==', '?'
1165
1180
  # unknown
1166
1181
  else
1167
1182
  lexer.unreadtok tok
@@ -1258,6 +1273,21 @@ class Preprocessor
1258
1273
  stack << Expression.new(opstack.pop, stack.pop, stack.pop)
1259
1274
  end
1260
1275
 
1276
+ if op.value == :'?'
1277
+ a1 = parse(lexer)
1278
+ if not tok = lexer.readtok or tok.type != :punct or tok.raw != ':'
1279
+ raise op, 'expected ":" ternary operator'
1280
+ end
1281
+ a2 = parse(lexer)
1282
+ case Expression[stack.pop].reduce
1283
+ when 0; stack << a2
1284
+ when ::Integer; stack << a1
1285
+ else; stack << a2
1286
+ end
1287
+
1288
+ next
1289
+ end
1290
+
1261
1291
  opstack << op.value
1262
1292
 
1263
1293
  raise op, 'need rhs' if not e = parse_value(lexer)
@@ -19,8 +19,10 @@ module Renderable
19
19
  r = proc { |e|
20
20
  case e
21
21
  when Expression
22
- yield e
23
22
  r[e.lexpr] ; r[e.rexpr]
23
+ yield e
24
+ when ExpressionType
25
+ yield e
24
26
  when Renderable
25
27
  e.render.each { |re| r[re] }
26
28
  end
@@ -64,45 +66,13 @@ end
64
66
 
65
67
  class Expression
66
68
  include Renderable
67
- attr_accessor :render_info
68
-
69
- # this is an accessor to @@render_int, the lambda used to render integers > 10
70
- # usage: Expression.render_int = lambda { |e| '0x%x' % e }
71
- # or Expression.render_int { |e| '0x%x' % e }
72
- # XXX the returned string should be suitable for inclusion in a label name etc
73
- def self.render_int(&b)
74
- if b
75
- @@render_int = b
76
- else
77
- @@render_int
78
- end
79
- end
80
- def self.render_int=(p)
81
- @@render_int = p
82
- end
83
- @@render_int = nil
84
69
 
85
70
  def render_integer(e)
86
- if render_info and @render_info[:char]
87
- ee = e
88
- v = []
89
- while ee > 0
90
- v << (ee & 0xff)
91
- ee >>= 8
92
- end
93
- v.reverse! if @render_info[:char] == :big
94
- if not v.empty? and v.all? { |c| c < 0x7f }
95
- # XXX endianness
96
- return "'" + v.pack('C*').inspect.gsub("'") { '\\\'' }[1...-1] + "'"
97
- end
98
- end
99
71
  if e < 0
100
72
  neg = true
101
73
  e = -e
102
74
  end
103
75
  if e < 10; e = e.to_s
104
- elsif @@render_int
105
- e = @@render_int[e]
106
76
  else
107
77
  e = '%xh' % e
108
78
  e = '0' << e unless (?0..?9).include? e[0]
@@ -114,17 +84,23 @@ class Expression
114
84
  NOSQ1 = NOSQ2 = {:* => [:*], :+ => [:+, :-, :*], :- => [:+, :-, :*]}
115
85
  NOSQ2[:-] = [:*]
116
86
  def render
117
- l = @lexpr.kind_of?(Integer) ? render_integer(@lexpr) : @lexpr
118
- r = @rexpr.kind_of?(Integer) ? render_integer(@rexpr) : @rexpr
119
- l = ['(', l, ')'] if @lexpr.kind_of? Expression and (not oa = NOSQ1[@op] or not oa.include?(@lexpr.op))
120
- r = ['(', r, ')'] if @rexpr.kind_of? Expression and (not oa = NOSQ2[@op] or not oa.include?(@rexpr.op))
87
+ l = @lexpr.kind_of?(::Integer) ? render_integer(@lexpr) : @lexpr
88
+ r = @rexpr.kind_of?(::Integer) ? render_integer(@rexpr) : @rexpr
89
+ l = ['(', l, ')'] if @lexpr.kind_of?(Expression) and (not oa = NOSQ1[@op] or not oa.include?(@lexpr.op))
90
+ r = ['(', r, ')'] if @rexpr.kind_of?(Expression) and (not oa = NOSQ2[@op] or not oa.include?(@rexpr.op))
121
91
  op = @op if l or @op != :+
122
92
  if op == :+
123
93
  r0 = [r].flatten.first
124
94
  r0 = r0.render.flatten.first while r0.kind_of? Renderable
125
- op = nil if (r0.kind_of? Integer and r0 < 0) or (r0.kind_of? String and r0[0] == ?-) or r0 == :-
95
+ op = nil if (r0.kind_of?(::Integer) and r0 < 0) or (r0.kind_of?(::String) and r0[0] == ?-) or r0 == :-
126
96
  end
127
97
  [l, op, r].compact
128
98
  end
129
99
  end
100
+
101
+ class ExpressionString
102
+ include Renderable
103
+
104
+ def render; hide_str ? @expr.render : render_str ; end
105
+ end
130
106
  end
data/misc/hexdump.rb CHANGED
@@ -29,7 +29,7 @@ def hexdump(ctx={})
29
29
  print s.unpack('C*').map { |b| '%02x' % b }.join(' ').ljust(3*16-1) + ' ' if fmt.include? 'c'
30
30
  print s.unpack('v*').map { |b| '%04x' % b }.join(' ').ljust(5*8-1) + ' ' if fmt.include? 'w'
31
31
  print s.unpack('L*').map { |b| '%08x' % b }.join(' ').ljust(9*4-1) + ' ' if fmt.include? 'd'
32
- print s.tr("\0-\x1f\x7f-\xff", '.') if fmt.include? 'a'
32
+ print s.tr("\0-\x1f\x7f-\xff".force_encoding('BINARY'), '.') if fmt.include? 'a'
33
33
  puts
34
34
  elsif not ctx[:lastdup]
35
35
  ctx[:lastdup] = true
@@ -50,6 +50,7 @@ if $0 == __FILE__
50
50
  fmt << 'w' if ARGV.delete '-W'
51
51
  fmt << 'd' if ARGV.delete '-D'
52
52
  fmt << 'a' if ARGV.delete '-A'
53
- fmt = ['c', 'd', 'a'] if ARGV.delete '-a'
54
- File.open(ARGV.first, 'rb').hexdump(:fmt => fmt)
53
+ fmt = ['c', 'd', 'a'] if ARGV.delete '-a' or fmt.empty?
54
+ infd = ARGV.empty? ? $stdin : File.open(ARGV.first, 'rb')
55
+ infd.hexdump(:fmt => fmt)
55
56
  end
data/misc/lint.rb ADDED
@@ -0,0 +1,58 @@
1
+ #!/usr/bin/ruby
2
+ # This file is part of Metasm, the Ruby assembly manipulation suite
3
+ # Copyright (C) 2006-2009 Yoann GUILLOT
4
+ #
5
+ # Licence is LGPL, see LICENCE in the top-level directory
6
+
7
+
8
+ # this is a ruby code cleaner tool
9
+ # it passes its argument to ruby -v -c, which displays warnings (eg unused variable)
10
+ # it shows the incriminated line along the warning, to help identify false positives
11
+ # probably linux-only, and need ruby-1.9.1 or newer
12
+
13
+ def lint(tg)
14
+ if File.symlink?(tg)
15
+ # nothing
16
+ elsif File.directory?(tg)
17
+ Dir.entries(tg).each { |ent|
18
+ next if ent == '.' or ent == '..'
19
+ ent = File.join(tg, ent)
20
+ lint(ent) if File.directory?(ent) or ent =~ /\.rb$/
21
+ }
22
+ else
23
+ lint_file(tg)
24
+ end
25
+ end
26
+
27
+ def lint_file(tg)
28
+ flines = nil
29
+ compile_warn(tg).each_line { |line|
30
+ file, lineno, warn = line.split(/\s*:\s*/, 3)
31
+ if file == tg
32
+ if not flines
33
+ puts "#{tg}:"
34
+ flines = File.readlines(file) #File.open(file, 'rb') { |fd| fd.readlines }
35
+ end
36
+ puts " l.#{lineno}: #{warn.strip}: #{flines[lineno.to_i-1].strip.inspect}"
37
+ end
38
+ }
39
+ puts if flines
40
+ end
41
+
42
+ def compile_warn(tg)
43
+ r, w = IO.pipe('binary')
44
+ if !fork
45
+ r.close
46
+ $stderr.reopen w
47
+ $stdout.reopen '/dev/null'
48
+ exec 'ruby', '-v', '-c', tg
49
+ exit!
50
+ else
51
+ w.close
52
+ end
53
+ r
54
+ end
55
+
56
+ ARGV << '.' if ARGV.empty?
57
+ ARGV.each { |arg| lint arg }
58
+
data/misc/objdiff.rb CHANGED
@@ -22,7 +22,10 @@ def Object.diff(o1, o2)
22
22
  h["[#{k.inspect}]"] = d if not d.empty?
23
23
  }
24
24
  else
25
- a = (@@diff_accessor_cache ||= {})[o1.class] ||= (im = o1.class.public_instance_methods.grep(/^[a-z]/) ; (im & im.map { |m| m + '=' }).map { |m| m.chop }.find_all { |m| o1.instance_variable_get('@'+m) })
25
+ a = ($diff_accessor_cache ||= {})[o1.class] ||= (
26
+ im = o1.class.public_instance_methods.map { |m| m.to_s }.grep(/^[a-z]/)
27
+ (im & im.map { |m| m+'=' }).map { |m| m.chop }.find_all { |m| o1.instance_variable_get('@'+m) }
28
+ )
26
29
  if a.empty?
27
30
  return o1 == o2 ? h : [o1, o2]
28
31
  end
data/misc/objscan.rb CHANGED
@@ -26,7 +26,7 @@ class Object
26
26
  end
27
27
  scan_iter { |v, p|
28
28
  case v
29
- when Fixnum, Symbol; next
29
+ when Integer, Symbol; next
30
30
  end
31
31
  p = path+p
32
32
  if done[v.object_id]
@@ -0,0 +1,79 @@
1
+ #!/usr/bin/ruby
2
+
3
+ require 'xml'
4
+
5
+ xml = Xml.parse_file(ARGV.shift || 'openrisc-insn.html')
6
+
7
+ # [name, bin, args]
8
+ addop = []
9
+ # arg => [flds]
10
+ valid_args = {}
11
+ # field => [bitoff, bitmask]
12
+ fields = {}
13
+
14
+ xml.each('ul') { |ul|
15
+ syntax = nil
16
+ bits = []
17
+ vals = []
18
+ trno = 0
19
+ ul.each('li') { |li|
20
+ if li.children[0] == 'syntax:'
21
+ # <li>syntax:<tt><font>l.add $rd, $ra, $rb</font></tt></li>
22
+ syntax = li.children[1].children[0].children[0]
23
+ end
24
+ }
25
+ next if not syntax
26
+ ul.each('tr') { |tr|
27
+ case trno
28
+ when 0; tr.each('td') { |td| bits << td.children[0].split.map { |b| b.to_i } }
29
+ when 2; tr.each('td') { |td| vals << td.children.map { |v| v =~ /^0x/ ? v.to_i(16) : v.gsub('-', '') }.first }
30
+ end
31
+ trno += 1
32
+ }
33
+
34
+ iname = syntax.split[0].sub(/^l\./, '')
35
+ iargs = syntax.split[1].to_s.split(',').map { |a| a.gsub(/[${}-]/, '').gsub(/(\w+)\((\w+)\)/, '\2_\1') }
36
+ bin = bits.zip(vals).inject(0) { |b, (bt, bv)| bv.kind_of?(Integer) ? b | (bv << bt.last) : b }
37
+ addop << [iname, bin]
38
+
39
+ flds = bits.zip(vals).inject({}) { |h, (bt, bv)|
40
+ next h if bv.kind_of?(Integer)
41
+ blen = bt.first + 1 - bt.last
42
+ h.update bv => [bt.last, (1 << blen) - 1]
43
+ }
44
+ flds.each { |n, (o, m)|
45
+ if not fields[n]
46
+ fields[n] = [o, m]
47
+ elsif fields[n] != [o, m]
48
+ puts "# fields mismatch in #{iname} #{n}"
49
+ end
50
+ }
51
+
52
+ addop.last << iargs
53
+
54
+ iargs.each { |a|
55
+ a.split('_').each { |f|
56
+ if not flds.delete(f)
57
+ puts "# no field #{f} for arg #{a} in #{iname}"
58
+ end
59
+ }
60
+ valid_args[a] ||= a.split('_')
61
+ }
62
+ flds.each_key { |f|
63
+ puts "# no arg using #{f} in #{iname}"
64
+ a_i = "#{f}_ign"
65
+ valid_args[a_i] ||= [f]
66
+ addop.last.last << a_i
67
+ }
68
+ }
69
+
70
+ puts "\tdef init_cpu"
71
+ puts "\t\t@opcode_list = []"
72
+ puts "\t\t@valid_args = { #{valid_args.map { |a, f| ":#{a} => [#{f.map { |ff| ':' + ff }.join(', ')}]" }.join(', ')} }"
73
+ puts "\t\t@fields_off = { #{fields.map { |k, v| ":#{k} => #{v[0]}" }.join(', ')} }"
74
+ puts "\t\t@fields_mask = { #{fields.map { |k, v| ":#{k} => #{'0x%02X' % v[1]}" }.join(', ')} }"
75
+ puts
76
+ addop.each { |op|
77
+ puts "\t\taddop '#{op[0]}', #{'0x%08X' % op[1]}#{op[2].map { |a| ", :#{a}" }.join('')}"
78
+ }
79
+ puts "\tend"