【问题标题】:ELF Executable SegfaultsELF 可执行段错误
【发布时间】:2022-01-18 12:49:34
【问题描述】:

我一直在尝试使用 libelf 创建一个 ELF 可执行文件,但还没有运行它。每当我尝试运行它时,我都会遇到段错误。 strace 输出:

execve("./test.elf", ["./test.elf"], 0x7ffc28d60660 /* 63 vars */) = -1 EINVAL (Invalid argument)
+++ killed by SIGSEGV +++

readelf 给出:

ELF Header:
  Magic:   7f 45 4c 46 02 01 01 00 00 00 00 00 00 00 00 00 
  Class:                             ELF64
  Data:                              2's complement, little endian
  Version:                           1 (current)
  OS/ABI:                            UNIX - System V
  ABI Version:                       0
  Type:                              EXEC (Executable file)
  Machine:                           Advanced Micro Devices X86-64
  Version:                           0x1
  Entry point address:               0x1000
  Start of program headers:          64 (bytes into file)
  Start of section headers:          152 (bytes into file)
  Flags:                             0x0
  Size of this header:               64 (bytes)
  Size of program headers:           56 (bytes)
  Number of program headers:         1
  Size of section headers:           64 (bytes)
  Number of section headers:         3
  Section header string table index: 2

Section Headers:
  [Nr] Name              Type             Address           Offset
       Size              EntSize          Flags  Link  Info  Align
  [ 0]                   NULL             0000000000000000  00000000
       0000000000000000  0000000000000000           0     0     0
  [ 1] .text             PROGBITS         0000000000001000  00000078
       0000000000000008  0000000000000000  AX       0     0     1
  [ 2] .shstrtab         STRTAB           0000000000000000  00000080
       0000000000000011  0000000000000000   A       0     0     1
Key to Flags:
  W (write), A (alloc), X (execute), M (merge), S (strings), I (info),
  L (link order), O (extra OS processing required), G (group), T (TLS),
  C (compressed), x (unknown), o (OS specific), E (exclude),
  l (large), p (processor specific)

There are no section groups in this file.

Program Headers:
  Type           Offset             VirtAddr           PhysAddr
                 FileSiz            MemSiz              Flags  Align
  LOAD           0x0000000000000078 0x0000000000001000 0x0000000000001000
                 0x0000000000000008 0x0000000000000008  R E    0x1

 Section to Segment mapping:
  Segment Sections...
   00     .text 

There is no dynamic section in this file.

There are no relocations in this file.

The decoding of unwind sections for machine type Advanced Micro Devices X86-64 is not currently supported.

No version information found in this file.

这是文件的十六进制转储:

00000000  7f 45 4c 46 02 01 01 00  00 00 00 00 00 00 00 00  |.ELF............|
00000010  02 00 3e 00 01 00 00 00  00 10 00 00 00 00 00 00  |..>.............|
00000020  40 00 00 00 00 00 00 00  98 00 00 00 00 00 00 00  |@...............|
00000030  00 00 00 00 40 00 38 00  01 00 40 00 03 00 02 00  |....@.8...@.....|
00000040  01 00 00 00 05 00 00 00  78 00 00 00 00 00 00 00  |........x.......|
00000050  00 10 00 00 00 00 00 00  00 10 00 00 00 00 00 00  |................|
00000060  08 00 00 00 00 00 00 00  08 00 00 00 00 00 00 00  |................|
00000070  01 00 00 00 00 00 00 00  31 c0 ff c0 b3 2a cd 80  |........1....*..|
00000080  00 2e 74 65 78 74 00 2e  73 68 73 74 72 74 61 62  |..text..shstrtab|
00000090  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
000000d0  00 00 00 00 00 00 00 00  01 00 00 00 01 00 00 00  |................|
000000e0  06 00 00 00 00 00 00 00  00 10 00 00 00 00 00 00  |................|
000000f0  78 00 00 00 00 00 00 00  08 00 00 00 00 00 00 00  |x...............|
00000100  00 00 00 00 00 00 00 00  01 00 00 00 00 00 00 00  |................|
00000110  00 00 00 00 00 00 00 00  07 00 00 00 03 00 00 00  |................|
00000120  02 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00000130  80 00 00 00 00 00 00 00  11 00 00 00 00 00 00 00  |................|
00000140  00 00 00 00 00 00 00 00  01 00 00 00 00 00 00 00  |................|
00000150  00 00 00 00 00 00 00 00                           |........|
00000158

根据我阅读的内容并将其与工作可执行文件进行比较,这应该可以工作。但事实并非如此,我也不知道为什么。任何帮助表示赞赏,谢谢。

