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 declare -A symbol_types
12 declare -a export_symbols
16 while read value type name
18 # Skip the line if the number of fields is less than 3.
21 # For undefined symbols, the first field (value) is empty.
22 # The outout looks like this:
24 # It is unneeded to record undefined symbols.
27 # For Clang LTO, llvm-nm outputs a line with type 't' but empty name:
28 # "---------------- t"
29 if [[ -z ${name} ]]; then
33 # save (name, type) in the associative array
34 symbol_types[${name}]=${type}
36 # append the exported symbol to the array
37 if [[ ${name} == __ksymtab_* ]]; then
38 export_symbols+=(${name#__ksymtab_})
41 # If there is no symbol in the object, ${NM} (both GNU nm and llvm-nm)
42 # shows 'no symbols' diagnostic (but exits with 0). It is harmless and
43 # hidden by '2>/dev/null'. However, it suppresses real error messages
44 # as well. Add a hand-crafted error message here.
46 # Use --quiet instead of 2>/dev/null when we upgrade the minimum version
47 # of binutils to 2.37, llvm to 13.0.0.
49 # Then, the following line will be really simple:
50 # done < <(${NM} --quiet ${1})
51 done < <(${NM} ${1} 2>/dev/null || { echo "${0}: ${NM} failed" >&2; false; } )
53 # Catch error in the process substitution
56 for name in "${export_symbols[@]}"
58 # nm(3) says "If lowercase, the symbol is usually local"
59 if [[ ${symbol_types[$name]} =~ [a-z] ]]; then
60 echo "$@: error: local symbol '${name}' was exported" >&2