bfd/PE: fix bigobj file corruption on objcopy
Fixes corruption in PE bigobj files created by objcopy.
Tobias Deiminger addresses a bug where converting to pe-bigobj-* formats using objcopy silently corrupted section headers. The issue stemmed from incorrect handling of f_opthdr, _bfd_aoutsz, and EXEC_P flags, leading to invalid gaps in the file. The fix ensures _bfd_aoutsz is zero and EXEC_P is unset for bigobj formats, accepting minor information loss for format correctness.
- proposer
Fixes PE bigobj file corruption issues in objcopy by correcting handling of optional headers and section sizes.
“Converting any linked executable to pe-bigobj-* silently produced corrupted section headers. For example: $ gcc -g -o testprog testprog.c $ objcopy -I elf64-x86-64 -O pe-bigobj-x86-64 testprog testprog.bigobj $ objdump -b pe-bigobj-x86-64 -h testprog.bigobj | head [...] Idx Name Size VMA LMA File off Algn 0 ^K^B^B/ 00001050 00000000000000…”
In Details
This patch corrects a critical bug in BFD's PE backend where objcopy would silently corrupt pe-bigobj-* files. The corruption occurred because the format's inability to store an OptionalHeader was not properly enforced, leading to incorrect section header offsets due to miscalculated _bfd_aoutsz and the presence of the EXEC_P flag. The fix ensures these fields are correctly set to zero or unset, preventing invalid output at the cost of some minor data loss inherent to the format's limitations.
- pe-bigobj-*
- A variant of the PE (Portable Executable) file format that supports larger section headers, allowing for executables with more sections than the standard PE format.
- objcopy
- A utility in GNU Binutils used to copy and translate object files, allowing modification of sections and symbols.
- section headers
- Metadata within an object file that describes each section, including its name, size, virtual address, and file offset.
- OptionalHeader
- A component of the PE file format that contains information about the file, such as entry point, base address, and sizes of code and data sections. Not present in
pe-bigobjformat. - _bfd_aoutsz
- An internal BFD structure field likely related to the size of the a.out header, which is incorrectly initialized and used in PE bigobj conversions.
- EXEC_P
- A flag or attribute indicating that a file is executable. Its incorrect presence in
pe-bigobjcontexts triggers logic that assumes an OptionalHeader.