-
Notifications
You must be signed in to change notification settings - Fork 38
Bare linker support #203
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
Merged
Merged
Bare linker support #203
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
f5d5a3d
Add support for recovering rustc codegen options
Shnatsel bfcaa58
drop boilerplate from test that's no longer relevant
Shnatsel 16b45a0
Add bare linker detection function
Shnatsel cb189ba
Get rid of a compiler warning
Shnatsel 5a3777f
Do not emit -Wl if a bare linker is in use
Shnatsel b3a862d
Placate clippy
Shnatsel 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,6 +21,7 @@ pub struct RustcArgs { | |
| pub out_dir: Option<PathBuf>, | ||
| pub target: Option<String>, | ||
| pub print: Vec<String>, | ||
| pub codegen: Vec<String>, | ||
| } | ||
|
|
||
| impl RustcArgs { | ||
|
|
@@ -35,6 +36,29 @@ impl RustcArgs { | |
| } | ||
| result | ||
| } | ||
|
|
||
| /// Normally `rustc` uses a C compiler such as `cc` or `clang` as linker, | ||
| /// and arguments to the actual linker need to be passed prefixed with `-Wl,`. | ||
| /// But it is possible to configure Cargo and rustc to call a linker directly, | ||
| /// and the breakage it causes is subtle enough that people just roll with it | ||
| /// and complain when cargo-auditable doesn't support this configuration: | ||
| /// <https://github.com/rust-secure-code/cargo-auditable/issues/202> | ||
| /// | ||
| /// This function can tell you if a bare linker is in use | ||
| /// and whether you need to prepend `-Wl,` or not. | ||
| /// | ||
| /// Such setups are exceptionally rare and frankly it's a misconfiguration | ||
| /// that will break more than just `cargo auditable`, but I am feeling generous. | ||
|
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. 🙏 |
||
| pub fn bare_linker(&self) -> bool { | ||
| let linker_flag = self.codegen.iter().find(|s| s.starts_with("linker=")); | ||
| if let Some(linker_flag) = linker_flag { | ||
| let linker = linker_flag.strip_prefix("linker=").unwrap(); | ||
| if linker.ends_with("ld") { | ||
| return true; | ||
| } | ||
| } | ||
| false | ||
| } | ||
| } | ||
|
|
||
| impl RustcArgs { | ||
|
|
@@ -62,6 +86,7 @@ impl RustcArgs { | |
| })?, | ||
| target: parser.opt_value_from_str("--target")?, | ||
| print: parser.values_from_str("--print")?, | ||
| codegen: parser.values_from_str("-C")?, | ||
| }) | ||
| } | ||
| } | ||
|
|
@@ -147,16 +172,7 @@ mod tests { | |
|
|
||
| #[test] | ||
| fn multiple_emit_values() { | ||
| let raw_rustc_args = vec![ | ||
| "--emit=dep-info,link", | ||
| "--emit", | ||
| "llvm-bc", | ||
| // end of interesting args, start of boilerplate | ||
| "--crate-name", | ||
| "foobar", | ||
| "--out-dir", | ||
| "/foo/bar", | ||
| ]; | ||
| let raw_rustc_args = vec!["--emit=dep-info,link", "--emit", "llvm-bc"]; | ||
| let raw_rustc_args: Vec<OsString> = raw_rustc_args.into_iter().map(|s| s.into()).collect(); | ||
| let mut args = RustcArgs::from_vec(raw_rustc_args).unwrap(); | ||
|
|
||
|
|
@@ -168,4 +184,29 @@ mod tests { | |
|
|
||
| assert_eq!(args.emit, expected) | ||
| } | ||
|
|
||
| #[test] | ||
| fn detect_bare_linker() { | ||
| let raw_rustc_args = vec!["-C", "linker=rust-lld"]; | ||
| let raw_rustc_args: Vec<OsString> = raw_rustc_args.into_iter().map(|s| s.into()).collect(); | ||
| let args = RustcArgs::from_vec(raw_rustc_args).unwrap(); | ||
| assert!(args.bare_linker()); | ||
| } | ||
|
|
||
| #[test] | ||
| fn multiple_codegen_options() { | ||
| let raw_rustc_args = vec!["-Clinker=clang", "-C", "link-arg=-fuse-ld=/usr/bin/mold"]; | ||
| let raw_rustc_args: Vec<OsString> = raw_rustc_args.into_iter().map(|s| s.into()).collect(); | ||
| let mut args = RustcArgs::from_vec(raw_rustc_args).unwrap(); | ||
|
|
||
| let expected = vec!["linker=clang", "link-arg=-fuse-ld=/usr/bin/mold"]; | ||
| let mut expected: Vec<String> = expected.into_iter().map(|s| s.into()).collect(); | ||
|
|
||
| args.codegen.sort(); | ||
| expected.sort(); | ||
|
|
||
| assert_eq!(args.codegen, expected); | ||
|
|
||
| assert!(!args.bare_linker()); | ||
| } | ||
| } | ||
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
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.
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.
😂