Blame SOURCES/find-requires.ksyms

837a2d
#! /bin/bash
837a2d
#
837a2d
# This script is called during external module building to create dependencies
837a2d
# both upon the RHEL kernel, and on additional external modules. Symbols that
837a2d
# cannot be reconciled against those provided by the kernel are assumed to be
837a2d
# provided by an external module and "ksym" replaces th regular "kernel" dep.
837a2d
837a2d
IFS=$'\n'
837a2d
837a2d
# Extract all of the symbols provided by this module.
837a2d
all_provides() {
837a2d
    for module in "$@"; do
837a2d
        tmpfile=""
837a2d
        if [ "x${module%.ko}" = "x${module}" ]; then
837a2d
            tmpfile=$(mktemp -t ${0##*/}.XXXXXX.ko)
837a2d
            proc_bin=
837a2d
            case "${module##*.}" in
837a2d
            zst)
837a2d
                    proc_bin=zstd
837a2d
                    ;;
837a2d
            xz)
837a2d
                    proc_bin=xz
837a2d
                    ;;
837a2d
            bz2)
837a2d
                    proc_bin=bzip2
837a2d
                    ;;
837a2d
            gz)
837a2d
                    proc_bin=gzip
837a2d
                    ;;
837a2d
            esac
837a2d
837a2d
            [ -n "$proc_bin" ] || continue
837a2d
837a2d
            "$proc_bin" -d -c - < "$module" > "$tmpfile" || continue
837a2d
            module="$tmpfile"
837a2d
        fi
837a2d
837a2d
        if [[ -n $(nm "$module" | sed -r -ne 's:^0*([0-9a-f]+) A __crc_(.+):0x\1 \2:p') ]]; then
837a2d
            nm "$module" \
837a2d
            | sed -r -ne 's:^0*([0-9a-f]+) A __crc_(.+):0x\1 \2:p' \
837a2d
            | awk --non-decimal-data '{printf("%s:0x%08x\n", $2, $1)}'
837a2d
        else
837a2d
            ELFRODATA=$(readelf -R .rodata "$module" | awk '/0x/{printf $2$3$4$5}')
837a2d
            if [[ -n $(readelf -h "$module" | grep "little endian") ]]; then
837a2d
                RODATA=$(echo $ELFRODATA | sed 's/\(..\)\(..\)\(..\)\(..\)/\4\3\2\1/g')
837a2d
            else
837a2d
                RODATA=$ELFRODATA
837a2d
            fi
837a2d
            # Commit binutils-2_33~1385[1] has changed (and binutils-2_35~1768[2]
837a2d
            # has not reverted it) the calculated type for symbols in read-write
837a2d
            # .rodata section from 'R' to 'D', since, apparently, many kernel
837a2d
            # modules have it indeed read-write.
837a2d
            #
837a2d
            # [1] https://sourceware.org/git/?p=binutils-gdb.git;a=commitdiff;h=a288c270991d
837a2d
            # [2] https://sourceware.org/git/?p=binutils-gdb.git;a=commitdiff;h=49d9fd42acef
837a2d
            for sym in $(nm "$module" | sed -r -ne 's:^0*([0-9a-f]+) [DR] __crc_(.+):0x\1 \2:p'); do
837a2d
                echo $sym $RODATA
837a2d
            done \
837a2d
            | awk --non-decimal-data '{printf("%s:0x%08s\n", $2, substr($3,($1*2)+1,8))}'
837a2d
        fi
837a2d
837a2d
        [ -z "$tmpfile" ] || rm -f -- "$tmpfile"
837a2d
    done \
837a2d
    | LC_ALL=C sort -k1,1 -u
837a2d
}
837a2d
837a2d
# Extract all of the requirements of this module.
837a2d
all_requires() {
837a2d
    for module in "$@"; do
837a2d
        set -- $(/sbin/modinfo -F vermagic "$module" | sed -e 's: .*::' -e q)
837a2d
        /sbin/modprobe --dump-modversions "$module" \
837a2d
        | awk --non-decimal-data '
837a2d
            BEGIN { FS = "\t" ; OFS = "\t" }
837a2d
            {printf("%s:0x%08x\n", $2, $1)}' \
837a2d
        | sed -r -e 's:$:\t'"$1"':'
837a2d
    done \
837a2d
    | LC_ALL=C sort -k1,1 -u
837a2d
}
837a2d
837a2d
# Filter out requirements fulfilled by the module itself.
837a2d
mod_requires() {
837a2d
    LC_ALL=C join -t $'\t' -j 1 -v 1 \
837a2d
        <(all_requires "$@") \
837a2d
        <(all_provides "$@") \
837a2d
    | LC_ALL=C sort -k1,1 -u
837a2d
}
837a2d
837a2d
if ! [ -e /sbin/modinfo -a -e /sbin/modprobe ]; then
837a2d
    cat > /dev/null
