Blame SOURCES/gdb-6.6-buildid-locate.patch

ed07ac
From FEDORA_PATCHES Mon Sep 17 00:00:00 2001
ed07ac
From: Fedora GDB patches <invalid@email.com>
ed07ac
Date: Fri, 27 Oct 2017 21:07:50 +0200
ed07ac
Subject: gdb-6.6-buildid-locate.patch
ed07ac
ed07ac
;; New locating of the matching binaries from the pure core file (build-id).
ed07ac
;;=push+jan
ed07ac
ed07ac
diff --git a/bfd/libbfd-in.h b/bfd/libbfd-in.h
ed07ac
--- a/bfd/libbfd-in.h
ed07ac
+++ b/bfd/libbfd-in.h
ed07ac
@@ -115,7 +115,7 @@ static inline char *
ed07ac
 bfd_strdup (const char *str)
ed07ac
 {
ed07ac
   size_t len = strlen (str) + 1;
ed07ac
-  char *buf = bfd_malloc (len);
ed07ac
+  char *buf = (char *) bfd_malloc (len);
ed07ac
   if (buf != NULL)
ed07ac
     memcpy (buf, str, len);
ed07ac
   return buf;
ed07ac
diff --git a/bfd/libbfd.h b/bfd/libbfd.h
ed07ac
--- a/bfd/libbfd.h
ed07ac
+++ b/bfd/libbfd.h
ed07ac
@@ -120,7 +120,7 @@ static inline char *
ed07ac
 bfd_strdup (const char *str)
ed07ac
 {
ed07ac
   size_t len = strlen (str) + 1;
ed07ac
-  char *buf = bfd_malloc (len);
ed07ac
+  char *buf = (char *) bfd_malloc (len);
ed07ac
   if (buf != NULL)
ed07ac
     memcpy (buf, str, len);
ed07ac
   return buf;
ed07ac
diff --git a/gdb/build-id.c b/gdb/build-id.c
ed07ac
--- a/gdb/build-id.c
ed07ac
+++ b/gdb/build-id.c
ed07ac
@@ -24,13 +24,70 @@
ed07ac
 #include "gdbsupport/gdb_vecs.h"
ed07ac
 #include "symfile.h"
ed07ac
 #include "objfiles.h"
ed07ac
+#include <sys/stat.h>
ed07ac
+#include "elf-bfd.h"
ed07ac
+#include "elf/common.h"
ed07ac
+#include "elf/external.h"
ed07ac
+#include "elf/internal.h"
ed07ac
 #include "filenames.h"
ed07ac
+#include "gdb_bfd.h"
ed07ac
+#include "gdbcmd.h"
ed07ac
 #include "gdbcore.h"
ed07ac
+#include "objfiles.h"
ed07ac
+#include "observable.h"
ed07ac
+#include "symfile.h"
ed07ac
+
ed07ac
+#define BUILD_ID_VERBOSE_NONE 0
ed07ac
+#define BUILD_ID_VERBOSE_FILENAMES 1
ed07ac
+#define BUILD_ID_VERBOSE_BINARY_PARSE 2
ed07ac
+static int build_id_verbose = BUILD_ID_VERBOSE_FILENAMES;
ed07ac
+static void
ed07ac
+show_build_id_verbose (struct ui_file *file, int from_tty,
ed07ac
+		       struct cmd_list_element *c, const char *value)
ed07ac
+{
ed07ac
+  fprintf_filtered (file, _("Verbosity level of the build-id locator is %s.\n"),
ed07ac
+		    value);
ed07ac
+}
ed07ac
+/* Locate NT_GNU_BUILD_ID and return its matching debug filename.
ed07ac
+   FIXME: NOTE decoding should be unified with the BFD core notes decoding.  */
ed07ac
+
ed07ac
+static struct bfd_build_id *
ed07ac
+build_id_buf_get (bfd *templ, gdb_byte *buf, bfd_size_type size)
ed07ac
+{
ed07ac
+  bfd_byte *p;
ed07ac
+
ed07ac
+  p = buf;
ed07ac
+  while (p < buf + size)
ed07ac
+    {
ed07ac
+      /* FIXME: bad alignment assumption.  */
ed07ac
+      Elf_External_Note *xnp = (Elf_External_Note *) p;
ed07ac
+      size_t namesz = H_GET_32 (templ, xnp->namesz);
ed07ac
+      size_t descsz = H_GET_32 (templ, xnp->descsz);
ed07ac
+      bfd_byte *descdata = (gdb_byte *) xnp->name + BFD_ALIGN (namesz, 4);
ed07ac
+
ed07ac
+      if (H_GET_32 (templ, xnp->type) == NT_GNU_BUILD_ID
ed07ac
+	  && namesz == sizeof "GNU"
ed07ac
+	  && memcmp (xnp->name, "GNU", sizeof "GNU") == 0)
ed07ac
+	{
ed07ac
+	  size_t sz = descsz;
ed07ac
+	  gdb_byte *data = (gdb_byte *) descdata;
ed07ac
+	  struct bfd_build_id *retval;
ed07ac
+
ed07ac
+	  retval = (struct bfd_build_id *) xmalloc (sizeof *retval - 1 + sz);
ed07ac
+	  retval->size = sz;
ed07ac
+	  memcpy (retval->data, data, sz);
ed07ac
+
ed07ac
+	  return retval;
ed07ac
+	}
ed07ac
+      p = descdata + BFD_ALIGN (descsz, 4);
ed07ac
+    }
ed07ac
+  return NULL;
ed07ac
+}
ed07ac
 
ed07ac
 /* See build-id.h.  */
ed07ac
 
ed07ac
 const struct bfd_build_id *
ed07ac
-build_id_bfd_get (bfd *abfd)
ed07ac
+build_id_bfd_shdr_get (bfd *abfd)
ed07ac
 {
ed07ac
   if (!bfd_check_format (abfd, bfd_object)
ed07ac
       && !bfd_check_format (abfd, bfd_core))
ed07ac
@@ -43,6 +100,348 @@ build_id_bfd_get (bfd *abfd)
ed07ac
   return NULL;
ed07ac
 }
ed07ac
 
ed07ac
+/* Core files may have missing (corrupt) SHDR but PDHR is correct there.
ed07ac
+   bfd_elf_bfd_from_remote_memory () has too much overhead by
ed07ac
+   allocating/reading all the available ELF PT_LOADs.  */
ed07ac
+
ed07ac
+static struct bfd_build_id *
ed07ac
+build_id_phdr_get (bfd *templ, bfd_vma loadbase, unsigned e_phnum,
ed07ac
+		   Elf_Internal_Phdr *i_phdr)
ed07ac
+{
ed07ac
+  int i;
ed07ac
+  struct bfd_build_id *retval = NULL;
ed07ac
+
ed07ac
+  for (i = 0; i < e_phnum; i++)
ed07ac
+    if (i_phdr[i].p_type == PT_NOTE && i_phdr[i].p_filesz > 0)
ed07ac
+      {
ed07ac
+	Elf_Internal_Phdr *hdr = &i_phdr[i];
ed07ac
+	gdb_byte *buf;
ed07ac
+	int err;
ed07ac
+
ed07ac
+	buf = (gdb_byte *) xmalloc (hdr->p_filesz);
ed07ac
+	err = target_read_memory (loadbase + i_phdr[i].p_vaddr, buf,
ed07ac
+				  hdr->p_filesz);
ed07ac
+	if (err == 0)
ed07ac
+	  retval = build_id_buf_get (templ, buf, hdr->p_filesz);
ed07ac
+	else
ed07ac
+	  retval = NULL;
ed07ac
+	xfree (buf);
ed07ac
+	if (retval != NULL)
ed07ac
+	  break;
ed07ac
+      }
ed07ac
+  return retval;
ed07ac
+}
ed07ac
+
ed07ac
+/* First we validate the file by reading in the ELF header and checking
ed07ac
+   the magic number.  */
ed07ac
+
ed07ac
+static inline bfd_boolean
ed07ac
+elf_file_p (Elf64_External_Ehdr *x_ehdrp64)
ed07ac
+{
ed07ac
+  gdb_assert (sizeof (Elf64_External_Ehdr) >= sizeof (Elf32_External_Ehdr));
ed07ac
+  gdb_assert (offsetof (Elf64_External_Ehdr, e_ident)
ed07ac
+	      == offsetof (Elf32_External_Ehdr, e_ident));
ed07ac
+  gdb_assert (sizeof (((Elf64_External_Ehdr *) 0)->e_ident)
ed07ac
+	      == sizeof (((Elf32_External_Ehdr *) 0)->e_ident));
ed07ac
+
ed07ac
+  return ((x_ehdrp64->e_ident[EI_MAG0] == ELFMAG0)
ed07ac
+	  && (x_ehdrp64->e_ident[EI_MAG1] == ELFMAG1)
ed07ac
+	  && (x_ehdrp64->e_ident[EI_MAG2] == ELFMAG2)
ed07ac
+	  && (x_ehdrp64->e_ident[EI_MAG3] == ELFMAG3));
ed07ac
+}
ed07ac
+
ed07ac
+/* Translate an ELF file header in external format into an ELF file header in
ed07ac
+   internal format.  */
ed07ac
+
ed07ac
+#define H_GET_WORD(bfd, ptr) (is64 ? H_GET_64 (bfd, (ptr))		\
ed07ac
+				   : H_GET_32 (bfd, (ptr)))
ed07ac
+#define H_GET_SIGNED_WORD(bfd, ptr) (is64 ? H_GET_S64 (bfd, (ptr))	\
ed07ac
+					  : H_GET_S32 (bfd, (ptr)))
ed07ac
+
ed07ac
+static void
ed07ac
+elf_swap_ehdr_in (bfd *abfd,
ed07ac
+		  const Elf64_External_Ehdr *src64,
ed07ac
+		  Elf_Internal_Ehdr *dst)
ed07ac
+{
ed07ac
+  int is64 = bfd_get_arch_size (abfd) == 64;
ed07ac
+#define SRC(field) (is64 ? src64->field \
ed07ac
+			 : ((const Elf32_External_Ehdr *) src64)->field)
ed07ac
+
ed07ac
+  int signed_vma = get_elf_backend_data (abfd)->sign_extend_vma;
ed07ac
+  memcpy (dst->e_ident, SRC (e_ident), EI_NIDENT);
ed07ac
+  dst->e_type = H_GET_16 (abfd, SRC (e_type));
ed07ac
+  dst->e_machine = H_GET_16 (abfd, SRC (e_machine));
ed07ac
+  dst->e_version = H_GET_32 (abfd, SRC (e_version));
ed07ac
+  if (signed_vma)
ed07ac
+    dst->e_entry = H_GET_SIGNED_WORD (abfd, SRC (e_entry));
ed07ac
+  else
ed07ac
+    dst->e_entry = H_GET_WORD (abfd, SRC (e_entry));
ed07ac
+  dst->e_phoff = H_GET_WORD (abfd, SRC (e_phoff));
ed07ac
+  dst->e_shoff = H_GET_WORD (abfd, SRC (e_shoff));
ed07ac
+  dst->e_flags = H_GET_32 (abfd, SRC (e_flags));
ed07ac
+  dst->e_ehsize = H_GET_16 (abfd, SRC (e_ehsize));
ed07ac
+  dst->e_phentsize = H_GET_16 (abfd, SRC (e_phentsize));
ed07ac
+  dst->e_phnum = H_GET_16 (abfd, SRC (e_phnum));
ed07ac
+  dst->e_shentsize = H_GET_16 (abfd, SRC (e_shentsize));
ed07ac
+  dst->e_shnum = H_GET_16 (abfd, SRC (e_shnum));
ed07ac
+  dst->e_shstrndx = H_GET_16 (abfd, SRC (e_shstrndx));
ed07ac
+
ed07ac
+#undef SRC
ed07ac
+}
ed07ac
+
ed07ac
+/* Translate an ELF program header table entry in external format into an
ed07ac
+   ELF program header table entry in internal format.  */
ed07ac
+
ed07ac
+static void
ed07ac
+elf_swap_phdr_in (bfd *abfd,
ed07ac
+		  const Elf64_External_Phdr *src64,
ed07ac
+		  Elf_Internal_Phdr *dst)
ed07ac
+{
ed07ac
+  int is64 = bfd_get_arch_size (abfd) == 64;
ed07ac
+#define SRC(field) (is64 ? src64->field					\
ed07ac
+			 : ((const Elf32_External_Phdr *) src64)->field)
ed07ac
+
ed07ac
+  int signed_vma = get_elf_backend_data (abfd)->sign_extend_vma;
ed07ac
+
ed07ac
+  dst->p_type = H_GET_32 (abfd, SRC (p_type));
ed07ac
+  dst->p_flags = H_GET_32 (abfd, SRC (p_flags));
ed07ac
+  dst->p_offset = H_GET_WORD (abfd, SRC (p_offset));
ed07ac
+  if (signed_vma)
ed07ac
+    {
ed07ac
+      dst->p_vaddr = H_GET_SIGNED_WORD (abfd, SRC (p_vaddr));
ed07ac
+      dst->p_paddr = H_GET_SIGNED_WORD (abfd, SRC (p_paddr));
ed07ac
+    }
ed07ac
+  else
ed07ac
+    {
ed07ac
+      dst->p_vaddr = H_GET_WORD (abfd, SRC (p_vaddr));
ed07ac
+      dst->p_paddr = H_GET_WORD (abfd, SRC (p_paddr));
ed07ac
+    }
ed07ac
+  dst->p_filesz = H_GET_WORD (abfd, SRC (p_filesz));
ed07ac
+  dst->p_memsz = H_GET_WORD (abfd, SRC (p_memsz));
ed07ac
+  dst->p_align = H_GET_WORD (abfd, SRC (p_align));
ed07ac
+
ed07ac
+#undef SRC
ed07ac
+}
ed07ac
+
ed07ac
+#undef H_GET_SIGNED_WORD
ed07ac
+#undef H_GET_WORD
ed07ac
+
ed07ac
+static Elf_Internal_Phdr *
ed07ac
+elf_get_phdr (bfd *templ, bfd_vma ehdr_vma, unsigned *e_phnum_pointer,
ed07ac
+              bfd_vma *loadbase_pointer)
ed07ac
+{
ed07ac
+  /* sizeof (Elf64_External_Ehdr) >= sizeof (Elf32_External_Ehdr)  */
ed07ac
+  Elf64_External_Ehdr x_ehdr64;	/* Elf file header, external form */
ed07ac
+  Elf_Internal_Ehdr i_ehdr;	/* Elf file header, internal form */
ed07ac
+  bfd_size_type x_phdrs_size;
ed07ac
+  gdb_byte *x_phdrs_ptr;
ed07ac
+  Elf_Internal_Phdr *i_phdrs;
ed07ac
+  int err;
ed07ac
+  unsigned int i;
ed07ac
+  bfd_vma loadbase;
ed07ac
+  int loadbase_set;
ed07ac
+
ed07ac
+  gdb_assert (templ != NULL);
ed07ac
+  gdb_assert (sizeof (Elf64_External_Ehdr) >= sizeof (Elf32_External_Ehdr));
ed07ac
+
ed07ac
+  /* Read in the ELF header in external format.  */
ed07ac
+  err = target_read_memory (ehdr_vma, (bfd_byte *) &x_ehdr64, sizeof x_ehdr64);
ed07ac
+  if (err)
ed07ac
+    {
ed07ac
+      if (build_id_verbose >= BUILD_ID_VERBOSE_BINARY_PARSE)
ed07ac
+        warning (_("build-id: Error reading ELF header at address 0x%lx"),
ed07ac
+		 (unsigned long) ehdr_vma);
ed07ac
+      return NULL;
ed07ac
+    }
ed07ac
+
ed07ac
+  /* Now check to see if we have a valid ELF file, and one that BFD can
ed07ac
+     make use of.  The magic number must match, the address size ('class')
ed07ac
+     and byte-swapping must match our XVEC entry.  */
ed07ac
+
ed07ac
+  if (! elf_file_p (&x_ehdr64)
ed07ac
+      || x_ehdr64.e_ident[EI_VERSION] != EV_CURRENT
ed07ac
+      || !((bfd_get_arch_size (templ) == 64
ed07ac
+            && x_ehdr64.e_ident[EI_CLASS] == ELFCLASS64)
ed07ac
+           || (bfd_get_arch_size (templ) == 32
ed07ac
+	       && x_ehdr64.e_ident[EI_CLASS] == ELFCLASS32)))
ed07ac
+    {
ed07ac
+      if (build_id_verbose >= BUILD_ID_VERBOSE_BINARY_PARSE)
ed07ac
+        warning (_("build-id: Unrecognized ELF header at address 0x%lx"),
ed07ac
+		 (unsigned long) ehdr_vma);
ed07ac
+      return NULL;
ed07ac
+    }
ed07ac
+
ed07ac
+  /* Check that file's byte order matches xvec's */
ed07ac
+  switch (x_ehdr64.e_ident[EI_DATA])
ed07ac
+    {
ed07ac
+    case ELFDATA2MSB:		/* Big-endian */
ed07ac
+      if (! bfd_header_big_endian (templ))
ed07ac
+	{
ed07ac
+	  if (build_id_verbose >= BUILD_ID_VERBOSE_BINARY_PARSE)
ed07ac
+	    warning (_("build-id: Unrecognized "
ed07ac
+		       "big-endian ELF header at address 0x%lx"),
ed07ac
+		     (unsigned long) ehdr_vma);
ed07ac
+	  return NULL;
ed07ac
+	}
ed07ac
+      break;
ed07ac
+    case ELFDATA2LSB:		/* Little-endian */
ed07ac
+      if (! bfd_header_little_endian (templ))
ed07ac
+	{
ed07ac
+	  if (build_id_verbose >= BUILD_ID_VERBOSE_BINARY_PARSE)
ed07ac
+	    warning (_("build-id: Unrecognized "
ed07ac
+		       "little-endian ELF header at address 0x%lx"),
ed07ac
+		     (unsigned long) ehdr_vma);
ed07ac
+	  return NULL;
ed07ac
+	}
ed07ac
+      break;
ed07ac
+    case ELFDATANONE:		/* No data encoding specified */
ed07ac
+    default:			/* Unknown data encoding specified */
ed07ac
+      if (build_id_verbose >= BUILD_ID_VERBOSE_BINARY_PARSE)
ed07ac
+	warning (_("build-id: Unrecognized "
ed07ac
+		   "ELF header endianity at address 0x%lx"),
ed07ac
+		 (unsigned long) ehdr_vma);
ed07ac
+      return NULL;
ed07ac
+    }
ed07ac
+
ed07ac
+  elf_swap_ehdr_in (templ, &x_ehdr64, &i_ehdr);
ed07ac
+
ed07ac
+  /* The file header tells where to find the program headers.
ed07ac
+     These are what we use to actually choose what to read.  */
ed07ac
+
ed07ac
+  if (i_ehdr.e_phentsize != (bfd_get_arch_size (templ) == 64
ed07ac
+                             ? sizeof (Elf64_External_Phdr)
ed07ac
+			     : sizeof (Elf32_External_Phdr))
ed07ac
+      || i_ehdr.e_phnum == 0)
ed07ac
+    {
ed07ac
+      if (build_id_verbose >= BUILD_ID_VERBOSE_BINARY_PARSE)
ed07ac
+	warning (_("build-id: Invalid ELF program headers from the ELF header "
ed07ac
+		   "at address 0x%lx"), (unsigned long) ehdr_vma);
ed07ac
+      return NULL;
ed07ac
+    }
ed07ac
+
ed07ac
+  x_phdrs_size = (bfd_get_arch_size (templ) == 64 ? sizeof (Elf64_External_Phdr)
ed07ac
+						: sizeof (Elf32_External_Phdr));
ed07ac
+
ed07ac
+  i_phdrs = (Elf_Internal_Phdr *) xmalloc (i_ehdr.e_phnum * (sizeof *i_phdrs + x_phdrs_size));
ed07ac
+  x_phdrs_ptr = (gdb_byte *) &i_phdrs[i_ehdr.e_phnum];
ed07ac
+  err = target_read_memory (ehdr_vma + i_ehdr.e_phoff, (bfd_byte *) x_phdrs_ptr,
ed07ac
+			    i_ehdr.e_phnum * x_phdrs_size);
ed07ac
+  if (err)
ed07ac
+    {
ed07ac
+      free (i_phdrs);
ed07ac
+      if (build_id_verbose >= BUILD_ID_VERBOSE_BINARY_PARSE)
ed07ac
+        warning (_("build-id: Error reading "
ed07ac
+		   "ELF program headers at address 0x%lx"),
ed07ac
+		 (unsigned long) (ehdr_vma + i_ehdr.e_phoff));
ed07ac
+      return NULL;
ed07ac
+    }
ed07ac
+
ed07ac
+  loadbase = ehdr_vma;
ed07ac
+  loadbase_set = 0;
ed07ac
+  for (i = 0; i < i_ehdr.e_phnum; ++i)
ed07ac
+    {
ed07ac
+      elf_swap_phdr_in (templ, (Elf64_External_Phdr *)
ed07ac
+			       (x_phdrs_ptr + i * x_phdrs_size), &i_phdrs[i]);
ed07ac
+      /* IA-64 vDSO may have two mappings for one segment, where one mapping
ed07ac
+	 is executable only, and one is read only.  We must not use the
ed07ac
+	 executable one (PF_R is the first one, PF_X the second one).  */
ed07ac
+      if (i_phdrs[i].p_type == PT_LOAD && (i_phdrs[i].p_flags & PF_R))
ed07ac
+	{
ed07ac
+	  /* Only the first PT_LOAD segment indicates the file bias.
ed07ac
+	     Next segments may have P_VADDR arbitrarily higher.
ed07ac
+	     If the first segment has P_VADDR zero any next segment must not
ed07ac
+	     confuse us, the first one sets LOADBASE certainly enough.  */
ed07ac
+	  if (!loadbase_set && i_phdrs[i].p_offset == 0)
ed07ac
+	    {
ed07ac
+	      loadbase = ehdr_vma - i_phdrs[i].p_vaddr;
ed07ac
+	      loadbase_set = 1;
ed07ac
+	    }
ed07ac
+	}
ed07ac
+    }
ed07ac
+
ed07ac
+  if (build_id_verbose >= BUILD_ID_VERBOSE_BINARY_PARSE)
ed07ac
+    warning (_("build-id: Found ELF header at address 0x%lx, loadbase 0x%lx"),
ed07ac
+	     (unsigned long) ehdr_vma, (unsigned long) loadbase);
ed07ac
+
ed07ac
+  *e_phnum_pointer = i_ehdr.e_phnum;
ed07ac
+  *loadbase_pointer = loadbase;
ed07ac
+  return i_phdrs;
ed07ac
+}
ed07ac
+
ed07ac
+/* BUILD_ID_ADDR_GET gets ADDR located somewhere in the object.
ed07ac
+   Find the first section before ADDR containing an ELF header.
ed07ac
+   We rely on the fact the sections from multiple files do not mix.
ed07ac
+   FIXME: We should check ADDR is contained _inside_ the section with possibly
ed07ac
+   missing content (P_FILESZ < P_MEMSZ).  These omitted sections are currently
ed07ac
+   hidden by _BFD_ELF_MAKE_SECTION_FROM_PHDR.  */
ed07ac
+
ed07ac
+static CORE_ADDR build_id_addr;
ed07ac
+struct build_id_addr_sect
ed07ac
+  {
ed07ac
+    struct build_id_addr_sect *next;
ed07ac
+    asection *sect;
ed07ac
+  };
ed07ac
+static struct build_id_addr_sect *build_id_addr_sect;
ed07ac
+
ed07ac
+static void build_id_addr_candidate (bfd *abfd, asection *sect, void *obj)
ed07ac
+{
ed07ac
+  if (build_id_addr >= bfd_section_vma (sect))
ed07ac
+    {
ed07ac
+      struct build_id_addr_sect *candidate;
ed07ac
+
ed07ac
+      candidate = (struct build_id_addr_sect *) xmalloc (sizeof *candidate);
ed07ac
+      candidate->next = build_id_addr_sect;
ed07ac
+      build_id_addr_sect = candidate;
ed07ac
+      candidate->sect = sect;
ed07ac
+    }
ed07ac
+}
ed07ac
+
ed07ac
+struct bfd_build_id *
ed07ac
+build_id_addr_get (CORE_ADDR addr)
ed07ac
+{
ed07ac
+  struct build_id_addr_sect *candidate;
ed07ac
+  struct bfd_build_id *retval = NULL;
ed07ac
+  Elf_Internal_Phdr *i_phdr = NULL;
ed07ac
+  bfd_vma loadbase = 0;
ed07ac
+  unsigned e_phnum = 0;
ed07ac
+
ed07ac
+  if (core_bfd == NULL)
ed07ac
+    return NULL;
ed07ac
+
ed07ac
+  build_id_addr = addr;
ed07ac
+  gdb_assert (build_id_addr_sect == NULL);
ed07ac
+  bfd_map_over_sections (core_bfd, build_id_addr_candidate, NULL);
ed07ac
+
ed07ac
+  /* Sections are sorted in the high-to-low VMAs order.
ed07ac
+     Stop the search on the first ELF header we find.
ed07ac
+     Do not continue the search even if it does not contain NT_GNU_BUILD_ID.  */
ed07ac
+
ed07ac
+  for (candidate = build_id_addr_sect; candidate != NULL;
ed07ac
+       candidate = candidate->next)
ed07ac
+    {
ed07ac
+      i_phdr = elf_get_phdr (core_bfd,
ed07ac
+			     bfd_section_vma (candidate->sect),
ed07ac
+			     &e_phnum, &loadbase);
ed07ac
+      if (i_phdr != NULL)
ed07ac
+	break;
ed07ac
+    }
ed07ac
+
ed07ac
+  if (i_phdr != NULL)
ed07ac
+    {
ed07ac
+      retval = build_id_phdr_get (core_bfd, loadbase, e_phnum, i_phdr);
ed07ac
+      xfree (i_phdr);
ed07ac
+    }
ed07ac
+
ed07ac
+  while (build_id_addr_sect != NULL)
ed07ac
+    {
ed07ac
+      candidate = build_id_addr_sect;
ed07ac
+      build_id_addr_sect = candidate->next;
ed07ac
+      xfree (candidate);
ed07ac
+    }
ed07ac
+
ed07ac
+  return retval;
ed07ac
+}
ed07ac
+
ed07ac
 /* See build-id.h.  */
