220-gc_sections.patch 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. From e3d8676f5722b7622685581e06e8f53e6138e3ab Mon Sep 17 00:00:00 2001
  2. From: Felix Fietkau <[email protected]>
  3. Date: Sat, 15 Jul 2017 23:42:36 +0200
  4. Subject: use -ffunction-sections, -fdata-sections and --gc-sections
  5. In combination with kernel symbol export stripping this significantly reduces
  6. the kernel image size. Used on both ARM and MIPS architectures.
  7. Signed-off-by: Felix Fietkau <[email protected]>
  8. Signed-off-by: Jonas Gorski <[email protected]>
  9. Signed-off-by: Gabor Juhos <[email protected]>
  10. ---
  11. Makefile | 10 +++----
  12. arch/arm/Kconfig | 1 +
  13. arch/arm/boot/compressed/Makefile | 1 +
  14. arch/arm/kernel/vmlinux.lds.S | 26 ++++++++--------
  15. arch/mips/Kconfig | 1 +
  16. arch/mips/kernel/vmlinux.lds.S | 4 +--
  17. include/asm-generic/vmlinux.lds.h | 63 ++++++++++++++++++++-------------------
  18. 7 files changed, 55 insertions(+), 51 deletions(-)
  19. --- a/Makefile
  20. +++ b/Makefile
  21. @@ -294,6 +294,11 @@ else
  22. scripts/Kbuild.include: ;
  23. include scripts/Kbuild.include
  24. +ifdef CONFIG_LD_DEAD_CODE_DATA_ELIMINATION
  25. +KBUILD_CFLAGS_KERNEL += -ffunction-sections -fdata-sections
  26. +LDFLAGS_vmlinux += --gc-sections
  27. +endif
  28. +
  29. # Read KERNELRELEASE from include/config/kernel.release (if it exists)
  30. KERNELRELEASE = $(shell cat include/config/kernel.release 2> /dev/null)
  31. KERNELVERSION = $(VERSION)$(if $(PATCHLEVEL),.$(PATCHLEVEL)$(if $(SUBLEVEL),.$(SUBLEVEL)))$(EXTRAVERSION)
  32. @@ -788,11 +793,6 @@ ifdef CONFIG_DEBUG_SECTION_MISMATCH
  33. KBUILD_CFLAGS += $(call cc-option, -fno-inline-functions-called-once)
  34. endif
  35. -ifdef CONFIG_LD_DEAD_CODE_DATA_ELIMINATION
  36. -KBUILD_CFLAGS_KERNEL += -ffunction-sections -fdata-sections
  37. -LDFLAGS_vmlinux += --gc-sections
  38. -endif
  39. -
  40. # arch Makefile may override CC so keep this after arch Makefile is included
  41. NOSTDINC_FLAGS += -nostdinc -isystem $(shell $(CC) -print-file-name=include)
  42. --- a/arch/arm/Kconfig
  43. +++ b/arch/arm/Kconfig
  44. @@ -98,6 +98,7 @@ config ARM
  45. select HAVE_UID16
  46. select HAVE_VIRT_CPU_ACCOUNTING_GEN
  47. select IRQ_FORCED_THREADING
  48. + select HAVE_LD_DEAD_CODE_DATA_ELIMINATION
  49. select MODULES_USE_ELF_REL
  50. select NEED_DMA_MAP_STATE
  51. select NO_BOOTMEM
  52. --- a/arch/arm/boot/compressed/Makefile
  53. +++ b/arch/arm/boot/compressed/Makefile
  54. @@ -106,6 +106,7 @@ ifeq ($(CONFIG_FUNCTION_TRACER),y)
  55. ORIG_CFLAGS := $(KBUILD_CFLAGS)
  56. KBUILD_CFLAGS = $(subst -pg, , $(ORIG_CFLAGS))
  57. endif
  58. +KBUILD_CFLAGS_KERNEL := $(patsubst -f%-sections,,$(KBUILD_CFLAGS_KERNEL))
  59. # -fstack-protector-strong triggers protection checks in this code,
  60. # but it is being used too early to link to meaningful stack_chk logic.
  61. --- a/arch/arm/kernel/vmlinux.lds.S
  62. +++ b/arch/arm/kernel/vmlinux.lds.S
  63. @@ -100,24 +100,24 @@ SECTIONS
  64. }
  65. .init.arch.info : {
  66. __arch_info_begin = .;
  67. - *(.arch.info.init)
  68. + KEEP(*(.arch.info.init))
  69. __arch_info_end = .;
  70. }
  71. .init.tagtable : {
  72. __tagtable_begin = .;
  73. - *(.taglist.init)
  74. + KEEP(*(.taglist.init))
  75. __tagtable_end = .;
  76. }
  77. #ifdef CONFIG_SMP_ON_UP
  78. .init.smpalt : {
  79. __smpalt_begin = .;
  80. - *(.alt.smp.init)
  81. + KEEP(*(.alt.smp.init))
  82. __smpalt_end = .;
  83. }
  84. #endif
  85. .init.pv_table : {
  86. __pv_table_begin = .;
  87. - *(.pv_table)
  88. + KEEP(*(.pv_table))
  89. __pv_table_end = .;
  90. }
  91. --- a/arch/arm/kernel/vmlinux.lds.h
  92. +++ b/arch/arm/kernel/vmlinux.lds.h
  93. @@ -22,13 +22,13 @@
  94. #define ARM_MMU_DISCARD(x)
  95. #else
  96. #define ARM_MMU_KEEP(x)
  97. -#define ARM_MMU_DISCARD(x) x
  98. +#define ARM_MMU_DISCARD(x) KEEP(x)
  99. #endif
  100. #define PROC_INFO \
  101. . = ALIGN(4); \
  102. __proc_info_begin = .; \
  103. - *(.proc.info.init) \
  104. + KEEP(*(.proc.info.init)) \
  105. __proc_info_end = .;
  106. #define HYPERVISOR_TEXT \
  107. @@ -39,11 +39,11 @@
  108. #define IDMAP_TEXT \
  109. ALIGN_FUNCTION(); \
  110. __idmap_text_start = .; \
  111. - *(.idmap.text) \
  112. + KEEP(*(.idmap.text)) \
  113. __idmap_text_end = .; \
  114. . = ALIGN(PAGE_SIZE); \
  115. __hyp_idmap_text_start = .; \
  116. - *(.hyp.idmap.text) \
  117. + KEEP(*(.hyp.idmap.text)) \
  118. __hyp_idmap_text_end = .;
  119. #define ARM_DISCARD \
  120. @@ -86,12 +86,12 @@
  121. . = ALIGN(8); \
  122. .ARM.unwind_idx : { \
  123. __start_unwind_idx = .; \
  124. - *(.ARM.exidx*) \
  125. + KEEP(*(.ARM.exidx*)) \
  126. __stop_unwind_idx = .; \
  127. } \
  128. .ARM.unwind_tab : { \
  129. __start_unwind_tab = .; \
  130. - *(.ARM.extab*) \
  131. + KEEP(*(.ARM.extab*)) \
  132. __stop_unwind_tab = .; \
  133. }
  134. @@ -102,14 +102,14 @@
  135. #define ARM_VECTORS \
  136. __vectors_start = .; \
  137. .vectors 0xffff0000 : AT(__vectors_start) { \
  138. - *(.vectors) \
  139. + KEEP(*(.vectors)) \
  140. } \
  141. . = __vectors_start + SIZEOF(.vectors); \
  142. __vectors_end = .; \
  143. \
  144. __stubs_start = .; \
  145. .stubs ADDR(.vectors) + 0x1000 : AT(__stubs_start) { \
  146. - *(.stubs) \
  147. + KEEP(*(.stubs)) \
  148. } \
  149. . = __stubs_start + SIZEOF(.stubs); \
  150. __stubs_end = .; \
  151. --- a/arch/mips/Kconfig
  152. +++ b/arch/mips/Kconfig
  153. @@ -43,6 +43,7 @@ config MIPS
  154. select HAVE_ARCH_TRANSPARENT_HUGEPAGE if CPU_SUPPORTS_HUGEPAGES && 64BIT
  155. select HAVE_CBPF_JIT if (!64BIT && !CPU_MICROMIPS)
  156. select HAVE_EBPF_JIT if (64BIT && !CPU_MICROMIPS)
  157. + select HAVE_LD_DEAD_CODE_DATA_ELIMINATION
  158. select HAVE_CONTEXT_TRACKING
  159. select HAVE_COPY_THREAD_TLS
  160. select HAVE_C_RECORDMCOUNT
  161. --- a/arch/mips/kernel/vmlinux.lds.S
  162. +++ b/arch/mips/kernel/vmlinux.lds.S
  163. @@ -72,7 +72,7 @@ SECTIONS
  164. /* Exception table for data bus errors */
  165. __dbe_table : {
  166. __start___dbe_table = .;
  167. - *(__dbe_table)
  168. + KEEP(*(__dbe_table))
  169. __stop___dbe_table = .;
  170. }
  171. @@ -123,7 +123,7 @@ SECTIONS
  172. . = ALIGN(4);
  173. .mips.machines.init : AT(ADDR(.mips.machines.init) - LOAD_OFFSET) {
  174. __mips_machines_start = .;
  175. - *(.mips.machines.init)
  176. + KEEP(*(.mips.machines.init))
  177. __mips_machines_end = .;
  178. }