link_linux cleanup

This commit is contained in:
Anton-4 2024-02-28 12:33:34 +01:00
parent 660d2b2b25
commit bb0df1ef0a
No known key found for this signature in database
GPG key ID: 0971D718C0A9B937

View file

@ -812,15 +812,15 @@ fn nix_glibc_path_opt() -> Option<OsString> {
env::var_os("NIX_GLIBC_PATH") env::var_os("NIX_GLIBC_PATH")
} }
fn build_path<const N: usize>(segments: [&str; N]) -> Option<PathBuf> { fn build_path_or_panic<const N: usize>(segments: [&str; N]) -> PathBuf {
let mut guess_path = PathBuf::new(); let mut guess_path = PathBuf::new();
for s in segments { for s in segments {
guess_path.push(s); guess_path.push(s);
} }
if guess_path.exists() { if guess_path.exists() {
Some(guess_path) guess_path
} else { } else {
None panic!("{} does not exist.", guess_path.display());
} }
} }
@ -927,7 +927,7 @@ fn link_linux(
let scrt1_name = "Scrt1.o"; let scrt1_name = "Scrt1.o";
let scrt1_path = look_for_library(&lib_dirs, scrt1_name); let scrt1_path = look_for_library(&lib_dirs, scrt1_name);
// Unwrap all the paths at once so we can inform the user of all missing libs at once // Unwrap all the paths at once so we can inform the user of any missing libs
let (libgcc_path, crti_path, crtn_path, scrt1_path) = let (libgcc_path, crti_path, crtn_path, scrt1_path) =
match (libgcc_path, crti_path, crtn_path, scrt1_path) { match (libgcc_path, crti_path, crtn_path, scrt1_path) {
(Some(libgcc), Some(crti), Some(crtn), Some(scrt1)) => (libgcc, crti, crtn, scrt1), (Some(libgcc), Some(crti), Some(crtn), Some(scrt1)) => (libgcc, crti, crtn, scrt1),
@ -964,33 +964,40 @@ fn link_linux(
} }
}; };
let ld_linux = match target.architecture { let (libgcc_path_str, crti_path_str, crtn_path_str, scrt1_path_str) = (
libgcc_path.to_string_lossy(),
crti_path.to_string_lossy(),
crtn_path.to_string_lossy(),
scrt1_path.to_string_lossy(),
);
let ld_linux_path = match target.architecture {
Architecture::X86_64 => { Architecture::X86_64 => {
// give preference to nix_path if it's defined, this prevents bugs // give preference to nix_path if it's defined, this prevents bugs
if let Some(nix_glibc_path) = nix_glibc_path_opt() { if let Some(nix_glibc_path) = nix_glibc_path_opt() {
build_path([ build_path_or_panic([
&nix_glibc_path.into_string().unwrap(), &nix_glibc_path.into_string().unwrap(),
"ld-linux-x86-64.so.2", "ld-linux-x86-64.so.2",
]) ])
} else { } else {
build_path(["/lib64", "ld-linux-x86-64.so.2"]) build_path_or_panic(["/lib64", "ld-linux-x86-64.so.2"])
} }
} }
Architecture::Aarch64(_) => build_path(["/lib", "ld-linux-aarch64.so.1"]), Architecture::Aarch64(_) => build_path_or_panic(["/lib", "ld-linux-aarch64.so.1"]),
_ => internal_error!( _ => internal_error!(
"TODO gracefully handle unsupported linux architecture: {:?}", "TODO gracefully handle unsupported linux architecture: {:?}",
target.architecture target.architecture
), ),
}; };
let ld_linux = ld_linux.unwrap();
let ld_linux = ld_linux.to_str().unwrap(); let ld_linux_path_str = &ld_linux_path.to_string_lossy();
let mut soname; let mut soname;
let (base_args, output_path) = match link_type { let (base_args, output_path) = match link_type {
LinkType::Executable => ( LinkType::Executable => (
// Presumably this S stands for Static, since if we include Scrt1.o // Presumably this S stands for Static, since if we include Scrt1.o
// in the linking for dynamic builds, linking fails. // in the linking for dynamic builds, linking fails.
vec![scrt1_path.to_string_lossy().into_owned()], vec![scrt1_path_str.to_string()],
output_path, output_path,
), ),
LinkType::Dylib => { LinkType::Dylib => {
@ -1026,9 +1033,9 @@ fn link_linux(
// 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
let mut command = Command::new("ld"); let mut ld_command = Command::new("ld");
command ld_command
// Don't allow LD_ env vars to affect this // Don't allow LD_ env vars to affect this
.env_clear() .env_clear()
.env("PATH", &env_path) .env("PATH", &env_path)
@ -1044,11 +1051,11 @@ fn link_linux(
"-A", "-A",
arch_str(target), arch_str(target),
"-pie", "-pie",
&*crti_path.to_string_lossy(), &crti_path_str,
&*crtn_path.to_string_lossy(), &crtn_path_str,
]) ])
.args(&base_args) .args(&base_args)
.args(["-dynamic-linker", ld_linux]) .args(["-dynamic-linker", ld_linux_path_str])
.args(input_paths) .args(input_paths)
.args(extra_link_flags()) .args(extra_link_flags())
// ld.lld requires this argument, and does not accept --arch // ld.lld requires this argument, and does not accept --arch
@ -1063,16 +1070,16 @@ fn link_linux(
"-lrt", "-lrt",
"-lutil", "-lutil",
"-lc_nonshared", "-lc_nonshared",
libgcc_path.to_str().unwrap(), &libgcc_path_str,
// Output // Output
"-o", "-o",
output_path.as_path().to_str().unwrap(), // app (or app.so or app.dylib etc.) output_path.as_path().to_str().unwrap(), // app (or app.so or app.dylib etc.)
]); ]);
debug_print_command(&command); debug_print_command(&ld_command);
let output = command.spawn()?; let ld_output = ld_command.spawn()?;
Ok((output, output_path)) Ok((ld_output, output_path))
} }
fn link_macos( fn link_macos(