Fix: Null pointer dereference in ldlex.l
[binutils-gdb.git] / gdb / contrib / gdb-add-index.sh
blobfc10bb52b49805f37a7a659f797e40185d5d3e5d
1 #! /bin/sh
3 # Add a .gdb_index section to a file.
5 # Copyright (C) 2010-2023 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 assumes gdb and objcopy are in $PATH.
20 # If not, or you want others, pass the following in the environment
21 GDB=${GDB:=gdb}
22 OBJCOPY=${OBJCOPY:=objcopy}
23 READELF=${READELF:=readelf}
25 myname="${0##*/}"
27 dwarf5=""
28 if [ "$1" = "-dwarf-5" ]; then
29 dwarf5="$1"
30 shift
33 if test $# != 1; then
34 echo "usage: $myname [-dwarf-5] FILE" 1>&2
35 exit 1
38 file="$1"
40 if test -L "$file"; then
41 if ! command -v readlink >/dev/null 2>&1; then
42 echo "$myname: 'readlink' missing. Failed to follow symlink $1." 1>&2
43 exit 1
46 # Count number of links followed in order to detect loops.
47 count=0
48 while test -L "$file"; do
49 target=$(readlink "$file")
51 case "$target" in
52 /*)
53 file="$target"
56 file="$(dirname "$file")/$target"
58 esac
60 count="$((count + 1))"
61 if test "$count" -gt 10; then
62 echo "$myname: Detected loop while following link $file"
63 exit 1
65 done
68 if test ! -r "$file"; then
69 echo "$myname: unable to access: $file" 1>&2
70 exit 1
73 dir="${file%/*}"
74 test "$dir" = "$file" && dir="."
76 dwz_file=""
77 if $READELF -S "$file" | grep -q " \.gnu_debugaltlink "; then
78 dwz_file=$($READELF --string-dump=.gnu_debugaltlink "$file" \
79 | grep -A1 "'\.gnu_debugaltlink':" \
80 | tail -n +2 \
81 | sed 's/.*]//')
82 dwz_file=$(echo $dwz_file)
83 if $READELF -S "$dwz_file" | grep -E -q " \.(gdb_index|debug_names) "; then
84 # Already has an index, skip it.
85 dwz_file=""
89 set_files ()
91 fpath="$1"
93 index4="${fpath}.gdb-index"
94 index5="${fpath}.debug_names"
95 debugstr="${fpath}.debug_str"
96 debugstrmerge="${fpath}.debug_str.merge"
97 debugstrerr="${fpath}.debug_str.err"
100 tmp_files=
101 for f in "$file" "$dwz_file"; do
102 if [ "$f" = "" ]; then
103 continue
105 set_files "$f"
106 tmp_files="$tmp_files $index4 $index5 $debugstr $debugstrmerge $debugstrerr"
107 done
109 rm -f $tmp_files
111 # Ensure intermediate index file is removed when we exit.
112 trap "rm -f $tmp_files" 0
114 $GDB --batch -nx -iex 'set auto-load no' \
115 -iex 'set debuginfod enabled off' \
116 -ex "file $file" -ex "save gdb-index $dwarf5 $dir" || {
117 # Just in case.
118 status=$?
119 echo "$myname: gdb error generating index for $file" 1>&2
120 exit $status
123 # In some situations gdb can exit without creating an index. This is
124 # not an error.
125 # E.g., if $file is stripped. This behaviour is akin to stripping an
126 # already stripped binary, it's a no-op.
127 status=0
129 handle_file ()
131 fpath="$1"
133 set_files "$fpath"
135 if test -f "$index4" -a -f "$index5"; then
136 echo "$myname: Both index types were created for $fpath" 1>&2
137 status=1
138 elif test -f "$index4" -o -f "$index5"; then
139 if test -f "$index4"; then
140 index="$index4"
141 section=".gdb_index"
142 else
143 index="$index5"
144 section=".debug_names"
146 debugstradd=false
147 debugstrupdate=false
148 if test -s "$debugstr"; then
149 if ! $OBJCOPY --dump-section .debug_str="$debugstrmerge" "$fpath" \
150 /dev/null 2>$debugstrerr; then
151 cat >&2 $debugstrerr
152 exit 1
154 if grep -q "can't dump section '.debug_str' - it does not exist" \
155 $debugstrerr; then
156 debugstradd=true
157 else
158 debugstrupdate=true
159 cat >&2 $debugstrerr
161 cat "$debugstr" >>"$debugstrmerge"
164 $OBJCOPY --add-section $section="$index" \
165 --set-section-flags $section=readonly \
166 $(if $debugstradd; then \
167 echo --add-section .debug_str="$debugstrmerge"; \
168 echo --set-section-flags .debug_str=readonly; \
169 fi; \
170 if $debugstrupdate; then \
171 echo --update-section .debug_str="$debugstrmerge"; \
172 fi) \
173 "$fpath" "$fpath"
175 status=$?
176 else
177 echo "$myname: No index was created for $fpath" 1>&2
178 echo "$myname: [Was there no debuginfo? Was there already an index?]" \
179 1>&2
183 handle_file "$file"
184 if [ "$dwz_file" != "" ]; then
185 handle_file "$dwz_file"
188 exit $status