Remove 'run_cleanup'.
[external/binutils.git] / gdb / solib.c
1 /* Handle shared libraries for GDB, the GNU Debugger.
2
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.
5
6    This file is part of GDB.
7
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.
12
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.
17
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/>.  */
20
21 #include "defs.h"
22
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"
46
47 /* Architecture-specific operations.  */
48
49 /* Per-architecture data key.  */
50 static struct gdbarch_data *solib_data;
51
52 static void *
53 solib_init (struct obstack *obstack)
54 {
55   struct target_so_ops **ops;
56
57   ops = OBSTACK_ZALLOC (obstack, struct target_so_ops *);
58   *ops = current_target_so_ops;
59   return ops;
60 }
61
62 static struct target_so_ops *
63 solib_ops (struct gdbarch *gdbarch)
64 {
65   struct target_so_ops **ops = gdbarch_data (gdbarch, solib_data);
66   return *ops;
67 }
68
69 /* Set the solib operations for GDBARCH to NEW_OPS.  */
70
71 void
72 set_solib_ops (struct gdbarch *gdbarch, struct target_so_ops *new_ops)
73 {
74   struct target_so_ops **ops = gdbarch_data (gdbarch, solib_data);
75   *ops = new_ops;
76 }
77 \f
78
79 /* external data declarations */
80
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;
84
85 /* local data declarations */
86
87 static struct so_list *so_list_head;    /* List of known shared objects */
88
89 /* Local function prototypes */
90
91 /* If non-empty, this is a search path for loading non-absolute shared library
92    symbol files.  This takes precedence over the environment variables PATH
93    and LD_LIBRARY_PATH.  */
94 static char *solib_search_path = NULL;
95 static void
96 show_solib_search_path (struct ui_file *file, int from_tty,
97                         struct cmd_list_element *c, const char *value)
98 {
99   fprintf_filtered (file, _("\
100 The search path for loading non-absolute shared library symbol files is %s.\n"),
101                     value);
102 }
103
104 /*
105
106    GLOBAL FUNCTION
107
108    solib_open -- Find a shared library file and open it.
109
110    SYNOPSIS
111
112    int solib_open (char *in_patname, char **found_pathname);
113
114    DESCRIPTION
115
116    Global variable GDB_SYSROOT is used as a prefix directory
117    to search for shared libraries if they have an absolute path.
118
119    Global variable SOLIB_SEARCH_PATH is used as a prefix directory
120    (or set of directories, as in LD_LIBRARY_PATH) to search for all
121    shared libraries if not found in GDB_SYSROOT.
122
123    Search algorithm:
124    * If there is a gdb_sysroot and path is absolute:
125    *   Search for gdb_sysroot/path.
126    * else
127    *   Look for it literally (unmodified).
128    * Look in SOLIB_SEARCH_PATH.
129    * If available, use target defined search function.
130    * If gdb_sysroot is NOT set, perform the following two searches:
131    *   Look in inferior's $PATH.
132    *   Look in inferior's $LD_LIBRARY_PATH.
133    *   
134    * The last check avoids doing this search when targetting remote
135    * machines since gdb_sysroot will almost always be set.
136
137    RETURNS
138
139    file handle for opened solib, or -1 for failure.  */
140
141 int
142 solib_open (char *in_pathname, char **found_pathname)
143 {
144   struct target_so_ops *ops = solib_ops (current_gdbarch);
145   int found_file = -1;
146   char *temp_pathname = NULL;
147   char *p = in_pathname;
148   int gdb_sysroot_is_empty;
149
150   gdb_sysroot_is_empty = (gdb_sysroot == NULL || *gdb_sysroot == 0);
151
152   if (! IS_ABSOLUTE_PATH (in_pathname) || gdb_sysroot_is_empty)
153     temp_pathname = in_pathname;
154   else
155     {
156       int prefix_len = strlen (gdb_sysroot);
157
158       /* Remove trailing slashes from absolute prefix.  */
159       while (prefix_len > 0
160              && IS_DIR_SEPARATOR (gdb_sysroot[prefix_len - 1]))
161         prefix_len--;
162
163       /* Cat the prefixed pathname together.  */
164       temp_pathname = alloca (prefix_len + strlen (in_pathname) + 1);
165       strncpy (temp_pathname, gdb_sysroot, prefix_len);
166       temp_pathname[prefix_len] = '\0';
167       strcat (temp_pathname, in_pathname);
168     }
169
170   /* Now see if we can open it.  */
171   found_file = open (temp_pathname, O_RDONLY | O_BINARY, 0);
172
173   /* We try to find the library in various ways.  After each attempt
174      (except for the one above), either found_file >= 0 and
175      temp_pathname is a malloc'd string, or found_file < 0 and
176      temp_pathname does not point to storage that needs to be
177      freed.  */
178
179     if (found_file < 0)
180       temp_pathname = NULL;
181     else
182       temp_pathname = xstrdup (temp_pathname);
183
184   /* If the search in gdb_sysroot failed, and the path name is
185      absolute at this point, make it relative.  (openp will try and open the
186      file according to its absolute path otherwise, which is not what we want.)
187      Affects subsequent searches for this solib.  */
188   if (found_file < 0 && IS_ABSOLUTE_PATH (in_pathname))
189     {
190       /* First, get rid of any drive letters etc.  */
191       while (!IS_DIR_SEPARATOR (*in_pathname))
192         in_pathname++;
193
194       /* Next, get rid of all leading dir separators.  */
195       while (IS_DIR_SEPARATOR (*in_pathname))
196         in_pathname++;
197     }
198   
199   /* If not found, search the solib_search_path (if any).  */
200   if (found_file < 0 && solib_search_path != NULL)
201     found_file = openp (solib_search_path, OPF_TRY_CWD_FIRST,
202                         in_pathname, O_RDONLY | O_BINARY, 0, &temp_pathname);
203   
204   /* If not found, next search the solib_search_path (if any) for the basename
205      only (ignoring the path).  This is to allow reading solibs from a path
206      that differs from the opened path.  */
207   if (found_file < 0 && solib_search_path != NULL)
208     found_file = openp (solib_search_path, OPF_TRY_CWD_FIRST,
209                         lbasename (in_pathname), O_RDONLY | O_BINARY, 0,
210                         &temp_pathname);
211
212   /* If not found, try to use target supplied solib search method */
213   if (found_file < 0 && ops->find_and_open_solib)
214     found_file = ops->find_and_open_solib (in_pathname, O_RDONLY | O_BINARY,
215                                            &temp_pathname);
216
217   /* If not found, next search the inferior's $PATH environment variable. */
218   if (found_file < 0 && gdb_sysroot_is_empty)
219     found_file = openp (get_in_environ (inferior_environ, "PATH"),
220                         OPF_TRY_CWD_FIRST, in_pathname, O_RDONLY | O_BINARY, 0,
221                         &temp_pathname);
222
223   /* If not found, next search the inferior's $LD_LIBRARY_PATH 
224      environment variable. */
225   if (found_file < 0 && gdb_sysroot_is_empty)
226     found_file = openp (get_in_environ (inferior_environ, "LD_LIBRARY_PATH"),
227                         OPF_TRY_CWD_FIRST, in_pathname, O_RDONLY | O_BINARY, 0,
228                         &temp_pathname);
229
230   /* Done.  If not found, tough luck.  Return found_file and 
231      (optionally) found_pathname.  */
232   if (temp_pathname)
233     {
234       if (found_pathname != NULL)
235         *found_pathname = temp_pathname;
236       else
237         xfree (temp_pathname);
238     }
239   return found_file;
240 }
241
242
243 /*
244
245    LOCAL FUNCTION
246
247    solib_map_sections -- open bfd and build sections for shared lib
248
249    SYNOPSIS
250
251    static int solib_map_sections (struct so_list *so)
252
253    DESCRIPTION
254
255    Given a pointer to one of the shared objects in our list
256    of mapped objects, use the recorded name to open a bfd
257    descriptor for the object, build a section table, and then
258    relocate all the section addresses by the base address at
259    which the shared object was mapped.
260
261    FIXMES
262
263    In most (all?) cases the shared object file name recorded in the
264    dynamic linkage tables will be a fully qualified pathname.  For
265    cases where it isn't, do we really mimic the systems search
266    mechanism correctly in the below code (particularly the tilde
267    expansion stuff?).
268  */
269
270 static int
271 solib_map_sections (void *arg)
272 {
273   struct so_list *so = (struct so_list *) arg;  /* catch_errors bogon */
274   char *filename;
275   char *scratch_pathname;
276   int scratch_chan;
277   struct section_table *p;
278   struct cleanup *old_chain;
279   bfd *abfd;
280
281   filename = tilde_expand (so->so_name);
282
283   old_chain = make_cleanup (xfree, filename);
284   scratch_chan = solib_open (filename, &scratch_pathname);
285
286   if (scratch_chan < 0)
287     {
288       perror_with_name (filename);
289     }
290
291   /* Leave scratch_pathname allocated.  abfd->name will point to it.  */
292   abfd = bfd_fopen (scratch_pathname, gnutarget, FOPEN_RB, scratch_chan);
293   if (!abfd)
294     {
295       close (scratch_chan);
296       error (_("Could not open `%s' as an executable file: %s"),
297              scratch_pathname, bfd_errmsg (bfd_get_error ()));
298     }
299
300   /* Leave bfd open, core_xfer_memory and "info files" need it.  */
301   so->abfd = abfd;
302   bfd_set_cacheable (abfd, 1);
303
304   /* copy full path name into so_name, so that later symbol_file_add
305      can find it */
306   if (strlen (scratch_pathname) >= SO_NAME_MAX_PATH_SIZE)
307     error (_("Full path name length of shared library exceeds SO_NAME_MAX_PATH_SIZE in so_list structure."));
308   strcpy (so->so_name, scratch_pathname);
309
310   if (!bfd_check_format (abfd, bfd_object))
311     {
312       error (_("\"%s\": not in executable format: %s."),
313              scratch_pathname, bfd_errmsg (bfd_get_error ()));
314     }
315   if (build_section_table (abfd, &so->sections, &so->sections_end))
316     {
317       error (_("Can't find the file sections in `%s': %s"),
318              bfd_get_filename (abfd), bfd_errmsg (bfd_get_error ()));
319     }
320
321   for (p = so->sections; p < so->sections_end; p++)
322     {
323       struct target_so_ops *ops = solib_ops (current_gdbarch);
324
325       /* Relocate the section binding addresses as recorded in the shared
326          object's file by the base address to which the object was actually
327          mapped. */
328       ops->relocate_section_addresses (so, p);
329
330       /* If the target didn't provide information about the address
331          range of the shared object, assume we want the location of
332          the .text section.  */
333       if (so->addr_low == 0 && so->addr_high == 0
334           && strcmp (p->the_bfd_section->name, ".text") == 0)
335         {
336           so->addr_low = p->addr;
337           so->addr_high = p->endaddr;
338         }
339     }
340
341   /* Free the file names, close the file now.  */
342   do_cleanups (old_chain);
343
344   return (1);
345 }
346
347 /* LOCAL FUNCTION
348
349    free_so --- free a `struct so_list' object
350
351    SYNOPSIS
352
353    void free_so (struct so_list *so)
354
355    DESCRIPTION
356
357    Free the storage associated with the `struct so_list' object SO.
358    If we have opened a BFD for SO, close it.  
359
360    The caller is responsible for removing SO from whatever list it is
361    a member of.  If we have placed SO's sections in some target's
362    section table, the caller is responsible for removing them.
363
364    This function doesn't mess with objfiles at all.  If there is an
365    objfile associated with SO that needs to be removed, the caller is
366    responsible for taking care of that.  */
367
368 void
369 free_so (struct so_list *so)
370 {
371   struct target_so_ops *ops = solib_ops (current_gdbarch);
372   char *bfd_filename = 0;
373
374   if (so->sections)
375     xfree (so->sections);
376       
377   if (so->abfd)
378     {
379       bfd_filename = bfd_get_filename (so->abfd);
380       if (! bfd_close (so->abfd))
381         warning (_("cannot close \"%s\": %s"),
382                  bfd_filename, bfd_errmsg (bfd_get_error ()));
383     }
384
385   if (bfd_filename)
386     xfree (bfd_filename);
387
388   ops->free_so (so);
389
390   xfree (so);
391 }
392
393
394 /* Return address of first so_list entry in master shared object list.  */
395 struct so_list *
396 master_so_list (void)
397 {
398   return so_list_head;
399 }
400
401
402 /* A small stub to get us past the arg-passing pinhole of catch_errors.  */
403
404 static int
405 symbol_add_stub (void *arg)
406 {
407   struct so_list *so = (struct so_list *) arg;  /* catch_errs bogon */
408   struct section_addr_info *sap;
409
410   /* Have we already loaded this shared object?  */
411   ALL_OBJFILES (so->objfile)
412     {
413       if (strcmp (so->objfile->name, so->so_name) == 0)
414         return 1;
415     }
416
417   sap = build_section_addr_info_from_section_table (so->sections,
418                                                     so->sections_end);
419
420   so->objfile = symbol_file_add (so->so_name, so->from_tty,
421                                  sap, 0, OBJF_SHARED);
422   free_section_addr_info (sap);
423
424   return (1);
425 }
426
427 /* Read in symbols for shared object SO.  If FROM_TTY is non-zero, be
428    chatty about it.  Return non-zero if any symbols were actually
429    loaded.  */
430
431 int
432 solib_read_symbols (struct so_list *so, int from_tty)
433 {
434   if (so->symbols_loaded)
435     {
436       if (from_tty)
437         printf_unfiltered (_("Symbols already loaded for %s\n"), so->so_name);
438     }
439   else if (so->abfd == NULL)
440     {
441       if (from_tty)
442         printf_unfiltered (_("Symbol file not found for %s\n"), so->so_name);
443     }
444   else
445     {
446       if (catch_errors (symbol_add_stub, so,
447                         "Error while reading shared library symbols:\n",
448                         RETURN_MASK_ALL))
449         {
450           if (from_tty)
451             printf_unfiltered (_("Loaded symbols for %s\n"), so->so_name);
452           so->symbols_loaded = 1;
453           return 1;
454         }
455     }
456
457   return 0;
458 }
459
460 /* LOCAL FUNCTION
461
462    update_solib_list --- synchronize GDB's shared object list with inferior's
463
464    SYNOPSIS
465
466    void update_solib_list (int from_tty, struct target_ops *TARGET)
467
468    Extract the list of currently loaded shared objects from the
469    inferior, and compare it with the list of shared objects currently
470    in GDB's so_list_head list.  Edit so_list_head to bring it in sync
471    with the inferior's new list.
472
473    If we notice that the inferior has unloaded some shared objects,
474    free any symbolic info GDB had read about those shared objects.
475
476    Don't load symbolic info for any new shared objects; just add them
477    to the list, and leave their symbols_loaded flag clear.
478
479    If FROM_TTY is non-null, feel free to print messages about what
480    we're doing.
481
482    If TARGET is non-null, add the sections of all new shared objects
483    to TARGET's section table.  Note that this doesn't remove any
484    sections for shared objects that have been unloaded, and it
485    doesn't check to see if the new shared objects are already present in
486    the section table.  But we only use this for core files and
487    processes we've just attached to, so that's okay.  */
488
489 static void
490 update_solib_list (int from_tty, struct target_ops *target)
491 {
492   struct target_so_ops *ops = solib_ops (current_gdbarch);
493   struct so_list *inferior = ops->current_sos();
494   struct so_list *gdb, **gdb_link;
495
496   /* If we are attaching to a running process for which we 
497      have not opened a symbol file, we may be able to get its 
498      symbols now!  */
499   if (attach_flag &&
500       symfile_objfile == NULL)
501     catch_errors (ops->open_symbol_file_object, &from_tty, 
502                   "Error reading attached process's symbol file.\n",
503                   RETURN_MASK_ALL);
504
505   /* GDB and the inferior's dynamic linker each maintain their own
506      list of currently loaded shared objects; we want to bring the
507      former in sync with the latter.  Scan both lists, seeing which
508      shared objects appear where.  There are three cases:
509
510      - A shared object appears on both lists.  This means that GDB
511      knows about it already, and it's still loaded in the inferior.
512      Nothing needs to happen.
513
514      - A shared object appears only on GDB's list.  This means that
515      the inferior has unloaded it.  We should remove the shared
516      object from GDB's tables.
517
518      - A shared object appears only on the inferior's list.  This
519      means that it's just been loaded.  We should add it to GDB's
520      tables.
521
522      So we walk GDB's list, checking each entry to see if it appears
523      in the inferior's list too.  If it does, no action is needed, and
524      we remove it from the inferior's list.  If it doesn't, the
525      inferior has unloaded it, and we remove it from GDB's list.  By
526      the time we're done walking GDB's list, the inferior's list
527      contains only the new shared objects, which we then add.  */
528
529   gdb = so_list_head;
530   gdb_link = &so_list_head;
531   while (gdb)
532     {
533       struct so_list *i = inferior;
534       struct so_list **i_link = &inferior;
535
536       /* Check to see whether the shared object *gdb also appears in
537          the inferior's current list.  */
538       while (i)
539         {
540           if (! strcmp (gdb->so_original_name, i->so_original_name))
541             break;
542
543           i_link = &i->next;
544           i = *i_link;
545         }
546
547       /* If the shared object appears on the inferior's list too, then
548          it's still loaded, so we don't need to do anything.  Delete
549          it from the inferior's list, and leave it on GDB's list.  */
550       if (i)
551         {
552           *i_link = i->next;
553           free_so (i);
554           gdb_link = &gdb->next;
555           gdb = *gdb_link;
556         }
557
558       /* If it's not on the inferior's list, remove it from GDB's tables.  */
559       else
560         {
561           /* Notify any observer that the shared object has been
562              unloaded before we remove it from GDB's tables.  */
563           observer_notify_solib_unloaded (gdb);
564
565           *gdb_link = gdb->next;
566
567           /* Unless the user loaded it explicitly, free SO's objfile.  */
568           if (gdb->objfile && ! (gdb->objfile->flags & OBJF_USERLOADED))
569             free_objfile (gdb->objfile);
570
571           /* Some targets' section tables might be referring to
572              sections from so->abfd; remove them.  */
573           remove_target_sections (gdb->abfd);
574
575           free_so (gdb);
576           gdb = *gdb_link;
577         }
578     }
579
580   /* Now the inferior's list contains only shared objects that don't
581      appear in GDB's list --- those that are newly loaded.  Add them
582      to GDB's shared object list.  */
583   if (inferior)
584     {
585       struct so_list *i;
586
587       /* Add the new shared objects to GDB's list.  */
588       *gdb_link = inferior;
589
590       /* Fill in the rest of each of the `struct so_list' nodes.  */
591       for (i = inferior; i; i = i->next)
592         {
593           i->from_tty = from_tty;
594
595           /* Fill in the rest of the `struct so_list' node.  */
596           catch_errors (solib_map_sections, i,
597                         "Error while mapping shared library sections:\n",
598                         RETURN_MASK_ALL);
599
600           /* If requested, add the shared object's sections to the TARGET's
601              section table.  Do this immediately after mapping the object so
602              that later nodes in the list can query this object, as is needed
603              in solib-osf.c.  */
604           if (target)
605             {
606               int count = (i->sections_end - i->sections);
607               if (count > 0)
608                 {
609                   int space = target_resize_to_sections (target, count);
610                   memcpy (target->to_sections + space,
611                           i->sections,
612                           count * sizeof (i->sections[0]));
613                 }
614             }
615
616           /* Notify any observer that the shared object has been
617              loaded now that we've added it to GDB's tables.  */
618           observer_notify_solib_loaded (i);
619         }
620     }
621 }
622
623 /* Return non-zero if SO is the libpthread shared library.
624
625    Uses a fairly simplistic heuristic approach where we check
626    the file name against "/libpthread".  This can lead to false
627    positives, but this should be good enough in practice.  */
628
629 static int
630 libpthread_solib_p (struct so_list *so)
631 {
632   return (strstr (so->so_name, "/libpthread") != NULL);
633 }
634
635 /* GLOBAL FUNCTION
636
637    solib_add -- read in symbol info for newly added shared libraries
638
639    SYNOPSIS
640
641    void solib_add (char *pattern, int from_tty, struct target_ops
642    *TARGET, int readsyms)
643
644    DESCRIPTION
645
646    Read in symbolic information for any shared objects whose names
647    match PATTERN.  (If we've already read a shared object's symbol
648    info, leave it alone.)  If PATTERN is zero, read them all.
649
650    If READSYMS is 0, defer reading symbolic information until later
651    but still do any needed low level processing.
652
653    FROM_TTY and TARGET are as described for update_solib_list, above.  */
654
655 void
656 solib_add (char *pattern, int from_tty, struct target_ops *target, int readsyms)
657 {
658   struct so_list *gdb;
659
660   if (pattern)
661     {
662       char *re_err = re_comp (pattern);
663
664       if (re_err)
665         error (_("Invalid regexp: %s"), re_err);
666     }
667
668   update_solib_list (from_tty, target);
669
670   /* Walk the list of currently loaded shared libraries, and read
671      symbols for any that match the pattern --- or any whose symbols
672      aren't already loaded, if no pattern was given.  */
673   {
674     int any_matches = 0;
675     int loaded_any_symbols = 0;
676
677     for (gdb = so_list_head; gdb; gdb = gdb->next)
678       if (! pattern || re_exec (gdb->so_name))
679         {
680           /* Normally, we would read the symbols from that library
681              only if READSYMS is set.  However, we're making a small
682              exception for the pthread library, because we sometimes
683              need the library symbols to be loaded in order to provide
684              thread support (x86-linux for instance).  */
685           const int add_this_solib =
686             (readsyms || libpthread_solib_p (gdb));
687
688           any_matches = 1;
689           if (add_this_solib && solib_read_symbols (gdb, from_tty))
690             loaded_any_symbols = 1;
691         }
692
693     if (from_tty && pattern && ! any_matches)
694       printf_unfiltered
695         ("No loaded shared libraries match the pattern `%s'.\n", pattern);
696
697     if (loaded_any_symbols)
698       {
699         struct target_so_ops *ops = solib_ops (current_gdbarch);
700
701         /* Getting new symbols may change our opinion about what is
702            frameless.  */
703         reinit_frame_cache ();
704
705         ops->special_symbol_handling ();
706       }
707   }
708 }
709
710
711 /*
712
713    LOCAL FUNCTION
714
715    info_sharedlibrary_command -- code for "info sharedlibrary"
716
717    SYNOPSIS
718
719    static void info_sharedlibrary_command ()
720
721    DESCRIPTION
722
723    Walk through the shared library list and print information
724    about each attached library.
725  */
726
727 static void
728 info_sharedlibrary_command (char *ignore, int from_tty)
729 {
730   struct so_list *so = NULL;    /* link map state variable */
731   int header_done = 0;
732   int addr_width;
733
734   /* "0x", a little whitespace, and two hex digits per byte of pointers.  */
735   addr_width = 4 + (gdbarch_ptr_bit (current_gdbarch) / 4);
736
737   update_solib_list (from_tty, 0);
738
739   for (so = so_list_head; so; so = so->next)
740     {
741       if (so->so_name[0])
742         {
743           if (!header_done)
744             {
745               printf_unfiltered ("%-*s%-*s%-12s%s\n", addr_width, "From",
746                                  addr_width, "To", "Syms Read",
747                                  "Shared Object Library");
748               header_done++;
749             }
750
751           printf_unfiltered ("%-*s", addr_width,
752                              so->addr_high != 0
753                                ? hex_string_custom (
754                                    (LONGEST) so->addr_low,
755                                    addr_width - 4)
756                                : "");
757           printf_unfiltered ("%-*s", addr_width,
758                              so->addr_high != 0
759                                ? hex_string_custom (
760                                    (LONGEST) so->addr_high,
761                                    addr_width - 4)
762                                : "");
763           printf_unfiltered ("%-12s", so->symbols_loaded ? "Yes" : "No");
764           printf_unfiltered ("%s\n", so->so_name);
765         }
766     }
767   if (so_list_head == NULL)
768     {
769       printf_unfiltered (_("No shared libraries loaded at this time.\n"));
770     }
771 }
772
773 /*
774
775    GLOBAL FUNCTION
776
777    solib_address -- check to see if an address is in a shared lib
778
779    SYNOPSIS
780
781    char * solib_address (CORE_ADDR address)
782
783    DESCRIPTION
784
785    Provides a hook for other gdb routines to discover whether or
786    not a particular address is within the mapped address space of
787    a shared library.
788
789    For example, this routine is called at one point to disable
790    breakpoints which are in shared libraries that are not currently
791    mapped in.
792  */
793
794 char *
795 solib_address (CORE_ADDR address)
796 {
797   struct so_list *so = 0;       /* link map state variable */
798
799   for (so = so_list_head; so; so = so->next)
800     {
801       struct section_table *p;
802
803       for (p = so->sections; p < so->sections_end; p++)
804         {
805           if (p->addr <= address && address < p->endaddr)
806             return (so->so_name);
807         }
808     }
809
810   return (0);
811 }
812
813 /* Called by free_all_symtabs */
814
815 void
816 clear_solib (void)
817 {
818   struct target_so_ops *ops = solib_ops (current_gdbarch);
819
820   /* This function is expected to handle ELF shared libraries.  It is
821      also used on Solaris, which can run either ELF or a.out binaries
822      (for compatibility with SunOS 4), both of which can use shared
823      libraries.  So we don't know whether we have an ELF executable or
824      an a.out executable until the user chooses an executable file.
825
826      ELF shared libraries don't get mapped into the address space
827      until after the program starts, so we'd better not try to insert
828      breakpoints in them immediately.  We have to wait until the
829      dynamic linker has loaded them; we'll hit a bp_shlib_event
830      breakpoint (look for calls to create_solib_event_breakpoint) when
831      it's ready.
832
833      SunOS shared libraries seem to be different --- they're present
834      as soon as the process begins execution, so there's no need to
835      put off inserting breakpoints.  There's also nowhere to put a
836      bp_shlib_event breakpoint, so if we put it off, we'll never get
837      around to it.
838
839      So: disable breakpoints only if we're using ELF shared libs.  */
840   if (exec_bfd != NULL
841       && bfd_get_flavour (exec_bfd) != bfd_target_aout_flavour)
842     disable_breakpoints_in_shlibs ();
843
844   while (so_list_head)
845     {
846       struct so_list *so = so_list_head;
847       so_list_head = so->next;
848       if (so->abfd)
849         remove_target_sections (so->abfd);
850       free_so (so);
851     }
852
853   ops->clear_solib ();
854 }
855
856 /* GLOBAL FUNCTION
857
858    solib_create_inferior_hook -- shared library startup support
859
860    SYNOPSIS
861
862    void solib_create_inferior_hook ()
863
864    DESCRIPTION
865
866    When gdb starts up the inferior, it nurses it along (through the
867    shell) until it is ready to execute it's first instruction.  At this
868    point, this function gets called via expansion of the macro
869    SOLIB_CREATE_INFERIOR_HOOK.  */
870
871 void
872 solib_create_inferior_hook (void)
873 {
874   struct target_so_ops *ops = solib_ops (current_gdbarch);
875   ops->solib_create_inferior_hook();
876 }
877
878 /* GLOBAL FUNCTION
879
880    in_solib_dynsym_resolve_code -- check to see if an address is in
881                                    dynamic loader's dynamic symbol
882                                    resolution code
883
884    SYNOPSIS
885
886    int in_solib_dynsym_resolve_code (CORE_ADDR pc)
887
888    DESCRIPTION
889
890    Determine if PC is in the dynamic linker's symbol resolution
891    code.  Return 1 if so, 0 otherwise.
892 */
893
894 int
895 in_solib_dynsym_resolve_code (CORE_ADDR pc)
896 {
897   struct target_so_ops *ops = solib_ops (current_gdbarch);
898   return ops->in_dynsym_resolve_code (pc);
899 }
900
901 /*
902
903    LOCAL FUNCTION
904
905    sharedlibrary_command -- handle command to explicitly add library
906
907    SYNOPSIS
908
909    static void sharedlibrary_command (char *args, int from_tty)
910
911    DESCRIPTION
912
913  */
914
915 static void
916 sharedlibrary_command (char *args, int from_tty)
917 {
918   dont_repeat ();
919   solib_add (args, from_tty, (struct target_ops *) 0, 1);
920 }
921
922 /* LOCAL FUNCTION
923
924    no_shared_libraries -- handle command to explicitly discard symbols
925    from shared libraries.
926
927    DESCRIPTION
928
929    Implements the command "nosharedlibrary", which discards symbols
930    that have been auto-loaded from shared libraries.  Symbols from
931    shared libraries that were added by explicit request of the user
932    are not discarded.  Also called from remote.c.  */
933
934 void
935 no_shared_libraries (char *ignored, int from_tty)
936 {
937   objfile_purge_solibs ();
938   clear_solib ();
939 }
940
941 static void
942 reload_shared_libraries (char *ignored, int from_tty,
943                          struct cmd_list_element *e)
944 {
945   no_shared_libraries (NULL, from_tty);
946   solib_add (NULL, from_tty, NULL, auto_solib_add);
947 }
948
949 static void
950 show_auto_solib_add (struct ui_file *file, int from_tty,
951                      struct cmd_list_element *c, const char *value)
952 {
953   fprintf_filtered (file, _("Autoloading of shared library symbols is %s.\n"),
954                     value);
955 }
956
957
958 /* Handler for library-specific lookup of global symbol NAME in OBJFILE.  Call
959    the library-specific handler if it is installed for the current target.  */
960
961 struct symbol *
962 solib_global_lookup (const struct objfile *objfile,
963                      const char *name,
964                      const char *linkage_name,
965                      const domain_enum domain,
966                      struct symtab **symtab)
967 {
968   struct target_so_ops *ops = solib_ops (current_gdbarch);
969
970   if (ops->lookup_lib_global_symbol != NULL)
971     return ops->lookup_lib_global_symbol (objfile, name, linkage_name,
972                                           domain, symtab);
973   return NULL;
974 }
975
976
977 extern initialize_file_ftype _initialize_solib; /* -Wmissing-prototypes */
978
979 void
980 _initialize_solib (void)
981 {
982   struct cmd_list_element *c;
983
984   solib_data = gdbarch_data_register_pre_init (solib_init);
985
986   add_com ("sharedlibrary", class_files, sharedlibrary_command,
987            _("Load shared object library symbols for files matching REGEXP."));
988   add_info ("sharedlibrary", info_sharedlibrary_command,
989             _("Status of loaded shared object libraries."));
990   add_com ("nosharedlibrary", class_files, no_shared_libraries,
991            _("Unload all shared object library symbols."));
992
993   add_setshow_boolean_cmd ("auto-solib-add", class_support,
994                            &auto_solib_add, _("\
995 Set autoloading of shared library symbols."), _("\
996 Show autoloading of shared library symbols."), _("\
997 If \"on\", symbols from all shared object libraries will be loaded\n\
998 automatically when the inferior begins execution, when the dynamic linker\n\
999 informs gdb that a new library has been loaded, or when attaching to the\n\
1000 inferior.  Otherwise, symbols must be loaded manually, using `sharedlibrary'."),
1001                            NULL,
1002                            show_auto_solib_add,
1003                            &setlist, &showlist);
1004
1005   add_setshow_filename_cmd ("sysroot", class_support,
1006                             &gdb_sysroot, _("\
1007 Set an alternate system root."), _("\
1008 Show the current system root."), _("\
1009 The system root is used to load absolute shared library symbol files.\n\
1010 For other (relative) files, you can add directories using\n\
1011 `set solib-search-path'."),
1012                             reload_shared_libraries,
1013                             NULL,
1014                             &setlist, &showlist);
1015
1016   add_alias_cmd ("solib-absolute-prefix", "sysroot", class_support, 0,
1017                  &setlist);
1018   add_alias_cmd ("solib-absolute-prefix", "sysroot", class_support, 0,
1019                  &showlist);
1020
1021   add_setshow_optional_filename_cmd ("solib-search-path", class_support,
1022                                      &solib_search_path, _("\
1023 Set the search path for loading non-absolute shared library symbol files."), _("\
1024 Show the search path for loading non-absolute shared library symbol files."), _("\
1025 This takes precedence over the environment variables PATH and LD_LIBRARY_PATH."),
1026                                      reload_shared_libraries,
1027                                      show_solib_search_path,
1028                                      &setlist, &showlist);
1029 }