1 Index: gdb-7.4.91.20120801/gdb/corelow.c
2 ===================================================================
3 --- gdb-7.4.91.20120801.orig/gdb/corelow.c 2012-08-01 18:36:51.000000000 +0200
4 +++ gdb-7.4.91.20120801/gdb/corelow.c 2012-08-01 18:37:44.584925750 +0200
10 +#include "elf/common.h"
15 @@ -274,6 +277,52 @@ add_to_thread_list (bfd *abfd, asection
16 inferior_ptid = ptid; /* Yes, make it current. */
19 +static int build_id_core_loads = 1;
22 +build_id_locate_exec (int from_tty)
25 + struct build_id *build_id;
26 + char *exec_filename, *debug_filename;
27 + char *build_id_filename;
28 + struct cleanup *back_to;
30 + if (exec_bfd != NULL || symfile_objfile != NULL)
33 + if (target_auxv_search (¤t_target, AT_ENTRY, &at_entry) <= 0)
36 + build_id = build_id_addr_get (at_entry);
37 + if (build_id == NULL)
40 + /* SYMFILE_OBJFILE should refer to the main executable (not only to its
41 + separate debug info file). gcc44+ keeps .eh_frame only in the main
42 + executable without its duplicate .debug_frame in the separate debug info
43 + file - such .eh_frame would not be found if SYMFILE_OBJFILE would refer
44 + directly to the separate debug info file. */
46 + exec_filename = build_id_to_filename (build_id, &build_id_filename, 0);
47 + back_to = make_cleanup (xfree, build_id_filename);
49 + if (exec_filename != NULL)
51 + make_cleanup (xfree, exec_filename);
52 + exec_file_attach (exec_filename, from_tty);
53 + symbol_file_add_main (exec_filename, from_tty);
54 + if (symfile_objfile != NULL)
55 + symfile_objfile->flags |= OBJF_BUILD_ID_CORE_LOADED;
58 + debug_print_missing (_("the main executable file"), build_id_filename);
60 + do_cleanups (back_to);
62 + /* No automatic SOLIB_ADD as the libraries would get read twice. */
65 /* This routine opens and sets up the core file bfd. */
68 @@ -418,6 +467,14 @@ core_open (char *filename, int from_tty)
69 switch_to_thread (thread->ptid);
72 + /* Find the build_id identifiers. If it gets executed after
73 + POST_CREATE_INFERIOR we would clash with asking to discard the already
74 + loaded VDSO symbols. If it gets executed before bfd_map_over_sections
75 + INFERIOR_PTID is still not set and libthread_db initialization crashes on
76 + PID == 0 in ps_pglobal_lookup. */
77 + if (build_id_core_loads != 0)
78 + build_id_locate_exec (from_tty);
80 post_create_inferior (&core_ops, from_tty);
82 /* Now go through the target stack looking for threads since there
83 @@ -939,4 +996,11 @@ _initialize_corelow (void)
86 add_target (&core_ops);
88 + add_setshow_boolean_cmd ("build-id-core-loads", class_files,
89 + &build_id_core_loads, _("\
90 +Set whether CORE-FILE loads the build-id associated files automatically."), _("\
91 +Show whether CORE-FILE loads the build-id associated files automatically."),
93 + &setlist, &showlist);
95 Index: gdb-7.4.91.20120801/gdb/doc/gdb.texinfo
96 ===================================================================
97 --- gdb-7.4.91.20120801.orig/gdb/doc/gdb.texinfo 2012-08-01 18:36:51.000000000 +0200
98 +++ gdb-7.4.91.20120801/gdb/doc/gdb.texinfo 2012-08-01 18:37:44.598925672 +0200
99 @@ -16530,6 +16530,27 @@ information files.
103 +You can also adjust the current verbosity of the @dfn{build id} locating.
107 +@kindex set build-id-verbose
108 +@item set build-id-verbose 0
109 +No additional messages are printed.
111 +@item set build-id-verbose 1
112 +Missing separate debug filenames are printed.
114 +@item set build-id-verbose 2
115 +Missing separate debug filenames are printed and also all the parsing of the
116 +binaries to find their @dfn{build id} content is printed.
118 +@kindex show build-id-verbose
119 +@item show build-id-verbose
120 +Show the current verbosity value for the @dfn{build id} content locating.
124 @cindex @code{.gnu_debuglink} sections
125 @cindex debug link sections
126 A debug link is a special section of the executable file named
127 Index: gdb-7.4.91.20120801/gdb/solib-svr4.c
128 ===================================================================
129 --- gdb-7.4.91.20120801.orig/gdb/solib-svr4.c 2012-08-01 18:36:51.000000000 +0200
130 +++ gdb-7.4.91.20120801/gdb/solib-svr4.c 2012-08-01 18:37:44.599925667 +0200
131 @@ -1228,9 +1228,52 @@ svr4_read_so_list (CORE_ADDR lm, struct
135 - strncpy (new->so_name, buffer, SO_NAME_MAX_PATH_SIZE - 1);
136 - new->so_name[SO_NAME_MAX_PATH_SIZE - 1] = '\0';
137 - strcpy (new->so_original_name, new->so_name);
139 + struct build_id *build_id;
141 + strncpy (new->so_original_name, buffer, SO_NAME_MAX_PATH_SIZE - 1);
142 + new->so_original_name[SO_NAME_MAX_PATH_SIZE - 1] = '\0';
143 + /* May get overwritten below. */
144 + strcpy (new->so_name, new->so_original_name);
146 + build_id = build_id_addr_get (new->lm_info->l_ld);
147 + if (build_id != NULL)
149 + char *name, *build_id_filename;
151 + /* Missing the build-id matching separate debug info file
152 + would be handled while SO_NAME gets loaded. */
153 + name = build_id_to_filename (build_id, &build_id_filename, 0);
156 + strncpy (new->so_name, name, SO_NAME_MAX_PATH_SIZE - 1);
157 + new->so_name[SO_NAME_MAX_PATH_SIZE - 1] = '\0';
162 + debug_print_missing (new->so_name, build_id_filename);
164 + /* In the case the main executable was found according to
165 + its build-id (from a core file) prevent loading
166 + a different build of a library with accidentally the
169 + It suppresses bogus backtraces (and prints "??" there
170 + instead) if the on-disk files no longer match the
171 + running program version. */
173 + if (symfile_objfile != NULL
174 + && (symfile_objfile->flags
175 + & OBJF_BUILD_ID_CORE_LOADED) != 0)
176 + new->so_name[0] = 0;
179 + xfree (build_id_filename);
186 /* If this entry has no name, or its name matches the name
187 Index: gdb-7.4.91.20120801/gdb/elfread.c
188 ===================================================================
189 --- gdb-7.4.91.20120801.orig/gdb/elfread.c 2012-08-01 18:36:51.000000000 +0200
190 +++ gdb-7.4.91.20120801/gdb/elfread.c 2012-08-01 18:38:08.956790879 +0200
192 #include "regcache.h"
196 +#include "gdbcore.h"
198 +#include "observer.h"
199 +#include <sys/stat.h>
201 extern void _initialize_elfread (void);
203 @@ -1074,16 +1079,65 @@ elf_gnu_ifunc_resolver_return_stop (stru
204 update_breakpoint_locations (b, sals, sals_end);
207 +#define BUILD_ID_VERBOSE_NONE 0
208 +#define BUILD_ID_VERBOSE_FILENAMES 1
209 +#define BUILD_ID_VERBOSE_BINARY_PARSE 2
210 +static int build_id_verbose = BUILD_ID_VERBOSE_FILENAMES;
212 +show_build_id_verbose (struct ui_file *file, int from_tty,
213 + struct cmd_list_element *c, const char *value)
215 + fprintf_filtered (file, _("Verbosity level of the build-id locator is %s.\n"),
225 -/* Locate NT_GNU_BUILD_ID from ABFD and return its content. */
226 +/* Locate NT_GNU_BUILD_ID and return its matching debug filename.
227 + FIXME: NOTE decoding should be unified with the BFD core notes decoding. */
229 static struct build_id *
230 -build_id_bfd_get (bfd *abfd)
231 +build_id_buf_get (bfd *templ, gdb_byte *buf, bfd_size_type size)
236 + while (p < buf + size)
238 + /* FIXME: bad alignment assumption. */
239 + Elf_External_Note *xnp = (Elf_External_Note *) p;
240 + size_t namesz = H_GET_32 (templ, xnp->namesz);
241 + size_t descsz = H_GET_32 (templ, xnp->descsz);
242 + bfd_byte *descdata = xnp->name + BFD_ALIGN (namesz, 4);
244 + if (H_GET_32 (templ, xnp->type) == NT_GNU_BUILD_ID
245 + && namesz == sizeof "GNU"
246 + && memcmp (xnp->name, "GNU", sizeof "GNU") == 0)
248 + size_t size = descsz;
249 + gdb_byte *data = (void *) descdata;
250 + struct build_id *retval;
252 + retval = xmalloc (sizeof *retval - 1 + size);
253 + retval->size = size;
254 + memcpy (retval->data, data, size);
258 + p = descdata + BFD_ALIGN (descsz, 4);
263 +/* Separate debuginfo files have corrupted PHDR but SHDR is correct there.
264 + Locate NT_GNU_BUILD_ID from ABFD and return its content. */
266 +static struct build_id *
267 +build_id_bfd_shdr_get (bfd *abfd)
269 struct build_id *retval;
271 @@ -1099,6 +1153,348 @@ build_id_bfd_get (bfd *abfd)
275 +/* Core files may have missing (corrupt) SHDR but PDHR is correct there.
276 + bfd_elf_bfd_from_remote_memory () has too much overhead by
277 + allocating/reading all the available ELF PT_LOADs. */
279 +static struct build_id *
280 +build_id_phdr_get (bfd *templ, bfd_vma loadbase, unsigned e_phnum,
281 + Elf_Internal_Phdr *i_phdr)
284 + struct build_id *retval = NULL;
286 + for (i = 0; i < e_phnum; i++)
287 + if (i_phdr[i].p_type == PT_NOTE && i_phdr[i].p_filesz > 0)
289 + Elf_Internal_Phdr *hdr = &i_phdr[i];
293 + buf = xmalloc (hdr->p_filesz);
294 + err = target_read_memory (loadbase + i_phdr[i].p_vaddr, buf,
297 + retval = build_id_buf_get (templ, buf, hdr->p_filesz);
301 + if (retval != NULL)
307 +/* First we validate the file by reading in the ELF header and checking
308 + the magic number. */
310 +static inline bfd_boolean
311 +elf_file_p (Elf64_External_Ehdr *x_ehdrp64)
313 + gdb_assert (sizeof (Elf64_External_Ehdr) >= sizeof (Elf32_External_Ehdr));
314 + gdb_assert (offsetof (Elf64_External_Ehdr, e_ident)
315 + == offsetof (Elf32_External_Ehdr, e_ident));
316 + gdb_assert (sizeof (((Elf64_External_Ehdr *) 0)->e_ident)
317 + == sizeof (((Elf32_External_Ehdr *) 0)->e_ident));
319 + return ((x_ehdrp64->e_ident[EI_MAG0] == ELFMAG0)
320 + && (x_ehdrp64->e_ident[EI_MAG1] == ELFMAG1)
321 + && (x_ehdrp64->e_ident[EI_MAG2] == ELFMAG2)
322 + && (x_ehdrp64->e_ident[EI_MAG3] == ELFMAG3));
325 +/* Translate an ELF file header in external format into an ELF file header in
326 + internal format. */
328 +#define H_GET_WORD(bfd, ptr) (is64 ? H_GET_64 (bfd, (ptr)) \
329 + : H_GET_32 (bfd, (ptr)))
330 +#define H_GET_SIGNED_WORD(bfd, ptr) (is64 ? H_GET_S64 (bfd, (ptr)) \
331 + : H_GET_S32 (bfd, (ptr)))
334 +elf_swap_ehdr_in (bfd *abfd,
335 + const Elf64_External_Ehdr *src64,
336 + Elf_Internal_Ehdr *dst)
338 + int is64 = bfd_get_arch_size (abfd) == 64;
339 +#define SRC(field) (is64 ? src64->field \
340 + : ((const Elf32_External_Ehdr *) src64)->field)
342 + int signed_vma = get_elf_backend_data (abfd)->sign_extend_vma;
343 + memcpy (dst->e_ident, SRC (e_ident), EI_NIDENT);
344 + dst->e_type = H_GET_16 (abfd, SRC (e_type));
345 + dst->e_machine = H_GET_16 (abfd, SRC (e_machine));
346 + dst->e_version = H_GET_32 (abfd, SRC (e_version));
348 + dst->e_entry = H_GET_SIGNED_WORD (abfd, SRC (e_entry));
350 + dst->e_entry = H_GET_WORD (abfd, SRC (e_entry));
351 + dst->e_phoff = H_GET_WORD (abfd, SRC (e_phoff));
352 + dst->e_shoff = H_GET_WORD (abfd, SRC (e_shoff));
353 + dst->e_flags = H_GET_32 (abfd, SRC (e_flags));
354 + dst->e_ehsize = H_GET_16 (abfd, SRC (e_ehsize));
355 + dst->e_phentsize = H_GET_16 (abfd, SRC (e_phentsize));
356 + dst->e_phnum = H_GET_16 (abfd, SRC (e_phnum));
357 + dst->e_shentsize = H_GET_16 (abfd, SRC (e_shentsize));
358 + dst->e_shnum = H_GET_16 (abfd, SRC (e_shnum));
359 + dst->e_shstrndx = H_GET_16 (abfd, SRC (e_shstrndx));
364 +/* Translate an ELF program header table entry in external format into an
365 + ELF program header table entry in internal format. */
368 +elf_swap_phdr_in (bfd *abfd,
369 + const Elf64_External_Phdr *src64,
370 + Elf_Internal_Phdr *dst)
372 + int is64 = bfd_get_arch_size (abfd) == 64;
373 +#define SRC(field) (is64 ? src64->field \
374 + : ((const Elf32_External_Phdr *) src64)->field)
376 + int signed_vma = get_elf_backend_data (abfd)->sign_extend_vma;
378 + dst->p_type = H_GET_32 (abfd, SRC (p_type));
379 + dst->p_flags = H_GET_32 (abfd, SRC (p_flags));
380 + dst->p_offset = H_GET_WORD (abfd, SRC (p_offset));
383 + dst->p_vaddr = H_GET_SIGNED_WORD (abfd, SRC (p_vaddr));
384 + dst->p_paddr = H_GET_SIGNED_WORD (abfd, SRC (p_paddr));
388 + dst->p_vaddr = H_GET_WORD (abfd, SRC (p_vaddr));
389 + dst->p_paddr = H_GET_WORD (abfd, SRC (p_paddr));
391 + dst->p_filesz = H_GET_WORD (abfd, SRC (p_filesz));
392 + dst->p_memsz = H_GET_WORD (abfd, SRC (p_memsz));
393 + dst->p_align = H_GET_WORD (abfd, SRC (p_align));
398 +#undef H_GET_SIGNED_WORD
401 +static Elf_Internal_Phdr *
402 +elf_get_phdr (bfd *templ, bfd_vma ehdr_vma, unsigned *e_phnum_pointer,
403 + bfd_vma *loadbase_pointer)
405 + /* sizeof (Elf64_External_Ehdr) >= sizeof (Elf32_External_Ehdr) */
406 + Elf64_External_Ehdr x_ehdr64; /* Elf file header, external form */
407 + Elf_Internal_Ehdr i_ehdr; /* Elf file header, internal form */
408 + bfd_size_type x_phdrs_size;
409 + gdb_byte *x_phdrs_ptr;
410 + Elf_Internal_Phdr *i_phdrs;
416 + gdb_assert (templ != NULL);
417 + gdb_assert (sizeof (Elf64_External_Ehdr) >= sizeof (Elf32_External_Ehdr));
419 + /* Read in the ELF header in external format. */
420 + err = target_read_memory (ehdr_vma, (bfd_byte *) &x_ehdr64, sizeof x_ehdr64);
423 + if (build_id_verbose >= BUILD_ID_VERBOSE_BINARY_PARSE)
424 + warning (_("build-id: Error reading ELF header at address 0x%lx"),
425 + (unsigned long) ehdr_vma);
429 + /* Now check to see if we have a valid ELF file, and one that BFD can
430 + make use of. The magic number must match, the address size ('class')
431 + and byte-swapping must match our XVEC entry. */
433 + if (! elf_file_p (&x_ehdr64)
434 + || x_ehdr64.e_ident[EI_VERSION] != EV_CURRENT
435 + || !((bfd_get_arch_size (templ) == 64
436 + && x_ehdr64.e_ident[EI_CLASS] == ELFCLASS64)
437 + || (bfd_get_arch_size (templ) == 32
438 + && x_ehdr64.e_ident[EI_CLASS] == ELFCLASS32)))
440 + if (build_id_verbose >= BUILD_ID_VERBOSE_BINARY_PARSE)
441 + warning (_("build-id: Unrecognized ELF header at address 0x%lx"),
442 + (unsigned long) ehdr_vma);
446 + /* Check that file's byte order matches xvec's */
447 + switch (x_ehdr64.e_ident[EI_DATA])
449 + case ELFDATA2MSB: /* Big-endian */
450 + if (! bfd_header_big_endian (templ))
452 + if (build_id_verbose >= BUILD_ID_VERBOSE_BINARY_PARSE)
453 + warning (_("build-id: Unrecognized "
454 + "big-endian ELF header at address 0x%lx"),
455 + (unsigned long) ehdr_vma);
459 + case ELFDATA2LSB: /* Little-endian */
460 + if (! bfd_header_little_endian (templ))
462 + if (build_id_verbose >= BUILD_ID_VERBOSE_BINARY_PARSE)
463 + warning (_("build-id: Unrecognized "
464 + "little-endian ELF header at address 0x%lx"),
465 + (unsigned long) ehdr_vma);
469 + case ELFDATANONE: /* No data encoding specified */
470 + default: /* Unknown data encoding specified */
471 + if (build_id_verbose >= BUILD_ID_VERBOSE_BINARY_PARSE)
472 + warning (_("build-id: Unrecognized "
473 + "ELF header endianity at address 0x%lx"),
474 + (unsigned long) ehdr_vma);
478 + elf_swap_ehdr_in (templ, &x_ehdr64, &i_ehdr);
480 + /* The file header tells where to find the program headers.
481 + These are what we use to actually choose what to read. */
483 + if (i_ehdr.e_phentsize != (bfd_get_arch_size (templ) == 64
484 + ? sizeof (Elf64_External_Phdr)
485 + : sizeof (Elf32_External_Phdr))
486 + || i_ehdr.e_phnum == 0)
488 + if (build_id_verbose >= BUILD_ID_VERBOSE_BINARY_PARSE)
489 + warning (_("build-id: Invalid ELF program headers from the ELF header "
490 + "at address 0x%lx"), (unsigned long) ehdr_vma);
494 + x_phdrs_size = (bfd_get_arch_size (templ) == 64 ? sizeof (Elf64_External_Phdr)
495 + : sizeof (Elf32_External_Phdr));
497 + i_phdrs = xmalloc (i_ehdr.e_phnum * (sizeof *i_phdrs + x_phdrs_size));
498 + x_phdrs_ptr = (void *) &i_phdrs[i_ehdr.e_phnum];
499 + err = target_read_memory (ehdr_vma + i_ehdr.e_phoff, (bfd_byte *) x_phdrs_ptr,
500 + i_ehdr.e_phnum * x_phdrs_size);
504 + if (build_id_verbose >= BUILD_ID_VERBOSE_BINARY_PARSE)
505 + warning (_("build-id: Error reading "
506 + "ELF program headers at address 0x%lx"),
507 + (unsigned long) (ehdr_vma + i_ehdr.e_phoff));
511 + loadbase = ehdr_vma;
513 + for (i = 0; i < i_ehdr.e_phnum; ++i)
515 + elf_swap_phdr_in (templ, (Elf64_External_Phdr *)
516 + (x_phdrs_ptr + i * x_phdrs_size), &i_phdrs[i]);
517 + /* IA-64 vDSO may have two mappings for one segment, where one mapping
518 + is executable only, and one is read only. We must not use the
519 + executable one (PF_R is the first one, PF_X the second one). */
520 + if (i_phdrs[i].p_type == PT_LOAD && (i_phdrs[i].p_flags & PF_R))
522 + /* Only the first PT_LOAD segment indicates the file bias.
523 + Next segments may have P_VADDR arbitrarily higher.
524 + If the first segment has P_VADDR zero any next segment must not
525 + confuse us, the first one sets LOADBASE certainly enough. */
526 + if (!loadbase_set && i_phdrs[i].p_offset == 0)
528 + loadbase = ehdr_vma - i_phdrs[i].p_vaddr;
534 + if (build_id_verbose >= BUILD_ID_VERBOSE_BINARY_PARSE)
535 + warning (_("build-id: Found ELF header at address 0x%lx, loadbase 0x%lx"),
536 + (unsigned long) ehdr_vma, (unsigned long) loadbase);
538 + *e_phnum_pointer = i_ehdr.e_phnum;
539 + *loadbase_pointer = loadbase;
543 +/* BUILD_ID_ADDR_GET gets ADDR located somewhere in the object.
544 + Find the first section before ADDR containing an ELF header.
545 + We rely on the fact the sections from multiple files do not mix.
546 + FIXME: We should check ADDR is contained _inside_ the section with possibly
547 + missing content (P_FILESZ < P_MEMSZ). These omitted sections are currently
548 + hidden by _BFD_ELF_MAKE_SECTION_FROM_PHDR. */
550 +static CORE_ADDR build_id_addr;
551 +struct build_id_addr_sect
553 + struct build_id_addr_sect *next;
556 +static struct build_id_addr_sect *build_id_addr_sect;
558 +static void build_id_addr_candidate (bfd *abfd, asection *sect, void *obj)
560 + if (build_id_addr >= bfd_section_vma (abfd, sect))
562 + struct build_id_addr_sect *candidate;
564 + candidate = xmalloc (sizeof *candidate);
565 + candidate->next = build_id_addr_sect;
566 + build_id_addr_sect = candidate;
567 + candidate->sect = sect;
572 +build_id_addr_get (CORE_ADDR addr)
574 + struct build_id_addr_sect *candidate;
575 + struct build_id *retval = NULL;
576 + Elf_Internal_Phdr *i_phdr = NULL;
577 + bfd_vma loadbase = 0;
578 + unsigned e_phnum = 0;
580 + if (core_bfd == NULL)
583 + build_id_addr = addr;
584 + gdb_assert (build_id_addr_sect == NULL);
585 + bfd_map_over_sections (core_bfd, build_id_addr_candidate, NULL);
587 + /* Sections are sorted in the high-to-low VMAs order.
588 + Stop the search on the first ELF header we find.
589 + Do not continue the search even if it does not contain NT_GNU_BUILD_ID. */
591 + for (candidate = build_id_addr_sect; candidate != NULL;
592 + candidate = candidate->next)
594 + i_phdr = elf_get_phdr (core_bfd,
595 + bfd_section_vma (core_bfd, candidate->sect),
596 + &e_phnum, &loadbase);
597 + if (i_phdr != NULL)
601 + if (i_phdr != NULL)
603 + retval = build_id_phdr_get (core_bfd, loadbase, e_phnum, i_phdr);
607 + while (build_id_addr_sect != NULL)
609 + candidate = build_id_addr_sect;
610 + build_id_addr_sect = candidate->next;
617 /* Return if FILENAME has NT_GNU_BUILD_ID matching the CHECK value. */
620 @@ -1113,7 +1509,7 @@ build_id_verify (const char *filename, s
624 - found = build_id_bfd_get (abfd);
625 + found = build_id_bfd_shdr_get (abfd);
628 warning (_("File \"%s\" has no build-id, file skipped"), filename);
629 @@ -1131,17 +1527,18 @@ build_id_verify (const char *filename, s
634 -build_id_to_debug_filename (struct build_id *build_id)
636 +build_id_to_filename (struct build_id *build_id, char **link_return,
637 + int add_debug_suffix)
639 char *link, *debugdir, *retval = NULL;
640 + char *link_all = NULL;
641 VEC (char_ptr) *debugdir_vec;
642 struct cleanup *back_to;
645 /* DEBUG_FILE_DIRECTORY/.build-id/ab/cdef */
646 - link = alloca (strlen (debug_file_directory) + (sizeof "/.build-id/" - 1) + 1
647 - + 2 * build_id->size + (sizeof ".debug" - 1) + 1);
648 + link = xmalloc (strlen (debug_file_directory) + 2 * build_id->size + 50);
650 /* Keep backward compatibility so that DEBUG_FILE_DIRECTORY being "" will
651 cause "/.build-id/..." lookups. */
652 @@ -1154,6 +1551,8 @@ build_id_to_debug_filename (struct build
653 size_t debugdir_len = strlen (debugdir);
654 gdb_byte *data = build_id->data;
655 size_t size = build_id->size;
657 + struct stat statbuf_trash;
660 memcpy (link, debugdir, debugdir_len);
661 @@ -1168,37 +1567,240 @@ build_id_to_debug_filename (struct build
664 s += sprintf (s, "%02x", (unsigned) *data++);
665 - strcpy (s, ".debug");
667 - /* lrealpath() is expensive even for the usually non-existent files. */
668 - if (access (link, F_OK) == 0)
669 - retval = lrealpath (link);
670 + for (seqno = 0;; seqno++)
676 + /* There can be multiple build-id symlinks pointing to real files
677 + with the same build-id (such as hard links). Some of the real
678 + files may not be installed. */
680 + s2 = s + sprintf (s, ".%u", seqno);
685 + if (add_debug_suffix)
686 + strcpy (s2, ".debug");
690 - if (retval != NULL && !build_id_verify (retval, build_id))
691 + /* `access' automatically dereferences LINK. */
692 + if (lstat (link, &statbuf_trash) != 0)
694 + /* Stop increasing SEQNO. */
698 + retval = lrealpath (link);
700 + if (retval != NULL && !build_id_verify (retval, build_id))
710 + if (retval != NULL)
714 + /* LINK_ALL is not used below in this non-NULL RETVAL case. */
718 + if (link_all == NULL)
719 + link_all = xstrdup (link);
722 + size_t len_orig = strlen (link_all);
724 + link_all = xrealloc (link_all, len_orig + 1 + strlen (link) + 1);
726 + /* Use whitespace instead of DIRNAME_SEPARATOR to be compatible with
727 + its possible use as an argument for installation command. */
728 + link_all[len_orig] = ' ';
730 + strcpy (&link_all[len_orig + 1], link);
734 + if (link_return != NULL)
739 + *link_return = link;
744 + *link_return = link_all;
751 do_cleanups (back_to);
755 +/* This MISSING_FILEPAIR_HASH tracker is used only for the duplicite messages
756 + Try to install the hash file ...
759 +struct missing_filepair
766 +static struct htab *missing_filepair_hash;
767 +static struct obstack missing_filepair_obstack;
770 +missing_filepair_xcalloc (size_t nmemb, size_t nmemb_size)
773 + size_t size = nmemb * nmemb_size;
775 + retval = obstack_alloc (&missing_filepair_obstack, size);
776 + memset (retval, 0, size);
781 +missing_filepair_hash_func (const struct missing_filepair *elem)
783 + hashval_t retval = 0;
785 + retval ^= htab_hash_string (elem->binary);
786 + if (elem->debug != NULL)
787 + retval ^= htab_hash_string (elem->debug);
793 +missing_filepair_eq (const struct missing_filepair *elem1,
794 + const struct missing_filepair *elem2)
796 + return strcmp (elem1->binary, elem2->binary) == 0
797 + && ((elem1->debug == NULL) == (elem2->debug == NULL))
798 + && (elem1->debug == NULL || strcmp (elem1->debug, elem2->debug) == 0);
802 +missing_filepair_change (void)
804 + if (missing_filepair_hash != NULL)
806 + obstack_free (&missing_filepair_obstack, NULL);
807 + /* All their memory came just from missing_filepair_OBSTACK. */
808 + missing_filepair_hash = NULL;
813 +debug_print_executable_changed (void)
815 + missing_filepair_change ();
818 +/* Notify user the file BINARY with (possibly NULL) associated separate debug
819 + information file DEBUG is missing. DEBUG may or may not be the build-id
820 + file such as would be:
821 + /usr/lib/debug/.build-id/dd/b1d2ce632721c47bb9e8679f369e2295ce71be.debug
825 +debug_print_missing (const char *binary, const char *debug)
827 + size_t binary_len0 = strlen (binary) + 1;
828 + size_t debug_len0 = debug ? strlen (debug) + 1 : 0;
829 + struct missing_filepair missing_filepair_find;
830 + struct missing_filepair *missing_filepair;
831 + struct missing_filepair **slot;
833 + if (build_id_verbose < BUILD_ID_VERBOSE_FILENAMES)
836 + if (missing_filepair_hash == NULL)
838 + obstack_init (&missing_filepair_obstack);
839 + missing_filepair_hash = htab_create_alloc (64,
840 + (hashval_t (*) (const void *)) missing_filepair_hash_func,
841 + (int (*) (const void *, const void *)) missing_filepair_eq, NULL,
842 + missing_filepair_xcalloc, NULL);
845 + /* Use MISSING_FILEPAIR_FIND first instead of calling obstack_alloc with
846 + obstack_free in the case of a (rare) match. The problem is ALLOC_F for
847 + MISSING_FILEPAIR_HASH allocates from MISSING_FILEPAIR_OBSTACK maintenance
848 + structures for MISSING_FILEPAIR_HASH. Calling obstack_free would possibly
849 + not to free only MISSING_FILEPAIR but also some such structures (allocated
850 + during the htab_find_slot call). */
852 + missing_filepair_find.binary = (char *) binary;
853 + missing_filepair_find.debug = (char *) debug;
854 + slot = (struct missing_filepair **) htab_find_slot (missing_filepair_hash,
855 + &missing_filepair_find,
858 + /* While it may be still printed duplicitely with the missing debuginfo file
859 + * it is due to once printing about the binary file build-id link and once
860 + * about the .debug file build-id link as both the build-id symlinks are
861 + * located in the debuginfo package. */
866 + missing_filepair = obstack_alloc (&missing_filepair_obstack,
867 + sizeof (*missing_filepair) - 1
868 + + binary_len0 + debug_len0);
869 + missing_filepair->binary = missing_filepair->data;
870 + memcpy (missing_filepair->binary, binary, binary_len0);
873 + missing_filepair->debug = missing_filepair->binary + binary_len0;
874 + memcpy (missing_filepair->debug, debug, debug_len0);
877 + missing_filepair->debug = NULL;
879 + *slot = missing_filepair;
881 + /* We do not collect and flush these messages as each such message
882 + already requires its own separate lines. */
884 + fprintf_unfiltered (gdb_stdlog,
885 + _("Missing separate debuginfo for %s\n"), binary);
887 + fprintf_unfiltered (gdb_stdlog, _("Try to install the hash file %s\n"),
892 -find_separate_debug_file_by_buildid (struct objfile *objfile)
893 +find_separate_debug_file_by_buildid (struct objfile *objfile,
894 + char **build_id_filename_return)
896 struct build_id *build_id;
898 - build_id = build_id_bfd_get (objfile->obfd);
899 + if (build_id_filename_return)
900 + *build_id_filename_return = NULL;
902 + build_id = build_id_bfd_shdr_get (objfile->obfd);
903 if (build_id != NULL)
907 - build_id_name = build_id_to_debug_filename (build_id);
908 + build_id_name = build_id_to_filename (build_id, build_id_filename_return,
911 /* Prevent looping on a stripped .debug file. */
912 if (build_id_name != NULL
913 @@ -1209,7 +1811,7 @@ find_separate_debug_file_by_buildid (str
914 xfree (build_id_name);
916 else if (build_id_name != NULL)
917 - return build_id_name;
918 + return build_id_name;
922 @@ -1436,9 +2038,10 @@ elf_symfile_read (struct objfile *objfil
923 `.note.gnu.build-id'. */
924 else if (!objfile_has_partial_symbols (objfile))
927 + char *debugfile, *build_id_filename;
929 - debugfile = find_separate_debug_file_by_buildid (objfile);
930 + debugfile = find_separate_debug_file_by_buildid (objfile,
931 + &build_id_filename);
933 if (debugfile == NULL)
934 debugfile = find_separate_debug_file_by_debuglink (objfile);
935 @@ -1452,6 +2055,12 @@ elf_symfile_read (struct objfile *objfil
936 symbol_file_add_separate (abfd, symfile_flags, objfile);
937 do_cleanups (cleanup);
939 + /* Check if any separate debug info has been extracted out. */
940 + else if (bfd_get_section_by_name (objfile->obfd, ".gnu_debuglink")
942 + debug_print_missing (objfile->name, build_id_filename);
944 + xfree (build_id_filename);
947 if (symtab_create_debug)
948 @@ -1784,4 +2393,16 @@ _initialize_elfread (void)
950 elf_objfile_gnu_ifunc_cache_data = register_objfile_data ();
951 gnu_ifunc_fns_p = &elf_gnu_ifunc_fns;
953 + add_setshow_zinteger_cmd ("build-id-verbose", no_class, &build_id_verbose,
955 +Set debugging level of the build-id locator."), _("\
956 +Show debugging level of the build-id locator."), _("\
957 +Level 1 (default) enables printing the missing debug filenames,\n\
958 +level 2 also prints the parsing of binaries to find the identificators."),
960 + show_build_id_verbose,
961 + &setlist, &showlist);
963 + observer_attach_executable_changed (debug_print_executable_changed);
965 Index: gdb-7.4.91.20120801/gdb/symfile.h
966 ===================================================================
967 --- gdb-7.4.91.20120801.orig/gdb/symfile.h 2012-08-01 18:36:51.000000000 +0200
968 +++ gdb-7.4.91.20120801/gdb/symfile.h 2012-08-01 18:37:44.600925662 +0200
969 @@ -615,6 +615,13 @@ void free_symfile_segment_data (struct s
971 extern struct cleanup *increment_reading_symtab (void);
973 +/* build-id support. */
975 +extern struct build_id *build_id_addr_get (CORE_ADDR addr);
976 +extern char *build_id_to_filename (struct build_id *build_id,
977 + char **link_return, int add_debug_suffix);
978 +extern void debug_print_missing (const char *binary, const char *debug);
980 /* From dwarf2read.c */
982 /* Names for a dwarf2 debugging section. The field NORMAL is the normal
983 Index: gdb-7.4.91.20120801/gdb/testsuite/lib/gdb.exp
984 ===================================================================
985 --- gdb-7.4.91.20120801.orig/gdb/testsuite/lib/gdb.exp 2012-08-01 18:36:51.000000000 +0200
986 +++ gdb-7.4.91.20120801/gdb/testsuite/lib/gdb.exp 2012-08-01 18:37:44.601925656 +0200
987 @@ -1388,6 +1388,16 @@ proc default_gdb_start { } {
988 warning "Couldn't set the width to 0."
991 + # Turn off the missing warnings as the testsuite does not expect it.
992 + send_gdb "set build-id-verbose 0\n"
994 + -re "$gdb_prompt $" {
995 + verbose "Disabled the missing debug infos warnings." 2
998 + warning "Could not disable the missing debug infos warnings.."
1004 Index: gdb-7.4.91.20120801/gdb/testsuite/lib/mi-support.exp
1005 ===================================================================
1006 --- gdb-7.4.91.20120801.orig/gdb/testsuite/lib/mi-support.exp 2012-07-25 22:35:30.000000000 +0200
1007 +++ gdb-7.4.91.20120801/gdb/testsuite/lib/mi-support.exp 2012-08-01 18:37:44.601925656 +0200
1008 @@ -213,6 +213,16 @@ proc default_mi_gdb_start { args } {
1009 warning "Couldn't set the width to 0."
1012 + # Turn off the missing warnings as the testsuite does not expect it.
1013 + send_gdb "190-gdb-set build-id-verbose 0\n"
1015 + -re ".*190-gdb-set build-id-verbose 0\r\n190\\\^done\r\n$mi_gdb_prompt$" {
1016 + verbose "Disabled the missing debug infos warnings." 2
1019 + warning "Could not disable the missing debug infos warnings.."
1022 # If allowing the inferior to have its own PTY then assign the inferior
1023 # its own terminal device here.
1024 if { $separate_inferior_pty } {
1025 Index: gdb-7.4.91.20120801/gdb/objfiles.h
1026 ===================================================================
1027 --- gdb-7.4.91.20120801.orig/gdb/objfiles.h 2012-08-01 18:36:51.000000000 +0200
1028 +++ gdb-7.4.91.20120801/gdb/objfiles.h 2012-08-01 18:37:44.601925656 +0200
1029 @@ -432,6 +432,10 @@ struct objfile
1031 #define OBJF_MAINLINE (1 << 5)
1033 +/* This file was loaded according to the BUILD_ID_CORE_LOADS rules. */
1035 +#define OBJF_BUILD_ID_CORE_LOADED (1 << 12)
1037 /* The object file that contains the runtime common minimal symbols
1038 for SunOS4. Note that this objfile has no associated BFD. */
1040 Index: gdb-7.4.91.20120801/gdb/testsuite/gdb.base/corefile.exp
1041 ===================================================================
1042 --- gdb-7.4.91.20120801.orig/gdb/testsuite/gdb.base/corefile.exp 2012-06-21 22:46:21.000000000 +0200
1043 +++ gdb-7.4.91.20120801/gdb/testsuite/gdb.base/corefile.exp 2012-08-01 18:37:44.602925650 +0200
1044 @@ -256,3 +256,33 @@ if ![is_remote target] {
1050 +# Test auto-loading of binary files through build-id from the core file.
1051 +set buildid [build_id_debug_filename_get $binfile]
1052 +set wholetest "binfile found by build-id"
1053 +if {$buildid == ""} {
1054 + untested "$wholetest (binary has no build-id)"
1059 + regsub {\.debug$} $buildid {} buildid
1060 + set debugdir ${objdir}/${subdir}/${testfile}-debugdir
1061 + file delete -force -- $debugdir
1062 + file mkdir $debugdir/[file dirname $buildid]
1063 + file copy $binfile $debugdir/$buildid
1065 + set test "show debug-file-directory"
1066 + gdb_test_multiple $test $test {
1067 + -re "The directory where separate debug symbols are searched for is \"(.*)\"\\.\r\n$gdb_prompt $" {
1068 + set debugdir_orig $expect_out(1,string)
1072 + gdb_test_no_output "set debug-file-directory $debugdir:$debugdir_orig" "set debug-file-directory"
1073 + gdb_test "show build-id-core-loads" {Whether CORE-FILE loads the build-id associated files automatically is on\.}
1074 + gdb_test "core-file $corefile" "\r\nProgram terminated with .*" "core-file without executable"
1075 + gdb_test "info files" "Local exec file:\r\n\[ \t\]*`[string_to_regexp $debugdir/$buildid]', file type .*"