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