xref: /dflybsd-src/contrib/gdb-7/gdb/solib.c (revision de8e141f24382815c10a4012d209bbbf7abf1112)
15796c8dcSSimon Schubert /* Handle shared libraries for GDB, the GNU Debugger.
25796c8dcSSimon Schubert 
3*ef5ccd6cSJohn Marino    Copyright (C) 1990-2013 Free Software Foundation, Inc.
45796c8dcSSimon Schubert 
55796c8dcSSimon Schubert    This file is part of GDB.
65796c8dcSSimon Schubert 
75796c8dcSSimon Schubert    This program is free software; you can redistribute it and/or modify
85796c8dcSSimon Schubert    it under the terms of the GNU General Public License as published by
95796c8dcSSimon Schubert    the Free Software Foundation; either version 3 of the License, or
105796c8dcSSimon Schubert    (at your option) any later version.
115796c8dcSSimon Schubert 
125796c8dcSSimon Schubert    This program is distributed in the hope that it will be useful,
135796c8dcSSimon Schubert    but WITHOUT ANY WARRANTY; without even the implied warranty of
145796c8dcSSimon Schubert    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
155796c8dcSSimon Schubert    GNU General Public License for more details.
165796c8dcSSimon Schubert 
175796c8dcSSimon Schubert    You should have received a copy of the GNU General Public License
185796c8dcSSimon Schubert    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
195796c8dcSSimon Schubert 
205796c8dcSSimon Schubert #include "defs.h"
215796c8dcSSimon Schubert 
225796c8dcSSimon Schubert #include <sys/types.h>
235796c8dcSSimon Schubert #include <fcntl.h>
245796c8dcSSimon Schubert #include "gdb_string.h"
255796c8dcSSimon Schubert #include "symtab.h"
265796c8dcSSimon Schubert #include "bfd.h"
275796c8dcSSimon Schubert #include "symfile.h"
285796c8dcSSimon Schubert #include "objfiles.h"
295796c8dcSSimon Schubert #include "exceptions.h"
305796c8dcSSimon Schubert #include "gdbcore.h"
315796c8dcSSimon Schubert #include "command.h"
325796c8dcSSimon Schubert #include "target.h"
335796c8dcSSimon Schubert #include "frame.h"
345796c8dcSSimon Schubert #include "gdb_regex.h"
355796c8dcSSimon Schubert #include "inferior.h"
365796c8dcSSimon Schubert #include "environ.h"
375796c8dcSSimon Schubert #include "language.h"
385796c8dcSSimon Schubert #include "gdbcmd.h"
395796c8dcSSimon Schubert #include "completer.h"
405796c8dcSSimon Schubert #include "filenames.h"		/* for DOSish file names */
415796c8dcSSimon Schubert #include "exec.h"
425796c8dcSSimon Schubert #include "solist.h"
435796c8dcSSimon Schubert #include "observer.h"
445796c8dcSSimon Schubert #include "readline/readline.h"
455796c8dcSSimon Schubert #include "remote.h"
465796c8dcSSimon Schubert #include "solib.h"
475796c8dcSSimon Schubert #include "interps.h"
48cf7f2e2dSJohn Marino #include "filesystem.h"
49*ef5ccd6cSJohn Marino #include "gdb_bfd.h"
505796c8dcSSimon Schubert 
515796c8dcSSimon Schubert /* Architecture-specific operations.  */
525796c8dcSSimon Schubert 
535796c8dcSSimon Schubert /* Per-architecture data key.  */
545796c8dcSSimon Schubert static struct gdbarch_data *solib_data;
555796c8dcSSimon Schubert 
565796c8dcSSimon Schubert static void *
solib_init(struct obstack * obstack)575796c8dcSSimon Schubert solib_init (struct obstack *obstack)
585796c8dcSSimon Schubert {
595796c8dcSSimon Schubert   struct target_so_ops **ops;
605796c8dcSSimon Schubert 
615796c8dcSSimon Schubert   ops = OBSTACK_ZALLOC (obstack, struct target_so_ops *);
625796c8dcSSimon Schubert   *ops = current_target_so_ops;
635796c8dcSSimon Schubert   return ops;
645796c8dcSSimon Schubert }
655796c8dcSSimon Schubert 
665796c8dcSSimon Schubert static struct target_so_ops *
solib_ops(struct gdbarch * gdbarch)675796c8dcSSimon Schubert solib_ops (struct gdbarch *gdbarch)
685796c8dcSSimon Schubert {
695796c8dcSSimon Schubert   struct target_so_ops **ops = gdbarch_data (gdbarch, solib_data);
70cf7f2e2dSJohn Marino 
715796c8dcSSimon Schubert   return *ops;
725796c8dcSSimon Schubert }
735796c8dcSSimon Schubert 
745796c8dcSSimon Schubert /* Set the solib operations for GDBARCH to NEW_OPS.  */
755796c8dcSSimon Schubert 
765796c8dcSSimon Schubert void
set_solib_ops(struct gdbarch * gdbarch,struct target_so_ops * new_ops)775796c8dcSSimon Schubert set_solib_ops (struct gdbarch *gdbarch, struct target_so_ops *new_ops)
785796c8dcSSimon Schubert {
795796c8dcSSimon Schubert   struct target_so_ops **ops = gdbarch_data (gdbarch, solib_data);
80cf7f2e2dSJohn Marino 
815796c8dcSSimon Schubert   *ops = new_ops;
825796c8dcSSimon Schubert }
835796c8dcSSimon Schubert 
845796c8dcSSimon Schubert 
855796c8dcSSimon Schubert /* external data declarations */
865796c8dcSSimon Schubert 
875796c8dcSSimon Schubert /* FIXME: gdbarch needs to control this variable, or else every
885796c8dcSSimon Schubert    configuration needs to call set_solib_ops.  */
895796c8dcSSimon Schubert struct target_so_ops *current_target_so_ops;
905796c8dcSSimon Schubert 
91cf7f2e2dSJohn Marino /* List of known shared objects */
92cf7f2e2dSJohn Marino #define so_list_head current_program_space->so_list
935796c8dcSSimon Schubert 
945796c8dcSSimon Schubert /* Local function prototypes */
955796c8dcSSimon Schubert 
965796c8dcSSimon Schubert /* If non-empty, this is a search path for loading non-absolute shared library
975796c8dcSSimon Schubert    symbol files.  This takes precedence over the environment variables PATH
985796c8dcSSimon Schubert    and LD_LIBRARY_PATH.  */
995796c8dcSSimon Schubert static char *solib_search_path = NULL;
1005796c8dcSSimon Schubert static void
show_solib_search_path(struct ui_file * file,int from_tty,struct cmd_list_element * c,const char * value)1015796c8dcSSimon Schubert show_solib_search_path (struct ui_file *file, int from_tty,
1025796c8dcSSimon Schubert 			struct cmd_list_element *c, const char *value)
1035796c8dcSSimon Schubert {
104c50c785cSJohn Marino   fprintf_filtered (file, _("The search path for loading non-absolute "
105c50c785cSJohn Marino 			    "shared library symbol files is %s.\n"),
1065796c8dcSSimon Schubert 		    value);
1075796c8dcSSimon Schubert }
1085796c8dcSSimon Schubert 
109cf7f2e2dSJohn Marino /* Same as HAVE_DOS_BASED_FILE_SYSTEM, but useable as an rvalue.  */
110cf7f2e2dSJohn Marino #if (HAVE_DOS_BASED_FILE_SYSTEM)
111cf7f2e2dSJohn Marino #  define DOS_BASED_FILE_SYSTEM 1
112cf7f2e2dSJohn Marino #else
113cf7f2e2dSJohn Marino #  define DOS_BASED_FILE_SYSTEM 0
114cf7f2e2dSJohn Marino #endif
115cf7f2e2dSJohn Marino 
116a45ae5f8SJohn Marino /* Returns the full pathname of the shared library file, or NULL if
117a45ae5f8SJohn Marino    not found.  (The pathname is malloc'ed; it needs to be freed by the
118a45ae5f8SJohn Marino    caller.)  *FD is set to either -1 or an open file handle for the
119a45ae5f8SJohn Marino    library.
1205796c8dcSSimon Schubert 
1215796c8dcSSimon Schubert    Global variable GDB_SYSROOT is used as a prefix directory
1225796c8dcSSimon Schubert    to search for shared libraries if they have an absolute path.
1235796c8dcSSimon Schubert 
1245796c8dcSSimon Schubert    Global variable SOLIB_SEARCH_PATH is used as a prefix directory
1255796c8dcSSimon Schubert    (or set of directories, as in LD_LIBRARY_PATH) to search for all
1265796c8dcSSimon Schubert    shared libraries if not found in GDB_SYSROOT.
1275796c8dcSSimon Schubert 
1285796c8dcSSimon Schubert    Search algorithm:
1295796c8dcSSimon Schubert    * If there is a gdb_sysroot and path is absolute:
1305796c8dcSSimon Schubert    *   Search for gdb_sysroot/path.
1315796c8dcSSimon Schubert    * else
1325796c8dcSSimon Schubert    *   Look for it literally (unmodified).
1335796c8dcSSimon Schubert    * Look in SOLIB_SEARCH_PATH.
1345796c8dcSSimon Schubert    * If available, use target defined search function.
1355796c8dcSSimon Schubert    * If gdb_sysroot is NOT set, perform the following two searches:
1365796c8dcSSimon Schubert    *   Look in inferior's $PATH.
1375796c8dcSSimon Schubert    *   Look in inferior's $LD_LIBRARY_PATH.
1385796c8dcSSimon Schubert    *
1395796c8dcSSimon Schubert    * The last check avoids doing this search when targetting remote
1405796c8dcSSimon Schubert    * machines since gdb_sysroot will almost always be set.
141a45ae5f8SJohn Marino */
1425796c8dcSSimon Schubert 
1435796c8dcSSimon Schubert char *
solib_find(char * in_pathname,int * fd)1445796c8dcSSimon Schubert solib_find (char *in_pathname, int *fd)
1455796c8dcSSimon Schubert {
146*ef5ccd6cSJohn Marino   struct target_so_ops *ops = solib_ops (target_gdbarch ());
1475796c8dcSSimon Schubert   int found_file = -1;
1485796c8dcSSimon Schubert   char *temp_pathname = NULL;
1495796c8dcSSimon Schubert   int gdb_sysroot_is_empty;
150cf7f2e2dSJohn Marino   const char *solib_symbols_extension
151*ef5ccd6cSJohn Marino     = gdbarch_solib_symbols_extension (target_gdbarch ());
152cf7f2e2dSJohn Marino   const char *fskind = effective_target_file_system_kind ();
153cf7f2e2dSJohn Marino   struct cleanup *old_chain = make_cleanup (null_cleanup, NULL);
154cf7f2e2dSJohn Marino   char *sysroot = NULL;
155cf7f2e2dSJohn Marino 
156cf7f2e2dSJohn Marino   /* If solib_symbols_extension is set, replace the file's
157cf7f2e2dSJohn Marino      extension.  */
158cf7f2e2dSJohn Marino   if (solib_symbols_extension)
159cf7f2e2dSJohn Marino     {
160cf7f2e2dSJohn Marino       char *p = in_pathname + strlen (in_pathname);
161cf7f2e2dSJohn Marino 
162cf7f2e2dSJohn Marino       while (p > in_pathname && *p != '.')
163cf7f2e2dSJohn Marino 	p--;
164cf7f2e2dSJohn Marino 
165cf7f2e2dSJohn Marino       if (*p == '.')
166cf7f2e2dSJohn Marino 	{
167cf7f2e2dSJohn Marino 	  char *new_pathname;
168cf7f2e2dSJohn Marino 
169cf7f2e2dSJohn Marino 	  new_pathname = alloca (p - in_pathname + 1
170cf7f2e2dSJohn Marino 				 + strlen (solib_symbols_extension) + 1);
171cf7f2e2dSJohn Marino 	  memcpy (new_pathname, in_pathname, p - in_pathname + 1);
172cf7f2e2dSJohn Marino 	  strcpy (new_pathname + (p - in_pathname) + 1,
173cf7f2e2dSJohn Marino 		  solib_symbols_extension);
174cf7f2e2dSJohn Marino 
175cf7f2e2dSJohn Marino 	  in_pathname = new_pathname;
176cf7f2e2dSJohn Marino 	}
177cf7f2e2dSJohn Marino     }
1785796c8dcSSimon Schubert 
1795796c8dcSSimon Schubert   gdb_sysroot_is_empty = (gdb_sysroot == NULL || *gdb_sysroot == 0);
1805796c8dcSSimon Schubert 
181cf7f2e2dSJohn Marino   if (!gdb_sysroot_is_empty)
1825796c8dcSSimon Schubert     {
1835796c8dcSSimon Schubert       int prefix_len = strlen (gdb_sysroot);
1845796c8dcSSimon Schubert 
1855796c8dcSSimon Schubert       /* Remove trailing slashes from absolute prefix.  */
1865796c8dcSSimon Schubert       while (prefix_len > 0
1875796c8dcSSimon Schubert 	     && IS_DIR_SEPARATOR (gdb_sysroot[prefix_len - 1]))
1885796c8dcSSimon Schubert 	prefix_len--;
1895796c8dcSSimon Schubert 
190cf7f2e2dSJohn Marino       sysroot = savestring (gdb_sysroot, prefix_len);
191cf7f2e2dSJohn Marino       make_cleanup (xfree, sysroot);
192cf7f2e2dSJohn Marino     }
193cf7f2e2dSJohn Marino 
194cf7f2e2dSJohn Marino   /* If we're on a non-DOS-based system, backslashes won't be
195cf7f2e2dSJohn Marino      understood as directory separator, so, convert them to forward
196cf7f2e2dSJohn Marino      slashes, iff we're supposed to handle DOS-based file system
197cf7f2e2dSJohn Marino      semantics for target paths.  */
198cf7f2e2dSJohn Marino   if (!DOS_BASED_FILE_SYSTEM && fskind == file_system_kind_dos_based)
199cf7f2e2dSJohn Marino     {
200cf7f2e2dSJohn Marino       char *p;
201cf7f2e2dSJohn Marino 
202cf7f2e2dSJohn Marino       /* Avoid clobbering our input.  */
203cf7f2e2dSJohn Marino       p = alloca (strlen (in_pathname) + 1);
204cf7f2e2dSJohn Marino       strcpy (p, in_pathname);
205cf7f2e2dSJohn Marino       in_pathname = p;
206cf7f2e2dSJohn Marino 
207cf7f2e2dSJohn Marino       for (; *p; p++)
208cf7f2e2dSJohn Marino 	{
209cf7f2e2dSJohn Marino 	  if (*p == '\\')
210cf7f2e2dSJohn Marino 	    *p = '/';
211cf7f2e2dSJohn Marino 	}
212cf7f2e2dSJohn Marino     }
213cf7f2e2dSJohn Marino 
214cf7f2e2dSJohn Marino   /* Note, we're interested in IS_TARGET_ABSOLUTE_PATH, not
215cf7f2e2dSJohn Marino      IS_ABSOLUTE_PATH.  The latter is for host paths only, while
216cf7f2e2dSJohn Marino      IN_PATHNAME is a target path.  For example, if we're supposed to
217cf7f2e2dSJohn Marino      be handling DOS-like semantics we want to consider a
218cf7f2e2dSJohn Marino      'c:/foo/bar.dll' path as an absolute path, even on a Unix box.
219cf7f2e2dSJohn Marino      With such a path, before giving up on the sysroot, we'll try:
220cf7f2e2dSJohn Marino 
221cf7f2e2dSJohn Marino        1st attempt, c:/foo/bar.dll ==> /sysroot/c:/foo/bar.dll
222cf7f2e2dSJohn Marino        2nd attempt, c:/foo/bar.dll ==> /sysroot/c/foo/bar.dll
223cf7f2e2dSJohn Marino        3rd attempt, c:/foo/bar.dll ==> /sysroot/foo/bar.dll
224cf7f2e2dSJohn Marino   */
225cf7f2e2dSJohn Marino 
226cf7f2e2dSJohn Marino   if (!IS_TARGET_ABSOLUTE_PATH (fskind, in_pathname) || gdb_sysroot_is_empty)
227cf7f2e2dSJohn Marino     temp_pathname = xstrdup (in_pathname);
228cf7f2e2dSJohn Marino   else
229cf7f2e2dSJohn Marino     {
230cf7f2e2dSJohn Marino       int need_dir_separator;
231cf7f2e2dSJohn Marino 
232cf7f2e2dSJohn Marino       need_dir_separator = !IS_DIR_SEPARATOR (in_pathname[0]);
233cf7f2e2dSJohn Marino 
2345796c8dcSSimon Schubert       /* Cat the prefixed pathname together.  */
235cf7f2e2dSJohn Marino       temp_pathname = concat (sysroot,
236cf7f2e2dSJohn Marino 			      need_dir_separator ? SLASH_STRING : "",
237cf7f2e2dSJohn Marino 			      in_pathname, (char *) NULL);
2385796c8dcSSimon Schubert     }
2395796c8dcSSimon Schubert 
2405796c8dcSSimon Schubert   /* Handle remote files.  */
2415796c8dcSSimon Schubert   if (remote_filename_p (temp_pathname))
2425796c8dcSSimon Schubert     {
2435796c8dcSSimon Schubert       *fd = -1;
244a45ae5f8SJohn Marino       do_cleanups (old_chain);
245cf7f2e2dSJohn Marino       return temp_pathname;
2465796c8dcSSimon Schubert     }
2475796c8dcSSimon Schubert 
2485796c8dcSSimon Schubert   /* Now see if we can open it.  */
2495796c8dcSSimon Schubert   found_file = open (temp_pathname, O_RDONLY | O_BINARY, 0);
250cf7f2e2dSJohn Marino   if (found_file < 0)
251cf7f2e2dSJohn Marino     xfree (temp_pathname);
2525796c8dcSSimon Schubert 
253cf7f2e2dSJohn Marino   /* If the search in gdb_sysroot failed, and the path name has a
254cf7f2e2dSJohn Marino      drive spec (e.g, c:/foo), try stripping ':' from the drive spec,
255cf7f2e2dSJohn Marino      and retrying in the sysroot:
256cf7f2e2dSJohn Marino        c:/foo/bar.dll ==> /sysroot/c/foo/bar.dll.  */
257cf7f2e2dSJohn Marino 
258cf7f2e2dSJohn Marino   if (found_file < 0
259cf7f2e2dSJohn Marino       && !gdb_sysroot_is_empty
260cf7f2e2dSJohn Marino       && HAS_TARGET_DRIVE_SPEC (fskind, in_pathname))
261cf7f2e2dSJohn Marino     {
262cf7f2e2dSJohn Marino       int need_dir_separator = !IS_DIR_SEPARATOR (in_pathname[2]);
263cf7f2e2dSJohn Marino       char *drive = savestring (in_pathname, 1);
264cf7f2e2dSJohn Marino 
265cf7f2e2dSJohn Marino       temp_pathname = concat (sysroot,
266cf7f2e2dSJohn Marino 			      SLASH_STRING,
267cf7f2e2dSJohn Marino 			      drive,
268cf7f2e2dSJohn Marino 			      need_dir_separator ? SLASH_STRING : "",
269cf7f2e2dSJohn Marino 			      in_pathname + 2, (char *) NULL);
270cf7f2e2dSJohn Marino       xfree (drive);
271cf7f2e2dSJohn Marino 
272cf7f2e2dSJohn Marino       found_file = open (temp_pathname, O_RDONLY | O_BINARY, 0);
273cf7f2e2dSJohn Marino       if (found_file < 0)
274cf7f2e2dSJohn Marino 	{
275cf7f2e2dSJohn Marino 	  xfree (temp_pathname);
276cf7f2e2dSJohn Marino 
277cf7f2e2dSJohn Marino 	  /* If the search in gdb_sysroot still failed, try fully
278cf7f2e2dSJohn Marino 	     stripping the drive spec, and trying once more in the
279cf7f2e2dSJohn Marino 	     sysroot before giving up.
280cf7f2e2dSJohn Marino 
281cf7f2e2dSJohn Marino 	     c:/foo/bar.dll ==> /sysroot/foo/bar.dll.  */
282cf7f2e2dSJohn Marino 
283cf7f2e2dSJohn Marino 	  temp_pathname = concat (sysroot,
284cf7f2e2dSJohn Marino 				  need_dir_separator ? SLASH_STRING : "",
285cf7f2e2dSJohn Marino 				  in_pathname + 2, (char *) NULL);
286cf7f2e2dSJohn Marino 
287cf7f2e2dSJohn Marino 	  found_file = open (temp_pathname, O_RDONLY | O_BINARY, 0);
288cf7f2e2dSJohn Marino 	  if (found_file < 0)
289cf7f2e2dSJohn Marino 	    xfree (temp_pathname);
290cf7f2e2dSJohn Marino 	}
291cf7f2e2dSJohn Marino     }
292cf7f2e2dSJohn Marino 
293cf7f2e2dSJohn Marino   do_cleanups (old_chain);
294cf7f2e2dSJohn Marino 
295cf7f2e2dSJohn Marino   /* We try to find the library in various ways.  After each attempt,
296cf7f2e2dSJohn Marino      either found_file >= 0 and temp_pathname is a malloc'd string, or
297cf7f2e2dSJohn Marino      found_file < 0 and temp_pathname does not point to storage that
298cf7f2e2dSJohn Marino      needs to be freed.  */
2995796c8dcSSimon Schubert 
3005796c8dcSSimon Schubert   if (found_file < 0)
3015796c8dcSSimon Schubert     temp_pathname = NULL;
302cf7f2e2dSJohn Marino 
3035796c8dcSSimon Schubert   /* If the search in gdb_sysroot failed, and the path name is
3045796c8dcSSimon Schubert      absolute at this point, make it relative.  (openp will try and open the
3055796c8dcSSimon Schubert      file according to its absolute path otherwise, which is not what we want.)
3065796c8dcSSimon Schubert      Affects subsequent searches for this solib.  */
307cf7f2e2dSJohn Marino   if (found_file < 0 && IS_TARGET_ABSOLUTE_PATH (fskind, in_pathname))
3085796c8dcSSimon Schubert     {
3095796c8dcSSimon Schubert       /* First, get rid of any drive letters etc.  */
310cf7f2e2dSJohn Marino       while (!IS_TARGET_DIR_SEPARATOR (fskind, *in_pathname))
3115796c8dcSSimon Schubert 	in_pathname++;
3125796c8dcSSimon Schubert 
3135796c8dcSSimon Schubert       /* Next, get rid of all leading dir separators.  */
314cf7f2e2dSJohn Marino       while (IS_TARGET_DIR_SEPARATOR (fskind, *in_pathname))
3155796c8dcSSimon Schubert 	in_pathname++;
3165796c8dcSSimon Schubert     }
3175796c8dcSSimon Schubert 
3185796c8dcSSimon Schubert   /* If not found, search the solib_search_path (if any).  */
3195796c8dcSSimon Schubert   if (found_file < 0 && solib_search_path != NULL)
3205796c8dcSSimon Schubert     found_file = openp (solib_search_path, OPF_TRY_CWD_FIRST,
3215796c8dcSSimon Schubert 			in_pathname, O_RDONLY | O_BINARY, &temp_pathname);
3225796c8dcSSimon Schubert 
3235796c8dcSSimon Schubert   /* If not found, next search the solib_search_path (if any) for the basename
3245796c8dcSSimon Schubert      only (ignoring the path).  This is to allow reading solibs from a path
3255796c8dcSSimon Schubert      that differs from the opened path.  */
3265796c8dcSSimon Schubert   if (found_file < 0 && solib_search_path != NULL)
3275796c8dcSSimon Schubert     found_file = openp (solib_search_path, OPF_TRY_CWD_FIRST,
328cf7f2e2dSJohn Marino 			target_lbasename (fskind, in_pathname),
329cf7f2e2dSJohn Marino 			O_RDONLY | O_BINARY, &temp_pathname);
3305796c8dcSSimon Schubert 
331c50c785cSJohn Marino   /* If not found, try to use target supplied solib search method.  */
3325796c8dcSSimon Schubert   if (found_file < 0 && ops->find_and_open_solib)
3335796c8dcSSimon Schubert     found_file = ops->find_and_open_solib (in_pathname, O_RDONLY | O_BINARY,
3345796c8dcSSimon Schubert 					   &temp_pathname);
3355796c8dcSSimon Schubert 
3365796c8dcSSimon Schubert   /* If not found, next search the inferior's $PATH environment variable.  */
3375796c8dcSSimon Schubert   if (found_file < 0 && gdb_sysroot_is_empty)
338cf7f2e2dSJohn Marino     found_file = openp (get_in_environ (current_inferior ()->environment,
339cf7f2e2dSJohn Marino 					"PATH"),
3405796c8dcSSimon Schubert 			OPF_TRY_CWD_FIRST, in_pathname, O_RDONLY | O_BINARY,
3415796c8dcSSimon Schubert 			&temp_pathname);
3425796c8dcSSimon Schubert 
3435796c8dcSSimon Schubert   /* If not found, next search the inferior's $LD_LIBRARY_PATH
3445796c8dcSSimon Schubert      environment variable.  */
3455796c8dcSSimon Schubert   if (found_file < 0 && gdb_sysroot_is_empty)
346cf7f2e2dSJohn Marino     found_file = openp (get_in_environ (current_inferior ()->environment,
347cf7f2e2dSJohn Marino 					"LD_LIBRARY_PATH"),
3485796c8dcSSimon Schubert 			OPF_TRY_CWD_FIRST, in_pathname, O_RDONLY | O_BINARY,
3495796c8dcSSimon Schubert 			&temp_pathname);
3505796c8dcSSimon Schubert 
3515796c8dcSSimon Schubert   *fd = found_file;
3525796c8dcSSimon Schubert   return temp_pathname;
3535796c8dcSSimon Schubert }
3545796c8dcSSimon Schubert 
3555796c8dcSSimon Schubert /* Open and return a BFD for the shared library PATHNAME.  If FD is not -1,
3565796c8dcSSimon Schubert    it is used as file handle to open the file.  Throws an error if the file
3575796c8dcSSimon Schubert    could not be opened.  Handles both local and remote file access.
3585796c8dcSSimon Schubert 
359*ef5ccd6cSJohn Marino    PATHNAME must be malloc'ed by the caller.  It will be freed by this
360*ef5ccd6cSJohn Marino    function.  If unsuccessful, the FD will be closed (unless FD was
361*ef5ccd6cSJohn Marino    -1).  */
3625796c8dcSSimon Schubert 
3635796c8dcSSimon Schubert bfd *
solib_bfd_fopen(char * pathname,int fd)3645796c8dcSSimon Schubert solib_bfd_fopen (char *pathname, int fd)
3655796c8dcSSimon Schubert {
3665796c8dcSSimon Schubert   bfd *abfd;
3675796c8dcSSimon Schubert 
3685796c8dcSSimon Schubert   if (remote_filename_p (pathname))
3695796c8dcSSimon Schubert     {
3705796c8dcSSimon Schubert       gdb_assert (fd == -1);
3715796c8dcSSimon Schubert       abfd = remote_bfd_open (pathname, gnutarget);
3725796c8dcSSimon Schubert     }
3735796c8dcSSimon Schubert   else
3745796c8dcSSimon Schubert     {
375*ef5ccd6cSJohn Marino       abfd = gdb_bfd_open (pathname, gnutarget, fd);
3765796c8dcSSimon Schubert 
3775796c8dcSSimon Schubert       if (abfd)
3785796c8dcSSimon Schubert 	bfd_set_cacheable (abfd, 1);
3795796c8dcSSimon Schubert     }
3805796c8dcSSimon Schubert 
3815796c8dcSSimon Schubert   if (!abfd)
3825796c8dcSSimon Schubert     {
3835796c8dcSSimon Schubert       make_cleanup (xfree, pathname);
3845796c8dcSSimon Schubert       error (_("Could not open `%s' as an executable file: %s"),
3855796c8dcSSimon Schubert 	     pathname, bfd_errmsg (bfd_get_error ()));
3865796c8dcSSimon Schubert     }
3875796c8dcSSimon Schubert 
388*ef5ccd6cSJohn Marino   xfree (pathname);
389*ef5ccd6cSJohn Marino 
3905796c8dcSSimon Schubert   return abfd;
3915796c8dcSSimon Schubert }
3925796c8dcSSimon Schubert 
3935796c8dcSSimon Schubert /* Find shared library PATHNAME and open a BFD for it.  */
3945796c8dcSSimon Schubert 
3955796c8dcSSimon Schubert bfd *
solib_bfd_open(char * pathname)3965796c8dcSSimon Schubert solib_bfd_open (char *pathname)
3975796c8dcSSimon Schubert {
3985796c8dcSSimon Schubert   char *found_pathname;
3995796c8dcSSimon Schubert   int found_file;
4005796c8dcSSimon Schubert   bfd *abfd;
4015796c8dcSSimon Schubert   const struct bfd_arch_info *b;
4025796c8dcSSimon Schubert 
4035796c8dcSSimon Schubert   /* Search for shared library file.  */
4045796c8dcSSimon Schubert   found_pathname = solib_find (pathname, &found_file);
4055796c8dcSSimon Schubert   if (found_pathname == NULL)
406cf7f2e2dSJohn Marino     {
407cf7f2e2dSJohn Marino       /* Return failure if the file could not be found, so that we can
408cf7f2e2dSJohn Marino 	 accumulate messages about missing libraries.  */
409cf7f2e2dSJohn Marino       if (errno == ENOENT)
410cf7f2e2dSJohn Marino 	return NULL;
411cf7f2e2dSJohn Marino 
4125796c8dcSSimon Schubert       perror_with_name (pathname);
413cf7f2e2dSJohn Marino     }
4145796c8dcSSimon Schubert 
4155796c8dcSSimon Schubert   /* Open bfd for shared library.  */
4165796c8dcSSimon Schubert   abfd = solib_bfd_fopen (found_pathname, found_file);
4175796c8dcSSimon Schubert 
4185796c8dcSSimon Schubert   /* Check bfd format.  */
4195796c8dcSSimon Schubert   if (!bfd_check_format (abfd, bfd_object))
4205796c8dcSSimon Schubert     {
421*ef5ccd6cSJohn Marino       make_cleanup_bfd_unref (abfd);
4225796c8dcSSimon Schubert       error (_("`%s': not in executable format: %s"),
423*ef5ccd6cSJohn Marino 	     bfd_get_filename (abfd), bfd_errmsg (bfd_get_error ()));
4245796c8dcSSimon Schubert     }
4255796c8dcSSimon Schubert 
4265796c8dcSSimon Schubert   /* Check bfd arch.  */
427*ef5ccd6cSJohn Marino   b = gdbarch_bfd_arch_info (target_gdbarch ());
4285796c8dcSSimon Schubert   if (!b->compatible (b, bfd_get_arch_info (abfd)))
4295796c8dcSSimon Schubert     warning (_("`%s': Shared library architecture %s is not compatible "
430*ef5ccd6cSJohn Marino                "with target architecture %s."), bfd_get_filename (abfd),
4315796c8dcSSimon Schubert              bfd_get_arch_info (abfd)->printable_name, b->printable_name);
4325796c8dcSSimon Schubert 
4335796c8dcSSimon Schubert   return abfd;
4345796c8dcSSimon Schubert }
4355796c8dcSSimon Schubert 
436a45ae5f8SJohn Marino /* Given a pointer to one of the shared objects in our list of mapped
437a45ae5f8SJohn Marino    objects, use the recorded name to open a bfd descriptor for the
438a45ae5f8SJohn Marino    object, build a section table, relocate all the section addresses
439a45ae5f8SJohn Marino    by the base address at which the shared object was mapped, and then
440a45ae5f8SJohn Marino    add the sections to the target's section table.
4415796c8dcSSimon Schubert 
442a45ae5f8SJohn Marino    FIXME: In most (all?) cases the shared object file name recorded in
443a45ae5f8SJohn Marino    the dynamic linkage tables will be a fully qualified pathname.  For
4445796c8dcSSimon Schubert    cases where it isn't, do we really mimic the systems search
4455796c8dcSSimon Schubert    mechanism correctly in the below code (particularly the tilde
446a45ae5f8SJohn Marino    expansion stuff?).  */
4475796c8dcSSimon Schubert 
4485796c8dcSSimon Schubert static int
solib_map_sections(struct so_list * so)449cf7f2e2dSJohn Marino solib_map_sections (struct so_list *so)
4505796c8dcSSimon Schubert {
451*ef5ccd6cSJohn Marino   struct target_so_ops *ops = solib_ops (target_gdbarch ());
4525796c8dcSSimon Schubert   char *filename;
4535796c8dcSSimon Schubert   struct target_section *p;
4545796c8dcSSimon Schubert   struct cleanup *old_chain;
4555796c8dcSSimon Schubert   bfd *abfd;
4565796c8dcSSimon Schubert 
4575796c8dcSSimon Schubert   filename = tilde_expand (so->so_name);
4585796c8dcSSimon Schubert   old_chain = make_cleanup (xfree, filename);
4595796c8dcSSimon Schubert   abfd = ops->bfd_open (filename);
4605796c8dcSSimon Schubert   do_cleanups (old_chain);
4615796c8dcSSimon Schubert 
462cf7f2e2dSJohn Marino   if (abfd == NULL)
463cf7f2e2dSJohn Marino     return 0;
464cf7f2e2dSJohn Marino 
4655796c8dcSSimon Schubert   /* Leave bfd open, core_xfer_memory and "info files" need it.  */
466*ef5ccd6cSJohn Marino   so->abfd = abfd;
4675796c8dcSSimon Schubert 
4685796c8dcSSimon Schubert   /* copy full path name into so_name, so that later symbol_file_add
469c50c785cSJohn Marino      can find it.  */
4705796c8dcSSimon Schubert   if (strlen (bfd_get_filename (abfd)) >= SO_NAME_MAX_PATH_SIZE)
4715796c8dcSSimon Schubert     error (_("Shared library file name is too long."));
4725796c8dcSSimon Schubert   strcpy (so->so_name, bfd_get_filename (abfd));
4735796c8dcSSimon Schubert 
4745796c8dcSSimon Schubert   if (build_section_table (abfd, &so->sections, &so->sections_end))
4755796c8dcSSimon Schubert     {
4765796c8dcSSimon Schubert       error (_("Can't find the file sections in `%s': %s"),
4775796c8dcSSimon Schubert 	     bfd_get_filename (abfd), bfd_errmsg (bfd_get_error ()));
4785796c8dcSSimon Schubert     }
4795796c8dcSSimon Schubert 
4805796c8dcSSimon Schubert   for (p = so->sections; p < so->sections_end; p++)
4815796c8dcSSimon Schubert     {
4825796c8dcSSimon Schubert       /* Relocate the section binding addresses as recorded in the shared
4835796c8dcSSimon Schubert          object's file by the base address to which the object was actually
4845796c8dcSSimon Schubert          mapped.  */
4855796c8dcSSimon Schubert       ops->relocate_section_addresses (so, p);
4865796c8dcSSimon Schubert 
4875796c8dcSSimon Schubert       /* If the target didn't provide information about the address
4885796c8dcSSimon Schubert 	 range of the shared object, assume we want the location of
4895796c8dcSSimon Schubert 	 the .text section.  */
4905796c8dcSSimon Schubert       if (so->addr_low == 0 && so->addr_high == 0
4915796c8dcSSimon Schubert 	  && strcmp (p->the_bfd_section->name, ".text") == 0)
4925796c8dcSSimon Schubert 	{
4935796c8dcSSimon Schubert 	  so->addr_low = p->addr;
4945796c8dcSSimon Schubert 	  so->addr_high = p->endaddr;
4955796c8dcSSimon Schubert 	}
4965796c8dcSSimon Schubert     }
4975796c8dcSSimon Schubert 
498cf7f2e2dSJohn Marino   /* Add the shared object's sections to the current set of file
499cf7f2e2dSJohn Marino      section tables.  Do this immediately after mapping the object so
500cf7f2e2dSJohn Marino      that later nodes in the list can query this object, as is needed
501cf7f2e2dSJohn Marino      in solib-osf.c.  */
502*ef5ccd6cSJohn Marino   add_target_sections (so, so->sections, so->sections_end);
503cf7f2e2dSJohn Marino 
504cf7f2e2dSJohn Marino   return 1;
505cf7f2e2dSJohn Marino }
506cf7f2e2dSJohn Marino 
507cf7f2e2dSJohn Marino /* Free symbol-file related contents of SO.  If we have opened a BFD
508cf7f2e2dSJohn Marino    for SO, close it.  If we have placed SO's sections in some target's
509cf7f2e2dSJohn Marino    section table, the caller is responsible for removing them.
510cf7f2e2dSJohn Marino 
511cf7f2e2dSJohn Marino    This function doesn't mess with objfiles at all.  If there is an
512cf7f2e2dSJohn Marino    objfile associated with SO that needs to be removed, the caller is
513cf7f2e2dSJohn Marino    responsible for taking care of that.  */
514cf7f2e2dSJohn Marino 
515cf7f2e2dSJohn Marino static void
free_so_symbols(struct so_list * so)516cf7f2e2dSJohn Marino free_so_symbols (struct so_list *so)
517cf7f2e2dSJohn Marino {
518cf7f2e2dSJohn Marino   if (so->sections)
519cf7f2e2dSJohn Marino     {
520cf7f2e2dSJohn Marino       xfree (so->sections);
521cf7f2e2dSJohn Marino       so->sections = so->sections_end = NULL;
522cf7f2e2dSJohn Marino     }
523cf7f2e2dSJohn Marino 
524cf7f2e2dSJohn Marino   gdb_bfd_unref (so->abfd);
525cf7f2e2dSJohn Marino   so->abfd = NULL;
526cf7f2e2dSJohn Marino 
527cf7f2e2dSJohn Marino   /* Our caller closed the objfile, possibly via objfile_purge_solibs.  */
528cf7f2e2dSJohn Marino   so->symbols_loaded = 0;
529cf7f2e2dSJohn Marino   so->objfile = NULL;
530cf7f2e2dSJohn Marino 
531cf7f2e2dSJohn Marino   so->addr_low = so->addr_high = 0;
532cf7f2e2dSJohn Marino 
533cf7f2e2dSJohn Marino   /* Restore the target-supplied file name.  SO_NAME may be the path
534cf7f2e2dSJohn Marino      of the symbol file.  */
535cf7f2e2dSJohn Marino   strcpy (so->so_name, so->so_original_name);
5365796c8dcSSimon Schubert }
5375796c8dcSSimon Schubert 
538a45ae5f8SJohn Marino /* Free the storage associated with the `struct so_list' object SO.
5395796c8dcSSimon Schubert    If we have opened a BFD for SO, close it.
5405796c8dcSSimon Schubert 
5415796c8dcSSimon Schubert    The caller is responsible for removing SO from whatever list it is
5425796c8dcSSimon Schubert    a member of.  If we have placed SO's sections in some target's
5435796c8dcSSimon Schubert    section table, the caller is responsible for removing them.
5445796c8dcSSimon Schubert 
5455796c8dcSSimon Schubert    This function doesn't mess with objfiles at all.  If there is an
5465796c8dcSSimon Schubert    objfile associated with SO that needs to be removed, the caller is
5475796c8dcSSimon Schubert    responsible for taking care of that.  */
5485796c8dcSSimon Schubert 
5495796c8dcSSimon Schubert void
free_so(struct so_list * so)5505796c8dcSSimon Schubert free_so (struct so_list *so)
5515796c8dcSSimon Schubert {
552*ef5ccd6cSJohn Marino   struct target_so_ops *ops = solib_ops (target_gdbarch ());
5535796c8dcSSimon Schubert 
554cf7f2e2dSJohn Marino   free_so_symbols (so);
5555796c8dcSSimon Schubert   ops->free_so (so);
5565796c8dcSSimon Schubert 
5575796c8dcSSimon Schubert   xfree (so);
5585796c8dcSSimon Schubert }
5595796c8dcSSimon Schubert 
5605796c8dcSSimon Schubert 
5615796c8dcSSimon Schubert /* Return address of first so_list entry in master shared object list.  */
5625796c8dcSSimon Schubert struct so_list *
master_so_list(void)5635796c8dcSSimon Schubert master_so_list (void)
5645796c8dcSSimon Schubert {
5655796c8dcSSimon Schubert   return so_list_head;
5665796c8dcSSimon Schubert }
5675796c8dcSSimon Schubert 
5685796c8dcSSimon Schubert /* Read in symbols for shared object SO.  If SYMFILE_VERBOSE is set in FLAGS,
5695796c8dcSSimon Schubert    be chatty about it.  Return non-zero if any symbols were actually
5705796c8dcSSimon Schubert    loaded.  */
5715796c8dcSSimon Schubert 
5725796c8dcSSimon Schubert int
solib_read_symbols(struct so_list * so,int flags)5735796c8dcSSimon Schubert solib_read_symbols (struct so_list *so, int flags)
5745796c8dcSSimon Schubert {
5755796c8dcSSimon Schubert   const int from_tty = flags & SYMFILE_VERBOSE;
5765796c8dcSSimon Schubert 
5775796c8dcSSimon Schubert   if (so->symbols_loaded)
5785796c8dcSSimon Schubert     {
579cf7f2e2dSJohn Marino       /* If needed, we've already warned in our caller.  */
5805796c8dcSSimon Schubert     }
5815796c8dcSSimon Schubert   else if (so->abfd == NULL)
5825796c8dcSSimon Schubert     {
583cf7f2e2dSJohn Marino       /* We've already warned about this library, when trying to open
584cf7f2e2dSJohn Marino 	 it.  */
5855796c8dcSSimon Schubert     }
5865796c8dcSSimon Schubert   else
5875796c8dcSSimon Schubert     {
588cf7f2e2dSJohn Marino       volatile struct gdb_exception e;
589cf7f2e2dSJohn Marino 
590*ef5ccd6cSJohn Marino       flags |= current_inferior ()->symfile_flags;
591*ef5ccd6cSJohn Marino 
592cf7f2e2dSJohn Marino       TRY_CATCH (e, RETURN_MASK_ERROR)
5935796c8dcSSimon Schubert 	{
594cf7f2e2dSJohn Marino 	  struct section_addr_info *sap;
595cf7f2e2dSJohn Marino 
596cf7f2e2dSJohn Marino 	  /* Have we already loaded this shared object?  */
597cf7f2e2dSJohn Marino 	  ALL_OBJFILES (so->objfile)
5985796c8dcSSimon Schubert 	    {
599c50c785cSJohn Marino 	      if (filename_cmp (so->objfile->name, so->so_name) == 0
600c50c785cSJohn Marino 		  && so->objfile->addr_low == so->addr_low)
601cf7f2e2dSJohn Marino 		break;
6025796c8dcSSimon Schubert 	    }
603cf7f2e2dSJohn Marino 	  if (so->objfile != NULL)
604cf7f2e2dSJohn Marino 	    break;
605cf7f2e2dSJohn Marino 
606cf7f2e2dSJohn Marino 	  sap = build_section_addr_info_from_section_table (so->sections,
607cf7f2e2dSJohn Marino 							    so->sections_end);
608cf7f2e2dSJohn Marino 	  so->objfile = symbol_file_add_from_bfd (so->abfd,
609a45ae5f8SJohn Marino 						  flags, sap, OBJF_SHARED,
610a45ae5f8SJohn Marino 						  NULL);
611c50c785cSJohn Marino 	  so->objfile->addr_low = so->addr_low;
612cf7f2e2dSJohn Marino 	  free_section_addr_info (sap);
613cf7f2e2dSJohn Marino 	}
614cf7f2e2dSJohn Marino 
615cf7f2e2dSJohn Marino       if (e.reason < 0)
616c50c785cSJohn Marino 	exception_fprintf (gdb_stderr, e, _("Error while reading shared"
617c50c785cSJohn Marino 					    " library symbols for %s:\n"),
618c50c785cSJohn Marino 			   so->so_name);
619cf7f2e2dSJohn Marino       else
620cf7f2e2dSJohn Marino 	{
6215796c8dcSSimon Schubert 	  if (from_tty || info_verbose)
6225796c8dcSSimon Schubert 	    printf_unfiltered (_("Loaded symbols for %s\n"), so->so_name);
6235796c8dcSSimon Schubert 	  so->symbols_loaded = 1;
624cf7f2e2dSJohn Marino 	}
6255796c8dcSSimon Schubert       return 1;
6265796c8dcSSimon Schubert     }
6275796c8dcSSimon Schubert 
6285796c8dcSSimon Schubert   return 0;
6295796c8dcSSimon Schubert }
6305796c8dcSSimon Schubert 
631a45ae5f8SJohn Marino /* Return 1 if KNOWN->objfile is used by any other so_list object in the
632a45ae5f8SJohn Marino    SO_LIST_HEAD list.  Return 0 otherwise.  */
6335796c8dcSSimon Schubert 
634a45ae5f8SJohn Marino static int
solib_used(const struct so_list * const known)635a45ae5f8SJohn Marino solib_used (const struct so_list *const known)
636a45ae5f8SJohn Marino {
637a45ae5f8SJohn Marino   const struct so_list *pivot;
6385796c8dcSSimon Schubert 
639a45ae5f8SJohn Marino   for (pivot = so_list_head; pivot != NULL; pivot = pivot->next)
640a45ae5f8SJohn Marino     if (pivot != known && pivot->objfile == known->objfile)
641a45ae5f8SJohn Marino       return 1;
642a45ae5f8SJohn Marino   return 0;
643a45ae5f8SJohn Marino }
6445796c8dcSSimon Schubert 
645a45ae5f8SJohn Marino /* Synchronize GDB's shared object list with inferior's.
6465796c8dcSSimon Schubert 
6475796c8dcSSimon Schubert    Extract the list of currently loaded shared objects from the
6485796c8dcSSimon Schubert    inferior, and compare it with the list of shared objects currently
6495796c8dcSSimon Schubert    in GDB's so_list_head list.  Edit so_list_head to bring it in sync
6505796c8dcSSimon Schubert    with the inferior's new list.
6515796c8dcSSimon Schubert 
6525796c8dcSSimon Schubert    If we notice that the inferior has unloaded some shared objects,
6535796c8dcSSimon Schubert    free any symbolic info GDB had read about those shared objects.
6545796c8dcSSimon Schubert 
6555796c8dcSSimon Schubert    Don't load symbolic info for any new shared objects; just add them
6565796c8dcSSimon Schubert    to the list, and leave their symbols_loaded flag clear.
6575796c8dcSSimon Schubert 
6585796c8dcSSimon Schubert    If FROM_TTY is non-null, feel free to print messages about what
6595796c8dcSSimon Schubert    we're doing.
6605796c8dcSSimon Schubert 
6615796c8dcSSimon Schubert    If TARGET is non-null, add the sections of all new shared objects
6625796c8dcSSimon Schubert    to TARGET's section table.  Note that this doesn't remove any
6635796c8dcSSimon Schubert    sections for shared objects that have been unloaded, and it
6645796c8dcSSimon Schubert    doesn't check to see if the new shared objects are already present in
6655796c8dcSSimon Schubert    the section table.  But we only use this for core files and
6665796c8dcSSimon Schubert    processes we've just attached to, so that's okay.  */
6675796c8dcSSimon Schubert 
6685796c8dcSSimon Schubert static void
update_solib_list(int from_tty,struct target_ops * target)6695796c8dcSSimon Schubert update_solib_list (int from_tty, struct target_ops *target)
6705796c8dcSSimon Schubert {
671*ef5ccd6cSJohn Marino   struct target_so_ops *ops = solib_ops (target_gdbarch ());
6725796c8dcSSimon Schubert   struct so_list *inferior = ops->current_sos();
6735796c8dcSSimon Schubert   struct so_list *gdb, **gdb_link;
6745796c8dcSSimon Schubert 
6755796c8dcSSimon Schubert   /* We can reach here due to changing solib-search-path or the
6765796c8dcSSimon Schubert      sysroot, before having any inferior.  */
6775796c8dcSSimon Schubert   if (target_has_execution && !ptid_equal (inferior_ptid, null_ptid))
6785796c8dcSSimon Schubert     {
6795796c8dcSSimon Schubert       struct inferior *inf = current_inferior ();
6805796c8dcSSimon Schubert 
6815796c8dcSSimon Schubert       /* If we are attaching to a running process for which we
6825796c8dcSSimon Schubert 	 have not opened a symbol file, we may be able to get its
6835796c8dcSSimon Schubert 	 symbols now!  */
6845796c8dcSSimon Schubert       if (inf->attach_flag && symfile_objfile == NULL)
6855796c8dcSSimon Schubert 	catch_errors (ops->open_symbol_file_object, &from_tty,
6865796c8dcSSimon Schubert 		      "Error reading attached process's symbol file.\n",
6875796c8dcSSimon Schubert 		      RETURN_MASK_ALL);
6885796c8dcSSimon Schubert     }
6895796c8dcSSimon Schubert 
6905796c8dcSSimon Schubert   /* GDB and the inferior's dynamic linker each maintain their own
6915796c8dcSSimon Schubert      list of currently loaded shared objects; we want to bring the
6925796c8dcSSimon Schubert      former in sync with the latter.  Scan both lists, seeing which
6935796c8dcSSimon Schubert      shared objects appear where.  There are three cases:
6945796c8dcSSimon Schubert 
6955796c8dcSSimon Schubert      - A shared object appears on both lists.  This means that GDB
6965796c8dcSSimon Schubert      knows about it already, and it's still loaded in the inferior.
6975796c8dcSSimon Schubert      Nothing needs to happen.
6985796c8dcSSimon Schubert 
6995796c8dcSSimon Schubert      - A shared object appears only on GDB's list.  This means that
7005796c8dcSSimon Schubert      the inferior has unloaded it.  We should remove the shared
7015796c8dcSSimon Schubert      object from GDB's tables.
7025796c8dcSSimon Schubert 
7035796c8dcSSimon Schubert      - A shared object appears only on the inferior's list.  This
7045796c8dcSSimon Schubert      means that it's just been loaded.  We should add it to GDB's
7055796c8dcSSimon Schubert      tables.
7065796c8dcSSimon Schubert 
7075796c8dcSSimon Schubert      So we walk GDB's list, checking each entry to see if it appears
7085796c8dcSSimon Schubert      in the inferior's list too.  If it does, no action is needed, and
7095796c8dcSSimon Schubert      we remove it from the inferior's list.  If it doesn't, the
7105796c8dcSSimon Schubert      inferior has unloaded it, and we remove it from GDB's list.  By
7115796c8dcSSimon Schubert      the time we're done walking GDB's list, the inferior's list
7125796c8dcSSimon Schubert      contains only the new shared objects, which we then add.  */
7135796c8dcSSimon Schubert 
7145796c8dcSSimon Schubert   gdb = so_list_head;
7155796c8dcSSimon Schubert   gdb_link = &so_list_head;
7165796c8dcSSimon Schubert   while (gdb)
7175796c8dcSSimon Schubert     {
7185796c8dcSSimon Schubert       struct so_list *i = inferior;
7195796c8dcSSimon Schubert       struct so_list **i_link = &inferior;
7205796c8dcSSimon Schubert 
7215796c8dcSSimon Schubert       /* Check to see whether the shared object *gdb also appears in
7225796c8dcSSimon Schubert 	 the inferior's current list.  */
7235796c8dcSSimon Schubert       while (i)
7245796c8dcSSimon Schubert 	{
7255796c8dcSSimon Schubert 	  if (ops->same)
7265796c8dcSSimon Schubert 	    {
7275796c8dcSSimon Schubert 	      if (ops->same (gdb, i))
7285796c8dcSSimon Schubert 		break;
7295796c8dcSSimon Schubert 	    }
7305796c8dcSSimon Schubert 	  else
7315796c8dcSSimon Schubert 	    {
732c50c785cSJohn Marino 	      if (! filename_cmp (gdb->so_original_name, i->so_original_name))
7335796c8dcSSimon Schubert 		break;
7345796c8dcSSimon Schubert 	    }
7355796c8dcSSimon Schubert 
7365796c8dcSSimon Schubert 	  i_link = &i->next;
7375796c8dcSSimon Schubert 	  i = *i_link;
7385796c8dcSSimon Schubert 	}
7395796c8dcSSimon Schubert 
7405796c8dcSSimon Schubert       /* If the shared object appears on the inferior's list too, then
7415796c8dcSSimon Schubert          it's still loaded, so we don't need to do anything.  Delete
7425796c8dcSSimon Schubert          it from the inferior's list, and leave it on GDB's list.  */
7435796c8dcSSimon Schubert       if (i)
7445796c8dcSSimon Schubert 	{
7455796c8dcSSimon Schubert 	  *i_link = i->next;
7465796c8dcSSimon Schubert 	  free_so (i);
7475796c8dcSSimon Schubert 	  gdb_link = &gdb->next;
7485796c8dcSSimon Schubert 	  gdb = *gdb_link;
7495796c8dcSSimon Schubert 	}
7505796c8dcSSimon Schubert 
7515796c8dcSSimon Schubert       /* If it's not on the inferior's list, remove it from GDB's tables.  */
7525796c8dcSSimon Schubert       else
7535796c8dcSSimon Schubert 	{
7545796c8dcSSimon Schubert 	  /* Notify any observer that the shared object has been
7555796c8dcSSimon Schubert 	     unloaded before we remove it from GDB's tables.  */
7565796c8dcSSimon Schubert 	  observer_notify_solib_unloaded (gdb);
7575796c8dcSSimon Schubert 
758*ef5ccd6cSJohn Marino 	  VEC_safe_push (char_ptr, current_program_space->deleted_solibs,
759*ef5ccd6cSJohn Marino 			 xstrdup (gdb->so_name));
760*ef5ccd6cSJohn Marino 
7615796c8dcSSimon Schubert 	  *gdb_link = gdb->next;
7625796c8dcSSimon Schubert 
7635796c8dcSSimon Schubert 	  /* Unless the user loaded it explicitly, free SO's objfile.  */
764a45ae5f8SJohn Marino 	  if (gdb->objfile && ! (gdb->objfile->flags & OBJF_USERLOADED)
765a45ae5f8SJohn Marino 	      && !solib_used (gdb))
7665796c8dcSSimon Schubert 	    free_objfile (gdb->objfile);
7675796c8dcSSimon Schubert 
7685796c8dcSSimon Schubert 	  /* Some targets' section tables might be referring to
7695796c8dcSSimon Schubert 	     sections from so->abfd; remove them.  */
770*ef5ccd6cSJohn Marino 	  remove_target_sections (gdb, gdb->abfd);
7715796c8dcSSimon Schubert 
7725796c8dcSSimon Schubert 	  free_so (gdb);
7735796c8dcSSimon Schubert 	  gdb = *gdb_link;
7745796c8dcSSimon Schubert 	}
7755796c8dcSSimon Schubert     }
7765796c8dcSSimon Schubert 
7775796c8dcSSimon Schubert   /* Now the inferior's list contains only shared objects that don't
7785796c8dcSSimon Schubert      appear in GDB's list --- those that are newly loaded.  Add them
7795796c8dcSSimon Schubert      to GDB's shared object list.  */
7805796c8dcSSimon Schubert   if (inferior)
7815796c8dcSSimon Schubert     {
782cf7f2e2dSJohn Marino       int not_found = 0;
783cf7f2e2dSJohn Marino       const char *not_found_filename = NULL;
784cf7f2e2dSJohn Marino 
7855796c8dcSSimon Schubert       struct so_list *i;
7865796c8dcSSimon Schubert 
7875796c8dcSSimon Schubert       /* Add the new shared objects to GDB's list.  */
7885796c8dcSSimon Schubert       *gdb_link = inferior;
7895796c8dcSSimon Schubert 
7905796c8dcSSimon Schubert       /* Fill in the rest of each of the `struct so_list' nodes.  */
7915796c8dcSSimon Schubert       for (i = inferior; i; i = i->next)
7925796c8dcSSimon Schubert 	{
793cf7f2e2dSJohn Marino 	  volatile struct gdb_exception e;
7945796c8dcSSimon Schubert 
795cf7f2e2dSJohn Marino 	  i->pspace = current_program_space;
796*ef5ccd6cSJohn Marino 	  VEC_safe_push (so_list_ptr, current_program_space->added_solibs, i);
797cf7f2e2dSJohn Marino 
798cf7f2e2dSJohn Marino 	  TRY_CATCH (e, RETURN_MASK_ERROR)
799cf7f2e2dSJohn Marino 	    {
8005796c8dcSSimon Schubert 	      /* Fill in the rest of the `struct so_list' node.  */
801cf7f2e2dSJohn Marino 	      if (!solib_map_sections (i))
802cf7f2e2dSJohn Marino 		{
803cf7f2e2dSJohn Marino 		  not_found++;
804cf7f2e2dSJohn Marino 		  if (not_found_filename == NULL)
805cf7f2e2dSJohn Marino 		    not_found_filename = i->so_original_name;
806cf7f2e2dSJohn Marino 		}
807cf7f2e2dSJohn Marino 	    }
8085796c8dcSSimon Schubert 
809cf7f2e2dSJohn Marino 	  if (e.reason < 0)
810c50c785cSJohn Marino 	    exception_fprintf (gdb_stderr, e,
811c50c785cSJohn Marino 			       _("Error while mapping shared "
812c50c785cSJohn Marino 				 "library sections:\n"));
8135796c8dcSSimon Schubert 
8145796c8dcSSimon Schubert 	  /* Notify any observer that the shared object has been
8155796c8dcSSimon Schubert 	     loaded now that we've added it to GDB's tables.  */
8165796c8dcSSimon Schubert 	  observer_notify_solib_loaded (i);
8175796c8dcSSimon Schubert 	}
818cf7f2e2dSJohn Marino 
819cf7f2e2dSJohn Marino       /* If a library was not found, issue an appropriate warning
820cf7f2e2dSJohn Marino 	 message.  We have to use a single call to warning in case the
821cf7f2e2dSJohn Marino 	 front end does something special with warnings, e.g., pop up
822cf7f2e2dSJohn Marino 	 a dialog box.  It Would Be Nice if we could get a "warning: "
823cf7f2e2dSJohn Marino 	 prefix on each line in the CLI front end, though - it doesn't
824cf7f2e2dSJohn Marino 	 stand out well.  */
825cf7f2e2dSJohn Marino 
826cf7f2e2dSJohn Marino       if (not_found == 1)
827c50c785cSJohn Marino 	warning (_("Could not load shared library symbols for %s.\n"
828c50c785cSJohn Marino 		   "Do you need \"set solib-search-path\" "
829c50c785cSJohn Marino 		   "or \"set sysroot\"?"),
830cf7f2e2dSJohn Marino 		 not_found_filename);
831cf7f2e2dSJohn Marino       else if (not_found > 1)
832cf7f2e2dSJohn Marino 	warning (_("\
833cf7f2e2dSJohn Marino Could not load shared library symbols for %d libraries, e.g. %s.\n\
834cf7f2e2dSJohn Marino Use the \"info sharedlibrary\" command to see the complete listing.\n\
835cf7f2e2dSJohn Marino Do you need \"set solib-search-path\" or \"set sysroot\"?"),
836cf7f2e2dSJohn Marino 		 not_found, not_found_filename);
8375796c8dcSSimon Schubert     }
8385796c8dcSSimon Schubert }
8395796c8dcSSimon Schubert 
8405796c8dcSSimon Schubert 
8415796c8dcSSimon Schubert /* Return non-zero if NAME is the libpthread shared library.
8425796c8dcSSimon Schubert 
8435796c8dcSSimon Schubert    Uses a fairly simplistic heuristic approach where we check
8445796c8dcSSimon Schubert    the file name against "/libpthread".  This can lead to false
8455796c8dcSSimon Schubert    positives, but this should be good enough in practice.  */
8465796c8dcSSimon Schubert 
8475796c8dcSSimon Schubert int
libpthread_name_p(const char * name)8485796c8dcSSimon Schubert libpthread_name_p (const char *name)
8495796c8dcSSimon Schubert {
8505796c8dcSSimon Schubert   return (strstr (name, "/libpthread") != NULL);
8515796c8dcSSimon Schubert }
8525796c8dcSSimon Schubert 
8535796c8dcSSimon Schubert /* Return non-zero if SO is the libpthread shared library.  */
8545796c8dcSSimon Schubert 
8555796c8dcSSimon Schubert static int
libpthread_solib_p(struct so_list * so)8565796c8dcSSimon Schubert libpthread_solib_p (struct so_list *so)
8575796c8dcSSimon Schubert {
8585796c8dcSSimon Schubert   return libpthread_name_p (so->so_name);
8595796c8dcSSimon Schubert }
8605796c8dcSSimon Schubert 
861a45ae5f8SJohn Marino /* Read in symbolic information for any shared objects whose names
8625796c8dcSSimon Schubert    match PATTERN.  (If we've already read a shared object's symbol
8635796c8dcSSimon Schubert    info, leave it alone.)  If PATTERN is zero, read them all.
8645796c8dcSSimon Schubert 
8655796c8dcSSimon Schubert    If READSYMS is 0, defer reading symbolic information until later
8665796c8dcSSimon Schubert    but still do any needed low level processing.
8675796c8dcSSimon Schubert 
8685796c8dcSSimon Schubert    FROM_TTY and TARGET are as described for update_solib_list, above.  */
8695796c8dcSSimon Schubert 
8705796c8dcSSimon Schubert void
solib_add(char * pattern,int from_tty,struct target_ops * target,int readsyms)871c50c785cSJohn Marino solib_add (char *pattern, int from_tty,
872c50c785cSJohn Marino 	   struct target_ops *target, int readsyms)
8735796c8dcSSimon Schubert {
8745796c8dcSSimon Schubert   struct so_list *gdb;
8755796c8dcSSimon Schubert 
876a45ae5f8SJohn Marino   current_program_space->solib_add_generation++;
877a45ae5f8SJohn Marino 
8785796c8dcSSimon Schubert   if (pattern)
8795796c8dcSSimon Schubert     {
8805796c8dcSSimon Schubert       char *re_err = re_comp (pattern);
8815796c8dcSSimon Schubert 
8825796c8dcSSimon Schubert       if (re_err)
8835796c8dcSSimon Schubert 	error (_("Invalid regexp: %s"), re_err);
8845796c8dcSSimon Schubert     }
8855796c8dcSSimon Schubert 
8865796c8dcSSimon Schubert   update_solib_list (from_tty, target);
8875796c8dcSSimon Schubert 
8885796c8dcSSimon Schubert   /* Walk the list of currently loaded shared libraries, and read
8895796c8dcSSimon Schubert      symbols for any that match the pattern --- or any whose symbols
8905796c8dcSSimon Schubert      aren't already loaded, if no pattern was given.  */
8915796c8dcSSimon Schubert   {
8925796c8dcSSimon Schubert     int any_matches = 0;
8935796c8dcSSimon Schubert     int loaded_any_symbols = 0;
8945796c8dcSSimon Schubert     const int flags =
8955796c8dcSSimon Schubert         SYMFILE_DEFER_BP_RESET | (from_tty ? SYMFILE_VERBOSE : 0);
8965796c8dcSSimon Schubert 
8975796c8dcSSimon Schubert     for (gdb = so_list_head; gdb; gdb = gdb->next)
8985796c8dcSSimon Schubert       if (! pattern || re_exec (gdb->so_name))
8995796c8dcSSimon Schubert 	{
9005796c8dcSSimon Schubert           /* Normally, we would read the symbols from that library
9015796c8dcSSimon Schubert              only if READSYMS is set.  However, we're making a small
9025796c8dcSSimon Schubert              exception for the pthread library, because we sometimes
9035796c8dcSSimon Schubert              need the library symbols to be loaded in order to provide
9045796c8dcSSimon Schubert              thread support (x86-linux for instance).  */
9055796c8dcSSimon Schubert           const int add_this_solib =
9065796c8dcSSimon Schubert             (readsyms || libpthread_solib_p (gdb));
9075796c8dcSSimon Schubert 
9085796c8dcSSimon Schubert 	  any_matches = 1;
909cf7f2e2dSJohn Marino 	  if (add_this_solib)
910cf7f2e2dSJohn Marino 	    {
911cf7f2e2dSJohn Marino 	      if (gdb->symbols_loaded)
912cf7f2e2dSJohn Marino 		{
913cf7f2e2dSJohn Marino 		  /* If no pattern was given, be quiet for shared
914cf7f2e2dSJohn Marino 		     libraries we have already loaded.  */
915cf7f2e2dSJohn Marino 		  if (pattern && (from_tty || info_verbose))
916cf7f2e2dSJohn Marino 		    printf_unfiltered (_("Symbols already loaded for %s\n"),
917cf7f2e2dSJohn Marino 				       gdb->so_name);
918cf7f2e2dSJohn Marino 		}
919cf7f2e2dSJohn Marino 	      else if (solib_read_symbols (gdb, flags))
9205796c8dcSSimon Schubert 		loaded_any_symbols = 1;
9215796c8dcSSimon Schubert 	    }
922cf7f2e2dSJohn Marino 	}
9235796c8dcSSimon Schubert 
9245796c8dcSSimon Schubert     if (loaded_any_symbols)
9255796c8dcSSimon Schubert       breakpoint_re_set ();
9265796c8dcSSimon Schubert 
9275796c8dcSSimon Schubert     if (from_tty && pattern && ! any_matches)
9285796c8dcSSimon Schubert       printf_unfiltered
9295796c8dcSSimon Schubert 	("No loaded shared libraries match the pattern `%s'.\n", pattern);
9305796c8dcSSimon Schubert 
9315796c8dcSSimon Schubert     if (loaded_any_symbols)
9325796c8dcSSimon Schubert       {
933*ef5ccd6cSJohn Marino 	struct target_so_ops *ops = solib_ops (target_gdbarch ());
9345796c8dcSSimon Schubert 
9355796c8dcSSimon Schubert 	/* Getting new symbols may change our opinion about what is
9365796c8dcSSimon Schubert 	   frameless.  */
9375796c8dcSSimon Schubert 	reinit_frame_cache ();
9385796c8dcSSimon Schubert 
9395796c8dcSSimon Schubert 	ops->special_symbol_handling ();
9405796c8dcSSimon Schubert       }
9415796c8dcSSimon Schubert   }
9425796c8dcSSimon Schubert }
9435796c8dcSSimon Schubert 
944a45ae5f8SJohn Marino /* Implement the "info sharedlibrary" command.  Walk through the
945a45ae5f8SJohn Marino    shared library list and print information about each attached
946a45ae5f8SJohn Marino    library matching PATTERN.  If PATTERN is elided, print them
947a45ae5f8SJohn Marino    all.  */
9485796c8dcSSimon Schubert 
9495796c8dcSSimon Schubert static void
info_sharedlibrary_command(char * pattern,int from_tty)9505796c8dcSSimon Schubert info_sharedlibrary_command (char *pattern, int from_tty)
9515796c8dcSSimon Schubert {
9525796c8dcSSimon Schubert   struct so_list *so = NULL;	/* link map state variable */
9535796c8dcSSimon Schubert   int so_missing_debug_info = 0;
9545796c8dcSSimon Schubert   int addr_width;
9555796c8dcSSimon Schubert   int nr_libs;
9565796c8dcSSimon Schubert   struct cleanup *table_cleanup;
957*ef5ccd6cSJohn Marino   struct gdbarch *gdbarch = target_gdbarch ();
958a45ae5f8SJohn Marino   struct ui_out *uiout = current_uiout;
9595796c8dcSSimon Schubert 
9605796c8dcSSimon Schubert   if (pattern)
9615796c8dcSSimon Schubert     {
9625796c8dcSSimon Schubert       char *re_err = re_comp (pattern);
9635796c8dcSSimon Schubert 
9645796c8dcSSimon Schubert       if (re_err)
9655796c8dcSSimon Schubert 	error (_("Invalid regexp: %s"), re_err);
9665796c8dcSSimon Schubert     }
9675796c8dcSSimon Schubert 
9685796c8dcSSimon Schubert   /* "0x", a little whitespace, and two hex digits per byte of pointers.  */
9695796c8dcSSimon Schubert   addr_width = 4 + (gdbarch_ptr_bit (gdbarch) / 4);
9705796c8dcSSimon Schubert 
9715796c8dcSSimon Schubert   update_solib_list (from_tty, 0);
9725796c8dcSSimon Schubert 
9735796c8dcSSimon Schubert   /* make_cleanup_ui_out_table_begin_end needs to know the number of
9745796c8dcSSimon Schubert      rows, so we need to make two passes over the libs.  */
9755796c8dcSSimon Schubert 
9765796c8dcSSimon Schubert   for (nr_libs = 0, so = so_list_head; so; so = so->next)
9775796c8dcSSimon Schubert     {
9785796c8dcSSimon Schubert       if (so->so_name[0])
9795796c8dcSSimon Schubert 	{
9805796c8dcSSimon Schubert 	  if (pattern && ! re_exec (so->so_name))
9815796c8dcSSimon Schubert 	    continue;
9825796c8dcSSimon Schubert 	  ++nr_libs;
9835796c8dcSSimon Schubert 	}
9845796c8dcSSimon Schubert     }
9855796c8dcSSimon Schubert 
9865796c8dcSSimon Schubert   table_cleanup =
9875796c8dcSSimon Schubert     make_cleanup_ui_out_table_begin_end (uiout, 4, nr_libs,
9885796c8dcSSimon Schubert 					 "SharedLibraryTable");
9895796c8dcSSimon Schubert 
9905796c8dcSSimon Schubert   /* The "- 1" is because ui_out adds one space between columns.  */
9915796c8dcSSimon Schubert   ui_out_table_header (uiout, addr_width - 1, ui_left, "from", "From");
9925796c8dcSSimon Schubert   ui_out_table_header (uiout, addr_width - 1, ui_left, "to", "To");
9935796c8dcSSimon Schubert   ui_out_table_header (uiout, 12 - 1, ui_left, "syms-read", "Syms Read");
9945796c8dcSSimon Schubert   ui_out_table_header (uiout, 0, ui_noalign,
9955796c8dcSSimon Schubert 		       "name", "Shared Object Library");
9965796c8dcSSimon Schubert 
9975796c8dcSSimon Schubert   ui_out_table_body (uiout);
9985796c8dcSSimon Schubert 
9995796c8dcSSimon Schubert   for (so = so_list_head; so; so = so->next)
10005796c8dcSSimon Schubert     {
10015796c8dcSSimon Schubert       struct cleanup *lib_cleanup;
10025796c8dcSSimon Schubert 
10035796c8dcSSimon Schubert       if (! so->so_name[0])
10045796c8dcSSimon Schubert 	continue;
10055796c8dcSSimon Schubert       if (pattern && ! re_exec (so->so_name))
10065796c8dcSSimon Schubert 	continue;
10075796c8dcSSimon Schubert 
10085796c8dcSSimon Schubert       lib_cleanup = make_cleanup_ui_out_tuple_begin_end (uiout, "lib");
10095796c8dcSSimon Schubert 
10105796c8dcSSimon Schubert       if (so->addr_high != 0)
10115796c8dcSSimon Schubert 	{
10125796c8dcSSimon Schubert 	  ui_out_field_core_addr (uiout, "from", gdbarch, so->addr_low);
10135796c8dcSSimon Schubert 	  ui_out_field_core_addr (uiout, "to", gdbarch, so->addr_high);
10145796c8dcSSimon Schubert 	}
10155796c8dcSSimon Schubert       else
10165796c8dcSSimon Schubert 	{
10175796c8dcSSimon Schubert 	  ui_out_field_skip (uiout, "from");
10185796c8dcSSimon Schubert 	  ui_out_field_skip (uiout, "to");
10195796c8dcSSimon Schubert 	}
10205796c8dcSSimon Schubert 
10215796c8dcSSimon Schubert       if (! ui_out_is_mi_like_p (interp_ui_out (top_level_interpreter ()))
10225796c8dcSSimon Schubert 	  && so->symbols_loaded
1023cf7f2e2dSJohn Marino 	  && !objfile_has_symbols (so->objfile))
10245796c8dcSSimon Schubert 	{
10255796c8dcSSimon Schubert 	  so_missing_debug_info = 1;
10265796c8dcSSimon Schubert 	  ui_out_field_string (uiout, "syms-read", "Yes (*)");
10275796c8dcSSimon Schubert 	}
10285796c8dcSSimon Schubert       else
10295796c8dcSSimon Schubert 	ui_out_field_string (uiout, "syms-read",
10305796c8dcSSimon Schubert 			     so->symbols_loaded ? "Yes" : "No");
10315796c8dcSSimon Schubert 
10325796c8dcSSimon Schubert       ui_out_field_string (uiout, "name", so->so_name);
10335796c8dcSSimon Schubert 
10345796c8dcSSimon Schubert       ui_out_text (uiout, "\n");
10355796c8dcSSimon Schubert 
10365796c8dcSSimon Schubert       do_cleanups (lib_cleanup);
10375796c8dcSSimon Schubert     }
10385796c8dcSSimon Schubert 
10395796c8dcSSimon Schubert   do_cleanups (table_cleanup);
10405796c8dcSSimon Schubert 
10415796c8dcSSimon Schubert   if (nr_libs == 0)
10425796c8dcSSimon Schubert     {
10435796c8dcSSimon Schubert       if (pattern)
10445796c8dcSSimon Schubert 	ui_out_message (uiout, 0,
10455796c8dcSSimon Schubert 			_("No shared libraries matched.\n"));
10465796c8dcSSimon Schubert       else
10475796c8dcSSimon Schubert 	ui_out_message (uiout, 0,
10485796c8dcSSimon Schubert 			_("No shared libraries loaded at this time.\n"));
10495796c8dcSSimon Schubert     }
10505796c8dcSSimon Schubert   else
10515796c8dcSSimon Schubert     {
10525796c8dcSSimon Schubert       if (so_missing_debug_info)
10535796c8dcSSimon Schubert 	ui_out_message (uiout, 0,
1054c50c785cSJohn Marino 			_("(*): Shared library is missing "
1055c50c785cSJohn Marino 			  "debugging information.\n"));
10565796c8dcSSimon Schubert     }
10575796c8dcSSimon Schubert }
10585796c8dcSSimon Schubert 
10595796c8dcSSimon Schubert /* Return 1 if ADDRESS lies within SOLIB.  */
10605796c8dcSSimon Schubert 
10615796c8dcSSimon Schubert int
solib_contains_address_p(const struct so_list * const solib,CORE_ADDR address)10625796c8dcSSimon Schubert solib_contains_address_p (const struct so_list *const solib,
10635796c8dcSSimon Schubert 			  CORE_ADDR address)
10645796c8dcSSimon Schubert {
10655796c8dcSSimon Schubert   struct target_section *p;
10665796c8dcSSimon Schubert 
10675796c8dcSSimon Schubert   for (p = solib->sections; p < solib->sections_end; p++)
10685796c8dcSSimon Schubert     if (p->addr <= address && address < p->endaddr)
10695796c8dcSSimon Schubert       return 1;
10705796c8dcSSimon Schubert 
10715796c8dcSSimon Schubert   return 0;
10725796c8dcSSimon Schubert }
10735796c8dcSSimon Schubert 
1074a45ae5f8SJohn Marino /* If ADDRESS is in a shared lib in program space PSPACE, return its
1075a45ae5f8SJohn Marino    name.
10765796c8dcSSimon Schubert 
1077a45ae5f8SJohn Marino    Provides a hook for other gdb routines to discover whether or not a
1078a45ae5f8SJohn Marino    particular address is within the mapped address space of a shared
1079a45ae5f8SJohn Marino    library.
10805796c8dcSSimon Schubert 
10815796c8dcSSimon Schubert    For example, this routine is called at one point to disable
10825796c8dcSSimon Schubert    breakpoints which are in shared libraries that are not currently
1083a45ae5f8SJohn Marino    mapped in.  */
10845796c8dcSSimon Schubert 
10855796c8dcSSimon Schubert char *
solib_name_from_address(struct program_space * pspace,CORE_ADDR address)1086cf7f2e2dSJohn Marino solib_name_from_address (struct program_space *pspace, CORE_ADDR address)
10875796c8dcSSimon Schubert {
1088cf7f2e2dSJohn Marino   struct so_list *so = NULL;
10895796c8dcSSimon Schubert 
1090cf7f2e2dSJohn Marino   for (so = pspace->so_list; so; so = so->next)
10915796c8dcSSimon Schubert     if (solib_contains_address_p (so, address))
10925796c8dcSSimon Schubert       return (so->so_name);
10935796c8dcSSimon Schubert 
10945796c8dcSSimon Schubert   return (0);
10955796c8dcSSimon Schubert }
10965796c8dcSSimon Schubert 
1097cf7f2e2dSJohn Marino /* Return whether the data starting at VADDR, size SIZE, must be kept
1098cf7f2e2dSJohn Marino    in a core file for shared libraries loaded before "gcore" is used
1099cf7f2e2dSJohn Marino    to be handled correctly when the core file is loaded.  This only
1100cf7f2e2dSJohn Marino    applies when the section would otherwise not be kept in the core
1101cf7f2e2dSJohn Marino    file (in particular, for readonly sections).  */
1102cf7f2e2dSJohn Marino 
1103cf7f2e2dSJohn Marino int
solib_keep_data_in_core(CORE_ADDR vaddr,unsigned long size)1104cf7f2e2dSJohn Marino solib_keep_data_in_core (CORE_ADDR vaddr, unsigned long size)
1105cf7f2e2dSJohn Marino {
1106*ef5ccd6cSJohn Marino   struct target_so_ops *ops = solib_ops (target_gdbarch ());
1107cf7f2e2dSJohn Marino 
1108cf7f2e2dSJohn Marino   if (ops->keep_data_in_core)
1109cf7f2e2dSJohn Marino     return ops->keep_data_in_core (vaddr, size);
1110cf7f2e2dSJohn Marino   else
1111cf7f2e2dSJohn Marino     return 0;
1112cf7f2e2dSJohn Marino }
1113cf7f2e2dSJohn Marino 
11145796c8dcSSimon Schubert /* Called by free_all_symtabs */
11155796c8dcSSimon Schubert 
11165796c8dcSSimon Schubert void
clear_solib(void)11175796c8dcSSimon Schubert clear_solib (void)
11185796c8dcSSimon Schubert {
1119*ef5ccd6cSJohn Marino   struct target_so_ops *ops = solib_ops (target_gdbarch ());
11205796c8dcSSimon Schubert 
11215796c8dcSSimon Schubert   /* This function is expected to handle ELF shared libraries.  It is
11225796c8dcSSimon Schubert      also used on Solaris, which can run either ELF or a.out binaries
11235796c8dcSSimon Schubert      (for compatibility with SunOS 4), both of which can use shared
11245796c8dcSSimon Schubert      libraries.  So we don't know whether we have an ELF executable or
11255796c8dcSSimon Schubert      an a.out executable until the user chooses an executable file.
11265796c8dcSSimon Schubert 
11275796c8dcSSimon Schubert      ELF shared libraries don't get mapped into the address space
11285796c8dcSSimon Schubert      until after the program starts, so we'd better not try to insert
11295796c8dcSSimon Schubert      breakpoints in them immediately.  We have to wait until the
11305796c8dcSSimon Schubert      dynamic linker has loaded them; we'll hit a bp_shlib_event
11315796c8dcSSimon Schubert      breakpoint (look for calls to create_solib_event_breakpoint) when
11325796c8dcSSimon Schubert      it's ready.
11335796c8dcSSimon Schubert 
11345796c8dcSSimon Schubert      SunOS shared libraries seem to be different --- they're present
11355796c8dcSSimon Schubert      as soon as the process begins execution, so there's no need to
11365796c8dcSSimon Schubert      put off inserting breakpoints.  There's also nowhere to put a
11375796c8dcSSimon Schubert      bp_shlib_event breakpoint, so if we put it off, we'll never get
11385796c8dcSSimon Schubert      around to it.
11395796c8dcSSimon Schubert 
11405796c8dcSSimon Schubert      So: disable breakpoints only if we're using ELF shared libs.  */
11415796c8dcSSimon Schubert   if (exec_bfd != NULL
11425796c8dcSSimon Schubert       && bfd_get_flavour (exec_bfd) != bfd_target_aout_flavour)
11435796c8dcSSimon Schubert     disable_breakpoints_in_shlibs ();
11445796c8dcSSimon Schubert 
11455796c8dcSSimon Schubert   while (so_list_head)
11465796c8dcSSimon Schubert     {
11475796c8dcSSimon Schubert       struct so_list *so = so_list_head;
1148cf7f2e2dSJohn Marino 
11495796c8dcSSimon Schubert       so_list_head = so->next;
11505796c8dcSSimon Schubert       observer_notify_solib_unloaded (so);
11515796c8dcSSimon Schubert       if (so->abfd)
1152*ef5ccd6cSJohn Marino 	remove_target_sections (so, so->abfd);
11535796c8dcSSimon Schubert       free_so (so);
11545796c8dcSSimon Schubert     }
11555796c8dcSSimon Schubert 
11565796c8dcSSimon Schubert   ops->clear_solib ();
11575796c8dcSSimon Schubert }
11585796c8dcSSimon Schubert 
1159a45ae5f8SJohn Marino /* Shared library startup support.  When GDB starts up the inferior,
1160a45ae5f8SJohn Marino    it nurses it along (through the shell) until it is ready to execute
1161a45ae5f8SJohn Marino    its first instruction.  At this point, this function gets
1162a45ae5f8SJohn Marino    called.  */
11635796c8dcSSimon Schubert 
11645796c8dcSSimon Schubert void
solib_create_inferior_hook(int from_tty)1165cf7f2e2dSJohn Marino solib_create_inferior_hook (int from_tty)
11665796c8dcSSimon Schubert {
1167*ef5ccd6cSJohn Marino   struct target_so_ops *ops = solib_ops (target_gdbarch ());
1168cf7f2e2dSJohn Marino 
1169cf7f2e2dSJohn Marino   ops->solib_create_inferior_hook (from_tty);
11705796c8dcSSimon Schubert }
11715796c8dcSSimon Schubert 
1172a45ae5f8SJohn Marino /* Check to see if an address is in the dynamic loader's dynamic
1173a45ae5f8SJohn Marino    symbol resolution code.  Return 1 if so, 0 otherwise.  */
11745796c8dcSSimon Schubert 
11755796c8dcSSimon Schubert int
in_solib_dynsym_resolve_code(CORE_ADDR pc)11765796c8dcSSimon Schubert in_solib_dynsym_resolve_code (CORE_ADDR pc)
11775796c8dcSSimon Schubert {
1178*ef5ccd6cSJohn Marino   struct target_so_ops *ops = solib_ops (target_gdbarch ());
1179cf7f2e2dSJohn Marino 
11805796c8dcSSimon Schubert   return ops->in_dynsym_resolve_code (pc);
11815796c8dcSSimon Schubert }
11825796c8dcSSimon Schubert 
1183a45ae5f8SJohn Marino /* Implements the "sharedlibrary" command.  */
11845796c8dcSSimon Schubert 
11855796c8dcSSimon Schubert static void
sharedlibrary_command(char * args,int from_tty)11865796c8dcSSimon Schubert sharedlibrary_command (char *args, int from_tty)
11875796c8dcSSimon Schubert {
11885796c8dcSSimon Schubert   dont_repeat ();
11895796c8dcSSimon Schubert   solib_add (args, from_tty, (struct target_ops *) 0, 1);
11905796c8dcSSimon Schubert }
11915796c8dcSSimon Schubert 
1192a45ae5f8SJohn Marino /* Implements the command "nosharedlibrary", which discards symbols
11935796c8dcSSimon Schubert    that have been auto-loaded from shared libraries.  Symbols from
11945796c8dcSSimon Schubert    shared libraries that were added by explicit request of the user
11955796c8dcSSimon Schubert    are not discarded.  Also called from remote.c.  */
11965796c8dcSSimon Schubert 
11975796c8dcSSimon Schubert void
no_shared_libraries(char * ignored,int from_tty)11985796c8dcSSimon Schubert no_shared_libraries (char *ignored, int from_tty)
11995796c8dcSSimon Schubert {
12005796c8dcSSimon Schubert   /* The order of the two routines below is important: clear_solib notifies
12015796c8dcSSimon Schubert      the solib_unloaded observers, and some of these observers might need
12025796c8dcSSimon Schubert      access to their associated objfiles.  Therefore, we can not purge the
12035796c8dcSSimon Schubert      solibs' objfiles before clear_solib has been called.  */
12045796c8dcSSimon Schubert 
12055796c8dcSSimon Schubert   clear_solib ();
12065796c8dcSSimon Schubert   objfile_purge_solibs ();
12075796c8dcSSimon Schubert }
12085796c8dcSSimon Schubert 
1209cf7f2e2dSJohn Marino /* Reload shared libraries, but avoid reloading the same symbol file
1210cf7f2e2dSJohn Marino    we already have loaded.  */
1211cf7f2e2dSJohn Marino 
1212cf7f2e2dSJohn Marino static void
reload_shared_libraries_1(int from_tty)1213cf7f2e2dSJohn Marino reload_shared_libraries_1 (int from_tty)
1214cf7f2e2dSJohn Marino {
1215cf7f2e2dSJohn Marino   struct so_list *so;
1216cf7f2e2dSJohn Marino   struct cleanup *old_chain = make_cleanup (null_cleanup, NULL);
1217cf7f2e2dSJohn Marino 
1218cf7f2e2dSJohn Marino   for (so = so_list_head; so != NULL; so = so->next)
1219cf7f2e2dSJohn Marino     {
1220cf7f2e2dSJohn Marino       char *filename, *found_pathname = NULL;
1221cf7f2e2dSJohn Marino       bfd *abfd;
1222cf7f2e2dSJohn Marino       int was_loaded = so->symbols_loaded;
1223cf7f2e2dSJohn Marino       const int flags =
1224cf7f2e2dSJohn Marino 	SYMFILE_DEFER_BP_RESET | (from_tty ? SYMFILE_VERBOSE : 0);
1225cf7f2e2dSJohn Marino 
1226cf7f2e2dSJohn Marino       filename = tilde_expand (so->so_original_name);
1227c50c785cSJohn Marino       make_cleanup (xfree, filename);
1228cf7f2e2dSJohn Marino       abfd = solib_bfd_open (filename);
1229cf7f2e2dSJohn Marino       if (abfd != NULL)
1230cf7f2e2dSJohn Marino 	{
1231cf7f2e2dSJohn Marino 	  found_pathname = xstrdup (bfd_get_filename (abfd));
1232cf7f2e2dSJohn Marino 	  make_cleanup (xfree, found_pathname);
1233*ef5ccd6cSJohn Marino 	  gdb_bfd_unref (abfd);
1234cf7f2e2dSJohn Marino 	}
1235cf7f2e2dSJohn Marino 
1236cf7f2e2dSJohn Marino       /* If this shared library is no longer associated with its previous
1237cf7f2e2dSJohn Marino 	 symbol file, close that.  */
1238cf7f2e2dSJohn Marino       if ((found_pathname == NULL && was_loaded)
1239cf7f2e2dSJohn Marino 	  || (found_pathname != NULL
1240c50c785cSJohn Marino 	      && filename_cmp (found_pathname, so->so_name) != 0))
1241cf7f2e2dSJohn Marino 	{
1242a45ae5f8SJohn Marino 	  if (so->objfile && ! (so->objfile->flags & OBJF_USERLOADED)
1243a45ae5f8SJohn Marino 	      && !solib_used (so))
1244cf7f2e2dSJohn Marino 	    free_objfile (so->objfile);
1245*ef5ccd6cSJohn Marino 	  remove_target_sections (so, so->abfd);
1246cf7f2e2dSJohn Marino 	  free_so_symbols (so);
1247cf7f2e2dSJohn Marino 	}
1248cf7f2e2dSJohn Marino 
1249cf7f2e2dSJohn Marino       /* If this shared library is now associated with a new symbol
1250cf7f2e2dSJohn Marino 	 file, open it.  */
1251cf7f2e2dSJohn Marino       if (found_pathname != NULL
1252cf7f2e2dSJohn Marino 	  && (!was_loaded
1253c50c785cSJohn Marino 	      || filename_cmp (found_pathname, so->so_name) != 0))
1254cf7f2e2dSJohn Marino 	{
1255cf7f2e2dSJohn Marino 	  volatile struct gdb_exception e;
1256cf7f2e2dSJohn Marino 
1257cf7f2e2dSJohn Marino 	  TRY_CATCH (e, RETURN_MASK_ERROR)
1258cf7f2e2dSJohn Marino 	    solib_map_sections (so);
1259cf7f2e2dSJohn Marino 
1260cf7f2e2dSJohn Marino 	  if (e.reason < 0)
1261c50c785cSJohn Marino 	    exception_fprintf (gdb_stderr, e,
1262c50c785cSJohn Marino 			       _("Error while mapping "
1263c50c785cSJohn Marino 				 "shared library sections:\n"));
1264cf7f2e2dSJohn Marino 	  else if (auto_solib_add || was_loaded || libpthread_solib_p (so))
1265cf7f2e2dSJohn Marino 	    solib_read_symbols (so, flags);
1266cf7f2e2dSJohn Marino 	}
1267cf7f2e2dSJohn Marino     }
1268cf7f2e2dSJohn Marino 
1269cf7f2e2dSJohn Marino   do_cleanups (old_chain);
1270cf7f2e2dSJohn Marino }
1271cf7f2e2dSJohn Marino 
12725796c8dcSSimon Schubert static void
reload_shared_libraries(char * ignored,int from_tty,struct cmd_list_element * e)12735796c8dcSSimon Schubert reload_shared_libraries (char *ignored, int from_tty,
12745796c8dcSSimon Schubert 			 struct cmd_list_element *e)
12755796c8dcSSimon Schubert {
1276cf7f2e2dSJohn Marino   struct target_so_ops *ops;
1277cf7f2e2dSJohn Marino 
1278cf7f2e2dSJohn Marino   reload_shared_libraries_1 (from_tty);
1279cf7f2e2dSJohn Marino 
1280*ef5ccd6cSJohn Marino   ops = solib_ops (target_gdbarch ());
1281cf7f2e2dSJohn Marino 
12825796c8dcSSimon Schubert   /* Creating inferior hooks here has two purposes.  First, if we reload
12835796c8dcSSimon Schubert      shared libraries then the address of solib breakpoint we've computed
12845796c8dcSSimon Schubert      previously might be no longer valid.  For example, if we forgot to set
12855796c8dcSSimon Schubert      solib-absolute-prefix and are setting it right now, then the previous
12865796c8dcSSimon Schubert      breakpoint address is plain wrong.  Second, installing solib hooks
12875796c8dcSSimon Schubert      also implicitly figures were ld.so is and loads symbols for it.
12885796c8dcSSimon Schubert      Absent this call, if we've just connected to a target and set
12895796c8dcSSimon Schubert      solib-absolute-prefix or solib-search-path, we'll lose all information
12905796c8dcSSimon Schubert      about ld.so.  */
12915796c8dcSSimon Schubert   if (target_has_execution)
12925796c8dcSSimon Schubert     {
1293cf7f2e2dSJohn Marino       /* Reset or free private data structures not associated with
1294cf7f2e2dSJohn Marino 	 so_list entries.  */
1295cf7f2e2dSJohn Marino       ops->clear_solib ();
1296cf7f2e2dSJohn Marino 
1297cf7f2e2dSJohn Marino       /* Remove any previous solib event breakpoint.  This is usually
1298cf7f2e2dSJohn Marino 	 done in common code, at breakpoint_init_inferior time, but
1299cf7f2e2dSJohn Marino 	 we're not really starting up the inferior here.  */
1300cf7f2e2dSJohn Marino       remove_solib_event_breakpoints ();
1301cf7f2e2dSJohn Marino 
13025796c8dcSSimon Schubert #ifdef SOLIB_CREATE_INFERIOR_HOOK
13035796c8dcSSimon Schubert       SOLIB_CREATE_INFERIOR_HOOK (PIDGET (inferior_ptid));
13045796c8dcSSimon Schubert #else
1305cf7f2e2dSJohn Marino       solib_create_inferior_hook (from_tty);
13065796c8dcSSimon Schubert #endif
13075796c8dcSSimon Schubert     }
1308cf7f2e2dSJohn Marino 
1309cf7f2e2dSJohn Marino   /* Sometimes the platform-specific hook loads initial shared
1310cf7f2e2dSJohn Marino      libraries, and sometimes it doesn't.  If it doesn't FROM_TTY will be
1311cf7f2e2dSJohn Marino      incorrectly 0 but such solib targets should be fixed anyway.  If we
1312cf7f2e2dSJohn Marino      made all the inferior hook methods consistent, this call could be
1313cf7f2e2dSJohn Marino      removed.  Call it only after the solib target has been initialized by
1314cf7f2e2dSJohn Marino      solib_create_inferior_hook.  */
1315cf7f2e2dSJohn Marino 
1316cf7f2e2dSJohn Marino   solib_add (NULL, 0, NULL, auto_solib_add);
1317cf7f2e2dSJohn Marino 
1318cf7f2e2dSJohn Marino   breakpoint_re_set ();
1319cf7f2e2dSJohn Marino 
1320cf7f2e2dSJohn Marino   /* We may have loaded or unloaded debug info for some (or all)
1321cf7f2e2dSJohn Marino      shared libraries.  However, frames may still reference them.  For
1322cf7f2e2dSJohn Marino      example, a frame's unwinder might still point at DWARF FDE
1323cf7f2e2dSJohn Marino      structures that are now freed.  Also, getting new symbols may
1324cf7f2e2dSJohn Marino      change our opinion about what is frameless.  */
13255796c8dcSSimon Schubert   reinit_frame_cache ();
1326cf7f2e2dSJohn Marino 
1327cf7f2e2dSJohn Marino   ops->special_symbol_handling ();
13285796c8dcSSimon Schubert }
13295796c8dcSSimon Schubert 
13305796c8dcSSimon Schubert static void
show_auto_solib_add(struct ui_file * file,int from_tty,struct cmd_list_element * c,const char * value)13315796c8dcSSimon Schubert show_auto_solib_add (struct ui_file *file, int from_tty,
13325796c8dcSSimon Schubert 		     struct cmd_list_element *c, const char *value)
13335796c8dcSSimon Schubert {
13345796c8dcSSimon Schubert   fprintf_filtered (file, _("Autoloading of shared library symbols is %s.\n"),
13355796c8dcSSimon Schubert 		    value);
13365796c8dcSSimon Schubert }
13375796c8dcSSimon Schubert 
13385796c8dcSSimon Schubert 
13395796c8dcSSimon Schubert /* Handler for library-specific lookup of global symbol NAME in OBJFILE.  Call
13405796c8dcSSimon Schubert    the library-specific handler if it is installed for the current target.  */
13415796c8dcSSimon Schubert 
13425796c8dcSSimon Schubert struct symbol *
solib_global_lookup(const struct objfile * objfile,const char * name,const domain_enum domain)13435796c8dcSSimon Schubert solib_global_lookup (const struct objfile *objfile,
13445796c8dcSSimon Schubert 		     const char *name,
13455796c8dcSSimon Schubert 		     const domain_enum domain)
13465796c8dcSSimon Schubert {
1347*ef5ccd6cSJohn Marino   struct target_so_ops *ops = solib_ops (target_gdbarch ());
13485796c8dcSSimon Schubert 
13495796c8dcSSimon Schubert   if (ops->lookup_lib_global_symbol != NULL)
1350cf7f2e2dSJohn Marino     return ops->lookup_lib_global_symbol (objfile, name, domain);
13515796c8dcSSimon Schubert   return NULL;
13525796c8dcSSimon Schubert }
13535796c8dcSSimon Schubert 
1354a45ae5f8SJohn Marino /* Lookup the value for a specific symbol from dynamic symbol table.  Look
1355a45ae5f8SJohn Marino    up symbol from ABFD.  MATCH_SYM is a callback function to determine
1356a45ae5f8SJohn Marino    whether to pick up a symbol.  DATA is the input of this callback
1357a45ae5f8SJohn Marino    function.  Return NULL if symbol is not found.  */
1358a45ae5f8SJohn Marino 
1359a45ae5f8SJohn Marino CORE_ADDR
gdb_bfd_lookup_symbol_from_symtab(bfd * abfd,int (* match_sym)(asymbol *,void *),void * data)1360a45ae5f8SJohn Marino gdb_bfd_lookup_symbol_from_symtab (bfd *abfd,
1361a45ae5f8SJohn Marino 				   int (*match_sym) (asymbol *, void *),
1362a45ae5f8SJohn Marino 				   void *data)
1363a45ae5f8SJohn Marino {
1364a45ae5f8SJohn Marino   long storage_needed = bfd_get_symtab_upper_bound (abfd);
1365a45ae5f8SJohn Marino   CORE_ADDR symaddr = 0;
1366a45ae5f8SJohn Marino 
1367a45ae5f8SJohn Marino   if (storage_needed > 0)
1368a45ae5f8SJohn Marino     {
1369a45ae5f8SJohn Marino       unsigned int i;
1370a45ae5f8SJohn Marino 
1371a45ae5f8SJohn Marino       asymbol **symbol_table = (asymbol **) xmalloc (storage_needed);
1372a45ae5f8SJohn Marino       struct cleanup *back_to = make_cleanup (xfree, symbol_table);
1373a45ae5f8SJohn Marino       unsigned int number_of_symbols =
1374a45ae5f8SJohn Marino 	bfd_canonicalize_symtab (abfd, symbol_table);
1375a45ae5f8SJohn Marino 
1376a45ae5f8SJohn Marino       for (i = 0; i < number_of_symbols; i++)
1377a45ae5f8SJohn Marino 	{
1378a45ae5f8SJohn Marino 	  asymbol *sym  = *symbol_table++;
1379a45ae5f8SJohn Marino 
1380a45ae5f8SJohn Marino 	  if (match_sym (sym, data))
1381a45ae5f8SJohn Marino 	    {
1382a45ae5f8SJohn Marino 	      /* BFD symbols are section relative.  */
1383a45ae5f8SJohn Marino 	      symaddr = sym->value + sym->section->vma;
1384a45ae5f8SJohn Marino 	      break;
1385a45ae5f8SJohn Marino 	    }
1386a45ae5f8SJohn Marino 	}
1387a45ae5f8SJohn Marino       do_cleanups (back_to);
1388a45ae5f8SJohn Marino     }
1389a45ae5f8SJohn Marino 
1390a45ae5f8SJohn Marino   return symaddr;
1391a45ae5f8SJohn Marino }
1392a45ae5f8SJohn Marino 
1393a45ae5f8SJohn Marino /* Lookup the value for a specific symbol from symbol table.  Look up symbol
1394a45ae5f8SJohn Marino    from ABFD.  MATCH_SYM is a callback function to determine whether to pick
1395a45ae5f8SJohn Marino    up a symbol.  DATA is the input of this callback function.  Return NULL
1396a45ae5f8SJohn Marino    if symbol is not found.  */
1397a45ae5f8SJohn Marino 
1398a45ae5f8SJohn Marino static CORE_ADDR
bfd_lookup_symbol_from_dyn_symtab(bfd * abfd,int (* match_sym)(asymbol *,void *),void * data)1399a45ae5f8SJohn Marino bfd_lookup_symbol_from_dyn_symtab (bfd *abfd,
1400a45ae5f8SJohn Marino 				   int (*match_sym) (asymbol *, void *),
1401a45ae5f8SJohn Marino 				   void *data)
1402a45ae5f8SJohn Marino {
1403a45ae5f8SJohn Marino   long storage_needed = bfd_get_dynamic_symtab_upper_bound (abfd);
1404a45ae5f8SJohn Marino   CORE_ADDR symaddr = 0;
1405a45ae5f8SJohn Marino 
1406a45ae5f8SJohn Marino   if (storage_needed > 0)
1407a45ae5f8SJohn Marino     {
1408a45ae5f8SJohn Marino       unsigned int i;
1409a45ae5f8SJohn Marino       asymbol **symbol_table = (asymbol **) xmalloc (storage_needed);
1410a45ae5f8SJohn Marino       struct cleanup *back_to = make_cleanup (xfree, symbol_table);
1411a45ae5f8SJohn Marino       unsigned int number_of_symbols =
1412a45ae5f8SJohn Marino 	bfd_canonicalize_dynamic_symtab (abfd, symbol_table);
1413a45ae5f8SJohn Marino 
1414a45ae5f8SJohn Marino       for (i = 0; i < number_of_symbols; i++)
1415a45ae5f8SJohn Marino 	{
1416a45ae5f8SJohn Marino 	  asymbol *sym = *symbol_table++;
1417a45ae5f8SJohn Marino 
1418a45ae5f8SJohn Marino 	  if (match_sym (sym, data))
1419a45ae5f8SJohn Marino 	    {
1420a45ae5f8SJohn Marino 	      /* BFD symbols are section relative.  */
1421a45ae5f8SJohn Marino 	      symaddr = sym->value + sym->section->vma;
1422a45ae5f8SJohn Marino 	      break;
1423a45ae5f8SJohn Marino 	    }
1424a45ae5f8SJohn Marino 	}
1425a45ae5f8SJohn Marino       do_cleanups (back_to);
1426a45ae5f8SJohn Marino     }
1427a45ae5f8SJohn Marino   return symaddr;
1428a45ae5f8SJohn Marino }
1429a45ae5f8SJohn Marino 
1430a45ae5f8SJohn Marino /* Lookup the value for a specific symbol from symbol table and dynamic
1431a45ae5f8SJohn Marino    symbol table.  Look up symbol from ABFD.  MATCH_SYM is a callback
1432a45ae5f8SJohn Marino    function to determine whether to pick up a symbol.  DATA is the
1433a45ae5f8SJohn Marino    input of this callback function.  Return NULL if symbol is not
1434a45ae5f8SJohn Marino    found.  */
1435a45ae5f8SJohn Marino 
1436a45ae5f8SJohn Marino CORE_ADDR
gdb_bfd_lookup_symbol(bfd * abfd,int (* match_sym)(asymbol *,void *),void * data)1437a45ae5f8SJohn Marino gdb_bfd_lookup_symbol (bfd *abfd,
1438a45ae5f8SJohn Marino 		       int (*match_sym) (asymbol *, void *),
1439a45ae5f8SJohn Marino 		       void *data)
1440a45ae5f8SJohn Marino {
1441a45ae5f8SJohn Marino   CORE_ADDR symaddr = gdb_bfd_lookup_symbol_from_symtab (abfd, match_sym, data);
1442a45ae5f8SJohn Marino 
1443a45ae5f8SJohn Marino   /* On FreeBSD, the dynamic linker is stripped by default.  So we'll
1444a45ae5f8SJohn Marino      have to check the dynamic string table too.  */
1445a45ae5f8SJohn Marino   if (symaddr == 0)
1446a45ae5f8SJohn Marino     symaddr = bfd_lookup_symbol_from_dyn_symtab (abfd, match_sym, data);
1447a45ae5f8SJohn Marino 
1448a45ae5f8SJohn Marino   return symaddr;
1449a45ae5f8SJohn Marino }
14505796c8dcSSimon Schubert 
14515796c8dcSSimon Schubert extern initialize_file_ftype _initialize_solib; /* -Wmissing-prototypes */
14525796c8dcSSimon Schubert 
14535796c8dcSSimon Schubert void
_initialize_solib(void)14545796c8dcSSimon Schubert _initialize_solib (void)
14555796c8dcSSimon Schubert {
14565796c8dcSSimon Schubert   solib_data = gdbarch_data_register_pre_init (solib_init);
14575796c8dcSSimon Schubert 
14585796c8dcSSimon Schubert   add_com ("sharedlibrary", class_files, sharedlibrary_command,
14595796c8dcSSimon Schubert 	   _("Load shared object library symbols for files matching REGEXP."));
14605796c8dcSSimon Schubert   add_info ("sharedlibrary", info_sharedlibrary_command,
14615796c8dcSSimon Schubert 	    _("Status of loaded shared object libraries."));
14625796c8dcSSimon Schubert   add_com ("nosharedlibrary", class_files, no_shared_libraries,
14635796c8dcSSimon Schubert 	   _("Unload all shared object library symbols."));
14645796c8dcSSimon Schubert 
14655796c8dcSSimon Schubert   add_setshow_boolean_cmd ("auto-solib-add", class_support,
14665796c8dcSSimon Schubert 			   &auto_solib_add, _("\
14675796c8dcSSimon Schubert Set autoloading of shared library symbols."), _("\
14685796c8dcSSimon Schubert Show autoloading of shared library symbols."), _("\
14695796c8dcSSimon Schubert If \"on\", symbols from all shared object libraries will be loaded\n\
14705796c8dcSSimon Schubert automatically when the inferior begins execution, when the dynamic linker\n\
14715796c8dcSSimon Schubert informs gdb that a new library has been loaded, or when attaching to the\n\
1472c50c785cSJohn Marino inferior.  Otherwise, symbols must be loaded manually, using \
1473c50c785cSJohn Marino `sharedlibrary'."),
14745796c8dcSSimon Schubert 			   NULL,
14755796c8dcSSimon Schubert 			   show_auto_solib_add,
14765796c8dcSSimon Schubert 			   &setlist, &showlist);
14775796c8dcSSimon Schubert 
14785796c8dcSSimon Schubert   add_setshow_filename_cmd ("sysroot", class_support,
14795796c8dcSSimon Schubert 			    &gdb_sysroot, _("\
14805796c8dcSSimon Schubert Set an alternate system root."), _("\
14815796c8dcSSimon Schubert Show the current system root."), _("\
14825796c8dcSSimon Schubert The system root is used to load absolute shared library symbol files.\n\
14835796c8dcSSimon Schubert For other (relative) files, you can add directories using\n\
14845796c8dcSSimon Schubert `set solib-search-path'."),
14855796c8dcSSimon Schubert 			    reload_shared_libraries,
14865796c8dcSSimon Schubert 			    NULL,
14875796c8dcSSimon Schubert 			    &setlist, &showlist);
14885796c8dcSSimon Schubert 
14895796c8dcSSimon Schubert   add_alias_cmd ("solib-absolute-prefix", "sysroot", class_support, 0,
14905796c8dcSSimon Schubert 		 &setlist);
14915796c8dcSSimon Schubert   add_alias_cmd ("solib-absolute-prefix", "sysroot", class_support, 0,
14925796c8dcSSimon Schubert 		 &showlist);
14935796c8dcSSimon Schubert 
14945796c8dcSSimon Schubert   add_setshow_optional_filename_cmd ("solib-search-path", class_support,
14955796c8dcSSimon Schubert 				     &solib_search_path, _("\
1496c50c785cSJohn Marino Set the search path for loading non-absolute shared library symbol files."),
1497c50c785cSJohn Marino 				     _("\
1498c50c785cSJohn Marino Show the search path for loading non-absolute shared library symbol files."),
1499c50c785cSJohn Marino 				     _("\
1500c50c785cSJohn Marino This takes precedence over the environment variables \
1501c50c785cSJohn Marino PATH and LD_LIBRARY_PATH."),
15025796c8dcSSimon Schubert 				     reload_shared_libraries,
15035796c8dcSSimon Schubert 				     show_solib_search_path,
15045796c8dcSSimon Schubert 				     &setlist, &showlist);
15055796c8dcSSimon Schubert }
1506