prom_init_check.sh 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #!/bin/sh
  2. # SPDX-License-Identifier: GPL-2.0-or-later
  3. #
  4. # Copyright © 2008 IBM Corporation
  5. #
  6. # This script checks prom_init.o to see what external symbols it
  7. # is using, if it finds symbols not in the whitelist it returns
  8. # an error. The point of this is to discourage people from
  9. # intentionally or accidentally adding new code to prom_init.c
  10. # which has side effects on other parts of the kernel.
  11. # If you really need to reference something from prom_init.o add
  12. # it to the list below:
  13. has_renamed_memintrinsics()
  14. {
  15. grep -q "^CONFIG_KASAN=y$" ${KCONFIG_CONFIG} && \
  16. ! grep -q "^CONFIG_CC_HAS_KASAN_MEMINTRINSIC_PREFIX=y" ${KCONFIG_CONFIG}
  17. }
  18. if has_renamed_memintrinsics
  19. then
  20. MEM_FUNCS="__memcpy __memset"
  21. else
  22. MEM_FUNCS="memcpy memset"
  23. fi
  24. WHITELIST="add_reloc_offset __bss_start __bss_stop copy_and_flush
  25. _end enter_prom $MEM_FUNCS reloc_offset __secondary_hold
  26. __secondary_hold_acknowledge __secondary_hold_spinloop __start
  27. logo_linux_clut224 btext_prepare_BAT
  28. reloc_got2 kernstart_addr memstart_addr linux_banner _stext
  29. btext_setup_display TOC. relocate"
  30. NM="$1"
  31. OBJ="$2"
  32. ERROR=0
  33. check_section()
  34. {
  35. file=$1
  36. section=$2
  37. size=$(objdump -h -j $section $file 2>/dev/null | awk "\$2 == \"$section\" {print \$3}")
  38. size=${size:-0}
  39. if [ $size -ne 0 ]; then
  40. ERROR=1
  41. echo "Error: Section $section not empty in prom_init.c" >&2
  42. fi
  43. }
  44. for UNDEF in $($NM -u $OBJ | awk '{print $2}')
  45. do
  46. # On 64-bit nm gives us the function descriptors, which have
  47. # a leading . on the name, so strip it off here.
  48. UNDEF="${UNDEF#.}"
  49. case "$KBUILD_VERBOSE" in
  50. *1*)
  51. echo "Checking prom_init.o symbol '$UNDEF'" ;;
  52. esac
  53. OK=0
  54. for WHITE in $WHITELIST
  55. do
  56. if [ "$UNDEF" = "$WHITE" ]; then
  57. OK=1
  58. break
  59. fi
  60. done
  61. # ignore register save/restore funcitons
  62. case $UNDEF in
  63. _restgpr_*|_restgpr0_*|_rest32gpr_*)
  64. OK=1
  65. ;;
  66. _savegpr_*|_savegpr0_*|_save32gpr_*)
  67. OK=1
  68. ;;
  69. esac
  70. if [ $OK -eq 0 ]; then
  71. ERROR=1
  72. echo "Error: External symbol '$UNDEF' referenced" \
  73. "from prom_init.c" >&2
  74. fi
  75. done
  76. check_section $OBJ .data
  77. check_section $OBJ .bss
  78. check_section $OBJ .init.data
  79. exit $ERROR