gdb/solib-target.c - gdb

Global variables defined

Data types defined

Functions defined

Source code

  1. /* Definitions for targets which report shared library events.

  2.    Copyright (C) 2007-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 "objfiles.h"
  16. #include "solist.h"
  17. #include "symtab.h"
  18. #include "symfile.h"
  19. #include "target.h"
  20. #include "vec.h"
  21. #include "solib-target.h"

  22. /* Private data for each loaded library.  */
  23. struct lm_info
  24. {
  25.   /* The library's name.  The name is normally kept in the struct
  26.      so_list; it is only here during XML parsing.  */
  27.   char *name;

  28.   /* The target can either specify segment bases or section bases, not
  29.      both.  */

  30.   /* The base addresses for each independently relocatable segment of
  31.      this shared library.  */
  32.   VEC(CORE_ADDR) *segment_bases;

  33.   /* The base addresses for each independently allocatable,
  34.      relocatable section of this shared library.  */
  35.   VEC(CORE_ADDR) *section_bases;

  36.   /* The cached offsets for each section of this shared library,
  37.      determined from SEGMENT_BASES, or SECTION_BASES.  */
  38.   struct section_offsets *offsets;
  39. };

  40. typedef struct lm_info *lm_info_p;
  41. DEF_VEC_P(lm_info_p);

  42. #if !defined(HAVE_LIBEXPAT)

  43. static VEC(lm_info_p) *
  44. solib_target_parse_libraries (const char *library)
  45. {
  46.   static int have_warned;

  47.   if (!have_warned)
  48.     {
  49.       have_warned = 1;
  50.       warning (_("Can not parse XML library list; XML support was disabled "
  51.                  "at compile time"));
  52.     }

  53.   return NULL;
  54. }

  55. #else /* HAVE_LIBEXPAT */

  56. #include "xml-support.h"

  57. /* Handle the start of a <segment> element.  */

  58. static void
  59. library_list_start_segment (struct gdb_xml_parser *parser,
  60.                             const struct gdb_xml_element *element,
  61.                             void *user_data, VEC(gdb_xml_value_s) *attributes)
  62. {
  63.   VEC(lm_info_p) **list = user_data;
  64.   struct lm_info *last = VEC_last (lm_info_p, *list);
  65.   ULONGEST *address_p = xml_find_attribute (attributes, "address")->value;
  66.   CORE_ADDR address = (CORE_ADDR) *address_p;

  67.   if (last->section_bases != NULL)
  68.     gdb_xml_error (parser,
  69.                    _("Library list with both segments and sections"));

  70.   VEC_safe_push (CORE_ADDR, last->segment_bases, address);
  71. }

  72. static void
  73. library_list_start_section (struct gdb_xml_parser *parser,
  74.                             const struct gdb_xml_element *element,
  75.                             void *user_data, VEC(gdb_xml_value_s) *attributes)
  76. {
  77.   VEC(lm_info_p) **list = user_data;
  78.   struct lm_info *last = VEC_last (lm_info_p, *list);
  79.   ULONGEST *address_p = xml_find_attribute (attributes, "address")->value;
  80.   CORE_ADDR address = (CORE_ADDR) *address_p;

  81.   if (last->segment_bases != NULL)
  82.     gdb_xml_error (parser,
  83.                    _("Library list with both segments and sections"));

  84.   VEC_safe_push (CORE_ADDR, last->section_bases, address);
  85. }

  86. /* Handle the start of a <library> element.  */

  87. static void
  88. library_list_start_library (struct gdb_xml_parser *parser,
  89.                             const struct gdb_xml_element *element,
  90.                             void *user_data, VEC(gdb_xml_value_s) *attributes)
  91. {
  92.   VEC(lm_info_p) **list = user_data;
  93.   struct lm_info *item = XCNEW (struct lm_info);
  94.   const char *name = xml_find_attribute (attributes, "name")->value;

  95.   item->name = xstrdup (name);
  96.   VEC_safe_push (lm_info_p, *list, item);
  97. }

  98. static void
  99. library_list_end_library (struct gdb_xml_parser *parser,
  100.                           const struct gdb_xml_element *element,
  101.                           void *user_data, const char *body_text)
  102. {
  103.   VEC(lm_info_p) **list = user_data;
  104.   struct lm_info *lm_info = VEC_last (lm_info_p, *list);

  105.   if (lm_info->segment_bases == NULL
  106.       && lm_info->section_bases == NULL)
  107.     gdb_xml_error (parser,
  108.                    _("No segment or section bases defined"));
  109. }


  110. /* Handle the start of a <library-list> element.  */

  111. static void
  112. library_list_start_list (struct gdb_xml_parser *parser,
  113.                          const struct gdb_xml_element *element,
  114.                          void *user_data, VEC(gdb_xml_value_s) *attributes)
  115. {
  116.   char *version = xml_find_attribute (attributes, "version")->value;

  117.   if (strcmp (version, "1.0") != 0)
  118.     gdb_xml_error (parser,
  119.                    _("Library list has unsupported version \"%s\""),
  120.                    version);
  121. }

  122. /* Discard the constructed library list.  */

  123. static void
  124. solib_target_free_library_list (void *p)
  125. {
  126.   VEC(lm_info_p) **result = p;
  127.   struct lm_info *info;
  128.   int ix;

  129.   for (ix = 0; VEC_iterate (lm_info_p, *result, ix, info); ix++)
  130.     {
  131.       xfree (info->name);
  132.       VEC_free (CORE_ADDR, info->segment_bases);
  133.       VEC_free (CORE_ADDR, info->section_bases);
  134.       xfree (info);
  135.     }
  136.   VEC_free (lm_info_p, *result);
  137.   *result = NULL;
  138. }

  139. /* The allowed elements and attributes for an XML library list.
  140.    The root element is a <library-list>.  */

  141. static const struct gdb_xml_attribute segment_attributes[] = {
  142.   { "address", GDB_XML_AF_NONE, gdb_xml_parse_attr_ulongest, NULL },
  143.   { NULL, GDB_XML_AF_NONE, NULL, NULL }
  144. };

  145. static const struct gdb_xml_attribute section_attributes[] = {
  146.   { "address", GDB_XML_AF_NONE, gdb_xml_parse_attr_ulongest, NULL },
  147.   { NULL, GDB_XML_AF_NONE, NULL, NULL }
  148. };

  149. static const struct gdb_xml_element library_children[] = {
  150.   { "segment", segment_attributes, NULL,
  151.     GDB_XML_EF_REPEATABLE | GDB_XML_EF_OPTIONAL,
  152.     library_list_start_segment, NULL },
  153.   { "section", section_attributes, NULL,
  154.     GDB_XML_EF_REPEATABLE | GDB_XML_EF_OPTIONAL,
  155.     library_list_start_section, NULL },
  156.   { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
  157. };

  158. static const struct gdb_xml_attribute library_attributes[] = {
  159.   { "name", GDB_XML_AF_NONE, NULL, NULL },
  160.   { NULL, GDB_XML_AF_NONE, NULL, NULL }
  161. };

  162. static const struct gdb_xml_element library_list_children[] = {
  163.   { "library", library_attributes, library_children,
  164.     GDB_XML_EF_REPEATABLE | GDB_XML_EF_OPTIONAL,
  165.     library_list_start_library, library_list_end_library },
  166.   { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
  167. };

  168. static const struct gdb_xml_attribute library_list_attributes[] = {
  169.   { "version", GDB_XML_AF_NONE, NULL, NULL },
  170.   { NULL, GDB_XML_AF_NONE, NULL, NULL }
  171. };

  172. static const struct gdb_xml_element library_list_elements[] = {
  173.   { "library-list", library_list_attributes, library_list_children,
  174.     GDB_XML_EF_NONE, library_list_start_list, NULL },
  175.   { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
  176. };

  177. static VEC(lm_info_p) *
  178. solib_target_parse_libraries (const char *library)
  179. {
  180.   VEC(lm_info_p) *result = NULL;
  181.   struct cleanup *back_to = make_cleanup (solib_target_free_library_list,
  182.                                           &result);

  183.   if (gdb_xml_parse_quick (_("target library list"), "library-list.dtd",
  184.                            library_list_elements, library, &result) == 0)
  185.     {
  186.       /* Parsed successfully, keep the result.  */
  187.       discard_cleanups (back_to);
  188.       return result;
  189.     }

  190.   do_cleanups (back_to);
  191.   return NULL;
  192. }
  193. #endif

  194. static struct so_list *
  195. solib_target_current_sos (void)
  196. {
  197.   struct so_list *new_solib, *start = NULL, *last = NULL;
  198.   char *library_document;
  199.   struct cleanup *old_chain;
  200.   VEC(lm_info_p) *library_list;
  201.   struct lm_info *info;
  202.   int ix;

  203.   /* Fetch the list of shared libraries.  */
  204.   library_document = target_read_stralloc (&current_target,
  205.                                            TARGET_OBJECT_LIBRARIES,
  206.                                            NULL);
  207.   if (library_document == NULL)
  208.     return NULL;

  209.   /* solib_target_parse_libraries may throw, so we use a cleanup.  */
  210.   old_chain = make_cleanup (xfree, library_document);

  211.   /* Parse the list.  */
  212.   library_list = solib_target_parse_libraries (library_document);

  213.   /* library_document string is not needed behind this point.  */
  214.   do_cleanups (old_chain);

  215.   if (library_list == NULL)
  216.     return NULL;

  217.   /* Build a struct so_list for each entry on the list.  */
  218.   for (ix = 0; VEC_iterate (lm_info_p, library_list, ix, info); ix++)
  219.     {
  220.       new_solib = XCNEW (struct so_list);
  221.       strncpy (new_solib->so_name, info->name, SO_NAME_MAX_PATH_SIZE - 1);
  222.       new_solib->so_name[SO_NAME_MAX_PATH_SIZE - 1] = '\0';
  223.       strncpy (new_solib->so_original_name, info->name,
  224.                SO_NAME_MAX_PATH_SIZE - 1);
  225.       new_solib->so_original_name[SO_NAME_MAX_PATH_SIZE - 1] = '\0';
  226.       new_solib->lm_info = info;

  227.       /* We no longer need this copy of the name.  */
  228.       xfree (info->name);
  229.       info->name = NULL;

  230.       /* Add it to the list.  */
  231.       if (!start)
  232.         last = start = new_solib;
  233.       else
  234.         {
  235.           last->next = new_solib;
  236.           last = new_solib;
  237.         }
  238.     }

  239.   /* Free the library list, but not its members.  */
  240.   VEC_free (lm_info_p, library_list);

  241.   return start;
  242. }

  243. static void
  244. solib_target_special_symbol_handling (void)
  245. {
  246.   /* Nothing needed.  */
  247. }

  248. static void
  249. solib_target_solib_create_inferior_hook (int from_tty)
  250. {
  251.   /* Nothing needed.  */
  252. }

  253. static void
  254. solib_target_clear_solib (void)
  255. {
  256.   /* Nothing needed.  */
  257. }

  258. static void
  259. solib_target_free_so (struct so_list *so)
  260. {
  261.   gdb_assert (so->lm_info->name == NULL);
  262.   xfree (so->lm_info->offsets);
  263.   VEC_free (CORE_ADDR, so->lm_info->segment_bases);
  264.   xfree (so->lm_info);
  265. }

  266. static void
  267. solib_target_relocate_section_addresses (struct so_list *so,
  268.                                          struct target_section *sec)
  269. {
  270.   CORE_ADDR offset;

  271.   /* Build the offset table only once per object file.  We can not do
  272.      it any earlier, since we need to open the file first.  */
  273.   if (so->lm_info->offsets == NULL)
  274.     {
  275.       int num_sections = gdb_bfd_count_sections (so->abfd);

  276.       so->lm_info->offsets = xzalloc (SIZEOF_N_SECTION_OFFSETS (num_sections));

  277.       if (so->lm_info->section_bases)
  278.         {
  279.           int i;
  280.           asection *sect;
  281.           int num_section_bases
  282.             = VEC_length (CORE_ADDR, so->lm_info->section_bases);
  283.           int num_alloc_sections = 0;

  284.           for (i = 0, sect = so->abfd->sections;
  285.                sect != NULL;
  286.                i++, sect = sect->next)
  287.             if ((bfd_get_section_flags (so->abfd, sect) & SEC_ALLOC))
  288.               num_alloc_sections++;

  289.           if (num_alloc_sections != num_section_bases)
  290.             warning (_("\
  291. Could not relocate shared library \"%s\": wrong number of ALLOC sections"),
  292.                      so->so_name);
  293.           else
  294.             {
  295.               int bases_index = 0;
  296.               int found_range = 0;
  297.               CORE_ADDR *section_bases;

  298.               section_bases = VEC_address (CORE_ADDR,
  299.                                            so->lm_info->section_bases);

  300.               so->addr_low = ~(CORE_ADDR) 0;
  301.               so->addr_high = 0;
  302.               for (i = 0, sect = so->abfd->sections;
  303.                    sect != NULL;
  304.                    i++, sect = sect->next)
  305.                 {
  306.                   if (!(bfd_get_section_flags (so->abfd, sect) & SEC_ALLOC))
  307.                     continue;
  308.                   if (bfd_section_size (so->abfd, sect) > 0)
  309.                     {
  310.                       CORE_ADDR low, high;

  311.                       low = section_bases[i];
  312.                       high = low + bfd_section_size (so->abfd, sect) - 1;

  313.                       if (low < so->addr_low)
  314.                         so->addr_low = low;
  315.                       if (high > so->addr_high)
  316.                         so->addr_high = high;
  317.                       gdb_assert (so->addr_low <= so->addr_high);
  318.                       found_range = 1;
  319.                     }
  320.                   so->lm_info->offsets->offsets[i]
  321.                     = section_bases[bases_index];
  322.                   bases_index++;
  323.                 }
  324.               if (!found_range)
  325.                 so->addr_low = so->addr_high = 0;
  326.               gdb_assert (so->addr_low <= so->addr_high);
  327.             }
  328.         }
  329.       else if (so->lm_info->segment_bases)
  330.         {
  331.           struct symfile_segment_data *data;

  332.           data = get_symfile_segment_data (so->abfd);
  333.           if (data == NULL)
  334.             warning (_("\
  335. Could not relocate shared library \"%s\": no segments"), so->so_name);
  336.           else
  337.             {
  338.               ULONGEST orig_delta;
  339.               int i;
  340.               int num_bases;
  341.               CORE_ADDR *segment_bases;

  342.               num_bases = VEC_length (CORE_ADDR, so->lm_info->segment_bases);
  343.               segment_bases = VEC_address (CORE_ADDR,
  344.                                            so->lm_info->segment_bases);

  345.               if (!symfile_map_offsets_to_segments (so->abfd, data,
  346.                                                     so->lm_info->offsets,
  347.                                                     num_bases, segment_bases))
  348.                 warning (_("\
  349. Could not relocate shared library \"%s\": bad offsets"), so->so_name);

  350.               /* Find the range of addresses to report for this library in
  351.                  "info sharedlibrary".  Report any consecutive segments
  352.                  which were relocated as a single unit.  */
  353.               gdb_assert (num_bases > 0);
  354.               orig_delta = segment_bases[0] - data->segment_bases[0];

  355.               for (i = 1; i < data->num_segments; i++)
  356.                 {
  357.                   /* If we have run out of offsets, assume all
  358.                      remaining segments have the same offset.  */
  359.                   if (i >= num_bases)
  360.                     continue;

  361.                   /* If this segment does not have the same offset, do
  362.                      not include it in the library's range.  */
  363.                   if (segment_bases[i] - data->segment_bases[i] != orig_delta)
  364.                     break;
  365.                 }

  366.               so->addr_low = segment_bases[0];
  367.               so->addr_high = (data->segment_bases[i - 1]
  368.                                + data->segment_sizes[i - 1]
  369.                                + orig_delta);
  370.               gdb_assert (so->addr_low <= so->addr_high);

  371.               free_symfile_segment_data (data);
  372.             }
  373.         }
  374.     }

  375.   offset = so->lm_info->offsets->offsets[gdb_bfd_section_index
  376.                                          (sec->the_bfd_section->owner,
  377.                                           sec->the_bfd_section)];
  378.   sec->addr += offset;
  379.   sec->endaddr += offset;
  380. }

  381. static int
  382. solib_target_open_symbol_file_object (void *from_ttyp)
  383. {
  384.   /* We can't locate the main symbol file based on the target's
  385.      knowledge; the user has to specify it.  */
  386.   return 0;
  387. }

  388. static int
  389. solib_target_in_dynsym_resolve_code (CORE_ADDR pc)
  390. {
  391.   /* We don't have a range of addresses for the dynamic linker; there
  392.      may not be one in the program's address space.  So only report
  393.      PLT entries (which may be import stubs).  */
  394.   return in_plt_section (pc);
  395. }

  396. struct target_so_ops solib_target_so_ops;

  397. /* -Wmissing-prototypes */
  398. extern initialize_file_ftype _initialize_solib_target;

  399. void
  400. _initialize_solib_target (void)
  401. {
  402.   solib_target_so_ops.relocate_section_addresses
  403.     = solib_target_relocate_section_addresses;
  404.   solib_target_so_ops.free_so = solib_target_free_so;
  405.   solib_target_so_ops.clear_solib = solib_target_clear_solib;
  406.   solib_target_so_ops.solib_create_inferior_hook
  407.     = solib_target_solib_create_inferior_hook;
  408.   solib_target_so_ops.special_symbol_handling
  409.     = solib_target_special_symbol_handling;
  410.   solib_target_so_ops.current_sos = solib_target_current_sos;
  411.   solib_target_so_ops.open_symbol_file_object
  412.     = solib_target_open_symbol_file_object;
  413.   solib_target_so_ops.in_dynsym_resolve_code
  414.     = solib_target_in_dynsym_resolve_code;
  415.   solib_target_so_ops.bfd_open = solib_bfd_open;

  416.   /* Set current_target_so_ops to solib_target_so_ops if not already
  417.      set.  */
  418.   if (current_target_so_ops == 0)
  419.     current_target_so_ops = &solib_target_so_ops;
  420. }