[4.0] Use strip (instead of eu-strip) to support --strip-debug of *.so at build time
[platform/upstream/rpm.git] / scripts / find-debuginfo.sh
1 #!/bin/bash
2 #find-debuginfo.sh - automagically generate debug info and file list
3 #for inclusion in an rpm spec file.
4 #
5 # Usage: find-debuginfo.sh [--strict-build-id] [--strip-disable] [--strip-option] [-g] [-r]
6 #                          [-o debugfiles.list]
7 #                          [[-l filelist]... [-p 'pattern'] -o debuginfo.list]
8 #                          [builddir]
9 #
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.
14 #
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 $).
22 #
23 # All file names in switches are relative to builddir (. if not given).
24 #
25
26 # With -g arg, pass it to strip on libraries.
27 strip_g=false
28
29 # with -r arg, pass --reloc-debug-sections to eu-strip.
30 strip_r=false
31
32 # Barf on missing build IDs.
33 strict=false
34
35 # With --strip-disable arg, no strip
36 strip_disable=false
37
38 # With --strip-option arg, this will be used as arg. of eu-strip
39 strip_option=
40
41 BUILDDIR=.
42 out=debugfiles.list
43 nout=0
44 while [ $# -gt 0 ]; do
45   case "$1" in
46   --strict-build-id)
47     strict=true
48     ;;
49   --strip-disable)
50     strip_disable=true
51     ;;
52   *--strip-option*)
53     strip_option=$(echo $1 | sed 's/--strip-option=//')
54     ;;
55   -g)
56     strip_g=true
57     ;;
58   -o)
59     if [ -z "${lists[$nout]}" -a -z "${ptns[$nout]}" ]; then
60       out=$2
61     else
62       outs[$nout]=$2
63       ((nout++))
64     fi
65     shift
66     ;;
67   -l)
68     lists[$nout]="${lists[$nout]} $2"
69     shift
70     ;;
71   -p)
72     ptns[$nout]=$2
73     shift
74     ;;
75   -r)
76     strip_r=true
77     ;;
78   *)
79     BUILDDIR=$1
80     shift
81     break
82     ;;
83   esac
84   shift
85 done
86
87 i=0
88 while ((i < nout)); do
89   outs[$i]="$BUILDDIR/${outs[$i]}"
90   l=''
91   for f in ${lists[$i]}; do
92     l="$l $BUILDDIR/$f"
93   done
94   lists[$i]=$l
95   ((++i))
96 done
97
98 LISTFILE="$BUILDDIR/$out"
99 SOURCEFILE="$BUILDDIR/debugsources.list"
100 LINKSFILE="$BUILDDIR/debuglinks.list"
101
102 > "$SOURCEFILE"
103 > "$LISTFILE"
104 > "$LINKSFILE"
105
106 debugdir="${RPM_BUILD_ROOT}/usr/lib/debug"
107
108 strip_to_debug()
109 {
110   local g=
111   local r=
112
113   if test "$strip_disable" = true ; then
114       exit
115   fi
116
117   $strip_r && r=--reloc-debug-sections
118   $strip_g && case "$(file -bi "$2")" in
119   application/x-sharedlib*) g=-g ;;
120   esac
121
122   case $2 in
123       *.ko)
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
128           ;;
129       *)
130           strip --remove-section=.comment  $g $strip_option -o "$1" "$2" || exit
131   esac
132   chmod 444 "$1" || exit
133 }
134
135 # Make a relative symlink to $1 called $3$2
136 shopt -s extglob
137 link_relative()
138 {
139   local t="$1" f="$2" pfx="$3"
140   local fn="${f#/}" tn="${t#/}"
141   local fd td d
142
143   while fd="${fn%%/*}"; td="${tn%%/*}"; [ "$fd" = "$td" ]; do
144     fn="${fn#*/}"
145     tn="${tn#*/}"
146   done
147
148   d="${fn%/*}"
149   if [ "$d" != "$fn" ]; then
150     d="${d//+([!\/])/..}"
151     tn="${d}/${tn}"
152   fi
153
154   mkdir -p "$(dirname "$pfx$f")" && ln -snf "$tn" "$pfx$f"
155 }
156
157 # Make a symlink in /usr/lib/debug/$2 to $1
158 debug_link()
159 {
160   local l="/usr/lib/debug$2"
161   local t="$1"
162   echo >> "$LINKSFILE" "$l $t"
163
164   # this should correspond to what brp-symlink is doing
165   case $t in
166       /usr*)
167           link_relative "$t" "$l" "$RPM_BUILD_ROOT"
168           ;;
169       *)
170           if [ ! -e $t ]; then
171               link_relative "$t" "$l" "$RPM_BUILD_ROOT"
172           else
173               mkdir -p "$(dirname "$RPM_BUILD_ROOT$l")" && \
174                   ln -snf "$t" "$RPM_BUILD_ROOT$l"
175           fi
176           ;;
177   esac
178 }
179
180 # Provide .2, .3, ... symlinks to all filename instances of this build-id.
181 make_id_dup_link()
182 {
183   # See https://bugzilla.redhat.com/show_bug.cgi?id=641377 for the reasoning, 
184   # but it has seveal drawbacks as we would need to split the .1 suffixes into 
185   # different subpackages and it's about impossible to predict the number
186   # -> perhaps later
187   return
188   local id="$1" file="$2" idfile
189
190   local n=1
191   while true; do
192     idfile=".build-id/${id:0:2}/${id:2}.$n"
193     [ $# -eq 3 ] && idfile="${idfile}$3"
194     if [ ! -L "$RPM_BUILD_ROOT/usr/lib/debug/$idfile" ]; then
195       break
196     fi
197     n=$[$n+1]
198   done
199   debug_link "$file" "/$idfile"
200 }
201
202 # Compare two binaries but ignore the .note.gnu.build-id section
203 elfcmp()
204 {
205   local tmp1=$(mktemp -t ${1##*/}.XXXXXX)
206   local tmp2=$(mktemp -t ${2##*/}.XXXXXX)
207
208   objcopy -R .note.gnu.build-id -R .gnu_debuglink $1 $tmp1
209   objcopy -R .note.gnu.build-id -R .gnu_debuglink $2 $tmp2
210   cmp -s $tmp1 $tmp2
211   local res=$?
212   rm -f $tmp1 $tmp2
213   return $res
214 }
215
216 # Make a build-id symlink for id $1 with suffix $3 to file $2.
217 make_id_link()
218 {
219   local id="$1" file="$2"
220   local idfile=".build-id/${id:0:2}/${id:2}"
221   [ $# -eq 3 ] && idfile="${idfile}$3"
222   local root_idfile="$RPM_BUILD_ROOT/usr/lib/debug/$idfile"
223
224   if [ ! -L "$root_idfile" ]; then
225     debug_link "$file" "/$idfile"
226     return
227   fi
228
229   make_id_dup_link "$@"
230
231   [ $# -eq 3 ] && return 0
232
233   local other=$(readlink -m "$root_idfile")
234   other=${other#$RPM_BUILD_ROOT}
235   if cmp -s "$RPM_BUILD_ROOT$other" "$RPM_BUILD_ROOT$file" ||
236      elfcmp "$RPM_BUILD_ROOT$other" "$RPM_BUILD_ROOT$file" ; then
237     # Two copies.  Maybe one has to be setuid or something.
238     echo >&2 "*** WARNING: identical binaries are copied, not linked:"
239     echo >&2 "        $file"
240     echo >&2 "   and  $other"
241   else
242     # This is pathological, break the build.
243     echo >&2 "*** ERROR: same build ID in nonidentical files!"
244     echo >&2 "        $file"
245     echo >&2 "   and  $other"
246     exit 2
247   fi
248 }
249
250 get_debugfn()
251 {
252   dn=$(dirname "${1#$RPM_BUILD_ROOT}")
253 # Do not strip existing .debug suffixes
254   bn=$(basename "$1").debug
255
256   debugdn=${debugdir}${dn}
257   debugfn=${debugdn}/${bn}
258 }
259
260 set -o pipefail
261
262 strict_error=ERROR
263 $strict || strict_error=WARNING
264
265 # Strip ELF binaries (and no static libraries)
266 find $RPM_BUILD_ROOT ! -path "${debugdir}/*.debug" -type f \( -perm /111 -or -name "*.so*" -or -name "*.ko" \) ! -name "*.a" -print0 | sort -z |
267 xargs --no-run-if-empty -0 stat -c '%h %D_%i %n' |
268 while read nlinks inum f; do
269   case $(objdump -h $f 2>/dev/null | egrep -o '(debug[\.a-z_]*|gnu.version)') in
270     *debuglink*) continue ;;
271     *debug*) ;;
272     *gnu.version*)
273         echo "WARNING: "`echo $f | sed -e "s,^$RPM_BUILD_ROOT/*,/,"`" is already stripped!"
274         continue
275         ;;
276     *) continue ;;
277   esac
278   get_debugfn "$f"
279   [ -f "${debugfn}" ] && continue
280
281   # If this file has multiple links, keep track and make
282   # the corresponding .debug files all links to one file too.
283   if [ $nlinks -gt 1 ]; then
284     eval linked=\$linked_$inum
285     if [ -n "$linked" ]; then
286       eval id=\$linkedid_$inum
287       make_id_dup_link "$id" "$dn/$(basename $f)"
288       make_id_dup_link "$id" "/usr/lib/debug$dn/$bn" .debug
289       link=$debugfn
290       get_debugfn "$linked"
291       echo "hard linked $link to $debugfn"
292       mkdir -p "$(dirname "$link")" && ln -nf "$debugfn" "$link"
293       continue
294     else
295       eval linked_$inum=\$f
296       echo "file $f has $[$nlinks - 1] other hard links"
297     fi
298   fi
299
300   echo "extracting debug info from $f"
301   id=$($(DEBUGEDIT=$(which debugedit 2>/dev/null); \
302       echo ${DEBUGEDIT:-/usr/lib/rpm/debugedit}) -b "$RPM_BUILD_DIR" \
303       -d /usr/src/debug -i -l "$SOURCEFILE" "$f") || exit
304   if [ $nlinks -gt 1 ]; then
305     eval linkedid_$inum=\$id
306   fi
307   if [ -z "$id" ]; then
308     echo >&2 "*** ${strict_error}: No build ID note found in $f"
309     $strict && exit 2
310   fi
311
312   [ -x /usr/bin/gdb-add-index ] && /usr/bin/gdb-add-index "$f" > /dev/null 2>&1
313
314   # A binary already copied into /usr/lib/debug doesn't get stripped,
315   # just has its file names collected and adjusted.
316   case "$dn" in
317   /usr/lib/debug/*)
318     [ -z "$id" ] || make_id_link "$id" "$dn/$(basename $f)"
319     continue ;;
320   esac
321
322   mkdir -p "${debugdn}"
323   if [ -e "${BUILDDIR}/Kconfig" ] ; then
324       mode=$(stat -c %a "$f")
325       chmod +w "$f"
326       objcopy --only-keep-debug $f $debugfn || :
327       (
328           shopt -s extglob
329           strip_option="--strip-all"
330           case "$f" in
331               *.ko)
332                   strip_option="--strip-debug" ;;
333               *$STRIP_KEEP_SYMTAB*)
334                   if test -n "$STRIP_KEEP_SYMTAB"; then
335                       strip_option="--strip-debug"
336                   fi
337                   ;;
338           esac
339           if test "$NO_DEBUGINFO_STRIP_DEBUG" = true ; then
340               strip_option=
341           fi
342           if test "$strip_disable" = true ; then
343               strip_option=
344           fi
345           objcopy --add-gnu-debuglink=$debugfn -R .comment -R .GCC.command.line $strip_option $f
346           chmod $mode $f
347       ) || :
348   else
349       if test -w "$f"; then
350           strip_to_debug "${debugfn}" "$f"
351       else
352           chmod u+w "$f"
353           strip_to_debug "${debugfn}" "$f"
354           chmod u-w "$f"
355       fi
356   fi
357
358   if [ -n "$id" ]; then
359     make_id_link "$id" "$dn/$(basename $f)"
360     make_id_link "$id" "/usr/lib/debug$dn/$bn" .debug
361   fi
362 done || exit
363
364 # We used to make a .debug symlink for each symlink whose target
365 # has a .debug file to that file.  This is not necessary because
366 # the debuglink section contains only the destination of those links.
367 # Creating those links anyway results in debuginfo packages for
368 # devel packages just because of the .so symlinks in them.
369
370 if [ -s "$SOURCEFILE" ]; then
371   mkdir -p "${RPM_BUILD_ROOT}/usr/src/debug"
372   LC_ALL=C sort -z -u "$SOURCEFILE" | grep -E -v -z '(<internal>|<built-in>)$' |
373   (cd "$RPM_BUILD_DIR"; cpio -pd0mL "${RPM_BUILD_ROOT}/usr/src/debug")
374   # stupid cpio creates new directories in mode 0700, fixup
375   find "${RPM_BUILD_ROOT}/usr/src/debug" -type d -print0 |
376   xargs --no-run-if-empty -0 chmod a+rx
377   find "${RPM_BUILD_ROOT}/usr/src/debug" -type f -print0 |
378   xargs --no-run-if-empty -0 chmod a+r
379 fi
380
381 if [ -d "${RPM_BUILD_ROOT}/usr/lib" -o -d "${RPM_BUILD_ROOT}/usr/src" ]; then
382   ((nout > 0)) ||
383   test ! -d "${RPM_BUILD_ROOT}/usr/lib" ||
384   (cd "${RPM_BUILD_ROOT}/usr/lib"; test ! -d debug || find debug -type d) |
385   sed 's,^,%dir /usr/lib/,' >> "$LISTFILE"
386
387   (cd "${RPM_BUILD_ROOT}/usr"
388    test ! -d lib/debug || find lib/debug ! -type d
389   ) | sed 's,^,/usr/,' >> "$LISTFILE"
390 fi
391
392 : > "$SOURCEFILE"
393 if [ -d "${RPM_BUILD_ROOT}/usr/src" ]; then
394   (cd "${RPM_BUILD_ROOT}/usr"
395    test ! -d src/debug || find src/debug -mindepth 1 -maxdepth 1
396   ) | sed 's,^,/usr/,' >> "$SOURCEFILE"
397 fi
398
399 # Append to $1 only the lines from stdin not already in the file.
400 append_uniq()
401 {
402   grep -F -f "$1" -x -v >> "$1"
403 }
404
405 # Helper to generate list of corresponding .debug files from a file list.
406 filelist_debugfiles()
407 {
408   local extra="$1"
409   shift
410   sed 's/^%[a-z0-9_][a-z0-9_]*([^)]*) *//
411 s/^%[a-z0-9_][a-z0-9_]* *//
412 /^$/d
413 '"$extra" "$@"
414 }
415
416 # Write an output debuginfo file list based on given input file lists.
417 filtered_list()
418 {
419   local out="$1"
420   shift
421   test $# -gt 0 || return
422   grep -F -f <(filelist_debugfiles 's,^.*$,/usr/lib/debug&.debug,' "$@") \
423         -x $LISTFILE >> $out
424   sed -n -f <(filelist_debugfiles 's/[\\.*+#]/\\&/g
425 h
426 s,^.*$,s# &$##p,p
427 g
428 s,^.*$,s# /usr/lib/debug&.debug$##p,p
429 ' "$@") "$LINKSFILE" | append_uniq "$out"
430 }
431
432 # Write an output debuginfo file list based on an grep -E -style regexp.
433 pattern_list()
434 {
435   local out="$1" ptn="$2"
436   test -n "$ptn" || return
437   grep -E -x -e "$ptn" "$LISTFILE" >> "$out"
438   sed -n -r "\#^$ptn #s/ .*\$//p" "$LINKSFILE" | append_uniq "$out"
439 }
440
441 #
442 # When given multiple -o switches, split up the output as directed.
443 #
444 i=0
445 while ((i < nout)); do
446   > ${outs[$i]}
447   filtered_list ${outs[$i]} ${lists[$i]}
448   pattern_list ${outs[$i]} "${ptns[$i]}"
449   grep -Fvx -f ${outs[$i]} "$LISTFILE" > "${LISTFILE}.new"
450   mv "${LISTFILE}.new" "$LISTFILE"
451   ((++i))
452 done
453 if ((nout > 0)); then
454   # Now add the right %dir lines to each output list.
455   (cd "${RPM_BUILD_ROOT}"; find usr/lib/debug -type d) |
456   sed 's#^.*$#\\@^/&/@{h;s@^.*$@%dir /&@p;g;}#' |
457   LC_ALL=C sort -ur > "${LISTFILE}.dirs.sed"
458   i=0
459   while ((i < nout)); do
460     sed -n -f "${LISTFILE}.dirs.sed" "${outs[$i]}" | sort -u > "${outs[$i]}.new"
461     cat "${outs[$i]}" >> "${outs[$i]}.new"
462     mv -f "${outs[$i]}.new" "${outs[$i]}"
463     ((++i))
464   done
465   sed -n -f "${LISTFILE}.dirs.sed" "${LISTFILE}" | sort -u > "${LISTFILE}.new"
466   cat "$LISTFILE" >> "${LISTFILE}.new"
467   mv "${LISTFILE}.new" "$LISTFILE"
468 fi