1 From: Felix Fietkau <nbd@openwrt.org>
3 use -ffunction-sections, -fdata-sections and --gc-sections --sort-section=name
5 In combination with kernel symbol export stripping this significantly reduces
6 the kernel image size. Used on both ARM and MIPS architectures.
8 Signed-off-by: Felix Fietkau <nbd@openwrt.org>
9 Signed-off-by: Jonas Gorski <jogo@openwrt.org>
10 Signed-off-by: Gabor Juhos <juhosg@openwrt.org>
13 --- a/arch/mips/Makefile
14 +++ b/arch/mips/Makefile
15 @@ -89,10 +89,14 @@ all-$(CONFIG_SYS_SUPPORTS_ZBOOT)+= vmlin
17 cflags-y += -G 0 -mno-abicalls -fno-pic -pipe
18 cflags-y += -msoft-float
19 -LDFLAGS_vmlinux += -G 0 -static -n -nostdlib
20 +LDFLAGS_vmlinux += -G 0 -static -n -nostdlib --gc-sections --sort-section=name
21 KBUILD_AFLAGS_MODULE += -mlong-calls
22 KBUILD_CFLAGS_MODULE += -mlong-calls
24 +ifndef CONFIG_FUNCTION_TRACER
25 +KBUILD_CFLAGS_KERNEL += -ffunction-sections -fdata-sections
28 cflags-y += -ffreestanding
31 --- a/arch/mips/kernel/vmlinux.lds.S
32 +++ b/arch/mips/kernel/vmlinux.lds.S
33 @@ -67,7 +67,7 @@ SECTIONS
34 /* Exception table for data bus errors */
36 __start___dbe_table = .;
38 + KEEP(*(__dbe_table))
39 __stop___dbe_table = .;
42 @@ -112,7 +112,7 @@ SECTIONS
44 .mips.machines.init : AT(ADDR(.mips.machines.init) - LOAD_OFFSET) {
45 __mips_machines_start = .;
46 - *(.mips.machines.init)
47 + KEEP(*(.mips.machines.init))
48 __mips_machines_end = .;
51 --- a/include/asm-generic/vmlinux.lds.h
52 +++ b/include/asm-generic/vmlinux.lds.h
54 #ifdef CONFIG_FTRACE_MCOUNT_RECORD
55 #define MCOUNT_REC() . = ALIGN(8); \
56 VMLINUX_SYMBOL(__start_mcount_loc) = .; \
58 + KEEP(*(__mcount_loc)) \
59 VMLINUX_SYMBOL(__stop_mcount_loc) = .;
64 #ifdef CONFIG_TRACE_BRANCH_PROFILING
65 #define LIKELY_PROFILE() VMLINUX_SYMBOL(__start_annotated_branch_profile) = .; \
66 - *(_ftrace_annotated_branch) \
67 + KEEP(*(_ftrace_annotated_branch)) \
68 VMLINUX_SYMBOL(__stop_annotated_branch_profile) = .;
70 #define LIKELY_PROFILE()
73 #ifdef CONFIG_PROFILE_ALL_BRANCHES
74 #define BRANCH_PROFILE() VMLINUX_SYMBOL(__start_branch_profile) = .; \
76 + KEEP(*(_ftrace_branch)) \
77 VMLINUX_SYMBOL(__stop_branch_profile) = .;
79 #define BRANCH_PROFILE()
81 #ifdef CONFIG_EVENT_TRACING
82 #define FTRACE_EVENTS() . = ALIGN(8); \
83 VMLINUX_SYMBOL(__start_ftrace_events) = .; \
85 + KEEP(*(_ftrace_events)) \
86 VMLINUX_SYMBOL(__stop_ftrace_events) = .;
88 #define FTRACE_EVENTS()
92 #define TRACE_PRINTKS() VMLINUX_SYMBOL(__start___trace_bprintk_fmt) = .; \
93 - *(__trace_printk_fmt) /* Trace_printk fmt' pointer */ \
94 + KEEP(*(__trace_printk_fmt)) /* Trace_printk fmt' pointer */ \
95 VMLINUX_SYMBOL(__stop___trace_bprintk_fmt) = .;
96 #define TRACEPOINT_STR() VMLINUX_SYMBOL(__start___tracepoint_str) = .; \
97 *(__tracepoint_str) /* Trace_printk fmt' pointer */ \
99 #ifdef CONFIG_FTRACE_SYSCALLS
100 #define TRACE_SYSCALLS() . = ALIGN(8); \
101 VMLINUX_SYMBOL(__start_syscalls_metadata) = .; \
102 - *(__syscalls_metadata) \
103 + KEEP(*(__syscalls_metadata)) \
104 VMLINUX_SYMBOL(__stop_syscalls_metadata) = .;
106 #define TRACE_SYSCALLS()
108 #ifdef CONFIG_CLKSRC_OF
109 #define CLKSRC_OF_TABLES() . = ALIGN(8); \
110 VMLINUX_SYMBOL(__clksrc_of_table) = .; \
111 - *(__clksrc_of_table) \
112 - *(__clksrc_of_table_end)
113 + KEEP(*(__clksrc_of_table)) \
114 + KEEP(*(__clksrc_of_table_end))
116 #define CLKSRC_OF_TABLES()
119 #define IRQCHIP_OF_MATCH_TABLE() \
121 VMLINUX_SYMBOL(__irqchip_begin) = .; \
122 - *(__irqchip_of_table) \
123 - *(__irqchip_of_end)
124 + KEEP(*(__irqchip_of_table)) \
125 + KEEP(*(__irqchip_of_end))
127 #define IRQCHIP_OF_MATCH_TABLE()
130 #ifdef CONFIG_COMMON_CLK
131 #define CLK_OF_TABLES() . = ALIGN(8); \
132 VMLINUX_SYMBOL(__clk_of_table) = .; \
133 - *(__clk_of_table) \
134 - *(__clk_of_table_end)
135 + KEEP(*(__clk_of_table)) \
136 + KEEP(*(__clk_of_table_end))
138 #define CLK_OF_TABLES()
141 #define KERNEL_DTB() \
143 VMLINUX_SYMBOL(__dtb_start) = .; \
144 - *(.dtb.init.rodata) \
145 + KEEP(*(.dtb.init.rodata)) \
146 VMLINUX_SYMBOL(__dtb_end) = .;
149 @@ -186,16 +186,17 @@
150 /* implement dynamic printk debug */ \
152 VMLINUX_SYMBOL(__start___jump_table) = .; \
154 + KEEP(*(__jump_table)) \
155 VMLINUX_SYMBOL(__stop___jump_table) = .; \
157 VMLINUX_SYMBOL(__start___verbose) = .; \
159 + KEEP(*(__verbose)) \
160 VMLINUX_SYMBOL(__stop___verbose) = .; \
166 + *(.data.[a-zA-Z_]*)
169 * Data section helpers
170 @@ -249,32 +250,32 @@
172 .pci_fixup : AT(ADDR(.pci_fixup) - LOAD_OFFSET) { \
173 VMLINUX_SYMBOL(__start_pci_fixups_early) = .; \
174 - *(.pci_fixup_early) \
175 + KEEP(*(.pci_fixup_early)) \
176 VMLINUX_SYMBOL(__end_pci_fixups_early) = .; \
177 VMLINUX_SYMBOL(__start_pci_fixups_header) = .; \
178 - *(.pci_fixup_header) \
179 + KEEP(*(.pci_fixup_header)) \
180 VMLINUX_SYMBOL(__end_pci_fixups_header) = .; \
181 VMLINUX_SYMBOL(__start_pci_fixups_final) = .; \
182 - *(.pci_fixup_final) \
183 + KEEP(*(.pci_fixup_final)) \
184 VMLINUX_SYMBOL(__end_pci_fixups_final) = .; \
185 VMLINUX_SYMBOL(__start_pci_fixups_enable) = .; \
186 - *(.pci_fixup_enable) \
187 + KEEP(*(.pci_fixup_enable)) \
188 VMLINUX_SYMBOL(__end_pci_fixups_enable) = .; \
189 VMLINUX_SYMBOL(__start_pci_fixups_resume) = .; \
190 - *(.pci_fixup_resume) \
191 + KEEP(*(.pci_fixup_resume)) \
192 VMLINUX_SYMBOL(__end_pci_fixups_resume) = .; \
193 VMLINUX_SYMBOL(__start_pci_fixups_resume_early) = .; \
194 - *(.pci_fixup_resume_early) \
195 + KEEP(*(.pci_fixup_resume_early)) \
196 VMLINUX_SYMBOL(__end_pci_fixups_resume_early) = .; \
197 VMLINUX_SYMBOL(__start_pci_fixups_suspend) = .; \
198 - *(.pci_fixup_suspend) \
199 + KEEP(*(.pci_fixup_suspend)) \
200 VMLINUX_SYMBOL(__end_pci_fixups_suspend) = .; \
203 /* Built-in firmware blobs */ \
204 .builtin_fw : AT(ADDR(.builtin_fw) - LOAD_OFFSET) { \
205 VMLINUX_SYMBOL(__start_builtin_fw) = .; \
207 + KEEP(*(.builtin_fw)) \
208 VMLINUX_SYMBOL(__end_builtin_fw) = .; \
211 @@ -283,49 +284,49 @@
212 /* Kernel symbol table: Normal symbols */ \
213 __ksymtab : AT(ADDR(__ksymtab) - LOAD_OFFSET) { \
214 VMLINUX_SYMBOL(__start___ksymtab) = .; \
215 - *(SORT(___ksymtab+*)) \
216 + KEEP(*(SORT(___ksymtab+*))) \
217 VMLINUX_SYMBOL(__stop___ksymtab) = .; \
220 /* Kernel symbol table: GPL-only symbols */ \
221 __ksymtab_gpl : AT(ADDR(__ksymtab_gpl) - LOAD_OFFSET) { \
222 VMLINUX_SYMBOL(__start___ksymtab_gpl) = .; \
223 - *(SORT(___ksymtab_gpl+*)) \
224 + KEEP(*(SORT(___ksymtab_gpl+*))) \
225 VMLINUX_SYMBOL(__stop___ksymtab_gpl) = .; \
228 /* Kernel symbol table: Normal unused symbols */ \
229 __ksymtab_unused : AT(ADDR(__ksymtab_unused) - LOAD_OFFSET) { \
230 VMLINUX_SYMBOL(__start___ksymtab_unused) = .; \
231 - *(SORT(___ksymtab_unused+*)) \
232 + KEEP(*(SORT(___ksymtab_unused+*))) \
233 VMLINUX_SYMBOL(__stop___ksymtab_unused) = .; \
236 /* Kernel symbol table: GPL-only unused symbols */ \
237 __ksymtab_unused_gpl : AT(ADDR(__ksymtab_unused_gpl) - LOAD_OFFSET) { \
238 VMLINUX_SYMBOL(__start___ksymtab_unused_gpl) = .; \
239 - *(SORT(___ksymtab_unused_gpl+*)) \
240 + KEEP(*(SORT(___ksymtab_unused_gpl+*))) \
241 VMLINUX_SYMBOL(__stop___ksymtab_unused_gpl) = .; \
244 /* Kernel symbol table: GPL-future-only symbols */ \
245 __ksymtab_gpl_future : AT(ADDR(__ksymtab_gpl_future) - LOAD_OFFSET) { \
246 VMLINUX_SYMBOL(__start___ksymtab_gpl_future) = .; \
247 - *(SORT(___ksymtab_gpl_future+*)) \
248 + KEEP(*(SORT(___ksymtab_gpl_future+*))) \
249 VMLINUX_SYMBOL(__stop___ksymtab_gpl_future) = .; \
252 /* Kernel symbol table: Normal symbols */ \
253 __kcrctab : AT(ADDR(__kcrctab) - LOAD_OFFSET) { \
254 VMLINUX_SYMBOL(__start___kcrctab) = .; \
255 - *(SORT(___kcrctab+*)) \
256 + KEEP(*(SORT(___kcrctab+*))) \
257 VMLINUX_SYMBOL(__stop___kcrctab) = .; \
260 /* Kernel symbol table: GPL-only symbols */ \
261 __kcrctab_gpl : AT(ADDR(__kcrctab_gpl) - LOAD_OFFSET) { \
262 VMLINUX_SYMBOL(__start___kcrctab_gpl) = .; \
263 - *(SORT(___kcrctab_gpl+*)) \
264 + KEEP(*(SORT(___kcrctab_gpl+*))) \
265 VMLINUX_SYMBOL(__stop___kcrctab_gpl) = .; \
268 @@ -339,14 +340,14 @@
269 /* Kernel symbol table: GPL-only unused symbols */ \
270 __kcrctab_unused_gpl : AT(ADDR(__kcrctab_unused_gpl) - LOAD_OFFSET) { \
271 VMLINUX_SYMBOL(__start___kcrctab_unused_gpl) = .; \
272 - *(SORT(___kcrctab_unused_gpl+*)) \
273 + KEEP(*(SORT(___kcrctab_unused_gpl+*))) \
274 VMLINUX_SYMBOL(__stop___kcrctab_unused_gpl) = .; \
277 /* Kernel symbol table: GPL-future-only symbols */ \
278 __kcrctab_gpl_future : AT(ADDR(__kcrctab_gpl_future) - LOAD_OFFSET) { \
279 VMLINUX_SYMBOL(__start___kcrctab_gpl_future) = .; \
280 - *(SORT(___kcrctab_gpl_future+*)) \
281 + KEEP(*(SORT(___kcrctab_gpl_future+*))) \
282 VMLINUX_SYMBOL(__stop___kcrctab_gpl_future) = .; \
285 @@ -365,14 +366,14 @@
286 /* Built-in module parameters. */ \
287 __param : AT(ADDR(__param) - LOAD_OFFSET) { \
288 VMLINUX_SYMBOL(__start___param) = .; \
291 VMLINUX_SYMBOL(__stop___param) = .; \
294 /* Built-in module versions. */ \
295 __modver : AT(ADDR(__modver) - LOAD_OFFSET) { \
296 VMLINUX_SYMBOL(__start___modver) = .; \
298 + KEEP(*(__modver)) \
299 VMLINUX_SYMBOL(__stop___modver) = .; \
300 . = ALIGN((align)); \
301 VMLINUX_SYMBOL(__end_rodata) = .; \
305 VMLINUX_SYMBOL(__entry_text_start) = .; \
307 + KEEP(*(.entry.text)) \
308 VMLINUX_SYMBOL(__entry_text_end) = .;
310 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
313 __ex_table : AT(ADDR(__ex_table) - LOAD_OFFSET) { \
314 VMLINUX_SYMBOL(__start___ex_table) = .; \
316 + KEEP(*(__ex_table)) \
317 VMLINUX_SYMBOL(__stop___ex_table) = .; \
321 #ifdef CONFIG_CONSTRUCTORS
322 #define KERNEL_CTORS() . = ALIGN(8); \
323 VMLINUX_SYMBOL(__ctors_start) = .; \
327 + KEEP(*(.init_array)) \
328 VMLINUX_SYMBOL(__ctors_end) = .;
330 #define KERNEL_CTORS()
332 #define SBSS(sbss_align) \
333 . = ALIGN(sbss_align); \
334 .sbss : AT(ADDR(.sbss) - LOAD_OFFSET) { \
342 *(.bss..page_aligned) \
351 __bug_table : AT(ADDR(__bug_table) - LOAD_OFFSET) { \
352 VMLINUX_SYMBOL(__start___bug_table) = .; \
354 + KEEP(*(__bug_table)) \
355 VMLINUX_SYMBOL(__stop___bug_table) = .; \
360 .tracedata : AT(ADDR(.tracedata) - LOAD_OFFSET) { \
361 VMLINUX_SYMBOL(__tracedata_start) = .; \
363 + KEEP(*(.tracedata)) \
364 VMLINUX_SYMBOL(__tracedata_end) = .; \
367 @@ -613,17 +614,17 @@
368 #define INIT_SETUP(initsetup_align) \
369 . = ALIGN(initsetup_align); \
370 VMLINUX_SYMBOL(__setup_start) = .; \
372 + KEEP(*(.init.setup)) \
373 VMLINUX_SYMBOL(__setup_end) = .;
375 #define INIT_CALLS_LEVEL(level) \
376 VMLINUX_SYMBOL(__initcall##level##_start) = .; \
377 - *(.initcall##level##.init) \
378 - *(.initcall##level##s.init) \
379 + KEEP(*(.initcall##level##.init)) \
380 + KEEP(*(.initcall##level##s.init)) \
383 VMLINUX_SYMBOL(__initcall_start) = .; \
384 - *(.initcallearly.init) \
385 + KEEP(*(.initcallearly.init)) \
386 INIT_CALLS_LEVEL(0) \
387 INIT_CALLS_LEVEL(1) \
388 INIT_CALLS_LEVEL(2) \
389 @@ -637,21 +638,21 @@
391 #define CON_INITCALL \
392 VMLINUX_SYMBOL(__con_initcall_start) = .; \
393 - *(.con_initcall.init) \
394 + KEEP(*(.con_initcall.init)) \
395 VMLINUX_SYMBOL(__con_initcall_end) = .;
397 #define SECURITY_INITCALL \
398 VMLINUX_SYMBOL(__security_initcall_start) = .; \
399 - *(.security_initcall.init) \
400 + KEEP(*(.security_initcall.init)) \
401 VMLINUX_SYMBOL(__security_initcall_end) = .;
403 #ifdef CONFIG_BLK_DEV_INITRD
404 #define INIT_RAM_FS \
406 VMLINUX_SYMBOL(__initramfs_start) = .; \
408 + KEEP(*(.init.ramfs)) \
410 - *(.init.ramfs.info)
411 + KEEP(*(.init.ramfs.info))
415 --- a/arch/arm/Makefile
416 +++ b/arch/arm/Makefile
417 @@ -18,11 +18,16 @@ ifeq ($(CONFIG_CPU_ENDIAN_BE8),y)
418 LDFLAGS_vmlinux += --be8
419 LDFLAGS_MODULE += --be8
421 +LDFLAGS_vmlinux += --gc-sections --sort-section=name
423 OBJCOPYFLAGS :=-O binary -R .comment -S
425 #KBUILD_CFLAGS +=-pipe
427 +ifndef CONFIG_FUNCTION_TRACER
428 +KBUILD_CFLAGS_KERNEL += -ffunction-sections -fdata-sections
431 # Never generate .eh_frame
432 KBUILD_CFLAGS += $(call cc-option,-fno-dwarf2-cfi-asm)
434 --- a/arch/arm/kernel/vmlinux.lds.S
435 +++ b/arch/arm/kernel/vmlinux.lds.S
439 VMLINUX_SYMBOL(__proc_info_begin) = .; \
440 - *(.proc.info.init) \
441 + KEEP(*(.proc.info.init)) \
442 VMLINUX_SYMBOL(__proc_info_end) = .;
446 VMLINUX_SYMBOL(__idmap_text_start) = .; \
448 + KEEP(*(.idmap.text)) \
449 VMLINUX_SYMBOL(__idmap_text_end) = .; \
451 VMLINUX_SYMBOL(__hyp_idmap_text_start) = .; \
452 @@ -93,7 +93,7 @@ SECTIONS
453 .text : { /* Real text segment */
454 _stext = .; /* Text and read-only data */
455 __exception_text_start = .;
457 + KEEP(*(.exception.text))
458 __exception_text_end = .;
461 @@ -118,7 +118,7 @@ SECTIONS
462 __ex_table : AT(ADDR(__ex_table) - LOAD_OFFSET) {
463 __start___ex_table = .;
466 + KEEP(*(__ex_table))
468 __stop___ex_table = .;
470 @@ -130,12 +130,12 @@ SECTIONS
473 __start_unwind_idx = .;
475 + KEEP(*(.ARM.exidx*))
476 __stop_unwind_idx = .;
479 __start_unwind_tab = .;
481 + KEEP(*(.ARM.extab*))
482 __stop_unwind_tab = .;
485 @@ -154,14 +154,14 @@ SECTIONS
488 .vectors 0 : AT(__vectors_start) {
492 . = __vectors_start + SIZEOF(.vectors);
496 .stubs 0x1000 : AT(__stubs_start) {
500 . = __stubs_start + SIZEOF(.stubs);
502 @@ -175,24 +175,24 @@ SECTIONS
505 __arch_info_begin = .;
507 + KEEP(*(.arch.info.init))
511 __tagtable_begin = .;
513 + KEEP(*(.taglist.init))
516 #ifdef CONFIG_SMP_ON_UP
520 + KEEP(*(.alt.smp.init))
525 __pv_table_begin = .;
531 --- a/arch/arm/boot/compressed/Makefile
532 +++ b/arch/arm/boot/compressed/Makefile
533 @@ -120,6 +120,7 @@ ifeq ($(CONFIG_FUNCTION_TRACER),y)
534 ORIG_CFLAGS := $(KBUILD_CFLAGS)
535 KBUILD_CFLAGS = $(subst -pg, , $(ORIG_CFLAGS))
537 +KBUILD_CFLAGS_KERNEL := $(patsubst -f%-sections,,$(KBUILD_CFLAGS_KERNEL))
539 ccflags-y := -fpic -mno-single-pic-base -fno-builtin -I$(obj)
540 asflags-y := -DZIMAGE