mailing list of musl libc
 help / color / mirror / code / Atom feed
* [PATCH] _start_c does more work than is necessary
@ 2018-08-26  0:56 Jon Chesterfield
  2018-08-26  1:23 ` Rich Felker
  0 siblings, 1 reply; 4+ messages in thread
From: Jon Chesterfield @ 2018-08-26  0:56 UTC (permalink / raw)
  To: musl

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

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);
 }

[-- Attachment #2: Type: text/html, Size: 1551 bytes --]

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

* Re: [PATCH] _start_c does more work than is necessary
  2018-08-26  0:56 [PATCH] _start_c does more work than is necessary Jon Chesterfield
@ 2018-08-26  1:23 ` Rich Felker
  2018-08-26  1:47   ` Jon Chesterfield
  2018-08-30 18:19   ` A. Wilcox
  0 siblings, 2 replies; 4+ messages in thread
From: Rich Felker @ 2018-08-26  1:23 UTC (permalink / raw)
  To: musl

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


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

* Re: [PATCH] _start_c does more work than is necessary
  2018-08-26  1:23 ` Rich Felker
@ 2018-08-26  1:47   ` Jon Chesterfield
  2018-08-30 18:19   ` A. Wilcox
  1 sibling, 0 replies; 4+ messages in thread
From: Jon Chesterfield @ 2018-08-26  1:47 UTC (permalink / raw)
  To: musl

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

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

[-- Attachment #2: Type: text/html, Size: 3456 bytes --]

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

* Re: [PATCH] _start_c does more work than is necessary
  2018-08-26  1:23 ` Rich Felker
  2018-08-26  1:47   ` Jon Chesterfield
@ 2018-08-30 18:19   ` A. Wilcox
  1 sibling, 0 replies; 4+ messages in thread
From: A. Wilcox @ 2018-08-30 18:19 UTC (permalink / raw)
  To: musl


[-- Attachment #1.1: Type: text/plain, Size: 756 bytes --]

On 08/25/18 20:23, Rich Felker wrote:
> 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?).


That wacky language runtime is still needed to bootstrap Java compilers
without relying on binaries.  This is especially important for musl
(because except for Oracle's semi-proprietary portola, there is no
binary for musl at all) and for other architectures (OpenJDK is, as far
as I'm aware, not a cross-compiler, so arches like mips or ppc64 would
be screwed).

--arw


-- 
A. Wilcox (awilfox)
Project Lead, Adélie Linux
http://adelielinux.org


[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

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

end of thread, other threads:[~2018-08-30 18:19 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-08-26  0:56 [PATCH] _start_c does more work than is necessary Jon Chesterfield
2018-08-26  1:23 ` Rich Felker
2018-08-26  1:47   ` Jon Chesterfield
2018-08-30 18:19   ` A. Wilcox

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/musl/

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 NNTP newsgroup(s).