Skip to content

Stripping BOLTed binaries may result in misaligned PT_LOADs #56738

Open

Description

Consider the following simple main.cc file:

int main() {}

Running:

$ clang++ main.cc -o main -Wl,-q
$ llvm-bolt main -o main.bolted
$ llvm-strip -S main.bolted -o main.bolted.stripped

Results in a misaligned PT_LOAD:

  Type           Offset   VirtAddr           PhysAddr           FileSiz  MemSiz   Flg Align
...
  LOAD           0x003028 0x0000000000200000 0x0000000000200000 0x200384 0x200384 R E 0x200000

which leads to program crashes at startup:

$ ./main.bolted.stripped
[1]    153575 segmentation fault  ./main.bolted.stripped
$ /lib64/ld-linux-x86-64.so.2 ./main.bolted.stripped
./main.bolted.stripped: error while loading shared libraries: ./main.bolted.stripped: ELF load command address/offset not properly aligned

This is because the new PT_PHDR header was placed at the same offset as the new PT_LOAD containing the modified .text section:

$ llvm-readelf -l -h main.bolted
...
  Type           Offset   VirtAddr           PhysAddr           FileSiz  MemSiz   Flg Align
  PHDR           0x200000 0x0000000000200000 0x0000000000200000 0x000310 0x000310 R   0x8
...
  LOAD           0x200000 0x0000000000200000 0x0000000000200000 0x200384 0x200384 R E 0x200000

Which confuses llvm-strip, as it thinks that the PT_LOAD is a child of the PT_PHDR and thus it will disregard the alignment requirements of the (alleged) child.

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions