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