Where exactly is the red zone on x86-64?

血红的双手。 提交于 2019-11-27 06:35:45

问题


From Wikipedia:

In computing, a red zone is a fixed-size area in a function's stack frame beyond the return address which is not preserved by that function. The callee function may use the red zone for storing local variables without the extra overhead of modifying the stack pointer. This region of memory is not to be modified by interrupt/exception/signal handlers. The x86-64 ABI used by System V mandates a 128-byte red zone, which begins directly after the return address and includes the function's arguments. The OpenRISC toolchain assumes a 128-byte red zone.

From the System V x86-64 ABI:

The 128-byte area beyond the location pointed to by %rsp is considered to be reserved and shall not be modified by signal or interrupt handlers. Therefore, functions may use this area for temporary data that is not needed across function calls. In particular, leaf functions may use this area for their entire stack frame, rather than adjusting the stack pointer in the prologue and epilogue. This area is known as the red zone.

  • Given these two quotes, is the red zone above the stacked return address or below the stacked return address?

  • Since this red zone is relative to RSP, does it move downward with each push and does it move upward with each pop?


回答1:


Given these two quotes, is the red zone above the stacked return address or below the stacked return address?

The red zone is the 128 bytes just below rsp, i.e. rsp - 128 to rsp - 1.

Since this red zone is relative to RSP, does it move downward with each push and does it move upward with each pop?

Yes.




回答2:


The Wikipedia article about the Red Zone was wrong, thus creating the ambiguity.

I had modified the article in April 2017 to fix the issue. As of that update the Wikipedia article reads:

In computing, a red zone is a fixed-size area in a function's stack frame beyond the current stack pointer which is not preserved by that function. The callee function may use the red zone for storing local variables without the extra overhead of modifying the stack pointer. This region of memory is not to be modified by interrupt/exception/signal handlers. The x86-64 ABI used by System V mandates a 128-byte red zone, which begins directly under the current value of the stack pointer. The OpenRISC toolchain assumes a 128-byte red zone

This brings the Wikipedia article more in line with the 64-bit System V ABI definition. With the ambiguity above resolved, regarding the question:

Since this red zone is relative to RSP, does it move downward with each push and does it move upward with each pop?

The Red Zone is always the 128 bytes just below RSP. As RSP changes (by PUSH/POP/MOV etc) so too does the location of the Red Zone.



来源:https://stackoverflow.com/questions/38042188/where-exactly-is-the-red-zone-on-x86-64

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!