1 /* IBM RS/6000 native-dependent code for GDB, the GNU debugger.
2 Copyright 1986, 1987, 1989, 1991, 1992, 1994, 1995
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 exec_one_dummy_insn PARAMS ((void));
58 add_text_to_loadinfo PARAMS ((CORE_ADDR textaddr, CORE_ADDR dataaddr));
61 fixup_breakpoints PARAMS ((CORE_ADDR low, CORE_ADDR high, CORE_ADDR delta));
63 /* Conversion from gdb-to-system special purpose register numbers.. */
65 static int special_regs[] = {
76 fetch_inferior_registers (regno)
80 extern char registers[];
82 if (regno < 0) { /* for all registers */
84 /* read 32 general purpose registers. */
86 for (ii=0; ii < 32; ++ii)
87 *(int*)®isters[REGISTER_BYTE (ii)] =
88 ptrace (PT_READ_GPR, inferior_pid, (PTRACE_ARG3_TYPE) ii, 0, 0);
90 /* read general purpose floating point registers. */
92 for (ii=0; ii < 32; ++ii)
93 ptrace (PT_READ_FPR, inferior_pid,
94 (PTRACE_ARG3_TYPE) ®isters [REGISTER_BYTE (FP0_REGNUM+ii)],
97 /* read special registers. */
98 for (ii=0; ii <= LAST_SP_REGNUM-FIRST_SP_REGNUM; ++ii)
99 *(int*)®isters[REGISTER_BYTE (FIRST_SP_REGNUM+ii)] =
100 ptrace (PT_READ_GPR, inferior_pid, (PTRACE_ARG3_TYPE) special_regs[ii],
103 registers_fetched ();
107 /* else an individual register is addressed. */
109 else if (regno < FP0_REGNUM) { /* a GPR */
110 *(int*)®isters[REGISTER_BYTE (regno)] =
111 ptrace (PT_READ_GPR, inferior_pid, (PTRACE_ARG3_TYPE) regno, 0, 0);
113 else if (regno <= FPLAST_REGNUM) { /* a FPR */
114 ptrace (PT_READ_FPR, inferior_pid,
115 (PTRACE_ARG3_TYPE) ®isters [REGISTER_BYTE (regno)],
116 (regno-FP0_REGNUM+FPR0), 0);
118 else if (regno <= LAST_SP_REGNUM) { /* a special register */
119 *(int*)®isters[REGISTER_BYTE (regno)] =
120 ptrace (PT_READ_GPR, inferior_pid,
121 (PTRACE_ARG3_TYPE) special_regs[regno-FIRST_SP_REGNUM], 0, 0);
124 fprintf_unfiltered (gdb_stderr, "gdb error: register no %d not implemented.\n", regno);
126 register_valid [regno] = 1;
129 /* Store our register values back into the inferior.
130 If REGNO is -1, do this for all registers.
131 Otherwise, REGNO specifies which register (so we can save time). */
134 store_inferior_registers (regno)
137 extern char registers[];
142 { /* for all registers.. */
145 /* execute one dummy instruction (which is a breakpoint) in inferior
146 process. So give kernel a chance to do internal house keeping.
147 Otherwise the following ptrace(2) calls will mess up user stack
148 since kernel will get confused about the bottom of the stack (%sp) */
150 exec_one_dummy_insn ();
152 /* write general purpose registers first! */
153 for ( ii=GPR0; ii<=GPR31; ++ii)
155 ptrace (PT_WRITE_GPR, inferior_pid, (PTRACE_ARG3_TYPE) ii,
156 *(int*)®isters[REGISTER_BYTE (ii)], 0);
159 perror ("ptrace write_gpr");
164 /* write floating point registers now. */
165 for ( ii=0; ii < 32; ++ii)
167 ptrace (PT_WRITE_FPR, inferior_pid,
168 (PTRACE_ARG3_TYPE) ®isters[REGISTER_BYTE (FP0_REGNUM+ii)],
172 perror ("ptrace write_fpr");
177 /* write special registers. */
178 for (ii=0; ii <= LAST_SP_REGNUM-FIRST_SP_REGNUM; ++ii)
180 ptrace (PT_WRITE_GPR, inferior_pid,
181 (PTRACE_ARG3_TYPE) special_regs[ii],
182 *(int*)®isters[REGISTER_BYTE (FIRST_SP_REGNUM+ii)], 0);
185 perror ("ptrace write_gpr");
191 /* else, a specific register number is given... */
193 else if (regno < FP0_REGNUM) /* a GPR */
195 ptrace (PT_WRITE_GPR, inferior_pid, (PTRACE_ARG3_TYPE) regno,
196 *(int*)®isters[REGISTER_BYTE (regno)], 0);
199 else if (regno <= FPLAST_REGNUM) /* a FPR */
201 ptrace (PT_WRITE_FPR, inferior_pid,
202 (PTRACE_ARG3_TYPE) ®isters[REGISTER_BYTE (regno)],
203 regno - FP0_REGNUM + FPR0, 0);
206 else if (regno <= LAST_SP_REGNUM) /* a special register */
208 ptrace (PT_WRITE_GPR, inferior_pid,
209 (PTRACE_ARG3_TYPE) special_regs [regno-FIRST_SP_REGNUM],
210 *(int*)®isters[REGISTER_BYTE (regno)], 0);
214 fprintf_unfiltered (gdb_stderr, "Gdb error: register no %d not implemented.\n", regno);
218 perror ("ptrace write");
223 /* Execute one dummy breakpoint instruction. This way we give the kernel
224 a chance to do some housekeeping and update inferior's internal data,
228 exec_one_dummy_insn ()
230 #define DUMMY_INSN_ADDR (TEXT_SEGMENT_BASE)+0x200
232 char shadow_contents[BREAKPOINT_MAX]; /* Stash old bkpt addr contents */
233 unsigned int status, pid;
235 /* We plant one dummy breakpoint into DUMMY_INSN_ADDR address. We assume that
236 this address will never be executed again by the real code. */
238 target_insert_breakpoint (DUMMY_INSN_ADDR, shadow_contents);
241 ptrace (PT_CONTINUE, inferior_pid, (PTRACE_ARG3_TYPE) DUMMY_INSN_ADDR, 0, 0);
243 perror ("pt_continue");
246 pid = wait (&status);
247 } while (pid != inferior_pid);
249 target_remove_breakpoint (DUMMY_INSN_ADDR, shadow_contents);
253 fetch_core_registers (core_reg_sect, core_reg_size, which, reg_addr)
255 unsigned core_reg_size;
257 unsigned int reg_addr; /* Unused in this version */
259 /* fetch GPRs and special registers from the first register section
263 /* copy GPRs first. */
264 memcpy (registers, core_reg_sect, 32 * 4);
266 /* gdb's internal register template and bfd's register section layout
267 should share a common include file. FIXMEmgo */
268 /* then comes special registes. They are supposed to be in the same
269 order in gdb template and bfd `.reg' section. */
270 core_reg_sect += (32 * 4);
271 memcpy (®isters [REGISTER_BYTE (FIRST_SP_REGNUM)], core_reg_sect,
272 (LAST_SP_REGNUM - FIRST_SP_REGNUM + 1) * 4);
275 /* fetch floating point registers from register section 2 in core bfd. */
277 memcpy (®isters [REGISTER_BYTE (FP0_REGNUM)], core_reg_sect, 32 * 8);
280 fprintf_unfiltered (gdb_stderr, "Gdb error: unknown parameter to fetch_core_registers().\n");
283 /* handle symbol translation on vmapping */
287 register struct vmap *vp;
289 register struct objfile *objfile;
290 CORE_ADDR text_delta;
291 CORE_ADDR data_delta;
293 struct section_offsets *new_offsets;
296 objfile = vp->objfile;
299 /* OK, it's not an objfile we opened ourselves.
300 Currently, that can only happen with the exec file, so
301 relocate the symbols for the symfile. */
302 if (symfile_objfile == NULL)
304 objfile = symfile_objfile;
308 (sizeof (struct section_offsets)
309 + sizeof (new_offsets->offsets) * objfile->num_sections);
311 for (i = 0; i < objfile->num_sections; ++i)
312 ANOFFSET (new_offsets, i) = ANOFFSET (objfile->section_offsets, i);
315 vp->tstart - ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT);
316 ANOFFSET (new_offsets, SECT_OFF_TEXT) = vp->tstart;
319 vp->dstart - ANOFFSET (objfile->section_offsets, SECT_OFF_DATA);
320 ANOFFSET (new_offsets, SECT_OFF_DATA) = vp->dstart;
323 vp->dstart - ANOFFSET (objfile->section_offsets, SECT_OFF_BSS);
324 ANOFFSET (new_offsets, SECT_OFF_BSS) = vp->dstart;
326 objfile_relocate (objfile, new_offsets);
329 /* Add symbols for an objfile. */
332 objfile_symbol_add (arg)
335 struct objfile *obj = (struct objfile *) arg;
337 syms_from_objfile (obj, 0, 0, 0);
338 new_symfile_objfile (obj, 0, 0);
342 /* Add a new vmap entry based on ldinfo() information.
344 If ldi->ldinfo_fd is not valid (e.g. this struct ld_info is from a
345 core file), the caller should set it to -1, and we will open the file.
347 Return the vmap new entry. */
351 register struct ld_info *ldi;
354 register char *mem, *objname;
358 /* This ldi structure was allocated using alloca() in
359 xcoff_relocate_symtab(). Now we need to have persistent object
360 and member names, so we should save them. */
362 mem = ldi->ldinfo_filename + strlen (ldi->ldinfo_filename) + 1;
363 mem = savestring (mem, strlen (mem));
364 objname = savestring (ldi->ldinfo_filename, strlen (ldi->ldinfo_filename));
366 if (ldi->ldinfo_fd < 0)
367 /* Note that this opens it once for every member; a possible
368 enhancement would be to only open it once for every object. */
369 abfd = bfd_openr (objname, gnutarget);
371 abfd = bfd_fdopenr (objname, gnutarget, ldi->ldinfo_fd);
373 error ("Could not open `%s' as an executable file: %s",
374 objname, bfd_errmsg (bfd_get_error ()));
376 /* make sure we have an object file */
378 if (bfd_check_format (abfd, bfd_object))
379 vp = map_vmap (abfd, 0);
381 else if (bfd_check_format (abfd, bfd_archive))
384 /* FIXME??? am I tossing BFDs? bfd? */
385 while ((last = bfd_openr_next_archived_file (abfd, last)))
386 if (STREQ (mem, last->filename))
392 /* FIXME -- should be error */
393 warning ("\"%s\": member \"%s\" missing.", abfd->filename, mem);
397 if (!bfd_check_format(last, bfd_object))
399 bfd_close (last); /* XXX??? */
403 vp = map_vmap (last, abfd);
409 error ("\"%s\": not in executable format: %s.",
410 objname, bfd_errmsg (bfd_get_error ()));
413 obj = allocate_objfile (vp->bfd, 0);
416 #ifndef SOLIB_SYMBOLS_MANUAL
417 if (catch_errors (objfile_symbol_add, (char *)obj,
418 "Error while reading shared library symbols:\n",
421 /* Note this is only done if symbol reading was successful. */
429 /* update VMAP info with ldinfo() information
430 Input is ptr to ldinfo() results. */
434 register struct ld_info *ldi;
437 register struct vmap *vp;
438 int got_one, retried;
441 /* For each *ldi, see if we have a corresponding *vp.
442 If so, update the mapping, and symbol table.
443 If not, add an entry and symbol table. */
446 char *name = ldi->ldinfo_filename;
447 char *memb = name + strlen(name) + 1;
451 if (fstat (ldi->ldinfo_fd, &ii) < 0)
452 fatal ("cannot fstat(fd=%d) on %s", ldi->ldinfo_fd, name);
454 for (got_one = 0, vp = vmap; vp; vp = vp->nxt)
456 /* First try to find a `vp', which is the same as in ldinfo.
457 If not the same, just continue and grep the next `vp'. If same,
458 relocate its tstart, tend, dstart, dend values. If no such `vp'
459 found, get out of this for loop, add this ldi entry as a new vmap
460 (add_vmap) and come back, fins its `vp' and so on... */
462 /* The filenames are not always sufficient to match on. */
464 if ((name[0] == '/' && !STREQ(name, vp->name))
465 || (memb[0] && !STREQ(memb, vp->member)))
468 /* See if we are referring to the same file. */
469 if (bfd_stat (vp->bfd, &vi) < 0)
470 /* An error here is innocuous, most likely meaning that
471 the file descriptor has become worthless.
472 FIXME: What does it mean for a file descriptor to become
473 "worthless"? What makes it happen? What error does it
474 produce (ENOENT? others?)? Should we at least provide
478 if (ii.st_dev != vi.st_dev || ii.st_ino != vi.st_ino)
482 close (ldi->ldinfo_fd);
486 /* Found a corresponding VMAP. Remap! */
488 /* We can assume pointer == CORE_ADDR, this code is native only. */
489 vp->tstart = (CORE_ADDR) ldi->ldinfo_textorg;
490 vp->tend = vp->tstart + ldi->ldinfo_textsize;
491 vp->dstart = (CORE_ADDR) ldi->ldinfo_dataorg;
492 vp->dend = vp->dstart + ldi->ldinfo_datasize;
496 vp->tstart += vp->tadj;
497 vp->tend += vp->tadj;
500 /* The objfile is only NULL for the exec file. */
501 if (vp->objfile == NULL)
504 #ifdef DONT_RELOCATE_SYMFILE_OBJFILE
505 if (vp->objfile == symfile_objfile
506 || vp->objfile == NULL)
508 ldi->ldinfo_dataorg = 0;
509 vp->dstart = (CORE_ADDR) 0;
510 vp->dend = ldi->ldinfo_datasize;
514 /* relocate symbol table(s). */
517 /* There may be more, so we don't break out of the loop. */
520 /* if there was no matching *vp, we must perforce create the sucker(s) */
521 if (!got_one && !retried)
527 } while (ldi->ldinfo_next
528 && (ldi = (void *) (ldi->ldinfo_next + (char *) ldi)));
530 /* If we don't find the symfile_objfile anywhere in the ldinfo, it
531 is unlikely that the symbol file is relocated to the proper
532 address. And we might have attached to a process which is
533 running a different copy of the same executable. */
534 if (symfile_objfile != NULL && !got_exec_file)
537 fputs_unfiltered ("Symbol file ", gdb_stderr);
538 fputs_unfiltered (symfile_objfile->name, gdb_stderr);
539 fputs_unfiltered ("\nis not mapped; discarding it.\n\
540 If in fact that file has symbols which the mapped files listed by\n\
541 \"info files\" lack, you can load symbols with the \"symbol-file\" or\n\
542 \"add-symbol-file\" commands (note that you must take care of relocating\n\
543 symbols to the proper address).\n", gdb_stderr);
544 free_objfile (symfile_objfile);
545 symfile_objfile = NULL;
547 breakpoint_re_set ();
550 /* As well as symbol tables, exec_sections need relocation. After
551 the inferior process' termination, there will be a relocated symbol
552 table exist with no corresponding inferior process. At that time, we
553 need to use `exec' bfd, rather than the inferior process's memory space
556 `exec_sections' need to be relocated only once, as long as the exec
557 file remains unchanged.
566 if (execbfd == exec_bfd)
571 if (!vmap || !exec_ops.to_sections)
572 error ("vmap_exec: vmap or exec_ops.to_sections == 0\n");
574 for (i=0; &exec_ops.to_sections[i] < exec_ops.to_sections_end; i++)
576 if (STREQ(".text", exec_ops.to_sections[i].the_bfd_section->name))
578 exec_ops.to_sections[i].addr += vmap->tstart;
579 exec_ops.to_sections[i].endaddr += vmap->tstart;
581 else if (STREQ(".data", exec_ops.to_sections[i].the_bfd_section->name))
583 exec_ops.to_sections[i].addr += vmap->dstart;
584 exec_ops.to_sections[i].endaddr += vmap->dstart;
589 /* xcoff_relocate_symtab - hook for symbol table relocation.
590 also reads shared libraries.. */
593 xcoff_relocate_symtab (pid)
596 #define MAX_LOAD_SEGS 64 /* maximum number of load segments */
600 ldi = (void *) alloca(MAX_LOAD_SEGS * sizeof (*ldi));
602 /* According to my humble theory, AIX has some timing problems and
603 when the user stack grows, kernel doesn't update stack info in time
604 and ptrace calls step on user stack. That is why we sleep here a little,
605 and give kernel to update its internals. */
610 ptrace (PT_LDINFO, pid, (PTRACE_ARG3_TYPE) ldi,
611 MAX_LOAD_SEGS * sizeof(*ldi), ldi);
613 perror_with_name ("ptrace ldinfo");
618 /* We are allowed to assume CORE_ADDR == pointer. This code is
620 add_text_to_loadinfo ((CORE_ADDR) ldi->ldinfo_textorg,
621 (CORE_ADDR) ldi->ldinfo_dataorg);
622 } while (ldi->ldinfo_next
623 && (ldi = (void *) (ldi->ldinfo_next + (char *) ldi)));
626 /* Now that we've jumbled things around, re-sort them. */
627 sort_minimal_symbols ();
630 /* relocate the exec and core sections as well. */
634 /* Core file stuff. */
636 /* Relocate symtabs and read in shared library info, based on symbols
637 from the core file. */
640 xcoff_relocate_core (target)
641 struct target_ops *target;
643 /* Offset of member MEMBER in a struct of type TYPE. */
645 #define offsetof(TYPE, MEMBER) ((int) &((TYPE *)0)->MEMBER)
648 /* Size of a struct ld_info except for the variable-length filename. */
649 #define LDINFO_SIZE (offsetof (struct ld_info, ldinfo_filename))
653 struct ld_info *ldip;
656 /* Allocated size of buffer. */
657 int buffer_size = LDINFO_SIZE;
658 char *buffer = xmalloc (buffer_size);
659 struct cleanup *old = make_cleanup (free_current_contents, &buffer);
661 /* FIXME, this restriction should not exist. For now, though I'll
662 avoid coredumps with error() pending a real fix. */
665 ("Can't debug a core file without an executable file (on the RS/6000)");
667 ldinfo_sec = bfd_get_section_by_name (core_bfd, ".ldinfo");
668 if (ldinfo_sec == NULL)
671 fprintf_filtered (gdb_stderr, "Couldn't get ldinfo from core file: %s\n",
672 bfd_errmsg (bfd_get_error ()));
681 /* Read in everything but the name. */
682 if (bfd_get_section_contents (core_bfd, ldinfo_sec, buffer,
683 offset, LDINFO_SIZE) == 0)
690 if (i == buffer_size)
693 buffer = xrealloc (buffer, buffer_size);
695 if (bfd_get_section_contents (core_bfd, ldinfo_sec, &buffer[i],
698 if (buffer[i++] == '\0')
700 } while (names_found < 2);
702 ldip = (struct ld_info *) buffer;
704 /* Can't use a file descriptor from the core file; need to open it. */
705 ldip->ldinfo_fd = -1;
707 /* The first ldinfo is for the exec file, allocated elsewhere. */
711 vp = add_vmap (ldip);
713 offset += ldip->ldinfo_next;
715 /* We can assume pointer == CORE_ADDR, this code is native only. */
716 vp->tstart = (CORE_ADDR) ldip->ldinfo_textorg;
717 vp->tend = vp->tstart + ldip->ldinfo_textsize;
718 vp->dstart = (CORE_ADDR) ldip->ldinfo_dataorg;
719 vp->dend = vp->dstart + ldip->ldinfo_datasize;
721 #ifdef DONT_RELOCATE_SYMFILE_OBJFILE
724 vp->dstart = (CORE_ADDR) 0;
725 vp->dend = ldip->ldinfo_datasize;
731 vp->tstart += vp->tadj;
732 vp->tend += vp->tadj;
735 /* Unless this is the exec file,
736 add our sections to the section table for the core target. */
740 struct section_table *stp;
742 count = target->to_sections_end - target->to_sections;
744 target->to_sections = (struct section_table *)
745 xrealloc (target->to_sections,
746 sizeof (struct section_table) * count);
747 target->to_sections_end = target->to_sections + count;
748 stp = target->to_sections_end - 2;
750 /* "Why do we add bfd_section_vma?", I hear you cry.
751 Well, the start of the section in the file is actually
752 that far into the section as the struct vmap understands it.
753 So for text sections, bfd_section_vma tends to be 0x200,
754 and if vp->tstart is 0xd0002000, then the first byte of
755 the text section on disk corresponds to address 0xd0002200. */
757 stp->the_bfd_section = bfd_get_section_by_name (stp->bfd, ".text");
758 stp->addr = bfd_section_vma (stp->bfd, stp->the_bfd_section) + vp->tstart;
759 stp->endaddr = bfd_section_vma (stp->bfd, stp->the_bfd_section) + vp->tend;
763 stp->the_bfd_section = bfd_get_section_by_name (stp->bfd, ".data");
764 stp->addr = bfd_section_vma (stp->bfd, stp->the_bfd_section) + vp->dstart;
765 stp->endaddr = bfd_section_vma (stp->bfd, stp->the_bfd_section) + vp->dend;
770 add_text_to_loadinfo ((CORE_ADDR)ldip->ldinfo_textorg,
771 (CORE_ADDR)ldip->ldinfo_dataorg);
772 } while (ldip->ldinfo_next != 0);
774 breakpoint_re_set ();