unofficial mirror of libc-alpha@sourceware.org
 help / color / mirror / Atom feed
* [PATCH] ld.so: add an --argv0 option
@ 2016-04-23  5:28 Mike Frysinger
  2016-04-23 12:21 ` Florian Weimer
  0 siblings, 1 reply; 22+ messages in thread
From: Mike Frysinger @ 2016-04-23  5:28 UTC (permalink / raw)
  To: libc-alpha; +Cc: shenhan

Sometimes when you run a program you want the argv[0] string passed to
the app to be different than the actual path you used to load it.  We
can't do this today with invoking via ld.so which can be limiting --
some programs like to inspect their argv[0] and make decisions as to
how it should (re)exec itself or helper tools.  For example, clang and
gcc both do argv[0] inspection to support relocatable toolchains.
---
 elf/rtld.c | 17 ++++++++++++++++-
 1 file changed, 16 insertions(+), 1 deletion(-)

diff --git a/elf/rtld.c b/elf/rtld.c
index 647661c..a42b5f6 100644
--- a/elf/rtld.c
+++ b/elf/rtld.c
@@ -796,6 +796,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;
 
@@ -850,6 +852,14 @@ dl_main (const ElfW(Phdr) *phdr,
 	    _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;
+	  }
 	else
 	  break;
 
@@ -878,7 +888,8 @@ of this helper program; chances are you did not intend to run this program.\n\
 			variable LD_LIBRARY_PATH\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");
+  --audit LIST          use objects named in LIST as auditors\n\
+  --argv0 STRING        set argv[0] to STRING before running\n");
 
       ++_dl_skip_args;
       --_dl_argc;
@@ -971,6 +982,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
     {
-- 
2.7.4



^ permalink raw reply related	[flat|nested] 22+ messages in thread

* Re: [PATCH] ld.so: add an --argv0 option
  2016-04-23  5:28 Mike Frysinger
@ 2016-04-23 12:21 ` Florian Weimer
  2016-04-23 20:06   ` Mike Frysinger
  0 siblings, 1 reply; 22+ messages in thread
From: Florian Weimer @ 2016-04-23 12:21 UTC (permalink / raw)
  To: Mike Frysinger; +Cc: libc-alpha, shenhan

* Mike Frysinger:

> Sometimes when you run a program you want the argv[0] string passed to
> the app to be different than the actual path you used to load it.  We
> can't do this today with invoking via ld.so which can be limiting --
> some programs like to inspect their argv[0] and make decisions as to
> how it should (re)exec itself or helper tools.  For example, clang and
> gcc both do argv[0] inspection to support relocatable toolchains.

Interesting idea.  I think it's sufficiently to include this, but it
needs a ChangeLog entry and a test case.


^ permalink raw reply	[flat|nested] 22+ messages in thread

* Re: [PATCH] ld.so: add an --argv0 option
  2016-04-23 12:21 ` Florian Weimer
@ 2016-04-23 20:06   ` Mike Frysinger
  2016-04-24 12:21     ` Florian Weimer
  0 siblings, 1 reply; 22+ messages in thread
From: Mike Frysinger @ 2016-04-23 20:06 UTC (permalink / raw)
  To: Florian Weimer; +Cc: libc-alpha, shenhan

