4 ms·
I'm curious to learn: what C++ constructors have to do with the executable format? I do get that atexit might require a special place in the binary for the kern
by egorfine 8y ago
I'm curious to learn: what C++ constructors have to do with the executable format? I do get that atexit might require a special place in the binary for the kernel to jump to, but how does a relatively high end language feature drop down to binary format?
- pjmlp 8y agoConstructors of global static objects, they are required to be called before main() starts execution, likewise destructors of such objects are supposed to run in inverse order on program termination, including after call to atexit(). This needs to happen regardless how the binary, or dynamic library gets loaded.
- gumby 8y ago> how does a relatively high end language feature drop down to binary format? All high end language features drop down to binary formats -- that's what ABIs are. In the case of things like constructors and destructors you can of course just stick them all in the text section and have the linker construct call to them in some fashion. You'd have to use some reserved name mangling to distinguish them so that the linker can even find them. If instead you have separate sections the linker can just run through the symbol table; the dynamic loader can even do so so the compile-time linker can ignore those sections (just copy them). You can even play tricks like unloading them after running (or only load exit code upon exit) if memory games like that matter. etc etc. It's all about how you communicate semantic issues from compile time to run time, as with all ABI issues.