diff mbox

[1/5] kbuild: allow architectures to use thin archives instead of ld -r

Message ID 1470399123-8455-2-git-send-email-npiggin@gmail.com (mailing list archive)
State New, archived
Headers show

Commit Message

Nicholas Piggin Aug. 5, 2016, 12:11 p.m. UTC
From: Stephen Rothwell <sfr@canb.auug.org.au>

ld -r is an incremental link used to create built-in.o files in build
subdirectories. It produces relocatable object files containing all
its input files, and these are are then pulled together and relocated
in the final link. Aside from the bloat, this constrains the final
link relocations, which has bitten large powerpc builds with
unresolvable relocations in the final link.

Alan Modra has recommended the kernel use thin archives for linking.
This is an alternative and means that the linker has more information
available to it when it links the kernel.

This patch enables a config option architectures can select, which
causes all built-in.o files to be built as thin archives. built-in.o
files in subdirectories do not get symbol table or index attached,
which improves speed and size. The final link pass creates a
built-in.o archive in the root output directory which includes the
symbol table and index. The linker then uses takes this file to link.

The --whole-archive linker option is required, because the linker now
has visibility to every individual object file, and it will otherwise
just completely avoid including those without external references
(consider a file with EXPORT_SYMBOL or initcall or hardware exceptions
as its only entry points). The traditional built works "by luck" as
built-in.o files are large enough that they're going to get external
references. However this optimisation is unpredictable for the kernel
(due to above external references), ineffective at culling unused, and
costly because the .o files have to be searched for references.
Superior alternatives for link-time culling should be used instead.

Build characteristics for inclink vs thinarc, on a small powerpc64le
pseries VM with a modest .config:

                                  inclink       thinarc
sizes
vmlinux                        15 618 680    15 625 028
sum of all built-in.o          56 091 808     1 054 334
sum excluding root built-in.o                   151 430

find -name built-in.o | xargs rm ; time make vmlinux
real                              22.772s       21.143s
user                              13.280s       13.430s
sys                                4.310s        2.750s

- Final kernel pulled in only about 6K more, which shows how
  ineffective the object file culling is.
- Build performance looks improved due to less pagecache activity.
  On IO constrained systems it could be a bigger win.
- Build size saving is significant.

Side note, the toochain understands archives, so there's some tricks,
$ ar t built-in.o          # list all files you linked with
$ size built-in.o          # and their sizes
$ objdump -d built-in.o    # disassembly (unrelocated) with filenames

Implementation by sfr, minor tweaks by npiggin.

Cc: linux-kbuild@vger.kernel.org
Cc: linux-arch@vger.kernel.org
Cc: linuxppc-dev@lists.ozlabs.org
Cc: Arnd Bergmann <arnd@arndb.de>
Cc: Segher Boessenkool <segher@kernel.crashing.org>
Cc: Alan Modra <amodra@gmail.com>
Signed-off-by: Stephen Rothwell <sfr@canb.auug.org.au>
Signed-off-by: Nicholas Piggin <npiggin@gmail.com>
---
 arch/Kconfig            |  6 +++++
 scripts/Makefile.build  | 23 +++++++++++++---
 scripts/link-vmlinux.sh | 71 +++++++++++++++++++++++++++++++++++++++++--------
 3 files changed, 85 insertions(+), 15 deletions(-)

Comments

kernel test robot Aug. 6, 2016, 3:50 a.m. UTC | #1
Hi Stephen,

[auto build test ERROR on kbuild/for-next]
[also build test ERROR on v4.7]
[cannot apply to linus/master linux/master next-20160805]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system]

url:    https://github.com/0day-ci/linux/commits/Nicholas-Piggin/kbuild-changes-thin-archives-gc-sections/20160805-202258
base:   https://git.kernel.org/pub/scm/linux/kernel/git/mmarek/kbuild.git for-next
config: um-allnoconfig (attached as .config)
compiler: gcc-6 (Debian 6.1.1-9) 6.1.1 20160705
reproduce:
        # save the attached .config to linux build tree
        make ARCH=um 

