relro_test.sh 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. #!/bin/sh
  2. # relro_test.sh -- test -z relro
  3. # Copyright (C) 2010-2015 Free Software Foundation, Inc.
  4. # Written by Cary Coutant <ccoutant@google.com>.
  5. # This file is part of gold.
  6. # This program is free software; you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation; either version 3 of the License, or
  9. # (at your option) any later version.
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program; if not, write to the Free Software
  16. # Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
  17. # MA 02110-1301, USA.
  18. # This test checks that the PT_GNU_RELRO segment is properly
  19. # aligned and is coincident with the beginning of the data segment.
  20. # Cleans a hexadecimal number for input to dc.
  21. clean_hex()
  22. {
  23. echo "$1" | sed -e 's/0x//' -e 'y/abcdef/ABCDEF/'
  24. }
  25. check()
  26. {
  27. # Get the address and length of the PT_GNU_RELRO segment.
  28. RELRO_START=`grep GNU_RELRO "$1" | awk '{ print $3; }'`
  29. RELRO_LEN=`grep GNU_RELRO "$1" | awk '{ print $6; }'`
  30. if test -z "$RELRO_START"
  31. then
  32. echo "Did not find a PT_GNU_RELRO segment."
  33. exit 1
  34. fi
  35. # Get the address and alignment of the PT_LOAD segment whose address
  36. # matches the PT_GNU_RELRO segment.
  37. LOAD_ALIGN=`grep LOAD "$1" | awk -v A=$RELRO_START '$3 == A { print $NF; }'`
  38. LOAD_LEN=`grep LOAD "$1" | awk -v A=$RELRO_START '$3 == A { print $6; }'`
  39. if test -z "$LOAD_LEN"
  40. then
  41. echo "Did not find a PT_LOAD segment matching the PT_GNU_RELRO segment."
  42. exit 1
  43. fi
  44. # Compute the address of the end of the PT_GNU_RELRO segment,
  45. # modulo the alignment of the PT_LOAD segment.
  46. RELRO_START=`clean_hex "$RELRO_START"`
  47. RELRO_LEN=`clean_hex "$RELRO_LEN"`
  48. LOAD_ALIGN=`clean_hex "$LOAD_ALIGN"`
  49. RELRO_END=`echo "16o 16i $RELRO_START $RELRO_LEN + p" | dc`
  50. REM=`echo "16i $RELRO_END $LOAD_ALIGN % p" | dc`
  51. if test "$REM" -eq 0; then
  52. :
  53. else
  54. echo "PT_GNU_RELRO segment does not end at page boundary."
  55. exit 1
  56. fi
  57. }
  58. check relro_test.stdout