2020-06-24 21:33:29 +03:00
#!/bin/sh -efu
#
# Process single file for debuginfo business.
# (Will be run multiple instances in parallel.)
#
# Copyright (c) 2020 Vitaly Chikunov <vt@altlinux.org>. Based on brp-debuginfo
# written by Alexey Tourbin <at@altlinux.org>. Based on find-debuginfo.sh
# by Jeff Johnson <jbj@rpm5.org> and Roland McGrath <roland@redhat.com>.
# License: GPLv2+.
2022-12-23 16:08:41 +03:00
. @RPMCONFIGDIR@/rpmb-functions
2020-06-24 21:33:29 +03:00
ValidateBuildRoot
cd "$RPM_BUILD_ROOT"
mkdir -p .tmp
f=$1
rezip=
elfcompress=
2020-12-15 04:36:52 +03:00
strip_rel=
2022-06-24 00:36:36 +03:00
strip_btf='--keep-section=.BTF'
2023-12-08 06:01:37 +03:00
strip='--strip-all'
2020-06-24 21:33:29 +03:00
if [ -z "${f#./lib/modules/*.ko}" ]; then
elfcompress=yes
2020-12-15 04:36:52 +03:00
strip_rel="--reloc-debug-sections"
2021-11-03 01:50:11 +03:00
elif [ -z "${f##./lib/modules/*.ko.*}" ]; then
ext=${f##*.}
unzip=
rezip=
case "$ext" in
gz)
unzip='gzip --force --decompress'
2024-10-26 03:13:42 +03:00
rezip='gzip --force'
2021-11-03 01:50:11 +03:00
;;
xz)
unzip='xz --force --decompress'
2024-10-26 03:13:42 +03:00
rezip='xz --check=crc32 --lzma2=dict=1MiB --force'
2021-11-03 01:50:11 +03:00
;;
2021-11-03 01:55:39 +03:00
zst)
unzip='zstd --quiet --force --rm --decompress'
2024-10-26 03:13:42 +03:00
rezip='zstd --quiet --force --rm'
2021-11-03 01:55:39 +03:00
;;
2021-11-03 01:50:11 +03:00
esac
2020-06-24 21:33:29 +03:00
# Compressed module: uncompress it, and work with uncompressed one.
2021-11-03 01:50:11 +03:00
$unzip "$f"
f="${f%.*}"
2020-06-24 21:33:29 +03:00
elfcompress=yes
2020-12-15 04:36:52 +03:00
strip_rel="--reloc-debug-sections"
2020-06-24 21:33:29 +03:00
elif [ -z "${f##./boot/vmlinuz-*}" ]; then
# Compressed kernel: trigger extraction of vmlinux from the source.
kver=${f#./boot/vmlinuz-}
2020-11-03 13:49:30 +03:00
vmlinuxs=$(find "$RPM_BUILD_DIR" -depth -maxdepth 4 -type f -name vmlinux -print -quit)
2020-06-24 21:33:29 +03:00
vmlinuxd=./usr/lib/debug/lib/modules/$kver/vmlinux
if [ -f "$vmlinuxs" ]; then
install -pD -m644 "$vmlinuxs" "$vmlinuxd"
f=$vmlinuxd
2023-12-08 06:01:37 +03:00
strip=
2020-06-24 21:33:29 +03:00
else
2020-11-03 13:49:30 +03:00
Warning "vmlinux not found in $RPM_BUILD_DIR for -debuginfo"
2020-07-06 23:50:42 +03:00
exit
2020-06-24 21:33:29 +03:00
fi
2022-06-24 00:36:36 +03:00
else
# Non-kernel binaries
strip_btf=
2020-06-24 21:33:29 +03:00
fi
debugf=./usr/lib/debug${f#.}.debug
for pat in ${RPM_BRP_STRIP_DEBUG-}; do
if [ -z "${f##.$pat}" ]; then
strip='--strip-debug'
break
fi
done
for pat in ${RPM_BRP_STRIP_NONE-}; do
if [ -z "${f##.$pat}" ]; then
strip=
break
fi
done
timestamp=$(date --iso-8601=ns --reference="$f")
2021-04-29 21:50:16 +03:00
debugedit -b "$RPM_BUILD_DIR" -d /usr/src/debug -l .tmp/src.$$ "$f"
2020-08-27 21:00:12 +03:00
if [ ! -s .tmp/src.$$ ] && [ -x /usr/bin/eu-elfcompress ]; then
2020-08-23 00:00:21 +03:00
# debugedit has failed to extract sources list, possibly
# due to ELF compression - uncompress and try again.
LD_ORIGIN_PATH=/usr/bin eu-readelf -S "$f" > .tmp/sections.$$
if grep -q ' \.debug_info .* PROGBITS .*C' .tmp/sections.$$; then
# Have SHF_COMPRESSED flag.
zmode=zlib-gabi
elif grep -q ' \.zdebug_info .* PROGBITS ' .tmp/sections.$$; then
zmode=zlib-gnu
else
zmode=
fi
rm .tmp/sections.$$
if [ -n "$zmode" ]; then
LD_ORIGIN_PATH=/usr/bin eu-elfcompress --type=none "$f"
2021-04-29 21:50:16 +03:00
debugedit -b "$RPM_BUILD_DIR" -d /usr/src/debug -l .tmp/src.$$ "$f"
2020-08-23 00:00:21 +03:00
LD_ORIGIN_PATH=/usr/bin eu-elfcompress --type=$zmode "$f"
fi
fi
2020-06-24 21:33:29 +03:00
if [ -n "$strip" ]; then
mkdir -p "${debugf%/*}"
2022-06-24 00:36:36 +03:00
LD_ORIGIN_PATH=/usr/bin eu-strip $strip $strip_rel $strip_btf --remove-comment -f "$debugf" "$f"
2020-06-24 21:33:29 +03:00
2020-07-09 18:20:51 +03:00
if [ -n "$elfcompress" ] && [ -x /usr/bin/eu-elfcompress ]; then
2020-06-24 21:33:29 +03:00
LD_ORIGIN_PATH=/usr/bin eu-elfcompress --quiet "$debugf"
fi
touch --date=$timestamp "$debugf" "$f"
chmod 644 "$debugf"
else
touch --date=$timestamp "$f"
fi
mkdir -p .debuginfo/src/"${f%/*}"
awk 'BEGIN{RS="\0";ORS="\n"}{print}' .tmp/src.$$ |LC_ALL=C sort -u |
while read -r src; do
2020-11-12 22:02:42 +03:00
if [ -f "$RPM_BUILD_DIR"/"$src" ]; then
if [ ! -f ./usr/src/debug/"$src" ]; then
install -pD -m644 "$RPM_BUILD_DIR"/"$src" ./usr/src/debug/"$src".$$
mv -f ./usr/src/debug/"$src".$$ ./usr/src/debug/"$src"
fi
elif [ -d "$RPM_BUILD_DIR"/"$src" ]; then
mkdir -p ./usr/src/debug/"$src"
else
continue
2020-06-24 21:33:29 +03:00
fi
printf '%s\n' /usr/src/debug/"$src"
done >.debuginfo/src/"$f"
2020-08-21 18:37:48 +03:00
[ -s .tmp/src.$$ ] || printf '%s\n' "$f" >> .tmp/no_debug_info
2020-06-24 21:33:29 +03:00
rm .tmp/src.$$
if [ -n "$rezip" ]; then
2024-10-26 03:13:42 +03:00
$rezip "$f"
2020-06-24 21:33:29 +03:00
fi