Skip to content

Commit 3a166fc

Browse files
npigginmasahir0y
authored andcommitted
kbuild: handle libs-y archives separately from built-in.o archives
The thin archives build currently puts all lib.a and built-in.o files together and links them with --whole-archive. This works because thin archives can recursively refer to thin archives. However some architectures include libgcc.a, which may not be a thin archive, or it may not be constructed with the "P" option, in which case its contents do not get linked correctly. So don't pull .a libs into the root built-in.o archive. These libs should already have symbol tables and indexes built, so they can be direct linker inputs. Move them out of the --whole-archive option, which restore the conditional linking behaviour of lib.a to thin archives builds. Signed-off-by: Nicholas Piggin <npiggin@gmail.com> Signed-off-by: Masahiro Yamada <yamada.masahiro@socionext.com>
1 parent 9a6cfca commit 3a166fc

3 files changed

Lines changed: 46 additions & 15 deletions

File tree

‎Documentation/kbuild/kbuild.txt‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -236,5 +236,9 @@ Files specified with KBUILD_VMLINUX_INIT are linked first.
236236
KBUILD_VMLINUX_MAIN
237237
--------------------------------------------------
238238
All object files for the main part of vmlinux.
239-
KBUILD_VMLINUX_INIT and KBUILD_VMLINUX_MAIN together specify
240-
all the object files used to link vmlinux.
239+
240+
KBUILD_VMLINUX_LIBS
241+
--------------------------------------------------
242+
All .a "lib" files for vmlinux.
243+
KBUILD_VMLINUX_INIT, KBUILD_VMLINUX_MAIN, and KBUILD_VMLINUX_LIBS together
244+
specify all the object files used to link vmlinux.

‎Makefile‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -952,19 +952,19 @@ core-y := $(patsubst %/, %/built-in.o, $(core-y))
952952
drivers-y := $(patsubst %/, %/built-in.o, $(drivers-y))
953953
net-y := $(patsubst %/, %/built-in.o, $(net-y))
954954
libs-y1 := $(patsubst %/, %/lib.a, $(libs-y))
955-
libs-y2 := $(patsubst %/, %/built-in.o, $(libs-y))
956-
libs-y := $(libs-y1) $(libs-y2)
955+
libs-y2 := $(filter-out %.a, $(patsubst %/, %/built-in.o, $(libs-y)))
957956
virt-y := $(patsubst %/, %/built-in.o, $(virt-y))
958957

959958
# Externally visible symbols (used by link-vmlinux.sh)
960959
export KBUILD_VMLINUX_INIT := $(head-y) $(init-y)
961-
export KBUILD_VMLINUX_MAIN := $(core-y) $(libs-y) $(drivers-y) $(net-y) $(virt-y)
960+
export KBUILD_VMLINUX_MAIN := $(core-y) $(libs-y2) $(drivers-y) $(net-y) $(virt-y)
961+
export KBUILD_VMLINUX_LIBS := $(libs-y1)
962962
export KBUILD_LDS := arch/$(SRCARCH)/kernel/vmlinux.lds
963963
export LDFLAGS_vmlinux
964964
# used by scripts/pacmage/Makefile
965965
export KBUILD_ALLDIRS := $(sort $(filter-out arch/%,$(vmlinux-alldirs)) arch Documentation include samples scripts tools)
966966

967-
vmlinux-deps := $(KBUILD_LDS) $(KBUILD_VMLINUX_INIT) $(KBUILD_VMLINUX_MAIN)
967+
vmlinux-deps := $(KBUILD_LDS) $(KBUILD_VMLINUX_INIT) $(KBUILD_VMLINUX_MAIN) $(KBUILD_VMLINUX_LIBS)
968968

969969
# Include targets which we want to execute sequentially if the rest of the
970970
# kernel build went well. If CONFIG_TRIM_UNUSED_KSYMS is set, this might be

