1 /* Definitions for BFD wrappers used by GDB.
3 Copyright (C) 2011-2014 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/>. */
22 #include "gdb_assert.h"
27 #include "filestuff.h"
35 #define MAP_FAILED ((void *) -1)
42 /* An object of this type is stored in the section's user data when
45 struct gdb_bfd_section_data
47 /* Size of the data. */
49 /* If the data was mmapped, this is the length of the map. */
50 bfd_size_type map_len
;
51 /* The data. If NULL, the section data has not been read. */
53 /* If the data was mmapped, this is the map address. */
57 /* A hash table holding every BFD that gdb knows about. This is not
58 to be confused with 'gdb_bfd_cache', which is used for sharing
59 BFDs; in contrast, this hash is used just to implement
62 static htab_t all_bfds
;
64 /* An object of this type is stored in each BFD's user data. */
68 /* The reference count. */
71 /* The mtime of the BFD at the point the cache entry was made. */
74 /* This is true if we have determined whether this BFD has any
75 sections requiring relocation. */
76 unsigned int relocation_computed
: 1;
78 /* This is true if any section needs relocation. */
79 unsigned int needs_relocations
: 1;
81 /* This is true if we have successfully computed the file's CRC. */
82 unsigned int crc_computed
: 1;
87 /* If the BFD comes from an archive, this points to the archive's
88 BFD. Otherwise, this is NULL. */
91 /* Table of all the bfds this bfd has included. */
92 VEC (bfdp
) *included_bfds
;
98 #define GDB_BFD_DATA_ACCESSOR(ABFD) \
99 ((struct gdb_bfd_data *) bfd_usrdata (ABFD))
101 DEFINE_REGISTRY (bfd
, GDB_BFD_DATA_ACCESSOR
)
103 /* A hash table storing all the BFDs maintained in the cache. */
105 static htab_t gdb_bfd_cache
;
107 /* The type of an object being looked up in gdb_bfd_cache. We use
108 htab's capability of storing one kind of object (BFD in this case)
109 and using a different sort of object for searching. */
111 struct gdb_bfd_cache_search
114 const char *filename
;
119 /* A hash function for BFDs. */
122 hash_bfd (const void *b
)
126 /* It is simplest to just hash the filename. */
127 return htab_hash_string (bfd_get_filename (abfd
));
130 /* An equality function for BFDs. Note that this expects the caller
131 to search using struct gdb_bfd_cache_search only, not BFDs. */
134 eq_bfd (const void *a
, const void *b
)
137 const struct gdb_bfd_cache_search
*s
= b
;
138 struct gdb_bfd_data
*gdata
= bfd_usrdata (abfd
);
140 return (gdata
->mtime
== s
->mtime
141 && strcmp (bfd_get_filename (abfd
), s
->filename
) == 0);
147 gdb_bfd_open (const char *name
, const char *target
, int fd
)
152 struct gdb_bfd_cache_search search
;
155 if (gdb_bfd_cache
== NULL
)
156 gdb_bfd_cache
= htab_create_alloc (1, hash_bfd
, eq_bfd
, NULL
,
161 fd
= gdb_open_cloexec (name
, O_RDONLY
| O_BINARY
, 0);
164 bfd_set_error (bfd_error_system_call
);
169 search
.filename
= name
;
170 if (fstat (fd
, &st
) < 0)
172 /* Weird situation here. */
176 search
.mtime
= st
.st_mtime
;
178 /* Note that this must compute the same result as hash_bfd. */
179 hash
= htab_hash_string (name
);
180 /* Note that we cannot use htab_find_slot_with_hash here, because
181 opening the BFD may fail; and this would violate hashtab
183 abfd
= htab_find_with_hash (gdb_bfd_cache
, &search
, hash
);
191 abfd
= bfd_fopen (name
, target
, FOPEN_RB
, fd
);
195 slot
= htab_find_slot_with_hash (gdb_bfd_cache
, &search
, hash
, INSERT
);
203 /* A helper function that releases any section data attached to the
207 free_one_bfd_section (bfd
*abfd
, asection
*sectp
, void *ignore
)
209 struct gdb_bfd_section_data
*sect
= bfd_get_section_userdata (abfd
, sectp
);
211 if (sect
!= NULL
&& sect
->data
!= NULL
)
214 if (sect
->map_addr
!= NULL
)
218 res
= munmap (sect
->map_addr
, sect
->map_len
);
219 gdb_assert (res
== 0);
227 /* Close ABFD, and warn if that fails. */
230 gdb_bfd_close_or_warn (struct bfd
*abfd
)
233 char *name
= bfd_get_filename (abfd
);
235 bfd_map_over_sections (abfd
, free_one_bfd_section
, NULL
);
237 ret
= bfd_close (abfd
);
240 warning (_("cannot close \"%s\": %s"),
241 name
, bfd_errmsg (bfd_get_error ()));
249 gdb_bfd_ref (struct bfd
*abfd
)
251 struct gdb_bfd_data
*gdata
;
257 gdata
= bfd_usrdata (abfd
);
265 /* Ask BFD to decompress sections in bfd_get_full_section_contents. */
266 abfd
->flags
|= BFD_DECOMPRESS
;
268 gdata
= bfd_zalloc (abfd
, sizeof (struct gdb_bfd_data
));
270 gdata
->mtime
= bfd_get_mtime (abfd
);
271 gdata
->archive_bfd
= NULL
;
272 bfd_usrdata (abfd
) = gdata
;
274 bfd_alloc_data (abfd
);
276 /* This is the first we've seen it, so add it to the hash table. */
277 slot
= htab_find_slot (all_bfds
, abfd
, INSERT
);
278 gdb_assert (slot
&& !*slot
);
285 gdb_bfd_unref (struct bfd
*abfd
)
288 struct gdb_bfd_data
*gdata
;
289 struct gdb_bfd_cache_search search
;
290 bfd
*archive_bfd
, *included_bfd
;
295 gdata
= bfd_usrdata (abfd
);
296 gdb_assert (gdata
->refc
>= 1);
302 archive_bfd
= gdata
->archive_bfd
;
303 search
.filename
= bfd_get_filename (abfd
);
305 if (gdb_bfd_cache
&& search
.filename
)
307 hashval_t hash
= htab_hash_string (search
.filename
);
310 search
.mtime
= gdata
->mtime
;
311 slot
= htab_find_slot_with_hash (gdb_bfd_cache
, &search
, hash
,
315 htab_clear_slot (gdb_bfd_cache
, slot
);
319 VEC_iterate (bfdp
, gdata
->included_bfds
, ix
, included_bfd
);
321 gdb_bfd_unref (included_bfd
);
322 VEC_free (bfdp
, gdata
->included_bfds
);
324 bfd_free_data (abfd
);
325 bfd_usrdata (abfd
) = NULL
; /* Paranoia. */
327 htab_remove_elt (all_bfds
, abfd
);
329 gdb_bfd_close_or_warn (abfd
);
331 gdb_bfd_unref (archive_bfd
);
334 /* A helper function that returns the section data descriptor
335 associated with SECTION. If no such descriptor exists, a new one
336 is allocated and cleared. */
338 static struct gdb_bfd_section_data
*
339 get_section_descriptor (asection
*section
)
341 struct gdb_bfd_section_data
*result
;
343 result
= bfd_get_section_userdata (section
->owner
, section
);
347 result
= bfd_zalloc (section
->owner
, sizeof (*result
));
348 bfd_set_section_userdata (section
->owner
, section
, result
);
357 gdb_bfd_map_section (asection
*sectp
, bfd_size_type
*size
)
360 struct gdb_bfd_section_data
*descriptor
;
363 gdb_assert ((sectp
->flags
& SEC_RELOC
) == 0);
364 gdb_assert (size
!= NULL
);
368 descriptor
= get_section_descriptor (sectp
);
370 /* If the data was already read for this BFD, just reuse it. */
371 if (descriptor
->data
!= NULL
)
375 if (!bfd_is_section_compressed (abfd
, sectp
))
377 /* The page size, used when mmapping. */
381 pagesize
= getpagesize ();
383 /* Only try to mmap sections which are large enough: we don't want
384 to waste space due to fragmentation. */
386 if (bfd_get_section_size (sectp
) > 4 * pagesize
)
388 descriptor
->size
= bfd_get_section_size (sectp
);
389 descriptor
->data
= bfd_mmap (abfd
, 0, descriptor
->size
, PROT_READ
,
390 MAP_PRIVATE
, sectp
->filepos
,
391 &descriptor
->map_addr
,
392 &descriptor
->map_len
);
394 if ((caddr_t
)descriptor
->data
!= MAP_FAILED
)
396 #if HAVE_POSIX_MADVISE
397 posix_madvise (descriptor
->map_addr
, descriptor
->map_len
,
398 POSIX_MADV_WILLNEED
);
403 /* On failure, clear out the section data and try again. */
404 memset (descriptor
, 0, sizeof (*descriptor
));
407 #endif /* HAVE_MMAP */
409 /* Handle compressed sections, or ordinary uncompressed sections in
412 descriptor
->size
= bfd_get_section_size (sectp
);
413 descriptor
->data
= NULL
;
416 if (!bfd_get_full_section_contents (abfd
, sectp
, &data
))
417 error (_("Can't read data for section '%s' in file '%s'"),
418 bfd_get_section_name (abfd
, sectp
),
419 bfd_get_filename (abfd
));
420 descriptor
->data
= data
;
423 gdb_assert (descriptor
->data
!= NULL
);
424 *size
= descriptor
->size
;
425 return descriptor
->data
;
428 /* Return 32-bit CRC for ABFD. If successful store it to *FILE_CRC_RETURN and
429 return 1. Otherwise print a warning and return 0. ABFD seek position is
433 get_file_crc (bfd
*abfd
, unsigned long *file_crc_return
)
435 unsigned long file_crc
= 0;
437 if (bfd_seek (abfd
, 0, SEEK_SET
) != 0)
439 warning (_("Problem reading \"%s\" for CRC: %s"),
440 bfd_get_filename (abfd
), bfd_errmsg (bfd_get_error ()));
446 gdb_byte buffer
[8 * 1024];
449 count
= bfd_bread (buffer
, sizeof (buffer
), abfd
);
450 if (count
== (bfd_size_type
) -1)
452 warning (_("Problem reading \"%s\" for CRC: %s"),
453 bfd_get_filename (abfd
), bfd_errmsg (bfd_get_error ()));
458 file_crc
= bfd_calc_gnu_debuglink_crc32 (file_crc
, buffer
, count
);
461 *file_crc_return
= file_crc
;
468 gdb_bfd_crc (struct bfd
*abfd
, unsigned long *crc_out
)
470 struct gdb_bfd_data
*gdata
= bfd_usrdata (abfd
);
472 if (!gdata
->crc_computed
)
473 gdata
->crc_computed
= get_file_crc (abfd
, &gdata
->crc
);
475 if (gdata
->crc_computed
)
476 *crc_out
= gdata
->crc
;
477 return gdata
->crc_computed
;
485 gdb_bfd_fopen (const char *filename
, const char *target
, const char *mode
,
488 bfd
*result
= bfd_fopen (filename
, target
, mode
, fd
);
491 gdb_bfd_ref (result
);
499 gdb_bfd_openr (const char *filename
, const char *target
)
501 bfd
*result
= bfd_openr (filename
, target
);
504 gdb_bfd_ref (result
);
512 gdb_bfd_openw (const char *filename
, const char *target
)
514 bfd
*result
= bfd_openw (filename
, target
);
517 gdb_bfd_ref (result
);
525 gdb_bfd_openr_iovec (const char *filename
, const char *target
,
526 void *(*open_func
) (struct bfd
*nbfd
,
529 file_ptr (*pread_func
) (struct bfd
*nbfd
,
534 int (*close_func
) (struct bfd
*nbfd
,
536 int (*stat_func
) (struct bfd
*abfd
,
540 bfd
*result
= bfd_openr_iovec (filename
, target
,
541 open_func
, open_closure
,
542 pread_func
, close_func
, stat_func
);
545 gdb_bfd_ref (result
);
553 gdb_bfd_mark_parent (bfd
*child
, bfd
*parent
)
555 struct gdb_bfd_data
*gdata
;
558 /* No need to stash the filename here, because we also keep a
559 reference on the parent archive. */
561 gdata
= bfd_usrdata (child
);
562 if (gdata
->archive_bfd
== NULL
)
564 gdata
->archive_bfd
= parent
;
565 gdb_bfd_ref (parent
);
568 gdb_assert (gdata
->archive_bfd
== parent
);
574 gdb_bfd_openr_next_archived_file (bfd
*archive
, bfd
*previous
)
576 bfd
*result
= bfd_openr_next_archived_file (archive
, previous
);
579 gdb_bfd_mark_parent (result
, archive
);
587 gdb_bfd_record_inclusion (bfd
*includer
, bfd
*includee
)
589 struct gdb_bfd_data
*gdata
;
591 gdb_bfd_ref (includee
);
592 gdata
= bfd_usrdata (includer
);
593 VEC_safe_push (bfdp
, gdata
->included_bfds
, includee
);
599 gdb_bfd_fdopenr (const char *filename
, const char *target
, int fd
)
601 bfd
*result
= bfd_fdopenr (filename
, target
, fd
);
604 gdb_bfd_ref (result
);
611 gdb_static_assert (ARRAY_SIZE (_bfd_std_section
) == 4);
616 gdb_bfd_section_index (bfd
*abfd
, asection
*section
)
620 else if (section
== bfd_com_section_ptr
)
621 return bfd_count_sections (abfd
) + 1;
622 else if (section
== bfd_und_section_ptr
)
623 return bfd_count_sections (abfd
) + 2;
624 else if (section
== bfd_abs_section_ptr
)
625 return bfd_count_sections (abfd
) + 3;
626 else if (section
== bfd_ind_section_ptr
)
627 return bfd_count_sections (abfd
) + 4;
628 return section
->index
;
634 gdb_bfd_count_sections (bfd
*abfd
)
636 return bfd_count_sections (abfd
) + 4;
642 gdb_bfd_requires_relocations (bfd
*abfd
)
644 struct gdb_bfd_data
*gdata
= bfd_usrdata (abfd
);
646 if (gdata
->relocation_computed
== 0)
650 for (sect
= abfd
->sections
; sect
!= NULL
; sect
= sect
->next
)
651 if ((sect
->flags
& SEC_RELOC
) != 0)
653 gdata
->needs_relocations
= 1;
657 gdata
->relocation_computed
= 1;
660 return gdata
->needs_relocations
;
665 /* A callback for htab_traverse that prints a single BFD. */
668 print_one_bfd (void **slot
, void *data
)
671 struct gdb_bfd_data
*gdata
= bfd_usrdata (abfd
);
672 struct ui_out
*uiout
= data
;
673 struct cleanup
*inner
;
675 inner
= make_cleanup_ui_out_tuple_begin_end (uiout
, NULL
);
676 ui_out_field_int (uiout
, "refcount", gdata
->refc
);
677 ui_out_field_string (uiout
, "addr", host_address_to_string (abfd
));
678 ui_out_field_string (uiout
, "filename", bfd_get_filename (abfd
));
679 ui_out_text (uiout
, "\n");
685 /* Implement the 'maint info bfd' command. */
688 maintenance_info_bfds (char *arg
, int from_tty
)
690 struct cleanup
*cleanup
;
691 struct ui_out
*uiout
= current_uiout
;
693 cleanup
= make_cleanup_ui_out_table_begin_end (uiout
, 3, -1, "bfds");
694 ui_out_table_header (uiout
, 10, ui_left
, "refcount", "Refcount");
695 ui_out_table_header (uiout
, 18, ui_left
, "addr", "Address");
696 ui_out_table_header (uiout
, 40, ui_left
, "filename", "Filename");
698 ui_out_table_body (uiout
);
699 htab_traverse (all_bfds
, print_one_bfd
, uiout
);
701 do_cleanups (cleanup
);
704 /* -Wmissing-prototypes */
705 extern initialize_file_ftype _initialize_gdb_bfd
;
708 _initialize_gdb_bfd (void)
710 all_bfds
= htab_create_alloc (10, htab_hash_pointer
, htab_eq_pointer
,
711 NULL
, xcalloc
, xfree
);
713 add_cmd ("bfds", class_maintenance
, maintenance_info_bfds
, _("\
714 List the BFDs that are currently open."),
715 &maintenanceinfolist
);