]> git.pld-linux.org Git - packages/gdb.git/blame - gdb-6.6-buildid-locate.patch
- fix build on x32
[packages/gdb.git] / gdb-6.6-buildid-locate.patch
CommitLineData
140f8057 1Index: gdb-7.99.90.20170420/gdb/corelow.c
3a58abaf 2===================================================================
140f8057
JR
3--- gdb-7.99.90.20170420.orig/gdb/corelow.c 2017-04-20 23:00:35.415584426 +0200
4+++ gdb-7.99.90.20170420/gdb/corelow.c 2017-04-20 23:00:43.358629183 +0200
b1b25d28 5@@ -45,6 +45,10 @@
a7de96f0 6 #include "gdb_bfd.h"
321e94d6
BS
7 #include "completer.h"
8 #include "filestuff.h"
3a58abaf
AM
9+#include "auxv.h"
10+#include "elf/common.h"
3a58abaf 11+#include "gdbcmd.h"
321e94d6 12+#include "build-id.h"
3a58abaf 13
3a58abaf 14 #ifndef O_LARGEFILE
f412e1b4 15 #define O_LARGEFILE 0
140f8057 16@@ -266,6 +270,54 @@
6ed6bacf 17 inferior_ptid = ptid; /* Yes, make it current. */
3a58abaf
AM
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;
324d13e1 26+ struct bfd_build_id *build_id;
1dce4dc1 27+ char *execfilename, *debug_filename;
3a58abaf 28+ char *build_id_filename;
7566401a 29+ struct cleanup *back_to;
3a58abaf 30+
7566401a 31+ if (exec_bfd != NULL || symfile_objfile != NULL)
3a58abaf
AM
32+ return;
33+
34+ if (target_auxv_search (&current_target, AT_ENTRY, &at_entry) <= 0)
35+ return;
36+
37+ build_id = build_id_addr_get (at_entry);
38+ if (build_id == NULL)
39+ return;
03ffe914 40+ back_to = make_cleanup (xfree, build_id);
3a58abaf 41+
7566401a
ER
42+ /* SYMFILE_OBJFILE should refer to the main executable (not only to its
43+ separate debug info file). gcc44+ keeps .eh_frame only in the main
44+ executable without its duplicate .debug_frame in the separate debug info
45+ file - such .eh_frame would not be found if SYMFILE_OBJFILE would refer
46+ directly to the separate debug info file. */
47+
321e94d6 48+ execfilename = build_id_to_filename (build_id, &build_id_filename);
03ffe914 49+ make_cleanup (xfree, build_id_filename);
3a58abaf 50+
1dce4dc1 51+ if (execfilename != NULL)
3a58abaf 52+ {
1dce4dc1
KK
53+ make_cleanup (xfree, execfilename);
54+ exec_file_attach (execfilename, from_tty);
140f8057
JR
55+ symbol_file_add_main (execfilename,
56+ symfile_add_flag (!from_tty ? 0 : SYMFILE_VERBOSE));
7566401a
ER
57+ if (symfile_objfile != NULL)
58+ symfile_objfile->flags |= OBJF_BUILD_ID_CORE_LOADED;
3a58abaf
AM
59+ }
60+ else
7566401a 61+ debug_print_missing (_("the main executable file"), build_id_filename);
3a58abaf 62+
7566401a 63+ do_cleanups (back_to);
3a58abaf
AM
64+
65+ /* No automatic SOLIB_ADD as the libraries would get read twice. */
66+}
67+
68 /* This routine opens and sets up the core file bfd. */
69
70 static void
140f8057 71@@ -402,6 +454,14 @@
a7de96f0
PS
72 switch_to_thread (thread->ptid);
73 }
3a58abaf
AM
74
75+ /* Find the build_id identifiers. If it gets executed after
76+ POST_CREATE_INFERIOR we would clash with asking to discard the already
a7de96f0
PS
77+ loaded VDSO symbols. If it gets executed before bfd_map_over_sections
78+ INFERIOR_PTID is still not set and libthread_db initialization crashes on
79+ PID == 0 in ps_pglobal_lookup. */
3a58abaf
AM
80+ if (build_id_core_loads != 0)
81+ build_id_locate_exec (from_tty);
82+
a7de96f0
PS
83 post_create_inferior (&core_ops, from_tty);
84
85 /* Now go through the target stack looking for threads since there
140f8057 86@@ -1079,4 +1139,11 @@
3a58abaf
AM
87 init_core_ops ();
88
321e94d6 89 add_target_with_completer (&core_ops, filename_completer);
3a58abaf
AM
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."), _("\
a7de96f0 94+Show whether CORE-FILE loads the build-id associated files automatically."),
3a58abaf
AM
95+ NULL, NULL, NULL,
96+ &setlist, &showlist);
97 }
140f8057 98Index: gdb-7.99.90.20170420/gdb/doc/gdb.texinfo
3a58abaf 99===================================================================
140f8057
JR
100--- gdb-7.99.90.20170420.orig/gdb/doc/gdb.texinfo 2017-04-20 23:00:35.415584426 +0200
101+++ gdb-7.99.90.20170420/gdb/doc/gdb.texinfo 2017-04-20 23:00:43.366629228 +0200
102@@ -18987,6 +18987,27 @@
3a58abaf
AM
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
140f8057 130Index: gdb-7.99.90.20170420/gdb/solib-svr4.c
3a58abaf 131===================================================================
140f8057
JR
132--- gdb-7.99.90.20170420.orig/gdb/solib-svr4.c 2017-04-20 23:00:35.415584426 +0200
133+++ gdb-7.99.90.20170420/gdb/solib-svr4.c 2017-04-20 23:00:43.367629234 +0200
b1b25d28
JR
134@@ -45,6 +45,7 @@
135 #include "auxv.h"
321e94d6
BS
136 #include "gdb_bfd.h"
137 #include "probe.h"
138+#include "build-id.h"
139
140 static struct link_map_offsets *svr4_fetch_link_map_offsets (void);
141 static int svr4_have_link_map_offsets (void);
140f8057 142@@ -1420,9 +1421,52 @@
f412e1b4
PS
143 continue;
144 }
145
324d13e1
JR
146- strncpy (newobj->so_name, buffer, SO_NAME_MAX_PATH_SIZE - 1);
147- newobj->so_name[SO_NAME_MAX_PATH_SIZE - 1] = '\0';
148- strcpy (newobj->so_original_name, newobj->so_name);
f412e1b4 149+ {
324d13e1 150+ struct bfd_build_id *build_id;
f412e1b4 151+
324d13e1
JR
152+ strncpy (newobj->so_original_name, buffer, SO_NAME_MAX_PATH_SIZE - 1);
153+ newobj->so_original_name[SO_NAME_MAX_PATH_SIZE - 1] = '\0';
f412e1b4 154+ /* May get overwritten below. */
324d13e1 155+ strcpy (newobj->so_name, newobj->so_original_name);
f412e1b4 156+
324d13e1 157+ build_id = build_id_addr_get (newobj->lm_info->l_ld);
f412e1b4
PS
158+ if (build_id != NULL)
159+ {
160+ char *name, *build_id_filename;
161+
162+ /* Missing the build-id matching separate debug info file
163+ would be handled while SO_NAME gets loaded. */
321e94d6 164+ name = build_id_to_filename (build_id, &build_id_filename);
f412e1b4
PS
165+ if (name != NULL)
166+ {
324d13e1
JR
167+ strncpy (newobj->so_name, name, SO_NAME_MAX_PATH_SIZE - 1);
168+ newobj->so_name[SO_NAME_MAX_PATH_SIZE - 1] = '\0';
f412e1b4
PS
169+ xfree (name);
170+ }
171+ else
172+ {
324d13e1 173+ debug_print_missing (newobj->so_name, build_id_filename);
f412e1b4
PS
174+
175+ /* In the case the main executable was found according to
176+ its build-id (from a core file) prevent loading
177+ a different build of a library with accidentally the
178+ same SO_NAME.
179+
180+ It suppresses bogus backtraces (and prints "??" there
181+ instead) if the on-disk files no longer match the
182+ running program version. */
183+
184+ if (symfile_objfile != NULL
185+ && (symfile_objfile->flags
186+ & OBJF_BUILD_ID_CORE_LOADED) != 0)
324d13e1 187+ newobj->so_name[0] = 0;
f412e1b4
PS
188+ }
189+
190+ xfree (build_id_filename);
191+ xfree (build_id);
192+ }
193+ }
194+
195 xfree (buffer);
3a58abaf 196
f412e1b4 197 /* If this entry has no name, or its name matches the name
140f8057 198Index: gdb-7.99.90.20170420/gdb/elfread.c
3a58abaf 199===================================================================
140f8057
JR
200--- gdb-7.99.90.20170420.orig/gdb/elfread.c 2017-04-20 23:00:35.415584426 +0200
201+++ gdb-7.99.90.20170420/gdb/elfread.c 2017-04-20 23:00:43.367629234 +0200
202@@ -1265,8 +1265,9 @@
321e94d6
BS
203 && objfile->separate_debug_objfile == NULL
204 && objfile->separate_debug_objfile_backlink == NULL)
205 {
140f8057
JR
206+ gdb::unique_xmalloc_ptr<char> build_id_filename;
207 gdb::unique_xmalloc_ptr<char> debugfile
208- (find_separate_debug_file_by_buildid (objfile));
209+ (find_separate_debug_file_by_buildid (objfile, &build_id_filename));
321e94d6
BS
210
211 if (debugfile == NULL)
140f8057
JR
212 debugfile.reset (find_separate_debug_file_by_debuglink (objfile));
213@@ -1278,6 +1279,10 @@
214 symbol_file_add_separate (abfd.get (), debugfile.get (),
215 symfile_flags, objfile);
321e94d6
BS
216 }
217+ /* Check if any separate debug info has been extracted out. */
218+ else if (bfd_get_section_by_name (objfile->obfd, ".gnu_debuglink")
219+ != NULL)
140f8057 220+ debug_print_missing (objfile_name (objfile), build_id_filename.get ());
321e94d6
BS
221 }
222 }
223
140f8057 224Index: gdb-7.99.90.20170420/gdb/symfile.h
321e94d6 225===================================================================
140f8057
JR
226--- gdb-7.99.90.20170420.orig/gdb/symfile.h 2017-04-20 23:00:35.415584426 +0200
227+++ gdb-7.99.90.20170420/gdb/symfile.h 2017-04-20 23:00:43.368629240 +0200
228@@ -567,6 +567,10 @@
f727df28
KK
229 void map_symbol_filenames (symbol_filename_ftype *fun, void *data,
230 int need_fullname);
321e94d6
BS
231
232+/* build-id support. */
324d13e1 233+extern struct bfd_build_id *build_id_addr_get (CORE_ADDR addr);
321e94d6
BS
234+extern void debug_print_missing (const char *binary, const char *debug);
235+
236 /* From dwarf2read.c */
237
238 /* Names for a dwarf2 debugging section. The field NORMAL is the normal
140f8057 239Index: gdb-7.99.90.20170420/gdb/testsuite/lib/gdb.exp
321e94d6 240===================================================================
140f8057
JR
241--- gdb-7.99.90.20170420.orig/gdb/testsuite/lib/gdb.exp 2017-04-20 23:00:35.415584426 +0200
242+++ gdb-7.99.90.20170420/gdb/testsuite/lib/gdb.exp 2017-04-20 23:00:43.369629245 +0200
243@@ -1646,6 +1646,16 @@
321e94d6
BS
244 warning "Couldn't set the width to 0."
245 }
246 }
247+ # Turn off the missing warnings as the testsuite does not expect it.
248+ send_gdb "set build-id-verbose 0\n"
249+ gdb_expect 10 {
250+ -re "$gdb_prompt $" {
251+ verbose "Disabled the missing debug infos warnings." 2
252+ }
253+ timeout {
254+ warning "Could not disable the missing debug infos warnings.."
255+ }
256+ }
257 return 0
258 }
259
140f8057 260Index: gdb-7.99.90.20170420/gdb/testsuite/lib/mi-support.exp
321e94d6 261===================================================================
140f8057
JR
262--- gdb-7.99.90.20170420.orig/gdb/testsuite/lib/mi-support.exp 2017-04-20 23:00:35.415584426 +0200
263+++ gdb-7.99.90.20170420/gdb/testsuite/lib/mi-support.exp 2017-04-20 23:00:43.369629245 +0200
264@@ -309,6 +309,16 @@
321e94d6
BS
265 warning "Couldn't set the width to 0."
266 }
267 }
268+ # Turn off the missing warnings as the testsuite does not expect it.
269+ send_gdb "190-gdb-set build-id-verbose 0\n"
270+ gdb_expect 10 {
271+ -re ".*190-gdb-set build-id-verbose 0\r\n190\\\^done\r\n$mi_gdb_prompt$" {
272+ verbose "Disabled the missing debug infos warnings." 2
273+ }
274+ timeout {
275+ warning "Could not disable the missing debug infos warnings.."
276+ }
277+ }
28b292e9 278
321e94d6 279 if { $separate_inferior_pty } {
140f8057
JR
280 mi_create_inferior_pty
281Index: gdb-7.99.90.20170420/gdb/testsuite/gdb.base/new-ui-pending-input.exp
321e94d6 282===================================================================
140f8057
JR
283--- gdb-7.99.90.20170420.orig/gdb/testsuite/gdb.base/new-ui-pending-input.exp 2017-04-20 23:00:35.415584426 +0200
284+++ gdb-7.99.90.20170420/gdb/testsuite/gdb.base/new-ui-pending-input.exp 2017-04-20 23:00:43.369629245 +0200
285@@ -62,6 +62,7 @@
286 set options ""
287 append options " -iex \"set height 0\""
288 append options " -iex \"set width 0\""
289+ append options " -iex \"set build-id-verbose 0\""
290 append options " -iex \"new-ui console $extra_tty_name\""
291 append options " -ex \"b $bpline\""
292 append options " -ex \"run\""
293Index: gdb-7.99.90.20170420/gdb/objfiles.h
294===================================================================
295--- gdb-7.99.90.20170420.orig/gdb/objfiles.h 2017-04-20 23:00:35.415584426 +0200
296+++ gdb-7.99.90.20170420/gdb/objfiles.h 2017-04-20 23:00:43.370629251 +0200
297@@ -444,6 +444,10 @@
298 htab_t static_links;
299 };
321e94d6
BS
300
301+/* This file was loaded according to the BUILD_ID_CORE_LOADS rules. */
302+
140f8057 303+#define OBJF_BUILD_ID_CORE_LOADED static_cast<enum objfile_flag>(1 << 12)
321e94d6
BS
304+
305 /* Declarations for functions defined in objfiles.c */
306
140f8057
JR
307 extern struct objfile *allocate_objfile (bfd *, const char *name,
308Index: gdb-7.99.90.20170420/gdb/testsuite/gdb.base/corefile.exp
321e94d6 309===================================================================
140f8057
JR
310--- gdb-7.99.90.20170420.orig/gdb/testsuite/gdb.base/corefile.exp 2017-04-20 23:00:35.415584426 +0200
311+++ gdb-7.99.90.20170420/gdb/testsuite/gdb.base/corefile.exp 2017-04-20 23:00:43.370629251 +0200
28b292e9 312@@ -293,3 +293,33 @@
321e94d6
BS
313 pass $test
314 }
315 }
316+
317+
318+# Test auto-loading of binary files through build-id from the core file.
319+set buildid [build_id_debug_filename_get $binfile]
320+set wholetest "binfile found by build-id"
321+if {$buildid == ""} {
322+ untested "$wholetest (binary has no build-id)"
323+} else {
324+ gdb_exit
325+ gdb_start
326+
327+ regsub {\.debug$} $buildid {} buildid
28b292e9 328+ set debugdir [standard_output_file ${testfile}-debugdir]
321e94d6
BS
329+ file delete -force -- $debugdir
330+ file mkdir $debugdir/[file dirname $buildid]
331+ file copy $binfile $debugdir/$buildid
332+
333+ set test "show debug-file-directory"
334+ gdb_test_multiple $test $test {
335+ -re "The directory where separate debug symbols are searched for is \"(.*)\"\\.\r\n$gdb_prompt $" {
336+ set debugdir_orig $expect_out(1,string)
337+ pass $test
338+ }
339+ }
340+ gdb_test_no_output "set debug-file-directory $debugdir:$debugdir_orig" "set debug-file-directory"
341+ gdb_test "show build-id-core-loads" {Whether CORE-FILE loads the build-id associated files automatically is on\.}
342+ gdb_test "core-file $corefile" "\r\nProgram terminated with .*" "core-file without executable"
343+ gdb_test "info files" "Local exec file:\r\n\[ \t\]*`[string_to_regexp $debugdir/$buildid]', file type .*"
344+ pass $wholetest
345+}
140f8057 346Index: gdb-7.99.90.20170420/gdb/build-id.c
321e94d6 347===================================================================
140f8057
JR
348--- gdb-7.99.90.20170420.orig/gdb/build-id.c 2017-04-20 23:00:35.415584426 +0200
349+++ gdb-7.99.90.20170420/gdb/build-id.c 2017-04-20 23:03:00.060399474 +0200
324d13e1 350@@ -26,11 +26,67 @@
321e94d6
BS
351 #include "objfiles.h"
352 #include "filenames.h"
324d13e1 353 #include "gdbcore.h"
3a58abaf 354+#include "libbfd.h"
51a5ef0f
PS
355+#include "gdbcore.h"
356+#include "gdbcmd.h"
357+#include "observer.h"
324d13e1
JR
358+#include "elf/external.h"
359+#include "elf/internal.h"
360+#include "elf/common.h"
361+#include "elf-bfd.h"
6ed6bacf 362+#include <sys/stat.h>
321e94d6 363+
3a58abaf
AM
364+#define BUILD_ID_VERBOSE_NONE 0
365+#define BUILD_ID_VERBOSE_FILENAMES 1
366+#define BUILD_ID_VERBOSE_BINARY_PARSE 2
367+static int build_id_verbose = BUILD_ID_VERBOSE_FILENAMES;
368+static void
369+show_build_id_verbose (struct ui_file *file, int from_tty,
370+ struct cmd_list_element *c, const char *value)
371+{
372+ fprintf_filtered (file, _("Verbosity level of the build-id locator is %s.\n"),
373+ value);
374+}
6ed6bacf
AM
375+/* Locate NT_GNU_BUILD_ID and return its matching debug filename.
376+ FIXME: NOTE decoding should be unified with the BFD core notes decoding. */
b1b25d28 377+
324d13e1 378+static struct bfd_build_id *
3a58abaf
AM
379+build_id_buf_get (bfd *templ, gdb_byte *buf, bfd_size_type size)
380+{
381+ bfd_byte *p;
382+
383+ p = buf;
384+ while (p < buf + size)
385+ {
386+ /* FIXME: bad alignment assumption. */
387+ Elf_External_Note *xnp = (Elf_External_Note *) p;
388+ size_t namesz = H_GET_32 (templ, xnp->namesz);
389+ size_t descsz = H_GET_32 (templ, xnp->descsz);
321e94d6 390+ bfd_byte *descdata = (gdb_byte *) xnp->name + BFD_ALIGN (namesz, 4);
3a58abaf
AM
391+
392+ if (H_GET_32 (templ, xnp->type) == NT_GNU_BUILD_ID
393+ && namesz == sizeof "GNU"
394+ && memcmp (xnp->name, "GNU", sizeof "GNU") == 0)
395+ {
396+ size_t size = descsz;
140f8057 397+ gdb_byte *data = (gdb_byte *) descdata;
324d13e1 398+ struct bfd_build_id *retval;
3a58abaf 399+
140f8057 400+ retval = (struct bfd_build_id *) xmalloc (sizeof *retval - 1 + size);
3a58abaf
AM
401+ retval->size = size;
402+ memcpy (retval->data, data, size);
324d13e1 403+
3a58abaf
AM
404+ return retval;
405+ }
406+ p = descdata + BFD_ALIGN (descsz, 4);
407+ }
408+ return NULL;
409+}
324d13e1
JR
410
411 /* See build-id.h. */
412
413 const struct bfd_build_id *
03ffe914 414-build_id_bfd_get (bfd *abfd)
3a58abaf
AM
415+build_id_bfd_shdr_get (bfd *abfd)
416 {
324d13e1
JR
417 if (!bfd_check_format (abfd, bfd_object))
418 return NULL;
28b292e9 419@@ -42,6 +98,348 @@
324d13e1 420 return NULL;
3a58abaf
AM
421 }
422
423+/* Core files may have missing (corrupt) SHDR but PDHR is correct there.
424+ bfd_elf_bfd_from_remote_memory () has too much overhead by
425+ allocating/reading all the available ELF PT_LOADs. */
426+
324d13e1 427+static struct bfd_build_id *
3a58abaf
AM
428+build_id_phdr_get (bfd *templ, bfd_vma loadbase, unsigned e_phnum,
429+ Elf_Internal_Phdr *i_phdr)
430+{
431+ int i;
324d13e1 432+ struct bfd_build_id *retval = NULL;
3a58abaf
AM
433+
434+ for (i = 0; i < e_phnum; i++)
435+ if (i_phdr[i].p_type == PT_NOTE && i_phdr[i].p_filesz > 0)
436+ {
437+ Elf_Internal_Phdr *hdr = &i_phdr[i];
438+ gdb_byte *buf;
439+ int err;
440+
140f8057 441+ buf = (gdb_byte *) xmalloc (hdr->p_filesz);
3a58abaf
AM
442+ err = target_read_memory (loadbase + i_phdr[i].p_vaddr, buf,
443+ hdr->p_filesz);
444+ if (err == 0)
445+ retval = build_id_buf_get (templ, buf, hdr->p_filesz);
446+ else
447+ retval = NULL;
448+ xfree (buf);
449+ if (retval != NULL)
450+ break;
451+ }
452+ return retval;
453+}
454+
455+/* First we validate the file by reading in the ELF header and checking
456+ the magic number. */
457+
458+static inline bfd_boolean
459+elf_file_p (Elf64_External_Ehdr *x_ehdrp64)
460+{
461+ gdb_assert (sizeof (Elf64_External_Ehdr) >= sizeof (Elf32_External_Ehdr));
462+ gdb_assert (offsetof (Elf64_External_Ehdr, e_ident)
463+ == offsetof (Elf32_External_Ehdr, e_ident));
464+ gdb_assert (sizeof (((Elf64_External_Ehdr *) 0)->e_ident)
465+ == sizeof (((Elf32_External_Ehdr *) 0)->e_ident));
466+
467+ return ((x_ehdrp64->e_ident[EI_MAG0] == ELFMAG0)
468+ && (x_ehdrp64->e_ident[EI_MAG1] == ELFMAG1)
469+ && (x_ehdrp64->e_ident[EI_MAG2] == ELFMAG2)
470+ && (x_ehdrp64->e_ident[EI_MAG3] == ELFMAG3));
471+}
472+
473+/* Translate an ELF file header in external format into an ELF file header in
474+ internal format. */
475+
476+#define H_GET_WORD(bfd, ptr) (is64 ? H_GET_64 (bfd, (ptr)) \
477+ : H_GET_32 (bfd, (ptr)))
478+#define H_GET_SIGNED_WORD(bfd, ptr) (is64 ? H_GET_S64 (bfd, (ptr)) \
479+ : H_GET_S32 (bfd, (ptr)))
480+
481+static void
482+elf_swap_ehdr_in (bfd *abfd,
483+ const Elf64_External_Ehdr *src64,
484+ Elf_Internal_Ehdr *dst)
485+{
486+ int is64 = bfd_get_arch_size (abfd) == 64;
487+#define SRC(field) (is64 ? src64->field \
488+ : ((const Elf32_External_Ehdr *) src64)->field)
489+
490+ int signed_vma = get_elf_backend_data (abfd)->sign_extend_vma;
491+ memcpy (dst->e_ident, SRC (e_ident), EI_NIDENT);
492+ dst->e_type = H_GET_16 (abfd, SRC (e_type));
493+ dst->e_machine = H_GET_16 (abfd, SRC (e_machine));
494+ dst->e_version = H_GET_32 (abfd, SRC (e_version));
495+ if (signed_vma)
496+ dst->e_entry = H_GET_SIGNED_WORD (abfd, SRC (e_entry));
497+ else
498+ dst->e_entry = H_GET_WORD (abfd, SRC (e_entry));
499+ dst->e_phoff = H_GET_WORD (abfd, SRC (e_phoff));
500+ dst->e_shoff = H_GET_WORD (abfd, SRC (e_shoff));
501+ dst->e_flags = H_GET_32 (abfd, SRC (e_flags));
502+ dst->e_ehsize = H_GET_16 (abfd, SRC (e_ehsize));
503+ dst->e_phentsize = H_GET_16 (abfd, SRC (e_phentsize));
504+ dst->e_phnum = H_GET_16 (abfd, SRC (e_phnum));
505+ dst->e_shentsize = H_GET_16 (abfd, SRC (e_shentsize));
506+ dst->e_shnum = H_GET_16 (abfd, SRC (e_shnum));
507+ dst->e_shstrndx = H_GET_16 (abfd, SRC (e_shstrndx));
508+
509+#undef SRC
510+}
511+
512+/* Translate an ELF program header table entry in external format into an
513+ ELF program header table entry in internal format. */
514+
a7de96f0 515+static void
3a58abaf
AM
516+elf_swap_phdr_in (bfd *abfd,
517+ const Elf64_External_Phdr *src64,
518+ Elf_Internal_Phdr *dst)
519+{
520+ int is64 = bfd_get_arch_size (abfd) == 64;
521+#define SRC(field) (is64 ? src64->field \
522+ : ((const Elf32_External_Phdr *) src64)->field)
523+
524+ int signed_vma = get_elf_backend_data (abfd)->sign_extend_vma;
525+
526+ dst->p_type = H_GET_32 (abfd, SRC (p_type));
527+ dst->p_flags = H_GET_32 (abfd, SRC (p_flags));
528+ dst->p_offset = H_GET_WORD (abfd, SRC (p_offset));
529+ if (signed_vma)
530+ {
531+ dst->p_vaddr = H_GET_SIGNED_WORD (abfd, SRC (p_vaddr));
532+ dst->p_paddr = H_GET_SIGNED_WORD (abfd, SRC (p_paddr));
533+ }
534+ else
535+ {
536+ dst->p_vaddr = H_GET_WORD (abfd, SRC (p_vaddr));
537+ dst->p_paddr = H_GET_WORD (abfd, SRC (p_paddr));
538+ }
539+ dst->p_filesz = H_GET_WORD (abfd, SRC (p_filesz));
540+ dst->p_memsz = H_GET_WORD (abfd, SRC (p_memsz));
541+ dst->p_align = H_GET_WORD (abfd, SRC (p_align));
542+
543+#undef SRC
544+}
545+
546+#undef H_GET_SIGNED_WORD
547+#undef H_GET_WORD
548+
549+static Elf_Internal_Phdr *
550+elf_get_phdr (bfd *templ, bfd_vma ehdr_vma, unsigned *e_phnum_pointer,
551+ bfd_vma *loadbase_pointer)
552+{
553+ /* sizeof (Elf64_External_Ehdr) >= sizeof (Elf32_External_Ehdr) */
554+ Elf64_External_Ehdr x_ehdr64; /* Elf file header, external form */
555+ Elf_Internal_Ehdr i_ehdr; /* Elf file header, internal form */
556+ bfd_size_type x_phdrs_size;
557+ gdb_byte *x_phdrs_ptr;
558+ Elf_Internal_Phdr *i_phdrs;
559+ int err;
560+ unsigned int i;
561+ bfd_vma loadbase;
562+ int loadbase_set;
563+
564+ gdb_assert (templ != NULL);
565+ gdb_assert (sizeof (Elf64_External_Ehdr) >= sizeof (Elf32_External_Ehdr));
566+
567+ /* Read in the ELF header in external format. */
568+ err = target_read_memory (ehdr_vma, (bfd_byte *) &x_ehdr64, sizeof x_ehdr64);
569+ if (err)
570+ {
571+ if (build_id_verbose >= BUILD_ID_VERBOSE_BINARY_PARSE)
572+ warning (_("build-id: Error reading ELF header at address 0x%lx"),
573+ (unsigned long) ehdr_vma);
574+ return NULL;
575+ }
576+
577+ /* Now check to see if we have a valid ELF file, and one that BFD can
578+ make use of. The magic number must match, the address size ('class')
579+ and byte-swapping must match our XVEC entry. */
580+
581+ if (! elf_file_p (&x_ehdr64)
582+ || x_ehdr64.e_ident[EI_VERSION] != EV_CURRENT
583+ || !((bfd_get_arch_size (templ) == 64
584+ && x_ehdr64.e_ident[EI_CLASS] == ELFCLASS64)
585+ || (bfd_get_arch_size (templ) == 32
586+ && x_ehdr64.e_ident[EI_CLASS] == ELFCLASS32)))
587+ {
588+ if (build_id_verbose >= BUILD_ID_VERBOSE_BINARY_PARSE)
589+ warning (_("build-id: Unrecognized ELF header at address 0x%lx"),
590+ (unsigned long) ehdr_vma);
591+ return NULL;
592+ }
593+
594+ /* Check that file's byte order matches xvec's */
595+ switch (x_ehdr64.e_ident[EI_DATA])
596+ {
597+ case ELFDATA2MSB: /* Big-endian */
598+ if (! bfd_header_big_endian (templ))
599+ {
600+ if (build_id_verbose >= BUILD_ID_VERBOSE_BINARY_PARSE)
601+ warning (_("build-id: Unrecognized "
602+ "big-endian ELF header at address 0x%lx"),
603+ (unsigned long) ehdr_vma);
604+ return NULL;
605+ }
606+ break;
607+ case ELFDATA2LSB: /* Little-endian */
608+ if (! bfd_header_little_endian (templ))
609+ {
610+ if (build_id_verbose >= BUILD_ID_VERBOSE_BINARY_PARSE)
611+ warning (_("build-id: Unrecognized "
612+ "little-endian ELF header at address 0x%lx"),
613+ (unsigned long) ehdr_vma);
614+ return NULL;
615+ }
616+ break;
617+ case ELFDATANONE: /* No data encoding specified */
618+ default: /* Unknown data encoding specified */
619+ if (build_id_verbose >= BUILD_ID_VERBOSE_BINARY_PARSE)
620+ warning (_("build-id: Unrecognized "
621+ "ELF header endianity at address 0x%lx"),
622+ (unsigned long) ehdr_vma);
623+ return NULL;
624+ }
625+
626+ elf_swap_ehdr_in (templ, &x_ehdr64, &i_ehdr);
627+
628+ /* The file header tells where to find the program headers.
629+ These are what we use to actually choose what to read. */
630+
631+ if (i_ehdr.e_phentsize != (bfd_get_arch_size (templ) == 64
632+ ? sizeof (Elf64_External_Phdr)
633+ : sizeof (Elf32_External_Phdr))
634+ || i_ehdr.e_phnum == 0)
635+ {
636+ if (build_id_verbose >= BUILD_ID_VERBOSE_BINARY_PARSE)
637+ warning (_("build-id: Invalid ELF program headers from the ELF header "
638+ "at address 0x%lx"), (unsigned long) ehdr_vma);
639+ return NULL;
640+ }
641+
642+ x_phdrs_size = (bfd_get_arch_size (templ) == 64 ? sizeof (Elf64_External_Phdr)
643+ : sizeof (Elf32_External_Phdr));
644+
140f8057
JR
645+ i_phdrs = (Elf_Internal_Phdr *) xmalloc (i_ehdr.e_phnum * (sizeof *i_phdrs + x_phdrs_size));
646+ x_phdrs_ptr = (gdb_byte *) &i_phdrs[i_ehdr.e_phnum];
3a58abaf
AM
647+ err = target_read_memory (ehdr_vma + i_ehdr.e_phoff, (bfd_byte *) x_phdrs_ptr,
648+ i_ehdr.e_phnum * x_phdrs_size);
649+ if (err)
650+ {
651+ free (i_phdrs);
652+ if (build_id_verbose >= BUILD_ID_VERBOSE_BINARY_PARSE)
653+ warning (_("build-id: Error reading "
654+ "ELF program headers at address 0x%lx"),
655+ (unsigned long) (ehdr_vma + i_ehdr.e_phoff));
656+ return NULL;
657+ }
658+
659+ loadbase = ehdr_vma;
660+ loadbase_set = 0;
661+ for (i = 0; i < i_ehdr.e_phnum; ++i)
662+ {
663+ elf_swap_phdr_in (templ, (Elf64_External_Phdr *)
664+ (x_phdrs_ptr + i * x_phdrs_size), &i_phdrs[i]);
665+ /* IA-64 vDSO may have two mappings for one segment, where one mapping
666+ is executable only, and one is read only. We must not use the
667+ executable one (PF_R is the first one, PF_X the second one). */
668+ if (i_phdrs[i].p_type == PT_LOAD && (i_phdrs[i].p_flags & PF_R))
669+ {
670+ /* Only the first PT_LOAD segment indicates the file bias.
671+ Next segments may have P_VADDR arbitrarily higher.
672+ If the first segment has P_VADDR zero any next segment must not
673+ confuse us, the first one sets LOADBASE certainly enough. */
674+ if (!loadbase_set && i_phdrs[i].p_offset == 0)
675+ {
676+ loadbase = ehdr_vma - i_phdrs[i].p_vaddr;
677+ loadbase_set = 1;
678+ }
679+ }
680+ }
681+
682+ if (build_id_verbose >= BUILD_ID_VERBOSE_BINARY_PARSE)
683+ warning (_("build-id: Found ELF header at address 0x%lx, loadbase 0x%lx"),
684+ (unsigned long) ehdr_vma, (unsigned long) loadbase);
685+
686+ *e_phnum_pointer = i_ehdr.e_phnum;
687+ *loadbase_pointer = loadbase;
688+ return i_phdrs;
689+}
690+
691+/* BUILD_ID_ADDR_GET gets ADDR located somewhere in the object.
692+ Find the first section before ADDR containing an ELF header.
693+ We rely on the fact the sections from multiple files do not mix.
694+ FIXME: We should check ADDR is contained _inside_ the section with possibly
695+ missing content (P_FILESZ < P_MEMSZ). These omitted sections are currently
696+ hidden by _BFD_ELF_MAKE_SECTION_FROM_PHDR. */
697+
698+static CORE_ADDR build_id_addr;
699+struct build_id_addr_sect
700+ {
701+ struct build_id_addr_sect *next;
702+ asection *sect;
703+ };
704+static struct build_id_addr_sect *build_id_addr_sect;
705+
706+static void build_id_addr_candidate (bfd *abfd, asection *sect, void *obj)
707+{
708+ if (build_id_addr >= bfd_section_vma (abfd, sect))
709+ {
710+ struct build_id_addr_sect *candidate;
711+
140f8057 712+ candidate = (struct build_id_addr_sect *) xmalloc (sizeof *candidate);
3a58abaf
AM
713+ candidate->next = build_id_addr_sect;
714+ build_id_addr_sect = candidate;
715+ candidate->sect = sect;
716+ }
717+}
718+
324d13e1 719+struct bfd_build_id *
3a58abaf
AM
720+build_id_addr_get (CORE_ADDR addr)
721+{
722+ struct build_id_addr_sect *candidate;
324d13e1 723+ struct bfd_build_id *retval = NULL;
3a58abaf
AM
724+ Elf_Internal_Phdr *i_phdr = NULL;
725+ bfd_vma loadbase = 0;
726+ unsigned e_phnum = 0;
727+
728+ if (core_bfd == NULL)
729+ return NULL;
730+
731+ build_id_addr = addr;
732+ gdb_assert (build_id_addr_sect == NULL);
733+ bfd_map_over_sections (core_bfd, build_id_addr_candidate, NULL);
734+
735+ /* Sections are sorted in the high-to-low VMAs order.
736+ Stop the search on the first ELF header we find.
737+ Do not continue the search even if it does not contain NT_GNU_BUILD_ID. */
738+
739+ for (candidate = build_id_addr_sect; candidate != NULL;
740+ candidate = candidate->next)
741+ {
742+ i_phdr = elf_get_phdr (core_bfd,
743+ bfd_section_vma (core_bfd, candidate->sect),
744+ &e_phnum, &loadbase);
745+ if (i_phdr != NULL)
746+ break;
747+ }
748+
749+ if (i_phdr != NULL)
750+ {
751+ retval = build_id_phdr_get (core_bfd, loadbase, e_phnum, i_phdr);
752+ xfree (i_phdr);
753+ }
754+
755+ while (build_id_addr_sect != NULL)
756+ {
757+ candidate = build_id_addr_sect;
758+ build_id_addr_sect = candidate->next;
759+ xfree (candidate);
760+ }
761+
762+ return retval;
763+}
764+
321e94d6 765 /* See build-id.h. */
3a58abaf 766
321e94d6 767 int
28b292e9 768@@ -50,7 +448,7 @@
324d13e1 769 const struct bfd_build_id *found;
321e94d6 770 int retval = 0;
3a58abaf
AM
771
772- found = build_id_bfd_get (abfd);
773+ found = build_id_bfd_shdr_get (abfd);
774
775 if (found == NULL)
321e94d6 776 warning (_("File \"%s\" has no build-id, file skipped"),
140f8057 777@@ -65,23 +463,54 @@
321e94d6 778 return retval;
3a58abaf
AM
779 }
780
321e94d6 781+static char *
03ffe914
JR
782+link_resolve (const char *symlink, int level)
783+{
784+ char buf[PATH_MAX + 1], *target, *retval;
785+ ssize_t got;
786+
787+ if (level > 10)
788+ return xstrdup (symlink);
789+
790+ got = readlink (symlink, buf, sizeof (buf));
791+ if (got < 0 || got >= sizeof (buf))
792+ return xstrdup (symlink);
793+ buf[got] = '\0';
794+
795+ if (IS_ABSOLUTE_PATH (buf))
796+ target = xstrdup (buf);
797+ else
798+ {
140f8057 799+ const std::string dir (ldirname (symlink));
03ffe914 800+
03ffe914
JR
801+ target = xstrprintf ("%s"
802+#ifndef HAVE_DOS_BASED_FILE_SYSTEM
803+ "/"
804+#else /* HAVE_DOS_BASED_FILE_SYSTEM */
805+ "\\"
806+#endif /* HAVE_DOS_BASED_FILE_SYSTEM */
140f8057 807+ "%s", dir.c_str(), buf);
03ffe914
JR
808+ }
809+
810+ retval = link_resolve (target, level + 1);
811+ xfree (target);
812+ return retval;
813+}
814+
321e94d6
BS
815 /* See build-id.h. */
816
140f8057 817 gdb_bfd_ref_ptr
321e94d6
BS
818-build_id_to_debug_bfd (size_t build_id_len, const bfd_byte *build_id)
819+build_id_to_debug_bfd (size_t build_id_len, const bfd_byte *build_id,
820+ char **link_return, int add_debug_suffix)
3a58abaf 821 {
321e94d6
BS
822- char *link, *debugdir;
823+ char *link, *debugdir, *link_all = NULL;
a7de96f0
PS
824 VEC (char_ptr) *debugdir_vec;
825 struct cleanup *back_to;
826 int ix;
140f8057 827 gdb_bfd_ref_ptr abfd;
28b292e9 828- int alloc_len;
3a58abaf
AM
829
830 /* DEBUG_FILE_DIRECTORY/.build-id/ab/cdef */
28b292e9
AM
831- alloc_len = (strlen (debug_file_directory)
832- + (sizeof "/.build-id/" - 1) + 1
833- + 2 * build_id_len + (sizeof ".debug" - 1) + 1);
834- link = (char *) alloca (alloc_len);
140f8057 835+ link = (char *) xmalloc (strlen (debug_file_directory) + 2 * build_id_len + 50);
ab050a48
BZ
836
837 /* Keep backward compatibility so that DEBUG_FILE_DIRECTORY being "" will
838 cause "/.build-id/..." lookups. */
140f8057 839@@ -94,9 +523,12 @@
a7de96f0 840 size_t debugdir_len = strlen (debugdir);
321e94d6
BS
841 const gdb_byte *data = build_id;
842 size_t size = build_id_len;
03ffe914 843- char *s;
321e94d6 844 char *filename = NULL;
324d13e1 845 struct cleanup *inner;
6ed6bacf
AM
846+ unsigned seqno;
847+ struct stat statbuf_trash;
03ffe914
JR
848+ /* Initialize it just to avoid a GCC false warning. */
849+ char *s, *link0 = NULL, *link0_resolved;
6ed6bacf 850
a7de96f0 851 memcpy (link, debugdir, debugdir_len);
03ffe914 852 s = &link[debugdir_len];
140f8057 853@@ -110,45 +542,290 @@
ab050a48
BZ
854 *s++ = '/';
855 while (size-- > 0)
856 s += sprintf (s, "%02x", (unsigned) *data++);
857- strcpy (s, ".debug");
6ed6bacf 858+ for (seqno = 0;; seqno++)
a7de96f0 859+ {
6ed6bacf 860+ char *s2;
321e94d6
BS
861
862- /* lrealpath() is expensive even for the usually non-existent files. */
863- if (access (link, F_OK) == 0)
864- filename = lrealpath (link);
6ed6bacf
AM
865+ if (seqno)
866+ {
867+ /* There can be multiple build-id symlinks pointing to real files
868+ with the same build-id (such as hard links). Some of the real
869+ files may not be installed. */
870+
871+ s2 = s + sprintf (s, ".%u", seqno);
872+ }
873+ else
874+ s2 = s;
875+
876+ if (add_debug_suffix)
877+ strcpy (s2, ".debug");
878+ else
879+ *s2 = 0;
03ffe914
JR
880+
881+ if (!seqno)
882+ {
883+ /* If none of the real files is found report as missing file
884+ always the non-.%u-suffixed file. */
885+ link0 = xstrdup (link);
886+ }
887+
6ed6bacf
AM
888+ /* `access' automatically dereferences LINK. */
889+ if (lstat (link, &statbuf_trash) != 0)
890+ {
891+ /* Stop increasing SEQNO. */
892+ break;
893+ }
894+
321e94d6
BS
895+ filename = lrealpath (link);
896+ if (filename == NULL)
897+ continue;
6ed6bacf 898+
321e94d6 899+ /* We expect to be silent on the non-existing files. */
324d13e1
JR
900+ inner = make_cleanup (xfree, filename);
901+ abfd = gdb_bfd_open (filename, gnutarget, -1);
902+ do_cleanups (inner);
140f8057
JR
903+
904+ if (abfd == NULL)
905+ continue;
321e94d6
BS
906
907- if (filename == NULL)
908- continue;
140f8057
JR
909+ if (build_id_verify (abfd.get(), build_id_len, build_id))
910+ break;
324d13e1
JR
911
912- /* We expect to be silent on the non-existing files. */
913- inner = make_cleanup (xfree, filename);
914- abfd = gdb_bfd_open (filename, gnutarget, -1);
915- do_cleanups (inner);
140f8057 916+ abfd.release ();
321e94d6 917
321e94d6
BS
918- if (abfd == NULL)
919- continue;
321e94d6 920+ filename = NULL;
ab050a48 921+ }
b1b25d28 922
140f8057
JR
923- if (build_id_verify (abfd.get(), build_id_len, build_id))
924- break;
321e94d6 925+ if (filename != NULL)
03ffe914 926+ {
321e94d6 927+ /* LINK_ALL is not used below in this non-NULL FILENAME case. */
03ffe914 928+ xfree (link0);
a7de96f0 929+ break;
03ffe914 930+ }
140f8057
JR
931
932- abfd.release ();
03ffe914
JR
933+ /* If the symlink has target request to install the target.
934+ BASE-debuginfo.rpm contains the symlink but BASE.rpm may be missing.
935+ https://bugzilla.redhat.com/show_bug.cgi?id=981154 */
936+ link0_resolved = link_resolve (link0, 0);
937+ xfree (link0);
321e94d6 938+
03ffe914 939+ if (link_all == NULL)
321e94d6 940+ link_all = link0_resolved;
03ffe914 941+ else
321e94d6 942+ {
03ffe914 943+ size_t len_orig = strlen (link_all);
ab050a48 944+
140f8057
JR
945+ link_all = (char *) xrealloc (link_all,
946+ len_orig + 1 + strlen (link0_resolved) + 1);
ab050a48 947+
03ffe914
JR
948+ /* Use whitespace instead of DIRNAME_SEPARATOR to be compatible with
949+ its possible use as an argument for installation command. */
950+ link_all[len_orig] = ' ';
b1b25d28 951+
03ffe914 952+ strcpy (&link_all[len_orig + 1], link0_resolved);
321e94d6
BS
953+ xfree (link0_resolved);
954+ }
a7de96f0 955+ }
321e94d6 956+
3a58abaf 957+ if (link_return != NULL)
ab050a48 958+ {
321e94d6 959+ if (abfd != NULL)
03ffe914
JR
960+ {
961+ *link_return = link;
962+ link = NULL;
963+ }
ab050a48 964+ else
03ffe914
JR
965+ {
966+ *link_return = link_all;
967+ link_all = NULL;
968+ }
a7de96f0 969 }
ab050a48
BZ
970+ xfree (link);
971+ xfree (link_all);
a7de96f0
PS
972
973 do_cleanups (back_to);
321e94d6 974 return abfd;
a7de96f0
PS
975 }
976
321e94d6 977+char *
324d13e1 978+build_id_to_filename (const struct bfd_build_id *build_id, char **link_return)
321e94d6 979+{
140f8057 980+ gdb_bfd_ref_ptr abfd;
321e94d6
BS
981+ char *result;
982+
983+ abfd = build_id_to_debug_bfd (build_id->size, build_id->data, link_return, 0);
984+ if (abfd == NULL)
985+ return NULL;
986+
987+ result = xstrdup (bfd_get_filename (abfd));
140f8057 988+ abfd.release ();
321e94d6
BS
989+ return result;
990+}
991+
3a58abaf
AM
992+/* This MISSING_FILEPAIR_HASH tracker is used only for the duplicite messages
993+ Try to install the hash file ...
994+ avoidance. */
995+
996+struct missing_filepair
997+ {
998+ char *binary;
999+ char *debug;
1000+ char data[1];
1001+ };
1002+
1003+static struct htab *missing_filepair_hash;
1004+static struct obstack missing_filepair_obstack;
1005+
1006+static void *
1007+missing_filepair_xcalloc (size_t nmemb, size_t nmemb_size)
1008+{
1009+ void *retval;
1010+ size_t size = nmemb * nmemb_size;
1011+
1012+ retval = obstack_alloc (&missing_filepair_obstack, size);
1013+ memset (retval, 0, size);
a7de96f0
PS
1014+ return retval;
1015+}
1016+
3a58abaf
AM
1017+static hashval_t
1018+missing_filepair_hash_func (const struct missing_filepair *elem)
1019+{
1020+ hashval_t retval = 0;
1021+
1022+ retval ^= htab_hash_string (elem->binary);
1023+ if (elem->debug != NULL)
1024+ retval ^= htab_hash_string (elem->debug);
1025+
1026+ return retval;
1027+}
1028+
1029+static int
1030+missing_filepair_eq (const struct missing_filepair *elem1,
1031+ const struct missing_filepair *elem2)
1032+{
1033+ return strcmp (elem1->binary, elem2->binary) == 0
7566401a
ER
1034+ && ((elem1->debug == NULL) == (elem2->debug == NULL))
1035+ && (elem1->debug == NULL || strcmp (elem1->debug, elem2->debug) == 0);
3a58abaf
AM
1036+}
1037+
1038+static void
1039+missing_filepair_change (void)
1040+{
1041+ if (missing_filepair_hash != NULL)
1042+ {
1043+ obstack_free (&missing_filepair_obstack, NULL);
1044+ /* All their memory came just from missing_filepair_OBSTACK. */
1045+ missing_filepair_hash = NULL;
1046+ }
1047+}
1048+
1049+static void
1050+debug_print_executable_changed (void)
1051+{
1052+ missing_filepair_change ();
1053+}
1054+
1055+/* Notify user the file BINARY with (possibly NULL) associated separate debug
1056+ information file DEBUG is missing. DEBUG may or may not be the build-id
1057+ file such as would be:
1058+ /usr/lib/debug/.build-id/dd/b1d2ce632721c47bb9e8679f369e2295ce71be.debug
1059+ */
1060+
1061+void
1062+debug_print_missing (const char *binary, const char *debug)
1063+{
1064+ size_t binary_len0 = strlen (binary) + 1;
1065+ size_t debug_len0 = debug ? strlen (debug) + 1 : 0;
7566401a 1066+ struct missing_filepair missing_filepair_find;
3a58abaf
AM
1067+ struct missing_filepair *missing_filepair;
1068+ struct missing_filepair **slot;
1069+
1070+ if (build_id_verbose < BUILD_ID_VERBOSE_FILENAMES)
1071+ return;
1072+
1073+ if (missing_filepair_hash == NULL)
1074+ {
1075+ obstack_init (&missing_filepair_obstack);
1076+ missing_filepair_hash = htab_create_alloc (64,
1077+ (hashval_t (*) (const void *)) missing_filepair_hash_func,
1078+ (int (*) (const void *, const void *)) missing_filepair_eq, NULL,
1079+ missing_filepair_xcalloc, NULL);
1080+ }
1081+
7566401a
ER
1082+ /* Use MISSING_FILEPAIR_FIND first instead of calling obstack_alloc with
1083+ obstack_free in the case of a (rare) match. The problem is ALLOC_F for
1084+ MISSING_FILEPAIR_HASH allocates from MISSING_FILEPAIR_OBSTACK maintenance
1085+ structures for MISSING_FILEPAIR_HASH. Calling obstack_free would possibly
1086+ not to free only MISSING_FILEPAIR but also some such structures (allocated
1087+ during the htab_find_slot call). */
1088+
1089+ missing_filepair_find.binary = (char *) binary;
1090+ missing_filepair_find.debug = (char *) debug;
1091+ slot = (struct missing_filepair **) htab_find_slot (missing_filepair_hash,
1092+ &missing_filepair_find,
1093+ INSERT);
1094+
1095+ /* While it may be still printed duplicitely with the missing debuginfo file
1096+ * it is due to once printing about the binary file build-id link and once
1097+ * about the .debug file build-id link as both the build-id symlinks are
1098+ * located in the debuginfo package. */
1099+
1100+ if (*slot != NULL)
1101+ return;
1102+
140f8057
JR
1103+ missing_filepair = (struct missing_filepair *) obstack_alloc (&missing_filepair_obstack,
1104+ sizeof (*missing_filepair) - 1
1105+ + binary_len0 + debug_len0);
3a58abaf
AM
1106+ missing_filepair->binary = missing_filepair->data;
1107+ memcpy (missing_filepair->binary, binary, binary_len0);
1108+ if (debug != NULL)
1109+ {
1110+ missing_filepair->debug = missing_filepair->binary + binary_len0;
1111+ memcpy (missing_filepair->debug, debug, debug_len0);
1112+ }
1113+ else
1114+ missing_filepair->debug = NULL;
1115+
3a58abaf
AM
1116+ *slot = missing_filepair;
1117+
1118+ /* We do not collect and flush these messages as each such message
1119+ already requires its own separate lines. */
1120+
1121+ fprintf_unfiltered (gdb_stdlog,
1122+ _("Missing separate debuginfo for %s\n"), binary);
1123+ if (debug != NULL)
1124+ fprintf_unfiltered (gdb_stdlog, _("Try to install the hash file %s\n"),
1125+ debug);
1126+}
1127+
321e94d6
BS
1128 /* See build-id.h. */
1129
1130 char *
51a5ef0f
PS
1131-find_separate_debug_file_by_buildid (struct objfile *objfile)
1132+find_separate_debug_file_by_buildid (struct objfile *objfile,
140f8057 1133+ gdb::unique_xmalloc_ptr<char> *build_id_filename_return)
3a58abaf 1134 {
324d13e1 1135 const struct bfd_build_id *build_id;
3a58abaf
AM
1136
1137- build_id = build_id_bfd_get (objfile->obfd);
51a5ef0f
PS
1138+ if (build_id_filename_return)
1139+ *build_id_filename_return = NULL;
1140+
3a58abaf
AM
1141+ build_id = build_id_bfd_shdr_get (objfile->obfd);
1142 if (build_id != NULL)
1143 {
140f8057
JR
1144+ char *build_id_filename_cstr = NULL;
1145 gdb_bfd_ref_ptr abfd (build_id_to_debug_bfd (build_id->size,
1146- build_id->data));
1147+ build_id->data,
1148+ (!build_id_filename_return ? NULL : &build_id_filename_cstr), 1));
1149+ if (build_id_filename_return)
1150+ {
1151+ if (!build_id_filename_cstr)
1152+ gdb_assert (!*build_id_filename_return);
1153+ else
1154+ {
1155+ *build_id_filename_return = gdb::unique_xmalloc_ptr<char> (build_id_filename_cstr);
1156+ build_id_filename_cstr = NULL;
1157+ }
1158+ }
3a58abaf 1159 /* Prevent looping on a stripped .debug file. */
321e94d6 1160 if (abfd != NULL
140f8057
JR
1161 && filename_cmp (bfd_get_filename (abfd.get ()),
1162@@ -160,3 +837,21 @@
51a5ef0f
PS
1163 }
1164 return NULL;
1165 }
51a5ef0f 1166+
321e94d6 1167+extern void _initialize_build_id (void);
51a5ef0f 1168+
321e94d6
BS
1169+void
1170+_initialize_build_id (void)
1171+{
51a5ef0f
PS
1172+ add_setshow_zinteger_cmd ("build-id-verbose", no_class, &build_id_verbose,
1173+ _("\
1174+Set debugging level of the build-id locator."), _("\
1175+Show debugging level of the build-id locator."), _("\
1176+Level 1 (default) enables printing the missing debug filenames,\n\
1177+level 2 also prints the parsing of binaries to find the identificators."),
1178+ NULL,
1179+ show_build_id_verbose,
1180+ &setlist, &showlist);
1181+
1182+ observer_attach_executable_changed (debug_print_executable_changed);
321e94d6 1183+}
140f8057 1184Index: gdb-7.99.90.20170420/gdb/build-id.h
3a58abaf 1185===================================================================
140f8057
JR
1186--- gdb-7.99.90.20170420.orig/gdb/build-id.h 2017-04-20 23:00:35.415584426 +0200
1187+++ gdb-7.99.90.20170420/gdb/build-id.h 2017-04-20 23:00:43.371629257 +0200
1188@@ -22,9 +22,10 @@
1189
1190 #include "gdb_bfd.h"
b1b25d28
JR
1191
1192-/* Locate NT_GNU_BUILD_ID from ABFD and return its content. */
1193+/* Separate debuginfo files have corrupted PHDR but SHDR is correct there.
1194+ Locate NT_GNU_BUILD_ID from ABFD and return its content. */
1195
324d13e1
JR
1196-extern const struct bfd_build_id *build_id_bfd_get (bfd *abfd);
1197+extern const struct bfd_build_id *build_id_bfd_shdr_get (bfd *abfd);
b1b25d28
JR
1198
1199 /* Return true if ABFD has NT_GNU_BUILD_ID matching the CHECK value.
1200 Otherwise, issue a warning and return false. */
140f8057 1201@@ -38,13 +39,19 @@
321e94d6 1202 the caller. */
3a58abaf 1203
140f8057
JR
1204 extern gdb_bfd_ref_ptr build_id_to_debug_bfd (size_t build_id_len,
1205- const bfd_byte *build_id);
1206+ const bfd_byte *build_id,
1207+ char **link_return,
1208+ int add_debug_suffix);
3a58abaf 1209+
324d13e1 1210+extern char *build_id_to_filename (const struct bfd_build_id *build_id,
321e94d6 1211+ char **link_return);
3a58abaf 1212
321e94d6
BS
1213 /* Find the separate debug file for OBJFILE, by using the build-id
1214 associated with OBJFILE's BFD. If successful, returns a malloc'd
1215 file name for the separate debug file. The caller must free this.
1216 Otherwise, returns NULL. */
1217
1218-extern char *find_separate_debug_file_by_buildid (struct objfile *objfile);
1219+extern char *find_separate_debug_file_by_buildid (struct objfile *objfile,
140f8057 1220+ gdb::unique_xmalloc_ptr<char> *build_id_filename_return);
3a58abaf 1221
321e94d6 1222 #endif /* BUILD_ID_H */
140f8057 1223Index: gdb-7.99.90.20170420/gdb/dwarf2read.c
7566401a 1224===================================================================
140f8057
JR
1225--- gdb-7.99.90.20170420.orig/gdb/dwarf2read.c 2017-04-20 23:00:35.415584426 +0200
1226+++ gdb-7.99.90.20170420/gdb/dwarf2read.c 2017-04-20 23:00:43.375629279 +0200
1227@@ -2671,7 +2671,7 @@
d566d21e 1228 }
7566401a 1229
321e94d6
BS
1230 if (dwz_bfd == NULL)
1231- dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
1232+ dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid, NULL, 1);
a7de96f0 1233
321e94d6
BS
1234 if (dwz_bfd == NULL)
1235 error (_("could not find '.gnu_debugaltlink' file for %s"),
140f8057 1236Index: gdb-7.99.90.20170420/gdb/python/py-objfile.c
b1b25d28 1237===================================================================
140f8057
JR
1238--- gdb-7.99.90.20170420.orig/gdb/python/py-objfile.c 2017-04-20 23:00:35.415584426 +0200
1239+++ gdb-7.99.90.20170420/gdb/python/py-objfile.c 2017-04-20 23:00:43.375629279 +0200
1240@@ -137,7 +137,7 @@
b1b25d28 1241
324d13e1 1242 TRY
b1b25d28
JR
1243 {
1244- build_id = build_id_bfd_get (objfile->obfd);
1245+ build_id = build_id_bfd_shdr_get (objfile->obfd);
1246 }
324d13e1
JR
1247 CATCH (except, RETURN_MASK_ALL)
1248 {
140f8057 1249@@ -544,7 +544,7 @@
b1b25d28
JR
1250 /* Don't return separate debug files. */
1251 if (objfile->separate_debug_objfile_backlink != NULL)
1252 continue;
1253- obfd_build_id = build_id_bfd_get (objfile->obfd);
1254+ obfd_build_id = build_id_bfd_shdr_get (objfile->obfd);
1255 if (obfd_build_id == NULL)
1256 continue;
1257 if (objfpy_build_id_matches (obfd_build_id, build_id))
140f8057 1258Index: gdb-7.99.90.20170420/gdb/coffread.c
324d13e1 1259===================================================================
140f8057
JR
1260--- gdb-7.99.90.20170420.orig/gdb/coffread.c 2017-04-20 23:00:35.415584426 +0200
1261+++ gdb-7.99.90.20170420/gdb/coffread.c 2017-04-20 23:00:43.376629285 +0200
1262@@ -734,7 +734,7 @@
324d13e1
JR
1263 {
1264 char *debugfile;
1265
1266- debugfile = find_separate_debug_file_by_buildid (objfile);
1267+ debugfile = find_separate_debug_file_by_buildid (objfile, NULL);
1268
1269 if (debugfile == NULL)
1270 debugfile = find_separate_debug_file_by_debuglink (objfile);
This page took 0.323471 seconds and 4 git commands to generate.