Use "new" to allocate gdb_bfd_data
[binutils-gdb.git] / gdb / gdb_bfd.c
1 /* Definitions for BFD wrappers used by GDB.
2
3 Copyright (C) 2011-2017 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
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.
11
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.
16
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/>. */
19
20 #include "defs.h"
21 #include "gdb_bfd.h"
22 #include "ui-out.h"
23 #include "gdbcmd.h"
24 #include "hashtab.h"
25 #include "filestuff.h"
26 #include "vec.h"
27 #ifdef HAVE_MMAP
28 #include <sys/mman.h>
29 #ifndef MAP_FAILED
30 #define MAP_FAILED ((void *) -1)
31 #endif
32 #endif
33 #include "target.h"
34 #include "gdb/fileio.h"
35 #include "inferior.h"
36
37 typedef bfd *bfdp;
38 DEF_VEC_P (bfdp);
39
40 /* An object of this type is stored in the section's user data when
41 mapping a section. */
42
43 struct gdb_bfd_section_data
44 {
45 /* Size of the data. */
46 bfd_size_type size;
47 /* If the data was mmapped, this is the length of the map. */
48 bfd_size_type map_len;
49 /* The data. If NULL, the section data has not been read. */
50 void *data;
51 /* If the data was mmapped, this is the map address. */
52 void *map_addr;
53 };
54
55 /* A hash table holding every BFD that gdb knows about. This is not
56 to be confused with 'gdb_bfd_cache', which is used for sharing
57 BFDs; in contrast, this hash is used just to implement
58 "maint info bfd". */
59
60 static htab_t all_bfds;
61
62 /* An object of this type is stored in each BFD's user data. */
63
64 struct gdb_bfd_data
65 {
66 gdb_bfd_data (bfd *abfd)
67 : mtime (bfd_get_mtime (abfd)),
68 size (bfd_get_size (abfd)),
69 relocation_computed (0),
70 needs_relocations (0),
71 crc_computed (0)
72 {
73 struct stat buf;
74
75 if (bfd_stat (abfd, &buf) == 0)
76 {
77 inode = buf.st_ino;
78 device_id = buf.st_dev;
79 }
80 else
81 {
82 /* The stat failed. */
83 inode = 0;
84 device_id = 0;
85 }
86 }
87
88 ~gdb_bfd_data ()
89 {
90 int ix;
91 bfd *included_bfd;
92
93 for (ix = 0;
94 VEC_iterate (bfdp, included_bfds, ix, included_bfd);
95 ++ix)
96 gdb_bfd_unref (included_bfd);
97 VEC_free (bfdp, included_bfds);
98 }
99
100 /* The reference count. */
101 int refc = 1;
102
103 /* The mtime of the BFD at the point the cache entry was made. */
104 time_t mtime;
105
106 /* The file size (in bytes) at the point the cache entry was made. */
107 off_t size;
108
109 /* The inode of the file at the point the cache entry was made. */
110 ino_t inode;
111
112 /* The device id of the file at the point the cache entry was made. */
113 dev_t device_id;
114
115 /* This is true if we have determined whether this BFD has any
116 sections requiring relocation. */
117 unsigned int relocation_computed : 1;
118
119 /* This is true if any section needs relocation. */
120 unsigned int needs_relocations : 1;
121
122 /* This is true if we have successfully computed the file's CRC. */
123 unsigned int crc_computed : 1;
124
125 /* The file's CRC. */
126 unsigned long crc = 0;
127
128 /* If the BFD comes from an archive, this points to the archive's
129 BFD. Otherwise, this is NULL. */
130 bfd *archive_bfd = nullptr;
131
132 /* Table of all the bfds this bfd has included. */
133 VEC (bfdp) *included_bfds = nullptr;
134
135 /* The registry. */
136 REGISTRY_FIELDS = {};
137 };
138
139 #define GDB_BFD_DATA_ACCESSOR(ABFD) \
140 ((struct gdb_bfd_data *) bfd_usrdata (ABFD))
141
142 DEFINE_REGISTRY (bfd, GDB_BFD_DATA_ACCESSOR)
143
144 /* A hash table storing all the BFDs maintained in the cache. */
145
146 static htab_t gdb_bfd_cache;
147
148 /* When true gdb will reuse an existing bfd object if the filename,
149 modification time, and file size all match. */
150
151 static int bfd_sharing = 1;
152 static void
153 show_bfd_sharing (struct ui_file *file, int from_tty,
154 struct cmd_list_element *c, const char *value)
155 {
156 fprintf_filtered (file, _("BFD sharing is %s.\n"), value);
157 }
158
159 /* When non-zero debugging of the bfd caches is enabled. */
160
161 static unsigned int debug_bfd_cache;
162 static void
163 show_bfd_cache_debug (struct ui_file *file, int from_tty,
164 struct cmd_list_element *c, const char *value)
165 {
166 fprintf_filtered (file, _("BFD cache debugging is %s.\n"), value);
167 }
168
169 /* The type of an object being looked up in gdb_bfd_cache. We use
170 htab's capability of storing one kind of object (BFD in this case)
171 and using a different sort of object for searching. */
172
173 struct gdb_bfd_cache_search
174 {
175 /* The filename. */
176 const char *filename;
177 /* The mtime. */
178 time_t mtime;
179 /* The file size (in bytes). */
180 off_t size;
181 /* The inode of the file. */
182 ino_t inode;
183 /* The device id of the file. */
184 dev_t device_id;
185 };
186
187 /* A hash function for BFDs. */
188
189 static hashval_t
190 hash_bfd (const void *b)
191 {
192 const bfd *abfd = (const struct bfd *) b;
193
194 /* It is simplest to just hash the filename. */
195 return htab_hash_string (bfd_get_filename (abfd));
196 }
197
198 /* An equality function for BFDs. Note that this expects the caller
199 to search using struct gdb_bfd_cache_search only, not BFDs. */
200
201 static int
202 eq_bfd (const void *a, const void *b)
203 {
204 const bfd *abfd = (const struct bfd *) a;
205 const struct gdb_bfd_cache_search *s
206 = (const struct gdb_bfd_cache_search *) b;
207 struct gdb_bfd_data *gdata = (struct gdb_bfd_data *) bfd_usrdata (abfd);
208
209 return (gdata->mtime == s->mtime
210 && gdata->size == s->size
211 && gdata->inode == s->inode
212 && gdata->device_id == s->device_id
213 && strcmp (bfd_get_filename (abfd), s->filename) == 0);
214 }
215
216 /* See gdb_bfd.h. */
217
218 int
219 is_target_filename (const char *name)
220 {
221 return startswith (name, TARGET_SYSROOT_PREFIX);
222 }
223
224 /* See gdb_bfd.h. */
225
226 int
227 gdb_bfd_has_target_filename (struct bfd *abfd)
228 {
229 return is_target_filename (bfd_get_filename (abfd));
230 }
231
232
233 /* Return the system error number corresponding to ERRNUM. */
234
235 static int
236 fileio_errno_to_host (int errnum)
237 {
238 switch (errnum)
239 {
240 case FILEIO_EPERM:
241 return EPERM;
242 case FILEIO_ENOENT:
243 return ENOENT;
244 case FILEIO_EINTR:
245 return EINTR;
246 case FILEIO_EIO:
247 return EIO;
248 case FILEIO_EBADF:
249 return EBADF;
250 case FILEIO_EACCES:
251 return EACCES;
252 case FILEIO_EFAULT:
253 return EFAULT;
254 case FILEIO_EBUSY:
255 return EBUSY;
256 case FILEIO_EEXIST:
257 return EEXIST;
258 case FILEIO_ENODEV:
259 return ENODEV;
260 case FILEIO_ENOTDIR:
261 return ENOTDIR;
262 case FILEIO_EISDIR:
263 return EISDIR;
264 case FILEIO_EINVAL:
265 return EINVAL;
266 case FILEIO_ENFILE:
267 return ENFILE;
268 case FILEIO_EMFILE:
269 return EMFILE;
270 case FILEIO_EFBIG:
271 return EFBIG;
272 case FILEIO_ENOSPC:
273 return ENOSPC;
274 case FILEIO_ESPIPE:
275 return ESPIPE;
276 case FILEIO_EROFS:
277 return EROFS;
278 case FILEIO_ENOSYS:
279 return ENOSYS;
280 case FILEIO_ENAMETOOLONG:
281 return ENAMETOOLONG;
282 }
283 return -1;
284 }
285
286 /* Wrapper for target_fileio_open suitable for passing as the
287 OPEN_FUNC argument to gdb_bfd_openr_iovec. The supplied
288 OPEN_CLOSURE is unused. */
289
290 static void *
291 gdb_bfd_iovec_fileio_open (struct bfd *abfd, void *inferior)
292 {
293 const char *filename = bfd_get_filename (abfd);
294 int fd, target_errno;
295 int *stream;
296
297 gdb_assert (is_target_filename (filename));
298
299 fd = target_fileio_open_warn_if_slow ((struct inferior *) inferior,
300 filename
301 + strlen (TARGET_SYSROOT_PREFIX),
302 FILEIO_O_RDONLY, 0,
303 &target_errno);
304 if (fd == -1)
305 {
306 errno = fileio_errno_to_host (target_errno);
307 bfd_set_error (bfd_error_system_call);
308 return NULL;
309 }
310
311 stream = XCNEW (int);
312 *stream = fd;
313 return stream;
314 }
315
316 /* Wrapper for target_fileio_pread suitable for passing as the
317 PREAD_FUNC argument to gdb_bfd_openr_iovec. */
318
319 static file_ptr
320 gdb_bfd_iovec_fileio_pread (struct bfd *abfd, void *stream, void *buf,
321 file_ptr nbytes, file_ptr offset)
322 {
323 int fd = *(int *) stream;
324 int target_errno;
325 file_ptr pos, bytes;
326
327 pos = 0;
328 while (nbytes > pos)
329 {
330 QUIT;
331
332 bytes = target_fileio_pread (fd, (gdb_byte *) buf + pos,
333 nbytes - pos, offset + pos,
334 &target_errno);
335 if (bytes == 0)
336 /* Success, but no bytes, means end-of-file. */
337 break;
338 if (bytes == -1)
339 {
340 errno = fileio_errno_to_host (target_errno);
341 bfd_set_error (bfd_error_system_call);
342 return -1;
343 }
344
345 pos += bytes;
346 }
347
348 return pos;
349 }
350
351 /* Wrapper for target_fileio_close suitable for passing as the
352 CLOSE_FUNC argument to gdb_bfd_openr_iovec. */
353
354 static int
355 gdb_bfd_iovec_fileio_close (struct bfd *abfd, void *stream)
356 {
357 int fd = *(int *) stream;
358 int target_errno;
359
360 xfree (stream);
361
362 /* Ignore errors on close. These may happen with remote
363 targets if the connection has already been torn down. */
364 target_fileio_close (fd, &target_errno);
365
366 /* Zero means success. */
367 return 0;
368 }
369
370 /* Wrapper for target_fileio_fstat suitable for passing as the
371 STAT_FUNC argument to gdb_bfd_openr_iovec. */
372
373 static int
374 gdb_bfd_iovec_fileio_fstat (struct bfd *abfd, void *stream,
375 struct stat *sb)
376 {
377 int fd = *(int *) stream;
378 int target_errno;
379 int result;
380
381 result = target_fileio_fstat (fd, sb, &target_errno);
382 if (result == -1)
383 {
384 errno = fileio_errno_to_host (target_errno);
385 bfd_set_error (bfd_error_system_call);
386 }
387
388 return result;
389 }
390
391 /* See gdb_bfd.h. */
392
393 gdb_bfd_ref_ptr
394 gdb_bfd_open (const char *name, const char *target, int fd)
395 {
396 hashval_t hash;
397 void **slot;
398 bfd *abfd;
399 struct gdb_bfd_cache_search search;
400 struct stat st;
401
402 if (is_target_filename (name))
403 {
404 if (!target_filesystem_is_local ())
405 {
406 gdb_assert (fd == -1);
407
408 return gdb_bfd_openr_iovec (name, target,
409 gdb_bfd_iovec_fileio_open,
410 current_inferior (),
411 gdb_bfd_iovec_fileio_pread,
412 gdb_bfd_iovec_fileio_close,
413 gdb_bfd_iovec_fileio_fstat);
414 }
415
416 name += strlen (TARGET_SYSROOT_PREFIX);
417 }
418
419 if (gdb_bfd_cache == NULL)
420 gdb_bfd_cache = htab_create_alloc (1, hash_bfd, eq_bfd, NULL,
421 xcalloc, xfree);
422
423 if (fd == -1)
424 {
425 fd = gdb_open_cloexec (name, O_RDONLY | O_BINARY, 0);
426 if (fd == -1)
427 {
428 bfd_set_error (bfd_error_system_call);
429 return NULL;
430 }
431 }
432
433 search.filename = name;
434 if (fstat (fd, &st) < 0)
435 {
436 /* Weird situation here. */
437 search.mtime = 0;
438 search.size = 0;
439 search.inode = 0;
440 search.device_id = 0;
441 }
442 else
443 {
444 search.mtime = st.st_mtime;
445 search.size = st.st_size;
446 search.inode = st.st_ino;
447 search.device_id = st.st_dev;
448 }
449
450 /* Note that this must compute the same result as hash_bfd. */
451 hash = htab_hash_string (name);
452 /* Note that we cannot use htab_find_slot_with_hash here, because
453 opening the BFD may fail; and this would violate hashtab
454 invariants. */
455 abfd = (struct bfd *) htab_find_with_hash (gdb_bfd_cache, &search, hash);
456 if (bfd_sharing && abfd != NULL)
457 {
458 if (debug_bfd_cache)
459 fprintf_unfiltered (gdb_stdlog,
460 "Reusing cached bfd %s for %s\n",
461 host_address_to_string (abfd),
462 bfd_get_filename (abfd));
463 close (fd);
464 return new_bfd_ref (abfd);
465 }
466
467 abfd = bfd_fopen (name, target, FOPEN_RB, fd);
468 if (abfd == NULL)
469 return NULL;
470
471 if (debug_bfd_cache)
472 fprintf_unfiltered (gdb_stdlog,
473 "Creating new bfd %s for %s\n",
474 host_address_to_string (abfd),
475 bfd_get_filename (abfd));
476
477 if (bfd_sharing)
478 {
479 slot = htab_find_slot_with_hash (gdb_bfd_cache, &search, hash, INSERT);
480 gdb_assert (!*slot);
481 *slot = abfd;
482 }
483
484 return new_bfd_ref (abfd);
485 }
486
487 /* A helper function that releases any section data attached to the
488 BFD. */
489
490 static void
491 free_one_bfd_section (bfd *abfd, asection *sectp, void *ignore)
492 {
493 struct gdb_bfd_section_data *sect
494 = (struct gdb_bfd_section_data *) bfd_get_section_userdata (abfd, sectp);
495
496 if (sect != NULL && sect->data != NULL)
497 {
498 #ifdef HAVE_MMAP
499 if (sect->map_addr != NULL)
500 {
501 int res;
502
503 res = munmap (sect->map_addr, sect->map_len);
504 gdb_assert (res == 0);
505 }
506 else
507 #endif
508 xfree (sect->data);
509 }
510 }
511
512 /* Close ABFD, and warn if that fails. */
513
514 static int
515 gdb_bfd_close_or_warn (struct bfd *abfd)
516 {
517 int ret;
518 char *name = bfd_get_filename (abfd);
519
520 bfd_map_over_sections (abfd, free_one_bfd_section, NULL);
521
522 ret = bfd_close (abfd);
523
524 if (!ret)
525 warning (_("cannot close \"%s\": %s"),
526 name, bfd_errmsg (bfd_get_error ()));
527
528 return ret;
529 }
530
531 /* See gdb_bfd.h. */
532
533 void
534 gdb_bfd_ref (struct bfd *abfd)
535 {
536 struct gdb_bfd_data *gdata;
537 void **slot;
538
539 if (abfd == NULL)
540 return;
541
542 gdata = (struct gdb_bfd_data *) bfd_usrdata (abfd);
543
544 if (debug_bfd_cache)
545 fprintf_unfiltered (gdb_stdlog,
546 "Increase reference count on bfd %s (%s)\n",
547 host_address_to_string (abfd),
548 bfd_get_filename (abfd));
549
550 if (gdata != NULL)
551 {
552 gdata->refc += 1;
553 return;
554 }
555
556 /* Ask BFD to decompress sections in bfd_get_full_section_contents. */
557 abfd->flags |= BFD_DECOMPRESS;
558
559 gdata = new gdb_bfd_data (abfd);
560 bfd_usrdata (abfd) = gdata;
561 bfd_alloc_data (abfd);
562
563 /* This is the first we've seen it, so add it to the hash table. */
564 slot = htab_find_slot (all_bfds, abfd, INSERT);
565 gdb_assert (slot && !*slot);
566 *slot = abfd;
567 }
568
569 /* See gdb_bfd.h. */
570
571 void
572 gdb_bfd_unref (struct bfd *abfd)
573 {
574 struct gdb_bfd_data *gdata;
575 struct gdb_bfd_cache_search search;
576 bfd *archive_bfd;
577
578 if (abfd == NULL)
579 return;
580
581 gdata = (struct gdb_bfd_data *) bfd_usrdata (abfd);
582 gdb_assert (gdata->refc >= 1);
583
584 gdata->refc -= 1;
585 if (gdata->refc > 0)
586 {
587 if (debug_bfd_cache)
588 fprintf_unfiltered (gdb_stdlog,
589 "Decrease reference count on bfd %s (%s)\n",
590 host_address_to_string (abfd),
591 bfd_get_filename (abfd));
592 return;
593 }
594
595 if (debug_bfd_cache)
596 fprintf_unfiltered (gdb_stdlog,
597 "Delete final reference count on bfd %s (%s)\n",
598 host_address_to_string (abfd),
599 bfd_get_filename (abfd));
600
601 archive_bfd = gdata->archive_bfd;
602 search.filename = bfd_get_filename (abfd);
603
604 if (gdb_bfd_cache && search.filename)
605 {
606 hashval_t hash = htab_hash_string (search.filename);
607 void **slot;
608
609 search.mtime = gdata->mtime;
610 search.size = gdata->size;
611 search.inode = gdata->inode;
612 search.device_id = gdata->device_id;
613 slot = htab_find_slot_with_hash (gdb_bfd_cache, &search, hash,
614 NO_INSERT);
615
616 if (slot && *slot)
617 htab_clear_slot (gdb_bfd_cache, slot);
618 }
619
620 bfd_free_data (abfd);
621 delete gdata;
622 bfd_usrdata (abfd) = NULL; /* Paranoia. */
623
624 htab_remove_elt (all_bfds, abfd);
625
626 gdb_bfd_close_or_warn (abfd);
627
628 gdb_bfd_unref (archive_bfd);
629 }
630
631 /* A helper function that returns the section data descriptor
632 associated with SECTION. If no such descriptor exists, a new one
633 is allocated and cleared. */
634
635 static struct gdb_bfd_section_data *
636 get_section_descriptor (asection *section)
637 {
638 struct gdb_bfd_section_data *result;
639
640 result = ((struct gdb_bfd_section_data *)
641 bfd_get_section_userdata (section->owner, section));
642
643 if (result == NULL)
644 {
645 result = ((struct gdb_bfd_section_data *)
646 bfd_zalloc (section->owner, sizeof (*result)));
647 bfd_set_section_userdata (section->owner, section, result);
648 }
649
650 return result;
651 }
652
653 /* See gdb_bfd.h. */
654
655 const gdb_byte *
656 gdb_bfd_map_section (asection *sectp, bfd_size_type *size)
657 {
658 bfd *abfd;
659 struct gdb_bfd_section_data *descriptor;
660 bfd_byte *data;
661
662 gdb_assert ((sectp->flags & SEC_RELOC) == 0);
663 gdb_assert (size != NULL);
664
665 abfd = sectp->owner;
666
667 descriptor = get_section_descriptor (sectp);
668
669 /* If the data was already read for this BFD, just reuse it. */
670 if (descriptor->data != NULL)
671 goto done;
672
673 #ifdef HAVE_MMAP
674 if (!bfd_is_section_compressed (abfd, sectp))
675 {
676 /* The page size, used when mmapping. */
677 static int pagesize;
678
679 if (pagesize == 0)
680 pagesize = getpagesize ();
681
682 /* Only try to mmap sections which are large enough: we don't want
683 to waste space due to fragmentation. */
684
685 if (bfd_get_section_size (sectp) > 4 * pagesize)
686 {
687 descriptor->size = bfd_get_section_size (sectp);
688 descriptor->data = bfd_mmap (abfd, 0, descriptor->size, PROT_READ,
689 MAP_PRIVATE, sectp->filepos,
690 &descriptor->map_addr,
691 &descriptor->map_len);
692
693 if ((caddr_t)descriptor->data != MAP_FAILED)
694 {
695 #if HAVE_POSIX_MADVISE
696 posix_madvise (descriptor->map_addr, descriptor->map_len,
697 POSIX_MADV_WILLNEED);
698 #endif
699 goto done;
700 }
701
702 /* On failure, clear out the section data and try again. */
703 memset (descriptor, 0, sizeof (*descriptor));
704 }
705 }
706 #endif /* HAVE_MMAP */
707
708 /* Handle compressed sections, or ordinary uncompressed sections in
709 the no-mmap case. */
710
711 descriptor->size = bfd_get_section_size (sectp);
712 descriptor->data = NULL;
713
714 data = NULL;
715 if (!bfd_get_full_section_contents (abfd, sectp, &data))
716 error (_("Can't read data for section '%s' in file '%s'"),
717 bfd_get_section_name (abfd, sectp),
718 bfd_get_filename (abfd));
719 descriptor->data = data;
720
721 done:
722 gdb_assert (descriptor->data != NULL);
723 *size = descriptor->size;
724 return (const gdb_byte *) descriptor->data;
725 }
726
727 /* Return 32-bit CRC for ABFD. If successful store it to *FILE_CRC_RETURN and
728 return 1. Otherwise print a warning and return 0. ABFD seek position is
729 not preserved. */
730
731 static int
732 get_file_crc (bfd *abfd, unsigned long *file_crc_return)
733 {
734 unsigned long file_crc = 0;
735
736 if (bfd_seek (abfd, 0, SEEK_SET) != 0)
737 {
738 warning (_("Problem reading \"%s\" for CRC: %s"),
739 bfd_get_filename (abfd), bfd_errmsg (bfd_get_error ()));
740 return 0;
741 }
742
743 for (;;)
744 {
745 gdb_byte buffer[8 * 1024];
746 bfd_size_type count;
747
748 count = bfd_bread (buffer, sizeof (buffer), abfd);
749 if (count == (bfd_size_type) -1)
750 {
751 warning (_("Problem reading \"%s\" for CRC: %s"),
752 bfd_get_filename (abfd), bfd_errmsg (bfd_get_error ()));
753 return 0;
754 }
755 if (count == 0)
756 break;
757 file_crc = bfd_calc_gnu_debuglink_crc32 (file_crc, buffer, count);
758 }
759
760 *file_crc_return = file_crc;
761 return 1;
762 }
763
764 /* See gdb_bfd.h. */
765
766 int
767 gdb_bfd_crc (struct bfd *abfd, unsigned long *crc_out)
768 {
769 struct gdb_bfd_data *gdata = (struct gdb_bfd_data *) bfd_usrdata (abfd);
770
771 if (!gdata->crc_computed)
772 gdata->crc_computed = get_file_crc (abfd, &gdata->crc);
773
774 if (gdata->crc_computed)
775 *crc_out = gdata->crc;
776 return gdata->crc_computed;
777 }
778
779 \f
780
781 /* See gdb_bfd.h. */
782
783 gdb_bfd_ref_ptr
784 gdb_bfd_fopen (const char *filename, const char *target, const char *mode,
785 int fd)
786 {
787 bfd *result = bfd_fopen (filename, target, mode, fd);
788
789 return new_bfd_ref (result);
790 }
791
792 /* See gdb_bfd.h. */
793
794 gdb_bfd_ref_ptr
795 gdb_bfd_openr (const char *filename, const char *target)
796 {
797 bfd *result = bfd_openr (filename, target);
798
799 return new_bfd_ref (result);
800 }
801
802 /* See gdb_bfd.h. */
803
804 gdb_bfd_ref_ptr
805 gdb_bfd_openw (const char *filename, const char *target)
806 {
807 bfd *result = bfd_openw (filename, target);
808
809 return new_bfd_ref (result);
810 }
811
812 /* See gdb_bfd.h. */
813
814 gdb_bfd_ref_ptr
815 gdb_bfd_openr_iovec (const char *filename, const char *target,
816 void *(*open_func) (struct bfd *nbfd,
817 void *open_closure),
818 void *open_closure,
819 file_ptr (*pread_func) (struct bfd *nbfd,
820 void *stream,
821 void *buf,
822 file_ptr nbytes,
823 file_ptr offset),
824 int (*close_func) (struct bfd *nbfd,
825 void *stream),
826 int (*stat_func) (struct bfd *abfd,
827 void *stream,
828 struct stat *sb))
829 {
830 bfd *result = bfd_openr_iovec (filename, target,
831 open_func, open_closure,
832 pread_func, close_func, stat_func);
833
834 return new_bfd_ref (result);
835 }
836
837 /* See gdb_bfd.h. */
838
839 void
840 gdb_bfd_mark_parent (bfd *child, bfd *parent)
841 {
842 struct gdb_bfd_data *gdata;
843
844 gdb_bfd_ref (child);
845 /* No need to stash the filename here, because we also keep a
846 reference on the parent archive. */
847
848 gdata = (struct gdb_bfd_data *) bfd_usrdata (child);
849 if (gdata->archive_bfd == NULL)
850 {
851 gdata->archive_bfd = parent;
852 gdb_bfd_ref (parent);
853 }
854 else
855 gdb_assert (gdata->archive_bfd == parent);
856 }
857
858 /* See gdb_bfd.h. */
859
860 gdb_bfd_ref_ptr
861 gdb_bfd_openr_next_archived_file (bfd *archive, bfd *previous)
862 {
863 bfd *result = bfd_openr_next_archived_file (archive, previous);
864
865 if (result)
866 gdb_bfd_mark_parent (result, archive);
867
868 return gdb_bfd_ref_ptr (result);
869 }
870
871 /* See gdb_bfd.h. */
872
873 void
874 gdb_bfd_record_inclusion (bfd *includer, bfd *includee)
875 {
876 struct gdb_bfd_data *gdata;
877
878 gdb_bfd_ref (includee);
879 gdata = (struct gdb_bfd_data *) bfd_usrdata (includer);
880 VEC_safe_push (bfdp, gdata->included_bfds, includee);
881 }
882
883 /* See gdb_bfd.h. */
884
885 gdb_bfd_ref_ptr
886 gdb_bfd_fdopenr (const char *filename, const char *target, int fd)
887 {
888 bfd *result = bfd_fdopenr (filename, target, fd);
889
890 return new_bfd_ref (result);
891 }
892
893 \f
894
895 gdb_static_assert (ARRAY_SIZE (_bfd_std_section) == 4);
896
897 /* See gdb_bfd.h. */
898
899 int
900 gdb_bfd_section_index (bfd *abfd, asection *section)
901 {
902 if (section == NULL)
903 return -1;
904 else if (section == bfd_com_section_ptr)
905 return bfd_count_sections (abfd);
906 else if (section == bfd_und_section_ptr)
907 return bfd_count_sections (abfd) + 1;
908 else if (section == bfd_abs_section_ptr)
909 return bfd_count_sections (abfd) + 2;
910 else if (section == bfd_ind_section_ptr)
911 return bfd_count_sections (abfd) + 3;
912 return section->index;
913 }
914
915 /* See gdb_bfd.h. */
916
917 int
918 gdb_bfd_count_sections (bfd *abfd)
919 {
920 return bfd_count_sections (abfd) + 4;
921 }
922
923 /* See gdb_bfd.h. */
924
925 int
926 gdb_bfd_requires_relocations (bfd *abfd)
927 {
928 struct gdb_bfd_data *gdata = (struct gdb_bfd_data *) bfd_usrdata (abfd);
929
930 if (gdata->relocation_computed == 0)
931 {
932 asection *sect;
933
934 for (sect = abfd->sections; sect != NULL; sect = sect->next)
935 if ((sect->flags & SEC_RELOC) != 0)
936 {
937 gdata->needs_relocations = 1;
938 break;
939 }
940
941 gdata->relocation_computed = 1;
942 }
943
944 return gdata->needs_relocations;
945 }
946
947 \f
948
949 /* A callback for htab_traverse that prints a single BFD. */
950
951 static int
952 print_one_bfd (void **slot, void *data)
953 {
954 bfd *abfd = (struct bfd *) *slot;
955 struct gdb_bfd_data *gdata = (struct gdb_bfd_data *) bfd_usrdata (abfd);
956 struct ui_out *uiout = (struct ui_out *) data;
957
958 ui_out_emit_tuple tuple_emitter (uiout, NULL);
959 uiout->field_int ("refcount", gdata->refc);
960 uiout->field_string ("addr", host_address_to_string (abfd));
961 uiout->field_string ("filename", bfd_get_filename (abfd));
962 uiout->text ("\n");
963
964 return 1;
965 }
966
967 /* Implement the 'maint info bfd' command. */
968
969 static void
970 maintenance_info_bfds (const char *arg, int from_tty)
971 {
972 struct ui_out *uiout = current_uiout;
973
974 ui_out_emit_table table_emitter (uiout, 3, -1, "bfds");
975 uiout->table_header (10, ui_left, "refcount", "Refcount");
976 uiout->table_header (18, ui_left, "addr", "Address");
977 uiout->table_header (40, ui_left, "filename", "Filename");
978
979 uiout->table_body ();
980 htab_traverse (all_bfds, print_one_bfd, uiout);
981 }
982
983 void
984 _initialize_gdb_bfd (void)
985 {
986 all_bfds = htab_create_alloc (10, htab_hash_pointer, htab_eq_pointer,
987 NULL, xcalloc, xfree);
988
989 add_cmd ("bfds", class_maintenance, maintenance_info_bfds, _("\
990 List the BFDs that are currently open."),
991 &maintenanceinfolist);
992
993 add_setshow_boolean_cmd ("bfd-sharing", no_class,
994 &bfd_sharing, _("\
995 Set whether gdb will share bfds that appear to be the same file."), _("\
996 Show whether gdb will share bfds that appear to be the same file."), _("\
997 When enabled gdb will reuse existing bfds rather than reopening the\n\
998 same file. To decide if two files are the same then gdb compares the\n\
999 filename, file size, file modification time, and file inode."),
1000 NULL,
1001 &show_bfd_sharing,
1002 &maintenance_set_cmdlist,
1003 &maintenance_show_cmdlist);
1004
1005 add_setshow_zuinteger_cmd ("bfd-cache", class_maintenance,
1006 &debug_bfd_cache, _("\
1007 Set bfd cache debugging."), _("\
1008 Show bfd cache debugging."), _("\
1009 When non-zero, bfd cache specific debugging is enabled."),
1010 NULL,
1011 &show_bfd_cache_debug,
1012 &setdebuglist, &showdebuglist);
1013 }