220-gc_sections.patch 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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. 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 <nbd@nbd.name>
  8. Signed-off-by: Jonas Gorski <jogo@openwrt.org>
  9. Signed-off-by: Gabor Juhos <juhosg@openwrt.org>
  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. @@ -279,6 +279,11 @@ else
  22. scripts/Kbuild.include: ;
  23. include scripts/Kbuild.include
  24. +ifdef CONFIG_LD_DEAD_CODE_DATA_ELIMINATION
  25. +KBUILD_CFLAGS_KERNEL += $(call cc-option,-ffunction-sections,)
  26. +KBUILD_CFLAGS_KERNEL += $(call cc-option,-fdata-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. @@ -797,11 +802,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 += $(call cc-option,-ffunction-sections,)
  37. -KBUILD_CFLAGS += $(call cc-option,-fdata-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. CHECKFLAGS += $(NOSTDINC_FLAGS)
  43. --- a/arch/arm/Kconfig
  44. +++ b/arch/arm/Kconfig
  45. @@ -93,6 +93,7 @@ config ARM
  46. select HAVE_UID16
  47. select HAVE_VIRT_CPU_ACCOUNTING_GEN
  48. select IRQ_FORCED_THREADING
  49. + select LD_DEAD_CODE_DATA_ELIMINATION
  50. select MODULES_USE_ELF_REL
  51. select NO_BOOTMEM
  52. select OF_EARLY_FLATTREE if OF
  53. --- a/arch/arm/boot/compressed/Makefile
  54. +++ b/arch/arm/boot/compressed/Makefile
  55. @@ -105,6 +105,7 @@ ifeq ($(CONFIG_FUNCTION_TRACER),y)
  56. ORIG_CFLAGS := $(KBUILD_CFLAGS)
  57. KBUILD_CFLAGS = $(subst -pg, , $(ORIG_CFLAGS))
  58. endif
  59. +KBUILD_CFLAGS_KERNEL := $(patsubst -f%-sections,,$(KBUILD_CFLAGS_KERNEL))
  60. # -fstack-protector-strong triggers protection checks in this code,
  61. # but it is being used too early to link to meaningful stack_chk logic.
  62. --- a/arch/arm/kernel/vmlinux.lds.S
  63. +++ b/arch/arm/kernel/vmlinux.lds.S
  64. @@ -31,7 +31,7 @@
  65. #define PROC_INFO \
  66. . = ALIGN(4); \
  67. VMLINUX_SYMBOL(__proc_info_begin) = .; \
  68. - *(.proc.info.init) \
  69. + KEEP(*(.proc.info.init)) \
  70. VMLINUX_SYMBOL(__proc_info_end) = .;
  71. #define HYPERVISOR_TEXT \
  72. @@ -42,11 +42,11 @@
  73. #define IDMAP_TEXT \
  74. ALIGN_FUNCTION(); \
  75. VMLINUX_SYMBOL(__idmap_text_start) = .; \
  76. - *(.idmap.text) \
  77. + KEEP(*(.idmap.text)) \
  78. VMLINUX_SYMBOL(__idmap_text_end) = .; \
  79. . = ALIGN(PAGE_SIZE); \
  80. VMLINUX_SYMBOL(__hyp_idmap_text_start) = .; \
  81. - *(.hyp.idmap.text) \
  82. + KEEP(*(.hyp.idmap.text)) \
  83. VMLINUX_SYMBOL(__hyp_idmap_text_end) = .;
  84. #ifdef CONFIG_HOTPLUG_CPU
  85. @@ -119,7 +119,7 @@ SECTIONS
  86. _stext = .; /* Text and read-only data */
  87. IDMAP_TEXT
  88. __exception_text_start = .;
  89. - *(.exception.text)
  90. + KEEP(*(.exception.text))
  91. __exception_text_end = .;
  92. IRQENTRY_TEXT
  93. SOFTIRQENTRY_TEXT
  94. @@ -148,7 +148,7 @@ SECTIONS
  95. __ex_table : AT(ADDR(__ex_table) - LOAD_OFFSET) {
  96. __start___ex_table = .;
  97. #ifdef CONFIG_MMU
  98. - *(__ex_table)
  99. + KEEP(*(__ex_table))
  100. #endif
  101. __stop___ex_table = .;
  102. }
  103. @@ -160,12 +160,12 @@ SECTIONS
  104. . = ALIGN(8);
  105. .ARM.unwind_idx : {
  106. __start_unwind_idx = .;
  107. - *(.ARM.exidx*)
  108. + KEEP(*(.ARM.exidx*))
  109. __stop_unwind_idx = .;
  110. }
  111. .ARM.unwind_tab : {
  112. __start_unwind_tab = .;
  113. - *(.ARM.extab*)
  114. + KEEP(*(.ARM.extab*))
  115. __stop_unwind_tab = .;
  116. }
  117. #endif
  118. @@ -186,13 +186,13 @@ SECTIONS
  119. __vectors_lma = .;
  120. OVERLAY 0xffff0000 : NOCROSSREFS AT(__vectors_lma) {
  121. .vectors {
  122. - *(.vectors)
  123. + KEEP(*(.vectors))
  124. }
  125. .vectors.bhb.loop8 {
  126. - *(.vectors.bhb.loop8)
  127. + KEEP(*(.vectors.bhb.loop8))
  128. }
  129. .vectors.bhb.bpiall {
  130. - *(.vectors.bhb.bpiall)
  131. + KEEP(*(.vectors.bhb.bpiall))
  132. }
  133. }
  134. ARM_LMA(__vectors, .vectors);
  135. @@ -204,7 +204,7 @@ SECTIONS
  136. __stubs_lma = .;
  137. .stubs ADDR(.vectors) + 0x1000 : AT(__stubs_lma) {
  138. - *(.stubs)
  139. + KEEP(*(.stubs))
  140. }
  141. ARM_LMA(__stubs, .stubs);
  142. . = __stubs_lma + SIZEOF(.stubs);
  143. @@ -220,24 +220,24 @@ SECTIONS
  144. }
  145. .init.arch.info : {
  146. __arch_info_begin = .;
  147. - *(.arch.info.init)
  148. + KEEP(*(.arch.info.init))
  149. __arch_info_end = .;
  150. }
  151. .init.tagtable : {
  152. __tagtable_begin = .;
  153. - *(.taglist.init)
  154. + KEEP(*(.taglist.init))
  155. __tagtable_end = .;
  156. }
  157. #ifdef CONFIG_SMP_ON_UP
  158. .init.smpalt : {
  159. __smpalt_begin = .;
  160. - *(.alt.smp.init)
  161. + KEEP(*(.alt.smp.init))
  162. __smpalt_end = .;
  163. }
  164. #endif
  165. .init.pv_table : {
  166. __pv_table_begin = .;
  167. - *(.pv_table)
  168. + KEEP(*(.pv_table))
  169. __pv_table_end = .;
  170. }
  171. .init.data : {
  172. --- a/arch/mips/Kconfig
  173. +++ b/arch/mips/Kconfig
  174. @@ -41,6 +41,7 @@ config MIPS
  175. select HAVE_CBPF_JIT if (!64BIT && !CPU_MICROMIPS)
  176. select HAVE_EBPF_JIT if (64BIT && !CPU_MICROMIPS)
  177. select HAVE_CC_STACKPROTECTOR
  178. + select LD_DEAD_CODE_DATA_ELIMINATION
  179. select HAVE_CONTEXT_TRACKING
  180. select HAVE_COPY_THREAD_TLS
  181. select HAVE_C_RECORDMCOUNT
  182. --- a/arch/mips/kernel/vmlinux.lds.S
  183. +++ b/arch/mips/kernel/vmlinux.lds.S
  184. @@ -72,7 +72,7 @@ SECTIONS
  185. /* Exception table for data bus errors */
  186. __dbe_table : {
  187. __start___dbe_table = .;
  188. - *(__dbe_table)
  189. + KEEP(*(__dbe_table))
  190. __stop___dbe_table = .;
  191. }
  192. @@ -124,7 +124,7 @@ SECTIONS
  193. . = ALIGN(4);
  194. .mips.machines.init : AT(ADDR(.mips.machines.init) - LOAD_OFFSET) {
  195. __mips_machines_start = .;
  196. - *(.mips.machines.init)
  197. + KEEP(*(.mips.machines.init))
  198. __mips_machines_end = .;
  199. }
  200. --- a/include/asm-generic/vmlinux.lds.h
  201. +++ b/include/asm-generic/vmlinux.lds.h
  202. @@ -105,7 +105,7 @@
  203. #ifdef CONFIG_FTRACE_MCOUNT_RECORD
  204. #define MCOUNT_REC() . = ALIGN(8); \
  205. VMLINUX_SYMBOL(__start_mcount_loc) = .; \
  206. - *(__mcount_loc) \
  207. + KEEP(*(__mcount_loc)) \
  208. VMLINUX_SYMBOL(__stop_mcount_loc) = .;
  209. #else
  210. #define MCOUNT_REC()
  211. @@ -113,7 +113,7 @@
  212. #ifdef CONFIG_TRACE_BRANCH_PROFILING
  213. #define LIKELY_PROFILE() VMLINUX_SYMBOL(__start_annotated_branch_profile) = .; \
  214. - *(_ftrace_annotated_branch) \
  215. + KEEP(*(_ftrace_annotated_branch)) \
  216. VMLINUX_SYMBOL(__stop_annotated_branch_profile) = .;
  217. #else
  218. #define LIKELY_PROFILE()
  219. @@ -121,7 +121,7 @@
  220. #ifdef CONFIG_PROFILE_ALL_BRANCHES
  221. #define BRANCH_PROFILE() VMLINUX_SYMBOL(__start_branch_profile) = .; \
  222. - *(_ftrace_branch) \
  223. + KEEP(*(_ftrace_branch)) \
  224. VMLINUX_SYMBOL(__stop_branch_profile) = .;
  225. #else
  226. #define BRANCH_PROFILE()
  227. @@ -237,7 +237,8 @@
  228. LIKELY_PROFILE() \
  229. BRANCH_PROFILE() \
  230. TRACE_PRINTKS() \
  231. - TRACEPOINT_STR()
  232. + TRACEPOINT_STR() \
  233. + *(.data.[a-zA-Z_]*)
  234. /*
  235. * Data section helpers
  236. @@ -501,7 +502,7 @@
  237. #define ENTRY_TEXT \
  238. ALIGN_FUNCTION(); \
  239. VMLINUX_SYMBOL(__entry_text_start) = .; \
  240. - *(.entry.text) \
  241. + KEEP(*(.entry.text)) \
  242. VMLINUX_SYMBOL(__entry_text_end) = .;
  243. #define IRQENTRY_TEXT \
  244. @@ -608,7 +609,7 @@
  245. . = ALIGN(sbss_align); \
  246. .sbss : AT(ADDR(.sbss) - LOAD_OFFSET) { \
  247. *(.dynsbss) \
  248. - *(.sbss) \
  249. + *(.sbss .sbss.*) \
  250. *(.scommon) \
  251. }