summaryrefslogtreecommitdiff
path: root/lstadjust.pl
blob: e2e395fed6de00f29b6c45f8f5b3a182b102e272 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/usr/bin/perl
#
# Take a NASM list and map file and make the offsets in the list file
# absolute.  This makes debugging a lot easier.
#
# Usage:
#
#  lstadjust.pl listfile mapfile outfile
#

($listfile, $mapfile, $outfile) = @ARGV;

open(LST, "< $listfile\0")
    or die "$0: cannot open: $listfile: $!\n";
open(MAP, "< $mapfile\0")
    or die "$0: cannot open: $mapfile: $!\n";
open(OUT, "> $outfile\0")
    or die "$0: cannot create: $outfile: $!\n";

%vstart = ();
undef $sec;

while (defined($line = <MAP>)) {
    chomp $line;
    if ($line =~ /^\-+\s+Section\s+(\S+)\s+\-/) {
	$sec = $1;
    }
    next unless (defined($sec));
    if ($line =~ /^vstart:\s+([0-9a-f]+)/i) {
	$vstart{$sec} = hex $1;
    }
}
close(MAP);

$offset = 0;
@ostack = ();

while (defined($line = <LST>)) {
    chomp $line;
    
    $source = substr($line, 40);
    if ($source =~ /^([^;]*);/) {
	$source = $1;
    }

    ($label, $op, $arg, $tail) = split(/\s+/, $source);
    if ($op =~ /^(|\[)section$/i) {
	$offset = $vstart{$arg};
    } elsif ($op =~ /^(absolute|\[absolute)$/i) {
	$offset = 0;
    } elsif ($op =~ /^struc$/i) {
	push(@ostack, $offset);
	$offset = 0;
    } elsif ($op =~ /^endstruc$/i) {
	$offset = pop(@ostack);
    }

    if ($line =~ /^(\s*[0-9]+ )([0-9A-F]{8})(\s.*)$/) {
	$line = sprintf("%s%08X%s", $1, (hex $2)+$offset, $3);
    }

    print OUT $line, "\n";
}