Blame SOURCES/gdb-rhbz2018504-do-not-update-elf-headers.patch

958593
From FEDORA_PATCHES Mon Sep 17 00:00:00 2001
958593
From: Jozef Lawrynowicz <jozef.l@mittosystems.com>
958593
Date: Mon, 6 Dec 2021 12:56:59 -0500
958593
Subject: gdb-rhbz2018504-do-not-update-elf-headers.patch
958593
958593
;; Backport gdb/20948 (--write option to GDB causes segfault)
958593
;; (Jozef Lawrynowicz, RHBZ 2018504)
958593
958593
Fix PR gdb/20948: --write option to GDB causes segmentation fault
958593
958593
When opening a BFD for update, as gdb --write does, modifications to
958593
anything but the contents of sections is restricted.
958593
958593
Do not try to write back any ELF headers in this case.
958593
958593
diff --git a/bfd/elf.c b/bfd/elf.c
958593
--- a/bfd/elf.c
958593
+++ b/bfd/elf.c
958593
@@ -6418,6 +6418,18 @@ _bfd_elf_write_object_contents (bfd *abfd)
958593
   if (! abfd->output_has_begun
958593
       && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
958593
     return FALSE;
958593
+  /* Do not rewrite ELF data when the BFD has been opened for update.
958593
+     abfd->output_has_begun was set to TRUE on opening, so creation of new
958593
+     sections, and modification of existing section sizes was restricted.
958593
+     This means the ELF header, program headers and section headers can't have
958593
+     changed.
958593
+     If the contents of any sections has been modified, then those changes have
958593
+     already been written to the BFD.  */
958593
+  else if (abfd->direction == both_direction)
958593
+    {
958593
+      BFD_ASSERT (abfd->output_has_begun);
958593
+      return TRUE;
958593
+    }
958593
 
958593
   i_shdrp = elf_elfsections (abfd);
958593
 
958593
diff --git a/gdb/testsuite/gdb.base/write_mem.c b/gdb/testsuite/gdb.base/write_mem.c
958593
new file mode 100644
958593
--- /dev/null
958593
+++ b/gdb/testsuite/gdb.base/write_mem.c
958593
@@ -0,0 +1,20 @@
958593
+/* Copyright (C) 2018 Free Software Foundation, Inc.
958593
+
958593
+   This program is free software; you can redistribute it and/or modify
958593
+   it under the terms of the GNU General Public License as published by
958593
+   the Free Software Foundation; either version 3 of the License, or
958593
+   (at your option) any later version.
958593
+
958593
+   This program is distributed in the hope that it will be useful,
958593
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
958593
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
958593
+   GNU General Public License for more details.
958593
+
958593
+   You should have received a copy of the GNU General Public License
958593
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
958593
+
958593
+int main (void)
958593
+{
958593
+  while (1);
958593
+  return 0;
958593
+}
958593
diff --git a/gdb/testsuite/gdb.base/write_mem.exp b/gdb/testsuite/gdb.base/write_mem.exp
958593
new file mode 100644
958593
--- /dev/null
958593
+++ b/gdb/testsuite/gdb.base/write_mem.exp
958593
@@ -0,0 +1,47 @@
958593
+# Copyright (C) 2018 Free Software Foundation, Inc.
958593
+
958593
+# This program is free software; you can redistribute it and/or modify
958593
+# it under the terms of the GNU General Public License as published by
958593
+# the Free Software Foundation; either version 3 of the License, or
958593
+# (at your option) any later version.
958593
+#
958593
+# This program is distributed in the hope that it will be useful,
958593
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
958593
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
958593
+# GNU General Public License for more details.
958593
+#
958593
+# You should have received a copy of the GNU General Public License
958593
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
958593
+
958593
+# Contributed by Jozef Lawrynowicz (jozef.l@mittosystems.com)
958593
+
958593
+# Test for PR gdb/20948
958593
+# Verify that invoking gdb with the --write argument works as expected
958593
+
958593
+global GDBFLAGS
958593
+standard_testfile
958593
+
958593
+if {[build_executable $testfile.exp $testfile \
958593
+	$srcfile [list debug nowarnings] ] == -1} {
958593
+    untested $testfile.exp
958593
+    return -1
958593
+}
958593
+
958593
+set old_gdbflags $GDBFLAGS
958593
+
958593
+# Expect a failure before --write has been added to the command line
958593
+set GDBFLAGS "$old_gdbflags $binfile"
958593
+clean_restart
958593
+test_print_reject "set {int}main = 0x4242" "Cannot access memory at address"
958593
+
958593
+# Setting memory should now work correctly after adding --write
958593
+set GDBFLAGS "$old_gdbflags --write $binfile"
958593
+clean_restart
958593
+gdb_test_no_output "set {int}main = 0x4242"
958593
+
958593
+# Check that memory write persists after quitting GDB
958593
+gdb_exit
958593
+gdb_start
958593
+gdb_test "x /xh main" "<main>:.*4242"
958593
+
958593
+set GDBFLAGS $old_gdbflags