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 # A binary already copied into /usr/lib/debug doesn't get stripped,
211 # just has its file names collected and adjusted.
214 [ -z "$id" ] || make_id_link "$id" "$dn/$(basename $f)"
218 mkdir -p "${debugdn}"
219 if test -w "$f"; then
220 strip_to_debug "${debugfn}" "$f"
223 strip_to_debug "${debugfn}" "$f"
227 if [ -n "$id" ]; then
228 make_id_link "$id" "$dn/$(basename $f)"
229 make_id_link "$id" "/usr/lib/debug$dn/$bn" .debug
233 # For each symlink whose target has a .debug file,
234 # make a .debug symlink to that file.
235 find $RPM_BUILD_ROOT ! -path "${debugdir}/*" -type l -print |
238 t=$(readlink -m "$f").debug
239 f=${f#$RPM_BUILD_ROOT}
240 t=${t#$RPM_BUILD_ROOT}
241 if [ -f "$debugdir$t" ]; then
242 echo "symlinked /usr/lib/debug$t to /usr/lib/debug${f}.debug"
243 debug_link "/usr/lib/debug$t" "${f}.debug"
247 if [ -s "$SOURCEFILE" ]; then
248 mkdir -p "${RPM_BUILD_ROOT}/usr/src/debug"
249 LC_ALL=C sort -z -u "$SOURCEFILE" | grep -E -v -z '(<internal>|<built-in>)$' |
250 (cd "$RPM_BUILD_DIR"; cpio -pd0mL "${RPM_BUILD_ROOT}/usr/src/debug")
251 # stupid cpio creates new directories in mode 0700, fixup
252 find "${RPM_BUILD_ROOT}/usr/src/debug" -type d -print0 |
253 xargs --no-run-if-empty -0 chmod a+rx
256 if [ -d "${RPM_BUILD_ROOT}/usr/lib" -o -d "${RPM_BUILD_ROOT}/usr/src" ]; then
258 test ! -d "${RPM_BUILD_ROOT}/usr/lib" ||
259 (cd "${RPM_BUILD_ROOT}/usr/lib"; find debug -type d) |
260 sed 's,^,%dir /usr/lib/,' >> "$LISTFILE"
262 (cd "${RPM_BUILD_ROOT}/usr"
263 test ! -d lib/debug || find lib/debug ! -type d
264 test ! -d src/debug || find src/debug -mindepth 1 -maxdepth 1
265 ) | sed 's,^,/usr/,' >> "$LISTFILE"
268 # Append to $1 only the lines from stdin not already in the file.
271 grep -F -f "$1" -x -v >> "$1"
274 # Helper to generate list of corresponding .debug files from a file list.
275 filelist_debugfiles()
279 sed 's/^%[a-z0-9_][a-z0-9_]*([^)]*) *//
280 s/^%[a-z0-9_][a-z0-9_]* *//
285 # Write an output debuginfo file list based on given input file lists.
290 test $# -gt 0 || return
291 grep -F -f <(filelist_debugfiles 's,^.*$,/usr/lib/debug&.debug,' "$@") \
293 sed -n -f <(filelist_debugfiles 's/[\\.*+#]/\\&/g
297 s,^.*$,s# /usr/lib/debug&.debug$##p,p
298 ' "$@") "$LINKSFILE" | append_uniq "$out"
301 # Write an output debuginfo file list based on an grep -E -style regexp.
304 local out="$1" ptn="$2"
305 test -n "$ptn" || return
306 grep -E -x -e "$ptn" "$LISTFILE" >> "$out"
307 sed -n -r "\#^$ptn #s/ .*\$//p" "$LINKSFILE" | append_uniq "$out"
311 # When given multiple -o switches, split up the output as directed.
314 while ((i < nout)); do
316 filtered_list ${outs[$i]} ${lists[$i]}
317 pattern_list ${outs[$i]} "${ptns[$i]}"
318 grep -Fvx -f ${outs[$i]} "$LISTFILE" > "${LISTFILE}.new"
319 mv "${LISTFILE}.new" "$LISTFILE"
322 if ((nout > 0)); then
323 # Now add the right %dir lines to each output list.
324 (cd "${RPM_BUILD_ROOT}"; find usr/lib/debug -type d) |
325 sed 's#^.*$#\\@^/&/@{h;s@^.*$@%dir /&@p;g;}#' |
326 LC_ALL=C sort -ur > "${LISTFILE}.dirs.sed"
328 while ((i < nout)); do
329 sed -n -f "${LISTFILE}.dirs.sed" "${outs[$i]}" | sort -u > "${outs[$i]}.new"
330 cat "${outs[$i]}" >> "${outs[$i]}.new"
331 mv -f "${outs[$i]}.new" "${outs[$i]}"
334 sed -n -f "${LISTFILE}.dirs.sed" "${LISTFILE}" | sort -u > "${LISTFILE}.new"
335 cat "$LISTFILE" >> "${LISTFILE}.new"
336 mv "${LISTFILE}.new" "$LISTFILE"