Skip to content

Commit ca34d07

Browse files
authored
fix(patchelf): run once per option to avoid broken offsets/symbols (#299)
Fixes #297
1 parent e9aab76 commit ca34d07

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

src/patch_bin.rs

+13-4
Original file line numberDiff line numberDiff line change
@@ -44,26 +44,35 @@ const LIBC_FILE_NAME: &str = "libc.so.6";
4444
///
4545
/// If `opts` has a libc, make its directory the RPATH of the binary.
4646
/// If `opts` has a linker, make it the interpreter of the binary.
47+
///
48+
/// Run `patchelf` once per option to avoid broken offsets/symbols (#297)
4749
fn run_patchelf(bin: &Path, opts: &Opts) -> Result<()> {
4850
println!(
4951
"{}",
5052
format!("running patchelf on {}", bin.to_string_lossy().bold()).green()
5153
);
5254

53-
let mut cmd = Command::new("patchelf");
54-
cmd.arg(bin);
5555
if let Some(lib_dir) = opts
5656
.libc
5757
.as_ref()
5858
// Prepend "." in case `libc`'s `parent()` is an empty path.
5959
.and_then(|libc| Path::new(".").join(libc).parent().map(Path::to_path_buf))
6060
{
61-
cmd.arg("--set-rpath").arg(lib_dir);
61+
run_patchelf_option(bin, "--set-rpath", &lib_dir)?;
6262
};
6363
if let Some(ld) = &opts.ld {
64-
cmd.arg("--set-interpreter").arg(ld);
64+
run_patchelf_option(bin, "--set-interpreter", ld)?;
6565
};
6666

67+
Ok(())
68+
}
69+
70+
/// Run `patchelf` on the binary `bin` using the option `option` with the path `argument`.
71+
fn run_patchelf_option(bin: &Path, option: &str, argument: &PathBuf) -> Result<()> {
72+
let mut cmd = Command::new("patchelf");
73+
cmd.arg(bin);
74+
cmd.arg(option).arg(argument);
75+
6776
let status = cmd.status().context(PatchelfExecSnafu)?;
6877
if status.success() {
6978
Ok(())

0 commit comments

Comments
 (0)