1 /* IBM RS/6000 native-dependent code for GDB, the GNU debugger.
2 Copyright 1986, 1987, 1989, 1991, 1992 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., 675 Mass Ave, Cambridge, MA 02139, USA. */
24 #include "xcoffsolib.h"
27 #include "libbfd.h" /* BFD internals (sigh!) FIXME */
30 #include <sys/ptrace.h>
33 #include <sys/param.h>
37 #include <sys/ioctl.h>
47 extern struct vmap
* map_vmap
PARAMS ((bfd
*bf
, bfd
*arch
));
49 extern struct target_ops exec_ops
;
52 exec_one_dummy_insn
PARAMS ((void));
55 add_text_to_loadinfo
PARAMS ((CORE_ADDR textaddr
, CORE_ADDR dataaddr
));
57 /* Conversion from gdb-to-system special purpose register numbers.. */
59 static int special_regs
[] = {
70 fetch_inferior_registers (regno
)
74 extern char registers
[];
76 if (regno
< 0) { /* for all registers */
78 /* read 32 general purpose registers. */
80 for (ii
=0; ii
< 32; ++ii
)
81 *(int*)®isters
[REGISTER_BYTE (ii
)] =
82 ptrace (PT_READ_GPR
, inferior_pid
, (PTRACE_ARG3_TYPE
) ii
, 0, 0);
84 /* read general purpose floating point registers. */
86 for (ii
=0; ii
< 32; ++ii
)
87 ptrace (PT_READ_FPR
, inferior_pid
,
88 (PTRACE_ARG3_TYPE
) ®isters
[REGISTER_BYTE (FP0_REGNUM
+ii
)],
91 /* read special registers. */
92 for (ii
=0; ii
<= LAST_SP_REGNUM
-FIRST_SP_REGNUM
; ++ii
)
93 *(int*)®isters
[REGISTER_BYTE (FIRST_SP_REGNUM
+ii
)] =
94 ptrace (PT_READ_GPR
, inferior_pid
, (PTRACE_ARG3_TYPE
) special_regs
[ii
],
101 /* else an individual register is addressed. */
103 else if (regno
< FP0_REGNUM
) { /* a GPR */
104 *(int*)®isters
[REGISTER_BYTE (regno
)] =
105 ptrace (PT_READ_GPR
, inferior_pid
, (PTRACE_ARG3_TYPE
) regno
, 0, 0);
107 else if (regno
<= FPLAST_REGNUM
) { /* a FPR */
108 ptrace (PT_READ_FPR
, inferior_pid
,
109 (PTRACE_ARG3_TYPE
) ®isters
[REGISTER_BYTE (regno
)],
110 (regno
-FP0_REGNUM
+FPR0
), 0);
112 else if (regno
<= LAST_SP_REGNUM
) { /* a special register */
113 *(int*)®isters
[REGISTER_BYTE (regno
)] =
114 ptrace (PT_READ_GPR
, inferior_pid
,
115 (PTRACE_ARG3_TYPE
) special_regs
[regno
-FIRST_SP_REGNUM
], 0, 0);
118 fprintf_unfiltered (gdb_stderr
, "gdb error: register no %d not implemented.\n", regno
);
120 register_valid
[regno
] = 1;
123 /* Store our register values back into the inferior.
124 If REGNO is -1, do this for all registers.
125 Otherwise, REGNO specifies which register (so we can save time). */
128 store_inferior_registers (regno
)
131 extern char registers
[];
135 if (regno
== -1) { /* for all registers.. */
138 /* execute one dummy instruction (which is a breakpoint) in inferior
139 process. So give kernel a chance to do internal house keeping.
140 Otherwise the following ptrace(2) calls will mess up user stack
141 since kernel will get confused about the bottom of the stack (%sp) */
143 exec_one_dummy_insn ();
145 /* write general purpose registers first! */
146 for ( ii
=GPR0
; ii
<=GPR31
; ++ii
) {
147 ptrace (PT_WRITE_GPR
, inferior_pid
, (PTRACE_ARG3_TYPE
) ii
,
148 *(int*)®isters
[REGISTER_BYTE (ii
)], 0);
150 perror ("ptrace write_gpr"); errno
= 0;
154 /* write floating point registers now. */
155 for ( ii
=0; ii
< 32; ++ii
) {
156 ptrace (PT_WRITE_FPR
, inferior_pid
,
157 (PTRACE_ARG3_TYPE
) ®isters
[REGISTER_BYTE (FP0_REGNUM
+ii
)],
160 perror ("ptrace write_fpr"); errno
= 0;
164 /* write special registers. */
165 for (ii
=0; ii
<= LAST_SP_REGNUM
-FIRST_SP_REGNUM
; ++ii
) {
166 ptrace (PT_WRITE_GPR
, inferior_pid
,
167 (PTRACE_ARG3_TYPE
) special_regs
[ii
],
168 *(int*)®isters
[REGISTER_BYTE (FIRST_SP_REGNUM
+ii
)], 0);
170 perror ("ptrace write_gpr"); errno
= 0;
175 /* else, a specific register number is given... */
177 else if (regno
< FP0_REGNUM
) { /* a GPR */
179 ptrace (PT_WRITE_GPR
, inferior_pid
, (PTRACE_ARG3_TYPE
) regno
,
180 *(int*)®isters
[REGISTER_BYTE (regno
)], 0);
183 else if (regno
<= FPLAST_REGNUM
) { /* a FPR */
184 ptrace (PT_WRITE_FPR
, inferior_pid
,
185 (PTRACE_ARG3_TYPE
) ®isters
[REGISTER_BYTE (regno
)],
186 regno
-FP0_REGNUM
+FPR0
, 0);
189 else if (regno
<= LAST_SP_REGNUM
) { /* a special register */
191 ptrace (PT_WRITE_GPR
, inferior_pid
,
192 (PTRACE_ARG3_TYPE
) special_regs
[regno
-FIRST_SP_REGNUM
],
193 *(int*)®isters
[REGISTER_BYTE (regno
)], 0);
197 fprintf_unfiltered (gdb_stderr
, "Gdb error: register no %d not implemented.\n", regno
);
200 perror ("ptrace write"); errno
= 0;
204 /* Execute one dummy breakpoint instruction. This way we give the kernel
205 a chance to do some housekeeping and update inferior's internal data,
208 exec_one_dummy_insn ()
210 #define DUMMY_INSN_ADDR (TEXT_SEGMENT_BASE)+0x200
212 unsigned long shadow
;
213 unsigned int status
, pid
;
215 /* We plant one dummy breakpoint into DUMMY_INSN_ADDR address. We assume that
216 this address will never be executed again by the real code. */
218 target_insert_breakpoint (DUMMY_INSN_ADDR
, &shadow
);
221 ptrace (PT_CONTINUE
, inferior_pid
, (PTRACE_ARG3_TYPE
) DUMMY_INSN_ADDR
, 0, 0);
223 perror ("pt_continue");
226 pid
= wait (&status
);
227 } while (pid
!= inferior_pid
);
229 target_remove_breakpoint (DUMMY_INSN_ADDR
, &shadow
);
233 fetch_core_registers (core_reg_sect
, core_reg_size
, which
, reg_addr
)
235 unsigned core_reg_size
;
237 unsigned int reg_addr
; /* Unused in this version */
239 /* fetch GPRs and special registers from the first register section
243 /* copy GPRs first. */
244 memcpy (registers
, core_reg_sect
, 32 * 4);
246 /* gdb's internal register template and bfd's register section layout
247 should share a common include file. FIXMEmgo */
248 /* then comes special registes. They are supposed to be in the same
249 order in gdb template and bfd `.reg' section. */
250 core_reg_sect
+= (32 * 4);
251 memcpy (®isters
[REGISTER_BYTE (FIRST_SP_REGNUM
)], core_reg_sect
,
252 (LAST_SP_REGNUM
- FIRST_SP_REGNUM
+ 1) * 4);
255 /* fetch floating point registers from register section 2 in core bfd. */
257 memcpy (®isters
[REGISTER_BYTE (FP0_REGNUM
)], core_reg_sect
, 32 * 8);
260 fprintf_unfiltered (gdb_stderr
, "Gdb error: unknown parameter to fetch_core_registers().\n");
263 /* vmap_symtab - handle symbol translation on vmapping */
267 register struct vmap
*vp
;
269 register struct objfile
*objfile
;
273 CORE_ADDR text_delta
;
274 CORE_ADDR data_delta
;
276 struct section_offsets
*new_offsets
;
279 objfile
= vp
->objfile
;
282 /* OK, it's not an objfile we opened ourselves.
283 Currently, that can only happen with the exec file, so
284 relocate the symbols for the symfile. */
285 if (symfile_objfile
== NULL
)
287 objfile
= symfile_objfile
;
291 (sizeof (struct section_offsets
)
292 + sizeof (new_offsets
->offsets
) * objfile
->num_sections
);
294 for (i
= 0; i
< objfile
->num_sections
; ++i
)
295 ANOFFSET (new_offsets
, i
) = ANOFFSET (objfile
->section_offsets
, i
);
297 textsec
= bfd_get_section_by_name (vp
->bfd
, ".text");
299 vp
->tstart
- ANOFFSET (objfile
->section_offsets
, textsec
->target_index
);
300 ANOFFSET (new_offsets
, textsec
->target_index
) = vp
->tstart
;
302 datasec
= bfd_get_section_by_name (vp
->bfd
, ".data");
304 vp
->dstart
- ANOFFSET (objfile
->section_offsets
, datasec
->target_index
);
305 ANOFFSET (new_offsets
, datasec
->target_index
) = vp
->dstart
;
307 bsssec
= bfd_get_section_by_name (vp
->bfd
, ".bss");
309 vp
->dstart
- ANOFFSET (objfile
->section_offsets
, bsssec
->target_index
);
310 ANOFFSET (new_offsets
, bsssec
->target_index
) = vp
->dstart
;
312 objfile_relocate (objfile
, new_offsets
);
315 struct obj_section
*s
;
316 for (s
= objfile
->sections
; s
< objfile
->sections_end
; ++s
)
318 if (s
->the_bfd_section
->target_index
== textsec
->target_index
)
320 s
->addr
+= text_delta
;
321 s
->endaddr
+= text_delta
;
323 else if (s
->the_bfd_section
->target_index
== datasec
->target_index
)
325 s
->addr
+= data_delta
;
326 s
->endaddr
+= data_delta
;
328 else if (s
->the_bfd_section
->target_index
== bsssec
->target_index
)
330 s
->addr
+= bss_delta
;
331 s
->endaddr
+= bss_delta
;
337 /* breakpoints need to be relocated as well. */
338 fixup_breakpoints (0, TEXT_SEGMENT_BASE
, text_delta
);
341 /* Add symbols for an objfile. */
343 objfile_symbol_add (arg
)
346 struct objfile
*obj
= (struct objfile
*) arg
;
347 syms_from_objfile (obj
, 0, 0, 0);
348 new_symfile_objfile (obj
, 0, 0);
352 /* Add a new vmap entry based on ldinfo() information.
354 If ldi->ldinfo_fd is not valid (e.g. this struct ld_info is from a
355 core file), the caller should set it to -1, and we will open the file.
357 Return the vmap new entry. */
360 register struct ld_info
*ldi
;
363 register char *mem
, *objname
;
367 /* This ldi structure was allocated using alloca() in
368 xcoff_relocate_symtab(). Now we need to have persistent object
369 and member names, so we should save them. */
371 mem
= ldi
->ldinfo_filename
+ strlen(ldi
->ldinfo_filename
) + 1;
372 mem
= savestring (mem
, strlen (mem
));
373 objname
= savestring (ldi
->ldinfo_filename
, strlen (ldi
->ldinfo_filename
));
375 if (ldi
->ldinfo_fd
< 0)
376 /* Note that this opens it once for every member; a possible
377 enhancement would be to only open it once for every object. */
378 abfd
= bfd_openr (objname
, gnutarget
);
380 abfd
= bfd_fdopenr(objname
, gnutarget
, ldi
->ldinfo_fd
);
382 error("Could not open `%s' as an executable file: %s",
383 objname
, bfd_errmsg(bfd_get_error ()));
386 /* make sure we have an object file */
388 if (bfd_check_format(abfd
, bfd_object
))
389 vp
= map_vmap (abfd
, 0);
391 else if (bfd_check_format(abfd
, bfd_archive
)) {
394 * FIXME??? am I tossing BFDs? bfd?
396 while (last
= bfd_openr_next_archived_file(abfd
, last
))
397 if (STREQ(mem
, last
->filename
))
402 /* FIXME -- should be error */
403 warning("\"%s\": member \"%s\" missing.", abfd
->filename
, mem
);
407 if (!bfd_check_format(last
, bfd_object
)) {
408 bfd_close(last
); /* XXX??? */
412 vp
= map_vmap (last
, abfd
);
417 error ("\"%s\": not in executable format: %s.",
418 objname
, bfd_errmsg(bfd_get_error ()));
421 obj
= allocate_objfile (vp
->bfd
, 0);
424 #ifndef SOLIB_SYMBOLS_MANUAL
425 if (catch_errors (objfile_symbol_add
, (char *)obj
,
426 "Error while reading shared library symbols:\n",
429 /* Note this is only done if symbol reading was successful. */
438 * vmap_ldinfo - update VMAP info with ldinfo() information
441 * ldi - ^ to ldinfo() results.
446 register struct ld_info
*ldi
;
449 register struct vmap
*vp
;
450 register got_one
, retried
;
454 * for each *ldi, see if we have a corresponding *vp
455 * if so, update the mapping, and symbol table.
456 * if not, add an entry and symbol table.
459 char *name
= ldi
->ldinfo_filename
;
460 char *memb
= name
+ strlen(name
) + 1;
464 if (fstat(ldi
->ldinfo_fd
, &ii
) < 0)
465 fatal("cannot fstat(%d) on %s"
469 for (got_one
= 0, vp
= vmap
; vp
; vp
= vp
->nxt
) {
472 /* First try to find a `vp', which is the same as in ldinfo.
473 If not the same, just continue and grep the next `vp'. If same,
474 relocate its tstart, tend, dstart, dend values. If no such `vp'
475 found, get out of this for loop, add this ldi entry as a new vmap
476 (add_vmap) and come back, fins its `vp' and so on... */
478 /* The filenames are not always sufficient to match on. */
480 if ((name
[0] == '/' && !STREQ(name
, vp
->name
))
481 || (memb
[0] && !STREQ(memb
, vp
->member
)))
484 io
= bfd_cache_lookup(vp
->bfd
); /* totally opaque! */
486 fatal("cannot find BFD's iostream for %s", vp
->name
);
488 /* see if we are referring to the same file */
490 if (fstat(fileno(io
), &vi
) < 0)
491 fatal("cannot fstat BFD for %s", vp
->name
);
493 if (ii
.st_dev
!= vi
.st_dev
|| ii
.st_ino
!= vi
.st_ino
)
497 close(ldi
->ldinfo_fd
);
501 /* found a corresponding VMAP. remap! */
504 /* We can assume pointer == CORE_ADDR, this code is native only. */
505 vp
->tstart
= (CORE_ADDR
) ldi
->ldinfo_textorg
;
506 vp
->tend
= vp
->tstart
+ ldi
->ldinfo_textsize
;
507 vp
->dstart
= (CORE_ADDR
) ldi
->ldinfo_dataorg
;
508 vp
->dend
= vp
->dstart
+ ldi
->ldinfo_datasize
;
511 vp
->tstart
+= vp
->tadj
;
512 vp
->tend
+= vp
->tadj
;
515 /* relocate symbol table(s). */
518 /* there may be more, so we don't break out of the loop. */
521 /* if there was no matching *vp, we must perforce create the sucker(s) */
522 if (!got_one
&& !retried
) {
527 } while (ldi
->ldinfo_next
528 && (ldi
= (void *) (ldi
->ldinfo_next
+ (char *) ldi
)));
532 /* As well as symbol tables, exec_sections need relocation. After
533 the inferior process' termination, there will be a relocated symbol
534 table exist with no corresponding inferior process. At that time, we
535 need to use `exec' bfd, rather than the inferior process's memory space
538 `exec_sections' need to be relocated only once, as long as the exec
539 file remains unchanged.
548 if (execbfd
== exec_bfd
)
553 if (!vmap
|| !exec_ops
.to_sections
)
554 error ("vmap_exec: vmap or exec_ops.to_sections == 0\n");
556 for (i
=0; &exec_ops
.to_sections
[i
] < exec_ops
.to_sections_end
; i
++)
558 if (STREQ(".text", exec_ops
.to_sections
[i
].the_bfd_section
->name
))
560 exec_ops
.to_sections
[i
].addr
+= vmap
->tstart
;
561 exec_ops
.to_sections
[i
].endaddr
+= vmap
->tstart
;
563 else if (STREQ(".data", exec_ops
.to_sections
[i
].the_bfd_section
->name
))
565 exec_ops
.to_sections
[i
].addr
+= vmap
->dstart
;
566 exec_ops
.to_sections
[i
].endaddr
+= vmap
->dstart
;
571 /* xcoff_relocate_symtab - hook for symbol table relocation.
572 also reads shared libraries.. */
574 xcoff_relocate_symtab (pid
)
577 #define MAX_LOAD_SEGS 64 /* maximum number of load segments */
582 ldi
= (void *) alloca(MAX_LOAD_SEGS
* sizeof (*ldi
));
584 /* According to my humble theory, AIX has some timing problems and
585 when the user stack grows, kernel doesn't update stack info in time
586 and ptrace calls step on user stack. That is why we sleep here a little,
587 and give kernel to update its internals. */
592 ptrace(PT_LDINFO
, pid
, (PTRACE_ARG3_TYPE
) ldi
,
593 MAX_LOAD_SEGS
* sizeof(*ldi
), ldi
);
595 perror_with_name ("ptrace ldinfo");
602 /* We are allowed to assume CORE_ADDR == pointer. This code is
604 add_text_to_loadinfo ((CORE_ADDR
) ldi
->ldinfo_textorg
,
605 (CORE_ADDR
) ldi
->ldinfo_dataorg
);
606 } while (ldi
->ldinfo_next
607 && (ldi
= (void *) (ldi
->ldinfo_next
+ (char *) ldi
)));
610 /* Now that we've jumbled things around, re-sort them. */
611 sort_minimal_symbols ();
614 /* relocate the exec and core sections as well. */
618 /* Core file stuff. */
620 /* Relocate symtabs and read in shared library info, based on symbols
621 from the core file. */
623 xcoff_relocate_core ()
625 /* Offset of member MEMBER in a struct of type TYPE. */
627 #define offsetof(TYPE, MEMBER) ((int) &((TYPE *)0)->MEMBER)
630 /* Size of a struct ld_info except for the variable-length filename. */
631 #define LDINFO_SIZE (offsetof (struct ld_info, ldinfo_filename))
635 struct ld_info
*ldip
;
638 /* Allocated size of buffer. */
639 int buffer_size
= LDINFO_SIZE
;
640 char *buffer
= xmalloc (buffer_size
);
641 struct cleanup
*old
= make_cleanup (free_current_contents
, &buffer
);
643 /* FIXME, this restriction should not exist. For now, though I'll
644 avoid coredumps with error() pending a real fix. */
647 ("Can't debug a core file without an executable file (on the RS/6000)");
649 ldinfo_sec
= bfd_get_section_by_name (core_bfd
, ".ldinfo");
650 if (ldinfo_sec
== NULL
)
653 fprintf_filtered (gdb_stderr
, "Couldn't get ldinfo from core file: %s\n",
654 bfd_errmsg (bfd_get_error ()));
663 /* Read in everything but the name. */
664 if (bfd_get_section_contents (core_bfd
, ldinfo_sec
, buffer
,
665 offset
, LDINFO_SIZE
) == 0)
672 if (i
== buffer_size
)
675 buffer
= xrealloc (buffer
, buffer_size
);
677 if (bfd_get_section_contents (core_bfd
, ldinfo_sec
, &buffer
[i
],
680 if (buffer
[i
++] == '\0')
682 } while (names_found
< 2);
684 ldip
= (struct ld_info
*)buffer
;
686 /* Can't use a file descriptor from the core file; need to open it. */
687 ldip
->ldinfo_fd
= -1;
689 /* The first ldinfo is for the exec file, allocated elsewhere. */
693 vp
= add_vmap (ldip
);
695 offset
+= ldip
->ldinfo_next
;
697 /* We can assume pointer == CORE_ADDR, this code is native only. */
698 vp
->tstart
= (CORE_ADDR
) ldip
->ldinfo_textorg
;
699 vp
->tend
= vp
->tstart
+ ldip
->ldinfo_textsize
;
700 vp
->dstart
= (CORE_ADDR
) ldip
->ldinfo_dataorg
;
701 vp
->dend
= vp
->dstart
+ ldip
->ldinfo_datasize
;
704 vp
->tstart
+= vp
->tadj
;
705 vp
->tend
+= vp
->tadj
;
708 /* Unless this is the exec file,
709 add our sections to the section table for the core target. */
713 struct section_table
*stp
;
715 count
= core_ops
.to_sections_end
- core_ops
.to_sections
;
717 core_ops
.to_sections
= (struct section_table
*)
718 xrealloc (core_ops
.to_sections
,
719 sizeof (struct section_table
) * count
);
720 core_ops
.to_sections_end
= core_ops
.to_sections
+ count
;
721 stp
= core_ops
.to_sections_end
- 2;
723 /* "Why do we add bfd_section_vma?", I hear you cry.
724 Well, the start of the section in the file is actually
725 that far into the section as the struct vmap understands it.
726 So for text sections, bfd_section_vma tends to be 0x200,
727 and if vp->tstart is 0xd0002000, then the first byte of
728 the text section on disk corresponds to address 0xd0002200. */
730 stp
->the_bfd_section
= bfd_get_section_by_name (stp
->bfd
, ".text");
731 stp
->addr
= bfd_section_vma (stp
->bfd
, stp
->the_bfd_section
) + vp
->tstart
;
732 stp
->endaddr
= bfd_section_vma (stp
->bfd
, stp
->the_bfd_section
) + vp
->tend
;
736 stp
->the_bfd_section
= bfd_get_section_by_name (stp
->bfd
, ".data");
737 stp
->addr
= bfd_section_vma (stp
->bfd
, stp
->the_bfd_section
) + vp
->dstart
;
738 stp
->endaddr
= bfd_section_vma (stp
->bfd
, stp
->the_bfd_section
) + vp
->dend
;
743 add_text_to_loadinfo ((CORE_ADDR
)ldip
->ldinfo_textorg
,
744 (CORE_ADDR
)ldip
->ldinfo_dataorg
);
745 } while (ldip
->ldinfo_next
!= 0);
This page took 0.060015 seconds and 5 git commands to generate.