Updated Swedish translation for the opcodes directory
[binutils-gdb.git] / gdb / build-id.c
blob00250c20ae9e6f944522be9228b53273e534a57d
1 /* build-id-related functions.
3 Copyright (C) 1991-2023 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/>. */
20 #include "defs.h"
21 #include "bfd.h"
22 #include "gdb_bfd.h"
23 #include "build-id.h"
24 #include "gdbsupport/gdb_vecs.h"
25 #include "symfile.h"
26 #include "objfiles.h"
27 #include "filenames.h"
28 #include "gdbcore.h"
30 /* See build-id.h. */
32 const struct bfd_build_id *
33 build_id_bfd_get (bfd *abfd)
35 /* Dynamic objfiles such as ones created by JIT reader API
36 have no underlaying bfd structure (that is, objfile->obfd
37 is NULL). */
38 if (abfd == nullptr)
39 return nullptr;
41 if (!bfd_check_format (abfd, bfd_object)
42 && !bfd_check_format (abfd, bfd_core))
43 return NULL;
45 if (abfd->build_id != NULL)
46 return abfd->build_id;
48 /* No build-id */
49 return NULL;
52 /* See build-id.h. */
54 int
55 build_id_verify (bfd *abfd, size_t check_len, const bfd_byte *check)
57 const struct bfd_build_id *found;
58 int retval = 0;
60 found = build_id_bfd_get (abfd);
62 if (found == NULL)
63 warning (_("File \"%s\" has no build-id, file skipped"),
64 bfd_get_filename (abfd));
65 else if (found->size != check_len
66 || memcmp (found->data, check, found->size) != 0)
67 warning (_("File \"%s\" has a different build-id, file skipped"),
68 bfd_get_filename (abfd));
69 else
70 retval = 1;
72 return retval;
75 /* Helper for build_id_to_debug_bfd. LINK is a path to a potential
76 build-id-based separate debug file, potentially a symlink to the real file.
77 If the file exists and matches BUILD_ID, return a BFD reference to it. */
79 static gdb_bfd_ref_ptr
80 build_id_to_debug_bfd_1 (const std::string &link, size_t build_id_len,
81 const bfd_byte *build_id)
83 if (separate_debug_file_debug)
85 gdb_printf (gdb_stdlog, _(" Trying %s..."), link.c_str ());
86 gdb_flush (gdb_stdlog);
89 /* lrealpath() is expensive even for the usually non-existent files. */
90 gdb::unique_xmalloc_ptr<char> filename_holder;
91 const char *filename = nullptr;
92 if (startswith (link, TARGET_SYSROOT_PREFIX))
93 filename = link.c_str ();
94 else if (access (link.c_str (), F_OK) == 0)
96 filename_holder.reset (lrealpath (link.c_str ()));
97 filename = filename_holder.get ();
100 if (filename == NULL)
102 if (separate_debug_file_debug)
103 gdb_printf (gdb_stdlog,
104 _(" no, unable to compute real path\n"));
106 return {};
109 /* We expect to be silent on the non-existing files. */
110 gdb_bfd_ref_ptr debug_bfd = gdb_bfd_open (filename, gnutarget);
112 if (debug_bfd == NULL)
114 if (separate_debug_file_debug)
115 gdb_printf (gdb_stdlog, _(" no, unable to open.\n"));
117 return {};
120 if (!build_id_verify (debug_bfd.get(), build_id_len, build_id))
122 if (separate_debug_file_debug)
123 gdb_printf (gdb_stdlog, _(" no, build-id does not match.\n"));
125 return {};
128 if (separate_debug_file_debug)
129 gdb_printf (gdb_stdlog, _(" yes!\n"));
131 return debug_bfd;
134 /* Common code for finding BFDs of a given build-id. This function
135 works with both debuginfo files (SUFFIX == ".debug") and executable
136 files (SUFFIX == ""). */
138 static gdb_bfd_ref_ptr
139 build_id_to_bfd_suffix (size_t build_id_len, const bfd_byte *build_id,
140 const char *suffix)
142 /* Keep backward compatibility so that DEBUG_FILE_DIRECTORY being "" will
143 cause "/.build-id/..." lookups. */
145 std::vector<gdb::unique_xmalloc_ptr<char>> debugdir_vec
146 = dirnames_to_char_ptr_vec (debug_file_directory.c_str ());
148 for (const gdb::unique_xmalloc_ptr<char> &debugdir : debugdir_vec)
150 const gdb_byte *data = build_id;
151 size_t size = build_id_len;
153 /* Compute where the file named after the build-id would be.
155 If debugdir is "/usr/lib/debug" and the build-id is abcdef, this will
156 give "/usr/lib/debug/.build-id/ab/cdef.debug". */
157 std::string link = debugdir.get ();
158 link += "/.build-id/";
160 if (size > 0)
162 size--;
163 string_appendf (link, "%02x/", (unsigned) *data++);
166 while (size-- > 0)
167 string_appendf (link, "%02x", (unsigned) *data++);
169 link += suffix;
171 gdb_bfd_ref_ptr debug_bfd
172 = build_id_to_debug_bfd_1 (link, build_id_len, build_id);
173 if (debug_bfd != NULL)
174 return debug_bfd;
176 /* Try to look under the sysroot as well. If the sysroot is
177 "/the/sysroot", it will give
178 "/the/sysroot/usr/lib/debug/.build-id/ab/cdef.debug". */
180 if (!gdb_sysroot.empty ())
182 link = gdb_sysroot + link;
183 debug_bfd = build_id_to_debug_bfd_1 (link, build_id_len, build_id);
184 if (debug_bfd != NULL)
185 return debug_bfd;
189 return {};
192 /* See build-id.h. */
194 gdb_bfd_ref_ptr
195 build_id_to_debug_bfd (size_t build_id_len, const bfd_byte *build_id)
197 return build_id_to_bfd_suffix (build_id_len, build_id, ".debug");
200 /* See build-id.h. */
202 gdb_bfd_ref_ptr
203 build_id_to_exec_bfd (size_t build_id_len, const bfd_byte *build_id)
205 return build_id_to_bfd_suffix (build_id_len, build_id, "");
208 /* See build-id.h. */
210 std::string
211 find_separate_debug_file_by_buildid (struct objfile *objfile,
212 std::vector<std::string> *warnings_vector)
214 const struct bfd_build_id *build_id;
216 build_id = build_id_bfd_get (objfile->obfd.get ());
217 if (build_id != NULL)
219 if (separate_debug_file_debug)
220 gdb_printf (gdb_stdlog,
221 _("\nLooking for separate debug info (build-id) for "
222 "%s\n"), objfile_name (objfile));
224 gdb_bfd_ref_ptr abfd (build_id_to_debug_bfd (build_id->size,
225 build_id->data));
226 /* Prevent looping on a stripped .debug file. */
227 if (abfd != NULL
228 && filename_cmp (bfd_get_filename (abfd.get ()),
229 objfile_name (objfile)) == 0)
231 std::string msg
232 = string_printf (_("\"%s\": separate debug info file has no "
233 "debug info"), bfd_get_filename (abfd.get ()));
234 if (separate_debug_file_debug)
235 gdb_printf (gdb_stdlog, "%s", msg.c_str ());
236 warnings_vector->emplace_back (std::move (msg));
238 else if (abfd != NULL)
239 return std::string (bfd_get_filename (abfd.get ()));
242 return std::string ();