Skip to content

Commit 771d428

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

File tree

3 files changed

+37
-9
lines changed

3 files changed

+37
-9
lines changed

src/librustc_back/target/linux_musl_base.rs

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

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

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

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

src/librustc_back/target/mod.rs

+16-5
Original file line numberDiff line numberDiff line change
@@ -279,20 +279,22 @@ pub struct TargetOptions {
279279
/// Linker to invoke. Defaults to "cc".
280280
pub linker: String,
281281

282-
/// Linker arguments that are unconditionally passed *before* any
283-
/// user-defined libraries.
284-
pub pre_link_args: LinkArgs,
282+
/// Linker arguments that are passed *before* any user-defined libraries.
283+
pub pre_link_args: LinkArgs, // ... unconditionally
284+
pub pre_link_args_crt: LinkArgs, // ... when linking with a bundled crt
285285
/// Objects to link before all others, always found within the
286286
/// sysroot folder.
287-
pub pre_link_objects_exe: Vec<String>, // ... when linking an executable
287+
pub pre_link_objects_exe: Vec<String>, // ... when linking an executable, unconditionally
288+
pub pre_link_objects_exe_crt: Vec<String>, // ... when linking an executable with a bundled crt
288289
pub pre_link_objects_dll: Vec<String>, // ... when linking a dylib
289290
/// Linker arguments that are unconditionally passed after any
290291
/// user-defined but before post_link_objects. Standard platform
291292
/// libraries that should be always be linked to, usually go here.
292293
pub late_link_args: LinkArgs,
293294
/// Objects to link after all others, always found within the
294295
/// sysroot folder.
295-
pub post_link_objects: Vec<String>,
296+
pub post_link_objects: Vec<String>, // ... unconditionally
297+
pub post_link_objects_crt: Vec<String>, // ... when linking with a bundled crt
296298
/// Linker arguments that are unconditionally passed *after* any
297299
/// user-defined libraries.
298300
pub post_link_args: LinkArgs,
@@ -482,6 +484,7 @@ impl Default for TargetOptions {
482484
is_builtin: false,
483485
linker: option_env!("CFG_DEFAULT_LINKER").unwrap_or("cc").to_string(),
484486
pre_link_args: LinkArgs::new(),
487+
pre_link_args_crt: LinkArgs::new(),
485488
post_link_args: LinkArgs::new(),
486489
asm_args: Vec::new(),
487490
cpu: "generic".to_string(),
@@ -515,8 +518,10 @@ impl Default for TargetOptions {
515518
position_independent_executables: false,
516519
relro_level: RelroLevel::Off,
517520
pre_link_objects_exe: Vec::new(),
521+
pre_link_objects_exe_crt: Vec::new(),
518522
pre_link_objects_dll: Vec::new(),
519523
post_link_objects: Vec::new(),
524+
post_link_objects_crt: Vec::new(),
520525
late_link_args: LinkArgs::new(),
521526
link_env: Vec::new(),
522527
archive_format: "gnu".to_string(),
@@ -732,10 +737,13 @@ impl Target {
732737
key!(is_builtin, bool);
733738
key!(linker);
734739
key!(pre_link_args, link_args);
740+
key!(pre_link_args_crt, link_args);
735741
key!(pre_link_objects_exe, list);
742+
key!(pre_link_objects_exe_crt, list);
736743
key!(pre_link_objects_dll, list);
737744
key!(late_link_args, link_args);
738745
key!(post_link_objects, list);
746+
key!(post_link_objects_crt, list);
739747
key!(post_link_args, link_args);
740748
key!(link_env, env);
741749
key!(asm_args, list);
@@ -930,10 +938,13 @@ impl ToJson for Target {
930938
target_option_val!(is_builtin);
931939
target_option_val!(linker);
932940
target_option_val!(link_args - pre_link_args);
941+
target_option_val!(link_args - pre_link_args_crt);
933942
target_option_val!(pre_link_objects_exe);
943+
target_option_val!(pre_link_objects_exe_crt);
934944
target_option_val!(pre_link_objects_dll);
935945
target_option_val!(link_args - late_link_args);
936946
target_option_val!(post_link_objects);
947+
target_option_val!(post_link_objects_crt);
937948
target_option_val!(link_args - post_link_args);
938949
target_option_val!(env - link_env);
939950
target_option_val!(asm_args);

src/librustc_trans/back/link.rs

+16
Original file line numberDiff line numberDiff line change
@@ -578,6 +578,11 @@ fn link_natively(sess: &Session,
578578
if let Some(args) = sess.target.target.options.pre_link_args.get(&flavor) {
579579
cmd.args(args);
580580
}
581+
if let Some(args) = sess.target.target.options.pre_link_args_crt.get(&flavor) {
582+
if sess.crt_static() {
583+
cmd.args(args);
584+
}
585+
}
581586
if let Some(ref args) = sess.opts.debugging_opts.pre_link_args {
582587
cmd.args(args);
583588
}
@@ -592,6 +597,12 @@ fn link_natively(sess: &Session,
592597
cmd.arg(root.join(obj));
593598
}
594599

600+
if crate_type == config::CrateTypeExecutable && sess.crt_static() {
601+
for obj in &sess.target.target.options.pre_link_objects_exe_crt {
602+
cmd.arg(root.join(obj));
603+
}
604+
}
605+
595606
if sess.target.target.options.is_like_emscripten {
596607
cmd.arg("-s");
597608
cmd.arg(if sess.panic_strategy() == PanicStrategy::Abort {
@@ -613,6 +624,11 @@ fn link_natively(sess: &Session,
613624
for obj in &sess.target.target.options.post_link_objects {
614625
cmd.arg(root.join(obj));
615626
}
627+
if sess.crt_static() {
628+
for obj in &sess.target.target.options.post_link_objects_crt {
629+
cmd.arg(root.join(obj));
630+
}
631+
}
616632
if let Some(args) = sess.target.target.options.post_link_args.get(&flavor) {
617633
cmd.args(args);
618634
}

0 commit comments

Comments
 (0)