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