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