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