1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
|
#!/bin/bash
#find-debuginfo.sh - automagically generate debug info and file list
#for inclusion in an rpm spec file.
#
# Usage: find-debuginfo.sh [--strict-build-id] [-g]
# [-o debugfiles.list]
# [[-l filelist]... [-p 'pattern'] -o debuginfo.list]
# [builddir]
#
# The -g flag says to use strip -g instead of full strip on DSOs.
# The --strict-build-id flag says to exit with failure status if
# any ELF binary processed fails to contain a build-id note.
#
# A single -o switch before any -l or -p switches simply renames
# the primary output file from debugfiles.list to something else.
# A -o switch that follows a -p switch or some -l switches produces
# an additional output file with the debuginfo for the files in
# the -l filelist file, or whose names match the -p pattern.
# The -p argument is an grep -E -style regexp matching the a file name,
# and must not use anchors (^ or $).
#
# All file names in switches are relative to builddir (. if not given).
#
debugedit=/usr/lib/rpm/debugedit
if [ ! -x $debugedit ]; then
# rpm5 since 5.2 has moved these binaries to a separate 'bin' subdirectory
if [ -x /usr/lib/rpm/bin/debugedit ]; then
debugedit=/usr/lib/rpm/bin/debugedit
fi
fi
# With -g arg, pass it to strip on libraries.
strip_g=false
# Barf on missing build IDs.
strict=false
BUILDDIR=.
out=debugfiles.list
nout=0
while [ $# -gt 0 ]; do
case "$1" in
--strict-build-id)
strict=true
;;
-g)
strip_g=true
;;
-o)
if [ -z "${lists[$nout]}" -a -z "${ptns[$nout]}" ]; then
out=$2
else
outs[$nout]=$2
((nout++))
fi
shift
;;
-l)
lists[$nout]="${lists[$nout]} $2"
shift
;;
-p)
ptns[$nout]=$2
shift
;;
*)
BUILDDIR=$1
shift
break
;;
esac
shift
done
i=0
while ((i < nout)); do
outs[$i]="$BUILDDIR/${outs[$i]}"
l=''
for f in ${lists[$i]}; do
l="$l $BUILDDIR/$f"
done
lists[$i]=$l
((++i))
done
LISTFILE="$BUILDDIR/$out"
SOURCEFILE="$BUILDDIR/debugsources.list"
LINKSFILE="$BUILDDIR/debuglinks.list"
> "$SOURCEFILE"
> "$LISTFILE"
> "$LINKSFILE"
debugdir="${RPM_BUILD_ROOT}/usr/lib/debug"
strip_to_debug()
{
local g=
$strip_g && case "$(file -bi "$2")" in
application/x-sharedlib*) g=-g ;;
esac
[ -n "$EXCLUDE_FULL_REGEXP" ] && grep -E -q "$EXCLUDE_FULL_REGEXP" <<< "$2" && g=-g
eu-strip --remove-comment $g $([ -n "$DISABLE_DEBUG" ] || echo -f "$1") "$2" || exit
[ -n "$DISABLE_DEBUG" ] || chmod 444 "$1" || exit
}
# Make a relative symlink to $1 called $3$2
shopt -s extglob
link_relative()
{
local t="$1" f="$2" pfx="$3"
local fn="${f#/}" tn="${t#/}"
local fd td d
while fd="${fn%%/*}"; td="${tn%%/*}"; [ "$fd" = "$td" ]; do
fn="${fn#*/}"
tn="${tn#*/}"
done
d="${fn%/*}"
if [ "$d" != "$fn" ]; then
d="${d//+([!\/])/..}"
tn="${d}/${tn}"
fi
mkdir -p "$(dirname "$pfx$f")" && ln -snf "$tn" "$pfx$f"
}
# Make a symlink in /usr/lib/debug/$2 to $1
debug_link()
{
local l="/usr/lib/debug$2"
local t="$1"
echo >> "$LINKSFILE" "$l $t"
link_relative "$t" "$l" "$RPM_BUILD_ROOT"
}
# Make a build-id symlink for id $1 with suffix $3 to file $2.
make_id_link()
{
local id="$1" file="$2"
local idfile=".build-id/${id:0:2}/${id:2}"
[ $# -eq 3 ] && idfile="${idfile}$3"
local root_idfile="$RPM_BUILD_ROOT/usr/lib/debug/$idfile"
if [ ! -L "$root_idfile" ]; then
debug_link "$file" "/$idfile"
return
fi
[ $# -eq 3 ] && return 0
local other=$(readlink -m "$root_idfile")
other=${other#$RPM_BUILD_ROOT}
if cmp -s "$root_idfile" "$RPM_BUILD_ROOT$file" ||
eu-elfcmp -q "$root_idfile" "$RPM_BUILD_ROOT$file" 2> /dev/null; then
# Two copies. Maybe one has to be setuid or something.
echo >&2 "*** WARNING: identical binaries are copied, not linked:"
echo >&2 " $file"
echo >&2 " and $other"
else
# This is pathological, break the build.
echo >&2 "*** ERROR: same build ID in nonidentical files!"
echo >&2 " $file"
echo >&2 " and $other"
exit 2
fi
}
get_debugfn()
{
dn=$(dirname "${1#$RPM_BUILD_ROOT}")
bn=$(basename "$1" .debug).debug
debugdn=${debugdir}${dn}
debugfn=${debugdn}/${bn}
}
set -o pipefail
strict_error=ERROR
$strict || strict_error=WARNING
[[ -n "$EXCLUDE_FROM_STRIP" ]] && \
EXCLUDE_REGEXP=`perl -e 'print "(", join("|", @ARGV), ")"' $EXCLUDE_FROM_STRIP`
[[ -n "$EXCLUDE_FROM_FULL_STRIP" ]] && \
EXCLUDE_FULL_REGEXP=`perl -e 'print "(", join("|", @ARGV), ")"' $EXCLUDE_FROM_FULL_STRIP`
echo $EXCLUDE_REGEXP
# Strip ELF binaries
find "$RPM_BUILD_ROOT" ! -path "${debugdir}/*.debug" -type f \
\( -perm -0100 -or -perm -0010 -or -perm -0001 \) \
-print |
file -N -f - | sed -n -e 's/^\(.*\):[ ]*.*ELF.*, not stripped/\1/p' |
xargs --no-run-if-empty stat -c '%h %D_%i %n' |
while read nlinks inum f; do
[[ -n "$EXCLUDE_REGEXP" ]] && grep -E -q "$EXCLUDE_REGEXP" <<< "$f" && \
continue
[ -n "$DISABLE_DEBUG" ] && strip_to_debug "" "$f" && continue
get_debugfn "$f"
[ -f "${debugfn}" ] && continue
# If this file has multiple links, keep track and make
# the corresponding .debug files all links to one file too.
if [ $nlinks -gt 1 ]; then
eval linked=\$linked_$inum
if [ -n "$linked" ]; then
link=$debugfn
get_debugfn "$linked"
echo "hard linked $link to $debugfn"
ln -nf "$debugfn" "$link"
continue
else
eval linked_$inum=\$f
echo "file $f has $[$nlinks - 1] other hard links"
fi
fi
echo "extracting debug info from $f"
id=$($debugedit -b "$RPM_BUILD_DIR" -d /usr/src/debug \
-i -l "$SOURCEFILE" "$f") || exit
if [ -z "$id" ]; then
echo >&2 "*** ${strict_error}: No build ID note found in $f"
$strict && exit 2
fi
# A binary already copied into /usr/lib/debug doesn't get stripped,
# just has its file names collected and adjusted.
case "$dn" in
/usr/lib/debug/*)
[ -z "$id" ] || make_id_link "$id" "$dn/$(basename $f)"
continue ;;
esac
mkdir -p "${debugdn}"
if test -w "$f"; then
strip_to_debug "${debugfn}" "$f"
else
chmod u+w "$f"
strip_to_debug "${debugfn}" "$f"
chmod u-w "$f"
fi
if [ -n "$id" ]; then
make_id_link "$id" "$dn/$(basename $f)"
make_id_link "$id" "/usr/lib/debug$dn/$bn" .debug
fi
done || exit
# For each symlink whose target has a .debug file,
# make a .debug symlink to that file.
find $RPM_BUILD_ROOT ! -path "${debugdir}/*" -type l -print |
while read f
do
t=$(readlink -m "$f").debug
f=${f#$RPM_BUILD_ROOT}
t=${t#$RPM_BUILD_ROOT}
if [ -f "$debugdir$t" ]; then
echo "symlinked /usr/lib/debug$t to /usr/lib/debug${f}.debug"
debug_link "/usr/lib/debug$t" "${f}.debug"
fi
done
if [ -s "$SOURCEFILE" ]; then
mkdir -p "${RPM_BUILD_ROOT}/usr/src/debug"
LC_ALL=C sort -z -u "$SOURCEFILE" | grep -E -v -z '(<internal>|<built-in>)$' |
(cd "$RPM_BUILD_DIR"; cpio -pd0mL "${RPM_BUILD_ROOT}/usr/src/debug")
# stupid cpio creates new directories in mode 0700, fixup
find "${RPM_BUILD_ROOT}/usr/src/debug" -type d -print0 |
xargs --no-run-if-empty -0 chmod a+rx
fi
if [ -d "${RPM_BUILD_ROOT}/usr/lib" -o -d "${RPM_BUILD_ROOT}/usr/src" ]; then
((nout > 0)) ||
test ! -d "${RPM_BUILD_ROOT}/usr/lib/debug" ||
(cd "${RPM_BUILD_ROOT}/usr/lib"; find debug -type d) |
sed 's,^,%dir /usr/lib/,' >> "$LISTFILE"
(cd "${RPM_BUILD_ROOT}/usr"
test ! -d lib/debug || find lib/debug ! -type d
test ! -d src/debug || find src/debug -mindepth 1 -maxdepth 1
) | sed 's,^,/usr/,' >> "$LISTFILE"
fi
# Append to $1 only the lines from stdin not already in the file.
append_uniq()
{
grep -F -f "$1" -x -v >> "$1"
}
# Helper to generate list of corresponding .debug files from a file list.
filelist_debugfiles()
{
local extra="$1"
shift
sed 's/^%[a-z0-9_][a-z0-9_]*([^)]*) *//
s/^%[a-z0-9_][a-z0-9_]* *//
/^$/d
'"$extra" "$@"
}
# Write an output debuginfo file list based on given input file lists.
filtered_list()
{
local out="$1"
shift
test $# -gt 0 || return
grep -F -f <(filelist_debugfiles 's,^.*$,/usr/lib/debug&.debug,' "$@") \
-x $LISTFILE >> $out
sed -n -f <(filelist_debugfiles 's/[\\.*+#]/\\&/g
h
s,^.*$,s# &$##p,p
g
s,^.*$,s# /usr/lib/debug&.debug$##p,p
' "$@") "$LINKSFILE" | append_uniq "$out"
}
# Write an output debuginfo file list based on an grep -E -style regexp.
pattern_list()
{
local out="$1" ptn="$2"
test -n "$ptn" || return
grep -E -x -e "$ptn" "$LISTFILE" >> "$out"
sed -n -r "\#^$ptn #s/ .*\$//p" "$LINKSFILE" | append_uniq "$out"
}
#
# When given multiple -o switches, split up the output as directed.
#
i=0
while ((i < nout)); do
> ${outs[$i]}
filtered_list ${outs[$i]} ${lists[$i]}
pattern_list ${outs[$i]} "${ptns[$i]}"
grep -Fvx -f ${outs[$i]} "$LISTFILE" > "${LISTFILE}.new"
mv "${LISTFILE}.new" "$LISTFILE"
((++i))
done
if ((nout > 0)); then
# Now add the right %dir lines to each output list.
(cd "${RPM_BUILD_ROOT}"; find usr/lib/debug -type d) |
sed 's#^.*$#\\@^/&/@{h;s@^.*$@%dir /&@p;g;}#' |
LC_ALL=C sort -ur > "${LISTFILE}.dirs.sed"
i=0
while ((i < nout)); do
sed -n -f "${LISTFILE}.dirs.sed" "${outs[$i]}" | sort -u > "${outs[$i]}.new"
cat "${outs[$i]}" >> "${outs[$i]}.new"
mv -f "${outs[$i]}.new" "${outs[$i]}"
((++i))
done
sed -n -f "${LISTFILE}.dirs.sed" "${LISTFILE}" | sort -u > "${LISTFILE}.new"
cat "$LISTFILE" >> "${LISTFILE}.new"
mv "${LISTFILE}.new" "$LISTFILE"
fi
|