patchelf icon indicating copy to clipboard operation
patchelf copied to clipboard

Add option to make the rpath relative under a specified root directory

Open grandwolf opened this issue 7 years ago • 10 comments

The buildroot project wants to make the SDK (toolchain) tree relocatable allowing to install it at any location. Furthermore, the target root file system trees needs ELF file sanitation to remove references to the build system. This patch implements the necessary actions to sanitize the ELF files under the specified tree. See also [1].

Running "patchelf" with the option "--make-rpath-relative ROOTDIR" will modify or delete the RPATHDIRs of the RPATH according the following rules:

  • RPATHDIR starts with "$ORIGIN": The original build-system already took care of setting a relative RPATH, resolve it and test if it's valid (does exist),

  • RPATHDIR starts with ROOTDIR: The original build-system added some absolute RPATH (absolute on the build machine). Test if it's valid (does exist).

  • ROOTDIR/RPATHDIR exists: The original build-system already took care of setting an absolute RPATH (absolute in the final rootfs), resolve it and test if it's valid (does exist).

  • RPATHDIR points somewhere else: (can be anywhere: build trees, staging tree, host location, non-existing location, etc.). Just discard such a path.

In addition, the option "--no-standard-libs" will discard RPATHDIRs "ROOTDIR/lib" and "ROOTDIR/usr/lib". Like "--shrink-rpath", RPATHDIRs are also discarded if the directories do not contain a library referenced by DT_NEEDED fields. If the option "--relative-to-file" is given, the rpath will start with "$ORIGIN" making it relative to the ELF file, otherwise an absolute path relative to ROOTDIR will be used.

Please comment. If necessary/useful we could also make it more modular, e.g. by extending "--shrink-rpath".

[1] http://lists.busybox.net/pipermail/buildroot/2016-April/159422.html

grandwolf avatar Mar 14 '17 11:03 grandwolf

@grandwolf This looks like a pretty interesting feature. Is this PR still being worked on?

ebkalderon avatar Feb 05 '19 10:02 ebkalderon

I'd also be interested in this feature, e.g., for use in linuxdeployqt.

probonopd avatar Feb 05 '19 18:02 probonopd

Hello,

it's used in Buildroot to build relocatable SDKs:

https://github.com/buildroot/buildroot/tree/master/package/patchelf

Wolfgang

Am 05.02.19 um 11:55 schrieb Eyal Kalderon:

@grandwolf https://github.com/grandwolf This looks like a pretty interesting feature. Is this PR still being worked on?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/NixOS/patchelf/pull/118#issuecomment-460595070, or mute the thread https://github.com/notifications/unsubscribe-auth/AZLsSBO2rgjupu2X69m2rmVpJ1L_8Zvtks5vKWMxgaJpZM4McbN3.

grandwolf avatar Feb 05 '19 18:02 grandwolf

Use patchelf --set-rpath "\$ORIGIN/my-sub-dir-if-any" my-exe. It worked fine for me.

jraygauthier avatar Oct 01 '20 23:10 jraygauthier

Does this PR could be merged in a near future?

ffontaine avatar Sep 12 '21 08:09 ffontaine

I'm not involved in the project, but the formatting looks inconsistent (is there an auto formatter configured?). Especially, tabs and spaces are mixed.

piegamesde avatar Sep 12 '21 10:09 piegamesde

Does this PR could be merged in a near future?

Hello, I think the comment I posted has not been addressed by the PR. I think it is still a problem to remove directories from RPATH just because they don't contain any library pointed by a DT_NEEDED entry.

fallen avatar Sep 19 '21 17:09 fallen

@fallen FWIW I think your concerns might be addressed (for buildroot, at least, presumably) with: https://github.com/buildroot/buildroot/commit/5be9c96030525ad0064868c7401b4595e3920437

Those changes appear listed on the issue page of #118 but might not be obvious, e.g. if you get notifications via email.

See, e.g.: https://github.com/NixOS/patchelf/pull/118#ref-commit-5be9c96

However GH is still reporting file conflicts.

follower avatar Sep 25 '21 14:09 follower

I can open a new PR to include buildroot/buildroot@5be9c96 and fix those file conflicts based on the patch that I sent to buildroot a few days ago: https://patchwork.ozlabs.org/project/buildroot/patch/[email protected]/

ffontaine avatar Sep 25 '21 20:09 ffontaine

/data/data/com.termux/files/usr/bin/bash: termux-change-repo: command not found

QGB avatar May 08 '23 14:05 QGB