1 /* Definitions for BFD wrappers used by GDB.
3 Copyright (C) 2011-2013 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"
23 #include "gdb_string.h"
33 #define MAP_FAILED ((void *) -1)
37 /* An object of this type is stored in the section's user data when
40 struct gdb_bfd_section_data
42 /* Size of the data. */
44 /* If the data was mmapped, this is the length of the map. */
45 bfd_size_type map_len
;
46 /* The data. If NULL, the section data has not been read. */
48 /* If the data was mmapped, this is the map address. */
52 /* A hash table holding every BFD that gdb knows about. This is not
53 to be confused with 'gdb_bfd_cache', which is used for sharing
54 BFDs; in contrast, this hash is used just to implement
57 static htab_t all_bfds
;
62 gdb_bfd_stash_filename (struct bfd
*abfd
)
64 char *name
= bfd_get_filename (abfd
);
67 data
= bfd_alloc (abfd
, strlen (name
) + 1);
70 /* Unwarranted chumminess with BFD. */
71 abfd
->filename
= data
;
74 /* An object of this type is stored in each BFD's user data. */
78 /* The reference count. */
81 /* The mtime of the BFD at the point the cache entry was made. */
84 /* If the BFD comes from an archive, this points to the archive's
85 BFD. Otherwise, this is NULL. */
92 #define GDB_BFD_DATA_ACCESSOR(ABFD) \
93 ((struct gdb_bfd_data *) bfd_usrdata (ABFD))
95 DEFINE_REGISTRY (bfd
, GDB_BFD_DATA_ACCESSOR
)
97 /* A hash table storing all the BFDs maintained in the cache. */
99 static htab_t gdb_bfd_cache
;
101 /* The type of an object being looked up in gdb_bfd_cache. We use
102 htab's capability of storing one kind of object (BFD in this case)
103 and using a different sort of object for searching. */
105 struct gdb_bfd_cache_search
108 const char *filename
;
113 /* A hash function for BFDs. */
116 hash_bfd (const void *b
)
120 /* It is simplest to just hash the filename. */
121 return htab_hash_string (bfd_get_filename (abfd
));
124 /* An equality function for BFDs. Note that this expects the caller
125 to search using struct gdb_bfd_cache_search only, not BFDs. */
128 eq_bfd (const void *a
, const void *b
)
131 const struct gdb_bfd_cache_search
*s
= b
;
132 struct gdb_bfd_data
*gdata
= bfd_usrdata (abfd
);
134 return (gdata
->mtime
== s
->mtime
135 && strcmp (bfd_get_filename (abfd
), s
->filename
) == 0);
141 gdb_bfd_open (const char *name
, const char *target
, int fd
)
146 struct gdb_bfd_cache_search search
;
149 if (gdb_bfd_cache
== NULL
)
150 gdb_bfd_cache
= htab_create_alloc (1, hash_bfd
, eq_bfd
, NULL
,
155 fd
= open (name
, O_RDONLY
| O_BINARY
);
158 bfd_set_error (bfd_error_system_call
);
163 search
.filename
= name
;
164 if (fstat (fd
, &st
) < 0)
166 /* Weird situation here. */
170 search
.mtime
= st
.st_mtime
;
172 /* Note that this must compute the same result as hash_bfd. */
173 hash
= htab_hash_string (name
);
174 /* Note that we cannot use htab_find_slot_with_hash here, because
175 opening the BFD may fail; and this would violate hashtab
177 abfd
= htab_find_with_hash (gdb_bfd_cache
, &search
, hash
);
185 abfd
= bfd_fopen (name
, target
, FOPEN_RB
, fd
);
189 slot
= htab_find_slot_with_hash (gdb_bfd_cache
, &search
, hash
, INSERT
);
193 gdb_bfd_stash_filename (abfd
);
198 /* A helper function that releases any section data attached to the
202 free_one_bfd_section (bfd
*abfd
, asection
*sectp
, void *ignore
)
204 struct gdb_bfd_section_data
*sect
= bfd_get_section_userdata (abfd
, sectp
);
206 if (sect
!= NULL
&& sect
->data
!= NULL
)
209 if (sect
->map_addr
!= NULL
)
213 res
= munmap (sect
->map_addr
, sect
->map_len
);
214 gdb_assert (res
== 0);
222 /* Close ABFD, and warn if that fails. */
225 gdb_bfd_close_or_warn (struct bfd
*abfd
)
228 char *name
= bfd_get_filename (abfd
);
230 bfd_map_over_sections (abfd
, free_one_bfd_section
, NULL
);
232 ret
= bfd_close (abfd
);
235 warning (_("cannot close \"%s\": %s"),
236 name
, bfd_errmsg (bfd_get_error ()));
244 gdb_bfd_ref (struct bfd
*abfd
)
246 struct gdb_bfd_data
*gdata
;
252 gdata
= bfd_usrdata (abfd
);
260 /* Ask BFD to decompress sections in bfd_get_full_section_contents. */
261 abfd
->flags
|= BFD_DECOMPRESS
;
263 gdata
= bfd_zalloc (abfd
, sizeof (struct gdb_bfd_data
));
265 gdata
->mtime
= bfd_get_mtime (abfd
);
266 gdata
->archive_bfd
= NULL
;
267 bfd_usrdata (abfd
) = gdata
;
269 bfd_alloc_data (abfd
);
271 /* This is the first we've seen it, so add it to the hash table. */
272 slot
= htab_find_slot (all_bfds
, abfd
, INSERT
);
273 gdb_assert (slot
&& !*slot
);
280 gdb_bfd_unref (struct bfd
*abfd
)
282 struct gdb_bfd_data
*gdata
;
283 struct gdb_bfd_cache_search search
;
289 gdata
= bfd_usrdata (abfd
);
290 gdb_assert (gdata
->refc
>= 1);
296 archive_bfd
= gdata
->archive_bfd
;
297 search
.filename
= bfd_get_filename (abfd
);
299 if (gdb_bfd_cache
&& search
.filename
)
301 hashval_t hash
= htab_hash_string (search
.filename
);
304 search
.mtime
= gdata
->mtime
;
305 slot
= htab_find_slot_with_hash (gdb_bfd_cache
, &search
, hash
,
309 htab_clear_slot (gdb_bfd_cache
, slot
);
312 bfd_free_data (abfd
);
313 bfd_usrdata (abfd
) = NULL
; /* Paranoia. */
315 htab_remove_elt (all_bfds
, abfd
);
317 gdb_bfd_close_or_warn (abfd
);
319 gdb_bfd_unref (archive_bfd
);
322 /* A helper function that returns the section data descriptor
323 associated with SECTION. If no such descriptor exists, a new one
324 is allocated and cleared. */
326 static struct gdb_bfd_section_data
*
327 get_section_descriptor (asection
*section
)
329 struct gdb_bfd_section_data
*result
;
331 result
= bfd_get_section_userdata (section
->owner
, section
);
335 result
= bfd_zalloc (section
->owner
, sizeof (*result
));
336 bfd_set_section_userdata (section
->owner
, section
, result
);
345 gdb_bfd_map_section (asection
*sectp
, bfd_size_type
*size
)
348 struct gdb_bfd_section_data
*descriptor
;
351 gdb_assert ((sectp
->flags
& SEC_RELOC
) == 0);
352 gdb_assert (size
!= NULL
);
356 descriptor
= get_section_descriptor (sectp
);
358 /* If the data was already read for this BFD, just reuse it. */
359 if (descriptor
->data
!= NULL
)
363 if (!bfd_is_section_compressed (abfd
, sectp
))
365 /* The page size, used when mmapping. */
369 pagesize
= getpagesize ();
371 /* Only try to mmap sections which are large enough: we don't want
372 to waste space due to fragmentation. */
374 if (bfd_get_section_size (sectp
) > 4 * pagesize
)
376 descriptor
->size
= bfd_get_section_size (sectp
);
377 descriptor
->data
= bfd_mmap (abfd
, 0, descriptor
->size
, PROT_READ
,
378 MAP_PRIVATE
, sectp
->filepos
,
379 &descriptor
->map_addr
,
380 &descriptor
->map_len
);
382 if ((caddr_t
)descriptor
->data
!= MAP_FAILED
)
384 #if HAVE_POSIX_MADVISE
385 posix_madvise (descriptor
->map_addr
, descriptor
->map_len
,
386 POSIX_MADV_WILLNEED
);
391 /* On failure, clear out the section data and try again. */
392 memset (descriptor
, 0, sizeof (*descriptor
));
395 #endif /* HAVE_MMAP */
397 /* Handle compressed sections, or ordinary uncompressed sections in
400 descriptor
->size
= bfd_get_section_size (sectp
);
401 descriptor
->data
= NULL
;
404 if (!bfd_get_full_section_contents (abfd
, sectp
, &data
))
405 error (_("Can't read data for section '%s' in file '%s'"),
406 bfd_get_section_name (abfd
, sectp
),
407 bfd_get_filename (abfd
));
408 descriptor
->data
= data
;
411 gdb_assert (descriptor
->data
!= NULL
);
412 *size
= descriptor
->size
;
413 return descriptor
->data
;
421 gdb_bfd_fopen (const char *filename
, const char *target
, const char *mode
,
424 bfd
*result
= bfd_fopen (filename
, target
, mode
, fd
);
428 gdb_bfd_stash_filename (result
);
429 gdb_bfd_ref (result
);
438 gdb_bfd_openr (const char *filename
, const char *target
)
440 bfd
*result
= bfd_openr (filename
, target
);
444 gdb_bfd_stash_filename (result
);
445 gdb_bfd_ref (result
);
454 gdb_bfd_openw (const char *filename
, const char *target
)
456 bfd
*result
= bfd_openw (filename
, target
);
460 gdb_bfd_stash_filename (result
);
461 gdb_bfd_ref (result
);
470 gdb_bfd_openr_iovec (const char *filename
, const char *target
,
471 void *(*open_func
) (struct bfd
*nbfd
,
474 file_ptr (*pread_func
) (struct bfd
*nbfd
,
479 int (*close_func
) (struct bfd
*nbfd
,
481 int (*stat_func
) (struct bfd
*abfd
,
485 bfd
*result
= bfd_openr_iovec (filename
, target
,
486 open_func
, open_closure
,
487 pread_func
, close_func
, stat_func
);
491 gdb_bfd_ref (result
);
492 gdb_bfd_stash_filename (result
);
501 gdb_bfd_mark_parent (bfd
*child
, bfd
*parent
)
503 struct gdb_bfd_data
*gdata
;
506 /* No need to stash the filename here, because we also keep a
507 reference on the parent archive. */
509 gdata
= bfd_usrdata (child
);
510 if (gdata
->archive_bfd
== NULL
)
512 gdata
->archive_bfd
= parent
;
513 gdb_bfd_ref (parent
);
516 gdb_assert (gdata
->archive_bfd
== parent
);
522 gdb_bfd_openr_next_archived_file (bfd
*archive
, bfd
*previous
)
524 bfd
*result
= bfd_openr_next_archived_file (archive
, previous
);
527 gdb_bfd_mark_parent (result
, archive
);
535 gdb_bfd_fdopenr (const char *filename
, const char *target
, int fd
)
537 bfd
*result
= bfd_fdopenr (filename
, target
, fd
);
541 gdb_bfd_ref (result
);
542 gdb_bfd_stash_filename (result
);
550 /* A callback for htab_traverse that prints a single BFD. */
553 print_one_bfd (void **slot
, void *data
)
556 struct gdb_bfd_data
*gdata
= bfd_usrdata (abfd
);
557 struct ui_out
*uiout
= data
;
558 struct cleanup
*inner
;
560 inner
= make_cleanup_ui_out_tuple_begin_end (uiout
, NULL
);
561 ui_out_field_int (uiout
, "refcount", gdata
->refc
);
562 ui_out_field_string (uiout
, "addr", host_address_to_string (abfd
));
563 ui_out_field_string (uiout
, "filename", bfd_get_filename (abfd
));
564 ui_out_text (uiout
, "\n");
570 /* Implement the 'maint info bfd' command. */
573 maintenance_info_bfds (char *arg
, int from_tty
)
575 struct cleanup
*cleanup
;
576 struct ui_out
*uiout
= current_uiout
;
578 cleanup
= make_cleanup_ui_out_table_begin_end (uiout
, 3, -1, "bfds");
579 ui_out_table_header (uiout
, 10, ui_left
, "refcount", "Refcount");
580 ui_out_table_header (uiout
, 18, ui_left
, "addr", "Address");
581 ui_out_table_header (uiout
, 40, ui_left
, "filename", "Filename");
583 ui_out_table_body (uiout
);
584 htab_traverse (all_bfds
, print_one_bfd
, uiout
);
586 do_cleanups (cleanup
);
589 /* -Wmissing-prototypes */
590 extern initialize_file_ftype _initialize_gdb_bfd
;
593 _initialize_gdb_bfd (void)
595 all_bfds
= htab_create_alloc (10, htab_hash_pointer
, htab_eq_pointer
,
596 NULL
, xcalloc
, xfree
);
598 add_cmd ("bfds", class_maintenance
, maintenance_info_bfds
, _("\
599 List the BFDs that are currently open."),
600 &maintenanceinfolist
);