Open
Description
openedon Jul 26, 2022
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.strippedResults in a misaligned PT_LOAD:
Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align
...
LOAD 0x003028 0x0000000000200000 0x0000000000200000 0x200384 0x200384 R E 0x200000which 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 alignedThis 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 0x200000Which 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.