gdb/rs6000-aix-tdep.c - gdb

Global variables defined

Data types defined

Functions defined

Macros defined

Source code

  1. /* Native support code for PPC AIX, for GDB the GNU debugger.

  2.    Copyright (C) 2006-2015 Free Software Foundation, Inc.

  3.    Free Software Foundation, Inc.

  4.    This file is part of GDB.

  5.    This program is free software; you can redistribute it and/or modify
  6.    it under the terms of the GNU General Public License as published by
  7.    the Free Software Foundation; either version 3 of the License, or
  8.    (at your option) any later version.

  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.

  13.    You should have received a copy of the GNU General Public License
  14.    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */

  15. #include "defs.h"
  16. #include "osabi.h"
  17. #include "regcache.h"
  18. #include "regset.h"
  19. #include "gdbtypes.h"
  20. #include "gdbcore.h"
  21. #include "target.h"
  22. #include "value.h"
  23. #include "infcall.h"
  24. #include "objfiles.h"
  25. #include "breakpoint.h"
  26. #include "rs6000-tdep.h"
  27. #include "ppc-tdep.h"
  28. #include "rs6000-aix-tdep.h"
  29. #include "xcoffread.h"
  30. #include "solib.h"
  31. #include "solib-aix.h"
  32. #include "xml-utils.h"

  33. /* If the kernel has to deliver a signal, it pushes a sigcontext
  34.    structure on the stack and then calls the signal handler, passing
  35.    the address of the sigcontext in an argument register.  Usually
  36.    the signal handler doesn't save this register, so we have to
  37.    access the sigcontext structure via an offset from the signal handler
  38.    frame.
  39.    The following constants were determined by experimentation on AIX 3.2.  */
  40. #define SIG_FRAME_PC_OFFSET 96
  41. #define SIG_FRAME_LR_OFFSET 108
  42. #define SIG_FRAME_FP_OFFSET 284


  43. /* Core file support.  */

  44. static struct ppc_reg_offsets rs6000_aix32_reg_offsets =
  45. {
  46.   /* General-purpose registers.  */
  47.   208, /* r0_offset */
  48.   4/* gpr_size */
  49.   4/* xr_size */
  50.   24, /* pc_offset */
  51.   28, /* ps_offset */
  52.   32, /* cr_offset */
  53.   36, /* lr_offset */
  54.   40, /* ctr_offset */
  55.   44, /* xer_offset */
  56.   48, /* mq_offset */

  57.   /* Floating-point registers.  */
  58.   336, /* f0_offset */
  59.   56, /* fpscr_offset */
  60.   4/* fpscr_size */

  61.   /* AltiVec registers.  */
  62.   -1, /* vr0_offset */
  63.   -1, /* vscr_offset */
  64.   -1 /* vrsave_offset */
  65. };

  66. static struct ppc_reg_offsets rs6000_aix64_reg_offsets =
  67. {
  68.   /* General-purpose registers.  */
  69.   0, /* r0_offset */
  70.   8/* gpr_size */
  71.   4/* xr_size */
  72.   264, /* pc_offset */
  73.   256, /* ps_offset */
  74.   288, /* cr_offset */
  75.   272, /* lr_offset */
  76.   280, /* ctr_offset */
  77.   292, /* xer_offset */
  78.   -1, /* mq_offset */

  79.   /* Floating-point registers.  */
  80.   312, /* f0_offset */
  81.   296, /* fpscr_offset */
  82.   4/* fpscr_size */

  83.   /* AltiVec registers.  */
  84.   -1, /* vr0_offset */
  85.   -1, /* vscr_offset */
  86.   -1 /* vrsave_offset */
  87. };


  88. /* Supply register REGNUM in the general-purpose register set REGSET
  89.    from the buffer specified by GREGS and LEN to register cache
  90.    REGCACHE.  If REGNUM is -1, do this for all registers in REGSET.  */

  91. static void
  92. rs6000_aix_supply_regset (const struct regset *regset,
  93.                           struct regcache *regcache, int regnum,
  94.                           const void *gregs, size_t len)
  95. {
  96.   ppc_supply_gregset (regset, regcache, regnum, gregs, len);
  97.   ppc_supply_fpregset (regset, regcache, regnum, gregs, len);
  98. }

  99. /* Collect register REGNUM in the general-purpose register set
  100.    REGSET, from register cache REGCACHE into the buffer specified by
  101.    GREGS and LEN.  If REGNUM is -1, do this for all registers in
  102.    REGSET.  */

  103. static void
  104. rs6000_aix_collect_regset (const struct regset *regset,
  105.                            const struct regcache *regcache, int regnum,
  106.                            void *gregs, size_t len)
  107. {
  108.   ppc_collect_gregset (regset, regcache, regnum, gregs, len);
  109.   ppc_collect_fpregset (regset, regcache, regnum, gregs, len);
  110. }

  111. /* AIX register set.  */

  112. static const struct regset rs6000_aix32_regset =
  113. {
  114.   &rs6000_aix32_reg_offsets,
  115.   rs6000_aix_supply_regset,
  116.   rs6000_aix_collect_regset,
  117. };

  118. static const struct regset rs6000_aix64_regset =
  119. {
  120.   &rs6000_aix64_reg_offsets,
  121.   rs6000_aix_supply_regset,
  122.   rs6000_aix_collect_regset,
  123. };

  124. /* Iterate over core file register note sections.  */

  125. static void
  126. rs6000_aix_iterate_over_regset_sections (struct gdbarch *gdbarch,
  127.                                          iterate_over_regset_sections_cb *cb,
  128.                                          void *cb_data,
  129.                                          const struct regcache *regcache)
  130. {
  131.   if (gdbarch_tdep (gdbarch)->wordsize == 4)
  132.     cb (".reg", 592, &rs6000_aix32_regset, NULL, cb_data);
  133.   else
  134.     cb (".reg", 576, &rs6000_aix64_regset, NULL, cb_data);
  135. }


  136. /* Pass the arguments in either registers, or in the stack.  In RS/6000,
  137.    the first eight words of the argument list (that might be less than
  138.    eight parameters if some parameters occupy more than one word) are
  139.    passed in r3..r10 registers.  Float and double parameters are
  140.    passed in fpr's, in addition to that.  Rest of the parameters if any
  141.    are passed in user stack.  There might be cases in which half of the
  142.    parameter is copied into registers, the other half is pushed into
  143.    stack.

  144.    Stack must be aligned on 64-bit boundaries when synthesizing
  145.    function calls.

  146.    If the function is returning a structure, then the return address is passed
  147.    in r3, then the first 7 words of the parameters can be passed in registers,
  148.    starting from r4.  */

  149. static CORE_ADDR
  150. rs6000_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
  151.                         struct regcache *regcache, CORE_ADDR bp_addr,
  152.                         int nargs, struct value **args, CORE_ADDR sp,
  153.                         int struct_return, CORE_ADDR struct_addr)
  154. {
  155.   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
  156.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  157.   int ii;
  158.   int len = 0;
  159.   int argno;                        /* current argument number */
  160.   int argbytes;                        /* current argument byte */
  161.   gdb_byte tmp_buffer[50];
  162.   int f_argno = 0;                /* current floating point argno */
  163.   int wordsize = gdbarch_tdep (gdbarch)->wordsize;
  164.   CORE_ADDR func_addr = find_function_addr (function, NULL);

  165.   struct value *arg = 0;
  166.   struct type *type;

  167.   ULONGEST saved_sp;

  168.   /* The calling convention this function implements assumes the
  169.      processor has floating-point registers.  We shouldn't be using it
  170.      on PPC variants that lack them.  */
  171.   gdb_assert (ppc_floating_point_unit_p (gdbarch));

  172.   /* The first eight words of ther arguments are passed in registers.
  173.      Copy them appropriately.  */
  174.   ii = 0;

  175.   /* If the function is returning a `struct', then the first word
  176.      (which will be passed in r3) is used for struct return address.
  177.      In that case we should advance one word and start from r4
  178.      register to copy parameters.  */
  179.   if (struct_return)
  180.     {
  181.       regcache_raw_write_unsigned (regcache, tdep->ppc_gp0_regnum + 3,
  182.                                    struct_addr);
  183.       ii++;
  184.     }

  185. /* effectively indirect call... gcc does...

  186.    return_val example( float, int);

  187.    eabi:
  188.    float in fp0, int in r3
  189.    offset of stack on overflow 8/16
  190.    for varargs, must go by type.
  191.    power open:
  192.    float in r3&r4, int in r5
  193.    offset of stack on overflow different
  194.    both:
  195.    return in r3 or f0.  If no float, must study how gcc emulates floats;
  196.    pay attention to arg promotion.
  197.    User may have to cast\args to handle promotion correctly
  198.    since gdb won't know if prototype supplied or not.  */

  199.   for (argno = 0, argbytes = 0; argno < nargs && ii < 8; ++ii)
  200.     {
  201.       int reg_size = register_size (gdbarch, ii + 3);

  202.       arg = args[argno];
  203.       type = check_typedef (value_type (arg));
  204.       len = TYPE_LENGTH (type);

  205.       if (TYPE_CODE (type) == TYPE_CODE_FLT)
  206.         {
  207.           /* Floating point arguments are passed in fpr's, as well as gpr's.
  208.              There are 13 fpr's reserved for passing parameters.  At this point
  209.              there is no way we would run out of them.

  210.              Always store the floating point value using the register's
  211.              floating-point format.  */
  212.           const int fp_regnum = tdep->ppc_fp0_regnum + 1 + f_argno;
  213.           gdb_byte reg_val[MAX_REGISTER_SIZE];
  214.           struct type *reg_type = register_type (gdbarch, fp_regnum);

  215.           gdb_assert (len <= 8);

  216.           convert_typed_floating (value_contents (arg), type,
  217.                                   reg_val, reg_type);
  218.           regcache_cooked_write (regcache, fp_regnum, reg_val);
  219.           ++f_argno;
  220.         }

  221.       if (len > reg_size)
  222.         {

  223.           /* Argument takes more than one register.  */
  224.           while (argbytes < len)
  225.             {
  226.               gdb_byte word[MAX_REGISTER_SIZE];
  227.               memset (word, 0, reg_size);
  228.               memcpy (word,
  229.                       ((char *) value_contents (arg)) + argbytes,
  230.                       (len - argbytes) > reg_size
  231.                         ? reg_size : len - argbytes);
  232.               regcache_cooked_write (regcache,
  233.                                     tdep->ppc_gp0_regnum + 3 + ii,
  234.                                     word);
  235.               ++ii, argbytes += reg_size;

  236.               if (ii >= 8)
  237.                 goto ran_out_of_registers_for_arguments;
  238.             }
  239.           argbytes = 0;
  240.           --ii;
  241.         }
  242.       else
  243.         {
  244.           /* Argument can fit in one register.  No problem.  */
  245.           int adj = gdbarch_byte_order (gdbarch)
  246.                     == BFD_ENDIAN_BIG ? reg_size - len : 0;
  247.           gdb_byte word[MAX_REGISTER_SIZE];

  248.           memset (word, 0, reg_size);
  249.           memcpy (word, value_contents (arg), len);
  250.           regcache_cooked_write (regcache, tdep->ppc_gp0_regnum + 3 +ii, word);
  251.         }
  252.       ++argno;
  253.     }

  254. ran_out_of_registers_for_arguments:

  255.   regcache_cooked_read_unsigned (regcache,
  256.                                  gdbarch_sp_regnum (gdbarch),
  257.                                  &saved_sp);

  258.   /* Location for 8 parameters are always reserved.  */
  259.   sp -= wordsize * 8;

  260.   /* Another six words for back chain, TOC register, link register, etc.  */
  261.   sp -= wordsize * 6;

  262.   /* Stack pointer must be quadword aligned.  */
  263.   sp &= -16;

  264.   /* If there are more arguments, allocate space for them in
  265.      the stack, then push them starting from the ninth one.  */

  266.   if ((argno < nargs) || argbytes)
  267.     {
  268.       int space = 0, jj;

  269.       if (argbytes)
  270.         {
  271.           space += ((len - argbytes + 3) & -4);
  272.           jj = argno + 1;
  273.         }
  274.       else
  275.         jj = argno;

  276.       for (; jj < nargs; ++jj)
  277.         {
  278.           struct value *val = args[jj];
  279.           space += ((TYPE_LENGTH (value_type (val))) + 3) & -4;
  280.         }

  281.       /* Add location required for the rest of the parameters.  */
  282.       space = (space + 15) & -16;
  283.       sp -= space;

  284.       /* This is another instance we need to be concerned about
  285.          securing our stack space.  If we write anything underneath %sp
  286.          (r1), we might conflict with the kernel who thinks he is free
  287.          to use this area.  So, update %sp first before doing anything
  288.          else.  */

  289.       regcache_raw_write_signed (regcache,
  290.                                  gdbarch_sp_regnum (gdbarch), sp);

  291.       /* If the last argument copied into the registers didn't fit there
  292.          completely, push the rest of it into stack.  */

  293.       if (argbytes)
  294.         {
  295.           write_memory (sp + 24 + (ii * 4),
  296.                         value_contents (arg) + argbytes,
  297.                         len - argbytes);
  298.           ++argno;
  299.           ii += ((len - argbytes + 3) & -4) / 4;
  300.         }

  301.       /* Push the rest of the arguments into stack.  */
  302.       for (; argno < nargs; ++argno)
  303.         {

  304.           arg = args[argno];
  305.           type = check_typedef (value_type (arg));
  306.           len = TYPE_LENGTH (type);


  307.           /* Float types should be passed in fpr's, as well as in the
  308.              stack.  */
  309.           if (TYPE_CODE (type) == TYPE_CODE_FLT && f_argno < 13)
  310.             {

  311.               gdb_assert (len <= 8);

  312.               regcache_cooked_write (regcache,
  313.                                      tdep->ppc_fp0_regnum + 1 + f_argno,
  314.                                      value_contents (arg));
  315.               ++f_argno;
  316.             }

  317.           write_memory (sp + 24 + (ii * 4), value_contents (arg), len);
  318.           ii += ((len + 3) & -4) / 4;
  319.         }
  320.     }

  321.   /* Set the stack pointer.  According to the ABI, the SP is meant to
  322.      be set _before_ the corresponding stack space is used.  On AIX,
  323.      this even applies when the target has been completely stopped!
  324.      Not doing this can lead to conflicts with the kernel which thinks
  325.      that it still has control over this not-yet-allocated stack
  326.      region.  */
  327.   regcache_raw_write_signed (regcache, gdbarch_sp_regnum (gdbarch), sp);

  328.   /* Set back chain properly.  */
  329.   store_unsigned_integer (tmp_buffer, wordsize, byte_order, saved_sp);
  330.   write_memory (sp, tmp_buffer, wordsize);

  331.   /* Point the inferior function call's return address at the dummy's
  332.      breakpoint.  */
  333.   regcache_raw_write_signed (regcache, tdep->ppc_lr_regnum, bp_addr);

  334.   /* Set the TOC register value.  */
  335.   regcache_raw_write_signed (regcache, tdep->ppc_toc_regnum,
  336.                              solib_aix_get_toc_value (func_addr));

  337.   target_store_registers (regcache, -1);
  338.   return sp;
  339. }

  340. static enum return_value_convention
  341. rs6000_return_value (struct gdbarch *gdbarch, struct value *function,
  342.                      struct type *valtype, struct regcache *regcache,
  343.                      gdb_byte *readbuf, const gdb_byte *writebuf)
  344. {
  345.   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
  346.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);

  347.   /* The calling convention this function implements assumes the
  348.      processor has floating-point registers.  We shouldn't be using it
  349.      on PowerPC variants that lack them.  */
  350.   gdb_assert (ppc_floating_point_unit_p (gdbarch));

  351.   /* AltiVec extension: Functions that declare a vector data type as a
  352.      return value place that return value in VR2.  */
  353.   if (TYPE_CODE (valtype) == TYPE_CODE_ARRAY && TYPE_VECTOR (valtype)
  354.       && TYPE_LENGTH (valtype) == 16)
  355.     {
  356.       if (readbuf)
  357.         regcache_cooked_read (regcache, tdep->ppc_vr0_regnum + 2, readbuf);
  358.       if (writebuf)
  359.         regcache_cooked_write (regcache, tdep->ppc_vr0_regnum + 2, writebuf);

  360.       return RETURN_VALUE_REGISTER_CONVENTION;
  361.     }

  362.   /* If the called subprogram returns an aggregate, there exists an
  363.      implicit first argument, whose value is the address of a caller-
  364.      allocated buffer into which the callee is assumed to store its
  365.      return value.  All explicit parameters are appropriately
  366.      relabeled.  */
  367.   if (TYPE_CODE (valtype) == TYPE_CODE_STRUCT
  368.       || TYPE_CODE (valtype) == TYPE_CODE_UNION
  369.       || TYPE_CODE (valtype) == TYPE_CODE_ARRAY)
  370.     return RETURN_VALUE_STRUCT_CONVENTION;

  371.   /* Scalar floating-point values are returned in FPR1 for float or
  372.      double, and in FPR1:FPR2 for quadword precision.  Fortran
  373.      complex*8 and complex*16 are returned in FPR1:FPR2, and
  374.      complex*32 is returned in FPR1:FPR4.  */
  375.   if (TYPE_CODE (valtype) == TYPE_CODE_FLT
  376.       && (TYPE_LENGTH (valtype) == 4 || TYPE_LENGTH (valtype) == 8))
  377.     {
  378.       struct type *regtype = register_type (gdbarch, tdep->ppc_fp0_regnum);
  379.       gdb_byte regval[8];

  380.       /* FIXME: kettenis/2007-01-01: Add support for quadword
  381.          precision and complex.  */

  382.       if (readbuf)
  383.         {
  384.           regcache_cooked_read (regcache, tdep->ppc_fp0_regnum + 1, regval);
  385.           convert_typed_floating (regval, regtype, readbuf, valtype);
  386.         }
  387.       if (writebuf)
  388.         {
  389.           convert_typed_floating (writebuf, valtype, regval, regtype);
  390.           regcache_cooked_write (regcache, tdep->ppc_fp0_regnum + 1, regval);
  391.         }

  392.       return RETURN_VALUE_REGISTER_CONVENTION;
  393.   }

  394.   /* Values of the types int, long, short, pointer, and char (length
  395.      is less than or equal to four bytes), as well as bit values of
  396.      lengths less than or equal to 32 bits, must be returned right
  397.      justified in GPR3 with signed values sign extended and unsigned
  398.      values zero extended, as necessary.  */
  399.   if (TYPE_LENGTH (valtype) <= tdep->wordsize)
  400.     {
  401.       if (readbuf)
  402.         {
  403.           ULONGEST regval;

  404.           /* For reading we don't have to worry about sign extension.  */
  405.           regcache_cooked_read_unsigned (regcache, tdep->ppc_gp0_regnum + 3,
  406.                                          &regval);
  407.           store_unsigned_integer (readbuf, TYPE_LENGTH (valtype), byte_order,
  408.                                   regval);
  409.         }
  410.       if (writebuf)
  411.         {
  412.           /* For writing, use unpack_long since that should handle any
  413.              required sign extension.  */
  414.           regcache_cooked_write_unsigned (regcache, tdep->ppc_gp0_regnum + 3,
  415.                                           unpack_long (valtype, writebuf));
  416.         }

  417.       return RETURN_VALUE_REGISTER_CONVENTION;
  418.     }

  419.   /* Eight-byte non-floating-point scalar values must be returned in
  420.      GPR3:GPR4.  */

  421.   if (TYPE_LENGTH (valtype) == 8)
  422.     {
  423.       gdb_assert (TYPE_CODE (valtype) != TYPE_CODE_FLT);
  424.       gdb_assert (tdep->wordsize == 4);

  425.       if (readbuf)
  426.         {
  427.           gdb_byte regval[8];

  428.           regcache_cooked_read (regcache, tdep->ppc_gp0_regnum + 3, regval);
  429.           regcache_cooked_read (regcache, tdep->ppc_gp0_regnum + 4,
  430.                                 regval + 4);
  431.           memcpy (readbuf, regval, 8);
  432.         }
  433.       if (writebuf)
  434.         {
  435.           regcache_cooked_write (regcache, tdep->ppc_gp0_regnum + 3, writebuf);
  436.           regcache_cooked_write (regcache, tdep->ppc_gp0_regnum + 4,
  437.                                  writebuf + 4);
  438.         }

  439.       return RETURN_VALUE_REGISTER_CONVENTION;
  440.     }

  441.   return RETURN_VALUE_STRUCT_CONVENTION;
  442. }

  443. /* Support for CONVERT_FROM_FUNC_PTR_ADDR (ARCH, ADDR, TARG).

  444.    Usually a function pointer's representation is simply the address
  445.    of the function.  On the RS/6000 however, a function pointer is
  446.    represented by a pointer to an OPD entry.  This OPD entry contains
  447.    three words, the first word is the address of the function, the
  448.    second word is the TOC pointer (r2), and the third word is the
  449.    static chain value.  Throughout GDB it is currently assumed that a
  450.    function pointer contains the address of the function, which is not
  451.    easy to fix.  In addition, the conversion of a function address to
  452.    a function pointer would require allocation of an OPD entry in the
  453.    inferior's memory space, with all its drawbacks.  To be able to
  454.    call C++ virtual methods in the inferior (which are called via
  455.    function pointers), find_function_addr uses this function to get the
  456.    function address from a function pointer.  */

  457. /* Return real function address if ADDR (a function pointer) is in the data
  458.    space and is therefore a special function pointer.  */

  459. static CORE_ADDR
  460. rs6000_convert_from_func_ptr_addr (struct gdbarch *gdbarch,
  461.                                    CORE_ADDR addr,
  462.                                    struct target_ops *targ)
  463. {
  464.   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
  465.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  466.   struct obj_section *s;

  467.   s = find_pc_section (addr);

  468.   /* Normally, functions live inside a section that is executable.
  469.      So, if ADDR points to a non-executable section, then treat it
  470.      as a function descriptor and return the target address iff
  471.      the target address itself points to a section that is executable.  */
  472.   if (s && (s->the_bfd_section->flags & SEC_CODE) == 0)
  473.     {
  474.       CORE_ADDR pc = 0;
  475.       struct obj_section *pc_section;
  476.       volatile struct gdb_exception e;

  477.       TRY_CATCH (e, RETURN_MASK_ERROR)
  478.         {
  479.           pc = read_memory_unsigned_integer (addr, tdep->wordsize, byte_order);
  480.         }
  481.       if (e.reason < 0)
  482.         {
  483.           /* An error occured during reading.  Probably a memory error
  484.              due to the section not being loaded yet.  This address
  485.              cannot be a function descriptor.  */
  486.           return addr;
  487.         }
  488.       pc_section = find_pc_section (pc);

  489.       if (pc_section && (pc_section->the_bfd_section->flags & SEC_CODE))
  490.         return pc;
  491.     }

  492.   return addr;
  493. }


  494. /* Calculate the destination of a branch/jump.  Return -1 if not a branch.  */

  495. static CORE_ADDR
  496. branch_dest (struct frame_info *frame, int opcode, int instr,
  497.              CORE_ADDR pc, CORE_ADDR safety)
  498. {
  499.   struct gdbarch *gdbarch = get_frame_arch (frame);
  500.   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
  501.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  502.   CORE_ADDR dest;
  503.   int immediate;
  504.   int absolute;
  505.   int ext_op;

  506.   absolute = (int) ((instr >> 1) & 1);

  507.   switch (opcode)
  508.     {
  509.     case 18:
  510.       immediate = ((instr & ~3) << 6) >> 6;        /* br unconditional */
  511.       if (absolute)
  512.         dest = immediate;
  513.       else
  514.         dest = pc + immediate;
  515.       break;

  516.     case 16:
  517.       immediate = ((instr & ~3) << 16) >> 16;        /* br conditional */
  518.       if (absolute)
  519.         dest = immediate;
  520.       else
  521.         dest = pc + immediate;
  522.       break;

  523.     case 19:
  524.       ext_op = (instr >> 1) & 0x3ff;

  525.       if (ext_op == 16)                /* br conditional register */
  526.         {
  527.           dest = get_frame_register_unsigned (frame, tdep->ppc_lr_regnum) & ~3;

  528.           /* If we are about to return from a signal handler, dest is
  529.              something like 0x3c90.  The current frame is a signal handler
  530.              caller frame, upon completion of the sigreturn system call
  531.              execution will return to the saved PC in the frame.  */
  532.           if (dest < AIX_TEXT_SEGMENT_BASE)
  533.             dest = read_memory_unsigned_integer
  534.                      (get_frame_base (frame) + SIG_FRAME_PC_OFFSET,
  535.                       tdep->wordsize, byte_order);
  536.         }

  537.       else if (ext_op == 528)        /* br cond to count reg */
  538.         {
  539.           dest = get_frame_register_unsigned (frame,
  540.                                               tdep->ppc_ctr_regnum) & ~3;

  541.           /* If we are about to execute a system call, dest is something
  542.              like 0x22fc or 0x3b00.  Upon completion the system call
  543.              will return to the address in the link register.  */
  544.           if (dest < AIX_TEXT_SEGMENT_BASE)
  545.             dest = get_frame_register_unsigned (frame,
  546.                                                 tdep->ppc_lr_regnum) & ~3;
  547.         }
  548.       else
  549.         return -1;
  550.       break;

  551.     default:
  552.       return -1;
  553.     }
  554.   return (dest < AIX_TEXT_SEGMENT_BASE) ? safety : dest;
  555. }

  556. /* AIX does not support PT_STEP.  Simulate it.  */

  557. static int
  558. rs6000_software_single_step (struct frame_info *frame)
  559. {
  560.   struct gdbarch *gdbarch = get_frame_arch (frame);
  561.   struct address_space *aspace = get_frame_address_space (frame);
  562.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  563.   int ii, insn;
  564.   CORE_ADDR loc;
  565.   CORE_ADDR breaks[2];
  566.   int opcode;

  567.   loc = get_frame_pc (frame);

  568.   insn = read_memory_integer (loc, 4, byte_order);

  569.   if (ppc_deal_with_atomic_sequence (frame))
  570.     return 1;

  571.   breaks[0] = loc + PPC_INSN_SIZE;
  572.   opcode = insn >> 26;
  573.   breaks[1] = branch_dest (frame, opcode, insn, loc, breaks[0]);

  574.   /* Don't put two breakpoints on the same address.  */
  575.   if (breaks[1] == breaks[0])
  576.     breaks[1] = -1;

  577.   for (ii = 0; ii < 2; ++ii)
  578.     {
  579.       /* ignore invalid breakpoint.  */
  580.       if (breaks[ii] == -1)
  581.         continue;
  582.       insert_single_step_breakpoint (gdbarch, aspace, breaks[ii]);
  583.     }

  584.   errno = 0;                        /* FIXME, don't ignore errors!  */
  585.   /* What errors?  {read,write}_memory call error().  */
  586.   return 1;
  587. }

  588. /* Implement the "auto_wide_charset" gdbarch method for this platform.  */

  589. static const char *
  590. rs6000_aix_auto_wide_charset (void)
  591. {
  592.   return "UTF-16";
  593. }

  594. /* Implement an osabi sniffer for RS6000/AIX.

  595.    This function assumes that ABFD's flavour is XCOFF.  In other words,
  596.    it should be registered as a sniffer for bfd_target_xcoff_flavour
  597.    objfiles only.  A failed assertion will be raised if this condition
  598.    is not met.  */

  599. static enum gdb_osabi
  600. rs6000_aix_osabi_sniffer (bfd *abfd)
  601. {
  602.   gdb_assert (bfd_get_flavour (abfd) == bfd_target_xcoff_flavour);

  603.   /* The only noticeable difference between Lynx178 XCOFF files and
  604.      AIX XCOFF files comes from the fact that there are no shared
  605.      libraries on Lynx178.  On AIX, we are betting that an executable
  606.      linked with no shared library will never exist.  */
  607.   if (xcoff_get_n_import_files (abfd) <= 0)
  608.     return GDB_OSABI_UNKNOWN;

  609.   return GDB_OSABI_AIX;
  610. }

  611. /* A structure encoding the offset and size of a field within
  612.    a struct.  */

  613. struct field_info
  614. {
  615.   int offset;
  616.   int size;
  617. };

  618. /* A structure describing the layout of all the fields of interest
  619.    in AIX's struct ld_info.  Each field in this struct corresponds
  620.    to the field of the same name in struct ld_info.  */

  621. struct ld_info_desc
  622. {
  623.   struct field_info ldinfo_next;
  624.   struct field_info ldinfo_fd;
  625.   struct field_info ldinfo_textorg;
  626.   struct field_info ldinfo_textsize;
  627.   struct field_info ldinfo_dataorg;
  628.   struct field_info ldinfo_datasize;
  629.   struct field_info ldinfo_filename;
  630. };

  631. /* The following data has been generated by compiling and running
  632.    the following program on AIX 5.3.  */

  633. #if 0
  634. #include <stddef.h>
  635. #include <stdio.h>
  636. #define __LDINFO_PTRACE32__
  637. #define __LDINFO_PTRACE64__
  638. #include <sys/ldr.h>

  639. #define pinfo(type,member)                  \
  640.   {                                         \
  641.     struct type ldi = {0};                  \
  642.                                             \
  643.     printf ("  {%d, %d},\t/* %s */\n",      \
  644.             offsetof (struct type, member), \
  645.             sizeof (ldi.member),            \
  646.             #member);                       \
  647.   }                                         \
  648.   while (0)

  649. int
  650. main (void)
  651. {
  652.   printf ("static const struct ld_info_desc ld_info32_desc =\n{\n");
  653.   pinfo (__ld_info32, ldinfo_next);
  654.   pinfo (__ld_info32, ldinfo_fd);
  655.   pinfo (__ld_info32, ldinfo_textorg);
  656.   pinfo (__ld_info32, ldinfo_textsize);
  657.   pinfo (__ld_info32, ldinfo_dataorg);
  658.   pinfo (__ld_info32, ldinfo_datasize);
  659.   pinfo (__ld_info32, ldinfo_filename);
  660.   printf ("};\n");

  661.   printf ("\n");

  662.   printf ("static const struct ld_info_desc ld_info64_desc =\n{\n");
  663.   pinfo (__ld_info64, ldinfo_next);
  664.   pinfo (__ld_info64, ldinfo_fd);
  665.   pinfo (__ld_info64, ldinfo_textorg);
  666.   pinfo (__ld_info64, ldinfo_textsize);
  667.   pinfo (__ld_info64, ldinfo_dataorg);
  668.   pinfo (__ld_info64, ldinfo_datasize);
  669.   pinfo (__ld_info64, ldinfo_filename);
  670.   printf ("};\n");

  671.   return 0;
  672. }
  673. #endif /* 0 */

  674. /* Layout of the 32bit version of struct ld_info.  */

  675. static const struct ld_info_desc ld_info32_desc =
  676. {
  677.   {0, 4},       /* ldinfo_next */
  678.   {4, 4},       /* ldinfo_fd */
  679.   {8, 4},       /* ldinfo_textorg */
  680.   {12, 4},      /* ldinfo_textsize */
  681.   {16, 4},      /* ldinfo_dataorg */
  682.   {20, 4},      /* ldinfo_datasize */
  683.   {24, 2},      /* ldinfo_filename */
  684. };

  685. /* Layout of the 64bit version of struct ld_info.  */

  686. static const struct ld_info_desc ld_info64_desc =
  687. {
  688.   {0, 4},       /* ldinfo_next */
  689.   {8, 4},       /* ldinfo_fd */
  690.   {16, 8},      /* ldinfo_textorg */
  691.   {24, 8},      /* ldinfo_textsize */
  692.   {32, 8},      /* ldinfo_dataorg */
  693.   {40, 8},      /* ldinfo_datasize */
  694.   {48, 2},      /* ldinfo_filename */
  695. };

  696. /* A structured representation of one entry read from the ld_info
  697.    binary data provided by the AIX loader.  */

  698. struct ld_info
  699. {
  700.   ULONGEST next;
  701.   int fd;
  702.   CORE_ADDR textorg;
  703.   ULONGEST textsize;
  704.   CORE_ADDR dataorg;
  705.   ULONGEST datasize;
  706.   char *filename;
  707.   char *member_name;
  708. };

  709. /* Return a struct ld_info object corresponding to the entry at
  710.    LDI_BUF.

  711.    Note that the filename and member_name strings still point
  712.    to the data in LDI_BUF.  So LDI_BUF must not be deallocated
  713.    while the struct ld_info object returned is in use.  */

  714. static struct ld_info
  715. rs6000_aix_extract_ld_info (struct gdbarch *gdbarch,
  716.                             const gdb_byte *ldi_buf)
  717. {
  718.   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
  719.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  720.   struct type *ptr_type = builtin_type (gdbarch)->builtin_data_ptr;
  721.   const struct ld_info_desc desc
  722.     = tdep->wordsize == 8 ? ld_info64_desc : ld_info32_desc;
  723.   struct ld_info info;

  724.   info.next = extract_unsigned_integer (ldi_buf + desc.ldinfo_next.offset,
  725.                                         desc.ldinfo_next.size,
  726.                                         byte_order);
  727.   info.fd = extract_signed_integer (ldi_buf + desc.ldinfo_fd.offset,
  728.                                     desc.ldinfo_fd.size,
  729.                                     byte_order);
  730.   info.textorg = extract_typed_address (ldi_buf + desc.ldinfo_textorg.offset,
  731.                                         ptr_type);
  732.   info.textsize
  733.     = extract_unsigned_integer (ldi_buf + desc.ldinfo_textsize.offset,
  734.                                 desc.ldinfo_textsize.size,
  735.                                 byte_order);
  736.   info.dataorg = extract_typed_address (ldi_buf + desc.ldinfo_dataorg.offset,
  737.                                         ptr_type);
  738.   info.datasize
  739.     = extract_unsigned_integer (ldi_buf + desc.ldinfo_datasize.offset,
  740.                                 desc.ldinfo_datasize.size,
  741.                                 byte_order);
  742.   info.filename = (char *) ldi_buf + desc.ldinfo_filename.offset;
  743.   info.member_name = info.filename + strlen (info.filename) + 1;

  744.   return info;
  745. }

  746. /* Append to OBJSTACK an XML string description of the shared library
  747.    corresponding to LDI, following the TARGET_OBJECT_LIBRARIES_AIX
  748.    format.  */

  749. static void
  750. rs6000_aix_shared_library_to_xml (struct ld_info *ldi,
  751.                                   struct obstack *obstack)
  752. {
  753.   char *p;

  754.   obstack_grow_str (obstack, "<library name=\"");
  755.   p = xml_escape_text (ldi->filename);
  756.   obstack_grow_str (obstack, p);
  757.   xfree (p);
  758.   obstack_grow_str (obstack, "\"");

  759.   if (ldi->member_name[0] != '\0')
  760.     {
  761.       obstack_grow_str (obstack, " member=\"");
  762.       p = xml_escape_text (ldi->member_name);
  763.       obstack_grow_str (obstack, p);
  764.       xfree (p);
  765.       obstack_grow_str (obstack, "\"");
  766.     }

  767.   obstack_grow_str (obstack, " text_addr=\"");
  768.   obstack_grow_str (obstack, core_addr_to_string (ldi->textorg));
  769.   obstack_grow_str (obstack, "\"");

  770.   obstack_grow_str (obstack, " text_size=\"");
  771.   obstack_grow_str (obstack, pulongest (ldi->textsize));
  772.   obstack_grow_str (obstack, "\"");

  773.   obstack_grow_str (obstack, " data_addr=\"");
  774.   obstack_grow_str (obstack, core_addr_to_string (ldi->dataorg));
  775.   obstack_grow_str (obstack, "\"");

  776.   obstack_grow_str (obstack, " data_size=\"");
  777.   obstack_grow_str (obstack, pulongest (ldi->datasize));
  778.   obstack_grow_str (obstack, "\"");

  779.   obstack_grow_str (obstack, "></library>");
  780. }

  781. /* Convert the ld_info binary data provided by the AIX loader into
  782.    an XML representation following the TARGET_OBJECT_LIBRARIES_AIX
  783.    format.

  784.    LDI_BUF is a buffer containing the ld_info data.
  785.    READBUF, OFFSET and LEN follow the same semantics as target_ops'
  786.    to_xfer_partial target_ops method.

  787.    If CLOSE_LDINFO_FD is nonzero, then this routine also closes
  788.    the ldinfo_fd file descriptor.  This is useful when the ldinfo
  789.    data is obtained via ptrace, as ptrace opens a file descriptor
  790.    for each and every entry; but we cannot use this descriptor
  791.    as the consumer of the XML library list might live in a different
  792.    process.  */

  793. ULONGEST
  794. rs6000_aix_ld_info_to_xml (struct gdbarch *gdbarch, const gdb_byte *ldi_buf,
  795.                            gdb_byte *readbuf, ULONGEST offset, ULONGEST len,
  796.                            int close_ldinfo_fd)
  797. {
  798.   struct obstack obstack;
  799.   const char *buf;
  800.   ULONGEST len_avail;

  801.   obstack_init (&obstack);
  802.   obstack_grow_str (&obstack, "<library-list-aix version=\"1.0\">\n");

  803.   while (1)
  804.     {
  805.       struct ld_info ldi = rs6000_aix_extract_ld_info (gdbarch, ldi_buf);

  806.       rs6000_aix_shared_library_to_xml (&ldi, &obstack);
  807.       if (close_ldinfo_fd)
  808.         close (ldi.fd);

  809.       if (!ldi.next)
  810.         break;
  811.       ldi_buf = ldi_buf + ldi.next;
  812.     }

  813.   obstack_grow_str0 (&obstack, "</library-list-aix>\n");

  814.   buf = obstack_finish (&obstack);
  815.   len_avail = strlen (buf);
  816.   if (offset >= len_avail)
  817.     len= 0;
  818.   else
  819.     {
  820.       if (len > len_avail - offset)
  821.         len = len_avail - offset;
  822.       memcpy (readbuf, buf + offset, len);
  823.     }

  824.   obstack_free (&obstack, NULL);
  825.   return len;
  826. }

  827. /* Implement the core_xfer_shared_libraries_aix gdbarch method.  */

  828. static ULONGEST
  829. rs6000_aix_core_xfer_shared_libraries_aix (struct gdbarch *gdbarch,
  830.                                            gdb_byte *readbuf,
  831.                                            ULONGEST offset,
  832.                                            ULONGEST len)
  833. {
  834.   struct bfd_section *ldinfo_sec;
  835.   int ldinfo_size;
  836.   gdb_byte *ldinfo_buf;
  837.   struct cleanup *cleanup;
  838.   LONGEST result;

  839.   ldinfo_sec = bfd_get_section_by_name (core_bfd, ".ldinfo");
  840.   if (ldinfo_sec == NULL)
  841.     error (_("cannot find .ldinfo section from core file: %s"),
  842.            bfd_errmsg (bfd_get_error ()));
  843.   ldinfo_size = bfd_get_section_size (ldinfo_sec);

  844.   ldinfo_buf = xmalloc (ldinfo_size);
  845.   cleanup = make_cleanup (xfree, ldinfo_buf);

  846.   if (! bfd_get_section_contents (core_bfd, ldinfo_sec,
  847.                                   ldinfo_buf, 0, ldinfo_size))
  848.     error (_("unable to read .ldinfo section from core file: %s"),
  849.           bfd_errmsg (bfd_get_error ()));

  850.   result = rs6000_aix_ld_info_to_xml (gdbarch, ldinfo_buf, readbuf,
  851.                                       offset, len, 0);

  852.   do_cleanups (cleanup);
  853.   return result;
  854. }

  855. static void
  856. rs6000_aix_init_osabi (struct gdbarch_info info, struct gdbarch *gdbarch)
  857. {
  858.   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);

  859.   /* RS6000/AIX does not support PT_STEP.  Has to be simulated.  */
  860.   set_gdbarch_software_single_step (gdbarch, rs6000_software_single_step);

  861.   /* Displaced stepping is currently not supported in combination with
  862.      software single-stepping.  */
  863.   set_gdbarch_displaced_step_copy_insn (gdbarch, NULL);
  864.   set_gdbarch_displaced_step_fixup (gdbarch, NULL);
  865.   set_gdbarch_displaced_step_free_closure (gdbarch, NULL);
  866.   set_gdbarch_displaced_step_location (gdbarch, NULL);

  867.   set_gdbarch_push_dummy_call (gdbarch, rs6000_push_dummy_call);
  868.   set_gdbarch_return_value (gdbarch, rs6000_return_value);
  869.   set_gdbarch_long_double_bit (gdbarch, 8 * TARGET_CHAR_BIT);

  870.   /* Handle RS/6000 function pointers (which are really function
  871.      descriptors).  */
  872.   set_gdbarch_convert_from_func_ptr_addr
  873.     (gdbarch, rs6000_convert_from_func_ptr_addr);

  874.   /* Core file support.  */
  875.   set_gdbarch_iterate_over_regset_sections
  876.     (gdbarch, rs6000_aix_iterate_over_regset_sections);
  877.   set_gdbarch_core_xfer_shared_libraries_aix
  878.     (gdbarch, rs6000_aix_core_xfer_shared_libraries_aix);

  879.   if (tdep->wordsize == 8)
  880.     tdep->lr_frame_offset = 16;
  881.   else
  882.     tdep->lr_frame_offset = 8;

  883.   if (tdep->wordsize == 4)
  884.     /* PowerOpen / AIX 32 bit.  The saved area or red zone consists of
  885.        19 4 byte GPRS + 18 8 byte FPRs giving a total of 220 bytes.
  886.        Problem is, 220 isn't frame (16 byte) aligned.  Round it up to
  887.        224.  */
  888.     set_gdbarch_frame_red_zone_size (gdbarch, 224);
  889.   else
  890.     set_gdbarch_frame_red_zone_size (gdbarch, 0);

  891.   set_gdbarch_auto_wide_charset (gdbarch, rs6000_aix_auto_wide_charset);

  892.   set_solib_ops (gdbarch, &solib_aix_so_ops);
  893. }

  894. /* Provide a prototype to silence -Wmissing-prototypes.  */
  895. extern initialize_file_ftype _initialize_rs6000_aix_tdep;

  896. void
  897. _initialize_rs6000_aix_tdep (void)
  898. {
  899.   gdbarch_register_osabi_sniffer (bfd_arch_rs6000,
  900.                                   bfd_target_xcoff_flavour,
  901.                                   rs6000_aix_osabi_sniffer);
  902.   gdbarch_register_osabi_sniffer (bfd_arch_powerpc,
  903.                                   bfd_target_xcoff_flavour,
  904.                                   rs6000_aix_osabi_sniffer);

  905.   gdbarch_register_osabi (bfd_arch_rs6000, 0, GDB_OSABI_AIX,
  906.                           rs6000_aix_init_osabi);
  907.   gdbarch_register_osabi (bfd_arch_powerpc, 0, GDB_OSABI_AIX,
  908.                           rs6000_aix_init_osabi);
  909. }