{"id":5680,"library":"patchelf","title":"patchelf","description":"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.","status":"active","version":"0.17.2.4","language":"en","source_language":"en","source_url":"https://github.com/NixOS/patchelf","tags":["cli","elf","linker","rpath","binary-patching","system-utilities"],"install":[{"cmd":"pip install patchelf","lang":"bash","label":"Install patchelf"}],"dependencies":[],"imports":[{"note":"The 'patchelf' package on PyPI primarily provides the command-line utility, not a Python library API for direct import and manipulation. Its functionality is accessed by executing the 'patchelf' binary via subprocess calls.","symbol":"CLI tool","correct":"import subprocess\nsubprocess.run(['patchelf', '--print-rpath', '/path/to/executable'], check=True)"}],"quickstart":{"code":"import subprocess\nimport os\n\n# Create a dummy executable for demonstration (requires clang/gcc)\n# In a real scenario, you'd use an existing ELF binary.\nwith open('my_test_program.c', 'w') as f:\n    f.write('int main() { return 0; }')\nsubprocess.run(['gcc', '-o', 'my_executable', 'my_test_program.c'], check=True)\n\nprint(\"Original RPATH:\")\ntry:\n    result = subprocess.run(['patchelf', '--print-rpath', './my_executable'], capture_output=True, text=True, check=True)\n    print(result.stdout.strip())\nexcept subprocess.CalledProcessError as e:\n    if 'has no RPATH' in e.stderr:\n        print('No RPATH found.')\n    else:\n        raise\n\nprint(\"\\nSetting RPATH to '$ORIGIN/lib'\")\nsubprocess.run(['patchelf', '--set-rpath', '$ORIGIN/lib', './my_executable'], check=True)\n\nprint(\"\\nNew RPATH:\")\nresult = subprocess.run(['patchelf', '--print-rpath', './my_executable'], capture_output=True, text=True, check=True)\nprint(result.stdout.strip())\n\n# Clean up dummy files\nos.remove('my_test_program.c')\nos.remove('my_executable')","lang":"python","description":"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."},"warnings":[{"fix":"Always use `subprocess.run()` to invoke the `patchelf` command-line tool within your Python scripts. Avoid searching for direct Python imports or classes within the `patchelf` package.","message":"The `patchelf` package on PyPI provides the command-line utility, not a direct Python API for ELF manipulation. Interaction with ELF files should be done by executing the `patchelf` binary via `subprocess.run()`.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Refer to the GitHub releases page for `patchelf` to understand the specific changes and backports included in each version. For critical applications, ensure you are using the latest stable release that includes all necessary security and stability fixes.","message":"The project actively maintains multiple release branches (e.g., 0.15.x, 0.17.x, 0.18.x). Bug fixes and minor features are often backported to older maintenance branches, meaning a seemingly 'older' minor version might contain recent critical fixes.","severity":"gotcha","affected_versions":"0.15.x, 0.17.x, 0.18.x and later"},{"fix":"Always back up your ELF files before attempting any modifications with `patchelf`. Thoroughly test modified binaries in a controlled environment to ensure their integrity and functionality.","message":"Directly modifying ELF executables is a low-level and potentially destructive operation. Incorrect usage can corrupt binaries, leading to crashes, unexpected behavior, or rendering them unexecutable.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Upgrade to at least `patchelf 0.17.1` (for `modifySoname` fixes) or `patchelf 0.15.5` (for `modifySoname` backports and general stability fixes). If modifying RPATH, ensure at least `0.15.3` or newer is used.","message":"Several critical out-of-bounds read/write vulnerabilities in functions like `modifySoname` and `modifyRPath` have been fixed in recent maintenance and feature releases. Older versions are susceptible to silent corruption or crashes.","severity":"breaking","affected_versions":"<=0.17.0, <=0.15.4"}],"env_vars":null,"last_verified":"2026-04-09T00:00:00.000Z","next_check":"2026-07-08T00:00:00.000Z"}