Merge tag 'bitmap-6.0-rc1' of https://github.com/norov/linux
[linux-2.6-microblaze.git] / scripts / check-local-export
1 #!/usr/bin/env bash
2 # SPDX-License-Identifier: GPL-2.0-only
3 #
4 # Copyright (C) 2022 Masahiro Yamada <masahiroy@kernel.org>
5 #
6 # Exit with error if a local exported symbol is found.
7 # EXPORT_SYMBOL should be used for global symbols.
8
9 set -e
10
11 # catch errors from ${NM}
12 set -o pipefail
13
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.
17 shopt -s lastpipe
18
19 declare -A symbol_types
20 declare -a export_symbols
21
22 exit_code=0
23
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.
28 #
29 # TODO:
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} |
34
35 { ${NM} ${1} 2>/dev/null || { echo "${0}: ${NM} failed" >&2; false; } } |
36 while read value type name
37 do
38         # Skip the line if the number of fields is less than 3.
39         #
40         # case 1)
41         #   For undefined symbols, the first field (value) is empty.
42         #   The outout looks like this:
43         #     "                 U _printk"
44         #   It is unneeded to record undefined symbols.
45         #
46         # case 2)
47         #   For Clang LTO, llvm-nm outputs a line with type 't' but empty name:
48         #     "---------------- t"
49         if [[ -z ${name} ]]; then
50                 continue
51         fi
52
53         # save (name, type) in the associative array
54         symbol_types[${name}]=${type}
55
56         # append the exported symbol to the array
57         if [[ ${name} == __ksymtab_* ]]; then
58                 export_symbols+=(${name#__ksymtab_})
59         fi
60 done
61
62 for name in "${export_symbols[@]}"
63 do
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
67                 exit_code=1
68         fi
69 done
70
71 exit ${exit_code}