Skip to content

Commit 6d9154a

Browse files
committed
musl: link crt{begin,end}.o from the system compiler
This fixes #36710 with +crt-static. We only need to add crtbegin.o and crtend.o as we only do static linking with the bundled start files and there is no static-pie support in rustc yet.
1 parent ec2b861 commit 6d9154a

File tree

3 files changed

+25
-2
lines changed

3 files changed

+25
-2
lines changed

src/librustc_target/spec/linux_musl_base.rs

+2
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ pub fn opts() -> TargetOptions {
5858
// they'll be included from there.
5959
base.pre_link_objects_exe_crt.push("crt1.o".to_string());
6060
base.pre_link_objects_exe_crt.push("crti.o".to_string());
61+
base.pre_link_objects_exe_crt_sys.push("crtbegin.o".to_string());
62+
base.post_link_objects_crt_sys.push("crtend.o".to_string());
6163
base.post_link_objects_crt.push("crtn.o".to_string());
6264

6365
// These targets statically link libc by default

src/librustc_target/spec/mod.rs

+12-2
Original file line numberDiff line numberDiff line change
@@ -423,19 +423,23 @@ pub struct TargetOptions {
423423
/// Linker arguments that are passed *before* any user-defined libraries.
424424
pub pre_link_args: LinkArgs, // ... unconditionally
425425
pub pre_link_args_crt: LinkArgs, // ... when linking with a bundled crt
426-
/// Objects to link before all others, always found within the
426+
/// Objects to link before all others, all except *_sys found within the
427427
/// sysroot folder.
428428
pub pre_link_objects_exe: Vec<String>, // ... when linking an executable, unconditionally
429429
pub pre_link_objects_exe_crt: Vec<String>, // ... when linking an executable with a bundled crt
430+
pub pre_link_objects_exe_crt_sys: Vec<String>, // ... when linking an executable with a bundled
431+
// crt, from the system library search path
430432
pub pre_link_objects_dll: Vec<String>, // ... when linking a dylib
431433
/// Linker arguments that are unconditionally passed after any
432434
/// user-defined but before post_link_objects. Standard platform
433435
/// libraries that should be always be linked to, usually go here.
434436
pub late_link_args: LinkArgs,
435-
/// Objects to link after all others, always found within the
437+
/// Objects to link after all others, all except *_sys found within the
436438
/// sysroot folder.
437439
pub post_link_objects: Vec<String>, // ... unconditionally
438440
pub post_link_objects_crt: Vec<String>, // ... when linking with a bundled crt
441+
pub post_link_objects_crt_sys: Vec<String>, // ... when linking with a bundled crt, from the
442+
// system library search path
439443
/// Linker arguments that are unconditionally passed *after* any
440444
/// user-defined libraries.
441445
pub post_link_args: LinkArgs,
@@ -670,9 +674,11 @@ impl Default for TargetOptions {
670674
relro_level: RelroLevel::None,
671675
pre_link_objects_exe: Vec::new(),
672676
pre_link_objects_exe_crt: Vec::new(),
677+
pre_link_objects_exe_crt_sys: Vec::new(),
673678
pre_link_objects_dll: Vec::new(),
674679
post_link_objects: Vec::new(),
675680
post_link_objects_crt: Vec::new(),
681+
post_link_objects_crt_sys: Vec::new(),
676682
late_link_args: LinkArgs::new(),
677683
link_env: Vec::new(),
678684
archive_format: "gnu".to_string(),
@@ -894,10 +900,12 @@ impl Target {
894900
key!(pre_link_args_crt, link_args);
895901
key!(pre_link_objects_exe, list);
896902
key!(pre_link_objects_exe_crt, list);
903+
key!(pre_link_objects_exe_crt_sys, list);
897904
key!(pre_link_objects_dll, list);
898905
key!(late_link_args, link_args);
899906
key!(post_link_objects, list);
900907
key!(post_link_objects_crt, list);
908+
key!(post_link_objects_crt_sys, list);
901909
key!(post_link_args, link_args);
902910
key!(link_env, env);
903911
key!(asm_args, list);
@@ -1102,10 +1110,12 @@ impl ToJson for Target {
11021110
target_option_val!(link_args - pre_link_args_crt);
11031111
target_option_val!(pre_link_objects_exe);
11041112
target_option_val!(pre_link_objects_exe_crt);
1113+
target_option_val!(pre_link_objects_exe_crt_sys);
11051114
target_option_val!(pre_link_objects_dll);
11061115
target_option_val!(link_args - late_link_args);
11071116
target_option_val!(post_link_objects);
11081117
target_option_val!(post_link_objects_crt);
1118+
target_option_val!(post_link_objects_crt_sys);
11091119
target_option_val!(link_args - post_link_args);
11101120
target_option_val!(env - link_env);
11111121
target_option_val!(asm_args);

src/librustc_trans/back/link.rs

+11
Original file line numberDiff line numberDiff line change
@@ -644,6 +644,12 @@ fn link_natively(sess: &Session,
644644
for obj in &sess.target.target.options.pre_link_objects_exe_crt {
645645
cmd.arg(root.join(obj));
646646
}
647+
648+
for obj in &sess.target.target.options.pre_link_objects_exe_crt_sys {
649+
if flavor == LinkerFlavor::Gcc {
650+
cmd.arg(format!("-l:{}", obj));
651+
}
652+
}
647653
}
648654

649655
if sess.target.target.options.is_like_emscripten {
@@ -668,6 +674,11 @@ fn link_natively(sess: &Session,
668674
cmd.arg(root.join(obj));
669675
}
670676
if sess.crt_static() {
677+
for obj in &sess.target.target.options.post_link_objects_crt_sys {
678+
if flavor == LinkerFlavor::Gcc {
679+
cmd.arg(format!("-l:{}", obj));
680+
}
681+
}
671682
for obj in &sess.target.target.options.post_link_objects_crt {
672683
cmd.arg(root.join(obj));
673684
}

0 commit comments

Comments
 (0)