calc_run_size.sh 1.0 KB

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