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

Commit

Permalink
kbuild: reuse intermediate linker scripts in the final link steps
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
ihalip authored and xanmod committed Nov 10, 2019
1 parent 21639b7 commit 4455b68
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 3 deletions.
4 changes: 3 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -975,18 +975,20 @@ drivers-y := $(patsubst %/, %/built-in.a, $(drivers-y))
net-y := $(patsubst %/, %/built-in.a, $(net-y))
libs-y1 := $(patsubst %/, %/lib.a, $(libs-y))
libs-y2 := $(patsubst %/, %/built-in.a, $(filter-out %.a, $(libs-y)))
libs-lds := $(strip $(patsubst %/, %/.lib-ksyms.o.lds, $(libs-y)))
virt-y := $(patsubst %/, %/built-in.a, $(virt-y))

# Externally visible symbols (used by link-vmlinux.sh)
export KBUILD_VMLINUX_INIT := $(head-y) $(init-y)
export KBUILD_VMLINUX_MAIN := $(core-y) $(libs-y2) $(drivers-y) $(net-y) $(virt-y)
export KBUILD_VMLINUX_LIBS := $(libs-y1)
export KBUILD_LDS := arch/$(SRCARCH)/kernel/vmlinux.lds
export KBUILD_EXTRA_LDS := $(libs-lds)
export LDFLAGS_vmlinux
# used by scripts/package/Makefile
export KBUILD_ALLDIRS := $(sort $(filter-out arch/%,$(vmlinux-alldirs)) arch Documentation include samples scripts tools)

vmlinux-deps := $(KBUILD_LDS) $(KBUILD_VMLINUX_INIT) $(KBUILD_VMLINUX_MAIN) $(KBUILD_VMLINUX_LIBS)
vmlinux-deps := $(KBUILD_LDS) $(KBUILD_EXTRA_LDS) $(KBUILD_VMLINUX_INIT) $(KBUILD_VMLINUX_MAIN) $(KBUILD_VMLINUX_LIBS)

# Recurse until adjust_autoksyms.sh is satisfied
PHONY += autoksyms_recursive
Expand Down
2 changes: 1 addition & 1 deletion scripts/Makefile.build
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,7 @@ cmd_export_list = $(OBJDUMP) -h $< | \
rm -f $(dummy-object);\
echo | $(CC) $(a_flags) -c -o $(dummy-object) -x assembler -;\
$(LD) $(ld_flags) -r -o $@ -T $(ksyms-lds) $(dummy-object);\
rm $(dummy-object) $(ksyms-lds)
rm $(dummy-object)

$(obj)/lib-ksyms.o: $(lib-target) FORCE
$(call if_changed,export_list)
Expand Down
14 changes: 13 additions & 1 deletion scripts/link-vmlinux.sh
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,15 @@ modpost_link()
vmlinux_link()
{
local lds="${objtree}/${KBUILD_LDS}"
local extra_lds=""
local objects

if [ "${SRCARCH}" != "um" ]; then
for extra_ld in ${KBUILD_EXTRA_LDS}
do
extra_lds="$extra_lds -T ${objtree}/$extra_ld"
done

objects="--whole-archive \
built-in.a \
--no-whole-archive \
Expand All @@ -96,8 +102,13 @@ vmlinux_link()
${1}"

${LD} ${KBUILD_LDFLAGS} ${LDFLAGS_vmlinux} -o ${2} \
-T ${lds} ${objects}
-T ${lds} ${extra_lds} ${objects}
else
for extra_ld in ${KBUILD_EXTRA_LDS}
do
extra_lds="$extra_lds -Wl,-T,${objtree}/$extra_ld"
done

objects="-Wl,--whole-archive \
built-in.a \
-Wl,--no-whole-archive \
Expand All @@ -108,6 +119,7 @@ vmlinux_link()

${CC} ${CFLAGS_vmlinux} -o ${2} \
-Wl,-T,${lds} \
${extra_lds} \
${objects} \
-lutil -lrt -lpthread
rm -f linux
Expand Down

0 comments on commit 4455b68

Please sign in to comment.