patchelf

0.17.2.4 · active · verified Thu Apr 09

Patchelf is a small utility for modifying existing ELF executables and libraries. It can change the dynamic linker (interpreter) of executables and adjust the RPATH/RUNPATH, which specifies directories where the dynamic linker should look for shared libraries. The current version is 0.17.2.4. The project has an active development cycle, often maintaining multiple parallel release branches for bug fixes and new features, with frequent backports.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use the `patchelf` command-line utility from Python to modify the RPATH of an ELF executable. It first creates a dummy executable (requires a C compiler like GCC or Clang), prints its original RPATH (if any), sets a new RPATH, and then prints the modified RPATH.

import subprocess
import os

# Create a dummy executable for demonstration (requires clang/gcc)
# In a real scenario, you'd use an existing ELF binary.
with open('my_test_program.c', 'w') as f:
    f.write('int main() { return 0; }')
subprocess.run(['gcc', '-o', 'my_executable', 'my_test_program.c'], check=True)

print("Original RPATH:")
try:
    result = subprocess.run(['patchelf', '--print-rpath', './my_executable'], capture_output=True, text=True, check=True)
    print(result.stdout.strip())
except subprocess.CalledProcessError as e:
    if 'has no RPATH' in e.stderr:
        print('No RPATH found.')
    else:
        raise

print("\nSetting RPATH to '$ORIGIN/lib'")
subprocess.run(['patchelf', '--set-rpath', '$ORIGIN/lib', './my_executable'], check=True)

print("\nNew RPATH:")
result = subprocess.run(['patchelf', '--print-rpath', './my_executable'], capture_output=True, text=True, check=True)
print(result.stdout.strip())

# Clean up dummy files
os.remove('my_test_program.c')
os.remove('my_executable')

view raw JSON →