From patchwork Thu Mar 16 14:23:21 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Wolfgang Grandegger X-Patchwork-Id: 739843 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from silver.osuosl.org (smtp3.osuosl.org [140.211.166.136]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3vkW1n0dv2z9s03 for ; Fri, 17 Mar 2017 01:24:13 +1100 (AEDT) Received: from localhost (localhost [127.0.0.1]) by silver.osuosl.org (Postfix) with ESMTP id 7D4133084E; Thu, 16 Mar 2017 14:24:09 +0000 (UTC) X-Virus-Scanned: amavisd-new at osuosl.org Received: from silver.osuosl.org ([127.0.0.1]) by localhost (.osuosl.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id hKs6aeJVirIR; Thu, 16 Mar 2017 14:24:03 +0000 (UTC) Received: from ash.osuosl.org (ash.osuosl.org [140.211.166.34]) by silver.osuosl.org (Postfix) with ESMTP id B401B307B1; Thu, 16 Mar 2017 14:23:56 +0000 (UTC) X-Original-To: buildroot@lists.busybox.net Delivered-To: buildroot@osuosl.org Received: from silver.osuosl.org (smtp3.osuosl.org [140.211.166.136]) by ash.osuosl.org (Postfix) with ESMTP id 70D401C04A5 for ; Thu, 16 Mar 2017 14:23:41 +0000 (UTC) Received: from localhost (localhost [127.0.0.1]) by silver.osuosl.org (Postfix) with ESMTP id 60C792FAAF for ; Thu, 16 Mar 2017 14:23:41 +0000 (UTC) X-Virus-Scanned: amavisd-new at osuosl.org Received: from silver.osuosl.org ([127.0.0.1]) by localhost (.osuosl.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 3s3807ocOK2l for ; Thu, 16 Mar 2017 14:23:39 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from mailproxy03.manitu.net (mailproxy03.manitu.net [217.11.48.67]) by silver.osuosl.org (Postfix) with ESMTPS id 396DA2A109 for ; Thu, 16 Mar 2017 14:23:39 +0000 (UTC) Received: from onex.fritz.box (aftr-88-217-180-211.dynamic.mnet-online.de [88.217.180.211]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-SHA256 (128/128 bits)) (No client certificate requested) (Authenticated sender: wg@grandegger.com) by mailproxy03.manitu.net (Postfix) with ESMTPSA id 3FCFBD4920A; Thu, 16 Mar 2017 15:23:37 +0100 (CET) From: Wolfgang Grandegger To: buildroot@buildroot.org Date: Thu, 16 Mar 2017 15:23:21 +0100 Message-Id: <1489674207-6876-4-git-send-email-wg@grandegger.com> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1489674207-6876-1-git-send-email-wg@grandegger.com> References: <1489674207-6876-1-git-send-email-wg@grandegger.com> Cc: Wolfgang Grandegger Subject: [Buildroot] [RFC PATCH v2 3/9] support/scripts: add fix-rpath script to sanitize the rpath X-BeenThere: buildroot@busybox.net X-Mailman-Version: 2.1.18-1 Precedence: list List-Id: Discussion and development of buildroot List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: buildroot-bounces@busybox.net Sender: "buildroot" From: Samuel Martin 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 ". Signed-off-by: Samuel Martin Signed-off-by: Wolfgang Grandegger --- 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..f87dc8b --- /dev/null +++ b/support/scripts/fix-rpath @@ -0,0 +1,103 @@ +#!/usr/bin/env bash + +# Copyright (C) 2016 Samuel Martin +# +# 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 <&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 ${@}