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

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