4e2e788
http://sources.redhat.com/ml/gdb-patches/2007-08/msg00478.html
4e2e788
v2
4e2e788
[ Backported for GDB-6.6. ]
4e2e788
4e2e788
2007-08-26  Jan Kratochvil  <jan.kratochvil@redhat.com>
4e2e788
4e2e788
	* Makefile.in (symfile.o): Update dependencies.
4e2e788
	* symfile.c (symbol_file_add_with_addrs_or_offsets): Initialize the
4e2e788
	DEBUGFILE variable.  FIND_SEPARATE_DEBUG_FILE called only if !PSYMTABS.
4e2e788
	(struct build_id): New structure.
4e2e788
	(build_id_bfd_get, build_id_verify, build_id_to_debug_filename): New.
4e2e788
	(find_separate_debug_file): New variable BUILD_ID.
4e2e788
	Call BUILD_ID_BFD_GET with BUILD_ID_TO_DEBUG_FILENAME as the first try.
4e2e788
4e2e788
2007-08-26  Jan Kratochvil  <jan.kratochvil@redhat.com>
4e2e788
4e2e788
	* lib/gdb.exp (build_id_debug_filename_get): New function.
4e2e788
	* gdb.base/sepdebug.exp: Reflect the changes in the heading comment.
4e2e788
	Remove the generate DEBUG file for the future testcase runs.
4e2e788
	New testcase for the NT_GNU_BUILD_ID retrieval.
4e2e788
	Move the final testing step to ...
4e2e788
	(test_different_dir): ... a new function.
4e2e788
	New parameter XFAIL to XFAIL all the tests performed.
4e2e788
	New parameter TEST_DIFFERENT_DIR parametrizing the directory.
4e2e788
	New parameter TYPE to PF_PREFIX all the tests performed.
4e2e788
4e2e788
2007-08-26  Jan Kratochvil  <jan.kratochvil@redhat.com>
4e2e788
4e2e788
	* gdb.texinfo (Separate Debug Files): Included a BUILD ID description.
4e2e788
	Enlisted BUILD ID to the debug file searching example.
