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