Skip to content

Instantly share code, notes, and snippets.

@skull-squadron
Last active March 18, 2023 02:36
Show Gist options
  • Save skull-squadron/1a0df1b7ff83809a7ecb5ade361750c3 to your computer and use it in GitHub Desktop.
Save skull-squadron/1a0df1b7ff83809a7ecb5ade361750c3 to your computer and use it in GitHub Desktop.
Linux utility: Remove hard-coded absolute library paths and set a single DT_RUNPATH
#!/usr/bin/env bash
set -euo pipefail
TGT="$1"
NEW_RUNPATH="${2:-\$ORIGIN/../lib}"
_patchelf() {
(set -x; patchelf "$@")
}
_patchelf --remove-rpath "$TGT"
_patchelf --set-rpath "$NEW_RUNPATH" "$TGT"
readelf -d "$TGT" | awk '/Shared lib.*\//{sub(".*\\[","");sub("\\]","");print}' | while read -r LIB; do
SHORT_LIB="${LIB##*/}"
_patchelf --replace-needed "$LIB" "$SHORT_LIB" "$TGT"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment