Commit d31ed5d7 authored by Peter Zijlstra's avatar Peter Zijlstra
Browse files

kbuild: Fixup the IBT kbuild changes



Masahiro-san deemed my kbuild changes to support whole module objtool
runs too terrible to live and gracefully provided an alternative.

Suggested-by: default avatarMasahiro Yamada <masahiroy@kernel.org>
Signed-off-by: default avatarPeter Zijlstra (Intel) <peterz@infradead.org>
Link: https://lkml.kernel.org/r/CAK7LNAQ2mYMnOKMQheVi+6byUFE3KEkjm1zcndNUfe0tORGvug@mail.gmail.com
parent 9cea0d46
Loading
Loading
Loading
Loading
+19 −47
Original line number Original line Diff line number Diff line
@@ -86,18 +86,12 @@ ifdef need-builtin
targets-for-builtin += $(obj)/built-in.a
targets-for-builtin += $(obj)/built-in.a
endif
endif


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


ifdef CONFIG_LTO_CLANG
ifneq ($(CONFIG_LTO_CLANG)$(CONFIG_X86_KERNEL_IBT),)
targets-for-modules += $(patsubst %.o, %.lto.o, $(filter %.o, $(obj-m)))
targets-for-modules += $(patsubst %.o, %.prelink.o, $(filter %.o, $(obj-m)))
endif

ifdef CONFIG_X86_KERNEL_IBT
targets-for-modules += $(patsubst %.o, %.objtool, $(filter %.o, $(obj-m)))
endif
endif


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

ifdef need-modorder
ifdef need-modorder
targets-for-modules += $(obj)/modules.order
targets-for-modules += $(obj)/modules.order
endif
endif
@@ -244,31 +238,16 @@ objtool_args = \
	$(if $(CONFIG_FTRACE_MCOUNT_USE_OBJTOOL), --mcount)		\
	$(if $(CONFIG_FTRACE_MCOUNT_USE_OBJTOOL), --mcount)		\
	$(if $(CONFIG_SLS), --sls)
	$(if $(CONFIG_SLS), --sls)


cmd_objtool = $(if $(objtool-enabled), ; $(objtool) $(objtool_args) $(@:.objtool=.o))
cmd_objtool = $(if $(objtool-enabled), ; $(objtool) $(objtool_args) $@)
cmd_gen_objtooldep = $(if $(objtool-enabled), { echo ; echo '$(@:.objtool=.o): $$(wildcard $(objtool))' ; } >> $(dot-target).cmd)
cmd_gen_objtooldep = $(if $(objtool-enabled), { echo ; echo '$@: $$(wildcard $(objtool))' ; } >> $(dot-target).cmd)


endif # CONFIG_STACK_VALIDATION
endif # CONFIG_STACK_VALIDATION


ifdef CONFIG_LTO_CLANG
ifneq ($(CONFIG_LTO_CLANG)$(CONFIG_X86_KERNEL_IBT),)


# Skip objtool for LLVM bitcode
# Skip objtool for LLVM bitcode
$(obj)/%.o: objtool-enabled :=
$(obj)/%.o: objtool-enabled :=


# objtool was skipped for LLVM bitcode, run it now that we have compiled
# modules into native code
$(obj)/%.lto.o: objtool-enabled = y
$(obj)/%.lto.o: part-of-module := y

else ifdef CONFIG_X86_KERNEL_IBT

# Skip objtool on individual files
$(obj)/%.o: objtool-enabled :=

# instead run objtool on the module as a whole, right before
# the final link pass with the linker script.
$(obj)/%.objtool: objtool-enabled = y
$(obj)/%.objtool: part-of-module := y

else
else


# 'OBJECT_FILES_NON_STANDARD := y': skip objtool checking for a directory
# 'OBJECT_FILES_NON_STANDARD := y': skip objtool checking for a directory
@@ -310,19 +289,24 @@ $(obj)/%.o: $(src)/%.c $(recordmcount_source) FORCE
	$(call if_changed_rule,cc_o_c)
	$(call if_changed_rule,cc_o_c)
	$(call cmd,force_checksrc)
	$(call cmd,force_checksrc)


ifdef CONFIG_LTO_CLANG
ifneq ($(CONFIG_LTO_CLANG)$(CONFIG_X86_KERNEL_IBT),)
# Module .o files may contain LLVM bitcode, compile them into native code
# Module .o files may contain LLVM bitcode, compile them into native code
# before ELF processing
# before ELF processing
quiet_cmd_cc_lto_link_modules = LTO [M] $@
quiet_cmd_cc_prelink_modules = LD [M]  $@
      cmd_cc_lto_link_modules =						\
      cmd_cc_prelink_modules =						\
	$(LD) $(ld_flags) -r -o $@					\
	$(LD) $(ld_flags) -r -o $@					\
		$(shell [ -s $(@:.lto.o=.o.symversions) ] &&		\
               $(shell [ -s $(@:.prelink.o=.o.symversions) ] &&		\
			echo -T $(@:.lto.o=.o.symversions))		\
                       echo -T $(@:.prelink.o=.o.symversions))		\
		--whole-archive $(filter-out FORCE,$^)			\
		--whole-archive $(filter-out FORCE,$^)			\
		$(cmd_objtool)
		$(cmd_objtool)


