]>
Commit | Line | Data |
---|---|---|
cbb099e8 TT |
1 | /* Definitions for BFD wrappers used by GDB. |
2 | ||
213516ef | 3 | Copyright (C) 2011-2023 Free Software Foundation, Inc. |
cbb099e8 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 "gdb_bfd.h" | |
d6b28940 TT |
22 | #include "ui-out.h" |
23 | #include "gdbcmd.h" | |
6ec53d05 | 24 | #include "hashtab.h" |
268a13a5 | 25 | #include "gdbsupport/filestuff.h" |
4bf44c1c TT |
26 | #ifdef HAVE_MMAP |
27 | #include <sys/mman.h> | |
28 | #ifndef MAP_FAILED | |
29 | #define MAP_FAILED ((void *) -1) | |
30 | #endif | |
31 | #endif | |
f08e97fe | 32 | #include "target.h" |
198f946f | 33 | #include "gdbsupport/fileio.h" |
07c138c8 | 34 | #include "inferior.h" |
972f7a4b | 35 | #include "cli/cli-style.h" |
84e60555 | 36 | #include <unordered_map> |
4bf44c1c TT |
37 | |
38 | /* An object of this type is stored in the section's user data when | |
39 | mapping a section. */ | |
40 | ||
41 | struct gdb_bfd_section_data | |
42 | { | |
43 | /* Size of the data. */ | |
44 | bfd_size_type size; | |
45 | /* If the data was mmapped, this is the length of the map. */ | |
46 | bfd_size_type map_len; | |
47 | /* The data. If NULL, the section data has not been read. */ | |
48 | void *data; | |
49 | /* If the data was mmapped, this is the map address. */ | |
50 | void *map_addr; | |
51 | }; | |
a4453b7e | 52 | |
d6b28940 TT |
53 | /* A hash table holding every BFD that gdb knows about. This is not |
54 | to be confused with 'gdb_bfd_cache', which is used for sharing | |
55 | BFDs; in contrast, this hash is used just to implement | |
56 | "maint info bfd". */ | |
57 | ||
58 | static htab_t all_bfds; | |
59 | ||
6ec53d05 TT |
60 | /* An object of this type is stored in each BFD's user data. */ |
61 | ||
62 | struct gdb_bfd_data | |
63 | { | |
3cae4447 TT |
64 | /* Note that if ST is nullptr, then we simply fill in zeroes. */ |
65 | gdb_bfd_data (bfd *abfd, struct stat *st) | |
66 | : mtime (st == nullptr ? 0 : st->st_mtime), | |
67 | size (st == nullptr ? 0 : st->st_size), | |
68 | inode (st == nullptr ? 0 : st->st_ino), | |
69 | device_id (st == nullptr ? 0 : st->st_dev), | |
06d5bbc8 TT |
70 | relocation_computed (0), |
71 | needs_relocations (0), | |
72 | crc_computed (0) | |
73 | { | |
06d5bbc8 TT |
74 | } |
75 | ||
76 | ~gdb_bfd_data () | |
77 | { | |
06d5bbc8 TT |
78 | } |
79 | ||
6ec53d05 | 80 | /* The reference count. */ |
06d5bbc8 | 81 | int refc = 1; |
6ec53d05 TT |
82 | |
83 | /* The mtime of the BFD at the point the cache entry was made. */ | |
84 | time_t mtime; | |
b82d08cd | 85 | |
c04fe68f AB |
86 | /* The file size (in bytes) at the point the cache entry was made. */ |
87 | off_t size; | |
88 | ||
89 | /* The inode of the file at the point the cache entry was made. */ | |
90 | ino_t inode; | |
91 | ||
92 | /* The device id of the file at the point the cache entry was made. */ | |
93 | dev_t device_id; | |
94 | ||
1da77581 TT |
95 | /* This is true if we have determined whether this BFD has any |
96 | sections requiring relocation. */ | |
97 | unsigned int relocation_computed : 1; | |
98 | ||
99 | /* This is true if any section needs relocation. */ | |
100 | unsigned int needs_relocations : 1; | |
101 | ||
dccee2de TT |
102 | /* This is true if we have successfully computed the file's CRC. */ |
103 | unsigned int crc_computed : 1; | |
104 | ||
105 | /* The file's CRC. */ | |
06d5bbc8 | 106 | unsigned long crc = 0; |
dccee2de | 107 | |
b82d08cd TT |
108 | /* If the BFD comes from an archive, this points to the archive's |
109 | BFD. Otherwise, this is NULL. */ | |
06d5bbc8 | 110 | bfd *archive_bfd = nullptr; |
e992eda4 | 111 | |
13aaf454 | 112 | /* Table of all the bfds this bfd has included. */ |
d5833c62 | 113 | std::vector<gdb_bfd_ref_ptr> included_bfds; |
13aaf454 | 114 | |
e992eda4 | 115 | /* The registry. */ |
08b8a139 | 116 | registry<bfd> registry_fields; |
6ec53d05 TT |
117 | }; |
118 | ||
08b8a139 TT |
119 | registry<bfd> * |
120 | registry_accessor<bfd>::get (bfd *abfd) | |
121 | { | |
122 | struct gdb_bfd_data *gdata = (struct gdb_bfd_data *) bfd_usrdata (abfd); | |
123 | return &gdata->registry_fields; | |
124 | } | |
e992eda4 | 125 | |
6ec53d05 TT |
126 | /* A hash table storing all the BFDs maintained in the cache. */ |
127 | ||
128 | static htab_t gdb_bfd_cache; | |
129 | ||
18989b3c AB |
130 | /* When true gdb will reuse an existing bfd object if the filename, |
131 | modification time, and file size all match. */ | |
132 | ||
491144b5 | 133 | static bool bfd_sharing = true; |
18989b3c AB |
134 | static void |
135 | show_bfd_sharing (struct ui_file *file, int from_tty, | |
136 | struct cmd_list_element *c, const char *value) | |
137 | { | |
6cb06a8c | 138 | gdb_printf (file, _("BFD sharing is %s.\n"), value); |
18989b3c AB |
139 | } |
140 | ||
d4dd4fca SM |
141 | /* When true debugging of the bfd caches is enabled. */ |
142 | ||
143 | static bool debug_bfd_cache; | |
566f5e3b | 144 | |
1e15fcac SM |
145 | /* Print an "bfd-cache" debug statement. */ |
146 | ||
147 | #define bfd_cache_debug_printf(fmt, ...) \ | |
148 | debug_prefixed_printf_cond (debug_bfd_cache, "bfd-cache", fmt, ##__VA_ARGS__) | |
149 | ||
566f5e3b AB |
150 | static void |
151 | show_bfd_cache_debug (struct ui_file *file, int from_tty, | |
152 | struct cmd_list_element *c, const char *value) | |
153 | { | |
6cb06a8c | 154 | gdb_printf (file, _("BFD cache debugging is %s.\n"), value); |
566f5e3b AB |
155 | } |
156 | ||
6ec53d05 TT |
157 | /* The type of an object being looked up in gdb_bfd_cache. We use |
158 | htab's capability of storing one kind of object (BFD in this case) | |
159 | and using a different sort of object for searching. */ | |
160 | ||
161 | struct gdb_bfd_cache_search | |
162 | { | |
163 | /* The filename. */ | |
164 | const char *filename; | |
165 | /* The mtime. */ | |
166 | time_t mtime; | |
c04fe68f AB |
167 | /* The file size (in bytes). */ |
168 | off_t size; | |
169 | /* The inode of the file. */ | |
170 | ino_t inode; | |
171 | /* The device id of the file. */ | |
172 | dev_t device_id; | |
6ec53d05 TT |
173 | }; |
174 | ||
175 | /* A hash function for BFDs. */ | |
176 | ||
177 | static hashval_t | |
178 | hash_bfd (const void *b) | |
179 | { | |
9a3c8263 | 180 | const bfd *abfd = (const struct bfd *) b; |
6ec53d05 TT |
181 | |
182 | /* It is simplest to just hash the filename. */ | |
183 | return htab_hash_string (bfd_get_filename (abfd)); | |
184 | } | |
185 | ||
186 | /* An equality function for BFDs. Note that this expects the caller | |
187 | to search using struct gdb_bfd_cache_search only, not BFDs. */ | |
188 | ||
189 | static int | |
190 | eq_bfd (const void *a, const void *b) | |
191 | { | |
9a3c8263 SM |
192 | const bfd *abfd = (const struct bfd *) a; |
193 | const struct gdb_bfd_cache_search *s | |
194 | = (const struct gdb_bfd_cache_search *) b; | |
195 | struct gdb_bfd_data *gdata = (struct gdb_bfd_data *) bfd_usrdata (abfd); | |
6ec53d05 TT |
196 | |
197 | return (gdata->mtime == s->mtime | |
c04fe68f AB |
198 | && gdata->size == s->size |
199 | && gdata->inode == s->inode | |
200 | && gdata->device_id == s->device_id | |
6ec53d05 TT |
201 | && strcmp (bfd_get_filename (abfd), s->filename) == 0); |
202 | } | |
203 | ||
204 | /* See gdb_bfd.h. */ | |
205 | ||
f08e97fe GB |
206 | int |
207 | is_target_filename (const char *name) | |
208 | { | |
209 | return startswith (name, TARGET_SYSROOT_PREFIX); | |
210 | } | |
211 | ||
212 | /* See gdb_bfd.h. */ | |
213 | ||
214 | int | |
215 | gdb_bfd_has_target_filename (struct bfd *abfd) | |
216 | { | |
217 | return is_target_filename (bfd_get_filename (abfd)); | |
218 | } | |
219 | ||
66984afd AB |
220 | /* For `gdb_bfd_open_from_target_memory`. An object that manages the |
221 | details of a BFD in target memory. */ | |
15cc148f | 222 | |
4d2edefa | 223 | struct target_buffer : public gdb_bfd_iovec_base |
15cc148f | 224 | { |
66984afd AB |
225 | /* Constructor. BASE and SIZE define where the BFD can be found in |
226 | target memory. */ | |
227 | target_buffer (CORE_ADDR base, ULONGEST size) | |
228 | : m_base (base), | |
5153027c TT |
229 | m_size (size), |
230 | m_filename (xstrprintf ("<in-memory@%s>", | |
231 | core_addr_to_string_nz (m_base))) | |
66984afd | 232 | { |
66984afd AB |
233 | } |
234 | ||
235 | /* Return the size of the in-memory BFD file. */ | |
236 | ULONGEST size () const | |
237 | { return m_size; } | |
238 | ||
239 | /* Return the base address of the in-memory BFD file. */ | |
240 | CORE_ADDR base () const | |
241 | { return m_base; } | |
242 | ||
243 | /* Return a generated filename for the in-memory BFD file. The generated | |
244 | name will include the M_BASE value. */ | |
245 | const char *filename () const | |
246 | { return m_filename.get (); } | |
247 | ||
4d2edefa TT |
248 | file_ptr read (bfd *abfd, void *buffer, file_ptr nbytes, |
249 | file_ptr offset) override; | |
250 | ||
251 | int stat (struct bfd *abfd, struct stat *sb) override; | |
252 | ||
66984afd AB |
253 | private: |
254 | /* The base address of the in-memory BFD file. */ | |
255 | CORE_ADDR m_base; | |
256 | ||
257 | /* The size (in-bytes) of the in-memory BFD file. */ | |
258 | ULONGEST m_size; | |
259 | ||
260 | /* Holds the generated name of the in-memory BFD file. */ | |
261 | gdb::unique_xmalloc_ptr<char> m_filename; | |
15cc148f MS |
262 | }; |
263 | ||
15cc148f MS |
264 | /* For `gdb_bfd_open_from_target_memory`. For reading the file, we just need to |
265 | pass through to target_read_memory and fix up the arguments and return | |
266 | values. */ | |
267 | ||
4d2edefa TT |
268 | file_ptr |
269 | target_buffer::read (struct bfd *abfd, void *buf, | |
15cc148f MS |
270 | file_ptr nbytes, file_ptr offset) |
271 | { | |
15cc148f | 272 | /* If this read will read all of the file, limit it to just the rest. */ |
4d2edefa TT |
273 | if (offset + nbytes > size ()) |
274 | nbytes = size () - offset; | |
15cc148f MS |
275 | |
276 | /* If there are no more bytes left, we've reached EOF. */ | |
277 | if (nbytes == 0) | |
278 | return 0; | |
279 | ||
4d2edefa | 280 | int err = target_read_memory (base () + offset, (gdb_byte *) buf, nbytes); |
15cc148f MS |
281 | if (err) |
282 | return -1; | |
283 | ||
284 | return nbytes; | |
285 | } | |
286 | ||
287 | /* For `gdb_bfd_open_from_target_memory`. For statting the file, we only | |
288 | support the st_size attribute. */ | |
289 | ||
4d2edefa TT |
290 | int |
291 | target_buffer::stat (struct bfd *abfd, struct stat *sb) | |
15cc148f | 292 | { |
15cc148f | 293 | memset (sb, 0, sizeof (struct stat)); |
4d2edefa | 294 | sb->st_size = size (); |
15cc148f MS |
295 | return 0; |
296 | } | |
297 | ||
298 | /* See gdb_bfd.h. */ | |
299 | ||
300 | gdb_bfd_ref_ptr | |
301 | gdb_bfd_open_from_target_memory (CORE_ADDR addr, ULONGEST size, | |
322dd71c | 302 | const char *target) |
15cc148f | 303 | { |
4d2edefa TT |
304 | std::unique_ptr<target_buffer> buffer |
305 | = gdb::make_unique<target_buffer> (addr, size); | |
15cc148f | 306 | |
66984afd | 307 | return gdb_bfd_openr_iovec (buffer->filename (), target, |
4d2edefa TT |
308 | [&] (bfd *nbfd) |
309 | { | |
310 | return buffer.release (); | |
311 | }); | |
15cc148f | 312 | } |
f08e97fe | 313 | |
dcc04527 TT |
314 | /* An object that manages the underlying stream for a BFD, using |
315 | target file I/O. */ | |
316 | ||
317 | struct target_fileio_stream : public gdb_bfd_iovec_base | |
98c59b52 | 318 | { |
dcc04527 TT |
319 | target_fileio_stream (bfd *nbfd, int fd) |
320 | : m_bfd (nbfd), | |
321 | m_fd (fd) | |
322 | { | |
323 | } | |
324 | ||
325 | ~target_fileio_stream (); | |
326 | ||
327 | file_ptr read (bfd *abfd, void *buffer, file_ptr nbytes, | |
328 | file_ptr offset) override; | |
329 | ||
330 | int stat (struct bfd *abfd, struct stat *sb) override; | |
331 | ||
332 | private: | |
333 | ||
334 | /* The BFD. Saved for the destructor. */ | |
335 | bfd *m_bfd; | |
336 | ||
337 | /* The file descriptor. */ | |
338 | int m_fd; | |
98c59b52 PA |
339 | }; |
340 | ||
dcc04527 TT |
341 | /* Wrapper for target_fileio_open suitable for use as a helper |
342 | function for gdb_bfd_openr_iovec. */ | |
f08e97fe | 343 | |
dcc04527 TT |
344 | static target_fileio_stream * |
345 | gdb_bfd_iovec_fileio_open (struct bfd *abfd, inferior *inf, bool warn_if_slow) | |
f08e97fe GB |
346 | { |
347 | const char *filename = bfd_get_filename (abfd); | |
b872057a SM |
348 | int fd; |
349 | fileio_error target_errno; | |
f08e97fe GB |
350 | |
351 | gdb_assert (is_target_filename (filename)); | |
352 | ||
dcc04527 | 353 | fd = target_fileio_open (inf, |
4111f652 | 354 | filename + strlen (TARGET_SYSROOT_PREFIX), |
dcc04527 | 355 | FILEIO_O_RDONLY, 0, warn_if_slow, |
4111f652 | 356 | &target_errno); |
f08e97fe GB |
357 | if (fd == -1) |
358 | { | |
517a63c2 | 359 | errno = fileio_error_to_host (target_errno); |
f08e97fe GB |
360 | bfd_set_error (bfd_error_system_call); |
361 | return NULL; | |
362 | } | |
363 | ||
dcc04527 | 364 | return new target_fileio_stream (abfd, fd); |
f08e97fe GB |
365 | } |
366 | ||
dcc04527 | 367 | /* Wrapper for target_fileio_pread. */ |
f08e97fe | 368 | |
dcc04527 TT |
369 | file_ptr |
370 | target_fileio_stream::read (struct bfd *abfd, void *buf, | |
f08e97fe GB |
371 | file_ptr nbytes, file_ptr offset) |
372 | { | |
b872057a | 373 | fileio_error target_errno; |
f08e97fe GB |
374 | file_ptr pos, bytes; |
375 | ||
376 | pos = 0; | |
377 | while (nbytes > pos) | |
378 | { | |
2d7711a3 GB |
379 | QUIT; |
380 | ||
dcc04527 | 381 | bytes = target_fileio_pread (m_fd, (gdb_byte *) buf + pos, |
f08e97fe GB |
382 | nbytes - pos, offset + pos, |
383 | &target_errno); | |
384 | if (bytes == 0) | |
dda83cd7 SM |
385 | /* Success, but no bytes, means end-of-file. */ |
386 | break; | |
f08e97fe GB |
387 | if (bytes == -1) |
388 | { | |
517a63c2 | 389 | errno = fileio_error_to_host (target_errno); |
f08e97fe GB |
390 | bfd_set_error (bfd_error_system_call); |
391 | return -1; | |
392 | } | |
393 | ||
394 | pos += bytes; | |
395 | } | |
396 | ||
397 | return pos; | |
398 | } | |
399 | ||
3a76f8f4 PA |
400 | /* Warn that it wasn't possible to close a bfd for file NAME, because |
401 | of REASON. */ | |
402 | ||
403 | static void | |
404 | gdb_bfd_close_warning (const char *name, const char *reason) | |
405 | { | |
406 | warning (_("cannot close \"%s\": %s"), name, reason); | |
407 | } | |
408 | ||
dcc04527 | 409 | /* Wrapper for target_fileio_close. */ |
f08e97fe | 410 | |
dcc04527 | 411 | target_fileio_stream::~target_fileio_stream () |
f08e97fe | 412 | { |
b872057a | 413 | fileio_error target_errno; |
f08e97fe | 414 | |
f08e97fe GB |
415 | /* Ignore errors on close. These may happen with remote |
416 | targets if the connection has already been torn down. */ | |
3a76f8f4 PA |
417 | try |
418 | { | |
dcc04527 | 419 | target_fileio_close (m_fd, &target_errno); |
3a76f8f4 PA |
420 | } |
421 | catch (const gdb_exception &ex) | |
422 | { | |
423 | /* Also avoid crossing exceptions over bfd. */ | |
dcc04527 | 424 | gdb_bfd_close_warning (bfd_get_filename (m_bfd), |
3a76f8f4 PA |
425 | ex.message->c_str ()); |
426 | } | |
f08e97fe GB |
427 | } |
428 | ||
dcc04527 | 429 | /* Wrapper for target_fileio_fstat. */ |
f08e97fe | 430 | |
dcc04527 TT |
431 | int |
432 | target_fileio_stream::stat (struct bfd *abfd, struct stat *sb) | |
f08e97fe | 433 | { |
b872057a | 434 | fileio_error target_errno; |
f08e97fe GB |
435 | int result; |
436 | ||
dcc04527 | 437 | result = target_fileio_fstat (m_fd, sb, &target_errno); |
f08e97fe GB |
438 | if (result == -1) |
439 | { | |
517a63c2 | 440 | errno = fileio_error_to_host (target_errno); |
f08e97fe GB |
441 | bfd_set_error (bfd_error_system_call); |
442 | } | |
443 | ||
444 | return result; | |
445 | } | |
446 | ||
3cae4447 TT |
447 | /* A helper function to initialize the data that gdb attaches to each |
448 | BFD. */ | |
449 | ||
450 | static void | |
451 | gdb_bfd_init_data (struct bfd *abfd, struct stat *st) | |
452 | { | |
453 | struct gdb_bfd_data *gdata; | |
454 | void **slot; | |
455 | ||
456 | gdb_assert (bfd_usrdata (abfd) == nullptr); | |
457 | ||
458 | /* Ask BFD to decompress sections in bfd_get_full_section_contents. */ | |
459 | abfd->flags |= BFD_DECOMPRESS; | |
460 | ||
461 | gdata = new gdb_bfd_data (abfd, st); | |
462 | bfd_set_usrdata (abfd, gdata); | |
3cae4447 TT |
463 | |
464 | /* This is the first we've seen it, so add it to the hash table. */ | |
465 | slot = htab_find_slot (all_bfds, abfd, INSERT); | |
466 | gdb_assert (slot && !*slot); | |
467 | *slot = abfd; | |
468 | } | |
469 | ||
f08e97fe GB |
470 | /* See gdb_bfd.h. */ |
471 | ||
192b62ce | 472 | gdb_bfd_ref_ptr |
98c59b52 PA |
473 | gdb_bfd_open (const char *name, const char *target, int fd, |
474 | bool warn_if_slow) | |
6ec53d05 TT |
475 | { |
476 | hashval_t hash; | |
477 | void **slot; | |
478 | bfd *abfd; | |
479 | struct gdb_bfd_cache_search search; | |
480 | struct stat st; | |
481 | ||
f08e97fe GB |
482 | if (is_target_filename (name)) |
483 | { | |
484 | if (!target_filesystem_is_local ()) | |
485 | { | |
486 | gdb_assert (fd == -1); | |
487 | ||
dcc04527 TT |
488 | auto open = [&] (bfd *nbfd) -> gdb_bfd_iovec_base * |
489 | { | |
490 | return gdb_bfd_iovec_fileio_open (nbfd, current_inferior (), | |
491 | warn_if_slow); | |
492 | }; | |
493 | ||
494 | return gdb_bfd_openr_iovec (name, target, open); | |
f08e97fe GB |
495 | } |
496 | ||
497 | name += strlen (TARGET_SYSROOT_PREFIX); | |
498 | } | |
499 | ||
6ec53d05 TT |
500 | if (gdb_bfd_cache == NULL) |
501 | gdb_bfd_cache = htab_create_alloc (1, hash_bfd, eq_bfd, NULL, | |
502 | xcalloc, xfree); | |
503 | ||
504 | if (fd == -1) | |
505 | { | |
13084383 | 506 | fd = gdb_open_cloexec (name, O_RDONLY | O_BINARY, 0).release (); |
6ec53d05 TT |
507 | if (fd == -1) |
508 | { | |
509 | bfd_set_error (bfd_error_system_call); | |
510 | return NULL; | |
511 | } | |
512 | } | |
513 | ||
6ec53d05 TT |
514 | if (fstat (fd, &st) < 0) |
515 | { | |
3cae4447 | 516 | /* Weird situation here -- don't cache if we can't stat. */ |
1e15fcac | 517 | bfd_cache_debug_printf ("Could not stat %s - not caching", name); |
03b0a45f TT |
518 | abfd = bfd_fopen (name, target, FOPEN_RB, fd); |
519 | if (abfd == nullptr) | |
520 | return nullptr; | |
3cae4447 | 521 | return gdb_bfd_ref_ptr::new_reference (abfd); |
c04fe68f | 522 | } |
6ec53d05 | 523 | |
3cae4447 TT |
524 | search.filename = name; |
525 | search.mtime = st.st_mtime; | |
526 | search.size = st.st_size; | |
527 | search.inode = st.st_ino; | |
528 | search.device_id = st.st_dev; | |
529 | ||
6ec53d05 TT |
530 | /* Note that this must compute the same result as hash_bfd. */ |
531 | hash = htab_hash_string (name); | |
532 | /* Note that we cannot use htab_find_slot_with_hash here, because | |
533 | opening the BFD may fail; and this would violate hashtab | |
534 | invariants. */ | |
9a3c8263 | 535 | abfd = (struct bfd *) htab_find_with_hash (gdb_bfd_cache, &search, hash); |
18989b3c | 536 | if (bfd_sharing && abfd != NULL) |
6ec53d05 | 537 | { |
1e15fcac SM |
538 | bfd_cache_debug_printf ("Reusing cached bfd %s for %s", |
539 | host_address_to_string (abfd), | |
540 | bfd_get_filename (abfd)); | |
6ec53d05 | 541 | close (fd); |
1831a9f9 | 542 | return gdb_bfd_ref_ptr::new_reference (abfd); |
6ec53d05 TT |
543 | } |
544 | ||
545 | abfd = bfd_fopen (name, target, FOPEN_RB, fd); | |
546 | if (abfd == NULL) | |
547 | return NULL; | |
548 | ||
0ecf4403 TT |
549 | bfd_set_cacheable (abfd, 1); |
550 | ||
1e15fcac SM |
551 | bfd_cache_debug_printf ("Creating new bfd %s for %s", |
552 | host_address_to_string (abfd), | |
553 | bfd_get_filename (abfd)); | |
566f5e3b | 554 | |
18989b3c AB |
555 | if (bfd_sharing) |
556 | { | |
557 | slot = htab_find_slot_with_hash (gdb_bfd_cache, &search, hash, INSERT); | |
558 | gdb_assert (!*slot); | |
559 | *slot = abfd; | |
560 | } | |
6ec53d05 | 561 | |
3cae4447 TT |
562 | /* It's important to pass the already-computed stat info here, |
563 | rather than, say, calling gdb_bfd_ref_ptr::new_reference. BFD by | |
564 | default will "stat" the file each time bfd_get_mtime is called -- | |
565 | and since we already entered it into the hash table using this | |
566 | mtime, if the file changed at the wrong moment, the race would | |
567 | lead to a hash table corruption. */ | |
568 | gdb_bfd_init_data (abfd, &st); | |
569 | return gdb_bfd_ref_ptr (abfd); | |
6ec53d05 TT |
570 | } |
571 | ||
4bf44c1c TT |
572 | /* A helper function that releases any section data attached to the |
573 | BFD. */ | |
574 | ||
575 | static void | |
1ce51eb5 | 576 | free_one_bfd_section (asection *sectp) |
4bf44c1c | 577 | { |
9a3c8263 | 578 | struct gdb_bfd_section_data *sect |
fd361982 | 579 | = (struct gdb_bfd_section_data *) bfd_section_userdata (sectp); |
4bf44c1c TT |
580 | |
581 | if (sect != NULL && sect->data != NULL) | |
582 | { | |
583 | #ifdef HAVE_MMAP | |
584 | if (sect->map_addr != NULL) | |
585 | { | |
586 | int res; | |
587 | ||
588 | res = munmap (sect->map_addr, sect->map_len); | |
589 | gdb_assert (res == 0); | |
590 | } | |
591 | else | |
592 | #endif | |
593 | xfree (sect->data); | |
594 | } | |
595 | } | |
596 | ||
cbb099e8 TT |
597 | /* Close ABFD, and warn if that fails. */ |
598 | ||
599 | static int | |
600 | gdb_bfd_close_or_warn (struct bfd *abfd) | |
601 | { | |
602 | int ret; | |
b16c44de | 603 | const char *name = bfd_get_filename (abfd); |
cbb099e8 | 604 | |
1ce51eb5 TT |
605 | for (asection *sect : gdb_bfd_sections (abfd)) |
606 | free_one_bfd_section (sect); | |
4bf44c1c | 607 | |
cbb099e8 TT |
608 | ret = bfd_close (abfd); |
609 | ||
610 | if (!ret) | |
3a76f8f4 PA |
611 | gdb_bfd_close_warning (name, |
612 | bfd_errmsg (bfd_get_error ())); | |
cbb099e8 TT |
613 | |
614 | return ret; | |
615 | } | |
616 | ||
596f7d67 | 617 | /* See gdb_bfd.h. */ |
cbb099e8 | 618 | |
520b0001 | 619 | void |
cbb099e8 TT |
620 | gdb_bfd_ref (struct bfd *abfd) |
621 | { | |
6ec53d05 | 622 | struct gdb_bfd_data *gdata; |
cbb099e8 TT |
623 | |
624 | if (abfd == NULL) | |
520b0001 | 625 | return; |
cbb099e8 | 626 | |
9a3c8263 | 627 | gdata = (struct gdb_bfd_data *) bfd_usrdata (abfd); |
cbb099e8 | 628 | |
1e15fcac SM |
629 | bfd_cache_debug_printf ("Increase reference count on bfd %s (%s)", |
630 | host_address_to_string (abfd), | |
631 | bfd_get_filename (abfd)); | |
566f5e3b | 632 | |
6ec53d05 | 633 | if (gdata != NULL) |
cbb099e8 | 634 | { |
6ec53d05 | 635 | gdata->refc += 1; |
520b0001 | 636 | return; |
cbb099e8 TT |
637 | } |
638 | ||
3cae4447 TT |
639 | /* Caching only happens via gdb_bfd_open, so passing nullptr here is |
640 | fine. */ | |
641 | gdb_bfd_init_data (abfd, nullptr); | |
cbb099e8 TT |
642 | } |
643 | ||
596f7d67 | 644 | /* See gdb_bfd.h. */ |
cbb099e8 TT |
645 | |
646 | void | |
647 | gdb_bfd_unref (struct bfd *abfd) | |
648 | { | |
6ec53d05 TT |
649 | struct gdb_bfd_data *gdata; |
650 | struct gdb_bfd_cache_search search; | |
06d5bbc8 | 651 | bfd *archive_bfd; |
cbb099e8 TT |
652 | |
653 | if (abfd == NULL) | |
654 | return; | |
655 | ||
9a3c8263 | 656 | gdata = (struct gdb_bfd_data *) bfd_usrdata (abfd); |
6ec53d05 | 657 | gdb_assert (gdata->refc >= 1); |
cbb099e8 | 658 | |
6ec53d05 TT |
659 | gdata->refc -= 1; |
660 | if (gdata->refc > 0) | |
566f5e3b | 661 | { |
1e15fcac SM |
662 | bfd_cache_debug_printf ("Decrease reference count on bfd %s (%s)", |
663 | host_address_to_string (abfd), | |
664 | bfd_get_filename (abfd)); | |
566f5e3b AB |
665 | return; |
666 | } | |
667 | ||
1e15fcac SM |
668 | bfd_cache_debug_printf ("Delete final reference count on bfd %s (%s)", |
669 | host_address_to_string (abfd), | |
670 | bfd_get_filename (abfd)); | |
cbb099e8 | 671 | |
b82d08cd | 672 | archive_bfd = gdata->archive_bfd; |
6ec53d05 TT |
673 | search.filename = bfd_get_filename (abfd); |
674 | ||
675 | if (gdb_bfd_cache && search.filename) | |
676 | { | |
677 | hashval_t hash = htab_hash_string (search.filename); | |
678 | void **slot; | |
679 | ||
680 | search.mtime = gdata->mtime; | |
c04fe68f AB |
681 | search.size = gdata->size; |
682 | search.inode = gdata->inode; | |
683 | search.device_id = gdata->device_id; | |
6ec53d05 TT |
684 | slot = htab_find_slot_with_hash (gdb_bfd_cache, &search, hash, |
685 | NO_INSERT); | |
686 | ||
687 | if (slot && *slot) | |
688 | htab_clear_slot (gdb_bfd_cache, slot); | |
689 | } | |
690 | ||
06d5bbc8 | 691 | delete gdata; |
00f93c44 | 692 | bfd_set_usrdata (abfd, NULL); /* Paranoia. */ |
cbb099e8 | 693 | |
d6b28940 TT |
694 | htab_remove_elt (all_bfds, abfd); |
695 | ||
cbb099e8 | 696 | gdb_bfd_close_or_warn (abfd); |
b82d08cd TT |
697 | |
698 | gdb_bfd_unref (archive_bfd); | |
cbb099e8 | 699 | } |
4bf44c1c TT |
700 | |
701 | /* A helper function that returns the section data descriptor | |
702 | associated with SECTION. If no such descriptor exists, a new one | |
703 | is allocated and cleared. */ | |
704 | ||
705 | static struct gdb_bfd_section_data * | |
706 | get_section_descriptor (asection *section) | |
707 | { | |
708 | struct gdb_bfd_section_data *result; | |
709 | ||
fd361982 | 710 | result = (struct gdb_bfd_section_data *) bfd_section_userdata (section); |
4bf44c1c TT |
711 | |
712 | if (result == NULL) | |
713 | { | |
224c3ddb SM |
714 | result = ((struct gdb_bfd_section_data *) |
715 | bfd_zalloc (section->owner, sizeof (*result))); | |
fd361982 | 716 | bfd_set_section_userdata (section, result); |
4bf44c1c TT |
717 | } |
718 | ||
719 | return result; | |
720 | } | |
721 | ||
4bf44c1c TT |
722 | /* See gdb_bfd.h. */ |
723 | ||
724 | const gdb_byte * | |
725 | gdb_bfd_map_section (asection *sectp, bfd_size_type *size) | |
726 | { | |
727 | bfd *abfd; | |
4bf44c1c | 728 | struct gdb_bfd_section_data *descriptor; |
ea9f10bb | 729 | bfd_byte *data; |
4bf44c1c TT |
730 | |
731 | gdb_assert ((sectp->flags & SEC_RELOC) == 0); | |
732 | gdb_assert (size != NULL); | |
733 | ||
734 | abfd = sectp->owner; | |
735 | ||
736 | descriptor = get_section_descriptor (sectp); | |
737 | ||
738 | /* If the data was already read for this BFD, just reuse it. */ | |
739 | if (descriptor->data != NULL) | |
740 | goto done; | |
741 | ||
ea9f10bb TT |
742 | #ifdef HAVE_MMAP |
743 | if (!bfd_is_section_compressed (abfd, sectp)) | |
4bf44c1c | 744 | { |
ea9f10bb TT |
745 | /* The page size, used when mmapping. */ |
746 | static int pagesize; | |
4bf44c1c | 747 | |
ea9f10bb TT |
748 | if (pagesize == 0) |
749 | pagesize = getpagesize (); | |
750 | ||
751 | /* Only try to mmap sections which are large enough: we don't want | |
752 | to waste space due to fragmentation. */ | |
753 | ||
fd361982 | 754 | if (bfd_section_size (sectp) > 4 * pagesize) |
ea9f10bb | 755 | { |
fd361982 | 756 | descriptor->size = bfd_section_size (sectp); |
ea9f10bb TT |
757 | descriptor->data = bfd_mmap (abfd, 0, descriptor->size, PROT_READ, |
758 | MAP_PRIVATE, sectp->filepos, | |
759 | &descriptor->map_addr, | |
760 | &descriptor->map_len); | |
761 | ||
762 | if ((caddr_t)descriptor->data != MAP_FAILED) | |
763 | { | |
4bf44c1c | 764 | #if HAVE_POSIX_MADVISE |
ea9f10bb TT |
765 | posix_madvise (descriptor->map_addr, descriptor->map_len, |
766 | POSIX_MADV_WILLNEED); | |
4bf44c1c | 767 | #endif |
ea9f10bb TT |
768 | goto done; |
769 | } | |
4bf44c1c | 770 | |
ea9f10bb TT |
771 | /* On failure, clear out the section data and try again. */ |
772 | memset (descriptor, 0, sizeof (*descriptor)); | |
773 | } | |
774 | } | |
4bf44c1c TT |
775 | #endif /* HAVE_MMAP */ |
776 | ||
ea9f10bb TT |
777 | /* Handle compressed sections, or ordinary uncompressed sections in |
778 | the no-mmap case. */ | |
4bf44c1c | 779 | |
fd361982 | 780 | descriptor->size = bfd_section_size (sectp); |
ea9f10bb | 781 | descriptor->data = NULL; |
4bf44c1c | 782 | |
ea9f10bb TT |
783 | data = NULL; |
784 | if (!bfd_get_full_section_contents (abfd, sectp, &data)) | |
41667530 MG |
785 | { |
786 | warning (_("Can't read data for section '%s' in file '%s'"), | |
fd361982 | 787 | bfd_section_name (sectp), |
41667530 MG |
788 | bfd_get_filename (abfd)); |
789 | /* Set size to 0 to prevent further attempts to read the invalid | |
790 | section. */ | |
791 | *size = 0; | |
cafb3438 | 792 | return NULL; |
41667530 | 793 | } |
ea9f10bb | 794 | descriptor->data = data; |
4bf44c1c TT |
795 | |
796 | done: | |
797 | gdb_assert (descriptor->data != NULL); | |
798 | *size = descriptor->size; | |
9a3c8263 | 799 | return (const gdb_byte *) descriptor->data; |
4bf44c1c | 800 | } |
64c31149 | 801 | |
dccee2de TT |
802 | /* Return 32-bit CRC for ABFD. If successful store it to *FILE_CRC_RETURN and |
803 | return 1. Otherwise print a warning and return 0. ABFD seek position is | |
804 | not preserved. */ | |
805 | ||
806 | static int | |
807 | get_file_crc (bfd *abfd, unsigned long *file_crc_return) | |
808 | { | |
df2fc6fb | 809 | uint32_t file_crc = 0; |
dccee2de TT |
810 | |
811 | if (bfd_seek (abfd, 0, SEEK_SET) != 0) | |
812 | { | |
813 | warning (_("Problem reading \"%s\" for CRC: %s"), | |
814 | bfd_get_filename (abfd), bfd_errmsg (bfd_get_error ())); | |
815 | return 0; | |
816 | } | |
817 | ||
818 | for (;;) | |
819 | { | |
820 | gdb_byte buffer[8 * 1024]; | |
821 | bfd_size_type count; | |
822 | ||
226f9f4f | 823 | count = bfd_read (buffer, sizeof (buffer), abfd); |
dccee2de TT |
824 | if (count == (bfd_size_type) -1) |
825 | { | |
826 | warning (_("Problem reading \"%s\" for CRC: %s"), | |
827 | bfd_get_filename (abfd), bfd_errmsg (bfd_get_error ())); | |
828 | return 0; | |
829 | } | |
830 | if (count == 0) | |
831 | break; | |
832 | file_crc = bfd_calc_gnu_debuglink_crc32 (file_crc, buffer, count); | |
833 | } | |
834 | ||
835 | *file_crc_return = file_crc; | |
836 | return 1; | |
837 | } | |
838 | ||
839 | /* See gdb_bfd.h. */ | |
840 | ||
841 | int | |
842 | gdb_bfd_crc (struct bfd *abfd, unsigned long *crc_out) | |
843 | { | |
9a3c8263 | 844 | struct gdb_bfd_data *gdata = (struct gdb_bfd_data *) bfd_usrdata (abfd); |
dccee2de TT |
845 | |
846 | if (!gdata->crc_computed) | |
847 | gdata->crc_computed = get_file_crc (abfd, &gdata->crc); | |
848 | ||
849 | if (gdata->crc_computed) | |
850 | *crc_out = gdata->crc; | |
851 | return gdata->crc_computed; | |
852 | } | |
853 | ||
64c31149 TT |
854 | \f |
855 | ||
856 | /* See gdb_bfd.h. */ | |
857 | ||
192b62ce | 858 | gdb_bfd_ref_ptr |
64c31149 TT |
859 | gdb_bfd_fopen (const char *filename, const char *target, const char *mode, |
860 | int fd) | |
861 | { | |
862 | bfd *result = bfd_fopen (filename, target, mode, fd); | |
863 | ||
0ecf4403 TT |
864 | if (result != nullptr) |
865 | bfd_set_cacheable (result, 1); | |
866 | ||
1831a9f9 | 867 | return gdb_bfd_ref_ptr::new_reference (result); |
64c31149 TT |
868 | } |
869 | ||
870 | /* See gdb_bfd.h. */ | |
871 | ||
192b62ce | 872 | gdb_bfd_ref_ptr |
64c31149 TT |
873 | gdb_bfd_openr (const char *filename, const char *target) |
874 | { | |
875 | bfd *result = bfd_openr (filename, target); | |
876 | ||
1831a9f9 | 877 | return gdb_bfd_ref_ptr::new_reference (result); |
64c31149 TT |
878 | } |
879 | ||
880 | /* See gdb_bfd.h. */ | |
881 | ||
192b62ce | 882 | gdb_bfd_ref_ptr |
64c31149 TT |
883 | gdb_bfd_openw (const char *filename, const char *target) |
884 | { | |
885 | bfd *result = bfd_openw (filename, target); | |
886 | ||
1831a9f9 | 887 | return gdb_bfd_ref_ptr::new_reference (result); |
64c31149 TT |
888 | } |
889 | ||
890 | /* See gdb_bfd.h. */ | |
891 | ||
bb75a869 TT |
892 | gdb_bfd_ref_ptr |
893 | gdb_bfd_openr_iovec (const char *filename, const char *target, | |
894 | gdb_iovec_opener_ftype open_fn) | |
895 | { | |
896 | auto do_open = [] (bfd *nbfd, void *closure) -> void * | |
897 | { | |
898 | auto real_opener = static_cast<gdb_iovec_opener_ftype *> (closure); | |
899 | return (*real_opener) (nbfd); | |
900 | }; | |
901 | ||
902 | auto read_trampoline = [] (bfd *nbfd, void *stream, void *buf, | |
903 | file_ptr nbytes, file_ptr offset) -> file_ptr | |
904 | { | |
905 | gdb_bfd_iovec_base *obj = static_cast<gdb_bfd_iovec_base *> (stream); | |
906 | return obj->read (nbfd, buf, nbytes, offset); | |
907 | }; | |
908 | ||
909 | auto stat_trampoline = [] (struct bfd *abfd, void *stream, | |
910 | struct stat *sb) -> int | |
911 | { | |
912 | gdb_bfd_iovec_base *obj = static_cast<gdb_bfd_iovec_base *> (stream); | |
913 | return obj->stat (abfd, sb); | |
914 | }; | |
915 | ||
916 | auto close_trampoline = [] (struct bfd *nbfd, void *stream) -> int | |
917 | { | |
918 | gdb_bfd_iovec_base *obj = static_cast<gdb_bfd_iovec_base *> (stream); | |
919 | delete obj; | |
920 | /* Success. */ | |
921 | return 0; | |
922 | }; | |
923 | ||
924 | bfd *result = bfd_openr_iovec (filename, target, | |
925 | do_open, &open_fn, | |
926 | read_trampoline, close_trampoline, | |
927 | stat_trampoline); | |
928 | ||
929 | return gdb_bfd_ref_ptr::new_reference (result); | |
930 | } | |
931 | ||
932 | /* See gdb_bfd.h. */ | |
933 | ||
0cd61f44 TT |
934 | void |
935 | gdb_bfd_mark_parent (bfd *child, bfd *parent) | |
936 | { | |
937 | struct gdb_bfd_data *gdata; | |
938 | ||
939 | gdb_bfd_ref (child); | |
940 | /* No need to stash the filename here, because we also keep a | |
941 | reference on the parent archive. */ | |
942 | ||
9a3c8263 | 943 | gdata = (struct gdb_bfd_data *) bfd_usrdata (child); |
0cd61f44 TT |
944 | if (gdata->archive_bfd == NULL) |
945 | { | |
946 | gdata->archive_bfd = parent; | |
947 | gdb_bfd_ref (parent); | |
948 | } | |
949 | else | |
950 | gdb_assert (gdata->archive_bfd == parent); | |
951 | } | |
952 | ||
953 | /* See gdb_bfd.h. */ | |
954 | ||
192b62ce | 955 | gdb_bfd_ref_ptr |
64c31149 TT |
956 | gdb_bfd_openr_next_archived_file (bfd *archive, bfd *previous) |
957 | { | |
958 | bfd *result = bfd_openr_next_archived_file (archive, previous); | |
959 | ||
960 | if (result) | |
0cd61f44 | 961 | gdb_bfd_mark_parent (result, archive); |
64c31149 | 962 | |
192b62ce | 963 | return gdb_bfd_ref_ptr (result); |
64c31149 TT |
964 | } |
965 | ||
966 | /* See gdb_bfd.h. */ | |
967 | ||
13aaf454 DE |
968 | void |
969 | gdb_bfd_record_inclusion (bfd *includer, bfd *includee) | |
970 | { | |
971 | struct gdb_bfd_data *gdata; | |
972 | ||
9a3c8263 | 973 | gdata = (struct gdb_bfd_data *) bfd_usrdata (includer); |
1831a9f9 | 974 | gdata->included_bfds.push_back (gdb_bfd_ref_ptr::new_reference (includee)); |
13aaf454 DE |
975 | } |
976 | ||
d6b28940 TT |
977 | \f |
978 | ||
65cf3563 TT |
979 | gdb_static_assert (ARRAY_SIZE (_bfd_std_section) == 4); |
980 | ||
981 | /* See gdb_bfd.h. */ | |
982 | ||
983 | int | |
984 | gdb_bfd_section_index (bfd *abfd, asection *section) | |
985 | { | |
986 | if (section == NULL) | |
987 | return -1; | |
988 | else if (section == bfd_com_section_ptr) | |
ce9c0ca1 | 989 | return bfd_count_sections (abfd); |
65cf3563 | 990 | else if (section == bfd_und_section_ptr) |
ce9c0ca1 | 991 | return bfd_count_sections (abfd) + 1; |
65cf3563 | 992 | else if (section == bfd_abs_section_ptr) |
ce9c0ca1 | 993 | return bfd_count_sections (abfd) + 2; |
65cf3563 | 994 | else if (section == bfd_ind_section_ptr) |
ce9c0ca1 | 995 | return bfd_count_sections (abfd) + 3; |
65cf3563 TT |
996 | return section->index; |
997 | } | |
998 | ||
999 | /* See gdb_bfd.h. */ | |
1000 | ||
1001 | int | |
1002 | gdb_bfd_count_sections (bfd *abfd) | |
1003 | { | |
1004 | return bfd_count_sections (abfd) + 4; | |
1005 | } | |
1006 | ||
1da77581 TT |
1007 | /* See gdb_bfd.h. */ |
1008 | ||
1009 | int | |
1010 | gdb_bfd_requires_relocations (bfd *abfd) | |
1011 | { | |
9a3c8263 | 1012 | struct gdb_bfd_data *gdata = (struct gdb_bfd_data *) bfd_usrdata (abfd); |
1da77581 TT |
1013 | |
1014 | if (gdata->relocation_computed == 0) | |
1015 | { | |
1016 | asection *sect; | |
1017 | ||
1018 | for (sect = abfd->sections; sect != NULL; sect = sect->next) | |
1019 | if ((sect->flags & SEC_RELOC) != 0) | |
1020 | { | |
1021 | gdata->needs_relocations = 1; | |
1022 | break; | |
1023 | } | |
1024 | ||
1025 | gdata->relocation_computed = 1; | |
1026 | } | |
1027 | ||
1028 | return gdata->needs_relocations; | |
1029 | } | |
1030 | ||
e0fc5c3f SM |
1031 | /* See gdb_bfd.h. */ |
1032 | ||
1033 | bool | |
1034 | gdb_bfd_get_full_section_contents (bfd *abfd, asection *section, | |
1035 | gdb::byte_vector *contents) | |
1036 | { | |
1037 | bfd_size_type section_size = bfd_section_size (section); | |
1038 | ||
1039 | contents->resize (section_size); | |
1040 | ||
1041 | return bfd_get_section_contents (abfd, section, contents->data (), 0, | |
1042 | section_size); | |
1043 | } | |
65cf3563 | 1044 | |
34b965f7 TT |
1045 | #define AMBIGUOUS_MESS1 ".\nMatching formats:" |
1046 | #define AMBIGUOUS_MESS2 \ | |
1047 | ".\nUse \"set gnutarget format-name\" to specify the format." | |
1048 | ||
1049 | /* See gdb_bfd.h. */ | |
1050 | ||
1051 | std::string | |
1052 | gdb_bfd_errmsg (bfd_error_type error_tag, char **matching) | |
1053 | { | |
1054 | char **p; | |
1055 | ||
1056 | /* Check if errmsg just need simple return. */ | |
1057 | if (error_tag != bfd_error_file_ambiguously_recognized || matching == NULL) | |
1058 | return bfd_errmsg (error_tag); | |
1059 | ||
1060 | std::string ret (bfd_errmsg (error_tag)); | |
1061 | ret += AMBIGUOUS_MESS1; | |
1062 | ||
1063 | for (p = matching; *p; p++) | |
1064 | { | |
1065 | ret += " "; | |
1066 | ret += *p; | |
1067 | } | |
1068 | ret += AMBIGUOUS_MESS2; | |
1069 | ||
1070 | xfree (matching); | |
1071 | ||
1072 | return ret; | |
1073 | } | |
1074 | ||
d6b28940 TT |
1075 | /* A callback for htab_traverse that prints a single BFD. */ |
1076 | ||
1077 | static int | |
1078 | print_one_bfd (void **slot, void *data) | |
1079 | { | |
9a3c8263 SM |
1080 | bfd *abfd = (struct bfd *) *slot; |
1081 | struct gdb_bfd_data *gdata = (struct gdb_bfd_data *) bfd_usrdata (abfd); | |
1082 | struct ui_out *uiout = (struct ui_out *) data; | |
d6b28940 | 1083 | |
2e783024 | 1084 | ui_out_emit_tuple tuple_emitter (uiout, NULL); |
381befee | 1085 | uiout->field_signed ("refcount", gdata->refc); |
112e8700 | 1086 | uiout->field_string ("addr", host_address_to_string (abfd)); |
972f7a4b TT |
1087 | uiout->field_string ("filename", bfd_get_filename (abfd), |
1088 | file_name_style.style ()); | |
112e8700 | 1089 | uiout->text ("\n"); |
d6b28940 TT |
1090 | |
1091 | return 1; | |
1092 | } | |
1093 | ||
1094 | /* Implement the 'maint info bfd' command. */ | |
1095 | ||
1096 | static void | |
e4e33335 | 1097 | maintenance_info_bfds (const char *arg, int from_tty) |
d6b28940 | 1098 | { |
d6b28940 TT |
1099 | struct ui_out *uiout = current_uiout; |
1100 | ||
4a2b031d | 1101 | ui_out_emit_table table_emitter (uiout, 3, -1, "bfds"); |
112e8700 SM |
1102 | uiout->table_header (10, ui_left, "refcount", "Refcount"); |
1103 | uiout->table_header (18, ui_left, "addr", "Address"); | |
1104 | uiout->table_header (40, ui_left, "filename", "Filename"); | |
d6b28940 | 1105 | |
112e8700 | 1106 | uiout->table_body (); |
d6b28940 | 1107 | htab_traverse (all_bfds, print_one_bfd, uiout); |
d6b28940 TT |
1108 | } |
1109 | ||
84e60555 KB |
1110 | /* BFD related per-inferior data. */ |
1111 | ||
1112 | struct bfd_inferior_data | |
1113 | { | |
1114 | std::unordered_map<std::string, unsigned long> bfd_error_string_counts; | |
1115 | }; | |
1116 | ||
1117 | /* Per-inferior data key. */ | |
1118 | ||
1119 | static const registry<inferior>::key<bfd_inferior_data> bfd_inferior_data_key; | |
1120 | ||
1121 | /* Fetch per-inferior BFD data. It always returns a valid pointer to | |
1122 | a bfd_inferior_data struct. */ | |
1123 | ||
1124 | static struct bfd_inferior_data * | |
1125 | get_bfd_inferior_data (struct inferior *inf) | |
1126 | { | |
1127 | struct bfd_inferior_data *data; | |
1128 | ||
1129 | data = bfd_inferior_data_key.get (inf); | |
1130 | if (data == nullptr) | |
1131 | data = bfd_inferior_data_key.emplace (inf); | |
1132 | ||
1133 | return data; | |
1134 | } | |
1135 | ||
1136 | /* Increment the BFD error count for STR and return the updated | |
1137 | count. */ | |
1138 | ||
1139 | static unsigned long | |
1140 | increment_bfd_error_count (std::string str) | |
1141 | { | |
1142 | struct bfd_inferior_data *bid = get_bfd_inferior_data (current_inferior ()); | |
1143 | ||
1144 | auto &map = bid->bfd_error_string_counts; | |
1145 | return ++map[std::move (str)]; | |
1146 | } | |
1147 | ||
1148 | static bfd_error_handler_type default_bfd_error_handler; | |
1149 | ||
1150 | /* Define a BFD error handler which will suppress the printing of | |
1151 | messages which have been printed once already. This is done on a | |
1152 | per-inferior basis. */ | |
1153 | ||
77b7377b | 1154 | static void ATTRIBUTE_PRINTF (1, 0) |
84e60555 KB |
1155 | gdb_bfd_error_handler (const char *fmt, va_list ap) |
1156 | { | |
1157 | va_list ap_copy; | |
1158 | ||
1159 | va_copy(ap_copy, ap); | |
1160 | const std::string str = string_vprintf (fmt, ap_copy); | |
1161 | va_end (ap_copy); | |
1162 | ||
1163 | if (increment_bfd_error_count (std::move (str)) > 1) | |
1164 | return; | |
1165 | ||
1166 | /* We must call the BFD mechanism for printing format strings since | |
1167 | it supports additional format specifiers that GDB's vwarning() doesn't | |
1168 | recognize. It also outputs additional text, i.e. "BFD: ", which | |
1169 | makes it clear that it's a BFD warning/error. */ | |
1170 | (*default_bfd_error_handler) (fmt, ap); | |
1171 | } | |
1172 | ||
6c265988 | 1173 | void _initialize_gdb_bfd (); |
d6b28940 | 1174 | void |
6c265988 | 1175 | _initialize_gdb_bfd () |
d6b28940 TT |
1176 | { |
1177 | all_bfds = htab_create_alloc (10, htab_hash_pointer, htab_eq_pointer, | |
1178 | NULL, xcalloc, xfree); | |
1179 | ||
1180 | add_cmd ("bfds", class_maintenance, maintenance_info_bfds, _("\ | |
1181 | List the BFDs that are currently open."), | |
1182 | &maintenanceinfolist); | |
18989b3c AB |
1183 | |
1184 | add_setshow_boolean_cmd ("bfd-sharing", no_class, | |
1185 | &bfd_sharing, _("\ | |
1186 | Set whether gdb will share bfds that appear to be the same file."), _("\ | |
1187 | Show whether gdb will share bfds that appear to be the same file."), _("\ | |
1188 | When enabled gdb will reuse existing bfds rather than reopening the\n\ | |
1189 | same file. To decide if two files are the same then gdb compares the\n\ | |
1190 | filename, file size, file modification time, and file inode."), | |
1191 | NULL, | |
1192 | &show_bfd_sharing, | |
1193 | &maintenance_set_cmdlist, | |
1194 | &maintenance_show_cmdlist); | |
566f5e3b | 1195 | |
d4dd4fca SM |
1196 | add_setshow_boolean_cmd ("bfd-cache", class_maintenance, |
1197 | &debug_bfd_cache, | |
1198 | _("Set bfd cache debugging."), | |
1199 | _("Show bfd cache debugging."), | |
1200 | _("\ | |
566f5e3b | 1201 | When non-zero, bfd cache specific debugging is enabled."), |
d4dd4fca SM |
1202 | NULL, |
1203 | &show_bfd_cache_debug, | |
1204 | &setdebuglist, &showdebuglist); | |
84e60555 KB |
1205 | |
1206 | /* Hook the BFD error/warning handler to limit amount of output. */ | |
1207 | default_bfd_error_handler = bfd_set_error_handler (gdb_bfd_error_handler); | |
d6b28940 | 1208 | } |