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] [--strip-disable] [--strip-option] [-g] [-r]
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.
13 # The -r flag says to use eu-strip --reloc-debug-sections.
15 # A single -o switch before any -l or -p switches simply renames
16 # the primary output file from debugfiles.list to something else.
17 # A -o switch that follows a -p switch or some -l switches produces
18 # an additional output file with the debuginfo for the files in
19 # the -l filelist file, or whose names match the -p pattern.
20 # The -p argument is an grep -E -style regexp matching the a file name,
21 # and must not use anchors (^ or $).
23 # All file names in switches are relative to builddir (. if not given).
26 # With -g arg, pass it to strip on libraries.
29 # with -r arg, pass --reloc-debug-sections to eu-strip.
32 # Barf on missing build IDs.
35 # With --strip-disable arg, no strip
38 # With --strip-option arg, this will be used as arg. of eu-strip
44 while [ $# -gt 0 ]; do
53 strip_option=$(echo $1 | sed 's/--strip-option=//')
59 if [ -z "${lists[$nout]}" -a -z "${ptns[$nout]}" ]; then
68 lists[$nout]="${lists[$nout]} $2"
88 while ((i < nout)); do
89 outs[$i]="$BUILDDIR/${outs[$i]}"
91 for f in ${lists[$i]}; do
98 LISTFILE="$BUILDDIR/$out"
99 SOURCEFILE="$BUILDDIR/debugsources.list"
100 LINKSFILE="$BUILDDIR/debuglinks.list"
106 debugdir="${RPM_BUILD_ROOT}/usr/lib/debug"
113 if test "$strip_disable" = true ; then
117 $strip_r && r=--reloc-debug-sections
118 $strip_g && case "$(file -bi "$2")" in
119 application/x-sharedlib*) g=-g ;;
124 # don't attempt to create a minimal backtrace binary for
125 # kernel modules as this just causes the stripping process
126 # to be skipped entirely
127 eu-strip --remove-comment $r $strip_option -f "$1" "$2" || exit
130 eu-strip --remove-comment $g $strip_option -f "$1" "$2" || exit
132 chmod 444 "$1" || exit
135 # Make a relative symlink to $1 called $3$2
139 local t="$1" f="$2" pfx="$3"
140 local fn="${f#/}" tn="${t#/}"
143 while fd="${fn%%/*}"; td="${tn%%/*}"; [ "$fd" = "$td" ]; do
149 if [ "$d" != "$fn" ]; then
150 d="${d//+([!\/])/..}"
154 mkdir -p "$(dirname "$pfx$f")" && ln -snf "$tn" "$pfx$f"
157 # Make a symlink in /usr/lib/debug/$2 to $1
160 local l="/usr/lib/debug$2"
162 echo >> "$LINKSFILE" "$l $t"
164 # this should correspond to what brp-symlink is doing
167 link_relative "$t" "$l" "$RPM_BUILD_ROOT"
170 mkdir -p "$(dirname "$RPM_BUILD_ROOT$l")" && \
171 ln -snf "$t" "$RPM_BUILD_ROOT$l"
176 # Provide .2, .3, ... symlinks to all filename instances of this build-id.
179 # See https://bugzilla.redhat.com/show_bug.cgi?id=641377 for the reasoning,
180 # but it has seveal drawbacks as we would need to split the .1 suffixes into
181 # different subpackages and it's about impossible to predict the number
184 local id="$1" file="$2" idfile
188 idfile=".build-id/${id:0:2}/${id:2}.$n"
189 [ $# -eq 3 ] && idfile="${idfile}$3"
190 if [ ! -L "$RPM_BUILD_ROOT/usr/lib/debug/$idfile" ]; then
195 debug_link "$file" "/$idfile"
198 # Compare two binaries but ignore the .note.gnu.build-id section
201 local tmp1=$(mktemp -t ${1##*/}.XXXXXX)
202 local tmp2=$(mktemp -t ${2##*/}.XXXXXX)
204 objcopy -R .note.gnu.build-id -R .gnu_debuglink $1 $tmp1
205 objcopy -R .note.gnu.build-id -R .gnu_debuglink $2 $tmp2
212 # Make a build-id symlink for id $1 with suffix $3 to file $2.
215 local id="$1" file="$2"
216 local idfile=".build-id/${id:0:2}/${id:2}"
217 [ $# -eq 3 ] && idfile="${idfile}$3"
218 local root_idfile="$RPM_BUILD_ROOT/usr/lib/debug/$idfile"
220 if [ ! -L "$root_idfile" ]; then
221 debug_link "$file" "/$idfile"
225 make_id_dup_link "$@"
227 [ $# -eq 3 ] && return 0
229 local other=$(readlink -m "$root_idfile")
230 other=${other#$RPM_BUILD_ROOT}
231 if cmp -s "$RPM_BUILD_ROOT$other" "$RPM_BUILD_ROOT$file" ||
232 elfcmp "$RPM_BUILD_ROOT$other" "$RPM_BUILD_ROOT$file" ; then
233 # Two copies. Maybe one has to be setuid or something.
234 echo >&2 "*** WARNING: identical binaries are copied, not linked:"
236 echo >&2 " and $other"
238 # This is pathological, break the build.
239 echo >&2 "*** ERROR: same build ID in nonidentical files!"
241 echo >&2 " and $other"
248 dn=$(dirname "${1#$RPM_BUILD_ROOT}")
249 # Do not strip existing .debug suffixes
250 bn=$(basename "$1").debug
252 debugdn=${debugdir}${dn}
253 debugfn=${debugdn}/${bn}
259 $strict || strict_error=WARNING
261 # Strip ELF binaries (and no static libraries)
262 find $RPM_BUILD_ROOT ! -path "${debugdir}/*.debug" -type f \( -perm /111 -or -name "*.so*" -or -name "*.ko" \) ! -name "*.a" -print0 | sort -z |
263 xargs --no-run-if-empty -0 stat -c '%h %D_%i %n' |
264 while read nlinks inum f; do
265 case $(objdump -h $f 2>/dev/null | egrep -o '(debug[\.a-z_]*|gnu.version)') in
266 *debuglink*) continue ;;
269 echo "WARNING: "`echo $f | sed -e "s,^$RPM_BUILD_ROOT/*,/,"`" is already stripped!"
275 [ -f "${debugfn}" ] && continue
277 # If this file has multiple links, keep track and make
278 # the corresponding .debug files all links to one file too.
279 if [ $nlinks -gt 1 ]; then
280 eval linked=\$linked_$inum
281 if [ -n "$linked" ]; then
282 eval id=\$linkedid_$inum
283 make_id_dup_link "$id" "$dn/$(basename $f)"
284 make_id_dup_link "$id" "/usr/lib/debug$dn/$bn" .debug
286 get_debugfn "$linked"
287 echo "hard linked $link to $debugfn"
288 mkdir -p "$(dirname "$link")" && ln -nf "$debugfn" "$link"
291 eval linked_$inum=\$f
292 echo "file $f has $[$nlinks - 1] other hard links"
296 echo "extracting debug info from $f"
297 id=$($(DEBUGEDIT=$(which debugedit 2>/dev/null); \
298 echo ${DEBUGEDIT:-/usr/lib/rpm/debugedit}) -b "$RPM_BUILD_DIR" \
299 -d /usr/src/debug -i -l "$SOURCEFILE" "$f") || exit
300 if [ $nlinks -gt 1 ]; then
301 eval linkedid_$inum=\$id
303 if [ -z "$id" ]; then
304 echo >&2 "*** ${strict_error}: No build ID note found in $f"
308 [ -x /usr/bin/gdb-add-index ] && /usr/bin/gdb-add-index "$f" > /dev/null 2>&1
310 # A binary already copied into /usr/lib/debug doesn't get stripped,
311 # just has its file names collected and adjusted.
314 [ -z "$id" ] || make_id_link "$id" "$dn/$(basename $f)"
318 mkdir -p "${debugdn}"
319 if [ -e "${BUILDDIR}/Kconfig" ] ; then
320 mode=$(stat -c %a "$f")
322 objcopy --only-keep-debug $f $debugfn || :
325 strip_option="--strip-all"
328 strip_option="--strip-debug" ;;
329 *$STRIP_KEEP_SYMTAB*)
330 if test -n "$STRIP_KEEP_SYMTAB"; then
331 strip_option="--strip-debug"
335 if test "$NO_DEBUGINFO_STRIP_DEBUG" = true ; then
338 if test "$strip_disable" = true ; then
341 objcopy --add-gnu-debuglink=$debugfn -R .comment -R .GCC.command.line $strip_option $f
345 if test -w "$f"; then
346 strip_to_debug "${debugfn}" "$f"
349 strip_to_debug "${debugfn}" "$f"
354 if [ -n "$id" ]; then
355 make_id_link "$id" "$dn/$(basename $f)"
356 make_id_link "$id" "/usr/lib/debug$dn/$bn" .debug
360 # We used to make a .debug symlink for each symlink whose target
361 # has a .debug file to that file. This is not necessary because
362 # the debuglink section contains only the destination of those links.
363 # Creating those links anyway results in debuginfo packages for
364 # devel packages just because of the .so symlinks in them.
366 if [ -s "$SOURCEFILE" ]; then
367 mkdir -p "${RPM_BUILD_ROOT}/usr/src/debug"
368 LC_ALL=C sort -z -u "$SOURCEFILE" | grep -E -v -z '(<internal>|<built-in>)$' |
369 (cd "$RPM_BUILD_DIR"; cpio -pd0mL "${RPM_BUILD_ROOT}/usr/src/debug")
370 # stupid cpio creates new directories in mode 0700, fixup
371 find "${RPM_BUILD_ROOT}/usr/src/debug" -type d -print0 |
372 xargs --no-run-if-empty -0 chmod a+rx
373 find "${RPM_BUILD_ROOT}/usr/src/debug" -type f -print0 |
374 xargs --no-run-if-empty -0 chmod a+r
377 if [ -d "${RPM_BUILD_ROOT}/usr/lib" -o -d "${RPM_BUILD_ROOT}/usr/src" ]; then
379 test ! -d "${RPM_BUILD_ROOT}/usr/lib" ||
380 (cd "${RPM_BUILD_ROOT}/usr/lib"; test ! -d debug || find debug -type d) |
381 sed 's,^,%dir /usr/lib/,' >> "$LISTFILE"
383 (cd "${RPM_BUILD_ROOT}/usr"
384 test ! -d lib/debug || find lib/debug ! -type d
385 ) | sed 's,^,/usr/,' >> "$LISTFILE"
389 if [ -d "${RPM_BUILD_ROOT}/usr/src" ]; then
390 (cd "${RPM_BUILD_ROOT}/usr"
391 test ! -d src/debug || find src/debug -mindepth 1 -maxdepth 1
392 ) | sed 's,^,/usr/,' >> "$SOURCEFILE"
395 # Append to $1 only the lines from stdin not already in the file.
398 grep -F -f "$1" -x -v >> "$1"
401 # Helper to generate list of corresponding .debug files from a file list.
402 filelist_debugfiles()
406 sed 's/^%[a-z0-9_][a-z0-9_]*([^)]*) *//
407 s/^%[a-z0-9_][a-z0-9_]* *//
412 # Write an output debuginfo file list based on given input file lists.
417 test $# -gt 0 || return
418 grep -F -f <(filelist_debugfiles 's,^.*$,/usr/lib/debug&.debug,' "$@") \
420 sed -n -f <(filelist_debugfiles 's/[\\.*+#]/\\&/g
424 s,^.*$,s# /usr/lib/debug&.debug$##p,p
425 ' "$@") "$LINKSFILE" | append_uniq "$out"
428 # Write an output debuginfo file list based on an grep -E -style regexp.
431 local out="$1" ptn="$2"
432 test -n "$ptn" || return
433 grep -E -x -e "$ptn" "$LISTFILE" >> "$out"
434 sed -n -r "\#^$ptn #s/ .*\$//p" "$LINKSFILE" | append_uniq "$out"
438 # When given multiple -o switches, split up the output as directed.
441 while ((i < nout)); do
443 filtered_list ${outs[$i]} ${lists[$i]}
444 pattern_list ${outs[$i]} "${ptns[$i]}"
445 grep -Fvx -f ${outs[$i]} "$LISTFILE" > "${LISTFILE}.new"
446 mv "${LISTFILE}.new" "$LISTFILE"
449 if ((nout > 0)); then
450 # Now add the right %dir lines to each output list.
451 (cd "${RPM_BUILD_ROOT}"; find usr/lib/debug -type d) |
452 sed 's#^.*$#\\@^/&/@{h;s@^.*$@%dir /&@p;g;}#' |
453 LC_ALL=C sort -ur > "${LISTFILE}.dirs.sed"
455 while ((i < nout)); do
456 sed -n -f "${LISTFILE}.dirs.sed" "${outs[$i]}" | sort -u > "${outs[$i]}.new"
457 cat "${outs[$i]}" >> "${outs[$i]}.new"
458 mv -f "${outs[$i]}.new" "${outs[$i]}"
461 sed -n -f "${LISTFILE}.dirs.sed" "${LISTFILE}" | sort -u > "${LISTFILE}.new"
462 cat "$LISTFILE" >> "${LISTFILE}.new"
463 mv "${LISTFILE}.new" "$LISTFILE"