0

I've been trying to create an ELF executable using libelf, but haven't gotten it running yet. Whenever I try to run it, I just get a segfault. strace outputs:

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

readelf gives:

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.

And here's the hexdump of the 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

From what I've read and comparing it to working executables, this should work. But it doesn't, and I have no idea why. Any help is appreciated, thanks.

TheTVGuy
  • 49
  • 1
  • 9
  • I get "readelf: Warning: Section 0 has an out of range sh_link value of 120 readelf: Warning: Section 1 has an out of range sh_link value of 128" –  Dec 15 '21 at 10:29
  • The man page for execve ties EINVAL to some mishap with the interpreter field. –  Dec 15 '21 at 10:30
  • Thanks @dratenik, what options did you use with readelf to get that warning? I can't seem to trigger it. – TheTVGuy Dec 15 '21 at 20:44
  • That was `readelf -a`. –  Dec 15 '21 at 21:53

1 Answers1

0

I figured it out! When I was looking at a working executable for reference, I was looking at a PIE, but I was trying to create a non-PIE. I didn't realize the EHDR type needs to be ET_DYN for a PIE. Also, a few of the addresses were wrong.

The new 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.

And the new 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
TheTVGuy
  • 49
  • 1
  • 9