Skip to content

Add linker_static_archive_order lint for -Clink-arg archives placed before referenced dynamic libs - #160201

Open
cezarbbb wants to merge 1 commit into
rust-lang:mainfrom
cezarbbb:link-arg-static-archive-lint
Open

Add linker_static_archive_order lint for -Clink-arg archives placed before referenced dynamic libs#160201
cezarbbb wants to merge 1 commit into
rust-lang:mainfrom
cezarbbb:link-arg-static-archive-lint

Conversation

@cezarbbb

@cezarbbb cezarbbb commented Jul 30, 2026

Copy link
Copy Markdown
Contributor

See issue #154975

When a static archive is passed via -Clink-arg, rustc appends it after its own native libraries. With --as-needed, strict left-to-right linkers like GNU ld.bfd (unlike lld, which tolerates back-references) can then drop a dynamic library that only that archive references, leaving unresolved symbols.

rustc can't tell whether the back-reference actually exists without resolving the archive's symbols, so this is a heuristic lint rather than a fix to the ordering itself. It's Allow by default and carries ignore_deny_warnings, so it stays silent unless explicitly requested with -Wlinker-static-archive-order, and -D warnings won't promote a possible false positive into an error — only -Dlinker-static-archive-order will.

The lint fires for GNU ld-family linkers that don't use lld (bare ld or cc-driven), excluding Windows and Darwin where the --as-needed model doesn't apply. The diagnostic points at three workarounds: -l static=, -Zpre-link-arg=, and wrapping the dynamic library in --no-as-needed/--as-needed.

r? @bjorn3

@rustbot rustbot added A-run-make Area: port run-make Makefiles to rmake.rs 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 Jul 30, 2026
@bjorn3

bjorn3 commented Jul 30, 2026

Copy link
Copy Markdown
Member

I think we should only point to -l static. -Zpre-link-args is unstable and --no-as-needed wouldn't help with for exanple fixing libc references when libc is statically linked. Maybe we can emit it even with lld? That way you don't suddenly get surprises if you have to disable the lld default in the future for whatever reason.

Comment thread compiler/rustc_codegen_ssa/src/back/link.rs Outdated
@cezarbbb

Copy link
Copy Markdown
Contributor Author

Maybe we can emit it even with lld? That way you don't suddenly get surprises if you have to disable the lld default in the future for whatever reason.

That makes sense. I'll do it.

@cezarbbb
cezarbbb force-pushed the link-arg-static-archive-lint branch from d73f481 to e5596e2 Compare July 30, 2026 09:28
@rustbot

This comment has been minimized.

@rust-bors

This comment has been minimized.

@cezarbbb
cezarbbb force-pushed the link-arg-static-archive-lint branch from e5596e2 to e723d7a Compare August 3, 2026 01:21
@rustbot

rustbot commented Aug 3, 2026

Copy link
Copy Markdown
Collaborator

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.

/// The `linker_static_archive_order` lint detects when a static archive (`.a`/`.o`) passed via
/// `-Clink-arg` is placed *before* a dynamic library that it references, which can cause the
/// dynamic library to be dropped under `--as-needed` with linkers that resolve symbols strictly
/// left-to-right (such as GNU `ld.bfd`).

@bjorn3 bjorn3 Aug 11, 2026

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.

Passing static libraries using -Clink-arg rather than -l feels iffy to me even ignoring the symbol resolution order issue. Maybe put less emphasis on the exact issue and instead say that -Clink-arg for static libraries is not idiomatic and what way should be used instead. You can leave a note about -Clink-arg playing badly with symbol resolution order as a concrete example about why you should avoid it.

View changes since the review

// The lint is heuristic: rustc can't know whether a back-reference actually exists, so the
// diagnostic may fire on link orders that link successfully. Prevent `-D warnings` from
// turning a possible false positive into a hard error. `-D linker-static-archive-order` still
// applies.

@bjorn3 bjorn3 Aug 11, 2026

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.

Even if it isn't strictly needed, I think the lint should still fire.

View changes since the review

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

Labels

A-run-make Area: port run-make Makefiles to rmake.rs 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.

3 participants