From nobody Sun Dec 14 19:34:07 2025 Received: from mail-pl1-f201.google.com (mail-pl1-f201.google.com [209.85.214.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 62B5621A444 for ; Wed, 21 May 2025 21:35:39 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747863341; cv=none; b=aB/JszmKUdbe/+eKm5mJiGaQt/HIyW9dGylGN/EgbAeZAdx30fEYF5uVeddMQjs+7zqgbbSHpQC1GLTtqEAxo/EMLlatWBZW1j26RohFS3nVyMxSffXZbngQR7FGrTtTZhV7RuOAfiUy/wlgQhf7O0NUJU8xUv+x1p29ZwX+MdM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747863341; c=relaxed/simple; bh=0WDxkufMxss/gJcxwa02FCUvPZcc5O/Gjwe2hYFCqrk=; h=Date:Mime-Version:Message-ID:Subject:From:To:Cc:Content-Type; b=MPo+b34jBkZ0aNW4tBo9ZmD5VcX0KN68l2n1OZPf4R7kkaKnvVWIBfeHlPIWeKyLd7h3RXQXuGG6hC27f5QcLbIsbxROQILwTX4gPTqTeBdPIy2692yh0WuuH8AhPz5QeJ9edHeAtyCRosMtQDc93T73yFru6e/vspnFisz2kIE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--xur.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=jjAcT8w0; arc=none smtp.client-ip=209.85.214.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--xur.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="jjAcT8w0" Received: by mail-pl1-f201.google.com with SMTP id d9443c01a7336-231e8ec895bso42363865ad.1 for ; Wed, 21 May 2025 14:35:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1747863338; x=1748468138; darn=vger.kernel.org; h=cc:to:from:subject:message-id:mime-version:date:from:to:cc:subject :date:message-id:reply-to; bh=twOQ0zkEbfhDHsU4LZ2wB2l+IbeKX16uAo6ab6ScVfc=; b=jjAcT8w0SxbP4dgP8zM1rlRHf3iLNqGhHrpuBQTPYDG1AQd11K1vIXYQn7BkwCAF2+ zrDGMmFm2ePme9hvhpq7YBv3lL/DaOg3SAf7PZ6/OTTE7sUXaceFgXgYu4J7ACk4d4Gm P4B1QhwnRBrVO9ZPAW2uWwz+vmIbEe9C1B4wNTMCkuXfy6kR8+CAceYTzcoTF2dt3Zu3 OTHit670wyYpF+zGdh6LCPtqfxFyqKJJjAlE6QmRhN+Akfkw/ELnyuqRdIVjOwgfMr0n SL/tkltnc2Ezc5StItnu475RM9tEBwfvzaF7SOJgPnvXYFzwbCOWFSkRId6rquZqZh8z shZA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1747863338; x=1748468138; h=cc:to:from:subject:message-id:mime-version:date:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=twOQ0zkEbfhDHsU4LZ2wB2l+IbeKX16uAo6ab6ScVfc=; b=nuO7mnuHWyEjDAJiOo64QzrrNkB3kBfydX6uirIEBgy4ByOh3sV8MUKDVcvPSdArzZ Sm/WU45KWrFOESX8lQ3y9pBXpPOzU4Ocd0iJqayo52tiQlA8aeDTrlaJd8Z7iVhkKQ03 0AsyWuvir0tUBf3D4PX8MIb4gNFmCRiE4ttOZjSf80qCeg9KzlmJKM8A6vc03+DOysPP HL2cF2WR5krEUVBebCUxOVbLwuyf+Gt4/0BjJao2QgnYtNBeBWSnbQJ17xxXKP/NATJo N/zsecadWZOUg0ER3EoPrTgTpsKhoN+xZG6Y3vRaLVtZ2scBWc46swrlpr/mT1Fns+DV Aezg== X-Gm-Message-State: AOJu0Yy6R6T3RRdh+ePjBuh2HD3PAY3WatoQrwAN5kSCMj7MRMHYuflx bhmSBWdVwSRweDPzBmXC2W6PSQNsY+JpLBw+QgRzrP8zy+wh+LQJRK6/7AyiQudOVv7qLA== X-Google-Smtp-Source: AGHT+IHO06DTHnhGFqnnQzopsQsVoFzJyJXOaU3ijWuWobAVfc03CRd0ey5KoG+UFRlbc25d4QFVXUw= X-Received: from plht3.prod.google.com ([2002:a17:903:2f03:b0:223:49cb:8f99]) (user=xur job=prod-delivery.src-stubby-dispatcher) by 2002:a17:902:e786:b0:22e:5e82:66fa with SMTP id d9443c01a7336-231d450f307mr312790615ad.19.1747863338608; Wed, 21 May 2025 14:35:38 -0700 (PDT) Date: Wed, 21 May 2025 14:35:34 -0700 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 X-Mailer: git-send-email 2.49.0.1143.g0be31eac6b-goog Message-ID: <20250521213534.3159514-1-xur@google.com> Subject: [PATCH v3] kbuild: distributed build support for Clang ThinLTO From: xur@google.com To: Masahiro Yamada , Nathan Chancellor , Eric Naim , Nicolas Schier , Nick Desaulniers , Bill Wendling , Justin Stitt , Miguel Ojeda , Thomas Gleixner , Alice Ryhl , Sami Tolvanen , "Mike Rapoport (Microsoft)" , Rafael Aquini , Michael Ellerman , Stafford Horne , Christophe Leroy , Rong Xu , Teresa Johnson Cc: linux-kernel@vger.kernel.org, linux-kbuild@vger.kernel.org, llvm@lists.linux.dev Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Rong Xu Add distributed ThinLTO build support for the Linux kernel. This new mode offers several advantages: (1) Increased flexibility in handling user-specified build options. (2) Improved user-friendliness for developers. (3) Greater convenience for integrating with objtool and livepatch. Note that "distributed" in this context refers to a term that differentiates in-process ThinLTO builds by invoking backend compilation through the linker, not necessarily building in distributed environments. Distributed ThinLTO is enabled via the `CONFIG_LTO_CLANG_THIN_DIST` Kconfig option. For example: > make LLVM=3D1 defconfig > scripts/config -e LTO_CLANG_THIN_DIST > make LLVM=3D1 oldconfig > make LLVM=3D1 vmlinux -j <..> The implementation changes the top-level Makefile with a macro for generating `vmlinux.o` for distributed ThinLTO builds. It uses the existing Kbuild infrastructure to perform two recursive passes through the subdirectories. The first pass generates LLVM IR object files, similar to in-process ThinLTO. Following the thin-link stage, a second pass compiles these IR files into the final native object files. The build rules and actions for this two-pass process are primarily implemented in `scripts/Makefile.build`. Currently, this patch focuses on building the main kernel image (`vmlinux`) only. Support for building kernel modules using this method is planned for a subsequent patch. Tested on the following arch: x86, arm64, loongarch, and riscv. Some implementation details can be found here: https://discourse.llvm.org/t/rfc-distributed-thinlto-build-for-kernel/85934 Signed-off-by: Rong Xu --- Changelog since v1: - Updated the description in arch/Kconfig based on feedback from Nathan Chancellor - Revised file suffixes: .final_o -> .o.thinlto.native, and .final_a -> .a.thinlto.native - Updated list of ignored files in .gitignore Changelog since v2: - Changed file suffixes: .o.thinlto.native -> .o_thinlto_native, and .a.thinlto.native -> .a_thinlto_native so that basename works as intended. - Tested the patch with AutoFDO and Propeller. --- .gitignore | 3 ++ MAINTAINERS | 5 +++ Makefile | 40 ++++++++++++++++++++--- arch/Kconfig | 19 +++++++++++ scripts/Makefile.build | 52 +++++++++++++++++++++++++++--- scripts/Makefile.lib | 7 +++- scripts/Makefile.vmlinux_o | 16 +++++++--- scripts/Makefile.vmlinux_thinlink | 53 +++++++++++++++++++++++++++++++ scripts/head-object-list.txt | 1 + 9 files changed, 181 insertions(+), 15 deletions(-) create mode 100644 scripts/Makefile.vmlinux_thinlink diff --git a/.gitignore b/.gitignore index f2f63e47fb886..b83a68185ef46 100644 --- a/.gitignore +++ b/.gitignore @@ -12,6 +12,7 @@ # .* *.a +*.a_thinlto_native *.asn1.[ch] *.bin *.bz2 @@ -39,6 +40,7 @@ *.mod.c *.o *.o.* +*.o_thinlto_native *.patch *.rmeta *.rpm @@ -64,6 +66,7 @@ modules.order /vmlinux /vmlinux.32 /vmlinux.map +/vmlinux.thinlink /vmlinux.symvers /vmlinux.unstripped /vmlinux-gdb.py diff --git a/MAINTAINERS b/MAINTAINERS index d48dd6726fe6b..f54090f364c93 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -5790,6 +5790,11 @@ F: scripts/Makefile.clang F: scripts/clang-tools/ K: \b(?i:clang|llvm)\b =20 +CLANG/LLVM THINLTO DISTRIBUTED BUILD +M: Rong Xu +S: Supported +F: scripts/Makefile.vmlinux_thinlink + CLK API M: Russell King L: linux-clk@vger.kernel.org diff --git a/Makefile b/Makefile index a9edd03036537..8fbff2ab87ebd 100644 --- a/Makefile +++ b/Makefile @@ -298,7 +298,8 @@ no-dot-config-targets :=3D $(clean-targets) \ outputmakefile rustavailable rustfmt rustfmtcheck no-sync-config-targets :=3D $(no-dot-config-targets) %install modules_sign= kernelrelease \ image_name -single-targets :=3D %.a %.i %.ko %.lds %.ll %.lst %.mod %.o %.rsi %.s %/ +single-targets :=3D %.a %.i %.ko %.lds %.ll %.lst %.mod %.o %.rsi %.s %.o_= thinlto_native \ + %.a_thinlto_native %.o.thinlto.bc %/ =20 config-build :=3D mixed-build :=3D @@ -991,10 +992,10 @@ export CC_FLAGS_SCS endif =20 ifdef CONFIG_LTO_CLANG -ifdef CONFIG_LTO_CLANG_THIN -CC_FLAGS_LTO :=3D -flto=3Dthin -fsplit-lto-unit -else +ifdef CONFIG_LTO_CLANG_FULL CC_FLAGS_LTO :=3D -flto +else # for CONFIG_LTO_CLANG_THIN or CONFIG_LTO_CLANG_THIN_DIST +CC_FLAGS_LTO :=3D -flto=3Dthin -fsplit-lto-unit endif CC_FLAGS_LTO +=3D -fvisibility=3Dhidden =20 @@ -1213,8 +1214,34 @@ vmlinux.a: $(KBUILD_VMLINUX_OBJS) scripts/head-objec= t-list.txt FORCE $(call if_changed,ar_vmlinux.a) =20 PHONY +=3D vmlinux_o +ifdef CONFIG_LTO_CLANG_THIN_DIST +vmlinux.thinlink: vmlinux.a $(KBUILD_VMLINUX_LIBS) FORCE + $(Q)$(MAKE) -f $(srctree)/scripts/Makefile.vmlinux_thinlink +targets +=3D vmlinux.thinlink + +vmlinux.a_thinlto_native :=3D $(patsubst %.a,%.a_thinlto_native,$(KBUILD_V= MLINUX_OBJS)) +quiet_cmd_ar_vmlinux.a_thinlto_native =3D AR $@ + cmd_ar_vmlinux.a_thinlto_native =3D \ + rm -f $@; \ + $(AR) cDPrST $@ $(vmlinux.a_thinlto_native); \ + $(AR) mPiT $$($(AR) t $@ | sed -n 1p) $@ $$($(AR) t $@ | grep -F -f $(src= tree)/scripts/head-object-list.txt) + +define rule_gen_vmlinux.a_thinlto_native + +$(Q)$(MAKE) $(build)=3D. need-builtin=3D1 thinlto_final_pass=3D1 need-mo= dorder=3D1 built-in.a_thinlto_native + $(call cmd_and_savecmd,ar_vmlinux.a_thinlto_native) +endef + +vmlinux.a_thinlto_native: vmlinux.thinlink scripts/head-object-list.txt FO= RCE + $(call if_changed_rule,gen_vmlinux.a_thinlto_native) + +targets +=3D vmlinux.a_thinlto_native + +vmlinux_o: vmlinux.a_thinlto_native + $(Q)$(MAKE) thinlto_final_pass=3D1 -f $(srctree)/scripts/Makefile.vmlinux= _o +else vmlinux_o: vmlinux.a $(KBUILD_VMLINUX_LIBS) $(Q)$(MAKE) -f $(srctree)/scripts/Makefile.vmlinux_o +endif =20 vmlinux.o modules.builtin.modinfo modules.builtin: vmlinux_o @: @@ -1572,7 +1599,8 @@ CLEAN_FILES +=3D vmlinux.symvers modules-only.symvers= \ modules.builtin.ranges vmlinux.o.map vmlinux.unstripped \ compile_commands.json rust/test \ rust-project.json .vmlinux.objs .vmlinux.export.c \ - .builtin-dtbs-list .builtin-dtb.S + .builtin-dtbs-list .builtin-dtb.S \ + .vmlinux_thinlto_bc_files vmlinux.thinlink =20 # Directories & files removed with 'make mrproper' MRPROPER_FILES +=3D include/config include/generated \ @@ -2023,6 +2051,8 @@ clean: $(clean-dirs) -o -name '*.symtypes' -o -name 'modules.order' \ -o -name '*.c.[012]*.*' \ -o -name '*.ll' \ + -o -name '*.a_thinlto_native' -o -name '*.o_thinlto_native' \ + -o -name '*.o.thinlto.bc' \ -o -name '*.gcno' \ \) -type f -print \ -o -name '.tmp_*' -print \ diff --git a/arch/Kconfig b/arch/Kconfig index b0adb665041f1..30dccda07c671 100644 --- a/arch/Kconfig +++ b/arch/Kconfig @@ -810,6 +810,25 @@ config LTO_CLANG_THIN https://clang.llvm.org/docs/ThinLTO.html =20 If unsure, say Y. + +config LTO_CLANG_THIN_DIST + bool "Clang ThinLTO in distributed mode (EXPERIMENTAL)" + depends on HAS_LTO_CLANG && ARCH_SUPPORTS_LTO_CLANG_THIN + select LTO_CLANG + help + This option enables Clang's ThinLTO in distributed build mode. + In this mode, the linker performs the thin-link, generating + ThinLTO index files. Subsequently, the build system explicitly + invokes ThinLTO backend compilation using these index files + and pre-linked IR objects. The resulting native object files + are with the .o_thinlto_native suffix. + + This build mode offers improved visibility into the ThinLTO + process through explicit subcommand exposure. It also makes + final native object files directly available, benefiting + tools like objtool and kpatch. Additionally, it provides + crucial granular control over back-end options, enabling + module-specific compiler options, and simplifies debugging. endchoice =20 config ARCH_SUPPORTS_AUTOFDO_CLANG diff --git a/scripts/Makefile.build b/scripts/Makefile.build index 13dcd86e74ca8..338e1aec0eaa3 100644 --- a/scripts/Makefile.build +++ b/scripts/Makefile.build @@ -50,18 +50,23 @@ endif =20 # =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D =20 +builtin_suffix :=3D $(if $(filter %.a_thinlto_native, $(MAKECMDGOALS)),.a_= thinlto_native,.a) +ifeq ($(thinlto_final_pass),1) +builtin_suffix :=3D.a_thinlto_native +endif + # subdir-builtin and subdir-modorder may contain duplications. Use $(sort = ...) -subdir-builtin :=3D $(sort $(filter %/built-in.a, $(real-obj-y))) +subdir-builtin :=3D $(sort $(filter %/built-in$(builtin_suffix), $(real-ob= j-y))) subdir-modorder :=3D $(sort $(filter %/modules.order, $(obj-m))) =20 targets-for-builtin :=3D $(extra-y) =20 ifneq ($(strip $(lib-y) $(lib-m) $(lib-)),) -targets-for-builtin +=3D $(obj)/lib.a +targets-for-builtin +=3D $(obj)/lib$(builtin_suffix) endif =20 ifdef need-builtin -targets-for-builtin +=3D $(obj)/built-in.a +targets-for-builtin +=3D $(obj)/built-in$(builtin_suffix) endif =20 targets-for-modules :=3D $(foreach x, o mod, \ @@ -337,6 +342,10 @@ $(obj)/%.o: $(obj)/%.S FORCE targets +=3D $(filter-out $(subdir-builtin), $(real-obj-y)) targets +=3D $(filter-out $(subdir-modorder), $(real-obj-m)) targets +=3D $(lib-y) $(always-y) +ifeq ($(builtin_suffix),.a_thinlto_native) +native_targets =3D $(patsubst,%.o,%.o_thinlto_native,$(targets)) +targets +=3D $(native_targets) +endif =20 # Linker scripts preprocessor (.lds.S -> .lds) # ------------------------------------------------------------------------= --- @@ -347,6 +356,24 @@ quiet_cmd_cpp_lds_S =3D LDS $@ $(obj)/%.lds: $(src)/%.lds.S FORCE $(call if_changed_dep,cpp_lds_S) =20 +ifdef CONFIG_LTO_CLANG_THIN_DIST +# Generate .o_thinlto_native (obj) from .o (bitcode) file +# ------------------------------------------------------------------------= --- +quiet_cmd_cc_o_bc =3D CC $(quiet_modtag) $@ + +cmd_cc_o_bc =3D $(if $(filter bitcode, $(shell file -b $<)),$(CC) \ + $(filter-out -Wp% $(LINUXINCLUDE) %.h.gch %.h -D% \ + -flto=3Dthin, $(c_flags)) \ + -Wno-unused-command-line-argument \ + -x ir -fthinlto-index=3D$<.thinlto.bc -c -o $@ \ + $(if $(findstring ../,$<), \ + $$(realpath --relative-to=3D$(srcroot) $<), $<), \ + cp $< $@) + +$(obj)/%.o_thinlto_native: $(obj)/%.o FORCE + $(call if_changed,cc_o_bc) +endif + # ASN.1 grammar # ------------------------------------------------------------------------= --- quiet_cmd_asn1_compiler =3D ASN.1 $(basename $@).[ch] @@ -360,7 +387,7 @@ $(obj)/%.asn1.c $(obj)/%.asn1.h: $(src)/%.asn1 $(objtre= e)/scripts/asn1_compiler # ------------------------------------------------------------------------= --- =20 # To build objects in subdirs, we need to descend into the directories -$(subdir-builtin): $(obj)/%/built-in.a: $(obj)/% ; +$(subdir-builtin): $(obj)/%/built-in$(builtin_suffix): $(obj)/% ; $(subdir-modorder): $(obj)/%/modules.order: $(obj)/% ; =20 # @@ -377,6 +404,12 @@ quiet_cmd_ar_builtin =3D AR $@ $(obj)/built-in.a: $(real-obj-y) FORCE $(call if_changed,ar_builtin) =20 +ifdef CONFIG_LTO_CLANG_THIN_DIST +# Rule to compile a set of .o_thinlto_native files into one .a_thinlto_nat= ive file. +$(obj)/built-in.a_thinlto_native: $(patsubst %.o,%.o_thinlto_native,$(real= -obj-y)) FORCE + $(call if_changed,ar_builtin) +endif + # This is a list of build artifacts from the current Makefile and its # sub-directories. The timestamp should be updated when any of the member = files. =20 @@ -394,6 +427,14 @@ $(obj)/modules.order: $(obj-m) FORCE $(obj)/lib.a: $(lib-y) FORCE $(call if_changed,ar) =20 +ifdef CONFIG_LTO_CLANG_THIN_DIST +quiet_cmd_ar_native =3D AR $@ + cmd_ar_native =3D rm -f $@; $(AR) cDPrsT $@ $(patsubst %.o,%.o_thinl= to_native,$(real-prereqs)) + +$(obj)/lib.a_thinlto_native: $(patsubst %.o,%.o_thinlto_native,$(lib-y)) F= ORCE + $(call if_changed,ar_native) +endif + quiet_cmd_ld_multi_m =3D LD [M] $@ cmd_ld_multi_m =3D $(LD) $(ld_flags) -r -o $@ @$< $(cmd_objtool) =20 @@ -459,7 +500,8 @@ $(single-subdir-goals): $(single-subdirs) PHONY +=3D $(subdir-ym) $(subdir-ym): $(Q)$(MAKE) $(build)=3D$@ \ - need-builtin=3D$(if $(filter $@/built-in.a, $(subdir-builtin)),1) \ + need-builtin=3D$(if $(filter $@/built-in$(builtin_suffix), $(subdir-built= in)),1) \ + thinlto_final_pass=3D$(if $(filter .a_thinlto_native, $(builtin_suffix)),= 1) \ need-modorder=3D$(if $(filter $@/modules.order, $(subdir-modorder)),1) \ $(filter $@/%, $(single-subdir-goals)) =20 diff --git a/scripts/Makefile.lib b/scripts/Makefile.lib index 2fe73cda0bddb..9cfd23590334d 100644 --- a/scripts/Makefile.lib +++ b/scripts/Makefile.lib @@ -34,8 +34,13 @@ else obj-m :=3D $(filter-out %/, $(obj-m)) endif =20 +builtin_suffix :=3D $(if $(filter %.a_thinlto_native, $(MAKECMDGOALS)),.a_= thinlto_native,.a) +ifeq ($(thinlto_final_pass),1) + builtin_suffix :=3D.a_thinlto_native +endif + ifdef need-builtin -obj-y :=3D $(patsubst %/, %/built-in.a, $(obj-y)) +obj-y :=3D $(patsubst %/, %/built-in$(builtin_suffix), $(obj-y)) else obj-y :=3D $(filter-out %/, $(obj-y)) endif diff --git a/scripts/Makefile.vmlinux_o b/scripts/Makefile.vmlinux_o index b024ffb3e2018..f9abc45a68b36 100644 --- a/scripts/Makefile.vmlinux_o +++ b/scripts/Makefile.vmlinux_o @@ -9,6 +9,14 @@ include $(srctree)/scripts/Kbuild.include # for objtool include $(srctree)/scripts/Makefile.lib =20 +ifeq ($(thinlto_final_pass),1) +vmlinux_a :=3D vmlinux.a_thinlto_native +vmlinux_libs :=3D $(patsubst %.a,%.a_thinlto_native,$(KBUILD_VMLINUX_LIBS)) +else +vmlinux_a :=3D vmlinux.a +vmlinux_libs :=3D $(KBUILD_VMLINUX_LIBS) +endif + # Generate a linker script to ensure correct ordering of initcalls for Cla= ng LTO # ------------------------------------------------------------------------= --- =20 @@ -18,7 +26,7 @@ quiet_cmd_gen_initcalls_lds =3D GEN $@ $(PERL) $(real-prereqs) > $@ =20 .tmp_initcalls.lds: $(srctree)/scripts/generate_initcall_order.pl \ - vmlinux.a $(KBUILD_VMLINUX_LIBS) FORCE + $(vmlinux_a) $(vmlinux_libs) FORCE $(call if_changed,gen_initcalls_lds) =20 targets :=3D .tmp_initcalls.lds @@ -59,8 +67,8 @@ quiet_cmd_ld_vmlinux.o =3D LD $@ $(LD) ${KBUILD_LDFLAGS} -r -o $@ \ $(vmlinux-o-ld-args-y) \ $(addprefix -T , $(initcalls-lds)) \ - --whole-archive vmlinux.a --no-whole-archive \ - --start-group $(KBUILD_VMLINUX_LIBS) --end-group \ + --whole-archive $(vmlinux_a) --no-whole-archive \ + --start-group $(vmlinux_libs) --end-group \ $(cmd_objtool) =20 define rule_ld_vmlinux.o @@ -68,7 +76,7 @@ define rule_ld_vmlinux.o $(call cmd,gen_objtooldep) endef =20 -vmlinux.o: $(initcalls-lds) vmlinux.a $(KBUILD_VMLINUX_LIBS) FORCE +vmlinux.o: $(initcalls-lds) $(vmlinux_a) $(vmlinux_libs) FORCE $(call if_changed_rule,ld_vmlinux.o) =20 targets +=3D vmlinux.o diff --git a/scripts/Makefile.vmlinux_thinlink b/scripts/Makefile.vmlinux_t= hinlink new file mode 100644 index 0000000000000..13e4026c7d45b --- /dev/null +++ b/scripts/Makefile.vmlinux_thinlink @@ -0,0 +1,53 @@ +# SPDX-License-Identifier: GPL-2.0-only + +PHONY :=3D __default +__default: vmlinux.thinlink + +include include/config/auto.conf +include $(srctree)/scripts/Kbuild.include + + +# Generate a linker script to ensure correct ordering of initcalls for Cla= ng LTO +# ------------------------------------------------------------------------= --- + +quiet_cmd_gen_initcalls_lds =3D GEN $@ + cmd_gen_initcalls_lds =3D \ + $(PYTHON3) $(srctree)/scripts/jobserver-exec \ + $(PERL) $(real-prereqs) > $@ + +.tmp_initcalls_thinlink.lds: $(srctree)/scripts/generate_initcall_order.pl= \ + vmlinux.a FORCE + $(call if_changed,gen_initcalls_lds) + +targets :=3D .tmp_initcalls_thinlink.lds + +initcalls-lds :=3D .tmp_initcalls_thinlink.lds + +quiet_cmd_ld_vmlinux.thinlink =3D LD $@ + cmd_ld_vmlinux.thinlink =3D \ + $(AR) t vmlinux.a > .vmlinux_thinlto_bc_files; \ + $(LD) ${KBUILD_LDFLAGS} -r $(addprefix -T , $(initcalls-lds)) \ + --thinlto-index-only @.vmlinux_thinlto_bc_files; \ + touch vmlinux.thinlink + +vmlinux.thinlink: vmlinux.a $(initcalls-lds) FORCE + $(call if_changed,ld_vmlinux.thinlink) + +targets +=3D vmlinux.thinlink + +# Add FORCE to the prerequisites of a target to force it to be always rebu= ilt. +# ------------------------------------------------------------------------= --- + +PHONY +=3D FORCE +FORCE: + +# Read all saved command lines and dependencies for the $(targets) we +# may be building above, using $(if_changed{,_dep}). As an +# optimization, we don't need to read them if the target does not +# exist, we will rebuild anyway in that case. + +existing-targets :=3D $(wildcard $(sort $(targets))) + +-include $(foreach f,$(existing-targets),$(dir $(f)).$(notdir $(f)).cmd) + +.PHONY: $(PHONY) diff --git a/scripts/head-object-list.txt b/scripts/head-object-list.txt index 7274dfc65af60..90710b87a3877 100644 --- a/scripts/head-object-list.txt +++ b/scripts/head-object-list.txt @@ -18,6 +18,7 @@ arch/arm/kernel/head.o arch/csky/kernel/head.o arch/hexagon/kernel/head.o arch/loongarch/kernel/head.o +arch/loongarch/kernel/head.o_thinlto_native arch/m68k/68000/head.o arch/m68k/coldfire/head.o arch/m68k/kernel/head.o --=20 2.49.0.1143.g0be31eac6b-goog