837a2d
    exit 0
837a2d
fi
837a2d
837a2d
check_kabi() {
837a2d
    arch=$(uname -m)
837a2d
    kabi_file="/lib/modules/kabi-current/kabi_stablelist_$arch"
837a2d
837a2d
    # If not installed, output a warning and return (continue)
837a2d
    if [ ! -f "$kabi_file" ]; then
837a2d
        echo "" >&2
837a2d
        echo "********************************************************************************" >&2
837a2d
        echo "*********************** KERNEL ABI COMPATIBILITY WARNING ***********************" >&2
837a2d
        echo "********************************************************************************" >&2
837a2d
        echo "The kernel ABI reference files (provided by "kabi-stablelists") were not found." >&2
837a2d
        echo "No compatibility check was performed. Please install the kABI reference files" >&2
837a2d
        echo "and rebuild if you would like to verify compatibility with kernel ABI." >&2
837a2d
        echo "" >&2
837a2d
        return
837a2d
    fi
837a2d
837a2d
    unset non_kabi
837a2d
    for symbol in "$@"; do
837a2d
        if ! egrep "^[[:space:]]$symbol\$" $kabi_file >/dev/null; then
837a2d
            non_kabi=("${non_kabi[@]}" "$symbol")
837a2d
        fi
837a2d
    done
837a2d
837a2d
    if [ ${#non_kabi[@]} -gt 0 ]; then
837a2d
        echo "" >&2
837a2d
        echo "********************************************************************************" >&2
837a2d
        echo "*********************** KERNEL ABI COMPATIBILITY WARNING ***********************" >&2
837a2d
        echo "********************************************************************************" >&2
837a2d
        echo "The following kernel symbols are not guaranteed to remain compatible with" >&2
837a2d
        echo "future kernel updates to this RHEL release:" >&2
837a2d
        echo "" >&2
837a2d
        for symbol in "${non_kabi[@]}"; do
837a2d
            printf "\t$symbol\n" >&2
837a2d
        done
837a2d
        echo "" >&2
837a2d
        echo "Red Hat recommends that you consider using only official kernel ABI symbols" >&2
837a2d
        echo "where possible. Requests for additions to the kernel ABI can be filed with" >&2
837a2d
        echo "your partner or customer representative (component: driver-update-program)." >&2
837a2d
        echo "" >&2
837a2d
    fi
837a2d
}
837a2d
837a2d
modules=($(grep -E '/lib/modules/.+\.ko(\.gz|\.bz2|\.xz|\.zst)?$') "$@")
837a2d
if [ ${#modules[@]} -gt 0 ]; then
837a2d
    kernel=$(/sbin/modinfo -F vermagic "${modules[0]}" | sed -e 's: .*::' -e q)
837a2d
837a2d
    # get all that kernel provides
837a2d
    symvers=$(mktemp -t ${0##*/}.XXXXX)
837a2d
837a2d
    cat /usr/src/kernels/$kernel/Module.symvers | awk '
837a2d
        BEGIN { FS = "\t" ; OFS = "\t" }
837a2d
        { print $2 ":" $1 }
837a2d
    ' \
837a2d
    | sed -r -e 's:$:\t'"$kernel"':' \
837a2d
    | LC_ALL=C sort -k1,1 -u > $symvers
837a2d
837a2d
    # Symbols matching with the kernel get a "kernel" dependency
837a2d
    mod_req=$(mktemp -t mod_req.XXXXX)
837a2d
    mod_requires "${modules[@]}" > "$mod_req"
837a2d
    LC_ALL=C join -t $'\t' -j 1 $symvers "$mod_req" | LC_ALL=C sort -u \
837a2d
    | awk 'BEGIN { FS = "[\t:]" ; OFS = "\t" } { print "kernel(" $1 ") = " $2 }'
837a2d
837a2d
    # Symbols from elsewhere get a "ksym" dependency
837a2d
    LC_ALL=C join -t $'\t' -j 1 -v 2 $symvers "$mod_req" | LC_ALL=C sort -u \
837a2d
    | awk 'BEGIN { FS = "[\t:]" ; OFS = "\t" } { print "ksym(" $1 ") = " $2 }'
837a2d
837a2d
    # Check kABI if the kabi-stablelists package is installed
837a2d
    # Do this last so we can try to output this error at the end
837a2d
    kabi_check_symbols=($(LC_ALL=C join -t $'\t' -j 1 $symvers "$mod_req" | LC_ALL=C sort -u \
837a2d
    | awk 'BEGIN { FS = "[\t:]" ; OFS = "\t" } { print $1 }'))
837a2d
    check_kabi "${kabi_check_symbols[@]}"
837a2d
fi