GDB (API)
/home/stan/gdb/src/gdb/rs6000-aix-tdep.c
Go to the documentation of this file.
00001 /* Native support code for PPC AIX, for GDB the GNU debugger.
00002 
00003    Copyright (C) 2006-2013 Free Software Foundation, Inc.
00004 
00005    Free Software Foundation, Inc.
00006 
00007    This file is part of GDB.
00008 
00009    This program is free software; you can redistribute it and/or modify
00010    it under the terms of the GNU General Public License as published by
00011    the Free Software Foundation; either version 3 of the License, or
00012    (at your option) any later version.
00013 
00014    This program is distributed in the hope that it will be useful,
00015    but WITHOUT ANY WARRANTY; without even the implied warranty of
00016    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017    GNU General Public License for more details.
00018 
00019    You should have received a copy of the GNU General Public License
00020    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
00021 
00022 #include "defs.h"
00023 #include "gdb_string.h"
00024 #include "gdb_assert.h"
00025 #include "osabi.h"
00026 #include "regcache.h"
00027 #include "regset.h"
00028 #include "gdbtypes.h"
00029 #include "gdbcore.h"
00030 #include "target.h"
00031 #include "value.h"
00032 #include "infcall.h"
00033 #include "objfiles.h"
00034 #include "breakpoint.h"
00035 #include "rs6000-tdep.h"
00036 #include "ppc-tdep.h"
00037 #include "rs6000-aix-tdep.h"
00038 #include "exceptions.h"
00039 #include "xcoffread.h"
00040 #include "solib.h"
00041 #include "solib-aix.h"
00042 #include "xml-utils.h"
00043 
00044 /* If the kernel has to deliver a signal, it pushes a sigcontext
00045    structure on the stack and then calls the signal handler, passing
00046    the address of the sigcontext in an argument register.  Usually
00047    the signal handler doesn't save this register, so we have to
00048    access the sigcontext structure via an offset from the signal handler
00049    frame.
00050    The following constants were determined by experimentation on AIX 3.2.  */
00051 #define SIG_FRAME_PC_OFFSET 96
00052 #define SIG_FRAME_LR_OFFSET 108
00053 #define SIG_FRAME_FP_OFFSET 284
00054 
00055 
00056 /* Core file support.  */
00057 
00058 static struct ppc_reg_offsets rs6000_aix32_reg_offsets =
00059 {
00060   /* General-purpose registers.  */
00061   208, /* r0_offset */
00062   4,  /* gpr_size */
00063   4,  /* xr_size */
00064   24, /* pc_offset */
00065   28, /* ps_offset */
00066   32, /* cr_offset */
00067   36, /* lr_offset */
00068   40, /* ctr_offset */
00069   44, /* xer_offset */
00070   48, /* mq_offset */
00071 
00072   /* Floating-point registers.  */
00073   336, /* f0_offset */
00074   56, /* fpscr_offset */
00075   4,  /* fpscr_size */
00076 
00077   /* AltiVec registers.  */
00078   -1, /* vr0_offset */
00079   -1, /* vscr_offset */
00080   -1 /* vrsave_offset */
00081 };
00082 
00083 static struct ppc_reg_offsets rs6000_aix64_reg_offsets =
00084 {
00085   /* General-purpose registers.  */
00086   0, /* r0_offset */
00087   8,  /* gpr_size */
00088   4,  /* xr_size */
00089   264, /* pc_offset */
00090   256, /* ps_offset */
00091   288, /* cr_offset */
00092   272, /* lr_offset */
00093   280, /* ctr_offset */
00094   292, /* xer_offset */
00095   -1, /* mq_offset */
00096 
00097   /* Floating-point registers.  */
00098   312, /* f0_offset */
00099   296, /* fpscr_offset */
00100   4,  /* fpscr_size */
00101 
00102   /* AltiVec registers.  */
00103   -1, /* vr0_offset */
00104   -1, /* vscr_offset */
00105   -1 /* vrsave_offset */
00106 };
00107 
00108 
00109 /* Supply register REGNUM in the general-purpose register set REGSET
00110    from the buffer specified by GREGS and LEN to register cache
00111    REGCACHE.  If REGNUM is -1, do this for all registers in REGSET.  */
00112 
00113 static void
00114 rs6000_aix_supply_regset (const struct regset *regset,
00115                           struct regcache *regcache, int regnum,
00116                           const void *gregs, size_t len)
00117 {
00118   ppc_supply_gregset (regset, regcache, regnum, gregs, len);
00119   ppc_supply_fpregset (regset, regcache, regnum, gregs, len);
00120 }
00121 
00122 /* Collect register REGNUM in the general-purpose register set
00123    REGSET, from register cache REGCACHE into the buffer specified by
00124    GREGS and LEN.  If REGNUM is -1, do this for all registers in
00125    REGSET.  */
00126 
00127 static void
00128 rs6000_aix_collect_regset (const struct regset *regset,
00129                            const struct regcache *regcache, int regnum,
00130                            void *gregs, size_t len)
00131 {
00132   ppc_collect_gregset (regset, regcache, regnum, gregs, len);
00133   ppc_collect_fpregset (regset, regcache, regnum, gregs, len);
00134 }
00135 
00136 /* AIX register set.  */
00137 
00138 static struct regset rs6000_aix32_regset =
00139 {
00140   &rs6000_aix32_reg_offsets,
00141   rs6000_aix_supply_regset,
00142   rs6000_aix_collect_regset,
00143 };
00144 
00145 static struct regset rs6000_aix64_regset =
00146 {
00147   &rs6000_aix64_reg_offsets,
00148   rs6000_aix_supply_regset,
00149   rs6000_aix_collect_regset,
00150 };
00151 
00152 /* Return the appropriate register set for the core section identified
00153    by SECT_NAME and SECT_SIZE.  */
00154 
00155 static const struct regset *
00156 rs6000_aix_regset_from_core_section (struct gdbarch *gdbarch,
00157                                      const char *sect_name, size_t sect_size)
00158 {
00159   if (gdbarch_tdep (gdbarch)->wordsize == 4)
00160     {
00161       if (strcmp (sect_name, ".reg") == 0 && sect_size >= 592)
00162         return &rs6000_aix32_regset;
00163     }
00164   else
00165     {
00166       if (strcmp (sect_name, ".reg") == 0 && sect_size >= 576)
00167         return &rs6000_aix64_regset;
00168     }
00169 
00170   return NULL;
00171 }
00172 
00173 
00174 /* Pass the arguments in either registers, or in the stack.  In RS/6000,
00175    the first eight words of the argument list (that might be less than
00176    eight parameters if some parameters occupy more than one word) are
00177    passed in r3..r10 registers.  Float and double parameters are
00178    passed in fpr's, in addition to that.  Rest of the parameters if any
00179    are passed in user stack.  There might be cases in which half of the
00180    parameter is copied into registers, the other half is pushed into
00181    stack.
00182 
00183    Stack must be aligned on 64-bit boundaries when synthesizing
00184    function calls.
00185 
00186    If the function is returning a structure, then the return address is passed
00187    in r3, then the first 7 words of the parameters can be passed in registers,
00188    starting from r4.  */
00189 
00190 static CORE_ADDR
00191 rs6000_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
00192                         struct regcache *regcache, CORE_ADDR bp_addr,
00193                         int nargs, struct value **args, CORE_ADDR sp,
00194                         int struct_return, CORE_ADDR struct_addr)
00195 {
00196   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
00197   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
00198   int ii;
00199   int len = 0;
00200   int argno;                    /* current argument number */
00201   int argbytes;                 /* current argument byte */
00202   gdb_byte tmp_buffer[50];
00203   int f_argno = 0;              /* current floating point argno */
00204   int wordsize = gdbarch_tdep (gdbarch)->wordsize;
00205   CORE_ADDR func_addr = find_function_addr (function, NULL);
00206 
00207   struct value *arg = 0;
00208   struct type *type;
00209 
00210   ULONGEST saved_sp;
00211 
00212   /* The calling convention this function implements assumes the
00213      processor has floating-point registers.  We shouldn't be using it
00214      on PPC variants that lack them.  */
00215   gdb_assert (ppc_floating_point_unit_p (gdbarch));
00216 
00217   /* The first eight words of ther arguments are passed in registers.
00218      Copy them appropriately.  */
00219   ii = 0;
00220 
00221   /* If the function is returning a `struct', then the first word
00222      (which will be passed in r3) is used for struct return address.
00223      In that case we should advance one word and start from r4
00224      register to copy parameters.  */
00225   if (struct_return)
00226     {
00227       regcache_raw_write_unsigned (regcache, tdep->ppc_gp0_regnum + 3,
00228                                    struct_addr);
00229       ii++;
00230     }
00231 
00232 /* effectively indirect call... gcc does...
00233 
00234    return_val example( float, int);
00235 
00236    eabi: 
00237    float in fp0, int in r3
00238    offset of stack on overflow 8/16
00239    for varargs, must go by type.
00240    power open:
00241    float in r3&r4, int in r5
00242    offset of stack on overflow different 
00243    both: 
00244    return in r3 or f0.  If no float, must study how gcc emulates floats;
00245    pay attention to arg promotion.
00246    User may have to cast\args to handle promotion correctly 
00247    since gdb won't know if prototype supplied or not.  */
00248 
00249   for (argno = 0, argbytes = 0; argno < nargs && ii < 8; ++ii)
00250     {
00251       int reg_size = register_size (gdbarch, ii + 3);
00252 
00253       arg = args[argno];
00254       type = check_typedef (value_type (arg));
00255       len = TYPE_LENGTH (type);
00256 
00257       if (TYPE_CODE (type) == TYPE_CODE_FLT)
00258         {
00259           /* Floating point arguments are passed in fpr's, as well as gpr's.
00260              There are 13 fpr's reserved for passing parameters.  At this point
00261              there is no way we would run out of them.
00262 
00263              Always store the floating point value using the register's
00264              floating-point format.  */
00265           const int fp_regnum = tdep->ppc_fp0_regnum + 1 + f_argno;
00266           gdb_byte reg_val[MAX_REGISTER_SIZE];
00267           struct type *reg_type = register_type (gdbarch, fp_regnum);
00268 
00269           gdb_assert (len <= 8);
00270 
00271           convert_typed_floating (value_contents (arg), type,
00272                                   reg_val, reg_type);
00273           regcache_cooked_write (regcache, fp_regnum, reg_val);
00274           ++f_argno;
00275         }
00276 
00277       if (len > reg_size)
00278         {
00279 
00280           /* Argument takes more than one register.  */
00281           while (argbytes < len)
00282             {
00283               gdb_byte word[MAX_REGISTER_SIZE];
00284               memset (word, 0, reg_size);
00285               memcpy (word,
00286                       ((char *) value_contents (arg)) + argbytes,
00287                       (len - argbytes) > reg_size
00288                         ? reg_size : len - argbytes);
00289               regcache_cooked_write (regcache,
00290                                     tdep->ppc_gp0_regnum + 3 + ii,
00291                                     word);
00292               ++ii, argbytes += reg_size;
00293 
00294               if (ii >= 8)
00295                 goto ran_out_of_registers_for_arguments;
00296             }
00297           argbytes = 0;
00298           --ii;
00299         }
00300       else
00301         {
00302           /* Argument can fit in one register.  No problem.  */
00303           int adj = gdbarch_byte_order (gdbarch)
00304                     == BFD_ENDIAN_BIG ? reg_size - len : 0;
00305           gdb_byte word[MAX_REGISTER_SIZE];
00306 
00307           memset (word, 0, reg_size);
00308           memcpy (word, value_contents (arg), len);
00309           regcache_cooked_write (regcache, tdep->ppc_gp0_regnum + 3 +ii, word);
00310         }
00311       ++argno;
00312     }
00313 
00314 ran_out_of_registers_for_arguments:
00315 
00316   regcache_cooked_read_unsigned (regcache,
00317                                  gdbarch_sp_regnum (gdbarch),
00318                                  &saved_sp);
00319 
00320   /* Location for 8 parameters are always reserved.  */
00321   sp -= wordsize * 8;
00322 
00323   /* Another six words for back chain, TOC register, link register, etc.  */
00324   sp -= wordsize * 6;
00325 
00326   /* Stack pointer must be quadword aligned.  */
00327   sp &= -16;
00328 
00329   /* If there are more arguments, allocate space for them in 
00330      the stack, then push them starting from the ninth one.  */
00331 
00332   if ((argno < nargs) || argbytes)
00333     {
00334       int space = 0, jj;
00335 
00336       if (argbytes)
00337         {
00338           space += ((len - argbytes + 3) & -4);
00339           jj = argno + 1;
00340         }
00341       else
00342         jj = argno;
00343 
00344       for (; jj < nargs; ++jj)
00345         {
00346           struct value *val = args[jj];
00347           space += ((TYPE_LENGTH (value_type (val))) + 3) & -4;
00348         }
00349 
00350       /* Add location required for the rest of the parameters.  */
00351       space = (space + 15) & -16;
00352       sp -= space;
00353 
00354       /* This is another instance we need to be concerned about
00355          securing our stack space.  If we write anything underneath %sp
00356          (r1), we might conflict with the kernel who thinks he is free
00357          to use this area.  So, update %sp first before doing anything
00358          else.  */
00359 
00360       regcache_raw_write_signed (regcache,
00361                                  gdbarch_sp_regnum (gdbarch), sp);
00362 
00363       /* If the last argument copied into the registers didn't fit there 
00364          completely, push the rest of it into stack.  */
00365 
00366       if (argbytes)
00367         {
00368           write_memory (sp + 24 + (ii * 4),
00369                         value_contents (arg) + argbytes,
00370                         len - argbytes);
00371           ++argno;
00372           ii += ((len - argbytes + 3) & -4) / 4;
00373         }
00374 
00375       /* Push the rest of the arguments into stack.  */
00376       for (; argno < nargs; ++argno)
00377         {
00378 
00379           arg = args[argno];
00380           type = check_typedef (value_type (arg));
00381           len = TYPE_LENGTH (type);
00382 
00383 
00384           /* Float types should be passed in fpr's, as well as in the
00385              stack.  */
00386           if (TYPE_CODE (type) == TYPE_CODE_FLT && f_argno < 13)
00387             {
00388 
00389               gdb_assert (len <= 8);
00390 
00391               regcache_cooked_write (regcache,
00392                                      tdep->ppc_fp0_regnum + 1 + f_argno,
00393                                      value_contents (arg));
00394               ++f_argno;
00395             }
00396 
00397           write_memory (sp + 24 + (ii * 4), value_contents (arg), len);
00398           ii += ((len + 3) & -4) / 4;
00399         }
00400     }
00401 
00402   /* Set the stack pointer.  According to the ABI, the SP is meant to
00403      be set _before_ the corresponding stack space is used.  On AIX,
00404      this even applies when the target has been completely stopped!
00405      Not doing this can lead to conflicts with the kernel which thinks
00406      that it still has control over this not-yet-allocated stack
00407      region.  */
00408   regcache_raw_write_signed (regcache, gdbarch_sp_regnum (gdbarch), sp);
00409 
00410   /* Set back chain properly.  */
00411   store_unsigned_integer (tmp_buffer, wordsize, byte_order, saved_sp);
00412   write_memory (sp, tmp_buffer, wordsize);
00413 
00414   /* Point the inferior function call's return address at the dummy's
00415      breakpoint.  */
00416   regcache_raw_write_signed (regcache, tdep->ppc_lr_regnum, bp_addr);
00417 
00418   /* Set the TOC register value.  */
00419   regcache_raw_write_signed (regcache, tdep->ppc_toc_regnum,
00420                              solib_aix_get_toc_value (func_addr));
00421 
00422   target_store_registers (regcache, -1);
00423   return sp;
00424 }
00425 
00426 static enum return_value_convention
00427 rs6000_return_value (struct gdbarch *gdbarch, struct value *function,
00428                      struct type *valtype, struct regcache *regcache,
00429                      gdb_byte *readbuf, const gdb_byte *writebuf)
00430 {
00431   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
00432   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
00433 
00434   /* The calling convention this function implements assumes the
00435      processor has floating-point registers.  We shouldn't be using it
00436      on PowerPC variants that lack them.  */
00437   gdb_assert (ppc_floating_point_unit_p (gdbarch));
00438 
00439   /* AltiVec extension: Functions that declare a vector data type as a
00440      return value place that return value in VR2.  */
00441   if (TYPE_CODE (valtype) == TYPE_CODE_ARRAY && TYPE_VECTOR (valtype)
00442       && TYPE_LENGTH (valtype) == 16)
00443     {
00444       if (readbuf)
00445         regcache_cooked_read (regcache, tdep->ppc_vr0_regnum + 2, readbuf);
00446       if (writebuf)
00447         regcache_cooked_write (regcache, tdep->ppc_vr0_regnum + 2, writebuf);
00448 
00449       return RETURN_VALUE_REGISTER_CONVENTION;
00450     }
00451 
00452   /* If the called subprogram returns an aggregate, there exists an
00453      implicit first argument, whose value is the address of a caller-
00454      allocated buffer into which the callee is assumed to store its
00455      return value.  All explicit parameters are appropriately
00456      relabeled.  */
00457   if (TYPE_CODE (valtype) == TYPE_CODE_STRUCT
00458       || TYPE_CODE (valtype) == TYPE_CODE_UNION
00459       || TYPE_CODE (valtype) == TYPE_CODE_ARRAY)
00460     return RETURN_VALUE_STRUCT_CONVENTION;
00461 
00462   /* Scalar floating-point values are returned in FPR1 for float or
00463      double, and in FPR1:FPR2 for quadword precision.  Fortran
00464      complex*8 and complex*16 are returned in FPR1:FPR2, and
00465      complex*32 is returned in FPR1:FPR4.  */
00466   if (TYPE_CODE (valtype) == TYPE_CODE_FLT
00467       && (TYPE_LENGTH (valtype) == 4 || TYPE_LENGTH (valtype) == 8))
00468     {
00469       struct type *regtype = register_type (gdbarch, tdep->ppc_fp0_regnum);
00470       gdb_byte regval[8];
00471 
00472       /* FIXME: kettenis/2007-01-01: Add support for quadword
00473          precision and complex.  */
00474 
00475       if (readbuf)
00476         {
00477           regcache_cooked_read (regcache, tdep->ppc_fp0_regnum + 1, regval);
00478           convert_typed_floating (regval, regtype, readbuf, valtype);
00479         }
00480       if (writebuf)
00481         {
00482           convert_typed_floating (writebuf, valtype, regval, regtype);
00483           regcache_cooked_write (regcache, tdep->ppc_fp0_regnum + 1, regval);
00484         }
00485 
00486       return RETURN_VALUE_REGISTER_CONVENTION;
00487   }
00488 
00489   /* Values of the types int, long, short, pointer, and char (length
00490      is less than or equal to four bytes), as well as bit values of
00491      lengths less than or equal to 32 bits, must be returned right
00492      justified in GPR3 with signed values sign extended and unsigned
00493      values zero extended, as necessary.  */
00494   if (TYPE_LENGTH (valtype) <= tdep->wordsize)
00495     {
00496       if (readbuf)
00497         {
00498           ULONGEST regval;
00499 
00500           /* For reading we don't have to worry about sign extension.  */
00501           regcache_cooked_read_unsigned (regcache, tdep->ppc_gp0_regnum + 3,
00502                                          &regval);
00503           store_unsigned_integer (readbuf, TYPE_LENGTH (valtype), byte_order,
00504                                   regval);
00505         }
00506       if (writebuf)
00507         {
00508           /* For writing, use unpack_long since that should handle any
00509              required sign extension.  */
00510           regcache_cooked_write_unsigned (regcache, tdep->ppc_gp0_regnum + 3,
00511                                           unpack_long (valtype, writebuf));
00512         }
00513 
00514       return RETURN_VALUE_REGISTER_CONVENTION;
00515     }
00516 
00517   /* Eight-byte non-floating-point scalar values must be returned in
00518      GPR3:GPR4.  */
00519 
00520   if (TYPE_LENGTH (valtype) == 8)
00521     {
00522       gdb_assert (TYPE_CODE (valtype) != TYPE_CODE_FLT);
00523       gdb_assert (tdep->wordsize == 4);
00524 
00525       if (readbuf)
00526         {
00527           gdb_byte regval[8];
00528 
00529           regcache_cooked_read (regcache, tdep->ppc_gp0_regnum + 3, regval);
00530           regcache_cooked_read (regcache, tdep->ppc_gp0_regnum + 4,
00531                                 regval + 4);
00532           memcpy (readbuf, regval, 8);
00533         }
00534       if (writebuf)
00535         {
00536           regcache_cooked_write (regcache, tdep->ppc_gp0_regnum + 3, writebuf);
00537           regcache_cooked_write (regcache, tdep->ppc_gp0_regnum + 4,
00538                                  writebuf + 4);
00539         }
00540 
00541       return RETURN_VALUE_REGISTER_CONVENTION;
00542     }
00543 
00544   return RETURN_VALUE_STRUCT_CONVENTION;
00545 }
00546 
00547 /* Support for CONVERT_FROM_FUNC_PTR_ADDR (ARCH, ADDR, TARG).
00548 
00549    Usually a function pointer's representation is simply the address
00550    of the function.  On the RS/6000 however, a function pointer is
00551    represented by a pointer to an OPD entry.  This OPD entry contains
00552    three words, the first word is the address of the function, the
00553    second word is the TOC pointer (r2), and the third word is the
00554    static chain value.  Throughout GDB it is currently assumed that a
00555    function pointer contains the address of the function, which is not
00556    easy to fix.  In addition, the conversion of a function address to
00557    a function pointer would require allocation of an OPD entry in the
00558    inferior's memory space, with all its drawbacks.  To be able to
00559    call C++ virtual methods in the inferior (which are called via
00560    function pointers), find_function_addr uses this function to get the
00561    function address from a function pointer.  */
00562 
00563 /* Return real function address if ADDR (a function pointer) is in the data
00564    space and is therefore a special function pointer.  */
00565 
00566 static CORE_ADDR
00567 rs6000_convert_from_func_ptr_addr (struct gdbarch *gdbarch,
00568                                    CORE_ADDR addr,
00569                                    struct target_ops *targ)
00570 {
00571   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
00572   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
00573   struct obj_section *s;
00574 
00575   s = find_pc_section (addr);
00576 
00577   /* Normally, functions live inside a section that is executable.
00578      So, if ADDR points to a non-executable section, then treat it
00579      as a function descriptor and return the target address iff
00580      the target address itself points to a section that is executable.  */
00581   if (s && (s->the_bfd_section->flags & SEC_CODE) == 0)
00582     {
00583       CORE_ADDR pc = 0;
00584       struct obj_section *pc_section;
00585       volatile struct gdb_exception e;
00586 
00587       TRY_CATCH (e, RETURN_MASK_ERROR)
00588         {
00589           pc = read_memory_unsigned_integer (addr, tdep->wordsize, byte_order);
00590         }
00591       if (e.reason < 0)
00592         {
00593           /* An error occured during reading.  Probably a memory error
00594              due to the section not being loaded yet.  This address
00595              cannot be a function descriptor.  */
00596           return addr;
00597         }
00598       pc_section = find_pc_section (pc);
00599 
00600       if (pc_section && (pc_section->the_bfd_section->flags & SEC_CODE))
00601         return pc;
00602     }
00603 
00604   return addr;
00605 }
00606 
00607 
00608 /* Calculate the destination of a branch/jump.  Return -1 if not a branch.  */
00609 
00610 static CORE_ADDR
00611 branch_dest (struct frame_info *frame, int opcode, int instr,
00612              CORE_ADDR pc, CORE_ADDR safety)
00613 {
00614   struct gdbarch *gdbarch = get_frame_arch (frame);
00615   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
00616   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
00617   CORE_ADDR dest;
00618   int immediate;
00619   int absolute;
00620   int ext_op;
00621 
00622   absolute = (int) ((instr >> 1) & 1);
00623 
00624   switch (opcode)
00625     {
00626     case 18:
00627       immediate = ((instr & ~3) << 6) >> 6;     /* br unconditional */
00628       if (absolute)
00629         dest = immediate;
00630       else
00631         dest = pc + immediate;
00632       break;
00633 
00634     case 16:
00635       immediate = ((instr & ~3) << 16) >> 16;   /* br conditional */
00636       if (absolute)
00637         dest = immediate;
00638       else
00639         dest = pc + immediate;
00640       break;
00641 
00642     case 19:
00643       ext_op = (instr >> 1) & 0x3ff;
00644 
00645       if (ext_op == 16)         /* br conditional register */
00646         {
00647           dest = get_frame_register_unsigned (frame, tdep->ppc_lr_regnum) & ~3;
00648 
00649           /* If we are about to return from a signal handler, dest is
00650              something like 0x3c90.  The current frame is a signal handler
00651              caller frame, upon completion of the sigreturn system call
00652              execution will return to the saved PC in the frame.  */
00653           if (dest < AIX_TEXT_SEGMENT_BASE)
00654             dest = read_memory_unsigned_integer
00655                      (get_frame_base (frame) + SIG_FRAME_PC_OFFSET,
00656                       tdep->wordsize, byte_order);
00657         }
00658 
00659       else if (ext_op == 528)   /* br cond to count reg */
00660         {
00661           dest = get_frame_register_unsigned (frame,
00662                                               tdep->ppc_ctr_regnum) & ~3;
00663 
00664           /* If we are about to execute a system call, dest is something
00665              like 0x22fc or 0x3b00.  Upon completion the system call
00666              will return to the address in the link register.  */
00667           if (dest < AIX_TEXT_SEGMENT_BASE)
00668             dest = get_frame_register_unsigned (frame,
00669                                                 tdep->ppc_lr_regnum) & ~3;
00670         }
00671       else
00672         return -1;
00673       break;
00674 
00675     default:
00676       return -1;
00677     }
00678   return (dest < AIX_TEXT_SEGMENT_BASE) ? safety : dest;
00679 }
00680 
00681 /* AIX does not support PT_STEP.  Simulate it.  */
00682 
00683 static int
00684 rs6000_software_single_step (struct frame_info *frame)
00685 {
00686   struct gdbarch *gdbarch = get_frame_arch (frame);
00687   struct address_space *aspace = get_frame_address_space (frame);
00688   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
00689   int ii, insn;
00690   CORE_ADDR loc;
00691   CORE_ADDR breaks[2];
00692   int opcode;
00693 
00694   loc = get_frame_pc (frame);
00695 
00696   insn = read_memory_integer (loc, 4, byte_order);
00697 
00698   if (ppc_deal_with_atomic_sequence (frame))
00699     return 1;
00700   
00701   breaks[0] = loc + PPC_INSN_SIZE;
00702   opcode = insn >> 26;
00703   breaks[1] = branch_dest (frame, opcode, insn, loc, breaks[0]);
00704 
00705   /* Don't put two breakpoints on the same address.  */
00706   if (breaks[1] == breaks[0])
00707     breaks[1] = -1;
00708 
00709   for (ii = 0; ii < 2; ++ii)
00710     {
00711       /* ignore invalid breakpoint.  */
00712       if (breaks[ii] == -1)
00713         continue;
00714       insert_single_step_breakpoint (gdbarch, aspace, breaks[ii]);
00715     }
00716 
00717   errno = 0;                    /* FIXME, don't ignore errors!  */
00718   /* What errors?  {read,write}_memory call error().  */
00719   return 1;
00720 }
00721 
00722 /* Implement the "auto_wide_charset" gdbarch method for this platform.  */
00723 
00724 static const char *
00725 rs6000_aix_auto_wide_charset (void)
00726 {
00727   return "UTF-16";
00728 }
00729 
00730 /* Implement an osabi sniffer for RS6000/AIX.
00731 
00732    This function assumes that ABFD's flavour is XCOFF.  In other words,
00733    it should be registered as a sniffer for bfd_target_xcoff_flavour
00734    objfiles only.  A failed assertion will be raised if this condition
00735    is not met.  */
00736 
00737 static enum gdb_osabi
00738 rs6000_aix_osabi_sniffer (bfd *abfd)
00739 {
00740   gdb_assert (bfd_get_flavour (abfd) == bfd_target_xcoff_flavour);
00741 
00742   /* The only noticeable difference between Lynx178 XCOFF files and
00743      AIX XCOFF files comes from the fact that there are no shared
00744      libraries on Lynx178.  On AIX, we are betting that an executable
00745      linked with no shared library will never exist.  */
00746   if (xcoff_get_n_import_files (abfd) <= 0)
00747     return GDB_OSABI_UNKNOWN;
00748 
00749   return GDB_OSABI_AIX;
00750 }
00751 
00752 /* A structure encoding the offset and size of a field within
00753    a struct.  */
00754 
00755 struct field_info
00756 {
00757   int offset;
00758   int size;
00759 };
00760 
00761 /* A structure describing the layout of all the fields of interest
00762    in AIX's struct ld_info.  Each field in this struct corresponds
00763    to the field of the same name in struct ld_info.  */
00764 
00765 struct ld_info_desc
00766 {
00767   struct field_info ldinfo_next;
00768   struct field_info ldinfo_fd;
00769   struct field_info ldinfo_textorg;
00770   struct field_info ldinfo_textsize;
00771   struct field_info ldinfo_dataorg;
00772   struct field_info ldinfo_datasize;
00773   struct field_info ldinfo_filename;
00774 };
00775 
00776 /* The following data has been generated by compiling and running
00777    the following program on AIX 5.3.  */
00778 
00779 #if 0
00780 #include <stddef.h>
00781 #include <stdio.h>
00782 #define __LDINFO_PTRACE32__
00783 #define __LDINFO_PTRACE64__
00784 #include <sys/ldr.h>
00785 
00786 #define pinfo(type,member)                  \
00787   {                                         \
00788     struct type ldi = {0};                  \
00789                                             \
00790     printf ("  {%d, %d},\t/* %s */\n",      \
00791             offsetof (struct type, member), \
00792             sizeof (ldi.member),            \
00793             #member);                       \
00794   }                                         \
00795   while (0)
00796 
00797 int
00798 main (void)
00799 {
00800   printf ("static const struct ld_info_desc ld_info32_desc =\n{\n");
00801   pinfo (__ld_info32, ldinfo_next);
00802   pinfo (__ld_info32, ldinfo_fd);
00803   pinfo (__ld_info32, ldinfo_textorg);
00804   pinfo (__ld_info32, ldinfo_textsize);
00805   pinfo (__ld_info32, ldinfo_dataorg);
00806   pinfo (__ld_info32, ldinfo_datasize);
00807   pinfo (__ld_info32, ldinfo_filename);
00808   printf ("};\n");
00809 
00810   printf ("\n");
00811 
00812   printf ("static const struct ld_info_desc ld_info64_desc =\n{\n");
00813   pinfo (__ld_info64, ldinfo_next);
00814   pinfo (__ld_info64, ldinfo_fd);
00815   pinfo (__ld_info64, ldinfo_textorg);
00816   pinfo (__ld_info64, ldinfo_textsize);
00817   pinfo (__ld_info64, ldinfo_dataorg);
00818   pinfo (__ld_info64, ldinfo_datasize);
00819   pinfo (__ld_info64, ldinfo_filename);
00820   printf ("};\n");
00821 
00822   return 0;
00823 }
00824 #endif /* 0 */
00825 
00826 /* Layout of the 32bit version of struct ld_info.  */
00827 
00828 static const struct ld_info_desc ld_info32_desc =
00829 {
00830   {0, 4},       /* ldinfo_next */
00831   {4, 4},       /* ldinfo_fd */
00832   {8, 4},       /* ldinfo_textorg */
00833   {12, 4},      /* ldinfo_textsize */
00834   {16, 4},      /* ldinfo_dataorg */
00835   {20, 4},      /* ldinfo_datasize */
00836   {24, 2},      /* ldinfo_filename */
00837 };
00838 
00839 /* Layout of the 64bit version of struct ld_info.  */
00840 
00841 static const struct ld_info_desc ld_info64_desc =
00842 {
00843   {0, 4},       /* ldinfo_next */
00844   {8, 4},       /* ldinfo_fd */
00845   {16, 8},      /* ldinfo_textorg */
00846   {24, 8},      /* ldinfo_textsize */
00847   {32, 8},      /* ldinfo_dataorg */
00848   {40, 8},      /* ldinfo_datasize */
00849   {48, 2},      /* ldinfo_filename */
00850 };
00851 
00852 /* A structured representation of one entry read from the ld_info
00853    binary data provided by the AIX loader.  */
00854 
00855 struct ld_info
00856 {
00857   ULONGEST next;
00858   int fd;
00859   CORE_ADDR textorg;
00860   ULONGEST textsize;
00861   CORE_ADDR dataorg;
00862   ULONGEST datasize;
00863   char *filename;
00864   char *member_name;
00865 };
00866 
00867 /* Return a struct ld_info object corresponding to the entry at
00868    LDI_BUF.
00869 
00870    Note that the filename and member_name strings still point
00871    to the data in LDI_BUF.  So LDI_BUF must not be deallocated
00872    while the struct ld_info object returned is in use.  */
00873 
00874 static struct ld_info
00875 rs6000_aix_extract_ld_info (struct gdbarch *gdbarch,
00876                             const gdb_byte *ldi_buf)
00877 {
00878   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
00879   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
00880   struct type *ptr_type = builtin_type (gdbarch)->builtin_data_ptr;
00881   const struct ld_info_desc desc
00882     = tdep->wordsize == 8 ? ld_info64_desc : ld_info32_desc;
00883   struct ld_info info;
00884 
00885   info.next = extract_unsigned_integer (ldi_buf + desc.ldinfo_next.offset,
00886                                         desc.ldinfo_next.size,
00887                                         byte_order);
00888   info.fd = extract_signed_integer (ldi_buf + desc.ldinfo_fd.offset,
00889                                     desc.ldinfo_fd.size,
00890                                     byte_order);
00891   info.textorg = extract_typed_address (ldi_buf + desc.ldinfo_textorg.offset,
00892                                         ptr_type);
00893   info.textsize
00894     = extract_unsigned_integer (ldi_buf + desc.ldinfo_textsize.offset,
00895                                 desc.ldinfo_textsize.size,
00896                                 byte_order);
00897   info.dataorg = extract_typed_address (ldi_buf + desc.ldinfo_dataorg.offset,
00898                                         ptr_type);
00899   info.datasize
00900     = extract_unsigned_integer (ldi_buf + desc.ldinfo_datasize.offset,
00901                                 desc.ldinfo_datasize.size,
00902                                 byte_order);
00903   info.filename = (char *) ldi_buf + desc.ldinfo_filename.offset;
00904   info.member_name = info.filename + strlen (info.filename) + 1;
00905 
00906   return info;
00907 }
00908 
00909 /* Append to OBJSTACK an XML string description of the shared library
00910    corresponding to LDI, following the TARGET_OBJECT_LIBRARIES_AIX
00911    format.  */
00912 
00913 static void
00914 rs6000_aix_shared_library_to_xml (struct ld_info *ldi,
00915                                   struct obstack *obstack)
00916 {
00917   char *p;
00918 
00919   obstack_grow_str (obstack, "<library name=\"");
00920   p = xml_escape_text (ldi->filename);
00921   obstack_grow_str (obstack, p);
00922   xfree (p);
00923   obstack_grow_str (obstack, "\"");
00924 
00925   if (ldi->member_name[0] != '\0')
00926     {
00927       obstack_grow_str (obstack, " member=\"");
00928       p = xml_escape_text (ldi->member_name);
00929       obstack_grow_str (obstack, p);
00930       xfree (p);
00931       obstack_grow_str (obstack, "\"");
00932     }
00933 
00934   obstack_grow_str (obstack, " text_addr=\"");
00935   obstack_grow_str (obstack, core_addr_to_string (ldi->textorg));
00936   obstack_grow_str (obstack, "\"");
00937 
00938   obstack_grow_str (obstack, " text_size=\"");
00939   obstack_grow_str (obstack, pulongest (ldi->textsize));
00940   obstack_grow_str (obstack, "\"");
00941 
00942   obstack_grow_str (obstack, " data_addr=\"");
00943   obstack_grow_str (obstack, core_addr_to_string (ldi->dataorg));
00944   obstack_grow_str (obstack, "\"");
00945 
00946   obstack_grow_str (obstack, " data_size=\"");
00947   obstack_grow_str (obstack, pulongest (ldi->datasize));
00948   obstack_grow_str (obstack, "\"");
00949 
00950   obstack_grow_str (obstack, "></library>");
00951 }
00952 
00953 /* Convert the ld_info binary data provided by the AIX loader into
00954    an XML representation following the TARGET_OBJECT_LIBRARIES_AIX
00955    format.
00956 
00957    LDI_BUF is a buffer containing the ld_info data.
00958    READBUF, OFFSET and LEN follow the same semantics as target_ops'
00959    to_xfer_partial target_ops method.
00960 
00961    If CLOSE_LDINFO_FD is nonzero, then this routine also closes
00962    the ldinfo_fd file descriptor.  This is useful when the ldinfo
00963    data is obtained via ptrace, as ptrace opens a file descriptor
00964    for each and every entry; but we cannot use this descriptor
00965    as the consumer of the XML library list might live in a different
00966    process.  */
00967 
00968 LONGEST
00969 rs6000_aix_ld_info_to_xml (struct gdbarch *gdbarch, const gdb_byte *ldi_buf,
00970                            gdb_byte *readbuf, ULONGEST offset, LONGEST len,
00971                            int close_ldinfo_fd)
00972 {
00973   struct obstack obstack;
00974   const char *buf;
00975   LONGEST len_avail;
00976 
00977   obstack_init (&obstack);
00978   obstack_grow_str (&obstack, "<library-list-aix version=\"1.0\">\n");
00979 
00980   while (1)
00981     {
00982       struct ld_info ldi = rs6000_aix_extract_ld_info (gdbarch, ldi_buf);
00983 
00984       rs6000_aix_shared_library_to_xml (&ldi, &obstack);
00985       if (close_ldinfo_fd)
00986         close (ldi.fd);
00987 
00988       if (!ldi.next)
00989         break;
00990       ldi_buf = ldi_buf + ldi.next;
00991     }
00992 
00993   obstack_grow_str0 (&obstack, "</library-list-aix>\n");
00994 
00995   buf = obstack_finish (&obstack);
00996   len_avail = strlen (buf);
00997   if (offset >= len_avail)
00998     len= 0;
00999   else
01000     {
01001       if (len > len_avail - offset)
01002         len = len_avail - offset;
01003       memcpy (readbuf, buf + offset, len);
01004     }
01005 
01006   obstack_free (&obstack, NULL);
01007   return len;
01008 }
01009 
01010 /* Implement the core_xfer_shared_libraries_aix gdbarch method.  */
01011 
01012 static LONGEST
01013 rs6000_aix_core_xfer_shared_libraries_aix (struct gdbarch *gdbarch,
01014                                            gdb_byte *readbuf,
01015                                            ULONGEST offset,
01016                                            LONGEST len)
01017 {
01018   struct bfd_section *ldinfo_sec;
01019   int ldinfo_size;
01020   gdb_byte *ldinfo_buf;
01021   struct cleanup *cleanup;
01022   LONGEST result;
01023 
01024   ldinfo_sec = bfd_get_section_by_name (core_bfd, ".ldinfo");
01025   if (ldinfo_sec == NULL)
01026     error (_("cannot find .ldinfo section from core file: %s"),
01027            bfd_errmsg (bfd_get_error ()));
01028   ldinfo_size = bfd_get_section_size (ldinfo_sec);
01029 
01030   ldinfo_buf = xmalloc (ldinfo_size);
01031   cleanup = make_cleanup (xfree, ldinfo_buf);
01032 
01033   if (! bfd_get_section_contents (core_bfd, ldinfo_sec,
01034                                   ldinfo_buf, 0, ldinfo_size))
01035     error (_("unable to read .ldinfo section from core file: %s"),
01036           bfd_errmsg (bfd_get_error ()));
01037 
01038   result = rs6000_aix_ld_info_to_xml (gdbarch, ldinfo_buf, readbuf,
01039                                       offset, len, 0);
01040 
01041   do_cleanups (cleanup);
01042   return result;
01043 }
01044 
01045 static void
01046 rs6000_aix_init_osabi (struct gdbarch_info info, struct gdbarch *gdbarch)
01047 {
01048   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
01049 
01050   /* RS6000/AIX does not support PT_STEP.  Has to be simulated.  */
01051   set_gdbarch_software_single_step (gdbarch, rs6000_software_single_step);
01052 
01053   /* Displaced stepping is currently not supported in combination with
01054      software single-stepping.  */
01055   set_gdbarch_displaced_step_copy_insn (gdbarch, NULL);
01056   set_gdbarch_displaced_step_fixup (gdbarch, NULL);
01057   set_gdbarch_displaced_step_free_closure (gdbarch, NULL);
01058   set_gdbarch_displaced_step_location (gdbarch, NULL);
01059 
01060   set_gdbarch_push_dummy_call (gdbarch, rs6000_push_dummy_call);
01061   set_gdbarch_return_value (gdbarch, rs6000_return_value);
01062   set_gdbarch_long_double_bit (gdbarch, 8 * TARGET_CHAR_BIT);
01063 
01064   /* Handle RS/6000 function pointers (which are really function
01065      descriptors).  */
01066   set_gdbarch_convert_from_func_ptr_addr
01067     (gdbarch, rs6000_convert_from_func_ptr_addr);
01068 
01069   /* Core file support.  */
01070   set_gdbarch_regset_from_core_section
01071     (gdbarch, rs6000_aix_regset_from_core_section);
01072   set_gdbarch_core_xfer_shared_libraries_aix
01073     (gdbarch, rs6000_aix_core_xfer_shared_libraries_aix);
01074 
01075   if (tdep->wordsize == 8)
01076     tdep->lr_frame_offset = 16;
01077   else
01078     tdep->lr_frame_offset = 8;
01079 
01080   if (tdep->wordsize == 4)
01081     /* PowerOpen / AIX 32 bit.  The saved area or red zone consists of
01082        19 4 byte GPRS + 18 8 byte FPRs giving a total of 220 bytes.
01083        Problem is, 220 isn't frame (16 byte) aligned.  Round it up to
01084        224.  */
01085     set_gdbarch_frame_red_zone_size (gdbarch, 224);
01086   else
01087     set_gdbarch_frame_red_zone_size (gdbarch, 0);
01088 
01089   set_gdbarch_auto_wide_charset (gdbarch, rs6000_aix_auto_wide_charset);
01090 
01091   set_solib_ops (gdbarch, &solib_aix_so_ops);
01092 }
01093 
01094 /* Provide a prototype to silence -Wmissing-prototypes.  */
01095 extern initialize_file_ftype _initialize_rs6000_aix_tdep;
01096 
01097 void
01098 _initialize_rs6000_aix_tdep (void)
01099 {
01100   gdbarch_register_osabi_sniffer (bfd_arch_rs6000,
01101                                   bfd_target_xcoff_flavour,
01102                                   rs6000_aix_osabi_sniffer);
01103   gdbarch_register_osabi_sniffer (bfd_arch_powerpc,
01104                                   bfd_target_xcoff_flavour,
01105                                   rs6000_aix_osabi_sniffer);
01106 
01107   gdbarch_register_osabi (bfd_arch_rs6000, 0, GDB_OSABI_AIX,
01108                           rs6000_aix_init_osabi);
01109   gdbarch_register_osabi (bfd_arch_powerpc, 0, GDB_OSABI_AIX,
01110                           rs6000_aix_init_osabi);
01111 }
01112 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines