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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

+1, although it's a bit of a complication to the code, perhaps we can do everything else and benchmark first.

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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Yes, we should open and parse the rlib once, and read the digest from already opened and parsed rlib.

@petrochenkov petrochenkov self-assigned this Apr 6, 2026

for m in &compiled_modules.modules {
if let Some(obj) = m.object.as_ref() {
if let Some(name) = obj.file_name().and_then(|n| n.to_str()) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

add_file uses unwraps, so they can be used here too.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Ok!

// Don't include Rust objects if LTO is enabled
if lto && looks_like_rust_object_file(fname) {
// Ignore the rlib digest file.
if fname == rlib_digest::FILENAME {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
if fname == rlib_digest::FILENAME {
if fname == METADATA_FILENAME || fname == rlib_digest::FILENAME {


use super::metadata::search_for_section;

pub(crate) const FILENAME: &str = "lib.rlib-digest";
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Bikeshed: we need a good "official" name for this.
If the regular metadata is lib.rmeta, then perhaps this can be called lib.rmeta-link, or lib.rmeta-late, or something.

@petrochenkov petrochenkov added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Apr 7, 2026
@mehdiakiki
Copy link
Copy Markdown
Contributor Author

Will address the review comments by tomorrow!

@rust-log-analyzer

This comment has been minimized.

// Don't include Rust objects if LTO is enabled
if lto && looks_like_rust_object_file(fname) {
// Don't include Rust objects if LTO is enabled.
if lto && digest.rust_object_files.iter().any(|f| f == fname) {
Copy link
Copy Markdown
Member

@bjorn3 bjorn3 Apr 9, 2026

Choose a reason for hiding this comment

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

View changes since the review

This also needs to be done in add_static_crate, right? And then the looks_like_rust_object_file function can be removed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. 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.

7 participants