From mboxrd@z Thu Jan 1 00:00:00 1970 X-Msuck: nntp://news.gmane.org/gmane.linux.lib.musl.general/13145 Path: news.gmane.org!.POSTED!not-for-mail From: Jon Chesterfield Newsgroups: gmane.linux.lib.musl.general Subject: Re: [PATCH] _start_c does more work than is necessary Date: Sun, 26 Aug 2018 02:47:52 +0100 Message-ID: References: <20180826012345.GP1878@brightrain.aerifal.cx> Reply-To: musl@lists.openwall.com NNTP-Posting-Host: blaine.gmane.org Mime-Version: 1.0 Content-Type: multipart/alternative; boundary="000000000000c1846d05744ccc24" X-Trace: blaine.gmane.org 1535247977 6281 195.159.176.226 (26 Aug 2018 01:46:17 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Sun, 26 Aug 2018 01:46:17 +0000 (UTC) To: musl@lists.openwall.com Original-X-From: musl-return-13161-gllmg-musl=m.gmane.org@lists.openwall.com Sun Aug 26 03:46:13 2018 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 1ftk82-0001Vm-Rf for gllmg-musl@m.gmane.org; Sun, 26 Aug 2018 03:46:11 +0200 Original-Received: (qmail 1363 invoked by uid 550); 26 Aug 2018 01:48:17 -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 1342 invoked from network); 26 Aug 2018 01:48:17 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to; bh=O9R+la0q2cY76dUt8imQoDlV5z/pMDZWMlP2pZ61Slc=; b=gdgB02tWFwbNs3rzwTuC0F6kuauM/yx1E4i18CHFGfZevx9hl16gZPhCECd1KxgP4t EsaKKApBbGkefH+95jVYba3YWntDhZGOZn2+vNqMPxN9NQOj95ZivIA8S8at+7PvMVc3 hyNWcH/shOXU0xufVpF4ntdjfUApRbujeT9JrrOjToSz18yzjwBn7qo9uRll+J69oyQL RXkXuM0oBXRQwdm3dGq9BIId0SblY7qJc41X3urXVB/+1Kw3OsSToc9GekHjOYKLrJ9l OKVUzy9PX2qCYc+H8KqUnH0bWg2mVnoyLddnFBg+v+hAUBOA+xdjG2GzVLcdI5x68lhj YKcg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to; bh=O9R+la0q2cY76dUt8imQoDlV5z/pMDZWMlP2pZ61Slc=; b=WelG7s9H8E3TyZZTe3RwFPlFxu8NzGpVnEpkahPwVVkDrvSbB2aacoCiTUEDq4PLOp 1dPGT5XVRFe2FpF8Pdn528VtXka3eD9IVmeDXYOUuh296zlWgUjOc7WaFcf5uKwam1f1 Xd2OE3OzJRlTIdXsGLwXnM/2lB5v/xEaAsFBSHFOZohBP14SjhGxkpj1ydPYz44+uyLN mhJ79UF0x3zfzoPZ+i+Hl6GVzLVuk1tjvB5jY6CPuC6drJCa27LdvZFqvMarf9SdBDQl SvzD9h3Im0KLCEwnYp2aC3aNOAFUAcLJpbiQkwloispKV2CSIKKBKX5wiSjRHNc+56Ha 1PSQ== X-Gm-Message-State: APzg51ABZpT0+w4U7epDPMTeIMFlW/LGgjodK3zqbnICSuPwhd2lBEoH 6jrZWCHdiiHjYmAVnQlqhamyCJ6Lm0GzE6SgANTBgYd3 X-Google-Smtp-Source: ANB0Vdbcc2wYL1WR1r1WN1HrqMabUS8R3pvN8bGFrY0cugWM1RH75E2qi9pxj5Lnc0R4yDnfu/wbhv4YD2P4OqKZhw8= X-Received: by 2002:aed:30a1:: with SMTP id 30-v6mr8271384qtf.29.1535248085122; Sat, 25 Aug 2018 18:48:05 -0700 (PDT) In-Reply-To: <20180826012345.GP1878@brightrain.aerifal.cx> Xref: news.gmane.org gmane.linux.lib.musl.general:13145 Archived-At: --000000000000c1846d05744ccc24 Content-Type: text/plain; charset="UTF-8" That's interesting context, thanks. rcrt1.c is ofc similar. I'd be happy to see the crt files go. The patch was motivated by debugging the consequences of importing crtbegin from bsd (as broadly recommended online) while trying to placate lld. Said crtbegin installed a function that walks init_array into .init so all the constructors fired twice. crt1.o appears to be the only necessary crt file for llvm/x86-64/c++. .ctor/.dtor is still implemented in lld along with a comment about legacy :) Cheers Jon On Sun, Aug 26, 2018 at 2:24 AM Rich Felker wrote: > On Sun, Aug 26, 2018 at 01:56:30AM +0100, Jon Chesterfield wrote: > > The init sequence in musl is _start calls _start_c which calls > > __libc_start_main. > > > > _start_c passes pointers to the _init and _fini functions, and also a > > trailing zero, to __libc_start_main. > > > > __libc_start_main currently takes exactly three arguments. I'd like to > > simplify crt1.c by only passing main, argc, argv. > > > > This is worth a few lines of C and three instructions in the startup > > sequence. E.g. x86-64 this removes mov, mov, xor for fourteen bytes. > > > > It also removes uses of _init() and _free() which I'm considering > deleting > > from a musl/llvm toolchain which makes no use of either, slightly > > decreasing the size of my out of tree patch. > > > > Thanks, > > > > Jon > > > > --- > > diff --git a/crt/crt1.c b/crt/crt1.c > > index af02af9..f27c949 100644 > > --- a/crt/crt1.c > > +++ b/crt/crt1.c > > @@ -5,14 +5,11 @@ > > #include "crt_arch.h" > > > > int main(); > > -void _init() __attribute__((weak)); > > -void _fini() __attribute__((weak)); > > -_Noreturn int __libc_start_main(int (*)(), int, char **, > > - void (*)(), void(*)(), void(*)()); > > +_Noreturn int __libc_start_main(int (*)(), int, char **); > > > > void _start_c(long *p) > > { > > int argc = p[0]; > > char **argv = (void *)(p+1); > > - __libc_start_main(main, argc, argv, _init, _fini, 0); > > + __libc_start_main(main, argc, argv); > > } > > It may be reasonable to make this change now, but it should be > reviewed. Read the git log for __libc_start_main.c and in particular > commit 7586360badcae6e73f04eb1b8189ce630281c4b2 which explains the > history and non-obvious reason musl does not have any reason to use > these arguments. > > Ultimately all crt files except crt1 should be removed at some point. > The _init/_fini machinery (crti/crtn) is purely legacy mess from > before init/fini arrays were used, and the gcc crtbegin/crtend mess is > just for wacky language runtimes like java (gcj, obsolete and removed > from gcc). I'm not sure if there are other compilers out there that > still don't know how to do the arrays, though (pcc? firm/cparser?). > > Rich > --000000000000c1846d05744ccc24 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
That's interesting context, thanks. rcrt1.c is of= c similar.

