x86, kaslr: Prevent .bss from overlaping initrd
[cascardo/linux.git] / arch / x86 / tools / calc_run_size.pl
1 #!/usr/bin/perl
2 #
3 # Calculate the amount of space needed to run the kernel, including room for
4 # the .bss and .brk sections.
5 #
6 # Usage:
7 # objdump -h a.out | perl calc_run_size.pl
8 use strict;
9
10 my $mem_size = 0;
11 my $file_offset = 0;
12
13 my $sections=" *[0-9]+ \.(?:bss|brk) +";
14 while (<>) {
15         if (/^$sections([0-9a-f]+) +(?:[0-9a-f]+ +){2}([0-9a-f]+)/) {
16                 my $size = hex($1);
17                 my $offset = hex($2);
18                 $mem_size += $size;
19                 if ($file_offset == 0) {
20                         $file_offset = $offset;
21                 } elsif ($file_offset != $offset) {
22                         die ".bss and .brk lack common file offset\n";
23                 }
24         }
25 }
26
27 if ($file_offset == 0) {
28         die "Never found .bss or .brk file offset\n";
29 }
30 printf("%d\n", $mem_size + $file_offset);