ed07ac
 
ed07ac
 int
ed07ac
@@ -51,7 +450,7 @@ build_id_verify (bfd *abfd, size_t check_len, const bfd_byte *check)
ed07ac
   const struct bfd_build_id *found;
ed07ac
   int retval = 0;
ed07ac
 
ed07ac
-  found = build_id_bfd_get (abfd);
ed07ac
+  found = build_id_bfd_shdr_get (abfd);
ed07ac
 
ed07ac
   if (found == NULL)
ed07ac
     warning (_("File \"%s\" has no build-id, file skipped"),
ed07ac
@@ -66,56 +465,159 @@ build_id_verify (bfd *abfd, size_t check_len, const bfd_byte *check)
ed07ac
   return retval;
ed07ac
 }
ed07ac
 
ed07ac
+static char *
ed07ac
+link_resolve (const char *symlink, int level)
ed07ac
+{
ed07ac
+  char buf[PATH_MAX + 1], *target, *retval;
ed07ac
+  ssize_t got;
ed07ac
+
ed07ac
+  if (level > 10)
ed07ac
+    return xstrdup (symlink);
ed07ac
+
ed07ac
+  got = readlink (symlink, buf, sizeof (buf));
ed07ac
+  if (got < 0 || got >= sizeof (buf))
ed07ac
+    return xstrdup (symlink);
ed07ac
+  buf[got] = '\0';
ed07ac
+
ed07ac
+  if (IS_ABSOLUTE_PATH (buf))
ed07ac
+    target = xstrdup (buf);
ed07ac
+  else
ed07ac
+    {
ed07ac
+      const std::string dir (ldirname (symlink));
ed07ac
+
ed07ac
+      target = xstrprintf ("%s"
ed07ac
+#ifndef HAVE_DOS_BASED_FILE_SYSTEM
ed07ac
+			   "/"
ed07ac
+#else /* HAVE_DOS_BASED_FILE_SYSTEM */
ed07ac
+			   "\\"
ed07ac
+#endif /* HAVE_DOS_BASED_FILE_SYSTEM */
ed07ac
+			   "%s", dir.c_str(), buf);
ed07ac
+    }
ed07ac
+
ed07ac
+  retval = link_resolve (target, level + 1);
ed07ac
+  xfree (target);
ed07ac
+  return retval;
ed07ac
+}
ed07ac
+
ed07ac
 /* Helper for build_id_to_debug_bfd.  LINK is a path to a potential
ed07ac
    build-id-based separate debug file, potentially a symlink to the real file.
ed07ac
    If the file exists and matches BUILD_ID, return a BFD reference to it.  */
