Skip to content

Commit

Permalink
fix(patchelf): run once per option to avoid broken offsets/symbols
Browse files Browse the repository at this point in the history
Fixes #297
  • Loading branch information
anthraxx committed Dec 30, 2023
1 parent e9aab76 commit 2c4c9e0
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions src/patch_bin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
Expand Down

0 comments on commit 2c4c9e0

Please sign in to comment.