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