gdb/solib.c - gdb

Global variables defined

Functions defined

Macros defined

Source code

  1. /* Handle shared libraries for GDB, the GNU Debugger.

  2.    Copyright (C) 1990-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 <sys/types.h>
  16. #include <fcntl.h>
  17. #include "symtab.h"
  18. #include "bfd.h"
  19. #include "symfile.h"
  20. #include "objfiles.h"
  21. #include "gdbcore.h"
  22. #include "command.h"
  23. #include "target.h"
  24. #include "frame.h"
  25. #include "gdb_regex.h"
  26. #include "inferior.h"
  27. #include "environ.h"
  28. #include "language.h"
  29. #include "gdbcmd.h"
  30. #include "completer.h"
  31. #include "filenames.h"                /* for DOSish file names */
  32. #include "exec.h"
  33. #include "solist.h"
  34. #include "observer.h"
  35. #include "readline/readline.h"
  36. #include "remote.h"
  37. #include "solib.h"
  38. #include "interps.h"
  39. #include "filesystem.h"
  40. #include "gdb_bfd.h"
  41. #include "filestuff.h"

  42. /* Architecture-specific operations.  */

  43. /* Per-architecture data key.  */
  44. static struct gdbarch_data *solib_data;

  45. static void *
  46. solib_init (struct obstack *obstack)
  47. {
  48.   struct target_so_ops **ops;

  49.   ops = OBSTACK_ZALLOC (obstack, struct target_so_ops *);
  50.   *ops = current_target_so_ops;
  51.   return ops;
  52. }

  53. static const struct target_so_ops *
  54. solib_ops (struct gdbarch *gdbarch)
  55. {
  56.   const struct target_so_ops **ops = gdbarch_data (gdbarch, solib_data);

  57.   return *ops;
  58. }

  59. /* Set the solib operations for GDBARCH to NEW_OPS.  */

  60. void
  61. set_solib_ops (struct gdbarch *gdbarch, const struct target_so_ops *new_ops)
  62. {
  63.   const struct target_so_ops **ops = gdbarch_data (gdbarch, solib_data);

  64.   *ops = new_ops;
  65. }


  66. /* external data declarations */

  67. /* FIXME: gdbarch needs to control this variable, or else every
  68.    configuration needs to call set_solib_ops.  */
  69. struct target_so_ops *current_target_so_ops;

  70. /* List of known shared objects */
  71. #define so_list_head current_program_space->so_list

  72. /* Local function prototypes */

  73. /* If non-empty, this is a search path for loading non-absolute shared library
  74.    symbol files.  This takes precedence over the environment variables PATH
  75.    and LD_LIBRARY_PATH.  */
  76. static char *solib_search_path = NULL;
  77. static void
  78. show_solib_search_path (struct ui_file *file, int from_tty,
  79.                         struct cmd_list_element *c, const char *value)
  80. {
  81.   fprintf_filtered (file, _("The search path for loading non-absolute "
  82.                             "shared library symbol files is %s.\n"),
  83.                     value);
  84. }

  85. /* Same as HAVE_DOS_BASED_FILE_SYSTEM, but useable as an rvalue.  */
  86. #if (HAVE_DOS_BASED_FILE_SYSTEM)
  87. #  define DOS_BASED_FILE_SYSTEM 1
  88. #else
  89. #  define DOS_BASED_FILE_SYSTEM 0
  90. #endif

  91. /* Returns the full pathname of the shared library file, or NULL if
  92.    not found.  (The pathname is malloc'ed; it needs to be freed by the
  93.    caller.)  *FD is set to either -1 or an open file handle for the
  94.    library.

  95.    Global variable GDB_SYSROOT is used as a prefix directory
  96.    to search for shared libraries if they have an absolute path.

  97.    Global variable SOLIB_SEARCH_PATH is used as a prefix directory
  98.    (or set of directories, as in LD_LIBRARY_PATH) to search for all
  99.    shared libraries if not found in GDB_SYSROOT.

  100.    Search algorithm:
  101.    * If there is a gdb_sysroot and path is absolute:
  102.    *   Search for gdb_sysroot/path.
  103.    * else
  104.    *   Look for it literally (unmodified).
  105.    * Look in SOLIB_SEARCH_PATH.
  106.    * If available, use target defined search function.
  107.    * If gdb_sysroot is NOT set, perform the following two searches:
  108.    *   Look in inferior's $PATH.
  109.    *   Look in inferior's $LD_LIBRARY_PATH.
  110.    *
  111.    * The last check avoids doing this search when targetting remote
  112.    * machines since gdb_sysroot will almost always be set.
  113. */

  114. char *
  115. solib_find (char *in_pathname, int *fd)
  116. {
  117.   const struct target_so_ops *ops = solib_ops (target_gdbarch ());
  118.   int found_file = -1;
  119.   char *temp_pathname = NULL;
  120.   int gdb_sysroot_is_empty;
  121.   const char *solib_symbols_extension
  122.     = gdbarch_solib_symbols_extension (target_gdbarch ());
  123.   const char *fskind = effective_target_file_system_kind ();
  124.   struct cleanup *old_chain = make_cleanup (null_cleanup, NULL);
  125.   char *sysroot = NULL;

  126.   /* If solib_symbols_extension is set, replace the file's
  127.      extension.  */
  128.   if (solib_symbols_extension)
  129.     {
  130.       char *p = in_pathname + strlen (in_pathname);

  131.       while (p > in_pathname && *p != '.')
  132.         p--;

  133.       if (*p == '.')
  134.         {
  135.           char *new_pathname;

  136.           new_pathname = alloca (p - in_pathname + 1
  137.                                  + strlen (solib_symbols_extension) + 1);
  138.           memcpy (new_pathname, in_pathname, p - in_pathname + 1);
  139.           strcpy (new_pathname + (p - in_pathname) + 1,
  140.                   solib_symbols_extension);

  141.           in_pathname = new_pathname;
  142.         }
  143.     }

  144.   gdb_sysroot_is_empty = (gdb_sysroot == NULL || *gdb_sysroot == 0);

  145.   if (!gdb_sysroot_is_empty)
  146.     {
  147.       int prefix_len = strlen (gdb_sysroot);

  148.       /* Remove trailing slashes from absolute prefix.  */
  149.       while (prefix_len > 0
  150.              && IS_DIR_SEPARATOR (gdb_sysroot[prefix_len - 1]))
  151.         prefix_len--;

  152.       sysroot = savestring (gdb_sysroot, prefix_len);
  153.       make_cleanup (xfree, sysroot);
  154.     }

  155.   /* If we're on a non-DOS-based system, backslashes won't be
  156.      understood as directory separator, so, convert them to forward
  157.      slashes, iff we're supposed to handle DOS-based file system
  158.      semantics for target paths.  */
  159.   if (!DOS_BASED_FILE_SYSTEM && fskind == file_system_kind_dos_based)
  160.     {
  161.       char *p;

  162.       /* Avoid clobbering our input.  */
  163.       p = alloca (strlen (in_pathname) + 1);
  164.       strcpy (p, in_pathname);
  165.       in_pathname = p;

  166.       for (; *p; p++)
  167.         {
  168.           if (*p == '\\')
  169.             *p = '/';
  170.         }
  171.     }

  172.   /* Note, we're interested in IS_TARGET_ABSOLUTE_PATH, not
  173.      IS_ABSOLUTE_PATH.  The latter is for host paths only, while
  174.      IN_PATHNAME is a target path.  For example, if we're supposed to
  175.      be handling DOS-like semantics we want to consider a
  176.      'c:/foo/bar.dll' path as an absolute path, even on a Unix box.
  177.      With such a path, before giving up on the sysroot, we'll try:

  178.        1st attempt, c:/foo/bar.dll ==> /sysroot/c:/foo/bar.dll
  179.        2nd attempt, c:/foo/bar.dll ==> /sysroot/c/foo/bar.dll
  180.        3rd attempt, c:/foo/bar.dll ==> /sysroot/foo/bar.dll
  181.   */

  182.   if (!IS_TARGET_ABSOLUTE_PATH (fskind, in_pathname) || gdb_sysroot_is_empty)
  183.     temp_pathname = xstrdup (in_pathname);
  184.   else
  185.     {
  186.       int need_dir_separator;

  187.       /* Concatenate the sysroot and the target reported filename.  We
  188.          may need to glue them with a directory separator.  Cases to
  189.          consider:

  190.         | sysroot         | separator | in_pathname    |
  191.         |-----------------+-----------+----------------|
  192.         | /some/dir       | /         | c:/foo/bar.dll |
  193.         | /some/dir       |           | /foo/bar.dll   |
  194.         | remote:         |           | c:/foo/bar.dll |
  195.         | remote:         |           | /foo/bar.dll   |
  196.         | remote:some/dir | /         | c:/foo/bar.dll |
  197.         | remote:some/dir |           | /foo/bar.dll   |

  198.         IOW, we don't need to add a separator if IN_PATHNAME already
  199.         has one, or when the the sysroot is exactly "remote:".
  200.         There's no need to check for drive spec explicitly, as we only
  201.         get here if IN_PATHNAME is considered an absolute path.  */
  202.       need_dir_separator = !(IS_DIR_SEPARATOR (in_pathname[0])
  203.                              || strcmp (REMOTE_SYSROOT_PREFIX, sysroot) == 0);

  204.       /* Cat the prefixed pathname together.  */
  205.       temp_pathname = concat (sysroot,
  206.                               need_dir_separator ? SLASH_STRING : "",
  207.                               in_pathname, (char *) NULL);
  208.     }

  209.   /* Handle remote files.  */
  210.   if (remote_filename_p (temp_pathname))
  211.     {
  212.       *fd = -1;
  213.       do_cleanups (old_chain);
  214.       return temp_pathname;
  215.     }

  216.   /* Now see if we can open it.  */
  217.   found_file = gdb_open_cloexec (temp_pathname, O_RDONLY | O_BINARY, 0);
  218.   if (found_file < 0)
  219.     xfree (temp_pathname);

  220.   /* If the search in gdb_sysroot failed, and the path name has a
  221.      drive spec (e.g, c:/foo), try stripping ':' from the drive spec,
  222.      and retrying in the sysroot:
  223.        c:/foo/bar.dll ==> /sysroot/c/foo/bar.dll.  */

  224.   if (found_file < 0
  225.       && !gdb_sysroot_is_empty
  226.       && HAS_TARGET_DRIVE_SPEC (fskind, in_pathname))
  227.     {
  228.       int need_dir_separator = !IS_DIR_SEPARATOR (in_pathname[2]);
  229.       char *drive = savestring (in_pathname, 1);

  230.       temp_pathname = concat (sysroot,
  231.                               SLASH_STRING,
  232.                               drive,
  233.                               need_dir_separator ? SLASH_STRING : "",
  234.                               in_pathname + 2, (char *) NULL);
  235.       xfree (drive);

  236.       found_file = gdb_open_cloexec (temp_pathname, O_RDONLY | O_BINARY, 0);
  237.       if (found_file < 0)
  238.         {
  239.           xfree (temp_pathname);

  240.           /* If the search in gdb_sysroot still failed, try fully
  241.              stripping the drive spec, and trying once more in the
  242.              sysroot before giving up.

  243.              c:/foo/bar.dll ==> /sysroot/foo/bar.dll.  */

  244.           temp_pathname = concat (sysroot,
  245.                                   need_dir_separator ? SLASH_STRING : "",
  246.                                   in_pathname + 2, (char *) NULL);

  247.           found_file = gdb_open_cloexec (temp_pathname, O_RDONLY | O_BINARY, 0);
  248.           if (found_file < 0)
  249.             xfree (temp_pathname);
  250.         }
  251.     }

  252.   do_cleanups (old_chain);

  253.   /* We try to find the library in various ways.  After each attempt,
  254.      either found_file >= 0 and temp_pathname is a malloc'd string, or
  255.      found_file < 0 and temp_pathname does not point to storage that
  256.      needs to be freed.  */

  257.   if (found_file < 0)
  258.     temp_pathname = NULL;

  259.   /* If the search in gdb_sysroot failed, and the path name is
  260.      absolute at this point, make it relative.  (openp will try and open the
  261.      file according to its absolute path otherwise, which is not what we want.)
  262.      Affects subsequent searches for this solib.  */
  263.   if (found_file < 0 && IS_TARGET_ABSOLUTE_PATH (fskind, in_pathname))
  264.     {
  265.       /* First, get rid of any drive letters etc.  */
  266.       while (!IS_TARGET_DIR_SEPARATOR (fskind, *in_pathname))
  267.         in_pathname++;

  268.       /* Next, get rid of all leading dir separators.  */
  269.       while (IS_TARGET_DIR_SEPARATOR (fskind, *in_pathname))
  270.         in_pathname++;
  271.     }

  272.   /* If not found, search the solib_search_path (if any).  */
  273.   if (found_file < 0 && solib_search_path != NULL)
  274.     found_file = openp (solib_search_path,
  275.                         OPF_TRY_CWD_FIRST | OPF_RETURN_REALPATH,
  276.                         in_pathname, O_RDONLY | O_BINARY, &temp_pathname);

  277.   /* If not found, next search the solib_search_path (if any) for the basename
  278.      only (ignoring the path).  This is to allow reading solibs from a path
  279.      that differs from the opened path.  */
  280.   if (found_file < 0 && solib_search_path != NULL)
  281.     found_file = openp (solib_search_path,
  282.                         OPF_TRY_CWD_FIRST | OPF_RETURN_REALPATH,
  283.                         target_lbasename (fskind, in_pathname),
  284.                         O_RDONLY | O_BINARY, &temp_pathname);

  285.   /* If not found, try to use target supplied solib search method.  */
  286.   if (found_file < 0 && ops->find_and_open_solib)
  287.     found_file = ops->find_and_open_solib (in_pathname, O_RDONLY | O_BINARY,
  288.                                            &temp_pathname);

  289.   /* If not found, next search the inferior's $PATH environment variable.  */
  290.   if (found_file < 0 && gdb_sysroot_is_empty)
  291.     found_file = openp (get_in_environ (current_inferior ()->environment,
  292.                                         "PATH"),
  293.                         OPF_TRY_CWD_FIRST | OPF_RETURN_REALPATH, in_pathname,
  294.                         O_RDONLY | O_BINARY, &temp_pathname);

  295.   /* If not found, next search the inferior's $LD_LIBRARY_PATH
  296.      environment variable.  */
  297.   if (found_file < 0 && gdb_sysroot_is_empty)
  298.     found_file = openp (get_in_environ (current_inferior ()->environment,
  299.                                         "LD_LIBRARY_PATH"),
  300.                         OPF_TRY_CWD_FIRST | OPF_RETURN_REALPATH, in_pathname,
  301.                         O_RDONLY | O_BINARY, &temp_pathname);

  302.   *fd = found_file;
  303.   return temp_pathname;
  304. }

  305. /* Open and return a BFD for the shared library PATHNAME.  If FD is not -1,
  306.    it is used as file handle to open the file.  Throws an error if the file
  307.    could not be opened.  Handles both local and remote file access.

  308.    PATHNAME must be malloc'ed by the caller.  It will be freed by this
  309.    function.  If unsuccessful, the FD will be closed (unless FD was
  310.    -1).  */

  311. bfd *
  312. solib_bfd_fopen (char *pathname, int fd)
  313. {
  314.   bfd *abfd;

  315.   if (remote_filename_p (pathname))
  316.     {
  317.       gdb_assert (fd == -1);
  318.       abfd = remote_bfd_open (pathname, gnutarget);
  319.     }
  320.   else
  321.     {
  322.       abfd = gdb_bfd_open (pathname, gnutarget, fd);

  323.       if (abfd)
  324.         bfd_set_cacheable (abfd, 1);
  325.     }

  326.   if (!abfd)
  327.     {
  328.       make_cleanup (xfree, pathname);
  329.       error (_("Could not open `%s' as an executable file: %s"),
  330.              pathname, bfd_errmsg (bfd_get_error ()));
  331.     }

  332.   xfree (pathname);

  333.   return abfd;
  334. }

  335. /* Find shared library PATHNAME and open a BFD for it.  */

  336. bfd *
  337. solib_bfd_open (char *pathname)
  338. {
  339.   char *found_pathname;
  340.   int found_file;
  341.   bfd *abfd;
  342.   const struct bfd_arch_info *b;

  343.   /* Search for shared library file.  */
  344.   found_pathname = solib_find (pathname, &found_file);
  345.   if (found_pathname == NULL)
  346.     {
  347.       /* Return failure if the file could not be found, so that we can
  348.          accumulate messages about missing libraries.  */
  349.       if (errno == ENOENT)
  350.         return NULL;

  351.       perror_with_name (pathname);
  352.     }

  353.   /* Open bfd for shared library.  */
  354.   abfd = solib_bfd_fopen (found_pathname, found_file);

  355.   /* Check bfd format.  */
  356.   if (!bfd_check_format (abfd, bfd_object))
  357.     {
  358.       make_cleanup_bfd_unref (abfd);
  359.       error (_("`%s': not in executable format: %s"),
  360.              bfd_get_filename (abfd), bfd_errmsg (bfd_get_error ()));
  361.     }

  362.   /* Check bfd arch.  */
  363.   b = gdbarch_bfd_arch_info (target_gdbarch ());
  364.   if (!b->compatible (b, bfd_get_arch_info (abfd)))
  365.     warning (_("`%s': Shared library architecture %s is not compatible "
  366.                "with target architecture %s."), bfd_get_filename (abfd),
  367.              bfd_get_arch_info (abfd)->printable_name, b->printable_name);

  368.   return abfd;
  369. }

  370. /* Given a pointer to one of the shared objects in our list of mapped
  371.    objects, use the recorded name to open a bfd descriptor for the
  372.    object, build a section table, relocate all the section addresses
  373.    by the base address at which the shared object was mapped, and then
  374.    add the sections to the target's section table.

  375.    FIXME: In most (all?) cases the shared object file name recorded in
  376.    the dynamic linkage tables will be a fully qualified pathname.  For
  377.    cases where it isn't, do we really mimic the systems search
  378.    mechanism correctly in the below code (particularly the tilde
  379.    expansion stuff?).  */

  380. static int
  381. solib_map_sections (struct so_list *so)
  382. {
  383.   const struct target_so_ops *ops = solib_ops (target_gdbarch ());
  384.   char *filename;
  385.   struct target_section *p;
  386.   struct cleanup *old_chain;
  387.   bfd *abfd;

  388.   filename = tilde_expand (so->so_name);
  389.   old_chain = make_cleanup (xfree, filename);
  390.   abfd = ops->bfd_open (filename);
  391.   do_cleanups (old_chain);

  392.   if (abfd == NULL)
  393.     return 0;

  394.   /* Leave bfd open, core_xfer_memory and "info files" need it.  */
  395.   so->abfd = abfd;

  396.   /* Copy the full path name into so_name, allowing symbol_file_add
  397.      to find it later.  This also affects the =library-loaded GDB/MI
  398.      event, and in particular the part of that notification providing
  399.      the library's host-side path.  If we let the target dictate
  400.      that objfile's path, and the target is different from the host,
  401.      GDB/MI will not provide the correct host-side path.  */
  402.   if (strlen (bfd_get_filename (abfd)) >= SO_NAME_MAX_PATH_SIZE)
  403.     error (_("Shared library file name is too long."));
  404.   strcpy (so->so_name, bfd_get_filename (abfd));

  405.   if (build_section_table (abfd, &so->sections, &so->sections_end))
  406.     {
  407.       error (_("Can't find the file sections in `%s': %s"),
  408.              bfd_get_filename (abfd), bfd_errmsg (bfd_get_error ()));
  409.     }

  410.   for (p = so->sections; p < so->sections_end; p++)
  411.     {
  412.       /* Relocate the section binding addresses as recorded in the shared
  413.          object's file by the base address to which the object was actually
  414.          mapped.  */
  415.       ops->relocate_section_addresses (so, p);

  416.       /* If the target didn't provide information about the address
  417.          range of the shared object, assume we want the location of
  418.          the .text section.  */
  419.       if (so->addr_low == 0 && so->addr_high == 0
  420.           && strcmp (p->the_bfd_section->name, ".text") == 0)
  421.         {
  422.           so->addr_low = p->addr;
  423.           so->addr_high = p->endaddr;
  424.         }
  425.     }

  426.   /* Add the shared object's sections to the current set of file
  427.      section tables.  Do this immediately after mapping the object so
  428.      that later nodes in the list can query this object, as is needed
  429.      in solib-osf.c.  */
  430.   add_target_sections (so, so->sections, so->sections_end);

  431.   return 1;
  432. }

  433. /* Free symbol-file related contents of SO and reset for possible reloading
  434.    of SO.  If we have opened a BFD for SO, close it.  If we have placed SO's
  435.    sections in some target's section table, the caller is responsible for
  436.    removing them.

  437.    This function doesn't mess with objfiles at all.  If there is an
  438.    objfile associated with SO that needs to be removed, the caller is
  439.    responsible for taking care of that.  */

  440. static void
  441. clear_so (struct so_list *so)
  442. {
  443.   const struct target_so_ops *ops = solib_ops (target_gdbarch ());

  444.   if (so->sections)
  445.     {
  446.       xfree (so->sections);
  447.       so->sections = so->sections_end = NULL;
  448.     }

  449.   gdb_bfd_unref (so->abfd);
  450.   so->abfd = NULL;

  451.   /* Our caller closed the objfile, possibly via objfile_purge_solibs.  */
  452.   so->symbols_loaded = 0;
  453.   so->objfile = NULL;

  454.   so->addr_low = so->addr_high = 0;

  455.   /* Restore the target-supplied file name.  SO_NAME may be the path
  456.      of the symbol file.  */
  457.   strcpy (so->so_name, so->so_original_name);

  458.   /* Do the same for target-specific data.  */
  459.   if (ops->clear_so != NULL)
  460.     ops->clear_so (so);
  461. }

  462. /* Free the storage associated with the `struct so_list' object SO.
  463.    If we have opened a BFD for SO, close it.

  464.    The caller is responsible for removing SO from whatever list it is
  465.    a member of.  If we have placed SO's sections in some target's
  466.    section table, the caller is responsible for removing them.

  467.    This function doesn't mess with objfiles at all.  If there is an
  468.    objfile associated with SO that needs to be removed, the caller is
  469.    responsible for taking care of that.  */

  470. void
  471. free_so (struct so_list *so)
  472. {
  473.   const struct target_so_ops *ops = solib_ops (target_gdbarch ());

  474.   clear_so (so);
  475.   ops->free_so (so);

  476.   xfree (so);
  477. }


  478. /* Return address of first so_list entry in master shared object list.  */
  479. struct so_list *
  480. master_so_list (void)
  481. {
  482.   return so_list_head;
  483. }

  484. /* Read in symbols for shared object SO.  If SYMFILE_VERBOSE is set in FLAGS,
  485.    be chatty about it.  Return non-zero if any symbols were actually
  486.    loaded.  */

  487. int
  488. solib_read_symbols (struct so_list *so, int flags)
  489. {
  490.   if (so->symbols_loaded)
  491.     {
  492.       /* If needed, we've already warned in our caller.  */
  493.     }
  494.   else if (so->abfd == NULL)
  495.     {
  496.       /* We've already warned about this library, when trying to open
  497.          it.  */
  498.     }
  499.   else
  500.     {
  501.       volatile struct gdb_exception e;

  502.       flags |= current_inferior ()->symfile_flags;

  503.       TRY_CATCH (e, RETURN_MASK_ERROR)
  504.         {
  505.           struct section_addr_info *sap;

  506.           /* Have we already loaded this shared object?  */
  507.           ALL_OBJFILES (so->objfile)
  508.             {
  509.               if (filename_cmp (objfile_name (so->objfile), so->so_name) == 0
  510.                   && so->objfile->addr_low == so->addr_low)
  511.                 break;
  512.             }
  513.           if (so->objfile != NULL)
  514.             break;

  515.           sap = build_section_addr_info_from_section_table (so->sections,
  516.                                                             so->sections_end);
  517.           so->objfile = symbol_file_add_from_bfd (so->abfd, so->so_name,
  518.                                                   flags, sap, OBJF_SHARED,
  519.                                                   NULL);
  520.           so->objfile->addr_low = so->addr_low;
  521.           free_section_addr_info (sap);
  522.         }

  523.       if (e.reason < 0)
  524.         exception_fprintf (gdb_stderr, e, _("Error while reading shared"
  525.                                             " library symbols for %s:\n"),
  526.                            so->so_name);
  527.       else
  528.         so->symbols_loaded = 1;
  529.       return 1;
  530.     }

  531.   return 0;
  532. }

  533. /* Return 1 if KNOWN->objfile is used by any other so_list object in the
  534.    SO_LIST_HEAD list.  Return 0 otherwise.  */

  535. static int
  536. solib_used (const struct so_list *const known)
  537. {
  538.   const struct so_list *pivot;

  539.   for (pivot = so_list_head; pivot != NULL; pivot = pivot->next)
  540.     if (pivot != known && pivot->objfile == known->objfile)
  541.       return 1;
  542.   return 0;
  543. }

  544. /* Synchronize GDB's shared object list with inferior's.

  545.    Extract the list of currently loaded shared objects from the
  546.    inferior, and compare it with the list of shared objects currently
  547.    in GDB's so_list_head list.  Edit so_list_head to bring it in sync
  548.    with the inferior's new list.

  549.    If we notice that the inferior has unloaded some shared objects,
  550.    free any symbolic info GDB had read about those shared objects.

  551.    Don't load symbolic info for any new shared objects; just add them
  552.    to the list, and leave their symbols_loaded flag clear.

  553.    If FROM_TTY is non-null, feel free to print messages about what
  554.    we're doing.

  555.    If TARGET is non-null, add the sections of all new shared objects
  556.    to TARGET's section table.  Note that this doesn't remove any
  557.    sections for shared objects that have been unloaded, and it
  558.    doesn't check to see if the new shared objects are already present in
  559.    the section table.  But we only use this for core files and
  560.    processes we've just attached to, so that's okay.  */

  561. static void
  562. update_solib_list (int from_tty, struct target_ops *target)
  563. {
  564.   const struct target_so_ops *ops = solib_ops (target_gdbarch ());
  565.   struct so_list *inferior = ops->current_sos();
  566.   struct so_list *gdb, **gdb_link;

  567.   /* We can reach here due to changing solib-search-path or the
  568.      sysroot, before having any inferior.  */
  569.   if (target_has_execution && !ptid_equal (inferior_ptid, null_ptid))
  570.     {
  571.       struct inferior *inf = current_inferior ();

  572.       /* If we are attaching to a running process for which we
  573.          have not opened a symbol file, we may be able to get its
  574.          symbols now!  */
  575.       if (inf->attach_flag && symfile_objfile == NULL)
  576.         catch_errors (ops->open_symbol_file_object, &from_tty,
  577.                       "Error reading attached process's symbol file.\n",
  578.                       RETURN_MASK_ALL);
  579.     }

  580.   /* GDB and the inferior's dynamic linker each maintain their own
  581.      list of currently loaded shared objects; we want to bring the
  582.      former in sync with the latter.  Scan both lists, seeing which
  583.      shared objects appear where.  There are three cases:

  584.      - A shared object appears on both lists.  This means that GDB
  585.      knows about it already, and it's still loaded in the inferior.
  586.      Nothing needs to happen.

  587.      - A shared object appears only on GDB's list.  This means that
  588.      the inferior has unloaded it.  We should remove the shared
  589.      object from GDB's tables.

  590.      - A shared object appears only on the inferior's list.  This
  591.      means that it's just been loaded.  We should add it to GDB's
  592.      tables.

  593.      So we walk GDB's list, checking each entry to see if it appears
  594.      in the inferior's list too.  If it does, no action is needed, and
  595.      we remove it from the inferior's list.  If it doesn't, the
  596.      inferior has unloaded it, and we remove it from GDB's list.  By
  597.      the time we're done walking GDB's list, the inferior's list
  598.      contains only the new shared objects, which we then add.  */

  599.   gdb = so_list_head;
  600.   gdb_link = &so_list_head;
  601.   while (gdb)
  602.     {
  603.       struct so_list *i = inferior;
  604.       struct so_list **i_link = &inferior;

  605.       /* Check to see whether the shared object *gdb also appears in
  606.          the inferior's current list.  */
  607.       while (i)
  608.         {
  609.           if (ops->same)
  610.             {
  611.               if (ops->same (gdb, i))
  612.                 break;
  613.             }
  614.           else
  615.             {
  616.               if (! filename_cmp (gdb->so_original_name, i->so_original_name))
  617.                 break;
  618.             }

  619.           i_link = &i->next;
  620.           i = *i_link;
  621.         }

  622.       /* If the shared object appears on the inferior's list too, then
  623.          it's still loaded, so we don't need to do anything.  Delete
  624.          it from the inferior's list, and leave it on GDB's list.  */
  625.       if (i)
  626.         {
  627.           *i_link = i->next;
  628.           free_so (i);
  629.           gdb_link = &gdb->next;
  630.           gdb = *gdb_link;
  631.         }

  632.       /* If it's not on the inferior's list, remove it from GDB's tables.  */
  633.       else
  634.         {
  635.           /* Notify any observer that the shared object has been
  636.              unloaded before we remove it from GDB's tables.  */
  637.           observer_notify_solib_unloaded (gdb);

  638.           VEC_safe_push (char_ptr, current_program_space->deleted_solibs,
  639.                          xstrdup (gdb->so_name));

  640.           *gdb_link = gdb->next;

  641.           /* Unless the user loaded it explicitly, free SO's objfile.  */
  642.           if (gdb->objfile && ! (gdb->objfile->flags & OBJF_USERLOADED)
  643.               && !solib_used (gdb))
  644.             free_objfile (gdb->objfile);

  645.           /* Some targets' section tables might be referring to
  646.              sections from so->abfd; remove them.  */
  647.           remove_target_sections (gdb);

  648.           free_so (gdb);
  649.           gdb = *gdb_link;
  650.         }
  651.     }

  652.   /* Now the inferior's list contains only shared objects that don't
  653.      appear in GDB's list --- those that are newly loaded.  Add them
  654.      to GDB's shared object list.  */
  655.   if (inferior)
  656.     {
  657.       int not_found = 0;
  658.       const char *not_found_filename = NULL;

  659.       struct so_list *i;

  660.       /* Add the new shared objects to GDB's list.  */
  661.       *gdb_link = inferior;

  662.       /* Fill in the rest of each of the `struct so_list' nodes.  */
  663.       for (i = inferior; i; i = i->next)
  664.         {
  665.           volatile struct gdb_exception e;

  666.           i->pspace = current_program_space;
  667.           VEC_safe_push (so_list_ptr, current_program_space->added_solibs, i);

  668.           TRY_CATCH (e, RETURN_MASK_ERROR)
  669.             {
  670.               /* Fill in the rest of the `struct so_list' node.  */
  671.               if (!solib_map_sections (i))
  672.                 {
  673.                   not_found++;
  674.                   if (not_found_filename == NULL)
  675.                     not_found_filename = i->so_original_name;
  676.                 }
  677.             }

  678.           if (e.reason < 0)
  679.             exception_fprintf (gdb_stderr, e,
  680.                                _("Error while mapping shared "
  681.                                  "library sections:\n"));

  682.           /* Notify any observer that the shared object has been
  683.              loaded now that we've added it to GDB's tables.  */
  684.           observer_notify_solib_loaded (i);
  685.         }

  686.       /* If a library was not found, issue an appropriate warning
  687.          message.  We have to use a single call to warning in case the
  688.          front end does something special with warnings, e.g., pop up
  689.          a dialog box.  It Would Be Nice if we could get a "warning: "
  690.          prefix on each line in the CLI front end, though - it doesn't
  691.          stand out well.  */

  692.       if (not_found == 1)
  693.         warning (_("Could not load shared library symbols for %s.\n"
  694.                    "Do you need \"set solib-search-path\" "
  695.                    "or \"set sysroot\"?"),
  696.                  not_found_filename);
  697.       else if (not_found > 1)
  698.         warning (_("\
  699. Could not load shared library symbols for %d libraries, e.g. %s.\n\
  700. Use the \"info sharedlibrary\" command to see the complete listing.\n\
  701. Do you need \"set solib-search-path\" or \"set sysroot\"?"),
  702.                  not_found, not_found_filename);
  703.     }
  704. }


  705. /* Return non-zero if NAME is the libpthread shared library.

  706.    Uses a fairly simplistic heuristic approach where we check
  707.    the file name against "/libpthread".  This can lead to false
  708.    positives, but this should be good enough in practice.  */

  709. int
  710. libpthread_name_p (const char *name)
  711. {
  712.   return (strstr (name, "/libpthread") != NULL);
  713. }

  714. /* Return non-zero if SO is the libpthread shared library.  */

  715. static int
  716. libpthread_solib_p (struct so_list *so)
  717. {
  718.   return libpthread_name_p (so->so_name);
  719. }

  720. /* Read in symbolic information for any shared objects whose names
  721.    match PATTERN.  (If we've already read a shared object's symbol
  722.    info, leave it alone.)  If PATTERN is zero, read them all.

  723.    If READSYMS is 0, defer reading symbolic information until later
  724.    but still do any needed low level processing.

  725.    FROM_TTY and TARGET are as described for update_solib_list, above.  */

  726. void
  727. solib_add (const char *pattern, int from_tty,
  728.            struct target_ops *target, int readsyms)
  729. {
  730.   struct so_list *gdb;

  731.   if (print_symbol_loading_p (from_tty, 0, 0))
  732.     {
  733.       if (pattern != NULL)
  734.         {
  735.           printf_unfiltered (_("Loading symbols for shared libraries: %s\n"),
  736.                              pattern);
  737.         }
  738.       else
  739.         printf_unfiltered (_("Loading symbols for shared libraries.\n"));
  740.     }

  741.   current_program_space->solib_add_generation++;

  742.   if (pattern)
  743.     {
  744.       char *re_err = re_comp (pattern);

  745.       if (re_err)
  746.         error (_("Invalid regexp: %s"), re_err);
  747.     }

  748.   update_solib_list (from_tty, target);

  749.   /* Walk the list of currently loaded shared libraries, and read
  750.      symbols for any that match the pattern --- or any whose symbols
  751.      aren't already loaded, if no pattern was given.  */
  752.   {
  753.     int any_matches = 0;
  754.     int loaded_any_symbols = 0;
  755.     const int flags =
  756.         SYMFILE_DEFER_BP_RESET | (from_tty ? SYMFILE_VERBOSE : 0);

  757.     for (gdb = so_list_head; gdb; gdb = gdb->next)
  758.       if (! pattern || re_exec (gdb->so_name))
  759.         {
  760.           /* Normally, we would read the symbols from that library
  761.              only if READSYMS is set.  However, we're making a small
  762.              exception for the pthread library, because we sometimes
  763.              need the library symbols to be loaded in order to provide
  764.              thread support (x86-linux for instance).  */
  765.           const int add_this_solib =
  766.             (readsyms || libpthread_solib_p (gdb));

  767.           any_matches = 1;
  768.           if (add_this_solib)
  769.             {
  770.               if (gdb->symbols_loaded)
  771.                 {
  772.                   /* If no pattern was given, be quiet for shared
  773.                      libraries we have already loaded.  */
  774.                   if (pattern && (from_tty || info_verbose))
  775.                     printf_unfiltered (_("Symbols already loaded for %s\n"),
  776.                                        gdb->so_name);
  777.                 }
  778.               else if (solib_read_symbols (gdb, flags))
  779.                 loaded_any_symbols = 1;
  780.             }
  781.         }

  782.     if (loaded_any_symbols)
  783.       breakpoint_re_set ();

  784.     if (from_tty && pattern && ! any_matches)
  785.       printf_unfiltered
  786.         ("No loaded shared libraries match the pattern `%s'.\n", pattern);

  787.     if (loaded_any_symbols)
  788.       {
  789.         const struct target_so_ops *ops = solib_ops (target_gdbarch ());

  790.         /* Getting new symbols may change our opinion about what is
  791.            frameless.  */
  792.         reinit_frame_cache ();

  793.         ops->special_symbol_handling ();
  794.       }
  795.   }
  796. }

  797. /* Implement the "info sharedlibrary" command.  Walk through the
  798.    shared library list and print information about each attached
  799.    library matching PATTERN.  If PATTERN is elided, print them
  800.    all.  */

  801. static void
  802. info_sharedlibrary_command (char *pattern, int from_tty)
  803. {
  804.   struct so_list *so = NULL;        /* link map state variable */
  805.   int so_missing_debug_info = 0;
  806.   int addr_width;
  807.   int nr_libs;
  808.   struct cleanup *table_cleanup;
  809.   struct gdbarch *gdbarch = target_gdbarch ();
  810.   struct ui_out *uiout = current_uiout;

  811.   if (pattern)
  812.     {
  813.       char *re_err = re_comp (pattern);

  814.       if (re_err)
  815.         error (_("Invalid regexp: %s"), re_err);
  816.     }

  817.   /* "0x", a little whitespace, and two hex digits per byte of pointers.  */
  818.   addr_width = 4 + (gdbarch_ptr_bit (gdbarch) / 4);

  819.   update_solib_list (from_tty, 0);

  820.   /* make_cleanup_ui_out_table_begin_end needs to know the number of
  821.      rows, so we need to make two passes over the libs.  */

  822.   for (nr_libs = 0, so = so_list_head; so; so = so->next)
  823.     {
  824.       if (so->so_name[0])
  825.         {
  826.           if (pattern && ! re_exec (so->so_name))
  827.             continue;
  828.           ++nr_libs;
  829.         }
  830.     }

  831.   table_cleanup =
  832.     make_cleanup_ui_out_table_begin_end (uiout, 4, nr_libs,
  833.                                          "SharedLibraryTable");

  834.   /* The "- 1" is because ui_out adds one space between columns.  */
  835.   ui_out_table_header (uiout, addr_width - 1, ui_left, "from", "From");
  836.   ui_out_table_header (uiout, addr_width - 1, ui_left, "to", "To");
  837.   ui_out_table_header (uiout, 12 - 1, ui_left, "syms-read", "Syms Read");
  838.   ui_out_table_header (uiout, 0, ui_noalign,
  839.                        "name", "Shared Object Library");

  840.   ui_out_table_body (uiout);

  841.   for (so = so_list_head; so; so = so->next)
  842.     {
  843.       struct cleanup *lib_cleanup;

  844.       if (! so->so_name[0])
  845.         continue;
  846.       if (pattern && ! re_exec (so->so_name))
  847.         continue;

  848.       lib_cleanup = make_cleanup_ui_out_tuple_begin_end (uiout, "lib");

  849.       if (so->addr_high != 0)
  850.         {
  851.           ui_out_field_core_addr (uiout, "from", gdbarch, so->addr_low);
  852.           ui_out_field_core_addr (uiout, "to", gdbarch, so->addr_high);
  853.         }
  854.       else
  855.         {
  856.           ui_out_field_skip (uiout, "from");
  857.           ui_out_field_skip (uiout, "to");
  858.         }

  859.       if (! ui_out_is_mi_like_p (interp_ui_out (top_level_interpreter ()))
  860.           && so->symbols_loaded
  861.           && !objfile_has_symbols (so->objfile))
  862.         {
  863.           so_missing_debug_info = 1;
  864.           ui_out_field_string (uiout, "syms-read", "Yes (*)");
  865.         }
  866.       else
  867.         ui_out_field_string (uiout, "syms-read",
  868.                              so->symbols_loaded ? "Yes" : "No");

  869.       ui_out_field_string (uiout, "name", so->so_name);

  870.       ui_out_text (uiout, "\n");

  871.       do_cleanups (lib_cleanup);
  872.     }

  873.   do_cleanups (table_cleanup);

  874.   if (nr_libs == 0)
  875.     {
  876.       if (pattern)
  877.         ui_out_message (uiout, 0,
  878.                         _("No shared libraries matched.\n"));
  879.       else
  880.         ui_out_message (uiout, 0,
  881.                         _("No shared libraries loaded at this time.\n"));
  882.     }
  883.   else
  884.     {
  885.       if (so_missing_debug_info)
  886.         ui_out_message (uiout, 0,
  887.                         _("(*): Shared library is missing "
  888.                           "debugging information.\n"));
  889.     }
  890. }

  891. /* Return 1 if ADDRESS lies within SOLIB.  */

  892. int
  893. solib_contains_address_p (const struct so_list *const solib,
  894.                           CORE_ADDR address)
  895. {
  896.   struct target_section *p;

  897.   for (p = solib->sections; p < solib->sections_end; p++)
  898.     if (p->addr <= address && address < p->endaddr)
  899.       return 1;

  900.   return 0;
  901. }

  902. /* If ADDRESS is in a shared lib in program space PSPACE, return its
  903.    name.

  904.    Provides a hook for other gdb routines to discover whether or not a
  905.    particular address is within the mapped address space of a shared
  906.    library.

  907.    For example, this routine is called at one point to disable
  908.    breakpoints which are in shared libraries that are not currently
  909.    mapped in.  */

  910. char *
  911. solib_name_from_address (struct program_space *pspace, CORE_ADDR address)
  912. {
  913.   struct so_list *so = NULL;

  914.   for (so = pspace->so_list; so; so = so->next)
  915.     if (solib_contains_address_p (so, address))
  916.       return (so->so_name);

  917.   return (0);
  918. }

  919. /* Return whether the data starting at VADDR, size SIZE, must be kept
  920.    in a core file for shared libraries loaded before "gcore" is used
  921.    to be handled correctly when the core file is loaded.  This only
  922.    applies when the section would otherwise not be kept in the core
  923.    file (in particular, for readonly sections).  */

  924. int
  925. solib_keep_data_in_core (CORE_ADDR vaddr, unsigned long size)
  926. {
  927.   const struct target_so_ops *ops = solib_ops (target_gdbarch ());

  928.   if (ops->keep_data_in_core)
  929.     return ops->keep_data_in_core (vaddr, size);
  930.   else
  931.     return 0;
  932. }

  933. /* Called by free_all_symtabs */

  934. void
  935. clear_solib (void)
  936. {
  937.   const struct target_so_ops *ops = solib_ops (target_gdbarch ());

  938.   /* This function is expected to handle ELF shared libraries.  It is
  939.      also used on Solaris, which can run either ELF or a.out binaries
  940.      (for compatibility with SunOS 4), both of which can use shared
  941.      libraries.  So we don't know whether we have an ELF executable or
  942.      an a.out executable until the user chooses an executable file.

  943.      ELF shared libraries don't get mapped into the address space
  944.      until after the program starts, so we'd better not try to insert
  945.      breakpoints in them immediately.  We have to wait until the
  946.      dynamic linker has loaded them; we'll hit a bp_shlib_event
  947.      breakpoint (look for calls to create_solib_event_breakpoint) when
  948.      it's ready.

  949.      SunOS shared libraries seem to be different --- they're present
  950.      as soon as the process begins execution, so there's no need to
  951.      put off inserting breakpoints.  There's also nowhere to put a
  952.      bp_shlib_event breakpoint, so if we put it off, we'll never get
  953.      around to it.

  954.      So: disable breakpoints only if we're using ELF shared libs.  */
  955.   if (exec_bfd != NULL
  956.       && bfd_get_flavour (exec_bfd) != bfd_target_aout_flavour)
  957.     disable_breakpoints_in_shlibs ();

  958.   while (so_list_head)
  959.     {
  960.       struct so_list *so = so_list_head;

  961.       so_list_head = so->next;
  962.       observer_notify_solib_unloaded (so);
  963.       remove_target_sections (so);
  964.       free_so (so);
  965.     }

  966.   ops->clear_solib ();
  967. }

  968. /* Shared library startup support.  When GDB starts up the inferior,
  969.    it nurses it along (through the shell) until it is ready to execute
  970.    its first instruction.  At this point, this function gets
  971.    called.  */

  972. void
  973. solib_create_inferior_hook (int from_tty)
  974. {
  975.   const struct target_so_ops *ops = solib_ops (target_gdbarch ());

  976.   ops->solib_create_inferior_hook (from_tty);
  977. }

  978. /* Check to see if an address is in the dynamic loader's dynamic
  979.    symbol resolution code.  Return 1 if so, 0 otherwise.  */

  980. int
  981. in_solib_dynsym_resolve_code (CORE_ADDR pc)
  982. {
  983.   const struct target_so_ops *ops = solib_ops (target_gdbarch ());

  984.   return ops->in_dynsym_resolve_code (pc);
  985. }

  986. /* Implements the "sharedlibrary" command.  */

  987. static void
  988. sharedlibrary_command (char *args, int from_tty)
  989. {
  990.   dont_repeat ();
  991.   solib_add (args, from_tty, (struct target_ops *) 0, 1);
  992. }

  993. /* Implements the command "nosharedlibrary", which discards symbols
  994.    that have been auto-loaded from shared libraries.  Symbols from
  995.    shared libraries that were added by explicit request of the user
  996.    are not discarded.  Also called from remote.c.  */

  997. void
  998. no_shared_libraries (char *ignored, int from_tty)
  999. {
  1000.   /* The order of the two routines below is important: clear_solib notifies
  1001.      the solib_unloaded observers, and some of these observers might need
  1002.      access to their associated objfiles.  Therefore, we can not purge the
  1003.      solibs' objfiles before clear_solib has been called.  */

  1004.   clear_solib ();
  1005.   objfile_purge_solibs ();
  1006. }

  1007. /* See solib.h.  */

  1008. void
  1009. update_solib_breakpoints (void)
  1010. {
  1011.   const struct target_so_ops *ops = solib_ops (target_gdbarch ());

  1012.   if (ops->update_breakpoints != NULL)
  1013.     ops->update_breakpoints ();
  1014. }

  1015. /* See solib.h.  */

  1016. void
  1017. handle_solib_event (void)
  1018. {
  1019.   const struct target_so_ops *ops = solib_ops (target_gdbarch ());

  1020.   if (ops->handle_event != NULL)
  1021.     ops->handle_event ();

  1022.   clear_program_space_solib_cache (current_inferior ()->pspace);

  1023.   /* Check for any newly added shared libraries if we're supposed to
  1024.      be adding them automatically.  Switch terminal for any messages
  1025.      produced by breakpoint_re_set.  */
  1026.   target_terminal_ours_for_output ();
  1027.   solib_add (NULL, 0, &current_target, auto_solib_add);
  1028.   target_terminal_inferior ();
  1029. }

  1030. /* Reload shared libraries, but avoid reloading the same symbol file
  1031.    we already have loaded.  */

  1032. static void
  1033. reload_shared_libraries_1 (int from_tty)
  1034. {
  1035.   struct so_list *so;
  1036.   struct cleanup *old_chain = make_cleanup (null_cleanup, NULL);

  1037.   if (print_symbol_loading_p (from_tty, 0, 0))
  1038.     printf_unfiltered (_("Loading symbols for shared libraries.\n"));

  1039.   for (so = so_list_head; so != NULL; so = so->next)
  1040.     {
  1041.       char *filename, *found_pathname = NULL;
  1042.       bfd *abfd;
  1043.       int was_loaded = so->symbols_loaded;
  1044.       const int flags =
  1045.         SYMFILE_DEFER_BP_RESET | (from_tty ? SYMFILE_VERBOSE : 0);

  1046.       filename = tilde_expand (so->so_original_name);
  1047.       make_cleanup (xfree, filename);
  1048.       abfd = solib_bfd_open (filename);
  1049.       if (abfd != NULL)
  1050.         {
  1051.           found_pathname = xstrdup (bfd_get_filename (abfd));
  1052.           make_cleanup (xfree, found_pathname);
  1053.           gdb_bfd_unref (abfd);
  1054.         }

  1055.       /* If this shared library is no longer associated with its previous
  1056.          symbol file, close that.  */
  1057.       if ((found_pathname == NULL && was_loaded)
  1058.           || (found_pathname != NULL
  1059.               && filename_cmp (found_pathname, so->so_name) != 0))
  1060.         {
  1061.           if (so->objfile && ! (so->objfile->flags & OBJF_USERLOADED)
  1062.               && !solib_used (so))
  1063.             free_objfile (so->objfile);
  1064.           remove_target_sections (so);
  1065.           clear_so (so);
  1066.         }

  1067.       /* If this shared library is now associated with a new symbol
  1068.          file, open it.  */
  1069.       if (found_pathname != NULL
  1070.           && (!was_loaded
  1071.               || filename_cmp (found_pathname, so->so_name) != 0))
  1072.         {
  1073.           volatile struct gdb_exception e;

  1074.           TRY_CATCH (e, RETURN_MASK_ERROR)
  1075.             solib_map_sections (so);

  1076.           if (e.reason < 0)
  1077.             exception_fprintf (gdb_stderr, e,
  1078.                                _("Error while mapping "
  1079.                                  "shared library sections:\n"));
  1080.           else if (auto_solib_add || was_loaded || libpthread_solib_p (so))
  1081.             solib_read_symbols (so, flags);
  1082.         }
  1083.     }

  1084.   do_cleanups (old_chain);
  1085. }

  1086. static void
  1087. reload_shared_libraries (char *ignored, int from_tty,
  1088.                          struct cmd_list_element *e)
  1089. {
  1090.   const struct target_so_ops *ops;

  1091.   reload_shared_libraries_1 (from_tty);

  1092.   ops = solib_ops (target_gdbarch ());

  1093.   /* Creating inferior hooks here has two purposes.  First, if we reload
  1094.      shared libraries then the address of solib breakpoint we've computed
  1095.      previously might be no longer valid.  For example, if we forgot to set
  1096.      solib-absolute-prefix and are setting it right now, then the previous
  1097.      breakpoint address is plain wrong.  Second, installing solib hooks
  1098.      also implicitly figures were ld.so is and loads symbols for it.
  1099.      Absent this call, if we've just connected to a target and set
  1100.      solib-absolute-prefix or solib-search-path, we'll lose all information
  1101.      about ld.so.  */
  1102.   if (target_has_execution)
  1103.     {
  1104.       /* Reset or free private data structures not associated with
  1105.          so_list entries.  */
  1106.       ops->clear_solib ();

  1107.       /* Remove any previous solib event breakpoint.  This is usually
  1108.          done in common code, at breakpoint_init_inferior time, but
  1109.          we're not really starting up the inferior here.  */
  1110.       remove_solib_event_breakpoints ();

  1111.       solib_create_inferior_hook (from_tty);
  1112.     }

  1113.   /* Sometimes the platform-specific hook loads initial shared
  1114.      libraries, and sometimes it doesn't.  If it doesn't FROM_TTY will be
  1115.      incorrectly 0 but such solib targets should be fixed anyway.  If we
  1116.      made all the inferior hook methods consistent, this call could be
  1117.      removed.  Call it only after the solib target has been initialized by
  1118.      solib_create_inferior_hook.  */

  1119.   solib_add (NULL, 0, NULL, auto_solib_add);

  1120.   breakpoint_re_set ();

  1121.   /* We may have loaded or unloaded debug info for some (or all)
  1122.      shared libraries.  However, frames may still reference them.  For
  1123.      example, a frame's unwinder might still point at DWARF FDE
  1124.      structures that are now freed.  Also, getting new symbols may
  1125.      change our opinion about what is frameless.  */
  1126.   reinit_frame_cache ();

  1127.   ops->special_symbol_handling ();
  1128. }

  1129. static void
  1130. show_auto_solib_add (struct ui_file *file, int from_tty,
  1131.                      struct cmd_list_element *c, const char *value)
  1132. {
  1133.   fprintf_filtered (file, _("Autoloading of shared library symbols is %s.\n"),
  1134.                     value);
  1135. }


  1136. /* Handler for library-specific lookup of global symbol NAME in OBJFILE.  Call
  1137.    the library-specific handler if it is installed for the current target.  */

  1138. struct symbol *
  1139. solib_global_lookup (struct objfile *objfile,
  1140.                      const char *name,
  1141.                      const domain_enum domain)
  1142. {
  1143.   const struct target_so_ops *ops = solib_ops (get_objfile_arch (objfile));

  1144.   if (ops->lookup_lib_global_symbol != NULL)
  1145.     return ops->lookup_lib_global_symbol (objfile, name, domain);
  1146.   return NULL;
  1147. }

  1148. /* Lookup the value for a specific symbol from dynamic symbol table.  Look
  1149.    up symbol from ABFD.  MATCH_SYM is a callback function to determine
  1150.    whether to pick up a symbol.  DATA is the input of this callback
  1151.    function.  Return NULL if symbol is not found.  */

  1152. CORE_ADDR
  1153. gdb_bfd_lookup_symbol_from_symtab (bfd *abfd,
  1154.                                    int (*match_sym) (asymbol *, void *),
  1155.                                    void *data)
  1156. {
  1157.   long storage_needed = bfd_get_symtab_upper_bound (abfd);
  1158.   CORE_ADDR symaddr = 0;

  1159.   if (storage_needed > 0)
  1160.     {
  1161.       unsigned int i;

  1162.       asymbol **symbol_table = (asymbol **) xmalloc (storage_needed);
  1163.       struct cleanup *back_to = make_cleanup (xfree, symbol_table);
  1164.       unsigned int number_of_symbols =
  1165.         bfd_canonicalize_symtab (abfd, symbol_table);

  1166.       for (i = 0; i < number_of_symbols; i++)
  1167.         {
  1168.           asymbol *sym  = *symbol_table++;

  1169.           if (match_sym (sym, data))
  1170.             {
  1171.               struct gdbarch *gdbarch = target_gdbarch ();
  1172.               symaddr = sym->value;

  1173.               /* Some ELF targets fiddle with addresses of symbols they
  1174.                  consider special.  They use minimal symbols to do that
  1175.                  and this is needed for correct breakpoint placement,
  1176.                  but we do not have full data here to build a complete
  1177.                  minimal symbol, so just set the address and let the
  1178.                  targets cope with that.  */
  1179.               if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
  1180.                   && gdbarch_elf_make_msymbol_special_p (gdbarch))
  1181.                 {
  1182.                   struct minimal_symbol msym;

  1183.                   memset (&msym, 0, sizeof (msym));
  1184.                   SET_MSYMBOL_VALUE_ADDRESS (&msym, symaddr);
  1185.                   gdbarch_elf_make_msymbol_special (gdbarch, sym, &msym);
  1186.                   symaddr = MSYMBOL_VALUE_RAW_ADDRESS (&msym);
  1187.                 }

  1188.               /* BFD symbols are section relative.  */
  1189.               symaddr += sym->section->vma;
  1190.               break;
  1191.             }
  1192.         }
  1193.       do_cleanups (back_to);
  1194.     }

  1195.   return symaddr;
  1196. }

  1197. /* Lookup the value for a specific symbol from symbol table.  Look up symbol
  1198.    from ABFD.  MATCH_SYM is a callback function to determine whether to pick
  1199.    up a symbol.  DATA is the input of this callback function.  Return NULL
  1200.    if symbol is not found.  */

  1201. static CORE_ADDR
  1202. bfd_lookup_symbol_from_dyn_symtab (bfd *abfd,
  1203.                                    int (*match_sym) (asymbol *, void *),
  1204.                                    void *data)
  1205. {
  1206.   long storage_needed = bfd_get_dynamic_symtab_upper_bound (abfd);
  1207.   CORE_ADDR symaddr = 0;

  1208.   if (storage_needed > 0)
  1209.     {
  1210.       unsigned int i;
  1211.       asymbol **symbol_table = (asymbol **) xmalloc (storage_needed);
  1212.       struct cleanup *back_to = make_cleanup (xfree, symbol_table);
  1213.       unsigned int number_of_symbols =
  1214.         bfd_canonicalize_dynamic_symtab (abfd, symbol_table);

  1215.       for (i = 0; i < number_of_symbols; i++)
  1216.         {
  1217.           asymbol *sym = *symbol_table++;

  1218.           if (match_sym (sym, data))
  1219.             {
  1220.               /* BFD symbols are section relative.  */
  1221.               symaddr = sym->value + sym->section->vma;
  1222.               break;
  1223.             }
  1224.         }
  1225.       do_cleanups (back_to);
  1226.     }
  1227.   return symaddr;
  1228. }

  1229. /* Lookup the value for a specific symbol from symbol table and dynamic
  1230.    symbol table.  Look up symbol from ABFD.  MATCH_SYM is a callback
  1231.    function to determine whether to pick up a symbol.  DATA is the
  1232.    input of this callback function.  Return NULL if symbol is not
  1233.    found.  */

  1234. CORE_ADDR
  1235. gdb_bfd_lookup_symbol (bfd *abfd,
  1236.                        int (*match_sym) (asymbol *, void *),
  1237.                        void *data)
  1238. {
  1239.   CORE_ADDR symaddr = gdb_bfd_lookup_symbol_from_symtab (abfd, match_sym, data);

  1240.   /* On FreeBSD, the dynamic linker is stripped by default.  So we'll
  1241.      have to check the dynamic string table too.  */
  1242.   if (symaddr == 0)
  1243.     symaddr = bfd_lookup_symbol_from_dyn_symtab (abfd, match_sym, data);

  1244.   return symaddr;
  1245. }

  1246. /* SO_LIST_HEAD may contain user-loaded object files that can be removed
  1247.    out-of-band by the user.  So upon notification of free_objfile remove
  1248.    all references to any user-loaded file that is about to be freed.  */

  1249. static void
  1250. remove_user_added_objfile (struct objfile *objfile)
  1251. {
  1252.   struct so_list *so;

  1253.   if (objfile != 0 && objfile->flags & OBJF_USERLOADED)
  1254.     {
  1255.       for (so = so_list_head; so != NULL; so = so->next)
  1256.         if (so->objfile == objfile)
  1257.           so->objfile = NULL;
  1258.     }
  1259. }

  1260. extern initialize_file_ftype _initialize_solib; /* -Wmissing-prototypes */

  1261. void
  1262. _initialize_solib (void)
  1263. {
  1264.   solib_data = gdbarch_data_register_pre_init (solib_init);

  1265.   observer_attach_free_objfile (remove_user_added_objfile);

  1266.   add_com ("sharedlibrary", class_files, sharedlibrary_command,
  1267.            _("Load shared object library symbols for files matching REGEXP."));
  1268.   add_info ("sharedlibrary", info_sharedlibrary_command,
  1269.             _("Status of loaded shared object libraries."));
  1270.   add_com ("nosharedlibrary", class_files, no_shared_libraries,
  1271.            _("Unload all shared object library symbols."));

  1272.   add_setshow_boolean_cmd ("auto-solib-add", class_support,
  1273.                            &auto_solib_add, _("\
  1274. Set autoloading of shared library symbols."), _("\
  1275. Show autoloading of shared library symbols."), _("\
  1276. If \"on\", symbols from all shared object libraries will be loaded\n\
  1277. automatically when the inferior begins execution, when the dynamic linker\n\
  1278. informs gdb that a new library has been loaded, or when attaching to the\n\
  1279. inferior.  Otherwise, symbols must be loaded manually, using \
  1280. `sharedlibrary'."),
  1281.                            NULL,
  1282.                            show_auto_solib_add,
  1283.                            &setlist, &showlist);

  1284.   add_setshow_filename_cmd ("sysroot", class_support,
  1285.                             &gdb_sysroot, _("\
  1286. Set an alternate system root."), _("\
  1287. Show the current system root."), _("\
  1288. The system root is used to load absolute shared library symbol files.\n\
  1289. For other (relative) files, you can add directories using\n\
  1290. `set solib-search-path'."),
  1291.                             reload_shared_libraries,
  1292.                             NULL,
  1293.                             &setlist, &showlist);

  1294.   add_alias_cmd ("solib-absolute-prefix", "sysroot", class_support, 0,
  1295.                  &setlist);
  1296.   add_alias_cmd ("solib-absolute-prefix", "sysroot", class_support, 0,
  1297.                  &showlist);

  1298.   add_setshow_optional_filename_cmd ("solib-search-path", class_support,
  1299.                                      &solib_search_path, _("\
  1300. Set the search path for loading non-absolute shared library symbol files."),
  1301.                                      _("\
  1302. Show the search path for loading non-absolute shared library symbol files."),
  1303.                                      _("\
  1304. This takes precedence over the environment variables \
  1305. PATH and LD_LIBRARY_PATH."),
  1306.                                      reload_shared_libraries,
  1307.                                      show_solib_search_path,
  1308.                                      &setlist, &showlist);
  1309. }