#!/usr/bin/perl -w

# decomp_insert_symbols : A decomp filter to insert symbol table entries.
# 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_insert_symbols inserts symbol table entries into the dump.  This
# typically only affects lines like:   call   0x804ffff
# This makes reading the dump much easier.
#
# Sample usage:
# % gendump the-binary | decomp_insert_symbols symbols > dump

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

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


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

my $symbols_filename = $ARGV[0];

# read offsets for object files
my %symbols = &read_symbols($symbols_filename);

# do the filtering
&filter(\%symbols);


exit 0;

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

# -----------------------------------------------------------------------------
# Reads in data from the symbols file.
# Params: $filename - name of the file containing the symbols
# Returns: hash of symbol offsets to names

sub read_symbols($)
{
   my ($filename) = @_;
   my ($fh, $line, %symbols);

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

   %symbols = ();

   # for each line (representing a symbol entry)
   while ($line = <$fh>) {
      chomp ($line);
      next if $line =~ /^$/;         # skip empty lines
      next if $line =~ /^#/;         # skip comment lines

      if ($line =~ /(.*) (.*)/) {
         $symbols{$1} = $2;
      }
      else {
         die "bad input `$line'";
      }
   }
   close $fh;

   return %symbols;
}

# -----------------------------------------------------------------------------
# Reads in data from stdin, performs the filter, then write result to stdout.
# Params: %symbols - hash of symbols
# Returns: none

sub filter(\%)
{
   my ($ref_symbols) = @_;
   my ($line, $offset, $prev, $ref_sym, $key, $trailer, $name, $to_insert);

   # for each line
   $prev = "";
   while ($line = <STDIN>) {
      chomp($line);
      $trailer = "";

      # skip empty lines and comment lines
      unless ($line =~ /^$/ or $line =~ /^#/ or $line =~ /^;/) {
         # search for symbol match
         foreach $key (keys %$ref_symbols) {
            if (index($line, $key) != -1) {
               if ($line !~ /<.*>$/) {
                  $trailer = " <$ref_symbols->{$key}>";
                  last;
               }
            }
         }

         # label previous line if symbol is defined at this offset
         $offset = substr($line, 0, 8);
         if (exists $ref_symbols->{'0x' . $offset}) {
            $name = $ref_symbols->{'0x' . $offset};
            $to_insert = "# $offset <$name>:";
            unless ($prev eq $to_insert) {
               print STDOUT "\n";
               print STDOUT $to_insert, "\n";
            }
         }
      }

      print STDOUT $line, $trailer, "\n";

      # remember previous line
      $prev = $line;
   }
}

############################### 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_quiet)   = 0;
   my ($want_version) = 0;
   my ($want_help)    = 0;

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

   if ($want_version) {
      print "$0 $VERSION\n";
      exit 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 insert symbols into the dump.
Reads in the current dump from stdin and writes the modified dump to stdout.

Usage: $0 [options] <symbols>
   symbols is a file containing symbol offset and name pairs
Options:
    -V, --version         outputs version information and exits
    -h, --help            displays this help and exits

_END

   exit 1;
}

