Bug 295629 - Linuxlator fails to run the linux x86_64 binary: Program headers not in the first page
Summary: Linuxlator fails to run the linux x86_64 binary: Program headers not in the f...
Status: Closed FIXED
Alias: None
Product: Base System
Classification: Unclassified
Component: bin (show other bugs)
Version: 15.0-STABLE
Hardware: amd64 Any
: --- Affects Some People
Assignee: Konstantin Belousov
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2026-05-27 04:10 UTC by Yuri Victorovich
Modified: 2026-06-18 21:14 UTC (History)
5 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Yuri Victorovich freebsd_committer freebsd_triage 2026-05-27 04:10:51 UTC
Testcase:

1. Download https://github.com/github/copilot-cli/releases/latest/download/copilot-linux-x64.tar.gz
2. Extract: the 'copilot' binary is the only file in this tarball

copilot fails to run:
> $ ./copilot 
> Program headers not in the first page
> bash: ./copilot: cannot execute binary file: Exec format error

> $ file ./copilot
> ./copilot: ELF 64-bit LSB executable, x86-64, version 1 (GNU/Linux), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, BuildID[sha1]=34bbdf28afe7d9a2b4987aba75069f65316455ac, for GNU/Linux 3.2.0, with debug_info, not stripped, too many notes (256)

linux_base-rl9-9.7 is installed.
Comment 1 Ed Maste freebsd_committer freebsd_triage 2026-05-27 19:21:19 UTC
What does `readelf -l` report for this binary?
Comment 2 Yuri Victorovich freebsd_committer freebsd_triage 2026-05-27 21:10:57 UTC
$ readelf -l /home/yuri/.local/bin/copilot
readelf: warning: '/home/yuri/.local/bin/copilot': SHT_DYNAMIC section with index 26 is not contained within the PT_DYNAMIC segment

Elf file type is EXEC (Executable file)
Entry point 0x7d5fb0
There are 15 program headers, starting at offset 45904260

Program Headers:
  Type           Offset   VirtAddr           PhysAddr           FileSiz  MemSiz   Flg Align
  PHDR           0x2bc7184 0x0000000002fc7184 0x0000000002fc7184 0x038e7c 0x038e7c R   0x8
  INTERP         0x000350 0x0000000000400350 0x0000000000400350 0x00001c 0x00001c R   0x1
      [Requesting program interpreter: /lib64/ld-linux-x86-64.so.2]
  LOAD           0x000000 0x0000000000400000 0x0000000000400000 0x3315b0 0x3315b0 R   0x1000
  LOAD           0x332000 0x0000000000732000 0x0000000000732000 0x0021d0 0x0021d0 R E 0x1000
  LOAD           0x336000 0x0000000000736000 0x0000000000736000 0x28ca000 0x28ca000 R E 0x2000
  LOAD           0x2c00000 0x0000000003000000 0x0000000003000000 0x0006a1 0x0006a1 R E 0x200000
  LOAD           0x2c01000 0x0000000003001000 0x0000000003001000 0x3869492 0x3869492 R   0x1000
  LOAD           0x646a5f0 0x000000000686b5f0 0x000000000686b5f0 0x02e1d0 0x066a38 RW  0x1000
  LOAD           0x75c0000 0x000000000f5c0000 0x000000000f5c0000 0x28e0000 0x28e0000 R   0x1000
  DYNAMIC        0x646cbb0 0x000000000686dbb0 0x000000000686dbb0 0x000200 0x000200 RW  0x8
  NOTE           0x75c0000 0x000000000f5c0000 0x000000000f5c0000 0x28d5108 0x28d5108 R   0x4
  TLS            0x646a5f0 0x000000000686b5f0 0x000000000686b5f0 0x000004 0x0001a8 R   0x8
  GNU_EH_FRAME   0x60001e4 0x00000000064001e4 0x00000000064001e4 0x0c6f0c 0x0c6f0c R   0x4
  GNU_STACK      0x000000 0x0000000000000000 0x0000000000000000 0x000000 0x000000 RW  0x10
  GNU_RELRO      0x646a5f0 0x000000000686b5f0 0x000000000686b5f0 0x003a10 0x003a10 R   0x1

 Section to Segment mapping:
  Segment Sections...
   00     
   01     .interp 
   02     .interp .gnu.hash .dynsym .dynstr .gnu.version .gnu.version_r .rela.dyn .rela.plt 
   03     .init .plt 
   04     .text 
   05     lpstub malloc_hook .fini 
   06     .rodata .eh_frame_hdr .eh_frame .gcc_except_table 
   07     .tdata .init_array .fini_array .data.rel.ro .dynamic .got .data .bss 
   08     .note.gnu.build-id .note.ABI-tag 
   09     
   10     .note.gnu.build-id .note.ABI-tag 
   11     .tdata .tbss 
   12     .eh_frame_hdr 
   13     
   14     .tdata .init_array .fini_array .data.rel.ro .dynamic .got 
   None   .comment .gnu.build.attributes .debug_aranges .debug_info .debug_abbrev .debug_line .debug_str .debug_loc .debug_ranges .symtab .strtab .shstrtab