ed07ac
 
ed07ac
 static gdb_bfd_ref_ptr
ed07ac
-build_id_to_debug_bfd_1 (const std::string &link, size_t build_id_len,
ed07ac
-			 const bfd_byte *build_id)
ed07ac
+build_id_to_debug_bfd_1 (const std::string &orig_link, size_t build_id_len,
ed07ac
+			 const bfd_byte *build_id, char **link_return)
ed07ac
 {
ed07ac
+  gdb_bfd_ref_ptr ret_bfd = {};
ed07ac
+  std::string ret_link;
ed07ac
+
ed07ac
   if (separate_debug_file_debug)
ed07ac
     {
ed07ac
-      printf_unfiltered (_("  Trying %s..."), link.c_str ());
ed07ac
+      printf_unfiltered (_("  Trying %s..."), orig_link.c_str ());
ed07ac
       gdb_flush (gdb_stdout);
ed07ac
     }
ed07ac
 
ed07ac
-  /* lrealpath() is expensive even for the usually non-existent files.  */
ed07ac
-  gdb::unique_xmalloc_ptr<char> filename;
ed07ac
-  if (access (link.c_str (), F_OK) == 0)
ed07ac
-    filename.reset (lrealpath (link.c_str ()));
ed07ac
-
ed07ac
-  if (filename == NULL)
ed07ac
+  for (unsigned seqno = 0;; seqno++)
ed07ac
     {
ed07ac
-      if (separate_debug_file_debug)
ed07ac
-	printf_unfiltered (_(" no, unable to compute real path\n"));
ed07ac
+      std::string link = orig_link;
ed07ac
 
ed07ac
-      return {};
ed07ac
-    }
ed07ac
+      if (seqno > 0)
ed07ac
+	{
ed07ac
+	  /* There can be multiple build-id symlinks pointing to real files
ed07ac
+	     with the same build-id (such as hard links).  Some of the real
ed07ac
+	     files may not be installed.  */
ed07ac
+
ed07ac
+	  string_appendf (link, ".%u", seqno);
ed07ac
+	}
ed07ac
 
ed07ac
-  /* We expect to be silent on the non-existing files.  */
ed07ac
-  gdb_bfd_ref_ptr debug_bfd = gdb_bfd_open (filename.get (), gnutarget);
ed07ac
+      ret_link = link;
ed07ac
 
ed07ac
-  if (debug_bfd == NULL)
ed07ac
-    {
ed07ac
-      if (separate_debug_file_debug)
ed07ac
-	printf_unfiltered (_(" no, unable to open.\n"));
ed07ac
+      struct stat statbuf_trash;
ed07ac
+
ed07ac
+      /* `access' automatically dereferences LINK.  */
ed07ac
+      if (lstat (link.c_str (), &statbuf_trash) != 0)
ed07ac
+	{
ed07ac
+	  /* Stop increasing SEQNO.  */
ed07ac
+	  break;
ed07ac
+	}
ed07ac
+
ed07ac
+      /* lrealpath() is expensive even for the usually non-existent files.  */
ed07ac
+      gdb::unique_xmalloc_ptr<char> filename;
ed07ac
+
ed07ac
+      if (access (link.c_str (), F_OK) == 0)
ed07ac
+	filename.reset (lrealpath (link.c_str ()));
ed07ac
+
ed07ac
+      if (filename == NULL)
ed07ac
+	{
ed07ac
+	  if (separate_debug_file_debug)
ed07ac
+	    printf_unfiltered (_(" no, unable to compute real path\n"));
ed07ac
+
ed07ac
+	  continue;
ed07ac
+	}
ed07ac
+
ed07ac
+      /* We expect to be silent on the non-existing files.  */
ed07ac
+      gdb_bfd_ref_ptr debug_bfd = gdb_bfd_open (filename.get (), gnutarget, -1);
ed07ac
 
ed07ac
-      return {};
ed07ac
+      if (debug_bfd == NULL)
ed07ac
+	{
ed07ac
+	  if (separate_debug_file_debug)
ed07ac
+	    printf_unfiltered (_(" no, unable to open.\n"));
ed07ac
+
ed07ac
+	  continue;
ed07ac
+	}
ed07ac
+
ed07ac
+      if (!build_id_verify (debug_bfd.get(), build_id_len, build_id))
ed07ac
+	{
ed07ac
+	  if (separate_debug_file_debug)
ed07ac
+	    printf_unfiltered (_(" no, build-id does not match.\n"));
ed07ac
+
ed07ac
+	  continue;
ed07ac
+	}
ed07ac
+
ed07ac
+      ret_bfd = debug_bfd;
ed07ac
+      break;
ed07ac
     }
ed07ac
 
ed07ac
-  if (!build_id_verify (debug_bfd.get(), build_id_len, build_id))
ed07ac
+  std::string link_all;
ed07ac
+
ed07ac
+  if (ret_bfd != NULL)
ed07ac
     {
ed07ac
       if (separate_debug_file_debug)
ed07ac
-	printf_unfiltered (_(" no, build-id does not match.\n"));
ed07ac
-
ed07ac
-      return {};
ed07ac
+	printf_unfiltered (_(" yes!\n"));
ed07ac
+    }
ed07ac
+  else
ed07ac
+    {
ed07ac
+      /* If none of the real files is found report as missing file
ed07ac
+	 always the non-.%u-suffixed file.  */
ed07ac
+      std::string link0 = orig_link;
ed07ac
+
ed07ac
+      /* If the symlink has target request to install the target.
ed07ac
+	 BASE-debuginfo.rpm contains the symlink but BASE.rpm may be missing.
ed07ac
+	 https://bugzilla.redhat.com/show_bug.cgi?id=981154  */
ed07ac
+      std::string link0_resolved (link_resolve (link0.c_str (), 0));
ed07ac
+
ed07ac
+      if (link_all.empty ())
ed07ac
+	link_all = link0_resolved;
ed07ac
+      else
ed07ac
+	{
ed07ac
+	  /* Use whitespace instead of DIRNAME_SEPARATOR to be compatible with
ed07ac
+	     its possible use as an argument for installation command.  */
ed07ac
+	  link_all += " " + link0_resolved;
ed07ac
+	}
ed07ac
     }
ed07ac
 
ed07ac
-  if (separate_debug_file_debug)
ed07ac
-    printf_unfiltered (_(" yes!\n"));
ed07ac
+  if (link_return != NULL)
ed07ac
+    {
ed07ac
+      if (ret_bfd != NULL)
ed07ac
+	{
ed07ac
+	  *link_return = xstrdup (ret_link.c_str ());
ed07ac
+	}
ed07ac
+      else
ed07ac
+	{
ed07ac
+	  *link_return = xstrdup (link_all.c_str ());
ed07ac
+	}
ed07ac
+    }
ed07ac
 
ed07ac
-  return debug_bfd;
ed07ac
+  return ret_bfd;
ed07ac
 }
