]> git.pld-linux.org Git - packages/gdb.git/blob - gdb-6.6-buildid-locate.patch
- updated to 14.1 + rebased Fedora buildid patches set
[packages/gdb.git] / gdb-6.6-buildid-locate.patch
1 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
5 @@ -45,6 +45,10 @@
6  #include "gdb_bfd.h"
7  #include "completer.h"
8  #include "filestuff.h"
9 +#include "auxv.h"
10 +#include "elf/common.h"
11 +#include "gdbcmd.h"
12 +#include "build-id.h"
13  
14  #ifndef O_LARGEFILE
15  #define O_LARGEFILE 0
16 @@ -266,6 +270,54 @@
17      inferior_ptid = ptid;                      /* Yes, make it current.  */
18  }
19  
20 +static int build_id_core_loads = 1;
21 +
22 +static void
23 +build_id_locate_exec (int from_tty)
24 +{
25 +  CORE_ADDR at_entry;
26 +  struct bfd_build_id *build_id;
27 +  char *execfilename, *debug_filename;
28 +  char *build_id_filename;
29 +  struct cleanup *back_to;
30 +
31 +  if (exec_bfd != NULL || symfile_objfile != NULL)
32 +    return;
33 +
34 +  if (target_auxv_search (&current_target, AT_ENTRY, &at_entry) <= 0)
35 +    return;
36 +
37 +  build_id = build_id_addr_get (at_entry);
38 +  if (build_id == NULL)
39 +    return;
40 +  back_to = make_cleanup (xfree, build_id);
41 +
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.  */
47 +
48 +  execfilename = build_id_to_filename (build_id, &build_id_filename);
49 +  make_cleanup (xfree, build_id_filename);
50 +
51 +  if (execfilename != NULL)
52 +    {
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;
59 +    }
60 +  else
61 +    debug_print_missing (_("the main executable file"), build_id_filename);
62 +
63 +  do_cleanups (back_to);
64 +
65 +  /* No automatic SOLIB_ADD as the libraries would get read twice.  */
66 +}
67 +
68  /* This routine opens and sets up the core file bfd.  */
69  
70  static void
71 @@ -402,6 +454,14 @@
72         switch_to_thread (thread->ptid);
73      }
74  
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);
82 +
83    post_create_inferior (&core_ops, from_tty);
84  
85    /* Now go through the target stack looking for threads since there
86 @@ -1079,4 +1139,11 @@
87    init_core_ops ();
88  
89    add_target_with_completer (&core_ops, filename_completer);
90 +
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."),
95 +                          NULL, NULL, NULL,
96 +                          &setlist, &showlist);
97  }
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 @@
103  
104  @end table
105  
106 +You can also adjust the current verbosity of the @dfn{build id} locating.
107 +
108 +@table @code
109 +
110 +@kindex set build-id-verbose
111 +@item set build-id-verbose 0
112 +No additional messages are printed.
113 +
114 +@item set build-id-verbose 1
115 +Missing separate debug filenames are printed.
116 +
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.
120 +
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.
124 +
125 +@end table
126 +
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
134 @@ -45,6 +45,7 @@
135  #include "auxv.h"
136  #include "gdb_bfd.h"
137  #include "probe.h"
138 +#include "build-id.h"
139  
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 @@
143           continue;
144         }
145  
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);
149 +      {
150 +       struct bfd_build_id *build_id;
151 +
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);
156 +
157 +       build_id = build_id_addr_get (newobj->lm_info->l_ld);
158 +       if (build_id != NULL)
159 +         {
160 +           char *name, *build_id_filename;
161 +
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);
165 +           if (name != NULL)
166 +             {
167 +               strncpy (newobj->so_name, name, SO_NAME_MAX_PATH_SIZE - 1);
168 +               newobj->so_name[SO_NAME_MAX_PATH_SIZE - 1] = '\0';
169 +               xfree (name);
170 +             }
171 +           else
172 +             {
173 +               debug_print_missing (newobj->so_name, build_id_filename);
174 +
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
178 +                  same SO_NAME.
179 +
180 +                  It suppresses bogus backtraces (and prints "??" there
181 +                  instead) if the on-disk files no longer match the
182 +                  running program version.  */
183 +
184 +               if (symfile_objfile != NULL
185 +                   && (symfile_objfile->flags
186 +                       & OBJF_BUILD_ID_CORE_LOADED) != 0)
187 +                 newobj->so_name[0] = 0;
188 +             }
189 +
190 +           xfree (build_id_filename);
191 +           xfree (build_id);
192 +         }
193 +      }
194 +
195        xfree (buffer);
196  
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)
205      {
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));
210  
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);
216         }
217 +      /* Check if any separate debug info has been extracted out.  */
218 +      else if (bfd_get_section_by_name (objfile->obfd, ".gnu_debuglink")
219 +              != NULL)
220 +       debug_print_missing (objfile_name (objfile), build_id_filename.get ());
221      }
222  }
223  
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
228 @@ -567,6 +567,10 @@
229  void map_symbol_filenames (symbol_filename_ftype *fun, void *data,
230                            int need_fullname);
231  
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);
235 +
236  /* From dwarf2read.c */
237  
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."
245         }
246      }
247 +    # Turn off the missing warnings as the testsuite does not expect it.
248 +    send_gdb "set build-id-verbose 0\n"
249 +    gdb_expect 10 {
250 +       -re "$gdb_prompt $" {
251 +           verbose "Disabled the missing debug infos warnings." 2
252 +       }
253 +       timeout {
254 +           warning "Could not disable the missing debug infos warnings.."
255 +       }
256 +    }
257      return 0
258  }
259  
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
264 @@ -309,6 +309,16 @@
265             warning "Couldn't set the width to 0."
266         }
267      }
268 +    # Turn off the missing warnings as the testsuite does not expect it.
269 +    send_gdb "190-gdb-set build-id-verbose 0\n"
270 +    gdb_expect 10 {
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
273 +       }
274 +       timeout {
275 +           warning "Could not disable the missing debug infos warnings.."
276 +       }
277 +    }
278  
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
285 @@ -62,6 +62,7 @@
286      set options ""
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
297 @@ -444,6 +444,10 @@
298    htab_t static_links;
299  };
300  
301 +/* This file was loaded according to the BUILD_ID_CORE_LOADS rules.  */
302 +
303 +#define OBJF_BUILD_ID_CORE_LOADED static_cast<enum objfile_flag>(1 << 12)
304 +
305  /* Declarations for functions defined in objfiles.c */
306  
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
312 @@ -293,3 +293,33 @@
313         pass $test
314      }
315  }
316 +
317 +
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)"
323 +} else {
324 +    gdb_exit
325 +    gdb_start
326 +
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
332 +
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)
337 +           pass $test
338 +       }
339 +    }
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 .*"
344 +    pass $wholetest
345 +}
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
350 @@ -26,11 +26,67 @@
351  #include "objfiles.h"
352  #include "filenames.h"
353  #include "gdbcore.h"
354 +#include "libbfd.h"
355 +#include "gdbcore.h"
356 +#include "gdbcmd.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>
363 +
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;
368 +static void
369 +show_build_id_verbose (struct ui_file *file, int from_tty,
370 +                      struct cmd_list_element *c, const char *value)
371 +{
372 +  fprintf_filtered (file, _("Verbosity level of the build-id locator is %s.\n"),
373 +                   value);
374 +}
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.  */
377 +
378 +static struct bfd_build_id *
379 +build_id_buf_get (bfd *templ, gdb_byte *buf, bfd_size_type size)
380 +{
381 +  bfd_byte *p;
382 +
383 +  p = buf;
384 +  while (p < buf + size)
385 +    {
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);
391 +
392 +      if (H_GET_32 (templ, xnp->type) == NT_GNU_BUILD_ID
393 +         && namesz == sizeof "GNU"
394 +         && memcmp (xnp->name, "GNU", sizeof "GNU") == 0)
395 +       {
396 +         size_t size = descsz;
397 +         gdb_byte *data = (gdb_byte *) descdata;
398 +         struct bfd_build_id *retval;
399 +
400 +         retval = (struct bfd_build_id *) xmalloc (sizeof *retval - 1 + size);
401 +         retval->size = size;
402 +         memcpy (retval->data, data, size);
403 +
404 +         return retval;
405 +       }
406 +      p = descdata + BFD_ALIGN (descsz, 4);
407 +    }
408 +  return NULL;
409 +}
410  
411  /* See build-id.h.  */
412  
413  const struct bfd_build_id *
414 -build_id_bfd_get (bfd *abfd)
415 +build_id_bfd_shdr_get (bfd *abfd)
416  {
417    if (!bfd_check_format (abfd, bfd_object))
418      return NULL;
419 @@ -42,6 +98,348 @@
420    return NULL;
421  }
422  
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.  */
426 +
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)
430 +{
431 +  int i;
432 +  struct bfd_build_id *retval = NULL;
433 +
434 +  for (i = 0; i < e_phnum; i++)
435 +    if (i_phdr[i].p_type == PT_NOTE && i_phdr[i].p_filesz > 0)
436 +      {
437 +       Elf_Internal_Phdr *hdr = &i_phdr[i];
438 +       gdb_byte *buf;
439 +       int err;
440 +
441 +       buf = (gdb_byte *) xmalloc (hdr->p_filesz);
442 +       err = target_read_memory (loadbase + i_phdr[i].p_vaddr, buf,
443 +                                 hdr->p_filesz);
444 +       if (err == 0)
445 +         retval = build_id_buf_get (templ, buf, hdr->p_filesz);
446 +       else
447 +         retval = NULL;
448 +       xfree (buf);
449 +       if (retval != NULL)
450 +         break;
451 +      }
452 +  return retval;
453 +}
454 +
455 +/* First we validate the file by reading in the ELF header and checking
456 +   the magic number.  */
457 +
458 +static inline bfd_boolean
459 +elf_file_p (Elf64_External_Ehdr *x_ehdrp64)
460 +{
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));
466 +
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));
471 +}
472 +
473 +/* Translate an ELF file header in external format into an ELF file header in
474 +   internal format.  */
475 +
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)))
480 +
481 +static void
482 +elf_swap_ehdr_in (bfd *abfd,
483 +                 const Elf64_External_Ehdr *src64,
484 +                 Elf_Internal_Ehdr *dst)
485 +{
486 +  int is64 = bfd_get_arch_size (abfd) == 64;
487 +#define SRC(field) (is64 ? src64->field \
488 +                        : ((const Elf32_External_Ehdr *) src64)->field)
489 +
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));
495 +  if (signed_vma)
496 +    dst->e_entry = H_GET_SIGNED_WORD (abfd, SRC (e_entry));
497 +  else
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));
508 +
509 +#undef SRC
510 +}
511 +
512 +/* Translate an ELF program header table entry in external format into an
513 +   ELF program header table entry in internal format.  */
514 +
515 +static void
516 +elf_swap_phdr_in (bfd *abfd,
517 +                 const Elf64_External_Phdr *src64,
518 +                 Elf_Internal_Phdr *dst)
519 +{
520 +  int is64 = bfd_get_arch_size (abfd) == 64;
521 +#define SRC(field) (is64 ? src64->field                                        \
522 +                        : ((const Elf32_External_Phdr *) src64)->field)
523 +
524 +  int signed_vma = get_elf_backend_data (abfd)->sign_extend_vma;
525 +
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));
529 +  if (signed_vma)
530 +    {
531 +      dst->p_vaddr = H_GET_SIGNED_WORD (abfd, SRC (p_vaddr));
532 +      dst->p_paddr = H_GET_SIGNED_WORD (abfd, SRC (p_paddr));
533 +    }
534 +  else
535 +    {
536 +      dst->p_vaddr = H_GET_WORD (abfd, SRC (p_vaddr));
537 +      dst->p_paddr = H_GET_WORD (abfd, SRC (p_paddr));
538 +    }
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));
542 +
543 +#undef SRC
544 +}
545 +
546 +#undef H_GET_SIGNED_WORD
547 +#undef H_GET_WORD
548 +
549 +static Elf_Internal_Phdr *
550 +elf_get_phdr (bfd *templ, bfd_vma ehdr_vma, unsigned *e_phnum_pointer,
551 +              bfd_vma *loadbase_pointer)
552 +{
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;
559 +  int err;
560 +  unsigned int i;
561 +  bfd_vma loadbase;
562 +  int loadbase_set;
563 +
564 +  gdb_assert (templ != NULL);
565 +  gdb_assert (sizeof (Elf64_External_Ehdr) >= sizeof (Elf32_External_Ehdr));
566 +
567 +  /* Read in the ELF header in external format.  */
568 +  err = target_read_memory (ehdr_vma, (bfd_byte *) &x_ehdr64, sizeof x_ehdr64);
569 +  if (err)
570 +    {
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);
574 +      return NULL;
575 +    }
576 +
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.  */
580 +
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)))
587 +    {
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);
591 +      return NULL;
592 +    }
593 +
594 +  /* Check that file's byte order matches xvec's */
595 +  switch (x_ehdr64.e_ident[EI_DATA])
596 +    {
597 +    case ELFDATA2MSB:          /* Big-endian */
598 +      if (! bfd_header_big_endian (templ))
599 +       {
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);
604 +         return NULL;
605 +       }
606 +      break;
607 +    case ELFDATA2LSB:          /* Little-endian */
608 +      if (! bfd_header_little_endian (templ))
609 +       {
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);
614 +         return NULL;
615 +       }
616 +      break;
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);
623 +      return NULL;
624 +    }
625 +
626 +  elf_swap_ehdr_in (templ, &x_ehdr64, &i_ehdr);
627 +
628 +  /* The file header tells where to find the program headers.
629 +     These are what we use to actually choose what to read.  */
630 +
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)
635 +    {
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);
639 +      return NULL;
640 +    }
641 +
642 +  x_phdrs_size = (bfd_get_arch_size (templ) == 64 ? sizeof (Elf64_External_Phdr)
643 +                                               : sizeof (Elf32_External_Phdr));
644 +
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);
649 +  if (err)
650 +    {
651 +      free (i_phdrs);
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));
656 +      return NULL;
657 +    }
658 +
659 +  loadbase = ehdr_vma;
660 +  loadbase_set = 0;
661 +  for (i = 0; i < i_ehdr.e_phnum; ++i)
662 +    {
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))
669 +       {
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)
675 +           {
676 +             loadbase = ehdr_vma - i_phdrs[i].p_vaddr;
677 +             loadbase_set = 1;
678 +           }
679 +       }
680 +    }
681 +
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);
685 +
686 +  *e_phnum_pointer = i_ehdr.e_phnum;
687 +  *loadbase_pointer = loadbase;
688 +  return i_phdrs;
689 +}
690 +
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.  */
697 +
698 +static CORE_ADDR build_id_addr;
699 +struct build_id_addr_sect
700 +  {
701 +    struct build_id_addr_sect *next;
702 +    asection *sect;
703 +  };
704 +static struct build_id_addr_sect *build_id_addr_sect;
705 +
706 +static void build_id_addr_candidate (bfd *abfd, asection *sect, void *obj)
707 +{
708 +  if (build_id_addr >= bfd_section_vma (abfd, sect))
709 +    {
710 +      struct build_id_addr_sect *candidate;
711 +
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;
716 +    }
717 +}
718 +
719 +struct bfd_build_id *
720 +build_id_addr_get (CORE_ADDR addr)
721 +{
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;
727 +
728 +  if (core_bfd == NULL)
729 +    return NULL;
730 +
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);
734 +
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.  */
738 +
739 +  for (candidate = build_id_addr_sect; candidate != NULL;
740 +       candidate = candidate->next)
741 +    {
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)
746 +       break;
747 +    }
748 +
749 +  if (i_phdr != NULL)
750 +    {
751 +      retval = build_id_phdr_get (core_bfd, loadbase, e_phnum, i_phdr);
752 +      xfree (i_phdr);
753 +    }
754 +
755 +  while (build_id_addr_sect != NULL)
756 +    {
757 +      candidate = build_id_addr_sect;
758 +      build_id_addr_sect = candidate->next;
759 +      xfree (candidate);
760 +    }
761 +
762 +  return retval;
763 +}
764 +
765  /* See build-id.h.  */
766  
767  int
768 @@ -50,7 +448,7 @@
769    const struct bfd_build_id *found;
770    int retval = 0;
771  
772 -  found = build_id_bfd_get (abfd);
773 +  found = build_id_bfd_shdr_get (abfd);
774  
775    if (found == NULL)
776      warning (_("File \"%s\" has no build-id, file skipped"),
777 @@ -65,23 +463,54 @@
778    return retval;
779  }
780  
781 +static char *
782 +link_resolve (const char *symlink, int level)
783 +{
784 +  char buf[PATH_MAX + 1], *target, *retval;
785 +  ssize_t got;
786 +
787 +  if (level > 10)
788 +    return xstrdup (symlink);
789 +
790 +  got = readlink (symlink, buf, sizeof (buf));
791 +  if (got < 0 || got >= sizeof (buf))
792 +    return xstrdup (symlink);
793 +  buf[got] = '\0';
794 +
795 +  if (IS_ABSOLUTE_PATH (buf))
796 +    target = xstrdup (buf);
797 +  else
798 +    {
799 +      const std::string dir (ldirname (symlink));
800 +
801 +      target = xstrprintf ("%s"
802 +#ifndef HAVE_DOS_BASED_FILE_SYSTEM
803 +                          "/"
804 +#else /* HAVE_DOS_BASED_FILE_SYSTEM */
805 +                          "\\"
806 +#endif /* HAVE_DOS_BASED_FILE_SYSTEM */
807 +                          "%s", dir.c_str(), buf);
808 +    }
809 +
810 +  retval = link_resolve (target, level + 1);
811 +  xfree (target);
812 +  return retval;
813 +}
814 +
815  /* See build-id.h.  */
816  
817  gdb_bfd_ref_ptr
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)
821  {
822 -  char *link, *debugdir;
823 +  char *link, *debugdir, *link_all = NULL;
824    VEC (char_ptr) *debugdir_vec;
825    struct cleanup *back_to;
826    int ix;
827    gdb_bfd_ref_ptr abfd;
828 -  int alloc_len;
829  
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);
836  
837    /* Keep backward compatibility so that DEBUG_FILE_DIRECTORY being "" will
838       cause "/.build-id/..." lookups.  */
839 @@ -94,9 +523,12 @@
840        size_t debugdir_len = strlen (debugdir);
841        const gdb_byte *data = build_id;
842        size_t size = build_id_len;
843 -      char *s;
844        char *filename = NULL;
845        struct cleanup *inner;
846 +      unsigned seqno;
847 +      struct stat statbuf_trash;
848 +      /* Initialize it just to avoid a GCC false warning.  */
849 +      char *s, *link0 = NULL, *link0_resolved;
850  
851        memcpy (link, debugdir, debugdir_len);
852        s = &link[debugdir_len];
853 @@ -110,45 +542,290 @@
854         *s++ = '/';
855        while (size-- > 0)
856         s += sprintf (s, "%02x", (unsigned) *data++);
857 -      strcpy (s, ".debug");
858 +      for (seqno = 0;; seqno++)
859 +       {
860 +         char *s2;
861  
862 -      /* lrealpath() is expensive even for the usually non-existent files.  */
863 -      if (access (link, F_OK) == 0)
864 -       filename = lrealpath (link);
865 +         if (seqno)
866 +           {
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.  */
870 +
871 +             s2 = s + sprintf (s, ".%u", seqno);
872 +           }
873 +         else
874 +           s2 = s;
875 +
876 +         if (add_debug_suffix)
877 +           strcpy (s2, ".debug");
878 +         else
879 +           *s2 = 0;
880 +
881 +         if (!seqno)
882 +           {
883 +             /* If none of the real files is found report as missing file
884 +                always the non-.%u-suffixed file.  */
885 +             link0 = xstrdup (link);
886 +           }
887 +
888 +         /* `access' automatically dereferences LINK.  */
889 +         if (lstat (link, &statbuf_trash) != 0)
890 +           {
891 +             /* Stop increasing SEQNO.  */
892 +             break;
893 +           }
894 +
895 +         filename = lrealpath (link);
896 +         if (filename == NULL)
897 +           continue;
898 +
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);
903 +
904 +         if (abfd == NULL)
905 +           continue;
906  
907 -      if (filename == NULL)
908 -       continue;
909 +         if (build_id_verify (abfd.get(), build_id_len, build_id))
910 +           break;
911  
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);
916 +         abfd.release ();
917  
918 -      if (abfd == NULL)
919 -       continue;
920 +         filename = NULL;
921 +       }
922  
923 -      if (build_id_verify (abfd.get(), build_id_len, build_id))
924 -       break;
925 +      if (filename != NULL)
926 +       {
927 +         /* LINK_ALL is not used below in this non-NULL FILENAME case.  */
928 +         xfree (link0);
929 +         break;
930 +       }
931  
932 -      abfd.release ();
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);
937 +      xfree (link0);
938 +
939 +      if (link_all == NULL)
940 +       link_all = link0_resolved;
941 +      else
942 +       {
943 +         size_t len_orig = strlen (link_all);
944 +
945 +         link_all = (char *) xrealloc (link_all,
946 +                                       len_orig + 1 + strlen (link0_resolved) + 1);
947 +
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] = ' ';
951 +
952 +         strcpy (&link_all[len_orig + 1], link0_resolved);
953 +         xfree (link0_resolved);
954 +       }
955 +    }
956 +
957 +  if (link_return != NULL)
958 +    {
959 +      if (abfd != NULL)
960 +       {
961 +         *link_return = link;
962 +         link = NULL;
963 +       }
964 +      else
965 +       {
966 +         *link_return = link_all;
967 +         link_all = NULL;
968 +       }
969      }
970 +  xfree (link);
971 +  xfree (link_all);
972  
973    do_cleanups (back_to);
974    return abfd;
975  }
976  
977 +char *
978 +build_id_to_filename (const struct bfd_build_id *build_id, char **link_return)
979 +{
980 +  gdb_bfd_ref_ptr abfd;
981 +  char *result;
982 +  
983 +  abfd = build_id_to_debug_bfd (build_id->size, build_id->data, link_return, 0);
984 +  if (abfd == NULL)
985 +    return NULL;
986 +
987 +  result = xstrdup (bfd_get_filename (abfd));
988 +  abfd.release ();
989 +  return result;
990 +}
991 +
992 +/* This MISSING_FILEPAIR_HASH tracker is used only for the duplicite messages
993 +     Try to install the hash file ...
994 +   avoidance.  */
995 +
996 +struct missing_filepair
997 +  {
998 +    char *binary;
999 +    char *debug;
1000 +    char data[1];
1001 +  };
1002 +
1003 +static struct htab *missing_filepair_hash;
1004 +static struct obstack missing_filepair_obstack;
1005 +
1006 +static void *
1007 +missing_filepair_xcalloc (size_t nmemb, size_t nmemb_size)
1008 +{
1009 +  void *retval;
1010 +  size_t size = nmemb * nmemb_size;
1011 +
1012 +  retval = obstack_alloc (&missing_filepair_obstack, size);
1013 +  memset (retval, 0, size);
1014 +  return retval;
1015 +}
1016 +
1017 +static hashval_t
1018 +missing_filepair_hash_func (const struct missing_filepair *elem)
1019 +{
1020 +  hashval_t retval = 0;
1021 +
1022 +  retval ^= htab_hash_string (elem->binary);
1023 +  if (elem->debug != NULL)
1024 +    retval ^= htab_hash_string (elem->debug);
1025 +
1026 +  return retval;
1027 +}
1028 +
1029 +static int
1030 +missing_filepair_eq (const struct missing_filepair *elem1,
1031 +                      const struct missing_filepair *elem2)
1032 +{
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);
1036 +}
1037 +
1038 +static void
1039 +missing_filepair_change (void)
1040 +{
1041 +  if (missing_filepair_hash != NULL)
1042 +    {
1043 +      obstack_free (&missing_filepair_obstack, NULL);
1044 +      /* All their memory came just from missing_filepair_OBSTACK.  */
1045 +      missing_filepair_hash = NULL;
1046 +    }
1047 +}
1048 +
1049 +static void
1050 +debug_print_executable_changed (void)
1051 +{
1052 +  missing_filepair_change ();
1053 +}
1054 +
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
1059 +   */
1060 +
1061 +void
1062 +debug_print_missing (const char *binary, const char *debug)
1063 +{
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;
1069 +
1070 +  if (build_id_verbose < BUILD_ID_VERBOSE_FILENAMES)
1071 +    return;
1072 +
1073 +  if (missing_filepair_hash == NULL)
1074 +    {
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);
1080 +    }
1081 +
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).  */
1088 +
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,
1093 +                                                     INSERT);
1094 +
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.  */
1099 +
1100 +  if (*slot != NULL)
1101 +    return;
1102 +
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)
1109 +    {
1110 +      missing_filepair->debug = missing_filepair->binary + binary_len0;
1111 +      memcpy (missing_filepair->debug, debug, debug_len0);
1112 +    }
1113 +  else
1114 +    missing_filepair->debug = NULL;
1115 +
1116 +  *slot = missing_filepair;
1117 +
1118 +  /* We do not collect and flush these messages as each such message
1119 +     already requires its own separate lines.  */
1120 +
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"),
1125 +                       debug);
1126 +}
1127 +
1128  /* See build-id.h.  */
1129  
1130  char *
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)
1134  {
1135    const struct bfd_build_id *build_id;
1136  
1137 -  build_id = build_id_bfd_get (objfile->obfd);
1138 +  if (build_id_filename_return)
1139 +    *build_id_filename_return = NULL;
1140 +
1141 +  build_id = build_id_bfd_shdr_get (objfile->obfd);
1142    if (build_id != NULL)
1143      {
1144 +      char *build_id_filename_cstr = NULL;
1145        gdb_bfd_ref_ptr abfd (build_id_to_debug_bfd (build_id->size,
1146 -                                                  build_id->data));
1147 +                                                   build_id->data,
1148 +             (!build_id_filename_return ? NULL : &build_id_filename_cstr), 1));
1149 +      if (build_id_filename_return)
1150 +       {
1151 +         if (!build_id_filename_cstr)
1152 +           gdb_assert (!*build_id_filename_return);
1153 +         else
1154 +           {
1155 +             *build_id_filename_return = gdb::unique_xmalloc_ptr<char> (build_id_filename_cstr);
1156 +             build_id_filename_cstr = NULL;
1157 +           }
1158 +       }
1159        /* Prevent looping on a stripped .debug file.  */
1160        if (abfd != NULL
1161           && filename_cmp (bfd_get_filename (abfd.get ()),
1162 @@ -160,3 +837,21 @@
1163      }
1164    return NULL;
1165  }
1166 +
1167 +extern void _initialize_build_id (void);
1168 +
1169 +void
1170 +_initialize_build_id (void)
1171 +{
1172 +  add_setshow_zinteger_cmd ("build-id-verbose", no_class, &build_id_verbose,
1173 +                           _("\
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."),
1178 +                           NULL,
1179 +                           show_build_id_verbose,
1180 +                           &setlist, &showlist);
1181 +
1182 +  observer_attach_executable_changed (debug_print_executable_changed);
1183 +}
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
1188 @@ -22,9 +22,10 @@
1189  
1190  #include "gdb_bfd.h"
1191  
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.  */
1195  
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);
1198  
1199  /* Return true if ABFD has NT_GNU_BUILD_ID matching the CHECK value.
1200     Otherwise, issue a warning and return false.  */
1201 @@ -38,13 +39,19 @@
1202     the caller.  */
1203  
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);
1209 +
1210 +extern char *build_id_to_filename (const struct bfd_build_id *build_id,
1211 +                                  char **link_return);
1212  
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.  */
1217  
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);
1221  
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 @@
1228      }
1229  
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);
1233  
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
1240 @@ -137,7 +137,7 @@
1241  
1242    TRY
1243      {
1244 -      build_id = build_id_bfd_get (objfile->obfd);
1245 +      build_id = build_id_bfd_shdr_get (objfile->obfd);
1246      }
1247    CATCH (except, RETURN_MASK_ALL)
1248      {
1249 @@ -544,7 +544,7 @@
1250        /* Don't return separate debug files.  */
1251        if (objfile->separate_debug_objfile_backlink != NULL)
1252         continue;
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)
1256         continue;
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
1262 @@ -734,7 +734,7 @@
1263      {
1264        char *debugfile;
1265  
1266 -      debugfile = find_separate_debug_file_by_buildid (objfile);
1267 +      debugfile = find_separate_debug_file_by_buildid (objfile, NULL);
1268  
1269        if (debugfile == NULL)
1270         debugfile = find_separate_debug_file_by_debuglink (objfile);
This page took 0.192609 seconds and 3 git commands to generate.