blob: 9ae3d9ae3d3674cdc3879ee6f448b3bbaaadad2f [file] [log] [blame]
Masahiro Yamada9b9a3f22019-08-15 01:06:23 +09001# SPDX-License-Identifier: GPL-2.0-only
2# ===========================================================================
3# Module final link
4# ===========================================================================
5
6PHONY := __modfinal
7__modfinal:
8
Sami Tolvanenba01e932020-03-06 14:14:03 -08009include $(objtree)/include/config/auto.conf
Masahiro Yamada9b9a3f22019-08-15 01:06:23 +090010include $(srctree)/scripts/Kbuild.include
11
Sami Tolvanen6dd43b92020-04-13 16:10:13 -070012# for c_flags and objtool_args
Masahiro Yamada9b9a3f22019-08-15 01:06:23 +090013include $(srctree)/scripts/Makefile.lib
14
15# find all modules listed in modules.order
16modules := $(sort $(shell cat $(MODORDER)))
17
18__modfinal: $(modules)
19 @:
20
Masahiro Yamadaeb27ea52019-08-19 17:58:43 +090021# modname and part-of-module are set to make c_flags define proper module flags
Masahiro Yamada9b9a3f22019-08-15 01:06:23 +090022modname = $(notdir $(@:.mod.o=))
Masahiro Yamadaeb27ea52019-08-19 17:58:43 +090023part-of-module = y
Masahiro Yamada9b9a3f22019-08-15 01:06:23 +090024
25quiet_cmd_cc_o_c = CC [M] $@
Sami Tolvanen2c351bb2019-04-25 16:09:05 -070026 cmd_cc_o_c = \
27 $(CC) $(filter-out $(CC_FLAGS_CFI) $(CC_FLAGS_FTRACE), \
28 $(c_flags)) -c -o $@ $<
Masahiro Yamada9b9a3f22019-08-15 01:06:23 +090029
30%.mod.o: %.mod.c FORCE
31 $(call if_changed_dep,cc_o_c)
32
33ARCH_POSTLINK := $(wildcard $(srctree)/arch/$(SRCARCH)/Makefile.postlink)
34
Sami Tolvanenba01e932020-03-06 14:14:03 -080035ifdef CONFIG_LTO_CLANG
36# With CONFIG_LTO_CLANG, reuse the object file we compiled for modpost to
37# avoid a second slow LTO link
38prelink-ext := .lto
Sami Tolvanen6dd43b92020-04-13 16:10:13 -070039
40# ELF processing was skipped earlier because we didn't have native code,
41# so let's now process the prelinked binary before we link the module.
42
43ifdef CONFIG_STACK_VALIDATION
44ifneq ($(SKIP_STACK_VALIDATION),1)
45cmd_ld_ko_o += \
46 $(objtree)/tools/objtool/objtool $(objtool_args) \
47 $(@:.ko=$(prelink-ext).o);
48
49endif # SKIP_STACK_VALIDATION
50endif # CONFIG_STACK_VALIDATION
51
52endif # CONFIG_LTO_CLANG
Sami Tolvanenba01e932020-03-06 14:14:03 -080053
Masahiro Yamada9b9a3f22019-08-15 01:06:23 +090054quiet_cmd_ld_ko_o = LD [M] $@
Sami Tolvanen6dd43b92020-04-13 16:10:13 -070055 cmd_ld_ko_o += \
Masahiro Yamada9b9a3f22019-08-15 01:06:23 +090056 $(LD) -r $(KBUILD_LDFLAGS) \
57 $(KBUILD_LDFLAGS_MODULE) $(LDFLAGS_MODULE) \
Masahiro Yamada596b0472020-09-08 13:27:08 +090058 -T scripts/module.lds -o $@ $(filter %.o, $^); \
Masahiro Yamada9b9a3f22019-08-15 01:06:23 +090059 $(if $(ARCH_POSTLINK), $(MAKE) -f $(ARCH_POSTLINK) $@, true)
60
Sami Tolvanen27eb5ff2021-04-26 17:13:46 -070061ifdef CONFIG_CFI_CLANG
62# LLVM can drops jump table symbols from the final binary. Add them
63# back to make stack traces and other symbol output readable.
64cmd_ld_ko_o += ; \
65 $(srctree)/scripts/generate_cfi_kallsyms.pl --module \
66 $@ > $(@:.ko=.lds); \
67 if [ -s $(@:.ko=.lds) ]; then \
68 $(LD) -r $(KBUILD_LDFLAGS) \
69 $(KBUILD_LDFLAGS_MODULE) $(LDFLAGS_MODULE) \
70 -T $(@:.ko=.lds) \
71 -o $(@:.ko=.tmp.ko) $@; \
72 mv -f $(@:.ko=.tmp.ko) $@; \
73 else \
74 rm -f $(@:.ko=.lds); \
75 fi
76endif
77
Sami Tolvanenba01e932020-03-06 14:14:03 -080078$(modules): %.ko: %$(prelink-ext).o %.mod.o scripts/module.lds FORCE
Masahiro Yamada9b9a3f22019-08-15 01:06:23 +090079 +$(call if_changed,ld_ko_o)
80
81targets += $(modules) $(modules:.ko=.mod.o)
82
83# Add FORCE to the prequisites of a target to force it to be always rebuilt.
84# ---------------------------------------------------------------------------
85
86PHONY += FORCE
87FORCE:
88
89# Read all saved command lines and dependencies for the $(targets) we
90# may be building above, using $(if_changed{,_dep}). As an
91# optimization, we don't need to read them if the target does not
92# exist, we will rebuild anyway in that case.
93
94existing-targets := $(wildcard $(sort $(targets)))
95
96-include $(foreach f,$(existing-targets),$(dir $(f)).$(notdir $(f)).cmd)
97
98.PHONY: $(PHONY)