Import gdb-7.0
[dragonfly.git] / contrib / gdb-6 / 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 static int solib_cleanup_queued = 0;    /* make_run_cleanup called */
90
91 /* Local function prototypes */
92
93 static void do_clear_solib (void *);
94
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)
102 {
103   fprintf_filtered (file, _("\
104 The search path for loading non-absolute shared library symbol files is %s.\n"),
105                     value);
106 }
107
108 /*
109
110    GLOBAL FUNCTION
111
112    solib_open -- Find a shared library file and open it.
113
114    SYNOPSIS
115
116    int solib_open (char *in_patname, char **found_pathname);
117
118    DESCRIPTION
119
120    Global variable GDB_SYSROOT is used as a prefix directory
121    to search for shared libraries if they have an absolute path.
122
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.
126
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.
137    *   
138    * The last check avoids doing this search when targetting remote
139    * machines since gdb_sysroot will almost always be set.
140
141    RETURNS
142
143    file handle for opened solib, or -1 for failure.  */
144
145 int
146 solib_open (char *in_pathname, char **found_pathname)
147 {
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;
153
154   gdb_sysroot_is_empty = (gdb_sysroot == NULL || *gdb_sysroot == 0);
155
156   if (! IS_ABSOLUTE_PATH (in_pathname) || gdb_sysroot_is_empty)
157     temp_pathname = in_pathname;
158   else
159     {
160       int prefix_len = strlen (gdb_sysroot);
161
162       /* Remove trailing slashes from absolute prefix.  */
163       while (prefix_len > 0
164              && IS_DIR_SEPARATOR (gdb_sysroot[prefix_len - 1]))
165         prefix_len--;
166
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);
172     }
173
174   /* Now see if we can open it.  */
175   found_file = open (temp_pathname, O_RDONLY | O_BINARY, 0);
176
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.  */
182
183     if (found_file < 0)
184       temp_pathname = NULL;
185     else
186       temp_pathname = xstrdup (temp_pathname);
187
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))
193     {
194       /* First, get rid of any drive letters etc.  */
195       while (!IS_DIR_SEPARATOR (*in_pathname))
196         in_pathname++;
197
198       /* Next, get rid of all leading dir separators.  */
199       while (IS_DIR_SEPARATOR (*in_pathname))
200         in_pathname++;
201     }
202   
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);
207   
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);
215
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);
220
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);
226
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);
233
234   /* Done.  If not found, tough luck.  Return found_file and 
235      (optionally) found_pathname.  */
236   if (temp_pathname)
237     {
238       if (found_pathname != NULL)
239         *found_pathname = temp_pathname;
240       else
241         xfree (temp_pathname);
242     }
243   return found_file;
244 }
245
246
247 /*
248
249    LOCAL FUNCTION
250
251    solib_map_sections -- open bfd and build sections for shared lib
252
253    SYNOPSIS
254
255    static int solib_map_sections (struct so_list *so)
256
257    DESCRIPTION
258
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.
264
265    FIXMES
266
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?).
272  */
273
274 static int
275 solib_map_sections (void *arg)
276 {
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;
284
285   filename = tilde_expand (so->so_name);
286
287   old_chain = make_cleanup (xfree, filename);
288   scratch_chan = solib_open (filename, &scratch_pathname);
289
290   if (scratch_chan < 0)
291     {
292       perror_with_name (filename);
293     }
294
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)
298     {
299       close (scratch_chan);
300       error (_("Could not open `%s' as an executable file: %s"),
301              scratch_pathname, bfd_errmsg (bfd_get_error ()));
302     }
303
304   /* Leave bfd open, core_xfer_memory and "info files" need it.  */
305   so->abfd = abfd;
306   bfd_set_cacheable (abfd, 1);
307
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);
313
314   if (!bfd_check_format (abfd, bfd_object))
315     {
316       error (_("\"%s\": not in executable format: %s."),
317              scratch_pathname, bfd_errmsg (bfd_get_error ()));
318     }
319   if (build_section_table (abfd, &so->sections, &so->sections_end))
320     {
321       error (_("Can't find the file sections in `%s': %s"),
322              bfd_get_filename (abfd), bfd_errmsg (bfd_get_error ()));
323     }
324
325   for (p = so->sections; p < so->sections_end; p++)
326     {
327       struct target_so_ops *ops = solib_ops (current_gdbarch);
328
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);
333
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)
339         {
340           so->addr_low = p->addr;
341           so->addr_high = p->endaddr;
342         }
343     }
344
345   /* Free the file names, close the file now.  */
346   do_cleanups (old_chain);
347
348   return (1);
349 }
350
351 /* LOCAL FUNCTION
352
353    free_so --- free a `struct so_list' object
354
355    SYNOPSIS
356
357    void free_so (struct so_list *so)
358
359    DESCRIPTION
360
361    Free the storage associated with the `struct so_list' object SO.
362    If we have opened a BFD for SO, close it.  
363
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.
367
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.  */
371
372 void
373 free_so (struct so_list *so)
374 {
375   struct target_so_ops *ops = solib_ops (current_gdbarch);
376   char *bfd_filename = 0;
377
378   if (so->sections)
379     xfree (so->sections);
380       
381   if (so->abfd)
382     {
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 ()));
387     }
388
389   if (bfd_filename)
390     xfree (bfd_filename);
391
392   ops->free_so (so);
393
394   xfree (so);
395 }
396
397
398 /* Return address of first so_list entry in master shared object list.  */
399 struct so_list *
400 master_so_list (void)
401 {
402   return so_list_head;
403 }
404
405
406 /* A small stub to get us past the arg-passing pinhole of catch_errors.  */
407
408 static int
409 symbol_add_stub (void *arg)
410 {
411   struct so_list *so = (struct so_list *) arg;  /* catch_errs bogon */
412   struct section_addr_info *sap;
413
414   /* Have we already loaded this shared object?  */
415   ALL_OBJFILES (so->objfile)
416     {
417       if (strcmp (so->objfile->name, so->so_name) == 0)
418         return 1;
419     }
420
421   sap = build_section_addr_info_from_section_table (so->sections,
422                                                     so->sections_end);
423
424   so->objfile = symbol_file_add (so->so_name, so->from_tty,
425                                  sap, 0, OBJF_SHARED);
426   free_section_addr_info (sap);
427
428   return (1);
429 }
430
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.  */
434
435 int
436 solib_read_symbols (struct so_list *so, int from_tty)
437 {
438   if (so->symbols_loaded)
439     {
440       if (from_tty)
441         printf_unfiltered (_("Symbols already loaded for %s\n"), so->so_name);
442     }
443   else if (so->abfd == NULL)
444     {
445       if (from_tty)
446         printf_unfiltered (_("Symbol file not found for %s\n"), so->so_name);
447     }
448   else
449     {
450       if (catch_errors (symbol_add_stub, so,
451                         "Error while reading shared library symbols:\n",
452                         RETURN_MASK_ALL))
453         {
454           if (from_tty)
455             printf_unfiltered (_("Loaded symbols for %s\n"), so->so_name);
456           so->symbols_loaded = 1;
457           return 1;
458         }
459     }
460
461   return 0;
462 }
463
464 /* LOCAL FUNCTION
465
466    update_solib_list --- synchronize GDB's shared object list with inferior's
467
468    SYNOPSIS
469
470    void update_solib_list (int from_tty, struct target_ops *TARGET)
471
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.
476
477    If we notice that the inferior has unloaded some shared objects,
478    free any symbolic info GDB had read about those shared objects.
479
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.
482
483    If FROM_TTY is non-null, feel free to print messages about what
484    we're doing.
485
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.  */
492
493 static void
494 update_solib_list (int from_tty, struct target_ops *target)
495 {
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;
499
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);
508
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)
513     {
514       make_run_cleanup (do_clear_solib, NULL);
515       solib_cleanup_queued = 1;
516     }
517
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:
522
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.
526
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.
530
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.
534
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.  */
541
542   gdb = so_list_head;
543   gdb_link = &so_list_head;
544   while (gdb)
545     {
546       struct so_list *i = inferior;
547       struct so_list **i_link = &inferior;
548
549       /* Check to see whether the shared object *gdb also appears in
550          the inferior's current list.  */
551       while (i)
552         {
553           if (! strcmp (gdb->so_original_name, i->so_original_name))
554             break;
555
556           i_link = &i->next;
557           i = *i_link;
558         }
559
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)
564         {
565           *i_link = i->next;
566           free_so (i);
567           gdb_link = &gdb->next;
568           gdb = *gdb_link;
569         }
570
571       /* If it's not on the inferior's list, remove it from GDB's tables.  */
572       else
573         {
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);
577
578           *gdb_link = gdb->next;
579
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);
583
584           /* Some targets' section tables might be referring to
585              sections from so->abfd; remove them.  */
586           remove_target_sections (gdb->abfd);
587
588           free_so (gdb);
589           gdb = *gdb_link;
590         }
591     }
592
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)
597     {
598       struct so_list *i;
599
600       /* Add the new shared objects to GDB's list.  */
601       *gdb_link = inferior;
602
603       /* Fill in the rest of each of the `struct so_list' nodes.  */
604       for (i = inferior; i; i = i->next)
605         {
606           i->from_tty = from_tty;
607
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);
612
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)
618             {
619               int count = (i->sections_end - i->sections);
620               if (count > 0)
621                 {
622                   int space = target_resize_to_sections (target, count);
623                   memcpy (target->to_sections + space,
624                           i->sections,
625                           count * sizeof (i->sections[0]));
626                 }
627             }
628
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);
632         }
633     }
634 }
635
636 /* Return non-zero if SO is the libpthread shared library.
637
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.  */
641
642 static int
643 libpthread_solib_p (struct so_list *so)
644 {
645   return (strstr (so->so_name, "/libpthread") != NULL);
646 }
647
648 /* GLOBAL FUNCTION
649
650    solib_add -- read in symbol info for newly added shared libraries
651
652    SYNOPSIS
653
654    void solib_add (char *pattern, int from_tty, struct target_ops
655    *TARGET, int readsyms)
656
657    DESCRIPTION
658
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.
662
663    If READSYMS is 0, defer reading symbolic information until later
664    but still do any needed low level processing.
665
666    FROM_TTY and TARGET are as described for update_solib_list, above.  */
667
668 void
669 solib_add (char *pattern, int from_tty, struct target_ops *target, int readsyms)
670 {
671   struct so_list *gdb;
672
673   if (pattern)
674     {
675       char *re_err = re_comp (pattern);
676
677       if (re_err)
678         error (_("Invalid regexp: %s"), re_err);
679     }
680
681   update_solib_list (from_tty, target);
682
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.  */
686   {
687     int any_matches = 0;
688     int loaded_any_symbols = 0;
689
690     for (gdb = so_list_head; gdb; gdb = gdb->next)
691       if (! pattern || re_exec (gdb->so_name))
692         {
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));
700
701           any_matches = 1;
702           if (add_this_solib && solib_read_symbols (gdb, from_tty))
703             loaded_any_symbols = 1;
704         }
705
706     if (from_tty && pattern && ! any_matches)
707       printf_unfiltered
708         ("No loaded shared libraries match the pattern `%s'.\n", pattern);
709
710     if (loaded_any_symbols)
711       {
712         struct target_so_ops *ops = solib_ops (current_gdbarch);
713
714         /* Getting new symbols may change our opinion about what is
715            frameless.  */
716         reinit_frame_cache ();
717
718         ops->special_symbol_handling ();
719       }
720   }
721 }
722
723
724 /*
725
726    LOCAL FUNCTION
727
728    info_sharedlibrary_command -- code for "info sharedlibrary"
729
730    SYNOPSIS
731
732    static void info_sharedlibrary_command ()
733
734    DESCRIPTION
735
736    Walk through the shared library list and print information
737    about each attached library.
738  */
739
740 static void
741 info_sharedlibrary_command (char *ignore, int from_tty)
742 {
743   struct so_list *so = NULL;    /* link map state variable */
744   int header_done = 0;
745   int addr_width;
746
747   /* "0x", a little whitespace, and two hex digits per byte of pointers.  */
748   addr_width = 4 + (gdbarch_ptr_bit (current_gdbarch) / 4);
749
750   update_solib_list (from_tty, 0);
751
752   for (so = so_list_head; so; so = so->next)
753     {
754       if (so->so_name[0])
755         {
756           if (!header_done)
757             {
758               printf_unfiltered ("%-*s%-*s%-12s%s\n", addr_width, "From",
759                                  addr_width, "To", "Syms Read",
760                                  "Shared Object Library");
761               header_done++;
762             }
763
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);
778         }
779     }
780   if (so_list_head == NULL)
781     {
782       printf_unfiltered (_("No shared libraries loaded at this time.\n"));
783     }
784 }
785
786 /*
787
788    GLOBAL FUNCTION
789
790    solib_address -- check to see if an address is in a shared lib
791
792    SYNOPSIS
793
794    char * solib_address (CORE_ADDR address)
795
796    DESCRIPTION
797
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.
801
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.
805  */
806
807 char *
808 solib_address (CORE_ADDR address)
809 {
810   struct so_list *so = 0;       /* link map state variable */
811
812   for (so = so_list_head; so; so = so->next)
813     {
814       struct section_table *p;
815
816       for (p = so->sections; p < so->sections_end; p++)
817         {
818           if (p->addr <= address && address < p->endaddr)
819             return (so->so_name);
820         }
821     }
822
823   return (0);
824 }
825
826 /* Called by free_all_symtabs */
827
828 void
829 clear_solib (void)
830 {
831   struct target_so_ops *ops = solib_ops (current_gdbarch);
832
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.
838
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.
845
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.
851
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 ();
856
857   while (so_list_head)
858     {
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);
864     }
865
866   ops->clear_solib ();
867 }
868
869 static void
870 do_clear_solib (void *dummy)
871 {
872   solib_cleanup_queued = 0;
873   clear_solib ();
874 }
875
876 /* GLOBAL FUNCTION
877
878    solib_create_inferior_hook -- shared library startup support
879
880    SYNOPSIS
881
882    void solib_create_inferior_hook ()
883
884    DESCRIPTION
885
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.  */
890
891 void
892 solib_create_inferior_hook (void)
893 {
894   struct target_so_ops *ops = solib_ops (current_gdbarch);
895   ops->solib_create_inferior_hook();
896 }
897
898 /* GLOBAL FUNCTION
899
900    in_solib_dynsym_resolve_code -- check to see if an address is in
901                                    dynamic loader's dynamic symbol
902                                    resolution code
903
904    SYNOPSIS
905
906    int in_solib_dynsym_resolve_code (CORE_ADDR pc)
907
908    DESCRIPTION
909
910    Determine if PC is in the dynamic linker's symbol resolution
911    code.  Return 1 if so, 0 otherwise.
912 */
913
914 int
915 in_solib_dynsym_resolve_code (CORE_ADDR pc)
916 {
917   struct target_so_ops *ops = solib_ops (current_gdbarch);
918   return ops->in_dynsym_resolve_code (pc);
919 }
920
921 /*
922
923    LOCAL FUNCTION
924
925    sharedlibrary_command -- handle command to explicitly add library
926
927    SYNOPSIS
928
929    static void sharedlibrary_command (char *args, int from_tty)
930
931    DESCRIPTION
932
933  */
934
935 static void
936 sharedlibrary_command (char *args, int from_tty)
937 {
938   dont_repeat ();
939   solib_add (args, from_tty, (struct target_ops *) 0, 1);
940 }
941
942 /* LOCAL FUNCTION
943
944    no_shared_libraries -- handle command to explicitly discard symbols
945    from shared libraries.
946
947    DESCRIPTION
948
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.  */
953
954 void
955 no_shared_libraries (char *ignored, int from_tty)
956 {
957   objfile_purge_solibs ();
958   do_clear_solib (NULL);
959 }
960
961 static void
962 reload_shared_libraries (char *ignored, int from_tty,
963                          struct cmd_list_element *e)
964 {
965   no_shared_libraries (NULL, from_tty);
966   solib_add (NULL, from_tty, NULL, auto_solib_add);
967 }
968
969 static void
970 show_auto_solib_add (struct ui_file *file, int from_tty,
971                      struct cmd_list_element *c, const char *value)
972 {
973   fprintf_filtered (file, _("Autoloading of shared library symbols is %s.\n"),
974                     value);
975 }
976
977
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.  */
980
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)
987 {
988   if (current_target_so_ops->lookup_lib_global_symbol != NULL)
989     return current_target_so_ops->lookup_lib_global_symbol (objfile,
990                                 name, linkage_name, domain, symtab);
991
992   return NULL;
993 }
994
995
996 extern initialize_file_ftype _initialize_solib; /* -Wmissing-prototypes */
997
998 void
999 _initialize_solib (void)
1000 {
1001   struct cmd_list_element *c;
1002
1003   solib_data = gdbarch_data_register_pre_init (solib_init);
1004
1005   add_com ("sharedlibrary", class_files, sharedlibrary_command,
1006            _("Load shared object library symbols for files matching REGEXP."));
1007   add_info ("sharedlibrary", info_sharedlibrary_command,
1008             _("Status of loaded shared object libraries."));
1009   add_com ("nosharedlibrary", class_files, no_shared_libraries,
1010            _("Unload all shared object library symbols."));
1011
1012   add_setshow_boolean_cmd ("auto-solib-add", class_support,
1013                            &auto_solib_add, _("\
1014 Set autoloading of shared library symbols."), _("\
1015 Show autoloading of shared library symbols."), _("\
1016 If \"on\", symbols from all shared object libraries will be loaded\n\
1017 automatically when the inferior begins execution, when the dynamic linker\n\
1018 informs gdb that a new library has been loaded, or when attaching to the\n\
1019 inferior.  Otherwise, symbols must be loaded manually, using `sharedlibrary'."),
1020                            NULL,
1021                            show_auto_solib_add,
1022                            &setlist, &showlist);
1023
1024   add_setshow_filename_cmd ("sysroot", class_support,
1025                             &gdb_sysroot, _("\
1026 Set an alternate system root."), _("\
1027 Show the current system root."), _("\
1028 The system root is used to load absolute shared library symbol files.\n\
1029 For other (relative) files, you can add directories using\n\
1030 `set solib-search-path'."),
1031                             reload_shared_libraries,
1032                             NULL,
1033                             &setlist, &showlist);
1034
1035   add_alias_cmd ("solib-absolute-prefix", "sysroot", class_support, 0,
1036                  &setlist);
1037   add_alias_cmd ("solib-absolute-prefix", "sysroot", class_support, 0,
1038                  &showlist);
1039
1040   add_setshow_optional_filename_cmd ("solib-search-path", class_support,
1041                                      &solib_search_path, _("\
1042 Set the search path for loading non-absolute shared library symbol files."), _("\
1043 Show the search path for loading non-absolute shared library symbol files."), _("\
1044 This takes precedence over the environment variables PATH and LD_LIBRARY_PATH."),
1045                                      reload_shared_libraries,
1046                                      show_solib_search_path,
1047                                      &setlist, &showlist);
1048 }