我的系统:在 x86_64 CPU 上运行的 Ubuntu 22.04.3。
我有这个 C 程序在名为 test.c 的文件中:
int main(){
long int a = 10;
long int b = 20;
}
我用“gcc test.c -fno-stack-protector -mno-red-zone -o test”编译了文件,然后执行了“objdump -dw -M suffix test”(我只显示 _start 函数) :
0000000000001040 <_start>:
1040: f3 0f 1e fa endbr64
1044: 31 ed xorl %ebp,%ebp
1046: 49 89 d1 movq %rdx,%r9
1049: 5e popq %rsi
104a: 48 89 e2 movq %rsp,%rdx
104d: 48 83 e4 f0 andq $0xfffffffffffffff0,%rsp
1051: 50 pushq %rax
1052: 54 pushq %rsp
1053: 45 31 c0 xorl %r8d,%r8d
1056: 31 c9 xorl %ecx,%ecx
1058: 48 8d 3d ca 00 00 00 leaq 0xca(%rip),%rdi # 1129 <main>
105f: ff 15 73 2f 00 00 callq *0x2f73(%rip) # 3fd8
<__libc_start_main@GLIBC_2.34>
1065: f4 hlt
1066: 66 2e 0f 1f 84 00 00 00 00 00 cs nopw 0x0(%rax,%rax,1)
我的假设是,我们有以下调用链,其中 _start 调用 __libc_start_main ,而 __libc_start_main 又调用 main :
_start -> __libc_start_main -> main
我的问题:
我的C程序的main函数是在我特定的Ubuntu系统中被__libc_start_main调用的吗?
我的C程序的main函数在我特定的Ubuntu系统中是由__libc_start_main调用的吗?
是的。它需要一个指向
main
函数的指针作为参数。即类型int (*)(int, char**, char**)
。
该指令使用指令指针相对寻址来引用您的
main
函数,并将其作为参数放入寄存器中:
1058: 48 8d 3d ca 00 00 00 leaq 0xca(%rip),%rdi # 1129 <main>
libs_start_main
将通过该寄存器的内容间接调用您的函数。