diff --git a/embed-riscv64-target-abi-in-bc-when-plugin-_-lto-is-used.patch b/embed-riscv64-target-abi-in-bc-when-plugin-_-lto-is-used.patch new file mode 100644 index 0000000000000000000000000000000000000000..631a5eb9fb1006f2268e9a569730917ef8e2b892 --- /dev/null +++ b/embed-riscv64-target-abi-in-bc-when-plugin-_-lto-is-used.patch @@ -0,0 +1,296 @@ +From a599284dbac82d272aac53a7ace524484b4bb4dc Mon Sep 17 00:00:00 2001 +From: kxxt +Date: Sun, 28 Jan 2024 18:38:41 +0800 +Subject: [PATCH] embed riscv64 target abi in bc when plugin-lto is used + +Fixes chromium linking error. + +Related: +- https://discourse.llvm.org/t/encode-target-abi-into-llvm-bitcode-for-lto/54116 +- https://internals.rust-lang.org/t/per-target-llvm-module-flags/12023 +--- + compiler/rustc_codegen_llvm/src/back/lto.rs | 2 +- + compiler/rustc_codegen_llvm/src/context.rs | 49 +++++++++++++------ + .../rustc_codegen_llvm/src/debuginfo/mod.rs | 6 +-- + compiler/rustc_codegen_llvm/src/llvm/ffi.rs | 10 +++- + compiler/rustc_codegen_llvm/src/llvm/mod.rs | 5 ++ + .../rustc_llvm/llvm-wrapper/RustWrapper.cpp | 11 ++++- + 6 files changed, 62 insertions(+), 21 deletions(-) + +diff --git a/compiler/rustc_codegen_llvm/src/back/lto.rs b/compiler/rustc_codegen_llvm/src/back/lto.rs +index 42bd8687042a1..6c204b7ab2bc9 100644 +--- a/compiler/rustc_codegen_llvm/src/back/lto.rs ++++ b/compiler/rustc_codegen_llvm/src/back/lto.rs +@@ -607,7 +607,7 @@ pub(crate) fn run_pass_manager( + "LTOPostLink".as_ptr().cast(), + 11, + ) { +- llvm::LLVMRustAddModuleFlag( ++ llvm::LLVMRustAddModuleFlagU32( + module.module_llvm.llmod(), + llvm::LLVMModFlagBehavior::Error, + c"LTOPostLink".as_ptr().cast(), +diff --git a/compiler/rustc_codegen_llvm/src/context.rs b/compiler/rustc_codegen_llvm/src/context.rs +index 6cb62280a595e..ebb657f97b398 100644 +--- a/compiler/rustc_codegen_llvm/src/context.rs ++++ b/compiler/rustc_codegen_llvm/src/context.rs +@@ -30,6 +30,7 @@ use rustc_span::Span; + use rustc_target::abi::{ + call::FnAbi, HasDataLayout, PointeeInfo, Size, TargetDataLayout, VariantIdx, + }; ++use rustc_target::spec::TargetTriple; + use rustc_target::spec::{HasTargetSpec, RelocModel, Target, TlsModel}; + use smallvec::SmallVec; + +@@ -203,13 +204,13 @@ pub unsafe fn create_module<'ll>( + // to ensure intrinsic calls don't use it. + if !sess.needs_plt() { + let avoid_plt = c"RtLibUseGOT".as_ptr().cast(); +- llvm::LLVMRustAddModuleFlag(llmod, llvm::LLVMModFlagBehavior::Warning, avoid_plt, 1); ++ llvm::LLVMRustAddModuleFlagU32(llmod, llvm::LLVMModFlagBehavior::Warning, avoid_plt, 1); + } + + // Enable canonical jump tables if CFI is enabled. (See https://reviews.llvm.org/D65629.) + if sess.is_sanitizer_cfi_canonical_jump_tables_enabled() && sess.is_sanitizer_cfi_enabled() { + let canonical_jump_tables = c"CFI Canonical Jump Tables".as_ptr().cast(); +- llvm::LLVMRustAddModuleFlag( ++ llvm::LLVMRustAddModuleFlagU32( + llmod, + llvm::LLVMModFlagBehavior::Override, + canonical_jump_tables, +@@ -220,7 +221,7 @@ pub unsafe fn create_module<'ll>( + // Enable LTO unit splitting if specified or if CFI is enabled. (See https://reviews.llvm.org/D53891.) + if sess.is_split_lto_unit_enabled() || sess.is_sanitizer_cfi_enabled() { + let enable_split_lto_unit = c"EnableSplitLTOUnit".as_ptr().cast(); +- llvm::LLVMRustAddModuleFlag( ++ llvm::LLVMRustAddModuleFlagU32( + llmod, + llvm::LLVMModFlagBehavior::Override, + enable_split_lto_unit, +@@ -231,7 +232,7 @@ pub unsafe fn create_module<'ll>( + // Add "kcfi" module flag if KCFI is enabled. (See https://reviews.llvm.org/D119296.) + if sess.is_sanitizer_kcfi_enabled() { + let kcfi = c"kcfi".as_ptr().cast(); +- llvm::LLVMRustAddModuleFlag(llmod, llvm::LLVMModFlagBehavior::Override, kcfi, 1); ++ llvm::LLVMRustAddModuleFlagU32(llmod, llvm::LLVMModFlagBehavior::Override, kcfi, 1); + } + + // Control Flow Guard is currently only supported by the MSVC linker on Windows. +@@ -240,7 +241,7 @@ pub unsafe fn create_module<'ll>( + CFGuard::Disabled => {} + CFGuard::NoChecks => { + // Set `cfguard=1` module flag to emit metadata only. +- llvm::LLVMRustAddModuleFlag( ++ llvm::LLVMRustAddModuleFlagU32( + llmod, + llvm::LLVMModFlagBehavior::Warning, + c"cfguard".as_ptr() as *const _, +@@ -249,7 +250,7 @@ pub unsafe fn create_module<'ll>( + } + CFGuard::Checks => { + // Set `cfguard=2` module flag to emit metadata and checks. +- llvm::LLVMRustAddModuleFlag( ++ llvm::LLVMRustAddModuleFlagU32( + llmod, + llvm::LLVMModFlagBehavior::Warning, + c"cfguard".as_ptr() as *const _, +@@ -267,26 +268,26 @@ pub unsafe fn create_module<'ll>( + }; + + if sess.target.arch == "aarch64" { +- llvm::LLVMRustAddModuleFlag( ++ llvm::LLVMRustAddModuleFlagU32( + llmod, + behavior, + c"branch-target-enforcement".as_ptr().cast(), + bti.into(), + ); +- llvm::LLVMRustAddModuleFlag( ++ llvm::LLVMRustAddModuleFlagU32( + llmod, + behavior, + c"sign-return-address".as_ptr().cast(), + pac_ret.is_some().into(), + ); + let pac_opts = pac_ret.unwrap_or(PacRet { leaf: false, key: PAuthKey::A }); +- llvm::LLVMRustAddModuleFlag( ++ llvm::LLVMRustAddModuleFlagU32( + llmod, + behavior, + c"sign-return-address-all".as_ptr().cast(), + pac_opts.leaf.into(), + ); +- llvm::LLVMRustAddModuleFlag( ++ llvm::LLVMRustAddModuleFlagU32( + llmod, + behavior, + c"sign-return-address-with-bkey".as_ptr().cast(), +@@ -302,7 +303,7 @@ pub unsafe fn create_module<'ll>( + + // Pass on the control-flow protection flags to LLVM (equivalent to `-fcf-protection` in Clang). + if let CFProtection::Branch | CFProtection::Full = sess.opts.unstable_opts.cf_protection { +- llvm::LLVMRustAddModuleFlag( ++ llvm::LLVMRustAddModuleFlagU32( + llmod, + llvm::LLVMModFlagBehavior::Override, + c"cf-protection-branch".as_ptr().cast(), +@@ -310,7 +311,7 @@ pub unsafe fn create_module<'ll>( + ) + } + if let CFProtection::Return | CFProtection::Full = sess.opts.unstable_opts.cf_protection { +- llvm::LLVMRustAddModuleFlag( ++ llvm::LLVMRustAddModuleFlagU32( + llmod, + llvm::LLVMModFlagBehavior::Override, + c"cf-protection-return".as_ptr().cast(), +@@ -319,7 +320,7 @@ pub unsafe fn create_module<'ll>( + } + + if sess.opts.unstable_opts.virtual_function_elimination { +- llvm::LLVMRustAddModuleFlag( ++ llvm::LLVMRustAddModuleFlagU32( + llmod, + llvm::LLVMModFlagBehavior::Error, + c"Virtual Function Elim".as_ptr().cast(), +@@ -329,7 +330,7 @@ pub unsafe fn create_module<'ll>( + + // Set module flag to enable Windows EHCont Guard (/guard:ehcont). + if sess.opts.unstable_opts.ehcont_guard { +- llvm::LLVMRustAddModuleFlag( ++ llvm::LLVMRustAddModuleFlagU32( + llmod, + llvm::LLVMModFlagBehavior::Warning, + c"ehcontguard".as_ptr() as *const _, +@@ -354,6 +355,24 @@ pub unsafe fn create_module<'ll>( + llvm::LLVMMDNodeInContext(llcx, &name_metadata, 1), + ); + ++ // Embed target-abi into bitcode for rv64gc when plugin-lto is enabled ++ // Relevant: ++ // https://discourse.llvm.org/t/encode-target-abi-into-llvm-bitcode-for-lto/54116 ++ // https://internals.rust-lang.org/t/per-target-llvm-module-flags/12023 ++ if sess.opts.cg.linker_plugin_lto.enabled() { ++ let TargetTriple::TargetTriple(ref triple) = sess.opts.target_triple else { ++ panic!("Unexpected TargetTriple::TargetJson") ++ }; ++ if triple == "riscv64gc-unknown-linux-gnu" { ++ llvm::LLVMRustAddModuleFlagString( ++ llmod, ++ llvm::LLVMModFlagBehavior::Error, ++ c"target-abi".as_ptr() as *const _, ++ c"lp64d".as_ptr() as *const _, ++ ) ++ } ++ } ++ + // Add module flags specified via -Z llvm_module_flag + for (key, value, behavior) in &sess.opts.unstable_opts.llvm_module_flag { + let key = format!("{key}\0"); +@@ -369,7 +388,7 @@ pub unsafe fn create_module<'ll>( + // We already checked this during option parsing + _ => unreachable!(), + }; +- llvm::LLVMRustAddModuleFlag(llmod, behavior, key.as_ptr().cast(), *value) ++ llvm::LLVMRustAddModuleFlagU32(llmod, behavior, key.as_ptr().cast(), *value) + } + + llmod +diff --git a/compiler/rustc_codegen_llvm/src/debuginfo/mod.rs b/compiler/rustc_codegen_llvm/src/debuginfo/mod.rs +index d3a851b40c0a2..4fdaa59e0e559 100644 +--- a/compiler/rustc_codegen_llvm/src/debuginfo/mod.rs ++++ b/compiler/rustc_codegen_llvm/src/debuginfo/mod.rs +@@ -110,7 +110,7 @@ impl<'ll, 'tcx> CodegenUnitDebugContext<'ll, 'tcx> { + .unstable_opts + .dwarf_version + .unwrap_or(sess.target.default_dwarf_version); +- llvm::LLVMRustAddModuleFlag( ++ llvm::LLVMRustAddModuleFlagU32( + self.llmod, + llvm::LLVMModFlagBehavior::Warning, + c"Dwarf Version".as_ptr().cast(), +@@ -118,7 +118,7 @@ impl<'ll, 'tcx> CodegenUnitDebugContext<'ll, 'tcx> { + ); + } else { + // Indicate that we want CodeView debug information on MSVC +- llvm::LLVMRustAddModuleFlag( ++ llvm::LLVMRustAddModuleFlagU32( + self.llmod, + llvm::LLVMModFlagBehavior::Warning, + c"CodeView".as_ptr().cast(), +@@ -127,7 +127,7 @@ impl<'ll, 'tcx> CodegenUnitDebugContext<'ll, 'tcx> { + } + + // Prevent bitcode readers from deleting the debug info. +- llvm::LLVMRustAddModuleFlag( ++ llvm::LLVMRustAddModuleFlagU32( + self.llmod, + llvm::LLVMModFlagBehavior::Warning, + c"Debug Info Version".as_ptr().cast(), +diff --git a/compiler/rustc_codegen_llvm/src/llvm/ffi.rs b/compiler/rustc_codegen_llvm/src/llvm/ffi.rs +index ee73c6b4756f0..4d1fc09c54854 100644 +--- a/compiler/rustc_codegen_llvm/src/llvm/ffi.rs ++++ b/compiler/rustc_codegen_llvm/src/llvm/ffi.rs +@@ -1793,12 +1793,20 @@ extern "C" { + /// + /// In order for Rust-C LTO to work, module flags must be compatible with Clang. What + /// "compatible" means depends on the merge behaviors involved. +- pub fn LLVMRustAddModuleFlag( ++ pub fn LLVMRustAddModuleFlagU32( + M: &Module, + merge_behavior: LLVMModFlagBehavior, + name: *const c_char, + value: u32, + ); ++ ++ pub fn LLVMRustAddModuleFlagString( ++ M: &Module, ++ merge_behavior: LLVMModFlagBehavior, ++ name: *const c_char, ++ value: *const c_char, ++ ); ++ + pub fn LLVMRustHasModuleFlag(M: &Module, name: *const c_char, len: size_t) -> bool; + + pub fn LLVMRustDIBuilderCreate(M: &Module) -> &mut DIBuilder<'_>; +diff --git a/compiler/rustc_codegen_llvm/src/llvm/mod.rs b/compiler/rustc_codegen_llvm/src/llvm/mod.rs +index 4f5cc575da6e5..73cd5e3052413 100644 +--- a/compiler/rustc_codegen_llvm/src/llvm/mod.rs ++++ b/compiler/rustc_codegen_llvm/src/llvm/mod.rs +@@ -324,3 +324,8 @@ impl Drop for OperandBundleDef<'_> { + } + } + } ++ ++pub enum LLVMModFlagValue { ++ String(String), ++ U32(u32), ++} +diff --git a/compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp b/compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp +index 0df7b7eed11f9..7408526f6ebef 100644 +--- a/compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp ++++ b/compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp +@@ -748,7 +748,7 @@ extern "C" uint32_t LLVMRustVersionMinor() { return LLVM_VERSION_MINOR; } + + extern "C" uint32_t LLVMRustVersionMajor() { return LLVM_VERSION_MAJOR; } + +-extern "C" void LLVMRustAddModuleFlag( ++extern "C" void LLVMRustAddModuleFlagU32( + LLVMModuleRef M, + Module::ModFlagBehavior MergeBehavior, + const char *Name, +@@ -756,6 +756,15 @@ extern "C" void LLVMRustAddModuleFlag( + unwrap(M)->addModuleFlag(MergeBehavior, Name, Value); + } + ++extern "C" void LLVMRustAddModuleFlagString( ++ LLVMModuleRef M, ++ Module::ModFlagBehavior MergeBehavior, ++ const char *Name, ++ const char *Value) { ++ llvm::LLVMContext &Ctx = unwrap(M)->getContext(); ++ unwrap(M)->addModuleFlag(MergeBehavior, Name, llvm::MDString::get(Ctx, Value)); ++} ++ + extern "C" bool LLVMRustHasModuleFlag(LLVMModuleRef M, const char *Name, + size_t Len) { + return unwrap(M)->getModuleFlag(StringRef(Name, Len)) != nullptr; diff --git a/rust.spec b/rust.spec index 170dc5b3e22138066b2ac434f5e401ee387bfacb..7533b92c53fa9de5e26e583dec8b6bb5c4e0f61d 100644 --- a/rust.spec +++ b/rust.spec @@ -11,7 +11,7 @@ Name: rust Version: 1.77.0 -Release: 1 +Release: 2 Summary: The Rust Programming Language License: Apache-2.0 OR MIT URL: https://www.rust-lang.org @@ -29,6 +29,10 @@ Patch0001: 0001-Use-lld-provided-by-system.patch # Set a substitute-path in rust-gdb for standard library sources. Patch0002: rustc-1.70.0-rust-gdb-substitute-path.patch +%ifarch riscv64 +Patch1000: embed-riscv64-target-abi-in-bc-when-plugin-_-lto-is-used.patch +%endif + %{lua: function rust_triple(arch) local abi = "gnu" if arch == "armv7hl" then @@ -111,6 +115,8 @@ BuildRequires: %{llvm}-static libffi-devel %endif BuildRequires: procps-ng BuildRequires: ninja-build +BuildRequires: compiler-rt +BuildRequires: clang Provides: rustc = %{version}-%{release} Provides: rustc%{?_isa} = %{version}-%{release} Requires: %{name}-std-static%{?_isa} = %{version}-%{release} @@ -254,6 +260,9 @@ sed -i.try-python -e '/^try python3 /i try "%{python}" "$@"' ./configure %endif %patch -P 0001 -p1 %patch -P 0002 -p1 +%ifarch riscv64 +%patch -P 1000 -p1 +%endif rm -rf vendor/curl-sys*/curl/ rm -rf vendor/jemalloc-sys/jemalloc/ rm -rf vendor/libffi-sys*/libffi/ @@ -306,10 +315,26 @@ max_cpus=$(( ($(free -g | awk '/^Mem:/{print $2}') + 1) / 2 )) if [ "$max_cpus" -ge 1 -a "$max_cpus" -lt "$ncpus" ]; then ncpus="$max_cpus" fi +# Find the compiler-rt library for the Rust profiler_builtins crate. +# But there are two versions in openEuler. Why? +# We don't have macros.clang so we need clang version here +# This is for avoiding rpm syntax error +%global clang_maj_ver 17 +%if %{?clang_maj_ver} >= 17 +# This is the new one, used on openEuler 24.03 LTS or later +%define profiler %(echo %{_prefix}/%{_lib}/clang/%{clang_maj_ver}/lib/%{_arch}-openEuler-linux-gnu/libclang_rt.profile.a) +%else +# This is used before openEuler 23.09 +%global clang_full_ver %%(clang --version | awk '/clang version/{print $3}') +%define profiler %(echo %{_prefix}/%{_lib}/clang/%{clang_full_ver}/lib/libclang_rt.profile-%{_arch}.a) +%endif +test -r "%{profiler}" + %configure --disable-option-checking \ --libdir=%{common_libdir} \ %{rust_musl_root}=%{musl_root} \ --build=%{rust_triple} --host=%{rust_triple} --target=%{rust_triple},%{rust_musl_triple} \ + --set target.%{rust_triple}.profiler="%{profiler}" \ --python=%{python} \ --local-rust-root=%{local_rust_root} \ %{!?with_bundled_llvm: --llvm-root=%{llvm_root} \ @@ -484,6 +509,9 @@ export %{rust_env} %{_mandir}/man1/cargo*.1* %changelog +* Thu Apr 11 2024 misaka00251 - 1.77.0-2 +- Enable profiler builtin + * Wed Apr 03 2024 wangkai <13474090681@163.com> - 1.77.0-1 - Update to 1.77.0