Commit | Line | Data |
---|---|---|
42ce60aa | 1 | #!/usr/bin/env bash |
31cb50b5 MY |
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 | declare -A symbol_types | |
12 | declare -a export_symbols | |
13 | ||
14 | exit_code=0 | |
15 | ||
16 | while read value type name | |
17 | do | |
18 | # Skip the line if the number of fields is less than 3. | |
19 | # | |
20 | # case 1) | |
21 | # For undefined symbols, the first field (value) is empty. | |
22 | # The outout looks like this: | |
23 | # " U _printk" | |
24 | # It is unneeded to record undefined symbols. | |
25 | # | |
26 | # case 2) | |
27 | # For Clang LTO, llvm-nm outputs a line with type 't' but empty name: | |
28 | # "---------------- t" | |
29 | if [[ -z ${name} ]]; then | |
30 | continue | |
31 | fi | |
32 | ||
33 | # save (name, type) in the associative array | |
34 | symbol_types[${name}]=${type} | |
35 | ||
36 | # append the exported symbol to the array | |
37 | if [[ ${name} == __ksymtab_* ]]; then | |
38 | export_symbols+=(${name#__ksymtab_}) | |
39 | fi | |
40 | ||
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. | |
45 | # | |
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. | |
48 | # | |
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; } ) | |
52 | ||
53 | # Catch error in the process substitution | |
54 | wait $! | |
55 | ||
56 | for name in "${export_symbols[@]}" | |
57 | do | |
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 | |
61 | exit_code=1 | |
62 | fi | |
63 | done | |
64 | ||
65 | exit ${exit_code} |