Blame SOURCES/find-requires.ksyms

67e50c
#! /bin/bash
67e50c
67e50c
IFS=$'\n'
67e50c
67e50c
# Extract all of the symbols provided by this module.
67e50c
all_provides() {
67e50c
    if [[ -n $(nm "$@" | sed -r -ne 's:^0*([0-9a-f]+) A __crc_(.+):0x\1 \2:p') ]]; then
67e50c
        nm "$@" \
67e50c
        | sed -r -ne 's:^0*([0-9a-f]+) A __crc_(.+):0x\1 \2:p' \
67e50c
        | awk --non-decimal-data '{printf("0x%08x\t%s\n", $1, $2)}' \
67e50c
        | LC_ALL=C sort -k2,2 -u
67e50c
    else
67e50c
        ELFRODATA=$(readelf -R .rodata "$@" | awk '/0x/{printf $2$3$4$5}')
67e50c
        if [[ -n $(readelf -h "$@" | grep "little endian") ]]; then
67e50c
            RODATA=$(echo $ELFRODATA | sed 's/\(..\)\(..\)\(..\)\(..\)/\4\3\2\1/g')
67e50c
        else
67e50c
            RODATA=$ELFRODATA
67e50c
        fi
67e50c
        for sym in $(nm "$@" | sed -r -ne 's:^0*([0-9a-f]+) R __crc_(.+):0x\1 \2:p'); do
67e50c
            echo $sym $RODATA
67e50c
        done \
67e50c
        | awk --non-decimal-data '{printf("0x%08s\t%s\n", substr($3,($1*2)+1,8), $2)}' \
67e50c
        | LC_ALL=C sort -k2,2 -u
67e50c
    fi
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" }
67e50c
            {printf("0x%08x\t%s\n", $1, $2)}' \
67e50c
        | sed -r -e 's:$:\t'"$1"':'
67e50c
    done \
67e50c
    | LC_ALL=C sort -k2,2 -u
67e50c
}
67e50c
67e50c
# Filter out requirements fulfilled by the module itself.
67e50c
mod_requires() {
67e50c
    LC_ALL=C join -t $'\t' -j 2 -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
67e50c
modules=($(grep -E '/lib/modules/.+\.ko$'))
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" }
67e50c
        { print $2 "\t" $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
67e50c
    LC_ALL=C join -t $'\t' -j 1 $symvers <(mod_requires "${modules[@]}") | LC_ALL=C sort -u \
67e50c
    | awk '{ FS = "\t" ; OFS = "\t" } { print "kernel(" $1 ") = " $2 }'
67e50c
67e50c
    # Symbols from elsewhere get a "ksym" dependency
67e50c
    LC_ALL=C join -t $'\t' -j 1 -v 2 $symvers <(mod_requires "${modules[@]}") | LC_ALL=C sort -u \
67e50c
    | awk '{ FS = "\t" ; OFS = "\t" } { print "ksym(" $1 ") = " $2 }'
67e50c
fi