From: Wolfgang Grandegger <wg@grandegger.com>
To: buildroot@busybox.net
Subject: [Buildroot] [RFC PATCH v3 03/10] support/scripts: add fix-rpath script to sanitize the rpath
Date: Thu, 23 Mar 2017 08:54:46 +0100 [thread overview]
Message-ID: <1490255693-9134-4-git-send-email-wg@grandegger.com> (raw)
In-Reply-To: <1490255693-9134-1-git-send-email-wg@grandegger.com>
From: Samuel Martin <s.martin49@gmail.com>
This commit introduces the script "fix-rpath" able to scan a tree,
detect ELF files, check their RPATH and fix it in a proper way.
The RPATH fixup is done by the patchelf utility using the option
"--make-rpath-relative <root-directory>".
Signed-off-by: Samuel Martin <s.martin49@gmail.com>
Signed-off-by: Wolfgang Grandegger <wg@grandegger.com>
---
support/scripts/fix-rpath | 103 ++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 103 insertions(+)
create mode 100755 support/scripts/fix-rpath
diff --git a/support/scripts/fix-rpath b/support/scripts/fix-rpath
new file mode 100755
index 0000000..5702b9a
--- /dev/null
+++ b/support/scripts/fix-rpath
@@ -0,0 +1,103 @@
+#!/usr/bin/env bash
+
+# Copyright (C) 2016 Samuel Martin <s.martin49@gmail.com>
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+# General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+
+usage() {
+ cat <<EOF >&2
+Usage: ${0} TREE_KIND
+
+Description:
+
+ This script scans a tree and sanitize ELF files' RPATH found in there.
+
+ Sanitization behaves the same whatever the kind of the processed tree,
+ but the resulting RPATH differs. The rpath sanitization is done using
+ "patchelf --make-rpath-relazive".
+
+Arguments:
+
+ TREE_KIND Kind of tree to be processed.
+ Allowed values: host, target
+
+Environment:
+
+ PATCHELF patchelf program to use
+ (default: HOST_DIR/usr/bin/patchelf)
+EOF
+}
+
+: ${PATCHELF:=${HOST_DIR}/usr/bin/patchelf}
+
+main() {
+ local rootdir
+ local tree="${1}"
+ local find_args=( )
+ local sanitize_extra_args=( )
+
+ case "${tree}" in
+ host)
+ rootdir="${HOST_DIR}"
+
+ # do not process the sysroot (only contains target binaries)
+ find_args+=( "-path" "${STAGING_DIR}" "-prune" "-o" )
+
+ # do not process the external toolchain installation directory to
+ # avoid breaking it.
+ test "${TOOLCHAIN_EXTERNAL_DOWNLOAD_INSTALL_DIR}" != "" && \
+ find_args+=( "-path" "${TOOLCHAIN_EXTERNAL_DOWNLOAD_INSTALL_DIR}" "-prune" "-o" )
+
+ # ELF files should not be in these sub-directories
+ find_args+=( "-path" "${STAGING_DIR}/usr/share/terminfo" "-prune" "-o" )
+
+ # do not process the patchelf binary but a copy to work-around "file in use"
+ find_args+=( "-path" "${PATCHELF}" "-prune" "-o" )
+ cp "${PATCHELF}" "${PATCHELF}.__to_be_patched"
+ ;;
+
+ target)
+ rootdir="${TARGET_DIR}"
+ sanitize_extra_args+=( "--no-standard-lib-dirs" )
+ ;;
+
+ *)
+ usage
+ exit 1
+ ;;
+ esac
+
+ find_args+=( "-type" "f" "-print" )
+
+ while read file ; do
+ # check if it's an ELF file
+ if ${PATCHELF} --print-rpath "${file}" > /dev/null 2>&1; then
+ # make files writable if necessary
+ changed=$(chmod -c u+w "${file}")
+ # call patchelf to sanitize the rpath
+ ${PATCHELF} --make-rpath-relative "${rootdir}" ${sanitize_extra_args[@]} "${file}"
+ # restore the original permission
+ test "${changed}" != "" && chmod u-w "${file}"
+ fi
+ done < <(find "${rootdir}" ${find_args[@]})
+
+ # Restore patched patchelf utility
+ test "${tree}" = "host" && mv "${PATCHELF}.__to_be_patched" "${PATCHELF}"
+
+ # ignore errors
+ return 0
+}
+
+main ${@}
--
1.9.1
next prev parent reply other threads:[~2017-03-23 7:54 UTC|newest]
Thread overview: 35+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-03-23 7:54 [Buildroot] [RFC PATCH v3 00/10] Make the SDK relocatable Wolfgang Grandegger
2017-03-23 7:54 ` [Buildroot] [RFC PATCH v3 01/10] package/patchelf: use most recent version as a base for rpath sanitation Wolfgang Grandegger
2017-03-23 7:54 ` [Buildroot] [RFC PATCH v3 02/10] package/patchelf: add patch for rpath sanitation under a root directory Wolfgang Grandegger
2017-03-23 7:54 ` Wolfgang Grandegger [this message]
2017-03-23 7:54 ` [Buildroot] [RFC PATCH v3 04/10] core: sanitize RPATH in host tree at the very end of the build Wolfgang Grandegger
2017-03-23 7:54 ` [Buildroot] [RFC PATCH v3 05/10] core: sanitize RPATH in target tree at the end of the target finalization Wolfgang Grandegger
2017-03-23 7:54 ` [Buildroot] [RFC PATCH v3 06/10] support/scripts: add reloacte-sdk.sh script for SDK relocation Wolfgang Grandegger
2017-03-23 7:54 ` [Buildroot] [RFC PATCH v3 07/10] core: install relocation script and location at the end of the build Wolfgang Grandegger
2017-03-23 7:54 ` [Buildroot] [RFC PATCH v3 08/10] external-toolchain: check if a buildroot SDK has already been relocated Wolfgang Grandegger
2017-03-23 7:54 ` [Buildroot] [RFC PATCH v3 09/10] support/scripts: check-host-rpath now handles $ORIGIN as well Wolfgang Grandegger
2017-03-23 7:54 ` [Buildroot] [RFC PATCH v3 10/10] package/qt5base: provide "qt.conf" to make "qmake" relocatable Wolfgang Grandegger
2017-04-12 13:59 ` [Buildroot] [RFC PATCH v3 00/10] Make the SDK relocatable Arnout Vandecappelle
2017-04-27 9:37 ` Wolfgang Grandegger
2017-06-21 7:59 ` Wolfgang Grandegger
2017-06-21 21:03 ` Arnout Vandecappelle
2017-06-21 21:06 ` Arnout Vandecappelle
2017-06-27 10:54 ` Wolfgang Grandegger
2017-06-27 22:41 ` Arnout Vandecappelle
2017-06-28 6:51 ` Wolfgang Grandegger
2017-06-28 10:44 ` Arnout Vandecappelle
2017-06-28 10:59 ` Wolfgang Grandegger
2017-06-22 6:37 ` Wolfgang Grandegger
2017-06-27 22:47 ` Arnout Vandecappelle
2017-06-28 6:40 ` Peter Korsgaard
2017-06-28 6:45 ` Wolfgang Grandegger
2017-06-28 10:41 ` Arnout Vandecappelle
2017-06-28 10:57 ` Wolfgang Grandegger
2017-06-28 11:16 ` Arnout Vandecappelle
2017-06-28 12:36 ` Wolfgang Grandegger
2017-06-28 19:33 ` Arnout Vandecappelle
2017-06-29 6:13 ` Wolfgang Grandegger
2017-06-29 8:02 ` Wolfgang Grandegger
2017-06-30 8:50 ` Wolfgang Grandegger
2017-06-30 9:14 ` Wolfgang Grandegger
2017-06-30 10:25 ` Arnout Vandecappelle
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1490255693-9134-4-git-send-email-wg@grandegger.com \
--to=wg@grandegger.com \
--cc=buildroot@busybox.net \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox