Skip to content

Commit b42d230

Browse files
committed
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 <[email protected]> Tested-by: Sedat Dilek <[email protected]> # LLVM-14 (x86-64)
1 parent 5d45950 commit b42d230

File tree

4 files changed

+52
-97
lines changed

4 files changed

+52
-97
lines changed

scripts/Makefile.build

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -210,38 +210,12 @@ cmd_record_mcount = $(if $(findstring $(strip $(CC_FLAGS_FTRACE)),$(_c_flags)),
210210
$(sub_cmd_record_mcount))
211211
endif # CONFIG_FTRACE_MCOUNT_USE_RECORDMCOUNT
212212

213-
ifdef CONFIG_OBJTOOL
214-
215-
objtool := $(objtree)/tools/objtool/objtool
216-
217-
objtool_args = \
218-
$(if $(CONFIG_HAVE_JUMP_LABEL_HACK), --hacks=jump_label) \
219-
$(if $(CONFIG_HAVE_NOINSTR_HACK), --hacks=noinstr) \
220-
$(if $(CONFIG_X86_KERNEL_IBT), --ibt) \
221-
$(if $(CONFIG_FTRACE_MCOUNT_USE_OBJTOOL), --mcount) \
222-
$(if $(CONFIG_UNWINDER_ORC), --orc) \
223-
$(if $(CONFIG_RETPOLINE), --retpoline) \
224-
$(if $(CONFIG_SLS), --sls) \
225-
$(if $(CONFIG_STACK_VALIDATION), --stackval) \
226-
$(if $(CONFIG_HAVE_STATIC_CALL_INLINE), --static-call) \
227-
--uaccess \
228-
$(if $(delay-objtool), --link) \
229-
$(if $(part-of-module), --module) \
230-
$(if $(CONFIG_GCOV_KERNEL), --no-unreachable)
231-
232-
cmd_objtool = $(if $(objtool-enabled), ; $(objtool) $(objtool_args) $@)
233-
cmd_gen_objtooldep = $(if $(objtool-enabled), { echo ; echo '$@: $$(wildcard $(objtool))' ; } >> $(dot-target).cmd)
234-
235-
endif # CONFIG_OBJTOOL
236-
237213
# 'OBJECT_FILES_NON_STANDARD := y': skip objtool checking for a directory
238214
# 'OBJECT_FILES_NON_STANDARD_foo.o := 'y': skip objtool checking for a file
239215
# 'OBJECT_FILES_NON_STANDARD_foo.o := 'n': override directory skip for a file
240216

241217
is-standard-object = $(if $(filter-out y%, $(OBJECT_FILES_NON_STANDARD_$(basetarget).o)$(OBJECT_FILES_NON_STANDARD)n),y)
242218

243-
delay-objtool := $(or $(CONFIG_LTO_CLANG),$(CONFIG_X86_KERNEL_IBT))
244-
245219
$(obj)/%.o: objtool-enabled = $(if $(is-standard-object),$(if $(delay-objtool),$(is-single-obj-m),y))
246220

247221
ifdef CONFIG_TRIM_UNUSED_KSYMS

scripts/Makefile.lib

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

228+
ifdef CONFIG_OBJTOOL
229+
230+
objtool := $(objtree)/tools/objtool/objtool
231+
232+
objtool_args = \
233+
$(if $(CONFIG_HAVE_JUMP_LABEL_HACK), --hacks=jump_label) \
234+
$(if $(CONFIG_HAVE_NOINSTR_HACK), --hacks=noinstr) \
235+
$(if $(CONFIG_X86_KERNEL_IBT), --ibt) \
236+
$(if $(CONFIG_FTRACE_MCOUNT_USE_OBJTOOL), --mcount) \
237+
$(if $(CONFIG_UNWINDER_ORC), --orc) \
238+
$(if $(CONFIG_RETPOLINE), --retpoline) \
239+
$(if $(CONFIG_SLS), --sls) \
240+
$(if $(CONFIG_STACK_VALIDATION), --stackval) \
241+
$(if $(CONFIG_HAVE_STATIC_CALL_INLINE), --static-call) \
242+
--uaccess \
243+
$(if $(delay-objtool), --link) \
244+
$(if $(part-of-module), --module) \
245+
$(if $(CONFIG_GCOV_KERNEL), --no-unreachable)
246+
247+
delay-objtool := $(or $(CONFIG_LTO_CLANG),$(CONFIG_X86_KERNEL_IBT))
248+
249+
cmd_objtool = $(if $(objtool-enabled), ; $(objtool) $(objtool_args) $@)
250+
cmd_gen_objtooldep = $(if $(objtool-enabled), { echo ; echo '$@: $$(wildcard $(objtool))' ; } >> $(dot-target).cmd)
251+
252+
endif # CONFIG_OBJTOOL
253+
228254
# Useful for describing the dependency of composite objects
229255
# Usage:
230256
# $(call multi_depend, multi_used_targets, suffix_to_remove, suffix_to_add)

scripts/Makefile.vmlinux_o

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ __default: vmlinux.o
66
include include/config/auto.conf
77
include $(srctree)/scripts/Kbuild.include
88

9+
# for objtool
10+
include $(srctree)/scripts/Makefile.lib
11+
912
# Generate a linker script to ensure correct ordering of initcalls for Clang LTO
1013
# ---------------------------------------------------------------------------
1114

