xref: /netbsd-src/external/gpl3/gdb.old/dist/gdb/contrib/gdb-add-index.sh (revision 8b657b0747480f8989760d71343d6dd33f8d4cf9)
1#! /bin/sh
2
3# Add a .gdb_index section to a file.
4
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.
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}
23READELF=${READELF:=readelf}
24
25myname="${0##*/}"
26
27dwarf5=""
28if [ "$1" = "-dwarf-5" ]; then
29    dwarf5="$1"
30    shift
31fi
32
33if test $# != 1; then
34    echo "usage: $myname [-dwarf-5] FILE" 1>&2
35    exit 1
36fi
37
38file="$1"
39
40if 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
44    fi
45
46    # Count number of links followed in order to detect loops.
47    count=0
48    while test -L "$file"; do
49	target=$(readlink "$file")
50
51	case "$target" in
52	    /*)
53		file="$target"
54		;;
55	    *)
56		file="$(dirname "$file")/$target"
57		;;
58	esac
59
60	count="$((count + 1))"
61	if test "$count" -gt 10; then
62	    echo "$myname: Detected loop while following link $file"
63	    exit 1
64	fi
65    done
66fi
67
68if test ! -r "$file"; then
69    echo "$myname: unable to access: $file" 1>&2
70    exit 1
71fi
72
73dir="${file%/*}"
74test "$dir" = "$file" && dir="."
75
76dwz_file=""
77if $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=""
86    fi
87fi
88
89set_files ()
90{
91    fpath="$1"
92
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"
98}
99
100tmp_files=
101for f in "$file" "$dwz_file"; do
102    if [ "$f" = "" ]; then
103	continue
104    fi
105    set_files "$f"
106    tmp_files="$tmp_files $index4 $index5 $debugstr $debugstrmerge $debugstrerr"
107done
108
109rm -f $tmp_files
110
111# Ensure intermediate index file is removed when we exit.
112trap "rm -f $tmp_files" 0
113
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
121}
122
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.
127status=0
128
129handle_file ()
130{
131    fpath="$1"
132
133    set_files "$fpath"
134
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"
145	fi
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
153	    fi
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
160	    fi
161	    cat "$debugstr" >>"$debugstrmerge"
162	fi
163
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"
174
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
180    fi
181}
182
183handle_file "$file"
184if [ "$dwz_file" != "" ]; then
185    handle_file "$dwz_file"
186fi
187
188exit $status
189