1#!/bin/sh
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 | sh calc_run_size.sh
8
9NUM='\([0-9a-fA-F]*[ \t]*\)'
10OUT=$(sed -n 's/^[ \t0-9]*.b[sr][sk][ \t]*'"$NUM$NUM$NUM$NUM"'.*/\1\4/p')
11if [ -z "$OUT" ] ; then
12	echo "Never found .bss or .brk file offset" >&2
13	exit 1
14fi
15
16OUT=$(echo ${OUT# })
17sizeA=$(printf "%d" 0x${OUT%% *})
18OUT=${OUT#* }
19offsetA=$(printf "%d" 0x${OUT%% *})
20OUT=${OUT#* }
21sizeB=$(printf "%d" 0x${OUT%% *})
22OUT=${OUT#* }
23offsetB=$(printf "%d" 0x${OUT%% *})
24
25run_size=$(( $offsetA + $sizeA + $sizeB ))
26
27# BFD linker shows the same file offset in ELF.
28if [ "$offsetA" -ne "$offsetB" ] ; then
29	# Gold linker shows them as consecutive.
30	endB=$(( $offsetB + $sizeB ))
31	if [ "$endB" != "$run_size" ] ; then
32		printf "sizeA: 0x%x\n" $sizeA >&2
33		printf "offsetA: 0x%x\n" $offsetA >&2
34		printf "sizeB: 0x%x\n" $sizeB >&2
35		printf "offsetB: 0x%x\n" $offsetB >&2
36		echo ".bss and .brk are non-contiguous" >&2
37		exit 1
38	fi
39fi
40
41printf "%d\n" $run_size
42exit 0
43