2 # Wrapper around gcc to tweak the output in various ways when running
5 # Copyright (C) 2010-2024 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.
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.
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/>.
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.
28 # bash$ cd $objdir/gdb/testsuite
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++"
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.)
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 # -c make an index (currently .gdb_index) in a cache dir
47 # -n make a dwarf5 index (.debug_names)
48 # -p create .dwp files (Fission), you need to also use gcc option -gsplit-dwarf
49 # -l creates separate debuginfo files linked to using .gnu_debuglink
50 # If nothing is given, no changes are made
52 myname
=cc-with-tweaks.sh
59 GDB
="./gdb -data-directory data-directory"
62 GDB
="../gdb -data-directory ../data-directory"
65 GDB
="../../gdb -data-directory ../../data-directory"
67 echo "$myname: unable to find usable gdb" >&2
72 OBJCOPY
=${OBJCOPY:-objcopy}
73 READELF
=${READELF:-readelf}
78 # shellcheck disable=SC2206 # Allow word splitting.
79 STRIP_ARGS_STRIP_DEBUG
=(${STRIP_ARGS_STRIP_DEBUG:---strip-debug})
80 # shellcheck disable=SC2206 # Allow word splitting.
81 STRIP_ARGS_KEEP_DEBUG
=(${STRIP_ARGS_KEEP_DEBUG:---only-keep-debug})
84 next_is_output_file
=no
89 want_index_cache
=false
93 want_objcopy_compress
=false
94 want_gnu_debuglink
=false
96 while [ $# -gt 0 ]; do
98 -Z) want_objcopy_compress
=true
;;
100 -i) want_index
=true
;;
101 -n) want_index
=true
; index_options
=-dwarf-5;;
102 -c) want_index_cache
=true
;;
103 -m) want_multi
=true
;;
105 -l) want_gnu_debuglink
=true
;;
111 if [ "$want_index" = true
]
113 if [ -z "$GDB_ADD_INDEX" ]
115 if [ -f "$mydir/gdb-add-index.sh" ]
117 GDB_ADD_INDEX
="$mydir/gdb-add-index.sh"
119 echo "$myname: unable to find usable contrib/gdb-add-index.sh" >&2
127 if [ "$next_is_output_file" = "yes" ]
130 next_is_output_file
=no
134 # Poor man's gcc argument parser.
135 # We don't need to handle all arguments, we just need to know if we're
136 # doing a link and what the output file is.
137 # It's not perfect, but it seems to work well enough for the task at hand.
139 "-c") have_link
=no
;;
140 "-E") have_link
=no
;;
141 "-S") have_link
=no
;;
142 "-o") next_is_output_file
=yes ;;
146 if [ "$next_is_output_file" = "yes" ]
148 echo "$myname: Unable to find output file" >&2
152 if [ "$have_link" = "no" ]
158 output_dir
="${output_file%/*}"
159 [ "$output_dir" = "$output_file" ] && output_dir
="."
163 [ $rc != 0 ] && exit $rc
164 if [ ! -f "$output_file" ]
166 echo "$myname: Internal error: $output_file missing." >&2
173 if [ "$subdir" = "" ]; then
177 tmpdir
=$
(dirname "$output_file")/"$subdir"
181 if [ "$want_objcopy_compress" = true
]; then
182 $OBJCOPY --compress-debug-sections "$output_file"
184 [ $rc != 0 ] && exit $rc
187 if [ "$want_index" = true
]; then
189 mv "$output_file" "$tmpdir"
190 output_dir
=$
(dirname "$output_file")
192 # Copy .dwo file alongside, to fix gdb.dwarf2/fission-relative-dwo.exp.
193 # Use copy instead of move to not break
194 # rtf=gdb.dwarf2/fission-absolute-dwo.exp.
195 dwo_pattern
="$output_dir/*.dwo"
196 for f
in $dwo_pattern; do
197 if [ "$f" = "$dwo_pattern" ]; then
203 tmpfile
=$tmpdir/$
(basename "$output_file")
204 # Filter out these messages which would stop dejagnu testcase run:
205 # echo "$myname: No index was created for $file" 1>&2
206 # echo "$myname: [Was there no debuginfo? Was there already an index?]" 1>&2
207 GDB
=$GDB $GDB_ADD_INDEX $index_options "$tmpfile" 2>&1 \
208 |
grep -v "^${GDB_ADD_INDEX##*/}: " >&2
210 mv "$tmpfile" "$output_file"
211 rm -f "$tmpdir"/*.dwo
212 [ "$rc" != 0 ] && exit "$rc"
215 if [ "$want_index_cache" = true
]; then
217 -ex "set index-cache directory $INDEX_CACHE_DIR" \
218 -ex "set index-cache enabled on" \
219 -ex "file $output_file"
221 [ $rc != 0 ] && exit $rc
224 if [ "$want_dwz" = true
] ||
[ "$want_multi" = true
]; then
225 # Require dwz version with PR dwz/24468 fixed.
226 dwz_version_major_required
=0
227 dwz_version_minor_required
=13
228 dwz_version_line
=$
($DWZ --version 2>&1 |
head -n 1)
229 dwz_version
=${dwz_version_line//dwz version /}
230 dwz_version_major
=${dwz_version//\.*/}
231 dwz_version_minor
=${dwz_version//*\./}
232 if [ "$dwz_version_major" -lt "$dwz_version_major_required" ] \
233 ||
{ [ "$dwz_version_major" -eq "$dwz_version_major_required" ] \
234 && [ "$dwz_version_minor" -lt "$dwz_version_minor_required" ]; }; then
235 detected
="$dwz_version_major.$dwz_version_minor"
236 required
="$dwz_version_major_required.$dwz_version_minor_required"
237 echo "$myname: dwz version $detected detected, version $required or higher required"
242 if [ "$want_dwz" = true
]; then
243 # Validate dwz's result by checking if the executable was modified.
244 cp "$output_file" "${output_file}.copy"
245 $DWZ "$output_file" > /dev
/null
246 cmp "$output_file" "$output_file.copy" > /dev
/null
248 rm -f "${output_file}.copy"
252 echo "$myname: dwz did not modify ${output_file}."
256 # File was modified, great.
259 # Other cmp error, it presumably has already printed something on
264 elif [ "$want_multi" = true
]; then
266 dwz_file
=$tmpdir/$
(basename "$output_file").dwz
267 # Remove the dwz output file if it exists, so we don't mistake it for a
268 # new file in case dwz fails.
271 cp "$output_file" "${output_file}.alt"
272 $DWZ -m "$dwz_file" "$output_file" "${output_file}.alt" > /dev
/null
273 rm -f "${output_file}.alt"
275 # Validate dwz's work by checking if the expected output file exists.
276 if [ ! -f "$dwz_file" ]; then
277 echo "$myname: dwz file $dwz_file missing."
282 if [ "$want_dwp" = true
]; then
283 mapfile
-t dwo_files \
285 <($READELF -wi "${output_file}" \
287 |
sed -e 's/^.*: //' \
291 if [ ${#dwo_files[@]} -ne 0 ]; then
292 $DWP -o "${output_file}.dwp" "${dwo_files[@]}" > /dev
/null
294 [ $rc != 0 ] && exit $rc
295 rm -f "${dwo_files[@]}"
299 if [ "$want_gnu_debuglink" = true
]; then
300 # Based on gdb_gnu_strip_debug.
302 # Gdb looks for the .gnu_debuglink file in the .debug subdirectory
303 # of the directory of the executable.
306 stripped_file
="$tmpdir"/$
(basename "$output_file").stripped
307 debug_file
="$tmpdir"/$
(basename "$output_file").debug
309 # Create stripped and debug versions of output_file.
310 strip
"${STRIP_ARGS_STRIP_DEBUG[@]}" "${output_file}" \
311 -o "${stripped_file}"
313 [ $rc != 0 ] && exit $rc
314 strip
"${STRIP_ARGS_KEEP_DEBUG[@]}" "${output_file}" \
317 [ $rc != 0 ] && exit $rc
319 # The .gnu_debuglink is supposed to contain no leading directories.
320 link
=$
(basename "${debug_file}")
323 # Temporarily cd to tmpdir to allow objcopy to find $link
324 cd "$tmpdir" ||
exit 1
326 # Overwrite output_file with stripped version containing
327 # .gnu_debuglink to debug_file.
328 $OBJCOPY --add-gnu-debuglink="$link" "${stripped_file}" \
332 [ $rc != 0 ] && exit $rc