linux gcc 堆栈,linux – 试图在main的顶部理解gcc复杂的堆栈对齐,复制返回地址

嗨,我已经拆解了一些程序(linux),我写的是为了更好地理解它是如何工作的,我注意到主函数始终以:

lea ecx,[esp+0x4] ; I assume this is for getting the adress of the first argument of the main...why ?

and esp,0xfffffff0 ; ??? is the compiler trying to align the stack pointer on 16 bytes ???

push DWORD PTR [ecx-0x4] ; I understand the assembler is pushing the return adress....why ?

push ebp

mov ebp,esp

push ecx ;why is ecx pushed too ??

所以我的问题是:为什么所有这些工作都完成了?

我只了解使用:

push ebp

mov ebp,esp

其余对我来说似乎毫无用处……

解决方法:

我已经开始了:

;# As you have already noticed, the compiler wants to align the stack

;# pointer on a 16 byte boundary before it pushes anything. That's

;# because certain instructions' memory access needs to be aligned

;# that way.

;# So in order to first save the original offset of esp (+4), it

;# executes the first instruction:

lea ecx,[esp+0x4]

;# Now alignment can happen. Without the previous insn the next one

;# would have made the original esp unrecoverable:

and esp,0xfffffff0

;# Next it pushes the return addresss and creates a stack frame. I

;# assume it now wants to make the stack look like a normal

;# subroutine call:

push DWORD PTR [ecx-0x4]

push ebp

mov ebp,esp

;# Remember that ecx is still the only value that can restore the

;# original esp. Since ecx may be garbled by any subroutine calls,

;# it has to save it somewhere:

push ecx

标签:x86,linux,assembly,gcc,compiler-construction

来源: https://codeday.me/bug/20190928/1826869.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值