__cxa_atexit and __cxa_finalized

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
tonytian
Posts: 17
Joined: Tue Jul 12, 2011 2:53 pm

__cxa_atexit and __cxa_finalized

Post 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.
torshie
Member
Member
Posts: 89
Joined: Sun Jan 11, 2009 7:41 pm

Re: __cxa_atexit and __cxa_finalized

Post 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
tonytian
Posts: 17
Joined: Tue Jul 12, 2011 2:53 pm

Re: __cxa_atexit and __cxa_finalized

Post 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
tonytian
Posts: 17
Joined: Tue Jul 12, 2011 2:53 pm

Re: __cxa_atexit and __cxa_finalized

Post by tonytian »

Thanks.
berkus wrote:Yes, just write dummies for these.
Yargh
Member
Member
Posts: 56
Joined: Sat Jun 12, 2010 9:04 pm
Location: Somewhere else.

Re: __cxa_atexit and __cxa_finalized

Post 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.
Wait... What?
User avatar
blobmiester
Member
Member
Posts: 45
Joined: Fri Jul 16, 2010 9:49 am

Re: __cxa_atexit and __cxa_finalized

Post 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).
Post Reply