我的系统:在 x86_64 CPU 上运行的 Ubuntu 22.04.3。
我在名为 test.c 的文件中有这个 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
我的问题:
在我的特定 Ubuntu 系统中,我的 C 程序的 main 函数是由 __libc_start_main 调用的吗?
最佳答案
Is the main function of my C program called by __libc_start_main in my specific Ubuntu system?
是的。它需要一个指向 main
函数的指针作为参数。即类型 int (*)(int, char **, char **)
。
该指令使用指令指针相对寻址来引用您的 main
函数,并将计算结果作为参数放入 rdi
寄存器中:
1058: 48 8d 3d ca 00 00 00 leaq 0xca(%rip),%rdi # 1129 <main>
你可能想知道,main
的0x1129
地址是如何计算的?您只需获取指令指针 0x1058
的当前值,添加 0x7
(指令本身的长度),然后添加 0xCA
偏移量:
0x1058 + 0x7 + 0xCA = 0x1129
libc_start_main
然后将通过该寄存器的内容间接调用您的函数。
关于c - 在 Linux Ubuntu 系统上,main 函数是由 _libc_start_main 函数调用的吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/77007062/