From 25937bedb946ce845ec70c9e77d3be20f23f46b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rodolphe=20de=20Saint=20L=C3=A9ger?= Date: Fri, 11 Oct 2024 14:10:37 +0200 Subject: [PATCH] Relocate interpreter to match libc --- pkg/relocator/relocator.go | 34 +++++++++++++++++++++++++++++----- 1 file changed, 29 insertions(+), 5 deletions(-) diff --git a/pkg/relocator/relocator.go b/pkg/relocator/relocator.go index 931839c..29c49e1 100644 --- a/pkg/relocator/relocator.go +++ b/pkg/relocator/relocator.go @@ -111,19 +111,43 @@ func relocateELFBinary(prefix, dest, src string) error { } } + var interpreter string + // Get the interpreter using "patchelf --print-interpreter" + interpOut, err := exec.Command("patchelf", "--print-interpreter", src).Output() + if err == nil { + interpreter = strings.TrimSpace(string(interpOut)) // Trim any whitespace or newlines + + RelocateFile(prefix, interpreter) + } else { + interpreter = "" + } + // Aggregate the directories into a colon-separated rpath var dirList []string for dir := range dirSet { dirList = append(dirList, filepath.Join(prefix, dir)) } - rpath := strings.Join(dirList, ":") - // Update the ELF binary's rpath using patchelf - if _, err := exec.Command("patchelf", "--set-rpath", rpath, dest).Output(); err != nil { - return fmt.Errorf("failed to update rpath for %s: %v", dest, err) + // If an interpreter was found, relocate it + if interpreter != "" { + newInterpPath := filepath.Join(prefix, interpreter) + if _, err := exec.Command("patchelf", "--set-interpreter", newInterpPath, dest).Output(); err != nil { + return fmt.Errorf("failed to update interpreter for %s: %v", dest, err) + } + fmt.Printf("Relocated interpreter from %s to %s\n", interpreter, newInterpPath) + } + + rpath := strings.Join(dirList, ":") + + if rpath != "" { + // Update the ELF binary's rpath using patchelf + if _, err := exec.Command("patchelf", "--set-rpath", rpath, dest).Output(); err != nil { + return fmt.Errorf("failed to update rpath for %s: %v", dest, err) + } + + fmt.Printf("Relocated binary %s to %s with rpath: %s\n", src, dest, rpath) } - fmt.Printf("Relocated binary %s to %s with rpath: %s\n", src, dest, rpath) return nil }