25#include "gdbsupport/filestuff.h"
29#define MAP_FAILED ((void *) -1)
33#include "gdbsupport/fileio.h"
36#include <unordered_map>
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),
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);
232 = xstrprintf (
"<in-memory@%s>", core_addr_to_string_nz (
m_base));
286 file_ptr nbytes, file_ptr offset)
291 if (offset + nbytes > buffer->
size ())
292 nbytes = buffer->
size () - offset;
314 memset (sb, 0,
sizeof (
struct stat));
315 sb->st_size = buffer->
size ();
348 const char *filename = bfd_get_filename (abfd);
350 fileio_error target_errno;
362 errno = fileio_error_to_host (target_errno);
363 bfd_set_error (bfd_error_system_call);
367 stream = XCNEW (
int);
377 file_ptr nbytes, file_ptr offset)
379 int fd = *(
int *) stream;
380 fileio_error target_errno;
389 nbytes - pos, offset + pos,
396 errno = fileio_error_to_host (target_errno);
397 bfd_set_error (bfd_error_system_call);
413 warning (_(
"cannot close \"%s\": %s"),
name, reason);
422 int fd = *(
int *) stream;
423 fileio_error target_errno;
433 catch (
const gdb_exception &ex)
437 ex.message->c_str ());
451 int fd = *(
int *) stream;
452 fileio_error target_errno;
458 errno = fileio_error_to_host (target_errno);
459 bfd_set_error (bfd_error_system_call);
474 gdb_assert (bfd_usrdata (abfd) ==
nullptr);
477 abfd->flags |= BFD_DECOMPRESS;
480 bfd_set_usrdata (abfd, gdata);
483 slot = htab_find_slot (
all_bfds, abfd, INSERT);
484 gdb_assert (slot && !*slot);
504 gdb_assert (fd == -1);
524 fd = gdb_open_cloexec (
name, O_RDONLY |
O_BINARY, 0).release ();
527 bfd_set_error (bfd_error_system_call);
532 if (fstat (fd, &st) < 0)
536 abfd = bfd_fopen (
name, target, FOPEN_RB, fd);
539 return gdb_bfd_ref_ptr::new_reference (abfd);
543 search.
mtime = st.st_mtime;
544 search.
size = st.st_size;
545 search.
inode = st.st_ino;
549 hash = htab_hash_string (
name);
553 abfd = (
struct bfd *) htab_find_with_hash (
gdb_bfd_cache, &search, hash);
557 host_address_to_string (abfd),
558 bfd_get_filename (abfd));
560 return gdb_bfd_ref_ptr::new_reference (abfd);
563 abfd = bfd_fopen (
name, target, FOPEN_RB, fd);
568 host_address_to_string (abfd),
569 bfd_get_filename (abfd));
573 slot = htab_find_slot_with_hash (
gdb_bfd_cache, &search, hash, INSERT);
597 if (sect != NULL && sect->
data != NULL)
605 gdb_assert (res == 0);
619 const char *
name = bfd_get_filename (abfd);
624 ret = bfd_close (abfd);
628 bfd_errmsg (bfd_get_error ()));
646 host_address_to_string (abfd),
647 bfd_get_filename (abfd));
673 gdb_assert (gdata->
refc >= 1);
679 host_address_to_string (abfd),
680 bfd_get_filename (abfd));
685 host_address_to_string (abfd),
686 bfd_get_filename (abfd));
689 search.
filename = bfd_get_filename (abfd);
693 hashval_t hash = htab_hash_string (search.
filename);
700 slot = htab_find_slot_with_hash (
gdb_bfd_cache, &search, hash,
708 bfd_set_usrdata (abfd, NULL);
731 bfd_zalloc (section->owner, sizeof (*result)));
732 bfd_set_section_userdata (section, result);
747 gdb_assert ((sectp->flags & SEC_RELOC) == 0);
748 gdb_assert (
size != NULL);
755 if (descriptor->
data != NULL)
759 if (!bfd_is_section_compressed (abfd, sectp))
765 pagesize = getpagesize ();
770 if (bfd_section_size (sectp) > 4 * pagesize)
772 descriptor->
size = bfd_section_size (sectp);
773 descriptor->
data = bfd_mmap (abfd, 0, descriptor->
size, PROT_READ,
774 MAP_PRIVATE, sectp->filepos,
778 if ((caddr_t)descriptor->
data != MAP_FAILED)
780#if HAVE_POSIX_MADVISE
782 POSIX_MADV_WILLNEED);
788 memset (descriptor, 0,
sizeof (*descriptor));
796 descriptor->
size = bfd_section_size (sectp);
797 descriptor->
data = NULL;
800 if (!bfd_get_full_section_contents (abfd, sectp, &
data))
802 warning (_(
"Can't read data for section '%s' in file '%s'"),
803 bfd_section_name (sectp),
804 bfd_get_filename (abfd));
813 gdb_assert (descriptor->
data != NULL);
815 return (
const gdb_byte *) descriptor->
data;
825 unsigned long file_crc = 0;
827 if (bfd_seek (abfd, 0,
SEEK_SET) != 0)
829 warning (_(
"Problem reading \"%s\" for CRC: %s"),
830 bfd_get_filename (abfd), bfd_errmsg (bfd_get_error ()));
836 gdb_byte buffer[8 * 1024];
839 count = bfd_bread (buffer,
sizeof (buffer), abfd);
840 if (count == (bfd_size_type) -1)
842 warning (_(
"Problem reading \"%s\" for CRC: %s"),
843 bfd_get_filename (abfd), bfd_errmsg (bfd_get_error ()));
848 file_crc = bfd_calc_gnu_debuglink_crc32 (file_crc, buffer, count);
851 *file_crc_return = file_crc;
866 *crc_out = gdata->
crc;
878 bfd *result = bfd_fopen (filename, target, mode, fd);
880 return gdb_bfd_ref_ptr::new_reference (result);
888 bfd *result = bfd_openr (filename, target);
890 return gdb_bfd_ref_ptr::new_reference (result);
898 bfd *result = bfd_openw (filename, target);
900 return gdb_bfd_ref_ptr::new_reference (result);
907 void *(*open_func) (
struct bfd *nbfd,
910 file_ptr (*pread_func) (
struct bfd *nbfd,
915 int (*close_func) (
struct bfd *nbfd,
917 int (*stat_func) (
struct bfd *abfd,
921 bfd *result = bfd_openr_iovec (filename, target,
922 open_func, open_closure,
923 pread_func, close_func, stat_func);
925 return gdb_bfd_ref_ptr::new_reference (result);
954 bfd *result = bfd_openr_next_archived_file (archive, previous);
970 gdata->
included_bfds.push_back (gdb_bfd_ref_ptr::new_reference (includee));
984 else if (section == bfd_com_section_ptr)
985 return bfd_count_sections (abfd);
986 else if (section == bfd_und_section_ptr)
987 return bfd_count_sections (abfd) + 1;
988 else if (section == bfd_abs_section_ptr)
989 return bfd_count_sections (abfd) + 2;
990 else if (section == bfd_ind_section_ptr)
991 return bfd_count_sections (abfd) + 3;
992 return section->index;
1000 return bfd_count_sections (abfd) + 4;
1014 for (sect = abfd->sections; sect != NULL; sect = sect->next)
1015 if ((sect->flags & SEC_RELOC) != 0)
1031 gdb::byte_vector *contents)
1033 bfd_size_type section_size = bfd_section_size (section);
1035 contents->resize (section_size);
1037 return bfd_get_section_contents (abfd, section, contents->data (), 0,
1041#define AMBIGUOUS_MESS1 ".\nMatching formats:"
1042#define AMBIGUOUS_MESS2 \
1043 ".\nUse \"set gnutarget format-name\" to specify the format."
1053 if (error_tag != bfd_error_file_ambiguously_recognized || matching == NULL)
1054 return bfd_errmsg (error_tag);
1056 std::string ret (bfd_errmsg (error_tag));
1059 for (p = matching; *p; p++)
1076 bfd *abfd = (
struct bfd *) *slot;
1082 uiout->
field_string (
"addr", host_address_to_string (abfd));
1083 uiout->
field_string (
"filename", bfd_get_filename (abfd),
1126 if (data ==
nullptr)
1141 return ++map[std::move (str)];
1151gdb_bfd_error_handler (const
char *fmt, va_list ap)
1155 va_copy(ap_copy, ap);
1156 const std::string str = string_vprintf (fmt, ap_copy);
1166 (*default_bfd_error_handler) (fmt, ap);
1173 all_bfds = htab_create_alloc (10, htab_hash_pointer, htab_eq_pointer,
1177List the BFDs that are currently open."),
1182Set whether gdb will share bfds that appear to be the same file."), _(
"\
1183Show whether gdb will share bfds that appear to be the same file."), _(
"\
1184When enabled gdb will reuse existing bfds rather than reopening the\n\
1185same file. To decide if two files are the same then gdb compares the\n\
1186filename, file size, file modification time, and file inode."),
1194 _(
"Set bfd cache debugging."),
1195 _(
"Show bfd cache debugging."),
1197When non-zero, bfd cache specific debugging is enabled."),
void * xcalloc(size_t number, size_t size)
ui_file_style style() const
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)
static file_ptr mem_bfd_iovec_pread(struct bfd *abfd, void *stream, void *buf, file_ptr nbytes, file_ptr offset)
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 * mem_bfd_iovec_open(struct bfd *abfd, void *open_closure)
static void * gdb_bfd_iovec_fileio_open(struct bfd *abfd, void *open_closure)
gdb_bfd_ref_ptr gdb_bfd_openr_iovec(const char *filename, const char *target, void *(*open_func)(struct bfd *nbfd, void *open_closure), void *open_closure, file_ptr(*pread_func)(struct bfd *nbfd, void *stream, void *buf, file_ptr nbytes, file_ptr offset), int(*close_func)(struct bfd *nbfd, void *stream), int(*stat_func)(struct bfd *abfd, void *stream, struct stat *sb))
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)
static int mem_bfd_iovec_stat(struct bfd *abfd, void *stream, struct stat *sb)
int gdb_bfd_has_target_filename(struct bfd *abfd)
static file_ptr gdb_bfd_iovec_fileio_pread(struct bfd *abfd, void *stream, void *buf, file_ptr nbytes, file_ptr offset)
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
static int gdb_bfd_iovec_fileio_close(struct bfd *abfd, void *stream)
void gdb_bfd_ref(struct bfd *abfd)
static int mem_bfd_iovec_close(struct bfd *abfd, void *stream)
static void free_one_bfd_section(asection *sectp)
int is_target_filename(const char *name)
#define bfd_cache_debug_printf(fmt,...)
static int gdb_bfd_iovec_fileio_fstat(struct bfd *abfd, void *stream, struct stat *sb)
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
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
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 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,...)