gdb/build-id.c - gdb

Functions defined

Source code

  1. /* build-id-related functions.

  2.    Copyright (C) 1991-2015 Free Software Foundation, Inc.

  3.    This file is part of GDB.

  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 3 of the License, or
  7.    (at your option) any later version.

  8.    This program is distributed in the hope that it will be useful,
  9.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  10.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11.    GNU General Public License for more details.

  12.    You should have received a copy of the GNU General Public License
  13.    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */

  14. #include "defs.h"
  15. #include "bfd.h"
  16. #include "elf-bfd.h"
  17. #include "gdb_bfd.h"
  18. #include "build-id.h"
  19. #include "gdb_vecs.h"
  20. #include "symfile.h"
  21. #include "objfiles.h"
  22. #include "filenames.h"

  23. /* See build-id.h.  */

  24. const struct elf_build_id *
  25. build_id_bfd_get (bfd *abfd)
  26. {
  27.   if (!bfd_check_format (abfd, bfd_object)
  28.       || bfd_get_flavour (abfd) != bfd_target_elf_flavour
  29.       /* Although this is ELF_specific, it is safe to do in generic
  30.          code because it does not rely on any ELF-specific symbols at
  31.          link time, and if the ELF code is not available in BFD, then
  32.          ABFD will not have the ELF flavour.  */
  33.       || elf_tdata (abfd)->build_id == NULL)
  34.     return NULL;

  35.   return elf_tdata (abfd)->build_id;
  36. }

  37. /* See build-id.h.  */

  38. int
  39. build_id_verify (bfd *abfd, size_t check_len, const bfd_byte *check)
  40. {
  41.   const struct elf_build_id *found;
  42.   int retval = 0;

  43.   found = build_id_bfd_get (abfd);

  44.   if (found == NULL)
  45.     warning (_("File \"%s\" has no build-id, file skipped"),
  46.              bfd_get_filename (abfd));
  47.   else if (found->size != check_len
  48.            || memcmp (found->data, check, found->size) != 0)
  49.     warning (_("File \"%s\" has a different build-id, file skipped"),
  50.              bfd_get_filename (abfd));
  51.   else
  52.     retval = 1;

  53.   return retval;
  54. }

  55. /* See build-id.h.  */

  56. bfd *
  57. build_id_to_debug_bfd (size_t build_id_len, const bfd_byte *build_id)
  58. {
  59.   char *link, *debugdir;
  60.   VEC (char_ptr) *debugdir_vec;
  61.   struct cleanup *back_to;
  62.   int ix;
  63.   bfd *abfd = NULL;

  64.   /* DEBUG_FILE_DIRECTORY/.build-id/ab/cdef */
  65.   link = alloca (strlen (debug_file_directory) + (sizeof "/.build-id/" - 1) + 1
  66.                  + 2 * build_id_len + (sizeof ".debug" - 1) + 1);

  67.   /* Keep backward compatibility so that DEBUG_FILE_DIRECTORY being "" will
  68.      cause "/.build-id/..." lookups.  */

  69.   debugdir_vec = dirnames_to_char_ptr_vec (debug_file_directory);
  70.   back_to = make_cleanup_free_char_ptr_vec (debugdir_vec);

  71.   for (ix = 0; VEC_iterate (char_ptr, debugdir_vec, ix, debugdir); ++ix)
  72.     {
  73.       size_t debugdir_len = strlen (debugdir);
  74.       const gdb_byte *data = build_id;
  75.       size_t size = build_id_len;
  76.       char *s;
  77.       char *filename = NULL;

  78.       memcpy (link, debugdir, debugdir_len);
  79.       s = &link[debugdir_len];
  80.       s += sprintf (s, "/.build-id/");
  81.       if (size > 0)
  82.         {
  83.           size--;
  84.           s += sprintf (s, "%02x", (unsigned) *data++);
  85.         }
  86.       if (size > 0)
  87.         *s++ = '/';
  88.       while (size-- > 0)
  89.         s += sprintf (s, "%02x", (unsigned) *data++);
  90.       strcpy (s, ".debug");

  91.       /* lrealpath() is expensive even for the usually non-existent files.  */
  92.       if (access (link, F_OK) == 0)
  93.         filename = lrealpath (link);

  94.       if (filename == NULL)
  95.         continue;

  96.       /* We expect to be silent on the non-existing files.  */
  97.       abfd = gdb_bfd_open_maybe_remote (filename);
  98.       if (abfd == NULL)
  99.         continue;

  100.       if (build_id_verify (abfd, build_id_len, build_id))
  101.         break;

  102.       gdb_bfd_unref (abfd);
  103.       abfd = NULL;
  104.     }

  105.   do_cleanups (back_to);
  106.   return abfd;
  107. }

  108. /* See build-id.h.  */

  109. char *
  110. find_separate_debug_file_by_buildid (struct objfile *objfile)
  111. {
  112.   const struct elf_build_id *build_id;

  113.   build_id = build_id_bfd_get (objfile->obfd);
  114.   if (build_id != NULL)
  115.     {
  116.       bfd *abfd;

  117.       abfd = build_id_to_debug_bfd (build_id->size, build_id->data);
  118.       /* Prevent looping on a stripped .debug file.  */
  119.       if (abfd != NULL
  120.           && filename_cmp (bfd_get_filename (abfd),
  121.                            objfile_name (objfile)) == 0)
  122.         {
  123.           warning (_("\"%s\": separate debug info file has no debug info"),
  124.                    bfd_get_filename (abfd));
  125.           gdb_bfd_unref (abfd);
  126.         }
  127.       else if (abfd != NULL)
  128.         {
  129.           char *result = xstrdup (bfd_get_filename (abfd));

  130.           gdb_bfd_unref (abfd);
  131.           return result;
  132.         }
  133.     }
  134.   return NULL;
  135. }