Use grep -E and grep -F instead of egrep and fgrep.
[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]
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 #
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 $).
21 #
22 # All file names in switches are relative to builddir (. if not given).
23 #
24
25 # With -g arg, pass it to strip on libraries.
26 strip_g=false
27
28 # Barf on missing build IDs.
29 strict=false
30
31 BUILDDIR=.
32 out=debugfiles.list
33 nout=0
34 while [ $# -gt 0 ]; do
35   case "$1" in
36   --strict-build-id)
37     strict=true
38     ;;
39   -g)
40     strip_g=true
41     ;;
42   -o)
43     if [ -z "${lists[$nout]}" -a -z "${ptns[$nout]}" ]; then
44       out=$2
45     else
46       outs[$nout]=$2
47       ((nout++))
48     fi
49     shift
50     ;;
51   -l)
52     lists[$nout]="${lists[$nout]} $2"
53     shift
54     ;;
55   -p)
56     ptns[$nout]=$2
57     shift
58     ;;
59   *)
60     BUILDDIR=$1
61     shift
62     break
63     ;;
64   esac
65   shift
66 done
67
68 i=0
69 while ((i < nout)); do
70   outs[$i]="$BUILDDIR/${outs[$i]}"
71   l=''
72   for f in ${lists[$i]}; do
73     l="$l $BUILDDIR/$f"
74   done
75   lists[$i]=$l
76   ((++i))
77 done
78
79 LISTFILE="$BUILDDIR/$out"
80 SOURCEFILE="$BUILDDIR/debugsources.list"
81 LINKSFILE="$BUILDDIR/debuglinks.list"
82
83 > "$SOURCEFILE"
84 > "$LISTFILE"
85 > "$LINKSFILE"
86
87 debugdir="${RPM_BUILD_ROOT}/usr/lib/debug"
88
89 strip_to_debug()
90 {
91   local g=
92   $strip_g && case "$(file -bi "$2")" in
93   application/x-sharedlib*) g=-g ;;
94   esac
95   eu-strip --remove-comment $g -f "$1" "$2" || exit
96   chmod 444 "$1" || exit
97 }
98
99 # Make a relative symlink to $1 called $3$2
100 shopt -s extglob
101 link_relative()
102 {
103   local t="$1" f="$2" pfx="$3"
104   local fn="${f#/}" tn="${t#/}"
105   local fd td d
106
107   while fd="${fn%%/*}"; td="${tn%%/*}"; [ "$fd" = "$td" ]; do
108     fn="${fn#*/}"
109     tn="${tn#*/}"
110   done
111
112   d="${fn%/*}"
113   if [ "$d" != "$fn" ]; then
114     d="${d//+([!\/])/..}"
115     tn="${d}/${tn}"
116   fi
117
118   mkdir -p "$(dirname "$pfx$f")" && ln -snf "$tn" "$pfx$f"
119 }
120
121 # Make a symlink in /usr/lib/debug/$2 to $1
122 debug_link()
123 {
124   local l="/usr/lib/debug$2"
125   local t="$1"
126   echo >> "$LINKSFILE" "$l $t"
127   link_relative "$t" "$l" "$RPM_BUILD_ROOT"
128 }
129
130 # Make a build-id symlink for id $1 with suffix $3 to file $2.
131 make_id_link()
132 {
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"
137
138   if [ ! -L "$root_idfile" ]; then
139     debug_link "$file" "/$idfile"
140     return
141   fi
142
143   [ $# -eq 3 ] && return 0
144
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:"
151     echo >&2 "        $file"
152     echo >&2 "   and  $other"
153   else
154     # This is pathological, break the build.
155     echo >&2 "*** ERROR: same build ID in nonidentical files!"
156     echo >&2 "        $file"
157     echo >&2 "   and  $other"
158     exit 2
159   fi
160 }
161
162 get_debugfn()
163 {
164   dn=$(dirname "${1#$RPM_BUILD_ROOT}")
165   bn=$(basename "$1" .debug).debug
166
167   debugdn=${debugdir}${dn}
168   debugfn=${debugdn}/${bn}
169 }
170
171 set -o pipefail
172
173 strict_error=ERROR
174 $strict || strict_error=WARNING
175
176 # Strip ELF binaries
177 find "$RPM_BUILD_ROOT" ! -path "${debugdir}/*.debug" -type f \
178                      \( -perm -0100 -or -perm -0010 -or -perm -0001 \) \
179                      -print |
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
183   get_debugfn "$f"
184   [ -f "${debugfn}" ] && continue
185
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
191       link=$debugfn
192       get_debugfn "$linked"
193       echo "hard linked $link to $debugfn"
194       ln -nf "$debugfn" "$link"
195       continue
196     else
197       eval linked_$inum=\$f
198       echo "file $f has $[$nlinks - 1] other hard links"
199     fi
200   fi
201
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"
207     $strict && exit 2
208   fi
209
210   # A binary already copied into /usr/lib/debug doesn't get stripped,
211   # just has its file names collected and adjusted.
212   case "$dn" in
213   /usr/lib/debug/*)
214     [ -z "$id" ] || make_id_link "$id" "$dn/$(basename $f)"
215     continue ;;
216   esac
217
218   mkdir -p "${debugdn}"
219   if test -w "$f"; then
220     strip_to_debug "${debugfn}" "$f"
221   else
222     chmod u+w "$f"
223     strip_to_debug "${debugfn}" "$f"
224     chmod u-w "$f"
225   fi
226
227   if [ -n "$id" ]; then
228     make_id_link "$id" "$dn/$(basename $f)"
229     make_id_link "$id" "/usr/lib/debug$dn/$bn" .debug
230   fi
231 done || exit
232
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 |
236 while read f
237 do
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"
244   fi
245 done
246
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
254 fi
255
256 if [ -d "${RPM_BUILD_ROOT}/usr/lib" -o -d "${RPM_BUILD_ROOT}/usr/src" ]; then
257   ((nout > 0)) ||
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"
261
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"
266 fi
267
268 # Append to $1 only the lines from stdin not already in the file.
269 append_uniq()
270 {
271   grep -F -f "$1" -x -v >> "$1"
272 }
273
274 # Helper to generate list of corresponding .debug files from a file list.
275 filelist_debugfiles()
276 {
277   local extra="$1"
278   shift
279   sed 's/^%[a-z0-9_][a-z0-9_]*([^)]*) *//
280 s/^%[a-z0-9_][a-z0-9_]* *//
281 /^$/d
282 '"$extra" "$@"
283 }
284
285 # Write an output debuginfo file list based on given input file lists.
286 filtered_list()
287 {
288   local out="$1"
289   shift
290   test $# -gt 0 || return
291   grep -F -f <(filelist_debugfiles 's,^.*$,/usr/lib/debug&.debug,' "$@") \
292         -x $LISTFILE >> $out
293   sed -n -f <(filelist_debugfiles 's/[\\.*+#]/\\&/g
294 h
295 s,^.*$,s# &$##p,p
296 g
297 s,^.*$,s# /usr/lib/debug&.debug$##p,p
298 ' "$@") "$LINKSFILE" | append_uniq "$out"
299 }
300
301 # Write an output debuginfo file list based on an grep -E -style regexp.
302 pattern_list()
303 {
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"
308 }
309
310 #
311 # When given multiple -o switches, split up the output as directed.
312 #
313 i=0
314 while ((i < nout)); do
315   > ${outs[$i]}
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"
320   ((++i))
321 done
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"
327   i=0
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]}"
332     ((++i))
333   done
334   sed -n -f "${LISTFILE}.dirs.sed" "${LISTFILE}" | sort -u > "${LISTFILE}.new"
335   cat "$LISTFILE" >> "${LISTFILE}.new"
336   mv "${LISTFILE}.new" "$LISTFILE"
337 fi