GDB (API)
/home/stan/gdb/src/gdb/solib-target.c
Go to the documentation of this file.
00001 /* Definitions for targets which report shared library events.
00002 
00003    Copyright (C) 2007-2013 Free Software Foundation, Inc.
00004 
00005    This file is part of GDB.
00006 
00007    This program is free software; you can redistribute it and/or modify
00008    it under the terms of the GNU General Public License as published by
00009    the Free Software Foundation; either version 3 of the License, or
00010    (at your option) any later version.
00011 
00012    This program is distributed in the hope that it will be useful,
00013    but WITHOUT ANY WARRANTY; without even the implied warranty of
00014    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015    GNU General Public License for more details.
00016 
00017    You should have received a copy of the GNU General Public License
00018    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
00019 
00020 #include "defs.h"
00021 #include "objfiles.h"
00022 #include "solist.h"
00023 #include "symtab.h"
00024 #include "symfile.h"
00025 #include "target.h"
00026 #include "vec.h"
00027 #include "solib-target.h"
00028 
00029 #include "gdb_string.h"
00030 
00031 /* Private data for each loaded library.  */
00032 struct lm_info
00033 {
00034   /* The library's name.  The name is normally kept in the struct
00035      so_list; it is only here during XML parsing.  */
00036   char *name;
00037 
00038   /* The target can either specify segment bases or section bases, not
00039      both.  */
00040 
00041   /* The base addresses for each independently relocatable segment of
00042      this shared library.  */
00043   VEC(CORE_ADDR) *segment_bases;
00044 
00045   /* The base addresses for each independently allocatable,
00046      relocatable section of this shared library.  */
00047   VEC(CORE_ADDR) *section_bases;
00048 
00049   /* The cached offsets for each section of this shared library,
00050      determined from SEGMENT_BASES, or SECTION_BASES.  */
00051   struct section_offsets *offsets;
00052 };
00053 
00054 typedef struct lm_info *lm_info_p;
00055 DEF_VEC_P(lm_info_p);
00056 
00057 #if !defined(HAVE_LIBEXPAT)
00058 
00059 static VEC(lm_info_p) *
00060 solib_target_parse_libraries (const char *library)
00061 {
00062   static int have_warned;
00063 
00064   if (!have_warned)
00065     {
00066       have_warned = 1;
00067       warning (_("Can not parse XML library list; XML support was disabled "
00068                  "at compile time"));
00069     }
00070 
00071   return NULL;
00072 }
00073 
00074 #else /* HAVE_LIBEXPAT */
00075 
00076 #include "xml-support.h"
00077 
00078 /* Handle the start of a <segment> element.  */
00079 
00080 static void
00081 library_list_start_segment (struct gdb_xml_parser *parser,
00082                             const struct gdb_xml_element *element,
00083                             void *user_data, VEC(gdb_xml_value_s) *attributes)
00084 {
00085   VEC(lm_info_p) **list = user_data;
00086   struct lm_info *last = VEC_last (lm_info_p, *list);
00087   ULONGEST *address_p = xml_find_attribute (attributes, "address")->value;
00088   CORE_ADDR address = (CORE_ADDR) *address_p;
00089 
00090   if (last->section_bases != NULL)
00091     gdb_xml_error (parser,
00092                    _("Library list with both segments and sections"));
00093 
00094   VEC_safe_push (CORE_ADDR, last->segment_bases, address);
00095 }
00096 
00097 static void
00098 library_list_start_section (struct gdb_xml_parser *parser,
00099                             const struct gdb_xml_element *element,
00100                             void *user_data, VEC(gdb_xml_value_s) *attributes)
00101 {
00102   VEC(lm_info_p) **list = user_data;
00103   struct lm_info *last = VEC_last (lm_info_p, *list);
00104   ULONGEST *address_p = xml_find_attribute (attributes, "address")->value;
00105   CORE_ADDR address = (CORE_ADDR) *address_p;
00106 
00107   if (last->segment_bases != NULL)
00108     gdb_xml_error (parser,
00109                    _("Library list with both segments and sections"));
00110 
00111   VEC_safe_push (CORE_ADDR, last->section_bases, address);
00112 }
00113 
00114 /* Handle the start of a <library> element.  */
00115 
00116 static void
00117 library_list_start_library (struct gdb_xml_parser *parser,
00118                             const struct gdb_xml_element *element,
00119                             void *user_data, VEC(gdb_xml_value_s) *attributes)
00120 {
00121   VEC(lm_info_p) **list = user_data;
00122   struct lm_info *item = XZALLOC (struct lm_info);
00123   const char *name = xml_find_attribute (attributes, "name")->value;
00124 
00125   item->name = xstrdup (name);
00126   VEC_safe_push (lm_info_p, *list, item);
00127 }
00128 
00129 static void
00130 library_list_end_library (struct gdb_xml_parser *parser,
00131                           const struct gdb_xml_element *element,
00132                           void *user_data, const char *body_text)
00133 {
00134   VEC(lm_info_p) **list = user_data;
00135   struct lm_info *lm_info = VEC_last (lm_info_p, *list);
00136 
00137   if (lm_info->segment_bases == NULL
00138       && lm_info->section_bases == NULL)
00139     gdb_xml_error (parser,
00140                    _("No segment or section bases defined"));
00141 }
00142 
00143 
00144 /* Handle the start of a <library-list> element.  */
00145 
00146 static void
00147 library_list_start_list (struct gdb_xml_parser *parser,
00148                          const struct gdb_xml_element *element,
00149                          void *user_data, VEC(gdb_xml_value_s) *attributes)
00150 {
00151   char *version = xml_find_attribute (attributes, "version")->value;
00152 
00153   if (strcmp (version, "1.0") != 0)
00154     gdb_xml_error (parser,
00155                    _("Library list has unsupported version \"%s\""),
00156                    version);
00157 }
00158 
00159 /* Discard the constructed library list.  */
00160 
00161 static void
00162 solib_target_free_library_list (void *p)
00163 {
00164   VEC(lm_info_p) **result = p;
00165   struct lm_info *info;
00166   int ix;
00167 
00168   for (ix = 0; VEC_iterate (lm_info_p, *result, ix, info); ix++)
00169     {
00170       xfree (info->name);
00171       VEC_free (CORE_ADDR, info->segment_bases);
00172       VEC_free (CORE_ADDR, info->section_bases);
00173       xfree (info);
00174     }
00175   VEC_free (lm_info_p, *result);
00176   *result = NULL;
00177 }
00178 
00179 /* The allowed elements and attributes for an XML library list.
00180    The root element is a <library-list>.  */
00181 
00182 static const struct gdb_xml_attribute segment_attributes[] = {
00183   { "address", GDB_XML_AF_NONE, gdb_xml_parse_attr_ulongest, NULL },
00184   { NULL, GDB_XML_AF_NONE, NULL, NULL }
00185 };
00186 
00187 static const struct gdb_xml_attribute section_attributes[] = {
00188   { "address", GDB_XML_AF_NONE, gdb_xml_parse_attr_ulongest, NULL },
00189   { NULL, GDB_XML_AF_NONE, NULL, NULL }
00190 };
00191 
00192 static const struct gdb_xml_element library_children[] = {
00193   { "segment", segment_attributes, NULL,
00194     GDB_XML_EF_REPEATABLE | GDB_XML_EF_OPTIONAL,
00195     library_list_start_segment, NULL },
00196   { "section", section_attributes, NULL,
00197     GDB_XML_EF_REPEATABLE | GDB_XML_EF_OPTIONAL,
00198     library_list_start_section, NULL },
00199   { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
00200 };
00201 
00202 static const struct gdb_xml_attribute library_attributes[] = {
00203   { "name", GDB_XML_AF_NONE, NULL, NULL },
00204   { NULL, GDB_XML_AF_NONE, NULL, NULL }
00205 };
00206 
00207 static const struct gdb_xml_element library_list_children[] = {
00208   { "library", library_attributes, library_children,
00209     GDB_XML_EF_REPEATABLE | GDB_XML_EF_OPTIONAL,
00210     library_list_start_library, library_list_end_library },
00211   { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
00212 };
00213 
00214 static const struct gdb_xml_attribute library_list_attributes[] = {
00215   { "version", GDB_XML_AF_NONE, NULL, NULL },
00216   { NULL, GDB_XML_AF_NONE, NULL, NULL }
00217 };
00218 
00219 static const struct gdb_xml_element library_list_elements[] = {
00220   { "library-list", library_list_attributes, library_list_children,
00221     GDB_XML_EF_NONE, library_list_start_list, NULL },
00222   { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
00223 };
00224 
00225 static VEC(lm_info_p) *
00226 solib_target_parse_libraries (const char *library)
00227 {
00228   VEC(lm_info_p) *result = NULL;
00229   struct cleanup *back_to = make_cleanup (solib_target_free_library_list,
00230                                           &result);
00231 
00232   if (gdb_xml_parse_quick (_("target library list"), "library-list.dtd",
00233                            library_list_elements, library, &result) == 0)
00234     {
00235       /* Parsed successfully, keep the result.  */
00236       discard_cleanups (back_to);
00237       return result;
00238     }
00239 
00240   do_cleanups (back_to);
00241   return NULL;
00242 }
00243 #endif
00244 
00245 static struct so_list *
00246 solib_target_current_sos (void)
00247 {
00248   struct so_list *new_solib, *start = NULL, *last = NULL;
00249   char *library_document;
00250   struct cleanup *old_chain;
00251   VEC(lm_info_p) *library_list;
00252   struct lm_info *info;
00253   int ix;
00254 
00255   /* Fetch the list of shared libraries.  */
00256   library_document = target_read_stralloc (&current_target,
00257                                            TARGET_OBJECT_LIBRARIES,
00258                                            NULL);
00259   if (library_document == NULL)
00260     return NULL;
00261 
00262   /* solib_target_parse_libraries may throw, so we use a cleanup.  */
00263   old_chain = make_cleanup (xfree, library_document);
00264 
00265   /* Parse the list.  */
00266   library_list = solib_target_parse_libraries (library_document);
00267 
00268   /* library_document string is not needed behind this point.  */
00269   do_cleanups (old_chain);
00270 
00271   if (library_list == NULL)
00272     return NULL;
00273 
00274   /* Build a struct so_list for each entry on the list.  */
00275   for (ix = 0; VEC_iterate (lm_info_p, library_list, ix, info); ix++)
00276     {
00277       new_solib = XZALLOC (struct so_list);
00278       strncpy (new_solib->so_name, info->name, SO_NAME_MAX_PATH_SIZE - 1);
00279       new_solib->so_name[SO_NAME_MAX_PATH_SIZE - 1] = '\0';
00280       strncpy (new_solib->so_original_name, info->name,
00281                SO_NAME_MAX_PATH_SIZE - 1);
00282       new_solib->so_original_name[SO_NAME_MAX_PATH_SIZE - 1] = '\0';
00283       new_solib->lm_info = info;
00284 
00285       /* We no longer need this copy of the name.  */
00286       xfree (info->name);
00287       info->name = NULL;
00288 
00289       /* Add it to the list.  */
00290       if (!start)
00291         last = start = new_solib;
00292       else
00293         {
00294           last->next = new_solib;
00295           last = new_solib;
00296         }
00297     }
00298 
00299   /* Free the library list, but not its members.  */
00300   VEC_free (lm_info_p, library_list);
00301 
00302   return start;
00303 }
00304 
00305 static void
00306 solib_target_special_symbol_handling (void)
00307 {
00308   /* Nothing needed.  */
00309 }
00310 
00311 static void
00312 solib_target_solib_create_inferior_hook (int from_tty)
00313 {
00314   /* Nothing needed.  */
00315 }
00316 
00317 static void
00318 solib_target_clear_solib (void)
00319 {
00320   /* Nothing needed.  */
00321 }
00322 
00323 static void
00324 solib_target_free_so (struct so_list *so)
00325 {
00326   gdb_assert (so->lm_info->name == NULL);
00327   xfree (so->lm_info->offsets);
00328   VEC_free (CORE_ADDR, so->lm_info->segment_bases);
00329   xfree (so->lm_info);
00330 }
00331 
00332 static void
00333 solib_target_relocate_section_addresses (struct so_list *so,
00334                                          struct target_section *sec)
00335 {
00336   CORE_ADDR offset;
00337 
00338   /* Build the offset table only once per object file.  We can not do
00339      it any earlier, since we need to open the file first.  */
00340   if (so->lm_info->offsets == NULL)
00341     {
00342       int num_sections = gdb_bfd_count_sections (so->abfd);
00343 
00344       so->lm_info->offsets = xzalloc (SIZEOF_N_SECTION_OFFSETS (num_sections));
00345 
00346       if (so->lm_info->section_bases)
00347         {
00348           int i;
00349           asection *sect;
00350           int num_section_bases
00351             = VEC_length (CORE_ADDR, so->lm_info->section_bases);
00352           int num_alloc_sections = 0;
00353 
00354           for (i = 0, sect = so->abfd->sections;
00355                sect != NULL;
00356                i++, sect = sect->next)
00357             if ((bfd_get_section_flags (so->abfd, sect) & SEC_ALLOC))
00358               num_alloc_sections++;
00359 
00360           if (num_alloc_sections != num_section_bases)
00361             warning (_("\
00362 Could not relocate shared library \"%s\": wrong number of ALLOC sections"),
00363                      so->so_name);
00364           else
00365             {
00366               int bases_index = 0;
00367               int found_range = 0;
00368               CORE_ADDR *section_bases;
00369 
00370               section_bases = VEC_address (CORE_ADDR,
00371                                            so->lm_info->section_bases);
00372 
00373               so->addr_low = ~(CORE_ADDR) 0;
00374               so->addr_high = 0;
00375               for (i = 0, sect = so->abfd->sections;
00376                    sect != NULL;
00377                    i++, sect = sect->next)
00378                 {
00379                   if (!(bfd_get_section_flags (so->abfd, sect) & SEC_ALLOC))
00380                     continue;
00381                   if (bfd_section_size (so->abfd, sect) > 0)
00382                     {
00383                       CORE_ADDR low, high;
00384 
00385                       low = section_bases[i];
00386                       high = low + bfd_section_size (so->abfd, sect) - 1;
00387 
00388                       if (low < so->addr_low)
00389                         so->addr_low = low;
00390                       if (high > so->addr_high)
00391                         so->addr_high = high;
00392                       gdb_assert (so->addr_low <= so->addr_high);
00393                       found_range = 1;
00394                     }
00395                   so->lm_info->offsets->offsets[i]
00396                     = section_bases[bases_index];
00397                   bases_index++;
00398                 }
00399               if (!found_range)
00400                 so->addr_low = so->addr_high = 0;
00401               gdb_assert (so->addr_low <= so->addr_high);
00402             }
00403         }
00404       else if (so->lm_info->segment_bases)
00405         {
00406           struct symfile_segment_data *data;
00407 
00408           data = get_symfile_segment_data (so->abfd);
00409           if (data == NULL)
00410             warning (_("\
00411 Could not relocate shared library \"%s\": no segments"), so->so_name);
00412           else
00413             {
00414               ULONGEST orig_delta;
00415               int i;
00416               int num_bases;
00417               CORE_ADDR *segment_bases;
00418 
00419               num_bases = VEC_length (CORE_ADDR, so->lm_info->segment_bases);
00420               segment_bases = VEC_address (CORE_ADDR,
00421                                            so->lm_info->segment_bases);
00422 
00423               if (!symfile_map_offsets_to_segments (so->abfd, data,
00424                                                     so->lm_info->offsets,
00425                                                     num_bases, segment_bases))
00426                 warning (_("\
00427 Could not relocate shared library \"%s\": bad offsets"), so->so_name);
00428 
00429               /* Find the range of addresses to report for this library in
00430                  "info sharedlibrary".  Report any consecutive segments
00431                  which were relocated as a single unit.  */
00432               gdb_assert (num_bases > 0);
00433               orig_delta = segment_bases[0] - data->segment_bases[0];
00434 
00435               for (i = 1; i < data->num_segments; i++)
00436                 {
00437                   /* If we have run out of offsets, assume all
00438                      remaining segments have the same offset.  */
00439                   if (i >= num_bases)
00440                     continue;
00441 
00442                   /* If this segment does not have the same offset, do
00443                      not include it in the library's range.  */
00444                   if (segment_bases[i] - data->segment_bases[i] != orig_delta)
00445                     break;
00446                 }
00447 
00448               so->addr_low = segment_bases[0];
00449               so->addr_high = (data->segment_bases[i - 1]
00450                                + data->segment_sizes[i - 1]
00451                                + orig_delta);
00452               gdb_assert (so->addr_low <= so->addr_high);
00453 
00454               free_symfile_segment_data (data);
00455             }
00456         }
00457     }
00458 
00459   offset = so->lm_info->offsets->offsets[gdb_bfd_section_index
00460                                          (sec->the_bfd_section->owner,
00461                                           sec->the_bfd_section)];
00462   sec->addr += offset;
00463   sec->endaddr += offset;
00464 }
00465 
00466 static int
00467 solib_target_open_symbol_file_object (void *from_ttyp)
00468 {
00469   /* We can't locate the main symbol file based on the target's
00470      knowledge; the user has to specify it.  */
00471   return 0;
00472 }
00473 
00474 static int
00475 solib_target_in_dynsym_resolve_code (CORE_ADDR pc)
00476 {
00477   /* We don't have a range of addresses for the dynamic linker; there
00478      may not be one in the program's address space.  So only report
00479      PLT entries (which may be import stubs).  */
00480   return in_plt_section (pc);
00481 }
00482 
00483 struct target_so_ops solib_target_so_ops;
00484 
00485 /* -Wmissing-prototypes */
00486 extern initialize_file_ftype _initialize_solib_target;
00487 
00488 void
00489 _initialize_solib_target (void)
00490 {
00491   solib_target_so_ops.relocate_section_addresses
00492     = solib_target_relocate_section_addresses;
00493   solib_target_so_ops.free_so = solib_target_free_so;
00494   solib_target_so_ops.clear_solib = solib_target_clear_solib;
00495   solib_target_so_ops.solib_create_inferior_hook
00496     = solib_target_solib_create_inferior_hook;
00497   solib_target_so_ops.special_symbol_handling
00498     = solib_target_special_symbol_handling;
00499   solib_target_so_ops.current_sos = solib_target_current_sos;
00500   solib_target_so_ops.open_symbol_file_object
00501     = solib_target_open_symbol_file_object;
00502   solib_target_so_ops.in_dynsym_resolve_code
00503     = solib_target_in_dynsym_resolve_code;
00504   solib_target_so_ops.bfd_open = solib_bfd_open;
00505 
00506   /* Set current_target_so_ops to solib_target_so_ops if not already
00507      set.  */
00508   if (current_target_so_ops == 0)
00509     current_target_so_ops = &solib_target_so_ops;
00510 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines