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

861f93
Index: gdb-7.6/gdb/corelow.c
861f93
===================================================================
861f93
--- gdb-7.6.orig/gdb/corelow.c	2013-01-31 19:37:37.000000000 +0100
861f93
+++ gdb-7.6/gdb/corelow.c	2013-08-06 16:57:14.832517976 +0200
861f93
@@ -46,6 +46,9 @@
861f93
 #include "progspace.h"
861f93
 #include "objfiles.h"
861f93
 #include "gdb_bfd.h"
861f93
+#include "auxv.h"
861f93
+#include "elf/common.h"
861f93
+#include "gdbcmd.h"
861f93
 
861f93
 #ifndef O_LARGEFILE
861f93
 #define O_LARGEFILE 0
861f93
@@ -271,6 +274,53 @@ add_to_thread_list (bfd *abfd, asection
861f93
     inferior_ptid = ptid;			/* Yes, make it current.  */
861f93
 }
861f93
 
861f93
+static int build_id_core_loads = 1;
861f93
+
861f93
+static void
861f93
+build_id_locate_exec (int from_tty)
861f93
+{
861f93
+  CORE_ADDR at_entry;
861f93
+  struct elf_build_id *build_id;
861f93
+  char *execfilename, *debug_filename;
861f93
+  char *build_id_filename;
861f93
+  struct cleanup *back_to;
861f93
+
861f93
+  if (exec_bfd != NULL || symfile_objfile != NULL)
861f93
+    return;
861f93
+
861f93
+  if (target_auxv_search (&current_target, AT_ENTRY, &at_entry) <= 0)
861f93
+    return;
861f93
+
861f93
+  build_id = build_id_addr_get (at_entry);
861f93
+  if (build_id == NULL)
861f93
+    return;
861f93
+  back_to = make_cleanup (xfree, build_id);
861f93
+
861f93
+  /* SYMFILE_OBJFILE should refer to the main executable (not only to its
861f93
+     separate debug info file).  gcc44+ keeps .eh_frame only in the main
861f93
+     executable without its duplicate .debug_frame in the separate debug info
861f93
+     file - such .eh_frame would not be found if SYMFILE_OBJFILE would refer
861f93
+     directly to the separate debug info file.  */
861f93
+
861f93
+  execfilename = build_id_to_filename (build_id, &build_id_filename, 0);
861f93
+  make_cleanup (xfree, build_id_filename);
861f93
+
861f93
+  if (execfilename != NULL)
861f93
+    {
861f93
+      make_cleanup (xfree, execfilename);
861f93
+      exec_file_attach (execfilename, from_tty);
861f93
+      symbol_file_add_main (execfilename, from_tty);
861f93
+      if (symfile_objfile != NULL)
861f93
+        symfile_objfile->flags |= OBJF_BUILD_ID_CORE_LOADED;
861f93
+    }
861f93
+  else
861f93
+    debug_print_missing (_("the main executable file"), build_id_filename);
861f93
+
861f93
+  do_cleanups (back_to);
861f93
+
861f93
+  /* No automatic SOLIB_ADD as the libraries would get read twice.  */
861f93
+}
861f93
+
861f93
 /* This routine opens and sets up the core file bfd.  */
861f93
 
861f93
 static void
861f93
@@ -409,6 +459,14 @@ core_open (char *filename, int from_tty)
861f93
 	switch_to_thread (thread->ptid);
861f93
     }
861f93
 
861f93
+  /* Find the build_id identifiers.  If it gets executed after
861f93
+     POST_CREATE_INFERIOR we would clash with asking to discard the already
861f93
+     loaded VDSO symbols.  If it gets executed before bfd_map_over_sections
861f93
+     INFERIOR_PTID is still not set and libthread_db initialization crashes on
861f93
+     PID == 0 in ps_pglobal_lookup.  */
861f93
+  if (build_id_core_loads != 0)
861f93
+    build_id_locate_exec (from_tty);
861f93
+
861f93
   post_create_inferior (&core_ops, from_tty);
861f93
 
861f93
   /* Now go through the target stack looking for threads since there
861f93
@@ -978,4 +1036,11 @@ _initialize_corelow (void)
861f93
   init_core_ops ();
861f93
 
861f93
   add_target (&core_ops);
861f93
+
861f93
+  add_setshow_boolean_cmd ("build-id-core-loads", class_files,
861f93
+			   &build_id_core_loads, _("\
861f93
+Set whether CORE-FILE loads the build-id associated files automatically."), _("\
861f93
+Show whether CORE-FILE loads the build-id associated files automatically."),
861f93
+			   NULL, NULL, NULL,
861f93
+			   &setlist, &showlist);
861f93
 }
861f93
Index: gdb-7.6/gdb/doc/gdb.texinfo
861f93
===================================================================
861f93
--- gdb-7.6.orig/gdb/doc/gdb.texinfo	2013-08-06 16:57:14.531517553 +0200
861f93
+++ gdb-7.6/gdb/doc/gdb.texinfo	2013-08-06 16:57:14.843517992 +0200
861f93
@@ -16831,6 +16831,27 @@ information files.
861f93
 
861f93
 @end table
861f93
 
861f93
+You can also adjust the current verbosity of the @dfn{build id} locating.
861f93
+
861f93
+@table @code
861f93
+
861f93
+@kindex set build-id-verbose
861f93
+@item set build-id-verbose 0
861f93
+No additional messages are printed.
861f93
+
861f93
+@item set build-id-verbose 1
861f93
+Missing separate debug filenames are printed.
861f93
+
861f93
+@item set build-id-verbose 2
861f93
+Missing separate debug filenames are printed and also all the parsing of the
861f93
+binaries to find their @dfn{build id} content is printed.
861f93
+
861f93
+@kindex show build-id-verbose
861f93
+@item show build-id-verbose
861f93
+Show the current verbosity value for the @dfn{build id} content locating.
861f93
+
861f93
+@end table
861f93
+
861f93
 @cindex @code{.gnu_debuglink} sections
861f93
 @cindex debug link sections
861f93
 A debug link is a special section of the executable file named
861f93
Index: gdb-7.6/gdb/solib-svr4.c
861f93
===================================================================
861f93
--- gdb-7.6.orig/gdb/solib-svr4.c	2013-01-31 14:52:52.000000000 +0100
861f93
+++ gdb-7.6/gdb/solib-svr4.c	2013-08-06 16:57:14.917518096 +0200
861f93
@@ -1224,9 +1224,52 @@ svr4_read_so_list (CORE_ADDR lm, struct
861f93
 	  continue;
861f93
 	}
861f93
 
861f93
-      strncpy (new->so_name, buffer, SO_NAME_MAX_PATH_SIZE - 1);
861f93
-      new->so_name[SO_NAME_MAX_PATH_SIZE - 1] = '\0';
861f93
-      strcpy (new->so_original_name, new->so_name);
861f93
+      {
861f93
+	struct elf_build_id *build_id;
861f93
+
861f93
+	strncpy (new->so_original_name, buffer, SO_NAME_MAX_PATH_SIZE - 1);
861f93
+	new->so_original_name[SO_NAME_MAX_PATH_SIZE - 1] = '\0';
861f93
+	/* May get overwritten below.  */
861f93
+	strcpy (new->so_name, new->so_original_name);
861f93
+
861f93
+	build_id = build_id_addr_get (new->lm_info->l_ld);
861f93
+	if (build_id != NULL)
861f93
+	  {
861f93
+	    char *name, *build_id_filename;
861f93
+
861f93
+	    /* Missing the build-id matching separate debug info file
861f93
+	       would be handled while SO_NAME gets loaded.  */
861f93
+	    name = build_id_to_filename (build_id, &build_id_filename, 0);
861f93
+	    if (name != NULL)
861f93
+	      {
861f93
+		strncpy (new->so_name, name, SO_NAME_MAX_PATH_SIZE - 1);
861f93
+		new->so_name[SO_NAME_MAX_PATH_SIZE - 1] = '\0';
861f93
+		xfree (name);
861f93
+	      }
861f93
+	    else
861f93
+	      {
861f93
+		debug_print_missing (new->so_name, build_id_filename);
861f93
+
861f93
+		/* In the case the main executable was found according to
861f93
+		   its build-id (from a core file) prevent loading
861f93
+		   a different build of a library with accidentally the
861f93
+		   same SO_NAME.
861f93
+
861f93
+		   It suppresses bogus backtraces (and prints "??" there
861f93
+		   instead) if the on-disk files no longer match the
861f93
+		   running program version.  */
861f93
+
861f93
+		if (symfile_objfile != NULL
861f93
+		    && (symfile_objfile->flags
861f93
+			& OBJF_BUILD_ID_CORE_LOADED) != 0)
861f93
+		  new->so_name[0] = 0;
861f93
+	      }
861f93
+
861f93
+	    xfree (build_id_filename);
861f93
+	    xfree (build_id);
861f93
+	  }
861f93
+      }
861f93
+
861f93
       xfree (buffer);