‎scripts/link-vmlinux.sh‎

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,12 @@
33
# link vmlinux
44
#
55
# vmlinux is linked from the objects selected by $(KBUILD_VMLINUX_INIT) and
6-
# $(KBUILD_VMLINUX_MAIN). Most are built-in.o files from top-level directories
7-
# in the kernel tree, others are specified in arch/$(ARCH)/Makefile.
8-
# Ordering when linking is important, and $(KBUILD_VMLINUX_INIT) must be first.
6+
# $(KBUILD_VMLINUX_MAIN) and $(KBUILD_VMLINUX_LIBS). Most are built-in.o files
7+
# from top-level directories in the kernel tree, others are specified in
8+
# arch/$(ARCH)/Makefile. Ordering when linking is important, and
9+
# $(KBUILD_VMLINUX_INIT) must be first. $(KBUILD_VMLINUX_LIBS) are archives
10+
# which are linked conditionally (not within --whole-archive), and do not
11+
# require symbol indexes added.
912
#
1013
# vmlinux
1114
# ^
@@ -16,6 +19,9 @@
1619
# +--< $(KBUILD_VMLINUX_MAIN)
1720
# | +--< drivers/built-in.o mm/built-in.o + more
1821
# |
22+
# +--< $(KBUILD_VMLINUX_LIBS)
23+
# | +--< lib/lib.a + more
24+
# |
1925
# +-< ${kallsymso} (see description in KALLSYMS section)
2026
#
2127
# vmlinux version (uname -v) cannot be updated during normal
@@ -37,9 +43,10 @@ info()
3743
fi
3844
}
3945

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.
46+
# Thin archive build here makes a final archive with symbol table and indexes
47+
# from vmlinux objects INIT and MAIN, which can be used as input to linker.
48+
# KBUILD_VMLINUX_LIBS archives should already have symbol table and indexes
49+
# added.
4350
#
4451
# Traditional incremental style of link does not require this step
4552
#
@@ -63,11 +70,17 @@ modpost_link()
6370
local objects
6471

6572
if [ -n "${CONFIG_THIN_ARCHIVES}" ]; then
66-
objects="--whole-archive built-in.o --no-whole-archive"
73+
objects="--whole-archive \
74+
built-in.o \
75+
--no-whole-archive \
76+
--start-group \
77+
${KBUILD_VMLINUX_LIBS} \
78+
--end-group"
6779
else
6880
objects="${KBUILD_VMLINUX_INIT} \
6981
--start-group \
7082
${KBUILD_VMLINUX_MAIN} \
83+
${KBUILD_VMLINUX_LIBS} \
7184
--end-group"
7285
fi
7386
${LD} ${LDFLAGS} -r -o ${1} ${objects}
@@ -83,11 +96,18 @@ vmlinux_link()
8396

8497
if [ "${SRCARCH}" != "um" ]; then
8598
if [ -n "${CONFIG_THIN_ARCHIVES}" ]; then
86-
objects="--whole-archive built-in.o ${1} --no-whole-archive"
99+
objects="--whole-archive \
100+
built-in.o \
101+
--no-whole-archive \
102+
--start-group \
103+
${KBUILD_VMLINUX_LIBS} \
104+
--end-group \
105+
${1}"
87106
else
88107
objects="${KBUILD_VMLINUX_INIT} \
89108
--start-group \
90109
${KBUILD_VMLINUX_MAIN} \
110+
${KBUILD_VMLINUX_LIBS} \
91111
--end-group \
92112
${1}"
93113
fi
@@ -96,11 +116,18 @@ vmlinux_link()
96116
-T ${lds} ${objects}
97117
else
98118
if [ -n "${CONFIG_THIN_ARCHIVES}" ]; then
99-
objects="-Wl,--whole-archive built-in.o ${1} -Wl,--no-whole-archive"
119+
objects="-Wl,--whole-archive \
120+
built-in.o \
121+
-Wl,--no-whole-archive \
122+
-Wl,--start-group \
123+
${KBUILD_VMLINUX_LIBS} \
124+
-Wl,--end-group \
125+
${1}"
100126
else
101127
objects="${KBUILD_VMLINUX_INIT} \
102128
-Wl,--start-group \
103129
${KBUILD_VMLINUX_MAIN} \
130+
${KBUILD_VMLINUX_LIBS} \
104131
-Wl,--end-group \
105132
${1}"
106133
fi

0 commit comments

Comments
 (0)