History

Merge tag 'clang-lto-v5.12-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/kees/linux

Pull clang LTO updates from Kees Cook: "Clang Link Time Optimization. This is built on the work done preparing for LTO by arm64 folks, tracing folks, etc. This includes the core changes as well as the remaining pieces for arm64 (LTO has been the default build method on Android for about 3 years now, as it is the prerequisite for the Control Flow Integrity protections). While x86 LTO enablement is done, it depends on some pending objtool clean-ups. It's possible that I'll send a "part 2" pull request for LTO that includes x86 support. For merge log posterity, and as detailed in commit dc5723b02e52 ("kbuild: add support for Clang LTO"), here is the lt;dr to do an LTO build: make LLVM=1 LLVM_IAS=1 defconfig scripts/config -e LTO_CLANG_THIN make LLVM=1 LLVM_IAS=1 (To do a cross-compile of arm64, add "CROSS_COMPILE=aarch64-linux-gnu-" and "ARCH=arm64" to the "make" command lines.) Summary: - Clang LTO build infrastructure and arm64-specific enablement (Sami Tolvanen) - Recursive build CC_FLAGS_LTO fix (Alexander Lobakin)" * tag 'clang-lto-v5.12-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/kees/linux: kbuild: prevent CC_FLAGS_LTO self-bloating on recursive rebuilds arm64: allow LTO to be selected arm64: disable recordmcount with DYNAMIC_FTRACE_WITH_REGS arm64: vdso: disable LTO drivers/misc/lkdtm: disable LTO for rodata.o efi/libstub: disable LTO scripts/mod: disable LTO for empty.c modpost: lto: strip .lto from module names PCI: Fix PREL32 relocations for LTO init: lto: fix PREL32 relocations init: lto: ensure initcall ordering kbuild: lto: add a default list of used symbols kbuild: lto: merge module sections kbuild: lto: limit inlining kbuild: lto: fix module versioning kbuild: add support for Clang LTO tracing: move function tracer options to Kconfig

Linus Torvalds committed 5 years ago

Commit 79db4d22 parents 3b9cdaf 2b8689524 files · +704 −59Browse files

Unified · Split

24 files
  1. .gitignore +1 −0
  2. Makefile +30 −15
  3. arch/Kconfig +90 −0
  4. arch/arm64/Kconfig +4 −0
  5. arch/arm64/kernel/vdso/Makefile +2 −1
  6. drivers/firmware/efi/libstub/Makefile +2 −0
  7. drivers/misc/lkdtm/Makefile +1 −0
  8. include/asm-generic/vmlinux.lds.h +4 −1
  9. include/linux/init.h +70 −9
  10. include/linux/pci.h +25 −2
  11. init/Kconfig +1 −0
  12. kernel/trace/Kconfig +16 −0
  13. scripts/Makefile.build +41 −7
  14. scripts/Makefile.lib +4 −2
  15. scripts/Makefile.modfinal +8 −1
  16. scripts/Makefile.modpost +23 −2
  17. scripts/generate_initcall_order.pl +270 −0
  18. scripts/link-vmlinux.sh +61 −9
  19. scripts/lto-used-symbollist.txt +5 −0
  20. scripts/mod/Makefile +1 −0
  21. scripts/mod/modpost.c +7 −9
  22. scripts/mod/modpost.h +9 −0
  23. scripts/mod/sumversion.c +5 −1
  24. scripts/module.lds.S +24 −0
