]> git.pld-linux.org Git - packages/gdb.git/blob - gdb-6.6-buildid-locate.patch
- removed fedora/RH patchwork
[packages/gdb.git] / gdb-6.6-buildid-locate.patch
1 Index: gdb-7.4.91.20120801/gdb/corelow.c
2 ===================================================================
3 --- gdb-7.4.91.20120801.orig/gdb/corelow.c      2012-08-01 18:36:51.000000000 +0200
4 +++ gdb-7.4.91.20120801/gdb/corelow.c   2012-08-01 18:37:44.584925750 +0200
5 @@ -47,6 +47,9 @@
6  #include "progspace.h"
7  #include "objfiles.h"
8  #include "gdb_bfd.h"
9 +#include "auxv.h"
10 +#include "elf/common.h"
11 +#include "gdbcmd.h"
12  
13  #ifndef O_LARGEFILE
14  #define O_LARGEFILE 0
15 @@ -274,6 +277,52 @@ add_to_thread_list (bfd *abfd, asection
16      inferior_ptid = ptid;                      /* Yes, make it current.  */
17  }
18  
19 +static int build_id_core_loads = 1;
20 +
21 +static void
22 +build_id_locate_exec (int from_tty)
23 +{
24 +  CORE_ADDR at_entry;
25 +  struct build_id *build_id;
26 +  char *exec_filename, *debug_filename;
27 +  char *build_id_filename;
28 +  struct cleanup *back_to;
29 +
30 +  if (exec_bfd != NULL || symfile_objfile != NULL)
31 +    return;
32 +
33 +  if (target_auxv_search (&current_target, AT_ENTRY, &at_entry) <= 0)
34 +    return;
35 +
36 +  build_id = build_id_addr_get (at_entry);
37 +  if (build_id == NULL)
38 +    return;
39 +
40 +  /* SYMFILE_OBJFILE should refer to the main executable (not only to its
41 +     separate debug info file).  gcc44+ keeps .eh_frame only in the main
42 +     executable without its duplicate .debug_frame in the separate debug info
43 +     file - such .eh_frame would not be found if SYMFILE_OBJFILE would refer
44 +     directly to the separate debug info file.  */
45 +
46 +  exec_filename = build_id_to_filename (build_id, &build_id_filename, 0);
47 +  back_to = make_cleanup (xfree, build_id_filename);
48 +
49 +  if (exec_filename != NULL)
50 +    {
51 +      make_cleanup (xfree, exec_filename);
52 +      exec_file_attach (exec_filename, from_tty);
53 +      symbol_file_add_main (exec_filename, from_tty);
54 +      if (symfile_objfile != NULL)
55 +        symfile_objfile->flags |= OBJF_BUILD_ID_CORE_LOADED;
56 +    }
57 +  else
58 +    debug_print_missing (_("the main executable file"), build_id_filename);
59 +
60 +  do_cleanups (back_to);
61 +
62 +  /* No automatic SOLIB_ADD as the libraries would get read twice.  */
63 +}
64 +
65  /* This routine opens and sets up the core file bfd.  */
66  
67  static void
68 @@ -418,6 +467,14 @@ core_open (char *filename, int from_tty)
69         switch_to_thread (thread->ptid);
70      }
71  
72 +  /* Find the build_id identifiers.  If it gets executed after
73 +     POST_CREATE_INFERIOR we would clash with asking to discard the already
74 +     loaded VDSO symbols.  If it gets executed before bfd_map_over_sections
75 +     INFERIOR_PTID is still not set and libthread_db initialization crashes on
76 +     PID == 0 in ps_pglobal_lookup.  */
77 +  if (build_id_core_loads != 0)
78 +    build_id_locate_exec (from_tty);
79 +
80    post_create_inferior (&core_ops, from_tty);
81  
82    /* Now go through the target stack looking for threads since there
83 @@ -939,4 +996,11 @@ _initialize_corelow (void)
84    init_core_ops ();
85  
86    add_target (&core_ops);
87 +
88 +  add_setshow_boolean_cmd ("build-id-core-loads", class_files,
89 +                          &build_id_core_loads, _("\
90 +Set whether CORE-FILE loads the build-id associated files automatically."), _("\
91 +Show whether CORE-FILE loads the build-id associated files automatically."),
92 +                          NULL, NULL, NULL,
93 +                          &setlist, &showlist);
94  }
95 Index: gdb-7.4.91.20120801/gdb/doc/gdb.texinfo
96 ===================================================================
97 --- gdb-7.4.91.20120801.orig/gdb/doc/gdb.texinfo        2012-08-01 18:36:51.000000000 +0200
98 +++ gdb-7.4.91.20120801/gdb/doc/gdb.texinfo     2012-08-01 18:37:44.598925672 +0200
99 @@ -16530,6 +16530,27 @@ information files.
100  
101  @end table
102  
103 +You can also adjust the current verbosity of the @dfn{build id} locating.
104 +
105 +@table @code
106 +
107 +@kindex set build-id-verbose
108 +@item set build-id-verbose 0
109 +No additional messages are printed.
110 +
111 +@item set build-id-verbose 1
112 +Missing separate debug filenames are printed.
113 +
114 +@item set build-id-verbose 2
115 +Missing separate debug filenames are printed and also all the parsing of the
116 +binaries to find their @dfn{build id} content is printed.
117 +
118 +@kindex show build-id-verbose
119 +@item show build-id-verbose
120 +Show the current verbosity value for the @dfn{build id} content locating.
121 +
122 +@end table
123 +
124  @cindex @code{.gnu_debuglink} sections
125  @cindex debug link sections
126  A debug link is a special section of the executable file named
127 Index: gdb-7.4.91.20120801/gdb/solib-svr4.c
128 ===================================================================
129 --- gdb-7.4.91.20120801.orig/gdb/solib-svr4.c   2012-08-01 18:36:51.000000000 +0200
130 +++ gdb-7.4.91.20120801/gdb/solib-svr4.c        2012-08-01 18:37:44.599925667 +0200
131 @@ -1228,9 +1228,52 @@ svr4_read_so_list (CORE_ADDR lm, struct
132           continue;
133         }
134  
135 -      strncpy (new->so_name, buffer, SO_NAME_MAX_PATH_SIZE - 1);
136 -      new->so_name[SO_NAME_MAX_PATH_SIZE - 1] = '\0';
137 -      strcpy (new->so_original_name, new->so_name);
138 +      {
139 +       struct build_id *build_id;
140 +
141 +       strncpy (new->so_original_name, buffer, SO_NAME_MAX_PATH_SIZE - 1);
142 +       new->so_original_name[SO_NAME_MAX_PATH_SIZE - 1] = '\0';
143 +       /* May get overwritten below.  */
144 +       strcpy (new->so_name, new->so_original_name);
145 +
146 +       build_id = build_id_addr_get (new->lm_info->l_ld);
147 +       if (build_id != NULL)
148 +         {
149 +           char *name, *build_id_filename;
150 +
151 +           /* Missing the build-id matching separate debug info file
152 +              would be handled while SO_NAME gets loaded.  */
153 +           name = build_id_to_filename (build_id, &build_id_filename, 0);
154 +           if (name != NULL)
155 +             {
156 +               strncpy (new->so_name, name, SO_NAME_MAX_PATH_SIZE - 1);
157 +               new->so_name[SO_NAME_MAX_PATH_SIZE - 1] = '\0';
158 +               xfree (name);
159 +             }
160 +           else
161 +             {
162 +               debug_print_missing (new->so_name, build_id_filename);
163 +
164 +               /* In the case the main executable was found according to
165 +                  its build-id (from a core file) prevent loading
166 +                  a different build of a library with accidentally the
167 +                  same SO_NAME.
168 +
169 +                  It suppresses bogus backtraces (and prints "??" there
170 +                  instead) if the on-disk files no longer match the
171 +                  running program version.  */
172 +
173 +               if (symfile_objfile != NULL
174 +                   && (symfile_objfile->flags
175 +                       & OBJF_BUILD_ID_CORE_LOADED) != 0)
176 +                 new->so_name[0] = 0;
177 +             }
178 +
179 +           xfree (build_id_filename);
180 +           xfree (build_id);
181 +         }
182 +      }
183 +
184        xfree (buffer);
185  
186        /* If this entry has no name, or its name matches the name
187 Index: gdb-7.4.91.20120801/gdb/elfread.c
188 ===================================================================
189 --- gdb-7.4.91.20120801.orig/gdb/elfread.c      2012-08-01 18:36:51.000000000 +0200
190 +++ gdb-7.4.91.20120801/gdb/elfread.c   2012-08-01 18:38:08.956790879 +0200
191 @@ -45,6 +45,11 @@
192  #include "regcache.h"
193  #include "bcache.h"
194  #include "gdb_bfd.h"
195 +#include "libbfd.h"
196 +#include "gdbcore.h"
197 +#include "gdbcmd.h"
198 +#include "observer.h"
199 +#include <sys/stat.h>
200  
201  extern void _initialize_elfread (void);
202  
203 @@ -1074,16 +1079,65 @@ elf_gnu_ifunc_resolver_return_stop (stru
204    update_breakpoint_locations (b, sals, sals_end);
205  }
206  
207 +#define BUILD_ID_VERBOSE_NONE 0
208 +#define BUILD_ID_VERBOSE_FILENAMES 1
209 +#define BUILD_ID_VERBOSE_BINARY_PARSE 2
210 +static int build_id_verbose = BUILD_ID_VERBOSE_FILENAMES;
211 +static void
212 +show_build_id_verbose (struct ui_file *file, int from_tty,
213 +                      struct cmd_list_element *c, const char *value)
214 +{
215 +  fprintf_filtered (file, _("Verbosity level of the build-id locator is %s.\n"),
216 +                   value);
217 +}
218 +
219  struct build_id
220    {
221      size_t size;
222      gdb_byte data[1];
223    };
224  
225 -/* Locate NT_GNU_BUILD_ID from ABFD and return its content.  */
226 +/* Locate NT_GNU_BUILD_ID and return its matching debug filename.
227 +   FIXME: NOTE decoding should be unified with the BFD core notes decoding.  */
228  
229  static struct build_id *
230 -build_id_bfd_get (bfd *abfd)
231 +build_id_buf_get (bfd *templ, gdb_byte *buf, bfd_size_type size)
232 +{
233 +  bfd_byte *p;
234 +
235 +  p = buf;
236 +  while (p < buf + size)
237 +    {
238 +      /* FIXME: bad alignment assumption.  */
239 +      Elf_External_Note *xnp = (Elf_External_Note *) p;
240 +      size_t namesz = H_GET_32 (templ, xnp->namesz);
241 +      size_t descsz = H_GET_32 (templ, xnp->descsz);
242 +      bfd_byte *descdata = xnp->name + BFD_ALIGN (namesz, 4);
243 +
244 +      if (H_GET_32 (templ, xnp->type) == NT_GNU_BUILD_ID
245 +         && namesz == sizeof "GNU"
246 +         && memcmp (xnp->name, "GNU", sizeof "GNU") == 0)
247 +       {
248 +         size_t size = descsz;
249 +         gdb_byte *data = (void *) descdata;
250 +         struct build_id *retval;
251 +
252 +         retval = xmalloc (sizeof *retval - 1 + size);
253 +         retval->size = size;
254 +         memcpy (retval->data, data, size);
255 +
256 +         return retval;
257 +       }
258 +      p = descdata + BFD_ALIGN (descsz, 4);
259 +    }
260 +  return NULL;
261 +}
262 +
263 +/* Separate debuginfo files have corrupted PHDR but SHDR is correct there.
264 +   Locate NT_GNU_BUILD_ID from ABFD and return its content.  */
265 +
266 +static struct build_id *
267 +build_id_bfd_shdr_get (bfd *abfd)
268  {
269    struct build_id *retval;
270  
271 @@ -1099,6 +1153,348 @@ build_id_bfd_get (bfd *abfd)
272    return retval;
273  }
274  
275 +/* Core files may have missing (corrupt) SHDR but PDHR is correct there.
276 +   bfd_elf_bfd_from_remote_memory () has too much overhead by
277 +   allocating/reading all the available ELF PT_LOADs.  */
278 +
279 +static struct build_id *
280 +build_id_phdr_get (bfd *templ, bfd_vma loadbase, unsigned e_phnum,
281 +                  Elf_Internal_Phdr *i_phdr)
282 +{
283 +  int i;
284 +  struct build_id *retval = NULL;
285 +
286 +  for (i = 0; i < e_phnum; i++)
287 +    if (i_phdr[i].p_type == PT_NOTE && i_phdr[i].p_filesz > 0)
288 +      {
289 +       Elf_Internal_Phdr *hdr = &i_phdr[i];
290 +       gdb_byte *buf;
291 +       int err;
292 +
293 +       buf = xmalloc (hdr->p_filesz);
294 +       err = target_read_memory (loadbase + i_phdr[i].p_vaddr, buf,
295 +                                 hdr->p_filesz);
296 +       if (err == 0)
297 +         retval = build_id_buf_get (templ, buf, hdr->p_filesz);
298 +       else
299 +         retval = NULL;
300 +       xfree (buf);
301 +       if (retval != NULL)
302 +         break;
303 +      }
304 +  return retval;
305 +}
306 +
307 +/* First we validate the file by reading in the ELF header and checking
308 +   the magic number.  */
309 +
310 +static inline bfd_boolean
311 +elf_file_p (Elf64_External_Ehdr *x_ehdrp64)
312 +{
313 +  gdb_assert (sizeof (Elf64_External_Ehdr) >= sizeof (Elf32_External_Ehdr));
314 +  gdb_assert (offsetof (Elf64_External_Ehdr, e_ident)
315 +             == offsetof (Elf32_External_Ehdr, e_ident));
316 +  gdb_assert (sizeof (((Elf64_External_Ehdr *) 0)->e_ident)
317 +             == sizeof (((Elf32_External_Ehdr *) 0)->e_ident));
318 +
319 +  return ((x_ehdrp64->e_ident[EI_MAG0] == ELFMAG0)
320 +         && (x_ehdrp64->e_ident[EI_MAG1] == ELFMAG1)
321 +         && (x_ehdrp64->e_ident[EI_MAG2] == ELFMAG2)
322 +         && (x_ehdrp64->e_ident[EI_MAG3] == ELFMAG3));
323 +}
324 +
325 +/* Translate an ELF file header in external format into an ELF file header in
326 +   internal format.  */
327 +
328 +#define H_GET_WORD(bfd, ptr) (is64 ? H_GET_64 (bfd, (ptr))             \
329 +                                  : H_GET_32 (bfd, (ptr)))
330 +#define H_GET_SIGNED_WORD(bfd, ptr) (is64 ? H_GET_S64 (bfd, (ptr))     \
331 +                                         : H_GET_S32 (bfd, (ptr)))
332 +
333 +static void
334 +elf_swap_ehdr_in (bfd *abfd,
335 +                 const Elf64_External_Ehdr *src64,
336 +                 Elf_Internal_Ehdr *dst)
337 +{
338 +  int is64 = bfd_get_arch_size (abfd) == 64;
339 +#define SRC(field) (is64 ? src64->field \
340 +                        : ((const Elf32_External_Ehdr *) src64)->field)
341 +
342 +  int signed_vma = get_elf_backend_data (abfd)->sign_extend_vma;
343 +  memcpy (dst->e_ident, SRC (e_ident), EI_NIDENT);
344 +  dst->e_type = H_GET_16 (abfd, SRC (e_type));
345 +  dst->e_machine = H_GET_16 (abfd, SRC (e_machine));
346 +  dst->e_version = H_GET_32 (abfd, SRC (e_version));
347 +  if (signed_vma)
348 +    dst->e_entry = H_GET_SIGNED_WORD (abfd, SRC (e_entry));
349 +  else
350 +    dst->e_entry = H_GET_WORD (abfd, SRC (e_entry));
351 +  dst->e_phoff = H_GET_WORD (abfd, SRC (e_phoff));
352 +  dst->e_shoff = H_GET_WORD (abfd, SRC (e_shoff));
353 +  dst->e_flags = H_GET_32 (abfd, SRC (e_flags));
354 +  dst->e_ehsize = H_GET_16 (abfd, SRC (e_ehsize));
355 +  dst->e_phentsize = H_GET_16 (abfd, SRC (e_phentsize));
356 +  dst->e_phnum = H_GET_16 (abfd, SRC (e_phnum));
357 +  dst->e_shentsize = H_GET_16 (abfd, SRC (e_shentsize));
358 +  dst->e_shnum = H_GET_16 (abfd, SRC (e_shnum));
359 +  dst->e_shstrndx = H_GET_16 (abfd, SRC (e_shstrndx));
360 +
361 +#undef SRC
362 +}
363 +
364 +/* Translate an ELF program header table entry in external format into an
365 +   ELF program header table entry in internal format.  */
366 +
367 +static void
368 +elf_swap_phdr_in (bfd *abfd,
369 +                 const Elf64_External_Phdr *src64,
370 +                 Elf_Internal_Phdr *dst)
371 +{
372 +  int is64 = bfd_get_arch_size (abfd) == 64;
373 +#define SRC(field) (is64 ? src64->field                                        \
374 +                        : ((const Elf32_External_Phdr *) src64)->field)
375 +
376 +  int signed_vma = get_elf_backend_data (abfd)->sign_extend_vma;
377 +
378 +  dst->p_type = H_GET_32 (abfd, SRC (p_type));
379 +  dst->p_flags = H_GET_32 (abfd, SRC (p_flags));
380 +  dst->p_offset = H_GET_WORD (abfd, SRC (p_offset));
381 +  if (signed_vma)
382 +    {
383 +      dst->p_vaddr = H_GET_SIGNED_WORD (abfd, SRC (p_vaddr));
384 +      dst->p_paddr = H_GET_SIGNED_WORD (abfd, SRC (p_paddr));
385 +    }
386 +  else
387 +    {
388 +      dst->p_vaddr = H_GET_WORD (abfd, SRC (p_vaddr));
389 +      dst->p_paddr = H_GET_WORD (abfd, SRC (p_paddr));
390 +    }
391 +  dst->p_filesz = H_GET_WORD (abfd, SRC (p_filesz));
392 +  dst->p_memsz = H_GET_WORD (abfd, SRC (p_memsz));
393 +  dst->p_align = H_GET_WORD (abfd, SRC (p_align));
394 +
395 +#undef SRC
396 +}
397 +
398 +#undef H_GET_SIGNED_WORD
399 +#undef H_GET_WORD
400 +
401 +static Elf_Internal_Phdr *
402 +elf_get_phdr (bfd *templ, bfd_vma ehdr_vma, unsigned *e_phnum_pointer,
403 +              bfd_vma *loadbase_pointer)
404 +{
405 +  /* sizeof (Elf64_External_Ehdr) >= sizeof (Elf32_External_Ehdr)  */
406 +  Elf64_External_Ehdr x_ehdr64;        /* Elf file header, external form */
407 +  Elf_Internal_Ehdr i_ehdr;    /* Elf file header, internal form */
408 +  bfd_size_type x_phdrs_size;
409 +  gdb_byte *x_phdrs_ptr;
410 +  Elf_Internal_Phdr *i_phdrs;
411 +  int err;
412 +  unsigned int i;
413 +  bfd_vma loadbase;
414 +  int loadbase_set;
415 +
416 +  gdb_assert (templ != NULL);
417 +  gdb_assert (sizeof (Elf64_External_Ehdr) >= sizeof (Elf32_External_Ehdr));
418 +
419 +  /* Read in the ELF header in external format.  */
420 +  err = target_read_memory (ehdr_vma, (bfd_byte *) &x_ehdr64, sizeof x_ehdr64);
421 +  if (err)
422 +    {
423 +      if (build_id_verbose >= BUILD_ID_VERBOSE_BINARY_PARSE)
424 +        warning (_("build-id: Error reading ELF header at address 0x%lx"),
425 +                (unsigned long) ehdr_vma);
426 +      return NULL;
427 +    }
428 +
429 +  /* Now check to see if we have a valid ELF file, and one that BFD can
430 +     make use of.  The magic number must match, the address size ('class')
431 +     and byte-swapping must match our XVEC entry.  */
432 +
433 +  if (! elf_file_p (&x_ehdr64)
434 +      || x_ehdr64.e_ident[EI_VERSION] != EV_CURRENT
435 +      || !((bfd_get_arch_size (templ) == 64
436 +            && x_ehdr64.e_ident[EI_CLASS] == ELFCLASS64)
437 +           || (bfd_get_arch_size (templ) == 32
438 +              && x_ehdr64.e_ident[EI_CLASS] == ELFCLASS32)))
439 +    {
440 +      if (build_id_verbose >= BUILD_ID_VERBOSE_BINARY_PARSE)
441 +        warning (_("build-id: Unrecognized ELF header at address 0x%lx"),
442 +                (unsigned long) ehdr_vma);
443 +      return NULL;
444 +    }
445 +
446 +  /* Check that file's byte order matches xvec's */
447 +  switch (x_ehdr64.e_ident[EI_DATA])
448 +    {
449 +    case ELFDATA2MSB:          /* Big-endian */
450 +      if (! bfd_header_big_endian (templ))
451 +       {
452 +         if (build_id_verbose >= BUILD_ID_VERBOSE_BINARY_PARSE)
453 +           warning (_("build-id: Unrecognized "
454 +                      "big-endian ELF header at address 0x%lx"),
455 +                    (unsigned long) ehdr_vma);
456 +         return NULL;
457 +       }
458 +      break;
459 +    case ELFDATA2LSB:          /* Little-endian */
460 +      if (! bfd_header_little_endian (templ))
461 +       {
462 +         if (build_id_verbose >= BUILD_ID_VERBOSE_BINARY_PARSE)
463 +           warning (_("build-id: Unrecognized "
464 +                      "little-endian ELF header at address 0x%lx"),
465 +                    (unsigned long) ehdr_vma);
466 +         return NULL;
467 +       }
468 +      break;
469 +    case ELFDATANONE:          /* No data encoding specified */
470 +    default:                   /* Unknown data encoding specified */
471 +      if (build_id_verbose >= BUILD_ID_VERBOSE_BINARY_PARSE)
472 +       warning (_("build-id: Unrecognized "
473 +                  "ELF header endianity at address 0x%lx"),
474 +                (unsigned long) ehdr_vma);
475 +      return NULL;
476 +    }
477 +
478 +  elf_swap_ehdr_in (templ, &x_ehdr64, &i_ehdr);
479 +
480 +  /* The file header tells where to find the program headers.
481 +     These are what we use to actually choose what to read.  */
482 +
483 +  if (i_ehdr.e_phentsize != (bfd_get_arch_size (templ) == 64
484 +                             ? sizeof (Elf64_External_Phdr)
485 +                            : sizeof (Elf32_External_Phdr))
486 +      || i_ehdr.e_phnum == 0)
487 +    {
488 +      if (build_id_verbose >= BUILD_ID_VERBOSE_BINARY_PARSE)
489 +       warning (_("build-id: Invalid ELF program headers from the ELF header "
490 +                  "at address 0x%lx"), (unsigned long) ehdr_vma);
491 +      return NULL;
492 +    }
493 +
494 +  x_phdrs_size = (bfd_get_arch_size (templ) == 64 ? sizeof (Elf64_External_Phdr)
495 +                                               : sizeof (Elf32_External_Phdr));
496 +
497 +  i_phdrs = xmalloc (i_ehdr.e_phnum * (sizeof *i_phdrs + x_phdrs_size));
498 +  x_phdrs_ptr = (void *) &i_phdrs[i_ehdr.e_phnum];
499 +  err = target_read_memory (ehdr_vma + i_ehdr.e_phoff, (bfd_byte *) x_phdrs_ptr,
500 +                           i_ehdr.e_phnum * x_phdrs_size);
501 +  if (err)
502 +    {
503 +      free (i_phdrs);
504 +      if (build_id_verbose >= BUILD_ID_VERBOSE_BINARY_PARSE)
505 +        warning (_("build-id: Error reading "
506 +                  "ELF program headers at address 0x%lx"),
507 +                (unsigned long) (ehdr_vma + i_ehdr.e_phoff));
508 +      return NULL;
509 +    }
510 +
511 +  loadbase = ehdr_vma;
512 +  loadbase_set = 0;
513 +  for (i = 0; i < i_ehdr.e_phnum; ++i)
514 +    {
515 +      elf_swap_phdr_in (templ, (Elf64_External_Phdr *)
516 +                              (x_phdrs_ptr + i * x_phdrs_size), &i_phdrs[i]);
517 +      /* IA-64 vDSO may have two mappings for one segment, where one mapping
518 +        is executable only, and one is read only.  We must not use the
519 +        executable one (PF_R is the first one, PF_X the second one).  */
520 +      if (i_phdrs[i].p_type == PT_LOAD && (i_phdrs[i].p_flags & PF_R))
521 +       {
522 +         /* Only the first PT_LOAD segment indicates the file bias.
523 +            Next segments may have P_VADDR arbitrarily higher.
524 +            If the first segment has P_VADDR zero any next segment must not
525 +            confuse us, the first one sets LOADBASE certainly enough.  */
526 +         if (!loadbase_set && i_phdrs[i].p_offset == 0)
527 +           {
528 +             loadbase = ehdr_vma - i_phdrs[i].p_vaddr;
529 +             loadbase_set = 1;
530 +           }
531 +       }
532 +    }
533 +
534 +  if (build_id_verbose >= BUILD_ID_VERBOSE_BINARY_PARSE)
535 +    warning (_("build-id: Found ELF header at address 0x%lx, loadbase 0x%lx"),
536 +            (unsigned long) ehdr_vma, (unsigned long) loadbase);
537 +
538 +  *e_phnum_pointer = i_ehdr.e_phnum;
539 +  *loadbase_pointer = loadbase;
540 +  return i_phdrs;
541 +}
542 +
543 +/* BUILD_ID_ADDR_GET gets ADDR located somewhere in the object.
544 +   Find the first section before ADDR containing an ELF header.
545 +   We rely on the fact the sections from multiple files do not mix.
546 +   FIXME: We should check ADDR is contained _inside_ the section with possibly
547 +   missing content (P_FILESZ < P_MEMSZ).  These omitted sections are currently
548 +   hidden by _BFD_ELF_MAKE_SECTION_FROM_PHDR.  */
549 +
550 +static CORE_ADDR build_id_addr;
551 +struct build_id_addr_sect
552 +  {
553 +    struct build_id_addr_sect *next;
554 +    asection *sect;
555 +  };
556 +static struct build_id_addr_sect *build_id_addr_sect;
557 +
558 +static void build_id_addr_candidate (bfd *abfd, asection *sect, void *obj)
559 +{
560 +  if (build_id_addr >= bfd_section_vma (abfd, sect))
561 +    {
562 +      struct build_id_addr_sect *candidate;
563 +
564 +      candidate = xmalloc (sizeof *candidate);
565 +      candidate->next = build_id_addr_sect;
566 +      build_id_addr_sect = candidate;
567 +      candidate->sect = sect;
568 +    }
569 +}
570 +
571 +struct build_id *
572 +build_id_addr_get (CORE_ADDR addr)
573 +{
574 +  struct build_id_addr_sect *candidate;
575 +  struct build_id *retval = NULL;
576 +  Elf_Internal_Phdr *i_phdr = NULL;
577 +  bfd_vma loadbase = 0;
578 +  unsigned e_phnum = 0;
579 +
580 +  if (core_bfd == NULL)
581 +    return NULL;
582 +
583 +  build_id_addr = addr;
584 +  gdb_assert (build_id_addr_sect == NULL);
585 +  bfd_map_over_sections (core_bfd, build_id_addr_candidate, NULL);
586 +
587 +  /* Sections are sorted in the high-to-low VMAs order.
588 +     Stop the search on the first ELF header we find.
589 +     Do not continue the search even if it does not contain NT_GNU_BUILD_ID.  */
590 +
591 +  for (candidate = build_id_addr_sect; candidate != NULL;
592 +       candidate = candidate->next)
593 +    {
594 +      i_phdr = elf_get_phdr (core_bfd,
595 +                            bfd_section_vma (core_bfd, candidate->sect),
596 +                            &e_phnum, &loadbase);
597 +      if (i_phdr != NULL)
598 +       break;
599 +    }
600 +
601 +  if (i_phdr != NULL)
602 +    {
603 +      retval = build_id_phdr_get (core_bfd, loadbase, e_phnum, i_phdr);
604 +      xfree (i_phdr);
605 +    }
606 +
607 +  while (build_id_addr_sect != NULL)
608 +    {
609 +      candidate = build_id_addr_sect;
610 +      build_id_addr_sect = candidate->next;
611 +      xfree (candidate);
612 +    }
613 +
614 +  return retval;
615 +}
616 +
617  /* Return if FILENAME has NT_GNU_BUILD_ID matching the CHECK value.  */
618  
619  static int
620 @@ -1113,7 +1509,7 @@ build_id_verify (const char *filename, s
621    if (abfd == NULL)
622      return 0;
623  
624 -  found = build_id_bfd_get (abfd);
625 +  found = build_id_bfd_shdr_get (abfd);
626  
627    if (found == NULL)
628      warning (_("File \"%s\" has no build-id, file skipped"), filename);
629 @@ -1131,17 +1527,18 @@ build_id_verify (const char *filename, s
630    return retval;
631  }
632  
633 -static char *
634 -build_id_to_debug_filename (struct build_id *build_id)
635 +char *
636 +build_id_to_filename (struct build_id *build_id, char **link_return,
637 +                     int add_debug_suffix)
638  {
639    char *link, *debugdir, *retval = NULL;
640 +  char *link_all = NULL;
641    VEC (char_ptr) *debugdir_vec;
642    struct cleanup *back_to;
643    int ix;
644  
645    /* DEBUG_FILE_DIRECTORY/.build-id/ab/cdef */
646 -  link = alloca (strlen (debug_file_directory) + (sizeof "/.build-id/" - 1) + 1
647 -                + 2 * build_id->size + (sizeof ".debug" - 1) + 1);
648 +  link = xmalloc (strlen (debug_file_directory) + 2 * build_id->size + 50);
649  
650    /* Keep backward compatibility so that DEBUG_FILE_DIRECTORY being "" will
651       cause "/.build-id/..." lookups.  */
652 @@ -1154,6 +1551,8 @@ build_id_to_debug_filename (struct build
653        size_t debugdir_len = strlen (debugdir);
654        gdb_byte *data = build_id->data;
655        size_t size = build_id->size;
656 +      unsigned seqno;
657 +      struct stat statbuf_trash;
658        char *s;
659  
660        memcpy (link, debugdir, debugdir_len);
661 @@ -1168,37 +1567,240 @@ build_id_to_debug_filename (struct build
662         *s++ = '/';
663        while (size-- > 0)
664         s += sprintf (s, "%02x", (unsigned) *data++);
665 -      strcpy (s, ".debug");
666  
667 -      /* lrealpath() is expensive even for the usually non-existent files.  */
668 -      if (access (link, F_OK) == 0)
669 -       retval = lrealpath (link);
670 +      for (seqno = 0;; seqno++)
671 +       {
672 +         char *s2;
673 +
674 +         if (seqno)
675 +           {
676 +             /* There can be multiple build-id symlinks pointing to real files
677 +                with the same build-id (such as hard links).  Some of the real
678 +                files may not be installed.  */
679 +
680 +             s2 = s + sprintf (s, ".%u", seqno);
681 +           }
682 +         else
683 +           s2 = s;
684 +
685 +         if (add_debug_suffix)
686 +           strcpy (s2, ".debug");
687 +         else
688 +           *s2 = 0;
689  
690 -      if (retval != NULL && !build_id_verify (retval, build_id))
691 +         /* `access' automatically dereferences LINK.  */
692 +         if (lstat (link, &statbuf_trash) != 0)
693 +           {
694 +             /* Stop increasing SEQNO.  */
695 +             break;
696 +           }
697 +
698 +         retval = lrealpath (link);
699 +
700 +         if (retval != NULL && !build_id_verify (retval, build_id))
701 +           {
702 +             xfree (retval);
703 +             retval = NULL;
704 +           }
705 +
706 +         if (retval)
707 +           break;
708 +       }
709 +
710 +      if (retval != NULL)
711         {
712 -         xfree (retval);
713 -         retval = NULL;
714 +         /* LINK_ALL is not used below in this non-NULL RETVAL case.  */
715 +         break;
716         }
717  
718 +       if (link_all == NULL)
719 +         link_all = xstrdup (link);
720 +       else
721 +         {
722 +           size_t len_orig = strlen (link_all);
723 +
724 +           link_all = xrealloc (link_all, len_orig + 1 + strlen (link) + 1);
725 +
726 +           /* Use whitespace instead of DIRNAME_SEPARATOR to be compatible with
727 +              its possible use as an argument for installation command.  */
728 +           link_all[len_orig] = ' ';
729 +
730 +           strcpy (&link_all[len_orig + 1], link);
731 +         }
732 +    }
733 +
734 +  if (link_return != NULL)
735 +    {
736        if (retval != NULL)
737 -       break;
738 +       {
739 +         *link_return = link;
740 +         link = NULL;
741 +       }
742 +      else
743 +       {
744 +         *link_return = link_all;
745 +         link_all = NULL;
746 +       }
747      }
748 +  xfree (link);
749 +  xfree (link_all);
750  
751    do_cleanups (back_to);
752    return retval;
753  }
754  
755 +/* This MISSING_FILEPAIR_HASH tracker is used only for the duplicite messages
756 +     Try to install the hash file ...
757 +   avoidance.  */
758 +
759 +struct missing_filepair
760 +  {
761 +    char *binary;
762 +    char *debug;
763 +    char data[1];
764 +  };
765 +
766 +static struct htab *missing_filepair_hash;
767 +static struct obstack missing_filepair_obstack;
768 +
769 +static void *
770 +missing_filepair_xcalloc (size_t nmemb, size_t nmemb_size)
771 +{
772 +  void *retval;
773 +  size_t size = nmemb * nmemb_size;
774 +
775 +  retval = obstack_alloc (&missing_filepair_obstack, size);
776 +  memset (retval, 0, size);
777 +  return retval;
778 +}
779 +
780 +static hashval_t
781 +missing_filepair_hash_func (const struct missing_filepair *elem)
782 +{
783 +  hashval_t retval = 0;
784 +
785 +  retval ^= htab_hash_string (elem->binary);
786 +  if (elem->debug != NULL)
787 +    retval ^= htab_hash_string (elem->debug);
788 +
789 +  return retval;
790 +}
791 +
792 +static int
793 +missing_filepair_eq (const struct missing_filepair *elem1,
794 +                      const struct missing_filepair *elem2)
795 +{
796 +  return strcmp (elem1->binary, elem2->binary) == 0
797 +         && ((elem1->debug == NULL) == (elem2->debug == NULL))
798 +         && (elem1->debug == NULL || strcmp (elem1->debug, elem2->debug) == 0);
799 +}
800 +
801 +static void
802 +missing_filepair_change (void)
803 +{
804 +  if (missing_filepair_hash != NULL)
805 +    {
806 +      obstack_free (&missing_filepair_obstack, NULL);
807 +      /* All their memory came just from missing_filepair_OBSTACK.  */
808 +      missing_filepair_hash = NULL;
809 +    }
810 +}
811 +
812 +static void
813 +debug_print_executable_changed (void)
814 +{
815 +  missing_filepair_change ();
816 +}
817 +
818 +/* Notify user the file BINARY with (possibly NULL) associated separate debug
819 +   information file DEBUG is missing.  DEBUG may or may not be the build-id
820 +   file such as would be:
821 +     /usr/lib/debug/.build-id/dd/b1d2ce632721c47bb9e8679f369e2295ce71be.debug
822 +   */
823 +
824 +void
825 +debug_print_missing (const char *binary, const char *debug)
826 +{
827 +  size_t binary_len0 = strlen (binary) + 1;
828 +  size_t debug_len0 = debug ? strlen (debug) + 1 : 0;
829 +  struct missing_filepair missing_filepair_find;
830 +  struct missing_filepair *missing_filepair;
831 +  struct missing_filepair **slot;
832 +
833 +  if (build_id_verbose < BUILD_ID_VERBOSE_FILENAMES)
834 +    return;
835 +
836 +  if (missing_filepair_hash == NULL)
837 +    {
838 +      obstack_init (&missing_filepair_obstack);
839 +      missing_filepair_hash = htab_create_alloc (64,
840 +       (hashval_t (*) (const void *)) missing_filepair_hash_func,
841 +       (int (*) (const void *, const void *)) missing_filepair_eq, NULL,
842 +       missing_filepair_xcalloc, NULL);
843 +    }
844 +
845 +  /* Use MISSING_FILEPAIR_FIND first instead of calling obstack_alloc with
846 +     obstack_free in the case of a (rare) match.  The problem is ALLOC_F for
847 +     MISSING_FILEPAIR_HASH allocates from MISSING_FILEPAIR_OBSTACK maintenance
848 +     structures for MISSING_FILEPAIR_HASH.  Calling obstack_free would possibly
849 +     not to free only MISSING_FILEPAIR but also some such structures (allocated
850 +     during the htab_find_slot call).  */
851 +
852 +  missing_filepair_find.binary = (char *) binary;
853 +  missing_filepair_find.debug = (char *) debug;
854 +  slot = (struct missing_filepair **) htab_find_slot (missing_filepair_hash,
855 +                                                     &missing_filepair_find,
856 +                                                     INSERT);
857 +
858 +  /* While it may be still printed duplicitely with the missing debuginfo file
859 +   * it is due to once printing about the binary file build-id link and once
860 +   * about the .debug file build-id link as both the build-id symlinks are
861 +   * located in the debuginfo package.  */
862 +
863 +  if (*slot != NULL)
864 +    return;
865 +
866 +  missing_filepair = obstack_alloc (&missing_filepair_obstack,
867 +                                     sizeof (*missing_filepair) - 1
868 +                                     + binary_len0 + debug_len0);
869 +  missing_filepair->binary = missing_filepair->data;
870 +  memcpy (missing_filepair->binary, binary, binary_len0);
871 +  if (debug != NULL)
872 +    {
873 +      missing_filepair->debug = missing_filepair->binary + binary_len0;
874 +      memcpy (missing_filepair->debug, debug, debug_len0);
875 +    }
876 +  else
877 +    missing_filepair->debug = NULL;
878 +
879 +  *slot = missing_filepair;
880 +
881 +  /* We do not collect and flush these messages as each such message
882 +     already requires its own separate lines.  */
883 +
884 +  fprintf_unfiltered (gdb_stdlog,
885 +                     _("Missing separate debuginfo for %s\n"), binary);
886 +  if (debug != NULL)
887 +    fprintf_unfiltered (gdb_stdlog, _("Try to install the hash file %s\n"),
888 +                       debug);
889 +}
890 +
891  static char *
892 -find_separate_debug_file_by_buildid (struct objfile *objfile)
893 +find_separate_debug_file_by_buildid (struct objfile *objfile,
894 +                                    char **build_id_filename_return)
895  {
896    struct build_id *build_id;
897  
898 -  build_id = build_id_bfd_get (objfile->obfd);
899 +  if (build_id_filename_return)
900 +    *build_id_filename_return = NULL;
901 +
902 +  build_id = build_id_bfd_shdr_get (objfile->obfd);
903    if (build_id != NULL)
904      {
905        char *build_id_name;
906  
907 -      build_id_name = build_id_to_debug_filename (build_id);
908 +      build_id_name = build_id_to_filename (build_id, build_id_filename_return,
909 +                                           1);
910        xfree (build_id);
911        /* Prevent looping on a stripped .debug file.  */
912        if (build_id_name != NULL
913 @@ -1209,7 +1811,7 @@ find_separate_debug_file_by_buildid (str
914           xfree (build_id_name);
915         }
916        else if (build_id_name != NULL)
917 -        return build_id_name;
918 +       return build_id_name;
919      }
920    return NULL;
921  }
922 @@ -1436,9 +2038,10 @@ elf_symfile_read (struct objfile *objfil
923       `.note.gnu.build-id'.  */
924    else if (!objfile_has_partial_symbols (objfile))
925      {
926 -      char *debugfile;
927 +      char *debugfile, *build_id_filename;
928  
929 -      debugfile = find_separate_debug_file_by_buildid (objfile);
930 +      debugfile = find_separate_debug_file_by_buildid (objfile,
931 +                                                      &build_id_filename);
932  
933        if (debugfile == NULL)
934         debugfile = find_separate_debug_file_by_debuglink (objfile);
935 @@ -1452,6 +2055,12 @@ elf_symfile_read (struct objfile *objfil
936           symbol_file_add_separate (abfd, symfile_flags, objfile);
937           do_cleanups (cleanup);
938         }
939 +      /* Check if any separate debug info has been extracted out.  */
940 +      else if (bfd_get_section_by_name (objfile->obfd, ".gnu_debuglink")
941 +              != NULL)
942 +       debug_print_missing (objfile->name, build_id_filename);
943 +
944 +      xfree (build_id_filename);
945      }
946  
947    if (symtab_create_debug)
948 @@ -1784,4 +2393,16 @@ _initialize_elfread (void)
949  
950    elf_objfile_gnu_ifunc_cache_data = register_objfile_data ();
951    gnu_ifunc_fns_p = &elf_gnu_ifunc_fns;
952 +
953 +  add_setshow_zinteger_cmd ("build-id-verbose", no_class, &build_id_verbose,
954 +                           _("\
955 +Set debugging level of the build-id locator."), _("\
956 +Show debugging level of the build-id locator."), _("\
957 +Level 1 (default) enables printing the missing debug filenames,\n\
958 +level 2 also prints the parsing of binaries to find the identificators."),
959 +                           NULL,
960 +                           show_build_id_verbose,
961 +                           &setlist, &showlist);
962 +
963 +  observer_attach_executable_changed (debug_print_executable_changed);
964  }
965 Index: gdb-7.4.91.20120801/gdb/symfile.h
966 ===================================================================
967 --- gdb-7.4.91.20120801.orig/gdb/symfile.h      2012-08-01 18:36:51.000000000 +0200
968 +++ gdb-7.4.91.20120801/gdb/symfile.h   2012-08-01 18:37:44.600925662 +0200
969 @@ -615,6 +615,13 @@ void free_symfile_segment_data (struct s
970  
971  extern struct cleanup *increment_reading_symtab (void);
972  
973 +/* build-id support.  */
974 +struct build_id;
975 +extern struct build_id *build_id_addr_get (CORE_ADDR addr);
976 +extern char *build_id_to_filename (struct build_id *build_id,
977 +                                  char **link_return, int add_debug_suffix);
978 +extern void debug_print_missing (const char *binary, const char *debug);
979 +
980  /* From dwarf2read.c */
981  
982  /* Names for a dwarf2 debugging section.  The field NORMAL is the normal
983 Index: gdb-7.4.91.20120801/gdb/testsuite/lib/gdb.exp
984 ===================================================================
985 --- gdb-7.4.91.20120801.orig/gdb/testsuite/lib/gdb.exp  2012-08-01 18:36:51.000000000 +0200
986 +++ gdb-7.4.91.20120801/gdb/testsuite/lib/gdb.exp       2012-08-01 18:37:44.601925656 +0200
987 @@ -1388,6 +1388,16 @@ proc default_gdb_start { } {
988             warning "Couldn't set the width to 0."
989         }
990      }
991 +    # Turn off the missing warnings as the testsuite does not expect it.
992 +    send_gdb "set build-id-verbose 0\n"
993 +    gdb_expect 10 {
994 +       -re "$gdb_prompt $" {
995 +           verbose "Disabled the missing debug infos warnings." 2
996 +       }
997 +       timeout {
998 +           warning "Could not disable the missing debug infos warnings.."
999 +       }
1000 +    }
1001      return 0;
1002  }
1003  
1004 Index: gdb-7.4.91.20120801/gdb/testsuite/lib/mi-support.exp
1005 ===================================================================
1006 --- gdb-7.4.91.20120801.orig/gdb/testsuite/lib/mi-support.exp   2012-07-25 22:35:30.000000000 +0200
1007 +++ gdb-7.4.91.20120801/gdb/testsuite/lib/mi-support.exp        2012-08-01 18:37:44.601925656 +0200
1008 @@ -213,6 +213,16 @@ proc default_mi_gdb_start { args } {
1009             warning "Couldn't set the width to 0."
1010         }
1011      }
1012 +    # Turn off the missing warnings as the testsuite does not expect it.
1013 +    send_gdb "190-gdb-set build-id-verbose 0\n"
1014 +    gdb_expect 10 {
1015 +       -re ".*190-gdb-set build-id-verbose 0\r\n190\\\^done\r\n$mi_gdb_prompt$" {
1016 +           verbose "Disabled the missing debug infos warnings." 2
1017 +       }
1018 +       timeout {
1019 +           warning "Could not disable the missing debug infos warnings.."
1020 +       }
1021 +    }
1022      # If allowing the inferior to have its own PTY then assign the inferior
1023      # its own terminal device here.
1024      if { $separate_inferior_pty } {
1025 Index: gdb-7.4.91.20120801/gdb/objfiles.h
1026 ===================================================================
1027 --- gdb-7.4.91.20120801.orig/gdb/objfiles.h     2012-08-01 18:36:51.000000000 +0200
1028 +++ gdb-7.4.91.20120801/gdb/objfiles.h  2012-08-01 18:37:44.601925656 +0200
1029 @@ -432,6 +432,10 @@ struct objfile
1030  
1031  #define OBJF_MAINLINE (1 << 5)
1032  
1033 +/* This file was loaded according to the BUILD_ID_CORE_LOADS rules.  */
1034 +
1035 +#define OBJF_BUILD_ID_CORE_LOADED (1 << 12)
1036 +
1037  /* The object file that contains the runtime common minimal symbols
1038     for SunOS4.  Note that this objfile has no associated BFD.  */
1039  
1040 Index: gdb-7.4.91.20120801/gdb/testsuite/gdb.base/corefile.exp
1041 ===================================================================
1042 --- gdb-7.4.91.20120801.orig/gdb/testsuite/gdb.base/corefile.exp        2012-06-21 22:46:21.000000000 +0200
1043 +++ gdb-7.4.91.20120801/gdb/testsuite/gdb.base/corefile.exp     2012-08-01 18:37:44.602925650 +0200
1044 @@ -256,3 +256,33 @@ if ![is_remote target] {
1045  
1046      gdb_exit
1047  }
1048 +
1049 +
1050 +# Test auto-loading of binary files through build-id from the core file.
1051 +set buildid [build_id_debug_filename_get $binfile]
1052 +set wholetest "binfile found by build-id"
1053 +if {$buildid == ""} {
1054 +    untested "$wholetest (binary has no build-id)"
1055 +} else {
1056 +    gdb_exit
1057 +    gdb_start
1058 +
1059 +    regsub {\.debug$} $buildid {} buildid
1060 +    set debugdir ${objdir}/${subdir}/${testfile}-debugdir
1061 +    file delete -force -- $debugdir
1062 +    file mkdir $debugdir/[file dirname $buildid]
1063 +    file copy $binfile $debugdir/$buildid
1064 +
1065 +    set test "show debug-file-directory"
1066 +    gdb_test_multiple $test $test {
1067 +       -re "The directory where separate debug symbols are searched for is \"(.*)\"\\.\r\n$gdb_prompt $" {
1068 +           set debugdir_orig $expect_out(1,string)
1069 +           pass $test
1070 +       }
1071 +    }
1072 +    gdb_test_no_output "set debug-file-directory $debugdir:$debugdir_orig" "set debug-file-directory"
1073 +    gdb_test "show build-id-core-loads" {Whether CORE-FILE loads the build-id associated files automatically is on\.}
1074 +    gdb_test "core-file $corefile" "\r\nProgram terminated with .*" "core-file without executable"
1075 +    gdb_test "info files" "Local exec file:\r\n\[ \t\]*`[string_to_regexp $debugdir/$buildid]', file type .*"
1076 +    pass $wholetest
1077 +}
This page took 0.123945 seconds and 3 git commands to generate.