861f93
 
861f93
       /* If this entry has no name, or its name matches the name
861f93
Index: gdb-7.6/gdb/elfread.c
861f93
===================================================================
861f93
--- gdb-7.6.orig/gdb/elfread.c	2013-03-18 10:20:02.000000000 +0100
861f93
+++ gdb-7.6/gdb/elfread.c	2013-08-06 16:57:19.548524529 +0200
861f93
@@ -45,6 +45,11 @@
861f93
 #include "regcache.h"
861f93
 #include "bcache.h"
861f93
 #include "gdb_bfd.h"
861f93
+#include "libbfd.h"
861f93
+#include "gdbcore.h"
861f93
+#include "gdbcmd.h"
861f93
+#include "observer.h"
861f93
+#include <sys/stat.h>
861f93
 
861f93
 extern void _initialize_elfread (void);
861f93
 
861f93
@@ -1068,10 +1073,59 @@ elf_gnu_ifunc_resolver_return_stop (stru
861f93
   update_breakpoint_locations (b, sals, sals_end);
861f93
 }
861f93
 
861f93
-/* Locate NT_GNU_BUILD_ID from ABFD and return its content.  */
861f93
+#define BUILD_ID_VERBOSE_NONE 0
861f93
+#define BUILD_ID_VERBOSE_FILENAMES 1
861f93
+#define BUILD_ID_VERBOSE_BINARY_PARSE 2
861f93
+static int build_id_verbose = BUILD_ID_VERBOSE_FILENAMES;
861f93
+static void
861f93
+show_build_id_verbose (struct ui_file *file, int from_tty,
861f93
+		       struct cmd_list_element *c, const char *value)
861f93
+{
861f93
+  fprintf_filtered (file, _("Verbosity level of the build-id locator is %s.\n"),
861f93
+		    value);
861f93
+}
861f93
+
861f93
+/* Locate NT_GNU_BUILD_ID and return its matching debug filename.
861f93
+   FIXME: NOTE decoding should be unified with the BFD core notes decoding.  */
861f93
+
861f93
+static struct elf_build_id *
861f93
+build_id_buf_get (bfd *templ, gdb_byte *buf, bfd_size_type size)
861f93
+{
861f93
+  bfd_byte *p;
861f93
+
861f93
+  p = buf;
861f93
+  while (p < buf + size)
861f93
+    {
861f93
+      /* FIXME: bad alignment assumption.  */
861f93
+      Elf_External_Note *xnp = (Elf_External_Note *) p;
861f93
+      size_t namesz = H_GET_32 (templ, xnp->namesz);
861f93
+      size_t descsz = H_GET_32 (templ, xnp->descsz);
861f93
+      bfd_byte *descdata = xnp->name + BFD_ALIGN (namesz, 4);
861f93
+
861f93
+      if (H_GET_32 (templ, xnp->type) == NT_GNU_BUILD_ID
861f93
+	  && namesz == sizeof "GNU"
861f93
+	  && memcmp (xnp->name, "GNU", sizeof "GNU") == 0)
861f93
+	{
861f93
+	  size_t size = descsz;
861f93
+	  gdb_byte *data = (void *) descdata;
861f93
+	  struct elf_build_id *retval;
861f93
+
861f93
+	  retval = xmalloc (sizeof *retval - 1 + size);
861f93
+	  retval->size = size;
861f93
+	  memcpy (retval->data, data, size);
861f93
+
861f93
+	  return retval;
861f93
+	}
861f93
+      p = descdata + BFD_ALIGN (descsz, 4);
861f93
+    }
861f93
+  return NULL;
861f93
+}
861f93
+
861f93
+/* Separate debuginfo files have corrupted PHDR but SHDR is correct there.
861f93
+   Locate NT_GNU_BUILD_ID from ABFD and return its content.  */
861f93
 
861f93
 static const struct elf_build_id *
