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

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