4e2e788
	Included a BUILD ID `.note.gnu.build-id' section description.
4e2e788
	Updated/added the debug files splitting instructions for OBJCOPY.
4e2e788
4e2e788
diff -u -rup gdb-6.6-orig/gdb/Makefile.in gdb-6.6/gdb/Makefile.in
4e2e788
--- gdb-6.6-orig/gdb/Makefile.in	2007-08-28 14:32:18.000000000 +0200
4e2e788
+++ gdb-6.6/gdb/Makefile.in	2007-08-28 14:33:56.000000000 +0200
4e2e788
@@ -2785,7 +2785,7 @@ symfile.o: symfile.c $(defs_h) $(bfdlink
4e2e788
 	$(gdb_stabs_h) $(gdb_obstack_h) $(completer_h) $(bcache_h) \
4e2e788
 	$(hashtab_h) $(readline_h) $(gdb_assert_h) $(block_h) \
4e2e788
 	$(gdb_string_h) $(gdb_stat_h) $(observer_h) $(exec_h) \
4e2e788
-	$(parser_defs_h)
4e2e788
+	$(parser_defs_h) $(elf_bfd_h)
4e2e788
 symfile-mem.o: symfile-mem.c $(defs_h) $(symtab_h) $(gdbcore_h) \
4e2e788
 	$(objfiles_h) $(exceptions_h) $(gdbcmd_h) $(target_h) $(value_h) \
4e2e788
 	$(symfile_h) $(observer_h) $(auxv_h) $(elf_common_h)
4e2e788
diff -u -rup gdb-6.6-orig/gdb/doc/gdb.texinfo gdb-6.6/gdb/doc/gdb.texinfo
4e2e788
--- gdb-6.6-orig/gdb/doc/gdb.texinfo	2007-08-28 14:32:18.000000000 +0200
4e2e788
+++ gdb-6.6/gdb/doc/gdb.texinfo	2007-08-28 14:33:03.000000000 +0200
4e2e788
@@ -11857,18 +11857,32 @@ than the executable code itself --- some
4e2e788
 information for their executables in separate files, which users can
4e2e788
 install only when they need to debug a problem.
4e2e788
 
4e2e788
-If an executable's debugging information has been extracted to a
4e2e788
-separate file, the executable should contain a @dfn{debug link} giving
4e2e788
-the name of the debugging information file (with no directory
4e2e788
-components), and a checksum of its contents.  (The exact form of a
4e2e788
-debug link is described below.)  If the full name of the directory
4e2e788
-containing the executable is @var{execdir}, and the executable has a
4e2e788
-debug link that specifies the name @var{debugfile}, then @value{GDBN}
4e2e788
-will automatically search for the debugging information file in three
4e2e788
-places:
4e2e788
+There are two identificators how the separate debug file may be found:
4e2e788
 
4e2e788
 @itemize @bullet
4e2e788
 @item
4e2e788
+@dfn{debug link} is present only in the executable if its debug information has
4e2e788
+been split out.  It is not present in the separate debug file.  It provides the
4e2e788
+separate debug file filename, usually as @file{executable.debug}.
4e2e788
+@item
4e2e788
+@dfn{build id} is present in all the files (if the operating system supports
4e2e788
+it).  The executable file and its separate debug file have the same unique
4e2e788
+@dfn{build id} content.
4e2e788
+@end itemize
4e2e788
+
4e2e788
+If the full name of the directory containing the executable is @var{execdir},
4e2e788
+the executable has a debug link that specifies the name @var{debugfile},
4e2e788
+@var{bu} is the first byte (two hexadecimal characters) of the build id
4e2e788
+content, @var{ild-id} are the remaining bytes / hexadecimal characters and
4e2e788
+@var{globaldebugdir} is the global debug file directory then @value{GDBN} will
4e2e788
+automatically search for the debugging information file in four places:
4e2e788
+
4e2e788
+@itemize @bullet
4e2e788
+@item
4e2e788
+a specific file in the subdirectory of the global debug file directory
4e2e788
+according to the @dfn{build id} content (if present), the file tried is
4e2e788
+@file{@var{globaldebugdir}/.debug-id/@var{bu}/@var{ild-id}.debug}.
4e2e788
+@item
4e2e788
 the directory containing the executable file (that is, it will look
4e2e788
 for a file named @file{@var{execdir}/@var{debugfile}},
4e2e788
 @item
4e2e788
@@ -11883,15 +11897,17 @@ executable's full path, and the name fro
4e2e788
 @end itemize
4e2e788
 @noindent
4e2e788
 @value{GDBN} checks under each of these names for a debugging
4e2e788
-information file whose checksum matches that given in the link, and
4e2e788
-reads the debugging information from the first one it finds.
4e2e788
-
4e2e788
-So, for example, if you ask @value{GDBN} to debug @file{/usr/bin/ls},
4e2e788
-which has a link containing the name @file{ls.debug}, and the global
4e2e788
-debug directory is @file{/usr/lib/debug}, then @value{GDBN} will look
4e2e788
-for debug information in @file{/usr/bin/ls.debug},
4e2e788
-@file{/usr/bin/.debug/ls.debug}, and
4e2e788
-@file{/usr/lib/debug/usr/bin/ls.debug}.
4e2e788
+information file with build id content matching the build id content of the
4e2e788
+executable file - or - whose checksum matches the one given in the link in the
4e2e788
+debug link case.  In each case @value{GDBN} reads the debugging information
4e2e788
+from the first debug file it finds.
4e2e788
+
4e2e788
+So, for example, if you ask @value{GDBN} to debug @file{/usr/bin/ls}, which has
4e2e788
+a @dfn{debug link} containing the name @file{ls.debug}, its @dfn{build id}
4e2e788
+value in hexadecimal is @code{abcdef} and the global debug directory is
4e2e788
+@file{/usr/lib/debug}, then @value{GDBN} will look for debug information in
4e2e788
+@file{/usr/lib/debug/.build-id/ab/cdef.debug}, @file{/usr/bin/ls.debug},
4e2e788
+@file{/usr/bin/.debug/ls.debug}, and @file{/usr/lib/debug/usr/bin/ls.debug}.
4e2e788
 
4e2e788
 You can set the global debugging info directory's name, and view the
4e2e788
 name @value{GDBN} is currently using.
4e2e788
@@ -11933,6 +11949,16 @@ Any executable file format can carry a d
4e2e788
 contain a section named @code{.gnu_debuglink} with the contents
4e2e788
 described above.
4e2e788
 
4e2e788
+@cindex @code{.note.gnu.build-id} sections
4e2e788
+@cindex build id
4e2e788
+Build id is a special section of the executable file named
4e2e788
+@code{.note.gnu.build-id}.  The section contains unique identification hash
4e2e788
+derived from the built files - it remains the same across multiple builds of
4e2e788
+the same build tree.  The default algorithm SHA1 produces 160 bits (40
4e2e788
+hexadecimal characters) of the content.  The same section and value is present
4e2e788
+in the original built binary with symbols, in its stripped variant and in the
4e2e788
+separate debug information file.
4e2e788
+
4e2e788
 The debugging information file itself should be an ordinary
4e2e788
 executable, containing a full set of linker symbols, sections, and
4e2e788
 debugging information.  The sections of the debugging information file
4e2e788
@@ -11940,18 +11966,21 @@ should have the same names, addresses an
4e2e788
 but they need not contain any data --- much like a @code{.bss} section
4e2e788
 in an ordinary executable.
4e2e788
 
4e2e788
-As of December 2002, there is no standard GNU utility to produce
4e2e788
-separated executable / debugging information file pairs.  Ulrich
4e2e788
-Drepper's @file{elfutils} package, starting with version 0.53,
4e2e788
-contains a version of the @code{strip} command such that the command
4e2e788
-@kbd{strip foo -f foo.debug} removes the debugging information from
4e2e788
-the executable file @file{foo}, places it in the file
4e2e788
-@file{foo.debug}, and leaves behind a debug link in @file{foo}.
4e2e788
-
4e2e788
-Since there are many different ways to compute CRC's (different
4e2e788
-polynomials, reversals, byte ordering, etc.), the simplest way to
4e2e788
-describe the CRC used in @code{.gnu_debuglink} sections is to give the
4e2e788
-complete code for a function that computes it:
4e2e788
+@sc{gnu} binary utilities contain the @samp{objcopy} utility able to produce
4e2e788
+the separated executable / debugging information file pairs by commands
4e2e788
+@kbd{objcopy --only-keep-debug foo foo.debug; strip -g foo; objcopy
4e2e788
+--add-gnu-debuglink="foo.debug" "foo"}.  These commands remove the debugging
4e2e788
+information from the executable file @file{foo}, place it in the file
4e2e788
+@file{foo.debug}, and leave behind a debug link in @file{foo}.  Ulrich
4e2e788
+Drepper's @file{elfutils} package, starting with version 0.53, contains
4e2e788
+a version of the @code{strip} command such that the command @kbd{strip foo -f
4e2e788
+foo.debug} has the same functionality as the three commands above.
4e2e788
+
4e2e788
+Since there are many different ways to compute CRC's for the debug link
4e2e788
+(different polynomials, reversals, byte ordering, etc.).  This computation does
4e2e788
+not apply to the build id section.  The simplest way to describe the CRC used
4e2e788
+in @code{.gnu_debuglink} sections is to give the complete code for a function
4e2e788
+that computes it:
4e2e788
 
4e2e788
 @kindex gnu_debuglink_crc32
4e2e788
 @smallexample
4e2e788
diff -u -rup gdb-6.6-orig/gdb/symfile.c gdb-6.6/gdb/symfile.c
4e2e788
--- gdb-6.6-orig/gdb/symfile.c	2007-08-28 14:32:17.000000000 +0200
4e2e788
+++ gdb-6.6/gdb/symfile.c	2007-08-28 14:34:12.000000000 +0200
4e2e788
@@ -53,6 +53,7 @@
4e2e788
 #include "observer.h"
4e2e788
 #include "exec.h"
4e2e788
 #include "parser-defs.h"
4e2e788
+#include "elf-bfd.h"
4e2e788
 
4e2e788
 #include <sys/types.h>
4e2e788
 #include <fcntl.h>
4e2e788
@@ -904,7 +905,7 @@ symbol_file_add_with_addrs_or_offsets (b
4e2e788
 {
4e2e788
   struct objfile *objfile;
4e2e788
   struct partial_symtab *psymtab;
4e2e788
-  char *debugfile;
4e2e788
+  char *debugfile = NULL;
4e2e788
   struct section_addr_info *orig_addrs = NULL;
4e2e788
   struct cleanup *my_cleanups;
4e2e788
   const char *name = bfd_get_filename (abfd);
4e2e788
@@ -968,7 +969,11 @@ symbol_file_add_with_addrs_or_offsets (b
4e2e788
 	}
4e2e788
     }
4e2e788
 
4e2e788
-  debugfile = find_separate_debug_file (objfile);
4e2e788
+  /* If the file has its own symbol tables it has no separate debug info.
4e2e788
+     `.dynsym'/`.symtab' go to MSYMBOLS, `.debug_info' goes to SYMTABS/PSYMTABS.
4e2e788
+     `.gnu_debuglink' may no longer be present with `.note.gnu.build-id'.  */
4e2e788
+  if (objfile->psymtabs == NULL)
4e2e788
+    debugfile = find_separate_debug_file (objfile);
4e2e788
   if (debugfile)
