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