Masahiro Yamada | 9b9a3f2 | 2019-08-15 01:06:23 +0900 | [diff] [blame] | 1 | # SPDX-License-Identifier: GPL-2.0-only |
| 2 | # =========================================================================== |
| 3 | # Module final link |
| 4 | # =========================================================================== |
| 5 | |
| 6 | PHONY := __modfinal |
| 7 | __modfinal: |
| 8 | |
Sami Tolvanen | ba01e93 | 2020-03-06 14:14:03 -0800 | [diff] [blame] | 9 | include $(objtree)/include/config/auto.conf |
Masahiro Yamada | 9b9a3f2 | 2019-08-15 01:06:23 +0900 | [diff] [blame] | 10 | include $(srctree)/scripts/Kbuild.include |
| 11 | |
Sami Tolvanen | 6dd43b9 | 2020-04-13 16:10:13 -0700 | [diff] [blame] | 12 | # for c_flags and objtool_args |
Masahiro Yamada | 9b9a3f2 | 2019-08-15 01:06:23 +0900 | [diff] [blame] | 13 | include $(srctree)/scripts/Makefile.lib |
| 14 | |
| 15 | # find all modules listed in modules.order |
| 16 | modules := $(sort $(shell cat $(MODORDER))) |
| 17 | |
| 18 | __modfinal: $(modules) |
| 19 | @: |
| 20 | |
Masahiro Yamada | eb27ea5 | 2019-08-19 17:58:43 +0900 | [diff] [blame] | 21 | # modname and part-of-module are set to make c_flags define proper module flags |
Masahiro Yamada | 9b9a3f2 | 2019-08-15 01:06:23 +0900 | [diff] [blame] | 22 | modname = $(notdir $(@:.mod.o=)) |
Masahiro Yamada | eb27ea5 | 2019-08-19 17:58:43 +0900 | [diff] [blame] | 23 | part-of-module = y |
Masahiro Yamada | 9b9a3f2 | 2019-08-15 01:06:23 +0900 | [diff] [blame] | 24 | |
| 25 | quiet_cmd_cc_o_c = CC [M] $@ |
Sami Tolvanen | 2c351bb | 2019-04-25 16:09:05 -0700 | [diff] [blame] | 26 | cmd_cc_o_c = \ |
| 27 | $(CC) $(filter-out $(CC_FLAGS_CFI) $(CC_FLAGS_FTRACE), \ |
| 28 | $(c_flags)) -c -o $@ $< |
Masahiro Yamada | 9b9a3f2 | 2019-08-15 01:06:23 +0900 | [diff] [blame] | 29 | |
| 30 | %.mod.o: %.mod.c FORCE |
| 31 | $(call if_changed_dep,cc_o_c) |
| 32 | |
| 33 | ARCH_POSTLINK := $(wildcard $(srctree)/arch/$(SRCARCH)/Makefile.postlink) |
| 34 | |
Sami Tolvanen | ba01e93 | 2020-03-06 14:14:03 -0800 | [diff] [blame] | 35 | ifdef CONFIG_LTO_CLANG |
| 36 | # With CONFIG_LTO_CLANG, reuse the object file we compiled for modpost to |
| 37 | # avoid a second slow LTO link |
| 38 | prelink-ext := .lto |
Sami Tolvanen | 6dd43b9 | 2020-04-13 16:10:13 -0700 | [diff] [blame] | 39 | |
| 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 | |
| 43 | ifdef CONFIG_STACK_VALIDATION |
| 44 | ifneq ($(SKIP_STACK_VALIDATION),1) |
| 45 | cmd_ld_ko_o += \ |
| 46 | $(objtree)/tools/objtool/objtool $(objtool_args) \ |
| 47 | $(@:.ko=$(prelink-ext).o); |
| 48 | |
| 49 | endif # SKIP_STACK_VALIDATION |
| 50 | endif # CONFIG_STACK_VALIDATION |
| 51 | |
| 52 | endif # CONFIG_LTO_CLANG |
Sami Tolvanen | ba01e93 | 2020-03-06 14:14:03 -0800 | [diff] [blame] | 53 | |
Masahiro Yamada | 9b9a3f2 | 2019-08-15 01:06:23 +0900 | [diff] [blame] | 54 | quiet_cmd_ld_ko_o = LD [M] $@ |
Sami Tolvanen | 6dd43b9 | 2020-04-13 16:10:13 -0700 | [diff] [blame] | 55 | cmd_ld_ko_o += \ |
Masahiro Yamada | 9b9a3f2 | 2019-08-15 01:06:23 +0900 | [diff] [blame] | 56 | $(LD) -r $(KBUILD_LDFLAGS) \ |
| 57 | $(KBUILD_LDFLAGS_MODULE) $(LDFLAGS_MODULE) \ |
Masahiro Yamada | 596b047 | 2020-09-08 13:27:08 +0900 | [diff] [blame] | 58 | -T scripts/module.lds -o $@ $(filter %.o, $^); \ |
Masahiro Yamada | 9b9a3f2 | 2019-08-15 01:06:23 +0900 | [diff] [blame] | 59 | $(if $(ARCH_POSTLINK), $(MAKE) -f $(ARCH_POSTLINK) $@, true) |
| 60 | |
Sami Tolvanen | 27eb5ff | 2021-04-26 17:13:46 -0700 | [diff] [blame] | 61 | ifdef 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. |
| 64 | cmd_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 |
| 76 | endif |
| 77 | |
Sami Tolvanen | ba01e93 | 2020-03-06 14:14:03 -0800 | [diff] [blame] | 78 | $(modules): %.ko: %$(prelink-ext).o %.mod.o scripts/module.lds FORCE |
Masahiro Yamada | 9b9a3f2 | 2019-08-15 01:06:23 +0900 | [diff] [blame] | 79 | +$(call if_changed,ld_ko_o) |
| 80 | |
| 81 | targets += $(modules) $(modules:.ko=.mod.o) |
| 82 | |
| 83 | # Add FORCE to the prequisites of a target to force it to be always rebuilt. |
| 84 | # --------------------------------------------------------------------------- |
| 85 | |
| 86 | PHONY += FORCE |
| 87 | FORCE: |
| 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 | |
| 94 | existing-targets := $(wildcard $(sort $(targets))) |
| 95 | |
| 96 | -include $(foreach f,$(existing-targets),$(dir $(f)).$(notdir $(f)).cmd) |
| 97 | |
| 98 | .PHONY: $(PHONY) |