]> git.ipfire.org Git - thirdparty/glibc.git/commitdiff
ld.so: add an --argv0 option [BZ #16124]
authorVincent Mihalkovic <vmihalko@redhat.com>
Tue, 29 Sep 2020 10:34:39 +0000 (12:34 +0200)
committerFlorian Weimer <fweimer@redhat.com>
Tue, 29 Sep 2020 10:34:40 +0000 (12:34 +0200)
NEWS
elf/Makefile
elf/argv0test.c [new file with mode: 0644]
elf/rtld.c
elf/tst-rtld-argv0.sh [new file with mode: 0755]

diff --git a/NEWS b/NEWS
index fc8dd15439e9738eae10ba1e0e636bb733deb4a5..ce05d05b162eb04cfefa721b9839bfb342fdd4a6 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -9,6 +9,9 @@ Version 2.33
 
 Major new features:
 
+* The dynamic linker accepts the --argv0 argument and provides opportunity
+  to change argv[0] string.
+
 * The mallinfo2 function is added to report statistics as per mallinfo,
   but with larger field widths to accurately report values that are
   larger than fit in an integer.
index 0b78721848b9317b6c542da965f847282ea29af1..c587e9f06edaaa5b0b75da96fca831be83d5ff21 100644 (file)
@@ -210,7 +210,8 @@ tests += restest1 preloadtest loadfail multiload origtest resolvfail \
         tst-filterobj tst-filterobj-dlopen tst-auxobj tst-auxobj-dlopen \
         tst-audit14 tst-audit15 tst-audit16 \
         tst-single_threaded tst-single_threaded-pthread \
-        tst-tls-ie tst-tls-ie-dlmopen
+        tst-tls-ie tst-tls-ie-dlmopen \
+        argv0test
 #       reldep9
 tests-internal += loadtest unload unload2 circleload1 \
         neededtest neededtest2 neededtest3 neededtest4 \
@@ -414,7 +415,7 @@ endif
 ifeq (yes,$(build-shared))
 ifeq ($(run-built-tests),yes)
 tests-special += $(objpfx)tst-pathopt.out $(objpfx)tst-rtld-load-self.out \
-                $(objpfx)tst-rtld-preload.out
+                $(objpfx)tst-rtld-preload.out $(objpfx)argv0test.out
 endif
 tests-special += $(objpfx)check-textrel.out $(objpfx)check-execstack.out \
                 $(objpfx)check-wx-segment.out \
@@ -1796,3 +1797,10 @@ $(objpfx)tst-tls-ie-dlmopen.out: \
   $(objpfx)tst-tls-ie-mod6.so
 
 $(objpfx)tst-tls-surplus: $(libdl)
+
+$(objpfx)argv0test.out: tst-rtld-argv0.sh $(objpfx)ld.so \
+                       $(objpfx)argv0test
+       $(SHELL) $< $(objpfx)ld.so $(objpfx)argv0test \
+            '$(test-wrapper-env)' '$(run_program_env)' \
+            '$(rpath-link)' 'test-argv0' > $@; \
+    $(evaluate-test)
diff --git a/elf/argv0test.c b/elf/argv0test.c
new file mode 100644 (file)
index 0000000..c22ba5e
--- /dev/null
@@ -0,0 +1,31 @@
+/* Test for --argv0 option ld.so.
+
+   Copyright (C) 2020 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library 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
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <https://www.gnu.org/licenses/>.  */
+
+#include <string.h>
+#include <support/check.h>
+
+static int
+do_test (int argc, char **argv)
+{
+  TEST_COMPARE_STRING (argv[0], "test-argv0");
+  return 0;
+}
+
+#define TEST_FUNCTION_ARGV do_test
+#include <support/test-driver.c>
index 5b882163fae5ea8a289b6fae197bc4dc1af43826..9918fda05e76212143d23c8cd4559dcca96d18fe 100644 (file)
@@ -1202,6 +1202,8 @@ dl_main (const ElfW(Phdr) *phdr,
         installing it.  */
       rtld_is_main = true;
 
+      char *argv0 = NULL;
+
       /* Note the place where the dynamic linker actually came from.  */
       GL(dl_rtld_map).l_name = rtld_progname;
 
@@ -1259,6 +1261,14 @@ dl_main (const ElfW(Phdr) *phdr,
        else if (! strcmp (_dl_argv[1], "--preload") && _dl_argc > 2)
          {
            preloadarg = _dl_argv[2];
+           _dl_skip_args += 2;
+           _dl_argc -= 2;
+           _dl_argv += 2;
+         }
+       else if (! strcmp (_dl_argv[1], "--argv0") && _dl_argc > 2)
+         {
+           argv0 = _dl_argv[2];
+
            _dl_skip_args += 2;
            _dl_argc -= 2;
            _dl_argv += 2;
@@ -1292,7 +1302,8 @@ of this helper program; chances are you did not intend to run this program.\n\
   --inhibit-rpath LIST  ignore RUNPATH and RPATH information in object names\n\
                        in LIST\n\
   --audit LIST          use objects named in LIST as auditors\n\
-  --preload LIST        preload objects named in LIST\n");
+  --preload LIST        preload objects named in LIST\n\
+  --argv0 STRING        set argv[0] to STRING before running\n");
 
       ++_dl_skip_args;
       --_dl_argc;
@@ -1384,6 +1395,10 @@ of this helper program; chances are you did not intend to run this program.\n\
            break;
          }
 #endif
+
+      /* Set the argv[0] string now that we've processed the executable.  */
+      if (argv0 != NULL)
+        _dl_argv[0] = argv0;
     }
   else
     {
diff --git a/elf/tst-rtld-argv0.sh b/elf/tst-rtld-argv0.sh
new file mode 100755 (executable)
index 0000000..14d97fb
--- /dev/null
@@ -0,0 +1,37 @@
+#!/bin/sh
+# Test for --argv0 option ld.so.
+# Copyright (C) 2020 Free Software Foundation, Inc.
+# This file is part of the GNU C Library.
+#
+# The GNU C Library is free software; you can redistribute it and/or
+# modify it under the terms of the GNU Lesser General Public
+# License as published by the Free Software Foundation; either
+# version 2.1 of the License, or (at your option) any later version.
+#
+# The GNU C Library 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
+# Lesser General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public
+# License along with the GNU C Library; if not, see
+# <https://www.gnu.org/licenses/>.
+
+set -e
+
+rtld=$1
+test_program=$2
+test_wrapper_env=$3
+run_program_env=$4
+library_path=$5
+argv0=$6
+
+echo "# [${test_wrapper_env}] [${run_program_env}] [$rtld] [--library-path]" \
+     "[$library_path] [--argv0] [$argv0] [$test_program]"
+${test_wrapper_env} \
+${run_program_env} \
+$rtld --library-path "$library_path" \
+  --argv0 "$argv0" $test_program 2>&1 && rc=0 || rc=$?
+echo "# exit status $rc"
+
+exit $rc