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
6 lede-commit: bb5a40c64b7c4f4848509fa0a6625055fc9e66cc
7 Signed-off-by: Felix Fietkau <nbd@nbd.name>
9 include/asm-generic/vmlinux.lds.h | 18 +++++++++++++++---
10 include/linux/export.h | 9 ++++++++-
11 scripts/Makefile.build | 2 +-
12 3 files changed, 24 insertions(+), 5 deletions(-)
14 --- a/include/asm-generic/vmlinux.lds.h
15 +++ b/include/asm-generic/vmlinux.lds.h
17 #define RO_EXCEPTION_TABLE
21 +#define SYMTAB_KEEP KEEP(*(SORT(___ksymtab+*)))
22 +#define SYMTAB_KEEP_GPL KEEP(*(SORT(___ksymtab_gpl+*)))
25 +#ifndef SYMTAB_DISCARD
26 +#define SYMTAB_DISCARD
27 +#define SYMTAB_DISCARD_GPL
30 /* Align . to a 8 byte boundary equals to maximum function alignment. */
31 #define ALIGN_FUNCTION() . = ALIGN(8)
34 /* Kernel symbol table: Normal symbols */ \
35 __ksymtab : AT(ADDR(__ksymtab) - LOAD_OFFSET) { \
36 __start___ksymtab = .; \
37 - KEEP(*(SORT(___ksymtab+*))) \
39 __stop___ksymtab = .; \
42 /* Kernel symbol table: GPL-only symbols */ \
43 __ksymtab_gpl : AT(ADDR(__ksymtab_gpl) - LOAD_OFFSET) { \
44 __start___ksymtab_gpl = .; \
45 - KEEP(*(SORT(___ksymtab_gpl+*))) \
47 __stop___ksymtab_gpl = .; \
52 /* Kernel symbol table: strings */ \
53 __ksymtab_strings : AT(ADDR(__ksymtab_strings) - LOAD_OFFSET) { \
54 - *(__ksymtab_strings) \
55 + *(__ksymtab_strings+*) \
58 /* __*init sections */ \
61 #define COMMON_DISCARDS \
64 + SYMTAB_DISCARD_GPL \
68 --- a/include/linux/export.h
69 +++ b/include/linux/export.h
70 @@ -82,6 +82,12 @@ struct kernel_symbol {
75 +#define __EXPORT_SUFFIX(sym)
77 +#define __EXPORT_SUFFIX(sym) "+" #sym
81 * For every exported symbol, do the following:
83 @@ -99,7 +105,7 @@ struct kernel_symbol {
84 extern const char __kstrtab_##sym[]; \
85 extern const char __kstrtabns_##sym[]; \
86 __CRC_SYMBOL(sym, sec); \
87 - asm(" .section \"__ksymtab_strings\",\"aMS\",%progbits,1 \n" \
88 + asm(" .section \"__ksymtab_strings" __EXPORT_SUFFIX(sym) "\",\"aMS\",%progbits,1 \n" \
89 "__kstrtab_" #sym ": \n" \
90 " .asciz \"" #sym "\" \n" \
91 "__kstrtabns_" #sym ": \n" \
92 --- a/include/asm-generic/export.h
93 +++ b/include/asm-generic/export.h
99 +#define __EXPORT_SUFFIX(name)
101 +#define __EXPORT_SUFFIX(name) + #name
105 * note on .section use: we specify progbits since usage of the "M" (SHF_MERGE)
106 * section flag requires it. Use '%progbits' instead of '@progbits' since the
109 __put \val, __kstrtab_\name
111 - .section __ksymtab_strings,"aMS",%progbits,1
112 + .section __ksymtab_strings __EXPORT_SUFFIX(name),"aMS",%progbits,1
116 --- a/scripts/Makefile.build
117 +++ b/scripts/Makefile.build
118 @@ -367,7 +367,7 @@ targets += $(lib-y) $(always-y) $(MAKECM
119 # Linker scripts preprocessor (.lds.S -> .lds)
120 # ---------------------------------------------------------------------------
121 quiet_cmd_cpp_lds_S = LDS $@
122 - cmd_cpp_lds_S = $(CPP) $(cpp_flags) -P -U$(ARCH) \
123 + cmd_cpp_lds_S = $(CPP) $(EXTRA_LDSFLAGS) $(cpp_flags) -P -U$(ARCH) \
124 -D__ASSEMBLY__ -DLINKER_SCRIPT -o $@ $<
126 $(obj)/%.lds: $(src)/%.lds.S FORCE