Automatic Copyright Year update after running gdb/copyright.py
[deliverable/binutils-gdb.git] / gdb / contrib / cc-with-tweaks.sh
1 #!/usr/bin/env bash
2 # Wrapper around gcc to tweak the output in various ways when running
3 # the testsuite.
4
5 # Copyright (C) 2010-2022 Free Software Foundation, Inc.
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 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
18
19 # This program requires gdb and objcopy in addition to gcc.
20 # The default values are gdb from the build tree and objcopy from $PATH.
21 # They may be overridden by setting environment variables GDB and OBJCOPY
22 # respectively. Note that GDB should contain the gdb binary as well as the
23 # -data-directory flag, e.g., "foo/gdb -data-directory foo/data-directory".
24 # We assume the current directory is either $obj/gdb or $obj/gdb/testsuite.
25 #
26 # Example usage:
27 #
28 # bash$ cd $objdir/gdb/testsuite
29 # bash$ runtest \
30 # CC_FOR_TARGET="/bin/bash $srcdir/gdb/contrib/cc-with-tweaks.sh ARGS gcc" \
31 # CXX_FOR_TARGET="/bin/bash $srcdir/gdb/contrib/cc-with-tweaks.sh ARGS g++"
32 #
33 # For documentation on Fission and dwp files:
34 # http://gcc.gnu.org/wiki/DebugFission
35 # http://gcc.gnu.org/wiki/DebugFissionDWP
36 # For documentation on index files: info -f gdb.info -n "Index Files"
37 # For information about 'dwz', see the announcement:
38 # http://gcc.gnu.org/ml/gcc/2012-04/msg00686.html
39 # (More documentation is to come.)
40
41 # ARGS determine what is done. They can be:
42 # -Z invoke objcopy --compress-debug-sections
43 # -z compress using dwz
44 # -m compress using dwz -m
45 # -i make an index (.gdb_index)
46 # -n make a dwarf5 index (.debug_names)
47 # -p create .dwp files (Fission), you need to also use gcc option -gsplit-dwarf
48 # -l creates separate debuginfo files linked to using .gnu_debuglink
49 # If nothing is given, no changes are made
50
51 myname=cc-with-tweaks.sh
52 mydir=`dirname "$0"`
53
54 if [ -z "$GDB" ]
55 then
56 if [ -f ./gdb ]
57 then
58 GDB="./gdb -data-directory data-directory"
59 elif [ -f ../gdb ]
60 then
61 GDB="../gdb -data-directory ../data-directory"
62 elif [ -f ../../gdb ]
63 then
64 GDB="../../gdb -data-directory ../../data-directory"
65 else
66 echo "$myname: unable to find usable gdb" >&2
67 exit 1
68 fi
69 fi
70
71 OBJCOPY=${OBJCOPY:-objcopy}
72 READELF=${READELF:-readelf}
73
74 DWZ=${DWZ:-dwz}
75 DWP=${DWP:-dwp}
76
77 have_link=unknown
78 next_is_output_file=no
79 output_file=a.out
80
81 want_index=false
82 index_options=""
83 want_dwz=false
84 want_multi=false
85 want_dwp=false
86 want_objcopy_compress=false
87 want_gnu_debuglink=false
88
89 while [ $# -gt 0 ]; do
90 case "$1" in
91 -Z) want_objcopy_compress=true ;;
92 -z) want_dwz=true ;;
93 -i) want_index=true ;;
94 -n) want_index=true; index_options=-dwarf-5;;
95 -m) want_multi=true ;;
96 -p) want_dwp=true ;;
97 -l) want_gnu_debuglink=true ;;
98 *) break ;;
99 esac
100 shift
101 done
102
103 if [ "$want_index" = true ]
104 then
105 if [ -z "$GDB_ADD_INDEX" ]
106 then
107 if [ -f $mydir/gdb-add-index.sh ]
108 then
109 GDB_ADD_INDEX="$mydir/gdb-add-index.sh"
110 else
111 echo "$myname: unable to find usable contrib/gdb-add-index.sh" >&2
112 exit 1
113 fi
114 fi
115 fi
116
117 for arg in "$@"
118 do
119 if [ "$next_is_output_file" = "yes" ]
120 then
121 output_file="$arg"
122 next_is_output_file=no
123 continue
124 fi
125
126 # Poor man's gcc argument parser.
127 # We don't need to handle all arguments, we just need to know if we're
128 # doing a link and what the output file is.
129 # It's not perfect, but it seems to work well enough for the task at hand.
130 case "$arg" in
131 "-c") have_link=no ;;
132 "-E") have_link=no ;;
133 "-S") have_link=no ;;
134 "-o") next_is_output_file=yes ;;
135 esac
136 done
137
138 if [ "$next_is_output_file" = "yes" ]
139 then
140 echo "$myname: Unable to find output file" >&2
141 exit 1
142 fi
143
144 if [ "$have_link" = "no" ]
145 then
146 "$@"
147 exit $?
148 fi
149
150 output_dir="${output_file%/*}"
151 [ "$output_dir" = "$output_file" ] && output_dir="."
152
153 "$@"
154 rc=$?
155 [ $rc != 0 ] && exit $rc
156 if [ ! -f "$output_file" ]
157 then
158 echo "$myname: Internal error: $output_file missing." >&2
159 exit 1
160 fi
161
162 get_tmpdir ()
163 {
164 subdir="$1"
165 if [ "$subdir" = "" ]; then
166 subdir=.tmp
167 fi
168
169 tmpdir=$(dirname "$output_file")/"$subdir"
170 mkdir -p "$tmpdir"
171 }
172
173 if [ "$want_objcopy_compress" = true ]; then
174 $OBJCOPY --compress-debug-sections "$output_file"
175 rc=$?
176 [ $rc != 0 ] && exit $rc
177 fi
178
179 if [ "$want_index" = true ]; then
180 get_tmpdir
181 mv "$output_file" "$tmpdir"
182 tmpfile="$tmpdir/$(basename $output_file)"
183 # Filter out these messages which would stop dejagnu testcase run:
184 # echo "$myname: No index was created for $file" 1>&2
185 # echo "$myname: [Was there no debuginfo? Was there already an index?]" 1>&2
186 GDB=$GDB $GDB_ADD_INDEX $index_options "$tmpfile" 2>&1 \
187 | grep -v "^${GDB_ADD_INDEX##*/}: " >&2
188 rc=${PIPESTATUS[0]}
189 mv "$tmpfile" "$output_file"
190 [ $rc != 0 ] && exit $rc
191 fi
192
193 if [ "$want_dwz" = true ]; then
194 # Validate dwz's result by checking if the executable was modified.
195 cp "$output_file" "${output_file}.copy"
196 $DWZ "$output_file" > /dev/null
197 cmp "$output_file" "$output_file.copy" > /dev/null
198 cmp_rc=$?
199 rm -f "${output_file}.copy"
200
201 case $cmp_rc in
202 0)
203 echo "$myname: dwz did not modify ${output_file}."
204 exit 1
205 ;;
206 1)
207 # File was modified, great.
208 ;;
209 *)
210 # Other cmp error, it presumably has already printed something on
211 # stderr.
212 exit 1
213 ;;
214 esac
215 elif [ "$want_multi" = true ]; then
216 get_tmpdir
217 dwz_file=$tmpdir/$(basename "$output_file").dwz
218 # Remove the dwz output file if it exists, so we don't mistake it for a
219 # new file in case dwz fails.
220 rm -f "$dwz_file"
221
222 cp $output_file ${output_file}.alt
223 $DWZ -m "$dwz_file" "$output_file" ${output_file}.alt > /dev/null
224 rm -f ${output_file}.alt
225
226 # Validate dwz's work by checking if the expected output file exists.
227 if [ ! -f "$dwz_file" ]; then
228 echo "$myname: dwz file $dwz_file missing."
229 exit 1
230 fi
231 fi
232
233 if [ "$want_dwp" = true ]; then
234 dwo_files=$($READELF -wi "${output_file}" | grep _dwo_name | \
235 sed -e 's/^.*: //' | sort | uniq)
236 rc=0
237 if [ -n "$dwo_files" ]; then
238 $DWP -o "${output_file}.dwp" ${dwo_files} > /dev/null
239 rc=$?
240 [ $rc != 0 ] && exit $rc
241 rm -f ${dwo_files}
242 fi
243 fi
244
245 if [ "$want_gnu_debuglink" = true ]; then
246 # Based on gdb_gnu_strip_debug.
247
248 # Gdb looks for the .gnu_debuglink file in the .debug subdirectory
249 # of the directory of the executable.
250 get_tmpdir .debug
251
252 stripped_file="$tmpdir"/$(basename "$output_file").stripped
253 debug_file="$tmpdir"/$(basename "$output_file").debug
254
255 # Create stripped and debug versions of output_file.
256 strip --strip-debug "${output_file}" \
257 -o "${stripped_file}"
258 rc=$?
259 [ $rc != 0 ] && exit $rc
260 strip --only-keep-debug "${output_file}" \
261 -o "${debug_file}"
262 rc=$?
263 [ $rc != 0 ] && exit $rc
264
265 # The .gnu_debuglink is supposed to contain no leading directories.
266 link=$(basename "${debug_file}")
267
268 (
269 # Temporarily cd to tmpdir to allow objcopy to find $link
270 cd "$tmpdir" || exit 1
271
272 # Overwrite output_file with stripped version containing
273 # .gnu_debuglink to debug_file.
274 objcopy --add-gnu-debuglink="$link" "${stripped_file}" \
275 "${output_file}"
276 rc=$?
277 [ $rc != 0 ] && exit $rc
278 )
279 fi
280
281 exit $rc
This page took 0.035111 seconds and 4 git commands to generate.