Skip to content

Commit b1a1a1a

Browse files
samitolvanenkees
authored andcommitted
kbuild: lto: postpone objtool
With LTO, LLVM bitcode won't be compiled into native code until modpost_link, or modfinal for modules. This change postpones calls to objtool until after these steps, and moves objtool_args to Makefile.lib, so the arguments can be reused in Makefile.modfinal. As we didn't have objects to process earlier, we use --duplicate when processing vmlinux.o. This change also disables unreachable instruction warnings with LTO to avoid warnings about the int3 padding between functions. Signed-off-by: Sami Tolvanen <[email protected]> Reviewed-by: Kees Cook <[email protected]>
1 parent 41425eb commit b1a1a1a

File tree

4 files changed

+55
-26
lines changed

4 files changed

+55
-26
lines changed

scripts/Makefile.build

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -218,30 +218,11 @@ cmd_record_mcount = $(if $(findstring $(strip $(CC_FLAGS_FTRACE)),$(_c_flags)),
218218
endif # CONFIG_FTRACE_MCOUNT_USE_RECORDMCOUNT
219219

220220
ifdef CONFIG_STACK_VALIDATION
221+
ifndef CONFIG_LTO_CLANG
221222
ifneq ($(SKIP_STACK_VALIDATION),1)
222223

223224
__objtool_obj := $(objtree)/tools/objtool/objtool
224225

225-
objtool_args = $(if $(CONFIG_UNWINDER_ORC),orc generate,check)
226-
227-
objtool_args += $(if $(part-of-module), --module,)
228-
229-
ifndef CONFIG_FRAME_POINTER
230-
objtool_args += --no-fp
231-
endif
232-
ifdef CONFIG_GCOV_KERNEL
233-
objtool_args += --no-unreachable
234-
endif
235-
ifdef CONFIG_RETPOLINE
236-
objtool_args += --retpoline
237-
endif
238-
ifdef CONFIG_X86_SMAP
239-
objtool_args += --uaccess
240-
endif
241-
ifdef CONFIG_FTRACE_MCOUNT_USE_OBJTOOL
242-
objtool_args += --mcount
243-
endif
244-
245226
# 'OBJECT_FILES_NON_STANDARD := y': skip objtool checking for a directory
246227
# 'OBJECT_FILES_NON_STANDARD_foo.o := 'y': skip objtool checking for a file
247228
# 'OBJECT_FILES_NON_STANDARD_foo.o := 'n': override directory skip for a file
@@ -253,6 +234,7 @@ objtool_obj = $(if $(patsubst y%,, \
253234
$(__objtool_obj))
254235

255236
endif # SKIP_STACK_VALIDATION
237+
endif # CONFIG_LTO_CLANG
256238
endif # CONFIG_STACK_VALIDATION
257239

258240
# Rebuild all objects when objtool changes, or is enabled/disabled.

scripts/Makefile.lib

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,18 @@ dtc_cpp_flags = -Wp,-MMD,$(depfile).pre.tmp -nostdinc \
220220
$(addprefix -I,$(DTC_INCLUDE)) \
221221
-undef -D__DTS__
222222

223+
# Objtool arguments are also needed for modfinal with LTO, so we define
224+
# then here to avoid duplication.
225+
objtool_args = \
226+
$(if $(CONFIG_UNWINDER_ORC),orc generate,check) \
227+
$(if $(part-of-module), --module,) \
228+
$(if $(CONFIG_FRAME_POINTER),, --no-fp) \
229+
$(if $(or $(CONFIG_GCOV_KERNEL),$(CONFIG_LTO_CLANG)), \
230+
--no-unreachable,) \
231+
$(if $(CONFIG_RETPOLINE), --retpoline,) \
232+
$(if $(CONFIG_X86_SMAP), --uaccess,) \
233+
$(if $(CONFIG_FTRACE_MCOUNT_USE_OBJTOOL), --mcount,)
234+
223235
# Useful for describing the dependency of composite objects
224236
# Usage:
225237
# $(call multi_depend, multi_used_targets, suffix_to_remove, suffix_to_add)

scripts/Makefile.modfinal

Lines changed: 16 additions & 3 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
12+
# for c_flags and objtool_args
1313
include $(srctree)/scripts/Makefile.lib
1414

1515
# find all modules listed in modules.order
@@ -34,10 +34,23 @@ ifdef CONFIG_LTO_CLANG
3434
# With CONFIG_LTO_CLANG, reuse the object file we compiled for modpost to
3535
# avoid a second slow LTO link
3636
prelink-ext := .lto
37-
endif
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+
ifneq ($(SKIP_STACK_VALIDATION),1)
43+
cmd_ld_ko_o += \
44+
$(objtree)/tools/objtool/objtool $(objtool_args) \
45+
$(@:.ko=$(prelink-ext).o);
46+
47+
endif # SKIP_STACK_VALIDATION
48+
endif # CONFIG_STACK_VALIDATION
49+
50+
endif # CONFIG_LTO_CLANG
3851

3952
quiet_cmd_ld_ko_o = LD [M] $@
40-
cmd_ld_ko_o = \
53+
cmd_ld_ko_o += \
4154
$(LD) -r $(KBUILD_LDFLAGS) \
4255
$(KBUILD_LDFLAGS_MODULE) $(LDFLAGS_MODULE) \
4356
-T scripts/module.lds -o $@ $(filter %.o, $^); \

scripts/link-vmlinux.sh

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,14 +103,36 @@ modpost_link()
103103

104104
objtool_link()
105105
{
106+
local objtoolcmd;
106107
local objtoolopt;
107108

109+
if [ "${CONFIG_LTO_CLANG} ${CONFIG_STACK_VALIDATION}" = "y y" ]; then
110+
# Don't perform vmlinux validation unless explicitly requested,
111+
# but run objtool on vmlinux.o now that we have an object file.
112+
if [ -n "${CONFIG_UNWINDER_ORC}" ]; then
113+
objtoolcmd="orc generate"
114+
fi
115+
116+
objtoolopt="${objtoolopt} --duplicate"
117+
118+
if [ -n "${CONFIG_FTRACE_MCOUNT_USE_OBJTOOL}" ]; then
119+
objtoolopt="${objtoolopt} --mcount"
120+
fi
121+
fi
122+
108123
if [ -n "${CONFIG_VMLINUX_VALIDATION}" ]; then
109-
objtoolopt="check --vmlinux --noinstr"
124+
objtoolopt="${objtoolopt} --noinstr"
125+
fi
126+
127+
if [ -n "${objtoolopt}" ]; then
128+
if [ -z "${objtoolcmd}" ]; then
129+
objtoolcmd="check"
130+
fi
131+
objtoolopt="${objtoolopt} --vmlinux"
110132
if [ -z "${CONFIG_FRAME_POINTER}" ]; then
111133
objtoolopt="${objtoolopt} --no-fp"
112134
fi
113-
if [ -n "${CONFIG_GCOV_KERNEL}" ]; then
135+
if [ -n "${CONFIG_GCOV_KERNEL}" ] || [ -n "${CONFIG_LTO_CLANG}" ]; then
114136
objtoolopt="${objtoolopt} --no-unreachable"
115137
fi
116138
if [ -n "${CONFIG_RETPOLINE}" ]; then
@@ -120,7 +142,7 @@ objtool_link()
120142
objtoolopt="${objtoolopt} --uaccess"
121143
fi
122144
info OBJTOOL ${1}
123-
tools/objtool/objtool ${objtoolopt} ${1}
145+
tools/objtool/objtool ${objtoolcmd} ${objtoolopt} ${1}
124146
fi
125147
}
126148

0 commit comments

Comments
 (0)