ELF 可执行段错误

ELF Executable Segfaults

我一直在尝试使用 libelf 创建 ELF 可执行文件,但还没有成功 运行ning。每当我尝试 运行 它时,我都会遇到段错误。 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

根据我阅读的内容并将其与工作的可执行文件进行比较,这应该可以工作。但它没有,我不知道为什么。感谢任何帮助,谢谢。

我想通了!当我查看工作可执行文件以供参考时,我查看的是 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.

以及新的十六进制转储:

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