ed07ac
 
ed07ac
 /* Common code for finding BFDs of a given build-id.  This function
ed07ac
@@ -124,7 +626,7 @@ build_id_to_debug_bfd_1 (const std::string &link, size_t build_id_len,
ed07ac
 
ed07ac
 static gdb_bfd_ref_ptr
ed07ac
 build_id_to_bfd_suffix (size_t build_id_len, const bfd_byte *build_id,
ed07ac
-			const char *suffix)
ed07ac
+			const char *suffix, char **link_return)
ed07ac
 {
ed07ac
   /* Keep backward compatibility so that DEBUG_FILE_DIRECTORY being "" will
ed07ac
      cause "/.build-id/..." lookups.  */
ed07ac
@@ -147,16 +649,17 @@ build_id_to_bfd_suffix (size_t build_id_len, const bfd_byte *build_id,
ed07ac
       if (size > 0)
ed07ac
 	{
ed07ac
 	  size--;
ed07ac
-	  string_appendf (link, "%02x/", (unsigned) *data++);
ed07ac
+	  string_appendf (link, "%02x", (unsigned) *data++);
ed07ac
 	}
ed07ac
-
ed07ac
+      if (size > 0)
ed07ac
+	link += "/";
ed07ac
       while (size-- > 0)
ed07ac
 	string_appendf (link, "%02x", (unsigned) *data++);
ed07ac
 
ed07ac
       link += suffix;
ed07ac
 
ed07ac
       gdb_bfd_ref_ptr debug_bfd
ed07ac
-	= build_id_to_debug_bfd_1 (link, build_id_len, build_id);
ed07ac
+	= build_id_to_debug_bfd_1 (link, build_id_len, build_id, link_return);
ed07ac
       if (debug_bfd != NULL)
ed07ac
 	return debug_bfd;
ed07ac
 
ed07ac
@@ -170,7 +673,8 @@ build_id_to_bfd_suffix (size_t build_id_len, const bfd_byte *build_id,
ed07ac
       if (strcmp (gdb_sysroot, TARGET_SYSROOT_PREFIX) != 0)
ed07ac
 	{
ed07ac
 	  link = gdb_sysroot + link;
ed07ac
-	  debug_bfd = build_id_to_debug_bfd_1 (link, build_id_len, build_id);
ed07ac
+	  debug_bfd = build_id_to_debug_bfd_1 (link, build_id_len, build_id,
ed07ac
+					       link_return);
ed07ac
 	  if (debug_bfd != NULL)
ed07ac
 	    return debug_bfd;
ed07ac
 	}
ed07ac
@@ -179,38 +683,208 @@ build_id_to_bfd_suffix (size_t build_id_len, const bfd_byte *build_id,
ed07ac
   return {};
ed07ac
 }
ed07ac
 
ed07ac
+char *
ed07ac
+build_id_to_filename (const struct bfd_build_id *build_id, char **link_return)
ed07ac
+{
ed07ac
+  gdb_bfd_ref_ptr abfd;
ed07ac
+  char *result;
ed07ac
+
ed07ac
+  abfd = build_id_to_exec_bfd (build_id->size, build_id->data, link_return);
ed07ac
+  if (abfd == NULL)
ed07ac
+    return NULL;
ed07ac
+
ed07ac
+  result = xstrdup (bfd_get_filename (abfd.get ()));
ed07ac
+  return result;
ed07ac
+}
ed07ac
+
ed07ac
+/* This MISSING_FILEPAIR_HASH tracker is used only for the duplicite messages
ed07ac
+     Try to install the hash file ...
ed07ac
+   avoidance.  */
ed07ac
+
ed07ac
+struct missing_filepair
ed07ac
+  {
ed07ac
+    char *binary;
ed07ac
+    char *debug;
ed07ac
+    char data[1];
ed07ac
+  };
ed07ac
+
ed07ac
+static struct htab *missing_filepair_hash;
ed07ac
+static struct obstack missing_filepair_obstack;
ed07ac
+
ed07ac
+static void *
ed07ac
+missing_filepair_xcalloc (size_t nmemb, size_t nmemb_size)
ed07ac
+{
ed07ac
+  void *retval;
ed07ac
+  size_t size = nmemb * nmemb_size;
ed07ac
+
ed07ac
+  retval = obstack_alloc (&missing_filepair_obstack, size);
ed07ac
+  memset (retval, 0, size);
ed07ac
+  return retval;
ed07ac
+}
ed07ac
+
ed07ac
+static hashval_t
ed07ac
+missing_filepair_hash_func (const struct missing_filepair *elem)
ed07ac
+{
ed07ac
+  hashval_t retval = 0;
ed07ac
+
ed07ac
+  retval ^= htab_hash_string (elem->binary);
ed07ac
+  if (elem->debug != NULL)
ed07ac
+    retval ^= htab_hash_string (elem->debug);
ed07ac
+
ed07ac
+  return retval;
ed07ac
+}
ed07ac
+
ed07ac
+static int
ed07ac
+missing_filepair_eq (const struct missing_filepair *elem1,
ed07ac
+		       const struct missing_filepair *elem2)
ed07ac
+{
ed07ac
+  return strcmp (elem1->binary, elem2->binary) == 0
ed07ac
+         && ((elem1->debug == NULL) == (elem2->debug == NULL))
ed07ac
+         && (elem1->debug == NULL || strcmp (elem1->debug, elem2->debug) == 0);
ed07ac
+}
ed07ac
+
ed07ac
+static void
ed07ac
+missing_filepair_change (void)
ed07ac
+{
ed07ac
+  if (missing_filepair_hash != NULL)
ed07ac
+    {
ed07ac
+      obstack_free (&missing_filepair_obstack, NULL);
ed07ac
+      /* All their memory came just from missing_filepair_OBSTACK.  */
ed07ac
+      missing_filepair_hash = NULL;
ed07ac
+    }
ed07ac
+}
ed07ac
+
ed07ac
+static void
ed07ac
+debug_print_executable_changed (void)
ed07ac
+{
ed07ac
+  missing_filepair_change ();
ed07ac
+}
ed07ac
+
ed07ac
+/* Notify user the file BINARY with (possibly NULL) associated separate debug
ed07ac
+   information file DEBUG is missing.  DEBUG may or may not be the build-id
ed07ac
+   file such as would be:
ed07ac
+     /usr/lib/debug/.build-id/dd/b1d2ce632721c47bb9e8679f369e2295ce71be.debug
ed07ac
+   */
ed07ac
+
ed07ac
+void
ed07ac
+debug_print_missing (const char *binary, const char *debug)
ed07ac
+{
ed07ac
+  size_t binary_len0 = strlen (binary) + 1;
ed07ac
+  size_t debug_len0 = debug ? strlen (debug) + 1 : 0;
ed07ac
+  struct missing_filepair missing_filepair_find;
ed07ac
+  struct missing_filepair *missing_filepair;
ed07ac
+  struct missing_filepair **slot;
ed07ac
+
ed07ac
+  if (build_id_verbose < BUILD_ID_VERBOSE_FILENAMES)
ed07ac
+    return;
ed07ac
+
ed07ac
+  if (missing_filepair_hash == NULL)
ed07ac
+    {
ed07ac
+      obstack_init (&missing_filepair_obstack);
ed07ac
+      missing_filepair_hash = htab_create_alloc (64,
ed07ac
+	(hashval_t (*) (const void *)) missing_filepair_hash_func,
ed07ac
+	(int (*) (const void *, const void *)) missing_filepair_eq, NULL,
ed07ac
+	missing_filepair_xcalloc, NULL);
ed07ac
+    }
ed07ac
+
ed07ac
+  /* Use MISSING_FILEPAIR_FIND first instead of calling obstack_alloc with
ed07ac
+     obstack_free in the case of a (rare) match.  The problem is ALLOC_F for
ed07ac
+     MISSING_FILEPAIR_HASH allocates from MISSING_FILEPAIR_OBSTACK maintenance
ed07ac
+     structures for MISSING_FILEPAIR_HASH.  Calling obstack_free would possibly
ed07ac
+     not to free only MISSING_FILEPAIR but also some such structures (allocated
ed07ac
+     during the htab_find_slot call).  */
ed07ac
+
ed07ac
+  missing_filepair_find.binary = (char *) binary;
ed07ac
+  missing_filepair_find.debug = (char *) debug;
ed07ac
+  slot = (struct missing_filepair **) htab_find_slot (missing_filepair_hash,
ed07ac
+						      &missing_filepair_find,
ed07ac
+						      INSERT);
ed07ac
+
ed07ac
+  /* While it may be still printed duplicitely with the missing debuginfo file
ed07ac
+   * it is due to once printing about the binary file build-id link and once
ed07ac
+   * about the .debug file build-id link as both the build-id symlinks are
ed07ac
+   * located in the debuginfo package.  */
ed07ac
+
ed07ac
+  if (*slot != NULL)
ed07ac
+    return;
ed07ac
+
ed07ac
+  missing_filepair = (struct missing_filepair *) obstack_alloc (&missing_filepair_obstack,
ed07ac
+								sizeof (*missing_filepair) - 1
ed07ac
+								+ binary_len0 + debug_len0);
ed07ac
+  missing_filepair->binary = missing_filepair->data;
ed07ac
+  memcpy (missing_filepair->binary, binary, binary_len0);
ed07ac
+  if (debug != NULL)
ed07ac
+    {
ed07ac
+      missing_filepair->debug = missing_filepair->binary + binary_len0;
ed07ac
+      memcpy (missing_filepair->debug, debug, debug_len0);
ed07ac
+    }
ed07ac
+  else
ed07ac
+    missing_filepair->debug = NULL;
ed07ac
+
ed07ac
+  *slot = missing_filepair;
ed07ac
+
ed07ac
+  /* We do not collect and flush these messages as each such message
ed07ac
+     already requires its own separate lines.  */
ed07ac
+
ed07ac
+  fprintf_unfiltered (gdb_stdlog,
ed07ac
+		      _("Missing separate debuginfo for %s\n"), binary);
ed07ac
+  if (debug != NULL)
ed07ac
+    fprintf_unfiltered (gdb_stdlog, _("Try to install the hash file %s\n"),
ed07ac
+			debug);
ed07ac
+}
ed07ac
+
ed07ac
 /* See build-id.h.  */
ed07ac
 
ed07ac
 gdb_bfd_ref_ptr
ed07ac
-build_id_to_debug_bfd (size_t build_id_len, const bfd_byte *build_id)
ed07ac
+build_id_to_debug_bfd (size_t build_id_len, const bfd_byte *build_id,
ed07ac
+		       char **link_return)
ed07ac
 {
ed07ac
-  return build_id_to_bfd_suffix (build_id_len, build_id, ".debug");
ed07ac
+  return build_id_to_bfd_suffix (build_id_len, build_id, ".debug",
ed07ac
+				 link_return);
ed07ac
 }
ed07ac
 
ed07ac
 /* See build-id.h.  */
ed07ac
 
ed07ac
 gdb_bfd_ref_ptr
ed07ac
-build_id_to_exec_bfd (size_t build_id_len, const bfd_byte *build_id)
ed07ac
+build_id_to_exec_bfd (size_t build_id_len, const bfd_byte *build_id,
ed07ac
+		      char **link_return)
ed07ac
 {
ed07ac
-  return build_id_to_bfd_suffix (build_id_len, build_id, "");
ed07ac
+  return build_id_to_bfd_suffix (build_id_len, build_id, "", link_return);
ed07ac
 }
ed07ac
 
ed07ac
 /* See build-id.h.  */
ed07ac
 
ed07ac
 std::string
ed07ac
-find_separate_debug_file_by_buildid (struct objfile *objfile)
ed07ac
+find_separate_debug_file_by_buildid (struct objfile *objfile,
ed07ac
+			gdb::unique_xmalloc_ptr<char> *build_id_filename_return)
ed07ac
 {
ed07ac
   const struct bfd_build_id *build_id;
ed07ac
 
ed07ac
-  build_id = build_id_bfd_get (objfile->obfd);
ed07ac
+  if (build_id_filename_return)
ed07ac
+    *build_id_filename_return = NULL;
ed07ac
+
ed07ac
+  build_id = build_id_bfd_shdr_get (objfile->obfd);
ed07ac
   if (build_id != NULL)
ed07ac
     {
ed07ac
       if (separate_debug_file_debug)
ed07ac
 	printf_unfiltered (_("\nLooking for separate debug info (build-id) for "
ed07ac
 			     "%s\n"), objfile_name (objfile));
ed07ac
 
ed07ac
+      char *build_id_filename_cstr = NULL;
ed07ac
       gdb_bfd_ref_ptr abfd (build_id_to_debug_bfd (build_id->size,
ed07ac
-						   build_id->data));
ed07ac
+						   build_id->data,
ed07ac
+	      (!build_id_filename_return ? NULL : &build_id_filename_cstr)));
ed07ac
+      if (build_id_filename_return)
ed07ac
+	{
ed07ac
+	  if (!build_id_filename_cstr)
ed07ac
+	    gdb_assert (!*build_id_filename_return);
ed07ac
+	  else
ed07ac
+	    {
ed07ac
+	      *build_id_filename_return = gdb::unique_xmalloc_ptr<char> (build_id_filename_cstr);
ed07ac
+	      build_id_filename_cstr = NULL;
ed07ac
+	    }
ed07ac
+	}
ed07ac
+
ed07ac
       /* Prevent looping on a stripped .debug file.  */
ed07ac
       if (abfd != NULL
ed07ac
 	  && filename_cmp (bfd_get_filename (abfd.get ()),
ed07ac
@@ -223,3 +897,22 @@ find_separate_debug_file_by_buildid (struct objfile *objfile)
ed07ac
 
ed07ac
   return std::string ();
ed07ac
 }
ed07ac
+
ed07ac
+void _initialize_build_id ();
ed07ac
+
ed07ac
+void
ed07ac
+_initialize_build_id ()
ed07ac
+{
ed07ac
+  add_setshow_zinteger_cmd ("build-id-verbose", no_class, &build_id_verbose,
ed07ac
+			    _("\
ed07ac
+Set debugging level of the build-id locator."), _("\
ed07ac
+Show debugging level of the build-id locator."), _("\
ed07ac
+Level 1 (default) enables printing the missing debug filenames,\n\
ed07ac
+level 2 also prints the parsing of binaries to find the identificators."),
ed07ac
+			    NULL,
ed07ac
+			    show_build_id_verbose,
ed07ac
+			    &setlist, &showlist);
ed07ac
+
ed07ac
+  gdb::observers::executable_changed.attach (debug_print_executable_changed,
ed07ac
+                                             "build-id");
ed07ac
+}
ed07ac
diff --git a/gdb/build-id.h b/gdb/build-id.h
ed07ac
--- a/gdb/build-id.h
ed07ac
+++ b/gdb/build-id.h
ed07ac
@@ -23,9 +23,10 @@
ed07ac
 #include "gdb_bfd.h"
ed07ac
 #include "gdbsupport/rsp-low.h"
ed07ac
 
ed07ac
-/* Locate NT_GNU_BUILD_ID from ABFD and return its content.  */
ed07ac
+/* Separate debuginfo files have corrupted PHDR but SHDR is correct there.
ed07ac
+   Locate NT_GNU_BUILD_ID from ABFD and return its content.  */
ed07ac
 
ed07ac
-extern const struct bfd_build_id *build_id_bfd_get (bfd *abfd);
ed07ac
+extern const struct bfd_build_id *build_id_bfd_shdr_get (bfd *abfd);
ed07ac
 
ed07ac
 /* Return true if ABFD has NT_GNU_BUILD_ID matching the CHECK value.
ed07ac
    Otherwise, issue a warning and return false.  */
ed07ac
@@ -38,21 +39,26 @@ extern int build_id_verify (bfd *abfd,
ed07ac
    can be found, return NULL.  */
ed07ac
 
ed07ac
 extern gdb_bfd_ref_ptr build_id_to_debug_bfd (size_t build_id_len,
ed07ac
-					      const bfd_byte *build_id);
ed07ac
+					      const bfd_byte *build_id,
ed07ac
+					      char **link_return = NULL);
ed07ac
+
ed07ac
+extern char *build_id_to_filename (const struct bfd_build_id *build_id,
ed07ac
+				   char **link_return);
ed07ac
 
ed07ac
 /* Find and open a BFD for an executable file given a build-id.  If no BFD
ed07ac
    can be found, return NULL.  The returned reference to the BFD must be
ed07ac
    released by the caller.  */
ed07ac
 
ed07ac
 extern gdb_bfd_ref_ptr build_id_to_exec_bfd (size_t build_id_len,
ed07ac
-					     const bfd_byte *build_id);
ed07ac
+					     const bfd_byte *build_id,
ed07ac
+					     char **link_return);
ed07ac
 
ed07ac
 /* Find the separate debug file for OBJFILE, by using the build-id
ed07ac
    associated with OBJFILE's BFD.  If successful, returns the file name for the
ed07ac
    separate debug file, otherwise, return an empty string.  */
ed07ac
 
ed07ac
-extern std::string find_separate_debug_file_by_buildid
ed07ac
-  (struct objfile *objfile);
ed07ac
+extern std::string find_separate_debug_file_by_buildid (struct objfile *objfile,
ed07ac
+		       gdb::unique_xmalloc_ptr<char> *build_id_filename_return);
ed07ac
 
ed07ac
 /* Return an hex-string representation of BUILD_ID.  */
ed07ac
 
ed07ac
diff --git a/gdb/coffread.c b/gdb/coffread.c
ed07ac
--- a/gdb/coffread.c
ed07ac
+++ b/gdb/coffread.c
ed07ac
@@ -710,7 +710,8 @@ coff_symfile_read (struct objfile *objfile, symfile_add_flags symfile_flags)
ed07ac
   /* Try to add separate debug file if no symbols table found.   */
ed07ac
   if (!objfile->has_partial_symbols ())
ed07ac
     {
ed07ac
-      std::string debugfile = find_separate_debug_file_by_buildid (objfile);
ed07ac
+      std::string debugfile = find_separate_debug_file_by_buildid (objfile,
ed07ac
+								   NULL);
ed07ac
 
ed07ac
       if (debugfile.empty ())
ed07ac
 	debugfile = find_separate_debug_file_by_debuglink (objfile);
ed07ac
diff --git a/gdb/corelow.c b/gdb/corelow.c
ed07ac
--- a/gdb/corelow.c
ed07ac
+++ b/gdb/corelow.c
ed07ac
@@ -22,6 +22,10 @@
ed07ac
 #include <signal.h>
ed07ac
 #include <fcntl.h>
ed07ac
 #include "frame.h"		/* required by inferior.h */
ed07ac
+#include "auxv.h"
ed07ac
+#include "build-id.h"
ed07ac
+#include "elf/common.h"
ed07ac
+#include "gdbcmd.h"
ed07ac
 #include "inferior.h"
ed07ac
 #include "infrun.h"
ed07ac
 #include "symtab.h"
ed07ac
@@ -356,6 +360,8 @@ add_to_thread_list (asection *asect, asection *reg_sect)
ed07ac
     switch_to_thread (thr);			/* Yes, make it current.  */
ed07ac
 }
ed07ac
 
ed07ac
+static bool build_id_core_loads = true;
ed07ac
+
ed07ac
 /* Issue a message saying we have no core to debug, if FROM_TTY.  */
ed07ac
 
ed07ac
 static void
ed07ac
@@ -392,19 +398,26 @@ core_file_command (const char *filename, int from_tty)
ed07ac
 static void
ed07ac
 locate_exec_from_corefile_build_id (bfd *abfd, int from_tty)
ed07ac
 {
ed07ac
-  const bfd_build_id *build_id = build_id_bfd_get (abfd);
ed07ac
+  const bfd_build_id *build_id = build_id_bfd_shdr_get (abfd);
ed07ac
   if (build_id == nullptr)
ed07ac
     return;
ed07ac
 
ed07ac
+  char *build_id_filename;
ed07ac
   gdb_bfd_ref_ptr execbfd
ed07ac
-    = build_id_to_exec_bfd (build_id->size, build_id->data);
ed07ac
+    = build_id_to_exec_bfd (build_id->size, build_id->data,
ed07ac
+			    &build_id_filename);
ed07ac
 
ed07ac
   if (execbfd != nullptr)
ed07ac
     {
ed07ac
       exec_file_attach (bfd_get_filename (execbfd.get ()), from_tty);
ed07ac
       symbol_file_add_main (bfd_get_filename (execbfd.get ()),
ed07ac
 			    symfile_add_flag (from_tty ? SYMFILE_VERBOSE : 0));
ed07ac
+      if (current_program_space->symfile_object_file != NULL)
ed07ac
+	current_program_space->symfile_object_file->flags |=
ed07ac
+	  OBJF_BUILD_ID_CORE_LOADED;
ed07ac
     }
ed07ac
+  else
ed07ac
+    debug_print_missing (BUILD_ID_MAIN_EXECUTABLE_FILENAME, build_id_filename);
ed07ac
 }
ed07ac
 
ed07ac
 /* See gdbcore.h.  */
ed07ac
@@ -1209,4 +1222,11 @@ _initialize_corelow ()
ed07ac
 	   maintenance_print_core_file_backed_mappings,
ed07ac
 	   _("Print core file's file-backed mappings."),
ed07ac
 	   &maintenanceprintlist);
ed07ac
+
ed07ac
+  add_setshow_boolean_cmd ("build-id-core-loads", class_files,
ed07ac
+			   &build_id_core_loads, _("\
ed07ac
+Set whether CORE-FILE loads the build-id associated files automatically."), _("\
ed07ac
+Show whether CORE-FILE loads the build-id associated files automatically."),
ed07ac
+			   NULL, NULL, NULL,
ed07ac
+			   &setlist, &showlist);
ed07ac
 }
ed07ac
diff --git a/gdb/doc/gdb.texinfo b/gdb/doc/gdb.texinfo
ed07ac
--- a/gdb/doc/gdb.texinfo
ed07ac
+++ b/gdb/doc/gdb.texinfo
ed07ac
@@ -21415,6 +21415,27 @@ information files.
ed07ac
 
ed07ac
 @end table
ed07ac
 
ed07ac
+You can also adjust the current verbosity of the @dfn{build id} locating.
ed07ac
+
ed07ac
+@table @code
ed07ac
+
ed07ac
+@kindex set build-id-verbose
ed07ac
+@item set build-id-verbose 0
ed07ac
+No additional messages are printed.
ed07ac
+
ed07ac
+@item set build-id-verbose 1
ed07ac
+Missing separate debug filenames are printed.
ed07ac
+
ed07ac
+@item set build-id-verbose 2
ed07ac
+Missing separate debug filenames are printed and also all the parsing of the
ed07ac
+binaries to find their @dfn{build id} content is printed.
ed07ac
+
ed07ac
+@kindex show build-id-verbose
ed07ac
+@item show build-id-verbose
ed07ac
+Show the current verbosity value for the @dfn{build id} content locating.
ed07ac
+
ed07ac
+@end table
ed07ac
+
ed07ac
 @cindex @code{.gnu_debuglink} sections
ed07ac
 @cindex debug link sections
ed07ac
 A debug link is a special section of the executable file named
ed07ac
diff --git a/gdb/dwarf2/index-cache.c b/gdb/dwarf2/index-cache.c
ed07ac
--- a/gdb/dwarf2/index-cache.c
ed07ac
+++ b/gdb/dwarf2/index-cache.c
ed07ac
@@ -95,7 +95,7 @@ index_cache::store (dwarf2_per_objfile *per_objfile)
ed07ac
     return;
ed07ac
 
ed07ac
   /* Get build id of objfile.  */
ed07ac
-  const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
ed07ac
+  const bfd_build_id *build_id = build_id_bfd_shdr_get (obj->obfd);
ed07ac
   if (build_id == nullptr)
ed07ac
     {
ed07ac
       if (debug_index_cache)
ed07ac
@@ -113,7 +113,8 @@ index_cache::store (dwarf2_per_objfile *per_objfile)
ed07ac
 
ed07ac
   if (dwz != nullptr)
ed07ac
     {
ed07ac
-      const bfd_build_id *dwz_build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
ed07ac
+      const bfd_build_id *dwz_build_id
ed07ac
+	= build_id_bfd_shdr_get (dwz->dwz_bfd.get ());
ed07ac
 
ed07ac
       if (dwz_build_id == nullptr)
ed07ac
 	{
ed07ac
diff --git a/gdb/dwarf2/read.c b/gdb/dwarf2/read.c
ed07ac
--- a/gdb/dwarf2/read.c
ed07ac
+++ b/gdb/dwarf2/read.c
ed07ac
@@ -5447,7 +5447,7 @@ get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
ed07ac
 static gdb::array_view<const gdb_byte>
ed07ac
 get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_bfd *dwarf2_per_bfd)
ed07ac
 {
ed07ac
-  const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
ed07ac
+  const bfd_build_id *build_id = build_id_bfd_shdr_get (obj->obfd);
ed07ac
   if (build_id == nullptr)
ed07ac
     return {};
ed07ac
 
ed07ac
@@ -5460,7 +5460,7 @@ get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_bfd *dwarf2_per_bfd)
ed07ac
 static gdb::array_view<const gdb_byte>
ed07ac
 get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
ed07ac
 {
ed07ac
-  const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
ed07ac
+  const bfd_build_id *build_id = build_id_bfd_shdr_get (dwz->dwz_bfd.get ());
ed07ac
   if (build_id == nullptr)
ed07ac
     return {};
ed07ac
 
ed07ac
diff --git a/gdb/elfread.c b/gdb/elfread.c
ed07ac
--- a/gdb/elfread.c
ed07ac
+++ b/gdb/elfread.c
ed07ac
@@ -1272,7 +1272,9 @@ elf_symfile_read (struct objfile *objfile, symfile_add_flags symfile_flags)
ed07ac
 	   && objfile->separate_debug_objfile == NULL
ed07ac
 	   && objfile->separate_debug_objfile_backlink == NULL)
ed07ac
     {
ed07ac
-      std::string debugfile = find_separate_debug_file_by_buildid (objfile);
ed07ac
+      gdb::unique_xmalloc_ptr<char> build_id_filename;
ed07ac
+      std::string debugfile
ed07ac
+	= find_separate_debug_file_by_buildid (objfile, &build_id_filename);
ed07ac
 
ed07ac
       if (debugfile.empty ())
ed07ac
 	debugfile = find_separate_debug_file_by_debuglink (objfile);
ed07ac
@@ -1287,7 +1289,7 @@ elf_symfile_read (struct objfile *objfile, symfile_add_flags symfile_flags)
ed07ac
       else
ed07ac
 	{
ed07ac
 	  has_dwarf2 = false;
ed07ac
-	  const struct bfd_build_id *build_id = build_id_bfd_get (objfile->obfd);
ed07ac
+	  const struct bfd_build_id *build_id = build_id_bfd_shdr_get (objfile->obfd);
ed07ac
 
ed07ac
 	  if (build_id != nullptr)
ed07ac
 	    {
ed07ac
@@ -1312,6 +1314,10 @@ elf_symfile_read (struct objfile *objfile, symfile_add_flags symfile_flags)
ed07ac
 		      has_dwarf2 = true;
ed07ac
 		    }
ed07ac
 		}
ed07ac
+		/* Check if any separate debug info has been extracted out.  */
ed07ac
+		else if (bfd_get_section_by_name (objfile->obfd, ".gnu_debuglink")
ed07ac
+			 != NULL)
ed07ac
+		  debug_print_missing (objfile_name (objfile), build_id_filename.get ());
ed07ac
 	    }
ed07ac
 	}
ed07ac
     }
ed07ac
diff --git a/gdb/exec.c b/gdb/exec.c
ed07ac
--- a/gdb/exec.c
ed07ac
+++ b/gdb/exec.c
ed07ac
@@ -237,7 +237,7 @@ validate_exec_file (int from_tty)
ed07ac
   current_exec_file = get_exec_file (0);
ed07ac
 
ed07ac
   const bfd_build_id *exec_file_build_id
ed07ac
-    = build_id_bfd_get (current_program_space->exec_bfd ());
ed07ac
+    = build_id_bfd_shdr_get (current_program_space->exec_bfd ());
ed07ac
   if (exec_file_build_id != nullptr)
ed07ac
     {
ed07ac
       /* Prepend the target prefix, to force gdb_bfd_open to open the
ed07ac
@@ -250,7 +250,7 @@ validate_exec_file (int from_tty)
ed07ac
       if (abfd != nullptr)
ed07ac
 	{
ed07ac
 	  const bfd_build_id *target_exec_file_build_id
ed07ac
-	    = build_id_bfd_get (abfd.get ());
ed07ac
+	    = build_id_bfd_shdr_get (abfd.get ());
ed07ac
 
ed07ac
 	  if (target_exec_file_build_id != nullptr)
ed07ac
 	    {
ed07ac
diff --git a/gdb/objfiles.h b/gdb/objfiles.h
ed07ac
--- a/gdb/objfiles.h
ed07ac
+++ b/gdb/objfiles.h
ed07ac
@@ -812,6 +812,10 @@ struct objfile
ed07ac
   bool skip_jit_symbol_lookup = false;
ed07ac
 };
ed07ac
 
ed07ac
+/* This file was loaded according to the BUILD_ID_CORE_LOADS rules.  */
ed07ac
+
ed07ac
+#define OBJF_BUILD_ID_CORE_LOADED static_cast<enum objfile_flag>(1 << 12)
ed07ac
+
ed07ac
 /* A deleter for objfile.  */
ed07ac
 
ed07ac
 struct objfile_deleter
ed07ac
diff --git a/gdb/python/py-objfile.c b/gdb/python/py-objfile.c
ed07ac
--- a/gdb/python/py-objfile.c
ed07ac
+++ b/gdb/python/py-objfile.c
ed07ac
@@ -132,7 +132,7 @@ objfpy_get_build_id (PyObject *self, void *closure)
ed07ac
 
ed07ac
   try
ed07ac
     {
ed07ac
-      build_id = build_id_bfd_get (objfile->obfd);
ed07ac
+      build_id = build_id_bfd_shdr_get (objfile->obfd);
ed07ac
     }
ed07ac
   catch (const gdb_exception &except)
ed07ac
     {
ed07ac
@@ -600,7 +600,7 @@ objfpy_lookup_objfile_by_build_id (const char *build_id)
ed07ac
       /* Don't return separate debug files.  */
ed07ac
       if (objfile->separate_debug_objfile_backlink != NULL)
ed07ac
 	continue;
ed07ac
-      obfd_build_id = build_id_bfd_get (objfile->obfd);
ed07ac
+      obfd_build_id = build_id_bfd_shdr_get (objfile->obfd);
ed07ac
       if (obfd_build_id == NULL)
ed07ac
 	continue;
ed07ac
       if (objfpy_build_id_matches (obfd_build_id, build_id))
ed07ac
diff --git a/gdb/solib-svr4.c b/gdb/solib-svr4.c
ed07ac
--- a/gdb/solib-svr4.c
ed07ac
+++ b/gdb/solib-svr4.c
ed07ac
@@ -45,6 +45,7 @@
ed07ac
 #include "auxv.h"
ed07ac
 #include "gdb_bfd.h"
ed07ac
 #include "probe.h"
ed07ac
+#include "build-id.h"
ed07ac
 
ed07ac
 static struct link_map_offsets *svr4_fetch_link_map_offsets (void);
ed07ac
 static int svr4_have_link_map_offsets (void);
ed07ac
@@ -1348,9 +1349,51 @@ svr4_read_so_list (svr4_info *info, CORE_ADDR lm, CORE_ADDR prev_lm,
ed07ac
 	  continue;
ed07ac
 	}
ed07ac
 
ed07ac
-      strncpy (newobj->so_name, buffer.get (), SO_NAME_MAX_PATH_SIZE - 1);
ed07ac
-      newobj->so_name[SO_NAME_MAX_PATH_SIZE - 1] = '\0';
ed07ac
-      strcpy (newobj->so_original_name, newobj->so_name);
ed07ac
+      {
ed07ac
+	struct bfd_build_id *build_id;
ed07ac
+
ed07ac
+	strncpy (newobj->so_original_name, buffer.get (), SO_NAME_MAX_PATH_SIZE - 1);
ed07ac
+	newobj->so_original_name[SO_NAME_MAX_PATH_SIZE - 1] = '\0';
ed07ac
+	/* May get overwritten below.  */
ed07ac
+	strcpy (newobj->so_name, newobj->so_original_name);
ed07ac
+
ed07ac
+	build_id = build_id_addr_get (((lm_info_svr4 *) newobj->lm_info)->l_ld);
ed07ac
+	if (build_id != NULL)
ed07ac
+	  {
ed07ac
+	    char *name, *build_id_filename;
ed07ac
+
ed07ac
+	    /* Missing the build-id matching separate debug info file
ed07ac
+	       would be handled while SO_NAME gets loaded.  */
ed07ac
+	    name = build_id_to_filename (build_id, &build_id_filename);
ed07ac
+	    if (name != NULL)
ed07ac
+	      {
ed07ac
+		strncpy (newobj->so_name, name, SO_NAME_MAX_PATH_SIZE - 1);
ed07ac
+		newobj->so_name[SO_NAME_MAX_PATH_SIZE - 1] = '\0';
ed07ac
+		xfree (name);
ed07ac
+	      }
ed07ac
+	    else
ed07ac
+	      {
ed07ac
+		debug_print_missing (newobj->so_name, build_id_filename);
ed07ac
+
ed07ac
+		/* In the case the main executable was found according to
ed07ac
+		   its build-id (from a core file) prevent loading
ed07ac
+		   a different build of a library with accidentally the
ed07ac
+		   same SO_NAME.
ed07ac
+
ed07ac
+		   It suppresses bogus backtraces (and prints "??" there
ed07ac
+		   instead) if the on-disk files no longer match the
ed07ac
+		   running program version.  */
ed07ac
+
ed07ac
+		if (current_program_space->symfile_object_file != NULL
ed07ac
+		    && (current_program_space->symfile_object_file->flags
ed07ac
+			& OBJF_BUILD_ID_CORE_LOADED) != 0)
ed07ac
+		  newobj->so_name[0] = 0;
ed07ac
+	      }
ed07ac
+
ed07ac
+	    xfree (build_id_filename);
ed07ac
+	    xfree (build_id);
ed07ac
+	  }
ed07ac
+      }
ed07ac
 
ed07ac
       /* If this entry has no name, or its name matches the name
ed07ac
 	 for the main executable, don't include it in the list.  */
ed07ac
diff --git a/gdb/source.c b/gdb/source.c
ed07ac
--- a/gdb/source.c
ed07ac
+++ b/gdb/source.c
ed07ac
@@ -1178,7 +1178,7 @@ open_source_file (struct symtab *s)
ed07ac
 	      srcpath += s->filename;
ed07ac
 	    }
ed07ac
 
ed07ac
-	  const struct bfd_build_id *build_id = build_id_bfd_get (ofp->obfd);
ed07ac
+	  const struct bfd_build_id *build_id = build_id_bfd_shdr_get (ofp->obfd);
ed07ac
 
ed07ac
 	  /* Query debuginfod for the source file.  */
ed07ac
 	  if (build_id != nullptr && !srcpath.empty ())
ed07ac
diff --git a/gdb/symfile.h b/gdb/symfile.h
ed07ac
--- a/gdb/symfile.h
ed07ac
+++ b/gdb/symfile.h
ed07ac
@@ -332,12 +332,18 @@ bool expand_symtabs_matching
ed07ac
 void map_symbol_filenames (gdb::function_view<symbol_filename_ftype> fun,
ed07ac
 			   bool need_fullname);
ed07ac
 
ed07ac
+
ed07ac
 /* Target-agnostic function to load the sections of an executable into memory.
ed07ac
 
ed07ac
    ARGS should be in the form "EXECUTABLE [OFFSET]", where OFFSET is an
ed07ac
    optional offset to apply to each section.  */
ed07ac
 extern void generic_load (const char *args, int from_tty);
ed07ac
 
ed07ac
+/* build-id support.  */
ed07ac
+extern struct bfd_build_id *build_id_addr_get (CORE_ADDR addr);
ed07ac
+extern void debug_print_missing (const char *binary, const char *debug);
ed07ac
+#define BUILD_ID_MAIN_EXECUTABLE_FILENAME _("the main executable file")
ed07ac
+
ed07ac
 /* From minidebug.c.  */
ed07ac
 
ed07ac
 extern gdb_bfd_ref_ptr find_separate_debug_file_in_section (struct objfile *);
ed07ac
diff --git a/gdb/testsuite/gdb.base/corefile.exp b/gdb/testsuite/gdb.base/corefile.exp
ed07ac
--- a/gdb/testsuite/gdb.base/corefile.exp
ed07ac
+++ b/gdb/testsuite/gdb.base/corefile.exp
ed07ac
@@ -343,3 +343,33 @@ gdb_test_multiple "core-file $corefile" $test {
ed07ac
 	pass $test
ed07ac
     }
ed07ac
 }
ed07ac
+
ed07ac
+
ed07ac
+# Test auto-loading of binary files through build-id from the core file.
ed07ac
+set buildid [build_id_debug_filename_get $binfile]
ed07ac
+set wholetest "binfile found by build-id"
ed07ac
+if {$buildid == ""} {
ed07ac
+    untested "$wholetest (binary has no build-id)"
ed07ac
+} else {
ed07ac
+    gdb_exit
ed07ac
+    gdb_start
ed07ac
+
ed07ac
+    regsub {\.debug$} $buildid {} buildid
ed07ac
+    set debugdir [standard_output_file ${testfile}-debugdir]
ed07ac
+    file delete -force -- $debugdir
ed07ac
+    file mkdir $debugdir/[file dirname $buildid]
ed07ac
+    file copy $binfile $debugdir/$buildid
ed07ac
+
ed07ac
+    set test "show debug-file-directory"
ed07ac
+    gdb_test_multiple $test $test {
ed07ac
+	-re "The directory where separate debug symbols are searched for is \"(.*)\"\\.\r\n$gdb_prompt $" {
ed07ac
+	    set debugdir_orig $expect_out(1,string)
ed07ac
+	    pass $test
ed07ac
+	}
ed07ac
+    }
ed07ac
+    gdb_test_no_output "set debug-file-directory $debugdir:$debugdir_orig" "set debug-file-directory"
ed07ac
+    gdb_test "show build-id-core-loads" {Whether CORE-FILE loads the build-id associated files automatically is on\.}
ed07ac
+    gdb_test "core-file $corefile" "\r\nProgram terminated with .*" "core-file without executable"
ed07ac
+    gdb_test "info files" "Local exec file:\r\n\[ \t\]*`[string_to_regexp $debugdir/$buildid]', file type .*"
ed07ac
+    pass $wholetest
ed07ac
+}
ed07ac
diff --git a/gdb/testsuite/gdb.base/gdbinit-history.exp b/gdb/testsuite/gdb.base/gdbinit-history.exp
ed07ac
--- a/gdb/testsuite/gdb.base/gdbinit-history.exp
ed07ac
+++ b/gdb/testsuite/gdb.base/gdbinit-history.exp
ed07ac
@@ -185,7 +185,8 @@ proc test_empty_history_filename { } {
ed07ac
     global env
ed07ac
     global gdb_prompt
ed07ac
 
ed07ac
-    set common_history [list "set height 0" "set width 0"]
ed07ac
+    set common_history [list "set height 0" "set width 0" \
ed07ac
+			    "set build-id-verbose 0"]
ed07ac
 
ed07ac
     set test_dir [standard_output_file history_test]
ed07ac
     remote_exec host "mkdir -p $test_dir"
ed07ac
diff --git a/gdb/testsuite/gdb.base/new-ui-pending-input.exp b/gdb/testsuite/gdb.base/new-ui-pending-input.exp
ed07ac
--- a/gdb/testsuite/gdb.base/new-ui-pending-input.exp
ed07ac
+++ b/gdb/testsuite/gdb.base/new-ui-pending-input.exp
ed07ac
@@ -62,6 +62,7 @@ proc test_command_line_new_ui_pending_input {} {
ed07ac
     set options ""
ed07ac
     append options " -iex \"set height 0\""
ed07ac
     append options " -iex \"set width 0\""
ed07ac
+    append options " -iex \"set build-id-verbose 0\""
ed07ac
     append options " -iex \"new-ui console $extra_tty_name\""
ed07ac
     append options " -ex \"b $bpline\""
ed07ac
     append options " -ex \"run\""
ed07ac
diff --git a/gdb/testsuite/lib/gdb.exp b/gdb/testsuite/lib/gdb.exp
ed07ac
--- a/gdb/testsuite/lib/gdb.exp
ed07ac
+++ b/gdb/testsuite/lib/gdb.exp
ed07ac
@@ -130,7 +130,8 @@ if ![info exists INTERNAL_GDBFLAGS] {
ed07ac
 		   "-nx" \
ed07ac
 		   "-data-directory $BUILD_DATA_DIRECTORY" \
ed07ac
 		   {-iex "set height 0"} \
ed07ac
-		   {-iex "set width 0"}]]
ed07ac
+		   {-iex "set width 0"} \
ed07ac
+		   {-iex "set build-id-verbose 0"}]]
ed07ac
 }
