Move mboot documentation to the doc/ directory
[profile/ivi/syslinux.git] / 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 undef $sec;
22
23 while (defined($line = <MAP>)) {
24     chomp $line;
25     if ($line =~ /^\-+\s+Section\s+(\S+)\s+\-/) {
26         $sec = $1;
27     }
28     next unless (defined($sec));
29     if ($line =~ /^vstart:\s+([0-9a-f]+)/i) {
30         $vstart{$sec} = hex $1;
31     }
32 }
33 close(MAP);
34
35 $offset = 0;
36 @ostack = ();
37
38 while (defined($line = <LST>)) {
39     chomp $line;
40
41     $source = substr($line, 40);
42     if ($source =~ /^([^;]*);/) {
43         $source = $1;
44     }
45
46     ($label, $op, $arg, $tail) = split(/\s+/, $source);
47     if ($op =~ /^(|\[)section$/i) {
48         $offset = $vstart{$arg};
49     } elsif ($op =~ /^(absolute|\[absolute)$/i) {
50         $offset = 0;
51     } elsif ($op =~ /^struc$/i) {
52         push(@ostack, $offset);
53         $offset = 0;
54     } elsif ($op =~ /^endstruc$/i) {
55         $offset = pop(@ostack);
56     }
57
58     if ($line =~ /^(\s*[0-9]+ )([0-9A-F]{8})(\s.*)$/) {
59         $line = sprintf("%s%08X%s", $1, (hex $2)+$offset, $3);
60     }
61
62     print OUT $line, "\n";
63 }