Skip to content

Commit a5a875d

Browse files
committed
musl: don't use the included startfiles with -crt-static
This fixes rust-lang#36710 with -crt-static.
1 parent f76f6fb commit a5a875d

File tree

3 files changed

+37
-9
lines changed

3 files changed

+37
-9
lines changed

src/librustc_target/spec/linux_musl_base.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ pub fn opts() -> TargetOptions {
1515

1616
// Make sure that the linker/gcc really don't pull in anything, including
1717
// default objects, libs, etc.
18-
base.pre_link_args.get_mut(&LinkerFlavor::Gcc).unwrap().push("-nostdlib".to_string());
18+
base.pre_link_args_crt.insert(LinkerFlavor::Gcc, Vec::new());
19+
base.pre_link_args_crt.get_mut(&LinkerFlavor::Gcc).unwrap().push("-nostdlib".to_string());
1920

2021
// At least when this was tested, the linker would not add the
2122
// `GNU_EH_FRAME` program header to executables generated, which is required
@@ -55,9 +56,9 @@ pub fn opts() -> TargetOptions {
5556
//
5657
// Each target directory for musl has these object files included in it so
5758
// they'll be included from there.
58-
base.pre_link_objects_exe.push("crt1.o".to_string());
59-
base.pre_link_objects_exe.push("crti.o".to_string());
60-
base.post_link_objects.push("crtn.o".to_string());
59+
base.pre_link_objects_exe_crt.push("crt1.o".to_string());
60+
base.pre_link_objects_exe_crt.push("crti.o".to_string());
61+
base.post_link_objects_crt.push("crtn.o".to_string());
6162

6263
// These targets statically link libc by default
6364
base.crt_static_default = true;

src/librustc_target/spec/mod.rs

+16-5
Original file line numberDiff line numberDiff line change
@@ -420,20 +420,22 @@ pub struct TargetOptions {
420420
/// Linker to invoke
421421
pub linker: Option<String>,
422422

423-
/// Linker arguments that are unconditionally passed *before* any
424-
/// user-defined libraries.
425-
pub pre_link_args: LinkArgs,
423+
/// Linker arguments that are passed *before* any user-defined libraries.
424+
pub pre_link_args: LinkArgs, // ... unconditionally
425+
pub pre_link_args_crt: LinkArgs, // ... when linking with a bundled crt
426426
/// Objects to link before all others, always found within the
427427
/// sysroot folder.
428-
pub pre_link_objects_exe: Vec<String>, // ... when linking an executable
428+
pub pre_link_objects_exe: Vec<String>, // ... when linking an executable, unconditionally
429+
pub pre_link_objects_exe_crt: Vec<String>, // ... when linking an executable with a bundled crt
429430
pub pre_link_objects_dll: Vec<String>, // ... when linking a dylib
430431
/// Linker arguments that are unconditionally passed after any
431432
/// user-defined but before post_link_objects. Standard platform
432433
/// libraries that should be always be linked to, usually go here.
433434
pub late_link_args: LinkArgs,
434435
/// Objects to link after all others, always found within the
435436
/// sysroot folder.
436-
pub post_link_objects: Vec<String>,
437+
pub post_link_objects: Vec<String>, // ... unconditionally
438+
pub post_link_objects_crt: Vec<String>, // ... when linking with a bundled crt
437439
/// Linker arguments that are unconditionally passed *after* any
438440
/// user-defined libraries.
439441
pub post_link_args: LinkArgs,
@@ -633,6 +635,7 @@ impl Default for TargetOptions {
633635
is_builtin: false,
634636
linker: option_env!("CFG_DEFAULT_LINKER").map(|s| s.to_string()),
635637
pre_link_args: LinkArgs::new(),
638+
pre_link_args_crt: LinkArgs::new(),
636639
post_link_args: LinkArgs::new(),
637640
asm_args: Vec::new(),
638641
cpu: "generic".to_string(),
@@ -666,8 +669,10 @@ impl Default for TargetOptions {
666669
position_independent_executables: false,
667670
relro_level: RelroLevel::None,
668671
pre_link_objects_exe: Vec::new(),
672+
pre_link_objects_exe_crt: Vec::new(),
669673
pre_link_objects_dll: Vec::new(),
670674
post_link_objects: Vec::new(),
675+
post_link_objects_crt: Vec::new(),
671676
late_link_args: LinkArgs::new(),
672677
link_env: Vec::new(),
673678
archive_format: "gnu".to_string(),
@@ -886,10 +891,13 @@ impl Target {
886891
key!(is_builtin, bool);
887892
key!(linker, optional);
888893
key!(pre_link_args, link_args);
894+
key!(pre_link_args_crt, link_args);
889895
key!(pre_link_objects_exe, list);
896+
key!(pre_link_objects_exe_crt, list);
890897
key!(pre_link_objects_dll, list);
891898
key!(late_link_args, link_args);
892899
key!(post_link_objects, list);
900+
key!(post_link_objects_crt, list);
893901
key!(post_link_args, link_args);
894902
key!(link_env, env);
895903
key!(asm_args, list);
@@ -1091,10 +1099,13 @@ impl ToJson for Target {
10911099
target_option_val!(is_builtin);
10921100
target_option_val!(linker);
10931101
target_option_val!(link_args - pre_link_args);
1102+
target_option_val!(link_args - pre_link_args_crt);
10941103
target_option_val!(pre_link_objects_exe);
1104+
target_option_val!(pre_link_objects_exe_crt);
10951105
target_option_val!(pre_link_objects_dll);
10961106
target_option_val!(link_args - late_link_args);
10971107
target_option_val!(post_link_objects);
1108+
target_option_val!(post_link_objects_crt);
10981109
target_option_val!(link_args - post_link_args);
10991110
target_option_val!(env - link_env);
11001111
target_option_val!(asm_args);

src/librustc_trans/back/link.rs

+16
Original file line numberDiff line numberDiff line change
@@ -621,6 +621,11 @@ fn link_natively(sess: &Session,
621621
if let Some(args) = sess.target.target.options.pre_link_args.get(&flavor) {
622622
cmd.args(args);
623623
}
624+
if let Some(args) = sess.target.target.options.pre_link_args_crt.get(&flavor) {
625+
if sess.crt_static() {
626+
cmd.args(args);
627+
}
628+
}
624629
if let Some(ref args) = sess.opts.debugging_opts.pre_link_args {
625630
cmd.args(args);
626631
}
@@ -635,6 +640,12 @@ fn link_natively(sess: &Session,
635640
cmd.arg(root.join(obj));
636641
}
637642

643+
if crate_type == config::CrateTypeExecutable && sess.crt_static() {
644+
for obj in &sess.target.target.options.pre_link_objects_exe_crt {
645+
cmd.arg(root.join(obj));
646+
}
647+
}
648+
638649
if sess.target.target.options.is_like_emscripten {
639650
cmd.arg("-s");
640651
cmd.arg(if sess.panic_strategy() == PanicStrategy::Abort {
@@ -656,6 +667,11 @@ fn link_natively(sess: &Session,
656667
for obj in &sess.target.target.options.post_link_objects {
657668
cmd.arg(root.join(obj));
658669
}
670+
if sess.crt_static() {
671+
for obj in &sess.target.target.options.post_link_objects_crt {
672+
cmd.arg(root.join(obj));
673+
}
674+
}
659675
if let Some(args) = sess.target.target.options.post_link_args.get(&flavor) {
660676
cmd.args(args);
661677
}

0 commit comments

Comments
 (0)