From mboxrd@z Thu Jan 1 00:00:00 1970 X-Msuck: nntp://news.gmane.org/gmane.linux.lib.musl.general/8937 Path: news.gmane.org!not-for-mail From: Szabolcs Nagy Newsgroups: gmane.linux.lib.musl.general Subject: Re: [PATCH 1/2] init_array/fini_array cleanup Date: Sun, 29 Nov 2015 17:47:24 +0100 Message-ID: <20151129164724.GP23362@port70.net> References: <20151129153051.GN23362@port70.net> Reply-To: musl@lists.openwall.com NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="ZmUaFz6apKcXQszQ" X-Trace: ger.gmane.org 1448815670 6906 80.91.229.3 (29 Nov 2015 16:47:50 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Sun, 29 Nov 2015 16:47:50 +0000 (UTC) To: musl@lists.openwall.com Original-X-From: musl-return-8950-gllmg-musl=m.gmane.org@lists.openwall.com Sun Nov 29 17:47:36 2015 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 1a358Q-000280-AM for gllmg-musl@m.gmane.org; Sun, 29 Nov 2015 17:47:34 +0100 Original-Received: (qmail 14256 invoked by uid 550); 29 Nov 2015 16:47:36 -0000 Mailing-List: contact musl-help@lists.openwall.com; run by ezmlm Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: Original-Received: (qmail 14234 invoked from network); 29 Nov 2015 16:47:36 -0000 Mail-Followup-To: musl@lists.openwall.com Content-Disposition: inline In-Reply-To: <20151129153051.GN23362@port70.net> User-Agent: Mutt/1.5.24 (2015-08-30) Xref: news.gmane.org gmane.linux.lib.musl.general:8937 Archived-At: --ZmUaFz6apKcXQszQ Content-Type: text/plain; charset=us-ascii Content-Disposition: inline * Szabolcs Nagy [2015-11-29 16:30:51 +0100]: > -extern void (*const __init_array_start)(void), (*const __init_array_end)(void); > +extern void (*const __init_array_start[])(void), (*const __init_array_end[])(void); Alexander Monakov pointed out that the compiler may consider arrays to be separate objects and then f<__init_array_end would be ub. attached an updated version, the same code is generated on x86_64 as before, except there is no xor %eax,%eax now before the call (because there is prototype). --ZmUaFz6apKcXQszQ Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename="0001-cleaner-init-fini-array-handling.patch" >From 3c29dc0d9bdb2e4d5f735d7b3d272d92cb82d2c6 Mon Sep 17 00:00:00 2001 From: Szabolcs Nagy Date: Sun, 29 Nov 2015 16:24:17 +0000 Subject: [PATCH 1/2] cleaner init/fini array handling Avoid casts and make sure there is a prototype when init/fini functions are called. --- src/env/__libc_start_main.c | 6 +++--- src/exit/exit.c | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/env/__libc_start_main.c b/src/env/__libc_start_main.c index 5c79be2..14a2825 100644 --- a/src/env/__libc_start_main.c +++ b/src/env/__libc_start_main.c @@ -55,10 +55,10 @@ void __init_libc(char **envp, char *pn) static void libc_start_init(void) { + void (*const *f)(void); _init(); - uintptr_t a = (uintptr_t)&__init_array_start; - for (; a<(uintptr_t)&__init_array_end; a+=sizeof(void(*)())) - (*(void (**)())a)(); + for (f=&__init_array_start; f<&__init_array_end; f++) + (*f)(); } weak_alias(libc_start_init, __libc_start_init); diff --git a/src/exit/exit.c b/src/exit/exit.c index bf7835a..b0603b4 100644 --- a/src/exit/exit.c +++ b/src/exit/exit.c @@ -17,9 +17,9 @@ extern void (*const __fini_array_start)(void), (*const __fini_array_end)(void); static void libc_exit_fini(void) { - uintptr_t a = (uintptr_t)&__fini_array_end; - for (; a>(uintptr_t)&__fini_array_start; a-=sizeof(void(*)())) - (*(void (**)())(a-sizeof(void(*)())))(); + void (*const *f)(void) = &__fini_array_end; + while (f > &__fini_array_start) + (*--f)(); _fini(); } -- 2.4.1 --ZmUaFz6apKcXQszQ--