跳到主要内容

GCC 中的 main 与 __main

· 阅读需 2 分钟
Chengyu HAN
Open Source Contributor

看RE4B, C3 中间有提到用gcc编译简单函数,试了下hello world,发现生成的汇编代码很奇怪,看上去想出现了死循环。

#include <stdio.h>

int main()
{
printf("hello, world\n");
return 0;
}
.file	"3.1.c"
.def __main; .scl 2; .type 32; .endef
.section .rdata,"dr"
.LC0:
.ascii "hello, world\0"
.text
.globl main
.def main; .scl 2; .type 32; .endef
main:
pushq %rbp
movq %rsp, %rbp
subq $32, %rsp
call __main
leaq .LC0(%rip), %rcx
call puts
movl $0, %eax
leave
ret
.ident "GCC: (Rev3, Built by MSYS2 project) 5.2.0"
.def puts; .scl 2; .type 32; .endef

main 函数中间有一句 call __main 看上去很像死循环,到群里问,把左懒大大也给坑了一下

不过实际上通过.def宏可以看出,他们是两个不同的函数,置于为什么这样用还有待深究。

ref: