1 /* Definitions for BFD wrappers used by GDB.
3 Copyright (C) 2011, 2012
4 Free Software Foundation, Inc.
6 This file is part of GDB.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
23 #include "gdb_assert.h"
24 #include "gdb_string.h"
34 #define MAP_FAILED ((void *) -1)
38 /* An object of this type is stored in the section's user data when
41 struct gdb_bfd_section_data
43 /* Size of the data. */
45 /* If the data was mmapped, this is the length of the map. */
46 bfd_size_type map_len
;
47 /* The data. If NULL, the section data has not been read. */
49 /* If the data was mmapped, this is the map address. */
53 /* A hash table holding every BFD that gdb knows about. This is not
54 to be confused with 'gdb_bfd_cache', which is used for sharing
55 BFDs; in contrast, this hash is used just to implement
58 static htab_t all_bfds
;
63 gdb_bfd_stash_filename (struct bfd
*abfd
)
65 char *name
= bfd_get_filename (abfd
);
68 data
= bfd_alloc (abfd
, strlen (name
) + 1);
71 /* Unwarranted chumminess with BFD. */
72 abfd
->filename
= data
;
75 /* An object of this type is stored in each BFD's user data. */
79 /* The reference count. */
82 /* The mtime of the BFD at the point the cache entry was made. */
86 /* A hash table storing all the BFDs maintained in the cache. */
88 static htab_t gdb_bfd_cache
;
90 /* The type of an object being looked up in gdb_bfd_cache. We use
91 htab's capability of storing one kind of object (BFD in this case)
92 and using a different sort of object for searching. */
94 struct gdb_bfd_cache_search
102 /* A hash function for BFDs. */
105 hash_bfd (const void *b
)
109 /* It is simplest to just hash the filename. */
110 return htab_hash_string (bfd_get_filename (abfd
));
113 /* An equality function for BFDs. Note that this expects the caller
114 to search using struct gdb_bfd_cache_search only, not BFDs. */
117 eq_bfd (const void *a
, const void *b
)
120 const struct gdb_bfd_cache_search
*s
= b
;
121 struct gdb_bfd_data
*gdata
= bfd_usrdata (abfd
);
123 return (gdata
->mtime
== s
->mtime
124 && strcmp (bfd_get_filename (abfd
), s
->filename
) == 0);
130 gdb_bfd_open (const char *name
, const char *target
, int fd
)
135 struct gdb_bfd_cache_search search
;
138 if (gdb_bfd_cache
== NULL
)
139 gdb_bfd_cache
= htab_create_alloc (1, hash_bfd
, eq_bfd
, NULL
,
144 fd
= open (name
, O_RDONLY
| O_BINARY
);
147 bfd_set_error (bfd_error_system_call
);
152 search
.filename
= name
;
153 if (fstat (fd
, &st
) < 0)
155 /* Weird situation here. */
159 search
.mtime
= st
.st_mtime
;
161 /* Note that this must compute the same result as hash_bfd. */
162 hash
= htab_hash_string (name
);
163 /* Note that we cannot use htab_find_slot_with_hash here, because
164 opening the BFD may fail; and this would violate hashtab
166 abfd
= htab_find_with_hash (gdb_bfd_cache
, &search
, hash
);
174 abfd
= bfd_fopen (name
, target
, FOPEN_RB
, fd
);
178 slot
= htab_find_slot_with_hash (gdb_bfd_cache
, &search
, hash
, INSERT
);
182 gdb_bfd_stash_filename (abfd
);
187 /* A helper function that releases any section data attached to the
191 free_one_bfd_section (bfd
*abfd
, asection
*sectp
, void *ignore
)
193 struct gdb_bfd_section_data
*sect
= bfd_get_section_userdata (abfd
, sectp
);
195 if (sect
!= NULL
&& sect
->data
!= NULL
)
198 if (sect
->map_addr
!= NULL
)
202 res
= munmap (sect
->map_addr
, sect
->map_len
);
203 gdb_assert (res
== 0);
211 /* Close ABFD, and warn if that fails. */
214 gdb_bfd_close_or_warn (struct bfd
*abfd
)
217 char *name
= bfd_get_filename (abfd
);
219 bfd_map_over_sections (abfd
, free_one_bfd_section
, NULL
);
221 ret
= bfd_close (abfd
);
224 warning (_("cannot close \"%s\": %s"),
225 name
, bfd_errmsg (bfd_get_error ()));
233 gdb_bfd_ref (struct bfd
*abfd
)
235 struct gdb_bfd_data
*gdata
;
241 gdata
= bfd_usrdata (abfd
);
249 gdata
= bfd_zalloc (abfd
, sizeof (struct gdb_bfd_data
));
251 gdata
->mtime
= bfd_get_mtime (abfd
);
252 bfd_usrdata (abfd
) = gdata
;
254 /* This is the first we've seen it, so add it to the hash table. */
255 slot
= htab_find_slot (all_bfds
, abfd
, INSERT
);
256 gdb_assert (slot
&& !*slot
);
263 gdb_bfd_unref (struct bfd
*abfd
)
265 struct gdb_bfd_data
*gdata
;
266 struct gdb_bfd_cache_search search
;
272 gdata
= bfd_usrdata (abfd
);
273 gdb_assert (gdata
->refc
>= 1);
279 search
.filename
= bfd_get_filename (abfd
);
281 if (gdb_bfd_cache
&& search
.filename
)
283 hashval_t hash
= htab_hash_string (search
.filename
);
286 search
.mtime
= gdata
->mtime
;
287 slot
= htab_find_slot_with_hash (gdb_bfd_cache
, &search
, hash
,
291 htab_clear_slot (gdb_bfd_cache
, slot
);
294 bfd_usrdata (abfd
) = NULL
; /* Paranoia. */
296 htab_remove_elt (all_bfds
, abfd
);
298 gdb_bfd_close_or_warn (abfd
);
301 /* A helper function that returns the section data descriptor
302 associated with SECTION. If no such descriptor exists, a new one
303 is allocated and cleared. */
305 static struct gdb_bfd_section_data
*
306 get_section_descriptor (asection
*section
)
308 struct gdb_bfd_section_data
*result
;
310 result
= bfd_get_section_userdata (section
->owner
, section
);
314 result
= bfd_zalloc (section
->owner
, sizeof (*result
));
315 bfd_set_section_userdata (section
->owner
, section
, result
);
321 /* Decompress a section that was compressed using zlib. Store the
322 decompressed buffer, and its size, in DESCRIPTOR. */
325 zlib_decompress_section (asection
*sectp
,
326 struct gdb_bfd_section_data
*descriptor
)
328 bfd
*abfd
= sectp
->owner
;
330 error (_("Support for zlib-compressed data (from '%s', section '%s') "
331 "is disabled in this copy of GDB"),
332 bfd_get_filename (abfd
),
333 bfd_get_section_name (abfd
, sectp
));
335 bfd_size_type compressed_size
= bfd_get_section_size (sectp
);
336 gdb_byte
*compressed_buffer
= xmalloc (compressed_size
);
337 struct cleanup
*cleanup
= make_cleanup (xfree
, compressed_buffer
);
338 struct cleanup
*inner_cleanup
;
339 bfd_size_type uncompressed_size
;
340 gdb_byte
*uncompressed_buffer
;
343 int header_size
= 12;
344 struct dwarf2_per_bfd_section
*section_data
;
346 if (bfd_seek (abfd
, sectp
->filepos
, SEEK_SET
) != 0
347 || bfd_bread (compressed_buffer
,
348 compressed_size
, abfd
) != compressed_size
)
349 error (_("can't read data from '%s', section '%s'"),
350 bfd_get_filename (abfd
),
351 bfd_get_section_name (abfd
, sectp
));
353 /* Read the zlib header. In this case, it should be "ZLIB" followed
354 by the uncompressed section size, 8 bytes in big-endian order. */
355 if (compressed_size
< header_size
356 || strncmp (compressed_buffer
, "ZLIB", 4) != 0)
357 error (_("corrupt ZLIB header from '%s', section '%s'"),
358 bfd_get_filename (abfd
),
359 bfd_get_section_name (abfd
, sectp
));
360 uncompressed_size
= compressed_buffer
[4]; uncompressed_size
<<= 8;
361 uncompressed_size
+= compressed_buffer
[5]; uncompressed_size
<<= 8;
362 uncompressed_size
+= compressed_buffer
[6]; uncompressed_size
<<= 8;
363 uncompressed_size
+= compressed_buffer
[7]; uncompressed_size
<<= 8;
364 uncompressed_size
+= compressed_buffer
[8]; uncompressed_size
<<= 8;
365 uncompressed_size
+= compressed_buffer
[9]; uncompressed_size
<<= 8;
366 uncompressed_size
+= compressed_buffer
[10]; uncompressed_size
<<= 8;
367 uncompressed_size
+= compressed_buffer
[11];
369 /* It is possible the section consists of several compressed
370 buffers concatenated together, so we uncompress in a loop. */
374 strm
.avail_in
= compressed_size
- header_size
;
375 strm
.next_in
= (Bytef
*) compressed_buffer
+ header_size
;
376 strm
.avail_out
= uncompressed_size
;
377 uncompressed_buffer
= xmalloc (uncompressed_size
);
378 inner_cleanup
= make_cleanup (xfree
, uncompressed_buffer
);
379 rc
= inflateInit (&strm
);
380 while (strm
.avail_in
> 0)
383 error (_("setting up uncompression in '%s', section '%s': %d"),
384 bfd_get_filename (abfd
),
385 bfd_get_section_name (abfd
, sectp
),
387 strm
.next_out
= ((Bytef
*) uncompressed_buffer
388 + (uncompressed_size
- strm
.avail_out
));
389 rc
= inflate (&strm
, Z_FINISH
);
390 if (rc
!= Z_STREAM_END
)
391 error (_("zlib error uncompressing from '%s', section '%s': %d"),
392 bfd_get_filename (abfd
),
393 bfd_get_section_name (abfd
, sectp
),
395 rc
= inflateReset (&strm
);
397 rc
= inflateEnd (&strm
);
399 || strm
.avail_out
!= 0)
400 error (_("concluding uncompression in '%s', section '%s': %d"),
401 bfd_get_filename (abfd
),
402 bfd_get_section_name (abfd
, sectp
),
405 discard_cleanups (inner_cleanup
);
406 do_cleanups (cleanup
);
408 /* Attach the data to the BFD section. */
409 descriptor
->data
= uncompressed_buffer
;
410 descriptor
->size
= uncompressed_size
;
417 gdb_bfd_map_section (asection
*sectp
, bfd_size_type
*size
)
420 gdb_byte
*buf
, *retbuf
;
421 unsigned char header
[4];
422 struct gdb_bfd_section_data
*descriptor
;
424 gdb_assert ((sectp
->flags
& SEC_RELOC
) == 0);
425 gdb_assert (size
!= NULL
);
429 descriptor
= get_section_descriptor (sectp
);
431 /* If the data was already read for this BFD, just reuse it. */
432 if (descriptor
->data
!= NULL
)
435 /* Check if the file has a 4-byte header indicating compression. */
436 if (bfd_get_section_size (sectp
) > sizeof (header
)
437 && bfd_seek (abfd
, sectp
->filepos
, SEEK_SET
) == 0
438 && bfd_bread (header
, sizeof (header
), abfd
) == sizeof (header
))
440 /* Upon decompression, update the buffer and its size. */
441 if (strncmp (header
, "ZLIB", sizeof (header
)) == 0)
443 zlib_decompress_section (sectp
, descriptor
);
450 /* The page size, used when mmapping. */
454 pagesize
= getpagesize ();
456 /* Only try to mmap sections which are large enough: we don't want
457 to waste space due to fragmentation. */
459 if (bfd_get_section_size (sectp
) > 4 * pagesize
)
461 descriptor
->size
= bfd_get_section_size (sectp
);
462 descriptor
->data
= bfd_mmap (abfd
, 0, descriptor
->size
, PROT_READ
,
463 MAP_PRIVATE
, sectp
->filepos
,
464 &descriptor
->map_addr
,
465 &descriptor
->map_len
);
467 if ((caddr_t
)descriptor
->data
!= MAP_FAILED
)
469 #if HAVE_POSIX_MADVISE
470 posix_madvise (descriptor
->map_addr
, descriptor
->map_len
,
471 POSIX_MADV_WILLNEED
);
476 /* On failure, clear out the section data and try again. */
477 memset (descriptor
, 0, sizeof (*descriptor
));
480 #endif /* HAVE_MMAP */
482 /* If we get here, we are a normal, not-compressed section. */
484 descriptor
->size
= bfd_get_section_size (sectp
);
485 descriptor
->data
= xmalloc (descriptor
->size
);
487 if (bfd_seek (abfd
, sectp
->filepos
, SEEK_SET
) != 0
488 || bfd_bread (descriptor
->data
, bfd_get_section_size (sectp
),
489 abfd
) != bfd_get_section_size (sectp
))
491 xfree (descriptor
->data
);
492 descriptor
->data
= NULL
;
493 error (_("Can't read data for section '%s'"),
494 bfd_get_filename (abfd
));
498 gdb_assert (descriptor
->data
!= NULL
);
499 *size
= descriptor
->size
;
500 return descriptor
->data
;
508 gdb_bfd_fopen (const char *filename
, const char *target
, const char *mode
,
511 bfd
*result
= bfd_fopen (filename
, target
, mode
, fd
);
515 gdb_bfd_stash_filename (result
);
516 gdb_bfd_ref (result
);
525 gdb_bfd_openr (const char *filename
, const char *target
)
527 bfd
*result
= bfd_openr (filename
, target
);
531 gdb_bfd_stash_filename (result
);
532 gdb_bfd_ref (result
);
541 gdb_bfd_openw (const char *filename
, const char *target
)
543 bfd
*result
= bfd_openw (filename
, target
);
547 gdb_bfd_stash_filename (result
);
548 gdb_bfd_ref (result
);
557 gdb_bfd_openr_iovec (const char *filename
, const char *target
,
558 void *(*open_func
) (struct bfd
*nbfd
,
561 file_ptr (*pread_func
) (struct bfd
*nbfd
,
566 int (*close_func
) (struct bfd
*nbfd
,
568 int (*stat_func
) (struct bfd
*abfd
,
572 bfd
*result
= bfd_openr_iovec (filename
, target
,
573 open_func
, open_closure
,
574 pread_func
, close_func
, stat_func
);
578 gdb_bfd_ref (result
);
579 gdb_bfd_stash_filename (result
);
588 gdb_bfd_openr_next_archived_file (bfd
*archive
, bfd
*previous
)
590 bfd
*result
= bfd_openr_next_archived_file (archive
, previous
);
594 gdb_bfd_ref (result
);
595 /* No need to stash the filename here. */
604 gdb_bfd_fdopenr (const char *filename
, const char *target
, int fd
)
606 bfd
*result
= bfd_fdopenr (filename
, target
, fd
);
610 gdb_bfd_ref (result
);
611 gdb_bfd_stash_filename (result
);
619 /* A callback for htab_traverse that prints a single BFD. */
622 print_one_bfd (void **slot
, void *data
)
625 struct gdb_bfd_data
*gdata
= bfd_usrdata (abfd
);
626 struct ui_out
*uiout
= data
;
627 struct cleanup
*inner
;
629 inner
= make_cleanup_ui_out_tuple_begin_end (uiout
, NULL
);
630 ui_out_field_int (uiout
, "refcount", gdata
->refc
);
631 ui_out_field_string (uiout
, "addr", host_address_to_string (abfd
));
632 ui_out_field_string (uiout
, "filename", bfd_get_filename (abfd
));
633 ui_out_text (uiout
, "\n");
639 /* Implement the 'maint info bfd' command. */
642 maintenance_info_bfds (char *arg
, int from_tty
)
644 struct cleanup
*cleanup
;
645 struct ui_out
*uiout
= current_uiout
;
647 cleanup
= make_cleanup_ui_out_table_begin_end (uiout
, 3, -1, "bfds");
648 ui_out_table_header (uiout
, 10, ui_left
, "refcount", "Refcount");
649 ui_out_table_header (uiout
, 18, ui_left
, "addr", "Address");
650 ui_out_table_header (uiout
, 40, ui_left
, "filename", "Filename");
652 ui_out_table_body (uiout
);
653 htab_traverse (all_bfds
, print_one_bfd
, uiout
);
655 do_cleanups (cleanup
);
658 /* -Wmissing-prototypes */
659 extern initialize_file_ftype _initialize_gdb_bfd
;
662 _initialize_gdb_bfd (void)
664 all_bfds
= htab_create_alloc (10, htab_hash_pointer
, htab_eq_pointer
,
665 NULL
, xcalloc
, xfree
);
667 add_cmd ("bfds", class_maintenance
, maintenance_info_bfds
, _("\
668 List the BFDs that are currently open."),
669 &maintenanceinfolist
);