kbuild: rewrite check-local-export in sh/awk

Remove the bash build dependency for those who otherwise do not
have it installed. This also provides a significant speedup:

$ make defconfig
$ make yes2modconfig

...

$ find  .  -name "*.o" | grep -v vmlinux | wc
     3169      3169     89615
$ export NM=nm
$ time sh -c 'find . -name "*.o" | grep -v vmlinux | xargs -n1
./scripts/check-local-export'

Without patch:
    0m15.90s real     0m12.17s user     0m05.28s system

With patch:
dash + nawk
    0m02.16s real     0m02.92s user     0m00.34s system

dash + busybox awk
    0m02.36s real     0m03.36s user     0m00.34s system

dash + gawk
    0m02.07s real     0m03.26s user     0m00.32s system

bash + gawk
    0m03.55s real     0m05.00s user     0m00.54s system

Signed-off-by: Owen Rafferty <owen@owenrafferty.com>
Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>
This commit is contained in:
Owen Rafferty 2022-08-31 15:57:08 -05:00 committed by Masahiro Yamada
parent a6c26e38aa
commit 033a52d033
1 changed files with 48 additions and 49 deletions

View File

@ -1,25 +1,14 @@
#!/usr/bin/env bash #!/bin/sh
# SPDX-License-Identifier: GPL-2.0-only # SPDX-License-Identifier: GPL-2.0-only
# #
# Copyright (C) 2022 Masahiro Yamada <masahiroy@kernel.org> # Copyright (C) 2022 Masahiro Yamada <masahiroy@kernel.org>
# Copyright (C) 2022 Owen Rafferty <owen@owenrafferty.com>
# #
# Exit with error if a local exported symbol is found. # Exit with error if a local exported symbol is found.
# EXPORT_SYMBOL should be used for global symbols. # EXPORT_SYMBOL should be used for global symbols.
set -e set -e
pid=$$
# catch errors from ${NM}
set -o pipefail
# Run the last element of a pipeline in the current shell.
# Without this, the while-loop would be executed in a subshell, and
# the changes made to 'symbol_types' and 'export_symbols' would be lost.
shopt -s lastpipe
declare -A symbol_types
declare -a export_symbols
exit_code=0
# If there is no symbol in the object, ${NM} (both GNU nm and llvm-nm) shows # If there is no symbol in the object, ${NM} (both GNU nm and llvm-nm) shows
# 'no symbols' diagnostic (but exits with 0). It is harmless and hidden by # 'no symbols' diagnostic (but exits with 0). It is harmless and hidden by
@ -29,43 +18,53 @@ exit_code=0
# TODO: # TODO:
# Use --quiet instead of 2>/dev/null when we upgrade the minimum version of # Use --quiet instead of 2>/dev/null when we upgrade the minimum version of
# binutils to 2.37, llvm to 13.0.0. # binutils to 2.37, llvm to 13.0.0.
# Then, the following line will be really simple: # Then, the following line will be simpler:
# ${NM} --quiet ${1} | # { ${NM} --quiet ${1} || kill 0; } |
{ ${NM} ${1} 2>/dev/null || { echo "${0}: ${NM} failed" >&2; false; } } | { ${NM} ${1} 2>/dev/null || { echo "${0}: ${NM} failed" >&2; kill $pid; } } |
while read value type name ${AWK} -v "file=${1}" '
do BEGIN {
# Skip the line if the number of fields is less than 3. i = 0
# }
# case 1)
# For undefined symbols, the first field (value) is empty.
# The outout looks like this:
# " U _printk"
# It is unneeded to record undefined symbols.
#
# case 2)
# For Clang LTO, llvm-nm outputs a line with type 't' but empty name:
# "---------------- t"
if [[ -z ${name} ]]; then
continue
fi
# save (name, type) in the associative array # Skip the line if the number of fields is less than 3.
symbol_types[${name}]=${type} #
# case 1)
# For undefined symbols, the first field (value) is empty.
# The outout looks like this:
# " U _printk"
# It is unneeded to record undefined symbols.
#
# case 2)
# For Clang LTO, llvm-nm outputs a line with type t but empty name:
# "---------------- t"
!length($3) {
next
}
# append the exported symbol to the array # save (name, type) in the associative array
if [[ ${name} == __ksymtab_* ]]; then { symbol_types[$3]=$2 }
export_symbols+=(${name#__ksymtab_})
fi
done
for name in "${export_symbols[@]}" # append the exported symbol to the array
do ($3 ~ /^__ksymtab_/) {
# nm(3) says "If lowercase, the symbol is usually local" export_symbols[i] = $3
if [[ ${symbol_types[$name]} =~ [a-z] ]]; then sub(/^__ksymtab_/, "", export_symbols[i])
echo "$@: error: local symbol '${name}' was exported" >&2 i++
exit_code=1 }
fi
done
exit ${exit_code} END {
exit_code = 0
for (j = 0; j < i; ++j) {
name = export_symbols[j]
# nm(3) says "If lowercase, the symbol is usually local"
if (symbol_types[name] ~ /[a-z]/) {
printf "%s: error: local symbol %s was exported\n",
file, name | "cat 1>&2"
exit_code = 1
}
}
exit exit_code
}'
exit $?