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
42*.so.dbg42*.so.dbg
43*.su43*.su
44*.symtypes44*.symtypes
45*.symversions
45*.tab.[ch]46*.tab.[ch]
46*.tar47*.tar
47*.xz48*.xz
Makefile+30−15
@@ -853,12 +853,8 @@ KBUILD_CFLAGS += $(DEBUG_CFLAGS) · 852 unmodified lines
853export DEBUG_CFLAGS853export DEBUG_CFLAGS
854 854
855ifdef CONFIG_FUNCTION_TRACER855ifdef CONFIG_FUNCTION_TRACER
856ifdef CONFIG_FTRACE_MCOUNT_RECORD856ifdef CONFIG_FTRACE_MCOUNT_USE_CC
857 # gcc 5 supports generating the mcount tables directly857 CC_FLAGS_FTRACE += -mrecord-mcount
858 ifeq ($(call cc-option-yn,-mrecord-mcount),y)
859 CC_FLAGS_FTRACE += -mrecord-mcount
860 export CC_USING_RECORD_MCOUNT := 1
861 endif
862 ifdef CONFIG_HAVE_NOP_MCOUNT858 ifdef CONFIG_HAVE_NOP_MCOUNT
863 ifeq ($(call cc-option-yn, -mnop-mcount),y)859 ifeq ($(call cc-option-yn, -mnop-mcount),y)
864 CC_FLAGS_FTRACE += -mnop-mcount860 CC_FLAGS_FTRACE += -mnop-mcount
@@ -866,6 +862,12 @@ ifdef CONFIG_FTRACE_MCOUNT_RECORD · 1 unmodified line
866 endif862 endif
867 endif863 endif
868endif864endif
865ifdef CONFIG_FTRACE_MCOUNT_USE_RECORDMCOUNT
866 ifdef CONFIG_HAVE_C_RECORDMCOUNT
867 BUILD_C_RECORDMCOUNT := y
868 export BUILD_C_RECORDMCOUNT
869 endif
870endif
869ifdef CONFIG_HAVE_FENTRY871ifdef CONFIG_HAVE_FENTRY
870 ifeq ($(call cc-option-yn, -mfentry),y)872 ifeq ($(call cc-option-yn, -mfentry),y)
871 CC_FLAGS_FTRACE += -mfentry873 CC_FLAGS_FTRACE += -mfentry
@@ -875,12 +877,6 @@ endif · 3 unmodified lines
875export CC_FLAGS_FTRACE877export CC_FLAGS_FTRACE
876KBUILD_CFLAGS += $(CC_FLAGS_FTRACE) $(CC_FLAGS_USING)878KBUILD_CFLAGS += $(CC_FLAGS_FTRACE) $(CC_FLAGS_USING)
877KBUILD_AFLAGS += $(CC_FLAGS_USING)879KBUILD_AFLAGS += $(CC_FLAGS_USING)
878ifdef CONFIG_DYNAMIC_FTRACE
879 ifdef CONFIG_HAVE_C_RECORDMCOUNT
880 BUILD_C_RECORDMCOUNT := y
881 export BUILD_C_RECORDMCOUNT
882 endif
883endif
884endif880endif
885 881
886# We trigger additional mismatches with less inlining882# We trigger additional mismatches with less inlining
@@ -899,6 +895,24 @@ KBUILD_CFLAGS += $(CC_FLAGS_SCS) · 12 unmodified lines
899export CC_FLAGS_SCS895export CC_FLAGS_SCS
900endif896endif
901 897
898ifdef CONFIG_LTO_CLANG
899ifdef CONFIG_LTO_CLANG_THIN
900CC_FLAGS_LTO := -flto=thin -fsplit-lto-unit
901KBUILD_LDFLAGS += --thinlto-cache-dir=$(extmod-prefix).thinlto-cache
902else
903CC_FLAGS_LTO := -flto
904endif
905CC_FLAGS_LTO += -fvisibility=hidden
906
907# Limit inlining across translation units to reduce binary size
908KBUILD_LDFLAGS += -mllvm -import-instr-limit=5
909endif
910
911ifdef CONFIG_LTO
912KBUILD_CFLAGS += $(CC_FLAGS_LTO)
913export CC_FLAGS_LTO
914endif
915
902ifdef CONFIG_DEBUG_FORCE_FUNCTION_ALIGN_32B916ifdef CONFIG_DEBUG_FORCE_FUNCTION_ALIGN_32B
903KBUILD_CFLAGS += -falign-functions=32917KBUILD_CFLAGS += -falign-functions=32
904endif918endif
@@ -1493,7 +1507,7 @@ MRPROPER_FILES += include/config include/generated \ · 588 unmodified lines
1493 *.spec1507 *.spec
1494 1508
1495# Directories & files removed with 'make distclean'1509# Directories & files removed with 'make distclean'
1496DISTCLEAN_FILES += tags TAGS cscope* GPATH GTAGS GRTAGS GSYMS1510DISTCLEAN_FILES += tags TAGS cscope* GPATH GTAGS GRTAGS GSYMS .thinlto-cache
1497 1511
1498# clean - Delete most, but leave enough to build external modules1512# clean - Delete most, but leave enough to build external modules
1499#1513#
@@ -1739,7 +1753,7 @@ PHONY += compile_commands.json · 239 unmodified lines
1739 1753
1740clean-dirs := $(KBUILD_EXTMOD)1754clean-dirs := $(KBUILD_EXTMOD)
1741clean: rm-files := $(KBUILD_EXTMOD)/Module.symvers $(KBUILD_EXTMOD)/modules.nsdeps \1755clean: rm-files := $(KBUILD_EXTMOD)/Module.symvers $(KBUILD_EXTMOD)/modules.nsdeps \
1742 $(KBUILD_EXTMOD)/compile_commands.json1756 $(KBUILD_EXTMOD)/compile_commands.json $(KBUILD_EXTMOD)/.thinlto-cache
1743 1757
1744PHONY += help1758PHONY += help
1745help:1759help:
@@ -1836,7 +1850,8 @@ clean: $(clean-dirs) · 90 unmodified lines
1836 -o -name '.tmp_*.o.*' \1850 -o -name '.tmp_*.o.*' \
1837 -o -name '*.c.[012]*.*' \1851 -o -name '*.c.[012]*.*' \
1838 -o -name '*.ll' \1852 -o -name '*.ll' \
1839 -o -name '*.gcno' \) -type f -print | xargs rm -f1853 -o -name '*.gcno' \
1854 -o -name '*.*.symversions' \) -type f -print | xargs rm -f
1840 1855
1841# Generate tags for editors1856# Generate tags for editors
1842# ---------------------------------------------------------------------------1857# ---------------------------------------------------------------------------
arch/Kconfig+90−0
@@ -603,6 +603,96 @@ config SHADOW_CALL_STACK · 602 unmodified lines
603 reading and writing arbitrary memory may be able to locate them603 reading and writing arbitrary memory may be able to locate them
604 and hijack control flow by modifying the stacks.604 and hijack control flow by modifying the stacks.
605 605
606config LTO
607 bool
608 help
609 Selected if the kernel will be built using the compiler's LTO feature.
610
611config LTO_CLANG
612 bool
613 select LTO
614 help
615 Selected if the kernel will be built using Clang's LTO feature.
616
617config 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
625config 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
631config 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
647choice
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
657config LTO_NONE
658 bool "None"
659 help
660 Build the kernel normally, without Link Time Optimization (LTO).
661
662config 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
681config 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.
694endchoice
695
606config HAVE_ARCH_WITHIN_STACK_FRAMES696config HAVE_ARCH_WITHIN_STACK_FRAMES
607 bool697 bool
608 help698 help
arch/arm64/Kconfig+4−0
@@ -73,6 +73,8 @@ config ARM64 · 72 unmodified lines
73 select ARCH_SUPPORTS_DEBUG_PAGEALLOC73 select ARCH_SUPPORTS_DEBUG_PAGEALLOC
74 select ARCH_SUPPORTS_MEMORY_FAILURE74 select ARCH_SUPPORTS_MEMORY_FAILURE
75 select ARCH_SUPPORTS_SHADOW_CALL_STACK if CC_HAVE_SHADOW_CALL_STACK75 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
76 select ARCH_SUPPORTS_ATOMIC_RMW78 select ARCH_SUPPORTS_ATOMIC_RMW
77 select ARCH_SUPPORTS_INT128 if CC_HAS_INT128 && (GCC_VERSION >= 50000 || CC_IS_CLANG)79 select ARCH_SUPPORTS_INT128 if CC_HAS_INT128 && (GCC_VERSION >= 50000 || CC_IS_CLANG)
78 select ARCH_SUPPORTS_NUMA_BALANCING80 select ARCH_SUPPORTS_NUMA_BALANCING
@@ -162,6 +164,8 @@ config ARM64 · 83 unmodified lines
162 select HAVE_DYNAMIC_FTRACE164 select HAVE_DYNAMIC_FTRACE
163 select HAVE_DYNAMIC_FTRACE_WITH_REGS \165 select HAVE_DYNAMIC_FTRACE_WITH_REGS \
164 if $(cc-option,-fpatchable-function-entry=2)166 if $(cc-option,-fpatchable-function-entry=2)
167 select FTRACE_MCOUNT_USE_PATCHABLE_FUNCTION_ENTRY \
168 if DYNAMIC_FTRACE_WITH_REGS
165 select HAVE_EFFICIENT_UNALIGNED_ACCESS169 select HAVE_EFFICIENT_UNALIGNED_ACCESS
166 select HAVE_FAST_GUP170 select HAVE_FAST_GUP
167 select HAVE_FTRACE_MCOUNT_RECORD171 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
29ccflags-y := -fno-common -fno-builtin -fno-stack-protector -ffixed-x1829ccflags-y := -fno-common -fno-builtin -fno-stack-protector -ffixed-x18
30ccflags-y += -DDISABLE_BRANCH_PROFILING -DBUILD_VDSO30ccflags-y += -DDISABLE_BRANCH_PROFILING -DBUILD_VDSO
31 31
32CFLAGS_REMOVE_vgettimeofday.o = $(CC_FLAGS_FTRACE) -Os $(CC_FLAGS_SCS) $(GCC_PLUGINS_CFLAGS)32CFLAGS_REMOVE_vgettimeofday.o = $(CC_FLAGS_FTRACE) -Os $(CC_FLAGS_SCS) $(GCC_PLUGINS_CFLAGS) \
33 $(CC_FLAGS_LTO)
33KASAN_SANITIZE := n34KASAN_SANITIZE := n
34UBSAN_SANITIZE := n35UBSAN_SANITIZE := n
35OBJECT_FILES_NON_STANDARD := y36OBJECT_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
38 38
39# remove SCS flags from all objects in this directory39# remove SCS flags from all objects in this directory
40KBUILD_CFLAGS := $(filter-out $(CC_FLAGS_SCS), $(KBUILD_CFLAGS))40KBUILD_CFLAGS := $(filter-out $(CC_FLAGS_SCS), $(KBUILD_CFLAGS))
41# disable LTO
42KBUILD_CFLAGS := $(filter-out $(CC_FLAGS_LTO), $(KBUILD_CFLAGS))
41 43
42GCOV_PROFILE := n44GCOV_PROFILE := n
43# Sanitizer runtimes are unavailable and cannot be linked here.45# 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
16KASAN_SANITIZE_rodata.o := n16KASAN_SANITIZE_rodata.o := n
17KASAN_SANITIZE_stackleak.o := n17KASAN_SANITIZE_stackleak.o := n
18KCOV_INSTRUMENT_rodata.o := n18KCOV_INSTRUMENT_rodata.o := n
19CFLAGS_REMOVE_rodata.o += $(CC_FLAGS_LTO)
19 20
20OBJCOPYFLAGS :=21OBJCOPYFLAGS :=
21OBJCOPYFLAGS_rodata_objcopy.o := \22OBJCOPYFLAGS_rodata_objcopy.o := \
include/asm-generic/vmlinux.lds.h+4−1
@@ -90,10 +90,13 @@ · 89 unmodified lines
90 * .data. We don't want to pull in .data..other sections, which Linux90 * .data. We don't want to pull in .data..other sections, which Linux
91 * has defined. Same for text and bss.91 * has defined. Same for text and bss.
92 *92 *
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 *
93 * RODATA_MAIN is not used because existing code already defines .rodata.x96 * RODATA_MAIN is not used because existing code already defines .rodata.x
94 * sections to be brought in with rodata.97 * sections to be brought in with rodata.
95 */98 */
96#ifdef CONFIG_LD_DEAD_CODE_DATA_ELIMINATION99#if defined(CONFIG_LD_DEAD_CODE_DATA_ELIMINATION) || defined(CONFIG_LTO_CLANG)
97#define TEXT_MAIN .text .text.[0-9a-zA-Z_]*100#define TEXT_MAIN .text .text.[0-9a-zA-Z_]*
98#define DATA_MAIN .data .data.[0-9a-zA-Z_]* .data..L* .data..compoundliteral* .data.$__unnamed_* .data.$Lubsan_*101#define DATA_MAIN .data .data.[0-9a-zA-Z_]* .data..L* .data..compoundliteral* .data.$__unnamed_* .data.$Lubsan_*
99#define SDATA_MAIN .sdata .sdata.[0-9a-zA-Z_]*102#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
184 * as KEEP() in the linker script.184 * as KEEP() in the linker script.
185 */185 */
186 186
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
187#ifdef CONFIG_HAVE_ARCH_PREL32_RELOCATIONS239#ifdef CONFIG_HAVE_ARCH_PREL32_RELOCATIONS
188#define ___define_initcall(fn, id, __sec) \240#define ____define_initcall(fn, __stub, __name, __sec) \
189 __ADDRESSABLE(fn) \241 __define_initcall_stub(__stub, fn) \
190 asm(".section \"" #__sec ".init\", \"a\" \n" \242 asm(".section \"" __sec "\", \"a\" \n" \
191 "__initcall_" #fn #id ": \n" \243 __stringify(__name) ": \n" \
192 ".long " #fn " - . \n" \244 ".long " __stringify(__stub) " - . \n" \
193 ".previous \n");245 ".previous \n");
194#else246#else
195#define ___define_initcall(fn, id, __sec) \247#define ____define_initcall(fn, __unused, __name, __sec) \
196 static initcall_t __initcall_##fn##id __used \248 static initcall_t __name __used \
197 __attribute__((__section__(#__sec ".init"))) = fn;249 __attribute__((__section__(__sec))) = fn;
198#endif250#endif
199 251
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
200#define __define_initcall(fn, id) ___define_initcall(fn, id, .initcall##id)261#define __define_initcall(fn, id) ___define_initcall(fn, id, .initcall##id)
201 262
202/*263/*
@@ -236,7 +297,7 @@ extern bool initcall_debug; · 33 unmodified lines
236#define __exitcall(fn) \297#define __exitcall(fn) \
237 static exitcall_t __exitcall_##fn __exit_call = fn298 static exitcall_t __exitcall_##fn __exit_call = fn
238 299
239#define console_initcall(fn) ___define_initcall(fn,, .con_initcall)300#define console_initcall(fn) ___define_initcall(fn, con, .con_initcall)
240 301
241struct obs_kernel_param {302struct obs_kernel_param {
242 const char *str;303 const char *str;
include/linux/pci.h+25−2
@@ -1926,7 +1926,7 @@ enum pci_fixup_pass { · 1925 unmodified lines
1926};1926};
1927 1927
1928#ifdef CONFIG_HAVE_ARCH_PREL32_RELOCATIONS1928#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, \
1930 class_shift, hook) \1930 class_shift, hook) \
1931 __ADDRESSABLE(hook) \1931 __ADDRESSABLE(hook) \
1932 asm(".section " #sec ", \"a\" \n" \1932 asm(".section " #sec ", \"a\" \n" \
@@ -1935,10 +1935,33 @@ enum pci_fixup_pass { · 2 unmodified lines
1935 ".long " #class ", " #class_shift " \n" \1935 ".long " #class ", " #class_shift " \n" \
1936 ".long " #hook " - . \n" \1936 ".long " #hook " - . \n" \
1937 ".previous \n");1937 ".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
1938#define DECLARE_PCI_FIXUP_SECTION(sec, name, vendor, device, class, \1961#define DECLARE_PCI_FIXUP_SECTION(sec, name, vendor, device, class, \
1939 class_shift, hook) \1962 class_shift, hook) \
1940 __DECLARE_PCI_FIXUP_SECTION(sec, name, vendor, device, class, \1963 __DECLARE_PCI_FIXUP_SECTION(sec, name, vendor, device, class, \
1941 class_shift, hook)1964 class_shift, hook, __UNIQUE_ID(hook))
1942#else1965#else
1943/* Anonymous variables would be nice... */1966/* Anonymous variables would be nice... */
1944#define DECLARE_PCI_FIXUP_SECTION(section, name, vendor, device, class, \1967#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
2307config UNUSED_KSYMS_WHITELIST2307config UNUSED_KSYMS_WHITELIST
2308 string "Whitelist of symbols to keep in ksymtab"2308 string "Whitelist of symbols to keep in ksymtab"
2309 depends on TRIM_UNUSED_KSYMS2309 depends on TRIM_UNUSED_KSYMS
2310 default "scripts/lto-used-symbollist.txt" if LTO_CLANG
2310 help2311 help
2311 By default, all unused exported symbols will be un-exported from the2312 By default, all unused exported symbols will be un-exported from the
2312 build when TRIM_UNUSED_KSYMS is selected.2313 build when TRIM_UNUSED_KSYMS is selected.
kernel/trace/Kconfig+16−0
@@ -602,6 +602,22 @@ config FTRACE_MCOUNT_RECORD · 601 unmodified lines
602 depends on DYNAMIC_FTRACE602 depends on DYNAMIC_FTRACE
603 depends on HAVE_FTRACE_MCOUNT_RECORD603 depends on HAVE_FTRACE_MCOUNT_RECORD
604 604
605config FTRACE_MCOUNT_USE_PATCHABLE_FUNCTION_ENTRY
606 bool
607 depends on FTRACE_MCOUNT_RECORD
608
609config 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
615config 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
605config TRACING_MAP621config TRACING_MAP
606 bool622 bool
607 depends on ARCH_HAVE_NMI_SAFE_CMPXCHG623 depends on ARCH_HAVE_NMI_SAFE_CMPXCHG
scripts/Makefile.build+41−7
@@ -111,7 +111,7 @@ endif · 110 unmodified lines
111# ---------------------------------------------------------------------------111# ---------------------------------------------------------------------------
112 112
113quiet_cmd_cc_s_c = CC $(quiet_modtag) $@113quiet_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 $@ $<
115 115
116$(obj)/%.s: $(src)/%.c FORCE116$(obj)/%.s: $(src)/%.c FORCE
117 $(call if_changed_dep,cc_s_c)117 $(call if_changed_dep,cc_s_c)
@@ -166,6 +166,15 @@ ifdef CONFIG_MODVERSIONS · 48 unmodified lines
166# the actual value of the checksum generated by genksyms166# the actual value of the checksum generated by genksyms
167# o remove .tmp_<file>.o to <file>.o167# o remove .tmp_<file>.o to <file>.o
168 168
169ifdef 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.
172cmd_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;
177else
169cmd_modversions_c = \178cmd_modversions_c = \
170 if $(OBJDUMP) -h $@ | grep -q __ksymtab; then \179 if $(OBJDUMP) -h $@ | grep -q __ksymtab; then \
171 $(call cmd_gensymtypes_c,$(KBUILD_SYMTYPES),$(@:.o=.symtypes)) \180 $(call cmd_gensymtypes_c,$(KBUILD_SYMTYPES),$(@:.o=.symtypes)) \
@@ -177,9 +186,9 @@ cmd_modversions_c = \ · 5 unmodified lines
177 rm -f $(@D)/.tmp_$(@F:.o=.ver); \186 rm -f $(@D)/.tmp_$(@F:.o=.ver); \
178 fi187 fi
179endif188endif
189endif
180 190
181ifdef CONFIG_FTRACE_MCOUNT_RECORD191ifdef CONFIG_FTRACE_MCOUNT_USE_RECORDMCOUNT
182ifndef CC_USING_RECORD_MCOUNT
183# compiler will not generate __mcount_loc use recordmcount or recordmcount.pl192# compiler will not generate __mcount_loc use recordmcount or recordmcount.pl
184ifdef BUILD_C_RECORDMCOUNT193ifdef BUILD_C_RECORDMCOUNT
185ifeq ("$(origin RECORDMCOUNT_WARN)", "command line")194ifeq ("$(origin RECORDMCOUNT_WARN)", "command line")
@@ -206,8 +215,7 @@ recordmcount_source := $(srctree)/scripts/recordmcount.pl · 20 unmodified lines
206endif # BUILD_C_RECORDMCOUNT215endif # BUILD_C_RECORDMCOUNT
207cmd_record_mcount = $(if $(findstring $(strip $(CC_FLAGS_FTRACE)),$(_c_flags)), \216cmd_record_mcount = $(if $(findstring $(strip $(CC_FLAGS_FTRACE)),$(_c_flags)), \
208 $(sub_cmd_record_mcount))217 $(sub_cmd_record_mcount))
209endif # CC_USING_RECORD_MCOUNT218endif # CONFIG_FTRACE_MCOUNT_USE_RECORDMCOUNT
210endif # CONFIG_FTRACE_MCOUNT_RECORD
211 219
212ifdef CONFIG_STACK_VALIDATION220ifdef CONFIG_STACK_VALIDATION
213ifneq ($(SKIP_STACK_VALIDATION),1)221ifneq ($(SKIP_STACK_VALIDATION),1)
@@ -388,6 +396,18 @@ $(obj)/%.asn1.c $(obj)/%.asn1.h: $(src)/%.asn1 $(objtree)/scripts/asn1_compiler · 174 unmodified lines
388$(subdir-builtin): $(obj)/%/built-in.a: $(obj)/% ;396$(subdir-builtin): $(obj)/%/built-in.a: $(obj)/% ;
389$(subdir-modorder): $(obj)/%/modules.order: $(obj)/% ;397$(subdir-modorder): $(obj)/%/modules.order: $(obj)/% ;
390 398
399# combine symversions for later processing
400quiet_cmd_update_lto_symversions = SYMVER $@
401ifeq ($(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))
407else
408 cmd_update_lto_symversions = echo >/dev/null
409endif
410
391#411#
392# Rule to compile a set of .o files into one .a file (without symbol table)412# Rule to compile a set of .o files into one .a file (without symbol table)
393#413#
@@ -395,8 +415,11 @@ $(subdir-modorder): $(obj)/%/modules.order: $(obj)/% ; · 1 unmodified line
395quiet_cmd_ar_builtin = AR $@415quiet_cmd_ar_builtin = AR $@
396 cmd_ar_builtin = rm -f $@; $(AR) cDPrST $@ $(real-prereqs)416 cmd_ar_builtin = rm -f $@; $(AR) cDPrST $@ $(real-prereqs)
397 417
418quiet_cmd_ar_and_symver = AR $@
419 cmd_ar_and_symver = $(cmd_update_lto_symversions); $(cmd_ar_builtin)
420
398$(obj)/built-in.a: $(real-obj-y) FORCE421$(obj)/built-in.a: $(real-obj-y) FORCE
399 $(call if_changed,ar_builtin)422 $(call if_changed,ar_and_symver)
400 423
401#424#
402# Rule to create modules.order file425# Rule to create modules.order file
@@ -416,15 +439,26 @@ $(obj)/modules.order: $(obj-m) FORCE · 13 unmodified lines
416#439#
417# Rule to compile a set of .o files into one .a file (with symbol table)440# Rule to compile a set of .o files into one .a file (with symbol table)
418#441#
442quiet_cmd_ar_lib = AR $@
443 cmd_ar_lib = $(cmd_update_lto_symversions); $(cmd_ar)
444
419$(obj)/lib.a: $(lib-y) FORCE445$(obj)/lib.a: $(lib-y) FORCE
420 $(call if_changed,ar)446 $(call if_changed,ar_lib)
421 447
422# NOTE:448# NOTE:
423# Do not replace $(filter %.o,^) with $(real-prereqs). When a single object449# Do not replace $(filter %.o,^) with $(real-prereqs). When a single object
424# module is turned into a multi object module, $^ will contain header file450# module is turned into a multi object module, $^ will contain header file
425# dependencies recorded in the .*.cmd file.451# dependencies recorded in the .*.cmd file.
452ifdef CONFIG_LTO_CLANG
453quiet_cmd_link_multi-m = AR [M] $@
454cmd_link_multi-m = \
455 $(cmd_update_lto_symversions); \
456 rm -f $@; \
457 $(AR) cDPrsT $@ $(filter %.o,$^)
458else
426quiet_cmd_link_multi-m = LD [M] $@459quiet_cmd_link_multi-m = LD [M] $@
427 cmd_link_multi-m = $(LD) $(ld_flags) -r -o $@ $(filter %.o,$^)460 cmd_link_multi-m = $(LD) $(ld_flags) -r -o $@ $(filter %.o,$^)
461endif
428 462
429$(multi-used-m): FORCE463$(multi-used-m): FORCE
430 $(call if_changed,link_multi-m)464 $(call if_changed,link_multi-m)
scripts/Makefile.lib+4−2
@@ -119,9 +119,11 @@ target-stem = $(basename $(patsubst $(obj)/%,%,$@)) · 118 unmodified lines
119# These flags are needed for modversions and compiling, so we define them here119# These flags are needed for modversions and compiling, so we define them here
120# $(modname_flags) defines KBUILD_MODNAME as the name of the module it will120# $(modname_flags) defines KBUILD_MODNAME as the name of the module it will
121# end up in (or would, if it gets compiled in)121# end up in (or would, if it gets compiled in)
122name-fix = $(call stringify,$(subst $(comma),_,$(subst -,_,$1)))122name-fix-token = $(subst $(comma),_,$(subst -,_,$1))
123name-fix = $(call stringify,$(call name-fix-token,$1))
123basename_flags = -DKBUILD_BASENAME=$(call name-fix,$(basetarget))124basename_flags = -DKBUILD_BASENAME=$(call name-fix,$(basetarget))
124modname_flags = -DKBUILD_MODNAME=$(call name-fix,$(modname))125modname_flags = -DKBUILD_MODNAME=$(call name-fix,$(modname)) \
126 -D__KBUILD_MODNAME=kmod_$(call name-fix-token,$(modname))
125modfile_flags = -DKBUILD_MODFILE=$(call stringify,$(modfile))127modfile_flags = -DKBUILD_MODFILE=$(call stringify,$(modfile))
126 128
127_c_flags = $(filter-out $(CFLAGS_REMOVE_$(target-stem).o), \129_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
30 30
31ARCH_POSTLINK := $(wildcard $(srctree)/arch/$(SRCARCH)/Makefile.postlink)31ARCH_POSTLINK := $(wildcard $(srctree)/arch/$(SRCARCH)/Makefile.postlink)
32 32
33ifdef CONFIG_LTO_CLANG
34# With CONFIG_LTO_CLANG, reuse the object file we compiled for modpost to
35# avoid a second slow LTO link
36prelink-ext := .lto
37endif
38
33quiet_cmd_ld_ko_o = LD [M] $@39quiet_cmd_ld_ko_o = LD [M] $@
34 cmd_ld_ko_o = \40 cmd_ld_ko_o = \
35 $(LD) -r $(KBUILD_LDFLAGS) \41 $(LD) -r $(KBUILD_LDFLAGS) \
@@ -53,8 +59,9 @@ if_changed_except = $(if $(call newer_prereqs_except,$(2))$(cmd-check), \ · 17 unmodified lines
53 $(cmd); \59 $(cmd); \
54 printf '%s\n' 'cmd_$@ := $(make-cmd)' > $(dot-target).cmd, @:)60 printf '%s\n' 'cmd_$@ := $(make-cmd)' > $(dot-target).cmd, @:)
55 61
62
56# Re-generate module BTFs if either module's .ko or vmlinux changed63# 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) FORCE64$(modules): %.ko: %$(prelink-ext).o %.mod.o scripts/module.lds $(if $(KBUILD_BUILTIN),vmlinux) FORCE
58 +$(call if_changed_except,ld_ko_o,vmlinux)65 +$(call if_changed_except,ld_ko_o,vmlinux)
59ifdef CONFIG_DEBUG_INFO_BTF_MODULES66ifdef CONFIG_DEBUG_INFO_BTF_MODULES
60 +$(if $(newer-prereqs),$(call cmd,btf_ko))67 +$(if $(newer-prereqs),$(call cmd,btf_ko))
scripts/Makefile.modpost+23−2
@@ -43,6 +43,9 @@ __modpost: · 42 unmodified lines
43include include/config/auto.conf43include include/config/auto.conf
44include scripts/Kbuild.include44include scripts/Kbuild.include
45 45
46# for ld_flags
47include scripts/Makefile.lib
48
46MODPOST = scripts/mod/modpost \49MODPOST = scripts/mod/modpost \
47 $(if $(CONFIG_MODVERSIONS),-m) \50 $(if $(CONFIG_MODVERSIONS),-m) \
48 $(if $(CONFIG_MODULE_SRCVERSION_ALL),-a) \51 $(if $(CONFIG_MODULE_SRCVERSION_ALL),-a) \
@@ -102,12 +105,30 @@ $(input-symdump): · 53 unmodified lines
102 @echo >&2 'WARNING: Symbol version dump "$@" is missing.'105 @echo >&2 'WARNING: Symbol version dump "$@" is missing.'
103 @echo >&2 ' Modules may not have dependencies or modversions.'106 @echo >&2 ' Modules may not have dependencies or modversions.'
104 107
108ifdef 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
111prelink-ext := .lto
112
113quiet_cmd_cc_lto_link_modules = LTO [M] $@
114cmd_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)
122endif
123
124modules := $(sort $(shell cat $(MODORDER)))
125
105# Read out modules.order to pass in modpost.126# Read out modules.order to pass in modpost.
106# Otherwise, allmodconfig would fail with "Argument list too long".127# Otherwise, allmodconfig would fail with "Argument list too long".
107quiet_cmd_modpost = MODPOST $@128quiet_cmd_modpost = MODPOST $@
108 cmd_modpost = sed 's/ko$$/o/' $< | $(MODPOST) -T -129 cmd_modpost = sed 's/\.ko$$/$(prelink-ext)\.o/' $< | $(MODPOST) -T -
109 130
110$(output-symdump): $(MODORDER) $(input-symdump) FORCE131$(output-symdump): $(MODORDER) $(input-symdump) $(modules:.ko=$(prelink-ext).o) FORCE
111 $(call if_changed,modpost)132 $(call if_changed,modpost)
112 133
113targets += $(output-symdump)134targets += $(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
8use strict;
9use warnings;
10use IO::Handle;
11use IO::Select;
12use POSIX ":sys_wait_h";
13
14my $nm = $ENV{'NM'} || die "$0: ERROR: NM not set?";
15my $objtree = $ENV{'objtree'} || '.';
16
17## currently active child processes
18my $jobs = {}; # child process pid -> file handle
19## results from child processes
20my $results = {}; # object index -> [ { level, secname }, ... ]
21
22## reads _NPROCESSORS_ONLN to determine the maximum number of processes to
23## start
24sub 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>
39sub 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
58sub 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
97sub 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
155sub 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
188sub 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
224sub 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
270generate_initcall_lds();
scripts/link-vmlinux.sh+61−9
@@ -43,11 +43,37 @@ info() · 42 unmodified lines
43 fi43 fi
44}44}
45 45
46# Generate a linker script to ensure correct ordering of initcalls.
47gen_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
59gen_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
46# Link of vmlinux.o used for section mismatch analysis71# Link of vmlinux.o used for section mismatch analysis
47# ${1} output file72# ${1} output file
48modpost_link()73modpost_link()
49{74{
50 local objects75 local objects
76 local lds=""
51 77
52 objects="--whole-archive \78 objects="--whole-archive \
53 ${KBUILD_VMLINUX_OBJS} \79 ${KBUILD_VMLINUX_OBJS} \
@@ -56,7 +82,23 @@ modpost_link() · 2 unmodified lines
56 ${KBUILD_VMLINUX_LIBS} \82 ${KBUILD_VMLINUX_LIBS} \
57 --end-group"83 --end-group"
58 84
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}
60}102}
61 103
62objtool_link()104objtool_link()
@@ -103,13 +145,22 @@ vmlinux_link() · 40 unmodified lines
103 fi145 fi
104 146
105 if [ "${SRCARCH}" != "um" ]; then147 if [ "${SRCARCH}" != "um" ]; then
106 objects="--whole-archive \148 if [ -n "${CONFIG_LTO_CLANG}" ]; then
107 ${KBUILD_VMLINUX_OBJS} \149 # Use vmlinux.o instead of performing the slow LTO
108 --no-whole-archive \150 # link again.
109 --start-group \151 objects="--whole-archive \
110 ${KBUILD_VMLINUX_LIBS} \152 vmlinux.o \
111 --end-group \153 --no-whole-archive \
112 ${@}"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
113 164
114 ${LD} ${KBUILD_LDFLAGS} ${LDFLAGS_vmlinux} \165 ${LD} ${KBUILD_LDFLAGS} ${LDFLAGS_vmlinux} \
115 ${strip_debug#-Wl,} \166 ${strip_debug#-Wl,} \
@@ -225,6 +276,8 @@ cleanup() · 109 unmodified lines
225{276{
226 rm -f .btf.*277 rm -f .btf.*
227 rm -f .tmp_System.map278 rm -f .tmp_System.map
279 rm -f .tmp_initcalls.lds
280 rm -f .tmp_symversions.lds
228 rm -f .tmp_vmlinux*281 rm -f .tmp_vmlinux*
229 rm -f System.map282 rm -f System.map
230 rm -f vmlinux283 rm -f vmlinux
@@ -274,7 +327,6 @@ fi; · 43 unmodified lines
274${MAKE} -f "${srctree}/scripts/Makefile.build" obj=init need-builtin=1327${MAKE} -f "${srctree}/scripts/Makefile.build" obj=init need-builtin=1
275 328
276#link vmlinux.o329#link vmlinux.o
277info LD vmlinux.o
278modpost_link vmlinux.o330modpost_link vmlinux.o
279objtool_link vmlinux.o331objtool_link vmlinux.o
280 332
scripts/lto-used-symbollist.txt+5−0
@@ -0,0 +1,5 @@
1memcpy
2memmove
3memset
4__stack_chk_fail
5__stack_chk_guard
scripts/mod/Makefile+1−0
@@ -1,5 +1,6 @@
1# SPDX-License-Identifier: GPL-2.01# SPDX-License-Identifier: GPL-2.0
2OBJECT_FILES_NON_STANDARD := y2OBJECT_FILES_NON_STANDARD := y
3CFLAGS_REMOVE_empty.o += $(CC_FLAGS_LTO)
3 4
4hostprogs-always-y += modpost mk_elfconfig5hostprogs-always-y += modpost mk_elfconfig
5always-y += empty.o6always-y += empty.o
scripts/mod/modpost.c+7−9
@@ -17,7 +17,6 @@ · 16 unmodified lines
17#include <ctype.h>17#include <ctype.h>
18#include <string.h>18#include <string.h>
19#include <limits.h>19#include <limits.h>
20#include <stdbool.h>
21#include <errno.h>20#include <errno.h>
22#include "modpost.h"21#include "modpost.h"
23#include "../../include/linux/license.h"22#include "../../include/linux/license.h"
@@ -84,14 +83,6 @@ modpost_log(enum loglevel loglevel, const char *fmt, ...) · 60 unmodified lines
84 error_occurred = true;83 error_occurred = true;
85}84}
86 85
87static 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
95void *do_nofail(void *ptr, const char *expr)86void *do_nofail(void *ptr, const char *expr)
96{87{
97 if (!ptr)88 if (!ptr)
@@ -1988,6 +1979,10 @@ static char *remove_dot(char *s) · 1890 unmodified lines
1988 size_t m = strspn(s + n + 1, "0123456789");1979 size_t m = strspn(s + n + 1, "0123456789");
1989 if (m && (s[n + m] == '.' || s[n + m] == 0))1980 if (m && (s[n + m] == '.' || s[n + m] == 0))
1990 s[n] = 0;1981 s[n] = 0;
1982
1983 /* strip trailing .lto */
1984 if (strends(s, ".lto"))
1985 s[strlen(s) - 4] = '\0';
1991 }1986 }
1992 return s;1987 return s;
1993}1988}
@@ -2011,6 +2006,9 @@ static void read_symbols(const char *modname) · 17 unmodified lines
2011 /* strip trailing .o */2006 /* strip trailing .o */
2012 tmp = NOFAIL(strdup(modname));2007 tmp = NOFAIL(strdup(modname));
2013 tmp[strlen(tmp) - 2] = '\0';2008 tmp[strlen(tmp) - 2] = '\0';
2009 /* strip trailing .lto */
2010 if (strends(tmp, ".lto"))
2011 tmp[strlen(tmp) - 4] = '\0';
2014 mod = new_module(tmp);2012 mod = new_module(tmp);
2015 free(tmp);2013 free(tmp);
2016 }2014 }
scripts/mod/modpost.h+9−0
@@ -2,6 +2,7 @@ · 1 unmodified line
2#include <stdio.h>2#include <stdio.h>
3#include <stdlib.h>3#include <stdlib.h>
4#include <stdarg.h>4#include <stdarg.h>
5#include <stdbool.h>
5#include <string.h>6#include <string.h>
6#include <sys/types.h>7#include <sys/types.h>
7#include <sys/stat.h>8#include <sys/stat.h>
@@ -180,6 +181,14 @@ static inline unsigned int get_secindex(const struct elf_info *info, · 172 unmodified lines
180 return info->symtab_shndx_start[sym - info->symtab_start];181 return info->symtab_shndx_start[sym - info->symtab_start];
181}182}
182 183
184static 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
183/* file2alias.c */192/* file2alias.c */
184extern unsigned int cross_build;193extern unsigned int cross_build;
185void handle_moddevtable(struct module *mod, struct elf_info *info,194void 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
391 struct md4_ctx md;391 struct md4_ctx md;
392 char *fname;392 char *fname;
393 char filelist[PATH_MAX + 1];393 char filelist[PATH_MAX + 1];
394 int postfix_len = 1;
395
396 if (strends(modname, ".lto.o"))
397 postfix_len = 5;
394 398
395 /* objects for a module are listed in the first line of *.mod file. */399 /* objects for a module are listed in the first line of *.mod file. */
396 snprintf(filelist, sizeof(filelist), "%.*smod",400 snprintf(filelist, sizeof(filelist), "%.*smod",
397 (int)strlen(modname) - 1, modname);401 (int)strlen(modname) - postfix_len, modname);
398 402
399 buf = read_text_file(filelist);403 buf = read_text_file(filelist);
400 404
scripts/module.lds.S+24−0
@@ -23,6 +23,30 @@ SECTIONS { · 22 unmodified lines
23 .init_array 0 : ALIGN(8) { *(SORT(.init_array.*)) *(.init_array) }23 .init_array 0 : ALIGN(8) { *(SORT(.init_array.*)) *(.init_array) }
24 24
25 __jump_table 0 : ALIGN(8) { KEEP(*(__jump_table)) }25 __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_]*) }
26}50}
27 51
28/* bring in arch-specific sections */52/* bring in arch-specific sections */