Speed up GDB startup time by not demangling partial symbols.
[external/binutils.git] / gdb / irix5-nat.c
1 /* Native support for the SGI Iris running IRIX version 5, for GDB.
2    Copyright 1988, 1989, 1990, 1991, 1992, 1993, 1994
3    Free Software Foundation, Inc.
4    Contributed by Alessandro Forin(af@cs.cmu.edu) at CMU
5    and by Per Bothner(bothner@cs.wisc.edu) at U.Wisconsin.
6    Implemented for Irix 4.x by Garrett A. Wollman.
7    Modified for Irix 5.x by Ian Lance Taylor.
8
9 This file is part of GDB.
10
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 2 of the License, or
14 (at your option) any later version.
15
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 GNU General Public License for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
24
25 #include "defs.h"
26 #include "inferior.h"
27 #include "gdbcore.h"
28 #include "target.h"
29
30 #include <sys/time.h>
31 #include <sys/procfs.h>
32 #include <setjmp.h>             /* For JB_XXX.  */
33
34 /* Size of elements in jmpbuf */
35
36 #define JB_ELEMENT_SIZE 4
37
38 /*
39  * See the comment in m68k-tdep.c regarding the utility of these functions.
40  *
41  * These definitions are from the MIPS SVR4 ABI, so they may work for
42  * any MIPS SVR4 target.
43  */
44
45 void 
46 supply_gregset (gregsetp)
47      gregset_t *gregsetp;
48 {
49   register int regi;
50   register greg_t *regp = &(*gregsetp)[0];
51
52   for(regi = 0; regi <= CTX_RA; regi++)
53     supply_register (regi, (char *)(regp + regi));
54
55   supply_register (PC_REGNUM, (char *)(regp + CTX_EPC));
56   supply_register (HI_REGNUM, (char *)(regp + CTX_MDHI));
57   supply_register (LO_REGNUM, (char *)(regp + CTX_MDLO));
58   supply_register (CAUSE_REGNUM, (char *)(regp + CTX_CAUSE));
59 }
60
61 void
62 fill_gregset (gregsetp, regno)
63      gregset_t *gregsetp;
64      int regno;
65 {
66   int regi;
67   register greg_t *regp = &(*gregsetp)[0];
68
69   for (regi = 0; regi <= CTX_RA; regi++)
70     if ((regno == -1) || (regno == regi))
71       *(regp + regi) = *(greg_t *) &registers[REGISTER_BYTE (regi)];
72
73   if ((regno == -1) || (regno == PC_REGNUM))
74     *(regp + CTX_EPC) = *(greg_t *) &registers[REGISTER_BYTE (PC_REGNUM)];
75
76   if ((regno == -1) || (regno == CAUSE_REGNUM))
77     *(regp + CTX_CAUSE) = *(greg_t *) &registers[REGISTER_BYTE (PS_REGNUM)];
78
79   if ((regno == -1) || (regno == HI_REGNUM))
80     *(regp + CTX_MDHI) = *(greg_t *) &registers[REGISTER_BYTE (HI_REGNUM)];
81
82   if ((regno == -1) || (regno == LO_REGNUM))
83     *(regp + CTX_MDLO) = *(greg_t *) &registers[REGISTER_BYTE (LO_REGNUM)];
84 }
85
86 /*
87  * Now we do the same thing for floating-point registers.
88  * We don't bother to condition on FP0_REGNUM since any
89  * reasonable MIPS configuration has an R3010 in it.
90  *
91  * Again, see the comments in m68k-tdep.c.
92  */
93
94 void
95 supply_fpregset (fpregsetp)
96      fpregset_t *fpregsetp;
97 {
98   register int regi;
99
100   for (regi = 0; regi < 32; regi++)
101     supply_register (FP0_REGNUM + regi,
102                      (char *)&fpregsetp->fp_r.fp_regs[regi]);
103
104   supply_register (FCRCS_REGNUM, (char *)&fpregsetp->fp_csr);
105
106   /* FIXME: how can we supply FCRIR_REGNUM?  SGI doesn't tell us. */
107 }
108
109 void
110 fill_fpregset (fpregsetp, regno)
111      fpregset_t *fpregsetp;
112      int regno;
113 {
114   int regi;
115   char *from, *to;
116
117   for (regi = FP0_REGNUM; regi < FP0_REGNUM + 32; regi++)
118     {
119       if ((regno == -1) || (regno == regi))
120         {
121           from = (char *) &registers[REGISTER_BYTE (regi)];
122           to = (char *) &(fpregsetp->fp_r.fp_regs[regi - FP0_REGNUM]);
123           memcpy(to, from, REGISTER_RAW_SIZE (regi));
124         }
125     }
126
127   if ((regno == -1) || (regno == FCRCS_REGNUM))
128     fpregsetp->fp_csr = *(unsigned *) &registers[REGISTER_BYTE(FCRCS_REGNUM)];
129 }
130
131
132 /* Figure out where the longjmp will land.
133    We expect the first arg to be a pointer to the jmp_buf structure from which
134    we extract the pc (JB_PC) that we will land at.  The pc is copied into PC.
135    This routine returns true on success. */
136
137 int
138 get_longjmp_target (pc)
139      CORE_ADDR *pc;
140 {
141   char buf[TARGET_PTR_BIT / TARGET_CHAR_BIT];
142   CORE_ADDR jb_addr;
143
144   jb_addr = read_register (A0_REGNUM);
145
146   if (target_read_memory (jb_addr + JB_PC * JB_ELEMENT_SIZE, buf,
147                           TARGET_PTR_BIT / TARGET_CHAR_BIT))
148     return 0;
149
150   *pc = extract_address (buf, TARGET_PTR_BIT / TARGET_CHAR_BIT);
151
152   return 1;
153 }
154
155 void
156 fetch_core_registers (core_reg_sect, core_reg_size, which, reg_addr)
157      char *core_reg_sect;
158      unsigned core_reg_size;
159      int which;                 /* Unused */
160      unsigned int reg_addr;     /* Unused */
161 {
162   if (core_reg_size != REGISTER_BYTES)
163     {
164       warning ("wrong size gregset struct in core file");
165       return;
166     }
167
168   memcpy ((char *)registers, core_reg_sect, core_reg_size);
169 }
170 \f
171 /* Irix 5 uses what appears to be a unique form of shared library
172    support.  This is a copy of solib.c modified for Irix 5.  */
173
174 #include <sys/types.h>
175 #include <signal.h>
176 #include <string.h>
177 #include <sys/param.h>
178 #include <fcntl.h>
179
180 /* <obj.h> includes <sym.h> and <symconst.h>, which causes conflicts
181    with our versions of those files included by tm-mips.h.  Prevent
182    <obj.h> from including them with some appropriate defines.  */
183 #define __SYM_H__
184 #define __SYMCONST_H__
185 #include <obj.h>
186
187 #include "symtab.h"
188 #include "bfd.h"
189 #include "symfile.h"
190 #include "objfiles.h"
191 #include "command.h"
192 #include "frame.h"
193 #include "regex.h"
194 #include "inferior.h"
195 #include "language.h"
196
197 /* The symbol which starts off the list of shared libraries.  */
198 #define DEBUG_BASE "__rld_obj_head"
199
200 /* How to get the loaded address of a shared library.  */
201 #define LM_ADDR(so) ((so)->lm.o_praw)
202
203 char shadow_contents[BREAKPOINT_MAX];   /* Stash old bkpt addr contents */
204
205 struct so_list {
206   struct so_list *next;                 /* next structure in linked list */
207   struct obj_list ll;
208   struct obj lm;                        /* copy of link map from inferior */
209   struct obj_list *lladdr;              /* addr in inferior lm was read from */
210   CORE_ADDR lmend;                      /* upper addr bound of mapped object */
211   char symbols_loaded;                  /* flag: symbols read in yet? */
212   char from_tty;                        /* flag: print msgs? */
213   struct objfile *objfile;              /* objfile for loaded lib */
214   struct section_table *sections;
215   struct section_table *sections_end;
216   struct section_table *textsection;
217   bfd *abfd;
218 };
219
220 static struct so_list *so_list_head;    /* List of known shared objects */
221 static CORE_ADDR debug_base;            /* Base of dynamic linker structures */
222 static CORE_ADDR breakpoint_addr;       /* Address where end bkpt is set */
223
224 /* Local function prototypes */
225
226 static void
227 sharedlibrary_command PARAMS ((char *, int));
228
229 static int
230 enable_break PARAMS ((void));
231
232 static int
233 disable_break PARAMS ((void));
234
235 static void
236 info_sharedlibrary_command PARAMS ((char *, int));
237
238 static int
239 symbol_add_stub PARAMS ((char *));
240
241 static struct so_list *
242 find_solib PARAMS ((struct so_list *));
243
244 static struct obj_list *
245 first_link_map_member PARAMS ((void));
246
247 static CORE_ADDR
248 locate_base PARAMS ((void));
249
250 static void
251 solib_map_sections PARAMS ((struct so_list *));
252
253 /*
254
255 LOCAL FUNCTION
256
257         solib_map_sections -- open bfd and build sections for shared lib
258
259 SYNOPSIS
260
261         static void solib_map_sections (struct so_list *so)
262
263 DESCRIPTION
264
265         Given a pointer to one of the shared objects in our list
266         of mapped objects, use the recorded name to open a bfd
267         descriptor for the object, build a section table, and then
268         relocate all the section addresses by the base address at
269         which the shared object was mapped.
270
271 FIXMES
272
273         In most (all?) cases the shared object file name recorded in the
274         dynamic linkage tables will be a fully qualified pathname.  For
275         cases where it isn't, do we really mimic the systems search
276         mechanism correctly in the below code (particularly the tilde
277         expansion stuff?).
278  */
279
280 static void
281 solib_map_sections (so)
282      struct so_list *so;
283 {
284   char *filename;
285   char *scratch_pathname;
286   int scratch_chan;
287   struct section_table *p;
288   struct cleanup *old_chain;
289   bfd *abfd;
290   CORE_ADDR offset;
291   
292   filename = tilde_expand (so -> lm.o_path);
293   old_chain = make_cleanup (free, filename);
294   
295   scratch_chan = openp (getenv ("PATH"), 1, filename, O_RDONLY, 0,
296                         &scratch_pathname);
297   if (scratch_chan < 0)
298     {
299       scratch_chan = openp (getenv ("LD_LIBRARY_PATH"), 1, filename,
300                             O_RDONLY, 0, &scratch_pathname);
301     }
302   if (scratch_chan < 0)
303     {
304       perror_with_name (filename);
305     }
306   /* Leave scratch_pathname allocated.  abfd->name will point to it.  */
307
308   abfd = bfd_fdopenr (scratch_pathname, gnutarget, scratch_chan);
309   if (!abfd)
310     {
311       close (scratch_chan);
312       error ("Could not open `%s' as an executable file: %s",
313              scratch_pathname, bfd_errmsg (bfd_get_error ()));
314     }
315   /* Leave bfd open, core_xfer_memory and "info files" need it.  */
316   so -> abfd = abfd;
317   abfd -> cacheable = true;
318
319   if (!bfd_check_format (abfd, bfd_object))
320     {
321       error ("\"%s\": not in executable format: %s.",
322              scratch_pathname, bfd_errmsg (bfd_get_error ()));
323     }
324   if (build_section_table (abfd, &so -> sections, &so -> sections_end))
325     {
326       error ("Can't find the file sections in `%s': %s", 
327              bfd_get_filename (exec_bfd), bfd_errmsg (bfd_get_error ()));
328     }
329
330   /* Irix 5 shared objects are pre-linked to particular addresses
331      although the dynamic linker may have to relocate them if the
332      address ranges of the libraries used by the main program clash.
333      The offset is the difference between the address where the object
334      is mapped and the binding address of the shared library.  */
335   offset = (CORE_ADDR) LM_ADDR (so) - so -> lm.o_base_address;
336
337   for (p = so -> sections; p < so -> sections_end; p++)
338     {
339       /* Relocate the section binding addresses as recorded in the shared
340          object's file by the offset to get the address to which the
341          object was actually mapped.  */
342       p -> addr += offset;
343       p -> endaddr += offset;
344       so -> lmend = (CORE_ADDR) max (p -> endaddr, so -> lmend);
345       if (STREQ (p -> the_bfd_section -> name, ".text"))
346         {
347           so -> textsection = p;
348         }
349     }
350
351   /* Free the file names, close the file now.  */
352   do_cleanups (old_chain);
353 }
354
355 /*
356
357 LOCAL FUNCTION
358
359         locate_base -- locate the base address of dynamic linker structs
360
361 SYNOPSIS
362
363         CORE_ADDR locate_base (void)
364
365 DESCRIPTION
366
367         For both the SunOS and SVR4 shared library implementations, if the
368         inferior executable has been linked dynamically, there is a single
369         address somewhere in the inferior's data space which is the key to
370         locating all of the dynamic linker's runtime structures.  This
371         address is the value of the symbol defined by the macro DEBUG_BASE.
372         The job of this function is to find and return that address, or to
373         return 0 if there is no such address (the executable is statically
374         linked for example).
375
376         For SunOS, the job is almost trivial, since the dynamic linker and
377         all of it's structures are statically linked to the executable at
378         link time.  Thus the symbol for the address we are looking for has
379         already been added to the minimal symbol table for the executable's
380         objfile at the time the symbol file's symbols were read, and all we
381         have to do is look it up there.  Note that we explicitly do NOT want
382         to find the copies in the shared library.
383
384         The SVR4 version is much more complicated because the dynamic linker
385         and it's structures are located in the shared C library, which gets
386         run as the executable's "interpreter" by the kernel.  We have to go
387         to a lot more work to discover the address of DEBUG_BASE.  Because
388         of this complexity, we cache the value we find and return that value
389         on subsequent invocations.  Note there is no copy in the executable
390         symbol tables.
391
392         Irix 5 is basically like SunOS.
393
394         Note that we can assume nothing about the process state at the time
395         we need to find this address.  We may be stopped on the first instruc-
396         tion of the interpreter (C shared library), the first instruction of
397         the executable itself, or somewhere else entirely (if we attached
398         to the process for example).
399
400  */
401
402 static CORE_ADDR
403 locate_base ()
404 {
405   struct minimal_symbol *msymbol;
406   CORE_ADDR address = 0;
407
408   msymbol = lookup_minimal_symbol (DEBUG_BASE, symfile_objfile);
409   if ((msymbol != NULL) && (SYMBOL_VALUE_ADDRESS (msymbol) != 0))
410     {
411       address = SYMBOL_VALUE_ADDRESS (msymbol);
412     }
413   return (address);
414 }
415
416 /*
417
418 LOCAL FUNCTION
419
420         first_link_map_member -- locate first member in dynamic linker's map
421
422 SYNOPSIS
423
424         static struct link_map *first_link_map_member (void)
425
426 DESCRIPTION
427
428         Read in a copy of the first member in the inferior's dynamic
429         link map from the inferior's dynamic linker structures, and return
430         a pointer to the copy in our address space.
431 */
432
433 static struct obj_list *
434 first_link_map_member ()
435 {
436   struct obj_list *lm;
437   struct obj_list s;
438
439   read_memory (debug_base, (char *) &lm, sizeof (struct obj_list *));
440
441   if (lm == NULL)
442     return NULL;
443
444   /* The first entry in the list is the object file we are debugging,
445      so skip it.  */
446   read_memory ((CORE_ADDR) lm, (char *) &s, sizeof (struct obj_list));
447
448   return s.next;
449 }
450
451 /*
452
453 LOCAL FUNCTION
454
455         find_solib -- step through list of shared objects
456
457 SYNOPSIS
458
459         struct so_list *find_solib (struct so_list *so_list_ptr)
460
461 DESCRIPTION
462
463         This module contains the routine which finds the names of any
464         loaded "images" in the current process. The argument in must be
465         NULL on the first call, and then the returned value must be passed
466         in on subsequent calls. This provides the capability to "step" down
467         the list of loaded objects. On the last object, a NULL value is
468         returned.
469  */
470
471 static struct so_list *
472 find_solib (so_list_ptr)
473      struct so_list *so_list_ptr;       /* Last lm or NULL for first one */
474 {
475   struct so_list *so_list_next = NULL;
476   struct obj_list *lm = NULL;
477   struct so_list *new;
478   
479   if (so_list_ptr == NULL)
480     {
481       /* We are setting up for a new scan through the loaded images. */
482       if ((so_list_next = so_list_head) == NULL)
483         {
484           /* We have not already read in the dynamic linking structures
485              from the inferior, lookup the address of the base structure. */
486           debug_base = locate_base ();
487           if (debug_base != 0)
488             {
489               /* Read the base structure in and find the address of the first
490                  link map list member. */
491               lm = first_link_map_member ();
492             }
493         }
494     }
495   else
496     {
497       /* We have been called before, and are in the process of walking
498          the shared library list.  Advance to the next shared object. */
499       if ((lm = so_list_ptr->ll.next) == NULL)
500         {
501           /* We have hit the end of the list, so check to see if any were
502              added, but be quiet if we can't read from the target any more. */
503           int status = target_read_memory ((CORE_ADDR) so_list_ptr -> lladdr,
504                                            (char *) &(so_list_ptr -> ll),
505                                            sizeof (struct obj_list));
506           if (status == 0)
507             {
508               lm = so_list_ptr->ll.next;
509             }
510           else
511             {
512               lm = NULL;
513             }
514         }
515       so_list_next = so_list_ptr -> next;
516     }
517   if ((so_list_next == NULL) && (lm != NULL))
518     {
519       int errcode;
520       char *buffer;
521
522       /* Get next link map structure from inferior image and build a local
523          abbreviated load_map structure */
524       new = (struct so_list *) xmalloc (sizeof (struct so_list));
525       memset ((char *) new, 0, sizeof (struct so_list));
526       new -> lladdr = lm;
527       /* Add the new node as the next node in the list, or as the root
528          node if this is the first one. */
529       if (so_list_ptr != NULL)
530         {
531           so_list_ptr -> next = new;
532         }
533       else
534         {
535           so_list_head = new;
536         }      
537       so_list_next = new;
538       read_memory ((CORE_ADDR) lm, (char *) &(new -> ll),
539                    sizeof (struct obj_list));
540       read_memory ((CORE_ADDR) new->ll.data, (char *) &(new -> lm),
541                    sizeof (struct obj));
542       target_read_string ((CORE_ADDR)new->lm.o_path, &buffer,
543                           INT_MAX, &errcode);
544       if (errcode != 0)
545         memory_error (errcode, (CORE_ADDR)new->lm.o_path);
546       new->lm.o_path = buffer;
547       solib_map_sections (new);
548     }
549   return (so_list_next);
550 }
551
552 /* A small stub to get us past the arg-passing pinhole of catch_errors.  */
553
554 static int
555 symbol_add_stub (arg)
556      char *arg;
557 {
558   register struct so_list *so = (struct so_list *) arg; /* catch_errs bogon */
559   
560   so -> objfile = symbol_file_add (so -> lm.o_path, so -> from_tty,
561                                    (unsigned int) so -> textsection -> addr,
562                                    0, 0, 0);
563   return (1);
564 }
565
566 /*
567
568 GLOBAL FUNCTION
569
570         solib_add -- add a shared library file to the symtab and section list
571
572 SYNOPSIS
573
574         void solib_add (char *arg_string, int from_tty,
575                         struct target_ops *target)
576
577 DESCRIPTION
578
579 */
580
581 void
582 solib_add (arg_string, from_tty, target)
583      char *arg_string;
584      int from_tty;
585      struct target_ops *target;
586 {       
587   register struct so_list *so = NULL;           /* link map state variable */
588
589   /* Last shared library that we read.  */
590   struct so_list *so_last = NULL;
591
592   char *re_err;
593   int count;
594   int old;
595   
596   if ((re_err = re_comp (arg_string ? arg_string : ".")) != NULL)
597     {
598       error ("Invalid regexp: %s", re_err);
599     }
600   
601   /* Add the shared library sections to the section table of the
602      specified target, if any.  */
603   if (target)
604     {
605       /* Count how many new section_table entries there are.  */
606       so = NULL;
607       count = 0;
608       while ((so = find_solib (so)) != NULL)
609         {
610           if (so -> lm.o_path[0])
611             {
612               count += so -> sections_end - so -> sections;
613             }
614         }
615       
616       if (count)
617         {
618           /* Reallocate the target's section table including the new size.  */
619           if (target -> to_sections)
620             {
621               old = target -> to_sections_end - target -> to_sections;
622               target -> to_sections = (struct section_table *)
623                 xrealloc ((char *)target -> to_sections,
624                          (sizeof (struct section_table)) * (count + old));
625             }
626           else
627             {
628               old = 0;
629               target -> to_sections = (struct section_table *)
630                 xmalloc ((sizeof (struct section_table)) * count);
631             }
632           target -> to_sections_end = target -> to_sections + (count + old);
633           
634           /* Add these section table entries to the target's table.  */
635           while ((so = find_solib (so)) != NULL)
636             {
637               if (so -> lm.o_path[0])
638                 {
639                   count = so -> sections_end - so -> sections;
640                   memcpy ((char *) (target -> to_sections + old),
641                           so -> sections, 
642                           (sizeof (struct section_table)) * count);
643                   old += count;
644                 }
645             }
646         }
647     }
648   
649   /* Now add the symbol files.  */
650   while ((so = find_solib (so)) != NULL)
651     {
652       if (so -> lm.o_path[0] && re_exec (so -> lm.o_path))
653         {
654           so -> from_tty = from_tty;
655           if (so -> symbols_loaded)
656             {
657               if (from_tty)
658                 {
659                   printf_unfiltered ("Symbols already loaded for %s\n", so -> lm.o_path);
660                 }
661             }
662           else if (catch_errors
663                    (symbol_add_stub, (char *) so,
664                     "Error while reading shared library symbols:\n",
665                     RETURN_MASK_ALL))
666             {
667               so_last = so;
668               so -> symbols_loaded = 1;
669             }
670         }
671     }
672
673   /* Getting new symbols may change our opinion about what is
674      frameless.  */
675   if (so_last)
676     reinit_frame_cache ();
677 }
678
679 /*
680
681 LOCAL FUNCTION
682
683         info_sharedlibrary_command -- code for "info sharedlibrary"
684
685 SYNOPSIS
686
687         static void info_sharedlibrary_command ()
688
689 DESCRIPTION
690
691         Walk through the shared library list and print information
692         about each attached library.
693 */
694
695 static void
696 info_sharedlibrary_command (ignore, from_tty)
697      char *ignore;
698      int from_tty;
699 {
700   register struct so_list *so = NULL;   /* link map state variable */
701   int header_done = 0;
702   
703   if (exec_bfd == NULL)
704     {
705       printf_unfiltered ("No exec file.\n");
706       return;
707     }
708   while ((so = find_solib (so)) != NULL)
709     {
710       if (so -> lm.o_path[0])
711         {
712           if (!header_done)
713             {
714               printf_unfiltered("%-12s%-12s%-12s%s\n", "From", "To", "Syms Read",
715                      "Shared Object Library");
716               header_done++;
717             }
718           printf_unfiltered ("%-12s",
719                   local_hex_string_custom ((unsigned long) LM_ADDR (so),
720                                            "08l"));
721           printf_unfiltered ("%-12s",
722                   local_hex_string_custom ((unsigned long) so -> lmend,
723                                            "08l"));
724           printf_unfiltered ("%-12s", so -> symbols_loaded ? "Yes" : "No");
725           printf_unfiltered ("%s\n",  so -> lm.o_path);
726         }
727     }
728   if (so_list_head == NULL)
729     {
730       printf_unfiltered ("No shared libraries loaded at this time.\n"); 
731     }
732 }
733
734 /*
735
736 GLOBAL FUNCTION
737
738         solib_address -- check to see if an address is in a shared lib
739
740 SYNOPSIS
741
742         int solib_address (CORE_ADDR address)
743
744 DESCRIPTION
745
746         Provides a hook for other gdb routines to discover whether or
747         not a particular address is within the mapped address space of
748         a shared library.  Any address between the base mapping address
749         and the first address beyond the end of the last mapping, is
750         considered to be within the shared library address space, for
751         our purposes.
752
753         For example, this routine is called at one point to disable
754         breakpoints which are in shared libraries that are not currently
755         mapped in.
756  */
757
758 int
759 solib_address (address)
760      CORE_ADDR address;
761 {
762   register struct so_list *so = 0;      /* link map state variable */
763   
764   while ((so = find_solib (so)) != NULL)
765     {
766       if (so -> lm.o_path[0])
767         {
768           if ((address >= (CORE_ADDR) LM_ADDR (so)) &&
769               (address < (CORE_ADDR) so -> lmend))
770             {
771               return (1);
772             }
773         }
774     }
775   return (0);
776 }
777
778 /* Called by free_all_symtabs */
779
780 void 
781 clear_solib()
782 {
783   struct so_list *next;
784   char *bfd_filename;
785   
786   while (so_list_head)
787     {
788       if (so_list_head -> sections)
789         {
790           free ((PTR)so_list_head -> sections);
791         }
792       if (so_list_head -> abfd)
793         {
794           bfd_filename = bfd_get_filename (so_list_head -> abfd);
795           bfd_close (so_list_head -> abfd);
796         }
797       else
798         /* This happens for the executable on SVR4.  */
799         bfd_filename = NULL;
800
801       next = so_list_head -> next;
802       if (bfd_filename)
803         free ((PTR)bfd_filename);
804       free (so_list_head->lm.o_path);
805       free ((PTR)so_list_head);
806       so_list_head = next;
807     }
808   debug_base = 0;
809 }
810
811 /*
812
813 LOCAL FUNCTION
814
815         disable_break -- remove the "mapping changed" breakpoint
816
817 SYNOPSIS
818
819         static int disable_break ()
820
821 DESCRIPTION
822
823         Removes the breakpoint that gets hit when the dynamic linker
824         completes a mapping change.
825
826 */
827
828 static int
829 disable_break ()
830 {
831   int status = 1;
832
833
834   /* Note that breakpoint address and original contents are in our address
835      space, so we just need to write the original contents back. */
836
837   if (memory_remove_breakpoint (breakpoint_addr, shadow_contents) != 0)
838     {
839       status = 0;
840     }
841
842   /* For the SVR4 version, we always know the breakpoint address.  For the
843      SunOS version we don't know it until the above code is executed.
844      Grumble if we are stopped anywhere besides the breakpoint address. */
845
846   if (stop_pc != breakpoint_addr)
847     {
848       warning ("stopped at unknown breakpoint while handling shared libraries");
849     }
850
851   return (status);
852 }
853
854 /*
855
856 LOCAL FUNCTION
857
858         enable_break -- arrange for dynamic linker to hit breakpoint
859
860 SYNOPSIS
861
862         int enable_break (void)
863
864 DESCRIPTION
865
866         This functions inserts a breakpoint at the entry point of the
867         main executable, where all shared libraries are mapped in.
868 */
869
870 static int
871 enable_break ()
872 {
873   if (symfile_objfile != NULL
874       && target_insert_breakpoint (symfile_objfile->ei.entry_point,
875                                    shadow_contents) == 0)
876     {
877       breakpoint_addr = symfile_objfile->ei.entry_point;
878       return 1;
879     }
880
881   return 0;
882 }
883   
884 /*
885   
886 GLOBAL FUNCTION
887   
888         solib_create_inferior_hook -- shared library startup support
889   
890 SYNOPSIS
891   
892         void solib_create_inferior_hook()
893   
894 DESCRIPTION
895   
896         When gdb starts up the inferior, it nurses it along (through the
897         shell) until it is ready to execute it's first instruction.  At this
898         point, this function gets called via expansion of the macro
899         SOLIB_CREATE_INFERIOR_HOOK.
900
901         For SunOS executables, this first instruction is typically the
902         one at "_start", or a similar text label, regardless of whether
903         the executable is statically or dynamically linked.  The runtime
904         startup code takes care of dynamically linking in any shared
905         libraries, once gdb allows the inferior to continue.
906
907         For SVR4 executables, this first instruction is either the first
908         instruction in the dynamic linker (for dynamically linked
909         executables) or the instruction at "start" for statically linked
910         executables.  For dynamically linked executables, the system
911         first exec's /lib/libc.so.N, which contains the dynamic linker,
912         and starts it running.  The dynamic linker maps in any needed
913         shared libraries, maps in the actual user executable, and then
914         jumps to "start" in the user executable.
915
916         For both SunOS shared libraries, and SVR4 shared libraries, we
917         can arrange to cooperate with the dynamic linker to discover the
918         names of shared libraries that are dynamically linked, and the
919         base addresses to which they are linked.
920
921         This function is responsible for discovering those names and
922         addresses, and saving sufficient information about them to allow
923         their symbols to be read at a later time.
924
925 FIXME
926
927         Between enable_break() and disable_break(), this code does not
928         properly handle hitting breakpoints which the user might have
929         set in the startup code or in the dynamic linker itself.  Proper
930         handling will probably have to wait until the implementation is
931         changed to use the "breakpoint handler function" method.
932
933         Also, what if child has exit()ed?  Must exit loop somehow.
934   */
935
936 void 
937 solib_create_inferior_hook()
938 {
939   if (!enable_break ())
940     {
941       warning ("shared library handler failed to enable breakpoint");
942       return;
943     }
944
945   /* Now run the target.  It will eventually hit the breakpoint, at
946      which point all of the libraries will have been mapped in and we
947      can go groveling around in the dynamic linker structures to find
948      out what we need to know about them. */
949
950   clear_proceed_status ();
951   stop_soon_quietly = 1;
952   stop_signal = 0;
953   do
954     {
955       target_resume (-1, 0, stop_signal);
956       wait_for_inferior ();
957     }
958   while (stop_signal != SIGTRAP);
959   
960   /* We are now either at the "mapping complete" breakpoint (or somewhere
961      else, a condition we aren't prepared to deal with anyway), so adjust
962      the PC as necessary after a breakpoint, disable the breakpoint, and
963      add any shared libraries that were mapped in. */
964
965   if (DECR_PC_AFTER_BREAK)
966     {
967       stop_pc -= DECR_PC_AFTER_BREAK;
968       write_register (PC_REGNUM, stop_pc);
969     }
970
971   if (!disable_break ())
972     {
973       warning ("shared library handler failed to disable breakpoint");
974     }
975
976   /*  solib_add will call reinit_frame_cache.
977       But we are stopped in the startup code and we might not have symbols
978       for the startup code, so heuristic_proc_start could be called
979       and will put out an annoying warning.
980       Delaying the resetting of stop_soon_quietly until after symbol loading
981       suppresses the warning.  */
982   solib_add ((char *) 0, 0, (struct target_ops *) 0);
983   stop_soon_quietly = 0;
984 }
985
986 /*
987
988 LOCAL FUNCTION
989
990         sharedlibrary_command -- handle command to explicitly add library
991
992 SYNOPSIS
993
994         static void sharedlibrary_command (char *args, int from_tty)
995
996 DESCRIPTION
997
998 */
999
1000 static void
1001 sharedlibrary_command (args, from_tty)
1002 char *args;
1003 int from_tty;
1004 {
1005   dont_repeat ();
1006   solib_add (args, from_tty, (struct target_ops *) 0);
1007 }
1008
1009 void
1010 _initialize_solib()
1011 {
1012   
1013   add_com ("sharedlibrary", class_files, sharedlibrary_command,
1014            "Load shared object library symbols for files matching REGEXP.");
1015   add_info ("sharedlibrary", info_sharedlibrary_command, 
1016             "Status of loaded shared object libraries.");
1017 }