221-module_exports.patch 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. From b14784e7883390c20ed3ff904892255404a5914b Mon Sep 17 00:00:00 2001
  2. From: Felix Fietkau <[email protected]>
  3. Date: Fri, 7 Jul 2017 17:05:53 +0200
  4. Subject: add an optional config option for stripping all unnecessary symbol exports from the kernel image
  5. lede-commit: bb5a40c64b7c4f4848509fa0a6625055fc9e66cc
  6. Signed-off-by: Felix Fietkau <[email protected]>
  7. ---
  8. include/asm-generic/vmlinux.lds.h | 18 +++++++++++++++---
  9. include/linux/export.h | 9 ++++++++-
  10. scripts/Makefile.build | 2 +-
  11. 3 files changed, 24 insertions(+), 5 deletions(-)
  12. --- a/include/asm-generic/vmlinux.lds.h
  13. +++ b/include/asm-generic/vmlinux.lds.h
  14. @@ -54,6 +54,16 @@
  15. #define LOAD_OFFSET 0
  16. #endif
  17. +#ifndef SYMTAB_KEEP
  18. +#define SYMTAB_KEEP KEEP(*(SORT(___ksymtab+*)))
  19. +#define SYMTAB_KEEP_GPL KEEP(*(SORT(___ksymtab_gpl+*)))
  20. +#endif
  21. +
  22. +#ifndef SYMTAB_DISCARD
  23. +#define SYMTAB_DISCARD
  24. +#define SYMTAB_DISCARD_GPL
  25. +#endif
  26. +
  27. /* Align . to a 8 byte boundary equals to maximum function alignment. */
  28. #define ALIGN_FUNCTION() . = ALIGN(8)
  29. @@ -372,14 +382,14 @@
  30. /* Kernel symbol table: Normal symbols */ \
  31. __ksymtab : AT(ADDR(__ksymtab) - LOAD_OFFSET) { \
  32. __start___ksymtab = .; \
  33. - KEEP(*(SORT(___ksymtab+*))) \
  34. + SYMTAB_KEEP \
  35. __stop___ksymtab = .; \
  36. } \
  37. \
  38. /* Kernel symbol table: GPL-only symbols */ \
  39. __ksymtab_gpl : AT(ADDR(__ksymtab_gpl) - LOAD_OFFSET) { \
  40. __start___ksymtab_gpl = .; \
  41. - KEEP(*(SORT(___ksymtab_gpl+*))) \
  42. + SYMTAB_KEEP_GPL \
  43. __stop___ksymtab_gpl = .; \
  44. } \
  45. \
  46. @@ -441,7 +451,7 @@
  47. \
  48. /* Kernel symbol table: strings */ \
  49. __ksymtab_strings : AT(ADDR(__ksymtab_strings) - LOAD_OFFSET) { \
  50. - *(__ksymtab_strings) \
  51. + *(__ksymtab_strings+*) \
  52. } \
  53. \
  54. /* __*init sections */ \
  55. @@ -841,6 +851,8 @@
  56. EXIT_TEXT \
  57. EXIT_DATA \
  58. EXIT_CALL \
  59. + SYMTAB_DISCARD \
  60. + SYMTAB_DISCARD_GPL \
  61. *(.discard) \
  62. *(.discard.*) \
  63. }
  64. --- a/include/linux/export.h
  65. +++ b/include/linux/export.h
  66. @@ -74,12 +74,19 @@ struct kernel_symbol {
  67. };
  68. #endif
  69. +#ifdef MODULE
  70. +#define __EXPORT_SUFFIX(sym)
  71. +#else
  72. +#define __EXPORT_SUFFIX(sym) "+" #sym
  73. +#endif
  74. +
  75. /* For every exported symbol, place a struct in the __ksymtab section */
  76. #define ___EXPORT_SYMBOL(sym, sec) \
  77. extern typeof(sym) sym; \
  78. __CRC_SYMBOL(sym, sec) \
  79. static const char __kstrtab_##sym[] \
  80. - __attribute__((section("__ksymtab_strings"), used, aligned(1))) \
  81. + __attribute__((section("__ksymtab_strings" \
  82. + __EXPORT_SUFFIX(sym)), used, aligned(1))) \
  83. = #sym; \
  84. __KSYMTAB_ENTRY(sym, sec)
  85. --- a/scripts/Makefile.build
  86. +++ b/scripts/Makefile.build
  87. @@ -408,7 +408,7 @@ targets += $(extra-y) $(MAKECMDGOALS) $(
  88. # Linker scripts preprocessor (.lds.S -> .lds)
  89. # ---------------------------------------------------------------------------
  90. quiet_cmd_cpp_lds_S = LDS $@
  91. - cmd_cpp_lds_S = $(CPP) $(cpp_flags) -P -U$(ARCH) \
  92. + cmd_cpp_lds_S = $(CPP) $(EXTRA_LDSFLAGS) $(cpp_flags) -P -U$(ARCH) \
  93. -D__ASSEMBLY__ -DLINKER_SCRIPT -o $@ $<
  94. $(obj)/%.lds: $(src)/%.lds.S FORCE