]> git.pld-linux.org Git - packages/gdb.git/blob - gdb-6.6-buildid-locate.patch
- updated to 14.1 + rebased Fedora buildid patches set
[packages/gdb.git] / gdb-6.6-buildid-locate.patch
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
5
6 ;; New locating of the matching binaries from the pure core file (build-id).
7 ;;=push+jan
8
9 diff --git a/bfd/libbfd-in.h b/bfd/libbfd-in.h
10 --- a/bfd/libbfd-in.h
11 +++ b/bfd/libbfd-in.h
12 @@ -110,7 +110,7 @@ static inline char *
13  bfd_strdup (const char *str)
14  {
15    size_t len = strlen (str) + 1;
16 -  char *buf = bfd_malloc (len);
17 +  char *buf = (char *) bfd_malloc (len);
18    if (buf != NULL)
19      memcpy (buf, str, len);
20    return buf;
21 diff --git a/bfd/libbfd.h b/bfd/libbfd.h
22 --- a/bfd/libbfd.h
23 +++ b/bfd/libbfd.h
24 @@ -116,7 +116,7 @@ static inline char *
25  bfd_strdup (const char *str)
26  {
27    size_t len = strlen (str) + 1;
28 -  char *buf = bfd_malloc (len);
29 +  char *buf = (char *) bfd_malloc (len);
30    if (buf != NULL)
31      memcpy (buf, str, len);
32    return buf;
33 diff --git a/gdb/build-id.c b/gdb/build-id.c
34 --- a/gdb/build-id.c
35 +++ b/gdb/build-id.c
36 @@ -24,14 +24,72 @@
37  #include "gdbsupport/gdb_vecs.h"
38  #include "symfile.h"
39  #include "objfiles.h"
40 +#include <sys/stat.h>
41 +#include "elf-bfd.h"
42 +#include "elf/common.h"
43 +#include "elf/external.h"
44 +#include "elf/internal.h"
45  #include "filenames.h"
46 +#include "gdb_bfd.h"
47 +#include "gdbcmd.h"
48  #include "gdbcore.h"
49  #include "cli/cli-style.h"
50 +#include "inferior.h"
51 +#include "objfiles.h"
52 +#include "observable.h"
53 +#include "symfile.h"
54 +
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;
59 +static void
60 +show_build_id_verbose (struct ui_file *file, int from_tty,
61 +                      struct cmd_list_element *c, const char *value)
62 +{
63 +  gdb_printf (file, _("Verbosity level of the build-id locator is %s.\n"),
64 +             value);
65 +}
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.  */
68 +
69 +static struct bfd_build_id *
70 +build_id_buf_get (bfd *templ, gdb_byte *buf, bfd_size_type size)
71 +{
72 +  bfd_byte *p;
73 +
74 +  p = buf;
75 +  while (p < buf + size)
76 +    {
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);
82 +
83 +      if (H_GET_32 (templ, xnp->type) == NT_GNU_BUILD_ID
84 +         && namesz == sizeof "GNU"
85 +         && memcmp (xnp->name, "GNU", sizeof "GNU") == 0)
86 +       {
87 +         size_t sz = descsz;
88 +         gdb_byte *data = (gdb_byte *) descdata;
89 +         struct bfd_build_id *retval;
90 +
91 +         retval = (struct bfd_build_id *) xmalloc (sizeof *retval - 1 + sz);
92 +         retval->size = sz;
93 +         memcpy (retval->data, data, sz);
94 +
95 +         return retval;
96 +       }
97 +      p = descdata + BFD_ALIGN (descsz, 4);
98 +    }
99 +  return NULL;
100 +}
101  
102  /* See build-id.h.  */
103  
104  const struct bfd_build_id *
105 -build_id_bfd_get (bfd *abfd)
106 +build_id_bfd_shdr_get (bfd *abfd)
107  {
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)
111    return NULL;
112  }
113  
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.  */
117 +
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)
121 +{
122 +  int i;
123 +  struct bfd_build_id *retval = NULL;
124 +
125 +  for (i = 0; i < e_phnum; i++)
126 +    if (i_phdr[i].p_type == PT_NOTE && i_phdr[i].p_filesz > 0)
127 +      {
128 +       Elf_Internal_Phdr *hdr = &i_phdr[i];
129 +       gdb_byte *buf;
130 +       int err;
131 +
132 +       buf = (gdb_byte *) xmalloc (hdr->p_filesz);
133 +       err = target_read_memory (loadbase + i_phdr[i].p_vaddr, buf,
134 +                                 hdr->p_filesz);
135 +       if (err == 0)
136 +         retval = build_id_buf_get (templ, buf, hdr->p_filesz);
137 +       else
138 +         retval = NULL;
139 +       xfree (buf);
140 +       if (retval != NULL)
141 +         break;
142 +      }
143 +  return retval;
144 +}
145 +
146 +/* First we validate the file by reading in the ELF header and checking
147 +   the magic number.  */
148 +
149 +static inline bfd_boolean
150 +elf_file_p (Elf64_External_Ehdr *x_ehdrp64)
151 +{
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));
157 +
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));
162 +}
163 +
164 +/* Translate an ELF file header in external format into an ELF file header in
165 +   internal format.  */
166 +
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)))
171 +
172 +static void
173 +elf_swap_ehdr_in (bfd *abfd,
174 +                 const Elf64_External_Ehdr *src64,
175 +                 Elf_Internal_Ehdr *dst)
176 +{
177 +  int is64 = bfd_get_arch_size (abfd) == 64;
178 +#define SRC(field) (is64 ? src64->field \
179 +                        : ((const Elf32_External_Ehdr *) src64)->field)
180 +
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));
186 +  if (signed_vma)
187 +    dst->e_entry = H_GET_SIGNED_WORD (abfd, SRC (e_entry));
188 +  else
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));
199 +
200 +#undef SRC
201 +}
202 +
203 +/* Translate an ELF program header table entry in external format into an
204 +   ELF program header table entry in internal format.  */
205 +
206 +static void
207 +elf_swap_phdr_in (bfd *abfd,
208 +                 const Elf64_External_Phdr *src64,
209 +                 Elf_Internal_Phdr *dst)
210 +{
211 +  int is64 = bfd_get_arch_size (abfd) == 64;
212 +#define SRC(field) (is64 ? src64->field                                        \
213 +                        : ((const Elf32_External_Phdr *) src64)->field)
214 +
215 +  int signed_vma = get_elf_backend_data (abfd)->sign_extend_vma;
216 +
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));
220 +  if (signed_vma)
221 +    {
222 +      dst->p_vaddr = H_GET_SIGNED_WORD (abfd, SRC (p_vaddr));
223 +      dst->p_paddr = H_GET_SIGNED_WORD (abfd, SRC (p_paddr));
224 +    }
225 +  else
226 +    {
227 +      dst->p_vaddr = H_GET_WORD (abfd, SRC (p_vaddr));
228 +      dst->p_paddr = H_GET_WORD (abfd, SRC (p_paddr));
229 +    }
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));
233 +
234 +#undef SRC
235 +}
236 +
237 +#undef H_GET_SIGNED_WORD
238 +#undef H_GET_WORD
239 +
240 +static Elf_Internal_Phdr *
241 +elf_get_phdr (bfd *templ, bfd_vma ehdr_vma, unsigned *e_phnum_pointer,
242 +              bfd_vma *loadbase_pointer)
243 +{
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;
250 +  int err;
251 +  unsigned int i;
252 +  bfd_vma loadbase;
253 +  int loadbase_set;
254 +
255 +  gdb_assert (templ != NULL);
256 +  gdb_assert (sizeof (Elf64_External_Ehdr) >= sizeof (Elf32_External_Ehdr));
257 +
258 +  /* Read in the ELF header in external format.  */
259 +  err = target_read_memory (ehdr_vma, (bfd_byte *) &x_ehdr64, sizeof x_ehdr64);
260 +  if (err)
261 +    {
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);
265 +      return NULL;
266 +    }
267 +
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.  */
271 +
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)))
278 +    {
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);
282 +      return NULL;
283 +    }
284 +
285 +  /* Check that file's byte order matches xvec's */
286 +  switch (x_ehdr64.e_ident[EI_DATA])
287 +    {
288 +    case ELFDATA2MSB:          /* Big-endian */
289 +      if (! bfd_header_big_endian (templ))
290 +       {
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);
295 +         return NULL;
296 +       }
297 +      break;
298 +    case ELFDATA2LSB:          /* Little-endian */
299 +      if (! bfd_header_little_endian (templ))
300 +       {
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);
305 +         return NULL;
306 +       }
307 +      break;
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);
314 +      return NULL;
315 +    }
316 +
317 +  elf_swap_ehdr_in (templ, &x_ehdr64, &i_ehdr);
318 +
319 +  /* The file header tells where to find the program headers.
320 +     These are what we use to actually choose what to read.  */
321 +
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)
326 +    {
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);
330 +      return NULL;
331 +    }
332 +
333 +  x_phdrs_size = (bfd_get_arch_size (templ) == 64 ? sizeof (Elf64_External_Phdr)
334 +                                               : sizeof (Elf32_External_Phdr));
335 +
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);
340 +  if (err)
341 +    {
342 +      free (i_phdrs);
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));
347 +      return NULL;
348 +    }
349 +
350 +  loadbase = ehdr_vma;
351 +  loadbase_set = 0;
352 +  for (i = 0; i < i_ehdr.e_phnum; ++i)
353 +    {
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))
360 +       {
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)
366 +           {
367 +             loadbase = ehdr_vma - i_phdrs[i].p_vaddr;
368 +             loadbase_set = 1;
369 +           }
370 +       }
371 +    }
372 +
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);
376 +
377 +  *e_phnum_pointer = i_ehdr.e_phnum;
378 +  *loadbase_pointer = loadbase;
379 +  return i_phdrs;
380 +}
381 +
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.  */
388 +
389 +static CORE_ADDR build_id_addr;
390 +struct build_id_addr_sect
391 +  {
392 +    struct build_id_addr_sect *next;
393 +    asection *sect;
394 +  };
395 +static struct build_id_addr_sect *build_id_addr_sect;
396 +
397 +static void build_id_addr_candidate (bfd *abfd, asection *sect, void *obj)
398 +{
399 +  if (build_id_addr >= bfd_section_vma (sect))
400 +    {
401 +      struct build_id_addr_sect *candidate;
402 +
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;
407 +    }
408 +}
409 +
410 +struct bfd_build_id *
411 +build_id_addr_get (CORE_ADDR addr)
412 +{
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;
418 +
419 +  if (core_bfd == NULL)
420 +    return NULL;
421 +
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);
425 +
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.  */
429 +
430 +  for (candidate = build_id_addr_sect; candidate != NULL;
431 +       candidate = candidate->next)
432 +    {
433 +      i_phdr = elf_get_phdr (core_bfd,
434 +                            bfd_section_vma (candidate->sect),
435 +                            &e_phnum, &loadbase);
436 +      if (i_phdr != NULL)
437 +       break;
438 +    }
439 +
440 +  if (i_phdr != NULL)
441 +    {
442 +      retval = build_id_phdr_get (core_bfd, loadbase, e_phnum, i_phdr);
443 +      xfree (i_phdr);
444 +    }
445 +
446 +  while (build_id_addr_sect != NULL)
447 +    {
448 +      candidate = build_id_addr_sect;
449 +      build_id_addr_sect = candidate->next;
450 +      xfree (candidate);
451 +    }
452 +
453 +  return retval;
454 +}
455 +
456  /* See build-id.h.  */
457  
458  int
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;
461    int retval = 0;
462  
463 -  found = build_id_bfd_get (abfd);
464 +  found = build_id_bfd_shdr_get (abfd);
465  
466    if (found == NULL)
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)
469    return retval;
470  }
471  
472 +static char *
473 +link_resolve (const char *symlink, int level)
474 +{
475 +  char buf[PATH_MAX + 1], *retval;
476 +  gdb::unique_xmalloc_ptr<char> target;
477 +  ssize_t got;
478 +
479 +  if (level > 10)
480 +    return xstrdup (symlink);
481 +
482 +  got = readlink (symlink, buf, sizeof (buf));
483 +  if (got < 0 || got >= sizeof (buf))
484 +    return xstrdup (symlink);
485 +  buf[got] = '\0';
486 +
487 +  if (IS_ABSOLUTE_PATH (buf))
488 +    target = make_unique_xstrdup (buf);
489 +  else
490 +    {
491 +      const std::string dir (ldirname (symlink));
492 +
493 +      target = xstrprintf ("%s"
494 +#ifndef HAVE_DOS_BASED_FILE_SYSTEM
495 +                          "/"
496 +#else /* HAVE_DOS_BASED_FILE_SYSTEM */
497 +                          "\\"
498 +#endif /* HAVE_DOS_BASED_FILE_SYSTEM */
499 +                          "%s", dir.c_str(), buf);
500 +    }
501 +
502 +  retval = link_resolve (target.get (), level + 1);
503 +  return retval;
504 +}
505 +
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.  */
509  
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)
515  {
516 +  gdb_bfd_ref_ptr ret_bfd = {};
517 +  std::string ret_link;
518 +
519    if (separate_debug_file_debug)
520      {
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);
525      }
526  
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++)
534      {
535 -      filename_holder.reset (lrealpath (link.c_str ()));
536 -      filename = filename_holder.get ();
537 -    }
538 +      std::string link = orig_link;
539  
540 -  if (filename == NULL)
541 -    {
542 -      if (separate_debug_file_debug)
543 -       gdb_printf (gdb_stdlog,
544 -                   _(" no, unable to compute real path\n"));
545 +      if (seqno > 0)
546 +       {
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.  */
550  
551 -      return {};
552 -    }
553 +         string_appendf (link, ".%u", seqno);
554 +       }
555  
556 -  /* We expect to be silent on the non-existing files.  */
557 -  gdb_bfd_ref_ptr debug_bfd = gdb_bfd_open (filename, gnutarget);
558 +      ret_link = link;
559  
560 -  if (debug_bfd == NULL)
561 -    {
562 -      if (separate_debug_file_debug)
563 -       gdb_printf (gdb_stdlog, _(" no, unable to open.\n"));
564 +      struct stat statbuf_trash;
565 +
566 +      /* `access' automatically dereferences LINK.  */
567 +      if (lstat (link.c_str (), &statbuf_trash) != 0)
568 +       {
569 +         /* Stop increasing SEQNO.  */
570 +         break;
571 +       }
572 +
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)
579 +       {
580 +         filename_holder.reset (lrealpath (link.c_str ()));
581 +         filename = filename_holder.get ();
582 +       }
583 +
584 +      if (filename == NULL)
585 +       {
586 +         if (separate_debug_file_debug)
587 +           gdb_printf (gdb_stdlog,
588 +                              _(" no, unable to compute real path\n"));
589 +
590 +         continue;
591 +       }
592 +
593 +      /* We expect to be silent on the non-existing files.  */
594 +      gdb_bfd_ref_ptr debug_bfd = gdb_bfd_open (filename, gnutarget);
595 +
596 +      if (debug_bfd == NULL)
597 +       {
598 +         if (separate_debug_file_debug)
599 +           gdb_printf (gdb_stdlog, _(" no, unable to open.\n"));
600  
601 -      return {};
602 +         continue;
603 +       }
604 +
605 +      if (!build_id_verify (debug_bfd.get(), build_id_len, build_id))
606 +       {
607 +         if (separate_debug_file_debug)
608 +           gdb_printf (gdb_stdlog,
609 +                       _(" no, build-id does not match.\n"));
610 +
611 +         continue;
612 +       }
613 +
614 +      ret_bfd = debug_bfd;
615 +      break;
616      }
617  
618 -  if (!build_id_verify (debug_bfd.get(), build_id_len, build_id))
619 +  std::string link_all;
620 +
621 +  if (ret_bfd != NULL)
622      {
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"));
626 +    }
627 +  else
628 +    {
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;
632  
633 -      return {};
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));
638 +
639 +      if (link_all.empty ())
640 +       link_all = link0_resolved;
641 +      else
642 +       {
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;
646 +       }
647      }
648  
649 -  if (separate_debug_file_debug)
650 -    gdb_printf (gdb_stdlog, _(" yes!\n"));
651 +  if (link_return != NULL)
652 +    {
653 +      if (ret_bfd != NULL)
654 +       {
655 +         *link_return = xstrdup (ret_link.c_str ());
656 +       }
657 +      else
658 +       {
659 +         *link_return = xstrdup (link_all.c_str ());
660 +       }
661 +    }
662  
663 -  return debug_bfd;
664 +  return ret_bfd;
665  }
666  
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,
669  
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)
674  {
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,
678        if (size > 0)
679         {
680           size--;
681 -         string_appendf (link, "%02x/", (unsigned) *data++);
682 +         string_appendf (link, "%02x", (unsigned) *data++);
683         }
684 -
685 +      if (size > 0)
686 +       link += "/";
687        while (size-- > 0)
688         string_appendf (link, "%02x", (unsigned) *data++);
689  
690        link += suffix;
691  
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)
696         return debug_bfd;
697  
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 ())
700         {
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)
705             return debug_bfd;
706         }
707 @@ -190,31 +694,663 @@ build_id_to_bfd_suffix (size_t build_id_len, const bfd_byte *build_id,
708    return {};
709  }
710  
711 +char *
712 +build_id_to_filename (const struct bfd_build_id *build_id, char **link_return)
713 +{
714 +  gdb_bfd_ref_ptr abfd;
715 +  char *result;
716 +
717 +  abfd = build_id_to_exec_bfd (build_id->size, build_id->data, link_return);
718 +  if (abfd == NULL)
719 +    return NULL;
720 +
721 +  result = xstrdup (bfd_get_filename (abfd.get ()));
722 +  return result;
723 +}
724 +
725 +void debug_flush_missing (void);
726 +
727 +#ifdef HAVE_LIBRPM
728 +
729 +#include <rpm/rpmlib.h>
730 +#include <rpm/rpmts.h>
731 +#include <rpm/rpmdb.h>
732 +#include <rpm/header.h>
733 +#ifdef DLOPEN_LIBRPM
734 +#include <dlfcn.h>
735 +#endif
736 +
737 +/* Workarodun https://bugzilla.redhat.com/show_bug.cgi?id=643031
738 +   librpm must not exit() an application on SIGINT
739 +
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);
744 +int
745 +rpmsqEnable (int signum, /* rpmsqAction_t handler */ void *handler)
746 +{
747 +  return 0;
748 +}
749 +
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.  */
752 +
753 +static struct htab *missing_rpm_hash;
754 +
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).
758 +   */
759 +
760 +struct missing_rpm
761 +  {
762 +    struct missing_rpm *next;
763 +    char rpm[1];
764 +  };
765 +static struct missing_rpm *missing_rpm_list;
766 +static int missing_rpm_list_entries;
767 +
768 +/* Returns the count of newly added rpms.  */
769 +
770 +static int
771 +#ifndef GDB_INDEX_VERIFY_VENDOR
772 +missing_rpm_enlist (const char *filename)
773 +#else
774 +missing_rpm_enlist_1 (const char *filename, int verify_vendor)
775 +#endif
776 +{
777 +  static int rpm_init_done = 0;
778 +  rpmts ts;
779 +  rpmdbMatchIterator mi;
780 +  int count = 0;
781 +
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,
800 +                                                   rpmTag rpmtag,
801 +                                                   const void *keyp,
802 +                                                   size_t keylen);
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 */
812 +
813 +  gdb_assert (filename != NULL);
814 +
815 +  if (strcmp (filename, BUILD_ID_MAIN_EXECUTABLE_FILENAME) == 0)
816 +    return 0;
817 +
818 +  if (is_target_filename (filename))
819 +    return 0;
820 +
821 +  if (filename[0] != '/')
822 +    {
823 +      warning (_("Ignoring non-absolute filename: <%s>"), filename);
824 +      return 0;
825 +    }
826 +
827 +  if (!rpm_init_done)
828 +    {
829 +      static int init_tried;
830 +
831 +      /* Already failed the initialization before?  */
832 +      if (init_tried)
833 +        return 0;
834 +      init_tried = 1;
835 +
836 +#ifdef DLOPEN_LIBRPM
837 +      {
838 +       void *h;
839 +
840 +       h = dlopen (DLOPEN_LIBRPM, RTLD_LAZY);
841 +       if (!h)
842 +         {
843 +           warning (_("Unable to open \"%s\" (%s), "
844 +                     "missing debuginfos notifications will not be displayed"),
845 +                    DLOPEN_LIBRPM, dlerror ());
846 +           return 0;
847 +         }
848 +
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"))))
856 +         {
857 +           warning (_("Opened library \"%s\" is incompatible (%s), "
858 +                     "missing debuginfos notifications will not be displayed"),
859 +                    DLOPEN_LIBRPM, dlerror ());
860 +           if (dlclose (h))
861 +             warning (_("Error closing library \"%s\": %s\n"), DLOPEN_LIBRPM,
862 +                      dlerror ());
863 +           return 0;
864 +         }
865 +      }
866 +#endif /* DLOPEN_LIBRPM */
867 +
868 +      if (rpmReadConfigFiles_p (NULL, NULL) != 0)
869 +       {
870 +         warning (_("Error reading the rpm configuration files"));
871 +         return 0;
872 +       }
873 +
874 +      rpm_init_done = 1;
875 +    }
876 +
877 +  ts = rpmtsCreate_p ();
878 +
879 +  mi = rpmtsInitIterator_p (ts, RPMTAG_BASENAMES, filename, 0);
880 +  if (mi != NULL)
881 +    {
882 +#ifndef GDB_INDEX_VERIFY_VENDOR
883 +      for (;;)
884 +#else
885 +      if (!verify_vendor) for (;;)
886 +#endif
887 +       {
888 +         Header h;
889 +         char *debuginfo, **slot, *s, *s2;
890 +         errmsg_t err;
891 +         size_t srcrpmlen = sizeof (".src.rpm") - 1;
892 +         size_t debuginfolen = sizeof ("-debuginfo") - 1;
893 +         rpmdbMatchIterator mi_debuginfo;
894 +
895 +         h = rpmdbNextIterator_p (mi);
896 +         if (h == NULL)
897 +           break;
898 +
899 +         /* Verify the debuginfo file is not already installed.  */
900 +
901 +         debuginfo = headerFormat_p (h, "%{sourcerpm}-debuginfo.%{arch}",
902 +                                     &err);
903 +         if (!debuginfo)
904 +           {
905 +             warning (_("Error querying the rpm file `%s': %s"), filename,
906 +                      err);
907 +             continue;
908 +           }
909 +         /* s = `.src.rpm-debuginfo.%{arch}' */
910 +         s = strrchr (debuginfo, '-') - srcrpmlen;
911 +         s2 = NULL;
912 +         if (s > debuginfo && memcmp (s, ".src.rpm", srcrpmlen) == 0)
913 +           {
914 +             /* s2 = `-%{release}.src.rpm-debuginfo.%{arch}' */
915 +             s2 = (char *) memrchr (debuginfo, '-', s - debuginfo);
916 +           }
917 +         if (s2)
918 +           {
919 +             /* s2 = `-%{version}-%{release}.src.rpm-debuginfo.%{arch}' */
920 +             s2 = (char *) memrchr (debuginfo, '-', s2 - debuginfo);
921 +           }
922 +         if (!s2)
923 +           {
924 +             warning (_("Error querying the rpm file `%s': %s"), filename,
925 +                      debuginfo);
926 +             xfree (debuginfo);
927 +             continue;
928 +           }
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}' */
940 +
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);
944 +         xfree (debuginfo);
945 +         if (mi_debuginfo)
946 +           {
947 +             rpmdbFreeIterator_p (mi_debuginfo);
948 +             count = 0;
949 +             break;
950 +           }
951 +
952 +         /* The allocated memory gets utilized below for MISSING_RPM_HASH.  */
953 +         debuginfo = headerFormat_p (h,
954 +                                     "%{name}-%{version}-%{release}.%{arch}",
955 +                                     &err);
956 +         if (!debuginfo)
957 +           {
958 +             warning (_("Error querying the rpm file `%s': %s"), filename,
959 +                      err);
960 +             continue;
961 +           }
962 +
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.  */
975 +
976 +         if (missing_rpm_hash == NULL)
977 +           {
978 +             /* DEL_F is passed NULL as MISSING_RPM_LIST's HTAB_DELETE
979 +                should not deallocate the entries.  */
980 +
981 +             missing_rpm_hash = htab_create_alloc (64, htab_hash_string,
982 +                              (int (*) (const void *, const void *)) streq,
983 +                                                   NULL, xcalloc, xfree);
984 +           }
985 +         slot = (char **) htab_find_slot (missing_rpm_hash, debuginfo, INSERT);
986 +         /* XCALLOC never returns NULL.  */
987 +         gdb_assert (slot != NULL);
988 +         if (*slot == NULL)
989 +           {
990 +             struct missing_rpm *missing_rpm;
991 +
992 +             *slot = debuginfo;
993 +
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++;
999 +           }
1000 +         else
1001 +           xfree (debuginfo);
1002 +         count++;
1003 +       }
1004 +#ifdef GDB_INDEX_VERIFY_VENDOR
1005 +      else /* verify_vendor */
1006 +       {
1007 +         int vendor_pass = 0, vendor_fail = 0;
1008 +
1009 +         for (;;)
1010 +           {
1011 +             Header h;
1012 +             errmsg_t err;
1013 +             char *vendor;
1014 +
1015 +             h = rpmdbNextIterator_p (mi);
1016 +             if (h == NULL)
1017 +               break;
1018 +
1019 +             vendor = headerFormat_p (h, "%{vendor}", &err);
1020 +             if (!vendor)
1021 +               {
1022 +                 warning (_("Error querying the rpm file `%s': %s"), filename,
1023 +                          err);
1024 +                 continue;
1025 +               }
1026 +             if (strcmp (vendor, "Red Hat, Inc.") == 0)
1027 +               vendor_pass = 1;
1028 +             else
1029 +               vendor_fail = 1;
1030 +             xfree (vendor);
1031 +           }
1032 +         count = vendor_pass != 0 && vendor_fail == 0;
1033 +       }
1034 +#endif
1035 +
1036 +      rpmdbFreeIterator_p (mi);
1037 +    }
1038 +
1039 +  rpmtsFree_p (ts);
1040 +
1041 +  return count;
1042 +}
1043 +
1044 +#ifdef GDB_INDEX_VERIFY_VENDOR
1045 +missing_rpm_enlist (const char *filename)
1046 +{
1047 +  return missing_rpm_enlist_1 (filename, 0);
1048 +}
1049 +
1050 +extern int rpm_verify_vendor (const char *filename);
1051 +int
1052 +rpm_verify_vendor (const char *filename)
1053 +{
1054 +  return missing_rpm_enlist_1 (filename, 1);
1055 +}
1056 +#endif
1057 +
1058 +static bool
1059 +missing_rpm_list_compar (const char *ap, const char *bp)
1060 +{
1061 +  return strcoll (ap, bp) < 0;
1062 +}
1063 +
1064 +/* It returns a NULL-terminated array of strings needing to be FREEd.  It may
1065 +   also return only NULL.  */
1066 +
1067 +static void
1068 +missing_rpm_list_print (void)
1069 +{
1070 +  struct missing_rpm *list_iter;
1071 +
1072 +  if (missing_rpm_list_entries == 0)
1073 +    return;
1074 +
1075 +  std::vector<const char *> array (missing_rpm_list_entries);
1076 +  size_t idx = 0;
1077 +
1078 +  for (list_iter = missing_rpm_list; list_iter != NULL;
1079 +       list_iter = list_iter->next)
1080 +    {
1081 +      array[idx++] = list_iter->rpm;
1082 +    }
1083 +  gdb_assert (idx == missing_rpm_list_entries);
1084 +
1085 +  std::sort (array.begin (), array.end (), missing_rpm_list_compar);
1086 +
1087 +  /* We zero out the number of missing RPMs here because of a nasty
1088 +     bug (see RHBZ 1801974).
1089 +
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
1093 +     like this:
1094 +
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 ...
1098 +
1099 +     For this reason, we make sure MISSING_RPM_LIST_ENTRIES is zero
1100 +     *before* calling any print function.
1101 +     
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;
1109 +
1110 +  gdb_printf (_("Missing separate debuginfos, use: %s"),
1111 +#ifdef DNF_DEBUGINFO_INSTALL
1112 +                    "dnf "
1113 +#endif
1114 +                    "debuginfo-install");
1115 +  for (const char *el : array)
1116 +    {
1117 +      gdb_printf (" %s", el);
1118 +    }
1119 +  gdb_printf ("\n");
1120 +
1121 +  while (missing_rpm_list != NULL)
1122 +    {
1123 +      list_iter = missing_rpm_list;
1124 +      missing_rpm_list = list_iter->next;
1125 +      xfree (list_iter);
1126 +    }
1127 +}
1128 +
1129 +static void
1130 +missing_rpm_change (void)
1131 +{
1132 +  debug_flush_missing ();
1133 +
1134 +  gdb_assert (missing_rpm_list == NULL);
1135 +  if (missing_rpm_hash != NULL)
1136 +    {
1137 +      htab_delete (missing_rpm_hash);
1138 +      missing_rpm_hash = NULL;
1139 +    }
1140 +}
1141 +
1142 +enum missing_exec
1143 +  {
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
1152 +  };
1153 +static enum missing_exec missing_exec = MISSING_EXEC_NOT_TRIED;
1154 +
1155 +#endif /* HAVE_LIBRPM */
1156 +
1157 +void
1158 +debug_flush_missing (void)
1159 +{
1160 +#ifdef HAVE_LIBRPM
1161 +  missing_rpm_list_print ();
1162 +#endif
1163 +}
1164 +
1165 +/* This MISSING_FILEPAIR_HASH tracker is used only for the duplicite messages
1166 +     yum --enablerepo='*debug*' install ...
1167 +   avoidance.  */
1168 +
1169 +struct missing_filepair
1170 +  {
1171 +    char *binary;
1172 +    char *debug;
1173 +    char data[1];
1174 +  };
1175 +
1176 +static struct htab *missing_filepair_hash;
1177 +static struct obstack missing_filepair_obstack;
1178 +
1179 +static void *
1180 +missing_filepair_xcalloc (size_t nmemb, size_t nmemb_size)
1181 +{
1182 +  void *retval;
1183 +  size_t size = nmemb * nmemb_size;
1184 +
1185 +  retval = obstack_alloc (&missing_filepair_obstack, size);
1186 +  memset (retval, 0, size);
1187 +  return retval;
1188 +}
1189 +
1190 +static hashval_t
1191 +missing_filepair_hash_func (const struct missing_filepair *elem)
1192 +{
1193 +  hashval_t retval = 0;
1194 +
1195 +  retval ^= htab_hash_string (elem->binary);
1196 +  if (elem->debug != NULL)
1197 +    retval ^= htab_hash_string (elem->debug);
1198 +
1199 +  return retval;
1200 +}
1201 +
1202 +static int
1203 +missing_filepair_eq (const struct missing_filepair *elem1,
1204 +                      const struct missing_filepair *elem2)
1205 +{
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);
1209 +}
1210 +
1211 +static void
1212 +missing_filepair_change (void)
1213 +{
1214 +  if (missing_filepair_hash != NULL)
1215 +    {
1216 +      obstack_free (&missing_filepair_obstack, NULL);
1217 +      /* All their memory came just from missing_filepair_OBSTACK.  */
1218 +      missing_filepair_hash = NULL;
1219 +    }
1220 +#ifdef HAVE_LIBRPM
1221 +  missing_exec = MISSING_EXEC_NOT_TRIED;
1222 +#endif
1223 +}
1224 +
1225 +static void
1226 +debug_print_executable_changed (struct program_space *pspace, bool reload_p)
1227 +{
1228 +#ifdef HAVE_LIBRPM
1229 +  missing_rpm_change ();
1230 +#endif
1231 +  missing_filepair_change ();
1232 +}
1233 +
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
1238 +   */
1239 +
1240 +void
1241 +debug_print_missing (const char *binary, const char *debug)
1242 +{
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;
1248 +
1249 +  if (build_id_verbose < BUILD_ID_VERBOSE_FILENAMES)
1250 +    return;
1251 +
1252 +  if (missing_filepair_hash == NULL)
1253 +    {
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);
1259 +    }
1260 +
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).  */
1267 +
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,
1272 +                                                     INSERT);
1273 +
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.  */
1278 +
1279 +  if (*slot != NULL)
1280 +    return;
1281 +
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)
1288 +    {
1289 +      missing_filepair->debug = missing_filepair->binary + binary_len0;
1290 +      memcpy (missing_filepair->debug, debug, debug_len0);
1291 +    }
1292 +  else
1293 +    missing_filepair->debug = NULL;
1294 +
1295 +  *slot = missing_filepair;
1296 +
1297 +#ifdef HAVE_LIBRPM
1298 +  if (missing_exec == MISSING_EXEC_NOT_TRIED)
1299 +    {
1300 +      const char *execfilename = get_exec_file (0);
1301 +
1302 +      if (execfilename != NULL)
1303 +       {
1304 +         if (missing_rpm_enlist (execfilename) == 0)
1305 +           missing_exec = MISSING_EXEC_NOT_FOUND;
1306 +         else
1307 +           missing_exec = MISSING_EXEC_ENLISTED;
1308 +       }
1309 +    }
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 */
1314 +      {
1315 +       /* We do not collect and flush these messages as each such message
1316 +          already requires its own separate lines.  */
1317 +
1318 +       gdb_printf (gdb_stdlog,
1319 +                   _("Missing separate debuginfo for %s.\n"), binary);
1320 +       if (debug != NULL)
1321 +       {
1322 +         if (access (debug, F_OK) == 0) {
1323 +           gdb_printf (gdb_stdlog, _("Try: %s %s\n"),
1324 +#ifdef DNF_DEBUGINFO_INSTALL
1325 +                       "dnf"
1326 +#else
1327 +                       "yum"
1328 +#endif
1329 +                       " --enablerepo='*debug*' install", debug);
1330 +         } else
1331 +           gdb_printf (gdb_stdlog, _("The debuginfo package for this file is probably broken.\n"));
1332 +       }
1333 +      }
1334 +}
1335 +
1336  /* See build-id.h.  */
1337  
1338  gdb_bfd_ref_ptr
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)
1342  {
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",
1345 +                                link_return);
1346  }
1347  
1348  /* See build-id.h.  */
1349  
1350  gdb_bfd_ref_ptr
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)
1354  {
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);
1357  }
1358  
1359  /* See build-id.h.  */
1360  
1361  std::string
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)
1366  {
1367    const struct bfd_build_id *build_id;
1368  
1369 -  build_id = build_id_bfd_get (objfile->obfd.get ());
1370 +  if (build_id_filename_return)
1371 +    *build_id_filename_return = NULL;
1372 +
1373 +  build_id = build_id_bfd_shdr_get (objfile->obfd.get ());
1374    if (build_id != NULL)
1375      {
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));
1380  
1381 +      char *build_id_filename_cstr = NULL;
1382        gdb_bfd_ref_ptr abfd (build_id_to_debug_bfd (build_id->size,
1383 -                                                  build_id->data));
1384 +                                                  build_id->data,
1385 +             (!build_id_filename_return ? NULL : &build_id_filename_cstr)));
1386 +      if (build_id_filename_return)
1387 +       {
1388 +         if (!build_id_filename_cstr)
1389 +           gdb_assert (!*build_id_filename_return);
1390 +         else
1391 +           {
1392 +             *build_id_filename_return = gdb::unique_xmalloc_ptr<char> (build_id_filename_cstr);
1393 +             build_id_filename_cstr = NULL;
1394 +           }
1395 +       }
1396 +
1397        /* Prevent looping on a stripped .debug file.  */
1398        if (abfd != NULL
1399           && filename_cmp (bfd_get_filename (abfd.get ()),
1400 @@ -243,3 +1392,22 @@ find_separate_debug_file_by_buildid (struct objfile *objfile,
1401  
1402    return std::string ();
1403  }
1404 +
1405 +void _initialize_build_id ();
1406 +
1407 +void
1408 +_initialize_build_id ()
1409 +{
1410 +  add_setshow_zinteger_cmd ("build-id-verbose", no_class, &build_id_verbose,
1411 +                           _("\
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."),
1416 +                           NULL,
1417 +                           show_build_id_verbose,
1418 +                           &setlist, &showlist);
1419 +
1420 +  gdb::observers::executable_changed.attach (debug_print_executable_changed,
1421 +                                             "build-id");
1422 +}
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
1426 @@ -23,9 +23,10 @@
1427  #include "gdb_bfd.h"
1428  #include "gdbsupport/rsp-low.h"
1429  
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.  */
1433  
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);
1436  
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.  */
1441  
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);
1446 +
1447 +extern char *build_id_to_filename (const struct bfd_build_id *build_id,
1448 +                                  char **link_return);
1449  
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.  */
1453  
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);
1458  
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,
1462     will be printed.  */
1463  
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);
1468  
1469  /* Return an hex-string representation of BUILD_ID.  */
1470  
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)
1475      {
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);
1480  
1481        if (debugfile.empty ())
1482         debugfile
1483 diff --git a/gdb/corelow.c b/gdb/corelow.c
1484 --- a/gdb/corelow.c
1485 +++ b/gdb/corelow.c
1486 @@ -22,6 +22,10 @@
1487  #include <signal.h>
1488  #include <fcntl.h>
1489  #include "frame.h"
1490 +#include "auxv.h"
1491 +#include "build-id.h"
1492 +#include "elf/common.h"
1493 +#include "gdbcmd.h"
1494  #include "inferior.h"
1495  #include "infrun.h"
1496  #include "symtab.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.  */
1499  }
1500  
1501 +static bool build_id_core_loads = true;
1502 +
1503  /* Issue a message saying we have no core to debug, if FROM_TTY.  */
1504  
1505  static void
1506 @@ -563,12 +569,14 @@ rename_vmcore_idle_reg_sections (bfd *abfd, inferior *inf)
1507  static void
1508  locate_exec_from_corefile_build_id (bfd *abfd, int from_tty)
1509  {
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)
1513      return;
1514  
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);
1520  
1521    if (execbfd == nullptr)
1522      {
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;
1530      }
1531 +  else
1532 +    debug_print_missing (BUILD_ID_MAIN_EXECUTABLE_FILENAME, build_id_filename);
1533  }
1534  
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);
1540 +
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."),
1545 +                          NULL, NULL, NULL,
1546 +                          &setlist, &showlist);
1547  }
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.
1552  
1553  @end table
1554  
1555 +You can also adjust the current verbosity of the @dfn{build id} locating.
1556 +
1557 +@table @code
1558 +
1559 +@kindex set build-id-verbose
1560 +@item set build-id-verbose 0
1561 +No additional messages are printed.
1562 +
1563 +@item set build-id-verbose 1
1564 +Missing separate debug filenames are printed.
1565 +
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.
1569 +
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.
1573 +
1574 +@end table
1575 +
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,
1583      return;
1584  
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)
1589      {
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,
1592  
1593    if (dwz != nullptr)
1594      {
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 ());
1598  
1599        if (dwz_build_id == nullptr)
1600         {
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)
1607  {
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)
1611      return {};
1612  
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)
1616  {
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)
1620      return {};
1621  
1622 diff --git a/gdb/elfread.c b/gdb/elfread.c
1623 --- a/gdb/elfread.c
1624 +++ b/gdb/elfread.c
1625 @@ -1220,8 +1220,10 @@ elf_symfile_read_dwarf2 (struct objfile *objfile,
1626      {
1627        deferred_warnings warnings;
1628  
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);
1634  
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,
1638         {
1639           has_dwarf2 = false;
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 ());
1644  
1645           if (build_id != nullptr)
1646 @@ -1265,6 +1267,11 @@ elf_symfile_read_dwarf2 (struct objfile *objfile,
1647                       has_dwarf2 = true;
1648                     }
1649                 }
1650 +               /* Check if any separate debug info has been extracted out.  */
1651 +               else if (bfd_get_section_by_name (objfile->obfd.get (),
1652 +                                                 ".gnu_debuglink")
1653 +                        != NULL)
1654 +                 debug_print_missing (objfile_name (objfile), build_id_filename.get ());
1655             }
1656         }
1657        /* If all the methods to collect the debuginfo failed, print the
1658 diff --git a/gdb/exec.c b/gdb/exec.c
1659 --- a/gdb/exec.c
1660 +++ b/gdb/exec.c
1661 @@ -237,7 +237,7 @@ validate_exec_file (int from_tty)
1662    current_exec_file = get_exec_file (0);
1663  
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)
1668      {
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)
1672         {
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 ());
1676  
1677           if (target_exec_file_build_id != nullptr)
1678             {
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;
1684  };
1685  
1686 +/* This file was loaded according to the BUILD_ID_CORE_LOADS rules.  */
1687 +
1688 +#define OBJF_BUILD_ID_CORE_LOADED static_cast<enum objfile_flag>(1 << 12)
1689 +
1690  /* A deleter for objfile.  */
1691  
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)
1697  
1698    try
1699      {
1700 -      build_id = build_id_bfd_get (objfile->obfd.get ());
1701 +      build_id = build_id_bfd_shdr_get (objfile->obfd.get ());
1702      }
1703    catch (const gdb_exception &except)
1704      {
1705 @@ -629,7 +629,7 @@ gdbpy_lookup_objfile (PyObject *self, PyObject *args, PyObject *kw)
1706            if (obfd == nullptr)
1707              return 0;
1708  
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)
1712              return 0;
1713  
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
1717 @@ -44,6 +44,7 @@
1718  #include "auxv.h"
1719  #include "gdb_bfd.h"
1720  #include "probe.h"
1721 +#include "build-id.h"
1722  
1723  #include <map>
1724  
1725 @@ -1318,9 +1319,51 @@ svr4_read_so_list (svr4_info *info, CORE_ADDR lm, CORE_ADDR prev_lm,
1726           continue;
1727         }
1728  
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);
1732 +      {
1733 +       struct bfd_build_id *build_id;
1734 +
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);
1739 +
1740 +       build_id = build_id_addr_get (((lm_info_svr4 *) newobj->lm_info)->l_ld);
1741 +       if (build_id != NULL)
1742 +         {
1743 +           char *name, *build_id_filename;
1744 +
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);
1748 +           if (name != NULL)
1749 +             {
1750 +               strncpy (newobj->so_name, name, SO_NAME_MAX_PATH_SIZE - 1);
1751 +               newobj->so_name[SO_NAME_MAX_PATH_SIZE - 1] = '\0';
1752 +               xfree (name);
1753 +             }
1754 +           else
1755 +             {
1756 +               debug_print_missing (newobj->so_name, build_id_filename);
1757 +
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
1761 +                  same SO_NAME.
1762 +
1763 +                  It suppresses bogus backtraces (and prints "??" there
1764 +                  instead) if the on-disk files no longer match the
1765 +                  running program version.  */
1766 +
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;
1771 +             }
1772 +
1773 +           xfree (build_id_filename);
1774 +           xfree (build_id);
1775 +         }
1776 +      }
1777  
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
1781 --- a/gdb/source.c
1782 +++ b/gdb/source.c
1783 @@ -1167,7 +1167,7 @@ open_source_file (struct symtab *s)
1784             }
1785  
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 ());
1789  
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
1793 --- a/gdb/symfile.h
1794 +++ 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);
1798  
1799 +
1800  /* Target-agnostic function to load the sections of an executable into memory.
1801  
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);
1805  
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")
1810 +
1811  /* From minidebug.c.  */
1812  
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 {
1818         pass $test
1819      }
1820  }
1821 +
1822 +
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)"
1828 +} else {
1829 +    gdb_exit
1830 +    gdb_start
1831 +
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
1837 +
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)
1842 +           pass $test
1843 +       }
1844 +    }
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 .*"
1849 +    pass $wholetest
1850 +}
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 { } {
1855      global env
1856      global gdb_prompt
1857  
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"]
1861  
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 {} {
1868      set options ""
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] {
1879                    "-nx" \
1880                    "-q" \
1881                    {-iex "set height 0"} \
1882 -                  {-iex "set width 0"}]]
1883 +                  {-iex "set width 0"} \
1884 +                  {-iex "set build-id-verbose 0"}]]
1885  
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 { } {
1889         }
1890      }
1891  
1892 +    # Turn off the missing warnings as the testsuite does not expect it.
1893 +    send_gdb "set build-id-verbose 0\n"
1894 +    gdb_expect 10 {
1895 +       -re "$gdb_prompt $" {
1896 +           verbose "Disabled the missing debug infos warnings." 2
1897 +       }
1898 +       timeout {
1899 +           warning "Could not disable the missing debug infos warnings.."
1900 +       }
1901 +    }
1902 +
1903      gdb_debug_init
1904      return 0
1905  }
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."
1911         }
1912      }
1913 +    # Turn off the missing warnings as the testsuite does not expect it.
1914 +    send_gdb "190-gdb-set build-id-verbose 0\n"
1915 +    gdb_expect 10 {
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
1918 +       }
1919 +       timeout {
1920 +           warning "Could not disable the missing debug infos warnings.."
1921 +       }
1922 +    }
1923  
1924      if { $separate_inferior_pty } {
1925         mi_create_inferior_pty
This page took 0.18927 seconds and 3 git commands to generate.