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