$(obj)/%.lto.o: $(obj)/%.o FORCE
# objtool was skipped for LLVM bitcode, run it now that we have compiled
	$(call if_changed,cc_lto_link_modules)
# modules into native code
$(obj)/%.prelink.o: objtool-enabled = y
$(obj)/%.prelink.o: part-of-module := y

$(obj)/%.prelink.o: $(obj)/%.o FORCE
	$(call if_changed,cc_prelink_modules)
endif
endif


cmd_mod = { \
cmd_mod = { \
@@ -333,18 +317,6 @@ cmd_mod = { \
$(obj)/%.mod: $(obj)/%$(mod-prelink-ext).o FORCE
$(obj)/%.mod: $(obj)/%$(mod-prelink-ext).o FORCE
	$(call if_changed,mod)
	$(call if_changed,mod)


#
# Since objtool will re-write the file it will change the timestamps, therefore
# it is critical that the %.objtool file gets a timestamp *after* objtool runs.
#
# Additionally, care must be had with ordering this rule against the other rules
# that take %.o as a dependency.
#
cmd_objtool_mod = true $(cmd_objtool) ; touch $@

$(obj)/%.objtool: $(obj)/%$(mod-prelink-ext).o FORCE
	$(call if_changed,objtool_mod)

quiet_cmd_cc_lst_c = MKLST   $@
quiet_cmd_cc_lst_c = MKLST   $@
      cmd_cc_lst_c = $(CC) $(c_flags) -g -c -o $*.o $< && \
      cmd_cc_lst_c = $(CC) $(c_flags) -g -c -o $*.o $< && \
		     $(CONFIG_SHELL) $(srctree)/scripts/makelst $*.o \
		     $(CONFIG_SHELL) $(srctree)/scripts/makelst $*.o \
@@ -498,7 +470,7 @@ $(obj)/lib.a: $(lib-y) FORCE
# Do not replace $(filter %.o,^) with $(real-prereqs). When a single object
# Do not replace $(filter %.o,^) with $(real-prereqs). When a single object
# module is turned into a multi object module, $^ will contain header file
# module is turned into a multi object module, $^ will contain header file
# dependencies recorded in the .*.cmd file.
# dependencies recorded in the .*.cmd file.
ifdef CONFIG_LTO_CLANG
ifneq ($(CONFIG_LTO_CLANG)$(CONFIG_X86_KERNEL_IBT),)
quiet_cmd_link_multi-m = AR [M]  $@
quiet_cmd_link_multi-m = AR [M]  $@
cmd_link_multi-m =						\
cmd_link_multi-m =						\
	$(cmd_update_lto_symversions);				\
	$(cmd_update_lto_symversions);				\
+2 −2
Original line number Original line Diff line number Diff line
@@ -230,11 +230,11 @@ dtc_cpp_flags = -Wp,-MMD,$(depfile).pre.tmp -nostdinc \
		 $(addprefix -I,$(DTC_INCLUDE))                          \
		 $(addprefix -I,$(DTC_INCLUDE))                          \
		 -undef -D__DTS__
		 -undef -D__DTS__


ifeq ($(CONFIG_LTO_CLANG),y)
ifneq ($(CONFIG_LTO_CLANG)$(CONFIG_X86_KERNEL_IBT),)
# With CONFIG_LTO_CLANG, .o files in modules might be LLVM bitcode, so we
# With CONFIG_LTO_CLANG, .o files in modules might be LLVM bitcode, so we
# need to run LTO to compile them into native code (.lto.o) before further
# need to run LTO to compile them into native code (.lto.o) before further
# processing.
# processing.
mod-prelink-ext := .lto
mod-prelink-ext := .prelink
endif
endif


# Useful for describing the dependency of composite objects
# Useful for describing the dependency of composite objects
+6 −6
Original line number Original line Diff line number Diff line
@@ -1989,9 +1989,9 @@ static char *remove_dot(char *s)
		if (m && (s[n + m] == '.' || s[n + m] == 0))
		if (m && (s[n + m] == '.' || s[n + m] == 0))
			s[n] = 0;
			s[n] = 0;


		/* strip trailing .lto */
		/* strip trailing .prelink */
		if (strends(s, ".lto"))
		if (strends(s, ".prelink"))
			s[strlen(s) - 4] = '\0';
			s[strlen(s) - 8] = '\0';
	}
	}
	return s;
	return s;
}
}
@@ -2015,9 +2015,9 @@ static void read_symbols(const char *modname)
		/* strip trailing .o */
		/* strip trailing .o */
		tmp = NOFAIL(strdup(modname));
		tmp = NOFAIL(strdup(modname));
		tmp[strlen(tmp) - 2] = '\0';
		tmp[strlen(tmp) - 2] = '\0';
		/* strip trailing .lto */
		/* strip trailing .prelink */
		if (strends(tmp, ".lto"))
		if (strends(tmp, ".prelink"))
			tmp[strlen(tmp) - 4] = '\0';
			tmp[strlen(tmp) - 8] = '\0';
		mod = new_module(tmp);
		mod = new_module(tmp);
		free(tmp);
		free(tmp);
	}
	}