Skip to content

Commit

Permalink
musl: link crt{begin,end}.o from the system compiler
Browse files Browse the repository at this point in the history
This fixes rust-lang#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.
  • Loading branch information
mixi committed May 1, 2018
1 parent ec2b861 commit 6d9154a
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 2 deletions.
2 changes: 2 additions & 0 deletions src/librustc_target/spec/linux_musl_base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ pub fn opts() -> TargetOptions {
// they'll be included from there.
base.pre_link_objects_exe_crt.push("crt1.o".to_string());
base.pre_link_objects_exe_crt.push("crti.o".to_string());
base.pre_link_objects_exe_crt_sys.push("crtbegin.o".to_string());
base.post_link_objects_crt_sys.push("crtend.o".to_string());
base.post_link_objects_crt.push("crtn.o".to_string());

// These targets statically link libc by default
Expand Down
14 changes: 12 additions & 2 deletions src/librustc_target/spec/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -423,19 +423,23 @@ pub struct TargetOptions {
/// Linker arguments that are passed *before* any user-defined libraries.
pub pre_link_args: LinkArgs, // ... unconditionally
pub pre_link_args_crt: LinkArgs, // ... when linking with a bundled crt
/// Objects to link before all others, always found within the
/// Objects to link before all others, all except *_sys found within the
/// sysroot folder.
pub pre_link_objects_exe: Vec<String>, // ... when linking an executable, unconditionally
pub pre_link_objects_exe_crt: Vec<String>, // ... when linking an executable with a bundled crt
pub pre_link_objects_exe_crt_sys: Vec<String>, // ... when linking an executable with a bundled
// crt, from the system library search path
pub pre_link_objects_dll: Vec<String>, // ... when linking a dylib
/// Linker arguments that are unconditionally passed after any
/// user-defined but before post_link_objects. Standard platform
/// libraries that should be always be linked to, usually go here.
pub late_link_args: LinkArgs,
/// Objects to link after all others, always found within the
/// Objects to link after all others, all except *_sys found within the
/// sysroot folder.
pub post_link_objects: Vec<String>, // ... unconditionally
pub post_link_objects_crt: Vec<String>, // ... when linking with a bundled crt
pub post_link_objects_crt_sys: Vec<String>, // ... when linking with a bundled crt, from the
// system library search path
/// Linker arguments that are unconditionally passed *after* any
/// user-defined libraries.
pub post_link_args: LinkArgs,
Expand Down Expand Up @@ -670,9 +674,11 @@ impl Default for TargetOptions {
relro_level: RelroLevel::None,
pre_link_objects_exe: Vec::new(),
pre_link_objects_exe_crt: Vec::new(),
pre_link_objects_exe_crt_sys: Vec::new(),
pre_link_objects_dll: Vec::new(),
post_link_objects: Vec::new(),
post_link_objects_crt: Vec::new(),
post_link_objects_crt_sys: Vec::new(),
late_link_args: LinkArgs::new(),
link_env: Vec::new(),
archive_format: "gnu".to_string(),
Expand Down Expand Up @@ -894,10 +900,12 @@ impl Target {
key!(pre_link_args_crt, link_args);
key!(pre_link_objects_exe, list);
key!(pre_link_objects_exe_crt, list);
key!(pre_link_objects_exe_crt_sys, list);
key!(pre_link_objects_dll, list);
key!(late_link_args, link_args);
key!(post_link_objects, list);
key!(post_link_objects_crt, list);
key!(post_link_objects_crt_sys, list);
key!(post_link_args, link_args);
key!(link_env, env);
key!(asm_args, list);
Expand Down Expand Up @@ -1102,10 +1110,12 @@ impl ToJson for Target {
target_option_val!(link_args - pre_link_args_crt);
target_option_val!(pre_link_objects_exe);
target_option_val!(pre_link_objects_exe_crt);
target_option_val!(pre_link_objects_exe_crt_sys);
target_option_val!(pre_link_objects_dll);
target_option_val!(link_args - late_link_args);
target_option_val!(post_link_objects);
target_option_val!(post_link_objects_crt);
target_option_val!(post_link_objects_crt_sys);
target_option_val!(link_args - post_link_args);
target_option_val!(env - link_env);
target_option_val!(asm_args);
Expand Down
11 changes: 11 additions & 0 deletions src/librustc_trans/back/link.rs
Original file line number Diff line number Diff line change
Expand Up @@ -644,6 +644,12 @@ fn link_natively(sess: &Session,
for obj in &sess.target.target.options.pre_link_objects_exe_crt {
cmd.arg(root.join(obj));
}

for obj in &sess.target.target.options.pre_link_objects_exe_crt_sys {
if flavor == LinkerFlavor::Gcc {
cmd.arg(format!("-l:{}", obj));
}
}
}

if sess.target.target.options.is_like_emscripten {
Expand All @@ -668,6 +674,11 @@ fn link_natively(sess: &Session,
cmd.arg(root.join(obj));
}
if sess.crt_static() {
for obj in &sess.target.target.options.post_link_objects_crt_sys {
if flavor == LinkerFlavor::Gcc {
cmd.arg(format!("-l:{}", obj));
}
}
for obj in &sess.target.target.options.post_link_objects_crt {
cmd.arg(root.join(obj));
}
Expand Down

0 comments on commit 6d9154a

Please sign in to comment.