check-local-export (1993B)
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 9set -e 10 11# catch errors from ${NM} 12set -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. 17shopt -s lastpipe 18 19declare -A symbol_types 20declare -a export_symbols 21 22exit_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; } } | 36while read value type name 37do 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 60done 61 62for name in "${export_symbols[@]}" 63do 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 69done 70 71exit ${exit_code}