221-module_exports.patch 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. From b14784e7883390c20ed3ff904892255404a5914b Mon Sep 17 00:00:00 2001
  2. From: Felix Fietkau <nbd@nbd.name>
  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 <nbd@nbd.name>
  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. #include <linux/export.h>
  28. /* Align . to a 8 byte boundary equals to maximum function alignment. */
  29. @@ -342,14 +352,14 @@
  30. /* Kernel symbol table: Normal symbols */ \
  31. __ksymtab : AT(ADDR(__ksymtab) - LOAD_OFFSET) { \
  32. VMLINUX_SYMBOL(__start___ksymtab) = .; \
  33. - KEEP(*(SORT(___ksymtab+*))) \
  34. + SYMTAB_KEEP \
  35. VMLINUX_SYMBOL(__stop___ksymtab) = .; \
  36. } \
  37. \
  38. /* Kernel symbol table: GPL-only symbols */ \
  39. __ksymtab_gpl : AT(ADDR(__ksymtab_gpl) - LOAD_OFFSET) { \
  40. VMLINUX_SYMBOL(__start___ksymtab_gpl) = .; \
  41. - KEEP(*(SORT(___ksymtab_gpl+*))) \
  42. + SYMTAB_KEEP_GPL \
  43. VMLINUX_SYMBOL(__stop___ksymtab_gpl) = .; \
  44. } \
  45. \
  46. @@ -411,7 +421,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. @@ -805,6 +815,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. @@ -60,12 +60,19 @@ extern struct module __this_module;
  67. #define __CRC_SYMBOL(sym, sec)
  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"), aligned(1))) \
  81. + __attribute__((section("__ksymtab_strings" \
  82. + __EXPORT_SUFFIX(sym)), aligned(1))) \
  83. = VMLINUX_SYMBOL_STR(sym); \
  84. static const struct kernel_symbol __ksymtab_##sym \
  85. __used \
  86. --- a/scripts/Makefile.build
  87. +++ b/scripts/Makefile.build
  88. @@ -435,7 +435,7 @@ targets += $(extra-y) $(MAKECMDGOALS) $(
  89. # Linker scripts preprocessor (.lds.S -> .lds)
  90. # ---------------------------------------------------------------------------
  91. quiet_cmd_cpp_lds_S = LDS $@
  92. - cmd_cpp_lds_S = $(CPP) $(cpp_flags) -P -U$(ARCH) \
  93. + cmd_cpp_lds_S = $(CPP) $(EXTRA_LDSFLAGS) $(cpp_flags) -P -U$(ARCH) \
  94. -D__ASSEMBLY__ -DLINKER_SCRIPT -o $@ $<
  95. $(obj)/%.lds: $(src)/%.lds.S FORCE