https://sourceware.org/bugzilla/show_bug.cgi?id=17466
Bug ID: 17466 Summary: objcopy corrupts Windows executables when adding a section Product: binutils Version: 2.24 Status: NEW Severity: normal Priority: P2 Component: binutils Assignee: unassigned at sourceware dot org Reporter: gintautas.miliauskas at gmail dot com I've been trying to add some additional information to a Windows .exe file (compiled by mingw gcc) using objcopy, but whenever I add a new section using objcopy, the binary fails to execute. Here's a summary script: $ cd /tmp $ echo 'int main() { return 0; }' > c.c $ gcc -o c.exe c.c $ ./c.exe && echo ok ok $ echo x > x $ objcopy c.exe --add-section .debug_foo=x $ ./c.exe bash: ./c.exe: cannot execute binary file: Exec format error $ objdump -j c.exe 0 .debug_foo 00000002 0000000100000000 0000000100000000 00000458 2**0 CONTENTS, READONLY, DEBUGGING 1 .text 00001d00 0000000000401000 0000000000401000 00000800 2**4 CONTENTS, ALLOC, LOAD, READONLY, CODE 2 .data 000000a0 0000000000403000 0000000000403000 00002600 2**4 CONTENTS, ALLOC, LOAD, DATA 3 .rdata 00000970 0000000000404000 0000000000404000 00002800 2**4 CONTENTS, ALLOC, LOAD, READONLY, DATA 4 .pdata 00000234 0000000000405000 0000000000405000 00003200 2**2 CONTENTS, ALLOC, LOAD, READONLY, DATA 5 .xdata 000001f8 0000000000406000 0000000000406000 00003600 2**2 CONTENTS, ALLOC, LOAD, READONLY, DATA 6 .bss 00000a60 0000000000407000 0000000000407000 00000000 2**6 ALLOC 7 .idata 000007ac 0000000000408000 0000000000408000 00003800 2**2 CONTENTS, ALLOC, LOAD, DATA 8 .CRT 00000068 0000000000409000 0000000000409000 00004000 2**3 CONTENTS, ALLOC, LOAD, DATA 9 .tls 00000068 000000000040a000 000000000040a000 00004200 2**5 CONTENTS, ALLOC, LOAD, DATA 10 .debug_aranges 000003d0 000000000040b000 000000000040b000 00004400 2**0 CONTENTS, READONLY, DEBUGGING 11 .debug_info 0000b2d4 000000000040c000 000000000040c000 00004800 2**0 CONTENTS, READONLY, DEBUGGING 12 .debug_abbrev 000019cc 0000000000418000 0000000000418000 0000fc00 2**0 CONTENTS, READONLY, DEBUGGING 13 .debug_line 000017c0 000000000041a000 000000000041a000 00011600 2**0 CONTENTS, READONLY, DEBUGGING 14 .debug_frame 00000ac0 000000000041c000 000000000041c000 00012e00 2**3 CONTENTS, READONLY, DEBUGGING 15 .debug_str 00000300 000000000041d000 000000000041d000 00013a00 2**0 CONTENTS, READONLY, DEBUGGING 16 .debug_loc 00002e19 000000000041e000 000000000041e000 00013e00 2**0 CONTENTS, READONLY, DEBUGGING 17 .debug_ranges 00000520 0000000000421000 0000000000421000 00016e00 2**0 CONTENTS, READONLY, DEBUGGING Not sure why the section ends up first, but either way, if I run the binary through objcopy again, the new section is moved to the end: $ objcopy c.exe $ objdump -h c.exe c.exe: file format pei-x86-64 Sections: Idx Name Size VMA LMA File off Algn 0 .text 00001d00 0000000000401000 0000000000401000 00000600 2**4 CONTENTS, ALLOC, LOAD, READONLY, CODE 1 .data 000000a0 0000000000403000 0000000000403000 00002400 2**4 CONTENTS, ALLOC, LOAD, DATA 2 .rdata 00000970 0000000000404000 0000000000404000 00002600 2**4 CONTENTS, ALLOC, LOAD, READONLY, DATA 3 .pdata 00000234 0000000000405000 0000000000405000 00003000 2**2 CONTENTS, ALLOC, LOAD, READONLY, DATA 4 .xdata 000001f8 0000000000406000 0000000000406000 00003400 2**2 CONTENTS, ALLOC, LOAD, READONLY, DATA 5 .bss 00000a60 0000000000407000 0000000000407000 00000000 2**6 ALLOC 6 .idata 000007ac 0000000000408000 0000000000408000 00003600 2**2 CONTENTS, ALLOC, LOAD, DATA 7 .CRT 00000068 0000000000409000 0000000000409000 00003e00 2**3 CONTENTS, ALLOC, LOAD, DATA 8 .tls 00000068 000000000040a000 000000000040a000 00004000 2**5 CONTENTS, ALLOC, LOAD, DATA 9 .debug_aranges 000003d0 000000000040b000 000000000040b000 00004200 2**0 CONTENTS, READONLY, DEBUGGING 10 .debug_info 0000b2d4 000000000040c000 000000000040c000 00004600 2**0 CONTENTS, READONLY, DEBUGGING 11 .debug_abbrev 000019cc 0000000000418000 0000000000418000 0000fa00 2**0 CONTENTS, READONLY, DEBUGGING 12 .debug_line 000017c0 000000000041a000 000000000041a000 00011400 2**0 CONTENTS, READONLY, DEBUGGING 13 .debug_frame 00000ac0 000000000041c000 000000000041c000 00012c00 2**3 CONTENTS, READONLY, DEBUGGING 14 .debug_str 00000300 000000000041d000 000000000041d000 00013800 2**0 CONTENTS, READONLY, DEBUGGING 15 .debug_loc 00002e19 000000000041e000 000000000041e000 00013c00 2**0 CONTENTS, READONLY, DEBUGGING 16 .debug_ranges 00000520 0000000000421000 0000000000421000 00016c00 2**0 CONTENTS, READONLY, DEBUGGING 17 .debug_foo 00000002 0000000100000000 0000000100000000 00017200 2**0 CONTENTS, READONLY, DEBUGGING The corruption is not irreversible: if I remove the section using objcopy, the binary starts working again: $ objcopy --remove-section .debug_foo c.exe $ ./c.exe && echo ok ok What's going on here? Is this use case of objcopy intrinsically not supported? Should I be using something else? -- You are receiving this mail because: You are on the CC list for the bug. _______________________________________________ bug-binutils mailing list bug-binutils@gnu.org https://lists.gnu.org/mailman/listinfo/bug-binutils