GDB (API)
/home/stan/gdb/src/gdb/rs6000-lynx178-tdep.c
Go to the documentation of this file.
00001 /* Copyright (C) 2012-2013 Free Software Foundation, Inc.
00002 
00003    This file is part of GDB.
00004 
00005    This program is free software; you can redistribute it and/or modify
00006    it under the terms of the GNU General Public License as published by
00007    the Free Software Foundation; either version 3 of the License, or
00008    (at your option) any later version.
00009 
00010    This program is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013    GNU General Public License for more details.
00014 
00015    You should have received a copy of the GNU General Public License
00016    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
00017 
00018 #include "defs.h"
00019 #include "osabi.h"
00020 #include "regcache.h"
00021 #include "gdbcore.h"
00022 #include "gdbtypes.h"
00023 #include "infcall.h"
00024 #include "ppc-tdep.h"
00025 #include "value.h"
00026 #include "xcoffread.h"
00027 
00028 /* Implement the "push_dummy_call" gdbarch method.  */
00029 
00030 static CORE_ADDR
00031 rs6000_lynx178_push_dummy_call (struct gdbarch *gdbarch,
00032                                 struct value *function,
00033                                 struct regcache *regcache, CORE_ADDR bp_addr,
00034                                 int nargs, struct value **args, CORE_ADDR sp,
00035                                 int struct_return, CORE_ADDR struct_addr)
00036 {
00037   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
00038   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
00039   int ii;
00040   int len = 0;
00041   int argno;                    /* current argument number */
00042   int argbytes;                 /* current argument byte */
00043   gdb_byte tmp_buffer[50];
00044   int f_argno = 0;              /* current floating point argno */
00045   int wordsize = gdbarch_tdep (gdbarch)->wordsize;
00046   CORE_ADDR func_addr = find_function_addr (function, NULL);
00047 
00048   struct value *arg = 0;
00049   struct type *type;
00050 
00051   ULONGEST saved_sp;
00052 
00053   /* The calling convention this function implements assumes the
00054      processor has floating-point registers.  We shouldn't be using it
00055      on PPC variants that lack them.  */
00056   gdb_assert (ppc_floating_point_unit_p (gdbarch));
00057 
00058   /* The first eight words of ther arguments are passed in registers.
00059      Copy them appropriately.  */
00060   ii = 0;
00061 
00062   /* If the function is returning a `struct', then the first word
00063      (which will be passed in r3) is used for struct return address.
00064      In that case we should advance one word and start from r4
00065      register to copy parameters.  */
00066   if (struct_return)
00067     {
00068       regcache_raw_write_unsigned (regcache, tdep->ppc_gp0_regnum + 3,
00069                                    struct_addr);
00070       ii++;
00071     }
00072 
00073   /* Effectively indirect call... gcc does...
00074 
00075      return_val example( float, int);
00076 
00077      eabi:
00078      float in fp0, int in r3
00079      offset of stack on overflow 8/16
00080      for varargs, must go by type.
00081      power open:
00082      float in r3&r4, int in r5
00083      offset of stack on overflow different
00084      both:
00085      return in r3 or f0.  If no float, must study how gcc emulates floats;
00086      pay attention to arg promotion.
00087      User may have to cast\args to handle promotion correctly
00088      since gdb won't know if prototype supplied or not.  */
00089 
00090   for (argno = 0, argbytes = 0; argno < nargs && ii < 8; ++ii)
00091     {
00092       int reg_size = register_size (gdbarch, ii + 3);
00093 
00094       arg = args[argno];
00095       type = check_typedef (value_type (arg));
00096       len = TYPE_LENGTH (type);
00097 
00098       if (TYPE_CODE (type) == TYPE_CODE_FLT)
00099         {
00100 
00101           /* Floating point arguments are passed in fpr's, as well as gpr's.
00102              There are 13 fpr's reserved for passing parameters.  At this point
00103              there is no way we would run out of them.
00104 
00105              Always store the floating point value using the register's
00106              floating-point format.  */
00107           const int fp_regnum = tdep->ppc_fp0_regnum + 1 + f_argno;
00108           gdb_byte reg_val[MAX_REGISTER_SIZE];
00109           struct type *reg_type = register_type (gdbarch, fp_regnum);
00110 
00111           gdb_assert (len <= 8);
00112 
00113           convert_typed_floating (value_contents (arg), type,
00114                                   reg_val, reg_type);
00115           regcache_cooked_write (regcache, fp_regnum, reg_val);
00116           ++f_argno;
00117         }
00118 
00119       if (len > reg_size)
00120         {
00121 
00122           /* Argument takes more than one register.  */
00123           while (argbytes < len)
00124             {
00125               gdb_byte word[MAX_REGISTER_SIZE];
00126               memset (word, 0, reg_size);
00127               memcpy (word,
00128                       ((char *) value_contents (arg)) + argbytes,
00129                       (len - argbytes) > reg_size
00130                         ? reg_size : len - argbytes);
00131               regcache_cooked_write (regcache,
00132                                     tdep->ppc_gp0_regnum + 3 + ii,
00133                                     word);
00134               ++ii, argbytes += reg_size;
00135 
00136               if (ii >= 8)
00137                 goto ran_out_of_registers_for_arguments;
00138             }
00139           argbytes = 0;
00140           --ii;
00141         }
00142       else
00143         {
00144           /* Argument can fit in one register.  No problem.  */
00145           int adj = gdbarch_byte_order (gdbarch)
00146                     == BFD_ENDIAN_BIG ? reg_size - len : 0;
00147           gdb_byte word[MAX_REGISTER_SIZE];
00148 
00149           memset (word, 0, reg_size);
00150           memcpy (word, value_contents (arg), len);
00151           regcache_cooked_write (regcache, tdep->ppc_gp0_regnum + 3 +ii, word);
00152         }
00153       ++argno;
00154     }
00155 
00156 ran_out_of_registers_for_arguments:
00157 
00158   regcache_cooked_read_unsigned (regcache,
00159                                  gdbarch_sp_regnum (gdbarch),
00160                                  &saved_sp);
00161 
00162   /* Location for 8 parameters are always reserved.  */
00163   sp -= wordsize * 8;
00164 
00165   /* Another six words for back chain, TOC register, link register, etc.  */
00166   sp -= wordsize * 6;
00167 
00168   /* Stack pointer must be quadword aligned.  */
00169   sp = align_down (sp, 16);
00170 
00171   /* If there are more arguments, allocate space for them in
00172      the stack, then push them starting from the ninth one.  */
00173 
00174   if ((argno < nargs) || argbytes)
00175     {
00176       int space = 0, jj;
00177 
00178       if (argbytes)
00179         {
00180           space += align_up (len - argbytes, 4);
00181           jj = argno + 1;
00182         }
00183       else
00184         jj = argno;
00185 
00186       for (; jj < nargs; ++jj)
00187         {
00188           struct value *val = args[jj];
00189 
00190           space += align_up (TYPE_LENGTH (value_type (val)), 4);
00191         }
00192 
00193       /* Add location required for the rest of the parameters.  */
00194       space = align_up (space, 16);
00195       sp -= space;
00196 
00197       /* This is another instance we need to be concerned about
00198          securing our stack space.  If we write anything underneath %sp
00199          (r1), we might conflict with the kernel who thinks he is free
00200          to use this area.  So, update %sp first before doing anything
00201          else.  */
00202 
00203       regcache_raw_write_signed (regcache,
00204                                  gdbarch_sp_regnum (gdbarch), sp);
00205 
00206       /* If the last argument copied into the registers didn't fit there
00207          completely, push the rest of it into stack.  */
00208 
00209       if (argbytes)
00210         {
00211           write_memory (sp + 24 + (ii * 4),
00212                         value_contents (arg) + argbytes,
00213                         len - argbytes);
00214           ++argno;
00215           ii += align_up (len - argbytes, 4) / 4;
00216         }
00217 
00218       /* Push the rest of the arguments into stack.  */
00219       for (; argno < nargs; ++argno)
00220         {
00221 
00222           arg = args[argno];
00223           type = check_typedef (value_type (arg));
00224           len = TYPE_LENGTH (type);
00225 
00226 
00227           /* Float types should be passed in fpr's, as well as in the
00228              stack.  */
00229           if (TYPE_CODE (type) == TYPE_CODE_FLT && f_argno < 13)
00230             {
00231 
00232               gdb_assert (len <= 8);
00233 
00234               regcache_cooked_write (regcache,
00235                                      tdep->ppc_fp0_regnum + 1 + f_argno,
00236                                      value_contents (arg));
00237               ++f_argno;
00238             }
00239 
00240           write_memory (sp + 24 + (ii * 4), value_contents (arg), len);
00241           ii += align_up (len, 4) / 4;
00242         }
00243     }
00244 
00245   /* Set the stack pointer.  According to the ABI, the SP is meant to
00246      be set _before_ the corresponding stack space is used.  On AIX,
00247      this even applies when the target has been completely stopped!
00248      Not doing this can lead to conflicts with the kernel which thinks
00249      that it still has control over this not-yet-allocated stack
00250      region.  */
00251   regcache_raw_write_signed (regcache, gdbarch_sp_regnum (gdbarch), sp);
00252 
00253   /* Set back chain properly.  */
00254   store_unsigned_integer (tmp_buffer, wordsize, byte_order, saved_sp);
00255   write_memory (sp, tmp_buffer, wordsize);
00256 
00257   /* Point the inferior function call's return address at the dummy's
00258      breakpoint.  */
00259   regcache_raw_write_signed (regcache, tdep->ppc_lr_regnum, bp_addr);
00260 
00261   target_store_registers (regcache, -1);
00262   return sp;
00263 }
00264 
00265 /* Implement the "return_value" gdbarch method.  */
00266 
00267 static enum return_value_convention
00268 rs6000_lynx178_return_value (struct gdbarch *gdbarch, struct value *function,
00269                              struct type *valtype, struct regcache *regcache,
00270                              gdb_byte *readbuf, const gdb_byte *writebuf)
00271 {
00272   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
00273   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
00274 
00275   /* The calling convention this function implements assumes the
00276      processor has floating-point registers.  We shouldn't be using it
00277      on PowerPC variants that lack them.  */
00278   gdb_assert (ppc_floating_point_unit_p (gdbarch));
00279 
00280   /* AltiVec extension: Functions that declare a vector data type as a
00281      return value place that return value in VR2.  */
00282   if (TYPE_CODE (valtype) == TYPE_CODE_ARRAY && TYPE_VECTOR (valtype)
00283       && TYPE_LENGTH (valtype) == 16)
00284     {
00285       if (readbuf)
00286         regcache_cooked_read (regcache, tdep->ppc_vr0_regnum + 2, readbuf);
00287       if (writebuf)
00288         regcache_cooked_write (regcache, tdep->ppc_vr0_regnum + 2, writebuf);
00289 
00290       return RETURN_VALUE_REGISTER_CONVENTION;
00291     }
00292 
00293   /* If the called subprogram returns an aggregate, there exists an
00294      implicit first argument, whose value is the address of a caller-
00295      allocated buffer into which the callee is assumed to store its
00296      return value.  All explicit parameters are appropriately
00297      relabeled.  */
00298   if (TYPE_CODE (valtype) == TYPE_CODE_STRUCT
00299       || TYPE_CODE (valtype) == TYPE_CODE_UNION
00300       || TYPE_CODE (valtype) == TYPE_CODE_ARRAY)
00301     return RETURN_VALUE_STRUCT_CONVENTION;
00302 
00303   /* Scalar floating-point values are returned in FPR1 for float or
00304      double, and in FPR1:FPR2 for quadword precision.  Fortran
00305      complex*8 and complex*16 are returned in FPR1:FPR2, and
00306      complex*32 is returned in FPR1:FPR4.  */
00307   if (TYPE_CODE (valtype) == TYPE_CODE_FLT
00308       && (TYPE_LENGTH (valtype) == 4 || TYPE_LENGTH (valtype) == 8))
00309     {
00310       struct type *regtype = register_type (gdbarch, tdep->ppc_fp0_regnum);
00311       gdb_byte regval[8];
00312 
00313       /* FIXME: kettenis/2007-01-01: Add support for quadword
00314          precision and complex.  */
00315 
00316       if (readbuf)
00317         {
00318           regcache_cooked_read (regcache, tdep->ppc_fp0_regnum + 1, regval);
00319           convert_typed_floating (regval, regtype, readbuf, valtype);
00320         }
00321       if (writebuf)
00322         {
00323           convert_typed_floating (writebuf, valtype, regval, regtype);
00324           regcache_cooked_write (regcache, tdep->ppc_fp0_regnum + 1, regval);
00325         }
00326 
00327       return RETURN_VALUE_REGISTER_CONVENTION;
00328   }
00329 
00330   /* Values of the types int, long, short, pointer, and char (length
00331      is less than or equal to four bytes), as well as bit values of
00332      lengths less than or equal to 32 bits, must be returned right
00333      justified in GPR3 with signed values sign extended and unsigned
00334      values zero extended, as necessary.  */
00335   if (TYPE_LENGTH (valtype) <= tdep->wordsize)
00336     {
00337       if (readbuf)
00338         {
00339           ULONGEST regval;
00340 
00341           /* For reading we don't have to worry about sign extension.  */
00342           regcache_cooked_read_unsigned (regcache, tdep->ppc_gp0_regnum + 3,
00343                                          &regval);
00344           store_unsigned_integer (readbuf, TYPE_LENGTH (valtype), byte_order,
00345                                   regval);
00346         }
00347       if (writebuf)
00348         {
00349           /* For writing, use unpack_long since that should handle any
00350              required sign extension.  */
00351           regcache_cooked_write_unsigned (regcache, tdep->ppc_gp0_regnum + 3,
00352                                           unpack_long (valtype, writebuf));
00353         }
00354 
00355       return RETURN_VALUE_REGISTER_CONVENTION;
00356     }
00357 
00358   /* Eight-byte non-floating-point scalar values must be returned in
00359      GPR3:GPR4.  */
00360 
00361   if (TYPE_LENGTH (valtype) == 8)
00362     {
00363       gdb_assert (TYPE_CODE (valtype) != TYPE_CODE_FLT);
00364       gdb_assert (tdep->wordsize == 4);
00365 
00366       if (readbuf)
00367         {
00368           gdb_byte regval[8];
00369 
00370           regcache_cooked_read (regcache, tdep->ppc_gp0_regnum + 3, regval);
00371           regcache_cooked_read (regcache, tdep->ppc_gp0_regnum + 4,
00372                                 regval + 4);
00373           memcpy (readbuf, regval, 8);
00374         }
00375       if (writebuf)
00376         {
00377           regcache_cooked_write (regcache, tdep->ppc_gp0_regnum + 3, writebuf);
00378           regcache_cooked_write (regcache, tdep->ppc_gp0_regnum + 4,
00379                                  writebuf + 4);
00380         }
00381 
00382       return RETURN_VALUE_REGISTER_CONVENTION;
00383     }
00384 
00385   return RETURN_VALUE_STRUCT_CONVENTION;
00386 }
00387 
00388 /* PowerPC Lynx178 OSABI sniffer.  */
00389 
00390 static enum gdb_osabi
00391 rs6000_lynx178_osabi_sniffer (bfd *abfd)
00392 {
00393   if (bfd_get_flavour (abfd) != bfd_target_xcoff_flavour)
00394     return GDB_OSABI_UNKNOWN;
00395 
00396   /* The only noticeable difference between Lynx178 XCOFF files and
00397      AIX XCOFF files comes from the fact that there are no shared
00398      libraries on Lynx178.  So if the number of import files is
00399      different from zero, it cannot be a Lynx178 binary.  */
00400   if (xcoff_get_n_import_files (abfd) != 0)
00401     return GDB_OSABI_UNKNOWN;
00402 
00403   return GDB_OSABI_LYNXOS178;
00404 }
00405 
00406 /* Callback for powerpc-lynx178 initialization.  */
00407 
00408 static void
00409 rs6000_lynx178_init_osabi (struct gdbarch_info info, struct gdbarch *gdbarch)
00410 {
00411   set_gdbarch_push_dummy_call (gdbarch, rs6000_lynx178_push_dummy_call);
00412   set_gdbarch_return_value (gdbarch, rs6000_lynx178_return_value);
00413   set_gdbarch_long_double_bit (gdbarch, 8 * TARGET_CHAR_BIT);
00414 }
00415 
00416 /* -Wmissing-prototypes.  */
00417 extern initialize_file_ftype _initialize_rs6000_lynx178_tdep;
00418 
00419 void
00420 _initialize_rs6000_lynx178_tdep (void)
00421 {
00422   gdbarch_register_osabi_sniffer (bfd_arch_rs6000,
00423                                   bfd_target_xcoff_flavour,
00424                                   rs6000_lynx178_osabi_sniffer);
00425   gdbarch_register_osabi (bfd_arch_rs6000, 0, GDB_OSABI_LYNXOS178,
00426                           rs6000_lynx178_init_osabi);
00427 }
00428 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines