Skip to content

Add option to replace an imported symbol #416

@raphaelr

Description

@raphaelr

Is your feature request related to a problem? Please describe.
Using the rpath arguments (e.g. --add-rpath), patchelf can override the location of libraries. However, libraries have other ways of depending on file system paths. A common example is drivers containing a hard-coded path to firmware binaries, which the library fopen()s.

Example: Broadcom fingerprint driver, the .so contains a hardcoded path to /var/lib/fprint/fw.

Currently it is difficult to replace such paths.

Describe the solution you'd like
Add a new argument to patchelf, --replace-symbol NAME_ORIG NAME_NEW. If specified, patchelf will iterate over the symbol table (.dynsym section), and replace the symbol with the name NAME_ORIG with NAME_NEW, adding it to the .dynstr section if neccessary.

This allows one to replace the fopen call from the library with a wrapper function, which can inspect the path it receives and change it if neccessary.

Example invocation:

$ # assume that fopen_wrapper.c defines a `fopen_wrapper()` function with the same signature as `fopen()`
$ cc -fPIC -shared fopen_wrapper.c -o libfopen_wrapper.so
$ patchelf \
    --replace-symbol fopen fopen_wrapper \
    --add-needed libfopen_wrapper.so \
    libdriver.so

Describe alternatives you've considered

  • Use buildFHSUserEnv from nixpkgs to give the process the filesystem layout it needs.
  • Use LD_PRELOAD to inject the wrapper into the process.

These methods have the disadvantage that they affect an entire process tree, not just the single library that needs patching.

Additional context
I have a prototype quality implementation of this feature. If this feature request is accepted, I'll clean it up and add tests/documentation for it.

Upstreaming of this feature has been requested by the iscan-snap project, which attempts to package scanner drivers using snap.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions