commit:     aadadb863a89af460726163703278b14750591ae
Author:     George Burgess IV <gbiv <AT> google <DOT> com>
AuthorDate: Tue Sep 22 15:09:47 2020 +0000
Commit:     Mike Frysinger <vapier <AT> gentoo <DOT> org>
CommitDate: Fri Dec 22 05:31:31 2023 +0000
URL:        https://gitweb.gentoo.org/proj/pax-utils.git/commit/?id=aadadb86

lddtree: add LD_ARGV0_REL

Some binaries use `/proc/self/exe` to get a link to the
currently-executing binary. Unfortunately, when `ld.so` is invoked
directly, `/proc/self/exe` alawys points to `ld.so`.

`LD_ARGV0` can only be used to determine the current executable in
programs which haven't changed their working directory from their
starting one, so that's difficult to generally use.

To solve this, this embeds the path of the current binary _relative to
ld.so_ in an env var.

Bug: https://crbug.com/1003841
Bug: https://issuetracker.google.com/187793259
Signed-off-by: George Burgess <gbiv <AT> chromium.org>
Signed-off-by: Mike Frysinger <vapier <AT> chromium.org>
Signed-off-by: Mike Frysinger <vapier <AT> gentoo.org>

 lddtree.py | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/lddtree.py b/lddtree.py
index 8184e8f..b26afcf 100755
--- a/lddtree.py
+++ b/lddtree.py
@@ -176,6 +176,7 @@ def GenerateLdsoWrapper(
 
     replacements = {
         "interp": os.path.join(os.path.relpath(interp_dir, basedir), 
interp_name),
+        "interp_rel": os.path.relpath(path, interp_dir),
         "libpaths": ":".join(
             "${basedir}/" + os.path.relpath(p, basedir) for p in libpaths
         ),
@@ -186,6 +187,10 @@ def GenerateLdsoWrapper(
     # Keep path relativeness of argv0 (in ${base}.elf). This allows tools to
     # remove absolute paths from build outputs and enables directory 
independent
     # cache sharing in distributed build systems.
+    #
+    # NB: LD_ARGV0_REL below is unrelated & non-standard.  It's to let tools 
see
+    # the original path if they need it and when they know they'll be wrapped 
up
+    # by this script.
     wrapper = """#!/bin/sh
 if base=$(readlink "$0" 2>/dev/null); then
   # If $0 is an abspath symlink, fully resolve the target.
@@ -200,6 +205,7 @@ else
   esac
 fi
 basedir=${base%%/*}
+LD_ARGV0_REL="%(interp_rel)s" \\
 exec \\
   "${basedir}/%(interp)s" \\
   %(argv0_arg)s \\

Reply via email to