kbuild: factor out the common objtool arguments

JIRA: https://issues.redhat.com/browse/RHEL-2773
Conflicts:
	scripts/Makefile.build
	scripts/Makefile.lib
	scripts/link-vmlinux.sh
	- Undo previous RHEL-only conflict workarounds in these files
	  from cs-9 commits that avoided this backport:
	  - 69cd8c67c7 ("x86: Add prefix symbols for function
	    padding")
	  - 2354000bfc ("objtool: Add entry UNRET validation")
	  - 73afd5ec29 ("objtool: Add --hacks=skylake")
	  - 21de65cbaf ("x86/ibt: Implement FineIBT")
	  - af8931b273 ("kbuild: Fixup the IBT kbuild changes")
	  - eeb9f34df065 ("x86/srso: Fix unret validation dependencies")
	scripts/Makefile.vmlinux_o
	- cs-9 is doesn't have 9ec6ab6ee5ca ("kbuild: use objtool-args-y
	  to clean up objtool arguments"), add CONFIG_CPU_SRSO check to
	  objtool_args and not vmlinux-objtool-args

commit b42d2306502419688190aa6dd4dab4a6def24b3d
Author: Masahiro Yamada <masahiroy@kernel.org>
Date:   Sun May 29 00:47:04 2022 +0900

    kbuild: factor out the common objtool arguments

    scripts/Makefile.build and scripts/link-vmlinux.sh have similar setups
    for the objtool arguments.

    It was difficult to factor out them because all the vmlinux build rules
    were written in a shell script. It is somewhat tedious to touch the two
    files every time a new objtool option is supported.

    To reduce the code duplication, move the objtool for vmlinux.o into
    scripts/Makefile.vmlinux_o. Then, move the common macros to Makefile.lib
    so they are shared between Makefile.build and Makefile.vmlinux_o.

    Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>
    Tested-by: Sedat Dilek <sedat.dilek@gmail.com> # LLVM-14 (x86-64)

Signed-off-by: Joe Lawrence <joe.lawrence@redhat.com>
This commit is contained in:
Joe Lawrence
2024-04-11 13:36:04 -04:00
parent 05c520e6c2
commit b206f546c7
4 changed files with 54 additions and 129 deletions
-28
View File
@@ -229,34 +229,6 @@ cmd_record_mcount = $(if $(findstring $(strip $(CC_FLAGS_FTRACE)),$(_c_flags)),
$(sub_cmd_record_mcount))
endif # CONFIG_FTRACE_MCOUNT_USE_RECORDMCOUNT
ifdef CONFIG_OBJTOOL
objtool := $(objtree)/tools/objtool/objtool
objtool_args = \
$(if $(CONFIG_HAVE_JUMP_LABEL_HACK), --hacks=jump_label) \
$(if $(CONFIG_HAVE_NOINSTR_HACK), --hacks=noinstr) \
$(if $(CONFIG_CALL_DEPTH_TRACKING), --hacks=skylake) \
$(if $(CONFIG_X86_KERNEL_IBT), --ibt) \
$(if $(CONFIG_FTRACE_MCOUNT_USE_OBJTOOL), --mcount) \
$(if $(CONFIG_UNWINDER_ORC), --orc) \
$(if $(CONFIG_RETPOLINE), --retpoline) \
$(if $(CONFIG_RETHUNK), --rethunk) \
$(if $(CONFIG_SLS), --sls) \
$(if $(CONFIG_STACK_VALIDATION), --stackval) \
$(if $(CONFIG_HAVE_STATIC_CALL_INLINE), --static-call) \
$(if $(CONFIG_HAVE_UACCESS_VALIDATION), --uaccess) \
$(if $(linked-object), --link) \
$(if $(part-of-module), --module) \
$(if $(CONFIG_GCOV_KERNEL), --no-unreachable) \
$(if $(CONFIG_PREFIX_SYMBOLS), --prefix=$(CONFIG_FUNCTION_PADDING_BYTES)) \
$(if $(CONFIG_FINEIBT), --cfi)
cmd_objtool = $(if $(objtool-enabled), ; $(objtool) $(objtool_args) $@)
cmd_gen_objtooldep = $(if $(objtool-enabled), { echo ; echo '$@: $$(wildcard $(objtool))' ; } >> $(dot-target).cmd)
endif # CONFIG_OBJTOOL
ifneq ($(CONFIG_LTO_CLANG)$(CONFIG_X86_KERNEL_IBT),)
# Skip objtool for LLVM bitcode
+28
View File
@@ -246,6 +246,34 @@ ifneq ($(CONFIG_LTO_CLANG)$(CONFIG_X86_KERNEL_IBT),)
mod-prelink-ext := .prelink
endif
ifdef CONFIG_OBJTOOL
objtool := $(objtree)/tools/objtool/objtool
objtool_args = \
$(if $(CONFIG_HAVE_JUMP_LABEL_HACK), --hacks=jump_label) \
$(if $(CONFIG_HAVE_NOINSTR_HACK), --hacks=noinstr) \
$(if $(CONFIG_CALL_DEPTH_TRACKING), --hacks=skylake) \
$(if $(CONFIG_X86_KERNEL_IBT), --ibt) \
$(if $(CONFIG_FTRACE_MCOUNT_USE_OBJTOOL), --mcount) \
$(if $(CONFIG_UNWINDER_ORC), --orc) \
$(if $(CONFIG_RETPOLINE), --retpoline) \
$(if $(CONFIG_RETHUNK), --rethunk) \
$(if $(CONFIG_SLS), --sls) \
$(if $(CONFIG_STACK_VALIDATION), --stackval) \
$(if $(CONFIG_HAVE_STATIC_CALL_INLINE), --static-call) \
$(if $(CONFIG_HAVE_UACCESS_VALIDATION), --uaccess) \
$(if $(linked-object), --link) \
$(if $(part-of-module), --module) \
$(if $(CONFIG_GCOV_KERNEL), --no-unreachable) \
$(if $(CONFIG_PREFIX_SYMBOLS), --prefix=$(CONFIG_FUNCTION_PADDING_BYTES)) \
$(if $(CONFIG_FINEIBT), --cfi)
cmd_objtool = $(if $(objtool-enabled), ; $(objtool) $(objtool_args) $@)
cmd_gen_objtooldep = $(if $(objtool-enabled), { echo ; echo '$@: $$(wildcard $(objtool))' ; } >> $(dot-target).cmd)
endif # CONFIG_OBJTOOL
# Useful for describing the dependency of composite objects
# Usage:
# $(call multi_depend, multi_used_targets, suffix_to_remove, suffix_to_add)
+26
View File
@@ -6,6 +6,9 @@ __default: vmlinux.o
include include/config/auto.conf
include $(srctree)/scripts/Kbuild.include
# for objtool
include $(srctree)/scripts/Makefile.lib
# Generate a linker script to ensure correct ordering of initcalls for Clang LTO
# ---------------------------------------------------------------------------
@@ -24,6 +27,27 @@ ifdef CONFIG_LTO_CLANG
initcalls-lds := .tmp_initcalls.lds
endif
# objtool for vmlinux.o
# ---------------------------------------------------------------------------
#
# For LTO and IBT, objtool doesn't run on individual translation units.
# Run everything on vmlinux instead.
objtool-enabled := $(or $(delay-objtool),$(CONFIG_NOINSTR_VALIDATION))
# Reuse objtool_args defined in scripts/Makefile.lib if LTO or IBT is enabled.
#
# Add some more flags as needed.
# --no-unreachable and --link might be added twice, but it is fine.
#
# Expand objtool_args to a simple variable to avoid circular reference.
objtool_args := \
$(if $(delay-objtool),$(objtool_args)) \
$(if $(CONFIG_NOINSTR_VALIDATION), --noinstr $(if $(or $(CONFIG_CPU_UNRET_ENTRY),$(CONFIG_CPU_SRSO)), --unret)) \
$(if $(CONFIG_GCOV_KERNEL), --no-unreachable) \
--link
# Link of vmlinux.o used for section mismatch analysis
# ---------------------------------------------------------------------------
@@ -33,9 +57,11 @@ quiet_cmd_ld_vmlinux.o = LD $@
$(addprefix -T , $(initcalls-lds)) \
--whole-archive $(KBUILD_VMLINUX_OBJS) --no-whole-archive \
--start-group $(KBUILD_VMLINUX_LIBS) --end-group \
$(cmd_objtool)
define rule_ld_vmlinux.o
$(call cmd_and_savecmd,ld_vmlinux.o)
$(call cmd,gen_objtooldep)
endef
vmlinux.o: $(initcalls-lds) $(KBUILD_VMLINUX_OBJS) $(KBUILD_VMLINUX_LIBS) FORCE
-101
View File
@@ -52,106 +52,6 @@ info()
printf " %-7s %s\n" "${1}" "${2}"
}
objtool_link()
{
local objtoolcmd;
local objtoolopt;
if ! is_enabled CONFIG_OBJTOOL; then
return;
fi
# RHEL-only workaround for missing upstream b42d23065024
# ("kbuild: factor out the common objtool arguments"), objtool
# is only enabled for vmlinux.o under the following conditions:
#
# scripts/Makefile.lib
# delay-objtool := $(or $(CONFIG_LTO_CLANG),$(CONFIG_X86_KERNEL_IBT))
#
# scripts/Makefile.vmlinux_o
# objtool-enabled := $(or $(delay-objtool),$(CONFIG_NOINSTR_VALIDATION))
if ! (is_enabled CONFIG_LTO_CLANG || is_enabled CONFIG_X86_KERNEL_IBT || is_enabled CONFIG_NOINSTR_VALIDATION); then
return
fi
if is_enabled CONFIG_LTO_CLANG || is_enabled CONFIG_X86_KERNEL_IBT; then
# For LTO and IBT, objtool doesn't run on individual
# translation units. Run everything on vmlinux instead.
if is_enabled CONFIG_HAVE_JUMP_LABEL_HACK; then
objtoolopt="${objtoolopt} --hacks=jump_label"
fi
if is_enabled CONFIG_HAVE_NOINSTR_HACK; then
objtoolopt="${objtoolopt} --hacks=noinstr"
fi
if is_enabled CONFIG_CALL_DEPTH_TRACKING; then
objtoolopt="${objtoolopt} --hacks=skylake"
fi
if is_enabled CONFIG_X86_KERNEL_IBT; then
objtoolopt="${objtoolopt} --ibt"
fi
if is_enabled CONFIG_FTRACE_MCOUNT_USE_OBJTOOL; then
objtoolopt="${objtoolopt} --mcount"
fi
if is_enabled CONFIG_FTRACE_MCOUNT_USE_OBJTOOL; then
objtoolopt="${objtoolopt} --mnop"
fi
if is_enabled CONFIG_UNWINDER_ORC; then
objtoolopt="${objtoolopt} --orc"
fi
if is_enabled CONFIG_RETPOLINE; then
objtoolopt="${objtoolopt} --retpoline"
fi
if is_enabled CONFIG_SLS; then
objtoolopt="${objtoolopt} --sls"
fi
if is_enabled CONFIG_STACK_VALIDATION; then
objtoolopt="${objtoolopt} --stackval"
fi
if is_enabled CONFIG_HAVE_STATIC_CALL_INLINE; then
objtoolopt="${objtoolopt} --static-call"
fi
if is_enabled CONFIG_HAVE_UACCESS_VALIDATION; then
objtoolopt="${objtoolopt} --uaccess"
fi
fi
if is_enabled CONFIG_NOINSTR_VALIDATION; then
objtoolopt="${objtoolopt} --noinstr"
if is_enabled CONFIG_CPU_UNRET_ENTRY || is_enabled CONFIG_CPU_SRSO; then
objtoolopt="${objtoolopt} --unret"
fi
fi
if is_enabled CONFIG_PREFIX_SYMBOLS; then
objtoolopt="${objtoolopt} --prefix=$(config_value "CONFIG_FUNCTION_PADDING_BYTES")"
fi
if [ -n "${objtoolopt}" ]; then
if is_enabled CONFIG_GCOV_KERNEL; then
objtoolopt="${objtoolopt} --no-unreachable"
fi
objtoolopt="${objtoolopt} --link"
info OBJTOOL ${1}
tools/objtool/objtool ${objtoolopt} ${1}
fi
}
# Link of vmlinux
# ${1} - output file
# ${2}, ${3}, ... - optional extra .o files
@@ -342,7 +242,6 @@ ${MAKE} -f "${srctree}/scripts/Makefile.build" obj=init need-builtin=1
#link vmlinux.o
${MAKE} -f "${srctree}/scripts/Makefile.vmlinux_o"
objtool_link vmlinux.o
# modpost vmlinux.o to check for section mismatches
${MAKE} -f "${srctree}/scripts/Makefile.modpost" MODPOST_VMLINUX=1