【发布时间】:2014-01-26 18:30:35
【问题描述】:
所以看起来everyone 知道 OSX 系统调用始终是 16 字节堆栈对齐的。太好了,当你有这样的代码时,这很有意义:
section .data
message db 'something', 10, 0
section .text
global start
start:
push 10 ; size of the message (4 bytes)
push msg ; the address of the message (4 bytes)
push 1 ; we want to write to STD_OUT (4 bytes)
mov eax, 4 ; write(...) syscall
sub esp, 4 ; move stack pointer down to 4 bytes for a total of 16.
int 0x80 ; invoke
add esp, 16 ; clean
完美,堆栈对齐为 16 个字节,非常有意义。虽然我们调用 syscall(1) (exit) 怎么样。逻辑上看起来像这样:
push 69 ; return value
mov eax, 1 ; exit(...) syscall
sub esp, 12 ; push down stack for total of 16 bytes.
int 0x80 ; invoke
虽然这不起作用,但这样做:
push 69 ; return value
mov eax, 1 ; exit(...) syscall
sub esp, 4 ; push down stack for total of 8 bytes.
int 0x80 ; invoke
这很好用,但只有 8 个字节???? Osx 很酷,但是这个 ABI 让我发疯了。有人可以阐明我不理解的内容吗?
【问题讨论】:
-
您确定堆栈指针在您显示的代码之前是 16 字节对齐的吗?此外,正如您所知,Apple 不会在系统调用级别维护二进制兼容性,仅在系统库级别。
-
是的,事实上我可以只写最后一个代码段,它按预期工作。但它并没有真正与我听到的关于 16 字节对齐的所有内容融为一体。