Merge commit 'origin/master' into nolen
[profile/ivi/syslinux.git] / checkov.pl
1 #!/usr/bin/perl
2 #
3 # checkov.pl
4 #
5 # Check NASM map output for overflow
6 #
7 # This assumes that a section for which start != vstart, both
8 # ranges need to be checked for overflow (true for SYSLINUX)
9 #
10
11 ($in, $target) = @ARGV;
12
13 sub overlap($$$$) {
14     my($s1,$e1,$s2,$e2) = @_;
15
16     return 1 if ( $s2 < $e1 && $e2 > $s1 );
17     return 1 if ( $s1 < $e2 && $e1 > $s2 );
18
19     return 0;
20 }
21
22 open(IN, '<', $in) or die "$0: Cannot open input file: $in\n";
23
24 $section = undef;
25 while ( $line = <IN> ) {
26     if ( $line =~ /^-/ ) {
27         if ( $line =~ /^\-\-\-\- Section (\S+) / ) {
28             $section = $1;
29         } else {
30             $section = undef;
31         }
32     } elsif ( defined($section) ) {
33         if ( $line =~ /^length\:\s*(\S+)/ ) {
34             $length{$section} = hex $1;
35         } elsif ( $line =~ /^start\:\s*(\S+)/ ) {
36             $start{$section} = hex $1;
37         } elsif ( $line =~ /^vstart\:\s*(\S+)/ ) {
38             $vstart{$section} = hex $1;
39         }
40     }
41 }
42 close(IN);
43
44 $err = 0;
45
46 foreach $s ( keys(%start) ) {
47     $sstart  = $start{$s};
48     $svstart = $vstart{$s};
49     $send    = $sstart + $length{$s};
50     $svend   = $svstart + $length{$s};
51
52     if ( $send > 0x10000 || $svend > 0x10000 ) {
53         print STDERR "$target: 16-bit overflow on section $s\n";
54         $err++;
55     }
56
57     foreach $o ( keys(%start) ) {
58         next if ( $s ge $o );
59
60         $ostart  = $start{$o};
61         $ovstart = $vstart{$o};
62         $oend    = $ostart + $length{$o};
63         $ovend   = $ovstart + $length{$o};
64
65         if ( overlap($sstart, $send, $ostart, $oend) ||
66              overlap($svstart, $svend, $ostart, $oend) ||
67              overlap($sstart, $send, $ovstart, $ovend) ||
68              overlap($svstart, $svend, $ovstart, $ovend) ) {
69             print STDERR "$target: section $s overlaps section $o\n";
70             $err++;
71         }
72     }
73 }
74
75 if ( $err ) {
76     unlink($target);
77     exit(1);
78 } else {
79     exit(0);
80 }