1161537Smarcel/* IBM RS/6000 native-dependent code for GDB, the GNU debugger.
2161537Smarcel
3161537Smarcel   Copyright 1986, 1987, 1989, 1991, 1992, 1993, 1994, 1995, 1996,
4161537Smarcel   1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004 Free Software
5161537Smarcel   Foundation, Inc.
6161537Smarcel
7161537Smarcel   This file is part of GDB.
8161537Smarcel
9161537Smarcel   This program is free software; you can redistribute it and/or modify
10161537Smarcel   it under the terms of the GNU General Public License as published by
11161537Smarcel   the Free Software Foundation; either version 2 of the License, or
12161537Smarcel   (at your option) any later version.
13161537Smarcel
14161537Smarcel   This program is distributed in the hope that it will be useful,
15161537Smarcel   but WITHOUT ANY WARRANTY; without even the implied warranty of
16161537Smarcel   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17161537Smarcel   GNU General Public License for more details.
18161537Smarcel
19161537Smarcel   You should have received a copy of the GNU General Public License
20161537Smarcel   along with this program; if not, write to the Free Software
21161537Smarcel   Foundation, Inc., 59 Temple Place - Suite 330,
22161537Smarcel   Boston, MA 02111-1307, USA.  */
23161537Smarcel
24161537Smarcel#include "defs.h"
25161537Smarcel#include "inferior.h"
26161537Smarcel#include "target.h"
27161537Smarcel#include "gdbcore.h"
28161537Smarcel#include "xcoffsolib.h"
29161537Smarcel#include "symfile.h"
30161537Smarcel#include "objfiles.h"
31161537Smarcel#include "libbfd.h"		/* For bfd_cache_lookup (FIXME) */
32161537Smarcel#include "bfd.h"
33161537Smarcel#include "gdb-stabs.h"
34161537Smarcel#include "regcache.h"
35161537Smarcel#include "arch-utils.h"
36161537Smarcel#include "language.h"		/* for local_hex_string().  */
37161537Smarcel#include "ppc-tdep.h"
38161537Smarcel#include "exec.h"
39161537Smarcel
40161537Smarcel#include <sys/ptrace.h>
41161537Smarcel#include <sys/reg.h>
42161537Smarcel
43161537Smarcel#include <sys/param.h>
44161537Smarcel#include <sys/dir.h>
45161537Smarcel#include <sys/user.h>
46161537Smarcel#include <signal.h>
47161537Smarcel#include <sys/ioctl.h>
48161537Smarcel#include <fcntl.h>
49161537Smarcel#include <errno.h>
50161537Smarcel
51161537Smarcel#include <a.out.h>
52161537Smarcel#include <sys/file.h>
53161537Smarcel#include "gdb_stat.h"
54161537Smarcel#include <sys/core.h>
55161537Smarcel#define __LDINFO_PTRACE32__	/* for __ld_info32 */
56161537Smarcel#define __LDINFO_PTRACE64__	/* for __ld_info64 */
57161537Smarcel#include <sys/ldr.h>
58161537Smarcel#include <sys/systemcfg.h>
59161537Smarcel
60161537Smarcel/* On AIX4.3+, sys/ldr.h provides different versions of struct ld_info for
61161537Smarcel   debugging 32-bit and 64-bit processes.  Define a typedef and macros for
62161537Smarcel   accessing fields in the appropriate structures. */
63161537Smarcel
64161537Smarcel/* In 32-bit compilation mode (which is the only mode from which ptrace()
65161537Smarcel   works on 4.3), __ld_info32 is #defined as equivalent to ld_info. */
66161537Smarcel
67161537Smarcel#ifdef __ld_info32
68161537Smarcel# define ARCH3264
69161537Smarcel#endif
70161537Smarcel
71161537Smarcel/* Return whether the current architecture is 64-bit. */
72161537Smarcel
73161537Smarcel#ifndef ARCH3264
74161537Smarcel# define ARCH64() 0
75161537Smarcel#else
76161537Smarcel# define ARCH64() (DEPRECATED_REGISTER_RAW_SIZE (0) == 8)
77161537Smarcel#endif
78161537Smarcel
79161537Smarcel/* Union of 32-bit and 64-bit ".reg" core file sections. */
80161537Smarcel
81161537Smarceltypedef union {
82161537Smarcel#ifdef ARCH3264
83161537Smarcel  struct __context64 r64;
84161537Smarcel#else
85161537Smarcel  struct mstsave r64;
86161537Smarcel#endif
87161537Smarcel  struct mstsave r32;
88161537Smarcel} CoreRegs;
89161537Smarcel
90161537Smarcel/* Union of 32-bit and 64-bit versions of ld_info. */
91161537Smarcel
92161537Smarceltypedef union {
93161537Smarcel#ifndef ARCH3264
94161537Smarcel  struct ld_info l32;
95161537Smarcel  struct ld_info l64;
96161537Smarcel#else
97161537Smarcel  struct __ld_info32 l32;
98161537Smarcel  struct __ld_info64 l64;
99161537Smarcel#endif
100161537Smarcel} LdInfo;
101161537Smarcel
102161537Smarcel/* If compiling with 32-bit and 64-bit debugging capability (e.g. AIX 4.x),
103161537Smarcel   declare and initialize a variable named VAR suitable for use as the arch64
104161537Smarcel   parameter to the various LDI_*() macros. */
105161537Smarcel
106161537Smarcel#ifndef ARCH3264
107161537Smarcel# define ARCH64_DECL(var)
108161537Smarcel#else
109161537Smarcel# define ARCH64_DECL(var) int var = ARCH64 ()
110161537Smarcel#endif
111161537Smarcel
112161537Smarcel/* Return LDI's FIELD for a 64-bit process if ARCH64 and for a 32-bit process
113161537Smarcel   otherwise.  This technique only works for FIELDs with the same data type in
114161537Smarcel   32-bit and 64-bit versions of ld_info. */
115161537Smarcel
116161537Smarcel#ifndef ARCH3264
117161537Smarcel# define LDI_FIELD(ldi, arch64, field) (ldi)->l32.ldinfo_##field
118161537Smarcel#else
119161537Smarcel# define LDI_FIELD(ldi, arch64, field) \
120161537Smarcel  (arch64 ? (ldi)->l64.ldinfo_##field : (ldi)->l32.ldinfo_##field)
121161537Smarcel#endif
122161537Smarcel
123161537Smarcel/* Return various LDI fields for a 64-bit process if ARCH64 and for a 32-bit
124161537Smarcel   process otherwise. */
125161537Smarcel
126161537Smarcel#define LDI_NEXT(ldi, arch64)		LDI_FIELD(ldi, arch64, next)
127161537Smarcel#define LDI_FD(ldi, arch64)		LDI_FIELD(ldi, arch64, fd)
128161537Smarcel#define LDI_FILENAME(ldi, arch64)	LDI_FIELD(ldi, arch64, filename)
129161537Smarcel
130161537Smarcelextern struct vmap *map_vmap (bfd * bf, bfd * arch);
131161537Smarcel
132161537Smarcelstatic void vmap_exec (void);
133161537Smarcel
134161537Smarcelstatic void vmap_ldinfo (LdInfo *);
135161537Smarcel
136161537Smarcelstatic struct vmap *add_vmap (LdInfo *);
137161537Smarcel
138161537Smarcelstatic int objfile_symbol_add (void *);
139161537Smarcel
140161537Smarcelstatic void vmap_symtab (struct vmap *);
141161537Smarcel
142161537Smarcelstatic void fetch_core_registers (char *, unsigned int, int, CORE_ADDR);
143161537Smarcel
144161537Smarcelstatic void exec_one_dummy_insn (void);
145161537Smarcel
146161537Smarcelextern void fixup_breakpoints (CORE_ADDR low, CORE_ADDR high, CORE_ADDR delta);
147161537Smarcel
148161537Smarcel/* Given REGNO, a gdb register number, return the corresponding
149161537Smarcel   number suitable for use as a ptrace() parameter.  Return -1 if
150161537Smarcel   there's no suitable mapping.  Also, set the int pointed to by
151161537Smarcel   ISFLOAT to indicate whether REGNO is a floating point register.  */
152161537Smarcel
153161537Smarcelstatic int
154161537Smarcelregmap (int regno, int *isfloat)
155161537Smarcel{
156161537Smarcel  struct gdbarch_tdep *tdep = gdbarch_tdep (current_gdbarch);
157161537Smarcel
158161537Smarcel  *isfloat = 0;
159161537Smarcel  if (tdep->ppc_gp0_regnum <= regno && regno <= tdep->ppc_gplast_regnum)
160161537Smarcel    return regno;
161161537Smarcel  else if (FP0_REGNUM <= regno && regno <= FPLAST_REGNUM)
162161537Smarcel    {
163161537Smarcel      *isfloat = 1;
164161537Smarcel      return regno - FP0_REGNUM + FPR0;
165161537Smarcel    }
166161537Smarcel  else if (regno == PC_REGNUM)
167161537Smarcel    return IAR;
168161537Smarcel  else if (regno == tdep->ppc_ps_regnum)
169161537Smarcel    return MSR;
170161537Smarcel  else if (regno == tdep->ppc_cr_regnum)
171161537Smarcel    return CR;
172161537Smarcel  else if (regno == tdep->ppc_lr_regnum)
173161537Smarcel    return LR;
174161537Smarcel  else if (regno == tdep->ppc_ctr_regnum)
175161537Smarcel    return CTR;
176161537Smarcel  else if (regno == tdep->ppc_xer_regnum)
177161537Smarcel    return XER;
178161537Smarcel  else if (regno == tdep->ppc_fpscr_regnum)
179161537Smarcel    return FPSCR;
180161537Smarcel  else if (tdep->ppc_mq_regnum >= 0 && regno == tdep->ppc_mq_regnum)
181161537Smarcel    return MQ;
182161537Smarcel  else
183161537Smarcel    return -1;
184161537Smarcel}
185161537Smarcel
186161537Smarcel/* Call ptrace(REQ, ID, ADDR, DATA, BUF). */
187161537Smarcel
188161537Smarcelstatic int
189161537Smarcelrs6000_ptrace32 (int req, int id, int *addr, int data, int *buf)
190161537Smarcel{
191161537Smarcel  int ret = ptrace (req, id, (int *)addr, data, buf);
192161537Smarcel#if 0
193161537Smarcel  printf ("rs6000_ptrace32 (%d, %d, 0x%x, %08x, 0x%x) = 0x%x\n",
194161537Smarcel	  req, id, (unsigned int)addr, data, (unsigned int)buf, ret);
195161537Smarcel#endif
196161537Smarcel  return ret;
197161537Smarcel}
198161537Smarcel
199161537Smarcel/* Call ptracex(REQ, ID, ADDR, DATA, BUF). */
200161537Smarcel
201161537Smarcelstatic int
202161537Smarcelrs6000_ptrace64 (int req, int id, long long addr, int data, int *buf)
203161537Smarcel{
204161537Smarcel#ifdef ARCH3264
205161537Smarcel  int ret = ptracex (req, id, addr, data, buf);
206161537Smarcel#else
207161537Smarcel  int ret = 0;
208161537Smarcel#endif
209161537Smarcel#if 0
210161537Smarcel  printf ("rs6000_ptrace64 (%d, %d, 0x%llx, %08x, 0x%x) = 0x%x\n",
211161537Smarcel	  req, id, addr, data, (unsigned int)buf, ret);
212161537Smarcel#endif
213161537Smarcel  return ret;
214161537Smarcel}
215161537Smarcel
216161537Smarcel/* Fetch register REGNO from the inferior. */
217161537Smarcel
218161537Smarcelstatic void
219161537Smarcelfetch_register (int regno)
220161537Smarcel{
221161537Smarcel  int addr[MAX_REGISTER_SIZE];
222161537Smarcel  int nr, isfloat;
223161537Smarcel
224161537Smarcel  /* Retrieved values may be -1, so infer errors from errno. */
225161537Smarcel  errno = 0;
226161537Smarcel
227161537Smarcel  nr = regmap (regno, &isfloat);
228161537Smarcel
229161537Smarcel  /* Floating-point registers. */
230161537Smarcel  if (isfloat)
231161537Smarcel    rs6000_ptrace32 (PT_READ_FPR, PIDGET (inferior_ptid), addr, nr, 0);
232161537Smarcel
233161537Smarcel  /* Bogus register number. */
234161537Smarcel  else if (nr < 0)
235161537Smarcel    {
236161537Smarcel      if (regno >= NUM_REGS)
237161537Smarcel	fprintf_unfiltered (gdb_stderr,
238161537Smarcel			    "gdb error: register no %d not implemented.\n",
239161537Smarcel			    regno);
240161537Smarcel      return;
241161537Smarcel    }
242161537Smarcel
243161537Smarcel  /* Fixed-point registers. */
244161537Smarcel  else
245161537Smarcel    {
246161537Smarcel      if (!ARCH64 ())
247161537Smarcel	*addr = rs6000_ptrace32 (PT_READ_GPR, PIDGET (inferior_ptid), (int *)nr, 0, 0);
248161537Smarcel      else
249161537Smarcel	{
250161537Smarcel	  /* PT_READ_GPR requires the buffer parameter to point to long long,
251161537Smarcel	     even if the register is really only 32 bits. */
252161537Smarcel	  long long buf;
253161537Smarcel	  rs6000_ptrace64 (PT_READ_GPR, PIDGET (inferior_ptid), nr, 0, (int *)&buf);
254161537Smarcel	  if (DEPRECATED_REGISTER_RAW_SIZE (regno) == 8)
255161537Smarcel	    memcpy (addr, &buf, 8);
256161537Smarcel	  else
257161537Smarcel	    *addr = buf;
258161537Smarcel	}
259161537Smarcel    }
260161537Smarcel
261161537Smarcel  if (!errno)
262161537Smarcel    supply_register (regno, (char *) addr);
263161537Smarcel  else
264161537Smarcel    {
265161537Smarcel#if 0
266161537Smarcel      /* FIXME: this happens 3 times at the start of each 64-bit program. */
267161537Smarcel      perror ("ptrace read");
268161537Smarcel#endif
269161537Smarcel      errno = 0;
270161537Smarcel    }
271161537Smarcel}
272161537Smarcel
273161537Smarcel/* Store register REGNO back into the inferior. */
274161537Smarcel
275161537Smarcelstatic void
276161537Smarcelstore_register (int regno)
277161537Smarcel{
278161537Smarcel  int addr[MAX_REGISTER_SIZE];
279161537Smarcel  int nr, isfloat;
280161537Smarcel
281161537Smarcel  /* Fetch the register's value from the register cache.  */
282161537Smarcel  regcache_collect (regno, addr);
283161537Smarcel
284161537Smarcel  /* -1 can be a successful return value, so infer errors from errno. */
285161537Smarcel  errno = 0;
286161537Smarcel
287161537Smarcel  nr = regmap (regno, &isfloat);
288161537Smarcel
289161537Smarcel  /* Floating-point registers. */
290161537Smarcel  if (isfloat)
291161537Smarcel    rs6000_ptrace32 (PT_WRITE_FPR, PIDGET (inferior_ptid), addr, nr, 0);
292161537Smarcel
293161537Smarcel  /* Bogus register number. */
294161537Smarcel  else if (nr < 0)
295161537Smarcel    {
296161537Smarcel      if (regno >= NUM_REGS)
297161537Smarcel	fprintf_unfiltered (gdb_stderr,
298161537Smarcel			    "gdb error: register no %d not implemented.\n",
299161537Smarcel			    regno);
300161537Smarcel    }
301161537Smarcel
302161537Smarcel  /* Fixed-point registers. */
303161537Smarcel  else
304161537Smarcel    {
305161537Smarcel      if (regno == SP_REGNUM)
306161537Smarcel	/* Execute one dummy instruction (which is a breakpoint) in inferior
307161537Smarcel	   process to give kernel a chance to do internal housekeeping.
308161537Smarcel	   Otherwise the following ptrace(2) calls will mess up user stack
309161537Smarcel	   since kernel will get confused about the bottom of the stack
310161537Smarcel	   (%sp). */
311161537Smarcel	exec_one_dummy_insn ();
312161537Smarcel
313161537Smarcel      /* The PT_WRITE_GPR operation is rather odd.  For 32-bit inferiors,
314161537Smarcel         the register's value is passed by value, but for 64-bit inferiors,
315161537Smarcel	 the address of a buffer containing the value is passed.  */
316161537Smarcel      if (!ARCH64 ())
317161537Smarcel	rs6000_ptrace32 (PT_WRITE_GPR, PIDGET (inferior_ptid), (int *)nr, *addr, 0);
318161537Smarcel      else
319161537Smarcel	{
320161537Smarcel	  /* PT_WRITE_GPR requires the buffer parameter to point to an 8-byte
321161537Smarcel	     area, even if the register is really only 32 bits. */
322161537Smarcel	  long long buf;
323161537Smarcel	  if (DEPRECATED_REGISTER_RAW_SIZE (regno) == 8)
324161537Smarcel	    memcpy (&buf, addr, 8);
325161537Smarcel	  else
326161537Smarcel	    buf = *addr;
327161537Smarcel	  rs6000_ptrace64 (PT_WRITE_GPR, PIDGET (inferior_ptid), nr, 0, (int *)&buf);
328161537Smarcel	}
329161537Smarcel    }
330161537Smarcel
331161537Smarcel  if (errno)
332161537Smarcel    {
333161537Smarcel      perror ("ptrace write");
334161537Smarcel      errno = 0;
335161537Smarcel    }
336161537Smarcel}
337161537Smarcel
338161537Smarcel/* Read from the inferior all registers if REGNO == -1 and just register
339161537Smarcel   REGNO otherwise. */
340161537Smarcel
341161537Smarcelvoid
342161537Smarcelfetch_inferior_registers (int regno)
343161537Smarcel{
344161537Smarcel  if (regno != -1)
345161537Smarcel    fetch_register (regno);
346161537Smarcel
347161537Smarcel  else
348161537Smarcel    {
349161537Smarcel      struct gdbarch_tdep *tdep = gdbarch_tdep (current_gdbarch);
350161537Smarcel
351161537Smarcel      /* Read 32 general purpose registers.  */
352161537Smarcel      for (regno = tdep->ppc_gp0_regnum;
353161537Smarcel           regno <= tdep->ppc_gplast_regnum;
354161537Smarcel	   regno++)
355161537Smarcel	{
356161537Smarcel	  fetch_register (regno);
357161537Smarcel	}
358161537Smarcel
359161537Smarcel      /* Read general purpose floating point registers.  */
360161537Smarcel      for (regno = FP0_REGNUM; regno <= FPLAST_REGNUM; regno++)
361161537Smarcel	fetch_register (regno);
362161537Smarcel
363161537Smarcel      /* Read special registers.  */
364161537Smarcel      fetch_register (PC_REGNUM);
365161537Smarcel      fetch_register (tdep->ppc_ps_regnum);
366161537Smarcel      fetch_register (tdep->ppc_cr_regnum);
367161537Smarcel      fetch_register (tdep->ppc_lr_regnum);
368161537Smarcel      fetch_register (tdep->ppc_ctr_regnum);
369161537Smarcel      fetch_register (tdep->ppc_xer_regnum);
370161537Smarcel      fetch_register (tdep->ppc_fpscr_regnum);
371161537Smarcel      if (tdep->ppc_mq_regnum >= 0)
372161537Smarcel	fetch_register (tdep->ppc_mq_regnum);
373161537Smarcel    }
374161537Smarcel}
375161537Smarcel
376161537Smarcel/* Store our register values back into the inferior.
377161537Smarcel   If REGNO is -1, do this for all registers.
378161537Smarcel   Otherwise, REGNO specifies which register (so we can save time).  */
379161537Smarcel
380161537Smarcelvoid
381161537Smarcelstore_inferior_registers (int regno)
382161537Smarcel{
383161537Smarcel  if (regno != -1)
384161537Smarcel    store_register (regno);
385161537Smarcel
386161537Smarcel  else
387161537Smarcel    {
388161537Smarcel      struct gdbarch_tdep *tdep = gdbarch_tdep (current_gdbarch);
389161537Smarcel
390161537Smarcel      /* Write general purpose registers first.  */
391161537Smarcel      for (regno = tdep->ppc_gp0_regnum;
392161537Smarcel           regno <= tdep->ppc_gplast_regnum;
393161537Smarcel	   regno++)
394161537Smarcel	{
395161537Smarcel	  store_register (regno);
396161537Smarcel	}
397161537Smarcel
398161537Smarcel      /* Write floating point registers.  */
399161537Smarcel      for (regno = FP0_REGNUM; regno <= FPLAST_REGNUM; regno++)
400161537Smarcel	store_register (regno);
401161537Smarcel
402161537Smarcel      /* Write special registers.  */
403161537Smarcel      store_register (PC_REGNUM);
404161537Smarcel      store_register (tdep->ppc_ps_regnum);
405161537Smarcel      store_register (tdep->ppc_cr_regnum);
406161537Smarcel      store_register (tdep->ppc_lr_regnum);
407161537Smarcel      store_register (tdep->ppc_ctr_regnum);
408161537Smarcel      store_register (tdep->ppc_xer_regnum);
409161537Smarcel      store_register (tdep->ppc_fpscr_regnum);
410161537Smarcel      if (tdep->ppc_mq_regnum >= 0)
411161537Smarcel	store_register (tdep->ppc_mq_regnum);
412161537Smarcel    }
413161537Smarcel}
414161537Smarcel
415161537Smarcel/* Store in *TO the 32-bit word at 32-bit-aligned ADDR in the child
416161537Smarcel   process, which is 64-bit if ARCH64 and 32-bit otherwise.  Return
417161537Smarcel   success. */
418161537Smarcel
419161537Smarcelstatic int
420161537Smarcelread_word (CORE_ADDR from, int *to, int arch64)
421161537Smarcel{
422161537Smarcel  /* Retrieved values may be -1, so infer errors from errno. */
423161537Smarcel  errno = 0;
424161537Smarcel
425161537Smarcel  if (arch64)
426161537Smarcel    *to = rs6000_ptrace64 (PT_READ_I, PIDGET (inferior_ptid), from, 0, NULL);
427161537Smarcel  else
428161537Smarcel    *to = rs6000_ptrace32 (PT_READ_I, PIDGET (inferior_ptid), (int *)(long) from,
429161537Smarcel                    0, NULL);
430161537Smarcel
431161537Smarcel  return !errno;
432161537Smarcel}
433161537Smarcel
434161537Smarcel/* Copy LEN bytes to or from inferior's memory starting at MEMADDR
435161537Smarcel   to debugger memory starting at MYADDR.  Copy to inferior if
436161537Smarcel   WRITE is nonzero.
437161537Smarcel
438161537Smarcel   Returns the length copied, which is either the LEN argument or zero.
439161537Smarcel   This xfer function does not do partial moves, since child_ops
440161537Smarcel   doesn't allow memory operations to cross below us in the target stack
441161537Smarcel   anyway.  */
442161537Smarcel
443161537Smarcelint
444161537Smarcelchild_xfer_memory (CORE_ADDR memaddr, char *myaddr, int len,
445161537Smarcel		   int write, struct mem_attrib *attrib,
446161537Smarcel		   struct target_ops *target)
447161537Smarcel{
448161537Smarcel  /* Round starting address down to 32-bit word boundary. */
449161537Smarcel  int mask = sizeof (int) - 1;
450161537Smarcel  CORE_ADDR addr = memaddr & ~(CORE_ADDR)mask;
451161537Smarcel
452161537Smarcel  /* Round ending address up to 32-bit word boundary. */
453161537Smarcel  int count = ((memaddr + len - addr + mask) & ~(CORE_ADDR)mask)
454161537Smarcel    / sizeof (int);
455161537Smarcel
456161537Smarcel  /* Allocate word transfer buffer. */
457161537Smarcel  /* FIXME (alloca): This code, cloned from infptrace.c, is unsafe
458161537Smarcel     because it uses alloca to allocate a buffer of arbitrary size.
459161537Smarcel     For very large xfers, this could crash GDB's stack.  */
460161537Smarcel  int *buf = (int *) alloca (count * sizeof (int));
461161537Smarcel
462161537Smarcel  int arch64 = ARCH64 ();
463161537Smarcel  int i;
464161537Smarcel
465161537Smarcel  if (!write)
466161537Smarcel    {
467161537Smarcel      /* Retrieve memory a word at a time. */
468161537Smarcel      for (i = 0; i < count; i++, addr += sizeof (int))
469161537Smarcel	{
470161537Smarcel	  if (!read_word (addr, buf + i, arch64))
471161537Smarcel	    return 0;
472161537Smarcel	  QUIT;
473161537Smarcel	}
474161537Smarcel
475161537Smarcel      /* Copy memory to supplied buffer. */
476161537Smarcel      addr -= count * sizeof (int);
477161537Smarcel      memcpy (myaddr, (char *)buf + (memaddr - addr), len);
478161537Smarcel    }
479161537Smarcel  else
480161537Smarcel    {
481161537Smarcel      /* Fetch leading memory needed for alignment. */
482161537Smarcel      if (addr < memaddr)
483161537Smarcel	if (!read_word (addr, buf, arch64))
484161537Smarcel	  return 0;
485161537Smarcel
486161537Smarcel      /* Fetch trailing memory needed for alignment. */
487161537Smarcel      if (addr + count * sizeof (int) > memaddr + len)
488161537Smarcel	if (!read_word (addr + (count - 1) * sizeof (int),
489161537Smarcel                        buf + count - 1, arch64))
490161537Smarcel	  return 0;
491161537Smarcel
492161537Smarcel      /* Copy supplied data into memory buffer. */
493161537Smarcel      memcpy ((char *)buf + (memaddr - addr), myaddr, len);
494161537Smarcel
495161537Smarcel      /* Store memory one word at a time. */
496161537Smarcel      for (i = 0, errno = 0; i < count; i++, addr += sizeof (int))
497161537Smarcel	{
498161537Smarcel	  if (arch64)
499161537Smarcel	    rs6000_ptrace64 (PT_WRITE_D, PIDGET (inferior_ptid), addr, buf[i], NULL);
500161537Smarcel	  else
501161537Smarcel	    rs6000_ptrace32 (PT_WRITE_D, PIDGET (inferior_ptid), (int *)(long) addr,
502161537Smarcel		      buf[i], NULL);
503161537Smarcel
504161537Smarcel	  if (errno)
505161537Smarcel	    return 0;
506161537Smarcel	  QUIT;
507161537Smarcel	}
508161537Smarcel    }
509161537Smarcel
510161537Smarcel  return len;
511161537Smarcel}
512161537Smarcel
513161537Smarcel/* Execute one dummy breakpoint instruction.  This way we give the kernel
514161537Smarcel   a chance to do some housekeeping and update inferior's internal data,
515161537Smarcel   including u_area. */
516161537Smarcel
517161537Smarcelstatic void
518161537Smarcelexec_one_dummy_insn (void)
519161537Smarcel{
520161537Smarcel#define	DUMMY_INSN_ADDR	(TEXT_SEGMENT_BASE)+0x200
521161537Smarcel
522161537Smarcel  char shadow_contents[BREAKPOINT_MAX];		/* Stash old bkpt addr contents */
523161537Smarcel  int ret, status, pid;
524161537Smarcel  CORE_ADDR prev_pc;
525161537Smarcel
526161537Smarcel  /* We plant one dummy breakpoint into DUMMY_INSN_ADDR address. We
527161537Smarcel     assume that this address will never be executed again by the real
528161537Smarcel     code. */
529161537Smarcel
530161537Smarcel  target_insert_breakpoint (DUMMY_INSN_ADDR, shadow_contents);
531161537Smarcel
532161537Smarcel  /* You might think this could be done with a single ptrace call, and
533161537Smarcel     you'd be correct for just about every platform I've ever worked
534161537Smarcel     on.  However, rs6000-ibm-aix4.1.3 seems to have screwed this up --
535161537Smarcel     the inferior never hits the breakpoint (it's also worth noting
536161537Smarcel     powerpc-ibm-aix4.1.3 works correctly).  */
537161537Smarcel  prev_pc = read_pc ();
538161537Smarcel  write_pc (DUMMY_INSN_ADDR);
539161537Smarcel  if (ARCH64 ())
540161537Smarcel    ret = rs6000_ptrace64 (PT_CONTINUE, PIDGET (inferior_ptid), 1, 0, NULL);
541161537Smarcel  else
542161537Smarcel    ret = rs6000_ptrace32 (PT_CONTINUE, PIDGET (inferior_ptid), (int *)1, 0, NULL);
543161537Smarcel
544161537Smarcel  if (ret != 0)
545161537Smarcel    perror ("pt_continue");
546161537Smarcel
547161537Smarcel  do
548161537Smarcel    {
549161537Smarcel      pid = wait (&status);
550161537Smarcel    }
551161537Smarcel  while (pid != PIDGET (inferior_ptid));
552161537Smarcel
553161537Smarcel  write_pc (prev_pc);
554161537Smarcel  target_remove_breakpoint (DUMMY_INSN_ADDR, shadow_contents);
555161537Smarcel}
556161537Smarcel
557161537Smarcel/* Fetch registers from the register section in core bfd. */
558161537Smarcel
559161537Smarcelstatic void
560161537Smarcelfetch_core_registers (char *core_reg_sect, unsigned core_reg_size,
561161537Smarcel		      int which, CORE_ADDR reg_addr)
562161537Smarcel{
563161537Smarcel  CoreRegs *regs;
564161537Smarcel  int regi;
565161537Smarcel  struct gdbarch_tdep *tdep = gdbarch_tdep (current_gdbarch);
566161537Smarcel
567161537Smarcel  if (which != 0)
568161537Smarcel    {
569161537Smarcel      fprintf_unfiltered
570161537Smarcel	(gdb_stderr,
571161537Smarcel	 "Gdb error: unknown parameter to fetch_core_registers().\n");
572161537Smarcel      return;
573161537Smarcel    }
574161537Smarcel
575161537Smarcel  regs = (CoreRegs *) core_reg_sect;
576161537Smarcel
577161537Smarcel  /* Put the register values from the core file section in the regcache.  */
578161537Smarcel
579161537Smarcel  if (ARCH64 ())
580161537Smarcel    {
581161537Smarcel      for (regi = 0; regi < 32; regi++)
582161537Smarcel        supply_register (regi, (char *) &regs->r64.gpr[regi]);
583161537Smarcel
584161537Smarcel      for (regi = 0; regi < 32; regi++)
585161537Smarcel	supply_register (FP0_REGNUM + regi, (char *) &regs->r64.fpr[regi]);
586161537Smarcel
587161537Smarcel      supply_register (PC_REGNUM, (char *) &regs->r64.iar);
588161537Smarcel      supply_register (tdep->ppc_ps_regnum, (char *) &regs->r64.msr);
589161537Smarcel      supply_register (tdep->ppc_cr_regnum, (char *) &regs->r64.cr);
590161537Smarcel      supply_register (tdep->ppc_lr_regnum, (char *) &regs->r64.lr);
591161537Smarcel      supply_register (tdep->ppc_ctr_regnum, (char *) &regs->r64.ctr);
592161537Smarcel      supply_register (tdep->ppc_xer_regnum, (char *) &regs->r64.xer);
593161537Smarcel      supply_register (tdep->ppc_fpscr_regnum, (char *) &regs->r64.fpscr);
594161537Smarcel    }
595161537Smarcel  else
596161537Smarcel    {
597161537Smarcel      for (regi = 0; regi < 32; regi++)
598161537Smarcel        supply_register (regi, (char *) &regs->r32.gpr[regi]);
599161537Smarcel
600161537Smarcel      for (regi = 0; regi < 32; regi++)
601161537Smarcel	supply_register (FP0_REGNUM + regi, (char *) &regs->r32.fpr[regi]);
602161537Smarcel
603161537Smarcel      supply_register (PC_REGNUM, (char *) &regs->r32.iar);
604161537Smarcel      supply_register (tdep->ppc_ps_regnum, (char *) &regs->r32.msr);
605161537Smarcel      supply_register (tdep->ppc_cr_regnum, (char *) &regs->r32.cr);
606161537Smarcel      supply_register (tdep->ppc_lr_regnum, (char *) &regs->r32.lr);
607161537Smarcel      supply_register (tdep->ppc_ctr_regnum, (char *) &regs->r32.ctr);
608161537Smarcel      supply_register (tdep->ppc_xer_regnum, (char *) &regs->r32.xer);
609161537Smarcel      supply_register (tdep->ppc_fpscr_regnum, (char *) &regs->r32.fpscr);
610161537Smarcel      if (tdep->ppc_mq_regnum >= 0)
611161537Smarcel	supply_register (tdep->ppc_mq_regnum, (char *) &regs->r32.mq);
612161537Smarcel    }
613161537Smarcel}
614161537Smarcel
615161537Smarcel
616161537Smarcel/* Copy information about text and data sections from LDI to VP for a 64-bit
617161537Smarcel   process if ARCH64 and for a 32-bit process otherwise. */
618161537Smarcel
619161537Smarcelstatic void
620161537Smarcelvmap_secs (struct vmap *vp, LdInfo *ldi, int arch64)
621161537Smarcel{
622161537Smarcel  if (arch64)
623161537Smarcel    {
624161537Smarcel      vp->tstart = (CORE_ADDR) ldi->l64.ldinfo_textorg;
625161537Smarcel      vp->tend = vp->tstart + ldi->l64.ldinfo_textsize;
626161537Smarcel      vp->dstart = (CORE_ADDR) ldi->l64.ldinfo_dataorg;
627161537Smarcel      vp->dend = vp->dstart + ldi->l64.ldinfo_datasize;
628161537Smarcel    }
629161537Smarcel  else
630161537Smarcel    {
631161537Smarcel      vp->tstart = (unsigned long) ldi->l32.ldinfo_textorg;
632161537Smarcel      vp->tend = vp->tstart + ldi->l32.ldinfo_textsize;
633161537Smarcel      vp->dstart = (unsigned long) ldi->l32.ldinfo_dataorg;
634161537Smarcel      vp->dend = vp->dstart + ldi->l32.ldinfo_datasize;
635161537Smarcel    }
636161537Smarcel
637161537Smarcel  /* The run time loader maps the file header in addition to the text
638161537Smarcel     section and returns a pointer to the header in ldinfo_textorg.
639161537Smarcel     Adjust the text start address to point to the real start address
640161537Smarcel     of the text section.  */
641161537Smarcel  vp->tstart += vp->toffs;
642161537Smarcel}
643161537Smarcel
644161537Smarcel/* handle symbol translation on vmapping */
645161537Smarcel
646161537Smarcelstatic void
647161537Smarcelvmap_symtab (struct vmap *vp)
648161537Smarcel{
649161537Smarcel  struct objfile *objfile;
650161537Smarcel  struct section_offsets *new_offsets;
651161537Smarcel  int i;
652161537Smarcel
653161537Smarcel  objfile = vp->objfile;
654161537Smarcel  if (objfile == NULL)
655161537Smarcel    {
656161537Smarcel      /* OK, it's not an objfile we opened ourselves.
657161537Smarcel         Currently, that can only happen with the exec file, so
658161537Smarcel         relocate the symbols for the symfile.  */
659161537Smarcel      if (symfile_objfile == NULL)
660161537Smarcel	return;
661161537Smarcel      objfile = symfile_objfile;
662161537Smarcel    }
663161537Smarcel  else if (!vp->loaded)
664161537Smarcel    /* If symbols are not yet loaded, offsets are not yet valid. */
665161537Smarcel    return;
666161537Smarcel
667161537Smarcel  new_offsets =
668161537Smarcel    (struct section_offsets *)
669161537Smarcel    alloca (SIZEOF_N_SECTION_OFFSETS (objfile->num_sections));
670161537Smarcel
671161537Smarcel  for (i = 0; i < objfile->num_sections; ++i)
672161537Smarcel    new_offsets->offsets[i] = ANOFFSET (objfile->section_offsets, i);
673161537Smarcel
674161537Smarcel  /* The symbols in the object file are linked to the VMA of the section,
675161537Smarcel     relocate them VMA relative.  */
676161537Smarcel  new_offsets->offsets[SECT_OFF_TEXT (objfile)] = vp->tstart - vp->tvma;
677161537Smarcel  new_offsets->offsets[SECT_OFF_DATA (objfile)] = vp->dstart - vp->dvma;
678161537Smarcel  new_offsets->offsets[SECT_OFF_BSS (objfile)] = vp->dstart - vp->dvma;
679161537Smarcel
680161537Smarcel  objfile_relocate (objfile, new_offsets);
681161537Smarcel}
682161537Smarcel
683161537Smarcel/* Add symbols for an objfile.  */
684161537Smarcel
685161537Smarcelstatic int
686161537Smarcelobjfile_symbol_add (void *arg)
687161537Smarcel{
688161537Smarcel  struct objfile *obj = (struct objfile *) arg;
689161537Smarcel
690161537Smarcel  syms_from_objfile (obj, NULL, 0, 0, 0, 0);
691161537Smarcel  new_symfile_objfile (obj, 0, 0);
692161537Smarcel  return 1;
693161537Smarcel}
694161537Smarcel
695161537Smarcel/* Add symbols for a vmap. Return zero upon error.  */
696161537Smarcel
697161537Smarcelint
698161537Smarcelvmap_add_symbols (struct vmap *vp)
699161537Smarcel{
700161537Smarcel  if (catch_errors (objfile_symbol_add, vp->objfile,
701161537Smarcel		    "Error while reading shared library symbols:\n",
702161537Smarcel		    RETURN_MASK_ALL))
703161537Smarcel    {
704161537Smarcel      /* Note this is only done if symbol reading was successful.  */
705161537Smarcel      vp->loaded = 1;
706161537Smarcel      vmap_symtab (vp);
707161537Smarcel      return 1;
708161537Smarcel    }
709161537Smarcel  return 0;
710161537Smarcel}
711161537Smarcel
712161537Smarcel/* Add a new vmap entry based on ldinfo() information.
713161537Smarcel
714161537Smarcel   If ldi->ldinfo_fd is not valid (e.g. this struct ld_info is from a
715161537Smarcel   core file), the caller should set it to -1, and we will open the file.
716161537Smarcel
717161537Smarcel   Return the vmap new entry.  */
718161537Smarcel
719161537Smarcelstatic struct vmap *
720161537Smarceladd_vmap (LdInfo *ldi)
721161537Smarcel{
722161537Smarcel  bfd *abfd, *last;
723161537Smarcel  char *mem, *objname, *filename;
724161537Smarcel  struct objfile *obj;
725161537Smarcel  struct vmap *vp;
726161537Smarcel  int fd;
727161537Smarcel  ARCH64_DECL (arch64);
728161537Smarcel
729161537Smarcel  /* This ldi structure was allocated using alloca() in
730161537Smarcel     xcoff_relocate_symtab(). Now we need to have persistent object
731161537Smarcel     and member names, so we should save them. */
732161537Smarcel
733161537Smarcel  filename = LDI_FILENAME (ldi, arch64);
734161537Smarcel  mem = filename + strlen (filename) + 1;
735161537Smarcel  mem = savestring (mem, strlen (mem));
736161537Smarcel  objname = savestring (filename, strlen (filename));
737161537Smarcel
738161537Smarcel  fd = LDI_FD (ldi, arch64);
739161537Smarcel  if (fd < 0)
740161537Smarcel    /* Note that this opens it once for every member; a possible
741161537Smarcel       enhancement would be to only open it once for every object.  */
742161537Smarcel    abfd = bfd_openr (objname, gnutarget);
743161537Smarcel  else
744161537Smarcel    abfd = bfd_fdopenr (objname, gnutarget, fd);
745161537Smarcel  if (!abfd)
746161537Smarcel    {
747161537Smarcel      warning ("Could not open `%s' as an executable file: %s",
748161537Smarcel	       objname, bfd_errmsg (bfd_get_error ()));
749161537Smarcel      return NULL;
750161537Smarcel    }
751161537Smarcel
752161537Smarcel  /* make sure we have an object file */
753161537Smarcel
754161537Smarcel  if (bfd_check_format (abfd, bfd_object))
755161537Smarcel    vp = map_vmap (abfd, 0);
756161537Smarcel
757161537Smarcel  else if (bfd_check_format (abfd, bfd_archive))
758161537Smarcel    {
759161537Smarcel      last = 0;
760161537Smarcel      /* FIXME??? am I tossing BFDs?  bfd? */
761161537Smarcel      while ((last = bfd_openr_next_archived_file (abfd, last)))
762161537Smarcel	if (DEPRECATED_STREQ (mem, last->filename))
763161537Smarcel	  break;
764161537Smarcel
765161537Smarcel      if (!last)
766161537Smarcel	{
767161537Smarcel	  warning ("\"%s\": member \"%s\" missing.", objname, mem);
768161537Smarcel	  bfd_close (abfd);
769161537Smarcel	  return NULL;
770161537Smarcel	}
771161537Smarcel
772161537Smarcel      if (!bfd_check_format (last, bfd_object))
773161537Smarcel	{
774161537Smarcel	  warning ("\"%s\": member \"%s\" not in executable format: %s.",
775161537Smarcel		   objname, mem, bfd_errmsg (bfd_get_error ()));
776161537Smarcel	  bfd_close (last);
777161537Smarcel	  bfd_close (abfd);
778161537Smarcel	  return NULL;
779161537Smarcel	}
780161537Smarcel
781161537Smarcel      vp = map_vmap (last, abfd);
782161537Smarcel    }
783161537Smarcel  else
784161537Smarcel    {
785161537Smarcel      warning ("\"%s\": not in executable format: %s.",
786161537Smarcel	       objname, bfd_errmsg (bfd_get_error ()));
787161537Smarcel      bfd_close (abfd);
788161537Smarcel      return NULL;
789161537Smarcel    }
790161537Smarcel  obj = allocate_objfile (vp->bfd, 0);
791161537Smarcel  vp->objfile = obj;
792161537Smarcel
793161537Smarcel  /* Always add symbols for the main objfile.  */
794161537Smarcel  if (vp == vmap || auto_solib_add)
795161537Smarcel    vmap_add_symbols (vp);
796161537Smarcel  return vp;
797161537Smarcel}
798161537Smarcel
799161537Smarcel/* update VMAP info with ldinfo() information
800161537Smarcel   Input is ptr to ldinfo() results.  */
801161537Smarcel
802161537Smarcelstatic void
803161537Smarcelvmap_ldinfo (LdInfo *ldi)
804161537Smarcel{
805161537Smarcel  struct stat ii, vi;
806161537Smarcel  struct vmap *vp;
807161537Smarcel  int got_one, retried;
808161537Smarcel  int got_exec_file = 0;
809161537Smarcel  uint next;
810161537Smarcel  int arch64 = ARCH64 ();
811161537Smarcel
812161537Smarcel  /* For each *ldi, see if we have a corresponding *vp.
813161537Smarcel     If so, update the mapping, and symbol table.
814161537Smarcel     If not, add an entry and symbol table.  */
815161537Smarcel
816161537Smarcel  do
817161537Smarcel    {
818161537Smarcel      char *name = LDI_FILENAME (ldi, arch64);
819161537Smarcel      char *memb = name + strlen (name) + 1;
820161537Smarcel      int fd = LDI_FD (ldi, arch64);
821161537Smarcel
822161537Smarcel      retried = 0;
823161537Smarcel
824161537Smarcel      if (fstat (fd, &ii) < 0)
825161537Smarcel	{
826161537Smarcel	  /* The kernel sets ld_info to -1, if the process is still using the
827161537Smarcel	     object, and the object is removed. Keep the symbol info for the
828161537Smarcel	     removed object and issue a warning.  */
829161537Smarcel	  warning ("%s (fd=%d) has disappeared, keeping its symbols",
830161537Smarcel		   name, fd);
831161537Smarcel	  continue;
832161537Smarcel	}
833161537Smarcel    retry:
834161537Smarcel      for (got_one = 0, vp = vmap; vp; vp = vp->nxt)
835161537Smarcel	{
836161537Smarcel	  struct objfile *objfile;
837161537Smarcel
838161537Smarcel	  /* First try to find a `vp', which is the same as in ldinfo.
839161537Smarcel	     If not the same, just continue and grep the next `vp'. If same,
840161537Smarcel	     relocate its tstart, tend, dstart, dend values. If no such `vp'
841161537Smarcel	     found, get out of this for loop, add this ldi entry as a new vmap
842161537Smarcel	     (add_vmap) and come back, find its `vp' and so on... */
843161537Smarcel
844161537Smarcel	  /* The filenames are not always sufficient to match on. */
845161537Smarcel
846161537Smarcel	  if ((name[0] == '/' && !DEPRECATED_STREQ (name, vp->name))
847161537Smarcel	      || (memb[0] && !DEPRECATED_STREQ (memb, vp->member)))
848161537Smarcel	    continue;
849161537Smarcel
850161537Smarcel	  /* See if we are referring to the same file.
851161537Smarcel	     We have to check objfile->obfd, symfile.c:reread_symbols might
852161537Smarcel	     have updated the obfd after a change.  */
853161537Smarcel	  objfile = vp->objfile == NULL ? symfile_objfile : vp->objfile;
854161537Smarcel	  if (objfile == NULL
855161537Smarcel	      || objfile->obfd == NULL
856161537Smarcel	      || bfd_stat (objfile->obfd, &vi) < 0)
857161537Smarcel	    {
858161537Smarcel	      warning ("Unable to stat %s, keeping its symbols", name);
859161537Smarcel	      continue;
860161537Smarcel	    }
861161537Smarcel
862161537Smarcel	  if (ii.st_dev != vi.st_dev || ii.st_ino != vi.st_ino)
863161537Smarcel	    continue;
864161537Smarcel
865161537Smarcel	  if (!retried)
866161537Smarcel	    close (fd);
867161537Smarcel
868161537Smarcel	  ++got_one;
869161537Smarcel
870161537Smarcel	  /* Found a corresponding VMAP.  Remap!  */
871161537Smarcel
872161537Smarcel	  vmap_secs (vp, ldi, arch64);
873161537Smarcel
874161537Smarcel	  /* The objfile is only NULL for the exec file.  */
875161537Smarcel	  if (vp->objfile == NULL)
876161537Smarcel	    got_exec_file = 1;
877161537Smarcel
878161537Smarcel	  /* relocate symbol table(s). */
879161537Smarcel	  vmap_symtab (vp);
880161537Smarcel
881161537Smarcel	  /* Announce new object files.  Doing this after symbol relocation
882161537Smarcel	     makes aix-thread.c's job easier. */
883161537Smarcel	  if (target_new_objfile_hook && vp->objfile)
884161537Smarcel	    target_new_objfile_hook (vp->objfile);
885161537Smarcel
886161537Smarcel	  /* There may be more, so we don't break out of the loop.  */
887161537Smarcel	}
888161537Smarcel
889161537Smarcel      /* if there was no matching *vp, we must perforce create the sucker(s) */
890161537Smarcel      if (!got_one && !retried)
891161537Smarcel	{
892161537Smarcel	  add_vmap (ldi);
893161537Smarcel	  ++retried;
894161537Smarcel	  goto retry;
895161537Smarcel	}
896161537Smarcel    }
897161537Smarcel  while ((next = LDI_NEXT (ldi, arch64))
898161537Smarcel	 && (ldi = (void *) (next + (char *) ldi)));
899161537Smarcel
900161537Smarcel  /* If we don't find the symfile_objfile anywhere in the ldinfo, it
901161537Smarcel     is unlikely that the symbol file is relocated to the proper
902161537Smarcel     address.  And we might have attached to a process which is
903161537Smarcel     running a different copy of the same executable.  */
904161537Smarcel  if (symfile_objfile != NULL && !got_exec_file)
905161537Smarcel    {
906161537Smarcel      warning ("Symbol file %s\nis not mapped; discarding it.\n\
907161537SmarcelIf in fact that file has symbols which the mapped files listed by\n\
908161537Smarcel\"info files\" lack, you can load symbols with the \"symbol-file\" or\n\
909161537Smarcel\"add-symbol-file\" commands (note that you must take care of relocating\n\
910161537Smarcelsymbols to the proper address).",
911161537Smarcel	       symfile_objfile->name);
912161537Smarcel      free_objfile (symfile_objfile);
913161537Smarcel      symfile_objfile = NULL;
914161537Smarcel    }
915161537Smarcel  breakpoint_re_set ();
916161537Smarcel}
917161537Smarcel
918161537Smarcel/* As well as symbol tables, exec_sections need relocation. After
919161537Smarcel   the inferior process' termination, there will be a relocated symbol
920161537Smarcel   table exist with no corresponding inferior process. At that time, we
921161537Smarcel   need to use `exec' bfd, rather than the inferior process's memory space
922161537Smarcel   to look up symbols.
923161537Smarcel
924161537Smarcel   `exec_sections' need to be relocated only once, as long as the exec
925161537Smarcel   file remains unchanged.
926161537Smarcel */
927161537Smarcel
928161537Smarcelstatic void
929161537Smarcelvmap_exec (void)
930161537Smarcel{
931161537Smarcel  static bfd *execbfd;
932161537Smarcel  int i;
933161537Smarcel
934161537Smarcel  if (execbfd == exec_bfd)
935161537Smarcel    return;
936161537Smarcel
937161537Smarcel  execbfd = exec_bfd;
938161537Smarcel
939161537Smarcel  if (!vmap || !exec_ops.to_sections)
940161537Smarcel    error ("vmap_exec: vmap or exec_ops.to_sections == 0\n");
941161537Smarcel
942161537Smarcel  for (i = 0; &exec_ops.to_sections[i] < exec_ops.to_sections_end; i++)
943161537Smarcel    {
944161537Smarcel      if (DEPRECATED_STREQ (".text", exec_ops.to_sections[i].the_bfd_section->name))
945161537Smarcel	{
946161537Smarcel	  exec_ops.to_sections[i].addr += vmap->tstart - vmap->tvma;
947161537Smarcel	  exec_ops.to_sections[i].endaddr += vmap->tstart - vmap->tvma;
948161537Smarcel	}
949161537Smarcel      else if (DEPRECATED_STREQ (".data", exec_ops.to_sections[i].the_bfd_section->name))
950161537Smarcel	{
951161537Smarcel	  exec_ops.to_sections[i].addr += vmap->dstart - vmap->dvma;
952161537Smarcel	  exec_ops.to_sections[i].endaddr += vmap->dstart - vmap->dvma;
953161537Smarcel	}
954161537Smarcel      else if (DEPRECATED_STREQ (".bss", exec_ops.to_sections[i].the_bfd_section->name))
955161537Smarcel	{
956161537Smarcel	  exec_ops.to_sections[i].addr += vmap->dstart - vmap->dvma;
957161537Smarcel	  exec_ops.to_sections[i].endaddr += vmap->dstart - vmap->dvma;
958161537Smarcel	}
959161537Smarcel    }
960161537Smarcel}
961161537Smarcel
962161537Smarcel/* Set the current architecture from the host running GDB.  Called when
963161537Smarcel   starting a child process. */
964161537Smarcel
965161537Smarcelstatic void
966161537Smarcelset_host_arch (int pid)
967161537Smarcel{
968161537Smarcel  enum bfd_architecture arch;
969161537Smarcel  unsigned long mach;
970161537Smarcel  bfd abfd;
971161537Smarcel  struct gdbarch_info info;
972161537Smarcel
973161537Smarcel  if (__power_rs ())
974161537Smarcel    {
975161537Smarcel      arch = bfd_arch_rs6000;
976161537Smarcel      mach = bfd_mach_rs6k;
977161537Smarcel    }
978161537Smarcel  else
979161537Smarcel    {
980161537Smarcel      arch = bfd_arch_powerpc;
981161537Smarcel      mach = bfd_mach_ppc;
982161537Smarcel    }
983161537Smarcel
984161537Smarcel  /* FIXME: schauer/2002-02-25:
985161537Smarcel     We don't know if we are executing a 32 or 64 bit executable,
986161537Smarcel     and have no way to pass the proper word size to rs6000_gdbarch_init.
987161537Smarcel     So we have to avoid switching to a new architecture, if the architecture
988161537Smarcel     matches already.
989161537Smarcel     Blindly calling rs6000_gdbarch_init used to work in older versions of
990161537Smarcel     GDB, as rs6000_gdbarch_init incorrectly used the previous tdep to
991161537Smarcel     determine the wordsize.  */
992161537Smarcel  if (exec_bfd)
993161537Smarcel    {
994161537Smarcel      const struct bfd_arch_info *exec_bfd_arch_info;
995161537Smarcel
996161537Smarcel      exec_bfd_arch_info = bfd_get_arch_info (exec_bfd);
997161537Smarcel      if (arch == exec_bfd_arch_info->arch)
998161537Smarcel	return;
999161537Smarcel    }
1000161537Smarcel
1001161537Smarcel  bfd_default_set_arch_mach (&abfd, arch, mach);
1002161537Smarcel
1003161537Smarcel  gdbarch_info_init (&info);
1004161537Smarcel  info.bfd_arch_info = bfd_get_arch_info (&abfd);
1005161537Smarcel  info.abfd = exec_bfd;
1006161537Smarcel
1007161537Smarcel  if (!gdbarch_update_p (info))
1008161537Smarcel    {
1009161537Smarcel      internal_error (__FILE__, __LINE__,
1010161537Smarcel		      "set_host_arch: failed to select architecture");
1011161537Smarcel    }
1012161537Smarcel}
1013161537Smarcel
1014161537Smarcel
1015161537Smarcel/* xcoff_relocate_symtab -      hook for symbol table relocation.
1016161537Smarcel   also reads shared libraries.. */
1017161537Smarcel
1018161537Smarcelvoid
1019161537Smarcelxcoff_relocate_symtab (unsigned int pid)
1020161537Smarcel{
1021161537Smarcel  int load_segs = 64; /* number of load segments */
1022161537Smarcel  int rc;
1023161537Smarcel  LdInfo *ldi = NULL;
1024161537Smarcel  int arch64 = ARCH64 ();
1025161537Smarcel  int ldisize = arch64 ? sizeof (ldi->l64) : sizeof (ldi->l32);
1026161537Smarcel  int size;
1027161537Smarcel
1028161537Smarcel  do
1029161537Smarcel    {
1030161537Smarcel      size = load_segs * ldisize;
1031161537Smarcel      ldi = (void *) xrealloc (ldi, size);
1032161537Smarcel
1033161537Smarcel#if 0
1034161537Smarcel      /* According to my humble theory, AIX has some timing problems and
1035161537Smarcel         when the user stack grows, kernel doesn't update stack info in time
1036161537Smarcel         and ptrace calls step on user stack. That is why we sleep here a
1037161537Smarcel         little, and give kernel to update its internals. */
1038161537Smarcel      usleep (36000);
1039161537Smarcel#endif
1040161537Smarcel
1041161537Smarcel      if (arch64)
1042161537Smarcel	rc = rs6000_ptrace64 (PT_LDINFO, pid, (unsigned long) ldi, size, NULL);
1043161537Smarcel      else
1044161537Smarcel	rc = rs6000_ptrace32 (PT_LDINFO, pid, (int *) ldi, size, NULL);
1045161537Smarcel
1046161537Smarcel      if (rc == -1)
1047161537Smarcel        {
1048161537Smarcel          if (errno == ENOMEM)
1049161537Smarcel            load_segs *= 2;
1050161537Smarcel          else
1051161537Smarcel            perror_with_name ("ptrace ldinfo");
1052161537Smarcel        }
1053161537Smarcel      else
1054161537Smarcel	{
1055161537Smarcel          vmap_ldinfo (ldi);
1056161537Smarcel          vmap_exec (); /* relocate the exec and core sections as well. */
1057161537Smarcel	}
1058161537Smarcel    } while (rc == -1);
1059161537Smarcel  if (ldi)
1060161537Smarcel    xfree (ldi);
1061161537Smarcel}
1062161537Smarcel
1063161537Smarcel/* Core file stuff.  */
1064161537Smarcel
1065161537Smarcel/* Relocate symtabs and read in shared library info, based on symbols
1066161537Smarcel   from the core file.  */
1067161537Smarcel
1068161537Smarcelvoid
1069161537Smarcelxcoff_relocate_core (struct target_ops *target)
1070161537Smarcel{
1071161537Smarcel  struct bfd_section *ldinfo_sec;
1072161537Smarcel  int offset = 0;
1073161537Smarcel  LdInfo *ldi;
1074161537Smarcel  struct vmap *vp;
1075161537Smarcel  int arch64 = ARCH64 ();
1076161537Smarcel
1077161537Smarcel  /* Size of a struct ld_info except for the variable-length filename. */
1078161537Smarcel  int nonfilesz = (int)LDI_FILENAME ((LdInfo *)0, arch64);
1079161537Smarcel
1080161537Smarcel  /* Allocated size of buffer.  */
1081161537Smarcel  int buffer_size = nonfilesz;
1082161537Smarcel  char *buffer = xmalloc (buffer_size);
1083161537Smarcel  struct cleanup *old = make_cleanup (free_current_contents, &buffer);
1084161537Smarcel
1085161537Smarcel  ldinfo_sec = bfd_get_section_by_name (core_bfd, ".ldinfo");
1086161537Smarcel  if (ldinfo_sec == NULL)
1087161537Smarcel    {
1088161537Smarcel    bfd_err:
1089161537Smarcel      fprintf_filtered (gdb_stderr, "Couldn't get ldinfo from core file: %s\n",
1090161537Smarcel			bfd_errmsg (bfd_get_error ()));
1091161537Smarcel      do_cleanups (old);
1092161537Smarcel      return;
1093161537Smarcel    }
1094161537Smarcel  do
1095161537Smarcel    {
1096161537Smarcel      int i;
1097161537Smarcel      int names_found = 0;
1098161537Smarcel
1099161537Smarcel      /* Read in everything but the name.  */
1100161537Smarcel      if (bfd_get_section_contents (core_bfd, ldinfo_sec, buffer,
1101161537Smarcel				    offset, nonfilesz) == 0)
1102161537Smarcel	goto bfd_err;
1103161537Smarcel
1104161537Smarcel      /* Now the name.  */
1105161537Smarcel      i = nonfilesz;
1106161537Smarcel      do
1107161537Smarcel	{
1108161537Smarcel	  if (i == buffer_size)
1109161537Smarcel	    {
1110161537Smarcel	      buffer_size *= 2;
1111161537Smarcel	      buffer = xrealloc (buffer, buffer_size);
1112161537Smarcel	    }
1113161537Smarcel	  if (bfd_get_section_contents (core_bfd, ldinfo_sec, &buffer[i],
1114161537Smarcel					offset + i, 1) == 0)
1115161537Smarcel	    goto bfd_err;
1116161537Smarcel	  if (buffer[i++] == '\0')
1117161537Smarcel	    ++names_found;
1118161537Smarcel	}
1119161537Smarcel      while (names_found < 2);
1120161537Smarcel
1121161537Smarcel      ldi = (LdInfo *) buffer;
1122161537Smarcel
1123161537Smarcel      /* Can't use a file descriptor from the core file; need to open it.  */
1124161537Smarcel      if (arch64)
1125161537Smarcel	ldi->l64.ldinfo_fd = -1;
1126161537Smarcel      else
1127161537Smarcel	ldi->l32.ldinfo_fd = -1;
1128161537Smarcel
1129161537Smarcel      /* The first ldinfo is for the exec file, allocated elsewhere.  */
1130161537Smarcel      if (offset == 0 && vmap != NULL)
1131161537Smarcel	vp = vmap;
1132161537Smarcel      else
1133161537Smarcel	vp = add_vmap (ldi);
1134161537Smarcel
1135161537Smarcel      /* Process next shared library upon error. */
1136161537Smarcel      offset += LDI_NEXT (ldi, arch64);
1137161537Smarcel      if (vp == NULL)
1138161537Smarcel	continue;
1139161537Smarcel
1140161537Smarcel      vmap_secs (vp, ldi, arch64);
1141161537Smarcel
1142161537Smarcel      /* Unless this is the exec file,
1143161537Smarcel         add our sections to the section table for the core target.  */
1144161537Smarcel      if (vp != vmap)
1145161537Smarcel	{
1146161537Smarcel	  struct section_table *stp;
1147161537Smarcel
1148161537Smarcel	  target_resize_to_sections (target, 2);
1149161537Smarcel	  stp = target->to_sections_end - 2;
1150161537Smarcel
1151161537Smarcel	  stp->bfd = vp->bfd;
1152161537Smarcel	  stp->the_bfd_section = bfd_get_section_by_name (stp->bfd, ".text");
1153161537Smarcel	  stp->addr = vp->tstart;
1154161537Smarcel	  stp->endaddr = vp->tend;
1155161537Smarcel	  stp++;
1156161537Smarcel
1157161537Smarcel	  stp->bfd = vp->bfd;
1158161537Smarcel	  stp->the_bfd_section = bfd_get_section_by_name (stp->bfd, ".data");
1159161537Smarcel	  stp->addr = vp->dstart;
1160161537Smarcel	  stp->endaddr = vp->dend;
1161161537Smarcel	}
1162161537Smarcel
1163161537Smarcel      vmap_symtab (vp);
1164161537Smarcel
1165161537Smarcel      if (target_new_objfile_hook && vp != vmap && vp->objfile)
1166161537Smarcel	target_new_objfile_hook (vp->objfile);
1167161537Smarcel    }
1168161537Smarcel  while (LDI_NEXT (ldi, arch64) != 0);
1169161537Smarcel  vmap_exec ();
1170161537Smarcel  breakpoint_re_set ();
1171161537Smarcel  do_cleanups (old);
1172161537Smarcel}
1173161537Smarcel
1174161537Smarcelint
1175161537Smarcelkernel_u_size (void)
1176161537Smarcel{
1177161537Smarcel  return (sizeof (struct user));
1178161537Smarcel}
1179161537Smarcel
1180161537Smarcel/* Under AIX, we have to pass the correct TOC pointer to a function
1181161537Smarcel   when calling functions in the inferior.
1182161537Smarcel   We try to find the relative toc offset of the objfile containing PC
1183161537Smarcel   and add the current load address of the data segment from the vmap.  */
1184161537Smarcel
1185161537Smarcelstatic CORE_ADDR
1186161537Smarcelfind_toc_address (CORE_ADDR pc)
1187161537Smarcel{
1188161537Smarcel  struct vmap *vp;
1189161537Smarcel  extern CORE_ADDR get_toc_offset (struct objfile *);	/* xcoffread.c */
1190161537Smarcel
1191161537Smarcel  for (vp = vmap; vp; vp = vp->nxt)
1192161537Smarcel    {
1193161537Smarcel      if (pc >= vp->tstart && pc < vp->tend)
1194161537Smarcel	{
1195161537Smarcel	  /* vp->objfile is only NULL for the exec file.  */
1196161537Smarcel	  return vp->dstart + get_toc_offset (vp->objfile == NULL
1197161537Smarcel					      ? symfile_objfile
1198161537Smarcel					      : vp->objfile);
1199161537Smarcel	}
1200161537Smarcel    }
1201161537Smarcel  error ("Unable to find TOC entry for pc %s\n", local_hex_string (pc));
1202161537Smarcel}
1203161537Smarcel
1204161537Smarcel/* Register that we are able to handle rs6000 core file formats. */
1205161537Smarcel
1206161537Smarcelstatic struct core_fns rs6000_core_fns =
1207161537Smarcel{
1208161537Smarcel  bfd_target_xcoff_flavour,		/* core_flavour */
1209161537Smarcel  default_check_format,			/* check_format */
1210161537Smarcel  default_core_sniffer,			/* core_sniffer */
1211161537Smarcel  fetch_core_registers,			/* core_read_registers */
1212161537Smarcel  NULL					/* next */
1213161537Smarcel};
1214161537Smarcel
1215161537Smarcelvoid
1216161537Smarcel_initialize_core_rs6000 (void)
1217161537Smarcel{
1218161537Smarcel  /* Initialize hook in rs6000-tdep.c for determining the TOC address when
1219161537Smarcel     calling functions in the inferior.  */
1220161537Smarcel  rs6000_find_toc_address_hook = find_toc_address;
1221161537Smarcel
1222161537Smarcel  /* Initialize hook in rs6000-tdep.c to set the current architecture when
1223161537Smarcel     starting a child process. */
1224161537Smarcel  rs6000_set_host_arch_hook = set_host_arch;
1225161537Smarcel
1226161537Smarcel  add_core_fns (&rs6000_core_fns);
1227161537Smarcel}
1228