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

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

This comment has been minimized.

Comment thread compiler/rustc_lint_defs/src/builtin.rs Outdated
Comment thread compiler/rustc_lint_defs/src/builtin.rs
@cezarbbb
cezarbbb force-pushed the link-arg-static-archive-lint branch from e723d7a to f25daf3 Compare August 12, 2026 01:45
@rust-bors

This comment has been minimized.

@cezarbbb
cezarbbb force-pushed the link-arg-static-archive-lint branch from f25daf3 to c2aee12 Compare August 28, 2026 08:52
@rustbot

rustbot commented Aug 28, 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.

@cezarbbb

Copy link
Copy Markdown
Contributor Author

Hi @bjorn3, I wanted to ask if you have any further concerns regarding this PR? I will actively follow up on it.

This branch has not been deployed

No deployments
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