Skip to content

Commit 850ded4

Browse files
samitolvanenmasahir0y
authored andcommitted
kbuild: Fix TRIM_UNUSED_KSYMS with LTO_CLANG
With CONFIG_LTO_CLANG, we currently link modules into native code just before modpost, which means with TRIM_UNUSED_KSYMS enabled, we still look at the LLVM bitcode in the .o files when generating the list of used symbols. As the bitcode doesn't yet have calls to compiler intrinsics and llvm-nm doesn't see function references that only exist in function-level inline assembly, we currently need a whitelist for TRIM_UNUSED_KSYMS to work with LTO. This change moves module LTO linking to happen earlier, and thus avoids the issue with LLVM bitcode and TRIM_UNUSED_KSYMS entirely, allowing us to also drop the whitelist from gen_autoksyms.sh. Link: ClangBuiltLinux/linux#1369 Signed-off-by: Sami Tolvanen <samitolvanen@google.com> Reviewed-by: Alexander Lobakin <alobakin@pm.me> Tested-by: Alexander Lobakin <alobakin@pm.me> Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>
1 parent 7d73c3e commit 850ded4

5 files changed

Lines changed: 38 additions & 51 deletions

File tree

‎scripts/Makefile.build‎

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,10 @@ endif
8888

8989
targets-for-modules := $(patsubst %.o, %.mod, $(filter %.o, $(obj-m)))
9090

91+
ifdef CONFIG_LTO_CLANG
92+
targets-for-modules += $(patsubst %.o, %.lto.o, $(filter %.o, $(obj-m)))
93+
endif
94+
9195
ifdef need-modorder
9296
targets-for-modules += $(obj)/modules.order
9397
endif
@@ -271,12 +275,33 @@ $(obj)/%.o: $(src)/%.c $(recordmcount_source) $$(objtool_dep) FORCE
271275
$(call if_changed_rule,cc_o_c)
272276
$(call cmd,force_checksrc)
273277

278+
ifdef CONFIG_LTO_CLANG
279+
# Module .o files may contain LLVM bitcode, compile them into native code
280+
# before ELF processing
281+
quiet_cmd_cc_lto_link_modules = LTO [M] $@
282+
cmd_cc_lto_link_modules = \
283+
$(LD) $(ld_flags) -r -o $@ \
284+
$(shell [ -s $(@:.lto.o=.o.symversions) ] && \
285+
echo -T $(@:.lto.o=.o.symversions)) \
286+
--whole-archive $(filter-out FORCE,$^)
287+
288+
ifdef CONFIG_STACK_VALIDATION
289+
# objtool was skipped for LLVM bitcode, run it now that we have compiled
290+
# modules into native code
291+
cmd_cc_lto_link_modules += ; \
292+
$(objtree)/tools/objtool/objtool $(objtool_args) --module $@
293+
endif
294+
295+
$(obj)/%.lto.o: $(obj)/%.o FORCE
296+
$(call if_changed,cc_lto_link_modules)
297+
endif
298+
274299
cmd_mod = { \
275300
echo $(if $($*-objs)$($*-y)$($*-m), $(addprefix $(obj)/, $($*-objs) $($*-y) $($*-m)), $(@:.mod=.o)); \
276301
$(undefined_syms) echo; \
277302
} > $@
278303

279-
$(obj)/%.mod: $(obj)/%.o FORCE
304+
$(obj)/%.mod: $(obj)/%$(mod-prelink-ext).o FORCE
280305
$(call if_changed,mod)
281306

282307
quiet_cmd_cc_lst_c = MKLST $@

‎scripts/Makefile.lib‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,13 @@ dtc_cpp_flags = -Wp,-MMD,$(depfile).pre.tmp -nostdinc \
225225
$(addprefix -I,$(DTC_INCLUDE)) \
226226
-undef -D__DTS__
227227

228+
ifeq ($(CONFIG_LTO_CLANG),y)
229+
# With CONFIG_LTO_CLANG, .o files in modules might be LLVM bitcode, so we
230+
# need to run LTO to compile them into native code (.lto.o) before further
231+
# processing.
232+
mod-prelink-ext := .lto
233+
endif
234+
228235
# Objtool arguments are also needed for modfinal with LTO, so we define
229236
# then here to avoid duplication.
230237
objtool_args = \

‎scripts/Makefile.modfinal‎

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ __modfinal:
99
include include/config/auto.conf
1010
include $(srctree)/scripts/Kbuild.include
1111

12-
# for c_flags and objtool_args
12+
# for c_flags and mod-prelink-ext
1313
include $(srctree)/scripts/Makefile.lib
1414

1515
# find all modules listed in modules.order
@@ -30,23 +30,6 @@ quiet_cmd_cc_o_c = CC [M] $@
3030

3131
ARCH_POSTLINK := $(wildcard $(srctree)/arch/$(SRCARCH)/Makefile.postlink)
3232

