Add linker_static_archive_order lint for -Clink-arg archives placed before referenced dynamic libs - #160201
Add linker_static_archive_order lint for -Clink-arg archives placed before referenced dynamic libs#160201cezarbbb wants to merge 1 commit into
linker_static_archive_order lint for -Clink-arg archives placed before referenced dynamic libs#160201Conversation
|
I think we should only point to |
That makes sense. I'll do it. |
d73f481 to
e5596e2
Compare
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
e5596e2 to
e723d7a
Compare
|
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`). |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Rewrote the lint docs to lead with -Clink-arg not being the idiomatic way to pass static libraries and -l static= being the alternative, with the ordering issue kept as a concrete example.
| // 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. |
There was a problem hiding this comment.
Even if it isn't strictly needed, I think the lint should still fire.
…ed before referenced dynamic libs
e723d7a to
f25daf3
Compare
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 GNUld.bfd(unlikelld, 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 warningswon't promote a possible false positive into an error — only-Dlinker-static-archive-orderwill.The lint fires for GNU
ld-family linkers that don't uselld(bareldorcc-driven), excluding Windows and Darwin where the--as-neededmodel 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