17fcc5cbd23eeac4f6753c31fb3f5943bb59e352
[librecmc/librecmc.git] / target / linux / generic / hack-4.19 / 220-gc_sections.patch
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
5
6 In combination with kernel symbol export stripping this significantly reduces
7 the kernel image size. Used on both ARM and MIPS architectures.
8
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>
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/Makefile
23 +++ b/Makefile
24 @@ -294,6 +294,11 @@ else
25  scripts/Kbuild.include: ;
26  include scripts/Kbuild.include
27  
28 +ifdef CONFIG_LD_DEAD_CODE_DATA_ELIMINATION
29 +KBUILD_CFLAGS_KERNEL += -ffunction-sections -fdata-sections
30 +LDFLAGS_vmlinux += --gc-sections
31 +endif
32 +
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 @@ -785,11 +790,6 @@ ifdef CONFIG_DEBUG_SECTION_MISMATCH
37  KBUILD_CFLAGS += $(call cc-option, -fno-inline-functions-called-once)
38  endif
39  
40 -ifdef CONFIG_LD_DEAD_CODE_DATA_ELIMINATION
41 -KBUILD_CFLAGS_KERNEL += -ffunction-sections -fdata-sections
42 -LDFLAGS_vmlinux += --gc-sections
43 -endif
44 -
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)
47  
48 --- a/arch/arm/Kconfig
49 +++ b/arch/arm/Kconfig
50 @@ -98,6 +98,7 @@ config ARM
51         select HAVE_UID16
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
57         select NO_BOOTMEM
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))
63  endif
64 +KBUILD_CFLAGS_KERNEL := $(patsubst -f%-sections,,$(KBUILD_CFLAGS_KERNEL))
65  
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
71         }
72         .init.arch.info : {
73                 __arch_info_begin = .;
74 -               *(.arch.info.init)
75 +               KEEP(*(.arch.info.init))
76                 __arch_info_end = .;
77         }
78         .init.tagtable : {
79                 __tagtable_begin = .;
80 -               *(.taglist.init)
81 +               KEEP(*(.taglist.init))
82                 __tagtable_end = .;
83         }
84  #ifdef CONFIG_SMP_ON_UP
85         .init.smpalt : {
86                 __smpalt_begin = .;
87 -               *(.alt.smp.init)
88 +               KEEP(*(.alt.smp.init))
89                 __smpalt_end = .;
90         }
91  #endif
92         .init.pv_table : {
93                 __pv_table_begin = .;
94 -               *(.pv_table)
95 +               KEEP(*(.pv_table))
96                 __pv_table_end = .;
97         }
98  
99 --- a/arch/arm/kernel/vmlinux.lds.h
100 +++ b/arch/arm/kernel/vmlinux.lds.h
101 @@ -22,13 +22,13 @@
102  #define ARM_MMU_DISCARD(x)
103  #else
104  #define ARM_MMU_KEEP(x)
105 -#define ARM_MMU_DISCARD(x)     x
106 +#define ARM_MMU_DISCARD(x)     KEEP(x)
107  #endif
108  
109  #define PROC_INFO                                                      \
110                 . = ALIGN(4);                                           \
111                 __proc_info_begin = .;                                  \
112 -               *(.proc.info.init)                                      \
113 +               KEEP(*(.proc.info.init))                                \
114                 __proc_info_end = .;
115  
116  #define HYPERVISOR_TEXT                                                        \
117 @@ -39,11 +39,11 @@
118  #define IDMAP_TEXT                                                     \
119                 ALIGN_FUNCTION();                                       \
120                 __idmap_text_start = .;                                 \
121 -               *(.idmap.text)                                          \
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 = .;
129  
130  #define ARM_DISCARD                                                    \
131 @@ -86,12 +86,12 @@
132         . = ALIGN(8);                                                   \
133         .ARM.unwind_idx : {                                             \
134                 __start_unwind_idx = .;                                 \
135 -               *(.ARM.exidx*)                                          \
136 +               KEEP(*(.ARM.exidx*))                                    \
137                 __stop_unwind_idx = .;                                  \
138         }                                                               \
139         .ARM.unwind_tab : {                                             \
140                 __start_unwind_tab = .;                                 \
141 -               *(.ARM.extab*)                                          \
142 +               KEEP(*(.ARM.extab*))                                    \
143                 __stop_unwind_tab = .;                                  \
144         }
145  
146 @@ -102,14 +102,14 @@
147  #define ARM_VECTORS                                                    \
148         __vectors_start = .;                                            \
149         .vectors 0xffff0000 : AT(__vectors_start) {                     \
150 -               *(.vectors)                                             \
151 +               KEEP(*(.vectors))                                       \
152         }                                                               \
153         . = __vectors_start + SIZEOF(.vectors);                         \
154         __vectors_end = .;                                              \
155                                                                         \
156         __stubs_start = .;                                              \
157         .stubs ADDR(.vectors) + 0x1000 : AT(__stubs_start) {            \
158 -               *(.stubs)                                               \
159 +               KEEP(*(.stubs))                                         \
160         }                                                               \
161         . = __stubs_start + SIZEOF(.stubs);                             \
162         __stubs_end = .;                                                \
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 */
177         __dbe_table : {
178                 __start___dbe_table = .;
179 -               *(__dbe_table)
180 +               KEEP(*(__dbe_table))
181                 __stop___dbe_table = .;
182         }
183  
184 @@ -123,7 +123,7 @@ SECTIONS
185         . = ALIGN(4);
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 = .;
191         }
192