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'
|
|
|
|
|
|
|
|
opts = { :cpu => 'Ia32', :exe => 'Shellcode' }
|
|
|
|
OptionParser.new { |opt|
|
|
|
|
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('-v') { $VERBOSE = true }
|
|
|
|
opt.on('-d') { $VERBOSE = $DEBUG = true }
|
|
|
|
}.parse!(ARGV)
|
2009-03-29 07:30:54 +00:00
|
|
|
|
|
|
|
src = ARGV.empty? ? <<EOS : ARGF.read
|
|
|
|
void foo(int);
|
|
|
|
void bla()
|
|
|
|
{
|
|
|
|
int i = 10;
|
|
|
|
while (--i)
|
|
|
|
foo(i);
|
|
|
|
}
|
|
|
|
EOS
|
|
|
|
|
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)
|
|
|
|
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)
|
|
|
|
cp.parse src
|
2009-03-29 07:30:54 +00:00
|
|
|
puts cpu.new_ccompiler(cp).compile
|