Relocate interpreter to match libc

This commit is contained in:
Rodolphe de Saint Léger 2024-10-11 14:10:37 +02:00
parent 5ed259c54c
commit 25937bedb9

View file

@ -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 // Aggregate the directories into a colon-separated rpath
var dirList []string var dirList []string
for dir := range dirSet { for dir := range dirSet {
dirList = append(dirList, filepath.Join(prefix, dir)) dirList = append(dirList, filepath.Join(prefix, dir))
} }
// 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, ":") rpath := strings.Join(dirList, ":")
if rpath != "" {
// Update the ELF binary's rpath using patchelf // Update the ELF binary's rpath using patchelf
if _, err := exec.Command("patchelf", "--set-rpath", rpath, dest).Output(); err != nil { if _, err := exec.Command("patchelf", "--set-rpath", rpath, dest).Output(); err != nil {
return fmt.Errorf("failed to update rpath for %s: %v", dest, err) 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 return nil
} }