gdb/arm-tdep.c - gdb

Global variables defined

Data types defined

Functions defined

Macros defined

Source code

  1. /* Common target dependent code for GDB on ARM systems.

  2.    Copyright (C) 1988-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 <ctype.h>                /* XXX for isupper ().  */

  16. #include "frame.h"
  17. #include "inferior.h"
  18. #include "infrun.h"
  19. #include "gdbcmd.h"
  20. #include "gdbcore.h"
  21. #include "dis-asm.h"                /* For register styles.  */
  22. #include "regcache.h"
  23. #include "reggroups.h"
  24. #include "doublest.h"
  25. #include "value.h"
  26. #include "arch-utils.h"
  27. #include "osabi.h"
  28. #include "frame-unwind.h"
  29. #include "frame-base.h"
  30. #include "trad-frame.h"
  31. #include "objfiles.h"
  32. #include "dwarf2-frame.h"
  33. #include "gdbtypes.h"
  34. #include "prologue-value.h"
  35. #include "remote.h"
  36. #include "target-descriptions.h"
  37. #include "user-regs.h"
  38. #include "observer.h"

  39. #include "arm-tdep.h"
  40. #include "gdb/sim-arm.h"

  41. #include "elf-bfd.h"
  42. #include "coff/internal.h"
  43. #include "elf/arm.h"

  44. #include "vec.h"

  45. #include "record.h"
  46. #include "record-full.h"

  47. #include "features/arm-with-m.c"
  48. #include "features/arm-with-m-fpa-layout.c"
  49. #include "features/arm-with-m-vfp-d16.c"
  50. #include "features/arm-with-iwmmxt.c"
  51. #include "features/arm-with-vfpv2.c"
  52. #include "features/arm-with-vfpv3.c"
  53. #include "features/arm-with-neon.c"

  54. static int arm_debug;

  55. /* Macros for setting and testing a bit in a minimal symbol that marks
  56.    it as Thumb function.  The MSB of the minimal symbol's "info" field
  57.    is used for this purpose.

  58.    MSYMBOL_SET_SPECIAL        Actually sets the "special" bit.
  59.    MSYMBOL_IS_SPECIAL   Tests the "special" bit in a minimal symbol.  */

  60. #define MSYMBOL_SET_SPECIAL(msym)                                \
  61.         MSYMBOL_TARGET_FLAG_1 (msym) = 1

  62. #define MSYMBOL_IS_SPECIAL(msym)                                \
  63.         MSYMBOL_TARGET_FLAG_1 (msym)

  64. /* Per-objfile data used for mapping symbols.  */
  65. static const struct objfile_data *arm_objfile_data_key;

  66. struct arm_mapping_symbol
  67. {
  68.   bfd_vma value;
  69.   char type;
  70. };
  71. typedef struct arm_mapping_symbol arm_mapping_symbol_s;
  72. DEF_VEC_O(arm_mapping_symbol_s);

  73. struct arm_per_objfile
  74. {
  75.   VEC(arm_mapping_symbol_s) **section_maps;
  76. };

  77. /* The list of available "set arm ..." and "show arm ..." commands.  */
  78. static struct cmd_list_element *setarmcmdlist = NULL;
  79. static struct cmd_list_element *showarmcmdlist = NULL;

  80. /* The type of floating-point to use.  Keep this in sync with enum
  81.    arm_float_model, and the help string in _initialize_arm_tdep.  */
  82. static const char *const fp_model_strings[] =
  83. {
  84.   "auto",
  85.   "softfpa",
  86.   "fpa",
  87.   "softvfp",
  88.   "vfp",
  89.   NULL
  90. };

  91. /* A variable that can be configured by the user.  */
  92. static enum arm_float_model arm_fp_model = ARM_FLOAT_AUTO;
  93. static const char *current_fp_model = "auto";

  94. /* The ABI to use.  Keep this in sync with arm_abi_kind.  */
  95. static const char *const arm_abi_strings[] =
  96. {
  97.   "auto",
  98.   "APCS",
  99.   "AAPCS",
  100.   NULL
  101. };

  102. /* A variable that can be configured by the user.  */
  103. static enum arm_abi_kind arm_abi_global = ARM_ABI_AUTO;
  104. static const char *arm_abi_string = "auto";

  105. /* The execution mode to assume.  */
  106. static const char *const arm_mode_strings[] =
  107.   {
  108.     "auto",
  109.     "arm",
  110.     "thumb",
  111.     NULL
  112.   };

  113. static const char *arm_fallback_mode_string = "auto";
  114. static const char *arm_force_mode_string = "auto";

  115. /* Internal override of the execution mode.  -1 means no override,
  116.    0 means override to ARM mode, 1 means override to Thumb mode.
  117.    The effect is the same as if arm_force_mode has been set by the
  118.    user (except the internal override has precedence over a user's
  119.    arm_force_mode override).  */
  120. static int arm_override_mode = -1;

  121. /* Number of different reg name sets (options).  */
  122. static int num_disassembly_options;

  123. /* The standard register names, and all the valid aliases for them.  Note
  124.    that `fp', `sp' and `pc' are not added in this alias list, because they
  125.    have been added as builtin user registers in
  126.    std-regs.c:_initialize_frame_reg.  */
  127. static const struct
  128. {
  129.   const char *name;
  130.   int regnum;
  131. } arm_register_aliases[] = {
  132.   /* Basic register numbers.  */
  133.   { "r0", 0 },
  134.   { "r1", 1 },
  135.   { "r2", 2 },
  136.   { "r3", 3 },
  137.   { "r4", 4 },
  138.   { "r5", 5 },
  139.   { "r6", 6 },
  140.   { "r7", 7 },
  141.   { "r8", 8 },
  142.   { "r9", 9 },
  143.   { "r10", 10 },
  144.   { "r11", 11 },
  145.   { "r12", 12 },
  146.   { "r13", 13 },
  147.   { "r14", 14 },
  148.   { "r15", 15 },
  149.   /* Synonyms (argument and variable registers).  */
  150.   { "a1", 0 },
  151.   { "a2", 1 },
  152.   { "a3", 2 },
  153.   { "a4", 3 },
  154.   { "v1", 4 },
  155.   { "v2", 5 },
  156.   { "v3", 6 },
  157.   { "v4", 7 },
  158.   { "v5", 8 },
  159.   { "v6", 9 },
  160.   { "v7", 10 },
  161.   { "v8", 11 },
  162.   /* Other platform-specific names for r9.  */
  163.   { "sb", 9 },
  164.   { "tr", 9 },
  165.   /* Special names.  */
  166.   { "ip", 12 },
  167.   { "lr", 14 },
  168.   /* Names used by GCC (not listed in the ARM EABI).  */
  169.   { "sl", 10 },
  170.   /* A special name from the older ATPCS.  */
  171.   { "wr", 7 },
  172. };

  173. static const char *const arm_register_names[] =
  174. {"r0""r1""r2""r3",        /*  0  1  2  3 */
  175. "r4""r5""r6""r7",        /*  4  5  6  7 */
  176. "r8""r9""r10", "r11",        /*  8  9 10 11 */
  177. "r12", "sp""lr""pc",        /* 12 13 14 15 */
  178. "f0""f1""f2""f3",        /* 16 17 18 19 */
  179. "f4""f5""f6""f7",        /* 20 21 22 23 */
  180. "fps", "cpsr" };                /* 24 25       */

  181. /* Valid register name styles.  */
  182. static const char **valid_disassembly_styles;

  183. /* Disassembly style to use. Default to "std" register names.  */
  184. static const char *disassembly_style;

  185. /* This is used to keep the bfd arch_info in sync with the disassembly
  186.    style.  */
  187. static void set_disassembly_style_sfunc(char *, int,
  188.                                          struct cmd_list_element *);
  189. static void set_disassembly_style (void);

  190. static void convert_from_extended (const struct floatformat *, const void *,
  191.                                    void *, int);
  192. static void convert_to_extended (const struct floatformat *, void *,
  193.                                  const void *, int);

  194. static enum register_status arm_neon_quad_read (struct gdbarch *gdbarch,
  195.                                                 struct regcache *regcache,
  196.                                                 int regnum, gdb_byte *buf);
  197. static void arm_neon_quad_write (struct gdbarch *gdbarch,
  198.                                  struct regcache *regcache,
  199.                                  int regnum, const gdb_byte *buf);

  200. static int thumb_insn_size (unsigned short inst1);

  201. struct arm_prologue_cache
  202. {
  203.   /* The stack pointer at the time this frame was created; i.e. the
  204.      caller's stack pointer when this function was called.  It is used
  205.      to identify this frame.  */
  206.   CORE_ADDR prev_sp;

  207.   /* The frame base for this frame is just prev_sp - frame size.
  208.      FRAMESIZE is the distance from the frame pointer to the
  209.      initial stack pointer.  */

  210.   int framesize;

  211.   /* The register used to hold the frame pointer for this frame.  */
  212.   int framereg;

  213.   /* Saved register offsets.  */
  214.   struct trad_frame_saved_reg *saved_regs;
  215. };

  216. static CORE_ADDR arm_analyze_prologue (struct gdbarch *gdbarch,
  217.                                        CORE_ADDR prologue_start,
  218.                                        CORE_ADDR prologue_end,
  219.                                        struct arm_prologue_cache *cache);

  220. /* Architecture version for displaced stepping.  This effects the behaviour of
  221.    certain instructions, and really should not be hard-wired.  */

  222. #define DISPLACED_STEPPING_ARCH_VERSION                5

  223. /* Addresses for calling Thumb functions have the bit 0 set.
  224.    Here are some macros to test, set, or clear bit 0 of addresses.  */
  225. #define IS_THUMB_ADDR(addr)        ((addr) & 1)
  226. #define MAKE_THUMB_ADDR(addr)        ((addr) | 1)
  227. #define UNMAKE_THUMB_ADDR(addr) ((addr) & ~1)

  228. /* Set to true if the 32-bit mode is in use.  */

  229. int arm_apcs_32 = 1;

  230. /* Return the bit mask in ARM_PS_REGNUM that indicates Thumb mode.  */

  231. int
  232. arm_psr_thumb_bit (struct gdbarch *gdbarch)
  233. {
  234.   if (gdbarch_tdep (gdbarch)->is_m)
  235.     return XPSR_T;
  236.   else
  237.     return CPSR_T;
  238. }

  239. /* Determine if FRAME is executing in Thumb mode.  */

  240. int
  241. arm_frame_is_thumb (struct frame_info *frame)
  242. {
  243.   CORE_ADDR cpsr;
  244.   ULONGEST t_bit = arm_psr_thumb_bit (get_frame_arch (frame));

  245.   /* Every ARM frame unwinder can unwind the T bit of the CPSR, either
  246.      directly (from a signal frame or dummy frame) or by interpreting
  247.      the saved LR (from a prologue or DWARF frame).  So consult it and
  248.      trust the unwinders.  */
  249.   cpsr = get_frame_register_unsigned (frame, ARM_PS_REGNUM);

  250.   return (cpsr & t_bit) != 0;
  251. }

  252. /* Callback for VEC_lower_bound.  */

  253. static inline int
  254. arm_compare_mapping_symbols (const struct arm_mapping_symbol *lhs,
  255.                              const struct arm_mapping_symbol *rhs)
  256. {
  257.   return lhs->value < rhs->value;
  258. }

  259. /* Search for the mapping symbol covering MEMADDR.  If one is found,
  260.    return its type.  Otherwise, return 0.  If START is non-NULL,
  261.    set *START to the location of the mapping symbol.  */

  262. static char
  263. arm_find_mapping_symbol (CORE_ADDR memaddr, CORE_ADDR *start)
  264. {
  265.   struct obj_section *sec;

  266.   /* If there are mapping symbols, consult them.  */
  267.   sec = find_pc_section (memaddr);
  268.   if (sec != NULL)
  269.     {
  270.       struct arm_per_objfile *data;
  271.       VEC(arm_mapping_symbol_s) *map;
  272.       struct arm_mapping_symbol map_key = { memaddr - obj_section_addr (sec),
  273.                                             0 };
  274.       unsigned int idx;

  275.       data = objfile_data (sec->objfile, arm_objfile_data_key);
  276.       if (data != NULL)
  277.         {
  278.           map = data->section_maps[sec->the_bfd_section->index];
  279.           if (!VEC_empty (arm_mapping_symbol_s, map))
  280.             {
  281.               struct arm_mapping_symbol *map_sym;

  282.               idx = VEC_lower_bound (arm_mapping_symbol_s, map, &map_key,
  283.                                      arm_compare_mapping_symbols);

  284.               /* VEC_lower_bound finds the earliest ordered insertion
  285.                  point.  If the following symbol starts at this exact
  286.                  address, we use that; otherwise, the preceding
  287.                  mapping symbol covers this address.  */
  288.               if (idx < VEC_length (arm_mapping_symbol_s, map))
  289.                 {
  290.                   map_sym = VEC_index (arm_mapping_symbol_s, map, idx);
  291.                   if (map_sym->value == map_key.value)
  292.                     {
  293.                       if (start)
  294.                         *start = map_sym->value + obj_section_addr (sec);
  295.                       return map_sym->type;
  296.                     }
  297.                 }

  298.               if (idx > 0)
  299.                 {
  300.                   map_sym = VEC_index (arm_mapping_symbol_s, map, idx - 1);
  301.                   if (start)
  302.                     *start = map_sym->value + obj_section_addr (sec);
  303.                   return map_sym->type;
  304.                 }
  305.             }
  306.         }
  307.     }

  308.   return 0;
  309. }

  310. /* Determine if the program counter specified in MEMADDR is in a Thumb
  311.    function.  This function should be called for addresses unrelated to
  312.    any executing frame; otherwise, prefer arm_frame_is_thumb.  */

  313. int
  314. arm_pc_is_thumb (struct gdbarch *gdbarch, CORE_ADDR memaddr)
  315. {
  316.   struct bound_minimal_symbol sym;
  317.   char type;
  318.   struct displaced_step_closure* dsc
  319.     = get_displaced_step_closure_by_addr(memaddr);

  320.   /* If checking the mode of displaced instruction in copy area, the mode
  321.      should be determined by instruction on the original address.  */
  322.   if (dsc)
  323.     {
  324.       if (debug_displaced)
  325.         fprintf_unfiltered (gdb_stdlog,
  326.                             "displaced: check mode of %.8lx instead of %.8lx\n",
  327.                             (unsigned long) dsc->insn_addr,
  328.                             (unsigned long) memaddr);
  329.       memaddr = dsc->insn_addr;
  330.     }

  331.   /* If bit 0 of the address is set, assume this is a Thumb address.  */
  332.   if (IS_THUMB_ADDR (memaddr))
  333.     return 1;

  334.   /* Respect internal mode override if active.  */
  335.   if (arm_override_mode != -1)
  336.     return arm_override_mode;

  337.   /* If the user wants to override the symbol table, let him.  */
  338.   if (strcmp (arm_force_mode_string, "arm") == 0)
  339.     return 0;
  340.   if (strcmp (arm_force_mode_string, "thumb") == 0)
  341.     return 1;

  342.   /* ARM v6-M and v7-M are always in Thumb mode.  */
  343.   if (gdbarch_tdep (gdbarch)->is_m)
  344.     return 1;

  345.   /* If there are mapping symbols, consult them.  */
  346.   type = arm_find_mapping_symbol (memaddr, NULL);
  347.   if (type)
  348.     return type == 't';

  349.   /* Thumb functions have a "special" bit set in minimal symbols.  */
  350.   sym = lookup_minimal_symbol_by_pc (memaddr);
  351.   if (sym.minsym)
  352.     return (MSYMBOL_IS_SPECIAL (sym.minsym));

  353.   /* If the user wants to override the fallback mode, let them.  */
  354.   if (strcmp (arm_fallback_mode_string, "arm") == 0)
  355.     return 0;
  356.   if (strcmp (arm_fallback_mode_string, "thumb") == 0)
  357.     return 1;

  358.   /* If we couldn't find any symbol, but we're talking to a running
  359.      target, then trust the current value of $cpsr.  This lets
  360.      "display/i $pc" always show the correct mode (though if there is
  361.      a symbol table we will not reach here, so it still may not be
  362.      displayed in the mode it will be executed).  */
  363.   if (target_has_registers)
  364.     return arm_frame_is_thumb (get_current_frame ());

  365.   /* Otherwise we're out of luck; we assume ARM.  */
  366.   return 0;
  367. }

  368. /* Remove useless bits from addresses in a running program.  */
  369. static CORE_ADDR
  370. arm_addr_bits_remove (struct gdbarch *gdbarch, CORE_ADDR val)
  371. {
  372.   /* On M-profile devices, do not strip the low bit from EXC_RETURN
  373.      (the magic exception return address).  */
  374.   if (gdbarch_tdep (gdbarch)->is_m
  375.       && (val & 0xfffffff0) == 0xfffffff0)
  376.     return val;

  377.   if (arm_apcs_32)
  378.     return UNMAKE_THUMB_ADDR (val);
  379.   else
  380.     return (val & 0x03fffffc);
  381. }

  382. /* Return 1 if PC is the start of a compiler helper function which
  383.    can be safely ignored during prologue skipping.  IS_THUMB is true
  384.    if the function is known to be a Thumb function due to the way it
  385.    is being called.  */
  386. static int
  387. skip_prologue_function (struct gdbarch *gdbarch, CORE_ADDR pc, int is_thumb)
  388. {
  389.   enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
  390.   struct bound_minimal_symbol msym;

  391.   msym = lookup_minimal_symbol_by_pc (pc);
  392.   if (msym.minsym != NULL
  393.       && BMSYMBOL_VALUE_ADDRESS (msym) == pc
  394.       && MSYMBOL_LINKAGE_NAME (msym.minsym) != NULL)
  395.     {
  396.       const char *name = MSYMBOL_LINKAGE_NAME (msym.minsym);

  397.       /* The GNU linker's Thumb call stub to foo is named
  398.          __foo_from_thumb.  */
  399.       if (strstr (name, "_from_thumb") != NULL)
  400.         name += 2;

  401.       /* On soft-float targets, __truncdfsf2 is called to convert promoted
  402.          arguments to their argument types in non-prototyped
  403.          functions.  */
  404.       if (strncmp (name, "__truncdfsf2", strlen ("__truncdfsf2")) == 0)
  405.         return 1;
  406.       if (strncmp (name, "__aeabi_d2f", strlen ("__aeabi_d2f")) == 0)
  407.         return 1;

  408.       /* Internal functions related to thread-local storage.  */
  409.       if (strncmp (name, "__tls_get_addr", strlen ("__tls_get_addr")) == 0)
  410.         return 1;
  411.       if (strncmp (name, "__aeabi_read_tp", strlen ("__aeabi_read_tp")) == 0)
  412.         return 1;
  413.     }
  414.   else
  415.     {
  416.       /* If we run against a stripped glibc, we may be unable to identify
  417.          special functions by name.  Check for one important case,
  418.          __aeabi_read_tp, by comparing the *code* against the default
  419.          implementation (this is hand-written ARM assembler in glibc).  */

  420.       if (!is_thumb
  421.           && read_memory_unsigned_integer (pc, 4, byte_order_for_code)
  422.              == 0xe3e00a0f /* mov r0, #0xffff0fff */
  423.           && read_memory_unsigned_integer (pc + 4, 4, byte_order_for_code)
  424.              == 0xe240f01f) /* sub pc, r0, #31 */
  425.         return 1;
  426.     }

  427.   return 0;
  428. }

  429. /* Support routines for instruction parsing.  */
  430. #define submask(x) ((1L << ((x) + 1)) - 1)
  431. #define bit(obj,st) (((obj) >> (st)) & 1)
  432. #define bits(obj,st,fn) (((obj) >> (st)) & submask ((fn) - (st)))
  433. #define sbits(obj,st,fn) \
  434.   ((long) (bits(obj,st,fn) | ((long) bit(obj,fn) * ~ submask (fn - st))))
  435. #define BranchDest(addr,instr) \
  436.   ((CORE_ADDR) (((unsigned long) (addr)) + 8 + (sbits (instr, 0, 23) << 2)))

  437. /* Extract the immediate from instruction movw/movt of encoding T.  INSN1 is
  438.    the first 16-bit of instruction, and INSN2 is the second 16-bit of
  439.    instruction.  */
  440. #define EXTRACT_MOVW_MOVT_IMM_T(insn1, insn2) \
  441.   ((bits ((insn1), 0, 3) << 12)               \
  442.    | (bits ((insn1), 10, 10) << 11)           \
  443.    | (bits ((insn2), 12, 14) << 8)            \
  444.    | bits ((insn2), 0, 7))

  445. /* Extract the immediate from instruction movw/movt of encoding AINSN is
  446.    the 32-bit instruction.  */
  447. #define EXTRACT_MOVW_MOVT_IMM_A(insn) \
  448.   ((bits ((insn), 16, 19) << 12) \
  449.    | bits ((insn), 0, 11))

  450. /* Decode immediate value; implements ThumbExpandImmediate pseudo-op.  */

  451. static unsigned int
  452. thumb_expand_immediate (unsigned int imm)
  453. {
  454.   unsigned int count = imm >> 7;

  455.   if (count < 8)
  456.     switch (count / 2)
  457.       {
  458.       case 0:
  459.         return imm & 0xff;
  460.       case 1:
  461.         return (imm & 0xff) | ((imm & 0xff) << 16);
  462.       case 2:
  463.         return ((imm & 0xff) << 8) | ((imm & 0xff) << 24);
  464.       case 3:
  465.         return (imm & 0xff) | ((imm & 0xff) << 8)
  466.                 | ((imm & 0xff) << 16) | ((imm & 0xff) << 24);
  467.       }

  468.   return (0x80 | (imm & 0x7f)) << (32 - count);
  469. }

  470. /* Return 1 if the 16-bit Thumb instruction INST might change
  471.    control flow, 0 otherwise.  */

  472. static int
  473. thumb_instruction_changes_pc (unsigned short inst)
  474. {
  475.   if ((inst & 0xff00) == 0xbd00)        /* pop {rlist, pc} */
  476.     return 1;

  477.   if ((inst & 0xf000) == 0xd000)        /* conditional branch */
  478.     return 1;

  479.   if ((inst & 0xf800) == 0xe000)        /* unconditional branch */
  480.     return 1;

  481.   if ((inst & 0xff00) == 0x4700)        /* bx REG, blx REG */
  482.     return 1;

  483.   if ((inst & 0xff87) == 0x4687)        /* mov pc, REG */
  484.     return 1;

  485.   if ((inst & 0xf500) == 0xb100)        /* CBNZ or CBZ.  */
  486.     return 1;

  487.   return 0;
  488. }

  489. /* Return 1 if the 32-bit Thumb instruction in INST1 and INST2
  490.    might change control flow, 0 otherwise.  */

  491. static int
  492. thumb2_instruction_changes_pc (unsigned short inst1, unsigned short inst2)
  493. {
  494.   if ((inst1 & 0xf800) == 0xf000 && (inst2 & 0x8000) == 0x8000)
  495.     {
  496.       /* Branches and miscellaneous control instructions.  */

  497.       if ((inst2 & 0x1000) != 0 || (inst2 & 0xd001) == 0xc000)
  498.         {
  499.           /* B, BL, BLX.  */
  500.           return 1;
  501.         }
  502.       else if (inst1 == 0xf3de && (inst2 & 0xff00) == 0x3f00)
  503.         {
  504.           /* SUBS PC, LR, #imm8.  */
  505.           return 1;
  506.         }
  507.       else if ((inst2 & 0xd000) == 0x8000 && (inst1 & 0x0380) != 0x0380)
  508.         {
  509.           /* Conditional branch.  */
  510.           return 1;
  511.         }

  512.       return 0;
  513.     }

  514.   if ((inst1 & 0xfe50) == 0xe810)
  515.     {
  516.       /* Load multiple or RFE.  */

  517.       if (bit (inst1, 7) && !bit (inst1, 8))
  518.         {
  519.           /* LDMIA or POP */
  520.           if (bit (inst2, 15))
  521.             return 1;
  522.         }
  523.       else if (!bit (inst1, 7) && bit (inst1, 8))
  524.         {
  525.           /* LDMDB */
  526.           if (bit (inst2, 15))
  527.             return 1;
  528.         }
  529.       else if (bit (inst1, 7) && bit (inst1, 8))
  530.         {
  531.           /* RFEIA */
  532.           return 1;
  533.         }
  534.       else if (!bit (inst1, 7) && !bit (inst1, 8))
  535.         {
  536.           /* RFEDB */
  537.           return 1;
  538.         }

  539.       return 0;
  540.     }

  541.   if ((inst1 & 0xffef) == 0xea4f && (inst2 & 0xfff0) == 0x0f00)
  542.     {
  543.       /* MOV PC or MOVS PC.  */
  544.       return 1;
  545.     }

  546.   if ((inst1 & 0xff70) == 0xf850 && (inst2 & 0xf000) == 0xf000)
  547.     {
  548.       /* LDR PC.  */
  549.       if (bits (inst1, 0, 3) == 15)
  550.         return 1;
  551.       if (bit (inst1, 7))
  552.         return 1;
  553.       if (bit (inst2, 11))
  554.         return 1;
  555.       if ((inst2 & 0x0fc0) == 0x0000)
  556.         return 1;

  557.       return 0;
  558.     }

  559.   if ((inst1 & 0xfff0) == 0xe8d0 && (inst2 & 0xfff0) == 0xf000)
  560.     {
  561.       /* TBB.  */
  562.       return 1;
  563.     }

  564.   if ((inst1 & 0xfff0) == 0xe8d0 && (inst2 & 0xfff0) == 0xf010)
  565.     {
  566.       /* TBH.  */
  567.       return 1;
  568.     }

  569.   return 0;
  570. }

  571. /* Return 1 if the 16-bit Thumb instruction INSN restores SP in
  572.    epilogue, 0 otherwise.  */

  573. static int
  574. thumb_instruction_restores_sp (unsigned short insn)
  575. {
  576.   return (insn == 0x46bd  /* mov sp, r7 */
  577.           || (insn & 0xff80) == 0xb000  /* add sp, imm */
  578.           || (insn & 0xfe00) == 0xbc00);  /* pop <registers> */
  579. }

  580. /* Analyze a Thumb prologue, looking for a recognizable stack frame
  581.    and frame pointer.  Scan until we encounter a store that could
  582.    clobber the stack frame unexpectedly, or an unknown instruction.
  583.    Return the last address which is definitely safe to skip for an
  584.    initial breakpoint.  */

  585. static CORE_ADDR
  586. thumb_analyze_prologue (struct gdbarch *gdbarch,
  587.                         CORE_ADDR start, CORE_ADDR limit,
  588.                         struct arm_prologue_cache *cache)
  589. {
  590.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  591.   enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
  592.   int i;
  593.   pv_t regs[16];
  594.   struct pv_area *stack;
  595.   struct cleanup *back_to;
  596.   CORE_ADDR offset;
  597.   CORE_ADDR unrecognized_pc = 0;

  598.   for (i = 0; i < 16; i++)
  599.     regs[i] = pv_register (i, 0);
  600.   stack = make_pv_area (ARM_SP_REGNUM, gdbarch_addr_bit (gdbarch));
  601.   back_to = make_cleanup_free_pv_area (stack);

  602.   while (start < limit)
  603.     {
  604.       unsigned short insn;

  605.       insn = read_memory_unsigned_integer (start, 2, byte_order_for_code);

  606.       if ((insn & 0xfe00) == 0xb400)                /* push { rlist } */
  607.         {
  608.           int regno;
  609.           int mask;

  610.           if (pv_area_store_would_trash (stack, regs[ARM_SP_REGNUM]))
  611.             break;

  612.           /* Bits 0-7 contain a mask for registers R0-R7.  Bit 8 says
  613.              whether to save LR (R14).  */
  614.           mask = (insn & 0xff) | ((insn & 0x100) << 6);

  615.           /* Calculate offsets of saved R0-R7 and LR.  */
  616.           for (regno = ARM_LR_REGNUM; regno >= 0; regno--)
  617.             if (mask & (1 << regno))
  618.               {
  619.                 regs[ARM_SP_REGNUM] = pv_add_constant (regs[ARM_SP_REGNUM],
  620.                                                        -4);
  621.                 pv_area_store (stack, regs[ARM_SP_REGNUM], 4, regs[regno]);
  622.               }
  623.         }
  624.       else if ((insn & 0xff80) == 0xb080)        /* sub sp, #imm */
  625.         {
  626.           offset = (insn & 0x7f) << 2;                /* get scaled offset */
  627.           regs[ARM_SP_REGNUM] = pv_add_constant (regs[ARM_SP_REGNUM],
  628.                                                  -offset);
  629.         }
  630.       else if (thumb_instruction_restores_sp (insn))
  631.         {
  632.           /* Don't scan past the epilogue.  */
  633.           break;
  634.         }
  635.       else if ((insn & 0xf800) == 0xa800)        /* add Rd, sp, #imm */
  636.         regs[bits (insn, 8, 10)] = pv_add_constant (regs[ARM_SP_REGNUM],
  637.                                                     (insn & 0xff) << 2);
  638.       else if ((insn & 0xfe00) == 0x1c00        /* add Rd, Rn, #imm */
  639.                && pv_is_register (regs[bits (insn, 3, 5)], ARM_SP_REGNUM))
  640.         regs[bits (insn, 0, 2)] = pv_add_constant (regs[bits (insn, 3, 5)],
  641.                                                    bits (insn, 6, 8));
  642.       else if ((insn & 0xf800) == 0x3000        /* add Rd, #imm */
  643.                && pv_is_register (regs[bits (insn, 8, 10)], ARM_SP_REGNUM))
  644.         regs[bits (insn, 8, 10)] = pv_add_constant (regs[bits (insn, 8, 10)],
  645.                                                     bits (insn, 0, 7));
  646.       else if ((insn & 0xfe00) == 0x1800        /* add Rd, Rn, Rm */
  647.                && pv_is_register (regs[bits (insn, 6, 8)], ARM_SP_REGNUM)
  648.                && pv_is_constant (regs[bits (insn, 3, 5)]))
  649.         regs[bits (insn, 0, 2)] = pv_add (regs[bits (insn, 3, 5)],
  650.                                           regs[bits (insn, 6, 8)]);
  651.       else if ((insn & 0xff00) == 0x4400        /* add Rd, Rm */
  652.                && pv_is_constant (regs[bits (insn, 3, 6)]))
  653.         {
  654.           int rd = (bit (insn, 7) << 3) + bits (insn, 0, 2);
  655.           int rm = bits (insn, 3, 6);
  656.           regs[rd] = pv_add (regs[rd], regs[rm]);
  657.         }
  658.       else if ((insn & 0xff00) == 0x4600)        /* mov hi, lo or mov lo, hi */
  659.         {
  660.           int dst_reg = (insn & 0x7) + ((insn & 0x80) >> 4);
  661.           int src_reg = (insn & 0x78) >> 3;
  662.           regs[dst_reg] = regs[src_reg];
  663.         }
  664.       else if ((insn & 0xf800) == 0x9000)        /* str rd, [sp, #off] */
  665.         {
  666.           /* Handle stores to the stack.  Normally pushes are used,
  667.              but with GCC -mtpcs-frame, there may be other stores
  668.              in the prologue to create the frame.  */
  669.           int regno = (insn >> 8) & 0x7;
  670.           pv_t addr;

  671.           offset = (insn & 0xff) << 2;
  672.           addr = pv_add_constant (regs[ARM_SP_REGNUM], offset);

  673.           if (pv_area_store_would_trash (stack, addr))
  674.             break;

  675.           pv_area_store (stack, addr, 4, regs[regno]);
  676.         }
  677.       else if ((insn & 0xf800) == 0x6000)        /* str rd, [rn, #off] */
  678.         {
  679.           int rd = bits (insn, 0, 2);
  680.           int rn = bits (insn, 3, 5);
  681.           pv_t addr;

  682.           offset = bits (insn, 6, 10) << 2;
  683.           addr = pv_add_constant (regs[rn], offset);

  684.           if (pv_area_store_would_trash (stack, addr))
  685.             break;

  686.           pv_area_store (stack, addr, 4, regs[rd]);
  687.         }
  688.       else if (((insn & 0xf800) == 0x7000        /* strb Rd, [Rn, #off] */
  689.                 || (insn & 0xf800) == 0x8000)        /* strh Rd, [Rn, #off] */
  690.                && pv_is_register (regs[bits (insn, 3, 5)], ARM_SP_REGNUM))
  691.         /* Ignore stores of argument registers to the stack.  */
  692.         ;
  693.       else if ((insn & 0xf800) == 0xc800        /* ldmia Rn!, { registers } */
  694.                && pv_is_register (regs[bits (insn, 8, 10)], ARM_SP_REGNUM))
  695.         /* Ignore block loads from the stack, potentially copying
  696.            parameters from memory.  */
  697.         ;
  698.       else if ((insn & 0xf800) == 0x9800        /* ldr Rd, [Rn, #immed] */
  699.                || ((insn & 0xf800) == 0x6800        /* ldr Rd, [sp, #immed] */
  700.                    && pv_is_register (regs[bits (insn, 3, 5)], ARM_SP_REGNUM)))
  701.         /* Similarly ignore single loads from the stack.  */
  702.         ;
  703.       else if ((insn & 0xffc0) == 0x0000        /* lsls Rd, Rm, #0 */
  704.                || (insn & 0xffc0) == 0x1c00)        /* add Rd, Rn, #0 */
  705.         /* Skip register copies, i.e. saves to another register
  706.            instead of the stack.  */
  707.         ;
  708.       else if ((insn & 0xf800) == 0x2000)        /* movs Rd, #imm */
  709.         /* Recognize constant loads; even with small stacks these are necessary
  710.            on Thumb.  */
  711.         regs[bits (insn, 8, 10)] = pv_constant (bits (insn, 0, 7));
  712.       else if ((insn & 0xf800) == 0x4800)        /* ldr Rd, [pc, #imm] */
  713.         {
  714.           /* Constant pool loads, for the same reason.  */
  715.           unsigned int constant;
  716.           CORE_ADDR loc;

  717.           loc = start + 4 + bits (insn, 0, 7) * 4;
  718.           constant = read_memory_unsigned_integer (loc, 4, byte_order);
  719.           regs[bits (insn, 8, 10)] = pv_constant (constant);
  720.         }
  721.       else if (thumb_insn_size (insn) == 4) /* 32-bit Thumb-2 instructions.  */
  722.         {
  723.           unsigned short inst2;

  724.           inst2 = read_memory_unsigned_integer (start + 2, 2,
  725.                                                 byte_order_for_code);

  726.           if ((insn & 0xf800) == 0xf000 && (inst2 & 0xe800) == 0xe800)
  727.             {
  728.               /* BL, BLX.  Allow some special function calls when
  729.                  skipping the prologue; GCC generates these before
  730.                  storing arguments to the stack.  */
  731.               CORE_ADDR nextpc;
  732.               int j1, j2, imm1, imm2;

  733.               imm1 = sbits (insn, 0, 10);
  734.               imm2 = bits (inst2, 0, 10);
  735.               j1 = bit (inst2, 13);
  736.               j2 = bit (inst2, 11);

  737.               offset = ((imm1 << 12) + (imm2 << 1));
  738.               offset ^= ((!j2) << 22) | ((!j1) << 23);

  739.               nextpc = start + 4 + offset;
  740.               /* For BLX make sure to clear the low bits.  */
  741.               if (bit (inst2, 12) == 0)
  742.                 nextpc = nextpc & 0xfffffffc;

  743.               if (!skip_prologue_function (gdbarch, nextpc,
  744.                                            bit (inst2, 12) != 0))
  745.                 break;
  746.             }

  747.           else if ((insn & 0xffd0) == 0xe900    /* stmdb Rn{!},
  748.                                                    { registers } */
  749.                    && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
  750.             {
  751.               pv_t addr = regs[bits (insn, 0, 3)];
  752.               int regno;

  753.               if (pv_area_store_would_trash (stack, addr))
  754.                 break;

  755.               /* Calculate offsets of saved registers.  */
  756.               for (regno = ARM_LR_REGNUM; regno >= 0; regno--)
  757.                 if (inst2 & (1 << regno))
  758.                   {
  759.                     addr = pv_add_constant (addr, -4);
  760.                     pv_area_store (stack, addr, 4, regs[regno]);
  761.                   }

  762.               if (insn & 0x0020)
  763.                 regs[bits (insn, 0, 3)] = addr;
  764.             }

  765.           else if ((insn & 0xff50) == 0xe940        /* strd Rt, Rt2,
  766.                                                    [Rn, #+/-imm]{!} */
  767.                    && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
  768.             {
  769.               int regno1 = bits (inst2, 12, 15);
  770.               int regno2 = bits (inst2, 8, 11);
  771.               pv_t addr = regs[bits (insn, 0, 3)];

  772.               offset = inst2 & 0xff;
  773.               if (insn & 0x0080)
  774.                 addr = pv_add_constant (addr, offset);
  775.               else
  776.                 addr = pv_add_constant (addr, -offset);

  777.               if (pv_area_store_would_trash (stack, addr))
  778.                 break;

  779.               pv_area_store (stack, addr, 4, regs[regno1]);
  780.               pv_area_store (stack, pv_add_constant (addr, 4),
  781.                              4, regs[regno2]);

  782.               if (insn & 0x0020)
  783.                 regs[bits (insn, 0, 3)] = addr;
  784.             }

  785.           else if ((insn & 0xfff0) == 0xf8c0        /* str Rt,[Rn,+/-#imm]{!} */
  786.                    && (inst2 & 0x0c00) == 0x0c00
  787.                    && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
  788.             {
  789.               int regno = bits (inst2, 12, 15);
  790.               pv_t addr = regs[bits (insn, 0, 3)];

  791.               offset = inst2 & 0xff;
  792.               if (inst2 & 0x0200)
  793.                 addr = pv_add_constant (addr, offset);
  794.               else
  795.                 addr = pv_add_constant (addr, -offset);

  796.               if (pv_area_store_would_trash (stack, addr))
  797.                 break;

  798.               pv_area_store (stack, addr, 4, regs[regno]);

  799.               if (inst2 & 0x0100)
  800.                 regs[bits (insn, 0, 3)] = addr;
  801.             }

  802.           else if ((insn & 0xfff0) == 0xf8c0        /* str.w Rt,[Rn,#imm] */
  803.                    && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
  804.             {
  805.               int regno = bits (inst2, 12, 15);
  806.               pv_t addr;

  807.               offset = inst2 & 0xfff;
  808.               addr = pv_add_constant (regs[bits (insn, 0, 3)], offset);

  809.               if (pv_area_store_would_trash (stack, addr))
  810.                 break;

  811.               pv_area_store (stack, addr, 4, regs[regno]);
  812.             }

  813.           else if ((insn & 0xffd0) == 0xf880        /* str{bh}.w Rt,[Rn,#imm] */
  814.                    && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
  815.             /* Ignore stores of argument registers to the stack.  */
  816.             ;

  817.           else if ((insn & 0xffd0) == 0xf800        /* str{bh} Rt,[Rn,#+/-imm] */
  818.                    && (inst2 & 0x0d00) == 0x0c00
  819.                    && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
  820.             /* Ignore stores of argument registers to the stack.  */
  821.             ;

  822.           else if ((insn & 0xffd0) == 0xe890        /* ldmia Rn[!],
  823.                                                    { registers } */
  824.                    && (inst2 & 0x8000) == 0x0000
  825.                    && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
  826.             /* Ignore block loads from the stack, potentially copying
  827.                parameters from memory.  */
  828.             ;

  829.           else if ((insn & 0xffb0) == 0xe950        /* ldrd Rt, Rt2,
  830.                                                    [Rn, #+/-imm] */
  831.                    && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
  832.             /* Similarly ignore dual loads from the stack.  */
  833.             ;

  834.           else if ((insn & 0xfff0) == 0xf850        /* ldr Rt,[Rn,#+/-imm] */
  835.                    && (inst2 & 0x0d00) == 0x0c00
  836.                    && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
  837.             /* Similarly ignore single loads from the stack.  */
  838.             ;

  839.           else if ((insn & 0xfff0) == 0xf8d0        /* ldr.w Rt,[Rn,#imm] */
  840.                    && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
  841.             /* Similarly ignore single loads from the stack.  */
  842.             ;

  843.           else if ((insn & 0xfbf0) == 0xf100        /* add.w Rd, Rn, #imm */
  844.                    && (inst2 & 0x8000) == 0x0000)
  845.             {
  846.               unsigned int imm = ((bits (insn, 10, 10) << 11)
  847.                                   | (bits (inst2, 12, 14) << 8)
  848.                                   | bits (inst2, 0, 7));

  849.               regs[bits (inst2, 8, 11)]
  850.                 = pv_add_constant (regs[bits (insn, 0, 3)],
  851.                                    thumb_expand_immediate (imm));
  852.             }

  853.           else if ((insn & 0xfbf0) == 0xf200        /* addw Rd, Rn, #imm */
  854.                    && (inst2 & 0x8000) == 0x0000)
  855.             {
  856.               unsigned int imm = ((bits (insn, 10, 10) << 11)
  857.                                   | (bits (inst2, 12, 14) << 8)
  858.                                   | bits (inst2, 0, 7));

  859.               regs[bits (inst2, 8, 11)]
  860.                 = pv_add_constant (regs[bits (insn, 0, 3)], imm);
  861.             }

  862.           else if ((insn & 0xfbf0) == 0xf1a0        /* sub.w Rd, Rn, #imm */
  863.                    && (inst2 & 0x8000) == 0x0000)
  864.             {
  865.               unsigned int imm = ((bits (insn, 10, 10) << 11)
  866.                                   | (bits (inst2, 12, 14) << 8)
  867.                                   | bits (inst2, 0, 7));

  868.               regs[bits (inst2, 8, 11)]
  869.                 = pv_add_constant (regs[bits (insn, 0, 3)],
  870.                                    - (CORE_ADDR) thumb_expand_immediate (imm));
  871.             }

  872.           else if ((insn & 0xfbf0) == 0xf2a0        /* subw Rd, Rn, #imm */
  873.                    && (inst2 & 0x8000) == 0x0000)
  874.             {
  875.               unsigned int imm = ((bits (insn, 10, 10) << 11)
  876.                                   | (bits (inst2, 12, 14) << 8)
  877.                                   | bits (inst2, 0, 7));

  878.               regs[bits (inst2, 8, 11)]
  879.                 = pv_add_constant (regs[bits (insn, 0, 3)], - (CORE_ADDR) imm);
  880.             }

  881.           else if ((insn & 0xfbff) == 0xf04f)        /* mov.w Rd, #const */
  882.             {
  883.               unsigned int imm = ((bits (insn, 10, 10) << 11)
  884.                                   | (bits (inst2, 12, 14) << 8)
  885.                                   | bits (inst2, 0, 7));

  886.               regs[bits (inst2, 8, 11)]
  887.                 = pv_constant (thumb_expand_immediate (imm));
  888.             }

  889.           else if ((insn & 0xfbf0) == 0xf240)        /* movw Rd, #const */
  890.             {
  891.               unsigned int imm
  892.                 = EXTRACT_MOVW_MOVT_IMM_T (insn, inst2);

  893.               regs[bits (inst2, 8, 11)] = pv_constant (imm);
  894.             }

  895.           else if (insn == 0xea5f                /* mov.w Rd,Rm */
  896.                    && (inst2 & 0xf0f0) == 0)
  897.             {
  898.               int dst_reg = (inst2 & 0x0f00) >> 8;
  899.               int src_reg = inst2 & 0xf;
  900.               regs[dst_reg] = regs[src_reg];
  901.             }

  902.           else if ((insn & 0xff7f) == 0xf85f)        /* ldr.w Rt,<label> */
  903.             {
  904.               /* Constant pool loads.  */
  905.               unsigned int constant;
  906.               CORE_ADDR loc;

  907.               offset = bits (inst2, 0, 11);
  908.               if (insn & 0x0080)
  909.                 loc = start + 4 + offset;
  910.               else
  911.                 loc = start + 4 - offset;

  912.               constant = read_memory_unsigned_integer (loc, 4, byte_order);
  913.               regs[bits (inst2, 12, 15)] = pv_constant (constant);
  914.             }

  915.           else if ((insn & 0xff7f) == 0xe95f)        /* ldrd Rt,Rt2,<label> */
  916.             {
  917.               /* Constant pool loads.  */
  918.               unsigned int constant;
  919.               CORE_ADDR loc;

  920.               offset = bits (inst2, 0, 7) << 2;
  921.               if (insn & 0x0080)
  922.                 loc = start + 4 + offset;
  923.               else
  924.                 loc = start + 4 - offset;

  925.               constant = read_memory_unsigned_integer (loc, 4, byte_order);
  926.               regs[bits (inst2, 12, 15)] = pv_constant (constant);

  927.               constant = read_memory_unsigned_integer (loc + 4, 4, byte_order);
  928.               regs[bits (inst2, 8, 11)] = pv_constant (constant);
  929.             }

  930.           else if (thumb2_instruction_changes_pc (insn, inst2))
  931.             {
  932.               /* Don't scan past anything that might change control flow.  */
  933.               break;
  934.             }
  935.           else
  936.             {
  937.               /* The optimizer might shove anything into the prologue,
  938.                  so we just skip what we don't recognize.  */
  939.               unrecognized_pc = start;
  940.             }

  941.           start += 2;
  942.         }
  943.       else if (thumb_instruction_changes_pc (insn))
  944.         {
  945.           /* Don't scan past anything that might change control flow.  */
  946.           break;
  947.         }
  948.       else
  949.         {
  950.           /* The optimizer might shove anything into the prologue,
  951.              so we just skip what we don't recognize.  */
  952.           unrecognized_pc = start;
  953.         }

  954.       start += 2;
  955.     }

  956.   if (arm_debug)
  957.     fprintf_unfiltered (gdb_stdlog, "Prologue scan stopped at %s\n",
  958.                         paddress (gdbarch, start));

  959.   if (unrecognized_pc == 0)
  960.     unrecognized_pc = start;

  961.   if (cache == NULL)
  962.     {
  963.       do_cleanups (back_to);
  964.       return unrecognized_pc;
  965.     }

  966.   if (pv_is_register (regs[ARM_FP_REGNUM], ARM_SP_REGNUM))
  967.     {
  968.       /* Frame pointer is fpFrame size is constant.  */
  969.       cache->framereg = ARM_FP_REGNUM;
  970.       cache->framesize = -regs[ARM_FP_REGNUM].k;
  971.     }
  972.   else if (pv_is_register (regs[THUMB_FP_REGNUM], ARM_SP_REGNUM))
  973.     {
  974.       /* Frame pointer is r7.  Frame size is constant.  */
  975.       cache->framereg = THUMB_FP_REGNUM;
  976.       cache->framesize = -regs[THUMB_FP_REGNUM].k;
  977.     }
  978.   else
  979.     {
  980.       /* Try the stack pointer... this is a bit desperate.  */
  981.       cache->framereg = ARM_SP_REGNUM;
  982.       cache->framesize = -regs[ARM_SP_REGNUM].k;
  983.     }

  984.   for (i = 0; i < 16; i++)
  985.     if (pv_area_find_reg (stack, gdbarch, i, &offset))
  986.       cache->saved_regs[i].addr = offset;

  987.   do_cleanups (back_to);
  988.   return unrecognized_pc;
  989. }


  990. /* Try to analyze the instructions starting from PC, which load symbol
  991.    __stack_chk_guard.  Return the address of instruction after loading this
  992.    symbol, set the dest register number to *BASEREG, and set the size of
  993.    instructions for loading symbol in OFFSET.  Return 0 if instructions are
  994.    not recognized.  */

  995. static CORE_ADDR
  996. arm_analyze_load_stack_chk_guard(CORE_ADDR pc, struct gdbarch *gdbarch,
  997.                                  unsigned int *destreg, int *offset)
  998. {
  999.   enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
  1000.   int is_thumb = arm_pc_is_thumb (gdbarch, pc);
  1001.   unsigned int low, high, address;

  1002.   address = 0;
  1003.   if (is_thumb)
  1004.     {
  1005.       unsigned short insn1
  1006.         = read_memory_unsigned_integer (pc, 2, byte_order_for_code);

  1007.       if ((insn1 & 0xf800) == 0x4800) /* ldr Rd, #immed */
  1008.         {
  1009.           *destreg = bits (insn1, 8, 10);
  1010.           *offset = 2;
  1011.           address = (pc & 0xfffffffc) + 4 + (bits (insn1, 0, 7) << 2);
  1012.           address = read_memory_unsigned_integer (address, 4,
  1013.                                                   byte_order_for_code);
  1014.         }
  1015.       else if ((insn1 & 0xfbf0) == 0xf240) /* movw Rd, #const */
  1016.         {
  1017.           unsigned short insn2
  1018.             = read_memory_unsigned_integer (pc + 2, 2, byte_order_for_code);

  1019.           low = EXTRACT_MOVW_MOVT_IMM_T (insn1, insn2);

  1020.           insn1
  1021.             = read_memory_unsigned_integer (pc + 4, 2, byte_order_for_code);
  1022.           insn2
  1023.             = read_memory_unsigned_integer (pc + 6, 2, byte_order_for_code);

  1024.           /* movt Rd, #const */
  1025.           if ((insn1 & 0xfbc0) == 0xf2c0)
  1026.             {
  1027.               high = EXTRACT_MOVW_MOVT_IMM_T (insn1, insn2);
  1028.               *destreg = bits (insn2, 8, 11);
  1029.               *offset = 8;
  1030.               address = (high << 16 | low);
  1031.             }
  1032.         }
  1033.     }
  1034.   else
  1035.     {
  1036.       unsigned int insn
  1037.         = read_memory_unsigned_integer (pc, 4, byte_order_for_code);

  1038.       if ((insn & 0x0e5f0000) == 0x041f0000) /* ldr Rd, [PC, #immed] */
  1039.         {
  1040.           address = bits (insn, 0, 11) + pc + 8;
  1041.           address = read_memory_unsigned_integer (address, 4,
  1042.                                                   byte_order_for_code);

  1043.           *destreg = bits (insn, 12, 15);
  1044.           *offset = 4;
  1045.         }
  1046.       else if ((insn & 0x0ff00000) == 0x03000000) /* movw Rd, #const */
  1047.         {
  1048.           low = EXTRACT_MOVW_MOVT_IMM_A (insn);

  1049.           insn
  1050.             = read_memory_unsigned_integer (pc + 4, 4, byte_order_for_code);

  1051.           if ((insn & 0x0ff00000) == 0x03400000) /* movt Rd, #const */
  1052.             {
  1053.               high = EXTRACT_MOVW_MOVT_IMM_A (insn);
  1054.               *destreg = bits (insn, 12, 15);
  1055.               *offset = 8;
  1056.               address = (high << 16 | low);
  1057.             }
  1058.         }
  1059.     }

  1060.   return address;
  1061. }

  1062. /* Try to skip a sequence of instructions used for stack protector.  If PC
  1063.    points to the first instruction of this sequence, return the address of
  1064.    first instruction after this sequence, otherwise, return original PC.

  1065.    On arm, this sequence of instructions is composed of mainly three steps,
  1066.      Step 1: load symbol __stack_chk_guard,
  1067.      Step 2: load from address of __stack_chk_guard,
  1068.      Step 3: store it to somewhere else.

  1069.    Usually, instructions on step 2 and step 3 are the same on various ARM
  1070.    architectures.  On step 2, it is one instruction 'ldr Rx, [Rn, #0]', and
  1071.    on step 3, it is also one instruction 'str Rx, [r7, #immd]'.  However,
  1072.    instructions in step 1 vary from different ARM architectures.  On ARMv7,
  1073.    they are,

  1074.         movw        Rn, #:lower16:__stack_chk_guard
  1075.         movt        Rn, #:upper16:__stack_chk_guard

  1076.    On ARMv5t, it is,

  1077.         ldr        Rn, .Label
  1078.         ....
  1079.         .Lable:
  1080.         .word        __stack_chk_guard

  1081.    Since ldr/str is a very popular instruction, we can't use them as
  1082.    'fingerprint' or 'signature' of stack protector sequence.  Here we choose
  1083.    sequence {movw/movt, ldr}/ldr/str plus symbol __stack_chk_guard, if not
  1084.    stripped, as the 'fingerprint' of a stack protector cdoe sequence.  */

  1085. static CORE_ADDR
  1086. arm_skip_stack_protector(CORE_ADDR pc, struct gdbarch *gdbarch)
  1087. {
  1088.   enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
  1089.   unsigned int basereg;
  1090.   struct bound_minimal_symbol stack_chk_guard;
  1091.   int offset;
  1092.   int is_thumb = arm_pc_is_thumb (gdbarch, pc);
  1093.   CORE_ADDR addr;

  1094.   /* Try to parse the instructions in Step 1.  */
  1095.   addr = arm_analyze_load_stack_chk_guard (pc, gdbarch,
  1096.                                            &basereg, &offset);
  1097.   if (!addr)
  1098.     return pc;

  1099.   stack_chk_guard = lookup_minimal_symbol_by_pc (addr);
  1100.   /* ADDR must correspond to a symbol whose name is __stack_chk_guard.
  1101.      Otherwise, this sequence cannot be for stack protector.  */
  1102.   if (stack_chk_guard.minsym == NULL
  1103.       || strncmp (MSYMBOL_LINKAGE_NAME (stack_chk_guard.minsym),
  1104.                   "__stack_chk_guard",
  1105.                   strlen ("__stack_chk_guard")) != 0)
  1106.    return pc;

  1107.   if (is_thumb)
  1108.     {
  1109.       unsigned int destreg;
  1110.       unsigned short insn
  1111.         = read_memory_unsigned_integer (pc + offset, 2, byte_order_for_code);

  1112.       /* Step 2: ldr Rd, [Rn, #immed], encoding T1.  */
  1113.       if ((insn & 0xf800) != 0x6800)
  1114.         return pc;
  1115.       if (bits (insn, 3, 5) != basereg)
  1116.         return pc;
  1117.       destreg = bits (insn, 0, 2);

  1118.       insn = read_memory_unsigned_integer (pc + offset + 2, 2,
  1119.                                            byte_order_for_code);
  1120.       /* Step 3: str Rd, [Rn, #immed], encoding T1.  */
  1121.       if ((insn & 0xf800) != 0x6000)
  1122.         return pc;
  1123.       if (destreg != bits (insn, 0, 2))
  1124.         return pc;
  1125.     }
  1126.   else
  1127.     {
  1128.       unsigned int destreg;
  1129.       unsigned int insn
  1130.         = read_memory_unsigned_integer (pc + offset, 4, byte_order_for_code);

  1131.       /* Step 2: ldr Rd, [Rn, #immed], encoding A1.  */
  1132.       if ((insn & 0x0e500000) != 0x04100000)
  1133.         return pc;
  1134.       if (bits (insn, 16, 19) != basereg)
  1135.         return pc;
  1136.       destreg = bits (insn, 12, 15);
  1137.       /* Step 3: str Rd, [Rn, #immed], encoding A1.  */
  1138.       insn = read_memory_unsigned_integer (pc + offset + 4,
  1139.                                            4, byte_order_for_code);
  1140.       if ((insn & 0x0e500000) != 0x04000000)
  1141.         return pc;
  1142.       if (bits (insn, 12, 15) != destreg)
  1143.         return pc;
  1144.     }
  1145.   /* The size of total two instructions ldr/str is 4 on Thumb-2, while 8
  1146.      on arm.  */
  1147.   if (is_thumb)
  1148.     return pc + offset + 4;
  1149.   else
  1150.     return pc + offset + 8;
  1151. }

  1152. /* Advance the PC across any function entry prologue instructions to
  1153.    reach some "real" code.

  1154.    The APCS (ARM Procedure Call Standard) defines the following
  1155.    prologue:

  1156.    mov          ip, sp
  1157.    [stmfd       sp!, {a1,a2,a3,a4}]
  1158.    stmfd        sp!, {...,fp,ip,lr,pc}
  1159.    [stfe        f7, [sp, #-12]!]
  1160.    [stfe        f6, [sp, #-12]!]
  1161.    [stfe        f5, [sp, #-12]!]
  1162.    [stfe        f4, [sp, #-12]!]
  1163.    sub fp, ip, #nn @@ nn == 20 or 4 depending on second insn.  */

  1164. static CORE_ADDR
  1165. arm_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR pc)
  1166. {
  1167.   enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
  1168.   unsigned long inst;
  1169.   CORE_ADDR func_addr, limit_pc;

  1170.   /* See if we can determine the end of the prologue via the symbol table.
  1171.      If so, then return either PC, or the PC after the prologue, whichever
  1172.      is greater.  */
  1173.   if (find_pc_partial_function (pc, NULL, &func_addr, NULL))
  1174.     {
  1175.       CORE_ADDR post_prologue_pc
  1176.         = skip_prologue_using_sal (gdbarch, func_addr);
  1177.       struct compunit_symtab *cust = find_pc_compunit_symtab (func_addr);

  1178.       if (post_prologue_pc)
  1179.         post_prologue_pc
  1180.           = arm_skip_stack_protector (post_prologue_pc, gdbarch);


  1181.       /* GCC always emits a line note before the prologue and another
  1182.          one after, even if the two are at the same address or on the
  1183.          same line.  Take advantage of this so that we do not need to
  1184.          know every instruction that might appear in the prologue.  We
  1185.          will have producer information for most binaries; if it is
  1186.          missing (e.g. for -gstabs), assuming the GNU tools.  */
  1187.       if (post_prologue_pc
  1188.           && (cust == NULL
  1189.               || COMPUNIT_PRODUCER (cust) == NULL
  1190.               || strncmp (COMPUNIT_PRODUCER (cust), "GNU ",
  1191.                           sizeof ("GNU ") - 1) == 0
  1192.               || strncmp (COMPUNIT_PRODUCER (cust), "clang ",
  1193.                           sizeof ("clang ") - 1) == 0))
  1194.         return post_prologue_pc;

  1195.       if (post_prologue_pc != 0)
  1196.         {
  1197.           CORE_ADDR analyzed_limit;

  1198.           /* For non-GCC compilers, make sure the entire line is an
  1199.              acceptable prologue; GDB will round this function's
  1200.              return value up to the end of the following line so we
  1201.              can not skip just part of a line (and we do not want to).

  1202.              RealView does not treat the prologue specially, but does
  1203.              associate prologue code with the opening brace; so this
  1204.              lets us skip the first line if we think it is the opening
  1205.              brace.  */
  1206.           if (arm_pc_is_thumb (gdbarch, func_addr))
  1207.             analyzed_limit = thumb_analyze_prologue (gdbarch, func_addr,
  1208.                                                      post_prologue_pc, NULL);
  1209.           else
  1210.             analyzed_limit = arm_analyze_prologue (gdbarch, func_addr,
  1211.                                                    post_prologue_pc, NULL);

  1212.           if (analyzed_limit != post_prologue_pc)
  1213.             return func_addr;

  1214.           return post_prologue_pc;
  1215.         }
  1216.     }

  1217.   /* Can't determine prologue from the symbol table, need to examine
  1218.      instructions.  */

  1219.   /* Find an upper limit on the function prologue using the debug
  1220.      information.  If the debug information could not be used to provide
  1221.      that bound, then use an arbitrary large number as the upper bound.  */
  1222.   /* Like arm_scan_prologue, stop no later than pc + 64.  */
  1223.   limit_pc = skip_prologue_using_sal (gdbarch, pc);
  1224.   if (limit_pc == 0)
  1225.     limit_pc = pc + 64;          /* Magic.  */


  1226.   /* Check if this is Thumb code.  */
  1227.   if (arm_pc_is_thumb (gdbarch, pc))
  1228.     return thumb_analyze_prologue (gdbarch, pc, limit_pc, NULL);
  1229.   else
  1230.     return arm_analyze_prologue (gdbarch, pc, limit_pc, NULL);
  1231. }

  1232. /* *INDENT-OFF* */
  1233. /* Function: thumb_scan_prologue (helper function for arm_scan_prologue)
  1234.    This function decodes a Thumb function prologue to determine:
  1235.      1) the size of the stack frame
  1236.      2) which registers are saved on it
  1237.      3) the offsets of saved regs
  1238.      4) the offset from the stack pointer to the frame pointer

  1239.    A typical Thumb function prologue would create this stack frame
  1240.    (offsets relative to FP)
  1241.      old SP ->        24  stack parameters
  1242.                 20  LR
  1243.                 16  R7
  1244.      R7 ->       0  local variables (16 bytes)
  1245.      SP ->     -12  additional stack space (12 bytes)
  1246.    The frame size would thus be 36 bytes, and the frame offset would be
  1247.    12 bytes.  The frame register is R7.

  1248.    The comments for thumb_skip_prolog() describe the algorithm we use
  1249.    to detect the end of the prolog.  */
  1250. /* *INDENT-ON* */

  1251. static void
  1252. thumb_scan_prologue (struct gdbarch *gdbarch, CORE_ADDR prev_pc,
  1253.                      CORE_ADDR block_addr, struct arm_prologue_cache *cache)
  1254. {
  1255.   CORE_ADDR prologue_start;
  1256.   CORE_ADDR prologue_end;

  1257.   if (find_pc_partial_function (block_addr, NULL, &prologue_start,
  1258.                                 &prologue_end))
  1259.     {
  1260.       /* See comment in arm_scan_prologue for an explanation of
  1261.          this heuristics.  */
  1262.       if (prologue_end > prologue_start + 64)
  1263.         {
  1264.           prologue_end = prologue_start + 64;
  1265.         }
  1266.     }
  1267.   else
  1268.     /* We're in the boondocks: we have no idea where the start of the
  1269.        function is.  */
  1270.     return;

  1271.   prologue_end = min (prologue_end, prev_pc);

  1272.   thumb_analyze_prologue (gdbarch, prologue_start, prologue_end, cache);
  1273. }

  1274. /* Return 1 if THIS_INSTR might change control flow, 0 otherwise.  */

  1275. static int
  1276. arm_instruction_changes_pc (uint32_t this_instr)
  1277. {
  1278.   if (bits (this_instr, 28, 31) == INST_NV)
  1279.     /* Unconditional instructions.  */
  1280.     switch (bits (this_instr, 24, 27))
  1281.       {
  1282.       case 0xa:
  1283.       case 0xb:
  1284.         /* Branch with Link and change to Thumb.  */
  1285.         return 1;
  1286.       case 0xc:
  1287.       case 0xd:
  1288.       case 0xe:
  1289.         /* Coprocessor register transfer.  */
  1290.         if (bits (this_instr, 12, 15) == 15)
  1291.           error (_("Invalid update to pc in instruction"));
  1292.         return 0;
  1293.       default:
  1294.         return 0;
  1295.       }
  1296.   else
  1297.     switch (bits (this_instr, 25, 27))
  1298.       {
  1299.       case 0x0:
  1300.         if (bits (this_instr, 23, 24) == 2 && bit (this_instr, 20) == 0)
  1301.           {
  1302.             /* Multiplies and extra load/stores.  */
  1303.             if (bit (this_instr, 4) == 1 && bit (this_instr, 7) == 1)
  1304.               /* Neither multiplies nor extension load/stores are allowed
  1305.                  to modify PC.  */
  1306.               return 0;

  1307.             /* Otherwise, miscellaneous instructions.  */

  1308.             /* BX <reg>, BXJ <reg>, BLX <reg> */
  1309.             if (bits (this_instr, 4, 27) == 0x12fff1
  1310.                 || bits (this_instr, 4, 27) == 0x12fff2
  1311.                 || bits (this_instr, 4, 27) == 0x12fff3)
  1312.               return 1;

  1313.             /* Other miscellaneous instructions are unpredictable if they
  1314.                modify PC.  */
  1315.             return 0;
  1316.           }
  1317.         /* Data processing instruction.  Fall through.  */

  1318.       case 0x1:
  1319.         if (bits (this_instr, 12, 15) == 15)
  1320.           return 1;
  1321.         else
  1322.           return 0;

  1323.       case 0x2:
  1324.       case 0x3:
  1325.         /* Media instructions and architecturally undefined instructions.  */
  1326.         if (bits (this_instr, 25, 27) == 3 && bit (this_instr, 4) == 1)
  1327.           return 0;

  1328.         /* Stores.  */
  1329.         if (bit (this_instr, 20) == 0)
  1330.           return 0;

  1331.         /* Loads.  */
  1332.         if (bits (this_instr, 12, 15) == ARM_PC_REGNUM)
  1333.           return 1;
  1334.         else
  1335.           return 0;

  1336.       case 0x4:
  1337.         /* Load/store multiple.  */
  1338.         if (bit (this_instr, 20) == 1 && bit (this_instr, 15) == 1)
  1339.           return 1;
  1340.         else
  1341.           return 0;

  1342.       case 0x5:
  1343.         /* Branch and branch with link.  */
  1344.         return 1;

  1345.       case 0x6:
  1346.       case 0x7:
  1347.         /* Coprocessor transfers or SWIs can not affect PC.  */
  1348.         return 0;

  1349.       default:
  1350.         internal_error (__FILE__, __LINE__, _("bad value in switch"));
  1351.       }
  1352. }

  1353. /* Return 1 if the ARM instruction INSN restores SP in epilogue, 0
  1354.    otherwise.  */

  1355. static int
  1356. arm_instruction_restores_sp (unsigned int insn)
  1357. {
  1358.   if (bits (insn, 28, 31) != INST_NV)
  1359.     {
  1360.       if ((insn & 0x0df0f000) == 0x0080d000
  1361.           /* ADD SP (register or immediate).  */
  1362.           || (insn & 0x0df0f000) == 0x0040d000
  1363.           /* SUB SP (register or immediate).  */
  1364.           || (insn & 0x0ffffff0) == 0x01a0d000
  1365.           /* MOV SP.  */
  1366.           || (insn & 0x0fff0000) == 0x08bd0000
  1367.           /* POP (LDMIA).  */
  1368.           || (insn & 0x0fff0000) == 0x049d0000)
  1369.           /* POP of a single register.  */
  1370.         return 1;
  1371.     }

  1372.   return 0;
  1373. }

  1374. /* Analyze an ARM mode prologue starting at PROLOGUE_START and
  1375.    continuing no further than PROLOGUE_END.  If CACHE is non-NULL,
  1376.    fill it in.  Return the first address not recognized as a prologue
  1377.    instruction.

  1378.    We recognize all the instructions typically found in ARM prologues,
  1379.    plus harmless instructions which can be skipped (either for analysis
  1380.    purposes, or a more restrictive set that can be skipped when finding
  1381.    the end of the prologue).  */

  1382. static CORE_ADDR
  1383. arm_analyze_prologue (struct gdbarch *gdbarch,
  1384.                       CORE_ADDR prologue_start, CORE_ADDR prologue_end,
  1385.                       struct arm_prologue_cache *cache)
  1386. {
  1387.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  1388.   enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
  1389.   int regno;
  1390.   CORE_ADDR offset, current_pc;
  1391.   pv_t regs[ARM_FPS_REGNUM];
  1392.   struct pv_area *stack;
  1393.   struct cleanup *back_to;
  1394.   CORE_ADDR unrecognized_pc = 0;

  1395.   /* Search the prologue looking for instructions that set up the
  1396.      frame pointer, adjust the stack pointer, and save registers.

  1397.      Be careful, however, and if it doesn't look like a prologue,
  1398.      don't try to scan it.  If, for instance, a frameless function
  1399.      begins with stmfd sp!, then we will tell ourselves there is
  1400.      a frame, which will confuse stack traceback, as well as "finish"
  1401.      and other operations that rely on a knowledge of the stack
  1402.      traceback.  */

  1403.   for (regno = 0; regno < ARM_FPS_REGNUM; regno++)
  1404.     regs[regno] = pv_register (regno, 0);
  1405.   stack = make_pv_area (ARM_SP_REGNUM, gdbarch_addr_bit (gdbarch));
  1406.   back_to = make_cleanup_free_pv_area (stack);

  1407.   for (current_pc = prologue_start;
  1408.        current_pc < prologue_end;
  1409.        current_pc += 4)
  1410.     {
  1411.       unsigned int insn
  1412.         = read_memory_unsigned_integer (current_pc, 4, byte_order_for_code);

  1413.       if (insn == 0xe1a0c00d)                /* mov ip, sp */
  1414.         {
  1415.           regs[ARM_IP_REGNUM] = regs[ARM_SP_REGNUM];
  1416.           continue;
  1417.         }
  1418.       else if ((insn & 0xfff00000) == 0xe2800000        /* add Rd, Rn, #n */
  1419.                && pv_is_register (regs[bits (insn, 16, 19)], ARM_SP_REGNUM))
  1420.         {
  1421.           unsigned imm = insn & 0xff;                   /* immediate value */
  1422.           unsigned rot = (insn & 0xf00) >> 7;           /* rotate amount */
  1423.           int rd = bits (insn, 12, 15);
  1424.           imm = (imm >> rot) | (imm << (32 - rot));
  1425.           regs[rd] = pv_add_constant (regs[bits (insn, 16, 19)], imm);
  1426.           continue;
  1427.         }
  1428.       else if ((insn & 0xfff00000) == 0xe2400000        /* sub Rd, Rn, #n */
  1429.                && pv_is_register (regs[bits (insn, 16, 19)], ARM_SP_REGNUM))
  1430.         {
  1431.           unsigned imm = insn & 0xff;                   /* immediate value */
  1432.           unsigned rot = (insn & 0xf00) >> 7;           /* rotate amount */
  1433.           int rd = bits (insn, 12, 15);
  1434.           imm = (imm >> rot) | (imm << (32 - rot));
  1435.           regs[rd] = pv_add_constant (regs[bits (insn, 16, 19)], -imm);
  1436.           continue;
  1437.         }
  1438.       else if ((insn & 0xffff0fff) == 0xe52d0004)        /* str Rd,
  1439.                                                            [sp, #-4]! */
  1440.         {
  1441.           if (pv_area_store_would_trash (stack, regs[ARM_SP_REGNUM]))
  1442.             break;
  1443.           regs[ARM_SP_REGNUM] = pv_add_constant (regs[ARM_SP_REGNUM], -4);
  1444.           pv_area_store (stack, regs[ARM_SP_REGNUM], 4,
  1445.                          regs[bits (insn, 12, 15)]);
  1446.           continue;
  1447.         }
  1448.       else if ((insn & 0xffff0000) == 0xe92d0000)
  1449.         /* stmfd sp!, {..., fp, ip, lr, pc}
  1450.            or
  1451.            stmfd sp!, {a1, a2, a3, a4}  */
  1452.         {
  1453.           int mask = insn & 0xffff;

  1454.           if (pv_area_store_would_trash (stack, regs[ARM_SP_REGNUM]))
  1455.             break;

  1456.           /* Calculate offsets of saved registers.  */
  1457.           for (regno = ARM_PC_REGNUM; regno >= 0; regno--)
  1458.             if (mask & (1 << regno))
  1459.               {
  1460.                 regs[ARM_SP_REGNUM]
  1461.                   = pv_add_constant (regs[ARM_SP_REGNUM], -4);
  1462.                 pv_area_store (stack, regs[ARM_SP_REGNUM], 4, regs[regno]);
  1463.               }
  1464.         }
  1465.       else if ((insn & 0xffff0000) == 0xe54b0000        /* strb rx,[r11,#-n] */
  1466.                || (insn & 0xffff00f0) == 0xe14b00b0        /* strh rx,[r11,#-n] */
  1467.                || (insn & 0xffffc000) == 0xe50b0000)        /* str  rx,[r11,#-n] */
  1468.         {
  1469.           /* No need to add this to saved_regs -- it's just an arg reg.  */
  1470.           continue;
  1471.         }
  1472.       else if ((insn & 0xffff0000) == 0xe5cd0000        /* strb rx,[sp,#n] */
  1473.                || (insn & 0xffff00f0) == 0xe1cd00b0        /* strh rx,[sp,#n] */
  1474.                || (insn & 0xffffc000) == 0xe58d0000)        /* str  rx,[sp,#n] */
  1475.         {
  1476.           /* No need to add this to saved_regs -- it's just an arg reg.  */
  1477.           continue;
  1478.         }
  1479.       else if ((insn & 0xfff00000) == 0xe8800000        /* stm Rn,
  1480.                                                            { registers } */
  1481.                && pv_is_register (regs[bits (insn, 16, 19)], ARM_SP_REGNUM))
  1482.         {
  1483.           /* No need to add this to saved_regs -- it's just arg regs.  */
  1484.           continue;
  1485.         }
  1486.       else if ((insn & 0xfffff000) == 0xe24cb000)        /* sub fp, ip #n */
  1487.         {
  1488.           unsigned imm = insn & 0xff;                        /* immediate value */
  1489.           unsigned rot = (insn & 0xf00) >> 7;                /* rotate amount */
  1490.           imm = (imm >> rot) | (imm << (32 - rot));
  1491.           regs[ARM_FP_REGNUM] = pv_add_constant (regs[ARM_IP_REGNUM], -imm);
  1492.         }
  1493.       else if ((insn & 0xfffff000) == 0xe24dd000)        /* sub sp, sp #n */
  1494.         {
  1495.           unsigned imm = insn & 0xff;                        /* immediate value */
  1496.           unsigned rot = (insn & 0xf00) >> 7;                /* rotate amount */
  1497.           imm = (imm >> rot) | (imm << (32 - rot));
  1498.           regs[ARM_SP_REGNUM] = pv_add_constant (regs[ARM_SP_REGNUM], -imm);
  1499.         }
  1500.       else if ((insn & 0xffff7fff) == 0xed6d0103        /* stfe f?,
  1501.                                                            [sp, -#c]! */
  1502.                && gdbarch_tdep (gdbarch)->have_fpa_registers)
  1503.         {
  1504.           if (pv_area_store_would_trash (stack, regs[ARM_SP_REGNUM]))
  1505.             break;

  1506.           regs[ARM_SP_REGNUM] = pv_add_constant (regs[ARM_SP_REGNUM], -12);
  1507.           regno = ARM_F0_REGNUM + ((insn >> 12) & 0x07);
  1508.           pv_area_store (stack, regs[ARM_SP_REGNUM], 12, regs[regno]);
  1509.         }
  1510.       else if ((insn & 0xffbf0fff) == 0xec2d0200        /* sfmfd f0, 4,
  1511.                                                            [sp!] */
  1512.                && gdbarch_tdep (gdbarch)->have_fpa_registers)
  1513.         {
  1514.           int n_saved_fp_regs;
  1515.           unsigned int fp_start_reg, fp_bound_reg;

  1516.           if (pv_area_store_would_trash (stack, regs[ARM_SP_REGNUM]))
  1517.             break;

  1518.           if ((insn & 0x800) == 0x800)                /* N0 is set */
  1519.             {
  1520.               if ((insn & 0x40000) == 0x40000)        /* N1 is set */
  1521.                 n_saved_fp_regs = 3;
  1522.               else
  1523.                 n_saved_fp_regs = 1;
  1524.             }
  1525.           else
  1526.             {
  1527.               if ((insn & 0x40000) == 0x40000)        /* N1 is set */
  1528.                 n_saved_fp_regs = 2;
  1529.               else
  1530.                 n_saved_fp_regs = 4;
  1531.             }

  1532.           fp_start_reg = ARM_F0_REGNUM + ((insn >> 12) & 0x7);
  1533.           fp_bound_reg = fp_start_reg + n_saved_fp_regs;
  1534.           for (; fp_start_reg < fp_bound_reg; fp_start_reg++)
  1535.             {
  1536.               regs[ARM_SP_REGNUM] = pv_add_constant (regs[ARM_SP_REGNUM], -12);
  1537.               pv_area_store (stack, regs[ARM_SP_REGNUM], 12,
  1538.                              regs[fp_start_reg++]);
  1539.             }
  1540.         }
  1541.       else if ((insn & 0xff000000) == 0xeb000000 && cache == NULL) /* bl */
  1542.         {
  1543.           /* Allow some special function calls when skipping the
  1544.              prologue; GCC generates these before storing arguments to
  1545.              the stack.  */
  1546.           CORE_ADDR dest = BranchDest (current_pc, insn);

  1547.           if (skip_prologue_function (gdbarch, dest, 0))
  1548.             continue;
  1549.           else
  1550.             break;
  1551.         }
  1552.       else if ((insn & 0xf0000000) != 0xe0000000)
  1553.         break;                        /* Condition not true, exit early.  */
  1554.       else if (arm_instruction_changes_pc (insn))
  1555.         /* Don't scan past anything that might change control flow.  */
  1556.         break;
  1557.       else if (arm_instruction_restores_sp (insn))
  1558.         {
  1559.           /* Don't scan past the epilogue.  */
  1560.           break;
  1561.         }
  1562.       else if ((insn & 0xfe500000) == 0xe8100000        /* ldm */
  1563.                && pv_is_register (regs[bits (insn, 16, 19)], ARM_SP_REGNUM))
  1564.         /* Ignore block loads from the stack, potentially copying
  1565.            parameters from memory.  */
  1566.         continue;
  1567.       else if ((insn & 0xfc500000) == 0xe4100000
  1568.                && pv_is_register (regs[bits (insn, 16, 19)], ARM_SP_REGNUM))
  1569.         /* Similarly ignore single loads from the stack.  */
  1570.         continue;
  1571.       else if ((insn & 0xffff0ff0) == 0xe1a00000)
  1572.         /* MOV Rd, Rm.  Skip register copies, i.e. saves to another
  1573.            register instead of the stack.  */
  1574.         continue;
  1575.       else
  1576.         {
  1577.           /* The optimizer might shove anything into the prologue, if
  1578.              we build up cache (cache != NULL) from scanning prologue,
  1579.              we just skip what we don't recognize and scan further to
  1580.              make cache as complete as possible.  However, if we skip
  1581.              prologue, we'll stop immediately on unrecognized
  1582.              instruction.  */
  1583.           unrecognized_pc = current_pc;
  1584.           if (cache != NULL)
  1585.             continue;
  1586.           else
  1587.             break;
  1588.         }
  1589.     }

  1590.   if (unrecognized_pc == 0)
  1591.     unrecognized_pc = current_pc;

  1592.   if (cache)
  1593.     {
  1594.       int framereg, framesize;

  1595.       /* The frame size is just the distance from the frame register
  1596.          to the original stack pointer.  */
  1597.       if (pv_is_register (regs[ARM_FP_REGNUM], ARM_SP_REGNUM))
  1598.         {
  1599.           /* Frame pointer is fp.  */
  1600.           framereg = ARM_FP_REGNUM;
  1601.           framesize = -regs[ARM_FP_REGNUM].k;
  1602.         }
  1603.       else
  1604.         {
  1605.           /* Try the stack pointer... this is a bit desperate.  */
  1606.           framereg = ARM_SP_REGNUM;
  1607.           framesize = -regs[ARM_SP_REGNUM].k;
  1608.         }

  1609.       cache->framereg = framereg;
  1610.       cache->framesize = framesize;

  1611.       for (regno = 0; regno < ARM_FPS_REGNUM; regno++)
  1612.         if (pv_area_find_reg (stack, gdbarch, regno, &offset))
  1613.           cache->saved_regs[regno].addr = offset;
  1614.     }

  1615.   if (arm_debug)
  1616.     fprintf_unfiltered (gdb_stdlog, "Prologue scan stopped at %s\n",
  1617.                         paddress (gdbarch, unrecognized_pc));

  1618.   do_cleanups (back_to);
  1619.   return unrecognized_pc;
  1620. }

  1621. static void
  1622. arm_scan_prologue (struct frame_info *this_frame,
  1623.                    struct arm_prologue_cache *cache)
  1624. {
  1625.   struct gdbarch *gdbarch = get_frame_arch (this_frame);
  1626.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  1627.   int regno;
  1628.   CORE_ADDR prologue_start, prologue_end, current_pc;
  1629.   CORE_ADDR prev_pc = get_frame_pc (this_frame);
  1630.   CORE_ADDR block_addr = get_frame_address_in_block (this_frame);
  1631.   pv_t regs[ARM_FPS_REGNUM];
  1632.   struct pv_area *stack;
  1633.   struct cleanup *back_to;
  1634.   CORE_ADDR offset;

  1635.   /* Assume there is no frame until proven otherwise.  */
  1636.   cache->framereg = ARM_SP_REGNUM;
  1637.   cache->framesize = 0;

  1638.   /* Check for Thumb prologue.  */
  1639.   if (arm_frame_is_thumb (this_frame))
  1640.     {
  1641.       thumb_scan_prologue (gdbarch, prev_pc, block_addr, cache);
  1642.       return;
  1643.     }

  1644.   /* Find the function prologue.  If we can't find the function in
  1645.      the symbol table, peek in the stack frame to find the PC.  */
  1646.   if (find_pc_partial_function (block_addr, NULL, &prologue_start,
  1647.                                 &prologue_end))
  1648.     {
  1649.       /* One way to find the end of the prologue (which works well
  1650.          for unoptimized code) is to do the following:

  1651.             struct symtab_and_line sal = find_pc_line (prologue_start, 0);

  1652.             if (sal.line == 0)
  1653.               prologue_end = prev_pc;
  1654.             else if (sal.end < prologue_end)
  1655.               prologue_end = sal.end;

  1656.          This mechanism is very accurate so long as the optimizer
  1657.          doesn't move any instructions from the function body into the
  1658.          prologue.  If this happens, sal.end will be the last
  1659.          instruction in the first hunk of prologue code just before
  1660.          the first instruction that the scheduler has moved from
  1661.          the body to the prologue.

  1662.          In order to make sure that we scan all of the prologue
  1663.          instructions, we use a slightly less accurate mechanism which
  1664.          may scan more than necessary.  To help compensate for this
  1665.          lack of accuracy, the prologue scanning loop below contains
  1666.          several clauses which'll cause the loop to terminate early if
  1667.          an implausible prologue instruction is encountered.

  1668.          The expression

  1669.               prologue_start + 64

  1670.          is a suitable endpoint since it accounts for the largest
  1671.          possible prologue plus up to five instructions inserted by
  1672.          the scheduler.  */

  1673.       if (prologue_end > prologue_start + 64)
  1674.         {
  1675.           prologue_end = prologue_start + 64;        /* See above.  */
  1676.         }
  1677.     }
  1678.   else
  1679.     {
  1680.       /* We have no symbol information.  Our only option is to assume this
  1681.          function has a standard stack frame and the normal frame register.
  1682.          Then, we can find the value of our frame pointer on entrance to
  1683.          the callee (or at the present moment if this is the innermost frame).
  1684.          The value stored there should be the address of the stmfd + 8.  */
  1685.       CORE_ADDR frame_loc;
  1686.       LONGEST return_value;

  1687.       frame_loc = get_frame_register_unsigned (this_frame, ARM_FP_REGNUM);
  1688.       if (!safe_read_memory_integer (frame_loc, 4, byte_order, &return_value))
  1689.         return;
  1690.       else
  1691.         {
  1692.           prologue_start = gdbarch_addr_bits_remove
  1693.                              (gdbarch, return_value) - 8;
  1694.           prologue_end = prologue_start + 64;        /* See above.  */
  1695.         }
  1696.     }

  1697.   if (prev_pc < prologue_end)
  1698.     prologue_end = prev_pc;

  1699.   arm_analyze_prologue (gdbarch, prologue_start, prologue_end, cache);
  1700. }

  1701. static struct arm_prologue_cache *
  1702. arm_make_prologue_cache (struct frame_info *this_frame)
  1703. {
  1704.   int reg;
  1705.   struct arm_prologue_cache *cache;
  1706.   CORE_ADDR unwound_fp;

  1707.   cache = FRAME_OBSTACK_ZALLOC (struct arm_prologue_cache);
  1708.   cache->saved_regs = trad_frame_alloc_saved_regs (this_frame);

  1709.   arm_scan_prologue (this_frame, cache);

  1710.   unwound_fp = get_frame_register_unsigned (this_frame, cache->framereg);
  1711.   if (unwound_fp == 0)
  1712.     return cache;

  1713.   cache->prev_sp = unwound_fp + cache->framesize;

  1714.   /* Calculate actual addresses of saved registers using offsets
  1715.      determined by arm_scan_prologue.  */
  1716.   for (reg = 0; reg < gdbarch_num_regs (get_frame_arch (this_frame)); reg++)
  1717.     if (trad_frame_addr_p (cache->saved_regs, reg))
  1718.       cache->saved_regs[reg].addr += cache->prev_sp;

  1719.   return cache;
  1720. }

  1721. /* Our frame ID for a normal frame is the current function's starting PC
  1722.    and the caller's SP when we were called.  */

  1723. static void
  1724. arm_prologue_this_id (struct frame_info *this_frame,
  1725.                       void **this_cache,
  1726.                       struct frame_id *this_id)
  1727. {
  1728.   struct arm_prologue_cache *cache;
  1729.   struct frame_id id;
  1730.   CORE_ADDR pc, func;

  1731.   if (*this_cache == NULL)
  1732.     *this_cache = arm_make_prologue_cache (this_frame);
  1733.   cache = *this_cache;

  1734.   /* This is meant to halt the backtrace at "_start".  */
  1735.   pc = get_frame_pc (this_frame);
  1736.   if (pc <= gdbarch_tdep (get_frame_arch (this_frame))->lowest_pc)
  1737.     return;

  1738.   /* If we've hit a wall, stop.  */
  1739.   if (cache->prev_sp == 0)
  1740.     return;

  1741.   /* Use function start address as part of the frame ID.  If we cannot
  1742.      identify the start address (due to missing symbol information),
  1743.      fall back to just using the current PC.  */
  1744.   func = get_frame_func (this_frame);
  1745.   if (!func)
  1746.     func = pc;

  1747.   id = frame_id_build (cache->prev_sp, func);
  1748.   *this_id = id;
  1749. }

  1750. static struct value *
  1751. arm_prologue_prev_register (struct frame_info *this_frame,
  1752.                             void **this_cache,
  1753.                             int prev_regnum)
  1754. {
  1755.   struct gdbarch *gdbarch = get_frame_arch (this_frame);
  1756.   struct arm_prologue_cache *cache;

  1757.   if (*this_cache == NULL)
  1758.     *this_cache = arm_make_prologue_cache (this_frame);
  1759.   cache = *this_cache;

  1760.   /* If we are asked to unwind the PC, then we need to return the LR
  1761.      instead.  The prologue may save PC, but it will point into this
  1762.      frame's prologue, not the next frame's resume location.  Also
  1763.      strip the saved T bitA valid LR may have the low bit set, but
  1764.      a valid PC never does.  */
  1765.   if (prev_regnum == ARM_PC_REGNUM)
  1766.     {
  1767.       CORE_ADDR lr;

  1768.       lr = frame_unwind_register_unsigned (this_frame, ARM_LR_REGNUM);
  1769.       return frame_unwind_got_constant (this_frame, prev_regnum,
  1770.                                         arm_addr_bits_remove (gdbarch, lr));
  1771.     }

  1772.   /* SP is generally not saved to the stack, but this frame is
  1773.      identified by the next frame's stack pointer at the time of the call.
  1774.      The value was already reconstructed into PREV_SP.  */
  1775.   if (prev_regnum == ARM_SP_REGNUM)
  1776.     return frame_unwind_got_constant (this_frame, prev_regnum, cache->prev_sp);

  1777.   /* The CPSR may have been changed by the call instruction and by the
  1778.      called function.  The only bit we can reconstruct is the T bit,
  1779.      by checking the low bit of LR as of the call.  This is a reliable
  1780.      indicator of Thumb-ness except for some ARM v4T pre-interworking
  1781.      Thumb code, which could get away with a clear low bit as long as
  1782.      the called function did not use bx.  Guess that all other
  1783.      bits are unchanged; the condition flags are presumably lost,
  1784.      but the processor status is likely valid.  */
  1785.   if (prev_regnum == ARM_PS_REGNUM)
  1786.     {
  1787.       CORE_ADDR lr, cpsr;
  1788.       ULONGEST t_bit = arm_psr_thumb_bit (gdbarch);

  1789.       cpsr = get_frame_register_unsigned (this_frame, prev_regnum);
  1790.       lr = frame_unwind_register_unsigned (this_frame, ARM_LR_REGNUM);
  1791.       if (IS_THUMB_ADDR (lr))
  1792.         cpsr |= t_bit;
  1793.       else
  1794.         cpsr &= ~t_bit;
  1795.       return frame_unwind_got_constant (this_frame, prev_regnum, cpsr);
  1796.     }

  1797.   return trad_frame_get_prev_register (this_frame, cache->saved_regs,
  1798.                                        prev_regnum);
  1799. }

  1800. struct frame_unwind arm_prologue_unwind = {
  1801.   NORMAL_FRAME,
  1802.   default_frame_unwind_stop_reason,
  1803.   arm_prologue_this_id,
  1804.   arm_prologue_prev_register,
  1805.   NULL,
  1806.   default_frame_sniffer
  1807. };

  1808. /* Maintain a list of ARM exception table entries per objfile, similar to the
  1809.    list of mapping symbols.  We only cache entries for standard ARM-defined
  1810.    personality routines; the cache will contain only the frame unwinding
  1811.    instructions associated with the entry (not the descriptors).  */

  1812. static const struct objfile_data *arm_exidx_data_key;

  1813. struct arm_exidx_entry
  1814. {
  1815.   bfd_vma addr;
  1816.   gdb_byte *entry;
  1817. };
  1818. typedef struct arm_exidx_entry arm_exidx_entry_s;
  1819. DEF_VEC_O(arm_exidx_entry_s);

  1820. struct arm_exidx_data
  1821. {
  1822.   VEC(arm_exidx_entry_s) **section_maps;
  1823. };

  1824. static void
  1825. arm_exidx_data_free (struct objfile *objfile, void *arg)
  1826. {
  1827.   struct arm_exidx_data *data = arg;
  1828.   unsigned int i;

  1829.   for (i = 0; i < objfile->obfd->section_count; i++)
  1830.     VEC_free (arm_exidx_entry_s, data->section_maps[i]);
  1831. }

  1832. static inline int
  1833. arm_compare_exidx_entries (const struct arm_exidx_entry *lhs,
  1834.                            const struct arm_exidx_entry *rhs)
  1835. {
  1836.   return lhs->addr < rhs->addr;
  1837. }

  1838. static struct obj_section *
  1839. arm_obj_section_from_vma (struct objfile *objfile, bfd_vma vma)
  1840. {
  1841.   struct obj_section *osect;

  1842.   ALL_OBJFILE_OSECTIONS (objfile, osect)
  1843.     if (bfd_get_section_flags (objfile->obfd,
  1844.                                osect->the_bfd_section) & SEC_ALLOC)
  1845.       {
  1846.         bfd_vma start, size;
  1847.         start = bfd_get_section_vma (objfile->obfd, osect->the_bfd_section);
  1848.         size = bfd_get_section_size (osect->the_bfd_section);

  1849.         if (start <= vma && vma < start + size)
  1850.           return osect;
  1851.       }

  1852.   return NULL;
  1853. }

  1854. /* Parse contents of exception table and exception index sections
  1855.    of OBJFILE, and fill in the exception table entry cache.

  1856.    For each entry that refers to a standard ARM-defined personality
  1857.    routine, extract the frame unwinding instructions (from either
  1858.    the index or the table section).  The unwinding instructions
  1859.    are normalized by:
  1860.     - extracting them from the rest of the table data
  1861.     - converting to host endianness
  1862.     - appending the implicit 0xb0 ("Finish") code

  1863.    The extracted and normalized instructions are stored for later
  1864.    retrieval by the arm_find_exidx_entry routine.  */

  1865. static void
  1866. arm_exidx_new_objfile (struct objfile *objfile)
  1867. {
  1868.   struct cleanup *cleanups;
  1869.   struct arm_exidx_data *data;
  1870.   asection *exidx, *extab;
  1871.   bfd_vma exidx_vma = 0, extab_vma = 0;
  1872.   bfd_size_type exidx_size = 0, extab_size = 0;
  1873.   gdb_byte *exidx_data = NULL, *extab_data = NULL;
  1874.   LONGEST i;

  1875.   /* If we've already touched this file, do nothing.  */
  1876.   if (!objfile || objfile_data (objfile, arm_exidx_data_key) != NULL)
  1877.     return;
  1878.   cleanups = make_cleanup (null_cleanup, NULL);

  1879.   /* Read contents of exception table and index.  */
  1880.   exidx = bfd_get_section_by_name (objfile->obfd, ".ARM.exidx");
  1881.   if (exidx)
  1882.     {
  1883.       exidx_vma = bfd_section_vma (objfile->obfd, exidx);
  1884.       exidx_size = bfd_get_section_size (exidx);
  1885.       exidx_data = xmalloc (exidx_size);
  1886.       make_cleanup (xfree, exidx_data);

  1887.       if (!bfd_get_section_contents (objfile->obfd, exidx,
  1888.                                      exidx_data, 0, exidx_size))
  1889.         {
  1890.           do_cleanups (cleanups);
  1891.           return;
  1892.         }
  1893.     }

  1894.   extab = bfd_get_section_by_name (objfile->obfd, ".ARM.extab");
  1895.   if (extab)
  1896.     {
  1897.       extab_vma = bfd_section_vma (objfile->obfd, extab);
  1898.       extab_size = bfd_get_section_size (extab);
  1899.       extab_data = xmalloc (extab_size);
  1900.       make_cleanup (xfree, extab_data);

  1901.       if (!bfd_get_section_contents (objfile->obfd, extab,
  1902.                                      extab_data, 0, extab_size))
  1903.         {
  1904.           do_cleanups (cleanups);
  1905.           return;
  1906.         }
  1907.     }

  1908.   /* Allocate exception table data structure.  */
  1909.   data = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct arm_exidx_data);
  1910.   set_objfile_data (objfile, arm_exidx_data_key, data);
  1911.   data->section_maps = OBSTACK_CALLOC (&objfile->objfile_obstack,
  1912.                                        objfile->obfd->section_count,
  1913.                                        VEC(arm_exidx_entry_s) *);

  1914.   /* Fill in exception table.  */
  1915.   for (i = 0; i < exidx_size / 8; i++)
  1916.     {
  1917.       struct arm_exidx_entry new_exidx_entry;
  1918.       bfd_vma idx = bfd_h_get_32 (objfile->obfd, exidx_data + i * 8);
  1919.       bfd_vma val = bfd_h_get_32 (objfile->obfd, exidx_data + i * 8 + 4);
  1920.       bfd_vma addr = 0, word = 0;
  1921.       int n_bytes = 0, n_words = 0;
  1922.       struct obj_section *sec;
  1923.       gdb_byte *entry = NULL;

  1924.       /* Extract address of start of function.  */
  1925.       idx = ((idx & 0x7fffffff) ^ 0x40000000) - 0x40000000;
  1926.       idx += exidx_vma + i * 8;

  1927.       /* Find section containing function and compute section offset.  */
  1928.       sec = arm_obj_section_from_vma (objfile, idx);
  1929.       if (sec == NULL)
  1930.         continue;
  1931.       idx -= bfd_get_section_vma (objfile->obfd, sec->the_bfd_section);

  1932.       /* Determine address of exception table entry.  */
  1933.       if (val == 1)
  1934.         {
  1935.           /* EXIDX_CANTUNWIND -- no exception table entry present.  */
  1936.         }
  1937.       else if ((val & 0xff000000) == 0x80000000)
  1938.         {
  1939.           /* Exception table entry embedded in .ARM.exidx
  1940.              -- must be short form.  */
  1941.           word = val;
  1942.           n_bytes = 3;
  1943.         }
  1944.       else if (!(val & 0x80000000))
  1945.         {
  1946.           /* Exception table entry in .ARM.extab.  */
  1947.           addr = ((val & 0x7fffffff) ^ 0x40000000) - 0x40000000;
  1948.           addr += exidx_vma + i * 8 + 4;

  1949.           if (addr >= extab_vma && addr + 4 <= extab_vma + extab_size)
  1950.             {
  1951.               word = bfd_h_get_32 (objfile->obfd,
  1952.                                    extab_data + addr - extab_vma);
  1953.               addr += 4;

  1954.               if ((word & 0xff000000) == 0x80000000)
  1955.                 {
  1956.                   /* Short form.  */
  1957.                   n_bytes = 3;
  1958.                 }
  1959.               else if ((word & 0xff000000) == 0x81000000
  1960.                        || (word & 0xff000000) == 0x82000000)
  1961.                 {
  1962.                   /* Long form.  */
  1963.                   n_bytes = 2;
  1964.                   n_words = ((word >> 16) & 0xff);
  1965.                 }
  1966.               else if (!(word & 0x80000000))
  1967.                 {
  1968.                   bfd_vma pers;
  1969.                   struct obj_section *pers_sec;
  1970.                   int gnu_personality = 0;

  1971.                   /* Custom personality routine.  */
  1972.                   pers = ((word & 0x7fffffff) ^ 0x40000000) - 0x40000000;
  1973.                   pers = UNMAKE_THUMB_ADDR (pers + addr - 4);

  1974.                   /* Check whether we've got one of the variants of the
  1975.                      GNU personality routines.  */
  1976.                   pers_sec = arm_obj_section_from_vma (objfile, pers);
  1977.                   if (pers_sec)
  1978.                     {
  1979.                       static const char *personality[] =
  1980.                         {
  1981.                           "__gcc_personality_v0",
  1982.                           "__gxx_personality_v0",
  1983.                           "__gcj_personality_v0",
  1984.                           "__gnu_objc_personality_v0",
  1985.                           NULL
  1986.                         };

  1987.                       CORE_ADDR pc = pers + obj_section_offset (pers_sec);
  1988.                       int k;

  1989.                       for (k = 0; personality[k]; k++)
  1990.                         if (lookup_minimal_symbol_by_pc_name
  1991.                               (pc, personality[k], objfile))
  1992.                           {
  1993.                             gnu_personality = 1;
  1994.                             break;
  1995.                           }
  1996.                     }

  1997.                   /* If so, the next word contains a word count in the high
  1998.                      byte, followed by the same unwind instructions as the
  1999.                      pre-defined forms.  */
  2000.                   if (gnu_personality
  2001.                       && addr + 4 <= extab_vma + extab_size)
  2002.                     {
  2003.                       word = bfd_h_get_32 (objfile->obfd,
  2004.                                            extab_data + addr - extab_vma);
  2005.                       addr += 4;
  2006.                       n_bytes = 3;
  2007.                       n_words = ((word >> 24) & 0xff);
  2008.                     }
  2009.                 }
  2010.             }
  2011.         }

  2012.       /* Sanity check address.  */
  2013.       if (n_words)
  2014.         if (addr < extab_vma || addr + 4 * n_words > extab_vma + extab_size)
  2015.           n_words = n_bytes = 0;

  2016.       /* The unwind instructions reside in WORD (only the N_BYTES least
  2017.          significant bytes are valid), followed by N_WORDS words in the
  2018.          extab section starting at ADDR.  */
  2019.       if (n_bytes || n_words)
  2020.         {
  2021.           gdb_byte *p = entry = obstack_alloc (&objfile->objfile_obstack,
  2022.                                                n_bytes + n_words * 4 + 1);

  2023.           while (n_bytes--)
  2024.             *p++ = (gdb_byte) ((word >> (8 * n_bytes)) & 0xff);

  2025.           while (n_words--)
  2026.             {
  2027.               word = bfd_h_get_32 (objfile->obfd,
  2028.                                    extab_data + addr - extab_vma);
  2029.               addr += 4;

  2030.               *p++ = (gdb_byte) ((word >> 24) & 0xff);
  2031.               *p++ = (gdb_byte) ((word >> 16) & 0xff);
  2032.               *p++ = (gdb_byte) ((word >> 8) & 0xff);
  2033.               *p++ = (gdb_byte) (word & 0xff);
  2034.             }

  2035.           /* Implied "Finish" to terminate the list.  */
  2036.           *p++ = 0xb0;
  2037.         }

  2038.       /* Push entry onto vector.  They are guaranteed to always
  2039.          appear in order of increasing addresses.  */
  2040.       new_exidx_entry.addr = idx;
  2041.       new_exidx_entry.entry = entry;
  2042.       VEC_safe_push (arm_exidx_entry_s,
  2043.                      data->section_maps[sec->the_bfd_section->index],
  2044.                      &new_exidx_entry);
  2045.     }

  2046.   do_cleanups (cleanups);
  2047. }

  2048. /* Search for the exception table entry covering MEMADDR.  If one is found,
  2049.    return a pointer to its data.  Otherwise, return 0.  If START is non-NULL,
  2050.    set *START to the start of the region covered by this entry.  */

  2051. static gdb_byte *
  2052. arm_find_exidx_entry (CORE_ADDR memaddr, CORE_ADDR *start)
  2053. {
  2054.   struct obj_section *sec;

  2055.   sec = find_pc_section (memaddr);
  2056.   if (sec != NULL)
  2057.     {
  2058.       struct arm_exidx_data *data;
  2059.       VEC(arm_exidx_entry_s) *map;
  2060.       struct arm_exidx_entry map_key = { memaddr - obj_section_addr (sec), 0 };
  2061.       unsigned int idx;

  2062.       data = objfile_data (sec->objfile, arm_exidx_data_key);
  2063.       if (data != NULL)
  2064.         {
  2065.           map = data->section_maps[sec->the_bfd_section->index];
  2066.           if (!VEC_empty (arm_exidx_entry_s, map))
  2067.             {
  2068.               struct arm_exidx_entry *map_sym;

  2069.               idx = VEC_lower_bound (arm_exidx_entry_s, map, &map_key,
  2070.                                      arm_compare_exidx_entries);

  2071.               /* VEC_lower_bound finds the earliest ordered insertion
  2072.                  point.  If the following symbol starts at this exact
  2073.                  address, we use that; otherwise, the preceding
  2074.                  exception table entry covers this address.  */
  2075.               if (idx < VEC_length (arm_exidx_entry_s, map))
  2076.                 {
  2077.                   map_sym = VEC_index (arm_exidx_entry_s, map, idx);
  2078.                   if (map_sym->addr == map_key.addr)
  2079.                     {
  2080.                       if (start)
  2081.                         *start = map_sym->addr + obj_section_addr (sec);
  2082.                       return map_sym->entry;
  2083.                     }
  2084.                 }

  2085.               if (idx > 0)
  2086.                 {
  2087.                   map_sym = VEC_index (arm_exidx_entry_s, map, idx - 1);
  2088.                   if (start)
  2089.                     *start = map_sym->addr + obj_section_addr (sec);
  2090.                   return map_sym->entry;
  2091.                 }
  2092.             }
  2093.         }
  2094.     }

  2095.   return NULL;
  2096. }

  2097. /* Given the current frame THIS_FRAME, and its associated frame unwinding
  2098.    instruction list from the ARM exception table entry ENTRY, allocate and
  2099.    return a prologue cache structure describing how to unwind this frame.

  2100.    Return NULL if the unwinding instruction list contains a "spare",
  2101.    "reserved" or "refuse to unwind" instruction as defined in section
  2102.    "9.3 Frame unwinding instructions" of the "Exception Handling ABI
  2103.    for the ARM Architecture" document.  */

  2104. static struct arm_prologue_cache *
  2105. arm_exidx_fill_cache (struct frame_info *this_frame, gdb_byte *entry)
  2106. {
  2107.   CORE_ADDR vsp = 0;
  2108.   int vsp_valid = 0;

  2109.   struct arm_prologue_cache *cache;
  2110.   cache = FRAME_OBSTACK_ZALLOC (struct arm_prologue_cache);
  2111.   cache->saved_regs = trad_frame_alloc_saved_regs (this_frame);

  2112.   for (;;)
  2113.     {
  2114.       gdb_byte insn;

  2115.       /* Whenever we reload SP, we actually have to retrieve its
  2116.          actual value in the current frame.  */
  2117.       if (!vsp_valid)
  2118.         {
  2119.           if (trad_frame_realreg_p (cache->saved_regs, ARM_SP_REGNUM))
  2120.             {
  2121.               int reg = cache->saved_regs[ARM_SP_REGNUM].realreg;
  2122.               vsp = get_frame_register_unsigned (this_frame, reg);
  2123.             }
  2124.           else
  2125.             {
  2126.               CORE_ADDR addr = cache->saved_regs[ARM_SP_REGNUM].addr;
  2127.               vsp = get_frame_memory_unsigned (this_frame, addr, 4);
  2128.             }

  2129.           vsp_valid = 1;
  2130.         }

  2131.       /* Decode next unwind instruction.  */
  2132.       insn = *entry++;

  2133.       if ((insn & 0xc0) == 0)
  2134.         {
  2135.           int offset = insn & 0x3f;
  2136.           vsp += (offset << 2) + 4;
  2137.         }
  2138.       else if ((insn & 0xc0) == 0x40)
  2139.         {
  2140.           int offset = insn & 0x3f;
  2141.           vsp -= (offset << 2) + 4;
  2142.         }
  2143.       else if ((insn & 0xf0) == 0x80)
  2144.         {
  2145.           int mask = ((insn & 0xf) << 8) | *entry++;
  2146.           int i;

  2147.           /* The special case of an all-zero mask identifies
  2148.              "Refuse to unwind".  We return NULL to fall back
  2149.              to the prologue analyzer.  */
  2150.           if (mask == 0)
  2151.             return NULL;

  2152.           /* Pop registers r4..r15 under mask.  */
  2153.           for (i = 0; i < 12; i++)
  2154.             if (mask & (1 << i))
  2155.               {
  2156.                 cache->saved_regs[4 + i].addr = vsp;
  2157.                 vsp += 4;
  2158.               }

  2159.           /* Special-case popping SP -- we need to reload vsp.  */
  2160.           if (mask & (1 << (ARM_SP_REGNUM - 4)))
  2161.             vsp_valid = 0;
  2162.         }
  2163.       else if ((insn & 0xf0) == 0x90)
  2164.         {
  2165.           int reg = insn & 0xf;

  2166.           /* Reserved cases.  */
  2167.           if (reg == ARM_SP_REGNUM || reg == ARM_PC_REGNUM)
  2168.             return NULL;

  2169.           /* Set SP from another register and mark VSP for reload.  */
  2170.           cache->saved_regs[ARM_SP_REGNUM] = cache->saved_regs[reg];
  2171.           vsp_valid = 0;
  2172.         }
  2173.       else if ((insn & 0xf0) == 0xa0)
  2174.         {
  2175.           int count = insn & 0x7;
  2176.           int pop_lr = (insn & 0x8) != 0;
  2177.           int i;

  2178.           /* Pop r4..r[4+count].  */
  2179.           for (i = 0; i <= count; i++)
  2180.             {
  2181.               cache->saved_regs[4 + i].addr = vsp;
  2182.               vsp += 4;
  2183.             }

  2184.           /* If indicated by flag, pop LR as well.  */
  2185.           if (pop_lr)
  2186.             {
  2187.               cache->saved_regs[ARM_LR_REGNUM].addr = vsp;
  2188.               vsp += 4;
  2189.             }
  2190.         }
  2191.       else if (insn == 0xb0)
  2192.         {
  2193.           /* We could only have updated PC by popping into it; if so, it
  2194.              will show up as address.  Otherwise, copy LR into PC.  */
  2195.           if (!trad_frame_addr_p (cache->saved_regs, ARM_PC_REGNUM))
  2196.             cache->saved_regs[ARM_PC_REGNUM]
  2197.               = cache->saved_regs[ARM_LR_REGNUM];

  2198.           /* We're done.  */
  2199.           break;
  2200.         }
  2201.       else if (insn == 0xb1)
  2202.         {
  2203.           int mask = *entry++;
  2204.           int i;

  2205.           /* All-zero mask and mask >= 16 is "spare".  */
  2206.           if (mask == 0 || mask >= 16)
  2207.             return NULL;

  2208.           /* Pop r0..r3 under mask.  */
  2209.           for (i = 0; i < 4; i++)
  2210.             if (mask & (1 << i))
  2211.               {
  2212.                 cache->saved_regs[i].addr = vsp;
  2213.                 vsp += 4;
  2214.               }
  2215.         }
  2216.       else if (insn == 0xb2)
  2217.         {
  2218.           ULONGEST offset = 0;
  2219.           unsigned shift = 0;

  2220.           do
  2221.             {
  2222.               offset |= (*entry & 0x7f) << shift;
  2223.               shift += 7;
  2224.             }
  2225.           while (*entry++ & 0x80);

  2226.           vsp += 0x204 + (offset << 2);
  2227.         }
  2228.       else if (insn == 0xb3)
  2229.         {
  2230.           int start = *entry >> 4;
  2231.           int count = (*entry++) & 0xf;
  2232.           int i;

  2233.           /* Only registers D0..D15 are valid here.  */
  2234.           if (start + count >= 16)
  2235.             return NULL;

  2236.           /* Pop VFP double-precision registers D[start]..D[start+count].  */
  2237.           for (i = 0; i <= count; i++)
  2238.             {
  2239.               cache->saved_regs[ARM_D0_REGNUM + start + i].addr = vsp;
  2240.               vsp += 8;
  2241.             }

  2242.           /* Add an extra 4 bytes for FSTMFDX-style stack.  */
  2243.           vsp += 4;
  2244.         }
  2245.       else if ((insn & 0xf8) == 0xb8)
  2246.         {
  2247.           int count = insn & 0x7;
  2248.           int i;

  2249.           /* Pop VFP double-precision registers D[8]..D[8+count].  */
  2250.           for (i = 0; i <= count; i++)
  2251.             {
  2252.               cache->saved_regs[ARM_D0_REGNUM + 8 + i].addr = vsp;
  2253.               vsp += 8;
  2254.             }

  2255.           /* Add an extra 4 bytes for FSTMFDX-style stack.  */
  2256.           vsp += 4;
  2257.         }
  2258.       else if (insn == 0xc6)
  2259.         {
  2260.           int start = *entry >> 4;
  2261.           int count = (*entry++) & 0xf;
  2262.           int i;

  2263.           /* Only registers WR0..WR15 are valid.  */
  2264.           if (start + count >= 16)
  2265.             return NULL;

  2266.           /* Pop iwmmx registers WR[start]..WR[start+count].  */
  2267.           for (i = 0; i <= count; i++)
  2268.             {
  2269.               cache->saved_regs[ARM_WR0_REGNUM + start + i].addr = vsp;
  2270.               vsp += 8;
  2271.             }
  2272.         }
  2273.       else if (insn == 0xc7)
  2274.         {
  2275.           int mask = *entry++;
  2276.           int i;

  2277.           /* All-zero mask and mask >= 16 is "spare".  */
  2278.           if (mask == 0 || mask >= 16)
  2279.             return NULL;

  2280.           /* Pop iwmmx general-purpose registers WCGR0..WCGR3 under mask.  */
  2281.           for (i = 0; i < 4; i++)
  2282.             if (mask & (1 << i))
  2283.               {
  2284.                 cache->saved_regs[ARM_WCGR0_REGNUM + i].addr = vsp;
  2285.                 vsp += 4;
  2286.               }
  2287.         }
  2288.       else if ((insn & 0xf8) == 0xc0)
  2289.         {
  2290.           int count = insn & 0x7;
  2291.           int i;

  2292.           /* Pop iwmmx registers WR[10]..WR[10+count].  */
  2293.           for (i = 0; i <= count; i++)
  2294.             {
  2295.               cache->saved_regs[ARM_WR0_REGNUM + 10 + i].addr = vsp;
  2296.               vsp += 8;
  2297.             }
  2298.         }
  2299.       else if (insn == 0xc8)
  2300.         {
  2301.           int start = *entry >> 4;
  2302.           int count = (*entry++) & 0xf;
  2303.           int i;

  2304.           /* Only registers D0..D31 are valid.  */
  2305.           if (start + count >= 16)
  2306.             return NULL;

  2307.           /* Pop VFP double-precision registers
  2308.              D[16+start]..D[16+start+count].  */
  2309.           for (i = 0; i <= count; i++)
  2310.             {
  2311.               cache->saved_regs[ARM_D0_REGNUM + 16 + start + i].addr = vsp;
  2312.               vsp += 8;
  2313.             }
  2314.         }
  2315.       else if (insn == 0xc9)
  2316.         {
  2317.           int start = *entry >> 4;
  2318.           int count = (*entry++) & 0xf;
  2319.           int i;

  2320.           /* Pop VFP double-precision registers D[start]..D[start+count].  */
  2321.           for (i = 0; i <= count; i++)
  2322.             {
  2323.               cache->saved_regs[ARM_D0_REGNUM + start + i].addr = vsp;
  2324.               vsp += 8;
  2325.             }
  2326.         }
  2327.       else if ((insn & 0xf8) == 0xd0)
  2328.         {
  2329.           int count = insn & 0x7;
  2330.           int i;

  2331.           /* Pop VFP double-precision registers D[8]..D[8+count].  */
  2332.           for (i = 0; i <= count; i++)
  2333.             {
  2334.               cache->saved_regs[ARM_D0_REGNUM + 8 + i].addr = vsp;
  2335.               vsp += 8;
  2336.             }
  2337.         }
  2338.       else
  2339.         {
  2340.           /* Everything else is "spare".  */
  2341.           return NULL;
  2342.         }
  2343.     }

  2344.   /* If we restore SP from a register, assume this was the frame register.
  2345.      Otherwise just fall back to SP as frame register.  */
  2346.   if (trad_frame_realreg_p (cache->saved_regs, ARM_SP_REGNUM))
  2347.     cache->framereg = cache->saved_regs[ARM_SP_REGNUM].realreg;
  2348.   else
  2349.     cache->framereg = ARM_SP_REGNUM;

  2350.   /* Determine offset to previous frame.  */
  2351.   cache->framesize
  2352.     = vsp - get_frame_register_unsigned (this_frame, cache->framereg);

  2353.   /* We already got the previous SP.  */
  2354.   cache->prev_sp = vsp;

  2355.   return cache;
  2356. }

  2357. /* Unwinding via ARM exception table entries.  Note that the sniffer
  2358.    already computes a filled-in prologue cache, which is then used
  2359.    with the same arm_prologue_this_id and arm_prologue_prev_register
  2360.    routines also used for prologue-parsing based unwinding.  */

  2361. static int
  2362. arm_exidx_unwind_sniffer (const struct frame_unwind *self,
  2363.                           struct frame_info *this_frame,
  2364.                           void **this_prologue_cache)
  2365. {
  2366.   struct gdbarch *gdbarch = get_frame_arch (this_frame);
  2367.   enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
  2368.   CORE_ADDR addr_in_block, exidx_region, func_start;
  2369.   struct arm_prologue_cache *cache;
  2370.   gdb_byte *entry;

  2371.   /* See if we have an ARM exception table entry covering this address.  */
  2372.   addr_in_block = get_frame_address_in_block (this_frame);
  2373.   entry = arm_find_exidx_entry (addr_in_block, &exidx_region);
  2374.   if (!entry)
  2375.     return 0;

  2376.   /* The ARM exception table does not describe unwind information
  2377.      for arbitrary PC values, but is guaranteed to be correct only
  2378.      at call sites.  We have to decide here whether we want to use
  2379.      ARM exception table information for this frame, or fall back
  2380.      to using prologue parsing.  (Note that if we have DWARF CFI,
  2381.      this sniffer isn't even called -- CFI is always preferred.)

  2382.      Before we make this decision, however, we check whether we
  2383.      actually have *symbol* information for the current frame.
  2384.      If not, prologue parsing would not work anyway, so we might
  2385.      as well use the exception table and hope for the best.  */
  2386.   if (find_pc_partial_function (addr_in_block, NULL, &func_start, NULL))
  2387.     {
  2388.       int exc_valid = 0;

  2389.       /* If the next frame is "normal", we are at a call site in this
  2390.          frame, so exception information is guaranteed to be valid.  */
  2391.       if (get_next_frame (this_frame)
  2392.           && get_frame_type (get_next_frame (this_frame)) == NORMAL_FRAME)
  2393.         exc_valid = 1;

  2394.       /* We also assume exception information is valid if we're currently
  2395.          blocked in a system call.  The system library is supposed to
  2396.          ensure this, so that e.g. pthread cancellation works.  */
  2397.       if (arm_frame_is_thumb (this_frame))
  2398.         {
  2399.           LONGEST insn;

  2400.           if (safe_read_memory_integer (get_frame_pc (this_frame) - 2, 2,
  2401.                                         byte_order_for_code, &insn)
  2402.               && (insn & 0xff00) == 0xdf00 /* svc */)
  2403.             exc_valid = 1;
  2404.         }
  2405.       else
  2406.         {
  2407.           LONGEST insn;

  2408.           if (safe_read_memory_integer (get_frame_pc (this_frame) - 4, 4,
  2409.                                         byte_order_for_code, &insn)
  2410.               && (insn & 0x0f000000) == 0x0f000000 /* svc */)
  2411.             exc_valid = 1;
  2412.         }

  2413.       /* Bail out if we don't know that exception information is valid.  */
  2414.       if (!exc_valid)
  2415.         return 0;

  2416.      /* The ARM exception index does not mark the *end* of the region
  2417.         covered by the entry, and some functions will not have any entry.
  2418.         To correctly recognize the end of the covered region, the linker
  2419.         should have inserted dummy records with a CANTUNWIND marker.

  2420.         Unfortunately, current versions of GNU ld do not reliably do
  2421.         this, and thus we may have found an incorrect entry above.
  2422.         As a (temporary) sanity check, we only use the entry if it
  2423.         lies *within* the bounds of the function.  Note that this check
  2424.         might reject perfectly valid entries that just happen to cover
  2425.         multiple functions; therefore this check ought to be removed
  2426.         once the linker is fixed.  */
  2427.       if (func_start > exidx_region)
  2428.         return 0;
  2429.     }

  2430.   /* Decode the list of unwinding instructions into a prologue cache.
  2431.      Note that this may fail due to e.g. a "refuse to unwind" code.  */
  2432.   cache = arm_exidx_fill_cache (this_frame, entry);
  2433.   if (!cache)
  2434.     return 0;

  2435.   *this_prologue_cache = cache;
  2436.   return 1;
  2437. }

  2438. struct frame_unwind arm_exidx_unwind = {
  2439.   NORMAL_FRAME,
  2440.   default_frame_unwind_stop_reason,
  2441.   arm_prologue_this_id,
  2442.   arm_prologue_prev_register,
  2443.   NULL,
  2444.   arm_exidx_unwind_sniffer
  2445. };

  2446. /* Recognize GCC's trampoline for thumb call-indirect.  If we are in a
  2447.    trampoline, return the target PC.  Otherwise return 0.

  2448.    void call0a (char c, short s, int i, long l) {}

  2449.    int main (void)
  2450.    {
  2451.      (*pointer_to_call0a) (c, s, i, l);
  2452.    }

  2453.    Instead of calling a stub library function  _call_via_xx (xx is
  2454.    the register name), GCC may inline the trampoline in the object
  2455.    file as below (register r2 has the address of call0a).

  2456.    .global main
  2457.    .type main, %function
  2458.    ...
  2459.    bl .L1
  2460.    ...
  2461.    .size main, .-main

  2462.    .L1:
  2463.    bx r2

  2464.    The trampoline 'bx r2' doesn't belong to main.  */

  2465. static CORE_ADDR
  2466. arm_skip_bx_reg (struct frame_info *frame, CORE_ADDR pc)
  2467. {
  2468.   /* The heuristics of recognizing such trampoline is that FRAME is
  2469.      executing in Thumb mode and the instruction on PC is 'bx Rm'.  */
  2470.   if (arm_frame_is_thumb (frame))
  2471.     {
  2472.       gdb_byte buf[2];

  2473.       if (target_read_memory (pc, buf, 2) == 0)
  2474.         {
  2475.           struct gdbarch *gdbarch = get_frame_arch (frame);
  2476.           enum bfd_endian byte_order_for_code
  2477.             = gdbarch_byte_order_for_code (gdbarch);
  2478.           uint16_t insn
  2479.             = extract_unsigned_integer (buf, 2, byte_order_for_code);

  2480.           if ((insn & 0xff80) == 0x4700/* bx <Rm> */
  2481.             {
  2482.               CORE_ADDR dest
  2483.                 = get_frame_register_unsigned (frame, bits (insn, 3, 6));

  2484.               /* Clear the LSB so that gdb core sets step-resume
  2485.                  breakpoint at the right address.  */
  2486.               return UNMAKE_THUMB_ADDR (dest);
  2487.             }
  2488.         }
  2489.     }

  2490.   return 0;
  2491. }

  2492. static struct arm_prologue_cache *
  2493. arm_make_stub_cache (struct frame_info *this_frame)
  2494. {
  2495.   struct arm_prologue_cache *cache;

  2496.   cache = FRAME_OBSTACK_ZALLOC (struct arm_prologue_cache);
  2497.   cache->saved_regs = trad_frame_alloc_saved_regs (this_frame);

  2498.   cache->prev_sp = get_frame_register_unsigned (this_frame, ARM_SP_REGNUM);

  2499.   return cache;
  2500. }

  2501. /* Our frame ID for a stub frame is the current SP and LR.  */

  2502. static void
  2503. arm_stub_this_id (struct frame_info *this_frame,
  2504.                   void **this_cache,
  2505.                   struct frame_id *this_id)
  2506. {
  2507.   struct arm_prologue_cache *cache;

  2508.   if (*this_cache == NULL)
  2509.     *this_cache = arm_make_stub_cache (this_frame);
  2510.   cache = *this_cache;

  2511.   *this_id = frame_id_build (cache->prev_sp, get_frame_pc (this_frame));
  2512. }

  2513. static int
  2514. arm_stub_unwind_sniffer (const struct frame_unwind *self,
  2515.                          struct frame_info *this_frame,
  2516.                          void **this_prologue_cache)
  2517. {
  2518.   CORE_ADDR addr_in_block;
  2519.   gdb_byte dummy[4];
  2520.   CORE_ADDR pc, start_addr;
  2521.   const char *name;

  2522.   addr_in_block = get_frame_address_in_block (this_frame);
  2523.   pc = get_frame_pc (this_frame);
  2524.   if (in_plt_section (addr_in_block)
  2525.       /* We also use the stub winder if the target memory is unreadable
  2526.          to avoid having the prologue unwinder trying to read it.  */
  2527.       || target_read_memory (pc, dummy, 4) != 0)
  2528.     return 1;

  2529.   if (find_pc_partial_function (pc, &name, &start_addr, NULL) == 0
  2530.       && arm_skip_bx_reg (this_frame, pc) != 0)
  2531.     return 1;

  2532.   return 0;
  2533. }

  2534. struct frame_unwind arm_stub_unwind = {
  2535.   NORMAL_FRAME,
  2536.   default_frame_unwind_stop_reason,
  2537.   arm_stub_this_id,
  2538.   arm_prologue_prev_register,
  2539.   NULL,
  2540.   arm_stub_unwind_sniffer
  2541. };

  2542. /* Put here the code to store, into CACHE->saved_regs, the addresses
  2543.    of the saved registers of frame described by THIS_FRAME.  CACHE is
  2544.    returned.  */

  2545. static struct arm_prologue_cache *
  2546. arm_m_exception_cache (struct frame_info *this_frame)
  2547. {
  2548.   struct gdbarch *gdbarch = get_frame_arch (this_frame);
  2549.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  2550.   struct arm_prologue_cache *cache;
  2551.   CORE_ADDR unwound_sp;
  2552.   LONGEST xpsr;

  2553.   cache = FRAME_OBSTACK_ZALLOC (struct arm_prologue_cache);
  2554.   cache->saved_regs = trad_frame_alloc_saved_regs (this_frame);

  2555.   unwound_sp = get_frame_register_unsigned (this_frame,
  2556.                                             ARM_SP_REGNUM);

  2557.   /* The hardware saves eight 32-bit words, comprising xPSR,
  2558.      ReturnAddress, LR (R14), R12, R3, R2, R1, R0.  See details in
  2559.      "B1.5.6 Exception entry behavior" in
  2560.      "ARMv7-M Architecture Reference Manual".  */
  2561.   cache->saved_regs[0].addr = unwound_sp;
  2562.   cache->saved_regs[1].addr = unwound_sp + 4;
  2563.   cache->saved_regs[2].addr = unwound_sp + 8;
  2564.   cache->saved_regs[3].addr = unwound_sp + 12;
  2565.   cache->saved_regs[12].addr = unwound_sp + 16;
  2566.   cache->saved_regs[14].addr = unwound_sp + 20;
  2567.   cache->saved_regs[15].addr = unwound_sp + 24;
  2568.   cache->saved_regs[ARM_PS_REGNUM].addr = unwound_sp + 28;

  2569.   /* If bit 9 of the saved xPSR is set, then there is a four-byte
  2570.      aligner between the top of the 32-byte stack frame and the
  2571.      previous context's stack pointer.  */
  2572.   cache->prev_sp = unwound_sp + 32;
  2573.   if (safe_read_memory_integer (unwound_sp + 28, 4, byte_order, &xpsr)
  2574.       && (xpsr & (1 << 9)) != 0)
  2575.     cache->prev_sp += 4;

  2576.   return cache;
  2577. }

  2578. /* Implementation of function hook 'this_id' in
  2579.    'struct frame_uwnind'.  */

  2580. static void
  2581. arm_m_exception_this_id (struct frame_info *this_frame,
  2582.                          void **this_cache,
  2583.                          struct frame_id *this_id)
  2584. {
  2585.   struct arm_prologue_cache *cache;

  2586.   if (*this_cache == NULL)
  2587.     *this_cache = arm_m_exception_cache (this_frame);
  2588.   cache = *this_cache;

  2589.   /* Our frame ID for a stub frame is the current SP and LR.  */
  2590.   *this_id = frame_id_build (cache->prev_sp,
  2591.                              get_frame_pc (this_frame));
  2592. }

  2593. /* Implementation of function hook 'prev_register' in
  2594.    'struct frame_uwnind'.  */

  2595. static struct value *
  2596. arm_m_exception_prev_register (struct frame_info *this_frame,
  2597.                                void **this_cache,
  2598.                                int prev_regnum)
  2599. {
  2600.   struct gdbarch *gdbarch = get_frame_arch (this_frame);
  2601.   struct arm_prologue_cache *cache;

  2602.   if (*this_cache == NULL)
  2603.     *this_cache = arm_m_exception_cache (this_frame);
  2604.   cache = *this_cache;

  2605.   /* The value was already reconstructed into PREV_SP.  */
  2606.   if (prev_regnum == ARM_SP_REGNUM)
  2607.     return frame_unwind_got_constant (this_frame, prev_regnum,
  2608.                                       cache->prev_sp);

  2609.   return trad_frame_get_prev_register (this_frame, cache->saved_regs,
  2610.                                        prev_regnum);
  2611. }

  2612. /* Implementation of function hook 'sniffer' in
  2613.    'struct frame_uwnind'.  */

  2614. static int
  2615. arm_m_exception_unwind_sniffer (const struct frame_unwind *self,
  2616.                                 struct frame_info *this_frame,
  2617.                                 void **this_prologue_cache)
  2618. {
  2619.   CORE_ADDR this_pc = get_frame_pc (this_frame);

  2620.   /* No need to check is_m; this sniffer is only registered for
  2621.      M-profile architectures.  */

  2622.   /* Exception frames return to one of these magic PCs.  Other values
  2623.      are not defined as of v7-M.  See details in "B1.5.8 Exception
  2624.      return behavior" in "ARMv7-M Architecture Reference Manual".  */
  2625.   if (this_pc == 0xfffffff1 || this_pc == 0xfffffff9
  2626.       || this_pc == 0xfffffffd)
  2627.     return 1;

  2628.   return 0;
  2629. }

  2630. /* Frame unwinder for M-profile exceptions.  */

  2631. struct frame_unwind arm_m_exception_unwind =
  2632. {
  2633.   SIGTRAMP_FRAME,
  2634.   default_frame_unwind_stop_reason,
  2635.   arm_m_exception_this_id,
  2636.   arm_m_exception_prev_register,
  2637.   NULL,
  2638.   arm_m_exception_unwind_sniffer
  2639. };

  2640. static CORE_ADDR
  2641. arm_normal_frame_base (struct frame_info *this_frame, void **this_cache)
  2642. {
  2643.   struct arm_prologue_cache *cache;

  2644.   if (*this_cache == NULL)
  2645.     *this_cache = arm_make_prologue_cache (this_frame);
  2646.   cache = *this_cache;

  2647.   return cache->prev_sp - cache->framesize;
  2648. }

  2649. struct frame_base arm_normal_base = {
  2650.   &arm_prologue_unwind,
  2651.   arm_normal_frame_base,
  2652.   arm_normal_frame_base,
  2653.   arm_normal_frame_base
  2654. };

  2655. /* Assuming THIS_FRAME is a dummy, return the frame ID of that
  2656.    dummy frame.  The frame ID's base needs to match the TOS value
  2657.    saved by save_dummy_frame_tos() and returned from
  2658.    arm_push_dummy_call, and the PC needs to match the dummy frame's
  2659.    breakpoint.  */

  2660. static struct frame_id
  2661. arm_dummy_id (struct gdbarch *gdbarch, struct frame_info *this_frame)
  2662. {
  2663.   return frame_id_build (get_frame_register_unsigned (this_frame,
  2664.                                                       ARM_SP_REGNUM),
  2665.                          get_frame_pc (this_frame));
  2666. }

  2667. /* Given THIS_FRAME, find the previous frame's resume PC (which will
  2668.    be used to construct the previous frame's ID, after looking up the
  2669.    containing function).  */

  2670. static CORE_ADDR
  2671. arm_unwind_pc (struct gdbarch *gdbarch, struct frame_info *this_frame)
  2672. {
  2673.   CORE_ADDR pc;
  2674.   pc = frame_unwind_register_unsigned (this_frame, ARM_PC_REGNUM);
  2675.   return arm_addr_bits_remove (gdbarch, pc);
  2676. }

  2677. static CORE_ADDR
  2678. arm_unwind_sp (struct gdbarch *gdbarch, struct frame_info *this_frame)
  2679. {
  2680.   return frame_unwind_register_unsigned (this_frame, ARM_SP_REGNUM);
  2681. }

  2682. static struct value *
  2683. arm_dwarf2_prev_register (struct frame_info *this_frame, void **this_cache,
  2684.                           int regnum)
  2685. {
  2686.   struct gdbarch * gdbarch = get_frame_arch (this_frame);
  2687.   CORE_ADDR lr, cpsr;
  2688.   ULONGEST t_bit = arm_psr_thumb_bit (gdbarch);

  2689.   switch (regnum)
  2690.     {
  2691.     case ARM_PC_REGNUM:
  2692.       /* The PC is normally copied from the return column, which
  2693.          describes saves of LR.  However, that version may have an
  2694.          extra bit set to indicate Thumb state.  The bit is not
  2695.          part of the PC.  */
  2696.       lr = frame_unwind_register_unsigned (this_frame, ARM_LR_REGNUM);
  2697.       return frame_unwind_got_constant (this_frame, regnum,
  2698.                                         arm_addr_bits_remove (gdbarch, lr));

  2699.     case ARM_PS_REGNUM:
  2700.       /* Reconstruct the T bit; see arm_prologue_prev_register for details.  */
  2701.       cpsr = get_frame_register_unsigned (this_frame, regnum);
  2702.       lr = frame_unwind_register_unsigned (this_frame, ARM_LR_REGNUM);
  2703.       if (IS_THUMB_ADDR (lr))
  2704.         cpsr |= t_bit;
  2705.       else
  2706.         cpsr &= ~t_bit;
  2707.       return frame_unwind_got_constant (this_frame, regnum, cpsr);

  2708.     default:
  2709.       internal_error (__FILE__, __LINE__,
  2710.                       _("Unexpected register %d"), regnum);
  2711.     }
  2712. }

  2713. static void
  2714. arm_dwarf2_frame_init_reg (struct gdbarch *gdbarch, int regnum,
  2715.                            struct dwarf2_frame_state_reg *reg,
  2716.                            struct frame_info *this_frame)
  2717. {
  2718.   switch (regnum)
  2719.     {
  2720.     case ARM_PC_REGNUM:
  2721.     case ARM_PS_REGNUM:
  2722.       reg->how = DWARF2_FRAME_REG_FN;
  2723.       reg->loc.fn = arm_dwarf2_prev_register;
  2724.       break;
  2725.     case ARM_SP_REGNUM:
  2726.       reg->how = DWARF2_FRAME_REG_CFA;
  2727.       break;
  2728.     }
  2729. }

  2730. /* Return true if we are in the function's epilogue, i.e. after the
  2731.    instruction that destroyed the function's stack frame.  */

  2732. static int
  2733. thumb_in_function_epilogue_p (struct gdbarch *gdbarch, CORE_ADDR pc)
  2734. {
  2735.   enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
  2736.   unsigned int insn, insn2;
  2737.   int found_return = 0, found_stack_adjust = 0;
  2738.   CORE_ADDR func_start, func_end;
  2739.   CORE_ADDR scan_pc;
  2740.   gdb_byte buf[4];

  2741.   if (!find_pc_partial_function (pc, NULL, &func_start, &func_end))
  2742.     return 0;

  2743.   /* The epilogue is a sequence of instructions along the following lines:

  2744.     - add stack frame size to SP or FP
  2745.     - [if frame pointer used] restore SP from FP
  2746.     - restore registers from SP [may include PC]
  2747.     - a return-type instruction [if PC wasn't already restored]

  2748.     In a first pass, we scan forward from the current PC and verify the
  2749.     instructions we find as compatible with this sequence, ending in a
  2750.     return instruction.

  2751.     However, this is not sufficient to distinguish indirect function calls
  2752.     within a function from indirect tail calls in the epilogue in some cases.
  2753.     Therefore, if we didn't already find any SP-changing instruction during
  2754.     forward scan, we add a backward scanning heuristic to ensure we actually
  2755.     are in the epilogue.  */

  2756.   scan_pc = pc;
  2757.   while (scan_pc < func_end && !found_return)
  2758.     {
  2759.       if (target_read_memory (scan_pc, buf, 2))
  2760.         break;

  2761.       scan_pc += 2;
  2762.       insn = extract_unsigned_integer (buf, 2, byte_order_for_code);

  2763.       if ((insn & 0xff80) == 0x4700/* bx <Rm> */
  2764.         found_return = 1;
  2765.       else if (insn == 0x46f7/* mov pc, lr */
  2766.         found_return = 1;
  2767.       else if (thumb_instruction_restores_sp (insn))
  2768.         {
  2769.           if ((insn & 0xff00) == 0xbd00/* pop <registers, PC> */
  2770.             found_return = 1;
  2771.         }
  2772.       else if (thumb_insn_size (insn) == 4/* 32-bit Thumb-2 instruction */
  2773.         {
  2774.           if (target_read_memory (scan_pc, buf, 2))
  2775.             break;

  2776.           scan_pc += 2;
  2777.           insn2 = extract_unsigned_integer (buf, 2, byte_order_for_code);

  2778.           if (insn == 0xe8bd/* ldm.w sp!, <registers> */
  2779.             {
  2780.               if (insn2 & 0x8000/* <registers> include PC.  */
  2781.                 found_return = 1;
  2782.             }
  2783.           else if (insn == 0xf85d  /* ldr.w <Rt>, [sp], #4 */
  2784.                    && (insn2 & 0x0fff) == 0x0b04)
  2785.             {
  2786.               if ((insn2 & 0xf000) == 0xf000) /* <Rt> is PC.  */
  2787.                 found_return = 1;
  2788.             }
  2789.           else if ((insn & 0xffbf) == 0xecbd  /* vldm sp!, <list> */
  2790.                    && (insn2 & 0x0e00) == 0x0a00)
  2791.             ;
  2792.           else
  2793.             break;
  2794.         }
  2795.       else
  2796.         break;
  2797.     }

  2798.   if (!found_return)
  2799.     return 0;

  2800.   /* Since any instruction in the epilogue sequence, with the possible
  2801.      exception of return itself, updates the stack pointer, we need to
  2802.      scan backwards for at most one instruction.  Try either a 16-bit or
  2803.      a 32-bit instruction.  This is just a heuristic, so we do not worry
  2804.      too much about false positives.  */

  2805.   if (pc - 4 < func_start)
  2806.     return 0;
  2807.   if (target_read_memory (pc - 4, buf, 4))
  2808.     return 0;

  2809.   insn = extract_unsigned_integer (buf, 2, byte_order_for_code);
  2810.   insn2 = extract_unsigned_integer (buf + 2, 2, byte_order_for_code);

  2811.   if (thumb_instruction_restores_sp (insn2))
  2812.     found_stack_adjust = 1;
  2813.   else if (insn == 0xe8bd/* ldm.w sp!, <registers> */
  2814.     found_stack_adjust = 1;
  2815.   else if (insn == 0xf85d  /* ldr.w <Rt>, [sp], #4 */
  2816.            && (insn2 & 0x0fff) == 0x0b04)
  2817.     found_stack_adjust = 1;
  2818.   else if ((insn & 0xffbf) == 0xecbd  /* vldm sp!, <list> */
  2819.            && (insn2 & 0x0e00) == 0x0a00)
  2820.     found_stack_adjust = 1;

  2821.   return found_stack_adjust;
  2822. }

  2823. /* Return true if we are in the function's epilogue, i.e. after the
  2824.    instruction that destroyed the function's stack frame.  */

  2825. static int
  2826. arm_in_function_epilogue_p (struct gdbarch *gdbarch, CORE_ADDR pc)
  2827. {
  2828.   enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
  2829.   unsigned int insn;
  2830.   int found_return;
  2831.   CORE_ADDR func_start, func_end;

  2832.   if (arm_pc_is_thumb (gdbarch, pc))
  2833.     return thumb_in_function_epilogue_p (gdbarch, pc);

  2834.   if (!find_pc_partial_function (pc, NULL, &func_start, &func_end))
  2835.     return 0;

  2836.   /* We are in the epilogue if the previous instruction was a stack
  2837.      adjustment and the next instruction is a possible return (bx, mov
  2838.      pc, or pop).  We could have to scan backwards to find the stack
  2839.      adjustment, or forwards to find the return, but this is a decent
  2840.      approximation.  First scan forwards.  */

  2841.   found_return = 0;
  2842.   insn = read_memory_unsigned_integer (pc, 4, byte_order_for_code);
  2843.   if (bits (insn, 28, 31) != INST_NV)
  2844.     {
  2845.       if ((insn & 0x0ffffff0) == 0x012fff10)
  2846.         /* BX.  */
  2847.         found_return = 1;
  2848.       else if ((insn & 0x0ffffff0) == 0x01a0f000)
  2849.         /* MOV PC.  */
  2850.         found_return = 1;
  2851.       else if ((insn & 0x0fff0000) == 0x08bd0000
  2852.           && (insn & 0x0000c000) != 0)
  2853.         /* POP (LDMIA), including PC or LR.  */
  2854.         found_return = 1;
  2855.     }

  2856.   if (!found_return)
  2857.     return 0;

  2858.   /* Scan backwards.  This is just a heuristic, so do not worry about
  2859.      false positives from mode changes.  */

  2860.   if (pc < func_start + 4)
  2861.     return 0;

  2862.   insn = read_memory_unsigned_integer (pc - 4, 4, byte_order_for_code);
  2863.   if (arm_instruction_restores_sp (insn))
  2864.     return 1;

  2865.   return 0;
  2866. }


  2867. /* When arguments must be pushed onto the stack, they go on in reverse
  2868.    order.  The code below implements a FILO (stack) to do this.  */

  2869. struct stack_item
  2870. {
  2871.   int len;
  2872.   struct stack_item *prev;
  2873.   void *data;
  2874. };

  2875. static struct stack_item *
  2876. push_stack_item (struct stack_item *prev, const void *contents, int len)
  2877. {
  2878.   struct stack_item *si;
  2879.   si = xmalloc (sizeof (struct stack_item));
  2880.   si->data = xmalloc (len);
  2881.   si->len = len;
  2882.   si->prev = prev;
  2883.   memcpy (si->data, contents, len);
  2884.   return si;
  2885. }

  2886. static struct stack_item *
  2887. pop_stack_item (struct stack_item *si)
  2888. {
  2889.   struct stack_item *dead = si;
  2890.   si = si->prev;
  2891.   xfree (dead->data);
  2892.   xfree (dead);
  2893.   return si;
  2894. }


  2895. /* Return the alignment (in bytes) of the given type.  */

  2896. static int
  2897. arm_type_align (struct type *t)
  2898. {
  2899.   int n;
  2900.   int align;
  2901.   int falign;

  2902.   t = check_typedef (t);
  2903.   switch (TYPE_CODE (t))
  2904.     {
  2905.     default:
  2906.       /* Should never happen.  */
  2907.       internal_error (__FILE__, __LINE__, _("unknown type alignment"));
  2908.       return 4;

  2909.     case TYPE_CODE_PTR:
  2910.     case TYPE_CODE_ENUM:
  2911.     case TYPE_CODE_INT:
  2912.     case TYPE_CODE_FLT:
  2913.     case TYPE_CODE_SET:
  2914.     case TYPE_CODE_RANGE:
  2915.     case TYPE_CODE_REF:
  2916.     case TYPE_CODE_CHAR:
  2917.     case TYPE_CODE_BOOL:
  2918.       return TYPE_LENGTH (t);

  2919.     case TYPE_CODE_ARRAY:
  2920.     case TYPE_CODE_COMPLEX:
  2921.       /* TODO: What about vector types?  */
  2922.       return arm_type_align (TYPE_TARGET_TYPE (t));

  2923.     case TYPE_CODE_STRUCT:
  2924.     case TYPE_CODE_UNION:
  2925.       align = 1;
  2926.       for (n = 0; n < TYPE_NFIELDS (t); n++)
  2927.         {
  2928.           falign = arm_type_align (TYPE_FIELD_TYPE (t, n));
  2929.           if (falign > align)
  2930.             align = falign;
  2931.         }
  2932.       return align;
  2933.     }
  2934. }

  2935. /* Possible base types for a candidate for passing and returning in
  2936.    VFP registers.  */

  2937. enum arm_vfp_cprc_base_type
  2938. {
  2939.   VFP_CPRC_UNKNOWN,
  2940.   VFP_CPRC_SINGLE,
  2941.   VFP_CPRC_DOUBLE,
  2942.   VFP_CPRC_VEC64,
  2943.   VFP_CPRC_VEC128
  2944. };

  2945. /* The length of one element of base type B.  */

  2946. static unsigned
  2947. arm_vfp_cprc_unit_length (enum arm_vfp_cprc_base_type b)
  2948. {
  2949.   switch (b)
  2950.     {
  2951.     case VFP_CPRC_SINGLE:
  2952.       return 4;
  2953.     case VFP_CPRC_DOUBLE:
  2954.       return 8;
  2955.     case VFP_CPRC_VEC64:
  2956.       return 8;
  2957.     case VFP_CPRC_VEC128:
  2958.       return 16;
  2959.     default:
  2960.       internal_error (__FILE__, __LINE__, _("Invalid VFP CPRC type: %d."),
  2961.                       (int) b);
  2962.     }
  2963. }

  2964. /* The character ('s', 'd' or 'q') for the type of VFP register used
  2965.    for passing base type B.  */

  2966. static int
  2967. arm_vfp_cprc_reg_char (enum arm_vfp_cprc_base_type b)
  2968. {
  2969.   switch (b)
  2970.     {
  2971.     case VFP_CPRC_SINGLE:
  2972.       return 's';
  2973.     case VFP_CPRC_DOUBLE:
  2974.       return 'd';
  2975.     case VFP_CPRC_VEC64:
  2976.       return 'd';
  2977.     case VFP_CPRC_VEC128:
  2978.       return 'q';
  2979.     default:
  2980.       internal_error (__FILE__, __LINE__, _("Invalid VFP CPRC type: %d."),
  2981.                       (int) b);
  2982.     }
  2983. }

  2984. /* Determine whether T may be part of a candidate for passing and
  2985.    returning in VFP registers, ignoring the limit on the total number
  2986.    of components.  If *BASE_TYPE is VFP_CPRC_UNKNOWN, set it to the
  2987.    classification of the first valid component found; if it is not
  2988.    VFP_CPRC_UNKNOWN, all components must have the same classification
  2989.    as *BASE_TYPE.  If it is found that T contains a type not permitted
  2990.    for passing and returning in VFP registers, a type differently
  2991.    classified from *BASE_TYPE, or two types differently classified
  2992.    from each other, return -1, otherwise return the total number of
  2993.    base-type elements found (possibly 0 in an empty structure or
  2994.    array).  Vector types are not currently supported, matching the
  2995.    generic AAPCS support.  */

  2996. static int
  2997. arm_vfp_cprc_sub_candidate (struct type *t,
  2998.                             enum arm_vfp_cprc_base_type *base_type)
  2999. {
  3000.   t = check_typedef (t);
  3001.   switch (TYPE_CODE (t))
  3002.     {
  3003.     case TYPE_CODE_FLT:
  3004.       switch (TYPE_LENGTH (t))
  3005.         {
  3006.         case 4:
  3007.           if (*base_type == VFP_CPRC_UNKNOWN)
  3008.             *base_type = VFP_CPRC_SINGLE;
  3009.           else if (*base_type != VFP_CPRC_SINGLE)
  3010.             return -1;
  3011.           return 1;

  3012.         case 8:
  3013.           if (*base_type == VFP_CPRC_UNKNOWN)
  3014.             *base_type = VFP_CPRC_DOUBLE;
  3015.           else if (*base_type != VFP_CPRC_DOUBLE)
  3016.             return -1;
  3017.           return 1;

  3018.         default:
  3019.           return -1;
  3020.         }
  3021.       break;

  3022.     case TYPE_CODE_COMPLEX:
  3023.       /* Arguments of complex T where T is one of the types float or
  3024.          double get treated as if they are implemented as:

  3025.          struct complexT
  3026.          {
  3027.            T real;
  3028.            T imag;
  3029.          };

  3030.       */
  3031.       switch (TYPE_LENGTH (t))
  3032.         {
  3033.         case 8:
  3034.           if (*base_type == VFP_CPRC_UNKNOWN)
  3035.             *base_type = VFP_CPRC_SINGLE;
  3036.           else if (*base_type != VFP_CPRC_SINGLE)
  3037.             return -1;
  3038.           return 2;

  3039.         case 16:
  3040.           if (*base_type == VFP_CPRC_UNKNOWN)
  3041.             *base_type = VFP_CPRC_DOUBLE;
  3042.           else if (*base_type != VFP_CPRC_DOUBLE)
  3043.             return -1;
  3044.           return 2;

  3045.         default:
  3046.           return -1;
  3047.         }
  3048.       break;

  3049.     case TYPE_CODE_ARRAY:
  3050.       {
  3051.         int count;
  3052.         unsigned unitlen;
  3053.         count = arm_vfp_cprc_sub_candidate (TYPE_TARGET_TYPE (t), base_type);
  3054.         if (count == -1)
  3055.           return -1;
  3056.         if (TYPE_LENGTH (t) == 0)
  3057.           {
  3058.             gdb_assert (count == 0);
  3059.             return 0;
  3060.           }
  3061.         else if (count == 0)
  3062.           return -1;
  3063.         unitlen = arm_vfp_cprc_unit_length (*base_type);
  3064.         gdb_assert ((TYPE_LENGTH (t) % unitlen) == 0);
  3065.         return TYPE_LENGTH (t) / unitlen;
  3066.       }
  3067.       break;

  3068.     case TYPE_CODE_STRUCT:
  3069.       {
  3070.         int count = 0;
  3071.         unsigned unitlen;
  3072.         int i;
  3073.         for (i = 0; i < TYPE_NFIELDS (t); i++)
  3074.           {
  3075.             int sub_count = arm_vfp_cprc_sub_candidate (TYPE_FIELD_TYPE (t, i),
  3076.                                                         base_type);
  3077.             if (sub_count == -1)
  3078.               return -1;
  3079.             count += sub_count;
  3080.           }
  3081.         if (TYPE_LENGTH (t) == 0)
  3082.           {
  3083.             gdb_assert (count == 0);
  3084.             return 0;
  3085.           }
  3086.         else if (count == 0)
  3087.           return -1;
  3088.         unitlen = arm_vfp_cprc_unit_length (*base_type);
  3089.         if (TYPE_LENGTH (t) != unitlen * count)
  3090.           return -1;
  3091.         return count;
  3092.       }

  3093.     case TYPE_CODE_UNION:
  3094.       {
  3095.         int count = 0;
  3096.         unsigned unitlen;
  3097.         int i;
  3098.         for (i = 0; i < TYPE_NFIELDS (t); i++)
  3099.           {
  3100.             int sub_count = arm_vfp_cprc_sub_candidate (TYPE_FIELD_TYPE (t, i),
  3101.                                                         base_type);
  3102.             if (sub_count == -1)
  3103.               return -1;
  3104.             count = (count > sub_count ? count : sub_count);
  3105.           }
  3106.         if (TYPE_LENGTH (t) == 0)
  3107.           {
  3108.             gdb_assert (count == 0);
  3109.             return 0;
  3110.           }
  3111.         else if (count == 0)
  3112.           return -1;
  3113.         unitlen = arm_vfp_cprc_unit_length (*base_type);
  3114.         if (TYPE_LENGTH (t) != unitlen * count)
  3115.           return -1;
  3116.         return count;
  3117.       }

  3118.     default:
  3119.       break;
  3120.     }

  3121.   return -1;
  3122. }

  3123. /* Determine whether T is a VFP co-processor register candidate (CPRC)
  3124.    if passed to or returned from a non-variadic function with the VFP
  3125.    ABI in effect.  Return 1 if it is, 0 otherwise.  If it is, set
  3126.    *BASE_TYPE to the base type for T and *COUNT to the number of
  3127.    elements of that base type before returning.  */

  3128. static int
  3129. arm_vfp_call_candidate (struct type *t, enum arm_vfp_cprc_base_type *base_type,
  3130.                         int *count)
  3131. {
  3132.   enum arm_vfp_cprc_base_type b = VFP_CPRC_UNKNOWN;
  3133.   int c = arm_vfp_cprc_sub_candidate (t, &b);
  3134.   if (c <= 0 || c > 4)
  3135.     return 0;
  3136.   *base_type = b;
  3137.   *count = c;
  3138.   return 1;
  3139. }

  3140. /* Return 1 if the VFP ABI should be used for passing arguments to and
  3141.    returning values from a function of type FUNC_TYPE, 0
  3142.    otherwise.  */

  3143. static int
  3144. arm_vfp_abi_for_function (struct gdbarch *gdbarch, struct type *func_type)
  3145. {
  3146.   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
  3147.   /* Variadic functions always use the base ABI.  Assume that functions
  3148.      without debug info are not variadic.  */
  3149.   if (func_type && TYPE_VARARGS (check_typedef (func_type)))
  3150.     return 0;
  3151.   /* The VFP ABI is only supported as a variant of AAPCS.  */
  3152.   if (tdep->arm_abi != ARM_ABI_AAPCS)
  3153.     return 0;
  3154.   return gdbarch_tdep (gdbarch)->fp_model == ARM_FLOAT_VFP;
  3155. }

  3156. /* We currently only support passing parameters in integer registers, which
  3157.    conforms with GCC's default model, and VFP argument passing following
  3158.    the VFP variant of AAPCS.  Several other variants exist and
  3159.    we should probably support some of them based on the selected ABI.  */

  3160. static CORE_ADDR
  3161. arm_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
  3162.                      struct regcache *regcache, CORE_ADDR bp_addr, int nargs,
  3163.                      struct value **args, CORE_ADDR sp, int struct_return,
  3164.                      CORE_ADDR struct_addr)
  3165. {
  3166.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  3167.   int argnum;
  3168.   int argreg;
  3169.   int nstack;
  3170.   struct stack_item *si = NULL;
  3171.   int use_vfp_abi;
  3172.   struct type *ftype;
  3173.   unsigned vfp_regs_free = (1 << 16) - 1;

  3174.   /* Determine the type of this function and whether the VFP ABI
  3175.      applies.  */
  3176.   ftype = check_typedef (value_type (function));
  3177.   if (TYPE_CODE (ftype) == TYPE_CODE_PTR)
  3178.     ftype = check_typedef (TYPE_TARGET_TYPE (ftype));
  3179.   use_vfp_abi = arm_vfp_abi_for_function (gdbarch, ftype);

  3180.   /* Set the return address.  For the ARM, the return breakpoint is
  3181.      always at BP_ADDR.  */
  3182.   if (arm_pc_is_thumb (gdbarch, bp_addr))
  3183.     bp_addr |= 1;
  3184.   regcache_cooked_write_unsigned (regcache, ARM_LR_REGNUM, bp_addr);

  3185.   /* Walk through the list of args and determine how large a temporary
  3186.      stack is required.  Need to take care here as structs may be
  3187.      passed on the stack, and we have to push them.  */
  3188.   nstack = 0;

  3189.   argreg = ARM_A1_REGNUM;
  3190.   nstack = 0;

  3191.   /* The struct_return pointer occupies the first parameter
  3192.      passing register.  */
  3193.   if (struct_return)
  3194.     {
  3195.       if (arm_debug)
  3196.         fprintf_unfiltered (gdb_stdlog, "struct return in %s = %s\n",
  3197.                             gdbarch_register_name (gdbarch, argreg),
  3198.                             paddress (gdbarch, struct_addr));
  3199.       regcache_cooked_write_unsigned (regcache, argreg, struct_addr);
  3200.       argreg++;
  3201.     }

  3202.   for (argnum = 0; argnum < nargs; argnum++)
  3203.     {
  3204.       int len;
  3205.       struct type *arg_type;
  3206.       struct type *target_type;
  3207.       enum type_code typecode;
  3208.       const bfd_byte *val;
  3209.       int align;
  3210.       enum arm_vfp_cprc_base_type vfp_base_type;
  3211.       int vfp_base_count;
  3212.       int may_use_core_reg = 1;

  3213.       arg_type = check_typedef (value_type (args[argnum]));
  3214.       len = TYPE_LENGTH (arg_type);
  3215.       target_type = TYPE_TARGET_TYPE (arg_type);
  3216.       typecode = TYPE_CODE (arg_type);
  3217.       val = value_contents (args[argnum]);

  3218.       align = arm_type_align (arg_type);
  3219.       /* Round alignment up to a whole number of words.  */
  3220.       align = (align + INT_REGISTER_SIZE - 1) & ~(INT_REGISTER_SIZE - 1);
  3221.       /* Different ABIs have different maximum alignments.  */
  3222.       if (gdbarch_tdep (gdbarch)->arm_abi == ARM_ABI_APCS)
  3223.         {
  3224.           /* The APCS ABI only requires word alignment.  */
  3225.           align = INT_REGISTER_SIZE;
  3226.         }
  3227.       else
  3228.         {
  3229.           /* The AAPCS requires at most doubleword alignment.  */
  3230.           if (align > INT_REGISTER_SIZE * 2)
  3231.             align = INT_REGISTER_SIZE * 2;
  3232.         }

  3233.       if (use_vfp_abi
  3234.           && arm_vfp_call_candidate (arg_type, &vfp_base_type,
  3235.                                      &vfp_base_count))
  3236.         {
  3237.           int regno;
  3238.           int unit_length;
  3239.           int shift;
  3240.           unsigned mask;

  3241.           /* Because this is a CPRC it cannot go in a core register or
  3242.              cause a core register to be skipped for alignment.
  3243.              Either it goes in VFP registers and the rest of this loop
  3244.              iteration is skipped for this argument, or it goes on the
  3245.              stack (and the stack alignment code is correct for this
  3246.              case).  */
  3247.           may_use_core_reg = 0;

  3248.           unit_length = arm_vfp_cprc_unit_length (vfp_base_type);
  3249.           shift = unit_length / 4;
  3250.           mask = (1 << (shift * vfp_base_count)) - 1;
  3251.           for (regno = 0; regno < 16; regno += shift)
  3252.             if (((vfp_regs_free >> regno) & mask) == mask)
  3253.               break;

  3254.           if (regno < 16)
  3255.             {
  3256.               int reg_char;
  3257.               int reg_scaled;
  3258.               int i;

  3259.               vfp_regs_free &= ~(mask << regno);
  3260.               reg_scaled = regno / shift;
  3261.               reg_char = arm_vfp_cprc_reg_char (vfp_base_type);
  3262.               for (i = 0; i < vfp_base_count; i++)
  3263.                 {
  3264.                   char name_buf[4];
  3265.                   int regnum;
  3266.                   if (reg_char == 'q')
  3267.                     arm_neon_quad_write (gdbarch, regcache, reg_scaled + i,
  3268.                                          val + i * unit_length);
  3269.                   else
  3270.                     {
  3271.                       xsnprintf (name_buf, sizeof (name_buf), "%c%d",
  3272.                                  reg_char, reg_scaled + i);
  3273.                       regnum = user_reg_map_name_to_regnum (gdbarch, name_buf,
  3274.                                                             strlen (name_buf));
  3275.                       regcache_cooked_write (regcache, regnum,
  3276.                                              val + i * unit_length);
  3277.                     }
  3278.                 }
  3279.               continue;
  3280.             }
  3281.           else
  3282.             {
  3283.               /* This CPRC could not go in VFP registers, so all VFP
  3284.                  registers are now marked as used.  */
  3285.               vfp_regs_free = 0;
  3286.             }
  3287.         }

  3288.       /* Push stack padding for dowubleword alignment.  */
  3289.       if (nstack & (align - 1))
  3290.         {
  3291.           si = push_stack_item (si, val, INT_REGISTER_SIZE);
  3292.           nstack += INT_REGISTER_SIZE;
  3293.         }

  3294.       /* Doubleword aligned quantities must go in even register pairs.  */
  3295.       if (may_use_core_reg
  3296.           && argreg <= ARM_LAST_ARG_REGNUM
  3297.           && align > INT_REGISTER_SIZE
  3298.           && argreg & 1)
  3299.         argreg++;

  3300.       /* If the argument is a pointer to a function, and it is a
  3301.          Thumb function, create a LOCAL copy of the value and set
  3302.          the THUMB bit in it.  */
  3303.       if (TYPE_CODE_PTR == typecode
  3304.           && target_type != NULL
  3305.           && TYPE_CODE_FUNC == TYPE_CODE (check_typedef (target_type)))
  3306.         {
  3307.           CORE_ADDR regval = extract_unsigned_integer (val, len, byte_order);
  3308.           if (arm_pc_is_thumb (gdbarch, regval))
  3309.             {
  3310.               bfd_byte *copy = alloca (len);
  3311.               store_unsigned_integer (copy, len, byte_order,
  3312.                                       MAKE_THUMB_ADDR (regval));
  3313.               val = copy;
  3314.             }
  3315.         }

  3316.       /* Copy the argument to general registers or the stack in
  3317.          register-sized pieces.  Large arguments are split between
  3318.          registers and stack.  */
  3319.       while (len > 0)
  3320.         {
  3321.           int partial_len = len < INT_REGISTER_SIZE ? len : INT_REGISTER_SIZE;

  3322.           if (may_use_core_reg && argreg <= ARM_LAST_ARG_REGNUM)
  3323.             {
  3324.               /* The argument is being passed in a general purpose
  3325.                  register.  */
  3326.               CORE_ADDR regval
  3327.                 = extract_unsigned_integer (val, partial_len, byte_order);
  3328.               if (byte_order == BFD_ENDIAN_BIG)
  3329.                 regval <<= (INT_REGISTER_SIZE - partial_len) * 8;
  3330.               if (arm_debug)
  3331.                 fprintf_unfiltered (gdb_stdlog, "arg %d in %s = 0x%s\n",
  3332.                                     argnum,
  3333.                                     gdbarch_register_name
  3334.                                       (gdbarch, argreg),
  3335.                                     phex (regval, INT_REGISTER_SIZE));
  3336.               regcache_cooked_write_unsigned (regcache, argreg, regval);
  3337.               argreg++;
  3338.             }
  3339.           else
  3340.             {
  3341.               /* Push the arguments onto the stack.  */
  3342.               if (arm_debug)
  3343.                 fprintf_unfiltered (gdb_stdlog, "arg %d @ sp + %d\n",
  3344.                                     argnum, nstack);
  3345.               si = push_stack_item (si, val, INT_REGISTER_SIZE);
  3346.               nstack += INT_REGISTER_SIZE;
  3347.             }

  3348.           len -= partial_len;
  3349.           val += partial_len;
  3350.         }
  3351.     }
  3352.   /* If we have an odd number of words to push, then decrement the stack
  3353.      by one word now, so first stack argument will be dword aligned.  */
  3354.   if (nstack & 4)
  3355.     sp -= 4;

  3356.   while (si)
  3357.     {
  3358.       sp -= si->len;
  3359.       write_memory (sp, si->data, si->len);
  3360.       si = pop_stack_item (si);
  3361.     }

  3362.   /* Finally, update teh SP register.  */
  3363.   regcache_cooked_write_unsigned (regcache, ARM_SP_REGNUM, sp);

  3364.   return sp;
  3365. }


  3366. /* Always align the frame to an 8-byte boundary.  This is required on
  3367.    some platforms and harmless on the rest.  */

  3368. static CORE_ADDR
  3369. arm_frame_align (struct gdbarch *gdbarch, CORE_ADDR sp)
  3370. {
  3371.   /* Align the stack to eight bytes.  */
  3372.   return sp & ~ (CORE_ADDR) 7;
  3373. }

  3374. static void
  3375. print_fpu_flags (struct ui_file *file, int flags)
  3376. {
  3377.   if (flags & (1 << 0))
  3378.     fputs_filtered ("IVO ", file);
  3379.   if (flags & (1 << 1))
  3380.     fputs_filtered ("DVZ ", file);
  3381.   if (flags & (1 << 2))
  3382.     fputs_filtered ("OFL ", file);
  3383.   if (flags & (1 << 3))
  3384.     fputs_filtered ("UFL ", file);
  3385.   if (flags & (1 << 4))
  3386.     fputs_filtered ("INX ", file);
  3387.   fputc_filtered ('\n', file);
  3388. }

  3389. /* Print interesting information about the floating point processor
  3390.    (if present) or emulator.  */
  3391. static void
  3392. arm_print_float_info (struct gdbarch *gdbarch, struct ui_file *file,
  3393.                       struct frame_info *frame, const char *args)
  3394. {
  3395.   unsigned long status = get_frame_register_unsigned (frame, ARM_FPS_REGNUM);
  3396.   int type;

  3397.   type = (status >> 24) & 127;
  3398.   if (status & (1 << 31))
  3399.     fprintf_filtered (file, _("Hardware FPU type %d\n"), type);
  3400.   else
  3401.     fprintf_filtered (file, _("Software FPU type %d\n"), type);
  3402.   /* i18n: [floating point unit] mask */
  3403.   fputs_filtered (_("mask: "), file);
  3404.   print_fpu_flags (file, status >> 16);
  3405.   /* i18n: [floating point unit] flags */
  3406.   fputs_filtered (_("flags: "), file);
  3407.   print_fpu_flags (file, status);
  3408. }

  3409. /* Construct the ARM extended floating point type.  */
  3410. static struct type *
  3411. arm_ext_type (struct gdbarch *gdbarch)
  3412. {
  3413.   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);

  3414.   if (!tdep->arm_ext_type)
  3415.     tdep->arm_ext_type
  3416.       = arch_float_type (gdbarch, -1, "builtin_type_arm_ext",
  3417.                          floatformats_arm_ext);

  3418.   return tdep->arm_ext_type;
  3419. }

  3420. static struct type *
  3421. arm_neon_double_type (struct gdbarch *gdbarch)
  3422. {
  3423.   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);

  3424.   if (tdep->neon_double_type == NULL)
  3425.     {
  3426.       struct type *t, *elem;

  3427.       t = arch_composite_type (gdbarch, "__gdb_builtin_type_neon_d",
  3428.                                TYPE_CODE_UNION);
  3429.       elem = builtin_type (gdbarch)->builtin_uint8;
  3430.       append_composite_type_field (t, "u8", init_vector_type (elem, 8));
  3431.       elem = builtin_type (gdbarch)->builtin_uint16;
  3432.       append_composite_type_field (t, "u16", init_vector_type (elem, 4));
  3433.       elem = builtin_type (gdbarch)->builtin_uint32;
  3434.       append_composite_type_field (t, "u32", init_vector_type (elem, 2));
  3435.       elem = builtin_type (gdbarch)->builtin_uint64;
  3436.       append_composite_type_field (t, "u64", elem);
  3437.       elem = builtin_type (gdbarch)->builtin_float;
  3438.       append_composite_type_field (t, "f32", init_vector_type (elem, 2));
  3439.       elem = builtin_type (gdbarch)->builtin_double;
  3440.       append_composite_type_field (t, "f64", elem);

  3441.       TYPE_VECTOR (t) = 1;
  3442.       TYPE_NAME (t) = "neon_d";
  3443.       tdep->neon_double_type = t;
  3444.     }

  3445.   return tdep->neon_double_type;
  3446. }

  3447. /* FIXME: The vector types are not correctly ordered on big-endian
  3448.    targets.  Just as s0 is the low bits of d0, d0[0] is also the low
  3449.    bits of d0 - regardless of what unit size is being held in d0.  So
  3450.    the offset of the first uint8 in d0 is 7, but the offset of the
  3451.    first float is 4.  This code works as-is for little-endian
  3452.    targets.  */

  3453. static struct type *
  3454. arm_neon_quad_type (struct gdbarch *gdbarch)
  3455. {
  3456.   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);

  3457.   if (tdep->neon_quad_type == NULL)
  3458.     {
  3459.       struct type *t, *elem;

  3460.       t = arch_composite_type (gdbarch, "__gdb_builtin_type_neon_q",
  3461.                                TYPE_CODE_UNION);
  3462.       elem = builtin_type (gdbarch)->builtin_uint8;
  3463.       append_composite_type_field (t, "u8", init_vector_type (elem, 16));
  3464.       elem = builtin_type (gdbarch)->builtin_uint16;
  3465.       append_composite_type_field (t, "u16", init_vector_type (elem, 8));
  3466.       elem = builtin_type (gdbarch)->builtin_uint32;
  3467.       append_composite_type_field (t, "u32", init_vector_type (elem, 4));
  3468.       elem = builtin_type (gdbarch)->builtin_uint64;
  3469.       append_composite_type_field (t, "u64", init_vector_type (elem, 2));
  3470.       elem = builtin_type (gdbarch)->builtin_float;
  3471.       append_composite_type_field (t, "f32", init_vector_type (elem, 4));
  3472.       elem = builtin_type (gdbarch)->builtin_double;
  3473.       append_composite_type_field (t, "f64", init_vector_type (elem, 2));

  3474.       TYPE_VECTOR (t) = 1;
  3475.       TYPE_NAME (t) = "neon_q";
  3476.       tdep->neon_quad_type = t;
  3477.     }

  3478.   return tdep->neon_quad_type;
  3479. }

  3480. /* Return the GDB type object for the "standard" data type of data in
  3481.    register N.  */

  3482. static struct type *
  3483. arm_register_type (struct gdbarch *gdbarch, int regnum)
  3484. {
  3485.   int num_regs = gdbarch_num_regs (gdbarch);

  3486.   if (gdbarch_tdep (gdbarch)->have_vfp_pseudos
  3487.       && regnum >= num_regs && regnum < num_regs + 32)
  3488.     return builtin_type (gdbarch)->builtin_float;

  3489.   if (gdbarch_tdep (gdbarch)->have_neon_pseudos
  3490.       && regnum >= num_regs + 32 && regnum < num_regs + 32 + 16)
  3491.     return arm_neon_quad_type (gdbarch);

  3492.   /* If the target description has register information, we are only
  3493.      in this function so that we can override the types of
  3494.      double-precision registers for NEON.  */
  3495.   if (tdesc_has_registers (gdbarch_target_desc (gdbarch)))
  3496.     {
  3497.       struct type *t = tdesc_register_type (gdbarch, regnum);

  3498.       if (regnum >= ARM_D0_REGNUM && regnum < ARM_D0_REGNUM + 32
  3499.           && TYPE_CODE (t) == TYPE_CODE_FLT
  3500.           && gdbarch_tdep (gdbarch)->have_neon)
  3501.         return arm_neon_double_type (gdbarch);
  3502.       else
  3503.         return t;
  3504.     }

  3505.   if (regnum >= ARM_F0_REGNUM && regnum < ARM_F0_REGNUM + NUM_FREGS)
  3506.     {
  3507.       if (!gdbarch_tdep (gdbarch)->have_fpa_registers)
  3508.         return builtin_type (gdbarch)->builtin_void;

  3509.       return arm_ext_type (gdbarch);
  3510.     }
  3511.   else if (regnum == ARM_SP_REGNUM)
  3512.     return builtin_type (gdbarch)->builtin_data_ptr;
  3513.   else if (regnum == ARM_PC_REGNUM)
  3514.     return builtin_type (gdbarch)->builtin_func_ptr;
  3515.   else if (regnum >= ARRAY_SIZE (arm_register_names))
  3516.     /* These registers are only supported on targets which supply
  3517.        an XML description.  */
  3518.     return builtin_type (gdbarch)->builtin_int0;
  3519.   else
  3520.     return builtin_type (gdbarch)->builtin_uint32;
  3521. }

  3522. /* Map a DWARF register REGNUM onto the appropriate GDB register
  3523.    number.  */

  3524. static int
  3525. arm_dwarf_reg_to_regnum (struct gdbarch *gdbarch, int reg)
  3526. {
  3527.   /* Core integer regs.  */
  3528.   if (reg >= 0 && reg <= 15)
  3529.     return reg;

  3530.   /* Legacy FPA encoding.  These were once used in a way which
  3531.      overlapped with VFP register numbering, so their use is
  3532.      discouraged, but GDB doesn't support the ARM toolchain
  3533.      which used them for VFP.  */
  3534.   if (reg >= 16 && reg <= 23)
  3535.     return ARM_F0_REGNUM + reg - 16;

  3536.   /* New assignments for the FPA registers.  */
  3537.   if (reg >= 96 && reg <= 103)
  3538.     return ARM_F0_REGNUM + reg - 96;

  3539.   /* WMMX register assignments.  */
  3540.   if (reg >= 104 && reg <= 111)
  3541.     return ARM_WCGR0_REGNUM + reg - 104;

  3542.   if (reg >= 112 && reg <= 127)
  3543.     return ARM_WR0_REGNUM + reg - 112;

  3544.   if (reg >= 192 && reg <= 199)
  3545.     return ARM_WC0_REGNUM + reg - 192;

  3546.   /* VFP v2 registersA double precision value is actually
  3547.      in d1 rather than s2, but the ABI only defines numbering
  3548.      for the single precision registers.  This will "just work"
  3549.      in GDB for little endian targets (we'll read eight bytes,
  3550.      starting in s0 and then progressing to s1), but will be
  3551.      reversed on big endian targets with VFP.  This won't
  3552.      be a problem for the new Neon quad registers; you're supposed
  3553.      to use DW_OP_piece for those.  */
  3554.   if (reg >= 64 && reg <= 95)
  3555.     {
  3556.       char name_buf[4];

  3557.       xsnprintf (name_buf, sizeof (name_buf), "s%d", reg - 64);
  3558.       return user_reg_map_name_to_regnum (gdbarch, name_buf,
  3559.                                           strlen (name_buf));
  3560.     }

  3561.   /* VFP v3 / Neon registers.  This range is also used for VFP v2
  3562.      registers, except that it now describes d0 instead of s0.  */
  3563.   if (reg >= 256 && reg <= 287)
  3564.     {
  3565.       char name_buf[4];

  3566.       xsnprintf (name_buf, sizeof (name_buf), "d%d", reg - 256);
  3567.       return user_reg_map_name_to_regnum (gdbarch, name_buf,
  3568.                                           strlen (name_buf));
  3569.     }

  3570.   return -1;
  3571. }

  3572. /* Map GDB internal REGNUM onto the Arm simulator register numbers.  */
  3573. static int
  3574. arm_register_sim_regno (struct gdbarch *gdbarch, int regnum)
  3575. {
  3576.   int reg = regnum;
  3577.   gdb_assert (reg >= 0 && reg < gdbarch_num_regs (gdbarch));

  3578.   if (regnum >= ARM_WR0_REGNUM && regnum <= ARM_WR15_REGNUM)
  3579.     return regnum - ARM_WR0_REGNUM + SIM_ARM_IWMMXT_COP0R0_REGNUM;

  3580.   if (regnum >= ARM_WC0_REGNUM && regnum <= ARM_WC7_REGNUM)
  3581.     return regnum - ARM_WC0_REGNUM + SIM_ARM_IWMMXT_COP1R0_REGNUM;

  3582.   if (regnum >= ARM_WCGR0_REGNUM && regnum <= ARM_WCGR7_REGNUM)
  3583.     return regnum - ARM_WCGR0_REGNUM + SIM_ARM_IWMMXT_COP1R8_REGNUM;

  3584.   if (reg < NUM_GREGS)
  3585.     return SIM_ARM_R0_REGNUM + reg;
  3586.   reg -= NUM_GREGS;

  3587.   if (reg < NUM_FREGS)
  3588.     return SIM_ARM_FP0_REGNUM + reg;
  3589.   reg -= NUM_FREGS;

  3590.   if (reg < NUM_SREGS)
  3591.     return SIM_ARM_FPS_REGNUM + reg;
  3592.   reg -= NUM_SREGS;

  3593.   internal_error (__FILE__, __LINE__, _("Bad REGNUM %d"), regnum);
  3594. }

  3595. /* NOTE: cagney/2001-08-20: Both convert_from_extended() and
  3596.    convert_to_extended() use floatformat_arm_ext_littlebyte_bigword.
  3597.    It is thought that this is is the floating-point register format on
  3598.    little-endian systems.  */

  3599. static void
  3600. convert_from_extended (const struct floatformat *fmt, const void *ptr,
  3601.                        void *dbl, int endianess)
  3602. {
  3603.   DOUBLEST d;

  3604.   if (endianess == BFD_ENDIAN_BIG)
  3605.     floatformat_to_doublest (&floatformat_arm_ext_big, ptr, &d);
  3606.   else
  3607.     floatformat_to_doublest (&floatformat_arm_ext_littlebyte_bigword,
  3608.                              ptr, &d);
  3609.   floatformat_from_doublest (fmt, &d, dbl);
  3610. }

  3611. static void
  3612. convert_to_extended (const struct floatformat *fmt, void *dbl, const void *ptr,
  3613.                      int endianess)
  3614. {
  3615.   DOUBLEST d;

  3616.   floatformat_to_doublest (fmt, ptr, &d);
  3617.   if (endianess == BFD_ENDIAN_BIG)
  3618.     floatformat_from_doublest (&floatformat_arm_ext_big, &d, dbl);
  3619.   else
  3620.     floatformat_from_doublest (&floatformat_arm_ext_littlebyte_bigword,
  3621.                                &d, dbl);
  3622. }

  3623. static int
  3624. condition_true (unsigned long cond, unsigned long status_reg)
  3625. {
  3626.   if (cond == INST_AL || cond == INST_NV)
  3627.     return 1;

  3628.   switch (cond)
  3629.     {
  3630.     case INST_EQ:
  3631.       return ((status_reg & FLAG_Z) != 0);
  3632.     case INST_NE:
  3633.       return ((status_reg & FLAG_Z) == 0);
  3634.     case INST_CS:
  3635.       return ((status_reg & FLAG_C) != 0);
  3636.     case INST_CC:
  3637.       return ((status_reg & FLAG_C) == 0);
  3638.     case INST_MI:
  3639.       return ((status_reg & FLAG_N) != 0);
  3640.     case INST_PL:
  3641.       return ((status_reg & FLAG_N) == 0);
  3642.     case INST_VS:
  3643.       return ((status_reg & FLAG_V) != 0);
  3644.     case INST_VC:
  3645.       return ((status_reg & FLAG_V) == 0);
  3646.     case INST_HI:
  3647.       return ((status_reg & (FLAG_C | FLAG_Z)) == FLAG_C);
  3648.     case INST_LS:
  3649.       return ((status_reg & (FLAG_C | FLAG_Z)) != FLAG_C);
  3650.     case INST_GE:
  3651.       return (((status_reg & FLAG_N) == 0) == ((status_reg & FLAG_V) == 0));
  3652.     case INST_LT:
  3653.       return (((status_reg & FLAG_N) == 0) != ((status_reg & FLAG_V) == 0));
  3654.     case INST_GT:
  3655.       return (((status_reg & FLAG_Z) == 0)
  3656.               && (((status_reg & FLAG_N) == 0)
  3657.                   == ((status_reg & FLAG_V) == 0)));
  3658.     case INST_LE:
  3659.       return (((status_reg & FLAG_Z) != 0)
  3660.               || (((status_reg & FLAG_N) == 0)
  3661.                   != ((status_reg & FLAG_V) == 0)));
  3662.     }
  3663.   return 1;
  3664. }

  3665. static unsigned long
  3666. shifted_reg_val (struct frame_info *frame, unsigned long inst, int carry,
  3667.                  unsigned long pc_val, unsigned long status_reg)
  3668. {
  3669.   unsigned long res, shift;
  3670.   int rm = bits (inst, 0, 3);
  3671.   unsigned long shifttype = bits (inst, 5, 6);

  3672.   if (bit (inst, 4))
  3673.     {
  3674.       int rs = bits (inst, 8, 11);
  3675.       shift = (rs == 15 ? pc_val + 8
  3676.                         : get_frame_register_unsigned (frame, rs)) & 0xFF;
  3677.     }
  3678.   else
  3679.     shift = bits (inst, 7, 11);

  3680.   res = (rm == ARM_PC_REGNUM
  3681.          ? (pc_val + (bit (inst, 4) ? 12 : 8))
  3682.          : get_frame_register_unsigned (frame, rm));

  3683.   switch (shifttype)
  3684.     {
  3685.     case 0:                        /* LSL */
  3686.       res = shift >= 32 ? 0 : res << shift;
  3687.       break;

  3688.     case 1:                        /* LSR */
  3689.       res = shift >= 32 ? 0 : res >> shift;
  3690.       break;

  3691.     case 2:                        /* ASR */
  3692.       if (shift >= 32)
  3693.         shift = 31;
  3694.       res = ((res & 0x80000000L)
  3695.              ? ~((~res) >> shift) : res >> shift);
  3696.       break;

  3697.     case 3:                        /* ROR/RRX */
  3698.       shift &= 31;
  3699.       if (shift == 0)
  3700.         res = (res >> 1) | (carry ? 0x80000000L : 0);
  3701.       else
  3702.         res = (res >> shift) | (res << (32 - shift));
  3703.       break;
  3704.     }

  3705.   return res & 0xffffffff;
  3706. }

  3707. /* Return number of 1-bits in VAL.  */

  3708. static int
  3709. bitcount (unsigned long val)
  3710. {
  3711.   int nbits;
  3712.   for (nbits = 0; val != 0; nbits++)
  3713.     val &= val - 1;                /* Delete rightmost 1-bit in val.  */
  3714.   return nbits;
  3715. }

  3716. /* Return the size in bytes of the complete Thumb instruction whose
  3717.    first halfword is INST1.  */

  3718. static int
  3719. thumb_insn_size (unsigned short inst1)
  3720. {
  3721.   if ((inst1 & 0xe000) == 0xe000 && (inst1 & 0x1800) != 0)
  3722.     return 4;
  3723.   else
  3724.     return 2;
  3725. }

  3726. static int
  3727. thumb_advance_itstate (unsigned int itstate)
  3728. {
  3729.   /* Preserve IT[7:5], the first three bits of the condition.  Shift
  3730.      the upcoming condition flags left by one bit.  */
  3731.   itstate = (itstate & 0xe0) | ((itstate << 1) & 0x1f);

  3732.   /* If we have finished the IT block, clear the state.  */
  3733.   if ((itstate & 0x0f) == 0)
  3734.     itstate = 0;

  3735.   return itstate;
  3736. }

  3737. /* Find the next PC after the current instruction executes.  In some
  3738.    cases we can not statically determine the answer (see the IT state
  3739.    handling in this function); in that case, a breakpoint may be
  3740.    inserted in addition to the returned PC, which will be used to set
  3741.    another breakpoint by our caller.  */

  3742. static CORE_ADDR
  3743. thumb_get_next_pc_raw (struct frame_info *frame, CORE_ADDR pc)
  3744. {
  3745.   struct gdbarch *gdbarch = get_frame_arch (frame);
  3746.   struct address_space *aspace = get_frame_address_space (frame);
  3747.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  3748.   enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
  3749.   unsigned long pc_val = ((unsigned long) pc) + 4;        /* PC after prefetch */
  3750.   unsigned short inst1;
  3751.   CORE_ADDR nextpc = pc + 2;                /* Default is next instruction.  */
  3752.   unsigned long offset;
  3753.   ULONGEST status, itstate;

  3754.   nextpc = MAKE_THUMB_ADDR (nextpc);
  3755.   pc_val = MAKE_THUMB_ADDR (pc_val);

  3756.   inst1 = read_memory_unsigned_integer (pc, 2, byte_order_for_code);

  3757.   /* Thumb-2 conditional execution support.  There are eight bits in
  3758.      the CPSR which describe conditional execution state.  Once
  3759.      reconstructed (they're in a funny order), the low five bits
  3760.      describe the low bit of the condition for each instruction and
  3761.      how many instructions remain.  The high three bits describe the
  3762.      base conditionOne of the low four bits will be set if an IT
  3763.      block is active.  These bits read as zero on earlier
  3764.      processors.  */
  3765.   status = get_frame_register_unsigned (frame, ARM_PS_REGNUM);
  3766.   itstate = ((status >> 8) & 0xfc) | ((status >> 25) & 0x3);

  3767.   /* If-Then handling.  On GNU/Linux, where this routine is used, we
  3768.      use an undefined instruction as a breakpoint.  Unlike BKPT, IT
  3769.      can disable execution of the undefined instruction.  So we might
  3770.      miss the breakpoint if we set it on a skipped conditional
  3771.      instruction.  Because conditional instructions can change the
  3772.      flags, affecting the execution of further instructions, we may
  3773.      need to set two breakpoints.  */

  3774.   if (gdbarch_tdep (gdbarch)->thumb2_breakpoint != NULL)
  3775.     {
  3776.       if ((inst1 & 0xff00) == 0xbf00 && (inst1 & 0x000f) != 0)
  3777.         {
  3778.           /* An IT instruction.  Because this instruction does not
  3779.              modify the flags, we can accurately predict the next
  3780.              executed instruction.  */
  3781.           itstate = inst1 & 0x00ff;
  3782.           pc += thumb_insn_size (inst1);

  3783.           while (itstate != 0 && ! condition_true (itstate >> 4, status))
  3784.             {
  3785.               inst1 = read_memory_unsigned_integer (pc, 2,
  3786.                                                     byte_order_for_code);
  3787.               pc += thumb_insn_size (inst1);
  3788.               itstate = thumb_advance_itstate (itstate);
  3789.             }

  3790.           return MAKE_THUMB_ADDR (pc);
  3791.         }
  3792.       else if (itstate != 0)
  3793.         {
  3794.           /* We are in a conditional block.  Check the condition.  */
  3795.           if (! condition_true (itstate >> 4, status))
  3796.             {
  3797.               /* Advance to the next executed instruction.  */
  3798.               pc += thumb_insn_size (inst1);
  3799.               itstate = thumb_advance_itstate (itstate);

  3800.               while (itstate != 0 && ! condition_true (itstate >> 4, status))
  3801.                 {
  3802.                   inst1 = read_memory_unsigned_integer (pc, 2,
  3803.                                                         byte_order_for_code);
  3804.                   pc += thumb_insn_size (inst1);
  3805.                   itstate = thumb_advance_itstate (itstate);
  3806.                 }

  3807.               return MAKE_THUMB_ADDR (pc);
  3808.             }
  3809.           else if ((itstate & 0x0f) == 0x08)
  3810.             {
  3811.               /* This is the last instruction of the conditional
  3812.                  block, and it is executed.  We can handle it normally
  3813.                  because the following instruction is not conditional,
  3814.                  and we must handle it normally because it is
  3815.                  permitted to branch.  Fall through.  */
  3816.             }
  3817.           else
  3818.             {
  3819.               int cond_negated;

  3820.               /* There are conditional instructions after this one.
  3821.                  If this instruction modifies the flags, then we can
  3822.                  not predict what the next executed instruction will
  3823.                  be.  Fortunately, this instruction is architecturally
  3824.                  forbidden to branch; we know it will fall through.
  3825.                  Start by skipping past it.  */
  3826.               pc += thumb_insn_size (inst1);
  3827.               itstate = thumb_advance_itstate (itstate);

  3828.               /* Set a breakpoint on the following instruction.  */
  3829.               gdb_assert ((itstate & 0x0f) != 0);
  3830.               arm_insert_single_step_breakpoint (gdbarch, aspace,
  3831.                                                  MAKE_THUMB_ADDR (pc));
  3832.               cond_negated = (itstate >> 4) & 1;

  3833.               /* Skip all following instructions with the same
  3834.                  condition.  If there is a later instruction in the IT
  3835.                  block with the opposite condition, set the other
  3836.                  breakpoint there.  If not, then set a breakpoint on
  3837.                  the instruction after the IT block.  */
  3838.               do
  3839.                 {
  3840.                   inst1 = read_memory_unsigned_integer (pc, 2,
  3841.                                                         byte_order_for_code);
  3842.                   pc += thumb_insn_size (inst1);
  3843.                   itstate = thumb_advance_itstate (itstate);
  3844.                 }
  3845.               while (itstate != 0 && ((itstate >> 4) & 1) == cond_negated);

  3846.               return MAKE_THUMB_ADDR (pc);
  3847.             }
  3848.         }
  3849.     }
  3850.   else if (itstate & 0x0f)
  3851.     {
  3852.       /* We are in a conditional block.  Check the condition.  */
  3853.       int cond = itstate >> 4;

  3854.       if (! condition_true (cond, status))
  3855.         /* Advance to the next instruction.  All the 32-bit
  3856.            instructions share a common prefix.  */
  3857.         return MAKE_THUMB_ADDR (pc + thumb_insn_size (inst1));

  3858.       /* Otherwise, handle the instruction normally.  */
  3859.     }

  3860.   if ((inst1 & 0xff00) == 0xbd00)        /* pop {rlist, pc} */
  3861.     {
  3862.       CORE_ADDR sp;

  3863.       /* Fetch the saved PC from the stack.  It's stored above
  3864.          all of the other registers.  */
  3865.       offset = bitcount (bits (inst1, 0, 7)) * INT_REGISTER_SIZE;
  3866.       sp = get_frame_register_unsigned (frame, ARM_SP_REGNUM);
  3867.       nextpc = read_memory_unsigned_integer (sp + offset, 4, byte_order);
  3868.     }
  3869.   else if ((inst1 & 0xf000) == 0xd000)        /* conditional branch */
  3870.     {
  3871.       unsigned long cond = bits (inst1, 8, 11);
  3872.       if (cond == 0x0f/* 0x0f = SWI */
  3873.         {
  3874.           struct gdbarch_tdep *tdep;
  3875.           tdep = gdbarch_tdep (gdbarch);

  3876.           if (tdep->syscall_next_pc != NULL)
  3877.             nextpc = tdep->syscall_next_pc (frame);

  3878.         }
  3879.       else if (cond != 0x0f && condition_true (cond, status))
  3880.         nextpc = pc_val + (sbits (inst1, 0, 7) << 1);
  3881.     }
  3882.   else if ((inst1 & 0xf800) == 0xe000)        /* unconditional branch */
  3883.     {
  3884.       nextpc = pc_val + (sbits (inst1, 0, 10) << 1);
  3885.     }
  3886.   else if (thumb_insn_size (inst1) == 4) /* 32-bit instruction */
  3887.     {
  3888.       unsigned short inst2;
  3889.       inst2 = read_memory_unsigned_integer (pc + 2, 2, byte_order_for_code);

  3890.       /* Default to the next instruction.  */
  3891.       nextpc = pc + 4;
  3892.       nextpc = MAKE_THUMB_ADDR (nextpc);

  3893.       if ((inst1 & 0xf800) == 0xf000 && (inst2 & 0x8000) == 0x8000)
  3894.         {
  3895.           /* Branches and miscellaneous control instructions.  */

  3896.           if ((inst2 & 0x1000) != 0 || (inst2 & 0xd001) == 0xc000)
  3897.             {
  3898.               /* B, BL, BLX.  */
  3899.               int j1, j2, imm1, imm2;

  3900.               imm1 = sbits (inst1, 0, 10);
  3901.               imm2 = bits (inst2, 0, 10);
  3902.               j1 = bit (inst2, 13);
  3903.               j2 = bit (inst2, 11);

  3904.               offset = ((imm1 << 12) + (imm2 << 1));
  3905.               offset ^= ((!j2) << 22) | ((!j1) << 23);

  3906.               nextpc = pc_val + offset;
  3907.               /* For BLX make sure to clear the low bits.  */
  3908.               if (bit (inst2, 12) == 0)
  3909.                 nextpc = nextpc & 0xfffffffc;
  3910.             }
  3911.           else if (inst1 == 0xf3de && (inst2 & 0xff00) == 0x3f00)
  3912.             {
  3913.               /* SUBS PC, LR, #imm8.  */
  3914.               nextpc = get_frame_register_unsigned (frame, ARM_LR_REGNUM);
  3915.               nextpc -= inst2 & 0x00ff;
  3916.             }
  3917.           else if ((inst2 & 0xd000) == 0x8000 && (inst1 & 0x0380) != 0x0380)
  3918.             {
  3919.               /* Conditional branch.  */
  3920.               if (condition_true (bits (inst1, 6, 9), status))
  3921.                 {
  3922.                   int sign, j1, j2, imm1, imm2;

  3923.                   sign = sbits (inst1, 10, 10);
  3924.                   imm1 = bits (inst1, 0, 5);
  3925.                   imm2 = bits (inst2, 0, 10);
  3926.                   j1 = bit (inst2, 13);
  3927.                   j2 = bit (inst2, 11);

  3928.                   offset = (sign << 20) + (j2 << 19) + (j1 << 18);
  3929.                   offset += (imm1 << 12) + (imm2 << 1);

  3930.                   nextpc = pc_val + offset;
  3931.                 }
  3932.             }
  3933.         }
  3934.       else if ((inst1 & 0xfe50) == 0xe810)
  3935.         {
  3936.           /* Load multiple or RFE.  */
  3937.           int rn, offset, load_pc = 1;

  3938.           rn = bits (inst1, 0, 3);
  3939.           if (bit (inst1, 7) && !bit (inst1, 8))
  3940.             {
  3941.               /* LDMIA or POP */
  3942.               if (!bit (inst2, 15))
  3943.                 load_pc = 0;
  3944.               offset = bitcount (inst2) * 4 - 4;
  3945.             }
  3946.           else if (!bit (inst1, 7) && bit (inst1, 8))
  3947.             {
  3948.               /* LDMDB */
  3949.               if (!bit (inst2, 15))
  3950.                 load_pc = 0;
  3951.               offset = -4;
  3952.             }
  3953.           else if (bit (inst1, 7) && bit (inst1, 8))
  3954.             {
  3955.               /* RFEIA */
  3956.               offset = 0;
  3957.             }
  3958.           else if (!bit (inst1, 7) && !bit (inst1, 8))
  3959.             {
  3960.               /* RFEDB */
  3961.               offset = -8;
  3962.             }
  3963.           else
  3964.             load_pc = 0;

  3965.           if (load_pc)
  3966.             {
  3967.               CORE_ADDR addr = get_frame_register_unsigned (frame, rn);
  3968.               nextpc = get_frame_memory_unsigned (frame, addr + offset, 4);
  3969.             }
  3970.         }
  3971.       else if ((inst1 & 0xffef) == 0xea4f && (inst2 & 0xfff0) == 0x0f00)
  3972.         {
  3973.           /* MOV PC or MOVS PC.  */
  3974.           nextpc = get_frame_register_unsigned (frame, bits (inst2, 0, 3));
  3975.           nextpc = MAKE_THUMB_ADDR (nextpc);
  3976.         }
  3977.       else if ((inst1 & 0xff70) == 0xf850 && (inst2 & 0xf000) == 0xf000)
  3978.         {
  3979.           /* LDR PC.  */
  3980.           CORE_ADDR base;
  3981.           int rn, load_pc = 1;

  3982.           rn = bits (inst1, 0, 3);
  3983.           base = get_frame_register_unsigned (frame, rn);
  3984.           if (rn == ARM_PC_REGNUM)
  3985.             {
  3986.               base = (base + 4) & ~(CORE_ADDR) 0x3;
  3987.               if (bit (inst1, 7))
  3988.                 base += bits (inst2, 0, 11);
  3989.               else
  3990.                 base -= bits (inst2, 0, 11);
  3991.             }
  3992.           else if (bit (inst1, 7))
  3993.             base += bits (inst2, 0, 11);
  3994.           else if (bit (inst2, 11))
  3995.             {
  3996.               if (bit (inst2, 10))
  3997.                 {
  3998.                   if (bit (inst2, 9))
  3999.                     base += bits (inst2, 0, 7);
  4000.                   else
  4001.                     base -= bits (inst2, 0, 7);
  4002.                 }
  4003.             }
  4004.           else if ((inst2 & 0x0fc0) == 0x0000)
  4005.             {
  4006.               int shift = bits (inst2, 4, 5), rm = bits (inst2, 0, 3);
  4007.               base += get_frame_register_unsigned (frame, rm) << shift;
  4008.             }
  4009.           else
  4010.             /* Reserved.  */
  4011.             load_pc = 0;

  4012.           if (load_pc)
  4013.             nextpc = get_frame_memory_unsigned (frame, base, 4);
  4014.         }
  4015.       else if ((inst1 & 0xfff0) == 0xe8d0 && (inst2 & 0xfff0) == 0xf000)
  4016.         {
  4017.           /* TBB.  */
  4018.           CORE_ADDR tbl_reg, table, offset, length;

  4019.           tbl_reg = bits (inst1, 0, 3);
  4020.           if (tbl_reg == 0x0f)
  4021.             table = pc + 4/* Regcache copy of PC isn't right yet.  */
  4022.           else
  4023.             table = get_frame_register_unsigned (frame, tbl_reg);

  4024.           offset = get_frame_register_unsigned (frame, bits (inst2, 0, 3));
  4025.           length = 2 * get_frame_memory_unsigned (frame, table + offset, 1);
  4026.           nextpc = pc_val + length;
  4027.         }
  4028.       else if ((inst1 & 0xfff0) == 0xe8d0 && (inst2 & 0xfff0) == 0xf010)
  4029.         {
  4030.           /* TBH.  */
  4031.           CORE_ADDR tbl_reg, table, offset, length;

  4032.           tbl_reg = bits (inst1, 0, 3);
  4033.           if (tbl_reg == 0x0f)
  4034.             table = pc + 4/* Regcache copy of PC isn't right yet.  */
  4035.           else
  4036.             table = get_frame_register_unsigned (frame, tbl_reg);

  4037.           offset = 2 * get_frame_register_unsigned (frame, bits (inst2, 0, 3));
  4038.           length = 2 * get_frame_memory_unsigned (frame, table + offset, 2);
  4039.           nextpc = pc_val + length;
  4040.         }
  4041.     }
  4042.   else if ((inst1 & 0xff00) == 0x4700)        /* bx REG, blx REG */
  4043.     {
  4044.       if (bits (inst1, 3, 6) == 0x0f)
  4045.         nextpc = UNMAKE_THUMB_ADDR (pc_val);
  4046.       else
  4047.         nextpc = get_frame_register_unsigned (frame, bits (inst1, 3, 6));
  4048.     }
  4049.   else if ((inst1 & 0xff87) == 0x4687)        /* mov pc, REG */
  4050.     {
  4051.       if (bits (inst1, 3, 6) == 0x0f)
  4052.         nextpc = pc_val;
  4053.       else
  4054.         nextpc = get_frame_register_unsigned (frame, bits (inst1, 3, 6));

  4055.       nextpc = MAKE_THUMB_ADDR (nextpc);
  4056.     }
  4057.   else if ((inst1 & 0xf500) == 0xb100)
  4058.     {
  4059.       /* CBNZ or CBZ.  */
  4060.       int imm = (bit (inst1, 9) << 6) + (bits (inst1, 3, 7) << 1);
  4061.       ULONGEST reg = get_frame_register_unsigned (frame, bits (inst1, 0, 2));

  4062.       if (bit (inst1, 11) && reg != 0)
  4063.         nextpc = pc_val + imm;
  4064.       else if (!bit (inst1, 11) && reg == 0)
  4065.         nextpc = pc_val + imm;
  4066.     }
  4067.   return nextpc;
  4068. }

  4069. /* Get the raw next addressPC is the current program counter, in
  4070.    FRAME, which is assumed to be executing in ARM mode.

  4071.    The value returned has the execution state of the next instruction
  4072.    encoded in it.  Use IS_THUMB_ADDR () to see whether the instruction is
  4073.    in Thumb-State, and gdbarch_addr_bits_remove () to get the plain memory
  4074.    address.  */

  4075. static CORE_ADDR
  4076. arm_get_next_pc_raw (struct frame_info *frame, CORE_ADDR pc)
  4077. {
  4078.   struct gdbarch *gdbarch = get_frame_arch (frame);
  4079.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  4080.   enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
  4081.   unsigned long pc_val;
  4082.   unsigned long this_instr;
  4083.   unsigned long status;
  4084.   CORE_ADDR nextpc;

  4085.   pc_val = (unsigned long) pc;
  4086.   this_instr = read_memory_unsigned_integer (pc, 4, byte_order_for_code);

  4087.   status = get_frame_register_unsigned (frame, ARM_PS_REGNUM);
  4088.   nextpc = (CORE_ADDR) (pc_val + 4);        /* Default case */

  4089.   if (bits (this_instr, 28, 31) == INST_NV)
  4090.     switch (bits (this_instr, 24, 27))
  4091.       {
  4092.       case 0xa:
  4093.       case 0xb:
  4094.         {
  4095.           /* Branch with Link and change to Thumb.  */
  4096.           nextpc = BranchDest (pc, this_instr);
  4097.           nextpc |= bit (this_instr, 24) << 1;
  4098.           nextpc = MAKE_THUMB_ADDR (nextpc);
  4099.           break;
  4100.         }
  4101.       case 0xc:
  4102.       case 0xd:
  4103.       case 0xe:
  4104.         /* Coprocessor register transfer.  */
  4105.         if (bits (this_instr, 12, 15) == 15)
  4106.           error (_("Invalid update to pc in instruction"));
  4107.         break;
  4108.       }
  4109.   else if (condition_true (bits (this_instr, 28, 31), status))
  4110.     {
  4111.       switch (bits (this_instr, 24, 27))
  4112.         {
  4113.         case 0x0:
  4114.         case 0x1:                        /* data processing */
  4115.         case 0x2:
  4116.         case 0x3:
  4117.           {
  4118.             unsigned long operand1, operand2, result = 0;
  4119.             unsigned long rn;
  4120.             int c;

  4121.             if (bits (this_instr, 12, 15) != 15)
  4122.               break;

  4123.             if (bits (this_instr, 22, 25) == 0
  4124.                 && bits (this_instr, 4, 7) == 9)        /* multiply */
  4125.               error (_("Invalid update to pc in instruction"));

  4126.             /* BX <reg>, BLX <reg> */
  4127.             if (bits (this_instr, 4, 27) == 0x12fff1
  4128.                 || bits (this_instr, 4, 27) == 0x12fff3)
  4129.               {
  4130.                 rn = bits (this_instr, 0, 3);
  4131.                 nextpc = ((rn == ARM_PC_REGNUM)
  4132.                           ? (pc_val + 8)
  4133.                           : get_frame_register_unsigned (frame, rn));

  4134.                 return nextpc;
  4135.               }

  4136.             /* Multiply into PC.  */
  4137.             c = (status & FLAG_C) ? 1 : 0;
  4138.             rn = bits (this_instr, 16, 19);
  4139.             operand1 = ((rn == ARM_PC_REGNUM)
  4140.                         ? (pc_val + 8)
  4141.                         : get_frame_register_unsigned (frame, rn));

  4142.             if (bit (this_instr, 25))
  4143.               {
  4144.                 unsigned long immval = bits (this_instr, 0, 7);
  4145.                 unsigned long rotate = 2 * bits (this_instr, 8, 11);
  4146.                 operand2 = ((immval >> rotate) | (immval << (32 - rotate)))
  4147.                   & 0xffffffff;
  4148.               }
  4149.             else                /* operand 2 is a shifted register.  */
  4150.               operand2 = shifted_reg_val (frame, this_instr, c,
  4151.                                           pc_val, status);

  4152.             switch (bits (this_instr, 21, 24))
  4153.               {
  4154.               case 0x0:        /*and */
  4155.                 result = operand1 & operand2;
  4156.                 break;

  4157.               case 0x1:        /*eor */
  4158.                 result = operand1 ^ operand2;
  4159.                 break;

  4160.               case 0x2:        /*sub */
  4161.                 result = operand1 - operand2;
  4162.                 break;

  4163.               case 0x3:        /*rsb */
  4164.                 result = operand2 - operand1;
  4165.                 break;

  4166.               case 0x4:        /*add */
  4167.                 result = operand1 + operand2;
  4168.                 break;

  4169.               case 0x5:        /*adc */
  4170.                 result = operand1 + operand2 + c;
  4171.                 break;

  4172.               case 0x6:        /*sbc */
  4173.                 result = operand1 - operand2 + c;
  4174.                 break;

  4175.               case 0x7:        /*rsc */
  4176.                 result = operand2 - operand1 + c;
  4177.                 break;

  4178.               case 0x8:
  4179.               case 0x9:
  4180.               case 0xa:
  4181.               case 0xb:        /* tst, teq, cmp, cmn */
  4182.                 result = (unsigned long) nextpc;
  4183.                 break;

  4184.               case 0xc:        /*orr */
  4185.                 result = operand1 | operand2;
  4186.                 break;

  4187.               case 0xd:        /*mov */
  4188.                 /* Always step into a function.  */
  4189.                 result = operand2;
  4190.                 break;

  4191.               case 0xe:        /*bic */
  4192.                 result = operand1 & ~operand2;
  4193.                 break;

  4194.               case 0xf:        /*mvn */
  4195.                 result = ~operand2;
  4196.                 break;
  4197.               }

  4198.             /* In 26-bit APCS the bottom two bits of the result are
  4199.                ignored, and we always end up in ARM state.  */
  4200.             if (!arm_apcs_32)
  4201.               nextpc = arm_addr_bits_remove (gdbarch, result);
  4202.             else
  4203.               nextpc = result;

  4204.             break;
  4205.           }

  4206.         case 0x4:
  4207.         case 0x5:                /* data transfer */
  4208.         case 0x6:
  4209.         case 0x7:
  4210.           if (bit (this_instr, 20))
  4211.             {
  4212.               /* load */
  4213.               if (bits (this_instr, 12, 15) == 15)
  4214.                 {
  4215.                   /* rd == pc */
  4216.                   unsigned long rn;
  4217.                   unsigned long base;

  4218.                   if (bit (this_instr, 22))
  4219.                     error (_("Invalid update to pc in instruction"));

  4220.                   /* byte write to PC */
  4221.                   rn = bits (this_instr, 16, 19);
  4222.                   base = ((rn == ARM_PC_REGNUM)
  4223.                           ? (pc_val + 8)
  4224.                           : get_frame_register_unsigned (frame, rn));

  4225.                   if (bit (this_instr, 24))
  4226.                     {
  4227.                       /* pre-indexed */
  4228.                       int c = (status & FLAG_C) ? 1 : 0;
  4229.                       unsigned long offset =
  4230.                       (bit (this_instr, 25)
  4231.                        ? shifted_reg_val (frame, this_instr, c, pc_val, status)
  4232.                        : bits (this_instr, 0, 11));

  4233.                       if (bit (this_instr, 23))
  4234.                         base += offset;
  4235.                       else
  4236.                         base -= offset;
  4237.                     }
  4238.                   nextpc =
  4239.                     (CORE_ADDR) read_memory_unsigned_integer ((CORE_ADDR) base,
  4240.                                                               4, byte_order);
  4241.                 }
  4242.             }
  4243.           break;

  4244.         case 0x8:
  4245.         case 0x9:                /* block transfer */
  4246.           if (bit (this_instr, 20))
  4247.             {
  4248.               /* LDM */
  4249.               if (bit (this_instr, 15))
  4250.                 {
  4251.                   /* loading pc */
  4252.                   int offset = 0;
  4253.                   unsigned long rn_val
  4254.                     = get_frame_register_unsigned (frame,
  4255.                                                    bits (this_instr, 16, 19));

  4256.                   if (bit (this_instr, 23))
  4257.                     {
  4258.                       /* up */
  4259.                       unsigned long reglist = bits (this_instr, 0, 14);
  4260.                       offset = bitcount (reglist) * 4;
  4261.                       if (bit (this_instr, 24))                /* pre */
  4262.                         offset += 4;
  4263.                     }
  4264.                   else if (bit (this_instr, 24))
  4265.                     offset = -4;

  4266.                   nextpc =
  4267.                     (CORE_ADDR) read_memory_unsigned_integer ((CORE_ADDR)
  4268.                                                               (rn_val + offset),
  4269.                                                               4, byte_order);
  4270.                 }
  4271.             }
  4272.           break;

  4273.         case 0xb:                /* branch & link */
  4274.         case 0xa:                /* branch */
  4275.           {
  4276.             nextpc = BranchDest (pc, this_instr);
  4277.             break;
  4278.           }

  4279.         case 0xc:
  4280.         case 0xd:
  4281.         case 0xe:                /* coproc ops */
  4282.           break;
  4283.         case 0xf:                /* SWI */
  4284.           {
  4285.             struct gdbarch_tdep *tdep;
  4286.             tdep = gdbarch_tdep (gdbarch);

  4287.             if (tdep->syscall_next_pc != NULL)
  4288.               nextpc = tdep->syscall_next_pc (frame);

  4289.           }
  4290.           break;

  4291.         default:
  4292.           fprintf_filtered (gdb_stderr, _("Bad bit-field extraction\n"));
  4293.           return (pc);
  4294.         }
  4295.     }

  4296.   return nextpc;
  4297. }

  4298. /* Determine next PC after current instruction executes.  Will call either
  4299.    arm_get_next_pc_raw or thumb_get_next_pc_raw.  Error out if infinite
  4300.    loop is detected.  */

  4301. CORE_ADDR
  4302. arm_get_next_pc (struct frame_info *frame, CORE_ADDR pc)
  4303. {
  4304.   CORE_ADDR nextpc;

  4305.   if (arm_frame_is_thumb (frame))
  4306.     nextpc = thumb_get_next_pc_raw (frame, pc);
  4307.   else
  4308.     nextpc = arm_get_next_pc_raw (frame, pc);

  4309.   return nextpc;
  4310. }

  4311. /* Like insert_single_step_breakpoint, but make sure we use a breakpoint
  4312.    of the appropriate mode (as encoded in the PC value), even if this
  4313.    differs from what would be expected according to the symbol tables.  */

  4314. void
  4315. arm_insert_single_step_breakpoint (struct gdbarch *gdbarch,
  4316.                                    struct address_space *aspace,
  4317.                                    CORE_ADDR pc)
  4318. {
  4319.   struct cleanup *old_chain
  4320.     = make_cleanup_restore_integer (&arm_override_mode);

  4321.   arm_override_mode = IS_THUMB_ADDR (pc);
  4322.   pc = gdbarch_addr_bits_remove (gdbarch, pc);

  4323.   insert_single_step_breakpoint (gdbarch, aspace, pc);

  4324.   do_cleanups (old_chain);
  4325. }

  4326. /* Checks for an atomic sequence of instructions beginning with a LDREX{,B,H,D}
  4327.    instruction and ending with a STREX{,B,H,D} instruction.  If such a sequence
  4328.    is found, attempt to step through it.  A breakpoint is placed at the end of
  4329.    the sequence.  */

  4330. static int
  4331. thumb_deal_with_atomic_sequence_raw (struct frame_info *frame)
  4332. {
  4333.   struct gdbarch *gdbarch = get_frame_arch (frame);
  4334.   struct address_space *aspace = get_frame_address_space (frame);
  4335.   enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
  4336.   CORE_ADDR pc = get_frame_pc (frame);
  4337.   CORE_ADDR breaks[2] = {-1, -1};
  4338.   CORE_ADDR loc = pc;
  4339.   unsigned short insn1, insn2;
  4340.   int insn_count;
  4341.   int index;
  4342.   int last_breakpoint = 0; /* Defaults to 0 (no breakpoints placed).  */
  4343.   const int atomic_sequence_length = 16; /* Instruction sequence length.  */
  4344.   ULONGEST status, itstate;

  4345.   /* We currently do not support atomic sequences within an IT block.  */
  4346.   status = get_frame_register_unsigned (frame, ARM_PS_REGNUM);
  4347.   itstate = ((status >> 8) & 0xfc) | ((status >> 25) & 0x3);
  4348.   if (itstate & 0x0f)
  4349.     return 0;

  4350.   /* Assume all atomic sequences start with a ldrex{,b,h,d} instruction.  */
  4351.   insn1 = read_memory_unsigned_integer (loc, 2, byte_order_for_code);
  4352.   loc += 2;
  4353.   if (thumb_insn_size (insn1) != 4)
  4354.     return 0;

  4355.   insn2 = read_memory_unsigned_integer (loc, 2, byte_order_for_code);
  4356.   loc += 2;
  4357.   if (!((insn1 & 0xfff0) == 0xe850
  4358.         || ((insn1 & 0xfff0) == 0xe8d0 && (insn2 & 0x00c0) == 0x0040)))
  4359.     return 0;

  4360.   /* Assume that no atomic sequence is longer than "atomic_sequence_length"
  4361.      instructions.  */
  4362.   for (insn_count = 0; insn_count < atomic_sequence_length; ++insn_count)
  4363.     {
  4364.       insn1 = read_memory_unsigned_integer (loc, 2, byte_order_for_code);
  4365.       loc += 2;

  4366.       if (thumb_insn_size (insn1) != 4)
  4367.         {
  4368.           /* Assume that there is at most one conditional branch in the
  4369.              atomic sequence.  If a conditional branch is found, put a
  4370.              breakpoint in its destination address.  */
  4371.           if ((insn1 & 0xf000) == 0xd000 && bits (insn1, 8, 11) != 0x0f)
  4372.             {
  4373.               if (last_breakpoint > 0)
  4374.                 return 0; /* More than one conditional branch found,
  4375.                              fallback to the standard code.  */

  4376.               breaks[1] = loc + 2 + (sbits (insn1, 0, 7) << 1);
  4377.               last_breakpoint++;
  4378.             }

  4379.           /* We do not support atomic sequences that use any *other*
  4380.              instructions but conditional branches to change the PC.
  4381.              Fall back to standard code to avoid losing control of
  4382.              execution.  */
  4383.           else if (thumb_instruction_changes_pc (insn1))
  4384.             return 0;
  4385.         }
  4386.       else
  4387.         {
  4388.           insn2 = read_memory_unsigned_integer (loc, 2, byte_order_for_code);
  4389.           loc += 2;

  4390.           /* Assume that there is at most one conditional branch in the
  4391.              atomic sequence.  If a conditional branch is found, put a
  4392.              breakpoint in its destination address.  */
  4393.           if ((insn1 & 0xf800) == 0xf000
  4394.               && (insn2 & 0xd000) == 0x8000
  4395.               && (insn1 & 0x0380) != 0x0380)
  4396.             {
  4397.               int sign, j1, j2, imm1, imm2;
  4398.               unsigned int offset;

  4399.               sign = sbits (insn1, 10, 10);
  4400.               imm1 = bits (insn1, 0, 5);
  4401.               imm2 = bits (insn2, 0, 10);
  4402.               j1 = bit (insn2, 13);
  4403.               j2 = bit (insn2, 11);

  4404.               offset = (sign << 20) + (j2 << 19) + (j1 << 18);
  4405.               offset += (imm1 << 12) + (imm2 << 1);

  4406.               if (last_breakpoint > 0)
  4407.                 return 0; /* More than one conditional branch found,
  4408.                              fallback to the standard code.  */

  4409.               breaks[1] = loc + offset;
  4410.               last_breakpoint++;
  4411.             }

  4412.           /* We do not support atomic sequences that use any *other*
  4413.              instructions but conditional branches to change the PC.
  4414.              Fall back to standard code to avoid losing control of
  4415.              execution.  */
  4416.           else if (thumb2_instruction_changes_pc (insn1, insn2))
  4417.             return 0;

  4418.           /* If we find a strex{,b,h,d}, we're done.  */
  4419.           if ((insn1 & 0xfff0) == 0xe840
  4420.               || ((insn1 & 0xfff0) == 0xe8c0 && (insn2 & 0x00c0) == 0x0040))
  4421.             break;
  4422.         }
  4423.     }

  4424.   /* If we didn't find the strex{,b,h,d}, we cannot handle the sequence.  */
  4425.   if (insn_count == atomic_sequence_length)
  4426.     return 0;

  4427.   /* Insert a breakpoint right after the end of the atomic sequence.  */
  4428.   breaks[0] = loc;

  4429.   /* Check for duplicated breakpoints.  Check also for a breakpoint
  4430.      placed (branch instruction's destination) anywhere in sequence.  */
  4431.   if (last_breakpoint
  4432.       && (breaks[1] == breaks[0]
  4433.           || (breaks[1] >= pc && breaks[1] < loc)))
  4434.     last_breakpoint = 0;

  4435.   /* Effectively inserts the breakpoints.  */
  4436.   for (index = 0; index <= last_breakpoint; index++)
  4437.     arm_insert_single_step_breakpoint (gdbarch, aspace,
  4438.                                        MAKE_THUMB_ADDR (breaks[index]));

  4439.   return 1;
  4440. }

  4441. static int
  4442. arm_deal_with_atomic_sequence_raw (struct frame_info *frame)
  4443. {
  4444.   struct gdbarch *gdbarch = get_frame_arch (frame);
  4445.   struct address_space *aspace = get_frame_address_space (frame);
  4446.   enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
  4447.   CORE_ADDR pc = get_frame_pc (frame);
  4448.   CORE_ADDR breaks[2] = {-1, -1};
  4449.   CORE_ADDR loc = pc;
  4450.   unsigned int insn;
  4451.   int insn_count;
  4452.   int index;
  4453.   int last_breakpoint = 0; /* Defaults to 0 (no breakpoints placed).  */
  4454.   const int atomic_sequence_length = 16; /* Instruction sequence length.  */

  4455.   /* Assume all atomic sequences start with a ldrex{,b,h,d} instruction.
  4456.      Note that we do not currently support conditionally executed atomic
  4457.      instructions.  */
  4458.   insn = read_memory_unsigned_integer (loc, 4, byte_order_for_code);
  4459.   loc += 4;
  4460.   if ((insn & 0xff9000f0) != 0xe1900090)
  4461.     return 0;

  4462.   /* Assume that no atomic sequence is longer than "atomic_sequence_length"
  4463.      instructions.  */
  4464.   for (insn_count = 0; insn_count < atomic_sequence_length; ++insn_count)
  4465.     {
  4466.       insn = read_memory_unsigned_integer (loc, 4, byte_order_for_code);
  4467.       loc += 4;

  4468.       /* Assume that there is at most one conditional branch in the atomic
  4469.          sequence.  If a conditional branch is found, put a breakpoint in
  4470.          its destination address.  */
  4471.       if (bits (insn, 24, 27) == 0xa)
  4472.         {
  4473.           if (last_breakpoint > 0)
  4474.             return 0; /* More than one conditional branch found, fallback
  4475.                          to the standard single-step code.  */

  4476.           breaks[1] = BranchDest (loc - 4, insn);
  4477.           last_breakpoint++;
  4478.         }

  4479.       /* We do not support atomic sequences that use any *other* instructions
  4480.          but conditional branches to change the PC.  Fall back to standard
  4481.          code to avoid losing control of execution.  */
  4482.       else if (arm_instruction_changes_pc (insn))
  4483.         return 0;

  4484.       /* If we find a strex{,b,h,d}, we're done.  */
  4485.       if ((insn & 0xff9000f0) == 0xe1800090)
  4486.         break;
  4487.     }

  4488.   /* If we didn't find the strex{,b,h,d}, we cannot handle the sequence.  */
  4489.   if (insn_count == atomic_sequence_length)
  4490.     return 0;

  4491.   /* Insert a breakpoint right after the end of the atomic sequence.  */
  4492.   breaks[0] = loc;

  4493.   /* Check for duplicated breakpoints.  Check also for a breakpoint
  4494.      placed (branch instruction's destination) anywhere in sequence.  */
  4495.   if (last_breakpoint
  4496.       && (breaks[1] == breaks[0]
  4497.           || (breaks[1] >= pc && breaks[1] < loc)))
  4498.     last_breakpoint = 0;

  4499.   /* Effectively inserts the breakpoints.  */
  4500.   for (index = 0; index <= last_breakpoint; index++)
  4501.     arm_insert_single_step_breakpoint (gdbarch, aspace, breaks[index]);

  4502.   return 1;
  4503. }

  4504. int
  4505. arm_deal_with_atomic_sequence (struct frame_info *frame)
  4506. {
  4507.   if (arm_frame_is_thumb (frame))
  4508.     return thumb_deal_with_atomic_sequence_raw (frame);
  4509.   else
  4510.     return arm_deal_with_atomic_sequence_raw (frame);
  4511. }

  4512. /* single_step() is called just before we want to resume the inferior,
  4513.    if we want to single-step it but there is no hardware or kernel
  4514.    single-step support.  We find the target of the coming instruction
  4515.    and breakpoint it.  */

  4516. int
  4517. arm_software_single_step (struct frame_info *frame)
  4518. {
  4519.   struct gdbarch *gdbarch = get_frame_arch (frame);
  4520.   struct address_space *aspace = get_frame_address_space (frame);
  4521.   CORE_ADDR next_pc;

  4522.   if (arm_deal_with_atomic_sequence (frame))
  4523.     return 1;

  4524.   next_pc = arm_get_next_pc (frame, get_frame_pc (frame));
  4525.   arm_insert_single_step_breakpoint (gdbarch, aspace, next_pc);

  4526.   return 1;
  4527. }

  4528. /* Given BUF, which is OLD_LEN bytes ending at ENDADDR, expand
  4529.    the buffer to be NEW_LEN bytes ending at ENDADDR.  Return
  4530.    NULL if an error occurs.  BUF is freed.  */

  4531. static gdb_byte *
  4532. extend_buffer_earlier (gdb_byte *buf, CORE_ADDR endaddr,
  4533.                        int old_len, int new_len)
  4534. {
  4535.   gdb_byte *new_buf;
  4536.   int bytes_to_read = new_len - old_len;

  4537.   new_buf = xmalloc (new_len);
  4538.   memcpy (new_buf + bytes_to_read, buf, old_len);
  4539.   xfree (buf);
  4540.   if (target_read_memory (endaddr - new_len, new_buf, bytes_to_read) != 0)
  4541.     {
  4542.       xfree (new_buf);
  4543.       return NULL;
  4544.     }
  4545.   return new_buf;
  4546. }

  4547. /* An IT block is at most the 2-byte IT instruction followed by
  4548.    four 4-byte instructions.  The furthest back we must search to
  4549.    find an IT block that affects the current instruction is thus
  4550.    2 + 3 * 4 == 14 bytes.  */
  4551. #define MAX_IT_BLOCK_PREFIX 14

  4552. /* Use a quick scan if there are more than this many bytes of
  4553.    code.  */
  4554. #define IT_SCAN_THRESHOLD 32

  4555. /* Adjust a breakpoint's address to move breakpoints out of IT blocks.
  4556.    A breakpoint in an IT block may not be hit, depending on the
  4557.    condition flags.  */
  4558. static CORE_ADDR
  4559. arm_adjust_breakpoint_address (struct gdbarch *gdbarch, CORE_ADDR bpaddr)
  4560. {
  4561.   gdb_byte *buf;
  4562.   char map_type;
  4563.   CORE_ADDR boundary, func_start;
  4564.   int buf_len;
  4565.   enum bfd_endian order = gdbarch_byte_order_for_code (gdbarch);
  4566.   int i, any, last_it, last_it_count;

  4567.   /* If we are using BKPT breakpoints, none of this is necessary.  */
  4568.   if (gdbarch_tdep (gdbarch)->thumb2_breakpoint == NULL)
  4569.     return bpaddr;

  4570.   /* ARM mode does not have this problem.  */
  4571.   if (!arm_pc_is_thumb (gdbarch, bpaddr))
  4572.     return bpaddr;

  4573.   /* We are setting a breakpoint in Thumb code that could potentially
  4574.      contain an IT block.  The first step is to find how much Thumb
  4575.      code there is; we do not need to read outside of known Thumb
  4576.      sequences.  */
  4577.   map_type = arm_find_mapping_symbol (bpaddr, &boundary);
  4578.   if (map_type == 0)
  4579.     /* Thumb-2 code must have mapping symbols to have a chance.  */
  4580.     return bpaddr;

  4581.   bpaddr = gdbarch_addr_bits_remove (gdbarch, bpaddr);

  4582.   if (find_pc_partial_function (bpaddr, NULL, &func_start, NULL)
  4583.       && func_start > boundary)
  4584.     boundary = func_start;

  4585.   /* Search for a candidate IT instruction.  We have to do some fancy
  4586.      footwork to distinguish a real IT instruction from the second
  4587.      half of a 32-bit instruction, but there is no need for that if
  4588.      there's no candidate.  */
  4589.   buf_len = min (bpaddr - boundary, MAX_IT_BLOCK_PREFIX);
  4590.   if (buf_len == 0)
  4591.     /* No room for an IT instruction.  */
  4592.     return bpaddr;

  4593.   buf = xmalloc (buf_len);
  4594.   if (target_read_memory (bpaddr - buf_len, buf, buf_len) != 0)
  4595.     return bpaddr;
  4596.   any = 0;
  4597.   for (i = 0; i < buf_len; i += 2)
  4598.     {
  4599.       unsigned short inst1 = extract_unsigned_integer (&buf[i], 2, order);
  4600.       if ((inst1 & 0xff00) == 0xbf00 && (inst1 & 0x000f) != 0)
  4601.         {
  4602.           any = 1;
  4603.           break;
  4604.         }
  4605.     }
  4606.   if (any == 0)
  4607.     {
  4608.       xfree (buf);
  4609.       return bpaddr;
  4610.     }

  4611.   /* OK, the code bytes before this instruction contain at least one
  4612.      halfword which resembles an IT instruction.  We know that it's
  4613.      Thumb code, but there are still two possibilities.  Either the
  4614.      halfword really is an IT instruction, or it is the second half of
  4615.      a 32-bit Thumb instruction.  The only way we can tell is to
  4616.      scan forwards from a known instruction boundary.  */
  4617.   if (bpaddr - boundary > IT_SCAN_THRESHOLD)
  4618.     {
  4619.       int definite;

  4620.       /* There's a lot of code before this instruction.  Start with an
  4621.          optimistic search; it's easy to recognize halfwords that can
  4622.          not be the start of a 32-bit instruction, and use that to
  4623.          lock on to the instruction boundaries.  */
  4624.       buf = extend_buffer_earlier (buf, bpaddr, buf_len, IT_SCAN_THRESHOLD);
  4625.       if (buf == NULL)
  4626.         return bpaddr;
  4627.       buf_len = IT_SCAN_THRESHOLD;

  4628.       definite = 0;
  4629.       for (i = 0; i < buf_len - sizeof (buf) && ! definite; i += 2)
  4630.         {
  4631.           unsigned short inst1 = extract_unsigned_integer (&buf[i], 2, order);
  4632.           if (thumb_insn_size (inst1) == 2)
  4633.             {
  4634.               definite = 1;
  4635.               break;
  4636.             }
  4637.         }

  4638.       /* At this point, if DEFINITE, BUF[I] is the first place we
  4639.          are sure that we know the instruction boundaries, and it is far
  4640.          enough from BPADDR that we could not miss an IT instruction
  4641.          affecting BPADDR.  If ! DEFINITE, give up - start from a
  4642.          known boundary.  */
  4643.       if (! definite)
  4644.         {
  4645.           buf = extend_buffer_earlier (buf, bpaddr, buf_len,
  4646.                                        bpaddr - boundary);
  4647.           if (buf == NULL)
  4648.             return bpaddr;
  4649.           buf_len = bpaddr - boundary;
  4650.           i = 0;
  4651.         }
  4652.     }
  4653.   else
  4654.     {
  4655.       buf = extend_buffer_earlier (buf, bpaddr, buf_len, bpaddr - boundary);
  4656.       if (buf == NULL)
  4657.         return bpaddr;
  4658.       buf_len = bpaddr - boundary;
  4659.       i = 0;
  4660.     }

  4661.   /* Scan forwards.  Find the last IT instruction before BPADDR.  */
  4662.   last_it = -1;
  4663.   last_it_count = 0;
  4664.   while (i < buf_len)
  4665.     {
  4666.       unsigned short inst1 = extract_unsigned_integer (&buf[i], 2, order);
  4667.       last_it_count--;
  4668.       if ((inst1 & 0xff00) == 0xbf00 && (inst1 & 0x000f) != 0)
  4669.         {
  4670.           last_it = i;
  4671.           if (inst1 & 0x0001)
  4672.             last_it_count = 4;
  4673.           else if (inst1 & 0x0002)
  4674.             last_it_count = 3;
  4675.           else if (inst1 & 0x0004)
  4676.             last_it_count = 2;
  4677.           else
  4678.             last_it_count = 1;
  4679.         }
  4680.       i += thumb_insn_size (inst1);
  4681.     }

  4682.   xfree (buf);

  4683.   if (last_it == -1)
  4684.     /* There wasn't really an IT instruction after all.  */
  4685.     return bpaddr;

  4686.   if (last_it_count < 1)
  4687.     /* It was too far away.  */
  4688.     return bpaddr;

  4689.   /* This really is a trouble spot.  Move the breakpoint to the IT
  4690.      instruction.  */
  4691.   return bpaddr - buf_len + last_it;
  4692. }

  4693. /* ARM displaced stepping support.

  4694.    Generally ARM displaced stepping works as follows:

  4695.    1. When an instruction is to be single-stepped, it is first decoded by
  4696.       arm_process_displaced_insn (called from arm_displaced_step_copy_insn).
  4697.       Depending on the type of instruction, it is then copied to a scratch
  4698.       location, possibly in a modified form.  The copy_* set of functions
  4699.       performs such modification, as necessary.  A breakpoint is placed after
  4700.       the modified instruction in the scratch space to return control to GDB.
  4701.       Note in particular that instructions which modify the PC will no longer
  4702.       do so after modification.

  4703.    2. The instruction is single-stepped, by setting the PC to the scratch
  4704.       location address, and resuming.  Control returns to GDB when the
  4705.       breakpoint is hit.

  4706.    3. A cleanup function (cleanup_*) is called corresponding to the copy_*
  4707.       function used for the current instruction.  This function's job is to
  4708.       put the CPU/memory state back to what it would have been if the
  4709.       instruction had been executed unmodified in its original location.  */

  4710. /* NOP instruction (mov r0, r0).  */
  4711. #define ARM_NOP                                0xe1a00000
  4712. #define THUMB_NOP 0x4600

  4713. /* Helper for register reads for displaced stepping.  In particular, this
  4714.    returns the PC as it would be seen by the instruction at its original
  4715.    location.  */

  4716. ULONGEST
  4717. displaced_read_reg (struct regcache *regs, struct displaced_step_closure *dsc,
  4718.                     int regno)
  4719. {
  4720.   ULONGEST ret;
  4721.   CORE_ADDR from = dsc->insn_addr;

  4722.   if (regno == ARM_PC_REGNUM)
  4723.     {
  4724.       /* Compute pipeline offset:
  4725.          - When executing an ARM instruction, PC reads as the address of the
  4726.          current instruction plus 8.
  4727.          - When executing a Thumb instruction, PC reads as the address of the
  4728.          current instruction plus 4.  */

  4729.       if (!dsc->is_thumb)
  4730.         from += 8;
  4731.       else
  4732.         from += 4;

  4733.       if (debug_displaced)
  4734.         fprintf_unfiltered (gdb_stdlog, "displaced: read pc value %.8lx\n",
  4735.                             (unsigned long) from);
  4736.       return (ULONGEST) from;
  4737.     }
  4738.   else
  4739.     {
  4740.       regcache_cooked_read_unsigned (regs, regno, &ret);
  4741.       if (debug_displaced)
  4742.         fprintf_unfiltered (gdb_stdlog, "displaced: read r%d value %.8lx\n",
  4743.                             regno, (unsigned long) ret);
  4744.       return ret;
  4745.     }
  4746. }

  4747. static int
  4748. displaced_in_arm_mode (struct regcache *regs)
  4749. {
  4750.   ULONGEST ps;
  4751.   ULONGEST t_bit = arm_psr_thumb_bit (get_regcache_arch (regs));

  4752.   regcache_cooked_read_unsigned (regs, ARM_PS_REGNUM, &ps);

  4753.   return (ps & t_bit) == 0;
  4754. }

  4755. /* Write to the PC as from a branch instruction.  */

  4756. static void
  4757. branch_write_pc (struct regcache *regs, struct displaced_step_closure *dsc,
  4758.                  ULONGEST val)
  4759. {
  4760.   if (!dsc->is_thumb)
  4761.     /* Note: If bits 0/1 are set, this branch would be unpredictable for
  4762.        architecture versions < 6.  */
  4763.     regcache_cooked_write_unsigned (regs, ARM_PC_REGNUM,
  4764.                                     val & ~(ULONGEST) 0x3);
  4765.   else
  4766.     regcache_cooked_write_unsigned (regs, ARM_PC_REGNUM,
  4767.                                     val & ~(ULONGEST) 0x1);
  4768. }

  4769. /* Write to the PC as from a branch-exchange instruction.  */

  4770. static void
  4771. bx_write_pc (struct regcache *regs, ULONGEST val)
  4772. {
  4773.   ULONGEST ps;
  4774.   ULONGEST t_bit = arm_psr_thumb_bit (get_regcache_arch (regs));

  4775.   regcache_cooked_read_unsigned (regs, ARM_PS_REGNUM, &ps);

  4776.   if ((val & 1) == 1)
  4777.     {
  4778.       regcache_cooked_write_unsigned (regs, ARM_PS_REGNUM, ps | t_bit);
  4779.       regcache_cooked_write_unsigned (regs, ARM_PC_REGNUM, val & 0xfffffffe);
  4780.     }
  4781.   else if ((val & 2) == 0)
  4782.     {
  4783.       regcache_cooked_write_unsigned (regs, ARM_PS_REGNUM, ps & ~t_bit);
  4784.       regcache_cooked_write_unsigned (regs, ARM_PC_REGNUM, val);
  4785.     }
  4786.   else
  4787.     {
  4788.       /* Unpredictable behaviour.  Try to do something sensible (switch to ARM
  4789.           mode, align dest to 4 bytes).  */
  4790.       warning (_("Single-stepping BX to non-word-aligned ARM instruction."));
  4791.       regcache_cooked_write_unsigned (regs, ARM_PS_REGNUM, ps & ~t_bit);
  4792.       regcache_cooked_write_unsigned (regs, ARM_PC_REGNUM, val & 0xfffffffc);
  4793.     }
  4794. }

  4795. /* Write to the PC as if from a load instruction.  */

  4796. static void
  4797. load_write_pc (struct regcache *regs, struct displaced_step_closure *dsc,
  4798.                ULONGEST val)
  4799. {
  4800.   if (DISPLACED_STEPPING_ARCH_VERSION >= 5)
  4801.     bx_write_pc (regs, val);
  4802.   else
  4803.     branch_write_pc (regs, dsc, val);
  4804. }

  4805. /* Write to the PC as if from an ALU instruction.  */

  4806. static void
  4807. alu_write_pc (struct regcache *regs, struct displaced_step_closure *dsc,
  4808.               ULONGEST val)
  4809. {
  4810.   if (DISPLACED_STEPPING_ARCH_VERSION >= 7 && !dsc->is_thumb)
  4811.     bx_write_pc (regs, val);
  4812.   else
  4813.     branch_write_pc (regs, dsc, val);
  4814. }

  4815. /* Helper for writing to registers for displaced stepping.  Writing to the PC
  4816.    has a varying effects depending on the instruction which does the write:
  4817.    this is controlled by the WRITE_PC argument.  */

  4818. void
  4819. displaced_write_reg (struct regcache *regs, struct displaced_step_closure *dsc,
  4820.                      int regno, ULONGEST val, enum pc_write_style write_pc)
  4821. {
  4822.   if (regno == ARM_PC_REGNUM)
  4823.     {
  4824.       if (debug_displaced)
  4825.         fprintf_unfiltered (gdb_stdlog, "displaced: writing pc %.8lx\n",
  4826.                             (unsigned long) val);
  4827.       switch (write_pc)
  4828.         {
  4829.         case BRANCH_WRITE_PC:
  4830.           branch_write_pc (regs, dsc, val);
  4831.           break;

  4832.         case BX_WRITE_PC:
  4833.           bx_write_pc (regs, val);
  4834.             break;

  4835.         case LOAD_WRITE_PC:
  4836.           load_write_pc (regs, dsc, val);
  4837.             break;

  4838.         case ALU_WRITE_PC:
  4839.           alu_write_pc (regs, dsc, val);
  4840.             break;

  4841.         case CANNOT_WRITE_PC:
  4842.           warning (_("Instruction wrote to PC in an unexpected way when "
  4843.                      "single-stepping"));
  4844.           break;

  4845.         default:
  4846.           internal_error (__FILE__, __LINE__,
  4847.                           _("Invalid argument to displaced_write_reg"));
  4848.         }

  4849.       dsc->wrote_to_pc = 1;
  4850.     }
  4851.   else
  4852.     {
  4853.       if (debug_displaced)
  4854.         fprintf_unfiltered (gdb_stdlog, "displaced: writing r%d value %.8lx\n",
  4855.                             regno, (unsigned long) val);
  4856.       regcache_cooked_write_unsigned (regs, regno, val);
  4857.     }
  4858. }

  4859. /* This function is used to concisely determine if an instruction INSN
  4860.    references PC.  Register fields of interest in INSN should have the
  4861.    corresponding fields of BITMASK set to 0b1111.  The function
  4862.    returns return 1 if any of these fields in INSN reference the PC
  4863.    (also 0b1111, r15), else it returns 0.  */

  4864. static int
  4865. insn_references_pc (uint32_t insn, uint32_t bitmask)
  4866. {
  4867.   uint32_t lowbit = 1;

  4868.   while (bitmask != 0)
  4869.     {
  4870.       uint32_t mask;

  4871.       for (; lowbit && (bitmask & lowbit) == 0; lowbit <<= 1)
  4872.         ;

  4873.       if (!lowbit)
  4874.         break;

  4875.       mask = lowbit * 0xf;

  4876.       if ((insn & mask) == mask)
  4877.         return 1;

  4878.       bitmask &= ~mask;
  4879.     }

  4880.   return 0;
  4881. }

  4882. /* The simplest copy function.  Many instructions have the same effect no
  4883.    matter what address they are executed at: in those cases, use this.  */

  4884. static int
  4885. arm_copy_unmodified (struct gdbarch *gdbarch, uint32_t insn,
  4886.                      const char *iname, struct displaced_step_closure *dsc)
  4887. {
  4888.   if (debug_displaced)
  4889.     fprintf_unfiltered (gdb_stdlog, "displaced: copying insn %.8lx, "
  4890.                         "opcode/class '%s' unmodified\n", (unsigned long) insn,
  4891.                         iname);

  4892.   dsc->modinsn[0] = insn;

  4893.   return 0;
  4894. }

  4895. static int
  4896. thumb_copy_unmodified_32bit (struct gdbarch *gdbarch, uint16_t insn1,
  4897.                              uint16_t insn2, const char *iname,
  4898.                              struct displaced_step_closure *dsc)
  4899. {
  4900.   if (debug_displaced)
  4901.     fprintf_unfiltered (gdb_stdlog, "displaced: copying insn %.4x %.4x, "
  4902.                         "opcode/class '%s' unmodified\n", insn1, insn2,
  4903.                         iname);

  4904.   dsc->modinsn[0] = insn1;
  4905.   dsc->modinsn[1] = insn2;
  4906.   dsc->numinsns = 2;

  4907.   return 0;
  4908. }

  4909. /* Copy 16-bit Thumb(Thumb and 16-bit Thumb-2) instruction without any
  4910.    modification.  */
  4911. static int
  4912. thumb_copy_unmodified_16bit (struct gdbarch *gdbarch, unsigned int insn,
  4913.                              const char *iname,
  4914.                              struct displaced_step_closure *dsc)
  4915. {
  4916.   if (debug_displaced)
  4917.     fprintf_unfiltered (gdb_stdlog, "displaced: copying insn %.4x, "
  4918.                         "opcode/class '%s' unmodified\n", insn,
  4919.                         iname);

  4920.   dsc->modinsn[0] = insn;

  4921.   return 0;
  4922. }

  4923. /* Preload instructions with immediate offset.  */

  4924. static void
  4925. cleanup_preload (struct gdbarch *gdbarch,
  4926.                  struct regcache *regs, struct displaced_step_closure *dsc)
  4927. {
  4928.   displaced_write_reg (regs, dsc, 0, dsc->tmp[0], CANNOT_WRITE_PC);
  4929.   if (!dsc->u.preload.immed)
  4930.     displaced_write_reg (regs, dsc, 1, dsc->tmp[1], CANNOT_WRITE_PC);
  4931. }

  4932. static void
  4933. install_preload (struct gdbarch *gdbarch, struct regcache *regs,
  4934.                  struct displaced_step_closure *dsc, unsigned int rn)
  4935. {
  4936.   ULONGEST rn_val;
  4937.   /* Preload instructions:

  4938.      {pli/pld} [rn, #+/-imm]
  4939.      ->
  4940.      {pli/pld} [r0, #+/-imm].  */

  4941.   dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
  4942.   rn_val = displaced_read_reg (regs, dsc, rn);
  4943.   displaced_write_reg (regs, dsc, 0, rn_val, CANNOT_WRITE_PC);
  4944.   dsc->u.preload.immed = 1;

  4945.   dsc->cleanup = &cleanup_preload;
  4946. }

  4947. static int
  4948. arm_copy_preload (struct gdbarch *gdbarch, uint32_t insn, struct regcache *regs,
  4949.                   struct displaced_step_closure *dsc)
  4950. {
  4951.   unsigned int rn = bits (insn, 16, 19);

  4952.   if (!insn_references_pc (insn, 0x000f0000ul))
  4953.     return arm_copy_unmodified (gdbarch, insn, "preload", dsc);

  4954.   if (debug_displaced)
  4955.     fprintf_unfiltered (gdb_stdlog, "displaced: copying preload insn %.8lx\n",
  4956.                         (unsigned long) insn);

  4957.   dsc->modinsn[0] = insn & 0xfff0ffff;

  4958.   install_preload (gdbarch, regs, dsc, rn);

  4959.   return 0;
  4960. }

  4961. static int
  4962. thumb2_copy_preload (struct gdbarch *gdbarch, uint16_t insn1, uint16_t insn2,
  4963.                      struct regcache *regs, struct displaced_step_closure *dsc)
  4964. {
  4965.   unsigned int rn = bits (insn1, 0, 3);
  4966.   unsigned int u_bit = bit (insn1, 7);
  4967.   int imm12 = bits (insn2, 0, 11);
  4968.   ULONGEST pc_val;

  4969.   if (rn != ARM_PC_REGNUM)
  4970.     return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2, "preload", dsc);

  4971.   /* PC is only allowed to use in PLI (immediate,literal) Encoding T3, and
  4972.      PLD (literal) Encoding T1.  */
  4973.   if (debug_displaced)
  4974.     fprintf_unfiltered (gdb_stdlog,
  4975.                         "displaced: copying pld/pli pc (0x%x) %c imm12 %.4x\n",
  4976.                         (unsigned int) dsc->insn_addr, u_bit ? '+' : '-',
  4977.                         imm12);

  4978.   if (!u_bit)
  4979.     imm12 = -1 * imm12;

  4980.   /* Rewrite instruction {pli/pld} PC imm12 into:
  4981.      Prepare: tmp[0] <- r0, tmp[1] <- r1, r0 <- pc, r1 <- imm12

  4982.      {pli/pld} [r0, r1]

  4983.      Cleanup: r0 <- tmp[0], r1 <- tmp[1].  */

  4984.   dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
  4985.   dsc->tmp[1] = displaced_read_reg (regs, dsc, 1);

  4986.   pc_val = displaced_read_reg (regs, dsc, ARM_PC_REGNUM);

  4987.   displaced_write_reg (regs, dsc, 0, pc_val, CANNOT_WRITE_PC);
  4988.   displaced_write_reg (regs, dsc, 1, imm12, CANNOT_WRITE_PC);
  4989.   dsc->u.preload.immed = 0;

  4990.   /* {pli/pld} [r0, r1] */
  4991.   dsc->modinsn[0] = insn1 & 0xfff0;
  4992.   dsc->modinsn[1] = 0xf001;
  4993.   dsc->numinsns = 2;

  4994.   dsc->cleanup = &cleanup_preload;
  4995.   return 0;
  4996. }

  4997. /* Preload instructions with register offset.  */

  4998. static void
  4999. install_preload_reg(struct gdbarch *gdbarch, struct regcache *regs,
  5000.                     struct displaced_step_closure *dsc, unsigned int rn,
  5001.                     unsigned int rm)
  5002. {
  5003.   ULONGEST rn_val, rm_val;

  5004.   /* Preload register-offset instructions:

  5005.      {pli/pld} [rn, rm {, shift}]
  5006.      ->
  5007.      {pli/pld} [r0, r1 {, shift}].  */

  5008.   dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
  5009.   dsc->tmp[1] = displaced_read_reg (regs, dsc, 1);
  5010.   rn_val = displaced_read_reg (regs, dsc, rn);
  5011.   rm_val = displaced_read_reg (regs, dsc, rm);
  5012.   displaced_write_reg (regs, dsc, 0, rn_val, CANNOT_WRITE_PC);
  5013.   displaced_write_reg (regs, dsc, 1, rm_val, CANNOT_WRITE_PC);
  5014.   dsc->u.preload.immed = 0;

  5015.   dsc->cleanup = &cleanup_preload;
  5016. }

  5017. static int
  5018. arm_copy_preload_reg (struct gdbarch *gdbarch, uint32_t insn,
  5019.                       struct regcache *regs,
  5020.                       struct displaced_step_closure *dsc)
  5021. {
  5022.   unsigned int rn = bits (insn, 16, 19);
  5023.   unsigned int rm = bits (insn, 0, 3);


  5024.   if (!insn_references_pc (insn, 0x000f000ful))
  5025.     return arm_copy_unmodified (gdbarch, insn, "preload reg", dsc);

  5026.   if (debug_displaced)
  5027.     fprintf_unfiltered (gdb_stdlog, "displaced: copying preload insn %.8lx\n",
  5028.                         (unsigned long) insn);

  5029.   dsc->modinsn[0] = (insn & 0xfff0fff0) | 0x1;

  5030.   install_preload_reg (gdbarch, regs, dsc, rn, rm);
  5031.   return 0;
  5032. }

  5033. /* Copy/cleanup coprocessor load and store instructions.  */

  5034. static void
  5035. cleanup_copro_load_store (struct gdbarch *gdbarch,
  5036.                           struct regcache *regs,
  5037.                           struct displaced_step_closure *dsc)
  5038. {
  5039.   ULONGEST rn_val = displaced_read_reg (regs, dsc, 0);

  5040.   displaced_write_reg (regs, dsc, 0, dsc->tmp[0], CANNOT_WRITE_PC);

  5041.   if (dsc->u.ldst.writeback)
  5042.     displaced_write_reg (regs, dsc, dsc->u.ldst.rn, rn_val, LOAD_WRITE_PC);
  5043. }

  5044. static void
  5045. install_copro_load_store (struct gdbarch *gdbarch, struct regcache *regs,
  5046.                           struct displaced_step_closure *dsc,
  5047.                           int writeback, unsigned int rn)
  5048. {
  5049.   ULONGEST rn_val;

  5050.   /* Coprocessor load/store instructions:

  5051.      {stc/stc2} [<Rn>, #+/-imm]  (and other immediate addressing modes)
  5052.      ->
  5053.      {stc/stc2} [r0, #+/-imm].

  5054.      ldc/ldc2 are handled identically.  */

  5055.   dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
  5056.   rn_val = displaced_read_reg (regs, dsc, rn);
  5057.   /* PC should be 4-byte aligned.  */
  5058.   rn_val = rn_val & 0xfffffffc;
  5059.   displaced_write_reg (regs, dsc, 0, rn_val, CANNOT_WRITE_PC);

  5060.   dsc->u.ldst.writeback = writeback;
  5061.   dsc->u.ldst.rn = rn;

  5062.   dsc->cleanup = &cleanup_copro_load_store;
  5063. }

  5064. static int
  5065. arm_copy_copro_load_store (struct gdbarch *gdbarch, uint32_t insn,
  5066.                            struct regcache *regs,
  5067.                            struct displaced_step_closure *dsc)
  5068. {
  5069.   unsigned int rn = bits (insn, 16, 19);

  5070.   if (!insn_references_pc (insn, 0x000f0000ul))
  5071.     return arm_copy_unmodified (gdbarch, insn, "copro load/store", dsc);

  5072.   if (debug_displaced)
  5073.     fprintf_unfiltered (gdb_stdlog, "displaced: copying coprocessor "
  5074.                         "load/store insn %.8lx\n", (unsigned long) insn);

  5075.   dsc->modinsn[0] = insn & 0xfff0ffff;

  5076.   install_copro_load_store (gdbarch, regs, dsc, bit (insn, 25), rn);

  5077.   return 0;
  5078. }

  5079. static int
  5080. thumb2_copy_copro_load_store (struct gdbarch *gdbarch, uint16_t insn1,
  5081.                               uint16_t insn2, struct regcache *regs,
  5082.                               struct displaced_step_closure *dsc)
  5083. {
  5084.   unsigned int rn = bits (insn1, 0, 3);

  5085.   if (rn != ARM_PC_REGNUM)
  5086.     return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
  5087.                                         "copro load/store", dsc);

  5088.   if (debug_displaced)
  5089.     fprintf_unfiltered (gdb_stdlog, "displaced: copying coprocessor "
  5090.                         "load/store insn %.4x%.4x\n", insn1, insn2);

  5091.   dsc->modinsn[0] = insn1 & 0xfff0;
  5092.   dsc->modinsn[1] = insn2;
  5093.   dsc->numinsns = 2;

  5094.   /* This function is called for copying instruction LDC/LDC2/VLDR, which
  5095.      doesn't support writeback, so pass 0.  */
  5096.   install_copro_load_store (gdbarch, regs, dsc, 0, rn);

  5097.   return 0;
  5098. }

  5099. /* Clean up branch instructions (actually perform the branch, by setting
  5100.    PC).  */

  5101. static void
  5102. cleanup_branch (struct gdbarch *gdbarch, struct regcache *regs,
  5103.                 struct displaced_step_closure *dsc)
  5104. {
  5105.   uint32_t status = displaced_read_reg (regs, dsc, ARM_PS_REGNUM);
  5106.   int branch_taken = condition_true (dsc->u.branch.cond, status);
  5107.   enum pc_write_style write_pc = dsc->u.branch.exchange
  5108.                                  ? BX_WRITE_PC : BRANCH_WRITE_PC;

  5109.   if (!branch_taken)
  5110.     return;

  5111.   if (dsc->u.branch.link)
  5112.     {
  5113.       /* The value of LR should be the next insn of current one.  In order
  5114.        not to confuse logic hanlding later insn `bx lr', if current insn mode
  5115.        is Thumb, the bit 0 of LR value should be set to 1.  */
  5116.       ULONGEST next_insn_addr = dsc->insn_addr + dsc->insn_size;

  5117.       if (dsc->is_thumb)
  5118.         next_insn_addr |= 0x1;

  5119.       displaced_write_reg (regs, dsc, ARM_LR_REGNUM, next_insn_addr,
  5120.                            CANNOT_WRITE_PC);
  5121.     }

  5122.   displaced_write_reg (regs, dsc, ARM_PC_REGNUM, dsc->u.branch.dest, write_pc);
  5123. }

  5124. /* Copy B/BL/BLX instructions with immediate destinations.  */

  5125. static void
  5126. install_b_bl_blx (struct gdbarch *gdbarch, struct regcache *regs,
  5127.                   struct displaced_step_closure *dsc,
  5128.                   unsigned int cond, int exchange, int link, long offset)
  5129. {
  5130.   /* Implement "BL<cond> <label>" as:

  5131.      Preparation: cond <- instruction condition
  5132.      Insn: mov r0, r0  (nop)
  5133.      Cleanup: if (condition true) { r14 <- pc; pc <- label }.

  5134.      B<cond> similar, but don't set r14 in cleanup.  */

  5135.   dsc->u.branch.cond = cond;
  5136.   dsc->u.branch.link = link;
  5137.   dsc->u.branch.exchange = exchange;

  5138.   dsc->u.branch.dest = dsc->insn_addr;
  5139.   if (link && exchange)
  5140.     /* For BLX, offset is computed from the Align (PC, 4).  */
  5141.     dsc->u.branch.dest = dsc->u.branch.dest & 0xfffffffc;

  5142.   if (dsc->is_thumb)
  5143.     dsc->u.branch.dest += 4 + offset;
  5144.   else
  5145.     dsc->u.branch.dest += 8 + offset;

  5146.   dsc->cleanup = &cleanup_branch;
  5147. }
  5148. static int
  5149. arm_copy_b_bl_blx (struct gdbarch *gdbarch, uint32_t insn,
  5150.                    struct regcache *regs, struct displaced_step_closure *dsc)
  5151. {
  5152.   unsigned int cond = bits (insn, 28, 31);
  5153.   int exchange = (cond == 0xf);
  5154.   int link = exchange || bit (insn, 24);
  5155.   long offset;

  5156.   if (debug_displaced)
  5157.     fprintf_unfiltered (gdb_stdlog, "displaced: copying %s immediate insn "
  5158.                         "%.8lx\n", (exchange) ? "blx" : (link) ? "bl" : "b",
  5159.                         (unsigned long) insn);
  5160.   if (exchange)
  5161.     /* For BLX, set bit 0 of the destination.  The cleanup_branch function will
  5162.        then arrange the switch into Thumb mode.  */
  5163.     offset = (bits (insn, 0, 23) << 2) | (bit (insn, 24) << 1) | 1;
  5164.   else
  5165.     offset = bits (insn, 0, 23) << 2;

  5166.   if (bit (offset, 25))
  5167.     offset = offset | ~0x3ffffff;

  5168.   dsc->modinsn[0] = ARM_NOP;

  5169.   install_b_bl_blx (gdbarch, regs, dsc, cond, exchange, link, offset);
  5170.   return 0;
  5171. }

  5172. static int
  5173. thumb2_copy_b_bl_blx (struct gdbarch *gdbarch, uint16_t insn1,
  5174.                       uint16_t insn2, struct regcache *regs,
  5175.                       struct displaced_step_closure *dsc)
  5176. {
  5177.   int link = bit (insn2, 14);
  5178.   int exchange = link && !bit (insn2, 12);
  5179.   int cond = INST_AL;
  5180.   long offset = 0;
  5181.   int j1 = bit (insn2, 13);
  5182.   int j2 = bit (insn2, 11);
  5183.   int s = sbits (insn1, 10, 10);
  5184.   int i1 = !(j1 ^ bit (insn1, 10));
  5185.   int i2 = !(j2 ^ bit (insn1, 10));

  5186.   if (!link && !exchange) /* B */
  5187.     {
  5188.       offset = (bits (insn2, 0, 10) << 1);
  5189.       if (bit (insn2, 12)) /* Encoding T4 */
  5190.         {
  5191.           offset |= (bits (insn1, 0, 9) << 12)
  5192.             | (i2 << 22)
  5193.             | (i1 << 23)
  5194.             | (s << 24);
  5195.           cond = INST_AL;
  5196.         }
  5197.       else /* Encoding T3 */
  5198.         {
  5199.           offset |= (bits (insn1, 0, 5) << 12)
  5200.             | (j1 << 18)
  5201.             | (j2 << 19)
  5202.             | (s << 20);
  5203.           cond = bits (insn1, 6, 9);
  5204.         }
  5205.     }
  5206.   else
  5207.     {
  5208.       offset = (bits (insn1, 0, 9) << 12);
  5209.       offset |= ((i2 << 22) | (i1 << 23) | (s << 24));
  5210.       offset |= exchange ?
  5211.         (bits (insn2, 1, 10) << 2) : (bits (insn2, 0, 10) << 1);
  5212.     }

  5213.   if (debug_displaced)
  5214.     fprintf_unfiltered (gdb_stdlog, "displaced: copying %s insn "
  5215.                         "%.4x %.4x with offset %.8lx\n",
  5216.                         link ? (exchange) ? "blx" : "bl" : "b",
  5217.                         insn1, insn2, offset);

  5218.   dsc->modinsn[0] = THUMB_NOP;

  5219.   install_b_bl_blx (gdbarch, regs, dsc, cond, exchange, link, offset);
  5220.   return 0;
  5221. }

  5222. /* Copy B Thumb instructions.  */
  5223. static int
  5224. thumb_copy_b (struct gdbarch *gdbarch, unsigned short insn,
  5225.               struct displaced_step_closure *dsc)
  5226. {
  5227.   unsigned int cond = 0;
  5228.   int offset = 0;
  5229.   unsigned short bit_12_15 = bits (insn, 12, 15);
  5230.   CORE_ADDR from = dsc->insn_addr;

  5231.   if (bit_12_15 == 0xd)
  5232.     {
  5233.       /* offset = SignExtend (imm8:0, 32) */
  5234.       offset = sbits ((insn << 1), 0, 8);
  5235.       cond = bits (insn, 8, 11);
  5236.     }
  5237.   else if (bit_12_15 == 0xe) /* Encoding T2 */
  5238.     {
  5239.       offset = sbits ((insn << 1), 0, 11);
  5240.       cond = INST_AL;
  5241.     }

  5242.   if (debug_displaced)
  5243.     fprintf_unfiltered (gdb_stdlog,
  5244.                         "displaced: copying b immediate insn %.4x "
  5245.                         "with offset %d\n", insn, offset);

  5246.   dsc->u.branch.cond = cond;
  5247.   dsc->u.branch.link = 0;
  5248.   dsc->u.branch.exchange = 0;
  5249.   dsc->u.branch.dest = from + 4 + offset;

  5250.   dsc->modinsn[0] = THUMB_NOP;

  5251.   dsc->cleanup = &cleanup_branch;

  5252.   return 0;
  5253. }

  5254. /* Copy BX/BLX with register-specified destinations.  */

  5255. static void
  5256. install_bx_blx_reg (struct gdbarch *gdbarch, struct regcache *regs,
  5257.                     struct displaced_step_closure *dsc, int link,
  5258.                     unsigned int cond, unsigned int rm)
  5259. {
  5260.   /* Implement {BX,BLX}<cond> <reg>" as:

  5261.      Preparation: cond <- instruction condition
  5262.      Insn: mov r0, r0 (nop)
  5263.      Cleanup: if (condition true) { r14 <- pc; pc <- dest; }.

  5264.      Don't set r14 in cleanup for BX.  */

  5265.   dsc->u.branch.dest = displaced_read_reg (regs, dsc, rm);

  5266.   dsc->u.branch.cond = cond;
  5267.   dsc->u.branch.link = link;

  5268.   dsc->u.branch.exchange = 1;

  5269.   dsc->cleanup = &cleanup_branch;
  5270. }

  5271. static int
  5272. arm_copy_bx_blx_reg (struct gdbarch *gdbarch, uint32_t insn,
  5273.                      struct regcache *regs, struct displaced_step_closure *dsc)
  5274. {
  5275.   unsigned int cond = bits (insn, 28, 31);
  5276.   /* BX:  x12xxx1x
  5277.      BLX: x12xxx3x.  */
  5278.   int link = bit (insn, 5);
  5279.   unsigned int rm = bits (insn, 0, 3);

  5280.   if (debug_displaced)
  5281.     fprintf_unfiltered (gdb_stdlog, "displaced: copying insn %.8lx",
  5282.                         (unsigned long) insn);

  5283.   dsc->modinsn[0] = ARM_NOP;

  5284.   install_bx_blx_reg (gdbarch, regs, dsc, link, cond, rm);
  5285.   return 0;
  5286. }

  5287. static int
  5288. thumb_copy_bx_blx_reg (struct gdbarch *gdbarch, uint16_t insn,
  5289.                        struct regcache *regs,
  5290.                        struct displaced_step_closure *dsc)
  5291. {
  5292.   int link = bit (insn, 7);
  5293.   unsigned int rm = bits (insn, 3, 6);

  5294.   if (debug_displaced)
  5295.     fprintf_unfiltered (gdb_stdlog, "displaced: copying insn %.4x",
  5296.                         (unsigned short) insn);

  5297.   dsc->modinsn[0] = THUMB_NOP;

  5298.   install_bx_blx_reg (gdbarch, regs, dsc, link, INST_AL, rm);

  5299.   return 0;
  5300. }


  5301. /* Copy/cleanup arithmetic/logic instruction with immediate RHS.  */

  5302. static void
  5303. cleanup_alu_imm (struct gdbarch *gdbarch,
  5304.                  struct regcache *regs, struct displaced_step_closure *dsc)
  5305. {
  5306.   ULONGEST rd_val = displaced_read_reg (regs, dsc, 0);
  5307.   displaced_write_reg (regs, dsc, 0, dsc->tmp[0], CANNOT_WRITE_PC);
  5308.   displaced_write_reg (regs, dsc, 1, dsc->tmp[1], CANNOT_WRITE_PC);
  5309.   displaced_write_reg (regs, dsc, dsc->rd, rd_val, ALU_WRITE_PC);
  5310. }

  5311. static int
  5312. arm_copy_alu_imm (struct gdbarch *gdbarch, uint32_t insn, struct regcache *regs,
  5313.                   struct displaced_step_closure *dsc)
  5314. {
  5315.   unsigned int rn = bits (insn, 16, 19);
  5316.   unsigned int rd = bits (insn, 12, 15);
  5317.   unsigned int op = bits (insn, 21, 24);
  5318.   int is_mov = (op == 0xd);
  5319.   ULONGEST rd_val, rn_val;

  5320.   if (!insn_references_pc (insn, 0x000ff000ul))
  5321.     return arm_copy_unmodified (gdbarch, insn, "ALU immediate", dsc);

  5322.   if (debug_displaced)
  5323.     fprintf_unfiltered (gdb_stdlog, "displaced: copying immediate %s insn "
  5324.                         "%.8lx\n", is_mov ? "move" : "ALU",
  5325.                         (unsigned long) insn);

  5326.   /* Instruction is of form:

  5327.      <op><cond> rd, [rn,] #imm

  5328.      Rewrite as:

  5329.      Preparation: tmp1, tmp2 <- r0, r1;
  5330.                   r0, r1 <- rd, rn
  5331.      Insn: <op><cond> r0, r1, #imm
  5332.      Cleanup: rd <- r0; r0 <- tmp1; r1 <- tmp2
  5333.   */

  5334.   dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
  5335.   dsc->tmp[1] = displaced_read_reg (regs, dsc, 1);
  5336.   rn_val = displaced_read_reg (regs, dsc, rn);
  5337.   rd_val = displaced_read_reg (regs, dsc, rd);
  5338.   displaced_write_reg (regs, dsc, 0, rd_val, CANNOT_WRITE_PC);
  5339.   displaced_write_reg (regs, dsc, 1, rn_val, CANNOT_WRITE_PC);
  5340.   dsc->rd = rd;

  5341.   if (is_mov)
  5342.     dsc->modinsn[0] = insn & 0xfff00fff;
  5343.   else
  5344.     dsc->modinsn[0] = (insn & 0xfff00fff) | 0x10000;

  5345.   dsc->cleanup = &cleanup_alu_imm;

  5346.   return 0;
  5347. }

  5348. static int
  5349. thumb2_copy_alu_imm (struct gdbarch *gdbarch, uint16_t insn1,
  5350.                      uint16_t insn2, struct regcache *regs,
  5351.                      struct displaced_step_closure *dsc)
  5352. {
  5353.   unsigned int op = bits (insn1, 5, 8);
  5354.   unsigned int rn, rm, rd;
  5355.   ULONGEST rd_val, rn_val;

  5356.   rn = bits (insn1, 0, 3); /* Rn */
  5357.   rm = bits (insn2, 0, 3); /* Rm */
  5358.   rd = bits (insn2, 8, 11); /* Rd */

  5359.   /* This routine is only called for instruction MOV.  */
  5360.   gdb_assert (op == 0x2 && rn == 0xf);

  5361.   if (rm != ARM_PC_REGNUM && rd != ARM_PC_REGNUM)
  5362.     return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2, "ALU imm", dsc);

  5363.   if (debug_displaced)
  5364.     fprintf_unfiltered (gdb_stdlog, "displaced: copying reg %s insn %.4x%.4x\n",
  5365.                         "ALU", insn1, insn2);

  5366.   /* Instruction is of form:

  5367.      <op><cond> rd, [rn,] #imm

  5368.      Rewrite as:

  5369.      Preparation: tmp1, tmp2 <- r0, r1;
  5370.                   r0, r1 <- rd, rn
  5371.      Insn: <op><cond> r0, r1, #imm
  5372.      Cleanup: rd <- r0; r0 <- tmp1; r1 <- tmp2
  5373.   */

  5374.   dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
  5375.   dsc->tmp[1] = displaced_read_reg (regs, dsc, 1);
  5376.   rn_val = displaced_read_reg (regs, dsc, rn);
  5377.   rd_val = displaced_read_reg (regs, dsc, rd);
  5378.   displaced_write_reg (regs, dsc, 0, rd_val, CANNOT_WRITE_PC);
  5379.   displaced_write_reg (regs, dsc, 1, rn_val, CANNOT_WRITE_PC);
  5380.   dsc->rd = rd;

  5381.   dsc->modinsn[0] = insn1;
  5382.   dsc->modinsn[1] = ((insn2 & 0xf0f0) | 0x1);
  5383.   dsc->numinsns = 2;

  5384.   dsc->cleanup = &cleanup_alu_imm;

  5385.   return 0;
  5386. }

  5387. /* Copy/cleanup arithmetic/logic insns with register RHS.  */

  5388. static void
  5389. cleanup_alu_reg (struct gdbarch *gdbarch,
  5390.                  struct regcache *regs, struct displaced_step_closure *dsc)
  5391. {
  5392.   ULONGEST rd_val;
  5393.   int i;

  5394.   rd_val = displaced_read_reg (regs, dsc, 0);

  5395.   for (i = 0; i < 3; i++)
  5396.     displaced_write_reg (regs, dsc, i, dsc->tmp[i], CANNOT_WRITE_PC);

  5397.   displaced_write_reg (regs, dsc, dsc->rd, rd_val, ALU_WRITE_PC);
  5398. }

  5399. static void
  5400. install_alu_reg (struct gdbarch *gdbarch, struct regcache *regs,
  5401.                  struct displaced_step_closure *dsc,
  5402.                  unsigned int rd, unsigned int rn, unsigned int rm)
  5403. {
  5404.   ULONGEST rd_val, rn_val, rm_val;

  5405.   /* Instruction is of form:

  5406.      <op><cond> rd, [rn,] rm [, <shift>]

  5407.      Rewrite as:

  5408.      Preparation: tmp1, tmp2, tmp3 <- r0, r1, r2;
  5409.                   r0, r1, r2 <- rd, rn, rm
  5410.      Insn: <op><cond> r0, r1, r2 [, <shift>]
  5411.      Cleanup: rd <- r0; r0, r1, r2 <- tmp1, tmp2, tmp3
  5412.   */

  5413.   dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
  5414.   dsc->tmp[1] = displaced_read_reg (regs, dsc, 1);
  5415.   dsc->tmp[2] = displaced_read_reg (regs, dsc, 2);
  5416.   rd_val = displaced_read_reg (regs, dsc, rd);
  5417.   rn_val = displaced_read_reg (regs, dsc, rn);
  5418.   rm_val = displaced_read_reg (regs, dsc, rm);
  5419.   displaced_write_reg (regs, dsc, 0, rd_val, CANNOT_WRITE_PC);
  5420.   displaced_write_reg (regs, dsc, 1, rn_val, CANNOT_WRITE_PC);
  5421.   displaced_write_reg (regs, dsc, 2, rm_val, CANNOT_WRITE_PC);
  5422.   dsc->rd = rd;

  5423.   dsc->cleanup = &cleanup_alu_reg;
  5424. }

  5425. static int
  5426. arm_copy_alu_reg (struct gdbarch *gdbarch, uint32_t insn, struct regcache *regs,
  5427.                   struct displaced_step_closure *dsc)
  5428. {
  5429.   unsigned int op = bits (insn, 21, 24);
  5430.   int is_mov = (op == 0xd);

  5431.   if (!insn_references_pc (insn, 0x000ff00ful))
  5432.     return arm_copy_unmodified (gdbarch, insn, "ALU reg", dsc);

  5433.   if (debug_displaced)
  5434.     fprintf_unfiltered (gdb_stdlog, "displaced: copying reg %s insn %.8lx\n",
  5435.                         is_mov ? "move" : "ALU", (unsigned long) insn);

  5436.   if (is_mov)
  5437.     dsc->modinsn[0] = (insn & 0xfff00ff0) | 0x2;
  5438.   else
  5439.     dsc->modinsn[0] = (insn & 0xfff00ff0) | 0x10002;

  5440.   install_alu_reg (gdbarch, regs, dsc, bits (insn, 12, 15), bits (insn, 16, 19),
  5441.                    bits (insn, 0, 3));
  5442.   return 0;
  5443. }

  5444. static int
  5445. thumb_copy_alu_reg (struct gdbarch *gdbarch, uint16_t insn,
  5446.                     struct regcache *regs,
  5447.                     struct displaced_step_closure *dsc)
  5448. {
  5449.   unsigned rn, rm, rd;

  5450.   rd = bits (insn, 3, 6);
  5451.   rn = (bit (insn, 7) << 3) | bits (insn, 0, 2);
  5452.   rm = 2;

  5453.   if (rd != ARM_PC_REGNUM && rn != ARM_PC_REGNUM)
  5454.     return thumb_copy_unmodified_16bit (gdbarch, insn, "ALU reg", dsc);

  5455.   if (debug_displaced)
  5456.     fprintf_unfiltered (gdb_stdlog, "displaced: copying reg %s insn %.4x\n",
  5457.                         "ALU", (unsigned short) insn);

  5458.   dsc->modinsn[0] = ((insn & 0xff00) | 0x08);

  5459.   install_alu_reg (gdbarch, regs, dsc, rd, rn, rm);

  5460.   return 0;
  5461. }

  5462. /* Cleanup/copy arithmetic/logic insns with shifted register RHS.  */

  5463. static void
  5464. cleanup_alu_shifted_reg (struct gdbarch *gdbarch,
  5465.                          struct regcache *regs,
  5466.                          struct displaced_step_closure *dsc)
  5467. {
  5468.   ULONGEST rd_val = displaced_read_reg (regs, dsc, 0);
  5469.   int i;

  5470.   for (i = 0; i < 4; i++)
  5471.     displaced_write_reg (regs, dsc, i, dsc->tmp[i], CANNOT_WRITE_PC);

  5472.   displaced_write_reg (regs, dsc, dsc->rd, rd_val, ALU_WRITE_PC);
  5473. }

  5474. static void
  5475. install_alu_shifted_reg (struct gdbarch *gdbarch, struct regcache *regs,
  5476.                          struct displaced_step_closure *dsc,
  5477.                          unsigned int rd, unsigned int rn, unsigned int rm,
  5478.                          unsigned rs)
  5479. {
  5480.   int i;
  5481.   ULONGEST rd_val, rn_val, rm_val, rs_val;

  5482.   /* Instruction is of form:

  5483.      <op><cond> rd, [rn,] rm, <shift> rs

  5484.      Rewrite as:

  5485.      Preparation: tmp1, tmp2, tmp3, tmp4 <- r0, r1, r2, r3
  5486.                   r0, r1, r2, r3 <- rd, rn, rm, rs
  5487.      Insn: <op><cond> r0, r1, r2, <shift> r3
  5488.      Cleanup: tmp5 <- r0
  5489.               r0, r1, r2, r3 <- tmp1, tmp2, tmp3, tmp4
  5490.               rd <- tmp5
  5491.   */

  5492.   for (i = 0; i < 4; i++)
  5493.     dsc->tmp[i] = displaced_read_reg (regs, dsc, i);

  5494.   rd_val = displaced_read_reg (regs, dsc, rd);
  5495.   rn_val = displaced_read_reg (regs, dsc, rn);
  5496.   rm_val = displaced_read_reg (regs, dsc, rm);
  5497.   rs_val = displaced_read_reg (regs, dsc, rs);
  5498.   displaced_write_reg (regs, dsc, 0, rd_val, CANNOT_WRITE_PC);
  5499.   displaced_write_reg (regs, dsc, 1, rn_val, CANNOT_WRITE_PC);
  5500.   displaced_write_reg (regs, dsc, 2, rm_val, CANNOT_WRITE_PC);
  5501.   displaced_write_reg (regs, dsc, 3, rs_val, CANNOT_WRITE_PC);
  5502.   dsc->rd = rd;
  5503.   dsc->cleanup = &cleanup_alu_shifted_reg;
  5504. }

  5505. static int
  5506. arm_copy_alu_shifted_reg (struct gdbarch *gdbarch, uint32_t insn,
  5507.                           struct regcache *regs,
  5508.                           struct displaced_step_closure *dsc)
  5509. {
  5510.   unsigned int op = bits (insn, 21, 24);
  5511.   int is_mov = (op == 0xd);
  5512.   unsigned int rd, rn, rm, rs;

  5513.   if (!insn_references_pc (insn, 0x000fff0ful))
  5514.     return arm_copy_unmodified (gdbarch, insn, "ALU shifted reg", dsc);

  5515.   if (debug_displaced)
  5516.     fprintf_unfiltered (gdb_stdlog, "displaced: copying shifted reg %s insn "
  5517.                         "%.8lx\n", is_mov ? "move" : "ALU",
  5518.                         (unsigned long) insn);

  5519.   rn = bits (insn, 16, 19);
  5520.   rm = bits (insn, 0, 3);
  5521.   rs = bits (insn, 8, 11);
  5522.   rd = bits (insn, 12, 15);

  5523.   if (is_mov)
  5524.     dsc->modinsn[0] = (insn & 0xfff000f0) | 0x302;
  5525.   else
  5526.     dsc->modinsn[0] = (insn & 0xfff000f0) | 0x10302;

  5527.   install_alu_shifted_reg (gdbarch, regs, dsc, rd, rn, rm, rs);

  5528.   return 0;
  5529. }

  5530. /* Clean up load instructions.  */

  5531. static void
  5532. cleanup_load (struct gdbarch *gdbarch, struct regcache *regs,
  5533.               struct displaced_step_closure *dsc)
  5534. {
  5535.   ULONGEST rt_val, rt_val2 = 0, rn_val;

  5536.   rt_val = displaced_read_reg (regs, dsc, 0);
  5537.   if (dsc->u.ldst.xfersize == 8)
  5538.     rt_val2 = displaced_read_reg (regs, dsc, 1);
  5539.   rn_val = displaced_read_reg (regs, dsc, 2);

  5540.   displaced_write_reg (regs, dsc, 0, dsc->tmp[0], CANNOT_WRITE_PC);
  5541.   if (dsc->u.ldst.xfersize > 4)
  5542.     displaced_write_reg (regs, dsc, 1, dsc->tmp[1], CANNOT_WRITE_PC);
  5543.   displaced_write_reg (regs, dsc, 2, dsc->tmp[2], CANNOT_WRITE_PC);
  5544.   if (!dsc->u.ldst.immed)
  5545.     displaced_write_reg (regs, dsc, 3, dsc->tmp[3], CANNOT_WRITE_PC);

  5546.   /* Handle register writeback.  */
  5547.   if (dsc->u.ldst.writeback)
  5548.     displaced_write_reg (regs, dsc, dsc->u.ldst.rn, rn_val, CANNOT_WRITE_PC);
  5549.   /* Put result in right place.  */
  5550.   displaced_write_reg (regs, dsc, dsc->rd, rt_val, LOAD_WRITE_PC);
  5551.   if (dsc->u.ldst.xfersize == 8)
  5552.     displaced_write_reg (regs, dsc, dsc->rd + 1, rt_val2, LOAD_WRITE_PC);
  5553. }

  5554. /* Clean up store instructions.  */

  5555. static void
  5556. cleanup_store (struct gdbarch *gdbarch, struct regcache *regs,
  5557.                struct displaced_step_closure *dsc)
  5558. {
  5559.   ULONGEST rn_val = displaced_read_reg (regs, dsc, 2);

  5560.   displaced_write_reg (regs, dsc, 0, dsc->tmp[0], CANNOT_WRITE_PC);
  5561.   if (dsc->u.ldst.xfersize > 4)
  5562.     displaced_write_reg (regs, dsc, 1, dsc->tmp[1], CANNOT_WRITE_PC);
  5563.   displaced_write_reg (regs, dsc, 2, dsc->tmp[2], CANNOT_WRITE_PC);
  5564.   if (!dsc->u.ldst.immed)
  5565.     displaced_write_reg (regs, dsc, 3, dsc->tmp[3], CANNOT_WRITE_PC);
  5566.   if (!dsc->u.ldst.restore_r4)
  5567.     displaced_write_reg (regs, dsc, 4, dsc->tmp[4], CANNOT_WRITE_PC);

  5568.   /* Writeback.  */
  5569.   if (dsc->u.ldst.writeback)
  5570.     displaced_write_reg (regs, dsc, dsc->u.ldst.rn, rn_val, CANNOT_WRITE_PC);
  5571. }

  5572. /* Copy "extra" load/store instructions.  These are halfword/doubleword
  5573.    transfers, which have a different encoding to byte/word transfers.  */

  5574. static int
  5575. arm_copy_extra_ld_st (struct gdbarch *gdbarch, uint32_t insn, int unpriveleged,
  5576.                       struct regcache *regs, struct displaced_step_closure *dsc)
  5577. {
  5578.   unsigned int op1 = bits (insn, 20, 24);
  5579.   unsigned int op2 = bits (insn, 5, 6);
  5580.   unsigned int rt = bits (insn, 12, 15);
  5581.   unsigned int rn = bits (insn, 16, 19);
  5582.   unsigned int rm = bits (insn, 0, 3);
  5583.   char load[12]     = {0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1};
  5584.   char bytesize[12] = {2, 2, 2, 2, 8, 1, 8, 1, 8, 2, 8, 2};
  5585.   int immed = (op1 & 0x4) != 0;
  5586.   int opcode;
  5587.   ULONGEST rt_val, rt_val2 = 0, rn_val, rm_val = 0;

  5588.   if (!insn_references_pc (insn, 0x000ff00ful))
  5589.     return arm_copy_unmodified (gdbarch, insn, "extra load/store", dsc);

  5590.   if (debug_displaced)
  5591.     fprintf_unfiltered (gdb_stdlog, "displaced: copying %sextra load/store "
  5592.                         "insn %.8lx\n", unpriveleged ? "unpriveleged " : "",
  5593.                         (unsigned long) insn);

  5594.   opcode = ((op2 << 2) | (op1 & 0x1) | ((op1 & 0x4) >> 1)) - 4;

  5595.   if (opcode < 0)
  5596.     internal_error (__FILE__, __LINE__,
  5597.                     _("copy_extra_ld_st: instruction decode error"));

  5598.   dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
  5599.   dsc->tmp[1] = displaced_read_reg (regs, dsc, 1);
  5600.   dsc->tmp[2] = displaced_read_reg (regs, dsc, 2);
  5601.   if (!immed)
  5602.     dsc->tmp[3] = displaced_read_reg (regs, dsc, 3);

  5603.   rt_val = displaced_read_reg (regs, dsc, rt);
  5604.   if (bytesize[opcode] == 8)
  5605.     rt_val2 = displaced_read_reg (regs, dsc, rt + 1);
  5606.   rn_val = displaced_read_reg (regs, dsc, rn);
  5607.   if (!immed)
  5608.     rm_val = displaced_read_reg (regs, dsc, rm);

  5609.   displaced_write_reg (regs, dsc, 0, rt_val, CANNOT_WRITE_PC);
  5610.   if (bytesize[opcode] == 8)
  5611.     displaced_write_reg (regs, dsc, 1, rt_val2, CANNOT_WRITE_PC);
  5612.   displaced_write_reg (regs, dsc, 2, rn_val, CANNOT_WRITE_PC);
  5613.   if (!immed)
  5614.     displaced_write_reg (regs, dsc, 3, rm_val, CANNOT_WRITE_PC);

  5615.   dsc->rd = rt;
  5616.   dsc->u.ldst.xfersize = bytesize[opcode];
  5617.   dsc->u.ldst.rn = rn;
  5618.   dsc->u.ldst.immed = immed;
  5619.   dsc->u.ldst.writeback = bit (insn, 24) == 0 || bit (insn, 21) != 0;
  5620.   dsc->u.ldst.restore_r4 = 0;

  5621.   if (immed)
  5622.     /* {ldr,str}<width><cond> rt, [rt2,] [rn, #imm]
  5623.         ->
  5624.        {ldr,str}<width><cond> r0, [r1,] [r2, #imm].  */
  5625.     dsc->modinsn[0] = (insn & 0xfff00fff) | 0x20000;
  5626.   else
  5627.     /* {ldr,str}<width><cond> rt, [rt2,] [rn, +/-rm]
  5628.         ->
  5629.        {ldr,str}<width><cond> r0, [r1,] [r2, +/-r3].  */
  5630.     dsc->modinsn[0] = (insn & 0xfff00ff0) | 0x20003;

  5631.   dsc->cleanup = load[opcode] ? &cleanup_load : &cleanup_store;

  5632.   return 0;
  5633. }

  5634. /* Copy byte/half word/word loads and stores.  */

  5635. static void
  5636. install_load_store (struct gdbarch *gdbarch, struct regcache *regs,
  5637.                     struct displaced_step_closure *dsc, int load,
  5638.                     int immed, int writeback, int size, int usermode,
  5639.                     int rt, int rm, int rn)
  5640. {
  5641.   ULONGEST rt_val, rn_val, rm_val = 0;

  5642.   dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
  5643.   dsc->tmp[2] = displaced_read_reg (regs, dsc, 2);
  5644.   if (!immed)
  5645.     dsc->tmp[3] = displaced_read_reg (regs, dsc, 3);
  5646.   if (!load)
  5647.     dsc->tmp[4] = displaced_read_reg (regs, dsc, 4);

  5648.   rt_val = displaced_read_reg (regs, dsc, rt);
  5649.   rn_val = displaced_read_reg (regs, dsc, rn);
  5650.   if (!immed)
  5651.     rm_val = displaced_read_reg (regs, dsc, rm);

  5652.   displaced_write_reg (regs, dsc, 0, rt_val, CANNOT_WRITE_PC);
  5653.   displaced_write_reg (regs, dsc, 2, rn_val, CANNOT_WRITE_PC);
  5654.   if (!immed)
  5655.     displaced_write_reg (regs, dsc, 3, rm_val, CANNOT_WRITE_PC);
  5656.   dsc->rd = rt;
  5657.   dsc->u.ldst.xfersize = size;
  5658.   dsc->u.ldst.rn = rn;
  5659.   dsc->u.ldst.immed = immed;
  5660.   dsc->u.ldst.writeback = writeback;

  5661.   /* To write PC we can do:

  5662.      Before this sequence of instructions:
  5663.      r0 is the PC value got from displaced_read_reg, so r0 = from + 8;
  5664.      r2 is the Rn value got from dispalced_read_reg.

  5665.      Insn1: push {pc} Write address of STR instruction + offset on stack
  5666.      Insn2: pop  {r4} Read it back from stack, r4 = addr(Insn1) + offset
  5667.      Insn3: sub r4, r4, pc   r4 = addr(Insn1) + offset - pc
  5668.                                 = addr(Insn1) + offset - addr(Insn3) - 8
  5669.                                 = offset - 16
  5670.      Insn4: add r4, r4, #8   r4 = offset - 8
  5671.      Insn5: add r0, r0, r4   r0 = from + 8 + offset - 8
  5672.                                 = from + offset
  5673.      Insn6: str r0, [r2, #imm] (or str r0, [r2, r3])

  5674.      Otherwise we don't know what value to write for PC, since the offset is
  5675.      architecture-dependent (sometimes PC+8, sometimes PC+12).  More details
  5676.      of this can be found in Section "Saving from r15" in
  5677.      http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dui0204g/Cihbjifh.html */

  5678.   dsc->cleanup = load ? &cleanup_load : &cleanup_store;
  5679. }


  5680. static int
  5681. thumb2_copy_load_literal (struct gdbarch *gdbarch, uint16_t insn1,
  5682.                           uint16_t insn2, struct regcache *regs,
  5683.                           struct displaced_step_closure *dsc, int size)
  5684. {
  5685.   unsigned int u_bit = bit (insn1, 7);
  5686.   unsigned int rt = bits (insn2, 12, 15);
  5687.   int imm12 = bits (insn2, 0, 11);
  5688.   ULONGEST pc_val;

  5689.   if (debug_displaced)
  5690.     fprintf_unfiltered (gdb_stdlog,
  5691.                         "displaced: copying ldr pc (0x%x) R%d %c imm12 %.4x\n",
  5692.                         (unsigned int) dsc->insn_addr, rt, u_bit ? '+' : '-',
  5693.                         imm12);

  5694.   if (!u_bit)
  5695.     imm12 = -1 * imm12;

  5696.   /* Rewrite instruction LDR Rt imm12 into:

  5697.      Prepare: tmp[0] <- r0, tmp[1] <- r2, tmp[2] <- r3, r2 <- pc, r3 <- imm12

  5698.      LDR R0, R2, R3,

  5699.      Cleanup: rt <- r0, r0 <- tmp[0], r2 <- tmp[1], r3 <- tmp[2].  */


  5700.   dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
  5701.   dsc->tmp[2] = displaced_read_reg (regs, dsc, 2);
  5702.   dsc->tmp[3] = displaced_read_reg (regs, dsc, 3);

  5703.   pc_val = displaced_read_reg (regs, dsc, ARM_PC_REGNUM);

  5704.   pc_val = pc_val & 0xfffffffc;

  5705.   displaced_write_reg (regs, dsc, 2, pc_val, CANNOT_WRITE_PC);
  5706.   displaced_write_reg (regs, dsc, 3, imm12, CANNOT_WRITE_PC);

  5707.   dsc->rd = rt;

  5708.   dsc->u.ldst.xfersize = size;
  5709.   dsc->u.ldst.immed = 0;
  5710.   dsc->u.ldst.writeback = 0;
  5711.   dsc->u.ldst.restore_r4 = 0;

  5712.   /* LDR R0, R2, R3 */
  5713.   dsc->modinsn[0] = 0xf852;
  5714.   dsc->modinsn[1] = 0x3;
  5715.   dsc->numinsns = 2;

  5716.   dsc->cleanup = &cleanup_load;

  5717.   return 0;
  5718. }

  5719. static int
  5720. thumb2_copy_load_reg_imm (struct gdbarch *gdbarch, uint16_t insn1,
  5721.                           uint16_t insn2, struct regcache *regs,
  5722.                           struct displaced_step_closure *dsc,
  5723.                           int writeback, int immed)
  5724. {
  5725.   unsigned int rt = bits (insn2, 12, 15);
  5726.   unsigned int rn = bits (insn1, 0, 3);
  5727.   unsigned int rm = bits (insn2, 0, 3);  /* Only valid if !immed.  */
  5728.   /* In LDR (register), there is also a register Rm, which is not allowed to
  5729.      be PC, so we don't have to check it.  */

  5730.   if (rt != ARM_PC_REGNUM && rn != ARM_PC_REGNUM)
  5731.     return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2, "load",
  5732.                                         dsc);

  5733.   if (debug_displaced)
  5734.     fprintf_unfiltered (gdb_stdlog,
  5735.                         "displaced: copying ldr r%d [r%d] insn %.4x%.4x\n",
  5736.                          rt, rn, insn1, insn2);

  5737.   install_load_store (gdbarch, regs, dsc, 1, immed, writeback, 4,
  5738.                       0, rt, rm, rn);

  5739.   dsc->u.ldst.restore_r4 = 0;

  5740.   if (immed)
  5741.     /* ldr[b]<cond> rt, [rn, #imm], etc.
  5742.        ->
  5743.        ldr[b]<cond> r0, [r2, #imm].  */
  5744.     {
  5745.       dsc->modinsn[0] = (insn1 & 0xfff0) | 0x2;
  5746.       dsc->modinsn[1] = insn2 & 0x0fff;
  5747.     }
  5748.   else
  5749.     /* ldr[b]<cond> rt, [rn, rm], etc.
  5750.        ->
  5751.        ldr[b]<cond> r0, [r2, r3].  */
  5752.     {
  5753.       dsc->modinsn[0] = (insn1 & 0xfff0) | 0x2;
  5754.       dsc->modinsn[1] = (insn2 & 0x0ff0) | 0x3;
  5755.     }

  5756.   dsc->numinsns = 2;

  5757.   return 0;
  5758. }


  5759. static int
  5760. arm_copy_ldr_str_ldrb_strb (struct gdbarch *gdbarch, uint32_t insn,
  5761.                             struct regcache *regs,
  5762.                             struct displaced_step_closure *dsc,
  5763.                             int load, int size, int usermode)
  5764. {
  5765.   int immed = !bit (insn, 25);
  5766.   int writeback = (bit (insn, 24) == 0 || bit (insn, 21) != 0);
  5767.   unsigned int rt = bits (insn, 12, 15);
  5768.   unsigned int rn = bits (insn, 16, 19);
  5769.   unsigned int rm = bits (insn, 0, 3);  /* Only valid if !immed.  */

  5770.   if (!insn_references_pc (insn, 0x000ff00ful))
  5771.     return arm_copy_unmodified (gdbarch, insn, "load/store", dsc);

  5772.   if (debug_displaced)
  5773.     fprintf_unfiltered (gdb_stdlog,
  5774.                         "displaced: copying %s%s r%d [r%d] insn %.8lx\n",
  5775.                         load ? (size == 1 ? "ldrb" : "ldr")
  5776.                              : (size == 1 ? "strb" : "str"), usermode ? "t" : "",
  5777.                         rt, rn,
  5778.                         (unsigned long) insn);

  5779.   install_load_store (gdbarch, regs, dsc, load, immed, writeback, size,
  5780.                       usermode, rt, rm, rn);

  5781.   if (load || rt != ARM_PC_REGNUM)
  5782.     {
  5783.       dsc->u.ldst.restore_r4 = 0;

  5784.       if (immed)
  5785.         /* {ldr,str}[b]<cond> rt, [rn, #imm], etc.
  5786.            ->
  5787.            {ldr,str}[b]<cond> r0, [r2, #imm].  */
  5788.         dsc->modinsn[0] = (insn & 0xfff00fff) | 0x20000;
  5789.       else
  5790.         /* {ldr,str}[b]<cond> rt, [rn, rm], etc.
  5791.            ->
  5792.            {ldr,str}[b]<cond> r0, [r2, r3].  */
  5793.         dsc->modinsn[0] = (insn & 0xfff00ff0) | 0x20003;
  5794.     }
  5795.   else
  5796.     {
  5797.       /* We need to use r4 as scratch.  Make sure it's restored afterwards.  */
  5798.       dsc->u.ldst.restore_r4 = 1;
  5799.       dsc->modinsn[0] = 0xe92d8000/* push {pc} */
  5800.       dsc->modinsn[1] = 0xe8bd0010/* pop  {r4} */
  5801.       dsc->modinsn[2] = 0xe044400f/* sub r4, r4, pc.  */
  5802.       dsc->modinsn[3] = 0xe2844008/* add r4, r4, #8.  */
  5803.       dsc->modinsn[4] = 0xe0800004/* add r0, r0, r4.  */

  5804.       /* As above.  */
  5805.       if (immed)
  5806.         dsc->modinsn[5] = (insn & 0xfff00fff) | 0x20000;
  5807.       else
  5808.         dsc->modinsn[5] = (insn & 0xfff00ff0) | 0x20003;

  5809.       dsc->numinsns = 6;
  5810.     }

  5811.   dsc->cleanup = load ? &cleanup_load : &cleanup_store;

  5812.   return 0;
  5813. }

  5814. /* Cleanup LDM instructions with fully-populated register list.  This is an
  5815.    unfortunate corner case: it's impossible to implement correctly by modifying
  5816.    the instruction.  The issue is as follows: we have an instruction,

  5817.    ldm rN, {r0-r15}

  5818.    which we must rewrite to avoid loading PCA possible solution would be to
  5819.    do the load in two halves, something like (with suitable cleanup
  5820.    afterwards):

  5821.    mov r8, rN
  5822.    ldm[id][ab] r8!, {r0-r7}
  5823.    str r7, <temp>
  5824.    ldm[id][ab] r8, {r7-r14}
  5825.    <bkpt>

  5826.    but at present there's no suitable place for <temp>, since the scratch space
  5827.    is overwritten before the cleanup routine is called.  For now, we simply
  5828.    emulate the instruction.  */

  5829. static void
  5830. cleanup_block_load_all (struct gdbarch *gdbarch, struct regcache *regs,
  5831.                         struct displaced_step_closure *dsc)
  5832. {
  5833.   int inc = dsc->u.block.increment;
  5834.   int bump_before = dsc->u.block.before ? (inc ? 4 : -4) : 0;
  5835.   int bump_after = dsc->u.block.before ? 0 : (inc ? 4 : -4);
  5836.   uint32_t regmask = dsc->u.block.regmask;
  5837.   int regno = inc ? 0 : 15;
  5838.   CORE_ADDR xfer_addr = dsc->u.block.xfer_addr;
  5839.   int exception_return = dsc->u.block.load && dsc->u.block.user
  5840.                          && (regmask & 0x8000) != 0;
  5841.   uint32_t status = displaced_read_reg (regs, dsc, ARM_PS_REGNUM);
  5842.   int do_transfer = condition_true (dsc->u.block.cond, status);
  5843.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);

  5844.   if (!do_transfer)
  5845.     return;

  5846.   /* If the instruction is ldm rN, {...pc}^, I don't think there's anything
  5847.      sensible we can do here.  Complain loudly.  */
  5848.   if (exception_return)
  5849.     error (_("Cannot single-step exception return"));

  5850.   /* We don't handle any stores here for now.  */
  5851.   gdb_assert (dsc->u.block.load != 0);

  5852.   if (debug_displaced)
  5853.     fprintf_unfiltered (gdb_stdlog, "displaced: emulating block transfer: "
  5854.                         "%s %s %s\n", dsc->u.block.load ? "ldm" : "stm",
  5855.                         dsc->u.block.increment ? "inc" : "dec",
  5856.                         dsc->u.block.before ? "before" : "after");

  5857.   while (regmask)
  5858.     {
  5859.       uint32_t memword;

  5860.       if (inc)
  5861.         while (regno <= ARM_PC_REGNUM && (regmask & (1 << regno)) == 0)
  5862.           regno++;
  5863.       else
  5864.         while (regno >= 0 && (regmask & (1 << regno)) == 0)
  5865.           regno--;

  5866.       xfer_addr += bump_before;

  5867.       memword = read_memory_unsigned_integer (xfer_addr, 4, byte_order);
  5868.       displaced_write_reg (regs, dsc, regno, memword, LOAD_WRITE_PC);

  5869.       xfer_addr += bump_after;

  5870.       regmask &= ~(1 << regno);
  5871.     }

  5872.   if (dsc->u.block.writeback)
  5873.     displaced_write_reg (regs, dsc, dsc->u.block.rn, xfer_addr,
  5874.                          CANNOT_WRITE_PC);
  5875. }

  5876. /* Clean up an STM which included the PC in the register list.  */

  5877. static void
  5878. cleanup_block_store_pc (struct gdbarch *gdbarch, struct regcache *regs,
  5879.                         struct displaced_step_closure *dsc)
  5880. {
  5881.   uint32_t status = displaced_read_reg (regs, dsc, ARM_PS_REGNUM);
  5882.   int store_executed = condition_true (dsc->u.block.cond, status);
  5883.   CORE_ADDR pc_stored_at, transferred_regs = bitcount (dsc->u.block.regmask);
  5884.   CORE_ADDR stm_insn_addr;
  5885.   uint32_t pc_val;
  5886.   long offset;
  5887.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);

  5888.   /* If condition code fails, there's nothing else to do.  */
  5889.   if (!store_executed)
  5890.     return;

  5891.   if (dsc->u.block.increment)
  5892.     {
  5893.       pc_stored_at = dsc->u.block.xfer_addr + 4 * transferred_regs;

  5894.       if (dsc->u.block.before)
  5895.          pc_stored_at += 4;
  5896.     }
  5897.   else
  5898.     {
  5899.       pc_stored_at = dsc->u.block.xfer_addr;

  5900.       if (dsc->u.block.before)
  5901.          pc_stored_at -= 4;
  5902.     }

  5903.   pc_val = read_memory_unsigned_integer (pc_stored_at, 4, byte_order);
  5904.   stm_insn_addr = dsc->scratch_base;
  5905.   offset = pc_val - stm_insn_addr;

  5906.   if (debug_displaced)
  5907.     fprintf_unfiltered (gdb_stdlog, "displaced: detected PC offset %.8lx for "
  5908.                         "STM instruction\n", offset);

  5909.   /* Rewrite the stored PC to the proper value for the non-displaced original
  5910.      instruction.  */
  5911.   write_memory_unsigned_integer (pc_stored_at, 4, byte_order,
  5912.                                  dsc->insn_addr + offset);
  5913. }

  5914. /* Clean up an LDM which includes the PC in the register list.  We clumped all
  5915.    the registers in the transferred list into a contiguous range r0...rX (to
  5916.    avoid loading PC directly and losing control of the debugged program), so we
  5917.    must undo that here.  */

  5918. static void
  5919. cleanup_block_load_pc (struct gdbarch *gdbarch,
  5920.                        struct regcache *regs,
  5921.                        struct displaced_step_closure *dsc)
  5922. {
  5923.   uint32_t status = displaced_read_reg (regs, dsc, ARM_PS_REGNUM);
  5924.   int load_executed = condition_true (dsc->u.block.cond, status);
  5925.   unsigned int mask = dsc->u.block.regmask, write_reg = ARM_PC_REGNUM;
  5926.   unsigned int regs_loaded = bitcount (mask);
  5927.   unsigned int num_to_shuffle = regs_loaded, clobbered;

  5928.   /* The method employed here will fail if the register list is fully populated
  5929.      (we need to avoid loading PC directly).  */
  5930.   gdb_assert (num_to_shuffle < 16);

  5931.   if (!load_executed)
  5932.     return;

  5933.   clobbered = (1 << num_to_shuffle) - 1;

  5934.   while (num_to_shuffle > 0)
  5935.     {
  5936.       if ((mask & (1 << write_reg)) != 0)
  5937.         {
  5938.           unsigned int read_reg = num_to_shuffle - 1;

  5939.           if (read_reg != write_reg)
  5940.             {
  5941.               ULONGEST rval = displaced_read_reg (regs, dsc, read_reg);
  5942.               displaced_write_reg (regs, dsc, write_reg, rval, LOAD_WRITE_PC);
  5943.               if (debug_displaced)
  5944.                 fprintf_unfiltered (gdb_stdlog, _("displaced: LDM: move "
  5945.                                     "loaded register r%d to r%d\n"), read_reg,
  5946.                                     write_reg);
  5947.             }
  5948.           else if (debug_displaced)
  5949.             fprintf_unfiltered (gdb_stdlog, _("displaced: LDM: register "
  5950.                                 "r%d already in the right place\n"),
  5951.                                 write_reg);

  5952.           clobbered &= ~(1 << write_reg);

  5953.           num_to_shuffle--;
  5954.         }

  5955.       write_reg--;
  5956.     }

  5957.   /* Restore any registers we scribbled over.  */
  5958.   for (write_reg = 0; clobbered != 0; write_reg++)
  5959.     {
  5960.       if ((clobbered & (1 << write_reg)) != 0)
  5961.         {
  5962.           displaced_write_reg (regs, dsc, write_reg, dsc->tmp[write_reg],
  5963.                                CANNOT_WRITE_PC);
  5964.           if (debug_displaced)
  5965.             fprintf_unfiltered (gdb_stdlog, _("displaced: LDM: restored "
  5966.                                 "clobbered register r%d\n"), write_reg);
  5967.           clobbered &= ~(1 << write_reg);
  5968.         }
  5969.     }

  5970.   /* Perform register writeback manually.  */
  5971.   if (dsc->u.block.writeback)
  5972.     {
  5973.       ULONGEST new_rn_val = dsc->u.block.xfer_addr;

  5974.       if (dsc->u.block.increment)
  5975.         new_rn_val += regs_loaded * 4;
  5976.       else
  5977.         new_rn_val -= regs_loaded * 4;

  5978.       displaced_write_reg (regs, dsc, dsc->u.block.rn, new_rn_val,
  5979.                            CANNOT_WRITE_PC);
  5980.     }
  5981. }

  5982. /* Handle ldm/stm, apart from some tricky cases which are unlikely to occur
  5983.    in user-level code (in particular exception return, ldm rn, {...pc}^).  */

  5984. static int
  5985. arm_copy_block_xfer (struct gdbarch *gdbarch, uint32_t insn,
  5986.                      struct regcache *regs,
  5987.                      struct displaced_step_closure *dsc)
  5988. {
  5989.   int load = bit (insn, 20);
  5990.   int user = bit (insn, 22);
  5991.   int increment = bit (insn, 23);
  5992.   int before = bit (insn, 24);
  5993.   int writeback = bit (insn, 21);
  5994.   int rn = bits (insn, 16, 19);

  5995.   /* Block transfers which don't mention PC can be run directly
  5996.      out-of-line.  */
  5997.   if (rn != ARM_PC_REGNUM && (insn & 0x8000) == 0)
  5998.     return arm_copy_unmodified (gdbarch, insn, "ldm/stm", dsc);

  5999.   if (rn == ARM_PC_REGNUM)
  6000.     {
  6001.       warning (_("displaced: Unpredictable LDM or STM with "
  6002.                  "base register r15"));
  6003.       return arm_copy_unmodified (gdbarch, insn, "unpredictable ldm/stm", dsc);
  6004.     }

  6005.   if (debug_displaced)
  6006.     fprintf_unfiltered (gdb_stdlog, "displaced: copying block transfer insn "
  6007.                         "%.8lx\n", (unsigned long) insn);

  6008.   dsc->u.block.xfer_addr = displaced_read_reg (regs, dsc, rn);
  6009.   dsc->u.block.rn = rn;

  6010.   dsc->u.block.load = load;
  6011.   dsc->u.block.user = user;
  6012.   dsc->u.block.increment = increment;
  6013.   dsc->u.block.before = before;
  6014.   dsc->u.block.writeback = writeback;
  6015.   dsc->u.block.cond = bits (insn, 28, 31);

  6016.   dsc->u.block.regmask = insn & 0xffff;

  6017.   if (load)
  6018.     {
  6019.       if ((insn & 0xffff) == 0xffff)
  6020.         {
  6021.           /* LDM with a fully-populated register list.  This case is
  6022.              particularly tricky.  Implement for now by fully emulating the
  6023.              instruction (which might not behave perfectly in all cases, but
  6024.              these instructions should be rare enough for that not to matter
  6025.              too much).  */
  6026.           dsc->modinsn[0] = ARM_NOP;

  6027.           dsc->cleanup = &cleanup_block_load_all;
  6028.         }
  6029.       else
  6030.         {
  6031.           /* LDM of a list of registers which includes PC.  Implement by
  6032.              rewriting the list of registers to be transferred into a
  6033.              contiguous chunk r0...rX before doing the transfer, then shuffling
  6034.              registers into the correct places in the cleanup routine.  */
  6035.           unsigned int regmask = insn & 0xffff;
  6036.           unsigned int num_in_list = bitcount (regmask), new_regmask, bit = 1;
  6037.           unsigned int to = 0, from = 0, i, new_rn;

  6038.           for (i = 0; i < num_in_list; i++)
  6039.             dsc->tmp[i] = displaced_read_reg (regs, dsc, i);

  6040.           /* Writeback makes things complicated.  We need to avoid clobbering
  6041.              the base register with one of the registers in our modified
  6042.              register list, but just using a different register can't work in
  6043.              all cases, e.g.:

  6044.                ldm r14!, {r0-r13,pc}

  6045.              which would need to be rewritten as:

  6046.                ldm rN!, {r0-r14}

  6047.              but that can't work, because there's no free register for N.

  6048.              Solve this by turning off the writeback bit, and emulating
  6049.              writeback manually in the cleanup routine.  */

  6050.           if (writeback)
  6051.             insn &= ~(1 << 21);

  6052.           new_regmask = (1 << num_in_list) - 1;

  6053.           if (debug_displaced)
  6054.             fprintf_unfiltered (gdb_stdlog, _("displaced: LDM r%d%s, "
  6055.                                 "{..., pc}: original reg list %.4x, modified "
  6056.                                 "list %.4x\n"), rn, writeback ? "!" : "",
  6057.                                 (int) insn & 0xffff, new_regmask);

  6058.           dsc->modinsn[0] = (insn & ~0xffff) | (new_regmask & 0xffff);

  6059.           dsc->cleanup = &cleanup_block_load_pc;
  6060.         }
  6061.     }
  6062.   else
  6063.     {
  6064.       /* STM of a list of registers which includes PC.  Run the instruction
  6065.          as-is, but out of line: this will store the wrong value for the PC,
  6066.          so we must manually fix up the memory in the cleanup routine.
  6067.          Doing things this way has the advantage that we can auto-detect
  6068.          the offset of the PC write (which is architecture-dependent) in
  6069.          the cleanup routine.  */
  6070.       dsc->modinsn[0] = insn;

  6071.       dsc->cleanup = &cleanup_block_store_pc;
  6072.     }

  6073.   return 0;
  6074. }

  6075. static int
  6076. thumb2_copy_block_xfer (struct gdbarch *gdbarch, uint16_t insn1, uint16_t insn2,
  6077.                         struct regcache *regs,
  6078.                         struct displaced_step_closure *dsc)
  6079. {
  6080.   int rn = bits (insn1, 0, 3);
  6081.   int load = bit (insn1, 4);
  6082.   int writeback = bit (insn1, 5);

  6083.   /* Block transfers which don't mention PC can be run directly
  6084.      out-of-line.  */
  6085.   if (rn != ARM_PC_REGNUM && (insn2 & 0x8000) == 0)
  6086.     return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2, "ldm/stm", dsc);

  6087.   if (rn == ARM_PC_REGNUM)
  6088.     {
  6089.       warning (_("displaced: Unpredictable LDM or STM with "
  6090.                  "base register r15"));
  6091.       return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
  6092.                                           "unpredictable ldm/stm", dsc);
  6093.     }

  6094.   if (debug_displaced)
  6095.     fprintf_unfiltered (gdb_stdlog, "displaced: copying block transfer insn "
  6096.                         "%.4x%.4x\n", insn1, insn2);

  6097.   /* Clear bit 13, since it should be always zero.  */
  6098.   dsc->u.block.regmask = (insn2 & 0xdfff);
  6099.   dsc->u.block.rn = rn;

  6100.   dsc->u.block.load = load;
  6101.   dsc->u.block.user = 0;
  6102.   dsc->u.block.increment = bit (insn1, 7);
  6103.   dsc->u.block.before = bit (insn1, 8);
  6104.   dsc->u.block.writeback = writeback;
  6105.   dsc->u.block.cond = INST_AL;
  6106.   dsc->u.block.xfer_addr = displaced_read_reg (regs, dsc, rn);

  6107.   if (load)
  6108.     {
  6109.       if (dsc->u.block.regmask == 0xffff)
  6110.         {
  6111.           /* This branch is impossible to happen.  */
  6112.           gdb_assert (0);
  6113.         }
  6114.       else
  6115.         {
  6116.           unsigned int regmask = dsc->u.block.regmask;
  6117.           unsigned int num_in_list = bitcount (regmask), new_regmask, bit = 1;
  6118.           unsigned int to = 0, from = 0, i, new_rn;

  6119.           for (i = 0; i < num_in_list; i++)
  6120.             dsc->tmp[i] = displaced_read_reg (regs, dsc, i);

  6121.           if (writeback)
  6122.             insn1 &= ~(1 << 5);

  6123.           new_regmask = (1 << num_in_list) - 1;

  6124.           if (debug_displaced)
  6125.             fprintf_unfiltered (gdb_stdlog, _("displaced: LDM r%d%s, "
  6126.                                 "{..., pc}: original reg list %.4x, modified "
  6127.                                 "list %.4x\n"), rn, writeback ? "!" : "",
  6128.                                 (int) dsc->u.block.regmask, new_regmask);

  6129.           dsc->modinsn[0] = insn1;
  6130.           dsc->modinsn[1] = (new_regmask & 0xffff);
  6131.           dsc->numinsns = 2;

  6132.           dsc->cleanup = &cleanup_block_load_pc;
  6133.         }
  6134.     }
  6135.   else
  6136.     {
  6137.       dsc->modinsn[0] = insn1;
  6138.       dsc->modinsn[1] = insn2;
  6139.       dsc->numinsns = 2;
  6140.       dsc->cleanup = &cleanup_block_store_pc;
  6141.     }
  6142.   return 0;
  6143. }

  6144. /* Cleanup/copy SVC (SWI) instructions.  These two functions are overridden
  6145.    for Linux, where some SVC instructions must be treated specially.  */

  6146. static void
  6147. cleanup_svc (struct gdbarch *gdbarch, struct regcache *regs,
  6148.              struct displaced_step_closure *dsc)
  6149. {
  6150.   CORE_ADDR resume_addr = dsc->insn_addr + dsc->insn_size;

  6151.   if (debug_displaced)
  6152.     fprintf_unfiltered (gdb_stdlog, "displaced: cleanup for svc, resume at "
  6153.                         "%.8lx\n", (unsigned long) resume_addr);

  6154.   displaced_write_reg (regs, dsc, ARM_PC_REGNUM, resume_addr, BRANCH_WRITE_PC);
  6155. }


  6156. /* Common copy routine for svc instruciton.  */

  6157. static int
  6158. install_svc (struct gdbarch *gdbarch, struct regcache *regs,
  6159.              struct displaced_step_closure *dsc)
  6160. {
  6161.   /* Preparation: none.
  6162.      Insn: unmodified svc.
  6163.      Cleanup: pc <- insn_addr + insn_size.  */

  6164.   /* Pretend we wrote to the PC, so cleanup doesn't set PC to the next
  6165.      instruction.  */
  6166.   dsc->wrote_to_pc = 1;

  6167.   /* Allow OS-specific code to override SVC handling.  */
  6168.   if (dsc->u.svc.copy_svc_os)
  6169.     return dsc->u.svc.copy_svc_os (gdbarch, regs, dsc);
  6170.   else
  6171.     {
  6172.       dsc->cleanup = &cleanup_svc;
  6173.       return 0;
  6174.     }
  6175. }

  6176. static int
  6177. arm_copy_svc (struct gdbarch *gdbarch, uint32_t insn,
  6178.               struct regcache *regs, struct displaced_step_closure *dsc)
  6179. {

  6180.   if (debug_displaced)
  6181.     fprintf_unfiltered (gdb_stdlog, "displaced: copying svc insn %.8lx\n",
  6182.                         (unsigned long) insn);

  6183.   dsc->modinsn[0] = insn;

  6184.   return install_svc (gdbarch, regs, dsc);
  6185. }

  6186. static int
  6187. thumb_copy_svc (struct gdbarch *gdbarch, uint16_t insn,
  6188.                 struct regcache *regs, struct displaced_step_closure *dsc)
  6189. {

  6190.   if (debug_displaced)
  6191.     fprintf_unfiltered (gdb_stdlog, "displaced: copying svc insn %.4x\n",
  6192.                         insn);

  6193.   dsc->modinsn[0] = insn;

  6194.   return install_svc (gdbarch, regs, dsc);
  6195. }

  6196. /* Copy undefined instructions.  */

  6197. static int
  6198. arm_copy_undef (struct gdbarch *gdbarch, uint32_t insn,
  6199.                 struct displaced_step_closure *dsc)
  6200. {
  6201.   if (debug_displaced)
  6202.     fprintf_unfiltered (gdb_stdlog,
  6203.                         "displaced: copying undefined insn %.8lx\n",
  6204.                         (unsigned long) insn);

  6205.   dsc->modinsn[0] = insn;

  6206.   return 0;
  6207. }

  6208. static int
  6209. thumb_32bit_copy_undef (struct gdbarch *gdbarch, uint16_t insn1, uint16_t insn2,
  6210.                        struct displaced_step_closure *dsc)
  6211. {

  6212.   if (debug_displaced)
  6213.     fprintf_unfiltered (gdb_stdlog, "displaced: copying undefined insn "
  6214.                        "%.4x %.4x\n", (unsigned short) insn1,
  6215.                        (unsigned short) insn2);

  6216.   dsc->modinsn[0] = insn1;
  6217.   dsc->modinsn[1] = insn2;
  6218.   dsc->numinsns = 2;

  6219.   return 0;
  6220. }

  6221. /* Copy unpredictable instructions.  */

  6222. static int
  6223. arm_copy_unpred (struct gdbarch *gdbarch, uint32_t insn,
  6224.                  struct displaced_step_closure *dsc)
  6225. {
  6226.   if (debug_displaced)
  6227.     fprintf_unfiltered (gdb_stdlog, "displaced: copying unpredictable insn "
  6228.                         "%.8lx\n", (unsigned long) insn);

  6229.   dsc->modinsn[0] = insn;

  6230.   return 0;
  6231. }

  6232. /* The decode_* functions are instruction decoding helpers.  They mostly follow
  6233.    the presentation in the ARM ARM.  */

  6234. static int
  6235. arm_decode_misc_memhint_neon (struct gdbarch *gdbarch, uint32_t insn,
  6236.                               struct regcache *regs,
  6237.                               struct displaced_step_closure *dsc)
  6238. {
  6239.   unsigned int op1 = bits (insn, 20, 26), op2 = bits (insn, 4, 7);
  6240.   unsigned int rn = bits (insn, 16, 19);

  6241.   if (op1 == 0x10 && (op2 & 0x2) == 0x0 && (rn & 0xe) == 0x0)
  6242.     return arm_copy_unmodified (gdbarch, insn, "cps", dsc);
  6243.   else if (op1 == 0x10 && op2 == 0x0 && (rn & 0xe) == 0x1)
  6244.     return arm_copy_unmodified (gdbarch, insn, "setend", dsc);
  6245.   else if ((op1 & 0x60) == 0x20)
  6246.     return arm_copy_unmodified (gdbarch, insn, "neon dataproc", dsc);
  6247.   else if ((op1 & 0x71) == 0x40)
  6248.     return arm_copy_unmodified (gdbarch, insn, "neon elt/struct load/store",
  6249.                                 dsc);
  6250.   else if ((op1 & 0x77) == 0x41)
  6251.     return arm_copy_unmodified (gdbarch, insn, "unallocated mem hint", dsc);
  6252.   else if ((op1 & 0x77) == 0x45)
  6253.     return arm_copy_preload (gdbarch, insn, regs, dsc);  /* pli.  */
  6254.   else if ((op1 & 0x77) == 0x51)
  6255.     {
  6256.       if (rn != 0xf)
  6257.         return arm_copy_preload (gdbarch, insn, regs, dsc);  /* pld/pldw.  */
  6258.       else
  6259.         return arm_copy_unpred (gdbarch, insn, dsc);
  6260.     }
  6261.   else if ((op1 & 0x77) == 0x55)
  6262.     return arm_copy_preload (gdbarch, insn, regs, dsc);  /* pld/pldw.  */
  6263.   else if (op1 == 0x57)
  6264.     switch (op2)
  6265.       {
  6266.       case 0x1: return arm_copy_unmodified (gdbarch, insn, "clrex", dsc);
  6267.       case 0x4: return arm_copy_unmodified (gdbarch, insn, "dsb", dsc);
  6268.       case 0x5: return arm_copy_unmodified (gdbarch, insn, "dmb", dsc);
  6269.       case 0x6: return arm_copy_unmodified (gdbarch, insn, "isb", dsc);
  6270.       default: return arm_copy_unpred (gdbarch, insn, dsc);
  6271.       }
  6272.   else if ((op1 & 0x63) == 0x43)
  6273.     return arm_copy_unpred (gdbarch, insn, dsc);
  6274.   else if ((op2 & 0x1) == 0x0)
  6275.     switch (op1 & ~0x80)
  6276.       {
  6277.       case 0x61:
  6278.         return arm_copy_unmodified (gdbarch, insn, "unallocated mem hint", dsc);
  6279.       case 0x65:
  6280.         return arm_copy_preload_reg (gdbarch, insn, regs, dsc);  /* pli reg.  */
  6281.       case 0x71: case 0x75:
  6282.         /* pld/pldw reg.  */
  6283.         return arm_copy_preload_reg (gdbarch, insn, regs, dsc);
  6284.       case 0x63: case 0x67: case 0x73: case 0x77:
  6285.         return arm_copy_unpred (gdbarch, insn, dsc);
  6286.       default:
  6287.         return arm_copy_undef (gdbarch, insn, dsc);
  6288.       }
  6289.   else
  6290.     return arm_copy_undef (gdbarch, insn, dsc);  /* Probably unreachable.  */
  6291. }

  6292. static int
  6293. arm_decode_unconditional (struct gdbarch *gdbarch, uint32_t insn,
  6294.                           struct regcache *regs,
  6295.                           struct displaced_step_closure *dsc)
  6296. {
  6297.   if (bit (insn, 27) == 0)
  6298.     return arm_decode_misc_memhint_neon (gdbarch, insn, regs, dsc);
  6299.   /* Switch on bits: 0bxxxxx321xxx0xxxxxxxxxxxxxxxxxxxx.  */
  6300.   else switch (((insn & 0x7000000) >> 23) | ((insn & 0x100000) >> 20))
  6301.     {
  6302.     case 0x0: case 0x2:
  6303.       return arm_copy_unmodified (gdbarch, insn, "srs", dsc);

  6304.     case 0x1: case 0x3:
  6305.       return arm_copy_unmodified (gdbarch, insn, "rfe", dsc);

  6306.     case 0x4: case 0x5: case 0x6: case 0x7:
  6307.       return arm_copy_b_bl_blx (gdbarch, insn, regs, dsc);

  6308.     case 0x8:
  6309.       switch ((insn & 0xe00000) >> 21)
  6310.         {
  6311.         case 0x1: case 0x3: case 0x4: case 0x5: case 0x6: case 0x7:
  6312.           /* stc/stc2.  */
  6313.           return arm_copy_copro_load_store (gdbarch, insn, regs, dsc);

  6314.         case 0x2:
  6315.           return arm_copy_unmodified (gdbarch, insn, "mcrr/mcrr2", dsc);

  6316.         default:
  6317.           return arm_copy_undef (gdbarch, insn, dsc);
  6318.         }

  6319.     case 0x9:
  6320.       {
  6321.          int rn_f = (bits (insn, 16, 19) == 0xf);
  6322.         switch ((insn & 0xe00000) >> 21)
  6323.           {
  6324.           case 0x1: case 0x3:
  6325.             /* ldc/ldc2 imm (undefined for rn == pc).  */
  6326.             return rn_f ? arm_copy_undef (gdbarch, insn, dsc)
  6327.                         : arm_copy_copro_load_store (gdbarch, insn, regs, dsc);

  6328.           case 0x2:
  6329.             return arm_copy_unmodified (gdbarch, insn, "mrrc/mrrc2", dsc);

  6330.           case 0x4: case 0x5: case 0x6: case 0x7:
  6331.             /* ldc/ldc2 lit (undefined for rn != pc).  */
  6332.             return rn_f ? arm_copy_copro_load_store (gdbarch, insn, regs, dsc)
  6333.                         : arm_copy_undef (gdbarch, insn, dsc);

  6334.           default:
  6335.             return arm_copy_undef (gdbarch, insn, dsc);
  6336.           }
  6337.       }

  6338.     case 0xa:
  6339.       return arm_copy_unmodified (gdbarch, insn, "stc/stc2", dsc);

  6340.     case 0xb:
  6341.       if (bits (insn, 16, 19) == 0xf)
  6342.         /* ldc/ldc2 lit.  */
  6343.         return arm_copy_copro_load_store (gdbarch, insn, regs, dsc);
  6344.       else
  6345.         return arm_copy_undef (gdbarch, insn, dsc);

  6346.     case 0xc:
  6347.       if (bit (insn, 4))
  6348.         return arm_copy_unmodified (gdbarch, insn, "mcr/mcr2", dsc);
  6349.       else
  6350.         return arm_copy_unmodified (gdbarch, insn, "cdp/cdp2", dsc);

  6351.     case 0xd:
  6352.       if (bit (insn, 4))
  6353.         return arm_copy_unmodified (gdbarch, insn, "mrc/mrc2", dsc);
  6354.       else
  6355.         return arm_copy_unmodified (gdbarch, insn, "cdp/cdp2", dsc);

  6356.     default:
  6357.       return arm_copy_undef (gdbarch, insn, dsc);
  6358.     }
  6359. }

  6360. /* Decode miscellaneous instructions in dp/misc encoding space.  */

  6361. static int
  6362. arm_decode_miscellaneous (struct gdbarch *gdbarch, uint32_t insn,
  6363.                           struct regcache *regs,
  6364.                           struct displaced_step_closure *dsc)
  6365. {
  6366.   unsigned int op2 = bits (insn, 4, 6);
  6367.   unsigned int op = bits (insn, 21, 22);
  6368.   unsigned int op1 = bits (insn, 16, 19);

  6369.   switch (op2)
  6370.     {
  6371.     case 0x0:
  6372.       return arm_copy_unmodified (gdbarch, insn, "mrs/msr", dsc);

  6373.     case 0x1:
  6374.       if (op == 0x1/* bx.  */
  6375.         return arm_copy_bx_blx_reg (gdbarch, insn, regs, dsc);
  6376.       else if (op == 0x3)
  6377.         return arm_copy_unmodified (gdbarch, insn, "clz", dsc);
  6378.       else
  6379.         return arm_copy_undef (gdbarch, insn, dsc);

  6380.     case 0x2:
  6381.       if (op == 0x1)
  6382.         /* Not really supported.  */
  6383.         return arm_copy_unmodified (gdbarch, insn, "bxj", dsc);
  6384.       else
  6385.         return arm_copy_undef (gdbarch, insn, dsc);

  6386.     case 0x3:
  6387.       if (op == 0x1)
  6388.         return arm_copy_bx_blx_reg (gdbarch, insn,
  6389.                                 regs, dsc);  /* blx register.  */
  6390.       else
  6391.         return arm_copy_undef (gdbarch, insn, dsc);

  6392.     case 0x5:
  6393.       return arm_copy_unmodified (gdbarch, insn, "saturating add/sub", dsc);

  6394.     case 0x7:
  6395.       if (op == 0x1)
  6396.         return arm_copy_unmodified (gdbarch, insn, "bkpt", dsc);
  6397.       else if (op == 0x3)
  6398.         /* Not really supported.  */
  6399.         return arm_copy_unmodified (gdbarch, insn, "smc", dsc);

  6400.     default:
  6401.       return arm_copy_undef (gdbarch, insn, dsc);
  6402.     }
  6403. }

  6404. static int
  6405. arm_decode_dp_misc (struct gdbarch *gdbarch, uint32_t insn,
  6406.                     struct regcache *regs,
  6407.                     struct displaced_step_closure *dsc)
  6408. {
  6409.   if (bit (insn, 25))
  6410.     switch (bits (insn, 20, 24))
  6411.       {
  6412.       case 0x10:
  6413.         return arm_copy_unmodified (gdbarch, insn, "movw", dsc);

  6414.       case 0x14:
  6415.         return arm_copy_unmodified (gdbarch, insn, "movt", dsc);

  6416.       case 0x12: case 0x16:
  6417.         return arm_copy_unmodified (gdbarch, insn, "msr imm", dsc);

  6418.       default:
  6419.         return arm_copy_alu_imm (gdbarch, insn, regs, dsc);
  6420.       }
  6421.   else
  6422.     {
  6423.       uint32_t op1 = bits (insn, 20, 24), op2 = bits (insn, 4, 7);

  6424.       if ((op1 & 0x19) != 0x10 && (op2 & 0x1) == 0x0)
  6425.         return arm_copy_alu_reg (gdbarch, insn, regs, dsc);
  6426.       else if ((op1 & 0x19) != 0x10 && (op2 & 0x9) == 0x1)
  6427.         return arm_copy_alu_shifted_reg (gdbarch, insn, regs, dsc);
  6428.       else if ((op1 & 0x19) == 0x10 && (op2 & 0x8) == 0x0)
  6429.         return arm_decode_miscellaneous (gdbarch, insn, regs, dsc);
  6430.       else if ((op1 & 0x19) == 0x10 && (op2 & 0x9) == 0x8)
  6431.         return arm_copy_unmodified (gdbarch, insn, "halfword mul/mla", dsc);
  6432.       else if ((op1 & 0x10) == 0x00 && op2 == 0x9)
  6433.         return arm_copy_unmodified (gdbarch, insn, "mul/mla", dsc);
  6434.       else if ((op1 & 0x10) == 0x10 && op2 == 0x9)
  6435.         return arm_copy_unmodified (gdbarch, insn, "synch", dsc);
  6436.       else if (op2 == 0xb || (op2 & 0xd) == 0xd)
  6437.         /* 2nd arg means "unpriveleged".  */
  6438.         return arm_copy_extra_ld_st (gdbarch, insn, (op1 & 0x12) == 0x02, regs,
  6439.                                      dsc);
  6440.     }

  6441.   /* Should be unreachable.  */
  6442.   return 1;
  6443. }

  6444. static int
  6445. arm_decode_ld_st_word_ubyte (struct gdbarch *gdbarch, uint32_t insn,
  6446.                              struct regcache *regs,
  6447.                              struct displaced_step_closure *dsc)
  6448. {
  6449.   int a = bit (insn, 25), b = bit (insn, 4);
  6450.   uint32_t op1 = bits (insn, 20, 24);
  6451.   int rn_f = bits (insn, 16, 19) == 0xf;

  6452.   if ((!a && (op1 & 0x05) == 0x00 && (op1 & 0x17) != 0x02)
  6453.       || (a && (op1 & 0x05) == 0x00 && (op1 & 0x17) != 0x02 && !b))
  6454.     return arm_copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 0, 4, 0);
  6455.   else if ((!a && (op1 & 0x17) == 0x02)
  6456.             || (a && (op1 & 0x17) == 0x02 && !b))
  6457.     return arm_copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 0, 4, 1);
  6458.   else if ((!a && (op1 & 0x05) == 0x01 && (op1 & 0x17) != 0x03)
  6459.             || (a && (op1 & 0x05) == 0x01 && (op1 & 0x17) != 0x03 && !b))
  6460.     return arm_copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 1, 4, 0);
  6461.   else if ((!a && (op1 & 0x17) == 0x03)
  6462.            || (a && (op1 & 0x17) == 0x03 && !b))
  6463.     return arm_copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 1, 4, 1);
  6464.   else if ((!a && (op1 & 0x05) == 0x04 && (op1 & 0x17) != 0x06)
  6465.             || (a && (op1 & 0x05) == 0x04 && (op1 & 0x17) != 0x06 && !b))
  6466.     return arm_copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 0, 1, 0);
  6467.   else if ((!a && (op1 & 0x17) == 0x06)
  6468.            || (a && (op1 & 0x17) == 0x06 && !b))
  6469.     return arm_copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 0, 1, 1);
  6470.   else if ((!a && (op1 & 0x05) == 0x05 && (op1 & 0x17) != 0x07)
  6471.            || (a && (op1 & 0x05) == 0x05 && (op1 & 0x17) != 0x07 && !b))
  6472.     return arm_copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 1, 1, 0);
  6473.   else if ((!a && (op1 & 0x17) == 0x07)
  6474.            || (a && (op1 & 0x17) == 0x07 && !b))
  6475.     return arm_copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 1, 1, 1);

  6476.   /* Should be unreachable.  */
  6477.   return 1;
  6478. }

  6479. static int
  6480. arm_decode_media (struct gdbarch *gdbarch, uint32_t insn,
  6481.                   struct displaced_step_closure *dsc)
  6482. {
  6483.   switch (bits (insn, 20, 24))
  6484.     {
  6485.     case 0x00: case 0x01: case 0x02: case 0x03:
  6486.       return arm_copy_unmodified (gdbarch, insn, "parallel add/sub signed", dsc);

  6487.     case 0x04: case 0x05: case 0x06: case 0x07:
  6488.       return arm_copy_unmodified (gdbarch, insn, "parallel add/sub unsigned", dsc);

  6489.     case 0x08: case 0x09: case 0x0a: case 0x0b:
  6490.     case 0x0c: case 0x0d: case 0x0e: case 0x0f:
  6491.       return arm_copy_unmodified (gdbarch, insn,
  6492.                               "decode/pack/unpack/saturate/reverse", dsc);

  6493.     case 0x18:
  6494.       if (bits (insn, 5, 7) == 0/* op2.  */
  6495.          {
  6496.           if (bits (insn, 12, 15) == 0xf)
  6497.             return arm_copy_unmodified (gdbarch, insn, "usad8", dsc);
  6498.           else
  6499.             return arm_copy_unmodified (gdbarch, insn, "usada8", dsc);
  6500.         }
  6501.       else
  6502.          return arm_copy_undef (gdbarch, insn, dsc);

  6503.     case 0x1a: case 0x1b:
  6504.       if (bits (insn, 5, 6) == 0x2/* op2[1:0].  */
  6505.         return arm_copy_unmodified (gdbarch, insn, "sbfx", dsc);
  6506.       else
  6507.         return arm_copy_undef (gdbarch, insn, dsc);

  6508.     case 0x1c: case 0x1d:
  6509.       if (bits (insn, 5, 6) == 0x0/* op2[1:0].  */
  6510.          {
  6511.           if (bits (insn, 0, 3) == 0xf)
  6512.             return arm_copy_unmodified (gdbarch, insn, "bfc", dsc);
  6513.           else
  6514.             return arm_copy_unmodified (gdbarch, insn, "bfi", dsc);
  6515.         }
  6516.       else
  6517.         return arm_copy_undef (gdbarch, insn, dsc);

  6518.     case 0x1e: case 0x1f:
  6519.       if (bits (insn, 5, 6) == 0x2/* op2[1:0].  */
  6520.         return arm_copy_unmodified (gdbarch, insn, "ubfx", dsc);
  6521.       else
  6522.         return arm_copy_undef (gdbarch, insn, dsc);
  6523.     }

  6524.   /* Should be unreachable.  */
  6525.   return 1;
  6526. }

  6527. static int
  6528. arm_decode_b_bl_ldmstm (struct gdbarch *gdbarch, int32_t insn,
  6529.                         struct regcache *regs,
  6530.                         struct displaced_step_closure *dsc)
  6531. {
  6532.   if (bit (insn, 25))
  6533.     return arm_copy_b_bl_blx (gdbarch, insn, regs, dsc);
  6534.   else
  6535.     return arm_copy_block_xfer (gdbarch, insn, regs, dsc);
  6536. }

  6537. static int
  6538. arm_decode_ext_reg_ld_st (struct gdbarch *gdbarch, uint32_t insn,
  6539.                           struct regcache *regs,
  6540.                           struct displaced_step_closure *dsc)
  6541. {
  6542.   unsigned int opcode = bits (insn, 20, 24);

  6543.   switch (opcode)
  6544.     {
  6545.     case 0x04: case 0x05/* VFP/Neon mrrc/mcrr.  */
  6546.       return arm_copy_unmodified (gdbarch, insn, "vfp/neon mrrc/mcrr", dsc);

  6547.     case 0x08: case 0x0a: case 0x0c: case 0x0e:
  6548.     case 0x12: case 0x16:
  6549.       return arm_copy_unmodified (gdbarch, insn, "vfp/neon vstm/vpush", dsc);

  6550.     case 0x09: case 0x0b: case 0x0d: case 0x0f:
  6551.     case 0x13: case 0x17:
  6552.       return arm_copy_unmodified (gdbarch, insn, "vfp/neon vldm/vpop", dsc);

  6553.     case 0x10: case 0x14: case 0x18: case 0x1c/* vstr.  */
  6554.     case 0x11: case 0x15: case 0x19: case 0x1d/* vldr.  */
  6555.       /* Note: no writeback for these instructions.  Bit 25 will always be
  6556.          zero though (via caller), so the following works OK.  */
  6557.       return arm_copy_copro_load_store (gdbarch, insn, regs, dsc);
  6558.     }

  6559.   /* Should be unreachable.  */
  6560.   return 1;
  6561. }

  6562. /* Decode shifted register instructions.  */

  6563. static int
  6564. thumb2_decode_dp_shift_reg (struct gdbarch *gdbarch, uint16_t insn1,
  6565.                             uint16_t insn2,  struct regcache *regs,
  6566.                             struct displaced_step_closure *dsc)
  6567. {
  6568.   /* PC is only allowed to be used in instruction MOV.  */

  6569.   unsigned int op = bits (insn1, 5, 8);
  6570.   unsigned int rn = bits (insn1, 0, 3);

  6571.   if (op == 0x2 && rn == 0xf) /* MOV */
  6572.     return thumb2_copy_alu_imm (gdbarch, insn1, insn2, regs, dsc);
  6573.   else
  6574.     return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
  6575.                                         "dp (shift reg)", dsc);
  6576. }


  6577. /* Decode extension register load/store.  Exactly the same as
  6578.    arm_decode_ext_reg_ld_st.  */

  6579. static int
  6580. thumb2_decode_ext_reg_ld_st (struct gdbarch *gdbarch, uint16_t insn1,
  6581.                              uint16_t insn2,  struct regcache *regs,
  6582.                              struct displaced_step_closure *dsc)
  6583. {
  6584.   unsigned int opcode = bits (insn1, 4, 8);

  6585.   switch (opcode)
  6586.     {
  6587.     case 0x04: case 0x05:
  6588.       return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
  6589.                                           "vfp/neon vmov", dsc);

  6590.     case 0x08: case 0x0c: /* 01x00 */
  6591.     case 0x0a: case 0x0e: /* 01x10 */
  6592.     case 0x12: case 0x16: /* 10x10 */
  6593.       return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
  6594.                                           "vfp/neon vstm/vpush", dsc);

  6595.     case 0x09: case 0x0d: /* 01x01 */
  6596.     case 0x0b: case 0x0f: /* 01x11 */
  6597.     case 0x13: case 0x17: /* 10x11 */
  6598.       return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
  6599.                                           "vfp/neon vldm/vpop", dsc);

  6600.     case 0x10: case 0x14: case 0x18: case 0x1c/* vstr.  */
  6601.       return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
  6602.                                           "vstr", dsc);
  6603.     case 0x11: case 0x15: case 0x19: case 0x1d/* vldr.  */
  6604.       return thumb2_copy_copro_load_store (gdbarch, insn1, insn2, regs, dsc);
  6605.     }

  6606.   /* Should be unreachable.  */
  6607.   return 1;
  6608. }

  6609. static int
  6610. arm_decode_svc_copro (struct gdbarch *gdbarch, uint32_t insn, CORE_ADDR to,
  6611.                       struct regcache *regs, struct displaced_step_closure *dsc)
  6612. {
  6613.   unsigned int op1 = bits (insn, 20, 25);
  6614.   int op = bit (insn, 4);
  6615.   unsigned int coproc = bits (insn, 8, 11);
  6616.   unsigned int rn = bits (insn, 16, 19);

  6617.   if ((op1 & 0x20) == 0x00 && (op1 & 0x3a) != 0x00 && (coproc & 0xe) == 0xa)
  6618.     return arm_decode_ext_reg_ld_st (gdbarch, insn, regs, dsc);
  6619.   else if ((op1 & 0x21) == 0x00 && (op1 & 0x3a) != 0x00
  6620.            && (coproc & 0xe) != 0xa)
  6621.     /* stc/stc2.  */
  6622.     return arm_copy_copro_load_store (gdbarch, insn, regs, dsc);
  6623.   else if ((op1 & 0x21) == 0x01 && (op1 & 0x3a) != 0x00
  6624.            && (coproc & 0xe) != 0xa)
  6625.     /* ldc/ldc2 imm/lit.  */
  6626.     return arm_copy_copro_load_store (gdbarch, insn, regs, dsc);
  6627.   else if ((op1 & 0x3e) == 0x00)
  6628.     return arm_copy_undef (gdbarch, insn, dsc);
  6629.   else if ((op1 & 0x3e) == 0x04 && (coproc & 0xe) == 0xa)
  6630.     return arm_copy_unmodified (gdbarch, insn, "neon 64bit xfer", dsc);
  6631.   else if (op1 == 0x04 && (coproc & 0xe) != 0xa)
  6632.     return arm_copy_unmodified (gdbarch, insn, "mcrr/mcrr2", dsc);
  6633.   else if (op1 == 0x05 && (coproc & 0xe) != 0xa)
  6634.     return arm_copy_unmodified (gdbarch, insn, "mrrc/mrrc2", dsc);
  6635.   else if ((op1 & 0x30) == 0x20 && !op)
  6636.     {
  6637.       if ((coproc & 0xe) == 0xa)
  6638.         return arm_copy_unmodified (gdbarch, insn, "vfp dataproc", dsc);
  6639.       else
  6640.         return arm_copy_unmodified (gdbarch, insn, "cdp/cdp2", dsc);
  6641.     }
  6642.   else if ((op1 & 0x30) == 0x20 && op)
  6643.     return arm_copy_unmodified (gdbarch, insn, "neon 8/16/32 bit xfer", dsc);
  6644.   else if ((op1 & 0x31) == 0x20 && op && (coproc & 0xe) != 0xa)
  6645.     return arm_copy_unmodified (gdbarch, insn, "mcr/mcr2", dsc);
  6646.   else if ((op1 & 0x31) == 0x21 && op && (coproc & 0xe) != 0xa)
  6647.     return arm_copy_unmodified (gdbarch, insn, "mrc/mrc2", dsc);
  6648.   else if ((op1 & 0x30) == 0x30)
  6649.     return arm_copy_svc (gdbarch, insn, regs, dsc);
  6650.   else
  6651.     return arm_copy_undef (gdbarch, insn, dsc);  /* Possibly unreachable.  */
  6652. }

  6653. static int
  6654. thumb2_decode_svc_copro (struct gdbarch *gdbarch, uint16_t insn1,
  6655.                          uint16_t insn2, struct regcache *regs,
  6656.                          struct displaced_step_closure *dsc)
  6657. {
  6658.   unsigned int coproc = bits (insn2, 8, 11);
  6659.   unsigned int op1 = bits (insn1, 4, 9);
  6660.   unsigned int bit_5_8 = bits (insn1, 5, 8);
  6661.   unsigned int bit_9 = bit (insn1, 9);
  6662.   unsigned int bit_4 = bit (insn1, 4);
  6663.   unsigned int rn = bits (insn1, 0, 3);

  6664.   if (bit_9 == 0)
  6665.     {
  6666.       if (bit_5_8 == 2)
  6667.         return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
  6668.                                             "neon 64bit xfer/mrrc/mrrc2/mcrr/mcrr2",
  6669.                                             dsc);
  6670.       else if (bit_5_8 == 0) /* UNDEFINED.  */
  6671.         return thumb_32bit_copy_undef (gdbarch, insn1, insn2, dsc);
  6672.       else
  6673.         {
  6674.            /*coproc is 101x.  SIMD/VFP, ext registers load/store.  */
  6675.           if ((coproc & 0xe) == 0xa)
  6676.             return thumb2_decode_ext_reg_ld_st (gdbarch, insn1, insn2, regs,
  6677.                                                 dsc);
  6678.           else /* coproc is not 101x.  */
  6679.             {
  6680.               if (bit_4 == 0) /* STC/STC2.  */
  6681.                 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
  6682.                                                     "stc/stc2", dsc);
  6683.               else /* LDC/LDC2 {literal, immeidate}.  */
  6684.                 return thumb2_copy_copro_load_store (gdbarch, insn1, insn2,
  6685.                                                      regs, dsc);
  6686.             }
  6687.         }
  6688.     }
  6689.   else
  6690.     return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2, "coproc", dsc);

  6691.   return 0;
  6692. }

  6693. static void
  6694. install_pc_relative (struct gdbarch *gdbarch, struct regcache *regs,
  6695.                      struct displaced_step_closure *dsc, int rd)
  6696. {
  6697.   /* ADR Rd, #imm

  6698.      Rewrite as:

  6699.      Preparation: Rd <- PC
  6700.      Insn: ADD Rd, #imm
  6701.      Cleanup: Null.
  6702.   */

  6703.   /* Rd <- PC */
  6704.   int val = displaced_read_reg (regs, dsc, ARM_PC_REGNUM);
  6705.   displaced_write_reg (regs, dsc, rd, val, CANNOT_WRITE_PC);
  6706. }

  6707. static int
  6708. thumb_copy_pc_relative_16bit (struct gdbarch *gdbarch, struct regcache *regs,
  6709.                               struct displaced_step_closure *dsc,
  6710.                               int rd, unsigned int imm)
  6711. {

  6712.   /* Encoding T2: ADDS Rd, #imm */
  6713.   dsc->modinsn[0] = (0x3000 | (rd << 8) | imm);

  6714.   install_pc_relative (gdbarch, regs, dsc, rd);

  6715.   return 0;
  6716. }

  6717. static int
  6718. thumb_decode_pc_relative_16bit (struct gdbarch *gdbarch, uint16_t insn,
  6719.                                 struct regcache *regs,
  6720.                                 struct displaced_step_closure *dsc)
  6721. {
  6722.   unsigned int rd = bits (insn, 8, 10);
  6723.   unsigned int imm8 = bits (insn, 0, 7);

  6724.   if (debug_displaced)
  6725.     fprintf_unfiltered (gdb_stdlog,
  6726.                         "displaced: copying thumb adr r%d, #%d insn %.4x\n",
  6727.                         rd, imm8, insn);

  6728.   return thumb_copy_pc_relative_16bit (gdbarch, regs, dsc, rd, imm8);
  6729. }

  6730. static int
  6731. thumb_copy_pc_relative_32bit (struct gdbarch *gdbarch, uint16_t insn1,
  6732.                               uint16_t insn2, struct regcache *regs,
  6733.                               struct displaced_step_closure *dsc)
  6734. {
  6735.   unsigned int rd = bits (insn2, 8, 11);
  6736.   /* Since immediate has the same encoding in ADR ADD and SUB, so we simply
  6737.      extract raw immediate encoding rather than computing immediate.  When
  6738.      generating ADD or SUB instruction, we can simply perform OR operation to
  6739.      set immediate into ADD.  */
  6740.   unsigned int imm_3_8 = insn2 & 0x70ff;
  6741.   unsigned int imm_i = insn1 & 0x0400; /* Clear all bits except bit 10.  */

  6742.   if (debug_displaced)
  6743.     fprintf_unfiltered (gdb_stdlog,
  6744.                         "displaced: copying thumb adr r%d, #%d:%d insn %.4x%.4x\n",
  6745.                         rd, imm_i, imm_3_8, insn1, insn2);

  6746.   if (bit (insn1, 7)) /* Encoding T2 */
  6747.     {
  6748.       /* Encoding T3: SUB Rd, Rd, #imm */
  6749.       dsc->modinsn[0] = (0xf1a0 | rd | imm_i);
  6750.       dsc->modinsn[1] = ((rd << 8) | imm_3_8);
  6751.     }
  6752.   else /* Encoding T3 */
  6753.     {
  6754.       /* Encoding T3: ADD Rd, Rd, #imm */
  6755.       dsc->modinsn[0] = (0xf100 | rd | imm_i);
  6756.       dsc->modinsn[1] = ((rd << 8) | imm_3_8);
  6757.     }
  6758.   dsc->numinsns = 2;

  6759.   install_pc_relative (gdbarch, regs, dsc, rd);

  6760.   return 0;
  6761. }

  6762. static int
  6763. thumb_copy_16bit_ldr_literal (struct gdbarch *gdbarch, unsigned short insn1,
  6764.                               struct regcache *regs,
  6765.                               struct displaced_step_closure *dsc)
  6766. {
  6767.   unsigned int rt = bits (insn1, 8, 10);
  6768.   unsigned int pc;
  6769.   int imm8 = (bits (insn1, 0, 7) << 2);
  6770.   CORE_ADDR from = dsc->insn_addr;

  6771.   /* LDR Rd, #imm8

  6772.      Rwrite as:

  6773.      Preparation: tmp0 <- R0, tmp2 <- R2, tmp3 <- R3, R2 <- PC, R3 <- #imm8;

  6774.      Insn: LDR R0, [R2, R3];
  6775.      Cleanup: R2 <- tmp2, R3 <- tmp3, Rd <- R0, R0 <- tmp0 */

  6776.   if (debug_displaced)
  6777.     fprintf_unfiltered (gdb_stdlog,
  6778.                         "displaced: copying thumb ldr r%d [pc #%d]\n"
  6779.                         , rt, imm8);

  6780.   dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
  6781.   dsc->tmp[2] = displaced_read_reg (regs, dsc, 2);
  6782.   dsc->tmp[3] = displaced_read_reg (regs, dsc, 3);
  6783.   pc = displaced_read_reg (regs, dsc, ARM_PC_REGNUM);
  6784.   /* The assembler calculates the required value of the offset from the
  6785.      Align(PC,4) value of this instruction to the label.  */
  6786.   pc = pc & 0xfffffffc;

  6787.   displaced_write_reg (regs, dsc, 2, pc, CANNOT_WRITE_PC);
  6788.   displaced_write_reg (regs, dsc, 3, imm8, CANNOT_WRITE_PC);

  6789.   dsc->rd = rt;
  6790.   dsc->u.ldst.xfersize = 4;
  6791.   dsc->u.ldst.rn = 0;
  6792.   dsc->u.ldst.immed = 0;
  6793.   dsc->u.ldst.writeback = 0;
  6794.   dsc->u.ldst.restore_r4 = 0;

  6795.   dsc->modinsn[0] = 0x58d0; /* ldr r0, [r2, r3]*/

  6796.   dsc->cleanup = &cleanup_load;

  6797.   return 0;
  6798. }

  6799. /* Copy Thumb cbnz/cbz insruction.  */

  6800. static int
  6801. thumb_copy_cbnz_cbz (struct gdbarch *gdbarch, uint16_t insn1,
  6802.                      struct regcache *regs,
  6803.                      struct displaced_step_closure *dsc)
  6804. {
  6805.   int non_zero = bit (insn1, 11);
  6806.   unsigned int imm5 = (bit (insn1, 9) << 6) | (bits (insn1, 3, 7) << 1);
  6807.   CORE_ADDR from = dsc->insn_addr;
  6808.   int rn = bits (insn1, 0, 2);
  6809.   int rn_val = displaced_read_reg (regs, dsc, rn);

  6810.   dsc->u.branch.cond = (rn_val && non_zero) || (!rn_val && !non_zero);
  6811.   /* CBNZ and CBZ do not affect the condition flags.  If condition is true,
  6812.      set it INST_AL, so cleanup_branch will know branch is taken, otherwise,
  6813.      condition is false, let it be, cleanup_branch will do nothing.  */
  6814.   if (dsc->u.branch.cond)
  6815.     {
  6816.       dsc->u.branch.cond = INST_AL;
  6817.       dsc->u.branch.dest = from + 4 + imm5;
  6818.     }
  6819.   else
  6820.       dsc->u.branch.dest = from + 2;

  6821.   dsc->u.branch.link = 0;
  6822.   dsc->u.branch.exchange = 0;

  6823.   if (debug_displaced)
  6824.     fprintf_unfiltered (gdb_stdlog, "displaced: copying %s [r%d = 0x%x]"
  6825.                         " insn %.4x to %.8lx\n", non_zero ? "cbnz" : "cbz",
  6826.                         rn, rn_val, insn1, dsc->u.branch.dest);

  6827.   dsc->modinsn[0] = THUMB_NOP;

  6828.   dsc->cleanup = &cleanup_branch;
  6829.   return 0;
  6830. }

  6831. /* Copy Table Branch Byte/Halfword */
  6832. static int
  6833. thumb2_copy_table_branch (struct gdbarch *gdbarch, uint16_t insn1,
  6834.                           uint16_t insn2, struct regcache *regs,
  6835.                           struct displaced_step_closure *dsc)
  6836. {
  6837.   ULONGEST rn_val, rm_val;
  6838.   int is_tbh = bit (insn2, 4);
  6839.   CORE_ADDR halfwords = 0;
  6840.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);

  6841.   rn_val = displaced_read_reg (regs, dsc, bits (insn1, 0, 3));
  6842.   rm_val = displaced_read_reg (regs, dsc, bits (insn2, 0, 3));

  6843.   if (is_tbh)
  6844.     {
  6845.       gdb_byte buf[2];

  6846.       target_read_memory (rn_val + 2 * rm_val, buf, 2);
  6847.       halfwords = extract_unsigned_integer (buf, 2, byte_order);
  6848.     }
  6849.   else
  6850.     {
  6851.       gdb_byte buf[1];

  6852.       target_read_memory (rn_val + rm_val, buf, 1);
  6853.       halfwords = extract_unsigned_integer (buf, 1, byte_order);
  6854.     }

  6855.   if (debug_displaced)
  6856.     fprintf_unfiltered (gdb_stdlog, "displaced: %s base 0x%x offset 0x%x"
  6857.                         " offset 0x%x\n", is_tbh ? "tbh" : "tbb",
  6858.                         (unsigned int) rn_val, (unsigned int) rm_val,
  6859.                         (unsigned int) halfwords);

  6860.   dsc->u.branch.cond = INST_AL;
  6861.   dsc->u.branch.link = 0;
  6862.   dsc->u.branch.exchange = 0;
  6863.   dsc->u.branch.dest = dsc->insn_addr + 4 + 2 * halfwords;

  6864.   dsc->cleanup = &cleanup_branch;

  6865.   return 0;
  6866. }

  6867. static void
  6868. cleanup_pop_pc_16bit_all (struct gdbarch *gdbarch, struct regcache *regs,
  6869.                           struct displaced_step_closure *dsc)
  6870. {
  6871.   /* PC <- r7 */
  6872.   int val = displaced_read_reg (regs, dsc, 7);
  6873.   displaced_write_reg (regs, dsc, ARM_PC_REGNUM, val, BX_WRITE_PC);

  6874.   /* r7 <- r8 */
  6875.   val = displaced_read_reg (regs, dsc, 8);
  6876.   displaced_write_reg (regs, dsc, 7, val, CANNOT_WRITE_PC);

  6877.   /* r8 <- tmp[0] */
  6878.   displaced_write_reg (regs, dsc, 8, dsc->tmp[0], CANNOT_WRITE_PC);

  6879. }

  6880. static int
  6881. thumb_copy_pop_pc_16bit (struct gdbarch *gdbarch, unsigned short insn1,
  6882.                          struct regcache *regs,
  6883.                          struct displaced_step_closure *dsc)
  6884. {
  6885.   dsc->u.block.regmask = insn1 & 0x00ff;

  6886.   /* Rewrite instruction: POP {rX, rY, ...,rZ, PC}
  6887.      to :

  6888.      (1) register list is full, that is, r0-r7 are used.
  6889.      Prepare: tmp[0] <- r8

  6890.      POP {r0, r1, ...., r6, r7}; remove PC from reglist
  6891.      MOV r8, r7; Move value of r7 to r8;
  6892.      POP {r7}; Store PC value into r7.

  6893.      Cleanup: PC <- r7, r7 <- r8, r8 <-tmp[0]

  6894.      (2) register list is not full, supposing there are N registers in
  6895.      register list (except PC, 0 <= N <= 7).
  6896.      Prepare: for each i, 0 - N, tmp[i] <- ri.

  6897.      POP {r0, r1, ...., rN};

  6898.      Cleanup: Set registers in original reglist from r0 - rN.  Restore r0 - rN
  6899.      from tmp[] properly.
  6900.   */
  6901.   if (debug_displaced)
  6902.     fprintf_unfiltered (gdb_stdlog,
  6903.                         "displaced: copying thumb pop {%.8x, pc} insn %.4x\n",
  6904.                         dsc->u.block.regmask, insn1);

  6905.   if (dsc->u.block.regmask == 0xff)
  6906.     {
  6907.       dsc->tmp[0] = displaced_read_reg (regs, dsc, 8);

  6908.       dsc->modinsn[0] = (insn1 & 0xfeff); /* POP {r0,r1,...,r6, r7} */
  6909.       dsc->modinsn[1] = 0x46b8; /* MOV r8, r7 */
  6910.       dsc->modinsn[2] = 0xbc80; /* POP {r7} */

  6911.       dsc->numinsns = 3;
  6912.       dsc->cleanup = &cleanup_pop_pc_16bit_all;
  6913.     }
  6914.   else
  6915.     {
  6916.       unsigned int num_in_list = bitcount (dsc->u.block.regmask);
  6917.       unsigned int new_regmask, bit = 1;
  6918.       unsigned int to = 0, from = 0, i, new_rn;

  6919.       for (i = 0; i < num_in_list + 1; i++)
  6920.         dsc->tmp[i] = displaced_read_reg (regs, dsc, i);

  6921.       new_regmask = (1 << (num_in_list + 1)) - 1;

  6922.       if (debug_displaced)
  6923.         fprintf_unfiltered (gdb_stdlog, _("displaced: POP "
  6924.                                           "{..., pc}: original reg list %.4x,"
  6925.                                           " modified list %.4x\n"),
  6926.                             (int) dsc->u.block.regmask, new_regmask);

  6927.       dsc->u.block.regmask |= 0x8000;
  6928.       dsc->u.block.writeback = 0;
  6929.       dsc->u.block.cond = INST_AL;

  6930.       dsc->modinsn[0] = (insn1 & ~0x1ff) | (new_regmask & 0xff);

  6931.       dsc->cleanup = &cleanup_block_load_pc;
  6932.     }

  6933.   return 0;
  6934. }

  6935. static void
  6936. thumb_process_displaced_16bit_insn (struct gdbarch *gdbarch, uint16_t insn1,
  6937.                                     struct regcache *regs,
  6938.                                     struct displaced_step_closure *dsc)
  6939. {
  6940.   unsigned short op_bit_12_15 = bits (insn1, 12, 15);
  6941.   unsigned short op_bit_10_11 = bits (insn1, 10, 11);
  6942.   int err = 0;

  6943.   /* 16-bit thumb instructions.  */
  6944.   switch (op_bit_12_15)
  6945.     {
  6946.       /* Shift (imme), add, subtract, move and compare.  */
  6947.     case 0: case 1: case 2: case 3:
  6948.       err = thumb_copy_unmodified_16bit (gdbarch, insn1,
  6949.                                          "shift/add/sub/mov/cmp",
  6950.                                          dsc);
  6951.       break;
  6952.     case 4:
  6953.       switch (op_bit_10_11)
  6954.         {
  6955.         case 0: /* Data-processing */
  6956.           err = thumb_copy_unmodified_16bit (gdbarch, insn1,
  6957.                                              "data-processing",
  6958.                                              dsc);
  6959.           break;
  6960.         case 1: /* Special data instructions and branch and exchange.  */
  6961.           {
  6962.             unsigned short op = bits (insn1, 7, 9);
  6963.             if (op == 6 || op == 7) /* BX or BLX */
  6964.               err = thumb_copy_bx_blx_reg (gdbarch, insn1, regs, dsc);
  6965.             else if (bits (insn1, 6, 7) != 0) /* ADD/MOV/CMP high registers.  */
  6966.               err = thumb_copy_alu_reg (gdbarch, insn1, regs, dsc);
  6967.             else
  6968.               err = thumb_copy_unmodified_16bit (gdbarch, insn1, "special data",
  6969.                                                  dsc);
  6970.           }
  6971.           break;
  6972.         default: /* LDR (literal) */
  6973.           err = thumb_copy_16bit_ldr_literal (gdbarch, insn1, regs, dsc);
  6974.         }
  6975.       break;
  6976.     case 5: case 6: case 7: case 8: case 9: /* Load/Store single data item */
  6977.       err = thumb_copy_unmodified_16bit (gdbarch, insn1, "ldr/str", dsc);
  6978.       break;
  6979.     case 10:
  6980.       if (op_bit_10_11 < 2) /* Generate PC-relative address */
  6981.         err = thumb_decode_pc_relative_16bit (gdbarch, insn1, regs, dsc);
  6982.       else /* Generate SP-relative address */
  6983.         err = thumb_copy_unmodified_16bit (gdbarch, insn1, "sp-relative", dsc);
  6984.       break;
  6985.     case 11: /* Misc 16-bit instructions */
  6986.       {
  6987.         switch (bits (insn1, 8, 11))
  6988.           {
  6989.           case 1: case 3case 9: case 11: /* CBNZ, CBZ */
  6990.             err = thumb_copy_cbnz_cbz (gdbarch, insn1, regs, dsc);
  6991.             break;
  6992.           case 12: case 13: /* POP */
  6993.             if (bit (insn1, 8)) /* PC is in register list.  */
  6994.               err = thumb_copy_pop_pc_16bit (gdbarch, insn1, regs, dsc);
  6995.             else
  6996.               err = thumb_copy_unmodified_16bit (gdbarch, insn1, "pop", dsc);
  6997.             break;
  6998.           case 15: /* If-Then, and hints */
  6999.             if (bits (insn1, 0, 3))
  7000.               /* If-Then makes up to four following instructions conditional.
  7001.                  IT instruction itself is not conditional, so handle it as a
  7002.                  common unmodified instruction.  */
  7003.               err = thumb_copy_unmodified_16bit (gdbarch, insn1, "If-Then",
  7004.                                                  dsc);
  7005.             else
  7006.               err = thumb_copy_unmodified_16bit (gdbarch, insn1, "hints", dsc);
  7007.             break;
  7008.           default:
  7009.             err = thumb_copy_unmodified_16bit (gdbarch, insn1, "misc", dsc);
  7010.           }
  7011.       }
  7012.       break;
  7013.     case 12:
  7014.       if (op_bit_10_11 < 2) /* Store multiple registers */
  7015.         err = thumb_copy_unmodified_16bit (gdbarch, insn1, "stm", dsc);
  7016.       else /* Load multiple registers */
  7017.         err = thumb_copy_unmodified_16bit (gdbarch, insn1, "ldm", dsc);
  7018.       break;
  7019.     case 13: /* Conditional branch and supervisor call */
  7020.       if (bits (insn1, 9, 11) != 7) /* conditional branch */
  7021.         err = thumb_copy_b (gdbarch, insn1, dsc);
  7022.       else
  7023.         err = thumb_copy_svc (gdbarch, insn1, regs, dsc);
  7024.       break;
  7025.     case 14: /* Unconditional branch */
  7026.       err = thumb_copy_b (gdbarch, insn1, dsc);
  7027.       break;
  7028.     default:
  7029.       err = 1;
  7030.     }

  7031.   if (err)
  7032.     internal_error (__FILE__, __LINE__,
  7033.                     _("thumb_process_displaced_16bit_insn: Instruction decode error"));
  7034. }

  7035. static int
  7036. decode_thumb_32bit_ld_mem_hints (struct gdbarch *gdbarch,
  7037.                                  uint16_t insn1, uint16_t insn2,
  7038.                                  struct regcache *regs,
  7039.                                  struct displaced_step_closure *dsc)
  7040. {
  7041.   int rt = bits (insn2, 12, 15);
  7042.   int rn = bits (insn1, 0, 3);
  7043.   int op1 = bits (insn1, 7, 8);
  7044.   int err = 0;

  7045.   switch (bits (insn1, 5, 6))
  7046.     {
  7047.     case 0: /* Load byte and memory hints */
  7048.       if (rt == 0xf) /* PLD/PLI */
  7049.         {
  7050.           if (rn == 0xf)
  7051.             /* PLD literal or Encoding T3 of PLI(immediate, literal).  */
  7052.             return thumb2_copy_preload (gdbarch, insn1, insn2, regs, dsc);
  7053.           else
  7054.             return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
  7055.                                                 "pli/pld", dsc);
  7056.         }
  7057.       else
  7058.         {
  7059.           if (rn == 0xf) /* LDRB/LDRSB (literal) */
  7060.             return thumb2_copy_load_literal (gdbarch, insn1, insn2, regs, dsc,
  7061.                                              1);
  7062.           else
  7063.             return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
  7064.                                                 "ldrb{reg, immediate}/ldrbt",
  7065.                                                 dsc);
  7066.         }

  7067.       break;
  7068.     case 1: /* Load halfword and memory hints.  */
  7069.       if (rt == 0xf) /* PLD{W} and Unalloc memory hint.  */
  7070.         return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
  7071.                                             "pld/unalloc memhint", dsc);
  7072.       else
  7073.         {
  7074.           if (rn == 0xf)
  7075.             return thumb2_copy_load_literal (gdbarch, insn1, insn2, regs, dsc,
  7076.                                              2);
  7077.           else
  7078.             return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
  7079.                                                 "ldrh/ldrht", dsc);
  7080.         }
  7081.       break;
  7082.     case 2: /* Load word */
  7083.       {
  7084.         int insn2_bit_8_11 = bits (insn2, 8, 11);

  7085.         if (rn == 0xf)
  7086.           return thumb2_copy_load_literal (gdbarch, insn1, insn2, regs, dsc, 4);
  7087.         else if (op1 == 0x1) /* Encoding T3 */
  7088.           return thumb2_copy_load_reg_imm (gdbarch, insn1, insn2, regs, dsc,
  7089.                                            0, 1);
  7090.         else /* op1 == 0x0 */
  7091.           {
  7092.             if (insn2_bit_8_11 == 0xc || (insn2_bit_8_11 & 0x9) == 0x9)
  7093.               /* LDR (immediate) */
  7094.               return thumb2_copy_load_reg_imm (gdbarch, insn1, insn2, regs,
  7095.                                                dsc, bit (insn2, 8), 1);
  7096.             else if (insn2_bit_8_11 == 0xe) /* LDRT */
  7097.               return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
  7098.                                                   "ldrt", dsc);
  7099.             else
  7100.               /* LDR (register) */
  7101.               return thumb2_copy_load_reg_imm (gdbarch, insn1, insn2, regs,
  7102.                                                dsc, 0, 0);
  7103.           }
  7104.         break;
  7105.       }
  7106.     default:
  7107.       return thumb_32bit_copy_undef (gdbarch, insn1, insn2, dsc);
  7108.       break;
  7109.     }
  7110.   return 0;
  7111. }

  7112. static void
  7113. thumb_process_displaced_32bit_insn (struct gdbarch *gdbarch, uint16_t insn1,
  7114.                                     uint16_t insn2, struct regcache *regs,
  7115.                                     struct displaced_step_closure *dsc)
  7116. {
  7117.   int err = 0;
  7118.   unsigned short op = bit (insn2, 15);
  7119.   unsigned int op1 = bits (insn1, 11, 12);

  7120.   switch (op1)
  7121.     {
  7122.     case 1:
  7123.       {
  7124.         switch (bits (insn1, 9, 10))
  7125.           {
  7126.           case 0:
  7127.             if (bit (insn1, 6))
  7128.               {
  7129.                 /* Load/store {dual, execlusive}, table branch.  */
  7130.                 if (bits (insn1, 7, 8) == 1 && bits (insn1, 4, 5) == 1
  7131.                     && bits (insn2, 5, 7) == 0)
  7132.                   err = thumb2_copy_table_branch (gdbarch, insn1, insn2, regs,
  7133.                                                   dsc);
  7134.                 else
  7135.                   /* PC is not allowed to use in load/store {dual, exclusive}
  7136.                      instructions.  */
  7137.                   err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
  7138.                                                      "load/store dual/ex", dsc);
  7139.               }
  7140.             else /* load/store multiple */
  7141.               {
  7142.                 switch (bits (insn1, 7, 8))
  7143.                   {
  7144.                   case 0: case 3: /* SRS, RFE */
  7145.                     err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
  7146.                                                        "srs/rfe", dsc);
  7147.                     break;
  7148.                   case 1: case 2: /* LDM/STM/PUSH/POP */
  7149.                     err = thumb2_copy_block_xfer (gdbarch, insn1, insn2, regs, dsc);
  7150.                     break;
  7151.                   }
  7152.               }
  7153.             break;

  7154.           case 1:
  7155.             /* Data-processing (shift register).  */
  7156.             err = thumb2_decode_dp_shift_reg (gdbarch, insn1, insn2, regs,
  7157.                                               dsc);
  7158.             break;
  7159.           default: /* Coprocessor instructions.  */
  7160.             err = thumb2_decode_svc_copro (gdbarch, insn1, insn2, regs, dsc);
  7161.             break;
  7162.           }
  7163.       break;
  7164.       }
  7165.     case 2: /* op1 = 2 */
  7166.       if (op) /* Branch and misc control.  */
  7167.         {
  7168.           if (bit (insn2, 14/* BLX/BL */
  7169.               || bit (insn2, 12) /* Unconditional branch */
  7170.               || (bits (insn1, 7, 9) != 0x7)) /* Conditional branch */
  7171.             err = thumb2_copy_b_bl_blx (gdbarch, insn1, insn2, regs, dsc);
  7172.           else
  7173.             err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
  7174.                                                "misc ctrl", dsc);
  7175.         }
  7176.       else
  7177.         {
  7178.           if (bit (insn1, 9)) /* Data processing (plain binary imm).  */
  7179.             {
  7180.               int op = bits (insn1, 4, 8);
  7181.               int rn = bits (insn1, 0, 3);
  7182.               if ((op == 0 || op == 0xa) && rn == 0xf)
  7183.                 err = thumb_copy_pc_relative_32bit (gdbarch, insn1, insn2,
  7184.                                                     regs, dsc);
  7185.               else
  7186.                 err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
  7187.                                                    "dp/pb", dsc);
  7188.             }
  7189.           else /* Data processing (modified immeidate) */
  7190.             err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
  7191.                                                "dp/mi", dsc);
  7192.         }
  7193.       break;
  7194.     case 3: /* op1 = 3 */
  7195.       switch (bits (insn1, 9, 10))
  7196.         {
  7197.         case 0:
  7198.           if (bit (insn1, 4))
  7199.             err = decode_thumb_32bit_ld_mem_hints (gdbarch, insn1, insn2,
  7200.                                                    regs, dsc);
  7201.           else /* NEON Load/Store and Store single data item */
  7202.             err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
  7203.                                                "neon elt/struct load/store",
  7204.                                                dsc);
  7205.           break;
  7206.         case 1: /* op1 = 3, bits (9, 10) == 1 */
  7207.           switch (bits (insn1, 7, 8))
  7208.             {
  7209.             case 0: case 1: /* Data processing (register) */
  7210.               err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
  7211.                                                  "dp(reg)", dsc);
  7212.               break;
  7213.             case 2: /* Multiply and absolute difference */
  7214.               err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
  7215.                                                  "mul/mua/diff", dsc);
  7216.               break;
  7217.             case 3: /* Long multiply and divide */
  7218.               err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
  7219.                                                  "lmul/lmua", dsc);
  7220.               break;
  7221.             }
  7222.           break;
  7223.         default: /* Coprocessor instructions */
  7224.           err = thumb2_decode_svc_copro (gdbarch, insn1, insn2, regs, dsc);
  7225.           break;
  7226.         }
  7227.       break;
  7228.     default:
  7229.       err = 1;
  7230.     }

  7231.   if (err)
  7232.     internal_error (__FILE__, __LINE__,
  7233.                     _("thumb_process_displaced_32bit_insn: Instruction decode error"));

  7234. }

  7235. static void
  7236. thumb_process_displaced_insn (struct gdbarch *gdbarch, CORE_ADDR from,
  7237.                               CORE_ADDR to, struct regcache *regs,
  7238.                               struct displaced_step_closure *dsc)
  7239. {
  7240.   enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
  7241.   uint16_t insn1
  7242.     = read_memory_unsigned_integer (from, 2, byte_order_for_code);

  7243.   if (debug_displaced)
  7244.     fprintf_unfiltered (gdb_stdlog, "displaced: process thumb insn %.4x "
  7245.                         "at %.8lx\n", insn1, (unsigned long) from);

  7246.   dsc->is_thumb = 1;
  7247.   dsc->insn_size = thumb_insn_size (insn1);
  7248.   if (thumb_insn_size (insn1) == 4)
  7249.     {
  7250.       uint16_t insn2
  7251.         = read_memory_unsigned_integer (from + 2, 2, byte_order_for_code);
  7252.       thumb_process_displaced_32bit_insn (gdbarch, insn1, insn2, regs, dsc);
  7253.     }
  7254.   else
  7255.     thumb_process_displaced_16bit_insn (gdbarch, insn1, regs, dsc);
  7256. }

  7257. void
  7258. arm_process_displaced_insn (struct gdbarch *gdbarch, CORE_ADDR from,
  7259.                             CORE_ADDR to, struct regcache *regs,
  7260.                             struct displaced_step_closure *dsc)
  7261. {
  7262.   int err = 0;
  7263.   enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
  7264.   uint32_t insn;

  7265.   /* Most displaced instructions use a 1-instruction scratch space, so set this
  7266.      here and override below if/when necessary.  */
  7267.   dsc->numinsns = 1;
  7268.   dsc->insn_addr = from;
  7269.   dsc->scratch_base = to;
  7270.   dsc->cleanup = NULL;
  7271.   dsc->wrote_to_pc = 0;

  7272.   if (!displaced_in_arm_mode (regs))
  7273.     return thumb_process_displaced_insn (gdbarch, from, to, regs, dsc);

  7274.   dsc->is_thumb = 0;
  7275.   dsc->insn_size = 4;
  7276.   insn = read_memory_unsigned_integer (from, 4, byte_order_for_code);
  7277.   if (debug_displaced)
  7278.     fprintf_unfiltered (gdb_stdlog, "displaced: stepping insn %.8lx "
  7279.                         "at %.8lx\n", (unsigned long) insn,
  7280.                         (unsigned long) from);

  7281.   if ((insn & 0xf0000000) == 0xf0000000)
  7282.     err = arm_decode_unconditional (gdbarch, insn, regs, dsc);
  7283.   else switch (((insn & 0x10) >> 4) | ((insn & 0xe000000) >> 24))
  7284.     {
  7285.     case 0x0: case 0x1: case 0x2: case 0x3:
  7286.       err = arm_decode_dp_misc (gdbarch, insn, regs, dsc);
  7287.       break;

  7288.     case 0x4: case 0x5: case 0x6:
  7289.       err = arm_decode_ld_st_word_ubyte (gdbarch, insn, regs, dsc);
  7290.       break;

  7291.     case 0x7:
  7292.       err = arm_decode_media (gdbarch, insn, dsc);
  7293.       break;

  7294.     case 0x8: case 0x9: case 0xa: case 0xb:
  7295.       err = arm_decode_b_bl_ldmstm (gdbarch, insn, regs, dsc);
  7296.       break;

  7297.     case 0xc: case 0xd: case 0xe: case 0xf:
  7298.       err = arm_decode_svc_copro (gdbarch, insn, to, regs, dsc);
  7299.       break;
  7300.     }

  7301.   if (err)
  7302.     internal_error (__FILE__, __LINE__,
  7303.                     _("arm_process_displaced_insn: Instruction decode error"));
  7304. }

  7305. /* Actually set up the scratch space for a displaced instruction.  */

  7306. void
  7307. arm_displaced_init_closure (struct gdbarch *gdbarch, CORE_ADDR from,
  7308.                             CORE_ADDR to, struct displaced_step_closure *dsc)
  7309. {
  7310.   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
  7311.   unsigned int i, len, offset;
  7312.   enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
  7313.   int size = dsc->is_thumb? 2 : 4;
  7314.   const gdb_byte *bkp_insn;

  7315.   offset = 0;
  7316.   /* Poke modified instruction(s).  */
  7317.   for (i = 0; i < dsc->numinsns; i++)
  7318.     {
  7319.       if (debug_displaced)
  7320.         {
  7321.           fprintf_unfiltered (gdb_stdlog, "displaced: writing insn ");
  7322.           if (size == 4)
  7323.             fprintf_unfiltered (gdb_stdlog, "%.8lx",
  7324.                                 dsc->modinsn[i]);
  7325.           else if (size == 2)
  7326.             fprintf_unfiltered (gdb_stdlog, "%.4x",
  7327.                                 (unsigned short)dsc->modinsn[i]);

  7328.           fprintf_unfiltered (gdb_stdlog, " at %.8lx\n",
  7329.                               (unsigned long) to + offset);

  7330.         }
  7331.       write_memory_unsigned_integer (to + offset, size,
  7332.                                      byte_order_for_code,
  7333.                                      dsc->modinsn[i]);
  7334.       offset += size;
  7335.     }

  7336.   /* Choose the correct breakpoint instruction.  */
  7337.   if (dsc->is_thumb)
  7338.     {
  7339.       bkp_insn = tdep->thumb_breakpoint;
  7340.       len = tdep->thumb_breakpoint_size;
  7341.     }
  7342.   else
  7343.     {
  7344.       bkp_insn = tdep->arm_breakpoint;
  7345.       len = tdep->arm_breakpoint_size;
  7346.     }

  7347.   /* Put breakpoint afterwards.  */
  7348.   write_memory (to + offset, bkp_insn, len);

  7349.   if (debug_displaced)
  7350.     fprintf_unfiltered (gdb_stdlog, "displaced: copy %s->%s: ",
  7351.                         paddress (gdbarch, from), paddress (gdbarch, to));
  7352. }

  7353. /* Entry point for copying an instruction into scratch space for displaced
  7354.    stepping.  */

  7355. struct displaced_step_closure *
  7356. arm_displaced_step_copy_insn (struct gdbarch *gdbarch,
  7357.                               CORE_ADDR from, CORE_ADDR to,
  7358.                               struct regcache *regs)
  7359. {
  7360.   struct displaced_step_closure *dsc
  7361.     = xmalloc (sizeof (struct displaced_step_closure));
  7362.   arm_process_displaced_insn (gdbarch, from, to, regs, dsc);
  7363.   arm_displaced_init_closure (gdbarch, from, to, dsc);

  7364.   return dsc;
  7365. }

  7366. /* Entry point for cleaning things up after a displaced instruction has been
  7367.    single-stepped.  */

  7368. void
  7369. arm_displaced_step_fixup (struct gdbarch *gdbarch,
  7370.                           struct displaced_step_closure *dsc,
  7371.                           CORE_ADDR from, CORE_ADDR to,
  7372.                           struct regcache *regs)
  7373. {
  7374.   if (dsc->cleanup)
  7375.     dsc->cleanup (gdbarch, regs, dsc);

  7376.   if (!dsc->wrote_to_pc)
  7377.     regcache_cooked_write_unsigned (regs, ARM_PC_REGNUM,
  7378.                                     dsc->insn_addr + dsc->insn_size);

  7379. }

  7380. #include "bfd-in2.h"
  7381. #include "libcoff.h"

  7382. static int
  7383. gdb_print_insn_arm (bfd_vma memaddr, disassemble_info *info)
  7384. {
  7385.   struct gdbarch *gdbarch = info->application_data;

  7386.   if (arm_pc_is_thumb (gdbarch, memaddr))
  7387.     {
  7388.       static asymbol *asym;
  7389.       static combined_entry_type ce;
  7390.       static struct coff_symbol_struct csym;
  7391.       static struct bfd fake_bfd;
  7392.       static bfd_target fake_target;

  7393.       if (csym.native == NULL)
  7394.         {
  7395.           /* Create a fake symbol vector containing a Thumb symbol.
  7396.              This is solely so that the code in print_insn_little_arm()
  7397.              and print_insn_big_arm() in opcodes/arm-dis.c will detect
  7398.              the presence of a Thumb symbol and switch to decoding
  7399.              Thumb instructions.  */

  7400.           fake_target.flavour = bfd_target_coff_flavour;
  7401.           fake_bfd.xvec = &fake_target;
  7402.           ce.u.syment.n_sclass = C_THUMBEXTFUNC;
  7403.           csym.native = &ce;
  7404.           csym.symbol.the_bfd = &fake_bfd;
  7405.           csym.symbol.name = "fake";
  7406.           asym = (asymbol *) & csym;
  7407.         }

  7408.       memaddr = UNMAKE_THUMB_ADDR (memaddr);
  7409.       info->symbols = &asym;
  7410.     }
  7411.   else
  7412.     info->symbols = NULL;

  7413.   if (info->endian == BFD_ENDIAN_BIG)
  7414.     return print_insn_big_arm (memaddr, info);
  7415.   else
  7416.     return print_insn_little_arm (memaddr, info);
  7417. }

  7418. /* The following define instruction sequences that will cause ARM
  7419.    cpu's to take an undefined instruction trap.  These are used to
  7420.    signal a breakpoint to GDB.

  7421.    The newer ARMv4T cpu's are capable of operating in ARM or Thumb
  7422.    modes.  A different instruction is required for each mode.  The ARM
  7423.    cpu's can also be big or little endian.  Thus four different
  7424.    instructions are needed to support all cases.

  7425.    Note: ARMv4 defines several new instructions that will take the
  7426.    undefined instruction trap.  ARM7TDMI is nominally ARMv4T, but does
  7427.    not in fact add the new instructions.  The new undefined
  7428.    instructions in ARMv4 are all instructions that had no defined
  7429.    behaviour in earlier chips.  There is no guarantee that they will
  7430.    raise an exception, but may be treated as NOP's.  In practice, it
  7431.    may only safe to rely on instructions matching:

  7432.    3 3 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1
  7433.    1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0
  7434.    C C C C 0 1 1 x x x x x x x x x x x x x x x x x x x x 1 x x x x

  7435.    Even this may only true if the condition predicate is true.  The
  7436.    following use a condition predicate of ALWAYS so it is always TRUE.

  7437.    There are other ways of forcing a breakpoint.  GNU/Linux, RISC iX,
  7438.    and NetBSD all use a software interrupt rather than an undefined
  7439.    instruction to force a trap.  This can be handled by by the
  7440.    abi-specific code during establishment of the gdbarch vector.  */

  7441. #define ARM_LE_BREAKPOINT {0xFE,0xDE,0xFF,0xE7}
  7442. #define ARM_BE_BREAKPOINT {0xE7,0xFF,0xDE,0xFE}
  7443. #define THUMB_LE_BREAKPOINT {0xbe,0xbe}
  7444. #define THUMB_BE_BREAKPOINT {0xbe,0xbe}

  7445. static const gdb_byte arm_default_arm_le_breakpoint[] = ARM_LE_BREAKPOINT;
  7446. static const gdb_byte arm_default_arm_be_breakpoint[] = ARM_BE_BREAKPOINT;
  7447. static const gdb_byte arm_default_thumb_le_breakpoint[] = THUMB_LE_BREAKPOINT;
  7448. static const gdb_byte arm_default_thumb_be_breakpoint[] = THUMB_BE_BREAKPOINT;

  7449. /* Determine the type and size of breakpoint to insert at PCPTR.  Uses
  7450.    the program counter value to determine whether a 16-bit or 32-bit
  7451.    breakpoint should be used.  It returns a pointer to a string of
  7452.    bytes that encode a breakpoint instruction, stores the length of
  7453.    the string to *lenptr, and adjusts the program counter (if
  7454.    necessary) to point to the actual memory location where the
  7455.    breakpoint should be inserted.  */

  7456. static const unsigned char *
  7457. arm_breakpoint_from_pc (struct gdbarch *gdbarch, CORE_ADDR *pcptr, int *lenptr)
  7458. {
  7459.   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
  7460.   enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);

  7461.   if (arm_pc_is_thumb (gdbarch, *pcptr))
  7462.     {
  7463.       *pcptr = UNMAKE_THUMB_ADDR (*pcptr);

  7464.       /* If we have a separate 32-bit breakpoint instruction for Thumb-2,
  7465.          check whether we are replacing a 32-bit instruction.  */
  7466.       if (tdep->thumb2_breakpoint != NULL)
  7467.         {
  7468.           gdb_byte buf[2];
  7469.           if (target_read_memory (*pcptr, buf, 2) == 0)
  7470.             {
  7471.               unsigned short inst1;
  7472.               inst1 = extract_unsigned_integer (buf, 2, byte_order_for_code);
  7473.               if (thumb_insn_size (inst1) == 4)
  7474.                 {
  7475.                   *lenptr = tdep->thumb2_breakpoint_size;
  7476.                   return tdep->thumb2_breakpoint;
  7477.                 }
  7478.             }
  7479.         }

  7480.       *lenptr = tdep->thumb_breakpoint_size;
  7481.       return tdep->thumb_breakpoint;
  7482.     }
  7483.   else
  7484.     {
  7485.       *lenptr = tdep->arm_breakpoint_size;
  7486.       return tdep->arm_breakpoint;
  7487.     }
  7488. }

  7489. static void
  7490. arm_remote_breakpoint_from_pc (struct gdbarch *gdbarch, CORE_ADDR *pcptr,
  7491.                                int *kindptr)
  7492. {
  7493.   arm_breakpoint_from_pc (gdbarch, pcptr, kindptr);

  7494.   if (arm_pc_is_thumb (gdbarch, *pcptr) && *kindptr == 4)
  7495.     /* The documented magic value for a 32-bit Thumb-2 breakpoint, so
  7496.        that this is not confused with a 32-bit ARM breakpoint.  */
  7497.     *kindptr = 3;
  7498. }

  7499. /* Extract from an array REGBUF containing the (raw) register state a
  7500.    function return value of type TYPE, and copy that, in virtual
  7501.    format, into VALBUF.  */

  7502. static void
  7503. arm_extract_return_value (struct type *type, struct regcache *regs,
  7504.                           gdb_byte *valbuf)
  7505. {
  7506.   struct gdbarch *gdbarch = get_regcache_arch (regs);
  7507.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);

  7508.   if (TYPE_CODE_FLT == TYPE_CODE (type))
  7509.     {
  7510.       switch (gdbarch_tdep (gdbarch)->fp_model)
  7511.         {
  7512.         case ARM_FLOAT_FPA:
  7513.           {
  7514.             /* The value is in register F0 in internal format.  We need to
  7515.                extract the raw value and then convert it to the desired
  7516.                internal type.  */
  7517.             bfd_byte tmpbuf[FP_REGISTER_SIZE];

  7518.             regcache_cooked_read (regs, ARM_F0_REGNUM, tmpbuf);
  7519.             convert_from_extended (floatformat_from_type (type), tmpbuf,
  7520.                                    valbuf, gdbarch_byte_order (gdbarch));
  7521.           }
  7522.           break;

  7523.         case ARM_FLOAT_SOFT_FPA:
  7524.         case ARM_FLOAT_SOFT_VFP:
  7525.           /* ARM_FLOAT_VFP can arise if this is a variadic function so
  7526.              not using the VFP ABI code.  */
  7527.         case ARM_FLOAT_VFP:
  7528.           regcache_cooked_read (regs, ARM_A1_REGNUM, valbuf);
  7529.           if (TYPE_LENGTH (type) > 4)
  7530.             regcache_cooked_read (regs, ARM_A1_REGNUM + 1,
  7531.                                   valbuf + INT_REGISTER_SIZE);
  7532.           break;

  7533.         default:
  7534.           internal_error (__FILE__, __LINE__,
  7535.                           _("arm_extract_return_value: "
  7536.                             "Floating point model not supported"));
  7537.           break;
  7538.         }
  7539.     }
  7540.   else if (TYPE_CODE (type) == TYPE_CODE_INT
  7541.            || TYPE_CODE (type) == TYPE_CODE_CHAR
  7542.            || TYPE_CODE (type) == TYPE_CODE_BOOL
  7543.            || TYPE_CODE (type) == TYPE_CODE_PTR
  7544.            || TYPE_CODE (type) == TYPE_CODE_REF
  7545.            || TYPE_CODE (type) == TYPE_CODE_ENUM)
  7546.     {
  7547.       /* If the type is a plain integer, then the access is
  7548.          straight-forward.  Otherwise we have to play around a bit
  7549.          more.  */
  7550.       int len = TYPE_LENGTH (type);
  7551.       int regno = ARM_A1_REGNUM;
  7552.       ULONGEST tmp;

  7553.       while (len > 0)
  7554.         {
  7555.           /* By using store_unsigned_integer we avoid having to do
  7556.              anything special for small big-endian values.  */
  7557.           regcache_cooked_read_unsigned (regs, regno++, &tmp);
  7558.           store_unsigned_integer (valbuf,
  7559.                                   (len > INT_REGISTER_SIZE
  7560.                                    ? INT_REGISTER_SIZE : len),
  7561.                                   byte_order, tmp);
  7562.           len -= INT_REGISTER_SIZE;
  7563.           valbuf += INT_REGISTER_SIZE;
  7564.         }
  7565.     }
  7566.   else
  7567.     {
  7568.       /* For a structure or union the behaviour is as if the value had
  7569.          been stored to word-aligned memory and then loaded into
  7570.          registers with 32-bit load instruction(s).  */
  7571.       int len = TYPE_LENGTH (type);
  7572.       int regno = ARM_A1_REGNUM;
  7573.       bfd_byte tmpbuf[INT_REGISTER_SIZE];

  7574.       while (len > 0)
  7575.         {
  7576.           regcache_cooked_read (regs, regno++, tmpbuf);
  7577.           memcpy (valbuf, tmpbuf,
  7578.                   len > INT_REGISTER_SIZE ? INT_REGISTER_SIZE : len);
  7579.           len -= INT_REGISTER_SIZE;
  7580.           valbuf += INT_REGISTER_SIZE;
  7581.         }
  7582.     }
  7583. }


  7584. /* Will a function return an aggregate type in memory or in a
  7585.    register?  Return 0 if an aggregate type can be returned in a
  7586.    register, 1 if it must be returned in memory.  */

  7587. static int
  7588. arm_return_in_memory (struct gdbarch *gdbarch, struct type *type)
  7589. {
  7590.   int nRc;
  7591.   enum type_code code;

  7592.   CHECK_TYPEDEF (type);

  7593.   /* In the ARM ABI, "integer" like aggregate types are returned in
  7594.      registers.  For an aggregate type to be integer like, its size
  7595.      must be less than or equal to INT_REGISTER_SIZE and the
  7596.      offset of each addressable subfield must be zero.  Note that bit
  7597.      fields are not addressable, and all addressable subfields of
  7598.      unions always start at offset zero.

  7599.      This function is based on the behaviour of GCC 2.95.1.
  7600.      See: gcc/arm.c: arm_return_in_memory() for details.

  7601.      Note: All versions of GCC before GCC 2.95.2 do not set up the
  7602.      parameters correctly for a function returning the following
  7603.      structure: struct { float f;}; This should be returned in memory,
  7604.      not a register.  Richard Earnshaw sent me a patch, but I do not
  7605.      know of any way to detect if a function like the above has been
  7606.      compiled with the correct calling convention.  */

  7607.   /* All aggregate types that won't fit in a register must be returned
  7608.      in memory.  */
  7609.   if (TYPE_LENGTH (type) > INT_REGISTER_SIZE)
  7610.     {
  7611.       return 1;
  7612.     }

  7613.   /* The AAPCS says all aggregates not larger than a word are returned
  7614.      in a register.  */
  7615.   if (gdbarch_tdep (gdbarch)->arm_abi != ARM_ABI_APCS)
  7616.     return 0;

  7617.   /* The only aggregate types that can be returned in a register are
  7618.      structs and unions.  Arrays must be returned in memory.  */
  7619.   code = TYPE_CODE (type);
  7620.   if ((TYPE_CODE_STRUCT != code) && (TYPE_CODE_UNION != code))
  7621.     {
  7622.       return 1;
  7623.     }

  7624.   /* Assume all other aggregate types can be returned in a register.
  7625.      Run a check for structures, unions and arrays.  */
  7626.   nRc = 0;

  7627.   if ((TYPE_CODE_STRUCT == code) || (TYPE_CODE_UNION == code))
  7628.     {
  7629.       int i;
  7630.       /* Need to check if this struct/union is "integer" like.  For
  7631.          this to be true, its size must be less than or equal to
  7632.          INT_REGISTER_SIZE and the offset of each addressable
  7633.          subfield must be zero.  Note that bit fields are not
  7634.          addressable, and unions always start at offset zero.  If any
  7635.          of the subfields is a floating point type, the struct/union
  7636.          cannot be an integer type.  */

  7637.       /* For each field in the object, check:
  7638.          1) Is it FP? --> yes, nRc = 1;
  7639.          2) Is it addressable (bitpos != 0) and
  7640.          not packed (bitsize == 0)?
  7641.          --> yes, nRc = 1
  7642.        */

  7643.       for (i = 0; i < TYPE_NFIELDS (type); i++)
  7644.         {
  7645.           enum type_code field_type_code;
  7646.           field_type_code = TYPE_CODE (check_typedef (TYPE_FIELD_TYPE (type,
  7647.                                                                        i)));

  7648.           /* Is it a floating point type field?  */
  7649.           if (field_type_code == TYPE_CODE_FLT)
  7650.             {
  7651.               nRc = 1;
  7652.               break;
  7653.             }

  7654.           /* If bitpos != 0, then we have to care about it.  */
  7655.           if (TYPE_FIELD_BITPOS (type, i) != 0)
  7656.             {
  7657.               /* Bitfields are not addressable.  If the field bitsize is
  7658.                  zero, then the field is not packed.  Hence it cannot be
  7659.                  a bitfield or any other packed type.  */
  7660.               if (TYPE_FIELD_BITSIZE (type, i) == 0)
  7661.                 {
  7662.                   nRc = 1;
  7663.                   break;
  7664.                 }
  7665.             }
  7666.         }
  7667.     }

  7668.   return nRc;
  7669. }

  7670. /* Write into appropriate registers a function return value of type
  7671.    TYPE, given in virtual format.  */

  7672. static void
  7673. arm_store_return_value (struct type *type, struct regcache *regs,
  7674.                         const gdb_byte *valbuf)
  7675. {
  7676.   struct gdbarch *gdbarch = get_regcache_arch (regs);
  7677.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);

  7678.   if (TYPE_CODE (type) == TYPE_CODE_FLT)
  7679.     {
  7680.       gdb_byte buf[MAX_REGISTER_SIZE];

  7681.       switch (gdbarch_tdep (gdbarch)->fp_model)
  7682.         {
  7683.         case ARM_FLOAT_FPA:

  7684.           convert_to_extended (floatformat_from_type (type), buf, valbuf,
  7685.                                gdbarch_byte_order (gdbarch));
  7686.           regcache_cooked_write (regs, ARM_F0_REGNUM, buf);
  7687.           break;

  7688.         case ARM_FLOAT_SOFT_FPA:
  7689.         case ARM_FLOAT_SOFT_VFP:
  7690.           /* ARM_FLOAT_VFP can arise if this is a variadic function so
  7691.              not using the VFP ABI code.  */
  7692.         case ARM_FLOAT_VFP:
  7693.           regcache_cooked_write (regs, ARM_A1_REGNUM, valbuf);
  7694.           if (TYPE_LENGTH (type) > 4)
  7695.             regcache_cooked_write (regs, ARM_A1_REGNUM + 1,
  7696.                                    valbuf + INT_REGISTER_SIZE);
  7697.           break;

  7698.         default:
  7699.           internal_error (__FILE__, __LINE__,
  7700.                           _("arm_store_return_value: Floating "
  7701.                             "point model not supported"));
  7702.           break;
  7703.         }
  7704.     }
  7705.   else if (TYPE_CODE (type) == TYPE_CODE_INT
  7706.            || TYPE_CODE (type) == TYPE_CODE_CHAR
  7707.            || TYPE_CODE (type) == TYPE_CODE_BOOL
  7708.            || TYPE_CODE (type) == TYPE_CODE_PTR
  7709.            || TYPE_CODE (type) == TYPE_CODE_REF
  7710.            || TYPE_CODE (type) == TYPE_CODE_ENUM)
  7711.     {
  7712.       if (TYPE_LENGTH (type) <= 4)
  7713.         {
  7714.           /* Values of one word or less are zero/sign-extended and
  7715.              returned in r0.  */
  7716.           bfd_byte tmpbuf[INT_REGISTER_SIZE];
  7717.           LONGEST val = unpack_long (type, valbuf);

  7718.           store_signed_integer (tmpbuf, INT_REGISTER_SIZE, byte_order, val);
  7719.           regcache_cooked_write (regs, ARM_A1_REGNUM, tmpbuf);
  7720.         }
  7721.       else
  7722.         {
  7723.           /* Integral values greater than one word are stored in consecutive
  7724.              registers starting with r0.  This will always be a multiple of
  7725.              the regiser size.  */
  7726.           int len = TYPE_LENGTH (type);
  7727.           int regno = ARM_A1_REGNUM;

  7728.           while (len > 0)
  7729.             {
  7730.               regcache_cooked_write (regs, regno++, valbuf);
  7731.               len -= INT_REGISTER_SIZE;
  7732.               valbuf += INT_REGISTER_SIZE;
  7733.             }
  7734.         }
  7735.     }
  7736.   else
  7737.     {
  7738.       /* For a structure or union the behaviour is as if the value had
  7739.          been stored to word-aligned memory and then loaded into
  7740.          registers with 32-bit load instruction(s).  */
  7741.       int len = TYPE_LENGTH (type);
  7742.       int regno = ARM_A1_REGNUM;
  7743.       bfd_byte tmpbuf[INT_REGISTER_SIZE];

  7744.       while (len > 0)
  7745.         {
  7746.           memcpy (tmpbuf, valbuf,
  7747.                   len > INT_REGISTER_SIZE ? INT_REGISTER_SIZE : len);
  7748.           regcache_cooked_write (regs, regno++, tmpbuf);
  7749.           len -= INT_REGISTER_SIZE;
  7750.           valbuf += INT_REGISTER_SIZE;
  7751.         }
  7752.     }
  7753. }


  7754. /* Handle function return values.  */

  7755. static enum return_value_convention
  7756. arm_return_value (struct gdbarch *gdbarch, struct value *function,
  7757.                   struct type *valtype, struct regcache *regcache,
  7758.                   gdb_byte *readbuf, const gdb_byte *writebuf)
  7759. {
  7760.   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
  7761.   struct type *func_type = function ? value_type (function) : NULL;
  7762.   enum arm_vfp_cprc_base_type vfp_base_type;
  7763.   int vfp_base_count;

  7764.   if (arm_vfp_abi_for_function (gdbarch, func_type)
  7765.       && arm_vfp_call_candidate (valtype, &vfp_base_type, &vfp_base_count))
  7766.     {
  7767.       int reg_char = arm_vfp_cprc_reg_char (vfp_base_type);
  7768.       int unit_length = arm_vfp_cprc_unit_length (vfp_base_type);
  7769.       int i;
  7770.       for (i = 0; i < vfp_base_count; i++)
  7771.         {
  7772.           if (reg_char == 'q')
  7773.             {
  7774.               if (writebuf)
  7775.                 arm_neon_quad_write (gdbarch, regcache, i,
  7776.                                      writebuf + i * unit_length);

  7777.               if (readbuf)
  7778.                 arm_neon_quad_read (gdbarch, regcache, i,
  7779.                                     readbuf + i * unit_length);
  7780.             }
  7781.           else
  7782.             {
  7783.               char name_buf[4];
  7784.               int regnum;

  7785.               xsnprintf (name_buf, sizeof (name_buf), "%c%d", reg_char, i);
  7786.               regnum = user_reg_map_name_to_regnum (gdbarch, name_buf,
  7787.                                                     strlen (name_buf));
  7788.               if (writebuf)
  7789.                 regcache_cooked_write (regcache, regnum,
  7790.                                        writebuf + i * unit_length);
  7791.               if (readbuf)
  7792.                 regcache_cooked_read (regcache, regnum,
  7793.                                       readbuf + i * unit_length);
  7794.             }
  7795.         }
  7796.       return RETURN_VALUE_REGISTER_CONVENTION;
  7797.     }

  7798.   if (TYPE_CODE (valtype) == TYPE_CODE_STRUCT
  7799.       || TYPE_CODE (valtype) == TYPE_CODE_UNION
  7800.       || TYPE_CODE (valtype) == TYPE_CODE_ARRAY)
  7801.     {
  7802.       if (tdep->struct_return == pcc_struct_return
  7803.           || arm_return_in_memory (gdbarch, valtype))
  7804.         return RETURN_VALUE_STRUCT_CONVENTION;
  7805.     }

  7806.   /* AAPCS returns complex types longer than a register in memory.  */
  7807.   if (tdep->arm_abi != ARM_ABI_APCS
  7808.       && TYPE_CODE (valtype) == TYPE_CODE_COMPLEX
  7809.       && TYPE_LENGTH (valtype) > INT_REGISTER_SIZE)
  7810.     return RETURN_VALUE_STRUCT_CONVENTION;

  7811.   if (writebuf)
  7812.     arm_store_return_value (valtype, regcache, writebuf);

  7813.   if (readbuf)
  7814.     arm_extract_return_value (valtype, regcache, readbuf);

  7815.   return RETURN_VALUE_REGISTER_CONVENTION;
  7816. }


  7817. static int
  7818. arm_get_longjmp_target (struct frame_info *frame, CORE_ADDR *pc)
  7819. {
  7820.   struct gdbarch *gdbarch = get_frame_arch (frame);
  7821.   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
  7822.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  7823.   CORE_ADDR jb_addr;
  7824.   gdb_byte buf[INT_REGISTER_SIZE];

  7825.   jb_addr = get_frame_register_unsigned (frame, ARM_A1_REGNUM);

  7826.   if (target_read_memory (jb_addr + tdep->jb_pc * tdep->jb_elt_size, buf,
  7827.                           INT_REGISTER_SIZE))
  7828.     return 0;

  7829.   *pc = extract_unsigned_integer (buf, INT_REGISTER_SIZE, byte_order);
  7830.   return 1;
  7831. }

  7832. /* Recognize GCC and GNU ld's trampolines.  If we are in a trampoline,
  7833.    return the target PC.  Otherwise return 0.  */

  7834. CORE_ADDR
  7835. arm_skip_stub (struct frame_info *frame, CORE_ADDR pc)
  7836. {
  7837.   const char *name;
  7838.   int namelen;
  7839.   CORE_ADDR start_addr;

  7840.   /* Find the starting address and name of the function containing the PC.  */
  7841.   if (find_pc_partial_function (pc, &name, &start_addr, NULL) == 0)
  7842.     {
  7843.       /* Trampoline 'bx reg' doesn't belong to any functions.  Do the
  7844.          check here.  */
  7845.       start_addr = arm_skip_bx_reg (frame, pc);
  7846.       if (start_addr != 0)
  7847.         return start_addr;

  7848.       return 0;
  7849.     }

  7850.   /* If PC is in a Thumb call or return stub, return the address of the
  7851.      target PC, which is in a register.  The thunk functions are called
  7852.      _call_via_xx, where x is the register name.  The possible names
  7853.      are r0-r9, sl, fp, ip, sp, and lr.  ARM RealView has similar
  7854.      functions, named __ARM_call_via_r[0-7].  */
  7855.   if (strncmp (name, "_call_via_", 10) == 0
  7856.       || strncmp (name, "__ARM_call_via_", strlen ("__ARM_call_via_")) == 0)
  7857.     {
  7858.       /* Use the name suffix to determine which register contains the
  7859.          target PC.  */
  7860.       static char *table[15] =
  7861.       {"r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
  7862.        "r8", "r9", "sl", "fp", "ip", "sp", "lr"
  7863.       };
  7864.       int regno;
  7865.       int offset = strlen (name) - 2;

  7866.       for (regno = 0; regno <= 14; regno++)
  7867.         if (strcmp (&name[offset], table[regno]) == 0)
  7868.           return get_frame_register_unsigned (frame, regno);
  7869.     }

  7870.   /* GNU ld generates __foo_from_arm or __foo_from_thumb for
  7871.      non-interworking calls to foo.  We could decode the stubs
  7872.      to find the target but it's easier to use the symbol table.  */
  7873.   namelen = strlen (name);
  7874.   if (name[0] == '_' && name[1] == '_'
  7875.       && ((namelen > 2 + strlen ("_from_thumb")
  7876.            && strncmp (name + namelen - strlen ("_from_thumb"), "_from_thumb",
  7877.                        strlen ("_from_thumb")) == 0)
  7878.           || (namelen > 2 + strlen ("_from_arm")
  7879.               && strncmp (name + namelen - strlen ("_from_arm"), "_from_arm",
  7880.                           strlen ("_from_arm")) == 0)))
  7881.     {
  7882.       char *target_name;
  7883.       int target_len = namelen - 2;
  7884.       struct bound_minimal_symbol minsym;
  7885.       struct objfile *objfile;
  7886.       struct obj_section *sec;

  7887.       if (name[namelen - 1] == 'b')
  7888.         target_len -= strlen ("_from_thumb");
  7889.       else
  7890.         target_len -= strlen ("_from_arm");

  7891.       target_name = alloca (target_len + 1);
  7892.       memcpy (target_name, name + 2, target_len);
  7893.       target_name[target_len] = '\0';

  7894.       sec = find_pc_section (pc);
  7895.       objfile = (sec == NULL) ? NULL : sec->objfile;
  7896.       minsym = lookup_minimal_symbol (target_name, NULL, objfile);
  7897.       if (minsym.minsym != NULL)
  7898.         return BMSYMBOL_VALUE_ADDRESS (minsym);
  7899.       else
  7900.         return 0;
  7901.     }

  7902.   return 0;                        /* not a stub */
  7903. }

  7904. static void
  7905. set_arm_command (char *args, int from_tty)
  7906. {
  7907.   printf_unfiltered (_("\
  7908. \"set arm\" must be followed by an apporpriate subcommand.\n"));
  7909.   help_list (setarmcmdlist, "set arm ", all_commands, gdb_stdout);
  7910. }

  7911. static void
  7912. show_arm_command (char *args, int from_tty)
  7913. {
  7914.   cmd_show_list (showarmcmdlist, from_tty, "");
  7915. }

  7916. static void
  7917. arm_update_current_architecture (void)
  7918. {
  7919.   struct gdbarch_info info;

  7920.   /* If the current architecture is not ARM, we have nothing to do.  */
  7921.   if (gdbarch_bfd_arch_info (target_gdbarch ())->arch != bfd_arch_arm)
  7922.     return;

  7923.   /* Update the architecture.  */
  7924.   gdbarch_info_init (&info);

  7925.   if (!gdbarch_update_p (info))
  7926.     internal_error (__FILE__, __LINE__, _("could not update architecture"));
  7927. }

  7928. static void
  7929. set_fp_model_sfunc (char *args, int from_tty,
  7930.                     struct cmd_list_element *c)
  7931. {
  7932.   enum arm_float_model fp_model;

  7933.   for (fp_model = ARM_FLOAT_AUTO; fp_model != ARM_FLOAT_LAST; fp_model++)
  7934.     if (strcmp (current_fp_model, fp_model_strings[fp_model]) == 0)
  7935.       {
  7936.         arm_fp_model = fp_model;
  7937.         break;
  7938.       }

  7939.   if (fp_model == ARM_FLOAT_LAST)
  7940.     internal_error (__FILE__, __LINE__, _("Invalid fp model accepted: %s."),
  7941.                     current_fp_model);

  7942.   arm_update_current_architecture ();
  7943. }

  7944. static void
  7945. show_fp_model (struct ui_file *file, int from_tty,
  7946.                struct cmd_list_element *c, const char *value)
  7947. {
  7948.   struct gdbarch_tdep *tdep = gdbarch_tdep (target_gdbarch ());

  7949.   if (arm_fp_model == ARM_FLOAT_AUTO
  7950.       && gdbarch_bfd_arch_info (target_gdbarch ())->arch == bfd_arch_arm)
  7951.     fprintf_filtered (file, _("\
  7952. The current ARM floating point model is \"auto\" (currently \"%s\").\n"),
  7953.                       fp_model_strings[tdep->fp_model]);
  7954.   else
  7955.     fprintf_filtered (file, _("\
  7956. The current ARM floating point model is \"%s\".\n"),
  7957.                       fp_model_strings[arm_fp_model]);
  7958. }

  7959. static void
  7960. arm_set_abi (char *args, int from_tty,
  7961.              struct cmd_list_element *c)
  7962. {
  7963.   enum arm_abi_kind arm_abi;

  7964.   for (arm_abi = ARM_ABI_AUTO; arm_abi != ARM_ABI_LAST; arm_abi++)
  7965.     if (strcmp (arm_abi_string, arm_abi_strings[arm_abi]) == 0)
  7966.       {
  7967.         arm_abi_global = arm_abi;
  7968.         break;
  7969.       }

  7970.   if (arm_abi == ARM_ABI_LAST)
  7971.     internal_error (__FILE__, __LINE__, _("Invalid ABI accepted: %s."),
  7972.                     arm_abi_string);

  7973.   arm_update_current_architecture ();
  7974. }

  7975. static void
  7976. arm_show_abi (struct ui_file *file, int from_tty,
  7977.              struct cmd_list_element *c, const char *value)
  7978. {
  7979.   struct gdbarch_tdep *tdep = gdbarch_tdep (target_gdbarch ());

  7980.   if (arm_abi_global == ARM_ABI_AUTO
  7981.       && gdbarch_bfd_arch_info (target_gdbarch ())->arch == bfd_arch_arm)
  7982.     fprintf_filtered (file, _("\
  7983. The current ARM ABI is \"auto\" (currently \"%s\").\n"),
  7984.                       arm_abi_strings[tdep->arm_abi]);
  7985.   else
  7986.     fprintf_filtered (file, _("The current ARM ABI is \"%s\".\n"),
  7987.                       arm_abi_string);
  7988. }

  7989. static void
  7990. arm_show_fallback_mode (struct ui_file *file, int from_tty,
  7991.                         struct cmd_list_element *c, const char *value)
  7992. {
  7993.   fprintf_filtered (file,
  7994.                     _("The current execution mode assumed "
  7995.                       "(when symbols are unavailable) is \"%s\".\n"),
  7996.                     arm_fallback_mode_string);
  7997. }

  7998. static void
  7999. arm_show_force_mode (struct ui_file *file, int from_tty,
  8000.                      struct cmd_list_element *c, const char *value)
  8001. {
  8002.   struct gdbarch_tdep *tdep = gdbarch_tdep (target_gdbarch ());

  8003.   fprintf_filtered (file,
  8004.                     _("The current execution mode assumed "
  8005.                       "(even when symbols are available) is \"%s\".\n"),
  8006.                     arm_force_mode_string);
  8007. }

  8008. /* If the user changes the register disassembly style used for info
  8009.    register and other commands, we have to also switch the style used
  8010.    in opcodes for disassembly output.  This function is run in the "set
  8011.    arm disassembly" command, and does that.  */

  8012. static void
  8013. set_disassembly_style_sfunc (char *args, int from_tty,
  8014.                               struct cmd_list_element *c)
  8015. {
  8016.   set_disassembly_style ();
  8017. }

  8018. /* Return the ARM register name corresponding to register I.  */
  8019. static const char *
  8020. arm_register_name (struct gdbarch *gdbarch, int i)
  8021. {
  8022.   const int num_regs = gdbarch_num_regs (gdbarch);

  8023.   if (gdbarch_tdep (gdbarch)->have_vfp_pseudos
  8024.       && i >= num_regs && i < num_regs + 32)
  8025.     {
  8026.       static const char *const vfp_pseudo_names[] = {
  8027.         "s0", "s1", "s2", "s3", "s4", "s5", "s6", "s7",
  8028.         "s8", "s9", "s10", "s11", "s12", "s13", "s14", "s15",
  8029.         "s16", "s17", "s18", "s19", "s20", "s21", "s22", "s23",
  8030.         "s24", "s25", "s26", "s27", "s28", "s29", "s30", "s31",
  8031.       };

  8032.       return vfp_pseudo_names[i - num_regs];
  8033.     }

  8034.   if (gdbarch_tdep (gdbarch)->have_neon_pseudos
  8035.       && i >= num_regs + 32 && i < num_regs + 32 + 16)
  8036.     {
  8037.       static const char *const neon_pseudo_names[] = {
  8038.         "q0", "q1", "q2", "q3", "q4", "q5", "q6", "q7",
  8039.         "q8", "q9", "q10", "q11", "q12", "q13", "q14", "q15",
  8040.       };

  8041.       return neon_pseudo_names[i - num_regs - 32];
  8042.     }

  8043.   if (i >= ARRAY_SIZE (arm_register_names))
  8044.     /* These registers are only supported on targets which supply
  8045.        an XML description.  */
  8046.     return "";

  8047.   return arm_register_names[i];
  8048. }

  8049. static void
  8050. set_disassembly_style (void)
  8051. {
  8052.   int current;

  8053.   /* Find the style that the user wants.  */
  8054.   for (current = 0; current < num_disassembly_options; current++)
  8055.     if (disassembly_style == valid_disassembly_styles[current])
  8056.       break;
  8057.   gdb_assert (current < num_disassembly_options);

  8058.   /* Synchronize the disassembler.  */
  8059.   set_arm_regname_option (current);
  8060. }

  8061. /* Test whether the coff symbol specific value corresponds to a Thumb
  8062.    function.  */

  8063. static int
  8064. coff_sym_is_thumb (int val)
  8065. {
  8066.   return (val == C_THUMBEXT
  8067.           || val == C_THUMBSTAT
  8068.           || val == C_THUMBEXTFUNC
  8069.           || val == C_THUMBSTATFUNC
  8070.           || val == C_THUMBLABEL);
  8071. }

  8072. /* arm_coff_make_msymbol_special()
  8073.    arm_elf_make_msymbol_special()

  8074.    These functions test whether the COFF or ELF symbol corresponds to
  8075.    an address in thumb code, and set a "special" bit in a minimal
  8076.    symbol to indicate that it does.  */

  8077. static void
  8078. arm_elf_make_msymbol_special(asymbol *sym, struct minimal_symbol *msym)
  8079. {
  8080.   if (ARM_SYM_BRANCH_TYPE (&((elf_symbol_type *)sym)->internal_elf_sym)
  8081.       == ST_BRANCH_TO_THUMB)
  8082.     MSYMBOL_SET_SPECIAL (msym);
  8083. }

  8084. static void
  8085. arm_coff_make_msymbol_special(int val, struct minimal_symbol *msym)
  8086. {
  8087.   if (coff_sym_is_thumb (val))
  8088.     MSYMBOL_SET_SPECIAL (msym);
  8089. }

  8090. static void
  8091. arm_objfile_data_free (struct objfile *objfile, void *arg)
  8092. {
  8093.   struct arm_per_objfile *data = arg;
  8094.   unsigned int i;

  8095.   for (i = 0; i < objfile->obfd->section_count; i++)
  8096.     VEC_free (arm_mapping_symbol_s, data->section_maps[i]);
  8097. }

  8098. static void
  8099. arm_record_special_symbol (struct gdbarch *gdbarch, struct objfile *objfile,
  8100.                            asymbol *sym)
  8101. {
  8102.   const char *name = bfd_asymbol_name (sym);
  8103.   struct arm_per_objfile *data;
  8104.   VEC(arm_mapping_symbol_s) **map_p;
  8105.   struct arm_mapping_symbol new_map_sym;

  8106.   gdb_assert (name[0] == '$');
  8107.   if (name[1] != 'a' && name[1] != 't' && name[1] != 'd')
  8108.     return;

  8109.   data = objfile_data (objfile, arm_objfile_data_key);
  8110.   if (data == NULL)
  8111.     {
  8112.       data = OBSTACK_ZALLOC (&objfile->objfile_obstack,
  8113.                              struct arm_per_objfile);
  8114.       set_objfile_data (objfile, arm_objfile_data_key, data);
  8115.       data->section_maps = OBSTACK_CALLOC (&objfile->objfile_obstack,
  8116.                                            objfile->obfd->section_count,
  8117.                                            VEC(arm_mapping_symbol_s) *);
  8118.     }
  8119.   map_p = &data->section_maps[bfd_get_section (sym)->index];

  8120.   new_map_sym.value = sym->value;
  8121.   new_map_sym.type = name[1];

  8122.   /* Assume that most mapping symbols appear in order of increasing
  8123.      value.  If they were randomly distributed, it would be faster to
  8124.      always push here and then sort at first use.  */
  8125.   if (!VEC_empty (arm_mapping_symbol_s, *map_p))
  8126.     {
  8127.       struct arm_mapping_symbol *prev_map_sym;

  8128.       prev_map_sym = VEC_last (arm_mapping_symbol_s, *map_p);
  8129.       if (prev_map_sym->value >= sym->value)
  8130.         {
  8131.           unsigned int idx;
  8132.           idx = VEC_lower_bound (arm_mapping_symbol_s, *map_p, &new_map_sym,
  8133.                                  arm_compare_mapping_symbols);
  8134.           VEC_safe_insert (arm_mapping_symbol_s, *map_p, idx, &new_map_sym);
  8135.           return;
  8136.         }
  8137.     }

  8138.   VEC_safe_push (arm_mapping_symbol_s, *map_p, &new_map_sym);
  8139. }

  8140. static void
  8141. arm_write_pc (struct regcache *regcache, CORE_ADDR pc)
  8142. {
  8143.   struct gdbarch *gdbarch = get_regcache_arch (regcache);
  8144.   regcache_cooked_write_unsigned (regcache, ARM_PC_REGNUM, pc);

  8145.   /* If necessary, set the T bit.  */
  8146.   if (arm_apcs_32)
  8147.     {
  8148.       ULONGEST val, t_bit;
  8149.       regcache_cooked_read_unsigned (regcache, ARM_PS_REGNUM, &val);
  8150.       t_bit = arm_psr_thumb_bit (gdbarch);
  8151.       if (arm_pc_is_thumb (gdbarch, pc))
  8152.         regcache_cooked_write_unsigned (regcache, ARM_PS_REGNUM,
  8153.                                         val | t_bit);
  8154.       else
  8155.         regcache_cooked_write_unsigned (regcache, ARM_PS_REGNUM,
  8156.                                         val & ~t_bit);
  8157.     }
  8158. }

  8159. /* Read the contents of a NEON quad register, by reading from two
  8160.    double registers.  This is used to implement the quad pseudo
  8161.    registers, and for argument passing in case the quad registers are
  8162.    missing; vectors are passed in quad registers when using the VFP
  8163.    ABI, even if a NEON unit is not present.  REGNUM is the index of
  8164.    the quad register, in [0, 15].  */

  8165. static enum register_status
  8166. arm_neon_quad_read (struct gdbarch *gdbarch, struct regcache *regcache,
  8167.                     int regnum, gdb_byte *buf)
  8168. {
  8169.   char name_buf[4];
  8170.   gdb_byte reg_buf[8];
  8171.   int offset, double_regnum;
  8172.   enum register_status status;

  8173.   xsnprintf (name_buf, sizeof (name_buf), "d%d", regnum << 1);
  8174.   double_regnum = user_reg_map_name_to_regnum (gdbarch, name_buf,
  8175.                                                strlen (name_buf));

  8176.   /* d0 is always the least significant half of q0.  */
  8177.   if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
  8178.     offset = 8;
  8179.   else
  8180.     offset = 0;

  8181.   status = regcache_raw_read (regcache, double_regnum, reg_buf);
  8182.   if (status != REG_VALID)
  8183.     return status;
  8184.   memcpy (buf + offset, reg_buf, 8);

  8185.   offset = 8 - offset;
  8186.   status = regcache_raw_read (regcache, double_regnum + 1, reg_buf);
  8187.   if (status != REG_VALID)
  8188.     return status;
  8189.   memcpy (buf + offset, reg_buf, 8);

  8190.   return REG_VALID;
  8191. }

  8192. static enum register_status
  8193. arm_pseudo_read (struct gdbarch *gdbarch, struct regcache *regcache,
  8194.                  int regnum, gdb_byte *buf)
  8195. {
  8196.   const int num_regs = gdbarch_num_regs (gdbarch);
  8197.   char name_buf[4];
  8198.   gdb_byte reg_buf[8];
  8199.   int offset, double_regnum;

  8200.   gdb_assert (regnum >= num_regs);
  8201.   regnum -= num_regs;

  8202.   if (gdbarch_tdep (gdbarch)->have_neon_pseudos && regnum >= 32 && regnum < 48)
  8203.     /* Quad-precision register.  */
  8204.     return arm_neon_quad_read (gdbarch, regcache, regnum - 32, buf);
  8205.   else
  8206.     {
  8207.       enum register_status status;

  8208.       /* Single-precision register.  */
  8209.       gdb_assert (regnum < 32);

  8210.       /* s0 is always the least significant half of d0.  */
  8211.       if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
  8212.         offset = (regnum & 1) ? 0 : 4;
  8213.       else
  8214.         offset = (regnum & 1) ? 4 : 0;

  8215.       xsnprintf (name_buf, sizeof (name_buf), "d%d", regnum >> 1);
  8216.       double_regnum = user_reg_map_name_to_regnum (gdbarch, name_buf,
  8217.                                                    strlen (name_buf));

  8218.       status = regcache_raw_read (regcache, double_regnum, reg_buf);
  8219.       if (status == REG_VALID)
  8220.         memcpy (buf, reg_buf + offset, 4);
  8221.       return status;
  8222.     }
  8223. }

  8224. /* Store the contents of BUF to a NEON quad register, by writing to
  8225.    two double registers.  This is used to implement the quad pseudo
  8226.    registers, and for argument passing in case the quad registers are
  8227.    missing; vectors are passed in quad registers when using the VFP
  8228.    ABI, even if a NEON unit is not present.  REGNUM is the index
  8229.    of the quad register, in [0, 15].  */

  8230. static void
  8231. arm_neon_quad_write (struct gdbarch *gdbarch, struct regcache *regcache,
  8232.                      int regnum, const gdb_byte *buf)
  8233. {
  8234.   char name_buf[4];
  8235.   int offset, double_regnum;

  8236.   xsnprintf (name_buf, sizeof (name_buf), "d%d", regnum << 1);
  8237.   double_regnum = user_reg_map_name_to_regnum (gdbarch, name_buf,
  8238.                                                strlen (name_buf));

  8239.   /* d0 is always the least significant half of q0.  */
  8240.   if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
  8241.     offset = 8;
  8242.   else
  8243.     offset = 0;

  8244.   regcache_raw_write (regcache, double_regnum, buf + offset);
  8245.   offset = 8 - offset;
  8246.   regcache_raw_write (regcache, double_regnum + 1, buf + offset);
  8247. }

  8248. static void
  8249. arm_pseudo_write (struct gdbarch *gdbarch, struct regcache *regcache,
  8250.                   int regnum, const gdb_byte *buf)
  8251. {
  8252.   const int num_regs = gdbarch_num_regs (gdbarch);
  8253.   char name_buf[4];
  8254.   gdb_byte reg_buf[8];
  8255.   int offset, double_regnum;

  8256.   gdb_assert (regnum >= num_regs);
  8257.   regnum -= num_regs;

  8258.   if (gdbarch_tdep (gdbarch)->have_neon_pseudos && regnum >= 32 && regnum < 48)
  8259.     /* Quad-precision register.  */
  8260.     arm_neon_quad_write (gdbarch, regcache, regnum - 32, buf);
  8261.   else
  8262.     {
  8263.       /* Single-precision register.  */
  8264.       gdb_assert (regnum < 32);

  8265.       /* s0 is always the least significant half of d0.  */
  8266.       if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
  8267.         offset = (regnum & 1) ? 0 : 4;
  8268.       else
  8269.         offset = (regnum & 1) ? 4 : 0;

  8270.       xsnprintf (name_buf, sizeof (name_buf), "d%d", regnum >> 1);
  8271.       double_regnum = user_reg_map_name_to_regnum (gdbarch, name_buf,
  8272.                                                    strlen (name_buf));

  8273.       regcache_raw_read (regcache, double_regnum, reg_buf);
  8274.       memcpy (reg_buf + offset, buf, 4);
  8275.       regcache_raw_write (regcache, double_regnum, reg_buf);
  8276.     }
  8277. }

  8278. static struct value *
  8279. value_of_arm_user_reg (struct frame_info *frame, const void *baton)
  8280. {
  8281.   const int *reg_p = baton;
  8282.   return value_of_register (*reg_p, frame);
  8283. }

  8284. static enum gdb_osabi
  8285. arm_elf_osabi_sniffer (bfd *abfd)
  8286. {
  8287.   unsigned int elfosabi;
  8288.   enum gdb_osabi osabi = GDB_OSABI_UNKNOWN;

  8289.   elfosabi = elf_elfheader (abfd)->e_ident[EI_OSABI];

  8290.   if (elfosabi == ELFOSABI_ARM)
  8291.     /* GNU tools use this value.  Check note sections in this case,
  8292.        as well.  */
  8293.     bfd_map_over_sections (abfd,
  8294.                            generic_elf_osabi_sniff_abi_tag_sections,
  8295.                            &osabi);

  8296.   /* Anything else will be handled by the generic ELF sniffer.  */
  8297.   return osabi;
  8298. }

  8299. static int
  8300. arm_register_reggroup_p (struct gdbarch *gdbarch, int regnum,
  8301.                           struct reggroup *group)
  8302. {
  8303.   /* FPS register's type is INT, but belongs to float_reggroup.  Beside
  8304.      this, FPS register belongs to save_regroup, restore_reggroup, and
  8305.      all_reggroup, of course.  */
  8306.   if (regnum == ARM_FPS_REGNUM)
  8307.     return (group == float_reggroup
  8308.             || group == save_reggroup
  8309.             || group == restore_reggroup
  8310.             || group == all_reggroup);
  8311.   else
  8312.     return default_register_reggroup_p (gdbarch, regnum, group);
  8313. }


  8314. /* For backward-compatibility we allow two 'g' packet lengths with
  8315.    the remote protocol depending on whether FPA registers are
  8316.    supplied.  M-profile targets do not have FPA registers, but some
  8317.    stubs already exist in the wild which use a 'g' packet which
  8318.    supplies them albeit with dummy values.  The packet format which
  8319.    includes FPA registers should be considered deprecated for
  8320.    M-profile targets.  */

  8321. static void
  8322. arm_register_g_packet_guesses (struct gdbarch *gdbarch)
  8323. {
  8324.   if (gdbarch_tdep (gdbarch)->is_m)
  8325.     {
  8326.       /* If we know from the executable this is an M-profile target,
  8327.          cater for remote targets whose register set layout is the
  8328.          same as the FPA layout.  */
  8329.       register_remote_g_packet_guess (gdbarch,
  8330.                                       /* r0-r12,sp,lr,pc; f0-f7; fps,xpsr */
  8331.                                       (16 * INT_REGISTER_SIZE)
  8332.                                       + (8 * FP_REGISTER_SIZE)
  8333.                                       + (2 * INT_REGISTER_SIZE),
  8334.                                       tdesc_arm_with_m_fpa_layout);

  8335.       /* The regular M-profile layout.  */
  8336.       register_remote_g_packet_guess (gdbarch,
  8337.                                       /* r0-r12,sp,lr,pc; xpsr */
  8338.                                       (16 * INT_REGISTER_SIZE)
  8339.                                       + INT_REGISTER_SIZE,
  8340.                                       tdesc_arm_with_m);

  8341.       /* M-profile plus M4F VFP.  */
  8342.       register_remote_g_packet_guess (gdbarch,
  8343.                                       /* r0-r12,sp,lr,pc; d0-d15; fpscr,xpsr */
  8344.                                       (16 * INT_REGISTER_SIZE)
  8345.                                       + (16 * VFP_REGISTER_SIZE)
  8346.                                       + (2 * INT_REGISTER_SIZE),
  8347.                                       tdesc_arm_with_m_vfp_d16);
  8348.     }

  8349.   /* Otherwise we don't have a useful guess.  */
  8350. }


  8351. /* Initialize the current architecture based on INFO.  If possible,
  8352.    re-use an architecture from ARCHES, which is a list of
  8353.    architectures already created during this debugging session.

  8354.    Called e.g. at program startup, when reading a core file, and when
  8355.    reading a binary file.  */

  8356. static struct gdbarch *
  8357. arm_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
  8358. {
  8359.   struct gdbarch_tdep *tdep;
  8360.   struct gdbarch *gdbarch;
  8361.   struct gdbarch_list *best_arch;
  8362.   enum arm_abi_kind arm_abi = arm_abi_global;
  8363.   enum arm_float_model fp_model = arm_fp_model;
  8364.   struct tdesc_arch_data *tdesc_data = NULL;
  8365.   int i, is_m = 0;
  8366.   int have_vfp_registers = 0, have_vfp_pseudos = 0, have_neon_pseudos = 0;
  8367.   int have_neon = 0;
  8368.   int have_fpa_registers = 1;
  8369.   const struct target_desc *tdesc = info.target_desc;

  8370.   /* If we have an object to base this architecture on, try to determine
  8371.      its ABI.  */

  8372.   if (arm_abi == ARM_ABI_AUTO && info.abfd != NULL)
  8373.     {
  8374.       int ei_osabi, e_flags;

  8375.       switch (bfd_get_flavour (info.abfd))
  8376.         {
  8377.         case bfd_target_aout_flavour:
  8378.           /* Assume it's an old APCS-style ABI.  */
  8379.           arm_abi = ARM_ABI_APCS;
  8380.           break;

  8381.         case bfd_target_coff_flavour:
  8382.           /* Assume it's an old APCS-style ABI.  */
  8383.           /* XXX WinCE?  */
  8384.           arm_abi = ARM_ABI_APCS;
  8385.           break;

  8386.         case bfd_target_elf_flavour:
  8387.           ei_osabi = elf_elfheader (info.abfd)->e_ident[EI_OSABI];
  8388.           e_flags = elf_elfheader (info.abfd)->e_flags;

  8389.           if (ei_osabi == ELFOSABI_ARM)
  8390.             {
  8391.               /* GNU tools used to use this value, but do not for EABI
  8392.                  objects.  There's nowhere to tag an EABI version
  8393.                  anyway, so assume APCS.  */
  8394.               arm_abi = ARM_ABI_APCS;
  8395.             }
  8396.           else if (ei_osabi == ELFOSABI_NONE)
  8397.             {
  8398.               int eabi_ver = EF_ARM_EABI_VERSION (e_flags);
  8399.               int attr_arch, attr_profile;

  8400.               switch (eabi_ver)
  8401.                 {
  8402.                 case EF_ARM_EABI_UNKNOWN:
  8403.                   /* Assume GNU tools.  */
  8404.                   arm_abi = ARM_ABI_APCS;
  8405.                   break;

  8406.                 case EF_ARM_EABI_VER4:
  8407.                 case EF_ARM_EABI_VER5:
  8408.                   arm_abi = ARM_ABI_AAPCS;
  8409.                   /* EABI binaries default to VFP float ordering.
  8410.                      They may also contain build attributes that can
  8411.                      be used to identify if the VFP argument-passing
  8412.                      ABI is in use.  */
  8413.                   if (fp_model == ARM_FLOAT_AUTO)
  8414.                     {
  8415. #ifdef HAVE_ELF
  8416.                       switch (bfd_elf_get_obj_attr_int (info.abfd,
  8417.                                                         OBJ_ATTR_PROC,
  8418.                                                         Tag_ABI_VFP_args))
  8419.                         {
  8420.                         case AEABI_VFP_args_base:
  8421.                           /* "The user intended FP parameter/result
  8422.                              passing to conform to AAPCS, base
  8423.                              variant".  */
  8424.                           fp_model = ARM_FLOAT_SOFT_VFP;
  8425.                           break;
  8426.                         case AEABI_VFP_args_vfp:
  8427.                           /* "The user intended FP parameter/result
  8428.                              passing to conform to AAPCS, VFP
  8429.                              variant".  */
  8430.                           fp_model = ARM_FLOAT_VFP;
  8431.                           break;
  8432.                         case AEABI_VFP_args_toolchain:
  8433.                           /* "The user intended FP parameter/result
  8434.                              passing to conform to tool chain-specific
  8435.                              conventions" - we don't know any such
  8436.                              conventions, so leave it as "auto".  */
  8437.                           break;
  8438.                         case AEABI_VFP_args_compatible:
  8439.                           /* "Code is compatible with both the base
  8440.                              and VFP variants; the user did not permit
  8441.                              non-variadic functions to pass FP
  8442.                              parameters/results" - leave it as
  8443.                              "auto".  */
  8444.                           break;
  8445.                         default:
  8446.                           /* Attribute value not mentioned in the
  8447.                              November 2012 ABI, so leave it as
  8448.                              "auto".  */
  8449.                           break;
  8450.                         }
  8451. #else
  8452.                       fp_model = ARM_FLOAT_SOFT_VFP;
  8453. #endif
  8454.                     }
  8455.                   break;

  8456.                 default:
  8457.                   /* Leave it as "auto".  */
  8458.                   warning (_("unknown ARM EABI version 0x%x"), eabi_ver);
  8459.                   break;
  8460.                 }

  8461. #ifdef HAVE_ELF
  8462.               /* Detect M-profile programs.  This only works if the
  8463.                  executable file includes build attributes; GCC does
  8464.                  copy them to the executable, but e.g. RealView does
  8465.                  not.  */
  8466.               attr_arch = bfd_elf_get_obj_attr_int (info.abfd, OBJ_ATTR_PROC,
  8467.                                                     Tag_CPU_arch);
  8468.               attr_profile = bfd_elf_get_obj_attr_int (info.abfd,
  8469.                                                        OBJ_ATTR_PROC,
  8470.                                                        Tag_CPU_arch_profile);
  8471.               /* GCC specifies the profile for v6-M; RealView only
  8472.                  specifies the profile for architectures starting with
  8473.                  V7 (as opposed to architectures with a tag
  8474.                  numerically greater than TAG_CPU_ARCH_V7).  */
  8475.               if (!tdesc_has_registers (tdesc)
  8476.                   && (attr_arch == TAG_CPU_ARCH_V6_M
  8477.                       || attr_arch == TAG_CPU_ARCH_V6S_M
  8478.                       || attr_profile == 'M'))
  8479.                 is_m = 1;
  8480. #endif
  8481.             }

  8482.           if (fp_model == ARM_FLOAT_AUTO)
  8483.             {
  8484.               int e_flags = elf_elfheader (info.abfd)->e_flags;

  8485.               switch (e_flags & (EF_ARM_SOFT_FLOAT | EF_ARM_VFP_FLOAT))
  8486.                 {
  8487.                 case 0:
  8488.                   /* Leave it as "auto".  Strictly speaking this case
  8489.                      means FPA, but almost nobody uses that now, and
  8490.                      many toolchains fail to set the appropriate bits
  8491.                      for the floating-point model they use.  */
  8492.                   break;
  8493.                 case EF_ARM_SOFT_FLOAT:
  8494.                   fp_model = ARM_FLOAT_SOFT_FPA;
  8495.                   break;
  8496.                 case EF_ARM_VFP_FLOAT:
  8497.                   fp_model = ARM_FLOAT_VFP;
  8498.                   break;
  8499.                 case EF_ARM_SOFT_FLOAT | EF_ARM_VFP_FLOAT:
  8500.                   fp_model = ARM_FLOAT_SOFT_VFP;
  8501.                   break;
  8502.                 }
  8503.             }

  8504.           if (e_flags & EF_ARM_BE8)
  8505.             info.byte_order_for_code = BFD_ENDIAN_LITTLE;

  8506.           break;

  8507.         default:
  8508.           /* Leave it as "auto".  */
  8509.           break;
  8510.         }
  8511.     }

  8512.   /* Check any target description for validity.  */
  8513.   if (tdesc_has_registers (tdesc))
  8514.     {
  8515.       /* For most registers we require GDB's default names; but also allow
  8516.          the numeric names for sp / lr / pc, as a convenience.  */
  8517.       static const char *const arm_sp_names[] = { "r13", "sp", NULL };
  8518.       static const char *const arm_lr_names[] = { "r14", "lr", NULL };
  8519.       static const char *const arm_pc_names[] = { "r15", "pc", NULL };

  8520.       const struct tdesc_feature *feature;
  8521.       int valid_p;

  8522.       feature = tdesc_find_feature (tdesc,
  8523.                                     "org.gnu.gdb.arm.core");
  8524.       if (feature == NULL)
  8525.         {
  8526.           feature = tdesc_find_feature (tdesc,
  8527.                                         "org.gnu.gdb.arm.m-profile");
  8528.           if (feature == NULL)
  8529.             return NULL;
  8530.           else
  8531.             is_m = 1;
  8532.         }

  8533.       tdesc_data = tdesc_data_alloc ();

  8534.       valid_p = 1;
  8535.       for (i = 0; i < ARM_SP_REGNUM; i++)
  8536.         valid_p &= tdesc_numbered_register (feature, tdesc_data, i,
  8537.                                             arm_register_names[i]);
  8538.       valid_p &= tdesc_numbered_register_choices (feature, tdesc_data,
  8539.                                                   ARM_SP_REGNUM,
  8540.                                                   arm_sp_names);
  8541.       valid_p &= tdesc_numbered_register_choices (feature, tdesc_data,
  8542.                                                   ARM_LR_REGNUM,
  8543.                                                   arm_lr_names);
  8544.       valid_p &= tdesc_numbered_register_choices (feature, tdesc_data,
  8545.                                                   ARM_PC_REGNUM,
  8546.                                                   arm_pc_names);
  8547.       if (is_m)
  8548.         valid_p &= tdesc_numbered_register (feature, tdesc_data,
  8549.                                             ARM_PS_REGNUM, "xpsr");
  8550.       else
  8551.         valid_p &= tdesc_numbered_register (feature, tdesc_data,
  8552.                                             ARM_PS_REGNUM, "cpsr");

  8553.       if (!valid_p)
  8554.         {
  8555.           tdesc_data_cleanup (tdesc_data);
  8556.           return NULL;
  8557.         }

  8558.       feature = tdesc_find_feature (tdesc,
  8559.                                     "org.gnu.gdb.arm.fpa");
  8560.       if (feature != NULL)
  8561.         {
  8562.           valid_p = 1;
  8563.           for (i = ARM_F0_REGNUM; i <= ARM_FPS_REGNUM; i++)
  8564.             valid_p &= tdesc_numbered_register (feature, tdesc_data, i,
  8565.                                                 arm_register_names[i]);
  8566.           if (!valid_p)
  8567.             {
  8568.               tdesc_data_cleanup (tdesc_data);
  8569.               return NULL;
  8570.             }
  8571.         }
  8572.       else
  8573.         have_fpa_registers = 0;

  8574.       feature = tdesc_find_feature (tdesc,
  8575.                                     "org.gnu.gdb.xscale.iwmmxt");
  8576.       if (feature != NULL)
  8577.         {
  8578.           static const char *const iwmmxt_names[] = {
  8579.             "wR0", "wR1", "wR2", "wR3", "wR4", "wR5", "wR6", "wR7",
  8580.             "wR8", "wR9", "wR10", "wR11", "wR12", "wR13", "wR14", "wR15",
  8581.             "wCID", "wCon", "wCSSF", "wCASF", "", "", "", "",
  8582.             "wCGR0", "wCGR1", "wCGR2", "wCGR3", "", "", "", "",
  8583.           };

  8584.           valid_p = 1;
  8585.           for (i = ARM_WR0_REGNUM; i <= ARM_WR15_REGNUM; i++)
  8586.             valid_p
  8587.               &= tdesc_numbered_register (feature, tdesc_data, i,
  8588.                                           iwmmxt_names[i - ARM_WR0_REGNUM]);

  8589.           /* Check for the control registers, but do not fail if they
  8590.              are missing.  */
  8591.           for (i = ARM_WC0_REGNUM; i <= ARM_WCASF_REGNUM; i++)
  8592.             tdesc_numbered_register (feature, tdesc_data, i,
  8593.                                      iwmmxt_names[i - ARM_WR0_REGNUM]);

  8594.           for (i = ARM_WCGR0_REGNUM; i <= ARM_WCGR3_REGNUM; i++)
  8595.             valid_p
  8596.               &= tdesc_numbered_register (feature, tdesc_data, i,
  8597.                                           iwmmxt_names[i - ARM_WR0_REGNUM]);

  8598.           if (!valid_p)
  8599.             {
  8600.               tdesc_data_cleanup (tdesc_data);
  8601.               return NULL;
  8602.             }
  8603.         }

  8604.       /* If we have a VFP unit, check whether the single precision registers
  8605.          are present.  If not, then we will synthesize them as pseudo
  8606.          registers.  */
  8607.       feature = tdesc_find_feature (tdesc,
  8608.                                     "org.gnu.gdb.arm.vfp");
  8609.       if (feature != NULL)
  8610.         {
  8611.           static const char *const vfp_double_names[] = {
  8612.             "d0", "d1", "d2", "d3", "d4", "d5", "d6", "d7",
  8613.             "d8", "d9", "d10", "d11", "d12", "d13", "d14", "d15",
  8614.             "d16", "d17", "d18", "d19", "d20", "d21", "d22", "d23",
  8615.             "d24", "d25", "d26", "d27", "d28", "d29", "d30", "d31",
  8616.           };

  8617.           /* Require the double precision registers.  There must be either
  8618.              16 or 32.  */
  8619.           valid_p = 1;
  8620.           for (i = 0; i < 32; i++)
  8621.             {
  8622.               valid_p &= tdesc_numbered_register (feature, tdesc_data,
  8623.                                                   ARM_D0_REGNUM + i,
  8624.                                                   vfp_double_names[i]);
  8625.               if (!valid_p)
  8626.                 break;
  8627.             }
  8628.           if (!valid_p && i == 16)
  8629.             valid_p = 1;

  8630.           /* Also require FPSCR.  */
  8631.           valid_p &= tdesc_numbered_register (feature, tdesc_data,
  8632.                                               ARM_FPSCR_REGNUM, "fpscr");
  8633.           if (!valid_p)
  8634.             {
  8635.               tdesc_data_cleanup (tdesc_data);
  8636.               return NULL;
  8637.             }

  8638.           if (tdesc_unnumbered_register (feature, "s0") == 0)
  8639.             have_vfp_pseudos = 1;

  8640.           have_vfp_registers = 1;

  8641.           /* If we have VFP, also check for NEON.  The architecture allows
  8642.              NEON without VFP (integer vector operations only), but GDB
  8643.              does not support that.  */
  8644.           feature = tdesc_find_feature (tdesc,
  8645.                                         "org.gnu.gdb.arm.neon");
  8646.           if (feature != NULL)
  8647.             {
  8648.               /* NEON requires 32 double-precision registers.  */
  8649.               if (i != 32)
  8650.                 {
  8651.                   tdesc_data_cleanup (tdesc_data);
  8652.                   return NULL;
  8653.                 }

  8654.               /* If there are quad registers defined by the stub, use
  8655.                  their type; otherwise (normally) provide them with
  8656.                  the default type.  */
  8657.               if (tdesc_unnumbered_register (feature, "q0") == 0)
  8658.                 have_neon_pseudos = 1;

  8659.               have_neon = 1;
  8660.             }
  8661.         }
  8662.     }

  8663.   /* If there is already a candidate, use it.  */
  8664.   for (best_arch = gdbarch_list_lookup_by_info (arches, &info);
  8665.        best_arch != NULL;
  8666.        best_arch = gdbarch_list_lookup_by_info (best_arch->next, &info))
  8667.     {
  8668.       if (arm_abi != ARM_ABI_AUTO
  8669.           && arm_abi != gdbarch_tdep (best_arch->gdbarch)->arm_abi)
  8670.         continue;

  8671.       if (fp_model != ARM_FLOAT_AUTO
  8672.           && fp_model != gdbarch_tdep (best_arch->gdbarch)->fp_model)
  8673.         continue;

  8674.       /* There are various other properties in tdep that we do not
  8675.          need to check here: those derived from a target description,
  8676.          since gdbarches with a different target description are
  8677.          automatically disqualified.  */

  8678.       /* Do check is_m, though, since it might come from the binary.  */
  8679.       if (is_m != gdbarch_tdep (best_arch->gdbarch)->is_m)
  8680.         continue;

  8681.       /* Found a match.  */
  8682.       break;
  8683.     }

  8684.   if (best_arch != NULL)
  8685.     {
  8686.       if (tdesc_data != NULL)
  8687.         tdesc_data_cleanup (tdesc_data);
  8688.       return best_arch->gdbarch;
  8689.     }

  8690.   tdep = xcalloc (1, sizeof (struct gdbarch_tdep));
  8691.   gdbarch = gdbarch_alloc (&info, tdep);

  8692.   /* Record additional information about the architecture we are defining.
  8693.      These are gdbarch discriminators, like the OSABI.  */
  8694.   tdep->arm_abi = arm_abi;
  8695.   tdep->fp_model = fp_model;
  8696.   tdep->is_m = is_m;
  8697.   tdep->have_fpa_registers = have_fpa_registers;
  8698.   tdep->have_vfp_registers = have_vfp_registers;
  8699.   tdep->have_vfp_pseudos = have_vfp_pseudos;
  8700.   tdep->have_neon_pseudos = have_neon_pseudos;
  8701.   tdep->have_neon = have_neon;

  8702.   arm_register_g_packet_guesses (gdbarch);

  8703.   /* Breakpoints.  */
  8704.   switch (info.byte_order_for_code)
  8705.     {
  8706.     case BFD_ENDIAN_BIG:
  8707.       tdep->arm_breakpoint = arm_default_arm_be_breakpoint;
  8708.       tdep->arm_breakpoint_size = sizeof (arm_default_arm_be_breakpoint);
  8709.       tdep->thumb_breakpoint = arm_default_thumb_be_breakpoint;
  8710.       tdep->thumb_breakpoint_size = sizeof (arm_default_thumb_be_breakpoint);

  8711.       break;

  8712.     case BFD_ENDIAN_LITTLE:
  8713.       tdep->arm_breakpoint = arm_default_arm_le_breakpoint;
  8714.       tdep->arm_breakpoint_size = sizeof (arm_default_arm_le_breakpoint);
  8715.       tdep->thumb_breakpoint = arm_default_thumb_le_breakpoint;
  8716.       tdep->thumb_breakpoint_size = sizeof (arm_default_thumb_le_breakpoint);

  8717.       break;

  8718.     default:
  8719.       internal_error (__FILE__, __LINE__,
  8720.                       _("arm_gdbarch_init: bad byte order for float format"));
  8721.     }

  8722.   /* On ARM targets char defaults to unsigned.  */
  8723.   set_gdbarch_char_signed (gdbarch, 0);

  8724.   /* Note: for displaced stepping, this includes the breakpoint, and one word
  8725.      of additional scratch space.  This setting isn't used for anything beside
  8726.      displaced stepping at present.  */
  8727.   set_gdbarch_max_insn_length (gdbarch, 4 * DISPLACED_MODIFIED_INSNS);

  8728.   /* This should be low enough for everything.  */
  8729.   tdep->lowest_pc = 0x20;
  8730.   tdep->jb_pc = -1;        /* Longjump support not enabled by default.  */

  8731.   /* The default, for both APCS and AAPCS, is to return small
  8732.      structures in registers.  */
  8733.   tdep->struct_return = reg_struct_return;

  8734.   set_gdbarch_push_dummy_call (gdbarch, arm_push_dummy_call);
  8735.   set_gdbarch_frame_align (gdbarch, arm_frame_align);

  8736.   set_gdbarch_write_pc (gdbarch, arm_write_pc);

  8737.   /* Frame handling.  */
  8738.   set_gdbarch_dummy_id (gdbarch, arm_dummy_id);
  8739.   set_gdbarch_unwind_pc (gdbarch, arm_unwind_pc);
  8740.   set_gdbarch_unwind_sp (gdbarch, arm_unwind_sp);

  8741.   frame_base_set_default (gdbarch, &arm_normal_base);

  8742.   /* Address manipulation.  */
  8743.   set_gdbarch_addr_bits_remove (gdbarch, arm_addr_bits_remove);

  8744.   /* Advance PC across function entry code.  */
  8745.   set_gdbarch_skip_prologue (gdbarch, arm_skip_prologue);

  8746.   /* Detect whether PC is in function epilogue.  */
  8747.   set_gdbarch_in_function_epilogue_p (gdbarch, arm_in_function_epilogue_p);

  8748.   /* Skip trampolines.  */
  8749.   set_gdbarch_skip_trampoline_code (gdbarch, arm_skip_stub);

  8750.   /* The stack grows downward.  */
  8751.   set_gdbarch_inner_than (gdbarch, core_addr_lessthan);

  8752.   /* Breakpoint manipulation.  */
  8753.   set_gdbarch_breakpoint_from_pc (gdbarch, arm_breakpoint_from_pc);
  8754.   set_gdbarch_remote_breakpoint_from_pc (gdbarch,
  8755.                                          arm_remote_breakpoint_from_pc);

  8756.   /* Information about registers, etc.  */
  8757.   set_gdbarch_sp_regnum (gdbarch, ARM_SP_REGNUM);
  8758.   set_gdbarch_pc_regnum (gdbarch, ARM_PC_REGNUM);
  8759.   set_gdbarch_num_regs (gdbarch, ARM_NUM_REGS);
  8760.   set_gdbarch_register_type (gdbarch, arm_register_type);
  8761.   set_gdbarch_register_reggroup_p (gdbarch, arm_register_reggroup_p);

  8762.   /* This "info float" is FPA-specific.  Use the generic version if we
  8763.      do not have FPA.  */
  8764.   if (gdbarch_tdep (gdbarch)->have_fpa_registers)
  8765.     set_gdbarch_print_float_info (gdbarch, arm_print_float_info);

  8766.   /* Internal <-> external register number maps.  */
  8767.   set_gdbarch_dwarf2_reg_to_regnum (gdbarch, arm_dwarf_reg_to_regnum);
  8768.   set_gdbarch_register_sim_regno (gdbarch, arm_register_sim_regno);

  8769.   set_gdbarch_register_name (gdbarch, arm_register_name);

  8770.   /* Returning results.  */
  8771.   set_gdbarch_return_value (gdbarch, arm_return_value);

  8772.   /* Disassembly.  */
  8773.   set_gdbarch_print_insn (gdbarch, gdb_print_insn_arm);

  8774.   /* Minsymbol frobbing.  */
  8775.   set_gdbarch_elf_make_msymbol_special (gdbarch, arm_elf_make_msymbol_special);
  8776.   set_gdbarch_coff_make_msymbol_special (gdbarch,
  8777.                                          arm_coff_make_msymbol_special);
  8778.   set_gdbarch_record_special_symbol (gdbarch, arm_record_special_symbol);

  8779.   /* Thumb-2 IT block support.  */
  8780.   set_gdbarch_adjust_breakpoint_address (gdbarch,
  8781.                                          arm_adjust_breakpoint_address);

  8782.   /* Virtual tables.  */
  8783.   set_gdbarch_vbit_in_delta (gdbarch, 1);

  8784.   /* Hook in the ABI-specific overrides, if they have been registered.  */
  8785.   gdbarch_init_osabi (info, gdbarch);

  8786.   dwarf2_frame_set_init_reg (gdbarch, arm_dwarf2_frame_init_reg);

  8787.   /* Add some default predicates.  */
  8788.   if (is_m)
  8789.     frame_unwind_append_unwinder (gdbarch, &arm_m_exception_unwind);
  8790.   frame_unwind_append_unwinder (gdbarch, &arm_stub_unwind);
  8791.   dwarf2_append_unwinders (gdbarch);
  8792.   frame_unwind_append_unwinder (gdbarch, &arm_exidx_unwind);
  8793.   frame_unwind_append_unwinder (gdbarch, &arm_prologue_unwind);

  8794.   /* Now we have tuned the configuration, set a few final things,
  8795.      based on what the OS ABI has told us.  */

  8796.   /* If the ABI is not otherwise marked, assume the old GNU APCS.  EABI
  8797.      binaries are always marked.  */
  8798.   if (tdep->arm_abi == ARM_ABI_AUTO)
  8799.     tdep->arm_abi = ARM_ABI_APCS;

  8800.   /* Watchpoints are not steppable.  */
  8801.   set_gdbarch_have_nonsteppable_watchpoint (gdbarch, 1);

  8802.   /* We used to default to FPA for generic ARM, but almost nobody
  8803.      uses that now, and we now provide a way for the user to force
  8804.      the model.  So default to the most useful variant.  */
  8805.   if (tdep->fp_model == ARM_FLOAT_AUTO)
  8806.     tdep->fp_model = ARM_FLOAT_SOFT_FPA;

  8807.   if (tdep->jb_pc >= 0)
  8808.     set_gdbarch_get_longjmp_target (gdbarch, arm_get_longjmp_target);

  8809.   /* Floating point sizes and format.  */
  8810.   set_gdbarch_float_format (gdbarch, floatformats_ieee_single);
  8811.   if (tdep->fp_model == ARM_FLOAT_SOFT_FPA || tdep->fp_model == ARM_FLOAT_FPA)
  8812.     {
  8813.       set_gdbarch_double_format
  8814.         (gdbarch, floatformats_ieee_double_littlebyte_bigword);
  8815.       set_gdbarch_long_double_format
  8816.         (gdbarch, floatformats_ieee_double_littlebyte_bigword);
  8817.     }
  8818.   else
  8819.     {
  8820.       set_gdbarch_double_format (gdbarch, floatformats_ieee_double);
  8821.       set_gdbarch_long_double_format (gdbarch, floatformats_ieee_double);
  8822.     }

  8823.   if (have_vfp_pseudos)
  8824.     {
  8825.       /* NOTE: These are the only pseudo registers used by
  8826.          the ARM target at the moment.  If more are added, a
  8827.          little more care in numbering will be needed.  */

  8828.       int num_pseudos = 32;
  8829.       if (have_neon_pseudos)
  8830.         num_pseudos += 16;
  8831.       set_gdbarch_num_pseudo_regs (gdbarch, num_pseudos);
  8832.       set_gdbarch_pseudo_register_read (gdbarch, arm_pseudo_read);
  8833.       set_gdbarch_pseudo_register_write (gdbarch, arm_pseudo_write);
  8834.     }

  8835.   if (tdesc_data)
  8836.     {
  8837.       set_tdesc_pseudo_register_name (gdbarch, arm_register_name);

  8838.       tdesc_use_registers (gdbarch, tdesc, tdesc_data);

  8839.       /* Override tdesc_register_type to adjust the types of VFP
  8840.          registers for NEON.  */
  8841.       set_gdbarch_register_type (gdbarch, arm_register_type);
  8842.     }

  8843.   /* Add standard register aliases.  We add aliases even for those
  8844.      nanes which are used by the current architecture - it's simpler,
  8845.      and does no harm, since nothing ever lists user registers.  */
  8846.   for (i = 0; i < ARRAY_SIZE (arm_register_aliases); i++)
  8847.     user_reg_add (gdbarch, arm_register_aliases[i].name,
  8848.                   value_of_arm_user_reg, &arm_register_aliases[i].regnum);

  8849.   return gdbarch;
  8850. }

  8851. static void
  8852. arm_dump_tdep (struct gdbarch *gdbarch, struct ui_file *file)
  8853. {
  8854.   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);

  8855.   if (tdep == NULL)
  8856.     return;

  8857.   fprintf_unfiltered (file, _("arm_dump_tdep: Lowest pc = 0x%lx"),
  8858.                       (unsigned long) tdep->lowest_pc);
  8859. }

  8860. extern initialize_file_ftype _initialize_arm_tdep; /* -Wmissing-prototypes */

  8861. void
  8862. _initialize_arm_tdep (void)
  8863. {
  8864.   struct ui_file *stb;
  8865.   long length;
  8866.   struct cmd_list_element *new_set, *new_show;
  8867.   const char *setname;
  8868.   const char *setdesc;
  8869.   const char *const *regnames;
  8870.   int numregs, i, j;
  8871.   static char *helptext;
  8872.   char regdesc[1024], *rdptr = regdesc;
  8873.   size_t rest = sizeof (regdesc);

  8874.   gdbarch_register (bfd_arch_arm, arm_gdbarch_init, arm_dump_tdep);

  8875.   arm_objfile_data_key
  8876.     = register_objfile_data_with_cleanup (NULL, arm_objfile_data_free);

  8877.   /* Add ourselves to objfile event chain.  */
  8878.   observer_attach_new_objfile (arm_exidx_new_objfile);
  8879.   arm_exidx_data_key
  8880.     = register_objfile_data_with_cleanup (NULL, arm_exidx_data_free);

  8881.   /* Register an ELF OS ABI sniffer for ARM binaries.  */
  8882.   gdbarch_register_osabi_sniffer (bfd_arch_arm,
  8883.                                   bfd_target_elf_flavour,
  8884.                                   arm_elf_osabi_sniffer);

  8885.   /* Initialize the standard target descriptions.  */
  8886.   initialize_tdesc_arm_with_m ();
  8887.   initialize_tdesc_arm_with_m_fpa_layout ();
  8888.   initialize_tdesc_arm_with_m_vfp_d16 ();
  8889.   initialize_tdesc_arm_with_iwmmxt ();
  8890.   initialize_tdesc_arm_with_vfpv2 ();
  8891.   initialize_tdesc_arm_with_vfpv3 ();
  8892.   initialize_tdesc_arm_with_neon ();

  8893.   /* Get the number of possible sets of register names defined in opcodes.  */
  8894.   num_disassembly_options = get_arm_regname_num_options ();

  8895.   /* Add root prefix command for all "set arm"/"show arm" commands.  */
  8896.   add_prefix_cmd ("arm", no_class, set_arm_command,
  8897.                   _("Various ARM-specific commands."),
  8898.                   &setarmcmdlist, "set arm ", 0, &setlist);

  8899.   add_prefix_cmd ("arm", no_class, show_arm_command,
  8900.                   _("Various ARM-specific commands."),
  8901.                   &showarmcmdlist, "show arm ", 0, &showlist);

  8902.   /* Sync the opcode insn printer with our register viewer.  */
  8903.   parse_arm_disassembler_option ("reg-names-std");

  8904.   /* Initialize the array that will be passed to
  8905.      add_setshow_enum_cmd().  */
  8906.   valid_disassembly_styles
  8907.     = xmalloc ((num_disassembly_options + 1) * sizeof (char *));
  8908.   for (i = 0; i < num_disassembly_options; i++)
  8909.     {
  8910.       numregs = get_arm_regnames (i, &setname, &setdesc, &regnames);
  8911.       valid_disassembly_styles[i] = setname;
  8912.       length = snprintf (rdptr, rest, "%s - %s\n", setname, setdesc);
  8913.       rdptr += length;
  8914.       rest -= length;
  8915.       /* When we find the default names, tell the disassembler to use
  8916.          them.  */
  8917.       if (!strcmp (setname, "std"))
  8918.         {
  8919.           disassembly_style = setname;
  8920.           set_arm_regname_option (i);
  8921.         }
  8922.     }
  8923.   /* Mark the end of valid options.  */
  8924.   valid_disassembly_styles[num_disassembly_options] = NULL;

  8925.   /* Create the help text.  */
  8926.   stb = mem_fileopen ();
  8927.   fprintf_unfiltered (stb, "%s%s%s",
  8928.                       _("The valid values are:\n"),
  8929.                       regdesc,
  8930.                       _("The default is \"std\"."));
  8931.   helptext = ui_file_xstrdup (stb, NULL);
  8932.   ui_file_delete (stb);

  8933.   add_setshow_enum_cmd("disassembler", no_class,
  8934.                        valid_disassembly_styles, &disassembly_style,
  8935.                        _("Set the disassembly style."),
  8936.                        _("Show the disassembly style."),
  8937.                        helptext,
  8938.                        set_disassembly_style_sfunc,
  8939.                        NULL, /* FIXME: i18n: The disassembly style is
  8940.                                 \"%s\".  */
  8941.                        &setarmcmdlist, &showarmcmdlist);

  8942.   add_setshow_boolean_cmd ("apcs32", no_class, &arm_apcs_32,
  8943.                            _("Set usage of ARM 32-bit mode."),
  8944.                            _("Show usage of ARM 32-bit mode."),
  8945.                            _("When off, a 26-bit PC will be used."),
  8946.                            NULL,
  8947.                            NULL, /* FIXME: i18n: Usage of ARM 32-bit
  8948.                                     mode is %s.  */
  8949.                            &setarmcmdlist, &showarmcmdlist);

  8950.   /* Add a command to allow the user to force the FPU model.  */
  8951.   add_setshow_enum_cmd ("fpu", no_class, fp_model_strings, &current_fp_model,
  8952.                         _("Set the floating point type."),
  8953.                         _("Show the floating point type."),
  8954.                         _("auto - Determine the FP typefrom the OS-ABI.\n\
  8955. softfpa - Software FP, mixed-endian doubles on little-endian ARMs.\n\
  8956. fpa - FPA co-processor (GCC compiled).\n\
  8957. softvfp - Software FP with pure-endian doubles.\n\
  8958. vfp - VFP co-processor."),
  8959.                         set_fp_model_sfunc, show_fp_model,
  8960.                         &setarmcmdlist, &showarmcmdlist);

  8961.   /* Add a command to allow the user to force the ABI.  */
  8962.   add_setshow_enum_cmd ("abi", class_support, arm_abi_strings, &arm_abi_string,
  8963.                         _("Set the ABI."),
  8964.                         _("Show the ABI."),
  8965.                         NULL, arm_set_abi, arm_show_abi,
  8966.                         &setarmcmdlist, &showarmcmdlist);

  8967.   /* Add two commands to allow the user to force the assumed
  8968.      execution mode.  */
  8969.   add_setshow_enum_cmd ("fallback-mode", class_support,
  8970.                         arm_mode_strings, &arm_fallback_mode_string,
  8971.                         _("Set the mode assumed when symbols are unavailable."),
  8972.                         _("Show the mode assumed when symbols are unavailable."),
  8973.                         NULL, NULL, arm_show_fallback_mode,
  8974.                         &setarmcmdlist, &showarmcmdlist);
  8975.   add_setshow_enum_cmd ("force-mode", class_support,
  8976.                         arm_mode_strings, &arm_force_mode_string,
  8977.                         _("Set the mode assumed even when symbols are available."),
  8978.                         _("Show the mode assumed even when symbols are available."),
  8979.                         NULL, NULL, arm_show_force_mode,
  8980.                         &setarmcmdlist, &showarmcmdlist);

  8981.   /* Debugging flag.  */
  8982.   add_setshow_boolean_cmd ("arm", class_maintenance, &arm_debug,
  8983.                            _("Set ARM debugging."),
  8984.                            _("Show ARM debugging."),
  8985.                            _("When on, arm-specific debugging is enabled."),
  8986.                            NULL,
  8987.                            NULL, /* FIXME: i18n: "ARM debugging is %s.  */
  8988.                            &setdebuglist, &showdebuglist);
  8989. }

  8990. /* ARM-reversible process record data structures.  */

  8991. #define ARM_INSN_SIZE_BYTES 4
  8992. #define THUMB_INSN_SIZE_BYTES 2
  8993. #define THUMB2_INSN_SIZE_BYTES 4


  8994. /* Position of the bit within a 32-bit ARM instruction
  8995.    that defines whether the instruction is a load or store.  */
  8996. #define INSN_S_L_BIT_NUM 20

  8997. #define REG_ALLOC(REGS, LENGTH, RECORD_BUF) \
  8998.         do  \
  8999.           { \
  9000.             unsigned int reg_len = LENGTH; \
  9001.             if (reg_len) \
  9002.               { \
  9003.                 REGS = XNEWVEC (uint32_t, reg_len); \
  9004.                 memcpy(&REGS[0], &RECORD_BUF[0], sizeof(uint32_t)*LENGTH); \
  9005.               } \
  9006.           } \
  9007.         while (0)

  9008. #define MEM_ALLOC(MEMS, LENGTH, RECORD_BUF) \
  9009.         do  \
  9010.           { \
  9011.             unsigned int mem_len = LENGTH; \
  9012.             if (mem_len) \
  9013.             { \
  9014.               MEMS =  XNEWVEC (struct arm_mem_r, mem_len);  \
  9015.               memcpy(&MEMS->len, &RECORD_BUF[0], \
  9016.                      sizeof(struct arm_mem_r) * LENGTH); \
  9017.             } \
  9018.           } \
  9019.           while (0)

  9020. /* Checks whether insn is already recorded or yet to be decoded. (boolean expression).  */
  9021. #define INSN_RECORDED(ARM_RECORD) \
  9022.         (0 != (ARM_RECORD)->reg_rec_count || 0 != (ARM_RECORD)->mem_rec_count)

  9023. /* ARM memory record structure.  */
  9024. struct arm_mem_r
  9025. {
  9026.   uint32_t len;    /* Record length.  */
  9027.   uint32_t addr;   /* Memory address.  */
  9028. };

  9029. /* ARM instruction record contains opcode of current insn
  9030.    and execution state (before entry to decode_insn()),
  9031.    contains list of to-be-modified registers and
  9032.    memory blocks (on return from decode_insn()).  */

  9033. typedef struct insn_decode_record_t
  9034. {
  9035.   struct gdbarch *gdbarch;
  9036.   struct regcache *regcache;
  9037.   CORE_ADDR this_addr;          /* Address of the insn being decoded.  */
  9038.   uint32_t arm_insn;            /* Should accommodate thumb.  */
  9039.   uint32_t cond;                /* Condition code.  */
  9040.   uint32_t opcode;              /* Insn opcode.  */
  9041.   uint32_t decode;              /* Insn decode bits.  */
  9042.   uint32_t mem_rec_count;       /* No of mem records.  */
  9043.   uint32_t reg_rec_count;       /* No of reg records.  */
  9044.   uint32_t *arm_regs;           /* Registers to be saved for this record.  */
  9045.   struct arm_mem_r *arm_mems;   /* Memory to be saved for this record.  */
  9046. } insn_decode_record;


  9047. /* Checks ARM SBZ and SBO mandatory fields.  */

  9048. static int
  9049. sbo_sbz (uint32_t insn, uint32_t bit_num, uint32_t len, uint32_t sbo)
  9050. {
  9051.   uint32_t ones = bits (insn, bit_num - 1, (bit_num -1) + (len - 1));

  9052.   if (!len)
  9053.     return 1;

  9054.   if (!sbo)
  9055.     ones = ~ones;

  9056.   while (ones)
  9057.     {
  9058.       if (!(ones & sbo))
  9059.         {
  9060.           return 0;
  9061.         }
  9062.       ones = ones >> 1;
  9063.     }
  9064.   return 1;
  9065. }

  9066. enum arm_record_result
  9067. {
  9068.   ARM_RECORD_SUCCESS = 0,
  9069.   ARM_RECORD_FAILURE = 1
  9070. };

  9071. typedef enum
  9072. {
  9073.   ARM_RECORD_STRH=1,
  9074.   ARM_RECORD_STRD
  9075. } arm_record_strx_t;

  9076. typedef enum
  9077. {
  9078.   ARM_RECORD=1,
  9079.   THUMB_RECORD,
  9080.   THUMB2_RECORD
  9081. } record_type_t;


  9082. static int
  9083. arm_record_strx (insn_decode_record *arm_insn_r, uint32_t *record_buf,
  9084.                  uint32_t *record_buf_mem, arm_record_strx_t str_type)
  9085. {

  9086.   struct regcache *reg_cache = arm_insn_r->regcache;
  9087.   ULONGEST u_regval[2]= {0};

  9088.   uint32_t reg_src1 = 0, reg_src2 = 0;
  9089.   uint32_t immed_high = 0, immed_low = 0,offset_8 = 0, tgt_mem_addr = 0;
  9090.   uint32_t opcode1 = 0;

  9091.   arm_insn_r->opcode = bits (arm_insn_r->arm_insn, 21, 24);
  9092.   arm_insn_r->decode = bits (arm_insn_r->arm_insn, 4, 7);
  9093.   opcode1 = bits (arm_insn_r->arm_insn, 20, 24);


  9094.   if (14 == arm_insn_r->opcode || 10 == arm_insn_r->opcode)
  9095.     {
  9096.       /* 1) Handle misc store, immediate offset.  */
  9097.       immed_low = bits (arm_insn_r->arm_insn, 0, 3);
  9098.       immed_high = bits (arm_insn_r->arm_insn, 8, 11);
  9099.       reg_src1 = bits (arm_insn_r->arm_insn, 16, 19);
  9100.       regcache_raw_read_unsigned (reg_cache, reg_src1,
  9101.                                   &u_regval[0]);
  9102.       if (ARM_PC_REGNUM == reg_src1)
  9103.         {
  9104.           /* If R15 was used as Rn, hence current PC+8.  */
  9105.           u_regval[0] = u_regval[0] + 8;
  9106.         }
  9107.       offset_8 = (immed_high << 4) | immed_low;
  9108.       /* Calculate target store address.  */
  9109.       if (14 == arm_insn_r->opcode)
  9110.         {
  9111.           tgt_mem_addr = u_regval[0] + offset_8;
  9112.         }
  9113.       else
  9114.         {
  9115.           tgt_mem_addr = u_regval[0] - offset_8;
  9116.         }
  9117.       if (ARM_RECORD_STRH == str_type)
  9118.         {
  9119.           record_buf_mem[0] = 2;
  9120.           record_buf_mem[1] = tgt_mem_addr;
  9121.           arm_insn_r->mem_rec_count = 1;
  9122.         }
  9123.       else if (ARM_RECORD_STRD == str_type)
  9124.         {
  9125.           record_buf_mem[0] = 4;
  9126.           record_buf_mem[1] = tgt_mem_addr;
  9127.           record_buf_mem[2] = 4;
  9128.           record_buf_mem[3] = tgt_mem_addr + 4;
  9129.           arm_insn_r->mem_rec_count = 2;
  9130.         }
  9131.     }
  9132.   else if (12 == arm_insn_r->opcode || 8 == arm_insn_r->opcode)
  9133.     {
  9134.       /* 2) Store, register offset.  */
  9135.       /* Get Rm.  */
  9136.       reg_src1 = bits (arm_insn_r->arm_insn, 0, 3);
  9137.       /* Get Rn.  */
  9138.       reg_src2 = bits (arm_insn_r->arm_insn, 16, 19);
  9139.       regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval[0]);
  9140.       regcache_raw_read_unsigned (reg_cache, reg_src2, &u_regval[1]);
  9141.       if (15 == reg_src2)
  9142.         {
  9143.           /* If R15 was used as Rn, hence current PC+8.  */
  9144.           u_regval[0] = u_regval[0] + 8;
  9145.         }
  9146.       /* Calculate target store address, Rn +/- Rm, register offset.  */
  9147.       if (12 == arm_insn_r->opcode)
  9148.         {
  9149.           tgt_mem_addr = u_regval[0] + u_regval[1];
  9150.         }
  9151.       else
  9152.         {
  9153.           tgt_mem_addr = u_regval[1] - u_regval[0];
  9154.         }
  9155.       if (ARM_RECORD_STRH == str_type)
  9156.         {
  9157.           record_buf_mem[0] = 2;
  9158.           record_buf_mem[1] = tgt_mem_addr;
  9159.           arm_insn_r->mem_rec_count = 1;
  9160.         }
  9161.       else if (ARM_RECORD_STRD == str_type)
  9162.         {
  9163.           record_buf_mem[0] = 4;
  9164.           record_buf_mem[1] = tgt_mem_addr;
  9165.           record_buf_mem[2] = 4;
  9166.           record_buf_mem[3] = tgt_mem_addr + 4;
  9167.           arm_insn_r->mem_rec_count = 2;
  9168.         }
  9169.     }
  9170.   else if (11 == arm_insn_r->opcode || 15 == arm_insn_r->opcode
  9171.            || 2 == arm_insn_r->opcode  || 6 == arm_insn_r->opcode)
  9172.     {
  9173.       /* 3) Store, immediate pre-indexed.  */
  9174.       /* 5) Store, immediate post-indexed.  */
  9175.       immed_low = bits (arm_insn_r->arm_insn, 0, 3);
  9176.       immed_high = bits (arm_insn_r->arm_insn, 8, 11);
  9177.       offset_8 = (immed_high << 4) | immed_low;
  9178.       reg_src1 = bits (arm_insn_r->arm_insn, 16, 19);
  9179.       regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval[0]);
  9180.       /* Calculate target store address, Rn +/- Rm, register offset.  */
  9181.       if (15 == arm_insn_r->opcode || 6 == arm_insn_r->opcode)
  9182.         {
  9183.           tgt_mem_addr = u_regval[0] + offset_8;
  9184.         }
  9185.       else
  9186.         {
  9187.           tgt_mem_addr = u_regval[0] - offset_8;
  9188.         }
  9189.       if (ARM_RECORD_STRH == str_type)
  9190.         {
  9191.           record_buf_mem[0] = 2;
  9192.           record_buf_mem[1] = tgt_mem_addr;
  9193.           arm_insn_r->mem_rec_count = 1;
  9194.         }
  9195.       else if (ARM_RECORD_STRD == str_type)
  9196.         {
  9197.           record_buf_mem[0] = 4;
  9198.           record_buf_mem[1] = tgt_mem_addr;
  9199.           record_buf_mem[2] = 4;
  9200.           record_buf_mem[3] = tgt_mem_addr + 4;
  9201.           arm_insn_r->mem_rec_count = 2;
  9202.         }
  9203.       /* Record Rn also as it changes.  */
  9204.       *(record_buf) = bits (arm_insn_r->arm_insn, 16, 19);
  9205.       arm_insn_r->reg_rec_count = 1;
  9206.     }
  9207.   else if (9 == arm_insn_r->opcode || 13 == arm_insn_r->opcode
  9208.            || 0 == arm_insn_r->opcode || 4 == arm_insn_r->opcode)
  9209.     {
  9210.       /* 4) Store, register pre-indexed.  */
  9211.       /* 6) Store, register post -indexed.  */
  9212.       reg_src1 = bits (arm_insn_r->arm_insn, 0, 3);
  9213.       reg_src2 = bits (arm_insn_r->arm_insn, 16, 19);
  9214.       regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval[0]);
  9215.       regcache_raw_read_unsigned (reg_cache, reg_src2, &u_regval[1]);
  9216.       /* Calculate target store address, Rn +/- Rm, register offset.  */
  9217.       if (13 == arm_insn_r->opcode || 4 == arm_insn_r->opcode)
  9218.         {
  9219.           tgt_mem_addr = u_regval[0] + u_regval[1];
  9220.         }
  9221.       else
  9222.         {
  9223.           tgt_mem_addr = u_regval[1] - u_regval[0];
  9224.         }
  9225.       if (ARM_RECORD_STRH == str_type)
  9226.         {
  9227.           record_buf_mem[0] = 2;
  9228.           record_buf_mem[1] = tgt_mem_addr;
  9229.           arm_insn_r->mem_rec_count = 1;
  9230.         }
  9231.       else if (ARM_RECORD_STRD == str_type)
  9232.         {
  9233.           record_buf_mem[0] = 4;
  9234.           record_buf_mem[1] = tgt_mem_addr;
  9235.           record_buf_mem[2] = 4;
  9236.           record_buf_mem[3] = tgt_mem_addr + 4;
  9237.           arm_insn_r->mem_rec_count = 2;
  9238.         }
  9239.       /* Record Rn also as it changes.  */
  9240.       *(record_buf) = bits (arm_insn_r->arm_insn, 16, 19);
  9241.       arm_insn_r->reg_rec_count = 1;
  9242.     }
  9243.   return 0;
  9244. }

  9245. /* Handling ARM extension space insns.  */

  9246. static int
  9247. arm_record_extension_space (insn_decode_record *arm_insn_r)
  9248. {
  9249.   uint32_t ret = 0/* Return value: -1:record failure ;  0:success  */
  9250.   uint32_t opcode1 = 0, opcode2 = 0, insn_op1 = 0;
  9251.   uint32_t record_buf[8], record_buf_mem[8];
  9252.   uint32_t reg_src1 = 0;
  9253.   uint32_t immed_high = 0, immed_low = 0,offset_8 = 0, tgt_mem_addr = 0;
  9254.   struct regcache *reg_cache = arm_insn_r->regcache;
  9255.   ULONGEST u_regval = 0;

  9256.   gdb_assert (!INSN_RECORDED(arm_insn_r));
  9257.   /* Handle unconditional insn extension space.  */

  9258.   opcode1 = bits (arm_insn_r->arm_insn, 20, 27);
  9259.   opcode2 = bits (arm_insn_r->arm_insn, 4, 7);
  9260.   if (arm_insn_r->cond)
  9261.     {
  9262.       /* PLD has no affect on architectural state, it just affects
  9263.          the caches.  */
  9264.       if (5 == ((opcode1 & 0xE0) >> 5))
  9265.         {
  9266.           /* BLX(1) */
  9267.           record_buf[0] = ARM_PS_REGNUM;
  9268.           record_buf[1] = ARM_LR_REGNUM;
  9269.           arm_insn_r->reg_rec_count = 2;
  9270.         }
  9271.       /* STC2, LDC2, MCR2, MRC2, CDP2: <TBD>, co-processor insn.  */
  9272.     }


  9273.   opcode1 = bits (arm_insn_r->arm_insn, 25, 27);
  9274.   if (3 == opcode1 && bit (arm_insn_r->arm_insn, 4))
  9275.     {
  9276.       ret = -1;
  9277.       /* Undefined instruction on ARM V5; need to handle if later
  9278.          versions define it.  */
  9279.     }

  9280.   opcode1 = bits (arm_insn_r->arm_insn, 24, 27);
  9281.   opcode2 = bits (arm_insn_r->arm_insn, 4, 7);
  9282.   insn_op1 = bits (arm_insn_r->arm_insn, 20, 23);

  9283.   /* Handle arithmetic insn extension space.  */
  9284.   if (!opcode1 && 9 == opcode2 && 1 != arm_insn_r->cond
  9285.       && !INSN_RECORDED(arm_insn_r))
  9286.     {
  9287.       /* Handle MLA(S) and MUL(S).  */
  9288.       if (0 <= insn_op1 && 3 >= insn_op1)
  9289.       {
  9290.         record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
  9291.         record_buf[1] = ARM_PS_REGNUM;
  9292.         arm_insn_r->reg_rec_count = 2;
  9293.       }
  9294.       else if (4 <= insn_op1 && 15 >= insn_op1)
  9295.       {
  9296.         /* Handle SMLAL(S), SMULL(S), UMLAL(S), UMULL(S).  */
  9297.         record_buf[0] = bits (arm_insn_r->arm_insn, 16, 19);
  9298.         record_buf[1] = bits (arm_insn_r->arm_insn, 12, 15);
  9299.         record_buf[2] = ARM_PS_REGNUM;
  9300.         arm_insn_r->reg_rec_count = 3;
  9301.       }
  9302.     }

  9303.   opcode1 = bits (arm_insn_r->arm_insn, 26, 27);
  9304.   opcode2 = bits (arm_insn_r->arm_insn, 23, 24);
  9305.   insn_op1 = bits (arm_insn_r->arm_insn, 21, 22);

  9306.   /* Handle control insn extension space.  */

  9307.   if (!opcode1 && 2 == opcode2 && !bit (arm_insn_r->arm_insn, 20)
  9308.       && 1 != arm_insn_r->cond && !INSN_RECORDED(arm_insn_r))
  9309.     {
  9310.       if (!bit (arm_insn_r->arm_insn,25))
  9311.         {
  9312.           if (!bits (arm_insn_r->arm_insn, 4, 7))
  9313.             {
  9314.               if ((0 == insn_op1) || (2 == insn_op1))
  9315.                 {
  9316.                   /* MRS.  */
  9317.                   record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
  9318.                   arm_insn_r->reg_rec_count = 1;
  9319.                 }
  9320.               else if (1 == insn_op1)
  9321.                 {
  9322.                   /* CSPR is going to be changed.  */
  9323.                   record_buf[0] = ARM_PS_REGNUM;
  9324.                   arm_insn_r->reg_rec_count = 1;
  9325.                 }
  9326.               else if (3 == insn_op1)
  9327.                 {
  9328.                   /* SPSR is going to be changed.  */
  9329.                   /* We need to get SPSR value, which is yet to be done.  */
  9330.                   printf_unfiltered (_("Process record does not support "
  9331.                                      "instruction  0x%0x at address %s.\n"),
  9332.                                      arm_insn_r->arm_insn,
  9333.                                      paddress (arm_insn_r->gdbarch,
  9334.                                      arm_insn_r->this_addr));
  9335.                   return -1;
  9336.                 }
  9337.             }
  9338.           else if (1 == bits (arm_insn_r->arm_insn, 4, 7))
  9339.             {
  9340.               if (1 == insn_op1)
  9341.                 {
  9342.                   /* BX.  */
  9343.                   record_buf[0] = ARM_PS_REGNUM;
  9344.                   arm_insn_r->reg_rec_count = 1;
  9345.                 }
  9346.               else if (3 == insn_op1)
  9347.                 {
  9348.                   /* CLZ.  */
  9349.                   record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
  9350.                   arm_insn_r->reg_rec_count = 1;
  9351.                 }
  9352.             }
  9353.           else if (3 == bits (arm_insn_r->arm_insn, 4, 7))
  9354.             {
  9355.               /* BLX.  */
  9356.               record_buf[0] = ARM_PS_REGNUM;
  9357.               record_buf[1] = ARM_LR_REGNUM;
  9358.               arm_insn_r->reg_rec_count = 2;
  9359.             }
  9360.           else if (5 == bits (arm_insn_r->arm_insn, 4, 7))
  9361.             {
  9362.               /* QADD, QSUB, QDADD, QDSUB */
  9363.               record_buf[0] = ARM_PS_REGNUM;
  9364.               record_buf[1] = bits (arm_insn_r->arm_insn, 12, 15);
  9365.               arm_insn_r->reg_rec_count = 2;
  9366.             }
  9367.           else if (7 == bits (arm_insn_r->arm_insn, 4, 7))
  9368.             {
  9369.               /* BKPT.  */
  9370.               record_buf[0] = ARM_PS_REGNUM;
  9371.               record_buf[1] = ARM_LR_REGNUM;
  9372.               arm_insn_r->reg_rec_count = 2;

  9373.               /* Save SPSR also;how?  */
  9374.               printf_unfiltered (_("Process record does not support "
  9375.                                   "instruction 0x%0x at address %s.\n"),
  9376.                                   arm_insn_r->arm_insn,
  9377.                   paddress (arm_insn_r->gdbarch, arm_insn_r->this_addr));
  9378.               return -1;
  9379.             }
  9380.           else if(8 == bits (arm_insn_r->arm_insn, 4, 7)
  9381.                   || 10 == bits (arm_insn_r->arm_insn, 4, 7)
  9382.                   || 12 == bits (arm_insn_r->arm_insn, 4, 7)
  9383.                   || 14 == bits (arm_insn_r->arm_insn, 4, 7)
  9384.                  )
  9385.             {
  9386.               if (0 == insn_op1 || 1 == insn_op1)
  9387.                 {
  9388.                   /* SMLA<x><y>, SMLAW<y>, SMULW<y>.  */
  9389.                   /* We dont do optimization for SMULW<y> where we
  9390.                      need only Rd.  */
  9391.                   record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
  9392.                   record_buf[1] = ARM_PS_REGNUM;
  9393.                   arm_insn_r->reg_rec_count = 2;
  9394.                 }
  9395.               else if (2 == insn_op1)
  9396.                 {
  9397.                   /* SMLAL<x><y>.  */
  9398.                   record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
  9399.                   record_buf[1] = bits (arm_insn_r->arm_insn, 16, 19);
  9400.                   arm_insn_r->reg_rec_count = 2;
  9401.                 }
  9402.               else if (3 == insn_op1)
  9403.                 {
  9404.                   /* SMUL<x><y>.  */
  9405.                   record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
  9406.                   arm_insn_r->reg_rec_count = 1;
  9407.                 }
  9408.             }
  9409.         }
  9410.       else
  9411.         {
  9412.           /* MSR : immediate form.  */
  9413.           if (1 == insn_op1)
  9414.             {
  9415.               /* CSPR is going to be changed.  */
  9416.               record_buf[0] = ARM_PS_REGNUM;
  9417.               arm_insn_r->reg_rec_count = 1;
  9418.             }
  9419.           else if (3 == insn_op1)
  9420.             {
  9421.               /* SPSR is going to be changed.  */
  9422.               /* we need to get SPSR value, which is yet to be done  */
  9423.               printf_unfiltered (_("Process record does not support "
  9424.                                    "instruction 0x%0x at address %s.\n"),
  9425.                                     arm_insn_r->arm_insn,
  9426.                                     paddress (arm_insn_r->gdbarch,
  9427.                                     arm_insn_r->this_addr));
  9428.               return -1;
  9429.             }
  9430.         }
  9431.     }

  9432.   opcode1 = bits (arm_insn_r->arm_insn, 25, 27);
  9433.   opcode2 = bits (arm_insn_r->arm_insn, 20, 24);
  9434.   insn_op1 = bits (arm_insn_r->arm_insn, 5, 6);

  9435.   /* Handle load/store insn extension space.  */

  9436.   if (!opcode1 && bit (arm_insn_r->arm_insn, 7)
  9437.       && bit (arm_insn_r->arm_insn, 4) && 1 != arm_insn_r->cond
  9438.       && !INSN_RECORDED(arm_insn_r))
  9439.     {
  9440.       /* SWP/SWPB.  */
  9441.       if (0 == insn_op1)
  9442.         {
  9443.           /* These insn, changes register and memory as well.  */
  9444.           /* SWP or SWPB insn.  */
  9445.           /* Get memory address given by Rn.  */
  9446.           reg_src1 = bits (arm_insn_r->arm_insn, 16, 19);
  9447.           regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval);
  9448.           /* SWP insn ?, swaps word.  */
  9449.           if (8 == arm_insn_r->opcode)
  9450.             {
  9451.               record_buf_mem[0] = 4;
  9452.             }
  9453.           else
  9454.             {
  9455.               /* SWPB insn, swaps only byte.  */
  9456.               record_buf_mem[0] = 1;
  9457.             }
  9458.           record_buf_mem[1] = u_regval;
  9459.           arm_insn_r->mem_rec_count = 1;
  9460.           record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
  9461.           arm_insn_r->reg_rec_count = 1;
  9462.         }
  9463.       else if (1 == insn_op1 && !bit (arm_insn_r->arm_insn, 20))
  9464.         {
  9465.           /* STRH.  */
  9466.           arm_record_strx(arm_insn_r, &record_buf[0], &record_buf_mem[0],
  9467.                           ARM_RECORD_STRH);
  9468.         }
  9469.       else if (2 == insn_op1 && !bit (arm_insn_r->arm_insn, 20))
  9470.         {
  9471.           /* LDRD.  */
  9472.           record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
  9473.           record_buf[1] = record_buf[0] + 1;
  9474.           arm_insn_r->reg_rec_count = 2;
  9475.         }
  9476.       else if (3 == insn_op1 && !bit (arm_insn_r->arm_insn, 20))
  9477.         {
  9478.           /* STRD.  */
  9479.           arm_record_strx(arm_insn_r, &record_buf[0], &record_buf_mem[0],
  9480.                         ARM_RECORD_STRD);
  9481.         }
  9482.       else if (bit (arm_insn_r->arm_insn, 20) && insn_op1 <= 3)
  9483.         {
  9484.           /* LDRH, LDRSB, LDRSH.  */
  9485.           record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
  9486.           arm_insn_r->reg_rec_count = 1;
  9487.         }

  9488.     }

  9489.   opcode1 = bits (arm_insn_r->arm_insn, 23, 27);
  9490.   if (24 == opcode1 && bit (arm_insn_r->arm_insn, 21)
  9491.       && !INSN_RECORDED(arm_insn_r))
  9492.     {
  9493.       ret = -1;
  9494.       /* Handle coprocessor insn extension space.  */
  9495.     }

  9496.   /* To be done for ARMv5 and later; as of now we return -1.  */
  9497.   if (-1 == ret)
  9498.     printf_unfiltered (_("Process record does not support instruction x%0x "
  9499.                          "at address %s.\n"),arm_insn_r->arm_insn,
  9500.                          paddress (arm_insn_r->gdbarch, arm_insn_r->this_addr));


  9501.   REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
  9502.   MEM_ALLOC (arm_insn_r->arm_mems, arm_insn_r->mem_rec_count, record_buf_mem);

  9503.   return ret;
  9504. }

  9505. /* Handling opcode 000 insns.  */

  9506. static int
  9507. arm_record_data_proc_misc_ld_str (insn_decode_record *arm_insn_r)
  9508. {
  9509.   struct regcache *reg_cache = arm_insn_r->regcache;
  9510.   uint32_t record_buf[8], record_buf_mem[8];
  9511.   ULONGEST u_regval[2] = {0};

  9512.   uint32_t reg_src1 = 0, reg_src2 = 0, reg_dest = 0;
  9513.   uint32_t immed_high = 0, immed_low = 0, offset_8 = 0, tgt_mem_addr = 0;
  9514.   uint32_t opcode1 = 0;

  9515.   arm_insn_r->opcode = bits (arm_insn_r->arm_insn, 21, 24);
  9516.   arm_insn_r->decode = bits (arm_insn_r->arm_insn, 4, 7);
  9517.   opcode1 = bits (arm_insn_r->arm_insn, 20, 24);

  9518.   /* Data processing insn /multiply insn.  */
  9519.   if (9 == arm_insn_r->decode
  9520.       && ((4 <= arm_insn_r->opcode && 7 >= arm_insn_r->opcode)
  9521.       ||  (0 == arm_insn_r->opcode || 1 == arm_insn_r->opcode)))
  9522.     {
  9523.       /* Handle multiply instructions.  */
  9524.       /* MLA, MUL, SMLAL, SMULL, UMLAL, UMULL.  */
  9525.         if (0 == arm_insn_r->opcode || 1 == arm_insn_r->opcode)
  9526.           {
  9527.             /* Handle MLA and MUL.  */
  9528.             record_buf[0] = bits (arm_insn_r->arm_insn, 16, 19);
  9529.             record_buf[1] = ARM_PS_REGNUM;
  9530.             arm_insn_r->reg_rec_count = 2;
  9531.           }
  9532.         else if (4 <= arm_insn_r->opcode && 7 >= arm_insn_r->opcode)
  9533.           {
  9534.             /* Handle SMLAL, SMULL, UMLAL, UMULL.  */
  9535.             record_buf[0] = bits (arm_insn_r->arm_insn, 16, 19);
  9536.             record_buf[1] = bits (arm_insn_r->arm_insn, 12, 15);
  9537.             record_buf[2] = ARM_PS_REGNUM;
  9538.             arm_insn_r->reg_rec_count = 3;
  9539.           }
  9540.     }
  9541.   else if (bit (arm_insn_r->arm_insn, INSN_S_L_BIT_NUM)
  9542.            && (11 == arm_insn_r->decode || 13 == arm_insn_r->decode))
  9543.     {
  9544.       /* Handle misc load insns, as 20th bit  (L = 1).  */
  9545.       /* LDR insn has a capability to do branching, if
  9546.          MOV LR, PC is precceded by LDR insn having Rn as R15
  9547.          in that case, it emulates branch and link insn, and hence we
  9548.          need to save CSPR and PC as well. I am not sure this is right
  9549.          place; as opcode = 010 LDR insn make this happen, if R15 was
  9550.          used.  */
  9551.       reg_dest = bits (arm_insn_r->arm_insn, 12, 15);
  9552.       if (15 != reg_dest)
  9553.         {
  9554.           record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
  9555.           arm_insn_r->reg_rec_count = 1;
  9556.         }
  9557.       else
  9558.         {
  9559.           record_buf[0] = reg_dest;
  9560.           record_buf[1] = ARM_PS_REGNUM;
  9561.           arm_insn_r->reg_rec_count = 2;
  9562.         }
  9563.     }
  9564.   else if ((9 == arm_insn_r->opcode || 11 == arm_insn_r->opcode)
  9565.            && sbo_sbz (arm_insn_r->arm_insn, 5, 12, 0)
  9566.            && sbo_sbz (arm_insn_r->arm_insn, 13, 4, 1)
  9567.            && 2 == bits (arm_insn_r->arm_insn, 20, 21))
  9568.     {
  9569.       /* Handle MSR insn.  */
  9570.       if (9 == arm_insn_r->opcode)
  9571.         {
  9572.           /* CSPR is going to be changed.  */
  9573.           record_buf[0] = ARM_PS_REGNUM;
  9574.           arm_insn_r->reg_rec_count = 1;
  9575.         }
  9576.       else
  9577.         {
  9578.           /* SPSR is going to be changed.  */
  9579.           /* How to read SPSR value?  */
  9580.           printf_unfiltered (_("Process record does not support instruction "
  9581.                             "0x%0x at address %s.\n"),
  9582.                             arm_insn_r->arm_insn,
  9583.                         paddress (arm_insn_r->gdbarch, arm_insn_r->this_addr));
  9584.           return -1;
  9585.         }
  9586.     }
  9587.   else if (9 == arm_insn_r->decode
  9588.            && (8 == arm_insn_r->opcode || 10 == arm_insn_r->opcode)
  9589.            && !bit (arm_insn_r->arm_insn, INSN_S_L_BIT_NUM))
  9590.     {
  9591.       /* Handling SWP, SWPB.  */
  9592.       /* These insn, changes register and memory as well.  */
  9593.       /* SWP or SWPB insn.  */

  9594.       reg_src1 = bits (arm_insn_r->arm_insn, 16, 19);
  9595.       regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval[0]);
  9596.       /* SWP insn ?, swaps word.  */
  9597.       if (8 == arm_insn_r->opcode)
  9598.         {
  9599.           record_buf_mem[0] = 4;
  9600.         }
  9601.         else
  9602.         {
  9603.           /* SWPB insn, swaps only byte.  */
  9604.           record_buf_mem[0] = 1;
  9605.         }
  9606.       record_buf_mem[1] = u_regval[0];
  9607.       arm_insn_r->mem_rec_count = 1;
  9608.       record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
  9609.       arm_insn_r->reg_rec_count = 1;
  9610.     }
  9611.   else if (3 == arm_insn_r->decode && 0x12 == opcode1
  9612.            && sbo_sbz (arm_insn_r->arm_insn, 9, 12, 1))
  9613.     {
  9614.       /* Handle BLX, branch and link/exchange.  */
  9615.       if (9 == arm_insn_r->opcode)
  9616.       {
  9617.         /* Branch is chosen by setting T bit of CSPR, bitp[0] of Rm,
  9618.            and R14 stores the return address.  */
  9619.         record_buf[0] = ARM_PS_REGNUM;
  9620.         record_buf[1] = ARM_LR_REGNUM;
  9621.         arm_insn_r->reg_rec_count = 2;
  9622.       }
  9623.     }
  9624.   else if (7 == arm_insn_r->decode && 0x12 == opcode1)
  9625.     {
  9626.       /* Handle enhanced software breakpoint insn, BKPT.  */
  9627.       /* CPSR is changed to be executed in ARM state,  disabling normal
  9628.          interrupts, entering abort mode.  */
  9629.       /* According to high vector configuration PC is set.  */
  9630.       /* user hit breakpoint and type reverse, in
  9631.          that case, we need to go back with previous CPSR and
  9632.          Program Counter.  */
  9633.       record_buf[0] = ARM_PS_REGNUM;
  9634.       record_buf[1] = ARM_LR_REGNUM;
  9635.       arm_insn_r->reg_rec_count = 2;

  9636.       /* Save SPSR also; how?  */
  9637.       printf_unfiltered (_("Process record does not support instruction "
  9638.                            "0x%0x at address %s.\n"),arm_insn_r->arm_insn,
  9639.                            paddress (arm_insn_r->gdbarch,
  9640.                            arm_insn_r->this_addr));
  9641.       return -1;
  9642.     }
  9643.   else if (11 == arm_insn_r->decode
  9644.            && !bit (arm_insn_r->arm_insn, INSN_S_L_BIT_NUM))
  9645.   {
  9646.     /* Handle enhanced store insns and DSP insns (e.g. LDRD).  */

  9647.     /* Handle str(x) insn */
  9648.     arm_record_strx(arm_insn_r, &record_buf[0], &record_buf_mem[0],
  9649.                     ARM_RECORD_STRH);
  9650.   }
  9651.   else if (1 == arm_insn_r->decode && 0x12 == opcode1
  9652.            && sbo_sbz (arm_insn_r->arm_insn, 9, 12, 1))
  9653.     {
  9654.       /* Handle BX, branch and link/exchange.  */
  9655.       /* Branch is chosen by setting T bit of CSPR, bitp[0] of Rm.  */
  9656.       record_buf[0] = ARM_PS_REGNUM;
  9657.       arm_insn_r->reg_rec_count = 1;
  9658.     }
  9659.   else if (1 == arm_insn_r->decode && 0x16 == opcode1
  9660.            && sbo_sbz (arm_insn_r->arm_insn, 9, 4, 1)
  9661.            && sbo_sbz (arm_insn_r->arm_insn, 17, 4, 1))
  9662.     {
  9663.       /* Count leading zeros: CLZ.  */
  9664.       record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
  9665.       arm_insn_r->reg_rec_count = 1;
  9666.     }
  9667.   else if (!bit (arm_insn_r->arm_insn, INSN_S_L_BIT_NUM)
  9668.            && (8 == arm_insn_r->opcode || 10 == arm_insn_r->opcode)
  9669.            && sbo_sbz (arm_insn_r->arm_insn, 17, 4, 1)
  9670.            && sbo_sbz (arm_insn_r->arm_insn, 1, 12, 0)
  9671.           )
  9672.     {
  9673.       /* Handle MRS insn.  */
  9674.       record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
  9675.       arm_insn_r->reg_rec_count = 1;
  9676.     }
  9677.   else if (arm_insn_r->opcode <= 15)
  9678.     {
  9679.       /* Normal data processing insns.  */
  9680.       /* Out of 11 shifter operands mode, all the insn modifies destination
  9681.          register, which is specified by 13-16 decode.  */
  9682.       record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
  9683.       record_buf[1] = ARM_PS_REGNUM;
  9684.       arm_insn_r->reg_rec_count = 2;
  9685.     }
  9686.   else
  9687.     {
  9688.       return -1;
  9689.     }

  9690.   REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
  9691.   MEM_ALLOC (arm_insn_r->arm_mems, arm_insn_r->mem_rec_count, record_buf_mem);
  9692.   return 0;
  9693. }

  9694. /* Handling opcode 001 insns.  */

  9695. static int
  9696. arm_record_data_proc_imm (insn_decode_record *arm_insn_r)
  9697. {
  9698.   uint32_t record_buf[8], record_buf_mem[8];

  9699.   arm_insn_r->opcode = bits (arm_insn_r->arm_insn, 21, 24);
  9700.   arm_insn_r->decode = bits (arm_insn_r->arm_insn, 4, 7);

  9701.   if ((9 == arm_insn_r->opcode || 11 == arm_insn_r->opcode)
  9702.       && 2 == bits (arm_insn_r->arm_insn, 20, 21)
  9703.       && sbo_sbz (arm_insn_r->arm_insn, 13, 4, 1)
  9704.      )
  9705.     {
  9706.       /* Handle MSR insn.  */
  9707.       if (9 == arm_insn_r->opcode)
  9708.         {
  9709.           /* CSPR is going to be changed.  */
  9710.           record_buf[0] = ARM_PS_REGNUM;
  9711.           arm_insn_r->reg_rec_count = 1;
  9712.         }
  9713.       else
  9714.         {
  9715.           /* SPSR is going to be changed.  */
  9716.         }
  9717.     }
  9718.   else if (arm_insn_r->opcode <= 15)
  9719.     {
  9720.       /* Normal data processing insns.  */
  9721.       /* Out of 11 shifter operands mode, all the insn modifies destination
  9722.          register, which is specified by 13-16 decode.  */
  9723.       record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
  9724.       record_buf[1] = ARM_PS_REGNUM;
  9725.       arm_insn_r->reg_rec_count = 2;
  9726.     }
  9727.   else
  9728.     {
  9729.       return -1;
  9730.     }

  9731.   REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
  9732.   MEM_ALLOC (arm_insn_r->arm_mems, arm_insn_r->mem_rec_count, record_buf_mem);
  9733.   return 0;
  9734. }

  9735. /* Handle ARM mode instructions with opcode 010.  */

  9736. static int
  9737. arm_record_ld_st_imm_offset (insn_decode_record *arm_insn_r)
  9738. {
  9739.   struct regcache *reg_cache = arm_insn_r->regcache;

  9740.   uint32_t reg_base , reg_dest;
  9741.   uint32_t offset_12, tgt_mem_addr;
  9742.   uint32_t record_buf[8], record_buf_mem[8];
  9743.   unsigned char wback;
  9744.   ULONGEST u_regval;

  9745.   /* Calculate wback.  */
  9746.   wback = (bit (arm_insn_r->arm_insn, 24) == 0)
  9747.           || (bit (arm_insn_r->arm_insn, 21) == 1);

  9748.   arm_insn_r->reg_rec_count = 0;
  9749.   reg_base = bits (arm_insn_r->arm_insn, 16, 19);

  9750.   if (bit (arm_insn_r->arm_insn, INSN_S_L_BIT_NUM))
  9751.     {
  9752.       /* LDR (immediate), LDR (literal), LDRB (immediate), LDRB (literal), LDRBT
  9753.          and LDRT.  */

  9754.       reg_dest = bits (arm_insn_r->arm_insn, 12, 15);
  9755.       record_buf[arm_insn_r->reg_rec_count++] = reg_dest;

  9756.       /* The LDR instruction is capable of doing branching.  If MOV LR, PC
  9757.          preceeds a LDR instruction having R15 as reg_base, it
  9758.          emulates a branch and link instruction, and hence we need to save
  9759.          CPSR and PC as well.  */
  9760.       if (ARM_PC_REGNUM == reg_dest)
  9761.         record_buf[arm_insn_r->reg_rec_count++] = ARM_PS_REGNUM;

  9762.       /* If wback is true, also save the base register, which is going to be
  9763.          written to.  */
  9764.       if (wback)
  9765.         record_buf[arm_insn_r->reg_rec_count++] = reg_base;
  9766.     }
  9767.   else
  9768.     {
  9769.       /* STR (immediate), STRB (immediate), STRBT and STRT.  */

  9770.       offset_12 = bits (arm_insn_r->arm_insn, 0, 11);
  9771.       regcache_raw_read_unsigned (reg_cache, reg_base, &u_regval);

  9772.       /* Handle bit U.  */
  9773.       if (bit (arm_insn_r->arm_insn, 23))
  9774.         {
  9775.           /* U == 1: Add the offset. */
  9776.           tgt_mem_addr = (uint32_t) u_regval + offset_12;
  9777.         }
  9778.       else
  9779.         {
  9780.           /* U == 0: subtract the offset. */
  9781.           tgt_mem_addr = (uint32_t) u_regval - offset_12;
  9782.         }

  9783.       /* Bit 22 tells us whether the store instruction writes 1 byte or 4
  9784.          bytes.  */
  9785.       if (bit (arm_insn_r->arm_insn, 22))
  9786.         {
  9787.           /* STRB and STRBT: 1 byte.  */
  9788.           record_buf_mem[0] = 1;
  9789.         }
  9790.       else
  9791.         {
  9792.           /* STR and STRT: 4 bytes.  */
  9793.           record_buf_mem[0] = 4;
  9794.         }

  9795.       /* Handle bit P.  */
  9796.       if (bit (arm_insn_r->arm_insn, 24))
  9797.         record_buf_mem[1] = tgt_mem_addr;
  9798.       else
  9799.         record_buf_mem[1] = (uint32_t) u_regval;

  9800.       arm_insn_r->mem_rec_count = 1;

  9801.       /* If wback is true, also save the base register, which is going to be
  9802.          written to.  */
  9803.       if (wback)
  9804.         record_buf[arm_insn_r->reg_rec_count++] = reg_base;
  9805.     }

  9806.   REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
  9807.   MEM_ALLOC (arm_insn_r->arm_mems, arm_insn_r->mem_rec_count, record_buf_mem);
  9808.   return 0;
  9809. }

  9810. /* Handling opcode 011 insns.  */

  9811. static int
  9812. arm_record_ld_st_reg_offset (insn_decode_record *arm_insn_r)
  9813. {
  9814.   struct regcache *reg_cache = arm_insn_r->regcache;

  9815.   uint32_t shift_imm = 0;
  9816.   uint32_t reg_src1 = 0, reg_src2 = 0, reg_dest = 0;
  9817.   uint32_t offset_12 = 0, tgt_mem_addr = 0;
  9818.   uint32_t record_buf[8], record_buf_mem[8];

  9819.   LONGEST s_word;
  9820.   ULONGEST u_regval[2];

  9821.   arm_insn_r->opcode = bits (arm_insn_r->arm_insn, 21, 24);
  9822.   arm_insn_r->decode = bits (arm_insn_r->arm_insn, 4, 7);

  9823.   /* Handle enhanced store insns and LDRD DSP insn,
  9824.      order begins according to addressing modes for store insns
  9825.      STRH insn.  */

  9826.   /* LDR or STR?  */
  9827.   if (bit (arm_insn_r->arm_insn, INSN_S_L_BIT_NUM))
  9828.     {
  9829.       reg_dest = bits (arm_insn_r->arm_insn, 12, 15);
  9830.       /* LDR insn has a capability to do branching, if
  9831.          MOV LR, PC is precedded by LDR insn having Rn as R15
  9832.          in that case, it emulates branch and link insn, and hence we
  9833.          need to save CSPR and PC as well.  */
  9834.       if (15 != reg_dest)
  9835.         {
  9836.           record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
  9837.           arm_insn_r->reg_rec_count = 1;
  9838.         }
  9839.       else
  9840.         {
  9841.           record_buf[0] = reg_dest;
  9842.           record_buf[1] = ARM_PS_REGNUM;
  9843.           arm_insn_r->reg_rec_count = 2;
  9844.         }
  9845.     }
  9846.   else
  9847.     {
  9848.       if (! bits (arm_insn_r->arm_insn, 4, 11))
  9849.         {
  9850.           /* Store insn, register offset and register pre-indexed,
  9851.              register post-indexed.  */
  9852.           /* Get Rm.  */
  9853.           reg_src1 = bits (arm_insn_r->arm_insn, 0, 3);
  9854.           /* Get Rn.  */
  9855.           reg_src2 = bits (arm_insn_r->arm_insn, 16, 19);
  9856.           regcache_raw_read_unsigned (reg_cache, reg_src1
  9857.                                       , &u_regval[0]);
  9858.           regcache_raw_read_unsigned (reg_cache, reg_src2
  9859.                                       , &u_regval[1]);
  9860.           if (15 == reg_src2)
  9861.             {
  9862.               /* If R15 was used as Rn, hence current PC+8.  */
  9863.               /* Pre-indexed mode doesnt reach here ; illegal insn.  */
  9864.                 u_regval[0] = u_regval[0] + 8;
  9865.             }
  9866.           /* Calculate target store address, Rn +/- Rm, register offset.  */
  9867.           /* U == 1.  */
  9868.           if (bit (arm_insn_r->arm_insn, 23))
  9869.             {
  9870.               tgt_mem_addr = u_regval[0] + u_regval[1];
  9871.             }
  9872.           else
  9873.             {
  9874.               tgt_mem_addr = u_regval[1] - u_regval[0];
  9875.             }

  9876.           switch (arm_insn_r->opcode)
  9877.             {
  9878.               /* STR.  */
  9879.               case 8:
  9880.               case 12:
  9881.               /* STR.  */
  9882.               case 9:
  9883.               case 13:
  9884.               /* STRT.  */
  9885.               case 1:
  9886.               case 5:
  9887.               /* STR.  */
  9888.               case 0:
  9889.               case 4:
  9890.                 record_buf_mem[0] = 4;
  9891.               break;

  9892.               /* STRB.  */
  9893.               case 10:
  9894.               case 14:
  9895.               /* STRB.  */
  9896.               case 11:
  9897.               case 15:
  9898.               /* STRBT.  */
  9899.               case 3:
  9900.               case 7:
  9901.               /* STRB.  */
  9902.               case 2:
  9903.               case 6:
  9904.                 record_buf_mem[0] = 1;
  9905.               break;

  9906.               default:
  9907.                 gdb_assert_not_reached ("no decoding pattern found");
  9908.               break;
  9909.             }
  9910.           record_buf_mem[1] = tgt_mem_addr;
  9911.           arm_insn_r->mem_rec_count = 1;

  9912.           if (9 == arm_insn_r->opcode || 11 == arm_insn_r->opcode
  9913.               || 13 == arm_insn_r->opcode || 15 == arm_insn_r->opcode
  9914.               || 0 == arm_insn_r->opcode || 2 == arm_insn_r->opcode
  9915.               || 4 == arm_insn_r->opcode || 6 == arm_insn_r->opcode
  9916.               || 1 == arm_insn_r->opcode || 3 == arm_insn_r->opcode
  9917.               || 5 == arm_insn_r->opcode || 7 == arm_insn_r->opcode
  9918.              )
  9919.             {
  9920.               /* Rn is going to be changed in pre-indexed mode and
  9921.                  post-indexed mode as well.  */
  9922.               record_buf[0] = reg_src2;
  9923.               arm_insn_r->reg_rec_count = 1;
  9924.             }
  9925.         }
  9926.       else
  9927.         {
  9928.           /* Store insn, scaled register offset; scaled pre-indexed.  */
  9929.           offset_12 = bits (arm_insn_r->arm_insn, 5, 6);
  9930.           /* Get Rm.  */
  9931.           reg_src1 = bits (arm_insn_r->arm_insn, 0, 3);
  9932.           /* Get Rn.  */
  9933.           reg_src2 = bits (arm_insn_r->arm_insn, 16, 19);
  9934.           /* Get shift_imm.  */
  9935.           shift_imm = bits (arm_insn_r->arm_insn, 7, 11);
  9936.           regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval[0]);
  9937.           regcache_raw_read_signed (reg_cache, reg_src1, &s_word);
  9938.           regcache_raw_read_unsigned (reg_cache, reg_src2, &u_regval[1]);
  9939.           /* Offset_12 used as shift.  */
  9940.           switch (offset_12)
  9941.             {
  9942.               case 0:
  9943.                 /* Offset_12 used as index.  */
  9944.                 offset_12 = u_regval[0] << shift_imm;
  9945.               break;

  9946.               case 1:
  9947.                 offset_12 = (!shift_imm)?0:u_regval[0] >> shift_imm;
  9948.               break;

  9949.               case 2:
  9950.                 if (!shift_imm)
  9951.                   {
  9952.                     if (bit (u_regval[0], 31))
  9953.                       {
  9954.                         offset_12 = 0xFFFFFFFF;
  9955.                       }
  9956.                     else
  9957.                       {
  9958.                         offset_12 = 0;
  9959.                       }
  9960.                   }
  9961.                 else
  9962.                   {
  9963.                     /* This is arithmetic shift.  */
  9964.                     offset_12 = s_word >> shift_imm;
  9965.                   }
  9966.                 break;

  9967.               case 3:
  9968.                 if (!shift_imm)
  9969.                   {
  9970.                     regcache_raw_read_unsigned (reg_cache, ARM_PS_REGNUM,
  9971.                                                 &u_regval[1]);
  9972.                     /* Get C flag value and shift it by 31.  */
  9973.                     offset_12 = (((bit (u_regval[1], 29)) << 31) \
  9974.                                   | (u_regval[0]) >> 1);
  9975.                   }
  9976.                 else
  9977.                   {
  9978.                     offset_12 = (u_regval[0] >> shift_imm) \
  9979.                                 | (u_regval[0] <<
  9980.                                 (sizeof(uint32_t) - shift_imm));
  9981.                   }
  9982.               break;

  9983.               default:
  9984.                 gdb_assert_not_reached ("no decoding pattern found");
  9985.               break;
  9986.             }

  9987.           regcache_raw_read_unsigned (reg_cache, reg_src2, &u_regval[1]);
  9988.           /* bit U set.  */
  9989.           if (bit (arm_insn_r->arm_insn, 23))
  9990.             {
  9991.               tgt_mem_addr = u_regval[1] + offset_12;
  9992.             }
  9993.           else
  9994.             {
  9995.               tgt_mem_addr = u_regval[1] - offset_12;
  9996.             }

  9997.           switch (arm_insn_r->opcode)
  9998.             {
  9999.               /* STR.  */
  10000.               case 8:
  10001.               case 12:
  10002.               /* STR.  */
  10003.               case 9:
  10004.               case 13:
  10005.               /* STRT.  */
  10006.               case 1:
  10007.               case 5:
  10008.               /* STR.  */
  10009.               case 0:
  10010.               case 4:
  10011.                 record_buf_mem[0] = 4;
  10012.               break;

  10013.               /* STRB.  */
  10014.               case 10:
  10015.               case 14:
  10016.               /* STRB.  */
  10017.               case 11:
  10018.               case 15:
  10019.               /* STRBT.  */
  10020.               case 3:
  10021.               case 7:
  10022.               /* STRB.  */
  10023.               case 2:
  10024.               case 6:
  10025.                 record_buf_mem[0] = 1;
  10026.               break;

  10027.               default:
  10028.                 gdb_assert_not_reached ("no decoding pattern found");
  10029.               break;
  10030.             }
  10031.           record_buf_mem[1] = tgt_mem_addr;
  10032.           arm_insn_r->mem_rec_count = 1;

  10033.           if (9 == arm_insn_r->opcode || 11 == arm_insn_r->opcode
  10034.               || 13 == arm_insn_r->opcode || 15 == arm_insn_r->opcode
  10035.               || 0 == arm_insn_r->opcode || 2 == arm_insn_r->opcode
  10036.               || 4 == arm_insn_r->opcode || 6 == arm_insn_r->opcode
  10037.               || 1 == arm_insn_r->opcode || 3 == arm_insn_r->opcode
  10038.               || 5 == arm_insn_r->opcode || 7 == arm_insn_r->opcode
  10039.              )
  10040.             {
  10041.               /* Rn is going to be changed in register scaled pre-indexed
  10042.                  mode,and scaled post indexed mode.  */
  10043.               record_buf[0] = reg_src2;
  10044.               arm_insn_r->reg_rec_count = 1;
  10045.             }
  10046.         }
  10047.     }

  10048.   REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
  10049.   MEM_ALLOC (arm_insn_r->arm_mems, arm_insn_r->mem_rec_count, record_buf_mem);
  10050.   return 0;
  10051. }

  10052. /* Handle ARM mode instructions with opcode 100.  */

  10053. static int
  10054. arm_record_ld_st_multiple (insn_decode_record *arm_insn_r)
  10055. {
  10056.   struct regcache *reg_cache = arm_insn_r->regcache;
  10057.   uint32_t register_count = 0, register_bits;
  10058.   uint32_t reg_base, addr_mode;
  10059.   uint32_t record_buf[24], record_buf_mem[48];
  10060.   uint32_t wback;
  10061.   ULONGEST u_regval;

  10062.   /* Fetch the list of registers.  */
  10063.   register_bits = bits (arm_insn_r->arm_insn, 0, 15);
  10064.   arm_insn_r->reg_rec_count = 0;

  10065.   /* Fetch the base register that contains the address we are loading data
  10066.      to.  */
  10067.   reg_base = bits (arm_insn_r->arm_insn, 16, 19);

  10068.   /* Calculate wback.  */
  10069.   wback = (bit (arm_insn_r->arm_insn, 21) == 1);

  10070.   if (bit (arm_insn_r->arm_insn, INSN_S_L_BIT_NUM))
  10071.     {
  10072.       /* LDM/LDMIA/LDMFD, LDMDA/LDMFA, LDMDB and LDMIB.  */

  10073.       /* Find out which registers are going to be loaded from memory.  */
  10074.       while (register_bits)
  10075.         {
  10076.           if (register_bits & 0x00000001)
  10077.             record_buf[arm_insn_r->reg_rec_count++] = register_count;
  10078.           register_bits = register_bits >> 1;
  10079.           register_count++;
  10080.         }


  10081.       /* If wback is true, also save the base register, which is going to be
  10082.          written to.  */
  10083.       if (wback)
  10084.         record_buf[arm_insn_r->reg_rec_count++] = reg_base;

  10085.       /* Save the CPSR register.  */
  10086.       record_buf[arm_insn_r->reg_rec_count++] = ARM_PS_REGNUM;
  10087.     }
  10088.   else
  10089.     {
  10090.       /* STM (STMIA, STMEA), STMDA (STMED), STMDB (STMFD) and STMIB (STMFA).  */

  10091.       addr_mode = bits (arm_insn_r->arm_insn, 23, 24);

  10092.       regcache_raw_read_unsigned (reg_cache, reg_base, &u_regval);

  10093.       /* Find out how many registers are going to be stored to memory.  */
  10094.       while (register_bits)
  10095.         {
  10096.           if (register_bits & 0x00000001)
  10097.             register_count++;
  10098.           register_bits = register_bits >> 1;
  10099.         }

  10100.       switch (addr_mode)
  10101.         {
  10102.           /* STMDA (STMED): Decrement after.  */
  10103.           case 0:
  10104.           record_buf_mem[1] = (uint32_t) u_regval
  10105.                               - register_count * INT_REGISTER_SIZE + 4;
  10106.           break;
  10107.           /* STM (STMIA, STMEA): Increment after.  */
  10108.           case 1:
  10109.           record_buf_mem[1] = (uint32_t) u_regval;
  10110.           break;
  10111.           /* STMDB (STMFD): Decrement before.  */
  10112.           case 2:
  10113.           record_buf_mem[1] = (uint32_t) u_regval
  10114.                               - register_count * INT_REGISTER_SIZE;
  10115.           break;
  10116.           /* STMIB (STMFA): Increment before.  */
  10117.           case 3:
  10118.           record_buf_mem[1] = (uint32_t) u_regval + INT_REGISTER_SIZE;
  10119.           break;
  10120.           default:
  10121.             gdb_assert_not_reached ("no decoding pattern found");
  10122.           break;
  10123.         }

  10124.       record_buf_mem[0] = register_count * INT_REGISTER_SIZE;
  10125.       arm_insn_r->mem_rec_count = 1;

  10126.       /* If wback is true, also save the base register, which is going to be
  10127.          written to.  */
  10128.       if (wback)
  10129.         record_buf[arm_insn_r->reg_rec_count++] = reg_base;
  10130.     }

  10131.   REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
  10132.   MEM_ALLOC (arm_insn_r->arm_mems, arm_insn_r->mem_rec_count, record_buf_mem);
  10133.   return 0;
  10134. }

  10135. /* Handling opcode 101 insns.  */

  10136. static int
  10137. arm_record_b_bl (insn_decode_record *arm_insn_r)
  10138. {
  10139.   uint32_t record_buf[8];

  10140.   /* Handle B, BL, BLX(1) insns.  */
  10141.   /* B simply branches so we do nothing here.  */
  10142.   /* Note: BLX(1) doesnt fall here but instead it falls into
  10143.      extension space.  */
  10144.   if (bit (arm_insn_r->arm_insn, 24))
  10145.   {
  10146.     record_buf[0] = ARM_LR_REGNUM;
  10147.     arm_insn_r->reg_rec_count = 1;
  10148.   }

  10149.   REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);

  10150.   return 0;
  10151. }

  10152. /* Handling opcode 110 insns.  */

  10153. static int
  10154. arm_record_unsupported_insn (insn_decode_record *arm_insn_r)
  10155. {
  10156.   printf_unfiltered (_("Process record does not support instruction "
  10157.                     "0x%0x at address %s.\n"),arm_insn_r->arm_insn,
  10158.                     paddress (arm_insn_r->gdbarch, arm_insn_r->this_addr));

  10159.   return -1;
  10160. }

  10161. /* Record handler for vector data transfer instructions.  */

  10162. static int
  10163. arm_record_vdata_transfer_insn (insn_decode_record *arm_insn_r)
  10164. {
  10165.   uint32_t bits_a, bit_c, bit_l, reg_t, reg_v;
  10166.   uint32_t record_buf[4];

  10167.   const int num_regs = gdbarch_num_regs (arm_insn_r->gdbarch);
  10168.   reg_t = bits (arm_insn_r->arm_insn, 12, 15);
  10169.   reg_v = bits (arm_insn_r->arm_insn, 21, 23);
  10170.   bits_a = bits (arm_insn_r->arm_insn, 21, 23);
  10171.   bit_l = bit (arm_insn_r->arm_insn, 20);
  10172.   bit_c = bit (arm_insn_r->arm_insn, 8);

  10173.   /* Handle VMOV instruction.  */
  10174.   if (bit_l && bit_c)
  10175.     {
  10176.       record_buf[0] = reg_t;
  10177.       arm_insn_r->reg_rec_count = 1;
  10178.     }
  10179.   else if (bit_l && !bit_c)
  10180.     {
  10181.       /* Handle VMOV instruction.  */
  10182.       if (bits_a == 0x00)
  10183.         {
  10184.           if (bit (arm_insn_r->arm_insn, 20))
  10185.             record_buf[0] = reg_t;
  10186.           else
  10187.             record_buf[0] = num_regs + (bit (arm_insn_r->arm_insn, 7) |
  10188.                             (reg_v << 1));

  10189.           arm_insn_r->reg_rec_count = 1;
  10190.         }
  10191.       /* Handle VMRS instruction.  */
  10192.       else if (bits_a == 0x07)
  10193.         {
  10194.           if (reg_t == 15)
  10195.             reg_t = ARM_PS_REGNUM;

  10196.           record_buf[0] = reg_t;
  10197.           arm_insn_r->reg_rec_count = 1;
  10198.         }
  10199.     }
  10200.   else if (!bit_l && !bit_c)
  10201.     {
  10202.       /* Handle VMOV instruction.  */
  10203.       if (bits_a == 0x00)
  10204.         {
  10205.           if (bit (arm_insn_r->arm_insn, 20))
  10206.             record_buf[0] = reg_t;
  10207.           else
  10208.             record_buf[0] = num_regs + (bit (arm_insn_r->arm_insn, 7) |
  10209.                             (reg_v << 1));

  10210.           arm_insn_r->reg_rec_count = 1;
  10211.         }
  10212.       /* Handle VMSR instruction.  */
  10213.       else if (bits_a == 0x07)
  10214.         {
  10215.           record_buf[0] = ARM_FPSCR_REGNUM;
  10216.           arm_insn_r->reg_rec_count = 1;
  10217.         }
  10218.     }
  10219.   else if (!bit_l && bit_c)
  10220.     {
  10221.       /* Handle VMOV instruction.  */
  10222.       if (!(bits_a & 0x04))
  10223.         {
  10224.           record_buf[0] = (reg_v | (bit (arm_insn_r->arm_insn, 7) << 4))
  10225.                           + ARM_D0_REGNUM;
  10226.           arm_insn_r->reg_rec_count = 1;
  10227.         }
  10228.       /* Handle VDUP instruction.  */
  10229.       else
  10230.         {
  10231.           if (bit (arm_insn_r->arm_insn, 21))
  10232.             {
  10233.               reg_v = reg_v | (bit (arm_insn_r->arm_insn, 7) << 4);
  10234.               record_buf[0] = reg_v + ARM_D0_REGNUM;
  10235.               record_buf[1] = reg_v + ARM_D0_REGNUM + 1;
  10236.               arm_insn_r->reg_rec_count = 2;
  10237.             }
  10238.           else
  10239.             {
  10240.               reg_v = reg_v | (bit (arm_insn_r->arm_insn, 7) << 4);
  10241.               record_buf[0] = reg_v + ARM_D0_REGNUM;
  10242.               arm_insn_r->reg_rec_count = 1;
  10243.             }
  10244.         }
  10245.     }

  10246.   REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
  10247.   return 0;
  10248. }

  10249. /* Record handler for extension register load/store instructions.  */

  10250. static int
  10251. arm_record_exreg_ld_st_insn (insn_decode_record *arm_insn_r)
  10252. {
  10253.   uint32_t opcode, single_reg;
  10254.   uint8_t op_vldm_vstm;
  10255.   uint32_t record_buf[8], record_buf_mem[128];
  10256.   ULONGEST u_regval = 0;

  10257.   struct regcache *reg_cache = arm_insn_r->regcache;
  10258.   const int num_regs = gdbarch_num_regs (arm_insn_r->gdbarch);

  10259.   opcode = bits (arm_insn_r->arm_insn, 20, 24);
  10260.   single_reg = bit (arm_insn_r->arm_insn, 8);
  10261.   op_vldm_vstm = opcode & 0x1b;

  10262.   /* Handle VMOV instructions.  */
  10263.   if ((opcode & 0x1e) == 0x04)
  10264.     {
  10265.       if (bit (arm_insn_r->arm_insn, 4))
  10266.         {
  10267.           record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
  10268.           record_buf[1] = bits (arm_insn_r->arm_insn, 16, 19);
  10269.           arm_insn_r->reg_rec_count = 2;
  10270.         }
  10271.       else
  10272.         {
  10273.           uint8_t reg_m = (bits (arm_insn_r->arm_insn, 0, 3) << 1)
  10274.                           | bit (arm_insn_r->arm_insn, 5);

  10275.           if (!single_reg)
  10276.             {
  10277.               record_buf[0] = num_regs + reg_m;
  10278.               record_buf[1] = num_regs + reg_m + 1;
  10279.               arm_insn_r->reg_rec_count = 2;
  10280.             }
  10281.           else
  10282.             {
  10283.               record_buf[0] = reg_m + ARM_D0_REGNUM;
  10284.               arm_insn_r->reg_rec_count = 1;
  10285.             }
  10286.         }
  10287.     }
  10288.   /* Handle VSTM and VPUSH instructions.  */
  10289.   else if (op_vldm_vstm == 0x08 || op_vldm_vstm == 0x0a
  10290.           || op_vldm_vstm == 0x12)
  10291.     {
  10292.       uint32_t start_address, reg_rn, imm_off32, imm_off8, memory_count;
  10293.       uint32_t memory_index = 0;

  10294.       reg_rn = bits (arm_insn_r->arm_insn, 16, 19);
  10295.       regcache_raw_read_unsigned (reg_cache, reg_rn, &u_regval);
  10296.       imm_off8 = bits (arm_insn_r->arm_insn, 0, 7);
  10297.       imm_off32 = imm_off8 << 24;
  10298.       memory_count = imm_off8;

  10299.       if (bit (arm_insn_r->arm_insn, 23))
  10300.         start_address = u_regval;
  10301.       else
  10302.         start_address = u_regval - imm_off32;

  10303.       if (bit (arm_insn_r->arm_insn, 21))
  10304.         {
  10305.           record_buf[0] = reg_rn;
  10306.           arm_insn_r->reg_rec_count = 1;
  10307.         }

  10308.       while (memory_count > 0)
  10309.         {
  10310.           if (!single_reg)
  10311.             {
  10312.               record_buf_mem[memory_index] = start_address;
  10313.               record_buf_mem[memory_index + 1] = 4;
  10314.               start_address = start_address + 4;
  10315.               memory_index = memory_index + 2;
  10316.             }
  10317.           else
  10318.             {
  10319.               record_buf_mem[memory_index] = start_address;
  10320.               record_buf_mem[memory_index + 1] = 4;
  10321.               record_buf_mem[memory_index + 2] = start_address + 4;
  10322.               record_buf_mem[memory_index + 3] = 4;
  10323.               start_address = start_address + 8;
  10324.               memory_index = memory_index + 4;
  10325.             }
  10326.           memory_count--;
  10327.         }
  10328.       arm_insn_r->mem_rec_count = (memory_index >> 1);
  10329.     }
  10330.   /* Handle VLDM instructions.  */
  10331.   else if (op_vldm_vstm == 0x09 || op_vldm_vstm == 0x0b
  10332.           || op_vldm_vstm == 0x13)
  10333.     {
  10334.       uint32_t reg_count, reg_vd;
  10335.       uint32_t reg_index = 0;

  10336.       reg_vd = bits (arm_insn_r->arm_insn, 12, 15);
  10337.       reg_count = bits (arm_insn_r->arm_insn, 0, 7);

  10338.       if (single_reg)
  10339.         reg_vd = reg_vd | (bit (arm_insn_r->arm_insn, 22) << 4);
  10340.       else
  10341.         reg_vd = (reg_vd << 1) | bit (arm_insn_r->arm_insn, 22);

  10342.       if (bit (arm_insn_r->arm_insn, 21))
  10343.         record_buf[reg_index++] = bits (arm_insn_r->arm_insn, 16, 19);

  10344.       while (reg_count > 0)
  10345.         {
  10346.           if (single_reg)
  10347.               record_buf[reg_index++] = num_regs + reg_vd + reg_count - 1;
  10348.           else
  10349.               record_buf[reg_index++] = ARM_D0_REGNUM + reg_vd + reg_count - 1;

  10350.           reg_count--;
  10351.         }
  10352.       arm_insn_r->reg_rec_count = reg_index;
  10353.     }
  10354.   /* VSTR Vector store register.  */
  10355.   else if ((opcode & 0x13) == 0x10)
  10356.     {
  10357.       uint32_t start_address, reg_rn, imm_off32, imm_off8, memory_count;
  10358.       uint32_t memory_index = 0;

  10359.       reg_rn = bits (arm_insn_r->arm_insn, 16, 19);
  10360.       regcache_raw_read_unsigned (reg_cache, reg_rn, &u_regval);
  10361.       imm_off8 = bits (arm_insn_r->arm_insn, 0, 7);
  10362.       imm_off32 = imm_off8 << 24;
  10363.       memory_count = imm_off8;

  10364.       if (bit (arm_insn_r->arm_insn, 23))
  10365.         start_address = u_regval + imm_off32;
  10366.       else
  10367.         start_address = u_regval - imm_off32;

  10368.       if (single_reg)
  10369.         {
  10370.           record_buf_mem[memory_index] = start_address;
  10371.           record_buf_mem[memory_index + 1] = 4;
  10372.           arm_insn_r->mem_rec_count = 1;
  10373.         }
  10374.       else
  10375.         {
  10376.           record_buf_mem[memory_index] = start_address;
  10377.           record_buf_mem[memory_index + 1] = 4;
  10378.           record_buf_mem[memory_index + 2] = start_address + 4;
  10379.           record_buf_mem[memory_index + 3] = 4;
  10380.           arm_insn_r->mem_rec_count = 2;
  10381.         }
  10382.     }
  10383.   /* VLDR Vector load register.  */
  10384.   else if ((opcode & 0x13) == 0x11)
  10385.     {
  10386.       uint32_t reg_vd = bits (arm_insn_r->arm_insn, 12, 15);

  10387.       if (!single_reg)
  10388.         {
  10389.           reg_vd = reg_vd | (bit (arm_insn_r->arm_insn, 22) << 4);
  10390.           record_buf[0] = ARM_D0_REGNUM + reg_vd;
  10391.         }
  10392.       else
  10393.         {
  10394.           reg_vd = (reg_vd << 1) | bit (arm_insn_r->arm_insn, 22);
  10395.           record_buf[0] = num_regs + reg_vd;
  10396.         }
  10397.       arm_insn_r->reg_rec_count = 1;
  10398.     }

  10399.   REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
  10400.   MEM_ALLOC (arm_insn_r->arm_mems, arm_insn_r->mem_rec_count, record_buf_mem);
  10401.   return 0;
  10402. }

  10403. /* Record handler for arm/thumb mode VFP data processing instructions.  */

  10404. static int
  10405. arm_record_vfp_data_proc_insn (insn_decode_record *arm_insn_r)
  10406. {
  10407.   uint32_t opc1, opc2, opc3, dp_op_sz, bit_d, reg_vd;
  10408.   uint32_t record_buf[4];
  10409.   enum insn_types {INSN_T0, INSN_T1, INSN_T2, INSN_T3, INSN_INV};
  10410.   enum insn_types curr_insn_type = INSN_INV;

  10411.   reg_vd = bits (arm_insn_r->arm_insn, 12, 15);
  10412.   opc1 = bits (arm_insn_r->arm_insn, 20, 23);
  10413.   opc2 = bits (arm_insn_r->arm_insn, 16, 19);
  10414.   opc3 = bits (arm_insn_r->arm_insn, 6, 7);
  10415.   dp_op_sz = bit (arm_insn_r->arm_insn, 8);
  10416.   bit_d = bit (arm_insn_r->arm_insn, 22);
  10417.   opc1 = opc1 & 0x04;

  10418.   /* Handle VMLA, VMLS.  */
  10419.   if (opc1 == 0x00)
  10420.     {
  10421.       if (bit (arm_insn_r->arm_insn, 10))
  10422.         {
  10423.           if (bit (arm_insn_r->arm_insn, 6))
  10424.             curr_insn_type = INSN_T0;
  10425.           else
  10426.             curr_insn_type = INSN_T1;
  10427.         }
  10428.       else
  10429.         {
  10430.           if (dp_op_sz)
  10431.             curr_insn_type = INSN_T1;
  10432.           else
  10433.             curr_insn_type = INSN_T2;
  10434.         }
  10435.     }
  10436.   /* Handle VNMLA, VNMLS, VNMUL.  */
  10437.   else if (opc1 == 0x01)
  10438.     {
  10439.       if (dp_op_sz)
  10440.         curr_insn_type = INSN_T1;
  10441.       else
  10442.         curr_insn_type = INSN_T2;
  10443.     }
  10444.   /* Handle VMUL.  */
  10445.   else if (opc1 == 0x02 && !(opc3 & 0x01))
  10446.     {
  10447.       if (bit (arm_insn_r->arm_insn, 10))
  10448.         {
  10449.           if (bit (arm_insn_r->arm_insn, 6))
  10450.             curr_insn_type = INSN_T0;
  10451.           else
  10452.             curr_insn_type = INSN_T1;
  10453.         }
  10454.       else
  10455.         {
  10456.           if (dp_op_sz)
  10457.             curr_insn_type = INSN_T1;
  10458.           else
  10459.             curr_insn_type = INSN_T2;
  10460.         }
  10461.     }
  10462.   /* Handle VADD, VSUB.  */
  10463.   else if (opc1 == 0x03)
  10464.     {
  10465.       if (!bit (arm_insn_r->arm_insn, 9))
  10466.         {
  10467.           if (bit (arm_insn_r->arm_insn, 6))
  10468.             curr_insn_type = INSN_T0;
  10469.           else
  10470.             curr_insn_type = INSN_T1;
  10471.         }
  10472.       else
  10473.         {
  10474.           if (dp_op_sz)
  10475.             curr_insn_type = INSN_T1;
  10476.           else
  10477.             curr_insn_type = INSN_T2;
  10478.         }
  10479.     }
  10480.   /* Handle VDIV.  */
  10481.   else if (opc1 == 0x0b)
  10482.     {
  10483.       if (dp_op_sz)
  10484.         curr_insn_type = INSN_T1;
  10485.       else
  10486.         curr_insn_type = INSN_T2;
  10487.     }
  10488.   /* Handle all other vfp data processing instructions.  */
  10489.   else if (opc1 == 0x0b)
  10490.     {
  10491.       /* Handle VMOV.  */
  10492.       if (!(opc3 & 0x01) || (opc2 == 0x00 && opc3 == 0x01))
  10493.         {
  10494.           if (bit (arm_insn_r->arm_insn, 4))
  10495.             {
  10496.               if (bit (arm_insn_r->arm_insn, 6))
  10497.                 curr_insn_type = INSN_T0;
  10498.               else
  10499.                 curr_insn_type = INSN_T1;
  10500.             }
  10501.           else
  10502.             {
  10503.               if (dp_op_sz)
  10504.                 curr_insn_type = INSN_T1;
  10505.               else
  10506.                 curr_insn_type = INSN_T2;
  10507.             }
  10508.         }
  10509.       /* Handle VNEG and VABS.  */
  10510.       else if ((opc2 == 0x01 && opc3 == 0x01)
  10511.               || (opc2 == 0x00 && opc3 == 0x03))
  10512.         {
  10513.           if (!bit (arm_insn_r->arm_insn, 11))
  10514.             {
  10515.               if (bit (arm_insn_r->arm_insn, 6))
  10516.                 curr_insn_type = INSN_T0;
  10517.               else
  10518.                 curr_insn_type = INSN_T1;
  10519.             }
  10520.           else
  10521.             {
  10522.               if (dp_op_sz)
  10523.                 curr_insn_type = INSN_T1;
  10524.               else
  10525.                 curr_insn_type = INSN_T2;
  10526.             }
  10527.         }
  10528.       /* Handle VSQRT.  */
  10529.       else if (opc2 == 0x01 && opc3 == 0x03)
  10530.         {
  10531.           if (dp_op_sz)
  10532.             curr_insn_type = INSN_T1;
  10533.           else
  10534.             curr_insn_type = INSN_T2;
  10535.         }
  10536.       /* Handle VCVT.  */
  10537.       else if (opc2 == 0x07 && opc3 == 0x03)
  10538.         {
  10539.           if (!dp_op_sz)
  10540.             curr_insn_type = INSN_T1;
  10541.           else
  10542.             curr_insn_type = INSN_T2;
  10543.         }
  10544.       else if (opc3 & 0x01)
  10545.         {
  10546.           /* Handle VCVT.  */
  10547.           if ((opc2 == 0x08) || (opc2 & 0x0e) == 0x0c)
  10548.             {
  10549.               if (!bit (arm_insn_r->arm_insn, 18))
  10550.                 curr_insn_type = INSN_T2;
  10551.               else
  10552.                 {
  10553.                   if (dp_op_sz)
  10554.                     curr_insn_type = INSN_T1;
  10555.                   else
  10556.                     curr_insn_type = INSN_T2;
  10557.                 }
  10558.             }
  10559.           /* Handle VCVT.  */
  10560.           else if ((opc2 & 0x0e) == 0x0a || (opc2 & 0x0e) == 0x0e)
  10561.             {
  10562.               if (dp_op_sz)
  10563.                 curr_insn_type = INSN_T1;
  10564.               else
  10565.                 curr_insn_type = INSN_T2;
  10566.             }
  10567.           /* Handle VCVTB, VCVTT.  */
  10568.           else if ((opc2 & 0x0e) == 0x02)
  10569.             curr_insn_type = INSN_T2;
  10570.           /* Handle VCMP, VCMPE.  */
  10571.           else if ((opc2 & 0x0e) == 0x04)
  10572.             curr_insn_type = INSN_T3;
  10573.         }
  10574.     }

  10575.   switch (curr_insn_type)
  10576.     {
  10577.       case INSN_T0:
  10578.         reg_vd = reg_vd | (bit_d << 4);
  10579.         record_buf[0] = reg_vd + ARM_D0_REGNUM;
  10580.         record_buf[1] = reg_vd + ARM_D0_REGNUM + 1;
  10581.         arm_insn_r->reg_rec_count = 2;
  10582.         break;

  10583.       case INSN_T1:
  10584.         reg_vd = reg_vd | (bit_d << 4);
  10585.         record_buf[0] = reg_vd + ARM_D0_REGNUM;
  10586.         arm_insn_r->reg_rec_count = 1;
  10587.         break;

  10588.       case INSN_T2:
  10589.         reg_vd = (reg_vd << 1) | bit_d;
  10590.         record_buf[0] = reg_vd + ARM_D0_REGNUM;
  10591.         arm_insn_r->reg_rec_count = 1;
  10592.         break;

  10593.       case INSN_T3:
  10594.         record_buf[0] = ARM_FPSCR_REGNUM;
  10595.         arm_insn_r->reg_rec_count = 1;
  10596.         break;

  10597.       default:
  10598.         gdb_assert_not_reached ("no decoding pattern found");
  10599.         break;
  10600.     }

  10601.   REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
  10602.   return 0;
  10603. }

  10604. /* Handling opcode 110 insns.  */

  10605. static int
  10606. arm_record_asimd_vfp_coproc (insn_decode_record *arm_insn_r)
  10607. {
  10608.   uint32_t op, op1, op1_sbit, op1_ebit, coproc;

  10609.   coproc = bits (arm_insn_r->arm_insn, 8, 11);
  10610.   op1 = bits (arm_insn_r->arm_insn, 20, 25);
  10611.   op1_ebit = bit (arm_insn_r->arm_insn, 20);

  10612.   if ((coproc & 0x0e) == 0x0a)
  10613.     {
  10614.       /* Handle extension register ld/st instructions.  */
  10615.       if (!(op1 & 0x20))
  10616.         return arm_record_exreg_ld_st_insn (arm_insn_r);

  10617.       /* 64-bit transfers between arm core and extension registers.  */
  10618.       if ((op1 & 0x3e) == 0x04)
  10619.         return arm_record_exreg_ld_st_insn (arm_insn_r);
  10620.     }
  10621.   else
  10622.     {
  10623.       /* Handle coprocessor ld/st instructions.  */
  10624.       if (!(op1 & 0x3a))
  10625.         {
  10626.           /* Store.  */
  10627.           if (!op1_ebit)
  10628.             return arm_record_unsupported_insn (arm_insn_r);
  10629.           else
  10630.             /* Load.  */
  10631.             return arm_record_unsupported_insn (arm_insn_r);
  10632.         }

  10633.       /* Move to coprocessor from two arm core registers.  */
  10634.       if (op1 == 0x4)
  10635.         return arm_record_unsupported_insn (arm_insn_r);

  10636.       /* Move to two arm core registers from coprocessor.  */
  10637.       if (op1 == 0x5)
  10638.         {
  10639.           uint32_t reg_t[2];

  10640.           reg_t[0] = bits (arm_insn_r->arm_insn, 12, 15);
  10641.           reg_t[1] = bits (arm_insn_r->arm_insn, 16, 19);
  10642.           arm_insn_r->reg_rec_count = 2;

  10643.           REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, reg_t);
  10644.           return 0;
  10645.        }
  10646.     }
  10647.   return arm_record_unsupported_insn (arm_insn_r);
  10648. }

  10649. /* Handling opcode 111 insns.  */

  10650. static int
  10651. arm_record_coproc_data_proc (insn_decode_record *arm_insn_r)
  10652. {
  10653.   uint32_t op, op1_sbit, op1_ebit, coproc;
  10654.   struct gdbarch_tdep *tdep = gdbarch_tdep (arm_insn_r->gdbarch);
  10655.   struct regcache *reg_cache = arm_insn_r->regcache;
  10656.   ULONGEST u_regval = 0;

  10657.   arm_insn_r->opcode = bits (arm_insn_r->arm_insn, 24, 27);
  10658.   coproc = bits (arm_insn_r->arm_insn, 8, 11);
  10659.   op1_sbit = bit (arm_insn_r->arm_insn, 24);
  10660.   op1_ebit = bit (arm_insn_r->arm_insn, 20);
  10661.   op = bit (arm_insn_r->arm_insn, 4);

  10662.   /* Handle arm SWI/SVC system call instructions.  */
  10663.   if (op1_sbit)
  10664.     {
  10665.       if (tdep->arm_syscall_record != NULL)
  10666.         {
  10667.           ULONGEST svc_operand, svc_number;

  10668.           svc_operand = (0x00ffffff & arm_insn_r->arm_insn);

  10669.           if (svc_operand)  /* OABI.  */
  10670.             svc_number = svc_operand - 0x900000;
  10671.           else /* EABI.  */
  10672.             regcache_raw_read_unsigned (reg_cache, 7, &svc_number);

  10673.           return tdep->arm_syscall_record (reg_cache, svc_number);
  10674.         }
  10675.       else
  10676.         {
  10677.           printf_unfiltered (_("no syscall record support\n"));
  10678.           return -1;
  10679.         }
  10680.     }

  10681.   if ((coproc & 0x0e) == 0x0a)
  10682.     {
  10683.       /* VFP data-processing instructions.  */
  10684.       if (!op1_sbit && !op)
  10685.         return arm_record_vfp_data_proc_insn (arm_insn_r);

  10686.       /* Advanced SIMD, VFP instructions.  */
  10687.       if (!op1_sbit && op)
  10688.         return arm_record_vdata_transfer_insn (arm_insn_r);
  10689.     }
  10690.   else
  10691.     {
  10692.       /* Coprocessor data operations.  */
  10693.       if (!op1_sbit && !op)
  10694.         return arm_record_unsupported_insn (arm_insn_r);

  10695.       /* Move to Coprocessor from ARM core register.  */
  10696.       if (!op1_sbit && !op1_ebit && op)
  10697.         return arm_record_unsupported_insn (arm_insn_r);

  10698.       /* Move to arm core register from coprocessor.  */
  10699.       if (!op1_sbit && op1_ebit && op)
  10700.         {
  10701.           uint32_t record_buf[1];

  10702.           record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
  10703.           if (record_buf[0] == 15)
  10704.             record_buf[0] = ARM_PS_REGNUM;

  10705.           arm_insn_r->reg_rec_count = 1;
  10706.           REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count,
  10707.                      record_buf);
  10708.           return 0;
  10709.         }
  10710.     }

  10711.   return arm_record_unsupported_insn (arm_insn_r);
  10712. }

  10713. /* Handling opcode 000 insns.  */

  10714. static int
  10715. thumb_record_shift_add_sub (insn_decode_record *thumb_insn_r)
  10716. {
  10717.   uint32_t record_buf[8];
  10718.   uint32_t reg_src1 = 0;

  10719.   reg_src1 = bits (thumb_insn_r->arm_insn, 0, 2);

  10720.   record_buf[0] = ARM_PS_REGNUM;
  10721.   record_buf[1] = reg_src1;
  10722.   thumb_insn_r->reg_rec_count = 2;

  10723.   REG_ALLOC (thumb_insn_r->arm_regs, thumb_insn_r->reg_rec_count, record_buf);

  10724.   return 0;
  10725. }


  10726. /* Handling opcode 001 insns.  */

  10727. static int
  10728. thumb_record_add_sub_cmp_mov (insn_decode_record *thumb_insn_r)
  10729. {
  10730.   uint32_t record_buf[8];
  10731.   uint32_t reg_src1 = 0;

  10732.   reg_src1 = bits (thumb_insn_r->arm_insn, 8, 10);

  10733.   record_buf[0] = ARM_PS_REGNUM;
  10734.   record_buf[1] = reg_src1;
  10735.   thumb_insn_r->reg_rec_count = 2;

  10736.   REG_ALLOC (thumb_insn_r->arm_regs, thumb_insn_r->reg_rec_count, record_buf);

  10737.   return 0;
  10738. }

  10739. /* Handling opcode 010 insns.  */

  10740. static int
  10741. thumb_record_ld_st_reg_offset (insn_decode_record *thumb_insn_r)
  10742. {
  10743.   struct regcache *reg_cache =  thumb_insn_r->regcache;
  10744.   uint32_t record_buf[8], record_buf_mem[8];

  10745.   uint32_t reg_src1 = 0, reg_src2 = 0;
  10746.   uint32_t opcode1 = 0, opcode2 = 0, opcode3 = 0;

  10747.   ULONGEST u_regval[2] = {0};

  10748.   opcode1 = bits (thumb_insn_r->arm_insn, 10, 12);

  10749.   if (bit (thumb_insn_r->arm_insn, 12))
  10750.     {
  10751.       /* Handle load/store register offset.  */
  10752.       opcode2 = bits (thumb_insn_r->arm_insn, 9, 10);
  10753.       if (opcode2 >= 12 && opcode2 <= 15)
  10754.         {
  10755.           /* LDR(2), LDRB(2) , LDRH(2), LDRSB, LDRSH.  */
  10756.           reg_src1 = bits (thumb_insn_r->arm_insn,0, 2);
  10757.           record_buf[0] = reg_src1;
  10758.           thumb_insn_r->reg_rec_count = 1;
  10759.         }
  10760.       else if (opcode2 >= 8 && opcode2 <= 10)
  10761.         {
  10762.           /* STR(2), STRB(2), STRH(2) .  */
  10763.           reg_src1 = bits (thumb_insn_r->arm_insn, 3, 5);
  10764.           reg_src2 = bits (thumb_insn_r->arm_insn, 6, 8);
  10765.           regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval[0]);
  10766.           regcache_raw_read_unsigned (reg_cache, reg_src2, &u_regval[1]);
  10767.           if (8 == opcode2)
  10768.             record_buf_mem[0] = 4;    /* STR (2).  */
  10769.           else if (10 == opcode2)
  10770.             record_buf_mem[0] = 1;    /*  STRB (2).  */
  10771.           else if (9 == opcode2)
  10772.             record_buf_mem[0] = 2;    /* STRH (2).  */
  10773.           record_buf_mem[1] = u_regval[0] + u_regval[1];
  10774.           thumb_insn_r->mem_rec_count = 1;
  10775.         }
  10776.     }
  10777.   else if (bit (thumb_insn_r->arm_insn, 11))
  10778.     {
  10779.       /* Handle load from literal pool.  */
  10780.       /* LDR(3).  */
  10781.       reg_src1 = bits (thumb_insn_r->arm_insn, 8, 10);
  10782.       record_buf[0] = reg_src1;
  10783.       thumb_insn_r->reg_rec_count = 1;
  10784.     }
  10785.   else if (opcode1)
  10786.     {
  10787.       opcode2 = bits (thumb_insn_r->arm_insn, 8, 9);
  10788.       opcode3 = bits (thumb_insn_r->arm_insn, 0, 2);
  10789.       if ((3 == opcode2) && (!opcode3))
  10790.         {
  10791.           /* Branch with exchange.  */
  10792.           record_buf[0] = ARM_PS_REGNUM;
  10793.           thumb_insn_r->reg_rec_count = 1;
  10794.         }
  10795.       else
  10796.         {
  10797.           /* Format 8; special data processing insns.  */
  10798.           reg_src1 = bits (thumb_insn_r->arm_insn, 0, 2);
  10799.           record_buf[0] = ARM_PS_REGNUM;
  10800.           record_buf[1] = reg_src1;
  10801.           thumb_insn_r->reg_rec_count = 2;
  10802.         }
  10803.     }
  10804.   else
  10805.     {
  10806.       /* Format 5; data processing insns.  */
  10807.       reg_src1 = bits (thumb_insn_r->arm_insn, 0, 2);
  10808.       if (bit (thumb_insn_r->arm_insn, 7))
  10809.         {
  10810.           reg_src1 = reg_src1 + 8;
  10811.         }
  10812.       record_buf[0] = ARM_PS_REGNUM;
  10813.       record_buf[1] = reg_src1;
  10814.       thumb_insn_r->reg_rec_count = 2;
  10815.     }

  10816.   REG_ALLOC (thumb_insn_r->arm_regs, thumb_insn_r->reg_rec_count, record_buf);
  10817.   MEM_ALLOC (thumb_insn_r->arm_mems, thumb_insn_r->mem_rec_count,
  10818.              record_buf_mem);

  10819.   return 0;
  10820. }

  10821. /* Handling opcode 001 insns.  */

  10822. static int
  10823. thumb_record_ld_st_imm_offset (insn_decode_record *thumb_insn_r)
  10824. {
  10825.   struct regcache *reg_cache = thumb_insn_r->regcache;
  10826.   uint32_t record_buf[8], record_buf_mem[8];

  10827.   uint32_t reg_src1 = 0;
  10828.   uint32_t opcode = 0, immed_5 = 0;

  10829.   ULONGEST u_regval = 0;

  10830.   opcode = bits (thumb_insn_r->arm_insn, 11, 12);

  10831.   if (opcode)
  10832.     {
  10833.       /* LDR(1).  */
  10834.       reg_src1 = bits (thumb_insn_r->arm_insn, 0, 2);
  10835.       record_buf[0] = reg_src1;
  10836.       thumb_insn_r->reg_rec_count = 1;
  10837.     }
  10838.   else
  10839.     {
  10840.       /* STR(1).  */
  10841.       reg_src1 = bits (thumb_insn_r->arm_insn, 3, 5);
  10842.       immed_5 = bits (thumb_insn_r->arm_insn, 6, 10);
  10843.       regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval);
  10844.       record_buf_mem[0] = 4;
  10845.       record_buf_mem[1] = u_regval + (immed_5 * 4);
  10846.       thumb_insn_r->mem_rec_count = 1;
  10847.     }

  10848.   REG_ALLOC (thumb_insn_r->arm_regs, thumb_insn_r->reg_rec_count, record_buf);
  10849.   MEM_ALLOC (thumb_insn_r->arm_mems, thumb_insn_r->mem_rec_count,
  10850.              record_buf_mem);

  10851.   return 0;
  10852. }

  10853. /* Handling opcode 100 insns.  */

  10854. static int
  10855. thumb_record_ld_st_stack (insn_decode_record *thumb_insn_r)
  10856. {
  10857.   struct regcache *reg_cache = thumb_insn_r->regcache;
  10858.   uint32_t record_buf[8], record_buf_mem[8];

  10859.   uint32_t reg_src1 = 0;
  10860.   uint32_t opcode = 0, immed_8 = 0, immed_5 = 0;

  10861.   ULONGEST u_regval = 0;

  10862.   opcode = bits (thumb_insn_r->arm_insn, 11, 12);

  10863.   if (3 == opcode)
  10864.     {
  10865.       /* LDR(4).  */
  10866.       reg_src1 = bits (thumb_insn_r->arm_insn, 8, 10);
  10867.       record_buf[0] = reg_src1;
  10868.       thumb_insn_r->reg_rec_count = 1;
  10869.     }
  10870.   else if (1 == opcode)
  10871.     {
  10872.       /* LDRH(1).  */
  10873.       reg_src1 = bits (thumb_insn_r->arm_insn, 0, 2);
  10874.       record_buf[0] = reg_src1;
  10875.       thumb_insn_r->reg_rec_count = 1;
  10876.     }
  10877.   else if (2 == opcode)
  10878.     {
  10879.       /* STR(3).  */
  10880.       immed_8 = bits (thumb_insn_r->arm_insn, 0, 7);
  10881.       regcache_raw_read_unsigned (reg_cache, ARM_SP_REGNUM, &u_regval);
  10882.       record_buf_mem[0] = 4;
  10883.       record_buf_mem[1] = u_regval + (immed_8 * 4);
  10884.       thumb_insn_r->mem_rec_count = 1;
  10885.     }
  10886.   else if (0 == opcode)
  10887.     {
  10888.       /* STRH(1).  */
  10889.       immed_5 = bits (thumb_insn_r->arm_insn, 6, 10);
  10890.       reg_src1 = bits (thumb_insn_r->arm_insn, 3, 5);
  10891.       regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval);
  10892.       record_buf_mem[0] = 2;
  10893.       record_buf_mem[1] = u_regval + (immed_5 * 2);
  10894.       thumb_insn_r->mem_rec_count = 1;
  10895.     }

  10896.   REG_ALLOC (thumb_insn_r->arm_regs, thumb_insn_r->reg_rec_count, record_buf);
  10897.   MEM_ALLOC (thumb_insn_r->arm_mems, thumb_insn_r->mem_rec_count,
  10898.              record_buf_mem);

  10899.   return 0;
  10900. }

  10901. /* Handling opcode 101 insns.  */

  10902. static int
  10903. thumb_record_misc (insn_decode_record *thumb_insn_r)
  10904. {
  10905.   struct regcache *reg_cache = thumb_insn_r->regcache;

  10906.   uint32_t opcode = 0, opcode1 = 0, opcode2 = 0;
  10907.   uint32_t register_bits = 0, register_count = 0;
  10908.   uint32_t register_list[8] = {0}, index = 0, start_address = 0;
  10909.   uint32_t record_buf[24], record_buf_mem[48];
  10910.   uint32_t reg_src1;

  10911.   ULONGEST u_regval = 0;

  10912.   opcode = bits (thumb_insn_r->arm_insn, 11, 12);
  10913.   opcode1 = bits (thumb_insn_r->arm_insn, 8, 12);
  10914.   opcode2 = bits (thumb_insn_r->arm_insn, 9, 12);

  10915.   if (14 == opcode2)
  10916.     {
  10917.       /* POP.  */
  10918.       register_bits = bits (thumb_insn_r->arm_insn, 0, 7);
  10919.       while (register_bits)
  10920.       {
  10921.         if (register_bits & 0x00000001)
  10922.           record_buf[index++] = register_count;
  10923.         register_bits = register_bits >> 1;
  10924.         register_count++;
  10925.       }
  10926.       record_buf[index++] = ARM_PS_REGNUM;
  10927.       record_buf[index++] = ARM_SP_REGNUM;
  10928.       thumb_insn_r->reg_rec_count = index;
  10929.     }
  10930.   else if (10 == opcode2)
  10931.     {
  10932.       /* PUSH.  */
  10933.       register_bits = bits (thumb_insn_r->arm_insn, 0, 7);
  10934.       regcache_raw_read_unsigned (reg_cache, ARM_SP_REGNUM, &u_regval);
  10935.       while (register_bits)
  10936.         {
  10937.           if (register_bits & 0x00000001)
  10938.             register_count++;
  10939.           register_bits = register_bits >> 1;
  10940.         }
  10941.       start_address = u_regval -  \
  10942.                   (4 * (bit (thumb_insn_r->arm_insn, 8) + register_count));
  10943.       thumb_insn_r->mem_rec_count = register_count;
  10944.       while (register_count)
  10945.         {
  10946.           record_buf_mem[(register_count * 2) - 1] = start_address;
  10947.           record_buf_mem[(register_count * 2) - 2] = 4;
  10948.           start_address = start_address + 4;
  10949.           register_count--;
  10950.         }
  10951.       record_buf[0] = ARM_SP_REGNUM;
  10952.       thumb_insn_r->reg_rec_count = 1;
  10953.     }
  10954.   else if (0x1E == opcode1)
  10955.     {
  10956.       /* BKPT insn.  */
  10957.       /* Handle enhanced software breakpoint insn, BKPT.  */
  10958.       /* CPSR is changed to be executed in ARM state,  disabling normal
  10959.          interrupts, entering abort mode.  */
  10960.       /* According to high vector configuration PC is set.  */
  10961.       /* User hits breakpoint and type reverse, in that case, we need to go back with
  10962.       previous CPSR and Program Counter.  */
  10963.       record_buf[0] = ARM_PS_REGNUM;
  10964.       record_buf[1] = ARM_LR_REGNUM;
  10965.       thumb_insn_r->reg_rec_count = 2;
  10966.       /* We need to save SPSR value, which is not yet done.  */
  10967.       printf_unfiltered (_("Process record does not support instruction "
  10968.                            "0x%0x at address %s.\n"),
  10969.                            thumb_insn_r->arm_insn,
  10970.                            paddress (thumb_insn_r->gdbarch,
  10971.                            thumb_insn_r->this_addr));
  10972.       return -1;
  10973.     }
  10974.   else if ((0 == opcode) || (1 == opcode))
  10975.     {
  10976.       /* ADD(5), ADD(6).  */
  10977.       reg_src1 = bits (thumb_insn_r->arm_insn, 8, 10);
  10978.       record_buf[0] = reg_src1;
  10979.       thumb_insn_r->reg_rec_count = 1;
  10980.     }
  10981.   else if (2 == opcode)
  10982.     {
  10983.       /* ADD(7), SUB(4).  */
  10984.       reg_src1 = bits (thumb_insn_r->arm_insn, 8, 10);
  10985.       record_buf[0] = ARM_SP_REGNUM;
  10986.       thumb_insn_r->reg_rec_count = 1;
  10987.     }

  10988.   REG_ALLOC (thumb_insn_r->arm_regs, thumb_insn_r->reg_rec_count, record_buf);
  10989.   MEM_ALLOC (thumb_insn_r->arm_mems, thumb_insn_r->mem_rec_count,
  10990.              record_buf_mem);

  10991.   return 0;
  10992. }

  10993. /* Handling opcode 110 insns.  */

  10994. static int
  10995. thumb_record_ldm_stm_swi (insn_decode_record *thumb_insn_r)
  10996. {
  10997.   struct gdbarch_tdep *tdep = gdbarch_tdep (thumb_insn_r->gdbarch);
  10998.   struct regcache *reg_cache = thumb_insn_r->regcache;

  10999.   uint32_t ret = 0; /* function return value: -1:record failure ;  0:success  */
  11000.   uint32_t reg_src1 = 0;
  11001.   uint32_t opcode1 = 0, opcode2 = 0, register_bits = 0, register_count = 0;
  11002.   uint32_t register_list[8] = {0}, index = 0, start_address = 0;
  11003.   uint32_t record_buf[24], record_buf_mem[48];

  11004.   ULONGEST u_regval = 0;

  11005.   opcode1 = bits (thumb_insn_r->arm_insn, 8, 12);
  11006.   opcode2 = bits (thumb_insn_r->arm_insn, 11, 12);

  11007.   if (1 == opcode2)
  11008.     {

  11009.       /* LDMIA.  */
  11010.       register_bits = bits (thumb_insn_r->arm_insn, 0, 7);
  11011.       /* Get Rn.  */
  11012.       reg_src1 = bits (thumb_insn_r->arm_insn, 8, 10);
  11013.       while (register_bits)
  11014.         {
  11015.           if (register_bits & 0x00000001)
  11016.             record_buf[index++] = register_count;
  11017.           register_bits = register_bits >> 1;
  11018.           register_count++;
  11019.         }
  11020.       record_buf[index++] = reg_src1;
  11021.       thumb_insn_r->reg_rec_count = index;
  11022.     }
  11023.   else if (0 == opcode2)
  11024.     {
  11025.       /* It handles both STMIA.  */
  11026.       register_bits = bits (thumb_insn_r->arm_insn, 0, 7);
  11027.       /* Get Rn.  */
  11028.       reg_src1 = bits (thumb_insn_r->arm_insn, 8, 10);
  11029.       regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval);
  11030.       while (register_bits)
  11031.         {
  11032.           if (register_bits & 0x00000001)
  11033.             register_count++;
  11034.           register_bits = register_bits >> 1;
  11035.         }
  11036.       start_address = u_regval;
  11037.       thumb_insn_r->mem_rec_count = register_count;
  11038.       while (register_count)
  11039.         {
  11040.           record_buf_mem[(register_count * 2) - 1] = start_address;
  11041.           record_buf_mem[(register_count * 2) - 2] = 4;
  11042.           start_address = start_address + 4;
  11043.           register_count--;
  11044.         }
  11045.     }
  11046.   else if (0x1F == opcode1)
  11047.     {
  11048.         /* Handle arm syscall insn.  */
  11049.         if (tdep->arm_syscall_record != NULL)
  11050.           {
  11051.             regcache_raw_read_unsigned (reg_cache, 7, &u_regval);
  11052.             ret = tdep->arm_syscall_record (reg_cache, u_regval);
  11053.           }
  11054.         else
  11055.           {
  11056.             printf_unfiltered (_("no syscall record support\n"));
  11057.             return -1;
  11058.           }
  11059.     }

  11060.   /* B (1), conditional branch is automatically taken care in process_record,
  11061.     as PC is saved there.  */

  11062.   REG_ALLOC (thumb_insn_r->arm_regs, thumb_insn_r->reg_rec_count, record_buf);
  11063.   MEM_ALLOC (thumb_insn_r->arm_mems, thumb_insn_r->mem_rec_count,
  11064.              record_buf_mem);

  11065.   return ret;
  11066. }

  11067. /* Handling opcode 111 insns.  */

  11068. static int
  11069. thumb_record_branch (insn_decode_record *thumb_insn_r)
  11070. {
  11071.   uint32_t record_buf[8];
  11072.   uint32_t bits_h = 0;

  11073.   bits_h = bits (thumb_insn_r->arm_insn, 11, 12);

  11074.   if (2 == bits_h || 3 == bits_h)
  11075.     {
  11076.       /* BL */
  11077.       record_buf[0] = ARM_LR_REGNUM;
  11078.       thumb_insn_r->reg_rec_count = 1;
  11079.     }
  11080.   else if (1 == bits_h)
  11081.     {
  11082.       /* BLX(1). */
  11083.       record_buf[0] = ARM_PS_REGNUM;
  11084.       record_buf[1] = ARM_LR_REGNUM;
  11085.       thumb_insn_r->reg_rec_count = 2;
  11086.     }

  11087.   /* B(2) is automatically taken care in process_record, as PC is
  11088.      saved there.  */

  11089.   REG_ALLOC (thumb_insn_r->arm_regs, thumb_insn_r->reg_rec_count, record_buf);

  11090.   return 0;
  11091. }

  11092. /* Handler for thumb2 load/store multiple instructions.  */

  11093. static int
  11094. thumb2_record_ld_st_multiple (insn_decode_record *thumb2_insn_r)
  11095. {
  11096.   struct regcache *reg_cache = thumb2_insn_r->regcache;

  11097.   uint32_t reg_rn, op;
  11098.   uint32_t register_bits = 0, register_count = 0;
  11099.   uint32_t index = 0, start_address = 0;
  11100.   uint32_t record_buf[24], record_buf_mem[48];

  11101.   ULONGEST u_regval = 0;

  11102.   reg_rn = bits (thumb2_insn_r->arm_insn, 16, 19);
  11103.   op = bits (thumb2_insn_r->arm_insn, 23, 24);

  11104.   if (0 == op || 3 == op)
  11105.     {
  11106.       if (bit (thumb2_insn_r->arm_insn, INSN_S_L_BIT_NUM))
  11107.         {
  11108.           /* Handle RFE instruction.  */
  11109.           record_buf[0] = ARM_PS_REGNUM;
  11110.           thumb2_insn_r->reg_rec_count = 1;
  11111.         }
  11112.       else
  11113.         {
  11114.           /* Handle SRS instruction after reading banked SP.  */
  11115.           return arm_record_unsupported_insn (thumb2_insn_r);
  11116.         }
  11117.     }
  11118.   else if (1 == op || 2 == op)
  11119.     {
  11120.       if (bit (thumb2_insn_r->arm_insn, INSN_S_L_BIT_NUM))
  11121.         {
  11122.           /* Handle LDM/LDMIA/LDMFD and LDMDB/LDMEA instructions.  */
  11123.           register_bits = bits (thumb2_insn_r->arm_insn, 0, 15);
  11124.           while (register_bits)
  11125.             {
  11126.               if (register_bits & 0x00000001)
  11127.                 record_buf[index++] = register_count;

  11128.               register_count++;
  11129.               register_bits = register_bits >> 1;
  11130.             }
  11131.           record_buf[index++] = reg_rn;
  11132.           record_buf[index++] = ARM_PS_REGNUM;
  11133.           thumb2_insn_r->reg_rec_count = index;
  11134.         }
  11135.       else
  11136.         {
  11137.           /* Handle STM/STMIA/STMEA and STMDB/STMFD.  */
  11138.           register_bits = bits (thumb2_insn_r->arm_insn, 0, 15);
  11139.           regcache_raw_read_unsigned (reg_cache, reg_rn, &u_regval);
  11140.           while (register_bits)
  11141.             {
  11142.               if (register_bits & 0x00000001)
  11143.                 register_count++;

  11144.               register_bits = register_bits >> 1;
  11145.             }

  11146.           if (1 == op)
  11147.             {
  11148.               /* Start address calculation for LDMDB/LDMEA.  */
  11149.               start_address = u_regval;
  11150.             }
  11151.           else if (2 == op)
  11152.             {
  11153.               /* Start address calculation for LDMDB/LDMEA.  */
  11154.               start_address = u_regval - register_count * 4;
  11155.             }

  11156.           thumb2_insn_r->mem_rec_count = register_count;
  11157.           while (register_count)
  11158.             {
  11159.               record_buf_mem[register_count * 2 - 1] = start_address;
  11160.               record_buf_mem[register_count * 2 - 2] = 4;
  11161.               start_address = start_address + 4;
  11162.               register_count--;
  11163.             }
  11164.           record_buf[0] = reg_rn;
  11165.           record_buf[1] = ARM_PS_REGNUM;
  11166.           thumb2_insn_r->reg_rec_count = 2;
  11167.         }
  11168.     }

  11169.   MEM_ALLOC (thumb2_insn_r->arm_mems, thumb2_insn_r->mem_rec_count,
  11170.             record_buf_mem);
  11171.   REG_ALLOC (thumb2_insn_r->arm_regs, thumb2_insn_r->reg_rec_count,
  11172.             record_buf);
  11173.   return ARM_RECORD_SUCCESS;
  11174. }

  11175. /* Handler for thumb2 load/store (dual/exclusive) and table branch
  11176.    instructions.  */

  11177. static int
  11178. thumb2_record_ld_st_dual_ex_tbb (insn_decode_record *thumb2_insn_r)
  11179. {
  11180.   struct regcache *reg_cache = thumb2_insn_r->regcache;

  11181.   uint32_t reg_rd, reg_rn, offset_imm;
  11182.   uint32_t reg_dest1, reg_dest2;
  11183.   uint32_t address, offset_addr;
  11184.   uint32_t record_buf[8], record_buf_mem[8];
  11185.   uint32_t op1, op2, op3;
  11186.   LONGEST s_word;

  11187.   ULONGEST u_regval[2];

  11188.   op1 = bits (thumb2_insn_r->arm_insn, 23, 24);
  11189.   op2 = bits (thumb2_insn_r->arm_insn, 20, 21);
  11190.   op3 = bits (thumb2_insn_r->arm_insn, 4, 7);

  11191.   if (bit (thumb2_insn_r->arm_insn, INSN_S_L_BIT_NUM))
  11192.     {
  11193.       if(!(1 == op1 && 1 == op2 && (0 == op3 || 1 == op3)))
  11194.         {
  11195.           reg_dest1 = bits (thumb2_insn_r->arm_insn, 12, 15);
  11196.           record_buf[0] = reg_dest1;
  11197.           record_buf[1] = ARM_PS_REGNUM;
  11198.           thumb2_insn_r->reg_rec_count = 2;
  11199.         }

  11200.       if (3 == op2 || (op1 & 2) || (1 == op1 && 1 == op2 && 7 == op3))
  11201.         {
  11202.           reg_dest2 = bits (thumb2_insn_r->arm_insn, 8, 11);
  11203.           record_buf[2] = reg_dest2;
  11204.           thumb2_insn_r->reg_rec_count = 3;
  11205.         }
  11206.     }
  11207.   else
  11208.     {
  11209.       reg_rn = bits (thumb2_insn_r->arm_insn, 16, 19);
  11210.       regcache_raw_read_unsigned (reg_cache, reg_rn, &u_regval[0]);

  11211.       if (0 == op1 && 0 == op2)
  11212.         {
  11213.           /* Handle STREX.  */
  11214.           offset_imm = bits (thumb2_insn_r->arm_insn, 0, 7);
  11215.           address = u_regval[0] + (offset_imm * 4);
  11216.           record_buf_mem[0] = 4;
  11217.           record_buf_mem[1] = address;
  11218.           thumb2_insn_r->mem_rec_count = 1;
  11219.           reg_rd = bits (thumb2_insn_r->arm_insn, 0, 3);
  11220.           record_buf[0] = reg_rd;
  11221.           thumb2_insn_r->reg_rec_count = 1;
  11222.         }
  11223.       else if (1 == op1 && 0 == op2)
  11224.         {
  11225.           reg_rd = bits (thumb2_insn_r->arm_insn, 0, 3);
  11226.           record_buf[0] = reg_rd;
  11227.           thumb2_insn_r->reg_rec_count = 1;
  11228.           address = u_regval[0];
  11229.           record_buf_mem[1] = address;

  11230.           if (4 == op3)
  11231.             {
  11232.               /* Handle STREXB.  */
  11233.               record_buf_mem[0] = 1;
  11234.               thumb2_insn_r->mem_rec_count = 1;
  11235.             }
  11236.           else if (5 == op3)
  11237.             {
  11238.               /* Handle STREXH.  */
  11239.               record_buf_mem[0] = 2 ;
  11240.               thumb2_insn_r->mem_rec_count = 1;
  11241.             }
  11242.           else if (7 == op3)
  11243.             {
  11244.               /* Handle STREXD.  */
  11245.               address = u_regval[0];
  11246.               record_buf_mem[0] = 4;
  11247.               record_buf_mem[2] = 4;
  11248.               record_buf_mem[3] = address + 4;
  11249.               thumb2_insn_r->mem_rec_count = 2;
  11250.             }
  11251.         }
  11252.       else
  11253.         {
  11254.           offset_imm = bits (thumb2_insn_r->arm_insn, 0, 7);

  11255.           if (bit (thumb2_insn_r->arm_insn, 24))
  11256.             {
  11257.               if (bit (thumb2_insn_r->arm_insn, 23))
  11258.                 offset_addr = u_regval[0] + (offset_imm * 4);
  11259.               else
  11260.                 offset_addr = u_regval[0] - (offset_imm * 4);

  11261.               address = offset_addr;
  11262.             }
  11263.           else
  11264.             address = u_regval[0];

  11265.           record_buf_mem[0] = 4;
  11266.           record_buf_mem[1] = address;
  11267.           record_buf_mem[2] = 4;
  11268.           record_buf_mem[3] = address + 4;
  11269.           thumb2_insn_r->mem_rec_count = 2;
  11270.           record_buf[0] = reg_rn;
  11271.           thumb2_insn_r->reg_rec_count = 1;
  11272.         }
  11273.     }

  11274.   REG_ALLOC (thumb2_insn_r->arm_regs, thumb2_insn_r->reg_rec_count,
  11275.             record_buf);
  11276.   MEM_ALLOC (thumb2_insn_r->arm_mems, thumb2_insn_r->mem_rec_count,
  11277.             record_buf_mem);
  11278.   return ARM_RECORD_SUCCESS;
  11279. }

  11280. /* Handler for thumb2 data processing (shift register and modified immediate)
  11281.    instructions.  */

  11282. static int
  11283. thumb2_record_data_proc_sreg_mimm (insn_decode_record *thumb2_insn_r)
  11284. {
  11285.   uint32_t reg_rd, op;
  11286.   uint32_t record_buf[8];

  11287.   op = bits (thumb2_insn_r->arm_insn, 21, 24);
  11288.   reg_rd = bits (thumb2_insn_r->arm_insn, 8, 11);

  11289.   if ((0 == op || 4 == op || 8 == op || 13 == op) && 15 == reg_rd)
  11290.     {
  11291.       record_buf[0] = ARM_PS_REGNUM;
  11292.       thumb2_insn_r->reg_rec_count = 1;
  11293.     }
  11294.   else
  11295.     {
  11296.       record_buf[0] = reg_rd;
  11297.       record_buf[1] = ARM_PS_REGNUM;
  11298.       thumb2_insn_r->reg_rec_count = 2;
  11299.     }

  11300.   REG_ALLOC (thumb2_insn_r->arm_regs, thumb2_insn_r->reg_rec_count,
  11301.             record_buf);
  11302.   return ARM_RECORD_SUCCESS;
  11303. }

  11304. /* Generic handler for thumb2 instructions which effect destination and PS
  11305.    registers.  */

  11306. static int
  11307. thumb2_record_ps_dest_generic (insn_decode_record *thumb2_insn_r)
  11308. {
  11309.   uint32_t reg_rd;
  11310.   uint32_t record_buf[8];

  11311.   reg_rd = bits (thumb2_insn_r->arm_insn, 8, 11);

  11312.   record_buf[0] = reg_rd;
  11313.   record_buf[1] = ARM_PS_REGNUM;
  11314.   thumb2_insn_r->reg_rec_count = 2;

  11315.   REG_ALLOC (thumb2_insn_r->arm_regs, thumb2_insn_r->reg_rec_count,
  11316.             record_buf);
  11317.   return ARM_RECORD_SUCCESS;
  11318. }

  11319. /* Handler for thumb2 branch and miscellaneous control instructions.  */

  11320. static int
  11321. thumb2_record_branch_misc_cntrl (insn_decode_record *thumb2_insn_r)
  11322. {
  11323.   uint32_t op, op1, op2;
  11324.   uint32_t record_buf[8];

  11325.   op = bits (thumb2_insn_r->arm_insn, 20, 26);
  11326.   op1 = bits (thumb2_insn_r->arm_insn, 12, 14);
  11327.   op2 = bits (thumb2_insn_r->arm_insn, 8, 11);

  11328.   /* Handle MSR insn.  */
  11329.   if (!(op1 & 0x2) && 0x38 == op)
  11330.     {
  11331.       if (!(op2 & 0x3))
  11332.         {
  11333.           /* CPSR is going to be changed.  */
  11334.           record_buf[0] = ARM_PS_REGNUM;
  11335.           thumb2_insn_r->reg_rec_count = 1;
  11336.         }
  11337.       else
  11338.         {
  11339.           arm_record_unsupported_insn(thumb2_insn_r);
  11340.           return -1;
  11341.         }
  11342.     }
  11343.   else if (4 == (op1 & 0x5) || 5 == (op1 & 0x5))
  11344.     {
  11345.       /* BLX.  */
  11346.       record_buf[0] = ARM_PS_REGNUM;
  11347.       record_buf[1] = ARM_LR_REGNUM;
  11348.       thumb2_insn_r->reg_rec_count = 2;
  11349.     }

  11350.   REG_ALLOC (thumb2_insn_r->arm_regs, thumb2_insn_r->reg_rec_count,
  11351.             record_buf);
  11352.   return ARM_RECORD_SUCCESS;
  11353. }

  11354. /* Handler for thumb2 store single data item instructions.  */

  11355. static int
  11356. thumb2_record_str_single_data (insn_decode_record *thumb2_insn_r)
  11357. {
  11358.   struct regcache *reg_cache = thumb2_insn_r->regcache;

  11359.   uint32_t reg_rn, reg_rm, offset_imm, shift_imm;
  11360.   uint32_t address, offset_addr;
  11361.   uint32_t record_buf[8], record_buf_mem[8];
  11362.   uint32_t op1, op2;

  11363.   ULONGEST u_regval[2];

  11364.   op1 = bits (thumb2_insn_r->arm_insn, 21, 23);
  11365.   op2 = bits (thumb2_insn_r->arm_insn, 6, 11);
  11366.   reg_rn = bits (thumb2_insn_r->arm_insn, 16, 19);
  11367.   regcache_raw_read_unsigned (reg_cache, reg_rn, &u_regval[0]);

  11368.   if (bit (thumb2_insn_r->arm_insn, 23))
  11369.     {
  11370.       /* T2 encoding.  */
  11371.       offset_imm = bits (thumb2_insn_r->arm_insn, 0, 11);
  11372.       offset_addr = u_regval[0] + offset_imm;
  11373.       address = offset_addr;
  11374.     }
  11375.   else
  11376.     {
  11377.       /* T3 encoding.  */
  11378.       if ((0 == op1 || 1 == op1 || 2 == op1) && !(op2 & 0x20))
  11379.         {
  11380.           /* Handle STRB (register).  */
  11381.           reg_rm = bits (thumb2_insn_r->arm_insn, 0, 3);
  11382.           regcache_raw_read_unsigned (reg_cache, reg_rm, &u_regval[1]);
  11383.           shift_imm = bits (thumb2_insn_r->arm_insn, 4, 5);
  11384.           offset_addr = u_regval[1] << shift_imm;
  11385.           address = u_regval[0] + offset_addr;
  11386.         }
  11387.       else
  11388.         {
  11389.           offset_imm = bits (thumb2_insn_r->arm_insn, 0, 7);
  11390.           if (bit (thumb2_insn_r->arm_insn, 10))
  11391.             {
  11392.               if (bit (thumb2_insn_r->arm_insn, 9))
  11393.                 offset_addr = u_regval[0] + offset_imm;
  11394.               else
  11395.                 offset_addr = u_regval[0] - offset_imm;

  11396.               address = offset_addr;
  11397.             }
  11398.           else
  11399.             address = u_regval[0];
  11400.         }
  11401.     }

  11402.   switch (op1)
  11403.     {
  11404.       /* Store byte instructions.  */
  11405.       case 4:
  11406.       case 0:
  11407.         record_buf_mem[0] = 1;
  11408.         break;
  11409.       /* Store half word instructions.  */
  11410.       case 1:
  11411.       case 5:
  11412.         record_buf_mem[0] = 2;
  11413.         break;
  11414.       /* Store word instructions.  */
  11415.       case 2:
  11416.       case 6:
  11417.         record_buf_mem[0] = 4;
  11418.         break;

  11419.       default:
  11420.         gdb_assert_not_reached ("no decoding pattern found");
  11421.         break;
  11422.     }

  11423.   record_buf_mem[1] = address;
  11424.   thumb2_insn_r->mem_rec_count = 1;
  11425.   record_buf[0] = reg_rn;
  11426.   thumb2_insn_r->reg_rec_count = 1;

  11427.   REG_ALLOC (thumb2_insn_r->arm_regs, thumb2_insn_r->reg_rec_count,
  11428.             record_buf);
  11429.   MEM_ALLOC (thumb2_insn_r->arm_mems, thumb2_insn_r->mem_rec_count,
  11430.             record_buf_mem);
  11431.   return ARM_RECORD_SUCCESS;
  11432. }

  11433. /* Handler for thumb2 load memory hints instructions.  */

  11434. static int
  11435. thumb2_record_ld_mem_hints (insn_decode_record *thumb2_insn_r)
  11436. {
  11437.   uint32_t record_buf[8];
  11438.   uint32_t reg_rt, reg_rn;

  11439.   reg_rt = bits (thumb2_insn_r->arm_insn, 12, 15);
  11440.   reg_rn = bits (thumb2_insn_r->arm_insn, 16, 19);

  11441.   if (ARM_PC_REGNUM != reg_rt)
  11442.     {
  11443.       record_buf[0] = reg_rt;
  11444.       record_buf[1] = reg_rn;
  11445.       record_buf[2] = ARM_PS_REGNUM;
  11446.       thumb2_insn_r->reg_rec_count = 3;

  11447.       REG_ALLOC (thumb2_insn_r->arm_regs, thumb2_insn_r->reg_rec_count,
  11448.                 record_buf);
  11449.       return ARM_RECORD_SUCCESS;
  11450.     }

  11451.   return ARM_RECORD_FAILURE;
  11452. }

  11453. /* Handler for thumb2 load word instructions.  */

  11454. static int
  11455. thumb2_record_ld_word (insn_decode_record *thumb2_insn_r)
  11456. {
  11457.   uint32_t opcode1 = 0, opcode2 = 0;
  11458.   uint32_t record_buf[8];

  11459.   record_buf[0] = bits (thumb2_insn_r->arm_insn, 12, 15);
  11460.   record_buf[1] = ARM_PS_REGNUM;
  11461.   thumb2_insn_r->reg_rec_count = 2;

  11462.   REG_ALLOC (thumb2_insn_r->arm_regs, thumb2_insn_r->reg_rec_count,
  11463.             record_buf);
  11464.   return ARM_RECORD_SUCCESS;
  11465. }

  11466. /* Handler for thumb2 long multiply, long multiply accumulate, and
  11467.    divide instructions.  */

  11468. static int
  11469. thumb2_record_lmul_lmla_div (insn_decode_record *thumb2_insn_r)
  11470. {
  11471.   uint32_t opcode1 = 0, opcode2 = 0;
  11472.   uint32_t record_buf[8];
  11473.   uint32_t reg_src1 = 0;

  11474.   opcode1 = bits (thumb2_insn_r->arm_insn, 20, 22);
  11475.   opcode2 = bits (thumb2_insn_r->arm_insn, 4, 7);

  11476.   if (0 == opcode1 || 2 == opcode1 || (opcode1 >= 4 && opcode1 <= 6))
  11477.     {
  11478.       /* Handle SMULL, UMULL, SMULAL.  */
  11479.       /* Handle SMLAL(S), SMULL(S), UMLAL(S), UMULL(S).  */
  11480.       record_buf[0] = bits (thumb2_insn_r->arm_insn, 16, 19);
  11481.       record_buf[1] = bits (thumb2_insn_r->arm_insn, 12, 15);
  11482.       record_buf[2] = ARM_PS_REGNUM;
  11483.       thumb2_insn_r->reg_rec_count = 3;
  11484.     }
  11485.   else if (1 == opcode1 || 3 == opcode2)
  11486.     {
  11487.       /* Handle SDIV and UDIV.  */
  11488.       record_buf[0] = bits (thumb2_insn_r->arm_insn, 16, 19);
  11489.       record_buf[1] = bits (thumb2_insn_r->arm_insn, 12, 15);
  11490.       record_buf[2] = ARM_PS_REGNUM;
  11491.       thumb2_insn_r->reg_rec_count = 3;
  11492.     }
  11493.   else
  11494.     return ARM_RECORD_FAILURE;

  11495.   REG_ALLOC (thumb2_insn_r->arm_regs, thumb2_insn_r->reg_rec_count,
  11496.             record_buf);
  11497.   return ARM_RECORD_SUCCESS;
  11498. }

  11499. /* Record handler for thumb32 coprocessor instructions.  */

  11500. static int
  11501. thumb2_record_coproc_insn (insn_decode_record *thumb2_insn_r)
  11502. {
  11503.   if (bit (thumb2_insn_r->arm_insn, 25))
  11504.     return arm_record_coproc_data_proc (thumb2_insn_r);
  11505.   else
  11506.     return arm_record_asimd_vfp_coproc (thumb2_insn_r);
  11507. }

  11508. /* Record handler for advance SIMD structure load/store instructions.  */

  11509. static int
  11510. thumb2_record_asimd_struct_ld_st (insn_decode_record *thumb2_insn_r)
  11511. {
  11512.   struct regcache *reg_cache = thumb2_insn_r->regcache;
  11513.   uint32_t l_bit, a_bit, b_bits;
  11514.   uint32_t record_buf[128], record_buf_mem[128];
  11515.   uint32_t reg_rn, reg_vd, address, f_esize, f_elem;
  11516.   uint32_t index_r = 0, index_e = 0, bf_regs = 0, index_m = 0, loop_t = 0;
  11517.   uint8_t f_ebytes;

  11518.   l_bit = bit (thumb2_insn_r->arm_insn, 21);
  11519.   a_bit = bit (thumb2_insn_r->arm_insn, 23);
  11520.   b_bits = bits (thumb2_insn_r->arm_insn, 8, 11);
  11521.   reg_rn = bits (thumb2_insn_r->arm_insn, 16, 19);
  11522.   reg_vd = bits (thumb2_insn_r->arm_insn, 12, 15);
  11523.   reg_vd = (bit (thumb2_insn_r->arm_insn, 22) << 4) | reg_vd;
  11524.   f_ebytes = (1 << bits (thumb2_insn_r->arm_insn, 6, 7));
  11525.   f_esize = 8 * f_ebytes;
  11526.   f_elem = 8 / f_ebytes;

  11527.   if (!l_bit)
  11528.     {
  11529.       ULONGEST u_regval = 0;
  11530.       regcache_raw_read_unsigned (reg_cache, reg_rn, &u_regval);
  11531.       address = u_regval;

  11532.       if (!a_bit)
  11533.         {
  11534.           /* Handle VST1.  */
  11535.           if (b_bits == 0x02 || b_bits == 0x0a || (b_bits & 0x0e) == 0x06)
  11536.             {
  11537.               if (b_bits == 0x07)
  11538.                 bf_regs = 1;
  11539.               else if (b_bits == 0x0a)
  11540.                 bf_regs = 2;
  11541.               else if (b_bits == 0x06)
  11542.                 bf_regs = 3;
  11543.               else if (b_bits == 0x02)
  11544.                 bf_regs = 4;
  11545.               else
  11546.                 bf_regs = 0;

  11547.               for (index_r = 0; index_r < bf_regs; index_r++)
  11548.                 {
  11549.                   for (index_e = 0; index_e < f_elem; index_e++)
  11550.                     {
  11551.                       record_buf_mem[index_m++] = f_ebytes;
  11552.                       record_buf_mem[index_m++] = address;
  11553.                       address = address + f_ebytes;
  11554.                       thumb2_insn_r->mem_rec_count += 1;
  11555.                     }
  11556.                 }
  11557.             }
  11558.           /* Handle VST2.  */
  11559.           else if (b_bits == 0x03 || (b_bits & 0x0e) == 0x08)
  11560.             {
  11561.               if (b_bits == 0x09 || b_bits == 0x08)
  11562.                 bf_regs = 1;
  11563.               else if (b_bits == 0x03)
  11564.                 bf_regs = 2;
  11565.               else
  11566.                 bf_regs = 0;

  11567.               for (index_r = 0; index_r < bf_regs; index_r++)
  11568.                 for (index_e = 0; index_e < f_elem; index_e++)
  11569.                   {
  11570.                     for (loop_t = 0; loop_t < 2; loop_t++)
  11571.                       {
  11572.                         record_buf_mem[index_m++] = f_ebytes;
  11573.                         record_buf_mem[index_m++] = address + (loop_t * f_ebytes);
  11574.                         thumb2_insn_r->mem_rec_count += 1;
  11575.                       }
  11576.                     address = address + (2 * f_ebytes);
  11577.                   }
  11578.             }
  11579.           /* Handle VST3.  */
  11580.           else if ((b_bits & 0x0e) == 0x04)
  11581.             {
  11582.               for (index_e = 0; index_e < f_elem; index_e++)
  11583.                 {
  11584.                   for (loop_t = 0; loop_t < 3; loop_t++)
  11585.                     {
  11586.                       record_buf_mem[index_m++] = f_ebytes;
  11587.                       record_buf_mem[index_m++] = address + (loop_t * f_ebytes);
  11588.                       thumb2_insn_r->mem_rec_count += 1;
  11589.                     }
  11590.                   address = address + (3 * f_ebytes);
  11591.                 }
  11592.             }
  11593.           /* Handle VST4.  */
  11594.           else if (!(b_bits & 0x0e))
  11595.             {
  11596.               for (index_e = 0; index_e < f_elem; index_e++)
  11597.                 {
  11598.                   for (loop_t = 0; loop_t < 4; loop_t++)
  11599.                     {
  11600.                       record_buf_mem[index_m++] = f_ebytes;
  11601.                       record_buf_mem[index_m++] = address + (loop_t * f_ebytes);
  11602.                       thumb2_insn_r->mem_rec_count += 1;
  11603.                     }
  11604.                   address = address + (4 * f_ebytes);
  11605.                 }
  11606.             }
  11607.         }
  11608.       else
  11609.         {
  11610.           uint8_t bft_size = bits (thumb2_insn_r->arm_insn, 10, 11);

  11611.           if (bft_size == 0x00)
  11612.             f_ebytes = 1;
  11613.           else if (bft_size == 0x01)
  11614.             f_ebytes = 2;
  11615.           else if (bft_size == 0x02)
  11616.             f_ebytes = 4;
  11617.           else
  11618.             f_ebytes = 0;

  11619.           /* Handle VST1.  */
  11620.           if (!(b_bits & 0x0b) || b_bits == 0x08)
  11621.             thumb2_insn_r->mem_rec_count = 1;
  11622.           /* Handle VST2.  */
  11623.           else if ((b_bits & 0x0b) == 0x01 || b_bits == 0x09)
  11624.             thumb2_insn_r->mem_rec_count = 2;
  11625.           /* Handle VST3.  */
  11626.           else if ((b_bits & 0x0b) == 0x02 || b_bits == 0x0a)
  11627.             thumb2_insn_r->mem_rec_count = 3;
  11628.           /* Handle VST4.  */
  11629.           else if ((b_bits & 0x0b) == 0x03 || b_bits == 0x0b)
  11630.             thumb2_insn_r->mem_rec_count = 4;

  11631.           for (index_m = 0; index_m < thumb2_insn_r->mem_rec_count; index_m++)
  11632.             {
  11633.               record_buf_mem[index_m] = f_ebytes;
  11634.               record_buf_mem[index_m] = address + (index_m * f_ebytes);
  11635.             }
  11636.         }
  11637.     }
  11638.   else
  11639.     {
  11640.       if (!a_bit)
  11641.         {
  11642.           /* Handle VLD1.  */
  11643.           if (b_bits == 0x02 || b_bits == 0x0a || (b_bits & 0x0e) == 0x06)
  11644.             thumb2_insn_r->reg_rec_count = 1;
  11645.           /* Handle VLD2.  */
  11646.           else if (b_bits == 0x03 || (b_bits & 0x0e) == 0x08)
  11647.             thumb2_insn_r->reg_rec_count = 2;
  11648.           /* Handle VLD3.  */
  11649.           else if ((b_bits & 0x0e) == 0x04)
  11650.             thumb2_insn_r->reg_rec_count = 3;
  11651.           /* Handle VLD4.  */
  11652.           else if (!(b_bits & 0x0e))
  11653.             thumb2_insn_r->reg_rec_count = 4;
  11654.         }
  11655.       else
  11656.         {
  11657.           /* Handle VLD1.  */
  11658.           if (!(b_bits & 0x0b) || b_bits == 0x08 || b_bits == 0x0c)
  11659.             thumb2_insn_r->reg_rec_count = 1;
  11660.           /* Handle VLD2.  */
  11661.           else if ((b_bits & 0x0b) == 0x01 || b_bits == 0x09 || b_bits == 0x0d)
  11662.             thumb2_insn_r->reg_rec_count = 2;
  11663.           /* Handle VLD3.  */
  11664.           else if ((b_bits & 0x0b) == 0x02 || b_bits == 0x0a || b_bits == 0x0e)
  11665.             thumb2_insn_r->reg_rec_count = 3;
  11666.           /* Handle VLD4.  */
  11667.           else if ((b_bits & 0x0b) == 0x03 || b_bits == 0x0b || b_bits == 0x0f)
  11668.             thumb2_insn_r->reg_rec_count = 4;

  11669.           for (index_r = 0; index_r < thumb2_insn_r->reg_rec_count; index_r++)
  11670.             record_buf[index_r] = reg_vd + ARM_D0_REGNUM + index_r;
  11671.         }
  11672.     }

  11673.   if (bits (thumb2_insn_r->arm_insn, 0, 3) != 15)
  11674.     {
  11675.       record_buf[index_r] = reg_rn;
  11676.       thumb2_insn_r->reg_rec_count += 1;
  11677.     }

  11678.   REG_ALLOC (thumb2_insn_r->arm_regs, thumb2_insn_r->reg_rec_count,
  11679.             record_buf);
  11680.   MEM_ALLOC (thumb2_insn_r->arm_mems, thumb2_insn_r->mem_rec_count,
  11681.             record_buf_mem);
  11682.   return 0;
  11683. }

  11684. /* Decodes thumb2 instruction type and invokes its record handler.  */

  11685. static unsigned int
  11686. thumb2_record_decode_insn_handler (insn_decode_record *thumb2_insn_r)
  11687. {
  11688.   uint32_t op, op1, op2;

  11689.   op = bit (thumb2_insn_r->arm_insn, 15);
  11690.   op1 = bits (thumb2_insn_r->arm_insn, 27, 28);
  11691.   op2 = bits (thumb2_insn_r->arm_insn, 20, 26);

  11692.   if (op1 == 0x01)
  11693.     {
  11694.       if (!(op2 & 0x64 ))
  11695.         {
  11696.           /* Load/store multiple instruction.  */
  11697.           return thumb2_record_ld_st_multiple (thumb2_insn_r);
  11698.         }
  11699.       else if (!((op2 & 0x64) ^ 0x04))
  11700.         {
  11701.           /* Load/store (dual/exclusive) and table branch instruction.  */
  11702.           return thumb2_record_ld_st_dual_ex_tbb (thumb2_insn_r);
  11703.         }
  11704.       else if (!((op2 & 0x20) ^ 0x20))
  11705.         {
  11706.           /* Data-processing (shifted register).  */
  11707.           return thumb2_record_data_proc_sreg_mimm (thumb2_insn_r);
  11708.         }
  11709.       else if (op2 & 0x40)
  11710.         {
  11711.           /* Co-processor instructions.  */
  11712.           return thumb2_record_coproc_insn (thumb2_insn_r);
  11713.         }
  11714.     }
  11715.   else if (op1 == 0x02)
  11716.     {
  11717.       if (op)
  11718.         {
  11719.           /* Branches and miscellaneous control instructions.  */
  11720.           return thumb2_record_branch_misc_cntrl (thumb2_insn_r);
  11721.         }
  11722.       else if (op2 & 0x20)
  11723.         {
  11724.           /* Data-processing (plain binary immediate) instruction.  */
  11725.           return thumb2_record_ps_dest_generic (thumb2_insn_r);
  11726.         }
  11727.       else
  11728.         {
  11729.           /* Data-processing (modified immediate).  */
  11730.           return thumb2_record_data_proc_sreg_mimm (thumb2_insn_r);
  11731.         }
  11732.     }
  11733.   else if (op1 == 0x03)
  11734.     {
  11735.       if (!(op2 & 0x71 ))
  11736.         {
  11737.           /* Store single data item.  */
  11738.           return thumb2_record_str_single_data (thumb2_insn_r);
  11739.         }
  11740.       else if (!((op2 & 0x71) ^ 0x10))
  11741.         {
  11742.           /* Advanced SIMD or structure load/store instructions.  */
  11743.           return thumb2_record_asimd_struct_ld_st (thumb2_insn_r);
  11744.         }
  11745.       else if (!((op2 & 0x67) ^ 0x01))
  11746.         {
  11747.           /* Load byte, memory hints instruction.  */
  11748.           return thumb2_record_ld_mem_hints (thumb2_insn_r);
  11749.         }
  11750.       else if (!((op2 & 0x67) ^ 0x03))
  11751.         {
  11752.           /* Load halfword, memory hints instruction.  */
  11753.           return thumb2_record_ld_mem_hints (thumb2_insn_r);
  11754.         }
  11755.       else if (!((op2 & 0x67) ^ 0x05))
  11756.         {
  11757.           /* Load word instruction.  */
  11758.           return thumb2_record_ld_word (thumb2_insn_r);
  11759.         }
  11760.       else if (!((op2 & 0x70) ^ 0x20))
  11761.         {
  11762.           /* Data-processing (register) instruction.  */
  11763.           return thumb2_record_ps_dest_generic (thumb2_insn_r);
  11764.         }
  11765.       else if (!((op2 & 0x78) ^ 0x30))
  11766.         {
  11767.           /* Multiply, multiply accumulate, abs diff instruction.  */
  11768.           return thumb2_record_ps_dest_generic (thumb2_insn_r);
  11769.         }
  11770.       else if (!((op2 & 0x78) ^ 0x38))
  11771.         {
  11772.           /* Long multiply, long multiply accumulate, and divide.  */
  11773.           return thumb2_record_lmul_lmla_div (thumb2_insn_r);
  11774.         }
  11775.       else if (op2 & 0x40)
  11776.         {
  11777.           /* Co-processor instructions.  */
  11778.           return thumb2_record_coproc_insn (thumb2_insn_r);
  11779.         }
  11780.    }

  11781.   return -1;
  11782. }

  11783. /* Extracts arm/thumb/thumb2 insn depending on the size, and returns 0 on success
  11784. and positive val on fauilure.  */

  11785. static int
  11786. extract_arm_insn (insn_decode_record *insn_record, uint32_t insn_size)
  11787. {
  11788.   gdb_byte buf[insn_size];

  11789.   memset (&buf[0], 0, insn_size);

  11790.   if (target_read_memory (insn_record->this_addr, &buf[0], insn_size))
  11791.     return 1;
  11792.   insn_record->arm_insn = (uint32_t) extract_unsigned_integer (&buf[0],
  11793.                            insn_size,
  11794.                            gdbarch_byte_order_for_code (insn_record->gdbarch));
  11795.   return 0;
  11796. }

  11797. typedef int (*sti_arm_hdl_fp_t) (insn_decode_record*);

  11798. /* Decode arm/thumb insn depending on condition cods and opcodes; and
  11799.    dispatch it.  */

  11800. static int
  11801. decode_insn (insn_decode_record *arm_record, record_type_t record_type,
  11802.                 uint32_t insn_size)
  11803. {

  11804.   /* (Starting from numerical 0); bits 25, 26, 27 decodes type of arm instruction.  */
  11805.   static const sti_arm_hdl_fp_t const arm_handle_insn[8] =
  11806.   {
  11807.     arm_record_data_proc_misc_ld_str,   /* 000.  */
  11808.     arm_record_data_proc_imm,           /* 001.  */
  11809.     arm_record_ld_st_imm_offset,        /* 010.  */
  11810.     arm_record_ld_st_reg_offset,        /* 011.  */
  11811.     arm_record_ld_st_multiple,          /* 100.  */
  11812.     arm_record_b_bl,                    /* 101.  */
  11813.     arm_record_asimd_vfp_coproc,        /* 110.  */
  11814.     arm_record_coproc_data_proc         /* 111.  */
  11815.   };

  11816.   /* (Starting from numerical 0); bits 13,14,15 decodes type of thumb instruction.  */
  11817.   static const sti_arm_hdl_fp_t const thumb_handle_insn[8] =
  11818.   { \
  11819.     thumb_record_shift_add_sub,        /* 000.  */
  11820.     thumb_record_add_sub_cmp_mov,      /* 001.  */
  11821.     thumb_record_ld_st_reg_offset,     /* 010.  */
  11822.     thumb_record_ld_st_imm_offset,     /* 011.  */
  11823.     thumb_record_ld_st_stack,          /* 100.  */
  11824.     thumb_record_misc,                 /* 101.  */
  11825.     thumb_record_ldm_stm_swi,          /* 110.  */
  11826.     thumb_record_branch                /* 111.  */
  11827.   };

  11828.   uint32_t ret = 0;    /* return value: negative:failure   0:success.  */
  11829.   uint32_t insn_id = 0;

  11830.   if (extract_arm_insn (arm_record, insn_size))
  11831.     {
  11832.       if (record_debug)
  11833.         {
  11834.           printf_unfiltered (_("Process record: error reading memory at "
  11835.                               "addr %s len = %d.\n"),
  11836.           paddress (arm_record->gdbarch, arm_record->this_addr), insn_size);
  11837.         }
  11838.       return -1;
  11839.     }
  11840.   else if (ARM_RECORD == record_type)
  11841.     {
  11842.       arm_record->cond = bits (arm_record->arm_insn, 28, 31);
  11843.       insn_id = bits (arm_record->arm_insn, 25, 27);
  11844.       ret = arm_record_extension_space (arm_record);
  11845.       /* If this insn has fallen into extension space
  11846.          then we need not decode it anymore.  */
  11847.       if (ret != -1 && !INSN_RECORDED(arm_record))
  11848.         {
  11849.           ret = arm_handle_insn[insn_id] (arm_record);
  11850.         }
  11851.     }
  11852.   else if (THUMB_RECORD == record_type)
  11853.     {
  11854.       /* As thumb does not have condition codes, we set negative.  */
  11855.       arm_record->cond = -1;
  11856.       insn_id = bits (arm_record->arm_insn, 13, 15);
  11857.       ret = thumb_handle_insn[insn_id] (arm_record);
  11858.     }
  11859.   else if (THUMB2_RECORD == record_type)
  11860.     {
  11861.       /* As thumb does not have condition codes, we set negative.  */
  11862.       arm_record->cond = -1;

  11863.       /* Swap first half of 32bit thumb instruction with second half.  */
  11864.       arm_record->arm_insn
  11865.         = (arm_record->arm_insn >> 16) | (arm_record->arm_insn << 16);

  11866.       insn_id = thumb2_record_decode_insn_handler (arm_record);

  11867.       if (insn_id != ARM_RECORD_SUCCESS)
  11868.         {
  11869.           arm_record_unsupported_insn (arm_record);
  11870.           ret = -1;
  11871.         }
  11872.     }
  11873.   else
  11874.     {
  11875.       /* Throw assertion.  */
  11876.       gdb_assert_not_reached ("not a valid instruction, could not decode");
  11877.     }

  11878.   return ret;
  11879. }


  11880. /* Cleans up local record registers and memory allocations.  */

  11881. static void
  11882. deallocate_reg_mem (insn_decode_record *record)
  11883. {
  11884.   xfree (record->arm_regs);
  11885.   xfree (record->arm_mems);
  11886. }


  11887. /* Parse the current instruction and record the values of the registers and
  11888.    memory that will be changed in current instruction to record_arch_list".
  11889.    Return -1 if something is wrong.  */

  11890. int
  11891. arm_process_record (struct gdbarch *gdbarch, struct regcache *regcache,
  11892.                         CORE_ADDR insn_addr)
  11893. {

  11894.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  11895.   uint32_t no_of_rec = 0;
  11896.   uint32_t ret = 0/* return value: -1:record failure ;  0:success  */
  11897.   ULONGEST t_bit = 0, insn_id = 0;

  11898.   ULONGEST u_regval = 0;

  11899.   insn_decode_record arm_record;

  11900.   memset (&arm_record, 0, sizeof (insn_decode_record));
  11901.   arm_record.regcache = regcache;
  11902.   arm_record.this_addr = insn_addr;
  11903.   arm_record.gdbarch = gdbarch;


  11904.   if (record_debug > 1)
  11905.     {
  11906.       fprintf_unfiltered (gdb_stdlog, "Process record: arm_process_record "
  11907.                                       "addr = %s\n",
  11908.       paddress (gdbarch, arm_record.this_addr));
  11909.     }

  11910.   if (extract_arm_insn (&arm_record, 2))
  11911.     {
  11912.       if (record_debug)
  11913.         {
  11914.           printf_unfiltered (_("Process record: error reading memory at "
  11915.                              "addr %s len = %d.\n"),
  11916.                              paddress (arm_record.gdbarch,
  11917.                              arm_record.this_addr), 2);
  11918.         }
  11919.       return -1;
  11920.     }

  11921.   /* Check the insn, whether it is thumb or arm one.  */

  11922.   t_bit = arm_psr_thumb_bit (arm_record.gdbarch);
  11923.   regcache_raw_read_unsigned (arm_record.regcache, ARM_PS_REGNUM, &u_regval);


  11924.   if (!(u_regval & t_bit))
  11925.     {
  11926.       /* We are decoding arm insn.  */
  11927.       ret = decode_insn (&arm_record, ARM_RECORD, ARM_INSN_SIZE_BYTES);
  11928.     }
  11929.   else
  11930.     {
  11931.       insn_id = bits (arm_record.arm_insn, 11, 15);
  11932.       /* is it thumb2 insn?  */
  11933.       if ((0x1D == insn_id) || (0x1E == insn_id) || (0x1F == insn_id))
  11934.         {
  11935.           ret = decode_insn (&arm_record, THUMB2_RECORD,
  11936.                              THUMB2_INSN_SIZE_BYTES);
  11937.         }
  11938.       else
  11939.         {
  11940.           /* We are decoding thumb insn.  */
  11941.           ret = decode_insn (&arm_record, THUMB_RECORD, THUMB_INSN_SIZE_BYTES);
  11942.         }
  11943.     }

  11944.   if (0 == ret)
  11945.     {
  11946.       /* Record registers.  */
  11947.       record_full_arch_list_add_reg (arm_record.regcache, ARM_PC_REGNUM);
  11948.       if (arm_record.arm_regs)
  11949.         {
  11950.           for (no_of_rec = 0; no_of_rec < arm_record.reg_rec_count; no_of_rec++)
  11951.             {
  11952.               if (record_full_arch_list_add_reg
  11953.                   (arm_record.regcache , arm_record.arm_regs[no_of_rec]))
  11954.               ret = -1;
  11955.             }
  11956.         }
  11957.       /* Record memories.  */
  11958.       if (arm_record.arm_mems)
  11959.         {
  11960.           for (no_of_rec = 0; no_of_rec < arm_record.mem_rec_count; no_of_rec++)
  11961.             {
  11962.               if (record_full_arch_list_add_mem
  11963.                   ((CORE_ADDR)arm_record.arm_mems[no_of_rec].addr,
  11964.                    arm_record.arm_mems[no_of_rec].len))
  11965.                 ret = -1;
  11966.             }
  11967.         }

  11968.       if (record_full_arch_list_add_end ())
  11969.         ret = -1;
  11970.     }


  11971.   deallocate_reg_mem (&arm_record);

  11972.   return ret;
  11973. }