All errors (new ones prefixed by >>):

   init/built-in.o:(.bss+0x4): multiple definition of `reset_devices'
   init/built-in.o:(.bss+0x4): first defined here
   init/built-in.o: In function `prepare_namespace':
   (.init.text+0x988): multiple definition of `prepare_namespace'
   init/built-in.o:(.init.text+0x988): first defined here
   init/built-in.o:(.init.data+0x1070): multiple definition of `late_time_init'
   init/built-in.o:(.init.data+0x1070): first defined here
   init/built-in.o: In function `load_default_modules':
   (.init.text+0x2f8): multiple definition of `load_default_modules'
   init/built-in.o:(.init.text+0x2f8): first defined here
   init/built-in.o:(.bss+0x10): multiple definition of `system_state'
   init/built-in.o:(.bss+0x10): first defined here
   init/built-in.o: In function `mount_root':
   (.init.text+0x987): multiple definition of `mount_root'
   init/built-in.o:(.init.text+0x987): first defined here
   init/built-in.o: In function `mount_block_root':
   (.init.text+0x836): multiple definition of `mount_block_root'
   init/built-in.o:(.init.text+0x836): first defined here
   init/built-in.o:(.bss+0x14): multiple definition of `early_boot_irqs_disabled'
   init/built-in.o:(.bss+0x14): first defined here
   init/built-in.o:(.data+0x0): multiple definition of `loops_per_jiffy'
   init/built-in.o:(.data+0x0): first defined here
   init/built-in.o:(.bss+0xc): multiple definition of `saved_command_line'
   init/built-in.o:(.bss+0xc): first defined here
   init/built-in.o: In function `calibrate_delay':
   (.text+0x2e0): multiple definition of `calibrate_delay'
   init/built-in.o:(.text+0x2e0): first defined here
   init/built-in.o: In function `do_one_initcall':
   (.init.text+0x57a): multiple definition of `do_one_initcall'
   init/built-in.o:(.init.text+0x57a): first defined here
   init/built-in.o:(.rodata+0x20): multiple definition of `linux_proc_banner'
   init/built-in.o:(.rodata+0x20): first defined here
   init/built-in.o:(.init.data+0x20e4): multiple definition of `rd_doload'
   init/built-in.o:(.init.data+0x20e4): first defined here
   init/built-in.o:(.data+0x620): multiple definition of `init_task'
   init/built-in.o:(.data+0x620): first defined here
   init/built-in.o:(.data+0x460): multiple definition of `init_uts_ns'
   init/built-in.o:(.data+0x460): first defined here
   init/built-in.o:(.data+0x20): multiple definition of `envp_init'
   init/built-in.o:(.data+0x20): first defined here
   init/built-in.o: In function `name_to_dev_t':
   (.text+0x70): multiple definition of `name_to_dev_t'
   init/built-in.o:(.text+0x70): first defined here
   init/built-in.o:(.data+0x618): multiple definition of `root_mountflags'
   init/built-in.o:(.data+0x618): first defined here
   init/built-in.o:(.bss+0x8): multiple definition of `static_key_initialized'
   init/built-in.o:(.bss+0x8): first defined here
   init/built-in.o: In function `start_kernel':
   (.init.text+0x351): multiple definition of `start_kernel'
   init/built-in.o:(.init.text+0x351): first defined here
>> init/built-in.o:(.bss+0x30): multiple definition of `Version_263936'
   init/built-in.o:(.bss+0x30): first defined here
   init/built-in.o: In function `parse_early_options':
   (.init.text+0x2f9): multiple definition of `parse_early_options'
   init/built-in.o:(.init.text+0x2f9): first defined here
   init/built-in.o: In function `parse_early_param':
   (.init.text+0x31a): multiple definition of `parse_early_param'
   init/built-in.o:(.init.text+0x31a): first defined here
   init/built-in.o:(.bss+0x40): multiple definition of `preset_lpj'
   init/built-in.o:(.bss+0x40): first defined here
   init/built-in.o:(.data..init_task+0x0): multiple definition of `init_thread_union'
   init/built-in.o:(.data..init_task+0x0): first defined here
   init/built-in.o: In function `init_rootfs':
   (.init.text+0x80a): multiple definition of `init_rootfs'
   init/built-in.o:(.init.text+0x80a): first defined here
   init/built-in.o:(.rodata+0x80): multiple definition of `linux_banner'
   init/built-in.o:(.rodata+0x80): first defined here
   init/built-in.o:(.bss+0x34): multiple definition of `ROOT_DEV'
   init/built-in.o:(.bss+0x34): first defined here
   init/built-in.o:(.bss+0x0): multiple definition of `initcall_debug'
   init/built-in.o:(.bss+0x0): first defined here
   init/built-in.o:(.init.data+0x1080): multiple definition of `boot_command_line'
   init/built-in.o:(.init.data+0x1080): first defined here
   init/built-in.o:(.bss+0x44): multiple definition of `lpj_fine'
   init/built-in.o:(.bss+0x44): first defined here
   collect2: error: ld returned 1 exit status

---
0-DAY kernel test infrastructure                Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all                   Intel Corporation
diff mbox

Patch

diff --git a/arch/Kconfig b/arch/Kconfig
index d794384..1330bf4 100644
--- a/arch/Kconfig
+++ b/arch/Kconfig
@@ -424,6 +424,12 @@  config CC_STACKPROTECTOR_STRONG
 
 endchoice
 
+config THIN_ARCHIVES
+	bool
+	help
+	  Select this if the architecture wants to use thin archives
+	  instead of ld -r to create the built-in.o files.
+
 config HAVE_CONTEXT_TRACKING
 	bool
 	help
diff --git a/scripts/Makefile.build b/scripts/Makefile.build
index 0d1ca5b..7fab825 100644
--- a/scripts/Makefile.build
+++ b/scripts/Makefile.build
@@ -358,12 +358,22 @@  $(sort $(subdir-obj-y)): $(subdir-ym) ;
 # Rule to compile a set of .o files into one .o file
 #
 ifdef builtin-target
-quiet_cmd_link_o_target = LD      $@
+
+ifdef CONFIG_THIN_ARCHIVES
+  cmd_make_builtin = rm -f $@; $(AR) rcST$(KBUILD_ARFLAGS)
+  cmd_make_empty_builtin = rm -f $@; $(AR) rcST$(KBUILD_ARFLAGS)
+  quiet_cmd_link_o_target = AR      $@
+else
+  cmd_make_builtin = $(LD) $(ld_flags) -r -o
+  cmd_make_empty_builtin = rm -f $@; $(AR) rcs$(KBUILD_ARFLAGS)
+  quiet_cmd_link_o_target = LD      $@
+endif
+
 # If the list of objects to link is empty, just create an empty built-in.o
 cmd_link_o_target = $(if $(strip $(obj-y)),\
-		      $(LD) $(ld_flags) -r -o $@ $(filter $(obj-y), $^) \
+		      $(cmd_make_builtin) $@ $(filter $(obj-y), $^) \
 		      $(cmd_secanalysis),\
-		      rm -f $@; $(AR) rcs$(KBUILD_ARFLAGS) $@)
+		      $(cmd_make_empty_builtin) $@)
 
 $(builtin-target): $(obj-y) FORCE
 	$(call if_changed,link_o_target)
@@ -389,7 +399,12 @@  $(modorder-target): $(subdir-ym) FORCE
 #
 ifdef lib-target
 quiet_cmd_link_l_target = AR      $@
-cmd_link_l_target = rm -f $@; $(AR) rcs$(KBUILD_ARFLAGS) $@ $(lib-y)
+
+ifdef CONFIG_THIN_ARCHIVES
+  cmd_link_l_target = rm -f $@; $(AR) rcST$(KBUILD_ARFLAGS) $@ $(lib-y)
+else
+  cmd_link_l_target = rm -f $@; $(AR) rcs$(KBUILD_ARFLAGS) $@ $(lib-y)
+endif
 
 $(lib-target): $(lib-y) FORCE
 	$(call if_changed,link_l_target)
diff --git a/scripts/link-vmlinux.sh b/scripts/link-vmlinux.sh
index f0f6d9d..a234ffe 100755
--- a/scripts/link-vmlinux.sh
+++ b/scripts/link-vmlinux.sh
@@ -37,12 +37,40 @@  info()
 	fi
 }
 
+# Thin archive build here makes a final archive with
+# symbol table and indexes from vmlinux objects, which can be
+# used as input to linker.
+#
+# Traditional incremental style of link does not require this step
+#
+# built-in.o output file
+#
+archive_builtin()
+{
+	if [ -n "${CONFIG_THIN_ARCHIVES}" ]; then
+		info AR built-in.o
+		rm -f built-in.o;
+		${AR} rcsT${KBUILD_ARFLAGS} built-in.o			\
+					${KBUILD_VMLINUX_INIT}		\
+					${KBUILD_VMLINUX_MAIN}
+	fi
+}
+
 # Link of vmlinux.o used for section mismatch analysis
 # ${1} output file
 modpost_link()
 {
-	${LD} ${LDFLAGS} -r -o ${1} ${KBUILD_VMLINUX_INIT}                   \
-		--start-group ${KBUILD_VMLINUX_MAIN} --end-group
+	local objects
+
+	if [ -n "${CONFIG_THIN_ARCHIVES}" ]; then
+		objects="--whole-archive built-in.o"
+	else
+		objects="${KBUILD_VMLINUX_INIT}				\
+			--start-group					\
+			${KBUILD_VMLINUX_MAIN}				\
+			--end-group"
+	fi
+	${LD} ${LDFLAGS} -r -o ${1} ${objects}
 }
 
 # Link of vmlinux
@@ -51,18 +79,36 @@  modpost_link()
 vmlinux_link()
 {
 	local lds="${objtree}/${KBUILD_LDS}"
+	local objects
 
 	if [ "${SRCARCH}" != "um" ]; then
-		${LD} ${LDFLAGS} ${LDFLAGS_vmlinux} -o ${2}                  \
-			-T ${lds} ${KBUILD_VMLINUX_INIT}                     \
-			--start-group ${KBUILD_VMLINUX_MAIN} --end-group ${1}
+		if [ -n "${CONFIG_THIN_ARCHIVES}" ]; then
+			objects="--whole-archive built-in.o ${1}"
+		else
+			objects="${KBUILD_VMLINUX_INIT}			\
+				--start-group				\
+				${KBUILD_VMLINUX_MAIN}			\
+				--end-group				\
+				${1}"
+		fi
+
+		${LD} ${LDFLAGS} ${LDFLAGS_vmlinux} -o ${2}		\
+			-T ${lds} ${objects}
 	else
-		${CC} ${CFLAGS_vmlinux} -o ${2}                              \
-			-Wl,-T,${lds} ${KBUILD_VMLINUX_INIT}                 \
-			-Wl,--start-group                                    \
-				 ${KBUILD_VMLINUX_MAIN}                      \
-			-Wl,--end-group                                      \
-			-lutil -lrt -lpthread ${1}
+		if [ -n "${CONFIG_THIN_ARCHIVES}" ]; then
+			objects="-Wl,--whole-archive built-in.o ${1}"
+		else
+			objects="${KBUILD_VMLINUX_INIT}			\
+				-Wl,--start-group			\
+				${KBUILD_VMLINUX_MAIN}			\
+				-Wl,--end-group				\
+				${1}"
+		fi
+
+		${CC} ${CFLAGS_vmlinux} -o ${2}				\
+			-Wl,-T,${lds} ${KBUILD_VMLINUX_INIT}		\
+			-lutil -lrt -lpthread				\
+			${objects}
 		rm -f linux
 	fi
 }
@@ -119,6 +165,7 @@  cleanup()
 	rm -f .tmp_kallsyms*
 	rm -f .tmp_version
 	rm -f .tmp_vmlinux*
+	rm -f built-in.o
 	rm -f System.map
 	rm -f vmlinux
 	rm -f vmlinux.o
@@ -162,6 +209,8 @@  case "${KCONFIG_CONFIG}" in
 	. "./${KCONFIG_CONFIG}"
 esac
 
+archive_builtin
+
 #link vmlinux.o
 info LD vmlinux.o
 modpost_link vmlinux.o