Skip to content

Commit a5967db

Browse files
sfrothwellMichal Marek
authored and
Michal Marek
committed
kbuild: allow architectures to use thin archives instead of ld -r
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. Signed-off-by: Stephen Rothwell <[email protected]> Signed-off-by: Nicholas Piggin <[email protected]> Signed-off-by: Michal Marek <[email protected]>
1 parent 5c6f322 commit a5967db

File tree

3 files changed

+85
-15
lines changed

3 files changed

+85
-15
lines changed

arch/Kconfig

+6
Original file line numberDiff line numberDiff line change
@@ -461,6 +461,12 @@ config CC_STACKPROTECTOR_STRONG
461461

462462
endchoice
463463

464+
config THIN_ARCHIVES
465+
bool
466+
help
467+
Select this if the architecture wants to use thin archives
468+
instead of ld -r to create the built-in.o files.
469+
464470
config HAVE_CONTEXT_TRACKING
465471
bool
466472
help

scripts/Makefile.build

+19-4
Original file line numberDiff line numberDiff line change
@@ -359,12 +359,22 @@ $(sort $(subdir-obj-y)): $(subdir-ym) ;
359359
# Rule to compile a set of .o files into one .o file
360360
#
361361
ifdef builtin-target
362-
quiet_cmd_link_o_target = LD $@
362+
363+
ifdef CONFIG_THIN_ARCHIVES
364+
cmd_make_builtin = rm -f $@; $(AR) rcST$(KBUILD_ARFLAGS)
365+
cmd_make_empty_builtin = rm -f $@; $(AR) rcST$(KBUILD_ARFLAGS)
366+
quiet_cmd_link_o_target = AR $@
367+
else
368+
cmd_make_builtin = $(LD) $(ld_flags) -r -o
369+
cmd_make_empty_builtin = rm -f $@; $(AR) rcs$(KBUILD_ARFLAGS)
370+
quiet_cmd_link_o_target = LD $@
371+
endif
372+
363373
# If the list of objects to link is empty, just create an empty built-in.o
364374
cmd_link_o_target = $(if $(strip $(obj-y)),\
365-
$(LD) $(ld_flags) -r -o $@ $(filter $(obj-y), $^) \
375+
$(cmd_make_builtin) $@ $(filter $(obj-y), $^) \
366376
$(cmd_secanalysis),\
367-
rm -f $@; $(AR) rcs$(KBUILD_ARFLAGS) $@)
377+
$(cmd_make_empty_builtin) $@)
368378

369379
$(builtin-target): $(obj-y) FORCE
370380
$(call if_changed,link_o_target)
@@ -390,7 +400,12 @@ $(modorder-target): $(subdir-ym) FORCE
390400
#
391401
ifdef lib-target
392402
quiet_cmd_link_l_target = AR $@
393-
cmd_link_l_target = rm -f $@; $(AR) rcs$(KBUILD_ARFLAGS) $@ $(lib-y)
403+
404+
ifdef CONFIG_THIN_ARCHIVES
405+
cmd_link_l_target = rm -f $@; $(AR) rcsT$(KBUILD_ARFLAGS) $@ $(lib-y)
406+
else
407+
cmd_link_l_target = rm -f $@; $(AR) rcs$(KBUILD_ARFLAGS) $@ $(lib-y)
408+
endif
394409

395410
$(lib-target): $(lib-y) FORCE
396411
$(call if_changed,link_l_target)

scripts/link-vmlinux.sh

+60-11
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,40 @@ info()
3737
fi
3838
}
3939

40+
# Thin archive build here makes a final archive with
41+
# symbol table and indexes from vmlinux objects, which can be
42+
# used as input to linker.
43+
#
44+
# Traditional incremental style of link does not require this step
45+
#
46+
# built-in.o output file
47+
#
48+
archive_builtin()
49+
{
50+
if [ -n "${CONFIG_THIN_ARCHIVES}" ]; then
51+
info AR built-in.o
52+
rm -f built-in.o;
53+
${AR} rcsT${KBUILD_ARFLAGS} built-in.o \
54+
${KBUILD_VMLINUX_INIT} \
55+
${KBUILD_VMLINUX_MAIN}
56+
fi
57+
}
58+
4059
# Link of vmlinux.o used for section mismatch analysis
4160
# ${1} output file
4261
modpost_link()
4362
{
44-
${LD} ${LDFLAGS} -r -o ${1} ${KBUILD_VMLINUX_INIT} \
45-
--start-group ${KBUILD_VMLINUX_MAIN} --end-group
63+
local objects
64+
65+
if [ -n "${CONFIG_THIN_ARCHIVES}" ]; then
66+
objects="--whole-archive built-in.o"
67+
else
68+
objects="${KBUILD_VMLINUX_INIT} \
69+
--start-group \
70+
${KBUILD_VMLINUX_MAIN} \
71+
--end-group"
72+
fi
73+
${LD} ${LDFLAGS} -r -o ${1} ${objects}
4674
}
4775

4876
# Link of vmlinux
@@ -51,18 +79,36 @@ modpost_link()
5179
vmlinux_link()
5280
{
5381
local lds="${objtree}/${KBUILD_LDS}"
82+
local objects
5483

5584
if [ "${SRCARCH}" != "um" ]; then
56-
${LD} ${LDFLAGS} ${LDFLAGS_vmlinux} -o ${2} \
57-
-T ${lds} ${KBUILD_VMLINUX_INIT} \
58-
--start-group ${KBUILD_VMLINUX_MAIN} --end-group ${1}
85+
if [ -n "${CONFIG_THIN_ARCHIVES}" ]; then
86+
objects="--whole-archive built-in.o ${1}"
87+
else
88+
objects="${KBUILD_VMLINUX_INIT} \
89+
--start-group \
90+
${KBUILD_VMLINUX_MAIN} \
91+
--end-group \
92+
${1}"
93+
fi
94+
95+
${LD} ${LDFLAGS} ${LDFLAGS_vmlinux} -o ${2} \
96+
-T ${lds} ${objects}
5997
else
60-
${CC} ${CFLAGS_vmlinux} -o ${2} \
61-
-Wl,-T,${lds} ${KBUILD_VMLINUX_INIT} \
62-
-Wl,--start-group \
63-
${KBUILD_VMLINUX_MAIN} \
64-
-Wl,--end-group \
65-
-lutil -lrt -lpthread ${1}
98+
if [ -n "${CONFIG_THIN_ARCHIVES}" ]; then
99+
objects="-Wl,--whole-archive built-in.o ${1}"
100+
else
101+
objects="${KBUILD_VMLINUX_INIT} \
102+
-Wl,--start-group \
103+
${KBUILD_VMLINUX_MAIN} \
104+
-Wl,--end-group \
105+
${1}"
106+
fi
107+
108+
${CC} ${CFLAGS_vmlinux} -o ${2} \
109+
-Wl,-T,${lds} \
110+
${objects} \
111+
-lutil -lrt -lpthread
66112
rm -f linux
67113
fi
68114
}
@@ -119,6 +165,7 @@ cleanup()
119165
rm -f .tmp_kallsyms*
120166
rm -f .tmp_version
121167
rm -f .tmp_vmlinux*
168+
rm -f built-in.o
122169
rm -f System.map
123170
rm -f vmlinux
124171
rm -f vmlinux.o
@@ -162,6 +209,8 @@ case "${KCONFIG_CONFIG}" in
162209
. "./${KCONFIG_CONFIG}"
163210
esac
164211

212+
archive_builtin
213+
165214
#link vmlinux.o
166215
info LD vmlinux.o
167216
modpost_link vmlinux.o

0 commit comments

Comments
 (0)