2 #find-debuginfo.sh - automagically generate debug info and file list
3 #for inclusion in an rpm spec file.
5 # Usage: find-debuginfo.sh [--strict-build-id] [-g]
7 # [[-l filelist]... [-p 'pattern'] -o debuginfo.list]
10 # The -g flag says to use strip -g instead of full strip on DSOs.
11 # The --strict-build-id flag says to exit with failure status if
12 # any ELF binary processed fails to contain a build-id note.
14 # A single -o switch before any -l or -p switches simply renames
15 # the primary output file from debugfiles.list to something else.
16 # A -o switch that follows a -p switch or some -l switches produces
17 # an additional output file with the debuginfo for the files in
18 # the -l filelist file, or whose names match the -p pattern.
19 # The -p argument is an grep -E -style regexp matching the a file name,
20 # and must not use anchors (^ or $).
22 # All file names in switches are relative to builddir (. if not given).
25 # With -g arg, pass it to strip on libraries.
28 # Barf on missing build IDs.
34 while [ $# -gt 0 ]; do
43 if [ -z "${lists[$nout]}" -a -z "${ptns[$nout]}" ]; then
52 lists[$nout]="${lists[$nout]} $2"
69 while ((i < nout)); do
70 outs[$i]="$BUILDDIR/${outs[$i]}"
72 for f in ${lists[$i]}; do
79 LISTFILE="$BUILDDIR/$out"
80 SOURCEFILE="$BUILDDIR/debugsources.list"
81 LINKSFILE="$BUILDDIR/debuglinks.list"
87 debugdir="${RPM_BUILD_ROOT}/usr/lib/debug"
92 $strip_g && case "$(file -bi "$2")" in
93 application/x-sharedlib*) g=-g ;;
95 eu-strip --remove-comment $g -f "$1" "$2" || exit
96 chmod 444 "$1" || exit
99 # Make a relative symlink to $1 called $3$2
103 local t="$1" f="$2" pfx="$3"
104 local fn="${f#/}" tn="${t#/}"
107 while fd="${fn%%/*}"; td="${tn%%/*}"; [ "$fd" = "$td" ]; do
113 if [ "$d" != "$fn" ]; then
114 d="${d//+([!\/])/..}"
118 mkdir -p "$(dirname "$pfx$f")" && ln -snf "$tn" "$pfx$f"
121 # Make a symlink in /usr/lib/debug/$2 to $1
124 local l="/usr/lib/debug$2"
126 echo >> "$LINKSFILE" "$l $t"
127 link_relative "$t" "$l" "$RPM_BUILD_ROOT"
130 # Make a build-id symlink for id $1 with suffix $3 to file $2.
133 local id="$1" file="$2"
134 local idfile=".build-id/${id:0:2}/${id:2}"
135 [ $# -eq 3 ] && idfile="${idfile}$3"
136 local root_idfile="$RPM_BUILD_ROOT/usr/lib/debug/$idfile"
138 if [ ! -L "$root_idfile" ]; then
139 debug_link "$file" "/$idfile"
143 [ $# -eq 3 ] && return 0
145 local other=$(readlink -m "$root_idfile")
146 other=${other#$RPM_BUILD_ROOT}
147 if cmp -s "$root_idfile" "$RPM_BUILD_ROOT$file" ||
148 eu-elfcmp -q "$root_idfile" "$RPM_BUILD_ROOT$file" 2> /dev/null; then
149 # Two copies. Maybe one has to be setuid or something.
150 echo >&2 "*** WARNING: identical binaries are copied, not linked:"
152 echo >&2 " and $other"
154 # This is pathological, break the build.
155 echo >&2 "*** ERROR: same build ID in nonidentical files!"
157 echo >&2 " and $other"
164 dn=$(dirname "${1#$RPM_BUILD_ROOT}")
165 bn=$(basename "$1" .debug).debug
167 debugdn=${debugdir}${dn}
168 debugfn=${debugdn}/${bn}
174 $strict || strict_error=WARNING
177 find "$RPM_BUILD_ROOT" ! -path "${debugdir}/*.debug" -type f \
178 \( -perm -0100 -or -perm -0010 -or -perm -0001 \) \
180 file -N -f - | sed -n -e 's/^\(.*\):[ ]*.*ELF.*, not stripped/\1/p' |
181 xargs --no-run-if-empty stat -c '%h %D_%i %n' |
182 while read nlinks inum f; do
184 [ -f "${debugfn}" ] && continue
186 # If this file has multiple links, keep track and make
187 # the corresponding .debug files all links to one file too.
188 if [ $nlinks -gt 1 ]; then
189 eval linked=\$linked_$inum
190 if [ -n "$linked" ]; then
192 get_debugfn "$linked"
193 echo "hard linked $link to $debugfn"
194 ln -nf "$debugfn" "$link"
197 eval linked_$inum=\$f
198 echo "file $f has $[$nlinks - 1] other hard links"
202 echo "extracting debug info from $f"
203 id=$(/usr/lib/rpm/debugedit -b "$RPM_BUILD_DIR" -d /usr/src/debug \
204 -i -l "$SOURCEFILE" "$f") || exit
205 if [ -z "$id" ]; then
206 echo >&2 "*** ${strict_error}: No build ID note found in $f"
210 [ -x /usr/bin/gdb-add-index ] && /usr/bin/gdb-add-index "$f" > /dev/null 2>&1
212 # A binary already copied into /usr/lib/debug doesn't get stripped,
213 # just has its file names collected and adjusted.
216 [ -z "$id" ] || make_id_link "$id" "$dn/$(basename $f)"
220 mkdir -p "${debugdn}"
221 if test -w "$f"; then
222 strip_to_debug "${debugfn}" "$f"
225 strip_to_debug "${debugfn}" "$f"
229 if [ -n "$id" ]; then
230 make_id_link "$id" "$dn/$(basename $f)"
231 make_id_link "$id" "/usr/lib/debug$dn/$bn" .debug
235 # For each symlink whose target has a .debug file,
236 # make a .debug symlink to that file.
237 find $RPM_BUILD_ROOT ! -path "${debugdir}/*" -type l -print |
240 t=$(readlink -m "$f").debug
241 f=${f#$RPM_BUILD_ROOT}
242 t=${t#$RPM_BUILD_ROOT}
243 if [ -f "$debugdir$t" ]; then
244 echo "symlinked /usr/lib/debug$t to /usr/lib/debug${f}.debug"
245 debug_link "/usr/lib/debug$t" "${f}.debug"
249 if [ -s "$SOURCEFILE" ]; then
250 mkdir -p "${RPM_BUILD_ROOT}/usr/src/debug"
251 LC_ALL=C sort -z -u "$SOURCEFILE" | grep -E -v -z '(<internal>|<built-in>)$' |
252 (cd "$RPM_BUILD_DIR"; cpio -pd0mL "${RPM_BUILD_ROOT}/usr/src/debug")
253 # stupid cpio creates new directories in mode 0700, fixup
254 find "${RPM_BUILD_ROOT}/usr/src/debug" -type d -print0 |
255 xargs --no-run-if-empty -0 chmod a+rx
258 if [ -d "${RPM_BUILD_ROOT}/usr/lib" -o -d "${RPM_BUILD_ROOT}/usr/src" ]; then
260 test ! -d "${RPM_BUILD_ROOT}/usr/lib" ||
261 (cd "${RPM_BUILD_ROOT}/usr/lib"; find debug -type d) |
262 sed 's,^,%dir /usr/lib/,' >> "$LISTFILE"
264 (cd "${RPM_BUILD_ROOT}/usr"
265 test ! -d lib/debug || find lib/debug ! -type d
266 test ! -d src/debug || find src/debug -mindepth 1 -maxdepth 1
267 ) | sed 's,^,/usr/,' >> "$LISTFILE"
270 # Append to $1 only the lines from stdin not already in the file.
273 grep -F -f "$1" -x -v >> "$1"
276 # Helper to generate list of corresponding .debug files from a file list.
277 filelist_debugfiles()
281 sed 's/^%[a-z0-9_][a-z0-9_]*([^)]*) *//
282 s/^%[a-z0-9_][a-z0-9_]* *//
287 # Write an output debuginfo file list based on given input file lists.
292 test $# -gt 0 || return
293 grep -F -f <(filelist_debugfiles 's,^.*$,/usr/lib/debug&.debug,' "$@") \
295 sed -n -f <(filelist_debugfiles 's/[\\.*+#]/\\&/g
299 s,^.*$,s# /usr/lib/debug&.debug$##p,p
300 ' "$@") "$LINKSFILE" | append_uniq "$out"
303 # Write an output debuginfo file list based on an grep -E -style regexp.
306 local out="$1" ptn="$2"
307 test -n "$ptn" || return
308 grep -E -x -e "$ptn" "$LISTFILE" >> "$out"
309 sed -n -r "\#^$ptn #s/ .*\$//p" "$LINKSFILE" | append_uniq "$out"
313 # When given multiple -o switches, split up the output as directed.
316 while ((i < nout)); do
318 filtered_list ${outs[$i]} ${lists[$i]}
319 pattern_list ${outs[$i]} "${ptns[$i]}"
320 grep -Fvx -f ${outs[$i]} "$LISTFILE" > "${LISTFILE}.new"
321 mv "${LISTFILE}.new" "$LISTFILE"
324 if ((nout > 0)); then
325 # Now add the right %dir lines to each output list.
326 (cd "${RPM_BUILD_ROOT}"; find usr/lib/debug -type d) |
327 sed 's#^.*$#\\@^/&/@{h;s@^.*$@%dir /&@p;g;}#' |
328 LC_ALL=C sort -ur > "${LISTFILE}.dirs.sed"
330 while ((i < nout)); do
331 sed -n -f "${LISTFILE}.dirs.sed" "${outs[$i]}" | sort -u > "${outs[$i]}.new"
332 cat "${outs[$i]}" >> "${outs[$i]}.new"
333 mv -f "${outs[$i]}.new" "${outs[$i]}"
336 sed -n -f "${LISTFILE}.dirs.sed" "${LISTFILE}" | sort -u > "${LISTFILE}.new"
337 cat "$LISTFILE" >> "${LISTFILE}.new"
338 mv "${LISTFILE}.new" "$LISTFILE"