check-local-export 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. #!/bin/sh
  2. # SPDX-License-Identifier: GPL-2.0-only
  3. #
  4. # Copyright (C) 2022 Masahiro Yamada <[email protected]>
  5. # Copyright (C) 2022 Owen Rafferty <[email protected]>
  6. #
  7. # Exit with error if a local exported symbol is found.
  8. # EXPORT_SYMBOL should be used for global symbols.
  9. set -e
  10. pid=$$
  11. # If there is no symbol in the object, ${NM} (both GNU nm and llvm-nm) shows
  12. # 'no symbols' diagnostic (but exits with 0). It is harmless and hidden by
  13. # '2>/dev/null'. However, it suppresses real error messages as well. Add a
  14. # hand-crafted error message here.
  15. #
  16. # TODO:
  17. # Use --quiet instead of 2>/dev/null when we upgrade the minimum version of
  18. # binutils to 2.37, llvm to 13.0.0.
  19. # Then, the following line will be simpler:
  20. # { ${NM} --quiet ${1} || kill 0; } |
  21. { ${NM} ${1} 2>/dev/null || { echo "${0}: ${NM} failed" >&2; kill $pid; } } |
  22. ${AWK} -v "file=${1}" '
  23. BEGIN {
  24. i = 0
  25. }
  26. # Skip the line if the number of fields is less than 3.
  27. #
  28. # case 1)
  29. # For undefined symbols, the first field (value) is empty.
  30. # The outout looks like this:
  31. # " U _printk"
  32. # It is unneeded to record undefined symbols.
  33. #
  34. # case 2)
  35. # For Clang LTO, llvm-nm outputs a line with type t but empty name:
  36. # "---------------- t"
  37. !length($3) {
  38. next
  39. }
  40. # save (name, type) in the associative array
  41. { symbol_types[$3]=$2 }
  42. # append the exported symbol to the array
  43. ($3 ~ /^__ksymtab_/) {
  44. export_symbols[i] = $3
  45. sub(/^__ksymtab_/, "", export_symbols[i])
  46. i++
  47. }
  48. END {
  49. exit_code = 0
  50. for (j = 0; j < i; ++j) {
  51. name = export_symbols[j]
  52. # nm(3) says "If lowercase, the symbol is usually local"
  53. if (symbol_types[name] ~ /[a-z]/) {
  54. printf "%s: error: local symbol %s was exported\n",
  55. file, name | "cat 1>&2"
  56. exit_code = 1
  57. }
  58. }
  59. exit exit_code
  60. }'
  61. exit $?