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(-)
22 --- a/arch/arm/Kconfig
23 +++ b/arch/arm/Kconfig
24 @@ -111,6 +111,7 @@ config ARM
26 select HAVE_VIRT_CPU_ACCOUNTING_GEN
27 select IRQ_FORCED_THREADING
28 + select HAVE_LD_DEAD_CODE_DATA_ELIMINATION
29 select MODULES_USE_ELF_REL
30 select NEED_DMA_MAP_STATE
31 select OF_EARLY_FLATTREE if OF
32 --- a/arch/arm/boot/compressed/Makefile
33 +++ b/arch/arm/boot/compressed/Makefile
34 @@ -106,6 +106,7 @@ ifeq ($(CONFIG_FUNCTION_TRACER),y)
35 ORIG_CFLAGS := $(KBUILD_CFLAGS)
36 KBUILD_CFLAGS = $(subst -pg, , $(ORIG_CFLAGS))
38 +KBUILD_CFLAGS_KERNEL := $(patsubst -f%-sections,,$(KBUILD_CFLAGS_KERNEL))
40 # -fstack-protector-strong triggers protection checks in this code,
41 # but it is being used too early to link to meaningful stack_chk logic.
42 --- a/arch/arm/kernel/vmlinux.lds.S
43 +++ b/arch/arm/kernel/vmlinux.lds.S
44 @@ -100,24 +100,24 @@ SECTIONS
47 __arch_info_begin = .;
49 + KEEP(*(.arch.info.init))
55 + KEEP(*(.taglist.init))
58 #ifdef CONFIG_SMP_ON_UP
62 + KEEP(*(.alt.smp.init))
73 --- a/arch/arm/kernel/vmlinux.lds.h
74 +++ b/arch/arm/kernel/vmlinux.lds.h
76 #define ARM_MMU_DISCARD(x)
78 #define ARM_MMU_KEEP(x)
79 -#define ARM_MMU_DISCARD(x) x
80 +#define ARM_MMU_DISCARD(x) KEEP(x)
85 __proc_info_begin = .; \
86 - *(.proc.info.init) \
87 + KEEP(*(.proc.info.init)) \
90 #define HYPERVISOR_TEXT \
94 __idmap_text_start = .; \
96 + KEEP(*(.idmap.text)) \
97 __idmap_text_end = .; \
98 . = ALIGN(PAGE_SIZE); \
99 __hyp_idmap_text_start = .; \
100 - *(.hyp.idmap.text) \
101 + KEEP(*(.hyp.idmap.text)) \
102 __hyp_idmap_text_end = .;
104 #define ARM_DISCARD \
107 .ARM.unwind_idx : { \
108 __start_unwind_idx = .; \
110 + KEEP(*(.ARM.exidx*)) \
111 __stop_unwind_idx = .; \
113 .ARM.unwind_tab : { \
114 __start_unwind_tab = .; \
116 + KEEP(*(.ARM.extab*)) \
117 __stop_unwind_tab = .; \
120 @@ -102,14 +102,14 @@
121 #define ARM_VECTORS \
122 __vectors_start = .; \
123 .vectors 0xffff0000 : AT(__vectors_start) { \
125 + KEEP(*(.vectors)) \
127 . = __vectors_start + SIZEOF(.vectors); \
131 .stubs ADDR(.vectors) + 0x1000 : AT(__stubs_start) { \
135 . = __stubs_start + SIZEOF(.stubs); \