gcc generates unnecessary (?) instructions

后端 未结 3 1236
长情又很酷
长情又很酷 2021-01-15 22:50

I decided to compile a very basic C program and take a look at the generated code with objdump -d.

int main(int argc, char *argv[]) {
    exit(0         


        
3条回答
  •  执念已碎
    2021-01-15 23:35

    C program does a lots of stuff before calling the main function. It has to initialize .data and .bss segments, set the stack, go through the constructors and destructors (yes gcc in C has a special attributes for such a functions) and initializes the library.

    gcc destructor and constructor functions:

    void __attribute__ ((constructor)) funcname(void);
    
    void __attribute__ ((destructor)) funcname(void);
    

    you may have as many constructors and destructors as you wish.

    constructors are called before call to the main function, destructors on exit from the program (after the main termination)

    https://gcc.gnu.org/onlinedocs/gcc-4.7.0/gcc/Function-Attributes.html#Function-Attributes

提交回复
热议问题