]> git.pld-linux.org Git - packages/gdb.git/blob - gdb-6.6-buildid-locate.patch
- obsolete file
[packages/gdb.git] / gdb-6.6-buildid-locate.patch
1 Index: gdb-6.8.50.20090302/gdb/corelow.c
2 ===================================================================
3 --- gdb-6.8.50.20090302.orig/gdb/corelow.c      2009-02-23 01:03:48.000000000 +0100
4 +++ gdb-6.8.50.20090302/gdb/corelow.c   2009-03-07 17:04:57.000000000 +0100
5 @@ -45,6 +45,10 @@
6  #include "exceptions.h"
7  #include "solib.h"
8  #include "filenames.h"
9 +#include "auxv.h"
10 +#include "elf/common.h"
11 +#include "objfiles.h"
12 +#include "gdbcmd.h"
13  
14  
15  #ifndef O_LARGEFILE
16 @@ -267,6 +271,56 @@ add_to_thread_list (bfd *abfd, asection 
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 build_id *build_id;
27 +  char *exec_filename, *debug_filename;
28 +  char *build_id_filename;
29 +
30 +  if (exec_bfd != 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 +  exec_filename = build_id_to_filename (build_id, &build_id_filename, 0);
41 +  if (exec_filename != NULL)
42 +    exec_file_attach (exec_filename, from_tty);
43 +  else
44 +    debug_print_missing (_("the main executable file"), build_id_filename);
45 +  xfree (build_id_filename);
46 +
47 +  /* `.note.gnu.build-id' section exists even for files without a separate
48 +     debuginfo.  */
49 +  debug_filename = build_id_to_filename (build_id, &build_id_filename, 1);
50 +  if (debug_filename != NULL)
51 +    {
52 +      symbol_file_add_main (debug_filename, from_tty);
53 +      xfree (debug_filename);
54 +    }
55 +  else
56 +    {
57 +      if (exec_filename != NULL)
58 +       symbol_file_add_main (exec_filename, from_tty);
59 +      /* For EXEC_FILENAME NULL we were already complaining above.  */
60 +      if (symfile_objfile == NULL && exec_filename != NULL)
61 +       debug_print_missing (exec_filename, build_id_filename);
62 +    }
63 +  xfree (build_id_filename);
64 +
65 +  xfree (exec_filename);
66 +
67 +  /* No automatic SOLIB_ADD as the libraries would get read twice.  */
68 +}
69 +
70  /* This routine opens and sets up the core file bfd.  */
71  
72  static void
73 @@ -363,6 +417,12 @@ core_open (char *filename, int from_tty)
74    push_target (&core_ops);
75    discard_cleanups (old_chain);
76  
77 +  /* Find the build_id identifiers.  If it gets executed after
78 +     POST_CREATE_INFERIOR we would clash with asking to discard the already
79 +     loaded VDSO symbols.  */
80 +  if (build_id_core_loads != 0)
81 +    build_id_locate_exec (from_tty);
82 +
83    add_inferior_silent (corelow_pid);
84  
85    /* Do this before acknowledging the inferior, so if
86 @@ -751,4 +811,11 @@ _initialize_corelow (void)
87    init_core_ops ();
88  
89    add_target (&core_ops);
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-6.8.50.20090302/gdb/doc/gdb.texinfo
99 ===================================================================
100 --- gdb-6.8.50.20090302.orig/gdb/doc/gdb.texinfo        2009-03-07 17:04:56.000000000 +0100
101 +++ gdb-6.8.50.20090302/gdb/doc/gdb.texinfo     2009-03-07 17:04:57.000000000 +0100
102 @@ -13294,6 +13294,27 @@ information files.
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-6.8.50.20090302/gdb/solib-svr4.c
131 ===================================================================
132 --- gdb-6.8.50.20090302.orig/gdb/solib-svr4.c   2009-03-07 17:04:52.000000000 +0100
133 +++ gdb-6.8.50.20090302/gdb/solib-svr4.c        2009-03-07 17:04:57.000000000 +0100
134 @@ -1000,9 +1000,33 @@ svr4_current_sos (void)
135                      safe_strerror (errcode));
136           else
137             {
138 -             strncpy (new->so_name, buffer, SO_NAME_MAX_PATH_SIZE - 1);
139 -             new->so_name[SO_NAME_MAX_PATH_SIZE - 1] = '\0';
140 -             strcpy (new->so_original_name, new->so_name);
141 +             struct build_id *build_id;
142 +
143 +             strncpy (new->so_original_name, buffer, SO_NAME_MAX_PATH_SIZE - 1);
144 +             new->so_original_name[SO_NAME_MAX_PATH_SIZE - 1] = '\0';
145 +             /* May get overwritten below.  */
146 +             strcpy (new->so_name, new->so_original_name);
147 +
148 +             build_id = build_id_addr_get (LM_DYNAMIC_FROM_LINK_MAP (new));
149 +             if (build_id != NULL)
150 +               {
151 +                 char *name, *build_id_filename;
152 +
153 +                 /* Missing the build-id matching separate debug info file
154 +                    would be handled while SO_NAME gets loaded.  */
155 +                 name = build_id_to_filename (build_id, &build_id_filename, 0);
156 +                 if (name != NULL)
157 +                   {
158 +                     strncpy (new->so_name, name, SO_NAME_MAX_PATH_SIZE - 1);
159 +                     new->so_name[SO_NAME_MAX_PATH_SIZE - 1] = '\0';
160 +                     xfree (name);
161 +                   }
162 +                 else
163 +                   debug_print_missing (new->so_name, build_id_filename);
164 +
165 +                 xfree (build_id_filename);
166 +                 xfree (build_id);
167 +               }
168             }
169           xfree (buffer);
170  
171 Index: gdb-6.8.50.20090302/gdb/symfile.c
172 ===================================================================
173 --- gdb-6.8.50.20090302.orig/gdb/symfile.c      2009-03-07 17:04:53.000000000 +0100
174 +++ gdb-6.8.50.20090302/gdb/symfile.c   2009-03-07 17:22:21.000000000 +0100
175 @@ -54,6 +54,7 @@
176  #include "elf-bfd.h"
177  #include "solib.h"
178  #include "remote.h"
179 +#include "libbfd.h"
180  
181  #include <sys/types.h>
182  #include <fcntl.h>
183 @@ -1185,16 +1186,65 @@ symbol_file_clear (int from_tty)
184        printf_unfiltered (_("No symbol file now.\n"));
185  }
186  
187 +/* Locate NT_GNU_BUILD_ID and return its matching debug filename.
188 +   FIXME: NOTE decoding should be unified with the BFD core notes decoding.  */
189 +
190 +#define BUILD_ID_VERBOSE_NONE 0
191 +#define BUILD_ID_VERBOSE_FILENAMES 1
192 +#define BUILD_ID_VERBOSE_BINARY_PARSE 2
193 +static int build_id_verbose = BUILD_ID_VERBOSE_FILENAMES;
194 +static void
195 +show_build_id_verbose (struct ui_file *file, int from_tty,
196 +                      struct cmd_list_element *c, const char *value)
197 +{
198 +  fprintf_filtered (file, _("Verbosity level of the build-id locator is %s.\n"),
199 +                   value);
200 +}
201 +
202  struct build_id
203    {
204      size_t size;
205      gdb_byte data[1];
206    };
207  
208 -/* Locate NT_GNU_BUILD_ID from ABFD and return its content.  */
209 +struct build_id *
210 +build_id_buf_get (bfd *templ, gdb_byte *buf, bfd_size_type size)
211 +{
212 +  bfd_byte *p;
213 +
214 +  p = buf;
215 +  while (p < buf + size)
216 +    {
217 +      /* FIXME: bad alignment assumption.  */
218 +      Elf_External_Note *xnp = (Elf_External_Note *) p;
219 +      size_t namesz = H_GET_32 (templ, xnp->namesz);
220 +      size_t descsz = H_GET_32 (templ, xnp->descsz);
221 +      bfd_byte *descdata = xnp->name + BFD_ALIGN (namesz, 4);
222 +
223 +      if (H_GET_32 (templ, xnp->type) == NT_GNU_BUILD_ID
224 +         && namesz == sizeof "GNU"
225 +         && memcmp (xnp->name, "GNU", sizeof "GNU") == 0)
226 +       {
227 +         size_t size = descsz;
228 +         gdb_byte *data = (void *) descdata;
229 +         struct build_id *retval;
230 +
231 +         retval = xmalloc (sizeof *retval - 1 + size);
232 +         retval->size = size;
233 +         memcpy (retval->data, data, size);
234 +
235 +         return retval;
236 +       }
237 +      p = descdata + BFD_ALIGN (descsz, 4);
238 +    }
239 +  return NULL;
240 +}
241 +
242 +/* Separate debuginfo files have corrupted PHDR but SHDR is correct there.
243 +   Locate NT_GNU_BUILD_ID from ABFD and return its content.  */
244  
245  static struct build_id *
246 -build_id_bfd_get (bfd *abfd)
247 +build_id_bfd_shdr_get (bfd *abfd)
248  {
249    struct build_id *retval;
250  
251 @@ -1210,6 +1260,348 @@ build_id_bfd_get (bfd *abfd)
252    return retval;
253  }
254  
255 +/* Core files may have missing (corrupt) SHDR but PDHR is correct there.
256 +   bfd_elf_bfd_from_remote_memory () has too much overhead by
257 +   allocating/reading all the available ELF PT_LOADs.  */
258 +
259 +static struct build_id *
260 +build_id_phdr_get (bfd *templ, bfd_vma loadbase, unsigned e_phnum,
261 +                  Elf_Internal_Phdr *i_phdr)
262 +{
263 +  int i;
264 +  struct build_id *retval = NULL;
265 +
266 +  for (i = 0; i < e_phnum; i++)
267 +    if (i_phdr[i].p_type == PT_NOTE && i_phdr[i].p_filesz > 0)
268 +      {
269 +       Elf_Internal_Phdr *hdr = &i_phdr[i];
270 +       gdb_byte *buf;
271 +       int err;
272 +
273 +       buf = xmalloc (hdr->p_filesz);
274 +       err = target_read_memory (loadbase + i_phdr[i].p_vaddr, buf,
275 +                                 hdr->p_filesz);
276 +       if (err == 0)
277 +         retval = build_id_buf_get (templ, buf, hdr->p_filesz);
278 +       else
279 +         retval = NULL;
280 +       xfree (buf);
281 +       if (retval != NULL)
282 +         break;
283 +      }
284 +  return retval;
285 +}
286 +
287 +/* First we validate the file by reading in the ELF header and checking
288 +   the magic number.  */
289 +
290 +static inline bfd_boolean
291 +elf_file_p (Elf64_External_Ehdr *x_ehdrp64)
292 +{
293 +  gdb_assert (sizeof (Elf64_External_Ehdr) >= sizeof (Elf32_External_Ehdr));
294 +  gdb_assert (offsetof (Elf64_External_Ehdr, e_ident)
295 +             == offsetof (Elf32_External_Ehdr, e_ident));
296 +  gdb_assert (sizeof (((Elf64_External_Ehdr *) 0)->e_ident)
297 +             == sizeof (((Elf32_External_Ehdr *) 0)->e_ident));
298 +
299 +  return ((x_ehdrp64->e_ident[EI_MAG0] == ELFMAG0)
300 +         && (x_ehdrp64->e_ident[EI_MAG1] == ELFMAG1)
301 +         && (x_ehdrp64->e_ident[EI_MAG2] == ELFMAG2)
302 +         && (x_ehdrp64->e_ident[EI_MAG3] == ELFMAG3));
303 +}
304 +
305 +/* Translate an ELF file header in external format into an ELF file header in
306 +   internal format.  */
307 +
308 +#define H_GET_WORD(bfd, ptr) (is64 ? H_GET_64 (bfd, (ptr))             \
309 +                                  : H_GET_32 (bfd, (ptr)))
310 +#define H_GET_SIGNED_WORD(bfd, ptr) (is64 ? H_GET_S64 (bfd, (ptr))     \
311 +                                         : H_GET_S32 (bfd, (ptr)))
312 +
313 +static void
314 +elf_swap_ehdr_in (bfd *abfd,
315 +                 const Elf64_External_Ehdr *src64,
316 +                 Elf_Internal_Ehdr *dst)
317 +{
318 +  int is64 = bfd_get_arch_size (abfd) == 64;
319 +#define SRC(field) (is64 ? src64->field \
320 +                        : ((const Elf32_External_Ehdr *) src64)->field)
321 +
322 +  int signed_vma = get_elf_backend_data (abfd)->sign_extend_vma;
323 +  memcpy (dst->e_ident, SRC (e_ident), EI_NIDENT);
324 +  dst->e_type = H_GET_16 (abfd, SRC (e_type));
325 +  dst->e_machine = H_GET_16 (abfd, SRC (e_machine));
326 +  dst->e_version = H_GET_32 (abfd, SRC (e_version));
327 +  if (signed_vma)
328 +    dst->e_entry = H_GET_SIGNED_WORD (abfd, SRC (e_entry));
329 +  else
330 +    dst->e_entry = H_GET_WORD (abfd, SRC (e_entry));
331 +  dst->e_phoff = H_GET_WORD (abfd, SRC (e_phoff));
332 +  dst->e_shoff = H_GET_WORD (abfd, SRC (e_shoff));
333 +  dst->e_flags = H_GET_32 (abfd, SRC (e_flags));
334 +  dst->e_ehsize = H_GET_16 (abfd, SRC (e_ehsize));
335 +  dst->e_phentsize = H_GET_16 (abfd, SRC (e_phentsize));
336 +  dst->e_phnum = H_GET_16 (abfd, SRC (e_phnum));
337 +  dst->e_shentsize = H_GET_16 (abfd, SRC (e_shentsize));
338 +  dst->e_shnum = H_GET_16 (abfd, SRC (e_shnum));
339 +  dst->e_shstrndx = H_GET_16 (abfd, SRC (e_shstrndx));
340 +
341 +#undef SRC
342 +}
343 +
344 +/* Translate an ELF program header table entry in external format into an
345 +   ELF program header table entry in internal format.  */
346 +
347 +void
348 +elf_swap_phdr_in (bfd *abfd,
349 +                 const Elf64_External_Phdr *src64,
350 +                 Elf_Internal_Phdr *dst)
351 +{
352 +  int is64 = bfd_get_arch_size (abfd) == 64;
353 +#define SRC(field) (is64 ? src64->field                                        \
354 +                        : ((const Elf32_External_Phdr *) src64)->field)
355 +
356 +  int signed_vma = get_elf_backend_data (abfd)->sign_extend_vma;
357 +
358 +  dst->p_type = H_GET_32 (abfd, SRC (p_type));
359 +  dst->p_flags = H_GET_32 (abfd, SRC (p_flags));
360 +  dst->p_offset = H_GET_WORD (abfd, SRC (p_offset));
361 +  if (signed_vma)
362 +    {
363 +      dst->p_vaddr = H_GET_SIGNED_WORD (abfd, SRC (p_vaddr));
364 +      dst->p_paddr = H_GET_SIGNED_WORD (abfd, SRC (p_paddr));
365 +    }
366 +  else
367 +    {
368 +      dst->p_vaddr = H_GET_WORD (abfd, SRC (p_vaddr));
369 +      dst->p_paddr = H_GET_WORD (abfd, SRC (p_paddr));
370 +    }
371 +  dst->p_filesz = H_GET_WORD (abfd, SRC (p_filesz));
372 +  dst->p_memsz = H_GET_WORD (abfd, SRC (p_memsz));
373 +  dst->p_align = H_GET_WORD (abfd, SRC (p_align));
374 +
375 +#undef SRC
376 +}
377 +
378 +#undef H_GET_SIGNED_WORD
379 +#undef H_GET_WORD
380 +
381 +static Elf_Internal_Phdr *
382 +elf_get_phdr (bfd *templ, bfd_vma ehdr_vma, unsigned *e_phnum_pointer,
383 +              bfd_vma *loadbase_pointer)
384 +{
385 +  /* sizeof (Elf64_External_Ehdr) >= sizeof (Elf32_External_Ehdr)  */
386 +  Elf64_External_Ehdr x_ehdr64;        /* Elf file header, external form */
387 +  Elf_Internal_Ehdr i_ehdr;    /* Elf file header, internal form */
388 +  bfd_size_type x_phdrs_size;
389 +  gdb_byte *x_phdrs_ptr;
390 +  Elf_Internal_Phdr *i_phdrs;
391 +  int err;
392 +  unsigned int i;
393 +  bfd_vma loadbase;
394 +  int loadbase_set;
395 +
396 +  gdb_assert (templ != NULL);
397 +  gdb_assert (sizeof (Elf64_External_Ehdr) >= sizeof (Elf32_External_Ehdr));
398 +
399 +  /* Read in the ELF header in external format.  */
400 +  err = target_read_memory (ehdr_vma, (bfd_byte *) &x_ehdr64, sizeof x_ehdr64);
401 +  if (err)
402 +    {
403 +      if (build_id_verbose >= BUILD_ID_VERBOSE_BINARY_PARSE)
404 +        warning (_("build-id: Error reading ELF header at address 0x%lx"),
405 +                (unsigned long) ehdr_vma);
406 +      return NULL;
407 +    }
408 +
409 +  /* Now check to see if we have a valid ELF file, and one that BFD can
410 +     make use of.  The magic number must match, the address size ('class')
411 +     and byte-swapping must match our XVEC entry.  */
412 +
413 +  if (! elf_file_p (&x_ehdr64)
414 +      || x_ehdr64.e_ident[EI_VERSION] != EV_CURRENT
415 +      || !((bfd_get_arch_size (templ) == 64
416 +            && x_ehdr64.e_ident[EI_CLASS] == ELFCLASS64)
417 +           || (bfd_get_arch_size (templ) == 32
418 +              && x_ehdr64.e_ident[EI_CLASS] == ELFCLASS32)))
419 +    {
420 +      if (build_id_verbose >= BUILD_ID_VERBOSE_BINARY_PARSE)
421 +        warning (_("build-id: Unrecognized ELF header at address 0x%lx"),
422 +                (unsigned long) ehdr_vma);
423 +      return NULL;
424 +    }
425 +
426 +  /* Check that file's byte order matches xvec's */
427 +  switch (x_ehdr64.e_ident[EI_DATA])
428 +    {
429 +    case ELFDATA2MSB:          /* Big-endian */
430 +      if (! bfd_header_big_endian (templ))
431 +       {
432 +         if (build_id_verbose >= BUILD_ID_VERBOSE_BINARY_PARSE)
433 +           warning (_("build-id: Unrecognized "
434 +                      "big-endian ELF header at address 0x%lx"),
435 +                    (unsigned long) ehdr_vma);
436 +         return NULL;
437 +       }
438 +      break;
439 +    case ELFDATA2LSB:          /* Little-endian */
440 +      if (! bfd_header_little_endian (templ))
441 +       {
442 +         if (build_id_verbose >= BUILD_ID_VERBOSE_BINARY_PARSE)
443 +           warning (_("build-id: Unrecognized "
444 +                      "little-endian ELF header at address 0x%lx"),
445 +                    (unsigned long) ehdr_vma);
446 +         return NULL;
447 +       }
448 +      break;
449 +    case ELFDATANONE:          /* No data encoding specified */
450 +    default:                   /* Unknown data encoding specified */
451 +      if (build_id_verbose >= BUILD_ID_VERBOSE_BINARY_PARSE)
452 +       warning (_("build-id: Unrecognized "
453 +                  "ELF header endianity at address 0x%lx"),
454 +                (unsigned long) ehdr_vma);
455 +      return NULL;
456 +    }
457 +
458 +  elf_swap_ehdr_in (templ, &x_ehdr64, &i_ehdr);
459 +
460 +  /* The file header tells where to find the program headers.
461 +     These are what we use to actually choose what to read.  */
462 +
463 +  if (i_ehdr.e_phentsize != (bfd_get_arch_size (templ) == 64
464 +                             ? sizeof (Elf64_External_Phdr)
465 +                            : sizeof (Elf32_External_Phdr))
466 +      || i_ehdr.e_phnum == 0)
467 +    {
468 +      if (build_id_verbose >= BUILD_ID_VERBOSE_BINARY_PARSE)
469 +       warning (_("build-id: Invalid ELF program headers from the ELF header "
470 +                  "at address 0x%lx"), (unsigned long) ehdr_vma);
471 +      return NULL;
472 +    }
473 +
474 +  x_phdrs_size = (bfd_get_arch_size (templ) == 64 ? sizeof (Elf64_External_Phdr)
475 +                                               : sizeof (Elf32_External_Phdr));
476 +
477 +  i_phdrs = xmalloc (i_ehdr.e_phnum * (sizeof *i_phdrs + x_phdrs_size));
478 +  x_phdrs_ptr = (void *) &i_phdrs[i_ehdr.e_phnum];
479 +  err = target_read_memory (ehdr_vma + i_ehdr.e_phoff, (bfd_byte *) x_phdrs_ptr,
480 +                           i_ehdr.e_phnum * x_phdrs_size);
481 +  if (err)
482 +    {
483 +      free (i_phdrs);
484 +      if (build_id_verbose >= BUILD_ID_VERBOSE_BINARY_PARSE)
485 +        warning (_("build-id: Error reading "
486 +                  "ELF program headers at address 0x%lx"),
487 +                (unsigned long) (ehdr_vma + i_ehdr.e_phoff));
488 +      return NULL;
489 +    }
490 +
491 +  loadbase = ehdr_vma;
492 +  loadbase_set = 0;
493 +  for (i = 0; i < i_ehdr.e_phnum; ++i)
494 +    {
495 +      elf_swap_phdr_in (templ, (Elf64_External_Phdr *)
496 +                              (x_phdrs_ptr + i * x_phdrs_size), &i_phdrs[i]);
497 +      /* IA-64 vDSO may have two mappings for one segment, where one mapping
498 +        is executable only, and one is read only.  We must not use the
499 +        executable one (PF_R is the first one, PF_X the second one).  */
500 +      if (i_phdrs[i].p_type == PT_LOAD && (i_phdrs[i].p_flags & PF_R))
501 +       {
502 +         /* Only the first PT_LOAD segment indicates the file bias.
503 +            Next segments may have P_VADDR arbitrarily higher.
504 +            If the first segment has P_VADDR zero any next segment must not
505 +            confuse us, the first one sets LOADBASE certainly enough.  */
506 +         if (!loadbase_set && i_phdrs[i].p_offset == 0)
507 +           {
508 +             loadbase = ehdr_vma - i_phdrs[i].p_vaddr;
509 +             loadbase_set = 1;
510 +           }
511 +       }
512 +    }
513 +
514 +  if (build_id_verbose >= BUILD_ID_VERBOSE_BINARY_PARSE)
515 +    warning (_("build-id: Found ELF header at address 0x%lx, loadbase 0x%lx"),
516 +            (unsigned long) ehdr_vma, (unsigned long) loadbase);
517 +
518 +  *e_phnum_pointer = i_ehdr.e_phnum;
519 +  *loadbase_pointer = loadbase;
520 +  return i_phdrs;
521 +}
522 +
523 +/* BUILD_ID_ADDR_GET gets ADDR located somewhere in the object.
524 +   Find the first section before ADDR containing an ELF header.
525 +   We rely on the fact the sections from multiple files do not mix.
526 +   FIXME: We should check ADDR is contained _inside_ the section with possibly
527 +   missing content (P_FILESZ < P_MEMSZ).  These omitted sections are currently
528 +   hidden by _BFD_ELF_MAKE_SECTION_FROM_PHDR.  */
529 +
530 +static CORE_ADDR build_id_addr;
531 +struct build_id_addr_sect
532 +  {
533 +    struct build_id_addr_sect *next;
534 +    asection *sect;
535 +  };
536 +static struct build_id_addr_sect *build_id_addr_sect;
537 +
538 +static void build_id_addr_candidate (bfd *abfd, asection *sect, void *obj)
539 +{
540 +  if (build_id_addr >= bfd_section_vma (abfd, sect))
541 +    {
542 +      struct build_id_addr_sect *candidate;
543 +
544 +      candidate = xmalloc (sizeof *candidate);
545 +      candidate->next = build_id_addr_sect;
546 +      build_id_addr_sect = candidate;
547 +      candidate->sect = sect;
548 +    }
549 +}
550 +
551 +struct build_id *
552 +build_id_addr_get (CORE_ADDR addr)
553 +{
554 +  struct build_id_addr_sect *candidate;
555 +  struct build_id *retval = NULL;
556 +  Elf_Internal_Phdr *i_phdr = NULL;
557 +  bfd_vma loadbase = 0;
558 +  unsigned e_phnum = 0;
559 +
560 +  if (core_bfd == NULL)
561 +    return NULL;
562 +
563 +  build_id_addr = addr;
564 +  gdb_assert (build_id_addr_sect == NULL);
565 +  bfd_map_over_sections (core_bfd, build_id_addr_candidate, NULL);
566 +
567 +  /* Sections are sorted in the high-to-low VMAs order.
568 +     Stop the search on the first ELF header we find.
569 +     Do not continue the search even if it does not contain NT_GNU_BUILD_ID.  */
570 +
571 +  for (candidate = build_id_addr_sect; candidate != NULL;
572 +       candidate = candidate->next)
573 +    {
574 +      i_phdr = elf_get_phdr (core_bfd,
575 +                            bfd_section_vma (core_bfd, candidate->sect),
576 +                            &e_phnum, &loadbase);
577 +      if (i_phdr != NULL)
578 +       break;
579 +    }
580 +
581 +  if (i_phdr != NULL)
582 +    {
583 +      retval = build_id_phdr_get (core_bfd, loadbase, e_phnum, i_phdr);
584 +      xfree (i_phdr);
585 +    }
586 +
587 +  while (build_id_addr_sect != NULL)
588 +    {
589 +      candidate = build_id_addr_sect;
590 +      build_id_addr_sect = candidate->next;
591 +      xfree (candidate);
592 +    }
593 +
594 +  return retval;
595 +}
596 +
597  /* Return if FILENAME has NT_GNU_BUILD_ID matching the CHECK value.  */
598  
599  static int
600 @@ -1227,7 +1619,7 @@ build_id_verify (const char *filename, s
601    if (abfd == NULL)
602      return 0;
603  
604 -  found = build_id_bfd_get (abfd);
605 +  found = build_id_bfd_shdr_get (abfd);
606  
607    if (found == NULL)
608      warning (_("File \"%s\" has no build-id, file skipped"), filename);
609 @@ -1246,8 +1638,9 @@ build_id_verify (const char *filename, s
610    return retval;
611  }
612  
613 -static char *
614 -build_id_to_debug_filename (struct build_id *build_id)
615 +char *
616 +build_id_to_filename (struct build_id *build_id, char **link_return,
617 +                     int add_debug_suffix)
618  {
619    char *link, *s, *retval = NULL;
620    gdb_byte *data = build_id->data;
621 @@ -1255,7 +1648,9 @@ build_id_to_debug_filename (struct build
622  
623    /* DEBUG_FILE_DIRECTORY/.build-id/ab/cdef */
624    link = xmalloc (strlen (debug_file_directory) + (sizeof "/.build-id/" - 1) + 1
625 -                 + 2 * size + (sizeof ".debug" - 1) + 1);
626 +                 + 2 * size
627 +                 + (add_debug_suffix ? sizeof ".debug" - 1 : 0)
628 +                 + 1);
629    s = link + sprintf (link, "%s/.build-id/", debug_file_directory);
630    if (size > 0)
631      {
632 @@ -1266,12 +1661,14 @@ build_id_to_debug_filename (struct build
633      *s++ = '/';
634    while (size-- > 0)
635      s += sprintf (s, "%02x", (unsigned) *data++);
636 -  strcpy (s, ".debug");
637 +  if (add_debug_suffix)
638 +    strcpy (s, ".debug");
639 +  else
640 +    *s = 0;
641  
642    /* lrealpath() is expensive even for the usually non-existent files.  */
643    if (access (link, F_OK) == 0)
644      retval = lrealpath (link);
645 -  xfree (link);
646  
647    if (retval != NULL && !build_id_verify (retval, build_id))
648      {
649 @@ -1279,9 +1676,142 @@ build_id_to_debug_filename (struct build
650        retval = NULL;
651      }
652  
653 +  if (link_return != NULL)
654 +    *link_return = link;
655 +  else
656 +    xfree (link);
657 +
658 +  return retval;
659 +}
660 +
661 +/* This MISSING_FILEPAIR_HASH tracker is used only for the duplicite messages
662 +     Try to install the hash file ...
663 +   avoidance.  */
664 +
665 +struct missing_filepair
666 +  {
667 +    char *binary;
668 +    char *debug;
669 +    char data[1];
670 +  };
671 +
672 +static struct htab *missing_filepair_hash;
673 +static struct obstack missing_filepair_obstack;
674 +
675 +static void *
676 +missing_filepair_xcalloc (size_t nmemb, size_t nmemb_size)
677 +{
678 +  void *retval;
679 +  size_t size = nmemb * nmemb_size;
680 +
681 +  retval = obstack_alloc (&missing_filepair_obstack, size);
682 +  memset (retval, 0, size);
683    return retval;
684  }
685  
686 +static hashval_t
687 +missing_filepair_hash_func (const struct missing_filepair *elem)
688 +{
689 +  hashval_t retval = 0;
690 +
691 +  retval ^= htab_hash_string (elem->binary);
692 +  if (elem->debug != NULL)
693 +    retval ^= htab_hash_string (elem->debug);
694 +
695 +  return retval;
696 +}
697 +
698 +static int
699 +missing_filepair_eq (const struct missing_filepair *elem1,
700 +                      const struct missing_filepair *elem2)
701 +{
702 +  return strcmp (elem1->binary, elem2->binary) == 0
703 +         && ((elem1->debug == NULL && elem2->debug == NULL)
704 +            || strcmp (elem1->debug, elem2->debug) == 0);
705 +}
706 +
707 +static void
708 +missing_filepair_change (void)
709 +{
710 +  if (missing_filepair_hash != NULL)
711 +    {
712 +      obstack_free (&missing_filepair_obstack, NULL);
713 +      /* All their memory came just from missing_filepair_OBSTACK.  */
714 +      missing_filepair_hash = NULL;
715 +    }
716 +}
717 +
718 +static void
719 +debug_print_executable_changed (void)
720 +{
721 +  missing_filepair_change ();
722 +}
723 +
724 +/* Notify user the file BINARY with (possibly NULL) associated separate debug
725 +   information file DEBUG is missing.  DEBUG may or may not be the build-id
726 +   file such as would be:
727 +     /usr/lib/debug/.build-id/dd/b1d2ce632721c47bb9e8679f369e2295ce71be.debug
728 +   */
729 +
730 +void
731 +debug_print_missing (const char *binary, const char *debug)
732 +{
733 +  size_t binary_len0 = strlen (binary) + 1;
734 +  size_t debug_len0 = debug ? strlen (debug) + 1 : 0;
735 +  struct missing_filepair *missing_filepair;
736 +  struct missing_filepair **slot;
737 +
738 +  if (build_id_verbose < BUILD_ID_VERBOSE_FILENAMES)
739 +    return;
740 +
741 +  if (missing_filepair_hash == NULL)
742 +    {
743 +      obstack_init (&missing_filepair_obstack);
744 +      missing_filepair_hash = htab_create_alloc (64,
745 +       (hashval_t (*) (const void *)) missing_filepair_hash_func,
746 +       (int (*) (const void *, const void *)) missing_filepair_eq, NULL,
747 +       missing_filepair_xcalloc, NULL);
748 +    }
749 +
750 +  missing_filepair = obstack_alloc (&missing_filepair_obstack,
751 +                                     sizeof (*missing_filepair) - 1
752 +                                     + binary_len0 + debug_len0);
753 +  missing_filepair->binary = missing_filepair->data;
754 +  memcpy (missing_filepair->binary, binary, binary_len0);
755 +  if (debug != NULL)
756 +    {
757 +      missing_filepair->debug = missing_filepair->binary + binary_len0;
758 +      memcpy (missing_filepair->debug, debug, debug_len0);
759 +    }
760 +  else
761 +    missing_filepair->debug = NULL;
762 +
763 +  slot = (struct missing_filepair **) htab_find_slot (missing_filepair_hash,
764 +                                                       missing_filepair,
765 +                                                       INSERT);
766 +
767 +  /* While it may be still printed duplicitely with the missing debuginfo file
768 +   * it is due to once printing about the binary file build-id link and once
769 +   * about the .debug file build-id link as both the build-id symlinks are
770 +   * located in the debuginfo package.  */
771 +
772 +  if (*slot != NULL)
773 +    {
774 +      obstack_free (&missing_filepair_obstack, missing_filepair);
775 +      return;
776 +    }
777 +  *slot = missing_filepair;
778 +
779 +  /* We do not collect and flush these messages as each such message
780 +     already requires its own separate lines.  */
781 +
782 +  fprintf_unfiltered (gdb_stdlog,
783 +                     _("Missing separate debuginfo for %s\n"), binary);
784 +  if (debug != NULL)
785 +    fprintf_unfiltered (gdb_stdlog, _("Try to install the hash file %s\n"),
786 +                       debug);
787 +}
788 +
789  static char *
790  get_debug_link_info (struct objfile *objfile, unsigned long *crc32_out)
791  {
792 @@ -1364,32 +1894,36 @@ static char *
793  find_separate_debug_file (struct objfile *objfile)
794  {
795    asection *sect;
796 -  char *basename;
797 -  char *dir;
798 -  char *debugfile;
799 +  char *basename = NULL;
800 +  char *dir = NULL;
801 +  char *debugfile = NULL;
802    char *name_copy;
803 -  char *canon_name;
804 +  char *canon_name = NULL;
805    bfd_size_type debuglink_size;
806    unsigned long crc32;
807    int i;
808    struct build_id *build_id;
809 +  char *build_id_filename = NULL;
810  
811 -  build_id = build_id_bfd_get (objfile->obfd);
812 +  build_id = build_id_bfd_shdr_get (objfile->obfd);
813    if (build_id != NULL)
814      {
815        char *build_id_name;
816  
817 -      build_id_name = build_id_to_debug_filename (build_id);
818 +      build_id_name = build_id_to_filename (build_id, &build_id_filename, 1);
819        xfree (build_id);
820        /* Prevent looping on a stripped .debug file.  */
821        if (build_id_name != NULL && strcmp (build_id_name, objfile->name) == 0)
822          {
823 -         warning (_("\"%s\": separate debug info file has no debug info"),
824 +         warning (_("\"%s\": The separate debug info file has no debug info"),
825                    build_id_name);
826           xfree (build_id_name);
827         }
828        else if (build_id_name != NULL)
829 -        return build_id_name;
830 +        {
831 +         xfree (build_id_filename);
832 +         return build_id_name;
833 +       }
834      }
835  
836    basename = get_debug_link_info (objfile, &crc32);
837 @@ -1397,7 +1931,7 @@ find_separate_debug_file (struct objfile
838    if (basename == NULL)
839      /* There's no separate debug info, hence there's no way we could
840         load it => no warning.  */
841 -    return NULL;
842 +    goto cleanup_return_debugfile;
843  
844    dir = xstrdup (objfile->name);
845  
846 @@ -1413,23 +1947,19 @@ find_separate_debug_file (struct objfile
847    gdb_assert (i >= 0 && IS_DIR_SEPARATOR (dir[i]));
848    dir[i+1] = '\0';
849  
850 -  debugfile = alloca (strlen (debug_file_directory) + 1
851 -                      + strlen (dir)
852 -                      + strlen (DEBUG_SUBDIRECTORY)
853 -                      + strlen ("/")
854 -                      + strlen (basename)
855 -                      + 1);
856 +  debugfile = xmalloc (strlen (debug_file_directory) + 1
857 +                      + strlen (dir)
858 +                      + strlen (DEBUG_SUBDIRECTORY)
859 +                      + strlen ("/")
860 +                      + strlen (basename)
861 +                      + 1);
862  
863    /* First try in the same directory as the original file.  */
864    strcpy (debugfile, dir);
865    strcat (debugfile, basename);
866  
867    if (separate_debug_file_exists (debugfile, crc32, objfile->name))
868 -    {
869 -      xfree (basename);
870 -      xfree (dir);
871 -      return xstrdup (debugfile);
872 -    }
873 +    goto cleanup_return_debugfile;
874  
875    /* Then try in the subdirectory named DEBUG_SUBDIRECTORY.  */
876    strcpy (debugfile, dir);
877 @@ -1438,11 +1968,7 @@ find_separate_debug_file (struct objfile
878    strcat (debugfile, basename);
879  
880    if (separate_debug_file_exists (debugfile, crc32, objfile->name))
881 -    {
882 -      xfree (basename);
883 -      xfree (dir);
884 -      return xstrdup (debugfile);
885 -    }
886 +    goto cleanup_return_debugfile;
887  
888    /* Then try in the global debugfile directory.  */
889    strcpy (debugfile, debug_file_directory);
890 @@ -1451,11 +1977,7 @@ find_separate_debug_file (struct objfile
891    strcat (debugfile, basename);
892  
893    if (separate_debug_file_exists (debugfile, crc32, objfile->name))
894 -    {
895 -      xfree (basename);
896 -      xfree (dir);
897 -      return xstrdup (debugfile);
898 -    }
899 +    goto cleanup_return_debugfile;
900  
901    /* If the file is in the sysroot, try using its base path in the
902       global debugfile directory.  */
903 @@ -1470,20 +1992,18 @@ find_separate_debug_file (struct objfile
904        strcat (debugfile, basename);
905  
906        if (separate_debug_file_exists (debugfile, crc32, objfile->name))
907 -       {
908 -         xfree (canon_name);
909 -         xfree (basename);
910 -         xfree (dir);
911 -         return xstrdup (debugfile);
912 -       }
913 +        goto cleanup_return_debugfile;
914      }
915 -  
916 -  if (canon_name)
917 -    xfree (canon_name);
918  
919 +  debugfile = NULL;
920 +  debug_print_missing (objfile->name, build_id_filename);
921 +
922 +cleanup_return_debugfile:
923 +  xfree (build_id_filename);
924 +  xfree (canon_name);
925    xfree (basename);
926    xfree (dir);
927 -  return NULL;
928 +  return debugfile;
929  }
930  
931  
932 @@ -4216,4 +4736,16 @@ Show printing of symbol loading messages
933                             NULL,
934                             NULL,
935                             &setprintlist, &showprintlist);
936 +
937 +  add_setshow_zinteger_cmd ("build-id-verbose", no_class, &build_id_verbose,
938 +                           _("\
939 +Set debugging level of the build-id locator."), _("\
940 +Show debugging level of the build-id locator."), _("\
941 +Level 1 (default) enables printing the missing debug filenames,\n\
942 +level 2 also prints the parsing of binaries to find the identificators."),
943 +                           NULL,
944 +                           show_build_id_verbose,
945 +                           &setlist, &showlist);
946 +
947 +  observer_attach_executable_changed (debug_print_executable_changed);
948  }
949 Index: gdb-6.8.50.20090302/gdb/symfile.h
950 ===================================================================
951 --- gdb-6.8.50.20090302.orig/gdb/symfile.h      2009-03-07 17:04:52.000000000 +0100
952 +++ gdb-6.8.50.20090302/gdb/symfile.h   2009-03-07 17:13:33.000000000 +0100
953 @@ -372,6 +372,13 @@ extern int symfile_map_offsets_to_segmen
954  struct symfile_segment_data *get_symfile_segment_data (bfd *abfd);
955  void free_symfile_segment_data (struct symfile_segment_data *data);
956  
957 +/* build-id support.  */
958 +struct build_id;
959 +extern struct build_id *build_id_addr_get (CORE_ADDR addr);
960 +extern char *build_id_to_filename (struct build_id *build_id,
961 +                                  char **link_return, int add_debug_suffix);
962 +extern void debug_print_missing (const char *binary, const char *debug);
963 +
964  /* From dwarf2read.c */
965  
966  extern int dwarf2_has_info (struct objfile *);
967 Index: gdb-6.8.50.20090302/gdb/testsuite/lib/gdb.exp
968 ===================================================================
969 --- gdb-6.8.50.20090302.orig/gdb/testsuite/lib/gdb.exp  2009-03-07 17:04:52.000000000 +0100
970 +++ gdb-6.8.50.20090302/gdb/testsuite/lib/gdb.exp       2009-03-07 17:13:33.000000000 +0100
971 @@ -1230,6 +1230,16 @@ proc default_gdb_start { } {
972             warning "Couldn't set the width to 0."
973         }
974      }
975 +    # Turn off the missing warnings as the testsuite does not expect it.
976 +    send_gdb "set build-id-verbose 0\n"
977 +    gdb_expect 10 {
978 +       -re "$gdb_prompt $" {
979 +           verbose "Disabled the missing debug infos warnings." 2
980 +       }
981 +       timeout {
982 +           warning "Could not disable the missing debug infos warnings.."
983 +       }
984 +    }
985      return 0;
986  }
987  
988 --- ./gdb/testsuite/lib/mi-support.exp  2009-03-21 21:09:09.000000000 +0100
989 +++ ./gdb/testsuite/lib/mi-support.exp  2009-03-22 16:37:58.000000000 +0100
990 @@ -221,6 +221,16 @@ proc default_mi_gdb_start { args } {
991             }
992         }
993      }
994 +    # Turn off the missing warnings as the testsuite does not expect it.
995 +    send_gdb "190-gdb-set build-id-verbose 0\n"
996 +    gdb_expect 10 {
997 +       -re ".*190-gdb-set build-id-verbose 0\r\n190\\\^done\r\n$mi_gdb_prompt$" {
998 +           verbose "Disabled the missing debug infos warnings." 2
999 +       }
1000 +       timeout {
1001 +           warning "Could not disable the missing debug infos warnings.."
1002 +       }
1003 +    }
1004  
1005      detect_async
1006  
This page took 0.68214 seconds and 3 git commands to generate.