Relocate interpreter to match libc
This commit is contained in:
parent
5ed259c54c
commit
25937bedb9
1 changed files with 29 additions and 5 deletions
|
|
@ -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
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue