我如何告诉 GCC 不要将 main 的堆栈与 16 字节边界对齐?

How can I tell GCC not to align main's stack to 16-byte boundary?

GCC 在调整我的 main 的堆栈,将参数的位置保存到 ecx

0x08049060      8d4c2404       lea ecx, [arg_4h]                       ; 4 ; [13] -r-x section size 465 named .text
0x08049064      83e4f0         and esp, 0xfffffff0
0x08049067      ff71fc         push dword [ecx - 4]
0x0804906a      55             push ebp
0x0804906b      89e5           mov ebp, esp
0x0804906d      51             push ecx

然后,

0x080490a4      8b4dfc         mov ecx, dword [local_4h]
0x080490a7      83c410         add esp, 0x10
0x080490aa      c9             leave
0x080490ab      8d61fc         lea esp, [ecx - 4]
0x080490ae      c3             ret

我相信我理解为什么 GCC 正在做它正在做的事情 (you can read about it here),但是我试图从源代码重建的二进制文件缺少这些说明教程,我想生成尽可能接近教程的程序集。

这两个都是 return 来自 file,

stack0: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), dynamically linked, interpreter /lib/ld-linux.so.2, for GNU/Linux 2.6.18,


我在 main 上试过 __attribute__ ((packed)),但 #pragma pack 都没有用。

您可以通过告诉 GCC 对齐到 2 字节而不是 16

来禁用堆栈对齐
-mpreferred-stack-boundary=2

可能会对性能产生一些影响。