Skip to content

Instantly share code, notes, and snippets.

@rgov
Last active January 2, 2024 22:41
Show Gist options
  • Select an option

  • Save rgov/c1702279a18cac2eac8662445a4da80d to your computer and use it in GitHub Desktop.

Select an option

Save rgov/c1702279a18cac2eac8662445a4da80d to your computer and use it in GitHub Desktop.

Revisions

  1. rgov revised this gist Jan 2, 2024. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion patch_load_paths.sh
    Original file line number Diff line number Diff line change
    @@ -17,7 +17,7 @@ process_binary() {

    # When empty, skip the loop to avoid a Bash unbound variable error
    if [ ${#libraries[@]} -le 0 ]; then
    continue
    return
    fi

    for lib in "${libraries[@]}"; do
  2. rgov created this gist Jan 1, 2024.
    42 changes: 42 additions & 0 deletions patch_load_paths.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,42 @@
    #!/bin/bash -eu

    process_binary() {
    local binary="$1"
    echo "[*] Processing $binary..."

    # Find all linked libraries from the Homebrew prefix
    local libraries=(
    $(otool -L "$binary" | grep '/usr/local/opt/' | awk '{print $1}')
    )

    # If this is a dylib, the first entry is the ID of the library itself, which
    # we don't really care about.
    if [ "${binary##*.}" = "dylib" ]; then
    libraries=("${libraries[@]:1}")
    fi

    # When empty, skip the loop to avoid a Bash unbound variable error
    if [ ${#libraries[@]} -le 0 ]; then
    continue
    fi

    for lib in "${libraries[@]}"; do
    local lib_basename=$(basename "$lib")

    # Check that we have a copy of each linked library
    if [ ! -f "$lib_basename" ]; then
    echo " [-] Failed to find linked library $lib_basename"
    continue
    fi

    # Change the path to be relative
    install_name_tool -change "$lib" "@executable_path/$lib_basename" \
    "$binary"
    echo " [+] Made relative link to $lib_basename"
    done
    }


    for binary in *.dylib $(find . -type f -perm +111); do
    process_binary "$binary"
    done