gdb/rx-tdep.c - gdb

Global variables defined

Data types defined

Functions defined

Source code

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

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

  3.    Contributed by Red Hat, Inc.

  4.    This file is part of GDB.

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

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

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

  15. #include "defs.h"
  16. #include "arch-utils.h"
  17. #include "prologue-value.h"
  18. #include "target.h"
  19. #include "regcache.h"
  20. #include "opcode/rx.h"
  21. #include "dis-asm.h"
  22. #include "gdbtypes.h"
  23. #include "frame.h"
  24. #include "frame-unwind.h"
  25. #include "frame-base.h"
  26. #include "value.h"
  27. #include "gdbcore.h"
  28. #include "dwarf2-frame.h"

  29. #include "elf/rx.h"
  30. #include "elf-bfd.h"

  31. /* Certain important register numbers.  */
  32. enum
  33. {
  34.   RX_SP_REGNUM = 0,
  35.   RX_R1_REGNUM = 1,
  36.   RX_R4_REGNUM = 4,
  37.   RX_FP_REGNUM = 6,
  38.   RX_R15_REGNUM = 15,
  39.   RX_PC_REGNUM = 19,
  40.   RX_ACC_REGNUM = 25,
  41.   RX_NUM_REGS = 26
  42. };

  43. /* Architecture specific data.  */
  44. struct gdbarch_tdep
  45. {
  46.   /* The ELF header flags specify the multilib used.  */
  47.   int elf_flags;
  48. };

  49. /* This structure holds the results of a prologue analysis.  */
  50. struct rx_prologue
  51. {
  52.   /* The offset from the frame base to the stack pointer --- always
  53.      zero or negative.

  54.      Calling this a "size" is a bit misleading, but given that the
  55.      stack grows downwards, using offsets for everything keeps one
  56.      from going completely sign-crazy: you never change anything's
  57.      sign for an ADD instruction; always change the second operand's
  58.      sign for a SUB instruction; and everything takes care of
  59.      itself.  */
  60.   int frame_size;

  61.   /* Non-zero if this function has initialized the frame pointer from
  62.      the stack pointer, zero otherwise.  */
  63.   int has_frame_ptr;

  64.   /* If has_frame_ptr is non-zero, this is the offset from the frame
  65.      base to where the frame pointer points.  This is always zero or
  66.      negative.  */
  67.   int frame_ptr_offset;

  68.   /* The address of the first instruction at which the frame has been
  69.      set up and the arguments are where the debug info says they are
  70.      --- as best as we can tell.  */
  71.   CORE_ADDR prologue_end;

  72.   /* reg_offset[R] is the offset from the CFA at which register R is
  73.      saved, or 1 if register R has not been saved.  (Real values are
  74.      always zero or negative.)  */
  75.   int reg_offset[RX_NUM_REGS];
  76. };

  77. /* Implement the "register_name" gdbarch method.  */
  78. static const char *
  79. rx_register_name (struct gdbarch *gdbarch, int regnr)
  80. {
  81.   static const char *const reg_names[] = {
  82.     "r0",
  83.     "r1",
  84.     "r2",
  85.     "r3",
  86.     "r4",
  87.     "r5",
  88.     "r6",
  89.     "r7",
  90.     "r8",
  91.     "r9",
  92.     "r10",
  93.     "r11",
  94.     "r12",
  95.     "r13",
  96.     "r14",
  97.     "r15",
  98.     "usp",
  99.     "isp",
  100.     "psw",
  101.     "pc",
  102.     "intb",
  103.     "bpsw",
  104.     "bpc",
  105.     "fintv",
  106.     "fpsw",
  107.     "acc"
  108.   };

  109.   return reg_names[regnr];
  110. }

  111. /* Implement the "register_type" gdbarch method.  */
  112. static struct type *
  113. rx_register_type (struct gdbarch *gdbarch, int reg_nr)
  114. {
  115.   if (reg_nr == RX_PC_REGNUM)
  116.     return builtin_type (gdbarch)->builtin_func_ptr;
  117.   else if (reg_nr == RX_ACC_REGNUM)
  118.     return builtin_type (gdbarch)->builtin_unsigned_long_long;
  119.   else
  120.     return builtin_type (gdbarch)->builtin_unsigned_long;
  121. }


  122. /* Function for finding saved registers in a 'struct pv_area'; this
  123.    function is passed to pv_area_scan.

  124.    If VALUE is a saved register, ADDR says it was saved at a constant
  125.    offset from the frame base, and SIZE indicates that the whole
  126.    register was saved, record its offset.  */
  127. static void
  128. check_for_saved (void *result_untyped, pv_t addr, CORE_ADDR size, pv_t value)
  129. {
  130.   struct rx_prologue *result = (struct rx_prologue *) result_untyped;

  131.   if (value.kind == pvk_register
  132.       && value.k == 0
  133.       && pv_is_register (addr, RX_SP_REGNUM)
  134.       && size == register_size (target_gdbarch (), value.reg))
  135.     result->reg_offset[value.reg] = addr.k;
  136. }

  137. /* Define a "handle" struct for fetching the next opcode.  */
  138. struct rx_get_opcode_byte_handle
  139. {
  140.   CORE_ADDR pc;
  141. };

  142. /* Fetch a byte on behalf of the opcode decoder.  HANDLE contains
  143.    the memory address of the next byte to fetch.  If successful,
  144.    the address in the handle is updated and the byte fetched is
  145.    returned as the value of the function.  If not successful, -1
  146.    is returned.  */
  147. static int
  148. rx_get_opcode_byte (void *handle)
  149. {
  150.   struct rx_get_opcode_byte_handle *opcdata = handle;
  151.   int status;
  152.   gdb_byte byte;

  153.   status = target_read_memory (opcdata->pc, &byte, 1);
  154.   if (status == 0)
  155.     {
  156.       opcdata->pc += 1;
  157.       return byte;
  158.     }
  159.   else
  160.     return -1;
  161. }

  162. /* Analyze a prologue starting at START_PC, going no further than
  163.    LIMIT_PC.  Fill in RESULT as appropriate.  */
  164. static void
  165. rx_analyze_prologue (CORE_ADDR start_pc,
  166.                      CORE_ADDR limit_pc, struct rx_prologue *result)
  167. {
  168.   CORE_ADDR pc, next_pc;
  169.   int rn;
  170.   pv_t reg[RX_NUM_REGS];
  171.   struct pv_area *stack;
  172.   struct cleanup *back_to;
  173.   CORE_ADDR after_last_frame_setup_insn = start_pc;

  174.   memset (result, 0, sizeof (*result));

  175.   for (rn = 0; rn < RX_NUM_REGS; rn++)
  176.     {
  177.       reg[rn] = pv_register (rn, 0);
  178.       result->reg_offset[rn] = 1;
  179.     }

  180.   stack = make_pv_area (RX_SP_REGNUM, gdbarch_addr_bit (target_gdbarch ()));
  181.   back_to = make_cleanup_free_pv_area (stack);

  182.   /* The call instruction has saved the return address on the stack.  */
  183.   reg[RX_SP_REGNUM] = pv_add_constant (reg[RX_SP_REGNUM], -4);
  184.   pv_area_store (stack, reg[RX_SP_REGNUM], 4, reg[RX_PC_REGNUM]);

  185.   pc = start_pc;
  186.   while (pc < limit_pc)
  187.     {
  188.       int bytes_read;
  189.       struct rx_get_opcode_byte_handle opcode_handle;
  190.       RX_Opcode_Decoded opc;

  191.       opcode_handle.pc = pc;
  192.       bytes_read = rx_decode_opcode (pc, &opc, rx_get_opcode_byte,
  193.                                      &opcode_handle);
  194.       next_pc = pc + bytes_read;

  195.       if (opc.id == RXO_pushm        /* pushm r1, r2 */
  196.           && opc.op[1].type == RX_Operand_Register
  197.           && opc.op[2].type == RX_Operand_Register)
  198.         {
  199.           int r1, r2;
  200.           int r;

  201.           r1 = opc.op[1].reg;
  202.           r2 = opc.op[2].reg;
  203.           for (r = r2; r >= r1; r--)
  204.             {
  205.               reg[RX_SP_REGNUM] = pv_add_constant (reg[RX_SP_REGNUM], -4);
  206.               pv_area_store (stack, reg[RX_SP_REGNUM], 4, reg[r]);
  207.             }
  208.           after_last_frame_setup_insn = next_pc;
  209.         }
  210.       else if (opc.id == RXO_mov        /* mov.l rdst, rsrc */
  211.                && opc.op[0].type == RX_Operand_Register
  212.                && opc.op[1].type == RX_Operand_Register
  213.                && opc.size == RX_Long)
  214.         {
  215.           int rdst, rsrc;

  216.           rdst = opc.op[0].reg;
  217.           rsrc = opc.op[1].reg;
  218.           reg[rdst] = reg[rsrc];
  219.           if (rdst == RX_FP_REGNUM && rsrc == RX_SP_REGNUM)
  220.             after_last_frame_setup_insn = next_pc;
  221.         }
  222.       else if (opc.id == RXO_mov        /* mov.l rsrc, [-SP] */
  223.                && opc.op[0].type == RX_Operand_Predec
  224.                && opc.op[0].reg == RX_SP_REGNUM
  225.                && opc.op[1].type == RX_Operand_Register
  226.                && opc.size == RX_Long)
  227.         {
  228.           int rsrc;

  229.           rsrc = opc.op[1].reg;
  230.           reg[RX_SP_REGNUM] = pv_add_constant (reg[RX_SP_REGNUM], -4);
  231.           pv_area_store (stack, reg[RX_SP_REGNUM], 4, reg[rsrc]);
  232.           after_last_frame_setup_insn = next_pc;
  233.         }
  234.       else if (opc.id == RXO_add        /* add #const, rsrc, rdst */
  235.                && opc.op[0].type == RX_Operand_Register
  236.                && opc.op[1].type == RX_Operand_Immediate
  237.                && opc.op[2].type == RX_Operand_Register)
  238.         {
  239.           int rdst = opc.op[0].reg;
  240.           int addend = opc.op[1].addend;
  241.           int rsrc = opc.op[2].reg;
  242.           reg[rdst] = pv_add_constant (reg[rsrc], addend);
  243.           /* Negative adjustments to the stack pointer or frame pointer
  244.              are (most likely) part of the prologue.  */
  245.           if ((rdst == RX_SP_REGNUM || rdst == RX_FP_REGNUM) && addend < 0)
  246.             after_last_frame_setup_insn = next_pc;
  247.         }
  248.       else if (opc.id == RXO_mov
  249.                && opc.op[0].type == RX_Operand_Indirect
  250.                && opc.op[1].type == RX_Operand_Register
  251.                && opc.size == RX_Long
  252.                && (opc.op[0].reg == RX_SP_REGNUM
  253.                    || opc.op[0].reg == RX_FP_REGNUM)
  254.                && (RX_R1_REGNUM <= opc.op[1].reg
  255.                    && opc.op[1].reg <= RX_R4_REGNUM))
  256.         {
  257.           /* This moves an argument register to the stack.  Don't
  258.              record it, but allow it to be a part of the prologue.  */
  259.         }
  260.       else if (opc.id == RXO_branch
  261.                && opc.op[0].type == RX_Operand_Immediate
  262.                && next_pc < opc.op[0].addend)
  263.         {
  264.           /* When a loop appears as the first statement of a function
  265.              body, gcc 4.x will use a BRA instruction to branch to the
  266.              loop condition checking code.  This BRA instruction is
  267.              marked as part of the prologue.  We therefore set next_pc
  268.              to this branch target and also stop the prologue scan.
  269.              The instructions at and beyond the branch target should
  270.              no longer be associated with the prologue.

  271.              Note that we only consider forward branches here.  We
  272.              presume that a forward branch is being used to skip over
  273.              a loop body.

  274.              A backwards branch is covered by the default case below.
  275.              If we were to encounter a backwards branch, that would
  276.              most likely mean that we've scanned through a loop body.
  277.              We definitely want to stop the prologue scan when this
  278.              happens and that is precisely what is done by the default
  279.              case below.  */

  280.           after_last_frame_setup_insn = opc.op[0].addend;
  281.           break;                /* Scan no further if we hit this case.  */
  282.         }
  283.       else
  284.         {
  285.           /* Terminate the prologue scan.  */
  286.           break;
  287.         }

  288.       pc = next_pc;
  289.     }

  290.   /* Is the frame size (offset, really) a known constant?  */
  291.   if (pv_is_register (reg[RX_SP_REGNUM], RX_SP_REGNUM))
  292.     result->frame_size = reg[RX_SP_REGNUM].k;

  293.   /* Was the frame pointer initialized?  */
  294.   if (pv_is_register (reg[RX_FP_REGNUM], RX_SP_REGNUM))
  295.     {
  296.       result->has_frame_ptr = 1;
  297.       result->frame_ptr_offset = reg[RX_FP_REGNUM].k;
  298.     }

  299.   /* Record where all the registers were saved.  */
  300.   pv_area_scan (stack, check_for_saved, (void *) result);

  301.   result->prologue_end = after_last_frame_setup_insn;

  302.   do_cleanups (back_to);
  303. }


  304. /* Implement the "skip_prologue" gdbarch method.  */
  305. static CORE_ADDR
  306. rx_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR pc)
  307. {
  308.   const char *name;
  309.   CORE_ADDR func_addr, func_end;
  310.   struct rx_prologue p;

  311.   /* Try to find the extent of the function that contains PC.  */
  312.   if (!find_pc_partial_function (pc, &name, &func_addr, &func_end))
  313.     return pc;

  314.   rx_analyze_prologue (pc, func_end, &p);
  315.   return p.prologue_end;
  316. }

  317. /* Given a frame described by THIS_FRAME, decode the prologue of its
  318.    associated function if there is not cache entry as specified by
  319.    THIS_PROLOGUE_CACHE.  Save the decoded prologue in the cache and
  320.    return that struct as the value of this function.  */
  321. static struct rx_prologue *
  322. rx_analyze_frame_prologue (struct frame_info *this_frame,
  323.                            void **this_prologue_cache)
  324. {
  325.   if (!*this_prologue_cache)
  326.     {
  327.       CORE_ADDR func_start, stop_addr;

  328.       *this_prologue_cache = FRAME_OBSTACK_ZALLOC (struct rx_prologue);

  329.       func_start = get_frame_func (this_frame);
  330.       stop_addr = get_frame_pc (this_frame);

  331.       /* If we couldn't find any function containing the PC, then
  332.          just initialize the prologue cache, but don't do anything.  */
  333.       if (!func_start)
  334.         stop_addr = func_start;

  335.       rx_analyze_prologue (func_start, stop_addr, *this_prologue_cache);
  336.     }

  337.   return *this_prologue_cache;
  338. }

  339. /* Given the next frame and a prologue cache, return this frame's
  340.    base.  */
  341. static CORE_ADDR
  342. rx_frame_base (struct frame_info *this_frame, void **this_prologue_cache)
  343. {
  344.   struct rx_prologue *p
  345.     = rx_analyze_frame_prologue (this_frame, this_prologue_cache);

  346.   /* In functions that use alloca, the distance between the stack
  347.      pointer and the frame base varies dynamically, so we can't use
  348.      the SP plus static information like prologue analysis to find the
  349.      frame base.  However, such functions must have a frame pointer,
  350.      to be able to restore the SP on exit.  So whenever we do have a
  351.      frame pointer, use that to find the base.  */
  352.   if (p->has_frame_ptr)
  353.     {
  354.       CORE_ADDR fp = get_frame_register_unsigned (this_frame, RX_FP_REGNUM);
  355.       return fp - p->frame_ptr_offset;
  356.     }
  357.   else
  358.     {
  359.       CORE_ADDR sp = get_frame_register_unsigned (this_frame, RX_SP_REGNUM);
  360.       return sp - p->frame_size;
  361.     }
  362. }

  363. /* Implement the "frame_this_id" method for unwinding frames.  */
  364. static void
  365. rx_frame_this_id (struct frame_info *this_frame,
  366.                   void **this_prologue_cache, struct frame_id *this_id)
  367. {
  368.   *this_id = frame_id_build (rx_frame_base (this_frame, this_prologue_cache),
  369.                              get_frame_func (this_frame));
  370. }

  371. /* Implement the "frame_prev_register" method for unwinding frames.  */
  372. static struct value *
  373. rx_frame_prev_register (struct frame_info *this_frame,
  374.                         void **this_prologue_cache, int regnum)
  375. {
  376.   struct rx_prologue *p
  377.     = rx_analyze_frame_prologue (this_frame, this_prologue_cache);
  378.   CORE_ADDR frame_base = rx_frame_base (this_frame, this_prologue_cache);
  379.   int reg_size = register_size (get_frame_arch (this_frame), regnum);

  380.   if (regnum == RX_SP_REGNUM)
  381.     return frame_unwind_got_constant (this_frame, regnum, frame_base);

  382.   /* If prologue analysis says we saved this register somewhere,
  383.      return a description of the stack slot holding it.  */
  384.   else if (p->reg_offset[regnum] != 1)
  385.     return frame_unwind_got_memory (this_frame, regnum,
  386.                                     frame_base + p->reg_offset[regnum]);

  387.   /* Otherwise, presume we haven't changed the value of this
  388.      register, and get it from the next frame.  */
  389.   else
  390.     return frame_unwind_got_register (this_frame, regnum, regnum);
  391. }

  392. static const struct frame_unwind rx_frame_unwind = {
  393.   NORMAL_FRAME,
  394.   default_frame_unwind_stop_reason,
  395.   rx_frame_this_id,
  396.   rx_frame_prev_register,
  397.   NULL,
  398.   default_frame_sniffer
  399. };

  400. /* Implement the "unwind_pc" gdbarch method.  */
  401. static CORE_ADDR
  402. rx_unwind_pc (struct gdbarch *gdbarch, struct frame_info *this_frame)
  403. {
  404.   ULONGEST pc;

  405.   pc = frame_unwind_register_unsigned (this_frame, RX_PC_REGNUM);
  406.   return pc;
  407. }

  408. /* Implement the "unwind_sp" gdbarch method.  */
  409. static CORE_ADDR
  410. rx_unwind_sp (struct gdbarch *gdbarch, struct frame_info *this_frame)
  411. {
  412.   ULONGEST sp;

  413.   sp = frame_unwind_register_unsigned (this_frame, RX_SP_REGNUM);
  414.   return sp;
  415. }

  416. /* Implement the "dummy_id" gdbarch method.  */
  417. static struct frame_id
  418. rx_dummy_id (struct gdbarch *gdbarch, struct frame_info *this_frame)
  419. {
  420.   return
  421.     frame_id_build (get_frame_register_unsigned (this_frame, RX_SP_REGNUM),
  422.                     get_frame_pc (this_frame));
  423. }

  424. /* Implement the "push_dummy_call" gdbarch method.  */
  425. static CORE_ADDR
  426. rx_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
  427.                     struct regcache *regcache, CORE_ADDR bp_addr, int nargs,
  428.                     struct value **args, CORE_ADDR sp, int struct_return,
  429.                     CORE_ADDR struct_addr)
  430. {
  431.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  432.   int write_pass;
  433.   int sp_off = 0;
  434.   CORE_ADDR cfa;
  435.   int num_register_candidate_args;

  436.   struct type *func_type = value_type (function);

  437.   /* Dereference function pointer types.  */
  438.   while (TYPE_CODE (func_type) == TYPE_CODE_PTR)
  439.     func_type = TYPE_TARGET_TYPE (func_type);

  440.   /* The end result had better be a function or a method.  */
  441.   gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC
  442.               || TYPE_CODE (func_type) == TYPE_CODE_METHOD);

  443.   /* Functions with a variable number of arguments have all of their
  444.      variable arguments and the last non-variable argument passed
  445.      on the stack.

  446.      Otherwise, we can pass up to four arguments on the stack.

  447.      Once computed, we leave this value alone.  I.e. we don't update
  448.      it in case of a struct return going in a register or an argument
  449.      requiring multiple registers, etc.  We rely instead on the value
  450.      of the ``arg_reg'' variable to get these other details correct.  */

  451.   if (TYPE_VARARGS (func_type))
  452.     num_register_candidate_args = TYPE_NFIELDS (func_type) - 1;
  453.   else
  454.     num_register_candidate_args = 4;

  455.   /* We make two passes; the first does the stack allocation,
  456.      the second actually stores the arguments.  */
  457.   for (write_pass = 0; write_pass <= 1; write_pass++)
  458.     {
  459.       int i;
  460.       int arg_reg = RX_R1_REGNUM;

  461.       if (write_pass)
  462.         sp = align_down (sp - sp_off, 4);
  463.       sp_off = 0;

  464.       if (struct_return)
  465.         {
  466.           struct type *return_type = TYPE_TARGET_TYPE (func_type);

  467.           gdb_assert (TYPE_CODE (return_type) == TYPE_CODE_STRUCT
  468.                       || TYPE_CODE (func_type) == TYPE_CODE_UNION);

  469.           if (TYPE_LENGTH (return_type) > 16
  470.               || TYPE_LENGTH (return_type) % 4 != 0)
  471.             {
  472.               if (write_pass)
  473.                 regcache_cooked_write_unsigned (regcache, RX_R15_REGNUM,
  474.                                                 struct_addr);
  475.             }
  476.         }

  477.       /* Push the arguments.  */
  478.       for (i = 0; i < nargs; i++)
  479.         {
  480.           struct value *arg = args[i];
  481.           const gdb_byte *arg_bits = value_contents_all (arg);
  482.           struct type *arg_type = check_typedef (value_type (arg));
  483.           ULONGEST arg_size = TYPE_LENGTH (arg_type);

  484.           if (i == 0 && struct_addr != 0 && !struct_return
  485.               && TYPE_CODE (arg_type) == TYPE_CODE_PTR
  486.               && extract_unsigned_integer (arg_bits, 4,
  487.                                            byte_order) == struct_addr)
  488.             {
  489.               /* This argument represents the address at which C++ (and
  490.                  possibly other languages) store their return value.
  491.                  Put this value in R15.  */
  492.               if (write_pass)
  493.                 regcache_cooked_write_unsigned (regcache, RX_R15_REGNUM,
  494.                                                 struct_addr);
  495.             }
  496.           else if (TYPE_CODE (arg_type) != TYPE_CODE_STRUCT
  497.                    && TYPE_CODE (arg_type) != TYPE_CODE_UNION)
  498.             {
  499.               /* Argument is a scalar.  */
  500.               if (arg_size == 8)
  501.                 {
  502.                   if (i < num_register_candidate_args
  503.                       && arg_reg <= RX_R4_REGNUM - 1)
  504.                     {
  505.                       /* If argument registers are going to be used to pass
  506.                          an 8 byte scalar, the ABI specifies that two registers
  507.                          must be available.  */
  508.                       if (write_pass)
  509.                         {
  510.                           regcache_cooked_write_unsigned (regcache, arg_reg,
  511.                                                           extract_unsigned_integer
  512.                                                           (arg_bits, 4,
  513.                                                            byte_order));
  514.                           regcache_cooked_write_unsigned (regcache,
  515.                                                           arg_reg + 1,
  516.                                                           extract_unsigned_integer
  517.                                                           (arg_bits + 4, 4,
  518.                                                            byte_order));
  519.                         }
  520.                       arg_reg += 2;
  521.                     }
  522.                   else
  523.                     {
  524.                       sp_off = align_up (sp_off, 4);
  525.                       /* Otherwise, pass the 8 byte scalar on the stack.  */
  526.                       if (write_pass)
  527.                         write_memory (sp + sp_off, arg_bits, 8);
  528.                       sp_off += 8;
  529.                     }
  530.                 }
  531.               else
  532.                 {
  533.                   ULONGEST u;

  534.                   gdb_assert (arg_size <= 4);

  535.                   u =
  536.                     extract_unsigned_integer (arg_bits, arg_size, byte_order);

  537.                   if (i < num_register_candidate_args
  538.                       && arg_reg <= RX_R4_REGNUM)
  539.                     {
  540.                       if (write_pass)
  541.                         regcache_cooked_write_unsigned (regcache, arg_reg, u);
  542.                       arg_reg += 1;
  543.                     }
  544.                   else
  545.                     {
  546.                       int p_arg_size = 4;

  547.                       if (TYPE_PROTOTYPED (func_type)
  548.                           && i < TYPE_NFIELDS (func_type))
  549.                         {
  550.                           struct type *p_arg_type =
  551.                             TYPE_FIELD_TYPE (func_type, i);
  552.                           p_arg_size = TYPE_LENGTH (p_arg_type);
  553.                         }

  554.                       sp_off = align_up (sp_off, p_arg_size);

  555.                       if (write_pass)
  556.                         write_memory_unsigned_integer (sp + sp_off,
  557.                                                        p_arg_size, byte_order,
  558.                                                        u);
  559.                       sp_off += p_arg_size;
  560.                     }
  561.                 }
  562.             }
  563.           else
  564.             {
  565.               /* Argument is a struct or union.  Pass as much of the struct
  566.                  in registers, if possible.  Pass the rest on the stack.  */
  567.               while (arg_size > 0)
  568.                 {
  569.                   if (i < num_register_candidate_args
  570.                       && arg_reg <= RX_R4_REGNUM
  571.                       && arg_size <= 4 * (RX_R4_REGNUM - arg_reg + 1)
  572.                       && arg_size % 4 == 0)
  573.                     {
  574.                       int len = min (arg_size, 4);

  575.                       if (write_pass)
  576.                         regcache_cooked_write_unsigned (regcache, arg_reg,
  577.                                                         extract_unsigned_integer
  578.                                                         (arg_bits, len,
  579.                                                          byte_order));
  580.                       arg_bits += len;
  581.                       arg_size -= len;
  582.                       arg_reg++;
  583.                     }
  584.                   else
  585.                     {
  586.                       sp_off = align_up (sp_off, 4);
  587.                       if (write_pass)
  588.                         write_memory (sp + sp_off, arg_bits, arg_size);
  589.                       sp_off += align_up (arg_size, 4);
  590.                       arg_size = 0;
  591.                     }
  592.                 }
  593.             }
  594.         }
  595.     }

  596.   /* Keep track of the stack address prior to pushing the return address.
  597.      This is the value that we'll return.  */
  598.   cfa = sp;

  599.   /* Push the return address.  */
  600.   sp = sp - 4;
  601.   write_memory_unsigned_integer (sp, 4, byte_order, bp_addr);

  602.   /* Update the stack pointer.  */
  603.   regcache_cooked_write_unsigned (regcache, RX_SP_REGNUM, sp);

  604.   return cfa;
  605. }

  606. /* Implement the "return_value" gdbarch method.  */
  607. static enum return_value_convention
  608. rx_return_value (struct gdbarch *gdbarch,
  609.                  struct value *function,
  610.                  struct type *valtype,
  611.                  struct regcache *regcache,
  612.                  gdb_byte *readbuf, const gdb_byte *writebuf)
  613. {
  614.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  615.   ULONGEST valtype_len = TYPE_LENGTH (valtype);

  616.   if (TYPE_LENGTH (valtype) > 16
  617.       || ((TYPE_CODE (valtype) == TYPE_CODE_STRUCT
  618.            || TYPE_CODE (valtype) == TYPE_CODE_UNION)
  619.           && TYPE_LENGTH (valtype) % 4 != 0))
  620.     return RETURN_VALUE_STRUCT_CONVENTION;

  621.   if (readbuf)
  622.     {
  623.       ULONGEST u;
  624.       int argreg = RX_R1_REGNUM;
  625.       int offset = 0;

  626.       while (valtype_len > 0)
  627.         {
  628.           int len = min (valtype_len, 4);

  629.           regcache_cooked_read_unsigned (regcache, argreg, &u);
  630.           store_unsigned_integer (readbuf + offset, len, byte_order, u);
  631.           valtype_len -= len;
  632.           offset += len;
  633.           argreg++;
  634.         }
  635.     }

  636.   if (writebuf)
  637.     {
  638.       ULONGEST u;
  639.       int argreg = RX_R1_REGNUM;
  640.       int offset = 0;

  641.       while (valtype_len > 0)
  642.         {
  643.           int len = min (valtype_len, 4);

  644.           u = extract_unsigned_integer (writebuf + offset, len, byte_order);
  645.           regcache_cooked_write_unsigned (regcache, argreg, u);
  646.           valtype_len -= len;
  647.           offset += len;
  648.           argreg++;
  649.         }
  650.     }

  651.   return RETURN_VALUE_REGISTER_CONVENTION;
  652. }

  653. /* Implement the "breakpoint_from_pc" gdbarch method.  */
  654. static const gdb_byte *
  655. rx_breakpoint_from_pc (struct gdbarch *gdbarch, CORE_ADDR *pcptr, int *lenptr)
  656. {
  657.   static gdb_byte breakpoint[] = { 0x00 };
  658.   *lenptr = sizeof breakpoint;
  659.   return breakpoint;
  660. }

  661. /* Allocate and initialize a gdbarch object.  */
  662. static struct gdbarch *
  663. rx_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
  664. {
  665.   struct gdbarch *gdbarch;
  666.   struct gdbarch_tdep *tdep;
  667.   int elf_flags;

  668.   /* Extract the elf_flags if available.  */
  669.   if (info.abfd != NULL
  670.       && bfd_get_flavour (info.abfd) == bfd_target_elf_flavour)
  671.     elf_flags = elf_elfheader (info.abfd)->e_flags;
  672.   else
  673.     elf_flags = 0;


  674.   /* Try to find the architecture in the list of already defined
  675.      architectures.  */
  676.   for (arches = gdbarch_list_lookup_by_info (arches, &info);
  677.        arches != NULL;
  678.        arches = gdbarch_list_lookup_by_info (arches->next, &info))
  679.     {
  680.       if (gdbarch_tdep (arches->gdbarch)->elf_flags != elf_flags)
  681.         continue;

  682.       return arches->gdbarch;
  683.     }

  684.   /* None found, create a new architecture from the information
  685.      provided.  */
  686.   tdep = (struct gdbarch_tdep *) xmalloc (sizeof (struct gdbarch_tdep));
  687.   gdbarch = gdbarch_alloc (&info, tdep);
  688.   tdep->elf_flags = elf_flags;

  689.   set_gdbarch_num_regs (gdbarch, RX_NUM_REGS);
  690.   set_gdbarch_num_pseudo_regs (gdbarch, 0);
  691.   set_gdbarch_register_name (gdbarch, rx_register_name);
  692.   set_gdbarch_register_type (gdbarch, rx_register_type);
  693.   set_gdbarch_pc_regnum (gdbarch, RX_PC_REGNUM);
  694.   set_gdbarch_sp_regnum (gdbarch, RX_SP_REGNUM);
  695.   set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
  696.   set_gdbarch_decr_pc_after_break (gdbarch, 1);
  697.   set_gdbarch_breakpoint_from_pc (gdbarch, rx_breakpoint_from_pc);
  698.   set_gdbarch_skip_prologue (gdbarch, rx_skip_prologue);

  699.   set_gdbarch_print_insn (gdbarch, print_insn_rx);

  700.   set_gdbarch_unwind_pc (gdbarch, rx_unwind_pc);
  701.   set_gdbarch_unwind_sp (gdbarch, rx_unwind_sp);

  702.   /* Target builtin data types.  */
  703.   set_gdbarch_char_signed (gdbarch, 0);
  704.   set_gdbarch_short_bit (gdbarch, 16);
  705.   set_gdbarch_int_bit (gdbarch, 32);
  706.   set_gdbarch_long_bit (gdbarch, 32);
  707.   set_gdbarch_long_long_bit (gdbarch, 64);
  708.   set_gdbarch_ptr_bit (gdbarch, 32);
  709.   set_gdbarch_float_bit (gdbarch, 32);
  710.   set_gdbarch_float_format (gdbarch, floatformats_ieee_single);
  711.   if (elf_flags & E_FLAG_RX_64BIT_DOUBLES)
  712.     {
  713.       set_gdbarch_double_bit (gdbarch, 64);
  714.       set_gdbarch_long_double_bit (gdbarch, 64);
  715.       set_gdbarch_double_format (gdbarch, floatformats_ieee_double);
  716.       set_gdbarch_long_double_format (gdbarch, floatformats_ieee_double);
  717.     }
  718.   else
  719.     {
  720.       set_gdbarch_double_bit (gdbarch, 32);
  721.       set_gdbarch_long_double_bit (gdbarch, 32);
  722.       set_gdbarch_double_format (gdbarch, floatformats_ieee_single);
  723.       set_gdbarch_long_double_format (gdbarch, floatformats_ieee_single);
  724.     }

  725.   /* Frame unwinding.  */
  726. #if 0
  727.   /* Note: The test results are better with the dwarf2 unwinder disabled,
  728.      so it's turned off for now.  */
  729.   dwarf2_append_unwinders (gdbarch);
  730. #endif
  731.   frame_unwind_append_unwinder (gdbarch, &rx_frame_unwind);

  732.   /* Methods for saving / extracting a dummy frame's ID.
  733.      The ID's stack address must match the SP value returned by
  734.      PUSH_DUMMY_CALL, and saved by generic_save_dummy_frame_tos.  */
  735.   set_gdbarch_dummy_id (gdbarch, rx_dummy_id);
  736.   set_gdbarch_push_dummy_call (gdbarch, rx_push_dummy_call);
  737.   set_gdbarch_return_value (gdbarch, rx_return_value);

  738.   /* Virtual tables.  */
  739.   set_gdbarch_vbit_in_delta (gdbarch, 1);

  740.   return gdbarch;
  741. }

  742. /* -Wmissing-prototypes */
  743. extern initialize_file_ftype _initialize_rx_tdep;

  744. /* Register the above initialization routine.  */

  745. void
  746. _initialize_rx_tdep (void)
  747. {
  748.   register_gdbarch_init (bfd_arch_rx, rx_gdbarch_init);
  749. }