Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

__do_global_dtors_aux and __do_global_ctors_aux

I disassembled a simple program written in C++ and there are these two function names. I guess that ctor means constructor and dtor means destructor, and word global maybe means that they create and destroy global objects. I cannot guess the name aux. What do these two functions do?

like image 642
scdmb Avatar asked Jun 25 '11 11:06

scdmb


2 Answers

The addresses of constructors and destructors of static objects are each stored in a different section in ELF executable. for the constructors there is a section called .CTORS and for the destructors there is the .DTORS section.

the compiler creates two auxillary functions __do_global_ctors_aux and __do_global_dtors_aux for calling the constructors and destructors of these static objects, respectively.

__do_global_ctors_aux function simply performs a walk on the .CTORS section, while the __do_global_dtors_aux does the same job only for the .DTORS section which contains the program specified destructors functions.

like image 134
Alok Save Avatar answered Oct 02 '22 00:10

Alok Save


They are auxiliary functions added by the compiler to construct and destroy static objects.

e.g.

std::vector<int> some_global;

int main() { return 0; }

some_global needs to be actually constructed (and destructed) somewhere, and C++ guarantees that construction happens before main. One way to do this is to emit a function that happens before main, which constructs global objects, and another function that happens after main to destroy them.

If you stuck a breakpoint inside the std::vector constructor and ran this program, the stack trace would show you the function that it was being initialised from.

like image 24
Peter Alexander Avatar answered Oct 01 '22 23:10

Peter Alexander