__cxa_atexit and other GCC stuff
Posted: Wed Apr 29, 2020 7:18 am
Hey everyone, I've recently started to learn about how GCC implements C++ as it was something good (necessary?) to know to make it work properly on my OS.
I've read a few very good articles about how it works, and I sort of understand it now, however i'm still a bit confused about what parts of the runtime are provided by GCC,
and what parts are OS/someone else.
AFAIK GCC uses itanium ABI for C++ so it expects __cxa_atexit and other functions to be available and calls them. But where does _init and _fini and other functions come from? Who implements frame_dummy, who implements __libc_start_main? Is is something GCC has hardcoded as a function name and expects a definition somewhere? I'd very much appreciate if someone could explain me the dependency structure of GCC.
I've used a tutorial from this website to implement ctri and crtn for global constructors via _init and _fini, but global destructors were not getting called until I passed -fno-use-cxa-atexit as a parameter to the compiler.
Why is it calling 0 and what is it even doing? (the only reason why it says __cxa_ateixt there is because I didn't put it in a section so it ended up at the top of the executable, and the starting address is 0x20000 so it makes 0 sense for it to call NULL.)
Also I know that it doesn't actually get to that call 0 instruction because in both cases the kernel successfully returns from the function.
Why did passing -fno-use-cxa-atexit make my global destructors work correctly? (my implementation of __cxa_atexit is just ret so I know this wouldn't work).
(FYI: I know that kernel doesn't need destructors, that's not the point of this question, i'm simply trying to get to the bottom of this.)
On the left of this screenshot is the -fno-use-cxa-atexit variant of my kernel and on the right is the default.
In the cxa-at-exit version, the global class constructor does `call 20000 <__cxa_atexit>`, which makes sense, however in the no-cxa version it doesn't, so how does do_global_dtors know to call the destructor?
I know this is a lot of questions but this feels like some secret ancient knowledge that like 10 people in the world know
Thanks.
I've read a few very good articles about how it works, and I sort of understand it now, however i'm still a bit confused about what parts of the runtime are provided by GCC,
and what parts are OS/someone else.
AFAIK GCC uses itanium ABI for C++ so it expects __cxa_atexit and other functions to be available and calls them. But where does _init and _fini and other functions come from? Who implements frame_dummy, who implements __libc_start_main? Is is something GCC has hardcoded as a function name and expects a definition somewhere? I'd very much appreciate if someone could explain me the dependency structure of GCC.
I've used a tutorial from this website to implement ctri and crtn for global constructors via _init and _fini, but global destructors were not getting called until I passed -fno-use-cxa-atexit as a parameter to the compiler.
Why is it calling 0 and what is it even doing? (the only reason why it says __cxa_ateixt there is because I didn't put it in a section so it ended up at the top of the executable, and the starting address is 0x20000 so it makes 0 sense for it to call NULL.)
Also I know that it doesn't actually get to that call 0 instruction because in both cases the kernel successfully returns from the function.
Why did passing -fno-use-cxa-atexit make my global destructors work correctly? (my implementation of __cxa_atexit is just ret so I know this wouldn't work).
(FYI: I know that kernel doesn't need destructors, that's not the point of this question, i'm simply trying to get to the bottom of this.)
On the left of this screenshot is the -fno-use-cxa-atexit variant of my kernel and on the right is the default.
In the cxa-at-exit version, the global class constructor does `call 20000 <__cxa_atexit>`, which makes sense, however in the no-cxa version it doesn't, so how does do_global_dtors know to call the destructor?
I know this is a lot of questions but this feels like some secret ancient knowledge that like 10 people in the world know
Thanks.