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