Skip to content

Add rlib digest to identify Rust object files#154861

Open
mehdiakiki wants to merge 1 commit intorust-lang:mainfrom
mehdiakiki:fix/rlib-digest
Open

Add rlib digest to identify Rust object files#154861
mehdiakiki wants to merge 1 commit intorust-lang:mainfrom
mehdiakiki:fix/rlib-digest

Conversation

@mehdiakiki
Copy link
Copy Markdown
Contributor

@mehdiakiki mehdiakiki commented Apr 6, 2026

This adds a metadata entry to rlib archives that lists which members are Rust object files instead of relying on the filename heuristic in looks_like_rust_object.file. I also added a fallback to the old behavior for rlibs built by older compilers.

Part of #138243.

@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 Apr 6, 2026
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Apr 6, 2026

r? @mati865

rustbot has assigned @mati865.
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

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: codegen, compiler
  • codegen, compiler expanded to 69 candidates
  • Random selection from 11 candidates

@rustbot

This comment has been minimized.

@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Apr 6, 2026

This PR was rebased onto a different main 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.

@mati865
Copy link
Copy Markdown
Member

mati865 commented Apr 6, 2026

Got too much on my plate already.

@rustbot reroll

@rustbot rustbot assigned chenyukang and unassigned mati865 Apr 6, 2026
create_wrapper_file(sess, rlib_digest::SECTION.to_string(), &digest_data);
let digest_file = emit_wrapper_file(sess, &wrapper, tmpdir.as_ref(), rlib_digest::FILENAME);
ab.add_file(&digest_file);
}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Can this be added right after the crate metadata instead? That saves time iterating over the rlib at staticlib build time.

pub(crate) fn read(rlib_path: &Path) -> Option<RlibDigest> {
let file = File::open(rlib_path).ok()?;
let mmap = unsafe { Mmap::map(file).ok()? };
let archive = ArchiveFile::parse(&*mmap).ok()?;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Ideally this mmap would be combined with the one in add_archive.

if lto {
let is_rust_object = match &digest {
Some(d) => d.rust_object_files.iter().any(|f| f == fname),
None => looks_like_rust_object_file(fname),
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Why is this fallback necessary? Wouldn't all rlibs contain the digest?


pub(crate) const FILENAME: &str = "lib.rlib-digest";
pub(crate) const SECTION: &str = ".rlib-digest";
const VERSION: u8 = 1;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This version is not necessary. By the time the linker code is reached, we already know that the rustc version as embedded in the crate metadata matches exactly.

@petrochenkov petrochenkov self-assigned this Apr 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants