Eliminate FIRST_UISA_SP_REGNUM and LAST_UISA_SP_REGNUM.
[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, 1993, 1994, 1995, 1996, 1997,
3 1998, 1999, 2000, 2001, 2002
4 Free Software Foundation, Inc.
5
6 This file is part of GDB.
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
22
23 #include "defs.h"
24 #include "inferior.h"
25 #include "target.h"
26 #include "gdbcore.h"
27 #include "xcoffsolib.h"
28 #include "symfile.h"
29 #include "objfiles.h"
30 #include "libbfd.h" /* For bfd_cache_lookup (FIXME) */
31 #include "bfd.h"
32 #include "gdb-stabs.h"
33 #include "regcache.h"
34 #include "arch-utils.h"
35 #include "language.h" /* for local_hex_string(). */
36 #include "ppc-tdep.h"
37
38 #include <sys/ptrace.h>
39 #include <sys/reg.h>
40
41 #include <sys/param.h>
42 #include <sys/dir.h>
43 #include <sys/user.h>
44 #include <signal.h>
45 #include <sys/ioctl.h>
46 #include <fcntl.h>
47 #include <errno.h>
48
49 #include <a.out.h>
50 #include <sys/file.h>
51 #include "gdb_stat.h"
52 #include <sys/core.h>
53 #define __LDINFO_PTRACE32__ /* for __ld_info32 */
54 #define __LDINFO_PTRACE64__ /* for __ld_info64 */
55 #include <sys/ldr.h>
56 #include <sys/systemcfg.h>
57
58 /* On AIX4.3+, sys/ldr.h provides different versions of struct ld_info for
59 debugging 32-bit and 64-bit processes. Define a typedef and macros for
60 accessing fields in the appropriate structures. */
61
62 /* In 32-bit compilation mode (which is the only mode from which ptrace()
63 works on 4.3), __ld_info32 is #defined as equivalent to ld_info. */
64
65 #ifdef __ld_info32
66 # define ARCH3264
67 #endif
68
69 /* Return whether the current architecture is 64-bit. */
70
71 #ifndef ARCH3264
72 # define ARCH64() 0
73 #else
74 # define ARCH64() (REGISTER_RAW_SIZE (0) == 8)
75 #endif
76
77 /* Union of 32-bit and 64-bit ".reg" core file sections. */
78
79 typedef union {
80 #ifdef ARCH3264
81 struct __context64 r64;
82 #else
83 struct mstsave r64;
84 #endif
85 struct mstsave r32;
86 } CoreRegs;
87
88 /* Union of 32-bit and 64-bit versions of ld_info. */
89
90 typedef union {
91 #ifndef ARCH3264
92 struct ld_info l32;
93 struct ld_info l64;
94 #else
95 struct __ld_info32 l32;
96 struct __ld_info64 l64;
97 #endif
98 } LdInfo;
99
100 /* If compiling with 32-bit and 64-bit debugging capability (e.g. AIX 4.x),
101 declare and initialize a variable named VAR suitable for use as the arch64
102 parameter to the various LDI_*() macros. */
103
104 #ifndef ARCH3264
105 # define ARCH64_DECL(var)
106 #else
107 # define ARCH64_DECL(var) int var = ARCH64 ()
108 #endif
109
110 /* Return LDI's FIELD for a 64-bit process if ARCH64 and for a 32-bit process
111 otherwise. This technique only works for FIELDs with the same data type in
112 32-bit and 64-bit versions of ld_info. */
113
114 #ifndef ARCH3264
115 # define LDI_FIELD(ldi, arch64, field) (ldi)->l32.ldinfo_##field
116 #else
117 # define LDI_FIELD(ldi, arch64, field) \
118 (arch64 ? (ldi)->l64.ldinfo_##field : (ldi)->l32.ldinfo_##field)
119 #endif
120
121 /* Return various LDI fields for a 64-bit process if ARCH64 and for a 32-bit
122 process otherwise. */
123
124 #define LDI_NEXT(ldi, arch64) LDI_FIELD(ldi, arch64, next)
125 #define LDI_FD(ldi, arch64) LDI_FIELD(ldi, arch64, fd)
126 #define LDI_FILENAME(ldi, arch64) LDI_FIELD(ldi, arch64, filename)
127
128 extern struct vmap *map_vmap (bfd * bf, bfd * arch);
129
130 extern struct target_ops exec_ops;
131
132 static void vmap_exec (void);
133
134 static void vmap_ldinfo (LdInfo *);
135
136 static struct vmap *add_vmap (LdInfo *);
137
138 static int objfile_symbol_add (void *);
139
140 static void vmap_symtab (struct vmap *);
141
142 static void fetch_core_registers (char *, unsigned int, int, CORE_ADDR);
143
144 static void exec_one_dummy_insn (void);
145
146 extern void
147 fixup_breakpoints (CORE_ADDR low, CORE_ADDR high, CORE_ADDR delta);
148
149 /* Given REGNO, a gdb register number, return the corresponding
150 number suitable for use as a ptrace() parameter. Return -1 if
151 there's no suitable mapping. Also, set the int pointed to by
152 ISFLOAT to indicate whether REGNO is a floating point register. */
153
154 static int
155 regmap (int regno, int *isfloat)
156 {
157 struct gdbarch_tdep *tdep = gdbarch_tdep (current_gdbarch);
158
159 *isfloat = 0;
160 if (tdep->ppc_gp0_regnum <= regno && regno <= tdep->ppc_gplast_regnum)
161 return regno;
162 else if (FP0_REGNUM <= regno && regno <= FPLAST_REGNUM)
163 {
164 *isfloat = 1;
165 return regno - FP0_REGNUM + FPR0;
166 }
167 else if (regno == PC_REGNUM)
168 return IAR;
169 else if (regno == tdep->ppc_ps_regnum)
170 return MSR;
171 else if (regno == tdep->ppc_cr_regnum)
172 return CR;
173 else if (regno == tdep->ppc_lr_regnum)
174 return LR;
175 else if (regno == tdep->ppc_ctr_regnum)
176 return CTR;
177 else if (regno == tdep->ppc_xer_regnum)
178 return XER;
179 else if (tdep->ppc_mq_regnum >= 0 && regno == tdep->ppc_mq_regnum)
180 return MQ;
181 else
182 return -1;
183 }
184
185 /* Call ptrace(REQ, ID, ADDR, DATA, BUF). */
186
187 static int
188 rs6000_ptrace32 (int req, int id, int *addr, int data, int *buf)
189 {
190 int ret = ptrace (req, id, (int *)addr, data, buf);
191 #if 0
192 printf ("rs6000_ptrace32 (%d, %d, 0x%x, %08x, 0x%x) = 0x%x\n",
193 req, id, (unsigned int)addr, data, (unsigned int)buf, ret);
194 #endif
195 return ret;
196 }
197
198 /* Call ptracex(REQ, ID, ADDR, DATA, BUF). */
199
200 static int
201 rs6000_ptrace64 (int req, int id, long long addr, int data, int *buf)
202 {
203 #ifdef ARCH3264
204 int ret = ptracex (req, id, addr, data, buf);
205 #else
206 int ret = 0;
207 #endif
208 #if 0
209 printf ("rs6000_ptrace64 (%d, %d, 0x%llx, %08x, 0x%x) = 0x%x\n",
210 req, id, addr, data, (unsigned int)buf, ret);
211 #endif
212 return ret;
213 }
214
215 /* Fetch register REGNO from the inferior. */
216
217 static void
218 fetch_register (int regno)
219 {
220 int *addr = alloca (MAX_REGISTER_RAW_SIZE);
221 int nr, isfloat;
222
223 /* Retrieved values may be -1, so infer errors from errno. */
224 errno = 0;
225
226 nr = regmap (regno, &isfloat);
227
228 /* Floating-point registers. */
229 if (isfloat)
230 rs6000_ptrace32 (PT_READ_FPR, PIDGET (inferior_ptid), addr, nr, 0);
231
232 /* Bogus register number. */
233 else if (nr < 0)
234 {
235 if (regno >= NUM_REGS)
236 fprintf_unfiltered (gdb_stderr,
237 "gdb error: register no %d not implemented.\n",
238 regno);
239 return;
240 }
241
242 /* Fixed-point registers. */
243 else
244 {
245 if (!ARCH64 ())
246 *addr = rs6000_ptrace32 (PT_READ_GPR, PIDGET (inferior_ptid), (int *)nr, 0, 0);
247 else
248 {
249 /* PT_READ_GPR requires the buffer parameter to point to long long,
250 even if the register is really only 32 bits. */
251 long long buf;
252 rs6000_ptrace64 (PT_READ_GPR, PIDGET (inferior_ptid), nr, 0, (int *)&buf);
253 if (REGISTER_RAW_SIZE (regno) == 8)
254 memcpy (addr, &buf, 8);
255 else
256 *addr = buf;
257 }
258 }
259
260 if (!errno)
261 supply_register (regno, (char *) addr);
262 else
263 {
264 #if 0
265 /* FIXME: this happens 3 times at the start of each 64-bit program. */
266 perror ("ptrace read");
267 #endif
268 errno = 0;
269 }
270 }
271
272 /* Store register REGNO back into the inferior. */
273
274 static void
275 store_register (int regno)
276 {
277 int *addr = alloca (MAX_REGISTER_RAW_SIZE);
278 int nr, isfloat;
279
280 /* Fetch the register's value from the register cache. */
281 regcache_collect (regno, addr);
282
283 /* -1 can be a successful return value, so infer errors from errno. */
284 errno = 0;
285
286 nr = regmap (regno, &isfloat);
287
288 /* Floating-point registers. */
289 if (isfloat)
290 rs6000_ptrace32 (PT_WRITE_FPR, PIDGET (inferior_ptid), addr, nr, 0);
291
292 /* Bogus register number. */
293 else if (nr < 0)
294 {
295 if (regno >= NUM_REGS)
296 fprintf_unfiltered (gdb_stderr,
297 "gdb error: register no %d not implemented.\n",
298 regno);
299 }
300
301 /* Fixed-point registers. */
302 else
303 {
304 if (regno == SP_REGNUM)
305 /* Execute one dummy instruction (which is a breakpoint) in inferior
306 process to give kernel a chance to do internal housekeeping.
307 Otherwise the following ptrace(2) calls will mess up user stack
308 since kernel will get confused about the bottom of the stack
309 (%sp). */
310 exec_one_dummy_insn ();
311
312 /* The PT_WRITE_GPR operation is rather odd. For 32-bit inferiors,
313 the register's value is passed by value, but for 64-bit inferiors,
314 the address of a buffer containing the value is passed. */
315 if (!ARCH64 ())
316 rs6000_ptrace32 (PT_WRITE_GPR, PIDGET (inferior_ptid), (int *)nr, *addr, 0);
317 else
318 {
319 /* PT_WRITE_GPR requires the buffer parameter to point to an 8-byte
320 area, even if the register is really only 32 bits. */
321 long long buf;
322 if (REGISTER_RAW_SIZE (regno) == 8)
323 memcpy (&buf, addr, 8);
324 else
325 buf = *addr;
326 rs6000_ptrace64 (PT_WRITE_GPR, PIDGET (inferior_ptid), nr, 0, (int *)&buf);
327 }
328 }
329
330 if (errno)
331 {
332 perror ("ptrace write");
333 errno = 0;
334 }
335 }
336
337 /* Read from the inferior all registers if REGNO == -1 and just register
338 REGNO otherwise. */
339
340 void
341 fetch_inferior_registers (int regno)
342 {
343 if (regno != -1)
344 fetch_register (regno);
345
346 else
347 {
348 struct gdbarch_tdep *tdep = gdbarch_tdep (current_gdbarch);
349
350 /* Read 32 general purpose registers. */
351 for (regno = tdep->ppc_gp0_regnum;
352 regno <= tdep->ppc_gplast_regnum;
353 regno++)
354 {
355 fetch_register (regno);
356 }
357
358 /* Read general purpose floating point registers. */
359 for (regno = FP0_REGNUM; regno <= FPLAST_REGNUM; regno++)
360 fetch_register (regno);
361
362 /* Read special registers. */
363 fetch_register (PC_REGNUM);
364 fetch_register (tdep->ppc_ps_regnum);
365 fetch_register (tdep->ppc_cr_regnum);
366 fetch_register (tdep->ppc_lr_regnum);
367 fetch_register (tdep->ppc_ctr_regnum);
368 fetch_register (tdep->ppc_xer_regnum);
369 if (tdep->ppc_mq_regnum >= 0)
370 fetch_register (tdep->ppc_mq_regnum);
371 }
372 }
373
374 /* Store our register values back into the inferior.
375 If REGNO is -1, do this for all registers.
376 Otherwise, REGNO specifies which register (so we can save time). */
377
378 void
379 store_inferior_registers (int regno)
380 {
381 if (regno != -1)
382 store_register (regno);
383
384 else
385 {
386 struct gdbarch_tdep *tdep = gdbarch_tdep (current_gdbarch);
387
388 /* Write general purpose registers first. */
389 for (regno = tdep->ppc_gp0_regnum;
390 regno <= tdep->ppc_gplast_regnum;
391 regno++)
392 {
393 store_register (regno);
394 }
395
396 /* Write floating point registers. */
397 for (regno = FP0_REGNUM; regno <= FPLAST_REGNUM; regno++)
398 store_register (regno);
399
400 /* Write special registers. */
401 store_register (PC_REGNUM);
402 store_register (tdep->ppc_ps_regnum);
403 store_register (tdep->ppc_cr_regnum);
404 store_register (tdep->ppc_lr_regnum);
405 store_register (tdep->ppc_ctr_regnum);
406 store_register (tdep->ppc_xer_regnum);
407 if (tdep->ppc_mq_regnum >= 0)
408 store_register (tdep->ppc_mq_regnum);
409 }
410 }
411
412 /* Store in *TO the 32-bit word at 32-bit-aligned ADDR in the child
413 process, which is 64-bit if ARCH64 and 32-bit otherwise. Return
414 success. */
415
416 static int
417 read_word (CORE_ADDR from, int *to, int arch64)
418 {
419 /* Retrieved values may be -1, so infer errors from errno. */
420 errno = 0;
421
422 if (arch64)
423 *to = rs6000_ptrace64 (PT_READ_I, PIDGET (inferior_ptid), from, 0, NULL);
424 else
425 *to = rs6000_ptrace32 (PT_READ_I, PIDGET (inferior_ptid), (int *)(long) from,
426 0, NULL);
427
428 return !errno;
429 }
430
431 /* Copy LEN bytes to or from inferior's memory starting at MEMADDR
432 to debugger memory starting at MYADDR. Copy to inferior if
433 WRITE is nonzero.
434
435 Returns the length copied, which is either the LEN argument or zero.
436 This xfer function does not do partial moves, since child_ops
437 doesn't allow memory operations to cross below us in the target stack
438 anyway. */
439
440 int
441 child_xfer_memory (CORE_ADDR memaddr, char *myaddr, int len,
442 int write, struct mem_attrib *attrib,
443 struct target_ops *target)
444 {
445 /* Round starting address down to 32-bit word boundary. */
446 int mask = sizeof (int) - 1;
447 CORE_ADDR addr = memaddr & ~(CORE_ADDR)mask;
448
449 /* Round ending address up to 32-bit word boundary. */
450 int count = ((memaddr + len - addr + mask) & ~(CORE_ADDR)mask)
451 / sizeof (int);
452
453 /* Allocate word transfer buffer. */
454 /* FIXME (alloca): This code, cloned from infptrace.c, is unsafe
455 because it uses alloca to allocate a buffer of arbitrary size.
456 For very large xfers, this could crash GDB's stack. */
457 int *buf = (int *) alloca (count * sizeof (int));
458
459 int arch64 = ARCH64 ();
460 int i;
461
462 if (!write)
463 {
464 /* Retrieve memory a word at a time. */
465 for (i = 0; i < count; i++, addr += sizeof (int))
466 {
467 if (!read_word (addr, buf + i, arch64))
468 return 0;
469 QUIT;
470 }
471
472 /* Copy memory to supplied buffer. */
473 addr -= count * sizeof (int);
474 memcpy (myaddr, (char *)buf + (memaddr - addr), len);
475 }
476 else
477 {
478 /* Fetch leading memory needed for alignment. */
479 if (addr < memaddr)
480 if (!read_word (addr, buf, arch64))
481 return 0;
482
483 /* Fetch trailing memory needed for alignment. */
484 if (addr + count * sizeof (int) > memaddr + len)
485 if (!read_word (addr, buf + count - 1, arch64))
486 return 0;
487
488 /* Copy supplied data into memory buffer. */
489 memcpy ((char *)buf + (memaddr - addr), myaddr, len);
490
491 /* Store memory one word at a time. */
492 for (i = 0, errno = 0; i < count; i++, addr += sizeof (int))
493 {
494 if (arch64)
495 rs6000_ptrace64 (PT_WRITE_D, PIDGET (inferior_ptid), addr, buf[i], NULL);
496 else
497 rs6000_ptrace32 (PT_WRITE_D, PIDGET (inferior_ptid), (int *)(long) addr,
498 buf[i], NULL);
499
500 if (errno)
501 return 0;
502 QUIT;
503 }
504 }
505
506 return len;
507 }
508
509 /* Execute one dummy breakpoint instruction. This way we give the kernel
510 a chance to do some housekeeping and update inferior's internal data,
511 including u_area. */
512
513 static void
514 exec_one_dummy_insn (void)
515 {
516 #define DUMMY_INSN_ADDR (TEXT_SEGMENT_BASE)+0x200
517
518 char shadow_contents[BREAKPOINT_MAX]; /* Stash old bkpt addr contents */
519 int ret, status, pid;
520 CORE_ADDR prev_pc;
521
522 /* We plant one dummy breakpoint into DUMMY_INSN_ADDR address. We
523 assume that this address will never be executed again by the real
524 code. */
525
526 target_insert_breakpoint (DUMMY_INSN_ADDR, shadow_contents);
527
528 /* You might think this could be done with a single ptrace call, and
529 you'd be correct for just about every platform I've ever worked
530 on. However, rs6000-ibm-aix4.1.3 seems to have screwed this up --
531 the inferior never hits the breakpoint (it's also worth noting
532 powerpc-ibm-aix4.1.3 works correctly). */
533 prev_pc = read_pc ();
534 write_pc (DUMMY_INSN_ADDR);
535 if (ARCH64 ())
536 ret = rs6000_ptrace64 (PT_CONTINUE, PIDGET (inferior_ptid), 1, 0, NULL);
537 else
538 ret = rs6000_ptrace32 (PT_CONTINUE, PIDGET (inferior_ptid), (int *)1, 0, NULL);
539
540 if (ret != 0)
541 perror ("pt_continue");
542
543 do
544 {
545 pid = wait (&status);
546 }
547 while (pid != PIDGET (inferior_ptid));
548
549 write_pc (prev_pc);
550 target_remove_breakpoint (DUMMY_INSN_ADDR, shadow_contents);
551 }
552
553 /* Fetch registers from the register section in core bfd. */
554
555 static void
556 fetch_core_registers (char *core_reg_sect, unsigned core_reg_size,
557 int which, CORE_ADDR reg_addr)
558 {
559 CoreRegs *regs;
560 int regi;
561 struct gdbarch_tdep *tdep = gdbarch_tdep (current_gdbarch);
562
563 if (which != 0)
564 {
565 fprintf_unfiltered
566 (gdb_stderr,
567 "Gdb error: unknown parameter to fetch_core_registers().\n");
568 return;
569 }
570
571 regs = (CoreRegs *) core_reg_sect;
572
573 /* Put the register values from the core file section in the regcache. */
574
575 if (ARCH64 ())
576 {
577 for (regi = 0; regi < 32; regi++)
578 supply_register (regi, (char *) &regs->r64.gpr[regi]);
579
580 for (regi = 0; regi < 32; regi++)
581 supply_register (FP0_REGNUM + regi, (char *) &regs->r64.fpr[regi]);
582
583 supply_register (PC_REGNUM, (char *) &regs->r64.iar);
584 supply_register (tdep->ppc_ps_regnum, (char *) &regs->r64.msr);
585 supply_register (tdep->ppc_cr_regnum, (char *) &regs->r64.cr);
586 supply_register (tdep->ppc_lr_regnum, (char *) &regs->r64.lr);
587 supply_register (tdep->ppc_ctr_regnum, (char *) &regs->r64.ctr);
588 supply_register (tdep->ppc_xer_regnum, (char *) &regs->r64.xer);
589 }
590 else
591 {
592 for (regi = 0; regi < 32; regi++)
593 supply_register (regi, (char *) &regs->r32.gpr[regi]);
594
595 for (regi = 0; regi < 32; regi++)
596 supply_register (FP0_REGNUM + regi, (char *) &regs->r32.fpr[regi]);
597
598 supply_register (PC_REGNUM, (char *) &regs->r32.iar);
599 supply_register (tdep->ppc_ps_regnum, (char *) &regs->r32.msr);
600 supply_register (tdep->ppc_cr_regnum, (char *) &regs->r32.cr);
601 supply_register (tdep->ppc_lr_regnum, (char *) &regs->r32.lr);
602 supply_register (tdep->ppc_ctr_regnum, (char *) &regs->r32.ctr);
603 supply_register (tdep->ppc_xer_regnum, (char *) &regs->r32.xer);
604 if (tdep->ppc_mq_regnum >= 0)
605 supply_register (tdep->ppc_mq_regnum, (char *) &regs->r32.mq);
606 }
607 }
608 \f
609
610 /* Copy information about text and data sections from LDI to VP for a 64-bit
611 process if ARCH64 and for a 32-bit process otherwise. */
612
613 static void
614 vmap_secs (struct vmap *vp, LdInfo *ldi, int arch64)
615 {
616 if (arch64)
617 {
618 vp->tstart = (CORE_ADDR) ldi->l64.ldinfo_textorg;
619 vp->tend = vp->tstart + ldi->l64.ldinfo_textsize;
620 vp->dstart = (CORE_ADDR) ldi->l64.ldinfo_dataorg;
621 vp->dend = vp->dstart + ldi->l64.ldinfo_datasize;
622 }
623 else
624 {
625 vp->tstart = (unsigned long) ldi->l32.ldinfo_textorg;
626 vp->tend = vp->tstart + ldi->l32.ldinfo_textsize;
627 vp->dstart = (unsigned long) ldi->l32.ldinfo_dataorg;
628 vp->dend = vp->dstart + ldi->l32.ldinfo_datasize;
629 }
630
631 /* The run time loader maps the file header in addition to the text
632 section and returns a pointer to the header in ldinfo_textorg.
633 Adjust the text start address to point to the real start address
634 of the text section. */
635 vp->tstart += vp->toffs;
636 }
637
638 /* handle symbol translation on vmapping */
639
640 static void
641 vmap_symtab (struct vmap *vp)
642 {
643 register struct objfile *objfile;
644 struct section_offsets *new_offsets;
645 int i;
646
647 objfile = vp->objfile;
648 if (objfile == NULL)
649 {
650 /* OK, it's not an objfile we opened ourselves.
651 Currently, that can only happen with the exec file, so
652 relocate the symbols for the symfile. */
653 if (symfile_objfile == NULL)
654 return;
655 objfile = symfile_objfile;
656 }
657 else if (!vp->loaded)
658 /* If symbols are not yet loaded, offsets are not yet valid. */
659 return;
660
661 new_offsets = (struct section_offsets *) alloca (SIZEOF_SECTION_OFFSETS);
662
663 for (i = 0; i < objfile->num_sections; ++i)
664 new_offsets->offsets[i] = ANOFFSET (objfile->section_offsets, i);
665
666 /* The symbols in the object file are linked to the VMA of the section,
667 relocate them VMA relative. */
668 new_offsets->offsets[SECT_OFF_TEXT (objfile)] = vp->tstart - vp->tvma;
669 new_offsets->offsets[SECT_OFF_DATA (objfile)] = vp->dstart - vp->dvma;
670 new_offsets->offsets[SECT_OFF_BSS (objfile)] = vp->dstart - vp->dvma;
671
672 objfile_relocate (objfile, new_offsets);
673 }
674 \f
675 /* Add symbols for an objfile. */
676
677 static int
678 objfile_symbol_add (void *arg)
679 {
680 struct objfile *obj = (struct objfile *) arg;
681
682 syms_from_objfile (obj, NULL, 0, 0);
683 new_symfile_objfile (obj, 0, 0);
684 return 1;
685 }
686
687 /* Add symbols for a vmap. Return zero upon error. */
688
689 int
690 vmap_add_symbols (struct vmap *vp)
691 {
692 if (catch_errors (objfile_symbol_add, vp->objfile,
693 "Error while reading shared library symbols:\n",
694 RETURN_MASK_ALL))
695 {
696 /* Note this is only done if symbol reading was successful. */
697 vp->loaded = 1;
698 vmap_symtab (vp);
699 return 1;
700 }
701 return 0;
702 }
703
704 /* Add a new vmap entry based on ldinfo() information.
705
706 If ldi->ldinfo_fd is not valid (e.g. this struct ld_info is from a
707 core file), the caller should set it to -1, and we will open the file.
708
709 Return the vmap new entry. */
710
711 static struct vmap *
712 add_vmap (LdInfo *ldi)
713 {
714 bfd *abfd, *last;
715 register char *mem, *objname, *filename;
716 struct objfile *obj;
717 struct vmap *vp;
718 int fd;
719 ARCH64_DECL (arch64);
720
721 /* This ldi structure was allocated using alloca() in
722 xcoff_relocate_symtab(). Now we need to have persistent object
723 and member names, so we should save them. */
724
725 filename = LDI_FILENAME (ldi, arch64);
726 mem = filename + strlen (filename) + 1;
727 mem = savestring (mem, strlen (mem));
728 objname = savestring (filename, strlen (filename));
729
730 fd = LDI_FD (ldi, arch64);
731 if (fd < 0)
732 /* Note that this opens it once for every member; a possible
733 enhancement would be to only open it once for every object. */
734 abfd = bfd_openr (objname, gnutarget);
735 else
736 abfd = bfd_fdopenr (objname, gnutarget, fd);
737 if (!abfd)
738 {
739 warning ("Could not open `%s' as an executable file: %s",
740 objname, bfd_errmsg (bfd_get_error ()));
741 return NULL;
742 }
743
744 /* make sure we have an object file */
745
746 if (bfd_check_format (abfd, bfd_object))
747 vp = map_vmap (abfd, 0);
748
749 else if (bfd_check_format (abfd, bfd_archive))
750 {
751 last = 0;
752 /* FIXME??? am I tossing BFDs? bfd? */
753 while ((last = bfd_openr_next_archived_file (abfd, last)))
754 if (STREQ (mem, last->filename))
755 break;
756
757 if (!last)
758 {
759 warning ("\"%s\": member \"%s\" missing.", objname, mem);
760 bfd_close (abfd);
761 return NULL;
762 }
763
764 if (!bfd_check_format (last, bfd_object))
765 {
766 warning ("\"%s\": member \"%s\" not in executable format: %s.",
767 objname, mem, bfd_errmsg (bfd_get_error ()));
768 bfd_close (last);
769 bfd_close (abfd);
770 return NULL;
771 }
772
773 vp = map_vmap (last, abfd);
774 }
775 else
776 {
777 warning ("\"%s\": not in executable format: %s.",
778 objname, bfd_errmsg (bfd_get_error ()));
779 bfd_close (abfd);
780 return NULL;
781 }
782 obj = allocate_objfile (vp->bfd, 0);
783 vp->objfile = obj;
784
785 /* Always add symbols for the main objfile. */
786 if (vp == vmap || auto_solib_add)
787 vmap_add_symbols (vp);
788 return vp;
789 }
790 \f
791 /* update VMAP info with ldinfo() information
792 Input is ptr to ldinfo() results. */
793
794 static void
795 vmap_ldinfo (LdInfo *ldi)
796 {
797 struct stat ii, vi;
798 register struct vmap *vp;
799 int got_one, retried;
800 int got_exec_file = 0;
801 uint next;
802 int arch64 = ARCH64 ();
803
804 /* For each *ldi, see if we have a corresponding *vp.
805 If so, update the mapping, and symbol table.
806 If not, add an entry and symbol table. */
807
808 do
809 {
810 char *name = LDI_FILENAME (ldi, arch64);
811 char *memb = name + strlen (name) + 1;
812 int fd = LDI_FD (ldi, arch64);
813
814 retried = 0;
815
816 if (fstat (fd, &ii) < 0)
817 {
818 /* The kernel sets ld_info to -1, if the process is still using the
819 object, and the object is removed. Keep the symbol info for the
820 removed object and issue a warning. */
821 warning ("%s (fd=%d) has disappeared, keeping its symbols",
822 name, fd);
823 continue;
824 }
825 retry:
826 for (got_one = 0, vp = vmap; vp; vp = vp->nxt)
827 {
828 struct objfile *objfile;
829
830 /* First try to find a `vp', which is the same as in ldinfo.
831 If not the same, just continue and grep the next `vp'. If same,
832 relocate its tstart, tend, dstart, dend values. If no such `vp'
833 found, get out of this for loop, add this ldi entry as a new vmap
834 (add_vmap) and come back, find its `vp' and so on... */
835
836 /* The filenames are not always sufficient to match on. */
837
838 if ((name[0] == '/' && !STREQ (name, vp->name))
839 || (memb[0] && !STREQ (memb, vp->member)))
840 continue;
841
842 /* See if we are referring to the same file.
843 We have to check objfile->obfd, symfile.c:reread_symbols might
844 have updated the obfd after a change. */
845 objfile = vp->objfile == NULL ? symfile_objfile : vp->objfile;
846 if (objfile == NULL
847 || objfile->obfd == NULL
848 || bfd_stat (objfile->obfd, &vi) < 0)
849 {
850 warning ("Unable to stat %s, keeping its symbols", name);
851 continue;
852 }
853
854 if (ii.st_dev != vi.st_dev || ii.st_ino != vi.st_ino)
855 continue;
856
857 if (!retried)
858 close (fd);
859
860 ++got_one;
861
862 /* Found a corresponding VMAP. Remap! */
863
864 vmap_secs (vp, ldi, arch64);
865
866 /* The objfile is only NULL for the exec file. */
867 if (vp->objfile == NULL)
868 got_exec_file = 1;
869
870 /* relocate symbol table(s). */
871 vmap_symtab (vp);
872
873 /* Announce new object files. Doing this after symbol relocation
874 makes aix-thread.c's job easier. */
875 if (target_new_objfile_hook && vp->objfile)
876 target_new_objfile_hook (vp->objfile);
877
878 /* There may be more, so we don't break out of the loop. */
879 }
880
881 /* if there was no matching *vp, we must perforce create the sucker(s) */
882 if (!got_one && !retried)
883 {
884 add_vmap (ldi);
885 ++retried;
886 goto retry;
887 }
888 }
889 while ((next = LDI_NEXT (ldi, arch64))
890 && (ldi = (void *) (next + (char *) ldi)));
891
892 /* If we don't find the symfile_objfile anywhere in the ldinfo, it
893 is unlikely that the symbol file is relocated to the proper
894 address. And we might have attached to a process which is
895 running a different copy of the same executable. */
896 if (symfile_objfile != NULL && !got_exec_file)
897 {
898 warning ("Symbol file %s\nis not mapped; discarding it.\n\
899 If in fact that file has symbols which the mapped files listed by\n\
900 \"info files\" lack, you can load symbols with the \"symbol-file\" or\n\
901 \"add-symbol-file\" commands (note that you must take care of relocating\n\
902 symbols to the proper address).",
903 symfile_objfile->name);
904 free_objfile (symfile_objfile);
905 symfile_objfile = NULL;
906 }
907 breakpoint_re_set ();
908 }
909 \f
910 /* As well as symbol tables, exec_sections need relocation. After
911 the inferior process' termination, there will be a relocated symbol
912 table exist with no corresponding inferior process. At that time, we
913 need to use `exec' bfd, rather than the inferior process's memory space
914 to look up symbols.
915
916 `exec_sections' need to be relocated only once, as long as the exec
917 file remains unchanged.
918 */
919
920 static void
921 vmap_exec (void)
922 {
923 static bfd *execbfd;
924 int i;
925
926 if (execbfd == exec_bfd)
927 return;
928
929 execbfd = exec_bfd;
930
931 if (!vmap || !exec_ops.to_sections)
932 error ("vmap_exec: vmap or exec_ops.to_sections == 0\n");
933
934 for (i = 0; &exec_ops.to_sections[i] < exec_ops.to_sections_end; i++)
935 {
936 if (STREQ (".text", exec_ops.to_sections[i].the_bfd_section->name))
937 {
938 exec_ops.to_sections[i].addr += vmap->tstart - vmap->tvma;
939 exec_ops.to_sections[i].endaddr += vmap->tstart - vmap->tvma;
940 }
941 else if (STREQ (".data", exec_ops.to_sections[i].the_bfd_section->name))
942 {
943 exec_ops.to_sections[i].addr += vmap->dstart - vmap->dvma;
944 exec_ops.to_sections[i].endaddr += vmap->dstart - vmap->dvma;
945 }
946 else if (STREQ (".bss", exec_ops.to_sections[i].the_bfd_section->name))
947 {
948 exec_ops.to_sections[i].addr += vmap->dstart - vmap->dvma;
949 exec_ops.to_sections[i].endaddr += vmap->dstart - vmap->dvma;
950 }
951 }
952 }
953
954 /* Set the current architecture from the host running GDB. Called when
955 starting a child process. */
956
957 static void
958 set_host_arch (int pid)
959 {
960 enum bfd_architecture arch;
961 unsigned long mach;
962 bfd abfd;
963 struct gdbarch_info info;
964
965 if (__power_rs ())
966 {
967 arch = bfd_arch_rs6000;
968 mach = bfd_mach_rs6k;
969 }
970 else
971 {
972 arch = bfd_arch_powerpc;
973 mach = bfd_mach_ppc;
974 }
975
976 /* FIXME: schauer/2002-02-25:
977 We don't know if we are executing a 32 or 64 bit executable,
978 and have no way to pass the proper word size to rs6000_gdbarch_init.
979 So we have to avoid switching to a new architecture, if the architecture
980 matches already.
981 Blindly calling rs6000_gdbarch_init used to work in older versions of
982 GDB, as rs6000_gdbarch_init incorrectly used the previous tdep to
983 determine the wordsize. */
984 if (exec_bfd)
985 {
986 const struct bfd_arch_info *exec_bfd_arch_info;
987
988 exec_bfd_arch_info = bfd_get_arch_info (exec_bfd);
989 if (arch == exec_bfd_arch_info->arch)
990 return;
991 }
992
993 bfd_default_set_arch_mach (&abfd, arch, mach);
994
995 gdbarch_info_init (&info);
996 info.bfd_arch_info = bfd_get_arch_info (&abfd);
997
998 if (!gdbarch_update_p (info))
999 {
1000 internal_error (__FILE__, __LINE__,
1001 "set_host_arch: failed to select architecture");
1002 }
1003 }
1004
1005 \f
1006 /* xcoff_relocate_symtab - hook for symbol table relocation.
1007 also reads shared libraries.. */
1008
1009 void
1010 xcoff_relocate_symtab (unsigned int pid)
1011 {
1012 int load_segs = 64; /* number of load segments */
1013 int rc;
1014 LdInfo *ldi = NULL;
1015 int arch64 = ARCH64 ();
1016 int ldisize = arch64 ? sizeof (ldi->l64) : sizeof (ldi->l32);
1017 int size;
1018
1019 do
1020 {
1021 size = load_segs * ldisize;
1022 ldi = (void *) xrealloc (ldi, size);
1023
1024 #if 0
1025 /* According to my humble theory, AIX has some timing problems and
1026 when the user stack grows, kernel doesn't update stack info in time
1027 and ptrace calls step on user stack. That is why we sleep here a
1028 little, and give kernel to update its internals. */
1029 usleep (36000);
1030 #endif
1031
1032 if (arch64)
1033 rc = rs6000_ptrace64 (PT_LDINFO, pid, (unsigned long) ldi, size, NULL);
1034 else
1035 rc = rs6000_ptrace32 (PT_LDINFO, pid, (int *) ldi, size, NULL);
1036
1037 if (rc == -1)
1038 {
1039 if (errno == ENOMEM)
1040 load_segs *= 2;
1041 else
1042 perror_with_name ("ptrace ldinfo");
1043 }
1044 else
1045 {
1046 vmap_ldinfo (ldi);
1047 vmap_exec (); /* relocate the exec and core sections as well. */
1048 }
1049 } while (rc == -1);
1050 if (ldi)
1051 xfree (ldi);
1052 }
1053 \f
1054 /* Core file stuff. */
1055
1056 /* Relocate symtabs and read in shared library info, based on symbols
1057 from the core file. */
1058
1059 void
1060 xcoff_relocate_core (struct target_ops *target)
1061 {
1062 sec_ptr ldinfo_sec;
1063 int offset = 0;
1064 LdInfo *ldi;
1065 struct vmap *vp;
1066 int arch64 = ARCH64 ();
1067
1068 /* Size of a struct ld_info except for the variable-length filename. */
1069 int nonfilesz = (int)LDI_FILENAME ((LdInfo *)0, arch64);
1070
1071 /* Allocated size of buffer. */
1072 int buffer_size = nonfilesz;
1073 char *buffer = xmalloc (buffer_size);
1074 struct cleanup *old = make_cleanup (free_current_contents, &buffer);
1075
1076 ldinfo_sec = bfd_get_section_by_name (core_bfd, ".ldinfo");
1077 if (ldinfo_sec == NULL)
1078 {
1079 bfd_err:
1080 fprintf_filtered (gdb_stderr, "Couldn't get ldinfo from core file: %s\n",
1081 bfd_errmsg (bfd_get_error ()));
1082 do_cleanups (old);
1083 return;
1084 }
1085 do
1086 {
1087 int i;
1088 int names_found = 0;
1089
1090 /* Read in everything but the name. */
1091 if (bfd_get_section_contents (core_bfd, ldinfo_sec, buffer,
1092 offset, nonfilesz) == 0)
1093 goto bfd_err;
1094
1095 /* Now the name. */
1096 i = nonfilesz;
1097 do
1098 {
1099 if (i == buffer_size)
1100 {
1101 buffer_size *= 2;
1102 buffer = xrealloc (buffer, buffer_size);
1103 }
1104 if (bfd_get_section_contents (core_bfd, ldinfo_sec, &buffer[i],
1105 offset + i, 1) == 0)
1106 goto bfd_err;
1107 if (buffer[i++] == '\0')
1108 ++names_found;
1109 }
1110 while (names_found < 2);
1111
1112 ldi = (LdInfo *) buffer;
1113
1114 /* Can't use a file descriptor from the core file; need to open it. */
1115 if (arch64)
1116 ldi->l64.ldinfo_fd = -1;
1117 else
1118 ldi->l32.ldinfo_fd = -1;
1119
1120 /* The first ldinfo is for the exec file, allocated elsewhere. */
1121 if (offset == 0 && vmap != NULL)
1122 vp = vmap;
1123 else
1124 vp = add_vmap (ldi);
1125
1126 /* Process next shared library upon error. */
1127 offset += LDI_NEXT (ldi, arch64);
1128 if (vp == NULL)
1129 continue;
1130
1131 vmap_secs (vp, ldi, arch64);
1132
1133 /* Unless this is the exec file,
1134 add our sections to the section table for the core target. */
1135 if (vp != vmap)
1136 {
1137 struct section_table *stp;
1138
1139 target_resize_to_sections (target, 2);
1140 stp = target->to_sections_end - 2;
1141
1142 stp->bfd = vp->bfd;
1143 stp->the_bfd_section = bfd_get_section_by_name (stp->bfd, ".text");
1144 stp->addr = vp->tstart;
1145 stp->endaddr = vp->tend;
1146 stp++;
1147
1148 stp->bfd = vp->bfd;
1149 stp->the_bfd_section = bfd_get_section_by_name (stp->bfd, ".data");
1150 stp->addr = vp->dstart;
1151 stp->endaddr = vp->dend;
1152 }
1153
1154 vmap_symtab (vp);
1155
1156 if (target_new_objfile_hook && vp != vmap && vp->objfile)
1157 target_new_objfile_hook (vp->objfile);
1158 }
1159 while (LDI_NEXT (ldi, arch64) != 0);
1160 vmap_exec ();
1161 breakpoint_re_set ();
1162 do_cleanups (old);
1163 }
1164
1165 int
1166 kernel_u_size (void)
1167 {
1168 return (sizeof (struct user));
1169 }
1170 \f
1171 /* Under AIX, we have to pass the correct TOC pointer to a function
1172 when calling functions in the inferior.
1173 We try to find the relative toc offset of the objfile containing PC
1174 and add the current load address of the data segment from the vmap. */
1175
1176 static CORE_ADDR
1177 find_toc_address (CORE_ADDR pc)
1178 {
1179 struct vmap *vp;
1180 extern CORE_ADDR get_toc_offset (struct objfile *); /* xcoffread.c */
1181
1182 for (vp = vmap; vp; vp = vp->nxt)
1183 {
1184 if (pc >= vp->tstart && pc < vp->tend)
1185 {
1186 /* vp->objfile is only NULL for the exec file. */
1187 return vp->dstart + get_toc_offset (vp->objfile == NULL
1188 ? symfile_objfile
1189 : vp->objfile);
1190 }
1191 }
1192 error ("Unable to find TOC entry for pc %s\n", local_hex_string (pc));
1193 }
1194 \f
1195 /* Register that we are able to handle rs6000 core file formats. */
1196
1197 static struct core_fns rs6000_core_fns =
1198 {
1199 bfd_target_xcoff_flavour, /* core_flavour */
1200 default_check_format, /* check_format */
1201 default_core_sniffer, /* core_sniffer */
1202 fetch_core_registers, /* core_read_registers */
1203 NULL /* next */
1204 };
1205
1206 void
1207 _initialize_core_rs6000 (void)
1208 {
1209 /* Initialize hook in rs6000-tdep.c for determining the TOC address when
1210 calling functions in the inferior. */
1211 rs6000_find_toc_address_hook = find_toc_address;
1212
1213 /* Initialize hook in rs6000-tdep.c to set the current architecture when
1214 starting a child process. */
1215 rs6000_set_host_arch_hook = set_host_arch;
1216
1217 add_core_fns (&rs6000_core_fns);
1218 }
This page took 0.069931 seconds and 5 git commands to generate.