Don't link Scrti.o when doing dynamic linking

This commit is contained in:
Richard Feldman 2020-10-25 01:08:03 -04:00
parent ac46d84c60
commit 14c8a00f9d
2 changed files with 32 additions and 24 deletions

View file

@ -125,9 +125,28 @@ fn link_linux(
input_paths: &[&str], input_paths: &[&str],
link_type: LinkType, link_type: LinkType,
) -> io::Result<(Child, PathBuf)> { ) -> io::Result<(Child, PathBuf)> {
let libcrt_path = if Path::new("/usr/lib/x86_64-linux-gnu").exists() {
Path::new("/usr/lib/x86_64-linux-gnu")
} else {
Path::new("/usr/lib")
};
let libgcc_path = if Path::new("/lib/x86_64-linux-gnu/libgcc_s.so.1").exists() {
Path::new("/lib/x86_64-linux-gnu/libgcc_s.so.1")
} else if Path::new("/usr/lib/x86_64-linux-gnu/libgcc_s.so.1").exists() {
Path::new("/usr/lib/x86_64-linux-gnu/libgcc_s.so.1")
} else {
Path::new("/usr/lib/libgcc_s.so.1")
};
let mut soname; let mut soname;
let (base_args, output_path) = match link_type { let (base_args, output_path) = match link_type {
LinkType::Executable => (Vec::new(), output_path), LinkType::Executable => (
// Presumably this S stands for Static, since if we include Scrt1.o
// in the linking for dynamic builds, linking fails.
vec![libcrt_path.join("Scrt1.o").to_str().unwrap().to_string()],
output_path,
),
LinkType::Dylib => { LinkType::Dylib => {
// TODO: do we acually need the version number on this? // TODO: do we acually need the version number on this?
// Do we even need the "-soname" argument? // Do we even need the "-soname" argument?
@ -144,42 +163,31 @@ fn link_linux(
( (
// TODO: find a way to avoid using a vec! here - should theoretically be // TODO: find a way to avoid using a vec! here - should theoretically be
// able to do this somehow using &[] but the borrow checker isn't having it. // able to do this somehow using &[] but the borrow checker isn't having it.
vec!["-shared", "-soname", soname.as_path().to_str().unwrap()], // Also find a way to have these be string slices instead of Strings.
vec![
"-shared".to_string(),
"-soname".to_string(),
soname.as_path().to_str().unwrap().to_string(),
],
output_path, output_path,
) )
} }
}; };
let libcrt_path = if Path::new("/usr/lib/x86_64-linux-gnu").exists() {
Path::new("/usr/lib/x86_64-linux-gnu")
} else {
Path::new("/usr/lib")
};
let libgcc_path = if Path::new("/lib/x86_64-linux-gnu/libgcc_s.so.1").exists() {
Path::new("/lib/x86_64-linux-gnu/libgcc_s.so.1")
} else if Path::new("/usr/lib/x86_64-linux-gnu/libgcc_s.so.1").exists() {
Path::new("/usr/lib/x86_64-linux-gnu/libgcc_s.so.1")
} else {
Path::new("/usr/lib/libgcc_s.so.1")
};
// NOTE: order of arguments to `ld` matters here! // NOTE: order of arguments to `ld` matters here!
// The `-l` flags should go after the `.o` arguments // The `-l` flags should go after the `.o` arguments
Ok(( Ok((
Command::new("ld") Command::new("ld")
// Don't allow LD_ env vars to affect this // Don't allow LD_ env vars to affect this
.env_clear() .env_clear()
.args(&base_args)
.args(&[ .args(&[
"-arch", "-arch",
arch_str(target), arch_str(target),
libcrt_path.join("crti.o").to_str().unwrap(), libcrt_path.join("crti.o").to_str().unwrap(),
libcrt_path.join("crtn.o").to_str().unwrap(), libcrt_path.join("crtn.o").to_str().unwrap(),
libcrt_path.join("Scrt1.o").to_str().unwrap(),
"-dynamic-linker",
"/lib64/ld-linux-x86-64.so.2",
]) ])
.args(&base_args)
.args(&["-dynamic-linker", "/lib64/ld-linux-x86-64.so.2"])
.args(input_paths) .args(input_paths)
.args(&[ .args(&[
// Libraries - see https://github.com/rtfeldman/roc/pull/554#discussion_r496365925 // Libraries - see https://github.com/rtfeldman/roc/pull/554#discussion_r496365925

View file

@ -68,7 +68,7 @@ mod gen_num {
indoc!( indoc!(
r#" r#"
limitedNegate = \num -> limitedNegate = \num ->
x = x =
if num == 1 then if num == 1 then
-1 -1
else if num == -1 then else if num == -1 then
@ -482,7 +482,7 @@ mod gen_num {
assert_evals_to!( assert_evals_to!(
indoc!( indoc!(
r#" r#"
wrapper = \{} -> wrapper = \{} ->
when 10 is when 10 is
x if x == 5 -> 0 x if x == 5 -> 0
_ -> 42 _ -> 42
@ -500,7 +500,7 @@ mod gen_num {
assert_evals_to!( assert_evals_to!(
indoc!( indoc!(
r#" r#"
wrapper = \{} -> wrapper = \{} ->
when 10 is when 10 is
x if x == 10 -> 42 x if x == 10 -> 42
_ -> 0 _ -> 0
@ -750,7 +750,7 @@ mod gen_num {
assert_evals_to!( assert_evals_to!(
indoc!( indoc!(
r#" r#"
when Num.addChecked 1.0 0.0 is when Num.addChecked 1.0 0.0 is
Ok v -> v Ok v -> v
Err Overflow -> -1.0 Err Overflow -> -1.0
"# "#