This is the mail archive of the
libc-alpha@sourceware.org
mailing list for the glibc project.
Re: Problem with atexit and _dl_fini
- From: Manfred <mx2927 at gmail dot com>
- To: libc-help at sourceware dot org, libc-alpha at sourceware dot org
- Date: Fri, 14 Jun 2019 14:29:51 +0200
- Subject: Re: Problem with atexit and _dl_fini
- References: <b055481f-fea2-8a30-54de-6513c86e2e40@mulle-kybernetik.com> <87blzypg5j.fsf@mid.deneb.enyo.de> <0a7c2435-43f8-8dfb-83ab-22ceff7ca51c@mulle-kybernetik.com> <a2719398-e5c4-0d15-9a60-d6837bede555@mulle-kybernetik.com> <cd53d1e5-530c-b327-9e79-69b02c3b701e@linaro.org> <9497a5c2-0dc8-18fe-6120-deb551f7ddd8@mulle-kybernetik.com> <dc69ce90-3670-af69-beb7-72d4d5f93c25@linaro.org> <31060c89-404f-e8e1-6c18-d75e0b63f6ad@linaro.org> <174f19a0-40ea-a5b1-d21d-3903f9bba9f8@mulle-kybernetik.com> <a06959e2-ef9a-8732-8082-7524bdf585d2@mulle-kybernetik.com> <b2404926-4b04-70a4-e648-ba13d3486f3f@mulle-kybernetik.com>
Interesting,
In fact the link you posted is about the LSB, not specific to the
Itanium C++ ABI, and indeed it does the right thing.
As a side note, it leaves up to user code the following:
- do not register with atexit functions residing in dso's that can be
unloaded early (i.e. by dlclose() during execution, not at exit()).
- do not instantate global/static C++ objects whose type is defined in
the main program and derived from classes defined in dso's that can be
unloaded early.
Both requirements descend (implicitly?) from the C and C++ standards,
though.
I'm cross-posting to alpha, in case anyone is interested.
On 6/14/2019 12:53 AM, Nat! wrote:
Funnily enough, if you read the Itanium C++ ABI, on which __cxa_finalize
is based, then the algorithm described
there is doing exactly the right thing.
Beause the wording of __cxa_finalize is so shortened, it its hard to
pick out the original meaning. But the description is
actually fully compatible with how `atexit` is supposed to function.
The gist is this. For atexit, functions are stored in a unique way in
the termination function table (clarifications in []):
http://refspecs.linuxbase.org/LSB_5.0.0/LSB-Core-generic/LSB-Core-generic.html#BASELIB---CXA-FINALIZE
```
In the latter case [atexit] the pointer to the function is the pointer
passed to atexit(), while the other pointers [operand, handle] are NULL.
```
When dlclose hits, the handle to be closed is `d` and not NULL:
```
The implementation shall arrange for__cxa_finalize() to be called during
early shared library unload (e.g. dlclose()) with a handle to the shared
library.
```
And then
```
When __cxa_finalize(d) is called, it shall walk the termination function
list, calling each in turn if d matches the handle of the termination
function entry.
```
So `atexit`s don't match, since the handle stored is NULL. Only if `d`
is NULL (the base process terminates), then will the atexits be called.
Currently though at `dlclose` time all handlers are called, which breaks
the `atexit` specification as well as your own LSB.
Well it's a goof up, but FreeBSD and MacOS aren't doing any better.
Ciao
Nat!