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