Skip to content

Commit f17cc4c

Browse files
committed
Auto merge of #28578 - gandro:nodefaultlibs, r=alexcrichton
This patch basically adds a target option for omitting the `-nodefaultlibs` flag when invoking the linker. I am not sure if this is the correct or only way to approach this problem, so any feedback is welcome. Motivation: I'm currently working on a Rust target specification for the [rumprun](/rumpkernel/rumprun) unikernel. rumprun is based on rump kernels and uses NetBSDs libc and drivers to provide a POSIXy environment. It provides its own linker wrapper that generates binaries which can be "baked" into a unikernel after configuration. Using `-nodefaultlibs` on the rumprun linker will prevent it from selecting the search paths for the rumprun libraries. My current target implementation for rumprun is here: gandro/rust@295744b Currently, only a target that `is_like_windows` will omit the `-nodefaultlibs` flag, but since rumprun is not like Windows otherwise, I think a separate flag makes more sense. This might be a breaking change for target specifications that have the `is_like_windows` option set to true. Such targets need to set `no_default_libraries` to false in order to restore the old behavior.
2 parents 0b089cd + 920f32b commit f17cc4c

File tree

4 files changed

+13
-7
lines changed

4 files changed

+13
-7
lines changed

src/librustc_back/target/mod.rs

+5
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,9 @@ pub struct TargetOptions {
157157
/// Whether to disable linking to compiler-rt. Defaults to false, as LLVM
158158
/// will emit references to the functions that compiler-rt provides.
159159
pub no_compiler_rt: bool,
160+
/// Whether to disable linking to the default libraries, typically corresponds
161+
/// to `-nodefaultlibs`. Defaults to true.
162+
pub no_default_libraries: bool,
160163
/// Dynamically linked executables can be compiled as position independent
161164
/// if the default relocation model of position independent code is not
162165
/// changed. This is a requirement to take advantage of ASLR, as otherwise
@@ -212,6 +215,7 @@ impl Default for TargetOptions {
212215
linker_is_gnu: false,
213216
has_rpath: false,
214217
no_compiler_rt: false,
218+
no_default_libraries: true,
215219
position_independent_executables: false,
216220
pre_link_objects: Vec::new(),
217221
post_link_objects: Vec::new(),
@@ -319,6 +323,7 @@ impl Target {
319323
key!(linker_is_gnu, bool);
320324
key!(has_rpath, bool);
321325
key!(no_compiler_rt, bool);
326+
key!(no_default_libraries, bool);
322327
key!(pre_link_args, list);
323328
key!(post_link_args, list);
324329
key!(allow_asm, bool);

src/librustc_back/target/windows_base.rs

+4
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ pub fn opts() -> TargetOptions {
2323
exe_suffix: ".exe".to_string(),
2424
staticlib_prefix: "".to_string(),
2525
staticlib_suffix: ".lib".to_string(),
26+
// Unfortunately right now passing -nodefaultlibs to gcc on windows
27+
// doesn't work so hot (in terms of native dependencies). This flag
28+
// should hopefully be removed one day though!
29+
no_default_libraries: false,
2630
is_like_windows: true,
2731
archive_format: "gnu".to_string(),
2832
pre_link_args: vec!(

src/librustc_trans/back/link.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -970,7 +970,9 @@ fn link_args(cmd: &mut Linker,
970970
// default. Note that this does not happen for windows because windows pulls
971971
// in some large number of libraries and I couldn't quite figure out which
972972
// subset we wanted.
973-
cmd.no_default_libraries();
973+
if t.options.no_default_libraries {
974+
cmd.no_default_libraries();
975+
}
974976

975977
// Take careful note of the ordering of the arguments we pass to the linker
976978
// here. Linkers will assume that things on the left depend on things to the

src/librustc_trans/back/linker.rs

+1-6
Original file line numberDiff line numberDiff line change
@@ -159,12 +159,7 @@ impl<'a> Linker for GnuLinker<'a> {
159159
}
160160

161161
fn no_default_libraries(&mut self) {
162-
// Unfortunately right now passing -nodefaultlibs to gcc on windows
163-
// doesn't work so hot (in terms of native dependencies). This if
164-
// statement should hopefully be removed one day though!
165-
if !self.sess.target.target.options.is_like_windows {
166-
self.cmd.arg("-nodefaultlibs");
167-
}
162+
self.cmd.arg("-nodefaultlibs");
168163
}
169164

170165
fn build_dylib(&mut self, out_filename: &Path) {

0 commit comments

Comments
 (0)