blob: da85f1623877e21091683fe3d48486186d739aa0 [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001From e3d8676f5722b7622685581e06e8f53e6138e3ab Mon Sep 17 00:00:00 2001
2From: Felix Fietkau <nbd@nbd.name>
3Date: Sat, 15 Jul 2017 23:42:36 +0200
4Subject: use -ffunction-sections, -fdata-sections and --gc-sections
5
6In combination with kernel symbol export stripping this significantly reduces
7the kernel image size. Used on both ARM and MIPS architectures.
8
9Signed-off-by: Felix Fietkau <nbd@nbd.name>
10Signed-off-by: Jonas Gorski <jogo@openwrt.org>
11Signed-off-by: Gabor Juhos <juhosg@openwrt.org>
12---
13 Makefile | 10 +++----
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(-)
21
22--- a/arch/arm/Kconfig
23+++ b/arch/arm/Kconfig
24@@ -112,6 +112,7 @@ config ARM
25 select HAVE_UID16
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))
37 endif
38+KBUILD_CFLAGS_KERNEL := $(patsubst -f%-sections,,$(KBUILD_CFLAGS_KERNEL))
39
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
45 }
46 .init.arch.info : {
47 __arch_info_begin = .;
48- *(.arch.info.init)
49+ KEEP(*(.arch.info.init))
50 __arch_info_end = .;
51 }
52 .init.tagtable : {
53 __tagtable_begin = .;
54- *(.taglist.init)
55+ KEEP(*(.taglist.init))
56 __tagtable_end = .;
57 }
58 #ifdef CONFIG_SMP_ON_UP
59 .init.smpalt : {
60 __smpalt_begin = .;
61- *(.alt.smp.init)
62+ KEEP(*(.alt.smp.init))
63 __smpalt_end = .;
64 }
65 #endif
66 .init.pv_table : {
67 __pv_table_begin = .;
68- *(.pv_table)
69+ KEEP(*(.pv_table))
70 __pv_table_end = .;
71 }
72
73--- a/arch/arm/kernel/vmlinux.lds.h
74+++ b/arch/arm/kernel/vmlinux.lds.h
75@@ -22,7 +22,7 @@
76 #define ARM_MMU_DISCARD(x)
77 #else
78 #define ARM_MMU_KEEP(x)
79-#define ARM_MMU_DISCARD(x) x
80+#define ARM_MMU_DISCARD(x) KEEP(x)
81 #endif
82
83 /*
84@@ -41,7 +41,7 @@
85 #define PROC_INFO \
86 . = ALIGN(4); \
87 __proc_info_begin = .; \
88- *(.proc.info.init) \
89+ KEEP(*(.proc.info.init)) \
90 __proc_info_end = .;
91
92 #define HYPERVISOR_TEXT \
93@@ -52,11 +52,11 @@
94 #define IDMAP_TEXT \
95 ALIGN_FUNCTION(); \
96 __idmap_text_start = .; \
97- *(.idmap.text) \
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 = .;
105
106 #define ARM_DISCARD \
107@@ -99,12 +99,12 @@
108 . = ALIGN(8); \
109 .ARM.unwind_idx : { \
110 __start_unwind_idx = .; \
111- *(.ARM.exidx*) \
112+ KEEP(*(.ARM.exidx*)) \
113 __stop_unwind_idx = .; \
114 } \
115 .ARM.unwind_tab : { \
116 __start_unwind_tab = .; \
117- *(.ARM.extab*) \
118+ KEEP(*(.ARM.extab*)) \
119 __stop_unwind_tab = .; \
120 }
121
122@@ -116,7 +116,7 @@
123 __vectors_lma = .; \
124 OVERLAY 0xffff0000 : NOCROSSREFS AT(__vectors_lma) { \
125 .vectors { \
126- *(.vectors) \
127+ KEEP(*(.vectors)) \
128 } \
129 .vectors.bhb.loop8 { \
130 *(.vectors.bhb.loop8) \
131@@ -134,7 +134,7 @@
132 \
133 __stubs_lma = .; \
134 .stubs ADDR(.vectors) + 0x1000 : AT(__stubs_lma) { \
135- *(.stubs) \
136+ KEEP(*(.stubs)) \
137 } \
138 ARM_LMA(__stubs, .stubs); \
139 . = __stubs_lma + SIZEOF(.stubs); \