2
0

221-module_exports.patch 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 . to a 8 byte boundary equals to maximum function alignment. */
  28. #define ALIGN_FUNCTION() . = ALIGN(8)
  29. @@ -485,14 +495,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. @@ -512,7 +522,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. @@ -1022,6 +1032,8 @@
  56. #define COMMON_DISCARDS \
  57. SANITIZER_DISCARDS \
  58. + SYMTAB_DISCARD \
  59. + SYMTAB_DISCARD_GPL \
  60. *(.discard) \
  61. *(.discard.*) \
  62. *(.modinfo) \
  63. --- a/include/linux/export.h
  64. +++ b/include/linux/export.h
  65. @@ -84,6 +84,12 @@ struct kernel_symbol {
  66. #else
  67. +#ifdef MODULE
  68. +#define __EXPORT_SUFFIX(sym)
  69. +#else
  70. +#define __EXPORT_SUFFIX(sym) "+" #sym
  71. +#endif
  72. +
  73. /*
  74. * For every exported symbol, do the following:
  75. *
  76. @@ -101,7 +107,7 @@ struct kernel_symbol {
  77. extern const char __kstrtab_##sym[]; \
  78. extern const char __kstrtabns_##sym[]; \
  79. __CRC_SYMBOL(sym, sec); \
  80. - asm(" .section \"__ksymtab_strings\",\"aMS\",%progbits,1 \n" \
  81. + asm(" .section \"__ksymtab_strings" __EXPORT_SUFFIX(sym) "\",\"aMS\",%progbits,1 \n" \
  82. "__kstrtab_" #sym ": \n" \
  83. " .asciz \"" #sym "\" \n" \
  84. "__kstrtabns_" #sym ": \n" \
  85. --- a/include/asm-generic/export.h
  86. +++ b/include/asm-generic/export.h
  87. @@ -26,6 +26,12 @@
  88. #endif
  89. .endm
  90. +#ifdef MODULE
  91. +#define __EXPORT_SUFFIX(name)
  92. +#else
  93. +#define __EXPORT_SUFFIX(name) + #name
  94. +#endif
  95. +
  96. /*
  97. * note on .section use: we specify progbits since usage of the "M" (SHF_MERGE)
  98. * section flag requires it. Use '%progbits' instead of '@progbits' since the
  99. @@ -39,7 +45,7 @@
  100. __ksymtab_\name:
  101. __put \val, __kstrtab_\name
  102. .previous
  103. - .section __ksymtab_strings,"aMS",%progbits,1
  104. + .section __ksymtab_strings __EXPORT_SUFFIX(name),"aMS",%progbits,1
  105. __kstrtab_\name:
  106. .asciz "\name"
  107. .previous
  108. --- a/scripts/Makefile.build
  109. +++ b/scripts/Makefile.build
  110. @@ -397,7 +397,7 @@ targets += $(real-dtb-y) $(lib-y) $(alwa
  111. # Linker scripts preprocessor (.lds.S -> .lds)
  112. # ---------------------------------------------------------------------------
  113. quiet_cmd_cpp_lds_S = LDS $@
  114. - cmd_cpp_lds_S = $(CPP) $(cpp_flags) -P -U$(ARCH) \
  115. + cmd_cpp_lds_S = $(CPP) $(EXTRA_LDSFLAGS) $(cpp_flags) -P -U$(ARCH) \
  116. -D__ASSEMBLY__ -DLINKER_SCRIPT -o $@ $<
  117. $(obj)/%.lds: $(src)/%.lds.S FORCE