Skip site navigation (1)Skip section navigation (2)
Date:      Fri, 6 Dec 2013 05:21:11 +0200
From:      Konstantin Belousov <kostikbel@gmail.com>
To:        Michael Gmelin <freebsd@grem.de>
Cc:        David Chisnall <theraven@freebsd.org>, Brooks Davis <brooks@FreeBSD.org>, Dimitry Andric <dim@FreeBSD.org>, Matthias Andree <mandree@FreeBSD.org>, "freebsd-ports@freebsd.org Ports" <freebsd-ports@freebsd.org>
Subject:   Re: Position independent code and global destructor order (devel/ice)
Message-ID:  <20131206032111.GH59496@kib.kiev.ua>
In-Reply-To: <20131206032552.59672a5b@bsd64.grem.de>
References:  <20130626231741.497f7a9b@bsd64.grem.de> <20130626212833.GB91021@kib.kiev.ua> <20130627015602.7a437aad@bsd64.grem.de> <51CC411C.4060105@FreeBSD.org> <20130627140428.GI91021@kib.kiev.ua> <20130827144507.385a552c@bsd64.grem.de> <20131130223427.3d95ae7c@bsd64.grem.de> <20131205170728.GF59496@kib.kiev.ua> <20131205182058.5479b1ff@bsd64.grem.de> <20131206032552.59672a5b@bsd64.grem.de>

next in thread | previous in thread | raw e-mail | index | archive | help

--5MTJTT7GctpGkbGB
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable

On Fri, Dec 06, 2013 at 03:25:52AM +0100, Michael Gmelin wrote:
> I tested your patch on 10-BETA2 and 10-BETA3. I built our complete
> production environment from scratch, ran a huge number of unit and
> integration tests. Everything works as expected, including Ice's
> "Schwarz Counter" implementation
> (http://en.wikibooks.org/wiki/More_C%2B%2B_Idioms/Nifty_Counter).
>=20
> So as far as I can tell this fixes the problem, while still
> accomplishing the goals of r211706.

Great, thank you.

I think that the patch can be further simplified.  From what I understand
about __cxa_finalize(3) protocol, confirmed by LSB, __cxa_finalize(NULL)
must only be called at the process exit, once.  Than, libc can use the
call as an indication of the final call to finalize, avoiding need for
help from rtld.

Please test the patch below.  It is enough to rebuild libc only.

diff --git a/lib/libc/stdlib/atexit.c b/lib/libc/stdlib/atexit.c
index 18c31c3..01d09fe 100644
--- a/lib/libc/stdlib/atexit.c
+++ b/lib/libc/stdlib/atexit.c
@@ -151,6 +151,8 @@ __cxa_atexit(void (*func)(void *), void *arg, void *dso)
 #pragma weak __pthread_cxa_finalize
 void __pthread_cxa_finalize(const struct dl_phdr_info *);
=20
+static int global_exit;
+
 /*
  * Call all handlers registered with __cxa_atexit for the shared
  * object owning 'dso'.  Note: if 'dso' is NULL, then all remaining
@@ -164,10 +166,12 @@ __cxa_finalize(void *dso)
 	struct atexit_fn fn;
 	int n, has_phdr;
=20
-	if (dso !=3D NULL)
+	if (dso !=3D NULL) {
 		has_phdr =3D _rtld_addr_phdr(dso, &phdr_info);
-	else
+	} else {
 		has_phdr =3D 0;
+		global_exit =3D 1;
+	}
=20
 	_MUTEX_LOCK(&atexit_mutex);
 	for (p =3D __atexit; p; p =3D p->next) {
@@ -177,8 +181,9 @@ __cxa_finalize(void *dso)
 			fn =3D p->fns[n];
 			if (dso !=3D NULL && dso !=3D fn.fn_dso) {
 				/* wrong DSO ? */
-				if (!has_phdr || !__elf_phdr_match_addr(
-				    &phdr_info, fn.fn_ptr.cxa_func))
+				if (!has_phdr || global_exit ||
+				    !__elf_phdr_match_addr(&phdr_info,
+				    fn.fn_ptr.cxa_func))
 					continue;
 			}
 			/*
@@ -200,6 +205,6 @@ __cxa_finalize(void *dso)
 	if (dso =3D=3D NULL)
 		_MUTEX_DESTROY(&atexit_mutex);
=20
-	if (has_phdr && &__pthread_cxa_finalize !=3D NULL)
+	if (has_phdr && !global_exit && &__pthread_cxa_finalize !=3D NULL)
 		__pthread_cxa_finalize(&phdr_info);
 }

--5MTJTT7GctpGkbGB
Content-Type: application/pgp-signature

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.22 (FreeBSD)

iQIcBAEBAgAGBQJSoUKmAAoJEJDCuSvBvK1BQgcP+wb7d/cD+Xkd3G9Qb+SjmkMH
i0XdieA7m1gCCr0pV56gAWk8oTx/t56ZXtPsKWqNEporRylSamcYoViftpGoMjOm
KnsGzenZqSSSjx2lCzwPZg155LWpfhf4KNHFne6HW9Y6Ro8fumGTRGJECQm98TXz
frstB6RO9BtmO5Nx0tKkl3omxe3diOleVWS3nULy87B4WBBrGy6d8CJT/oorOjus
YRcUbodNaHaTgfM/N8K+7cWcTwTJDIg7+Pnedvo658FkXLu3AD/9eC/7p0GghWHz
o4zL7BV3mbBd7TazVnymEG8WFUSZfsBLIwUufaAV04jUUO5r34Dd2nrluPNvmV4p
b0u7q5HYRo1mxVMWfrFU42OqxH3akXk/GvzMYTlknwgsStYNT/wC9f1xhV1v5Qpj
fY6/B4e1sVQ21qpcBftOT+RHCmFeHNuhukZLGZZS/rXjEPDmx4FN+622Hrq87EYT
ZfMlJi53NAwyKHlXH4iixFjNB2qx8y7SoNgYztnfxH1wT2I9+uIDF3bkEShJvKIo
0Og8c7oUOMMxmW/0AGs+ZSTtgjTNBJyzwaGksJ0lMib0Mohm/PrWcRX7rsiXiyhv
6AWmYYajCDaFjMRdQl2XWZUPdakOzJY9VNCambIRK5yQ7pi13EZ+KZu0jdkSu/JR
CQ5rDzSIru2x6+rdV99x
=KUP7
-----END PGP SIGNATURE-----

--5MTJTT7GctpGkbGB--



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20131206032111.GH59496>