From 698f8c2f01ea549d77d7dc3338a12e04c11057b9 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Wed, 17 Apr 2024 14:02:58 +0200 Subject: Adding upstream version 1.64.0+dfsg1. Signed-off-by: Daniel Baumann --- src/tools/lld-wrapper/Cargo.toml | 5 ++ src/tools/lld-wrapper/src/main.rs | 106 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 111 insertions(+) create mode 100644 src/tools/lld-wrapper/Cargo.toml create mode 100644 src/tools/lld-wrapper/src/main.rs (limited to 'src/tools/lld-wrapper') diff --git a/src/tools/lld-wrapper/Cargo.toml b/src/tools/lld-wrapper/Cargo.toml new file mode 100644 index 000000000..bf5138b16 --- /dev/null +++ b/src/tools/lld-wrapper/Cargo.toml @@ -0,0 +1,5 @@ +[package] +name = "lld-wrapper" +version = "0.1.0" +edition = "2021" +license = "MIT OR Apache-2.0" diff --git a/src/tools/lld-wrapper/src/main.rs b/src/tools/lld-wrapper/src/main.rs new file mode 100644 index 000000000..90bd24a75 --- /dev/null +++ b/src/tools/lld-wrapper/src/main.rs @@ -0,0 +1,106 @@ +//! Script to invoke the bundled rust-lld with the correct flavor. +//! +//! lld supports multiple command line interfaces. If `-flavor ` are passed as the first +//! two arguments the `` command line interface is used to process the remaining arguments. +//! If no `-flavor` argument is present the flavor is determined by the executable name. +//! +//! In Rust with `-Z gcc-ld=lld` we have gcc or clang invoke rust-lld. Since there is no way to +//! make gcc/clang pass `-flavor ` as the first two arguments in the linker invocation +//! and since Windows does not support symbolic links for files this wrapper is used in place of a +//! symbolic link. It execs `../rust-lld -flavor ` by propagating the flavor argument +//! passed to the wrapper as the first two arguments. On Windows it spawns a `..\rust-lld.exe` +//! child process. + +use std::fmt::Display; +use std::path::{Path, PathBuf}; +use std::{env, process}; + +trait UnwrapOrExitWith { + fn unwrap_or_exit_with(self, context: &str) -> T; +} + +impl UnwrapOrExitWith for Option { + fn unwrap_or_exit_with(self, context: &str) -> T { + self.unwrap_or_else(|| { + eprintln!("lld-wrapper: {}", context); + process::exit(1); + }) + } +} + +impl UnwrapOrExitWith for Result { + fn unwrap_or_exit_with(self, context: &str) -> T { + self.unwrap_or_else(|err| { + eprintln!("lld-wrapper: {}: {}", context, err); + process::exit(1); + }) + } +} + +/// Returns the path to rust-lld in the parent directory. +/// +/// Exits if the parent directory cannot be determined. +fn get_rust_lld_path(current_exe_path: &Path) -> PathBuf { + let mut rust_lld_exe_name = "rust-lld".to_owned(); + rust_lld_exe_name.push_str(env::consts::EXE_SUFFIX); + let mut rust_lld_path = current_exe_path + .parent() + .unwrap_or_exit_with("directory containing current executable could not be determined") + .parent() + .unwrap_or_exit_with("parent directory could not be determined") + .to_owned(); + rust_lld_path.push(rust_lld_exe_name); + rust_lld_path +} + +/// Returns the command for invoking rust-lld with the correct flavor. +/// LLD only accepts the flavor argument at the first two arguments, so move it there. +/// +/// Exits on error. +fn get_rust_lld_command(current_exe_path: &Path) -> process::Command { + let rust_lld_path = get_rust_lld_path(current_exe_path); + let mut command = process::Command::new(rust_lld_path); + + let mut flavor = None; + let args = env::args_os() + .skip(1) + .filter(|arg| match arg.to_str().and_then(|s| s.strip_prefix("-rustc-lld-flavor=")) { + Some(suffix) => { + flavor = Some(suffix.to_string()); + false + } + None => true, + }) + .collect::>(); + + command.arg("-flavor"); + command.arg(flavor.unwrap_or_exit_with("-rustc-lld-flavor= is not passed")); + command.args(args); + command +} + +#[cfg(unix)] +fn exec_lld(mut command: process::Command) { + use std::os::unix::prelude::CommandExt; + Result::<(), _>::Err(command.exec()).unwrap_or_exit_with("could not exec rust-lld"); + unreachable!("lld-wrapper: after exec without error"); +} + +#[cfg(not(unix))] +fn exec_lld(mut command: process::Command) { + // Windows has no exec(), spawn a child process and wait for it. + let exit_status = command.status().unwrap_or_exit_with("error running rust-lld child process"); + let code = exit_status + .code() + .ok_or(exit_status) + .unwrap_or_exit_with("rust-lld child process exited with error"); + // Return the original lld exit code. + process::exit(code); +} + +fn main() { + let current_exe_path = + env::current_exe().unwrap_or_exit_with("could not get the path of the current executable"); + + exec_lld(get_rust_lld_command(current_exe_path.as_ref())); +} -- cgit v1.2.3