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