__do_global_dtors_aux and __do_global_ctors_aux

你说的曾经没有我的故事 提交于 2020-01-29 04:54:24

问题


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.



来源:https://stackoverflow.com/questions/6477494/do-global-dtors-aux-and-do-global-ctors-aux

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!