Skip to content

Commit bf3b8df

Browse files
committed
musl: don't use the included startfiles with -crt-static
This fixes (only for -crt-static) #36710.
1 parent a47b2d0 commit bf3b8df

File tree

3 files changed

+37
-9
lines changed

3 files changed

+37
-9
lines changed

src/librustc_codegen_llvm/back/link.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -625,6 +625,11 @@ fn link_natively(sess: &Session,
625625
if let Some(args) = sess.target.target.options.pre_link_args.get(&flavor) {
626626
cmd.args(args);
627627
}
628+
if let Some(args) = sess.target.target.options.pre_link_args_crt.get(&flavor) {
629+
if sess.crt_static() {
630+
cmd.args(args);
631+
}
632+
}
628633
if let Some(ref args) = sess.opts.debugging_opts.pre_link_args {
629634
cmd.args(args);
630635
}
@@ -639,6 +644,12 @@ fn link_natively(sess: &Session,
639644
cmd.arg(root.join(obj));
640645
}
641646

647+
if crate_type == config::CrateTypeExecutable && sess.crt_static() {
648+
for obj in &sess.target.target.options.pre_link_objects_exe_crt {
649+
cmd.arg(root.join(obj));
650+
}
651+
}
652+
642653
if sess.target.target.options.is_like_emscripten {
643654
cmd.arg("-s");
644655
cmd.arg(if sess.panic_strategy() == PanicStrategy::Abort {
@@ -660,6 +671,11 @@ fn link_natively(sess: &Session,
660671
for obj in &sess.target.target.options.post_link_objects {
661672
cmd.arg(root.join(obj));
662673
}
674+
if sess.crt_static() {
675+
for obj in &sess.target.target.options.post_link_objects_crt {
676+
cmd.arg(root.join(obj));
677+
}
678+
}
663679
if let Some(args) = sess.target.target.options.post_link_args.get(&flavor) {
664680
cmd.args(args);
665681
}

src/librustc_target/spec/linux_musl_base.rs

Lines changed: 5 additions & 4 deletions
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

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -426,20 +426,22 @@ pub struct TargetOptions {
426426
/// Linker to invoke
427427
pub linker: Option<String>,
428428

429-
/// Linker arguments that are unconditionally passed *before* any
430-
/// user-defined libraries.
431-
pub pre_link_args: LinkArgs,
429+
/// Linker arguments that are passed *before* any user-defined libraries.
430+
pub pre_link_args: LinkArgs, // ... unconditionally
431+
pub pre_link_args_crt: LinkArgs, // ... when linking with a bundled crt
432432
/// Objects to link before all others, always found within the
433433
/// sysroot folder.
434-
pub pre_link_objects_exe: Vec<String>, // ... when linking an executable
434+
pub pre_link_objects_exe: Vec<String>, // ... when linking an executable, unconditionally
435+
pub pre_link_objects_exe_crt: Vec<String>, // ... when linking an executable with a bundled crt
435436
pub pre_link_objects_dll: Vec<String>, // ... when linking a dylib
436437
/// Linker arguments that are unconditionally passed after any
437438
/// user-defined but before post_link_objects. Standard platform
438439
/// libraries that should be always be linked to, usually go here.
439440
pub late_link_args: LinkArgs,
440441
/// Objects to link after all others, always found within the
441442
/// sysroot folder.
442-
pub post_link_objects: Vec<String>,
443+
pub post_link_objects: Vec<String>, // ... unconditionally
444+
pub post_link_objects_crt: Vec<String>, // ... when linking with a bundled crt
443445
/// Linker arguments that are unconditionally passed *after* any
444446
/// user-defined libraries.
445447
pub post_link_args: LinkArgs,
@@ -639,6 +641,7 @@ impl Default for TargetOptions {
639641
is_builtin: false,
640642
linker: option_env!("CFG_DEFAULT_LINKER").map(|s| s.to_string()),
641643
pre_link_args: LinkArgs::new(),
644+
pre_link_args_crt: LinkArgs::new(),
642645
post_link_args: LinkArgs::new(),
643646
asm_args: Vec::new(),
644647
cpu: "generic".to_string(),
@@ -672,8 +675,10 @@ impl Default for TargetOptions {
672675
position_independent_executables: false,
673676
relro_level: RelroLevel::None,
674677
pre_link_objects_exe: Vec::new(),
678+
pre_link_objects_exe_crt: Vec::new(),
675679
pre_link_objects_dll: Vec::new(),
676680
post_link_objects: Vec::new(),
681+
post_link_objects_crt: Vec::new(),
677682
late_link_args: LinkArgs::new(),
678683
link_env: Vec::new(),
679684
archive_format: "gnu".to_string(),
@@ -892,10 +897,13 @@ impl Target {
892897
key!(is_builtin, bool);
893898
key!(linker, optional);
894899
key!(pre_link_args, link_args);
900+
key!(pre_link_args_crt, link_args);
895901
key!(pre_link_objects_exe, list);
902+
key!(pre_link_objects_exe_crt, list);
896903
key!(pre_link_objects_dll, list);
897904
key!(late_link_args, link_args);
898905
key!(post_link_objects, list);
906+
key!(post_link_objects_crt, list);
899907
key!(post_link_args, link_args);
900908
key!(link_env, env);
901909
key!(asm_args, list);
@@ -1097,10 +1105,13 @@ impl ToJson for Target {
10971105
target_option_val!(is_builtin);
10981106
target_option_val!(linker);
10991107
target_option_val!(link_args - pre_link_args);
1108+
target_option_val!(link_args - pre_link_args_crt);
11001109
target_option_val!(pre_link_objects_exe);
1110+
target_option_val!(pre_link_objects_exe_crt);
11011111
target_option_val!(pre_link_objects_dll);
11021112
target_option_val!(link_args - late_link_args);
11031113
target_option_val!(post_link_objects);
1114+
target_option_val!(post_link_objects_crt);
11041115
target_option_val!(link_args - post_link_args);
11051116
target_option_val!(env - link_env);
11061117
target_option_val!(asm_args);

0 commit comments

Comments
 (0)