Page 1 of 1
__cxa_atexit and __cxa_finalized
Posted: Tue Jul 12, 2011 6:40 pm
by tonytian
I am reading the C++ and C++ Bare Bones posts on wiki.osdev.org and found myself confused on the use of these two functions.
I understand that they are inserted by g++ and therefore have to be defined in order to write an OS using c++. According to the example in C++ Bare Bones post, however, we will call all functions in .ctors and .dtors sections. Then why do we need to implement these two functions when writing a kernel? In what situation a function has to be registered with __cxa_atexit and called in __ctx_finalized? Can someone please help me understand this? I really appreciate it.
Re: __cxa_atexit and __cxa_finalized
Posted: Tue Jul 12, 2011 10:46 pm
by torshie
From LSB specification:
__cxa_atexit — register a function to be called by exit or when a shared library is unloaded
__cxa_finalize — call destructors of global (or local static) C++ objects and exit functions registered with atexit
More detailed information can be found in LSB-core-generic
Re: __cxa_atexit and __cxa_finalized
Posted: Tue Jul 12, 2011 11:04 pm
by tonytian
Thanks for the reply.
Sure, LSB defines their behaviors. But what if a kernel does not use any shared library? Can I write two dummy functions to fool g++?
In fact, I have no idea why a kernel needs a shared library.
torshie wrote:From LSB specification:
__cxa_atexit — register a function to be called by exit or when a shared library is unloaded
__cxa_finalize — call destructors of global (or local static) C++ objects and exit functions registered with atexit
More detailed information can be found in LSB-core-generic
Re: __cxa_atexit and __cxa_finalized
Posted: Wed Jul 13, 2011 4:55 pm
by tonytian
Thanks.
berkus wrote:Yes, just write dummies for these.
Re: __cxa_atexit and __cxa_finalized
Posted: Thu Jul 14, 2011 9:24 am
by Yargh
In older versions of G++, constructors and destructors are stored in their specific sections (.ctors, .dtors). Now, they make use of __cxa_atexit and __cxa_finalize. If you're using an older version of GCC, it's safe to use the sections, but for the later ones you really should actually implement these functions. They're not that hard to implement.
They're also required if you want to use global objects.
Correct me if I'm wrong.
Re: __cxa_atexit and __cxa_finalized
Posted: Thu Jul 14, 2011 10:11 am
by blobmiester
Let me clear some things up.
The .ctors/.dtors sections are still relevant. They are slowy being superseded by .init_array/.fini_array however (on systems which support it) because .init_array sorts ascending and not descending.
In practice, the .dtors section is normally empty now (unless a function is otherwise made a destructor by use of function attributes in GCC) because inside every global and local static C++ .ctor a call to __cxa_atexit (or atexit on some non-compliant targets) is made to register it's destructor.
So an OS must still call constructors in the .ctors list (or .init_array depending on your system) and must implement __cxa_atexit and __cxa_finalize.
On a fun side note: the Itanium C++ ABI specifies how to control constructor priority (for some reason this tends to go unnoticed by the hordes of people asking which order my ctors go in).