e354a5
commit c6702789344043fa998923c8f32ed0bdb2edfa9c
e354a5
Author: Vincent Mihalkovic <vmihalko@redhat.com>
e354a5
Date:   Tue Sep 29 12:34:39 2020 +0200
e354a5
e354a5
    ld.so: add an --argv0 option [BZ #16124]
e354a5
e354a5
Conflicts:
e354a5
	elf/Makefile
e354a5
	  (Missing test backports.)
e354a5
e354a5
diff --git a/elf/Makefile b/elf/Makefile
e354a5
index 82b5b4a07495c805..837a070c267527e1 100644
e354a5
--- a/elf/Makefile
e354a5
+++ b/elf/Makefile
e354a5
@@ -198,7 +198,8 @@ tests += restest1 preloadtest loadfail multiload origtest resolvfail \
e354a5
 	 tst-dlopenfail tst-dlopenfail-2 \
e354a5
 	 tst-filterobj tst-filterobj-dlopen tst-auxobj tst-auxobj-dlopen \
e354a5
 	 tst-audit14 tst-audit15 tst-audit16 \
e354a5
-	 tst-tls-ie tst-tls-ie-dlmopen
e354a5
+	 tst-tls-ie tst-tls-ie-dlmopen \
e354a5
+	 argv0test
e354a5
 #	 reldep9
e354a5
 tests-internal += loadtest unload unload2 circleload1 \
e354a5
 	 neededtest neededtest2 neededtest3 neededtest4 \
e354a5
@@ -395,7 +396,7 @@ endif
e354a5
 ifeq (yes,$(build-shared))
e354a5
 ifeq ($(run-built-tests),yes)
e354a5
 tests-special += $(objpfx)tst-pathopt.out $(objpfx)tst-rtld-load-self.out \
e354a5
-		 $(objpfx)tst-rtld-preload.out
e354a5
+		 $(objpfx)tst-rtld-preload.out $(objpfx)argv0test.out
e354a5
 endif
e354a5
 tests-special += $(objpfx)check-textrel.out $(objpfx)check-execstack.out \
e354a5
 		 $(objpfx)check-wx-segment.out \
e354a5
@@ -1716,3 +1717,10 @@ $(objpfx)tst-tls-ie-dlmopen.out: \
e354a5
   $(objpfx)tst-tls-ie-mod6.so
e354a5
 
e354a5
 $(objpfx)tst-tls-surplus: $(libdl)
e354a5
+
e354a5
+$(objpfx)argv0test.out: tst-rtld-argv0.sh $(objpfx)ld.so \
e354a5
+			$(objpfx)argv0test
e354a5
+	$(SHELL) $< $(objpfx)ld.so $(objpfx)argv0test \
e354a5
+            '$(test-wrapper-env)' '$(run_program_env)' \
e354a5
+            '$(rpath-link)' 'test-argv0' > $@; \
e354a5
+    $(evaluate-test)
e354a5
diff --git a/elf/argv0test.c b/elf/argv0test.c
e354a5
new file mode 100644
e354a5
index 0000000000000000..c22ba5ea70ed7dbf
e354a5
--- /dev/null
e354a5
+++ b/elf/argv0test.c
e354a5
@@ -0,0 +1,31 @@
e354a5
+/* Test for --argv0 option ld.so.
e354a5
+
e354a5
+   Copyright (C) 2020 Free Software Foundation, Inc.
e354a5
+   This file is part of the GNU C Library.
e354a5
+
e354a5
+   The GNU C Library is free software; you can redistribute it and/or
e354a5
+   modify it under the terms of the GNU Lesser General Public
e354a5
+   License as published by the Free Software Foundation; either
e354a5
+   version 2.1 of the License, or (at your option) any later version.
e354a5
+
e354a5
+   The GNU C Library is distributed in the hope that it will be useful,
e354a5
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
e354a5
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
e354a5
+   Lesser General Public License for more details.
e354a5
+
e354a5
+   You should have received a copy of the GNU Lesser General Public
e354a5
+   License along with the GNU C Library; if not, see
e354a5
+   <https://www.gnu.org/licenses/>.  */
e354a5
+
e354a5
+#include <string.h>
e354a5
+#include <support/check.h>
e354a5
+
e354a5
+static int
e354a5
+do_test (int argc, char **argv)
e354a5
+{
e354a5
+  TEST_COMPARE_STRING (argv[0], "test-argv0");
e354a5
+  return 0;
e354a5
+}
e354a5
+
e354a5
+#define TEST_FUNCTION_ARGV do_test
e354a5
+#include <support/test-driver.c>
e354a5
diff --git a/elf/rtld.c b/elf/rtld.c
e354a5
index 67441ac6f252350e..4107a215abd554f4 100644
e354a5
--- a/elf/rtld.c
e354a5
+++ b/elf/rtld.c
e354a5
@@ -1196,6 +1196,8 @@ dl_main (const ElfW(Phdr) *phdr,
e354a5
 	 installing it.  */
e354a5
       rtld_is_main = true;
e354a5
 
e354a5
+      char *argv0 = NULL;
e354a5
+
e354a5
       /* Note the place where the dynamic linker actually came from.  */
e354a5
       GL(dl_rtld_map).l_name = rtld_progname;
e354a5
 
e354a5
@@ -1253,6 +1255,14 @@ dl_main (const ElfW(Phdr) *phdr,
e354a5
 	else if (! strcmp (_dl_argv[1], "--preload") && _dl_argc > 2)
e354a5
 	  {
e354a5
 	    preloadarg = _dl_argv[2];
e354a5
+	    _dl_skip_args += 2;
e354a5
+	    _dl_argc -= 2;
e354a5
+	    _dl_argv += 2;
e354a5
+	  }
e354a5
+	else if (! strcmp (_dl_argv[1], "--argv0") && _dl_argc > 2)
e354a5
+	  {
e354a5
+	    argv0 = _dl_argv[2];
e354a5
+
e354a5
 	    _dl_skip_args += 2;
e354a5
 	    _dl_argc -= 2;
e354a5
 	    _dl_argv += 2;
e354a5
@@ -1286,7 +1296,8 @@ of this helper program; chances are you did not intend to run this program.\n\
e354a5
   --inhibit-rpath LIST  ignore RUNPATH and RPATH information in object names\n\
e354a5
 			in LIST\n\
e354a5
   --audit LIST          use objects named in LIST as auditors\n\
e354a5
-  --preload LIST        preload objects named in LIST\n");
e354a5
+  --preload LIST        preload objects named in LIST\n\
e354a5
+  --argv0 STRING        set argv[0] to STRING before running\n");
e354a5
 
e354a5
       ++_dl_skip_args;
e354a5
       --_dl_argc;
e354a5
@@ -1378,6 +1389,10 @@ of this helper program; chances are you did not intend to run this program.\n\
e354a5
 	    break;
e354a5
 	  }
e354a5
 #endif
e354a5
+
e354a5
+      /* Set the argv[0] string now that we've processed the executable.  */
e354a5
+      if (argv0 != NULL)
e354a5
+        _dl_argv[0] = argv0;
e354a5
     }
e354a5
   else
e354a5
     {
e354a5
diff --git a/elf/tst-rtld-argv0.sh b/elf/tst-rtld-argv0.sh
e354a5
new file mode 100755
e354a5
index 0000000000000000..14d97fb375fc652b
e354a5
--- /dev/null
e354a5
+++ b/elf/tst-rtld-argv0.sh
e354a5
@@ -0,0 +1,37 @@
e354a5
+#!/bin/sh
e354a5
+# Test for --argv0 option ld.so.
e354a5
+# Copyright (C) 2020 Free Software Foundation, Inc.
e354a5
+# This file is part of the GNU C Library.
e354a5
+#
e354a5
+# The GNU C Library is free software; you can redistribute it and/or
e354a5
+# modify it under the terms of the GNU Lesser General Public
e354a5
+# License as published by the Free Software Foundation; either
e354a5
+# version 2.1 of the License, or (at your option) any later version.
e354a5
+#
e354a5
+# The GNU C Library is distributed in the hope that it will be useful,
e354a5
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
e354a5
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
e354a5
+# Lesser General Public License for more details.
e354a5
+#
e354a5
+# You should have received a copy of the GNU Lesser General Public
e354a5
+# License along with the GNU C Library; if not, see
e354a5
+# <https://www.gnu.org/licenses/>.
e354a5
+
e354a5
+set -e
e354a5
+
e354a5
+rtld=$1
e354a5
+test_program=$2
e354a5
+test_wrapper_env=$3
e354a5
+run_program_env=$4
e354a5
+library_path=$5
e354a5
+argv0=$6
e354a5
+
e354a5
+echo "# [${test_wrapper_env}] [${run_program_env}] [$rtld] [--library-path]" \
e354a5
+     "[$library_path] [--argv0] [$argv0] [$test_program]"
e354a5
+${test_wrapper_env} \
e354a5
+${run_program_env} \
e354a5
+$rtld --library-path "$library_path" \
e354a5
+  --argv0 "$argv0" $test_program 2>&1 && rc=0 || rc=$?
e354a5
+echo "# exit status $rc"
e354a5
+
e354a5
+exit $rc