gpt4 book ai didi

c - 为什么我的机器在此代码中将堆栈指针减少 20?

转载 作者:太空宇宙 更新时间:2023-11-04 06:04:21 25 4
gpt4 key购买 nike

我用 GCC 编译了以下 C 代码,并在 gdb 中反汇编为汇编。我正在使用配备 64 位 Intel i5 处理器的 Macbook pro。在“main()”中,“char* name[2]”有 2 个字符指针,应该让堆栈指针减少 2 个字(16 字节)?但是,当我在 gdb 中反汇编时,它减少了 20...有人可以帮助我理解吗?C代码:

#include <stdio.h>
#include <unistd.h>
int main(void)
{
char* name[2];
name[0] = "/bin/sh";
name[1]= NULL;
execve(name[0],name,NULL);
}

gdb反汇编代码:

GNU gdb 6.3.50-20050815 (Apple version gdb-1708) (Thu Nov  3 21:59:02 UTC 2011)
Copyright 2004 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB. Type "show warranty" for details.
This GDB was configured as "x86_64-apple-darwin"...Reading symbols for shared libraries .. done

(gdb) disassemble main
Dump of assembler code for function main:
0x0000000100000ee0 <main+0>: push %rbp
0x0000000100000ee1 <main+1>: mov %rsp,%rbp
0x0000000100000ee4 <main+4>: sub $0x20,%rsp
0x0000000100000ee8 <main+8>: lea -0x18(%rbp),%rax
0x0000000100000eec <main+12>: lea 0x61(%rip),%rcx # 0x100000f54
0x0000000100000ef3 <main+19>: mov %rcx,-0x18(%rbp)
0x0000000100000ef7 <main+23>: movq $0x0,-0x10(%rbp)
0x0000000100000eff <main+31>: mov -0x18(%rbp),%rcx
0x0000000100000f03 <main+35>: mov $0x0,%rdx
0x0000000100000f0d <main+45>: mov %rcx,%rdi
0x0000000100000f10 <main+48>: mov %rax,%rsi
0x0000000100000f13 <main+51>: callq 0x100000f22 <dyld_stub_execve>
0x0000000100000f18 <main+56>: mov -0x4(%rbp),%eax
0x0000000100000f1b <main+59>: add $0x20,%rsp
0x0000000100000f1f <main+63>: pop %rbp
0x0000000100000f20 <main+64>: retq
End of assembler dump.

最佳答案

因此,读取程序集,name[0] 在堆栈上的 -0x18 和 name 1位于 -0x10。此外,如前所述,-0x4 用于返回值。

剩下 12 个字节,因为 0x20 实际上是 32。范围 -32 到 -24 和范围 -8 到 -4。-8 到 -4 绝对是对齐,我猜 -32 到 -24 也是如此(对于 16 字节对齐)。

编辑:请注意,实际上减去了 48 个字节。调用 main 的 callq 指令从 RSP 中减去 8 个字节,然后 push %rbp 再减去 8 个字节。

已验证 16 字节对齐:x86-64 System V ABI第 3.2.2 节(direct link to PDF of an old version)

关于c - 为什么我的机器在此代码中将堆栈指针减少 20?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13222965/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com