diff options
author | Mike Frysinger <vapier@chromium.org> | 2021-04-15 23:24:07 -0400 |
---|---|---|
committer | Mike Frysinger <vapier@gentoo.org> | 2021-04-15 23:24:07 -0400 |
commit | cb5da5f0f8ca714a552b70f201ace2eb24f0d2b1 (patch) | |
tree | 22bca6f6a6d65a44242c632439d2c86e58e6c9de | |
parent | lddtree: apply functools cache to some repeated filesystem calls (diff) | |
download | pax-utils-cb5da5f0f8ca714a552b70f201ace2eb24f0d2b1.tar.gz pax-utils-cb5da5f0f8ca714a552b70f201ace2eb24f0d2b1.tar.bz2 pax-utils-cb5da5f0f8ca714a552b70f201ace2eb24f0d2b1.zip |
lddtree: use ldso's --argv0 when available
Newer glibc has a --argv0 option in current releases to explicitly
set argv[0]. This is useful for the generated shell wrappers as we
need to rename it (with an .elf suffix) and that can either confuse
the output or break tools that inspect their argv[0] strictly.
Signed-off-by: Mike Frysinger <vapier@gentoo.org>
-rwxr-xr-x | lddtree.py | 26 |
1 files changed, 20 insertions, 6 deletions
@@ -43,6 +43,7 @@ import argparse import functools import glob import errno +import mmap import os import shutil import sys @@ -123,6 +124,17 @@ def dedupe(items): return [seen.setdefault(x, x) for x in items if x not in seen] +@functools.lru_cache(maxsize=None) +def interp_supports_argv0(interp) -> bool: + """See whether |interp| supports the --argv0 option. + + Starting with glibc-2.33, the ldso supports --argv0 to override argv[0]. + """ + with open(interp, 'rb') as fp: + with mmap.mmap(fp.fileno(), 0, prot=mmap.PROT_READ) as mm: + return mm.find(b'--argv0') >= 0 + + def GenerateLdsoWrapper(root, path, interp, libpaths=()): """Generate a shell script wrapper which uses local ldso to run the ELF @@ -145,6 +157,7 @@ def GenerateLdsoWrapper(root, path, interp, libpaths=()): interp_name), 'libpaths': ':'.join(['${basedir}/' + os.path.relpath(p, basedir) for p in libpaths]), + 'argv0_arg': '--argv0 "$0"' if interp_supports_argv0(interp) else '', } wrapper = """#!/bin/sh if ! base=$(realpath "$0" 2>/dev/null); then @@ -154,12 +167,13 @@ if ! base=$(realpath "$0" 2>/dev/null); then esac fi basedir=${base%%/*} -exec \ - "${basedir}/%(interp)s" \ - --library-path "%(libpaths)s" \ - --inhibit-cache \ - --inhibit-rpath '' \ - "${base}.elf" \ +exec \\ + "${basedir}/%(interp)s" \\ + %(argv0_arg)s \\ + --library-path "%(libpaths)s" \\ + --inhibit-cache \\ + --inhibit-rpath '' \\ + "${base}.elf" \\ "$@" """ wrappath = root + path |