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