gdb/gdb_bfd.c - gdb

Global variables defined

Data types defined

Functions defined

Macros defined

Source code

  1. /* Definitions for BFD wrappers used by GDB.

  2.    Copyright (C) 2011-2015 Free Software Foundation, Inc.

  3.    This file is part of GDB.

  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 3 of the License, or
  7.    (at your option) any later version.

  8.    This program is distributed in the hope that it will be useful,
  9.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  10.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11.    GNU General Public License for more details.

  12.    You should have received a copy of the GNU General Public License
  13.    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */

  14. #include "defs.h"
  15. #include "gdb_bfd.h"
  16. #include "ui-out.h"
  17. #include "gdbcmd.h"
  18. #include "hashtab.h"
  19. #include "filestuff.h"
  20. #include "vec.h"
  21. #ifdef HAVE_ZLIB_H
  22. #include <zlib.h>
  23. #endif
  24. #ifdef HAVE_MMAP
  25. #include <sys/mman.h>
  26. #ifndef MAP_FAILED
  27. #define MAP_FAILED ((void *) -1)
  28. #endif
  29. #endif

  30. typedef bfd *bfdp;
  31. DEF_VEC_P (bfdp);

  32. /* An object of this type is stored in the section's user data when
  33.    mapping a section.  */

  34. struct gdb_bfd_section_data
  35. {
  36.   /* Size of the data.  */
  37.   bfd_size_type size;
  38.   /* If the data was mmapped, this is the length of the map.  */
  39.   bfd_size_type map_len;
  40.   /* The data.  If NULL, the section data has not been read.  */
  41.   void *data;
  42.   /* If the data was mmapped, this is the map address.  */
  43.   void *map_addr;
  44. };

  45. /* A hash table holding every BFD that gdb knows about.  This is not
  46.    to be confused with 'gdb_bfd_cache', which is used for sharing
  47.    BFDs; in contrast, this hash is used just to implement
  48.    "maint info bfd".  */

  49. static htab_t all_bfds;

  50. /* An object of this type is stored in each BFD's user data.  */

  51. struct gdb_bfd_data
  52. {
  53.   /* The reference count.  */
  54.   int refc;

  55.   /* The mtime of the BFD at the point the cache entry was made.  */
  56.   time_t mtime;

  57.   /* This is true if we have determined whether this BFD has any
  58.      sections requiring relocation.  */
  59.   unsigned int relocation_computed : 1;

  60.   /* This is true if any section needs relocation.  */
  61.   unsigned int needs_relocations : 1;

  62.   /* This is true if we have successfully computed the file's CRC.  */
  63.   unsigned int crc_computed : 1;

  64.   /* The file's CRC.  */
  65.   unsigned long crc;

  66.   /* If the BFD comes from an archive, this points to the archive's
  67.      BFD.  Otherwise, this is NULL.  */
  68.   bfd *archive_bfd;

  69.   /* Table of all the bfds this bfd has included.  */
  70.   VEC (bfdp) *included_bfds;

  71.   /* The registry.  */
  72.   REGISTRY_FIELDS;
  73. };

  74. #define GDB_BFD_DATA_ACCESSOR(ABFD) \
  75.   ((struct gdb_bfd_data *) bfd_usrdata (ABFD))

  76. DEFINE_REGISTRY (bfd, GDB_BFD_DATA_ACCESSOR)

  77. /* A hash table storing all the BFDs maintained in the cache.  */

  78. static htab_t gdb_bfd_cache;

  79. /* The type of an object being looked up in gdb_bfd_cache.  We use
  80.    htab's capability of storing one kind of object (BFD in this case)
  81.    and using a different sort of object for searching.  */

  82. struct gdb_bfd_cache_search
  83. {
  84.   /* The filename.  */
  85.   const char *filename;
  86.   /* The mtime.  */
  87.   time_t mtime;
  88. };

  89. /* A hash function for BFDs.  */

  90. static hashval_t
  91. hash_bfd (const void *b)
  92. {
  93.   const bfd *abfd = b;

  94.   /* It is simplest to just hash the filename.  */
  95.   return htab_hash_string (bfd_get_filename (abfd));
  96. }

  97. /* An equality function for BFDs.  Note that this expects the caller
  98.    to search using struct gdb_bfd_cache_search only, not BFDs.  */

  99. static int
  100. eq_bfd (const void *a, const void *b)
  101. {
  102.   const bfd *abfd = a;
  103.   const struct gdb_bfd_cache_search *s = b;
  104.   struct gdb_bfd_data *gdata = bfd_usrdata (abfd);

  105.   return (gdata->mtime == s->mtime
  106.           && strcmp (bfd_get_filename (abfd), s->filename) == 0);
  107. }

  108. /* See gdb_bfd.h.  */

  109. struct bfd *
  110. gdb_bfd_open (const char *name, const char *target, int fd)
  111. {
  112.   hashval_t hash;
  113.   void **slot;
  114.   bfd *abfd;
  115.   struct gdb_bfd_cache_search search;
  116.   struct stat st;

  117.   if (gdb_bfd_cache == NULL)
  118.     gdb_bfd_cache = htab_create_alloc (1, hash_bfd, eq_bfd, NULL,
  119.                                        xcalloc, xfree);

  120.   if (fd == -1)
  121.     {
  122.       fd = gdb_open_cloexec (name, O_RDONLY | O_BINARY, 0);
  123.       if (fd == -1)
  124.         {
  125.           bfd_set_error (bfd_error_system_call);
  126.           return NULL;
  127.         }
  128.     }

  129.   search.filename = name;
  130.   if (fstat (fd, &st) < 0)
  131.     {
  132.       /* Weird situation here.  */
  133.       search.mtime = 0;
  134.     }
  135.   else
  136.     search.mtime = st.st_mtime;

  137.   /* Note that this must compute the same result as hash_bfd.  */
  138.   hash = htab_hash_string (name);
  139.   /* Note that we cannot use htab_find_slot_with_hash here, because
  140.      opening the BFD may fail; and this would violate hashtab
  141.      invariants.  */
  142.   abfd = htab_find_with_hash (gdb_bfd_cache, &search, hash);
  143.   if (abfd != NULL)
  144.     {
  145.       close (fd);
  146.       gdb_bfd_ref (abfd);
  147.       return abfd;
  148.     }

  149.   abfd = bfd_fopen (name, target, FOPEN_RB, fd);
  150.   if (abfd == NULL)
  151.     return NULL;

  152.   slot = htab_find_slot_with_hash (gdb_bfd_cache, &search, hash, INSERT);
  153.   gdb_assert (!*slot);
  154.   *slot = abfd;

  155.   gdb_bfd_ref (abfd);
  156.   return abfd;
  157. }

  158. /* A helper function that releases any section data attached to the
  159.    BFD.  */

  160. static void
  161. free_one_bfd_section (bfd *abfd, asection *sectp, void *ignore)
  162. {
  163.   struct gdb_bfd_section_data *sect = bfd_get_section_userdata (abfd, sectp);

  164.   if (sect != NULL && sect->data != NULL)
  165.     {
  166. #ifdef HAVE_MMAP
  167.       if (sect->map_addr != NULL)
  168.         {
  169.           int res;

  170.           res = munmap (sect->map_addr, sect->map_len);
  171.           gdb_assert (res == 0);
  172.         }
  173.       else
  174. #endif
  175.         xfree (sect->data);
  176.     }
  177. }

  178. /* Close ABFD, and warn if that fails.  */

  179. static int
  180. gdb_bfd_close_or_warn (struct bfd *abfd)
  181. {
  182.   int ret;
  183.   char *name = bfd_get_filename (abfd);

  184.   bfd_map_over_sections (abfd, free_one_bfd_section, NULL);

  185.   ret = bfd_close (abfd);

  186.   if (!ret)
  187.     warning (_("cannot close \"%s\": %s"),
  188.              name, bfd_errmsg (bfd_get_error ()));

  189.   return ret;
  190. }

  191. /* See gdb_bfd.h.  */

  192. void
  193. gdb_bfd_ref (struct bfd *abfd)
  194. {
  195.   struct gdb_bfd_data *gdata;
  196.   void **slot;

  197.   if (abfd == NULL)
  198.     return;

  199.   gdata = bfd_usrdata (abfd);

  200.   if (gdata != NULL)
  201.     {
  202.       gdata->refc += 1;
  203.       return;
  204.     }

  205.   /* Ask BFD to decompress sections in bfd_get_full_section_contents.  */
  206.   abfd->flags |= BFD_DECOMPRESS;

  207.   gdata = bfd_zalloc (abfd, sizeof (struct gdb_bfd_data));
  208.   gdata->refc = 1;
  209.   gdata->mtime = bfd_get_mtime (abfd);
  210.   gdata->archive_bfd = NULL;
  211.   bfd_usrdata (abfd) = gdata;

  212.   bfd_alloc_data (abfd);

  213.   /* This is the first we've seen it, so add it to the hash table.  */
  214.   slot = htab_find_slot (all_bfds, abfd, INSERT);
  215.   gdb_assert (slot && !*slot);
  216.   *slot = abfd;
  217. }

  218. /* See gdb_bfd.h.  */

  219. void
  220. gdb_bfd_unref (struct bfd *abfd)
  221. {
  222.   int ix;
  223.   struct gdb_bfd_data *gdata;
  224.   struct gdb_bfd_cache_search search;
  225.   bfd *archive_bfd, *included_bfd;

  226.   if (abfd == NULL)
  227.     return;

  228.   gdata = bfd_usrdata (abfd);
  229.   gdb_assert (gdata->refc >= 1);

  230.   gdata->refc -= 1;
  231.   if (gdata->refc > 0)
  232.     return;

  233.   archive_bfd = gdata->archive_bfd;
  234.   search.filename = bfd_get_filename (abfd);

  235.   if (gdb_bfd_cache && search.filename)
  236.     {
  237.       hashval_t hash = htab_hash_string (search.filename);
  238.       void **slot;

  239.       search.mtime = gdata->mtime;
  240.       slot = htab_find_slot_with_hash (gdb_bfd_cache, &search, hash,
  241.                                        NO_INSERT);

  242.       if (slot && *slot)
  243.         htab_clear_slot (gdb_bfd_cache, slot);
  244.     }

  245.   for (ix = 0;
  246.        VEC_iterate (bfdp, gdata->included_bfds, ix, included_bfd);
  247.        ++ix)
  248.     gdb_bfd_unref (included_bfd);
  249.   VEC_free (bfdp, gdata->included_bfds);

  250.   bfd_free_data (abfd);
  251.   bfd_usrdata (abfd) = NULL/* Paranoia.  */

  252.   htab_remove_elt (all_bfds, abfd);

  253.   gdb_bfd_close_or_warn (abfd);

  254.   gdb_bfd_unref (archive_bfd);
  255. }

  256. /* A helper function that returns the section data descriptor
  257.    associated with SECTION.  If no such descriptor exists, a new one
  258.    is allocated and cleared.  */

  259. static struct gdb_bfd_section_data *
  260. get_section_descriptor (asection *section)
  261. {
  262.   struct gdb_bfd_section_data *result;

  263.   result = bfd_get_section_userdata (section->owner, section);

  264.   if (result == NULL)
  265.     {
  266.       result = bfd_zalloc (section->owner, sizeof (*result));
  267.       bfd_set_section_userdata (section->owner, section, result);
  268.     }

  269.   return result;
  270. }

  271. /* See gdb_bfd.h.  */

  272. const gdb_byte *
  273. gdb_bfd_map_section (asection *sectp, bfd_size_type *size)
  274. {
  275.   bfd *abfd;
  276.   struct gdb_bfd_section_data *descriptor;
  277.   bfd_byte *data;

  278.   gdb_assert ((sectp->flags & SEC_RELOC) == 0);
  279.   gdb_assert (size != NULL);

  280.   abfd = sectp->owner;

  281.   descriptor = get_section_descriptor (sectp);

  282.   /* If the data was already read for this BFD, just reuse it.  */
  283.   if (descriptor->data != NULL)
  284.     goto done;

  285. #ifdef HAVE_MMAP
  286.   if (!bfd_is_section_compressed (abfd, sectp))
  287.     {
  288.       /* The page size, used when mmapping.  */
  289.       static int pagesize;

  290.       if (pagesize == 0)
  291.         pagesize = getpagesize ();

  292.       /* Only try to mmap sections which are large enough: we don't want
  293.          to waste space due to fragmentation.  */

  294.       if (bfd_get_section_size (sectp) > 4 * pagesize)
  295.         {
  296.           descriptor->size = bfd_get_section_size (sectp);
  297.           descriptor->data = bfd_mmap (abfd, 0, descriptor->size, PROT_READ,
  298.                                        MAP_PRIVATE, sectp->filepos,
  299.                                        &descriptor->map_addr,
  300.                                        &descriptor->map_len);

  301.           if ((caddr_t)descriptor->data != MAP_FAILED)
  302.             {
  303. #if HAVE_POSIX_MADVISE
  304.               posix_madvise (descriptor->map_addr, descriptor->map_len,
  305.                              POSIX_MADV_WILLNEED);
  306. #endif
  307.               goto done;
  308.             }

  309.           /* On failure, clear out the section data and try again.  */
  310.           memset (descriptor, 0, sizeof (*descriptor));
  311.         }
  312.     }
  313. #endif /* HAVE_MMAP */

  314.   /* Handle compressed sections, or ordinary uncompressed sections in
  315.      the no-mmap case.  */

  316.   descriptor->size = bfd_get_section_size (sectp);
  317.   descriptor->data = NULL;

  318.   data = NULL;
  319.   if (!bfd_get_full_section_contents (abfd, sectp, &data))
  320.     error (_("Can't read data for section '%s' in file '%s'"),
  321.            bfd_get_section_name (abfd, sectp),
  322.            bfd_get_filename (abfd));
  323.   descriptor->data = data;

  324. done:
  325.   gdb_assert (descriptor->data != NULL);
  326.   *size = descriptor->size;
  327.   return descriptor->data;
  328. }

  329. /* Return 32-bit CRC for ABFD.  If successful store it to *FILE_CRC_RETURN and
  330.    return 1.  Otherwise print a warning and return 0.  ABFD seek position is
  331.    not preserved.  */

  332. static int
  333. get_file_crc (bfd *abfd, unsigned long *file_crc_return)
  334. {
  335.   unsigned long file_crc = 0;

  336.   if (bfd_seek (abfd, 0, SEEK_SET) != 0)
  337.     {
  338.       warning (_("Problem reading \"%s\" for CRC: %s"),
  339.                bfd_get_filename (abfd), bfd_errmsg (bfd_get_error ()));
  340.       return 0;
  341.     }

  342.   for (;;)
  343.     {
  344.       gdb_byte buffer[8 * 1024];
  345.       bfd_size_type count;

  346.       count = bfd_bread (buffer, sizeof (buffer), abfd);
  347.       if (count == (bfd_size_type) -1)
  348.         {
  349.           warning (_("Problem reading \"%s\" for CRC: %s"),
  350.                    bfd_get_filename (abfd), bfd_errmsg (bfd_get_error ()));
  351.           return 0;
  352.         }
  353.       if (count == 0)
  354.         break;
  355.       file_crc = bfd_calc_gnu_debuglink_crc32 (file_crc, buffer, count);
  356.     }

  357.   *file_crc_return = file_crc;
  358.   return 1;
  359. }

  360. /* See gdb_bfd.h.  */

  361. int
  362. gdb_bfd_crc (struct bfd *abfd, unsigned long *crc_out)
  363. {
  364.   struct gdb_bfd_data *gdata = bfd_usrdata (abfd);

  365.   if (!gdata->crc_computed)
  366.     gdata->crc_computed = get_file_crc (abfd, &gdata->crc);

  367.   if (gdata->crc_computed)
  368.     *crc_out = gdata->crc;
  369.   return gdata->crc_computed;
  370. }



  371. /* See gdb_bfd.h.  */

  372. bfd *
  373. gdb_bfd_fopen (const char *filename, const char *target, const char *mode,
  374.                int fd)
  375. {
  376.   bfd *result = bfd_fopen (filename, target, mode, fd);

  377.   if (result)
  378.     gdb_bfd_ref (result);

  379.   return result;
  380. }

  381. /* See gdb_bfd.h.  */

  382. bfd *
  383. gdb_bfd_openr (const char *filename, const char *target)
  384. {
  385.   bfd *result = bfd_openr (filename, target);

  386.   if (result)
  387.     gdb_bfd_ref (result);

  388.   return result;
  389. }

  390. /* See gdb_bfd.h.  */

  391. bfd *
  392. gdb_bfd_openw (const char *filename, const char *target)
  393. {
  394.   bfd *result = bfd_openw (filename, target);

  395.   if (result)
  396.     gdb_bfd_ref (result);

  397.   return result;
  398. }

  399. /* See gdb_bfd.h.  */

  400. bfd *
  401. gdb_bfd_openr_iovec (const char *filename, const char *target,
  402.                      void *(*open_func) (struct bfd *nbfd,
  403.                                          void *open_closure),
  404.                      void *open_closure,
  405.                      file_ptr (*pread_func) (struct bfd *nbfd,
  406.                                              void *stream,
  407.                                              void *buf,
  408.                                              file_ptr nbytes,
  409.                                              file_ptr offset),
  410.                      int (*close_func) (struct bfd *nbfd,
  411.                                         void *stream),
  412.                      int (*stat_func) (struct bfd *abfd,
  413.                                        void *stream,
  414.                                        struct stat *sb))
  415. {
  416.   bfd *result = bfd_openr_iovec (filename, target,
  417.                                  open_func, open_closure,
  418.                                  pread_func, close_func, stat_func);

  419.   if (result)
  420.     gdb_bfd_ref (result);

  421.   return result;
  422. }

  423. /* See gdb_bfd.h.  */

  424. void
  425. gdb_bfd_mark_parent (bfd *child, bfd *parent)
  426. {
  427.   struct gdb_bfd_data *gdata;

  428.   gdb_bfd_ref (child);
  429.   /* No need to stash the filename here, because we also keep a
  430.      reference on the parent archive.  */

  431.   gdata = bfd_usrdata (child);
  432.   if (gdata->archive_bfd == NULL)
  433.     {
  434.       gdata->archive_bfd = parent;
  435.       gdb_bfd_ref (parent);
  436.     }
  437.   else
  438.     gdb_assert (gdata->archive_bfd == parent);
  439. }

  440. /* See gdb_bfd.h.  */

  441. bfd *
  442. gdb_bfd_openr_next_archived_file (bfd *archive, bfd *previous)
  443. {
  444.   bfd *result = bfd_openr_next_archived_file (archive, previous);

  445.   if (result)
  446.     gdb_bfd_mark_parent (result, archive);

  447.   return result;
  448. }

  449. /* See gdb_bfd.h.  */

  450. void
  451. gdb_bfd_record_inclusion (bfd *includer, bfd *includee)
  452. {
  453.   struct gdb_bfd_data *gdata;

  454.   gdb_bfd_ref (includee);
  455.   gdata = bfd_usrdata (includer);
  456.   VEC_safe_push (bfdp, gdata->included_bfds, includee);
  457. }

  458. /* See gdb_bfd.h.  */

  459. bfd *
  460. gdb_bfd_fdopenr (const char *filename, const char *target, int fd)
  461. {
  462.   bfd *result = bfd_fdopenr (filename, target, fd);

  463.   if (result)
  464.     gdb_bfd_ref (result);

  465.   return result;
  466. }



  467. gdb_static_assert (ARRAY_SIZE (_bfd_std_section) == 4);

  468. /* See gdb_bfd.h.  */

  469. int
  470. gdb_bfd_section_index (bfd *abfd, asection *section)
  471. {
  472.   if (section == NULL)
  473.     return -1;
  474.   else if (section == bfd_com_section_ptr)
  475.     return bfd_count_sections (abfd) + 1;
  476.   else if (section == bfd_und_section_ptr)
  477.     return bfd_count_sections (abfd) + 2;
  478.   else if (section == bfd_abs_section_ptr)
  479.     return bfd_count_sections (abfd) + 3;
  480.   else if (section == bfd_ind_section_ptr)
  481.     return bfd_count_sections (abfd) + 4;
  482.   return section->index;
  483. }

  484. /* See gdb_bfd.h.  */

  485. int
  486. gdb_bfd_count_sections (bfd *abfd)
  487. {
  488.   return bfd_count_sections (abfd) + 4;
  489. }

  490. /* See gdb_bfd.h.  */

  491. int
  492. gdb_bfd_requires_relocations (bfd *abfd)
  493. {
  494.   struct gdb_bfd_data *gdata = bfd_usrdata (abfd);

  495.   if (gdata->relocation_computed == 0)
  496.     {
  497.       asection *sect;

  498.       for (sect = abfd->sections; sect != NULL; sect = sect->next)
  499.         if ((sect->flags & SEC_RELOC) != 0)
  500.           {
  501.             gdata->needs_relocations = 1;
  502.             break;
  503.           }

  504.       gdata->relocation_computed = 1;
  505.     }

  506.   return gdata->needs_relocations;
  507. }



  508. /* A callback for htab_traverse that prints a single BFD.  */

  509. static int
  510. print_one_bfd (void **slot, void *data)
  511. {
  512.   bfd *abfd = *slot;
  513.   struct gdb_bfd_data *gdata = bfd_usrdata (abfd);
  514.   struct ui_out *uiout = data;
  515.   struct cleanup *inner;

  516.   inner = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
  517.   ui_out_field_int (uiout, "refcount", gdata->refc);
  518.   ui_out_field_string (uiout, "addr", host_address_to_string (abfd));
  519.   ui_out_field_string (uiout, "filename", bfd_get_filename (abfd));
  520.   ui_out_text (uiout, "\n");
  521.   do_cleanups (inner);

  522.   return 1;
  523. }

  524. /* Implement the 'maint info bfd' command.  */

  525. static void
  526. maintenance_info_bfds (char *arg, int from_tty)
  527. {
  528.   struct cleanup *cleanup;
  529.   struct ui_out *uiout = current_uiout;

  530.   cleanup = make_cleanup_ui_out_table_begin_end (uiout, 3, -1, "bfds");
  531.   ui_out_table_header (uiout, 10, ui_left, "refcount", "Refcount");
  532.   ui_out_table_header (uiout, 18, ui_left, "addr", "Address");
  533.   ui_out_table_header (uiout, 40, ui_left, "filename", "Filename");

  534.   ui_out_table_body (uiout);
  535.   htab_traverse (all_bfds, print_one_bfd, uiout);

  536.   do_cleanups (cleanup);
  537. }

  538. /* -Wmissing-prototypes */
  539. extern initialize_file_ftype _initialize_gdb_bfd;

  540. void
  541. _initialize_gdb_bfd (void)
  542. {
  543.   all_bfds = htab_create_alloc (10, htab_hash_pointer, htab_eq_pointer,
  544.                                 NULL, xcalloc, xfree);

  545.   add_cmd ("bfds", class_maintenance, maintenance_info_bfds, _("\
  546. List the BFDs that are currently open."),
  547.            &maintenanceinfolist);
  548. }