Skip to content

rustc_trans: implement lld compatibility #33327

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 1 addition & 18 deletions src/librustc_back/target/i686_unknown_linux_musl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,31 +8,14 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

// See x86_64_unknown_linux_musl for explanation of arguments

use target::Target;

pub fn target() -> Target {
let mut base = super::linux_base::opts();
let mut base = super::linux_musl_base::opts();
base.cpu = "pentium4".to_string();
base.pre_link_args.push("-m32".to_string());
base.pre_link_args.push("-Wl,-melf_i386".to_string());

base.pre_link_args.push("-nostdlib".to_string());
base.pre_link_args.push("-static".to_string());
base.pre_link_args.push("-Wl,--eh-frame-hdr".to_string());

base.pre_link_args.push("-Wl,-(".to_string());
base.post_link_args.push("-Wl,-)".to_string());

base.pre_link_objects_exe.push("crt1.o".to_string());
base.pre_link_objects_exe.push("crti.o".to_string());
base.post_link_objects.push("crtn.o".to_string());

base.dynamic_linking = false;
base.has_rpath = false;
base.position_independent_executables = false;

Target {
llvm_target: "i686-unknown-linux-musl".to_string(),
target_endian: "little".to_string(),
Expand Down
71 changes: 71 additions & 0 deletions src/librustc_back/target/linux_musl_base.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use target::TargetOptions;

pub fn opts() -> TargetOptions {
let mut base = super::linux_base::opts();

// Make sure that the linker/gcc really don't pull in anything, including
// default objects, libs, etc.
base.pre_link_args.push("-nostdlib".to_string());
base.pre_link_args.push("-static".to_string());

// At least when this was tested, the linker would not add the
// `GNU_EH_FRAME` program header to executables generated, which is required
// when unwinding to locate the unwinding information. I'm not sure why this
// argument is *not* necessary for normal builds, but it can't hurt!
base.pre_link_args.push("-Wl,--eh-frame-hdr".to_string());

// There's a whole bunch of circular dependencies when dealing with MUSL
// unfortunately. To put this in perspective libc is statically linked to
// liblibc and libunwind is statically linked to libstd:
//
// * libcore depends on `fmod` which is in libc (transitively in liblibc).
// liblibc, however, depends on libcore.
// * compiler-rt has personality symbols that depend on libunwind, but
// libunwind is in libstd which depends on compiler-rt.
//
// Recall that linkers discard libraries and object files as much as
// possible, and with all the static linking and archives flying around with
// MUSL the linker is super aggressively stripping out objects. For example
// the first case has fmod stripped from liblibc (it's in its own object
// file) so it's not there when libcore needs it. In the second example all
// the unused symbols from libunwind are stripped (each is in its own object
// file in libstd) before we end up linking compiler-rt which depends on
// those symbols.
//
// To deal with these circular dependencies we just force the compiler to
// link everything as a group, not stripping anything out until everything
// is processed. The linker will still perform a pass to strip out object
// files but it won't do so until all objects/archives have been processed.
base.pre_link_args.push("-Wl,-(".to_string());
base.post_link_args.push("-Wl,-)".to_string());

// When generating a statically linked executable there's generally some
// small setup needed which is listed in these files. These are provided by
// a musl toolchain and are linked by default by the `musl-gcc` script. Note
// that `gcc` also does this by default, it just uses some different files.
//
// Each target directory for musl has these object files included in it so
// they'll be included from there.
base.pre_link_objects_exe.push("crt1.o".to_string());
base.pre_link_objects_exe.push("crti.o".to_string());
base.post_link_objects.push("crtn.o".to_string());

// MUSL support doesn't currently include dynamic linking, so there's no
// need for dylibs or rpath business. Additionally `-pie` is incompatible
// with `-static`, so we can't pass `-pie`.
base.dynamic_linking = false;
base.has_rpath = false;
base.position_independent_executables = false;

base
}
1 change: 1 addition & 0 deletions src/librustc_back/target/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ mod bitrig_base;
mod dragonfly_base;
mod freebsd_base;
mod linux_base;
mod linux_musl_base;
mod openbsd_base;
mod netbsd_base;
mod solaris_base;
Expand Down
56 changes: 1 addition & 55 deletions src/librustc_back/target/x86_64_unknown_linux_musl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,64 +11,10 @@
use target::Target;

pub fn target() -> Target {
let mut base = super::linux_base::opts();
let mut base = super::linux_musl_base::opts();
base.cpu = "x86-64".to_string();
base.pre_link_args.push("-m64".to_string());

// Make sure that the linker/gcc really don't pull in anything, including
// default objects, libs, etc.
base.pre_link_args.push("-nostdlib".to_string());
base.pre_link_args.push("-static".to_string());

// At least when this was tested, the linker would not add the
// `GNU_EH_FRAME` program header to executables generated, which is required
// when unwinding to locate the unwinding information. I'm not sure why this
// argument is *not* necessary for normal builds, but it can't hurt!
base.pre_link_args.push("-Wl,--eh-frame-hdr".to_string());

// There's a whole bunch of circular dependencies when dealing with MUSL
// unfortunately. To put this in perspective libc is statically linked to
// liblibc and libunwind is statically linked to libstd:
//
// * libcore depends on `fmod` which is in libc (transitively in liblibc).
// liblibc, however, depends on libcore.
// * compiler-rt has personality symbols that depend on libunwind, but
// libunwind is in libstd which depends on compiler-rt.
//
// Recall that linkers discard libraries and object files as much as
// possible, and with all the static linking and archives flying around with
// MUSL the linker is super aggressively stripping out objects. For example
// the first case has fmod stripped from liblibc (it's in its own object
// file) so it's not there when libcore needs it. In the second example all
// the unused symbols from libunwind are stripped (each is in its own object
// file in libstd) before we end up linking compiler-rt which depends on
// those symbols.
//
// To deal with these circular dependencies we just force the compiler to
// link everything as a group, not stripping anything out until everything
// is processed. The linker will still perform a pass to strip out object
// files but it won't do so until all objects/archives have been processed.
base.pre_link_args.push("-Wl,-(".to_string());
base.post_link_args.push("-Wl,-)".to_string());

// When generating a statically linked executable there's generally some
// small setup needed which is listed in these files. These are provided by
// a musl toolchain and are linked by default by the `musl-gcc` script. Note
// that `gcc` also does this by default, it just uses some different files.
//
// Each target directory for musl has these object files included in it so
// they'll be included from there.
base.pre_link_objects_exe.push("crt1.o".to_string());
base.pre_link_objects_exe.push("crti.o".to_string());
base.post_link_objects.push("crtn.o".to_string());

// MUSL support doesn't currently include dynamic linking, so there's no
// need for dylibs or rpath business. Additionally `-pie` is incompatible
// with `-static`, so we can't pass `-pie`.
base.dynamic_linking = false;
base.has_rpath = false;
base.position_independent_executables = false;

Target {
llvm_target: "x86_64-unknown-linux-musl".to_string(),
target_endian: "little".to_string(),
Expand Down
139 changes: 96 additions & 43 deletions src/librustc_trans/back/link.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,14 +135,14 @@ pub fn build_link_meta(tcx: &TyCtxt,
return r;
}

pub fn get_linker(sess: &Session) -> (String, Command) {
pub fn get_linker(sess: &Session) -> (&str, Command) {
if let Some(ref linker) = sess.opts.cg.linker {
(linker.clone(), Command::new(linker))
(linker, Command::new(linker))
} else if sess.target.target.options.is_like_msvc {
("link.exe".to_string(), msvc::link_exe_cmd(sess))
("link.exe", msvc::link_exe_cmd(sess))
} else {
(sess.target.target.options.linker.clone(),
Command::new(&sess.target.target.options.linker))
let linker = &sess.target.target.options.linker;
(linker, Command::new(linker))
}
}

Expand Down Expand Up @@ -621,6 +621,67 @@ fn link_natively(sess: &Session, dylib: bool,
let (pname, mut cmd) = get_linker(sess);
cmd.env("PATH", command_path(sess));

let is_lld = pname.ends_with("lld");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Couldn't this end with lld.exe on Windows?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LLD on Windows also has versions called ld.lld.exe and lld-link.exe which have the flavor hard coded in.


if is_lld {
if sess.target.target.options.is_like_msvc {
cmd.args(&["-flavor", "link"]);
} else if sess.target.target.options.is_like_osx {
cmd.args(&["-flavor", "darwin"]);
} else {
cmd.args(&["-flavor", "gnu"]);
}
}

if sess.target.target.options.is_like_msvc {
let mut linker = MsvcLinker {
name: pname,
cmd: cmd,
sess: &sess,
};
link_natively_helper(&mut linker,
sess,
dylib,
objects,
out_filename,
trans,
outputs,
tmpdir);
} else {
let mut linker = GnuLinker {
name: pname,
cmd: cmd,
sess: &sess,
is_lld: is_lld,
};
link_natively_helper(&mut linker,
sess,
dylib,
objects,
out_filename,
trans,
outputs,
tmpdir);
}

// On OSX, debuggers need this utility to get run to do some munging of
// the symbols
if sess.target.target.options.is_like_osx && sess.opts.debuginfo != NoDebugInfo {
match Command::new("dsymutil").arg(out_filename).output() {
Ok(..) => {}
Err(e) => sess.fatal(&format!("failed to run dsymutil: {}", e)),
}
}
}

fn link_natively_helper<T: Linker>(cmd: &mut T,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was actually intentionally a trait object before as this is a serious wad of code to monomorphize, could it stay that way?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, how come this function was split out?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The proximate cause of this having to be generic was the generic arguments to Linker.arg{,s}.

This function was split out because I needed to put the bound on something.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah you can add where Self: Sized to those functions and then have a trait method of fn _arg(&mut self, s: &OsStr) or something like that (to keep the trait object safe)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I misspoke, the real cause is:

../rust/src/librustc_trans/back/link.rs:714:1: 852:2 note: method `arg` references the `Self` type in its arguments or return type
../rust/src/librustc_trans/back/link.rs:714:1: 852:2 note: method `args` references the `Self` type in its arguments or return type
../rust/src/librustc_trans/back/link.rs:865:1: 908:2 error: the trait `back::linker::Linker` cannot be made into an object [E0038]

sess: &Session,
dylib: bool,
objects: &[PathBuf],
out_filename: &Path,
trans: &CrateTranslation,
outputs: &OutputFilenames,
tmpdir: &Path) {
let root = sess.target_filesearch(PathKind::Native).get_lib_path();
cmd.args(&sess.target.target.options.pre_link_args);

Expand All @@ -633,18 +694,18 @@ fn link_natively(sess: &Session, dylib: bool,
cmd.arg(root.join(obj));
}

{
let mut linker = if sess.target.target.options.is_like_msvc {
Box::new(MsvcLinker { cmd: &mut cmd, sess: &sess }) as Box<Linker>
} else {
Box::new(GnuLinker { cmd: &mut cmd, sess: &sess }) as Box<Linker>
};
link_args(&mut *linker, sess, dylib, tmpdir,
objects, out_filename, trans, outputs);
if !sess.target.target.options.no_compiler_rt {
linker.link_staticlib("compiler-rt");
}
link_args(cmd,
sess,
dylib,
tmpdir,
objects,
out_filename,
trans,
outputs);
if !sess.target.target.options.no_compiler_rt {
cmd.link_staticlib("compiler-rt");
}

cmd.args(&sess.target.target.options.late_link_args);
for obj in &sess.target.target.options.post_link_objects {
cmd.arg(root.join(obj));
Expand Down Expand Up @@ -677,7 +738,7 @@ fn link_natively(sess: &Session, dylib: bool,
let mut output = prog.stderr.clone();
output.extend_from_slice(&prog.stdout);
sess.struct_err(&format!("linking with `{}` failed: {}",
pname,
cmd,
prog.status))
.note(&format!("{:?}", &cmd))
.note(&escape_string(&output[..]))
Expand All @@ -688,29 +749,19 @@ fn link_natively(sess: &Session, dylib: bool,
info!("linker stdout:\n{}", escape_string(&prog.stdout[..]));
},
Err(e) => {
sess.fatal(&format!("could not exec the linker `{}`: {}", pname, e));
}
}


// On OSX, debuggers need this utility to get run to do some munging of
// the symbols
if sess.target.target.options.is_like_osx && sess.opts.debuginfo != NoDebugInfo {
match Command::new("dsymutil").arg(out_filename).output() {
Ok(..) => {}
Err(e) => sess.fatal(&format!("failed to run dsymutil: {}", e)),
sess.fatal(&format!("could not exec the linker `{}`: {}", cmd, e));
}
}
}

fn link_args(cmd: &mut Linker,
sess: &Session,
dylib: bool,
tmpdir: &Path,
objects: &[PathBuf],
out_filename: &Path,
trans: &CrateTranslation,
outputs: &OutputFilenames) {
fn link_args<T: Linker>(cmd: &mut T,
sess: &Session,
dylib: bool,
tmpdir: &Path,
objects: &[PathBuf],
out_filename: &Path,
trans: &CrateTranslation,
outputs: &OutputFilenames) {

// The default library location, we need this to find the runtime.
// The location of crates will be determined as needed.
Expand Down Expand Up @@ -854,7 +905,7 @@ fn link_args(cmd: &mut Linker,
// Also note that the native libraries linked here are only the ones located
// in the current crate. Upstream crates with native library dependencies
// may have their native library pulled in above.
fn add_local_native_libraries(cmd: &mut Linker, sess: &Session) {
fn add_local_native_libraries<T: Linker>(cmd: &mut T, sess: &Session) {
sess.target_filesearch(PathKind::All).for_each_lib_search_path(|path, k| {
match k {
PathKind::Framework => { cmd.framework_path(path); }
Expand Down Expand Up @@ -904,8 +955,7 @@ fn add_local_native_libraries(cmd: &mut Linker, sess: &Session) {
// Rust crates are not considered at all when creating an rlib output. All
// dependencies will be linked when producing the final output (instead of
// the intermediate rlib version)
fn add_upstream_rust_crates(cmd: &mut Linker, sess: &Session,
dylib: bool, tmpdir: &Path) {
fn add_upstream_rust_crates<T: Linker>(cmd: &mut T, sess: &Session, dylib: bool, tmpdir: &Path) {
// All of the heavy lifting has previously been accomplished by the
// dependency_format module of the compiler. This is just crawling the
// output of that module, adding crates as necessary.
Expand Down Expand Up @@ -979,8 +1029,11 @@ fn add_upstream_rust_crates(cmd: &mut Linker, sess: &Session,
// (aka we're making an executable), we can just pass the rlib blindly to
// the linker (fast) because it's fine if it's not actually included as
// we're at the end of the dependency chain.
fn add_static_crate(cmd: &mut Linker, sess: &Session, tmpdir: &Path,
dylib: bool, cratepath: &Path) {
fn add_static_crate<T: Linker>(cmd: &mut T,
sess: &Session,
tmpdir: &Path,
dylib: bool,
cratepath: &Path) {
if !sess.lto() && !dylib {
cmd.link_rlib(&fix_windows_verbatim_for_gcc(cratepath));
return
Expand Down Expand Up @@ -1027,7 +1080,7 @@ fn add_upstream_rust_crates(cmd: &mut Linker, sess: &Session,
}

// Same thing as above, but for dynamic crates instead of static crates.
fn add_dynamic_crate(cmd: &mut Linker, sess: &Session, cratepath: &Path) {
fn add_dynamic_crate<T: Linker>(cmd: &mut T, sess: &Session, cratepath: &Path) {
// If we're performing LTO, then it should have been previously required
// that all upstream rust dependencies were available in an rlib format.
assert!(!sess.lto());
Expand Down Expand Up @@ -1062,7 +1115,7 @@ fn add_upstream_rust_crates(cmd: &mut Linker, sess: &Session,
// generic function calls a native function, then the generic function must
// be instantiated in the target crate, meaning that the native symbol must
// also be resolved in the target crate.
fn add_upstream_native_libraries(cmd: &mut Linker, sess: &Session) {
fn add_upstream_native_libraries<T: Linker>(cmd: &mut T, sess: &Session) {
// Be sure to use a topological sorting of crates because there may be
// interdependencies between native libraries. When passing -nodefaultlibs,
// for example, almost all native libraries depend on libc, so we have to
Expand Down
Loading