GDB (API)
/home/stan/gdb/src/gdb/solib.c
Go to the documentation of this file.
00001 /* Handle shared libraries for GDB, the GNU Debugger.
00002 
00003    Copyright (C) 1990-2013 Free Software Foundation, Inc.
00004 
00005    This file is part of GDB.
00006 
00007    This program is free software; you can redistribute it and/or modify
00008    it under the terms of the GNU General Public License as published by
00009    the Free Software Foundation; either version 3 of the License, or
00010    (at your option) any later version.
00011 
00012    This program is distributed in the hope that it will be useful,
00013    but WITHOUT ANY WARRANTY; without even the implied warranty of
00014    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015    GNU General Public License for more details.
00016 
00017    You should have received a copy of the GNU General Public License
00018    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
00019 
00020 #include "defs.h"
00021 
00022 #include <sys/types.h>
00023 #include <fcntl.h>
00024 #include "gdb_string.h"
00025 #include "symtab.h"
00026 #include "bfd.h"
00027 #include "symfile.h"
00028 #include "objfiles.h"
00029 #include "exceptions.h"
00030 #include "gdbcore.h"
00031 #include "command.h"
00032 #include "target.h"
00033 #include "frame.h"
00034 #include "gdb_regex.h"
00035 #include "inferior.h"
00036 #include "environ.h"
00037 #include "language.h"
00038 #include "gdbcmd.h"
00039 #include "completer.h"
00040 #include "filenames.h"          /* for DOSish file names */
00041 #include "exec.h"
00042 #include "solist.h"
00043 #include "observer.h"
00044 #include "readline/readline.h"
00045 #include "remote.h"
00046 #include "solib.h"
00047 #include "interps.h"
00048 #include "filesystem.h"
00049 #include "gdb_bfd.h"
00050 #include "filestuff.h"
00051 
00052 /* Architecture-specific operations.  */
00053 
00054 /* Per-architecture data key.  */
00055 static struct gdbarch_data *solib_data;
00056 
00057 static void *
00058 solib_init (struct obstack *obstack)
00059 {
00060   struct target_so_ops **ops;
00061 
00062   ops = OBSTACK_ZALLOC (obstack, struct target_so_ops *);
00063   *ops = current_target_so_ops;
00064   return ops;
00065 }
00066 
00067 static const struct target_so_ops *
00068 solib_ops (struct gdbarch *gdbarch)
00069 {
00070   const struct target_so_ops **ops = gdbarch_data (gdbarch, solib_data);
00071 
00072   return *ops;
00073 }
00074 
00075 /* Set the solib operations for GDBARCH to NEW_OPS.  */
00076 
00077 void
00078 set_solib_ops (struct gdbarch *gdbarch, const struct target_so_ops *new_ops)
00079 {
00080   const struct target_so_ops **ops = gdbarch_data (gdbarch, solib_data);
00081 
00082   *ops = new_ops;
00083 }
00084 
00085 
00086 /* external data declarations */
00087 
00088 /* FIXME: gdbarch needs to control this variable, or else every
00089    configuration needs to call set_solib_ops.  */
00090 struct target_so_ops *current_target_so_ops;
00091 
00092 /* List of known shared objects */
00093 #define so_list_head current_program_space->so_list
00094 
00095 /* Local function prototypes */
00096 
00097 /* If non-empty, this is a search path for loading non-absolute shared library
00098    symbol files.  This takes precedence over the environment variables PATH
00099    and LD_LIBRARY_PATH.  */
00100 static char *solib_search_path = NULL;
00101 static void
00102 show_solib_search_path (struct ui_file *file, int from_tty,
00103                         struct cmd_list_element *c, const char *value)
00104 {
00105   fprintf_filtered (file, _("The search path for loading non-absolute "
00106                             "shared library symbol files is %s.\n"),
00107                     value);
00108 }
00109 
00110 /* Same as HAVE_DOS_BASED_FILE_SYSTEM, but useable as an rvalue.  */
00111 #if (HAVE_DOS_BASED_FILE_SYSTEM)
00112 #  define DOS_BASED_FILE_SYSTEM 1
00113 #else
00114 #  define DOS_BASED_FILE_SYSTEM 0
00115 #endif
00116 
00117 /* Returns the full pathname of the shared library file, or NULL if
00118    not found.  (The pathname is malloc'ed; it needs to be freed by the
00119    caller.)  *FD is set to either -1 or an open file handle for the
00120    library.
00121 
00122    Global variable GDB_SYSROOT is used as a prefix directory
00123    to search for shared libraries if they have an absolute path.
00124 
00125    Global variable SOLIB_SEARCH_PATH is used as a prefix directory
00126    (or set of directories, as in LD_LIBRARY_PATH) to search for all
00127    shared libraries if not found in GDB_SYSROOT.
00128 
00129    Search algorithm:
00130    * If there is a gdb_sysroot and path is absolute:
00131    *   Search for gdb_sysroot/path.
00132    * else
00133    *   Look for it literally (unmodified).
00134    * Look in SOLIB_SEARCH_PATH.
00135    * If available, use target defined search function.
00136    * If gdb_sysroot is NOT set, perform the following two searches:
00137    *   Look in inferior's $PATH.
00138    *   Look in inferior's $LD_LIBRARY_PATH.
00139    *
00140    * The last check avoids doing this search when targetting remote
00141    * machines since gdb_sysroot will almost always be set.
00142 */
00143 
00144 char *
00145 solib_find (char *in_pathname, int *fd)
00146 {
00147   const struct target_so_ops *ops = solib_ops (target_gdbarch ());
00148   int found_file = -1;
00149   char *temp_pathname = NULL;
00150   int gdb_sysroot_is_empty;
00151   const char *solib_symbols_extension
00152     = gdbarch_solib_symbols_extension (target_gdbarch ());
00153   const char *fskind = effective_target_file_system_kind ();
00154   struct cleanup *old_chain = make_cleanup (null_cleanup, NULL);
00155   char *sysroot = NULL;
00156 
00157   /* If solib_symbols_extension is set, replace the file's
00158      extension.  */
00159   if (solib_symbols_extension)
00160     {
00161       char *p = in_pathname + strlen (in_pathname);
00162 
00163       while (p > in_pathname && *p != '.')
00164         p--;
00165 
00166       if (*p == '.')
00167         {
00168           char *new_pathname;
00169 
00170           new_pathname = alloca (p - in_pathname + 1
00171                                  + strlen (solib_symbols_extension) + 1);
00172           memcpy (new_pathname, in_pathname, p - in_pathname + 1);
00173           strcpy (new_pathname + (p - in_pathname) + 1,
00174                   solib_symbols_extension);
00175 
00176           in_pathname = new_pathname;
00177         }
00178     }
00179 
00180   gdb_sysroot_is_empty = (gdb_sysroot == NULL || *gdb_sysroot == 0);
00181 
00182   if (!gdb_sysroot_is_empty)
00183     {
00184       int prefix_len = strlen (gdb_sysroot);
00185 
00186       /* Remove trailing slashes from absolute prefix.  */
00187       while (prefix_len > 0
00188              && IS_DIR_SEPARATOR (gdb_sysroot[prefix_len - 1]))
00189         prefix_len--;
00190 
00191       sysroot = savestring (gdb_sysroot, prefix_len);
00192       make_cleanup (xfree, sysroot);
00193     }
00194 
00195   /* If we're on a non-DOS-based system, backslashes won't be
00196      understood as directory separator, so, convert them to forward
00197      slashes, iff we're supposed to handle DOS-based file system
00198      semantics for target paths.  */
00199   if (!DOS_BASED_FILE_SYSTEM && fskind == file_system_kind_dos_based)
00200     {
00201       char *p;
00202 
00203       /* Avoid clobbering our input.  */
00204       p = alloca (strlen (in_pathname) + 1);
00205       strcpy (p, in_pathname);
00206       in_pathname = p;
00207 
00208       for (; *p; p++)
00209         {
00210           if (*p == '\\')
00211             *p = '/';
00212         }
00213     }
00214 
00215   /* Note, we're interested in IS_TARGET_ABSOLUTE_PATH, not
00216      IS_ABSOLUTE_PATH.  The latter is for host paths only, while
00217      IN_PATHNAME is a target path.  For example, if we're supposed to
00218      be handling DOS-like semantics we want to consider a
00219      'c:/foo/bar.dll' path as an absolute path, even on a Unix box.
00220      With such a path, before giving up on the sysroot, we'll try:
00221 
00222        1st attempt, c:/foo/bar.dll ==> /sysroot/c:/foo/bar.dll
00223        2nd attempt, c:/foo/bar.dll ==> /sysroot/c/foo/bar.dll
00224        3rd attempt, c:/foo/bar.dll ==> /sysroot/foo/bar.dll
00225   */
00226 
00227   if (!IS_TARGET_ABSOLUTE_PATH (fskind, in_pathname) || gdb_sysroot_is_empty)
00228     temp_pathname = xstrdup (in_pathname);
00229   else
00230     {
00231       int need_dir_separator;
00232 
00233       /* Concatenate the sysroot and the target reported filename.  We
00234          may need to glue them with a directory separator.  Cases to
00235          consider:
00236 
00237         | sysroot         | separator | in_pathname    |
00238         |-----------------+-----------+----------------|
00239         | /some/dir       | /         | c:/foo/bar.dll |
00240         | /some/dir       |           | /foo/bar.dll   |
00241         | remote:         |           | c:/foo/bar.dll |
00242         | remote:         |           | /foo/bar.dll   |
00243         | remote:some/dir | /         | c:/foo/bar.dll |
00244         | remote:some/dir |           | /foo/bar.dll   |
00245 
00246         IOW, we don't need to add a separator if IN_PATHNAME already
00247         has one, or when the the sysroot is exactly "remote:".
00248         There's no need to check for drive spec explicitly, as we only
00249         get here if IN_PATHNAME is considered an absolute path.  */
00250       need_dir_separator = !(IS_DIR_SEPARATOR (in_pathname[0])
00251                              || strcmp (REMOTE_SYSROOT_PREFIX, sysroot) == 0);
00252 
00253       /* Cat the prefixed pathname together.  */
00254       temp_pathname = concat (sysroot,
00255                               need_dir_separator ? SLASH_STRING : "",
00256                               in_pathname, (char *) NULL);
00257     }
00258 
00259   /* Handle remote files.  */
00260   if (remote_filename_p (temp_pathname))
00261     {
00262       *fd = -1;
00263       do_cleanups (old_chain);
00264       return temp_pathname;
00265     }
00266 
00267   /* Now see if we can open it.  */
00268   found_file = gdb_open_cloexec (temp_pathname, O_RDONLY | O_BINARY, 0);
00269   if (found_file < 0)
00270     xfree (temp_pathname);
00271 
00272   /* If the search in gdb_sysroot failed, and the path name has a
00273      drive spec (e.g, c:/foo), try stripping ':' from the drive spec,
00274      and retrying in the sysroot:
00275        c:/foo/bar.dll ==> /sysroot/c/foo/bar.dll.  */
00276 
00277   if (found_file < 0
00278       && !gdb_sysroot_is_empty
00279       && HAS_TARGET_DRIVE_SPEC (fskind, in_pathname))
00280     {
00281       int need_dir_separator = !IS_DIR_SEPARATOR (in_pathname[2]);
00282       char *drive = savestring (in_pathname, 1);
00283 
00284       temp_pathname = concat (sysroot,
00285                               SLASH_STRING,
00286                               drive,
00287                               need_dir_separator ? SLASH_STRING : "",
00288                               in_pathname + 2, (char *) NULL);
00289       xfree (drive);
00290 
00291       found_file = gdb_open_cloexec (temp_pathname, O_RDONLY | O_BINARY, 0);
00292       if (found_file < 0)
00293         {
00294           xfree (temp_pathname);
00295 
00296           /* If the search in gdb_sysroot still failed, try fully
00297              stripping the drive spec, and trying once more in the
00298              sysroot before giving up.
00299 
00300              c:/foo/bar.dll ==> /sysroot/foo/bar.dll.  */
00301 
00302           temp_pathname = concat (sysroot,
00303                                   need_dir_separator ? SLASH_STRING : "",
00304                                   in_pathname + 2, (char *) NULL);
00305 
00306           found_file = gdb_open_cloexec (temp_pathname, O_RDONLY | O_BINARY, 0);
00307           if (found_file < 0)
00308             xfree (temp_pathname);
00309         }
00310     }
00311 
00312   do_cleanups (old_chain);
00313 
00314   /* We try to find the library in various ways.  After each attempt,
00315      either found_file >= 0 and temp_pathname is a malloc'd string, or
00316      found_file < 0 and temp_pathname does not point to storage that
00317      needs to be freed.  */
00318 
00319   if (found_file < 0)
00320     temp_pathname = NULL;
00321 
00322   /* If the search in gdb_sysroot failed, and the path name is
00323      absolute at this point, make it relative.  (openp will try and open the
00324      file according to its absolute path otherwise, which is not what we want.)
00325      Affects subsequent searches for this solib.  */
00326   if (found_file < 0 && IS_TARGET_ABSOLUTE_PATH (fskind, in_pathname))
00327     {
00328       /* First, get rid of any drive letters etc.  */
00329       while (!IS_TARGET_DIR_SEPARATOR (fskind, *in_pathname))
00330         in_pathname++;
00331 
00332       /* Next, get rid of all leading dir separators.  */
00333       while (IS_TARGET_DIR_SEPARATOR (fskind, *in_pathname))
00334         in_pathname++;
00335     }
00336 
00337   /* If not found, search the solib_search_path (if any).  */
00338   if (found_file < 0 && solib_search_path != NULL)
00339     found_file = openp (solib_search_path,
00340                         OPF_TRY_CWD_FIRST | OPF_RETURN_REALPATH,
00341                         in_pathname, O_RDONLY | O_BINARY, &temp_pathname);
00342 
00343   /* If not found, next search the solib_search_path (if any) for the basename
00344      only (ignoring the path).  This is to allow reading solibs from a path
00345      that differs from the opened path.  */
00346   if (found_file < 0 && solib_search_path != NULL)
00347     found_file = openp (solib_search_path,
00348                         OPF_TRY_CWD_FIRST | OPF_RETURN_REALPATH,
00349                         target_lbasename (fskind, in_pathname),
00350                         O_RDONLY | O_BINARY, &temp_pathname);
00351 
00352   /* If not found, try to use target supplied solib search method.  */
00353   if (found_file < 0 && ops->find_and_open_solib)
00354     found_file = ops->find_and_open_solib (in_pathname, O_RDONLY | O_BINARY,
00355                                            &temp_pathname);
00356 
00357   /* If not found, next search the inferior's $PATH environment variable.  */
00358   if (found_file < 0 && gdb_sysroot_is_empty)
00359     found_file = openp (get_in_environ (current_inferior ()->environment,
00360                                         "PATH"),
00361                         OPF_TRY_CWD_FIRST | OPF_RETURN_REALPATH, in_pathname,
00362                         O_RDONLY | O_BINARY, &temp_pathname);
00363 
00364   /* If not found, next search the inferior's $LD_LIBRARY_PATH
00365      environment variable.  */
00366   if (found_file < 0 && gdb_sysroot_is_empty)
00367     found_file = openp (get_in_environ (current_inferior ()->environment,
00368                                         "LD_LIBRARY_PATH"),
00369                         OPF_TRY_CWD_FIRST | OPF_RETURN_REALPATH, in_pathname,
00370                         O_RDONLY | O_BINARY, &temp_pathname);
00371 
00372   *fd = found_file;
00373   return temp_pathname;
00374 }
00375 
00376 /* Open and return a BFD for the shared library PATHNAME.  If FD is not -1,
00377    it is used as file handle to open the file.  Throws an error if the file
00378    could not be opened.  Handles both local and remote file access.
00379 
00380    PATHNAME must be malloc'ed by the caller.  It will be freed by this
00381    function.  If unsuccessful, the FD will be closed (unless FD was
00382    -1).  */
00383 
00384 bfd *
00385 solib_bfd_fopen (char *pathname, int fd)
00386 {
00387   bfd *abfd;
00388 
00389   if (remote_filename_p (pathname))
00390     {
00391       gdb_assert (fd == -1);
00392       abfd = remote_bfd_open (pathname, gnutarget);
00393     }
00394   else
00395     {
00396       abfd = gdb_bfd_open (pathname, gnutarget, fd);
00397 
00398       if (abfd)
00399         bfd_set_cacheable (abfd, 1);
00400     }
00401 
00402   if (!abfd)
00403     {
00404       make_cleanup (xfree, pathname);
00405       error (_("Could not open `%s' as an executable file: %s"),
00406              pathname, bfd_errmsg (bfd_get_error ()));
00407     }
00408 
00409   xfree (pathname);
00410 
00411   return abfd;
00412 }
00413 
00414 /* Find shared library PATHNAME and open a BFD for it.  */
00415 
00416 bfd *
00417 solib_bfd_open (char *pathname)
00418 {
00419   char *found_pathname;
00420   int found_file;
00421   bfd *abfd;
00422   const struct bfd_arch_info *b;
00423 
00424   /* Search for shared library file.  */
00425   found_pathname = solib_find (pathname, &found_file);
00426   if (found_pathname == NULL)
00427     {
00428       /* Return failure if the file could not be found, so that we can
00429          accumulate messages about missing libraries.  */
00430       if (errno == ENOENT)
00431         return NULL;
00432 
00433       perror_with_name (pathname);
00434     }
00435 
00436   /* Open bfd for shared library.  */
00437   abfd = solib_bfd_fopen (found_pathname, found_file);
00438 
00439   /* Check bfd format.  */
00440   if (!bfd_check_format (abfd, bfd_object))
00441     {
00442       make_cleanup_bfd_unref (abfd);
00443       error (_("`%s': not in executable format: %s"),
00444              bfd_get_filename (abfd), bfd_errmsg (bfd_get_error ()));
00445     }
00446 
00447   /* Check bfd arch.  */
00448   b = gdbarch_bfd_arch_info (target_gdbarch ());
00449   if (!b->compatible (b, bfd_get_arch_info (abfd)))
00450     warning (_("`%s': Shared library architecture %s is not compatible "
00451                "with target architecture %s."), bfd_get_filename (abfd),
00452              bfd_get_arch_info (abfd)->printable_name, b->printable_name);
00453 
00454   return abfd;
00455 }
00456 
00457 /* Given a pointer to one of the shared objects in our list of mapped
00458    objects, use the recorded name to open a bfd descriptor for the
00459    object, build a section table, relocate all the section addresses
00460    by the base address at which the shared object was mapped, and then
00461    add the sections to the target's section table.
00462 
00463    FIXME: In most (all?) cases the shared object file name recorded in
00464    the dynamic linkage tables will be a fully qualified pathname.  For
00465    cases where it isn't, do we really mimic the systems search
00466    mechanism correctly in the below code (particularly the tilde
00467    expansion stuff?).  */
00468 
00469 static int
00470 solib_map_sections (struct so_list *so)
00471 {
00472   const struct target_so_ops *ops = solib_ops (target_gdbarch ());
00473   char *filename;
00474   struct target_section *p;
00475   struct cleanup *old_chain;
00476   bfd *abfd;
00477 
00478   filename = tilde_expand (so->so_name);
00479   old_chain = make_cleanup (xfree, filename);
00480   abfd = ops->bfd_open (filename);
00481   do_cleanups (old_chain);
00482 
00483   if (abfd == NULL)
00484     return 0;
00485 
00486   /* Leave bfd open, core_xfer_memory and "info files" need it.  */
00487   so->abfd = abfd;
00488 
00489   if (build_section_table (abfd, &so->sections, &so->sections_end))
00490     {
00491       error (_("Can't find the file sections in `%s': %s"),
00492              bfd_get_filename (abfd), bfd_errmsg (bfd_get_error ()));
00493     }
00494 
00495   for (p = so->sections; p < so->sections_end; p++)
00496     {
00497       /* Relocate the section binding addresses as recorded in the shared
00498          object's file by the base address to which the object was actually
00499          mapped.  */
00500       ops->relocate_section_addresses (so, p);
00501 
00502       /* If the target didn't provide information about the address
00503          range of the shared object, assume we want the location of
00504          the .text section.  */
00505       if (so->addr_low == 0 && so->addr_high == 0
00506           && strcmp (p->the_bfd_section->name, ".text") == 0)
00507         {
00508           so->addr_low = p->addr;
00509           so->addr_high = p->endaddr;
00510         }
00511     }
00512 
00513   /* Add the shared object's sections to the current set of file
00514      section tables.  Do this immediately after mapping the object so
00515      that later nodes in the list can query this object, as is needed
00516      in solib-osf.c.  */
00517   add_target_sections (so, so->sections, so->sections_end);
00518 
00519   return 1;
00520 }
00521 
00522 /* Free symbol-file related contents of SO and reset for possible reloading
00523    of SO.  If we have opened a BFD for SO, close it.  If we have placed SO's
00524    sections in some target's section table, the caller is responsible for
00525    removing them.
00526 
00527    This function doesn't mess with objfiles at all.  If there is an
00528    objfile associated with SO that needs to be removed, the caller is
00529    responsible for taking care of that.  */
00530 
00531 static void
00532 clear_so (struct so_list *so)
00533 {
00534   const struct target_so_ops *ops = solib_ops (target_gdbarch ());
00535 
00536   if (so->sections)
00537     {
00538       xfree (so->sections);
00539       so->sections = so->sections_end = NULL;
00540     }
00541 
00542   gdb_bfd_unref (so->abfd);
00543   so->abfd = NULL;
00544 
00545   /* Our caller closed the objfile, possibly via objfile_purge_solibs.  */
00546   so->symbols_loaded = 0;
00547   so->objfile = NULL;
00548 
00549   so->addr_low = so->addr_high = 0;
00550 
00551   /* Restore the target-supplied file name.  SO_NAME may be the path
00552      of the symbol file.  */
00553   strcpy (so->so_name, so->so_original_name);
00554 
00555   /* Do the same for target-specific data.  */
00556   if (ops->clear_so != NULL)
00557     ops->clear_so (so);
00558 }
00559 
00560 /* Free the storage associated with the `struct so_list' object SO.
00561    If we have opened a BFD for SO, close it.
00562 
00563    The caller is responsible for removing SO from whatever list it is
00564    a member of.  If we have placed SO's sections in some target's
00565    section table, the caller is responsible for removing them.
00566 
00567    This function doesn't mess with objfiles at all.  If there is an
00568    objfile associated with SO that needs to be removed, the caller is
00569    responsible for taking care of that.  */
00570 
00571 void
00572 free_so (struct so_list *so)
00573 {
00574   const struct target_so_ops *ops = solib_ops (target_gdbarch ());
00575 
00576   clear_so (so);
00577   ops->free_so (so);
00578 
00579   xfree (so);
00580 }
00581 
00582 
00583 /* Return address of first so_list entry in master shared object list.  */
00584 struct so_list *
00585 master_so_list (void)
00586 {
00587   return so_list_head;
00588 }
00589 
00590 /* Read in symbols for shared object SO.  If SYMFILE_VERBOSE is set in FLAGS,
00591    be chatty about it.  Return non-zero if any symbols were actually
00592    loaded.  */
00593 
00594 int
00595 solib_read_symbols (struct so_list *so, int flags)
00596 {
00597   const int from_tty = flags & SYMFILE_VERBOSE;
00598 
00599   if (so->symbols_loaded)
00600     {
00601       /* If needed, we've already warned in our caller.  */
00602     }
00603   else if (so->abfd == NULL)
00604     {
00605       /* We've already warned about this library, when trying to open
00606          it.  */
00607     }
00608   else
00609     {
00610       volatile struct gdb_exception e;
00611 
00612       flags |= current_inferior ()->symfile_flags;
00613 
00614       TRY_CATCH (e, RETURN_MASK_ERROR)
00615         {
00616           struct section_addr_info *sap;
00617 
00618           /* Have we already loaded this shared object?  */
00619           ALL_OBJFILES (so->objfile)
00620             {
00621               if (filename_cmp (objfile_name (so->objfile), so->so_name) == 0
00622                   && so->objfile->addr_low == so->addr_low)
00623                 break;
00624             }
00625           if (so->objfile != NULL)
00626             break;
00627 
00628           sap = build_section_addr_info_from_section_table (so->sections,
00629                                                             so->sections_end);
00630           so->objfile = symbol_file_add_from_bfd (so->abfd, so->so_name,
00631                                                   flags, sap, OBJF_SHARED,
00632                                                   NULL);
00633           so->objfile->addr_low = so->addr_low;
00634           free_section_addr_info (sap);
00635         }
00636 
00637       if (e.reason < 0)
00638         exception_fprintf (gdb_stderr, e, _("Error while reading shared"
00639                                             " library symbols for %s:\n"),
00640                            so->so_name);
00641       else
00642         {
00643           if (from_tty || info_verbose)
00644             printf_unfiltered (_("Loaded symbols for %s\n"), so->so_name);
00645           so->symbols_loaded = 1;
00646         }
00647       return 1;
00648     }
00649 
00650   return 0;
00651 }
00652 
00653 /* Return 1 if KNOWN->objfile is used by any other so_list object in the
00654    SO_LIST_HEAD list.  Return 0 otherwise.  */
00655 
00656 static int
00657 solib_used (const struct so_list *const known)
00658 {
00659   const struct so_list *pivot;
00660 
00661   for (pivot = so_list_head; pivot != NULL; pivot = pivot->next)
00662     if (pivot != known && pivot->objfile == known->objfile)
00663       return 1;
00664   return 0;
00665 }
00666 
00667 /* Synchronize GDB's shared object list with inferior's.
00668 
00669    Extract the list of currently loaded shared objects from the
00670    inferior, and compare it with the list of shared objects currently
00671    in GDB's so_list_head list.  Edit so_list_head to bring it in sync
00672    with the inferior's new list.
00673 
00674    If we notice that the inferior has unloaded some shared objects,
00675    free any symbolic info GDB had read about those shared objects.
00676 
00677    Don't load symbolic info for any new shared objects; just add them
00678    to the list, and leave their symbols_loaded flag clear.
00679 
00680    If FROM_TTY is non-null, feel free to print messages about what
00681    we're doing.
00682 
00683    If TARGET is non-null, add the sections of all new shared objects
00684    to TARGET's section table.  Note that this doesn't remove any
00685    sections for shared objects that have been unloaded, and it
00686    doesn't check to see if the new shared objects are already present in
00687    the section table.  But we only use this for core files and
00688    processes we've just attached to, so that's okay.  */
00689 
00690 static void
00691 update_solib_list (int from_tty, struct target_ops *target)
00692 {
00693   const struct target_so_ops *ops = solib_ops (target_gdbarch ());
00694   struct so_list *inferior = ops->current_sos();
00695   struct so_list *gdb, **gdb_link;
00696 
00697   /* We can reach here due to changing solib-search-path or the
00698      sysroot, before having any inferior.  */
00699   if (target_has_execution && !ptid_equal (inferior_ptid, null_ptid))
00700     {
00701       struct inferior *inf = current_inferior ();
00702 
00703       /* If we are attaching to a running process for which we
00704          have not opened a symbol file, we may be able to get its
00705          symbols now!  */
00706       if (inf->attach_flag && symfile_objfile == NULL)
00707         catch_errors (ops->open_symbol_file_object, &from_tty,
00708                       "Error reading attached process's symbol file.\n",
00709                       RETURN_MASK_ALL);
00710     }
00711 
00712   /* GDB and the inferior's dynamic linker each maintain their own
00713      list of currently loaded shared objects; we want to bring the
00714      former in sync with the latter.  Scan both lists, seeing which
00715      shared objects appear where.  There are three cases:
00716 
00717      - A shared object appears on both lists.  This means that GDB
00718      knows about it already, and it's still loaded in the inferior.
00719      Nothing needs to happen.
00720 
00721      - A shared object appears only on GDB's list.  This means that
00722      the inferior has unloaded it.  We should remove the shared
00723      object from GDB's tables.
00724 
00725      - A shared object appears only on the inferior's list.  This
00726      means that it's just been loaded.  We should add it to GDB's
00727      tables.
00728 
00729      So we walk GDB's list, checking each entry to see if it appears
00730      in the inferior's list too.  If it does, no action is needed, and
00731      we remove it from the inferior's list.  If it doesn't, the
00732      inferior has unloaded it, and we remove it from GDB's list.  By
00733      the time we're done walking GDB's list, the inferior's list
00734      contains only the new shared objects, which we then add.  */
00735 
00736   gdb = so_list_head;
00737   gdb_link = &so_list_head;
00738   while (gdb)
00739     {
00740       struct so_list *i = inferior;
00741       struct so_list **i_link = &inferior;
00742 
00743       /* Check to see whether the shared object *gdb also appears in
00744          the inferior's current list.  */
00745       while (i)
00746         {
00747           if (ops->same)
00748             {
00749               if (ops->same (gdb, i))
00750                 break;
00751             }
00752           else
00753             {
00754               if (! filename_cmp (gdb->so_original_name, i->so_original_name))
00755                 break;        
00756             }
00757 
00758           i_link = &i->next;
00759           i = *i_link;
00760         }
00761 
00762       /* If the shared object appears on the inferior's list too, then
00763          it's still loaded, so we don't need to do anything.  Delete
00764          it from the inferior's list, and leave it on GDB's list.  */
00765       if (i)
00766         {
00767           *i_link = i->next;
00768           free_so (i);
00769           gdb_link = &gdb->next;
00770           gdb = *gdb_link;
00771         }
00772 
00773       /* If it's not on the inferior's list, remove it from GDB's tables.  */
00774       else
00775         {
00776           /* Notify any observer that the shared object has been
00777              unloaded before we remove it from GDB's tables.  */
00778           observer_notify_solib_unloaded (gdb);
00779 
00780           VEC_safe_push (char_ptr, current_program_space->deleted_solibs,
00781                          xstrdup (gdb->so_name));
00782 
00783           *gdb_link = gdb->next;
00784 
00785           /* Unless the user loaded it explicitly, free SO's objfile.  */
00786           if (gdb->objfile && ! (gdb->objfile->flags & OBJF_USERLOADED)
00787               && !solib_used (gdb))
00788             free_objfile (gdb->objfile);
00789 
00790           /* Some targets' section tables might be referring to
00791              sections from so->abfd; remove them.  */
00792           remove_target_sections (gdb);
00793 
00794           free_so (gdb);
00795           gdb = *gdb_link;
00796         }
00797     }
00798 
00799   /* Now the inferior's list contains only shared objects that don't
00800      appear in GDB's list --- those that are newly loaded.  Add them
00801      to GDB's shared object list.  */
00802   if (inferior)
00803     {
00804       int not_found = 0;
00805       const char *not_found_filename = NULL;
00806 
00807       struct so_list *i;
00808 
00809       /* Add the new shared objects to GDB's list.  */
00810       *gdb_link = inferior;
00811 
00812       /* Fill in the rest of each of the `struct so_list' nodes.  */
00813       for (i = inferior; i; i = i->next)
00814         {
00815           volatile struct gdb_exception e;
00816 
00817           i->pspace = current_program_space;
00818           VEC_safe_push (so_list_ptr, current_program_space->added_solibs, i);
00819 
00820           TRY_CATCH (e, RETURN_MASK_ERROR)
00821             {
00822               /* Fill in the rest of the `struct so_list' node.  */
00823               if (!solib_map_sections (i))
00824                 {
00825                   not_found++;
00826                   if (not_found_filename == NULL)
00827                     not_found_filename = i->so_original_name;
00828                 }
00829             }
00830 
00831           if (e.reason < 0)
00832             exception_fprintf (gdb_stderr, e,
00833                                _("Error while mapping shared "
00834                                  "library sections:\n"));
00835 
00836           /* Notify any observer that the shared object has been
00837              loaded now that we've added it to GDB's tables.  */
00838           observer_notify_solib_loaded (i);
00839         }
00840 
00841       /* If a library was not found, issue an appropriate warning
00842          message.  We have to use a single call to warning in case the
00843          front end does something special with warnings, e.g., pop up
00844          a dialog box.  It Would Be Nice if we could get a "warning: "
00845          prefix on each line in the CLI front end, though - it doesn't
00846          stand out well.  */
00847 
00848       if (not_found == 1)
00849         warning (_("Could not load shared library symbols for %s.\n"
00850                    "Do you need \"set solib-search-path\" "
00851                    "or \"set sysroot\"?"),
00852                  not_found_filename);
00853       else if (not_found > 1)
00854         warning (_("\
00855 Could not load shared library symbols for %d libraries, e.g. %s.\n\
00856 Use the \"info sharedlibrary\" command to see the complete listing.\n\
00857 Do you need \"set solib-search-path\" or \"set sysroot\"?"),
00858                  not_found, not_found_filename);
00859     }
00860 }
00861 
00862 
00863 /* Return non-zero if NAME is the libpthread shared library.
00864 
00865    Uses a fairly simplistic heuristic approach where we check
00866    the file name against "/libpthread".  This can lead to false
00867    positives, but this should be good enough in practice.  */
00868 
00869 int
00870 libpthread_name_p (const char *name)
00871 {
00872   return (strstr (name, "/libpthread") != NULL);
00873 }
00874 
00875 /* Return non-zero if SO is the libpthread shared library.  */
00876 
00877 static int
00878 libpthread_solib_p (struct so_list *so)
00879 {
00880   return libpthread_name_p (so->so_name);
00881 }
00882 
00883 /* Read in symbolic information for any shared objects whose names
00884    match PATTERN.  (If we've already read a shared object's symbol
00885    info, leave it alone.)  If PATTERN is zero, read them all.
00886 
00887    If READSYMS is 0, defer reading symbolic information until later
00888    but still do any needed low level processing.
00889 
00890    FROM_TTY and TARGET are as described for update_solib_list, above.  */
00891 
00892 void
00893 solib_add (char *pattern, int from_tty,
00894            struct target_ops *target, int readsyms)
00895 {
00896   struct so_list *gdb;
00897 
00898   current_program_space->solib_add_generation++;
00899 
00900   if (pattern)
00901     {
00902       char *re_err = re_comp (pattern);
00903 
00904       if (re_err)
00905         error (_("Invalid regexp: %s"), re_err);
00906     }
00907 
00908   update_solib_list (from_tty, target);
00909 
00910   /* Walk the list of currently loaded shared libraries, and read
00911      symbols for any that match the pattern --- or any whose symbols
00912      aren't already loaded, if no pattern was given.  */
00913   {
00914     int any_matches = 0;
00915     int loaded_any_symbols = 0;
00916     const int flags =
00917         SYMFILE_DEFER_BP_RESET | (from_tty ? SYMFILE_VERBOSE : 0);
00918 
00919     for (gdb = so_list_head; gdb; gdb = gdb->next)
00920       if (! pattern || re_exec (gdb->so_name))
00921         {
00922           /* Normally, we would read the symbols from that library
00923              only if READSYMS is set.  However, we're making a small
00924              exception for the pthread library, because we sometimes
00925              need the library symbols to be loaded in order to provide
00926              thread support (x86-linux for instance).  */
00927           const int add_this_solib =
00928             (readsyms || libpthread_solib_p (gdb));
00929 
00930           any_matches = 1;
00931           if (add_this_solib)
00932             {
00933               if (gdb->symbols_loaded)
00934                 {
00935                   /* If no pattern was given, be quiet for shared
00936                      libraries we have already loaded.  */
00937                   if (pattern && (from_tty || info_verbose))
00938                     printf_unfiltered (_("Symbols already loaded for %s\n"),
00939                                        gdb->so_name);
00940                 }
00941               else if (solib_read_symbols (gdb, flags))
00942                 loaded_any_symbols = 1;
00943             }
00944         }
00945 
00946     if (loaded_any_symbols)
00947       breakpoint_re_set ();
00948 
00949     if (from_tty && pattern && ! any_matches)
00950       printf_unfiltered
00951         ("No loaded shared libraries match the pattern `%s'.\n", pattern);
00952 
00953     if (loaded_any_symbols)
00954       {
00955         const struct target_so_ops *ops = solib_ops (target_gdbarch ());
00956 
00957         /* Getting new symbols may change our opinion about what is
00958            frameless.  */
00959         reinit_frame_cache ();
00960 
00961         ops->special_symbol_handling ();
00962       }
00963   }
00964 }
00965 
00966 /* Implement the "info sharedlibrary" command.  Walk through the
00967    shared library list and print information about each attached
00968    library matching PATTERN.  If PATTERN is elided, print them
00969    all.  */
00970 
00971 static void
00972 info_sharedlibrary_command (char *pattern, int from_tty)
00973 {
00974   struct so_list *so = NULL;    /* link map state variable */
00975   int so_missing_debug_info = 0;
00976   int addr_width;
00977   int nr_libs;
00978   struct cleanup *table_cleanup;
00979   struct gdbarch *gdbarch = target_gdbarch ();
00980   struct ui_out *uiout = current_uiout;
00981 
00982   if (pattern)
00983     {
00984       char *re_err = re_comp (pattern);
00985 
00986       if (re_err)
00987         error (_("Invalid regexp: %s"), re_err);
00988     }
00989 
00990   /* "0x", a little whitespace, and two hex digits per byte of pointers.  */
00991   addr_width = 4 + (gdbarch_ptr_bit (gdbarch) / 4);
00992 
00993   update_solib_list (from_tty, 0);
00994 
00995   /* make_cleanup_ui_out_table_begin_end needs to know the number of
00996      rows, so we need to make two passes over the libs.  */
00997 
00998   for (nr_libs = 0, so = so_list_head; so; so = so->next)
00999     {
01000       if (so->so_name[0])
01001         {
01002           if (pattern && ! re_exec (so->so_name))
01003             continue;
01004           ++nr_libs;
01005         }
01006     }
01007 
01008   table_cleanup =
01009     make_cleanup_ui_out_table_begin_end (uiout, 4, nr_libs,
01010                                          "SharedLibraryTable");
01011 
01012   /* The "- 1" is because ui_out adds one space between columns.  */
01013   ui_out_table_header (uiout, addr_width - 1, ui_left, "from", "From");
01014   ui_out_table_header (uiout, addr_width - 1, ui_left, "to", "To");
01015   ui_out_table_header (uiout, 12 - 1, ui_left, "syms-read", "Syms Read");
01016   ui_out_table_header (uiout, 0, ui_noalign,
01017                        "name", "Shared Object Library");
01018 
01019   ui_out_table_body (uiout);
01020 
01021   for (so = so_list_head; so; so = so->next)
01022     {
01023       struct cleanup *lib_cleanup;
01024 
01025       if (! so->so_name[0])
01026         continue;
01027       if (pattern && ! re_exec (so->so_name))
01028         continue;
01029 
01030       lib_cleanup = make_cleanup_ui_out_tuple_begin_end (uiout, "lib");
01031 
01032       if (so->addr_high != 0)
01033         {
01034           ui_out_field_core_addr (uiout, "from", gdbarch, so->addr_low);
01035           ui_out_field_core_addr (uiout, "to", gdbarch, so->addr_high);
01036         }
01037       else
01038         {
01039           ui_out_field_skip (uiout, "from");
01040           ui_out_field_skip (uiout, "to");
01041         }
01042 
01043       if (! ui_out_is_mi_like_p (interp_ui_out (top_level_interpreter ()))
01044           && so->symbols_loaded
01045           && !objfile_has_symbols (so->objfile))
01046         {
01047           so_missing_debug_info = 1;
01048           ui_out_field_string (uiout, "syms-read", "Yes (*)");
01049         }
01050       else
01051         ui_out_field_string (uiout, "syms-read", 
01052                              so->symbols_loaded ? "Yes" : "No");
01053 
01054       ui_out_field_string (uiout, "name", so->so_name);
01055 
01056       ui_out_text (uiout, "\n");
01057 
01058       do_cleanups (lib_cleanup);
01059     }
01060 
01061   do_cleanups (table_cleanup);
01062 
01063   if (nr_libs == 0)
01064     {
01065       if (pattern)
01066         ui_out_message (uiout, 0,
01067                         _("No shared libraries matched.\n"));
01068       else
01069         ui_out_message (uiout, 0,
01070                         _("No shared libraries loaded at this time.\n"));
01071     }
01072   else
01073     {
01074       if (so_missing_debug_info)
01075         ui_out_message (uiout, 0,
01076                         _("(*): Shared library is missing "
01077                           "debugging information.\n"));
01078     }
01079 }
01080 
01081 /* Return 1 if ADDRESS lies within SOLIB.  */
01082 
01083 int
01084 solib_contains_address_p (const struct so_list *const solib,
01085                           CORE_ADDR address)
01086 {
01087   struct target_section *p;
01088 
01089   for (p = solib->sections; p < solib->sections_end; p++)
01090     if (p->addr <= address && address < p->endaddr)
01091       return 1;
01092 
01093   return 0;
01094 }
01095 
01096 /* If ADDRESS is in a shared lib in program space PSPACE, return its
01097    name.
01098 
01099    Provides a hook for other gdb routines to discover whether or not a
01100    particular address is within the mapped address space of a shared
01101    library.
01102 
01103    For example, this routine is called at one point to disable
01104    breakpoints which are in shared libraries that are not currently
01105    mapped in.  */
01106 
01107 char *
01108 solib_name_from_address (struct program_space *pspace, CORE_ADDR address)
01109 {
01110   struct so_list *so = NULL;
01111 
01112   for (so = pspace->so_list; so; so = so->next)
01113     if (solib_contains_address_p (so, address))
01114       return (so->so_name);
01115 
01116   return (0);
01117 }
01118 
01119 /* Return whether the data starting at VADDR, size SIZE, must be kept
01120    in a core file for shared libraries loaded before "gcore" is used
01121    to be handled correctly when the core file is loaded.  This only
01122    applies when the section would otherwise not be kept in the core
01123    file (in particular, for readonly sections).  */
01124 
01125 int
01126 solib_keep_data_in_core (CORE_ADDR vaddr, unsigned long size)
01127 {
01128   const struct target_so_ops *ops = solib_ops (target_gdbarch ());
01129 
01130   if (ops->keep_data_in_core)
01131     return ops->keep_data_in_core (vaddr, size);
01132   else
01133     return 0;
01134 }
01135 
01136 /* Called by free_all_symtabs */
01137 
01138 void
01139 clear_solib (void)
01140 {
01141   const struct target_so_ops *ops = solib_ops (target_gdbarch ());
01142 
01143   /* This function is expected to handle ELF shared libraries.  It is
01144      also used on Solaris, which can run either ELF or a.out binaries
01145      (for compatibility with SunOS 4), both of which can use shared
01146      libraries.  So we don't know whether we have an ELF executable or
01147      an a.out executable until the user chooses an executable file.
01148 
01149      ELF shared libraries don't get mapped into the address space
01150      until after the program starts, so we'd better not try to insert
01151      breakpoints in them immediately.  We have to wait until the
01152      dynamic linker has loaded them; we'll hit a bp_shlib_event
01153      breakpoint (look for calls to create_solib_event_breakpoint) when
01154      it's ready.
01155 
01156      SunOS shared libraries seem to be different --- they're present
01157      as soon as the process begins execution, so there's no need to
01158      put off inserting breakpoints.  There's also nowhere to put a
01159      bp_shlib_event breakpoint, so if we put it off, we'll never get
01160      around to it.
01161 
01162      So: disable breakpoints only if we're using ELF shared libs.  */
01163   if (exec_bfd != NULL
01164       && bfd_get_flavour (exec_bfd) != bfd_target_aout_flavour)
01165     disable_breakpoints_in_shlibs ();
01166 
01167   while (so_list_head)
01168     {
01169       struct so_list *so = so_list_head;
01170 
01171       so_list_head = so->next;
01172       observer_notify_solib_unloaded (so);
01173       remove_target_sections (so);
01174       free_so (so);
01175     }
01176 
01177   ops->clear_solib ();
01178 }
01179 
01180 /* Shared library startup support.  When GDB starts up the inferior,
01181    it nurses it along (through the shell) until it is ready to execute
01182    its first instruction.  At this point, this function gets
01183    called.  */
01184 
01185 void
01186 solib_create_inferior_hook (int from_tty)
01187 {
01188   const struct target_so_ops *ops = solib_ops (target_gdbarch ());
01189 
01190   ops->solib_create_inferior_hook (from_tty);
01191 }
01192 
01193 /* Check to see if an address is in the dynamic loader's dynamic
01194    symbol resolution code.  Return 1 if so, 0 otherwise.  */
01195 
01196 int
01197 in_solib_dynsym_resolve_code (CORE_ADDR pc)
01198 {
01199   const struct target_so_ops *ops = solib_ops (target_gdbarch ());
01200 
01201   return ops->in_dynsym_resolve_code (pc);
01202 }
01203 
01204 /* Implements the "sharedlibrary" command.  */
01205 
01206 static void
01207 sharedlibrary_command (char *args, int from_tty)
01208 {
01209   dont_repeat ();
01210   solib_add (args, from_tty, (struct target_ops *) 0, 1);
01211 }
01212 
01213 /* Implements the command "nosharedlibrary", which discards symbols
01214    that have been auto-loaded from shared libraries.  Symbols from
01215    shared libraries that were added by explicit request of the user
01216    are not discarded.  Also called from remote.c.  */
01217 
01218 void
01219 no_shared_libraries (char *ignored, int from_tty)
01220 {
01221   /* The order of the two routines below is important: clear_solib notifies
01222      the solib_unloaded observers, and some of these observers might need
01223      access to their associated objfiles.  Therefore, we can not purge the
01224      solibs' objfiles before clear_solib has been called.  */
01225 
01226   clear_solib ();
01227   objfile_purge_solibs ();
01228 }
01229 
01230 /* See solib.h.  */
01231 
01232 void
01233 update_solib_breakpoints (void)
01234 {
01235   const struct target_so_ops *ops = solib_ops (target_gdbarch ());
01236 
01237   if (ops->update_breakpoints != NULL)
01238     ops->update_breakpoints ();
01239 }
01240 
01241 /* See solib.h.  */
01242 
01243 void
01244 handle_solib_event (void)
01245 {
01246   const struct target_so_ops *ops = solib_ops (target_gdbarch ());
01247 
01248   if (ops->handle_event != NULL)
01249     ops->handle_event ();
01250 
01251   clear_program_space_solib_cache (current_inferior ()->pspace);
01252 
01253   /* Check for any newly added shared libraries if we're supposed to
01254      be adding them automatically.  Switch terminal for any messages
01255      produced by breakpoint_re_set.  */
01256   target_terminal_ours_for_output ();
01257   solib_add (NULL, 0, &current_target, auto_solib_add);
01258   target_terminal_inferior ();
01259 }
01260 
01261 /* Reload shared libraries, but avoid reloading the same symbol file
01262    we already have loaded.  */
01263 
01264 static void
01265 reload_shared_libraries_1 (int from_tty)
01266 {
01267   struct so_list *so;
01268   struct cleanup *old_chain = make_cleanup (null_cleanup, NULL);
01269 
01270   for (so = so_list_head; so != NULL; so = so->next)
01271     {
01272       char *filename, *found_pathname = NULL;
01273       bfd *abfd;
01274       int was_loaded = so->symbols_loaded;
01275       const int flags =
01276         SYMFILE_DEFER_BP_RESET | (from_tty ? SYMFILE_VERBOSE : 0);
01277 
01278       filename = tilde_expand (so->so_original_name);
01279       make_cleanup (xfree, filename);
01280       abfd = solib_bfd_open (filename);
01281       if (abfd != NULL)
01282         {
01283           found_pathname = xstrdup (bfd_get_filename (abfd));
01284           make_cleanup (xfree, found_pathname);
01285           gdb_bfd_unref (abfd);
01286         }
01287 
01288       /* If this shared library is no longer associated with its previous
01289          symbol file, close that.  */
01290       if ((found_pathname == NULL && was_loaded)
01291           || (found_pathname != NULL
01292               && filename_cmp (found_pathname, so->so_name) != 0))
01293         {
01294           if (so->objfile && ! (so->objfile->flags & OBJF_USERLOADED)
01295               && !solib_used (so))
01296             free_objfile (so->objfile);
01297           remove_target_sections (so);
01298           clear_so (so);
01299         }
01300 
01301       /* If this shared library is now associated with a new symbol
01302          file, open it.  */
01303       if (found_pathname != NULL
01304           && (!was_loaded
01305               || filename_cmp (found_pathname, so->so_name) != 0))
01306         {
01307           volatile struct gdb_exception e;
01308 
01309           TRY_CATCH (e, RETURN_MASK_ERROR)
01310             solib_map_sections (so);
01311 
01312           if (e.reason < 0)
01313             exception_fprintf (gdb_stderr, e,
01314                                _("Error while mapping "
01315                                  "shared library sections:\n"));
01316           else if (auto_solib_add || was_loaded || libpthread_solib_p (so))
01317             solib_read_symbols (so, flags);
01318         }
01319     }
01320 
01321   do_cleanups (old_chain);
01322 }
01323 
01324 static void
01325 reload_shared_libraries (char *ignored, int from_tty,
01326                          struct cmd_list_element *e)
01327 {
01328   const struct target_so_ops *ops;
01329 
01330   reload_shared_libraries_1 (from_tty);
01331 
01332   ops = solib_ops (target_gdbarch ());
01333 
01334   /* Creating inferior hooks here has two purposes.  First, if we reload 
01335      shared libraries then the address of solib breakpoint we've computed
01336      previously might be no longer valid.  For example, if we forgot to set
01337      solib-absolute-prefix and are setting it right now, then the previous
01338      breakpoint address is plain wrong.  Second, installing solib hooks
01339      also implicitly figures were ld.so is and loads symbols for it.
01340      Absent this call, if we've just connected to a target and set 
01341      solib-absolute-prefix or solib-search-path, we'll lose all information
01342      about ld.so.  */
01343   if (target_has_execution)
01344     {
01345       /* Reset or free private data structures not associated with
01346          so_list entries.  */
01347       ops->clear_solib ();
01348 
01349       /* Remove any previous solib event breakpoint.  This is usually
01350          done in common code, at breakpoint_init_inferior time, but
01351          we're not really starting up the inferior here.  */
01352       remove_solib_event_breakpoints ();
01353 
01354       solib_create_inferior_hook (from_tty);
01355     }
01356 
01357   /* Sometimes the platform-specific hook loads initial shared
01358      libraries, and sometimes it doesn't.  If it doesn't FROM_TTY will be
01359      incorrectly 0 but such solib targets should be fixed anyway.  If we
01360      made all the inferior hook methods consistent, this call could be
01361      removed.  Call it only after the solib target has been initialized by
01362      solib_create_inferior_hook.  */
01363 
01364   solib_add (NULL, 0, NULL, auto_solib_add);
01365 
01366   breakpoint_re_set ();
01367 
01368   /* We may have loaded or unloaded debug info for some (or all)
01369      shared libraries.  However, frames may still reference them.  For
01370      example, a frame's unwinder might still point at DWARF FDE
01371      structures that are now freed.  Also, getting new symbols may
01372      change our opinion about what is frameless.  */
01373   reinit_frame_cache ();
01374 
01375   ops->special_symbol_handling ();
01376 }
01377 
01378 static void
01379 show_auto_solib_add (struct ui_file *file, int from_tty,
01380                      struct cmd_list_element *c, const char *value)
01381 {
01382   fprintf_filtered (file, _("Autoloading of shared library symbols is %s.\n"),
01383                     value);
01384 }
01385 
01386 
01387 /* Handler for library-specific lookup of global symbol NAME in OBJFILE.  Call
01388    the library-specific handler if it is installed for the current target.  */
01389 
01390 struct symbol *
01391 solib_global_lookup (const struct objfile *objfile,
01392                      const char *name,
01393                      const domain_enum domain)
01394 {
01395   const struct target_so_ops *ops = solib_ops (target_gdbarch ());
01396 
01397   if (ops->lookup_lib_global_symbol != NULL)
01398     return ops->lookup_lib_global_symbol (objfile, name, domain);
01399   return NULL;
01400 }
01401 
01402 /* Lookup the value for a specific symbol from dynamic symbol table.  Look
01403    up symbol from ABFD.  MATCH_SYM is a callback function to determine
01404    whether to pick up a symbol.  DATA is the input of this callback
01405    function.  Return NULL if symbol is not found.  */
01406 
01407 CORE_ADDR
01408 gdb_bfd_lookup_symbol_from_symtab (bfd *abfd,
01409                                    int (*match_sym) (asymbol *, void *),
01410                                    void *data)
01411 {
01412   long storage_needed = bfd_get_symtab_upper_bound (abfd);
01413   CORE_ADDR symaddr = 0;
01414 
01415   if (storage_needed > 0)
01416     {
01417       unsigned int i;
01418 
01419       asymbol **symbol_table = (asymbol **) xmalloc (storage_needed);
01420       struct cleanup *back_to = make_cleanup (xfree, symbol_table);
01421       unsigned int number_of_symbols =
01422         bfd_canonicalize_symtab (abfd, symbol_table);
01423 
01424       for (i = 0; i < number_of_symbols; i++)
01425         {
01426           asymbol *sym  = *symbol_table++;
01427 
01428           if (match_sym (sym, data))
01429             {
01430               /* BFD symbols are section relative.  */
01431               symaddr = sym->value + sym->section->vma;
01432               break;
01433             }
01434         }
01435       do_cleanups (back_to);
01436     }
01437 
01438   return symaddr;
01439 }
01440 
01441 /* Lookup the value for a specific symbol from symbol table.  Look up symbol
01442    from ABFD.  MATCH_SYM is a callback function to determine whether to pick
01443    up a symbol.  DATA is the input of this callback function.  Return NULL
01444    if symbol is not found.  */
01445 
01446 static CORE_ADDR
01447 bfd_lookup_symbol_from_dyn_symtab (bfd *abfd,
01448                                    int (*match_sym) (asymbol *, void *),
01449                                    void *data)
01450 {
01451   long storage_needed = bfd_get_dynamic_symtab_upper_bound (abfd);
01452   CORE_ADDR symaddr = 0;
01453 
01454   if (storage_needed > 0)
01455     {
01456       unsigned int i;
01457       asymbol **symbol_table = (asymbol **) xmalloc (storage_needed);
01458       struct cleanup *back_to = make_cleanup (xfree, symbol_table);
01459       unsigned int number_of_symbols =
01460         bfd_canonicalize_dynamic_symtab (abfd, symbol_table);
01461 
01462       for (i = 0; i < number_of_symbols; i++)
01463         {
01464           asymbol *sym = *symbol_table++;
01465 
01466           if (match_sym (sym, data))
01467             {
01468               /* BFD symbols are section relative.  */
01469               symaddr = sym->value + sym->section->vma;
01470               break;
01471             }
01472         }
01473       do_cleanups (back_to);
01474     }
01475   return symaddr;
01476 }
01477 
01478 /* Lookup the value for a specific symbol from symbol table and dynamic
01479    symbol table.  Look up symbol from ABFD.  MATCH_SYM is a callback
01480    function to determine whether to pick up a symbol.  DATA is the
01481    input of this callback function.  Return NULL if symbol is not
01482    found.  */
01483 
01484 CORE_ADDR
01485 gdb_bfd_lookup_symbol (bfd *abfd,
01486                        int (*match_sym) (asymbol *, void *),
01487                        void *data)
01488 {
01489   CORE_ADDR symaddr = gdb_bfd_lookup_symbol_from_symtab (abfd, match_sym, data);
01490 
01491   /* On FreeBSD, the dynamic linker is stripped by default.  So we'll
01492      have to check the dynamic string table too.  */
01493   if (symaddr == 0)
01494     symaddr = bfd_lookup_symbol_from_dyn_symtab (abfd, match_sym, data);
01495 
01496   return symaddr;
01497 }
01498 
01499 extern initialize_file_ftype _initialize_solib; /* -Wmissing-prototypes */
01500 
01501 void
01502 _initialize_solib (void)
01503 {
01504   solib_data = gdbarch_data_register_pre_init (solib_init);
01505 
01506   add_com ("sharedlibrary", class_files, sharedlibrary_command,
01507            _("Load shared object library symbols for files matching REGEXP."));
01508   add_info ("sharedlibrary", info_sharedlibrary_command,
01509             _("Status of loaded shared object libraries."));
01510   add_com ("nosharedlibrary", class_files, no_shared_libraries,
01511            _("Unload all shared object library symbols."));
01512 
01513   add_setshow_boolean_cmd ("auto-solib-add", class_support,
01514                            &auto_solib_add, _("\
01515 Set autoloading of shared library symbols."), _("\
01516 Show autoloading of shared library symbols."), _("\
01517 If \"on\", symbols from all shared object libraries will be loaded\n\
01518 automatically when the inferior begins execution, when the dynamic linker\n\
01519 informs gdb that a new library has been loaded, or when attaching to the\n\
01520 inferior.  Otherwise, symbols must be loaded manually, using \
01521 `sharedlibrary'."),
01522                            NULL,
01523                            show_auto_solib_add,
01524                            &setlist, &showlist);
01525 
01526   add_setshow_filename_cmd ("sysroot", class_support,
01527                             &gdb_sysroot, _("\
01528 Set an alternate system root."), _("\
01529 Show the current system root."), _("\
01530 The system root is used to load absolute shared library symbol files.\n\
01531 For other (relative) files, you can add directories using\n\
01532 `set solib-search-path'."),
01533                             reload_shared_libraries,
01534                             NULL,
01535                             &setlist, &showlist);
01536 
01537   add_alias_cmd ("solib-absolute-prefix", "sysroot", class_support, 0,
01538                  &setlist);
01539   add_alias_cmd ("solib-absolute-prefix", "sysroot", class_support, 0,
01540                  &showlist);
01541 
01542   add_setshow_optional_filename_cmd ("solib-search-path", class_support,
01543                                      &solib_search_path, _("\
01544 Set the search path for loading non-absolute shared library symbol files."),
01545                                      _("\
01546 Show the search path for loading non-absolute shared library symbol files."),
01547                                      _("\
01548 This takes precedence over the environment variables \
01549 PATH and LD_LIBRARY_PATH."),
01550                                      reload_shared_libraries,
01551                                      show_solib_search_path,
01552                                      &setlist, &showlist);
01553 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines