-
Notifications
You must be signed in to change notification settings - Fork 173
Description
When saving a file loaded with ELFIO, without any modifications, the segment definition can change.
I added a test in MofX@0ac9c67, that shows the behavior and could be added as a regression test, after fixing the bug.
It includes a binary generated with echo "int main(){}" | gcc -xc -static -o x86_64_static -
, that is loaded with elfio and saved again. In the initial version the segments are compared to the output of readelf -l
and for the saved one the same is done, but fails, because elfio changes four fields of the segment Definition:
- The file size and memory size for segment 6 is changed from 0x20/0x20 to 0x40/0x80
- The file size for segment 9 is changed from 0x2f40 to 0x2f28 and the alignment is changed from 0x1 to 0x20
At least the change in segment 6, which is the the TLS segment breaks the binary, because it changes the memory layout of the TLS, so that the libc initialization code uses an invalid pointer and segfaults.
I think the changed sizes are due to how elfio tries to map sections to segments. Instead of just mapping .tdata and .tbss to the TLS segment, it also maps .init_array and .finit_array to the TLS segment. The combined file size of these sections is 0x80. readelf -l
shows a correct mapping. I imagine, the same is happening for segment 9.
I did not look into why the alignment of segment 9 is changed.