compare-lto 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. #! /bin/sh
  2. # Compare copies of two given object files.
  3. # Copyright (C) 2007, 2008, 2009, 2010, 2012 Free Software Foundation
  4. # Originally by Alexandre Oliva <aoliva@redhat.com>
  5. # Modified for LTO bootstrap by Richard Biener <rguenther@suse.de>
  6. # This file is part of GCC.
  7. # GCC is free software; you can redistribute it and/or modify it under
  8. # the terms of the GNU General Public License as published by the Free
  9. # Software Foundation; either version 3, or (at your option) any later
  10. # version.
  11. # GCC is distributed in the hope that it will be useful, but WITHOUT
  12. # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  13. # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
  14. # License for more details.
  15. # You should have received a copy of the GNU General Public License
  16. # along with GCC; see the file COPYING3. If not see
  17. # <http://www.gnu.org/licenses/>.
  18. rm='rm -f'
  19. case $1 in
  20. -p | --preserve)
  21. rm='echo preserving'
  22. shift
  23. ;;
  24. esac
  25. if test $# != 2; then
  26. echo 'usage: compare-lto file1 file2' >&2
  27. exit 1
  28. fi
  29. if test ! -f "$1"; then
  30. echo "$1" does not exist >&2
  31. exit 1
  32. fi
  33. if test ! -f "$2"; then
  34. echo "$2" does not exist >&2
  35. exit 1
  36. fi
  37. suf1=stripped
  38. while test -f "$1.$suf1"; do
  39. suf1=$suf1.
  40. done
  41. suf2=stripped
  42. while test -f "$2.$suf2"; do
  43. suf2=$suf2.
  44. done
  45. trap 'rm -f "$1.$suf1" "$2.$suf2"' 0 1 2 15
  46. if cmp "$1" "$2"; then
  47. status=0
  48. else
  49. status=1
  50. cmd=
  51. for t in objdump readelf eu-readelf; do
  52. if ($t --help) 2>&1 | grep ' --\[*section-\]*headers' > /dev/null; then
  53. cmd=$t
  54. break
  55. fi
  56. done
  57. # If there are LTO option sections, try to strip them off.
  58. if test "x$cmd" = "x" ||
  59. $cmd --section-headers "$1" | grep '.gnu.lto_.opts' > /dev/null ||
  60. $cmd --section-headers "$2" | grep '.gnu.lto_.opts' > /dev/null ; then
  61. echo stripping off LTO option section, then retrying >&2
  62. seclist=".gnu.lto_.opts"
  63. rsopts=`for sec in $seclist; do echo " --remove-section $sec"; done`
  64. if (objcopy -v) 2>&1 | grep ' --remove-section' > /dev/null; then
  65. objcopy $rsopts "$1" "$1.$suf1"
  66. objcopy $rsopts "$2" "$2.$suf2"
  67. elif (strip --help) 2>&1 | grep ' --remove-section' > /dev/null; then
  68. cp "$1" "$1.$suf1"
  69. strip $rsopts "$1.$suf1"
  70. cp "$2" "$2.$suf2"
  71. strip $rsopts "$2.$suf2"
  72. else
  73. echo failed to strip off LTO option section >&2
  74. fi
  75. trap 'rm -f "$1.$suf1" "$2.$suf2"' 0 1 2 15
  76. if cmp "$1.$suf1" "$2.$suf2"; then
  77. status=0
  78. else
  79. status=1
  80. fi
  81. # PE-COFF executables are timestamped so skip leading bytes for them.
  82. else
  83. case "$1" in
  84. *.exe)
  85. if cmp -i 256 "$1" "$2"; then
  86. status=0
  87. else
  88. status=1
  89. fi
  90. ;;
  91. *)
  92. if test -f "$1.exe" && cmp -i 256 "$1.exe" "$2.exe"; then
  93. status=0
  94. else
  95. status=1
  96. fi
  97. ;;
  98. esac
  99. fi
  100. fi
  101. $rm "$1.$suf1" "$2.$suf2"
  102. trap "exit $status; exit" 0 1 2 15
  103. exit $status