I'd be happy to see the crt files go. The patch was m= otivated by debugging the
consequences of importing crtbegin from bsd (a= s broadly recommended online)
while trying to placate lld. Said crtbegin= installed a function that walks
init_array into .init so all the constr= uctors fired twice.

crt1.o appears to be the only necessary crt file= for llvm/x86-64/c++.

.ctor/.dtor is still implemented in lld along = with a comment about legacy :)

Cheers

Jon

=
On Sun, Aug 26, 2018 at 2:2= 4 AM Rich Felker <dalias@libc.org= > wrote:
On Sun, Aug 26, 2018 at= 01:56:30AM +0100, Jon Chesterfield wrote:
> The init sequence in musl is _start calls _start_c which calls
> __libc_start_main.
>
> _start_c passes pointers to the _init and _fini functions, and also a<= br> > trailing zero, to __libc_start_main.
>
> __libc_start_main currently takes exactly three arguments. I'd lik= e to
> simplify crt1.c by only passing main, argc, argv.
>
> This is worth a few lines of C and three instructions in the startup > sequence. E.g. x86-64 this removes mov, mov, xor for fourteen bytes. >
> It also removes uses of _init() and _free() which I'm considering = deleting
> from a musl/llvm toolchain which makes no use of either, slightly
> decreasing the size of my out of tree patch.
>
> Thanks,
>
> Jon
>
> ---
> diff --git a/crt/crt1.c b/crt/crt1.c
> index af02af9..f27c949 100644
> --- a/crt/crt1.c
> +++ b/crt/crt1.c
> @@ -5,14 +5,11 @@
>=C2=A0 #include "crt_arch.h"
>
>=C2=A0 int main();
> -void _init() __attribute__((weak));
> -void _fini() __attribute__((weak));
> -_Noreturn int __libc_start_main(int (*)(), int, char **,
> -=C2=A0 =C2=A0 =C2=A0 =C2=A0void (*)(), void(*)(), void(*)());
> +_Noreturn int __libc_start_main(int (*)(), int, char **);
>
>=C2=A0 void _start_c(long *p)
>=C2=A0 {
>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0int argc =3D p[0];
>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0char **argv =3D (void *)(p+1);
> -=C2=A0 =C2=A0 =C2=A0 =C2=A0__libc_start_main(main, argc, argv, _init,= _fini, 0);
> +=C2=A0 =C2=A0 =C2=A0 =C2=A0__libc_start_main(main, argc, argv);
>=C2=A0 }

It may be reasonable to make this change now, but it should be
reviewed. Read the git log for __libc_start_main.c and in particular
commit 7586360badcae6e73f04eb1b8189ce630281c4b2 which explains the
history and non-obvious reason musl does not have any reason to use
these arguments.

Ultimately all crt files except crt1 should be removed at some point.
The _init/_fini machinery (crti/crtn) is purely legacy mess from
before init/fini arrays were used, and the gcc crtbegin/crtend mess is
just for wacky language runtimes like java (gcj, obsolete and removed
from gcc). I'm not sure if there are other compilers out there that
still don't know how to do the arrays, though (pcc? firm/cparser?).

Rich
--000000000000c1846d05744ccc24--