Skip to content

Conversation

a4lg
Copy link
Contributor

@a4lg a4lg commented Aug 25, 2025

This change implements more conformant, more comprehensive RISC-V ELF flags handling when generating certain object files directly from rustc.

  • Use "zca" instead of "c"
    The "Zca" extension (a subset of "C") is the minimal configuration for compressed instructions to set EF_RISCV_RVC flag.
  • Set TSO flag from "ztso"
    The "Ztso" extension denotes that the program depends on the RVTSO (Total Store Ordering) memory consistency model, which is stronger than the standard RVWMO (Weak Memory Ordering) consistency model and on ELF targets, we need to set EF_RISCV_TSO flag.

@rustbot label +O-riscv

@rustbot
Copy link
Collaborator

rustbot commented Aug 25, 2025

r? @lcnr

rustbot has assigned @lcnr.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Aug 25, 2025
@rustbot
Copy link
Collaborator

rustbot commented Aug 25, 2025

Some changes occurred in compiler/rustc_codegen_ssa

cc @WaffleLapkin

@rustbot rustbot added the O-riscv Target: RISC-V architecture label Aug 25, 2025
@WaffleLapkin
Copy link
Member

Is zca implied by c? i.e. does this preserve the behavior for code that has been using c?

@WaffleLapkin
Copy link
Member

And are these elf flags documented anywhere? (I would like to learn more about the context here)

@a4lg
Copy link
Contributor Author

a4lg commented Aug 25, 2025

Is zca implied by c? i.e. does this preserve the behavior for code that has been using c?

Yes. I tested so that targets only with c enables EF_RISCV_RVC (suggesting zca is implied by c).

And from compiler/rustc_target/src/target_features.rs:

    ("c", Stable, &["zca"]),

Let me give some time to answer your second question.

@a4lg
Copy link
Contributor Author

a4lg commented Aug 26, 2025

@WaffleLapkin

And are these elf flags documented anywhere? (I would like to learn more about the context here)

I think following quotes from two documents should be sufficient (as a supplement, I show that EF_RISCV_TSO is indeed set if the Ztso extension is enabled in both GNU/LLVM toolchains).

RISC-V ELF Specification

EF_RISCV_TSO (0x0010)
This bit is set when the binary requires the RVTSO memory consistency model.

RISC-V ISA Manual (version 20240411)

Chapter 18. "Ztso" Extension for Total Store Ordering, Version 1.0

This chapter defines the "Ztso" extension for the RISC-V Total Store Ordering (RVTSO) memory consistency model.

and (bold range is marked by me):

In spite of the fact that Ztso adds no new instructions to the ISA, code written assuming RVTSO will not run correctly on implementations not supporting Ztso.
Binaries compiled to run only under Ztso should indicate as such via a flag in the binary, so that platforms which do not implement Ztso can simply refuse to run them.

GNU Binutils

I show EF_RISCV_TSO is set when the Ztso extension is enabled.

gas/config/tc-riscv.c: riscv_set_arch

  if (riscv_subset_supports (&riscv_rps_as, "ztso"))
    riscv_set_tso ();

gas/config/tc-riscv.c: riscv_set_tso

static void
riscv_set_tso (void)
{
  elf_flags |= EF_RISCV_TSO;
}

LLVM

I show EF_RISCV_TSO is set when the Ztso extension is enabled and indirectly show EF_RISCV_RVC is set when the Zca extension is enabled.

llvm/lib/Target/RISCV/MCTargetDesc/RISCVELFStreamer.cpp: RISCVTargetELFStreamer::finish()

  if (hasTSO())
    EFlags |= ELF::EF_RISCV_TSO;

llvm/lib/Target/RISCV/MCTargetDesc/RISCVTargetStreamer.h: RISCVTargetStreamer::hasTSO()

  bool hasTSO() const { return HasTSO; }

llvm/lib/Target/RISCV/MCTargetDesc/RISCVTargetStreamer.cpp: RISCVTargetStreamer::setFlagsFromFeatures(const MCSubtargetInfo&)

void RISCVTargetStreamer::setFlagsFromFeatures(const MCSubtargetInfo &STI) {
  HasRVC = STI.hasFeature(RISCV::FeatureStdExtZca);
  HasTSO = STI.hasFeature(RISCV::FeatureStdExtZtso);
}

a4lg added 2 commits August 26, 2025 03:42
This commit implements more conformant, more comprehensive RISC-V ELF
flags handling when generating certain object files directly from rustc.

*   Use "zca" instead of "c"
    The "Zca" extension (a subset of "C") is the minimal configuration
    for compressed instructions to set `EF_RISCV_RVC` flag.
*   Set TSO flag from "ztso"
    The "Ztso" extension denotes that the program depends on the RVTSO
    (Total Store Ordering) memory consistency model, which is stronger
    than the standard RVWMO (Weak Memory Ordering) consistency model and
    on ELF targets, we need to set `EF_RISCV_TSO` flag.
@a4lg a4lg force-pushed the riscv-elf-flags-for-internal-objs branch from 5bcf295 to cb8c905 Compare August 26, 2025 03:45
@rustbot
Copy link
Collaborator

rustbot commented Aug 26, 2025

This PR was rebased onto a different master commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

@a4lg
Copy link
Contributor Author

