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?
问题:
回答1:
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.
回答2:
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.