Comment 3 Yuri Victorovich freebsd_committer freebsd_triage 2026-05-28 05:02:28 UTC
Here is the problem analysis:

 Root Cause

  The copilot binary has an extremely non-standard ELF layout where the program header table is placed at file offset 45,904,260 bytes (~44 MB into the file):

   Start of program headers:  45904260 (bytes into file)  # e_phoff = 0x2BC7184

  FreeBSD's ELF loader in sys/kern/imgact_elf.c enforces this check:

   static bool
   __elfN(phdr_in_zero_page)(const Elf_Ehdr *hdr)
   {
       return (hdr->e_phoff <= PAGE_SIZE &&
           (u_int)hdr->e_phentsize * hdr->e_phnum <= PAGE_SIZE - hdr->e_phoff);
   }

  This requires:

   1. e_phoff ≤ PAGE_SIZE (4096) — FAILS: 45,904,260 >> 4,096
   2. All program headers fit in the remainder of the first page

  The kernel only maps the first page of an executable into imgp->image_header and immediately dereferences program headers from that buffer (imgp->image_header + hdr->e_phoff). If e_phoff
  points beyond page 0, the data isn't there.

  Linux's kernel is different: load_elf_binary() uses kernel_read() to read program headers at whatever offset e_phoff specifies, so it handles this layout fine.

  Why is the binary like this?

  The copilot binary (a Go binary) is built with debug info embedded, and has an unusual segment layout — its first LOAD segment covers 0x3315b0 (~3.1 MB) of read-only data placed before
  the executable code, with the program header table appended after most of the file content rather than near the ELF header. In a normal ELF binary, e_phoff = 64 (immediately after the
  64-byte ELF header).

  Kernel fix: Extend FreeBSD's ELF loader to use vn_rdwr to read program headers at arbitrary offsets (like Linux does), instead of requiring them in the first page.
Comment 4 Yuri Victorovich freebsd_committer freebsd_triage 2026-05-28 05:04:08 UTC
I wonder would a FreeBSD ELF also fail to run if it would have a similar layout?
Comment 5 Konstantin Belousov freebsd_committer freebsd_triage 2026-05-28 09:27:49 UTC
This is the first time ever we see such binary.

Try https://reviews.freebsd.org/D57294, untested.
Comment 6 Alex S 2026-05-28 16:13:33 UTC
I've seen 4 such binaries so far [1], but since they all were games
(and thus low value targets), I didn't bother reporting anything.
I'll test them just in case.

