Blame SOURCES/gdb-rhbz1931344-bfd_seek-elf_read_notes.patch

405ea9
From FEDORA_PATCHES Mon Sep 17 00:00:00 2001
405ea9
From: Keith Seitz <keiths@redhat.com>
405ea9
Date: Thu, 25 Mar 2021 10:31:48 -0700
405ea9
Subject: gdb-rhbz1931344-bfd_seek-elf_read_notes.patch
405ea9
405ea9
;; Backport "Save/restore file offset while reading notes in core file"
405ea9
;; (Keith Seitz, RHBZ 1931344)
405ea9
405ea9
A recent bug (RH BZ 1931344) has exposed a bug in the core file
405ea9
build-ID support that I introduced a while ago. It is pretty
405ea9
easy to demonstate the problem following a simplified procedure
405ea9
outlined in that bug:
405ea9
405ea9
[shell1]
405ea9
shell1$ /usr/libexec/qemu-kvm
405ea9
405ea9
[shell2]
405ea9
shell2$ pkill -SEGV -x qemu-kvm
405ea9
405ea9
[shell1]
405ea9
Segmentation fault (core dumped)
405ea9
405ea9
Load this core file into GDB without specifying an executable
405ea9
(an unfortunate Fedora/RHEL-ism), and GDB will inform the user
405ea9
to install debuginfo for the "missing" executable:
405ea9
405ea9
$ gdb -nx -q core.12345
405ea9
...
405ea9
Missing separate debuginfo for the main executable file
405ea9
Try: dnf --enablerepo='*debug*' install /usr/lib/debug/.build-id/e2/e9c66d3117fb2bbb5b2be122f04f2664e5df54
405ea9
Core was generated by `/usr/libexec/qemu-kvm'.
405ea9
Program terminated with signal SIGSEGV, Segmentation fault.
405ea9
...
405ea9
405ea9
The suggested build-ID is actaully for gmp not qemu-kvm. The problem
405ea9
lies in _bfd_elf_core_find_build_id, where we loop over program headers
405ea9
looking for note segments:
405ea9
405ea9
  /* Read in program headers and parse notes.  */
405ea9
  for (i = 0; i < i_ehdr.e_phnum; ++i, ++i_phdr)
405ea9
    {
405ea9
      Elf_External_Phdr x_phdr;
405ea9
405ea9
      if (bfd_bread (&x_phdr, sizeof (x_phdr), abfd) != sizeof (x_phdr))
405ea9
        goto fail;
405ea9
      elf_swap_phdr_in (abfd, &x_phdr, i_phdr);
405ea9
405ea9
      if (i_phdr->p_type == PT_NOTE && i_phdr->p_filesz > 0)
405ea9
        {
405ea9
          elf_read_notes (abfd, offset + i_phdr->p_offset,
405ea9
                          i_phdr->p_filesz, i_phdr->p_align);
405ea9
405ea9
          if (abfd->build_id != NULL)
405ea9
            return TRUE;
405ea9
        }
405ea9
405ea9
elf_read_notes uses bfd_seek to forward the stream to the location of
405ea9
the note segment. When control returns to _bfd_elf_core_fild_build_id,
405ea9
the stream is no longer in the location looking at program headers, and
405ea9
all subsequent reads will read from the wrong file offset.
405ea9
405ea9
To fix this, this patch marks the stream location and ensures
405ea9
that it is restored after elf_read_notes is called.
405ea9
405ea9
bfd/ChangeLog
405ea9
2021-03-26  Keith Seitz  <keiths@redhat.com>
405ea9
405ea9
	* elfcore.h (_bfd_elf_core_find_build_id): Seek file
405ea9
	offset of program headers after calling elf_read_notes.
405ea9
405ea9
diff --git a/bfd/elfcore.h b/bfd/elfcore.h
405ea9
--- a/bfd/elfcore.h
405ea9
+++ b/bfd/elfcore.h
405ea9
@@ -410,6 +410,13 @@ NAME(_bfd_elf, core_find_build_id)
405ea9
 	{
405ea9
 	  elf_read_notes (abfd, offset + i_phdr->p_offset,
405ea9
 			  i_phdr->p_filesz, i_phdr->p_align);
405ea9
+
405ea9
+	  /* Make sure ABFD returns to processing the program headers.  */
405ea9
+	  if (bfd_seek (abfd, (file_ptr) (offset + i_ehdr.e_phoff
405ea9
+					  + (i + 1) * sizeof (x_phdr)),
405ea9
+			SEEK_SET) != 0)
405ea9
+	    goto fail;
405ea9
+
405ea9
 	  if (abfd->build_id != NULL)
405ea9
 	    return TRUE;
405ea9
 	}