xref: /netbsd-src/external/gpl3/gcc/dist/contrib/compare-lto (revision fb8a8121f28072308659629b86cfb7c449bd93e1)
1181254a7Smrg#! /bin/sh
2181254a7Smrg
3181254a7Smrg# Compare copies of two given object files.
4181254a7Smrg
5181254a7Smrg# Copyright (C) 2007, 2008, 2009, 2010, 2012 Free Software Foundation
6181254a7Smrg# Originally by Alexandre Oliva <aoliva@redhat.com>
7181254a7Smrg# Modified for LTO bootstrap by Richard Biener <rguenther@suse.de>
8181254a7Smrg
9181254a7Smrg# This file is part of GCC.
10181254a7Smrg
11181254a7Smrg# GCC is free software; you can redistribute it and/or modify it under
12181254a7Smrg# the terms of the GNU General Public License as published by the Free
13181254a7Smrg# Software Foundation; either version 3, or (at your option) any later
14181254a7Smrg# version.
15181254a7Smrg
16181254a7Smrg# GCC is distributed in the hope that it will be useful, but WITHOUT
17181254a7Smrg# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
18181254a7Smrg# or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
19181254a7Smrg# License for more details.
20181254a7Smrg
21181254a7Smrg# You should have received a copy of the GNU General Public License
22181254a7Smrg# along with GCC; see the file COPYING3.  If not see
23181254a7Smrg# <http://www.gnu.org/licenses/>.
24181254a7Smrg
25181254a7Smrgrm='rm -f'
26181254a7Smrg
27181254a7Smrgcase $1 in
28181254a7Smrg-p | --preserve)
29181254a7Smrg  rm='echo preserving'
30181254a7Smrg  shift
31181254a7Smrg  ;;
32181254a7Smrgesac
33181254a7Smrg
34181254a7Smrgif test $# != 2; then
35*fb8a8121Smrg  echo 'usage: compare-lto file1 file2' >&2
36181254a7Smrg  exit 1
37181254a7Smrgfi
38181254a7Smrg
39181254a7Smrgif test ! -f "$1"; then
40181254a7Smrg  echo "$1" does not exist >&2
41181254a7Smrg  exit 1
42181254a7Smrgfi
43181254a7Smrg
44181254a7Smrgif test ! -f "$2"; then
45181254a7Smrg  echo "$2" does not exist >&2
46181254a7Smrg  exit 1
47181254a7Smrgfi
48181254a7Smrg
49181254a7Smrgsuf1=stripped
50181254a7Smrgwhile test -f "$1.$suf1"; do
51181254a7Smrg  suf1=$suf1.
52181254a7Smrgdone
53181254a7Smrg
54181254a7Smrgsuf2=stripped
55181254a7Smrgwhile test -f "$2.$suf2"; do
56181254a7Smrg  suf2=$suf2.
57181254a7Smrgdone
58181254a7Smrg
59181254a7Smrgtrap 'rm -f "$1.$suf1" "$2.$suf2"' 0 1 2 15
60181254a7Smrg
61181254a7Smrgif cmp "$1" "$2"; then
62181254a7Smrg  status=0
63181254a7Smrgelse
64181254a7Smrg  status=1
65181254a7Smrg
66181254a7Smrg  cmd=
67181254a7Smrg  for t in objdump readelf eu-readelf; do
68181254a7Smrg    if ($t --help) 2>&1 | grep ' --\[*section-\]*headers' > /dev/null; then
69181254a7Smrg      cmd=$t
70181254a7Smrg      break
71181254a7Smrg    fi
72181254a7Smrg  done
73181254a7Smrg
74181254a7Smrg  # If there are LTO option sections, try to strip them off.
75181254a7Smrg  if test "x$cmd" = "x" ||
76181254a7Smrg     $cmd --section-headers "$1" | grep '.gnu.lto_.opts' > /dev/null ||
77181254a7Smrg     $cmd --section-headers "$2" | grep '.gnu.lto_.opts' > /dev/null ; then
78181254a7Smrg
79181254a7Smrg    echo stripping off LTO option section, then retrying >&2
80181254a7Smrg
81181254a7Smrg    seclist=".gnu.lto_.opts"
82181254a7Smrg    rsopts=`for sec in $seclist; do echo " --remove-section $sec"; done`
83181254a7Smrg
84181254a7Smrg    if (objcopy -v) 2>&1 | grep ' --remove-section' > /dev/null; then
85181254a7Smrg      objcopy $rsopts "$1" "$1.$suf1"
86181254a7Smrg      objcopy $rsopts "$2" "$2.$suf2"
87181254a7Smrg    elif (strip --help) 2>&1 | grep ' --remove-section' > /dev/null; then
88181254a7Smrg      cp "$1" "$1.$suf1"
89181254a7Smrg      strip $rsopts "$1.$suf1"
90181254a7Smrg
91181254a7Smrg      cp "$2" "$2.$suf2"
92181254a7Smrg      strip $rsopts "$2.$suf2"
93181254a7Smrg    else
94181254a7Smrg      echo failed to strip off LTO option section >&2
95181254a7Smrg    fi
96181254a7Smrg
97181254a7Smrg    trap 'rm -f "$1.$suf1" "$2.$suf2"' 0 1 2 15
98181254a7Smrg
99181254a7Smrg    if cmp "$1.$suf1" "$2.$suf2"; then
100181254a7Smrg      status=0
101181254a7Smrg    else
102181254a7Smrg      status=1
103181254a7Smrg    fi
104*fb8a8121Smrg
105*fb8a8121Smrg  # PE-COFF executables are timestamped so skip leading bytes for them.
106*fb8a8121Smrg  else
107*fb8a8121Smrg    case "$1" in
108*fb8a8121Smrg      *.exe)
109*fb8a8121Smrg        if cmp -i 256 "$1" "$2"; then
110*fb8a8121Smrg          status=0
111*fb8a8121Smrg        else
112*fb8a8121Smrg          status=1
113*fb8a8121Smrg        fi
114*fb8a8121Smrg        ;;
115*fb8a8121Smrg      *)
116*fb8a8121Smrg        if test -f "$1.exe" && cmp -i 256 "$1.exe" "$2.exe"; then
117*fb8a8121Smrg          status=0
118*fb8a8121Smrg        else
119*fb8a8121Smrg          status=1
120*fb8a8121Smrg        fi
121*fb8a8121Smrg        ;;
122*fb8a8121Smrg    esac
123181254a7Smrg  fi
124181254a7Smrgfi
125181254a7Smrg
126181254a7Smrg$rm "$1.$suf1" "$2.$suf2"
127181254a7Smrg
128181254a7Smrgtrap "exit $status; exit" 0 1 2 15
129181254a7Smrg
130181254a7Smrgexit $status
131