.gitignore+1−0
@@ -42,6 +42,7 @@ · 41 unmodified lines
4242 *.so.dbg
4343 *.su
4444 *.symtypes
45+*.symversions
4546 *.tab.[ch]
4647 *.tar
4748 *.xz
Makefile+30−15
@@ -853,12 +853,8 @@ KBUILD_CFLAGS += $(DEBUG_CFLAGS) · 852 unmodified lines
853853 export DEBUG_CFLAGS
854854
855855 ifdef CONFIG_FUNCTION_TRACER
856−ifdef CONFIG_FTRACE_MCOUNT_RECORD
857− # gcc 5 supports generating the mcount tables directly
858− ifeq ($(call cc-option-yn,-mrecord-mcount),y)
859− CC_FLAGS_FTRACE += -mrecord-mcount
860− export CC_USING_RECORD_MCOUNT := 1
861− endif
856+ifdef CONFIG_FTRACE_MCOUNT_USE_CC
857+ CC_FLAGS_FTRACE += -mrecord-mcount
862858 ifdef CONFIG_HAVE_NOP_MCOUNT
863859 ifeq ($(call cc-option-yn, -mnop-mcount),y)
864860 CC_FLAGS_FTRACE += -mnop-mcount
@@ -866,6 +862,12 @@ ifdef CONFIG_FTRACE_MCOUNT_RECORD · 1 unmodified line
866862 endif
867863 endif
868864 endif
865+ifdef CONFIG_FTRACE_MCOUNT_USE_RECORDMCOUNT
866+ ifdef CONFIG_HAVE_C_RECORDMCOUNT
867+ BUILD_C_RECORDMCOUNT := y
868+ export BUILD_C_RECORDMCOUNT
869+ endif
870+endif
869871 ifdef CONFIG_HAVE_FENTRY
870872 ifeq ($(call cc-option-yn, -mfentry),y)
871873 CC_FLAGS_FTRACE += -mfentry
@@ -875,12 +877,6 @@ endif · 3 unmodified lines
875877 export CC_FLAGS_FTRACE
876878 KBUILD_CFLAGS += $(CC_FLAGS_FTRACE) $(CC_FLAGS_USING)
877879 KBUILD_AFLAGS += $(CC_FLAGS_USING)
878−ifdef CONFIG_DYNAMIC_FTRACE
879− ifdef CONFIG_HAVE_C_RECORDMCOUNT
880− BUILD_C_RECORDMCOUNT := y
881− export BUILD_C_RECORDMCOUNT
882− endif
883−endif
884880 endif
885881
886882 # We trigger additional mismatches with less inlining
@@ -899,6 +895,24 @@ KBUILD_CFLAGS += $(CC_FLAGS_SCS) · 12 unmodified lines
899895 export CC_FLAGS_SCS
900896 endif
901897
898+ifdef CONFIG_LTO_CLANG
899+ifdef CONFIG_LTO_CLANG_THIN
900+CC_FLAGS_LTO := -flto=thin -fsplit-lto-unit
901+KBUILD_LDFLAGS += --thinlto-cache-dir=$(extmod-prefix).thinlto-cache
902+else
903+CC_FLAGS_LTO := -flto
904+endif
905+CC_FLAGS_LTO += -fvisibility=hidden
906+
907+# Limit inlining across translation units to reduce binary size
908+KBUILD_LDFLAGS += -mllvm -import-instr-limit=5
909+endif
910+
911+ifdef CONFIG_LTO
912+KBUILD_CFLAGS += $(CC_FLAGS_LTO)
913+export CC_FLAGS_LTO
914+endif
915+
902916 ifdef CONFIG_DEBUG_FORCE_FUNCTION_ALIGN_32B
903917 KBUILD_CFLAGS += -falign-functions=32
904918 endif
@@ -1493,7 +1507,7 @@ MRPROPER_FILES += include/config include/generated \ · 588 unmodified lines
14931507 *.spec
14941508
14951509 # Directories & files removed with 'make distclean'
1496−DISTCLEAN_FILES += tags TAGS cscope* GPATH GTAGS GRTAGS GSYMS
1510+DISTCLEAN_FILES += tags TAGS cscope* GPATH GTAGS GRTAGS GSYMS .thinlto-cache
14971511
14981512 # clean - Delete most, but leave enough to build external modules
14991513 #
@@ -1739,7 +1753,7 @@ PHONY += compile_commands.json · 239 unmodified lines
17391753
17401754 clean-dirs := $(KBUILD_EXTMOD)
17411755 clean: rm-files := $(KBUILD_EXTMOD)/Module.symvers $(KBUILD_EXTMOD)/modules.nsdeps \
1742− $(KBUILD_EXTMOD)/compile_commands.json
1756+ $(KBUILD_EXTMOD)/compile_commands.json $(KBUILD_EXTMOD)/.thinlto-cache
17431757
17441758 PHONY += help
17451759 help:
@@ -1836,7 +1850,8 @@ clean: $(clean-dirs) · 90 unmodified lines
18361850 -o -name '.tmp_*.o.*' \
18371851 -o -name '*.c.[012]*.*' \
18381852 -o -name '*.ll' \
1839− -o -name '*.gcno' \) -type f -print | xargs rm -f
1853+ -o -name '*.gcno' \
1854+ -o -name '*.*.symversions' \) -type f -print | xargs rm -f
18401855
18411856 # Generate tags for editors
18421857 # ---------------------------------------------------------------------------
arch/Kconfig+90−0
@@ -603,6 +603,96 @@ config SHADOW_CALL_STACK · 602 unmodified lines
603603 reading and writing arbitrary memory may be able to locate them
604604 and hijack control flow by modifying the stacks.
605605
606+config LTO
607+ bool
608+ help
609+ Selected if the kernel will be built using the compiler's LTO feature.
610+
611+config LTO_CLANG
612+ bool
613+ select LTO
614+ help
615+ Selected if the kernel will be built using Clang's LTO feature.
616+
617+config ARCH_SUPPORTS_LTO_CLANG
618+ bool
619+ help
620+ An architecture should select this option if it supports:
621+ - compiling with Clang,
622+ - compiling inline assembly with Clang's integrated assembler,
623+ - and linking with LLD.
624+
625+config ARCH_SUPPORTS_LTO_CLANG_THIN
626+ bool
627+ help
628+ An architecture should select this option if it can support Clang's
629+ ThinLTO mode.
630+
631+config HAS_LTO_CLANG
632+ def_bool y
633+ # Clang >= 11: https://github.com/ClangBuiltLinux/linux/issues/510
634+ depends on CC_IS_CLANG && CLANG_VERSION >= 110000 && LD_IS_LLD
635+ depends on $(success,test $(LLVM) -eq 1)
636+ depends on $(success,test $(LLVM_IAS) -eq 1)
637+ depends on $(success,$(NM) --help | head -n 1 | grep -qi llvm)
638+ depends on $(success,$(AR) --help | head -n 1 | grep -qi llvm)
639+ depends on ARCH_SUPPORTS_LTO_CLANG
640+ depends on !FTRACE_MCOUNT_USE_RECORDMCOUNT
641+ depends on !KASAN
642+ depends on !GCOV_KERNEL
643+ help
644+ The compiler and Kconfig options support building with Clang's
645+ LTO.
646+
647+choice
648+ prompt "Link Time Optimization (LTO)"
649+ default LTO_NONE
650+ help
651+ This option enables Link Time Optimization (LTO), which allows the
652+ compiler to optimize binaries globally.
653+
654+ If unsure, select LTO_NONE. Note that LTO is very resource-intensive
655+ so it's disabled by default.
656+
657+config LTO_NONE
658+ bool "None"
659+ help
660+ Build the kernel normally, without Link Time Optimization (LTO).
661+
662+config LTO_CLANG_FULL
663+ bool "Clang Full LTO (EXPERIMENTAL)"
664+ depends on HAS_LTO_CLANG
665+ depends on !COMPILE_TEST
666+ select LTO_CLANG
667+ help
668+ This option enables Clang's full Link Time Optimization (LTO), which
669+ allows the compiler to optimize the kernel globally. If you enable
670+ this option, the compiler generates LLVM bitcode instead of ELF
671+ object files, and the actual compilation from bitcode happens at
672+ the LTO link step, which may take several minutes depending on the
673+ kernel configuration. More information can be found from LLVM's
674+ documentation:
675+
676+ https://llvm.org/docs/LinkTimeOptimization.html
677+
678+ During link time, this option can use a large amount of RAM, and
679+ may take much longer than the ThinLTO option.
680+
681+config LTO_CLANG_THIN
682+ bool "Clang ThinLTO (EXPERIMENTAL)"
683+ depends on HAS_LTO_CLANG && ARCH_SUPPORTS_LTO_CLANG_THIN
684+ select LTO_CLANG
685+ help
686+ This option enables Clang's ThinLTO, which allows for parallel
687+ optimization and faster incremental compiles compared to the
688+ CONFIG_LTO_CLANG_FULL option. More information can be found
689+ from Clang's documentation:
690+
691+ https://clang.llvm.org/docs/ThinLTO.html
692+
693+ If unsure, say Y.
694+endchoice
695+
606696 config HAVE_ARCH_WITHIN_STACK_FRAMES
607697 bool
608698 help
arch/arm64/Kconfig+4−0
@@ -73,6 +73,8 @@ config ARM64 · 72 unmodified lines
7373 select ARCH_SUPPORTS_DEBUG_PAGEALLOC
7474 select ARCH_SUPPORTS_MEMORY_FAILURE
7575 select ARCH_SUPPORTS_SHADOW_CALL_STACK if CC_HAVE_SHADOW_CALL_STACK
76+ select ARCH_SUPPORTS_LTO_CLANG if CPU_LITTLE_ENDIAN
77+ select ARCH_SUPPORTS_LTO_CLANG_THIN
7678 select ARCH_SUPPORTS_ATOMIC_RMW
7779 select ARCH_SUPPORTS_INT128 if CC_HAS_INT128 && (GCC_VERSION >= 50000 || CC_IS_CLANG)
7880 select ARCH_SUPPORTS_NUMA_BALANCING
@@ -162,6 +164,8 @@ config ARM64 · 83 unmodified lines
162164 select HAVE_DYNAMIC_FTRACE
163165 select HAVE_DYNAMIC_FTRACE_WITH_REGS \
164166 if $(cc-option,-fpatchable-function-entry=2)
167+ select FTRACE_MCOUNT_USE_PATCHABLE_FUNCTION_ENTRY \
168+ if DYNAMIC_FTRACE_WITH_REGS
165169 select HAVE_EFFICIENT_UNALIGNED_ACCESS
166170 select HAVE_FAST_GUP
167171 select HAVE_FTRACE_MCOUNT_RECORD
arch/arm64/kernel/vdso/Makefile+2−1
@@ -29,7 +29,8 @@ ldflags-y := -shared -nostdlib -soname=linux-vdso.so.1 --hash-style=sysv \ · 28 unmodified lines
2929 ccflags-y := -fno-common -fno-builtin -fno-stack-protector -ffixed-x18
3030 ccflags-y += -DDISABLE_BRANCH_PROFILING -DBUILD_VDSO
3131
32−CFLAGS_REMOVE_vgettimeofday.o = $(CC_FLAGS_FTRACE) -Os $(CC_FLAGS_SCS) $(GCC_PLUGINS_CFLAGS)
32+CFLAGS_REMOVE_vgettimeofday.o = $(CC_FLAGS_FTRACE) -Os $(CC_FLAGS_SCS) $(GCC_PLUGINS_CFLAGS) \
33+ $(CC_FLAGS_LTO)
3334 KASAN_SANITIZE := n
3435 UBSAN_SANITIZE := n
3536 OBJECT_FILES_NON_STANDARD := y
drivers/firmware/efi/libstub/Makefile+2−0
@@ -38,6 +38,8 @@ KBUILD_CFLAGS := $(cflags-y) -Os -DDISABLE_BRANCH_PROFILING \ · 37 unmodified lines
3838
3939 # remove SCS flags from all objects in this directory
4040 KBUILD_CFLAGS := $(filter-out $(CC_FLAGS_SCS), $(KBUILD_CFLAGS))
41+# disable LTO
42+KBUILD_CFLAGS := $(filter-out $(CC_FLAGS_LTO), $(KBUILD_CFLAGS))
4143
4244 GCOV_PROFILE := n
4345 # Sanitizer runtimes are unavailable and cannot be linked here.
drivers/misc/lkdtm/Makefile+1−0
@@ -16,6 +16,7 @@ lkdtm-$(CONFIG_PPC_BOOK3S_64) += powerpc.o · 15 unmodified lines
1616 KASAN_SANITIZE_rodata.o := n
1717 KASAN_SANITIZE_stackleak.o := n
1818 KCOV_INSTRUMENT_rodata.o := n
19+CFLAGS_REMOVE_rodata.o += $(CC_FLAGS_LTO)
1920
2021 OBJCOPYFLAGS :=
2122 OBJCOPYFLAGS_rodata_objcopy.o := \
include/asm-generic/vmlinux.lds.h+4−1
@@ -90,10 +90,13 @@ · 89 unmodified lines
9090 * .data. We don't want to pull in .data..other sections, which Linux
9191 * has defined. Same for text and bss.
9292 *
93+ * With LTO_CLANG, the linker also splits sections by default, so we need
94+ * these macros to combine the sections during the final link.
95+ *
9396 * RODATA_MAIN is not used because existing code already defines .rodata.x
9497 * sections to be brought in with rodata.
9598 */
96−#ifdef CONFIG_LD_DEAD_CODE_DATA_ELIMINATION
99+#if defined(CONFIG_LD_DEAD_CODE_DATA_ELIMINATION) || defined(CONFIG_LTO_CLANG)
97100 #define TEXT_MAIN .text .text.[0-9a-zA-Z_]*
98101 #define DATA_MAIN .data .data.[0-9a-zA-Z_]* .data..L* .data..compoundliteral* .data.$__unnamed_* .data.$Lubsan_*
99102 #define SDATA_MAIN .sdata .sdata.[0-9a-zA-Z_]*
include/linux/init.h+70−9
@@ -184,19 +184,80 @@ extern bool initcall_debug; · 183 unmodified lines
184184 * as KEEP() in the linker script.
185185 */
186186
187+/* Format: <modname>__<counter>_<line>_<fn> */
188+#define __initcall_id(fn) \
189+ __PASTE(__KBUILD_MODNAME, \
190+ __PASTE(__, \
191+ __PASTE(__COUNTER__, \
192+ __PASTE(_, \
193+ __PASTE(__LINE__, \
194+ __PASTE(_, fn))))))
195+
196+/* Format: __<prefix>__<iid><id> */
197+#define __initcall_name(prefix, __iid, id) \
198+ __PASTE(__, \
199+ __PASTE(prefix, \
200+ __PASTE(__, \
201+ __PASTE(__iid, id))))
202+
203+#ifdef CONFIG_LTO_CLANG
204+/*
205+ * With LTO, the compiler doesn't necessarily obey link order for
206+ * initcalls. In order to preserve the correct order, we add each
207+ * variable into its own section and generate a linker script (in
208+ * scripts/link-vmlinux.sh) to specify the order of the sections.
209+ */
210+#define __initcall_section(__sec, __iid) \
211+ #__sec ".init.." #__iid
212+
213+/*
214+ * With LTO, the compiler can rename static functions to avoid
215+ * global naming collisions. We use a global stub function for
216+ * initcalls to create a stable symbol name whose address can be
217+ * taken in inline assembly when PREL32 relocations are used.
218+ */
219+#define __initcall_stub(fn, __iid, id) \
220+ __initcall_name(initstub, __iid, id)
221+
222+#define __define_initcall_stub(__stub, fn) \
223+ int __init __stub(void); \
224+ int __init __stub(void) \
225+ { \
226+ return fn(); \
227+ } \
228+ __ADDRESSABLE(__stub)
229+#else
230+#define __initcall_section(__sec, __iid) \
231+ #__sec ".init"
232+
233+#define __initcall_stub(fn, __iid, id) fn
234+
235+#define __define_initcall_stub(__stub, fn) \
236+ __ADDRESSABLE(fn)
237+#endif
238+
187239 #ifdef CONFIG_HAVE_ARCH_PREL32_RELOCATIONS
188−#define ___define_initcall(fn, id, __sec) \
189− __ADDRESSABLE(fn) \
190− asm(".section \"" #__sec ".init\", \"a\" \n" \
191− "__initcall_" #fn #id ": \n" \
192− ".long " #fn " - . \n" \
240+#define ____define_initcall(fn, __stub, __name, __sec) \
241+ __define_initcall_stub(__stub, fn) \
242+ asm(".section \"" __sec "\", \"a\" \n" \
243+ __stringify(__name) ": \n" \
244+ ".long " __stringify(__stub) " - . \n" \
193245 ".previous \n");
194246 #else
195−#define ___define_initcall(fn, id, __sec) \
196− static initcall_t __initcall_##fn##id __used \
197− __attribute__((__section__(#__sec ".init"))) = fn;
247+#define ____define_initcall(fn, __unused, __name, __sec) \
248+ static initcall_t __name __used \
249+ __attribute__((__section__(__sec))) = fn;
198250 #endif
199251
252+#define __unique_initcall(fn, id, __sec, __iid) \
253+ ____define_initcall(fn, \
254+ __initcall_stub(fn, __iid, id), \
255+ __initcall_name(initcall, __iid, id), \
256+ __initcall_section(__sec, __iid))
257+
258+#define ___define_initcall(fn, id, __sec) \
259+ __unique_initcall(fn, id, __sec, __initcall_id(fn))
260+
200261 #define __define_initcall(fn, id) ___define_initcall(fn, id, .initcall##id)
201262
202263 /*
@@ -236,7 +297,7 @@ extern bool initcall_debug; · 33 unmodified lines
236297 #define __exitcall(fn) \
237298 static exitcall_t __exitcall_##fn __exit_call = fn
238299
239−#define console_initcall(fn) ___define_initcall(fn,, .con_initcall)
300+#define console_initcall(fn) ___define_initcall(fn, con, .con_initcall)
240301
241302 struct obs_kernel_param {
242303 const char *str;
include/linux/pci.h+25−2
@@ -1926,7 +1926,7 @@ enum pci_fixup_pass { · 1925 unmodified lines
19261926 };
19271927
19281928 #ifdef CONFIG_HAVE_ARCH_PREL32_RELOCATIONS
1929−#define __DECLARE_PCI_FIXUP_SECTION(sec, name, vendor, device, class, \
1929+#define ___DECLARE_PCI_FIXUP_SECTION(sec, name, vendor, device, class, \
19301930 class_shift, hook) \
19311931 __ADDRESSABLE(hook) \
19321932 asm(".section " #sec ", \"a\" \n" \
@@ -1935,10 +1935,33 @@ enum pci_fixup_pass { · 2 unmodified lines
19351935 ".long " #class ", " #class_shift " \n" \
19361936 ".long " #hook " - . \n" \
19371937 ".previous \n");
1938+
1939+/*
1940+ * Clang's LTO may rename static functions in C, but has no way to
1941+ * handle such renamings when referenced from inline asm. To work
1942+ * around this, create global C stubs for these cases.
1943+ */
1944+#ifdef CONFIG_LTO_CLANG
1945+#define __DECLARE_PCI_FIXUP_SECTION(sec, name, vendor, device, class, \
1946+ class_shift, hook, stub) \
1947+ void stub(struct pci_dev *dev); \
1948+ void stub(struct pci_dev *dev) \
1949+ { \
1950+ hook(dev); \
1951+ } \
1952+ ___DECLARE_PCI_FIXUP_SECTION(sec, name, vendor, device, class, \
1953+ class_shift, stub)
1954+#else
1955+#define __DECLARE_PCI_FIXUP_SECTION(sec, name, vendor, device, class, \
1956+ class_shift, hook, stub) \
1957+ ___DECLARE_PCI_FIXUP_SECTION(sec, name, vendor, device, class, \
1958+ class_shift, hook)
1959+#endif
1960+
19381961 #define DECLARE_PCI_FIXUP_SECTION(sec, name, vendor, device, class, \
19391962 class_shift, hook) \
19401963 __DECLARE_PCI_FIXUP_SECTION(sec, name, vendor, device, class, \
1941− class_shift, hook)
1964+ class_shift, hook, __UNIQUE_ID(hook))
19421965 #else
19431966 /* Anonymous variables would be nice... */
19441967 #define DECLARE_PCI_FIXUP_SECTION(section, name, vendor, device, class, \
init/Kconfig+1−0
@@ -2307,6 +2307,7 @@ config TRIM_UNUSED_KSYMS · 2306 unmodified lines
23072307 config UNUSED_KSYMS_WHITELIST
23082308 string "Whitelist of symbols to keep in ksymtab"
23092309 depends on TRIM_UNUSED_KSYMS
2310+ default "scripts/lto-used-symbollist.txt" if LTO_CLANG
23102311 help
23112312 By default, all unused exported symbols will be un-exported from the
23122313 build when TRIM_UNUSED_KSYMS is selected.
kernel/trace/Kconfig+16−0
@@ -602,6 +602,22 @@ config FTRACE_MCOUNT_RECORD · 601 unmodified lines
602602 depends on DYNAMIC_FTRACE
603603 depends on HAVE_FTRACE_MCOUNT_RECORD
604604
605+config FTRACE_MCOUNT_USE_PATCHABLE_FUNCTION_ENTRY
606+ bool
607+ depends on FTRACE_MCOUNT_RECORD
608+
609+config FTRACE_MCOUNT_USE_CC
610+ def_bool y
611+ depends on $(cc-option,-mrecord-mcount)
612+ depends on !FTRACE_MCOUNT_USE_PATCHABLE_FUNCTION_ENTRY
613+ depends on FTRACE_MCOUNT_RECORD
614+
615+config FTRACE_MCOUNT_USE_RECORDMCOUNT
616+ def_bool y
617+ depends on !FTRACE_MCOUNT_USE_PATCHABLE_FUNCTION_ENTRY
618+ depends on !FTRACE_MCOUNT_USE_CC
619+ depends on FTRACE_MCOUNT_RECORD
620+
605621 config TRACING_MAP
606622 bool
607623 depends on ARCH_HAVE_NMI_SAFE_CMPXCHG
scripts/Makefile.build+41−7
@@ -111,7 +111,7 @@ endif · 110 unmodified lines
111111 # ---------------------------------------------------------------------------
112112
113113 quiet_cmd_cc_s_c = CC $(quiet_modtag) $@
114− cmd_cc_s_c = $(CC) $(filter-out $(DEBUG_CFLAGS), $(c_flags)) -fverbose-asm -S -o $@ $<
114+ cmd_cc_s_c = $(CC) $(filter-out $(DEBUG_CFLAGS) $(CC_FLAGS_LTO), $(c_flags)) -fverbose-asm -S -o $@ $<
115115
116116 $(obj)/%.s: $(src)/%.c FORCE
117117 $(call if_changed_dep,cc_s_c)
@@ -166,6 +166,15 @@ ifdef CONFIG_MODVERSIONS · 48 unmodified lines
166166 # the actual value of the checksum generated by genksyms
167167 # o remove .tmp_<file>.o to <file>.o
168168
169+ifdef CONFIG_LTO_CLANG
170+# Generate .o.symversions files for each .o with exported symbols, and link these
171+# to the kernel and/or modules at the end.
172+cmd_modversions_c = \
173+ if $(NM) $@ 2>/dev/null | grep -q __ksymtab; then \
174+ $(call cmd_gensymtypes_c,$(KBUILD_SYMTYPES),$(@:.o=.symtypes)) \
175+ > $@.symversions; \
176+ fi;
177+else
169178 cmd_modversions_c = \
170179 if $(OBJDUMP) -h $@ | grep -q __ksymtab; then \
171180 $(call cmd_gensymtypes_c,$(KBUILD_SYMTYPES),$(@:.o=.symtypes)) \
@@ -177,9 +186,9 @@ cmd_modversions_c = \ · 5 unmodified lines
177186 rm -f $(@D)/.tmp_$(@F:.o=.ver); \
178187 fi
179188 endif
189+endif
180190
181−ifdef CONFIG_FTRACE_MCOUNT_RECORD
182−ifndef CC_USING_RECORD_MCOUNT
191+ifdef CONFIG_FTRACE_MCOUNT_USE_RECORDMCOUNT
183192 # compiler will not generate __mcount_loc use recordmcount or recordmcount.pl
184193 ifdef BUILD_C_RECORDMCOUNT
185194 ifeq ("$(origin RECORDMCOUNT_WARN)", "command line")
@@ -206,8 +215,7 @@ recordmcount_source := $(srctree)/scripts/recordmcount.pl · 20 unmodified lines
206215 endif # BUILD_C_RECORDMCOUNT
207216 cmd_record_mcount = $(if $(findstring $(strip $(CC_FLAGS_FTRACE)),$(_c_flags)), \
208217 $(sub_cmd_record_mcount))
209−endif # CC_USING_RECORD_MCOUNT
210−endif # CONFIG_FTRACE_MCOUNT_RECORD
218+endif # CONFIG_FTRACE_MCOUNT_USE_RECORDMCOUNT
211219
212220 ifdef CONFIG_STACK_VALIDATION
213221 ifneq ($(SKIP_STACK_VALIDATION),1)
@@ -388,6 +396,18 @@ $(obj)/%.asn1.c $(obj)/%.asn1.h: $(src)/%.asn1 $(objtree)/scripts/asn1_compiler · 174 unmodified lines
388396 $(subdir-builtin): $(obj)/%/built-in.a: $(obj)/% ;
389397 $(subdir-modorder): $(obj)/%/modules.order: $(obj)/% ;
390398
399+# combine symversions for later processing
400+quiet_cmd_update_lto_symversions = SYMVER $@
401+ifeq ($(CONFIG_LTO_CLANG) $(CONFIG_MODVERSIONS),y y)
402+ cmd_update_lto_symversions = \
403+ rm -f $@.symversions \
404+ $(foreach n, $(filter-out FORCE,$^), \
405+ $(if $(wildcard $(n).symversions), \
406+ ; cat $(n).symversions >> $@.symversions))
407+else
408+ cmd_update_lto_symversions = echo >/dev/null
409+endif
410+
391411 #
392412 # Rule to compile a set of .o files into one .a file (without symbol table)
393413 #
@@ -395,8 +415,11 @@ $(subdir-modorder): $(obj)/%/modules.order: $(obj)/% ; · 1 unmodified line
395415 quiet_cmd_ar_builtin = AR $@
396416 cmd_ar_builtin = rm -f $@; $(AR) cDPrST $@ $(real-prereqs)
397417
418+quiet_cmd_ar_and_symver = AR $@
419+ cmd_ar_and_symver = $(cmd_update_lto_symversions); $(cmd_ar_builtin)
420+
398421 $(obj)/built-in.a: $(real-obj-y) FORCE
399− $(call if_changed,ar_builtin)
422+ $(call if_changed,ar_and_symver)
400423
401424 #
402425 # Rule to create modules.order file
@@ -416,15 +439,26 @@ $(obj)/modules.order: $(obj-m) FORCE · 13 unmodified lines
416439 #
417440 # Rule to compile a set of .o files into one .a file (with symbol table)
418441 #
442+quiet_cmd_ar_lib = AR $@
443+ cmd_ar_lib = $(cmd_update_lto_symversions); $(cmd_ar)
444+
419445 $(obj)/lib.a: $(lib-y) FORCE
420− $(call if_changed,ar)
446+ $(call if_changed,ar_lib)
421447
422448 # NOTE:
423449 # Do not replace $(filter %.o,^) with $(real-prereqs). When a single object
424450 # module is turned into a multi object module, $^ will contain header file
425451 # dependencies recorded in the .*.cmd file.
452+ifdef CONFIG_LTO_CLANG
453+quiet_cmd_link_multi-m = AR [M] $@
454+cmd_link_multi-m = \
455+ $(cmd_update_lto_symversions); \
456+ rm -f $@; \
457+ $(AR) cDPrsT $@ $(filter %.o,$^)
458+else
426459 quiet_cmd_link_multi-m = LD [M] $@
427460 cmd_link_multi-m = $(LD) $(ld_flags) -r -o $@ $(filter %.o,$^)
461+endif
428462
429463 $(multi-used-m): FORCE
430464 $(call if_changed,link_multi-m)
scripts/Makefile.lib+4−2
@@ -119,9 +119,11 @@ target-stem = $(basename $(patsubst $(obj)/%,%,$@)) · 118 unmodified lines
119119 # These flags are needed for modversions and compiling, so we define them here
120120 # $(modname_flags) defines KBUILD_MODNAME as the name of the module it will
121121 # end up in (or would, if it gets compiled in)
122−name-fix = $(call stringify,$(subst $(comma),_,$(subst -,_,$1)))
122+name-fix-token = $(subst $(comma),_,$(subst -,_,$1))
123+name-fix = $(call stringify,$(call name-fix-token,$1))
123124 basename_flags = -DKBUILD_BASENAME=$(call name-fix,$(basetarget))
124−modname_flags = -DKBUILD_MODNAME=$(call name-fix,$(modname))
125+modname_flags = -DKBUILD_MODNAME=$(call name-fix,$(modname)) \
126+ -D__KBUILD_MODNAME=kmod_$(call name-fix-token,$(modname))
125127 modfile_flags = -DKBUILD_MODFILE=$(call stringify,$(modfile))
126128
127129 _c_flags = $(filter-out $(CFLAGS_REMOVE_$(target-stem).o), \
scripts/Makefile.modfinal+8−1
@@ -30,6 +30,12 @@ quiet_cmd_cc_o_c = CC [M] $@ · 29 unmodified lines
3030
3131 ARCH_POSTLINK := $(wildcard $(srctree)/arch/$(SRCARCH)/Makefile.postlink)
3232
33+ifdef CONFIG_LTO_CLANG
34+# With CONFIG_LTO_CLANG, reuse the object file we compiled for modpost to
35+# avoid a second slow LTO link
36+prelink-ext := .lto
37+endif
38+
3339 quiet_cmd_ld_ko_o = LD [M] $@
3440 cmd_ld_ko_o = \
3541 $(LD) -r $(KBUILD_LDFLAGS) \
@@ -53,8 +59,9 @@ if_changed_except = $(if $(call newer_prereqs_except,$(2))$(cmd-check), \ · 17 unmodified lines
5359 $(cmd); \
5460 printf '%s\n' 'cmd_$@ := $(make-cmd)' > $(dot-target).cmd, @:)
5561
62+
5663 # Re-generate module BTFs if either module's .ko or vmlinux changed
57−$(modules): %.ko: %.o %.mod.o scripts/module.lds $(if $(KBUILD_BUILTIN),vmlinux) FORCE
64+$(modules): %.ko: %$(prelink-ext).o %.mod.o scripts/module.lds $(if $(KBUILD_BUILTIN),vmlinux) FORCE
5865 +$(call if_changed_except,ld_ko_o,vmlinux)
5966 ifdef CONFIG_DEBUG_INFO_BTF_MODULES
6067 +$(if $(newer-prereqs),$(call cmd,btf_ko))
scripts/Makefile.modpost+23−2
@@ -43,6 +43,9 @@ __modpost: · 42 unmodified lines
4343 include include/config/auto.conf
4444 include scripts/Kbuild.include
4545
46+# for ld_flags
47+include scripts/Makefile.lib
48+
4649 MODPOST = scripts/mod/modpost \
4750 $(if $(CONFIG_MODVERSIONS),-m) \
4851 $(if $(CONFIG_MODULE_SRCVERSION_ALL),-a) \
@@ -102,12 +105,30 @@ $(input-symdump): · 53 unmodified lines
102105 @echo >&2 'WARNING: Symbol version dump "$@" is missing.'
103106 @echo >&2 ' Modules may not have dependencies or modversions.'
104107
108+ifdef CONFIG_LTO_CLANG
109+# With CONFIG_LTO_CLANG, .o files might be LLVM bitcode, so we need to run
110+# LTO to compile them into native code before running modpost
111+prelink-ext := .lto
112+
113+quiet_cmd_cc_lto_link_modules = LTO [M] $@
114+cmd_cc_lto_link_modules = \
115+ $(LD) $(ld_flags) -r -o $@ \
116+ $(shell [ -s $(@:.lto.o=.o.symversions) ] && \
117+ echo -T $(@:.lto.o=.o.symversions)) \
118+ --whole-archive $^
119+
120+%.lto.o: %.o
121+ $(call if_changed,cc_lto_link_modules)
122+endif
123+
124+modules := $(sort $(shell cat $(MODORDER)))
125+
105126 # Read out modules.order to pass in modpost.
106127 # Otherwise, allmodconfig would fail with "Argument list too long".
107128 quiet_cmd_modpost = MODPOST $@
108− cmd_modpost = sed 's/ko$$/o/' $< | $(MODPOST) -T -
129+ cmd_modpost = sed 's/\.ko$$/$(prelink-ext)\.o/' $< | $(MODPOST) -T -
109130
110−$(output-symdump): $(MODORDER) $(input-symdump) FORCE
131+$(output-symdump): $(MODORDER) $(input-symdump) $(modules:.ko=$(prelink-ext).o) FORCE
111132 $(call if_changed,modpost)
112133
113134 targets += $(output-symdump)
scripts/generate_initcall_order.pl+270−0
@@ -0,0 +1,270 @@
1+#!/usr/bin/env perl
2+# SPDX-License-Identifier: GPL-2.0
3+#
4+# Generates a linker script that specifies the correct initcall order.
5+#
6+# Copyright (C) 2019 Google LLC
7+
8+use strict;
9+use warnings;
10+use IO::Handle;
11+use IO::Select;
12+use POSIX ":sys_wait_h";
13+
14+my $nm = $ENV{'NM'} || die "$0: ERROR: NM not set?";
15+my $objtree = $ENV{'objtree'} || '.';
16+
17+## currently active child processes
18+my $jobs = {}; # child process pid -> file handle
19+## results from child processes
20+my $results = {}; # object index -> [ { level, secname }, ... ]
21+
22+## reads _NPROCESSORS_ONLN to determine the maximum number of processes to
23+## start
24+sub get_online_processors {
25+ open(my $fh, "getconf _NPROCESSORS_ONLN 2>/dev/null |")
26+ or die "$0: ERROR: failed to execute getconf: $!";
27+ my $procs = <$fh>;
28+ close($fh);
29+
30+ if (!($procs =~ /^\d+$/)) {
31+ return 1;
32+ }
33+
34+ return int($procs);
35+}
36+
37+## writes results to the parent process
38+## format: <file index> <initcall level> <base initcall section name>
39+sub write_results {
40+ my ($index, $initcalls) = @_;
41+
42+ # sort by the counter value to ensure the order of initcalls within
43+ # each object file is correct
44+ foreach my $counter (sort { $a <=> $b } keys(%{$initcalls})) {
45+ my $level = $initcalls->{$counter}->{'level'};
46+
47+ # section name for the initcall function
48+ my $secname = $initcalls->{$counter}->{'module'} . '__' .
49+ $counter . '_' .
50+ $initcalls->{$counter}->{'line'} . '_' .
51+ $initcalls->{$counter}->{'function'};
52+
53+ print "$index $level $secname\n";
54+ }
55+}
56+
57+## reads a result line from a child process and adds it to the $results array
58+sub read_results{
59+ my ($fh) = @_;
60+
61+ # each child prints out a full line w/ autoflush and exits after the
62+ # last line, so even if buffered I/O blocks here, it shouldn't block
63+ # very long
64+ my $data = <$fh>;
65+
66+ if (!defined($data)) {
67+ return 0;
68+ }
69+
70+ chomp($data);
71+
72+ my ($index, $level, $secname) = $data =~
73+ /^(\d+)\ ([^\ ]+)\ (.*)$/;
74+
75+ if (!defined($index) ||
76+ !defined($level) ||
77+ !defined($secname)) {
78+ die "$0: ERROR: child process returned invalid data: $data\n";
79+ }
80+
81+ $index = int($index);
82+
83+ if (!exists($results->{$index})) {
84+ $results->{$index} = [];
85+ }
86+
87+ push (@{$results->{$index}}, {
88+ 'level' => $level,
89+ 'secname' => $secname
90+ });
91+
92+ return 1;
93+}
94+
95+## finds initcalls from an object file or all object files in an archive, and
96+## writes results back to the parent process
97+sub find_initcalls {
98+ my ($index, $file) = @_;
99+
100+ die "$0: ERROR: file $file doesn't exist?" if (! -f $file);
101+
102+ open(my $fh, "\"$nm\" --defined-only \"$file\" 2>/dev/null |")
103+ or die "$0: ERROR: failed to execute \"$nm\": $!";
104+
105+ my $initcalls = {};
106+
107+ while (<$fh>) {
108+ chomp;
109+
110+ # check for the start of a new object file (if processing an
111+ # archive)
112+ my ($path)= $_ =~ /^(.+)\:$/;
113+
114+ if (defined($path)) {
115+ write_results($index, $initcalls);
116+ $initcalls = {};
117+ next;
118+ }
119+
120+ # look for an initcall
121+ my ($module, $counter, $line, $symbol) = $_ =~
122+ /[a-z]\s+__initcall__(\S*)__(\d+)_(\d+)_(.*)$/;
123+
124+ if (!defined($module)) {
125+ $module = ''
126+ }
127+
128+ if (!defined($counter) ||
129+ !defined($line) ||
130+ !defined($symbol)) {
131+ next;
132+ }
133+
134+ # parse initcall level
135+ my ($function, $level) = $symbol =~
136+ /^(.*)((early|rootfs|con|[0-9])s?)$/;
137+
138+ die "$0: ERROR: invalid initcall name $symbol in $file($path)"
139+ if (!defined($function) || !defined($level));
140+
141+ $initcalls->{$counter} = {
142+ 'module' => $module,
143+ 'line' => $line,
144+ 'function' => $function,
145+ 'level' => $level,
146+ };
147+ }
148+
149+ close($fh);
150+ write_results($index, $initcalls);
151+}
152+
153+## waits for any child process to complete, reads the results, and adds them to
154+## the $results array for later processing
155+sub wait_for_results {
156+ my ($select) = @_;
157+
158+ my $pid = 0;
159+ do {
160+ # unblock children that may have a full write buffer
161+ foreach my $fh ($select->can_read(0)) {
162+ read_results($fh);
163+ }
164+
165+ # check for children that have exited, read the remaining data
166+ # from them, and clean up
167+ $pid = waitpid(-1, WNOHANG);
168+ if ($pid > 0) {
169+ if (!exists($jobs->{$pid})) {
170+ next;
171+ }
172+
173+ my $fh = $jobs->{$pid};
174+ $select->remove($fh);
175+
176+ while (read_results($fh)) {
177+ # until eof
178+ }
179+
180+ close($fh);
181+ delete($jobs->{$pid});
182+ }
183+ } while ($pid > 0);
184+}
185+
186+## forks a child to process each file passed in the command line and collects
187+## the results
188+sub process_files {
189+ my $index = 0;
190+ my $njobs = $ENV{'PARALLELISM'} || get_online_processors();
191+ my $select = IO::Select->new();
192+
193+ while (my $file = shift(@ARGV)) {
194+ # fork a child process and read it's stdout
195+ my $pid = open(my $fh, '-|');
196+
197+ if (!defined($pid)) {
198+ die "$0: ERROR: failed to fork: $!";
199+ } elsif ($pid) {
200+ # save the child process pid and the file handle
201+ $select->add($fh);
202+ $jobs->{$pid} = $fh;
203+ } else {
204+ # in the child process
205+ STDOUT->autoflush(1);
206+ find_initcalls($index, "$objtree/$file");
207+ exit;
208+ }
209+
210+ $index++;
211+
212+ # limit the number of children to $njobs
213+ if (scalar(keys(%{$jobs})) >= $njobs) {
214+ wait_for_results($select);
215+ }
216+ }
217+
218+ # wait for the remaining children to complete
219+ while (scalar(keys(%{$jobs})) > 0) {
220+ wait_for_results($select);
221+ }
222+}
223+
224+sub generate_initcall_lds() {
225+ process_files();
226+
227+ my $sections = {}; # level -> [ secname, ...]
228+
229+ # sort results to retain link order and split to sections per
230+ # initcall level
231+ foreach my $index (sort { $a <=> $b } keys(%{$results})) {
232+ foreach my $result (@{$results->{$index}}) {
233+ my $level = $result->{'level'};
234+
235+ if (!exists($sections->{$level})) {
236+ $sections->{$level} = [];
237+ }
238+
239+ push(@{$sections->{$level}}, $result->{'secname'});
240+ }
241+ }
242+
243+ die "$0: ERROR: no initcalls?" if (!keys(%{$sections}));
244+
245+ # print out a linker script that defines the order of initcalls for
246+ # each level
247+ print "SECTIONS {\n";
248+
249+ foreach my $level (sort(keys(%{$sections}))) {
250+ my $section;
251+
252+ if ($level eq 'con') {
253+ $section = '.con_initcall.init';
254+ } else {
255+ $section = ".initcall${level}.init";
256+ }
257+
258+ print "\t${section} : {\n";
259+
260+ foreach my $secname (@{$sections->{$level}}) {
261+ print "\t\t*(${section}..${secname}) ;\n";
262+ }
263+
264+ print "\t}\n";
265+ }
266+
267+ print "}\n";
268+}
269+
270+generate_initcall_lds();
scripts/link-vmlinux.sh+61−9
@@ -43,11 +43,37 @@ info() · 42 unmodified lines
4343 fi
4444 }
4545
46+# Generate a linker script to ensure correct ordering of initcalls.
47+gen_initcalls()
48+{
49+ info GEN .tmp_initcalls.lds
50+
51+ ${PYTHON} ${srctree}/scripts/jobserver-exec \
52+ ${PERL} ${srctree}/scripts/generate_initcall_order.pl \
53+ ${KBUILD_VMLINUX_OBJS} ${KBUILD_VMLINUX_LIBS} \
54+ > .tmp_initcalls.lds
55+}
56+
57+# If CONFIG_LTO_CLANG is selected, collect generated symbol versions into
58+# .tmp_symversions.lds
59+gen_symversions()
60+{
61+ info GEN .tmp_symversions.lds
62+ rm -f .tmp_symversions.lds
63+
64+ for o in ${KBUILD_VMLINUX_OBJS} ${KBUILD_VMLINUX_LIBS}; do
65+ if [ -f ${o}.symversions ]; then
66+ cat ${o}.symversions >> .tmp_symversions.lds
67+ fi
68+ done
69+}
70+
4671 # Link of vmlinux.o used for section mismatch analysis
4772 # ${1} output file
4873 modpost_link()
4974 {
5075 local objects
76+ local lds=""
5177
5278 objects="--whole-archive \
5379 ${KBUILD_VMLINUX_OBJS} \
@@ -56,7 +82,23 @@ modpost_link() · 2 unmodified lines
5682 ${KBUILD_VMLINUX_LIBS} \
5783 --end-group"
5884
59− ${LD} ${KBUILD_LDFLAGS} -r -o ${1} ${objects}
85+ if [ -n "${CONFIG_LTO_CLANG}" ]; then
86+ gen_initcalls
87+ lds="-T .tmp_initcalls.lds"
88+
89+ if [ -n "${CONFIG_MODVERSIONS}" ]; then
90+ gen_symversions
91+ lds="${lds} -T .tmp_symversions.lds"
92+ fi
93+
94+ # This might take a while, so indicate that we're doing
95+ # an LTO link
96+ info LTO ${1}
97+ else
98+ info LD ${1}
99+ fi
100+
101+ ${LD} ${KBUILD_LDFLAGS} -r -o ${1} ${lds} ${objects}
60102 }
61103
62104 objtool_link()
@@ -103,13 +145,22 @@ vmlinux_link() · 40 unmodified lines
103145 fi
104146
105147 if [ "${SRCARCH}" != "um" ]; then
106− objects="--whole-archive \
107− ${KBUILD_VMLINUX_OBJS} \
108− --no-whole-archive \
109− --start-group \
110− ${KBUILD_VMLINUX_LIBS} \
111− --end-group \
112− ${@}"
148+ if [ -n "${CONFIG_LTO_CLANG}" ]; then
149+ # Use vmlinux.o instead of performing the slow LTO
150+ # link again.
151+ objects="--whole-archive \
152+ vmlinux.o \
153+ --no-whole-archive \
154+ ${@}"
155+ else
156+ objects="--whole-archive \
157+ ${KBUILD_VMLINUX_OBJS} \
158+ --no-whole-archive \
159+ --start-group \
160+ ${KBUILD_VMLINUX_LIBS} \
161+ --end-group \
162+ ${@}"
163+ fi
113164
114165 ${LD} ${KBUILD_LDFLAGS} ${LDFLAGS_vmlinux} \
115166 ${strip_debug#-Wl,} \
@@ -225,6 +276,8 @@ cleanup() · 109 unmodified lines
225276 {
226277 rm -f .btf.*
227278 rm -f .tmp_System.map
279+ rm -f .tmp_initcalls.lds
280+ rm -f .tmp_symversions.lds
228281 rm -f .tmp_vmlinux*
229282 rm -f System.map
230283 rm -f vmlinux
@@ -274,7 +327,6 @@ fi; · 43 unmodified lines
274327 ${MAKE} -f "${srctree}/scripts/Makefile.build" obj=init need-builtin=1
275328
276329 #link vmlinux.o
277−info LD vmlinux.o
278330 modpost_link vmlinux.o
279331 objtool_link vmlinux.o
280332
scripts/lto-used-symbollist.txt+5−0
@@ -0,0 +1,5 @@
1+memcpy
2+memmove
3+memset
4+__stack_chk_fail
5+__stack_chk_guard
scripts/mod/Makefile+1−0
@@ -1,5 +1,6 @@
11 # SPDX-License-Identifier: GPL-2.0
22 OBJECT_FILES_NON_STANDARD := y
3+CFLAGS_REMOVE_empty.o += $(CC_FLAGS_LTO)
34
45 hostprogs-always-y += modpost mk_elfconfig
56 always-y += empty.o
scripts/mod/modpost.c+7−9
@@ -17,7 +17,6 @@ · 16 unmodified lines
1717 #include <ctype.h>
1818 #include <string.h>
1919 #include <limits.h>
20−#include <stdbool.h>
2120 #include <errno.h>
2221 #include "modpost.h"
2322 #include "../../include/linux/license.h"
@@ -84,14 +83,6 @@ modpost_log(enum loglevel loglevel, const char *fmt, ...) · 60 unmodified lines
8483 error_occurred = true;
8584 }
8685
87−static inline bool strends(const char *str, const char *postfix)
88−{
89− if (strlen(str) < strlen(postfix))
90− return false;
91−
92− return strcmp(str + strlen(str) - strlen(postfix), postfix) == 0;
93−}
94−
9586 void *do_nofail(void *ptr, const char *expr)
9687 {
9788 if (!ptr)
@@ -1988,6 +1979,10 @@ static char *remove_dot(char *s) · 1890 unmodified lines
19881979 size_t m = strspn(s + n + 1, "0123456789");
19891980 if (m && (s[n + m] == '.' || s[n + m] == 0))
19901981 s[n] = 0;
1982+
1983+ /* strip trailing .lto */
1984+ if (strends(s, ".lto"))
1985+ s[strlen(s) - 4] = '\0';
19911986 }
19921987 return s;
19931988 }
@@ -2011,6 +2006,9 @@ static void read_symbols(const char *modname) · 17 unmodified lines
20112006 /* strip trailing .o */
20122007 tmp = NOFAIL(strdup(modname));
20132008 tmp[strlen(tmp) - 2] = '\0';
2009+ /* strip trailing .lto */
2010+ if (strends(tmp, ".lto"))
2011+ tmp[strlen(tmp) - 4] = '\0';
20142012 mod = new_module(tmp);
20152013 free(tmp);
20162014 }
scripts/mod/modpost.h+9−0
@@ -2,6 +2,7 @@ · 1 unmodified line
22 #include <stdio.h>
33 #include <stdlib.h>
44 #include <stdarg.h>
5+#include <stdbool.h>
56 #include <string.h>
67 #include <sys/types.h>
78 #include <sys/stat.h>
@@ -180,6 +181,14 @@ static inline unsigned int get_secindex(const struct elf_info *info, · 172 unmodified lines
180181 return info->symtab_shndx_start[sym - info->symtab_start];
181182 }
182183
184+static inline bool strends(const char *str, const char *postfix)
185+{
186+ if (strlen(str) < strlen(postfix))
187+ return false;
188+
189+ return strcmp(str + strlen(str) - strlen(postfix), postfix) == 0;
190+}
191+
183192 /* file2alias.c */
184193 extern unsigned int cross_build;
185194 void handle_moddevtable(struct module *mod, struct elf_info *info,
scripts/mod/sumversion.c+5−1
@@ -391,10 +391,14 @@ void get_src_version(const char *modname, char sum[], unsigned sumlen) · 390 unmodified lines
391391 struct md4_ctx md;
392392 char *fname;
393393 char filelist[PATH_MAX + 1];
394+ int postfix_len = 1;
395+
396+ if (strends(modname, ".lto.o"))
397+ postfix_len = 5;
394398
395399 /* objects for a module are listed in the first line of *.mod file. */
396400 snprintf(filelist, sizeof(filelist), "%.*smod",
397− (int)strlen(modname) - 1, modname);
401+ (int)strlen(modname) - postfix_len, modname);
398402
399403 buf = read_text_file(filelist);
400404
scripts/module.lds.S+24−0
@@ -23,6 +23,30 @@ SECTIONS { · 22 unmodified lines
2323 .init_array 0 : ALIGN(8) { *(SORT(.init_array.*)) *(.init_array) }
2424
2525 __jump_table 0 : ALIGN(8) { KEEP(*(__jump_table)) }
26+
27+ __patchable_function_entries : { *(__patchable_function_entries) }
28+
29+ /*
30+ * With CONFIG_LTO_CLANG, LLD always enables -fdata-sections and
31+ * -ffunction-sections, which increases the size of the final module.
32+ * Merge the split sections in the final binary.
33+ */
34+ .bss : {
35+ *(.bss .bss.[0-9a-zA-Z_]*)
36+ *(.bss..L*)
37+ }
38+
39+ .data : {
40+ *(.data .data.[0-9a-zA-Z_]*)
41+ *(.data..L*)
42+ }
43+
44+ .rodata : {
45+ *(.rodata .rodata.[0-9a-zA-Z_]*)
46+ *(.rodata..L*)
47+ }
48+
49+ .text : { *(.text .text.[0-9a-zA-Z_]*) }
2650 }
2751
2852 /* bring in arch-specific sections */