2 # SPDX-License-Identifier: GPL-2.0-only
4 # Copyright (C) 2022 Masahiro Yamada <masahiroy@kernel.org>
6 # Exit with error if a local exported symbol is found.
7 # EXPORT_SYMBOL should be used for global symbols.
11 # catch errors from ${NM}
14 # Run the last element of a pipeline in the current shell.
15 # Without this, the while-loop would be executed in a subshell, and
16 # the changes made to 'symbol_types' and 'export_symbols' would be lost.
19 declare -A symbol_types
20 declare -a export_symbols
24 # If there is no symbol in the object, ${NM} (both GNU nm and llvm-nm) shows
25 # 'no symbols' diagnostic (but exits with 0). It is harmless and hidden by
26 # '2>/dev/null'. However, it suppresses real error messages as well. Add a
27 # hand-crafted error message here.
30 # Use --quiet instead of 2>/dev/null when we upgrade the minimum version of
31 # binutils to 2.37, llvm to 13.0.0.
32 # Then, the following line will be really simple:
33 # ${NM} --quiet ${1} |
35 { ${NM} ${1} 2>/dev/null || { echo "${0}: ${NM} failed" >&2; false; } } |
36 while read value type name
38 # Skip the line if the number of fields is less than 3.
41 # For undefined symbols, the first field (value) is empty.
42 # The outout looks like this:
44 # It is unneeded to record undefined symbols.
47 # For Clang LTO, llvm-nm outputs a line with type 't' but empty name:
48 # "---------------- t"
49 if [[ -z ${name} ]]; then
53 # save (name, type) in the associative array
54 symbol_types[${name}]=${type}
56 # append the exported symbol to the array
57 if [[ ${name} == __ksymtab_* ]]; then
58 export_symbols+=(${name#__ksymtab_})
62 for name in "${export_symbols[@]}"
64 # nm(3) says "If lowercase, the symbol is usually local"
65 if [[ ${symbol_types[$name]} =~ [a-z] ]]; then
66 echo "$@: error: local symbol '${name}' was exported" >&2