From mboxrd@z Thu Jan 1 00:00:00 1970 X-Msuck: nntp://news.gmane.org/gmane.linux.lib.musl.general/10036 Path: news.gmane.org!not-for-mail From: Szabolcs Nagy Newsgroups: gmane.linux.lib.musl.general Subject: Re: [PATCH 2/2] add preinit_array support Date: Tue, 17 May 2016 23:52:24 +0200 Message-ID: <20160517215223.GG22574@port70.net> References: <20151129153322.GO23362@port70.net> <20151129165925.GQ23362@port70.net> <20151129234309.GS3818@brightrain.aerifal.cx> Reply-To: musl@lists.openwall.com NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="xJK8B5Wah2CMJs8h" X-Trace: ger.gmane.org 1463521968 3187 80.91.229.3 (17 May 2016 21:52:48 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Tue, 17 May 2016 21:52:48 +0000 (UTC) To: musl@lists.openwall.com Original-X-From: musl-return-10049-gllmg-musl=m.gmane.org@lists.openwall.com Tue May 17 23:52:41 2016 Return-path: Envelope-to: gllmg-musl@m.gmane.org Original-Received: from mother.openwall.net ([195.42.179.200]) by plane.gmane.org with smtp (Exim 4.69) (envelope-from ) id 1b2muu-0005ba-JD for gllmg-musl@m.gmane.org; Tue, 17 May 2016 23:52:40 +0200 Original-Received: (qmail 29863 invoked by uid 550); 17 May 2016 21:52:36 -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 29843 invoked from network); 17 May 2016 21:52:35 -0000 Mail-Followup-To: musl@lists.openwall.com Content-Disposition: inline In-Reply-To: <20151129234309.GS3818@brightrain.aerifal.cx> User-Agent: Mutt/1.5.24 (2015-08-30) Xref: news.gmane.org gmane.linux.lib.musl.general:10036 Archived-At: --xJK8B5Wah2CMJs8h Content-Type: text/plain; charset=us-ascii Content-Disposition: inline * Rich Felker [2015-11-29 18:43:09 -0500]: > On Sun, Nov 29, 2015 at 05:59:25PM +0100, Szabolcs Nagy wrote: > > * Szabolcs Nagy [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. --xJK8B5Wah2CMJs8h Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename="0001-add-preinit_array-support.patch" >From 18cfe83a9f3afacb03f3cad8e8b9d78121179451 Mon Sep 17 00:00:00 2001 From: Szabolcs Nagy 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 --xJK8B5Wah2CMJs8h--