__do_global_dtors_aux and __do_global_ctors_aux

后端 未结 2 1367
长情又很酷
长情又很酷 2021-01-02 09:53

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 m

相关标签:
2条回答
  • 2021-01-02 10:26

    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.

    0 讨论(0)
  • 2021-01-02 10:50

    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.

    0 讨论(0)
提交回复
热议问题