Commit | Line | Data |
---|---|---|
dc294be5 TT |
1 | /* build-id-related functions. |
2 | ||
42a4f53d | 3 | Copyright (C) 1991-2019 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" | |
4de283e4 | 22 | #include "gdb_bfd.h" |
dc294be5 | 23 | #include "build-id.h" |
268a13a5 | 24 | #include "gdbsupport/gdb_vecs.h" |
4de283e4 TT |
25 | #include "symfile.h" |
26 | #include "objfiles.h" | |
dc294be5 | 27 | #include "filenames.h" |
2938e6cf | 28 | #include "gdbcore.h" |
dc294be5 | 29 | |
7c50a931 | 30 | /* See build-id.h. */ |
dc294be5 | 31 | |
c74f7d1c | 32 | const struct bfd_build_id * |
dc294be5 TT |
33 | build_id_bfd_get (bfd *abfd) |
34 | { | |
c74f7d1c | 35 | if (!bfd_check_format (abfd, bfd_object)) |
dc294be5 TT |
36 | return NULL; |
37 | ||
c74f7d1c JT |
38 | if (abfd->build_id != NULL) |
39 | return abfd->build_id; | |
40 | ||
41 | /* No build-id */ | |
42 | return NULL; | |
dc294be5 TT |
43 | } |
44 | ||
45 | /* See build-id.h. */ | |
46 | ||
47 | int | |
48 | build_id_verify (bfd *abfd, size_t check_len, const bfd_byte *check) | |
49 | { | |
c74f7d1c | 50 | const struct bfd_build_id *found; |
dc294be5 TT |
51 | int retval = 0; |
52 | ||
53 | found = build_id_bfd_get (abfd); | |
54 | ||
55 | if (found == NULL) | |
56 | warning (_("File \"%s\" has no build-id, file skipped"), | |
57 | bfd_get_filename (abfd)); | |
58 | else if (found->size != check_len | |
59 | || memcmp (found->data, check, found->size) != 0) | |
60 | warning (_("File \"%s\" has a different build-id, file skipped"), | |
61 | bfd_get_filename (abfd)); | |
62 | else | |
63 | retval = 1; | |
64 | ||
65 | return retval; | |
66 | } | |
67 | ||
07bc701d SM |
68 | /* Helper for build_id_to_debug_bfd. LINK is a path to a potential |
69 | build-id-based separate debug file, potentially a symlink to the real file. | |
70 | If the file exists and matches BUILD_ID, return a BFD reference to it. */ | |
71 | ||
72 | static gdb_bfd_ref_ptr | |
73 | build_id_to_debug_bfd_1 (const std::string &link, size_t build_id_len, | |
74 | const bfd_byte *build_id) | |
75 | { | |
76 | if (separate_debug_file_debug) | |
77 | { | |
78 | printf_unfiltered (_(" Trying %s..."), link.c_str ()); | |
79 | gdb_flush (gdb_stdout); | |
80 | } | |
81 | ||
82 | /* lrealpath() is expensive even for the usually non-existent files. */ | |
83 | gdb::unique_xmalloc_ptr<char> filename; | |
84 | if (access (link.c_str (), F_OK) == 0) | |
85 | filename.reset (lrealpath (link.c_str ())); | |
86 | ||
87 | if (filename == NULL) | |
88 | { | |
89 | if (separate_debug_file_debug) | |
90 | printf_unfiltered (_(" no, unable to compute real path\n")); | |
91 | ||
92 | return {}; | |
93 | } | |
94 | ||
95 | /* We expect to be silent on the non-existing files. */ | |
96 | gdb_bfd_ref_ptr debug_bfd = gdb_bfd_open (filename.get (), gnutarget, -1); | |
97 | ||
98 | if (debug_bfd == NULL) | |
99 | { | |
100 | if (separate_debug_file_debug) | |
101 | printf_unfiltered (_(" no, unable to open.\n")); | |
102 | ||
103 | return {}; | |
104 | } | |
105 | ||
106 | if (!build_id_verify (debug_bfd.get(), build_id_len, build_id)) | |
107 | { | |
108 | if (separate_debug_file_debug) | |
109 | printf_unfiltered (_(" no, build-id does not match.\n")); | |
110 | ||
111 | return {}; | |
112 | } | |
113 | ||
114 | if (separate_debug_file_debug) | |
115 | printf_unfiltered (_(" yes!\n")); | |
116 | ||
117 | return debug_bfd; | |
118 | } | |
119 | ||
dc294be5 TT |
120 | /* See build-id.h. */ |
121 | ||
192b62ce | 122 | gdb_bfd_ref_ptr |
dc294be5 TT |
123 | build_id_to_debug_bfd (size_t build_id_len, const bfd_byte *build_id) |
124 | { | |
dc294be5 TT |
125 | /* Keep backward compatibility so that DEBUG_FILE_DIRECTORY being "" will |
126 | cause "/.build-id/..." lookups. */ | |
127 | ||
e80aaf61 SM |
128 | std::vector<gdb::unique_xmalloc_ptr<char>> debugdir_vec |
129 | = dirnames_to_char_ptr_vec (debug_file_directory); | |
dc294be5 | 130 | |
e80aaf61 | 131 | for (const gdb::unique_xmalloc_ptr<char> &debugdir : debugdir_vec) |
dc294be5 | 132 | { |
dc294be5 TT |
133 | const gdb_byte *data = build_id; |
134 | size_t size = build_id_len; | |
dc294be5 | 135 | |
07bc701d SM |
136 | /* Compute where the file named after the build-id would be. |
137 | ||
138 | If debugdir is "/usr/lib/debug" and the build-id is abcdef, this will | |
139 | give "/usr/lib/debug/.build-id/ab/cdef.debug". */ | |
00b40057 SM |
140 | std::string link = debugdir.get (); |
141 | link += "/.build-id/"; | |
142 | ||
dc294be5 TT |
143 | if (size > 0) |
144 | { | |
145 | size--; | |
00b40057 | 146 | string_appendf (link, "%02x/", (unsigned) *data++); |
dc294be5 | 147 | } |
00b40057 | 148 | |
dc294be5 | 149 | while (size-- > 0) |
00b40057 SM |
150 | string_appendf (link, "%02x", (unsigned) *data++); |
151 | ||
152 | link += ".debug"; | |
dc294be5 | 153 | |
07bc701d SM |
154 | gdb_bfd_ref_ptr debug_bfd |
155 | = build_id_to_debug_bfd_1 (link, build_id_len, build_id); | |
156 | if (debug_bfd != NULL) | |
157 | return debug_bfd; | |
c4dcb155 | 158 | |
07bc701d SM |
159 | /* Try to look under the sysroot as well. If the sysroot is |
160 | "/the/sysroot", it will give | |
161 | "/the/sysroot/usr/lib/debug/.build-id/ab/cdef.debug". | |
dc294be5 | 162 | |
07bc701d SM |
163 | Don't do it if the sysroot is the target system ("target:"). It |
164 | could work in theory, but the lrealpath in build_id_to_debug_bfd_1 | |
165 | only works with local paths. */ | |
166 | if (strcmp (gdb_sysroot, TARGET_SYSROOT_PREFIX) != 0) | |
50794b45 | 167 | { |
07bc701d SM |
168 | link = gdb_sysroot + link; |
169 | debug_bfd = build_id_to_debug_bfd_1 (link, build_id_len, build_id); | |
170 | if (debug_bfd != NULL) | |
171 | return debug_bfd; | |
50794b45 | 172 | } |
dc294be5 TT |
173 | } |
174 | ||
07bc701d | 175 | return {}; |
dc294be5 TT |
176 | } |
177 | ||
178 | /* See build-id.h. */ | |
179 | ||
a8dbfd58 | 180 | std::string |
dc294be5 TT |
181 | find_separate_debug_file_by_buildid (struct objfile *objfile) |
182 | { | |
c74f7d1c | 183 | const struct bfd_build_id *build_id; |
dc294be5 TT |
184 | |
185 | build_id = build_id_bfd_get (objfile->obfd); | |
186 | if (build_id != NULL) | |
187 | { | |
c4dcb155 SM |
188 | if (separate_debug_file_debug) |
189 | printf_unfiltered (_("\nLooking for separate debug info (build-id) for " | |
190 | "%s\n"), objfile_name (objfile)); | |
191 | ||
192b62ce TT |
192 | gdb_bfd_ref_ptr abfd (build_id_to_debug_bfd (build_id->size, |
193 | build_id->data)); | |
dc294be5 TT |
194 | /* Prevent looping on a stripped .debug file. */ |
195 | if (abfd != NULL | |
192b62ce | 196 | && filename_cmp (bfd_get_filename (abfd.get ()), |
dc294be5 | 197 | objfile_name (objfile)) == 0) |
192b62ce TT |
198 | warning (_("\"%s\": separate debug info file has no debug info"), |
199 | bfd_get_filename (abfd.get ())); | |
dc294be5 | 200 | else if (abfd != NULL) |
a8dbfd58 | 201 | return std::string (bfd_get_filename (abfd.get ())); |
dc294be5 | 202 | } |
a8dbfd58 SM |
203 | |
204 | return std::string (); | |
dc294be5 | 205 | } |