#!/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

KLP_DATA_VERSION="0.1"
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
	# 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"
fi

get_symbols()
{
	nm -f posix "$1" | grep -v '\( [UN] \)\|\(\.L\|__crc_\)' | cut -d\  -f1
}

echo "klp-convert-symbol-data.$KLP_DATA_VERSION" > "$KLP_SYMBOLS"
echo "*vmlinux" >> "$KLP_SYMBOLS"
get_symbols "$VMLINUX" >> "$KLP_SYMBOLS"

find "$MODVER_DIR" -iname '*.mod' | sort | while read KMOD ; do
    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

    # 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
