1 Index: gdb-7.10.90.20160211/gdb/corelow.c
2 ===================================================================
3 --- gdb-7.10.90.20160211.orig/gdb/corelow.c 2016-02-15 23:25:00.859346221 +0100
4 +++ gdb-7.10.90.20160211/gdb/corelow.c 2016-02-15 23:26:46.516096395 +0100
10 +#include "elf/common.h"
12 +#include "build-id.h"
17 inferior_ptid = ptid; /* Yes, make it current. */
20 +static int build_id_core_loads = 1;
23 +build_id_locate_exec (int from_tty)
26 + struct bfd_build_id *build_id;
27 + char *execfilename, *debug_filename;
28 + char *build_id_filename;
29 + struct cleanup *back_to;
31 + if (exec_bfd != NULL || symfile_objfile != NULL)
34 + if (target_auxv_search (¤t_target, AT_ENTRY, &at_entry) <= 0)
37 + build_id = build_id_addr_get (at_entry);
38 + if (build_id == NULL)
40 + back_to = make_cleanup (xfree, build_id);
42 + /* SYMFILE_OBJFILE should refer to the main executable (not only to its
43 + separate debug info file). gcc44+ keeps .eh_frame only in the main
44 + executable without its duplicate .debug_frame in the separate debug info
45 + file - such .eh_frame would not be found if SYMFILE_OBJFILE would refer
46 + directly to the separate debug info file. */
48 + execfilename = build_id_to_filename (build_id, &build_id_filename);
49 + make_cleanup (xfree, build_id_filename);
51 + if (execfilename != NULL)
53 + make_cleanup (xfree, execfilename);
54 + exec_file_attach (execfilename, from_tty);
55 + symbol_file_add_main (execfilename, from_tty);
56 + if (symfile_objfile != NULL)
57 + symfile_objfile->flags |= OBJF_BUILD_ID_CORE_LOADED;
60 + debug_print_missing (_("the main executable file"), build_id_filename);
62 + do_cleanups (back_to);
64 + /* No automatic SOLIB_ADD as the libraries would get read twice. */
67 /* This routine opens and sets up the core file bfd. */
71 switch_to_thread (thread->ptid);
74 + /* Find the build_id identifiers. If it gets executed after
75 + POST_CREATE_INFERIOR we would clash with asking to discard the already
76 + loaded VDSO symbols. If it gets executed before bfd_map_over_sections
77 + INFERIOR_PTID is still not set and libthread_db initialization crashes on
78 + PID == 0 in ps_pglobal_lookup. */
79 + if (build_id_core_loads != 0)
80 + build_id_locate_exec (from_tty);
82 post_create_inferior (&core_ops, from_tty);
84 /* Now go through the target stack looking for threads since there
85 @@ -1068,4 +1127,11 @@
88 add_target_with_completer (&core_ops, filename_completer);
90 + add_setshow_boolean_cmd ("build-id-core-loads", class_files,
91 + &build_id_core_loads, _("\
92 +Set whether CORE-FILE loads the build-id associated files automatically."), _("\
93 +Show whether CORE-FILE loads the build-id associated files automatically."),
95 + &setlist, &showlist);
97 Index: gdb-7.10.90.20160211/gdb/doc/gdb.texinfo
98 ===================================================================
99 --- gdb-7.10.90.20160211.orig/gdb/doc/gdb.texinfo 2016-02-15 23:25:36.455598958 +0100
100 +++ gdb-7.10.90.20160211/gdb/doc/gdb.texinfo 2016-02-15 23:26:06.362811302 +0100
101 @@ -18684,6 +18684,27 @@
105 +You can also adjust the current verbosity of the @dfn{build id} locating.
109 +@kindex set build-id-verbose
110 +@item set build-id-verbose 0
111 +No additional messages are printed.
113 +@item set build-id-verbose 1
114 +Missing separate debug filenames are printed.
116 +@item set build-id-verbose 2
117 +Missing separate debug filenames are printed and also all the parsing of the
118 +binaries to find their @dfn{build id} content is printed.
120 +@kindex show build-id-verbose
121 +@item show build-id-verbose
122 +Show the current verbosity value for the @dfn{build id} content locating.
126 @cindex @code{.gnu_debuglink} sections
127 @cindex debug link sections
128 A debug link is a special section of the executable file named
129 Index: gdb-7.10.90.20160211/gdb/solib-svr4.c
130 ===================================================================
131 --- gdb-7.10.90.20160211.orig/gdb/solib-svr4.c 2016-02-15 23:25:00.859346221 +0100
132 +++ gdb-7.10.90.20160211/gdb/solib-svr4.c 2016-02-15 23:26:46.613097083 +0100
137 +#include "build-id.h"
139 static struct link_map_offsets *svr4_fetch_link_map_offsets (void);
140 static int svr4_have_link_map_offsets (void);
141 @@ -1416,9 +1417,52 @@
145 - strncpy (newobj->so_name, buffer, SO_NAME_MAX_PATH_SIZE - 1);
146 - newobj->so_name[SO_NAME_MAX_PATH_SIZE - 1] = '\0';
147 - strcpy (newobj->so_original_name, newobj->so_name);
149 + struct bfd_build_id *build_id;
151 + strncpy (newobj->so_original_name, buffer, SO_NAME_MAX_PATH_SIZE - 1);
152 + newobj->so_original_name[SO_NAME_MAX_PATH_SIZE - 1] = '\0';
153 + /* May get overwritten below. */
154 + strcpy (newobj->so_name, newobj->so_original_name);
156 + build_id = build_id_addr_get (newobj->lm_info->l_ld);
157 + if (build_id != NULL)
159 + char *name, *build_id_filename;
161 + /* Missing the build-id matching separate debug info file
162 + would be handled while SO_NAME gets loaded. */
163 + name = build_id_to_filename (build_id, &build_id_filename);
166 + strncpy (newobj->so_name, name, SO_NAME_MAX_PATH_SIZE - 1);
167 + newobj->so_name[SO_NAME_MAX_PATH_SIZE - 1] = '\0';
172 + debug_print_missing (newobj->so_name, build_id_filename);
174 + /* In the case the main executable was found according to
175 + its build-id (from a core file) prevent loading
176 + a different build of a library with accidentally the
179 + It suppresses bogus backtraces (and prints "??" there
180 + instead) if the on-disk files no longer match the
181 + running program version. */
183 + if (symfile_objfile != NULL
184 + && (symfile_objfile->flags
185 + & OBJF_BUILD_ID_CORE_LOADED) != 0)
186 + newobj->so_name[0] = 0;
189 + xfree (build_id_filename);
196 /* If this entry has no name, or its name matches the name
197 Index: gdb-7.10.90.20160211/gdb/elfread.c
198 ===================================================================
199 --- gdb-7.10.90.20160211.orig/gdb/elfread.c 2016-02-15 23:25:00.859346221 +0100
200 +++ gdb-7.10.90.20160211/gdb/elfread.c 2016-02-15 23:26:06.364811316 +0100
201 @@ -1259,9 +1259,10 @@
202 && objfile->separate_debug_objfile == NULL
203 && objfile->separate_debug_objfile_backlink == NULL)
206 + char *debugfile, *build_id_filename;
208 - debugfile = find_separate_debug_file_by_buildid (objfile);
209 + debugfile = find_separate_debug_file_by_buildid (objfile,
210 + &build_id_filename);
212 if (debugfile == NULL)
213 debugfile = find_separate_debug_file_by_debuglink (objfile);
214 @@ -1275,6 +1276,12 @@
215 symbol_file_add_separate (abfd, debugfile, symfile_flags, objfile);
216 do_cleanups (cleanup);
218 + /* Check if any separate debug info has been extracted out. */
219 + else if (bfd_get_section_by_name (objfile->obfd, ".gnu_debuglink")
221 + debug_print_missing (objfile_name (objfile), build_id_filename);
223 + xfree (build_id_filename);
227 Index: gdb-7.10.90.20160211/gdb/symfile.h
228 ===================================================================
229 --- gdb-7.10.90.20160211.orig/gdb/symfile.h 2016-02-15 23:25:00.859346221 +0100
230 +++ gdb-7.10.90.20160211/gdb/symfile.h 2016-02-15 23:26:46.516096395 +0100
232 void map_symbol_filenames (symbol_filename_ftype *fun, void *data,
235 +/* build-id support. */
236 +extern struct bfd_build_id *build_id_addr_get (CORE_ADDR addr);
237 +extern void debug_print_missing (const char *binary, const char *debug);
239 /* From dwarf2read.c */
241 /* Names for a dwarf2 debugging section. The field NORMAL is the normal
242 Index: gdb-7.10.90.20160211/gdb/testsuite/lib/gdb.exp
243 ===================================================================
244 --- gdb-7.10.90.20160211.orig/gdb/testsuite/lib/gdb.exp 2016-02-15 23:25:00.859346221 +0100
245 +++ gdb-7.10.90.20160211/gdb/testsuite/lib/gdb.exp 2016-02-15 23:26:46.516096395 +0100
246 @@ -1640,6 +1640,16 @@
247 warning "Couldn't set the width to 0."
250 + # Turn off the missing warnings as the testsuite does not expect it.
251 + send_gdb "set build-id-verbose 0\n"
253 + -re "$gdb_prompt $" {
254 + verbose "Disabled the missing debug infos warnings." 2
257 + warning "Could not disable the missing debug infos warnings.."
263 Index: gdb-7.10.90.20160211/gdb/testsuite/lib/mi-support.exp
264 ===================================================================
265 --- gdb-7.10.90.20160211.orig/gdb/testsuite/lib/mi-support.exp 2016-02-15 23:25:00.859346221 +0100
266 +++ gdb-7.10.90.20160211/gdb/testsuite/lib/mi-support.exp 2016-02-15 23:26:46.516096395 +0100
268 warning "Couldn't set the width to 0."
271 + # Turn off the missing warnings as the testsuite does not expect it.
272 + send_gdb "190-gdb-set build-id-verbose 0\n"
274 + -re ".*190-gdb-set build-id-verbose 0\r\n190\\\^done\r\n$mi_gdb_prompt$" {
275 + verbose "Disabled the missing debug infos warnings." 2
278 + warning "Could not disable the missing debug infos warnings.."
282 # Create the new PTY for the inferior process.
283 if { $separate_inferior_pty } {
284 Index: gdb-7.10.90.20160211/gdb/objfiles.h
285 ===================================================================
286 --- gdb-7.10.90.20160211.orig/gdb/objfiles.h 2016-02-15 23:25:00.859346221 +0100
287 +++ gdb-7.10.90.20160211/gdb/objfiles.h 2016-02-15 23:26:06.366811330 +0100
290 #define OBJF_NOT_FILENAME (1 << 6)
292 +/* This file was loaded according to the BUILD_ID_CORE_LOADS rules. */
294 +#define OBJF_BUILD_ID_CORE_LOADED (1 << 12)
296 /* Declarations for functions defined in objfiles.c */
298 extern struct objfile *allocate_objfile (bfd *, const char *name, int);
299 Index: gdb-7.10.90.20160211/gdb/testsuite/gdb.base/corefile.exp
300 ===================================================================
301 --- gdb-7.10.90.20160211.orig/gdb/testsuite/gdb.base/corefile.exp 2016-02-15 23:25:00.859346221 +0100
302 +++ gdb-7.10.90.20160211/gdb/testsuite/gdb.base/corefile.exp 2016-02-15 23:26:55.178157896 +0100
309 +# Test auto-loading of binary files through build-id from the core file.
310 +set buildid [build_id_debug_filename_get $binfile]
311 +set wholetest "binfile found by build-id"
312 +if {$buildid == ""} {
313 + untested "$wholetest (binary has no build-id)"
318 + regsub {\.debug$} $buildid {} buildid
319 + set debugdir [standard_output_file ${testfile}-debugdir]
320 + file delete -force -- $debugdir
321 + file mkdir $debugdir/[file dirname $buildid]
322 + file copy $binfile $debugdir/$buildid
324 + set test "show debug-file-directory"
325 + gdb_test_multiple $test $test {
326 + -re "The directory where separate debug symbols are searched for is \"(.*)\"\\.\r\n$gdb_prompt $" {
327 + set debugdir_orig $expect_out(1,string)
331 + gdb_test_no_output "set debug-file-directory $debugdir:$debugdir_orig" "set debug-file-directory"
332 + gdb_test "show build-id-core-loads" {Whether CORE-FILE loads the build-id associated files automatically is on\.}
333 + gdb_test "core-file $corefile" "\r\nProgram terminated with .*" "core-file without executable"
334 + gdb_test "info files" "Local exec file:\r\n\[ \t\]*`[string_to_regexp $debugdir/$buildid]', file type .*"
337 Index: gdb-7.10.90.20160211/gdb/build-id.c
338 ===================================================================
339 --- gdb-7.10.90.20160211.orig/gdb/build-id.c 2016-02-15 23:25:00.859346221 +0100
340 +++ gdb-7.10.90.20160211/gdb/build-id.c 2016-02-15 23:26:46.516096395 +0100
342 #include "objfiles.h"
343 #include "filenames.h"
346 +#include "gdbcore.h"
348 +#include "observer.h"
349 +#include "elf/external.h"
350 +#include "elf/internal.h"
351 +#include "elf/common.h"
352 +#include "elf-bfd.h"
353 +#include <sys/stat.h>
355 +#define BUILD_ID_VERBOSE_NONE 0
356 +#define BUILD_ID_VERBOSE_FILENAMES 1
357 +#define BUILD_ID_VERBOSE_BINARY_PARSE 2
358 +static int build_id_verbose = BUILD_ID_VERBOSE_FILENAMES;
360 +show_build_id_verbose (struct ui_file *file, int from_tty,
361 + struct cmd_list_element *c, const char *value)
363 + fprintf_filtered (file, _("Verbosity level of the build-id locator is %s.\n"),
366 +/* Locate NT_GNU_BUILD_ID and return its matching debug filename.
367 + FIXME: NOTE decoding should be unified with the BFD core notes decoding. */
369 +static struct bfd_build_id *
370 +build_id_buf_get (bfd *templ, gdb_byte *buf, bfd_size_type size)
375 + while (p < buf + size)
377 + /* FIXME: bad alignment assumption. */
378 + Elf_External_Note *xnp = (Elf_External_Note *) p;
379 + size_t namesz = H_GET_32 (templ, xnp->namesz);
380 + size_t descsz = H_GET_32 (templ, xnp->descsz);
381 + bfd_byte *descdata = (gdb_byte *) xnp->name + BFD_ALIGN (namesz, 4);
383 + if (H_GET_32 (templ, xnp->type) == NT_GNU_BUILD_ID
384 + && namesz == sizeof "GNU"
385 + && memcmp (xnp->name, "GNU", sizeof "GNU") == 0)
387 + size_t size = descsz;
388 + gdb_byte *data = (void *) descdata;
389 + struct bfd_build_id *retval;
391 + retval = xmalloc (sizeof *retval - 1 + size);
392 + retval->size = size;
393 + memcpy (retval->data, data, size);
397 + p = descdata + BFD_ALIGN (descsz, 4);
402 /* See build-id.h. */
404 const struct bfd_build_id *
405 -build_id_bfd_get (bfd *abfd)
406 +build_id_bfd_shdr_get (bfd *abfd)
408 if (!bfd_check_format (abfd, bfd_object))
414 +/* Core files may have missing (corrupt) SHDR but PDHR is correct there.
415 + bfd_elf_bfd_from_remote_memory () has too much overhead by
416 + allocating/reading all the available ELF PT_LOADs. */
418 +static struct bfd_build_id *
419 +build_id_phdr_get (bfd *templ, bfd_vma loadbase, unsigned e_phnum,
420 + Elf_Internal_Phdr *i_phdr)
423 + struct bfd_build_id *retval = NULL;
425 + for (i = 0; i < e_phnum; i++)
426 + if (i_phdr[i].p_type == PT_NOTE && i_phdr[i].p_filesz > 0)
428 + Elf_Internal_Phdr *hdr = &i_phdr[i];
432 + buf = xmalloc (hdr->p_filesz);
433 + err = target_read_memory (loadbase + i_phdr[i].p_vaddr, buf,
436 + retval = build_id_buf_get (templ, buf, hdr->p_filesz);
440 + if (retval != NULL)
446 +/* First we validate the file by reading in the ELF header and checking
447 + the magic number. */
449 +static inline bfd_boolean
450 +elf_file_p (Elf64_External_Ehdr *x_ehdrp64)
452 + gdb_assert (sizeof (Elf64_External_Ehdr) >= sizeof (Elf32_External_Ehdr));
453 + gdb_assert (offsetof (Elf64_External_Ehdr, e_ident)
454 + == offsetof (Elf32_External_Ehdr, e_ident));
455 + gdb_assert (sizeof (((Elf64_External_Ehdr *) 0)->e_ident)
456 + == sizeof (((Elf32_External_Ehdr *) 0)->e_ident));
458 + return ((x_ehdrp64->e_ident[EI_MAG0] == ELFMAG0)
459 + && (x_ehdrp64->e_ident[EI_MAG1] == ELFMAG1)
460 + && (x_ehdrp64->e_ident[EI_MAG2] == ELFMAG2)
461 + && (x_ehdrp64->e_ident[EI_MAG3] == ELFMAG3));
464 +/* Translate an ELF file header in external format into an ELF file header in
465 + internal format. */
467 +#define H_GET_WORD(bfd, ptr) (is64 ? H_GET_64 (bfd, (ptr)) \
468 + : H_GET_32 (bfd, (ptr)))
469 +#define H_GET_SIGNED_WORD(bfd, ptr) (is64 ? H_GET_S64 (bfd, (ptr)) \
470 + : H_GET_S32 (bfd, (ptr)))
473 +elf_swap_ehdr_in (bfd *abfd,
474 + const Elf64_External_Ehdr *src64,
475 + Elf_Internal_Ehdr *dst)
477 + int is64 = bfd_get_arch_size (abfd) == 64;
478 +#define SRC(field) (is64 ? src64->field \
479 + : ((const Elf32_External_Ehdr *) src64)->field)
481 + int signed_vma = get_elf_backend_data (abfd)->sign_extend_vma;
482 + memcpy (dst->e_ident, SRC (e_ident), EI_NIDENT);
483 + dst->e_type = H_GET_16 (abfd, SRC (e_type));
484 + dst->e_machine = H_GET_16 (abfd, SRC (e_machine));
485 + dst->e_version = H_GET_32 (abfd, SRC (e_version));
487 + dst->e_entry = H_GET_SIGNED_WORD (abfd, SRC (e_entry));
489 + dst->e_entry = H_GET_WORD (abfd, SRC (e_entry));
490 + dst->e_phoff = H_GET_WORD (abfd, SRC (e_phoff));
491 + dst->e_shoff = H_GET_WORD (abfd, SRC (e_shoff));
492 + dst->e_flags = H_GET_32 (abfd, SRC (e_flags));
493 + dst->e_ehsize = H_GET_16 (abfd, SRC (e_ehsize));
494 + dst->e_phentsize = H_GET_16 (abfd, SRC (e_phentsize));
495 + dst->e_phnum = H_GET_16 (abfd, SRC (e_phnum));
496 + dst->e_shentsize = H_GET_16 (abfd, SRC (e_shentsize));
497 + dst->e_shnum = H_GET_16 (abfd, SRC (e_shnum));
498 + dst->e_shstrndx = H_GET_16 (abfd, SRC (e_shstrndx));
503 +/* Translate an ELF program header table entry in external format into an
504 + ELF program header table entry in internal format. */
507 +elf_swap_phdr_in (bfd *abfd,
508 + const Elf64_External_Phdr *src64,
509 + Elf_Internal_Phdr *dst)
511 + int is64 = bfd_get_arch_size (abfd) == 64;
512 +#define SRC(field) (is64 ? src64->field \
513 + : ((const Elf32_External_Phdr *) src64)->field)
515 + int signed_vma = get_elf_backend_data (abfd)->sign_extend_vma;
517 + dst->p_type = H_GET_32 (abfd, SRC (p_type));
518 + dst->p_flags = H_GET_32 (abfd, SRC (p_flags));
519 + dst->p_offset = H_GET_WORD (abfd, SRC (p_offset));
522 + dst->p_vaddr = H_GET_SIGNED_WORD (abfd, SRC (p_vaddr));
523 + dst->p_paddr = H_GET_SIGNED_WORD (abfd, SRC (p_paddr));
527 + dst->p_vaddr = H_GET_WORD (abfd, SRC (p_vaddr));
528 + dst->p_paddr = H_GET_WORD (abfd, SRC (p_paddr));
530 + dst->p_filesz = H_GET_WORD (abfd, SRC (p_filesz));
531 + dst->p_memsz = H_GET_WORD (abfd, SRC (p_memsz));
532 + dst->p_align = H_GET_WORD (abfd, SRC (p_align));
537 +#undef H_GET_SIGNED_WORD
540 +static Elf_Internal_Phdr *
541 +elf_get_phdr (bfd *templ, bfd_vma ehdr_vma, unsigned *e_phnum_pointer,
542 + bfd_vma *loadbase_pointer)
544 + /* sizeof (Elf64_External_Ehdr) >= sizeof (Elf32_External_Ehdr) */
545 + Elf64_External_Ehdr x_ehdr64; /* Elf file header, external form */
546 + Elf_Internal_Ehdr i_ehdr; /* Elf file header, internal form */
547 + bfd_size_type x_phdrs_size;
548 + gdb_byte *x_phdrs_ptr;
549 + Elf_Internal_Phdr *i_phdrs;
555 + gdb_assert (templ != NULL);
556 + gdb_assert (sizeof (Elf64_External_Ehdr) >= sizeof (Elf32_External_Ehdr));
558 + /* Read in the ELF header in external format. */
559 + err = target_read_memory (ehdr_vma, (bfd_byte *) &x_ehdr64, sizeof x_ehdr64);
562 + if (build_id_verbose >= BUILD_ID_VERBOSE_BINARY_PARSE)
563 + warning (_("build-id: Error reading ELF header at address 0x%lx"),
564 + (unsigned long) ehdr_vma);
568 + /* Now check to see if we have a valid ELF file, and one that BFD can
569 + make use of. The magic number must match, the address size ('class')
570 + and byte-swapping must match our XVEC entry. */
572 + if (! elf_file_p (&x_ehdr64)
573 + || x_ehdr64.e_ident[EI_VERSION] != EV_CURRENT
574 + || !((bfd_get_arch_size (templ) == 64
575 + && x_ehdr64.e_ident[EI_CLASS] == ELFCLASS64)
576 + || (bfd_get_arch_size (templ) == 32
577 + && x_ehdr64.e_ident[EI_CLASS] == ELFCLASS32)))
579 + if (build_id_verbose >= BUILD_ID_VERBOSE_BINARY_PARSE)
580 + warning (_("build-id: Unrecognized ELF header at address 0x%lx"),
581 + (unsigned long) ehdr_vma);
585 + /* Check that file's byte order matches xvec's */
586 + switch (x_ehdr64.e_ident[EI_DATA])
588 + case ELFDATA2MSB: /* Big-endian */
589 + if (! bfd_header_big_endian (templ))
591 + if (build_id_verbose >= BUILD_ID_VERBOSE_BINARY_PARSE)
592 + warning (_("build-id: Unrecognized "
593 + "big-endian ELF header at address 0x%lx"),
594 + (unsigned long) ehdr_vma);
598 + case ELFDATA2LSB: /* Little-endian */
599 + if (! bfd_header_little_endian (templ))
601 + if (build_id_verbose >= BUILD_ID_VERBOSE_BINARY_PARSE)
602 + warning (_("build-id: Unrecognized "
603 + "little-endian ELF header at address 0x%lx"),
604 + (unsigned long) ehdr_vma);
608 + case ELFDATANONE: /* No data encoding specified */
609 + default: /* Unknown data encoding specified */
610 + if (build_id_verbose >= BUILD_ID_VERBOSE_BINARY_PARSE)
611 + warning (_("build-id: Unrecognized "
612 + "ELF header endianity at address 0x%lx"),
613 + (unsigned long) ehdr_vma);
617 + elf_swap_ehdr_in (templ, &x_ehdr64, &i_ehdr);
619 + /* The file header tells where to find the program headers.
620 + These are what we use to actually choose what to read. */
622 + if (i_ehdr.e_phentsize != (bfd_get_arch_size (templ) == 64
623 + ? sizeof (Elf64_External_Phdr)
624 + : sizeof (Elf32_External_Phdr))
625 + || i_ehdr.e_phnum == 0)
627 + if (build_id_verbose >= BUILD_ID_VERBOSE_BINARY_PARSE)
628 + warning (_("build-id: Invalid ELF program headers from the ELF header "
629 + "at address 0x%lx"), (unsigned long) ehdr_vma);
633 + x_phdrs_size = (bfd_get_arch_size (templ) == 64 ? sizeof (Elf64_External_Phdr)
634 + : sizeof (Elf32_External_Phdr));
636 + i_phdrs = xmalloc (i_ehdr.e_phnum * (sizeof *i_phdrs + x_phdrs_size));
637 + x_phdrs_ptr = (void *) &i_phdrs[i_ehdr.e_phnum];
638 + err = target_read_memory (ehdr_vma + i_ehdr.e_phoff, (bfd_byte *) x_phdrs_ptr,
639 + i_ehdr.e_phnum * x_phdrs_size);
643 + if (build_id_verbose >= BUILD_ID_VERBOSE_BINARY_PARSE)
644 + warning (_("build-id: Error reading "
645 + "ELF program headers at address 0x%lx"),
646 + (unsigned long) (ehdr_vma + i_ehdr.e_phoff));
650 + loadbase = ehdr_vma;
652 + for (i = 0; i < i_ehdr.e_phnum; ++i)
654 + elf_swap_phdr_in (templ, (Elf64_External_Phdr *)
655 + (x_phdrs_ptr + i * x_phdrs_size), &i_phdrs[i]);
656 + /* IA-64 vDSO may have two mappings for one segment, where one mapping
657 + is executable only, and one is read only. We must not use the
658 + executable one (PF_R is the first one, PF_X the second one). */
659 + if (i_phdrs[i].p_type == PT_LOAD && (i_phdrs[i].p_flags & PF_R))
661 + /* Only the first PT_LOAD segment indicates the file bias.
662 + Next segments may have P_VADDR arbitrarily higher.
663 + If the first segment has P_VADDR zero any next segment must not
664 + confuse us, the first one sets LOADBASE certainly enough. */
665 + if (!loadbase_set && i_phdrs[i].p_offset == 0)
667 + loadbase = ehdr_vma - i_phdrs[i].p_vaddr;
673 + if (build_id_verbose >= BUILD_ID_VERBOSE_BINARY_PARSE)
674 + warning (_("build-id: Found ELF header at address 0x%lx, loadbase 0x%lx"),
675 + (unsigned long) ehdr_vma, (unsigned long) loadbase);
677 + *e_phnum_pointer = i_ehdr.e_phnum;
678 + *loadbase_pointer = loadbase;
682 +/* BUILD_ID_ADDR_GET gets ADDR located somewhere in the object.
683 + Find the first section before ADDR containing an ELF header.
684 + We rely on the fact the sections from multiple files do not mix.
685 + FIXME: We should check ADDR is contained _inside_ the section with possibly
686 + missing content (P_FILESZ < P_MEMSZ). These omitted sections are currently
687 + hidden by _BFD_ELF_MAKE_SECTION_FROM_PHDR. */
689 +static CORE_ADDR build_id_addr;
690 +struct build_id_addr_sect
692 + struct build_id_addr_sect *next;
695 +static struct build_id_addr_sect *build_id_addr_sect;
697 +static void build_id_addr_candidate (bfd *abfd, asection *sect, void *obj)
699 + if (build_id_addr >= bfd_section_vma (abfd, sect))
701 + struct build_id_addr_sect *candidate;
703 + candidate = xmalloc (sizeof *candidate);
704 + candidate->next = build_id_addr_sect;
705 + build_id_addr_sect = candidate;
706 + candidate->sect = sect;
710 +struct bfd_build_id *
711 +build_id_addr_get (CORE_ADDR addr)
713 + struct build_id_addr_sect *candidate;
714 + struct bfd_build_id *retval = NULL;
715 + Elf_Internal_Phdr *i_phdr = NULL;
716 + bfd_vma loadbase = 0;
717 + unsigned e_phnum = 0;
719 + if (core_bfd == NULL)
722 + build_id_addr = addr;
723 + gdb_assert (build_id_addr_sect == NULL);
724 + bfd_map_over_sections (core_bfd, build_id_addr_candidate, NULL);
726 + /* Sections are sorted in the high-to-low VMAs order.
727 + Stop the search on the first ELF header we find.
728 + Do not continue the search even if it does not contain NT_GNU_BUILD_ID. */
730 + for (candidate = build_id_addr_sect; candidate != NULL;
731 + candidate = candidate->next)
733 + i_phdr = elf_get_phdr (core_bfd,
734 + bfd_section_vma (core_bfd, candidate->sect),
735 + &e_phnum, &loadbase);
736 + if (i_phdr != NULL)
740 + if (i_phdr != NULL)
742 + retval = build_id_phdr_get (core_bfd, loadbase, e_phnum, i_phdr);
746 + while (build_id_addr_sect != NULL)
748 + candidate = build_id_addr_sect;
749 + build_id_addr_sect = candidate->next;
756 /* See build-id.h. */
760 const struct bfd_build_id *found;
763 - found = build_id_bfd_get (abfd);
764 + found = build_id_bfd_shdr_get (abfd);
767 warning (_("File \"%s\" has no build-id, file skipped"),
773 +link_resolve (const char *symlink, int level)
775 + char buf[PATH_MAX + 1], *target, *retval;
779 + return xstrdup (symlink);
781 + got = readlink (symlink, buf, sizeof (buf));
782 + if (got < 0 || got >= sizeof (buf))
783 + return xstrdup (symlink);
786 + if (IS_ABSOLUTE_PATH (buf))
787 + target = xstrdup (buf);
790 + char *dir = ldirname (symlink);
793 + return xstrdup (symlink);
794 + target = xstrprintf ("%s"
795 +#ifndef HAVE_DOS_BASED_FILE_SYSTEM
797 +#else /* HAVE_DOS_BASED_FILE_SYSTEM */
799 +#endif /* HAVE_DOS_BASED_FILE_SYSTEM */
803 + retval = link_resolve (target, level + 1);
808 /* See build-id.h. */
811 -build_id_to_debug_bfd (size_t build_id_len, const bfd_byte *build_id)
812 +build_id_to_debug_bfd (size_t build_id_len, const bfd_byte *build_id,
813 + char **link_return, int add_debug_suffix)
815 - char *link, *debugdir;
816 + char *link, *debugdir, *link_all = NULL;
817 VEC (char_ptr) *debugdir_vec;
818 struct cleanup *back_to;
823 /* DEBUG_FILE_DIRECTORY/.build-id/ab/cdef */
824 - alloc_len = (strlen (debug_file_directory)
825 - + (sizeof "/.build-id/" - 1) + 1
826 - + 2 * build_id_len + (sizeof ".debug" - 1) + 1);
827 - link = (char *) alloca (alloc_len);
828 + link = xmalloc (strlen (debug_file_directory) + 2 * build_id_len + 50);
830 /* Keep backward compatibility so that DEBUG_FILE_DIRECTORY being "" will
831 cause "/.build-id/..." lookups. */
833 size_t debugdir_len = strlen (debugdir);
834 const gdb_byte *data = build_id;
835 size_t size = build_id_len;
837 char *filename = NULL;
838 struct cleanup *inner;
840 + struct stat statbuf_trash;
841 + /* Initialize it just to avoid a GCC false warning. */
842 + char *s, *link0 = NULL, *link0_resolved;
844 memcpy (link, debugdir, debugdir_len);
845 s = &link[debugdir_len];
846 @@ -110,47 +544,281 @@
849 s += sprintf (s, "%02x", (unsigned) *data++);
850 - strcpy (s, ".debug");
851 + for (seqno = 0;; seqno++)
855 - /* lrealpath() is expensive even for the usually non-existent files. */
856 - if (access (link, F_OK) == 0)
857 - filename = lrealpath (link);
860 + /* There can be multiple build-id symlinks pointing to real files
861 + with the same build-id (such as hard links). Some of the real
862 + files may not be installed. */
864 + s2 = s + sprintf (s, ".%u", seqno);
869 + if (add_debug_suffix)
870 + strcpy (s2, ".debug");
876 + /* If none of the real files is found report as missing file
877 + always the non-.%u-suffixed file. */
878 + link0 = xstrdup (link);
881 + /* `access' automatically dereferences LINK. */
882 + if (lstat (link, &statbuf_trash) != 0)
884 + /* Stop increasing SEQNO. */
888 + filename = lrealpath (link);
889 + if (filename == NULL)
892 + /* We expect to be silent on the non-existing files. */
893 + inner = make_cleanup (xfree, filename);
894 + abfd = gdb_bfd_open (filename, gnutarget, -1);
895 + do_cleanups (inner);
897 - if (filename == NULL)
902 - /* We expect to be silent on the non-existing files. */
903 - inner = make_cleanup (xfree, filename);
904 - abfd = gdb_bfd_open (filename, gnutarget, -1);
905 - do_cleanups (inner);
906 + if (build_id_verify (abfd, build_id_len, build_id))
911 + gdb_bfd_unref (abfd);
914 - if (build_id_verify (abfd, build_id_len, build_id))
919 - gdb_bfd_unref (abfd);
921 + if (filename != NULL)
923 + /* LINK_ALL is not used below in this non-NULL FILENAME case. */
928 + /* If the symlink has target request to install the target.
929 + BASE-debuginfo.rpm contains the symlink but BASE.rpm may be missing.
930 + https://bugzilla.redhat.com/show_bug.cgi?id=981154 */
931 + link0_resolved = link_resolve (link0, 0);
934 + if (link_all == NULL)
935 + link_all = link0_resolved;
938 + size_t len_orig = strlen (link_all);
940 + link_all = xrealloc (link_all,
941 + len_orig + 1 + strlen (link0_resolved) + 1);
943 + /* Use whitespace instead of DIRNAME_SEPARATOR to be compatible with
944 + its possible use as an argument for installation command. */
945 + link_all[len_orig] = ' ';
947 + strcpy (&link_all[len_orig + 1], link0_resolved);
948 + xfree (link0_resolved);
952 + if (link_return != NULL)
956 + *link_return = link;
961 + *link_return = link_all;
968 do_cleanups (back_to);
973 +build_id_to_filename (const struct bfd_build_id *build_id, char **link_return)
978 + abfd = build_id_to_debug_bfd (build_id->size, build_id->data, link_return, 0);
982 + result = xstrdup (bfd_get_filename (abfd));
983 + gdb_bfd_unref (abfd);
987 +/* This MISSING_FILEPAIR_HASH tracker is used only for the duplicite messages
988 + Try to install the hash file ...
991 +struct missing_filepair
998 +static struct htab *missing_filepair_hash;
999 +static struct obstack missing_filepair_obstack;
1002 +missing_filepair_xcalloc (size_t nmemb, size_t nmemb_size)
1005 + size_t size = nmemb * nmemb_size;
1007 + retval = obstack_alloc (&missing_filepair_obstack, size);
1008 + memset (retval, 0, size);
1013 +missing_filepair_hash_func (const struct missing_filepair *elem)
1015 + hashval_t retval = 0;
1017 + retval ^= htab_hash_string (elem->binary);
1018 + if (elem->debug != NULL)
1019 + retval ^= htab_hash_string (elem->debug);
1025 +missing_filepair_eq (const struct missing_filepair *elem1,
1026 + const struct missing_filepair *elem2)
1028 + return strcmp (elem1->binary, elem2->binary) == 0
1029 + && ((elem1->debug == NULL) == (elem2->debug == NULL))
1030 + && (elem1->debug == NULL || strcmp (elem1->debug, elem2->debug) == 0);
1034 +missing_filepair_change (void)
1036 + if (missing_filepair_hash != NULL)
1038 + obstack_free (&missing_filepair_obstack, NULL);
1039 + /* All their memory came just from missing_filepair_OBSTACK. */
1040 + missing_filepair_hash = NULL;
1045 +debug_print_executable_changed (void)
1047 + missing_filepair_change ();
1050 +/* Notify user the file BINARY with (possibly NULL) associated separate debug
1051 + information file DEBUG is missing. DEBUG may or may not be the build-id
1052 + file such as would be:
1053 + /usr/lib/debug/.build-id/dd/b1d2ce632721c47bb9e8679f369e2295ce71be.debug
1057 +debug_print_missing (const char *binary, const char *debug)
1059 + size_t binary_len0 = strlen (binary) + 1;
1060 + size_t debug_len0 = debug ? strlen (debug) + 1 : 0;
1061 + struct missing_filepair missing_filepair_find;
1062 + struct missing_filepair *missing_filepair;
1063 + struct missing_filepair **slot;
1065 + if (build_id_verbose < BUILD_ID_VERBOSE_FILENAMES)
1068 + if (missing_filepair_hash == NULL)
1070 + obstack_init (&missing_filepair_obstack);
1071 + missing_filepair_hash = htab_create_alloc (64,
1072 + (hashval_t (*) (const void *)) missing_filepair_hash_func,
1073 + (int (*) (const void *, const void *)) missing_filepair_eq, NULL,
1074 + missing_filepair_xcalloc, NULL);
1077 + /* Use MISSING_FILEPAIR_FIND first instead of calling obstack_alloc with
1078 + obstack_free in the case of a (rare) match. The problem is ALLOC_F for
1079 + MISSING_FILEPAIR_HASH allocates from MISSING_FILEPAIR_OBSTACK maintenance
1080 + structures for MISSING_FILEPAIR_HASH. Calling obstack_free would possibly
1081 + not to free only MISSING_FILEPAIR but also some such structures (allocated
1082 + during the htab_find_slot call). */
1084 + missing_filepair_find.binary = (char *) binary;
1085 + missing_filepair_find.debug = (char *) debug;
1086 + slot = (struct missing_filepair **) htab_find_slot (missing_filepair_hash,
1087 + &missing_filepair_find,
1090 + /* While it may be still printed duplicitely with the missing debuginfo file
1091 + * it is due to once printing about the binary file build-id link and once
1092 + * about the .debug file build-id link as both the build-id symlinks are
1093 + * located in the debuginfo package. */
1095 + if (*slot != NULL)
1098 + missing_filepair = obstack_alloc (&missing_filepair_obstack,
1099 + sizeof (*missing_filepair) - 1
1100 + + binary_len0 + debug_len0);
1101 + missing_filepair->binary = missing_filepair->data;
1102 + memcpy (missing_filepair->binary, binary, binary_len0);
1103 + if (debug != NULL)
1105 + missing_filepair->debug = missing_filepair->binary + binary_len0;
1106 + memcpy (missing_filepair->debug, debug, debug_len0);
1109 + missing_filepair->debug = NULL;
1111 + *slot = missing_filepair;
1113 + /* We do not collect and flush these messages as each such message
1114 + already requires its own separate lines. */
1116 + fprintf_unfiltered (gdb_stdlog,
1117 + _("Missing separate debuginfo for %s\n"), binary);
1118 + if (debug != NULL)
1119 + fprintf_unfiltered (gdb_stdlog, _("Try to install the hash file %s\n"),
1123 /* See build-id.h. */
1126 -find_separate_debug_file_by_buildid (struct objfile *objfile)
1127 +find_separate_debug_file_by_buildid (struct objfile *objfile,
1128 + char **build_id_filename_return)
1130 const struct bfd_build_id *build_id;
1132 - build_id = build_id_bfd_get (objfile->obfd);
1133 + if (build_id_filename_return)
1134 + *build_id_filename_return = NULL;
1136 + build_id = build_id_bfd_shdr_get (objfile->obfd);
1137 if (build_id != NULL)
1141 - abfd = build_id_to_debug_bfd (build_id->size, build_id->data);
1142 + abfd = build_id_to_debug_bfd (build_id->size, build_id->data,
1143 + build_id_filename_return, 1);
1144 /* Prevent looping on a stripped .debug file. */
1146 && filename_cmp (bfd_get_filename (abfd),
1147 @@ -170,3 +838,21 @@
1152 +extern void _initialize_build_id (void);
1155 +_initialize_build_id (void)
1157 + add_setshow_zinteger_cmd ("build-id-verbose", no_class, &build_id_verbose,
1159 +Set debugging level of the build-id locator."), _("\
1160 +Show debugging level of the build-id locator."), _("\
1161 +Level 1 (default) enables printing the missing debug filenames,\n\
1162 +level 2 also prints the parsing of binaries to find the identificators."),
1164 + show_build_id_verbose,
1165 + &setlist, &showlist);
1167 + observer_attach_executable_changed (debug_print_executable_changed);
1169 Index: gdb-7.10.90.20160211/gdb/build-id.h
1170 ===================================================================
1171 --- gdb-7.10.90.20160211.orig/gdb/build-id.h 2016-02-15 23:25:00.859346221 +0100
1172 +++ gdb-7.10.90.20160211/gdb/build-id.h 2016-02-15 23:26:06.368811345 +0100
1177 -/* Locate NT_GNU_BUILD_ID from ABFD and return its content. */
1178 +/* Separate debuginfo files have corrupted PHDR but SHDR is correct there.
1179 + Locate NT_GNU_BUILD_ID from ABFD and return its content. */
1181 -extern const struct bfd_build_id *build_id_bfd_get (bfd *abfd);
1182 +extern const struct bfd_build_id *build_id_bfd_shdr_get (bfd *abfd);
1184 /* Return true if ABFD has NT_GNU_BUILD_ID matching the CHECK value.
1185 Otherwise, issue a warning and return false. */
1189 extern bfd *build_id_to_debug_bfd (size_t build_id_len,
1190 - const bfd_byte *build_id);
1191 + const bfd_byte *build_id, char **link_return,
1192 + int add_debug_suffix);
1194 +extern char *build_id_to_filename (const struct bfd_build_id *build_id,
1195 + char **link_return);
1197 /* Find the separate debug file for OBJFILE, by using the build-id
1198 associated with OBJFILE's BFD. If successful, returns a malloc'd
1199 file name for the separate debug file. The caller must free this.
1200 Otherwise, returns NULL. */
1202 -extern char *find_separate_debug_file_by_buildid (struct objfile *objfile);
1203 +extern char *find_separate_debug_file_by_buildid (struct objfile *objfile,
1204 + char **build_id_filename_return);
1206 #endif /* BUILD_ID_H */
1207 Index: gdb-7.10.90.20160211/gdb/dwarf2read.c
1208 ===================================================================
1209 --- gdb-7.10.90.20160211.orig/gdb/dwarf2read.c 2016-02-15 23:25:36.461599001 +0100
1210 +++ gdb-7.10.90.20160211/gdb/dwarf2read.c 2016-02-15 23:26:06.373811380 +0100
1211 @@ -2516,7 +2516,7 @@
1214 if (dwz_bfd == NULL)
1215 - dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
1216 + dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid, NULL, 1);
1218 if (dwz_bfd == NULL)
1219 error (_("could not find '.gnu_debugaltlink' file for %s"),
1220 Index: gdb-7.10.90.20160211/gdb/python/py-objfile.c
1221 ===================================================================
1222 --- gdb-7.10.90.20160211.orig/gdb/python/py-objfile.c 2016-02-15 23:25:00.859346221 +0100
1223 +++ gdb-7.10.90.20160211/gdb/python/py-objfile.c 2016-02-15 23:26:06.373811380 +0100
1228 - build_id = build_id_bfd_get (objfile->obfd);
1229 + build_id = build_id_bfd_shdr_get (objfile->obfd);
1231 CATCH (except, RETURN_MASK_ALL)
1234 /* Don't return separate debug files. */
1235 if (objfile->separate_debug_objfile_backlink != NULL)
1237 - obfd_build_id = build_id_bfd_get (objfile->obfd);
1238 + obfd_build_id = build_id_bfd_shdr_get (objfile->obfd);
1239 if (obfd_build_id == NULL)
1241 if (objfpy_build_id_matches (obfd_build_id, build_id))
1242 Index: gdb-7.10.90.20160211/gdb/coffread.c
1243 ===================================================================
1244 --- gdb-7.10.90.20160211.orig/gdb/coffread.c 2016-02-15 23:25:00.859346221 +0100
1245 +++ gdb-7.10.90.20160211/gdb/coffread.c 2016-02-15 23:26:06.374811387 +0100
1250 - debugfile = find_separate_debug_file_by_buildid (objfile);
1251 + debugfile = find_separate_debug_file_by_buildid (objfile, NULL);
1253 if (debugfile == NULL)
1254 debugfile = find_separate_debug_file_by_debuglink (objfile);