1 /* Definitions for BFD wrappers used by GDB.
3 Copyright (C) 2011-2017 Free Software Foundation, Inc.
5 This file is part of GDB.
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.
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.
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/>. */
25 #include "filestuff.h"
30 #define MAP_FAILED ((void *) -1)
34 #include "gdb/fileio.h"
40 /* An object of this type is stored in the section's user data when
43 struct gdb_bfd_section_data
45 /* Size of the data. */
47 /* If the data was mmapped, this is the length of the map. */
48 bfd_size_type map_len
;
49 /* The data. If NULL, the section data has not been read. */
51 /* If the data was mmapped, this is the map address. */
55 /* A hash table holding every BFD that gdb knows about. This is not
56 to be confused with 'gdb_bfd_cache', which is used for sharing
57 BFDs; in contrast, this hash is used just to implement
60 static htab_t all_bfds
;
62 /* An object of this type is stored in each BFD's user data. */
66 gdb_bfd_data (bfd
*abfd
)
67 : mtime (bfd_get_mtime (abfd
)),
68 size (bfd_get_size (abfd
)),
69 relocation_computed (0),
70 needs_relocations (0),
75 if (bfd_stat (abfd
, &buf
) == 0)
78 device_id
= buf
.st_dev
;
82 /* The stat failed. */
94 VEC_iterate (bfdp
, included_bfds
, ix
, included_bfd
);
96 gdb_bfd_unref (included_bfd
);
97 VEC_free (bfdp
, included_bfds
);
100 /* The reference count. */
103 /* The mtime of the BFD at the point the cache entry was made. */
106 /* The file size (in bytes) at the point the cache entry was made. */
109 /* The inode of the file at the point the cache entry was made. */
112 /* The device id of the file at the point the cache entry was made. */
115 /* This is true if we have determined whether this BFD has any
116 sections requiring relocation. */
117 unsigned int relocation_computed
: 1;
119 /* This is true if any section needs relocation. */
120 unsigned int needs_relocations
: 1;
122 /* This is true if we have successfully computed the file's CRC. */
123 unsigned int crc_computed
: 1;
125 /* The file's CRC. */
126 unsigned long crc
= 0;
128 /* If the BFD comes from an archive, this points to the archive's
129 BFD. Otherwise, this is NULL. */
130 bfd
*archive_bfd
= nullptr;
132 /* Table of all the bfds this bfd has included. */
133 VEC (bfdp
) *included_bfds
= nullptr;
136 REGISTRY_FIELDS
= {};
139 #define GDB_BFD_DATA_ACCESSOR(ABFD) \
140 ((struct gdb_bfd_data *) bfd_usrdata (ABFD))
142 DEFINE_REGISTRY (bfd
, GDB_BFD_DATA_ACCESSOR
)
144 /* A hash table storing all the BFDs maintained in the cache. */
146 static htab_t gdb_bfd_cache
;
148 /* When true gdb will reuse an existing bfd object if the filename,
149 modification time, and file size all match. */
151 static int bfd_sharing
= 1;
153 show_bfd_sharing (struct ui_file
*file
, int from_tty
,
154 struct cmd_list_element
*c
, const char *value
)
156 fprintf_filtered (file
, _("BFD sharing is %s.\n"), value
);
159 /* When non-zero debugging of the bfd caches is enabled. */
161 static unsigned int debug_bfd_cache
;
163 show_bfd_cache_debug (struct ui_file
*file
, int from_tty
,
164 struct cmd_list_element
*c
, const char *value
)
166 fprintf_filtered (file
, _("BFD cache debugging is %s.\n"), value
);
169 /* The type of an object being looked up in gdb_bfd_cache. We use
170 htab's capability of storing one kind of object (BFD in this case)
171 and using a different sort of object for searching. */
173 struct gdb_bfd_cache_search
176 const char *filename
;
179 /* The file size (in bytes). */
181 /* The inode of the file. */
183 /* The device id of the file. */
187 /* A hash function for BFDs. */
190 hash_bfd (const void *b
)
192 const bfd
*abfd
= (const struct bfd
*) b
;
194 /* It is simplest to just hash the filename. */
195 return htab_hash_string (bfd_get_filename (abfd
));
198 /* An equality function for BFDs. Note that this expects the caller
199 to search using struct gdb_bfd_cache_search only, not BFDs. */
202 eq_bfd (const void *a
, const void *b
)
204 const bfd
*abfd
= (const struct bfd
*) a
;
205 const struct gdb_bfd_cache_search
*s
206 = (const struct gdb_bfd_cache_search
*) b
;
207 struct gdb_bfd_data
*gdata
= (struct gdb_bfd_data
*) bfd_usrdata (abfd
);
209 return (gdata
->mtime
== s
->mtime
210 && gdata
->size
== s
->size
211 && gdata
->inode
== s
->inode
212 && gdata
->device_id
== s
->device_id
213 && strcmp (bfd_get_filename (abfd
), s
->filename
) == 0);
219 is_target_filename (const char *name
)
221 return startswith (name
, TARGET_SYSROOT_PREFIX
);
227 gdb_bfd_has_target_filename (struct bfd
*abfd
)
229 return is_target_filename (bfd_get_filename (abfd
));
233 /* Return the system error number corresponding to ERRNUM. */
236 fileio_errno_to_host (int errnum
)
280 case FILEIO_ENAMETOOLONG
:
286 /* Wrapper for target_fileio_open suitable for passing as the
287 OPEN_FUNC argument to gdb_bfd_openr_iovec. The supplied
288 OPEN_CLOSURE is unused. */
291 gdb_bfd_iovec_fileio_open (struct bfd
*abfd
, void *inferior
)
293 const char *filename
= bfd_get_filename (abfd
);
294 int fd
, target_errno
;
297 gdb_assert (is_target_filename (filename
));
299 fd
= target_fileio_open_warn_if_slow ((struct inferior
*) inferior
,
301 + strlen (TARGET_SYSROOT_PREFIX
),
306 errno
= fileio_errno_to_host (target_errno
);
307 bfd_set_error (bfd_error_system_call
);
311 stream
= XCNEW (int);
316 /* Wrapper for target_fileio_pread suitable for passing as the
317 PREAD_FUNC argument to gdb_bfd_openr_iovec. */
320 gdb_bfd_iovec_fileio_pread (struct bfd
*abfd
, void *stream
, void *buf
,
321 file_ptr nbytes
, file_ptr offset
)
323 int fd
= *(int *) stream
;
332 bytes
= target_fileio_pread (fd
, (gdb_byte
*) buf
+ pos
,
333 nbytes
- pos
, offset
+ pos
,
336 /* Success, but no bytes, means end-of-file. */
340 errno
= fileio_errno_to_host (target_errno
);
341 bfd_set_error (bfd_error_system_call
);
351 /* Wrapper for target_fileio_close suitable for passing as the
352 CLOSE_FUNC argument to gdb_bfd_openr_iovec. */
355 gdb_bfd_iovec_fileio_close (struct bfd
*abfd
, void *stream
)
357 int fd
= *(int *) stream
;
362 /* Ignore errors on close. These may happen with remote
363 targets if the connection has already been torn down. */
364 target_fileio_close (fd
, &target_errno
);
366 /* Zero means success. */
370 /* Wrapper for target_fileio_fstat suitable for passing as the
371 STAT_FUNC argument to gdb_bfd_openr_iovec. */
374 gdb_bfd_iovec_fileio_fstat (struct bfd
*abfd
, void *stream
,
377 int fd
= *(int *) stream
;
381 result
= target_fileio_fstat (fd
, sb
, &target_errno
);
384 errno
= fileio_errno_to_host (target_errno
);
385 bfd_set_error (bfd_error_system_call
);
394 gdb_bfd_open (const char *name
, const char *target
, int fd
)
399 struct gdb_bfd_cache_search search
;
402 if (is_target_filename (name
))
404 if (!target_filesystem_is_local ())
406 gdb_assert (fd
== -1);
408 return gdb_bfd_openr_iovec (name
, target
,
409 gdb_bfd_iovec_fileio_open
,
411 gdb_bfd_iovec_fileio_pread
,
412 gdb_bfd_iovec_fileio_close
,
413 gdb_bfd_iovec_fileio_fstat
);
416 name
+= strlen (TARGET_SYSROOT_PREFIX
);
419 if (gdb_bfd_cache
== NULL
)
420 gdb_bfd_cache
= htab_create_alloc (1, hash_bfd
, eq_bfd
, NULL
,
425 fd
= gdb_open_cloexec (name
, O_RDONLY
| O_BINARY
, 0);
428 bfd_set_error (bfd_error_system_call
);
433 search
.filename
= name
;
434 if (fstat (fd
, &st
) < 0)
436 /* Weird situation here. */
440 search
.device_id
= 0;
444 search
.mtime
= st
.st_mtime
;
445 search
.size
= st
.st_size
;
446 search
.inode
= st
.st_ino
;
447 search
.device_id
= st
.st_dev
;
450 /* Note that this must compute the same result as hash_bfd. */
451 hash
= htab_hash_string (name
);
452 /* Note that we cannot use htab_find_slot_with_hash here, because
453 opening the BFD may fail; and this would violate hashtab
455 abfd
= (struct bfd
*) htab_find_with_hash (gdb_bfd_cache
, &search
, hash
);
456 if (bfd_sharing
&& abfd
!= NULL
)
459 fprintf_unfiltered (gdb_stdlog
,
460 "Reusing cached bfd %s for %s\n",
461 host_address_to_string (abfd
),
462 bfd_get_filename (abfd
));
464 return new_bfd_ref (abfd
);
467 abfd
= bfd_fopen (name
, target
, FOPEN_RB
, fd
);
472 fprintf_unfiltered (gdb_stdlog
,
473 "Creating new bfd %s for %s\n",
474 host_address_to_string (abfd
),
475 bfd_get_filename (abfd
));
479 slot
= htab_find_slot_with_hash (gdb_bfd_cache
, &search
, hash
, INSERT
);
484 return new_bfd_ref (abfd
);
487 /* A helper function that releases any section data attached to the
491 free_one_bfd_section (bfd
*abfd
, asection
*sectp
, void *ignore
)
493 struct gdb_bfd_section_data
*sect
494 = (struct gdb_bfd_section_data
*) bfd_get_section_userdata (abfd
, sectp
);
496 if (sect
!= NULL
&& sect
->data
!= NULL
)
499 if (sect
->map_addr
!= NULL
)
503 res
= munmap (sect
->map_addr
, sect
->map_len
);
504 gdb_assert (res
== 0);
512 /* Close ABFD, and warn if that fails. */
515 gdb_bfd_close_or_warn (struct bfd
*abfd
)
518 char *name
= bfd_get_filename (abfd
);
520 bfd_map_over_sections (abfd
, free_one_bfd_section
, NULL
);
522 ret
= bfd_close (abfd
);
525 warning (_("cannot close \"%s\": %s"),
526 name
, bfd_errmsg (bfd_get_error ()));
534 gdb_bfd_ref (struct bfd
*abfd
)
536 struct gdb_bfd_data
*gdata
;
542 gdata
= (struct gdb_bfd_data
*) bfd_usrdata (abfd
);
545 fprintf_unfiltered (gdb_stdlog
,
546 "Increase reference count on bfd %s (%s)\n",
547 host_address_to_string (abfd
),
548 bfd_get_filename (abfd
));
556 /* Ask BFD to decompress sections in bfd_get_full_section_contents. */
557 abfd
->flags
|= BFD_DECOMPRESS
;
559 gdata
= new gdb_bfd_data (abfd
);
560 bfd_usrdata (abfd
) = gdata
;
561 bfd_alloc_data (abfd
);
563 /* This is the first we've seen it, so add it to the hash table. */
564 slot
= htab_find_slot (all_bfds
, abfd
, INSERT
);
565 gdb_assert (slot
&& !*slot
);
572 gdb_bfd_unref (struct bfd
*abfd
)
574 struct gdb_bfd_data
*gdata
;
575 struct gdb_bfd_cache_search search
;
581 gdata
= (struct gdb_bfd_data
*) bfd_usrdata (abfd
);
582 gdb_assert (gdata
->refc
>= 1);
588 fprintf_unfiltered (gdb_stdlog
,
589 "Decrease reference count on bfd %s (%s)\n",
590 host_address_to_string (abfd
),
591 bfd_get_filename (abfd
));
596 fprintf_unfiltered (gdb_stdlog
,
597 "Delete final reference count on bfd %s (%s)\n",
598 host_address_to_string (abfd
),
599 bfd_get_filename (abfd
));
601 archive_bfd
= gdata
->archive_bfd
;
602 search
.filename
= bfd_get_filename (abfd
);
604 if (gdb_bfd_cache
&& search
.filename
)
606 hashval_t hash
= htab_hash_string (search
.filename
);
609 search
.mtime
= gdata
->mtime
;
610 search
.size
= gdata
->size
;
611 search
.inode
= gdata
->inode
;
612 search
.device_id
= gdata
->device_id
;
613 slot
= htab_find_slot_with_hash (gdb_bfd_cache
, &search
, hash
,
617 htab_clear_slot (gdb_bfd_cache
, slot
);
620 bfd_free_data (abfd
);
622 bfd_usrdata (abfd
) = NULL
; /* Paranoia. */
624 htab_remove_elt (all_bfds
, abfd
);
626 gdb_bfd_close_or_warn (abfd
);
628 gdb_bfd_unref (archive_bfd
);
631 /* A helper function that returns the section data descriptor
632 associated with SECTION. If no such descriptor exists, a new one
633 is allocated and cleared. */
635 static struct gdb_bfd_section_data
*
636 get_section_descriptor (asection
*section
)
638 struct gdb_bfd_section_data
*result
;
640 result
= ((struct gdb_bfd_section_data
*)
641 bfd_get_section_userdata (section
->owner
, section
));
645 result
= ((struct gdb_bfd_section_data
*)
646 bfd_zalloc (section
->owner
, sizeof (*result
)));
647 bfd_set_section_userdata (section
->owner
, section
, result
);
656 gdb_bfd_map_section (asection
*sectp
, bfd_size_type
*size
)
659 struct gdb_bfd_section_data
*descriptor
;
662 gdb_assert ((sectp
->flags
& SEC_RELOC
) == 0);
663 gdb_assert (size
!= NULL
);
667 descriptor
= get_section_descriptor (sectp
);
669 /* If the data was already read for this BFD, just reuse it. */
670 if (descriptor
->data
!= NULL
)
674 if (!bfd_is_section_compressed (abfd
, sectp
))
676 /* The page size, used when mmapping. */
680 pagesize
= getpagesize ();
682 /* Only try to mmap sections which are large enough: we don't want
683 to waste space due to fragmentation. */
685 if (bfd_get_section_size (sectp
) > 4 * pagesize
)
687 descriptor
->size
= bfd_get_section_size (sectp
);
688 descriptor
->data
= bfd_mmap (abfd
, 0, descriptor
->size
, PROT_READ
,
689 MAP_PRIVATE
, sectp
->filepos
,
690 &descriptor
->map_addr
,
691 &descriptor
->map_len
);
693 if ((caddr_t
)descriptor
->data
!= MAP_FAILED
)
695 #if HAVE_POSIX_MADVISE
696 posix_madvise (descriptor
->map_addr
, descriptor
->map_len
,
697 POSIX_MADV_WILLNEED
);
702 /* On failure, clear out the section data and try again. */
703 memset (descriptor
, 0, sizeof (*descriptor
));
706 #endif /* HAVE_MMAP */
708 /* Handle compressed sections, or ordinary uncompressed sections in
711 descriptor
->size
= bfd_get_section_size (sectp
);
712 descriptor
->data
= NULL
;
715 if (!bfd_get_full_section_contents (abfd
, sectp
, &data
))
716 error (_("Can't read data for section '%s' in file '%s'"),
717 bfd_get_section_name (abfd
, sectp
),
718 bfd_get_filename (abfd
));
719 descriptor
->data
= data
;
722 gdb_assert (descriptor
->data
!= NULL
);
723 *size
= descriptor
->size
;
724 return (const gdb_byte
*) descriptor
->data
;
727 /* Return 32-bit CRC for ABFD. If successful store it to *FILE_CRC_RETURN and
728 return 1. Otherwise print a warning and return 0. ABFD seek position is
732 get_file_crc (bfd
*abfd
, unsigned long *file_crc_return
)
734 unsigned long file_crc
= 0;
736 if (bfd_seek (abfd
, 0, SEEK_SET
) != 0)
738 warning (_("Problem reading \"%s\" for CRC: %s"),
739 bfd_get_filename (abfd
), bfd_errmsg (bfd_get_error ()));
745 gdb_byte buffer
[8 * 1024];
748 count
= bfd_bread (buffer
, sizeof (buffer
), abfd
);
749 if (count
== (bfd_size_type
) -1)
751 warning (_("Problem reading \"%s\" for CRC: %s"),
752 bfd_get_filename (abfd
), bfd_errmsg (bfd_get_error ()));
757 file_crc
= bfd_calc_gnu_debuglink_crc32 (file_crc
, buffer
, count
);
760 *file_crc_return
= file_crc
;
767 gdb_bfd_crc (struct bfd
*abfd
, unsigned long *crc_out
)
769 struct gdb_bfd_data
*gdata
= (struct gdb_bfd_data
*) bfd_usrdata (abfd
);
771 if (!gdata
->crc_computed
)
772 gdata
->crc_computed
= get_file_crc (abfd
, &gdata
->crc
);
774 if (gdata
->crc_computed
)
775 *crc_out
= gdata
->crc
;
776 return gdata
->crc_computed
;
784 gdb_bfd_fopen (const char *filename
, const char *target
, const char *mode
,
787 bfd
*result
= bfd_fopen (filename
, target
, mode
, fd
);
789 return new_bfd_ref (result
);
795 gdb_bfd_openr (const char *filename
, const char *target
)
797 bfd
*result
= bfd_openr (filename
, target
);
799 return new_bfd_ref (result
);
805 gdb_bfd_openw (const char *filename
, const char *target
)
807 bfd
*result
= bfd_openw (filename
, target
);
809 return new_bfd_ref (result
);
815 gdb_bfd_openr_iovec (const char *filename
, const char *target
,
816 void *(*open_func
) (struct bfd
*nbfd
,
819 file_ptr (*pread_func
) (struct bfd
*nbfd
,
824 int (*close_func
) (struct bfd
*nbfd
,
826 int (*stat_func
) (struct bfd
*abfd
,
830 bfd
*result
= bfd_openr_iovec (filename
, target
,
831 open_func
, open_closure
,
832 pread_func
, close_func
, stat_func
);
834 return new_bfd_ref (result
);
840 gdb_bfd_mark_parent (bfd
*child
, bfd
*parent
)
842 struct gdb_bfd_data
*gdata
;
845 /* No need to stash the filename here, because we also keep a
846 reference on the parent archive. */
848 gdata
= (struct gdb_bfd_data
*) bfd_usrdata (child
);
849 if (gdata
->archive_bfd
== NULL
)
851 gdata
->archive_bfd
= parent
;
852 gdb_bfd_ref (parent
);
855 gdb_assert (gdata
->archive_bfd
== parent
);
861 gdb_bfd_openr_next_archived_file (bfd
*archive
, bfd
*previous
)
863 bfd
*result
= bfd_openr_next_archived_file (archive
, previous
);
866 gdb_bfd_mark_parent (result
, archive
);
868 return gdb_bfd_ref_ptr (result
);
874 gdb_bfd_record_inclusion (bfd
*includer
, bfd
*includee
)
876 struct gdb_bfd_data
*gdata
;
878 gdb_bfd_ref (includee
);
879 gdata
= (struct gdb_bfd_data
*) bfd_usrdata (includer
);
880 VEC_safe_push (bfdp
, gdata
->included_bfds
, includee
);
886 gdb_bfd_fdopenr (const char *filename
, const char *target
, int fd
)
888 bfd
*result
= bfd_fdopenr (filename
, target
, fd
);
890 return new_bfd_ref (result
);
895 gdb_static_assert (ARRAY_SIZE (_bfd_std_section
) == 4);
900 gdb_bfd_section_index (bfd
*abfd
, asection
*section
)
904 else if (section
== bfd_com_section_ptr
)
905 return bfd_count_sections (abfd
);
906 else if (section
== bfd_und_section_ptr
)
907 return bfd_count_sections (abfd
) + 1;
908 else if (section
== bfd_abs_section_ptr
)
909 return bfd_count_sections (abfd
) + 2;
910 else if (section
== bfd_ind_section_ptr
)
911 return bfd_count_sections (abfd
) + 3;
912 return section
->index
;
918 gdb_bfd_count_sections (bfd
*abfd
)
920 return bfd_count_sections (abfd
) + 4;
926 gdb_bfd_requires_relocations (bfd
*abfd
)
928 struct gdb_bfd_data
*gdata
= (struct gdb_bfd_data
*) bfd_usrdata (abfd
);
930 if (gdata
->relocation_computed
== 0)
934 for (sect
= abfd
->sections
; sect
!= NULL
; sect
= sect
->next
)
935 if ((sect
->flags
& SEC_RELOC
) != 0)
937 gdata
->needs_relocations
= 1;
941 gdata
->relocation_computed
= 1;
944 return gdata
->needs_relocations
;
949 /* A callback for htab_traverse that prints a single BFD. */
952 print_one_bfd (void **slot
, void *data
)
954 bfd
*abfd
= (struct bfd
*) *slot
;
955 struct gdb_bfd_data
*gdata
= (struct gdb_bfd_data
*) bfd_usrdata (abfd
);
956 struct ui_out
*uiout
= (struct ui_out
*) data
;
958 ui_out_emit_tuple
tuple_emitter (uiout
, NULL
);
959 uiout
->field_int ("refcount", gdata
->refc
);
960 uiout
->field_string ("addr", host_address_to_string (abfd
));
961 uiout
->field_string ("filename", bfd_get_filename (abfd
));
967 /* Implement the 'maint info bfd' command. */
970 maintenance_info_bfds (const char *arg
, int from_tty
)
972 struct ui_out
*uiout
= current_uiout
;
974 ui_out_emit_table
table_emitter (uiout
, 3, -1, "bfds");
975 uiout
->table_header (10, ui_left
, "refcount", "Refcount");
976 uiout
->table_header (18, ui_left
, "addr", "Address");
977 uiout
->table_header (40, ui_left
, "filename", "Filename");
979 uiout
->table_body ();
980 htab_traverse (all_bfds
, print_one_bfd
, uiout
);
984 _initialize_gdb_bfd (void)
986 all_bfds
= htab_create_alloc (10, htab_hash_pointer
, htab_eq_pointer
,
987 NULL
, xcalloc
, xfree
);
989 add_cmd ("bfds", class_maintenance
, maintenance_info_bfds
, _("\
990 List the BFDs that are currently open."),
991 &maintenanceinfolist
);
993 add_setshow_boolean_cmd ("bfd-sharing", no_class
,
995 Set whether gdb will share bfds that appear to be the same file."), _("\
996 Show whether gdb will share bfds that appear to be the same file."), _("\
997 When enabled gdb will reuse existing bfds rather than reopening the\n\
998 same file. To decide if two files are the same then gdb compares the\n\
999 filename, file size, file modification time, and file inode."),
1002 &maintenance_set_cmdlist
,
1003 &maintenance_show_cmdlist
);
1005 add_setshow_zuinteger_cmd ("bfd-cache", class_maintenance
,
1006 &debug_bfd_cache
, _("\
1007 Set bfd cache debugging."), _("\
1008 Show bfd cache debugging."), _("\
1009 When non-zero, bfd cache specific debugging is enabled."),
1011 &show_bfd_cache_debug
,
1012 &setdebuglist
, &showdebuglist
);