#!/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 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 echo "Error: Directory does not exist: $MODVER_DIR" >&2 exit 1 fi get_symbols() { nm -f posix "$1" | grep -v '\( [UN] \)\|\(\.L\|__crc_\)' | cut -d\ -f1,2 } echo "*vmlinux" > "$KLP_SYMBOLS" get_symbols "$VMLINUX" >> "$KLP_SYMBOLS" find "$MODVER_DIR" -iname '*.mod' | while read KMOD ; do # read .ko path and file from .mod file KO_FILE="$(head -n1 $KMOD)" # 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