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