From 4ebec9d0d2b7c25cf3b762faf2b89437f48ea08a Mon Sep 17 00:00:00 2001 From: "Eddy (Eduard) Stefes" Date: Tue, 10 Feb 2026 09:18:07 +0100 Subject: [PATCH] add rustc option -Zpacked-stack this enables packed-stack just as -mpacked-stack in clang and gcc. packed-stack is needed on s390x for kernel development. --- compiler/rustc_codegen_llvm/src/attributes.rs | 15 +++++++++++++++ compiler/rustc_session/src/errors.rs | 4 ++++ compiler/rustc_session/src/options.rs | 2 ++ compiler/rustc_session/src/session.rs | 6 ++++++ compiler/rustc_target/src/spec/mod.rs | 10 ++++++++++ src/doc/rustc/src/codegen-options/index.md | 5 +++++ 6 files changed, 42 insertions(+) diff --git a/compiler/rustc_codegen_llvm/src/attributes.rs b/compiler/rustc_codegen_llvm/src/attributes.rs index f5eb9c10db996..c2d349ac567e1 100644 --- a/compiler/rustc_codegen_llvm/src/attributes.rs +++ b/compiler/rustc_codegen_llvm/src/attributes.rs @@ -312,6 +312,18 @@ fn backchain_attr<'ll>(cx: &SimpleCx<'ll>, sess: &Session) -> Option<&'ll Attrib if found_positive { Some(llvm::CreateAttrString(cx.llcx, "backchain")) } else { None } } +fn packed_stack_attr<'ll>(cx: &SimpleCx<'ll>, sess: &Session) -> Option<&'ll Attribute> { + if sess.target.arch != Arch::S390x { + return None; + } + + if sess.opts.unstable_opts.packed_stack { + Some(llvm::CreateAttrString(cx.llcx, "packed-stack")) + } else { + None + } +} + pub(crate) fn target_cpu_attr<'ll>(cx: &SimpleCx<'ll>, sess: &Session) -> &'ll Attribute { let target_cpu = llvm_util::target_cpu(sess); llvm::CreateAttrStringValue(cx.llcx, "target-cpu", target_cpu) @@ -533,6 +545,9 @@ pub(crate) fn llfn_attrs_from_instance<'ll, 'tcx>( if let Some(backchain) = backchain_attr(cx, sess) { to_add.push(backchain); } + if let Some(packed_stack) = packed_stack_attr(cx, sess) { + to_add.push(packed_stack); + } to_add.extend(patchable_function_entry_attrs( cx, sess, diff --git a/compiler/rustc_session/src/errors.rs b/compiler/rustc_session/src/errors.rs index 0c6a33f228082..c8aa1130e66c3 100644 --- a/compiler/rustc_session/src/errors.rs +++ b/compiler/rustc_session/src/errors.rs @@ -537,3 +537,7 @@ pub(crate) struct UnexpectedBuiltinCfg { pub(crate) cfg_name: Symbol, pub(crate) controlled_by: &'static str, } + +#[derive(Diagnostic)] +#[diag("`-Zpacked-stack` is only supported on s390x")] +pub(crate) struct UnsupportedPackedStack; diff --git a/compiler/rustc_session/src/options.rs b/compiler/rustc_session/src/options.rs index 9219b5a7e8aca..790a5af6344d4 100644 --- a/compiler/rustc_session/src/options.rs +++ b/compiler/rustc_session/src/options.rs @@ -2536,6 +2536,8 @@ options! { "pass `-install_name @rpath/...` to the macOS linker (default: no)"), packed_bundled_libs: bool = (false, parse_bool, [TRACKED], "change rlib format to store native libraries as archives"), + packed_stack: bool = (false, parse_bool, [TRACKED], + "use packed stack frames (s390x only) (default: no)"), panic_abort_tests: bool = (false, parse_bool, [TRACKED], "support compiling tests with panic=abort (default: no)"), panic_in_drop: PanicStrategy = (PanicStrategy::Unwind, parse_panic_strategy, [TRACKED], diff --git a/compiler/rustc_session/src/session.rs b/compiler/rustc_session/src/session.rs index ed37e9e960cd8..0706bad9fac8e 100644 --- a/compiler/rustc_session/src/session.rs +++ b/compiler/rustc_session/src/session.rs @@ -1348,6 +1348,12 @@ fn validate_commandline_args_with_session_available(sess: &Session) { sess.dcx().emit_warn(errors::SoftFloatIgnored); } } + + if sess.opts.unstable_opts.packed_stack { + if sess.target.arch != Arch::S390x { + sess.dcx().emit_err(errors::UnsupportedPackedStack); + } + } } /// Holds data on the current incremental compilation session, if there is one. diff --git a/compiler/rustc_target/src/spec/mod.rs b/compiler/rustc_target/src/spec/mod.rs index 537185f536ab1..f995e4d10eba6 100644 --- a/compiler/rustc_target/src/spec/mod.rs +++ b/compiler/rustc_target/src/spec/mod.rs @@ -3258,6 +3258,16 @@ impl Target { )); } } + // If both `packed-stack` and `backchain` are set we need to use soft-float ABI. + if self.arch == Arch::S390x + && features_enabled.contains("packed-stack") + && features_enabled.contains("backchain") + && !matches!(self.abi, Abi::SoftFloat) + { + return Err(format!( + "Enabling both `packed-stack` and `backchain` attributes is incompatible with the default s390x target. Switch to s390x-unknown-none-softfloat if you need both attributes." + )); + } } Ok(()) diff --git a/src/doc/rustc/src/codegen-options/index.md b/src/doc/rustc/src/codegen-options/index.md index f0f991ed0c909..f10fc5118e40f 100644 --- a/src/doc/rustc/src/codegen-options/index.md +++ b/src/doc/rustc/src/codegen-options/index.md @@ -487,6 +487,11 @@ one of the following values: If not specified, overflow checks are enabled if [debug-assertions](#debug-assertions) are enabled, disabled otherwise. +## packed-stack + +This flag enables packed StackFrames on s390x. Enabling both `packed-stack` and `backchain` attributes is incompatible +with the default s390x target. Switch to s390x-unknown-none-softfloat if you need both attributes. + ## panic This option lets you control what happens when the code panics.