1 /* build-id-related functions.
2 
3    Copyright (C) 1991-2024 Free Software Foundation, Inc.
4 
5    This file is part of GDB.
6 
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.
11 
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.
16 
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/>.  */
19 
20 #include "bfd.h"
21 #include "gdb_bfd.h"
22 #include "build-id.h"
23 #include "gdbsupport/gdb_vecs.h"
24 #include "symfile.h"
25 #include "objfiles.h"
26 #include "filenames.h"
27 #include "gdbcore.h"
28 #include "cli/cli-style.h"
29 
30 /* See build-id.h.  */
31 
32 const struct bfd_build_id *
build_id_bfd_get(bfd * abfd)33 build_id_bfd_get (bfd *abfd)
34 {
35   /* Dynamic objfiles such as ones created by JIT reader API
36      have no underlying bfd structure (that is, objfile->obfd
37      is NULL).  */
38   if (abfd == nullptr)
39     return nullptr;
40 
41   if (!bfd_check_format (abfd, bfd_object)
42       && !bfd_check_format (abfd, bfd_core))
43     return NULL;
44 
45   if (abfd->build_id != NULL)
46     return abfd->build_id;
47 
48   /* No build-id */
49   return NULL;
50 }
51 
52 /* See build-id.h.  */
53 
54 int
build_id_verify(bfd * abfd,size_t check_len,const bfd_byte * check)55 build_id_verify (bfd *abfd, size_t check_len, const bfd_byte *check)
56 {
57   const struct bfd_build_id *found;
58   int retval = 0;
59 
60   found = build_id_bfd_get (abfd);
61 
62   if (found == NULL)
63     warning (_("File \"%ps\" has no build-id, file skipped"),
64                styled_string (file_name_style.style (),
65                                   bfd_get_filename (abfd)));
66   else if (!build_id_equal (found, check_len, check))
67     warning (_("File \"%ps\" has a different build-id, file skipped"),
68                styled_string (file_name_style.style (),
69                                   bfd_get_filename (abfd)));
70   else
71     retval = 1;
72 
73   return retval;
74 }
75 
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.  */
79 
80 static gdb_bfd_ref_ptr
build_id_to_debug_bfd_1(const std::string & link,size_t build_id_len,const bfd_byte * build_id)81 build_id_to_debug_bfd_1 (const std::string &link, size_t build_id_len,
82                                const bfd_byte *build_id)
83 {
84   if (separate_debug_file_debug)
85     {
86       gdb_printf (gdb_stdlog, _("  Trying %s..."), link.c_str ());
87       gdb_flush (gdb_stdlog);
88     }
89 
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)
96     {
97       filename_holder.reset (lrealpath (link.c_str ()));
98       filename = filename_holder.get ();
99     }
100 
101   if (filename == NULL)
102     {
103       if (separate_debug_file_debug)
104           gdb_printf (gdb_stdlog,
105                         _(" no, unable to compute real path\n"));
106 
107       return {};
108     }
109 
110   /* We expect to be silent on the non-existing files.  */
111   gdb_bfd_ref_ptr debug_bfd = gdb_bfd_open (filename, gnutarget);
112 
113   if (debug_bfd == NULL)
114     {
115       if (separate_debug_file_debug)
116           gdb_printf (gdb_stdlog, _(" no, unable to open.\n"));
117 
118       return {};
119     }
120 
121   if (!build_id_verify (debug_bfd.get(), build_id_len, build_id))
122     {
123       if (separate_debug_file_debug)
124           gdb_printf (gdb_stdlog, _(" no, build-id does not match.\n"));
125 
126       return {};
127     }
128 
129   if (separate_debug_file_debug)
130     gdb_printf (gdb_stdlog, _(" yes!\n"));
131 
132   return debug_bfd;
133 }
134 
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 == "").  */
138 
139 static gdb_bfd_ref_ptr
build_id_to_bfd_suffix(size_t build_id_len,const bfd_byte * build_id,const char * suffix)140 build_id_to_bfd_suffix (size_t build_id_len, const bfd_byte *build_id,
141                               const char *suffix)
142 {
143   /* Keep backward compatibility so that DEBUG_FILE_DIRECTORY being "" will
144      cause "/.build-id/..." lookups.  */
145 
146   std::vector<gdb::unique_xmalloc_ptr<char>> debugdir_vec
147     = dirnames_to_char_ptr_vec (debug_file_directory.c_str ());
148 
149   for (const gdb::unique_xmalloc_ptr<char> &debugdir : debugdir_vec)
150     {
151       const gdb_byte *data = build_id;
152       size_t size = build_id_len;
153 
154       /* Compute where the file named after the build-id would be.
155 
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/";
160 
161       if (size > 0)
162           {
163             size--;
164             string_appendf (link, "%02x/", (unsigned) *data++);
165           }
166 
167       while (size-- > 0)
168           string_appendf (link, "%02x", (unsigned) *data++);
169 
170       link += suffix;
171 
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;
176 
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".  */
180 
181       if (!gdb_sysroot.empty ())
182           {
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;
187           }
188     }
189 
190   return {};
191 }
192 
193 /* See build-id.h.  */
194 
195 gdb_bfd_ref_ptr
build_id_to_debug_bfd(size_t build_id_len,const bfd_byte * build_id)196 build_id_to_debug_bfd (size_t build_id_len, const bfd_byte *build_id)
197 {
198   return build_id_to_bfd_suffix (build_id_len, build_id, ".debug");
199 }
200 
201 /* See build-id.h.  */
202 
203 gdb_bfd_ref_ptr
build_id_to_exec_bfd(size_t build_id_len,const bfd_byte * build_id)204 build_id_to_exec_bfd (size_t build_id_len, const bfd_byte *build_id)
205 {
206   return build_id_to_bfd_suffix (build_id_len, build_id, "");
207 }
208 
209 /* See build-id.h.  */
210 
211 std::string
find_separate_debug_file_by_buildid(struct objfile * objfile,deferred_warnings * warnings)212 find_separate_debug_file_by_buildid (struct objfile *objfile,
213                                              deferred_warnings *warnings)
214 {
215   const struct bfd_build_id *build_id;
216 
217   build_id = build_id_bfd_get (objfile->obfd.get ());
218   if (build_id != NULL)
219     {
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));
224 
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)
231           {
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 ())));
239           }
240       else if (abfd != NULL)
241           return std::string (bfd_get_filename (abfd.get ()));
242     }
243 
244   return std::string ();
245 }
246