[1]: https://github.com/shkhln/linuxulator-steam-utils/wiki/Compatibility
Comment 7 Alex S 2026-05-28 20:42:09 UTC
(In reply to Alex S from comment #6)

> I've seen 4 such binaries so far

All of them start with D57294.
Comment 8 commit-hook freebsd_committer freebsd_triage 2026-05-28 21:00:12 UTC
A commit in branch main references this bug:

URL: https://cgit.FreeBSD.org/src/commit/?id=804daf166406a1b0e22f93b9417b90ed7d3aa1ae

commit 804daf166406a1b0e22f93b9417b90ed7d3aa1ae
Author:     Konstantin Belousov <kib@FreeBSD.org>
AuthorDate: 2026-05-28 09:06:33 +0000
Commit:     Konstantin Belousov <kib@FreeBSD.org>
CommitDate: 2026-05-28 20:59:26 +0000

    imgact_elf: read program headers if not contained in the first page

    PR:     295629
    Reviewed by:    markj
    Tested by:      Alex S <iwtcex@gmail.com>
    Sponsored by:   The FreeBSD Foundation
    MFC after:      1 week
    Differential revision:  https://reviews.freebsd.org/D57294

 sys/kern/imgact_elf.c | 91 +++++++++++++++++++++++++++++++++++----------------
 1 file changed, 62 insertions(+), 29 deletions(-)
Comment 9 commit-hook freebsd_committer freebsd_triage 2026-06-04 00:36:43 UTC
A commit in branch stable/15 references this bug:

URL: https://cgit.FreeBSD.org/src/commit/?id=2fa4a6b79f7b00caa206d1bd52cb0253988f2f6a

commit 2fa4a6b79f7b00caa206d1bd52cb0253988f2f6a
Author:     Konstantin Belousov <kib@FreeBSD.org>
AuthorDate: 2026-05-28 09:06:33 +0000
Commit:     Konstantin Belousov <kib@FreeBSD.org>
CommitDate: 2026-06-04 00:34:42 +0000

    imgact_elf: read program headers if not contained in the first page

    PR:     295629

    (cherry picked from commit 804daf166406a1b0e22f93b9417b90ed7d3aa1ae)

 sys/kern/imgact_elf.c | 91 +++++++++++++++++++++++++++++++++++----------------
 1 file changed, 62 insertions(+), 29 deletions(-)
Comment 10 Yuri Victorovich freebsd_committer freebsd_triage 2026-06-07 19:18:09 UTC
I updated the system to FreeBSD xx.xx.xx 15.1-STABLE FreeBSD 15.1-STABLE stable/15-n283852-ab599e44dba3 GENERIC amd64.

It still fails to run this binary but with a different error message now:
> $ ./copilot
> Unaligned program headers
> bash: ./copilot: cannot execute binary file: Exec format error

Here is the binary: https://freebsd.org/~yuri/copilot

Does this binary work for you on 15 and 16 ?
Comment 11 Konstantin Belousov freebsd_committer freebsd_triage 2026-06-08 01:29:01 UTC
(In reply to Yuri Victorovich from comment #10)
This is a different issue, the binary is non-compliant because phdrs must be aligned.

Still, the new handling for off-page phdrs make its possible to overcome the problematic
binaries.  https://reviews.freebsd.org/D57498 should make it.
Comment 12 commit-hook freebsd_committer freebsd_triage 2026-06-08 17:04:03 UTC
A commit in branch main references this bug:

URL: https://cgit.FreeBSD.org/src/commit/?id=0b269737f9ca057826a6c9376c2474b1ae5bc91c

commit 0b269737f9ca057826a6c9376c2474b1ae5bc91c
Author:     Konstantin Belousov <kib@FreeBSD.org>
AuthorDate: 2026-06-08 01:22:51 +0000
Commit:     Konstantin Belousov <kib@FreeBSD.org>
CommitDate: 2026-06-08 17:02:13 +0000

    imgact_elf: handle unaligned phdrs

    Althought non-compliant, there are binaries which have the phdrs placed
    unaligned in the image.  Since we have the code to allocate memory for
    off-page phdrs, the same code path can be used to handle unaligned
    phdrs.

    Relax the requirement for both the activated image and interpreter.

    PR:     295629
    Reviewed by:    emaste, markj, olce
    Sponsored by:   The FreeBSD Foundation
    MFC after:      1 week
    Differential revision:  https://reviews.freebsd.org/D57498

 sys/kern/imgact_elf.c | 13 +++++--------
 1 file changed, 5 insertions(+), 8 deletions(-)
Comment 13 commit-hook freebsd_committer freebsd_triage 2026-06-13 01:04:37 UTC
A commit in branch stable/15 references this bug:

URL: https://cgit.FreeBSD.org/src/commit/?id=093cc4194e6e88569bc25ecb73f6d887c5d69037

commit 093cc4194e6e88569bc25ecb73f6d887c5d69037
Author:     Konstantin Belousov <kib@FreeBSD.org>
AuthorDate: 2026-06-08 01:22:51 +0000
Commit:     Konstantin Belousov <kib@FreeBSD.org>
CommitDate: 2026-06-13 00:58:36 +0000

    imgact_elf: handle unaligned phdrs

    PR:     295629

    (cherry picked from commit 0b269737f9ca057826a6c9376c2474b1ae5bc91c)

 sys/kern/imgact_elf.c | 13 +++++--------
 1 file changed, 5 insertions(+), 8 deletions(-)
Comment 14 Ed Maste freebsd_committer freebsd_triage 2026-06-15 14:49:25 UTC
Yuri is this now solved with these two patches?
Comment 15 Yuri Victorovich freebsd_committer freebsd_triage 2026-06-17 16:39:31 UTC
(In reply to Ed Maste from comment #14)

I'll retest soon and will let you know.
Comment 16 Yuri Victorovich freebsd_committer freebsd_triage 2026-06-18 21:14:34 UTC
This binary runs now.

Thank you for fixing this problem!