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

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