221-module_exports.patch 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. @@ -81,6 +81,16 @@
  15. #define RO_EXCEPTION_TABLE
  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 . function alignment. */
  28. #define ALIGN_FUNCTION() . = ALIGN(CONFIG_FUNCTION_ALIGNMENT)
  29. @@ -486,14 +496,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. @@ -513,7 +523,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. @@ -1000,6 +1010,8 @@
  56. #define COMMON_DISCARDS \
  57. SANITIZER_DISCARDS \
  58. PATCHABLE_DISCARDS \
  59. + SYMTAB_DISCARD \
  60. + SYMTAB_DISCARD_GPL \
  61. *(.discard) \
  62. *(.discard.*) \
  63. *(.export_symbol) \
  64. --- a/include/linux/export-internal.h
  65. +++ b/include/linux/export-internal.h
  66. @@ -26,6 +26,12 @@
  67. #define __KSYM_REF(sym) ".long " #sym
  68. #endif
  69. +#ifdef MODULE
  70. +#define __EXPORT_SUFFIX(sym)
  71. +#else
  72. +#define __EXPORT_SUFFIX(sym) "+" #sym
  73. +#endif
  74. +
  75. /*
  76. * For every exported symbol, do the following:
  77. *
  78. @@ -38,7 +44,7 @@
  79. * former apparently works on all arches according to the binutils source.
  80. */
  81. #define __KSYMTAB(name, sym, sec, ns) \
  82. - asm(" .section \"__ksymtab_strings\",\"aMS\",%progbits,1" "\n" \
  83. + asm(" .section \"__ksymtab_strings" __EXPORT_SUFFIX(sym) "\",\"aMS\",%progbits,1" "\n" \
  84. "__kstrtab_" #name ":" "\n" \
  85. " .asciz \"" #name "\"" "\n" \
  86. "__kstrtabns_" #name ":" "\n" \
  87. --- a/scripts/Makefile.build
  88. +++ b/scripts/Makefile.build
  89. @@ -366,7 +366,7 @@ targets += $(real-dtb-y) $(lib-y) $(alwa
  90. # Linker scripts preprocessor (.lds.S -> .lds)
  91. # ---------------------------------------------------------------------------
  92. quiet_cmd_cpp_lds_S = LDS $@
  93. - cmd_cpp_lds_S = $(CPP) $(cpp_flags) -P -U$(ARCH) \
  94. + cmd_cpp_lds_S = $(CPP) $(EXTRA_LDSFLAGS) $(cpp_flags) -P -U$(ARCH) \
  95. -D__ASSEMBLY__ -DLINKER_SCRIPT -o $@ $<
  96. $(obj)/%.lds: $(src)/%.lds.S FORCE