aarch64: Fix tlbi and tlbip instructions
[binutils-gdb.git] / gdb / build-id.c
bloba40c56087ed1af84f456f9323b53624cfbd77731
1 /* build-id-related functions.
3 Copyright (C) 1991-2024 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"
29 #include "cli/cli-style.h"
31 /* See build-id.h. */
33 const struct bfd_build_id *
34 build_id_bfd_get (bfd *abfd)
36 /* Dynamic objfiles such as ones created by JIT reader API
37 have no underlying bfd structure (that is, objfile->obfd
38 is NULL). */
39 if (abfd == nullptr)
40 return nullptr;
42 if (!bfd_check_format (abfd, bfd_object)
43 && !bfd_check_format (abfd, bfd_core))
44 return NULL;
46 if (abfd->build_id != NULL)
47 return abfd->build_id;
49 /* No build-id */
50 return NULL;
53 /* See build-id.h. */
55 int
56 build_id_verify (bfd *abfd, size_t check_len, const bfd_byte *check)
58 const struct bfd_build_id *found;
59 int retval = 0;
61 found = build_id_bfd_get (abfd);
63 if (found == NULL)
64 warning (_("File \"%s\" has no build-id, file skipped"),
65 bfd_get_filename (abfd));
66 else if (found->size != check_len
67 || memcmp (found->data, check, found->size) != 0)
68 warning (_("File \"%s\" has a different build-id, file skipped"),
69 bfd_get_filename (abfd));
70 else
71 retval = 1;
73 return retval;
76 /* Helper for build_id_to_debug_bfd. LINK is a path to a potential
77 build-id-based separate debug file, potentially a symlink to the real file.
78 If the file exists and matches BUILD_ID, return a BFD reference to it. */
80 static gdb_bfd_ref_ptr
81 build_id_to_debug_bfd_1 (const std::string &link, size_t build_id_len,
82 const bfd_byte *build_id)
84 if (separate_debug_file_debug)
86 gdb_printf (gdb_stdlog, _(" Trying %s..."), link.c_str ());
87 gdb_flush (gdb_stdlog);
90 /* lrealpath() is expensive even for the usually non-existent files. */
91 gdb::unique_xmalloc_ptr<char> filename_holder;
92 const char *filename = nullptr;
93 if (is_target_filename (link))
94 filename = link.c_str ();
95 else if (access (link.c_str (), F_OK) == 0)
97 filename_holder.reset (lrealpath (link.c_str ()));
98 filename = filename_holder.get ();
101 if (filename == NULL)
103 if (separate_debug_file_debug)
104 gdb_printf (gdb_stdlog,
105 _(" no, unable to compute real path\n"));
107 return {};
110 /* We expect to be silent on the non-existing files. */
111 gdb_bfd_ref_ptr debug_bfd = gdb_bfd_open (filename, gnutarget);
113 if (debug_bfd == NULL)
115 if (separate_debug_file_debug)
116 gdb_printf (gdb_stdlog, _(" no, unable to open.\n"));
118 return {};
121 if (!build_id_verify (debug_bfd.get(), build_id_len, build_id))
123 if (separate_debug_file_debug)
124 gdb_printf (gdb_stdlog, _(" no, build-id does not match.\n"));
126 return {};
129 if (separate_debug_file_debug)
130 gdb_printf (gdb_stdlog, _(" yes!\n"));
132 return debug_bfd;
135 /* Common code for finding BFDs of a given build-id. This function
136 works with both debuginfo files (SUFFIX == ".debug") and executable
137 files (SUFFIX == ""). */
139 static gdb_bfd_ref_ptr
140 build_id_to_bfd_suffix (size_t build_id_len, const bfd_byte *build_id,
141 const char *suffix)
143 /* Keep backward compatibility so that DEBUG_FILE_DIRECTORY being "" will
144 cause "/.build-id/..." lookups. */
146 std::vector<gdb::unique_xmalloc_ptr<char>> debugdir_vec
147 = dirnames_to_char_ptr_vec (debug_file_directory.c_str ());
149 for (const gdb::unique_xmalloc_ptr<char> &debugdir : debugdir_vec)
151 const gdb_byte *data = build_id;
152 size_t size = build_id_len;
154 /* Compute where the file named after the build-id would be.
156 If debugdir is "/usr/lib/debug" and the build-id is abcdef, this will
157 give "/usr/lib/debug/.build-id/ab/cdef.debug". */
158 std::string link = debugdir.get ();
159 link += "/.build-id/";
161 if (size > 0)
163 size--;
164 string_appendf (link, "%02x/", (unsigned) *data++);
167 while (size-- > 0)
168 string_appendf (link, "%02x", (unsigned) *data++);
170 link += suffix;
172 gdb_bfd_ref_ptr debug_bfd
173 = build_id_to_debug_bfd_1 (link, build_id_len, build_id);
174 if (debug_bfd != NULL)
175 return debug_bfd;
177 /* Try to look under the sysroot as well. If the sysroot is
178 "/the/sysroot", it will give
179 "/the/sysroot/usr/lib/debug/.build-id/ab/cdef.debug". */
181 if (!gdb_sysroot.empty ())
183 link = gdb_sysroot + link;
184 debug_bfd = build_id_to_debug_bfd_1 (link, build_id_len, build_id);
185 if (debug_bfd != NULL)
186 return debug_bfd;
190 return {};
193 /* See build-id.h. */
195 gdb_bfd_ref_ptr
196 build_id_to_debug_bfd (size_t build_id_len, const bfd_byte *build_id)
198 return build_id_to_bfd_suffix (build_id_len, build_id, ".debug");
201 /* See build-id.h. */
203 gdb_bfd_ref_ptr
204 build_id_to_exec_bfd (size_t build_id_len, const bfd_byte *build_id)
206 return build_id_to_bfd_suffix (build_id_len, build_id, "");
209 /* See build-id.h. */
211 std::string
212 find_separate_debug_file_by_buildid (struct objfile *objfile,
213 deferred_warnings *warnings)
215 const struct bfd_build_id *build_id;
217 build_id = build_id_bfd_get (objfile->obfd.get ());
218 if (build_id != NULL)
220 if (separate_debug_file_debug)
221 gdb_printf (gdb_stdlog,
222 _("\nLooking for separate debug info (build-id) for "
223 "%s\n"), objfile_name (objfile));
225 gdb_bfd_ref_ptr abfd (build_id_to_debug_bfd (build_id->size,
226 build_id->data));
227 /* Prevent looping on a stripped .debug file. */
228 if (abfd != NULL
229 && filename_cmp (bfd_get_filename (abfd.get ()),
230 objfile_name (objfile)) == 0)
232 if (separate_debug_file_debug)
233 gdb_printf (gdb_stdlog, "\"%s\": separate debug info file has no "
234 "debug info", bfd_get_filename (abfd.get ()));
235 warnings->warn (_("\"%ps\": separate debug info file has no "
236 "debug info"),
237 styled_string (file_name_style.style (),
238 bfd_get_filename (abfd.get ())));
240 else if (abfd != NULL)
241 return std::string (bfd_get_filename (abfd.get ()));
244 return std::string ();