gcc generates unnecessary (?) instructions

后端 未结 3 1264
长情又很酷
长情又很酷 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:37

    gcc generates unnecessary (?) instructions

    Yes, because you invoked GCC without asking for any compiler optimizations.

    My recommendation: compile with

    gcc -fverbose-asm -O2 -S test.c
    

    then look inside the generated test.s assembler code.

    BTW, most of the code is from crt0, which is given by, not emitted by, gcc. Build your executable with gcc -O2 -v test.c -o testprog to understand what GCC really does. Read documentation of GCC internals.

    Since GCC is free software, you are allowed to look inside its source code and improve it. But the crt0 stuff is tricky, and operating system specific.

    Consider also reading about linkers and loaders, about ELF executables, and How to write shared libraries, and the Linux Assembler HowTo.

提交回复
热议问题