Back to home page

OSCL-LXR

 
 

    


0001 #!/usr/bin/env bash
0002 # SPDX-License-Identifier: GPL-2.0-only
0003 #
0004 # Copyright (C) 2022 Masahiro Yamada <masahiroy@kernel.org>
0005 #
0006 # Exit with error if a local exported symbol is found.
0007 # EXPORT_SYMBOL should be used for global symbols.
0008 
0009 set -e
0010 
0011 # catch errors from ${NM}
0012 set -o pipefail
0013 
0014 # Run the last element of a pipeline in the current shell.
0015 # Without this, the while-loop would be executed in a subshell, and
0016 # the changes made to 'symbol_types' and 'export_symbols' would be lost.
0017 shopt -s lastpipe
0018 
0019 declare -A symbol_types
0020 declare -a export_symbols
0021 
0022 exit_code=0
0023 
0024 # If there is no symbol in the object, ${NM} (both GNU nm and llvm-nm) shows
0025 # 'no symbols' diagnostic (but exits with 0). It is harmless and hidden by
0026 # '2>/dev/null'. However, it suppresses real error messages as well. Add a
0027 # hand-crafted error message here.
0028 #
0029 # TODO:
0030 # Use --quiet instead of 2>/dev/null when we upgrade the minimum version of
0031 # binutils to 2.37, llvm to 13.0.0.
0032 # Then, the following line will be really simple:
0033 #   ${NM} --quiet ${1} |
0034 
0035 { ${NM} ${1} 2>/dev/null || { echo "${0}: ${NM} failed" >&2; false; } } |
0036 while read value type name
0037 do
0038         # Skip the line if the number of fields is less than 3.
0039         #
0040         # case 1)
0041         #   For undefined symbols, the first field (value) is empty.
0042         #   The outout looks like this:
0043         #     "                 U _printk"
0044         #   It is unneeded to record undefined symbols.
0045         #
0046         # case 2)
0047         #   For Clang LTO, llvm-nm outputs a line with type 't' but empty name:
0048         #     "---------------- t"
0049         if [[ -z ${name} ]]; then
0050                 continue
0051         fi
0052 
0053         # save (name, type) in the associative array
0054         symbol_types[${name}]=${type}
0055 
0056         # append the exported symbol to the array
0057         if [[ ${name} == __ksymtab_* ]]; then
0058                 export_symbols+=(${name#__ksymtab_})
0059         fi
0060 done
0061 
0062 for name in "${export_symbols[@]}"
0063 do
0064         # nm(3) says "If lowercase, the symbol is usually local"
0065         if [[ ${symbol_types[$name]} =~ [a-z] ]]; then
0066                 echo "$@: error: local symbol '${name}' was exported" >&2
0067                 exit_code=1
0068         fi
0069 done
0070 
0071 exit ${exit_code}