NixOS / patchelf

A small utility to modify the dynamic linker and RPATH of ELF executables
GNU General Public License v3.0
3.57k stars 487 forks source link

Support idempotent addition of RPATHs and NEEDEDs (i.e. remove duplicates) #497

Open de11n opened 1 year ago

de11n commented 1 year ago

patchelf --add-needed libX Y && patchelf --add-needed libX Y will add libX twice. The same goes for patchelf --add-rpath.

It would be very handy if there were a way to enable patchelf to only make additions when the requested NEEDED/RPATH is not already present.

Ordering matters for both of these, so this feature would likely only come into effect for duplicates that would otherwise have a lower precedence than an existing entry.

This feature could be added numerous ways:

patchelf --add-needed libX --add-rpath /path/y --uniq-needed --uniq-rpaths Z

This would be strictly more general than merely making --add-needed/--add-rpath idempotent, but it would achieve the goal.

(Indeed, --shrink-rpath probably has this effect, but it cannot be used when the RPATH contains directories that don't exist yet.)

A more granular option could be:

patchelf --add-needed-idempotent libX Z

Obviously this functionality can be achieved with a few calls to patchelf --print-needed/patchelf --print-rpath along with some tr and awk. But that's also true of many features (e.g. --add-rpath could be implemented in terms of --print-rpath and --set-rpath) and I think it's probably the semantics people mean when they say --add-needed etc. It's also just much more convenient for patchelf to do it, while also more efficient.