build-id.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /* build-id-related functions.
  2. Copyright (C) 1991-2022 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 "gdb_bfd.h"
  17. #include "build-id.h"
  18. #include "gdbsupport/gdb_vecs.h"
  19. #include "symfile.h"
  20. #include "objfiles.h"
  21. #include "filenames.h"
  22. #include "gdbcore.h"
  23. /* See build-id.h. */
  24. const struct bfd_build_id *
  25. build_id_bfd_get (bfd *abfd)
  26. {
  27. if (!bfd_check_format (abfd, bfd_object)
  28. && !bfd_check_format (abfd, bfd_core))
  29. return NULL;
  30. if (abfd->build_id != NULL)
  31. return abfd->build_id;
  32. /* No build-id */
  33. return NULL;
  34. }
  35. /* See build-id.h. */
  36. int
  37. build_id_verify (bfd *abfd, size_t check_len, const bfd_byte *check)
  38. {
  39. const struct bfd_build_id *found;
  40. int retval = 0;
  41. found = build_id_bfd_get (abfd);
  42. if (found == NULL)
  43. warning (_("File \"%s\" has no build-id, file skipped"),
  44. bfd_get_filename (abfd));
  45. else if (found->size != check_len
  46. || memcmp (found->data, check, found->size) != 0)
  47. warning (_("File \"%s\" has a different build-id, file skipped"),
  48. bfd_get_filename (abfd));
  49. else
  50. retval = 1;
  51. return retval;
  52. }
  53. /* Helper for build_id_to_debug_bfd. LINK is a path to a potential
  54. build-id-based separate debug file, potentially a symlink to the real file.
  55. If the file exists and matches BUILD_ID, return a BFD reference to it. */
  56. static gdb_bfd_ref_ptr
  57. build_id_to_debug_bfd_1 (const std::string &link, size_t build_id_len,
  58. const bfd_byte *build_id)
  59. {
  60. if (separate_debug_file_debug)
  61. {
  62. gdb_printf (gdb_stdlog, _(" Trying %s..."), link.c_str ());
  63. gdb_flush (gdb_stdlog);
  64. }
  65. /* lrealpath() is expensive even for the usually non-existent files. */
  66. gdb::unique_xmalloc_ptr<char> filename_holder;
  67. const char *filename = nullptr;
  68. if (startswith (link, TARGET_SYSROOT_PREFIX))
  69. filename = link.c_str ();
  70. else if (access (link.c_str (), F_OK) == 0)
  71. {
  72. filename_holder.reset (lrealpath (link.c_str ()));
  73. filename = filename_holder.get ();
  74. }
  75. if (filename == NULL)
  76. {
  77. if (separate_debug_file_debug)
  78. gdb_printf (gdb_stdlog,
  79. _(" no, unable to compute real path\n"));
  80. return {};
  81. }
  82. /* We expect to be silent on the non-existing files. */
  83. gdb_bfd_ref_ptr debug_bfd = gdb_bfd_open (filename, gnutarget);
  84. if (debug_bfd == NULL)
  85. {
  86. if (separate_debug_file_debug)
  87. gdb_printf (gdb_stdlog, _(" no, unable to open.\n"));
  88. return {};
  89. }
  90. if (!build_id_verify (debug_bfd.get(), build_id_len, build_id))
  91. {
  92. if (separate_debug_file_debug)
  93. gdb_printf (gdb_stdlog, _(" no, build-id does not match.\n"));
  94. return {};
  95. }
  96. if (separate_debug_file_debug)
  97. gdb_printf (gdb_stdlog, _(" yes!\n"));
  98. return debug_bfd;
  99. }
  100. /* Common code for finding BFDs of a given build-id. This function
  101. works with both debuginfo files (SUFFIX == ".debug") and executable
  102. files (SUFFIX == ""). */
  103. static gdb_bfd_ref_ptr
  104. build_id_to_bfd_suffix (size_t build_id_len, const bfd_byte *build_id,
  105. const char *suffix)
  106. {
  107. /* Keep backward compatibility so that DEBUG_FILE_DIRECTORY being "" will
  108. cause "/.build-id/..." lookups. */
  109. std::vector<gdb::unique_xmalloc_ptr<char>> debugdir_vec
  110. = dirnames_to_char_ptr_vec (debug_file_directory.c_str ());
  111. for (const gdb::unique_xmalloc_ptr<char> &debugdir : debugdir_vec)
  112. {
  113. const gdb_byte *data = build_id;
  114. size_t size = build_id_len;
  115. /* Compute where the file named after the build-id would be.
  116. If debugdir is "/usr/lib/debug" and the build-id is abcdef, this will
  117. give "/usr/lib/debug/.build-id/ab/cdef.debug". */
  118. std::string link = debugdir.get ();
  119. link += "/.build-id/";
  120. if (size > 0)
  121. {
  122. size--;
  123. string_appendf (link, "%02x/", (unsigned) *data++);
  124. }
  125. while (size-- > 0)
  126. string_appendf (link, "%02x", (unsigned) *data++);
  127. link += suffix;
  128. gdb_bfd_ref_ptr debug_bfd
  129. = build_id_to_debug_bfd_1 (link, build_id_len, build_id);
  130. if (debug_bfd != NULL)
  131. return debug_bfd;
  132. /* Try to look under the sysroot as well. If the sysroot is
  133. "/the/sysroot", it will give
  134. "/the/sysroot/usr/lib/debug/.build-id/ab/cdef.debug". */
  135. if (!gdb_sysroot.empty ())
  136. {
  137. link = gdb_sysroot + link;
  138. debug_bfd = build_id_to_debug_bfd_1 (link, build_id_len, build_id);
  139. if (debug_bfd != NULL)
  140. return debug_bfd;
  141. }
  142. }
  143. return {};
  144. }
  145. /* See build-id.h. */
  146. gdb_bfd_ref_ptr
  147. build_id_to_debug_bfd (size_t build_id_len, const bfd_byte *build_id)
  148. {
  149. return build_id_to_bfd_suffix (build_id_len, build_id, ".debug");
  150. }
  151. /* See build-id.h. */
  152. gdb_bfd_ref_ptr
  153. build_id_to_exec_bfd (size_t build_id_len, const bfd_byte *build_id)
  154. {
  155. return build_id_to_bfd_suffix (build_id_len, build_id, "");
  156. }
  157. /* See build-id.h. */
  158. std::string
  159. find_separate_debug_file_by_buildid (struct objfile *objfile)
  160. {
  161. const struct bfd_build_id *build_id;
  162. build_id = build_id_bfd_get (objfile->obfd);
  163. if (build_id != NULL)
  164. {
  165. if (separate_debug_file_debug)
  166. gdb_printf (gdb_stdlog,
  167. _("\nLooking for separate debug info (build-id) for "
  168. "%s\n"), objfile_name (objfile));
  169. gdb_bfd_ref_ptr abfd (build_id_to_debug_bfd (build_id->size,
  170. build_id->data));
  171. /* Prevent looping on a stripped .debug file. */
  172. if (abfd != NULL
  173. && filename_cmp (bfd_get_filename (abfd.get ()),
  174. objfile_name (objfile)) == 0)
  175. warning (_("\"%s\": separate debug info file has no debug info"),
  176. bfd_get_filename (abfd.get ()));
  177. else if (abfd != NULL)
  178. return std::string (bfd_get_filename (abfd.get ()));
  179. }
  180. return std::string ();
  181. }