Skip to content
This repository was archived by the owner on Oct 31, 2024. It is now read-only.

Commit ca38fe2

Browse files
ihalipxanmod
authored andcommitted
kbuild: reuse intermediate linker scripts in the final link steps
ld.bfd forces `--undefined X` symbols to be added to the resulting binary even if they're never used. In contrast, ld.lld may silently discard them if they're not referenced. If a kernel exported symbol (EXPORT_SYMBOL*(X)) is not used internally, it may get stripped away by ld.lld. An obvious example is __memcat_p(), which is only used by the `stm` module. With CONFIG_STM=m, the build fails: ERROR: "__memcat_p" [drivers/hwtracing/stm/stm_core.ko] undefined! Work around this issue by reusing the intermediate linker scripts in the final link steps. Signed-off-by: Ilie Halip <ilie.halip@gmail.com> Signed-off-by: Alexandre Frade <admfrade@gmail.com>
1 parent 51af8a7 commit ca38fe2

File tree

3 files changed

+17
-3
lines changed

3 files changed

+17
-3
lines changed

Makefile

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1032,18 +1032,20 @@ drivers-y := $(patsubst %/, %/built-in.a, $(drivers-y))
10321032
net-y := $(patsubst %/, %/built-in.a, $(net-y))
10331033
libs-y1 := $(patsubst %/, %/lib.a, $(libs-y))
10341034
libs-y2 := $(patsubst %/, %/built-in.a, $(filter-out %.a, $(libs-y)))
1035+
libs-lds := $(strip $(patsubst %/, %/.lib-ksyms.o.lds, $(libs-y)))
10351036
virt-y := $(patsubst %/, %/built-in.a, $(virt-y))
10361037

10371038
# Externally visible symbols (used by link-vmlinux.sh)
10381039
export KBUILD_VMLINUX_OBJS := $(head-y) $(init-y) $(core-y) $(libs-y2) \
10391040
$(drivers-y) $(net-y) $(virt-y)
10401041
export KBUILD_VMLINUX_LIBS := $(libs-y1)
10411042
export KBUILD_LDS := arch/$(SRCARCH)/kernel/vmlinux.lds
1043+
export KBUILD_EXTRA_LDS := $(libs-lds)
10421044
export LDFLAGS_vmlinux
10431045
# used by scripts/Makefile.package
10441046
export KBUILD_ALLDIRS := $(sort $(filter-out arch/%,$(vmlinux-alldirs)) LICENSES arch include scripts tools)
10451047

1046-
vmlinux-deps := $(KBUILD_LDS) $(KBUILD_VMLINUX_OBJS) $(KBUILD_VMLINUX_LIBS)
1048+
vmlinux-deps := $(KBUILD_LDS) $(KBUILD_EXTRA_LDS) $(KBUILD_VMLINUX_OBJS) $(KBUILD_VMLINUX_LIBS)
10471049

10481050
# Recurse until adjust_autoksyms.sh is satisfied
10491051
PHONY += autoksyms_recursive

scripts/Makefile.build

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -426,7 +426,7 @@ cmd_export_list = $(OBJDUMP) -h $< | \
426426
rm -f $(dummy-object);\
427427
echo | $(CC) $(a_flags) -c -o $(dummy-object) -x assembler -;\
428428
$(LD) $(ld_flags) -r -o $@ -T $(ksyms-lds) $(dummy-object);\
429-
rm $(dummy-object) $(ksyms-lds)
429+
rm $(dummy-object)
430430

431431
$(obj)/lib-ksyms.o: $(lib-target) FORCE
432432
$(call if_changed,export_list)

scripts/link-vmlinux.sh

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ modpost_link()
6161
vmlinux_link()
6262
{
6363
local lds="${objtree}/${KBUILD_LDS}"
64+
local extra_lds=""
6465
local output=${1}
6566
local objects
6667

@@ -70,6 +71,11 @@ vmlinux_link()
7071
shift
7172

7273
if [ "${SRCARCH}" != "um" ]; then
74+
for extra_ld in ${KBUILD_EXTRA_LDS}
75+
do
76+
extra_lds="$extra_lds -T ${objtree}/$extra_ld"
77+
done
78+
7379
objects="--whole-archive \
7480
${KBUILD_VMLINUX_OBJS} \
7581
--no-whole-archive \
@@ -80,8 +86,13 @@ vmlinux_link()
8086

8187
${LD} ${KBUILD_LDFLAGS} ${LDFLAGS_vmlinux} \
8288
-o ${output} \
83-
-T ${lds} ${objects}
89+
-T ${lds} ${extra_lds} ${objects}
8490
else
91+
for extra_ld in ${KBUILD_EXTRA_LDS}
92+
do
93+
extra_lds="$extra_lds -Wl,-T,${objtree}/$extra_ld"
94+
done
95+
8596
objects="-Wl,--whole-archive \
8697
${KBUILD_VMLINUX_OBJS} \
8798
-Wl,--no-whole-archive \
@@ -93,6 +104,7 @@ vmlinux_link()
93104
${CC} ${CFLAGS_vmlinux} \
94105
-o ${output} \
95106
-Wl,-T,${lds} \
107+
${extra_lds} \
96108
${objects} \
97109
-lutil -lrt -lpthread
98110
rm -f linux

0 commit comments

Comments
 (0)