### Install Executable Source: https://github.com/nixos/patchelf/blob/master/src/CMakeLists.txt Installs the compiled patchelf executable to the standard binary directory. ```cmake install(TARGETS patchelf RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}) ``` -------------------------------- ### Build with Meson and Ninja Source: https://github.com/nixos/patchelf/blob/master/README.md Build process using Meson with the Ninja generator. ```console mkdir build meson configure build cd build ninja all sudo ninja install ``` -------------------------------- ### Build with CMake and Ninja Source: https://github.com/nixos/patchelf/blob/master/README.md Build process using CMake with the Ninja generator. ```console mkdir build cd build cmake .. -GNinja ninja all sudo ninja install ``` -------------------------------- ### Manage Dependencies Source: https://github.com/nixos/patchelf/blob/master/README.md Remove, add, or replace DT_NEEDED entries for dynamic libraries. ```console $ patchelf --remove-needed libfoo.so.1 my-program ``` ```console $ patchelf --add-needed libfoo.so.1 my-program ``` ```console $ patchelf --replace-needed liboriginal.so.1 libreplacement.so.1 my-program ``` -------------------------------- ### Build with Autotools Source: https://github.com/nixos/patchelf/blob/master/README.md Standard build sequence for GNU Autotools projects. ```console ./bootstrap.sh ./configure make make check sudo make install ``` -------------------------------- ### Set RPATH/RUNPATH Source: https://context7.com/nixos/patchelf/llms.txt Configure the library search path for an executable. Use $ORIGIN for relative paths or --force-rpath to use the obsolete DT_RPATH instead of DT_RUNPATH. ```bash # Print current RPATH/RUNPATH patchelf --print-rpath my-program # Output: /usr/local/lib:/opt/mylibs # Set a new RPATH (replaces existing) patchelf --set-rpath '/opt/my-libs/lib:/other-libs' my-program # Use $ORIGIN for relocatable binaries (path relative to executable) patchelf --set-rpath '$ORIGIN/../lib:$ORIGIN/../lib64' my-program # Set multiple library search paths patchelf --set-rpath '/app/lib:/app/vendor/lib:/usr/local/lib' my-program # Force use of obsolete DT_RPATH instead of DT_RUNPATH # (DT_RPATH affects child dependencies, DT_RUNPATH doesn't) patchelf --force-rpath --set-rpath '/opt/legacy/lib' my-program # Read rpath from file echo '/opt/generated/lib:/usr/lib' > rpath.txt patchelf --set-rpath @rpath.txt my-program ``` -------------------------------- ### Set Compile Definitions Source: https://github.com/nixos/patchelf/blob/master/src/CMakeLists.txt Adds preprocessor definitions to the patchelf target, including page size and package version string. ```cmake target_compile_definitions( patchelf PRIVATE PAGESIZE=${PAGESIZE} PACKAGE_STRING="patchelf ${VERSION_STRING}") ``` -------------------------------- ### Change SONAME Source: https://github.com/nixos/patchelf/blob/master/README.md Update the SONAME of a dynamic library. ```console $ patchelf --set-soname libnewname.so.3.4.5 path/to/libmylibrary.so.1.2.3 ``` -------------------------------- ### Print rpath Source: https://context7.com/nixos/patchelf/llms.txt Prints the runtime search path (RPATH) of an executable. An empty output indicates no RPATH is set. ```bash patchelf --print-rpath my-program ``` -------------------------------- ### Control Default Library Search Paths Source: https://context7.com/nixos/patchelf/llms.txt Marks an object to ignore default library search paths, making it rely solely on RPATH/RUNPATH and LD_LIBRARY_PATH. This is useful for creating fully self-contained applications. ```bash # Mark binary to ignore default library paths patchelf --no-default-lib my-program ``` ```bash patchelf --set-rpath '$ORIGIN/lib' --no-default-lib my-app ``` -------------------------------- ### Set C++ Standard and Extensions Source: https://github.com/nixos/patchelf/blob/master/src/CMakeLists.txt Configures the C++ standard to C++17 and disables language extensions for stricter compliance. ```cmake set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_EXTENSIONS OFF) ``` -------------------------------- ### Add Executable Target Source: https://github.com/nixos/patchelf/blob/master/src/CMakeLists.txt Defines the main executable for the project, linking source files. ```cmake add_executable(${PROJECT_NAME} patchelf.cc elf.h patchelf.h) ``` -------------------------------- ### Enable Debug Mode Source: https://context7.com/nixos/patchelf/llms.txt Enables verbose output detailing the modifications being made to the ELF file. This is helpful for troubleshooting and understanding the tool's behavior. Debug mode can also be enabled via the PATCHELF_DEBUG environment variable. ```bash # Enable debug output patchelf --debug --set-rpath /opt/lib my-program ``` ```bash # Can also be enabled via environment variable PATCHELF_DEBUG=1 patchelf --set-interpreter /lib/ld.so my-program ``` -------------------------------- ### Set ELF Interpreter Source: https://context7.com/nixos/patchelf/llms.txt Modify the dynamic loader path for an executable. Use the @ prefix to read the path from a file or --output to save to a new file. ```bash # Print current interpreter patchelf --print-interpreter my-program # Output: /lib64/ld-linux-x86-64.so.2 # Set a new interpreter patchelf --set-interpreter /opt/custom-glibc/lib/ld-linux-x86-64.so.2 my-program # Set interpreter with a very long path (patchelf handles resizing automatically) patchelf --set-interpreter /very/long/path/to/custom/lib64/ld-linux-x86-64.so.2 my-program # Use --output to preserve the original file patchelf --set-interpreter /lib/my-ld-linux.so.2 --output my-program-patched my-program # Read interpreter path from a file using @ prefix echo "/opt/custom/lib/ld-linux.so.2" > interpreter.txt patchelf --set-interpreter @interpreter.txt my-program ``` -------------------------------- ### Define Page Size Source: https://github.com/nixos/patchelf/blob/master/src/CMakeLists.txt Sets a constant for page size, likely used in memory management or file operations. ```cmake set(PAGESIZE 4096) ``` -------------------------------- ### Set OS ABI Source: https://context7.com/nixos/patchelf/llms.txt Changes the OS ABI field (EI_OSABI) in the ELF header, affecting how the operating system loads and interprets the binary. Supported values include System V, Linux, FreeBSD, NetBSD, OpenBSD, Solaris, and others. ```bash # Print current OS ABI patchelf --print-os-abi my-program ``` ```bash # Set OS ABI to System V (generic UNIX) patchelf --set-os-abi "System V" my-program ``` ```bash # Set to specific operating system patchelf --set-os-abi Linux my-program patchelf --set-os-abi FreeBSD my-program patchelf --set-os-abi NetBSD my-program patchelf --set-os-abi OpenBSD my-program patchelf --set-os-abi Solaris my-program ``` ```bash # Case-insensitive matching works patchelf --set-os-abi linux my-program patchelf --set-os-abi LINUX my-program ``` -------------------------------- ### Set Interpreter Source: https://context7.com/nixos/patchelf/llms.txt Changes the dynamic loader (ELF interpreter) of an executable. This is useful for relocating binaries or using custom library paths. ```APIDOC ## Set Interpreter ### Description Changes the dynamic loader (ELF interpreter) of an executable. This is useful when you need to use a different C library or dynamic linker, such as when relocating binaries or using custom library paths. The interpreter is the program that loads and prepares an executable for running. ### Method `patchelf` ### Parameters #### Command Line Arguments - `--print-interpreter` - Prints the current interpreter path. - `--set-interpreter ` - Sets a new interpreter path. Can be a direct path or a file path prefixed with `@`. - `--output ` - Specifies an output file to preserve the original binary. ### Request Example ```bash # Print current interpreter patchelf --print-interpreter my-program # Set a new interpreter patchelf --set-interpreter /opt/custom-glibc/lib/ld-linux-x86-64.so.2 my-program # Set interpreter with a very long path patchelf --set-interpreter /very/long/path/to/custom/lib64/ld-linux-x86-64.so.2 my-program # Use --output to preserve the original file patchelf --set-interpreter /lib/my-ld-linux.so.2 --output my-program-patched my-program # Read interpreter path from a file using @ prefix echo "/opt/custom/lib/ld-linux.so.2" > interpreter.txt patchelf --set-interpreter @interpreter.txt my-program ``` ### Response Example ``` # Output of --print-interpreter /lib64/ld-linux-x86-64.so.2 ``` ``` -------------------------------- ### Shrink RPATH Source: https://github.com/nixos/patchelf/blob/master/README.md Remove unnecessary directories from the RPATH. Use --allowed-rpath-prefixes to preserve specific paths. ```console $ patchelf --shrink-rpath my-program ``` ```console $ patchelf --shrink-rpath --allowed-rpath-prefixes /usr/lib:/foo/lib my-program ``` -------------------------------- ### Add RPATH Source: https://context7.com/nixos/patchelf/llms.txt Appends a new directory to the existing DT_RUNPATH without replacing it. ```APIDOC ## Add RPATH ### Description Appends a new directory to the existing DT_RUNPATH without replacing it. This is useful when you need to add additional library search paths while preserving the original ones. ### Method `patchelf` ### Parameters #### Command Line Arguments - `--print-rpath` - Prints the current RPATH. - `--add-rpath ` - Appends a new directory to the existing RPATH. - `--output ` - Specifies an output file to preserve the original binary. ### Request Example ```bash # Print current RPATH patchelf --print-rpath my-program # Add a new path to existing RPATH patchelf --add-rpath /opt/newlibs my-program # Print RPATH after modification patchelf --print-rpath my-program # Add multiple paths (run multiple times or use colon-separated in some versions) patchelf --add-rpath /app/lib my-program patchelf --add-rpath /vendor/lib my-program ``` ### Response Example ``` # Output of --print-rpath after adding /opt/newlibs /usr/local/lib:/opt/newlibs ``` ``` -------------------------------- ### Perform Combined Operations Source: https://context7.com/nixos/patchelf/llms.txt Allows multiple ELF modifications in a single command invocation for efficiency and atomic updates. This is useful for complex relocations, preparing libraries for distribution, or fixing binaries for custom environments. ```bash # Complete binary relocation in one command patchelf \ --set-interpreter '$ORIGIN/../lib/ld-linux-x86-64.so.2' \ --set-rpath '$ORIGIN/../lib' \ --replace-needed libc.so.6 '$ORIGIN/../lib/libc.so.6' \ my-program ``` ```bash # Prepare library for distribution patchelf \ --set-soname libmyapp.so.1 \ --set-rpath '$ORIGIN' \ --remove-needed libdevel.so \ --clear-execstack \ libmyapp.so.1.2.3 ``` ```bash # Fix binary for custom environment patchelf \ --set-interpreter /opt/glibc-2.31/lib/ld-linux-x86-64.so.2 \ --set-rpath /opt/glibc-2.31/lib:/opt/myapp/lib \ --add-needed libcustom.so \ --output my-program-patched \ my-program ``` -------------------------------- ### Modify RPATH Source: https://github.com/nixos/patchelf/blob/master/README.md Set the RPATH for executables and libraries. ```console $ patchelf --set-rpath /opt/my-libs/lib:/other-libs my-program ``` -------------------------------- ### Modify ELF Interpreter Source: https://github.com/nixos/patchelf/blob/master/README.md Change the dynamic loader of an executable. ```console $ patchelf --set-interpreter /lib/my-ld-linux.so.2 my-program ``` -------------------------------- ### Add RPATH Source: https://context7.com/nixos/patchelf/llms.txt Append a directory to the existing DT_RUNPATH without overwriting current entries. ```bash # Print current RPATH patchelf --print-rpath my-program # Output: /usr/local/lib # Add a new path to existing RPATH patchelf --add-rpath /opt/newlibs my-program # Print RPATH after modification patchelf --print-rpath my-program # Output: /usr/local/lib:/opt/newlibs # Add multiple paths (run multiple times or use colon-separated in some versions) patchelf --add-rpath /app/lib my-program patchelf --add-rpath /vendor/lib my-program ``` -------------------------------- ### Replace Needed Library Dependency Source: https://context7.com/nixos/patchelf/llms.txt Replaces a declared dependency on one library with another. This is useful for redirecting dependencies to alternative implementations, newer versions, or different library locations. ```bash # Print current dependencies patchelf --print-needed my-program ``` ```bash # Replace one library with another patchelf --replace-needed libssl.so.1.0.0 libssl.so.1.1 my-program ``` ```bash # Replace with absolute path patchelf --replace-needed libfoo.so /opt/custom/lib/libfoo-custom.so my-program ``` ```bash # Multiple replacements in one command patchelf \ --replace-needed libold1.so libnew1.so \ --replace-needed libold2.so libnew2.so \ my-program ``` ```bash # Combine with other operations patchelf \ --replace-needed libc.so.6 /opt/glibc/lib/libc.so.6 \ --add-needed /opt/extra/libextra.so \ --set-rpath '/opt/glibc/lib:/opt/extra' \ my-program ``` -------------------------------- ### Rename Dynamic Symbols Source: https://context7.com/nixos/patchelf/llms.txt Renames dynamic symbols in the binary using a mapping file. This is useful for symbol interposition, ABI compatibility fixes, or redirecting function calls to alternative implementations. ```bash # Create a symbol mapping file (space-separated: old_name new_name) cat > symbol_map.txt << 'EOF' malloc custom_malloc free custom_free open custom_open write custom_write EOF ``` -------------------------------- ### Output Modifications to a Different File Source: https://context7.com/nixos/patchelf/llms.txt Writes ELF file modifications to a specified output file instead of modifying the input file in place. This preserves the original binary and is useful for testing or creating patched versions. ```bash # Modify and write to new file patchelf --set-interpreter /lib/ld-linux.so.2 --output modified-program original-program ``` ```bash # Create patched version without modifying original patchelf \ --set-rpath '$ORIGIN/lib' \ --replace-needed libssl.so.1.0 libssl.so.1.1 \ --output app-patched \ app-original ``` ```bash # Useful for creating distribution packages cp my-app my-app.orig patchelf --set-rpath '$ORIGIN/../lib' --output my-app my-app.orig ``` -------------------------------- ### Set RPATH/RUNPATH Source: https://context7.com/nixos/patchelf/llms.txt Changes or sets the DT_RUNPATH (or DT_RPATH) of executables and libraries to control where the dynamic linker searches for shared libraries at runtime. ```APIDOC ## Set RPATH/RUNPATH ### Description Changes or sets the DT_RUNPATH (or DT_RPATH) of executables and libraries to control where the dynamic linker searches for shared libraries at runtime. RUNPATH is the modern replacement for RPATH with the advantage that it can be overridden by LD_LIBRARY_PATH and is scoped to the current binary only. ### Method `patchelf` ### Parameters #### Command Line Arguments - `--print-rpath` - Prints the current RPATH/RUNPATH. - `--set-rpath ` - Sets a new RPATH/RUNPATH. Paths should be colon-separated. Can use `$ORIGIN` for relative paths. Can read paths from a file prefixed with `@`. - `--force-rpath` - Forces the use of obsolete DT_RPATH instead of DT_RUNPATH. - `--output ` - Specifies an output file to preserve the original binary. ### Request Example ```bash # Print current RPATH/RUNPATH patchelf --print-rpath my-program # Set a new RPATH (replaces existing) patchelf --set-rpath '/opt/my-libs/lib:/other-libs' my-program # Use $ORIGIN for relocatable binaries (path relative to executable) patchelf --set-rpath '$ORIGIN/../lib:$ORIGIN/../lib64' my-program # Set multiple library search paths patchelf --set-rpath '/app/lib:/app/vendor/lib:/usr/local/lib' my-program # Force use of obsolete DT_RPATH instead of DT_RUNPATH patchelf --force-rpath --set-rpath '/opt/legacy/lib' my-program # Read rpath from file echo '/opt/generated/lib:/usr/lib' > rpath.txt patchelf --set-rpath @rpath.txt my-program ``` ### Response Example ``` # Output of --print-rpath /usr/local/lib:/opt/mylibs ``` ``` -------------------------------- ### Add Needed Library Dependency Source: https://context7.com/nixos/patchelf/llms.txt Adds a new DT_NEEDED entry to declare a dependency on a shared library. This is useful for permanently adding library dependencies that were not linked at build time, such as LD_PRELOAD libraries. ```bash # Print current needed libraries patchelf --print-needed my-program ``` ```bash # Add a new library dependency patchelf --add-needed libbar.so.1 my-program ``` ```bash # Add multiple dependencies patchelf --add-needed libextra.so.1 my-program patchelf --add-needed libhelper.so.2 my-program ``` ```bash # Verify the addition patchelf --print-needed my-program ``` -------------------------------- ### Add DT_DEBUG Tag Source: https://context7.com/nixos/patchelf/llms.txt Adds a DT_DEBUG tag to the .dynamic section of a shared library if it's not already present. This is crucial for enabling proper debugger support, especially when the library can be run as an executable. ```bash # Add DT_DEBUG tag to a shared library with entry point patchelf --add-debug-tag libmylib.so ``` -------------------------------- ### Rename Dynamic Symbols Source: https://context7.com/nixos/patchelf/llms.txt Renames dynamic symbols in an ELF file based on a provided map. Use a file with 'old_name new_name' per line for multiple renames, or pipe 'old_name new_name' for a single rename. ```bash # Rename symbols according to the map patchelf --rename-dynamic-symbols symbol_map.txt my-program ``` ```bash # Single symbol rename echo "old_function new_function" > single_map.txt patchelf --rename-dynamic-symbols single_map.txt libmylib.so ``` -------------------------------- ### Override ELF Page Size Source: https://context7.com/nixos/patchelf/llms.txt Overrides the default page size used when modifying ELF files. This is useful for architectures with non-standard page sizes or when default detection fails. ```bash # Use custom page size (in bytes) patchelf --page-size 4096 --set-rpath /opt/lib my-program ``` ```bash # For architectures with larger page sizes (e.g., 64KB pages) patchelf --page-size 65536 --set-interpreter /lib/ld.so my-program ``` -------------------------------- ### Remove RPATH Source: https://context7.com/nixos/patchelf/llms.txt Completely delete the DT_RPATH or DT_RUNPATH entry from the binary. ```bash # Check current RPATH patchelf --print-rpath my-program # Output: /custom/lib:/usr/local/lib # Remove RPATH entirely patchelf --remove-rpath my-program ``` -------------------------------- ### Shrink RPATH Source: https://context7.com/nixos/patchelf/llms.txt Removes from the RPATH all directories that do not contain a library referenced by DT_NEEDED entries. ```APIDOC ## Shrink RPATH ### Description Removes from the RPATH all directories that do not contain a library referenced by DT_NEEDED entries. This optimization reduces unnecessary directory searches and cleans up build artifacts like temporary build directories from the library search path. ### Method `patchelf` ### Parameters #### Command Line Arguments - `--shrink-rpath` - Shrinks the RPATH by removing unused directories. - `--allowed-rpath-prefixes ` - Optional. Specifies prefixes to keep during shrinking, useful for preserving system paths. - `--output ` - Specifies an output file to preserve the original binary. ### Request Example ```bash # Before: RPATH might contain build directories patchelf --print-rpath my-program # Shrink RPATH to only directories containing needed libraries patchelf --shrink-rpath my-program # After: Only directories with actual needed libraries remain patchelf --print-rpath my-program # Shrink RPATH but only allow certain prefixes patchelf --shrink-rpath --allowed-rpath-prefixes /usr/lib:/opt/mylibs my-program # This removes /tmp/build entries but keeps /usr and /opt paths patchelf --shrink-rpath --allowed-rpath-prefixes /usr:/opt my-program ``` ### Response Example ``` # Output of --print-rpath after shrinking /usr/local/lib:/opt/mylibs ``` ``` -------------------------------- ### Shrink RPATH Source: https://context7.com/nixos/patchelf/llms.txt Remove directories from RPATH that do not contain libraries referenced by DT_NEEDED. Use --allowed-rpath-prefixes to restrict which paths are kept. ```bash # Before: RPATH might contain build directories patchelf --print-rpath my-program # Output: /tmp/build-foo/.libs:/usr/local/lib:/opt/mylibs:/nonexistent/path # Shrink RPATH to only directories containing needed libraries patchelf --shrink-rpath my-program # After: Only directories with actual needed libraries remain patchelf --print-rpath my-program # Output: /usr/local/lib:/opt/mylibs # Shrink RPATH but only allow certain prefixes # Useful for removing temporary build paths while keeping system paths patchelf --shrink-rpath --allowed-rpath-prefixes /usr/lib:/opt/mylibs my-program # This removes /tmp/build entries but keeps /usr and /opt paths patchelf --shrink-rpath --allowed-rpath-prefixes /usr:/opt my-program ``` -------------------------------- ### Set SONAME for Shared Library Source: https://context7.com/nixos/patchelf/llms.txt Sets or modifies the DT_SONAME entry of a shared library. The SONAME is the canonical name used by the dynamic linker to identify the library at runtime. ```bash # Print current SONAME patchelf --print-soname libmylib.so.1.2.3 ``` ```bash # Set a new SONAME patchelf --set-soname libmylib.so.2 libmylib.so.1.2.3 ``` ```bash # Create a library with proper SONAME cp libmylib.so libmylib.so.1.0.0 patchelf --set-soname libmylib.so.1 libmylib.so.1.0.0 ``` ```bash # Rename a library's SONAME for versioning patchelf --set-soname libnewname.so.3.4.5 path/to/libmylibrary.so.1.2.3 ``` -------------------------------- ### Executable Stack Control Source: https://context7.com/nixos/patchelf/llms.txt Controls the executable stack flag in the GNU_STACK program header. Clearing this flag improves security by preventing stack-based code execution attacks. Setting it is for legacy binaries that require an executable stack. ```bash # Print current execstack status patchelf --print-execstack my-program ``` ```bash # Clear the executable stack flag (improve security) patchelf --clear-execstack my-program ``` ```bash # Set the executable stack flag (for legacy binaries that require it) patchelf --set-execstack my-program ``` ```bash # Works on both executables and shared libraries patchelf --clear-execstack libmylib.so patchelf --print-execstack libmylib.so ``` -------------------------------- ### Remove Needed Library Dependency Source: https://context7.com/nixos/patchelf/llms.txt Removes a declared dependency on a shared library (DT_NEEDED entry). This is useful for removing unnecessary dependencies or replacing them with alternative implementations. ```bash # Print current dependencies patchelf --print-needed my-program ``` ```bash # Remove a specific dependency patchelf --remove-needed libbar.so.1 my-program ``` ```bash # Remove multiple dependencies patchelf --remove-needed libold1.so --remove-needed libold2.so my-program ``` ```bash # Verify removal patchelf --print-needed my-program ``` -------------------------------- ### Remove RPATH Source: https://context7.com/nixos/patchelf/llms.txt Completely removes the DT_RPATH or DT_RUNPATH entry from an executable or library. ```APIDOC ## Remove RPATH ### Description Completely removes the DT_RPATH or DT_RUNPATH entry from an executable or library, causing the dynamic linker to use only default search paths and LD_LIBRARY_PATH. ### Method `patchelf` ### Parameters #### Command Line Arguments - `--print-rpath` - Prints the current RPATH. - `--remove-rpath` - Removes the RPATH/RUNPATH entry entirely. - `--output ` - Specifies an output file to preserve the original binary. ### Request Example ```bash # Check current RPATH patchelf --print-rpath my-program # Remove RPATH entirely patchelf --remove-rpath my-program ``` ### Response Example ``` # Output of --print-rpath after removal (empty output or no RPATH entry) ``` ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.