mailing list of musl libc
 help / color / mirror / code / Atom feed
From: Szabolcs Nagy <nsz@port70.net>
To: musl@lists.openwall.com
Subject: Re: [PATCH 2/2] add preinit_array support
Date: Tue, 17 May 2016 23:52:24 +0200	[thread overview]
Message-ID: <20160517215223.GG22574@port70.net> (raw)
In-Reply-To: <20151129234309.GS3818@brightrain.aerifal.cx>

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

* Rich Felker <dalias@libc.org> [2015-11-29 18:43:09 -0500]:
> On Sun, Nov 29, 2015 at 05:59:25PM +0100, Szabolcs Nagy wrote:
> > * Szabolcs Nagy <nsz@port70.net> [2015-11-29 16:33:22 +0100]:
> > handle DT_PREINIT_ARRAY{SZ} in case of dynamic linking and
> > __preinit_array_{start,end} in case of static linking.
> 
> Omission of preinit array support was intentional since it's reserved
> for use by the [library] implementation and musl does not use it, and
> I've tried to keep mandatory-linked startup code as small as possible
> to keep the small-static-binaries people happy.
> 
> However on IRC we discussed that ASan and VTV and perhaps other
> similar tools might use it, and such use makes sense, so we may need
> to add it. I just don't want to get in a habit of adding whatever
> random junk is in the ELF spec when there's no reasonable,
> well-defined way for application code to use or depend on it.
> 
> > redefined DYN_CNT because 32 is not enough for DT_PREINIT_ARRAY.
> 
> This should be reviewed for possible problems; IIRC I reduced it to 32
> to avoid having to worry about undefined bitshifts in some code that
> can't do 64-bit shifts (because libgcc functions might not yet be
> callable) but that code should really hard-code 32 rather than using
> DYN_CNT if it's still a problem.
> 

attached another variant in case we will need it
(handles DT_PREINIT* separately).

i didn't handle the issue described in
http://git.musl-libc.org/cgit/musl/commit/?id=19caa25d0a8e587bb89b79c3f629085548709dd4
i don't know if that may apply to the preinit* symbols too.

[-- Attachment #2: 0001-add-preinit_array-support.patch --]
[-- Type: text/x-diff, Size: 1975 bytes --]

From 18cfe83a9f3afacb03f3cad8e8b9d78121179451 Mon Sep 17 00:00:00 2001
From: Szabolcs Nagy <nsz@port70.net>
Date: Sun, 15 May 2016 20:11:42 +0000
Subject: [PATCH] add preinit_array support

---
 ldso/dynlink.c              | 13 +++++++++++++
 src/env/__libc_start_main.c |  8 +++++++-
 2 files changed, 20 insertions(+), 1 deletion(-)

diff --git a/ldso/dynlink.c b/ldso/dynlink.c
index e458f38..38709ec 100644
--- a/ldso/dynlink.c
+++ b/ldso/dynlink.c
@@ -1207,6 +1207,18 @@ void __libc_exit_fini()
 	}
 }
 
+static void do_preinit(void)
+{
+	size_t *v, *fn, n=0;
+
+	for (v = head->dynv; v[0]; v+=2)
+		if (v[0] == DT_PREINIT_ARRAY)
+			fn = laddr(head, v[1]);
+		else if (v[0] == DT_PREINIT_ARRAYSZ)
+			n = v[1]/sizeof(size_t);
+	while (n--) ((void(*)(void))*fn++)();
+}
+
 static void do_init_fini(struct dso *p)
 {
 	size_t dyn[DYN_CNT];
@@ -1242,6 +1254,7 @@ static void do_init_fini(struct dso *p)
 
 void __libc_start_init(void)
 {
+	do_preinit();
 	do_init_fini(tail);
 }
 
diff --git a/src/env/__libc_start_main.c b/src/env/__libc_start_main.c
index 5c79be2..1397ec4 100644
--- a/src/env/__libc_start_main.c
+++ b/src/env/__libc_start_main.c
@@ -12,6 +12,9 @@ static void dummy(void) {}
 weak_alias(dummy, _init);
 
 __attribute__((__weak__, __visibility__("hidden")))
+extern void (*const __preinit_array_start)(void), (*const __preinit_array_end)(void);
+
+__attribute__((__weak__, __visibility__("hidden")))
 extern void (*const __init_array_start)(void), (*const __init_array_end)(void);
 
 static void dummy1(void *p) {}
@@ -55,8 +58,11 @@ void __init_libc(char **envp, char *pn)
 
 static void libc_start_init(void)
 {
+	uintptr_t a = (uintptr_t)&__preinit_array_start;
+	for (; a<(uintptr_t)&__preinit_array_end; a+=sizeof(void(*)()))
+		(*(void (**)())a)();
 	_init();
-	uintptr_t a = (uintptr_t)&__init_array_start;
+	a = (uintptr_t)&__init_array_start;
 	for (; a<(uintptr_t)&__init_array_end; a+=sizeof(void(*)()))
 		(*(void (**)())a)();
 }
-- 
2.8.1


      reply	other threads:[~2016-05-17 21:52 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-11-29 15:33 Szabolcs Nagy
2015-11-29 16:59 ` Szabolcs Nagy
2015-11-29 23:43   ` Rich Felker
2016-05-17 21:52     ` Szabolcs Nagy [this message]

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=20160517215223.GG22574@port70.net \
    --to=nsz@port70.net \
    --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).