Commit | Line | Data |
---|---|---|
dc294be5 TT |
1 | /* build-id-related functions. |
2 | ||
32d0add0 | 3 | Copyright (C) 1991-2015 Free Software Foundation, Inc. |
dc294be5 TT |
4 | |
5 | This file is part of GDB. | |
6 | ||
7 | This program is free software; you can redistribute it and/or modify | |
8 | it under the terms of the GNU General Public License as published by | |
9 | the Free Software Foundation; either version 3 of the License, or | |
10 | (at your option) any later version. | |
11 | ||
12 | This program is distributed in the hope that it will be useful, | |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License for more details. | |
16 | ||
17 | You should have received a copy of the GNU General Public License | |
18 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ | |
19 | ||
20 | #include "defs.h" | |
21 | #include "bfd.h" | |
22 | #include "elf-bfd.h" | |
23 | #include "gdb_bfd.h" | |
24 | #include "build-id.h" | |
dc294be5 TT |
25 | #include "gdb_vecs.h" |
26 | #include "symfile.h" | |
27 | #include "objfiles.h" | |
28 | #include "filenames.h" | |
29 | ||
7c50a931 | 30 | /* See build-id.h. */ |
dc294be5 | 31 | |
7c50a931 | 32 | const struct elf_build_id * |
dc294be5 TT |
33 | build_id_bfd_get (bfd *abfd) |
34 | { | |
35 | if (!bfd_check_format (abfd, bfd_object) | |
36 | || bfd_get_flavour (abfd) != bfd_target_elf_flavour | |
37 | /* Although this is ELF_specific, it is safe to do in generic | |
38 | code because it does not rely on any ELF-specific symbols at | |
39 | link time, and if the ELF code is not available in BFD, then | |
40 | ABFD will not have the ELF flavour. */ | |
41 | || elf_tdata (abfd)->build_id == NULL) | |
42 | return NULL; | |
43 | ||
44 | return elf_tdata (abfd)->build_id; | |
45 | } | |
46 | ||
47 | /* See build-id.h. */ | |
48 | ||
49 | int | |
50 | build_id_verify (bfd *abfd, size_t check_len, const bfd_byte *check) | |
51 | { | |
52 | const struct elf_build_id *found; | |
53 | int retval = 0; | |
54 | ||
55 | found = build_id_bfd_get (abfd); | |
56 | ||
57 | if (found == NULL) | |
58 | warning (_("File \"%s\" has no build-id, file skipped"), | |
59 | bfd_get_filename (abfd)); | |
60 | else if (found->size != check_len | |
61 | || memcmp (found->data, check, found->size) != 0) | |
62 | warning (_("File \"%s\" has a different build-id, file skipped"), | |
63 | bfd_get_filename (abfd)); | |
64 | else | |
65 | retval = 1; | |
66 | ||
67 | return retval; | |
68 | } | |
69 | ||
70 | /* See build-id.h. */ | |
71 | ||
72 | bfd * | |
73 | build_id_to_debug_bfd (size_t build_id_len, const bfd_byte *build_id) | |
74 | { | |
75 | char *link, *debugdir; | |
76 | VEC (char_ptr) *debugdir_vec; | |
77 | struct cleanup *back_to; | |
78 | int ix; | |
79 | bfd *abfd = NULL; | |
80 | ||
81 | /* DEBUG_FILE_DIRECTORY/.build-id/ab/cdef */ | |
82 | link = alloca (strlen (debug_file_directory) + (sizeof "/.build-id/" - 1) + 1 | |
83 | + 2 * build_id_len + (sizeof ".debug" - 1) + 1); | |
84 | ||
85 | /* Keep backward compatibility so that DEBUG_FILE_DIRECTORY being "" will | |
86 | cause "/.build-id/..." lookups. */ | |
87 | ||
88 | debugdir_vec = dirnames_to_char_ptr_vec (debug_file_directory); | |
89 | back_to = make_cleanup_free_char_ptr_vec (debugdir_vec); | |
90 | ||
91 | for (ix = 0; VEC_iterate (char_ptr, debugdir_vec, ix, debugdir); ++ix) | |
92 | { | |
93 | size_t debugdir_len = strlen (debugdir); | |
94 | const gdb_byte *data = build_id; | |
95 | size_t size = build_id_len; | |
96 | char *s; | |
97 | char *filename = NULL; | |
98 | ||
99 | memcpy (link, debugdir, debugdir_len); | |
100 | s = &link[debugdir_len]; | |
101 | s += sprintf (s, "/.build-id/"); | |
102 | if (size > 0) | |
103 | { | |
104 | size--; | |
105 | s += sprintf (s, "%02x", (unsigned) *data++); | |
106 | } | |
107 | if (size > 0) | |
108 | *s++ = '/'; | |
109 | while (size-- > 0) | |
110 | s += sprintf (s, "%02x", (unsigned) *data++); | |
111 | strcpy (s, ".debug"); | |
112 | ||
113 | /* lrealpath() is expensive even for the usually non-existent files. */ | |
114 | if (access (link, F_OK) == 0) | |
115 | filename = lrealpath (link); | |
116 | ||
117 | if (filename == NULL) | |
118 | continue; | |
119 | ||
120 | /* We expect to be silent on the non-existing files. */ | |
121 | abfd = gdb_bfd_open_maybe_remote (filename); | |
122 | if (abfd == NULL) | |
123 | continue; | |
124 | ||
125 | if (build_id_verify (abfd, build_id_len, build_id)) | |
126 | break; | |
127 | ||
128 | gdb_bfd_unref (abfd); | |
129 | abfd = NULL; | |
130 | } | |
131 | ||
132 | do_cleanups (back_to); | |
133 | return abfd; | |
134 | } | |
135 | ||
136 | /* See build-id.h. */ | |
137 | ||
138 | char * | |
139 | find_separate_debug_file_by_buildid (struct objfile *objfile) | |
140 | { | |
141 | const struct elf_build_id *build_id; | |
142 | ||
143 | build_id = build_id_bfd_get (objfile->obfd); | |
144 | if (build_id != NULL) | |
145 | { | |
146 | bfd *abfd; | |
147 | ||
148 | abfd = build_id_to_debug_bfd (build_id->size, build_id->data); | |
149 | /* Prevent looping on a stripped .debug file. */ | |
150 | if (abfd != NULL | |
151 | && filename_cmp (bfd_get_filename (abfd), | |
152 | objfile_name (objfile)) == 0) | |
153 | { | |
154 | warning (_("\"%s\": separate debug info file has no debug info"), | |
155 | bfd_get_filename (abfd)); | |
156 | gdb_bfd_unref (abfd); | |
157 | } | |
158 | else if (abfd != NULL) | |
159 | { | |
160 | char *result = xstrdup (bfd_get_filename (abfd)); | |
161 | ||
162 | gdb_bfd_unref (abfd); | |
163 | return result; | |
164 | } | |
165 | } | |
166 | return NULL; | |
167 | } |