Bug 18168 - "set write on" or "--write" corrupt the binary file - 7.9 regression
Summary: "set write on" or "--write" corrupt the binary file - 7.9 regression
Status: RESOLVED FIXED
Alias: None
Product: gdb
Classification: Unclassified
Component: gdb (show other bugs)
Version: HEAD
: P2 critical
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2015-03-26 16:48 UTC by Krzysztof Majzerowicz-Jaszcz
Modified: 2022-05-13 10:08 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Last reconfirmed:


Attachments
the hexdumps of a binary file before and after executing commands in the description (10.70 KB, application/x-xz)
2015-03-26 16:48 UTC, Krzysztof Majzerowicz-Jaszcz
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Krzysztof Majzerowicz-Jaszcz 2015-03-26 16:48:32 UTC
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
Comment 1 Krzysztof Majzerowicz-Jaszcz 2015-03-26 16:52:56 UTC
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)
Comment 2 Krzysztof Majzerowicz-Jaszcz 2015-03-26 18:10:00 UTC
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"
Comment 3 Jan Kratochvil 2015-07-26 20:34:07 UTC
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
Comment 4 Pedro Alves 2022-05-13 10:08:07 UTC
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.