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
2fc89e
        if nm "$module" | grep -qE '^([0-9a-f]+) A __crc_(.+)' 2> /dev/null; then
837a2d
            nm "$module" \
2fc89e
            | awk \
2fc89e
               -v 'dep_pfx='"$dep_pfx" \
2fc89e
               --non-decimal-data \
2fc89e
              'match($0, /^([0-9a-f]+) A __crc_(.+)/, a) { printf("%s(%s) = 0x%08x\n", dep_pfx, a[2], strtonum("0x" a[1])) }'
837a2d
        else
2fc89e
            objdump -t "$module" \
2fc89e
            | sed -n 's/^[0-9a-f][0-9a-f]* g...... \(.*\)	[0-9a-f][0-9a-f]* __crc_.*$/\1/p' \
2fc89e
            | sort -u \
2fc89e
            | while read sectname; do
2fc89e
                [ -n "$sectname" ] || continue
2fc89e
2fc89e
                ELFSECTDATA=$(readelf -R .rodata "$module" | awk '/0x/{printf $2$3$4$5}')
2fc89e
                if [[ -n $(readelf -h "$module" | grep "little endian") ]]; then
2fc89e
                    SECTDATA=$(echo $ELFSECTDATA | sed 's/\(..\)\(..\)\(..\)\(..\)/\4\3\2\1/g')
2fc89e
                else
2fc89e
                    SECTDATA=$ELFSECTDATA
2fc89e
                fi
2fc89e
2fc89e
                objdump -t "$module" \
2fc89e
                | awk \
2fc89e
                  -v 'dep_pfx='"$dep_pfx" \
2fc89e
                  -v 'sectdata='"$SECTDATA" \
2fc89e
                  --non-decimal-data \
2fc89e
                  '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)) }'
2fc89e
            done
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