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