861f93
-build_id_bfd_get (bfd *abfd)
861f93
+build_id_bfd_shdr_get (bfd *abfd)
861f93
 {
861f93
   if (!bfd_check_format (abfd, bfd_object)
861f93
       || bfd_get_flavour (abfd) != bfd_target_elf_flavour
861f93
@@ -1081,6 +1135,348 @@ build_id_bfd_get (bfd *abfd)
861f93
   return elf_tdata (abfd)->build_id;
861f93
 }
861f93
 
861f93
+/* Core files may have missing (corrupt) SHDR but PDHR is correct there.
861f93
+   bfd_elf_bfd_from_remote_memory () has too much overhead by
861f93
+   allocating/reading all the available ELF PT_LOADs.  */
861f93
+
861f93
+static struct elf_build_id *
861f93
+build_id_phdr_get (bfd *templ, bfd_vma loadbase, unsigned e_phnum,
861f93
+		   Elf_Internal_Phdr *i_phdr)
861f93
+{
861f93
+  int i;
861f93
+  struct elf_build_id *retval = NULL;
861f93
+
861f93
+  for (i = 0; i < e_phnum; i++)
861f93
+    if (i_phdr[i].p_type == PT_NOTE && i_phdr[i].p_filesz > 0)
861f93
+      {
861f93
+	Elf_Internal_Phdr *hdr = &i_phdr[i];
861f93
+	gdb_byte *buf;
861f93
+	int err;
861f93
+
861f93
+	buf = xmalloc (hdr->p_filesz);
861f93
+	err = target_read_memory (loadbase + i_phdr[i].p_vaddr, buf,
861f93
+				  hdr->p_filesz);
861f93
+	if (err == 0)
861f93
+	  retval = build_id_buf_get (templ, buf, hdr->p_filesz);
861f93
+	else
861f93
+	  retval = NULL;
861f93
+	xfree (buf);
861f93
+	if (retval != NULL)
861f93
+	  break;
861f93
+      }
861f93
+  return retval;
861f93
+}
861f93
+
861f93
+/* First we validate the file by reading in the ELF header and checking
861f93
+   the magic number.  */
861f93
+
861f93
+static inline bfd_boolean
861f93
+elf_file_p (Elf64_External_Ehdr *x_ehdrp64)
861f93
+{
861f93
+  gdb_assert (sizeof (Elf64_External_Ehdr) >= sizeof (Elf32_External_Ehdr));
861f93
+  gdb_assert (offsetof (Elf64_External_Ehdr, e_ident)
861f93
+	      == offsetof (Elf32_External_Ehdr, e_ident));
861f93
+  gdb_assert (sizeof (((Elf64_External_Ehdr *) 0)->e_ident)
861f93
+	      == sizeof (((Elf32_External_Ehdr *) 0)->e_ident));
861f93
+
861f93
+  return ((x_ehdrp64->e_ident[EI_MAG0] == ELFMAG0)
861f93
+	  && (x_ehdrp64->e_ident[EI_MAG1] == ELFMAG1)
861f93
+	  && (x_ehdrp64->e_ident[EI_MAG2] == ELFMAG2)
861f93
+	  && (x_ehdrp64->e_ident[EI_MAG3] == ELFMAG3));
861f93
+}
861f93
+
861f93
+/* Translate an ELF file header in external format into an ELF file header in
861f93
+   internal format.  */
861f93
+
861f93
+#define H_GET_WORD(bfd, ptr) (is64 ? H_GET_64 (bfd, (ptr))		\
861f93
+				   : H_GET_32 (bfd, (ptr)))
861f93
+#define H_GET_SIGNED_WORD(bfd, ptr) (is64 ? H_GET_S64 (bfd, (ptr))	\
861f93
+					  : H_GET_S32 (bfd, (ptr)))
861f93
+
861f93
+static void
861f93
+elf_swap_ehdr_in (bfd *abfd,
861f93
+		  const Elf64_External_Ehdr *src64,
861f93
+		  Elf_Internal_Ehdr *dst)
861f93
+{
861f93
+  int is64 = bfd_get_arch_size (abfd) == 64;
861f93
+#define SRC(field) (is64 ? src64->field \
861f93
+			 : ((const Elf32_External_Ehdr *) src64)->field)
861f93
+
861f93
+  int signed_vma = get_elf_backend_data (abfd)->sign_extend_vma;
861f93
+  memcpy (dst->e_ident, SRC (e_ident), EI_NIDENT);
861f93
+  dst->e_type = H_GET_16 (abfd, SRC (e_type));
861f93
+  dst->e_machine = H_GET_16 (abfd, SRC (e_machine));
861f93
+  dst->e_version = H_GET_32 (abfd, SRC (e_version));
861f93
+  if (signed_vma)
861f93
+    dst->e_entry = H_GET_SIGNED_WORD (abfd, SRC (e_entry));
861f93
+  else
861f93
+    dst->e_entry = H_GET_WORD (abfd, SRC (e_entry));
861f93
+  dst->e_phoff = H_GET_WORD (abfd, SRC (e_phoff));
861f93
+  dst->e_shoff = H_GET_WORD (abfd, SRC (e_shoff));
861f93
+  dst->e_flags = H_GET_32 (abfd, SRC (e_flags));
861f93
+  dst->e_ehsize = H_GET_16 (abfd, SRC (e_ehsize));
861f93
+  dst->e_phentsize = H_GET_16 (abfd, SRC (e_phentsize));
861f93
+  dst->e_phnum = H_GET_16 (abfd, SRC (e_phnum));
861f93
+  dst->e_shentsize = H_GET_16 (abfd, SRC (e_shentsize));
861f93
+  dst->e_shnum = H_GET_16 (abfd, SRC (e_shnum));
861f93
+  dst->e_shstrndx = H_GET_16 (abfd, SRC (e_shstrndx));
861f93
+
861f93
+#undef SRC
861f93
+}
861f93
+
861f93
+/* Translate an ELF program header table entry in external format into an
861f93
+   ELF program header table entry in internal format.  */
861f93
+
861f93
+static void
861f93
+elf_swap_phdr_in (bfd *abfd,
861f93
+		  const Elf64_External_Phdr *src64,
861f93
+		  Elf_Internal_Phdr *dst)
861f93
+{
861f93
+  int is64 = bfd_get_arch_size (abfd) == 64;
861f93
+#define SRC(field) (is64 ? src64->field					\
861f93
+			 : ((const Elf32_External_Phdr *) src64)->field)
861f93
+
861f93
+  int signed_vma = get_elf_backend_data (abfd)->sign_extend_vma;
861f93
+
861f93
+  dst->p_type = H_GET_32 (abfd, SRC (p_type));
861f93
+  dst->p_flags = H_GET_32 (abfd, SRC (p_flags));
861f93
+  dst->p_offset = H_GET_WORD (abfd, SRC (p_offset));
861f93
+  if (signed_vma)
861f93
+    {
861f93
+      dst->p_vaddr = H_GET_SIGNED_WORD (abfd, SRC (p_vaddr));
861f93
+      dst->p_paddr = H_GET_SIGNED_WORD (abfd, SRC (p_paddr));
861f93
+    }
861f93
+  else
861f93
+    {
861f93
+      dst->p_vaddr = H_GET_WORD (abfd, SRC (p_vaddr));
861f93
+      dst->p_paddr = H_GET_WORD (abfd, SRC (p_paddr));
861f93
+    }
861f93
+  dst->p_filesz = H_GET_WORD (abfd, SRC (p_filesz));
861f93
+  dst->p_memsz = H_GET_WORD (abfd, SRC (p_memsz));
861f93
+  dst->p_align = H_GET_WORD (abfd, SRC (p_align));
861f93
+
861f93
+#undef SRC
861f93
+}
861f93
+
861f93
+#undef H_GET_SIGNED_WORD
861f93
+#undef H_GET_WORD
861f93
+
861f93
+static Elf_Internal_Phdr *
861f93
+elf_get_phdr (bfd *templ, bfd_vma ehdr_vma, unsigned *e_phnum_pointer,
861f93
+              bfd_vma *loadbase_pointer)
861f93
+{
861f93
+  /* sizeof (Elf64_External_Ehdr) >= sizeof (Elf32_External_Ehdr)  */
861f93
+  Elf64_External_Ehdr x_ehdr64;	/* Elf file header, external form */
861f93
+  Elf_Internal_Ehdr i_ehdr;	/* Elf file header, internal form */
861f93
+  bfd_size_type x_phdrs_size;
861f93
+  gdb_byte *x_phdrs_ptr;
861f93
+  Elf_Internal_Phdr *i_phdrs;
861f93
+  int err;
861f93
+  unsigned int i;
861f93
+  bfd_vma loadbase;
861f93
+  int loadbase_set;
861f93
+
861f93
+  gdb_assert (templ != NULL);
861f93
+  gdb_assert (sizeof (Elf64_External_Ehdr) >= sizeof (Elf32_External_Ehdr));
861f93
+
861f93
+  /* Read in the ELF header in external format.  */
861f93
+  err = target_read_memory (ehdr_vma, (bfd_byte *) &x_ehdr64, sizeof x_ehdr64);
861f93
+  if (err)
861f93
+    {
861f93
+      if (build_id_verbose >= BUILD_ID_VERBOSE_BINARY_PARSE)
861f93
+        warning (_("build-id: Error reading ELF header at address 0x%lx"),
861f93
+		 (unsigned long) ehdr_vma);
861f93
+      return NULL;
861f93
+    }
861f93
+
861f93
+  /* Now check to see if we have a valid ELF file, and one that BFD can
861f93
+     make use of.  The magic number must match, the address size ('class')
861f93
+     and byte-swapping must match our XVEC entry.  */
861f93
+
861f93
+  if (! elf_file_p (&x_ehdr64)
861f93
+      || x_ehdr64.e_ident[EI_VERSION] != EV_CURRENT
861f93
+      || !((bfd_get_arch_size (templ) == 64
861f93
+            && x_ehdr64.e_ident[EI_CLASS] == ELFCLASS64)
861f93
+           || (bfd_get_arch_size (templ) == 32
861f93
+	       && x_ehdr64.e_ident[EI_CLASS] == ELFCLASS32)))
861f93
+    {
861f93
+      if (build_id_verbose >= BUILD_ID_VERBOSE_BINARY_PARSE)
861f93
+        warning (_("build-id: Unrecognized ELF header at address 0x%lx"),
861f93
+		 (unsigned long) ehdr_vma);
861f93
+      return NULL;
861f93
+    }
861f93
+
861f93
+  /* Check that file's byte order matches xvec's */
861f93
+  switch (x_ehdr64.e_ident[EI_DATA])
861f93
+    {
861f93
+    case ELFDATA2MSB:		/* Big-endian */
861f93
+      if (! bfd_header_big_endian (templ))
861f93
+	{
861f93
+	  if (build_id_verbose >= BUILD_ID_VERBOSE_BINARY_PARSE)
861f93
+	    warning (_("build-id: Unrecognized "
861f93
+		       "big-endian ELF header at address 0x%lx"),
861f93
+		     (unsigned long) ehdr_vma);
861f93
+	  return NULL;
861f93
+	}
861f93
+      break;
861f93
+    case ELFDATA2LSB:		/* Little-endian */
861f93
+      if (! bfd_header_little_endian (templ))
861f93
+	{
861f93
+	  if (build_id_verbose >= BUILD_ID_VERBOSE_BINARY_PARSE)
861f93
+	    warning (_("build-id: Unrecognized "
861f93
+		       "little-endian ELF header at address 0x%lx"),
861f93
+		     (unsigned long) ehdr_vma);
861f93
+	  return NULL;
861f93
+	}
861f93
+      break;
861f93
+    case ELFDATANONE:		/* No data encoding specified */
861f93
+    default:			/* Unknown data encoding specified */
861f93
+      if (build_id_verbose >= BUILD_ID_VERBOSE_BINARY_PARSE)
861f93
+	warning (_("build-id: Unrecognized "
861f93
+		   "ELF header endianity at address 0x%lx"),
861f93
+		 (unsigned long) ehdr_vma);
861f93
+      return NULL;
861f93
+    }
861f93
+
861f93
+  elf_swap_ehdr_in (templ, &x_ehdr64, &i_ehdr);
861f93
+
861f93
+  /* The file header tells where to find the program headers.
861f93
+     These are what we use to actually choose what to read.  */
861f93
+
861f93
+  if (i_ehdr.e_phentsize != (bfd_get_arch_size (templ) == 64
861f93
+                             ? sizeof (Elf64_External_Phdr)
861f93
+			     : sizeof (Elf32_External_Phdr))
861f93
+      || i_ehdr.e_phnum == 0)
861f93
+    {
861f93
+      if (build_id_verbose >= BUILD_ID_VERBOSE_BINARY_PARSE)
861f93
+	warning (_("build-id: Invalid ELF program headers from the ELF header "
861f93
+		   "at address 0x%lx"), (unsigned long) ehdr_vma);
861f93
+      return NULL;
861f93
+    }
861f93
+
861f93
+  x_phdrs_size = (bfd_get_arch_size (templ) == 64 ? sizeof (Elf64_External_Phdr)
861f93
+						: sizeof (Elf32_External_Phdr));
861f93
+
861f93
+  i_phdrs = xmalloc (i_ehdr.e_phnum * (sizeof *i_phdrs + x_phdrs_size));
861f93
+  x_phdrs_ptr = (void *) &i_phdrs[i_ehdr.e_phnum];
861f93
+  err = target_read_memory (ehdr_vma + i_ehdr.e_phoff, (bfd_byte *) x_phdrs_ptr,
861f93
+			    i_ehdr.e_phnum * x_phdrs_size);
861f93
+  if (err)
861f93
+    {
861f93
+      free (i_phdrs);
861f93
+      if (build_id_verbose >= BUILD_ID_VERBOSE_BINARY_PARSE)
861f93
+        warning (_("build-id: Error reading "
861f93
+		   "ELF program headers at address 0x%lx"),
861f93
+		 (unsigned long) (ehdr_vma + i_ehdr.e_phoff));
861f93
+      return NULL;
861f93
+    }
861f93
+
861f93
+  loadbase = ehdr_vma;
861f93
+  loadbase_set = 0;
861f93
+  for (i = 0; i < i_ehdr.e_phnum; ++i)
861f93
+    {
861f93
+      elf_swap_phdr_in (templ, (Elf64_External_Phdr *)
861f93
+			       (x_phdrs_ptr + i * x_phdrs_size), &i_phdrs[i]);
861f93
+      /* IA-64 vDSO may have two mappings for one segment, where one mapping
861f93
+	 is executable only, and one is read only.  We must not use the
861f93
+	 executable one (PF_R is the first one, PF_X the second one).  */
861f93
+      if (i_phdrs[i].p_type == PT_LOAD && (i_phdrs[i].p_flags & PF_R))
861f93
+	{
861f93
+	  /* Only the first PT_LOAD segment indicates the file bias.
861f93
+	     Next segments may have P_VADDR arbitrarily higher.
861f93
+	     If the first segment has P_VADDR zero any next segment must not
861f93
+	     confuse us, the first one sets LOADBASE certainly enough.  */
861f93
+	  if (!loadbase_set && i_phdrs[i].p_offset == 0)
861f93
+	    {
861f93
+	      loadbase = ehdr_vma - i_phdrs[i].p_vaddr;
861f93
+	      loadbase_set = 1;
861f93
+	    }
861f93
+	}
861f93
+    }
861f93
+
861f93
+  if (build_id_verbose >= BUILD_ID_VERBOSE_BINARY_PARSE)
861f93
+    warning (_("build-id: Found ELF header at address 0x%lx, loadbase 0x%lx"),
861f93
+	     (unsigned long) ehdr_vma, (unsigned long) loadbase);
861f93
+
861f93
+  *e_phnum_pointer = i_ehdr.e_phnum;
861f93
+  *loadbase_pointer = loadbase;
861f93
+  return i_phdrs;
861f93
+}
861f93
+
861f93
+/* BUILD_ID_ADDR_GET gets ADDR located somewhere in the object.
861f93
+   Find the first section before ADDR containing an ELF header.
861f93
+   We rely on the fact the sections from multiple files do not mix.
861f93
+   FIXME: We should check ADDR is contained _inside_ the section with possibly
861f93
+   missing content (P_FILESZ < P_MEMSZ).  These omitted sections are currently
861f93
+   hidden by _BFD_ELF_MAKE_SECTION_FROM_PHDR.  */
861f93
+
861f93
+static CORE_ADDR build_id_addr;
861f93
+struct build_id_addr_sect
861f93
+  {
861f93
+    struct build_id_addr_sect *next;
861f93
+    asection *sect;
861f93
+  };
861f93
+static struct build_id_addr_sect *build_id_addr_sect;
861f93
+
861f93
+static void build_id_addr_candidate (bfd *abfd, asection *sect, void *obj)
861f93
+{
861f93
+  if (build_id_addr >= bfd_section_vma (abfd, sect))
861f93
+    {
861f93
+      struct build_id_addr_sect *candidate;
861f93
+
861f93
+      candidate = xmalloc (sizeof *candidate);
861f93
+      candidate->next = build_id_addr_sect;
861f93
+      build_id_addr_sect = candidate;
861f93
+      candidate->sect = sect;
861f93
+    }
861f93
+}
861f93
+
861f93
+struct elf_build_id *
861f93
+build_id_addr_get (CORE_ADDR addr)
861f93
+{
861f93
+  struct build_id_addr_sect *candidate;
861f93
+  struct elf_build_id *retval = NULL;
861f93
+  Elf_Internal_Phdr *i_phdr = NULL;
861f93
+  bfd_vma loadbase = 0;
861f93
+  unsigned e_phnum = 0;
861f93
+
861f93
+  if (core_bfd == NULL)
861f93
+    return NULL;
861f93
+
861f93
+  build_id_addr = addr;
861f93
+  gdb_assert (build_id_addr_sect == NULL);
861f93
+  bfd_map_over_sections (core_bfd, build_id_addr_candidate, NULL);
861f93
+
861f93
+  /* Sections are sorted in the high-to-low VMAs order.
861f93
+     Stop the search on the first ELF header we find.
861f93
+     Do not continue the search even if it does not contain NT_GNU_BUILD_ID.  */
861f93
+
861f93
+  for (candidate = build_id_addr_sect; candidate != NULL;
861f93
+       candidate = candidate->next)
861f93
+    {
861f93
+      i_phdr = elf_get_phdr (core_bfd,
861f93
+			     bfd_section_vma (core_bfd, candidate->sect),
861f93
+			     &e_phnum, &loadbase);
861f93
+      if (i_phdr != NULL)
861f93
+	break;
861f93
+    }
861f93
+
861f93
+  if (i_phdr != NULL)
861f93
+    {
861f93
+      retval = build_id_phdr_get (core_bfd, loadbase, e_phnum, i_phdr);
861f93
+      xfree (i_phdr);
861f93
+    }
861f93
+
861f93
+  while (build_id_addr_sect != NULL)
861f93
+    {
861f93
+      candidate = build_id_addr_sect;
861f93
+      build_id_addr_sect = candidate->next;
861f93
+      xfree (candidate);
861f93
+    }
861f93
+
861f93
+  return retval;
861f93
+}
861f93
+
861f93
 /* Return if FILENAME has NT_GNU_BUILD_ID matching the CHECK value.  */
