1 Index: gdb-7.99.90.20170420/gdb/corelow.c
2 ===================================================================
3 --- gdb-7.99.90.20170420.orig/gdb/corelow.c 2017-04-20 23:00:35.415584426 +0200
4 +++ gdb-7.99.90.20170420/gdb/corelow.c 2017-04-20 23:00:43.358629183 +0200
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,
56 + symfile_add_flag (!from_tty ? 0 : SYMFILE_VERBOSE));
57 + if (symfile_objfile != NULL)
58 + symfile_objfile->flags |= OBJF_BUILD_ID_CORE_LOADED;
61 + debug_print_missing (_("the main executable file"), build_id_filename);
63 + do_cleanups (back_to);
65 + /* No automatic SOLIB_ADD as the libraries would get read twice. */
68 /* This routine opens and sets up the core file bfd. */
72 switch_to_thread (thread->ptid);
75 + /* Find the build_id identifiers. If it gets executed after
76 + POST_CREATE_INFERIOR we would clash with asking to discard the already
77 + loaded VDSO symbols. If it gets executed before bfd_map_over_sections
78 + INFERIOR_PTID is still not set and libthread_db initialization crashes on
79 + PID == 0 in ps_pglobal_lookup. */
80 + if (build_id_core_loads != 0)
81 + build_id_locate_exec (from_tty);
83 post_create_inferior (&core_ops, from_tty);
85 /* Now go through the target stack looking for threads since there
86 @@ -1079,4 +1139,11 @@
89 add_target_with_completer (&core_ops, filename_completer);
91 + add_setshow_boolean_cmd ("build-id-core-loads", class_files,
92 + &build_id_core_loads, _("\
93 +Set whether CORE-FILE loads the build-id associated files automatically."), _("\
94 +Show whether CORE-FILE loads the build-id associated files automatically."),
96 + &setlist, &showlist);
98 Index: gdb-7.99.90.20170420/gdb/doc/gdb.texinfo
99 ===================================================================
100 --- gdb-7.99.90.20170420.orig/gdb/doc/gdb.texinfo 2017-04-20 23:00:35.415584426 +0200
101 +++ gdb-7.99.90.20170420/gdb/doc/gdb.texinfo 2017-04-20 23:00:43.366629228 +0200
102 @@ -18987,6 +18987,27 @@
106 +You can also adjust the current verbosity of the @dfn{build id} locating.
110 +@kindex set build-id-verbose
111 +@item set build-id-verbose 0
112 +No additional messages are printed.
114 +@item set build-id-verbose 1
115 +Missing separate debug filenames are printed.
117 +@item set build-id-verbose 2
118 +Missing separate debug filenames are printed and also all the parsing of the
119 +binaries to find their @dfn{build id} content is printed.
121 +@kindex show build-id-verbose
122 +@item show build-id-verbose
123 +Show the current verbosity value for the @dfn{build id} content locating.
127 @cindex @code{.gnu_debuglink} sections
128 @cindex debug link sections
129 A debug link is a special section of the executable file named
130 Index: gdb-7.99.90.20170420/gdb/solib-svr4.c
131 ===================================================================
132 --- gdb-7.99.90.20170420.orig/gdb/solib-svr4.c 2017-04-20 23:00:35.415584426 +0200
133 +++ gdb-7.99.90.20170420/gdb/solib-svr4.c 2017-04-20 23:00:43.367629234 +0200
138 +#include "build-id.h"
140 static struct link_map_offsets *svr4_fetch_link_map_offsets (void);
141 static int svr4_have_link_map_offsets (void);
142 @@ -1420,9 +1421,52 @@
146 - strncpy (newobj->so_name, buffer, SO_NAME_MAX_PATH_SIZE - 1);
147 - newobj->so_name[SO_NAME_MAX_PATH_SIZE - 1] = '\0';
148 - strcpy (newobj->so_original_name, newobj->so_name);
150 + struct bfd_build_id *build_id;
152 + strncpy (newobj->so_original_name, buffer, SO_NAME_MAX_PATH_SIZE - 1);
153 + newobj->so_original_name[SO_NAME_MAX_PATH_SIZE - 1] = '\0';
154 + /* May get overwritten below. */
155 + strcpy (newobj->so_name, newobj->so_original_name);
157 + build_id = build_id_addr_get (newobj->lm_info->l_ld);
158 + if (build_id != NULL)
160 + char *name, *build_id_filename;
162 + /* Missing the build-id matching separate debug info file
163 + would be handled while SO_NAME gets loaded. */
164 + name = build_id_to_filename (build_id, &build_id_filename);
167 + strncpy (newobj->so_name, name, SO_NAME_MAX_PATH_SIZE - 1);
168 + newobj->so_name[SO_NAME_MAX_PATH_SIZE - 1] = '\0';
173 + debug_print_missing (newobj->so_name, build_id_filename);
175 + /* In the case the main executable was found according to
176 + its build-id (from a core file) prevent loading
177 + a different build of a library with accidentally the
180 + It suppresses bogus backtraces (and prints "??" there
181 + instead) if the on-disk files no longer match the
182 + running program version. */
184 + if (symfile_objfile != NULL
185 + && (symfile_objfile->flags
186 + & OBJF_BUILD_ID_CORE_LOADED) != 0)
187 + newobj->so_name[0] = 0;
190 + xfree (build_id_filename);
197 /* If this entry has no name, or its name matches the name
198 Index: gdb-7.99.90.20170420/gdb/elfread.c
199 ===================================================================
200 --- gdb-7.99.90.20170420.orig/gdb/elfread.c 2017-04-20 23:00:35.415584426 +0200
201 +++ gdb-7.99.90.20170420/gdb/elfread.c 2017-04-20 23:00:43.367629234 +0200
202 @@ -1265,8 +1265,9 @@
203 && objfile->separate_debug_objfile == NULL
204 && objfile->separate_debug_objfile_backlink == NULL)
206 + gdb::unique_xmalloc_ptr<char> build_id_filename;
207 gdb::unique_xmalloc_ptr<char> debugfile
208 - (find_separate_debug_file_by_buildid (objfile));
209 + (find_separate_debug_file_by_buildid (objfile, &build_id_filename));
211 if (debugfile == NULL)
212 debugfile.reset (find_separate_debug_file_by_debuglink (objfile));
213 @@ -1278,6 +1279,10 @@
214 symbol_file_add_separate (abfd.get (), debugfile.get (),
215 symfile_flags, objfile);
217 + /* Check if any separate debug info has been extracted out. */
218 + else if (bfd_get_section_by_name (objfile->obfd, ".gnu_debuglink")
220 + debug_print_missing (objfile_name (objfile), build_id_filename.get ());
224 Index: gdb-7.99.90.20170420/gdb/symfile.h
225 ===================================================================
226 --- gdb-7.99.90.20170420.orig/gdb/symfile.h 2017-04-20 23:00:35.415584426 +0200
227 +++ gdb-7.99.90.20170420/gdb/symfile.h 2017-04-20 23:00:43.368629240 +0200
229 void map_symbol_filenames (symbol_filename_ftype *fun, void *data,
232 +/* build-id support. */
233 +extern struct bfd_build_id *build_id_addr_get (CORE_ADDR addr);
234 +extern void debug_print_missing (const char *binary, const char *debug);
236 /* From dwarf2read.c */
238 /* Names for a dwarf2 debugging section. The field NORMAL is the normal
239 Index: gdb-7.99.90.20170420/gdb/testsuite/lib/gdb.exp
240 ===================================================================
241 --- gdb-7.99.90.20170420.orig/gdb/testsuite/lib/gdb.exp 2017-04-20 23:00:35.415584426 +0200
242 +++ gdb-7.99.90.20170420/gdb/testsuite/lib/gdb.exp 2017-04-20 23:00:43.369629245 +0200
243 @@ -1646,6 +1646,16 @@
244 warning "Couldn't set the width to 0."
247 + # Turn off the missing warnings as the testsuite does not expect it.
248 + send_gdb "set build-id-verbose 0\n"
250 + -re "$gdb_prompt $" {
251 + verbose "Disabled the missing debug infos warnings." 2
254 + warning "Could not disable the missing debug infos warnings.."
260 Index: gdb-7.99.90.20170420/gdb/testsuite/lib/mi-support.exp
261 ===================================================================
262 --- gdb-7.99.90.20170420.orig/gdb/testsuite/lib/mi-support.exp 2017-04-20 23:00:35.415584426 +0200
263 +++ gdb-7.99.90.20170420/gdb/testsuite/lib/mi-support.exp 2017-04-20 23:00:43.369629245 +0200
265 warning "Couldn't set the width to 0."
268 + # Turn off the missing warnings as the testsuite does not expect it.
269 + send_gdb "190-gdb-set build-id-verbose 0\n"
271 + -re ".*190-gdb-set build-id-verbose 0\r\n190\\\^done\r\n$mi_gdb_prompt$" {
272 + verbose "Disabled the missing debug infos warnings." 2
275 + warning "Could not disable the missing debug infos warnings.."
279 if { $separate_inferior_pty } {
280 mi_create_inferior_pty
281 Index: gdb-7.99.90.20170420/gdb/testsuite/gdb.base/new-ui-pending-input.exp
282 ===================================================================
283 --- gdb-7.99.90.20170420.orig/gdb/testsuite/gdb.base/new-ui-pending-input.exp 2017-04-20 23:00:35.415584426 +0200
284 +++ gdb-7.99.90.20170420/gdb/testsuite/gdb.base/new-ui-pending-input.exp 2017-04-20 23:00:43.369629245 +0200
287 append options " -iex \"set height 0\""
288 append options " -iex \"set width 0\""
289 + append options " -iex \"set build-id-verbose 0\""
290 append options " -iex \"new-ui console $extra_tty_name\""
291 append options " -ex \"b $bpline\""
292 append options " -ex \"run\""
293 Index: gdb-7.99.90.20170420/gdb/objfiles.h
294 ===================================================================
295 --- gdb-7.99.90.20170420.orig/gdb/objfiles.h 2017-04-20 23:00:35.415584426 +0200
296 +++ gdb-7.99.90.20170420/gdb/objfiles.h 2017-04-20 23:00:43.370629251 +0200
301 +/* This file was loaded according to the BUILD_ID_CORE_LOADS rules. */
303 +#define OBJF_BUILD_ID_CORE_LOADED static_cast<enum objfile_flag>(1 << 12)
305 /* Declarations for functions defined in objfiles.c */
307 extern struct objfile *allocate_objfile (bfd *, const char *name,
308 Index: gdb-7.99.90.20170420/gdb/testsuite/gdb.base/corefile.exp
309 ===================================================================
310 --- gdb-7.99.90.20170420.orig/gdb/testsuite/gdb.base/corefile.exp 2017-04-20 23:00:35.415584426 +0200
311 +++ gdb-7.99.90.20170420/gdb/testsuite/gdb.base/corefile.exp 2017-04-20 23:00:43.370629251 +0200
318 +# Test auto-loading of binary files through build-id from the core file.
319 +set buildid [build_id_debug_filename_get $binfile]
320 +set wholetest "binfile found by build-id"
321 +if {$buildid == ""} {
322 + untested "$wholetest (binary has no build-id)"
327 + regsub {\.debug$} $buildid {} buildid
328 + set debugdir [standard_output_file ${testfile}-debugdir]
329 + file delete -force -- $debugdir
330 + file mkdir $debugdir/[file dirname $buildid]
331 + file copy $binfile $debugdir/$buildid
333 + set test "show debug-file-directory"
334 + gdb_test_multiple $test $test {
335 + -re "The directory where separate debug symbols are searched for is \"(.*)\"\\.\r\n$gdb_prompt $" {
336 + set debugdir_orig $expect_out(1,string)
340 + gdb_test_no_output "set debug-file-directory $debugdir:$debugdir_orig" "set debug-file-directory"
341 + gdb_test "show build-id-core-loads" {Whether CORE-FILE loads the build-id associated files automatically is on\.}
342 + gdb_test "core-file $corefile" "\r\nProgram terminated with .*" "core-file without executable"
343 + gdb_test "info files" "Local exec file:\r\n\[ \t\]*`[string_to_regexp $debugdir/$buildid]', file type .*"
346 Index: gdb-7.99.90.20170420/gdb/build-id.c
347 ===================================================================
348 --- gdb-7.99.90.20170420.orig/gdb/build-id.c 2017-04-20 23:00:35.415584426 +0200
349 +++ gdb-7.99.90.20170420/gdb/build-id.c 2017-04-20 23:03:00.060399474 +0200
351 #include "objfiles.h"
352 #include "filenames.h"
355 +#include "gdbcore.h"
357 +#include "observer.h"
358 +#include "elf/external.h"
359 +#include "elf/internal.h"
360 +#include "elf/common.h"
361 +#include "elf-bfd.h"
362 +#include <sys/stat.h>
364 +#define BUILD_ID_VERBOSE_NONE 0
365 +#define BUILD_ID_VERBOSE_FILENAMES 1
366 +#define BUILD_ID_VERBOSE_BINARY_PARSE 2
367 +static int build_id_verbose = BUILD_ID_VERBOSE_FILENAMES;
369 +show_build_id_verbose (struct ui_file *file, int from_tty,
370 + struct cmd_list_element *c, const char *value)
372 + fprintf_filtered (file, _("Verbosity level of the build-id locator is %s.\n"),
375 +/* Locate NT_GNU_BUILD_ID and return its matching debug filename.
376 + FIXME: NOTE decoding should be unified with the BFD core notes decoding. */
378 +static struct bfd_build_id *
379 +build_id_buf_get (bfd *templ, gdb_byte *buf, bfd_size_type size)
384 + while (p < buf + size)
386 + /* FIXME: bad alignment assumption. */
387 + Elf_External_Note *xnp = (Elf_External_Note *) p;
388 + size_t namesz = H_GET_32 (templ, xnp->namesz);
389 + size_t descsz = H_GET_32 (templ, xnp->descsz);
390 + bfd_byte *descdata = (gdb_byte *) xnp->name + BFD_ALIGN (namesz, 4);
392 + if (H_GET_32 (templ, xnp->type) == NT_GNU_BUILD_ID
393 + && namesz == sizeof "GNU"
394 + && memcmp (xnp->name, "GNU", sizeof "GNU") == 0)
396 + size_t size = descsz;
397 + gdb_byte *data = (gdb_byte *) descdata;
398 + struct bfd_build_id *retval;
400 + retval = (struct bfd_build_id *) xmalloc (sizeof *retval - 1 + size);
401 + retval->size = size;
402 + memcpy (retval->data, data, size);
406 + p = descdata + BFD_ALIGN (descsz, 4);
411 /* See build-id.h. */
413 const struct bfd_build_id *
414 -build_id_bfd_get (bfd *abfd)
415 +build_id_bfd_shdr_get (bfd *abfd)
417 if (!bfd_check_format (abfd, bfd_object))
423 +/* Core files may have missing (corrupt) SHDR but PDHR is correct there.
424 + bfd_elf_bfd_from_remote_memory () has too much overhead by
425 + allocating/reading all the available ELF PT_LOADs. */
427 +static struct bfd_build_id *
428 +build_id_phdr_get (bfd *templ, bfd_vma loadbase, unsigned e_phnum,
429 + Elf_Internal_Phdr *i_phdr)
432 + struct bfd_build_id *retval = NULL;
434 + for (i = 0; i < e_phnum; i++)
435 + if (i_phdr[i].p_type == PT_NOTE && i_phdr[i].p_filesz > 0)
437 + Elf_Internal_Phdr *hdr = &i_phdr[i];
441 + buf = (gdb_byte *) xmalloc (hdr->p_filesz);
442 + err = target_read_memory (loadbase + i_phdr[i].p_vaddr, buf,
445 + retval = build_id_buf_get (templ, buf, hdr->p_filesz);
449 + if (retval != NULL)
455 +/* First we validate the file by reading in the ELF header and checking
456 + the magic number. */
458 +static inline bfd_boolean
459 +elf_file_p (Elf64_External_Ehdr *x_ehdrp64)
461 + gdb_assert (sizeof (Elf64_External_Ehdr) >= sizeof (Elf32_External_Ehdr));
462 + gdb_assert (offsetof (Elf64_External_Ehdr, e_ident)
463 + == offsetof (Elf32_External_Ehdr, e_ident));
464 + gdb_assert (sizeof (((Elf64_External_Ehdr *) 0)->e_ident)
465 + == sizeof (((Elf32_External_Ehdr *) 0)->e_ident));
467 + return ((x_ehdrp64->e_ident[EI_MAG0] == ELFMAG0)
468 + && (x_ehdrp64->e_ident[EI_MAG1] == ELFMAG1)
469 + && (x_ehdrp64->e_ident[EI_MAG2] == ELFMAG2)
470 + && (x_ehdrp64->e_ident[EI_MAG3] == ELFMAG3));
473 +/* Translate an ELF file header in external format into an ELF file header in
474 + internal format. */
476 +#define H_GET_WORD(bfd, ptr) (is64 ? H_GET_64 (bfd, (ptr)) \
477 + : H_GET_32 (bfd, (ptr)))
478 +#define H_GET_SIGNED_WORD(bfd, ptr) (is64 ? H_GET_S64 (bfd, (ptr)) \
479 + : H_GET_S32 (bfd, (ptr)))
482 +elf_swap_ehdr_in (bfd *abfd,
483 + const Elf64_External_Ehdr *src64,
484 + Elf_Internal_Ehdr *dst)
486 + int is64 = bfd_get_arch_size (abfd) == 64;
487 +#define SRC(field) (is64 ? src64->field \
488 + : ((const Elf32_External_Ehdr *) src64)->field)
490 + int signed_vma = get_elf_backend_data (abfd)->sign_extend_vma;
491 + memcpy (dst->e_ident, SRC (e_ident), EI_NIDENT);
492 + dst->e_type = H_GET_16 (abfd, SRC (e_type));
493 + dst->e_machine = H_GET_16 (abfd, SRC (e_machine));
494 + dst->e_version = H_GET_32 (abfd, SRC (e_version));
496 + dst->e_entry = H_GET_SIGNED_WORD (abfd, SRC (e_entry));
498 + dst->e_entry = H_GET_WORD (abfd, SRC (e_entry));
499 + dst->e_phoff = H_GET_WORD (abfd, SRC (e_phoff));
500 + dst->e_shoff = H_GET_WORD (abfd, SRC (e_shoff));
501 + dst->e_flags = H_GET_32 (abfd, SRC (e_flags));
502 + dst->e_ehsize = H_GET_16 (abfd, SRC (e_ehsize));
503 + dst->e_phentsize = H_GET_16 (abfd, SRC (e_phentsize));
504 + dst->e_phnum = H_GET_16 (abfd, SRC (e_phnum));
505 + dst->e_shentsize = H_GET_16 (abfd, SRC (e_shentsize));
506 + dst->e_shnum = H_GET_16 (abfd, SRC (e_shnum));
507 + dst->e_shstrndx = H_GET_16 (abfd, SRC (e_shstrndx));
512 +/* Translate an ELF program header table entry in external format into an
513 + ELF program header table entry in internal format. */
516 +elf_swap_phdr_in (bfd *abfd,
517 + const Elf64_External_Phdr *src64,
518 + Elf_Internal_Phdr *dst)
520 + int is64 = bfd_get_arch_size (abfd) == 64;
521 +#define SRC(field) (is64 ? src64->field \
522 + : ((const Elf32_External_Phdr *) src64)->field)
524 + int signed_vma = get_elf_backend_data (abfd)->sign_extend_vma;
526 + dst->p_type = H_GET_32 (abfd, SRC (p_type));
527 + dst->p_flags = H_GET_32 (abfd, SRC (p_flags));
528 + dst->p_offset = H_GET_WORD (abfd, SRC (p_offset));
531 + dst->p_vaddr = H_GET_SIGNED_WORD (abfd, SRC (p_vaddr));
532 + dst->p_paddr = H_GET_SIGNED_WORD (abfd, SRC (p_paddr));
536 + dst->p_vaddr = H_GET_WORD (abfd, SRC (p_vaddr));
537 + dst->p_paddr = H_GET_WORD (abfd, SRC (p_paddr));
539 + dst->p_filesz = H_GET_WORD (abfd, SRC (p_filesz));
540 + dst->p_memsz = H_GET_WORD (abfd, SRC (p_memsz));
541 + dst->p_align = H_GET_WORD (abfd, SRC (p_align));
546 +#undef H_GET_SIGNED_WORD
549 +static Elf_Internal_Phdr *
550 +elf_get_phdr (bfd *templ, bfd_vma ehdr_vma, unsigned *e_phnum_pointer,
551 + bfd_vma *loadbase_pointer)
553 + /* sizeof (Elf64_External_Ehdr) >= sizeof (Elf32_External_Ehdr) */
554 + Elf64_External_Ehdr x_ehdr64; /* Elf file header, external form */
555 + Elf_Internal_Ehdr i_ehdr; /* Elf file header, internal form */
556 + bfd_size_type x_phdrs_size;
557 + gdb_byte *x_phdrs_ptr;
558 + Elf_Internal_Phdr *i_phdrs;
564 + gdb_assert (templ != NULL);
565 + gdb_assert (sizeof (Elf64_External_Ehdr) >= sizeof (Elf32_External_Ehdr));
567 + /* Read in the ELF header in external format. */
568 + err = target_read_memory (ehdr_vma, (bfd_byte *) &x_ehdr64, sizeof x_ehdr64);
571 + if (build_id_verbose >= BUILD_ID_VERBOSE_BINARY_PARSE)
572 + warning (_("build-id: Error reading ELF header at address 0x%lx"),
573 + (unsigned long) ehdr_vma);
577 + /* Now check to see if we have a valid ELF file, and one that BFD can
578 + make use of. The magic number must match, the address size ('class')
579 + and byte-swapping must match our XVEC entry. */
581 + if (! elf_file_p (&x_ehdr64)
582 + || x_ehdr64.e_ident[EI_VERSION] != EV_CURRENT
583 + || !((bfd_get_arch_size (templ) == 64
584 + && x_ehdr64.e_ident[EI_CLASS] == ELFCLASS64)
585 + || (bfd_get_arch_size (templ) == 32
586 + && x_ehdr64.e_ident[EI_CLASS] == ELFCLASS32)))
588 + if (build_id_verbose >= BUILD_ID_VERBOSE_BINARY_PARSE)
589 + warning (_("build-id: Unrecognized ELF header at address 0x%lx"),
590 + (unsigned long) ehdr_vma);
594 + /* Check that file's byte order matches xvec's */
595 + switch (x_ehdr64.e_ident[EI_DATA])
597 + case ELFDATA2MSB: /* Big-endian */
598 + if (! bfd_header_big_endian (templ))
600 + if (build_id_verbose >= BUILD_ID_VERBOSE_BINARY_PARSE)
601 + warning (_("build-id: Unrecognized "
602 + "big-endian ELF header at address 0x%lx"),
603 + (unsigned long) ehdr_vma);
607 + case ELFDATA2LSB: /* Little-endian */
608 + if (! bfd_header_little_endian (templ))
610 + if (build_id_verbose >= BUILD_ID_VERBOSE_BINARY_PARSE)
611 + warning (_("build-id: Unrecognized "
612 + "little-endian ELF header at address 0x%lx"),
613 + (unsigned long) ehdr_vma);
617 + case ELFDATANONE: /* No data encoding specified */
618 + default: /* Unknown data encoding specified */
619 + if (build_id_verbose >= BUILD_ID_VERBOSE_BINARY_PARSE)
620 + warning (_("build-id: Unrecognized "
621 + "ELF header endianity at address 0x%lx"),
622 + (unsigned long) ehdr_vma);
626 + elf_swap_ehdr_in (templ, &x_ehdr64, &i_ehdr);
628 + /* The file header tells where to find the program headers.
629 + These are what we use to actually choose what to read. */
631 + if (i_ehdr.e_phentsize != (bfd_get_arch_size (templ) == 64
632 + ? sizeof (Elf64_External_Phdr)
633 + : sizeof (Elf32_External_Phdr))
634 + || i_ehdr.e_phnum == 0)
636 + if (build_id_verbose >= BUILD_ID_VERBOSE_BINARY_PARSE)
637 + warning (_("build-id: Invalid ELF program headers from the ELF header "
638 + "at address 0x%lx"), (unsigned long) ehdr_vma);
642 + x_phdrs_size = (bfd_get_arch_size (templ) == 64 ? sizeof (Elf64_External_Phdr)
643 + : sizeof (Elf32_External_Phdr));
645 + i_phdrs = (Elf_Internal_Phdr *) xmalloc (i_ehdr.e_phnum * (sizeof *i_phdrs + x_phdrs_size));
646 + x_phdrs_ptr = (gdb_byte *) &i_phdrs[i_ehdr.e_phnum];
647 + err = target_read_memory (ehdr_vma + i_ehdr.e_phoff, (bfd_byte *) x_phdrs_ptr,
648 + i_ehdr.e_phnum * x_phdrs_size);
652 + if (build_id_verbose >= BUILD_ID_VERBOSE_BINARY_PARSE)
653 + warning (_("build-id: Error reading "
654 + "ELF program headers at address 0x%lx"),
655 + (unsigned long) (ehdr_vma + i_ehdr.e_phoff));
659 + loadbase = ehdr_vma;
661 + for (i = 0; i < i_ehdr.e_phnum; ++i)
663 + elf_swap_phdr_in (templ, (Elf64_External_Phdr *)
664 + (x_phdrs_ptr + i * x_phdrs_size), &i_phdrs[i]);
665 + /* IA-64 vDSO may have two mappings for one segment, where one mapping
666 + is executable only, and one is read only. We must not use the
667 + executable one (PF_R is the first one, PF_X the second one). */
668 + if (i_phdrs[i].p_type == PT_LOAD && (i_phdrs[i].p_flags & PF_R))
670 + /* Only the first PT_LOAD segment indicates the file bias.
671 + Next segments may have P_VADDR arbitrarily higher.
672 + If the first segment has P_VADDR zero any next segment must not
673 + confuse us, the first one sets LOADBASE certainly enough. */
674 + if (!loadbase_set && i_phdrs[i].p_offset == 0)
676 + loadbase = ehdr_vma - i_phdrs[i].p_vaddr;
682 + if (build_id_verbose >= BUILD_ID_VERBOSE_BINARY_PARSE)
683 + warning (_("build-id: Found ELF header at address 0x%lx, loadbase 0x%lx"),
684 + (unsigned long) ehdr_vma, (unsigned long) loadbase);
686 + *e_phnum_pointer = i_ehdr.e_phnum;
687 + *loadbase_pointer = loadbase;
691 +/* BUILD_ID_ADDR_GET gets ADDR located somewhere in the object.
692 + Find the first section before ADDR containing an ELF header.
693 + We rely on the fact the sections from multiple files do not mix.
694 + FIXME: We should check ADDR is contained _inside_ the section with possibly
695 + missing content (P_FILESZ < P_MEMSZ). These omitted sections are currently
696 + hidden by _BFD_ELF_MAKE_SECTION_FROM_PHDR. */
698 +static CORE_ADDR build_id_addr;
699 +struct build_id_addr_sect
701 + struct build_id_addr_sect *next;
704 +static struct build_id_addr_sect *build_id_addr_sect;
706 +static void build_id_addr_candidate (bfd *abfd, asection *sect, void *obj)
708 + if (build_id_addr >= bfd_section_vma (abfd, sect))
710 + struct build_id_addr_sect *candidate;
712 + candidate = (struct build_id_addr_sect *) xmalloc (sizeof *candidate);
713 + candidate->next = build_id_addr_sect;
714 + build_id_addr_sect = candidate;
715 + candidate->sect = sect;
719 +struct bfd_build_id *
720 +build_id_addr_get (CORE_ADDR addr)
722 + struct build_id_addr_sect *candidate;
723 + struct bfd_build_id *retval = NULL;
724 + Elf_Internal_Phdr *i_phdr = NULL;
725 + bfd_vma loadbase = 0;
726 + unsigned e_phnum = 0;
728 + if (core_bfd == NULL)
731 + build_id_addr = addr;
732 + gdb_assert (build_id_addr_sect == NULL);
733 + bfd_map_over_sections (core_bfd, build_id_addr_candidate, NULL);
735 + /* Sections are sorted in the high-to-low VMAs order.
736 + Stop the search on the first ELF header we find.
737 + Do not continue the search even if it does not contain NT_GNU_BUILD_ID. */
739 + for (candidate = build_id_addr_sect; candidate != NULL;
740 + candidate = candidate->next)
742 + i_phdr = elf_get_phdr (core_bfd,
743 + bfd_section_vma (core_bfd, candidate->sect),
744 + &e_phnum, &loadbase);
745 + if (i_phdr != NULL)
749 + if (i_phdr != NULL)
751 + retval = build_id_phdr_get (core_bfd, loadbase, e_phnum, i_phdr);
755 + while (build_id_addr_sect != NULL)
757 + candidate = build_id_addr_sect;
758 + build_id_addr_sect = candidate->next;
765 /* See build-id.h. */
769 const struct bfd_build_id *found;
772 - found = build_id_bfd_get (abfd);
773 + found = build_id_bfd_shdr_get (abfd);
776 warning (_("File \"%s\" has no build-id, file skipped"),
782 +link_resolve (const char *symlink, int level)
784 + char buf[PATH_MAX + 1], *target, *retval;
788 + return xstrdup (symlink);
790 + got = readlink (symlink, buf, sizeof (buf));
791 + if (got < 0 || got >= sizeof (buf))
792 + return xstrdup (symlink);
795 + if (IS_ABSOLUTE_PATH (buf))
796 + target = xstrdup (buf);
799 + const std::string dir (ldirname (symlink));
801 + target = xstrprintf ("%s"
802 +#ifndef HAVE_DOS_BASED_FILE_SYSTEM
804 +#else /* HAVE_DOS_BASED_FILE_SYSTEM */
806 +#endif /* HAVE_DOS_BASED_FILE_SYSTEM */
807 + "%s", dir.c_str(), buf);
810 + retval = link_resolve (target, level + 1);
815 /* See build-id.h. */
818 -build_id_to_debug_bfd (size_t build_id_len, const bfd_byte *build_id)
819 +build_id_to_debug_bfd (size_t build_id_len, const bfd_byte *build_id,
820 + char **link_return, int add_debug_suffix)
822 - char *link, *debugdir;
823 + char *link, *debugdir, *link_all = NULL;
824 VEC (char_ptr) *debugdir_vec;
825 struct cleanup *back_to;
827 gdb_bfd_ref_ptr abfd;
830 /* DEBUG_FILE_DIRECTORY/.build-id/ab/cdef */
831 - alloc_len = (strlen (debug_file_directory)
832 - + (sizeof "/.build-id/" - 1) + 1
833 - + 2 * build_id_len + (sizeof ".debug" - 1) + 1);
834 - link = (char *) alloca (alloc_len);
835 + link = (char *) xmalloc (strlen (debug_file_directory) + 2 * build_id_len + 50);
837 /* Keep backward compatibility so that DEBUG_FILE_DIRECTORY being "" will
838 cause "/.build-id/..." lookups. */
840 size_t debugdir_len = strlen (debugdir);
841 const gdb_byte *data = build_id;
842 size_t size = build_id_len;
844 char *filename = NULL;
845 struct cleanup *inner;
847 + struct stat statbuf_trash;
848 + /* Initialize it just to avoid a GCC false warning. */
849 + char *s, *link0 = NULL, *link0_resolved;
851 memcpy (link, debugdir, debugdir_len);
852 s = &link[debugdir_len];
853 @@ -110,45 +542,290 @@
856 s += sprintf (s, "%02x", (unsigned) *data++);
857 - strcpy (s, ".debug");
858 + for (seqno = 0;; seqno++)
862 - /* lrealpath() is expensive even for the usually non-existent files. */
863 - if (access (link, F_OK) == 0)
864 - filename = lrealpath (link);
867 + /* There can be multiple build-id symlinks pointing to real files
868 + with the same build-id (such as hard links). Some of the real
869 + files may not be installed. */
871 + s2 = s + sprintf (s, ".%u", seqno);
876 + if (add_debug_suffix)
877 + strcpy (s2, ".debug");
883 + /* If none of the real files is found report as missing file
884 + always the non-.%u-suffixed file. */
885 + link0 = xstrdup (link);
888 + /* `access' automatically dereferences LINK. */
889 + if (lstat (link, &statbuf_trash) != 0)
891 + /* Stop increasing SEQNO. */
895 + filename = lrealpath (link);
896 + if (filename == NULL)
899 + /* We expect to be silent on the non-existing files. */
900 + inner = make_cleanup (xfree, filename);
901 + abfd = gdb_bfd_open (filename, gnutarget, -1);
902 + do_cleanups (inner);
907 - if (filename == NULL)
909 + if (build_id_verify (abfd.get(), build_id_len, build_id))
912 - /* We expect to be silent on the non-existing files. */
913 - inner = make_cleanup (xfree, filename);
914 - abfd = gdb_bfd_open (filename, gnutarget, -1);
915 - do_cleanups (inner);
923 - if (build_id_verify (abfd.get(), build_id_len, build_id))
925 + if (filename != NULL)
927 + /* LINK_ALL is not used below in this non-NULL FILENAME case. */
933 + /* If the symlink has target request to install the target.
934 + BASE-debuginfo.rpm contains the symlink but BASE.rpm may be missing.
935 + https://bugzilla.redhat.com/show_bug.cgi?id=981154 */
936 + link0_resolved = link_resolve (link0, 0);
939 + if (link_all == NULL)
940 + link_all = link0_resolved;
943 + size_t len_orig = strlen (link_all);
945 + link_all = (char *) xrealloc (link_all,
946 + len_orig + 1 + strlen (link0_resolved) + 1);
948 + /* Use whitespace instead of DIRNAME_SEPARATOR to be compatible with
949 + its possible use as an argument for installation command. */
950 + link_all[len_orig] = ' ';
952 + strcpy (&link_all[len_orig + 1], link0_resolved);
953 + xfree (link0_resolved);
957 + if (link_return != NULL)
961 + *link_return = link;
966 + *link_return = link_all;
973 do_cleanups (back_to);
978 +build_id_to_filename (const struct bfd_build_id *build_id, char **link_return)
980 + gdb_bfd_ref_ptr abfd;
983 + abfd = build_id_to_debug_bfd (build_id->size, build_id->data, link_return, 0);
987 + result = xstrdup (bfd_get_filename (abfd));
992 +/* This MISSING_FILEPAIR_HASH tracker is used only for the duplicite messages
993 + Try to install the hash file ...
996 +struct missing_filepair
1003 +static struct htab *missing_filepair_hash;
1004 +static struct obstack missing_filepair_obstack;
1007 +missing_filepair_xcalloc (size_t nmemb, size_t nmemb_size)
1010 + size_t size = nmemb * nmemb_size;
1012 + retval = obstack_alloc (&missing_filepair_obstack, size);
1013 + memset (retval, 0, size);
1018 +missing_filepair_hash_func (const struct missing_filepair *elem)
1020 + hashval_t retval = 0;
1022 + retval ^= htab_hash_string (elem->binary);
1023 + if (elem->debug != NULL)
1024 + retval ^= htab_hash_string (elem->debug);
1030 +missing_filepair_eq (const struct missing_filepair *elem1,
1031 + const struct missing_filepair *elem2)
1033 + return strcmp (elem1->binary, elem2->binary) == 0
1034 + && ((elem1->debug == NULL) == (elem2->debug == NULL))
1035 + && (elem1->debug == NULL || strcmp (elem1->debug, elem2->debug) == 0);
1039 +missing_filepair_change (void)
1041 + if (missing_filepair_hash != NULL)
1043 + obstack_free (&missing_filepair_obstack, NULL);
1044 + /* All their memory came just from missing_filepair_OBSTACK. */
1045 + missing_filepair_hash = NULL;
1050 +debug_print_executable_changed (void)
1052 + missing_filepair_change ();
1055 +/* Notify user the file BINARY with (possibly NULL) associated separate debug
1056 + information file DEBUG is missing. DEBUG may or may not be the build-id
1057 + file such as would be:
1058 + /usr/lib/debug/.build-id/dd/b1d2ce632721c47bb9e8679f369e2295ce71be.debug
1062 +debug_print_missing (const char *binary, const char *debug)
1064 + size_t binary_len0 = strlen (binary) + 1;
1065 + size_t debug_len0 = debug ? strlen (debug) + 1 : 0;
1066 + struct missing_filepair missing_filepair_find;
1067 + struct missing_filepair *missing_filepair;
1068 + struct missing_filepair **slot;
1070 + if (build_id_verbose < BUILD_ID_VERBOSE_FILENAMES)
1073 + if (missing_filepair_hash == NULL)
1075 + obstack_init (&missing_filepair_obstack);
1076 + missing_filepair_hash = htab_create_alloc (64,
1077 + (hashval_t (*) (const void *)) missing_filepair_hash_func,
1078 + (int (*) (const void *, const void *)) missing_filepair_eq, NULL,
1079 + missing_filepair_xcalloc, NULL);
1082 + /* Use MISSING_FILEPAIR_FIND first instead of calling obstack_alloc with
1083 + obstack_free in the case of a (rare) match. The problem is ALLOC_F for
1084 + MISSING_FILEPAIR_HASH allocates from MISSING_FILEPAIR_OBSTACK maintenance
1085 + structures for MISSING_FILEPAIR_HASH. Calling obstack_free would possibly
1086 + not to free only MISSING_FILEPAIR but also some such structures (allocated
1087 + during the htab_find_slot call). */
1089 + missing_filepair_find.binary = (char *) binary;
1090 + missing_filepair_find.debug = (char *) debug;
1091 + slot = (struct missing_filepair **) htab_find_slot (missing_filepair_hash,
1092 + &missing_filepair_find,
1095 + /* While it may be still printed duplicitely with the missing debuginfo file
1096 + * it is due to once printing about the binary file build-id link and once
1097 + * about the .debug file build-id link as both the build-id symlinks are
1098 + * located in the debuginfo package. */
1100 + if (*slot != NULL)
1103 + missing_filepair = (struct missing_filepair *) obstack_alloc (&missing_filepair_obstack,
1104 + sizeof (*missing_filepair) - 1
1105 + + binary_len0 + debug_len0);
1106 + missing_filepair->binary = missing_filepair->data;
1107 + memcpy (missing_filepair->binary, binary, binary_len0);
1108 + if (debug != NULL)
1110 + missing_filepair->debug = missing_filepair->binary + binary_len0;
1111 + memcpy (missing_filepair->debug, debug, debug_len0);
1114 + missing_filepair->debug = NULL;
1116 + *slot = missing_filepair;
1118 + /* We do not collect and flush these messages as each such message
1119 + already requires its own separate lines. */
1121 + fprintf_unfiltered (gdb_stdlog,
1122 + _("Missing separate debuginfo for %s\n"), binary);
1123 + if (debug != NULL)
1124 + fprintf_unfiltered (gdb_stdlog, _("Try to install the hash file %s\n"),
1128 /* See build-id.h. */
1131 -find_separate_debug_file_by_buildid (struct objfile *objfile)
1132 +find_separate_debug_file_by_buildid (struct objfile *objfile,
1133 + gdb::unique_xmalloc_ptr<char> *build_id_filename_return)
1135 const struct bfd_build_id *build_id;
1137 - build_id = build_id_bfd_get (objfile->obfd);
1138 + if (build_id_filename_return)
1139 + *build_id_filename_return = NULL;
1141 + build_id = build_id_bfd_shdr_get (objfile->obfd);
1142 if (build_id != NULL)
1144 + char *build_id_filename_cstr = NULL;
1145 gdb_bfd_ref_ptr abfd (build_id_to_debug_bfd (build_id->size,
1148 + (!build_id_filename_return ? NULL : &build_id_filename_cstr), 1));
1149 + if (build_id_filename_return)
1151 + if (!build_id_filename_cstr)
1152 + gdb_assert (!*build_id_filename_return);
1155 + *build_id_filename_return = gdb::unique_xmalloc_ptr<char> (build_id_filename_cstr);
1156 + build_id_filename_cstr = NULL;
1159 /* Prevent looping on a stripped .debug file. */
1161 && filename_cmp (bfd_get_filename (abfd.get ()),
1162 @@ -160,3 +837,21 @@
1167 +extern void _initialize_build_id (void);
1170 +_initialize_build_id (void)
1172 + add_setshow_zinteger_cmd ("build-id-verbose", no_class, &build_id_verbose,
1174 +Set debugging level of the build-id locator."), _("\
1175 +Show debugging level of the build-id locator."), _("\
1176 +Level 1 (default) enables printing the missing debug filenames,\n\
1177 +level 2 also prints the parsing of binaries to find the identificators."),
1179 + show_build_id_verbose,
1180 + &setlist, &showlist);
1182 + observer_attach_executable_changed (debug_print_executable_changed);
1184 Index: gdb-7.99.90.20170420/gdb/build-id.h
1185 ===================================================================
1186 --- gdb-7.99.90.20170420.orig/gdb/build-id.h 2017-04-20 23:00:35.415584426 +0200
1187 +++ gdb-7.99.90.20170420/gdb/build-id.h 2017-04-20 23:00:43.371629257 +0200
1190 #include "gdb_bfd.h"
1192 -/* Locate NT_GNU_BUILD_ID from ABFD and return its content. */
1193 +/* Separate debuginfo files have corrupted PHDR but SHDR is correct there.
1194 + Locate NT_GNU_BUILD_ID from ABFD and return its content. */
1196 -extern const struct bfd_build_id *build_id_bfd_get (bfd *abfd);
1197 +extern const struct bfd_build_id *build_id_bfd_shdr_get (bfd *abfd);
1199 /* Return true if ABFD has NT_GNU_BUILD_ID matching the CHECK value.
1200 Otherwise, issue a warning and return false. */
1204 extern gdb_bfd_ref_ptr build_id_to_debug_bfd (size_t build_id_len,
1205 - const bfd_byte *build_id);
1206 + const bfd_byte *build_id,
1207 + char **link_return,
1208 + int add_debug_suffix);
1210 +extern char *build_id_to_filename (const struct bfd_build_id *build_id,
1211 + char **link_return);
1213 /* Find the separate debug file for OBJFILE, by using the build-id
1214 associated with OBJFILE's BFD. If successful, returns a malloc'd
1215 file name for the separate debug file. The caller must free this.
1216 Otherwise, returns NULL. */
1218 -extern char *find_separate_debug_file_by_buildid (struct objfile *objfile);
1219 +extern char *find_separate_debug_file_by_buildid (struct objfile *objfile,
1220 + gdb::unique_xmalloc_ptr<char> *build_id_filename_return);
1222 #endif /* BUILD_ID_H */
1223 Index: gdb-7.99.90.20170420/gdb/dwarf2read.c
1224 ===================================================================
1225 --- gdb-7.99.90.20170420.orig/gdb/dwarf2read.c 2017-04-20 23:00:35.415584426 +0200
1226 +++ gdb-7.99.90.20170420/gdb/dwarf2read.c 2017-04-20 23:00:43.375629279 +0200
1227 @@ -2671,7 +2671,7 @@
1230 if (dwz_bfd == NULL)
1231 - dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
1232 + dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid, NULL, 1);
1234 if (dwz_bfd == NULL)
1235 error (_("could not find '.gnu_debugaltlink' file for %s"),
1236 Index: gdb-7.99.90.20170420/gdb/python/py-objfile.c
1237 ===================================================================
1238 --- gdb-7.99.90.20170420.orig/gdb/python/py-objfile.c 2017-04-20 23:00:35.415584426 +0200
1239 +++ gdb-7.99.90.20170420/gdb/python/py-objfile.c 2017-04-20 23:00:43.375629279 +0200
1244 - build_id = build_id_bfd_get (objfile->obfd);
1245 + build_id = build_id_bfd_shdr_get (objfile->obfd);
1247 CATCH (except, RETURN_MASK_ALL)
1250 /* Don't return separate debug files. */
1251 if (objfile->separate_debug_objfile_backlink != NULL)
1253 - obfd_build_id = build_id_bfd_get (objfile->obfd);
1254 + obfd_build_id = build_id_bfd_shdr_get (objfile->obfd);
1255 if (obfd_build_id == NULL)
1257 if (objfpy_build_id_matches (obfd_build_id, build_id))
1258 Index: gdb-7.99.90.20170420/gdb/coffread.c
1259 ===================================================================
1260 --- gdb-7.99.90.20170420.orig/gdb/coffread.c 2017-04-20 23:00:35.415584426 +0200
1261 +++ gdb-7.99.90.20170420/gdb/coffread.c 2017-04-20 23:00:43.376629285 +0200
1266 - debugfile = find_separate_debug_file_by_buildid (objfile);
1267 + debugfile = find_separate_debug_file_by_buildid (objfile, NULL);
1269 if (debugfile == NULL)
1270 debugfile = find_separate_debug_file_by_debuglink (objfile);