From mboxrd@z Thu Jan 1 00:00:00 1970 X-Msuck: nntp://news.gmane.org/gmane.linux.lib.musl.general/11702 Path: news.gmane.org!.POSTED!not-for-mail From: u-uy74@aetey.se Newsgroups: gmane.linux.lib.musl.general Subject: Re: Question about setting argv[0] when manually using dynamic linker Date: Sun, 9 Jul 2017 11:23:23 +0200 Message-ID: <20170709092322.GA27260@example.net> References: <20170517070115.GL6320@example.net> <20170517162428.GH17319@brightrain.aerifal.cx> <20170702173618.GL27260@example.net> <20170704205811.GN1627@brightrain.aerifal.cx> Reply-To: musl@lists.openwall.com NNTP-Posting-Host: blaine.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: blaine.gmane.org 1499592244 20728 195.159.176.226 (9 Jul 2017 09:24:04 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Sun, 9 Jul 2017 09:24:04 +0000 (UTC) To: musl@lists.openwall.com Original-X-From: musl-return-11715-gllmg-musl=m.gmane.org@lists.openwall.com Sun Jul 09 11:23:59 2017 Return-path: Envelope-to: gllmg-musl@m.gmane.org Original-Received: from mother.openwall.net ([195.42.179.200]) by blaine.gmane.org with smtp (Exim 4.84_2) (envelope-from ) id 1dU8Ra-00052l-6Q for gllmg-musl@m.gmane.org; Sun, 09 Jul 2017 11:23:58 +0200 Original-Received: (qmail 21587 invoked by uid 550); 9 Jul 2017 09:23:56 -0000 Mailing-List: contact musl-help@lists.openwall.com; run by ezmlm Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-ID: Original-Received: (qmail 21566 invoked from network); 9 Jul 2017 09:23:55 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=fripost.org; h= in-reply-to:content-disposition:content-type:content-type :mime-version:references:message-id:subject:subject:from:from :date:date; s=20140703; t=1499592224; x=1501406625; bh=jSQl0ttqd crEMQoxod0AaUAr9xvQasYwo4Up6x9fky4=; b=kcSnIc3wktXlMJUqw0yY625Eq VUEwA6WeI8vWXi1GZRAGGeM9Aswh931yeF2Qu/fwf4oMQHL64DQF67h9rph9hN+Z BlhTrAGjmhhegGsgdfwtPiF/flW651XeDl40jELW5cg1cRdoIxKmEQ/5XBtSMNPF 5aLZj65MRvRIOhTCA8= X-Virus-Scanned: Debian amavisd-new at fripost.org Content-Disposition: inline In-Reply-To: <20170704205811.GN1627@brightrain.aerifal.cx> Xref: news.gmane.org gmane.linux.lib.musl.general:11702 Archived-At: On Tue, Jul 04, 2017 at 04:58:11PM -0400, Rich Felker wrote: > On Sun, Jul 02, 2017 at 07:36:19PM +0200, u-uy74@aetey.se wrote: > > On Wed, May 17, 2017 at 12:24:28PM -0400, Rich Felker wrote: > > > Perhaps adding an option like --argv0=foo would be > > > appropriate. > > > > Is this option being considered to introduce? > Yes, adding it. Thanks again, it makes some "unsolvable" cases work as needed. OTOH when applying this in practice, I noticed that a slightly different behaviour would be very handy: if the linker could supply its own argv[0] as the one for the program to run. This would fit nicely into the framework where we already set argv[0] anyway, in the same way for both statically and dynamically linked programs. (The only exception so far would be a hypothetical need to pass on the value "ldd" as argv[0], then --argv0=ldd would save the day) Otherwise we have to set the dynloader --argv0 argument per binary which is of course possible but remarkably less convenient, among others because this setting is dynloader-specific, while otherwise our tools are libc- and dynamic vs static agnostic. IOW as long as the loader itself does not rely on its argv[0] too much, plainly passing on argv[0] is a very practical means to handle the programs like busybox and gcc transparently. What about always passing on the loader argv[0] unless --argv0 is present? This will not matter for programs which do not analyze argv[0] and will not make it worse for programs which do. --- a/ldso/dynlink.c +++ b/ldso/dynlink.c @@ -1388,7 +1388,7 @@ kernel_mapped_dso(&app); } else { int fd; - char *ldname = argv[0]; + char *ldname = replace_argv0 = argv[0]; size_t l = strlen(ldname); if (l >= 3 && !strcmp(ldname+l-3, "ldd")) ldd_mode = 1; argv++; or even --- a/ldso/dynlink.c +++ b/ldso/dynlink.c @@ -1322,7 +1322,7 @@ size_t aux[AUX_CNT], *auxv; size_t i; char *env_preload=0; - char *replace_argv0=0; + char *replace_argv0=argv[0]; size_t vdso_base; int argc = *sp; char **argv = (void *)(sp+1); @@ -1567,7 +1567,7 @@ debug.state = 0; _dl_debug_state(); - if (replace_argv0) argv[0] = replace_argv0; + argv[0] = replace_argv0; errno = 0; Otherwise may be something like this: --- a/ldso/dynlink.c +++ b/ldso/dynlink.c @@ -1409,6 +1409,7 @@ else if (*argv) env_preload = *argv++; } else if (!memcmp(opt, "argv0", 5)) { if (opt[5]=='=') replace_argv0 = opt+6; + else if (opt[5]=='^') replace_argv0 = ldname; else if (opt[5]) *argv = 0; else if (*argv) replace_argv0 = *argv++; } else { to instead introduce an extra --argv0^ option ? Regards, Rune