ELF文件格式如何定义堆栈? [英] How does ELF file format defines the stack?

查看:432
本文介绍了ELF文件格式如何定义堆栈?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究ELF文件格式,所以我编译了一个小程序,从生成的可执行文件中转储了节标题及其内容.

I'm studying the ELF file format, so I compiled a small program, dumped the section headers and their contents from the resulting executable.

ELF标头包含入口地址,该地址指向.text节的开头.

The ELF header contains the entry point address, which points into start of the .text section.

我还找到了包含静态数据的.data部分和包含只读数据的.rodata ...我希望堆栈中也有一个部分,但我找不到该部分.

I also found the .data section that contains the static data and .rodata that contains the read only data... I expect there is a section for the stack too, but I can't find that section.

我还希望在某个时候将ESP设置为某个部分的顶部,但在反汇编中找不到类似的内容.

I also expect that at some point ESP is set to the top of some section but I can't find anything like that in the disassembly.

那么ESP如何获得其初始值?

So how does ESP gets its initial value?

推荐答案

下图描述了x86上典型C ELF可执行文件的内存映射.

  • 该过程将.text.data部分加载到基地址.

  • The process loads the .text and .data sections at the base address.

主堆栈位于下方并向下增长.

The main-stack is located just below and grows downwards.

每个线程和函数调用将具有其自己的堆栈.
它位于主堆栈下方.

Each thread and function-call will have its own-stack.
This is located located below the main-stack.

每个堆栈由保护页分隔以检测堆栈溢出.

Each stack is separated by a guard page to detect Stack-Overflow.

因此,不需要ELF文件中的专用stack部分.

Hence one does NOT need a dedicated stack section in the ELF file.

但是在 ELF手册页中 ,一个可以在ELF文件中找到一些可以控制 stack 属性的东西.主要是对内存中堆栈的可执行权限.

However within the man pages for ELF, one does find a couple of things in an ELF file that control the stack attributes. Mainly the executable permissions to the stack in memory.

  1. PT_GNU_STACK
    GNU扩展,Linux内核使用它通过p_flags成员中设置的标志来控制堆栈的状态.

  1. PT_GNU_STACK
    GNU extension which is used by the Linux kernel to control the state of the stack via the flags set in the p_flags member.

.note.GNU-stack
本节在Linux目标文件中用于声明堆栈属性.此部分的类型为SHT_PROGBITS.使用的唯一属性是SHF_EXECINSTR.这向GNU链接器表明目标文件需要可执行堆栈.

.note.GNU-stack
This section is used in Linux object files for declaring stack attributes. This section is of type SHT_PROGBITS. The only attribute used is SHF_EXECINSTR. This indicates to the GNU linker that the object file requires an executable stack.

这篇关于ELF文件格式如何定义堆栈?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