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