ELF文件的程序头位置敏感吗?

问题描述 投票:0回答:1

我正在学习 ELF 文件格式,想知道如何修补 ELF 文件。 这本书ELF_Format.pdf说:

虽然图中显示的是紧接在 ELF header 之后的程序头表,而该节 各部分后面的标头表,实际文件可能有所不同。此外,节和段没有 指定的顺序。只有ELF头在文件中有固定的位置 在第 8 页。

出于好奇,我复制了程序头并将其附加到 ELF 的末尾,如下所示:

  1. dd if=test.elf skip=64 bs=1 count=504 of=program_header.bin
    // 复制源程序头(PH 偏移=64, PH 条目大小=56, PH 条目数量=9)
  2. cat program_header.bin >> test.elf
    // 将程序头追加到elf文件末尾
  3. 修改ELF头的
    e_phoff
    ,指向步骤2复制的程序头。

之后

readelf -l test.elf
可以看到Program Header更改成功: enter image description here

但是当我尝试执行修改后的 elf 时,发生了“分段错误”:

[root@localhost 1]# ./test.elf 
Segmentation fault (core dumped)
[root@localhost 1]# ls
program_header.bin test.elf  test.elf.54214.core
[root@localhost 1]#
[root@localhost 1]# gdb test.elf -c test.elf.54214.core 
GNU gdb (GDB) Rocky Linux 10.2-13.el9
Copyright (C) 2021 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Type "show copying" and "show warranty" for details.
This GDB was configured as "x86_64-redhat-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<https://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
    <http://www.gnu.org/software/gdb/documentation/>.

For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from test.elf...
(No debugging symbols found in test.elf)
[New LWP 54214]

warning: Section `.reg-xstate/54214' in core file too small.
Core was generated by `./test.elf'.
Program terminated with signal SIGSEGV, Segmentation fault.

warning: Section `.reg-xstate/54214' in core file too small.
#0  0x00007fad7fcc2b38 in ?? ()
(gdb) bt
#0  0x00007fad7fcc2b38 in ?? ()
#1  0x0000000000000000 in ?? ()
(gdb) 

我想知道程序头是否有位置敏感的内容,是什么导致了核心转储?

我试图从 elf 格式手册中查找并用谷歌搜索,但找不到答案。

linux reverse-engineering elf
1个回答
0
投票

我在ELF_format.pdf第34页找到了关于PT_PHDR的描述:

此外,只有当程序头 表是程序内存映像的一部分。

也许这就是原因。我修改了ELF文件后,程序头不再属于任何段(当然不属于程序内存映像的一部分)。

但是原来的程序头表还在,我还是想知道为什么会崩溃..

© www.soinside.com 2019 - 2024. All rights reserved.