Skip to content

LTO breaks TRIM_UNUSED_KSYMS when symbols are only referenced from inline assembly #1369

Description

@samitolvanen

@nickdesaulniers reports that x86_64 defconfig + CONFIG_LTO_CLANG_THIN + CONFIG_TRIM_UNUSED_KSYMS currently fails to build with the following modpost error:

  MODPOST modules-only.symvers
ERROR: modpost: "ex_handler_rdmsr_unsafe" [drivers/thermal/intel/x86_pkg_temp_thermal.ko] undefined!
ERROR: modpost: "ex_handler_wrmsr_unsafe" [drivers/thermal/intel/x86_pkg_temp_thermal.ko] undefined!
make[2]: *** [../scripts/Makefile.modpost:150: modules-only.symvers] Error 1

This happens because the list of undefined symbols in x86_pkg_temp_thermal.ko is actually determined from the LLVM bitcode in x86_pg_temp_thermal.o. While the bitcode does contain references to these symbols, they are in inline assembly, which llvm-nm ignores.

An obvious solution would be to generate the list of undefined symbols only after we have compiled the bitcode into .lto.o, but that's complicated by the fact that the undefined_syms command is run before the module is linked into .lto.o. Here's the relevant part from scripts/Makefile.build:

ifdef CONFIG_TRIM_UNUSED_KSYMS
...
# List module undefined symbols
undefined_syms = $(NM) $< | $(AWK) '$$1 == "U" { printf("%s%s", x++ ? " " : "", $$2) }';
endif
...
cmd_mod = { \
        echo $(if $($*-objs)$($*-y)$($*-m), $(addprefix $(obj)/, $($*-objs) $($*-y) $($*-m)), $(@:.mod=.o)); \
        $(undefined_syms) echo; \
        } > $@

$(obj)/%.mod: $(obj)/%.o FORCE
        $(call if_changed,mod)

In order to fix this, I think we would have to either postpone undefined_syms until after .lto.o has been compiled, or compile .lto.o earlier.

Metadata

Metadata

Assignees

No one assigned

    Labels

    [BUG] linuxA bug that should be fixed in the mainline kernel.[FEATURE] LTORelated to building the kernel with LLVM Link Time Optimization[FIXED][LINUX] 5.15This bug was fixed in Linux 5.15

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions