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] [-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.
38 while [ $# -gt 0 ]; do
47 if [ -z "${lists[$nout]}" -a -z "${ptns[$nout]}" ]; then
56 lists[$nout]="${lists[$nout]} $2"
76 while ((i < nout)); do
77 outs[$i]="$BUILDDIR/${outs[$i]}"
79 for f in ${lists[$i]}; do
86 LISTFILE="$BUILDDIR/$out"
87 SOURCEFILE="$BUILDDIR/debugsources.list"
88 LINKSFILE="$BUILDDIR/debuglinks.list"
94 debugdir="${RPM_BUILD_ROOT}/usr/lib/debug"
99 $strip_r && r=--reloc-debug-sections
102 # don't attempt to create a minimal backtrace binary for
103 # kernel modules as this just causes the stripping process
104 # to be skipped entirely
105 eu-strip --remove-comment $r -f "$1" "$2" || exit
108 eu-strip --remove-comment -g -f "$1" "$2" || exit
110 chmod 444 "$1" || exit
113 # Make a relative symlink to $1 called $3$2
117 local t="$1" f="$2" pfx="$3"
118 local fn="${f#/}" tn="${t#/}"
121 while fd="${fn%%/*}"; td="${tn%%/*}"; [ "$fd" = "$td" ]; do
127 if [ "$d" != "$fn" ]; then
128 d="${d//+([!\/])/..}"
132 mkdir -p "$(dirname "$pfx$f")" && ln -snf "$tn" "$pfx$f"
135 # Make a symlink in /usr/lib/debug/$2 to $1
138 local l="/usr/lib/debug$2"
140 echo >> "$LINKSFILE" "$l $t"
142 # this should correspond to what brp-symlink is doing
145 link_relative "$t" "$l" "$RPM_BUILD_ROOT"
148 mkdir -p "$(dirname "$RPM_BUILD_ROOT$l")" && \
149 ln -snf "$t" "$RPM_BUILD_ROOT$l"
154 # Provide .2, .3, ... symlinks to all filename instances of this build-id.
157 # See https://bugzilla.redhat.com/show_bug.cgi?id=641377 for the reasoning,
158 # but it has seveal drawbacks as we would need to split the .1 suffixes into
159 # different subpackages and it's about impossible to predict the number
162 local id="$1" file="$2" idfile
166 idfile=".build-id/${id:0:2}/${id:2}.$n"
167 [ $# -eq 3 ] && idfile="${idfile}$3"
168 if [ ! -L "$RPM_BUILD_ROOT/usr/lib/debug/$idfile" ]; then
173 debug_link "$file" "/$idfile"
176 # Compare two binaries but ignore the .note.gnu.build-id section
179 local tmp1=$(mktemp -t ${1##*/}.XXXXXX)
180 local tmp2=$(mktemp -t ${2##*/}.XXXXXX)
182 objcopy -R .note.gnu.build-id -R .gnu_debuglink $1 $tmp1
183 objcopy -R .note.gnu.build-id -R .gnu_debuglink $2 $tmp2
190 # Make a build-id symlink for id $1 with suffix $3 to file $2.
193 local id="$1" file="$2"
194 local idfile=".build-id/${id:0:2}/${id:2}"
195 [ $# -eq 3 ] && idfile="${idfile}$3"
196 local root_idfile="$RPM_BUILD_ROOT/usr/lib/debug/$idfile"
198 if [ ! -L "$root_idfile" ]; then
199 debug_link "$file" "/$idfile"
203 make_id_dup_link "$@"
205 [ $# -eq 3 ] && return 0
207 local other=$(readlink -m "$root_idfile")
208 other=${other#$RPM_BUILD_ROOT}
209 if cmp -s "$RPM_BUILD_ROOT$other" "$RPM_BUILD_ROOT$file" ||
210 elfcmp "$RPM_BUILD_ROOT$other" "$RPM_BUILD_ROOT$file" ; then
211 # Two copies. Maybe one has to be setuid or something.
212 echo >&2 "*** WARNING: identical binaries are copied, not linked:"
214 echo >&2 " and $other"
216 # This is pathological, break the build.
217 echo >&2 "*** ERROR: same build ID in nonidentical files!"
219 echo >&2 " and $other"
226 dn=$(dirname "${1#$RPM_BUILD_ROOT}")
227 # Do not strip existing .debug suffixes
228 bn=$(basename "$1").debug
230 debugdn=${debugdir}${dn}
231 debugfn=${debugdn}/${bn}
237 $strict || strict_error=WARNING
239 # Strip ELF binaries (and no static libraries)
240 find $RPM_BUILD_ROOT ! -path "${debugdir}/*.debug" -type f \( -perm +111 -or -name "*.so*" -or -name "*.ko" \) ! -name "*.a" -print0 | sort -z |
241 xargs --no-run-if-empty -0 stat -c '%h %D_%i %n' |
242 while read nlinks inum f; do
243 case $(objdump -h $f 2>/dev/null | egrep -o '(debug[\.a-z_]*|gnu.version)') in
244 *debuglink*) continue ;;
247 echo "WARNING: "`echo $f | sed -e "s,^$RPM_BUILD_ROOT/*,/,"`" is already stripped!"
253 [ -f "${debugfn}" ] && continue
255 # If this file has multiple links, keep track and make
256 # the corresponding .debug files all links to one file too.
257 if [ $nlinks -gt 1 ]; then
258 eval linked=\$linked_$inum
259 if [ -n "$linked" ]; then
260 eval id=\$linkedid_$inum
261 make_id_dup_link "$id" "$dn/$(basename $f)"
262 make_id_dup_link "$id" "/usr/lib/debug$dn/$bn" .debug
264 get_debugfn "$linked"
265 echo "hard linked $link to $debugfn"
266 mkdir -p "$(dirname "$link")" && ln -nf "$debugfn" "$link"
269 eval linked_$inum=\$f
270 echo "file $f has $[$nlinks - 1] other hard links"
274 echo "extracting debug info from $f"
275 id=$($(DEBUGEDIT=$(which debugedit 2>/dev/null); \
276 echo ${DEBUGEDIT:-/usr/lib/rpm/debugedit}) -b "$RPM_BUILD_DIR" \
277 -d /usr/src/debug -i -l "$SOURCEFILE" "$f") || exit
278 if [ $nlinks -gt 1 ]; then
279 eval linkedid_$inum=\$id
281 if [ -z "$id" ]; then
282 echo >&2 "*** ${strict_error}: No build ID note found in $f"
286 [ -x /usr/bin/gdb-add-index ] && /usr/bin/gdb-add-index "$f" > /dev/null 2>&1
288 # A binary already copied into /usr/lib/debug doesn't get stripped,
289 # just has its file names collected and adjusted.
292 [ -z "$id" ] || make_id_link "$id" "$dn/$(basename $f)"
296 mkdir -p "${debugdn}"
297 if [ -e "${BUILDDIR}/Kconfig" ] ; then
298 mode=$(stat -c %a "$f")
300 objcopy --only-keep-debug $f $debugfn || :
303 strip_option="--strip-all"
306 strip_option="--strip-debug" ;;
307 *$STRIP_KEEP_SYMTAB*)
308 if test -n "$STRIP_KEEP_SYMTAB"; then
309 strip_option="--strip-debug"
313 if test "$NO_DEBUGINFO_STRIP_DEBUG" = true ; then
316 objcopy --add-gnu-debuglink=$debugfn -R .comment -R .GCC.command.line $strip_option $f
320 if test -w "$f"; then
321 strip_to_debug "${debugfn}" "$f"
324 strip_to_debug "${debugfn}" "$f"
329 if [ -n "$id" ]; then
330 make_id_link "$id" "$dn/$(basename $f)"
331 make_id_link "$id" "/usr/lib/debug$dn/$bn" .debug
335 # We used to make a .debug symlink for each symlink whose target
336 # has a .debug file to that file. This is not necessary because
337 # the debuglink section contains only the destination of those links.
338 # Creating those links anyway results in debuginfo packages for
339 # devel packages just because of the .so symlinks in them.
341 if [ -s "$SOURCEFILE" ]; then
342 mkdir -p "${RPM_BUILD_ROOT}/usr/src/debug"
343 LC_ALL=C sort -z -u "$SOURCEFILE" | grep -E -v -z '(<internal>|<built-in>)$' |
344 (cd "$RPM_BUILD_DIR"; cpio -pd0mL "${RPM_BUILD_ROOT}/usr/src/debug")
345 # stupid cpio creates new directories in mode 0700, fixup
346 find "${RPM_BUILD_ROOT}/usr/src/debug" -type d -print0 |
347 xargs --no-run-if-empty -0 chmod a+rx
348 find "${RPM_BUILD_ROOT}/usr/src/debug" -type f -print0 |
349 xargs --no-run-if-empty -0 chmod a+r
352 if [ -d "${RPM_BUILD_ROOT}/usr/lib" -o -d "${RPM_BUILD_ROOT}/usr/src" ]; then
354 test ! -d "${RPM_BUILD_ROOT}/usr/lib" ||
355 (cd "${RPM_BUILD_ROOT}/usr/lib"; test ! -d debug || find debug -type d) |
356 sed 's,^,%dir /usr/lib/,' >> "$LISTFILE"
358 (cd "${RPM_BUILD_ROOT}/usr"
359 test ! -d lib/debug || find lib/debug ! -type d
360 ) | sed 's,^,/usr/,' >> "$LISTFILE"
364 if [ -d "${RPM_BUILD_ROOT}/usr/src" ]; then
365 (cd "${RPM_BUILD_ROOT}/usr"
366 test ! -d src/debug || find src/debug -mindepth 1 -maxdepth 1
367 ) | sed 's,^,/usr/,' >> "$SOURCEFILE"
370 # Append to $1 only the lines from stdin not already in the file.
373 grep -F -f "$1" -x -v >> "$1"
376 # Helper to generate list of corresponding .debug files from a file list.
377 filelist_debugfiles()
381 sed 's/^%[a-z0-9_][a-z0-9_]*([^)]*) *//
382 s/^%[a-z0-9_][a-z0-9_]* *//
387 # Write an output debuginfo file list based on given input file lists.
392 test $# -gt 0 || return
393 grep -F -f <(filelist_debugfiles 's,^.*$,/usr/lib/debug&.debug,' "$@") \
395 sed -n -f <(filelist_debugfiles 's/[\\.*+#]/\\&/g
399 s,^.*$,s# /usr/lib/debug&.debug$##p,p
400 ' "$@") "$LINKSFILE" | append_uniq "$out"
403 # Write an output debuginfo file list based on an grep -E -style regexp.
406 local out="$1" ptn="$2"
407 test -n "$ptn" || return
408 grep -E -x -e "$ptn" "$LISTFILE" >> "$out"
409 sed -n -r "\#^$ptn #s/ .*\$//p" "$LINKSFILE" | append_uniq "$out"
413 # When given multiple -o switches, split up the output as directed.
416 while ((i < nout)); do
418 filtered_list ${outs[$i]} ${lists[$i]}
419 pattern_list ${outs[$i]} "${ptns[$i]}"
420 grep -Fvx -f ${outs[$i]} "$LISTFILE" > "${LISTFILE}.new"
421 mv "${LISTFILE}.new" "$LISTFILE"
424 if ((nout > 0)); then
425 # Now add the right %dir lines to each output list.
426 (cd "${RPM_BUILD_ROOT}"; find usr/lib/debug -type d) |
427 sed 's#^.*$#\\@^/&/@{h;s@^.*$@%dir /&@p;g;}#' |
428 LC_ALL=C sort -ur > "${LISTFILE}.dirs.sed"
430 while ((i < nout)); do
431 sed -n -f "${LISTFILE}.dirs.sed" "${outs[$i]}" | sort -u > "${outs[$i]}.new"
432 cat "${outs[$i]}" >> "${outs[$i]}.new"
433 mv -f "${outs[$i]}.new" "${outs[$i]}"
436 sed -n -f "${LISTFILE}.dirs.sed" "${LISTFILE}" | sort -u > "${LISTFILE}.new"
437 cat "$LISTFILE" >> "${LISTFILE}.new"
438 mv "${LISTFILE}.new" "$LISTFILE"