Initial import
[gdb.git] / gdb / solib.c
blobf687558180fa9c680b53f4992b1f92250707530f
1 /* Handle shared libraries for GDB, the GNU Debugger.
3 Copyright (C) 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
4 2000, 2001, 2002, 2003, 2005, 2006, 2007 Free Software Foundation, Inc.
6 This file is part of GDB.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
21 #include "defs.h"
23 #include <sys/types.h>
24 #include <fcntl.h>
25 #include "gdb_string.h"
26 #include "symtab.h"
27 #include "bfd.h"
28 #include "symfile.h"
29 #include "objfiles.h"
30 #include "exceptions.h"
31 #include "gdbcore.h"
32 #include "command.h"
33 #include "target.h"
34 #include "frame.h"
35 #include "gdb_regex.h"
36 #include "inferior.h"
37 #include "environ.h"
38 #include "language.h"
39 #include "gdbcmd.h"
40 #include "completer.h"
41 #include "filenames.h" /* for DOSish file names */
42 #include "exec.h"
43 #include "solist.h"
44 #include "observer.h"
45 #include "readline/readline.h"
47 /* Architecture-specific operations. */
49 /* Per-architecture data key. */
50 static struct gdbarch_data *solib_data;
52 static void *
53 solib_init (struct obstack *obstack)
55 struct target_so_ops **ops;
57 ops = OBSTACK_ZALLOC (obstack, struct target_so_ops *);
58 *ops = current_target_so_ops;
59 return ops;
62 static struct target_so_ops *
63 solib_ops (struct gdbarch *gdbarch)
65 struct target_so_ops **ops = gdbarch_data (gdbarch, solib_data);
66 return *ops;
69 /* Set the solib operations for GDBARCH to NEW_OPS. */
71 void
72 set_solib_ops (struct gdbarch *gdbarch, struct target_so_ops *new_ops)
74 struct target_so_ops **ops = gdbarch_data (gdbarch, solib_data);
75 *ops = new_ops;
79 /* external data declarations */
81 /* FIXME: gdbarch needs to control this variable, or else every
82 configuration needs to call set_solib_ops. */
83 struct target_so_ops *current_target_so_ops;
85 /* local data declarations */
87 static struct so_list *so_list_head; /* List of known shared objects */
89 static int solib_cleanup_queued = 0; /* make_run_cleanup called */
91 /* Local function prototypes */
93 static void do_clear_solib (void *);
95 /* If non-empty, this is a search path for loading non-absolute shared library
96 symbol files. This takes precedence over the environment variables PATH
97 and LD_LIBRARY_PATH. */
98 static char *solib_search_path = NULL;
99 static void
100 show_solib_search_path (struct ui_file *file, int from_tty,
101 struct cmd_list_element *c, const char *value)
103 fprintf_filtered (file, _("\
104 The search path for loading non-absolute shared library symbol files is %s.\n"),
105 value);
110 GLOBAL FUNCTION
112 solib_open -- Find a shared library file and open it.
114 SYNOPSIS
116 int solib_open (char *in_patname, char **found_pathname);
118 DESCRIPTION
120 Global variable GDB_SYSROOT is used as a prefix directory
121 to search for shared libraries if they have an absolute path.
123 Global variable SOLIB_SEARCH_PATH is used as a prefix directory
124 (or set of directories, as in LD_LIBRARY_PATH) to search for all
125 shared libraries if not found in GDB_SYSROOT.
127 Search algorithm:
128 * If there is a gdb_sysroot and path is absolute:
129 * Search for gdb_sysroot/path.
130 * else
131 * Look for it literally (unmodified).
132 * Look in SOLIB_SEARCH_PATH.
133 * If available, use target defined search function.
134 * If gdb_sysroot is NOT set, perform the following two searches:
135 * Look in inferior's $PATH.
136 * Look in inferior's $LD_LIBRARY_PATH.
138 * The last check avoids doing this search when targetting remote
139 * machines since gdb_sysroot will almost always be set.
141 RETURNS
143 file handle for opened solib, or -1 for failure. */
146 solib_open (char *in_pathname, char **found_pathname)
148 struct target_so_ops *ops = solib_ops (current_gdbarch);
149 int found_file = -1;
150 char *temp_pathname = NULL;
151 char *p = in_pathname;
152 int gdb_sysroot_is_empty;
154 gdb_sysroot_is_empty = (gdb_sysroot == NULL || *gdb_sysroot == 0);
156 if (! IS_ABSOLUTE_PATH (in_pathname) || gdb_sysroot_is_empty)
157 temp_pathname = in_pathname;
158 else
160 int prefix_len = strlen (gdb_sysroot);
162 /* Remove trailing slashes from absolute prefix. */
163 while (prefix_len > 0
164 && IS_DIR_SEPARATOR (gdb_sysroot[prefix_len - 1]))
165 prefix_len--;
167 /* Cat the prefixed pathname together. */
168 temp_pathname = alloca (prefix_len + strlen (in_pathname) + 1);
169 strncpy (temp_pathname, gdb_sysroot, prefix_len);
170 temp_pathname[prefix_len] = '\0';
171 strcat (temp_pathname, in_pathname);
174 /* Now see if we can open it. */
175 found_file = open (temp_pathname, O_RDONLY | O_BINARY, 0);
177 /* We try to find the library in various ways. After each attempt
178 (except for the one above), either found_file >= 0 and
179 temp_pathname is a malloc'd string, or found_file < 0 and
180 temp_pathname does not point to storage that needs to be
181 freed. */
183 if (found_file < 0)
184 temp_pathname = NULL;
185 else
186 temp_pathname = xstrdup (temp_pathname);
188 /* If the search in gdb_sysroot failed, and the path name is
189 absolute at this point, make it relative. (openp will try and open the
190 file according to its absolute path otherwise, which is not what we want.)
191 Affects subsequent searches for this solib. */
192 if (found_file < 0 && IS_ABSOLUTE_PATH (in_pathname))
194 /* First, get rid of any drive letters etc. */
195 while (!IS_DIR_SEPARATOR (*in_pathname))
196 in_pathname++;
198 /* Next, get rid of all leading dir separators. */
199 while (IS_DIR_SEPARATOR (*in_pathname))
200 in_pathname++;
203 /* If not found, search the solib_search_path (if any). */
204 if (found_file < 0 && solib_search_path != NULL)
205 found_file = openp (solib_search_path, OPF_TRY_CWD_FIRST,
206 in_pathname, O_RDONLY | O_BINARY, 0, &temp_pathname);
208 /* If not found, next search the solib_search_path (if any) for the basename
209 only (ignoring the path). This is to allow reading solibs from a path
210 that differs from the opened path. */
211 if (found_file < 0 && solib_search_path != NULL)
212 found_file = openp (solib_search_path, OPF_TRY_CWD_FIRST,
213 lbasename (in_pathname), O_RDONLY | O_BINARY, 0,
214 &temp_pathname);
216 /* If not found, try to use target supplied solib search method */
217 if (found_file < 0 && ops->find_and_open_solib)
218 found_file = ops->find_and_open_solib (in_pathname, O_RDONLY | O_BINARY,
219 &temp_pathname);
221 /* If not found, next search the inferior's $PATH environment variable. */
222 if (found_file < 0 && gdb_sysroot_is_empty)
223 found_file = openp (get_in_environ (inferior_environ, "PATH"),
224 OPF_TRY_CWD_FIRST, in_pathname, O_RDONLY | O_BINARY, 0,
225 &temp_pathname);
227 /* If not found, next search the inferior's $LD_LIBRARY_PATH
228 environment variable. */
229 if (found_file < 0 && gdb_sysroot_is_empty)
230 found_file = openp (get_in_environ (inferior_environ, "LD_LIBRARY_PATH"),
231 OPF_TRY_CWD_FIRST, in_pathname, O_RDONLY | O_BINARY, 0,
232 &temp_pathname);
234 /* Done. If not found, tough luck. Return found_file and
235 (optionally) found_pathname. */
236 if (temp_pathname)
238 if (found_pathname != NULL)
239 *found_pathname = temp_pathname;
240 else
241 xfree (temp_pathname);
243 return found_file;
249 LOCAL FUNCTION
251 solib_map_sections -- open bfd and build sections for shared lib
253 SYNOPSIS
255 static int solib_map_sections (struct so_list *so)
257 DESCRIPTION
259 Given a pointer to one of the shared objects in our list
260 of mapped objects, use the recorded name to open a bfd
261 descriptor for the object, build a section table, and then
262 relocate all the section addresses by the base address at
263 which the shared object was mapped.
265 FIXMES
267 In most (all?) cases the shared object file name recorded in the
268 dynamic linkage tables will be a fully qualified pathname. For
269 cases where it isn't, do we really mimic the systems search
270 mechanism correctly in the below code (particularly the tilde
271 expansion stuff?).
274 static int
275 solib_map_sections (void *arg)
277 struct so_list *so = (struct so_list *) arg; /* catch_errors bogon */
278 char *filename;
279 char *scratch_pathname;
280 int scratch_chan;
281 struct section_table *p;
282 struct cleanup *old_chain;
283 bfd *abfd;
285 filename = tilde_expand (so->so_name);
287 old_chain = make_cleanup (xfree, filename);
288 scratch_chan = solib_open (filename, &scratch_pathname);
290 if (scratch_chan < 0)
292 perror_with_name (filename);
295 /* Leave scratch_pathname allocated. abfd->name will point to it. */
296 abfd = bfd_fopen (scratch_pathname, gnutarget, FOPEN_RB, scratch_chan);
297 if (!abfd)
299 close (scratch_chan);
300 error (_("Could not open `%s' as an executable file: %s"),
301 scratch_pathname, bfd_errmsg (bfd_get_error ()));
304 /* Leave bfd open, core_xfer_memory and "info files" need it. */
305 so->abfd = abfd;
306 bfd_set_cacheable (abfd, 1);
308 /* copy full path name into so_name, so that later symbol_file_add
309 can find it */
310 if (strlen (scratch_pathname) >= SO_NAME_MAX_PATH_SIZE)
311 error (_("Full path name length of shared library exceeds SO_NAME_MAX_PATH_SIZE in so_list structure."));
312 strcpy (so->so_name, scratch_pathname);
314 if (!bfd_check_format (abfd, bfd_object))
316 error (_("\"%s\": not in executable format: %s."),
317 scratch_pathname, bfd_errmsg (bfd_get_error ()));
319 if (build_section_table (abfd, &so->sections, &so->sections_end))
321 error (_("Can't find the file sections in `%s': %s"),
322 bfd_get_filename (abfd), bfd_errmsg (bfd_get_error ()));
325 for (p = so->sections; p < so->sections_end; p++)
327 struct target_so_ops *ops = solib_ops (current_gdbarch);
329 /* Relocate the section binding addresses as recorded in the shared
330 object's file by the base address to which the object was actually
331 mapped. */
332 ops->relocate_section_addresses (so, p);
334 /* If the target didn't provide information about the address
335 range of the shared object, assume we want the location of
336 the .text section. */
337 if (so->addr_low == 0 && so->addr_high == 0
338 && strcmp (p->the_bfd_section->name, ".text") == 0)
340 so->addr_low = p->addr;
341 so->addr_high = p->endaddr;
345 /* Free the file names, close the file now. */
346 do_cleanups (old_chain);
348 return (1);
351 /* LOCAL FUNCTION
353 free_so --- free a `struct so_list' object
355 SYNOPSIS
357 void free_so (struct so_list *so)
359 DESCRIPTION
361 Free the storage associated with the `struct so_list' object SO.
362 If we have opened a BFD for SO, close it.
364 The caller is responsible for removing SO from whatever list it is
365 a member of. If we have placed SO's sections in some target's
366 section table, the caller is responsible for removing them.
368 This function doesn't mess with objfiles at all. If there is an
369 objfile associated with SO that needs to be removed, the caller is
370 responsible for taking care of that. */
372 void
373 free_so (struct so_list *so)
375 struct target_so_ops *ops = solib_ops (current_gdbarch);
376 char *bfd_filename = 0;
378 if (so->sections)
379 xfree (so->sections);
381 if (so->abfd)
383 bfd_filename = bfd_get_filename (so->abfd);
384 if (! bfd_close (so->abfd))
385 warning (_("cannot close \"%s\": %s"),
386 bfd_filename, bfd_errmsg (bfd_get_error ()));
389 if (bfd_filename)
390 xfree (bfd_filename);
392 ops->free_so (so);
394 xfree (so);
398 /* Return address of first so_list entry in master shared object list. */
399 struct so_list *
400 master_so_list (void)
402 return so_list_head;
406 /* A small stub to get us past the arg-passing pinhole of catch_errors. */
408 static int
409 symbol_add_stub (void *arg)
411 struct so_list *so = (struct so_list *) arg; /* catch_errs bogon */
412 struct section_addr_info *sap;
414 /* Have we already loaded this shared object? */
415 ALL_OBJFILES (so->objfile)
417 if (strcmp (so->objfile->name, so->so_name) == 0)
418 return 1;
421 sap = build_section_addr_info_from_section_table (so->sections,
422 so->sections_end);
424 so->objfile = symbol_file_add (so->so_name, so->from_tty,
425 sap, 0, OBJF_SHARED);
426 free_section_addr_info (sap);
428 return (1);
431 /* Read in symbols for shared object SO. If FROM_TTY is non-zero, be
432 chatty about it. Return non-zero if any symbols were actually
433 loaded. */
436 solib_read_symbols (struct so_list *so, int from_tty)
438 if (so->symbols_loaded)
440 if (from_tty)
441 printf_unfiltered (_("Symbols already loaded for %s\n"), so->so_name);
443 else if (so->abfd == NULL)
445 if (from_tty)
446 printf_unfiltered (_("Symbol file not found for %s\n"), so->so_name);
448 else
450 if (catch_errors (symbol_add_stub, so,
451 "Error while reading shared library symbols:\n",
452 RETURN_MASK_ALL))
454 if (from_tty)
455 printf_unfiltered (_("Loaded symbols for %s\n"), so->so_name);
456 so->symbols_loaded = 1;
457 return 1;
461 return 0;
464 /* LOCAL FUNCTION
466 update_solib_list --- synchronize GDB's shared object list with inferior's
468 SYNOPSIS
470 void update_solib_list (int from_tty, struct target_ops *TARGET)
472 Extract the list of currently loaded shared objects from the
473 inferior, and compare it with the list of shared objects currently
474 in GDB's so_list_head list. Edit so_list_head to bring it in sync
475 with the inferior's new list.
477 If we notice that the inferior has unloaded some shared objects,
478 free any symbolic info GDB had read about those shared objects.
480 Don't load symbolic info for any new shared objects; just add them
481 to the list, and leave their symbols_loaded flag clear.
483 If FROM_TTY is non-null, feel free to print messages about what
484 we're doing.
486 If TARGET is non-null, add the sections of all new shared objects
487 to TARGET's section table. Note that this doesn't remove any
488 sections for shared objects that have been unloaded, and it
489 doesn't check to see if the new shared objects are already present in
490 the section table. But we only use this for core files and
491 processes we've just attached to, so that's okay. */
493 static void
494 update_solib_list (int from_tty, struct target_ops *target)
496 struct target_so_ops *ops = solib_ops (current_gdbarch);
497 struct so_list *inferior = ops->current_sos();
498 struct so_list *gdb, **gdb_link;
500 /* If we are attaching to a running process for which we
501 have not opened a symbol file, we may be able to get its
502 symbols now! */
503 if (attach_flag &&
504 symfile_objfile == NULL)
505 catch_errors (ops->open_symbol_file_object, &from_tty,
506 "Error reading attached process's symbol file.\n",
507 RETURN_MASK_ALL);
509 /* Since this function might actually add some elements to the
510 so_list_head list, arrange for it to be cleaned up when
511 appropriate. */
512 if (!solib_cleanup_queued)
514 make_run_cleanup (do_clear_solib, NULL);
515 solib_cleanup_queued = 1;
518 /* GDB and the inferior's dynamic linker each maintain their own
519 list of currently loaded shared objects; we want to bring the
520 former in sync with the latter. Scan both lists, seeing which
521 shared objects appear where. There are three cases:
523 - A shared object appears on both lists. This means that GDB
524 knows about it already, and it's still loaded in the inferior.
525 Nothing needs to happen.
527 - A shared object appears only on GDB's list. This means that
528 the inferior has unloaded it. We should remove the shared
529 object from GDB's tables.
531 - A shared object appears only on the inferior's list. This
532 means that it's just been loaded. We should add it to GDB's
533 tables.
535 So we walk GDB's list, checking each entry to see if it appears
536 in the inferior's list too. If it does, no action is needed, and
537 we remove it from the inferior's list. If it doesn't, the
538 inferior has unloaded it, and we remove it from GDB's list. By
539 the time we're done walking GDB's list, the inferior's list
540 contains only the new shared objects, which we then add. */
542 gdb = so_list_head;
543 gdb_link = &so_list_head;
544 while (gdb)
546 struct so_list *i = inferior;
547 struct so_list **i_link = &inferior;
549 /* Check to see whether the shared object *gdb also appears in
550 the inferior's current list. */
551 while (i)
553 if (! strcmp (gdb->so_original_name, i->so_original_name))
554 break;
556 i_link = &i->next;
557 i = *i_link;
560 /* If the shared object appears on the inferior's list too, then
561 it's still loaded, so we don't need to do anything. Delete
562 it from the inferior's list, and leave it on GDB's list. */
563 if (i)
565 *i_link = i->next;
566 free_so (i);
567 gdb_link = &gdb->next;
568 gdb = *gdb_link;
571 /* If it's not on the inferior's list, remove it from GDB's tables. */
572 else
574 /* Notify any observer that the shared object has been
575 unloaded before we remove it from GDB's tables. */
576 observer_notify_solib_unloaded (gdb);
578 *gdb_link = gdb->next;
580 /* Unless the user loaded it explicitly, free SO's objfile. */
581 if (gdb->objfile && ! (gdb->objfile->flags & OBJF_USERLOADED))
582 free_objfile (gdb->objfile);
584 /* Some targets' section tables might be referring to
585 sections from so->abfd; remove them. */
586 remove_target_sections (gdb->abfd);
588 free_so (gdb);
589 gdb = *gdb_link;
593 /* Now the inferior's list contains only shared objects that don't
594 appear in GDB's list --- those that are newly loaded. Add them
595 to GDB's shared object list. */
596 if (inferior)
598 struct so_list *i;
600 /* Add the new shared objects to GDB's list. */
601 *gdb_link = inferior;
603 /* Fill in the rest of each of the `struct so_list' nodes. */
604 for (i = inferior; i; i = i->next)
606 i->from_tty = from_tty;
608 /* Fill in the rest of the `struct so_list' node. */
609 catch_errors (solib_map_sections, i,
610 "Error while mapping shared library sections:\n",
611 RETURN_MASK_ALL);
613 /* If requested, add the shared object's sections to the TARGET's
614 section table. Do this immediately after mapping the object so
615 that later nodes in the list can query this object, as is needed
616 in solib-osf.c. */
617 if (target)
619 int count = (i->sections_end - i->sections);
620 if (count > 0)
622 int space = target_resize_to_sections (target, count);
623 memcpy (target->to_sections + space,
624 i->sections,
625 count * sizeof (i->sections[0]));
629 /* Notify any observer that the shared object has been
630 loaded now that we've added it to GDB's tables. */
631 observer_notify_solib_loaded (i);
636 /* Return non-zero if SO is the libpthread shared library.
638 Uses a fairly simplistic heuristic approach where we check
639 the file name against "/libpthread". This can lead to false
640 positives, but this should be good enough in practice. */
642 static int
643 libpthread_solib_p (struct so_list *so)
645 return (strstr (so->so_name, "/libpthread") != NULL);
648 /* GLOBAL FUNCTION
650 solib_add -- read in symbol info for newly added shared libraries
652 SYNOPSIS
654 void solib_add (char *pattern, int from_tty, struct target_ops
655 *TARGET, int readsyms)
657 DESCRIPTION
659 Read in symbolic information for any shared objects whose names
660 match PATTERN. (If we've already read a shared object's symbol
661 info, leave it alone.) If PATTERN is zero, read them all.
663 If READSYMS is 0, defer reading symbolic information until later
664 but still do any needed low level processing.
666 FROM_TTY and TARGET are as described for update_solib_list, above. */
668 void
669 solib_add (char *pattern, int from_tty, struct target_ops *target, int readsyms)
671 struct so_list *gdb;
673 if (pattern)
675 char *re_err = re_comp (pattern);
677 if (re_err)
678 error (_("Invalid regexp: %s"), re_err);
681 update_solib_list (from_tty, target);
683 /* Walk the list of currently loaded shared libraries, and read
684 symbols for any that match the pattern --- or any whose symbols
685 aren't already loaded, if no pattern was given. */
687 int any_matches = 0;
688 int loaded_any_symbols = 0;
690 for (gdb = so_list_head; gdb; gdb = gdb->next)
691 if (! pattern || re_exec (gdb->so_name))
693 /* Normally, we would read the symbols from that library
694 only if READSYMS is set. However, we're making a small
695 exception for the pthread library, because we sometimes
696 need the library symbols to be loaded in order to provide
697 thread support (x86-linux for instance). */
698 const int add_this_solib =
699 (readsyms || libpthread_solib_p (gdb));
701 any_matches = 1;
702 if (add_this_solib && solib_read_symbols (gdb, from_tty))
703 loaded_any_symbols = 1;
706 if (from_tty && pattern && ! any_matches)
707 printf_unfiltered
708 ("No loaded shared libraries match the pattern `%s'.\n", pattern);
710 if (loaded_any_symbols)
712 struct target_so_ops *ops = solib_ops (current_gdbarch);
714 /* Getting new symbols may change our opinion about what is
715 frameless. */
716 reinit_frame_cache ();
718 ops->special_symbol_handling ();
726 LOCAL FUNCTION
728 info_sharedlibrary_command -- code for "info sharedlibrary"
730 SYNOPSIS
732 static void info_sharedlibrary_command ()
734 DESCRIPTION
736 Walk through the shared library list and print information
737 about each attached library.
740 static void
741 info_sharedlibrary_command (char *ignore, int from_tty)
743 struct so_list *so = NULL; /* link map state variable */
744 int header_done = 0;
745 int addr_width;
747 /* "0x", a little whitespace, and two hex digits per byte of pointers. */
748 addr_width = 4 + (gdbarch_ptr_bit (current_gdbarch) / 4);
750 update_solib_list (from_tty, 0);
752 for (so = so_list_head; so; so = so->next)
754 if (so->so_name[0])
756 if (!header_done)
758 printf_unfiltered ("%-*s%-*s%-12s%s\n", addr_width, "From",
759 addr_width, "To", "Syms Read",
760 "Shared Object Library");
761 header_done++;
764 printf_unfiltered ("%-*s", addr_width,
765 so->addr_high != 0
766 ? hex_string_custom (
767 (LONGEST) so->addr_low,
768 addr_width - 4)
769 : "");
770 printf_unfiltered ("%-*s", addr_width,
771 so->addr_high != 0
772 ? hex_string_custom (
773 (LONGEST) so->addr_high,
774 addr_width - 4)
775 : "");
776 printf_unfiltered ("%-12s", so->symbols_loaded ? "Yes" : "No");
777 printf_unfiltered ("%s\n", so->so_name);
780 if (so_list_head == NULL)
782 printf_unfiltered (_("No shared libraries loaded at this time.\n"));
788 GLOBAL FUNCTION
790 solib_address -- check to see if an address is in a shared lib
792 SYNOPSIS
794 char * solib_address (CORE_ADDR address)
796 DESCRIPTION
798 Provides a hook for other gdb routines to discover whether or
799 not a particular address is within the mapped address space of
800 a shared library.
802 For example, this routine is called at one point to disable
803 breakpoints which are in shared libraries that are not currently
804 mapped in.
807 char *
808 solib_address (CORE_ADDR address)
810 struct so_list *so = 0; /* link map state variable */
812 for (so = so_list_head; so; so = so->next)
814 struct section_table *p;
816 for (p = so->sections; p < so->sections_end; p++)
818 if (p->addr <= address && address < p->endaddr)
819 return (so->so_name);
823 return (0);
826 /* Called by free_all_symtabs */
828 void
829 clear_solib (void)
831 struct target_so_ops *ops = solib_ops (current_gdbarch);
833 /* This function is expected to handle ELF shared libraries. It is
834 also used on Solaris, which can run either ELF or a.out binaries
835 (for compatibility with SunOS 4), both of which can use shared
836 libraries. So we don't know whether we have an ELF executable or
837 an a.out executable until the user chooses an executable file.
839 ELF shared libraries don't get mapped into the address space
840 until after the program starts, so we'd better not try to insert
841 breakpoints in them immediately. We have to wait until the
842 dynamic linker has loaded them; we'll hit a bp_shlib_event
843 breakpoint (look for calls to create_solib_event_breakpoint) when
844 it's ready.
846 SunOS shared libraries seem to be different --- they're present
847 as soon as the process begins execution, so there's no need to
848 put off inserting breakpoints. There's also nowhere to put a
849 bp_shlib_event breakpoint, so if we put it off, we'll never get
850 around to it.
852 So: disable breakpoints only if we're using ELF shared libs. */
853 if (exec_bfd != NULL
854 && bfd_get_flavour (exec_bfd) != bfd_target_aout_flavour)
855 disable_breakpoints_in_shlibs ();
857 while (so_list_head)
859 struct so_list *so = so_list_head;
860 so_list_head = so->next;
861 if (so->abfd)
862 remove_target_sections (so->abfd);
863 free_so (so);
866 ops->clear_solib ();
869 static void
870 do_clear_solib (void *dummy)
872 solib_cleanup_queued = 0;
873 clear_solib ();
876 /* GLOBAL FUNCTION
878 solib_create_inferior_hook -- shared library startup support
880 SYNOPSIS
882 void solib_create_inferior_hook ()
884 DESCRIPTION
886 When gdb starts up the inferior, it nurses it along (through the
887 shell) until it is ready to execute it's first instruction. At this
888 point, this function gets called via expansion of the macro
889 SOLIB_CREATE_INFERIOR_HOOK. */
891 void
892 solib_create_inferior_hook (void)
894 struct target_so_ops *ops = solib_ops (current_gdbarch);
895 ops->solib_create_inferior_hook();
898 /* GLOBAL FUNCTION
900 in_solib_dynsym_resolve_code -- check to see if an address is in
901 dynamic loader's dynamic symbol
902 resolution code
904 SYNOPSIS
906 int in_solib_dynsym_resolve_code (CORE_ADDR pc)
908 DESCRIPTION
910 Determine if PC is in the dynamic linker's symbol resolution
911 code. Return 1 if so, 0 otherwise.
915 in_solib_dynsym_resolve_code (CORE_ADDR pc)
917 struct target_so_ops *ops = solib_ops (current_gdbarch);
918 return ops->in_dynsym_resolve_code (pc);
923 LOCAL FUNCTION
925 sharedlibrary_command -- handle command to explicitly add library
927 SYNOPSIS
929 static void sharedlibrary_command (char *args, int from_tty)
931 DESCRIPTION
935 static void
936 sharedlibrary_command (char *args, int from_tty)
938 dont_repeat ();
939 solib_add (args, from_tty, (struct target_ops *) 0, 1);
942 /* LOCAL FUNCTION
944 no_shared_libraries -- handle command to explicitly discard symbols
945 from shared libraries.
947 DESCRIPTION
949 Implements the command "nosharedlibrary", which discards symbols
950 that have been auto-loaded from shared libraries. Symbols from
951 shared libraries that were added by explicit request of the user
952 are not discarded. Also called from remote.c. */
954 void
955 no_shared_libraries (char *ignored, int from_tty)
957 objfile_purge_solibs ();
958 do_clear_solib (NULL);
961 static void
962 reload_shared_libraries (char *ignored, int from_tty,
963 struct cmd_list_element *e)
965 no_shared_libraries (NULL, from_tty);
966 solib_add (NULL, from_tty, NULL, auto_solib_add);
969 static void
970 show_auto_solib_add (struct ui_file *file, int from_tty,
971 struct cmd_list_element *c, const char *value)
973 fprintf_filtered (file, _("Autoloading of shared library symbols is %s.\n"),
974 value);
978 /* Handler for library-specific lookup of global symbol NAME in OBJFILE. Call
979 the library-specific handler if it is installed for the current target. */
981 struct symbol *
982 solib_global_lookup (const struct objfile *objfile,
983 const char *name,
984 const char *linkage_name,
985 const domain_enum domain,
986 struct symtab **symtab)
988 struct target_so_ops *ops = solib_ops (current_gdbarch);
990 if (ops->lookup_lib_global_symbol != NULL)
991 return ops->lookup_lib_global_symbol (objfile, name, linkage_name,
992 domain, symtab);
993 return NULL;
997 extern initialize_file_ftype _initialize_solib; /* -Wmissing-prototypes */
999 void
1000 _initialize_solib (void)
1002 struct cmd_list_element *c;
1004 solib_data = gdbarch_data_register_pre_init (solib_init);
1006 add_com ("sharedlibrary", class_files, sharedlibrary_command,
1007 _("Load shared object library symbols for files matching REGEXP."));
1008 add_info ("sharedlibrary", info_sharedlibrary_command,
1009 _("Status of loaded shared object libraries."));
1010 add_com ("nosharedlibrary", class_files, no_shared_libraries,
1011 _("Unload all shared object library symbols."));
1013 add_setshow_boolean_cmd ("auto-solib-add", class_support,
1014 &auto_solib_add, _("\
1015 Set autoloading of shared library symbols."), _("\
1016 Show autoloading of shared library symbols."), _("\
1017 If \"on\", symbols from all shared object libraries will be loaded\n\
1018 automatically when the inferior begins execution, when the dynamic linker\n\
1019 informs gdb that a new library has been loaded, or when attaching to the\n\
1020 inferior. Otherwise, symbols must be loaded manually, using `sharedlibrary'."),
1021 NULL,
1022 show_auto_solib_add,
1023 &setlist, &showlist);
1025 add_setshow_filename_cmd ("sysroot", class_support,
1026 &gdb_sysroot, _("\
1027 Set an alternate system root."), _("\
1028 Show the current system root."), _("\
1029 The system root is used to load absolute shared library symbol files.\n\
1030 For other (relative) files, you can add directories using\n\
1031 `set solib-search-path'."),
1032 reload_shared_libraries,
1033 NULL,
1034 &setlist, &showlist);
1036 add_alias_cmd ("solib-absolute-prefix", "sysroot", class_support, 0,
1037 &setlist);
1038 add_alias_cmd ("solib-absolute-prefix", "sysroot", class_support, 0,
1039 &showlist);
1041 add_setshow_optional_filename_cmd ("solib-search-path", class_support,
1042 &solib_search_path, _("\
1043 Set the search path for loading non-absolute shared library symbol files."), _("\
1044 Show the search path for loading non-absolute shared library symbol files."), _("\
1045 This takes precedence over the environment variables PATH and LD_LIBRARY_PATH."),
1046 reload_shared_libraries,
1047 show_solib_search_path,
1048 &setlist, &showlist);