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