From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <gentoo-commits+bounces-1271760-garchives=archives.gentoo.org@lists.gentoo.org>
Received: from lists.gentoo.org (pigeon.gentoo.org [208.92.234.80])
	(using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits))
	(No client certificate requested)
	by finch.gentoo.org (Postfix) with ESMTPS id A4CC91382C5
	for <garchives@archives.gentoo.org>; Fri, 16 Apr 2021 15:08:27 +0000 (UTC)
Received: from pigeon.gentoo.org (localhost [127.0.0.1])
	by pigeon.gentoo.org (Postfix) with SMTP id 8F088E0823;
	Fri, 16 Apr 2021 15:08:26 +0000 (UTC)
Received: from smtp.gentoo.org (smtp.gentoo.org [140.211.166.183])
	(using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits))
	(No client certificate requested)
	by pigeon.gentoo.org (Postfix) with ESMTPS id 68758E081A
	for <gentoo-commits@lists.gentoo.org>; Fri, 16 Apr 2021 15:08:26 +0000 (UTC)
Received: from oystercatcher.gentoo.org (oystercatcher.gentoo.org [148.251.78.52])
	(using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits))
	(No client certificate requested)
	by smtp.gentoo.org (Postfix) with ESMTPS id 02EEE340B9E
	for <gentoo-commits@lists.gentoo.org>; Fri, 16 Apr 2021 15:08:25 +0000 (UTC)
Received: from localhost.localdomain (localhost [IPv6:::1])
	by oystercatcher.gentoo.org (Postfix) with ESMTP id 94A0464C
	for <gentoo-commits@lists.gentoo.org>; Fri, 16 Apr 2021 15:08:23 +0000 (UTC)
From: "Mike Frysinger" <vapier@gentoo.org>
To: gentoo-commits@lists.gentoo.org
Content-Transfer-Encoding: 8bit
Content-type: text/plain; charset=UTF-8
Reply-To: gentoo-dev@lists.gentoo.org, "Mike Frysinger" <vapier@gentoo.org>
Message-ID: <1618584596.080a59e801e121ebadc3e1e170e57ca27de15876.vapier@gentoo>
Subject: [gentoo-commits] proj/pax-utils:master commit in: /
X-VCS-Repository: proj/pax-utils
X-VCS-Files: lddtree.py
X-VCS-Directories: /
X-VCS-Committer: vapier
X-VCS-Committer-Name: Mike Frysinger
X-VCS-Revision: 080a59e801e121ebadc3e1e170e57ca27de15876
X-VCS-Branch: master
Date: Fri, 16 Apr 2021 15:08:23 +0000 (UTC)
Precedence: bulk
List-Post: <mailto:gentoo-commits@lists.gentoo.org>
List-Help: <mailto:gentoo-commits+help@lists.gentoo.org>
List-Unsubscribe: <mailto:gentoo-commits+unsubscribe@lists.gentoo.org>
List-Subscribe: <mailto:gentoo-commits+subscribe@lists.gentoo.org>
List-Id: Gentoo Linux mail <gentoo-commits.gentoo.org>
X-BeenThere: gentoo-commits@lists.gentoo.org
X-Auto-Response-Suppress: DR, RN, NRN, OOF, AutoReply
X-Archives-Salt: a218a5df-7984-4467-a9b1-9d6d928ff3eb
X-Archives-Hash: ebeb12b81025f998a638a7a599940ecd

commit:     080a59e801e121ebadc3e1e170e57ca27de15876
Author:     Mike Frysinger <vapier <AT> chromium <DOT> org>
AuthorDate: Fri Apr 16 14:49:56 2021 +0000
Commit:     Mike Frysinger <vapier <AT> gentoo <DOT> org>
CommitDate: Fri Apr 16 14:49:56 2021 +0000
URL:        https://gitweb.gentoo.org/proj/pax-utils.git/commit/?id=080a59e8

lddtree: handle relative ldpaths

Tweak the ldpath logic to handle all relative paths relative to the
cwd instead of the root.  Such ELFs are uncommon and weird, but not
invalid, so might as well.

Bug: https://bugs.gentoo.org/653586
Signed-off-by: Mike Frysinger <vapier <AT> gentoo.org>

 lddtree.py | 21 ++++++++++++++++-----
 1 file changed, 16 insertions(+), 5 deletions(-)

diff --git a/lddtree.py b/lddtree.py
index cd068f6..d91e729 100755
--- a/lddtree.py
+++ b/lddtree.py
@@ -184,7 +184,7 @@ exec \\
 
 
 @functools.lru_cache(maxsize=None)
-def ParseLdPaths(str_ldpaths, root='', path=None):
+def ParseLdPaths(str_ldpaths, root='', cwd=None, path=None):
     """Parse the colon-delimited list of paths and apply ldso rules to each
 
     Note the special handling as dictated by the ldso:
@@ -195,23 +195,34 @@ def ParseLdPaths(str_ldpaths, root='', path=None):
     Args:
       str_ldpaths: A colon-delimited string of paths
       root: The path to prepend to all paths found
+      cwd: The path to resolve relative paths against (defaults to getcwd()).
       path: The object actively being parsed (used for $ORIGIN)
 
     Returns:
       list of processed paths
     """
+    if cwd is None:
+        cwd = os.getcwd()
+
     ldpaths = []
     for ldpath in str_ldpaths.split(':'):
-        if not ldpath:
-            # The ldso treats "" paths as $PWD.
-            ldpath = os.getcwd()
-        elif '$ORIGIN' in ldpath:
+        # Expand placeholders first.
+        if '$ORIGIN' in ldpath:
             ldpath = ldpath.replace('$ORIGIN', os.path.dirname(path))
         elif '${ORIGIN}' in ldpath:
             ldpath = ldpath.replace('${ORIGIN}', os.path.dirname(path))
+
+        # Expand relative paths if needed.  These don't make sense in general,
+        # but that doesn't stop people from using them.  As such, root prefix
+        # doesn't make sense with it either.
+        if not ldpath.startswith('/'):
+            # NB: The ldso treats "" paths as cwd too.
+            ldpath = os.path.join(cwd, ldpath)
         else:
             ldpath = root + ldpath
+
         ldpaths.append(normpath(ldpath))
+
     return dedupe(ldpaths)