]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/contrib/gdb-add-index.sh
Automatic date update in version.in
[thirdparty/binutils-gdb.git] / gdb / contrib / gdb-add-index.sh
CommitLineData
caf26be9
SB
1#! /bin/sh
2
3# Add a .gdb_index section to a file.
4
d01e8234 5# Copyright (C) 2010-2025 Free Software Foundation, Inc.
caf26be9
SB
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 assumes gdb and objcopy are in $PATH.
20# If not, or you want others, pass the following in the environment
21GDB=${GDB:=gdb}
22OBJCOPY=${OBJCOPY:=objcopy}
2b9f6e89 23READELF=${READELF:=readelf}
caf26be9 24
3bed6861
SJ
25PKGVERSION="@PKGVERSION@"
26VERSION="@VERSION@"
71f193a5 27
caf26be9
SB
28myname="${0##*/}"
29
71f193a5
AB
30print_usage() {
31 prefix="Usage: $myname"
32 echo "$prefix [-h|--help] [-v|--version] [--dwarf-5] FILENAME"
33}
34
35print_try_help() {
36 echo "Try '$myname --help' for more information."
37}
38
39print_help() {
40 print_usage
41 echo
42 echo "Add a .gdb_index section to FILENAME to facilitate faster debug"
43 echo "information loading by GDB."
44 echo
45 echo " -h, --help Print this message then exit."
46 echo " -v, --version Print version information then exit."
47 echo " --dwarf-5 Add the DWARF-5 style .debug_names section"
48 echo " instead of .gdb_index."
49}
50
51print_version() {
52 echo "GNU gdb-add-index (${PKGVERSION}) ${VERSION}"
53}
54
25289ac1 55dwarf5=""
71f193a5
AB
56
57# Parse options.
58until
59opt=$1
60case ${opt} in
61 --dwarf-5 | -dwarf-5)
62 dwarf5="-dwarf-5"
63 ;;
64
65 --help | -help | -h)
66 print_help
67 exit 0
68 ;;
69
70 --version | -version | -v)
71 print_version
72 exit 0
73 ;;
74
75 -?*)
76 print_try_help 1>&2
77 exit 2
78 ;;
79
80 *)
81 # No arguments remaining.
82 ;;
83esac
84# Break from loop if the first character of OPT is not '-'.
85[ "x$(printf %.1s "$opt")" != "x-" ]
86do
25289ac1 87 shift
71f193a5 88done
25289ac1 89
caf26be9 90if test $# != 1; then
71f193a5 91 print_try_help
caf26be9
SB
92 exit 1
93fi
94
95file="$1"
96
db1f6cd6
LS
97if test -L "$file"; then
98 if ! command -v readlink >/dev/null 2>&1; then
99 echo "$myname: 'readlink' missing. Failed to follow symlink $1." 1>&2
100 exit 1
101 fi
102
103 # Count number of links followed in order to detect loops.
104 count=0
105 while test -L "$file"; do
106 target=$(readlink "$file")
107
108 case "$target" in
109 /*)
110 file="$target"
111 ;;
112 *)
113 file="$(dirname "$file")/$target"
114 ;;
115 esac
116
117 count="$((count + 1))"
118 if test "$count" -gt 10; then
119 echo "$myname: Detected loop while following link $file"
120 exit 1
121 fi
122 done
123fi
124
caf26be9
SB
125if test ! -r "$file"; then
126 echo "$myname: unable to access: $file" 1>&2
127 exit 1
128fi
129
130dir="${file%/*}"
131test "$dir" = "$file" && dir="."
caf26be9 132
2b9f6e89
TV
133dwz_file=""
134if $READELF -S "$file" | grep -q " \.gnu_debugaltlink "; then
135 dwz_file=$($READELF --string-dump=.gnu_debugaltlink "$file" \
136 | grep -A1 "'\.gnu_debugaltlink':" \
137 | tail -n +2 \
138 | sed 's/.*]//')
139 dwz_file=$(echo $dwz_file)
140 if $READELF -S "$dwz_file" | grep -E -q " \.(gdb_index|debug_names) "; then
141 # Already has an index, skip it.
142 dwz_file=""
143 fi
144fi
145
146set_files ()
147{
efe1ecd8 148 fpath="$1"
2b9f6e89 149
efe1ecd8
LS
150 index4="${fpath}.gdb-index"
151 index5="${fpath}.debug_names"
152 debugstr="${fpath}.debug_str"
153 debugstrmerge="${fpath}.debug_str.merge"
154 debugstrerr="${fpath}.debug_str.err"
2b9f6e89
TV
155}
156
157tmp_files=
158for f in "$file" "$dwz_file"; do
159 if [ "$f" = "" ]; then
160 continue
161 fi
162 set_files "$f"
163 tmp_files="$tmp_files $index4 $index5 $debugstr $debugstrmerge $debugstrerr"
164done
165
166rm -f $tmp_files
167
caf26be9 168# Ensure intermediate index file is removed when we exit.
2b9f6e89 169trap "rm -f $tmp_files" 0
caf26be9
SB
170
171$GDB --batch -nx -iex 'set auto-load no' \
18694ad5 172 -iex 'set debuginfod enabled off' \
24e5f974 173 -ex "file '$file'" -ex "save gdb-index $dwarf5 '$dir'" || {
caf26be9
SB
174 # Just in case.
175 status=$?
176 echo "$myname: gdb error generating index for $file" 1>&2
177 exit $status
178}
179
180# In some situations gdb can exit without creating an index. This is
181# not an error.
ac51afb5 182# E.g., if $file is stripped. This behavior is akin to stripping an
caf26be9
SB
183# already stripped binary, it's a no-op.
184status=0
185
2b9f6e89
TV
186handle_file ()
187{
efe1ecd8 188 fpath="$1"
2b9f6e89 189
efe1ecd8 190 set_files "$fpath"
2b9f6e89
TV
191
192 if test -f "$index4" -a -f "$index5"; then
efe1ecd8 193 echo "$myname: Both index types were created for $fpath" 1>&2
2b9f6e89
TV
194 status=1
195 elif test -f "$index4" -o -f "$index5"; then
196 if test -f "$index4"; then
197 index="$index4"
198 section=".gdb_index"
437afbb8 199 else
2b9f6e89
TV
200 index="$index5"
201 section=".debug_names"
202 fi
2b9f6e89 203 if test -s "$debugstr"; then
efe1ecd8 204 if ! $OBJCOPY --dump-section .debug_str="$debugstrmerge" "$fpath" \
24e5f974
TT
205 /dev/null 2> "$debugstrerr"; then
206 cat >&2 "$debugstrerr"
2b9f6e89
TV
207 exit 1
208 fi
24e5f974 209 cat "$debugstr" >>"$debugstrmerge"
2b9f6e89 210 if grep -q "can't dump section '.debug_str' - it does not exist" \
24e5f974
TT
211 "$debugstrerr"; then
212 $OBJCOPY --add-section $section="$index" \
213 --set-section-flags $section=readonly \
214 --add-section .debug_str="$debugstrmerge" \
215 --set-section-flags .debug_str=readonly \
216 "$fpath" "$fpath"
2b9f6e89 217 else
24e5f974
TT
218 $OBJCOPY --add-section $section="$index" \
219 --set-section-flags $section=readonly \
220 --update-section .debug_str="$debugstrmerge" \
221 "$fpath" "$fpath"
2b9f6e89 222 fi
24e5f974
TT
223 else
224 $OBJCOPY --add-section $section="$index" \
225 --set-section-flags $section=readonly \
226 "$fpath" "$fpath"
437afbb8 227 fi
437afbb8 228
2b9f6e89
TV
229 status=$?
230 else
efe1ecd8 231 echo "$myname: No index was created for $fpath" 1>&2
2b9f6e89
TV
232 echo "$myname: [Was there no debuginfo? Was there already an index?]" \
233 1>&2
234 fi
235}
437afbb8 236
2b9f6e89
TV
237handle_file "$file"
238if [ "$dwz_file" != "" ]; then
239 handle_file "$dwz_file"
caf26be9
SB
240fi
241
242exit $status