25#include "gdbsupport/filestuff.h"
29#define MAP_FAILED ((void *) -1)
33#include "gdbsupport/fileio.h"
36#include <unordered_map>
147#define bfd_cache_debug_printf(fmt, ...) \
148 debug_prefixed_printf_cond (debug_bfd_cache, "bfd-cache", fmt, ##__VA_ARGS__)
180 const bfd *abfd = (
const struct bfd *) b;
183 return htab_hash_string (bfd_get_filename (abfd));
192 const bfd *abfd = (
const struct bfd *) a;
201 && strcmp (bfd_get_filename (abfd), s->
filename) == 0);
231 core_addr_to_string_nz (
m_base)))
248 file_ptr
read (bfd *abfd,
void *buffer, file_ptr nbytes,
249 file_ptr offset)
override;
251 int stat (
struct bfd *abfd,
struct stat *sb)
override;
270 file_ptr nbytes, file_ptr offset)
273 if (offset + nbytes >
size ())
274 nbytes =
size () - offset;
293 memset (sb, 0,
sizeof (
struct stat));
294 sb->st_size =
size ();
304 std::unique_ptr<target_buffer> buffer
305 = gdb::make_unique<target_buffer> (addr,
size);
310 return buffer.release ();
327 file_ptr
read (bfd *abfd,
void *buffer, file_ptr nbytes,
328 file_ptr offset)
override;
330 int stat (
struct bfd *abfd,
struct stat *sb)
override;
347 const char *filename = bfd_get_filename (abfd);
349 fileio_error target_errno;
355 FILEIO_O_RDONLY, 0, warn_if_slow,
359 errno = fileio_error_to_host (target_errno);
360 bfd_set_error (bfd_error_system_call);
371 file_ptr nbytes, file_ptr offset)
373 fileio_error target_errno;
382 nbytes - pos, offset + pos,
389 errno = fileio_error_to_host (target_errno);
390 bfd_set_error (bfd_error_system_call);
406 warning (_(
"cannot close \"%s\": %s"),
name, reason);
413 fileio_error target_errno;
421 catch (
const gdb_exception &ex)
425 ex.message->c_str ());
434 fileio_error target_errno;
440 errno = fileio_error_to_host (target_errno);
441 bfd_set_error (bfd_error_system_call);
456 gdb_assert (bfd_usrdata (abfd) ==
nullptr);
459 abfd->flags |= BFD_DECOMPRESS;
462 bfd_set_usrdata (abfd, gdata);
465 slot = htab_find_slot (
all_bfds, abfd, INSERT);
466 gdb_assert (slot && !*slot);
486 gdb_assert (fd == -1);
506 fd = gdb_open_cloexec (
name, O_RDONLY |
O_BINARY, 0).release ();
509 bfd_set_error (bfd_error_system_call);
514 if (fstat (fd, &st) < 0)
518 abfd = bfd_fopen (
name, target, FOPEN_RB, fd);
521 return gdb_bfd_ref_ptr::new_reference (abfd);
525 search.
mtime = st.st_mtime;
526 search.
size = st.st_size;
527 search.
inode = st.st_ino;
531 hash = htab_hash_string (
name);
535 abfd = (
struct bfd *) htab_find_with_hash (
gdb_bfd_cache, &search, hash);
539 host_address_to_string (abfd),
540 bfd_get_filename (abfd));
542 return gdb_bfd_ref_ptr::new_reference (abfd);
545 abfd = bfd_fopen (
name, target, FOPEN_RB, fd);
549 bfd_set_cacheable (abfd, 1);
552 host_address_to_string (abfd),
553 bfd_get_filename (abfd));
557 slot = htab_find_slot_with_hash (
gdb_bfd_cache, &search, hash, INSERT);
581 if (sect != NULL && sect->
data != NULL)
589 gdb_assert (res == 0);
603 const char *
name = bfd_get_filename (abfd);
608 ret = bfd_close (abfd);
612 bfd_errmsg (bfd_get_error ()));
630 host_address_to_string (abfd),
631 bfd_get_filename (abfd));
657 gdb_assert (gdata->
refc >= 1);
663 host_address_to_string (abfd),
664 bfd_get_filename (abfd));
669 host_address_to_string (abfd),
670 bfd_get_filename (abfd));
673 search.
filename = bfd_get_filename (abfd);
677 hashval_t hash = htab_hash_string (search.
filename);
684 slot = htab_find_slot_with_hash (
gdb_bfd_cache, &search, hash,
692 bfd_set_usrdata (abfd, NULL);
715 bfd_zalloc (section->owner, sizeof (*result)));
716 bfd_set_section_userdata (section, result);
731 gdb_assert ((sectp->flags & SEC_RELOC) == 0);
732 gdb_assert (
size != NULL);
739 if (descriptor->
data != NULL)
743 if (!bfd_is_section_compressed (abfd, sectp))
749 pagesize = getpagesize ();
754 if (bfd_section_size (sectp) > 4 * pagesize)
756 descriptor->
size = bfd_section_size (sectp);
757 descriptor->
data = bfd_mmap (abfd, 0, descriptor->
size, PROT_READ,
758 MAP_PRIVATE, sectp->filepos,
762 if ((caddr_t)descriptor->
data != MAP_FAILED)
764#if HAVE_POSIX_MADVISE
766 POSIX_MADV_WILLNEED);
772 memset (descriptor, 0,
sizeof (*descriptor));
780 descriptor->
size = bfd_section_size (sectp);
781 descriptor->
data = NULL;
784 if (!bfd_get_full_section_contents (abfd, sectp, &
data))
786 warning (_(
"Can't read data for section '%s' in file '%s'"),
787 bfd_section_name (sectp),
788 bfd_get_filename (abfd));
797 gdb_assert (descriptor->
data != NULL);
799 return (
const gdb_byte *) descriptor->
data;
809 uint32_t file_crc = 0;
811 if (bfd_seek (abfd, 0,
SEEK_SET) != 0)
813 warning (_(
"Problem reading \"%s\" for CRC: %s"),
814 bfd_get_filename (abfd), bfd_errmsg (bfd_get_error ()));
820 gdb_byte buffer[8 * 1024];
823 count = bfd_read (buffer,
sizeof (buffer), abfd);
824 if (count == (bfd_size_type) -1)
826 warning (_(
"Problem reading \"%s\" for CRC: %s"),
827 bfd_get_filename (abfd), bfd_errmsg (bfd_get_error ()));
832 file_crc = bfd_calc_gnu_debuglink_crc32 (file_crc, buffer, count);
835 *file_crc_return = file_crc;
850 *crc_out = gdata->
crc;
862 bfd *result = bfd_fopen (filename, target, mode, fd);
864 if (result !=
nullptr)
865 bfd_set_cacheable (result, 1);
867 return gdb_bfd_ref_ptr::new_reference (result);
875 bfd *result = bfd_openr (filename, target);
877 return gdb_bfd_ref_ptr::new_reference (result);
885 bfd *result = bfd_openw (filename, target);
887 return gdb_bfd_ref_ptr::new_reference (result);
896 auto do_open = [] (bfd *nbfd,
void *closure) ->
void *
899 return (*real_opener) (nbfd);
902 auto read_trampoline = [] (bfd *nbfd,
void *stream,
void *buf,
903 file_ptr nbytes, file_ptr offset) -> file_ptr
906 return obj->
read (nbfd, buf, nbytes, offset);
909 auto stat_trampoline = [] (
struct bfd *abfd,
void *stream,
910 struct stat *sb) ->
int
913 return obj->
stat (abfd, sb);
916 auto close_trampoline = [] (
struct bfd *nbfd,
void *stream) ->
int
924 bfd *result = bfd_openr_iovec (filename, target,
926 read_trampoline, close_trampoline,
929 return gdb_bfd_ref_ptr::new_reference (result);
958 bfd *result = bfd_openr_next_archived_file (archive, previous);
974 gdata->
included_bfds.push_back (gdb_bfd_ref_ptr::new_reference (includee));
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;
1004 return bfd_count_sections (abfd) + 4;
1018 for (sect = abfd->sections; sect != NULL; sect = sect->next)
1019 if ((sect->flags & SEC_RELOC) != 0)
1035 gdb::byte_vector *contents)
1037 bfd_size_type section_size = bfd_section_size (section);
1039 contents->resize (section_size);
1041 return bfd_get_section_contents (abfd, section, contents->data (), 0,
1045#define AMBIGUOUS_MESS1 ".\nMatching formats:"
1046#define AMBIGUOUS_MESS2 \
1047 ".\nUse \"set gnutarget format-name\" to specify the format."
1057 if (error_tag != bfd_error_file_ambiguously_recognized || matching == NULL)
1058 return bfd_errmsg (error_tag);
1060 std::string ret (bfd_errmsg (error_tag));
1063 for (p = matching; *p; p++)
1080 bfd *abfd = (
struct bfd *) *slot;
1086 uiout->
field_string (
"addr", host_address_to_string (abfd));
1087 uiout->
field_string (
"filename", bfd_get_filename (abfd),
1130 if (data ==
nullptr)
1145 return ++map[std::move (str)];
1155gdb_bfd_error_handler (const
char *fmt, va_list ap)
1159 va_copy(ap_copy, ap);
1160 const std::string str = string_vprintf (fmt, ap_copy);
1170 (*default_bfd_error_handler) (fmt, ap);
1177 all_bfds = htab_create_alloc (10, htab_hash_pointer, htab_eq_pointer,
1181List the BFDs that are currently open."),
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."),
1198 _(
"Set bfd cache debugging."),
1199 _(
"Show bfd cache debugging."),
1201When non-zero, bfd cache specific debugging is enabled."),
void * xcalloc(size_t number, size_t size)
ui_file_style style() const
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 field_string(const char *fldname, const char *string, const ui_file_style &style=ui_file_style())
void field_signed(const char *fldname, LONGEST value)
void text(const char *string)
void table_header(int width, ui_align align, const std::string &col_name, const std::string &col_hdr)
struct cmd_list_element * showdebuglist
struct cmd_list_element * maintenanceinfolist
struct cmd_list_element * setdebuglist
struct cmd_list_element * maintenance_show_cmdlist
struct cmd_list_element * maintenance_set_cmdlist
struct cmd_list_element * add_cmd(const char *name, enum command_class theclass, const char *doc, struct cmd_list_element **list)
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)
cli_style_option file_name_style
static void show_bfd_cache_debug(struct ui_file *file, int from_tty, struct cmd_list_element *c, const char *value)
void gdb_bfd_unref(struct bfd *abfd)
static void maintenance_info_bfds(const char *arg, int from_tty)
static int get_file_crc(bfd *abfd, unsigned long *file_crc_return)
gdb_bfd_ref_ptr gdb_bfd_fopen(const char *filename, const char *target, const char *mode, int fd)
static unsigned long increment_bfd_error_count(std::string str)
static struct bfd_inferior_data * get_bfd_inferior_data(struct inferior *inf)
gdb_bfd_ref_ptr gdb_bfd_openr_iovec(const char *filename, const char *target, gdb_iovec_opener_ftype open_fn)
static void ATTRIBUTE_PRINTF(1, 0)
gdb_static_assert(ARRAY_SIZE(_bfd_std_section)==4)
static int gdb_bfd_close_or_warn(struct bfd *abfd)
int gdb_bfd_crc(struct bfd *abfd, unsigned long *crc_out)
gdb_bfd_ref_ptr gdb_bfd_openr(const char *filename, const char *target)
static int print_one_bfd(void **slot, void *data)
static struct gdb_bfd_section_data * get_section_descriptor(asection *section)
static htab_t gdb_bfd_cache
static bfd_error_handler_type default_bfd_error_handler
static void show_bfd_sharing(struct ui_file *file, int from_tty, struct cmd_list_element *c, const char *value)
void _initialize_gdb_bfd()
int gdb_bfd_section_index(bfd *abfd, asection *section)
std::string gdb_bfd_errmsg(bfd_error_type error_tag, char **matching)
int gdb_bfd_requires_relocations(bfd *abfd)
gdb_bfd_ref_ptr gdb_bfd_openw(const char *filename, const char *target)
int gdb_bfd_has_target_filename(struct bfd *abfd)
static void gdb_bfd_close_warning(const char *name, const char *reason)
static int eq_bfd(const void *a, const void *b)
int gdb_bfd_count_sections(bfd *abfd)
void gdb_bfd_mark_parent(bfd *child, bfd *parent)
gdb_bfd_ref_ptr gdb_bfd_openr_next_archived_file(bfd *archive, bfd *previous)
static hashval_t hash_bfd(const void *b)
void gdb_bfd_record_inclusion(bfd *includer, bfd *includee)
static bool debug_bfd_cache
void gdb_bfd_ref(struct bfd *abfd)
static void free_one_bfd_section(asection *sectp)
int is_target_filename(const char *name)
#define bfd_cache_debug_printf(fmt,...)
static void gdb_bfd_init_data(struct bfd *abfd, struct stat *st)
gdb_bfd_ref_ptr gdb_bfd_open(const char *name, const char *target, int fd, bool warn_if_slow)
gdb_bfd_ref_ptr gdb_bfd_open_from_target_memory(CORE_ADDR addr, ULONGEST size, const char *target)
const gdb_byte * gdb_bfd_map_section(asection *sectp, bfd_size_type *size)
static const registry< inferior >::key< bfd_inferior_data > bfd_inferior_data_key
static target_fileio_stream * gdb_bfd_iovec_fileio_open(struct bfd *abfd, inferior *inf, bool warn_if_slow)
bool gdb_bfd_get_full_section_contents(bfd *abfd, asection *section, gdb::byte_vector *contents)
#define TARGET_SYSROOT_PREFIX
gdb::ref_ptr< struct bfd, gdb_bfd_ref_policy > gdb_bfd_ref_ptr
gdb::function_view< gdb_bfd_iovec_base *(bfd *)> gdb_iovec_opener_ftype
static gdb_bfd_section_range gdb_bfd_sections(bfd *abfd)
mach_port_t mach_port_t name mach_port_t mach_port_t name kern_return_t err
struct inferior * current_inferior(void)
std::unordered_map< std::string, unsigned long > bfd_error_string_counts
unsigned int needs_relocations
std::vector< gdb_bfd_ref_ptr > included_bfds
registry< bfd > registry_fields
gdb_bfd_data(bfd *abfd, struct stat *st)
unsigned int relocation_computed
unsigned int crc_computed
static registry< T > * get(T *obj)
const char * filename() const
gdb::unique_xmalloc_ptr< char > m_filename
target_buffer(CORE_ADDR base, ULONGEST size)
int stat(struct bfd *abfd, struct stat *sb) override
file_ptr read(bfd *abfd, void *buffer, file_ptr nbytes, file_ptr offset) override
target_fileio_stream(bfd *nbfd, int fd)
file_ptr read(bfd *abfd, void *buffer, file_ptr nbytes, file_ptr offset) override
int stat(struct bfd *abfd, struct stat *sb) override
int target_fileio_fstat(int fd, struct stat *sb, fileio_error *target_errno)
bool target_filesystem_is_local()
int target_fileio_open(struct inferior *inf, const char *filename, int flags, int mode, bool warn_if_slow, fileio_error *target_errno)
int target_fileio_close(int fd, fileio_error *target_errno)
int target_fileio_pread(int fd, gdb_byte *read_buf, int len, ULONGEST offset, fileio_error *target_errno)
int target_read_memory(CORE_ADDR memaddr, gdb_byte *myaddr, ssize_t len)
void gdb_printf(struct ui_file *stream, const char *format,...)