On Wed, Feb 15, 2023 at 7:48 AM Stuart Henderson <s...@spacehopper.org>
wrote:

> Noticed while testing with LLVM 15, but it affects plain -current
> as well. If I take a binary that was linked with ld.bfd and strip it
> (i.e. this is now using llvm-strip), it breaks the output file in
> such a way that it cannot be executed:
>
...

> Seems that the SIGABRT is from kern_exec's sys_execve ->
> exec_process_vmcmds() failing.
>
...

> Does anyone have an idea what might be wrong please?
>

llvm-strip is somehow ignoring the alignment requirements of the segments.
If you look at the "readelf -l" output instead:

Good:
Program Headers:
  Type           Offset   VirtAddr   PhysAddr   FileSiz MemSiz  Flg Align
  PHDR           0x000034 0x00000034 0x00000034 0x001e0 0x001e0 R E 0x4
  INTERP         0x001000 0x20000000 0x20000000 0x00013 0x00013 R   0x1
      [Requesting program interpreter: /usr/libexec/ld.so]
  LOAD           0x000000 0x00000000 0x00000000 0x0058d 0x0058d R E 0x1000
  LOAD           0x001000 0x20000000 0x20000000 0x003e8 0x003e8 R   0x1000
...

Note: offset == virtaddr mod alignment


Bad:
Program Headers:
  Type           Offset   VirtAddr   PhysAddr   FileSiz MemSiz  Flg Align
  PHDR           0x000034 0x00000034 0x00000034 0x001e0 0x001e0 R E 0x4
  INTERP         0x00058d 0x20000000 0x20000000 0x00013 0x00013 R   0x1
      [Requesting program interpreter: /usr/libexec/ld.so]
  LOAD           0x000000 0x00000000 0x00000000 0x0058d 0x0058d R E 0x1000
  LOAD           0x00058d 0x20000000 0x20000000 0x003e8 0x003e8 R   0x1000

Boom, that second LOAD does not have offset == virtaddr mod alignment.

Now, the sections that go into that segment have a max alignment of 4 and
llvm-strip's changes abides by that, but *IF* it's not going to keep the
segments page-aligned then it should be adjusting the virtaddr field of the
LOAD segment to keep the offset aligned with the virtaddr (and adjusting
the sizes so the LOAD continues to cover the total data).

Or it shouldn't be screwing with the packing like that.


Philip Guenther

Reply via email to