1 From FEDORA_PATCHES Mon Sep 17 00:00:00 2001
2 From: Fedora GDB patches <invalid@email.com>
3 Date: Fri, 27 Oct 2017 21:07:50 +0200
4 Subject: gdb-6.6-buildid-locate.patch
6 ;; New locating of the matching binaries from the pure core file (build-id).
9 diff --git a/bfd/libbfd-in.h b/bfd/libbfd-in.h
12 @@ -110,7 +110,7 @@ static inline char *
13 bfd_strdup (const char *str)
15 size_t len = strlen (str) + 1;
16 - char *buf = bfd_malloc (len);
17 + char *buf = (char *) bfd_malloc (len);
19 memcpy (buf, str, len);
21 diff --git a/bfd/libbfd.h b/bfd/libbfd.h
24 @@ -116,7 +116,7 @@ static inline char *
25 bfd_strdup (const char *str)
27 size_t len = strlen (str) + 1;
28 - char *buf = bfd_malloc (len);
29 + char *buf = (char *) bfd_malloc (len);
31 memcpy (buf, str, len);
33 diff --git a/gdb/build-id.c b/gdb/build-id.c
37 #include "gdbsupport/gdb_vecs.h"
40 +#include <sys/stat.h>
42 +#include "elf/common.h"
43 +#include "elf/external.h"
44 +#include "elf/internal.h"
45 #include "filenames.h"
49 #include "cli/cli-style.h"
50 +#include "inferior.h"
51 +#include "objfiles.h"
52 +#include "observable.h"
55 +#define BUILD_ID_VERBOSE_NONE 0
56 +#define BUILD_ID_VERBOSE_FILENAMES 1
57 +#define BUILD_ID_VERBOSE_BINARY_PARSE 2
58 +static int build_id_verbose = BUILD_ID_VERBOSE_FILENAMES;
60 +show_build_id_verbose (struct ui_file *file, int from_tty,
61 + struct cmd_list_element *c, const char *value)
63 + gdb_printf (file, _("Verbosity level of the build-id locator is %s.\n"),
66 +/* Locate NT_GNU_BUILD_ID and return its matching debug filename.
67 + FIXME: NOTE decoding should be unified with the BFD core notes decoding. */
69 +static struct bfd_build_id *
70 +build_id_buf_get (bfd *templ, gdb_byte *buf, bfd_size_type size)
75 + while (p < buf + size)
77 + /* FIXME: bad alignment assumption. */
78 + Elf_External_Note *xnp = (Elf_External_Note *) p;
79 + size_t namesz = H_GET_32 (templ, xnp->namesz);
80 + size_t descsz = H_GET_32 (templ, xnp->descsz);
81 + bfd_byte *descdata = (gdb_byte *) xnp->name + BFD_ALIGN (namesz, 4);
83 + if (H_GET_32 (templ, xnp->type) == NT_GNU_BUILD_ID
84 + && namesz == sizeof "GNU"
85 + && memcmp (xnp->name, "GNU", sizeof "GNU") == 0)
88 + gdb_byte *data = (gdb_byte *) descdata;
89 + struct bfd_build_id *retval;
91 + retval = (struct bfd_build_id *) xmalloc (sizeof *retval - 1 + sz);
93 + memcpy (retval->data, data, sz);
97 + p = descdata + BFD_ALIGN (descsz, 4);
102 /* See build-id.h. */
104 const struct bfd_build_id *
105 -build_id_bfd_get (bfd *abfd)
106 +build_id_bfd_shdr_get (bfd *abfd)
108 /* Dynamic objfiles such as ones created by JIT reader API
109 have no underlying bfd structure (that is, objfile->obfd
110 @@ -50,6 +108,348 @@ build_id_bfd_get (bfd *abfd)
114 +/* Core files may have missing (corrupt) SHDR but PDHR is correct there.
115 + bfd_elf_bfd_from_remote_memory () has too much overhead by
116 + allocating/reading all the available ELF PT_LOADs. */
118 +static struct bfd_build_id *
119 +build_id_phdr_get (bfd *templ, bfd_vma loadbase, unsigned e_phnum,
120 + Elf_Internal_Phdr *i_phdr)
123 + struct bfd_build_id *retval = NULL;
125 + for (i = 0; i < e_phnum; i++)
126 + if (i_phdr[i].p_type == PT_NOTE && i_phdr[i].p_filesz > 0)
128 + Elf_Internal_Phdr *hdr = &i_phdr[i];
132 + buf = (gdb_byte *) xmalloc (hdr->p_filesz);
133 + err = target_read_memory (loadbase + i_phdr[i].p_vaddr, buf,
136 + retval = build_id_buf_get (templ, buf, hdr->p_filesz);
140 + if (retval != NULL)
146 +/* First we validate the file by reading in the ELF header and checking
147 + the magic number. */
149 +static inline bfd_boolean
150 +elf_file_p (Elf64_External_Ehdr *x_ehdrp64)
152 + gdb_assert (sizeof (Elf64_External_Ehdr) >= sizeof (Elf32_External_Ehdr));
153 + gdb_assert (offsetof (Elf64_External_Ehdr, e_ident)
154 + == offsetof (Elf32_External_Ehdr, e_ident));
155 + gdb_assert (sizeof (((Elf64_External_Ehdr *) 0)->e_ident)
156 + == sizeof (((Elf32_External_Ehdr *) 0)->e_ident));
158 + return ((x_ehdrp64->e_ident[EI_MAG0] == ELFMAG0)
159 + && (x_ehdrp64->e_ident[EI_MAG1] == ELFMAG1)
160 + && (x_ehdrp64->e_ident[EI_MAG2] == ELFMAG2)
161 + && (x_ehdrp64->e_ident[EI_MAG3] == ELFMAG3));
164 +/* Translate an ELF file header in external format into an ELF file header in
165 + internal format. */
167 +#define H_GET_WORD(bfd, ptr) (is64 ? H_GET_64 (bfd, (ptr)) \
168 + : H_GET_32 (bfd, (ptr)))
169 +#define H_GET_SIGNED_WORD(bfd, ptr) (is64 ? H_GET_S64 (bfd, (ptr)) \
170 + : H_GET_S32 (bfd, (ptr)))
173 +elf_swap_ehdr_in (bfd *abfd,
174 + const Elf64_External_Ehdr *src64,
175 + Elf_Internal_Ehdr *dst)
177 + int is64 = bfd_get_arch_size (abfd) == 64;
178 +#define SRC(field) (is64 ? src64->field \
179 + : ((const Elf32_External_Ehdr *) src64)->field)
181 + int signed_vma = get_elf_backend_data (abfd)->sign_extend_vma;
182 + memcpy (dst->e_ident, SRC (e_ident), EI_NIDENT);
183 + dst->e_type = H_GET_16 (abfd, SRC (e_type));
184 + dst->e_machine = H_GET_16 (abfd, SRC (e_machine));
185 + dst->e_version = H_GET_32 (abfd, SRC (e_version));
187 + dst->e_entry = H_GET_SIGNED_WORD (abfd, SRC (e_entry));
189 + dst->e_entry = H_GET_WORD (abfd, SRC (e_entry));
190 + dst->e_phoff = H_GET_WORD (abfd, SRC (e_phoff));
191 + dst->e_shoff = H_GET_WORD (abfd, SRC (e_shoff));
192 + dst->e_flags = H_GET_32 (abfd, SRC (e_flags));
193 + dst->e_ehsize = H_GET_16 (abfd, SRC (e_ehsize));
194 + dst->e_phentsize = H_GET_16 (abfd, SRC (e_phentsize));
195 + dst->e_phnum = H_GET_16 (abfd, SRC (e_phnum));
196 + dst->e_shentsize = H_GET_16 (abfd, SRC (e_shentsize));
197 + dst->e_shnum = H_GET_16 (abfd, SRC (e_shnum));
198 + dst->e_shstrndx = H_GET_16 (abfd, SRC (e_shstrndx));
203 +/* Translate an ELF program header table entry in external format into an
204 + ELF program header table entry in internal format. */
207 +elf_swap_phdr_in (bfd *abfd,
208 + const Elf64_External_Phdr *src64,
209 + Elf_Internal_Phdr *dst)
211 + int is64 = bfd_get_arch_size (abfd) == 64;
212 +#define SRC(field) (is64 ? src64->field \
213 + : ((const Elf32_External_Phdr *) src64)->field)
215 + int signed_vma = get_elf_backend_data (abfd)->sign_extend_vma;
217 + dst->p_type = H_GET_32 (abfd, SRC (p_type));
218 + dst->p_flags = H_GET_32 (abfd, SRC (p_flags));
219 + dst->p_offset = H_GET_WORD (abfd, SRC (p_offset));
222 + dst->p_vaddr = H_GET_SIGNED_WORD (abfd, SRC (p_vaddr));
223 + dst->p_paddr = H_GET_SIGNED_WORD (abfd, SRC (p_paddr));
227 + dst->p_vaddr = H_GET_WORD (abfd, SRC (p_vaddr));
228 + dst->p_paddr = H_GET_WORD (abfd, SRC (p_paddr));
230 + dst->p_filesz = H_GET_WORD (abfd, SRC (p_filesz));
231 + dst->p_memsz = H_GET_WORD (abfd, SRC (p_memsz));
232 + dst->p_align = H_GET_WORD (abfd, SRC (p_align));
237 +#undef H_GET_SIGNED_WORD
240 +static Elf_Internal_Phdr *
241 +elf_get_phdr (bfd *templ, bfd_vma ehdr_vma, unsigned *e_phnum_pointer,
242 + bfd_vma *loadbase_pointer)
244 + /* sizeof (Elf64_External_Ehdr) >= sizeof (Elf32_External_Ehdr) */
245 + Elf64_External_Ehdr x_ehdr64; /* Elf file header, external form */
246 + Elf_Internal_Ehdr i_ehdr; /* Elf file header, internal form */
247 + bfd_size_type x_phdrs_size;
248 + gdb_byte *x_phdrs_ptr;
249 + Elf_Internal_Phdr *i_phdrs;
255 + gdb_assert (templ != NULL);
256 + gdb_assert (sizeof (Elf64_External_Ehdr) >= sizeof (Elf32_External_Ehdr));
258 + /* Read in the ELF header in external format. */
259 + err = target_read_memory (ehdr_vma, (bfd_byte *) &x_ehdr64, sizeof x_ehdr64);
262 + if (build_id_verbose >= BUILD_ID_VERBOSE_BINARY_PARSE)
263 + warning (_("build-id: Error reading ELF header at address 0x%lx"),
264 + (unsigned long) ehdr_vma);
268 + /* Now check to see if we have a valid ELF file, and one that BFD can
269 + make use of. The magic number must match, the address size ('class')
270 + and byte-swapping must match our XVEC entry. */
272 + if (! elf_file_p (&x_ehdr64)
273 + || x_ehdr64.e_ident[EI_VERSION] != EV_CURRENT
274 + || !((bfd_get_arch_size (templ) == 64
275 + && x_ehdr64.e_ident[EI_CLASS] == ELFCLASS64)
276 + || (bfd_get_arch_size (templ) == 32
277 + && x_ehdr64.e_ident[EI_CLASS] == ELFCLASS32)))
279 + if (build_id_verbose >= BUILD_ID_VERBOSE_BINARY_PARSE)
280 + warning (_("build-id: Unrecognized ELF header at address 0x%lx"),
281 + (unsigned long) ehdr_vma);
285 + /* Check that file's byte order matches xvec's */
286 + switch (x_ehdr64.e_ident[EI_DATA])
288 + case ELFDATA2MSB: /* Big-endian */
289 + if (! bfd_header_big_endian (templ))
291 + if (build_id_verbose >= BUILD_ID_VERBOSE_BINARY_PARSE)
292 + warning (_("build-id: Unrecognized "
293 + "big-endian ELF header at address 0x%lx"),
294 + (unsigned long) ehdr_vma);
298 + case ELFDATA2LSB: /* Little-endian */
299 + if (! bfd_header_little_endian (templ))
301 + if (build_id_verbose >= BUILD_ID_VERBOSE_BINARY_PARSE)
302 + warning (_("build-id: Unrecognized "
303 + "little-endian ELF header at address 0x%lx"),
304 + (unsigned long) ehdr_vma);
308 + case ELFDATANONE: /* No data encoding specified */
309 + default: /* Unknown data encoding specified */
310 + if (build_id_verbose >= BUILD_ID_VERBOSE_BINARY_PARSE)
311 + warning (_("build-id: Unrecognized "
312 + "ELF header endianity at address 0x%lx"),
313 + (unsigned long) ehdr_vma);
317 + elf_swap_ehdr_in (templ, &x_ehdr64, &i_ehdr);
319 + /* The file header tells where to find the program headers.
320 + These are what we use to actually choose what to read. */
322 + if (i_ehdr.e_phentsize != (bfd_get_arch_size (templ) == 64
323 + ? sizeof (Elf64_External_Phdr)
324 + : sizeof (Elf32_External_Phdr))
325 + || i_ehdr.e_phnum == 0)
327 + if (build_id_verbose >= BUILD_ID_VERBOSE_BINARY_PARSE)
328 + warning (_("build-id: Invalid ELF program headers from the ELF header "
329 + "at address 0x%lx"), (unsigned long) ehdr_vma);
333 + x_phdrs_size = (bfd_get_arch_size (templ) == 64 ? sizeof (Elf64_External_Phdr)
334 + : sizeof (Elf32_External_Phdr));
336 + i_phdrs = (Elf_Internal_Phdr *) xmalloc (i_ehdr.e_phnum * (sizeof *i_phdrs + x_phdrs_size));
337 + x_phdrs_ptr = (gdb_byte *) &i_phdrs[i_ehdr.e_phnum];
338 + err = target_read_memory (ehdr_vma + i_ehdr.e_phoff, (bfd_byte *) x_phdrs_ptr,
339 + i_ehdr.e_phnum * x_phdrs_size);
343 + if (build_id_verbose >= BUILD_ID_VERBOSE_BINARY_PARSE)
344 + warning (_("build-id: Error reading "
345 + "ELF program headers at address 0x%lx"),
346 + (unsigned long) (ehdr_vma + i_ehdr.e_phoff));
350 + loadbase = ehdr_vma;
352 + for (i = 0; i < i_ehdr.e_phnum; ++i)
354 + elf_swap_phdr_in (templ, (Elf64_External_Phdr *)
355 + (x_phdrs_ptr + i * x_phdrs_size), &i_phdrs[i]);
356 + /* IA-64 vDSO may have two mappings for one segment, where one mapping
357 + is executable only, and one is read only. We must not use the
358 + executable one (PF_R is the first one, PF_X the second one). */
359 + if (i_phdrs[i].p_type == PT_LOAD && (i_phdrs[i].p_flags & PF_R))
361 + /* Only the first PT_LOAD segment indicates the file bias.
362 + Next segments may have P_VADDR arbitrarily higher.
363 + If the first segment has P_VADDR zero any next segment must not
364 + confuse us, the first one sets LOADBASE certainly enough. */
365 + if (!loadbase_set && i_phdrs[i].p_offset == 0)
367 + loadbase = ehdr_vma - i_phdrs[i].p_vaddr;
373 + if (build_id_verbose >= BUILD_ID_VERBOSE_BINARY_PARSE)
374 + warning (_("build-id: Found ELF header at address 0x%lx, loadbase 0x%lx"),
375 + (unsigned long) ehdr_vma, (unsigned long) loadbase);
377 + *e_phnum_pointer = i_ehdr.e_phnum;
378 + *loadbase_pointer = loadbase;
382 +/* BUILD_ID_ADDR_GET gets ADDR located somewhere in the object.
383 + Find the first section before ADDR containing an ELF header.
384 + We rely on the fact the sections from multiple files do not mix.
385 + FIXME: We should check ADDR is contained _inside_ the section with possibly
386 + missing content (P_FILESZ < P_MEMSZ). These omitted sections are currently
387 + hidden by _BFD_ELF_MAKE_SECTION_FROM_PHDR. */
389 +static CORE_ADDR build_id_addr;
390 +struct build_id_addr_sect
392 + struct build_id_addr_sect *next;
395 +static struct build_id_addr_sect *build_id_addr_sect;
397 +static void build_id_addr_candidate (bfd *abfd, asection *sect, void *obj)
399 + if (build_id_addr >= bfd_section_vma (sect))
401 + struct build_id_addr_sect *candidate;
403 + candidate = (struct build_id_addr_sect *) xmalloc (sizeof *candidate);
404 + candidate->next = build_id_addr_sect;
405 + build_id_addr_sect = candidate;
406 + candidate->sect = sect;
410 +struct bfd_build_id *
411 +build_id_addr_get (CORE_ADDR addr)
413 + struct build_id_addr_sect *candidate;
414 + struct bfd_build_id *retval = NULL;
415 + Elf_Internal_Phdr *i_phdr = NULL;
416 + bfd_vma loadbase = 0;
417 + unsigned e_phnum = 0;
419 + if (core_bfd == NULL)
422 + build_id_addr = addr;
423 + gdb_assert (build_id_addr_sect == NULL);
424 + bfd_map_over_sections (core_bfd, build_id_addr_candidate, NULL);
426 + /* Sections are sorted in the high-to-low VMAs order.
427 + Stop the search on the first ELF header we find.
428 + Do not continue the search even if it does not contain NT_GNU_BUILD_ID. */
430 + for (candidate = build_id_addr_sect; candidate != NULL;
431 + candidate = candidate->next)
433 + i_phdr = elf_get_phdr (core_bfd,
434 + bfd_section_vma (candidate->sect),
435 + &e_phnum, &loadbase);
436 + if (i_phdr != NULL)
440 + if (i_phdr != NULL)
442 + retval = build_id_phdr_get (core_bfd, loadbase, e_phnum, i_phdr);
446 + while (build_id_addr_sect != NULL)
448 + candidate = build_id_addr_sect;
449 + build_id_addr_sect = candidate->next;
456 /* See build-id.h. */
459 @@ -58,7 +458,7 @@ build_id_verify (bfd *abfd, size_t check_len, const bfd_byte *check)
460 const struct bfd_build_id *found;
463 - found = build_id_bfd_get (abfd);
464 + found = build_id_bfd_shdr_get (abfd);
467 warning (_("File \"%s\" has no build-id, file skipped"),
468 @@ -73,63 +473,166 @@ build_id_verify (bfd *abfd, size_t check_len, const bfd_byte *check)
473 +link_resolve (const char *symlink, int level)
475 + char buf[PATH_MAX + 1], *retval;
476 + gdb::unique_xmalloc_ptr<char> target;
480 + return xstrdup (symlink);
482 + got = readlink (symlink, buf, sizeof (buf));
483 + if (got < 0 || got >= sizeof (buf))
484 + return xstrdup (symlink);
487 + if (IS_ABSOLUTE_PATH (buf))
488 + target = make_unique_xstrdup (buf);
491 + const std::string dir (ldirname (symlink));
493 + target = xstrprintf ("%s"
494 +#ifndef HAVE_DOS_BASED_FILE_SYSTEM
496 +#else /* HAVE_DOS_BASED_FILE_SYSTEM */
498 +#endif /* HAVE_DOS_BASED_FILE_SYSTEM */
499 + "%s", dir.c_str(), buf);
502 + retval = link_resolve (target.get (), level + 1);
506 /* Helper for build_id_to_debug_bfd. LINK is a path to a potential
507 build-id-based separate debug file, potentially a symlink to the real file.
508 If the file exists and matches BUILD_ID, return a BFD reference to it. */
510 static gdb_bfd_ref_ptr
511 -build_id_to_debug_bfd_1 (const std::string &link, size_t build_id_len,
512 - const bfd_byte *build_id)
513 +build_id_to_debug_bfd_1 (const std::string &orig_link, size_t build_id_len,
514 + const bfd_byte *build_id, char **link_return)
516 + gdb_bfd_ref_ptr ret_bfd = {};
517 + std::string ret_link;
519 if (separate_debug_file_debug)
521 - gdb_printf (gdb_stdlog, _(" Trying %s..."), link.c_str ());
522 - gdb_flush (gdb_stdlog);
523 + gdb_printf (gdb_stdlog, _(" Trying %s..."), orig_link.c_str ());
524 + gdb_flush (gdb_stdout);
527 - /* lrealpath() is expensive even for the usually non-existent files. */
528 - gdb::unique_xmalloc_ptr<char> filename_holder;
529 - const char *filename = nullptr;
530 - if (startswith (link, TARGET_SYSROOT_PREFIX))
531 - filename = link.c_str ();
532 - else if (access (link.c_str (), F_OK) == 0)
533 + for (unsigned seqno = 0;; seqno++)
535 - filename_holder.reset (lrealpath (link.c_str ()));
536 - filename = filename_holder.get ();
538 + std::string link = orig_link;
540 - if (filename == NULL)
542 - if (separate_debug_file_debug)
543 - gdb_printf (gdb_stdlog,
544 - _(" no, unable to compute real path\n"));
547 + /* There can be multiple build-id symlinks pointing to real files
548 + with the same build-id (such as hard links). Some of the real
549 + files may not be installed. */
553 + string_appendf (link, ".%u", seqno);
556 - /* We expect to be silent on the non-existing files. */
557 - gdb_bfd_ref_ptr debug_bfd = gdb_bfd_open (filename, gnutarget);
560 - if (debug_bfd == NULL)
562 - if (separate_debug_file_debug)
563 - gdb_printf (gdb_stdlog, _(" no, unable to open.\n"));
564 + struct stat statbuf_trash;
566 + /* `access' automatically dereferences LINK. */
567 + if (lstat (link.c_str (), &statbuf_trash) != 0)
569 + /* Stop increasing SEQNO. */
573 + /* lrealpath() is expensive even for the usually non-existent files. */
574 + gdb::unique_xmalloc_ptr<char> filename_holder;
575 + const char *filename = nullptr;
576 + if (startswith (link, TARGET_SYSROOT_PREFIX))
577 + filename = link.c_str ();
578 + else if (access (link.c_str (), F_OK) == 0)
580 + filename_holder.reset (lrealpath (link.c_str ()));
581 + filename = filename_holder.get ();
584 + if (filename == NULL)
586 + if (separate_debug_file_debug)
587 + gdb_printf (gdb_stdlog,
588 + _(" no, unable to compute real path\n"));
593 + /* We expect to be silent on the non-existing files. */
594 + gdb_bfd_ref_ptr debug_bfd = gdb_bfd_open (filename, gnutarget);
596 + if (debug_bfd == NULL)
598 + if (separate_debug_file_debug)
599 + gdb_printf (gdb_stdlog, _(" no, unable to open.\n"));
605 + if (!build_id_verify (debug_bfd.get(), build_id_len, build_id))
607 + if (separate_debug_file_debug)
608 + gdb_printf (gdb_stdlog,
609 + _(" no, build-id does not match.\n"));
614 + ret_bfd = debug_bfd;
618 - if (!build_id_verify (debug_bfd.get(), build_id_len, build_id))
619 + std::string link_all;
621 + if (ret_bfd != NULL)
623 if (separate_debug_file_debug)
624 - gdb_printf (gdb_stdlog, _(" no, build-id does not match.\n"));
625 + gdb_printf (gdb_stdlog, _(" yes!\n"));
629 + /* If none of the real files is found report as missing file
630 + always the non-.%u-suffixed file. */
631 + std::string link0 = orig_link;
634 + /* If the symlink has target request to install the target.
635 + BASE-debuginfo.rpm contains the symlink but BASE.rpm may be missing.
636 + https://bugzilla.redhat.com/show_bug.cgi?id=981154 */
637 + std::string link0_resolved (link_resolve (link0.c_str (), 0));
639 + if (link_all.empty ())
640 + link_all = link0_resolved;
643 + /* Use whitespace instead of DIRNAME_SEPARATOR to be compatible with
644 + its possible use as an argument for installation command. */
645 + link_all += " " + link0_resolved;
649 - if (separate_debug_file_debug)
650 - gdb_printf (gdb_stdlog, _(" yes!\n"));
651 + if (link_return != NULL)
653 + if (ret_bfd != NULL)
655 + *link_return = xstrdup (ret_link.c_str ());
659 + *link_return = xstrdup (link_all.c_str ());
667 /* Common code for finding BFDs of a given build-id. This function
668 @@ -138,7 +641,7 @@ build_id_to_debug_bfd_1 (const std::string &link, size_t build_id_len,
670 static gdb_bfd_ref_ptr
671 build_id_to_bfd_suffix (size_t build_id_len, const bfd_byte *build_id,
672 - const char *suffix)
673 + const char *suffix, char **link_return)
675 /* Keep backward compatibility so that DEBUG_FILE_DIRECTORY being "" will
676 cause "/.build-id/..." lookups. */
677 @@ -161,16 +664,17 @@ build_id_to_bfd_suffix (size_t build_id_len, const bfd_byte *build_id,
681 - string_appendf (link, "%02x/", (unsigned) *data++);
682 + string_appendf (link, "%02x", (unsigned) *data++);
688 string_appendf (link, "%02x", (unsigned) *data++);
692 gdb_bfd_ref_ptr debug_bfd
693 - = build_id_to_debug_bfd_1 (link, build_id_len, build_id);
694 + = build_id_to_debug_bfd_1 (link, build_id_len, build_id, link_return);
695 if (debug_bfd != NULL)
698 @@ -181,7 +685,7 @@ build_id_to_bfd_suffix (size_t build_id_len, const bfd_byte *build_id,
699 if (!gdb_sysroot.empty ())
701 link = gdb_sysroot + link;
702 - debug_bfd = build_id_to_debug_bfd_1 (link, build_id_len, build_id);
703 + debug_bfd = build_id_to_debug_bfd_1 (link, build_id_len, build_id, NULL);
704 if (debug_bfd != NULL)
707 @@ -190,31 +694,663 @@ build_id_to_bfd_suffix (size_t build_id_len, const bfd_byte *build_id,
712 +build_id_to_filename (const struct bfd_build_id *build_id, char **link_return)
714 + gdb_bfd_ref_ptr abfd;
717 + abfd = build_id_to_exec_bfd (build_id->size, build_id->data, link_return);
721 + result = xstrdup (bfd_get_filename (abfd.get ()));
725 +void debug_flush_missing (void);
729 +#include <rpm/rpmlib.h>
730 +#include <rpm/rpmts.h>
731 +#include <rpm/rpmdb.h>
732 +#include <rpm/header.h>
733 +#ifdef DLOPEN_LIBRPM
737 +/* Workarodun https://bugzilla.redhat.com/show_bug.cgi?id=643031
738 + librpm must not exit() an application on SIGINT
740 + Enable or disable a signal handler. SIGNUM: signal to enable (or disable
741 + if negative). HANDLER: sa_sigaction handler (or NULL to use
742 + rpmsqHandler()). Returns: no. of refs, -1 on error. */
743 +extern int rpmsqEnable (int signum, /* rpmsqAction_t handler */ void *handler);
745 +rpmsqEnable (int signum, /* rpmsqAction_t handler */ void *handler)
750 +/* This MISSING_RPM_HASH tracker is used to collect all the missing rpm files
751 + and avoid their duplicities during a single inferior run. */
753 +static struct htab *missing_rpm_hash;
755 +/* This MISSING_RPM_LIST tracker is used to collect and print as a single line
756 + all the rpms right before the nearest GDB prompt. It gets cleared after
757 + each such print (it is questionable if we should clear it after the print).
762 + struct missing_rpm *next;
765 +static struct missing_rpm *missing_rpm_list;
766 +static int missing_rpm_list_entries;
768 +/* Returns the count of newly added rpms. */
771 +#ifndef GDB_INDEX_VERIFY_VENDOR
772 +missing_rpm_enlist (const char *filename)
774 +missing_rpm_enlist_1 (const char *filename, int verify_vendor)
777 + static int rpm_init_done = 0;
779 + rpmdbMatchIterator mi;
782 +#ifdef DLOPEN_LIBRPM
783 + /* Duplicate here the declarations to verify they match. The same sanity
784 + check is present also in `configure.ac'. */
785 + extern char * headerFormat(Header h, const char * fmt, errmsg_t * errmsg);
786 + static char *(*headerFormat_p) (Header h, const char * fmt, errmsg_t *errmsg);
787 + extern int rpmReadConfigFiles(const char * file, const char * target);
788 + static int (*rpmReadConfigFiles_p) (const char * file, const char * target);
789 + extern rpmdbMatchIterator rpmdbFreeIterator(rpmdbMatchIterator mi);
790 + static rpmdbMatchIterator (*rpmdbFreeIterator_p) (rpmdbMatchIterator mi);
791 + extern Header rpmdbNextIterator(rpmdbMatchIterator mi);
792 + static Header (*rpmdbNextIterator_p) (rpmdbMatchIterator mi);
793 + extern rpmts rpmtsCreate(void);
794 + static rpmts (*rpmtsCreate_p) (void);
795 + extern rpmts rpmtsFree(rpmts ts);
796 + static rpmts (*rpmtsFree_p) (rpmts ts);
797 + extern rpmdbMatchIterator rpmtsInitIterator(const rpmts ts, rpmTag rpmtag,
798 + const void * keyp, size_t keylen);
799 + static rpmdbMatchIterator (*rpmtsInitIterator_p) (const rpmts ts,
803 +#else /* !DLOPEN_LIBRPM */
804 +# define headerFormat_p headerFormat
805 +# define rpmReadConfigFiles_p rpmReadConfigFiles
806 +# define rpmdbFreeIterator_p rpmdbFreeIterator
807 +# define rpmdbNextIterator_p rpmdbNextIterator
808 +# define rpmtsCreate_p rpmtsCreate
809 +# define rpmtsFree_p rpmtsFree
810 +# define rpmtsInitIterator_p rpmtsInitIterator
811 +#endif /* !DLOPEN_LIBRPM */
813 + gdb_assert (filename != NULL);
815 + if (strcmp (filename, BUILD_ID_MAIN_EXECUTABLE_FILENAME) == 0)
818 + if (is_target_filename (filename))
821 + if (filename[0] != '/')
823 + warning (_("Ignoring non-absolute filename: <%s>"), filename);
827 + if (!rpm_init_done)
829 + static int init_tried;
831 + /* Already failed the initialization before? */
836 +#ifdef DLOPEN_LIBRPM
840 + h = dlopen (DLOPEN_LIBRPM, RTLD_LAZY);
843 + warning (_("Unable to open \"%s\" (%s), "
844 + "missing debuginfos notifications will not be displayed"),
845 + DLOPEN_LIBRPM, dlerror ());
849 + if (!((headerFormat_p = (char *(*) (Header h, const char * fmt, errmsg_t *errmsg)) dlsym (h, "headerFormat"))
850 + && (rpmReadConfigFiles_p = (int (*) (const char * file, const char * target)) dlsym (h, "rpmReadConfigFiles"))
851 + && (rpmdbFreeIterator_p = (rpmdbMatchIterator (*) (rpmdbMatchIterator mi)) dlsym (h, "rpmdbFreeIterator"))
852 + && (rpmdbNextIterator_p = (Header (*) (rpmdbMatchIterator mi)) dlsym (h, "rpmdbNextIterator"))
853 + && (rpmtsCreate_p = (rpmts (*) (void)) dlsym (h, "rpmtsCreate"))
854 + && (rpmtsFree_p = (rpmts (*) (rpmts ts)) dlsym (h, "rpmtsFree"))
855 + && (rpmtsInitIterator_p = (rpmdbMatchIterator (*) (const rpmts ts, rpmTag rpmtag, const void *keyp, size_t keylen)) dlsym (h, "rpmtsInitIterator"))))
857 + warning (_("Opened library \"%s\" is incompatible (%s), "
858 + "missing debuginfos notifications will not be displayed"),
859 + DLOPEN_LIBRPM, dlerror ());
861 + warning (_("Error closing library \"%s\": %s\n"), DLOPEN_LIBRPM,
866 +#endif /* DLOPEN_LIBRPM */
868 + if (rpmReadConfigFiles_p (NULL, NULL) != 0)
870 + warning (_("Error reading the rpm configuration files"));
877 + ts = rpmtsCreate_p ();
879 + mi = rpmtsInitIterator_p (ts, RPMTAG_BASENAMES, filename, 0);
882 +#ifndef GDB_INDEX_VERIFY_VENDOR
885 + if (!verify_vendor) for (;;)
889 + char *debuginfo, **slot, *s, *s2;
891 + size_t srcrpmlen = sizeof (".src.rpm") - 1;
892 + size_t debuginfolen = sizeof ("-debuginfo") - 1;
893 + rpmdbMatchIterator mi_debuginfo;
895 + h = rpmdbNextIterator_p (mi);
899 + /* Verify the debuginfo file is not already installed. */
901 + debuginfo = headerFormat_p (h, "%{sourcerpm}-debuginfo.%{arch}",
905 + warning (_("Error querying the rpm file `%s': %s"), filename,
909 + /* s = `.src.rpm-debuginfo.%{arch}' */
910 + s = strrchr (debuginfo, '-') - srcrpmlen;
912 + if (s > debuginfo && memcmp (s, ".src.rpm", srcrpmlen) == 0)
914 + /* s2 = `-%{release}.src.rpm-debuginfo.%{arch}' */
915 + s2 = (char *) memrchr (debuginfo, '-', s - debuginfo);
919 + /* s2 = `-%{version}-%{release}.src.rpm-debuginfo.%{arch}' */
920 + s2 = (char *) memrchr (debuginfo, '-', s2 - debuginfo);
924 + warning (_("Error querying the rpm file `%s': %s"), filename,
929 + /* s = `.src.rpm-debuginfo.%{arch}' */
930 + /* s2 = `-%{version}-%{release}.src.rpm-debuginfo.%{arch}' */
931 + memmove (s2 + debuginfolen, s2, s - s2);
932 + memcpy (s2, "-debuginfo", debuginfolen);
933 + /* s = `XXXX.%{arch}' */
934 + /* strlen ("XXXX") == srcrpmlen + debuginfolen */
935 + /* s2 = `-debuginfo-%{version}-%{release}XX.%{arch}' */
936 + /* strlen ("XX") == srcrpmlen */
937 + memmove (s + debuginfolen, s + srcrpmlen + debuginfolen,
938 + strlen (s + srcrpmlen + debuginfolen) + 1);
939 + /* s = `-debuginfo-%{version}-%{release}.%{arch}' */
941 + /* RPMDBI_PACKAGES requires keylen == sizeof (int). */
942 + /* RPMDBI_LABEL is an interface for NVR-based dbiFindByLabel(). */
943 + mi_debuginfo = rpmtsInitIterator_p (ts, (rpmTag) RPMDBI_LABEL, debuginfo, 0);
947 + rpmdbFreeIterator_p (mi_debuginfo);
952 + /* The allocated memory gets utilized below for MISSING_RPM_HASH. */
953 + debuginfo = headerFormat_p (h,
954 + "%{name}-%{version}-%{release}.%{arch}",
958 + warning (_("Error querying the rpm file `%s': %s"), filename,
963 + /* Base package name for `debuginfo-install'. We do not use the
964 + `yum' command directly as the line
965 + yum --enablerepo='*debug*' install NAME-debuginfo.ARCH
966 + would be more complicated than just:
967 + debuginfo-install NAME-VERSION-RELEASE.ARCH
968 + Do not supply the rpm base name (derived from .src.rpm name) as
969 + debuginfo-install is unable to install the debuginfo package if
970 + the base name PKG binary rpm is not installed while for example
971 + PKG-libs would be installed (RH Bug 467901).
972 + FUTURE: After multiple debuginfo versions simultaneously installed
973 + get supported the support for the VERSION-RELEASE tags handling
974 + may need an update. */
976 + if (missing_rpm_hash == NULL)
978 + /* DEL_F is passed NULL as MISSING_RPM_LIST's HTAB_DELETE
979 + should not deallocate the entries. */
981 + missing_rpm_hash = htab_create_alloc (64, htab_hash_string,
982 + (int (*) (const void *, const void *)) streq,
983 + NULL, xcalloc, xfree);
985 + slot = (char **) htab_find_slot (missing_rpm_hash, debuginfo, INSERT);
986 + /* XCALLOC never returns NULL. */
987 + gdb_assert (slot != NULL);
990 + struct missing_rpm *missing_rpm;
994 + missing_rpm = (struct missing_rpm *) xmalloc (sizeof (*missing_rpm) + strlen (debuginfo));
995 + strcpy (missing_rpm->rpm, debuginfo);
996 + missing_rpm->next = missing_rpm_list;
997 + missing_rpm_list = missing_rpm;
998 + missing_rpm_list_entries++;
1001 + xfree (debuginfo);
1004 +#ifdef GDB_INDEX_VERIFY_VENDOR
1005 + else /* verify_vendor */
1007 + int vendor_pass = 0, vendor_fail = 0;
1015 + h = rpmdbNextIterator_p (mi);
1019 + vendor = headerFormat_p (h, "%{vendor}", &err);
1022 + warning (_("Error querying the rpm file `%s': %s"), filename,
1026 + if (strcmp (vendor, "Red Hat, Inc.") == 0)
1032 + count = vendor_pass != 0 && vendor_fail == 0;
1036 + rpmdbFreeIterator_p (mi);
1044 +#ifdef GDB_INDEX_VERIFY_VENDOR
1045 +missing_rpm_enlist (const char *filename)
1047 + return missing_rpm_enlist_1 (filename, 0);
1050 +extern int rpm_verify_vendor (const char *filename);
1052 +rpm_verify_vendor (const char *filename)
1054 + return missing_rpm_enlist_1 (filename, 1);
1059 +missing_rpm_list_compar (const char *ap, const char *bp)
1061 + return strcoll (ap, bp) < 0;
1064 +/* It returns a NULL-terminated array of strings needing to be FREEd. It may
1065 + also return only NULL. */
1068 +missing_rpm_list_print (void)
1070 + struct missing_rpm *list_iter;
1072 + if (missing_rpm_list_entries == 0)
1075 + std::vector<const char *> array (missing_rpm_list_entries);
1078 + for (list_iter = missing_rpm_list; list_iter != NULL;
1079 + list_iter = list_iter->next)
1081 + array[idx++] = list_iter->rpm;
1083 + gdb_assert (idx == missing_rpm_list_entries);
1085 + std::sort (array.begin (), array.end (), missing_rpm_list_compar);
1087 + /* We zero out the number of missing RPMs here because of a nasty
1088 + bug (see RHBZ 1801974).
1090 + When we call 'puts_unfiltered' below, if pagination is on and if
1091 + the number of missing RPMs is big enough to trigger pagination,
1092 + we will end up in an infinite recursion. The call chain looks
1095 + missing_rpm_list_print -> puts_unfiltered -> fputs_maybe_filtered
1096 + -> prompt_for_continue -> display_gdb_prompt ->
1097 + debug_flush_missing -> missing_rpm_list_print ...
1099 + For this reason, we make sure MISSING_RPM_LIST_ENTRIES is zero
1100 + *before* calling any print function.
1102 + Note: kevinb/2023-02-22: The code below used to call
1103 + puts_unfiltered() and printf_unfiltered(), but calls to these
1104 + functions have been replaced by calls to gdb_printf(). The call
1105 + chain shown above (probably) used to be the case at one time and
1106 + hopefully something similar is still the case now that
1107 + gdb_printf() is being used instead. */
1108 + missing_rpm_list_entries = 0;
1110 + gdb_printf (_("Missing separate debuginfos, use: %s"),
1111 +#ifdef DNF_DEBUGINFO_INSTALL
1114 + "debuginfo-install");
1115 + for (const char *el : array)
1117 + gdb_printf (" %s", el);
1119 + gdb_printf ("\n");
1121 + while (missing_rpm_list != NULL)
1123 + list_iter = missing_rpm_list;
1124 + missing_rpm_list = list_iter->next;
1125 + xfree (list_iter);
1130 +missing_rpm_change (void)
1132 + debug_flush_missing ();
1134 + gdb_assert (missing_rpm_list == NULL);
1135 + if (missing_rpm_hash != NULL)
1137 + htab_delete (missing_rpm_hash);
1138 + missing_rpm_hash = NULL;
1144 + /* Init state. EXEC_BFD also still could be NULL. */
1145 + MISSING_EXEC_NOT_TRIED,
1146 + /* We saw a non-NULL EXEC_BFD but RPM has no info about it. */
1147 + MISSING_EXEC_NOT_FOUND,
1148 + /* We found EXEC_BFD by RPM and we either have its symbols (either embedded
1149 + or separate) or the main executable's RPM is now contained in
1150 + MISSING_RPM_HASH. */
1151 + MISSING_EXEC_ENLISTED
1153 +static enum missing_exec missing_exec = MISSING_EXEC_NOT_TRIED;
1155 +#endif /* HAVE_LIBRPM */
1158 +debug_flush_missing (void)
1161 + missing_rpm_list_print ();
1165 +/* This MISSING_FILEPAIR_HASH tracker is used only for the duplicite messages
1166 + yum --enablerepo='*debug*' install ...
1169 +struct missing_filepair
1176 +static struct htab *missing_filepair_hash;
1177 +static struct obstack missing_filepair_obstack;
1180 +missing_filepair_xcalloc (size_t nmemb, size_t nmemb_size)
1183 + size_t size = nmemb * nmemb_size;
1185 + retval = obstack_alloc (&missing_filepair_obstack, size);
1186 + memset (retval, 0, size);
1191 +missing_filepair_hash_func (const struct missing_filepair *elem)
1193 + hashval_t retval = 0;
1195 + retval ^= htab_hash_string (elem->binary);
1196 + if (elem->debug != NULL)
1197 + retval ^= htab_hash_string (elem->debug);
1203 +missing_filepair_eq (const struct missing_filepair *elem1,
1204 + const struct missing_filepair *elem2)
1206 + return strcmp (elem1->binary, elem2->binary) == 0
1207 + && ((elem1->debug == NULL) == (elem2->debug == NULL))
1208 + && (elem1->debug == NULL || strcmp (elem1->debug, elem2->debug) == 0);
1212 +missing_filepair_change (void)
1214 + if (missing_filepair_hash != NULL)
1216 + obstack_free (&missing_filepair_obstack, NULL);
1217 + /* All their memory came just from missing_filepair_OBSTACK. */
1218 + missing_filepair_hash = NULL;
1221 + missing_exec = MISSING_EXEC_NOT_TRIED;
1226 +debug_print_executable_changed (struct program_space *pspace, bool reload_p)
1229 + missing_rpm_change ();
1231 + missing_filepair_change ();
1234 +/* Notify user the file BINARY with (possibly NULL) associated separate debug
1235 + information file DEBUG is missing. DEBUG may or may not be the build-id
1236 + file such as would be:
1237 + /usr/lib/debug/.build-id/dd/b1d2ce632721c47bb9e8679f369e2295ce71be.debug
1241 +debug_print_missing (const char *binary, const char *debug)
1243 + size_t binary_len0 = strlen (binary) + 1;
1244 + size_t debug_len0 = debug ? strlen (debug) + 1 : 0;
1245 + struct missing_filepair missing_filepair_find;
1246 + struct missing_filepair *missing_filepair;
1247 + struct missing_filepair **slot;
1249 + if (build_id_verbose < BUILD_ID_VERBOSE_FILENAMES)
1252 + if (missing_filepair_hash == NULL)
1254 + obstack_init (&missing_filepair_obstack);
1255 + missing_filepair_hash = htab_create_alloc (64,
1256 + (hashval_t (*) (const void *)) missing_filepair_hash_func,
1257 + (int (*) (const void *, const void *)) missing_filepair_eq, NULL,
1258 + missing_filepair_xcalloc, NULL);
1261 + /* Use MISSING_FILEPAIR_FIND first instead of calling obstack_alloc with
1262 + obstack_free in the case of a (rare) match. The problem is ALLOC_F for
1263 + MISSING_FILEPAIR_HASH allocates from MISSING_FILEPAIR_OBSTACK maintenance
1264 + structures for MISSING_FILEPAIR_HASH. Calling obstack_free would possibly
1265 + not to free only MISSING_FILEPAIR but also some such structures (allocated
1266 + during the htab_find_slot call). */
1268 + missing_filepair_find.binary = (char *) binary;
1269 + missing_filepair_find.debug = (char *) debug;
1270 + slot = (struct missing_filepair **) htab_find_slot (missing_filepair_hash,
1271 + &missing_filepair_find,
1274 + /* While it may be still printed duplicitely with the missing debuginfo file
1275 + * it is due to once printing about the binary file build-id link and once
1276 + * about the .debug file build-id link as both the build-id symlinks are
1277 + * located in the debuginfo package. */
1279 + if (*slot != NULL)
1282 + missing_filepair = (struct missing_filepair *) obstack_alloc (&missing_filepair_obstack,
1283 + sizeof (*missing_filepair) - 1
1284 + + binary_len0 + debug_len0);
1285 + missing_filepair->binary = missing_filepair->data;
1286 + memcpy (missing_filepair->binary, binary, binary_len0);
1287 + if (debug != NULL)
1289 + missing_filepair->debug = missing_filepair->binary + binary_len0;
1290 + memcpy (missing_filepair->debug, debug, debug_len0);
1293 + missing_filepair->debug = NULL;
1295 + *slot = missing_filepair;
1298 + if (missing_exec == MISSING_EXEC_NOT_TRIED)
1300 + const char *execfilename = get_exec_file (0);
1302 + if (execfilename != NULL)
1304 + if (missing_rpm_enlist (execfilename) == 0)
1305 + missing_exec = MISSING_EXEC_NOT_FOUND;
1307 + missing_exec = MISSING_EXEC_ENLISTED;
1310 + if (missing_exec != MISSING_EXEC_ENLISTED)
1311 + if ((binary[0] == 0 || missing_rpm_enlist (binary) == 0)
1312 + && (debug == NULL || missing_rpm_enlist (debug) == 0))
1313 +#endif /* HAVE_LIBRPM */
1315 + /* We do not collect and flush these messages as each such message
1316 + already requires its own separate lines. */
1318 + gdb_printf (gdb_stdlog,
1319 + _("Missing separate debuginfo for %s.\n"), binary);
1320 + if (debug != NULL)
1322 + if (access (debug, F_OK) == 0) {
1323 + gdb_printf (gdb_stdlog, _("Try: %s %s\n"),
1324 +#ifdef DNF_DEBUGINFO_INSTALL
1329 + " --enablerepo='*debug*' install", debug);
1331 + gdb_printf (gdb_stdlog, _("The debuginfo package for this file is probably broken.\n"));
1336 /* See build-id.h. */
1339 -build_id_to_debug_bfd (size_t build_id_len, const bfd_byte *build_id)
1340 +build_id_to_debug_bfd (size_t build_id_len, const bfd_byte *build_id,
1341 + char **link_return)
1343 - return build_id_to_bfd_suffix (build_id_len, build_id, ".debug");
1344 + return build_id_to_bfd_suffix (build_id_len, build_id, ".debug",
1348 /* See build-id.h. */
1351 -build_id_to_exec_bfd (size_t build_id_len, const bfd_byte *build_id)
1352 +build_id_to_exec_bfd (size_t build_id_len, const bfd_byte *build_id,
1353 + char **link_return)
1355 - return build_id_to_bfd_suffix (build_id_len, build_id, "");
1356 + return build_id_to_bfd_suffix (build_id_len, build_id, "", link_return);
1359 /* See build-id.h. */
1362 find_separate_debug_file_by_buildid (struct objfile *objfile,
1363 - deferred_warnings *warnings)
1364 + deferred_warnings *warnings,
1365 + gdb::unique_xmalloc_ptr<char> *build_id_filename_return)
1367 const struct bfd_build_id *build_id;
1369 - build_id = build_id_bfd_get (objfile->obfd.get ());
1370 + if (build_id_filename_return)
1371 + *build_id_filename_return = NULL;
1373 + build_id = build_id_bfd_shdr_get (objfile->obfd.get ());
1374 if (build_id != NULL)
1376 if (separate_debug_file_debug)
1377 @@ -222,8 +1358,21 @@ find_separate_debug_file_by_buildid (struct objfile *objfile,
1378 _("\nLooking for separate debug info (build-id) for "
1379 "%s\n"), objfile_name (objfile));
1381 + char *build_id_filename_cstr = NULL;
1382 gdb_bfd_ref_ptr abfd (build_id_to_debug_bfd (build_id->size,
1385 + (!build_id_filename_return ? NULL : &build_id_filename_cstr)));
1386 + if (build_id_filename_return)
1388 + if (!build_id_filename_cstr)
1389 + gdb_assert (!*build_id_filename_return);
1392 + *build_id_filename_return = gdb::unique_xmalloc_ptr<char> (build_id_filename_cstr);
1393 + build_id_filename_cstr = NULL;
1397 /* Prevent looping on a stripped .debug file. */
1399 && filename_cmp (bfd_get_filename (abfd.get ()),
1400 @@ -243,3 +1392,22 @@ find_separate_debug_file_by_buildid (struct objfile *objfile,
1402 return std::string ();
1405 +void _initialize_build_id ();
1408 +_initialize_build_id ()
1410 + add_setshow_zinteger_cmd ("build-id-verbose", no_class, &build_id_verbose,
1412 +Set debugging level of the build-id locator."), _("\
1413 +Show debugging level of the build-id locator."), _("\
1414 +Level 1 (default) enables printing the missing debug filenames,\n\
1415 +level 2 also prints the parsing of binaries to find the identificators."),
1417 + show_build_id_verbose,
1418 + &setlist, &showlist);
1420 + gdb::observers::executable_changed.attach (debug_print_executable_changed,
1423 diff --git a/gdb/build-id.h b/gdb/build-id.h
1424 --- a/gdb/build-id.h
1425 +++ b/gdb/build-id.h
1427 #include "gdb_bfd.h"
1428 #include "gdbsupport/rsp-low.h"
1430 -/* Locate NT_GNU_BUILD_ID from ABFD and return its content. */
1431 +/* Separate debuginfo files have corrupted PHDR but SHDR is correct there.
1432 + Locate NT_GNU_BUILD_ID from ABFD and return its content. */
1434 -extern const struct bfd_build_id *build_id_bfd_get (bfd *abfd);
1435 +extern const struct bfd_build_id *build_id_bfd_shdr_get (bfd *abfd);
1437 /* Return true if ABFD has NT_GNU_BUILD_ID matching the CHECK value.
1438 Otherwise, issue a warning and return false. */
1439 @@ -38,14 +39,19 @@ extern int build_id_verify (bfd *abfd,
1440 can be found, return NULL. */
1442 extern gdb_bfd_ref_ptr build_id_to_debug_bfd (size_t build_id_len,
1443 - const bfd_byte *build_id);
1444 + const bfd_byte *build_id,
1445 + char **link_return = NULL);
1447 +extern char *build_id_to_filename (const struct bfd_build_id *build_id,
1448 + char **link_return);
1450 /* Find and open a BFD for an executable file given a build-id. If no BFD
1451 can be found, return NULL. The returned reference to the BFD must be
1452 released by the caller. */
1454 extern gdb_bfd_ref_ptr build_id_to_exec_bfd (size_t build_id_len,
1455 - const bfd_byte *build_id);
1456 + const bfd_byte *build_id,
1457 + char **link_return);
1459 /* Find the separate debug file for OBJFILE, by using the build-id
1460 associated with OBJFILE's BFD. If successful, returns the file name for the
1461 @@ -58,7 +64,8 @@ extern gdb_bfd_ref_ptr build_id_to_exec_bfd (size_t build_id_len,
1464 extern std::string find_separate_debug_file_by_buildid
1465 - (struct objfile *objfile, deferred_warnings *warnings);
1466 + (struct objfile *objfile, deferred_warnings *warnings,
1467 + gdb::unique_xmalloc_ptr<char> *build_id_filename_return);
1469 /* Return an hex-string representation of BUILD_ID. */
1471 diff --git a/gdb/coffread.c b/gdb/coffread.c
1472 --- a/gdb/coffread.c
1473 +++ b/gdb/coffread.c
1474 @@ -729,7 +729,7 @@ coff_symfile_read (struct objfile *objfile, symfile_add_flags symfile_flags)
1476 deferred_warnings warnings;
1477 std::string debugfile
1478 - = find_separate_debug_file_by_buildid (objfile, &warnings);
1479 + = find_separate_debug_file_by_buildid (objfile, &warnings, NULL);
1481 if (debugfile.empty ())
1483 diff --git a/gdb/corelow.c b/gdb/corelow.c
1491 +#include "build-id.h"
1492 +#include "elf/common.h"
1493 +#include "gdbcmd.h"
1494 #include "inferior.h"
1497 @@ -380,6 +384,8 @@ add_to_thread_list (asection *asect, asection *reg_sect, inferior *inf)
1498 switch_to_thread (thr); /* Yes, make it current. */
1501 +static bool build_id_core_loads = true;
1503 /* Issue a message saying we have no core to debug, if FROM_TTY. */
1506 @@ -563,12 +569,14 @@ rename_vmcore_idle_reg_sections (bfd *abfd, inferior *inf)
1508 locate_exec_from_corefile_build_id (bfd *abfd, int from_tty)
1510 - const bfd_build_id *build_id = build_id_bfd_get (abfd);
1511 + const bfd_build_id *build_id = build_id_bfd_shdr_get (abfd);
1512 if (build_id == nullptr)
1515 + char *build_id_filename;
1516 gdb_bfd_ref_ptr execbfd
1517 - = build_id_to_exec_bfd (build_id->size, build_id->data);
1518 + = build_id_to_exec_bfd (build_id->size, build_id->data,
1519 + &build_id_filename);
1521 if (execbfd == nullptr)
1523 @@ -596,7 +604,12 @@ locate_exec_from_corefile_build_id (bfd *abfd, int from_tty)
1524 exec_file_attach (bfd_get_filename (execbfd.get ()), from_tty);
1525 symbol_file_add_main (bfd_get_filename (execbfd.get ()),
1526 symfile_add_flag (from_tty ? SYMFILE_VERBOSE : 0));
1527 + if (current_program_space->symfile_object_file != NULL)
1528 + current_program_space->symfile_object_file->flags |=
1529 + OBJF_BUILD_ID_CORE_LOADED;
1532 + debug_print_missing (BUILD_ID_MAIN_EXECUTABLE_FILENAME, build_id_filename);
1535 /* See gdbcore.h. */
1536 @@ -1506,4 +1519,11 @@ _initialize_corelow ()
1537 maintenance_print_core_file_backed_mappings,
1538 _("Print core file's file-backed mappings."),
1539 &maintenanceprintlist);
1541 + add_setshow_boolean_cmd ("build-id-core-loads", class_files,
1542 + &build_id_core_loads, _("\
1543 +Set whether CORE-FILE loads the build-id associated files automatically."), _("\
1544 +Show whether CORE-FILE loads the build-id associated files automatically."),
1546 + &setlist, &showlist);
1548 diff --git a/gdb/doc/gdb.texinfo b/gdb/doc/gdb.texinfo
1549 --- a/gdb/doc/gdb.texinfo
1550 +++ b/gdb/doc/gdb.texinfo
1551 @@ -22296,6 +22296,27 @@ information files.
1555 +You can also adjust the current verbosity of the @dfn{build id} locating.
1559 +@kindex set build-id-verbose
1560 +@item set build-id-verbose 0
1561 +No additional messages are printed.
1563 +@item set build-id-verbose 1
1564 +Missing separate debug filenames are printed.
1566 +@item set build-id-verbose 2
1567 +Missing separate debug filenames are printed and also all the parsing of the
1568 +binaries to find their @dfn{build id} content is printed.
1570 +@kindex show build-id-verbose
1571 +@item show build-id-verbose
1572 +Show the current verbosity value for the @dfn{build id} content locating.
1576 @cindex @code{.gnu_debuglink} sections
1577 @cindex debug link sections
1578 A debug link is a special section of the executable file named
1579 diff --git a/gdb/dwarf2/index-cache.c b/gdb/dwarf2/index-cache.c
1580 --- a/gdb/dwarf2/index-cache.c
1581 +++ b/gdb/dwarf2/index-cache.c
1582 @@ -96,7 +96,7 @@ index_cache_store_context::index_cache_store_context (const index_cache &ic,
1585 /* Get build id of objfile. */
1586 - const bfd_build_id *build_id = build_id_bfd_get (per_bfd->obfd);
1587 + const bfd_build_id *build_id = build_id_bfd_shdr_get (per_bfd->obfd);
1588 if (build_id == nullptr)
1590 index_cache_debug ("objfile %s has no build id",
1591 @@ -111,7 +111,8 @@ index_cache_store_context::index_cache_store_context (const index_cache &ic,
1595 - const bfd_build_id *dwz_build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
1596 + const bfd_build_id *dwz_build_id
1597 + = build_id_bfd_shdr_get (dwz->dwz_bfd.get ());
1599 if (dwz_build_id == nullptr)
1601 diff --git a/gdb/dwarf2/read.c b/gdb/dwarf2/read.c
1602 --- a/gdb/dwarf2/read.c
1603 +++ b/gdb/dwarf2/read.c
1604 @@ -3355,7 +3355,7 @@ get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
1605 static gdb::array_view<const gdb_byte>
1606 get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_bfd *dwarf2_per_bfd)
1608 - const bfd_build_id *build_id = build_id_bfd_get (obj->obfd.get ());
1609 + const bfd_build_id *build_id = build_id_bfd_shdr_get (obj->obfd.get ());
1610 if (build_id == nullptr)
1613 @@ -3368,7 +3368,7 @@ get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_bfd *dwarf2_per_bfd)
1614 static gdb::array_view<const gdb_byte>
1615 get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
1617 - const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
1618 + const bfd_build_id *build_id = build_id_bfd_shdr_get (dwz->dwz_bfd.get ());
1619 if (build_id == nullptr)
1622 diff --git a/gdb/elfread.c b/gdb/elfread.c
1625 @@ -1220,8 +1220,10 @@ elf_symfile_read_dwarf2 (struct objfile *objfile,
1627 deferred_warnings warnings;
1629 + gdb::unique_xmalloc_ptr<char> build_id_filename;
1630 std::string debugfile
1631 - = find_separate_debug_file_by_buildid (objfile, &warnings);
1632 + = find_separate_debug_file_by_buildid (objfile, &warnings,
1633 + &build_id_filename);
1635 if (debugfile.empty ())
1636 debugfile = find_separate_debug_file_by_debuglink (objfile, &warnings);
1637 @@ -1239,7 +1241,7 @@ elf_symfile_read_dwarf2 (struct objfile *objfile,
1640 const struct bfd_build_id *build_id
1641 - = build_id_bfd_get (objfile->obfd.get ());
1642 + = build_id_bfd_shdr_get (objfile->obfd.get ());
1643 const char *filename = bfd_get_filename (objfile->obfd.get ());
1645 if (build_id != nullptr)
1646 @@ -1265,6 +1267,11 @@ elf_symfile_read_dwarf2 (struct objfile *objfile,
1650 + /* Check if any separate debug info has been extracted out. */
1651 + else if (bfd_get_section_by_name (objfile->obfd.get (),
1654 + debug_print_missing (objfile_name (objfile), build_id_filename.get ());
1657 /* If all the methods to collect the debuginfo failed, print the
1658 diff --git a/gdb/exec.c b/gdb/exec.c
1661 @@ -237,7 +237,7 @@ validate_exec_file (int from_tty)
1662 current_exec_file = get_exec_file (0);
1664 const bfd_build_id *exec_file_build_id
1665 - = build_id_bfd_get (current_program_space->exec_bfd ());
1666 + = build_id_bfd_shdr_get (current_program_space->exec_bfd ());
1667 if (exec_file_build_id != nullptr)
1669 /* Prepend the target prefix, to force gdb_bfd_open to open the
1670 @@ -250,7 +250,7 @@ validate_exec_file (int from_tty)
1671 if (abfd != nullptr)
1673 const bfd_build_id *target_exec_file_build_id
1674 - = build_id_bfd_get (abfd.get ());
1675 + = build_id_bfd_shdr_get (abfd.get ());
1677 if (target_exec_file_build_id != nullptr)
1679 diff --git a/gdb/objfiles.h b/gdb/objfiles.h
1680 --- a/gdb/objfiles.h
1681 +++ b/gdb/objfiles.h
1682 @@ -884,6 +884,10 @@ struct objfile
1683 bool object_format_has_copy_relocs = false;
1686 +/* This file was loaded according to the BUILD_ID_CORE_LOADS rules. */
1688 +#define OBJF_BUILD_ID_CORE_LOADED static_cast<enum objfile_flag>(1 << 12)
1690 /* A deleter for objfile. */
1692 struct objfile_deleter
1693 diff --git a/gdb/python/py-objfile.c b/gdb/python/py-objfile.c
1694 --- a/gdb/python/py-objfile.c
1695 +++ b/gdb/python/py-objfile.c
1696 @@ -158,7 +158,7 @@ objfpy_get_build_id (PyObject *self, void *closure)
1700 - build_id = build_id_bfd_get (objfile->obfd.get ());
1701 + build_id = build_id_bfd_shdr_get (objfile->obfd.get ());
1703 catch (const gdb_exception &except)
1705 @@ -629,7 +629,7 @@ gdbpy_lookup_objfile (PyObject *self, PyObject *args, PyObject *kw)
1706 if (obfd == nullptr)
1709 - const bfd_build_id *obfd_build_id = build_id_bfd_get (obfd);
1710 + const bfd_build_id *obfd_build_id = build_id_bfd_shdr_get (obfd);
1711 if (obfd_build_id == nullptr)
1714 diff --git a/gdb/solib-svr4.c b/gdb/solib-svr4.c
1715 --- a/gdb/solib-svr4.c
1716 +++ b/gdb/solib-svr4.c
1719 #include "gdb_bfd.h"
1721 +#include "build-id.h"
1725 @@ -1318,9 +1319,51 @@ svr4_read_so_list (svr4_info *info, CORE_ADDR lm, CORE_ADDR prev_lm,
1729 - strncpy (newobj->so_name, buffer.get (), SO_NAME_MAX_PATH_SIZE - 1);
1730 - newobj->so_name[SO_NAME_MAX_PATH_SIZE - 1] = '\0';
1731 - strcpy (newobj->so_original_name, newobj->so_name);
1733 + struct bfd_build_id *build_id;
1735 + strncpy (newobj->so_original_name, buffer.get (), SO_NAME_MAX_PATH_SIZE - 1);
1736 + newobj->so_original_name[SO_NAME_MAX_PATH_SIZE - 1] = '\0';
1737 + /* May get overwritten below. */
1738 + strcpy (newobj->so_name, newobj->so_original_name);
1740 + build_id = build_id_addr_get (((lm_info_svr4 *) newobj->lm_info)->l_ld);
1741 + if (build_id != NULL)
1743 + char *name, *build_id_filename;
1745 + /* Missing the build-id matching separate debug info file
1746 + would be handled while SO_NAME gets loaded. */
1747 + name = build_id_to_filename (build_id, &build_id_filename);
1750 + strncpy (newobj->so_name, name, SO_NAME_MAX_PATH_SIZE - 1);
1751 + newobj->so_name[SO_NAME_MAX_PATH_SIZE - 1] = '\0';
1756 + debug_print_missing (newobj->so_name, build_id_filename);
1758 + /* In the case the main executable was found according to
1759 + its build-id (from a core file) prevent loading
1760 + a different build of a library with accidentally the
1763 + It suppresses bogus backtraces (and prints "??" there
1764 + instead) if the on-disk files no longer match the
1765 + running program version. */
1767 + if (current_program_space->symfile_object_file != NULL
1768 + && (current_program_space->symfile_object_file->flags
1769 + & OBJF_BUILD_ID_CORE_LOADED) != 0)
1770 + newobj->so_name[0] = 0;
1773 + xfree (build_id_filename);
1778 /* If this entry has no name, or its name matches the name
1779 for the main executable, don't include it in the list. */
1780 diff --git a/gdb/source.c b/gdb/source.c
1783 @@ -1167,7 +1167,7 @@ open_source_file (struct symtab *s)
1786 const struct bfd_build_id *build_id
1787 - = build_id_bfd_get (ofp->obfd.get ());
1788 + = build_id_bfd_shdr_get (ofp->obfd.get ());
1790 /* Query debuginfod for the source file. */
1791 if (build_id != nullptr && !srcpath.empty ())
1792 diff --git a/gdb/symfile.h b/gdb/symfile.h
1795 @@ -357,12 +357,18 @@ bool expand_symtabs_matching
1796 void map_symbol_filenames (gdb::function_view<symbol_filename_ftype> fun,
1797 bool need_fullname);
1800 /* Target-agnostic function to load the sections of an executable into memory.
1802 ARGS should be in the form "EXECUTABLE [OFFSET]", where OFFSET is an
1803 optional offset to apply to each section. */
1804 extern void generic_load (const char *args, int from_tty);
1806 +/* build-id support. */
1807 +extern struct bfd_build_id *build_id_addr_get (CORE_ADDR addr);
1808 +extern void debug_print_missing (const char *binary, const char *debug);
1809 +#define BUILD_ID_MAIN_EXECUTABLE_FILENAME _("the main executable file")
1811 /* From minidebug.c. */
1813 extern gdb_bfd_ref_ptr find_separate_debug_file_in_section (struct objfile *);
1814 diff --git a/gdb/testsuite/gdb.base/corefile.exp b/gdb/testsuite/gdb.base/corefile.exp
1815 --- a/gdb/testsuite/gdb.base/corefile.exp
1816 +++ b/gdb/testsuite/gdb.base/corefile.exp
1817 @@ -347,3 +347,33 @@ gdb_test_multiple "core-file $corefile" $test {
1823 +# Test auto-loading of binary files through build-id from the core file.
1824 +set buildid [build_id_debug_filename_get $binfile]
1825 +set wholetest "binfile found by build-id"
1826 +if {$buildid == ""} {
1827 + untested "$wholetest (binary has no build-id)"
1832 + regsub {\.debug$} $buildid {} buildid
1833 + set debugdir [standard_output_file ${testfile}-debugdir]
1834 + file delete -force -- $debugdir
1835 + file mkdir $debugdir/[file dirname $buildid]
1836 + file copy $binfile $debugdir/$buildid
1838 + set test "show debug-file-directory"
1839 + gdb_test_multiple $test $test {
1840 + -re "The directory where separate debug symbols are searched for is \"(.*)\"\\.\r\n$gdb_prompt $" {
1841 + set debugdir_orig $expect_out(1,string)
1845 + gdb_test_no_output "set debug-file-directory $debugdir:$debugdir_orig" "set debug-file-directory"
1846 + gdb_test "show build-id-core-loads" {Whether CORE-FILE loads the build-id associated files automatically is on\.}
1847 + gdb_test "core-file $corefile" "\r\nProgram terminated with .*" "core-file without executable"
1848 + gdb_test "info files" "Local exec file:\r\n\[ \t\]*`[string_to_regexp $debugdir/$buildid]', file type .*"
1851 diff --git a/gdb/testsuite/gdb.base/gdbinit-history.exp b/gdb/testsuite/gdb.base/gdbinit-history.exp
1852 --- a/gdb/testsuite/gdb.base/gdbinit-history.exp
1853 +++ b/gdb/testsuite/gdb.base/gdbinit-history.exp
1854 @@ -179,7 +179,8 @@ proc test_empty_history_filename { } {
1858 - set common_history [list "set height 0" "set width 0"]
1859 + set common_history [list "set height 0" "set width 0" \
1860 + "set build-id-verbose 0"]
1862 set test_dir [standard_output_file history_test]
1863 remote_exec host "mkdir -p $test_dir"
1864 diff --git a/gdb/testsuite/gdb.base/new-ui-pending-input.exp b/gdb/testsuite/gdb.base/new-ui-pending-input.exp
1865 --- a/gdb/testsuite/gdb.base/new-ui-pending-input.exp
1866 +++ b/gdb/testsuite/gdb.base/new-ui-pending-input.exp
1867 @@ -62,6 +62,7 @@ proc test_command_line_new_ui_pending_input {} {
1869 append options " -iex \"set height 0\""
1870 append options " -iex \"set width 0\""
1871 + append options " -iex \"set build-id-verbose 0\""
1872 append options " -iex \"new-ui console $extra_tty_name\""
1873 append options " -ex \"b $bpline\""
1874 append options " -ex \"run\""
1875 diff --git a/gdb/testsuite/lib/gdb.exp b/gdb/testsuite/lib/gdb.exp
1876 --- a/gdb/testsuite/lib/gdb.exp
1877 +++ b/gdb/testsuite/lib/gdb.exp
1878 @@ -226,7 +226,8 @@ if ![info exists INTERNAL_GDBFLAGS] {
1881 {-iex "set height 0"} \
1882 - {-iex "set width 0"}]]
1883 + {-iex "set width 0"} \
1884 + {-iex "set build-id-verbose 0"}]]
1886 # If DEBUGINFOD_URLS is set, gdb will try to download sources and
1887 # debug info for f.i. system libraries. Prevent this.
1888 @@ -2434,6 +2435,17 @@ proc default_gdb_start { } {
1892 + # Turn off the missing warnings as the testsuite does not expect it.
1893 + send_gdb "set build-id-verbose 0\n"
1895 + -re "$gdb_prompt $" {
1896 + verbose "Disabled the missing debug infos warnings." 2
1899 + warning "Could not disable the missing debug infos warnings.."
1906 diff --git a/gdb/testsuite/lib/mi-support.exp b/gdb/testsuite/lib/mi-support.exp
1907 --- a/gdb/testsuite/lib/mi-support.exp
1908 +++ b/gdb/testsuite/lib/mi-support.exp
1909 @@ -321,6 +321,16 @@ proc default_mi_gdb_start { { flags {} } } {
1910 warning "Couldn't set the width to 0."
1913 + # Turn off the missing warnings as the testsuite does not expect it.
1914 + send_gdb "190-gdb-set build-id-verbose 0\n"
1916 + -re ".*190-gdb-set build-id-verbose 0\r\n190\\\^done\r\n$mi_gdb_prompt$" {
1917 + verbose "Disabled the missing debug infos warnings." 2
1920 + warning "Could not disable the missing debug infos warnings.."
1924 if { $separate_inferior_pty } {
1925 mi_create_inferior_pty