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