33-
ifdef CONFIG_LTO_CLANG
34-
# With CONFIG_LTO_CLANG, reuse the object file we compiled for modpost to
35-
# avoid a second slow LTO link
36-
prelink-ext := .lto
37-
38-
# ELF processing was skipped earlier because we didn't have native code,
39-
# so let's now process the prelinked binary before we link the module.
40-
41-
ifdef CONFIG_STACK_VALIDATION
42-
cmd_ld_ko_o += \
43-
$(objtree)/tools/objtool/objtool $(objtool_args) \
44-
$(@:.ko=$(prelink-ext).o);
45-
46-
endif # CONFIG_STACK_VALIDATION
47-
48-
endif # CONFIG_LTO_CLANG
49-
5033
quiet_cmd_ld_ko_o = LD [M] $@
5134
cmd_ld_ko_o += \
5235
$(LD) -r $(KBUILD_LDFLAGS) \
@@ -72,7 +55,7 @@ if_changed_except = $(if $(call newer_prereqs_except,$(2))$(cmd-check), \
7255

7356

7457
# Re-generate module BTFs if either module's .ko or vmlinux changed
75-
$(modules): %.ko: %$(prelink-ext).o %.mod.o scripts/module.lds $(if $(KBUILD_BUILTIN),vmlinux) FORCE
58+
$(modules): %.ko: %$(mod-prelink-ext).o %.mod.o scripts/module.lds $(if $(KBUILD_BUILTIN),vmlinux) FORCE
7659
+$(call if_changed_except,ld_ko_o,vmlinux)
7760
ifdef CONFIG_DEBUG_INFO_BTF_MODULES
7861
+$(if $(newer-prereqs),$(call cmd,btf_ko))

‎scripts/Makefile.modpost‎

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ __modpost:
4141
include include/config/auto.conf
4242
include $(srctree)/scripts/Kbuild.include
4343

44-
# for ld_flags
44+
# for mod-prelink-ext
4545
include $(srctree)/scripts/Makefile.lib
4646

4747
MODPOST = scripts/mod/modpost \
@@ -118,22 +118,6 @@ $(input-symdump):
118118
@echo >&2 ' Modules may not have dependencies or modversions.'
119119
@echo >&2 ' You may get many unresolved symbol warnings.'
120120

121-
ifdef CONFIG_LTO_CLANG
122-
# With CONFIG_LTO_CLANG, .o files might be LLVM bitcode, so we need to run
123-
# LTO to compile them into native code before running modpost
124-
prelink-ext := .lto
125-
126-
quiet_cmd_cc_lto_link_modules = LTO [M] $@
127-
cmd_cc_lto_link_modules = \
128-
$(LD) $(ld_flags) -r -o $@ \
129-
$(shell [ -s $(@:.lto.o=.o.symversions) ] && \
130-
echo -T $(@:.lto.o=.o.symversions)) \
131-
--whole-archive $^
132-
133-
%.lto.o: %.o
134-
$(call if_changed,cc_lto_link_modules)
135-
endif
136-
137121
modules := $(sort $(shell cat $(MODORDER)))
138122

139123
# KBUILD_MODPOST_WARN can be set to avoid error out in case of undefined symbols
@@ -144,9 +128,9 @@ endif
144128
# Read out modules.order to pass in modpost.
145129
# Otherwise, allmodconfig would fail with "Argument list too long".
146130
quiet_cmd_modpost = MODPOST $@
147-
cmd_modpost = sed 's/\.ko$$/$(prelink-ext)\.o/' $< | $(MODPOST) -T -
131+
cmd_modpost = sed 's/\.ko$$/$(mod-prelink-ext)\.o/' $< | $(MODPOST) -T -
148132

149-
$(output-symdump): $(MODORDER) $(input-symdump) $(modules:.ko=$(prelink-ext).o) FORCE
133+
$(output-symdump): $(MODORDER) $(input-symdump) $(modules:.ko=$(mod-prelink-ext).o) FORCE
150134
$(call if_changed,modpost)
151135

152136
targets += $(output-symdump)

‎scripts/gen_autoksyms.sh‎

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,6 @@ if [ -n "$CONFIG_MODVERSIONS" ]; then
2626
needed_symbols="$needed_symbols module_layout"
2727
fi
2828

29-
# With CONFIG_LTO_CLANG, LLVM bitcode has not yet been compiled into a binary
30-
# when the .mod files are generated, which means they don't yet contain
31-
# references to certain symbols that will be present in the final binaries.
32-
if [ -n "$CONFIG_LTO_CLANG" ]; then
33-
# intrinsic functions
34-
needed_symbols="$needed_symbols memcpy memmove memset"
35-
# ftrace
36-
needed_symbols="$needed_symbols _mcount"
37-
# stack protector symbols
38-
needed_symbols="$needed_symbols __stack_chk_fail __stack_chk_guard"
39-
fi
40-
4129
ksym_wl=
4230
if [ -n "$CONFIG_UNUSED_KSYMS_WHITELIST" ]; then
4331
# Use 'eval' to expand the whitelist path and check if it is relative

0 commit comments

Comments
 (0)