Blame SOURCES/find-requires.ksyms

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