GDB (xrefs)
Loading...
Searching...
No Matches
gdb_bfd.c
Go to the documentation of this file.
1/* Definitions for BFD wrappers used by GDB.
2
3 Copyright (C) 2011-2023 Free Software Foundation, Inc.
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"
22#include "ui-out.h"
23#include "gdbcmd.h"
24#include "hashtab.h"
25#include "gdbsupport/filestuff.h"
26#ifdef HAVE_MMAP
27#include <sys/mman.h>
28#ifndef MAP_FAILED
29#define MAP_FAILED ((void *) -1)
30#endif
31#endif
32#include "target.h"
33#include "gdbsupport/fileio.h"
34#include "inferior.h"
35#include "cli/cli-style.h"
36#include <unordered_map>
37
38/* An object of this type is stored in the section's user data when
39 mapping a section. */
40
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};
52
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
58static htab_t all_bfds;
59
60/* An object of this type is stored in each BFD's user data. */
61
63{
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),
72 crc_computed (0)
73 {
74 }
75
77 {
78 }
79
80 /* The reference count. */
81 int refc = 1;
82
83 /* The mtime of the BFD at the point the cache entry was made. */
84 time_t mtime;
85
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
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
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. */
106 unsigned long crc = 0;
107
108 /* If the BFD comes from an archive, this points to the archive's
109 BFD. Otherwise, this is NULL. */
110 bfd *archive_bfd = nullptr;
111
112 /* Table of all the bfds this bfd has included. */
113 std::vector<gdb_bfd_ref_ptr> included_bfds;
114
115 /* The registry. */
117};
118
121{
122 struct gdb_bfd_data *gdata = (struct gdb_bfd_data *) bfd_usrdata (abfd);
123 return &gdata->registry_fields;
124}
125
126/* A hash table storing all the BFDs maintained in the cache. */
127
128static htab_t gdb_bfd_cache;
129
130/* When true gdb will reuse an existing bfd object if the filename,
131 modification time, and file size all match. */
132
133static bool bfd_sharing = true;
134static void
135show_bfd_sharing (struct ui_file *file, int from_tty,
136 struct cmd_list_element *c, const char *value)
137{
138 gdb_printf (file, _("BFD sharing is %s.\n"), value);
139}
140
141/* When true debugging of the bfd caches is enabled. */
142
143static bool debug_bfd_cache;
144
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
150static void
151show_bfd_cache_debug (struct ui_file *file, int from_tty,
152 struct cmd_list_element *c, const char *value)
153{
154 gdb_printf (file, _("BFD cache debugging is %s.\n"), value);
155}
156
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
162{
163 /* The filename. */
164 const char *filename;
165 /* The mtime. */
166 time_t mtime;
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. */
173};
174
175/* A hash function for BFDs. */
176
177static hashval_t
178hash_bfd (const void *b)
179{
180 const bfd *abfd = (const struct bfd *) b;
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
189static int
190eq_bfd (const void *a, const void *b)
191{
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);
196
197 return (gdata->mtime == s->mtime
198 && gdata->size == s->size
199 && gdata->inode == s->inode
200 && gdata->device_id == s->device_id
201 && strcmp (bfd_get_filename (abfd), s->filename) == 0);
202}
203
204/* See gdb_bfd.h. */
205
206int
208{
209 return startswith (name, TARGET_SYSROOT_PREFIX);
210}
211
212/* See gdb_bfd.h. */
213
214int
216{
217 return is_target_filename (bfd_get_filename (abfd));
218}
219
220/* For `gdb_bfd_open_from_target_memory`. An object that manages the
221 details of a BFD in target memory. */
222
224{
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),
229 m_size (size),
230 m_filename (xstrprintf ("<in-memory@%s>",
231 core_addr_to_string_nz (m_base)))
232 {
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
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
253private:
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;
262};
263
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
268file_ptr
269target_buffer::read (struct bfd *abfd, void *buf,
270 file_ptr nbytes, file_ptr offset)
271{
272 /* If this read will read all of the file, limit it to just the rest. */
273 if (offset + nbytes > size ())
274 nbytes = size () - offset;
275
276 /* If there are no more bytes left, we've reached EOF. */
277 if (nbytes == 0)
278 return 0;
279
280 int err = target_read_memory (base () + offset, (gdb_byte *) buf, nbytes);
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
290int
291target_buffer::stat (struct bfd *abfd, struct stat *sb)
292{
293 memset (sb, 0, sizeof (struct stat));
294 sb->st_size = size ();
295 return 0;
296}
297
298/* See gdb_bfd.h. */
299
301gdb_bfd_open_from_target_memory (CORE_ADDR addr, ULONGEST size,
302 const char *target)
303{
304 std::unique_ptr<target_buffer> buffer
305 = gdb::make_unique<target_buffer> (addr, size);
306
307 return gdb_bfd_openr_iovec (buffer->filename (), target,
308 [&] (bfd *nbfd)
309 {
310 return buffer.release ();
311 });
312}
313
314/* An object that manages the underlying stream for a BFD, using
315 target file I/O. */
316
318{
319 target_fileio_stream (bfd *nbfd, int fd)
320 : m_bfd (nbfd),
321 m_fd (fd)
322 {
323 }
324
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
332private:
333
334 /* The BFD. Saved for the destructor. */
335 bfd *m_bfd;
336
337 /* The file descriptor. */
338 int m_fd;
339};
340
341/* Wrapper for target_fileio_open suitable for use as a helper
342 function for gdb_bfd_openr_iovec. */
343
345gdb_bfd_iovec_fileio_open (struct bfd *abfd, inferior *inf, bool warn_if_slow)
346{
347 const char *filename = bfd_get_filename (abfd);
348 int fd;
349 fileio_error target_errno;
350
351 gdb_assert (is_target_filename (filename));
352
354 filename + strlen (TARGET_SYSROOT_PREFIX),
355 FILEIO_O_RDONLY, 0, warn_if_slow,
356 &target_errno);
357 if (fd == -1)
358 {
359 errno = fileio_error_to_host (target_errno);
360 bfd_set_error (bfd_error_system_call);
361 return NULL;
362 }
363
364 return new target_fileio_stream (abfd, fd);
365}
366
367/* Wrapper for target_fileio_pread. */
368
369file_ptr
370target_fileio_stream::read (struct bfd *abfd, void *buf,
371 file_ptr nbytes, file_ptr offset)
372{
373 fileio_error target_errno;
374 file_ptr pos, bytes;
375
376 pos = 0;
377 while (nbytes > pos)
378 {
379 QUIT;
380
381 bytes = target_fileio_pread (m_fd, (gdb_byte *) buf + pos,
382 nbytes - pos, offset + pos,
383 &target_errno);
384 if (bytes == 0)
385 /* Success, but no bytes, means end-of-file. */
386 break;
387 if (bytes == -1)
388 {
389 errno = fileio_error_to_host (target_errno);
390 bfd_set_error (bfd_error_system_call);
391 return -1;
392 }
393
394 pos += bytes;
395 }
396
397 return pos;
398}
399
400/* Warn that it wasn't possible to close a bfd for file NAME, because
401 of REASON. */
402
403static void
404gdb_bfd_close_warning (const char *name, const char *reason)
405{
406 warning (_("cannot close \"%s\": %s"), name, reason);
407}
408
409/* Wrapper for target_fileio_close. */
410
412{
413 fileio_error target_errno;
414
415 /* Ignore errors on close. These may happen with remote
416 targets if the connection has already been torn down. */
417 try
418 {
419 target_fileio_close (m_fd, &target_errno);
420 }
421 catch (const gdb_exception &ex)
422 {
423 /* Also avoid crossing exceptions over bfd. */
424 gdb_bfd_close_warning (bfd_get_filename (m_bfd),
425 ex.message->c_str ());
426 }
427}
428
429/* Wrapper for target_fileio_fstat. */
430
431int
432target_fileio_stream::stat (struct bfd *abfd, struct stat *sb)
433{
434 fileio_error target_errno;
435 int result;
436
437 result = target_fileio_fstat (m_fd, sb, &target_errno);
438 if (result == -1)
439 {
440 errno = fileio_error_to_host (target_errno);
441 bfd_set_error (bfd_error_system_call);
442 }
443
444 return result;
445}
446
447/* A helper function to initialize the data that gdb attaches to each
448 BFD. */
449
450static void
451gdb_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);
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
470/* See gdb_bfd.h. */
471
473gdb_bfd_open (const char *name, const char *target, int fd,
474 bool warn_if_slow)
475{
476 hashval_t hash;
477 void **slot;
478 bfd *abfd;
479 struct gdb_bfd_cache_search search;
480 struct stat st;
481
483 {
485 {
486 gdb_assert (fd == -1);
487
488 auto open = [&] (bfd *nbfd) -> gdb_bfd_iovec_base *
489 {
491 warn_if_slow);
492 };
493
494 return gdb_bfd_openr_iovec (name, target, open);
495 }
496
497 name += strlen (TARGET_SYSROOT_PREFIX);
498 }
499
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 {
506 fd = gdb_open_cloexec (name, O_RDONLY | O_BINARY, 0).release ();
507 if (fd == -1)
508 {
509 bfd_set_error (bfd_error_system_call);
510 return NULL;
511 }
512 }
513
514 if (fstat (fd, &st) < 0)
515 {
516 /* Weird situation here -- don't cache if we can't stat. */
517 bfd_cache_debug_printf ("Could not stat %s - not caching", name);
518 abfd = bfd_fopen (name, target, FOPEN_RB, fd);
519 if (abfd == nullptr)
520 return nullptr;
521 return gdb_bfd_ref_ptr::new_reference (abfd);
522 }
523
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
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. */
535 abfd = (struct bfd *) htab_find_with_hash (gdb_bfd_cache, &search, hash);
536 if (bfd_sharing && abfd != NULL)
537 {
538 bfd_cache_debug_printf ("Reusing cached bfd %s for %s",
539 host_address_to_string (abfd),
540 bfd_get_filename (abfd));
541 close (fd);
542 return gdb_bfd_ref_ptr::new_reference (abfd);
543 }
544
545 abfd = bfd_fopen (name, target, FOPEN_RB, fd);
546 if (abfd == NULL)
547 return NULL;
548
549 bfd_set_cacheable (abfd, 1);
550
551 bfd_cache_debug_printf ("Creating new bfd %s for %s",
552 host_address_to_string (abfd),
553 bfd_get_filename (abfd));
554
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 }
561
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);
570}
571
572/* A helper function that releases any section data attached to the
573 BFD. */
574
575static void
576free_one_bfd_section (asection *sectp)
577{
578 struct gdb_bfd_section_data *sect
579 = (struct gdb_bfd_section_data *) bfd_section_userdata (sectp);
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
597/* Close ABFD, and warn if that fails. */
598
599static int
600gdb_bfd_close_or_warn (struct bfd *abfd)
601{
602 int ret;
603 const char *name = bfd_get_filename (abfd);
604
605 for (asection *sect : gdb_bfd_sections (abfd))
607
608 ret = bfd_close (abfd);
609
610 if (!ret)
612 bfd_errmsg (bfd_get_error ()));
613
614 return ret;
615}
616
617/* See gdb_bfd.h. */
618
619void
620gdb_bfd_ref (struct bfd *abfd)
621{
622 struct gdb_bfd_data *gdata;
623
624 if (abfd == NULL)
625 return;
626
627 gdata = (struct gdb_bfd_data *) bfd_usrdata (abfd);
628
629 bfd_cache_debug_printf ("Increase reference count on bfd %s (%s)",
630 host_address_to_string (abfd),
631 bfd_get_filename (abfd));
632
633 if (gdata != NULL)
634 {
635 gdata->refc += 1;
636 return;
637 }
638
639 /* Caching only happens via gdb_bfd_open, so passing nullptr here is
640 fine. */
641 gdb_bfd_init_data (abfd, nullptr);
642}
643
644/* See gdb_bfd.h. */
645
646void
647gdb_bfd_unref (struct bfd *abfd)
648{
649 struct gdb_bfd_data *gdata;
650 struct gdb_bfd_cache_search search;
651 bfd *archive_bfd;
652
653 if (abfd == NULL)
654 return;
655
656 gdata = (struct gdb_bfd_data *) bfd_usrdata (abfd);
657 gdb_assert (gdata->refc >= 1);
658
659 gdata->refc -= 1;
660 if (gdata->refc > 0)
661 {
662 bfd_cache_debug_printf ("Decrease reference count on bfd %s (%s)",
663 host_address_to_string (abfd),
664 bfd_get_filename (abfd));
665 return;
666 }
667
668 bfd_cache_debug_printf ("Delete final reference count on bfd %s (%s)",
669 host_address_to_string (abfd),
670 bfd_get_filename (abfd));
671
672 archive_bfd = gdata->archive_bfd;
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;
681 search.size = gdata->size;
682 search.inode = gdata->inode;
683 search.device_id = gdata->device_id;
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
691 delete gdata;
692 bfd_set_usrdata (abfd, NULL); /* Paranoia. */
693
694 htab_remove_elt (all_bfds, abfd);
695
697
699}
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
705static struct gdb_bfd_section_data *
706get_section_descriptor (asection *section)
707{
708 struct gdb_bfd_section_data *result;
709
710 result = (struct gdb_bfd_section_data *) bfd_section_userdata (section);
711
712 if (result == NULL)
713 {
714 result = ((struct gdb_bfd_section_data *)
715 bfd_zalloc (section->owner, sizeof (*result)));
716 bfd_set_section_userdata (section, result);
717 }
718
719 return result;
720}
721
722/* See gdb_bfd.h. */
723
724const gdb_byte *
725gdb_bfd_map_section (asection *sectp, bfd_size_type *size)
726{
727 bfd *abfd;
728 struct gdb_bfd_section_data *descriptor;
729 bfd_byte *data;
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
742#ifdef HAVE_MMAP
743 if (!bfd_is_section_compressed (abfd, sectp))
744 {
745 /* The page size, used when mmapping. */
746 static int pagesize;
747
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
754 if (bfd_section_size (sectp) > 4 * pagesize)
755 {
756 descriptor->size = bfd_section_size (sectp);
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 {
764#if HAVE_POSIX_MADVISE
765 posix_madvise (descriptor->map_addr, descriptor->map_len,
766 POSIX_MADV_WILLNEED);
767#endif
768 goto done;
769 }
770
771 /* On failure, clear out the section data and try again. */
772 memset (descriptor, 0, sizeof (*descriptor));
773 }
774 }
775#endif /* HAVE_MMAP */
776
777 /* Handle compressed sections, or ordinary uncompressed sections in
778 the no-mmap case. */
779
780 descriptor->size = bfd_section_size (sectp);
781 descriptor->data = NULL;
782
783 data = NULL;
784 if (!bfd_get_full_section_contents (abfd, sectp, &data))
785 {
786 warning (_("Can't read data for section '%s' in file '%s'"),
787 bfd_section_name (sectp),
788 bfd_get_filename (abfd));
789 /* Set size to 0 to prevent further attempts to read the invalid
790 section. */
791 *size = 0;
792 return NULL;
793 }
794 descriptor->data = data;
795
796 done:
797 gdb_assert (descriptor->data != NULL);
798 *size = descriptor->size;
799 return (const gdb_byte *) descriptor->data;
800}
801
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
806static int
807get_file_crc (bfd *abfd, unsigned long *file_crc_return)
808{
809 uint32_t file_crc = 0;
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
823 count = bfd_read (buffer, sizeof (buffer), abfd);
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
841int
842gdb_bfd_crc (struct bfd *abfd, unsigned long *crc_out)
843{
844 struct gdb_bfd_data *gdata = (struct gdb_bfd_data *) bfd_usrdata (abfd);
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
854
855
856/* See gdb_bfd.h. */
857
859gdb_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
864 if (result != nullptr)
865 bfd_set_cacheable (result, 1);
866
867 return gdb_bfd_ref_ptr::new_reference (result);
868}
869
870/* See gdb_bfd.h. */
871
873gdb_bfd_openr (const char *filename, const char *target)
874{
875 bfd *result = bfd_openr (filename, target);
876
877 return gdb_bfd_ref_ptr::new_reference (result);
878}
879
880/* See gdb_bfd.h. */
881
883gdb_bfd_openw (const char *filename, const char *target)
884{
885 bfd *result = bfd_openw (filename, target);
886
887 return gdb_bfd_ref_ptr::new_reference (result);
888}
889
890/* See gdb_bfd.h. */
891
893gdb_bfd_openr_iovec (const char *filename, const char *target,
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
934void
935gdb_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
943 gdata = (struct gdb_bfd_data *) bfd_usrdata (child);
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
956gdb_bfd_openr_next_archived_file (bfd *archive, bfd *previous)
957{
958 bfd *result = bfd_openr_next_archived_file (archive, previous);
959
960 if (result)
961 gdb_bfd_mark_parent (result, archive);
962
963 return gdb_bfd_ref_ptr (result);
964}
965
966/* See gdb_bfd.h. */
967
968void
969gdb_bfd_record_inclusion (bfd *includer, bfd *includee)
970{
971 struct gdb_bfd_data *gdata;
972
973 gdata = (struct gdb_bfd_data *) bfd_usrdata (includer);
974 gdata->included_bfds.push_back (gdb_bfd_ref_ptr::new_reference (includee));
975}
976
977
978
979gdb_static_assert (ARRAY_SIZE (_bfd_std_section) == 4);
980
981/* See gdb_bfd.h. */
982
983int
984gdb_bfd_section_index (bfd *abfd, asection *section)
985{
986 if (section == NULL)
987 return -1;
988 else if (section == bfd_com_section_ptr)
989 return bfd_count_sections (abfd);
990 else if (section == bfd_und_section_ptr)
991 return bfd_count_sections (abfd) + 1;
992 else if (section == bfd_abs_section_ptr)
993 return bfd_count_sections (abfd) + 2;
994 else if (section == bfd_ind_section_ptr)
995 return bfd_count_sections (abfd) + 3;
996 return section->index;
997}
998
999/* See gdb_bfd.h. */
1000
1001int
1003{
1004 return bfd_count_sections (abfd) + 4;
1005}
1006
1007/* See gdb_bfd.h. */
1008
1009int
1011{
1012 struct gdb_bfd_data *gdata = (struct gdb_bfd_data *) bfd_usrdata (abfd);
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
1031/* See gdb_bfd.h. */
1032
1033bool
1034gdb_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}
1044
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
1051std::string
1052gdb_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
1075/* A callback for htab_traverse that prints a single BFD. */
1076
1077static int
1078print_one_bfd (void **slot, void *data)
1079{
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;
1083
1084 ui_out_emit_tuple tuple_emitter (uiout, NULL);
1085 uiout->field_signed ("refcount", gdata->refc);
1086 uiout->field_string ("addr", host_address_to_string (abfd));
1087 uiout->field_string ("filename", bfd_get_filename (abfd),
1089 uiout->text ("\n");
1090
1091 return 1;
1092}
1093
1094/* Implement the 'maint info bfd' command. */
1095
1096static void
1097maintenance_info_bfds (const char *arg, int from_tty)
1098{
1099 struct ui_out *uiout = current_uiout;
1100
1101 ui_out_emit_table table_emitter (uiout, 3, -1, "bfds");
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");
1105
1106 uiout->table_body ();
1107 htab_traverse (all_bfds, print_one_bfd, uiout);
1108}
1109
1110/* BFD related per-inferior data. */
1111
1113{
1114 std::unordered_map<std::string, unsigned long> bfd_error_string_counts;
1115};
1116
1117/* Per-inferior data key. */
1118
1120
1121/* Fetch per-inferior BFD data. It always returns a valid pointer to
1122 a bfd_inferior_data struct. */
1123
1124static struct bfd_inferior_data *
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
1139static unsigned long
1141{
1143
1144 auto &map = bid->bfd_error_string_counts;
1145 return ++map[std::move (str)];
1146}
1147
1148static 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
1154static void ATTRIBUTE_PRINTF (1, 0)
1155gdb_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
1173void _initialize_gdb_bfd ();
1174void
1176{
1177 all_bfds = htab_create_alloc (10, htab_hash_pointer, htab_eq_pointer,
1178 NULL, xcalloc, xfree);
1179
1181List the BFDs that are currently open."),
1183
1184 add_setshow_boolean_cmd ("bfd-sharing", no_class,
1185 &bfd_sharing, _("\
1186Set whether gdb will share bfds that appear to be the same file."), _("\
1187Show whether gdb will share bfds that appear to be the same file."), _("\
1188When enabled gdb will reuse existing bfds rather than reopening the\n\
1189same file. To decide if two files are the same then gdb compares the\n\
1190filename, file size, file modification time, and file inode."),
1191 NULL,
1195
1198 _("Set bfd cache debugging."),
1199 _("Show bfd cache debugging."),
1200 _("\
1201When non-zero, bfd cache specific debugging is enabled."),
1202 NULL,
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);
1208}
const char *const name
void xfree(void *)
void * xcalloc(size_t number, size_t size)
Definition alloc.c:85
ui_file_style style() const
Definition cli-style.c:169
virtual int stat(struct bfd *abfd, struct stat *sb)=0
virtual file_ptr read(bfd *abfd, void *buffer, file_ptr nbytes, file_ptr offset)=0
void * get(unsigned key)
Definition registry.h:211
void field_string(const char *fldname, const char *string, const ui_file_style &style=ui_file_style())
Definition ui-out.c:511
void field_signed(const char *fldname, LONGEST value)
Definition ui-out.c:437
void text(const char *string)
Definition ui-out.c:566
void table_header(int width, ui_align align, const std::string &col_name, const std::string &col_hdr)
Definition ui-out.c:363
void table_body()
Definition ui-out.c:376
struct cmd_list_element * showdebuglist
Definition cli-cmds.c:167
struct cmd_list_element * maintenanceinfolist
Definition cli-cmds.c:147
struct cmd_list_element * setdebuglist
Definition cli-cmds.c:165
struct cmd_list_element * maintenance_show_cmdlist
Definition maint.c:752
struct cmd_list_element * maintenance_set_cmdlist
Definition maint.c:751
struct cmd_list_element * add_cmd(const char *name, enum command_class theclass, const char *doc, struct cmd_list_element **list)
Definition cli-decode.c:233
set_show_commands add_setshow_boolean_cmd(const char *name, enum command_class theclass, bool *var, const char *set_doc, const char *show_doc, const char *help_doc, cmd_func_ftype *set_func, show_value_ftype *show_func, struct cmd_list_element **set_list, struct cmd_list_element **show_list)
Definition cli-decode.c:809
cli_style_option file_name_style
@ class_maintenance
Definition command.h:65
@ no_class
Definition command.h:53
#define SEEK_SET
Definition defs.h:101
#define O_BINARY
Definition defs.h:114
#define QUIT
Definition defs.h:187
#define AMBIGUOUS_MESS2
Definition gdb_bfd.c:1046
static void show_bfd_cache_debug(struct ui_file *file, int from_tty, struct cmd_list_element *c, const char *value)
Definition gdb_bfd.c:151
void gdb_bfd_unref(struct bfd *abfd)
Definition gdb_bfd.c:647
static void maintenance_info_bfds(const char *arg, int from_tty)
Definition gdb_bfd.c:1097
static int get_file_crc(bfd *abfd, unsigned long *file_crc_return)
Definition gdb_bfd.c:807
gdb_bfd_ref_ptr gdb_bfd_fopen(const char *filename, const char *target, const char *mode, int fd)
Definition gdb_bfd.c:859
static unsigned long increment_bfd_error_count(std::string str)
Definition gdb_bfd.c:1140
static struct bfd_inferior_data * get_bfd_inferior_data(struct inferior *inf)
Definition gdb_bfd.c:1125
gdb_bfd_ref_ptr gdb_bfd_openr_iovec(const char *filename, const char *target, gdb_iovec_opener_ftype open_fn)
Definition gdb_bfd.c:893
static void ATTRIBUTE_PRINTF(1, 0)
Definition gdb_bfd.c:1154
static htab_t all_bfds
Definition gdb_bfd.c:58
gdb_static_assert(ARRAY_SIZE(_bfd_std_section)==4)
static bool bfd_sharing
Definition gdb_bfd.c:133
static int gdb_bfd_close_or_warn(struct bfd *abfd)
Definition gdb_bfd.c:600
int gdb_bfd_crc(struct bfd *abfd, unsigned long *crc_out)
Definition gdb_bfd.c:842
gdb_bfd_ref_ptr gdb_bfd_openr(const char *filename, const char *target)
Definition gdb_bfd.c:873
static int print_one_bfd(void **slot, void *data)
Definition gdb_bfd.c:1078
static struct gdb_bfd_section_data * get_section_descriptor(asection *section)
Definition gdb_bfd.c:706
static htab_t gdb_bfd_cache
Definition gdb_bfd.c:128
static bfd_error_handler_type default_bfd_error_handler
Definition gdb_bfd.c:1148
static void show_bfd_sharing(struct ui_file *file, int from_tty, struct cmd_list_element *c, const char *value)
Definition gdb_bfd.c:135
void _initialize_gdb_bfd()
Definition gdb_bfd.c:1175
int gdb_bfd_section_index(bfd *abfd, asection *section)
Definition gdb_bfd.c:984
std::string gdb_bfd_errmsg(bfd_error_type error_tag, char **matching)
Definition gdb_bfd.c:1052
int gdb_bfd_requires_relocations(bfd *abfd)
Definition gdb_bfd.c:1010
gdb_bfd_ref_ptr gdb_bfd_openw(const char *filename, const char *target)
Definition gdb_bfd.c:883
int gdb_bfd_has_target_filename(struct bfd *abfd)
Definition gdb_bfd.c:215
static void gdb_bfd_close_warning(const char *name, const char *reason)
Definition gdb_bfd.c:404
static int eq_bfd(const void *a, const void *b)
Definition gdb_bfd.c:190
int gdb_bfd_count_sections(bfd *abfd)
Definition gdb_bfd.c:1002
void gdb_bfd_mark_parent(bfd *child, bfd *parent)
Definition gdb_bfd.c:935
gdb_bfd_ref_ptr gdb_bfd_openr_next_archived_file(bfd *archive, bfd *previous)
Definition gdb_bfd.c:956
static hashval_t hash_bfd(const void *b)
Definition gdb_bfd.c:178
void gdb_bfd_record_inclusion(bfd *includer, bfd *includee)
Definition gdb_bfd.c:969
#define AMBIGUOUS_MESS1
Definition gdb_bfd.c:1045
static bool debug_bfd_cache
Definition gdb_bfd.c:143
void gdb_bfd_ref(struct bfd *abfd)
Definition gdb_bfd.c:620
static void free_one_bfd_section(asection *sectp)
Definition gdb_bfd.c:576
int is_target_filename(const char *name)
Definition gdb_bfd.c:207
#define bfd_cache_debug_printf(fmt,...)
Definition gdb_bfd.c:147
static void gdb_bfd_init_data(struct bfd *abfd, struct stat *st)
Definition gdb_bfd.c:451
gdb_bfd_ref_ptr gdb_bfd_open(const char *name, const char *target, int fd, bool warn_if_slow)
Definition gdb_bfd.c:473
gdb_bfd_ref_ptr gdb_bfd_open_from_target_memory(CORE_ADDR addr, ULONGEST size, const char *target)
Definition gdb_bfd.c:301
const gdb_byte * gdb_bfd_map_section(asection *sectp, bfd_size_type *size)
Definition gdb_bfd.c:725
static const registry< inferior >::key< bfd_inferior_data > bfd_inferior_data_key
Definition gdb_bfd.c:1119
static target_fileio_stream * gdb_bfd_iovec_fileio_open(struct bfd *abfd, inferior *inf, bool warn_if_slow)
Definition gdb_bfd.c:345
bool gdb_bfd_get_full_section_contents(bfd *abfd, asection *section, gdb::byte_vector *contents)
Definition gdb_bfd.c:1034
#define TARGET_SYSROOT_PREFIX
Definition gdb_bfd.h:41
gdb::ref_ptr< struct bfd, gdb_bfd_ref_policy > gdb_bfd_ref_ptr
Definition gdb_bfd.h:79
gdb::function_view< gdb_bfd_iovec_base *(bfd *)> gdb_iovec_opener_ftype
Definition gdb_bfd.h:176
static gdb_bfd_section_range gdb_bfd_sections(bfd *abfd)
Definition gdb_bfd.h:234
mach_port_t mach_port_t name mach_port_t mach_port_t name kern_return_t err
Definition gnu-nat.c:1789
size_t size
Definition go32-nat.c:239
struct inferior * current_inferior(void)
Definition inferior.c:55
std::unordered_map< std::string, unsigned long > bfd_error_string_counts
Definition gdb_bfd.c:1114
const char * filename
Definition gdb_bfd.c:164
unsigned int needs_relocations
Definition gdb_bfd.c:100
bfd * archive_bfd
Definition gdb_bfd.c:110
dev_t device_id
Definition gdb_bfd.c:93
ino_t inode
Definition gdb_bfd.c:90
std::vector< gdb_bfd_ref_ptr > included_bfds
Definition gdb_bfd.c:113
time_t mtime
Definition gdb_bfd.c:84
unsigned long crc
Definition gdb_bfd.c:106
registry< bfd > registry_fields
Definition gdb_bfd.c:116
off_t size
Definition gdb_bfd.c:87
gdb_bfd_data(bfd *abfd, struct stat *st)
Definition gdb_bfd.c:65
~gdb_bfd_data()
Definition gdb_bfd.c:76
unsigned int relocation_computed
Definition gdb_bfd.c:97
unsigned int crc_computed
Definition gdb_bfd.c:103
bfd_size_type size
Definition gdb_bfd.c:44
bfd_size_type map_len
Definition gdb_bfd.c:46
Definition gnu-nat.c:153
static registry< T > * get(T *obj)
Definition registry.h:41
const char * filename() const
Definition gdb_bfd.c:245
gdb::unique_xmalloc_ptr< char > m_filename
Definition gdb_bfd.c:261
CORE_ADDR m_base
Definition gdb_bfd.c:255
target_buffer(CORE_ADDR base, ULONGEST size)
Definition gdb_bfd.c:227
ULONGEST m_size
Definition gdb_bfd.c:258
CORE_ADDR base() const
Definition gdb_bfd.c:240
int stat(struct bfd *abfd, struct stat *sb) override
Definition gdb_bfd.c:291
ULONGEST size() const
Definition gdb_bfd.c:236
file_ptr read(bfd *abfd, void *buffer, file_ptr nbytes, file_ptr offset) override
Definition gdb_bfd.c:269
target_fileio_stream(bfd *nbfd, int fd)
Definition gdb_bfd.c:319
file_ptr read(bfd *abfd, void *buffer, file_ptr nbytes, file_ptr offset) override
Definition gdb_bfd.c:370
int stat(struct bfd *abfd, struct stat *sb) override
Definition gdb_bfd.c:432
Definition value.h:130
int target_fileio_fstat(int fd, struct stat *sb, fileio_error *target_errno)
Definition target.c:3346
bool target_filesystem_is_local()
Definition target.c:608
int target_fileio_open(struct inferior *inf, const char *filename, int flags, int mode, bool warn_if_slow, fileio_error *target_errno)
Definition target.c:3260
int target_fileio_close(int fd, fileio_error *target_errno)
Definition target.c:3368
int target_fileio_pread(int fd, gdb_byte *read_buf, int len, ULONGEST offset, fileio_error *target_errno)
Definition target.c:3320
int target_read_memory(CORE_ADDR memaddr, gdb_byte *myaddr, ssize_t len)
Definition target.c:1785
@ ui_left
Definition ui-out.h:45
#define current_uiout
Definition ui-out.h:40
void gdb_printf(struct ui_file *stream, const char *format,...)
Definition utils.c:1886
#define nullptr
Definition x86-cpuid.h:28