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