[-- Attachment #1: Type: text/plain, Size: 869 bytes --]

On 23 Apr 2016 14:21, Florian Weimer wrote:
> * Mike Frysinger:
> > Sometimes when you run a program you want the argv[0] string passed to
> > the app to be different than the actual path you used to load it.  We
> > can't do this today with invoking via ld.so which can be limiting --
> > some programs like to inspect their argv[0] and make decisions as to
> > how it should (re)exec itself or helper tools.  For example, clang and
> > gcc both do argv[0] inspection to support relocatable toolchains.
> 
> Interesting idea.  I think it's sufficiently to include this, but it
> needs a ChangeLog entry and a test case.

for the test, seems like i'll have to write a small C file that just
displays its argv, and then another shell script which runs the ldso
against that simple program and checks the output.  unless anyone has
a simpler idea.
-mike

[-- Attachment #2: Digital signature --]
[-- Type: application/pgp-signature, Size: 819 bytes --]

^ permalink raw reply	[flat|nested] 22+ messages in thread

* Re: [PATCH] ld.so: add an --argv0 option
  2016-04-23 20:06   ` Mike Frysinger
@ 2016-04-24 12:21     ` Florian Weimer
  0 siblings, 0 replies; 22+ messages in thread
From: Florian Weimer @ 2016-04-24 12:21 UTC (permalink / raw)
  To: libc-alpha; +Cc: shenhan

* Mike Frysinger:

> On 23 Apr 2016 14:21, Florian Weimer wrote:
>> * Mike Frysinger:
>> > Sometimes when you run a program you want the argv[0] string passed to
>> > the app to be different than the actual path you used to load it.  We
>> > can't do this today with invoking via ld.so which can be limiting --
>> > some programs like to inspect their argv[0] and make decisions as to
>> > how it should (re)exec itself or helper tools.  For example, clang and
>> > gcc both do argv[0] inspection to support relocatable toolchains.
>> 
>> Interesting idea.  I think it's sufficiently to include this, but it
>> needs a ChangeLog entry and a test case.
>
> for the test, seems like i'll have to write a small C file that just
> displays its argv, and then another shell script which runs the ldso
> against that simple program and checks the output.  unless anyone has
> a simpler idea.

You'll need the shell script wrapper.

Supplying the expected value in another argument, with some prefix and
suffix character, and comparing argv[0] to the extracted orignal
argument, is likely simpler than writing argv[0] to standard output
and reading it back from the shell script.


^ permalink raw reply	[flat|nested] 22+ messages in thread

* [PATCH] ld.so: add an --argv0 option
@ 2020-07-22  8:33 Vincent Mihalkovic via Libc-alpha
  2020-07-22  9:00 ` Florian Weimer via Libc-alpha
  0 siblings, 1 reply; 22+ messages in thread
From: Vincent Mihalkovic via Libc-alpha @ 2020-07-22  8:33 UTC (permalink / raw)
  To: libc-alpha

[-- Attachment #1: Type: text/plain, Size: 218 bytes --]

Hi,
a few years ago there was an effort for adding --argv0 option:
https://sourceware.org/legacy-ml/libc-alpha/2016-04/msg00576.html.
I made the old patch actual for version 2.31.9000, added a test case and
changelog.

[-- Attachment #2: changelog.txt --]
[-- Type: text/plain, Size: 240 bytes --]

2020-07-22  Vincent Mihalkovic  <vmihalko@redhat.com>

	* elf/Makefile: added argv0 test case, Modified.
	* elf/rtld.c: added --argv0 option, Modified.
	* elf/argv0test.c: test case, New file.
	* elf/tst-rtld-argv0.sh: test case, New file.

[-- Attachment #3: argv0.patch --]
[-- Type: text/x-patch, Size: 4788 bytes --]

diff --git a/elf/Makefile b/elf/Makefile
index 0b78721848..f38904d831 100644
--- a/elf/Makefile
+++ b/elf/Makefile
@@ -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,11 @@ $(objpfx)tst-tls-ie-dlmopen.out: \
   $(objpfx)tst-tls-ie-mod6.so
 
 $(objpfx)tst-tls-surplus: $(libdl)
+
+ARGV0 = test-argv0
+$(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)' '$(ARGV0)' > $@; \
+    $(evaluate-test)
diff --git a/elf/argv0test.c b/elf/argv0test.c
new file mode 100644
index 0000000000..4c79bebf23
--- /dev/null
+++ b/elf/argv0test.c
@@ -0,0 +1,13 @@
+#include <stdio.h>   // for printf
+#include <string.h>  // for strcmp
+
+
+int main( int argc, char *argv[] ) {
+	int result = strcmp( argv[0], "test-argv0");
+ 
+	printf ("argv[0] = %s, strcmp( argv[0], \"test-argv0\" ) = %d, %s\n", \
+					argv[0], result, !result ? "ok" : "wrong");
+
+	return result;
+
+}
diff --git a/elf/rtld.c b/elf/rtld.c
index 5b882163fa..cafa4f9bd3 100644
--- a/elf/rtld.c
+++ b/elf/rtld.c
@@ -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,11 @@ 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
index 0000000000..5f873b6c5c
--- /dev/null
+++ b/elf/tst-rtld-argv0.sh
@@ -0,0 +1,37 @@
+#!/bin/sh
+# Test --argv0 argument ld.so.
+# Copyright (C) 2019-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

^ permalink raw reply related	[flat|nested] 22+ messages in thread

* Re: [PATCH] ld.so: add an --argv0 option
  2020-07-22  8:33 [PATCH] " Vincent Mihalkovic via Libc-alpha
@ 2020-07-22  9:00 ` Florian Weimer via Libc-alpha
  2020-07-26 20:00   ` Vincent Mihalkovic via Libc-alpha
  0 siblings, 1 reply; 22+ messages in thread
From: Florian Weimer via Libc-alpha @ 2020-07-22  9:00 UTC (permalink / raw)
  To: Vincent Mihalkovic via Libc-alpha; +Cc: Vincent Mihalkovic

* Vincent Mihalkovic via Libc-alpha:

> a few years ago there was an effort for adding --argv0 option:
> https://sourceware.org/legacy-ml/libc-alpha/2016-04/msg00576.html.
> I made the old patch actual for version 2.31.9000, added a test case and
> changelog.

Many shells support “exec -a” to get a similar effect.  The advantage is
that this does not perturb the argument vector layout.  Given that, I
think promoting --argv0 would merely introduce further compatibility
issues.

Thanks,
Florian


^ permalink raw reply	[flat|nested] 22+ messages in thread

* Re: [PATCH] ld.so: add an --argv0 option
  2020-07-22  9:00 ` Florian Weimer via Libc-alpha
@ 2020-07-26 20:00   ` Vincent Mihalkovic via Libc-alpha
  2020-07-27  5:54     ` Florian Weimer via Libc-alpha
  0 siblings, 1 reply; 22+ messages in thread
From: Vincent Mihalkovic via Libc-alpha @ 2020-07-26 20:00 UTC (permalink / raw)
  To: Florian Weimer, libc-alpha, Kamil Dudka

Hi,

Sorry, I forgot to CC the libc-alpha mailing-list on my reply.

We do not need the option for shell scripting, where it is indeed not
needed,
as you say.  We are developing a tool for running dynamic analysis tools on
RPM packages fully automatically.  The tool appends custom linker flags
while
executing the %build section of RPMs to make the binaries use our custom ELF
interpreter.  While running the %check section, the interpreter takes care
of
running the binaries built in %build through the selected dynamic analyzer
in
a way that does not break the testing framework.  For this to work, we need
to run dynamic linker explicitly.  There is currently no way to preserve the
original argv[0], which some programs are sensitive to.  This unnecessarily
breaks the tests running in %check of some RPM packages.

An experimental implementation of the custom ELF interpreter is available
here:

    https://github.com/kdudka/cswrap/pull/2

thanks for considering this idea,
vincent mihalkovic

On Wed, Jul 22, 2020 at 11:00 AM Florian Weimer <fweimer@redhat.com> wrote:

> * Vincent Mihalkovic via Libc-alpha:
>
> > a few years ago there was an effort for adding --argv0 option:
> > https://sourceware.org/legacy-ml/libc-alpha/2016-04/msg00576.html.
> > I made the old patch actual for version 2.31.9000, added a test case and
> > changelog.
>
> Many shells support “exec -a” to get a similar effect.  The advantage is
> that this does not perturb the argument vector layout.  Given that, I
> think promoting --argv0 would merely introduce further compatibility
> issues.
>
> Thanks,
> Florian
>
>

^ permalink raw reply	[flat|nested] 22+ messages in thread

* Re: [PATCH] ld.so: add an --argv0 option
  2020-07-26 20:00   ` Vincent Mihalkovic via Libc-alpha
@ 2020-07-27  5:54     ` Florian Weimer via Libc-alpha
  0 siblings, 0 replies; 22+ messages in thread
From: Florian Weimer via Libc-alpha @ 2020-07-27  5:54 UTC (permalink / raw)
  To: Vincent Mihalkovic; +Cc: libc-alpha

* Vincent Mihalkovic:

> Sorry, I forgot to CC the libc-alpha mailing-list on my reply.

And I think this won't make it to the list due to the HTML filters, so
here's a full quote:

> We do not need the option for shell scripting, where it is indeed not
> needed, as you say.  We are developing a tool for running dynamic
> analysis tools on RPM packages fully automatically.  The tool appends
> custom linker flags while executing the %build section of RPMs to make
> the binaries use our custom ELF interpreter.  While running the %check
> section, the interpreter takes care of running the binaries built in
> %build through the selected dynamic analyzer in a way that does not
> break the testing framework.  For this to work, we need to run dynamic
> linker explicitly.  There is currently no way to preserve the original
> argv[0], which some programs are sensitive to.  This unnecessarily
> breaks the tests running in %check of some RPM packages.
>
> An experimental implementation of the custom ELF interpreter is available
> here:
>
>     https://github.com/kdudka/cswrap/pull/2
>
> thanks for considering this idea,
> vincent mihalkovic

I was wrong about this, and it is not possible to get the desired
behavior using “exec -a”.  The question remains if just updating the
pointer is sufficient in this context, or if a more elaborate copying
operation is needed to preserve the expected semantics of the argument
vector.

I guest we could add --argv0 now (well, after the 2.32 release), and if
it's incompatible with some applications, we can perhaps tweak it later.

Thanks,
Florian


^ permalink raw reply	[flat|nested] 22+ messages in thread

* [PATCH] ld.so: add an --argv0 option
@ 2020-08-12 11:06 Vincent Mihalkovic via Libc-alpha
  2020-08-25 15:10 ` Adhemerval Zanella via Libc-alpha
  0 siblings, 1 reply; 22+ messages in thread
From: Vincent Mihalkovic via Libc-alpha @ 2020-08-12 11:06 UTC (permalink / raw)
  To: libc-alpha; +Cc: Florian Weimer

[-- Attachment #1: Type: text/plain, Size: 343 bytes --]

Hi,

I have seen that the development branch is open for glibc 2.33.
In reaction to previous e-mail communication with Florian Weimer:

> "I guest we could add --argv0 now (well, after the 2.32 release), and if
> it's incompatible with some applications, we can perhaps tweak it later."
>
I'm sending my argv0 patch again.

vincent mihalkovic

[-- Attachment #2: changelog.txt --]
[-- Type: text/plain, Size: 240 bytes --]

2020-07-22  Vincent Mihalkovic  <vmihalko@redhat.com>

	* elf/Makefile: added argv0 test case, Modified.
	* elf/rtld.c: added --argv0 option, Modified.
	* elf/argv0test.c: test case, New file.
	* elf/tst-rtld-argv0.sh: test case, New file.

[-- Attachment #3: argv0.patch --]
[-- Type: application/x-patch, Size: 4788 bytes --]

^ permalink raw reply	[flat|nested] 22+ messages in thread

* Re: [PATCH] ld.so: add an --argv0 option
  2020-08-12 11:06 [PATCH] ld.so: add an --argv0 option Vincent Mihalkovic via Libc-alpha
@ 2020-08-25 15:10 ` Adhemerval Zanella via Libc-alpha
  2020-08-26 11:53   ` [PATCH v2] " vmihalko--- via Libc-alpha
  0 siblings, 1 reply; 22+ messages in thread
From: Adhemerval Zanella via Libc-alpha @ 2020-08-25 15:10 UTC (permalink / raw)
  To: libc-alpha



On 12/08/2020 08:06, Vincent Mihalkovic via Libc-alpha wrote:
> Hi,
> 
> I have seen that the development branch is open for glibc 2.33.
> In reaction to previous e-mail communication with Florian Weimer:
> 
>> "I guest we could add --argv0 now (well, after the 2.32 release), and if
>> it's incompatible with some applications, we can perhaps tweak it later."
>>
> I'm sending my argv0 patch again.
> 
> vincent mihalkovic
> 

No need to send ChangeLog.txt anymore.  As a side note I would like ask you send
the patch as inline instead of attachments.

> diff --git a/elf/Makefile b/elf/Makefile
> index 0b78721848..f38904d831 100644
> --- a/elf/Makefile
> +++ b/elf/Makefile
> @@ -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 \

Ok.

> @@ -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,11 @@ $(objpfx)tst-tls-ie-dlmopen.out: \
>    $(objpfx)tst-tls-ie-mod6.so
>  
>  $(objpfx)tst-tls-surplus: $(libdl)
> +
> +ARGV0 = test-argv0
> +$(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)' '$(ARGV0)' > $@; \
> +    $(evaluate-test)

Ok.

> diff --git a/elf/argv0test.c b/elf/argv0test.c
> new file mode 100644
> index 0000000000..4c79bebf23
> --- /dev/null
> +++ b/elf/argv0test.c
> @@ -0,0 +1,13 @@
> +#include <stdio.h>   // for printf
> +#include <string.h>  // for strcmp
> +
> +
> +int main( int argc, char *argv[] ) {
> +	int result = strcmp( argv[0], "test-argv0");
> + 
> +	printf ("argv[0] = %s, strcmp( argv[0], \"test-argv0\" ) = %d, %s\n", \
> +					argv[0], result, !result ? "ok" : "wrong");
> +
> +	return result;
> +
> +}
> diff --git a/elf/rtld.c b/elf/rtld.c

Please use libsupport even for such simple tests. Also use glibc code style [1].

> index 5b882163fa..cafa4f9bd3 100644
> --- a/elf/rtld.c
> +++ b/elf/rtld.c
> @@ -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;

Ok.

> @@ -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,11 @@ 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;
> +	  }

Use the glibc code style and conventions [1]. In this case just drop the brackets.

>      }
>    else
>      {
> diff --git a/elf/tst-rtld-argv0.sh b/elf/tst-rtld-argv0.sh
> new file mode 100755
> index 0000000000..5f873b6c5c
> --- /dev/null
> +++ b/elf/tst-rtld-argv0.sh
> @@ -0,0 +1,37 @@
> +#!/bin/sh
> +# Test --argv0 argument ld.so.
> +# Copyright (C) 2019-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

Ok.

[1] https://sourceware.org/glibc/wiki/Style_and_Conventions

^ permalink raw reply	[flat|nested] 22+ messages in thread

* [PATCH v2] ld.so: add an --argv0 option
  2020-08-25 15:10 ` Adhemerval Zanella via Libc-alpha
@ 2020-08-26 11:53   ` vmihalko--- via Libc-alpha
  2020-08-26 13:55     ` Adhemerval Zanella via Libc-alpha
  0 siblings, 1 reply; 22+ messages in thread
From: vmihalko--- via Libc-alpha @ 2020-08-26 11:53 UTC (permalink / raw)
  To: libc-alpha

From: Vincent Mihalkovic <vmihalko@redhat.com>

---
 elf/Makefile          | 13 +++++++++++--
 elf/argv0test.c       | 18 ++++++++++++++++++
 elf/rtld.c            | 17 ++++++++++++++++-
 elf/tst-rtld-argv0.sh | 37 +++++++++++++++++++++++++++++++++++++
 4 files changed, 82 insertions(+), 3 deletions(-)
 create mode 100644 elf/argv0test.c
 create mode 100755 elf/tst-rtld-argv0.sh

diff --git a/elf/Makefile b/elf/Makefile
index 0b787218..f38904d8 100644
--- a/elf/Makefile
+++ b/elf/Makefile
@@ -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,11 @@ $(objpfx)tst-tls-ie-dlmopen.out: \
   $(objpfx)tst-tls-ie-mod6.so
 
 $(objpfx)tst-tls-surplus: $(libdl)
+
+ARGV0 = test-argv0
+$(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)' '$(ARGV0)' > $@; \
+    $(evaluate-test)
diff --git a/elf/argv0test.c b/elf/argv0test.c
new file mode 100644
index 00000000..137ced8d
--- /dev/null
+++ b/elf/argv0test.c
@@ -0,0 +1,18 @@
+#include <stdio.h>   // for printf
+#include <string.h>  // for strcmp
+
+static int
+do_test (int argc, char **argv)
+{
+	int result = strcmp (argv[0], "test-argv0");
+
+	printf ("argv[0] = %s, strcmp (argv[0], \"test-argv0\") = %d, %s\n",
+					argv[0], result, !result ? "ok" : "wrong");
+
+	return result;
+
+}
+
+
+#define TEST_FUNCTION_ARGV do_test
+#include <support/test-driver.c>
diff --git a/elf/rtld.c b/elf/rtld.c
index 5b882163..e66e033c 100644
--- a/elf/rtld.c
+++ b/elf/rtld.c
@@ -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
index 00000000..5f873b6c
--- /dev/null
+++ b/elf/tst-rtld-argv0.sh
@@ -0,0 +1,37 @@
+#!/bin/sh
+# Test --argv0 argument ld.so.
+# Copyright (C) 2019-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
-- 
2.25.4


^ permalink raw reply related	[flat|nested] 22+ messages in thread

* Re: [PATCH v2] ld.so: add an --argv0 option
  2020-08-26 11:53   ` [PATCH v2] " vmihalko--- via Libc-alpha
@ 2020-08-26 13:55     ` Adhemerval Zanella via Libc-alpha
  2020-08-31 13:22       ` [PATCH v3] " vincent via Libc-alpha
  0 siblings, 1 reply; 22+ messages in thread
From: Adhemerval Zanella via Libc-alpha @ 2020-08-26 13:55 UTC (permalink / raw)
  To: libc-alpha



On 26/08/2020 08:53, vmihalko--- via Libc-alpha wrote:
> From: Vincent Mihalkovic <vmihalko@redhat.com>

The testcase still misses some pieces (copyright headers, indentation, libsupport
usage).

> 
> ---
>  elf/Makefile          | 13 +++++++++++--
>  elf/argv0test.c       | 18 ++++++++++++++++++
>  elf/rtld.c            | 17 ++++++++++++++++-
>  elf/tst-rtld-argv0.sh | 37 +++++++++++++++++++++++++++++++++++++
>  4 files changed, 82 insertions(+), 3 deletions(-)
>  create mode 100644 elf/argv0test.c
>  create mode 100755 elf/tst-rtld-argv0.sh
> 
> diff --git a/elf/Makefile b/elf/Makefile
> index 0b787218..f38904d8 100644
> --- a/elf/Makefile
> +++ b/elf/Makefile
> @@ -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,11 @@ $(objpfx)tst-tls-ie-dlmopen.out: \
>    $(objpfx)tst-tls-ie-mod6.so
>  
>  $(objpfx)tst-tls-surplus: $(libdl)
> +
> +ARGV0 = test-argv0
> +$(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)' '$(ARGV0)' > $@; \
> +    $(evaluate-test)
> diff --git a/elf/argv0test.c b/elf/argv0test.c
> new file mode 100644
> index 00000000..137ced8d
> --- /dev/null
> +++ b/elf/argv0test.c
> @@ -0,0 +1,18 @@

It misses the GPL copyright header.

> +#include <stdio.h>   // for printf
> +#include <string.h>  // for strcmp

Afaik the style prefer C-style comments over C++ one, even though they are
added on C99.

> +
> +static int
> +do_test (int argc, char **argv)
> +{
> +	int result = strcmp (argv[0], "test-argv0");
> +
> +	printf ("argv[0] = %s, strcmp (argv[0], \"test-argv0\") = %d, %s\n",
> +					argv[0], result, !result ? "ok" : "wrong");
> +
> +	return result;
> +
> +}
> +
> +
> +#define TEST_FUNCTION_ARGV do_test
> +#include <support/test-driver.c>

The indentation it still off, it should be use double space not four space.
Also a simple implementation using libsupport would be:

  #include <support/check.h>

  static int
  do_test (int argc, char **argv)
  {
     TEST_COMPARE_BLOB (argv[0], strlen (argv[0]),
			"test-argv0", strlen ("test-argv0"));
     return 0;
  }

  #define TEST_FUNCTION_ARGV do_test
  #include <support/test-driver.c>

> diff --git a/elf/rtld.c b/elf/rtld.c
> index 5b882163..e66e033c 100644
> --- a/elf/rtld.c
> +++ b/elf/rtld.c
> @@ -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
> index 00000000..5f873b6c
> --- /dev/null
> +++ b/elf/tst-rtld-argv0.sh
> @@ -0,0 +1,37 @@
> +#!/bin/sh
> +# Test --argv0 argument ld.so.
> +# Copyright (C) 2019-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
> 

^ permalink raw reply	[flat|nested] 22+ messages in thread

* [PATCH v3] ld.so: add an --argv0 option
  2020-08-26 13:55     ` Adhemerval Zanella via Libc-alpha
@ 2020-08-31 13:22       ` vincent via Libc-alpha
  2020-09-01 15:09         ` Adhemerval Zanella via Libc-alpha
  0 siblings, 1 reply; 22+ messages in thread
From: vincent via Libc-alpha @ 2020-08-31 13:22 UTC (permalink / raw)
  To: libc-alpha

From: Vincent Mihalkovic <vmihalko@redhat.com>

I hope everything is fine now, thanks for the feedback and help with
the patch. I was writing the test case against this documentation:
https://sourceware.org/glibc/wiki/Testing/Testsuite and could't find
any documentation for libsupport, that's why I didn't use it.

---
 elf/Makefile          | 13 +++++++++++--
 elf/argv0test.c       | 32 ++++++++++++++++++++++++++++++++
 elf/rtld.c            | 17 ++++++++++++++++-
 elf/tst-rtld-argv0.sh | 37 +++++++++++++++++++++++++++++++++++++
 4 files changed, 96 insertions(+), 3 deletions(-)
 create mode 100644 elf/argv0test.c
 create mode 100755 elf/tst-rtld-argv0.sh

diff --git a/elf/Makefile b/elf/Makefile
index 0b787218..f38904d8 100644
--- a/elf/Makefile
+++ b/elf/Makefile
@@ -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,11 @@ $(objpfx)tst-tls-ie-dlmopen.out: \
   $(objpfx)tst-tls-ie-mod6.so
 
 $(objpfx)tst-tls-surplus: $(libdl)
+
+ARGV0 = test-argv0
+$(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)' '$(ARGV0)' > $@; \
+    $(evaluate-test)
diff --git a/elf/argv0test.c b/elf/argv0test.c
new file mode 100644
index 00000000..6c6c7e9a
--- /dev/null
+++ b/elf/argv0test.c
@@ -0,0 +1,32 @@
+/* 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_BLOB (argv[0], strlen (argv[0]),
+                       "test-argv0", strlen ("test-argv0"));
+return 0;
+}
+
+#define TEST_FUNCTION_ARGV do_test
+#include <support/test-driver.c>
diff --git a/elf/rtld.c b/elf/rtld.c
index 5b882163..e66e033c 100644
--- a/elf/rtld.c
+++ b/elf/rtld.c
@@ -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
index 00000000..14d97fb3
--- /dev/null
+++ b/elf/tst-rtld-argv0.sh
@@ -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
-- 
2.25.4


^ permalink raw reply related	[flat|nested] 22+ messages in thread

* Re: [PATCH v3] ld.so: add an --argv0 option
  2020-08-31 13:22       ` [PATCH v3] " vincent via Libc-alpha
@ 2020-09-01 15:09         ` Adhemerval Zanella via Libc-alpha
  2020-09-14 14:17           ` [PATCH v4] " vincent via Libc-alpha
  0 siblings, 1 reply; 22+ messages in thread
From: Adhemerval Zanella via Libc-alpha @ 2020-09-01 15:09 UTC (permalink / raw)
  To: libc-alpha, vmihalko



On 31/08/2020 10:22, vincent via Libc-alpha wrote:
> From: Vincent Mihalkovic <vmihalko@redhat.com>
> 
> I hope everything is fine now, thanks for the feedback and help with
> the patch. I was writing the test case against this documentation:
> https://sourceware.org/glibc/wiki/Testing/Testsuite and could't find
> any documentation for libsupport, that's why I didn't use it.

On the 'Writing a test case' bullet it has a minimal example where it does
use libsupport and it also points the documentation at support/README-testing.c.
Maybe we can outline it is what we refer as libsupport on patch review.

LGTM with the elf/argv0test.c indentation fixed.

Reviewed-by: Adhemerval Zanella  <adhemerval.zanella@linaro.org>

> 
> ---
>  elf/Makefile          | 13 +++++++++++--
>  elf/argv0test.c       | 32 ++++++++++++++++++++++++++++++++
>  elf/rtld.c            | 17 ++++++++++++++++-
>  elf/tst-rtld-argv0.sh | 37 +++++++++++++++++++++++++++++++++++++
>  4 files changed, 96 insertions(+), 3 deletions(-)
>  create mode 100644 elf/argv0test.c
>  create mode 100755 elf/tst-rtld-argv0.sh
> 
> diff --git a/elf/Makefile b/elf/Makefile
> index 0b787218..f38904d8 100644
> --- a/elf/Makefile
> +++ b/elf/Makefile
> @@ -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 \

Ok.

> @@ -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 \

Ok.

> @@ -1796,3 +1797,11 @@ $(objpfx)tst-tls-ie-dlmopen.out: \
>    $(objpfx)tst-tls-ie-mod6.so
>  
>  $(objpfx)tst-tls-surplus: $(libdl)
> +
> +ARGV0 = test-argv0
> +$(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)' '$(ARGV0)' > $@; \
> +    $(evaluate-test)

Ok.

> diff --git a/elf/argv0test.c b/elf/argv0test.c
> new file mode 100644
> index 00000000..6c6c7e9a
> --- /dev/null
> +++ b/elf/argv0test.c
> @@ -0,0 +1,32 @@
> +/* 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_BLOB (argv[0], strlen (argv[0]),
> +                       "test-argv0", strlen ("test-argv0"));
> +return 0;
> +}
> +
> +#define TEST_FUNCTION_ARGV do_test
> +#include <support/test-driver.c>

Indentation seems off, use double space and align the 'return'.

> diff --git a/elf/rtld.c b/elf/rtld.c
> index 5b882163..e66e033c 100644
> --- a/elf/rtld.c
> +++ b/elf/rtld.c
> @@ -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;

Ok.

> @@ -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
>      {

Ok.

> diff --git a/elf/tst-rtld-argv0.sh b/elf/tst-rtld-argv0.sh
> new file mode 100755
> index 00000000..14d97fb3
> --- /dev/null
> +++ b/elf/tst-rtld-argv0.sh
> @@ -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
> 

Ok.

^ permalink raw reply	[flat|nested] 22+ messages in thread

* [PATCH v4] ld.so: add an --argv0 option
  2020-09-01 15:09         ` Adhemerval Zanella via Libc-alpha
@ 2020-09-14 14:17           ` vincent via Libc-alpha
  2020-09-14 15:03             ` Andreas Schwab
  0 siblings, 1 reply; 22+ messages in thread
From: vincent via Libc-alpha @ 2020-09-14 14:17 UTC (permalink / raw)
  To: libc-alpha

From: Vincent Mihalkovic <vmihalko@redhat.com>

Indentation fixed. 

Thank's for your time and help.

Vincent Mihalkovic
---
 elf/Makefile          | 13 +++++++++++--
 elf/argv0test.c       | 32 ++++++++++++++++++++++++++++++++
 elf/rtld.c            | 17 ++++++++++++++++-
 elf/tst-rtld-argv0.sh | 37 +++++++++++++++++++++++++++++++++++++
 4 files changed, 96 insertions(+), 3 deletions(-)
 create mode 100644 elf/argv0test.c
 create mode 100755 elf/tst-rtld-argv0.sh

diff --git a/elf/Makefile b/elf/Makefile
index 0b787218..f38904d8 100644
--- a/elf/Makefile
+++ b/elf/Makefile
@@ -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,11 @@ $(objpfx)tst-tls-ie-dlmopen.out: \
   $(objpfx)tst-tls-ie-mod6.so
 
 $(objpfx)tst-tls-surplus: $(libdl)
+
+ARGV0 = test-argv0
+$(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)' '$(ARGV0)' > $@; \
+    $(evaluate-test)
diff --git a/elf/argv0test.c b/elf/argv0test.c
new file mode 100644
index 00000000..fcb99581
--- /dev/null
+++ b/elf/argv0test.c
@@ -0,0 +1,32 @@
+/* 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_BLOB (argv[0], strlen (argv[0]),
+                     "test-argv0", strlen ("test-argv0"));
+  return 0;
+}
+
+#define TEST_FUNCTION_ARGV do_test
+#include <support/test-driver.c>
diff --git a/elf/rtld.c b/elf/rtld.c
index 5b882163..e66e033c 100644
--- a/elf/rtld.c
+++ b/elf/rtld.c
@@ -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
index 00000000..14d97fb3
--- /dev/null
+++ b/elf/tst-rtld-argv0.sh
@@ -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
-- 
2.25.4


^ permalink raw reply related	[flat|nested] 22+ messages in thread

* Re: [PATCH v4] ld.so: add an --argv0 option
  2020-09-14 14:17           ` [PATCH v4] " vincent via Libc-alpha
@ 2020-09-14 15:03             ` Andreas Schwab
  2020-09-14 21:32               ` [PATCH v5] " vincent via Libc-alpha
  0 siblings, 1 reply; 22+ messages in thread
From: Andreas Schwab @ 2020-09-14 15:03 UTC (permalink / raw)
  To: vincent via Libc-alpha; +Cc: vmihalko

On Sep 14 2020, vincent via Libc-alpha wrote:

> @@ -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;

Indentation is still off here.

Andreas.

-- 
Andreas Schwab, schwab@linux-m68k.org
GPG Key fingerprint = 7578 EB47 D4E5 4D69 2510  2552 DF73 E780 A9DA AEC1
"And now for something completely different."

^ permalink raw reply	[flat|nested] 22+ messages in thread

* [PATCH v5] ld.so: add an --argv0 option
  2020-09-14 15:03             ` Andreas Schwab
@ 2020-09-14 21:32               ` vincent via Libc-alpha
  2020-09-23  6:06                 ` vincent via Libc-alpha
  0 siblings, 1 reply; 22+ messages in thread
From: vincent via Libc-alpha @ 2020-09-14 21:32 UTC (permalink / raw)
  To: libc-alpha

From: Vincent Mihalkovic <vmihalko@redhat.com>

---
 elf/Makefile          | 13 +++++++++++--
 elf/argv0test.c       | 32 ++++++++++++++++++++++++++++++++
 elf/rtld.c            | 17 ++++++++++++++++-
 elf/tst-rtld-argv0.sh | 37 +++++++++++++++++++++++++++++++++++++
 4 files changed, 96 insertions(+), 3 deletions(-)
 create mode 100644 elf/argv0test.c
 create mode 100755 elf/tst-rtld-argv0.sh

diff --git a/elf/Makefile b/elf/Makefile
index 0b787218..f38904d8 100644
--- a/elf/Makefile
+++ b/elf/Makefile
@@ -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,11 @@ $(objpfx)tst-tls-ie-dlmopen.out: \
   $(objpfx)tst-tls-ie-mod6.so
 
 $(objpfx)tst-tls-surplus: $(libdl)
+
+ARGV0 = test-argv0
+$(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)' '$(ARGV0)' > $@; \
+    $(evaluate-test)
diff --git a/elf/argv0test.c b/elf/argv0test.c
new file mode 100644
index 00000000..fcb99581
--- /dev/null
+++ b/elf/argv0test.c
@@ -0,0 +1,32 @@
+/* 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_BLOB (argv[0], strlen (argv[0]),
+                     "test-argv0", strlen ("test-argv0"));
+  return 0;
+}
+
+#define TEST_FUNCTION_ARGV do_test
+#include <support/test-driver.c>
diff --git a/elf/rtld.c b/elf/rtld.c
index 5b882163..9918fda0 100644
--- a/elf/rtld.c
+++ b/elf/rtld.c
@@ -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
index 00000000..14d97fb3
--- /dev/null
+++ b/elf/tst-rtld-argv0.sh
@@ -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
-- 
2.25.4


^ permalink raw reply related	[flat|nested] 22+ messages in thread

* [PATCH v5] ld.so: add an --argv0 option
  2020-09-14 21:32               ` [PATCH v5] " vincent via Libc-alpha
@ 2020-09-23  6:06                 ` vincent via Libc-alpha
  2020-09-24  9:53                   ` Florian Weimer via Libc-alpha
  0 siblings, 1 reply; 22+ messages in thread
From: vincent via Libc-alpha @ 2020-09-23  6:06 UTC (permalink / raw)
  To: libc-alpha

From: Vincent Mihalkovic <vmihalko@redhat.com>

Hi,

is there any problem with this patch?  

vincent mihalkovic
---
 elf/Makefile          | 13 +++++++++++--
 elf/argv0test.c       | 32 ++++++++++++++++++++++++++++++++
 elf/rtld.c            | 17 ++++++++++++++++-
 elf/tst-rtld-argv0.sh | 37 +++++++++++++++++++++++++++++++++++++
 4 files changed, 96 insertions(+), 3 deletions(-)
 create mode 100644 elf/argv0test.c
 create mode 100755 elf/tst-rtld-argv0.sh

diff --git a/elf/Makefile b/elf/Makefile
index 0b787218..f38904d8 100644
--- a/elf/Makefile
+++ b/elf/Makefile
@@ -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,11 @@ $(objpfx)tst-tls-ie-dlmopen.out: \
   $(objpfx)tst-tls-ie-mod6.so
 
 $(objpfx)tst-tls-surplus: $(libdl)
+
+ARGV0 = test-argv0
+$(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)' '$(ARGV0)' > $@; \
+    $(evaluate-test)
diff --git a/elf/argv0test.c b/elf/argv0test.c
new file mode 100644
index 00000000..fcb99581
--- /dev/null
+++ b/elf/argv0test.c
@@ -0,0 +1,32 @@
+/* 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_BLOB (argv[0], strlen (argv[0]),
+                     "test-argv0", strlen ("test-argv0"));
+  return 0;
+}
+
+#define TEST_FUNCTION_ARGV do_test
+#include <support/test-driver.c>
diff --git a/elf/rtld.c b/elf/rtld.c
index 5b882163..9918fda0 100644
--- a/elf/rtld.c
+++ b/elf/rtld.c
@@ -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
index 00000000..14d97fb3
--- /dev/null
+++ b/elf/tst-rtld-argv0.sh
@@ -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
-- 
2.25.4


^ permalink raw reply related	[flat|nested] 22+ messages in thread

* Re: [PATCH v5] ld.so: add an --argv0 option
  2020-09-23  6:06                 ` vincent via Libc-alpha
@ 2020-09-24  9:53                   ` Florian Weimer via Libc-alpha
  2020-09-24 12:43                     ` [PATCH v6] " vincent via Libc-alpha
  2020-09-29  8:21                     ` vincent via Libc-alpha
  0 siblings, 2 replies; 22+ messages in thread
From: Florian Weimer via Libc-alpha @ 2020-09-24  9:53 UTC (permalink / raw)
  To: vincent via Libc-alpha; +Cc: vmihalko

* vincent via Libc-alpha:

> is there any problem with this patch?

I think adding a NEWS entry would make sense.

> +ARGV0 = test-argv0
> +$(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)' '$(ARGV0)' > $@; \
> +    $(evaluate-test)

Here I do not think the ARGV0 variable adds much clarity.

> +#include <string.h>
> +#include <support/check.h>
> +
> +static int
> +do_test (int argc, char **argv)
> +{
> +  TEST_COMPARE_BLOB (argv[0], strlen (argv[0]),
> +                     "test-argv0", strlen ("test-argv0"));
> +  return 0;
> +}

You can use TEST_COMPARE_STRING, which will do the strlen for you.

The patch itself looks okay to me.

Thanks,
Florian
-- 
Red Hat GmbH, https://de.redhat.com/ , Registered seat: Grasbrunn,
Commercial register: Amtsgericht Muenchen, HRB 153243,
Managing Directors: Charles Cachera, Brian Klemm, Laurie Krebs, Michael O'Neill


^ permalink raw reply	[flat|nested] 22+ messages in thread

* [PATCH v6] ld.so: add an --argv0 option
  2020-09-24  9:53                   ` Florian Weimer via Libc-alpha
@ 2020-09-24 12:43                     ` vincent via Libc-alpha
  2020-09-29  8:21                     ` vincent via Libc-alpha
  1 sibling, 0 replies; 22+ messages in thread
From: vincent via Libc-alpha @ 2020-09-24 12:43 UTC (permalink / raw)
  To: libc-alpha

From: Vincent Mihalkovic <vmihalko@redhat.com>

> I think adding a NEWS entry would make sense.
NEWS entry added.

> Here I do not think the ARGV0 variable adds much clarity.
I removed the ARGV0 variable.

> You can use TEST_COMPARE_STRING, which will do the strlen for you.
There was TEST_COMPARE_BLOB because Adhemerval Zanella suggest it 
in https://sourceware.org/pipermail/libc-alpha/2020-August/117278.html.
I've changed it to TEST_COMPARE_STRING.

Thank you for your advice,
vincent mihalkovic
---
 NEWS                  |  3 +++
 elf/Makefile          | 12 ++++++++++--
 elf/argv0test.c       | 31 +++++++++++++++++++++++++++++++
 elf/rtld.c            | 17 ++++++++++++++++-
 elf/tst-rtld-argv0.sh | 37 +++++++++++++++++++++++++++++++++++++
 5 files changed, 97 insertions(+), 3 deletions(-)
 create mode 100644 elf/argv0test.c
 create mode 100755 elf/tst-rtld-argv0.sh

diff --git a/NEWS b/NEWS
index fc8dd154..099fc3c8 100644
--- a/NEWS
+++ b/NEWS
@@ -9,6 +9,9 @@ Version 2.33
 
 Major new features:
 
+* The dynamic linker accepts the --argv0 argument and provide 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.
diff --git a/elf/Makefile b/elf/Makefile
index 0b787218..c587e9f0 100644
--- a/elf/Makefile
+++ b/elf/Makefile
@@ -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
index 00000000..c22ba5ea
--- /dev/null
+++ b/elf/argv0test.c
@@ -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>
diff --git a/elf/rtld.c b/elf/rtld.c
index 5b882163..9918fda0 100644
--- a/elf/rtld.c
+++ b/elf/rtld.c
@@ -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
index 00000000..14d97fb3
--- /dev/null
+++ b/elf/tst-rtld-argv0.sh
@@ -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
-- 
2.25.4


^ permalink raw reply related	[flat|nested] 22+ messages in thread

* [PATCH v6] ld.so: add an --argv0 option
  2020-09-24  9:53                   ` Florian Weimer via Libc-alpha
  2020-09-24 12:43                     ` [PATCH v6] " vincent via Libc-alpha
@ 2020-09-29  8:21                     ` vincent via Libc-alpha
  2020-09-29 10:37                       ` Florian Weimer via Libc-alpha
  1 sibling, 1 reply; 22+ messages in thread
From: vincent via Libc-alpha @ 2020-09-29  8:21 UTC (permalink / raw)
  To: libc-alpha

From: Vincent Mihalkovic <vmihalko@redhat.com>

Hi,

I'm resending this patch due to small grammar corrections in NEWS.

vincent mihalkovic

---
 NEWS                  |  3 +++
 elf/Makefile          | 12 ++++++++++--
 elf/argv0test.c       | 31 +++++++++++++++++++++++++++++++
 elf/rtld.c            | 17 ++++++++++++++++-
 elf/tst-rtld-argv0.sh | 37 +++++++++++++++++++++++++++++++++++++
 5 files changed, 97 insertions(+), 3 deletions(-)
 create mode 100644 elf/argv0test.c
 create mode 100755 elf/tst-rtld-argv0.sh

diff --git a/NEWS b/NEWS
index fc8dd154..099fc3c8 100644
--- 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.
diff --git a/elf/Makefile b/elf/Makefile
index 0b787218..c587e9f0 100644
--- a/elf/Makefile
+++ b/elf/Makefile
@@ -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
index 00000000..c22ba5ea
--- /dev/null
+++ b/elf/argv0test.c
@@ -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>
diff --git a/elf/rtld.c b/elf/rtld.c
index 5b882163..9918fda0 100644
--- a/elf/rtld.c
+++ b/elf/rtld.c
@@ -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
index 00000000..14d97fb3
--- /dev/null
+++ b/elf/tst-rtld-argv0.sh
@@ -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
-- 
2.25.4


^ permalink raw reply related	[flat|nested] 22+ messages in thread

* Re: [PATCH v6] ld.so: add an --argv0 option
  2020-09-29  8:21                     ` vincent via Libc-alpha
@ 2020-09-29 10:37                       ` Florian Weimer via Libc-alpha
  0 siblings, 0 replies; 22+ messages in thread
From: Florian Weimer via Libc-alpha @ 2020-09-29 10:37 UTC (permalink / raw)
  To: vincent via Libc-alpha; +Cc: vmihalko

* vincent via Libc-alpha:

> I'm resending this patch due to small grammar corrections in NEWS.

I have pushed this for you, after adding the reference to bug 16124.
(The NEWS entry will likely see further updates due to other ld.so
changes.)

Thank you for your contribution to glibc.

Florian
-- 
Red Hat GmbH, https://de.redhat.com/ , Registered seat: Grasbrunn,
Commercial register: Amtsgericht Muenchen, HRB 153243,
Managing Directors: Charles Cachera, Brian Klemm, Laurie Krebs, Michael O'Neill


^ permalink raw reply	[flat|nested] 22+ messages in thread

end of thread, other threads:[~2020-09-29 10:37 UTC | newest]

Thread overview: 22+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-08-12 11:06 [PATCH] ld.so: add an --argv0 option Vincent Mihalkovic via Libc-alpha
2020-08-25 15:10 ` Adhemerval Zanella via Libc-alpha
2020-08-26 11:53   ` [PATCH v2] " vmihalko--- via Libc-alpha
2020-08-26 13:55     ` Adhemerval Zanella via Libc-alpha
2020-08-31 13:22       ` [PATCH v3] " vincent via Libc-alpha
2020-09-01 15:09         ` Adhemerval Zanella via Libc-alpha
2020-09-14 14:17           ` [PATCH v4] " vincent via Libc-alpha
2020-09-14 15:03             ` Andreas Schwab
2020-09-14 21:32               ` [PATCH v5] " vincent via Libc-alpha
2020-09-23  6:06                 ` vincent via Libc-alpha
2020-09-24  9:53                   ` Florian Weimer via Libc-alpha
2020-09-24 12:43                     ` [PATCH v6] " vincent via Libc-alpha
2020-09-29  8:21                     ` vincent via Libc-alpha
2020-09-29 10:37                       ` Florian Weimer via Libc-alpha
  -- strict thread matches above, loose matches on Subject: below --
2020-07-22  8:33 [PATCH] " Vincent Mihalkovic via Libc-alpha
2020-07-22  9:00 ` Florian Weimer via Libc-alpha
2020-07-26 20:00   ` Vincent Mihalkovic via Libc-alpha
2020-07-27  5:54     ` Florian Weimer via Libc-alpha
2016-04-23  5:28 Mike Frysinger
2016-04-23 12:21 ` Florian Weimer
2016-04-23 20:06   ` Mike Frysinger
2016-04-24 12:21     ` Florian Weimer

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).