2018-04-19 23:07:42 +02:00
|
|
|
#!/bin/bash
|
|
|
|
|
|
|
|
set -e
|
|
|
|
export LC_COLLATE=C
|
|
|
|
|
|
|
|
usage()
|
|
|
|
{
|
|
|
|
echo "Tool to generate list of symbols that are used to build kernel livepatches"
|
|
|
|
echo
|
|
|
|
echo "Usage: ${0##*/} kernel_build_dir klp_symbols"
|
|
|
|
echo " kernel_build_dir Directory where to find the built kernel binaries"
|
|
|
|
echo " klp_symbols Output file with the list of symbols"
|
|
|
|
}
|
|
|
|
|
|
|
|
if test "$1" == "-h" -o "$1" == "--help" -o $# -ne 2 ; then
|
|
|
|
usage >&2
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2019-02-15 10:39:41 +01:00
|
|
|
KLP_DATA_VERSION="0.1"
|
2018-04-19 23:07:42 +02:00
|
|
|
KERNEL_BUILD_DIR="$1"
|
|
|
|
KLP_SYMBOLS="$2"
|
|
|
|
|
|
|
|
VMLINUX="$KERNEL_BUILD_DIR/vmlinux"
|
|
|
|
MODVER_DIR="$KERNEL_BUILD_DIR/.tmp_versions"
|
|
|
|
|
|
|
|
if ! test -f "$VMLINUX" ; then
|
|
|
|
echo "Error: Cannot open file: $VMLINUX" >&2
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
if ! test -d "$MODVER_DIR" ; then
|
2019-07-25 10:06:00 +02:00
|
|
|
# kernel >= 5.3 has *.mod in the build tree
|
|
|
|
mod_cnt=$(find "$KERNEL_BUILD_DIR" -name '*.mod' | wc -l)
|
|
|
|
if [ $mod_cnt -eq 0 ]; then
|
|
|
|
echo "Error: Directory does not exist: $MODVER_DIR" >&2
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
MODVER_DIR="$KERNEL_BUILD_DIR"
|
2018-04-19 23:07:42 +02:00
|
|
|
fi
|
|
|
|
|
|
|
|
get_symbols()
|
|
|
|
{
|
2019-03-04 09:21:45 +01:00
|
|
|
nm -f posix "$1" | grep -v '\( [UN] \)\|\(\.L\|__crc_\)' | cut -d\ -f1
|
2018-04-19 23:07:42 +02:00
|
|
|
}
|
|
|
|
|
2019-02-15 10:39:41 +01:00
|
|
|
echo "klp-convert-symbol-data.$KLP_DATA_VERSION" > "$KLP_SYMBOLS"
|
|
|
|
echo "*vmlinux" >> "$KLP_SYMBOLS"
|
2018-04-19 23:07:42 +02:00
|
|
|
get_symbols "$VMLINUX" >> "$KLP_SYMBOLS"
|
|
|
|
|
2021-06-14 10:03:53 +02:00
|
|
|
find "$MODVER_DIR" -iname '*.mod' | sort | while read KMOD ; do
|
2019-07-25 10:06:00 +02:00
|
|
|
if [ "$MODVER_DIR" = "$KERNEL_BUILD_DIR" ]; then
|
|
|
|
# kernel >= 5.3, just replace the suffix
|
|
|
|
KO_FILE="${KMOD%.mod}.ko"
|
|
|
|
else
|
|
|
|
# kernel <= 5.2, read .ko path and file from .mod file
|
|
|
|
KO_FILE="$(head -n1 $KMOD)"
|
|
|
|
fi
|
2018-04-19 23:07:42 +02:00
|
|
|
|
|
|
|
# obtain module name and its object file by processing .ko file name
|
|
|
|
NO_SUFFIX="$(basename -s .ko $KO_FILE)"
|
|
|
|
KMOD_NAME="$(echo $NO_SUFFIX | sed 's/-/_/g')"
|
|
|
|
OBJ_FILE="$(dirname $KO_FILE)/$NO_SUFFIX.o"
|
|
|
|
|
|
|
|
# write module name to Symbols file
|
|
|
|
echo "*$KMOD_NAME" >> "$KLP_SYMBOLS"
|
|
|
|
|
|
|
|
# extract symbols from object file and write them to Symbols file
|
|
|
|
get_symbols "$OBJ_FILE" >> "$KLP_SYMBOLS"
|
|
|
|
done
|
|
|
|
|
|
|
|
exit 0
|