1 /* IBM RS/6000 native-dependent code for GDB, the GNU debugger.
2 Copyright 1986, 1987, 1989, 1991, 1992, 1994, 1995, 1996, 1997, 1998
3 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
25 #include "xcoffsolib.h"
28 #include "libbfd.h" /* For bfd_cache_lookup (FIXME) */
30 #include "gdb-stabs.h"
32 #include <sys/ptrace.h>
35 #include <sys/param.h>
39 #include <sys/ioctl.h>
50 extern struct vmap * map_vmap PARAMS ((bfd *bf, bfd *arch));
52 extern struct target_ops exec_ops;
55 vmap_exec PARAMS ((void));
58 vmap_ldinfo PARAMS ((struct ld_info *));
61 add_vmap PARAMS ((struct ld_info *));
64 objfile_symbol_add PARAMS ((char *));
67 vmap_symtab PARAMS ((struct vmap *));
70 fetch_core_registers PARAMS ((char *, unsigned int, int, CORE_ADDR));
73 exec_one_dummy_insn PARAMS ((void));
76 fixup_breakpoints PARAMS ((CORE_ADDR low, CORE_ADDR high, CORE_ADDR delta));
78 /* Conversion from gdb-to-system special purpose register numbers.. */
80 static int special_regs[] = {
91 fetch_inferior_registers (regno)
95 extern char registers[];
97 if (regno < 0) { /* for all registers */
99 /* read 32 general purpose registers. */
101 for (ii=0; ii < 32; ++ii)
102 *(int*)®isters[REGISTER_BYTE (ii)] =
103 ptrace (PT_READ_GPR, inferior_pid, (PTRACE_ARG3_TYPE) ii, 0, 0);
105 /* read general purpose floating point registers. */
107 for (ii=0; ii < 32; ++ii)
108 ptrace (PT_READ_FPR, inferior_pid,
109 (PTRACE_ARG3_TYPE) ®isters [REGISTER_BYTE (FP0_REGNUM+ii)],
112 /* read special registers. */
113 for (ii=0; ii <= LAST_SP_REGNUM-FIRST_SP_REGNUM; ++ii)
114 *(int*)®isters[REGISTER_BYTE (FIRST_SP_REGNUM+ii)] =
115 ptrace (PT_READ_GPR, inferior_pid, (PTRACE_ARG3_TYPE) special_regs[ii],
118 registers_fetched ();
122 /* else an individual register is addressed. */
124 else if (regno < FP0_REGNUM) { /* a GPR */
125 *(int*)®isters[REGISTER_BYTE (regno)] =
126 ptrace (PT_READ_GPR, inferior_pid, (PTRACE_ARG3_TYPE) regno, 0, 0);
128 else if (regno <= FPLAST_REGNUM) { /* a FPR */
129 ptrace (PT_READ_FPR, inferior_pid,
130 (PTRACE_ARG3_TYPE) ®isters [REGISTER_BYTE (regno)],
131 (regno-FP0_REGNUM+FPR0), 0);
133 else if (regno <= LAST_SP_REGNUM) { /* a special register */
134 *(int*)®isters[REGISTER_BYTE (regno)] =
135 ptrace (PT_READ_GPR, inferior_pid,
136 (PTRACE_ARG3_TYPE) special_regs[regno-FIRST_SP_REGNUM], 0, 0);
139 fprintf_unfiltered (gdb_stderr, "gdb error: register no %d not implemented.\n", regno);
141 register_valid [regno] = 1;
144 /* Store our register values back into the inferior.
145 If REGNO is -1, do this for all registers.
146 Otherwise, REGNO specifies which register (so we can save time). */
149 store_inferior_registers (regno)
152 extern char registers[];
157 { /* for all registers.. */
160 /* execute one dummy instruction (which is a breakpoint) in inferior
161 process. So give kernel a chance to do internal house keeping.
162 Otherwise the following ptrace(2) calls will mess up user stack
163 since kernel will get confused about the bottom of the stack (%sp) */
165 exec_one_dummy_insn ();
167 /* write general purpose registers first! */
168 for ( ii=GPR0; ii<=GPR31; ++ii)
170 ptrace (PT_WRITE_GPR, inferior_pid, (PTRACE_ARG3_TYPE) ii,
171 *(int*)®isters[REGISTER_BYTE (ii)], 0);
174 perror ("ptrace write_gpr");
179 /* write floating point registers now. */
180 for ( ii=0; ii < 32; ++ii)
182 ptrace (PT_WRITE_FPR, inferior_pid,
183 (PTRACE_ARG3_TYPE) ®isters[REGISTER_BYTE (FP0_REGNUM+ii)],
187 perror ("ptrace write_fpr");
192 /* write special registers. */
193 for (ii=0; ii <= LAST_SP_REGNUM-FIRST_SP_REGNUM; ++ii)
195 ptrace (PT_WRITE_GPR, inferior_pid,
196 (PTRACE_ARG3_TYPE) special_regs[ii],
197 *(int*)®isters[REGISTER_BYTE (FIRST_SP_REGNUM+ii)], 0);
200 perror ("ptrace write_gpr");
206 /* else, a specific register number is given... */
208 else if (regno < FP0_REGNUM) /* a GPR */
210 ptrace (PT_WRITE_GPR, inferior_pid, (PTRACE_ARG3_TYPE) regno,
211 *(int*)®isters[REGISTER_BYTE (regno)], 0);
214 else if (regno <= FPLAST_REGNUM) /* a FPR */
216 ptrace (PT_WRITE_FPR, inferior_pid,
217 (PTRACE_ARG3_TYPE) ®isters[REGISTER_BYTE (regno)],
218 regno - FP0_REGNUM + FPR0, 0);
221 else if (regno <= LAST_SP_REGNUM) /* a special register */
223 ptrace (PT_WRITE_GPR, inferior_pid,
224 (PTRACE_ARG3_TYPE) special_regs [regno-FIRST_SP_REGNUM],
225 *(int*)®isters[REGISTER_BYTE (regno)], 0);
229 fprintf_unfiltered (gdb_stderr, "Gdb error: register no %d not implemented.\n", regno);
233 perror ("ptrace write");
238 /* Execute one dummy breakpoint instruction. This way we give the kernel
239 a chance to do some housekeeping and update inferior's internal data,
243 exec_one_dummy_insn ()
245 #define DUMMY_INSN_ADDR (TEXT_SEGMENT_BASE)+0x200
247 char shadow_contents[BREAKPOINT_MAX]; /* Stash old bkpt addr contents */
251 /* We plant one dummy breakpoint into DUMMY_INSN_ADDR address. We assume that
252 this address will never be executed again by the real code. */
254 target_insert_breakpoint (DUMMY_INSN_ADDR, shadow_contents);
258 /* You might think this could be done with a single ptrace call, and
259 you'd be correct for just about every platform I've ever worked
260 on. However, rs6000-ibm-aix4.1.3 seems to have screwed this up --
261 the inferior never hits the breakpoint (it's also worth noting
262 powerpc-ibm-aix4.1.3 works correctly). */
263 prev_pc = read_pc ();
264 write_pc (DUMMY_INSN_ADDR);
265 ptrace (PT_CONTINUE, inferior_pid, (PTRACE_ARG3_TYPE)1, 0, 0);
268 perror ("pt_continue");
271 pid = wait (&status);
272 } while (pid != inferior_pid);
275 target_remove_breakpoint (DUMMY_INSN_ADDR, shadow_contents);
279 fetch_core_registers (core_reg_sect, core_reg_size, which, reg_addr)
281 unsigned core_reg_size;
283 CORE_ADDR reg_addr; /* Unused in this version */
285 /* fetch GPRs and special registers from the first register section
289 /* copy GPRs first. */
290 memcpy (registers, core_reg_sect, 32 * 4);
292 /* gdb's internal register template and bfd's register section layout
293 should share a common include file. FIXMEmgo */
294 /* then comes special registes. They are supposed to be in the same
295 order in gdb template and bfd `.reg' section. */
296 core_reg_sect += (32 * 4);
297 memcpy (®isters [REGISTER_BYTE (FIRST_SP_REGNUM)], core_reg_sect,
298 (LAST_SP_REGNUM - FIRST_SP_REGNUM + 1) * 4);
301 /* fetch floating point registers from register section 2 in core bfd. */
303 memcpy (®isters [REGISTER_BYTE (FP0_REGNUM)], core_reg_sect, 32 * 8);
306 fprintf_unfiltered (gdb_stderr, "Gdb error: unknown parameter to fetch_core_registers().\n");
309 /* handle symbol translation on vmapping */
313 register struct vmap *vp;
315 register struct objfile *objfile;
316 struct section_offsets *new_offsets;
319 objfile = vp->objfile;
322 /* OK, it's not an objfile we opened ourselves.
323 Currently, that can only happen with the exec file, so
324 relocate the symbols for the symfile. */
325 if (symfile_objfile == NULL)
327 objfile = symfile_objfile;
331 (sizeof (struct section_offsets)
332 + sizeof (new_offsets->offsets) * objfile->num_sections);
334 for (i = 0; i < objfile->num_sections; ++i)
335 ANOFFSET (new_offsets, i) = ANOFFSET (objfile->section_offsets, i);
337 /* The symbols in the object file are linked to the VMA of the section,
338 relocate them VMA relative. */
339 ANOFFSET (new_offsets, SECT_OFF_TEXT) = vp->tstart - vp->tvma;
340 ANOFFSET (new_offsets, SECT_OFF_DATA) = vp->dstart - vp->dvma;
341 ANOFFSET (new_offsets, SECT_OFF_BSS) = vp->dstart - vp->dvma;
343 objfile_relocate (objfile, new_offsets);
346 /* Add symbols for an objfile. */
349 objfile_symbol_add (arg)
352 struct objfile *obj = (struct objfile *) arg;
354 syms_from_objfile (obj, 0, 0, 0);
355 new_symfile_objfile (obj, 0, 0);
359 /* Add a new vmap entry based on ldinfo() information.
361 If ldi->ldinfo_fd is not valid (e.g. this struct ld_info is from a
362 core file), the caller should set it to -1, and we will open the file.
364 Return the vmap new entry. */
368 register struct ld_info *ldi;
371 register char *mem, *objname;
375 /* This ldi structure was allocated using alloca() in
376 xcoff_relocate_symtab(). Now we need to have persistent object
377 and member names, so we should save them. */
379 mem = ldi->ldinfo_filename + strlen (ldi->ldinfo_filename) + 1;
380 mem = savestring (mem, strlen (mem));
381 objname = savestring (ldi->ldinfo_filename, strlen (ldi->ldinfo_filename));
383 if (ldi->ldinfo_fd < 0)
384 /* Note that this opens it once for every member; a possible
385 enhancement would be to only open it once for every object. */
386 abfd = bfd_openr (objname, gnutarget);
388 abfd = bfd_fdopenr (objname, gnutarget, ldi->ldinfo_fd);
390 error ("Could not open `%s' as an executable file: %s",
391 objname, bfd_errmsg (bfd_get_error ()));
393 /* make sure we have an object file */
395 if (bfd_check_format (abfd, bfd_object))
396 vp = map_vmap (abfd, 0);
398 else if (bfd_check_format (abfd, bfd_archive))
401 /* FIXME??? am I tossing BFDs? bfd? */
402 while ((last = bfd_openr_next_archived_file (abfd, last)))
403 if (STREQ (mem, last->filename))
409 /* FIXME -- should be error */
410 warning ("\"%s\": member \"%s\" missing.", abfd->filename, mem);
414 if (!bfd_check_format(last, bfd_object))
416 bfd_close (last); /* XXX??? */
420 vp = map_vmap (last, abfd);
426 error ("\"%s\": not in executable format: %s.",
427 objname, bfd_errmsg (bfd_get_error ()));
430 obj = allocate_objfile (vp->bfd, 0);
433 #ifndef SOLIB_SYMBOLS_MANUAL
434 if (catch_errors (objfile_symbol_add, (char *)obj,
435 "Error while reading shared library symbols:\n",
438 /* Note this is only done if symbol reading was successful. */
446 /* update VMAP info with ldinfo() information
447 Input is ptr to ldinfo() results. */
451 register struct ld_info *ldi;
454 register struct vmap *vp;
455 int got_one, retried;
456 int got_exec_file = 0;
458 /* For each *ldi, see if we have a corresponding *vp.
459 If so, update the mapping, and symbol table.
460 If not, add an entry and symbol table. */
463 char *name = ldi->ldinfo_filename;
464 char *memb = name + strlen(name) + 1;
468 if (fstat (ldi->ldinfo_fd, &ii) < 0)
470 /* The kernel sets ld_info to -1, if the process is still using the
471 object, and the object is removed. Keep the symbol info for the
472 removed object and issue a warning. */
473 warning ("%s (fd=%d) has disappeared, keeping its symbols",
474 name, ldi->ldinfo_fd);
478 for (got_one = 0, vp = vmap; vp; vp = vp->nxt)
480 struct objfile *objfile;
482 /* First try to find a `vp', which is the same as in ldinfo.
483 If not the same, just continue and grep the next `vp'. If same,
484 relocate its tstart, tend, dstart, dend values. If no such `vp'
485 found, get out of this for loop, add this ldi entry as a new vmap
486 (add_vmap) and come back, find its `vp' and so on... */
488 /* The filenames are not always sufficient to match on. */
490 if ((name[0] == '/' && !STREQ(name, vp->name))
491 || (memb[0] && !STREQ(memb, vp->member)))
494 /* See if we are referring to the same file.
495 We have to check objfile->obfd, symfile.c:reread_symbols might
496 have updated the obfd after a change. */
497 objfile = vp->objfile == NULL ? symfile_objfile : vp->objfile;
499 || objfile->obfd == NULL
500 || bfd_stat (objfile->obfd, &vi) < 0)
502 warning ("Unable to stat %s, keeping its symbols", name);
506 if (ii.st_dev != vi.st_dev || ii.st_ino != vi.st_ino)
510 close (ldi->ldinfo_fd);
514 /* Found a corresponding VMAP. Remap! */
516 /* We can assume pointer == CORE_ADDR, this code is native only. */
517 vp->tstart = (CORE_ADDR) ldi->ldinfo_textorg;
518 vp->tend = vp->tstart + ldi->ldinfo_textsize;
519 vp->dstart = (CORE_ADDR) ldi->ldinfo_dataorg;
520 vp->dend = vp->dstart + ldi->ldinfo_datasize;
522 /* The run time loader maps the file header in addition to the text
523 section and returns a pointer to the header in ldinfo_textorg.
524 Adjust the text start address to point to the real start address
525 of the text section. */
526 vp->tstart += vp->toffs;
528 /* The objfile is only NULL for the exec file. */
529 if (vp->objfile == NULL)
532 /* relocate symbol table(s). */
535 /* There may be more, so we don't break out of the loop. */
538 /* if there was no matching *vp, we must perforce create the sucker(s) */
539 if (!got_one && !retried)
545 } while (ldi->ldinfo_next
546 && (ldi = (void *) (ldi->ldinfo_next + (char *) ldi)));
548 /* If we don't find the symfile_objfile anywhere in the ldinfo, it
549 is unlikely that the symbol file is relocated to the proper
550 address. And we might have attached to a process which is
551 running a different copy of the same executable. */
552 if (symfile_objfile != NULL && !got_exec_file)
555 fputs_unfiltered ("Symbol file ", gdb_stderr);
556 fputs_unfiltered (symfile_objfile->name, gdb_stderr);
557 fputs_unfiltered ("\nis not mapped; discarding it.\n\
558 If in fact that file has symbols which the mapped files listed by\n\
559 \"info files\" lack, you can load symbols with the \"symbol-file\" or\n\
560 \"add-symbol-file\" commands (note that you must take care of relocating\n\
561 symbols to the proper address).\n", gdb_stderr);
562 free_objfile (symfile_objfile);
563 symfile_objfile = NULL;
565 breakpoint_re_set ();
568 /* As well as symbol tables, exec_sections need relocation. After
569 the inferior process' termination, there will be a relocated symbol
570 table exist with no corresponding inferior process. At that time, we
571 need to use `exec' bfd, rather than the inferior process's memory space
574 `exec_sections' need to be relocated only once, as long as the exec
575 file remains unchanged.
584 if (execbfd == exec_bfd)
589 if (!vmap || !exec_ops.to_sections)
590 error ("vmap_exec: vmap or exec_ops.to_sections == 0\n");
592 for (i=0; &exec_ops.to_sections[i] < exec_ops.to_sections_end; i++)
594 if (STREQ(".text", exec_ops.to_sections[i].the_bfd_section->name))
596 exec_ops.to_sections[i].addr += vmap->tstart - vmap->tvma;
597 exec_ops.to_sections[i].endaddr += vmap->tstart - vmap->tvma;
599 else if (STREQ(".data", exec_ops.to_sections[i].the_bfd_section->name))
601 exec_ops.to_sections[i].addr += vmap->dstart - vmap->dvma;
602 exec_ops.to_sections[i].endaddr += vmap->dstart - vmap->dvma;
604 else if (STREQ(".bss", exec_ops.to_sections[i].the_bfd_section->name))
606 exec_ops.to_sections[i].addr += vmap->dstart - vmap->dvma;
607 exec_ops.to_sections[i].endaddr += vmap->dstart - vmap->dvma;
612 /* xcoff_relocate_symtab - hook for symbol table relocation.
613 also reads shared libraries.. */
616 xcoff_relocate_symtab (pid)
619 #define MAX_LOAD_SEGS 64 /* maximum number of load segments */
623 ldi = (void *) alloca(MAX_LOAD_SEGS * sizeof (*ldi));
625 /* According to my humble theory, AIX has some timing problems and
626 when the user stack grows, kernel doesn't update stack info in time
627 and ptrace calls step on user stack. That is why we sleep here a little,
628 and give kernel to update its internals. */
633 ptrace (PT_LDINFO, pid, (PTRACE_ARG3_TYPE) ldi,
634 MAX_LOAD_SEGS * sizeof(*ldi), (int *) ldi);
636 perror_with_name ("ptrace ldinfo");
640 /* relocate the exec and core sections as well. */
644 /* Core file stuff. */
646 /* Relocate symtabs and read in shared library info, based on symbols
647 from the core file. */
650 xcoff_relocate_core (target)
651 struct target_ops *target;
653 /* Offset of member MEMBER in a struct of type TYPE. */
655 #define offsetof(TYPE, MEMBER) ((int) &((TYPE *)0)->MEMBER)
658 /* Size of a struct ld_info except for the variable-length filename. */
659 #define LDINFO_SIZE (offsetof (struct ld_info, ldinfo_filename))
663 struct ld_info *ldip;
666 /* Allocated size of buffer. */
667 int buffer_size = LDINFO_SIZE;
668 char *buffer = xmalloc (buffer_size);
669 struct cleanup *old = make_cleanup (free_current_contents, &buffer);
671 /* FIXME, this restriction should not exist. For now, though I'll
672 avoid coredumps with error() pending a real fix. */
675 ("Can't debug a core file without an executable file (on the RS/6000)");
677 ldinfo_sec = bfd_get_section_by_name (core_bfd, ".ldinfo");
678 if (ldinfo_sec == NULL)
681 fprintf_filtered (gdb_stderr, "Couldn't get ldinfo from core file: %s\n",
682 bfd_errmsg (bfd_get_error ()));
691 /* Read in everything but the name. */
692 if (bfd_get_section_contents (core_bfd, ldinfo_sec, buffer,
693 offset, LDINFO_SIZE) == 0)
700 if (i == buffer_size)
703 buffer = xrealloc (buffer, buffer_size);
705 if (bfd_get_section_contents (core_bfd, ldinfo_sec, &buffer[i],
708 if (buffer[i++] == '\0')
710 } while (names_found < 2);
712 ldip = (struct ld_info *) buffer;
714 /* Can't use a file descriptor from the core file; need to open it. */
715 ldip->ldinfo_fd = -1;
717 /* The first ldinfo is for the exec file, allocated elsewhere. */
721 vp = add_vmap (ldip);
723 offset += ldip->ldinfo_next;
725 /* We can assume pointer == CORE_ADDR, this code is native only. */
726 vp->tstart = (CORE_ADDR) ldip->ldinfo_textorg;
727 vp->tend = vp->tstart + ldip->ldinfo_textsize;
728 vp->dstart = (CORE_ADDR) ldip->ldinfo_dataorg;
729 vp->dend = vp->dstart + ldip->ldinfo_datasize;
731 /* The run time loader maps the file header in addition to the text
732 section and returns a pointer to the header in ldinfo_textorg.
733 Adjust the text start address to point to the real start address
734 of the text section. */
735 vp->tstart += vp->toffs;
737 /* Unless this is the exec file,
738 add our sections to the section table for the core target. */
742 struct section_table *stp;
745 /* We must update the to_sections field in the core_ops structure
746 now to avoid dangling pointer dereferences. */
747 update_coreops = core_ops.to_sections == target->to_sections;
749 count = target->to_sections_end - target->to_sections;
751 target->to_sections = (struct section_table *)
752 xrealloc (target->to_sections,
753 sizeof (struct section_table) * count);
754 target->to_sections_end = target->to_sections + count;
756 /* Update the to_sections field in the core_ops structure
760 core_ops.to_sections = target->to_sections;
761 core_ops.to_sections_end = target->to_sections_end;
763 stp = target->to_sections_end - 2;
766 stp->the_bfd_section = bfd_get_section_by_name (stp->bfd, ".text");
767 stp->addr = vp->tstart;
768 stp->endaddr = vp->tend;
772 stp->the_bfd_section = bfd_get_section_by_name (stp->bfd, ".data");
773 stp->addr = vp->dstart;
774 stp->endaddr = vp->dend;
778 } while (ldip->ldinfo_next != 0);
780 breakpoint_re_set ();
787 return (sizeof (struct user));
790 /* Under AIX, we have to pass the correct TOC pointer to a function
791 when calling functions in the inferior.
792 We try to find the relative toc offset of the objfile containing PC
793 and add the current load address of the data segment from the vmap. */
796 find_toc_address (pc)
801 for (vp = vmap; vp; vp = vp->nxt)
803 if (pc >= vp->tstart && pc < vp->tend)
805 /* vp->objfile is only NULL for the exec file. */
806 return vp->dstart + get_toc_offset (vp->objfile == NULL
811 error ("Unable to find TOC entry for pc 0x%x\n", pc);
814 /* Register that we are able to handle rs6000 core file formats. */
816 static struct core_fns rs6000_core_fns =
818 bfd_target_coff_flavour,
819 fetch_core_registers,
824 _initialize_core_rs6000 ()
826 /* Initialize hook in rs6000-tdep.c for determining the TOC address when
827 calling functions in the inferior. */
828 find_toc_address_hook = &find_toc_address;
830 /* For native configurations, where this module is included, inform
831 the xcoffsolib module where it can find the function for symbol table
832 relocation at runtime. */
833 xcoff_relocate_symtab_hook = &xcoff_relocate_symtab;
834 add_core_fns (&rs6000_core_fns);