AIX: Change XML to use library-list-aix instead of library-list.
[deliverable/binutils-gdb.git] / gdb / rs6000-nat.c
1 /* IBM RS/6000 native-dependent code for GDB, the GNU debugger.
2
3 Copyright (C) 1986-2013 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19
20 #include "defs.h"
21 #include "inferior.h"
22 #include "target.h"
23 #include "gdbcore.h"
24 #include "symfile.h"
25 #include "objfiles.h"
26 #include "libbfd.h" /* For bfd_default_set_arch_mach (FIXME) */
27 #include "bfd.h"
28 #include "exceptions.h"
29 #include "gdb-stabs.h"
30 #include "regcache.h"
31 #include "arch-utils.h"
32 #include "inf-child.h"
33 #include "inf-ptrace.h"
34 #include "ppc-tdep.h"
35 #include "rs6000-tdep.h"
36 #include "exec.h"
37 #include "observer.h"
38 #include "xcoffread.h"
39
40 #include <sys/ptrace.h>
41 #include <sys/reg.h>
42
43 #include <sys/param.h>
44 #include <sys/dir.h>
45 #include <sys/user.h>
46 #include <signal.h>
47 #include <sys/ioctl.h>
48 #include <fcntl.h>
49 #include <errno.h>
50
51 #include <a.out.h>
52 #include <sys/file.h>
53 #include "gdb_stat.h"
54 #include "gdb_bfd.h"
55 #include <sys/core.h>
56 #define __LDINFO_PTRACE32__ /* for __ld_info32 */
57 #define __LDINFO_PTRACE64__ /* for __ld_info64 */
58 #include <sys/ldr.h>
59 #include <sys/systemcfg.h>
60 #include "xml-utils.h"
61
62 /* On AIX4.3+, sys/ldr.h provides different versions of struct ld_info for
63 debugging 32-bit and 64-bit processes. Define a typedef and macros for
64 accessing fields in the appropriate structures. */
65
66 /* In 32-bit compilation mode (which is the only mode from which ptrace()
67 works on 4.3), __ld_info32 is #defined as equivalent to ld_info. */
68
69 #ifdef __ld_info32
70 # define ARCH3264
71 #endif
72
73 /* Return whether the current architecture is 64-bit. */
74
75 #ifndef ARCH3264
76 # define ARCH64() 0
77 #else
78 # define ARCH64() (register_size (target_gdbarch (), 0) == 8)
79 #endif
80
81 /* Union of 32-bit and 64-bit versions of ld_info. */
82
83 typedef union {
84 #ifndef ARCH3264
85 struct ld_info l32;
86 struct ld_info l64;
87 #else
88 struct __ld_info32 l32;
89 struct __ld_info64 l64;
90 #endif
91 } LdInfo;
92
93 /* If compiling with 32-bit and 64-bit debugging capability (e.g. AIX 4.x),
94 declare and initialize a variable named VAR suitable for use as the arch64
95 parameter to the various LDI_*() macros. */
96
97 #ifndef ARCH3264
98 # define ARCH64_DECL(var)
99 #else
100 # define ARCH64_DECL(var) int var = ARCH64 ()
101 #endif
102
103 /* Return LDI's FIELD for a 64-bit process if ARCH64 and for a 32-bit process
104 otherwise. This technique only works for FIELDs with the same data type in
105 32-bit and 64-bit versions of ld_info. */
106
107 #ifndef ARCH3264
108 # define LDI_FIELD(ldi, arch64, field) (ldi)->l32.ldinfo_##field
109 #else
110 # define LDI_FIELD(ldi, arch64, field) \
111 (arch64 ? (ldi)->l64.ldinfo_##field : (ldi)->l32.ldinfo_##field)
112 #endif
113
114 /* Return various LDI fields for a 64-bit process if ARCH64 and for a 32-bit
115 process otherwise. */
116
117 #define LDI_NEXT(ldi, arch64) LDI_FIELD(ldi, arch64, next)
118 #define LDI_FD(ldi, arch64) LDI_FIELD(ldi, arch64, fd)
119 #define LDI_FILENAME(ldi, arch64) LDI_FIELD(ldi, arch64, filename)
120
121 static void exec_one_dummy_insn (struct regcache *);
122
123 static LONGEST rs6000_xfer_shared_libraries
124 (struct target_ops *ops, enum target_object object,
125 const char *annex, gdb_byte *readbuf, const gdb_byte *writebuf,
126 ULONGEST offset, LONGEST len);
127
128 /* Given REGNO, a gdb register number, return the corresponding
129 number suitable for use as a ptrace() parameter. Return -1 if
130 there's no suitable mapping. Also, set the int pointed to by
131 ISFLOAT to indicate whether REGNO is a floating point register. */
132
133 static int
134 regmap (struct gdbarch *gdbarch, int regno, int *isfloat)
135 {
136 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
137
138 *isfloat = 0;
139 if (tdep->ppc_gp0_regnum <= regno
140 && regno < tdep->ppc_gp0_regnum + ppc_num_gprs)
141 return regno;
142 else if (tdep->ppc_fp0_regnum >= 0
143 && tdep->ppc_fp0_regnum <= regno
144 && regno < tdep->ppc_fp0_regnum + ppc_num_fprs)
145 {
146 *isfloat = 1;
147 return regno - tdep->ppc_fp0_regnum + FPR0;
148 }
149 else if (regno == gdbarch_pc_regnum (gdbarch))
150 return IAR;
151 else if (regno == tdep->ppc_ps_regnum)
152 return MSR;
153 else if (regno == tdep->ppc_cr_regnum)
154 return CR;
155 else if (regno == tdep->ppc_lr_regnum)
156 return LR;
157 else if (regno == tdep->ppc_ctr_regnum)
158 return CTR;
159 else if (regno == tdep->ppc_xer_regnum)
160 return XER;
161 else if (tdep->ppc_fpscr_regnum >= 0
162 && regno == tdep->ppc_fpscr_regnum)
163 return FPSCR;
164 else if (tdep->ppc_mq_regnum >= 0 && regno == tdep->ppc_mq_regnum)
165 return MQ;
166 else
167 return -1;
168 }
169
170 /* Call ptrace(REQ, ID, ADDR, DATA, BUF). */
171
172 static int
173 rs6000_ptrace32 (int req, int id, int *addr, int data, int *buf)
174 {
175 int ret = ptrace (req, id, (int *)addr, data, buf);
176 #if 0
177 printf ("rs6000_ptrace32 (%d, %d, 0x%x, %08x, 0x%x) = 0x%x\n",
178 req, id, (unsigned int)addr, data, (unsigned int)buf, ret);
179 #endif
180 return ret;
181 }
182
183 /* Call ptracex(REQ, ID, ADDR, DATA, BUF). */
184
185 static int
186 rs6000_ptrace64 (int req, int id, long long addr, int data, void *buf)
187 {
188 #ifdef ARCH3264
189 int ret = ptracex (req, id, addr, data, buf);
190 #else
191 int ret = 0;
192 #endif
193 #if 0
194 printf ("rs6000_ptrace64 (%d, %d, %s, %08x, 0x%x) = 0x%x\n",
195 req, id, hex_string (addr), data, (unsigned int)buf, ret);
196 #endif
197 return ret;
198 }
199
200 /* Fetch register REGNO from the inferior. */
201
202 static void
203 fetch_register (struct regcache *regcache, int regno)
204 {
205 struct gdbarch *gdbarch = get_regcache_arch (regcache);
206 int addr[MAX_REGISTER_SIZE];
207 int nr, isfloat;
208
209 /* Retrieved values may be -1, so infer errors from errno. */
210 errno = 0;
211
212 nr = regmap (gdbarch, regno, &isfloat);
213
214 /* Floating-point registers. */
215 if (isfloat)
216 rs6000_ptrace32 (PT_READ_FPR, PIDGET (inferior_ptid), addr, nr, 0);
217
218 /* Bogus register number. */
219 else if (nr < 0)
220 {
221 if (regno >= gdbarch_num_regs (gdbarch))
222 fprintf_unfiltered (gdb_stderr,
223 "gdb error: register no %d not implemented.\n",
224 regno);
225 return;
226 }
227
228 /* Fixed-point registers. */
229 else
230 {
231 if (!ARCH64 ())
232 *addr = rs6000_ptrace32 (PT_READ_GPR, PIDGET (inferior_ptid),
233 (int *) nr, 0, 0);
234 else
235 {
236 /* PT_READ_GPR requires the buffer parameter to point to long long,
237 even if the register is really only 32 bits. */
238 long long buf;
239 rs6000_ptrace64 (PT_READ_GPR, PIDGET (inferior_ptid), nr, 0, &buf);
240 if (register_size (gdbarch, regno) == 8)
241 memcpy (addr, &buf, 8);
242 else
243 *addr = buf;
244 }
245 }
246
247 if (!errno)
248 regcache_raw_supply (regcache, regno, (char *) addr);
249 else
250 {
251 #if 0
252 /* FIXME: this happens 3 times at the start of each 64-bit program. */
253 perror (_("ptrace read"));
254 #endif
255 errno = 0;
256 }
257 }
258
259 /* Store register REGNO back into the inferior. */
260
261 static void
262 store_register (struct regcache *regcache, int regno)
263 {
264 struct gdbarch *gdbarch = get_regcache_arch (regcache);
265 int addr[MAX_REGISTER_SIZE];
266 int nr, isfloat;
267
268 /* Fetch the register's value from the register cache. */
269 regcache_raw_collect (regcache, regno, addr);
270
271 /* -1 can be a successful return value, so infer errors from errno. */
272 errno = 0;
273
274 nr = regmap (gdbarch, regno, &isfloat);
275
276 /* Floating-point registers. */
277 if (isfloat)
278 rs6000_ptrace32 (PT_WRITE_FPR, PIDGET (inferior_ptid), addr, nr, 0);
279
280 /* Bogus register number. */
281 else if (nr < 0)
282 {
283 if (regno >= gdbarch_num_regs (gdbarch))
284 fprintf_unfiltered (gdb_stderr,
285 "gdb error: register no %d not implemented.\n",
286 regno);
287 }
288
289 /* Fixed-point registers. */
290 else
291 {
292 if (regno == gdbarch_sp_regnum (gdbarch))
293 /* Execute one dummy instruction (which is a breakpoint) in inferior
294 process to give kernel a chance to do internal housekeeping.
295 Otherwise the following ptrace(2) calls will mess up user stack
296 since kernel will get confused about the bottom of the stack
297 (%sp). */
298 exec_one_dummy_insn (regcache);
299
300 /* The PT_WRITE_GPR operation is rather odd. For 32-bit inferiors,
301 the register's value is passed by value, but for 64-bit inferiors,
302 the address of a buffer containing the value is passed. */
303 if (!ARCH64 ())
304 rs6000_ptrace32 (PT_WRITE_GPR, PIDGET (inferior_ptid),
305 (int *) nr, *addr, 0);
306 else
307 {
308 /* PT_WRITE_GPR requires the buffer parameter to point to an 8-byte
309 area, even if the register is really only 32 bits. */
310 long long buf;
311 if (register_size (gdbarch, regno) == 8)
312 memcpy (&buf, addr, 8);
313 else
314 buf = *addr;
315 rs6000_ptrace64 (PT_WRITE_GPR, PIDGET (inferior_ptid), nr, 0, &buf);
316 }
317 }
318
319 if (errno)
320 {
321 perror (_("ptrace write"));
322 errno = 0;
323 }
324 }
325
326 /* Read from the inferior all registers if REGNO == -1 and just register
327 REGNO otherwise. */
328
329 static void
330 rs6000_fetch_inferior_registers (struct target_ops *ops,
331 struct regcache *regcache, int regno)
332 {
333 struct gdbarch *gdbarch = get_regcache_arch (regcache);
334 if (regno != -1)
335 fetch_register (regcache, regno);
336
337 else
338 {
339 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
340
341 /* Read 32 general purpose registers. */
342 for (regno = tdep->ppc_gp0_regnum;
343 regno < tdep->ppc_gp0_regnum + ppc_num_gprs;
344 regno++)
345 {
346 fetch_register (regcache, regno);
347 }
348
349 /* Read general purpose floating point registers. */
350 if (tdep->ppc_fp0_regnum >= 0)
351 for (regno = 0; regno < ppc_num_fprs; regno++)
352 fetch_register (regcache, tdep->ppc_fp0_regnum + regno);
353
354 /* Read special registers. */
355 fetch_register (regcache, gdbarch_pc_regnum (gdbarch));
356 fetch_register (regcache, tdep->ppc_ps_regnum);
357 fetch_register (regcache, tdep->ppc_cr_regnum);
358 fetch_register (regcache, tdep->ppc_lr_regnum);
359 fetch_register (regcache, tdep->ppc_ctr_regnum);
360 fetch_register (regcache, tdep->ppc_xer_regnum);
361 if (tdep->ppc_fpscr_regnum >= 0)
362 fetch_register (regcache, tdep->ppc_fpscr_regnum);
363 if (tdep->ppc_mq_regnum >= 0)
364 fetch_register (regcache, tdep->ppc_mq_regnum);
365 }
366 }
367
368 /* Store our register values back into the inferior.
369 If REGNO is -1, do this for all registers.
370 Otherwise, REGNO specifies which register (so we can save time). */
371
372 static void
373 rs6000_store_inferior_registers (struct target_ops *ops,
374 struct regcache *regcache, int regno)
375 {
376 struct gdbarch *gdbarch = get_regcache_arch (regcache);
377 if (regno != -1)
378 store_register (regcache, regno);
379
380 else
381 {
382 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
383
384 /* Write general purpose registers first. */
385 for (regno = tdep->ppc_gp0_regnum;
386 regno < tdep->ppc_gp0_regnum + ppc_num_gprs;
387 regno++)
388 {
389 store_register (regcache, regno);
390 }
391
392 /* Write floating point registers. */
393 if (tdep->ppc_fp0_regnum >= 0)
394 for (regno = 0; regno < ppc_num_fprs; regno++)
395 store_register (regcache, tdep->ppc_fp0_regnum + regno);
396
397 /* Write special registers. */
398 store_register (regcache, gdbarch_pc_regnum (gdbarch));
399 store_register (regcache, tdep->ppc_ps_regnum);
400 store_register (regcache, tdep->ppc_cr_regnum);
401 store_register (regcache, tdep->ppc_lr_regnum);
402 store_register (regcache, tdep->ppc_ctr_regnum);
403 store_register (regcache, tdep->ppc_xer_regnum);
404 if (tdep->ppc_fpscr_regnum >= 0)
405 store_register (regcache, tdep->ppc_fpscr_regnum);
406 if (tdep->ppc_mq_regnum >= 0)
407 store_register (regcache, tdep->ppc_mq_regnum);
408 }
409 }
410
411
412 /* Attempt a transfer all LEN bytes starting at OFFSET between the
413 inferior's OBJECT:ANNEX space and GDB's READBUF/WRITEBUF buffer.
414 Return the number of bytes actually transferred. */
415
416 static LONGEST
417 rs6000_xfer_partial (struct target_ops *ops, enum target_object object,
418 const char *annex, gdb_byte *readbuf,
419 const gdb_byte *writebuf,
420 ULONGEST offset, LONGEST len)
421 {
422 pid_t pid = ptid_get_pid (inferior_ptid);
423 int arch64 = ARCH64 ();
424
425 switch (object)
426 {
427 case TARGET_OBJECT_LIBRARIES_AIX:
428 return rs6000_xfer_shared_libraries (ops, object, annex,
429 readbuf, writebuf,
430 offset, len);
431 case TARGET_OBJECT_MEMORY:
432 {
433 union
434 {
435 PTRACE_TYPE_RET word;
436 gdb_byte byte[sizeof (PTRACE_TYPE_RET)];
437 } buffer;
438 ULONGEST rounded_offset;
439 LONGEST partial_len;
440
441 /* Round the start offset down to the next long word
442 boundary. */
443 rounded_offset = offset & -(ULONGEST) sizeof (PTRACE_TYPE_RET);
444
445 /* Since ptrace will transfer a single word starting at that
446 rounded_offset the partial_len needs to be adjusted down to
447 that (remember this function only does a single transfer).
448 Should the required length be even less, adjust it down
449 again. */
450 partial_len = (rounded_offset + sizeof (PTRACE_TYPE_RET)) - offset;
451 if (partial_len > len)
452 partial_len = len;
453
454 if (writebuf)
455 {
456 /* If OFFSET:PARTIAL_LEN is smaller than
457 ROUNDED_OFFSET:WORDSIZE then a read/modify write will
458 be needed. Read in the entire word. */
459 if (rounded_offset < offset
460 || (offset + partial_len
461 < rounded_offset + sizeof (PTRACE_TYPE_RET)))
462 {
463 /* Need part of initial word -- fetch it. */
464 if (arch64)
465 buffer.word = rs6000_ptrace64 (PT_READ_I, pid,
466 rounded_offset, 0, NULL);
467 else
468 buffer.word = rs6000_ptrace32 (PT_READ_I, pid,
469 (int *) (uintptr_t)
470 rounded_offset,
471 0, NULL);
472 }
473
474 /* Copy data to be written over corresponding part of
475 buffer. */
476 memcpy (buffer.byte + (offset - rounded_offset),
477 writebuf, partial_len);
478
479 errno = 0;
480 if (arch64)
481 rs6000_ptrace64 (PT_WRITE_D, pid,
482 rounded_offset, buffer.word, NULL);
483 else
484 rs6000_ptrace32 (PT_WRITE_D, pid,
485 (int *) (uintptr_t) rounded_offset,
486 buffer.word, NULL);
487 if (errno)
488 return 0;
489 }
490
491 if (readbuf)
492 {
493 errno = 0;
494 if (arch64)
495 buffer.word = rs6000_ptrace64 (PT_READ_I, pid,
496 rounded_offset, 0, NULL);
497 else
498 buffer.word = rs6000_ptrace32 (PT_READ_I, pid,
499 (int *)(uintptr_t)rounded_offset,
500 0, NULL);
501 if (errno)
502 return 0;
503
504 /* Copy appropriate bytes out of the buffer. */
505 memcpy (readbuf, buffer.byte + (offset - rounded_offset),
506 partial_len);
507 }
508
509 return partial_len;
510 }
511
512 default:
513 return -1;
514 }
515 }
516
517 /* Wait for the child specified by PTID to do something. Return the
518 process ID of the child, or MINUS_ONE_PTID in case of error; store
519 the status in *OURSTATUS. */
520
521 static ptid_t
522 rs6000_wait (struct target_ops *ops,
523 ptid_t ptid, struct target_waitstatus *ourstatus, int options)
524 {
525 pid_t pid;
526 int status, save_errno;
527
528 do
529 {
530 set_sigint_trap ();
531
532 do
533 {
534 pid = waitpid (ptid_get_pid (ptid), &status, 0);
535 save_errno = errno;
536 }
537 while (pid == -1 && errno == EINTR);
538
539 clear_sigint_trap ();
540
541 if (pid == -1)
542 {
543 fprintf_unfiltered (gdb_stderr,
544 _("Child process unexpectedly missing: %s.\n"),
545 safe_strerror (save_errno));
546
547 /* Claim it exited with unknown signal. */
548 ourstatus->kind = TARGET_WAITKIND_SIGNALLED;
549 ourstatus->value.sig = GDB_SIGNAL_UNKNOWN;
550 return inferior_ptid;
551 }
552
553 /* Ignore terminated detached child processes. */
554 if (!WIFSTOPPED (status) && pid != ptid_get_pid (inferior_ptid))
555 pid = -1;
556 }
557 while (pid == -1);
558
559 /* AIX has a couple of strange returns from wait(). */
560
561 /* stop after load" status. */
562 if (status == 0x57c)
563 ourstatus->kind = TARGET_WAITKIND_LOADED;
564 /* signal 0. I have no idea why wait(2) returns with this status word. */
565 else if (status == 0x7f)
566 ourstatus->kind = TARGET_WAITKIND_SPURIOUS;
567 /* A normal waitstatus. Let the usual macros deal with it. */
568 else
569 store_waitstatus (ourstatus, status);
570
571 return pid_to_ptid (pid);
572 }
573
574 /* Execute one dummy breakpoint instruction. This way we give the kernel
575 a chance to do some housekeeping and update inferior's internal data,
576 including u_area. */
577
578 static void
579 exec_one_dummy_insn (struct regcache *regcache)
580 {
581 #define DUMMY_INSN_ADDR AIX_TEXT_SEGMENT_BASE+0x200
582
583 struct gdbarch *gdbarch = get_regcache_arch (regcache);
584 int ret, status, pid;
585 CORE_ADDR prev_pc;
586 void *bp;
587
588 /* We plant one dummy breakpoint into DUMMY_INSN_ADDR address. We
589 assume that this address will never be executed again by the real
590 code. */
591
592 bp = deprecated_insert_raw_breakpoint (gdbarch, NULL, DUMMY_INSN_ADDR);
593
594 /* You might think this could be done with a single ptrace call, and
595 you'd be correct for just about every platform I've ever worked
596 on. However, rs6000-ibm-aix4.1.3 seems to have screwed this up --
597 the inferior never hits the breakpoint (it's also worth noting
598 powerpc-ibm-aix4.1.3 works correctly). */
599 prev_pc = regcache_read_pc (regcache);
600 regcache_write_pc (regcache, DUMMY_INSN_ADDR);
601 if (ARCH64 ())
602 ret = rs6000_ptrace64 (PT_CONTINUE, PIDGET (inferior_ptid), 1, 0, NULL);
603 else
604 ret = rs6000_ptrace32 (PT_CONTINUE, PIDGET (inferior_ptid),
605 (int *) 1, 0, NULL);
606
607 if (ret != 0)
608 perror (_("pt_continue"));
609
610 do
611 {
612 pid = waitpid (PIDGET (inferior_ptid), &status, 0);
613 }
614 while (pid != PIDGET (inferior_ptid));
615
616 regcache_write_pc (regcache, prev_pc);
617 deprecated_remove_raw_breakpoint (gdbarch, bp);
618 }
619 \f
620
621 /* Set the current architecture from the host running GDB. Called when
622 starting a child process. */
623
624 static void (*super_create_inferior) (struct target_ops *,char *exec_file,
625 char *allargs, char **env, int from_tty);
626 static void
627 rs6000_create_inferior (struct target_ops * ops, char *exec_file,
628 char *allargs, char **env, int from_tty)
629 {
630 enum bfd_architecture arch;
631 unsigned long mach;
632 bfd abfd;
633 struct gdbarch_info info;
634
635 super_create_inferior (ops, exec_file, allargs, env, from_tty);
636
637 if (__power_rs ())
638 {
639 arch = bfd_arch_rs6000;
640 mach = bfd_mach_rs6k;
641 }
642 else
643 {
644 arch = bfd_arch_powerpc;
645 mach = bfd_mach_ppc;
646 }
647
648 /* FIXME: schauer/2002-02-25:
649 We don't know if we are executing a 32 or 64 bit executable,
650 and have no way to pass the proper word size to rs6000_gdbarch_init.
651 So we have to avoid switching to a new architecture, if the architecture
652 matches already.
653 Blindly calling rs6000_gdbarch_init used to work in older versions of
654 GDB, as rs6000_gdbarch_init incorrectly used the previous tdep to
655 determine the wordsize. */
656 if (exec_bfd)
657 {
658 const struct bfd_arch_info *exec_bfd_arch_info;
659
660 exec_bfd_arch_info = bfd_get_arch_info (exec_bfd);
661 if (arch == exec_bfd_arch_info->arch)
662 return;
663 }
664
665 bfd_default_set_arch_mach (&abfd, arch, mach);
666
667 gdbarch_info_init (&info);
668 info.bfd_arch_info = bfd_get_arch_info (&abfd);
669 info.abfd = exec_bfd;
670
671 if (!gdbarch_update_p (info))
672 internal_error (__FILE__, __LINE__,
673 _("rs6000_create_inferior: failed "
674 "to select architecture"));
675 }
676 \f
677
678 /* Shared Object support. */
679
680 /* Return the LdInfo data for the given process. Raises an error
681 if the data could not be obtained.
682
683 The returned value must be deallocated after use. */
684
685 static LdInfo *
686 rs6000_ptrace_ldinfo (ptid_t ptid)
687 {
688 const int pid = ptid_get_pid (ptid);
689 int ldi_size = 1024;
690 LdInfo *ldi = xmalloc (ldi_size);
691 int rc = -1;
692
693 while (1)
694 {
695 if (ARCH64 ())
696 rc = rs6000_ptrace64 (PT_LDINFO, pid, (unsigned long) ldi, ldi_size,
697 NULL);
698 else
699 rc = rs6000_ptrace32 (PT_LDINFO, pid, (int *) ldi, ldi_size, NULL);
700
701 if (rc != -1)
702 break; /* Success, we got the entire ld_info data. */
703
704 if (errno != ENOMEM)
705 perror_with_name (_("ptrace ldinfo"));
706
707 /* ldi is not big enough. Double it and try again. */
708 ldi_size *= 2;
709 ldi = xrealloc (ldi, ldi_size);
710 }
711
712 return ldi;
713 }
714
715 /* Assuming ABFD refers to a core file, return the LdInfo data
716 stored in that core file. Raises an error if the data could
717 not be read or extracted.
718
719 The returned value much be deallocated after use. */
720
721 static LdInfo *
722 rs6000_core_ldinfo (bfd *abfd)
723 {
724 struct bfd_section *ldinfo_sec;
725 int ldinfo_size;
726 gdb_byte *ldinfo_buf;
727 struct cleanup *cleanup;
728
729 ldinfo_sec = bfd_get_section_by_name (abfd, ".ldinfo");
730 if (ldinfo_sec == NULL)
731 error (_("cannot find .ldinfo section from core file: %s\n"),
732 bfd_errmsg (bfd_get_error ()));
733 ldinfo_size = bfd_get_section_size (ldinfo_sec);
734
735 ldinfo_buf = xmalloc (ldinfo_size);
736 cleanup = make_cleanup (xfree, ldinfo_buf);
737
738 if (! bfd_get_section_contents (abfd, ldinfo_sec,
739 ldinfo_buf, 0, ldinfo_size))
740 error (_("unable to read .ldinfo section from core file: %s\n"),
741 bfd_errmsg (bfd_get_error ()));
742
743 discard_cleanups (cleanup);
744 return (LdInfo *) ldinfo_buf;
745 }
746
747 /* Append to OBJSTACK an XML string description of the shared library
748 corresponding to LDI, following the TARGET_OBJECT_LIBRARIES_AIX
749 format. */
750
751 static void
752 rs6000_xfer_shared_library (LdInfo *ldi, struct obstack *obstack)
753 {
754 const int arch64 = ARCH64 ();
755 const char *archive_name = LDI_FILENAME (ldi, arch64);
756 const char *member_name = archive_name + strlen (archive_name) + 1;
757 CORE_ADDR text_addr, data_addr;
758 ULONGEST text_size, data_size;
759 char *p;
760
761 if (arch64)
762 {
763 text_addr = ldi->l64.ldinfo_textorg;
764 text_size = ldi->l64.ldinfo_textsize;
765 data_addr = ldi->l64.ldinfo_dataorg;
766 data_size = ldi->l64.ldinfo_datasize;
767 }
768 else
769 {
770 /* The text and data addresses are defined as pointers.
771 To avoid sign-extending their value in the assignments
772 below, we cast their value to unsigned long first. */
773 text_addr = (unsigned long) ldi->l32.ldinfo_textorg;
774 text_size = ldi->l32.ldinfo_textsize;
775 data_addr = (unsigned long) ldi->l32.ldinfo_dataorg;
776 data_size = ldi->l32.ldinfo_datasize;
777 }
778
779 obstack_grow_str (obstack, "<library name=\"");
780 p = xml_escape_text (archive_name);
781 obstack_grow_str (obstack, p);
782 xfree (p);
783 obstack_grow_str (obstack, "\"");
784
785 if (member_name[0] != '\0')
786 {
787 obstack_grow_str (obstack, " member=\"");
788 p = xml_escape_text (member_name);
789 obstack_grow_str (obstack, p);
790 xfree (p);
791 obstack_grow_str (obstack, "\"");
792 }
793
794 obstack_grow_str (obstack, " text_addr=\"");
795 obstack_grow_str (obstack, core_addr_to_string (text_addr));
796 obstack_grow_str (obstack, "\"");
797
798 obstack_grow_str (obstack, " text_size=\"");
799 obstack_grow_str (obstack, pulongest (text_size));
800 obstack_grow_str (obstack, "\"");
801
802 obstack_grow_str (obstack, " data_addr=\"");
803 obstack_grow_str (obstack, core_addr_to_string (data_addr));
804 obstack_grow_str (obstack, "\"");
805
806 obstack_grow_str (obstack, " data_size=\"");
807 obstack_grow_str (obstack, pulongest (data_size));
808 obstack_grow_str (obstack, "\"");
809
810 obstack_grow_str (obstack, "></library>");
811 }
812
813 /* Implement the to_xfer_partial target_ops method for
814 TARGET_OBJECT_LIBRARIES_AIX objects. */
815
816 static LONGEST
817 rs6000_xfer_shared_libraries
818 (struct target_ops *ops, enum target_object object,
819 const char *annex, gdb_byte *readbuf, const gdb_byte *writebuf,
820 ULONGEST offset, LONGEST len)
821 {
822 const int arch64 = ARCH64 ();
823 LdInfo *ldi_data;
824 LdInfo *ldi;
825 struct obstack obstack;
826 const char *buf;
827 LONGEST len_avail;
828
829 if (writebuf)
830 return -1;
831
832 /* Get the ldinfo raw data: If debugging a live process, we get it
833 using ptrace. Otherwise, the info is stored in the .ldinfo
834 section of the core file. */
835
836 if (target_has_execution)
837 ldi_data = rs6000_ptrace_ldinfo (inferior_ptid);
838 else
839 ldi_data = rs6000_core_ldinfo (core_bfd);
840
841 /* Convert the raw data into an XML representation. */
842
843 obstack_init (&obstack);
844 obstack_grow_str (&obstack, "<library-list-aix version=\"1.0\">\n");
845
846 ldi = ldi_data;
847 while (1)
848 {
849 /* Close the fd. We cannot use it, because we cannot assume
850 that the user of this descriptor will be in the same
851 process. */
852 close (LDI_FD (ldi, arch64));
853
854 rs6000_xfer_shared_library (ldi, &obstack);
855
856 if (!LDI_NEXT (ldi, arch64))
857 break;
858 ldi = (LdInfo *) ((char *) ldi + LDI_NEXT (ldi, arch64));
859 }
860
861 xfree (ldi_data);
862
863 obstack_grow_str0 (&obstack, "</library-list-aix>\n");
864
865 buf = obstack_finish (&obstack);
866 len_avail = strlen (buf);
867 if (offset >= len_avail)
868 len= 0;
869 else
870 {
871 if (len > len_avail - offset)
872 len = len_avail - offset;
873 memcpy (readbuf, buf + offset, len);
874 }
875
876 obstack_free (&obstack, NULL);
877 return len;
878 }
879
880 void _initialize_rs6000_nat (void);
881
882 void
883 _initialize_rs6000_nat (void)
884 {
885 struct target_ops *t;
886
887 t = inf_ptrace_target ();
888 t->to_fetch_registers = rs6000_fetch_inferior_registers;
889 t->to_store_registers = rs6000_store_inferior_registers;
890 t->to_xfer_partial = rs6000_xfer_partial;
891
892 super_create_inferior = t->to_create_inferior;
893 t->to_create_inferior = rs6000_create_inferior;
894
895 t->to_wait = rs6000_wait;
896
897 add_target (t);
898 }
This page took 0.052771 seconds and 5 git commands to generate.