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 @@ -108,6 +108,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 @@ -73,7 +73,7 @@ SECTIONS
46 __ex_table : AT(ADDR(__ex_table) - LOAD_OFFSET) {
47 __start___ex_table = .;
48 - ARM_MMU_KEEP(*(__ex_table))
50 __stop___ex_table = .;
53 @@ -100,24 +100,24 @@ SECTIONS
56 __arch_info_begin = .;
58 + KEEP(*(.arch.info.init))
64 + KEEP(*(.taglist.init))
67 #ifdef CONFIG_SMP_ON_UP
71 + KEEP(*(.alt.smp.init))
82 --- a/arch/arm/kernel/vmlinux.lds.h
83 +++ b/arch/arm/kernel/vmlinux.lds.h
87 __proc_info_begin = .; \
88 - *(.proc.info.init) \
89 + KEEP(*(.proc.info.init)) \
92 #define HYPERVISOR_TEXT \
96 __idmap_text_start = .; \
98 + KEEP(*(.idmap.text)) \
99 __idmap_text_end = .; \
100 . = ALIGN(PAGE_SIZE); \
101 __hyp_idmap_text_start = .; \
102 - *(.hyp.idmap.text) \
103 + KEEP(*(.hyp.idmap.text)) \
104 __hyp_idmap_text_end = .;
106 #define ARM_DISCARD \
109 .ARM.unwind_idx : { \
110 __start_unwind_idx = .; \
112 + KEEP(*(.ARM.exidx*)) \
113 __stop_unwind_idx = .; \
115 .ARM.unwind_tab : { \
116 __start_unwind_tab = .; \
118 + KEEP(*(.ARM.extab*)) \
119 __stop_unwind_tab = .; \
122 @@ -102,14 +102,14 @@
123 #define ARM_VECTORS \
124 __vectors_start = .; \
125 .vectors 0xffff0000 : AT(__vectors_start) { \
127 + KEEP(*(.vectors)) \
129 . = __vectors_start + SIZEOF(.vectors); \
133 .stubs ADDR(.vectors) + 0x1000 : AT(__stubs_start) { \
137 . = __stubs_start + SIZEOF(.stubs); \