This commit was generated by cvs2svn to track changes on a CVS vendor
[external/binutils.git] / gdb / rs6000-nat.c
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.
4
5    This file is part of GDB.
6
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.
11
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.
16
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,
20    Boston, MA 02111-1307, USA.  */
21
22 #include "defs.h"
23 #include "inferior.h"
24 #include "target.h"
25 #include "gdbcore.h"
26 #include "xcoffsolib.h"
27 #include "symfile.h"
28 #include "objfiles.h"
29 #include "libbfd.h"             /* For bfd_cache_lookup (FIXME) */
30 #include "bfd.h"
31 #include "gdb-stabs.h"
32
33 #include <sys/ptrace.h>
34 #include <sys/reg.h>
35
36 #include <sys/param.h>
37 #include <sys/dir.h>
38 #include <sys/user.h>
39 #include <signal.h>
40 #include <sys/ioctl.h>
41 #include <fcntl.h>
42
43 #include <a.out.h>
44 #include <sys/file.h>
45 #include "gdb_stat.h"
46 #include <sys/core.h>
47 #include <sys/ldr.h>
48
49 extern int errno;
50
51 extern struct vmap *map_vmap PARAMS ((bfd * bf, bfd * arch));
52
53 extern struct target_ops exec_ops;
54
55 static void
56 vmap_exec PARAMS ((void));
57
58 static void
59 vmap_ldinfo PARAMS ((struct ld_info *));
60
61 static struct vmap *
62   add_vmap PARAMS ((struct ld_info *));
63
64 static int
65 objfile_symbol_add PARAMS ((char *));
66
67 static void
68 vmap_symtab PARAMS ((struct vmap *));
69
70 static void
71 fetch_core_registers PARAMS ((char *, unsigned int, int, CORE_ADDR));
72
73 static void
74 exec_one_dummy_insn PARAMS ((void));
75
76 extern void
77 fixup_breakpoints PARAMS ((CORE_ADDR low, CORE_ADDR high, CORE_ADDR delta));
78
79 /* Conversion from gdb-to-system special purpose register numbers.. */
80
81 static int special_regs[] =
82 {
83   IAR,                          /* PC_REGNUM    */
84   MSR,                          /* PS_REGNUM    */
85   CR,                           /* CR_REGNUM    */
86   LR,                           /* LR_REGNUM    */
87   CTR,                          /* CTR_REGNUM   */
88   XER,                          /* XER_REGNUM   */
89   MQ                            /* MQ_REGNUM    */
90 };
91
92 void
93 fetch_inferior_registers (regno)
94      int regno;
95 {
96   int ii;
97
98   if (regno < 0)
99     {                           /* for all registers */
100
101       /* read 32 general purpose registers. */
102
103       for (ii = 0; ii < 32; ++ii)
104         *(int *) &registers[REGISTER_BYTE (ii)] =
105           ptrace (PT_READ_GPR, inferior_pid, (PTRACE_ARG3_TYPE) ii, 0, 0);
106
107       /* read general purpose floating point registers. */
108
109       for (ii = 0; ii < 32; ++ii)
110         ptrace (PT_READ_FPR, inferior_pid,
111             (PTRACE_ARG3_TYPE) & registers[REGISTER_BYTE (FP0_REGNUM + ii)],
112                 FPR0 + ii, 0);
113
114       /* read special registers. */
115       for (ii = 0; ii <= LAST_UISA_SP_REGNUM - FIRST_UISA_SP_REGNUM; ++ii)
116         *(int *) &registers[REGISTER_BYTE (FIRST_UISA_SP_REGNUM + ii)] =
117           ptrace (PT_READ_GPR, inferior_pid, (PTRACE_ARG3_TYPE) special_regs[ii],
118                   0, 0);
119
120       registers_fetched ();
121       return;
122     }
123
124   /* else an individual register is addressed. */
125
126   else if (regno < FP0_REGNUM)
127     {                           /* a GPR */
128       *(int *) &registers[REGISTER_BYTE (regno)] =
129         ptrace (PT_READ_GPR, inferior_pid, (PTRACE_ARG3_TYPE) regno, 0, 0);
130     }
131   else if (regno <= FPLAST_REGNUM)
132     {                           /* a FPR */
133       ptrace (PT_READ_FPR, inferior_pid,
134               (PTRACE_ARG3_TYPE) & registers[REGISTER_BYTE (regno)],
135               (regno - FP0_REGNUM + FPR0), 0);
136     }
137   else if (regno <= LAST_UISA_SP_REGNUM)
138     {                           /* a special register */
139       *(int *) &registers[REGISTER_BYTE (regno)] =
140         ptrace (PT_READ_GPR, inferior_pid,
141               (PTRACE_ARG3_TYPE) special_regs[regno - FIRST_UISA_SP_REGNUM],
142                 0, 0);
143     }
144   else
145     fprintf_unfiltered (gdb_stderr,
146                         "gdb error: register no %d not implemented.\n",
147                         regno);
148
149   register_valid[regno] = 1;
150 }
151
152 /* Store our register values back into the inferior.
153    If REGNO is -1, do this for all registers.
154    Otherwise, REGNO specifies which register (so we can save time).  */
155
156 void
157 store_inferior_registers (regno)
158      int regno;
159 {
160
161   errno = 0;
162
163   if (regno == -1)
164     {                           /* for all registers..  */
165       int ii;
166
167       /* execute one dummy instruction (which is a breakpoint) in inferior
168          process. So give kernel a chance to do internal house keeping.
169          Otherwise the following ptrace(2) calls will mess up user stack
170          since kernel will get confused about the bottom of the stack (%sp) */
171
172       exec_one_dummy_insn ();
173
174       /* write general purpose registers first! */
175       for (ii = GPR0; ii <= GPR31; ++ii)
176         {
177           ptrace (PT_WRITE_GPR, inferior_pid, (PTRACE_ARG3_TYPE) ii,
178                   *(int *) &registers[REGISTER_BYTE (ii)], 0);
179           if (errno)
180             {
181               perror ("ptrace write_gpr");
182               errno = 0;
183             }
184         }
185
186       /* write floating point registers now. */
187       for (ii = 0; ii < 32; ++ii)
188         {
189           ptrace (PT_WRITE_FPR, inferior_pid,
190             (PTRACE_ARG3_TYPE) & registers[REGISTER_BYTE (FP0_REGNUM + ii)],
191                   FPR0 + ii, 0);
192           if (errno)
193             {
194               perror ("ptrace write_fpr");
195               errno = 0;
196             }
197         }
198
199       /* write special registers. */
200       for (ii = 0; ii <= LAST_UISA_SP_REGNUM - FIRST_UISA_SP_REGNUM; ++ii)
201         {
202           ptrace (PT_WRITE_GPR, inferior_pid,
203                   (PTRACE_ARG3_TYPE) special_regs[ii],
204              *(int *) &registers[REGISTER_BYTE (FIRST_UISA_SP_REGNUM + ii)],
205                   0);
206           if (errno)
207             {
208               perror ("ptrace write_gpr");
209               errno = 0;
210             }
211         }
212     }
213
214   /* else, a specific register number is given... */
215
216   else if (regno < FP0_REGNUM)  /* a GPR */
217     {
218       ptrace (PT_WRITE_GPR, inferior_pid, (PTRACE_ARG3_TYPE) regno,
219               *(int *) &registers[REGISTER_BYTE (regno)], 0);
220     }
221
222   else if (regno <= FPLAST_REGNUM)      /* a FPR */
223     {
224       ptrace (PT_WRITE_FPR, inferior_pid,
225               (PTRACE_ARG3_TYPE) & registers[REGISTER_BYTE (regno)],
226               regno - FP0_REGNUM + FPR0, 0);
227     }
228
229   else if (regno <= LAST_UISA_SP_REGNUM)        /* a special register */
230     {
231       ptrace (PT_WRITE_GPR, inferior_pid,
232               (PTRACE_ARG3_TYPE) special_regs[regno - FIRST_UISA_SP_REGNUM],
233               *(int *) &registers[REGISTER_BYTE (regno)], 0);
234     }
235
236   else
237     fprintf_unfiltered (gdb_stderr,
238                         "Gdb error: register no %d not implemented.\n",
239                         regno);
240
241   if (errno)
242     {
243       perror ("ptrace write");
244       errno = 0;
245     }
246 }
247
248 /* Execute one dummy breakpoint instruction.  This way we give the kernel
249    a chance to do some housekeeping and update inferior's internal data,
250    including u_area. */
251
252 static void
253 exec_one_dummy_insn ()
254 {
255 #define DUMMY_INSN_ADDR (TEXT_SEGMENT_BASE)+0x200
256
257   char shadow_contents[BREAKPOINT_MAX];         /* Stash old bkpt addr contents */
258   int status, pid;
259   CORE_ADDR prev_pc;
260
261   /* We plant one dummy breakpoint into DUMMY_INSN_ADDR address. We
262      assume that this address will never be executed again by the real
263      code. */
264
265   target_insert_breakpoint (DUMMY_INSN_ADDR, shadow_contents);
266
267   errno = 0;
268
269   /* You might think this could be done with a single ptrace call, and
270      you'd be correct for just about every platform I've ever worked
271      on.  However, rs6000-ibm-aix4.1.3 seems to have screwed this up --
272      the inferior never hits the breakpoint (it's also worth noting
273      powerpc-ibm-aix4.1.3 works correctly).  */
274   prev_pc = read_pc ();
275   write_pc (DUMMY_INSN_ADDR);
276   ptrace (PT_CONTINUE, inferior_pid, (PTRACE_ARG3_TYPE) 1, 0, 0);
277
278   if (errno)
279     perror ("pt_continue");
280
281   do
282     {
283       pid = wait (&status);
284     }
285   while (pid != inferior_pid);
286
287   write_pc (prev_pc);
288   target_remove_breakpoint (DUMMY_INSN_ADDR, shadow_contents);
289 }
290
291 static void
292 fetch_core_registers (core_reg_sect, core_reg_size, which, reg_addr)
293      char *core_reg_sect;
294      unsigned core_reg_size;
295      int which;
296      CORE_ADDR reg_addr;        /* Unused in this version */
297 {
298   /* fetch GPRs and special registers from the first register section
299      in core bfd. */
300   if (which == 0)
301     {
302       /* copy GPRs first. */
303       memcpy (registers, core_reg_sect, 32 * 4);
304
305       /* gdb's internal register template and bfd's register section layout
306          should share a common include file. FIXMEmgo */
307       /* then comes special registes. They are supposed to be in the same
308          order in gdb template and bfd `.reg' section. */
309       core_reg_sect += (32 * 4);
310       memcpy (&registers[REGISTER_BYTE (FIRST_UISA_SP_REGNUM)],
311               core_reg_sect,
312               (LAST_UISA_SP_REGNUM - FIRST_UISA_SP_REGNUM + 1) * 4);
313     }
314
315   /* fetch floating point registers from register section 2 in core bfd. */
316   else if (which == 2)
317     memcpy (&registers[REGISTER_BYTE (FP0_REGNUM)], core_reg_sect, 32 * 8);
318
319   else
320     fprintf_unfiltered
321       (gdb_stderr,
322        "Gdb error: unknown parameter to fetch_core_registers().\n");
323 }
324 \f
325 /* handle symbol translation on vmapping */
326
327 static void
328 vmap_symtab (vp)
329      register struct vmap *vp;
330 {
331   register struct objfile *objfile;
332   struct section_offsets *new_offsets;
333   int i;
334
335   objfile = vp->objfile;
336   if (objfile == NULL)
337     {
338       /* OK, it's not an objfile we opened ourselves.
339          Currently, that can only happen with the exec file, so
340          relocate the symbols for the symfile.  */
341       if (symfile_objfile == NULL)
342         return;
343       objfile = symfile_objfile;
344     }
345
346   new_offsets = alloca
347     (sizeof (struct section_offsets)
348      + sizeof (new_offsets->offsets) * objfile->num_sections);
349
350   for (i = 0; i < objfile->num_sections; ++i)
351     ANOFFSET (new_offsets, i) = ANOFFSET (objfile->section_offsets, i);
352
353   /* The symbols in the object file are linked to the VMA of the section,
354      relocate them VMA relative.  */
355   ANOFFSET (new_offsets, SECT_OFF_TEXT) = vp->tstart - vp->tvma;
356   ANOFFSET (new_offsets, SECT_OFF_DATA) = vp->dstart - vp->dvma;
357   ANOFFSET (new_offsets, SECT_OFF_BSS) = vp->dstart - vp->dvma;
358
359   objfile_relocate (objfile, new_offsets);
360 }
361 \f
362 /* Add symbols for an objfile.  */
363
364 static int
365 objfile_symbol_add (arg)
366      char *arg;
367 {
368   struct objfile *obj = (struct objfile *) arg;
369
370   syms_from_objfile (obj, 0, 0, 0);
371   new_symfile_objfile (obj, 0, 0);
372   return 1;
373 }
374
375 /* Add a new vmap entry based on ldinfo() information.
376
377    If ldi->ldinfo_fd is not valid (e.g. this struct ld_info is from a
378    core file), the caller should set it to -1, and we will open the file.
379
380    Return the vmap new entry.  */
381
382 static struct vmap *
383 add_vmap (ldi)
384      register struct ld_info *ldi;
385 {
386   bfd *abfd, *last;
387   register char *mem, *objname;
388   struct objfile *obj;
389   struct vmap *vp;
390
391   /* This ldi structure was allocated using alloca() in 
392      xcoff_relocate_symtab(). Now we need to have persistent object 
393      and member names, so we should save them. */
394
395   mem = ldi->ldinfo_filename + strlen (ldi->ldinfo_filename) + 1;
396   mem = savestring (mem, strlen (mem));
397   objname = savestring (ldi->ldinfo_filename, strlen (ldi->ldinfo_filename));
398
399   if (ldi->ldinfo_fd < 0)
400     /* Note that this opens it once for every member; a possible
401        enhancement would be to only open it once for every object.  */
402     abfd = bfd_openr (objname, gnutarget);
403   else
404     abfd = bfd_fdopenr (objname, gnutarget, ldi->ldinfo_fd);
405   if (!abfd)
406     error ("Could not open `%s' as an executable file: %s",
407            objname, bfd_errmsg (bfd_get_error ()));
408
409   /* make sure we have an object file */
410
411   if (bfd_check_format (abfd, bfd_object))
412     vp = map_vmap (abfd, 0);
413
414   else if (bfd_check_format (abfd, bfd_archive))
415     {
416       last = 0;
417       /* FIXME??? am I tossing BFDs?  bfd? */
418       while ((last = bfd_openr_next_archived_file (abfd, last)))
419         if (STREQ (mem, last->filename))
420           break;
421
422       if (!last)
423         {
424           bfd_close (abfd);
425           /* FIXME -- should be error */
426           warning ("\"%s\": member \"%s\" missing.", abfd->filename, mem);
427           return 0;
428         }
429
430       if (!bfd_check_format (last, bfd_object))
431         {
432           bfd_close (last);     /* XXX???       */
433           goto obj_err;
434         }
435
436       vp = map_vmap (last, abfd);
437     }
438   else
439     {
440     obj_err:
441       bfd_close (abfd);
442       error ("\"%s\": not in executable format: %s.",
443              objname, bfd_errmsg (bfd_get_error ()));
444       /*NOTREACHED */
445     }
446   obj = allocate_objfile (vp->bfd, 0, 0, 0);
447   vp->objfile = obj;
448
449 #ifndef SOLIB_SYMBOLS_MANUAL
450   if (catch_errors (objfile_symbol_add, (char *) obj,
451                     "Error while reading shared library symbols:\n",
452                     RETURN_MASK_ALL))
453     {
454       /* Note this is only done if symbol reading was successful.  */
455       vmap_symtab (vp);
456       vp->loaded = 1;
457     }
458 #endif
459   return vp;
460 }
461 \f
462 /* update VMAP info with ldinfo() information
463    Input is ptr to ldinfo() results.  */
464
465 static void
466 vmap_ldinfo (ldi)
467      register struct ld_info *ldi;
468 {
469   struct stat ii, vi;
470   register struct vmap *vp;
471   int got_one, retried;
472   int got_exec_file = 0;
473
474   /* For each *ldi, see if we have a corresponding *vp.
475      If so, update the mapping, and symbol table.
476      If not, add an entry and symbol table.  */
477
478   do
479     {
480       char *name = ldi->ldinfo_filename;
481       char *memb = name + strlen (name) + 1;
482
483       retried = 0;
484
485       if (fstat (ldi->ldinfo_fd, &ii) < 0)
486         {
487           /* The kernel sets ld_info to -1, if the process is still using the
488              object, and the object is removed. Keep the symbol info for the
489              removed object and issue a warning.  */
490           warning ("%s (fd=%d) has disappeared, keeping its symbols",
491                    name, ldi->ldinfo_fd);
492           continue;
493         }
494     retry:
495       for (got_one = 0, vp = vmap; vp; vp = vp->nxt)
496         {
497           struct objfile *objfile;
498
499           /* First try to find a `vp', which is the same as in ldinfo.
500              If not the same, just continue and grep the next `vp'. If same,
501              relocate its tstart, tend, dstart, dend values. If no such `vp'
502              found, get out of this for loop, add this ldi entry as a new vmap
503              (add_vmap) and come back, find its `vp' and so on... */
504
505           /* The filenames are not always sufficient to match on. */
506
507           if ((name[0] == '/' && !STREQ (name, vp->name))
508               || (memb[0] && !STREQ (memb, vp->member)))
509             continue;
510
511           /* See if we are referring to the same file.
512              We have to check objfile->obfd, symfile.c:reread_symbols might
513              have updated the obfd after a change.  */
514           objfile = vp->objfile == NULL ? symfile_objfile : vp->objfile;
515           if (objfile == NULL
516               || objfile->obfd == NULL
517               || bfd_stat (objfile->obfd, &vi) < 0)
518             {
519               warning ("Unable to stat %s, keeping its symbols", name);
520               continue;
521             }
522
523           if (ii.st_dev != vi.st_dev || ii.st_ino != vi.st_ino)
524             continue;
525
526           if (!retried)
527             close (ldi->ldinfo_fd);
528
529           ++got_one;
530
531           /* Found a corresponding VMAP.  Remap!  */
532
533           /* We can assume pointer == CORE_ADDR, this code is native only.  */
534           vp->tstart = (CORE_ADDR) ldi->ldinfo_textorg;
535           vp->tend = vp->tstart + ldi->ldinfo_textsize;
536           vp->dstart = (CORE_ADDR) ldi->ldinfo_dataorg;
537           vp->dend = vp->dstart + ldi->ldinfo_datasize;
538
539           /* The run time loader maps the file header in addition to the text
540              section and returns a pointer to the header in ldinfo_textorg.
541              Adjust the text start address to point to the real start address
542              of the text section.  */
543           vp->tstart += vp->toffs;
544
545           /* The objfile is only NULL for the exec file.  */
546           if (vp->objfile == NULL)
547             got_exec_file = 1;
548
549           /* relocate symbol table(s). */
550           vmap_symtab (vp);
551
552           /* There may be more, so we don't break out of the loop.  */
553         }
554
555       /* if there was no matching *vp, we must perforce create the sucker(s) */
556       if (!got_one && !retried)
557         {
558           add_vmap (ldi);
559           ++retried;
560           goto retry;
561         }
562     }
563   while (ldi->ldinfo_next
564          && (ldi = (void *) (ldi->ldinfo_next + (char *) ldi)));
565
566   /* If we don't find the symfile_objfile anywhere in the ldinfo, it
567      is unlikely that the symbol file is relocated to the proper
568      address.  And we might have attached to a process which is
569      running a different copy of the same executable.  */
570   if (symfile_objfile != NULL && !got_exec_file)
571     {
572       warning_begin ();
573       fputs_unfiltered ("Symbol file ", gdb_stderr);
574       fputs_unfiltered (symfile_objfile->name, gdb_stderr);
575       fputs_unfiltered ("\nis not mapped; discarding it.\n\
576 If in fact that file has symbols which the mapped files listed by\n\
577 \"info files\" lack, you can load symbols with the \"symbol-file\" or\n\
578 \"add-symbol-file\" commands (note that you must take care of relocating\n\
579 symbols to the proper address).\n", gdb_stderr);
580       free_objfile (symfile_objfile);
581       symfile_objfile = NULL;
582     }
583   breakpoint_re_set ();
584 }
585 \f
586 /* As well as symbol tables, exec_sections need relocation. After
587    the inferior process' termination, there will be a relocated symbol
588    table exist with no corresponding inferior process. At that time, we
589    need to use `exec' bfd, rather than the inferior process's memory space
590    to look up symbols.
591
592    `exec_sections' need to be relocated only once, as long as the exec
593    file remains unchanged.
594  */
595
596 static void
597 vmap_exec ()
598 {
599   static bfd *execbfd;
600   int i;
601
602   if (execbfd == exec_bfd)
603     return;
604
605   execbfd = exec_bfd;
606
607   if (!vmap || !exec_ops.to_sections)
608     error ("vmap_exec: vmap or exec_ops.to_sections == 0\n");
609
610   for (i = 0; &exec_ops.to_sections[i] < exec_ops.to_sections_end; i++)
611     {
612       if (STREQ (".text", exec_ops.to_sections[i].the_bfd_section->name))
613         {
614           exec_ops.to_sections[i].addr += vmap->tstart - vmap->tvma;
615           exec_ops.to_sections[i].endaddr += vmap->tstart - vmap->tvma;
616         }
617       else if (STREQ (".data", exec_ops.to_sections[i].the_bfd_section->name))
618         {
619           exec_ops.to_sections[i].addr += vmap->dstart - vmap->dvma;
620           exec_ops.to_sections[i].endaddr += vmap->dstart - vmap->dvma;
621         }
622       else if (STREQ (".bss", exec_ops.to_sections[i].the_bfd_section->name))
623         {
624           exec_ops.to_sections[i].addr += vmap->dstart - vmap->dvma;
625           exec_ops.to_sections[i].endaddr += vmap->dstart - vmap->dvma;
626         }
627     }
628 }
629 \f
630 /* xcoff_relocate_symtab -      hook for symbol table relocation.
631    also reads shared libraries.. */
632
633 void
634 xcoff_relocate_symtab (pid)
635      unsigned int pid;
636 {
637 #define MAX_LOAD_SEGS 64        /* maximum number of load segments */
638
639   struct ld_info *ldi;
640
641   ldi = (void *) alloca (MAX_LOAD_SEGS * sizeof (*ldi));
642
643   /* According to my humble theory, AIX has some timing problems and
644      when the user stack grows, kernel doesn't update stack info in time
645      and ptrace calls step on user stack. That is why we sleep here a little,
646      and give kernel to update its internals. */
647
648   usleep (36000);
649
650   errno = 0;
651   ptrace (PT_LDINFO, pid, (PTRACE_ARG3_TYPE) ldi,
652           MAX_LOAD_SEGS * sizeof (*ldi), (int *) ldi);
653   if (errno)
654     perror_with_name ("ptrace ldinfo");
655
656   vmap_ldinfo (ldi);
657
658   /* relocate the exec and core sections as well. */
659   vmap_exec ();
660 }
661 \f
662 /* Core file stuff.  */
663
664 /* Relocate symtabs and read in shared library info, based on symbols
665    from the core file.  */
666
667 void
668 xcoff_relocate_core (target)
669      struct target_ops *target;
670 {
671 /* Offset of member MEMBER in a struct of type TYPE.  */
672 #ifndef offsetof
673 #define offsetof(TYPE, MEMBER) ((int) &((TYPE *)0)->MEMBER)
674 #endif
675
676 /* Size of a struct ld_info except for the variable-length filename.  */
677 #define LDINFO_SIZE (offsetof (struct ld_info, ldinfo_filename))
678
679   sec_ptr ldinfo_sec;
680   int offset = 0;
681   struct ld_info *ldip;
682   struct vmap *vp;
683
684   /* Allocated size of buffer.  */
685   int buffer_size = LDINFO_SIZE;
686   char *buffer = xmalloc (buffer_size);
687   struct cleanup *old = make_cleanup (free_current_contents, &buffer);
688
689   /* FIXME, this restriction should not exist.  For now, though I'll
690      avoid coredumps with error() pending a real fix.  */
691   if (vmap == NULL)
692     error
693       ("Can't debug a core file without an executable file (on the RS/6000)");
694
695   ldinfo_sec = bfd_get_section_by_name (core_bfd, ".ldinfo");
696   if (ldinfo_sec == NULL)
697     {
698     bfd_err:
699       fprintf_filtered (gdb_stderr, "Couldn't get ldinfo from core file: %s\n",
700                         bfd_errmsg (bfd_get_error ()));
701       do_cleanups (old);
702       return;
703     }
704   do
705     {
706       int i;
707       int names_found = 0;
708
709       /* Read in everything but the name.  */
710       if (bfd_get_section_contents (core_bfd, ldinfo_sec, buffer,
711                                     offset, LDINFO_SIZE) == 0)
712         goto bfd_err;
713
714       /* Now the name.  */
715       i = LDINFO_SIZE;
716       do
717         {
718           if (i == buffer_size)
719             {
720               buffer_size *= 2;
721               buffer = xrealloc (buffer, buffer_size);
722             }
723           if (bfd_get_section_contents (core_bfd, ldinfo_sec, &buffer[i],
724                                         offset + i, 1) == 0)
725             goto bfd_err;
726           if (buffer[i++] == '\0')
727             ++names_found;
728         }
729       while (names_found < 2);
730
731       ldip = (struct ld_info *) buffer;
732
733       /* Can't use a file descriptor from the core file; need to open it.  */
734       ldip->ldinfo_fd = -1;
735
736       /* The first ldinfo is for the exec file, allocated elsewhere.  */
737       if (offset == 0)
738         vp = vmap;
739       else
740         vp = add_vmap (ldip);
741
742       offset += ldip->ldinfo_next;
743
744       /* We can assume pointer == CORE_ADDR, this code is native only.  */
745       vp->tstart = (CORE_ADDR) ldip->ldinfo_textorg;
746       vp->tend = vp->tstart + ldip->ldinfo_textsize;
747       vp->dstart = (CORE_ADDR) ldip->ldinfo_dataorg;
748       vp->dend = vp->dstart + ldip->ldinfo_datasize;
749
750       /* The run time loader maps the file header in addition to the text
751          section and returns a pointer to the header in ldinfo_textorg.
752          Adjust the text start address to point to the real start address
753          of the text section.  */
754       vp->tstart += vp->toffs;
755
756       /* Unless this is the exec file,
757          add our sections to the section table for the core target.  */
758       if (vp != vmap)
759         {
760           int count;
761           struct section_table *stp;
762           int update_coreops;
763
764           /* We must update the to_sections field in the core_ops structure
765              now to avoid dangling pointer dereferences.  */
766           update_coreops = core_ops.to_sections == target->to_sections;
767
768           count = target->to_sections_end - target->to_sections;
769           count += 2;
770           target->to_sections = (struct section_table *)
771             xrealloc (target->to_sections,
772                       sizeof (struct section_table) * count);
773           target->to_sections_end = target->to_sections + count;
774
775           /* Update the to_sections field in the core_ops structure
776              if needed.  */
777           if (update_coreops)
778             {
779               core_ops.to_sections = target->to_sections;
780               core_ops.to_sections_end = target->to_sections_end;
781             }
782           stp = target->to_sections_end - 2;
783
784           stp->bfd = vp->bfd;
785           stp->the_bfd_section = bfd_get_section_by_name (stp->bfd, ".text");
786           stp->addr = vp->tstart;
787           stp->endaddr = vp->tend;
788           stp++;
789
790           stp->bfd = vp->bfd;
791           stp->the_bfd_section = bfd_get_section_by_name (stp->bfd, ".data");
792           stp->addr = vp->dstart;
793           stp->endaddr = vp->dend;
794         }
795
796       vmap_symtab (vp);
797     }
798   while (ldip->ldinfo_next != 0);
799   vmap_exec ();
800   breakpoint_re_set ();
801   do_cleanups (old);
802 }
803
804 int
805 kernel_u_size ()
806 {
807   return (sizeof (struct user));
808 }
809 \f
810 /* Under AIX, we have to pass the correct TOC pointer to a function
811    when calling functions in the inferior.
812    We try to find the relative toc offset of the objfile containing PC
813    and add the current load address of the data segment from the vmap.  */
814
815 static CORE_ADDR
816 find_toc_address (pc)
817      CORE_ADDR pc;
818 {
819   struct vmap *vp;
820
821   for (vp = vmap; vp; vp = vp->nxt)
822     {
823       if (pc >= vp->tstart && pc < vp->tend)
824         {
825           /* vp->objfile is only NULL for the exec file.  */
826           return vp->dstart + get_toc_offset (vp->objfile == NULL
827                                               ? symfile_objfile
828                                               : vp->objfile);
829         }
830     }
831   error ("Unable to find TOC entry for pc 0x%x\n", pc);
832 }
833 \f
834 /* Register that we are able to handle rs6000 core file formats. */
835
836 static struct core_fns rs6000_core_fns =
837 {
838   bfd_target_coff_flavour,
839   fetch_core_registers,
840   NULL
841 };
842
843 void
844 _initialize_core_rs6000 ()
845 {
846   /* Initialize hook in rs6000-tdep.c for determining the TOC address when
847      calling functions in the inferior.  */
848   find_toc_address_hook = &find_toc_address;
849
850   /* For native configurations, where this module is included, inform
851      the xcoffsolib module where it can find the function for symbol table
852      relocation at runtime. */
853   xcoff_relocate_symtab_hook = &xcoff_relocate_symtab;
854   add_core_fns (&rs6000_core_fns);
855 }