1 /* build-id-related functions.
3 Copyright (C) 1991-2018 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
27 #include "filenames.h"
32 const struct bfd_build_id *
33 build_id_bfd_get (bfd *abfd)
35 if (!bfd_check_format (abfd, bfd_object))
38 if (abfd->build_id != NULL)
39 return abfd->build_id;
48 build_id_verify (bfd *abfd, size_t check_len, const bfd_byte *check)
50 const struct bfd_build_id *found;
53 found = build_id_bfd_get (abfd);
56 warning (_("File \"%s\" has no build-id, file skipped"),
57 bfd_get_filename (abfd));
58 else if (found->size != check_len
59 || memcmp (found->data, check, found->size) != 0)
60 warning (_("File \"%s\" has a different build-id, file skipped"),
61 bfd_get_filename (abfd));
71 build_id_to_debug_bfd (size_t build_id_len, const bfd_byte *build_id)
73 char *link, *debugdir;
78 /* DEBUG_FILE_DIRECTORY/.build-id/ab/cdef */
79 alloc_len = (strlen (debug_file_directory)
80 + (sizeof "/.build-id/" - 1) + 1
81 + 2 * build_id_len + (sizeof ".debug" - 1) + 1);
82 link = (char *) alloca (alloc_len);
84 /* Keep backward compatibility so that DEBUG_FILE_DIRECTORY being "" will
85 cause "/.build-id/..." lookups. */
87 std::vector<gdb::unique_xmalloc_ptr<char>> debugdir_vec
88 = dirnames_to_char_ptr_vec (debug_file_directory);
90 for (const gdb::unique_xmalloc_ptr<char> &debugdir : debugdir_vec)
92 size_t debugdir_len = strlen (debugdir.get ());
93 const gdb_byte *data = build_id;
94 size_t size = build_id_len;
97 memcpy (link, debugdir.get (), debugdir_len);
98 s = &link[debugdir_len];
99 s += sprintf (s, "/.build-id/");
103 s += sprintf (s, "%02x", (unsigned) *data++);
108 s += sprintf (s, "%02x", (unsigned) *data++);
109 strcpy (s, ".debug");
111 if (separate_debug_file_debug)
112 printf_unfiltered (_(" Trying %s\n"), link);
114 /* lrealpath() is expensive even for the usually non-existent files. */
115 gdb::unique_xmalloc_ptr<char> filename;
116 if (access (link, F_OK) == 0)
117 filename.reset (lrealpath (link));
119 if (filename == NULL)
122 /* We expect to be silent on the non-existing files. */
123 abfd = gdb_bfd_open (filename.get (), gnutarget, -1);
128 if (build_id_verify (abfd.get(), build_id_len, build_id))
137 /* See build-id.h. */
140 find_separate_debug_file_by_buildid (struct objfile *objfile)
142 const struct bfd_build_id *build_id;
144 build_id = build_id_bfd_get (objfile->obfd);
145 if (build_id != NULL)
147 if (separate_debug_file_debug)
148 printf_unfiltered (_("\nLooking for separate debug info (build-id) for "
149 "%s\n"), objfile_name (objfile));
151 gdb_bfd_ref_ptr abfd (build_id_to_debug_bfd (build_id->size,
153 /* Prevent looping on a stripped .debug file. */
155 && filename_cmp (bfd_get_filename (abfd.get ()),
156 objfile_name (objfile)) == 0)
157 warning (_("\"%s\": separate debug info file has no debug info"),
158 bfd_get_filename (abfd.get ()));
159 else if (abfd != NULL)
160 return xstrdup (bfd_get_filename (abfd.get ()));