strreplace: clean up and simplify
[profile/ivi/syslinux.git] / core / lstadjust.pl
1 #!/usr/bin/perl
2 #
3 # Take a NASM list and map file and make the offsets in the list file
4 # absolute.  This makes debugging a lot easier.
5 #
6 # Usage:
7 #
8 #  lstadjust.pl listfile mapfile outfile
9 #
10
11 ($listfile, $mapfile, $outfile) = @ARGV;
12
13 open(LST, "< $listfile\0")
14     or die "$0: cannot open: $listfile: $!\n";
15 open(MAP, "< $mapfile\0")
16     or die "$0: cannot open: $mapfile: $!\n";
17 open(OUT, "> $outfile\0")
18     or die "$0: cannot create: $outfile: $!\n";
19
20 %vstart = ();
21
22 while (defined($line = <MAP>)) {
23     if ($line =~ /^\s*([0-9]+)\s+(\S+)\s+([0-9a-f]+)\s+([0-9a-f]+)\s+([0-9a-f]+)\s+([0-9a-f]+)\s+2\*\*([0-9]+)/i) {
24         $vstart{$2} = hex $4;
25     }
26 }
27 close(MAP);
28
29 $offset = 0;
30 @ostack = ();
31
32 while (defined($line = <LST>)) {
33     chomp $line;
34
35     $source = substr($line, 40);
36     if ($source =~ /^([^;]*);/) {
37         $source = $1;
38     }
39
40     ($label, $op, $arg, $tail) = split(/\s+/, $source);
41     if ($op =~ /^(|\[)section$/i) {
42         $offset = $vstart{$arg};
43     } elsif ($op =~ /^(absolute|\[absolute)$/i) {
44         $offset = 0;
45     } elsif ($op =~ /^struc$/i) {
46         push(@ostack, $offset);
47         $offset = 0;
48     } elsif ($op =~ /^endstruc$/i) {
49         $offset = pop(@ostack);
50     }
51
52     if ($line =~ /^(\s*[0-9]+ )([0-9A-F]{8})(\s.*)$/) {
53         $line = sprintf("%s%08X%s", $1, (hex $2)+$offset, $3);
54     }
55
56     print OUT $line, "\n";
57 }