update copyright year range in GDB files
[deliverable/binutils-gdb.git] / gdb / rs6000-nat.c
CommitLineData
c906108c 1/* IBM RS/6000 native-dependent code for GDB, the GNU debugger.
4646aa9d 2
61baf725 3 Copyright (C) 1986-2017 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
a9762ec7 9 the Free Software Foundation; either version 3 of the License, or
c5aa993b 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 17 You should have received a copy of the GNU General Public License
a9762ec7 18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c
SS
19
20#include "defs.h"
21#include "inferior.h"
22#include "target.h"
23#include "gdbcore.h"
c906108c
SS
24#include "symfile.h"
25#include "objfiles.h"
c906108c
SS
26#include "bfd.h"
27#include "gdb-stabs.h"
4e052eda 28#include "regcache.h"
19caaa45 29#include "arch-utils.h"
dab06dbe 30#include "inf-child.h"
037a727e 31#include "inf-ptrace.h"
11bf77db 32#include "ppc-tdep.h"
6f7f3f0d 33#include "rs6000-tdep.h"
356a5233 34#include "rs6000-aix-tdep.h"
4646aa9d 35#include "exec.h"
06d3b283 36#include "observer.h"
63807e1d 37#include "xcoffread.h"
c906108c
SS
38
39#include <sys/ptrace.h>
40#include <sys/reg.h>
41
c906108c
SS
42#include <sys/dir.h>
43#include <sys/user.h>
44#include <signal.h>
45#include <sys/ioctl.h>
46#include <fcntl.h>
47
48#include <a.out.h>
49#include <sys/file.h>
53ce3c39 50#include <sys/stat.h>
92107356 51#include "gdb_bfd.h"
c906108c 52#include <sys/core.h>
7a78ae4e
ND
53#define __LDINFO_PTRACE32__ /* for __ld_info32 */
54#define __LDINFO_PTRACE64__ /* for __ld_info64 */
c906108c 55#include <sys/ldr.h>
7a78ae4e 56#include <sys/systemcfg.h>
c906108c 57
7a78ae4e
ND
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
0df8b418 60 accessing fields in the appropriate structures. */
7a78ae4e
ND
61
62/* In 32-bit compilation mode (which is the only mode from which ptrace()
0df8b418 63 works on 4.3), __ld_info32 is #defined as equivalent to ld_info. */
7a78ae4e 64
b08ee99f 65#if defined (__ld_info32) || defined (__ld_info64)
7a78ae4e
ND
66# define ARCH3264
67#endif
68
0df8b418 69/* Return whether the current architecture is 64-bit. */
7a78ae4e
ND
70
71#ifndef ARCH3264
72# define ARCH64() 0
73#else
f5656ead 74# define ARCH64() (register_size (target_gdbarch (), 0) == 8)
7a78ae4e
ND
75#endif
76
05804640 77static target_xfer_partial_ftype rs6000_xfer_shared_libraries;
4d1eb6b4 78
dd7be90a
KB
79/* Given REGNO, a gdb register number, return the corresponding
80 number suitable for use as a ptrace() parameter. Return -1 if
81 there's no suitable mapping. Also, set the int pointed to by
82 ISFLOAT to indicate whether REGNO is a floating point register. */
c906108c 83
dd7be90a 84static int
206988c4 85regmap (struct gdbarch *gdbarch, int regno, int *isfloat)
c5aa993b 86{
206988c4 87 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
dd7be90a
KB
88
89 *isfloat = 0;
8bf659e8
JB
90 if (tdep->ppc_gp0_regnum <= regno
91 && regno < tdep->ppc_gp0_regnum + ppc_num_gprs)
dd7be90a 92 return regno;
383f0f5b
JB
93 else if (tdep->ppc_fp0_regnum >= 0
94 && tdep->ppc_fp0_regnum <= regno
366f009f 95 && regno < tdep->ppc_fp0_regnum + ppc_num_fprs)
dd7be90a
KB
96 {
97 *isfloat = 1;
366f009f 98 return regno - tdep->ppc_fp0_regnum + FPR0;
dd7be90a 99 }
206988c4 100 else if (regno == gdbarch_pc_regnum (gdbarch))
dd7be90a
KB
101 return IAR;
102 else if (regno == tdep->ppc_ps_regnum)
103 return MSR;
104 else if (regno == tdep->ppc_cr_regnum)
105 return CR;
106 else if (regno == tdep->ppc_lr_regnum)
107 return LR;
108 else if (regno == tdep->ppc_ctr_regnum)
109 return CTR;
110 else if (regno == tdep->ppc_xer_regnum)
111 return XER;
383f0f5b
JB
112 else if (tdep->ppc_fpscr_regnum >= 0
113 && regno == tdep->ppc_fpscr_regnum)
0e061eef 114 return FPSCR;
dd7be90a
KB
115 else if (tdep->ppc_mq_regnum >= 0 && regno == tdep->ppc_mq_regnum)
116 return MQ;
117 else
118 return -1;
119}
c906108c 120
0df8b418 121/* Call ptrace(REQ, ID, ADDR, DATA, BUF). */
c906108c 122
7a78ae4e 123static int
8b5790f2 124rs6000_ptrace32 (int req, int id, int *addr, int data, int *buf)
7a78ae4e 125{
1ed3ee94 126#ifdef HAVE_PTRACE64
11cb8762 127 int ret = ptrace64 (req, id, (uintptr_t) addr, data, buf);
1ed3ee94 128#else
7a78ae4e 129 int ret = ptrace (req, id, (int *)addr, data, buf);
1ed3ee94 130#endif
7a78ae4e 131#if 0
8b5790f2 132 printf ("rs6000_ptrace32 (%d, %d, 0x%x, %08x, 0x%x) = 0x%x\n",
7a78ae4e
ND
133 req, id, (unsigned int)addr, data, (unsigned int)buf, ret);
134#endif
135 return ret;
136}
c906108c 137
0df8b418 138/* Call ptracex(REQ, ID, ADDR, DATA, BUF). */
c906108c 139
7a78ae4e 140static int
0d16ee5d 141rs6000_ptrace64 (int req, int id, long long addr, int data, void *buf)
7a78ae4e
ND
142{
143#ifdef ARCH3264
1ed3ee94 144# ifdef HAVE_PTRACE64
71829b1a 145 int ret = ptrace64 (req, id, addr, data, (PTRACE_TYPE_ARG5) buf);
1ed3ee94 146# else
71829b1a 147 int ret = ptracex (req, id, addr, data, (PTRACE_TYPE_ARG5) buf);
1ed3ee94 148# endif
7a78ae4e
ND
149#else
150 int ret = 0;
151#endif
152#if 0
2244ba2e
PM
153 printf ("rs6000_ptrace64 (%d, %d, %s, %08x, 0x%x) = 0x%x\n",
154 req, id, hex_string (addr), data, (unsigned int)buf, ret);
7a78ae4e
ND
155#endif
156 return ret;
157}
c906108c 158
0df8b418 159/* Fetch register REGNO from the inferior. */
c906108c 160
7a78ae4e 161static void
56be3814 162fetch_register (struct regcache *regcache, int regno)
7a78ae4e 163{
8b164abb 164 struct gdbarch *gdbarch = get_regcache_arch (regcache);
d9d9c31f 165 int addr[MAX_REGISTER_SIZE];
dd7be90a 166 int nr, isfloat;
c906108c 167
0df8b418 168 /* Retrieved values may be -1, so infer errors from errno. */
7a78ae4e 169 errno = 0;
c906108c 170
206988c4 171 nr = regmap (gdbarch, regno, &isfloat);
dd7be90a 172
0df8b418 173 /* Floating-point registers. */
dd7be90a 174 if (isfloat)
dfd4cc63 175 rs6000_ptrace32 (PT_READ_FPR, ptid_get_pid (inferior_ptid), addr, nr, 0);
c906108c 176
0df8b418 177 /* Bogus register number. */
dd7be90a 178 else if (nr < 0)
2a18e3d9 179 {
8b164abb 180 if (regno >= gdbarch_num_regs (gdbarch))
2a18e3d9
EZ
181 fprintf_unfiltered (gdb_stderr,
182 "gdb error: register no %d not implemented.\n",
183 regno);
dd7be90a 184 return;
2a18e3d9 185 }
c906108c 186
0df8b418 187 /* Fixed-point registers. */
7a78ae4e
ND
188 else
189 {
7a78ae4e 190 if (!ARCH64 ())
dfd4cc63 191 *addr = rs6000_ptrace32 (PT_READ_GPR, ptid_get_pid (inferior_ptid),
0df8b418 192 (int *) nr, 0, 0);
7a78ae4e
ND
193 else
194 {
195 /* PT_READ_GPR requires the buffer parameter to point to long long,
0df8b418 196 even if the register is really only 32 bits. */
7a78ae4e 197 long long buf;
dfd4cc63
LM
198 rs6000_ptrace64 (PT_READ_GPR, ptid_get_pid (inferior_ptid),
199 nr, 0, &buf);
8b164abb 200 if (register_size (gdbarch, regno) == 8)
7a78ae4e
ND
201 memcpy (addr, &buf, 8);
202 else
203 *addr = buf;
204 }
205 }
206
207 if (!errno)
56be3814 208 regcache_raw_supply (regcache, regno, (char *) addr);
7a78ae4e
ND
209 else
210 {
211#if 0
0df8b418 212 /* FIXME: this happens 3 times at the start of each 64-bit program. */
9b20d036 213 perror (_("ptrace read"));
7a78ae4e
ND
214#endif
215 errno = 0;
216 }
c906108c
SS
217}
218
0df8b418 219/* Store register REGNO back into the inferior. */
c906108c 220
7a78ae4e 221static void
fb14de7b 222store_register (struct regcache *regcache, int regno)
c906108c 223{
8b164abb 224 struct gdbarch *gdbarch = get_regcache_arch (regcache);
d9d9c31f 225 int addr[MAX_REGISTER_SIZE];
dd7be90a 226 int nr, isfloat;
c906108c 227
11bf77db 228 /* Fetch the register's value from the register cache. */
56be3814 229 regcache_raw_collect (regcache, regno, addr);
11bf77db 230
0df8b418 231 /* -1 can be a successful return value, so infer errors from errno. */
c906108c
SS
232 errno = 0;
233
206988c4 234 nr = regmap (gdbarch, regno, &isfloat);
dd7be90a 235
0df8b418 236 /* Floating-point registers. */
dd7be90a 237 if (isfloat)
dfd4cc63 238 rs6000_ptrace32 (PT_WRITE_FPR, ptid_get_pid (inferior_ptid), addr, nr, 0);
c906108c 239
0df8b418 240 /* Bogus register number. */
dd7be90a 241 else if (nr < 0)
7a78ae4e 242 {
8b164abb 243 if (regno >= gdbarch_num_regs (gdbarch))
7a78ae4e
ND
244 fprintf_unfiltered (gdb_stderr,
245 "gdb error: register no %d not implemented.\n",
246 regno);
247 }
c906108c 248
0df8b418 249 /* Fixed-point registers. */
7a78ae4e
ND
250 else
251 {
11bf77db
KB
252 /* The PT_WRITE_GPR operation is rather odd. For 32-bit inferiors,
253 the register's value is passed by value, but for 64-bit inferiors,
254 the address of a buffer containing the value is passed. */
7a78ae4e 255 if (!ARCH64 ())
dfd4cc63 256 rs6000_ptrace32 (PT_WRITE_GPR, ptid_get_pid (inferior_ptid),
0df8b418 257 (int *) nr, *addr, 0);
7a78ae4e 258 else
c906108c 259 {
7a78ae4e 260 /* PT_WRITE_GPR requires the buffer parameter to point to an 8-byte
0df8b418 261 area, even if the register is really only 32 bits. */
7a78ae4e 262 long long buf;
8b164abb 263 if (register_size (gdbarch, regno) == 8)
7a78ae4e
ND
264 memcpy (&buf, addr, 8);
265 else
266 buf = *addr;
dfd4cc63
LM
267 rs6000_ptrace64 (PT_WRITE_GPR, ptid_get_pid (inferior_ptid),
268 nr, 0, &buf);
c906108c
SS
269 }
270 }
271
7a78ae4e 272 if (errno)
c906108c 273 {
9b20d036 274 perror (_("ptrace write"));
7a78ae4e 275 errno = 0;
c906108c 276 }
7a78ae4e 277}
c906108c 278
7a78ae4e 279/* Read from the inferior all registers if REGNO == -1 and just register
0df8b418 280 REGNO otherwise. */
c906108c 281
037a727e 282static void
28439f5e
PA
283rs6000_fetch_inferior_registers (struct target_ops *ops,
284 struct regcache *regcache, int regno)
7a78ae4e 285{
8b164abb 286 struct gdbarch *gdbarch = get_regcache_arch (regcache);
7a78ae4e 287 if (regno != -1)
56be3814 288 fetch_register (regcache, regno);
7a78ae4e
ND
289
290 else
c906108c 291 {
8b164abb 292 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
7a78ae4e 293
dd7be90a
KB
294 /* Read 32 general purpose registers. */
295 for (regno = tdep->ppc_gp0_regnum;
8bf659e8 296 regno < tdep->ppc_gp0_regnum + ppc_num_gprs;
dd7be90a
KB
297 regno++)
298 {
56be3814 299 fetch_register (regcache, regno);
dd7be90a
KB
300 }
301
302 /* Read general purpose floating point registers. */
383f0f5b
JB
303 if (tdep->ppc_fp0_regnum >= 0)
304 for (regno = 0; regno < ppc_num_fprs; regno++)
56be3814 305 fetch_register (regcache, tdep->ppc_fp0_regnum + regno);
7a78ae4e 306
dd7be90a 307 /* Read special registers. */
8b164abb 308 fetch_register (regcache, gdbarch_pc_regnum (gdbarch));
56be3814
UW
309 fetch_register (regcache, tdep->ppc_ps_regnum);
310 fetch_register (regcache, tdep->ppc_cr_regnum);
311 fetch_register (regcache, tdep->ppc_lr_regnum);
312 fetch_register (regcache, tdep->ppc_ctr_regnum);
313 fetch_register (regcache, tdep->ppc_xer_regnum);
383f0f5b 314 if (tdep->ppc_fpscr_regnum >= 0)
56be3814 315 fetch_register (regcache, tdep->ppc_fpscr_regnum);
dd7be90a 316 if (tdep->ppc_mq_regnum >= 0)
56be3814 317 fetch_register (regcache, tdep->ppc_mq_regnum);
c906108c 318 }
7a78ae4e 319}
c906108c 320
7a78ae4e
ND
321/* Store our register values back into the inferior.
322 If REGNO is -1, do this for all registers.
323 Otherwise, REGNO specifies which register (so we can save time). */
324
037a727e 325static void
28439f5e
PA
326rs6000_store_inferior_registers (struct target_ops *ops,
327 struct regcache *regcache, int regno)
7a78ae4e 328{
8b164abb 329 struct gdbarch *gdbarch = get_regcache_arch (regcache);
7a78ae4e 330 if (regno != -1)
56be3814 331 store_register (regcache, regno);
7a78ae4e
ND
332
333 else
f6077098 334 {
8b164abb 335 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
dd7be90a
KB
336
337 /* Write general purpose registers first. */
338 for (regno = tdep->ppc_gp0_regnum;
8bf659e8 339 regno < tdep->ppc_gp0_regnum + ppc_num_gprs;
dd7be90a
KB
340 regno++)
341 {
56be3814 342 store_register (regcache, regno);
dd7be90a 343 }
7a78ae4e 344
dd7be90a 345 /* Write floating point registers. */
383f0f5b
JB
346 if (tdep->ppc_fp0_regnum >= 0)
347 for (regno = 0; regno < ppc_num_fprs; regno++)
56be3814 348 store_register (regcache, tdep->ppc_fp0_regnum + regno);
7a78ae4e 349
dd7be90a 350 /* Write special registers. */
8b164abb 351 store_register (regcache, gdbarch_pc_regnum (gdbarch));
56be3814
UW
352 store_register (regcache, tdep->ppc_ps_regnum);
353 store_register (regcache, tdep->ppc_cr_regnum);
354 store_register (regcache, tdep->ppc_lr_regnum);
355 store_register (regcache, tdep->ppc_ctr_regnum);
356 store_register (regcache, tdep->ppc_xer_regnum);
383f0f5b 357 if (tdep->ppc_fpscr_regnum >= 0)
56be3814 358 store_register (regcache, tdep->ppc_fpscr_regnum);
dd7be90a 359 if (tdep->ppc_mq_regnum >= 0)
56be3814 360 store_register (regcache, tdep->ppc_mq_regnum);
f6077098 361 }
7a78ae4e 362}
f6077098 363
edcc890f 364/* Implement the to_xfer_partial target_ops method. */
7a78ae4e 365
9b409511 366static enum target_xfer_status
037a727e
UW
367rs6000_xfer_partial (struct target_ops *ops, enum target_object object,
368 const char *annex, gdb_byte *readbuf,
369 const gdb_byte *writebuf,
9b409511 370 ULONGEST offset, ULONGEST len, ULONGEST *xfered_len)
7a78ae4e 371{
037a727e 372 pid_t pid = ptid_get_pid (inferior_ptid);
7a78ae4e 373 int arch64 = ARCH64 ();
7a78ae4e 374
037a727e 375 switch (object)
c906108c 376 {
ff99b71b 377 case TARGET_OBJECT_LIBRARIES_AIX:
4d1eb6b4
JB
378 return rs6000_xfer_shared_libraries (ops, object, annex,
379 readbuf, writebuf,
9b409511 380 offset, len, xfered_len);
037a727e
UW
381 case TARGET_OBJECT_MEMORY:
382 {
383 union
7a78ae4e 384 {
037a727e
UW
385 PTRACE_TYPE_RET word;
386 gdb_byte byte[sizeof (PTRACE_TYPE_RET)];
387 } buffer;
388 ULONGEST rounded_offset;
389 LONGEST partial_len;
390
391 /* Round the start offset down to the next long word
392 boundary. */
393 rounded_offset = offset & -(ULONGEST) sizeof (PTRACE_TYPE_RET);
394
395 /* Since ptrace will transfer a single word starting at that
396 rounded_offset the partial_len needs to be adjusted down to
397 that (remember this function only does a single transfer).
398 Should the required length be even less, adjust it down
399 again. */
400 partial_len = (rounded_offset + sizeof (PTRACE_TYPE_RET)) - offset;
401 if (partial_len > len)
402 partial_len = len;
403
404 if (writebuf)
405 {
406 /* If OFFSET:PARTIAL_LEN is smaller than
407 ROUNDED_OFFSET:WORDSIZE then a read/modify write will
408 be needed. Read in the entire word. */
409 if (rounded_offset < offset
410 || (offset + partial_len
411 < rounded_offset + sizeof (PTRACE_TYPE_RET)))
412 {
413 /* Need part of initial word -- fetch it. */
414 if (arch64)
415 buffer.word = rs6000_ptrace64 (PT_READ_I, pid,
416 rounded_offset, 0, NULL);
417 else
418 buffer.word = rs6000_ptrace32 (PT_READ_I, pid,
0df8b418
MS
419 (int *) (uintptr_t)
420 rounded_offset,
037a727e
UW
421 0, NULL);
422 }
423
424 /* Copy data to be written over corresponding part of
425 buffer. */
426 memcpy (buffer.byte + (offset - rounded_offset),
427 writebuf, partial_len);
428
429 errno = 0;
430 if (arch64)
431 rs6000_ptrace64 (PT_WRITE_D, pid,
432 rounded_offset, buffer.word, NULL);
433 else
434 rs6000_ptrace32 (PT_WRITE_D, pid,
0df8b418
MS
435 (int *) (uintptr_t) rounded_offset,
436 buffer.word, NULL);
037a727e 437 if (errno)
9b409511 438 return TARGET_XFER_EOF;
037a727e
UW
439 }
440
441 if (readbuf)
442 {
443 errno = 0;
444 if (arch64)
445 buffer.word = rs6000_ptrace64 (PT_READ_I, pid,
446 rounded_offset, 0, NULL);
447 else
448 buffer.word = rs6000_ptrace32 (PT_READ_I, pid,
449 (int *)(uintptr_t)rounded_offset,
450 0, NULL);
451 if (errno)
9b409511 452 return TARGET_XFER_EOF;
037a727e
UW
453
454 /* Copy appropriate bytes out of the buffer. */
455 memcpy (readbuf, buffer.byte + (offset - rounded_offset),
456 partial_len);
457 }
458
9b409511
YQ
459 *xfered_len = (ULONGEST) partial_len;
460 return TARGET_XFER_OK;
037a727e
UW
461 }
462
463 default:
2ed4b548 464 return TARGET_XFER_E_IO;
7a78ae4e 465 }
c906108c
SS
466}
467
482f7fee
UW
468/* Wait for the child specified by PTID to do something. Return the
469 process ID of the child, or MINUS_ONE_PTID in case of error; store
470 the status in *OURSTATUS. */
471
472static ptid_t
117de6a9 473rs6000_wait (struct target_ops *ops,
47608cb1 474 ptid_t ptid, struct target_waitstatus *ourstatus, int options)
482f7fee
UW
475{
476 pid_t pid;
477 int status, save_errno;
478
479 do
480 {
481 set_sigint_trap ();
482f7fee
UW
482
483 do
484 {
485 pid = waitpid (ptid_get_pid (ptid), &status, 0);
486 save_errno = errno;
487 }
488 while (pid == -1 && errno == EINTR);
489
482f7fee
UW
490 clear_sigint_trap ();
491
492 if (pid == -1)
493 {
494 fprintf_unfiltered (gdb_stderr,
495 _("Child process unexpectedly missing: %s.\n"),
496 safe_strerror (save_errno));
497
498 /* Claim it exited with unknown signal. */
499 ourstatus->kind = TARGET_WAITKIND_SIGNALLED;
a493e3e2 500 ourstatus->value.sig = GDB_SIGNAL_UNKNOWN;
fb66883a 501 return inferior_ptid;
482f7fee
UW
502 }
503
504 /* Ignore terminated detached child processes. */
505 if (!WIFSTOPPED (status) && pid != ptid_get_pid (inferior_ptid))
506 pid = -1;
507 }
508 while (pid == -1);
509
510 /* AIX has a couple of strange returns from wait(). */
511
512 /* stop after load" status. */
513 if (status == 0x57c)
514 ourstatus->kind = TARGET_WAITKIND_LOADED;
0df8b418 515 /* signal 0. I have no idea why wait(2) returns with this status word. */
482f7fee
UW
516 else if (status == 0x7f)
517 ourstatus->kind = TARGET_WAITKIND_SPURIOUS;
518 /* A normal waitstatus. Let the usual macros deal with it. */
519 else
520 store_waitstatus (ourstatus, status);
521
522 return pid_to_ptid (pid);
523}
c906108c 524\f
7a78ae4e 525
7a78ae4e 526/* Set the current architecture from the host running GDB. Called when
0df8b418 527 starting a child process. */
7a78ae4e 528
136d6dae
VP
529static void (*super_create_inferior) (struct target_ops *,char *exec_file,
530 char *allargs, char **env, int from_tty);
1f480a5e 531static void
136d6dae
VP
532rs6000_create_inferior (struct target_ops * ops, char *exec_file,
533 char *allargs, char **env, int from_tty)
7a78ae4e
ND
534{
535 enum bfd_architecture arch;
536 unsigned long mach;
537 bfd abfd;
538 struct gdbarch_info info;
539
136d6dae 540 super_create_inferior (ops, exec_file, allargs, env, from_tty);
1f480a5e 541
7a78ae4e
ND
542 if (__power_rs ())
543 {
544 arch = bfd_arch_rs6000;
545 mach = bfd_mach_rs6k;
546 }
547 else
548 {
549 arch = bfd_arch_powerpc;
550 mach = bfd_mach_ppc;
551 }
19caaa45
PS
552
553 /* FIXME: schauer/2002-02-25:
554 We don't know if we are executing a 32 or 64 bit executable,
555 and have no way to pass the proper word size to rs6000_gdbarch_init.
556 So we have to avoid switching to a new architecture, if the architecture
557 matches already.
558 Blindly calling rs6000_gdbarch_init used to work in older versions of
559 GDB, as rs6000_gdbarch_init incorrectly used the previous tdep to
560 determine the wordsize. */
561 if (exec_bfd)
562 {
563 const struct bfd_arch_info *exec_bfd_arch_info;
564
565 exec_bfd_arch_info = bfd_get_arch_info (exec_bfd);
566 if (arch == exec_bfd_arch_info->arch)
567 return;
568 }
569
7a78ae4e
ND
570 bfd_default_set_arch_mach (&abfd, arch, mach);
571
fb6ecb0f 572 gdbarch_info_init (&info);
7a78ae4e 573 info.bfd_arch_info = bfd_get_arch_info (&abfd);
7aea86e6 574 info.abfd = exec_bfd;
7a78ae4e 575
16f33e29 576 if (!gdbarch_update_p (info))
e2e0b3e5 577 internal_error (__FILE__, __LINE__,
0df8b418
MS
578 _("rs6000_create_inferior: failed "
579 "to select architecture"));
7a78ae4e 580}
c906108c 581\f
c906108c 582
4d1eb6b4 583/* Shared Object support. */
c906108c 584
4d1eb6b4
JB
585/* Return the LdInfo data for the given process. Raises an error
586 if the data could not be obtained.
8d08c9ce 587
4d1eb6b4 588 The returned value must be deallocated after use. */
c906108c 589
356a5233 590static gdb_byte *
4d1eb6b4
JB
591rs6000_ptrace_ldinfo (ptid_t ptid)
592{
593 const int pid = ptid_get_pid (ptid);
594 int ldi_size = 1024;
71829b1a 595 void *ldi = xmalloc (ldi_size);
4d1eb6b4 596 int rc = -1;
7a78ae4e 597
4d1eb6b4
JB
598 while (1)
599 {
600 if (ARCH64 ())
601 rc = rs6000_ptrace64 (PT_LDINFO, pid, (unsigned long) ldi, ldi_size,
602 NULL);
c18e0d23 603 else
4d1eb6b4
JB
604 rc = rs6000_ptrace32 (PT_LDINFO, pid, (int *) ldi, ldi_size, NULL);
605
606 if (rc != -1)
607 break; /* Success, we got the entire ld_info data. */
608
609 if (errno != ENOMEM)
610 perror_with_name (_("ptrace ldinfo"));
611
612 /* ldi is not big enough. Double it and try again. */
613 ldi_size *= 2;
614 ldi = xrealloc (ldi, ldi_size);
615 }
616
71829b1a 617 return (gdb_byte *) ldi;
c906108c 618}
c906108c 619
4d1eb6b4 620/* Implement the to_xfer_partial target_ops method for
ff99b71b 621 TARGET_OBJECT_LIBRARIES_AIX objects. */
6426a772 622
9b409511 623static enum target_xfer_status
4d1eb6b4
JB
624rs6000_xfer_shared_libraries
625 (struct target_ops *ops, enum target_object object,
626 const char *annex, gdb_byte *readbuf, const gdb_byte *writebuf,
9b409511 627 ULONGEST offset, ULONGEST len, ULONGEST *xfered_len)
4d1eb6b4 628{
356a5233
JB
629 gdb_byte *ldi_buf;
630 ULONGEST result;
631 struct cleanup *cleanup;
632
633 /* This function assumes that it is being run with a live process.
634 Core files are handled via gdbarch. */
635 gdb_assert (target_has_execution);
c906108c 636
4d1eb6b4 637 if (writebuf)
2ed4b548 638 return TARGET_XFER_E_IO;
c5aa993b 639
356a5233
JB
640 ldi_buf = rs6000_ptrace_ldinfo (inferior_ptid);
641 gdb_assert (ldi_buf != NULL);
642 cleanup = make_cleanup (xfree, ldi_buf);
643 result = rs6000_aix_ld_info_to_xml (target_gdbarch (), ldi_buf,
644 readbuf, offset, len, 1);
645 xfree (ldi_buf);
4d1eb6b4 646
356a5233 647 do_cleanups (cleanup);
9b409511
YQ
648
649 if (result == 0)
650 return TARGET_XFER_EOF;
651 else
652 {
653 *xfered_len = result;
654 return TARGET_XFER_OK;
655 }
c906108c 656}
c906108c 657
e1aca11e
JB
658void _initialize_rs6000_nat (void);
659
c906108c 660void
7a61a01c 661_initialize_rs6000_nat (void)
c906108c 662{
037a727e
UW
663 struct target_ops *t;
664
665 t = inf_ptrace_target ();
666 t->to_fetch_registers = rs6000_fetch_inferior_registers;
667 t->to_store_registers = rs6000_store_inferior_registers;
668 t->to_xfer_partial = rs6000_xfer_partial;
1f480a5e
UW
669
670 super_create_inferior = t->to_create_inferior;
671 t->to_create_inferior = rs6000_create_inferior;
672
482f7fee
UW
673 t->to_wait = rs6000_wait;
674
037a727e 675 add_target (t);
c906108c 676}
This page took 2.20778 seconds and 4 git commands to generate.