-
-
Notifications
You must be signed in to change notification settings - Fork 15.4k
Add linker_static_archive_order lint for -Clink-arg archives placed before referenced dynamic libs
#160201
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
cezarbbb
wants to merge
1
commit into
rust-lang:main
Choose a base branch
from
cezarbbb:link-arg-static-archive-lint
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Add linker_static_archive_order lint for -Clink-arg archives placed before referenced dynamic libs
#160201
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -66,6 +66,7 @@ pub mod hardwired { | |
| LEGACY_DERIVE_HELPERS, | ||
| LINKER_INFO, | ||
| LINKER_MESSAGES, | ||
| LINKER_STATIC_ARCHIVE_ORDER, | ||
| LONG_RUNNING_CONST_EVAL, | ||
| MACRO_EXPANDED_MACRO_EXPORTS_ACCESSED_BY_ABSOLUTE_PATHS, | ||
| MACRO_USE_EXTERN_CRATE, | ||
|
|
@@ -4089,6 +4090,44 @@ declare_lint! { | |
| "linker warnings known to be informational-only and not indicative of a problem" | ||
| } | ||
|
|
||
| declare_lint! { | ||
| /// 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`). | ||
| /// | ||
| /// ### Example | ||
| /// | ||
| /// ```rust,ignore (needs CLI args, platform-specific) | ||
| /// // libfoo.a references symbols from libm.so; built with the GNU `ld.bfd` linker: | ||
| /// // rustc -Clink-arg=/path/to/libfoo.a -Clink-arg=-lm foo.rs | ||
| /// fn main() {} | ||
| /// ``` | ||
| /// | ||
| /// rustc appends `-Clink-arg` arguments after its own native libraries, so `-lm` lands after | ||
| /// `libfoo.a`. With `--as-needed`, GNU `ld.bfd` sees that nothing references `-lm` at the point | ||
| /// it appears (the reference lives inside `libfoo.a` to the left), so `-lm` is dropped, leading | ||
| /// to unresolved symbols. `lld` tolerates this because it back-references, masking the problem; | ||
| /// the lint still fires there so the latent issue surfaces before a switch back to `ld.bfd`. | ||
| /// | ||
| /// ### Explanation | ||
| /// | ||
| /// This lint flags the combination so users can use `-l static=` to place the archive with the | ||
| /// other native libraries. It is allowed by default because rustc cannot, without symbol | ||
| /// resolution, know whether the archive actually back-references the later dynamic library. See | ||
| /// this issue for more details: | ||
| /// <https://github.com/rust-lang/rust/issues/154975>. | ||
| pub LINKER_STATIC_ARCHIVE_ORDER, | ||
| Allow, | ||
| "static archive passed via `-Clink-arg` may be ordered before a dynamic library it references, \ | ||
| which `--as-needed` linkers such as GNU `ld.bfd` can then drop", | ||
| // 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. | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
| ignore_deny_warnings | ||
| } | ||
|
|
||
| declare_lint! { | ||
| /// The `named_arguments_used_positionally` lint detects cases where named arguments are only | ||
| /// used positionally in format strings. This usage is valid but potentially very confusing. | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| fn main() {} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,117 @@ | ||
| // Tests for the `linker_static_archive_order` lint. | ||
| // See <https://github.com/rust-lang/rust/issues/154975>. | ||
|
|
||
| //@ only-linux | ||
| // The lint applies to GNU `ld`-family linkers (including `lld`) on non-Windows, non-Darwin. | ||
|
|
||
| use run_make_support::{cwd, is_darwin, is_windows, rustc}; | ||
|
|
||
| fn main() { | ||
| // The diagnostic carries the full archive path, so assert on the stable filename substring. | ||
| let archive = cwd().join("libdoesnotexist.a"); | ||
| let archive_str = archive.to_str().unwrap(); | ||
|
|
||
| // 1. A `.a` path via `-Clink-arg` on a bare `ld` flavor fires the lint. | ||
| rustc() | ||
| .input("empty.rs") | ||
| .linker_flavor("ld") | ||
| .link_arg(archive_str) | ||
| .arg("-Wlinker-static-archive-order") | ||
| .print("link-args") | ||
| .run_unchecked() | ||
| .assert_stderr_contains("warning: static archive") | ||
| .assert_stderr_contains("libdoesnotexist.a"); | ||
|
|
||
| // 2. A `.o` path is flagged the same way. | ||
| let object = cwd().join("doesnotexist.o"); | ||
| rustc() | ||
| .input("empty.rs") | ||
| .linker_flavor("ld") | ||
| .link_arg(object.to_str().unwrap()) | ||
| .arg("-Wlinker-static-archive-order") | ||
| .print("link-args") | ||
| .run_unchecked() | ||
| .assert_stderr_contains("warning: static archive") | ||
| .assert_stderr_contains("doesnotexist.o"); | ||
|
|
||
| // 3. A non-archive arg like `-lm` does not fire. | ||
| rustc() | ||
| .input("empty.rs") | ||
| .linker_flavor("ld") | ||
| .link_arg("-lm") | ||
| .arg("-Wlinker-static-archive-order") | ||
| .print("link-args") | ||
| .run_unchecked() | ||
| .assert_stderr_not_contains("linker_static_archive_order"); | ||
|
|
||
| // 4. `Allow` by default: no `-W`, no output. | ||
| rustc() | ||
| .input("empty.rs") | ||
| .linker_flavor("ld") | ||
| .link_arg(archive_str) | ||
| .print("link-args") | ||
| .run_unchecked() | ||
| .assert_stderr_not_contains("linker_static_archive_order"); | ||
|
|
||
| // 5. `ignore_deny_warnings`: `-Dwarnings` does not promote the `-W` to an error. | ||
| rustc() | ||
| .input("empty.rs") | ||
| .linker_flavor("ld") | ||
| .link_arg(archive_str) | ||
| .arg("-Wlinker-static-archive-order") | ||
| .arg("-Dwarnings") | ||
| .print("link-args") | ||
| .run_unchecked() | ||
| .assert_stderr_contains("warning: static archive") | ||
| .assert_stderr_contains("the `linker_static_archive_order` lint ignores `-D warnings`"); | ||
|
|
||
| // 6. `-Dlinker-static-archive-order` (specific) does promote to an error. | ||
| rustc() | ||
| .input("empty.rs") | ||
| .linker_flavor("ld") | ||
| .link_arg(archive_str) | ||
| .arg("-Dlinker-static-archive-order") | ||
| .print("link-args") | ||
| .run_fail() | ||
| .assert_stderr_contains("error: static archive") | ||
| .assert_stderr_contains("libdoesnotexist.a"); | ||
|
|
||
| // 7. `#![allow]` suppresses even an explicit `-W`. | ||
| rustc() | ||
| .input("-") | ||
| .linker_flavor("ld") | ||
| .link_arg(archive_str) | ||
| .arg("-Wlinker-static-archive-order") | ||
| .arg("--crate-type=lib") | ||
| .stdin_buf("#![allow(linker_static_archive_order)] fn main() {}") | ||
| .print("link-args") | ||
| .run_unchecked() | ||
| .assert_stderr_not_contains("static archive"); | ||
|
|
||
| // 8. The `gnu-cc` flavor (cc-driven, `Gnu(Cc::Yes, Lld::No)`) also fires: it uses real `ld` | ||
| // behind `cc` and the same `--as-needed` + left-to-right interaction applies. | ||
| rustc() | ||
| .input("empty.rs") | ||
| .arg("-Zunstable-options") | ||
| .linker_flavor("gnu-cc") | ||
| .link_arg(archive_str) | ||
| .arg("-Wlinker-static-archive-order") | ||
| .print("link-args") | ||
| .run_unchecked() | ||
| .assert_stderr_contains("warning: static archive") | ||
| .assert_stderr_contains("libdoesnotexist.a"); | ||
|
|
||
| // 9. The lint also fires with `lld` (`Gnu(.., Lld::Yes)`): the risky ordering is the same, so | ||
| // the latent issue surfaces before a switch back to `ld.bfd`. | ||
| rustc() | ||
| .input("empty.rs") | ||
| .linker_flavor("ld.lld") | ||
| .link_arg(archive_str) | ||
| .arg("-Wlinker-static-archive-order") | ||
| .print("link-args") | ||
| .run_unchecked() | ||
| .assert_stderr_contains("warning: static archive") | ||
| .assert_stderr_contains("libdoesnotexist.a"); | ||
|
|
||
| assert!(!is_windows() && !is_darwin()); | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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-argrather than-lfeels iffy to me even ignoring the symbol resolution order issue. Maybe put less emphasis on the exact issue and instead say that-Clink-argfor static libraries is not idiomatic and what way should be used instead. You can leave a note about-Clink-argplaying badly with symbol resolution order as a concrete example about why you should avoid it.View changes since the review