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

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