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 1/2] init_array/fini_array cleanup
Date: Sun, 29 Nov 2015 17:47:24 +0100	[thread overview]
Message-ID: <20151129164724.GP23362@port70.net> (raw)
In-Reply-To: <20151129153051.GN23362@port70.net>

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

* Szabolcs Nagy <nsz@port70.net> [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).

[-- Attachment #2: 0001-cleaner-init-fini-array-handling.patch --]
[-- Type: text/x-diff, Size: 1515 bytes --]

From 3c29dc0d9bdb2e4d5f735d7b3d272d92cb82d2c6 Mon Sep 17 00:00:00 2001
From: Szabolcs Nagy <nsz@port70.net>
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


  reply	other threads:[~2015-11-29 16:47 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-11-29 15:30 Szabolcs Nagy
2015-11-29 16:47 ` Szabolcs Nagy [this message]
2015-11-29 23:25   ` 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=20151129164724.GP23362@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).