a4lg commented Aug 26, 2025

Made a rebase along with a few commit message fixes (no code changes).

@a4lg
Copy link
Contributor Author

a4lg commented Aug 26, 2025

For instance, this change affects ELF flags of lib.rmeta when a crate is built as an rlib.

// src/lib.rs
// (no_core empty library to generate lib.rmeta in minimum effort (without adding targets to compile))
#![feature(no_core)]
#![no_core]
# Cargo.toml
[package]
name = "test"
version = "0.0.0"
edition = "2024"

[lib]
crate-type = ["rlib"]
cargo clean
# Test TSO flag
RUSTFLAGS='-Ctarget-feature=+ztso' cargo build --target riscv32imafc-unknown-none-elf
# # Test RVC flag
# RUSTFLAGS='-Ctarget-feature=+zca' cargo build --target riscv32ima-unknown-none-elf
# Extract `lib*.rlib` on the target directory (removing temporary `*.o`).
rm *.o; ar x target/riscv32*-unknown-none-elf/debug/libtest.rlib

Before this PR

$ readelf -h *.o
...
  Flags:                             0x13, RVC, TSO, single-float ABI
...
$ readelf -h lib.rmeta
...
  # Flags not matching with *.o (emitted by LLVM)
  Flags:                             0x3, RVC, single-float ABI
...

After this PR

$ readelf -h *.o
...
  Flags:                             0x13, RVC, TSO, single-float ABI
...
$ readelf -h lib.rmeta
...
  # Flags match with *.o (emitted by LLVM)
  Flags:                             0x13, RVC, TSO, single-float ABI
...

@lcnr
Copy link
Contributor

lcnr commented Aug 26, 2025

r? @WaffleLapkin maybe :3

@rustbot rustbot assigned WaffleLapkin and unassigned lcnr Aug 26, 2025
@rustbot
Copy link
Collaborator

rustbot commented Aug 26, 2025

WaffleLapkin is not on the review rotation at the moment.
They may take a while to respond.

Copy link
Member

@WaffleLapkin WaffleLapkin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@a4lg thanks for extensive references (and the PR)!

View changes since this review

@WaffleLapkin
Copy link
Member

@bors r+ rollup

@bors
Copy link
Collaborator

bors commented Aug 26, 2025

📌 Commit cb8c905 has been approved by WaffleLapkin

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Aug 26, 2025
bors added a commit that referenced this pull request Aug 26, 2025
Rollup of 9 pull requests

Successful merges:

 - #144499 (ci: Begin running ui tests with `rust.debuginfo-level-tests=1`)
 - #145790 (Improve dist for gnullvm hosts)
 - #145792 (Use attribute name in message for "outer attr used as inner attr" errors)
 - #145840 (rustc_codegen_ssa: More comprehensive RISC-V ELF flags)
 - #145876 (Enable building/disting standard library in stage 0)
 - #145887 (bootstrap: Don't panic if codegen-backends is set to empty)
 - #145888 (platform-support: Fix LoongArch32 host column)
 - #145892 (add a flag to codegen fn attrs for foreign items)
 - #145901 (Fix typo in comment of library/alloc/src/raw_vec/mod.rs)

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit ac7f423 into rust-lang:master Aug 27, 2025
10 checks passed
@rustbot rustbot added this to the 1.91.0 milestone Aug 27, 2025
rust-timer added a commit that referenced this pull request Aug 27, 2025
Rollup merge of #145840 - a4lg:riscv-elf-flags-for-internal-objs, r=WaffleLapkin

rustc_codegen_ssa: More comprehensive RISC-V ELF flags

This change implements more conformant, more comprehensive RISC-V ELF flags handling when generating certain object files directly from rustc.

*   Use `"zca"` instead of `"c"`
    The "Zca" extension (a subset of "C") is the minimal configuration for compressed instructions to set `EF_RISCV_RVC` flag.
*   Set TSO flag from `"ztso"`
    The "Ztso" extension denotes that the program depends on the RVTSO (Total Store Ordering) memory consistency model, which is stronger than the standard RVWMO (Weak Memory Ordering) consistency model and on ELF targets, we need to set `EF_RISCV_TSO` flag.
github-actions bot pushed a commit to rust-lang/miri that referenced this pull request Aug 27, 2025
Rollup of 9 pull requests

Successful merges:

 - rust-lang/rust#144499 (ci: Begin running ui tests with `rust.debuginfo-level-tests=1`)
 - rust-lang/rust#145790 (Improve dist for gnullvm hosts)
 - rust-lang/rust#145792 (Use attribute name in message for "outer attr used as inner attr" errors)
 - rust-lang/rust#145840 (rustc_codegen_ssa: More comprehensive RISC-V ELF flags)
 - rust-lang/rust#145876 (Enable building/disting standard library in stage 0)
 - rust-lang/rust#145887 (bootstrap: Don't panic if codegen-backends is set to empty)
 - rust-lang/rust#145888 (platform-support: Fix LoongArch32 host column)
 - rust-lang/rust#145892 (add a flag to codegen fn attrs for foreign items)
 - rust-lang/rust#145901 (Fix typo in comment of library/alloc/src/raw_vec/mod.rs)

r? `@ghost`
`@rustbot` modify labels: rollup
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
O-riscv Target: RISC-V architecture S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants