]>
Commit | Line | Data |
---|---|---|
6432ec65 | 1 | #!/usr/bin/env bash |
853254db TT |
2 | # Wrapper around gcc to tweak the output in various ways when running |
3 | # the testsuite. | |
ea65fe05 | 4 | |
1d506c26 | 5 | # Copyright (C) 2010-2024 Free Software Foundation, Inc. |
ea65fe05 DE |
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. | |
0b5574da DE |
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 | |
63b46266 YPK |
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". | |
ea65fe05 DE |
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 \ | |
6432ec65 JK |
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++" | |
ea65fe05 | 32 | # |
fb8cf7c5 DE |
33 | # For documentation on Fission and dwp files: |
34 | # http://gcc.gnu.org/wiki/DebugFission | |
35 | # http://gcc.gnu.org/wiki/DebugFissionDWP | |
ea65fe05 | 36 | # For documentation on index files: info -f gdb.info -n "Index Files" |
853254db TT |
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.) | |
ea65fe05 | 40 | |
853254db | 41 | # ARGS determine what is done. They can be: |
ea9f10bb | 42 | # -Z invoke objcopy --compress-debug-sections |
853254db TT |
43 | # -z compress using dwz |
44 | # -m compress using dwz -m | |
9d6d4be8 | 45 | # -i make an index (.gdb_index) |
f1b8ee6f | 46 | # -c make an index (currently .gdb_index) in a cache dir |
9d6d4be8 | 47 | # -n make a dwarf5 index (.debug_names) |
7fb2b84a | 48 | # -p create .dwp files (Fission), you need to also use gcc option -gsplit-dwarf |
778ae9cc | 49 | # -l creates separate debuginfo files linked to using .gnu_debuglink |
853254db TT |
50 | # If nothing is given, no changes are made |
51 | ||
52 | myname=cc-with-tweaks.sh | |
6432ec65 | 53 | mydir=`dirname "$0"` |
ea65fe05 DE |
54 | |
55 | if [ -z "$GDB" ] | |
56 | then | |
57 | if [ -f ./gdb ] | |
58 | then | |
63b46266 | 59 | GDB="./gdb -data-directory data-directory" |
ea65fe05 DE |
60 | elif [ -f ../gdb ] |
61 | then | |
63b46266 | 62 | GDB="../gdb -data-directory ../data-directory" |
b8e9bd6c DE |
63 | elif [ -f ../../gdb ] |
64 | then | |
63b46266 | 65 | GDB="../../gdb -data-directory ../../data-directory" |
ea65fe05 DE |
66 | else |
67 | echo "$myname: unable to find usable gdb" >&2 | |
68 | exit 1 | |
69 | fi | |
70 | fi | |
71 | ||
72 | OBJCOPY=${OBJCOPY:-objcopy} | |
80626a55 | 73 | READELF=${READELF:-readelf} |
ea65fe05 | 74 | |
853254db | 75 | DWZ=${DWZ:-dwz} |
80626a55 | 76 | DWP=${DWP:-dwp} |
853254db | 77 | |
fb799bba TV |
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}) | |
82 | ||
ea65fe05 DE |
83 | have_link=unknown |
84 | next_is_output_file=no | |
00e14314 | 85 | output_file=a.out |
ea65fe05 | 86 | |
853254db | 87 | want_index=false |
9d6d4be8 | 88 | index_options="" |
f1b8ee6f | 89 | want_index_cache=false |
853254db TT |
90 | want_dwz=false |
91 | want_multi=false | |
80626a55 | 92 | want_dwp=false |
ea9f10bb | 93 | want_objcopy_compress=false |
778ae9cc | 94 | want_gnu_debuglink=false |
853254db TT |
95 | |
96 | while [ $# -gt 0 ]; do | |
97 | case "$1" in | |
ea9f10bb | 98 | -Z) want_objcopy_compress=true ;; |
853254db TT |
99 | -z) want_dwz=true ;; |
100 | -i) want_index=true ;; | |
9d6d4be8 | 101 | -n) want_index=true; index_options=-dwarf-5;; |
f1b8ee6f | 102 | -c) want_index_cache=true ;; |
853254db | 103 | -m) want_multi=true ;; |
80626a55 | 104 | -p) want_dwp=true ;; |
778ae9cc | 105 | -l) want_gnu_debuglink=true ;; |
853254db TT |
106 | *) break ;; |
107 | esac | |
108 | shift | |
109 | done | |
110 | ||
6432ec65 JK |
111 | if [ "$want_index" = true ] |
112 | then | |
113 | if [ -z "$GDB_ADD_INDEX" ] | |
114 | then | |
115 | if [ -f $mydir/gdb-add-index.sh ] | |
116 | then | |
117 | GDB_ADD_INDEX="$mydir/gdb-add-index.sh" | |
118 | else | |
119 | echo "$myname: unable to find usable contrib/gdb-add-index.sh" >&2 | |
120 | exit 1 | |
121 | fi | |
122 | fi | |
123 | fi | |
124 | ||
ea65fe05 DE |
125 | for arg in "$@" |
126 | do | |
127 | if [ "$next_is_output_file" = "yes" ] | |
128 | then | |
129 | output_file="$arg" | |
130 | next_is_output_file=no | |
131 | continue | |
132 | fi | |
133 | ||
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. | |
138 | case "$arg" in | |
139 | "-c") have_link=no ;; | |
140 | "-E") have_link=no ;; | |
141 | "-S") have_link=no ;; | |
142 | "-o") next_is_output_file=yes ;; | |
143 | esac | |
144 | done | |
145 | ||
146 | if [ "$next_is_output_file" = "yes" ] | |
147 | then | |
148 | echo "$myname: Unable to find output file" >&2 | |
149 | exit 1 | |
150 | fi | |
151 | ||
152 | if [ "$have_link" = "no" ] | |
153 | then | |
154 | "$@" | |
155 | exit $? | |
156 | fi | |
157 | ||
ea65fe05 DE |
158 | output_dir="${output_file%/*}" |
159 | [ "$output_dir" = "$output_file" ] && output_dir="." | |
160 | ||
161 | "$@" | |
162 | rc=$? | |
163 | [ $rc != 0 ] && exit $rc | |
164 | if [ ! -f "$output_file" ] | |
165 | then | |
166 | echo "$myname: Internal error: $output_file missing." >&2 | |
167 | exit 1 | |
168 | fi | |
169 | ||
0df0352a TV |
170 | get_tmpdir () |
171 | { | |
778ae9cc TV |
172 | subdir="$1" |
173 | if [ "$subdir" = "" ]; then | |
174 | subdir=.tmp | |
175 | fi | |
176 | ||
177 | tmpdir=$(dirname "$output_file")/"$subdir" | |
0df0352a TV |
178 | mkdir -p "$tmpdir" |
179 | } | |
180 | ||
ea9f10bb TT |
181 | if [ "$want_objcopy_compress" = true ]; then |
182 | $OBJCOPY --compress-debug-sections "$output_file" | |
183 | rc=$? | |
184 | [ $rc != 0 ] && exit $rc | |
185 | fi | |
186 | ||
853254db | 187 | if [ "$want_index" = true ]; then |
f80cb3b4 TV |
188 | get_tmpdir |
189 | mv "$output_file" "$tmpdir" | |
a66b7a04 TV |
190 | output_dir=$(dirname "$output_file") |
191 | ||
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 | |
198 | break | |
199 | fi | |
200 | cp "$f" "$tmpdir" | |
201 | done | |
202 | ||
f80cb3b4 | 203 | tmpfile="$tmpdir/$(basename $output_file)" |
6432ec65 JK |
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 | |
f80cb3b4 | 207 | GDB=$GDB $GDB_ADD_INDEX $index_options "$tmpfile" 2>&1 \ |
9d6d4be8 | 208 | | grep -v "^${GDB_ADD_INDEX##*/}: " >&2 |
6432ec65 | 209 | rc=${PIPESTATUS[0]} |
f80cb3b4 | 210 | mv "$tmpfile" "$output_file" |
a66b7a04 | 211 | rm -f "$tmpdir"/*.dwo |
853254db TT |
212 | [ $rc != 0 ] && exit $rc |
213 | fi | |
214 | ||
f1b8ee6f TV |
215 | if [ "$want_index_cache" = true ]; then |
216 | $GDB -q -batch \ | |
217 | -ex "set index-cache directory $INDEX_CACHE_DIR" \ | |
218 | -ex "set index-cache enabled on" \ | |
219 | -ex "file $output_file" | |
220 | rc=$? | |
221 | [ $rc != 0 ] && exit $rc | |
222 | fi | |
223 | ||
3f62178e TV |
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" | |
238 | exit 1 | |
239 | fi | |
240 | fi | |
241 | ||
853254db | 242 | if [ "$want_dwz" = true ]; then |
a9eac7f9 SM |
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 | |
247 | cmp_rc=$? | |
248 | rm -f "${output_file}.copy" | |
249 | ||
250 | case $cmp_rc in | |
251 | 0) | |
252 | echo "$myname: dwz did not modify ${output_file}." | |
253 | exit 1 | |
254 | ;; | |
255 | 1) | |
256 | # File was modified, great. | |
257 | ;; | |
258 | *) | |
259 | # Other cmp error, it presumably has already printed something on | |
260 | # stderr. | |
261 | exit 1 | |
262 | ;; | |
263 | esac | |
853254db | 264 | elif [ "$want_multi" = true ]; then |
0df0352a TV |
265 | get_tmpdir |
266 | dwz_file=$tmpdir/$(basename "$output_file").dwz | |
a9eac7f9 SM |
267 | # Remove the dwz output file if it exists, so we don't mistake it for a |
268 | # new file in case dwz fails. | |
0df0352a | 269 | rm -f "$dwz_file" |
a9eac7f9 | 270 | |
853254db | 271 | cp $output_file ${output_file}.alt |
0df0352a | 272 | $DWZ -m "$dwz_file" "$output_file" ${output_file}.alt > /dev/null |
6892f601 | 273 | rm -f ${output_file}.alt |
a9eac7f9 SM |
274 | |
275 | # Validate dwz's work by checking if the expected output file exists. | |
0df0352a TV |
276 | if [ ! -f "$dwz_file" ]; then |
277 | echo "$myname: dwz file $dwz_file missing." | |
a9eac7f9 SM |
278 | exit 1 |
279 | fi | |
ea65fe05 DE |
280 | fi |
281 | ||
80626a55 DE |
282 | if [ "$want_dwp" = true ]; then |
283 | dwo_files=$($READELF -wi "${output_file}" | grep _dwo_name | \ | |
284 | sed -e 's/^.*: //' | sort | uniq) | |
20d7f211 DE |
285 | rc=0 |
286 | if [ -n "$dwo_files" ]; then | |
287 | $DWP -o "${output_file}.dwp" ${dwo_files} > /dev/null | |
288 | rc=$? | |
289 | [ $rc != 0 ] && exit $rc | |
290 | rm -f ${dwo_files} | |
291 | fi | |
80626a55 DE |
292 | fi |
293 | ||
778ae9cc TV |
294 | if [ "$want_gnu_debuglink" = true ]; then |
295 | # Based on gdb_gnu_strip_debug. | |
296 | ||
297 | # Gdb looks for the .gnu_debuglink file in the .debug subdirectory | |
298 | # of the directory of the executable. | |
299 | get_tmpdir .debug | |
300 | ||
301 | stripped_file="$tmpdir"/$(basename "$output_file").stripped | |
302 | debug_file="$tmpdir"/$(basename "$output_file").debug | |
303 | ||
304 | # Create stripped and debug versions of output_file. | |
fb799bba | 305 | strip "${STRIP_ARGS_STRIP_DEBUG[@]}" "${output_file}" \ |
778ae9cc TV |
306 | -o "${stripped_file}" |
307 | rc=$? | |
308 | [ $rc != 0 ] && exit $rc | |
fb799bba | 309 | strip "${STRIP_ARGS_KEEP_DEBUG[@]}" "${output_file}" \ |
778ae9cc TV |
310 | -o "${debug_file}" |
311 | rc=$? | |
312 | [ $rc != 0 ] && exit $rc | |
313 | ||
314 | # The .gnu_debuglink is supposed to contain no leading directories. | |
315 | link=$(basename "${debug_file}") | |
316 | ||
317 | ( | |
318 | # Temporarily cd to tmpdir to allow objcopy to find $link | |
319 | cd "$tmpdir" || exit 1 | |
320 | ||
321 | # Overwrite output_file with stripped version containing | |
322 | # .gnu_debuglink to debug_file. | |
33fb3d6f | 323 | $OBJCOPY --add-gnu-debuglink="$link" "${stripped_file}" \ |
778ae9cc TV |
324 | "${output_file}" |
325 | rc=$? | |
326 | [ $rc != 0 ] && exit $rc | |
327 | ) | |
328 | fi | |
329 | ||
ea65fe05 | 330 | exit $rc |