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