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