【问题讨论】:

  • 我收到“readelf:警告:第 0 节的 sh_link 值超出范围 120 readelf:警告:第 1 节的 sh_link 值超出范围 128”
  • execve 的手册页将 EINVAL 与解释器字段的一些事故联系起来。
  • 感谢@dratenik,您在 readelf 中使用了哪些选项来获得该警告?我似乎无法触发它。
  • 那是readelf -a

标签: binaryfiles elf


【解决方案1】:

我想通了!当我查看一个工作可执行文件以供参考时,我正在查看一个 PIE,但我正在尝试创建一个非 PIE。我没有意识到EHDR type needs to be ET_DYN for a PIE。另外,有些地址是错误的。

新的readelf

ELF Header:
  Magic:   7f 45 4c 46 02 01 01 00 00 00 00 00 00 00 00 00 
  Class:                             ELF64
  Data:                              2's complement, little endian
  Version:                           1 (current)
  OS/ABI:                            UNIX - System V
  ABI Version:                       0
  Type:                              DYN (Shared object file)
  Machine:                           Advanced Micro Devices X86-64
  Version:                           0x1
  Entry point address:               0x1078
  Start of program headers:          64 (bytes into file)
  Start of section headers:          152 (bytes into file)
  Flags:                             0x0
  Size of this header:               64 (bytes)
  Size of program headers:           56 (bytes)
  Number of program headers:         1
  Size of section headers:           64 (bytes)
  Number of section headers:         3
  Section header string table index: 2

Section Headers:
  [Nr] Name              Type             Address           Offset
       Size              EntSize          Flags  Link  Info  Align
  [ 0]                   NULL             0000000000000000  00000000
       0000000000000000  0000000000000000           0     0     0
  [ 1] .text             PROGBITS         0000000000001078  00000078
       0000000000000008  0000000000000000  AX       0     0     1
  [ 2] .shstrtab         STRTAB           0000000000000000  00000080
       0000000000000011  0000000000000000   A       0     0     1
Key to Flags:
  W (write), A (alloc), X (execute), M (merge), S (strings), I (info),
  L (link order), O (extra OS processing required), G (group), T (TLS),
  C (compressed), x (unknown), o (OS specific), E (exclude),
  l (large), p (processor specific)

There are no section groups in this file.

Program Headers:
  Type           Offset             VirtAddr           PhysAddr
                 FileSiz            MemSiz              Flags  Align
  LOAD           0x0000000000000078 0x0000000000001078 0x0000000000001078
                 0x0000000000000008 0x0000000000000008  R E    0x1

 Section to Segment mapping:
  Segment Sections...
   00     .text 

There is no dynamic section in this file.

There are no relocations in this file.

The decoding of unwind sections for machine type Advanced Micro Devices X86-64 is not currently supported.

No version information found in this file.

还有新的 hexdump:

00000000  7f 45 4c 46 02 01 01 00  00 00 00 00 00 00 00 00  |.ELF............|
00000010  03 00 3e 00 01 00 00 00  78 10 00 00 00 00 00 00  |..>.....x.......|
00000020  40 00 00 00 00 00 00 00  98 00 00 00 00 00 00 00  |@...............|
00000030  00 00 00 00 40 00 38 00  01 00 40 00 03 00 02 00  |....@.8...@.....|
00000040  01 00 00 00 05 00 00 00  78 00 00 00 00 00 00 00  |........x.......|
00000050  78 10 00 00 00 00 00 00  78 10 00 00 00 00 00 00  |x.......x.......|
00000060  08 00 00 00 00 00 00 00  08 00 00 00 00 00 00 00  |................|
00000070  01 00 00 00 00 00 00 00  31 c0 ff c0 b3 2a cd 80  |........1....*..|
00000080  00 2e 74 65 78 74 00 2e  73 68 73 74 72 74 61 62  |..text..shstrtab|
00000090  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
000000d0  00 00 00 00 00 00 00 00  01 00 00 00 01 00 00 00  |................|
000000e0  06 00 00 00 00 00 00 00  78 10 00 00 00 00 00 00  |........x.......|
000000f0  78 00 00 00 00 00 00 00  08 00 00 00 00 00 00 00  |x...............|
00000100  00 00 00 00 00 00 00 00  01 00 00 00 00 00 00 00  |................|
00000110  00 00 00 00 00 00 00 00  07 00 00 00 03 00 00 00  |................|
00000120  02 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00000130  80 00 00 00 00 00 00 00  11 00 00 00 00 00 00 00  |................|
00000140  00 00 00 00 00 00 00 00  01 00 00 00 00 00 00 00  |................|
00000150  00 00 00 00 00 00 00 00                           |........|
00000158

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-10-23
    • 1970-01-01
    • 2012-06-25
    • 2015-09-10
    • 2023-03-04
    • 2018-12-12
    • 2020-09-18
    相关资源
    最近更新 更多