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