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