[英]How ELF file is laid out in memory
When an ELF file is run and its various segments are loaded into memory, is there a way to know in advance how the segments will be arranged in relation to each other?当运行 ELF 文件并将其各个段加载到 memory 中时,有没有办法提前知道这些段将如何相互排列? That is, could I know, for example, that the rodata segment comes after the bss segment?也就是说,我是否可以知道,例如,rodata 段在 bss 段之后?
is there a way to know in advance how the segments will be arranged in relation to each other?有没有办法提前知道这些段将如何相互排列?
Yes.是的。 There are two types of ELF files: ET_EXEC
and ER_DYN
.有两种类型的 ELF 文件: ET_EXEC
和ER_DYN
。 The former is for executables, the latter for PIE executables and shared libraries.前者用于可执行文件,后者用于 PIE 可执行文件和共享库。
An ET_EXEC
segments will be in memory in exactly the location they were linked at. ET_EXEC
段将位于 memory 中,与它们链接的位置完全相同。 The executable will not work if its segments are loaded anywhere else.如果它的段被加载到其他任何地方,则可执行文件将无法工作。
You can examine the linked-at addresses with readelf -Wl a.out
.您可以使用readelf -Wl a.out
检查链接地址。
For ET_DYN
, the segments will be loaded at a single fixed offset of where they were linked.对于ET_DYN
,段将在它们链接位置的单个固定偏移量处加载。 That is, if the first PT_LOAD
segment is linked at address 0x0
, and loaded at address 0x1234000
, then you can add 0x1234000
to the p_vaddr
of every subsequent segment to determine where it is loaded in memory.也就是说,如果第一个PT_LOAD
段链接在地址0x0
并加载到地址0x1234000
,那么您可以将0x1234000
添加到每个后续段的p_vaddr
以确定它在 memory 中的加载位置。
could I know, for example, that the rodata segment comes after the bss segment?例如,我可以知道rodata 段在bss 段之后吗?
There is no such thing as rodata
and bss
segment -- they are sections .没有rodata
和bss
段这样的东西——它们是段。 To see which segment these sections are assigned to, use readelf -Wl
.要查看这些部分分配到哪个段,请使用readelf -Wl
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.