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
22 OBJCOPY
=${OBJCOPY:=objcopy}
23 READELF
=${READELF:=readelf}
28 if [ "$1" = "-dwarf-5" ]; then
34 echo "usage: $myname [-dwarf-5] FILE" 1>&2
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
46 # Count number of links followed in order to detect loops.
48 while test -L "$file"; do
49 target
=$
(readlink
"$file")
56 file="$(dirname "$file")/$target"
60 count
="$((count + 1))"
61 if test "$count" -gt 10; then
62 echo "$myname: Detected loop while following link $file"
68 if test ! -r "$file"; then
69 echo "$myname: unable to access: $file" 1>&2
74 test "$dir" = "$file" && dir
="."
77 if $READELF -S "$file" |
grep -q " \.gnu_debugaltlink "; then
78 dwz_file
=$
($READELF --string-dump=.gnu_debugaltlink
"$file" \
79 |
grep -A1 "'\.gnu_debugaltlink':" \
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.
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"
101 for f
in "$file" "$dwz_file"; do
102 if [ "$f" = "" ]; then
106 tmp_files
="$tmp_files $index4 $index5 $debugstr $debugstrmerge $debugstrerr"
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" ||
{
119 echo "$myname: gdb error generating index for $file" 1>&2
123 # In some situations gdb can exit without creating an index. This is
125 # E.g., if $file is stripped. This behaviour is akin to stripping an
126 # already stripped binary, it's a no-op.
135 if test -f "$index4" -a -f "$index5"; then
136 echo "$myname: Both index types were created for $fpath" 1>&2
138 elif test -f "$index4" -o -f "$index5"; then
139 if test -f "$index4"; then
144 section
=".debug_names"
148 if test -s "$debugstr"; then
149 if ! $OBJCOPY --dump-section .debug_str
="$debugstrmerge" "$fpath" \
150 /dev
/null
2>$debugstrerr; then
154 if grep -q "can't dump section '.debug_str' - it does not exist" \
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; \
170 if $debugstrupdate; then \
171 echo --update-section .debug_str
="$debugstrmerge"; \
177 echo "$myname: No index was created for $fpath" 1>&2
178 echo "$myname: [Was there no debuginfo? Was there already an index?]" \
184 if [ "$dwz_file" != "" ]; then
185 handle_file
"$dwz_file"