Skip to content

Commit f55129d

Browse files
committed
Auto merge of #54675 - alexcrichton:defaultlibs, r=varkor
rust: Add a `-C default-linker-libraries` option This commit adds a new codegen option for the compiler which disables rustc's passing of `-nodefaultlibs` by default on relevant platforms. Sometimes Rust is linked with C code which fails to link with `-nodefaultlibs` and is unnecessarily onerous to get linking correctly with `-nodefaultlibs`. An example of this is that when you compile C code with sanitizers and then pass `-fsanitize=address` to the linker, it's incompatible with `-nodefaultlibs` also being passed to the linker. In these situations it's easiest to turn off Rust's default passing of `-nodefaultlibs`, which was more ideological to start with than anything! Preserving the default is somewhat important but having this be opt-in shouldn't cause any breakage. Closes #54237
2 parents 0337964 + 6f4b378 commit f55129d

File tree

2 files changed

+12
-6
lines changed

2 files changed

+12
-6
lines changed

src/librustc/session/config.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1135,6 +1135,8 @@ options! {CodegenOptions, CodegenSetter, basic_codegen_options,
11351135
[TRACKED], "panic strategy to compile crate with"),
11361136
incremental: Option<String> = (None, parse_opt_string, [UNTRACKED],
11371137
"enable incremental compilation"),
1138+
default_linker_libraries: Option<bool> = (None, parse_opt_bool, [UNTRACKED],
1139+
"allow the linker to link its default libraries"),
11381140
}
11391141

11401142
options! {DebuggingOptions, DebuggingSetter, basic_debugging_options,

src/librustc_codegen_llvm/back/link.rs

+10-6
Original file line numberDiff line numberDiff line change
@@ -1138,12 +1138,16 @@ fn link_args(cmd: &mut dyn Linker,
11381138
// Pass debuginfo flags down to the linker.
11391139
cmd.debuginfo();
11401140

1141-
// We want to prevent the compiler from accidentally leaking in any system
1142-
// libraries, so we explicitly ask gcc to not link to any libraries by
1143-
// default. Note that this does not happen for windows because windows pulls
1144-
// in some large number of libraries and I couldn't quite figure out which
1145-
// subset we wanted.
1146-
if t.options.no_default_libraries {
1141+
// We want to, by default, prevent the compiler from accidentally leaking in
1142+
// any system libraries, so we may explicitly ask linkers to not link to any
1143+
// libraries by default. Note that this does not happen for windows because
1144+
// windows pulls in some large number of libraries and I couldn't quite
1145+
// figure out which subset we wanted.
1146+
//
1147+
// This is all naturally configurable via the standard methods as well.
1148+
if !sess.opts.cg.default_linker_libraries.unwrap_or(false) &&
1149+
t.options.no_default_libraries
1150+
{
11471151
cmd.no_default_libraries();
11481152
}
11491153

0 commit comments

Comments
 (0)