facebookarchive / BOLT

Binary Optimization and Layout Tool - A linux command-line utility used for optimizing performance of binaries
2.51k stars 176 forks source link

[Question] Section alignment less than 0x1000 cause program crashed #247

Closed getianao closed 2 years ago

getianao commented 2 years ago

Hi, I try to modify the alignment of the available address for new allocatable sections. An alignment less than 0x1000 will cause the program crash. Is it possible to set BC->PageAlign to a value less than 0x1000? Thanks a lot :) https://github.com/facebookincubator/BOLT/blob/61a2062542cd7f015cde0e0ff06abe305993943d/bolt/lib/Rewrite/RewriteInstance.cpp#L503

yavtuk commented 2 years ago

Hi @getianao, for x86 minimum page size is 4K, it's not possible to use less

getianao commented 2 years ago

Thanks for your reply. Seems a LOAD segment with an alignment less than 4K will make program crash becouse of x86 minimum page size. I modify the Phdr table and avoid adding new allocatable sections at the end of program, then the program works well.

Now I'm curious why BOLT choosing to place sections at the end of the binary, rather than the original place and modify the offsets of other sections. Because the former will leave lots of useless empty space and make binary larger.

yavtuk commented 2 years ago

@getianao one of the reason is described as comment https://github.com/facebookincubator/BOLT/blob/main/bolt/lib/Rewrite/RewriteInstance.cpp#L506

yavtuk commented 2 years ago

@getianao what sections are you talking about?

getianao commented 2 years ago

@yavtuk I notice .eh_frame_hdr and .eh_frame sections are added at the end. Also I want to know why not changing the offset when new .text section larger than the old .text section.

yavtuk commented 2 years ago

.cfi_startproc is used at the beginning of each function that should have an entry in .eh_frame. As we move new “.text” section to new segment due to larger size we also couldn’t use the old sections eh_frame & eh_frane_hdr and we should create new one with new size, but I am not sure actually

getianao commented 2 years ago

Thanks, it makes sense though.