2009-09-09 Richard Guenther <rguenther@suse.de>
[official-gcc.git] / contrib / compare-debug
blob6f2b4abfb5c907b650ffc860ba4ac6a886d01eb6
1 #! /bin/sh
3 # Compare stripped copies of two given object files.
5 # Copyright (C) 2007, 2008, 2009 Free Software Foundation
6 # Originally by Alexandre Oliva <aoliva@redhat.com>
8 # This file is part of GCC.
10 # GCC is free software; you can redistribute it and/or modify it under
11 # the terms of the GNU General Public License as published by the Free
12 # Software Foundation; either version 3, or (at your option) any later
13 # version.
15 # GCC is distributed in the hope that it will be useful, but WITHOUT
16 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
17 # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
18 # License for more details.
20 # You should have received a copy of the GNU General Public License
21 # along with GCC; see the file COPYING3. If not see
22 # <http://www.gnu.org/licenses/>.
24 rm='rm -f'
26 case $1 in
27 -p | --preserve)
28 rm='echo preserving'
29 shift
31 esac
33 if test $# != 2; then
34 echo 'usage: compare-debug file1.o file2.o' >&2
35 exit 1
38 if test ! -f "$1"; then
39 echo "$1" does not exist >&2
40 exit 1
43 if test ! -f "$2"; then
44 echo "$2" does not exist >&2
45 exit 1
48 suf1=stripped
49 while test -f "$1.$suf1"; do
50 suf1=$suf1.
51 done
53 suf2=stripped
54 while test -f "$2.$suf2"; do
55 suf2=$suf2.
56 done
58 trap 'rm -f "$1.$suf1" "$2.$suf2"' 0 1 2 15
60 cp "$1" "$1.$suf1"
61 strip "$1.$suf1"
63 cp "$2" "$2.$suf2"
64 strip "$2.$suf2"
66 if cmp "$1.$suf1" "$2.$suf2"; then
67 status=0
68 else
69 status=1
71 # Assembler-generated CFI will add an .eh_frame section for -g not
72 # present in -g0. Try to cope with it by checking that an .eh_frame
73 # section is present in either object file, and then stripping it
74 # off before re-comparing.
76 cmd=
77 cmp1=
78 cmp2=
80 for t in objdump readelf eu-readelf; do
81 if ($t --help) 2>&1 | grep ' --\[*section-\]*headers' > /dev/null; then
82 cmd=$t
84 $cmd --section-headers "$1.$suf1" | grep '\.eh_frame' > /dev/null
85 cmp1=$?
87 $cmd --section-headers "$2.$suf2" | grep '\.eh_frame' > /dev/null
88 cmp2=$?
90 break
92 done
94 # If we found .eh_frame in one but not the other, or if we could not
95 # find a command to tell, try to strip off the .eh_frame section
96 # from both.
97 if test "x$cmp1" != "x$cmp2" || test "x$cmd" = "x"; then
98 suf3=$suf1.
99 while test -f "$1.$suf3"; do
100 suf3=$suf3.
101 done
103 suf4=$suf2.
104 while test -f "$2.$suf4"; do
105 suf4=$suf4.
106 done
108 trap 'rm -f "$1.$suf1" "$2.$suf2" "$1.$suf3" "$2.$suf4"' 0 1 2 15
110 echo stripping off .eh_frame, then retrying >&2
112 if (objcopy -v) 2>&1 | grep ' --remove-section' > /dev/null; then
113 objcopy --remove-section .eh_frame --remove-section .rel.eh_frame --remove-section .rela.eh_frame "$1.$suf1" "$1.$suf3"
114 mv "$1.$suf3" "$1.$suf1"
116 objcopy --remove-section .eh_frame --remove-section .rel.eh_frame --remove-section .rela.eh_frame "$2.$suf2" "$2.$suf4"
117 mv "$2.$suf4" "$2.$suf2"
118 elif (strip --help) 2>&1 | grep ' --remove-section' > /dev/null; then
119 cp "$1.$suf1" "$1.$suf3"
120 strip --remove-section .eh_frame --remove-section .rel.eh_frame --remove-section .rela.eh_frame "$1.$suf3"
121 mv "$1.$suf3" "$1.$suf1"
123 cp "$2.$suf2" "$2.$suf4"
124 strip --remove-section .eh_frame --remove-section .rel.eh_frame --remove-section .rela.eh_frame "$2.$suf4"
125 mv "$2.$suf4" "$2.$suf2"
126 else
127 echo failed to strip off .eh_frame >&2
130 trap 'rm -f "$1.$suf1" "$2.$suf2"' 0 1 2 15
132 if cmp "$1.$suf1" "$2.$suf2"; then
133 status=0
134 else
135 status=1
140 $rm "$1.$suf1" "$2.$suf2"
142 trap "exit $status; exit" 0 1 2 15
144 if test -f "$1".gkd || test -f "$2".gkd; then
145 if cmp "$1".gkd "$2".gkd; then
147 else
148 status=$?
152 exit $status