#!/usr/bin/perl -w # decomp_xref_data : A decomp filter to crossref data references. # 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: # % gendump the-binary | decomp_xref_data the-binary > dump use Getopt::Long "GetOptions"; use FileHandle; use strict 'vars'; use vars '$VERSION', '$Verbose', '$OBJDUMP'; # full path to objdump program $OBJDUMP = "/usr/bin/objdump"; $VERSION = "1.0"; # version of this program $Verbose = 1; &parse_command_line_for_options(); &usage if (scalar @ARGV != 1); my $exec_filename = $ARGV[0]; # do the filtering &filter($exec_filename); exit 0; ############################### Filter Functions ############################## # ----------------------------------------------------------------------------- # Reads in data from stdin, performs the filter, then write result to stdout. # Params: $filename - filename of the executable file. # Returns: none sub filter($) { my ($filename) = @_; my ($line, $size, $fh, $length, $offset, $str, $start, @output); my ($ro_data_start, $ro_data_end, $ro_data_offset, $ro_data); my ($data_start, $data_end, $data_offset, $data); my ($bss_start, $bss_end); # get start and end offsets for the .rodata, .data, and .bss sections @output = `$OBJDUMP -h $filename`; foreach $line (@output) { if ($line =~ /.rodata/) { $size = oct('0x' . substr($line, 18, 8)); $ro_data_start = oct('0x' . substr($line, 38, 8)); $ro_data_offset = oct('0x' . substr($line, 48, 8)); $ro_data_end = $ro_data_start + $size; } if ($line =~ /.data/) { $size = oct('0x' . substr($line, 18, 8)); $data_start = oct('0x' . substr($line, 38, 8)); $data_offset = oct('0x' . substr($line, 48, 8)); $data_end = $data_start + $size; } if ($line =~ /.bss/) { $size = oct('0x' . substr($line, 18, 8)); $bss_start = oct('0x' . substr($line, 38, 8)); $bss_end = $bss_start + $size; } } # read in data for .rodata and .data sections $fh = new FileHandle; open $fh, "< $filename" or die "could not open file `$filename': $!"; seek $fh, $ro_data_offset, 0; read $fh, $ro_data, ($ro_data_end - $ro_data_start); seek $fh, $data_offset, 0; read $fh, $data, ($data_end - $data_start); close $fh; # do the filtering while ($line = ) { chomp($line); if ($line =~ /(0x8[0-9a-f]{6})/) { $offset = oct($1); if ($ro_data_start <= $offset and $offset < $ro_data_end) { $start = $offset - $ro_data_start; $length = index($ro_data, "\0", $start) - $start; $str = substr($ro_data, $start, $length); printf STDOUT ("\n# Possible reference to rodata '%s'\n", $str); } elsif ($data_start <= $offset and $offset < $data_end) { $start = $offset - $data_start; $length = index($data, "\0", $start) - $start; $str = substr($data, $start, $length); printf STDOUT ("\n# Possible reference to data '%s'\n", $str); } elsif ($bss_start <= $offset and $offset < $bss_end) { printf STDOUT ("\n# Possible reference to data in bss\n"); } } 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_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 cross reference data references. Reads in the current dump from stdin and writes the modified dump to stdout. Usage: $0 [options] executable file is the name of the executable file Options: -m, --leave-markers leave markers for delete object code in dump -V, --version outputs version information and exits -h, --help displays this help and exits _END exit 1; }