From 2c4c9e092397402c559c756375a4e14337c60918 Mon Sep 17 00:00:00 2001 From: Levente Polyak Date: Sat, 30 Dec 2023 21:57:19 +0100 Subject: [PATCH] fix(patchelf): run once per option to avoid broken offsets/symbols Fixes #297 --- src/patch_bin.rs | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/src/patch_bin.rs b/src/patch_bin.rs index 3e65cff..fc4e95d 100644 --- a/src/patch_bin.rs +++ b/src/patch_bin.rs @@ -44,26 +44,35 @@ const LIBC_FILE_NAME: &str = "libc.so.6"; /// /// If `opts` has a libc, make its directory the RPATH of the binary. /// If `opts` has a linker, make it the interpreter of the binary. +/// +/// Run `patchelf` once per option to avoid broken offsets/symbols (#297) fn run_patchelf(bin: &Path, opts: &Opts) -> Result<()> { println!( "{}", format!("running patchelf on {}", bin.to_string_lossy().bold()).green() ); - let mut cmd = Command::new("patchelf"); - cmd.arg(bin); if let Some(lib_dir) = opts .libc .as_ref() // Prepend "." in case `libc`'s `parent()` is an empty path. .and_then(|libc| Path::new(".").join(libc).parent().map(Path::to_path_buf)) { - cmd.arg("--set-rpath").arg(lib_dir); + run_patchelf_option(bin, "--set-rpath", &lib_dir)?; }; if let Some(ld) = &opts.ld { - cmd.arg("--set-interpreter").arg(ld); + run_patchelf_option(bin, "--set-interpreter", ld)?; }; + Ok(()) +} + +/// Run `patchelf` on the binary `bin` using the option `option` with the path `argument`. +fn run_patchelf_option(bin: &Path, option: &str, argument: &PathBuf) -> Result<()> { + let mut cmd = Command::new("patchelf"); + cmd.arg(bin); + cmd.arg(option).arg(argument); + let status = cmd.status().context(PatchelfExecSnafu)?; if status.success() { Ok(())