2010-09-09 18:19:35 +00:00
|
|
|
# This file is part of Metasm, the Ruby assembly manipulation suite
|
|
|
|
# Copyright (C) 2006-2009 Yoann GUILLOT
|
|
|
|
#
|
|
|
|
# Licence is LGPL, see LICENCE in the top-level directory
|
|
|
|
|
|
|
|
# shows the compilation phase step by step: c, simplified c, asm
|
|
|
|
|
2009-03-29 07:30:54 +00:00
|
|
|
require 'metasm'
|
2010-09-09 18:19:35 +00:00
|
|
|
require 'optparse'
|
|
|
|
|
2011-04-06 17:40:01 +00:00
|
|
|
opts = { :cpu => 'Ia32', :exe => 'Shellcode', :macros => {} }
|
2010-09-09 18:19:35 +00:00
|
|
|
OptionParser.new { |opt|
|
2013-08-30 21:28:33 +00:00
|
|
|
opt.on('--pic', 'generate position-independant code') { opts[:pic] = true }
|
|
|
|
opt.on('--cpu cpu') { |c| opts[:cpu] = c }
|
|
|
|
opt.on('--exe exe') { |e| opts[:exe] = e }
|
|
|
|
opt.on('-D var=val', 'define a preprocessor macro') { |v| v0, v1 = v.split('=', 2) ; opts[:macros][v0] = v1 }
|
|
|
|
opt.on('-v') { $VERBOSE = true }
|
|
|
|
opt.on('-d') { $VERBOSE = $DEBUG = true }
|
2010-09-09 18:19:35 +00:00
|
|
|
}.parse!(ARGV)
|
2009-03-29 07:30:54 +00:00
|
|
|
|
|
|
|
src = ARGV.empty? ? <<EOS : ARGF.read
|
|
|
|
void foo(int);
|
|
|
|
void bla()
|
|
|
|
{
|
2013-08-30 21:28:33 +00:00
|
|
|
int i = 10;
|
|
|
|
while (--i)
|
|
|
|
foo(i);
|
2009-03-29 07:30:54 +00:00
|
|
|
}
|
|
|
|
EOS
|
|
|
|
|
2011-04-06 17:40:01 +00:00
|
|
|
pp = opts[:macros].map { |k, v| "#define #{k} #{v}" }.join("\n")
|
|
|
|
|
2010-09-09 18:19:35 +00:00
|
|
|
cpu = Metasm.const_get(opts[:cpu]).new
|
|
|
|
exe = Metasm.const_get(opts[:exe]).new(cpu)
|
|
|
|
cpu.generate_PIC = false unless opts[:pic]
|
|
|
|
|
|
|
|
cp = Metasm::C::Parser.new(exe)
|
2011-04-06 17:40:01 +00:00
|
|
|
cp.parse pp
|
2010-09-09 18:19:35 +00:00
|
|
|
cp.parse src
|
2009-03-29 07:30:54 +00:00
|
|
|
puts cp, '', ' ----', ''
|
2010-09-09 18:19:35 +00:00
|
|
|
|
2009-03-29 07:30:54 +00:00
|
|
|
cp.precompile
|
|
|
|
puts cp, '', ' ----', ''
|
|
|
|
|
2010-09-09 18:19:35 +00:00
|
|
|
cp = Metasm::C::Parser.new(exe)
|
2011-04-06 17:40:01 +00:00
|
|
|
cp.parse pp
|
2010-09-09 18:19:35 +00:00
|
|
|
cp.parse src
|
2009-03-29 07:30:54 +00:00
|
|
|
puts cpu.new_ccompiler(cp).compile
|