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 @@ -294,6 +294,11 @@ else
25 scripts/Kbuild.include: ;
26 include scripts/Kbuild.include
28 +ifdef CONFIG_LD_DEAD_CODE_DATA_ELIMINATION
29 +KBUILD_CFLAGS_KERNEL += -ffunction-sections -fdata-sections
30 +LDFLAGS_vmlinux += --gc-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 @@ -782,11 +787,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_KERNEL += -ffunction-sections -fdata-sections
42 -LDFLAGS_vmlinux += --gc-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)
48 --- a/arch/arm/Kconfig
49 +++ b/arch/arm/Kconfig
50 @@ -98,6 +98,7 @@ config ARM
52 select HAVE_VIRT_CPU_ACCOUNTING_GEN
53 select IRQ_FORCED_THREADING
54 + select HAVE_LD_DEAD_CODE_DATA_ELIMINATION
55 select MODULES_USE_ELF_REL
56 select NEED_DMA_MAP_STATE
58 --- a/arch/arm/boot/compressed/Makefile
59 +++ b/arch/arm/boot/compressed/Makefile
60 @@ -106,6 +106,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
70 @@ -100,24 +100,24 @@ SECTIONS
73 __arch_info_begin = .;
75 + KEEP(*(.arch.info.init))
81 + KEEP(*(.taglist.init))
84 #ifdef CONFIG_SMP_ON_UP
88 + KEEP(*(.alt.smp.init))
99 --- a/arch/arm/kernel/vmlinux.lds.h
100 +++ b/arch/arm/kernel/vmlinux.lds.h
102 #define ARM_MMU_DISCARD(x)
104 #define ARM_MMU_KEEP(x)
105 -#define ARM_MMU_DISCARD(x) x
106 +#define ARM_MMU_DISCARD(x) KEEP(x)
111 __proc_info_begin = .; \
112 - *(.proc.info.init) \
113 + KEEP(*(.proc.info.init)) \
116 #define HYPERVISOR_TEXT \
120 __idmap_text_start = .; \
122 + KEEP(*(.idmap.text)) \
123 __idmap_text_end = .; \
124 . = ALIGN(PAGE_SIZE); \
125 __hyp_idmap_text_start = .; \
126 - *(.hyp.idmap.text) \
127 + KEEP(*(.hyp.idmap.text)) \
128 __hyp_idmap_text_end = .;
130 #define ARM_DISCARD \
133 .ARM.unwind_idx : { \
134 __start_unwind_idx = .; \
136 + KEEP(*(.ARM.exidx*)) \
137 __stop_unwind_idx = .; \
139 .ARM.unwind_tab : { \
140 __start_unwind_tab = .; \
142 + KEEP(*(.ARM.extab*)) \
143 __stop_unwind_tab = .; \
146 @@ -102,14 +102,14 @@
147 #define ARM_VECTORS \
148 __vectors_start = .; \
149 .vectors 0xffff0000 : AT(__vectors_start) { \
151 + KEEP(*(.vectors)) \
153 . = __vectors_start + SIZEOF(.vectors); \
157 .stubs ADDR(.vectors) + 0x1000 : AT(__stubs_start) { \
161 . = __stubs_start + SIZEOF(.stubs); \
163 --- a/arch/mips/Kconfig
164 +++ b/arch/mips/Kconfig
165 @@ -43,6 +43,7 @@ config MIPS
166 select HAVE_ARCH_TRANSPARENT_HUGEPAGE if CPU_SUPPORTS_HUGEPAGES && 64BIT
167 select HAVE_CBPF_JIT if (!64BIT && !CPU_MICROMIPS)
168 select HAVE_EBPF_JIT if (64BIT && !CPU_MICROMIPS)
169 + select HAVE_LD_DEAD_CODE_DATA_ELIMINATION
170 select HAVE_CONTEXT_TRACKING
171 select HAVE_COPY_THREAD_TLS
172 select HAVE_C_RECORDMCOUNT
173 --- a/arch/mips/kernel/vmlinux.lds.S
174 +++ b/arch/mips/kernel/vmlinux.lds.S
175 @@ -72,7 +72,7 @@ SECTIONS
176 /* Exception table for data bus errors */
178 __start___dbe_table = .;
180 + KEEP(*(__dbe_table))
181 __stop___dbe_table = .;
184 @@ -123,7 +123,7 @@ SECTIONS
186 .mips.machines.init : AT(ADDR(.mips.machines.init) - LOAD_OFFSET) {
187 __mips_machines_start = .;
188 - *(.mips.machines.init)
189 + KEEP(*(.mips.machines.init))
190 __mips_machines_end = .;