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

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