mailing list of musl libc
 help / color / mirror / code / Atom feed
From: Rich Felker <dalias@aerifal.cx>
To: musl@lists.openwall.com
Subject: New "portable" crt1.c
Date: Thu, 18 Jul 2013 22:17:26 -0400	[thread overview]
Message-ID: <20130719021726.GA19068@brightrain.aerifal.cx> (raw)

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

Hi all,

I've been experimenting with an idea for eliminating most of the asm
from the "crt" start files. The basic concept is to have a tiny
file-scope __asm__ statement, pulled in from arch/$(ARCH)/crt1.h or
similar, which does nothing but calling a portable C version of
_start, contained in crt1.c, with the right arguments.

The benefits:
- We get regular (crt1.o) and PIE (Scrt1.o) versions for free.
- Porting to new archs is simpler.
- Applying the same technique to crti.o/crtn.o, we can support the
  new-style init_array/fini_array on all archs without lots of new
  per-arch asm.

Attached is a demo for i386 showing how well this approach works (size
increase is less than 16 bytes).

I'm open to comments, but my feeling is that we should keep the
existing crt asm for archs where it already exists, but replace the
empty crt1.c with a version based on this approach, and encourage
using the new approach on all new ports. In the future, if maintaining
the current asm becomes a burden (e.g. if new requirements are
introduced) we could remove the existing asm and switch to the C for
all ports.

Rich

[-- Attachment #2: newcrt.c --]
[-- Type: text/plain, Size: 621 bytes --]

__asm__("\
.global _start\n\
_start:\n\
	xor %ebp,%ebp\n\
	mov %esp,%eax\n\
	and $-16,%esp\n\
	push %eax\n\
	push %eax\n\
	push %edx\n\
	push %eax\n\
	call ___start\n\
");

__attribute__((__noreturn__)) int __libc_start_main(
	int (*)(int, char **, char **), int, char **,
	int (*)(int, char **, char **), void (*)(void),
	void (*)(void));

int main();
int _init() __attribute__((__weak__,__visibility__("hidden")));
void _fini() __attribute__((__weak__,__visibility__("hidden")));

void ___start(long *p, void *q)
{
	int argc = p[0];
	char **argv = (void *)p[1];
	__libc_start_main(main, argc, argv, _init, _fini, q);
}

             reply	other threads:[~2013-07-19  2:17 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-07-19  2:17 Rich Felker [this message]
2013-07-25  7:38 ` Rob Landley
2013-07-25  7:41   ` Rich Felker

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20130719021726.GA19068@brightrain.aerifal.cx \
    --to=dalias@aerifal.cx \
    --cc=musl@lists.openwall.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).