#!/usr/bin/perl -w

# decomp_strip : A decomp filter to strip statically linked object code.
# Copyright (C) 2002 Dion Mendel
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.

# decomp_strip removes statically linked object files from the assembly dump.
# This makes reading the dump much easier.
#
# Sample usage:
# % for i in obj/*.o ; do
#      elfgrep -t $i the-binary
#   done | elfgrep_fixup > object_files
# % gendump the-binary | decomp_strip object_files > stripped_dump

use Getopt::Long "GetOptions";
use FileHandle;
use strict 'vars';
use vars '$VERSION', '$Verbose', '$StripAll';

$VERSION = "1.0";                            # version of this program
$Verbose = 1;
$StripAll = 1;


&parse_command_line_for_options();
&usage if (scalar @ARGV != 1);

my $obj_listing = $ARGV[0];

# read offsets for object files
my @offsets = &read_obj_offsets($obj_listing);

# do the filtering
&filter(\@offsets);


exit 0;

############################### Filter Functions ##############################

# -----------------------------------------------------------------------------
# Reads in data from the object file listing.
# Params: $filename - name of the file containing the listing
# Returns: list of details

sub read_obj_offsets($)
{
   my ($filename) = @_;
   my ($fh, $line, @offsets, $name, $base_offset, $size);

   $fh = new FileHandle;
   open $fh, "< $filename" or die "could not open file `$filename': $!";

   @offsets = ();

   # for each line (representing an object file)
   while ($line = <$fh>) {
      next if $line =~ /^$/;         # skip empty lines
      next if $line =~ /^#/;         # skip comment lines

      if ($line =~ /(.*) - match at (0x.*) \((0x.*) bytes\)/) {
         # get object file details
         $name = $1;
         $base_offset = oct($2);
         $size = oct($3);
         # store details
         push( @offsets, { name => $name, start => $base_offset,
                           end => $base_offset + $size });
      }
      else {
         die "bad input `$line'";
      }
   }
   close $fh;

   # terminator
   push(@offsets, { name => "dummy", start => 0xffffffff, end => 0xffffffff });

   # return list sorted by starting offset
   return sort { $a->{start} <=> $b->{start} } @offsets;
}

# -----------------------------------------------------------------------------
# Reads in data from stdin, performs the filter, then write result to stdout.
# Params: @offsets - list of offsets of object files.
# Returns: none

sub filter(\@)
{
   my ($ref_offsets) = @_;
   my ($line, $ref_off, $name, $start, $end, $in_code, $cur_offset, $str);

   my @output = ();

   $in_code = 0;

   # get offset details
   $ref_off = shift @$ref_offsets;
   $name = $ref_off->{name};
   $start = $ref_off->{start};
   $end = $ref_off->{end};

   # for each line (representing an object file)
   while ($line = <STDIN>) {
      chomp($line);
      # skip empty lines and comment lines
      if ($line =~ /^$/ or $line =~ /^#/ or $line =~ /^;/) {
         push (@output, $line) unless $in_code;
         next;
      }

      # get offset of current line
      $str = '0x' . substr($line, 0, 8);
      $cur_offset = oct($str);

      if ((not $in_code) and ($cur_offset >= $start)) {
         # now in middle of object code to remove
         $in_code = 1;
      }

      if (($in_code) and ($cur_offset >= $end)) {
         # at end of object code to remove
         push @output, sprintf("# replaced %s (0x%x .. 0x%x)", $name,
                               $start, $end);
         $in_code = 0;

         # get next offset details
         $ref_off = shift @$ref_offsets;
         $name = $ref_off->{name};
         $start = $ref_off->{start};
         $end = $ref_off->{end};
      }

      if ((not $in_code) and ($cur_offset >= $start)) {
         # now in middle of object code to remove
         $in_code = 1;
      }

      if (not $in_code) {
         push @output, $line;
      }
   }

   if ($StripAll) {
      my @cleaned_output = ();
      my $clean = 0;
      foreach $line (@output) {
         if ($clean and (not $line =~ /nop/)) {
            $clean = 0;
         }
         if ($line =~ /# replaced .* \(0x.*0x.*\)/) {
            $clean = 1;
         }

         if (not $clean) {
            push @cleaned_output, $line;
         }
      }

      @output = @cleaned_output;
   }

   # output results
   foreach $line (@output) {
      print STDOUT $line, "\n";
   }
}

############################### Usage Functions ###############################

# -----------------------------------------------------------------------------
# Parses the command line for any specified options.  Sets the appropriate
# option flags if options are specified.  Prints usage info if invalid options
# are given.
# Returns: nothing

sub parse_command_line_for_options()
{
   my ($want_markers) = 0;
   my ($want_quiet)   = 0;
   my ($want_version) = 0;
   my ($want_help)    = 0;

   &GetOptions("q|quiet"   => \$want_quiet,
               "m|leave-markers" => \$want_markers,
               "V|version" => \$want_version,
               "h|help"    => \$want_help,
              );

   if ($want_version) {
      print "$0 $VERSION\n";
      exit 0;
   }

   if ($want_markers) {
      $StripAll = 0;
   }

   if ($want_help) {
      &usage();
   }

   $Verbose = !$want_quiet;
}

# -----------------------------------------------------------------------------
# Prints a nice usage message to stdout, and then exits.

sub usage()
{
   print <<"_END";

$0 v${VERSION}
A decomp filter to remove statically linked object code from the assembly dump.
Reads in the current dump from stdin and writes the modified dump to stdout.

Usage: $0 [options] <objfile listing>
   objfile listing is a listing generated from elfgrep_fixup
Options:
    -m, --leave-markers   leave markers for deleted object code in dump
    -V, --version         outputs version information and exits
    -h, --help            displays this help and exits

_END

   exit 1;
}