ed07ac
 
ed07ac
 # The variable gdb_prompt is a regexp which matches the gdb prompt.
ed07ac
@@ -2130,6 +2131,17 @@ proc default_gdb_start { } {
ed07ac
 	}
ed07ac
     }
ed07ac
 
ed07ac
+    # Turn off the missing warnings as the testsuite does not expect it.
ed07ac
+    send_gdb "set build-id-verbose 0\n"
ed07ac
+    gdb_expect 10 {
ed07ac
+	-re "$gdb_prompt $" {
ed07ac
+	    verbose "Disabled the missing debug infos warnings." 2
ed07ac
+	}
ed07ac
+	timeout {
ed07ac
+	    warning "Could not disable the missing debug infos warnings.."
ed07ac
+	}
ed07ac
+    }
ed07ac
+
ed07ac
     gdb_debug_init
ed07ac
     return 0
ed07ac
 }
ed07ac
diff --git a/gdb/testsuite/lib/mi-support.exp b/gdb/testsuite/lib/mi-support.exp
ed07ac
--- a/gdb/testsuite/lib/mi-support.exp
ed07ac
+++ b/gdb/testsuite/lib/mi-support.exp
ed07ac
@@ -322,6 +322,16 @@ proc default_mi_gdb_start { args } {
ed07ac
 	    warning "Couldn't set the width to 0."
ed07ac
 	}
ed07ac
     }
ed07ac
+    # Turn off the missing warnings as the testsuite does not expect it.
ed07ac
+    send_gdb "190-gdb-set build-id-verbose 0\n"
ed07ac
+    gdb_expect 10 {
ed07ac
+	-re ".*190-gdb-set build-id-verbose 0\r\n190\\\^done\r\n$mi_gdb_prompt$" {
ed07ac
+	    verbose "Disabled the missing debug infos warnings." 2
ed07ac
+	}
ed07ac
+	timeout {
ed07ac
+	    warning "Could not disable the missing debug infos warnings.."
ed07ac
+	}
ed07ac
+    }
ed07ac
 
ed07ac
     if { $separate_inferior_pty } {
ed07ac
 	mi_create_inferior_pty