@@ -24,6 +27,27 @@ ifdef CONFIG_LTO_CLANG
2427
initcalls-lds := .tmp_initcalls.lds
2528
endif
2629

30+
# objtool for vmlinux.o
31+
# ---------------------------------------------------------------------------
32+
#
33+
# For LTO and IBT, objtool doesn't run on individual translation units.
34+
# Run everything on vmlinux instead.
35+
36+
objtool-enabled := $(or $(delay-objtool),$(CONFIG_NOINSTR_VALIDATION))
37+
38+
# Reuse objtool_args defined in scripts/Makefile.lib if LTO or IBT is enabled.
39+
#
40+
# Add some more flags as needed.
41+
# --no-unreachable and --link might be added twice, but it is fine.
42+
#
43+
# Expand objtool_args to a simple variable to avoid circular reference.
44+
45+
objtool_args := \
46+
$(if $(delay-objtool),$(objtool_args)) \
47+
$(if $(CONFIG_NOINSTR_VALIDATION), --noinstr) \
48+
$(if $(CONFIG_GCOV_KERNEL), --no-unreachable) \
49+
--link
50+
2751
# Link of vmlinux.o used for section mismatch analysis
2852
# ---------------------------------------------------------------------------
2953

@@ -33,9 +57,11 @@ quiet_cmd_ld_vmlinux.o = LD $@
3357
$(addprefix -T , $(initcalls-lds)) \
3458
--whole-archive $(KBUILD_VMLINUX_OBJS) --no-whole-archive \
3559
--start-group $(KBUILD_VMLINUX_LIBS) --end-group \
60+
$(cmd_objtool)
3661

3762
define rule_ld_vmlinux.o
3863
$(call cmd_and_savecmd,ld_vmlinux.o)
64+
$(call cmd,gen_objtooldep)
3965
endef
4066

4167
vmlinux.o: $(initcalls-lds) $(KBUILD_VMLINUX_OBJS) $(KBUILD_VMLINUX_LIBS) FORCE

scripts/link-vmlinux.sh

Lines changed: 0 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -45,76 +45,6 @@ info()
4545
printf " %-7s %s\n" "${1}" "${2}"
4646
}
4747

48-
objtool_link()
49-
{
50-
local objtoolcmd;
51-
local objtoolopt;
52-
53-
if ! is_enabled CONFIG_OBJTOOL; then
54-
return;
55-
fi
56-
57-
if is_enabled CONFIG_LTO_CLANG || is_enabled CONFIG_X86_KERNEL_IBT; then
58-
59-
# For LTO and IBT, objtool doesn't run on individual
60-
# translation units. Run everything on vmlinux instead.
61-
62-
if is_enabled CONFIG_HAVE_JUMP_LABEL_HACK; then
63-
objtoolopt="${objtoolopt} --hacks=jump_label"
64-
fi
65-
66-
if is_enabled CONFIG_HAVE_NOINSTR_HACK; then
67-
objtoolopt="${objtoolopt} --hacks=noinstr"
68-
fi
69-
70-
if is_enabled CONFIG_X86_KERNEL_IBT; then
71-
objtoolopt="${objtoolopt} --ibt"
72-
fi
73-
74-
if is_enabled CONFIG_FTRACE_MCOUNT_USE_OBJTOOL; then
75-
objtoolopt="${objtoolopt} --mcount"
76-
fi
77-
78-
if is_enabled CONFIG_UNWINDER_ORC; then
79-
objtoolopt="${objtoolopt} --orc"
80-
fi
81-
82-
if is_enabled CONFIG_RETPOLINE; then
83-
objtoolopt="${objtoolopt} --retpoline"
84-
fi
85-
86-
if is_enabled CONFIG_SLS; then
87-
objtoolopt="${objtoolopt} --sls"
88-
fi
89-
90-
if is_enabled CONFIG_STACK_VALIDATION; then
91-
objtoolopt="${objtoolopt} --stackval"
92-
fi
93-
94-
if is_enabled CONFIG_HAVE_STATIC_CALL_INLINE; then
95-
objtoolopt="${objtoolopt} --static-call"
96-
fi
97-
98-
objtoolopt="${objtoolopt} --uaccess"
99-
fi
100-
101-
if is_enabled CONFIG_NOINSTR_VALIDATION; then
102-
objtoolopt="${objtoolopt} --noinstr"
103-
fi
104-
105-
if [ -n "${objtoolopt}" ]; then
106-
107-
if is_enabled CONFIG_GCOV_KERNEL; then
108-
objtoolopt="${objtoolopt} --no-unreachable"
109-
fi
110-
111-
objtoolopt="${objtoolopt} --link"
112-
113-
info OBJTOOL ${1}
114-
tools/objtool/objtool ${objtoolopt} ${1}
115-
fi
116-
}
117-
11848
# Link of vmlinux
11949
# ${1} - output file
12050
# ${2}, ${3}, ... - optional extra .o files
@@ -298,7 +228,6 @@ ${MAKE} -f "${srctree}/scripts/Makefile.build" obj=init need-builtin=1
298228

299229
#link vmlinux.o
300230
${MAKE} -f "${srctree}/scripts/Makefile.vmlinux_o"
301-
objtool_link vmlinux.o
302231

303232
# Generate the list of in-tree objects in vmlinux
304233
#

0 commit comments

Comments
 (0)