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., 675 Mass Ave, Cambridge, MA 02139, USA. */
24 #include "xcoffsolib.h"
27 #include "libbfd.h" /* For bfd_cache_lookup (FIXME) */
30 #include <sys/ptrace.h>
33 #include <sys/param.h>
37 #include <sys/ioctl.h>
48 extern struct vmap * map_vmap PARAMS ((bfd *bf, bfd *arch));
50 extern struct target_ops exec_ops;
53 exec_one_dummy_insn PARAMS ((void));
56 add_text_to_loadinfo PARAMS ((CORE_ADDR textaddr, CORE_ADDR dataaddr));
59 fixup_breakpoints PARAMS ((CORE_ADDR low, CORE_ADDR high, CORE_ADDR delta));
61 /* Conversion from gdb-to-system special purpose register numbers.. */
63 static int special_regs[] = {
74 fetch_inferior_registers (regno)
78 extern char registers[];
80 if (regno < 0) { /* for all registers */
82 /* read 32 general purpose registers. */
84 for (ii=0; ii < 32; ++ii)
85 *(int*)®isters[REGISTER_BYTE (ii)] =
86 ptrace (PT_READ_GPR, inferior_pid, (PTRACE_ARG3_TYPE) ii, 0, 0);
88 /* read general purpose floating point registers. */
90 for (ii=0; ii < 32; ++ii)
91 ptrace (PT_READ_FPR, inferior_pid,
92 (PTRACE_ARG3_TYPE) ®isters [REGISTER_BYTE (FP0_REGNUM+ii)],
95 /* read special registers. */
96 for (ii=0; ii <= LAST_SP_REGNUM-FIRST_SP_REGNUM; ++ii)
97 *(int*)®isters[REGISTER_BYTE (FIRST_SP_REGNUM+ii)] =
98 ptrace (PT_READ_GPR, inferior_pid, (PTRACE_ARG3_TYPE) special_regs[ii],
101 registers_fetched ();
105 /* else an individual register is addressed. */
107 else if (regno < FP0_REGNUM) { /* a GPR */
108 *(int*)®isters[REGISTER_BYTE (regno)] =
109 ptrace (PT_READ_GPR, inferior_pid, (PTRACE_ARG3_TYPE) regno, 0, 0);
111 else if (regno <= FPLAST_REGNUM) { /* a FPR */
112 ptrace (PT_READ_FPR, inferior_pid,
113 (PTRACE_ARG3_TYPE) ®isters [REGISTER_BYTE (regno)],
114 (regno-FP0_REGNUM+FPR0), 0);
116 else if (regno <= LAST_SP_REGNUM) { /* a special register */
117 *(int*)®isters[REGISTER_BYTE (regno)] =
118 ptrace (PT_READ_GPR, inferior_pid,
119 (PTRACE_ARG3_TYPE) special_regs[regno-FIRST_SP_REGNUM], 0, 0);
122 fprintf_unfiltered (gdb_stderr, "gdb error: register no %d not implemented.\n", regno);
124 register_valid [regno] = 1;
127 /* Store our register values back into the inferior.
128 If REGNO is -1, do this for all registers.
129 Otherwise, REGNO specifies which register (so we can save time). */
132 store_inferior_registers (regno)
135 extern char registers[];
140 { /* for all registers.. */
143 /* execute one dummy instruction (which is a breakpoint) in inferior
144 process. So give kernel a chance to do internal house keeping.
145 Otherwise the following ptrace(2) calls will mess up user stack
146 since kernel will get confused about the bottom of the stack (%sp) */
148 exec_one_dummy_insn ();
150 /* write general purpose registers first! */
151 for ( ii=GPR0; ii<=GPR31; ++ii)
153 ptrace (PT_WRITE_GPR, inferior_pid, (PTRACE_ARG3_TYPE) ii,
154 *(int*)®isters[REGISTER_BYTE (ii)], 0);
157 perror ("ptrace write_gpr");
162 /* write floating point registers now. */
163 for ( ii=0; ii < 32; ++ii)
165 ptrace (PT_WRITE_FPR, inferior_pid,
166 (PTRACE_ARG3_TYPE) ®isters[REGISTER_BYTE (FP0_REGNUM+ii)],
170 perror ("ptrace write_fpr");
175 /* write special registers. */
176 for (ii=0; ii <= LAST_SP_REGNUM-FIRST_SP_REGNUM; ++ii)
178 ptrace (PT_WRITE_GPR, inferior_pid,
179 (PTRACE_ARG3_TYPE) special_regs[ii],
180 *(int*)®isters[REGISTER_BYTE (FIRST_SP_REGNUM+ii)], 0);
183 perror ("ptrace write_gpr");
189 /* else, a specific register number is given... */
191 else if (regno < FP0_REGNUM) /* a GPR */
193 ptrace (PT_WRITE_GPR, inferior_pid, (PTRACE_ARG3_TYPE) regno,
194 *(int*)®isters[REGISTER_BYTE (regno)], 0);
197 else if (regno <= FPLAST_REGNUM) /* a FPR */
199 ptrace (PT_WRITE_FPR, inferior_pid,
200 (PTRACE_ARG3_TYPE) ®isters[REGISTER_BYTE (regno)],
201 regno - FP0_REGNUM + FPR0, 0);
204 else if (regno <= LAST_SP_REGNUM) /* a special register */
206 ptrace (PT_WRITE_GPR, inferior_pid,
207 (PTRACE_ARG3_TYPE) special_regs [regno-FIRST_SP_REGNUM],
208 *(int*)®isters[REGISTER_BYTE (regno)], 0);
212 fprintf_unfiltered (gdb_stderr, "Gdb error: register no %d not implemented.\n", regno);
216 perror ("ptrace write");
221 /* Execute one dummy breakpoint instruction. This way we give the kernel
222 a chance to do some housekeeping and update inferior's internal data,
226 exec_one_dummy_insn ()
228 #define DUMMY_INSN_ADDR (TEXT_SEGMENT_BASE)+0x200
230 char shadow_contents[BREAKPOINT_MAX]; /* Stash old bkpt addr contents */
231 unsigned int status, pid;
233 /* We plant one dummy breakpoint into DUMMY_INSN_ADDR address. We assume that
234 this address will never be executed again by the real code. */
236 target_insert_breakpoint (DUMMY_INSN_ADDR, shadow_contents);
239 ptrace (PT_CONTINUE, inferior_pid, (PTRACE_ARG3_TYPE) DUMMY_INSN_ADDR, 0, 0);
241 perror ("pt_continue");
244 pid = wait (&status);
245 } while (pid != inferior_pid);
247 target_remove_breakpoint (DUMMY_INSN_ADDR, shadow_contents);
251 fetch_core_registers (core_reg_sect, core_reg_size, which, reg_addr)
253 unsigned core_reg_size;
255 unsigned int reg_addr; /* Unused in this version */
257 /* fetch GPRs and special registers from the first register section
261 /* copy GPRs first. */
262 memcpy (registers, core_reg_sect, 32 * 4);
264 /* gdb's internal register template and bfd's register section layout
265 should share a common include file. FIXMEmgo */
266 /* then comes special registes. They are supposed to be in the same
267 order in gdb template and bfd `.reg' section. */
268 core_reg_sect += (32 * 4);
269 memcpy (®isters [REGISTER_BYTE (FIRST_SP_REGNUM)], core_reg_sect,
270 (LAST_SP_REGNUM - FIRST_SP_REGNUM + 1) * 4);
273 /* fetch floating point registers from register section 2 in core bfd. */
275 memcpy (®isters [REGISTER_BYTE (FP0_REGNUM)], core_reg_sect, 32 * 8);
278 fprintf_unfiltered (gdb_stderr, "Gdb error: unknown parameter to fetch_core_registers().\n");
281 /* handle symbol translation on vmapping */
285 register struct vmap *vp;
287 register struct objfile *objfile;
291 CORE_ADDR text_delta;
292 CORE_ADDR data_delta;
294 struct section_offsets *new_offsets;
297 objfile = vp->objfile;
300 /* OK, it's not an objfile we opened ourselves.
301 Currently, that can only happen with the exec file, so
302 relocate the symbols for the symfile. */
303 if (symfile_objfile == NULL)
305 objfile = symfile_objfile;
309 (sizeof (struct section_offsets)
310 + sizeof (new_offsets->offsets) * objfile->num_sections);
312 for (i = 0; i < objfile->num_sections; ++i)
313 ANOFFSET (new_offsets, i) = ANOFFSET (objfile->section_offsets, i);
315 textsec = bfd_get_section_by_name (vp->bfd, ".text");
317 vp->tstart - ANOFFSET (objfile->section_offsets, textsec->target_index);
318 ANOFFSET (new_offsets, textsec->target_index) = vp->tstart;
320 datasec = bfd_get_section_by_name (vp->bfd, ".data");
322 vp->dstart - ANOFFSET (objfile->section_offsets, datasec->target_index);
323 ANOFFSET (new_offsets, datasec->target_index) = vp->dstart;
325 bsssec = bfd_get_section_by_name (vp->bfd, ".bss");
327 vp->dstart - ANOFFSET (objfile->section_offsets, bsssec->target_index);
328 ANOFFSET (new_offsets, bsssec->target_index) = vp->dstart;
330 objfile_relocate (objfile, new_offsets);
333 struct obj_section *s;
334 for (s = objfile->sections; s < objfile->sections_end; ++s)
336 if (s->the_bfd_section->target_index == textsec->target_index)
338 s->addr += text_delta;
339 s->endaddr += text_delta;
341 else if (s->the_bfd_section->target_index == datasec->target_index)
343 s->addr += data_delta;
344 s->endaddr += data_delta;
346 else if (s->the_bfd_section->target_index == bsssec->target_index)
348 s->addr += bss_delta;
349 s->endaddr += bss_delta;
355 /* breakpoints need to be relocated as well. */
356 fixup_breakpoints (0, TEXT_SEGMENT_BASE, text_delta);
359 /* Add symbols for an objfile. */
362 objfile_symbol_add (arg)
365 struct objfile *obj = (struct objfile *) arg;
367 syms_from_objfile (obj, 0, 0, 0);
368 new_symfile_objfile (obj, 0, 0);
372 /* Add a new vmap entry based on ldinfo() information.
374 If ldi->ldinfo_fd is not valid (e.g. this struct ld_info is from a
375 core file), the caller should set it to -1, and we will open the file.
377 Return the vmap new entry. */
381 register struct ld_info *ldi;
384 register char *mem, *objname;
388 /* This ldi structure was allocated using alloca() in
389 xcoff_relocate_symtab(). Now we need to have persistent object
390 and member names, so we should save them. */
392 mem = ldi->ldinfo_filename + strlen (ldi->ldinfo_filename) + 1;
393 mem = savestring (mem, strlen (mem));
394 objname = savestring (ldi->ldinfo_filename, strlen (ldi->ldinfo_filename));
396 if (ldi->ldinfo_fd < 0)
397 /* Note that this opens it once for every member; a possible
398 enhancement would be to only open it once for every object. */
399 abfd = bfd_openr (objname, gnutarget);
401 abfd = bfd_fdopenr (objname, gnutarget, ldi->ldinfo_fd);
403 error ("Could not open `%s' as an executable file: %s",
404 objname, bfd_errmsg (bfd_get_error ()));
406 /* make sure we have an object file */
408 if (bfd_check_format (abfd, bfd_object))
409 vp = map_vmap (abfd, 0);
411 else if (bfd_check_format (abfd, bfd_archive))
414 /* FIXME??? am I tossing BFDs? bfd? */
415 while ((last = bfd_openr_next_archived_file (abfd, last)))
416 if (STREQ (mem, last->filename))
422 /* FIXME -- should be error */
423 warning ("\"%s\": member \"%s\" missing.", abfd->filename, mem);
427 if (!bfd_check_format(last, bfd_object))
429 bfd_close (last); /* XXX??? */
433 vp = map_vmap (last, abfd);
439 error ("\"%s\": not in executable format: %s.",
440 objname, bfd_errmsg (bfd_get_error ()));
443 obj = allocate_objfile (vp->bfd, 0);
446 #ifndef SOLIB_SYMBOLS_MANUAL
447 if (catch_errors (objfile_symbol_add, (char *)obj,
448 "Error while reading shared library symbols:\n",
451 /* Note this is only done if symbol reading was successful. */
459 /* update VMAP info with ldinfo() information
460 Input is ptr to ldinfo() results. */
464 register struct ld_info *ldi;
467 register struct vmap *vp;
468 int got_one, retried;
472 /* For each *ldi, see if we have a corresponding *vp.
473 If so, update the mapping, and symbol table.
474 If not, add an entry and symbol table. */
477 char *name = ldi->ldinfo_filename;
478 char *memb = name + strlen(name) + 1;
482 if (fstat (ldi->ldinfo_fd, &ii) < 0)
483 fatal ("cannot fstat(fd=%d) on %s", ldi->ldinfo_fd, name);
485 for (got_one = 0, vp = vmap; vp; vp = vp->nxt)
489 /* First try to find a `vp', which is the same as in ldinfo.
490 If not the same, just continue and grep the next `vp'. If same,
491 relocate its tstart, tend, dstart, dend values. If no such `vp'
492 found, get out of this for loop, add this ldi entry as a new vmap
493 (add_vmap) and come back, fins its `vp' and so on... */
495 /* The filenames are not always sufficient to match on. */
497 if ((name[0] == '/' && !STREQ(name, vp->name))
498 || (memb[0] && !STREQ(memb, vp->member)))
501 io = bfd_cache_lookup (vp->bfd); /* totally opaque! */
503 fatal ("cannot find BFD's iostream for %s", vp->name);
505 /* See if we are referring to the same file. */
506 /* An error here is innocuous, most likely meaning that
507 the file descriptor has become worthless. */
508 if (fstat (fileno(io), &vi) < 0)
511 if (ii.st_dev != vi.st_dev || ii.st_ino != vi.st_ino)
515 close (ldi->ldinfo_fd);
519 /* found a corresponding VMAP. remap! */
522 /* We can assume pointer == CORE_ADDR, this code is native only. */
523 vp->tstart = (CORE_ADDR) ldi->ldinfo_textorg;
524 vp->tend = vp->tstart + ldi->ldinfo_textsize;
525 vp->dstart = (CORE_ADDR) ldi->ldinfo_dataorg;
526 vp->dend = vp->dstart + ldi->ldinfo_datasize;
530 vp->tstart += vp->tadj;
531 vp->tend += vp->tadj;
534 /* The objfile is only NULL for the exec file. */
535 if (vp->objfile == NULL)
538 /* relocate symbol table(s). */
541 /* there may be more, so we don't break out of the loop. */
544 /* if there was no matching *vp, we must perforce create the sucker(s) */
545 if (!got_one && !retried)
551 } while (ldi->ldinfo_next
552 && (ldi = (void *) (ldi->ldinfo_next + (char *) ldi)));
554 /* If we don't find the symfile_objfile anywhere in the ldinfo, it
555 is unlikely that the symbol file is relocated to the proper
556 address. And we might have attached to a process which is
557 running a different copy of the same executable. */
558 if (symfile_objfile != NULL && !got_exec_file)
561 fputs_unfiltered ("Symbol file ", gdb_stderr);
562 fputs_unfiltered (symfile_objfile->name, gdb_stderr);
563 fputs_unfiltered ("\nis not mapped; discarding it.\n\
564 If in fact that file has symbols which the mapped files listed by\n\
565 \"info files\" lack, you can load symbols with the \"symbol-file\" or\n\
566 \"add-symbol-file\" commands (note that you must take care of relocating\n\
567 symbols to the proper address).\n", gdb_stderr);
568 free_objfile (symfile_objfile);
569 symfile_objfile = NULL;
573 /* As well as symbol tables, exec_sections need relocation. After
574 the inferior process' termination, there will be a relocated symbol
575 table exist with no corresponding inferior process. At that time, we
576 need to use `exec' bfd, rather than the inferior process's memory space
579 `exec_sections' need to be relocated only once, as long as the exec
580 file remains unchanged.
589 if (execbfd == exec_bfd)
594 if (!vmap || !exec_ops.to_sections)
595 error ("vmap_exec: vmap or exec_ops.to_sections == 0\n");
597 for (i=0; &exec_ops.to_sections[i] < exec_ops.to_sections_end; i++)
599 if (STREQ(".text", exec_ops.to_sections[i].the_bfd_section->name))
601 exec_ops.to_sections[i].addr += vmap->tstart;
602 exec_ops.to_sections[i].endaddr += vmap->tstart;
604 else if (STREQ(".data", exec_ops.to_sections[i].the_bfd_section->name))
606 exec_ops.to_sections[i].addr += vmap->dstart;
607 exec_ops.to_sections[i].endaddr += vmap->dstart;
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), ldi);
636 perror_with_name ("ptrace ldinfo");
641 /* We are allowed to assume CORE_ADDR == pointer. This code is
643 add_text_to_loadinfo ((CORE_ADDR) ldi->ldinfo_textorg,
644 (CORE_ADDR) ldi->ldinfo_dataorg);
645 } while (ldi->ldinfo_next
646 && (ldi = (void *) (ldi->ldinfo_next + (char *) ldi)));
649 /* Now that we've jumbled things around, re-sort them. */
650 sort_minimal_symbols ();
653 /* relocate the exec and core sections as well. */
657 /* Core file stuff. */
659 /* Relocate symtabs and read in shared library info, based on symbols
660 from the core file. */
663 xcoff_relocate_core (target)
664 struct target_ops *target;
666 /* Offset of member MEMBER in a struct of type TYPE. */
668 #define offsetof(TYPE, MEMBER) ((int) &((TYPE *)0)->MEMBER)
671 /* Size of a struct ld_info except for the variable-length filename. */
672 #define LDINFO_SIZE (offsetof (struct ld_info, ldinfo_filename))
676 struct ld_info *ldip;
679 /* Allocated size of buffer. */
680 int buffer_size = LDINFO_SIZE;
681 char *buffer = xmalloc (buffer_size);
682 struct cleanup *old = make_cleanup (free_current_contents, &buffer);
684 /* FIXME, this restriction should not exist. For now, though I'll
685 avoid coredumps with error() pending a real fix. */
688 ("Can't debug a core file without an executable file (on the RS/6000)");
690 ldinfo_sec = bfd_get_section_by_name (core_bfd, ".ldinfo");
691 if (ldinfo_sec == NULL)
694 fprintf_filtered (gdb_stderr, "Couldn't get ldinfo from core file: %s\n",
695 bfd_errmsg (bfd_get_error ()));
704 /* Read in everything but the name. */
705 if (bfd_get_section_contents (core_bfd, ldinfo_sec, buffer,
706 offset, LDINFO_SIZE) == 0)
713 if (i == buffer_size)
716 buffer = xrealloc (buffer, buffer_size);
718 if (bfd_get_section_contents (core_bfd, ldinfo_sec, &buffer[i],
721 if (buffer[i++] == '\0')
723 } while (names_found < 2);
725 ldip = (struct ld_info *) buffer;
727 /* Can't use a file descriptor from the core file; need to open it. */
728 ldip->ldinfo_fd = -1;
730 /* The first ldinfo is for the exec file, allocated elsewhere. */
734 vp = add_vmap (ldip);
736 offset += ldip->ldinfo_next;
738 /* We can assume pointer == CORE_ADDR, this code is native only. */
739 vp->tstart = (CORE_ADDR) ldip->ldinfo_textorg;
740 vp->tend = vp->tstart + ldip->ldinfo_textsize;
741 vp->dstart = (CORE_ADDR) ldip->ldinfo_dataorg;
742 vp->dend = vp->dstart + ldip->ldinfo_datasize;
746 vp->tstart += vp->tadj;
747 vp->tend += vp->tadj;
750 /* Unless this is the exec file,
751 add our sections to the section table for the core target. */
755 struct section_table *stp;
757 count = target->to_sections_end - target->to_sections;
759 target->to_sections = (struct section_table *)
760 xrealloc (target->to_sections,
761 sizeof (struct section_table) * count);
762 target->to_sections_end = target->to_sections + count;
763 stp = target->to_sections_end - 2;
765 /* "Why do we add bfd_section_vma?", I hear you cry.
766 Well, the start of the section in the file is actually
767 that far into the section as the struct vmap understands it.
768 So for text sections, bfd_section_vma tends to be 0x200,
769 and if vp->tstart is 0xd0002000, then the first byte of
770 the text section on disk corresponds to address 0xd0002200. */
772 stp->the_bfd_section = bfd_get_section_by_name (stp->bfd, ".text");
773 stp->addr = bfd_section_vma (stp->bfd, stp->the_bfd_section) + vp->tstart;
774 stp->endaddr = bfd_section_vma (stp->bfd, stp->the_bfd_section) + vp->tend;
778 stp->the_bfd_section = bfd_get_section_by_name (stp->bfd, ".data");
779 stp->addr = bfd_section_vma (stp->bfd, stp->the_bfd_section) + vp->dstart;
780 stp->endaddr = bfd_section_vma (stp->bfd, stp->the_bfd_section) + vp->dend;
785 add_text_to_loadinfo ((CORE_ADDR)ldip->ldinfo_textorg,
786 (CORE_ADDR)ldip->ldinfo_dataorg);
787 } while (ldip->ldinfo_next != 0);