]> Git Repo - binutils.git/blob - gdb/contrib/gdb-add-index.sh
Automatic date update in version.in
[binutils.git] / gdb / contrib / gdb-add-index.sh
1 #! /bin/sh
2
3 # Add a .gdb_index section to a file.
4
5 # Copyright (C) 2010-2022 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
21 GDB=${GDB:=gdb}
22 OBJCOPY=${OBJCOPY:=objcopy}
23 READELF=${READELF:=readelf}
24
25 myname="${0##*/}"
26
27 dwarf5=""
28 if [ "$1" = "-dwarf-5" ]; then
29     dwarf5="$1"
30     shift
31 fi
32
33 if test $# != 1; then
34     echo "usage: $myname [-dwarf-5] FILE" 1>&2
35     exit 1
36 fi
37
38 file="$1"
39
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
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
66 fi
67
68 if test ! -r "$file"; then
69     echo "$myname: unable to access: $file" 1>&2
70     exit 1
71 fi
72
73 dir="${file%/*}"
74 test "$dir" = "$file" && dir="."
75
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=""
86     fi
87 fi
88
89 set_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
100 tmp_files=
101 for 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"
107 done
108
109 rm -f $tmp_files
110
111 # Ensure intermediate index file is removed when we exit.
112 trap "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.
127 status=0
128
129 handle_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
183 handle_file "$file"
184 if [ "$dwz_file" != "" ]; then
185     handle_file "$dwz_file"
186 fi
187
188 exit $status
This page took 0.035913 seconds and 4 git commands to generate.