4e2e788
     {
4e2e788
       if (addrs != NULL)
4e2e788
@@ -1115,6 +1120,99 @@ symbol_file_clear (int from_tty)
4e2e788
       printf_unfiltered (_("No symbol file now.\n"));
4e2e788
 }
4e2e788
 
4e2e788
+struct build_id
4e2e788
+  {
4e2e788
+    size_t size;
4e2e788
+    gdb_byte data[1];
4e2e788
+  };
4e2e788
+
4e2e788
+/* Locate NT_GNU_BUILD_ID from ABFD and return its content.  */
4e2e788
+
4e2e788
+static struct build_id *
4e2e788
+build_id_bfd_get (bfd *abfd)
4e2e788
+{
4e2e788
+  struct build_id *retval;
4e2e788
+
4e2e788
+  if (!bfd_check_format (abfd, bfd_object)
4e2e788
+      || bfd_get_flavour (abfd) != bfd_target_elf_flavour
4e2e788
+      || elf_tdata (abfd)->build_id == NULL)
4e2e788
+    return NULL;
4e2e788
+
4e2e788
+  retval = xmalloc (sizeof *retval - 1 + elf_tdata (abfd)->build_id_size);
4e2e788
+  retval->size = elf_tdata (abfd)->build_id_size;
4e2e788
+  memcpy (retval->data, elf_tdata (abfd)->build_id, retval->size);
4e2e788
+
4e2e788
+  return retval;
4e2e788
+}
4e2e788
+
4e2e788
+/* Return if FILENAME has NT_GNU_BUILD_ID matching the CHECK value.  */
4e2e788
+
4e2e788
+static int
4e2e788
+build_id_verify (const char *filename, struct build_id *check)
4e2e788
+{
4e2e788
+  bfd *abfd;
4e2e788
+  struct build_id *found = NULL;
4e2e788
+  int retval = 0;
4e2e788
+
4e2e788
+  /* We expect to be silent on the non-existing files.  */
4e2e788
+  abfd = bfd_openr (filename, gnutarget);
4e2e788
+  if (abfd == NULL)
4e2e788
+    return 0;
4e2e788
+
4e2e788
+  found = build_id_bfd_get (abfd);
4e2e788
+
4e2e788
+  if (found == NULL)
4e2e788
+    warning (_("File \"%s\" has no build-id, file skipped"), filename);
4e2e788
+  else if (found->size != check->size
4e2e788
+           || memcmp (found->data, check->data, found->size) != 0)
4e2e788
+    warning (_("File \"%s\" has a different build-id, file skipped"), filename);
4e2e788
+  else
4e2e788
+    retval = 1;
4e2e788
+
4e2e788
+  if (!bfd_close (abfd))
4e2e788
+    warning (_("cannot close \"%s\": %s"), filename,
4e2e788
+	     bfd_errmsg (bfd_get_error ()));
4e2e788
+  return retval;
4e2e788
+}
4e2e788
+
4e2e788
+static char *debug_file_directory = NULL;
4e2e788
+
4e2e788
+static char *
4e2e788
+build_id_to_debug_filename (struct build_id *build_id)
4e2e788
+{
4e2e788
+  char *link, *s, *retval = NULL;
4e2e788
+  gdb_byte *data = build_id->data;
4e2e788
+  size_t size = build_id->size;
4e2e788
+
4e2e788
+  /* DEBUG_FILE_DIRECTORY/.build-id/ab/cdef */
4e2e788
+  link = xmalloc (strlen (debug_file_directory) + (sizeof "/.build-id/" - 1) + 1
4e2e788
+		  + 2 * size + (sizeof ".debug" - 1) + 1);
4e2e788
+  s = link + sprintf (link, "%s/.build-id/", debug_file_directory);
4e2e788
+  if (size > 0)
4e2e788
+    {
4e2e788
+      size--;
4e2e788
+      s += sprintf (s, "%02x", (unsigned) *data++);
4e2e788
+    }
4e2e788
+  if (size > 0)
4e2e788
+    *s++ = '/';
4e2e788
+  while (size-- > 0)
4e2e788
+    s += sprintf (s, "%02x", (unsigned) *data++);
4e2e788
+  strcpy (s, ".debug");
4e2e788
+
4e2e788
+  /* lrealpath() is expensive even for the usually non-existent files.  */
4e2e788
+  if (access (link, F_OK) == 0)
4e2e788
+    retval = lrealpath (link);
4e2e788
+  xfree (link);
4e2e788
+
4e2e788
+  if (retval != NULL && !build_id_verify (retval, build_id))
4e2e788
+    {
4e2e788
+      xfree (retval);
4e2e788
+      retval = NULL;
4e2e788
+    }
4e2e788
+
4e2e788
+  return retval;
4e2e788
+}
4e2e788
+
4e2e788
 static char *
