gdb/ppc-linux-tdep.c - gdb

Global variables defined

Data types defined

Functions defined

Macros defined

Source code

  1. /* Target-dependent code for GDB, the GNU debugger.

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

  3.    This file is part of GDB.

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

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

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

  14. #include "defs.h"
  15. #include "frame.h"
  16. #include "inferior.h"
  17. #include "symtab.h"
  18. #include "target.h"
  19. #include "gdbcore.h"
  20. #include "gdbcmd.h"
  21. #include "symfile.h"
  22. #include "objfiles.h"
  23. #include "regcache.h"
  24. #include "value.h"
  25. #include "osabi.h"
  26. #include "regset.h"
  27. #include "solib-svr4.h"
  28. #include "solib-spu.h"
  29. #include "solib.h"
  30. #include "solist.h"
  31. #include "ppc-tdep.h"
  32. #include "ppc64-tdep.h"
  33. #include "ppc-linux-tdep.h"
  34. #include "glibc-tdep.h"
  35. #include "trad-frame.h"
  36. #include "frame-unwind.h"
  37. #include "tramp-frame.h"
  38. #include "observer.h"
  39. #include "auxv.h"
  40. #include "elf/common.h"
  41. #include "elf/ppc64.h"
  42. #include "arch-utils.h"
  43. #include "spu-tdep.h"
  44. #include "xml-syscall.h"
  45. #include "linux-tdep.h"

  46. #include "stap-probe.h"
  47. #include "ax.h"
  48. #include "ax-gdb.h"
  49. #include "cli/cli-utils.h"
  50. #include "parser-defs.h"
  51. #include "user-regs.h"
  52. #include <ctype.h>
  53. #include "elf-bfd.h"            /* for elfcore_write_* */

  54. #include "features/rs6000/powerpc-32l.c"
  55. #include "features/rs6000/powerpc-altivec32l.c"
  56. #include "features/rs6000/powerpc-cell32l.c"
  57. #include "features/rs6000/powerpc-vsx32l.c"
  58. #include "features/rs6000/powerpc-isa205-32l.c"
  59. #include "features/rs6000/powerpc-isa205-altivec32l.c"
  60. #include "features/rs6000/powerpc-isa205-vsx32l.c"
  61. #include "features/rs6000/powerpc-64l.c"
  62. #include "features/rs6000/powerpc-altivec64l.c"
  63. #include "features/rs6000/powerpc-cell64l.c"
  64. #include "features/rs6000/powerpc-vsx64l.c"
  65. #include "features/rs6000/powerpc-isa205-64l.c"
  66. #include "features/rs6000/powerpc-isa205-altivec64l.c"
  67. #include "features/rs6000/powerpc-isa205-vsx64l.c"
  68. #include "features/rs6000/powerpc-e500l.c"

  69. /* Shared library operations for PowerPC-Linux.  */
  70. static struct target_so_ops powerpc_so_ops;

  71. /* The syscall's XML filename for PPC and PPC64.  */
  72. #define XML_SYSCALL_FILENAME_PPC "syscalls/ppc-linux.xml"
  73. #define XML_SYSCALL_FILENAME_PPC64 "syscalls/ppc64-linux.xml"

  74. /* ppc_linux_memory_remove_breakpoints attempts to remove a breakpoint
  75.    in much the same fashion as memory_remove_breakpoint in mem-break.c,
  76.    but is careful not to write back the previous contents if the code
  77.    in question has changed in between inserting the breakpoint and
  78.    removing it.

  79.    Here is the problem that we're trying to solve...

  80.    Once upon a time, before introducing this function to remove
  81.    breakpoints from the inferior, setting a breakpoint on a shared
  82.    library function prior to running the program would not work
  83.    properly.  In order to understand the problem, it is first
  84.    necessary to understand a little bit about dynamic linking on
  85.    this platform.

  86.    A call to a shared library function is accomplished via a bl
  87.    (branch-and-link) instruction whose branch target is an entry
  88.    in the procedure linkage table (PLT).  The PLT in the object
  89.    file is uninitialized.  To gdb, prior to running the program, the
  90.    entries in the PLT are all zeros.

  91.    Once the program starts running, the shared libraries are loaded
  92.    and the procedure linkage table is initialized, but the entries in
  93.    the table are not (necessarily) resolved.  Once a function is
  94.    actually called, the code in the PLT is hit and the function is
  95.    resolved.  In order to better illustrate this, an example is in
  96.    order; the following example is from the gdb testsuite.

  97.         We start the program shmain.

  98.             [kev@arroyo testsuite]$ ../gdb gdb.base/shmain
  99.             [...]

  100.         We place two breakpoints, one on shr1 and the other on main.

  101.             (gdb) b shr1
  102.             Breakpoint 1 at 0x100409d4
  103.             (gdb) b main
  104.             Breakpoint 2 at 0x100006a0: file gdb.base/shmain.c, line 44.

  105.         Examine the instruction (and the immediatly following instruction)
  106.         upon which the breakpoint was placed.  Note that the PLT entry
  107.         for shr1 contains zeros.

  108.             (gdb) x/2i 0x100409d4
  109.             0x100409d4 <shr1>:      .long 0x0
  110.             0x100409d8 <shr1+4>:    .long 0x0

  111.         Now run 'til main.

  112.             (gdb) r
  113.             Starting program: gdb.base/shmain
  114.             Breakpoint 1 at 0xffaf790: file gdb.base/shr1.c, line 19.

  115.             Breakpoint 2, main ()
  116.                 at gdb.base/shmain.c:44
  117.             44        g = 1;

  118.         Examine the PLT again.  Note that the loading of the shared
  119.         library has initialized the PLT to code which loads a constant
  120.         (which I think is an index into the GOT) into r11 and then
  121.         branchs a short distance to the code which actually does the
  122.         resolving.

  123.             (gdb) x/2i 0x100409d4
  124.             0x100409d4 <shr1>:      li      r11,4
  125.             0x100409d8 <shr1+4>:    b       0x10040984 <sg+4>
  126.             (gdb) c
  127.             Continuing.

  128.             Breakpoint 1, shr1 (x=1)
  129.                 at gdb.base/shr1.c:19
  130.             19        l = 1;

  131.         Now we've hit the breakpoint at shr1.  (The breakpoint was
  132.         reset from the PLT entry to the actual shr1 function after the
  133.         shared library was loaded.) Note that the PLT entry has been
  134.         resolved to contain a branch that takes us directly to shr1.
  135.         (The real one, not the PLT entry.)

  136.             (gdb) x/2i 0x100409d4
  137.             0x100409d4 <shr1>:      b       0xffaf76c <shr1>
  138.             0x100409d8 <shr1+4>:    b       0x10040984 <sg+4>

  139.    The thing to note here is that the PLT entry for shr1 has been
  140.    changed twice.

  141.    Now the problem should be obvious.  GDB places a breakpoint (a
  142.    trap instruction) on the zero value of the PLT entry for shr1.
  143.    Later on, after the shared library had been loaded and the PLT
  144.    initialized, GDB gets a signal indicating this fact and attempts
  145.    (as it always does when it stops) to remove all the breakpoints.

  146.    The breakpoint removal was causing the former contents (a zero
  147.    word) to be written back to the now initialized PLT entry thus
  148.    destroying a portion of the initialization that had occurred only a
  149.    short time ago.  When execution continued, the zero word would be
  150.    executed as an instruction an illegal instruction trap was
  151.    generated instead.  (0 is not a legal instruction.)

  152.    The fix for this problem was fairly straightforward.  The function
  153.    memory_remove_breakpoint from mem-break.c was copied to this file,
  154.    modified slightly, and renamed to ppc_linux_memory_remove_breakpoint.
  155.    In tm-linux.h, MEMORY_REMOVE_BREAKPOINT is defined to call this new
  156.    function.

  157.    The differences between ppc_linux_memory_remove_breakpoint () and
  158.    memory_remove_breakpoint () are minor.  All that the former does
  159.    that the latter does not is check to make sure that the breakpoint
  160.    location actually contains a breakpoint (trap instruction) prior
  161.    to attempting to write back the old contents.  If it does contain
  162.    a trap instruction, we allow the old contents to be written back.
  163.    Otherwise, we silently do nothing.

  164.    The big question is whether memory_remove_breakpoint () should be
  165.    changed to have the same functionality.  The downside is that more
  166.    traffic is generated for remote targets since we'll have an extra
  167.    fetch of a memory word each time a breakpoint is removed.

  168.    For the time being, we'll leave this self-modifying-code-friendly
  169.    version in ppc-linux-tdep.c, but it ought to be migrated somewhere
  170.    else in the event that some other platform has similar needs with
  171.    regard to removing breakpoints in some potentially self modifying
  172.    code.  */
  173. static int
  174. ppc_linux_memory_remove_breakpoint (struct gdbarch *gdbarch,
  175.                                     struct bp_target_info *bp_tgt)
  176. {
  177.   CORE_ADDR addr = bp_tgt->reqstd_address;
  178.   const unsigned char *bp;
  179.   int val;
  180.   int bplen;
  181.   gdb_byte old_contents[BREAKPOINT_MAX];
  182.   struct cleanup *cleanup;

  183.   /* Determine appropriate breakpoint contents and size for this address.  */
  184.   bp = gdbarch_breakpoint_from_pc (gdbarch, &addr, &bplen);
  185.   if (bp == NULL)
  186.     error (_("Software breakpoints not implemented for this target."));

  187.   /* Make sure we see the memory breakpoints.  */
  188.   cleanup = make_show_memory_breakpoints_cleanup (1);
  189.   val = target_read_memory (addr, old_contents, bplen);

  190.   /* If our breakpoint is no longer at the address, this means that the
  191.      program modified the code on us, so it is wrong to put back the
  192.      old value.  */
  193.   if (val == 0 && memcmp (bp, old_contents, bplen) == 0)
  194.     val = target_write_raw_memory (addr, bp_tgt->shadow_contents, bplen);

  195.   do_cleanups (cleanup);
  196.   return val;
  197. }

  198. /* For historic reasons, PPC 32 GNU/Linux follows PowerOpen rather
  199.    than the 32 bit SYSV R4 ABI structure return convention - all
  200.    structures, no matter their size, are put in memory.  Vectors,
  201.    which were added later, do get returned in a register though.  */

  202. static enum return_value_convention
  203. ppc_linux_return_value (struct gdbarch *gdbarch, struct value *function,
  204.                         struct type *valtype, struct regcache *regcache,
  205.                         gdb_byte *readbuf, const gdb_byte *writebuf)
  206. {
  207.   if ((TYPE_CODE (valtype) == TYPE_CODE_STRUCT
  208.        || TYPE_CODE (valtype) == TYPE_CODE_UNION)
  209.       && !((TYPE_LENGTH (valtype) == 16 || TYPE_LENGTH (valtype) == 8)
  210.            && TYPE_VECTOR (valtype)))
  211.     return RETURN_VALUE_STRUCT_CONVENTION;
  212.   else
  213.     return ppc_sysv_abi_return_value (gdbarch, function, valtype, regcache,
  214.                                       readbuf, writebuf);
  215. }

  216. /* PLT stub in executable.  */
  217. static struct ppc_insn_pattern powerpc32_plt_stub[] =
  218.   {
  219.     { 0xffff0000, 0x3d600000, 0 },        /* lis   r11, xxxx         */
  220.     { 0xffff0000, 0x816b0000, 0 },        /* lwz   r11, xxxx(r11)  */
  221.     { 0xffffffff, 0x7d6903a6, 0 },        /* mtctr r11                 */
  222.     { 0xffffffff, 0x4e800420, 0 },        /* bctr                         */
  223.     {          0,          0, 0 }
  224.   };

  225. /* PLT stub in shared library.  */
  226. static struct ppc_insn_pattern powerpc32_plt_stub_so[] =
  227.   {
  228.     { 0xffff0000, 0x817e0000, 0 },        /* lwz   r11, xxxx(r30)  */
  229.     { 0xffffffff, 0x7d6903a6, 0 },        /* mtctr r11                 */
  230.     { 0xffffffff, 0x4e800420, 0 },        /* bctr                         */
  231.     { 0xffffffff, 0x60000000, 0 },        /* nop                         */
  232.     {          0,          0, 0 }
  233.   };
  234. #define POWERPC32_PLT_STUB_LEN         ARRAY_SIZE (powerpc32_plt_stub)

  235. /* Check if PC is in PLT stub.  For non-secure PLT, stub is in .plt
  236.    section.  For secure PLT, stub is in .text and we need to check
  237.    instruction patterns.  */

  238. static int
  239. powerpc_linux_in_dynsym_resolve_code (CORE_ADDR pc)
  240. {
  241.   struct bound_minimal_symbol sym;

  242.   /* Check whether PC is in the dynamic linker.  This also checks
  243.      whether it is in the .plt section, used by non-PIC executables.  */
  244.   if (svr4_in_dynsym_resolve_code (pc))
  245.     return 1;

  246.   /* Check if we are in the resolver.  */
  247.   sym = lookup_minimal_symbol_by_pc (pc);
  248.   if (sym.minsym != NULL
  249.       && (strcmp (MSYMBOL_LINKAGE_NAME (sym.minsym), "__glink") == 0
  250.           || strcmp (MSYMBOL_LINKAGE_NAME (sym.minsym),
  251.                      "__glink_PLTresolve") == 0))
  252.     return 1;

  253.   return 0;
  254. }

  255. /* Follow PLT stub to actual routine.  */

  256. static CORE_ADDR
  257. ppc_skip_trampoline_code (struct frame_info *frame, CORE_ADDR pc)
  258. {
  259.   unsigned int insnbuf[POWERPC32_PLT_STUB_LEN];
  260.   struct gdbarch *gdbarch = get_frame_arch (frame);
  261.   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
  262.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  263.   CORE_ADDR target = 0;

  264.   if (ppc_insns_match_pattern (frame, pc, powerpc32_plt_stub, insnbuf))
  265.     {
  266.       /* Insn pattern is
  267.                 lis   r11, xxxx
  268.                 lwz   r11, xxxx(r11)
  269.          Branch target is in r11.  */

  270.       target = (ppc_insn_d_field (insnbuf[0]) << 16)
  271.         | ppc_insn_d_field (insnbuf[1]);
  272.       target = read_memory_unsigned_integer (target, 4, byte_order);
  273.     }

  274.   if (ppc_insns_match_pattern (frame, pc, powerpc32_plt_stub_so, insnbuf))
  275.     {
  276.       /* Insn pattern is
  277.                 lwz   r11, xxxx(r30)
  278.          Branch target is in r11.  */

  279.       target = get_frame_register_unsigned (frame, tdep->ppc_gp0_regnum + 30)
  280.                + ppc_insn_d_field (insnbuf[0]);
  281.       target = read_memory_unsigned_integer (target, 4, byte_order);
  282.     }

  283.   return target;
  284. }

  285. /* Wrappers to handle Linux-only registers.  */

  286. static void
  287. ppc_linux_supply_gregset (const struct regset *regset,
  288.                           struct regcache *regcache,
  289.                           int regnum, const void *gregs, size_t len)
  290. {
  291.   const struct ppc_reg_offsets *offsets = regset->regmap;

  292.   ppc_supply_gregset (regset, regcache, regnum, gregs, len);

  293.   if (ppc_linux_trap_reg_p (get_regcache_arch (regcache)))
  294.     {
  295.       /* "orig_r3" is stored 2 slots after "pc".  */
  296.       if (regnum == -1 || regnum == PPC_ORIG_R3_REGNUM)
  297.         ppc_supply_reg (regcache, PPC_ORIG_R3_REGNUM, gregs,
  298.                         offsets->pc_offset + 2 * offsets->gpr_size,
  299.                         offsets->gpr_size);

  300.       /* "trap" is stored 8 slots after "pc".  */
  301.       if (regnum == -1 || regnum == PPC_TRAP_REGNUM)
  302.         ppc_supply_reg (regcache, PPC_TRAP_REGNUM, gregs,
  303.                         offsets->pc_offset + 8 * offsets->gpr_size,
  304.                         offsets->gpr_size);
  305.     }
  306. }

  307. static void
  308. ppc_linux_collect_gregset (const struct regset *regset,
  309.                            const struct regcache *regcache,
  310.                            int regnum, void *gregs, size_t len)
  311. {
  312.   const struct ppc_reg_offsets *offsets = regset->regmap;

  313.   /* Clear areas in the linux gregset not written elsewhere.  */
  314.   if (regnum == -1)
  315.     memset (gregs, 0, len);

  316.   ppc_collect_gregset (regset, regcache, regnum, gregs, len);

  317.   if (ppc_linux_trap_reg_p (get_regcache_arch (regcache)))
  318.     {
  319.       /* "orig_r3" is stored 2 slots after "pc".  */
  320.       if (regnum == -1 || regnum == PPC_ORIG_R3_REGNUM)
  321.         ppc_collect_reg (regcache, PPC_ORIG_R3_REGNUM, gregs,
  322.                          offsets->pc_offset + 2 * offsets->gpr_size,
  323.                          offsets->gpr_size);

  324.       /* "trap" is stored 8 slots after "pc".  */
  325.       if (regnum == -1 || regnum == PPC_TRAP_REGNUM)
  326.         ppc_collect_reg (regcache, PPC_TRAP_REGNUM, gregs,
  327.                          offsets->pc_offset + 8 * offsets->gpr_size,
  328.                          offsets->gpr_size);
  329.     }
  330. }

  331. /* Regset descriptions.  */
  332. static const struct ppc_reg_offsets ppc32_linux_reg_offsets =
  333.   {
  334.     /* General-purpose registers.  */
  335.     /* .r0_offset = */ 0,
  336.     /* .gpr_size = */ 4,
  337.     /* .xr_size = */ 4,
  338.     /* .pc_offset = */ 128,
  339.     /* .ps_offset = */ 132,
  340.     /* .cr_offset = */ 152,
  341.     /* .lr_offset = */ 144,
  342.     /* .ctr_offset = */ 140,
  343.     /* .xer_offset = */ 148,
  344.     /* .mq_offset = */ 156,

  345.     /* Floating-point registers.  */
  346.     /* .f0_offset = */ 0,
  347.     /* .fpscr_offset = */ 256,
  348.     /* .fpscr_size = */ 8,

  349.     /* AltiVec registers.  */
  350.     /* .vr0_offset = */ 0,
  351.     /* .vscr_offset = */ 512 + 12,
  352.     /* .vrsave_offset = */ 528
  353.   };

  354. static const struct ppc_reg_offsets ppc64_linux_reg_offsets =
  355.   {
  356.     /* General-purpose registers.  */
  357.     /* .r0_offset = */ 0,
  358.     /* .gpr_size = */ 8,
  359.     /* .xr_size = */ 8,
  360.     /* .pc_offset = */ 256,
  361.     /* .ps_offset = */ 264,
  362.     /* .cr_offset = */ 304,
  363.     /* .lr_offset = */ 288,
  364.     /* .ctr_offset = */ 280,
  365.     /* .xer_offset = */ 296,
  366.     /* .mq_offset = */ 312,

  367.     /* Floating-point registers.  */
  368.     /* .f0_offset = */ 0,
  369.     /* .fpscr_offset = */ 256,
  370.     /* .fpscr_size = */ 8,

  371.     /* AltiVec registers.  */
  372.     /* .vr0_offset = */ 0,
  373.     /* .vscr_offset = */ 512 + 12,
  374.     /* .vrsave_offset = */ 528
  375.   };

  376. static const struct regset ppc32_linux_gregset = {
  377.   &ppc32_linux_reg_offsets,
  378.   ppc_linux_supply_gregset,
  379.   ppc_linux_collect_gregset
  380. };

  381. static const struct regset ppc64_linux_gregset = {
  382.   &ppc64_linux_reg_offsets,
  383.   ppc_linux_supply_gregset,
  384.   ppc_linux_collect_gregset
  385. };

  386. static const struct regset ppc32_linux_fpregset = {
  387.   &ppc32_linux_reg_offsets,
  388.   ppc_supply_fpregset,
  389.   ppc_collect_fpregset
  390. };

  391. static const struct regset ppc32_linux_vrregset = {
  392.   &ppc32_linux_reg_offsets,
  393.   ppc_supply_vrregset,
  394.   ppc_collect_vrregset
  395. };

  396. static const struct regset ppc32_linux_vsxregset = {
  397.   &ppc32_linux_reg_offsets,
  398.   ppc_supply_vsxregset,
  399.   ppc_collect_vsxregset
  400. };

  401. const struct regset *
  402. ppc_linux_gregset (int wordsize)
  403. {
  404.   return wordsize == 8 ? &ppc64_linux_gregset : &ppc32_linux_gregset;
  405. }

  406. const struct regset *
  407. ppc_linux_fpregset (void)
  408. {
  409.   return &ppc32_linux_fpregset;
  410. }

  411. /* Iterate over supported core file register note sections. */

  412. static void
  413. ppc_linux_iterate_over_regset_sections (struct gdbarch *gdbarch,
  414.                                         iterate_over_regset_sections_cb *cb,
  415.                                         void *cb_data,
  416.                                         const struct regcache *regcache)
  417. {
  418.   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
  419.   int have_altivec = tdep->ppc_vr0_regnum != -1;
  420.   int have_vsx = tdep->ppc_vsr0_upper_regnum != -1;

  421.   if (tdep->wordsize == 4)
  422.     cb (".reg", 48 * 4, &ppc32_linux_gregset, NULL, cb_data);
  423.   else
  424.     cb (".reg", 48 * 8, &ppc64_linux_gregset, NULL, cb_data);

  425.   cb (".reg2", 264, &ppc32_linux_fpregset, NULL, cb_data);

  426.   if (have_altivec)
  427.     cb (".reg-ppc-vmx", 544, &ppc32_linux_vrregset, "ppc Altivec", cb_data);

  428.   if (have_vsx)
  429.     cb (".reg-ppc-vsx", 256, &ppc32_linux_vsxregset, "POWER7 VSX", cb_data);
  430. }

  431. static void
  432. ppc_linux_sigtramp_cache (struct frame_info *this_frame,
  433.                           struct trad_frame_cache *this_cache,
  434.                           CORE_ADDR func, LONGEST offset,
  435.                           int bias)
  436. {
  437.   CORE_ADDR base;
  438.   CORE_ADDR regs;
  439.   CORE_ADDR gpregs;
  440.   CORE_ADDR fpregs;
  441.   int i;
  442.   struct gdbarch *gdbarch = get_frame_arch (this_frame);
  443.   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
  444.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);

  445.   base = get_frame_register_unsigned (this_frame,
  446.                                       gdbarch_sp_regnum (gdbarch));
  447.   if (bias > 0 && get_frame_pc (this_frame) != func)
  448.     /* See below, some signal trampolines increment the stack as their
  449.        first instruction, need to compensate for that.  */
  450.     base -= bias;

  451.   /* Find the address of the register buffer pointer.  */
  452.   regs = base + offset;
  453.   /* Use that to find the address of the corresponding register
  454.      buffers.  */
  455.   gpregs = read_memory_unsigned_integer (regs, tdep->wordsize, byte_order);
  456.   fpregs = gpregs + 48 * tdep->wordsize;

  457.   /* General purpose.  */
  458.   for (i = 0; i < 32; i++)
  459.     {
  460.       int regnum = i + tdep->ppc_gp0_regnum;
  461.       trad_frame_set_reg_addr (this_cache,
  462.                                regnum, gpregs + i * tdep->wordsize);
  463.     }
  464.   trad_frame_set_reg_addr (this_cache,
  465.                            gdbarch_pc_regnum (gdbarch),
  466.                            gpregs + 32 * tdep->wordsize);
  467.   trad_frame_set_reg_addr (this_cache, tdep->ppc_ctr_regnum,
  468.                            gpregs + 35 * tdep->wordsize);
  469.   trad_frame_set_reg_addr (this_cache, tdep->ppc_lr_regnum,
  470.                            gpregs + 36 * tdep->wordsize);
  471.   trad_frame_set_reg_addr (this_cache, tdep->ppc_xer_regnum,
  472.                            gpregs + 37 * tdep->wordsize);
  473.   trad_frame_set_reg_addr (this_cache, tdep->ppc_cr_regnum,
  474.                            gpregs + 38 * tdep->wordsize);

  475.   if (ppc_linux_trap_reg_p (gdbarch))
  476.     {
  477.       trad_frame_set_reg_addr (this_cache, PPC_ORIG_R3_REGNUM,
  478.                                gpregs + 34 * tdep->wordsize);
  479.       trad_frame_set_reg_addr (this_cache, PPC_TRAP_REGNUM,
  480.                                gpregs + 40 * tdep->wordsize);
  481.     }

  482.   if (ppc_floating_point_unit_p (gdbarch))
  483.     {
  484.       /* Floating point registers.  */
  485.       for (i = 0; i < 32; i++)
  486.         {
  487.           int regnum = i + gdbarch_fp0_regnum (gdbarch);
  488.           trad_frame_set_reg_addr (this_cache, regnum,
  489.                                    fpregs + i * tdep->wordsize);
  490.         }
  491.       trad_frame_set_reg_addr (this_cache, tdep->ppc_fpscr_regnum,
  492.                          fpregs + 32 * tdep->wordsize);
  493.     }
  494.   trad_frame_set_id (this_cache, frame_id_build (base, func));
  495. }

  496. static void
  497. ppc32_linux_sigaction_cache_init (const struct tramp_frame *self,
  498.                                   struct frame_info *this_frame,
  499.                                   struct trad_frame_cache *this_cache,
  500.                                   CORE_ADDR func)
  501. {
  502.   ppc_linux_sigtramp_cache (this_frame, this_cache, func,
  503.                             0xd0 /* Offset to ucontext_t.  */
  504.                             + 0x30 /* Offset to .reg.  */,
  505.                             0);
  506. }

  507. static void
  508. ppc64_linux_sigaction_cache_init (const struct tramp_frame *self,
  509.                                   struct frame_info *this_frame,
  510.                                   struct trad_frame_cache *this_cache,
  511.                                   CORE_ADDR func)
  512. {
  513.   ppc_linux_sigtramp_cache (this_frame, this_cache, func,
  514.                             0x80 /* Offset to ucontext_t.  */
  515.                             + 0xe0 /* Offset to .reg.  */,
  516.                             128);
  517. }

  518. static void
  519. ppc32_linux_sighandler_cache_init (const struct tramp_frame *self,
  520.                                    struct frame_info *this_frame,
  521.                                    struct trad_frame_cache *this_cache,
  522.                                    CORE_ADDR func)
  523. {
  524.   ppc_linux_sigtramp_cache (this_frame, this_cache, func,
  525.                             0x40 /* Offset to ucontext_t.  */
  526.                             + 0x1c /* Offset to .reg.  */,
  527.                             0);
  528. }

  529. static void
  530. ppc64_linux_sighandler_cache_init (const struct tramp_frame *self,
  531.                                    struct frame_info *this_frame,
  532.                                    struct trad_frame_cache *this_cache,
  533.                                    CORE_ADDR func)
  534. {
  535.   ppc_linux_sigtramp_cache (this_frame, this_cache, func,
  536.                             0x80 /* Offset to struct sigcontext.  */
  537.                             + 0x38 /* Offset to .reg.  */,
  538.                             128);
  539. }

  540. static struct tramp_frame ppc32_linux_sigaction_tramp_frame = {
  541.   SIGTRAMP_FRAME,
  542.   4,
  543.   {
  544.     { 0x380000ac, -1 }, /* li r0, 172 */
  545.     { 0x44000002, -1 }, /* sc */
  546.     { TRAMP_SENTINEL_INSN },
  547.   },
  548.   ppc32_linux_sigaction_cache_init
  549. };
  550. static struct tramp_frame ppc64_linux_sigaction_tramp_frame = {
  551.   SIGTRAMP_FRAME,
  552.   4,
  553.   {
  554.     { 0x38210080, -1 }, /* addi r1,r1,128 */
  555.     { 0x380000ac, -1 }, /* li r0, 172 */
  556.     { 0x44000002, -1 }, /* sc */
  557.     { TRAMP_SENTINEL_INSN },
  558.   },
  559.   ppc64_linux_sigaction_cache_init
  560. };
  561. static struct tramp_frame ppc32_linux_sighandler_tramp_frame = {
  562.   SIGTRAMP_FRAME,
  563.   4,
  564.   {
  565.     { 0x38000077, -1 }, /* li r0,119 */
  566.     { 0x44000002, -1 }, /* sc */
  567.     { TRAMP_SENTINEL_INSN },
  568.   },
  569.   ppc32_linux_sighandler_cache_init
  570. };
  571. static struct tramp_frame ppc64_linux_sighandler_tramp_frame = {
  572.   SIGTRAMP_FRAME,
  573.   4,
  574.   {
  575.     { 0x38210080, -1 }, /* addi r1,r1,128 */
  576.     { 0x38000077, -1 }, /* li r0,119 */
  577.     { 0x44000002, -1 }, /* sc */
  578.     { TRAMP_SENTINEL_INSN },
  579.   },
  580.   ppc64_linux_sighandler_cache_init
  581. };


  582. /* Address to use for displaced stepping.  When debugging a stand-alone
  583.    SPU executable, entry_point_address () will point to an SPU local-store
  584.    address and is thus not usable as displaced stepping location.  We use
  585.    the auxiliary vector to determine the PowerPC-side entry point address
  586.    instead.  */

  587. static CORE_ADDR ppc_linux_entry_point_addr = 0;

  588. static void
  589. ppc_linux_inferior_created (struct target_ops *target, int from_tty)
  590. {
  591.   ppc_linux_entry_point_addr = 0;
  592. }

  593. static CORE_ADDR
  594. ppc_linux_displaced_step_location (struct gdbarch *gdbarch)
  595. {
  596.   if (ppc_linux_entry_point_addr == 0)
  597.     {
  598.       CORE_ADDR addr;

  599.       /* Determine entry point from target auxiliary vector.  */
  600.       if (target_auxv_search (&current_target, AT_ENTRY, &addr) <= 0)
  601.         error (_("Cannot find AT_ENTRY auxiliary vector entry."));

  602.       /* Make certain that the address points at real code, and not a
  603.          function descriptor.  */
  604.       addr = gdbarch_convert_from_func_ptr_addr (gdbarch, addr,
  605.                                                  &current_target);

  606.       /* Inferior calls also use the entry point as a breakpoint location.
  607.          We don't want displaced stepping to interfere with those
  608.          breakpoints, so leave space.  */
  609.       ppc_linux_entry_point_addr = addr + 2 * PPC_INSN_SIZE;
  610.     }

  611.   return ppc_linux_entry_point_addr;
  612. }


  613. /* Return 1 if PPC_ORIG_R3_REGNUM and PPC_TRAP_REGNUM are usable.  */
  614. int
  615. ppc_linux_trap_reg_p (struct gdbarch *gdbarch)
  616. {
  617.   /* If we do not have a target description with registers, then
  618.      the special registers will not be included in the register set.  */
  619.   if (!tdesc_has_registers (gdbarch_target_desc (gdbarch)))
  620.     return 0;

  621.   /* If we do, then it is safe to check the size.  */
  622.   return register_size (gdbarch, PPC_ORIG_R3_REGNUM) > 0
  623.          && register_size (gdbarch, PPC_TRAP_REGNUM) > 0;
  624. }

  625. /* Return the current system call's number present in the
  626.    r0 register.  When the function fails, it returns -1.  */
  627. static LONGEST
  628. ppc_linux_get_syscall_number (struct gdbarch *gdbarch,
  629.                               ptid_t ptid)
  630. {
  631.   struct regcache *regcache = get_thread_regcache (ptid);
  632.   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
  633.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  634.   struct cleanup *cleanbuf;
  635.   /* The content of a register */
  636.   gdb_byte *buf;
  637.   /* The result */
  638.   LONGEST ret;

  639.   /* Make sure we're in a 32- or 64-bit machine */
  640.   gdb_assert (tdep->wordsize == 4 || tdep->wordsize == 8);

  641.   buf = (gdb_byte *) xmalloc (tdep->wordsize * sizeof (gdb_byte));

  642.   cleanbuf = make_cleanup (xfree, buf);

  643.   /* Getting the system call number from the register.
  644.      When dealing with PowerPC architecture, this information
  645.      is stored at 0th register.  */
  646.   regcache_cooked_read (regcache, tdep->ppc_gp0_regnum, buf);

  647.   ret = extract_signed_integer (buf, tdep->wordsize, byte_order);
  648.   do_cleanups (cleanbuf);

  649.   return ret;
  650. }

  651. static void
  652. ppc_linux_write_pc (struct regcache *regcache, CORE_ADDR pc)
  653. {
  654.   struct gdbarch *gdbarch = get_regcache_arch (regcache);

  655.   regcache_cooked_write_unsigned (regcache, gdbarch_pc_regnum (gdbarch), pc);

  656.   /* Set special TRAP register to -1 to prevent the kernel from
  657.      messing with the PC we just installed, if we happen to be
  658.      within an interrupted system call that the kernel wants to
  659.      restart.

  660.      Note that after we return from the dummy call, the TRAP and
  661.      ORIG_R3 registers will be automatically restored, and the
  662.      kernel continues to restart the system call at this point.  */
  663.   if (ppc_linux_trap_reg_p (gdbarch))
  664.     regcache_cooked_write_unsigned (regcache, PPC_TRAP_REGNUM, -1);
  665. }

  666. static int
  667. ppc_linux_spu_section (bfd *abfd, asection *asect, void *user_data)
  668. {
  669.   return strncmp (bfd_section_name (abfd, asect), "SPU/", 4) == 0;
  670. }

  671. static const struct target_desc *
  672. ppc_linux_core_read_description (struct gdbarch *gdbarch,
  673.                                  struct target_ops *target,
  674.                                  bfd *abfd)
  675. {
  676.   asection *cell = bfd_sections_find_if (abfd, ppc_linux_spu_section, NULL);
  677.   asection *altivec = bfd_get_section_by_name (abfd, ".reg-ppc-vmx");
  678.   asection *vsx = bfd_get_section_by_name (abfd, ".reg-ppc-vsx");
  679.   asection *section = bfd_get_section_by_name (abfd, ".reg");
  680.   if (! section)
  681.     return NULL;

  682.   switch (bfd_section_size (abfd, section))
  683.     {
  684.     case 48 * 4:
  685.       if (cell)
  686.         return tdesc_powerpc_cell32l;
  687.       else if (vsx)
  688.         return tdesc_powerpc_vsx32l;
  689.       else if (altivec)
  690.         return tdesc_powerpc_altivec32l;
  691.       else
  692.         return tdesc_powerpc_32l;

  693.     case 48 * 8:
  694.       if (cell)
  695.         return tdesc_powerpc_cell64l;
  696.       else if (vsx)
  697.         return tdesc_powerpc_vsx64l;
  698.       else if (altivec)
  699.         return tdesc_powerpc_altivec64l;
  700.       else
  701.         return tdesc_powerpc_64l;

  702.     default:
  703.       return NULL;
  704.     }
  705. }


  706. /* Implementation of `gdbarch_elf_make_msymbol_special', as defined in
  707.    gdbarch.h.  This implementation is used for the ELFv2 ABI only.  */

  708. static void
  709. ppc_elfv2_elf_make_msymbol_special (asymbol *sym, struct minimal_symbol *msym)
  710. {
  711.   elf_symbol_type *elf_sym = (elf_symbol_type *)sym;

  712.   /* If the symbol is marked as having a local entry point, set a target
  713.      flag in the msymbol.  We currently only support local entry point
  714.      offsets of 8 bytes, which is the only entry point offset ever used
  715.      by current compilers.  If/when other offsets are ever used, we will
  716.      have to use additional target flag bits to store them.  */
  717.   switch (PPC64_LOCAL_ENTRY_OFFSET (elf_sym->internal_elf_sym.st_other))
  718.     {
  719.     default:
  720.       break;
  721.     case 8:
  722.       MSYMBOL_TARGET_FLAG_1 (msym) = 1;
  723.       break;
  724.     }
  725. }

  726. /* Implementation of `gdbarch_skip_entrypoint', as defined in
  727.    gdbarch.h.  This implementation is used for the ELFv2 ABI only.  */

  728. static CORE_ADDR
  729. ppc_elfv2_skip_entrypoint (struct gdbarch *gdbarch, CORE_ADDR pc)
  730. {
  731.   struct bound_minimal_symbol fun;
  732.   int local_entry_offset = 0;

  733.   fun = lookup_minimal_symbol_by_pc (pc);
  734.   if (fun.minsym == NULL)
  735.     return pc;

  736.   /* See ppc_elfv2_elf_make_msymbol_special for how local entry point
  737.      offset values are encoded.  */
  738.   if (MSYMBOL_TARGET_FLAG_1 (fun.minsym))
  739.     local_entry_offset = 8;

  740.   if (BMSYMBOL_VALUE_ADDRESS (fun) <= pc
  741.       && pc < BMSYMBOL_VALUE_ADDRESS (fun) + local_entry_offset)
  742.     return BMSYMBOL_VALUE_ADDRESS (fun) + local_entry_offset;

  743.   return pc;
  744. }

  745. /* Implementation of `gdbarch_stap_is_single_operand', as defined in
  746.    gdbarch.h.  */

  747. static int
  748. ppc_stap_is_single_operand (struct gdbarch *gdbarch, const char *s)
  749. {
  750.   return (*s == 'i' /* Literal number.  */
  751.           || (isdigit (*s) && s[1] == '('
  752.               && isdigit (s[2])) /* Displacement.  */
  753.           || (*s == '(' && isdigit (s[1])) /* Register indirection.  */
  754.           || isdigit (*s)); /* Register value.  */
  755. }

  756. /* Implementation of `gdbarch_stap_parse_special_token', as defined in
  757.    gdbarch.h.  */

  758. static int
  759. ppc_stap_parse_special_token (struct gdbarch *gdbarch,
  760.                               struct stap_parse_info *p)
  761. {
  762.   if (isdigit (*p->arg))
  763.     {
  764.       /* This temporary pointer is needed because we have to do a lookahead.
  765.           We could be dealing with a register displacement, and in such case
  766.           we would not need to do anything.  */
  767.       const char *s = p->arg;
  768.       char *regname;
  769.       int len;
  770.       struct stoken str;

  771.       while (isdigit (*s))
  772.         ++s;

  773.       if (*s == '(')
  774.         {
  775.           /* It is a register displacement indeed.  Returning 0 means we are
  776.              deferring the treatment of this case to the generic parser.  */
  777.           return 0;
  778.         }

  779.       len = s - p->arg;
  780.       regname = alloca (len + 2);
  781.       regname[0] = 'r';

  782.       strncpy (regname + 1, p->arg, len);
  783.       ++len;
  784.       regname[len] = '\0';

  785.       if (user_reg_map_name_to_regnum (gdbarch, regname, len) == -1)
  786.         error (_("Invalid register name `%s' on expression `%s'."),
  787.                regname, p->saved_arg);

  788.       write_exp_elt_opcode (&p->pstate, OP_REGISTER);
  789.       str.ptr = regname;
  790.       str.length = len;
  791.       write_exp_string (&p->pstate, str);
  792.       write_exp_elt_opcode (&p->pstate, OP_REGISTER);

  793.       p->arg = s;
  794.     }
  795.   else
  796.     {
  797.       /* All the other tokens should be handled correctly by the generic
  798.          parser.  */
  799.       return 0;
  800.     }

  801.   return 1;
  802. }

  803. /* Cell/B.E. active SPE context tracking support.  */

  804. static struct objfile *spe_context_objfile = NULL;
  805. static CORE_ADDR spe_context_lm_addr = 0;
  806. static CORE_ADDR spe_context_offset = 0;

  807. static ptid_t spe_context_cache_ptid;
  808. static CORE_ADDR spe_context_cache_address;

  809. /* Hook into inferior_created, solib_loaded, and solib_unloaded observers
  810.    to track whether we've loaded a version of libspe2 (as static or dynamic
  811.    library) that provides the __spe_current_active_context variable.  */
  812. static void
  813. ppc_linux_spe_context_lookup (struct objfile *objfile)
  814. {
  815.   struct bound_minimal_symbol sym;

  816.   if (!objfile)
  817.     {
  818.       spe_context_objfile = NULL;
  819.       spe_context_lm_addr = 0;
  820.       spe_context_offset = 0;
  821.       spe_context_cache_ptid = minus_one_ptid;
  822.       spe_context_cache_address = 0;
  823.       return;
  824.     }

  825.   sym = lookup_minimal_symbol ("__spe_current_active_context", NULL, objfile);
  826.   if (sym.minsym)
  827.     {
  828.       spe_context_objfile = objfile;
  829.       spe_context_lm_addr = svr4_fetch_objfile_link_map (objfile);
  830.       spe_context_offset = BMSYMBOL_VALUE_ADDRESS (sym);
  831.       spe_context_cache_ptid = minus_one_ptid;
  832.       spe_context_cache_address = 0;
  833.       return;
  834.     }
  835. }

  836. static void
  837. ppc_linux_spe_context_inferior_created (struct target_ops *t, int from_tty)
  838. {
  839.   struct objfile *objfile;

  840.   ppc_linux_spe_context_lookup (NULL);
  841.   ALL_OBJFILES (objfile)
  842.     ppc_linux_spe_context_lookup (objfile);
  843. }

  844. static void
  845. ppc_linux_spe_context_solib_loaded (struct so_list *so)
  846. {
  847.   if (strstr (so->so_original_name, "/libspe") != NULL)
  848.     {
  849.       solib_read_symbols (so, 0);
  850.       ppc_linux_spe_context_lookup (so->objfile);
  851.     }
  852. }

  853. static void
  854. ppc_linux_spe_context_solib_unloaded (struct so_list *so)
  855. {
  856.   if (so->objfile == spe_context_objfile)
  857.     ppc_linux_spe_context_lookup (NULL);
  858. }

  859. /* Retrieve contents of the N'th element in the current thread's
  860.    linked SPE context list into ID and NPC.  Return the address of
  861.    said context element, or 0 if not found.  */
  862. static CORE_ADDR
  863. ppc_linux_spe_context (int wordsize, enum bfd_endian byte_order,
  864.                        int n, int *id, unsigned int *npc)
  865. {
  866.   CORE_ADDR spe_context = 0;
  867.   gdb_byte buf[16];
  868.   int i;

  869.   /* Quick exit if we have not found __spe_current_active_context.  */
  870.   if (!spe_context_objfile)
  871.     return 0;

  872.   /* Look up cached address of thread-local variable.  */
  873.   if (!ptid_equal (spe_context_cache_ptid, inferior_ptid))
  874.     {
  875.       struct target_ops *target = &current_target;
  876.       volatile struct gdb_exception ex;

  877.       TRY_CATCH (ex, RETURN_MASK_ERROR)
  878.         {
  879.           /* We do not call target_translate_tls_address here, because
  880.              svr4_fetch_objfile_link_map may invalidate the frame chain,
  881.              which must not do while inside a frame sniffer.

  882.              Instead, we have cached the lm_addr value, and use that to
  883.              directly call the target's to_get_thread_local_address.  */
  884.           spe_context_cache_address
  885.             = target->to_get_thread_local_address (target, inferior_ptid,
  886.                                                    spe_context_lm_addr,
  887.                                                    spe_context_offset);
  888.           spe_context_cache_ptid = inferior_ptid;
  889.         }

  890.       if (ex.reason < 0)
  891.         return 0;
  892.     }

  893.   /* Read variable value.  */
  894.   if (target_read_memory (spe_context_cache_address, buf, wordsize) == 0)
  895.     spe_context = extract_unsigned_integer (buf, wordsize, byte_order);

  896.   /* Cyle through to N'th linked list element.  */
  897.   for (i = 0; i < n && spe_context; i++)
  898.     if (target_read_memory (spe_context + align_up (12, wordsize),
  899.                             buf, wordsize) == 0)
  900.       spe_context = extract_unsigned_integer (buf, wordsize, byte_order);
  901.     else
  902.       spe_context = 0;

  903.   /* Read current context.  */
  904.   if (spe_context
  905.       && target_read_memory (spe_context, buf, 12) != 0)
  906.     spe_context = 0;

  907.   /* Extract data elements.  */
  908.   if (spe_context)
  909.     {
  910.       if (id)
  911.         *id = extract_signed_integer (buf, 4, byte_order);
  912.       if (npc)
  913.         *npc = extract_unsigned_integer (buf + 4, 4, byte_order);
  914.     }

  915.   return spe_context;
  916. }


  917. /* Cell/B.E. cross-architecture unwinder support.  */

  918. struct ppu2spu_cache
  919. {
  920.   struct frame_id frame_id;
  921.   struct regcache *regcache;
  922. };

  923. static struct gdbarch *
  924. ppu2spu_prev_arch (struct frame_info *this_frame, void **this_cache)
  925. {
  926.   struct ppu2spu_cache *cache = *this_cache;
  927.   return get_regcache_arch (cache->regcache);
  928. }

  929. static void
  930. ppu2spu_this_id (struct frame_info *this_frame,
  931.                  void **this_cache, struct frame_id *this_id)
  932. {
  933.   struct ppu2spu_cache *cache = *this_cache;
  934.   *this_id = cache->frame_id;
  935. }

  936. static struct value *
  937. ppu2spu_prev_register (struct frame_info *this_frame,
  938.                        void **this_cache, int regnum)
  939. {
  940.   struct ppu2spu_cache *cache = *this_cache;
  941.   struct gdbarch *gdbarch = get_regcache_arch (cache->regcache);
  942.   gdb_byte *buf;

  943.   buf = alloca (register_size (gdbarch, regnum));

  944.   if (regnum < gdbarch_num_regs (gdbarch))
  945.     regcache_raw_read (cache->regcache, regnum, buf);
  946.   else
  947.     gdbarch_pseudo_register_read (gdbarch, cache->regcache, regnum, buf);

  948.   return frame_unwind_got_bytes (this_frame, regnum, buf);
  949. }

  950. struct ppu2spu_data
  951. {
  952.   struct gdbarch *gdbarch;
  953.   int id;
  954.   unsigned int npc;
  955.   gdb_byte gprs[128*16];
  956. };

  957. static int
  958. ppu2spu_unwind_register (void *src, int regnum, gdb_byte *buf)
  959. {
  960.   struct ppu2spu_data *data = src;
  961.   enum bfd_endian byte_order = gdbarch_byte_order (data->gdbarch);

  962.   if (regnum >= 0 && regnum < SPU_NUM_GPRS)
  963.     memcpy (buf, data->gprs + 16*regnum, 16);
  964.   else if (regnum == SPU_ID_REGNUM)
  965.     store_unsigned_integer (buf, 4, byte_order, data->id);
  966.   else if (regnum == SPU_PC_REGNUM)
  967.     store_unsigned_integer (buf, 4, byte_order, data->npc);
  968.   else
  969.     return REG_UNAVAILABLE;

  970.   return REG_VALID;
  971. }

  972. static int
  973. ppu2spu_sniffer (const struct frame_unwind *self,
  974.                  struct frame_info *this_frame, void **this_prologue_cache)
  975. {
  976.   struct gdbarch *gdbarch = get_frame_arch (this_frame);
  977.   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
  978.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  979.   struct ppu2spu_data data;
  980.   struct frame_info *fi;
  981.   CORE_ADDR base, func, backchain, spe_context;
  982.   gdb_byte buf[8];
  983.   int n = 0;

  984.   /* Count the number of SPU contexts already in the frame chain.  */
  985.   for (fi = get_next_frame (this_frame); fi; fi = get_next_frame (fi))
  986.     if (get_frame_type (fi) == ARCH_FRAME
  987.         && gdbarch_bfd_arch_info (get_frame_arch (fi))->arch == bfd_arch_spu)
  988.       n++;

  989.   base = get_frame_sp (this_frame);
  990.   func = get_frame_pc (this_frame);
  991.   if (target_read_memory (base, buf, tdep->wordsize))
  992.     return 0;
  993.   backchain = extract_unsigned_integer (buf, tdep->wordsize, byte_order);

  994.   spe_context = ppc_linux_spe_context (tdep->wordsize, byte_order,
  995.                                        n, &data.id, &data.npc);
  996.   if (spe_context && base <= spe_context && spe_context < backchain)
  997.     {
  998.       char annex[32];

  999.       /* Find gdbarch for SPU.  */
  1000.       struct gdbarch_info info;
  1001.       gdbarch_info_init (&info);
  1002.       info.bfd_arch_info = bfd_lookup_arch (bfd_arch_spu, bfd_mach_spu);
  1003.       info.byte_order = BFD_ENDIAN_BIG;
  1004.       info.osabi = GDB_OSABI_LINUX;
  1005.       info.tdep_info = (void *) &data.id;
  1006.       data.gdbarch = gdbarch_find_by_info (info);
  1007.       if (!data.gdbarch)
  1008.         return 0;

  1009.       xsnprintf (annex, sizeof annex, "%d/regs", data.id);
  1010.       if (target_read (&current_target, TARGET_OBJECT_SPU, annex,
  1011.                        data.gprs, 0, sizeof data.gprs)
  1012.           == sizeof data.gprs)
  1013.         {
  1014.           struct ppu2spu_cache *cache
  1015.             = FRAME_OBSTACK_CALLOC (1, struct ppu2spu_cache);

  1016.           struct address_space *aspace = get_frame_address_space (this_frame);
  1017.           struct regcache *regcache = regcache_xmalloc (data.gdbarch, aspace);
  1018.           struct cleanup *cleanups = make_cleanup_regcache_xfree (regcache);
  1019.           regcache_save (regcache, ppu2spu_unwind_register, &data);
  1020.           discard_cleanups (cleanups);

  1021.           cache->frame_id = frame_id_build (base, func);
  1022.           cache->regcache = regcache;
  1023.           *this_prologue_cache = cache;
  1024.           return 1;
  1025.         }
  1026.     }

  1027.   return 0;
  1028. }

  1029. static void
  1030. ppu2spu_dealloc_cache (struct frame_info *self, void *this_cache)
  1031. {
  1032.   struct ppu2spu_cache *cache = this_cache;
  1033.   regcache_xfree (cache->regcache);
  1034. }

  1035. static const struct frame_unwind ppu2spu_unwind = {
  1036.   ARCH_FRAME,
  1037.   default_frame_unwind_stop_reason,
  1038.   ppu2spu_this_id,
  1039.   ppu2spu_prev_register,
  1040.   NULL,
  1041.   ppu2spu_sniffer,
  1042.   ppu2spu_dealloc_cache,
  1043.   ppu2spu_prev_arch,
  1044. };


  1045. static void
  1046. ppc_linux_init_abi (struct gdbarch_info info,
  1047.                     struct gdbarch *gdbarch)
  1048. {
  1049.   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
  1050.   struct tdesc_arch_data *tdesc_data = (void *) info.tdep_info;
  1051.   static const char *const stap_integer_prefixes[] = { "i", NULL };
  1052.   static const char *const stap_register_indirection_prefixes[] = { "(",
  1053.                                                                     NULL };
  1054.   static const char *const stap_register_indirection_suffixes[] = { ")",
  1055.                                                                     NULL };

  1056.   linux_init_abi (info, gdbarch);

  1057.   /* PPC GNU/Linux uses either 64-bit or 128-bit long doubles; where
  1058.      128-bit, they are IBM long double, not IEEE quad long double as
  1059.      in the System V ABI PowerPC Processor Supplement.  We can safely
  1060.      let them default to 128-bit, since the debug info will give the
  1061.      size of type actually used in each case.  */
  1062.   set_gdbarch_long_double_bit (gdbarch, 16 * TARGET_CHAR_BIT);
  1063.   set_gdbarch_long_double_format (gdbarch, floatformats_ibm_long_double);

  1064.   /* Handle inferior calls during interrupted system calls.  */
  1065.   set_gdbarch_write_pc (gdbarch, ppc_linux_write_pc);

  1066.   /* Get the syscall number from the arch's register.  */
  1067.   set_gdbarch_get_syscall_number (gdbarch, ppc_linux_get_syscall_number);

  1068.   /* SystemTap functions.  */
  1069.   set_gdbarch_stap_integer_prefixes (gdbarch, stap_integer_prefixes);
  1070.   set_gdbarch_stap_register_indirection_prefixes (gdbarch,
  1071.                                           stap_register_indirection_prefixes);
  1072.   set_gdbarch_stap_register_indirection_suffixes (gdbarch,
  1073.                                           stap_register_indirection_suffixes);
  1074.   set_gdbarch_stap_gdb_register_prefix (gdbarch, "r");
  1075.   set_gdbarch_stap_is_single_operand (gdbarch, ppc_stap_is_single_operand);
  1076.   set_gdbarch_stap_parse_special_token (gdbarch,
  1077.                                         ppc_stap_parse_special_token);

  1078.   if (tdep->wordsize == 4)
  1079.     {
  1080.       /* Until November 2001, gcc did not comply with the 32 bit SysV
  1081.          R4 ABI requirement that structures less than or equal to 8
  1082.          bytes should be returned in registers.  Instead GCC was using
  1083.          the AIX/PowerOpen ABI - everything returned in memory
  1084.          (well ignoring vectors that is).  When this was corrected, it
  1085.          wasn't fixed for GNU/Linux native platform.  Use the
  1086.          PowerOpen struct convention.  */
  1087.       set_gdbarch_return_value (gdbarch, ppc_linux_return_value);

  1088.       set_gdbarch_memory_remove_breakpoint (gdbarch,
  1089.                                             ppc_linux_memory_remove_breakpoint);

  1090.       /* Shared library handling.  */
  1091.       set_gdbarch_skip_trampoline_code (gdbarch, ppc_skip_trampoline_code);
  1092.       set_solib_svr4_fetch_link_map_offsets
  1093.         (gdbarch, svr4_ilp32_fetch_link_map_offsets);

  1094.       /* Setting the correct XML syscall filename.  */
  1095.       set_xml_syscall_file_name (gdbarch, XML_SYSCALL_FILENAME_PPC);

  1096.       /* Trampolines.  */
  1097.       tramp_frame_prepend_unwinder (gdbarch,
  1098.                                     &ppc32_linux_sigaction_tramp_frame);
  1099.       tramp_frame_prepend_unwinder (gdbarch,
  1100.                                     &ppc32_linux_sighandler_tramp_frame);

  1101.       /* BFD target for core files.  */
  1102.       if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_LITTLE)
  1103.         set_gdbarch_gcore_bfd_target (gdbarch, "elf32-powerpcle");
  1104.       else
  1105.         set_gdbarch_gcore_bfd_target (gdbarch, "elf32-powerpc");

  1106.       if (powerpc_so_ops.in_dynsym_resolve_code == NULL)
  1107.         {
  1108.           powerpc_so_ops = svr4_so_ops;
  1109.           /* Override dynamic resolve function.  */
  1110.           powerpc_so_ops.in_dynsym_resolve_code =
  1111.             powerpc_linux_in_dynsym_resolve_code;
  1112.         }
  1113.       set_solib_ops (gdbarch, &powerpc_so_ops);

  1114.       set_gdbarch_skip_solib_resolver (gdbarch, glibc_skip_solib_resolver);
  1115.     }

  1116.   if (tdep->wordsize == 8)
  1117.     {
  1118.       if (tdep->elf_abi == POWERPC_ELF_V1)
  1119.         {
  1120.           /* Handle PPC GNU/Linux 64-bit function pointers (which are really
  1121.              function descriptors).  */
  1122.           set_gdbarch_convert_from_func_ptr_addr
  1123.             (gdbarch, ppc64_convert_from_func_ptr_addr);

  1124.           set_gdbarch_elf_make_msymbol_special
  1125.             (gdbarch, ppc64_elf_make_msymbol_special);
  1126.         }
  1127.       else
  1128.         {
  1129.           set_gdbarch_elf_make_msymbol_special
  1130.             (gdbarch, ppc_elfv2_elf_make_msymbol_special);

  1131.           set_gdbarch_skip_entrypoint (gdbarch, ppc_elfv2_skip_entrypoint);
  1132.         }

  1133.       /* Shared library handling.  */
  1134.       set_gdbarch_skip_trampoline_code (gdbarch, ppc64_skip_trampoline_code);
  1135.       set_solib_svr4_fetch_link_map_offsets
  1136.         (gdbarch, svr4_lp64_fetch_link_map_offsets);

  1137.       /* Setting the correct XML syscall filename.  */
  1138.       set_xml_syscall_file_name (gdbarch, XML_SYSCALL_FILENAME_PPC64);

  1139.       /* Trampolines.  */
  1140.       tramp_frame_prepend_unwinder (gdbarch,
  1141.                                     &ppc64_linux_sigaction_tramp_frame);
  1142.       tramp_frame_prepend_unwinder (gdbarch,
  1143.                                     &ppc64_linux_sighandler_tramp_frame);

  1144.       /* BFD target for core files.  */
  1145.       if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_LITTLE)
  1146.         set_gdbarch_gcore_bfd_target (gdbarch, "elf64-powerpcle");
  1147.       else
  1148.         set_gdbarch_gcore_bfd_target (gdbarch, "elf64-powerpc");
  1149.     }

  1150.   /* PPC32 uses a different prpsinfo32 compared to most other Linux
  1151.      archs.  */
  1152.   if (tdep->wordsize == 4)
  1153.     set_gdbarch_elfcore_write_linux_prpsinfo (gdbarch,
  1154.                                               elfcore_write_ppc_linux_prpsinfo32);

  1155.   set_gdbarch_core_read_description (gdbarch, ppc_linux_core_read_description);
  1156.   set_gdbarch_iterate_over_regset_sections (gdbarch,
  1157.                                             ppc_linux_iterate_over_regset_sections);

  1158.   /* Enable TLS support.  */
  1159.   set_gdbarch_fetch_tls_load_module_address (gdbarch,
  1160.                                              svr4_fetch_objfile_link_map);

  1161.   if (tdesc_data)
  1162.     {
  1163.       const struct tdesc_feature *feature;

  1164.       /* If we have target-described registers, then we can safely
  1165.          reserve a number for PPC_ORIG_R3_REGNUM and PPC_TRAP_REGNUM
  1166.          (whether they are described or not).  */
  1167.       gdb_assert (gdbarch_num_regs (gdbarch) <= PPC_ORIG_R3_REGNUM);
  1168.       set_gdbarch_num_regs (gdbarch, PPC_TRAP_REGNUM + 1);

  1169.       /* If they are present, then assign them to the reserved number.  */
  1170.       feature = tdesc_find_feature (info.target_desc,
  1171.                                     "org.gnu.gdb.power.linux");
  1172.       if (feature != NULL)
  1173.         {
  1174.           tdesc_numbered_register (feature, tdesc_data,
  1175.                                    PPC_ORIG_R3_REGNUM, "orig_r3");
  1176.           tdesc_numbered_register (feature, tdesc_data,
  1177.                                    PPC_TRAP_REGNUM, "trap");
  1178.         }
  1179.     }

  1180.   /* Enable Cell/B.E. if supported by the target.  */
  1181.   if (tdesc_compatible_p (info.target_desc,
  1182.                           bfd_lookup_arch (bfd_arch_spu, bfd_mach_spu)))
  1183.     {
  1184.       /* Cell/B.E. multi-architecture support.  */
  1185.       set_spu_solib_ops (gdbarch);

  1186.       /* Cell/B.E. cross-architecture unwinder support.  */
  1187.       frame_unwind_prepend_unwinder (gdbarch, &ppu2spu_unwind);

  1188.       /* The default displaced_step_at_entry_point doesn't work for
  1189.          SPU stand-alone executables.  */
  1190.       set_gdbarch_displaced_step_location (gdbarch,
  1191.                                            ppc_linux_displaced_step_location);
  1192.     }

  1193.   set_gdbarch_get_siginfo_type (gdbarch, linux_get_siginfo_type);
  1194. }

  1195. /* Provide a prototype to silence -Wmissing-prototypes.  */
  1196. extern initialize_file_ftype _initialize_ppc_linux_tdep;

  1197. void
  1198. _initialize_ppc_linux_tdep (void)
  1199. {
  1200.   /* Register for all sub-familes of the POWER/PowerPC: 32-bit and
  1201.      64-bit PowerPC, and the older rs6k.  */
  1202.   gdbarch_register_osabi (bfd_arch_powerpc, bfd_mach_ppc, GDB_OSABI_LINUX,
  1203.                          ppc_linux_init_abi);
  1204.   gdbarch_register_osabi (bfd_arch_powerpc, bfd_mach_ppc64, GDB_OSABI_LINUX,
  1205.                          ppc_linux_init_abi);
  1206.   gdbarch_register_osabi (bfd_arch_rs6000, bfd_mach_rs6k, GDB_OSABI_LINUX,
  1207.                          ppc_linux_init_abi);

  1208.   /* Attach to inferior_created observer.  */
  1209.   observer_attach_inferior_created (ppc_linux_inferior_created);

  1210.   /* Attach to observers to track __spe_current_active_context.  */
  1211.   observer_attach_inferior_created (ppc_linux_spe_context_inferior_created);
  1212.   observer_attach_solib_loaded (ppc_linux_spe_context_solib_loaded);
  1213.   observer_attach_solib_unloaded (ppc_linux_spe_context_solib_unloaded);

  1214.   /* Initialize the Linux target descriptions.  */
  1215.   initialize_tdesc_powerpc_32l ();
  1216.   initialize_tdesc_powerpc_altivec32l ();
  1217.   initialize_tdesc_powerpc_cell32l ();
  1218.   initialize_tdesc_powerpc_vsx32l ();
  1219.   initialize_tdesc_powerpc_isa205_32l ();
  1220.   initialize_tdesc_powerpc_isa205_altivec32l ();
  1221.   initialize_tdesc_powerpc_isa205_vsx32l ();
  1222.   initialize_tdesc_powerpc_64l ();
  1223.   initialize_tdesc_powerpc_altivec64l ();
  1224.   initialize_tdesc_powerpc_cell64l ();
  1225.   initialize_tdesc_powerpc_vsx64l ();
  1226.   initialize_tdesc_powerpc_isa205_64l ();
  1227.   initialize_tdesc_powerpc_isa205_altivec64l ();
  1228.   initialize_tdesc_powerpc_isa205_vsx64l ();
  1229.   initialize_tdesc_powerpc_e500l ();
  1230. }