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