gdb/solib-aix.c - gdb

Global variables defined

Data types defined

Functions defined

Source code

  1. /* Copyright (C) 2013-2015 Free Software Foundation, Inc.

  2.    This file is part of GDB.

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

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

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

  13. #include "defs.h"
  14. #include "solib-aix.h"
  15. #include "solist.h"
  16. #include "inferior.h"
  17. #include "gdb_bfd.h"
  18. #include "gdbcore.h"
  19. #include "objfiles.h"
  20. #include "symtab.h"
  21. #include "xcoffread.h"
  22. #include "observer.h"
  23. #include "gdbcmd.h"

  24. /* Variable controlling the output of the debugging traces for
  25.    this module.  */
  26. static int solib_aix_debug;

  27. /* Our private data in struct so_list.  */

  28. struct lm_info
  29. {
  30.   /* The name of the file mapped by the loader.  Apart from the entry
  31.      for the main executable, this is usually a shared library (which,
  32.      on AIX, is an archive library file, created using the "ar"
  33.      command).  */
  34.   char *filename;

  35.   /* The name of the shared object file with the actual dynamic
  36.      loading dependency.  This may be NULL (Eg. main executable).  */
  37.   char *member_name;

  38.   /* The address in inferior memory where the text section got mapped.  */
  39.   CORE_ADDR text_addr;

  40.   /* The size of the text section, obtained via the loader data.  */
  41.   ULONGEST text_size;

  42.   /* The address in inferior memory where the data section got mapped.  */
  43.   CORE_ADDR data_addr;

  44.   /* The size of the data section, obtained via the loader data.  */
  45.   ULONGEST data_size;
  46. };

  47. typedef struct lm_info *lm_info_p;
  48. DEF_VEC_P(lm_info_p);

  49. /* Return a deep copy of the given struct lm_info object.  */

  50. static struct lm_info *
  51. solib_aix_new_lm_info (struct lm_info *info)
  52. {
  53.   struct lm_info *result = xmalloc (sizeof (struct lm_info));

  54.   memcpy (result, info, sizeof (struct lm_info));
  55.   result->filename = xstrdup (info->filename);
  56.   if (info->member_name != NULL)
  57.     result->member_name = xstrdup (info->member_name);

  58.   return result;
  59. }

  60. /* Free the memory allocated for the given lm_info.  */

  61. static void
  62. solib_aix_xfree_lm_info (struct lm_info *info)
  63. {
  64.   xfree (info->filename);
  65.   xfree (info->member_name);
  66.   xfree (info);
  67. }

  68. /* This module's per-inferior data.  */

  69. struct solib_aix_inferior_data
  70. {
  71.   /* The list of shared libraries.  NULL if not computed yet.

  72.      Note that the first element of this list is always the main
  73.      executable, which is not technically a shared library.  But
  74.      we need that information to perform its relocation, and
  75.      the same principles applied to shared libraries also apply
  76.      to the main executable.  So it's simpler to keep it as part
  77.      of this list.  */
  78.   VEC (lm_info_p) *library_list;
  79. };

  80. /* Key to our per-inferior data.  */
  81. static const struct inferior_data *solib_aix_inferior_data_handle;

  82. /* Return this module's data for the given inferior.
  83.    If none is found, add a zero'ed one now.  */

  84. static struct solib_aix_inferior_data *
  85. get_solib_aix_inferior_data (struct inferior *inf)
  86. {
  87.   struct solib_aix_inferior_data *data;

  88.   data = inferior_data (inf, solib_aix_inferior_data_handle);
  89.   if (data == NULL)
  90.     {
  91.       data = XCNEW (struct solib_aix_inferior_data);
  92.       set_inferior_data (inf, solib_aix_inferior_data_handle, data);
  93.     }

  94.   return data;
  95. }

  96. #if !defined(HAVE_LIBEXPAT)

  97. /* Dummy implementation if XML support is not compiled in.  */

  98. static VEC (lm_info_p) *
  99. solib_aix_parse_libraries (const char *library)
  100. {
  101.   static int have_warned;

  102.   if (!have_warned)
  103.     {
  104.       have_warned = 1;
  105.       warning (_("Can not parse XML library list; XML support was disabled "
  106.                  "at compile time"));
  107.     }

  108.   return NULL;
  109. }

  110. /* Dummy implementation if XML support is not compiled in.  */

  111. static void
  112. solib_aix_free_library_list (void *p)
  113. {
  114. }

  115. #else /* HAVE_LIBEXPAT */

  116. #include "xml-support.h"

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

  118. static void
  119. library_list_start_library (struct gdb_xml_parser *parser,
  120.                             const struct gdb_xml_element *element,
  121.                             void *user_data,
  122.                             VEC (gdb_xml_value_s) *attributes)
  123. {
  124.   VEC (lm_info_p) **list = user_data;
  125.   struct lm_info *item = XCNEW (struct lm_info);
  126.   struct gdb_xml_value *attr;

  127.   attr = xml_find_attribute (attributes, "name");
  128.   item->filename = xstrdup (attr->value);

  129.   attr = xml_find_attribute (attributes, "member");
  130.   if (attr != NULL)
  131.     item->member_name = xstrdup (attr->value);

  132.   attr = xml_find_attribute (attributes, "text_addr");
  133.   item->text_addr = * (ULONGEST *) attr->value;

  134.   attr = xml_find_attribute (attributes, "text_size");
  135.   item->text_size = * (ULONGEST *) attr->value;

  136.   attr = xml_find_attribute (attributes, "data_addr");
  137.   item->data_addr = * (ULONGEST *) attr->value;

  138.   attr = xml_find_attribute (attributes, "data_size");
  139.   item->data_size = * (ULONGEST *) attr->value;

  140.   VEC_safe_push (lm_info_p, *list, item);
  141. }

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

  143. static void
  144. library_list_start_list (struct gdb_xml_parser *parser,
  145.                          const struct gdb_xml_element *element,
  146.                          void *user_data, VEC (gdb_xml_value_s) *attributes)
  147. {
  148.   char *version = xml_find_attribute (attributes, "version")->value;

  149.   if (strcmp (version, "1.0") != 0)
  150.     gdb_xml_error (parser,
  151.                    _("Library list has unsupported version \"%s\""),
  152.                    version);
  153. }

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

  155. static void
  156. solib_aix_free_library_list (void *p)
  157. {
  158.   VEC (lm_info_p) **result = p;
  159.   struct lm_info *info;
  160.   int ix;

  161.   if (solib_aix_debug)
  162.     fprintf_unfiltered (gdb_stdlog, "DEBUG: solib_aix_free_library_list\n");

  163.   for (ix = 0; VEC_iterate (lm_info_p, *result, ix, info); ix++)
  164.     solib_aix_xfree_lm_info (info);
  165.   VEC_free (lm_info_p, *result);
  166.   *result = NULL;
  167. }

  168. /* The allowed elements and attributes for an AIX library list
  169.    described in XML format.  The root element is a <library-list-aix>.  */

  170. static const struct gdb_xml_attribute library_attributes[] =
  171. {
  172.   { "name", GDB_XML_AF_NONE, NULL, NULL },
  173.   { "member", GDB_XML_AF_OPTIONAL, NULL, NULL },
  174.   { "text_addr", GDB_XML_AF_NONE, gdb_xml_parse_attr_ulongest, NULL },
  175.   { "text_size", GDB_XML_AF_NONE, gdb_xml_parse_attr_ulongest, NULL },
  176.   { "data_addr", GDB_XML_AF_NONE, gdb_xml_parse_attr_ulongest, NULL },
  177.   { "data_size", GDB_XML_AF_NONE, gdb_xml_parse_attr_ulongest, NULL },
  178.   { NULL, GDB_XML_AF_NONE, NULL, NULL }
  179. };

  180. static const struct gdb_xml_element library_list_children[] =
  181. {
  182.   { "library", library_attributes, NULL,
  183.     GDB_XML_EF_REPEATABLE | GDB_XML_EF_OPTIONAL,
  184.     library_list_start_library, NULL},
  185.   { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
  186. };

  187. static const struct gdb_xml_attribute library_list_attributes[] =
  188. {
  189.   { "version", GDB_XML_AF_NONE, NULL, NULL },
  190.   { NULL, GDB_XML_AF_NONE, NULL, NULL }
  191. };

  192. static const struct gdb_xml_element library_list_elements[] =
  193. {
  194.   { "library-list-aix", library_list_attributes, library_list_children,
  195.     GDB_XML_EF_NONE, library_list_start_list, NULL },
  196.   { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
  197. };

  198. /* Parse LIBRARY, a string containing the loader info in XML format,
  199.    and return an lm_info_p vector.

  200.    Return NULL if the parsing failed.  */

  201. static VEC (lm_info_p) *
  202. solib_aix_parse_libraries (const char *library)
  203. {
  204.   VEC (lm_info_p) *result = NULL;
  205.   struct cleanup *back_to = make_cleanup (solib_aix_free_library_list,
  206.                                           &result);

  207.   if (gdb_xml_parse_quick (_("aix library list"), "library-list-aix.dtd",
  208.                            library_list_elements, library, &result) == 0)
  209.     {
  210.       /* Parsed successfully, keep the result.  */
  211.       discard_cleanups (back_to);
  212.       return result;
  213.     }

  214.   do_cleanups (back_to);
  215.   return NULL;
  216. }

  217. #endif /* HAVE_LIBEXPAT */

  218. /* Return the loader info for the given inferior (INF), or NULL if
  219.    the list could not be computed.

  220.    Cache the result in per-inferior data, so as to avoid recomputing it
  221.    each time this function is called.

  222.    If an error occurs while computing this list, and WARNING_MSG
  223.    is not NULL, then print a warning including WARNING_MSG and
  224.    a description of the error.  */

  225. static VEC (lm_info_p) *
  226. solib_aix_get_library_list (struct inferior *inf, const char *warning_msg)
  227. {
  228.   struct solib_aix_inferior_data *data;
  229.   char *library_document;
  230.   struct cleanup *cleanup;

  231.   /* If already computed, return the cached value.  */
  232.   data = get_solib_aix_inferior_data (inf);
  233.   if (data->library_list != NULL)
  234.     return data->library_list;

  235.   library_document = target_read_stralloc (&current_target,
  236.                                            TARGET_OBJECT_LIBRARIES_AIX,
  237.                                            NULL);
  238.   if (library_document == NULL && warning_msg != NULL)
  239.     {
  240.       warning (_("%s (failed to read TARGET_OBJECT_LIBRARIES_AIX)"),
  241.                warning_msg);
  242.       return NULL;
  243.     }
  244.   cleanup = make_cleanup (xfree, library_document);

  245.   if (solib_aix_debug)
  246.     fprintf_unfiltered (gdb_stdlog,
  247.                         "DEBUG: TARGET_OBJECT_LIBRARIES_AIX = \n%s\n",
  248.                         library_document);

  249.   data->library_list = solib_aix_parse_libraries (library_document);
  250.   if (data->library_list == NULL && warning_msg != NULL)
  251.     {
  252.       warning (_("%s (missing XML support?)"), warning_msg);
  253.       do_cleanups (cleanup);
  254.       return NULL;
  255.     }

  256.   do_cleanups (cleanup);
  257.   return data->library_list;
  258. }

  259. /* If the .bss section's VMA is set to an address located before
  260.    the end of the .data section, causing the two sections to overlap,
  261.    return the overlap in bytes.  Otherwise, return zero.

  262.    Motivation:

  263.    The GNU linker sometimes sets the start address of the .bss session
  264.    before the end of the .data section, making the 2 sections overlap.
  265.    The loader appears to handle this situation gracefully, by simply
  266.    loading the bss section right after the end of the .data section.

  267.    This means that the .data and the .bss sections are sometimes
  268.    no longer relocated by the same amount.  The problem is that
  269.    the ldinfo data does not contain any information regarding
  270.    the relocation of the .bss section, assuming that it would be
  271.    identical to the information provided for the .data section
  272.    (this is what would normally happen if the program was linked
  273.    correctly).

  274.    GDB therefore needs to detect those cases, and make the corresponding
  275.    adjustment to the .bss section offset computed from the ldinfo data
  276.    when necessary.  This function returns the adjustment amount  (or
  277.    zero when no adjustment is needed).  */

  278. static CORE_ADDR
  279. solib_aix_bss_data_overlap (bfd *abfd)
  280. {
  281.   struct bfd_section *data_sect, *bss_sect;

  282.   data_sect = bfd_get_section_by_name (abfd, ".data");
  283.   if (data_sect == NULL)
  284.     return 0; /* No overlap possible.  */

  285.   bss_sect = bfd_get_section_by_name (abfd, ".bss");
  286.   if (bss_sect == NULL)
  287.     return 0; /* No overlap possible.  */

  288.   /* Assume the problem only occurs with linkers that place the .bss
  289.      section after the .data section (the problem has only been
  290.      observed when using the GNU linker, and the default linker
  291.      script always places the .data and .bss sections in that order).  */
  292.   if (bfd_section_vma (abfd, bss_sect)
  293.       < bfd_section_vma (abfd, data_sect))
  294.     return 0;

  295.   if (bfd_section_vma (abfd, bss_sect)
  296.       < bfd_section_vma (abfd, data_sect) + bfd_get_section_size (data_sect))
  297.     return ((bfd_section_vma (abfd, data_sect)
  298.              + bfd_get_section_size (data_sect))
  299.             - bfd_section_vma (abfd, bss_sect));

  300.   return 0;
  301. }

  302. /* Implement the "relocate_section_addresses" target_so_ops method.  */

  303. static void
  304. solib_aix_relocate_section_addresses (struct so_list *so,
  305.                                       struct target_section *sec)
  306. {
  307.   struct bfd_section *bfd_sect = sec->the_bfd_section;
  308.   bfd *abfd = bfd_sect->owner;
  309.   const char *section_name = bfd_section_name (abfd, bfd_sect);
  310.   struct lm_info *info = so->lm_info;

  311.   if (strcmp (section_name, ".text") == 0)
  312.     {
  313.       sec->addr = info->text_addr;
  314.       sec->endaddr = sec->addr + info->text_size;

  315.       /* The text address given to us by the loader contains
  316.          XCOFF headers, so we need to adjust by this much.  */
  317.       sec->addr += bfd_sect->filepos;
  318.     }
  319.   else if (strcmp (section_name, ".data") == 0)
  320.     {
  321.       sec->addr = info->data_addr;
  322.       sec->endaddr = sec->addr + info->data_size;
  323.     }
  324.   else if (strcmp (section_name, ".bss") == 0)
  325.     {
  326.       /* The information provided by the loader does not include
  327.          the address of the .bss section, but we know that it gets
  328.          relocated by the same offset as the .data section.  So,
  329.          compute the relocation offset for the .data section, and
  330.          apply it to the .bss section as well.  If the .data section
  331.          is not defined (which seems highly unlikely), do our best
  332.          by assuming no relocation.  */
  333.       struct bfd_section *data_sect
  334.         = bfd_get_section_by_name (abfd, ".data");
  335.       CORE_ADDR data_offset = 0;

  336.       if (data_sect != NULL)
  337.         data_offset = info->data_addr - bfd_section_vma (abfd, data_sect);

  338.       sec->addr = bfd_section_vma (abfd, bfd_sect) + data_offset;
  339.       sec->addr += solib_aix_bss_data_overlap (abfd);
  340.       sec->endaddr = sec->addr + bfd_section_size (abfd, bfd_sect);
  341.     }
  342.   else
  343.     {
  344.       /* All other sections should not be relocated.  */
  345.       sec->addr = bfd_section_vma (abfd, bfd_sect);
  346.       sec->endaddr = sec->addr + bfd_section_size (abfd, bfd_sect);
  347.     }
  348. }

  349. /* Implement the "free_so" target_so_ops method.  */

  350. static void
  351. solib_aix_free_so (struct so_list *so)
  352. {
  353.   if (solib_aix_debug)
  354.     fprintf_unfiltered (gdb_stdlog, "DEBUG: solib_aix_free_so (%s)\n",
  355.                         so->so_name);
  356.   solib_aix_xfree_lm_info (so->lm_info);
  357. }

  358. /* Implement the "clear_solib" target_so_ops method.  */

  359. static void
  360. solib_aix_clear_solib (void)
  361. {
  362.   /* Nothing needed.  */
  363. }

  364. /* Compute and return the OBJFILE's section_offset array, using
  365.    the associated loader info (INFO).

  366.    The resulting array is computed on the heap and must be
  367.    deallocated after use.  */

  368. static struct section_offsets *
  369. solib_aix_get_section_offsets (struct objfile *objfile,
  370.                                struct lm_info *info)
  371. {
  372.   struct section_offsets *offsets;
  373.   bfd *abfd = objfile->obfd;
  374.   int i;

  375.   offsets = XCNEWVEC (struct section_offsets, objfile->num_sections);

  376.   /* .text */

  377.   if (objfile->sect_index_text != -1)
  378.     {
  379.       struct bfd_section *sect
  380.         = objfile->sections[objfile->sect_index_text].the_bfd_section;

  381.       offsets->offsets[objfile->sect_index_text]
  382.         = info->text_addr + sect->filepos - bfd_section_vma (abfd, sect);
  383.     }

  384.   /* .data */

  385.   if (objfile->sect_index_data != -1)
  386.     {
  387.       struct bfd_section *sect
  388.         = objfile->sections[objfile->sect_index_data].the_bfd_section;

  389.       offsets->offsets[objfile->sect_index_data]
  390.         = info->data_addr - bfd_section_vma (abfd, sect);
  391.     }

  392.   /* .bss

  393.      The offset of the .bss section should be identical to the offset
  394.      of the .data section.  If no .data section (which seems hard to
  395.      believe it is possible), assume it is zero.  */

  396.   if (objfile->sect_index_bss != -1
  397.       && objfile->sect_index_data != -1)
  398.     {
  399.       offsets->offsets[objfile->sect_index_bss]
  400.         = (offsets->offsets[objfile->sect_index_data]
  401.            + solib_aix_bss_data_overlap (abfd));
  402.     }

  403.   /* All other sections should not need relocation.  */

  404.   return offsets;
  405. }

  406. /* Implement the "solib_create_inferior_hook" target_so_ops method.  */

  407. static void
  408. solib_aix_solib_create_inferior_hook (int from_tty)
  409. {
  410.   const char *warning_msg = "unable to relocate main executable";
  411.   VEC (lm_info_p) *library_list;
  412.   struct lm_info *exec_info;

  413.   /* We need to relocate the main executable...  */

  414.   library_list = solib_aix_get_library_list (current_inferior (),
  415.                                              warning_msg);
  416.   if (library_list == NULL)
  417.     return/* Warning already printed.  */

  418.   if (VEC_length (lm_info_p, library_list) < 1)
  419.     {
  420.       warning (_("unable to relocate main executable (no info from loader)"));
  421.       return;
  422.     }

  423.   exec_info = VEC_index (lm_info_p, library_list, 0);

  424.   if (symfile_objfile != NULL)
  425.     {
  426.       struct section_offsets *offsets
  427.         = solib_aix_get_section_offsets (symfile_objfile, exec_info);
  428.       struct cleanup *cleanup = make_cleanup (xfree, offsets);

  429.       objfile_relocate (symfile_objfile, offsets);
  430.       do_cleanups (cleanup);
  431.     }
  432. }

  433. /* Implement the "special_symbol_handling" target_so_ops method.  */

  434. static void
  435. solib_aix_special_symbol_handling (void)
  436. {
  437.   /* Nothing needed.  */
  438. }

  439. /* Implement the "current_sos" target_so_ops method.  */

  440. static struct so_list *
  441. solib_aix_current_sos (void)
  442. {
  443.   struct so_list *start = NULL, *last = NULL;
  444.   VEC (lm_info_p) *library_list;
  445.   struct lm_info *info;
  446.   int ix;

  447.   library_list = solib_aix_get_library_list (current_inferior (), NULL);
  448.   if (library_list == NULL)
  449.     return NULL;

  450.   /* Build a struct so_list for each entry on the list.
  451.      We skip the first entry, since this is the entry corresponding
  452.      to the main executable, not a shared library.  */
  453.   for (ix = 1; VEC_iterate (lm_info_p, library_list, ix, info); ix++)
  454.     {
  455.       struct so_list *new_solib = XCNEW (struct so_list);
  456.       char *so_name;

  457.       if (info->member_name == NULL)
  458.         {
  459.          /* INFO->FILENAME is probably not an archive, but rather
  460.             a shared object.  Unusual, but it should be possible
  461.             to link a program against a shared object directory,
  462.             without having to put it in an archive first.  */
  463.          so_name = xstrdup (info->filename);
  464.         }
  465.       else
  466.         {
  467.          /* This is the usual case on AIX, where the shared object
  468.             is a member of an archive.  Create a synthetic so_name
  469.             that follows the same convention as AIX's ldd tool
  470.             (Eg: "/lib/libc.a(shr.o)").  */
  471.          so_name = xstrprintf ("%s(%s)", info->filename, info->member_name);
  472.         }
  473.       strncpy (new_solib->so_original_name, so_name,
  474.                SO_NAME_MAX_PATH_SIZE - 1);
  475.       new_solib->so_name[SO_NAME_MAX_PATH_SIZE - 1] = '\0';
  476.       memcpy (new_solib->so_name, new_solib->so_original_name,
  477.               SO_NAME_MAX_PATH_SIZE);
  478.       new_solib->lm_info = solib_aix_new_lm_info (info);

  479.       /* Add it to the list.  */
  480.       if (!start)
  481.         last = start = new_solib;
  482.       else
  483.         {
  484.           last->next = new_solib;
  485.           last = new_solib;
  486.         }
  487.     }

  488.   return start;
  489. }

  490. /* Implement the "open_symbol_file_object" target_so_ops method.  */

  491. static int
  492. solib_aix_open_symbol_file_object (void *from_ttyp)
  493. {
  494.   return 0;
  495. }

  496. /* Implement the "in_dynsym_resolve_code" target_so_ops method.  */

  497. static int
  498. solib_aix_in_dynsym_resolve_code (CORE_ADDR pc)
  499. {
  500.   return 0;
  501. }

  502. /* Implement the "bfd_open" target_so_ops method.  */

  503. static bfd *
  504. solib_aix_bfd_open (char *pathname)
  505. {
  506.   /* The pathname is actually a synthetic filename with the following
  507.      form: "/path/to/sharedlib(member.o)" (double-quotes excluded).
  508.      split this into archive name and member name.

  509.      FIXME: This is a little hacky.  Perhaps we should provide access
  510.      to the solib's lm_info here?  */
  511.   const int path_len = strlen (pathname);
  512.   char *sep;
  513.   char *filename;
  514.   int filename_len;
  515.   char *member_name;
  516.   bfd *archive_bfd, *object_bfd;
  517.   struct cleanup *cleanup;

  518.   if (pathname[path_len - 1] != ')')
  519.     return solib_bfd_open (pathname);

  520.   /* Search for the associated parens.  */
  521.   sep = strrchr (pathname, '(');
  522.   if (sep == NULL)
  523.     {
  524.       /* Should never happen, but recover as best as we can (trying
  525.          to open pathname without decoding, possibly leading to
  526.          a failure), rather than triggering an assert failure).  */
  527.       warning (_("missing '(' in shared object pathname: %s"), pathname);
  528.       return solib_bfd_open (pathname);
  529.     }
  530.   filename_len = sep - pathname;

  531.   filename = xstrprintf ("%.*s", filename_len, pathname);
  532.   cleanup = make_cleanup (xfree, filename);
  533.   member_name = xstrprintf ("%.*s", path_len - filename_len - 2, sep + 1);
  534.   make_cleanup (xfree, member_name);

  535.   archive_bfd = gdb_bfd_open (filename, gnutarget, -1);
  536.   if (archive_bfd == NULL)
  537.     {
  538.       warning (_("Could not open `%s' as an executable file: %s"),
  539.                filename, bfd_errmsg (bfd_get_error ()));
  540.       do_cleanups (cleanup);
  541.       return NULL;
  542.     }

  543.   if (bfd_check_format (archive_bfd, bfd_object))
  544.     {
  545.       do_cleanups (cleanup);
  546.       return archive_bfd;
  547.     }

  548.   if (! bfd_check_format (archive_bfd, bfd_archive))
  549.     {
  550.       warning (_("\"%s\": not in executable format: %s."),
  551.                filename, bfd_errmsg (bfd_get_error ()));
  552.       gdb_bfd_unref (archive_bfd);
  553.       do_cleanups (cleanup);
  554.       return NULL;
  555.     }

  556.   object_bfd = gdb_bfd_openr_next_archived_file (archive_bfd, NULL);
  557.   while (object_bfd != NULL)
  558.     {
  559.       bfd *next;

  560.       if (strcmp (member_name, object_bfd->filename) == 0)
  561.         break;

  562.       next = gdb_bfd_openr_next_archived_file (archive_bfd, object_bfd);
  563.       gdb_bfd_unref (object_bfd);
  564.       object_bfd = next;
  565.     }

  566.   if (object_bfd == NULL)
  567.     {
  568.       warning (_("\"%s\": member \"%s\" missing."), filename, member_name);
  569.       gdb_bfd_unref (archive_bfd);
  570.       do_cleanups (cleanup);
  571.       return NULL;
  572.     }

  573.   if (! bfd_check_format (object_bfd, bfd_object))
  574.     {
  575.       warning (_("%s(%s): not in object format: %s."),
  576.                filename, member_name, bfd_errmsg (bfd_get_error ()));
  577.       gdb_bfd_unref (archive_bfd);
  578.       gdb_bfd_unref (object_bfd);
  579.       do_cleanups (cleanup);
  580.       return NULL;
  581.     }

  582.   /* Override the returned bfd's name with our synthetic name in order
  583.      to allow commands listing all shared libraries to display that
  584.      synthetic name.  Otherwise, we would only be displaying the name
  585.      of the archive member object.  */
  586.   xfree (bfd_get_filename (object_bfd));
  587.   object_bfd->filename = xstrdup (pathname);

  588.   gdb_bfd_unref (archive_bfd);
  589.   do_cleanups (cleanup);
  590.   return object_bfd;
  591. }

  592. /* Return the obj_section corresponding to OBJFILE's data section,
  593.    or NULL if not found.  */
  594. /* FIXME: Define in a more general location? */

  595. static struct obj_section *
  596. data_obj_section_from_objfile (struct objfile *objfile)
  597. {
  598.   struct obj_section *osect;

  599.   ALL_OBJFILE_OSECTIONS (objfile, osect)
  600.     if (strcmp (bfd_section_name (objfile->obfd, osect->the_bfd_section),
  601.                 ".data") == 0)
  602.       return osect;

  603.   return NULL;
  604. }

  605. /* Return the TOC value corresponding to the given PC address,
  606.    or raise an error if the value could not be determined.  */

  607. CORE_ADDR
  608. solib_aix_get_toc_value (CORE_ADDR pc)
  609. {
  610.   struct obj_section *pc_osect = find_pc_section (pc);
  611.   struct obj_section *data_osect;
  612.   CORE_ADDR result;

  613.   if (pc_osect == NULL)
  614.     error (_("unable to find TOC entry for pc %s "
  615.              "(no section contains this PC)"),
  616.            core_addr_to_string (pc));

  617.   data_osect = data_obj_section_from_objfile (pc_osect->objfile);
  618.   if (data_osect == NULL)
  619.     error (_("unable to find TOC entry for pc %s "
  620.              "(%s has no data section)"),
  621.            core_addr_to_string (pc), objfile_name (pc_osect->objfile));

  622.   result = (obj_section_addr (data_osect)
  623.             + xcoff_get_toc_offset (pc_osect->objfile));
  624.   if (solib_aix_debug)
  625.     fprintf_unfiltered (gdb_stdlog,
  626.                         "DEBUG: solib_aix_get_toc_value (pc=%s) -> %s\n",
  627.                         core_addr_to_string (pc),
  628.                         core_addr_to_string (result));

  629.   return result;
  630. }

  631. /* This module's normal_stop observer.  */

  632. static void
  633. solib_aix_normal_stop_observer (struct bpstats *unused_1, int unused_2)
  634. {
  635.   struct solib_aix_inferior_data *data
  636.     = get_solib_aix_inferior_data (current_inferior ());

  637.   /* The inferior execution has been resumed, and it just stopped
  638.      again.  This means that the list of shared libraries may have
  639.      evolved.  Reset our cached value.  */
  640.   solib_aix_free_library_list (&data->library_list);
  641. }

  642. /* Implements the "show debug aix-solib" command.  */

  643. static void
  644. show_solib_aix_debug (struct ui_file *file, int from_tty,
  645.                       struct cmd_list_element *c, const char *value)
  646. {
  647.   fprintf_filtered (file, _("solib-aix debugging is %s.\n"), value);
  648. }

  649. /* The target_so_ops for AIX targets.  */
  650. struct target_so_ops solib_aix_so_ops;

  651. /* -Wmissing-prototypes */
  652. extern initialize_file_ftype _initialize_solib_aix;

  653. void
  654. _initialize_solib_aix (void)
  655. {
  656.   solib_aix_so_ops.relocate_section_addresses
  657.     = solib_aix_relocate_section_addresses;
  658.   solib_aix_so_ops.free_so = solib_aix_free_so;
  659.   solib_aix_so_ops.clear_solib = solib_aix_clear_solib;
  660.   solib_aix_so_ops.solib_create_inferior_hook
  661.     = solib_aix_solib_create_inferior_hook;
  662.   solib_aix_so_ops.special_symbol_handling
  663.     = solib_aix_special_symbol_handling;
  664.   solib_aix_so_ops.current_sos = solib_aix_current_sos;
  665.   solib_aix_so_ops.open_symbol_file_object
  666.     = solib_aix_open_symbol_file_object;
  667.   solib_aix_so_ops.in_dynsym_resolve_code
  668.     = solib_aix_in_dynsym_resolve_code;
  669.   solib_aix_so_ops.bfd_open = solib_aix_bfd_open;

  670.   solib_aix_inferior_data_handle = register_inferior_data ();

  671.   observer_attach_normal_stop (solib_aix_normal_stop_observer);

  672.   /* Debug this file's internals.  */
  673.   add_setshow_boolean_cmd ("aix-solib", class_maintenance,
  674.                            &solib_aix_debug, _("\
  675. Control the debugging traces for the solib-aix module."), _("\
  676. Show whether solib-aix debugging traces are enabled."), _("\
  677. When on, solib-aix debugging traces are enabled."),
  678.                             NULL,
  679.                             show_solib_aix_debug,
  680.                             &setdebuglist, &showdebuglist);
  681. }