2022-09-01 04:57:08 +08:00
|
|
|
#!/bin/sh
|
2022-05-27 18:01:51 +08:00
|
|
|
# SPDX-License-Identifier: GPL-2.0-only
|
|
|
|
#
|
|
|
|
# Copyright (C) 2022 Masahiro Yamada <masahiroy@kernel.org>
|
2022-09-01 04:57:08 +08:00
|
|
|
# Copyright (C) 2022 Owen Rafferty <owen@owenrafferty.com>
|
2022-05-27 18:01:51 +08:00
|
|
|
#
|
|
|
|
# Exit with error if a local exported symbol is found.
|
|
|
|
# EXPORT_SYMBOL should be used for global symbols.
|
|
|
|
|
|
|
|
set -e
|
2022-09-01 04:57:08 +08:00
|
|
|
pid=$$
|
2022-05-27 18:01:51 +08:00
|
|
|
|
scripts/check-local-export: avoid 'wait $!' for process substitution
Bash 4.4, released in 2016, supports 'wait $!' to check the exit status
of a process substitution, but it seems too new.
Some people using older bash versions (on CentOS 7, Ubuntu 16.04, etc.)
reported an error like this:
./scripts/check-local-export: line 54: wait: pid 17328 is not a child of this shell
I used the process substitution to avoid a pipeline, which executes each
command in a subshell. If the while-loop is executed in the subshell
context, variable changes within are lost after the subshell terminates.
Fortunately, Bash 4.2, released in 2011, supports the 'lastpipe' option,
which makes the last element of a pipeline run in the current shell process.
Switch to the pipeline with 'lastpipe' solution, and also set 'pipefail'
to catch errors from ${NM}.
Add the bash requirement to Documentation/process/changes.rst.
Fixes: 31cb50b5590f ("kbuild: check static EXPORT_SYMBOL* by script instead of modpost")
Reported-by: Tetsuo Handa <penguin-kernel@I-love.SAKURA.ne.jp>
Reported-by: Michael Ellerman <mpe@ellerman.id.au>
Reported-by: Wang Yugui <wangyugui@e16-tech.com>
Tested-by: Tetsuo Handa <penguin-kernel@I-love.SAKURA.ne.jp>
Tested-by: Jon Hunter <jonathanh@nvidia.com>
Acked-by: Nick Desaulniers <ndesaulniers@google.com>
Tested-by: Sedat Dilek <sedat.dilek@gmail.com> # LLVM-14 (x86-64)
Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>
2022-06-08 09:11:00 +08:00
|
|
|
# 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
|
|
|
|
# '2>/dev/null'. However, it suppresses real error messages as well. Add a
|
|
|
|
# hand-crafted error message here.
|
|
|
|
#
|
|
|
|
# TODO:
|
|
|
|
# Use --quiet instead of 2>/dev/null when we upgrade the minimum version of
|
|
|
|
# binutils to 2.37, llvm to 13.0.0.
|
2022-09-01 04:57:08 +08:00
|
|
|
# Then, the following line will be simpler:
|
|
|
|
# { ${NM} --quiet ${1} || kill 0; } |
|
|
|
|
|
|
|
|
{ ${NM} ${1} 2>/dev/null || { echo "${0}: ${NM} failed" >&2; kill $pid; } } |
|
|
|
|
${AWK} -v "file=${1}" '
|
|
|
|
BEGIN {
|
|
|
|
i = 0
|
|
|
|
}
|
|
|
|
|
|
|
|
# Skip the line if the number of fields is less than 3.
|
|
|
|
#
|
|
|
|
# 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
|
|
|
|
}
|
scripts/check-local-export: avoid 'wait $!' for process substitution
Bash 4.4, released in 2016, supports 'wait $!' to check the exit status
of a process substitution, but it seems too new.
Some people using older bash versions (on CentOS 7, Ubuntu 16.04, etc.)
reported an error like this:
./scripts/check-local-export: line 54: wait: pid 17328 is not a child of this shell
I used the process substitution to avoid a pipeline, which executes each
command in a subshell. If the while-loop is executed in the subshell
context, variable changes within are lost after the subshell terminates.
Fortunately, Bash 4.2, released in 2011, supports the 'lastpipe' option,
which makes the last element of a pipeline run in the current shell process.
Switch to the pipeline with 'lastpipe' solution, and also set 'pipefail'
to catch errors from ${NM}.
Add the bash requirement to Documentation/process/changes.rst.
Fixes: 31cb50b5590f ("kbuild: check static EXPORT_SYMBOL* by script instead of modpost")
Reported-by: Tetsuo Handa <penguin-kernel@I-love.SAKURA.ne.jp>
Reported-by: Michael Ellerman <mpe@ellerman.id.au>
Reported-by: Wang Yugui <wangyugui@e16-tech.com>
Tested-by: Tetsuo Handa <penguin-kernel@I-love.SAKURA.ne.jp>
Tested-by: Jon Hunter <jonathanh@nvidia.com>
Acked-by: Nick Desaulniers <ndesaulniers@google.com>
Tested-by: Sedat Dilek <sedat.dilek@gmail.com> # LLVM-14 (x86-64)
Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>
2022-06-08 09:11:00 +08:00
|
|
|
|
2022-09-01 04:57:08 +08:00
|
|
|
# save (name, type) in the associative array
|
|
|
|
{ symbol_types[$3]=$2 }
|
2022-05-27 18:01:51 +08:00
|
|
|
|
2022-09-01 04:57:08 +08:00
|
|
|
# append the exported symbol to the array
|
|
|
|
($3 ~ /^__ksymtab_/) {
|
|
|
|
export_symbols[i] = $3
|
|
|
|
sub(/^__ksymtab_/, "", export_symbols[i])
|
|
|
|
i++
|
|
|
|
}
|
2022-05-27 18:01:51 +08:00
|
|
|
|
2022-09-01 04:57:08 +08:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
2022-05-27 18:01:51 +08:00
|
|
|
|
2022-09-01 04:57:08 +08:00
|
|
|
exit exit_code
|
|
|
|
}'
|
2022-05-27 18:01:51 +08:00
|
|
|
|
2022-09-01 04:57:08 +08:00
|
|
|
exit $?
|