1 From e3d8676f5722b7622685581e06e8f53e6138e3ab Mon Sep 17 00:00:00 2001
2 From: Felix Fietkau <nbd@nbd.name>
3 Date: Sat, 15 Jul 2017 23:42:36 +0200
4 Subject: use -ffunction-sections, -fdata-sections and --gc-sections
6 In combination with kernel symbol export stripping this significantly reduces
7 the kernel image size. Used on both ARM and MIPS architectures.
9 Signed-off-by: Felix Fietkau <nbd@nbd.name>
10 Signed-off-by: Jonas Gorski <jogo@openwrt.org>
11 Signed-off-by: Gabor Juhos <juhosg@openwrt.org>
14 arch/arm/Kconfig | 1 +
15 arch/arm/boot/compressed/Makefile | 1 +
16 arch/arm/kernel/vmlinux.lds.S | 26 ++++++++--------
17 arch/mips/Kconfig | 1 +
18 arch/mips/kernel/vmlinux.lds.S | 4 +--
19 include/asm-generic/vmlinux.lds.h | 63 ++++++++++++++++++++-------------------
20 7 files changed, 55 insertions(+), 51 deletions(-)
24 @@ -272,6 +272,11 @@ else
25 scripts/Kbuild.include: ;
26 include scripts/Kbuild.include
28 +ifdef CONFIG_LD_DEAD_CODE_DATA_ELIMINATION
29 +KBUILD_CFLAGS_KERNEL += $(call cc-option,-ffunction-sections,)
30 +KBUILD_CFLAGS_KERNEL += $(call cc-option,-fdata-sections,)
33 # Read KERNELRELEASE from include/config/kernel.release (if it exists)
34 KERNELRELEASE = $(shell cat include/config/kernel.release 2> /dev/null)
35 KERNELVERSION = $(VERSION)$(if $(PATCHLEVEL),.$(PATCHLEVEL)$(if $(SUBLEVEL),.$(SUBLEVEL)))$(EXTRAVERSION)
36 @@ -785,11 +790,6 @@ ifdef CONFIG_DEBUG_SECTION_MISMATCH
37 KBUILD_CFLAGS += $(call cc-option, -fno-inline-functions-called-once)
40 -ifdef CONFIG_LD_DEAD_CODE_DATA_ELIMINATION
41 -KBUILD_CFLAGS += $(call cc-option,-ffunction-sections,)
42 -KBUILD_CFLAGS += $(call cc-option,-fdata-sections,)
45 # arch Makefile may override CC so keep this after arch Makefile is included
46 NOSTDINC_FLAGS += -nostdinc -isystem $(shell $(CC) -print-file-name=include)
47 CHECKFLAGS += $(NOSTDINC_FLAGS)
48 --- a/arch/arm/Kconfig
49 +++ b/arch/arm/Kconfig
50 @@ -92,6 +92,7 @@ config ARM
52 select HAVE_VIRT_CPU_ACCOUNTING_GEN
53 select IRQ_FORCED_THREADING
54 + select LD_DEAD_CODE_DATA_ELIMINATION
55 select MODULES_USE_ELF_REL
57 select OF_EARLY_FLATTREE if OF
58 --- a/arch/arm/boot/compressed/Makefile
59 +++ b/arch/arm/boot/compressed/Makefile
60 @@ -105,6 +105,7 @@ ifeq ($(CONFIG_FUNCTION_TRACER),y)
61 ORIG_CFLAGS := $(KBUILD_CFLAGS)
62 KBUILD_CFLAGS = $(subst -pg, , $(ORIG_CFLAGS))
64 +KBUILD_CFLAGS_KERNEL := $(patsubst -f%-sections,,$(KBUILD_CFLAGS_KERNEL))
66 # -fstack-protector-strong triggers protection checks in this code,
67 # but it is being used too early to link to meaningful stack_chk logic.
68 --- a/arch/arm/kernel/vmlinux.lds.S
69 +++ b/arch/arm/kernel/vmlinux.lds.S
73 VMLINUX_SYMBOL(__proc_info_begin) = .; \
74 - *(.proc.info.init) \
75 + KEEP(*(.proc.info.init)) \
76 VMLINUX_SYMBOL(__proc_info_end) = .;
78 #define HYPERVISOR_TEXT \
82 VMLINUX_SYMBOL(__idmap_text_start) = .; \
84 + KEEP(*(.idmap.text)) \
85 VMLINUX_SYMBOL(__idmap_text_end) = .; \
86 . = ALIGN(PAGE_SIZE); \
87 VMLINUX_SYMBOL(__hyp_idmap_text_start) = .; \
88 - *(.hyp.idmap.text) \
89 + KEEP(*(.hyp.idmap.text)) \
90 VMLINUX_SYMBOL(__hyp_idmap_text_end) = .;
92 #ifdef CONFIG_HOTPLUG_CPU
93 @@ -119,7 +119,7 @@ SECTIONS
94 _stext = .; /* Text and read-only data */
96 __exception_text_start = .;
98 + KEEP(*(.exception.text))
99 __exception_text_end = .;
102 @@ -148,7 +148,7 @@ SECTIONS
103 __ex_table : AT(ADDR(__ex_table) - LOAD_OFFSET) {
104 __start___ex_table = .;
107 + KEEP(*(__ex_table))
109 __stop___ex_table = .;
111 @@ -160,12 +160,12 @@ SECTIONS
114 __start_unwind_idx = .;
116 + KEEP(*(.ARM.exidx*))
117 __stop_unwind_idx = .;
120 __start_unwind_tab = .;
122 + KEEP(*(.ARM.extab*))
123 __stop_unwind_tab = .;
126 @@ -186,13 +186,13 @@ SECTIONS
128 OVERLAY 0xffff0000 : NOCROSSREFS AT(__vectors_lma) {
134 - *(.vectors.bhb.loop8)
135 + KEEP(*(.vectors.bhb.loop8))
137 .vectors.bhb.bpiall {
138 - *(.vectors.bhb.bpiall)
139 + KEEP(*(.vectors.bhb.bpiall))
142 ARM_LMA(__vectors, .vectors);
143 @@ -204,7 +204,7 @@ SECTIONS
146 .stubs ADDR(.vectors) + 0x1000 : AT(__stubs_lma) {
150 ARM_LMA(__stubs, .stubs);
151 . = __stubs_lma + SIZEOF(.stubs);
152 @@ -220,24 +220,24 @@ SECTIONS
155 __arch_info_begin = .;
157 + KEEP(*(.arch.info.init))
161 __tagtable_begin = .;
163 + KEEP(*(.taglist.init))
166 #ifdef CONFIG_SMP_ON_UP
170 + KEEP(*(.alt.smp.init))
175 __pv_table_begin = .;
181 --- a/arch/mips/Kconfig
182 +++ b/arch/mips/Kconfig
183 @@ -40,6 +40,7 @@ config MIPS
184 select HAVE_CBPF_JIT if (!64BIT && !CPU_MICROMIPS)
185 select HAVE_EBPF_JIT if (64BIT && !CPU_MICROMIPS)
186 select HAVE_CC_STACKPROTECTOR
187 + select LD_DEAD_CODE_DATA_ELIMINATION
188 select HAVE_CONTEXT_TRACKING
189 select HAVE_COPY_THREAD_TLS
190 select HAVE_C_RECORDMCOUNT
191 --- a/arch/mips/kernel/vmlinux.lds.S
192 +++ b/arch/mips/kernel/vmlinux.lds.S
193 @@ -72,7 +72,7 @@ SECTIONS
194 /* Exception table for data bus errors */
196 __start___dbe_table = .;
198 + KEEP(*(__dbe_table))
199 __stop___dbe_table = .;
202 @@ -124,7 +124,7 @@ SECTIONS
204 .mips.machines.init : AT(ADDR(.mips.machines.init) - LOAD_OFFSET) {
205 __mips_machines_start = .;
206 - *(.mips.machines.init)
207 + KEEP(*(.mips.machines.init))
208 __mips_machines_end = .;
211 --- a/include/asm-generic/vmlinux.lds.h
212 +++ b/include/asm-generic/vmlinux.lds.h
214 #ifdef CONFIG_FTRACE_MCOUNT_RECORD
215 #define MCOUNT_REC() . = ALIGN(8); \
216 VMLINUX_SYMBOL(__start_mcount_loc) = .; \
218 + KEEP(*(__mcount_loc)) \
219 VMLINUX_SYMBOL(__stop_mcount_loc) = .;
224 #ifdef CONFIG_TRACE_BRANCH_PROFILING
225 #define LIKELY_PROFILE() VMLINUX_SYMBOL(__start_annotated_branch_profile) = .; \
226 - *(_ftrace_annotated_branch) \
227 + KEEP(*(_ftrace_annotated_branch)) \
228 VMLINUX_SYMBOL(__stop_annotated_branch_profile) = .;
230 #define LIKELY_PROFILE()
233 #ifdef CONFIG_PROFILE_ALL_BRANCHES
234 #define BRANCH_PROFILE() VMLINUX_SYMBOL(__start_branch_profile) = .; \
235 - *(_ftrace_branch) \
236 + KEEP(*(_ftrace_branch)) \
237 VMLINUX_SYMBOL(__stop_branch_profile) = .;
239 #define BRANCH_PROFILE()
246 + *(.data.[a-zA-Z_]*)
249 * Data section helpers
253 VMLINUX_SYMBOL(__entry_text_start) = .; \
255 + KEEP(*(.entry.text)) \
256 VMLINUX_SYMBOL(__entry_text_end) = .;
258 #define IRQENTRY_TEXT \
260 . = ALIGN(sbss_align); \
261 .sbss : AT(ADDR(.sbss) - LOAD_OFFSET) { \