1 /* IBM RS/6000 native-dependent code for GDB, the GNU debugger.
2 Copyright 1986, 1987, 1989, 1991, 1992, 1994 Free Software Foundation, Inc.
4 This file is part of GDB.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
24 #include "xcoffsolib.h"
27 #include "libbfd.h" /* For bfd_cache_lookup (FIXME) */
29 #include "gdb-stabs.h"
31 #include <sys/ptrace.h>
34 #include <sys/param.h>
38 #include <sys/ioctl.h>
49 extern struct vmap
* map_vmap
PARAMS ((bfd
*bf
, bfd
*arch
));
51 extern struct target_ops exec_ops
;
54 exec_one_dummy_insn
PARAMS ((void));
57 add_text_to_loadinfo
PARAMS ((CORE_ADDR textaddr
, CORE_ADDR dataaddr
));
60 fixup_breakpoints
PARAMS ((CORE_ADDR low
, CORE_ADDR high
, CORE_ADDR delta
));
62 /* Conversion from gdb-to-system special purpose register numbers.. */
64 static int special_regs
[] = {
75 fetch_inferior_registers (regno
)
79 extern char registers
[];
81 if (regno
< 0) { /* for all registers */
83 /* read 32 general purpose registers. */
85 for (ii
=0; ii
< 32; ++ii
)
86 *(int*)®isters
[REGISTER_BYTE (ii
)] =
87 ptrace (PT_READ_GPR
, inferior_pid
, (PTRACE_ARG3_TYPE
) ii
, 0, 0);
89 /* read general purpose floating point registers. */
91 for (ii
=0; ii
< 32; ++ii
)
92 ptrace (PT_READ_FPR
, inferior_pid
,
93 (PTRACE_ARG3_TYPE
) ®isters
[REGISTER_BYTE (FP0_REGNUM
+ii
)],
96 /* read special registers. */
97 for (ii
=0; ii
<= LAST_SP_REGNUM
-FIRST_SP_REGNUM
; ++ii
)
98 *(int*)®isters
[REGISTER_BYTE (FIRST_SP_REGNUM
+ii
)] =
99 ptrace (PT_READ_GPR
, inferior_pid
, (PTRACE_ARG3_TYPE
) special_regs
[ii
],
102 registers_fetched ();
106 /* else an individual register is addressed. */
108 else if (regno
< FP0_REGNUM
) { /* a GPR */
109 *(int*)®isters
[REGISTER_BYTE (regno
)] =
110 ptrace (PT_READ_GPR
, inferior_pid
, (PTRACE_ARG3_TYPE
) regno
, 0, 0);
112 else if (regno
<= FPLAST_REGNUM
) { /* a FPR */
113 ptrace (PT_READ_FPR
, inferior_pid
,
114 (PTRACE_ARG3_TYPE
) ®isters
[REGISTER_BYTE (regno
)],
115 (regno
-FP0_REGNUM
+FPR0
), 0);
117 else if (regno
<= LAST_SP_REGNUM
) { /* a special register */
118 *(int*)®isters
[REGISTER_BYTE (regno
)] =
119 ptrace (PT_READ_GPR
, inferior_pid
,
120 (PTRACE_ARG3_TYPE
) special_regs
[regno
-FIRST_SP_REGNUM
], 0, 0);
123 fprintf_unfiltered (gdb_stderr
, "gdb error: register no %d not implemented.\n", regno
);
125 register_valid
[regno
] = 1;
128 /* Store our register values back into the inferior.
129 If REGNO is -1, do this for all registers.
130 Otherwise, REGNO specifies which register (so we can save time). */
133 store_inferior_registers (regno
)
136 extern char registers
[];
141 { /* for all registers.. */
144 /* execute one dummy instruction (which is a breakpoint) in inferior
145 process. So give kernel a chance to do internal house keeping.
146 Otherwise the following ptrace(2) calls will mess up user stack
147 since kernel will get confused about the bottom of the stack (%sp) */
149 exec_one_dummy_insn ();
151 /* write general purpose registers first! */
152 for ( ii
=GPR0
; ii
<=GPR31
; ++ii
)
154 ptrace (PT_WRITE_GPR
, inferior_pid
, (PTRACE_ARG3_TYPE
) ii
,
155 *(int*)®isters
[REGISTER_BYTE (ii
)], 0);
158 perror ("ptrace write_gpr");
163 /* write floating point registers now. */
164 for ( ii
=0; ii
< 32; ++ii
)
166 ptrace (PT_WRITE_FPR
, inferior_pid
,
167 (PTRACE_ARG3_TYPE
) ®isters
[REGISTER_BYTE (FP0_REGNUM
+ii
)],
171 perror ("ptrace write_fpr");
176 /* write special registers. */
177 for (ii
=0; ii
<= LAST_SP_REGNUM
-FIRST_SP_REGNUM
; ++ii
)
179 ptrace (PT_WRITE_GPR
, inferior_pid
,
180 (PTRACE_ARG3_TYPE
) special_regs
[ii
],
181 *(int*)®isters
[REGISTER_BYTE (FIRST_SP_REGNUM
+ii
)], 0);
184 perror ("ptrace write_gpr");
190 /* else, a specific register number is given... */
192 else if (regno
< FP0_REGNUM
) /* a GPR */
194 ptrace (PT_WRITE_GPR
, inferior_pid
, (PTRACE_ARG3_TYPE
) regno
,
195 *(int*)®isters
[REGISTER_BYTE (regno
)], 0);
198 else if (regno
<= FPLAST_REGNUM
) /* a FPR */
200 ptrace (PT_WRITE_FPR
, inferior_pid
,
201 (PTRACE_ARG3_TYPE
) ®isters
[REGISTER_BYTE (regno
)],
202 regno
- FP0_REGNUM
+ FPR0
, 0);
205 else if (regno
<= LAST_SP_REGNUM
) /* a special register */
207 ptrace (PT_WRITE_GPR
, inferior_pid
,
208 (PTRACE_ARG3_TYPE
) special_regs
[regno
-FIRST_SP_REGNUM
],
209 *(int*)®isters
[REGISTER_BYTE (regno
)], 0);
213 fprintf_unfiltered (gdb_stderr
, "Gdb error: register no %d not implemented.\n", regno
);
217 perror ("ptrace write");
222 /* Execute one dummy breakpoint instruction. This way we give the kernel
223 a chance to do some housekeeping and update inferior's internal data,
227 exec_one_dummy_insn ()
229 #define DUMMY_INSN_ADDR (TEXT_SEGMENT_BASE)+0x200
231 char shadow_contents
[BREAKPOINT_MAX
]; /* Stash old bkpt addr contents */
232 unsigned int status
, pid
;
234 /* We plant one dummy breakpoint into DUMMY_INSN_ADDR address. We assume that
235 this address will never be executed again by the real code. */
237 target_insert_breakpoint (DUMMY_INSN_ADDR
, shadow_contents
);
240 ptrace (PT_CONTINUE
, inferior_pid
, (PTRACE_ARG3_TYPE
) DUMMY_INSN_ADDR
, 0, 0);
242 perror ("pt_continue");
245 pid
= wait (&status
);
246 } while (pid
!= inferior_pid
);
248 target_remove_breakpoint (DUMMY_INSN_ADDR
, shadow_contents
);
252 fetch_core_registers (core_reg_sect
, core_reg_size
, which
, reg_addr
)
254 unsigned core_reg_size
;
256 unsigned int reg_addr
; /* Unused in this version */
258 /* fetch GPRs and special registers from the first register section
262 /* copy GPRs first. */
263 memcpy (registers
, core_reg_sect
, 32 * 4);
265 /* gdb's internal register template and bfd's register section layout
266 should share a common include file. FIXMEmgo */
267 /* then comes special registes. They are supposed to be in the same
268 order in gdb template and bfd `.reg' section. */
269 core_reg_sect
+= (32 * 4);
270 memcpy (®isters
[REGISTER_BYTE (FIRST_SP_REGNUM
)], core_reg_sect
,
271 (LAST_SP_REGNUM
- FIRST_SP_REGNUM
+ 1) * 4);
274 /* fetch floating point registers from register section 2 in core bfd. */
276 memcpy (®isters
[REGISTER_BYTE (FP0_REGNUM
)], core_reg_sect
, 32 * 8);
279 fprintf_unfiltered (gdb_stderr
, "Gdb error: unknown parameter to fetch_core_registers().\n");
282 /* handle symbol translation on vmapping */
286 register struct vmap
*vp
;
288 register struct objfile
*objfile
;
289 CORE_ADDR text_delta
;
290 CORE_ADDR data_delta
;
292 struct section_offsets
*new_offsets
;
295 objfile
= vp
->objfile
;
298 /* OK, it's not an objfile we opened ourselves.
299 Currently, that can only happen with the exec file, so
300 relocate the symbols for the symfile. */
301 if (symfile_objfile
== NULL
)
303 objfile
= symfile_objfile
;
307 (sizeof (struct section_offsets
)
308 + sizeof (new_offsets
->offsets
) * objfile
->num_sections
);
310 for (i
= 0; i
< objfile
->num_sections
; ++i
)
311 ANOFFSET (new_offsets
, i
) = ANOFFSET (objfile
->section_offsets
, i
);
314 vp
->tstart
- ANOFFSET (objfile
->section_offsets
, SECT_OFF_TEXT
);
315 ANOFFSET (new_offsets
, SECT_OFF_TEXT
) = vp
->tstart
;
318 vp
->dstart
- ANOFFSET (objfile
->section_offsets
, SECT_OFF_DATA
);
319 ANOFFSET (new_offsets
, SECT_OFF_DATA
) = vp
->dstart
;
322 vp
->dstart
- ANOFFSET (objfile
->section_offsets
, SECT_OFF_BSS
);
323 ANOFFSET (new_offsets
, SECT_OFF_BSS
) = vp
->dstart
;
325 objfile_relocate (objfile
, new_offsets
);
328 /* Add symbols for an objfile. */
331 objfile_symbol_add (arg
)
334 struct objfile
*obj
= (struct objfile
*) arg
;
336 syms_from_objfile (obj
, 0, 0, 0);
337 new_symfile_objfile (obj
, 0, 0);
341 /* Add a new vmap entry based on ldinfo() information.
343 If ldi->ldinfo_fd is not valid (e.g. this struct ld_info is from a
344 core file), the caller should set it to -1, and we will open the file.
346 Return the vmap new entry. */
350 register struct ld_info
*ldi
;
353 register char *mem
, *objname
;
357 /* This ldi structure was allocated using alloca() in
358 xcoff_relocate_symtab(). Now we need to have persistent object
359 and member names, so we should save them. */
361 mem
= ldi
->ldinfo_filename
+ strlen (ldi
->ldinfo_filename
) + 1;
362 mem
= savestring (mem
, strlen (mem
));
363 objname
= savestring (ldi
->ldinfo_filename
, strlen (ldi
->ldinfo_filename
));
365 if (ldi
->ldinfo_fd
< 0)
366 /* Note that this opens it once for every member; a possible
367 enhancement would be to only open it once for every object. */
368 abfd
= bfd_openr (objname
, gnutarget
);
370 abfd
= bfd_fdopenr (objname
, gnutarget
, ldi
->ldinfo_fd
);
372 error ("Could not open `%s' as an executable file: %s",
373 objname
, bfd_errmsg (bfd_get_error ()));
375 /* make sure we have an object file */
377 if (bfd_check_format (abfd
, bfd_object
))
378 vp
= map_vmap (abfd
, 0);
380 else if (bfd_check_format (abfd
, bfd_archive
))
383 /* FIXME??? am I tossing BFDs? bfd? */
384 while ((last
= bfd_openr_next_archived_file (abfd
, last
)))
385 if (STREQ (mem
, last
->filename
))
391 /* FIXME -- should be error */
392 warning ("\"%s\": member \"%s\" missing.", abfd
->filename
, mem
);
396 if (!bfd_check_format(last
, bfd_object
))
398 bfd_close (last
); /* XXX??? */
402 vp
= map_vmap (last
, abfd
);
408 error ("\"%s\": not in executable format: %s.",
409 objname
, bfd_errmsg (bfd_get_error ()));
412 obj
= allocate_objfile (vp
->bfd
, 0);
415 #ifndef SOLIB_SYMBOLS_MANUAL
416 if (catch_errors (objfile_symbol_add
, (char *)obj
,
417 "Error while reading shared library symbols:\n",
420 /* Note this is only done if symbol reading was successful. */
428 /* update VMAP info with ldinfo() information
429 Input is ptr to ldinfo() results. */
433 register struct ld_info
*ldi
;
436 register struct vmap
*vp
;
437 int got_one
, retried
;
440 /* For each *ldi, see if we have a corresponding *vp.
441 If so, update the mapping, and symbol table.
442 If not, add an entry and symbol table. */
445 char *name
= ldi
->ldinfo_filename
;
446 char *memb
= name
+ strlen(name
) + 1;
450 if (fstat (ldi
->ldinfo_fd
, &ii
) < 0)
451 fatal ("cannot fstat(fd=%d) on %s", ldi
->ldinfo_fd
, name
);
453 for (got_one
= 0, vp
= vmap
; vp
; vp
= vp
->nxt
)
455 /* First try to find a `vp', which is the same as in ldinfo.
456 If not the same, just continue and grep the next `vp'. If same,
457 relocate its tstart, tend, dstart, dend values. If no such `vp'
458 found, get out of this for loop, add this ldi entry as a new vmap
459 (add_vmap) and come back, fins its `vp' and so on... */
461 /* The filenames are not always sufficient to match on. */
463 if ((name
[0] == '/' && !STREQ(name
, vp
->name
))
464 || (memb
[0] && !STREQ(memb
, vp
->member
)))
467 /* See if we are referring to the same file. */
468 if (bfd_stat (vp
->bfd
, &vi
) < 0)
469 /* An error here is innocuous, most likely meaning that
470 the file descriptor has become worthless.
471 FIXME: What does it mean for a file descriptor to become
472 "worthless"? What makes it happen? What error does it
473 produce (ENOENT? others?)? Should we at least provide
477 if (ii
.st_dev
!= vi
.st_dev
|| ii
.st_ino
!= vi
.st_ino
)
481 close (ldi
->ldinfo_fd
);
485 /* Found a corresponding VMAP. Remap! */
487 /* We can assume pointer == CORE_ADDR, this code is native only. */
488 vp
->tstart
= (CORE_ADDR
) ldi
->ldinfo_textorg
;
489 vp
->tend
= vp
->tstart
+ ldi
->ldinfo_textsize
;
490 vp
->dstart
= (CORE_ADDR
) ldi
->ldinfo_dataorg
;
491 vp
->dend
= vp
->dstart
+ ldi
->ldinfo_datasize
;
495 vp
->tstart
+= vp
->tadj
;
496 vp
->tend
+= vp
->tadj
;
499 /* The objfile is only NULL for the exec file. */
500 if (vp
->objfile
== NULL
)
503 /* relocate symbol table(s). */
506 /* There may be more, so we don't break out of the loop. */
509 /* if there was no matching *vp, we must perforce create the sucker(s) */
510 if (!got_one
&& !retried
)
516 } while (ldi
->ldinfo_next
517 && (ldi
= (void *) (ldi
->ldinfo_next
+ (char *) ldi
)));
519 /* If we don't find the symfile_objfile anywhere in the ldinfo, it
520 is unlikely that the symbol file is relocated to the proper
521 address. And we might have attached to a process which is
522 running a different copy of the same executable. */
523 if (symfile_objfile
!= NULL
&& !got_exec_file
)
526 fputs_unfiltered ("Symbol file ", gdb_stderr
);
527 fputs_unfiltered (symfile_objfile
->name
, gdb_stderr
);
528 fputs_unfiltered ("\nis not mapped; discarding it.\n\
529 If in fact that file has symbols which the mapped files listed by\n\
530 \"info files\" lack, you can load symbols with the \"symbol-file\" or\n\
531 \"add-symbol-file\" commands (note that you must take care of relocating\n\
532 symbols to the proper address).\n", gdb_stderr
);
533 free_objfile (symfile_objfile
);
534 symfile_objfile
= NULL
;
536 breakpoint_re_set ();
539 /* As well as symbol tables, exec_sections need relocation. After
540 the inferior process' termination, there will be a relocated symbol
541 table exist with no corresponding inferior process. At that time, we
542 need to use `exec' bfd, rather than the inferior process's memory space
545 `exec_sections' need to be relocated only once, as long as the exec
546 file remains unchanged.
555 if (execbfd
== exec_bfd
)
560 if (!vmap
|| !exec_ops
.to_sections
)
561 error ("vmap_exec: vmap or exec_ops.to_sections == 0\n");
563 for (i
=0; &exec_ops
.to_sections
[i
] < exec_ops
.to_sections_end
; i
++)
565 if (STREQ(".text", exec_ops
.to_sections
[i
].the_bfd_section
->name
))
567 exec_ops
.to_sections
[i
].addr
+= vmap
->tstart
;
568 exec_ops
.to_sections
[i
].endaddr
+= vmap
->tstart
;
570 else if (STREQ(".data", exec_ops
.to_sections
[i
].the_bfd_section
->name
))
572 exec_ops
.to_sections
[i
].addr
+= vmap
->dstart
;
573 exec_ops
.to_sections
[i
].endaddr
+= vmap
->dstart
;
578 /* xcoff_relocate_symtab - hook for symbol table relocation.
579 also reads shared libraries.. */
582 xcoff_relocate_symtab (pid
)
585 #define MAX_LOAD_SEGS 64 /* maximum number of load segments */
589 ldi
= (void *) alloca(MAX_LOAD_SEGS
* sizeof (*ldi
));
591 /* According to my humble theory, AIX has some timing problems and
592 when the user stack grows, kernel doesn't update stack info in time
593 and ptrace calls step on user stack. That is why we sleep here a little,
594 and give kernel to update its internals. */
599 ptrace (PT_LDINFO
, pid
, (PTRACE_ARG3_TYPE
) ldi
,
600 MAX_LOAD_SEGS
* sizeof(*ldi
), ldi
);
602 perror_with_name ("ptrace ldinfo");
607 /* We are allowed to assume CORE_ADDR == pointer. This code is
609 add_text_to_loadinfo ((CORE_ADDR
) ldi
->ldinfo_textorg
,
610 (CORE_ADDR
) ldi
->ldinfo_dataorg
);
611 } while (ldi
->ldinfo_next
612 && (ldi
= (void *) (ldi
->ldinfo_next
+ (char *) ldi
)));
615 /* Now that we've jumbled things around, re-sort them. */
616 sort_minimal_symbols ();
619 /* relocate the exec and core sections as well. */
623 /* Core file stuff. */
625 /* Relocate symtabs and read in shared library info, based on symbols
626 from the core file. */
629 xcoff_relocate_core (target
)
630 struct target_ops
*target
;
632 /* Offset of member MEMBER in a struct of type TYPE. */
634 #define offsetof(TYPE, MEMBER) ((int) &((TYPE *)0)->MEMBER)
637 /* Size of a struct ld_info except for the variable-length filename. */
638 #define LDINFO_SIZE (offsetof (struct ld_info, ldinfo_filename))
642 struct ld_info
*ldip
;
645 /* Allocated size of buffer. */
646 int buffer_size
= LDINFO_SIZE
;
647 char *buffer
= xmalloc (buffer_size
);
648 struct cleanup
*old
= make_cleanup (free_current_contents
, &buffer
);
650 /* FIXME, this restriction should not exist. For now, though I'll
651 avoid coredumps with error() pending a real fix. */
654 ("Can't debug a core file without an executable file (on the RS/6000)");
656 ldinfo_sec
= bfd_get_section_by_name (core_bfd
, ".ldinfo");
657 if (ldinfo_sec
== NULL
)
660 fprintf_filtered (gdb_stderr
, "Couldn't get ldinfo from core file: %s\n",
661 bfd_errmsg (bfd_get_error ()));
670 /* Read in everything but the name. */
671 if (bfd_get_section_contents (core_bfd
, ldinfo_sec
, buffer
,
672 offset
, LDINFO_SIZE
) == 0)
679 if (i
== buffer_size
)
682 buffer
= xrealloc (buffer
, buffer_size
);
684 if (bfd_get_section_contents (core_bfd
, ldinfo_sec
, &buffer
[i
],
687 if (buffer
[i
++] == '\0')
689 } while (names_found
< 2);
691 ldip
= (struct ld_info
*) buffer
;
693 /* Can't use a file descriptor from the core file; need to open it. */
694 ldip
->ldinfo_fd
= -1;
696 /* The first ldinfo is for the exec file, allocated elsewhere. */
700 vp
= add_vmap (ldip
);
702 offset
+= ldip
->ldinfo_next
;
704 /* We can assume pointer == CORE_ADDR, this code is native only. */
705 vp
->tstart
= (CORE_ADDR
) ldip
->ldinfo_textorg
;
706 vp
->tend
= vp
->tstart
+ ldip
->ldinfo_textsize
;
707 vp
->dstart
= (CORE_ADDR
) ldip
->ldinfo_dataorg
;
708 vp
->dend
= vp
->dstart
+ ldip
->ldinfo_datasize
;
712 vp
->tstart
+= vp
->tadj
;
713 vp
->tend
+= vp
->tadj
;
716 /* Unless this is the exec file,
717 add our sections to the section table for the core target. */
721 struct section_table
*stp
;
723 count
= target
->to_sections_end
- target
->to_sections
;
725 target
->to_sections
= (struct section_table
*)
726 xrealloc (target
->to_sections
,
727 sizeof (struct section_table
) * count
);
728 target
->to_sections_end
= target
->to_sections
+ count
;
729 stp
= target
->to_sections_end
- 2;
731 /* "Why do we add bfd_section_vma?", I hear you cry.
732 Well, the start of the section in the file is actually
733 that far into the section as the struct vmap understands it.
734 So for text sections, bfd_section_vma tends to be 0x200,
735 and if vp->tstart is 0xd0002000, then the first byte of
736 the text section on disk corresponds to address 0xd0002200. */
738 stp
->the_bfd_section
= bfd_get_section_by_name (stp
->bfd
, ".text");
739 stp
->addr
= bfd_section_vma (stp
->bfd
, stp
->the_bfd_section
) + vp
->tstart
;
740 stp
->endaddr
= bfd_section_vma (stp
->bfd
, stp
->the_bfd_section
) + vp
->tend
;
744 stp
->the_bfd_section
= bfd_get_section_by_name (stp
->bfd
, ".data");
745 stp
->addr
= bfd_section_vma (stp
->bfd
, stp
->the_bfd_section
) + vp
->dstart
;
746 stp
->endaddr
= bfd_section_vma (stp
->bfd
, stp
->the_bfd_section
) + vp
->dend
;
751 add_text_to_loadinfo ((CORE_ADDR
)ldip
->ldinfo_textorg
,
752 (CORE_ADDR
)ldip
->ldinfo_dataorg
);
753 } while (ldip
->ldinfo_next
!= 0);
755 breakpoint_re_set ();