gdb/rs6000-lynx178-tdep.c - gdb

Functions defined

Source code

  1. /* Copyright (C) 2012-2015 Free Software Foundation, Inc.

  2.    This file is part of GDB.

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

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

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

  13. #include "defs.h"
  14. #include "osabi.h"
  15. #include "regcache.h"
  16. #include "gdbcore.h"
  17. #include "gdbtypes.h"
  18. #include "infcall.h"
  19. #include "ppc-tdep.h"
  20. #include "value.h"
  21. #include "xcoffread.h"

  22. /* Implement the "push_dummy_call" gdbarch method.  */

  23. static CORE_ADDR
  24. rs6000_lynx178_push_dummy_call (struct gdbarch *gdbarch,
  25.                                 struct value *function,
  26.                                 struct regcache *regcache, CORE_ADDR bp_addr,
  27.                                 int nargs, struct value **args, CORE_ADDR sp,
  28.                                 int struct_return, CORE_ADDR struct_addr)
  29. {
  30.   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
  31.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  32.   int ii;
  33.   int len = 0;
  34.   int argno;                        /* current argument number */
  35.   int argbytes;                        /* current argument byte */
  36.   gdb_byte tmp_buffer[50];
  37.   int f_argno = 0;                /* current floating point argno */
  38.   int wordsize = gdbarch_tdep (gdbarch)->wordsize;
  39.   CORE_ADDR func_addr = find_function_addr (function, NULL);

  40.   struct value *arg = 0;
  41.   struct type *type;

  42.   ULONGEST saved_sp;

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

  47.   /* The first eight words of ther arguments are passed in registers.
  48.      Copy them appropriately.  */
  49.   ii = 0;

  50.   /* If the function is returning a `struct', then the first word
  51.      (which will be passed in r3) is used for struct return address.
  52.      In that case we should advance one word and start from r4
  53.      register to copy parameters.  */
  54.   if (struct_return)
  55.     {
  56.       regcache_raw_write_unsigned (regcache, tdep->ppc_gp0_regnum + 3,
  57.                                    struct_addr);
  58.       ii++;
  59.     }

  60.   /* Effectively indirect call... gcc does...

  61.      return_val example( float, int);

  62.      eabi:
  63.      float in fp0, int in r3
  64.      offset of stack on overflow 8/16
  65.      for varargs, must go by type.
  66.      power open:
  67.      float in r3&r4, int in r5
  68.      offset of stack on overflow different
  69.      both:
  70.      return in r3 or f0.  If no float, must study how gcc emulates floats;
  71.      pay attention to arg promotion.
  72.      User may have to cast\args to handle promotion correctly
  73.      since gdb won't know if prototype supplied or not.  */

  74.   for (argno = 0, argbytes = 0; argno < nargs && ii < 8; ++ii)
  75.     {
  76.       int reg_size = register_size (gdbarch, ii + 3);

  77.       arg = args[argno];
  78.       type = check_typedef (value_type (arg));
  79.       len = TYPE_LENGTH (type);

  80.       if (TYPE_CODE (type) == TYPE_CODE_FLT)
  81.         {

  82.           /* Floating point arguments are passed in fpr's, as well as gpr's.
  83.              There are 13 fpr's reserved for passing parameters.  At this point
  84.              there is no way we would run out of them.

  85.              Always store the floating point value using the register's
  86.              floating-point format.  */
  87.           const int fp_regnum = tdep->ppc_fp0_regnum + 1 + f_argno;
  88.           gdb_byte reg_val[MAX_REGISTER_SIZE];
  89.           struct type *reg_type = register_type (gdbarch, fp_regnum);

  90.           gdb_assert (len <= 8);

  91.           convert_typed_floating (value_contents (arg), type,
  92.                                   reg_val, reg_type);
  93.           regcache_cooked_write (regcache, fp_regnum, reg_val);
  94.           ++f_argno;
  95.         }

  96.       if (len > reg_size)
  97.         {

  98.           /* Argument takes more than one register.  */
  99.           while (argbytes < len)
  100.             {
  101.               gdb_byte word[MAX_REGISTER_SIZE];
  102.               memset (word, 0, reg_size);
  103.               memcpy (word,
  104.                       ((char *) value_contents (arg)) + argbytes,
  105.                       (len - argbytes) > reg_size
  106.                         ? reg_size : len - argbytes);
  107.               regcache_cooked_write (regcache,
  108.                                     tdep->ppc_gp0_regnum + 3 + ii,
  109.                                     word);
  110.               ++ii, argbytes += reg_size;

  111.               if (ii >= 8)
  112.                 goto ran_out_of_registers_for_arguments;
  113.             }
  114.           argbytes = 0;
  115.           --ii;
  116.         }
  117.       else
  118.         {
  119.           /* Argument can fit in one register.  No problem.  */
  120.           int adj = gdbarch_byte_order (gdbarch)
  121.                     == BFD_ENDIAN_BIG ? reg_size - len : 0;
  122.           gdb_byte word[MAX_REGISTER_SIZE];

  123.           memset (word, 0, reg_size);
  124.           memcpy (word, value_contents (arg), len);
  125.           regcache_cooked_write (regcache, tdep->ppc_gp0_regnum + 3 +ii, word);
  126.         }
  127.       ++argno;
  128.     }

  129. ran_out_of_registers_for_arguments:

  130.   regcache_cooked_read_unsigned (regcache,
  131.                                  gdbarch_sp_regnum (gdbarch),
  132.                                  &saved_sp);

  133.   /* Location for 8 parameters are always reserved.  */
  134.   sp -= wordsize * 8;

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

  137.   /* Stack pointer must be quadword aligned.  */
  138.   sp = align_down (sp, 16);

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

  141.   if ((argno < nargs) || argbytes)
  142.     {
  143.       int space = 0, jj;

  144.       if (argbytes)
  145.         {
  146.           space += align_up (len - argbytes, 4);
  147.           jj = argno + 1;
  148.         }
  149.       else
  150.         jj = argno;

  151.       for (; jj < nargs; ++jj)
  152.         {
  153.           struct value *val = args[jj];

  154.           space += align_up (TYPE_LENGTH (value_type (val)), 4);
  155.         }

  156.       /* Add location required for the rest of the parameters.  */
  157.       space = align_up (space, 16);
  158.       sp -= space;

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

  164.       regcache_raw_write_signed (regcache,
  165.                                  gdbarch_sp_regnum (gdbarch), sp);

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

  168.       if (argbytes)
  169.         {
  170.           write_memory (sp + 24 + (ii * 4),
  171.                         value_contents (arg) + argbytes,
  172.                         len - argbytes);
  173.           ++argno;
  174.           ii += align_up (len - argbytes, 4) / 4;
  175.         }

  176.       /* Push the rest of the arguments into stack.  */
  177.       for (; argno < nargs; ++argno)
  178.         {

  179.           arg = args[argno];
  180.           type = check_typedef (value_type (arg));
  181.           len = TYPE_LENGTH (type);


  182.           /* Float types should be passed in fpr's, as well as in the
  183.              stack.  */
  184.           if (TYPE_CODE (type) == TYPE_CODE_FLT && f_argno < 13)
  185.             {

  186.               gdb_assert (len <= 8);

  187.               regcache_cooked_write (regcache,
  188.                                      tdep->ppc_fp0_regnum + 1 + f_argno,
  189.                                      value_contents (arg));
  190.               ++f_argno;
  191.             }

  192.           write_memory (sp + 24 + (ii * 4), value_contents (arg), len);
  193.           ii += align_up (len, 4) / 4;
  194.         }
  195.     }

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

  203.   /* Set back chain properly.  */
  204.   store_unsigned_integer (tmp_buffer, wordsize, byte_order, saved_sp);
  205.   write_memory (sp, tmp_buffer, wordsize);

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

  209.   target_store_registers (regcache, -1);
  210.   return sp;
  211. }

  212. /* Implement the "return_value" gdbarch method.  */

  213. static enum return_value_convention
  214. rs6000_lynx178_return_value (struct gdbarch *gdbarch, struct value *function,
  215.                              struct type *valtype, struct regcache *regcache,
  216.                              gdb_byte *readbuf, const gdb_byte *writebuf)
  217. {
  218.   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
  219.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);

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

  224.   /* AltiVec extension: Functions that declare a vector data type as a
  225.      return value place that return value in VR2.  */
  226.   if (TYPE_CODE (valtype) == TYPE_CODE_ARRAY && TYPE_VECTOR (valtype)
  227.       && TYPE_LENGTH (valtype) == 16)
  228.     {
  229.       if (readbuf)
  230.         regcache_cooked_read (regcache, tdep->ppc_vr0_regnum + 2, readbuf);
  231.       if (writebuf)
  232.         regcache_cooked_write (regcache, tdep->ppc_vr0_regnum + 2, writebuf);

  233.       return RETURN_VALUE_REGISTER_CONVENTION;
  234.     }

  235.   /* If the called subprogram returns an aggregate, there exists an
  236.      implicit first argument, whose value is the address of a caller-
  237.      allocated buffer into which the callee is assumed to store its
  238.      return value.  All explicit parameters are appropriately
  239.      relabeled.  */
  240.   if (TYPE_CODE (valtype) == TYPE_CODE_STRUCT
  241.       || TYPE_CODE (valtype) == TYPE_CODE_UNION
  242.       || TYPE_CODE (valtype) == TYPE_CODE_ARRAY)
  243.     return RETURN_VALUE_STRUCT_CONVENTION;

  244.   /* Scalar floating-point values are returned in FPR1 for float or
  245.      double, and in FPR1:FPR2 for quadword precision.  Fortran
  246.      complex*8 and complex*16 are returned in FPR1:FPR2, and
  247.      complex*32 is returned in FPR1:FPR4.  */
  248.   if (TYPE_CODE (valtype) == TYPE_CODE_FLT
  249.       && (TYPE_LENGTH (valtype) == 4 || TYPE_LENGTH (valtype) == 8))
  250.     {
  251.       struct type *regtype = register_type (gdbarch, tdep->ppc_fp0_regnum);
  252.       gdb_byte regval[8];

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

  255.       if (readbuf)
  256.         {
  257.           regcache_cooked_read (regcache, tdep->ppc_fp0_regnum + 1, regval);
  258.           convert_typed_floating (regval, regtype, readbuf, valtype);
  259.         }
  260.       if (writebuf)
  261.         {
  262.           convert_typed_floating (writebuf, valtype, regval, regtype);
  263.           regcache_cooked_write (regcache, tdep->ppc_fp0_regnum + 1, regval);
  264.         }

  265.       return RETURN_VALUE_REGISTER_CONVENTION;
  266.   }

  267.   /* Values of the types int, long, short, pointer, and char (length
  268.      is less than or equal to four bytes), as well as bit values of
  269.      lengths less than or equal to 32 bits, must be returned right
  270.      justified in GPR3 with signed values sign extended and unsigned
  271.      values zero extended, as necessary.  */
  272.   if (TYPE_LENGTH (valtype) <= tdep->wordsize)
  273.     {
  274.       if (readbuf)
  275.         {
  276.           ULONGEST regval;

  277.           /* For reading we don't have to worry about sign extension.  */
  278.           regcache_cooked_read_unsigned (regcache, tdep->ppc_gp0_regnum + 3,
  279.                                          &regval);
  280.           store_unsigned_integer (readbuf, TYPE_LENGTH (valtype), byte_order,
  281.                                   regval);
  282.         }
  283.       if (writebuf)
  284.         {
  285.           /* For writing, use unpack_long since that should handle any
  286.              required sign extension.  */
  287.           regcache_cooked_write_unsigned (regcache, tdep->ppc_gp0_regnum + 3,
  288.                                           unpack_long (valtype, writebuf));
  289.         }

  290.       return RETURN_VALUE_REGISTER_CONVENTION;
  291.     }

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

  294.   if (TYPE_LENGTH (valtype) == 8)
  295.     {
  296.       gdb_assert (TYPE_CODE (valtype) != TYPE_CODE_FLT);
  297.       gdb_assert (tdep->wordsize == 4);

  298.       if (readbuf)
  299.         {
  300.           gdb_byte regval[8];

  301.           regcache_cooked_read (regcache, tdep->ppc_gp0_regnum + 3, regval);
  302.           regcache_cooked_read (regcache, tdep->ppc_gp0_regnum + 4,
  303.                                 regval + 4);
  304.           memcpy (readbuf, regval, 8);
  305.         }
  306.       if (writebuf)
  307.         {
  308.           regcache_cooked_write (regcache, tdep->ppc_gp0_regnum + 3, writebuf);
  309.           regcache_cooked_write (regcache, tdep->ppc_gp0_regnum + 4,
  310.                                  writebuf + 4);
  311.         }

  312.       return RETURN_VALUE_REGISTER_CONVENTION;
  313.     }

  314.   return RETURN_VALUE_STRUCT_CONVENTION;
  315. }

  316. /* PowerPC Lynx178 OSABI sniffer.  */

  317. static enum gdb_osabi
  318. rs6000_lynx178_osabi_sniffer (bfd *abfd)
  319. {
  320.   if (bfd_get_flavour (abfd) != bfd_target_xcoff_flavour)
  321.     return GDB_OSABI_UNKNOWN;

  322.   /* The only noticeable difference between Lynx178 XCOFF files and
  323.      AIX XCOFF files comes from the fact that there are no shared
  324.      libraries on Lynx178.  So if the number of import files is
  325.      different from zero, it cannot be a Lynx178 binary.  */
  326.   if (xcoff_get_n_import_files (abfd) != 0)
  327.     return GDB_OSABI_UNKNOWN;

  328.   return GDB_OSABI_LYNXOS178;
  329. }

  330. /* Callback for powerpc-lynx178 initialization.  */

  331. static void
  332. rs6000_lynx178_init_osabi (struct gdbarch_info info, struct gdbarch *gdbarch)
  333. {
  334.   set_gdbarch_push_dummy_call (gdbarch, rs6000_lynx178_push_dummy_call);
  335.   set_gdbarch_return_value (gdbarch, rs6000_lynx178_return_value);
  336.   set_gdbarch_long_double_bit (gdbarch, 8 * TARGET_CHAR_BIT);
  337. }

  338. /* -Wmissing-prototypes.  */
  339. extern initialize_file_ftype _initialize_rs6000_lynx178_tdep;

  340. void
  341. _initialize_rs6000_lynx178_tdep (void)
  342. {
  343.   gdbarch_register_osabi_sniffer (bfd_arch_rs6000,
  344.                                   bfd_target_xcoff_flavour,
  345.                                   rs6000_lynx178_osabi_sniffer);
  346.   gdbarch_register_osabi (bfd_arch_rs6000, 0, GDB_OSABI_LYNXOS178,
  347.                           rs6000_lynx178_init_osabi);
  348. }