Created attachment 8213 [details] the hexdumps of a binary file before and after executing commands in the description When enabling binary patching via "set write on" command or "--write" command line switch, the exectuable loaded becomes corrupted. Try executing: $ gdb --write a_binary_file (gdb)quit # ./a_binary_file bash: ./a_binary_file: cannot execute binary file: Exec format error Careful examination of the binary shows this: # file ./a_binary_file a_binary_file: data # readelf -a ./a_binary_file readelf: Error: Not an ELF file - it has the wrong magic bytes at the start Attached are the hexdumps of the files before and after executing "gdb --write" and quitting gdb immediately
The bug exists on the latest HEAD GNU gdb (GDB) 7.9.50.20150326-cvs My environment: $ uname -a Linux tesla 3.19.2-1-ARCH #1 SMP PREEMPT Wed Mar 18 16:21:02 CET 2015 x86_64 GNU/Linux $ gcc -v Using built-in specs. COLLECT_GCC=gcc COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-unknown-linux-gnu/4.9.2/lto-wrapper Target: x86_64-unknown-linux-gnu Configured with: /build/gcc-multilib/src/gcc-4.9-20150304/configure --prefix=/usr --libdir=/usr/lib --libexecdir=/usr/lib --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=https://bugs.archlinux.org/ --enable-languages=c,c++,ada,fortran,go,lto,objc,obj-c++ --enable-shared --enable-threads=posix --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-clocale=gnu --disable-libstdcxx-pch --disable-libssp --enable-gnu-unique-object --enable-linker-build-id --enable-cloog-backend=isl --enable-lto --enable-plugin --enable-install-libiberty --with-linker-hash-style=gnu --enable-multilib --disable-werror --enable-checking=release Thread model: posix gcc version 4.9.2 20150304 (prerelease) (GCC)
I can confirm that the bug does NOT exist on CentOS 7 - GDB version "GNU gdb (GDB) Red Hat Enterprise Linux 7.6.1-51.el7"
a485e98ea0cbb61ea9da1e7858da545e0bcf1a46 is the first bad commit commit a485e98ea0cbb61ea9da1e7858da545e0bcf1a46 Author: Alan Modra <amodra@gmail.com> Date: Wed Sep 10 14:26:25 2014 +0930 Move ELF section headers to end of object file present first in gdb-7.9-branch
This has been fixed since, probably by: commit db72737006fc383cb8838bf7f3dc8e641e60c38f Author: Jozef Lawrynowicz <jozef.l@mittosystems.com> AuthorDate: Tue Sep 11 22:56:36 2018 +0100 Commit: Tom Tromey <tom@tromey.com> CommitDate: Mon Sep 24 06:20:17 2018 -0600 Fix PR gdb/20948: --write option to GDB causes segmentation fault This commit adds a testcase that would fail if GDB still corrupted the binary: commit 169692ce6c0fa21c4648d2862cb2bb94012a1cd9 Author: Pedro Alves <pedro@palves.net> AuthorDate: Wed May 11 14:20:15 2022 +0100 Commit: Pedro Alves <pedro@palves.net> CommitDate: Fri May 13 10:56:05 2022 +0100 Fix "gdb --write" with core files Closing.