861f93
 
861f93
 static int
861f93
@@ -1095,7 +1491,7 @@ build_id_verify (const char *filename, c
861f93
   if (abfd == NULL)
861f93
     return 0;
861f93
 
861f93
-  found = build_id_bfd_get (abfd);
861f93
+  found = build_id_bfd_shdr_get (abfd);
861f93
 
861f93
   if (found == NULL)
861f93
     warning (_("File \"%s\" has no build-id, file skipped"), filename);
861f93
@@ -1112,16 +1508,53 @@ build_id_verify (const char *filename, c
861f93
 }
861f93
 
861f93
 static char *
861f93
-build_id_to_debug_filename (const struct elf_build_id *build_id)
861f93
+link_resolve (const char *symlink, int level)
861f93
+{
861f93
+  char buf[PATH_MAX + 1], *target, *retval;
861f93
+  ssize_t got;
861f93
+
861f93
+  if (level > 10)
861f93
+    return xstrdup (symlink);
861f93
+
861f93
+  got = readlink (symlink, buf, sizeof (buf));
861f93
+  if (got < 0 || got >= sizeof (buf))
861f93
+    return xstrdup (symlink);
861f93
+  buf[got] = '\0';
861f93
+
861f93
+  if (IS_ABSOLUTE_PATH (buf))
861f93
+    target = xstrdup (buf);
861f93
+  else
861f93
+    {
861f93
+      char *dir = ldirname (symlink);
861f93
+
861f93
+      if (dir == NULL)
861f93
+	return xstrdup (symlink);
861f93
+      target = xstrprintf ("%s"
861f93
+#ifndef HAVE_DOS_BASED_FILE_SYSTEM
861f93
+			   "/"
861f93
+#else /* HAVE_DOS_BASED_FILE_SYSTEM */
861f93
+			   "\\"
861f93
+#endif /* HAVE_DOS_BASED_FILE_SYSTEM */
861f93
+			   "%s", dir, buf);
861f93
+    }
861f93
+
861f93
+  retval = link_resolve (target, level + 1);
861f93
+  xfree (target);
861f93
+  return retval;
861f93
+}
861f93
+
861f93
+char *
861f93
+build_id_to_filename (const struct elf_build_id *build_id, char **link_return,
861f93
+		      int add_debug_suffix)
861f93
 {
861f93
   char *link, *debugdir, *retval = NULL;
861f93
+  char *link_all = NULL;
861f93
   VEC (char_ptr) *debugdir_vec;
861f93
   struct cleanup *back_to;
861f93
   int ix;
861f93
 
861f93
   /* DEBUG_FILE_DIRECTORY/.build-id/ab/cdef */
861f93
-  link = alloca (strlen (debug_file_directory) + (sizeof "/.build-id/" - 1) + 1
861f93
-		 + 2 * build_id->size + (sizeof ".debug" - 1) + 1);
861f93
+  link = xmalloc (strlen (debug_file_directory) + 2 * build_id->size + 50);
861f93
 
861f93
   /* Keep backward compatibility so that DEBUG_FILE_DIRECTORY being "" will
861f93
      cause "/.build-id/..." lookups.  */
861f93
@@ -1134,7 +1567,10 @@ build_id_to_debug_filename (const struct
861f93
       size_t debugdir_len = strlen (debugdir);
861f93
       const gdb_byte *data = build_id->data;
861f93
       size_t size = build_id->size;
861f93
-      char *s;
861f93
+      unsigned seqno;
861f93
+      struct stat statbuf_trash;
861f93
+      /* Initialize it just to avoid a GCC false warning.  */
861f93
+      char *s, *link0 = NULL, *link0_resolved;
861f93
 
861f93
       memcpy (link, debugdir, debugdir_len);
861f93
       s = &link[debugdir_len];
861f93
@@ -1148,37 +1584,256 @@ build_id_to_debug_filename (const struct
861f93
 	*s++ = '/';
861f93
       while (size-- > 0)
861f93
 	s += sprintf (s, "%02x", (unsigned) *data++);
861f93
-      strcpy (s, ".debug");
861f93
 
861f93
-      /* lrealpath() is expensive even for the usually non-existent files.  */
861f93
-      if (access (link, F_OK) == 0)
861f93
-	retval = lrealpath (link);
861f93
+      for (seqno = 0;; seqno++)
861f93
+	{
861f93
+	  char *s2;
861f93
+
861f93
+	  if (seqno)
861f93
+	    {
861f93
+	      /* There can be multiple build-id symlinks pointing to real files
861f93
+		 with the same build-id (such as hard links).  Some of the real
861f93
+		 files may not be installed.  */
861f93
+
861f93
+	      s2 = s + sprintf (s, ".%u", seqno);
861f93
+	    }
861f93
+	  else
861f93
+	    s2 = s;
861f93
+
861f93
+	  if (add_debug_suffix)
861f93
+	    strcpy (s2, ".debug");
861f93
+	  else
861f93
+	    *s2 = 0;
861f93
+
861f93
+	  if (!seqno)
861f93
+	    {
861f93
+	      /* If none of the real files is found report as missing file
861f93
+		 always the non-.%u-suffixed file.  */
861f93
+	      link0 = xstrdup (link);
861f93
+	    }
861f93
+
861f93
+	  /* `access' automatically dereferences LINK.  */
861f93
+	  if (lstat (link, &statbuf_trash) != 0)
861f93
+	    {
861f93
+	      /* Stop increasing SEQNO.  */
861f93
+	      break;
861f93
+	    }
861f93
+
861f93
+	  retval = lrealpath (link);
861f93
+
861f93
+	  if (retval != NULL && !build_id_verify (retval, build_id))
861f93
+	    {
861f93
+	      xfree (retval);
861f93
+	      retval = NULL;
861f93
+	    }
861f93
+
861f93
+	  if (retval)
861f93
+	    break;
861f93
+	}
861f93
+
861f93
+      if (retval != NULL)
861f93
+	{
861f93
+	  /* LINK_ALL is not used below in this non-NULL RETVAL case.  */
861f93
+	  xfree (link0);
861f93
+	  break;
861f93
+	}
861f93
+
861f93
+      /* If the symlink has target request to install the target.
861f93
+         BASE-debuginfo.rpm contains the symlink but BASE.rpm may be missing.
861f93
+         https://bugzilla.redhat.com/show_bug.cgi?id=981154  */
861f93
+      link0_resolved = link_resolve (link0, 0);
861f93
+      xfree (link0);
861f93
 
861f93
-      if (retval != NULL && !build_id_verify (retval, build_id))
861f93
+      if (link_all == NULL)
861f93
+	link_all = xstrdup (link0_resolved);
861f93
+      else
861f93
 	{
861f93
-	  xfree (retval);
861f93
-	  retval = NULL;
861f93
+	  size_t len_orig = strlen (link_all);
861f93
+
861f93
+	  link_all = xrealloc (link_all,
861f93
+			       len_orig + 1 + strlen (link0_resolved) + 1);
861f93
+
861f93
+	  /* Use whitespace instead of DIRNAME_SEPARATOR to be compatible with
861f93
+	     its possible use as an argument for installation command.  */
861f93
+	  link_all[len_orig] = ' ';
861f93
+
861f93
+	  strcpy (&link_all[len_orig + 1], link0_resolved);
861f93
 	}
861f93
+      xfree (link0_resolved);
861f93
+    }
861f93
 
861f93
+  if (link_return != NULL)
861f93
+    {
861f93
       if (retval != NULL)
861f93
-	break;
861f93
+	{
861f93
+	  *link_return = link;
861f93
+	  link = NULL;
861f93
+	}
861f93
+      else
861f93
+	{
861f93
+	  *link_return = link_all;
861f93
+	  link_all = NULL;
861f93
+	}
861f93
     }
861f93
+  xfree (link);
861f93
+  xfree (link_all);
861f93
 
861f93
   do_cleanups (back_to);
861f93
   return retval;
861f93
 }
861f93
 
861f93
+/* This MISSING_FILEPAIR_HASH tracker is used only for the duplicite messages
861f93
+     Try to install the hash file ...
861f93
+   avoidance.  */
861f93
+
861f93
+struct missing_filepair
861f93
+  {
861f93
+    char *binary;
861f93
+    char *debug;
861f93
+    char data[1];
861f93
+  };
861f93
+
861f93
+static struct htab *missing_filepair_hash;
861f93
+static struct obstack missing_filepair_obstack;
861f93
+
861f93
+static void *
861f93
+missing_filepair_xcalloc (size_t nmemb, size_t nmemb_size)
861f93
+{
861f93
+  void *retval;
861f93
+  size_t size = nmemb * nmemb_size;
861f93
+
861f93
+  retval = obstack_alloc (&missing_filepair_obstack, size);
861f93
+  memset (retval, 0, size);
861f93
+  return retval;
861f93
+}
861f93
+
861f93
+static hashval_t
861f93
+missing_filepair_hash_func (const struct missing_filepair *elem)
861f93
+{
861f93
+  hashval_t retval = 0;
861f93
+
861f93
+  retval ^= htab_hash_string (elem->binary);
861f93
+  if (elem->debug != NULL)
861f93
+    retval ^= htab_hash_string (elem->debug);
861f93
+
861f93
+  return retval;
861f93
+}
861f93
+
861f93
+static int
861f93
+missing_filepair_eq (const struct missing_filepair *elem1,
861f93
+		       const struct missing_filepair *elem2)
861f93
+{
861f93
+  return strcmp (elem1->binary, elem2->binary) == 0
861f93
+         && ((elem1->debug == NULL) == (elem2->debug == NULL))
861f93
+         && (elem1->debug == NULL || strcmp (elem1->debug, elem2->debug) == 0);
861f93
+}
861f93
+
861f93
+static void
861f93
+missing_filepair_change (void)
861f93
+{
861f93
+  if (missing_filepair_hash != NULL)
861f93
+    {
861f93
+      obstack_free (&missing_filepair_obstack, NULL);
861f93
+      /* All their memory came just from missing_filepair_OBSTACK.  */
861f93
+      missing_filepair_hash = NULL;
861f93
+    }
861f93
+}
861f93
+
861f93
+static void
861f93
+debug_print_executable_changed (void)
861f93
+{
861f93
+  missing_filepair_change ();
861f93
+}
861f93
+
861f93
+/* Notify user the file BINARY with (possibly NULL) associated separate debug
861f93
+   information file DEBUG is missing.  DEBUG may or may not be the build-id
861f93
+   file such as would be:
861f93
+     /usr/lib/debug/.build-id/dd/b1d2ce632721c47bb9e8679f369e2295ce71be.debug
861f93
+   */
861f93
+
861f93
+void
861f93
+debug_print_missing (const char *binary, const char *debug)
861f93
+{
861f93
+  size_t binary_len0 = strlen (binary) + 1;
861f93
+  size_t debug_len0 = debug ? strlen (debug) + 1 : 0;
861f93
+  struct missing_filepair missing_filepair_find;
861f93
+  struct missing_filepair *missing_filepair;
861f93
+  struct missing_filepair **slot;
861f93
+
861f93
+  if (build_id_verbose < BUILD_ID_VERBOSE_FILENAMES)
861f93
+    return;
861f93
+
861f93
+  if (missing_filepair_hash == NULL)
861f93
+    {
861f93
+      obstack_init (&missing_filepair_obstack);
861f93
+      missing_filepair_hash = htab_create_alloc (64,
861f93
+	(hashval_t (*) (const void *)) missing_filepair_hash_func,
861f93
+	(int (*) (const void *, const void *)) missing_filepair_eq, NULL,
861f93
+	missing_filepair_xcalloc, NULL);
861f93
+    }
861f93
+
861f93
+  /* Use MISSING_FILEPAIR_FIND first instead of calling obstack_alloc with
861f93
+     obstack_free in the case of a (rare) match.  The problem is ALLOC_F for
861f93
+     MISSING_FILEPAIR_HASH allocates from MISSING_FILEPAIR_OBSTACK maintenance
861f93
+     structures for MISSING_FILEPAIR_HASH.  Calling obstack_free would possibly
861f93
+     not to free only MISSING_FILEPAIR but also some such structures (allocated
861f93
+     during the htab_find_slot call).  */
861f93
+
861f93
+  missing_filepair_find.binary = (char *) binary;
861f93
+  missing_filepair_find.debug = (char *) debug;
861f93
+  slot = (struct missing_filepair **) htab_find_slot (missing_filepair_hash,
861f93
+						      &missing_filepair_find,
861f93
+						      INSERT);
861f93
+
861f93
+  /* While it may be still printed duplicitely with the missing debuginfo file
861f93
+   * it is due to once printing about the binary file build-id link and once
861f93
+   * about the .debug file build-id link as both the build-id symlinks are
861f93
+   * located in the debuginfo package.  */
861f93
+
861f93
+  if (*slot != NULL)
861f93
+    return;
861f93
+
861f93
+  missing_filepair = obstack_alloc (&missing_filepair_obstack,
861f93
+				      sizeof (*missing_filepair) - 1
861f93
+				      + binary_len0 + debug_len0);
861f93
+  missing_filepair->binary = missing_filepair->data;
861f93
+  memcpy (missing_filepair->binary, binary, binary_len0);
861f93
+  if (debug != NULL)
861f93
+    {
861f93
+      missing_filepair->debug = missing_filepair->binary + binary_len0;
861f93
+      memcpy (missing_filepair->debug, debug, debug_len0);
861f93
+    }
861f93
+  else
861f93
+    missing_filepair->debug = NULL;
861f93
+
861f93
+  *slot = missing_filepair;
861f93
+
861f93
+  /* We do not collect and flush these messages as each such message
861f93
+     already requires its own separate lines.  */
861f93
+
861f93
+  fprintf_unfiltered (gdb_stdlog,
861f93
+		      _("Missing separate debuginfo for %s\n"), binary);
861f93
+  if (debug != NULL)
861f93
+    fprintf_unfiltered (gdb_stdlog, _("Try to install the hash file %s\n"),
861f93
+			debug);
861f93
+}
861f93
+
861f93
 static char *
861f93
-find_separate_debug_file_by_buildid (struct objfile *objfile)
861f93
+find_separate_debug_file_by_buildid (struct objfile *objfile,
861f93
+				     char **build_id_filename_return)
861f93
 {
861f93
   const struct elf_build_id *build_id;
861f93
 
861f93
-  build_id = build_id_bfd_get (objfile->obfd);
861f93
+  if (build_id_filename_return)
861f93
+    *build_id_filename_return = NULL;
861f93
+
861f93
+  build_id = build_id_bfd_shdr_get (objfile->obfd);
861f93
   if (build_id != NULL)
861f93
     {
861f93
       char *build_id_name;
861f93
 
861f93
-      build_id_name = build_id_to_debug_filename (build_id);
861f93
+      build_id_name = build_id_to_filename (build_id, build_id_filename_return,
861f93
+					    1);
861f93
       /* Prevent looping on a stripped .debug file.  */
861f93
       if (build_id_name != NULL
861f93
 	  && filename_cmp (build_id_name, objfile->name) == 0)
861f93
@@ -1188,7 +1843,7 @@ find_separate_debug_file_by_buildid (str
861f93
 	  xfree (build_id_name);
861f93
 	}
861f93
       else if (build_id_name != NULL)
861f93
-        return build_id_name;
861f93
+	return build_id_name;
861f93
     }
861f93
   return NULL;
861f93
 }
861f93
@@ -1426,9 +2081,10 @@ elf_symfile_read (struct objfile *objfil
861f93
 	   && objfile->separate_debug_objfile == NULL
861f93
 	   && objfile->separate_debug_objfile_backlink == NULL)
861f93
     {
861f93
-      char *debugfile;
861f93
+      char *debugfile, *build_id_filename;
861f93
 
861f93
-      debugfile = find_separate_debug_file_by_buildid (objfile);
861f93
+      debugfile = find_separate_debug_file_by_buildid (objfile,
861f93
+						       &build_id_filename);
861f93
 
861f93
       if (debugfile == NULL)
861f93
 	debugfile = find_separate_debug_file_by_debuglink (objfile);
861f93
@@ -1442,6 +2098,12 @@ elf_symfile_read (struct objfile *objfil
861f93
 	  symbol_file_add_separate (abfd, symfile_flags, objfile);
861f93
 	  do_cleanups (cleanup);
861f93
 	}
861f93
+      /* Check if any separate debug info has been extracted out.  */
861f93
+      else if (bfd_get_section_by_name (objfile->obfd, ".gnu_debuglink")
861f93
+	       != NULL)
861f93
+	debug_print_missing (objfile->name, build_id_filename);
861f93
+
861f93
+      xfree (build_id_filename);
861f93
     }
861f93
 }
861f93
 
861f93
@@ -1761,4 +2423,16 @@ _initialize_elfread (void)
861f93
 
861f93
   elf_objfile_gnu_ifunc_cache_data = register_objfile_data ();
861f93
   gnu_ifunc_fns_p = &elf_gnu_ifunc_fns;
861f93
+
861f93
+  add_setshow_zinteger_cmd ("build-id-verbose", no_class, &build_id_verbose,
861f93
+			    _("\
861f93
+Set debugging level of the build-id locator."), _("\
861f93
+Show debugging level of the build-id locator."), _("\
861f93
+Level 1 (default) enables printing the missing debug filenames,\n\
861f93
+level 2 also prints the parsing of binaries to find the identificators."),
861f93
+			    NULL,
861f93
+			    show_build_id_verbose,
861f93
+			    &setlist, &showlist);
861f93
+
861f93
+  observer_attach_executable_changed (debug_print_executable_changed);
861f93
 }
861f93
Index: gdb-7.6/gdb/symfile.h
861f93
===================================================================
861f93
--- gdb-7.6.orig/gdb/symfile.h	2013-02-03 17:20:18.000000000 +0100
861f93
+++ gdb-7.6/gdb/symfile.h	2013-08-06 16:57:14.919518099 +0200
861f93
@@ -593,6 +593,12 @@ void free_symfile_segment_data (struct s
861f93
 
861f93
 extern struct cleanup *increment_reading_symtab (void);
861f93
 
861f93
+/* build-id support.  */
861f93
+extern struct elf_build_id *build_id_addr_get (CORE_ADDR addr);
861f93
+extern char *build_id_to_filename (const struct elf_build_id *build_id,
861f93
+				   char **link_return, int add_debug_suffix);
861f93
+extern void debug_print_missing (const char *binary, const char *debug);
861f93
+
861f93
 /* From dwarf2read.c */
861f93
 
861f93
 /* Names for a dwarf2 debugging section.  The field NORMAL is the normal
861f93
Index: gdb-7.6/gdb/testsuite/lib/gdb.exp
861f93
===================================================================
861f93
--- gdb-7.6.orig/gdb/testsuite/lib/gdb.exp	2013-08-06 16:57:14.298517226 +0200
861f93
+++ gdb-7.6/gdb/testsuite/lib/gdb.exp	2013-08-06 16:57:14.920518100 +0200
861f93
@@ -1482,6 +1482,16 @@ proc default_gdb_start { } {
861f93
 	    warning "Couldn't set the width to 0."
861f93
 	}
861f93
     }
861f93
+    # Turn off the missing warnings as the testsuite does not expect it.
861f93
+    send_gdb "set build-id-verbose 0\n"
861f93
+    gdb_expect 10 {
861f93
+	-re "$gdb_prompt $" {
861f93
+	    verbose "Disabled the missing debug infos warnings." 2
861f93
+	}
861f93
+	timeout {
861f93
+	    warning "Could not disable the missing debug infos warnings.."
861f93
+	}
861f93
+    }
861f93
     return 0;
861f93
 }
861f93
 
861f93
Index: gdb-7.6/gdb/testsuite/lib/mi-support.exp
861f93
===================================================================
861f93
--- gdb-7.6.orig/gdb/testsuite/lib/mi-support.exp	2013-01-22 00:57:59.000000000 +0100
861f93
+++ gdb-7.6/gdb/testsuite/lib/mi-support.exp	2013-08-06 16:57:14.920518100 +0200
861f93
@@ -212,6 +212,16 @@ proc default_mi_gdb_start { args } {
861f93
 	    warning "Couldn't set the width to 0."
861f93
 	}
861f93
     }
861f93
+    # Turn off the missing warnings as the testsuite does not expect it.
861f93
+    send_gdb "190-gdb-set build-id-verbose 0\n"
861f93
+    gdb_expect 10 {
861f93
+	-re ".*190-gdb-set build-id-verbose 0\r\n190\\\^done\r\n$mi_gdb_prompt$" {
861f93
+	    verbose "Disabled the missing debug infos warnings." 2
861f93
+	}
861f93
+	timeout {
861f93
+	    warning "Could not disable the missing debug infos warnings.."
861f93
+	}
861f93
+    }
861f93
     # If allowing the inferior to have its own PTY then assign the inferior
861f93
     # its own terminal device here.
861f93
     if { $separate_inferior_pty } {
861f93
Index: gdb-7.6/gdb/objfiles.h
861f93
===================================================================
861f93
--- gdb-7.6.orig/gdb/objfiles.h	2013-02-28 20:00:31.000000000 +0100
861f93
+++ gdb-7.6/gdb/objfiles.h	2013-08-06 16:57:14.920518100 +0200
861f93
@@ -432,6 +432,10 @@ struct objfile
861f93
 
861f93
 #define OBJF_MAINLINE (1 << 5)
861f93
 
861f93
+/* This file was loaded according to the BUILD_ID_CORE_LOADS rules.  */
861f93
+
861f93
+#define OBJF_BUILD_ID_CORE_LOADED (1 << 12)
861f93
+
861f93
 /* The object file that contains the runtime common minimal symbols
861f93
    for SunOS4.  Note that this objfile has no associated BFD.  */
861f93
 
861f93
Index: gdb-7.6/gdb/testsuite/gdb.base/corefile.exp
861f93
===================================================================
861f93
--- gdb-7.6.orig/gdb/testsuite/gdb.base/corefile.exp	2013-01-01 07:33:25.000000000 +0100
861f93
+++ gdb-7.6/gdb/testsuite/gdb.base/corefile.exp	2013-08-06 16:57:14.921518102 +0200
861f93
@@ -256,3 +256,33 @@ if ![is_remote target] {
861f93
 
861f93
     gdb_exit
861f93
 }
861f93
+
861f93
+
861f93
+# Test auto-loading of binary files through build-id from the core file.
861f93
+set buildid [build_id_debug_filename_get $binfile]
861f93
+set wholetest "binfile found by build-id"
861f93
+if {$buildid == ""} {
861f93
+    untested "$wholetest (binary has no build-id)"
861f93
+} else {
861f93
+    gdb_exit
861f93
+    gdb_start
861f93
+
861f93
+    regsub {\.debug$} $buildid {} buildid
861f93
+    set debugdir ${objdir}/${subdir}/${testfile}-debugdir
861f93
+    file delete -force -- $debugdir
861f93
+    file mkdir $debugdir/[file dirname $buildid]
861f93
+    file copy $binfile $debugdir/$buildid
861f93
+
861f93
+    set test "show debug-file-directory"
861f93
+    gdb_test_multiple $test $test {
861f93
+	-re "The directory where separate debug symbols are searched for is \"(.*)\"\\.\r\n$gdb_prompt $" {
861f93
+	    set debugdir_orig $expect_out(1,string)
861f93
+	    pass $test
861f93
+	}
861f93
+    }
861f93
+    gdb_test_no_output "set debug-file-directory $debugdir:$debugdir_orig" "set debug-file-directory"
861f93
+    gdb_test "show build-id-core-loads" {Whether CORE-FILE loads the build-id associated files automatically is on\.}
861f93
+    gdb_test "core-file $corefile" "\r\nProgram terminated with .*" "core-file without executable"
861f93
+    gdb_test "info files" "Local exec file:\r\n\[ \t\]*`[string_to_regexp $debugdir/$buildid]', file type .*"
861f93
+    pass $wholetest
861f93
+}