headers_install.sh 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. #!/bin/sh
  2. # SPDX-License-Identifier: GPL-2.0
  3. if [ $# -ne 2 ]
  4. then
  5. echo "Usage: headers_install.sh INFILE OUTFILE"
  6. echo
  7. echo "Prepares kernel header files for use by user space, by removing"
  8. echo "all compiler.h definitions and #includes, removing any"
  9. echo "#ifdef __KERNEL__ sections, and putting __underscores__ around"
  10. echo "asm/inline/volatile keywords."
  11. echo
  12. echo "INFILE: header file to operate on"
  13. echo "OUTFILE: output file which the processed header is written to"
  14. exit 1
  15. fi
  16. # Grab arguments
  17. INFILE=$1
  18. OUTFILE=$2
  19. TMPFILE=$OUTFILE.tmp
  20. trap 'rm -f $OUTFILE $TMPFILE' EXIT
  21. # SPDX-License-Identifier with GPL variants must have "WITH Linux-syscall-note"
  22. if [ -n "$(sed -n -e "/SPDX-License-Identifier:.*GPL-/{/WITH Linux-syscall-note/!p}" $INFILE)" ]; then
  23. echo "error: $INFILE: missing \"WITH Linux-syscall-note\" for SPDX-License-Identifier" >&2
  24. exit 1
  25. fi
  26. sed -E -e '
  27. s/([[:space:](])(__user|__force|__iomem)[[:space:]]/\1/g
  28. s/__attribute_const__([[:space:]]|$)/\1/g
  29. s@^#include <linux/compiler(|_types).h>@@
  30. s/(^|[^a-zA-Z0-9])__packed([^a-zA-Z0-9_]|$)/\1__attribute__((packed))\2/g
  31. s/(^|[[:space:](])(inline|asm|volatile)([[:space:](]|$)/\1__\2__\3/g
  32. s@#(ifndef|define|endif[[:space:]]*/[*])[[:space:]]*_UAPI@#\1 @
  33. ' $INFILE > $TMPFILE || exit 1
  34. scripts/unifdef -U__KERNEL__ -D__EXPORTED_HEADERS__ $TMPFILE > $OUTFILE
  35. [ $? -gt 1 ] && exit 1
  36. # Remove /* ... */ style comments, and find CONFIG_ references in code
  37. configs=$(sed -e '
  38. :comment
  39. s:/\*[^*][^*]*:/*:
  40. s:/\*\*\**\([^/]\):/*\1:
  41. t comment
  42. s:/\*\*/: :
  43. t comment
  44. /\/\*/! b check
  45. N
  46. b comment
  47. :print
  48. P
  49. D
  50. :check
  51. s:^\(CONFIG_[[:alnum:]_]*\):\1\n:
  52. t print
  53. s:^[[:alnum:]_][[:alnum:]_]*::
  54. s:^[^[:alnum:]_][^[:alnum:]_]*::
  55. t check
  56. d
  57. ' $OUTFILE)
  58. # The entries in the following list do not result in an error.
  59. # Please do not add a new entry. This list is only for existing ones.
  60. # The list will be reduced gradually, and deleted eventually. (hopefully)
  61. #
  62. # The format is <file-name>:<CONFIG-option> in each line.
  63. config_leak_ignores="
  64. arch/arc/include/uapi/asm/page.h:CONFIG_ARC_PAGE_SIZE_16K
  65. arch/arc/include/uapi/asm/page.h:CONFIG_ARC_PAGE_SIZE_4K
  66. arch/arc/include/uapi/asm/swab.h:CONFIG_ARC_HAS_SWAPE
  67. arch/arm/include/uapi/asm/ptrace.h:CONFIG_CPU_ENDIAN_BE8
  68. arch/nios2/include/uapi/asm/swab.h:CONFIG_NIOS2_CI_SWAB_NO
  69. arch/nios2/include/uapi/asm/swab.h:CONFIG_NIOS2_CI_SWAB_SUPPORT
  70. arch/x86/include/uapi/asm/auxvec.h:CONFIG_IA32_EMULATION
  71. arch/x86/include/uapi/asm/auxvec.h:CONFIG_X86_64
  72. "
  73. for c in $configs
  74. do
  75. leak_error=1
  76. for ignore in $config_leak_ignores
  77. do
  78. if echo "$INFILE:$c" | grep -q "$ignore$"; then
  79. leak_error=
  80. break
  81. fi
  82. done
  83. if [ "$leak_error" = 1 ]; then
  84. echo "error: $INFILE: leak $c to user-space" >&2
  85. exit 1
  86. fi
  87. done
  88. rm -f $TMPFILE
  89. trap - EXIT