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

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