4e2e788
 get_debug_link_info (struct objfile *objfile, unsigned long *crc32_out)
4e2e788
 {
4e2e788
@@ -1278,7 +1280,6 @@ separate_debug_file_exists (const char *
4e2e788
   return 1;
4e2e788
 }
4e2e788
 
4e2e788
-static char *debug_file_directory = NULL;
4e2e788
 static void
4e2e788
 show_debug_file_directory (struct ui_file *file, int from_tty,
4e2e788
 			   struct cmd_list_element *c, const char *value)
4e2e788
@@ -1207,6 +1303,25 @@ find_separate_debug_file (struct objfile
4e2e788
   bfd_size_type debuglink_size;
4e2e788
   unsigned long crc32;
4e2e788
   int i;
4e2e788
+  struct build_id *build_id;
4e2e788
+
4e2e788
+  build_id = build_id_bfd_get (objfile->obfd);
4e2e788
+  if (build_id != NULL)
4e2e788
+    {
4e2e788
+      char *build_id_name;
4e2e788
+
4e2e788
+      build_id_name = build_id_to_debug_filename (build_id);
4e2e788
+      free (build_id);
4e2e788
+      /* Prevent looping on a stripped .debug file.  */
4e2e788
+      if (build_id_name != NULL && strcmp (build_id_name, objfile->name) == 0)
4e2e788
+        {
4e2e788
+	  warning (_("\"%s\": separate debug info file has no debug info"),
4e2e788
+		   build_id_name);
4e2e788
+	  xfree (build_id_name);
4e2e788
+	}
4e2e788
+      else if (build_id_name != NULL)
4e2e788
+        return build_id_name;
4e2e788
+    }
4e2e788
 
4e2e788
   basename = get_debug_link_info (objfile, &crc32);
4e2e788
 
4e2e788
diff -u -rup gdb-6.6-orig/gdb/testsuite/gdb.base/sepdebug.exp gdb-6.6/gdb/testsuite/gdb.base/sepdebug.exp
4e2e788
--- gdb-6.6-orig/gdb/testsuite/gdb.base/sepdebug.exp	2007-08-28 14:32:17.000000000 +0200
4e2e788
+++ gdb-6.6/gdb/testsuite/gdb.base/sepdebug.exp	2007-08-28 14:46:38.000000000 +0200
4e2e788
@@ -21,11 +21,14 @@
4e2e788
 
4e2e788
 # Based on break.exp, written by Rob Savoye. (rob@cygnus.com)
4e2e788
 # Modified to test gdb's handling of separate debug info files.
4e2e788
+# Modified to test gdb's handling of a debug-id retrieval.
4e2e788
 
4e2e788
 # This file has two parts. The first is testing that gdb behaves
4e2e788
 # normally after reading in an executable and its corresponding
4e2e788
 # separate debug file. The second moves the .debug file to a different
4e2e788
 # location and tests the "set debug-file-directory" command.
4e2e788
+# The third is for testing build-id retrievel by finding the separate
4e2e788
+# ".debug-id/ab/cdef.debug" file.
4e2e788
 
4e2e788
 
4e2e788
 if $tracelevel then {
4e2e788
@@ -851,110 +854,6 @@ proc test_next_with_recursion {} { 
4e2e788
 test_next_with_recursion
4e2e788
 
4e2e788
 
4e2e788
-#********
4e2e788
-
4e2e788
-# now move the .debug file to a different location so that we can test
4e2e788
-# the "set debug-file-directory" command.
4e2e788
-  
4e2e788
-remote_exec build "mv ${objdir}/${subdir}/.debug/${testfile}.debug ${objdir}/${subdir}"
4e2e788
-gdb_exit
4e2e788
-gdb_start
4e2e788
-gdb_reinitialize_dir $srcdir/$subdir
4e2e788
-gdb_test "set debug-file-directory ${objdir}/${subdir}" ".*" "set separate debug location"
4e2e788
-gdb_load ${binfile}
4e2e788
-
4e2e788
-if [target_info exists gdb_stub] {
4e2e788
-    gdb_step_for_stub;
4e2e788
-}
4e2e788
-
4e2e788
-#
4e2e788
-# test break at function
4e2e788
-#
4e2e788
-gdb_test "break main" \
4e2e788
-    "Breakpoint.*at.* file .*$srcfile, line.*" \
4e2e788
-    "breakpoint function, optimized file"
4e2e788
-
4e2e788
-#
4e2e788
-# test break at function
4e2e788
-#
4e2e788
-gdb_test "break marker4" \
4e2e788
-    "Breakpoint.*at.* file .*$srcfile, line.*" \
4e2e788
-    "breakpoint small function, optimized file"
4e2e788
-
4e2e788
-#
4e2e788
-# run until the breakpoint at main is hit. For non-stubs-using targets.
4e2e788
-#
4e2e788
-if ![target_info exists use_gdb_stub] {
4e2e788
-  if [istarget "*-*-vxworks*"] then {
4e2e788
-    send_gdb "run vxmain \"2\"\n"
4e2e788
-    set timeout 120
4e2e788
-    verbose "Timeout is now $timeout seconds" 2
4e2e788
-  } else {
4e2e788
-	send_gdb "run\n"
4e2e788
-  }
4e2e788
-  gdb_expect {
4e2e788
-    -re "The program .* has been started already.*y or n. $" {
4e2e788
-	send_gdb "y\n"
4e2e788
-	exp_continue
4e2e788
-    }
4e2e788
-    -re "Starting program.*Breakpoint \[0-9\]+,.*main .*argc.*argv.* at .*$srcfile:$bp_location6.*$bp_location6\[\t \]+if .argc.* \{.*$gdb_prompt $"\
4e2e788
-	                    { pass "run until function breakpoint, optimized file" }
4e2e788
-    -re "Starting program.*Breakpoint \[0-9\]+,.*main .*argc.*argv.* at .*$gdb_prompt $"\
4e2e788
-	                    { pass "run until function breakpoint, optimized file (code motion)" }
4e2e788
-    -re ".*$gdb_prompt $"       { fail "run until function breakpoint, optimized file" }
4e2e788
-    timeout	            { fail "run until function breakpoint, optimized file (timeout)" }
4e2e788
-  }
4e2e788
-} else {
4e2e788
-    if ![target_info exists gdb_stub] {
4e2e788
-	gdb_test continue ".*Continuing\\..*Breakpoint \[0-9\]+, main \\(argc=.*, argv=.*, envp=.*\\) at .*$srcfile:$bp_location6.*$bp_location6\[\t \]+if .argc.*\{.*" "stub continue, optimized file"
4e2e788
-    }
4e2e788
-}
4e2e788
-
4e2e788
-#
4e2e788
-# run until the breakpoint at a small function
4e2e788
-#
4e2e788
-
4e2e788
-#
4e2e788
-# Add a second pass pattern.  The behavior differs here between stabs
4e2e788
-# and dwarf for one-line functions.  Stabs preserves two line symbols
4e2e788
-# (one before the prologue and one after) with the same line number, 
4e2e788
-# but dwarf regards these as duplicates and discards one of them.
4e2e788
-# Therefore the address after the prologue (where the breakpoint is)
4e2e788
-# has no exactly matching line symbol, and GDB reports the breakpoint
4e2e788
-# as if it were in the middle of a line rather than at the beginning.
4e2e788
-
4e2e788
-set bp_location13 [gdb_get_line_number "set breakpoint 13 here"]
4e2e788
-set bp_location14 [gdb_get_line_number "set breakpoint 14 here"]
4e2e788
-send_gdb "continue\n"
4e2e788
-gdb_expect {
4e2e788
-    -re "Breakpoint $decimal, marker4 \\(d=177601976\\) at .*$srcfile:$bp_location13\[\r\n\]+$bp_location13\[\t \]+void marker4.*" {
4e2e788
-	pass "run until breakpoint set at small function, optimized file"
4e2e788
-    }
4e2e788
-    -re "Breakpoint $decimal, $hex in marker4 \\(d=177601976\\) at .*$srcfile:$bp_location13\[\r\n\]+$bp_location13\[\t \]+void marker4.*" {
4e2e788
-	pass "run until breakpoint set at small function, optimized file"
4e2e788
-    }
4e2e788
-    -re "Breakpoint $decimal, marker4 \\(d=177601976\\) at .*$srcfile:$bp_location14\[\r\n\]+$bp_location14\[\t \]+void marker4.*" {
4e2e788
-        # marker4() is defined at line 46 when compiled with -DPROTOTYPES
4e2e788
-	pass "run until breakpoint set at small function, optimized file (line bp_location14)"
4e2e788
-    }
4e2e788
-    -re ".*$gdb_prompt " {
4e2e788
-	fail "run until breakpoint set at small function, optimized file"
4e2e788
-    }
4e2e788
-    timeout {
4e2e788
-	fail "run until breakpoint set at small function, optimized file (timeout)"
4e2e788
-    }
4e2e788
-}
4e2e788
-
4e2e788
-
4e2e788
-# Reset the default arguments for VxWorks
4e2e788
-if [istarget "*-*-vxworks*"] {
4e2e788
-    set timeout 10
4e2e788
-    verbose "Timeout is now $timeout seconds" 2
4e2e788
-    send_gdb "set args main\n"
4e2e788
-    gdb_expect -re ".*$gdb_prompt $" {}
4e2e788
-}
4e2e788
-
4e2e788
-
4e2e788
 # Compile up a second, different, object file.  Copy its debug info
4e2e788
 # over the top of the new debug info.  Note that somewhere in the
4e2e788
 # above the "set debug-file-directory" variable is set to
4e2e788
@@ -973,6 +872,7 @@ if [gdb_gnu_strip_debug $binfile] {
4e2e788
     unsupported "cannot produce separate debug info files"
4e2e788
     return -1
4e2e788
 }
4e2e788
+remote_exec build "cp ${existing_binfile}.debug ${existing_binfile}.debug-backup"
4e2e788
 remote_exec build "cp $corrupt_debug_file ${existing_binfile}.debug"
4e2e788
 
4e2e788
 gdb_exit
4e2e788
@@ -990,3 +890,188 @@ gdb_test_multiple "file $existing_binfil
4e2e788
 	exp_continue
4e2e788
     }
4e2e788
 }
4e2e788
+
4e2e788
+remote_exec build "cp ${existing_binfile}.debug-backup ${existing_binfile}.debug"
4e2e788
+
4e2e788
+#********
4e2e788
+
4e2e788
+proc test_different_dir {type test_different_dir xfail} {
4e2e788
+    global srcdir subdir objdir binfile srcfile timeout gdb_prompt
4e2e788
+    global pf_prefix
4e2e788
+    global bp_location6 decimal hex
4e2e788
+
4e2e788
+    set pf_prefix "$type:"
4e2e788
+
4e2e788
+    gdb_exit
4e2e788
+    gdb_start
4e2e788
+    gdb_reinitialize_dir $srcdir/$subdir
4e2e788
+    gdb_test "set debug-file-directory ${test_different_dir}" ".*" "set separate debug location"
4e2e788
+    gdb_load ${binfile}
4e2e788
+
4e2e788
+    if [target_info exists gdb_stub] {
4e2e788
+        gdb_step_for_stub;
4e2e788
+    }
4e2e788
+
4e2e788
+    #
4e2e788
+    # test break at function
4e2e788
+    #
4e2e788
+    if {$xfail} {
4e2e788
+        setup_xfail "*-*-*"
4e2e788
+    }
4e2e788
+    gdb_test "break main" \
4e2e788
+        "Breakpoint.*at.* file .*$srcfile, line.*" \
4e2e788
+        "breakpoint function, optimized file"
4e2e788
+
4e2e788
+    #
4e2e788
+    # test break at function
4e2e788
+    #
4e2e788
+    if {$xfail} {
4e2e788
+        setup_xfail "*-*-*"
4e2e788
+    }
4e2e788
+    gdb_test "break marker4" \
4e2e788
+        "Breakpoint.*at.* file .*$srcfile, line.*" \
4e2e788
+        "breakpoint small function, optimized file"
4e2e788
+
4e2e788
+    #
4e2e788
+    # run until the breakpoint at main is hit. For non-stubs-using targets.
4e2e788
+    #
4e2e788
+    gdb_run_cmd
4e2e788
+    if {$xfail} {
4e2e788
+        setup_xfail "*-*-*"
4e2e788
+    }
4e2e788
+    gdb_expect {
4e2e788
+        -re "Breakpoint \[0-9\]+,.*main .*argc.*argv.* at .*$srcfile:$bp_location6.*$bp_location6\[\t \]+if .argc.* \{.*$gdb_prompt $" {
4e2e788
+            pass "run until function breakpoint, optimized file"
4e2e788
+        }
4e2e788
+        -re "Breakpoint \[0-9\]+,.*main .*argc.*argv.* at .*$gdb_prompt $" {
4e2e788
+            pass "run until function breakpoint, optimized file (code motion)"
4e2e788
+        }
4e2e788
+        -re "$gdb_prompt $" {
4e2e788
+            fail "run until function breakpoint, optimized file"
4e2e788
+        }
4e2e788
+        timeout {
4e2e788
+            fail "run until function breakpoint, optimized file (timeout)"
4e2e788
+        }
4e2e788
+    }
4e2e788
+
4e2e788
+    if ![target_info exists use_gdb_stub] {
4e2e788
+      if [istarget "*-*-vxworks*"] then {
4e2e788
+        send_gdb "run vxmain \"2\"\n"
4e2e788
+        set timeout 120
4e2e788
+        verbose "Timeout is now $timeout seconds" 2
4e2e788
+      } else {
4e2e788
+            send_gdb "run\n"
4e2e788
+      }
4e2e788
+      if {$xfail} {
4e2e788
+        setup_xfail "*-*-*"
4e2e788
+      }
4e2e788
+      gdb_expect {
4e2e788
+        -re "The program .* has been started already.*y or n. $" {
4e2e788
+            send_gdb "y\n"
4e2e788
+            exp_continue
4e2e788
+        }
4e2e788
+        -re "Starting program.*Breakpoint \[0-9\]+,.*main .*argc.*argv.* at .*$srcfile:$bp_location6.*$bp_location6\[\t \]+if .argc.* \{.*$gdb_prompt $"\
4e2e788
+                                { pass "run until function breakpoint, optimized file" }
4e2e788
+        -re "Starting program.*Breakpoint \[0-9\]+,.*main .*argc.*argv.* at .*$gdb_prompt $"\
4e2e788
+                                { pass "run until function breakpoint, optimized file (code motion)" }
4e2e788
+        -re ".*$gdb_prompt $"       { fail "run until function breakpoint, optimized file" }
4e2e788
+        timeout                    { fail "run until function breakpoint, optimized file (timeout)" }
4e2e788
+      }
4e2e788
+    } else {
4e2e788
+        if ![target_info exists gdb_stub] {
4e2e788
+            gdb_test continue ".*Continuing\\..*Breakpoint \[0-9\]+, main \\(argc=.*, argv=.*, envp=.*\\) at .*$srcfile:$bp_location6.*$bp_location6\[\t \]+if .argc.*\{.*" "stub continue, optimized file"
4e2e788
+        }
4e2e788
+    }
4e2e788
+
4e2e788
+
4e2e788
+
4e2e788
+    #
4e2e788
+    # run until the breakpoint at a small function
4e2e788
+    #
4e2e788
+
4e2e788
+    #
4e2e788
+    # Add a second pass pattern.  The behavior differs here between stabs
4e2e788
+    # and dwarf for one-line functions.  Stabs preserves two line symbols
4e2e788
+    # (one before the prologue and one after) with the same line number, 
4e2e788
+    # but dwarf regards these as duplicates and discards one of them.
4e2e788
+    # Therefore the address after the prologue (where the breakpoint is)
4e2e788
+    # has no exactly matching line symbol, and GDB reports the breakpoint
4e2e788
+    # as if it were in the middle of a line rather than at the beginning.
4e2e788
+
4e2e788
+    set bp_location13 [gdb_get_line_number "set breakpoint 13 here"]
4e2e788
+    set bp_location14 [gdb_get_line_number "set breakpoint 14 here"]
4e2e788
+    send_gdb "continue\n"
4e2e788
+    if {$xfail} {
4e2e788
+        setup_xfail "*-*-*"
4e2e788
+    }
4e2e788
+    gdb_expect {
4e2e788
+        -re "Breakpoint $decimal, marker4 \\(d=177601976\\) at .*$srcfile:$bp_location13\[\r\n\]+$bp_location13\[\t \]+void marker4.*" {
4e2e788
+            pass "run until breakpoint set at small function, optimized file"
4e2e788
+        }
4e2e788
+        -re "Breakpoint $decimal, $hex in marker4 \\(d=177601976\\) at .*$srcfile:$bp_location13\[\r\n\]+$bp_location13\[\t \]+void marker4.*" {
4e2e788
+            pass "run until breakpoint set at small function, optimized file"
4e2e788
+        }
4e2e788
+        -re "Breakpoint $decimal, marker4 \\(d=177601976\\) at .*$srcfile:$bp_location14\[\r\n\]+$bp_location14\[\t \]+void marker4.*" {
4e2e788
+            # marker4() is defined at line 46 when compiled with -DPROTOTYPES
4e2e788
+            pass "run until breakpoint set at small function, optimized file (line bp_location14)"
4e2e788
+        }
4e2e788
+        -re ".*$gdb_prompt " {
4e2e788
+            fail "run until breakpoint set at small function, optimized file"
4e2e788
+        }
4e2e788
+        timeout {
4e2e788
+            fail "run until breakpoint set at small function, optimized file (timeout)"
4e2e788
+        }
4e2e788
+    }
4e2e788
+
4e2e788
+
4e2e788
+    # Reset the default arguments for VxWorks
4e2e788
+    if [istarget "*-*-vxworks*"] {
4e2e788
+        set timeout 10
4e2e788
+        verbose "Timeout is now $timeout seconds" 2
4e2e788
+        send_gdb "set args main\n"
4e2e788
+        gdb_expect -re ".*$gdb_prompt $" {}
4e2e788
+    }
4e2e788
+
4e2e788
+    unset pf_prefix
4e2e788
+# proc test_different_dir
4e2e788
+}
4e2e788
+
4e2e788
+
4e2e788
+# now move the .debug file to a different location so that we can test
4e2e788
+# the "set debug-file-directory" command.
4e2e788
+  
4e2e788
+remote_exec build "mv ${objdir}/${subdir}/.debug/${testfile}.debug ${objdir}/${subdir}"
4e2e788
+set debugfile "${objdir}/${subdir}/${testfile}.debug"
4e2e788
+
4e2e788
+test_different_dir debuglink "${objdir}/${subdir}" 0
4e2e788
+
4e2e788
+
4e2e788
+# NT_GNU_BUILD_ID / .note.gnu.build-id test:
4e2e788
+
4e2e788
+set build_id_debug_filename [build_id_debug_filename_get $binfile]
4e2e788
+if {$build_id_debug_filename eq ""} {
4e2e788
+    unsupported "build-id is not supported by the compiler"
4e2e788
+
4e2e788
+    # Spare debug files may confuse testsuite runs in the future.
4e2e788
+    remote_exec build "rm -f $debugfile"
4e2e788
+} else {
4e2e788
+    set build_id_debugself_filename [build_id_debug_filename_get $debugfile]
4e2e788
+    set test "build-id support by binutils"
4e2e788
+    set xfail 0
4e2e788
+    if {$build_id_debugself_filename eq ""} {
4e2e788
+        unsupported $test
4e2e788
+        set xfail 1
4e2e788
+    } elseif {$build_id_debugself_filename ne $build_id_debug_filename} {
4e2e788
+        fail $test
4e2e788
+    } else {
4e2e788
+        pass $test
4e2e788
+    }
4e2e788
+
4e2e788
+    file mkdir [file dirname ${objdir}/${subdir}/${build_id_debug_filename}]
4e2e788
+    remote_exec build "mv $debugfile ${objdir}/${subdir}/${build_id_debug_filename}"
4e2e788
+
4e2e788
+    test_different_dir build-id "${objdir}/${subdir}" $xfail
4e2e788
+
4e2e788
+    # Spare debug files may confuse testsuite runs in the future.
4e2e788
+    remote_exec build "rm -f ${objdir}/${subdir}/${build_id_debug_filename}"
4e2e788
+}
4e2e788
diff -u -rup gdb-6.6-orig/gdb/testsuite/lib/gdb.exp gdb-6.6/gdb/testsuite/lib/gdb.exp
4e2e788
--- gdb-6.6-orig/gdb/testsuite/lib/gdb.exp	2007-08-28 14:32:18.000000000 +0200
4e2e788
+++ gdb-6.6/gdb/testsuite/lib/gdb.exp	2007-08-28 14:33:03.000000000 +0200
4e2e788
@@ -2323,6 +2323,27 @@ proc separate_debug_filename { exec } {
4e2e788
     return $debug_file
4e2e788
 }
4e2e788
 
4e2e788
+# Return the build-id hex string (usually 160 bits as 40 hex characters)
4e2e788
+# converted to the form: .build-id/ab/cdef1234...89.debug
4e2e788
+# Return "" if no build-id found.
4e2e788
+proc build_id_debug_filename_get { exec } {
4e2e788
+    set tmp "${exec}-tmp"
4e2e788
+    exec objcopy -j .note.gnu.build-id -O binary $exec $tmp
4e2e788
+    set fi [open $tmp]
4e2e788
+    # Skip the NOTE header.
4e2e788
+    read $fi 16
4e2e788
+    set data [read $fi]
4e2e788
+    close $fi
4e2e788
+    file delete $tmp
4e2e788
+    if {$data eq ""} {
4e2e788
+	return ""
4e2e788
+    }
4e2e788
+    # Convert it to hex.
4e2e788
+    binary scan $data H* data
4e2e788
+    set data [regsub {^..} $data {\0/}]
4e2e788
+    return ".build-id/${data}.debug";
4e2e788
+}
4e2e788
+
4e2e788
 # Create stripped files for DEST, replacing it.  If ARGS is passed, it is a
4e2e788
 # list of optional flags.  The only currently supported flag is no-main,
4e2e788
 # which removes the symbol entry for main from the separate debug file.
4e2e788
--- gdb-6.6/gdb/testsuite/gdb.base/sepdebug.exp	2007-08-28 15:27:36.000000000 +0200
4e2e788
+++ gdb-6.6/gdb/testsuite/gdb.base/sepdebug.exp	2007-08-28 15:25:23.000000000 +0200
4e2e788
@@ -860,14 +860,14 @@ test_next_with_recursion
4e2e788
 # ${objdir}/${subdir} so need to move things there.
4e2e788
 
4e2e788
 set existing_binfile $binfile
4e2e788
-set testfile "sepdebug2"
4e2e788
-set srcfile ${testfile}.c
4e2e788
-set binfile ${objdir}/${subdir}/${testfile}
4e2e788
-set corrupt_debug_file [separate_debug_filename $binfile]
4e2e788
-if  { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {debug additional_flags=-w}] != "" } {
4e2e788
+set testfile2 "sepdebug2"
4e2e788
+set srcfile2 ${testfile2}.c
4e2e788
+set binfile2 ${objdir}/${subdir}/${testfile2}
4e2e788
+set corrupt_debug_file [separate_debug_filename $binfile2]
4e2e788
+if  { [gdb_compile "${srcdir}/${subdir}/${srcfile2}" "${binfile2}" executable {debug additional_flags=-w}] != "" } {
4e2e788
     return -1
4e2e788
 }
4e2e788
-if [gdb_gnu_strip_debug $binfile] {
4e2e788
+if [gdb_gnu_strip_debug $binfile2] {
4e2e788
     # check that you have a recent version of strip and objcopy installed
4e2e788
     unsupported "cannot produce separate debug info files"
4e2e788
     return -1