GDB (API)
/home/stan/gdb/src/gdb/arm-tdep.c
Go to the documentation of this file.
00001 /* Common target dependent code for GDB on ARM systems.
00002 
00003    Copyright (C) 1988-2013 Free Software Foundation, Inc.
00004 
00005    This file is part of GDB.
00006 
00007    This program is free software; you can redistribute it and/or modify
00008    it under the terms of the GNU General Public License as published by
00009    the Free Software Foundation; either version 3 of the License, or
00010    (at your option) any later version.
00011 
00012    This program is distributed in the hope that it will be useful,
00013    but WITHOUT ANY WARRANTY; without even the implied warranty of
00014    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015    GNU General Public License for more details.
00016 
00017    You should have received a copy of the GNU General Public License
00018    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
00019 
00020 #include "defs.h"
00021 
00022 #include <ctype.h>              /* XXX for isupper ().  */
00023 
00024 #include "frame.h"
00025 #include "inferior.h"
00026 #include "gdbcmd.h"
00027 #include "gdbcore.h"
00028 #include "gdb_string.h"
00029 #include "dis-asm.h"            /* For register styles.  */
00030 #include "regcache.h"
00031 #include "reggroups.h"
00032 #include "doublest.h"
00033 #include "value.h"
00034 #include "arch-utils.h"
00035 #include "osabi.h"
00036 #include "frame-unwind.h"
00037 #include "frame-base.h"
00038 #include "trad-frame.h"
00039 #include "objfiles.h"
00040 #include "dwarf2-frame.h"
00041 #include "gdbtypes.h"
00042 #include "prologue-value.h"
00043 #include "remote.h"
00044 #include "target-descriptions.h"
00045 #include "user-regs.h"
00046 #include "observer.h"
00047 
00048 #include "arm-tdep.h"
00049 #include "gdb/sim-arm.h"
00050 
00051 #include "elf-bfd.h"
00052 #include "coff/internal.h"
00053 #include "elf/arm.h"
00054 
00055 #include "gdb_assert.h"
00056 #include "vec.h"
00057 
00058 #include "record.h"
00059 #include "record-full.h"
00060 
00061 #include "features/arm-with-m.c"
00062 #include "features/arm-with-m-fpa-layout.c"
00063 #include "features/arm-with-m-vfp-d16.c"
00064 #include "features/arm-with-iwmmxt.c"
00065 #include "features/arm-with-vfpv2.c"
00066 #include "features/arm-with-vfpv3.c"
00067 #include "features/arm-with-neon.c"
00068 
00069 static int arm_debug;
00070 
00071 /* Macros for setting and testing a bit in a minimal symbol that marks
00072    it as Thumb function.  The MSB of the minimal symbol's "info" field
00073    is used for this purpose.
00074 
00075    MSYMBOL_SET_SPECIAL  Actually sets the "special" bit.
00076    MSYMBOL_IS_SPECIAL   Tests the "special" bit in a minimal symbol.  */
00077 
00078 #define MSYMBOL_SET_SPECIAL(msym)                               \
00079         MSYMBOL_TARGET_FLAG_1 (msym) = 1
00080 
00081 #define MSYMBOL_IS_SPECIAL(msym)                                \
00082         MSYMBOL_TARGET_FLAG_1 (msym)
00083 
00084 /* Per-objfile data used for mapping symbols.  */
00085 static const struct objfile_data *arm_objfile_data_key;
00086 
00087 struct arm_mapping_symbol
00088 {
00089   bfd_vma value;
00090   char type;
00091 };
00092 typedef struct arm_mapping_symbol arm_mapping_symbol_s;
00093 DEF_VEC_O(arm_mapping_symbol_s);
00094 
00095 struct arm_per_objfile
00096 {
00097   VEC(arm_mapping_symbol_s) **section_maps;
00098 };
00099 
00100 /* The list of available "set arm ..." and "show arm ..." commands.  */
00101 static struct cmd_list_element *setarmcmdlist = NULL;
00102 static struct cmd_list_element *showarmcmdlist = NULL;
00103 
00104 /* The type of floating-point to use.  Keep this in sync with enum
00105    arm_float_model, and the help string in _initialize_arm_tdep.  */
00106 static const char *const fp_model_strings[] =
00107 {
00108   "auto",
00109   "softfpa",
00110   "fpa",
00111   "softvfp",
00112   "vfp",
00113   NULL
00114 };
00115 
00116 /* A variable that can be configured by the user.  */
00117 static enum arm_float_model arm_fp_model = ARM_FLOAT_AUTO;
00118 static const char *current_fp_model = "auto";
00119 
00120 /* The ABI to use.  Keep this in sync with arm_abi_kind.  */
00121 static const char *const arm_abi_strings[] =
00122 {
00123   "auto",
00124   "APCS",
00125   "AAPCS",
00126   NULL
00127 };
00128 
00129 /* A variable that can be configured by the user.  */
00130 static enum arm_abi_kind arm_abi_global = ARM_ABI_AUTO;
00131 static const char *arm_abi_string = "auto";
00132 
00133 /* The execution mode to assume.  */
00134 static const char *const arm_mode_strings[] =
00135   {
00136     "auto",
00137     "arm",
00138     "thumb",
00139     NULL
00140   };
00141 
00142 static const char *arm_fallback_mode_string = "auto";
00143 static const char *arm_force_mode_string = "auto";
00144 
00145 /* Internal override of the execution mode.  -1 means no override,
00146    0 means override to ARM mode, 1 means override to Thumb mode.
00147    The effect is the same as if arm_force_mode has been set by the
00148    user (except the internal override has precedence over a user's
00149    arm_force_mode override).  */
00150 static int arm_override_mode = -1;
00151 
00152 /* Number of different reg name sets (options).  */
00153 static int num_disassembly_options;
00154 
00155 /* The standard register names, and all the valid aliases for them.  Note
00156    that `fp', `sp' and `pc' are not added in this alias list, because they
00157    have been added as builtin user registers in
00158    std-regs.c:_initialize_frame_reg.  */
00159 static const struct
00160 {
00161   const char *name;
00162   int regnum;
00163 } arm_register_aliases[] = {
00164   /* Basic register numbers.  */
00165   { "r0", 0 },
00166   { "r1", 1 },
00167   { "r2", 2 },
00168   { "r3", 3 },
00169   { "r4", 4 },
00170   { "r5", 5 },
00171   { "r6", 6 },
00172   { "r7", 7 },
00173   { "r8", 8 },
00174   { "r9", 9 },
00175   { "r10", 10 },
00176   { "r11", 11 },
00177   { "r12", 12 },
00178   { "r13", 13 },
00179   { "r14", 14 },
00180   { "r15", 15 },
00181   /* Synonyms (argument and variable registers).  */
00182   { "a1", 0 },
00183   { "a2", 1 },
00184   { "a3", 2 },
00185   { "a4", 3 },
00186   { "v1", 4 },
00187   { "v2", 5 },
00188   { "v3", 6 },
00189   { "v4", 7 },
00190   { "v5", 8 },
00191   { "v6", 9 },
00192   { "v7", 10 },
00193   { "v8", 11 },
00194   /* Other platform-specific names for r9.  */
00195   { "sb", 9 },
00196   { "tr", 9 },
00197   /* Special names.  */
00198   { "ip", 12 },
00199   { "lr", 14 },
00200   /* Names used by GCC (not listed in the ARM EABI).  */
00201   { "sl", 10 },
00202   /* A special name from the older ATPCS.  */
00203   { "wr", 7 },
00204 };
00205 
00206 static const char *const arm_register_names[] =
00207 {"r0",  "r1",  "r2",  "r3",     /*  0  1  2  3 */
00208  "r4",  "r5",  "r6",  "r7",     /*  4  5  6  7 */
00209  "r8",  "r9",  "r10", "r11",    /*  8  9 10 11 */
00210  "r12", "sp",  "lr",  "pc",     /* 12 13 14 15 */
00211  "f0",  "f1",  "f2",  "f3",     /* 16 17 18 19 */
00212  "f4",  "f5",  "f6",  "f7",     /* 20 21 22 23 */
00213  "fps", "cpsr" };               /* 24 25       */
00214 
00215 /* Valid register name styles.  */
00216 static const char **valid_disassembly_styles;
00217 
00218 /* Disassembly style to use. Default to "std" register names.  */
00219 static const char *disassembly_style;
00220 
00221 /* This is used to keep the bfd arch_info in sync with the disassembly
00222    style.  */
00223 static void set_disassembly_style_sfunc(char *, int,
00224                                          struct cmd_list_element *);
00225 static void set_disassembly_style (void);
00226 
00227 static void convert_from_extended (const struct floatformat *, const void *,
00228                                    void *, int);
00229 static void convert_to_extended (const struct floatformat *, void *,
00230                                  const void *, int);
00231 
00232 static enum register_status arm_neon_quad_read (struct gdbarch *gdbarch,
00233                                                 struct regcache *regcache,
00234                                                 int regnum, gdb_byte *buf);
00235 static void arm_neon_quad_write (struct gdbarch *gdbarch,
00236                                  struct regcache *regcache,
00237                                  int regnum, const gdb_byte *buf);
00238 
00239 static int thumb_insn_size (unsigned short inst1);
00240 
00241 struct arm_prologue_cache
00242 {
00243   /* The stack pointer at the time this frame was created; i.e. the
00244      caller's stack pointer when this function was called.  It is used
00245      to identify this frame.  */
00246   CORE_ADDR prev_sp;
00247 
00248   /* The frame base for this frame is just prev_sp - frame size.
00249      FRAMESIZE is the distance from the frame pointer to the
00250      initial stack pointer.  */
00251 
00252   int framesize;
00253 
00254   /* The register used to hold the frame pointer for this frame.  */
00255   int framereg;
00256 
00257   /* Saved register offsets.  */
00258   struct trad_frame_saved_reg *saved_regs;
00259 };
00260 
00261 static CORE_ADDR arm_analyze_prologue (struct gdbarch *gdbarch,
00262                                        CORE_ADDR prologue_start,
00263                                        CORE_ADDR prologue_end,
00264                                        struct arm_prologue_cache *cache);
00265 
00266 /* Architecture version for displaced stepping.  This effects the behaviour of
00267    certain instructions, and really should not be hard-wired.  */
00268 
00269 #define DISPLACED_STEPPING_ARCH_VERSION         5
00270 
00271 /* Addresses for calling Thumb functions have the bit 0 set.
00272    Here are some macros to test, set, or clear bit 0 of addresses.  */
00273 #define IS_THUMB_ADDR(addr)     ((addr) & 1)
00274 #define MAKE_THUMB_ADDR(addr)   ((addr) | 1)
00275 #define UNMAKE_THUMB_ADDR(addr) ((addr) & ~1)
00276 
00277 /* Set to true if the 32-bit mode is in use.  */
00278 
00279 int arm_apcs_32 = 1;
00280 
00281 /* Return the bit mask in ARM_PS_REGNUM that indicates Thumb mode.  */
00282 
00283 int
00284 arm_psr_thumb_bit (struct gdbarch *gdbarch)
00285 {
00286   if (gdbarch_tdep (gdbarch)->is_m)
00287     return XPSR_T;
00288   else
00289     return CPSR_T;
00290 }
00291 
00292 /* Determine if FRAME is executing in Thumb mode.  */
00293 
00294 int
00295 arm_frame_is_thumb (struct frame_info *frame)
00296 {
00297   CORE_ADDR cpsr;
00298   ULONGEST t_bit = arm_psr_thumb_bit (get_frame_arch (frame));
00299 
00300   /* Every ARM frame unwinder can unwind the T bit of the CPSR, either
00301      directly (from a signal frame or dummy frame) or by interpreting
00302      the saved LR (from a prologue or DWARF frame).  So consult it and
00303      trust the unwinders.  */
00304   cpsr = get_frame_register_unsigned (frame, ARM_PS_REGNUM);
00305 
00306   return (cpsr & t_bit) != 0;
00307 }
00308 
00309 /* Callback for VEC_lower_bound.  */
00310 
00311 static inline int
00312 arm_compare_mapping_symbols (const struct arm_mapping_symbol *lhs,
00313                              const struct arm_mapping_symbol *rhs)
00314 {
00315   return lhs->value < rhs->value;
00316 }
00317 
00318 /* Search for the mapping symbol covering MEMADDR.  If one is found,
00319    return its type.  Otherwise, return 0.  If START is non-NULL,
00320    set *START to the location of the mapping symbol.  */
00321 
00322 static char
00323 arm_find_mapping_symbol (CORE_ADDR memaddr, CORE_ADDR *start)
00324 {
00325   struct obj_section *sec;
00326 
00327   /* If there are mapping symbols, consult them.  */
00328   sec = find_pc_section (memaddr);
00329   if (sec != NULL)
00330     {
00331       struct arm_per_objfile *data;
00332       VEC(arm_mapping_symbol_s) *map;
00333       struct arm_mapping_symbol map_key = { memaddr - obj_section_addr (sec),
00334                                             0 };
00335       unsigned int idx;
00336 
00337       data = objfile_data (sec->objfile, arm_objfile_data_key);
00338       if (data != NULL)
00339         {
00340           map = data->section_maps[sec->the_bfd_section->index];
00341           if (!VEC_empty (arm_mapping_symbol_s, map))
00342             {
00343               struct arm_mapping_symbol *map_sym;
00344 
00345               idx = VEC_lower_bound (arm_mapping_symbol_s, map, &map_key,
00346                                      arm_compare_mapping_symbols);
00347 
00348               /* VEC_lower_bound finds the earliest ordered insertion
00349                  point.  If the following symbol starts at this exact
00350                  address, we use that; otherwise, the preceding
00351                  mapping symbol covers this address.  */
00352               if (idx < VEC_length (arm_mapping_symbol_s, map))
00353                 {
00354                   map_sym = VEC_index (arm_mapping_symbol_s, map, idx);
00355                   if (map_sym->value == map_key.value)
00356                     {
00357                       if (start)
00358                         *start = map_sym->value + obj_section_addr (sec);
00359                       return map_sym->type;
00360                     }
00361                 }
00362 
00363               if (idx > 0)
00364                 {
00365                   map_sym = VEC_index (arm_mapping_symbol_s, map, idx - 1);
00366                   if (start)
00367                     *start = map_sym->value + obj_section_addr (sec);
00368                   return map_sym->type;
00369                 }
00370             }
00371         }
00372     }
00373 
00374   return 0;
00375 }
00376 
00377 /* Determine if the program counter specified in MEMADDR is in a Thumb
00378    function.  This function should be called for addresses unrelated to
00379    any executing frame; otherwise, prefer arm_frame_is_thumb.  */
00380 
00381 int
00382 arm_pc_is_thumb (struct gdbarch *gdbarch, CORE_ADDR memaddr)
00383 {
00384   struct bound_minimal_symbol sym;
00385   char type;
00386   struct displaced_step_closure* dsc
00387     = get_displaced_step_closure_by_addr(memaddr);
00388 
00389   /* If checking the mode of displaced instruction in copy area, the mode
00390      should be determined by instruction on the original address.  */
00391   if (dsc)
00392     {
00393       if (debug_displaced)
00394         fprintf_unfiltered (gdb_stdlog,
00395                             "displaced: check mode of %.8lx instead of %.8lx\n",
00396                             (unsigned long) dsc->insn_addr,
00397                             (unsigned long) memaddr);
00398       memaddr = dsc->insn_addr;
00399     }
00400 
00401   /* If bit 0 of the address is set, assume this is a Thumb address.  */
00402   if (IS_THUMB_ADDR (memaddr))
00403     return 1;
00404 
00405   /* Respect internal mode override if active.  */
00406   if (arm_override_mode != -1)
00407     return arm_override_mode;
00408 
00409   /* If the user wants to override the symbol table, let him.  */
00410   if (strcmp (arm_force_mode_string, "arm") == 0)
00411     return 0;
00412   if (strcmp (arm_force_mode_string, "thumb") == 0)
00413     return 1;
00414 
00415   /* ARM v6-M and v7-M are always in Thumb mode.  */
00416   if (gdbarch_tdep (gdbarch)->is_m)
00417     return 1;
00418 
00419   /* If there are mapping symbols, consult them.  */
00420   type = arm_find_mapping_symbol (memaddr, NULL);
00421   if (type)
00422     return type == 't';
00423 
00424   /* Thumb functions have a "special" bit set in minimal symbols.  */
00425   sym = lookup_minimal_symbol_by_pc (memaddr);
00426   if (sym.minsym)
00427     return (MSYMBOL_IS_SPECIAL (sym.minsym));
00428 
00429   /* If the user wants to override the fallback mode, let them.  */
00430   if (strcmp (arm_fallback_mode_string, "arm") == 0)
00431     return 0;
00432   if (strcmp (arm_fallback_mode_string, "thumb") == 0)
00433     return 1;
00434 
00435   /* If we couldn't find any symbol, but we're talking to a running
00436      target, then trust the current value of $cpsr.  This lets
00437      "display/i $pc" always show the correct mode (though if there is
00438      a symbol table we will not reach here, so it still may not be
00439      displayed in the mode it will be executed).  */
00440   if (target_has_registers)
00441     return arm_frame_is_thumb (get_current_frame ());
00442 
00443   /* Otherwise we're out of luck; we assume ARM.  */
00444   return 0;
00445 }
00446 
00447 /* Remove useless bits from addresses in a running program.  */
00448 static CORE_ADDR
00449 arm_addr_bits_remove (struct gdbarch *gdbarch, CORE_ADDR val)
00450 {
00451   /* On M-profile devices, do not strip the low bit from EXC_RETURN
00452      (the magic exception return address).  */
00453   if (gdbarch_tdep (gdbarch)->is_m
00454       && (val & 0xfffffff0) == 0xfffffff0)
00455     return val;
00456 
00457   if (arm_apcs_32)
00458     return UNMAKE_THUMB_ADDR (val);
00459   else
00460     return (val & 0x03fffffc);
00461 }
00462 
00463 /* Return 1 if PC is the start of a compiler helper function which
00464    can be safely ignored during prologue skipping.  IS_THUMB is true
00465    if the function is known to be a Thumb function due to the way it
00466    is being called.  */
00467 static int
00468 skip_prologue_function (struct gdbarch *gdbarch, CORE_ADDR pc, int is_thumb)
00469 {
00470   enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
00471   struct bound_minimal_symbol msym;
00472 
00473   msym = lookup_minimal_symbol_by_pc (pc);
00474   if (msym.minsym != NULL
00475       && SYMBOL_VALUE_ADDRESS (msym.minsym) == pc
00476       && SYMBOL_LINKAGE_NAME (msym.minsym) != NULL)
00477     {
00478       const char *name = SYMBOL_LINKAGE_NAME (msym.minsym);
00479 
00480       /* The GNU linker's Thumb call stub to foo is named
00481          __foo_from_thumb.  */
00482       if (strstr (name, "_from_thumb") != NULL)
00483         name += 2;
00484 
00485       /* On soft-float targets, __truncdfsf2 is called to convert promoted
00486          arguments to their argument types in non-prototyped
00487          functions.  */
00488       if (strncmp (name, "__truncdfsf2", strlen ("__truncdfsf2")) == 0)
00489         return 1;
00490       if (strncmp (name, "__aeabi_d2f", strlen ("__aeabi_d2f")) == 0)
00491         return 1;
00492 
00493       /* Internal functions related to thread-local storage.  */
00494       if (strncmp (name, "__tls_get_addr", strlen ("__tls_get_addr")) == 0)
00495         return 1;
00496       if (strncmp (name, "__aeabi_read_tp", strlen ("__aeabi_read_tp")) == 0)
00497         return 1;
00498     }
00499   else
00500     {
00501       /* If we run against a stripped glibc, we may be unable to identify
00502          special functions by name.  Check for one important case,
00503          __aeabi_read_tp, by comparing the *code* against the default
00504          implementation (this is hand-written ARM assembler in glibc).  */
00505 
00506       if (!is_thumb
00507           && read_memory_unsigned_integer (pc, 4, byte_order_for_code)
00508              == 0xe3e00a0f /* mov r0, #0xffff0fff */
00509           && read_memory_unsigned_integer (pc + 4, 4, byte_order_for_code)
00510              == 0xe240f01f) /* sub pc, r0, #31 */
00511         return 1;
00512     }
00513 
00514   return 0;
00515 }
00516 
00517 /* Support routines for instruction parsing.  */
00518 #define submask(x) ((1L << ((x) + 1)) - 1)
00519 #define bit(obj,st) (((obj) >> (st)) & 1)
00520 #define bits(obj,st,fn) (((obj) >> (st)) & submask ((fn) - (st)))
00521 #define sbits(obj,st,fn) \
00522   ((long) (bits(obj,st,fn) | ((long) bit(obj,fn) * ~ submask (fn - st))))
00523 #define BranchDest(addr,instr) \
00524   ((CORE_ADDR) (((unsigned long) (addr)) + 8 + (sbits (instr, 0, 23) << 2)))
00525 
00526 /* Extract the immediate from instruction movw/movt of encoding T.  INSN1 is
00527    the first 16-bit of instruction, and INSN2 is the second 16-bit of
00528    instruction.  */
00529 #define EXTRACT_MOVW_MOVT_IMM_T(insn1, insn2) \
00530   ((bits ((insn1), 0, 3) << 12)               \
00531    | (bits ((insn1), 10, 10) << 11)           \
00532    | (bits ((insn2), 12, 14) << 8)            \
00533    | bits ((insn2), 0, 7))
00534 
00535 /* Extract the immediate from instruction movw/movt of encoding A.  INSN is
00536    the 32-bit instruction.  */
00537 #define EXTRACT_MOVW_MOVT_IMM_A(insn) \
00538   ((bits ((insn), 16, 19) << 12) \
00539    | bits ((insn), 0, 11))
00540 
00541 /* Decode immediate value; implements ThumbExpandImmediate pseudo-op.  */
00542 
00543 static unsigned int
00544 thumb_expand_immediate (unsigned int imm)
00545 {
00546   unsigned int count = imm >> 7;
00547 
00548   if (count < 8)
00549     switch (count / 2)
00550       {
00551       case 0:
00552         return imm & 0xff;
00553       case 1:
00554         return (imm & 0xff) | ((imm & 0xff) << 16);
00555       case 2:
00556         return ((imm & 0xff) << 8) | ((imm & 0xff) << 24);
00557       case 3:
00558         return (imm & 0xff) | ((imm & 0xff) << 8)
00559                 | ((imm & 0xff) << 16) | ((imm & 0xff) << 24);
00560       }
00561 
00562   return (0x80 | (imm & 0x7f)) << (32 - count);
00563 }
00564 
00565 /* Return 1 if the 16-bit Thumb instruction INST might change
00566    control flow, 0 otherwise.  */
00567 
00568 static int
00569 thumb_instruction_changes_pc (unsigned short inst)
00570 {
00571   if ((inst & 0xff00) == 0xbd00)        /* pop {rlist, pc} */
00572     return 1;
00573 
00574   if ((inst & 0xf000) == 0xd000)        /* conditional branch */
00575     return 1;
00576 
00577   if ((inst & 0xf800) == 0xe000)        /* unconditional branch */
00578     return 1;
00579 
00580   if ((inst & 0xff00) == 0x4700)        /* bx REG, blx REG */
00581     return 1;
00582 
00583   if ((inst & 0xff87) == 0x4687)        /* mov pc, REG */
00584     return 1;
00585 
00586   if ((inst & 0xf500) == 0xb100)        /* CBNZ or CBZ.  */
00587     return 1;
00588 
00589   return 0;
00590 }
00591 
00592 /* Return 1 if the 32-bit Thumb instruction in INST1 and INST2
00593    might change control flow, 0 otherwise.  */
00594 
00595 static int
00596 thumb2_instruction_changes_pc (unsigned short inst1, unsigned short inst2)
00597 {
00598   if ((inst1 & 0xf800) == 0xf000 && (inst2 & 0x8000) == 0x8000)
00599     {
00600       /* Branches and miscellaneous control instructions.  */
00601 
00602       if ((inst2 & 0x1000) != 0 || (inst2 & 0xd001) == 0xc000)
00603         {
00604           /* B, BL, BLX.  */
00605           return 1;
00606         }
00607       else if (inst1 == 0xf3de && (inst2 & 0xff00) == 0x3f00)
00608         {
00609           /* SUBS PC, LR, #imm8.  */
00610           return 1;
00611         }
00612       else if ((inst2 & 0xd000) == 0x8000 && (inst1 & 0x0380) != 0x0380)
00613         {
00614           /* Conditional branch.  */
00615           return 1;
00616         }
00617 
00618       return 0;
00619     }
00620 
00621   if ((inst1 & 0xfe50) == 0xe810)
00622     {
00623       /* Load multiple or RFE.  */
00624 
00625       if (bit (inst1, 7) && !bit (inst1, 8))
00626         {
00627           /* LDMIA or POP */
00628           if (bit (inst2, 15))
00629             return 1;
00630         }
00631       else if (!bit (inst1, 7) && bit (inst1, 8))
00632         {
00633           /* LDMDB */
00634           if (bit (inst2, 15))
00635             return 1;
00636         }
00637       else if (bit (inst1, 7) && bit (inst1, 8))
00638         {
00639           /* RFEIA */
00640           return 1;
00641         }
00642       else if (!bit (inst1, 7) && !bit (inst1, 8))
00643         {
00644           /* RFEDB */
00645           return 1;
00646         }
00647 
00648       return 0;
00649     }
00650 
00651   if ((inst1 & 0xffef) == 0xea4f && (inst2 & 0xfff0) == 0x0f00)
00652     {
00653       /* MOV PC or MOVS PC.  */
00654       return 1;
00655     }
00656 
00657   if ((inst1 & 0xff70) == 0xf850 && (inst2 & 0xf000) == 0xf000)
00658     {
00659       /* LDR PC.  */
00660       if (bits (inst1, 0, 3) == 15)
00661         return 1;
00662       if (bit (inst1, 7))
00663         return 1;
00664       if (bit (inst2, 11))
00665         return 1;
00666       if ((inst2 & 0x0fc0) == 0x0000)
00667         return 1;       
00668 
00669       return 0;
00670     }
00671 
00672   if ((inst1 & 0xfff0) == 0xe8d0 && (inst2 & 0xfff0) == 0xf000)
00673     {
00674       /* TBB.  */
00675       return 1;
00676     }
00677 
00678   if ((inst1 & 0xfff0) == 0xe8d0 && (inst2 & 0xfff0) == 0xf010)
00679     {
00680       /* TBH.  */
00681       return 1;
00682     }
00683 
00684   return 0;
00685 }
00686 
00687 /* Analyze a Thumb prologue, looking for a recognizable stack frame
00688    and frame pointer.  Scan until we encounter a store that could
00689    clobber the stack frame unexpectedly, or an unknown instruction.
00690    Return the last address which is definitely safe to skip for an
00691    initial breakpoint.  */
00692 
00693 static CORE_ADDR
00694 thumb_analyze_prologue (struct gdbarch *gdbarch,
00695                         CORE_ADDR start, CORE_ADDR limit,
00696                         struct arm_prologue_cache *cache)
00697 {
00698   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
00699   enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
00700   int i;
00701   pv_t regs[16];
00702   struct pv_area *stack;
00703   struct cleanup *back_to;
00704   CORE_ADDR offset;
00705   CORE_ADDR unrecognized_pc = 0;
00706 
00707   for (i = 0; i < 16; i++)
00708     regs[i] = pv_register (i, 0);
00709   stack = make_pv_area (ARM_SP_REGNUM, gdbarch_addr_bit (gdbarch));
00710   back_to = make_cleanup_free_pv_area (stack);
00711 
00712   while (start < limit)
00713     {
00714       unsigned short insn;
00715 
00716       insn = read_memory_unsigned_integer (start, 2, byte_order_for_code);
00717 
00718       if ((insn & 0xfe00) == 0xb400)            /* push { rlist } */
00719         {
00720           int regno;
00721           int mask;
00722 
00723           if (pv_area_store_would_trash (stack, regs[ARM_SP_REGNUM]))
00724             break;
00725 
00726           /* Bits 0-7 contain a mask for registers R0-R7.  Bit 8 says
00727              whether to save LR (R14).  */
00728           mask = (insn & 0xff) | ((insn & 0x100) << 6);
00729 
00730           /* Calculate offsets of saved R0-R7 and LR.  */
00731           for (regno = ARM_LR_REGNUM; regno >= 0; regno--)
00732             if (mask & (1 << regno))
00733               {
00734                 regs[ARM_SP_REGNUM] = pv_add_constant (regs[ARM_SP_REGNUM],
00735                                                        -4);
00736                 pv_area_store (stack, regs[ARM_SP_REGNUM], 4, regs[regno]);
00737               }
00738         }
00739       else if ((insn & 0xff00) == 0xb000)       /* add sp, #simm  OR  
00740                                                    sub sp, #simm */
00741         {
00742           offset = (insn & 0x7f) << 2;          /* get scaled offset */
00743           if (insn & 0x80)                      /* Check for SUB.  */
00744             regs[ARM_SP_REGNUM] = pv_add_constant (regs[ARM_SP_REGNUM],
00745                                                    -offset);
00746           else
00747             regs[ARM_SP_REGNUM] = pv_add_constant (regs[ARM_SP_REGNUM],
00748                                                    offset);
00749         }
00750       else if ((insn & 0xf800) == 0xa800)       /* add Rd, sp, #imm */
00751         regs[bits (insn, 8, 10)] = pv_add_constant (regs[ARM_SP_REGNUM],
00752                                                     (insn & 0xff) << 2);
00753       else if ((insn & 0xfe00) == 0x1c00        /* add Rd, Rn, #imm */
00754                && pv_is_register (regs[bits (insn, 3, 5)], ARM_SP_REGNUM))
00755         regs[bits (insn, 0, 2)] = pv_add_constant (regs[bits (insn, 3, 5)],
00756                                                    bits (insn, 6, 8));
00757       else if ((insn & 0xf800) == 0x3000        /* add Rd, #imm */
00758                && pv_is_register (regs[bits (insn, 8, 10)], ARM_SP_REGNUM))
00759         regs[bits (insn, 8, 10)] = pv_add_constant (regs[bits (insn, 8, 10)],
00760                                                     bits (insn, 0, 7));
00761       else if ((insn & 0xfe00) == 0x1800        /* add Rd, Rn, Rm */
00762                && pv_is_register (regs[bits (insn, 6, 8)], ARM_SP_REGNUM)
00763                && pv_is_constant (regs[bits (insn, 3, 5)]))
00764         regs[bits (insn, 0, 2)] = pv_add (regs[bits (insn, 3, 5)],
00765                                           regs[bits (insn, 6, 8)]);
00766       else if ((insn & 0xff00) == 0x4400        /* add Rd, Rm */
00767                && pv_is_constant (regs[bits (insn, 3, 6)]))
00768         {
00769           int rd = (bit (insn, 7) << 3) + bits (insn, 0, 2);
00770           int rm = bits (insn, 3, 6);
00771           regs[rd] = pv_add (regs[rd], regs[rm]);
00772         }
00773       else if ((insn & 0xff00) == 0x4600)       /* mov hi, lo or mov lo, hi */
00774         {
00775           int dst_reg = (insn & 0x7) + ((insn & 0x80) >> 4);
00776           int src_reg = (insn & 0x78) >> 3;
00777           regs[dst_reg] = regs[src_reg];
00778         }
00779       else if ((insn & 0xf800) == 0x9000)       /* str rd, [sp, #off] */
00780         {
00781           /* Handle stores to the stack.  Normally pushes are used,
00782              but with GCC -mtpcs-frame, there may be other stores
00783              in the prologue to create the frame.  */
00784           int regno = (insn >> 8) & 0x7;
00785           pv_t addr;
00786 
00787           offset = (insn & 0xff) << 2;
00788           addr = pv_add_constant (regs[ARM_SP_REGNUM], offset);
00789 
00790           if (pv_area_store_would_trash (stack, addr))
00791             break;
00792 
00793           pv_area_store (stack, addr, 4, regs[regno]);
00794         }
00795       else if ((insn & 0xf800) == 0x6000)       /* str rd, [rn, #off] */
00796         {
00797           int rd = bits (insn, 0, 2);
00798           int rn = bits (insn, 3, 5);
00799           pv_t addr;
00800 
00801           offset = bits (insn, 6, 10) << 2;
00802           addr = pv_add_constant (regs[rn], offset);
00803 
00804           if (pv_area_store_would_trash (stack, addr))
00805             break;
00806 
00807           pv_area_store (stack, addr, 4, regs[rd]);
00808         }
00809       else if (((insn & 0xf800) == 0x7000       /* strb Rd, [Rn, #off] */
00810                 || (insn & 0xf800) == 0x8000)   /* strh Rd, [Rn, #off] */
00811                && pv_is_register (regs[bits (insn, 3, 5)], ARM_SP_REGNUM))
00812         /* Ignore stores of argument registers to the stack.  */
00813         ;
00814       else if ((insn & 0xf800) == 0xc800        /* ldmia Rn!, { registers } */
00815                && pv_is_register (regs[bits (insn, 8, 10)], ARM_SP_REGNUM))
00816         /* Ignore block loads from the stack, potentially copying
00817            parameters from memory.  */
00818         ;
00819       else if ((insn & 0xf800) == 0x9800        /* ldr Rd, [Rn, #immed] */
00820                || ((insn & 0xf800) == 0x6800    /* ldr Rd, [sp, #immed] */
00821                    && pv_is_register (regs[bits (insn, 3, 5)], ARM_SP_REGNUM)))
00822         /* Similarly ignore single loads from the stack.  */
00823         ;
00824       else if ((insn & 0xffc0) == 0x0000        /* lsls Rd, Rm, #0 */
00825                || (insn & 0xffc0) == 0x1c00)    /* add Rd, Rn, #0 */
00826         /* Skip register copies, i.e. saves to another register
00827            instead of the stack.  */
00828         ;
00829       else if ((insn & 0xf800) == 0x2000)       /* movs Rd, #imm */
00830         /* Recognize constant loads; even with small stacks these are necessary
00831            on Thumb.  */
00832         regs[bits (insn, 8, 10)] = pv_constant (bits (insn, 0, 7));
00833       else if ((insn & 0xf800) == 0x4800)       /* ldr Rd, [pc, #imm] */
00834         {
00835           /* Constant pool loads, for the same reason.  */
00836           unsigned int constant;
00837           CORE_ADDR loc;
00838 
00839           loc = start + 4 + bits (insn, 0, 7) * 4;
00840           constant = read_memory_unsigned_integer (loc, 4, byte_order);
00841           regs[bits (insn, 8, 10)] = pv_constant (constant);
00842         }
00843       else if (thumb_insn_size (insn) == 4) /* 32-bit Thumb-2 instructions.  */
00844         {
00845           unsigned short inst2;
00846 
00847           inst2 = read_memory_unsigned_integer (start + 2, 2,
00848                                                 byte_order_for_code);
00849 
00850           if ((insn & 0xf800) == 0xf000 && (inst2 & 0xe800) == 0xe800)
00851             {
00852               /* BL, BLX.  Allow some special function calls when
00853                  skipping the prologue; GCC generates these before
00854                  storing arguments to the stack.  */
00855               CORE_ADDR nextpc;
00856               int j1, j2, imm1, imm2;
00857 
00858               imm1 = sbits (insn, 0, 10);
00859               imm2 = bits (inst2, 0, 10);
00860               j1 = bit (inst2, 13);
00861               j2 = bit (inst2, 11);
00862 
00863               offset = ((imm1 << 12) + (imm2 << 1));
00864               offset ^= ((!j2) << 22) | ((!j1) << 23);
00865 
00866               nextpc = start + 4 + offset;
00867               /* For BLX make sure to clear the low bits.  */
00868               if (bit (inst2, 12) == 0)
00869                 nextpc = nextpc & 0xfffffffc;
00870 
00871               if (!skip_prologue_function (gdbarch, nextpc,
00872                                            bit (inst2, 12) != 0))
00873                 break;
00874             }
00875 
00876           else if ((insn & 0xffd0) == 0xe900    /* stmdb Rn{!},
00877                                                    { registers } */
00878                    && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
00879             {
00880               pv_t addr = regs[bits (insn, 0, 3)];
00881               int regno;
00882 
00883               if (pv_area_store_would_trash (stack, addr))
00884                 break;
00885 
00886               /* Calculate offsets of saved registers.  */
00887               for (regno = ARM_LR_REGNUM; regno >= 0; regno--)
00888                 if (inst2 & (1 << regno))
00889                   {
00890                     addr = pv_add_constant (addr, -4);
00891                     pv_area_store (stack, addr, 4, regs[regno]);
00892                   }
00893 
00894               if (insn & 0x0020)
00895                 regs[bits (insn, 0, 3)] = addr;
00896             }
00897 
00898           else if ((insn & 0xff50) == 0xe940    /* strd Rt, Rt2,
00899                                                    [Rn, #+/-imm]{!} */
00900                    && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
00901             {
00902               int regno1 = bits (inst2, 12, 15);
00903               int regno2 = bits (inst2, 8, 11);
00904               pv_t addr = regs[bits (insn, 0, 3)];
00905 
00906               offset = inst2 & 0xff;
00907               if (insn & 0x0080)
00908                 addr = pv_add_constant (addr, offset);
00909               else
00910                 addr = pv_add_constant (addr, -offset);
00911 
00912               if (pv_area_store_would_trash (stack, addr))
00913                 break;
00914 
00915               pv_area_store (stack, addr, 4, regs[regno1]);
00916               pv_area_store (stack, pv_add_constant (addr, 4),
00917                              4, regs[regno2]);
00918 
00919               if (insn & 0x0020)
00920                 regs[bits (insn, 0, 3)] = addr;
00921             }
00922 
00923           else if ((insn & 0xfff0) == 0xf8c0    /* str Rt,[Rn,+/-#imm]{!} */
00924                    && (inst2 & 0x0c00) == 0x0c00
00925                    && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
00926             {
00927               int regno = bits (inst2, 12, 15);
00928               pv_t addr = regs[bits (insn, 0, 3)];
00929 
00930               offset = inst2 & 0xff;
00931               if (inst2 & 0x0200)
00932                 addr = pv_add_constant (addr, offset);
00933               else
00934                 addr = pv_add_constant (addr, -offset);
00935 
00936               if (pv_area_store_would_trash (stack, addr))
00937                 break;
00938 
00939               pv_area_store (stack, addr, 4, regs[regno]);
00940 
00941               if (inst2 & 0x0100)
00942                 regs[bits (insn, 0, 3)] = addr;
00943             }
00944 
00945           else if ((insn & 0xfff0) == 0xf8c0    /* str.w Rt,[Rn,#imm] */
00946                    && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
00947             {
00948               int regno = bits (inst2, 12, 15);
00949               pv_t addr;
00950 
00951               offset = inst2 & 0xfff;
00952               addr = pv_add_constant (regs[bits (insn, 0, 3)], offset);
00953 
00954               if (pv_area_store_would_trash (stack, addr))
00955                 break;
00956 
00957               pv_area_store (stack, addr, 4, regs[regno]);
00958             }
00959 
00960           else if ((insn & 0xffd0) == 0xf880    /* str{bh}.w Rt,[Rn,#imm] */
00961                    && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
00962             /* Ignore stores of argument registers to the stack.  */
00963             ;
00964 
00965           else if ((insn & 0xffd0) == 0xf800    /* str{bh} Rt,[Rn,#+/-imm] */
00966                    && (inst2 & 0x0d00) == 0x0c00
00967                    && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
00968             /* Ignore stores of argument registers to the stack.  */
00969             ;
00970 
00971           else if ((insn & 0xffd0) == 0xe890    /* ldmia Rn[!],
00972                                                    { registers } */
00973                    && (inst2 & 0x8000) == 0x0000
00974                    && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
00975             /* Ignore block loads from the stack, potentially copying
00976                parameters from memory.  */
00977             ;
00978 
00979           else if ((insn & 0xffb0) == 0xe950    /* ldrd Rt, Rt2,
00980                                                    [Rn, #+/-imm] */
00981                    && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
00982             /* Similarly ignore dual loads from the stack.  */
00983             ;
00984 
00985           else if ((insn & 0xfff0) == 0xf850    /* ldr Rt,[Rn,#+/-imm] */
00986                    && (inst2 & 0x0d00) == 0x0c00
00987                    && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
00988             /* Similarly ignore single loads from the stack.  */
00989             ;
00990 
00991           else if ((insn & 0xfff0) == 0xf8d0    /* ldr.w Rt,[Rn,#imm] */
00992                    && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
00993             /* Similarly ignore single loads from the stack.  */
00994             ;
00995 
00996           else if ((insn & 0xfbf0) == 0xf100    /* add.w Rd, Rn, #imm */
00997                    && (inst2 & 0x8000) == 0x0000)
00998             {
00999               unsigned int imm = ((bits (insn, 10, 10) << 11)
01000                                   | (bits (inst2, 12, 14) << 8)
01001                                   | bits (inst2, 0, 7));
01002 
01003               regs[bits (inst2, 8, 11)]
01004                 = pv_add_constant (regs[bits (insn, 0, 3)],
01005                                    thumb_expand_immediate (imm));
01006             }
01007 
01008           else if ((insn & 0xfbf0) == 0xf200    /* addw Rd, Rn, #imm */
01009                    && (inst2 & 0x8000) == 0x0000)
01010             {
01011               unsigned int imm = ((bits (insn, 10, 10) << 11)
01012                                   | (bits (inst2, 12, 14) << 8)
01013                                   | bits (inst2, 0, 7));
01014 
01015               regs[bits (inst2, 8, 11)]
01016                 = pv_add_constant (regs[bits (insn, 0, 3)], imm);
01017             }
01018 
01019           else if ((insn & 0xfbf0) == 0xf1a0    /* sub.w Rd, Rn, #imm */
01020                    && (inst2 & 0x8000) == 0x0000)
01021             {
01022               unsigned int imm = ((bits (insn, 10, 10) << 11)
01023                                   | (bits (inst2, 12, 14) << 8)
01024                                   | bits (inst2, 0, 7));
01025 
01026               regs[bits (inst2, 8, 11)]
01027                 = pv_add_constant (regs[bits (insn, 0, 3)],
01028                                    - (CORE_ADDR) thumb_expand_immediate (imm));
01029             }
01030 
01031           else if ((insn & 0xfbf0) == 0xf2a0    /* subw Rd, Rn, #imm */
01032                    && (inst2 & 0x8000) == 0x0000)
01033             {
01034               unsigned int imm = ((bits (insn, 10, 10) << 11)
01035                                   | (bits (inst2, 12, 14) << 8)
01036                                   | bits (inst2, 0, 7));
01037 
01038               regs[bits (inst2, 8, 11)]
01039                 = pv_add_constant (regs[bits (insn, 0, 3)], - (CORE_ADDR) imm);
01040             }
01041 
01042           else if ((insn & 0xfbff) == 0xf04f)   /* mov.w Rd, #const */
01043             {
01044               unsigned int imm = ((bits (insn, 10, 10) << 11)
01045                                   | (bits (inst2, 12, 14) << 8)
01046                                   | bits (inst2, 0, 7));
01047 
01048               regs[bits (inst2, 8, 11)]
01049                 = pv_constant (thumb_expand_immediate (imm));
01050             }
01051 
01052           else if ((insn & 0xfbf0) == 0xf240)   /* movw Rd, #const */
01053             {
01054               unsigned int imm
01055                 = EXTRACT_MOVW_MOVT_IMM_T (insn, inst2);
01056 
01057               regs[bits (inst2, 8, 11)] = pv_constant (imm);
01058             }
01059 
01060           else if (insn == 0xea5f               /* mov.w Rd,Rm */
01061                    && (inst2 & 0xf0f0) == 0)
01062             {
01063               int dst_reg = (inst2 & 0x0f00) >> 8;
01064               int src_reg = inst2 & 0xf;
01065               regs[dst_reg] = regs[src_reg];
01066             }
01067 
01068           else if ((insn & 0xff7f) == 0xf85f)   /* ldr.w Rt,<label> */
01069             {
01070               /* Constant pool loads.  */
01071               unsigned int constant;
01072               CORE_ADDR loc;
01073 
01074               offset = bits (insn, 0, 11);
01075               if (insn & 0x0080)
01076                 loc = start + 4 + offset;
01077               else
01078                 loc = start + 4 - offset;
01079 
01080               constant = read_memory_unsigned_integer (loc, 4, byte_order);
01081               regs[bits (inst2, 12, 15)] = pv_constant (constant);
01082             }
01083 
01084           else if ((insn & 0xff7f) == 0xe95f)   /* ldrd Rt,Rt2,<label> */
01085             {
01086               /* Constant pool loads.  */
01087               unsigned int constant;
01088               CORE_ADDR loc;
01089 
01090               offset = bits (insn, 0, 7) << 2;
01091               if (insn & 0x0080)
01092                 loc = start + 4 + offset;
01093               else
01094                 loc = start + 4 - offset;
01095 
01096               constant = read_memory_unsigned_integer (loc, 4, byte_order);
01097               regs[bits (inst2, 12, 15)] = pv_constant (constant);
01098 
01099               constant = read_memory_unsigned_integer (loc + 4, 4, byte_order);
01100               regs[bits (inst2, 8, 11)] = pv_constant (constant);
01101             }
01102 
01103           else if (thumb2_instruction_changes_pc (insn, inst2))
01104             {
01105               /* Don't scan past anything that might change control flow.  */
01106               break;
01107             }
01108           else
01109             {
01110               /* The optimizer might shove anything into the prologue,
01111                  so we just skip what we don't recognize.  */
01112               unrecognized_pc = start;
01113             }
01114 
01115           start += 2;
01116         }
01117       else if (thumb_instruction_changes_pc (insn))
01118         {
01119           /* Don't scan past anything that might change control flow.  */
01120           break;
01121         }
01122       else
01123         {
01124           /* The optimizer might shove anything into the prologue,
01125              so we just skip what we don't recognize.  */
01126           unrecognized_pc = start;
01127         }
01128 
01129       start += 2;
01130     }
01131 
01132   if (arm_debug)
01133     fprintf_unfiltered (gdb_stdlog, "Prologue scan stopped at %s\n",
01134                         paddress (gdbarch, start));
01135 
01136   if (unrecognized_pc == 0)
01137     unrecognized_pc = start;
01138 
01139   if (cache == NULL)
01140     {
01141       do_cleanups (back_to);
01142       return unrecognized_pc;
01143     }
01144 
01145   if (pv_is_register (regs[ARM_FP_REGNUM], ARM_SP_REGNUM))
01146     {
01147       /* Frame pointer is fp.  Frame size is constant.  */
01148       cache->framereg = ARM_FP_REGNUM;
01149       cache->framesize = -regs[ARM_FP_REGNUM].k;
01150     }
01151   else if (pv_is_register (regs[THUMB_FP_REGNUM], ARM_SP_REGNUM))
01152     {
01153       /* Frame pointer is r7.  Frame size is constant.  */
01154       cache->framereg = THUMB_FP_REGNUM;
01155       cache->framesize = -regs[THUMB_FP_REGNUM].k;
01156     }
01157   else
01158     {
01159       /* Try the stack pointer... this is a bit desperate.  */
01160       cache->framereg = ARM_SP_REGNUM;
01161       cache->framesize = -regs[ARM_SP_REGNUM].k;
01162     }
01163 
01164   for (i = 0; i < 16; i++)
01165     if (pv_area_find_reg (stack, gdbarch, i, &offset))
01166       cache->saved_regs[i].addr = offset;
01167 
01168   do_cleanups (back_to);
01169   return unrecognized_pc;
01170 }
01171 
01172 
01173 /* Try to analyze the instructions starting from PC, which load symbol
01174    __stack_chk_guard.  Return the address of instruction after loading this
01175    symbol, set the dest register number to *BASEREG, and set the size of
01176    instructions for loading symbol in OFFSET.  Return 0 if instructions are
01177    not recognized.  */
01178 
01179 static CORE_ADDR
01180 arm_analyze_load_stack_chk_guard(CORE_ADDR pc, struct gdbarch *gdbarch,
01181                                  unsigned int *destreg, int *offset)
01182 {
01183   enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
01184   int is_thumb = arm_pc_is_thumb (gdbarch, pc);
01185   unsigned int low, high, address;
01186 
01187   address = 0;
01188   if (is_thumb)
01189     {
01190       unsigned short insn1
01191         = read_memory_unsigned_integer (pc, 2, byte_order_for_code);
01192 
01193       if ((insn1 & 0xf800) == 0x4800) /* ldr Rd, #immed */
01194         {
01195           *destreg = bits (insn1, 8, 10);
01196           *offset = 2;
01197           address = bits (insn1, 0, 7);
01198         }
01199       else if ((insn1 & 0xfbf0) == 0xf240) /* movw Rd, #const */
01200         {
01201           unsigned short insn2
01202             = read_memory_unsigned_integer (pc + 2, 2, byte_order_for_code);
01203 
01204           low = EXTRACT_MOVW_MOVT_IMM_T (insn1, insn2);
01205 
01206           insn1
01207             = read_memory_unsigned_integer (pc + 4, 2, byte_order_for_code);
01208           insn2
01209             = read_memory_unsigned_integer (pc + 6, 2, byte_order_for_code);
01210 
01211           /* movt Rd, #const */
01212           if ((insn1 & 0xfbc0) == 0xf2c0)
01213             {
01214               high = EXTRACT_MOVW_MOVT_IMM_T (insn1, insn2);
01215               *destreg = bits (insn2, 8, 11);
01216               *offset = 8;
01217               address = (high << 16 | low);
01218             }
01219         }
01220     }
01221   else
01222     {
01223       unsigned int insn
01224         = read_memory_unsigned_integer (pc, 4, byte_order_for_code);
01225 
01226       if ((insn & 0x0e5f0000) == 0x041f0000) /* ldr Rd, #immed */
01227         {
01228           address = bits (insn, 0, 11);
01229           *destreg = bits (insn, 12, 15);
01230           *offset = 4;
01231         }
01232       else if ((insn & 0x0ff00000) == 0x03000000) /* movw Rd, #const */
01233         {
01234           low = EXTRACT_MOVW_MOVT_IMM_A (insn);
01235 
01236           insn
01237             = read_memory_unsigned_integer (pc + 4, 4, byte_order_for_code);
01238 
01239           if ((insn & 0x0ff00000) == 0x03400000) /* movt Rd, #const */
01240             {
01241               high = EXTRACT_MOVW_MOVT_IMM_A (insn);
01242               *destreg = bits (insn, 12, 15);
01243               *offset = 8;
01244               address = (high << 16 | low);
01245             }
01246         }
01247     }
01248 
01249   return address;
01250 }
01251 
01252 /* Try to skip a sequence of instructions used for stack protector.  If PC
01253    points to the first instruction of this sequence, return the address of
01254    first instruction after this sequence, otherwise, return original PC.
01255 
01256    On arm, this sequence of instructions is composed of mainly three steps,
01257      Step 1: load symbol __stack_chk_guard,
01258      Step 2: load from address of __stack_chk_guard,
01259      Step 3: store it to somewhere else.
01260 
01261    Usually, instructions on step 2 and step 3 are the same on various ARM
01262    architectures.  On step 2, it is one instruction 'ldr Rx, [Rn, #0]', and
01263    on step 3, it is also one instruction 'str Rx, [r7, #immd]'.  However,
01264    instructions in step 1 vary from different ARM architectures.  On ARMv7,
01265    they are,
01266 
01267         movw    Rn, #:lower16:__stack_chk_guard
01268         movt    Rn, #:upper16:__stack_chk_guard
01269 
01270    On ARMv5t, it is,
01271 
01272         ldr     Rn, .Label
01273         ....
01274         .Lable:
01275         .word   __stack_chk_guard
01276 
01277    Since ldr/str is a very popular instruction, we can't use them as
01278    'fingerprint' or 'signature' of stack protector sequence.  Here we choose
01279    sequence {movw/movt, ldr}/ldr/str plus symbol __stack_chk_guard, if not
01280    stripped, as the 'fingerprint' of a stack protector cdoe sequence.  */
01281 
01282 static CORE_ADDR
01283 arm_skip_stack_protector(CORE_ADDR pc, struct gdbarch *gdbarch)
01284 {
01285   enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
01286   unsigned int basereg;
01287   struct bound_minimal_symbol stack_chk_guard;
01288   int offset;
01289   int is_thumb = arm_pc_is_thumb (gdbarch, pc);
01290   CORE_ADDR addr;
01291 
01292   /* Try to parse the instructions in Step 1.  */
01293   addr = arm_analyze_load_stack_chk_guard (pc, gdbarch,
01294                                            &basereg, &offset);
01295   if (!addr)
01296     return pc;
01297 
01298   stack_chk_guard = lookup_minimal_symbol_by_pc (addr);
01299   /* If name of symbol doesn't start with '__stack_chk_guard', this
01300      instruction sequence is not for stack protector.  If symbol is
01301      removed, we conservatively think this sequence is for stack protector.  */
01302   if (stack_chk_guard.minsym
01303       && strncmp (SYMBOL_LINKAGE_NAME (stack_chk_guard.minsym),
01304                   "__stack_chk_guard",
01305                   strlen ("__stack_chk_guard")) != 0)
01306    return pc;
01307 
01308   if (is_thumb)
01309     {
01310       unsigned int destreg;
01311       unsigned short insn
01312         = read_memory_unsigned_integer (pc + offset, 2, byte_order_for_code);
01313 
01314       /* Step 2: ldr Rd, [Rn, #immed], encoding T1.  */
01315       if ((insn & 0xf800) != 0x6800)
01316         return pc;
01317       if (bits (insn, 3, 5) != basereg)
01318         return pc;
01319       destreg = bits (insn, 0, 2);
01320 
01321       insn = read_memory_unsigned_integer (pc + offset + 2, 2,
01322                                            byte_order_for_code);
01323       /* Step 3: str Rd, [Rn, #immed], encoding T1.  */
01324       if ((insn & 0xf800) != 0x6000)
01325         return pc;
01326       if (destreg != bits (insn, 0, 2))
01327         return pc;
01328     }
01329   else
01330     {
01331       unsigned int destreg;
01332       unsigned int insn
01333         = read_memory_unsigned_integer (pc + offset, 4, byte_order_for_code);
01334 
01335       /* Step 2: ldr Rd, [Rn, #immed], encoding A1.  */
01336       if ((insn & 0x0e500000) != 0x04100000)
01337         return pc;
01338       if (bits (insn, 16, 19) != basereg)
01339         return pc;
01340       destreg = bits (insn, 12, 15);
01341       /* Step 3: str Rd, [Rn, #immed], encoding A1.  */
01342       insn = read_memory_unsigned_integer (pc + offset + 4,
01343                                            4, byte_order_for_code);
01344       if ((insn & 0x0e500000) != 0x04000000)
01345         return pc;
01346       if (bits (insn, 12, 15) != destreg)
01347         return pc;
01348     }
01349   /* The size of total two instructions ldr/str is 4 on Thumb-2, while 8
01350      on arm.  */
01351   if (is_thumb)
01352     return pc + offset + 4;
01353   else
01354     return pc + offset + 8;
01355 }
01356 
01357 /* Advance the PC across any function entry prologue instructions to
01358    reach some "real" code.
01359 
01360    The APCS (ARM Procedure Call Standard) defines the following
01361    prologue:
01362 
01363    mov          ip, sp
01364    [stmfd       sp!, {a1,a2,a3,a4}]
01365    stmfd        sp!, {...,fp,ip,lr,pc}
01366    [stfe        f7, [sp, #-12]!]
01367    [stfe        f6, [sp, #-12]!]
01368    [stfe        f5, [sp, #-12]!]
01369    [stfe        f4, [sp, #-12]!]
01370    sub fp, ip, #nn @@ nn == 20 or 4 depending on second insn.  */
01371 
01372 static CORE_ADDR
01373 arm_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR pc)
01374 {
01375   enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
01376   unsigned long inst;
01377   CORE_ADDR skip_pc;
01378   CORE_ADDR func_addr, limit_pc;
01379 
01380   /* See if we can determine the end of the prologue via the symbol table.
01381      If so, then return either PC, or the PC after the prologue, whichever
01382      is greater.  */
01383   if (find_pc_partial_function (pc, NULL, &func_addr, NULL))
01384     {
01385       CORE_ADDR post_prologue_pc
01386         = skip_prologue_using_sal (gdbarch, func_addr);
01387       struct symtab *s = find_pc_symtab (func_addr);
01388 
01389       if (post_prologue_pc)
01390         post_prologue_pc
01391           = arm_skip_stack_protector (post_prologue_pc, gdbarch);
01392 
01393 
01394       /* GCC always emits a line note before the prologue and another
01395          one after, even if the two are at the same address or on the
01396          same line.  Take advantage of this so that we do not need to
01397          know every instruction that might appear in the prologue.  We
01398          will have producer information for most binaries; if it is
01399          missing (e.g. for -gstabs), assuming the GNU tools.  */
01400       if (post_prologue_pc
01401           && (s == NULL
01402               || s->producer == NULL
01403               || strncmp (s->producer, "GNU ", sizeof ("GNU ") - 1) == 0 
01404               || strncmp (s->producer, "clang ", sizeof ("clang ") - 1) == 0))
01405         return post_prologue_pc;
01406 
01407       if (post_prologue_pc != 0)
01408         {
01409           CORE_ADDR analyzed_limit;
01410 
01411           /* For non-GCC compilers, make sure the entire line is an
01412              acceptable prologue; GDB will round this function's
01413              return value up to the end of the following line so we
01414              can not skip just part of a line (and we do not want to).
01415 
01416              RealView does not treat the prologue specially, but does
01417              associate prologue code with the opening brace; so this
01418              lets us skip the first line if we think it is the opening
01419              brace.  */
01420           if (arm_pc_is_thumb (gdbarch, func_addr))
01421             analyzed_limit = thumb_analyze_prologue (gdbarch, func_addr,
01422                                                      post_prologue_pc, NULL);
01423           else
01424             analyzed_limit = arm_analyze_prologue (gdbarch, func_addr,
01425                                                    post_prologue_pc, NULL);
01426 
01427           if (analyzed_limit != post_prologue_pc)
01428             return func_addr;
01429 
01430           return post_prologue_pc;
01431         }
01432     }
01433 
01434   /* Can't determine prologue from the symbol table, need to examine
01435      instructions.  */
01436 
01437   /* Find an upper limit on the function prologue using the debug
01438      information.  If the debug information could not be used to provide
01439      that bound, then use an arbitrary large number as the upper bound.  */
01440   /* Like arm_scan_prologue, stop no later than pc + 64.  */
01441   limit_pc = skip_prologue_using_sal (gdbarch, pc);
01442   if (limit_pc == 0)
01443     limit_pc = pc + 64;          /* Magic.  */
01444 
01445 
01446   /* Check if this is Thumb code.  */
01447   if (arm_pc_is_thumb (gdbarch, pc))
01448     return thumb_analyze_prologue (gdbarch, pc, limit_pc, NULL);
01449 
01450   for (skip_pc = pc; skip_pc < limit_pc; skip_pc += 4)
01451     {
01452       inst = read_memory_unsigned_integer (skip_pc, 4, byte_order_for_code);
01453 
01454       /* "mov ip, sp" is no longer a required part of the prologue.  */
01455       if (inst == 0xe1a0c00d)                   /* mov ip, sp */
01456         continue;
01457 
01458       if ((inst & 0xfffff000) == 0xe28dc000)    /* add ip, sp #n */
01459         continue;
01460 
01461       if ((inst & 0xfffff000) == 0xe24dc000)    /* sub ip, sp #n */
01462         continue;
01463 
01464       /* Some prologues begin with "str lr, [sp, #-4]!".  */
01465       if (inst == 0xe52de004)                   /* str lr, [sp, #-4]! */
01466         continue;
01467 
01468       if ((inst & 0xfffffff0) == 0xe92d0000)    /* stmfd sp!,{a1,a2,a3,a4} */
01469         continue;
01470 
01471       if ((inst & 0xfffff800) == 0xe92dd800)    /* stmfd sp!,{fp,ip,lr,pc} */
01472         continue;
01473 
01474       /* Any insns after this point may float into the code, if it makes
01475          for better instruction scheduling, so we skip them only if we
01476          find them, but still consider the function to be frame-ful.  */
01477 
01478       /* We may have either one sfmfd instruction here, or several stfe
01479          insns, depending on the version of floating point code we
01480          support.  */
01481       if ((inst & 0xffbf0fff) == 0xec2d0200)    /* sfmfd fn, <cnt>, [sp]! */
01482         continue;
01483 
01484       if ((inst & 0xffff8fff) == 0xed6d0103)    /* stfe fn, [sp, #-12]! */
01485         continue;
01486 
01487       if ((inst & 0xfffff000) == 0xe24cb000)    /* sub fp, ip, #nn */
01488         continue;
01489 
01490       if ((inst & 0xfffff000) == 0xe24dd000)    /* sub sp, sp, #nn */
01491         continue;
01492 
01493       if ((inst & 0xffffc000) == 0xe54b0000     /* strb r(0123),[r11,#-nn] */
01494           || (inst & 0xffffc0f0) == 0xe14b00b0  /* strh r(0123),[r11,#-nn] */
01495           || (inst & 0xffffc000) == 0xe50b0000) /* str  r(0123),[r11,#-nn] */
01496         continue;
01497 
01498       if ((inst & 0xffffc000) == 0xe5cd0000     /* strb r(0123),[sp,#nn] */
01499           || (inst & 0xffffc0f0) == 0xe1cd00b0  /* strh r(0123),[sp,#nn] */
01500           || (inst & 0xffffc000) == 0xe58d0000) /* str  r(0123),[sp,#nn] */
01501         continue;
01502 
01503       /* Un-recognized instruction; stop scanning.  */
01504       break;
01505     }
01506 
01507   return skip_pc;               /* End of prologue.  */
01508 }
01509 
01510 /* *INDENT-OFF* */
01511 /* Function: thumb_scan_prologue (helper function for arm_scan_prologue)
01512    This function decodes a Thumb function prologue to determine:
01513      1) the size of the stack frame
01514      2) which registers are saved on it
01515      3) the offsets of saved regs
01516      4) the offset from the stack pointer to the frame pointer
01517 
01518    A typical Thumb function prologue would create this stack frame
01519    (offsets relative to FP)
01520      old SP ->  24  stack parameters
01521                 20  LR
01522                 16  R7
01523      R7 ->       0  local variables (16 bytes)
01524      SP ->     -12  additional stack space (12 bytes)
01525    The frame size would thus be 36 bytes, and the frame offset would be
01526    12 bytes.  The frame register is R7.
01527    
01528    The comments for thumb_skip_prolog() describe the algorithm we use
01529    to detect the end of the prolog.  */
01530 /* *INDENT-ON* */
01531 
01532 static void
01533 thumb_scan_prologue (struct gdbarch *gdbarch, CORE_ADDR prev_pc,
01534                      CORE_ADDR block_addr, struct arm_prologue_cache *cache)
01535 {
01536   CORE_ADDR prologue_start;
01537   CORE_ADDR prologue_end;
01538 
01539   if (find_pc_partial_function (block_addr, NULL, &prologue_start,
01540                                 &prologue_end))
01541     {
01542       /* See comment in arm_scan_prologue for an explanation of
01543          this heuristics.  */
01544       if (prologue_end > prologue_start + 64)
01545         {
01546           prologue_end = prologue_start + 64;
01547         }
01548     }
01549   else
01550     /* We're in the boondocks: we have no idea where the start of the
01551        function is.  */
01552     return;
01553 
01554   prologue_end = min (prologue_end, prev_pc);
01555 
01556   thumb_analyze_prologue (gdbarch, prologue_start, prologue_end, cache);
01557 }
01558 
01559 /* Return 1 if THIS_INSTR might change control flow, 0 otherwise.  */
01560 
01561 static int
01562 arm_instruction_changes_pc (uint32_t this_instr)
01563 {
01564   if (bits (this_instr, 28, 31) == INST_NV)
01565     /* Unconditional instructions.  */
01566     switch (bits (this_instr, 24, 27))
01567       {
01568       case 0xa:
01569       case 0xb:
01570         /* Branch with Link and change to Thumb.  */
01571         return 1;
01572       case 0xc:
01573       case 0xd:
01574       case 0xe:
01575         /* Coprocessor register transfer.  */
01576         if (bits (this_instr, 12, 15) == 15)
01577           error (_("Invalid update to pc in instruction"));
01578         return 0;
01579       default:
01580         return 0;
01581       }
01582   else
01583     switch (bits (this_instr, 25, 27))
01584       {
01585       case 0x0:
01586         if (bits (this_instr, 23, 24) == 2 && bit (this_instr, 20) == 0)
01587           {
01588             /* Multiplies and extra load/stores.  */
01589             if (bit (this_instr, 4) == 1 && bit (this_instr, 7) == 1)
01590               /* Neither multiplies nor extension load/stores are allowed
01591                  to modify PC.  */
01592               return 0;
01593 
01594             /* Otherwise, miscellaneous instructions.  */
01595 
01596             /* BX <reg>, BXJ <reg>, BLX <reg> */
01597             if (bits (this_instr, 4, 27) == 0x12fff1
01598                 || bits (this_instr, 4, 27) == 0x12fff2
01599                 || bits (this_instr, 4, 27) == 0x12fff3)
01600               return 1;
01601 
01602             /* Other miscellaneous instructions are unpredictable if they
01603                modify PC.  */
01604             return 0;
01605           }
01606         /* Data processing instruction.  Fall through.  */
01607 
01608       case 0x1:
01609         if (bits (this_instr, 12, 15) == 15)
01610           return 1;
01611         else
01612           return 0;
01613 
01614       case 0x2:
01615       case 0x3:
01616         /* Media instructions and architecturally undefined instructions.  */
01617         if (bits (this_instr, 25, 27) == 3 && bit (this_instr, 4) == 1)
01618           return 0;
01619 
01620         /* Stores.  */
01621         if (bit (this_instr, 20) == 0)
01622           return 0;
01623 
01624         /* Loads.  */
01625         if (bits (this_instr, 12, 15) == ARM_PC_REGNUM)
01626           return 1;
01627         else
01628           return 0;
01629 
01630       case 0x4:
01631         /* Load/store multiple.  */
01632         if (bit (this_instr, 20) == 1 && bit (this_instr, 15) == 1)
01633           return 1;
01634         else
01635           return 0;
01636 
01637       case 0x5:
01638         /* Branch and branch with link.  */
01639         return 1;
01640 
01641       case 0x6:
01642       case 0x7:
01643         /* Coprocessor transfers or SWIs can not affect PC.  */
01644         return 0;
01645 
01646       default:
01647         internal_error (__FILE__, __LINE__, _("bad value in switch"));
01648       }
01649 }
01650 
01651 /* Analyze an ARM mode prologue starting at PROLOGUE_START and
01652    continuing no further than PROLOGUE_END.  If CACHE is non-NULL,
01653    fill it in.  Return the first address not recognized as a prologue
01654    instruction.
01655 
01656    We recognize all the instructions typically found in ARM prologues,
01657    plus harmless instructions which can be skipped (either for analysis
01658    purposes, or a more restrictive set that can be skipped when finding
01659    the end of the prologue).  */
01660 
01661 static CORE_ADDR
01662 arm_analyze_prologue (struct gdbarch *gdbarch,
01663                       CORE_ADDR prologue_start, CORE_ADDR prologue_end,
01664                       struct arm_prologue_cache *cache)
01665 {
01666   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
01667   enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
01668   int regno;
01669   CORE_ADDR offset, current_pc;
01670   pv_t regs[ARM_FPS_REGNUM];
01671   struct pv_area *stack;
01672   struct cleanup *back_to;
01673   int framereg, framesize;
01674   CORE_ADDR unrecognized_pc = 0;
01675 
01676   /* Search the prologue looking for instructions that set up the
01677      frame pointer, adjust the stack pointer, and save registers.
01678 
01679      Be careful, however, and if it doesn't look like a prologue,
01680      don't try to scan it.  If, for instance, a frameless function
01681      begins with stmfd sp!, then we will tell ourselves there is
01682      a frame, which will confuse stack traceback, as well as "finish" 
01683      and other operations that rely on a knowledge of the stack
01684      traceback.  */
01685 
01686   for (regno = 0; regno < ARM_FPS_REGNUM; regno++)
01687     regs[regno] = pv_register (regno, 0);
01688   stack = make_pv_area (ARM_SP_REGNUM, gdbarch_addr_bit (gdbarch));
01689   back_to = make_cleanup_free_pv_area (stack);
01690 
01691   for (current_pc = prologue_start;
01692        current_pc < prologue_end;
01693        current_pc += 4)
01694     {
01695       unsigned int insn
01696         = read_memory_unsigned_integer (current_pc, 4, byte_order_for_code);
01697 
01698       if (insn == 0xe1a0c00d)           /* mov ip, sp */
01699         {
01700           regs[ARM_IP_REGNUM] = regs[ARM_SP_REGNUM];
01701           continue;
01702         }
01703       else if ((insn & 0xfff00000) == 0xe2800000        /* add Rd, Rn, #n */
01704                && pv_is_register (regs[bits (insn, 16, 19)], ARM_SP_REGNUM))
01705         {
01706           unsigned imm = insn & 0xff;                   /* immediate value */
01707           unsigned rot = (insn & 0xf00) >> 7;           /* rotate amount */
01708           int rd = bits (insn, 12, 15);
01709           imm = (imm >> rot) | (imm << (32 - rot));
01710           regs[rd] = pv_add_constant (regs[bits (insn, 16, 19)], imm);
01711           continue;
01712         }
01713       else if ((insn & 0xfff00000) == 0xe2400000        /* sub Rd, Rn, #n */
01714                && pv_is_register (regs[bits (insn, 16, 19)], ARM_SP_REGNUM))
01715         {
01716           unsigned imm = insn & 0xff;                   /* immediate value */
01717           unsigned rot = (insn & 0xf00) >> 7;           /* rotate amount */
01718           int rd = bits (insn, 12, 15);
01719           imm = (imm >> rot) | (imm << (32 - rot));
01720           regs[rd] = pv_add_constant (regs[bits (insn, 16, 19)], -imm);
01721           continue;
01722         }
01723       else if ((insn & 0xffff0fff) == 0xe52d0004)       /* str Rd,
01724                                                            [sp, #-4]! */
01725         {
01726           if (pv_area_store_would_trash (stack, regs[ARM_SP_REGNUM]))
01727             break;
01728           regs[ARM_SP_REGNUM] = pv_add_constant (regs[ARM_SP_REGNUM], -4);
01729           pv_area_store (stack, regs[ARM_SP_REGNUM], 4,
01730                          regs[bits (insn, 12, 15)]);
01731           continue;
01732         }
01733       else if ((insn & 0xffff0000) == 0xe92d0000)
01734         /* stmfd sp!, {..., fp, ip, lr, pc}
01735            or
01736            stmfd sp!, {a1, a2, a3, a4}  */
01737         {
01738           int mask = insn & 0xffff;
01739 
01740           if (pv_area_store_would_trash (stack, regs[ARM_SP_REGNUM]))
01741             break;
01742 
01743           /* Calculate offsets of saved registers.  */
01744           for (regno = ARM_PC_REGNUM; regno >= 0; regno--)
01745             if (mask & (1 << regno))
01746               {
01747                 regs[ARM_SP_REGNUM]
01748                   = pv_add_constant (regs[ARM_SP_REGNUM], -4);
01749                 pv_area_store (stack, regs[ARM_SP_REGNUM], 4, regs[regno]);
01750               }
01751         }
01752       else if ((insn & 0xffff0000) == 0xe54b0000        /* strb rx,[r11,#-n] */
01753                || (insn & 0xffff00f0) == 0xe14b00b0     /* strh rx,[r11,#-n] */
01754                || (insn & 0xffffc000) == 0xe50b0000)    /* str  rx,[r11,#-n] */
01755         {
01756           /* No need to add this to saved_regs -- it's just an arg reg.  */
01757           continue;
01758         }
01759       else if ((insn & 0xffff0000) == 0xe5cd0000        /* strb rx,[sp,#n] */
01760                || (insn & 0xffff00f0) == 0xe1cd00b0     /* strh rx,[sp,#n] */
01761                || (insn & 0xffffc000) == 0xe58d0000)    /* str  rx,[sp,#n] */
01762         {
01763           /* No need to add this to saved_regs -- it's just an arg reg.  */
01764           continue;
01765         }
01766       else if ((insn & 0xfff00000) == 0xe8800000        /* stm Rn,
01767                                                            { registers } */
01768                && pv_is_register (regs[bits (insn, 16, 19)], ARM_SP_REGNUM))
01769         {
01770           /* No need to add this to saved_regs -- it's just arg regs.  */
01771           continue;
01772         }
01773       else if ((insn & 0xfffff000) == 0xe24cb000)       /* sub fp, ip #n */
01774         {
01775           unsigned imm = insn & 0xff;                   /* immediate value */
01776           unsigned rot = (insn & 0xf00) >> 7;           /* rotate amount */
01777           imm = (imm >> rot) | (imm << (32 - rot));
01778           regs[ARM_FP_REGNUM] = pv_add_constant (regs[ARM_IP_REGNUM], -imm);
01779         }
01780       else if ((insn & 0xfffff000) == 0xe24dd000)       /* sub sp, sp #n */
01781         {
01782           unsigned imm = insn & 0xff;                   /* immediate value */
01783           unsigned rot = (insn & 0xf00) >> 7;           /* rotate amount */
01784           imm = (imm >> rot) | (imm << (32 - rot));
01785           regs[ARM_SP_REGNUM] = pv_add_constant (regs[ARM_SP_REGNUM], -imm);
01786         }
01787       else if ((insn & 0xffff7fff) == 0xed6d0103        /* stfe f?,
01788                                                            [sp, -#c]! */
01789                && gdbarch_tdep (gdbarch)->have_fpa_registers)
01790         {
01791           if (pv_area_store_would_trash (stack, regs[ARM_SP_REGNUM]))
01792             break;
01793 
01794           regs[ARM_SP_REGNUM] = pv_add_constant (regs[ARM_SP_REGNUM], -12);
01795           regno = ARM_F0_REGNUM + ((insn >> 12) & 0x07);
01796           pv_area_store (stack, regs[ARM_SP_REGNUM], 12, regs[regno]);
01797         }
01798       else if ((insn & 0xffbf0fff) == 0xec2d0200        /* sfmfd f0, 4,
01799                                                            [sp!] */
01800                && gdbarch_tdep (gdbarch)->have_fpa_registers)
01801         {
01802           int n_saved_fp_regs;
01803           unsigned int fp_start_reg, fp_bound_reg;
01804 
01805           if (pv_area_store_would_trash (stack, regs[ARM_SP_REGNUM]))
01806             break;
01807 
01808           if ((insn & 0x800) == 0x800)          /* N0 is set */
01809             {
01810               if ((insn & 0x40000) == 0x40000)  /* N1 is set */
01811                 n_saved_fp_regs = 3;
01812               else
01813                 n_saved_fp_regs = 1;
01814             }
01815           else
01816             {
01817               if ((insn & 0x40000) == 0x40000)  /* N1 is set */
01818                 n_saved_fp_regs = 2;
01819               else
01820                 n_saved_fp_regs = 4;
01821             }
01822 
01823           fp_start_reg = ARM_F0_REGNUM + ((insn >> 12) & 0x7);
01824           fp_bound_reg = fp_start_reg + n_saved_fp_regs;
01825           for (; fp_start_reg < fp_bound_reg; fp_start_reg++)
01826             {
01827               regs[ARM_SP_REGNUM] = pv_add_constant (regs[ARM_SP_REGNUM], -12);
01828               pv_area_store (stack, regs[ARM_SP_REGNUM], 12,
01829                              regs[fp_start_reg++]);
01830             }
01831         }
01832       else if ((insn & 0xff000000) == 0xeb000000 && cache == NULL) /* bl */
01833         {
01834           /* Allow some special function calls when skipping the
01835              prologue; GCC generates these before storing arguments to
01836              the stack.  */
01837           CORE_ADDR dest = BranchDest (current_pc, insn);
01838 
01839           if (skip_prologue_function (gdbarch, dest, 0))
01840             continue;
01841           else
01842             break;
01843         }
01844       else if ((insn & 0xf0000000) != 0xe0000000)
01845         break;                  /* Condition not true, exit early.  */
01846       else if (arm_instruction_changes_pc (insn))
01847         /* Don't scan past anything that might change control flow.  */
01848         break;
01849       else if ((insn & 0xfe500000) == 0xe8100000        /* ldm */
01850                && pv_is_register (regs[bits (insn, 16, 19)], ARM_SP_REGNUM))
01851         /* Ignore block loads from the stack, potentially copying
01852            parameters from memory.  */
01853         continue;
01854       else if ((insn & 0xfc500000) == 0xe4100000
01855                && pv_is_register (regs[bits (insn, 16, 19)], ARM_SP_REGNUM))
01856         /* Similarly ignore single loads from the stack.  */
01857         continue;
01858       else if ((insn & 0xffff0ff0) == 0xe1a00000)
01859         /* MOV Rd, Rm.  Skip register copies, i.e. saves to another
01860            register instead of the stack.  */
01861         continue;
01862       else
01863         {
01864           /* The optimizer might shove anything into the prologue,
01865              so we just skip what we don't recognize.  */
01866           unrecognized_pc = current_pc;
01867           continue;
01868         }
01869     }
01870 
01871   if (unrecognized_pc == 0)
01872     unrecognized_pc = current_pc;
01873 
01874   /* The frame size is just the distance from the frame register
01875      to the original stack pointer.  */
01876   if (pv_is_register (regs[ARM_FP_REGNUM], ARM_SP_REGNUM))
01877     {
01878       /* Frame pointer is fp.  */
01879       framereg = ARM_FP_REGNUM;
01880       framesize = -regs[ARM_FP_REGNUM].k;
01881     }
01882   else
01883     {
01884       /* Try the stack pointer... this is a bit desperate.  */
01885       framereg = ARM_SP_REGNUM;
01886       framesize = -regs[ARM_SP_REGNUM].k;
01887     }
01888 
01889   if (cache)
01890     {
01891       cache->framereg = framereg;
01892       cache->framesize = framesize;
01893 
01894       for (regno = 0; regno < ARM_FPS_REGNUM; regno++)
01895         if (pv_area_find_reg (stack, gdbarch, regno, &offset))
01896           cache->saved_regs[regno].addr = offset;
01897     }
01898 
01899   if (arm_debug)
01900     fprintf_unfiltered (gdb_stdlog, "Prologue scan stopped at %s\n",
01901                         paddress (gdbarch, unrecognized_pc));
01902 
01903   do_cleanups (back_to);
01904   return unrecognized_pc;
01905 }
01906 
01907 static void
01908 arm_scan_prologue (struct frame_info *this_frame,
01909                    struct arm_prologue_cache *cache)
01910 {
01911   struct gdbarch *gdbarch = get_frame_arch (this_frame);
01912   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
01913   int regno;
01914   CORE_ADDR prologue_start, prologue_end, current_pc;
01915   CORE_ADDR prev_pc = get_frame_pc (this_frame);
01916   CORE_ADDR block_addr = get_frame_address_in_block (this_frame);
01917   pv_t regs[ARM_FPS_REGNUM];
01918   struct pv_area *stack;
01919   struct cleanup *back_to;
01920   CORE_ADDR offset;
01921 
01922   /* Assume there is no frame until proven otherwise.  */
01923   cache->framereg = ARM_SP_REGNUM;
01924   cache->framesize = 0;
01925 
01926   /* Check for Thumb prologue.  */
01927   if (arm_frame_is_thumb (this_frame))
01928     {
01929       thumb_scan_prologue (gdbarch, prev_pc, block_addr, cache);
01930       return;
01931     }
01932 
01933   /* Find the function prologue.  If we can't find the function in
01934      the symbol table, peek in the stack frame to find the PC.  */
01935   if (find_pc_partial_function (block_addr, NULL, &prologue_start,
01936                                 &prologue_end))
01937     {
01938       /* One way to find the end of the prologue (which works well
01939          for unoptimized code) is to do the following:
01940 
01941             struct symtab_and_line sal = find_pc_line (prologue_start, 0);
01942 
01943             if (sal.line == 0)
01944               prologue_end = prev_pc;
01945             else if (sal.end < prologue_end)
01946               prologue_end = sal.end;
01947 
01948          This mechanism is very accurate so long as the optimizer
01949          doesn't move any instructions from the function body into the
01950          prologue.  If this happens, sal.end will be the last
01951          instruction in the first hunk of prologue code just before
01952          the first instruction that the scheduler has moved from
01953          the body to the prologue.
01954 
01955          In order to make sure that we scan all of the prologue
01956          instructions, we use a slightly less accurate mechanism which
01957          may scan more than necessary.  To help compensate for this
01958          lack of accuracy, the prologue scanning loop below contains
01959          several clauses which'll cause the loop to terminate early if
01960          an implausible prologue instruction is encountered.
01961 
01962          The expression
01963 
01964               prologue_start + 64
01965 
01966          is a suitable endpoint since it accounts for the largest
01967          possible prologue plus up to five instructions inserted by
01968          the scheduler.  */
01969 
01970       if (prologue_end > prologue_start + 64)
01971         {
01972           prologue_end = prologue_start + 64;   /* See above.  */
01973         }
01974     }
01975   else
01976     {
01977       /* We have no symbol information.  Our only option is to assume this
01978          function has a standard stack frame and the normal frame register.
01979          Then, we can find the value of our frame pointer on entrance to
01980          the callee (or at the present moment if this is the innermost frame).
01981          The value stored there should be the address of the stmfd + 8.  */
01982       CORE_ADDR frame_loc;
01983       LONGEST return_value;
01984 
01985       frame_loc = get_frame_register_unsigned (this_frame, ARM_FP_REGNUM);
01986       if (!safe_read_memory_integer (frame_loc, 4, byte_order, &return_value))
01987         return;
01988       else
01989         {
01990           prologue_start = gdbarch_addr_bits_remove
01991                              (gdbarch, return_value) - 8;
01992           prologue_end = prologue_start + 64;   /* See above.  */
01993         }
01994     }
01995 
01996   if (prev_pc < prologue_end)
01997     prologue_end = prev_pc;
01998 
01999   arm_analyze_prologue (gdbarch, prologue_start, prologue_end, cache);
02000 }
02001 
02002 static struct arm_prologue_cache *
02003 arm_make_prologue_cache (struct frame_info *this_frame)
02004 {
02005   int reg;
02006   struct arm_prologue_cache *cache;
02007   CORE_ADDR unwound_fp;
02008 
02009   cache = FRAME_OBSTACK_ZALLOC (struct arm_prologue_cache);
02010   cache->saved_regs = trad_frame_alloc_saved_regs (this_frame);
02011 
02012   arm_scan_prologue (this_frame, cache);
02013 
02014   unwound_fp = get_frame_register_unsigned (this_frame, cache->framereg);
02015   if (unwound_fp == 0)
02016     return cache;
02017 
02018   cache->prev_sp = unwound_fp + cache->framesize;
02019 
02020   /* Calculate actual addresses of saved registers using offsets
02021      determined by arm_scan_prologue.  */
02022   for (reg = 0; reg < gdbarch_num_regs (get_frame_arch (this_frame)); reg++)
02023     if (trad_frame_addr_p (cache->saved_regs, reg))
02024       cache->saved_regs[reg].addr += cache->prev_sp;
02025 
02026   return cache;
02027 }
02028 
02029 /* Our frame ID for a normal frame is the current function's starting PC
02030    and the caller's SP when we were called.  */
02031 
02032 static void
02033 arm_prologue_this_id (struct frame_info *this_frame,
02034                       void **this_cache,
02035                       struct frame_id *this_id)
02036 {
02037   struct arm_prologue_cache *cache;
02038   struct frame_id id;
02039   CORE_ADDR pc, func;
02040 
02041   if (*this_cache == NULL)
02042     *this_cache = arm_make_prologue_cache (this_frame);
02043   cache = *this_cache;
02044 
02045   /* This is meant to halt the backtrace at "_start".  */
02046   pc = get_frame_pc (this_frame);
02047   if (pc <= gdbarch_tdep (get_frame_arch (this_frame))->lowest_pc)
02048     return;
02049 
02050   /* If we've hit a wall, stop.  */
02051   if (cache->prev_sp == 0)
02052     return;
02053 
02054   /* Use function start address as part of the frame ID.  If we cannot
02055      identify the start address (due to missing symbol information),
02056      fall back to just using the current PC.  */
02057   func = get_frame_func (this_frame);
02058   if (!func)
02059     func = pc;
02060 
02061   id = frame_id_build (cache->prev_sp, func);
02062   *this_id = id;
02063 }
02064 
02065 static struct value *
02066 arm_prologue_prev_register (struct frame_info *this_frame,
02067                             void **this_cache,
02068                             int prev_regnum)
02069 {
02070   struct gdbarch *gdbarch = get_frame_arch (this_frame);
02071   struct arm_prologue_cache *cache;
02072 
02073   if (*this_cache == NULL)
02074     *this_cache = arm_make_prologue_cache (this_frame);
02075   cache = *this_cache;
02076 
02077   /* If we are asked to unwind the PC, then we need to return the LR
02078      instead.  The prologue may save PC, but it will point into this
02079      frame's prologue, not the next frame's resume location.  Also
02080      strip the saved T bit.  A valid LR may have the low bit set, but
02081      a valid PC never does.  */
02082   if (prev_regnum == ARM_PC_REGNUM)
02083     {
02084       CORE_ADDR lr;
02085 
02086       lr = frame_unwind_register_unsigned (this_frame, ARM_LR_REGNUM);
02087       return frame_unwind_got_constant (this_frame, prev_regnum,
02088                                         arm_addr_bits_remove (gdbarch, lr));
02089     }
02090 
02091   /* SP is generally not saved to the stack, but this frame is
02092      identified by the next frame's stack pointer at the time of the call.
02093      The value was already reconstructed into PREV_SP.  */
02094   if (prev_regnum == ARM_SP_REGNUM)
02095     return frame_unwind_got_constant (this_frame, prev_regnum, cache->prev_sp);
02096 
02097   /* The CPSR may have been changed by the call instruction and by the
02098      called function.  The only bit we can reconstruct is the T bit,
02099      by checking the low bit of LR as of the call.  This is a reliable
02100      indicator of Thumb-ness except for some ARM v4T pre-interworking
02101      Thumb code, which could get away with a clear low bit as long as
02102      the called function did not use bx.  Guess that all other
02103      bits are unchanged; the condition flags are presumably lost,
02104      but the processor status is likely valid.  */
02105   if (prev_regnum == ARM_PS_REGNUM)
02106     {
02107       CORE_ADDR lr, cpsr;
02108       ULONGEST t_bit = arm_psr_thumb_bit (gdbarch);
02109 
02110       cpsr = get_frame_register_unsigned (this_frame, prev_regnum);
02111       lr = frame_unwind_register_unsigned (this_frame, ARM_LR_REGNUM);
02112       if (IS_THUMB_ADDR (lr))
02113         cpsr |= t_bit;
02114       else
02115         cpsr &= ~t_bit;
02116       return frame_unwind_got_constant (this_frame, prev_regnum, cpsr);
02117     }
02118 
02119   return trad_frame_get_prev_register (this_frame, cache->saved_regs,
02120                                        prev_regnum);
02121 }
02122 
02123 struct frame_unwind arm_prologue_unwind = {
02124   NORMAL_FRAME,
02125   default_frame_unwind_stop_reason,
02126   arm_prologue_this_id,
02127   arm_prologue_prev_register,
02128   NULL,
02129   default_frame_sniffer
02130 };
02131 
02132 /* Maintain a list of ARM exception table entries per objfile, similar to the
02133    list of mapping symbols.  We only cache entries for standard ARM-defined
02134    personality routines; the cache will contain only the frame unwinding
02135    instructions associated with the entry (not the descriptors).  */
02136 
02137 static const struct objfile_data *arm_exidx_data_key;
02138 
02139 struct arm_exidx_entry
02140 {
02141   bfd_vma addr;
02142   gdb_byte *entry;
02143 };
02144 typedef struct arm_exidx_entry arm_exidx_entry_s;
02145 DEF_VEC_O(arm_exidx_entry_s);
02146 
02147 struct arm_exidx_data
02148 {
02149   VEC(arm_exidx_entry_s) **section_maps;
02150 };
02151 
02152 static void
02153 arm_exidx_data_free (struct objfile *objfile, void *arg)
02154 {
02155   struct arm_exidx_data *data = arg;
02156   unsigned int i;
02157 
02158   for (i = 0; i < objfile->obfd->section_count; i++)
02159     VEC_free (arm_exidx_entry_s, data->section_maps[i]);
02160 }
02161 
02162 static inline int
02163 arm_compare_exidx_entries (const struct arm_exidx_entry *lhs,
02164                            const struct arm_exidx_entry *rhs)
02165 {
02166   return lhs->addr < rhs->addr;
02167 }
02168 
02169 static struct obj_section *
02170 arm_obj_section_from_vma (struct objfile *objfile, bfd_vma vma)
02171 {
02172   struct obj_section *osect;
02173 
02174   ALL_OBJFILE_OSECTIONS (objfile, osect)
02175     if (bfd_get_section_flags (objfile->obfd,
02176                                osect->the_bfd_section) & SEC_ALLOC)
02177       {
02178         bfd_vma start, size;
02179         start = bfd_get_section_vma (objfile->obfd, osect->the_bfd_section);
02180         size = bfd_get_section_size (osect->the_bfd_section);
02181 
02182         if (start <= vma && vma < start + size)
02183           return osect;
02184       }
02185 
02186   return NULL;
02187 }
02188 
02189 /* Parse contents of exception table and exception index sections
02190    of OBJFILE, and fill in the exception table entry cache.
02191 
02192    For each entry that refers to a standard ARM-defined personality
02193    routine, extract the frame unwinding instructions (from either
02194    the index or the table section).  The unwinding instructions
02195    are normalized by:
02196     - extracting them from the rest of the table data
02197     - converting to host endianness
02198     - appending the implicit 0xb0 ("Finish") code
02199 
02200    The extracted and normalized instructions are stored for later
02201    retrieval by the arm_find_exidx_entry routine.  */
02202  
02203 static void
02204 arm_exidx_new_objfile (struct objfile *objfile)
02205 {
02206   struct cleanup *cleanups;
02207   struct arm_exidx_data *data;
02208   asection *exidx, *extab;
02209   bfd_vma exidx_vma = 0, extab_vma = 0;
02210   bfd_size_type exidx_size = 0, extab_size = 0;
02211   gdb_byte *exidx_data = NULL, *extab_data = NULL;
02212   LONGEST i;
02213 
02214   /* If we've already touched this file, do nothing.  */
02215   if (!objfile || objfile_data (objfile, arm_exidx_data_key) != NULL)
02216     return;
02217   cleanups = make_cleanup (null_cleanup, NULL);
02218 
02219   /* Read contents of exception table and index.  */
02220   exidx = bfd_get_section_by_name (objfile->obfd, ".ARM.exidx");
02221   if (exidx)
02222     {
02223       exidx_vma = bfd_section_vma (objfile->obfd, exidx);
02224       exidx_size = bfd_get_section_size (exidx);
02225       exidx_data = xmalloc (exidx_size);
02226       make_cleanup (xfree, exidx_data);
02227 
02228       if (!bfd_get_section_contents (objfile->obfd, exidx,
02229                                      exidx_data, 0, exidx_size))
02230         {
02231           do_cleanups (cleanups);
02232           return;
02233         }
02234     }
02235 
02236   extab = bfd_get_section_by_name (objfile->obfd, ".ARM.extab");
02237   if (extab)
02238     {
02239       extab_vma = bfd_section_vma (objfile->obfd, extab);
02240       extab_size = bfd_get_section_size (extab);
02241       extab_data = xmalloc (extab_size);
02242       make_cleanup (xfree, extab_data);
02243 
02244       if (!bfd_get_section_contents (objfile->obfd, extab,
02245                                      extab_data, 0, extab_size))
02246         {
02247           do_cleanups (cleanups);
02248           return;
02249         }
02250     }
02251 
02252   /* Allocate exception table data structure.  */
02253   data = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct arm_exidx_data);
02254   set_objfile_data (objfile, arm_exidx_data_key, data);
02255   data->section_maps = OBSTACK_CALLOC (&objfile->objfile_obstack,
02256                                        objfile->obfd->section_count,
02257                                        VEC(arm_exidx_entry_s) *);
02258 
02259   /* Fill in exception table.  */
02260   for (i = 0; i < exidx_size / 8; i++)
02261     {
02262       struct arm_exidx_entry new_exidx_entry;
02263       bfd_vma idx = bfd_h_get_32 (objfile->obfd, exidx_data + i * 8);
02264       bfd_vma val = bfd_h_get_32 (objfile->obfd, exidx_data + i * 8 + 4);
02265       bfd_vma addr = 0, word = 0;
02266       int n_bytes = 0, n_words = 0;
02267       struct obj_section *sec;
02268       gdb_byte *entry = NULL;
02269 
02270       /* Extract address of start of function.  */
02271       idx = ((idx & 0x7fffffff) ^ 0x40000000) - 0x40000000;
02272       idx += exidx_vma + i * 8;
02273 
02274       /* Find section containing function and compute section offset.  */
02275       sec = arm_obj_section_from_vma (objfile, idx);
02276       if (sec == NULL)
02277         continue;
02278       idx -= bfd_get_section_vma (objfile->obfd, sec->the_bfd_section);
02279 
02280       /* Determine address of exception table entry.  */
02281       if (val == 1)
02282         {
02283           /* EXIDX_CANTUNWIND -- no exception table entry present.  */
02284         }
02285       else if ((val & 0xff000000) == 0x80000000)
02286         {
02287           /* Exception table entry embedded in .ARM.exidx
02288              -- must be short form.  */
02289           word = val;
02290           n_bytes = 3;
02291         }
02292       else if (!(val & 0x80000000))
02293         {
02294           /* Exception table entry in .ARM.extab.  */
02295           addr = ((val & 0x7fffffff) ^ 0x40000000) - 0x40000000;
02296           addr += exidx_vma + i * 8 + 4;
02297 
02298           if (addr >= extab_vma && addr + 4 <= extab_vma + extab_size)
02299             {
02300               word = bfd_h_get_32 (objfile->obfd,
02301                                    extab_data + addr - extab_vma);
02302               addr += 4;
02303 
02304               if ((word & 0xff000000) == 0x80000000)
02305                 {
02306                   /* Short form.  */
02307                   n_bytes = 3;
02308                 }
02309               else if ((word & 0xff000000) == 0x81000000
02310                        || (word & 0xff000000) == 0x82000000)
02311                 {
02312                   /* Long form.  */
02313                   n_bytes = 2;
02314                   n_words = ((word >> 16) & 0xff);
02315                 }
02316               else if (!(word & 0x80000000))
02317                 {
02318                   bfd_vma pers;
02319                   struct obj_section *pers_sec;
02320                   int gnu_personality = 0;
02321 
02322                   /* Custom personality routine.  */
02323                   pers = ((word & 0x7fffffff) ^ 0x40000000) - 0x40000000;
02324                   pers = UNMAKE_THUMB_ADDR (pers + addr - 4);
02325 
02326                   /* Check whether we've got one of the variants of the
02327                      GNU personality routines.  */
02328                   pers_sec = arm_obj_section_from_vma (objfile, pers);
02329                   if (pers_sec)
02330                     {
02331                       static const char *personality[] = 
02332                         {
02333                           "__gcc_personality_v0",
02334                           "__gxx_personality_v0",
02335                           "__gcj_personality_v0",
02336                           "__gnu_objc_personality_v0",
02337                           NULL
02338                         };
02339 
02340                       CORE_ADDR pc = pers + obj_section_offset (pers_sec);
02341                       int k;
02342 
02343                       for (k = 0; personality[k]; k++)
02344                         if (lookup_minimal_symbol_by_pc_name
02345                               (pc, personality[k], objfile))
02346                           {
02347                             gnu_personality = 1;
02348                             break;
02349                           }
02350                     }
02351 
02352                   /* If so, the next word contains a word count in the high
02353                      byte, followed by the same unwind instructions as the
02354                      pre-defined forms.  */
02355                   if (gnu_personality
02356                       && addr + 4 <= extab_vma + extab_size)
02357                     {
02358                       word = bfd_h_get_32 (objfile->obfd,
02359                                            extab_data + addr - extab_vma);
02360                       addr += 4;
02361                       n_bytes = 3;
02362                       n_words = ((word >> 24) & 0xff);
02363                     }
02364                 }
02365             }
02366         }
02367 
02368       /* Sanity check address.  */
02369       if (n_words)
02370         if (addr < extab_vma || addr + 4 * n_words > extab_vma + extab_size)
02371           n_words = n_bytes = 0;
02372 
02373       /* The unwind instructions reside in WORD (only the N_BYTES least
02374          significant bytes are valid), followed by N_WORDS words in the
02375          extab section starting at ADDR.  */
02376       if (n_bytes || n_words)
02377         {
02378           gdb_byte *p = entry = obstack_alloc (&objfile->objfile_obstack,
02379                                                n_bytes + n_words * 4 + 1);
02380 
02381           while (n_bytes--)
02382             *p++ = (gdb_byte) ((word >> (8 * n_bytes)) & 0xff);
02383 
02384           while (n_words--)
02385             {
02386               word = bfd_h_get_32 (objfile->obfd,
02387                                    extab_data + addr - extab_vma);
02388               addr += 4;
02389 
02390               *p++ = (gdb_byte) ((word >> 24) & 0xff);
02391               *p++ = (gdb_byte) ((word >> 16) & 0xff);
02392               *p++ = (gdb_byte) ((word >> 8) & 0xff);
02393               *p++ = (gdb_byte) (word & 0xff);
02394             }
02395 
02396           /* Implied "Finish" to terminate the list.  */
02397           *p++ = 0xb0;
02398         }
02399 
02400       /* Push entry onto vector.  They are guaranteed to always
02401          appear in order of increasing addresses.  */
02402       new_exidx_entry.addr = idx;
02403       new_exidx_entry.entry = entry;
02404       VEC_safe_push (arm_exidx_entry_s,
02405                      data->section_maps[sec->the_bfd_section->index],
02406                      &new_exidx_entry);
02407     }
02408 
02409   do_cleanups (cleanups);
02410 }
02411 
02412 /* Search for the exception table entry covering MEMADDR.  If one is found,
02413    return a pointer to its data.  Otherwise, return 0.  If START is non-NULL,
02414    set *START to the start of the region covered by this entry.  */
02415 
02416 static gdb_byte *
02417 arm_find_exidx_entry (CORE_ADDR memaddr, CORE_ADDR *start)
02418 {
02419   struct obj_section *sec;
02420 
02421   sec = find_pc_section (memaddr);
02422   if (sec != NULL)
02423     {
02424       struct arm_exidx_data *data;
02425       VEC(arm_exidx_entry_s) *map;
02426       struct arm_exidx_entry map_key = { memaddr - obj_section_addr (sec), 0 };
02427       unsigned int idx;
02428 
02429       data = objfile_data (sec->objfile, arm_exidx_data_key);
02430       if (data != NULL)
02431         {
02432           map = data->section_maps[sec->the_bfd_section->index];
02433           if (!VEC_empty (arm_exidx_entry_s, map))
02434             {
02435               struct arm_exidx_entry *map_sym;
02436 
02437               idx = VEC_lower_bound (arm_exidx_entry_s, map, &map_key,
02438                                      arm_compare_exidx_entries);
02439 
02440               /* VEC_lower_bound finds the earliest ordered insertion
02441                  point.  If the following symbol starts at this exact
02442                  address, we use that; otherwise, the preceding
02443                  exception table entry covers this address.  */
02444               if (idx < VEC_length (arm_exidx_entry_s, map))
02445                 {
02446                   map_sym = VEC_index (arm_exidx_entry_s, map, idx);
02447                   if (map_sym->addr == map_key.addr)
02448                     {
02449                       if (start)
02450                         *start = map_sym->addr + obj_section_addr (sec);
02451                       return map_sym->entry;
02452                     }
02453                 }
02454 
02455               if (idx > 0)
02456                 {
02457                   map_sym = VEC_index (arm_exidx_entry_s, map, idx - 1);
02458                   if (start)
02459                     *start = map_sym->addr + obj_section_addr (sec);
02460                   return map_sym->entry;
02461                 }
02462             }
02463         }
02464     }
02465 
02466   return NULL;
02467 }
02468 
02469 /* Given the current frame THIS_FRAME, and its associated frame unwinding
02470    instruction list from the ARM exception table entry ENTRY, allocate and
02471    return a prologue cache structure describing how to unwind this frame.
02472 
02473    Return NULL if the unwinding instruction list contains a "spare",
02474    "reserved" or "refuse to unwind" instruction as defined in section
02475    "9.3 Frame unwinding instructions" of the "Exception Handling ABI
02476    for the ARM Architecture" document.  */
02477 
02478 static struct arm_prologue_cache *
02479 arm_exidx_fill_cache (struct frame_info *this_frame, gdb_byte *entry)
02480 {
02481   CORE_ADDR vsp = 0;
02482   int vsp_valid = 0;
02483 
02484   struct arm_prologue_cache *cache;
02485   cache = FRAME_OBSTACK_ZALLOC (struct arm_prologue_cache);
02486   cache->saved_regs = trad_frame_alloc_saved_regs (this_frame);
02487 
02488   for (;;)
02489     {
02490       gdb_byte insn;
02491 
02492       /* Whenever we reload SP, we actually have to retrieve its
02493          actual value in the current frame.  */
02494       if (!vsp_valid)
02495         {
02496           if (trad_frame_realreg_p (cache->saved_regs, ARM_SP_REGNUM))
02497             {
02498               int reg = cache->saved_regs[ARM_SP_REGNUM].realreg;
02499               vsp = get_frame_register_unsigned (this_frame, reg);
02500             }
02501           else
02502             {
02503               CORE_ADDR addr = cache->saved_regs[ARM_SP_REGNUM].addr;
02504               vsp = get_frame_memory_unsigned (this_frame, addr, 4);
02505             }
02506 
02507           vsp_valid = 1;
02508         }
02509 
02510       /* Decode next unwind instruction.  */
02511       insn = *entry++;
02512 
02513       if ((insn & 0xc0) == 0)
02514         {
02515           int offset = insn & 0x3f;
02516           vsp += (offset << 2) + 4;
02517         }
02518       else if ((insn & 0xc0) == 0x40)
02519         {
02520           int offset = insn & 0x3f;
02521           vsp -= (offset << 2) + 4;
02522         }
02523       else if ((insn & 0xf0) == 0x80)
02524         {
02525           int mask = ((insn & 0xf) << 8) | *entry++;
02526           int i;
02527 
02528           /* The special case of an all-zero mask identifies
02529              "Refuse to unwind".  We return NULL to fall back
02530              to the prologue analyzer.  */
02531           if (mask == 0)
02532             return NULL;
02533 
02534           /* Pop registers r4..r15 under mask.  */
02535           for (i = 0; i < 12; i++)
02536             if (mask & (1 << i))
02537               {
02538                 cache->saved_regs[4 + i].addr = vsp;
02539                 vsp += 4;
02540               }
02541 
02542           /* Special-case popping SP -- we need to reload vsp.  */
02543           if (mask & (1 << (ARM_SP_REGNUM - 4)))
02544             vsp_valid = 0;
02545         }
02546       else if ((insn & 0xf0) == 0x90)
02547         {
02548           int reg = insn & 0xf;
02549 
02550           /* Reserved cases.  */
02551           if (reg == ARM_SP_REGNUM || reg == ARM_PC_REGNUM)
02552             return NULL;
02553 
02554           /* Set SP from another register and mark VSP for reload.  */
02555           cache->saved_regs[ARM_SP_REGNUM] = cache->saved_regs[reg];
02556           vsp_valid = 0;
02557         }
02558       else if ((insn & 0xf0) == 0xa0)
02559         {
02560           int count = insn & 0x7;
02561           int pop_lr = (insn & 0x8) != 0;
02562           int i;
02563 
02564           /* Pop r4..r[4+count].  */
02565           for (i = 0; i <= count; i++)
02566             {
02567               cache->saved_regs[4 + i].addr = vsp;
02568               vsp += 4;
02569             }
02570 
02571           /* If indicated by flag, pop LR as well.  */
02572           if (pop_lr)
02573             {
02574               cache->saved_regs[ARM_LR_REGNUM].addr = vsp;
02575               vsp += 4;
02576             }
02577         }
02578       else if (insn == 0xb0)
02579         {
02580           /* We could only have updated PC by popping into it; if so, it
02581              will show up as address.  Otherwise, copy LR into PC.  */
02582           if (!trad_frame_addr_p (cache->saved_regs, ARM_PC_REGNUM))
02583             cache->saved_regs[ARM_PC_REGNUM]
02584               = cache->saved_regs[ARM_LR_REGNUM];
02585 
02586           /* We're done.  */
02587           break;
02588         }
02589       else if (insn == 0xb1)
02590         {
02591           int mask = *entry++;
02592           int i;
02593 
02594           /* All-zero mask and mask >= 16 is "spare".  */
02595           if (mask == 0 || mask >= 16)
02596             return NULL;
02597 
02598           /* Pop r0..r3 under mask.  */
02599           for (i = 0; i < 4; i++)
02600             if (mask & (1 << i))
02601               {
02602                 cache->saved_regs[i].addr = vsp;
02603                 vsp += 4;
02604               }
02605         }
02606       else if (insn == 0xb2)
02607         {
02608           ULONGEST offset = 0;
02609           unsigned shift = 0;
02610 
02611           do
02612             {
02613               offset |= (*entry & 0x7f) << shift;
02614               shift += 7;
02615             }
02616           while (*entry++ & 0x80);
02617 
02618           vsp += 0x204 + (offset << 2);
02619         }
02620       else if (insn == 0xb3)
02621         {
02622           int start = *entry >> 4;
02623           int count = (*entry++) & 0xf;
02624           int i;
02625 
02626           /* Only registers D0..D15 are valid here.  */
02627           if (start + count >= 16)
02628             return NULL;
02629 
02630           /* Pop VFP double-precision registers D[start]..D[start+count].  */
02631           for (i = 0; i <= count; i++)
02632             {
02633               cache->saved_regs[ARM_D0_REGNUM + start + i].addr = vsp;
02634               vsp += 8;
02635             }
02636 
02637           /* Add an extra 4 bytes for FSTMFDX-style stack.  */
02638           vsp += 4;
02639         }
02640       else if ((insn & 0xf8) == 0xb8)
02641         {
02642           int count = insn & 0x7;
02643           int i;
02644 
02645           /* Pop VFP double-precision registers D[8]..D[8+count].  */
02646           for (i = 0; i <= count; i++)
02647             {
02648               cache->saved_regs[ARM_D0_REGNUM + 8 + i].addr = vsp;
02649               vsp += 8;
02650             }
02651 
02652           /* Add an extra 4 bytes for FSTMFDX-style stack.  */
02653           vsp += 4;
02654         }
02655       else if (insn == 0xc6)
02656         {
02657           int start = *entry >> 4;
02658           int count = (*entry++) & 0xf;
02659           int i;
02660 
02661           /* Only registers WR0..WR15 are valid.  */
02662           if (start + count >= 16)
02663             return NULL;
02664 
02665           /* Pop iwmmx registers WR[start]..WR[start+count].  */
02666           for (i = 0; i <= count; i++)
02667             {
02668               cache->saved_regs[ARM_WR0_REGNUM + start + i].addr = vsp;
02669               vsp += 8;
02670             }
02671         }
02672       else if (insn == 0xc7)
02673         {
02674           int mask = *entry++;
02675           int i;
02676 
02677           /* All-zero mask and mask >= 16 is "spare".  */
02678           if (mask == 0 || mask >= 16)
02679             return NULL;
02680 
02681           /* Pop iwmmx general-purpose registers WCGR0..WCGR3 under mask.  */
02682           for (i = 0; i < 4; i++)
02683             if (mask & (1 << i))
02684               {
02685                 cache->saved_regs[ARM_WCGR0_REGNUM + i].addr = vsp;
02686                 vsp += 4;
02687               }
02688         }
02689       else if ((insn & 0xf8) == 0xc0)
02690         {
02691           int count = insn & 0x7;
02692           int i;
02693 
02694           /* Pop iwmmx registers WR[10]..WR[10+count].  */
02695           for (i = 0; i <= count; i++)
02696             {
02697               cache->saved_regs[ARM_WR0_REGNUM + 10 + i].addr = vsp;
02698               vsp += 8;
02699             }
02700         }
02701       else if (insn == 0xc8)
02702         {
02703           int start = *entry >> 4;
02704           int count = (*entry++) & 0xf;
02705           int i;
02706 
02707           /* Only registers D0..D31 are valid.  */
02708           if (start + count >= 16)
02709             return NULL;
02710 
02711           /* Pop VFP double-precision registers
02712              D[16+start]..D[16+start+count].  */
02713           for (i = 0; i <= count; i++)
02714             {
02715               cache->saved_regs[ARM_D0_REGNUM + 16 + start + i].addr = vsp;
02716               vsp += 8;
02717             }
02718         }
02719       else if (insn == 0xc9)
02720         {
02721           int start = *entry >> 4;
02722           int count = (*entry++) & 0xf;
02723           int i;
02724 
02725           /* Pop VFP double-precision registers D[start]..D[start+count].  */
02726           for (i = 0; i <= count; i++)
02727             {
02728               cache->saved_regs[ARM_D0_REGNUM + start + i].addr = vsp;
02729               vsp += 8;
02730             }
02731         }
02732       else if ((insn & 0xf8) == 0xd0)
02733         {
02734           int count = insn & 0x7;
02735           int i;
02736 
02737           /* Pop VFP double-precision registers D[8]..D[8+count].  */
02738           for (i = 0; i <= count; i++)
02739             {
02740               cache->saved_regs[ARM_D0_REGNUM + 8 + i].addr = vsp;
02741               vsp += 8;
02742             }
02743         }
02744       else
02745         {
02746           /* Everything else is "spare".  */
02747           return NULL;
02748         }
02749     }
02750 
02751   /* If we restore SP from a register, assume this was the frame register.
02752      Otherwise just fall back to SP as frame register.  */
02753   if (trad_frame_realreg_p (cache->saved_regs, ARM_SP_REGNUM))
02754     cache->framereg = cache->saved_regs[ARM_SP_REGNUM].realreg;
02755   else
02756     cache->framereg = ARM_SP_REGNUM;
02757 
02758   /* Determine offset to previous frame.  */
02759   cache->framesize
02760     = vsp - get_frame_register_unsigned (this_frame, cache->framereg);
02761 
02762   /* We already got the previous SP.  */
02763   cache->prev_sp = vsp;
02764 
02765   return cache;
02766 }
02767 
02768 /* Unwinding via ARM exception table entries.  Note that the sniffer
02769    already computes a filled-in prologue cache, which is then used
02770    with the same arm_prologue_this_id and arm_prologue_prev_register
02771    routines also used for prologue-parsing based unwinding.  */
02772 
02773 static int
02774 arm_exidx_unwind_sniffer (const struct frame_unwind *self,
02775                           struct frame_info *this_frame,
02776                           void **this_prologue_cache)
02777 {
02778   struct gdbarch *gdbarch = get_frame_arch (this_frame);
02779   enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
02780   CORE_ADDR addr_in_block, exidx_region, func_start;
02781   struct arm_prologue_cache *cache;
02782   gdb_byte *entry;
02783 
02784   /* See if we have an ARM exception table entry covering this address.  */
02785   addr_in_block = get_frame_address_in_block (this_frame);
02786   entry = arm_find_exidx_entry (addr_in_block, &exidx_region);
02787   if (!entry)
02788     return 0;
02789 
02790   /* The ARM exception table does not describe unwind information
02791      for arbitrary PC values, but is guaranteed to be correct only
02792      at call sites.  We have to decide here whether we want to use
02793      ARM exception table information for this frame, or fall back
02794      to using prologue parsing.  (Note that if we have DWARF CFI,
02795      this sniffer isn't even called -- CFI is always preferred.)
02796 
02797      Before we make this decision, however, we check whether we
02798      actually have *symbol* information for the current frame.
02799      If not, prologue parsing would not work anyway, so we might
02800      as well use the exception table and hope for the best.  */
02801   if (find_pc_partial_function (addr_in_block, NULL, &func_start, NULL))
02802     {
02803       int exc_valid = 0;
02804 
02805       /* If the next frame is "normal", we are at a call site in this
02806          frame, so exception information is guaranteed to be valid.  */
02807       if (get_next_frame (this_frame)
02808           && get_frame_type (get_next_frame (this_frame)) == NORMAL_FRAME)
02809         exc_valid = 1;
02810 
02811       /* We also assume exception information is valid if we're currently
02812          blocked in a system call.  The system library is supposed to
02813          ensure this, so that e.g. pthread cancellation works.  */
02814       if (arm_frame_is_thumb (this_frame))
02815         {
02816           LONGEST insn;
02817 
02818           if (safe_read_memory_integer (get_frame_pc (this_frame) - 2, 2,
02819                                         byte_order_for_code, &insn)
02820               && (insn & 0xff00) == 0xdf00 /* svc */)
02821             exc_valid = 1;
02822         }
02823       else
02824         {
02825           LONGEST insn;
02826 
02827           if (safe_read_memory_integer (get_frame_pc (this_frame) - 4, 4,
02828                                         byte_order_for_code, &insn)
02829               && (insn & 0x0f000000) == 0x0f000000 /* svc */)
02830             exc_valid = 1;
02831         }
02832         
02833       /* Bail out if we don't know that exception information is valid.  */
02834       if (!exc_valid)
02835         return 0;
02836 
02837      /* The ARM exception index does not mark the *end* of the region
02838         covered by the entry, and some functions will not have any entry.
02839         To correctly recognize the end of the covered region, the linker
02840         should have inserted dummy records with a CANTUNWIND marker.
02841 
02842         Unfortunately, current versions of GNU ld do not reliably do
02843         this, and thus we may have found an incorrect entry above.
02844         As a (temporary) sanity check, we only use the entry if it
02845         lies *within* the bounds of the function.  Note that this check
02846         might reject perfectly valid entries that just happen to cover
02847         multiple functions; therefore this check ought to be removed
02848         once the linker is fixed.  */
02849       if (func_start > exidx_region)
02850         return 0;
02851     }
02852 
02853   /* Decode the list of unwinding instructions into a prologue cache.
02854      Note that this may fail due to e.g. a "refuse to unwind" code.  */
02855   cache = arm_exidx_fill_cache (this_frame, entry);
02856   if (!cache)
02857     return 0;
02858 
02859   *this_prologue_cache = cache;
02860   return 1;
02861 }
02862 
02863 struct frame_unwind arm_exidx_unwind = {
02864   NORMAL_FRAME,
02865   default_frame_unwind_stop_reason,
02866   arm_prologue_this_id,
02867   arm_prologue_prev_register,
02868   NULL,
02869   arm_exidx_unwind_sniffer
02870 };
02871 
02872 static struct arm_prologue_cache *
02873 arm_make_stub_cache (struct frame_info *this_frame)
02874 {
02875   struct arm_prologue_cache *cache;
02876 
02877   cache = FRAME_OBSTACK_ZALLOC (struct arm_prologue_cache);
02878   cache->saved_regs = trad_frame_alloc_saved_regs (this_frame);
02879 
02880   cache->prev_sp = get_frame_register_unsigned (this_frame, ARM_SP_REGNUM);
02881 
02882   return cache;
02883 }
02884 
02885 /* Our frame ID for a stub frame is the current SP and LR.  */
02886 
02887 static void
02888 arm_stub_this_id (struct frame_info *this_frame,
02889                   void **this_cache,
02890                   struct frame_id *this_id)
02891 {
02892   struct arm_prologue_cache *cache;
02893 
02894   if (*this_cache == NULL)
02895     *this_cache = arm_make_stub_cache (this_frame);
02896   cache = *this_cache;
02897 
02898   *this_id = frame_id_build (cache->prev_sp, get_frame_pc (this_frame));
02899 }
02900 
02901 static int
02902 arm_stub_unwind_sniffer (const struct frame_unwind *self,
02903                          struct frame_info *this_frame,
02904                          void **this_prologue_cache)
02905 {
02906   CORE_ADDR addr_in_block;
02907   gdb_byte dummy[4];
02908 
02909   addr_in_block = get_frame_address_in_block (this_frame);
02910   if (in_plt_section (addr_in_block)
02911       /* We also use the stub winder if the target memory is unreadable
02912          to avoid having the prologue unwinder trying to read it.  */
02913       || target_read_memory (get_frame_pc (this_frame), dummy, 4) != 0)
02914     return 1;
02915 
02916   return 0;
02917 }
02918 
02919 struct frame_unwind arm_stub_unwind = {
02920   NORMAL_FRAME,
02921   default_frame_unwind_stop_reason,
02922   arm_stub_this_id,
02923   arm_prologue_prev_register,
02924   NULL,
02925   arm_stub_unwind_sniffer
02926 };
02927 
02928 /* Put here the code to store, into CACHE->saved_regs, the addresses
02929    of the saved registers of frame described by THIS_FRAME.  CACHE is
02930    returned.  */
02931 
02932 static struct arm_prologue_cache *
02933 arm_m_exception_cache (struct frame_info *this_frame)
02934 {
02935   struct gdbarch *gdbarch = get_frame_arch (this_frame);
02936   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
02937   struct arm_prologue_cache *cache;
02938   CORE_ADDR unwound_sp;
02939   LONGEST xpsr;
02940 
02941   cache = FRAME_OBSTACK_ZALLOC (struct arm_prologue_cache);
02942   cache->saved_regs = trad_frame_alloc_saved_regs (this_frame);
02943 
02944   unwound_sp = get_frame_register_unsigned (this_frame,
02945                                             ARM_SP_REGNUM);
02946 
02947   /* The hardware saves eight 32-bit words, comprising xPSR,
02948      ReturnAddress, LR (R14), R12, R3, R2, R1, R0.  See details in
02949      "B1.5.6 Exception entry behavior" in
02950      "ARMv7-M Architecture Reference Manual".  */
02951   cache->saved_regs[0].addr = unwound_sp;
02952   cache->saved_regs[1].addr = unwound_sp + 4;
02953   cache->saved_regs[2].addr = unwound_sp + 8;
02954   cache->saved_regs[3].addr = unwound_sp + 12;
02955   cache->saved_regs[12].addr = unwound_sp + 16;
02956   cache->saved_regs[14].addr = unwound_sp + 20;
02957   cache->saved_regs[15].addr = unwound_sp + 24;
02958   cache->saved_regs[ARM_PS_REGNUM].addr = unwound_sp + 28;
02959 
02960   /* If bit 9 of the saved xPSR is set, then there is a four-byte
02961      aligner between the top of the 32-byte stack frame and the
02962      previous context's stack pointer.  */
02963   cache->prev_sp = unwound_sp + 32;
02964   if (safe_read_memory_integer (unwound_sp + 28, 4, byte_order, &xpsr)
02965       && (xpsr & (1 << 9)) != 0)
02966     cache->prev_sp += 4;
02967 
02968   return cache;
02969 }
02970 
02971 /* Implementation of function hook 'this_id' in
02972    'struct frame_uwnind'.  */
02973 
02974 static void
02975 arm_m_exception_this_id (struct frame_info *this_frame,
02976                          void **this_cache,
02977                          struct frame_id *this_id)
02978 {
02979   struct arm_prologue_cache *cache;
02980 
02981   if (*this_cache == NULL)
02982     *this_cache = arm_m_exception_cache (this_frame);
02983   cache = *this_cache;
02984 
02985   /* Our frame ID for a stub frame is the current SP and LR.  */
02986   *this_id = frame_id_build (cache->prev_sp,
02987                              get_frame_pc (this_frame));
02988 }
02989 
02990 /* Implementation of function hook 'prev_register' in
02991    'struct frame_uwnind'.  */
02992 
02993 static struct value *
02994 arm_m_exception_prev_register (struct frame_info *this_frame,
02995                                void **this_cache,
02996                                int prev_regnum)
02997 {
02998   struct gdbarch *gdbarch = get_frame_arch (this_frame);
02999   struct arm_prologue_cache *cache;
03000 
03001   if (*this_cache == NULL)
03002     *this_cache = arm_m_exception_cache (this_frame);
03003   cache = *this_cache;
03004 
03005   /* The value was already reconstructed into PREV_SP.  */
03006   if (prev_regnum == ARM_SP_REGNUM)
03007     return frame_unwind_got_constant (this_frame, prev_regnum,
03008                                       cache->prev_sp);
03009 
03010   return trad_frame_get_prev_register (this_frame, cache->saved_regs,
03011                                        prev_regnum);
03012 }
03013 
03014 /* Implementation of function hook 'sniffer' in
03015    'struct frame_uwnind'.  */
03016 
03017 static int
03018 arm_m_exception_unwind_sniffer (const struct frame_unwind *self,
03019                                 struct frame_info *this_frame,
03020                                 void **this_prologue_cache)
03021 {
03022   CORE_ADDR this_pc = get_frame_pc (this_frame);
03023 
03024   /* No need to check is_m; this sniffer is only registered for
03025      M-profile architectures.  */
03026 
03027   /* Exception frames return to one of these magic PCs.  Other values
03028      are not defined as of v7-M.  See details in "B1.5.8 Exception
03029      return behavior" in "ARMv7-M Architecture Reference Manual".  */
03030   if (this_pc == 0xfffffff1 || this_pc == 0xfffffff9
03031       || this_pc == 0xfffffffd)
03032     return 1;
03033 
03034   return 0;
03035 }
03036 
03037 /* Frame unwinder for M-profile exceptions.  */
03038 
03039 struct frame_unwind arm_m_exception_unwind =
03040 {
03041   SIGTRAMP_FRAME,
03042   default_frame_unwind_stop_reason,
03043   arm_m_exception_this_id,
03044   arm_m_exception_prev_register,
03045   NULL,
03046   arm_m_exception_unwind_sniffer
03047 };
03048 
03049 static CORE_ADDR
03050 arm_normal_frame_base (struct frame_info *this_frame, void **this_cache)
03051 {
03052   struct arm_prologue_cache *cache;
03053 
03054   if (*this_cache == NULL)
03055     *this_cache = arm_make_prologue_cache (this_frame);
03056   cache = *this_cache;
03057 
03058   return cache->prev_sp - cache->framesize;
03059 }
03060 
03061 struct frame_base arm_normal_base = {
03062   &arm_prologue_unwind,
03063   arm_normal_frame_base,
03064   arm_normal_frame_base,
03065   arm_normal_frame_base
03066 };
03067 
03068 /* Assuming THIS_FRAME is a dummy, return the frame ID of that
03069    dummy frame.  The frame ID's base needs to match the TOS value
03070    saved by save_dummy_frame_tos() and returned from
03071    arm_push_dummy_call, and the PC needs to match the dummy frame's
03072    breakpoint.  */
03073 
03074 static struct frame_id
03075 arm_dummy_id (struct gdbarch *gdbarch, struct frame_info *this_frame)
03076 {
03077   return frame_id_build (get_frame_register_unsigned (this_frame,
03078                                                       ARM_SP_REGNUM),
03079                          get_frame_pc (this_frame));
03080 }
03081 
03082 /* Given THIS_FRAME, find the previous frame's resume PC (which will
03083    be used to construct the previous frame's ID, after looking up the
03084    containing function).  */
03085 
03086 static CORE_ADDR
03087 arm_unwind_pc (struct gdbarch *gdbarch, struct frame_info *this_frame)
03088 {
03089   CORE_ADDR pc;
03090   pc = frame_unwind_register_unsigned (this_frame, ARM_PC_REGNUM);
03091   return arm_addr_bits_remove (gdbarch, pc);
03092 }
03093 
03094 static CORE_ADDR
03095 arm_unwind_sp (struct gdbarch *gdbarch, struct frame_info *this_frame)
03096 {
03097   return frame_unwind_register_unsigned (this_frame, ARM_SP_REGNUM);
03098 }
03099 
03100 static struct value *
03101 arm_dwarf2_prev_register (struct frame_info *this_frame, void **this_cache,
03102                           int regnum)
03103 {
03104   struct gdbarch * gdbarch = get_frame_arch (this_frame);
03105   CORE_ADDR lr, cpsr;
03106   ULONGEST t_bit = arm_psr_thumb_bit (gdbarch);
03107 
03108   switch (regnum)
03109     {
03110     case ARM_PC_REGNUM:
03111       /* The PC is normally copied from the return column, which
03112          describes saves of LR.  However, that version may have an
03113          extra bit set to indicate Thumb state.  The bit is not
03114          part of the PC.  */
03115       lr = frame_unwind_register_unsigned (this_frame, ARM_LR_REGNUM);
03116       return frame_unwind_got_constant (this_frame, regnum,
03117                                         arm_addr_bits_remove (gdbarch, lr));
03118 
03119     case ARM_PS_REGNUM:
03120       /* Reconstruct the T bit; see arm_prologue_prev_register for details.  */
03121       cpsr = get_frame_register_unsigned (this_frame, regnum);
03122       lr = frame_unwind_register_unsigned (this_frame, ARM_LR_REGNUM);
03123       if (IS_THUMB_ADDR (lr))
03124         cpsr |= t_bit;
03125       else
03126         cpsr &= ~t_bit;
03127       return frame_unwind_got_constant (this_frame, regnum, cpsr);
03128 
03129     default:
03130       internal_error (__FILE__, __LINE__,
03131                       _("Unexpected register %d"), regnum);
03132     }
03133 }
03134 
03135 static void
03136 arm_dwarf2_frame_init_reg (struct gdbarch *gdbarch, int regnum,
03137                            struct dwarf2_frame_state_reg *reg,
03138                            struct frame_info *this_frame)
03139 {
03140   switch (regnum)
03141     {
03142     case ARM_PC_REGNUM:
03143     case ARM_PS_REGNUM:
03144       reg->how = DWARF2_FRAME_REG_FN;
03145       reg->loc.fn = arm_dwarf2_prev_register;
03146       break;
03147     case ARM_SP_REGNUM:
03148       reg->how = DWARF2_FRAME_REG_CFA;
03149       break;
03150     }
03151 }
03152 
03153 /* Return true if we are in the function's epilogue, i.e. after the
03154    instruction that destroyed the function's stack frame.  */
03155 
03156 static int
03157 thumb_in_function_epilogue_p (struct gdbarch *gdbarch, CORE_ADDR pc)
03158 {
03159   enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
03160   unsigned int insn, insn2;
03161   int found_return = 0, found_stack_adjust = 0;
03162   CORE_ADDR func_start, func_end;
03163   CORE_ADDR scan_pc;
03164   gdb_byte buf[4];
03165 
03166   if (!find_pc_partial_function (pc, NULL, &func_start, &func_end))
03167     return 0;
03168 
03169   /* The epilogue is a sequence of instructions along the following lines:
03170 
03171     - add stack frame size to SP or FP
03172     - [if frame pointer used] restore SP from FP
03173     - restore registers from SP [may include PC]
03174     - a return-type instruction [if PC wasn't already restored]
03175 
03176     In a first pass, we scan forward from the current PC and verify the
03177     instructions we find as compatible with this sequence, ending in a
03178     return instruction.
03179 
03180     However, this is not sufficient to distinguish indirect function calls
03181     within a function from indirect tail calls in the epilogue in some cases.
03182     Therefore, if we didn't already find any SP-changing instruction during
03183     forward scan, we add a backward scanning heuristic to ensure we actually
03184     are in the epilogue.  */
03185 
03186   scan_pc = pc;
03187   while (scan_pc < func_end && !found_return)
03188     {
03189       if (target_read_memory (scan_pc, buf, 2))
03190         break;
03191 
03192       scan_pc += 2;
03193       insn = extract_unsigned_integer (buf, 2, byte_order_for_code);
03194 
03195       if ((insn & 0xff80) == 0x4700)  /* bx <Rm> */
03196         found_return = 1;
03197       else if (insn == 0x46f7)  /* mov pc, lr */
03198         found_return = 1;
03199       else if (insn == 0x46bd)  /* mov sp, r7 */
03200         found_stack_adjust = 1;
03201       else if ((insn & 0xff00) == 0xb000)  /* add sp, imm or sub sp, imm  */
03202         found_stack_adjust = 1;
03203       else if ((insn & 0xfe00) == 0xbc00)  /* pop <registers> */
03204         {
03205           found_stack_adjust = 1;
03206           if (insn & 0x0100)  /* <registers> include PC.  */
03207             found_return = 1;
03208         }
03209       else if (thumb_insn_size (insn) == 4)  /* 32-bit Thumb-2 instruction */
03210         {
03211           if (target_read_memory (scan_pc, buf, 2))
03212             break;
03213 
03214           scan_pc += 2;
03215           insn2 = extract_unsigned_integer (buf, 2, byte_order_for_code);
03216 
03217           if (insn == 0xe8bd)  /* ldm.w sp!, <registers> */
03218             {
03219               found_stack_adjust = 1;
03220               if (insn2 & 0x8000)  /* <registers> include PC.  */
03221                 found_return = 1;
03222             }
03223           else if (insn == 0xf85d  /* ldr.w <Rt>, [sp], #4 */
03224                    && (insn2 & 0x0fff) == 0x0b04)
03225             {
03226               found_stack_adjust = 1;
03227               if ((insn2 & 0xf000) == 0xf000) /* <Rt> is PC.  */
03228                 found_return = 1;
03229             }
03230           else if ((insn & 0xffbf) == 0xecbd  /* vldm sp!, <list> */
03231                    && (insn2 & 0x0e00) == 0x0a00)
03232             found_stack_adjust = 1;
03233           else
03234             break;
03235         }
03236       else
03237         break;
03238     }
03239 
03240   if (!found_return)
03241     return 0;
03242 
03243   /* Since any instruction in the epilogue sequence, with the possible
03244      exception of return itself, updates the stack pointer, we need to
03245      scan backwards for at most one instruction.  Try either a 16-bit or
03246      a 32-bit instruction.  This is just a heuristic, so we do not worry
03247      too much about false positives.  */
03248 
03249   if (!found_stack_adjust)
03250     {
03251       if (pc - 4 < func_start)
03252         return 0;
03253       if (target_read_memory (pc - 4, buf, 4))
03254         return 0;
03255 
03256       insn = extract_unsigned_integer (buf, 2, byte_order_for_code);
03257       insn2 = extract_unsigned_integer (buf + 2, 2, byte_order_for_code);
03258 
03259       if (insn2 == 0x46bd)  /* mov sp, r7 */
03260         found_stack_adjust = 1;
03261       else if ((insn2 & 0xff00) == 0xb000)  /* add sp, imm or sub sp, imm  */
03262         found_stack_adjust = 1;
03263       else if ((insn2 & 0xff00) == 0xbc00)  /* pop <registers> without PC */
03264         found_stack_adjust = 1;
03265       else if (insn == 0xe8bd)  /* ldm.w sp!, <registers> */
03266         found_stack_adjust = 1;
03267       else if (insn == 0xf85d  /* ldr.w <Rt>, [sp], #4 */
03268                && (insn2 & 0x0fff) == 0x0b04)
03269         found_stack_adjust = 1;
03270       else if ((insn & 0xffbf) == 0xecbd  /* vldm sp!, <list> */
03271                && (insn2 & 0x0e00) == 0x0a00)
03272         found_stack_adjust = 1;
03273     }
03274 
03275   return found_stack_adjust;
03276 }
03277 
03278 /* Return true if we are in the function's epilogue, i.e. after the
03279    instruction that destroyed the function's stack frame.  */
03280 
03281 static int
03282 arm_in_function_epilogue_p (struct gdbarch *gdbarch, CORE_ADDR pc)
03283 {
03284   enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
03285   unsigned int insn;
03286   int found_return, found_stack_adjust;
03287   CORE_ADDR func_start, func_end;
03288 
03289   if (arm_pc_is_thumb (gdbarch, pc))
03290     return thumb_in_function_epilogue_p (gdbarch, pc);
03291 
03292   if (!find_pc_partial_function (pc, NULL, &func_start, &func_end))
03293     return 0;
03294 
03295   /* We are in the epilogue if the previous instruction was a stack
03296      adjustment and the next instruction is a possible return (bx, mov
03297      pc, or pop).  We could have to scan backwards to find the stack
03298      adjustment, or forwards to find the return, but this is a decent
03299      approximation.  First scan forwards.  */
03300 
03301   found_return = 0;
03302   insn = read_memory_unsigned_integer (pc, 4, byte_order_for_code);
03303   if (bits (insn, 28, 31) != INST_NV)
03304     {
03305       if ((insn & 0x0ffffff0) == 0x012fff10)
03306         /* BX.  */
03307         found_return = 1;
03308       else if ((insn & 0x0ffffff0) == 0x01a0f000)
03309         /* MOV PC.  */
03310         found_return = 1;
03311       else if ((insn & 0x0fff0000) == 0x08bd0000
03312           && (insn & 0x0000c000) != 0)
03313         /* POP (LDMIA), including PC or LR.  */
03314         found_return = 1;
03315     }
03316 
03317   if (!found_return)
03318     return 0;
03319 
03320   /* Scan backwards.  This is just a heuristic, so do not worry about
03321      false positives from mode changes.  */
03322 
03323   if (pc < func_start + 4)
03324     return 0;
03325 
03326   found_stack_adjust = 0;
03327   insn = read_memory_unsigned_integer (pc - 4, 4, byte_order_for_code);
03328   if (bits (insn, 28, 31) != INST_NV)
03329     {
03330       if ((insn & 0x0df0f000) == 0x0080d000)
03331         /* ADD SP (register or immediate).  */
03332         found_stack_adjust = 1;
03333       else if ((insn & 0x0df0f000) == 0x0040d000)
03334         /* SUB SP (register or immediate).  */
03335         found_stack_adjust = 1;
03336       else if ((insn & 0x0ffffff0) == 0x01a0d000)
03337         /* MOV SP.  */
03338         found_stack_adjust = 1;
03339       else if ((insn & 0x0fff0000) == 0x08bd0000)
03340         /* POP (LDMIA).  */
03341         found_stack_adjust = 1;
03342       else if ((insn & 0x0fff0000) == 0x049d0000)
03343         /* POP of a single register.  */
03344         found_stack_adjust = 1;
03345     }
03346 
03347   if (found_stack_adjust)
03348     return 1;
03349 
03350   return 0;
03351 }
03352 
03353 
03354 /* When arguments must be pushed onto the stack, they go on in reverse
03355    order.  The code below implements a FILO (stack) to do this.  */
03356 
03357 struct stack_item
03358 {
03359   int len;
03360   struct stack_item *prev;
03361   void *data;
03362 };
03363 
03364 static struct stack_item *
03365 push_stack_item (struct stack_item *prev, const void *contents, int len)
03366 {
03367   struct stack_item *si;
03368   si = xmalloc (sizeof (struct stack_item));
03369   si->data = xmalloc (len);
03370   si->len = len;
03371   si->prev = prev;
03372   memcpy (si->data, contents, len);
03373   return si;
03374 }
03375 
03376 static struct stack_item *
03377 pop_stack_item (struct stack_item *si)
03378 {
03379   struct stack_item *dead = si;
03380   si = si->prev;
03381   xfree (dead->data);
03382   xfree (dead);
03383   return si;
03384 }
03385 
03386 
03387 /* Return the alignment (in bytes) of the given type.  */
03388 
03389 static int
03390 arm_type_align (struct type *t)
03391 {
03392   int n;
03393   int align;
03394   int falign;
03395 
03396   t = check_typedef (t);
03397   switch (TYPE_CODE (t))
03398     {
03399     default:
03400       /* Should never happen.  */
03401       internal_error (__FILE__, __LINE__, _("unknown type alignment"));
03402       return 4;
03403 
03404     case TYPE_CODE_PTR:
03405     case TYPE_CODE_ENUM:
03406     case TYPE_CODE_INT:
03407     case TYPE_CODE_FLT:
03408     case TYPE_CODE_SET:
03409     case TYPE_CODE_RANGE:
03410     case TYPE_CODE_REF:
03411     case TYPE_CODE_CHAR:
03412     case TYPE_CODE_BOOL:
03413       return TYPE_LENGTH (t);
03414 
03415     case TYPE_CODE_ARRAY:
03416     case TYPE_CODE_COMPLEX:
03417       /* TODO: What about vector types?  */
03418       return arm_type_align (TYPE_TARGET_TYPE (t));
03419 
03420     case TYPE_CODE_STRUCT:
03421     case TYPE_CODE_UNION:
03422       align = 1;
03423       for (n = 0; n < TYPE_NFIELDS (t); n++)
03424         {
03425           falign = arm_type_align (TYPE_FIELD_TYPE (t, n));
03426           if (falign > align)
03427             align = falign;
03428         }
03429       return align;
03430     }
03431 }
03432 
03433 /* Possible base types for a candidate for passing and returning in
03434    VFP registers.  */
03435 
03436 enum arm_vfp_cprc_base_type
03437 {
03438   VFP_CPRC_UNKNOWN,
03439   VFP_CPRC_SINGLE,
03440   VFP_CPRC_DOUBLE,
03441   VFP_CPRC_VEC64,
03442   VFP_CPRC_VEC128
03443 };
03444 
03445 /* The length of one element of base type B.  */
03446 
03447 static unsigned
03448 arm_vfp_cprc_unit_length (enum arm_vfp_cprc_base_type b)
03449 {
03450   switch (b)
03451     {
03452     case VFP_CPRC_SINGLE:
03453       return 4;
03454     case VFP_CPRC_DOUBLE:
03455       return 8;
03456     case VFP_CPRC_VEC64:
03457       return 8;
03458     case VFP_CPRC_VEC128:
03459       return 16;
03460     default:
03461       internal_error (__FILE__, __LINE__, _("Invalid VFP CPRC type: %d."),
03462                       (int) b);
03463     }
03464 }
03465 
03466 /* The character ('s', 'd' or 'q') for the type of VFP register used
03467    for passing base type B.  */
03468 
03469 static int
03470 arm_vfp_cprc_reg_char (enum arm_vfp_cprc_base_type b)
03471 {
03472   switch (b)
03473     {
03474     case VFP_CPRC_SINGLE:
03475       return 's';
03476     case VFP_CPRC_DOUBLE:
03477       return 'd';
03478     case VFP_CPRC_VEC64:
03479       return 'd';
03480     case VFP_CPRC_VEC128:
03481       return 'q';
03482     default:
03483       internal_error (__FILE__, __LINE__, _("Invalid VFP CPRC type: %d."),
03484                       (int) b);
03485     }
03486 }
03487 
03488 /* Determine whether T may be part of a candidate for passing and
03489    returning in VFP registers, ignoring the limit on the total number
03490    of components.  If *BASE_TYPE is VFP_CPRC_UNKNOWN, set it to the
03491    classification of the first valid component found; if it is not
03492    VFP_CPRC_UNKNOWN, all components must have the same classification
03493    as *BASE_TYPE.  If it is found that T contains a type not permitted
03494    for passing and returning in VFP registers, a type differently
03495    classified from *BASE_TYPE, or two types differently classified
03496    from each other, return -1, otherwise return the total number of
03497    base-type elements found (possibly 0 in an empty structure or
03498    array).  Vectors and complex types are not currently supported,
03499    matching the generic AAPCS support.  */
03500 
03501 static int
03502 arm_vfp_cprc_sub_candidate (struct type *t,
03503                             enum arm_vfp_cprc_base_type *base_type)
03504 {
03505   t = check_typedef (t);
03506   switch (TYPE_CODE (t))
03507     {
03508     case TYPE_CODE_FLT:
03509       switch (TYPE_LENGTH (t))
03510         {
03511         case 4:
03512           if (*base_type == VFP_CPRC_UNKNOWN)
03513             *base_type = VFP_CPRC_SINGLE;
03514           else if (*base_type != VFP_CPRC_SINGLE)
03515             return -1;
03516           return 1;
03517 
03518         case 8:
03519           if (*base_type == VFP_CPRC_UNKNOWN)
03520             *base_type = VFP_CPRC_DOUBLE;
03521           else if (*base_type != VFP_CPRC_DOUBLE)
03522             return -1;
03523           return 1;
03524 
03525         default:
03526           return -1;
03527         }
03528       break;
03529 
03530     case TYPE_CODE_ARRAY:
03531       {
03532         int count;
03533         unsigned unitlen;
03534         count = arm_vfp_cprc_sub_candidate (TYPE_TARGET_TYPE (t), base_type);
03535         if (count == -1)
03536           return -1;
03537         if (TYPE_LENGTH (t) == 0)
03538           {
03539             gdb_assert (count == 0);
03540             return 0;
03541           }
03542         else if (count == 0)
03543           return -1;
03544         unitlen = arm_vfp_cprc_unit_length (*base_type);
03545         gdb_assert ((TYPE_LENGTH (t) % unitlen) == 0);
03546         return TYPE_LENGTH (t) / unitlen;
03547       }
03548       break;
03549 
03550     case TYPE_CODE_STRUCT:
03551       {
03552         int count = 0;
03553         unsigned unitlen;
03554         int i;
03555         for (i = 0; i < TYPE_NFIELDS (t); i++)
03556           {
03557             int sub_count = arm_vfp_cprc_sub_candidate (TYPE_FIELD_TYPE (t, i),
03558                                                         base_type);
03559             if (sub_count == -1)
03560               return -1;
03561             count += sub_count;
03562           }
03563         if (TYPE_LENGTH (t) == 0)
03564           {
03565             gdb_assert (count == 0);
03566             return 0;
03567           }
03568         else if (count == 0)
03569           return -1;
03570         unitlen = arm_vfp_cprc_unit_length (*base_type);
03571         if (TYPE_LENGTH (t) != unitlen * count)
03572           return -1;
03573         return count;
03574       }
03575 
03576     case TYPE_CODE_UNION:
03577       {
03578         int count = 0;
03579         unsigned unitlen;
03580         int i;
03581         for (i = 0; i < TYPE_NFIELDS (t); i++)
03582           {
03583             int sub_count = arm_vfp_cprc_sub_candidate (TYPE_FIELD_TYPE (t, i),
03584                                                         base_type);
03585             if (sub_count == -1)
03586               return -1;
03587             count = (count > sub_count ? count : sub_count);
03588           }
03589         if (TYPE_LENGTH (t) == 0)
03590           {
03591             gdb_assert (count == 0);
03592             return 0;
03593           }
03594         else if (count == 0)
03595           return -1;
03596         unitlen = arm_vfp_cprc_unit_length (*base_type);
03597         if (TYPE_LENGTH (t) != unitlen * count)
03598           return -1;
03599         return count;
03600       }
03601 
03602     default:
03603       break;
03604     }
03605 
03606   return -1;
03607 }
03608 
03609 /* Determine whether T is a VFP co-processor register candidate (CPRC)
03610    if passed to or returned from a non-variadic function with the VFP
03611    ABI in effect.  Return 1 if it is, 0 otherwise.  If it is, set
03612    *BASE_TYPE to the base type for T and *COUNT to the number of
03613    elements of that base type before returning.  */
03614 
03615 static int
03616 arm_vfp_call_candidate (struct type *t, enum arm_vfp_cprc_base_type *base_type,
03617                         int *count)
03618 {
03619   enum arm_vfp_cprc_base_type b = VFP_CPRC_UNKNOWN;
03620   int c = arm_vfp_cprc_sub_candidate (t, &b);
03621   if (c <= 0 || c > 4)
03622     return 0;
03623   *base_type = b;
03624   *count = c;
03625   return 1;
03626 }
03627 
03628 /* Return 1 if the VFP ABI should be used for passing arguments to and
03629    returning values from a function of type FUNC_TYPE, 0
03630    otherwise.  */
03631 
03632 static int
03633 arm_vfp_abi_for_function (struct gdbarch *gdbarch, struct type *func_type)
03634 {
03635   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
03636   /* Variadic functions always use the base ABI.  Assume that functions
03637      without debug info are not variadic.  */
03638   if (func_type && TYPE_VARARGS (check_typedef (func_type)))
03639     return 0;
03640   /* The VFP ABI is only supported as a variant of AAPCS.  */
03641   if (tdep->arm_abi != ARM_ABI_AAPCS)
03642     return 0;
03643   return gdbarch_tdep (gdbarch)->fp_model == ARM_FLOAT_VFP;
03644 }
03645 
03646 /* We currently only support passing parameters in integer registers, which
03647    conforms with GCC's default model, and VFP argument passing following
03648    the VFP variant of AAPCS.  Several other variants exist and
03649    we should probably support some of them based on the selected ABI.  */
03650 
03651 static CORE_ADDR
03652 arm_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
03653                      struct regcache *regcache, CORE_ADDR bp_addr, int nargs,
03654                      struct value **args, CORE_ADDR sp, int struct_return,
03655                      CORE_ADDR struct_addr)
03656 {
03657   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
03658   int argnum;
03659   int argreg;
03660   int nstack;
03661   struct stack_item *si = NULL;
03662   int use_vfp_abi;
03663   struct type *ftype;
03664   unsigned vfp_regs_free = (1 << 16) - 1;
03665 
03666   /* Determine the type of this function and whether the VFP ABI
03667      applies.  */
03668   ftype = check_typedef (value_type (function));
03669   if (TYPE_CODE (ftype) == TYPE_CODE_PTR)
03670     ftype = check_typedef (TYPE_TARGET_TYPE (ftype));
03671   use_vfp_abi = arm_vfp_abi_for_function (gdbarch, ftype);
03672 
03673   /* Set the return address.  For the ARM, the return breakpoint is
03674      always at BP_ADDR.  */
03675   if (arm_pc_is_thumb (gdbarch, bp_addr))
03676     bp_addr |= 1;
03677   regcache_cooked_write_unsigned (regcache, ARM_LR_REGNUM, bp_addr);
03678 
03679   /* Walk through the list of args and determine how large a temporary
03680      stack is required.  Need to take care here as structs may be
03681      passed on the stack, and we have to push them.  */
03682   nstack = 0;
03683 
03684   argreg = ARM_A1_REGNUM;
03685   nstack = 0;
03686 
03687   /* The struct_return pointer occupies the first parameter
03688      passing register.  */
03689   if (struct_return)
03690     {
03691       if (arm_debug)
03692         fprintf_unfiltered (gdb_stdlog, "struct return in %s = %s\n",
03693                             gdbarch_register_name (gdbarch, argreg),
03694                             paddress (gdbarch, struct_addr));
03695       regcache_cooked_write_unsigned (regcache, argreg, struct_addr);
03696       argreg++;
03697     }
03698 
03699   for (argnum = 0; argnum < nargs; argnum++)
03700     {
03701       int len;
03702       struct type *arg_type;
03703       struct type *target_type;
03704       enum type_code typecode;
03705       const bfd_byte *val;
03706       int align;
03707       enum arm_vfp_cprc_base_type vfp_base_type;
03708       int vfp_base_count;
03709       int may_use_core_reg = 1;
03710 
03711       arg_type = check_typedef (value_type (args[argnum]));
03712       len = TYPE_LENGTH (arg_type);
03713       target_type = TYPE_TARGET_TYPE (arg_type);
03714       typecode = TYPE_CODE (arg_type);
03715       val = value_contents (args[argnum]);
03716 
03717       align = arm_type_align (arg_type);
03718       /* Round alignment up to a whole number of words.  */
03719       align = (align + INT_REGISTER_SIZE - 1) & ~(INT_REGISTER_SIZE - 1);
03720       /* Different ABIs have different maximum alignments.  */
03721       if (gdbarch_tdep (gdbarch)->arm_abi == ARM_ABI_APCS)
03722         {
03723           /* The APCS ABI only requires word alignment.  */
03724           align = INT_REGISTER_SIZE;
03725         }
03726       else
03727         {
03728           /* The AAPCS requires at most doubleword alignment.  */
03729           if (align > INT_REGISTER_SIZE * 2)
03730             align = INT_REGISTER_SIZE * 2;
03731         }
03732 
03733       if (use_vfp_abi
03734           && arm_vfp_call_candidate (arg_type, &vfp_base_type,
03735                                      &vfp_base_count))
03736         {
03737           int regno;
03738           int unit_length;
03739           int shift;
03740           unsigned mask;
03741 
03742           /* Because this is a CPRC it cannot go in a core register or
03743              cause a core register to be skipped for alignment.
03744              Either it goes in VFP registers and the rest of this loop
03745              iteration is skipped for this argument, or it goes on the
03746              stack (and the stack alignment code is correct for this
03747              case).  */
03748           may_use_core_reg = 0;
03749 
03750           unit_length = arm_vfp_cprc_unit_length (vfp_base_type);
03751           shift = unit_length / 4;
03752           mask = (1 << (shift * vfp_base_count)) - 1;
03753           for (regno = 0; regno < 16; regno += shift)
03754             if (((vfp_regs_free >> regno) & mask) == mask)
03755               break;
03756 
03757           if (regno < 16)
03758             {
03759               int reg_char;
03760               int reg_scaled;
03761               int i;
03762 
03763               vfp_regs_free &= ~(mask << regno);
03764               reg_scaled = regno / shift;
03765               reg_char = arm_vfp_cprc_reg_char (vfp_base_type);
03766               for (i = 0; i < vfp_base_count; i++)
03767                 {
03768                   char name_buf[4];
03769                   int regnum;
03770                   if (reg_char == 'q')
03771                     arm_neon_quad_write (gdbarch, regcache, reg_scaled + i,
03772                                          val + i * unit_length);
03773                   else
03774                     {
03775                       xsnprintf (name_buf, sizeof (name_buf), "%c%d",
03776                                  reg_char, reg_scaled + i);
03777                       regnum = user_reg_map_name_to_regnum (gdbarch, name_buf,
03778                                                             strlen (name_buf));
03779                       regcache_cooked_write (regcache, regnum,
03780                                              val + i * unit_length);
03781                     }
03782                 }
03783               continue;
03784             }
03785           else
03786             {
03787               /* This CPRC could not go in VFP registers, so all VFP
03788                  registers are now marked as used.  */
03789               vfp_regs_free = 0;
03790             }
03791         }
03792 
03793       /* Push stack padding for dowubleword alignment.  */
03794       if (nstack & (align - 1))
03795         {
03796           si = push_stack_item (si, val, INT_REGISTER_SIZE);
03797           nstack += INT_REGISTER_SIZE;
03798         }
03799       
03800       /* Doubleword aligned quantities must go in even register pairs.  */
03801       if (may_use_core_reg
03802           && argreg <= ARM_LAST_ARG_REGNUM
03803           && align > INT_REGISTER_SIZE
03804           && argreg & 1)
03805         argreg++;
03806 
03807       /* If the argument is a pointer to a function, and it is a
03808          Thumb function, create a LOCAL copy of the value and set
03809          the THUMB bit in it.  */
03810       if (TYPE_CODE_PTR == typecode
03811           && target_type != NULL
03812           && TYPE_CODE_FUNC == TYPE_CODE (check_typedef (target_type)))
03813         {
03814           CORE_ADDR regval = extract_unsigned_integer (val, len, byte_order);
03815           if (arm_pc_is_thumb (gdbarch, regval))
03816             {
03817               bfd_byte *copy = alloca (len);
03818               store_unsigned_integer (copy, len, byte_order,
03819                                       MAKE_THUMB_ADDR (regval));
03820               val = copy;
03821             }
03822         }
03823 
03824       /* Copy the argument to general registers or the stack in
03825          register-sized pieces.  Large arguments are split between
03826          registers and stack.  */
03827       while (len > 0)
03828         {
03829           int partial_len = len < INT_REGISTER_SIZE ? len : INT_REGISTER_SIZE;
03830 
03831           if (may_use_core_reg && argreg <= ARM_LAST_ARG_REGNUM)
03832             {
03833               /* The argument is being passed in a general purpose
03834                  register.  */
03835               CORE_ADDR regval
03836                 = extract_unsigned_integer (val, partial_len, byte_order);
03837               if (byte_order == BFD_ENDIAN_BIG)
03838                 regval <<= (INT_REGISTER_SIZE - partial_len) * 8;
03839               if (arm_debug)
03840                 fprintf_unfiltered (gdb_stdlog, "arg %d in %s = 0x%s\n",
03841                                     argnum,
03842                                     gdbarch_register_name
03843                                       (gdbarch, argreg),
03844                                     phex (regval, INT_REGISTER_SIZE));
03845               regcache_cooked_write_unsigned (regcache, argreg, regval);
03846               argreg++;
03847             }
03848           else
03849             {
03850               /* Push the arguments onto the stack.  */
03851               if (arm_debug)
03852                 fprintf_unfiltered (gdb_stdlog, "arg %d @ sp + %d\n",
03853                                     argnum, nstack);
03854               si = push_stack_item (si, val, INT_REGISTER_SIZE);
03855               nstack += INT_REGISTER_SIZE;
03856             }
03857               
03858           len -= partial_len;
03859           val += partial_len;
03860         }
03861     }
03862   /* If we have an odd number of words to push, then decrement the stack
03863      by one word now, so first stack argument will be dword aligned.  */
03864   if (nstack & 4)
03865     sp -= 4;
03866 
03867   while (si)
03868     {
03869       sp -= si->len;
03870       write_memory (sp, si->data, si->len);
03871       si = pop_stack_item (si);
03872     }
03873 
03874   /* Finally, update teh SP register.  */
03875   regcache_cooked_write_unsigned (regcache, ARM_SP_REGNUM, sp);
03876 
03877   return sp;
03878 }
03879 
03880 
03881 /* Always align the frame to an 8-byte boundary.  This is required on
03882    some platforms and harmless on the rest.  */
03883 
03884 static CORE_ADDR
03885 arm_frame_align (struct gdbarch *gdbarch, CORE_ADDR sp)
03886 {
03887   /* Align the stack to eight bytes.  */
03888   return sp & ~ (CORE_ADDR) 7;
03889 }
03890 
03891 static void
03892 print_fpu_flags (int flags)
03893 {
03894   if (flags & (1 << 0))
03895     fputs ("IVO ", stdout);
03896   if (flags & (1 << 1))
03897     fputs ("DVZ ", stdout);
03898   if (flags & (1 << 2))
03899     fputs ("OFL ", stdout);
03900   if (flags & (1 << 3))
03901     fputs ("UFL ", stdout);
03902   if (flags & (1 << 4))
03903     fputs ("INX ", stdout);
03904   putchar ('\n');
03905 }
03906 
03907 /* Print interesting information about the floating point processor
03908    (if present) or emulator.  */
03909 static void
03910 arm_print_float_info (struct gdbarch *gdbarch, struct ui_file *file,
03911                       struct frame_info *frame, const char *args)
03912 {
03913   unsigned long status = get_frame_register_unsigned (frame, ARM_FPS_REGNUM);
03914   int type;
03915 
03916   type = (status >> 24) & 127;
03917   if (status & (1 << 31))
03918     printf (_("Hardware FPU type %d\n"), type);
03919   else
03920     printf (_("Software FPU type %d\n"), type);
03921   /* i18n: [floating point unit] mask */
03922   fputs (_("mask: "), stdout);
03923   print_fpu_flags (status >> 16);
03924   /* i18n: [floating point unit] flags */
03925   fputs (_("flags: "), stdout);
03926   print_fpu_flags (status);
03927 }
03928 
03929 /* Construct the ARM extended floating point type.  */
03930 static struct type *
03931 arm_ext_type (struct gdbarch *gdbarch)
03932 {
03933   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
03934 
03935   if (!tdep->arm_ext_type)
03936     tdep->arm_ext_type
03937       = arch_float_type (gdbarch, -1, "builtin_type_arm_ext",
03938                          floatformats_arm_ext);
03939 
03940   return tdep->arm_ext_type;
03941 }
03942 
03943 static struct type *
03944 arm_neon_double_type (struct gdbarch *gdbarch)
03945 {
03946   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
03947 
03948   if (tdep->neon_double_type == NULL)
03949     {
03950       struct type *t, *elem;
03951 
03952       t = arch_composite_type (gdbarch, "__gdb_builtin_type_neon_d",
03953                                TYPE_CODE_UNION);
03954       elem = builtin_type (gdbarch)->builtin_uint8;
03955       append_composite_type_field (t, "u8", init_vector_type (elem, 8));
03956       elem = builtin_type (gdbarch)->builtin_uint16;
03957       append_composite_type_field (t, "u16", init_vector_type (elem, 4));
03958       elem = builtin_type (gdbarch)->builtin_uint32;
03959       append_composite_type_field (t, "u32", init_vector_type (elem, 2));
03960       elem = builtin_type (gdbarch)->builtin_uint64;
03961       append_composite_type_field (t, "u64", elem);
03962       elem = builtin_type (gdbarch)->builtin_float;
03963       append_composite_type_field (t, "f32", init_vector_type (elem, 2));
03964       elem = builtin_type (gdbarch)->builtin_double;
03965       append_composite_type_field (t, "f64", elem);
03966 
03967       TYPE_VECTOR (t) = 1;
03968       TYPE_NAME (t) = "neon_d";
03969       tdep->neon_double_type = t;
03970     }
03971 
03972   return tdep->neon_double_type;
03973 }
03974 
03975 /* FIXME: The vector types are not correctly ordered on big-endian
03976    targets.  Just as s0 is the low bits of d0, d0[0] is also the low
03977    bits of d0 - regardless of what unit size is being held in d0.  So
03978    the offset of the first uint8 in d0 is 7, but the offset of the
03979    first float is 4.  This code works as-is for little-endian
03980    targets.  */
03981 
03982 static struct type *
03983 arm_neon_quad_type (struct gdbarch *gdbarch)
03984 {
03985   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
03986 
03987   if (tdep->neon_quad_type == NULL)
03988     {
03989       struct type *t, *elem;
03990 
03991       t = arch_composite_type (gdbarch, "__gdb_builtin_type_neon_q",
03992                                TYPE_CODE_UNION);
03993       elem = builtin_type (gdbarch)->builtin_uint8;
03994       append_composite_type_field (t, "u8", init_vector_type (elem, 16));
03995       elem = builtin_type (gdbarch)->builtin_uint16;
03996       append_composite_type_field (t, "u16", init_vector_type (elem, 8));
03997       elem = builtin_type (gdbarch)->builtin_uint32;
03998       append_composite_type_field (t, "u32", init_vector_type (elem, 4));
03999       elem = builtin_type (gdbarch)->builtin_uint64;
04000       append_composite_type_field (t, "u64", init_vector_type (elem, 2));
04001       elem = builtin_type (gdbarch)->builtin_float;
04002       append_composite_type_field (t, "f32", init_vector_type (elem, 4));
04003       elem = builtin_type (gdbarch)->builtin_double;
04004       append_composite_type_field (t, "f64", init_vector_type (elem, 2));
04005 
04006       TYPE_VECTOR (t) = 1;
04007       TYPE_NAME (t) = "neon_q";
04008       tdep->neon_quad_type = t;
04009     }
04010 
04011   return tdep->neon_quad_type;
04012 }
04013 
04014 /* Return the GDB type object for the "standard" data type of data in
04015    register N.  */
04016 
04017 static struct type *
04018 arm_register_type (struct gdbarch *gdbarch, int regnum)
04019 {
04020   int num_regs = gdbarch_num_regs (gdbarch);
04021 
04022   if (gdbarch_tdep (gdbarch)->have_vfp_pseudos
04023       && regnum >= num_regs && regnum < num_regs + 32)
04024     return builtin_type (gdbarch)->builtin_float;
04025 
04026   if (gdbarch_tdep (gdbarch)->have_neon_pseudos
04027       && regnum >= num_regs + 32 && regnum < num_regs + 32 + 16)
04028     return arm_neon_quad_type (gdbarch);
04029 
04030   /* If the target description has register information, we are only
04031      in this function so that we can override the types of
04032      double-precision registers for NEON.  */
04033   if (tdesc_has_registers (gdbarch_target_desc (gdbarch)))
04034     {
04035       struct type *t = tdesc_register_type (gdbarch, regnum);
04036 
04037       if (regnum >= ARM_D0_REGNUM && regnum < ARM_D0_REGNUM + 32
04038           && TYPE_CODE (t) == TYPE_CODE_FLT
04039           && gdbarch_tdep (gdbarch)->have_neon)
04040         return arm_neon_double_type (gdbarch);
04041       else
04042         return t;
04043     }
04044 
04045   if (regnum >= ARM_F0_REGNUM && regnum < ARM_F0_REGNUM + NUM_FREGS)
04046     {
04047       if (!gdbarch_tdep (gdbarch)->have_fpa_registers)
04048         return builtin_type (gdbarch)->builtin_void;
04049 
04050       return arm_ext_type (gdbarch);
04051     }
04052   else if (regnum == ARM_SP_REGNUM)
04053     return builtin_type (gdbarch)->builtin_data_ptr;
04054   else if (regnum == ARM_PC_REGNUM)
04055     return builtin_type (gdbarch)->builtin_func_ptr;
04056   else if (regnum >= ARRAY_SIZE (arm_register_names))
04057     /* These registers are only supported on targets which supply
04058        an XML description.  */
04059     return builtin_type (gdbarch)->builtin_int0;
04060   else
04061     return builtin_type (gdbarch)->builtin_uint32;
04062 }
04063 
04064 /* Map a DWARF register REGNUM onto the appropriate GDB register
04065    number.  */
04066 
04067 static int
04068 arm_dwarf_reg_to_regnum (struct gdbarch *gdbarch, int reg)
04069 {
04070   /* Core integer regs.  */
04071   if (reg >= 0 && reg <= 15)
04072     return reg;
04073 
04074   /* Legacy FPA encoding.  These were once used in a way which
04075      overlapped with VFP register numbering, so their use is
04076      discouraged, but GDB doesn't support the ARM toolchain
04077      which used them for VFP.  */
04078   if (reg >= 16 && reg <= 23)
04079     return ARM_F0_REGNUM + reg - 16;
04080 
04081   /* New assignments for the FPA registers.  */
04082   if (reg >= 96 && reg <= 103)
04083     return ARM_F0_REGNUM + reg - 96;
04084 
04085   /* WMMX register assignments.  */
04086   if (reg >= 104 && reg <= 111)
04087     return ARM_WCGR0_REGNUM + reg - 104;
04088 
04089   if (reg >= 112 && reg <= 127)
04090     return ARM_WR0_REGNUM + reg - 112;
04091 
04092   if (reg >= 192 && reg <= 199)
04093     return ARM_WC0_REGNUM + reg - 192;
04094 
04095   /* VFP v2 registers.  A double precision value is actually
04096      in d1 rather than s2, but the ABI only defines numbering
04097      for the single precision registers.  This will "just work"
04098      in GDB for little endian targets (we'll read eight bytes,
04099      starting in s0 and then progressing to s1), but will be
04100      reversed on big endian targets with VFP.  This won't
04101      be a problem for the new Neon quad registers; you're supposed
04102      to use DW_OP_piece for those.  */
04103   if (reg >= 64 && reg <= 95)
04104     {
04105       char name_buf[4];
04106 
04107       xsnprintf (name_buf, sizeof (name_buf), "s%d", reg - 64);
04108       return user_reg_map_name_to_regnum (gdbarch, name_buf,
04109                                           strlen (name_buf));
04110     }
04111 
04112   /* VFP v3 / Neon registers.  This range is also used for VFP v2
04113      registers, except that it now describes d0 instead of s0.  */
04114   if (reg >= 256 && reg <= 287)
04115     {
04116       char name_buf[4];
04117 
04118       xsnprintf (name_buf, sizeof (name_buf), "d%d", reg - 256);
04119       return user_reg_map_name_to_regnum (gdbarch, name_buf,
04120                                           strlen (name_buf));
04121     }
04122 
04123   return -1;
04124 }
04125 
04126 /* Map GDB internal REGNUM onto the Arm simulator register numbers.  */
04127 static int
04128 arm_register_sim_regno (struct gdbarch *gdbarch, int regnum)
04129 {
04130   int reg = regnum;
04131   gdb_assert (reg >= 0 && reg < gdbarch_num_regs (gdbarch));
04132 
04133   if (regnum >= ARM_WR0_REGNUM && regnum <= ARM_WR15_REGNUM)
04134     return regnum - ARM_WR0_REGNUM + SIM_ARM_IWMMXT_COP0R0_REGNUM;
04135 
04136   if (regnum >= ARM_WC0_REGNUM && regnum <= ARM_WC7_REGNUM)
04137     return regnum - ARM_WC0_REGNUM + SIM_ARM_IWMMXT_COP1R0_REGNUM;
04138 
04139   if (regnum >= ARM_WCGR0_REGNUM && regnum <= ARM_WCGR7_REGNUM)
04140     return regnum - ARM_WCGR0_REGNUM + SIM_ARM_IWMMXT_COP1R8_REGNUM;
04141 
04142   if (reg < NUM_GREGS)
04143     return SIM_ARM_R0_REGNUM + reg;
04144   reg -= NUM_GREGS;
04145 
04146   if (reg < NUM_FREGS)
04147     return SIM_ARM_FP0_REGNUM + reg;
04148   reg -= NUM_FREGS;
04149 
04150   if (reg < NUM_SREGS)
04151     return SIM_ARM_FPS_REGNUM + reg;
04152   reg -= NUM_SREGS;
04153 
04154   internal_error (__FILE__, __LINE__, _("Bad REGNUM %d"), regnum);
04155 }
04156 
04157 /* NOTE: cagney/2001-08-20: Both convert_from_extended() and
04158    convert_to_extended() use floatformat_arm_ext_littlebyte_bigword.
04159    It is thought that this is is the floating-point register format on
04160    little-endian systems.  */
04161 
04162 static void
04163 convert_from_extended (const struct floatformat *fmt, const void *ptr,
04164                        void *dbl, int endianess)
04165 {
04166   DOUBLEST d;
04167 
04168   if (endianess == BFD_ENDIAN_BIG)
04169     floatformat_to_doublest (&floatformat_arm_ext_big, ptr, &d);
04170   else
04171     floatformat_to_doublest (&floatformat_arm_ext_littlebyte_bigword,
04172                              ptr, &d);
04173   floatformat_from_doublest (fmt, &d, dbl);
04174 }
04175 
04176 static void
04177 convert_to_extended (const struct floatformat *fmt, void *dbl, const void *ptr,
04178                      int endianess)
04179 {
04180   DOUBLEST d;
04181 
04182   floatformat_to_doublest (fmt, ptr, &d);
04183   if (endianess == BFD_ENDIAN_BIG)
04184     floatformat_from_doublest (&floatformat_arm_ext_big, &d, dbl);
04185   else
04186     floatformat_from_doublest (&floatformat_arm_ext_littlebyte_bigword,
04187                                &d, dbl);
04188 }
04189 
04190 static int
04191 condition_true (unsigned long cond, unsigned long status_reg)
04192 {
04193   if (cond == INST_AL || cond == INST_NV)
04194     return 1;
04195 
04196   switch (cond)
04197     {
04198     case INST_EQ:
04199       return ((status_reg & FLAG_Z) != 0);
04200     case INST_NE:
04201       return ((status_reg & FLAG_Z) == 0);
04202     case INST_CS:
04203       return ((status_reg & FLAG_C) != 0);
04204     case INST_CC:
04205       return ((status_reg & FLAG_C) == 0);
04206     case INST_MI:
04207       return ((status_reg & FLAG_N) != 0);
04208     case INST_PL:
04209       return ((status_reg & FLAG_N) == 0);
04210     case INST_VS:
04211       return ((status_reg & FLAG_V) != 0);
04212     case INST_VC:
04213       return ((status_reg & FLAG_V) == 0);
04214     case INST_HI:
04215       return ((status_reg & (FLAG_C | FLAG_Z)) == FLAG_C);
04216     case INST_LS:
04217       return ((status_reg & (FLAG_C | FLAG_Z)) != FLAG_C);
04218     case INST_GE:
04219       return (((status_reg & FLAG_N) == 0) == ((status_reg & FLAG_V) == 0));
04220     case INST_LT:
04221       return (((status_reg & FLAG_N) == 0) != ((status_reg & FLAG_V) == 0));
04222     case INST_GT:
04223       return (((status_reg & FLAG_Z) == 0)
04224               && (((status_reg & FLAG_N) == 0)
04225                   == ((status_reg & FLAG_V) == 0)));
04226     case INST_LE:
04227       return (((status_reg & FLAG_Z) != 0)
04228               || (((status_reg & FLAG_N) == 0)
04229                   != ((status_reg & FLAG_V) == 0)));
04230     }
04231   return 1;
04232 }
04233 
04234 static unsigned long
04235 shifted_reg_val (struct frame_info *frame, unsigned long inst, int carry,
04236                  unsigned long pc_val, unsigned long status_reg)
04237 {
04238   unsigned long res, shift;
04239   int rm = bits (inst, 0, 3);
04240   unsigned long shifttype = bits (inst, 5, 6);
04241 
04242   if (bit (inst, 4))
04243     {
04244       int rs = bits (inst, 8, 11);
04245       shift = (rs == 15 ? pc_val + 8
04246                         : get_frame_register_unsigned (frame, rs)) & 0xFF;
04247     }
04248   else
04249     shift = bits (inst, 7, 11);
04250 
04251   res = (rm == ARM_PC_REGNUM
04252          ? (pc_val + (bit (inst, 4) ? 12 : 8))
04253          : get_frame_register_unsigned (frame, rm));
04254 
04255   switch (shifttype)
04256     {
04257     case 0:                     /* LSL */
04258       res = shift >= 32 ? 0 : res << shift;
04259       break;
04260 
04261     case 1:                     /* LSR */
04262       res = shift >= 32 ? 0 : res >> shift;
04263       break;
04264 
04265     case 2:                     /* ASR */
04266       if (shift >= 32)
04267         shift = 31;
04268       res = ((res & 0x80000000L)
04269              ? ~((~res) >> shift) : res >> shift);
04270       break;
04271 
04272     case 3:                     /* ROR/RRX */
04273       shift &= 31;
04274       if (shift == 0)
04275         res = (res >> 1) | (carry ? 0x80000000L : 0);
04276       else
04277         res = (res >> shift) | (res << (32 - shift));
04278       break;
04279     }
04280 
04281   return res & 0xffffffff;
04282 }
04283 
04284 /* Return number of 1-bits in VAL.  */
04285 
04286 static int
04287 bitcount (unsigned long val)
04288 {
04289   int nbits;
04290   for (nbits = 0; val != 0; nbits++)
04291     val &= val - 1;             /* Delete rightmost 1-bit in val.  */
04292   return nbits;
04293 }
04294 
04295 /* Return the size in bytes of the complete Thumb instruction whose
04296    first halfword is INST1.  */
04297 
04298 static int
04299 thumb_insn_size (unsigned short inst1)
04300 {
04301   if ((inst1 & 0xe000) == 0xe000 && (inst1 & 0x1800) != 0)
04302     return 4;
04303   else
04304     return 2;
04305 }
04306 
04307 static int
04308 thumb_advance_itstate (unsigned int itstate)
04309 {
04310   /* Preserve IT[7:5], the first three bits of the condition.  Shift
04311      the upcoming condition flags left by one bit.  */
04312   itstate = (itstate & 0xe0) | ((itstate << 1) & 0x1f);
04313 
04314   /* If we have finished the IT block, clear the state.  */
04315   if ((itstate & 0x0f) == 0)
04316     itstate = 0;
04317 
04318   return itstate;
04319 }
04320 
04321 /* Find the next PC after the current instruction executes.  In some
04322    cases we can not statically determine the answer (see the IT state
04323    handling in this function); in that case, a breakpoint may be
04324    inserted in addition to the returned PC, which will be used to set
04325    another breakpoint by our caller.  */
04326 
04327 static CORE_ADDR
04328 thumb_get_next_pc_raw (struct frame_info *frame, CORE_ADDR pc)
04329 {
04330   struct gdbarch *gdbarch = get_frame_arch (frame);
04331   struct address_space *aspace = get_frame_address_space (frame);
04332   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
04333   enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
04334   unsigned long pc_val = ((unsigned long) pc) + 4;      /* PC after prefetch */
04335   unsigned short inst1;
04336   CORE_ADDR nextpc = pc + 2;            /* Default is next instruction.  */
04337   unsigned long offset;
04338   ULONGEST status, itstate;
04339 
04340   nextpc = MAKE_THUMB_ADDR (nextpc);
04341   pc_val = MAKE_THUMB_ADDR (pc_val);
04342 
04343   inst1 = read_memory_unsigned_integer (pc, 2, byte_order_for_code);
04344 
04345   /* Thumb-2 conditional execution support.  There are eight bits in
04346      the CPSR which describe conditional execution state.  Once
04347      reconstructed (they're in a funny order), the low five bits
04348      describe the low bit of the condition for each instruction and
04349      how many instructions remain.  The high three bits describe the
04350      base condition.  One of the low four bits will be set if an IT
04351      block is active.  These bits read as zero on earlier
04352      processors.  */
04353   status = get_frame_register_unsigned (frame, ARM_PS_REGNUM);
04354   itstate = ((status >> 8) & 0xfc) | ((status >> 25) & 0x3);
04355 
04356   /* If-Then handling.  On GNU/Linux, where this routine is used, we
04357      use an undefined instruction as a breakpoint.  Unlike BKPT, IT
04358      can disable execution of the undefined instruction.  So we might
04359      miss the breakpoint if we set it on a skipped conditional
04360      instruction.  Because conditional instructions can change the
04361      flags, affecting the execution of further instructions, we may
04362      need to set two breakpoints.  */
04363 
04364   if (gdbarch_tdep (gdbarch)->thumb2_breakpoint != NULL)
04365     {
04366       if ((inst1 & 0xff00) == 0xbf00 && (inst1 & 0x000f) != 0)
04367         {
04368           /* An IT instruction.  Because this instruction does not
04369              modify the flags, we can accurately predict the next
04370              executed instruction.  */
04371           itstate = inst1 & 0x00ff;
04372           pc += thumb_insn_size (inst1);
04373 
04374           while (itstate != 0 && ! condition_true (itstate >> 4, status))
04375             {
04376               inst1 = read_memory_unsigned_integer (pc, 2,
04377                                                     byte_order_for_code);
04378               pc += thumb_insn_size (inst1);
04379               itstate = thumb_advance_itstate (itstate);
04380             }
04381 
04382           return MAKE_THUMB_ADDR (pc);
04383         }
04384       else if (itstate != 0)
04385         {
04386           /* We are in a conditional block.  Check the condition.  */
04387           if (! condition_true (itstate >> 4, status))
04388             {
04389               /* Advance to the next executed instruction.  */
04390               pc += thumb_insn_size (inst1);
04391               itstate = thumb_advance_itstate (itstate);
04392 
04393               while (itstate != 0 && ! condition_true (itstate >> 4, status))
04394                 {
04395                   inst1 = read_memory_unsigned_integer (pc, 2, 
04396                                                         byte_order_for_code);
04397                   pc += thumb_insn_size (inst1);
04398                   itstate = thumb_advance_itstate (itstate);
04399                 }
04400 
04401               return MAKE_THUMB_ADDR (pc);
04402             }
04403           else if ((itstate & 0x0f) == 0x08)
04404             {
04405               /* This is the last instruction of the conditional
04406                  block, and it is executed.  We can handle it normally
04407                  because the following instruction is not conditional,
04408                  and we must handle it normally because it is
04409                  permitted to branch.  Fall through.  */
04410             }
04411           else
04412             {
04413               int cond_negated;
04414 
04415               /* There are conditional instructions after this one.
04416                  If this instruction modifies the flags, then we can
04417                  not predict what the next executed instruction will
04418                  be.  Fortunately, this instruction is architecturally
04419                  forbidden to branch; we know it will fall through.
04420                  Start by skipping past it.  */
04421               pc += thumb_insn_size (inst1);
04422               itstate = thumb_advance_itstate (itstate);
04423 
04424               /* Set a breakpoint on the following instruction.  */
04425               gdb_assert ((itstate & 0x0f) != 0);
04426               arm_insert_single_step_breakpoint (gdbarch, aspace,
04427                                                  MAKE_THUMB_ADDR (pc));
04428               cond_negated = (itstate >> 4) & 1;
04429 
04430               /* Skip all following instructions with the same
04431                  condition.  If there is a later instruction in the IT
04432                  block with the opposite condition, set the other
04433                  breakpoint there.  If not, then set a breakpoint on
04434                  the instruction after the IT block.  */
04435               do
04436                 {
04437                   inst1 = read_memory_unsigned_integer (pc, 2,
04438                                                         byte_order_for_code);
04439                   pc += thumb_insn_size (inst1);
04440                   itstate = thumb_advance_itstate (itstate);
04441                 }
04442               while (itstate != 0 && ((itstate >> 4) & 1) == cond_negated);
04443 
04444               return MAKE_THUMB_ADDR (pc);
04445             }
04446         }
04447     }
04448   else if (itstate & 0x0f)
04449     {
04450       /* We are in a conditional block.  Check the condition.  */
04451       int cond = itstate >> 4;
04452 
04453       if (! condition_true (cond, status))
04454         /* Advance to the next instruction.  All the 32-bit
04455            instructions share a common prefix.  */
04456         return MAKE_THUMB_ADDR (pc + thumb_insn_size (inst1));
04457 
04458       /* Otherwise, handle the instruction normally.  */
04459     }
04460 
04461   if ((inst1 & 0xff00) == 0xbd00)       /* pop {rlist, pc} */
04462     {
04463       CORE_ADDR sp;
04464 
04465       /* Fetch the saved PC from the stack.  It's stored above
04466          all of the other registers.  */
04467       offset = bitcount (bits (inst1, 0, 7)) * INT_REGISTER_SIZE;
04468       sp = get_frame_register_unsigned (frame, ARM_SP_REGNUM);
04469       nextpc = read_memory_unsigned_integer (sp + offset, 4, byte_order);
04470     }
04471   else if ((inst1 & 0xf000) == 0xd000)  /* conditional branch */
04472     {
04473       unsigned long cond = bits (inst1, 8, 11);
04474       if (cond == 0x0f)  /* 0x0f = SWI */
04475         {
04476           struct gdbarch_tdep *tdep;
04477           tdep = gdbarch_tdep (gdbarch);
04478 
04479           if (tdep->syscall_next_pc != NULL)
04480             nextpc = tdep->syscall_next_pc (frame);
04481 
04482         }
04483       else if (cond != 0x0f && condition_true (cond, status))
04484         nextpc = pc_val + (sbits (inst1, 0, 7) << 1);
04485     }
04486   else if ((inst1 & 0xf800) == 0xe000)  /* unconditional branch */
04487     {
04488       nextpc = pc_val + (sbits (inst1, 0, 10) << 1);
04489     }
04490   else if (thumb_insn_size (inst1) == 4) /* 32-bit instruction */
04491     {
04492       unsigned short inst2;
04493       inst2 = read_memory_unsigned_integer (pc + 2, 2, byte_order_for_code);
04494 
04495       /* Default to the next instruction.  */
04496       nextpc = pc + 4;
04497       nextpc = MAKE_THUMB_ADDR (nextpc);
04498 
04499       if ((inst1 & 0xf800) == 0xf000 && (inst2 & 0x8000) == 0x8000)
04500         {
04501           /* Branches and miscellaneous control instructions.  */
04502 
04503           if ((inst2 & 0x1000) != 0 || (inst2 & 0xd001) == 0xc000)
04504             {
04505               /* B, BL, BLX.  */
04506               int j1, j2, imm1, imm2;
04507 
04508               imm1 = sbits (inst1, 0, 10);
04509               imm2 = bits (inst2, 0, 10);
04510               j1 = bit (inst2, 13);
04511               j2 = bit (inst2, 11);
04512 
04513               offset = ((imm1 << 12) + (imm2 << 1));
04514               offset ^= ((!j2) << 22) | ((!j1) << 23);
04515 
04516               nextpc = pc_val + offset;
04517               /* For BLX make sure to clear the low bits.  */
04518               if (bit (inst2, 12) == 0)
04519                 nextpc = nextpc & 0xfffffffc;
04520             }
04521           else if (inst1 == 0xf3de && (inst2 & 0xff00) == 0x3f00)
04522             {
04523               /* SUBS PC, LR, #imm8.  */
04524               nextpc = get_frame_register_unsigned (frame, ARM_LR_REGNUM);
04525               nextpc -= inst2 & 0x00ff;
04526             }
04527           else if ((inst2 & 0xd000) == 0x8000 && (inst1 & 0x0380) != 0x0380)
04528             {
04529               /* Conditional branch.  */
04530               if (condition_true (bits (inst1, 6, 9), status))
04531                 {
04532                   int sign, j1, j2, imm1, imm2;
04533 
04534                   sign = sbits (inst1, 10, 10);
04535                   imm1 = bits (inst1, 0, 5);
04536                   imm2 = bits (inst2, 0, 10);
04537                   j1 = bit (inst2, 13);
04538                   j2 = bit (inst2, 11);
04539 
04540                   offset = (sign << 20) + (j2 << 19) + (j1 << 18);
04541                   offset += (imm1 << 12) + (imm2 << 1);
04542 
04543                   nextpc = pc_val + offset;
04544                 }
04545             }
04546         }
04547       else if ((inst1 & 0xfe50) == 0xe810)
04548         {
04549           /* Load multiple or RFE.  */
04550           int rn, offset, load_pc = 1;
04551 
04552           rn = bits (inst1, 0, 3);
04553           if (bit (inst1, 7) && !bit (inst1, 8))
04554             {
04555               /* LDMIA or POP */
04556               if (!bit (inst2, 15))
04557                 load_pc = 0;
04558               offset = bitcount (inst2) * 4 - 4;
04559             }
04560           else if (!bit (inst1, 7) && bit (inst1, 8))
04561             {
04562               /* LDMDB */
04563               if (!bit (inst2, 15))
04564                 load_pc = 0;
04565               offset = -4;
04566             }
04567           else if (bit (inst1, 7) && bit (inst1, 8))
04568             {
04569               /* RFEIA */
04570               offset = 0;
04571             }
04572           else if (!bit (inst1, 7) && !bit (inst1, 8))
04573             {
04574               /* RFEDB */
04575               offset = -8;
04576             }
04577           else
04578             load_pc = 0;
04579 
04580           if (load_pc)
04581             {
04582               CORE_ADDR addr = get_frame_register_unsigned (frame, rn);
04583               nextpc = get_frame_memory_unsigned (frame, addr + offset, 4);
04584             }
04585         }
04586       else if ((inst1 & 0xffef) == 0xea4f && (inst2 & 0xfff0) == 0x0f00)
04587         {
04588           /* MOV PC or MOVS PC.  */
04589           nextpc = get_frame_register_unsigned (frame, bits (inst2, 0, 3));
04590           nextpc = MAKE_THUMB_ADDR (nextpc);
04591         }
04592       else if ((inst1 & 0xff70) == 0xf850 && (inst2 & 0xf000) == 0xf000)
04593         {
04594           /* LDR PC.  */
04595           CORE_ADDR base;
04596           int rn, load_pc = 1;
04597 
04598           rn = bits (inst1, 0, 3);
04599           base = get_frame_register_unsigned (frame, rn);
04600           if (rn == ARM_PC_REGNUM)
04601             {
04602               base = (base + 4) & ~(CORE_ADDR) 0x3;
04603               if (bit (inst1, 7))
04604                 base += bits (inst2, 0, 11);
04605               else
04606                 base -= bits (inst2, 0, 11);
04607             }
04608           else if (bit (inst1, 7))
04609             base += bits (inst2, 0, 11);
04610           else if (bit (inst2, 11))
04611             {
04612               if (bit (inst2, 10))
04613                 {
04614                   if (bit (inst2, 9))
04615                     base += bits (inst2, 0, 7);
04616                   else
04617                     base -= bits (inst2, 0, 7);
04618                 }
04619             }
04620           else if ((inst2 & 0x0fc0) == 0x0000)
04621             {
04622               int shift = bits (inst2, 4, 5), rm = bits (inst2, 0, 3);
04623               base += get_frame_register_unsigned (frame, rm) << shift;
04624             }
04625           else
04626             /* Reserved.  */
04627             load_pc = 0;
04628 
04629           if (load_pc)
04630             nextpc = get_frame_memory_unsigned (frame, base, 4);
04631         }
04632       else if ((inst1 & 0xfff0) == 0xe8d0 && (inst2 & 0xfff0) == 0xf000)
04633         {
04634           /* TBB.  */
04635           CORE_ADDR tbl_reg, table, offset, length;
04636 
04637           tbl_reg = bits (inst1, 0, 3);
04638           if (tbl_reg == 0x0f)
04639             table = pc + 4;  /* Regcache copy of PC isn't right yet.  */
04640           else
04641             table = get_frame_register_unsigned (frame, tbl_reg);
04642 
04643           offset = get_frame_register_unsigned (frame, bits (inst2, 0, 3));
04644           length = 2 * get_frame_memory_unsigned (frame, table + offset, 1);
04645           nextpc = pc_val + length;
04646         }
04647       else if ((inst1 & 0xfff0) == 0xe8d0 && (inst2 & 0xfff0) == 0xf010)
04648         {
04649           /* TBH.  */
04650           CORE_ADDR tbl_reg, table, offset, length;
04651 
04652           tbl_reg = bits (inst1, 0, 3);
04653           if (tbl_reg == 0x0f)
04654             table = pc + 4;  /* Regcache copy of PC isn't right yet.  */
04655           else
04656             table = get_frame_register_unsigned (frame, tbl_reg);
04657 
04658           offset = 2 * get_frame_register_unsigned (frame, bits (inst2, 0, 3));
04659           length = 2 * get_frame_memory_unsigned (frame, table + offset, 2);
04660           nextpc = pc_val + length;
04661         }
04662     }
04663   else if ((inst1 & 0xff00) == 0x4700)  /* bx REG, blx REG */
04664     {
04665       if (bits (inst1, 3, 6) == 0x0f)
04666         nextpc = UNMAKE_THUMB_ADDR (pc_val);
04667       else
04668         nextpc = get_frame_register_unsigned (frame, bits (inst1, 3, 6));
04669     }
04670   else if ((inst1 & 0xff87) == 0x4687)  /* mov pc, REG */
04671     {
04672       if (bits (inst1, 3, 6) == 0x0f)
04673         nextpc = pc_val;
04674       else
04675         nextpc = get_frame_register_unsigned (frame, bits (inst1, 3, 6));
04676 
04677       nextpc = MAKE_THUMB_ADDR (nextpc);
04678     }
04679   else if ((inst1 & 0xf500) == 0xb100)
04680     {
04681       /* CBNZ or CBZ.  */
04682       int imm = (bit (inst1, 9) << 6) + (bits (inst1, 3, 7) << 1);
04683       ULONGEST reg = get_frame_register_unsigned (frame, bits (inst1, 0, 2));
04684 
04685       if (bit (inst1, 11) && reg != 0)
04686         nextpc = pc_val + imm;
04687       else if (!bit (inst1, 11) && reg == 0)
04688         nextpc = pc_val + imm;
04689     }
04690   return nextpc;
04691 }
04692 
04693 /* Get the raw next address.  PC is the current program counter, in 
04694    FRAME, which is assumed to be executing in ARM mode.
04695 
04696    The value returned has the execution state of the next instruction 
04697    encoded in it.  Use IS_THUMB_ADDR () to see whether the instruction is
04698    in Thumb-State, and gdbarch_addr_bits_remove () to get the plain memory
04699    address.  */
04700 
04701 static CORE_ADDR
04702 arm_get_next_pc_raw (struct frame_info *frame, CORE_ADDR pc)
04703 {
04704   struct gdbarch *gdbarch = get_frame_arch (frame);
04705   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
04706   enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
04707   unsigned long pc_val;
04708   unsigned long this_instr;
04709   unsigned long status;
04710   CORE_ADDR nextpc;
04711 
04712   pc_val = (unsigned long) pc;
04713   this_instr = read_memory_unsigned_integer (pc, 4, byte_order_for_code);
04714 
04715   status = get_frame_register_unsigned (frame, ARM_PS_REGNUM);
04716   nextpc = (CORE_ADDR) (pc_val + 4);    /* Default case */
04717 
04718   if (bits (this_instr, 28, 31) == INST_NV)
04719     switch (bits (this_instr, 24, 27))
04720       {
04721       case 0xa:
04722       case 0xb:
04723         {
04724           /* Branch with Link and change to Thumb.  */
04725           nextpc = BranchDest (pc, this_instr);
04726           nextpc |= bit (this_instr, 24) << 1;
04727           nextpc = MAKE_THUMB_ADDR (nextpc);
04728           break;
04729         }
04730       case 0xc:
04731       case 0xd:
04732       case 0xe:
04733         /* Coprocessor register transfer.  */
04734         if (bits (this_instr, 12, 15) == 15)
04735           error (_("Invalid update to pc in instruction"));
04736         break;
04737       }
04738   else if (condition_true (bits (this_instr, 28, 31), status))
04739     {
04740       switch (bits (this_instr, 24, 27))
04741         {
04742         case 0x0:
04743         case 0x1:                       /* data processing */
04744         case 0x2:
04745         case 0x3:
04746           {
04747             unsigned long operand1, operand2, result = 0;
04748             unsigned long rn;
04749             int c;
04750 
04751             if (bits (this_instr, 12, 15) != 15)
04752               break;
04753 
04754             if (bits (this_instr, 22, 25) == 0
04755                 && bits (this_instr, 4, 7) == 9)        /* multiply */
04756               error (_("Invalid update to pc in instruction"));
04757 
04758             /* BX <reg>, BLX <reg> */
04759             if (bits (this_instr, 4, 27) == 0x12fff1
04760                 || bits (this_instr, 4, 27) == 0x12fff3)
04761               {
04762                 rn = bits (this_instr, 0, 3);
04763                 nextpc = ((rn == ARM_PC_REGNUM)
04764                           ? (pc_val + 8)
04765                           : get_frame_register_unsigned (frame, rn));
04766 
04767                 return nextpc;
04768               }
04769 
04770             /* Multiply into PC.  */
04771             c = (status & FLAG_C) ? 1 : 0;
04772             rn = bits (this_instr, 16, 19);
04773             operand1 = ((rn == ARM_PC_REGNUM)
04774                         ? (pc_val + 8)
04775                         : get_frame_register_unsigned (frame, rn));
04776 
04777             if (bit (this_instr, 25))
04778               {
04779                 unsigned long immval = bits (this_instr, 0, 7);
04780                 unsigned long rotate = 2 * bits (this_instr, 8, 11);
04781                 operand2 = ((immval >> rotate) | (immval << (32 - rotate)))
04782                   & 0xffffffff;
04783               }
04784             else                /* operand 2 is a shifted register.  */
04785               operand2 = shifted_reg_val (frame, this_instr, c,
04786                                           pc_val, status);
04787 
04788             switch (bits (this_instr, 21, 24))
04789               {
04790               case 0x0: /*and */
04791                 result = operand1 & operand2;
04792                 break;
04793 
04794               case 0x1: /*eor */
04795                 result = operand1 ^ operand2;
04796                 break;
04797 
04798               case 0x2: /*sub */
04799                 result = operand1 - operand2;
04800                 break;
04801 
04802               case 0x3: /*rsb */
04803                 result = operand2 - operand1;
04804                 break;
04805 
04806               case 0x4: /*add */
04807                 result = operand1 + operand2;
04808                 break;
04809 
04810               case 0x5: /*adc */
04811                 result = operand1 + operand2 + c;
04812                 break;
04813 
04814               case 0x6: /*sbc */
04815                 result = operand1 - operand2 + c;
04816                 break;
04817 
04818               case 0x7: /*rsc */
04819                 result = operand2 - operand1 + c;
04820                 break;
04821 
04822               case 0x8:
04823               case 0x9:
04824               case 0xa:
04825               case 0xb: /* tst, teq, cmp, cmn */
04826                 result = (unsigned long) nextpc;
04827                 break;
04828 
04829               case 0xc: /*orr */
04830                 result = operand1 | operand2;
04831                 break;
04832 
04833               case 0xd: /*mov */
04834                 /* Always step into a function.  */
04835                 result = operand2;
04836                 break;
04837 
04838               case 0xe: /*bic */
04839                 result = operand1 & ~operand2;
04840                 break;
04841 
04842               case 0xf: /*mvn */
04843                 result = ~operand2;
04844                 break;
04845               }
04846 
04847             /* In 26-bit APCS the bottom two bits of the result are 
04848                ignored, and we always end up in ARM state.  */
04849             if (!arm_apcs_32)
04850               nextpc = arm_addr_bits_remove (gdbarch, result);
04851             else
04852               nextpc = result;
04853 
04854             break;
04855           }
04856 
04857         case 0x4:
04858         case 0x5:               /* data transfer */
04859         case 0x6:
04860         case 0x7:
04861           if (bit (this_instr, 20))
04862             {
04863               /* load */
04864               if (bits (this_instr, 12, 15) == 15)
04865                 {
04866                   /* rd == pc */
04867                   unsigned long rn;
04868                   unsigned long base;
04869 
04870                   if (bit (this_instr, 22))
04871                     error (_("Invalid update to pc in instruction"));
04872 
04873                   /* byte write to PC */
04874                   rn = bits (this_instr, 16, 19);
04875                   base = ((rn == ARM_PC_REGNUM)
04876                           ? (pc_val + 8)
04877                           : get_frame_register_unsigned (frame, rn));
04878 
04879                   if (bit (this_instr, 24))
04880                     {
04881                       /* pre-indexed */
04882                       int c = (status & FLAG_C) ? 1 : 0;
04883                       unsigned long offset =
04884                       (bit (this_instr, 25)
04885                        ? shifted_reg_val (frame, this_instr, c, pc_val, status)
04886                        : bits (this_instr, 0, 11));
04887 
04888                       if (bit (this_instr, 23))
04889                         base += offset;
04890                       else
04891                         base -= offset;
04892                     }
04893                   nextpc =
04894                     (CORE_ADDR) read_memory_unsigned_integer ((CORE_ADDR) base,
04895                                                               4, byte_order);
04896                 }
04897             }
04898           break;
04899 
04900         case 0x8:
04901         case 0x9:               /* block transfer */
04902           if (bit (this_instr, 20))
04903             {
04904               /* LDM */
04905               if (bit (this_instr, 15))
04906                 {
04907                   /* loading pc */
04908                   int offset = 0;
04909                   unsigned long rn_val
04910                     = get_frame_register_unsigned (frame,
04911                                                    bits (this_instr, 16, 19));
04912 
04913                   if (bit (this_instr, 23))
04914                     {
04915                       /* up */
04916                       unsigned long reglist = bits (this_instr, 0, 14);
04917                       offset = bitcount (reglist) * 4;
04918                       if (bit (this_instr, 24))         /* pre */
04919                         offset += 4;
04920                     }
04921                   else if (bit (this_instr, 24))
04922                     offset = -4;
04923 
04924                   nextpc =
04925                     (CORE_ADDR) read_memory_unsigned_integer ((CORE_ADDR)
04926                                                               (rn_val + offset),
04927                                                               4, byte_order);
04928                 }
04929             }
04930           break;
04931 
04932         case 0xb:               /* branch & link */
04933         case 0xa:               /* branch */
04934           {
04935             nextpc = BranchDest (pc, this_instr);
04936             break;
04937           }
04938 
04939         case 0xc:
04940         case 0xd:
04941         case 0xe:               /* coproc ops */
04942           break;
04943         case 0xf:               /* SWI */
04944           {
04945             struct gdbarch_tdep *tdep;
04946             tdep = gdbarch_tdep (gdbarch);
04947 
04948             if (tdep->syscall_next_pc != NULL)
04949               nextpc = tdep->syscall_next_pc (frame);
04950 
04951           }
04952           break;
04953 
04954         default:
04955           fprintf_filtered (gdb_stderr, _("Bad bit-field extraction\n"));
04956           return (pc);
04957         }
04958     }
04959 
04960   return nextpc;
04961 }
04962 
04963 /* Determine next PC after current instruction executes.  Will call either
04964    arm_get_next_pc_raw or thumb_get_next_pc_raw.  Error out if infinite
04965    loop is detected.  */
04966 
04967 CORE_ADDR
04968 arm_get_next_pc (struct frame_info *frame, CORE_ADDR pc)
04969 {
04970   CORE_ADDR nextpc;
04971 
04972   if (arm_frame_is_thumb (frame))
04973     {
04974       nextpc = thumb_get_next_pc_raw (frame, pc);
04975       if (nextpc == MAKE_THUMB_ADDR (pc))
04976         error (_("Infinite loop detected"));
04977     }
04978   else
04979     {
04980       nextpc = arm_get_next_pc_raw (frame, pc);
04981       if (nextpc == pc)
04982         error (_("Infinite loop detected"));
04983     }
04984 
04985   return nextpc;
04986 }
04987 
04988 /* Like insert_single_step_breakpoint, but make sure we use a breakpoint
04989    of the appropriate mode (as encoded in the PC value), even if this
04990    differs from what would be expected according to the symbol tables.  */
04991 
04992 void
04993 arm_insert_single_step_breakpoint (struct gdbarch *gdbarch,
04994                                    struct address_space *aspace,
04995                                    CORE_ADDR pc)
04996 {
04997   struct cleanup *old_chain
04998     = make_cleanup_restore_integer (&arm_override_mode);
04999 
05000   arm_override_mode = IS_THUMB_ADDR (pc);
05001   pc = gdbarch_addr_bits_remove (gdbarch, pc);
05002 
05003   insert_single_step_breakpoint (gdbarch, aspace, pc);
05004 
05005   do_cleanups (old_chain);
05006 }
05007 
05008 /* Checks for an atomic sequence of instructions beginning with a LDREX{,B,H,D}
05009    instruction and ending with a STREX{,B,H,D} instruction.  If such a sequence
05010    is found, attempt to step through it.  A breakpoint is placed at the end of
05011    the sequence.  */
05012 
05013 static int
05014 thumb_deal_with_atomic_sequence_raw (struct frame_info *frame)
05015 {
05016   struct gdbarch *gdbarch = get_frame_arch (frame);
05017   struct address_space *aspace = get_frame_address_space (frame);
05018   enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
05019   CORE_ADDR pc = get_frame_pc (frame);
05020   CORE_ADDR breaks[2] = {-1, -1};
05021   CORE_ADDR loc = pc;
05022   unsigned short insn1, insn2;
05023   int insn_count;
05024   int index;
05025   int last_breakpoint = 0; /* Defaults to 0 (no breakpoints placed).  */
05026   const int atomic_sequence_length = 16; /* Instruction sequence length.  */
05027   ULONGEST status, itstate;
05028 
05029   /* We currently do not support atomic sequences within an IT block.  */
05030   status = get_frame_register_unsigned (frame, ARM_PS_REGNUM);
05031   itstate = ((status >> 8) & 0xfc) | ((status >> 25) & 0x3);
05032   if (itstate & 0x0f)
05033     return 0;
05034 
05035   /* Assume all atomic sequences start with a ldrex{,b,h,d} instruction.  */
05036   insn1 = read_memory_unsigned_integer (loc, 2, byte_order_for_code);
05037   loc += 2;
05038   if (thumb_insn_size (insn1) != 4)
05039     return 0;
05040 
05041   insn2 = read_memory_unsigned_integer (loc, 2, byte_order_for_code);
05042   loc += 2;
05043   if (!((insn1 & 0xfff0) == 0xe850
05044         || ((insn1 & 0xfff0) == 0xe8d0 && (insn2 & 0x00c0) == 0x0040)))
05045     return 0;
05046 
05047   /* Assume that no atomic sequence is longer than "atomic_sequence_length"
05048      instructions.  */
05049   for (insn_count = 0; insn_count < atomic_sequence_length; ++insn_count)
05050     {
05051       insn1 = read_memory_unsigned_integer (loc, 2, byte_order_for_code);
05052       loc += 2;
05053 
05054       if (thumb_insn_size (insn1) != 4)
05055         {
05056           /* Assume that there is at most one conditional branch in the
05057              atomic sequence.  If a conditional branch is found, put a
05058              breakpoint in its destination address.  */
05059           if ((insn1 & 0xf000) == 0xd000 && bits (insn1, 8, 11) != 0x0f)
05060             {
05061               if (last_breakpoint > 0)
05062                 return 0; /* More than one conditional branch found,
05063                              fallback to the standard code.  */
05064 
05065               breaks[1] = loc + 2 + (sbits (insn1, 0, 7) << 1);
05066               last_breakpoint++;
05067             }
05068 
05069           /* We do not support atomic sequences that use any *other*
05070              instructions but conditional branches to change the PC.
05071              Fall back to standard code to avoid losing control of
05072              execution.  */
05073           else if (thumb_instruction_changes_pc (insn1))
05074             return 0;
05075         }
05076       else
05077         {
05078           insn2 = read_memory_unsigned_integer (loc, 2, byte_order_for_code);
05079           loc += 2;
05080 
05081           /* Assume that there is at most one conditional branch in the
05082              atomic sequence.  If a conditional branch is found, put a
05083              breakpoint in its destination address.  */
05084           if ((insn1 & 0xf800) == 0xf000
05085               && (insn2 & 0xd000) == 0x8000
05086               && (insn1 & 0x0380) != 0x0380)
05087             {
05088               int sign, j1, j2, imm1, imm2;
05089               unsigned int offset;
05090 
05091               sign = sbits (insn1, 10, 10);
05092               imm1 = bits (insn1, 0, 5);
05093               imm2 = bits (insn2, 0, 10);
05094               j1 = bit (insn2, 13);
05095               j2 = bit (insn2, 11);
05096 
05097               offset = (sign << 20) + (j2 << 19) + (j1 << 18);
05098               offset += (imm1 << 12) + (imm2 << 1);
05099 
05100               if (last_breakpoint > 0)
05101                 return 0; /* More than one conditional branch found,
05102                              fallback to the standard code.  */
05103 
05104               breaks[1] = loc + offset;
05105               last_breakpoint++;
05106             }
05107 
05108           /* We do not support atomic sequences that use any *other*
05109              instructions but conditional branches to change the PC.
05110              Fall back to standard code to avoid losing control of
05111              execution.  */
05112           else if (thumb2_instruction_changes_pc (insn1, insn2))
05113             return 0;
05114 
05115           /* If we find a strex{,b,h,d}, we're done.  */
05116           if ((insn1 & 0xfff0) == 0xe840
05117               || ((insn1 & 0xfff0) == 0xe8c0 && (insn2 & 0x00c0) == 0x0040))
05118             break;
05119         }
05120     }
05121 
05122   /* If we didn't find the strex{,b,h,d}, we cannot handle the sequence.  */
05123   if (insn_count == atomic_sequence_length)
05124     return 0;
05125 
05126   /* Insert a breakpoint right after the end of the atomic sequence.  */
05127   breaks[0] = loc;
05128 
05129   /* Check for duplicated breakpoints.  Check also for a breakpoint
05130      placed (branch instruction's destination) anywhere in sequence.  */
05131   if (last_breakpoint
05132       && (breaks[1] == breaks[0]
05133           || (breaks[1] >= pc && breaks[1] < loc)))
05134     last_breakpoint = 0;
05135 
05136   /* Effectively inserts the breakpoints.  */
05137   for (index = 0; index <= last_breakpoint; index++)
05138     arm_insert_single_step_breakpoint (gdbarch, aspace,
05139                                        MAKE_THUMB_ADDR (breaks[index]));
05140 
05141   return 1;
05142 }
05143 
05144 static int
05145 arm_deal_with_atomic_sequence_raw (struct frame_info *frame)
05146 {
05147   struct gdbarch *gdbarch = get_frame_arch (frame);
05148   struct address_space *aspace = get_frame_address_space (frame);
05149   enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
05150   CORE_ADDR pc = get_frame_pc (frame);
05151   CORE_ADDR breaks[2] = {-1, -1};
05152   CORE_ADDR loc = pc;
05153   unsigned int insn;
05154   int insn_count;
05155   int index;
05156   int last_breakpoint = 0; /* Defaults to 0 (no breakpoints placed).  */
05157   const int atomic_sequence_length = 16; /* Instruction sequence length.  */
05158 
05159   /* Assume all atomic sequences start with a ldrex{,b,h,d} instruction.
05160      Note that we do not currently support conditionally executed atomic
05161      instructions.  */
05162   insn = read_memory_unsigned_integer (loc, 4, byte_order_for_code);
05163   loc += 4;
05164   if ((insn & 0xff9000f0) != 0xe1900090)
05165     return 0;
05166 
05167   /* Assume that no atomic sequence is longer than "atomic_sequence_length"
05168      instructions.  */
05169   for (insn_count = 0; insn_count < atomic_sequence_length; ++insn_count)
05170     {
05171       insn = read_memory_unsigned_integer (loc, 4, byte_order_for_code);
05172       loc += 4;
05173 
05174       /* Assume that there is at most one conditional branch in the atomic
05175          sequence.  If a conditional branch is found, put a breakpoint in
05176          its destination address.  */
05177       if (bits (insn, 24, 27) == 0xa)
05178         {
05179           if (last_breakpoint > 0)
05180             return 0; /* More than one conditional branch found, fallback
05181                          to the standard single-step code.  */
05182 
05183           breaks[1] = BranchDest (loc - 4, insn);
05184           last_breakpoint++;
05185         }
05186 
05187       /* We do not support atomic sequences that use any *other* instructions
05188          but conditional branches to change the PC.  Fall back to standard
05189          code to avoid losing control of execution.  */
05190       else if (arm_instruction_changes_pc (insn))
05191         return 0;
05192 
05193       /* If we find a strex{,b,h,d}, we're done.  */
05194       if ((insn & 0xff9000f0) == 0xe1800090)
05195         break;
05196     }
05197 
05198   /* If we didn't find the strex{,b,h,d}, we cannot handle the sequence.  */
05199   if (insn_count == atomic_sequence_length)
05200     return 0;
05201 
05202   /* Insert a breakpoint right after the end of the atomic sequence.  */
05203   breaks[0] = loc;
05204 
05205   /* Check for duplicated breakpoints.  Check also for a breakpoint
05206      placed (branch instruction's destination) anywhere in sequence.  */
05207   if (last_breakpoint
05208       && (breaks[1] == breaks[0]
05209           || (breaks[1] >= pc && breaks[1] < loc)))
05210     last_breakpoint = 0;
05211 
05212   /* Effectively inserts the breakpoints.  */
05213   for (index = 0; index <= last_breakpoint; index++)
05214     arm_insert_single_step_breakpoint (gdbarch, aspace, breaks[index]);
05215 
05216   return 1;
05217 }
05218 
05219 int
05220 arm_deal_with_atomic_sequence (struct frame_info *frame)
05221 {
05222   if (arm_frame_is_thumb (frame))
05223     return thumb_deal_with_atomic_sequence_raw (frame);
05224   else
05225     return arm_deal_with_atomic_sequence_raw (frame);
05226 }
05227 
05228 /* single_step() is called just before we want to resume the inferior,
05229    if we want to single-step it but there is no hardware or kernel
05230    single-step support.  We find the target of the coming instruction
05231    and breakpoint it.  */
05232 
05233 int
05234 arm_software_single_step (struct frame_info *frame)
05235 {
05236   struct gdbarch *gdbarch = get_frame_arch (frame);
05237   struct address_space *aspace = get_frame_address_space (frame);
05238   CORE_ADDR next_pc;
05239 
05240   if (arm_deal_with_atomic_sequence (frame))
05241     return 1;
05242 
05243   next_pc = arm_get_next_pc (frame, get_frame_pc (frame));
05244   arm_insert_single_step_breakpoint (gdbarch, aspace, next_pc);
05245 
05246   return 1;
05247 }
05248 
05249 /* Given BUF, which is OLD_LEN bytes ending at ENDADDR, expand
05250    the buffer to be NEW_LEN bytes ending at ENDADDR.  Return
05251    NULL if an error occurs.  BUF is freed.  */
05252 
05253 static gdb_byte *
05254 extend_buffer_earlier (gdb_byte *buf, CORE_ADDR endaddr,
05255                        int old_len, int new_len)
05256 {
05257   gdb_byte *new_buf;
05258   int bytes_to_read = new_len - old_len;
05259 
05260   new_buf = xmalloc (new_len);
05261   memcpy (new_buf + bytes_to_read, buf, old_len);
05262   xfree (buf);
05263   if (target_read_memory (endaddr - new_len, new_buf, bytes_to_read) != 0)
05264     {
05265       xfree (new_buf);
05266       return NULL;
05267     }
05268   return new_buf;
05269 }
05270 
05271 /* An IT block is at most the 2-byte IT instruction followed by
05272    four 4-byte instructions.  The furthest back we must search to
05273    find an IT block that affects the current instruction is thus
05274    2 + 3 * 4 == 14 bytes.  */
05275 #define MAX_IT_BLOCK_PREFIX 14
05276 
05277 /* Use a quick scan if there are more than this many bytes of
05278    code.  */
05279 #define IT_SCAN_THRESHOLD 32
05280 
05281 /* Adjust a breakpoint's address to move breakpoints out of IT blocks.
05282    A breakpoint in an IT block may not be hit, depending on the
05283    condition flags.  */
05284 static CORE_ADDR
05285 arm_adjust_breakpoint_address (struct gdbarch *gdbarch, CORE_ADDR bpaddr)
05286 {
05287   gdb_byte *buf;
05288   char map_type;
05289   CORE_ADDR boundary, func_start;
05290   int buf_len;
05291   enum bfd_endian order = gdbarch_byte_order_for_code (gdbarch);
05292   int i, any, last_it, last_it_count;
05293 
05294   /* If we are using BKPT breakpoints, none of this is necessary.  */
05295   if (gdbarch_tdep (gdbarch)->thumb2_breakpoint == NULL)
05296     return bpaddr;
05297 
05298   /* ARM mode does not have this problem.  */
05299   if (!arm_pc_is_thumb (gdbarch, bpaddr))
05300     return bpaddr;
05301 
05302   /* We are setting a breakpoint in Thumb code that could potentially
05303      contain an IT block.  The first step is to find how much Thumb
05304      code there is; we do not need to read outside of known Thumb
05305      sequences.  */
05306   map_type = arm_find_mapping_symbol (bpaddr, &boundary);
05307   if (map_type == 0)
05308     /* Thumb-2 code must have mapping symbols to have a chance.  */
05309     return bpaddr;
05310 
05311   bpaddr = gdbarch_addr_bits_remove (gdbarch, bpaddr);
05312 
05313   if (find_pc_partial_function (bpaddr, NULL, &func_start, NULL)
05314       && func_start > boundary)
05315     boundary = func_start;
05316 
05317   /* Search for a candidate IT instruction.  We have to do some fancy
05318      footwork to distinguish a real IT instruction from the second
05319      half of a 32-bit instruction, but there is no need for that if
05320      there's no candidate.  */
05321   buf_len = min (bpaddr - boundary, MAX_IT_BLOCK_PREFIX);
05322   if (buf_len == 0)
05323     /* No room for an IT instruction.  */
05324     return bpaddr;
05325 
05326   buf = xmalloc (buf_len);
05327   if (target_read_memory (bpaddr - buf_len, buf, buf_len) != 0)
05328     return bpaddr;
05329   any = 0;
05330   for (i = 0; i < buf_len; i += 2)
05331     {
05332       unsigned short inst1 = extract_unsigned_integer (&buf[i], 2, order);
05333       if ((inst1 & 0xff00) == 0xbf00 && (inst1 & 0x000f) != 0)
05334         {
05335           any = 1;
05336           break;
05337         }
05338     }
05339   if (any == 0)
05340     {
05341       xfree (buf);
05342       return bpaddr;
05343     }
05344 
05345   /* OK, the code bytes before this instruction contain at least one
05346      halfword which resembles an IT instruction.  We know that it's
05347      Thumb code, but there are still two possibilities.  Either the
05348      halfword really is an IT instruction, or it is the second half of
05349      a 32-bit Thumb instruction.  The only way we can tell is to
05350      scan forwards from a known instruction boundary.  */
05351   if (bpaddr - boundary > IT_SCAN_THRESHOLD)
05352     {
05353       int definite;
05354 
05355       /* There's a lot of code before this instruction.  Start with an
05356          optimistic search; it's easy to recognize halfwords that can
05357          not be the start of a 32-bit instruction, and use that to
05358          lock on to the instruction boundaries.  */
05359       buf = extend_buffer_earlier (buf, bpaddr, buf_len, IT_SCAN_THRESHOLD);
05360       if (buf == NULL)
05361         return bpaddr;
05362       buf_len = IT_SCAN_THRESHOLD;
05363 
05364       definite = 0;
05365       for (i = 0; i < buf_len - sizeof (buf) && ! definite; i += 2)
05366         {
05367           unsigned short inst1 = extract_unsigned_integer (&buf[i], 2, order);
05368           if (thumb_insn_size (inst1) == 2)
05369             {
05370               definite = 1;
05371               break;
05372             }
05373         }
05374 
05375       /* At this point, if DEFINITE, BUF[I] is the first place we
05376          are sure that we know the instruction boundaries, and it is far
05377          enough from BPADDR that we could not miss an IT instruction
05378          affecting BPADDR.  If ! DEFINITE, give up - start from a
05379          known boundary.  */
05380       if (! definite)
05381         {
05382           buf = extend_buffer_earlier (buf, bpaddr, buf_len,
05383                                        bpaddr - boundary);
05384           if (buf == NULL)
05385             return bpaddr;
05386           buf_len = bpaddr - boundary;
05387           i = 0;
05388         }
05389     }
05390   else
05391     {
05392       buf = extend_buffer_earlier (buf, bpaddr, buf_len, bpaddr - boundary);
05393       if (buf == NULL)
05394         return bpaddr;
05395       buf_len = bpaddr - boundary;
05396       i = 0;
05397     }
05398 
05399   /* Scan forwards.  Find the last IT instruction before BPADDR.  */
05400   last_it = -1;
05401   last_it_count = 0;
05402   while (i < buf_len)
05403     {
05404       unsigned short inst1 = extract_unsigned_integer (&buf[i], 2, order);
05405       last_it_count--;
05406       if ((inst1 & 0xff00) == 0xbf00 && (inst1 & 0x000f) != 0)
05407         {
05408           last_it = i;
05409           if (inst1 & 0x0001)
05410             last_it_count = 4;
05411           else if (inst1 & 0x0002)
05412             last_it_count = 3;
05413           else if (inst1 & 0x0004)
05414             last_it_count = 2;
05415           else
05416             last_it_count = 1;
05417         }
05418       i += thumb_insn_size (inst1);
05419     }
05420 
05421   xfree (buf);
05422 
05423   if (last_it == -1)
05424     /* There wasn't really an IT instruction after all.  */
05425     return bpaddr;
05426 
05427   if (last_it_count < 1)
05428     /* It was too far away.  */
05429     return bpaddr;
05430 
05431   /* This really is a trouble spot.  Move the breakpoint to the IT
05432      instruction.  */
05433   return bpaddr - buf_len + last_it;
05434 }
05435 
05436 /* ARM displaced stepping support.
05437 
05438    Generally ARM displaced stepping works as follows:
05439 
05440    1. When an instruction is to be single-stepped, it is first decoded by
05441       arm_process_displaced_insn (called from arm_displaced_step_copy_insn).
05442       Depending on the type of instruction, it is then copied to a scratch
05443       location, possibly in a modified form.  The copy_* set of functions
05444       performs such modification, as necessary.  A breakpoint is placed after
05445       the modified instruction in the scratch space to return control to GDB.
05446       Note in particular that instructions which modify the PC will no longer
05447       do so after modification.
05448 
05449    2. The instruction is single-stepped, by setting the PC to the scratch
05450       location address, and resuming.  Control returns to GDB when the
05451       breakpoint is hit.
05452 
05453    3. A cleanup function (cleanup_*) is called corresponding to the copy_*
05454       function used for the current instruction.  This function's job is to
05455       put the CPU/memory state back to what it would have been if the
05456       instruction had been executed unmodified in its original location.  */
05457 
05458 /* NOP instruction (mov r0, r0).  */
05459 #define ARM_NOP                         0xe1a00000
05460 #define THUMB_NOP 0x4600
05461 
05462 /* Helper for register reads for displaced stepping.  In particular, this
05463    returns the PC as it would be seen by the instruction at its original
05464    location.  */
05465 
05466 ULONGEST
05467 displaced_read_reg (struct regcache *regs, struct displaced_step_closure *dsc,
05468                     int regno)
05469 {
05470   ULONGEST ret;
05471   CORE_ADDR from = dsc->insn_addr;
05472 
05473   if (regno == ARM_PC_REGNUM)
05474     {
05475       /* Compute pipeline offset:
05476          - When executing an ARM instruction, PC reads as the address of the
05477          current instruction plus 8.
05478          - When executing a Thumb instruction, PC reads as the address of the
05479          current instruction plus 4.  */
05480 
05481       if (!dsc->is_thumb)
05482         from += 8;
05483       else
05484         from += 4;
05485 
05486       if (debug_displaced)
05487         fprintf_unfiltered (gdb_stdlog, "displaced: read pc value %.8lx\n",
05488                             (unsigned long) from);
05489       return (ULONGEST) from;
05490     }
05491   else
05492     {
05493       regcache_cooked_read_unsigned (regs, regno, &ret);
05494       if (debug_displaced)
05495         fprintf_unfiltered (gdb_stdlog, "displaced: read r%d value %.8lx\n",
05496                             regno, (unsigned long) ret);
05497       return ret;
05498     }
05499 }
05500 
05501 static int
05502 displaced_in_arm_mode (struct regcache *regs)
05503 {
05504   ULONGEST ps;
05505   ULONGEST t_bit = arm_psr_thumb_bit (get_regcache_arch (regs));
05506 
05507   regcache_cooked_read_unsigned (regs, ARM_PS_REGNUM, &ps);
05508 
05509   return (ps & t_bit) == 0;
05510 }
05511 
05512 /* Write to the PC as from a branch instruction.  */
05513 
05514 static void
05515 branch_write_pc (struct regcache *regs, struct displaced_step_closure *dsc,
05516                  ULONGEST val)
05517 {
05518   if (!dsc->is_thumb)
05519     /* Note: If bits 0/1 are set, this branch would be unpredictable for
05520        architecture versions < 6.  */
05521     regcache_cooked_write_unsigned (regs, ARM_PC_REGNUM,
05522                                     val & ~(ULONGEST) 0x3);
05523   else
05524     regcache_cooked_write_unsigned (regs, ARM_PC_REGNUM,
05525                                     val & ~(ULONGEST) 0x1);
05526 }
05527 
05528 /* Write to the PC as from a branch-exchange instruction.  */
05529 
05530 static void
05531 bx_write_pc (struct regcache *regs, ULONGEST val)
05532 {
05533   ULONGEST ps;
05534   ULONGEST t_bit = arm_psr_thumb_bit (get_regcache_arch (regs));
05535 
05536   regcache_cooked_read_unsigned (regs, ARM_PS_REGNUM, &ps);
05537 
05538   if ((val & 1) == 1)
05539     {
05540       regcache_cooked_write_unsigned (regs, ARM_PS_REGNUM, ps | t_bit);
05541       regcache_cooked_write_unsigned (regs, ARM_PC_REGNUM, val & 0xfffffffe);
05542     }
05543   else if ((val & 2) == 0)
05544     {
05545       regcache_cooked_write_unsigned (regs, ARM_PS_REGNUM, ps & ~t_bit);
05546       regcache_cooked_write_unsigned (regs, ARM_PC_REGNUM, val);
05547     }
05548   else
05549     {
05550       /* Unpredictable behaviour.  Try to do something sensible (switch to ARM
05551           mode, align dest to 4 bytes).  */
05552       warning (_("Single-stepping BX to non-word-aligned ARM instruction."));
05553       regcache_cooked_write_unsigned (regs, ARM_PS_REGNUM, ps & ~t_bit);
05554       regcache_cooked_write_unsigned (regs, ARM_PC_REGNUM, val & 0xfffffffc);
05555     }
05556 }
05557 
05558 /* Write to the PC as if from a load instruction.  */
05559 
05560 static void
05561 load_write_pc (struct regcache *regs, struct displaced_step_closure *dsc,
05562                ULONGEST val)
05563 {
05564   if (DISPLACED_STEPPING_ARCH_VERSION >= 5)
05565     bx_write_pc (regs, val);
05566   else
05567     branch_write_pc (regs, dsc, val);
05568 }
05569 
05570 /* Write to the PC as if from an ALU instruction.  */
05571 
05572 static void
05573 alu_write_pc (struct regcache *regs, struct displaced_step_closure *dsc,
05574               ULONGEST val)
05575 {
05576   if (DISPLACED_STEPPING_ARCH_VERSION >= 7 && !dsc->is_thumb)
05577     bx_write_pc (regs, val);
05578   else
05579     branch_write_pc (regs, dsc, val);
05580 }
05581 
05582 /* Helper for writing to registers for displaced stepping.  Writing to the PC
05583    has a varying effects depending on the instruction which does the write:
05584    this is controlled by the WRITE_PC argument.  */
05585 
05586 void
05587 displaced_write_reg (struct regcache *regs, struct displaced_step_closure *dsc,
05588                      int regno, ULONGEST val, enum pc_write_style write_pc)
05589 {
05590   if (regno == ARM_PC_REGNUM)
05591     {
05592       if (debug_displaced)
05593         fprintf_unfiltered (gdb_stdlog, "displaced: writing pc %.8lx\n",
05594                             (unsigned long) val);
05595       switch (write_pc)
05596         {
05597         case BRANCH_WRITE_PC:
05598           branch_write_pc (regs, dsc, val);
05599           break;
05600 
05601         case BX_WRITE_PC:
05602           bx_write_pc (regs, val);
05603           break;
05604 
05605         case LOAD_WRITE_PC:
05606           load_write_pc (regs, dsc, val);
05607           break;
05608 
05609         case ALU_WRITE_PC:
05610           alu_write_pc (regs, dsc, val);
05611           break;
05612 
05613         case CANNOT_WRITE_PC:
05614           warning (_("Instruction wrote to PC in an unexpected way when "
05615                      "single-stepping"));
05616           break;
05617 
05618         default:
05619           internal_error (__FILE__, __LINE__,
05620                           _("Invalid argument to displaced_write_reg"));
05621         }
05622 
05623       dsc->wrote_to_pc = 1;
05624     }
05625   else
05626     {
05627       if (debug_displaced)
05628         fprintf_unfiltered (gdb_stdlog, "displaced: writing r%d value %.8lx\n",
05629                             regno, (unsigned long) val);
05630       regcache_cooked_write_unsigned (regs, regno, val);
05631     }
05632 }
05633 
05634 /* This function is used to concisely determine if an instruction INSN
05635    references PC.  Register fields of interest in INSN should have the
05636    corresponding fields of BITMASK set to 0b1111.  The function
05637    returns return 1 if any of these fields in INSN reference the PC
05638    (also 0b1111, r15), else it returns 0.  */
05639 
05640 static int
05641 insn_references_pc (uint32_t insn, uint32_t bitmask)
05642 {
05643   uint32_t lowbit = 1;
05644 
05645   while (bitmask != 0)
05646     {
05647       uint32_t mask;
05648 
05649       for (; lowbit && (bitmask & lowbit) == 0; lowbit <<= 1)
05650         ;
05651 
05652       if (!lowbit)
05653         break;
05654 
05655       mask = lowbit * 0xf;
05656 
05657       if ((insn & mask) == mask)
05658         return 1;
05659 
05660       bitmask &= ~mask;
05661     }
05662 
05663   return 0;
05664 }
05665 
05666 /* The simplest copy function.  Many instructions have the same effect no
05667    matter what address they are executed at: in those cases, use this.  */
05668 
05669 static int
05670 arm_copy_unmodified (struct gdbarch *gdbarch, uint32_t insn,
05671                      const char *iname, struct displaced_step_closure *dsc)
05672 {
05673   if (debug_displaced)
05674     fprintf_unfiltered (gdb_stdlog, "displaced: copying insn %.8lx, "
05675                         "opcode/class '%s' unmodified\n", (unsigned long) insn,
05676                         iname);
05677 
05678   dsc->modinsn[0] = insn;
05679 
05680   return 0;
05681 }
05682 
05683 static int
05684 thumb_copy_unmodified_32bit (struct gdbarch *gdbarch, uint16_t insn1,
05685                              uint16_t insn2, const char *iname,
05686                              struct displaced_step_closure *dsc)
05687 {
05688   if (debug_displaced)
05689     fprintf_unfiltered (gdb_stdlog, "displaced: copying insn %.4x %.4x, "
05690                         "opcode/class '%s' unmodified\n", insn1, insn2,
05691                         iname);
05692 
05693   dsc->modinsn[0] = insn1;
05694   dsc->modinsn[1] = insn2;
05695   dsc->numinsns = 2;
05696 
05697   return 0;
05698 }
05699 
05700 /* Copy 16-bit Thumb(Thumb and 16-bit Thumb-2) instruction without any
05701    modification.  */
05702 static int
05703 thumb_copy_unmodified_16bit (struct gdbarch *gdbarch, unsigned int insn,
05704                              const char *iname,
05705                              struct displaced_step_closure *dsc)
05706 {
05707   if (debug_displaced)
05708     fprintf_unfiltered (gdb_stdlog, "displaced: copying insn %.4x, "
05709                         "opcode/class '%s' unmodified\n", insn,
05710                         iname);
05711 
05712   dsc->modinsn[0] = insn;
05713 
05714   return 0;
05715 }
05716 
05717 /* Preload instructions with immediate offset.  */
05718 
05719 static void
05720 cleanup_preload (struct gdbarch *gdbarch,
05721                  struct regcache *regs, struct displaced_step_closure *dsc)
05722 {
05723   displaced_write_reg (regs, dsc, 0, dsc->tmp[0], CANNOT_WRITE_PC);
05724   if (!dsc->u.preload.immed)
05725     displaced_write_reg (regs, dsc, 1, dsc->tmp[1], CANNOT_WRITE_PC);
05726 }
05727 
05728 static void
05729 install_preload (struct gdbarch *gdbarch, struct regcache *regs,
05730                  struct displaced_step_closure *dsc, unsigned int rn)
05731 {
05732   ULONGEST rn_val;
05733   /* Preload instructions:
05734 
05735      {pli/pld} [rn, #+/-imm]
05736      ->
05737      {pli/pld} [r0, #+/-imm].  */
05738 
05739   dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
05740   rn_val = displaced_read_reg (regs, dsc, rn);
05741   displaced_write_reg (regs, dsc, 0, rn_val, CANNOT_WRITE_PC);
05742   dsc->u.preload.immed = 1;
05743 
05744   dsc->cleanup = &cleanup_preload;
05745 }
05746 
05747 static int
05748 arm_copy_preload (struct gdbarch *gdbarch, uint32_t insn, struct regcache *regs,
05749                   struct displaced_step_closure *dsc)
05750 {
05751   unsigned int rn = bits (insn, 16, 19);
05752 
05753   if (!insn_references_pc (insn, 0x000f0000ul))
05754     return arm_copy_unmodified (gdbarch, insn, "preload", dsc);
05755 
05756   if (debug_displaced)
05757     fprintf_unfiltered (gdb_stdlog, "displaced: copying preload insn %.8lx\n",
05758                         (unsigned long) insn);
05759 
05760   dsc->modinsn[0] = insn & 0xfff0ffff;
05761 
05762   install_preload (gdbarch, regs, dsc, rn);
05763 
05764   return 0;
05765 }
05766 
05767 static int
05768 thumb2_copy_preload (struct gdbarch *gdbarch, uint16_t insn1, uint16_t insn2,
05769                      struct regcache *regs, struct displaced_step_closure *dsc)
05770 {
05771   unsigned int rn = bits (insn1, 0, 3);
05772   unsigned int u_bit = bit (insn1, 7);
05773   int imm12 = bits (insn2, 0, 11);
05774   ULONGEST pc_val;
05775 
05776   if (rn != ARM_PC_REGNUM)
05777     return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2, "preload", dsc);
05778 
05779   /* PC is only allowed to use in PLI (immediate,literal) Encoding T3, and
05780      PLD (literal) Encoding T1.  */
05781   if (debug_displaced)
05782     fprintf_unfiltered (gdb_stdlog,
05783                         "displaced: copying pld/pli pc (0x%x) %c imm12 %.4x\n",
05784                         (unsigned int) dsc->insn_addr, u_bit ? '+' : '-',
05785                         imm12);
05786 
05787   if (!u_bit)
05788     imm12 = -1 * imm12;
05789 
05790   /* Rewrite instruction {pli/pld} PC imm12 into:
05791      Prepare: tmp[0] <- r0, tmp[1] <- r1, r0 <- pc, r1 <- imm12
05792 
05793      {pli/pld} [r0, r1]
05794 
05795      Cleanup: r0 <- tmp[0], r1 <- tmp[1].  */
05796 
05797   dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
05798   dsc->tmp[1] = displaced_read_reg (regs, dsc, 1);
05799 
05800   pc_val = displaced_read_reg (regs, dsc, ARM_PC_REGNUM);
05801 
05802   displaced_write_reg (regs, dsc, 0, pc_val, CANNOT_WRITE_PC);
05803   displaced_write_reg (regs, dsc, 1, imm12, CANNOT_WRITE_PC);
05804   dsc->u.preload.immed = 0;
05805 
05806   /* {pli/pld} [r0, r1] */
05807   dsc->modinsn[0] = insn1 & 0xfff0;
05808   dsc->modinsn[1] = 0xf001;
05809   dsc->numinsns = 2;
05810 
05811   dsc->cleanup = &cleanup_preload;
05812   return 0;
05813 }
05814 
05815 /* Preload instructions with register offset.  */
05816 
05817 static void
05818 install_preload_reg(struct gdbarch *gdbarch, struct regcache *regs,
05819                     struct displaced_step_closure *dsc, unsigned int rn,
05820                     unsigned int rm)
05821 {
05822   ULONGEST rn_val, rm_val;
05823 
05824   /* Preload register-offset instructions:
05825 
05826      {pli/pld} [rn, rm {, shift}]
05827      ->
05828      {pli/pld} [r0, r1 {, shift}].  */
05829 
05830   dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
05831   dsc->tmp[1] = displaced_read_reg (regs, dsc, 1);
05832   rn_val = displaced_read_reg (regs, dsc, rn);
05833   rm_val = displaced_read_reg (regs, dsc, rm);
05834   displaced_write_reg (regs, dsc, 0, rn_val, CANNOT_WRITE_PC);
05835   displaced_write_reg (regs, dsc, 1, rm_val, CANNOT_WRITE_PC);
05836   dsc->u.preload.immed = 0;
05837 
05838   dsc->cleanup = &cleanup_preload;
05839 }
05840 
05841 static int
05842 arm_copy_preload_reg (struct gdbarch *gdbarch, uint32_t insn,
05843                       struct regcache *regs,
05844                       struct displaced_step_closure *dsc)
05845 {
05846   unsigned int rn = bits (insn, 16, 19);
05847   unsigned int rm = bits (insn, 0, 3);
05848 
05849 
05850   if (!insn_references_pc (insn, 0x000f000ful))
05851     return arm_copy_unmodified (gdbarch, insn, "preload reg", dsc);
05852 
05853   if (debug_displaced)
05854     fprintf_unfiltered (gdb_stdlog, "displaced: copying preload insn %.8lx\n",
05855                         (unsigned long) insn);
05856 
05857   dsc->modinsn[0] = (insn & 0xfff0fff0) | 0x1;
05858 
05859   install_preload_reg (gdbarch, regs, dsc, rn, rm);
05860   return 0;
05861 }
05862 
05863 /* Copy/cleanup coprocessor load and store instructions.  */
05864 
05865 static void
05866 cleanup_copro_load_store (struct gdbarch *gdbarch,
05867                           struct regcache *regs,
05868                           struct displaced_step_closure *dsc)
05869 {
05870   ULONGEST rn_val = displaced_read_reg (regs, dsc, 0);
05871 
05872   displaced_write_reg (regs, dsc, 0, dsc->tmp[0], CANNOT_WRITE_PC);
05873 
05874   if (dsc->u.ldst.writeback)
05875     displaced_write_reg (regs, dsc, dsc->u.ldst.rn, rn_val, LOAD_WRITE_PC);
05876 }
05877 
05878 static void
05879 install_copro_load_store (struct gdbarch *gdbarch, struct regcache *regs,
05880                           struct displaced_step_closure *dsc,
05881                           int writeback, unsigned int rn)
05882 {
05883   ULONGEST rn_val;
05884 
05885   /* Coprocessor load/store instructions:
05886 
05887      {stc/stc2} [<Rn>, #+/-imm]  (and other immediate addressing modes)
05888      ->
05889      {stc/stc2} [r0, #+/-imm].
05890 
05891      ldc/ldc2 are handled identically.  */
05892 
05893   dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
05894   rn_val = displaced_read_reg (regs, dsc, rn);
05895   /* PC should be 4-byte aligned.  */
05896   rn_val = rn_val & 0xfffffffc;
05897   displaced_write_reg (regs, dsc, 0, rn_val, CANNOT_WRITE_PC);
05898 
05899   dsc->u.ldst.writeback = writeback;
05900   dsc->u.ldst.rn = rn;
05901 
05902   dsc->cleanup = &cleanup_copro_load_store;
05903 }
05904 
05905 static int
05906 arm_copy_copro_load_store (struct gdbarch *gdbarch, uint32_t insn,
05907                            struct regcache *regs,
05908                            struct displaced_step_closure *dsc)
05909 {
05910   unsigned int rn = bits (insn, 16, 19);
05911 
05912   if (!insn_references_pc (insn, 0x000f0000ul))
05913     return arm_copy_unmodified (gdbarch, insn, "copro load/store", dsc);
05914 
05915   if (debug_displaced)
05916     fprintf_unfiltered (gdb_stdlog, "displaced: copying coprocessor "
05917                         "load/store insn %.8lx\n", (unsigned long) insn);
05918 
05919   dsc->modinsn[0] = insn & 0xfff0ffff;
05920 
05921   install_copro_load_store (gdbarch, regs, dsc, bit (insn, 25), rn);
05922 
05923   return 0;
05924 }
05925 
05926 static int
05927 thumb2_copy_copro_load_store (struct gdbarch *gdbarch, uint16_t insn1,
05928                               uint16_t insn2, struct regcache *regs,
05929                               struct displaced_step_closure *dsc)
05930 {
05931   unsigned int rn = bits (insn1, 0, 3);
05932 
05933   if (rn != ARM_PC_REGNUM)
05934     return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
05935                                         "copro load/store", dsc);
05936 
05937   if (debug_displaced)
05938     fprintf_unfiltered (gdb_stdlog, "displaced: copying coprocessor "
05939                         "load/store insn %.4x%.4x\n", insn1, insn2);
05940 
05941   dsc->modinsn[0] = insn1 & 0xfff0;
05942   dsc->modinsn[1] = insn2;
05943   dsc->numinsns = 2;
05944 
05945   /* This function is called for copying instruction LDC/LDC2/VLDR, which
05946      doesn't support writeback, so pass 0.  */
05947   install_copro_load_store (gdbarch, regs, dsc, 0, rn);
05948 
05949   return 0;
05950 }
05951 
05952 /* Clean up branch instructions (actually perform the branch, by setting
05953    PC).  */
05954 
05955 static void
05956 cleanup_branch (struct gdbarch *gdbarch, struct regcache *regs,
05957                 struct displaced_step_closure *dsc)
05958 {
05959   uint32_t status = displaced_read_reg (regs, dsc, ARM_PS_REGNUM);
05960   int branch_taken = condition_true (dsc->u.branch.cond, status);
05961   enum pc_write_style write_pc = dsc->u.branch.exchange
05962                                  ? BX_WRITE_PC : BRANCH_WRITE_PC;
05963 
05964   if (!branch_taken)
05965     return;
05966 
05967   if (dsc->u.branch.link)
05968     {
05969       /* The value of LR should be the next insn of current one.  In order
05970        not to confuse logic hanlding later insn `bx lr', if current insn mode
05971        is Thumb, the bit 0 of LR value should be set to 1.  */
05972       ULONGEST next_insn_addr = dsc->insn_addr + dsc->insn_size;
05973 
05974       if (dsc->is_thumb)
05975         next_insn_addr |= 0x1;
05976 
05977       displaced_write_reg (regs, dsc, ARM_LR_REGNUM, next_insn_addr,
05978                            CANNOT_WRITE_PC);
05979     }
05980 
05981   displaced_write_reg (regs, dsc, ARM_PC_REGNUM, dsc->u.branch.dest, write_pc);
05982 }
05983 
05984 /* Copy B/BL/BLX instructions with immediate destinations.  */
05985 
05986 static void
05987 install_b_bl_blx (struct gdbarch *gdbarch, struct regcache *regs,
05988                   struct displaced_step_closure *dsc,
05989                   unsigned int cond, int exchange, int link, long offset)
05990 {
05991   /* Implement "BL<cond> <label>" as:
05992 
05993      Preparation: cond <- instruction condition
05994      Insn: mov r0, r0  (nop)
05995      Cleanup: if (condition true) { r14 <- pc; pc <- label }.
05996 
05997      B<cond> similar, but don't set r14 in cleanup.  */
05998 
05999   dsc->u.branch.cond = cond;
06000   dsc->u.branch.link = link;
06001   dsc->u.branch.exchange = exchange;
06002 
06003   dsc->u.branch.dest = dsc->insn_addr;
06004   if (link && exchange)
06005     /* For BLX, offset is computed from the Align (PC, 4).  */
06006     dsc->u.branch.dest = dsc->u.branch.dest & 0xfffffffc;
06007 
06008   if (dsc->is_thumb)
06009     dsc->u.branch.dest += 4 + offset;
06010   else
06011     dsc->u.branch.dest += 8 + offset;
06012 
06013   dsc->cleanup = &cleanup_branch;
06014 }
06015 static int
06016 arm_copy_b_bl_blx (struct gdbarch *gdbarch, uint32_t insn,
06017                    struct regcache *regs, struct displaced_step_closure *dsc)
06018 {
06019   unsigned int cond = bits (insn, 28, 31);
06020   int exchange = (cond == 0xf);
06021   int link = exchange || bit (insn, 24);
06022   long offset;
06023 
06024   if (debug_displaced)
06025     fprintf_unfiltered (gdb_stdlog, "displaced: copying %s immediate insn "
06026                         "%.8lx\n", (exchange) ? "blx" : (link) ? "bl" : "b",
06027                         (unsigned long) insn);
06028   if (exchange)
06029     /* For BLX, set bit 0 of the destination.  The cleanup_branch function will
06030        then arrange the switch into Thumb mode.  */
06031     offset = (bits (insn, 0, 23) << 2) | (bit (insn, 24) << 1) | 1;
06032   else
06033     offset = bits (insn, 0, 23) << 2;
06034 
06035   if (bit (offset, 25))
06036     offset = offset | ~0x3ffffff;
06037 
06038   dsc->modinsn[0] = ARM_NOP;
06039 
06040   install_b_bl_blx (gdbarch, regs, dsc, cond, exchange, link, offset);
06041   return 0;
06042 }
06043 
06044 static int
06045 thumb2_copy_b_bl_blx (struct gdbarch *gdbarch, uint16_t insn1,
06046                       uint16_t insn2, struct regcache *regs,
06047                       struct displaced_step_closure *dsc)
06048 {
06049   int link = bit (insn2, 14);
06050   int exchange = link && !bit (insn2, 12);
06051   int cond = INST_AL;
06052   long offset = 0;
06053   int j1 = bit (insn2, 13);
06054   int j2 = bit (insn2, 11);
06055   int s = sbits (insn1, 10, 10);
06056   int i1 = !(j1 ^ bit (insn1, 10));
06057   int i2 = !(j2 ^ bit (insn1, 10));
06058 
06059   if (!link && !exchange) /* B */
06060     {
06061       offset = (bits (insn2, 0, 10) << 1);
06062       if (bit (insn2, 12)) /* Encoding T4 */
06063         {
06064           offset |= (bits (insn1, 0, 9) << 12)
06065             | (i2 << 22)
06066             | (i1 << 23)
06067             | (s << 24);
06068           cond = INST_AL;
06069         }
06070       else /* Encoding T3 */
06071         {
06072           offset |= (bits (insn1, 0, 5) << 12)
06073             | (j1 << 18)
06074             | (j2 << 19)
06075             | (s << 20);
06076           cond = bits (insn1, 6, 9);
06077         }
06078     }
06079   else
06080     {
06081       offset = (bits (insn1, 0, 9) << 12);
06082       offset |= ((i2 << 22) | (i1 << 23) | (s << 24));
06083       offset |= exchange ?
06084         (bits (insn2, 1, 10) << 2) : (bits (insn2, 0, 10) << 1);
06085     }
06086 
06087   if (debug_displaced)
06088     fprintf_unfiltered (gdb_stdlog, "displaced: copying %s insn "
06089                         "%.4x %.4x with offset %.8lx\n",
06090                         link ? (exchange) ? "blx" : "bl" : "b",
06091                         insn1, insn2, offset);
06092 
06093   dsc->modinsn[0] = THUMB_NOP;
06094 
06095   install_b_bl_blx (gdbarch, regs, dsc, cond, exchange, link, offset);
06096   return 0;
06097 }
06098 
06099 /* Copy B Thumb instructions.  */
06100 static int
06101 thumb_copy_b (struct gdbarch *gdbarch, unsigned short insn,
06102               struct displaced_step_closure *dsc)
06103 {
06104   unsigned int cond = 0;
06105   int offset = 0;
06106   unsigned short bit_12_15 = bits (insn, 12, 15);
06107   CORE_ADDR from = dsc->insn_addr;
06108 
06109   if (bit_12_15 == 0xd)
06110     {
06111       /* offset = SignExtend (imm8:0, 32) */
06112       offset = sbits ((insn << 1), 0, 8);
06113       cond = bits (insn, 8, 11);
06114     }
06115   else if (bit_12_15 == 0xe) /* Encoding T2 */
06116     {
06117       offset = sbits ((insn << 1), 0, 11);
06118       cond = INST_AL;
06119     }
06120 
06121   if (debug_displaced)
06122     fprintf_unfiltered (gdb_stdlog,
06123                         "displaced: copying b immediate insn %.4x "
06124                         "with offset %d\n", insn, offset);
06125 
06126   dsc->u.branch.cond = cond;
06127   dsc->u.branch.link = 0;
06128   dsc->u.branch.exchange = 0;
06129   dsc->u.branch.dest = from + 4 + offset;
06130 
06131   dsc->modinsn[0] = THUMB_NOP;
06132 
06133   dsc->cleanup = &cleanup_branch;
06134 
06135   return 0;
06136 }
06137 
06138 /* Copy BX/BLX with register-specified destinations.  */
06139 
06140 static void
06141 install_bx_blx_reg (struct gdbarch *gdbarch, struct regcache *regs,
06142                     struct displaced_step_closure *dsc, int link,
06143                     unsigned int cond, unsigned int rm)
06144 {
06145   /* Implement {BX,BLX}<cond> <reg>" as:
06146 
06147      Preparation: cond <- instruction condition
06148      Insn: mov r0, r0 (nop)
06149      Cleanup: if (condition true) { r14 <- pc; pc <- dest; }.
06150 
06151      Don't set r14 in cleanup for BX.  */
06152 
06153   dsc->u.branch.dest = displaced_read_reg (regs, dsc, rm);
06154 
06155   dsc->u.branch.cond = cond;
06156   dsc->u.branch.link = link;
06157 
06158   dsc->u.branch.exchange = 1;
06159 
06160   dsc->cleanup = &cleanup_branch;
06161 }
06162 
06163 static int
06164 arm_copy_bx_blx_reg (struct gdbarch *gdbarch, uint32_t insn,
06165                      struct regcache *regs, struct displaced_step_closure *dsc)
06166 {
06167   unsigned int cond = bits (insn, 28, 31);
06168   /* BX:  x12xxx1x
06169      BLX: x12xxx3x.  */
06170   int link = bit (insn, 5);
06171   unsigned int rm = bits (insn, 0, 3);
06172 
06173   if (debug_displaced)
06174     fprintf_unfiltered (gdb_stdlog, "displaced: copying insn %.8lx",
06175                         (unsigned long) insn);
06176 
06177   dsc->modinsn[0] = ARM_NOP;
06178 
06179   install_bx_blx_reg (gdbarch, regs, dsc, link, cond, rm);
06180   return 0;
06181 }
06182 
06183 static int
06184 thumb_copy_bx_blx_reg (struct gdbarch *gdbarch, uint16_t insn,
06185                        struct regcache *regs,
06186                        struct displaced_step_closure *dsc)
06187 {
06188   int link = bit (insn, 7);
06189   unsigned int rm = bits (insn, 3, 6);
06190 
06191   if (debug_displaced)
06192     fprintf_unfiltered (gdb_stdlog, "displaced: copying insn %.4x",
06193                         (unsigned short) insn);
06194 
06195   dsc->modinsn[0] = THUMB_NOP;
06196 
06197   install_bx_blx_reg (gdbarch, regs, dsc, link, INST_AL, rm);
06198 
06199   return 0;
06200 }
06201 
06202 
06203 /* Copy/cleanup arithmetic/logic instruction with immediate RHS.  */
06204 
06205 static void
06206 cleanup_alu_imm (struct gdbarch *gdbarch,
06207                  struct regcache *regs, struct displaced_step_closure *dsc)
06208 {
06209   ULONGEST rd_val = displaced_read_reg (regs, dsc, 0);
06210   displaced_write_reg (regs, dsc, 0, dsc->tmp[0], CANNOT_WRITE_PC);
06211   displaced_write_reg (regs, dsc, 1, dsc->tmp[1], CANNOT_WRITE_PC);
06212   displaced_write_reg (regs, dsc, dsc->rd, rd_val, ALU_WRITE_PC);
06213 }
06214 
06215 static int
06216 arm_copy_alu_imm (struct gdbarch *gdbarch, uint32_t insn, struct regcache *regs,
06217                   struct displaced_step_closure *dsc)
06218 {
06219   unsigned int rn = bits (insn, 16, 19);
06220   unsigned int rd = bits (insn, 12, 15);
06221   unsigned int op = bits (insn, 21, 24);
06222   int is_mov = (op == 0xd);
06223   ULONGEST rd_val, rn_val;
06224 
06225   if (!insn_references_pc (insn, 0x000ff000ul))
06226     return arm_copy_unmodified (gdbarch, insn, "ALU immediate", dsc);
06227 
06228   if (debug_displaced)
06229     fprintf_unfiltered (gdb_stdlog, "displaced: copying immediate %s insn "
06230                         "%.8lx\n", is_mov ? "move" : "ALU",
06231                         (unsigned long) insn);
06232 
06233   /* Instruction is of form:
06234 
06235      <op><cond> rd, [rn,] #imm
06236 
06237      Rewrite as:
06238 
06239      Preparation: tmp1, tmp2 <- r0, r1;
06240                   r0, r1 <- rd, rn
06241      Insn: <op><cond> r0, r1, #imm
06242      Cleanup: rd <- r0; r0 <- tmp1; r1 <- tmp2
06243   */
06244 
06245   dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
06246   dsc->tmp[1] = displaced_read_reg (regs, dsc, 1);
06247   rn_val = displaced_read_reg (regs, dsc, rn);
06248   rd_val = displaced_read_reg (regs, dsc, rd);
06249   displaced_write_reg (regs, dsc, 0, rd_val, CANNOT_WRITE_PC);
06250   displaced_write_reg (regs, dsc, 1, rn_val, CANNOT_WRITE_PC);
06251   dsc->rd = rd;
06252 
06253   if (is_mov)
06254     dsc->modinsn[0] = insn & 0xfff00fff;
06255   else
06256     dsc->modinsn[0] = (insn & 0xfff00fff) | 0x10000;
06257 
06258   dsc->cleanup = &cleanup_alu_imm;
06259 
06260   return 0;
06261 }
06262 
06263 static int
06264 thumb2_copy_alu_imm (struct gdbarch *gdbarch, uint16_t insn1,
06265                      uint16_t insn2, struct regcache *regs,
06266                      struct displaced_step_closure *dsc)
06267 {
06268   unsigned int op = bits (insn1, 5, 8);
06269   unsigned int rn, rm, rd;
06270   ULONGEST rd_val, rn_val;
06271 
06272   rn = bits (insn1, 0, 3); /* Rn */
06273   rm = bits (insn2, 0, 3); /* Rm */
06274   rd = bits (insn2, 8, 11); /* Rd */
06275 
06276   /* This routine is only called for instruction MOV.  */
06277   gdb_assert (op == 0x2 && rn == 0xf);
06278 
06279   if (rm != ARM_PC_REGNUM && rd != ARM_PC_REGNUM)
06280     return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2, "ALU imm", dsc);
06281 
06282   if (debug_displaced)
06283     fprintf_unfiltered (gdb_stdlog, "displaced: copying reg %s insn %.4x%.4x\n",
06284                         "ALU", insn1, insn2);
06285 
06286   /* Instruction is of form:
06287 
06288      <op><cond> rd, [rn,] #imm
06289 
06290      Rewrite as:
06291 
06292      Preparation: tmp1, tmp2 <- r0, r1;
06293                   r0, r1 <- rd, rn
06294      Insn: <op><cond> r0, r1, #imm
06295      Cleanup: rd <- r0; r0 <- tmp1; r1 <- tmp2
06296   */
06297 
06298   dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
06299   dsc->tmp[1] = displaced_read_reg (regs, dsc, 1);
06300   rn_val = displaced_read_reg (regs, dsc, rn);
06301   rd_val = displaced_read_reg (regs, dsc, rd);
06302   displaced_write_reg (regs, dsc, 0, rd_val, CANNOT_WRITE_PC);
06303   displaced_write_reg (regs, dsc, 1, rn_val, CANNOT_WRITE_PC);
06304   dsc->rd = rd;
06305 
06306   dsc->modinsn[0] = insn1;
06307   dsc->modinsn[1] = ((insn2 & 0xf0f0) | 0x1);
06308   dsc->numinsns = 2;
06309 
06310   dsc->cleanup = &cleanup_alu_imm;
06311 
06312   return 0;
06313 }
06314 
06315 /* Copy/cleanup arithmetic/logic insns with register RHS.  */
06316 
06317 static void
06318 cleanup_alu_reg (struct gdbarch *gdbarch,
06319                  struct regcache *regs, struct displaced_step_closure *dsc)
06320 {
06321   ULONGEST rd_val;
06322   int i;
06323 
06324   rd_val = displaced_read_reg (regs, dsc, 0);
06325 
06326   for (i = 0; i < 3; i++)
06327     displaced_write_reg (regs, dsc, i, dsc->tmp[i], CANNOT_WRITE_PC);
06328 
06329   displaced_write_reg (regs, dsc, dsc->rd, rd_val, ALU_WRITE_PC);
06330 }
06331 
06332 static void
06333 install_alu_reg (struct gdbarch *gdbarch, struct regcache *regs,
06334                  struct displaced_step_closure *dsc,
06335                  unsigned int rd, unsigned int rn, unsigned int rm)
06336 {
06337   ULONGEST rd_val, rn_val, rm_val;
06338 
06339   /* Instruction is of form:
06340 
06341      <op><cond> rd, [rn,] rm [, <shift>]
06342 
06343      Rewrite as:
06344 
06345      Preparation: tmp1, tmp2, tmp3 <- r0, r1, r2;
06346                   r0, r1, r2 <- rd, rn, rm
06347      Insn: <op><cond> r0, r1, r2 [, <shift>]
06348      Cleanup: rd <- r0; r0, r1, r2 <- tmp1, tmp2, tmp3
06349   */
06350 
06351   dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
06352   dsc->tmp[1] = displaced_read_reg (regs, dsc, 1);
06353   dsc->tmp[2] = displaced_read_reg (regs, dsc, 2);
06354   rd_val = displaced_read_reg (regs, dsc, rd);
06355   rn_val = displaced_read_reg (regs, dsc, rn);
06356   rm_val = displaced_read_reg (regs, dsc, rm);
06357   displaced_write_reg (regs, dsc, 0, rd_val, CANNOT_WRITE_PC);
06358   displaced_write_reg (regs, dsc, 1, rn_val, CANNOT_WRITE_PC);
06359   displaced_write_reg (regs, dsc, 2, rm_val, CANNOT_WRITE_PC);
06360   dsc->rd = rd;
06361 
06362   dsc->cleanup = &cleanup_alu_reg;
06363 }
06364 
06365 static int
06366 arm_copy_alu_reg (struct gdbarch *gdbarch, uint32_t insn, struct regcache *regs,
06367                   struct displaced_step_closure *dsc)
06368 {
06369   unsigned int op = bits (insn, 21, 24);
06370   int is_mov = (op == 0xd);
06371 
06372   if (!insn_references_pc (insn, 0x000ff00ful))
06373     return arm_copy_unmodified (gdbarch, insn, "ALU reg", dsc);
06374 
06375   if (debug_displaced)
06376     fprintf_unfiltered (gdb_stdlog, "displaced: copying reg %s insn %.8lx\n",
06377                         is_mov ? "move" : "ALU", (unsigned long) insn);
06378 
06379   if (is_mov)
06380     dsc->modinsn[0] = (insn & 0xfff00ff0) | 0x2;
06381   else
06382     dsc->modinsn[0] = (insn & 0xfff00ff0) | 0x10002;
06383 
06384   install_alu_reg (gdbarch, regs, dsc, bits (insn, 12, 15), bits (insn, 16, 19),
06385                    bits (insn, 0, 3));
06386   return 0;
06387 }
06388 
06389 static int
06390 thumb_copy_alu_reg (struct gdbarch *gdbarch, uint16_t insn,
06391                     struct regcache *regs,
06392                     struct displaced_step_closure *dsc)
06393 {
06394   unsigned rn, rm, rd;
06395 
06396   rd = bits (insn, 3, 6);
06397   rn = (bit (insn, 7) << 3) | bits (insn, 0, 2);
06398   rm = 2;
06399 
06400   if (rd != ARM_PC_REGNUM && rn != ARM_PC_REGNUM)
06401     return thumb_copy_unmodified_16bit (gdbarch, insn, "ALU reg", dsc);
06402 
06403   if (debug_displaced)
06404     fprintf_unfiltered (gdb_stdlog, "displaced: copying reg %s insn %.4x\n",
06405                         "ALU", (unsigned short) insn);
06406 
06407   dsc->modinsn[0] = ((insn & 0xff00) | 0x08);
06408 
06409   install_alu_reg (gdbarch, regs, dsc, rd, rn, rm);
06410 
06411   return 0;
06412 }
06413 
06414 /* Cleanup/copy arithmetic/logic insns with shifted register RHS.  */
06415 
06416 static void
06417 cleanup_alu_shifted_reg (struct gdbarch *gdbarch,
06418                          struct regcache *regs,
06419                          struct displaced_step_closure *dsc)
06420 {
06421   ULONGEST rd_val = displaced_read_reg (regs, dsc, 0);
06422   int i;
06423 
06424   for (i = 0; i < 4; i++)
06425     displaced_write_reg (regs, dsc, i, dsc->tmp[i], CANNOT_WRITE_PC);
06426 
06427   displaced_write_reg (regs, dsc, dsc->rd, rd_val, ALU_WRITE_PC);
06428 }
06429 
06430 static void
06431 install_alu_shifted_reg (struct gdbarch *gdbarch, struct regcache *regs,
06432                          struct displaced_step_closure *dsc,
06433                          unsigned int rd, unsigned int rn, unsigned int rm,
06434                          unsigned rs)
06435 {
06436   int i;
06437   ULONGEST rd_val, rn_val, rm_val, rs_val;
06438 
06439   /* Instruction is of form:
06440 
06441      <op><cond> rd, [rn,] rm, <shift> rs
06442 
06443      Rewrite as:
06444 
06445      Preparation: tmp1, tmp2, tmp3, tmp4 <- r0, r1, r2, r3
06446                   r0, r1, r2, r3 <- rd, rn, rm, rs
06447      Insn: <op><cond> r0, r1, r2, <shift> r3
06448      Cleanup: tmp5 <- r0
06449               r0, r1, r2, r3 <- tmp1, tmp2, tmp3, tmp4
06450               rd <- tmp5
06451   */
06452 
06453   for (i = 0; i < 4; i++)
06454     dsc->tmp[i] = displaced_read_reg (regs, dsc, i);
06455 
06456   rd_val = displaced_read_reg (regs, dsc, rd);
06457   rn_val = displaced_read_reg (regs, dsc, rn);
06458   rm_val = displaced_read_reg (regs, dsc, rm);
06459   rs_val = displaced_read_reg (regs, dsc, rs);
06460   displaced_write_reg (regs, dsc, 0, rd_val, CANNOT_WRITE_PC);
06461   displaced_write_reg (regs, dsc, 1, rn_val, CANNOT_WRITE_PC);
06462   displaced_write_reg (regs, dsc, 2, rm_val, CANNOT_WRITE_PC);
06463   displaced_write_reg (regs, dsc, 3, rs_val, CANNOT_WRITE_PC);
06464   dsc->rd = rd;
06465   dsc->cleanup = &cleanup_alu_shifted_reg;
06466 }
06467 
06468 static int
06469 arm_copy_alu_shifted_reg (struct gdbarch *gdbarch, uint32_t insn,
06470                           struct regcache *regs,
06471                           struct displaced_step_closure *dsc)
06472 {
06473   unsigned int op = bits (insn, 21, 24);
06474   int is_mov = (op == 0xd);
06475   unsigned int rd, rn, rm, rs;
06476 
06477   if (!insn_references_pc (insn, 0x000fff0ful))
06478     return arm_copy_unmodified (gdbarch, insn, "ALU shifted reg", dsc);
06479 
06480   if (debug_displaced)
06481     fprintf_unfiltered (gdb_stdlog, "displaced: copying shifted reg %s insn "
06482                         "%.8lx\n", is_mov ? "move" : "ALU",
06483                         (unsigned long) insn);
06484 
06485   rn = bits (insn, 16, 19);
06486   rm = bits (insn, 0, 3);
06487   rs = bits (insn, 8, 11);
06488   rd = bits (insn, 12, 15);
06489 
06490   if (is_mov)
06491     dsc->modinsn[0] = (insn & 0xfff000f0) | 0x302;
06492   else
06493     dsc->modinsn[0] = (insn & 0xfff000f0) | 0x10302;
06494 
06495   install_alu_shifted_reg (gdbarch, regs, dsc, rd, rn, rm, rs);
06496 
06497   return 0;
06498 }
06499 
06500 /* Clean up load instructions.  */
06501 
06502 static void
06503 cleanup_load (struct gdbarch *gdbarch, struct regcache *regs,
06504               struct displaced_step_closure *dsc)
06505 {
06506   ULONGEST rt_val, rt_val2 = 0, rn_val;
06507 
06508   rt_val = displaced_read_reg (regs, dsc, 0);
06509   if (dsc->u.ldst.xfersize == 8)
06510     rt_val2 = displaced_read_reg (regs, dsc, 1);
06511   rn_val = displaced_read_reg (regs, dsc, 2);
06512 
06513   displaced_write_reg (regs, dsc, 0, dsc->tmp[0], CANNOT_WRITE_PC);
06514   if (dsc->u.ldst.xfersize > 4)
06515     displaced_write_reg (regs, dsc, 1, dsc->tmp[1], CANNOT_WRITE_PC);
06516   displaced_write_reg (regs, dsc, 2, dsc->tmp[2], CANNOT_WRITE_PC);
06517   if (!dsc->u.ldst.immed)
06518     displaced_write_reg (regs, dsc, 3, dsc->tmp[3], CANNOT_WRITE_PC);
06519 
06520   /* Handle register writeback.  */
06521   if (dsc->u.ldst.writeback)
06522     displaced_write_reg (regs, dsc, dsc->u.ldst.rn, rn_val, CANNOT_WRITE_PC);
06523   /* Put result in right place.  */
06524   displaced_write_reg (regs, dsc, dsc->rd, rt_val, LOAD_WRITE_PC);
06525   if (dsc->u.ldst.xfersize == 8)
06526     displaced_write_reg (regs, dsc, dsc->rd + 1, rt_val2, LOAD_WRITE_PC);
06527 }
06528 
06529 /* Clean up store instructions.  */
06530 
06531 static void
06532 cleanup_store (struct gdbarch *gdbarch, struct regcache *regs,
06533                struct displaced_step_closure *dsc)
06534 {
06535   ULONGEST rn_val = displaced_read_reg (regs, dsc, 2);
06536 
06537   displaced_write_reg (regs, dsc, 0, dsc->tmp[0], CANNOT_WRITE_PC);
06538   if (dsc->u.ldst.xfersize > 4)
06539     displaced_write_reg (regs, dsc, 1, dsc->tmp[1], CANNOT_WRITE_PC);
06540   displaced_write_reg (regs, dsc, 2, dsc->tmp[2], CANNOT_WRITE_PC);
06541   if (!dsc->u.ldst.immed)
06542     displaced_write_reg (regs, dsc, 3, dsc->tmp[3], CANNOT_WRITE_PC);
06543   if (!dsc->u.ldst.restore_r4)
06544     displaced_write_reg (regs, dsc, 4, dsc->tmp[4], CANNOT_WRITE_PC);
06545 
06546   /* Writeback.  */
06547   if (dsc->u.ldst.writeback)
06548     displaced_write_reg (regs, dsc, dsc->u.ldst.rn, rn_val, CANNOT_WRITE_PC);
06549 }
06550 
06551 /* Copy "extra" load/store instructions.  These are halfword/doubleword
06552    transfers, which have a different encoding to byte/word transfers.  */
06553 
06554 static int
06555 arm_copy_extra_ld_st (struct gdbarch *gdbarch, uint32_t insn, int unpriveleged,
06556                       struct regcache *regs, struct displaced_step_closure *dsc)
06557 {
06558   unsigned int op1 = bits (insn, 20, 24);
06559   unsigned int op2 = bits (insn, 5, 6);
06560   unsigned int rt = bits (insn, 12, 15);
06561   unsigned int rn = bits (insn, 16, 19);
06562   unsigned int rm = bits (insn, 0, 3);
06563   char load[12]     = {0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1};
06564   char bytesize[12] = {2, 2, 2, 2, 8, 1, 8, 1, 8, 2, 8, 2};
06565   int immed = (op1 & 0x4) != 0;
06566   int opcode;
06567   ULONGEST rt_val, rt_val2 = 0, rn_val, rm_val = 0;
06568 
06569   if (!insn_references_pc (insn, 0x000ff00ful))
06570     return arm_copy_unmodified (gdbarch, insn, "extra load/store", dsc);
06571 
06572   if (debug_displaced)
06573     fprintf_unfiltered (gdb_stdlog, "displaced: copying %sextra load/store "
06574                         "insn %.8lx\n", unpriveleged ? "unpriveleged " : "",
06575                         (unsigned long) insn);
06576 
06577   opcode = ((op2 << 2) | (op1 & 0x1) | ((op1 & 0x4) >> 1)) - 4;
06578 
06579   if (opcode < 0)
06580     internal_error (__FILE__, __LINE__,
06581                     _("copy_extra_ld_st: instruction decode error"));
06582 
06583   dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
06584   dsc->tmp[1] = displaced_read_reg (regs, dsc, 1);
06585   dsc->tmp[2] = displaced_read_reg (regs, dsc, 2);
06586   if (!immed)
06587     dsc->tmp[3] = displaced_read_reg (regs, dsc, 3);
06588 
06589   rt_val = displaced_read_reg (regs, dsc, rt);
06590   if (bytesize[opcode] == 8)
06591     rt_val2 = displaced_read_reg (regs, dsc, rt + 1);
06592   rn_val = displaced_read_reg (regs, dsc, rn);
06593   if (!immed)
06594     rm_val = displaced_read_reg (regs, dsc, rm);
06595 
06596   displaced_write_reg (regs, dsc, 0, rt_val, CANNOT_WRITE_PC);
06597   if (bytesize[opcode] == 8)
06598     displaced_write_reg (regs, dsc, 1, rt_val2, CANNOT_WRITE_PC);
06599   displaced_write_reg (regs, dsc, 2, rn_val, CANNOT_WRITE_PC);
06600   if (!immed)
06601     displaced_write_reg (regs, dsc, 3, rm_val, CANNOT_WRITE_PC);
06602 
06603   dsc->rd = rt;
06604   dsc->u.ldst.xfersize = bytesize[opcode];
06605   dsc->u.ldst.rn = rn;
06606   dsc->u.ldst.immed = immed;
06607   dsc->u.ldst.writeback = bit (insn, 24) == 0 || bit (insn, 21) != 0;
06608   dsc->u.ldst.restore_r4 = 0;
06609 
06610   if (immed)
06611     /* {ldr,str}<width><cond> rt, [rt2,] [rn, #imm]
06612         ->
06613        {ldr,str}<width><cond> r0, [r1,] [r2, #imm].  */
06614     dsc->modinsn[0] = (insn & 0xfff00fff) | 0x20000;
06615   else
06616     /* {ldr,str}<width><cond> rt, [rt2,] [rn, +/-rm]
06617         ->
06618        {ldr,str}<width><cond> r0, [r1,] [r2, +/-r3].  */
06619     dsc->modinsn[0] = (insn & 0xfff00ff0) | 0x20003;
06620 
06621   dsc->cleanup = load[opcode] ? &cleanup_load : &cleanup_store;
06622 
06623   return 0;
06624 }
06625 
06626 /* Copy byte/half word/word loads and stores.  */
06627 
06628 static void
06629 install_load_store (struct gdbarch *gdbarch, struct regcache *regs,
06630                     struct displaced_step_closure *dsc, int load,
06631                     int immed, int writeback, int size, int usermode,
06632                     int rt, int rm, int rn)
06633 {
06634   ULONGEST rt_val, rn_val, rm_val = 0;
06635 
06636   dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
06637   dsc->tmp[2] = displaced_read_reg (regs, dsc, 2);
06638   if (!immed)
06639     dsc->tmp[3] = displaced_read_reg (regs, dsc, 3);
06640   if (!load)
06641     dsc->tmp[4] = displaced_read_reg (regs, dsc, 4);
06642 
06643   rt_val = displaced_read_reg (regs, dsc, rt);
06644   rn_val = displaced_read_reg (regs, dsc, rn);
06645   if (!immed)
06646     rm_val = displaced_read_reg (regs, dsc, rm);
06647 
06648   displaced_write_reg (regs, dsc, 0, rt_val, CANNOT_WRITE_PC);
06649   displaced_write_reg (regs, dsc, 2, rn_val, CANNOT_WRITE_PC);
06650   if (!immed)
06651     displaced_write_reg (regs, dsc, 3, rm_val, CANNOT_WRITE_PC);
06652   dsc->rd = rt;
06653   dsc->u.ldst.xfersize = size;
06654   dsc->u.ldst.rn = rn;
06655   dsc->u.ldst.immed = immed;
06656   dsc->u.ldst.writeback = writeback;
06657 
06658   /* To write PC we can do:
06659 
06660      Before this sequence of instructions:
06661      r0 is the PC value got from displaced_read_reg, so r0 = from + 8;
06662      r2 is the Rn value got from dispalced_read_reg.
06663 
06664      Insn1: push {pc} Write address of STR instruction + offset on stack
06665      Insn2: pop  {r4} Read it back from stack, r4 = addr(Insn1) + offset
06666      Insn3: sub r4, r4, pc   r4 = addr(Insn1) + offset - pc
06667                                 = addr(Insn1) + offset - addr(Insn3) - 8
06668                                 = offset - 16
06669      Insn4: add r4, r4, #8   r4 = offset - 8
06670      Insn5: add r0, r0, r4   r0 = from + 8 + offset - 8
06671                                 = from + offset
06672      Insn6: str r0, [r2, #imm] (or str r0, [r2, r3])
06673 
06674      Otherwise we don't know what value to write for PC, since the offset is
06675      architecture-dependent (sometimes PC+8, sometimes PC+12).  More details
06676      of this can be found in Section "Saving from r15" in
06677      http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dui0204g/Cihbjifh.html */
06678 
06679   dsc->cleanup = load ? &cleanup_load : &cleanup_store;
06680 }
06681 
06682 
06683 static int
06684 thumb2_copy_load_literal (struct gdbarch *gdbarch, uint16_t insn1,
06685                           uint16_t insn2, struct regcache *regs,
06686                           struct displaced_step_closure *dsc, int size)
06687 {
06688   unsigned int u_bit = bit (insn1, 7);
06689   unsigned int rt = bits (insn2, 12, 15);
06690   int imm12 = bits (insn2, 0, 11);
06691   ULONGEST pc_val;
06692 
06693   if (debug_displaced)
06694     fprintf_unfiltered (gdb_stdlog,
06695                         "displaced: copying ldr pc (0x%x) R%d %c imm12 %.4x\n",
06696                         (unsigned int) dsc->insn_addr, rt, u_bit ? '+' : '-',
06697                         imm12);
06698 
06699   if (!u_bit)
06700     imm12 = -1 * imm12;
06701 
06702   /* Rewrite instruction LDR Rt imm12 into:
06703 
06704      Prepare: tmp[0] <- r0, tmp[1] <- r2, tmp[2] <- r3, r2 <- pc, r3 <- imm12
06705 
06706      LDR R0, R2, R3,
06707 
06708      Cleanup: rt <- r0, r0 <- tmp[0], r2 <- tmp[1], r3 <- tmp[2].  */
06709 
06710 
06711   dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
06712   dsc->tmp[2] = displaced_read_reg (regs, dsc, 2);
06713   dsc->tmp[3] = displaced_read_reg (regs, dsc, 3);
06714 
06715   pc_val = displaced_read_reg (regs, dsc, ARM_PC_REGNUM);
06716 
06717   pc_val = pc_val & 0xfffffffc;
06718 
06719   displaced_write_reg (regs, dsc, 2, pc_val, CANNOT_WRITE_PC);
06720   displaced_write_reg (regs, dsc, 3, imm12, CANNOT_WRITE_PC);
06721 
06722   dsc->rd = rt;
06723 
06724   dsc->u.ldst.xfersize = size;
06725   dsc->u.ldst.immed = 0;
06726   dsc->u.ldst.writeback = 0;
06727   dsc->u.ldst.restore_r4 = 0;
06728 
06729   /* LDR R0, R2, R3 */
06730   dsc->modinsn[0] = 0xf852;
06731   dsc->modinsn[1] = 0x3;
06732   dsc->numinsns = 2;
06733 
06734   dsc->cleanup = &cleanup_load;
06735 
06736   return 0;
06737 }
06738 
06739 static int
06740 thumb2_copy_load_reg_imm (struct gdbarch *gdbarch, uint16_t insn1,
06741                           uint16_t insn2, struct regcache *regs,
06742                           struct displaced_step_closure *dsc,
06743                           int writeback, int immed)
06744 {
06745   unsigned int rt = bits (insn2, 12, 15);
06746   unsigned int rn = bits (insn1, 0, 3);
06747   unsigned int rm = bits (insn2, 0, 3);  /* Only valid if !immed.  */
06748   /* In LDR (register), there is also a register Rm, which is not allowed to
06749      be PC, so we don't have to check it.  */
06750 
06751   if (rt != ARM_PC_REGNUM && rn != ARM_PC_REGNUM)
06752     return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2, "load",
06753                                         dsc);
06754 
06755   if (debug_displaced)
06756     fprintf_unfiltered (gdb_stdlog,
06757                         "displaced: copying ldr r%d [r%d] insn %.4x%.4x\n",
06758                          rt, rn, insn1, insn2);
06759 
06760   install_load_store (gdbarch, regs, dsc, 1, immed, writeback, 4,
06761                       0, rt, rm, rn);
06762 
06763   dsc->u.ldst.restore_r4 = 0;
06764 
06765   if (immed)
06766     /* ldr[b]<cond> rt, [rn, #imm], etc.
06767        ->
06768        ldr[b]<cond> r0, [r2, #imm].  */
06769     {
06770       dsc->modinsn[0] = (insn1 & 0xfff0) | 0x2;
06771       dsc->modinsn[1] = insn2 & 0x0fff;
06772     }
06773   else
06774     /* ldr[b]<cond> rt, [rn, rm], etc.
06775        ->
06776        ldr[b]<cond> r0, [r2, r3].  */
06777     {
06778       dsc->modinsn[0] = (insn1 & 0xfff0) | 0x2;
06779       dsc->modinsn[1] = (insn2 & 0x0ff0) | 0x3;
06780     }
06781 
06782   dsc->numinsns = 2;
06783 
06784   return 0;
06785 }
06786 
06787 
06788 static int
06789 arm_copy_ldr_str_ldrb_strb (struct gdbarch *gdbarch, uint32_t insn,
06790                             struct regcache *regs,
06791                             struct displaced_step_closure *dsc,
06792                             int load, int size, int usermode)
06793 {
06794   int immed = !bit (insn, 25);
06795   int writeback = (bit (insn, 24) == 0 || bit (insn, 21) != 0);
06796   unsigned int rt = bits (insn, 12, 15);
06797   unsigned int rn = bits (insn, 16, 19);
06798   unsigned int rm = bits (insn, 0, 3);  /* Only valid if !immed.  */
06799 
06800   if (!insn_references_pc (insn, 0x000ff00ful))
06801     return arm_copy_unmodified (gdbarch, insn, "load/store", dsc);
06802 
06803   if (debug_displaced)
06804     fprintf_unfiltered (gdb_stdlog,
06805                         "displaced: copying %s%s r%d [r%d] insn %.8lx\n",
06806                         load ? (size == 1 ? "ldrb" : "ldr")
06807                              : (size == 1 ? "strb" : "str"), usermode ? "t" : "",
06808                         rt, rn,
06809                         (unsigned long) insn);
06810 
06811   install_load_store (gdbarch, regs, dsc, load, immed, writeback, size,
06812                       usermode, rt, rm, rn);
06813 
06814   if (load || rt != ARM_PC_REGNUM)
06815     {
06816       dsc->u.ldst.restore_r4 = 0;
06817 
06818       if (immed)
06819         /* {ldr,str}[b]<cond> rt, [rn, #imm], etc.
06820            ->
06821            {ldr,str}[b]<cond> r0, [r2, #imm].  */
06822         dsc->modinsn[0] = (insn & 0xfff00fff) | 0x20000;
06823       else
06824         /* {ldr,str}[b]<cond> rt, [rn, rm], etc.
06825            ->
06826            {ldr,str}[b]<cond> r0, [r2, r3].  */
06827         dsc->modinsn[0] = (insn & 0xfff00ff0) | 0x20003;
06828     }
06829   else
06830     {
06831       /* We need to use r4 as scratch.  Make sure it's restored afterwards.  */
06832       dsc->u.ldst.restore_r4 = 1;
06833       dsc->modinsn[0] = 0xe92d8000;  /* push {pc} */
06834       dsc->modinsn[1] = 0xe8bd0010;  /* pop  {r4} */
06835       dsc->modinsn[2] = 0xe044400f;  /* sub r4, r4, pc.  */
06836       dsc->modinsn[3] = 0xe2844008;  /* add r4, r4, #8.  */
06837       dsc->modinsn[4] = 0xe0800004;  /* add r0, r0, r4.  */
06838 
06839       /* As above.  */
06840       if (immed)
06841         dsc->modinsn[5] = (insn & 0xfff00fff) | 0x20000;
06842       else
06843         dsc->modinsn[5] = (insn & 0xfff00ff0) | 0x20003;
06844 
06845       dsc->numinsns = 6;
06846     }
06847 
06848   dsc->cleanup = load ? &cleanup_load : &cleanup_store;
06849 
06850   return 0;
06851 }
06852 
06853 /* Cleanup LDM instructions with fully-populated register list.  This is an
06854    unfortunate corner case: it's impossible to implement correctly by modifying
06855    the instruction.  The issue is as follows: we have an instruction,
06856 
06857    ldm rN, {r0-r15}
06858 
06859    which we must rewrite to avoid loading PC.  A possible solution would be to
06860    do the load in two halves, something like (with suitable cleanup
06861    afterwards):
06862 
06863    mov r8, rN
06864    ldm[id][ab] r8!, {r0-r7}
06865    str r7, <temp>
06866    ldm[id][ab] r8, {r7-r14}
06867    <bkpt>
06868 
06869    but at present there's no suitable place for <temp>, since the scratch space
06870    is overwritten before the cleanup routine is called.  For now, we simply
06871    emulate the instruction.  */
06872 
06873 static void
06874 cleanup_block_load_all (struct gdbarch *gdbarch, struct regcache *regs,
06875                         struct displaced_step_closure *dsc)
06876 {
06877   int inc = dsc->u.block.increment;
06878   int bump_before = dsc->u.block.before ? (inc ? 4 : -4) : 0;
06879   int bump_after = dsc->u.block.before ? 0 : (inc ? 4 : -4);
06880   uint32_t regmask = dsc->u.block.regmask;
06881   int regno = inc ? 0 : 15;
06882   CORE_ADDR xfer_addr = dsc->u.block.xfer_addr;
06883   int exception_return = dsc->u.block.load && dsc->u.block.user
06884                          && (regmask & 0x8000) != 0;
06885   uint32_t status = displaced_read_reg (regs, dsc, ARM_PS_REGNUM);
06886   int do_transfer = condition_true (dsc->u.block.cond, status);
06887   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
06888 
06889   if (!do_transfer)
06890     return;
06891 
06892   /* If the instruction is ldm rN, {...pc}^, I don't think there's anything
06893      sensible we can do here.  Complain loudly.  */
06894   if (exception_return)
06895     error (_("Cannot single-step exception return"));
06896 
06897   /* We don't handle any stores here for now.  */
06898   gdb_assert (dsc->u.block.load != 0);
06899 
06900   if (debug_displaced)
06901     fprintf_unfiltered (gdb_stdlog, "displaced: emulating block transfer: "
06902                         "%s %s %s\n", dsc->u.block.load ? "ldm" : "stm",
06903                         dsc->u.block.increment ? "inc" : "dec",
06904                         dsc->u.block.before ? "before" : "after");
06905 
06906   while (regmask)
06907     {
06908       uint32_t memword;
06909 
06910       if (inc)
06911         while (regno <= ARM_PC_REGNUM && (regmask & (1 << regno)) == 0)
06912           regno++;
06913       else
06914         while (regno >= 0 && (regmask & (1 << regno)) == 0)
06915           regno--;
06916 
06917       xfer_addr += bump_before;
06918 
06919       memword = read_memory_unsigned_integer (xfer_addr, 4, byte_order);
06920       displaced_write_reg (regs, dsc, regno, memword, LOAD_WRITE_PC);
06921 
06922       xfer_addr += bump_after;
06923 
06924       regmask &= ~(1 << regno);
06925     }
06926 
06927   if (dsc->u.block.writeback)
06928     displaced_write_reg (regs, dsc, dsc->u.block.rn, xfer_addr,
06929                          CANNOT_WRITE_PC);
06930 }
06931 
06932 /* Clean up an STM which included the PC in the register list.  */
06933 
06934 static void
06935 cleanup_block_store_pc (struct gdbarch *gdbarch, struct regcache *regs,
06936                         struct displaced_step_closure *dsc)
06937 {
06938   uint32_t status = displaced_read_reg (regs, dsc, ARM_PS_REGNUM);
06939   int store_executed = condition_true (dsc->u.block.cond, status);
06940   CORE_ADDR pc_stored_at, transferred_regs = bitcount (dsc->u.block.regmask);
06941   CORE_ADDR stm_insn_addr;
06942   uint32_t pc_val;
06943   long offset;
06944   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
06945 
06946   /* If condition code fails, there's nothing else to do.  */
06947   if (!store_executed)
06948     return;
06949 
06950   if (dsc->u.block.increment)
06951     {
06952       pc_stored_at = dsc->u.block.xfer_addr + 4 * transferred_regs;
06953 
06954       if (dsc->u.block.before)
06955          pc_stored_at += 4;
06956     }
06957   else
06958     {
06959       pc_stored_at = dsc->u.block.xfer_addr;
06960 
06961       if (dsc->u.block.before)
06962          pc_stored_at -= 4;
06963     }
06964 
06965   pc_val = read_memory_unsigned_integer (pc_stored_at, 4, byte_order);
06966   stm_insn_addr = dsc->scratch_base;
06967   offset = pc_val - stm_insn_addr;
06968 
06969   if (debug_displaced)
06970     fprintf_unfiltered (gdb_stdlog, "displaced: detected PC offset %.8lx for "
06971                         "STM instruction\n", offset);
06972 
06973   /* Rewrite the stored PC to the proper value for the non-displaced original
06974      instruction.  */
06975   write_memory_unsigned_integer (pc_stored_at, 4, byte_order,
06976                                  dsc->insn_addr + offset);
06977 }
06978 
06979 /* Clean up an LDM which includes the PC in the register list.  We clumped all
06980    the registers in the transferred list into a contiguous range r0...rX (to
06981    avoid loading PC directly and losing control of the debugged program), so we
06982    must undo that here.  */
06983 
06984 static void
06985 cleanup_block_load_pc (struct gdbarch *gdbarch,
06986                        struct regcache *regs,
06987                        struct displaced_step_closure *dsc)
06988 {
06989   uint32_t status = displaced_read_reg (regs, dsc, ARM_PS_REGNUM);
06990   int load_executed = condition_true (dsc->u.block.cond, status);
06991   unsigned int mask = dsc->u.block.regmask, write_reg = ARM_PC_REGNUM;
06992   unsigned int regs_loaded = bitcount (mask);
06993   unsigned int num_to_shuffle = regs_loaded, clobbered;
06994 
06995   /* The method employed here will fail if the register list is fully populated
06996      (we need to avoid loading PC directly).  */
06997   gdb_assert (num_to_shuffle < 16);
06998 
06999   if (!load_executed)
07000     return;
07001 
07002   clobbered = (1 << num_to_shuffle) - 1;
07003 
07004   while (num_to_shuffle > 0)
07005     {
07006       if ((mask & (1 << write_reg)) != 0)
07007         {
07008           unsigned int read_reg = num_to_shuffle - 1;
07009 
07010           if (read_reg != write_reg)
07011             {
07012               ULONGEST rval = displaced_read_reg (regs, dsc, read_reg);
07013               displaced_write_reg (regs, dsc, write_reg, rval, LOAD_WRITE_PC);
07014               if (debug_displaced)
07015                 fprintf_unfiltered (gdb_stdlog, _("displaced: LDM: move "
07016                                     "loaded register r%d to r%d\n"), read_reg,
07017                                     write_reg);
07018             }
07019           else if (debug_displaced)
07020             fprintf_unfiltered (gdb_stdlog, _("displaced: LDM: register "
07021                                 "r%d already in the right place\n"),
07022                                 write_reg);
07023 
07024           clobbered &= ~(1 << write_reg);
07025 
07026           num_to_shuffle--;
07027         }
07028 
07029       write_reg--;
07030     }
07031 
07032   /* Restore any registers we scribbled over.  */
07033   for (write_reg = 0; clobbered != 0; write_reg++)
07034     {
07035       if ((clobbered & (1 << write_reg)) != 0)
07036         {
07037           displaced_write_reg (regs, dsc, write_reg, dsc->tmp[write_reg],
07038                                CANNOT_WRITE_PC);
07039           if (debug_displaced)
07040             fprintf_unfiltered (gdb_stdlog, _("displaced: LDM: restored "
07041                                 "clobbered register r%d\n"), write_reg);
07042           clobbered &= ~(1 << write_reg);
07043         }
07044     }
07045 
07046   /* Perform register writeback manually.  */
07047   if (dsc->u.block.writeback)
07048     {
07049       ULONGEST new_rn_val = dsc->u.block.xfer_addr;
07050 
07051       if (dsc->u.block.increment)
07052         new_rn_val += regs_loaded * 4;
07053       else
07054         new_rn_val -= regs_loaded * 4;
07055 
07056       displaced_write_reg (regs, dsc, dsc->u.block.rn, new_rn_val,
07057                            CANNOT_WRITE_PC);
07058     }
07059 }
07060 
07061 /* Handle ldm/stm, apart from some tricky cases which are unlikely to occur
07062    in user-level code (in particular exception return, ldm rn, {...pc}^).  */
07063 
07064 static int
07065 arm_copy_block_xfer (struct gdbarch *gdbarch, uint32_t insn,
07066                      struct regcache *regs,
07067                      struct displaced_step_closure *dsc)
07068 {
07069   int load = bit (insn, 20);
07070   int user = bit (insn, 22);
07071   int increment = bit (insn, 23);
07072   int before = bit (insn, 24);
07073   int writeback = bit (insn, 21);
07074   int rn = bits (insn, 16, 19);
07075 
07076   /* Block transfers which don't mention PC can be run directly
07077      out-of-line.  */
07078   if (rn != ARM_PC_REGNUM && (insn & 0x8000) == 0)
07079     return arm_copy_unmodified (gdbarch, insn, "ldm/stm", dsc);
07080 
07081   if (rn == ARM_PC_REGNUM)
07082     {
07083       warning (_("displaced: Unpredictable LDM or STM with "
07084                  "base register r15"));
07085       return arm_copy_unmodified (gdbarch, insn, "unpredictable ldm/stm", dsc);
07086     }
07087 
07088   if (debug_displaced)
07089     fprintf_unfiltered (gdb_stdlog, "displaced: copying block transfer insn "
07090                         "%.8lx\n", (unsigned long) insn);
07091 
07092   dsc->u.block.xfer_addr = displaced_read_reg (regs, dsc, rn);
07093   dsc->u.block.rn = rn;
07094 
07095   dsc->u.block.load = load;
07096   dsc->u.block.user = user;
07097   dsc->u.block.increment = increment;
07098   dsc->u.block.before = before;
07099   dsc->u.block.writeback = writeback;
07100   dsc->u.block.cond = bits (insn, 28, 31);
07101 
07102   dsc->u.block.regmask = insn & 0xffff;
07103 
07104   if (load)
07105     {
07106       if ((insn & 0xffff) == 0xffff)
07107         {
07108           /* LDM with a fully-populated register list.  This case is
07109              particularly tricky.  Implement for now by fully emulating the
07110              instruction (which might not behave perfectly in all cases, but
07111              these instructions should be rare enough for that not to matter
07112              too much).  */
07113           dsc->modinsn[0] = ARM_NOP;
07114 
07115           dsc->cleanup = &cleanup_block_load_all;
07116         }
07117       else
07118         {
07119           /* LDM of a list of registers which includes PC.  Implement by
07120              rewriting the list of registers to be transferred into a
07121              contiguous chunk r0...rX before doing the transfer, then shuffling
07122              registers into the correct places in the cleanup routine.  */
07123           unsigned int regmask = insn & 0xffff;
07124           unsigned int num_in_list = bitcount (regmask), new_regmask, bit = 1;
07125           unsigned int to = 0, from = 0, i, new_rn;
07126 
07127           for (i = 0; i < num_in_list; i++)
07128             dsc->tmp[i] = displaced_read_reg (regs, dsc, i);
07129 
07130           /* Writeback makes things complicated.  We need to avoid clobbering
07131              the base register with one of the registers in our modified
07132              register list, but just using a different register can't work in
07133              all cases, e.g.:
07134 
07135                ldm r14!, {r0-r13,pc}
07136 
07137              which would need to be rewritten as:
07138 
07139                ldm rN!, {r0-r14}
07140 
07141              but that can't work, because there's no free register for N.
07142 
07143              Solve this by turning off the writeback bit, and emulating
07144              writeback manually in the cleanup routine.  */
07145 
07146           if (writeback)
07147             insn &= ~(1 << 21);
07148 
07149           new_regmask = (1 << num_in_list) - 1;
07150 
07151           if (debug_displaced)
07152             fprintf_unfiltered (gdb_stdlog, _("displaced: LDM r%d%s, "
07153                                 "{..., pc}: original reg list %.4x, modified "
07154                                 "list %.4x\n"), rn, writeback ? "!" : "",
07155                                 (int) insn & 0xffff, new_regmask);
07156 
07157           dsc->modinsn[0] = (insn & ~0xffff) | (new_regmask & 0xffff);
07158 
07159           dsc->cleanup = &cleanup_block_load_pc;
07160         }
07161     }
07162   else
07163     {
07164       /* STM of a list of registers which includes PC.  Run the instruction
07165          as-is, but out of line: this will store the wrong value for the PC,
07166          so we must manually fix up the memory in the cleanup routine.
07167          Doing things this way has the advantage that we can auto-detect
07168          the offset of the PC write (which is architecture-dependent) in
07169          the cleanup routine.  */
07170       dsc->modinsn[0] = insn;
07171 
07172       dsc->cleanup = &cleanup_block_store_pc;
07173     }
07174 
07175   return 0;
07176 }
07177 
07178 static int
07179 thumb2_copy_block_xfer (struct gdbarch *gdbarch, uint16_t insn1, uint16_t insn2,
07180                         struct regcache *regs,
07181                         struct displaced_step_closure *dsc)
07182 {
07183   int rn = bits (insn1, 0, 3);
07184   int load = bit (insn1, 4);
07185   int writeback = bit (insn1, 5);
07186 
07187   /* Block transfers which don't mention PC can be run directly
07188      out-of-line.  */
07189   if (rn != ARM_PC_REGNUM && (insn2 & 0x8000) == 0)
07190     return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2, "ldm/stm", dsc);
07191 
07192   if (rn == ARM_PC_REGNUM)
07193     {
07194       warning (_("displaced: Unpredictable LDM or STM with "
07195                  "base register r15"));
07196       return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
07197                                           "unpredictable ldm/stm", dsc);
07198     }
07199 
07200   if (debug_displaced)
07201     fprintf_unfiltered (gdb_stdlog, "displaced: copying block transfer insn "
07202                         "%.4x%.4x\n", insn1, insn2);
07203 
07204   /* Clear bit 13, since it should be always zero.  */
07205   dsc->u.block.regmask = (insn2 & 0xdfff);
07206   dsc->u.block.rn = rn;
07207 
07208   dsc->u.block.load = load;
07209   dsc->u.block.user = 0;
07210   dsc->u.block.increment = bit (insn1, 7);
07211   dsc->u.block.before = bit (insn1, 8);
07212   dsc->u.block.writeback = writeback;
07213   dsc->u.block.cond = INST_AL;
07214   dsc->u.block.xfer_addr = displaced_read_reg (regs, dsc, rn);
07215 
07216   if (load)
07217     {
07218       if (dsc->u.block.regmask == 0xffff)
07219         {
07220           /* This branch is impossible to happen.  */
07221           gdb_assert (0);
07222         }
07223       else
07224         {
07225           unsigned int regmask = dsc->u.block.regmask;
07226           unsigned int num_in_list = bitcount (regmask), new_regmask, bit = 1;
07227           unsigned int to = 0, from = 0, i, new_rn;
07228 
07229           for (i = 0; i < num_in_list; i++)
07230             dsc->tmp[i] = displaced_read_reg (regs, dsc, i);
07231 
07232           if (writeback)
07233             insn1 &= ~(1 << 5);
07234 
07235           new_regmask = (1 << num_in_list) - 1;
07236 
07237           if (debug_displaced)
07238             fprintf_unfiltered (gdb_stdlog, _("displaced: LDM r%d%s, "
07239                                 "{..., pc}: original reg list %.4x, modified "
07240                                 "list %.4x\n"), rn, writeback ? "!" : "",
07241                                 (int) dsc->u.block.regmask, new_regmask);
07242 
07243           dsc->modinsn[0] = insn1;
07244           dsc->modinsn[1] = (new_regmask & 0xffff);
07245           dsc->numinsns = 2;
07246 
07247           dsc->cleanup = &cleanup_block_load_pc;
07248         }
07249     }
07250   else
07251     {
07252       dsc->modinsn[0] = insn1;
07253       dsc->modinsn[1] = insn2;
07254       dsc->numinsns = 2;
07255       dsc->cleanup = &cleanup_block_store_pc;
07256     }
07257   return 0;
07258 }
07259 
07260 /* Cleanup/copy SVC (SWI) instructions.  These two functions are overridden
07261    for Linux, where some SVC instructions must be treated specially.  */
07262 
07263 static void
07264 cleanup_svc (struct gdbarch *gdbarch, struct regcache *regs,
07265              struct displaced_step_closure *dsc)
07266 {
07267   CORE_ADDR resume_addr = dsc->insn_addr + dsc->insn_size;
07268 
07269   if (debug_displaced)
07270     fprintf_unfiltered (gdb_stdlog, "displaced: cleanup for svc, resume at "
07271                         "%.8lx\n", (unsigned long) resume_addr);
07272 
07273   displaced_write_reg (regs, dsc, ARM_PC_REGNUM, resume_addr, BRANCH_WRITE_PC);
07274 }
07275 
07276 
07277 /* Common copy routine for svc instruciton.  */
07278 
07279 static int
07280 install_svc (struct gdbarch *gdbarch, struct regcache *regs,
07281              struct displaced_step_closure *dsc)
07282 {
07283   /* Preparation: none.
07284      Insn: unmodified svc.
07285      Cleanup: pc <- insn_addr + insn_size.  */
07286 
07287   /* Pretend we wrote to the PC, so cleanup doesn't set PC to the next
07288      instruction.  */
07289   dsc->wrote_to_pc = 1;
07290 
07291   /* Allow OS-specific code to override SVC handling.  */
07292   if (dsc->u.svc.copy_svc_os)
07293     return dsc->u.svc.copy_svc_os (gdbarch, regs, dsc);
07294   else
07295     {
07296       dsc->cleanup = &cleanup_svc;
07297       return 0;
07298     }
07299 }
07300 
07301 static int
07302 arm_copy_svc (struct gdbarch *gdbarch, uint32_t insn,
07303               struct regcache *regs, struct displaced_step_closure *dsc)
07304 {
07305 
07306   if (debug_displaced)
07307     fprintf_unfiltered (gdb_stdlog, "displaced: copying svc insn %.8lx\n",
07308                         (unsigned long) insn);
07309 
07310   dsc->modinsn[0] = insn;
07311 
07312   return install_svc (gdbarch, regs, dsc);
07313 }
07314 
07315 static int
07316 thumb_copy_svc (struct gdbarch *gdbarch, uint16_t insn,
07317                 struct regcache *regs, struct displaced_step_closure *dsc)
07318 {
07319 
07320   if (debug_displaced)
07321     fprintf_unfiltered (gdb_stdlog, "displaced: copying svc insn %.4x\n",
07322                         insn);
07323 
07324   dsc->modinsn[0] = insn;
07325 
07326   return install_svc (gdbarch, regs, dsc);
07327 }
07328 
07329 /* Copy undefined instructions.  */
07330 
07331 static int
07332 arm_copy_undef (struct gdbarch *gdbarch, uint32_t insn,
07333                 struct displaced_step_closure *dsc)
07334 {
07335   if (debug_displaced)
07336     fprintf_unfiltered (gdb_stdlog,
07337                         "displaced: copying undefined insn %.8lx\n",
07338                         (unsigned long) insn);
07339 
07340   dsc->modinsn[0] = insn;
07341 
07342   return 0;
07343 }
07344 
07345 static int
07346 thumb_32bit_copy_undef (struct gdbarch *gdbarch, uint16_t insn1, uint16_t insn2,
07347                        struct displaced_step_closure *dsc)
07348 {
07349 
07350   if (debug_displaced)
07351     fprintf_unfiltered (gdb_stdlog, "displaced: copying undefined insn "
07352                        "%.4x %.4x\n", (unsigned short) insn1,
07353                        (unsigned short) insn2);
07354 
07355   dsc->modinsn[0] = insn1;
07356   dsc->modinsn[1] = insn2;
07357   dsc->numinsns = 2;
07358 
07359   return 0;
07360 }
07361 
07362 /* Copy unpredictable instructions.  */
07363 
07364 static int
07365 arm_copy_unpred (struct gdbarch *gdbarch, uint32_t insn,
07366                  struct displaced_step_closure *dsc)
07367 {
07368   if (debug_displaced)
07369     fprintf_unfiltered (gdb_stdlog, "displaced: copying unpredictable insn "
07370                         "%.8lx\n", (unsigned long) insn);
07371 
07372   dsc->modinsn[0] = insn;
07373 
07374   return 0;
07375 }
07376 
07377 /* The decode_* functions are instruction decoding helpers.  They mostly follow
07378    the presentation in the ARM ARM.  */
07379 
07380 static int
07381 arm_decode_misc_memhint_neon (struct gdbarch *gdbarch, uint32_t insn,
07382                               struct regcache *regs,
07383                               struct displaced_step_closure *dsc)
07384 {
07385   unsigned int op1 = bits (insn, 20, 26), op2 = bits (insn, 4, 7);
07386   unsigned int rn = bits (insn, 16, 19);
07387 
07388   if (op1 == 0x10 && (op2 & 0x2) == 0x0 && (rn & 0xe) == 0x0)
07389     return arm_copy_unmodified (gdbarch, insn, "cps", dsc);
07390   else if (op1 == 0x10 && op2 == 0x0 && (rn & 0xe) == 0x1)
07391     return arm_copy_unmodified (gdbarch, insn, "setend", dsc);
07392   else if ((op1 & 0x60) == 0x20)
07393     return arm_copy_unmodified (gdbarch, insn, "neon dataproc", dsc);
07394   else if ((op1 & 0x71) == 0x40)
07395     return arm_copy_unmodified (gdbarch, insn, "neon elt/struct load/store",
07396                                 dsc);
07397   else if ((op1 & 0x77) == 0x41)
07398     return arm_copy_unmodified (gdbarch, insn, "unallocated mem hint", dsc);
07399   else if ((op1 & 0x77) == 0x45)
07400     return arm_copy_preload (gdbarch, insn, regs, dsc);  /* pli.  */
07401   else if ((op1 & 0x77) == 0x51)
07402     {
07403       if (rn != 0xf)
07404         return arm_copy_preload (gdbarch, insn, regs, dsc);  /* pld/pldw.  */
07405       else
07406         return arm_copy_unpred (gdbarch, insn, dsc);
07407     }
07408   else if ((op1 & 0x77) == 0x55)
07409     return arm_copy_preload (gdbarch, insn, regs, dsc);  /* pld/pldw.  */
07410   else if (op1 == 0x57)
07411     switch (op2)
07412       {
07413       case 0x1: return arm_copy_unmodified (gdbarch, insn, "clrex", dsc);
07414       case 0x4: return arm_copy_unmodified (gdbarch, insn, "dsb", dsc);
07415       case 0x5: return arm_copy_unmodified (gdbarch, insn, "dmb", dsc);
07416       case 0x6: return arm_copy_unmodified (gdbarch, insn, "isb", dsc);
07417       default: return arm_copy_unpred (gdbarch, insn, dsc);
07418       }
07419   else if ((op1 & 0x63) == 0x43)
07420     return arm_copy_unpred (gdbarch, insn, dsc);
07421   else if ((op2 & 0x1) == 0x0)
07422     switch (op1 & ~0x80)
07423       {
07424       case 0x61:
07425         return arm_copy_unmodified (gdbarch, insn, "unallocated mem hint", dsc);
07426       case 0x65:
07427         return arm_copy_preload_reg (gdbarch, insn, regs, dsc);  /* pli reg.  */
07428       case 0x71: case 0x75:
07429         /* pld/pldw reg.  */
07430         return arm_copy_preload_reg (gdbarch, insn, regs, dsc);
07431       case 0x63: case 0x67: case 0x73: case 0x77:
07432         return arm_copy_unpred (gdbarch, insn, dsc);
07433       default:
07434         return arm_copy_undef (gdbarch, insn, dsc);
07435       }
07436   else
07437     return arm_copy_undef (gdbarch, insn, dsc);  /* Probably unreachable.  */
07438 }
07439 
07440 static int
07441 arm_decode_unconditional (struct gdbarch *gdbarch, uint32_t insn,
07442                           struct regcache *regs,
07443                           struct displaced_step_closure *dsc)
07444 {
07445   if (bit (insn, 27) == 0)
07446     return arm_decode_misc_memhint_neon (gdbarch, insn, regs, dsc);
07447   /* Switch on bits: 0bxxxxx321xxx0xxxxxxxxxxxxxxxxxxxx.  */
07448   else switch (((insn & 0x7000000) >> 23) | ((insn & 0x100000) >> 20))
07449     {
07450     case 0x0: case 0x2:
07451       return arm_copy_unmodified (gdbarch, insn, "srs", dsc);
07452 
07453     case 0x1: case 0x3:
07454       return arm_copy_unmodified (gdbarch, insn, "rfe", dsc);
07455 
07456     case 0x4: case 0x5: case 0x6: case 0x7:
07457       return arm_copy_b_bl_blx (gdbarch, insn, regs, dsc);
07458 
07459     case 0x8:
07460       switch ((insn & 0xe00000) >> 21)
07461         {
07462         case 0x1: case 0x3: case 0x4: case 0x5: case 0x6: case 0x7:
07463           /* stc/stc2.  */
07464           return arm_copy_copro_load_store (gdbarch, insn, regs, dsc);
07465 
07466         case 0x2:
07467           return arm_copy_unmodified (gdbarch, insn, "mcrr/mcrr2", dsc);
07468 
07469         default:
07470           return arm_copy_undef (gdbarch, insn, dsc);
07471         }
07472 
07473     case 0x9:
07474       {
07475          int rn_f = (bits (insn, 16, 19) == 0xf);
07476         switch ((insn & 0xe00000) >> 21)
07477           {
07478           case 0x1: case 0x3:
07479             /* ldc/ldc2 imm (undefined for rn == pc).  */
07480             return rn_f ? arm_copy_undef (gdbarch, insn, dsc)
07481                         : arm_copy_copro_load_store (gdbarch, insn, regs, dsc);
07482 
07483           case 0x2:
07484             return arm_copy_unmodified (gdbarch, insn, "mrrc/mrrc2", dsc);
07485 
07486           case 0x4: case 0x5: case 0x6: case 0x7:
07487             /* ldc/ldc2 lit (undefined for rn != pc).  */
07488             return rn_f ? arm_copy_copro_load_store (gdbarch, insn, regs, dsc)
07489                         : arm_copy_undef (gdbarch, insn, dsc);
07490 
07491           default:
07492             return arm_copy_undef (gdbarch, insn, dsc);
07493           }
07494       }
07495 
07496     case 0xa:
07497       return arm_copy_unmodified (gdbarch, insn, "stc/stc2", dsc);
07498 
07499     case 0xb:
07500       if (bits (insn, 16, 19) == 0xf)
07501         /* ldc/ldc2 lit.  */
07502         return arm_copy_copro_load_store (gdbarch, insn, regs, dsc);
07503       else
07504         return arm_copy_undef (gdbarch, insn, dsc);
07505 
07506     case 0xc:
07507       if (bit (insn, 4))
07508         return arm_copy_unmodified (gdbarch, insn, "mcr/mcr2", dsc);
07509       else
07510         return arm_copy_unmodified (gdbarch, insn, "cdp/cdp2", dsc);
07511 
07512     case 0xd:
07513       if (bit (insn, 4))
07514         return arm_copy_unmodified (gdbarch, insn, "mrc/mrc2", dsc);
07515       else
07516         return arm_copy_unmodified (gdbarch, insn, "cdp/cdp2", dsc);
07517 
07518     default:
07519       return arm_copy_undef (gdbarch, insn, dsc);
07520     }
07521 }
07522 
07523 /* Decode miscellaneous instructions in dp/misc encoding space.  */
07524 
07525 static int
07526 arm_decode_miscellaneous (struct gdbarch *gdbarch, uint32_t insn,
07527                           struct regcache *regs,
07528                           struct displaced_step_closure *dsc)
07529 {
07530   unsigned int op2 = bits (insn, 4, 6);
07531   unsigned int op = bits (insn, 21, 22);
07532   unsigned int op1 = bits (insn, 16, 19);
07533 
07534   switch (op2)
07535     {
07536     case 0x0:
07537       return arm_copy_unmodified (gdbarch, insn, "mrs/msr", dsc);
07538 
07539     case 0x1:
07540       if (op == 0x1)  /* bx.  */
07541         return arm_copy_bx_blx_reg (gdbarch, insn, regs, dsc);
07542       else if (op == 0x3)
07543         return arm_copy_unmodified (gdbarch, insn, "clz", dsc);
07544       else
07545         return arm_copy_undef (gdbarch, insn, dsc);
07546 
07547     case 0x2:
07548       if (op == 0x1)
07549         /* Not really supported.  */
07550         return arm_copy_unmodified (gdbarch, insn, "bxj", dsc);
07551       else
07552         return arm_copy_undef (gdbarch, insn, dsc);
07553 
07554     case 0x3:
07555       if (op == 0x1)
07556         return arm_copy_bx_blx_reg (gdbarch, insn,
07557                                 regs, dsc);  /* blx register.  */
07558       else
07559         return arm_copy_undef (gdbarch, insn, dsc);
07560 
07561     case 0x5:
07562       return arm_copy_unmodified (gdbarch, insn, "saturating add/sub", dsc);
07563 
07564     case 0x7:
07565       if (op == 0x1)
07566         return arm_copy_unmodified (gdbarch, insn, "bkpt", dsc);
07567       else if (op == 0x3)
07568         /* Not really supported.  */
07569         return arm_copy_unmodified (gdbarch, insn, "smc", dsc);
07570 
07571     default:
07572       return arm_copy_undef (gdbarch, insn, dsc);
07573     }
07574 }
07575 
07576 static int
07577 arm_decode_dp_misc (struct gdbarch *gdbarch, uint32_t insn,
07578                     struct regcache *regs,
07579                     struct displaced_step_closure *dsc)
07580 {
07581   if (bit (insn, 25))
07582     switch (bits (insn, 20, 24))
07583       {
07584       case 0x10:
07585         return arm_copy_unmodified (gdbarch, insn, "movw", dsc);
07586 
07587       case 0x14:
07588         return arm_copy_unmodified (gdbarch, insn, "movt", dsc);
07589 
07590       case 0x12: case 0x16:
07591         return arm_copy_unmodified (gdbarch, insn, "msr imm", dsc);
07592 
07593       default:
07594         return arm_copy_alu_imm (gdbarch, insn, regs, dsc);
07595       }
07596   else
07597     {
07598       uint32_t op1 = bits (insn, 20, 24), op2 = bits (insn, 4, 7);
07599 
07600       if ((op1 & 0x19) != 0x10 && (op2 & 0x1) == 0x0)
07601         return arm_copy_alu_reg (gdbarch, insn, regs, dsc);
07602       else if ((op1 & 0x19) != 0x10 && (op2 & 0x9) == 0x1)
07603         return arm_copy_alu_shifted_reg (gdbarch, insn, regs, dsc);
07604       else if ((op1 & 0x19) == 0x10 && (op2 & 0x8) == 0x0)
07605         return arm_decode_miscellaneous (gdbarch, insn, regs, dsc);
07606       else if ((op1 & 0x19) == 0x10 && (op2 & 0x9) == 0x8)
07607         return arm_copy_unmodified (gdbarch, insn, "halfword mul/mla", dsc);
07608       else if ((op1 & 0x10) == 0x00 && op2 == 0x9)
07609         return arm_copy_unmodified (gdbarch, insn, "mul/mla", dsc);
07610       else if ((op1 & 0x10) == 0x10 && op2 == 0x9)
07611         return arm_copy_unmodified (gdbarch, insn, "synch", dsc);
07612       else if (op2 == 0xb || (op2 & 0xd) == 0xd)
07613         /* 2nd arg means "unpriveleged".  */
07614         return arm_copy_extra_ld_st (gdbarch, insn, (op1 & 0x12) == 0x02, regs,
07615                                      dsc);
07616     }
07617 
07618   /* Should be unreachable.  */
07619   return 1;
07620 }
07621 
07622 static int
07623 arm_decode_ld_st_word_ubyte (struct gdbarch *gdbarch, uint32_t insn,
07624                              struct regcache *regs,
07625                              struct displaced_step_closure *dsc)
07626 {
07627   int a = bit (insn, 25), b = bit (insn, 4);
07628   uint32_t op1 = bits (insn, 20, 24);
07629   int rn_f = bits (insn, 16, 19) == 0xf;
07630 
07631   if ((!a && (op1 & 0x05) == 0x00 && (op1 & 0x17) != 0x02)
07632       || (a && (op1 & 0x05) == 0x00 && (op1 & 0x17) != 0x02 && !b))
07633     return arm_copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 0, 4, 0);
07634   else if ((!a && (op1 & 0x17) == 0x02)
07635             || (a && (op1 & 0x17) == 0x02 && !b))
07636     return arm_copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 0, 4, 1);
07637   else if ((!a && (op1 & 0x05) == 0x01 && (op1 & 0x17) != 0x03)
07638             || (a && (op1 & 0x05) == 0x01 && (op1 & 0x17) != 0x03 && !b))
07639     return arm_copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 1, 4, 0);
07640   else if ((!a && (op1 & 0x17) == 0x03)
07641            || (a && (op1 & 0x17) == 0x03 && !b))
07642     return arm_copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 1, 4, 1);
07643   else if ((!a && (op1 & 0x05) == 0x04 && (op1 & 0x17) != 0x06)
07644             || (a && (op1 & 0x05) == 0x04 && (op1 & 0x17) != 0x06 && !b))
07645     return arm_copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 0, 1, 0);
07646   else if ((!a && (op1 & 0x17) == 0x06)
07647            || (a && (op1 & 0x17) == 0x06 && !b))
07648     return arm_copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 0, 1, 1);
07649   else if ((!a && (op1 & 0x05) == 0x05 && (op1 & 0x17) != 0x07)
07650            || (a && (op1 & 0x05) == 0x05 && (op1 & 0x17) != 0x07 && !b))
07651     return arm_copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 1, 1, 0);
07652   else if ((!a && (op1 & 0x17) == 0x07)
07653            || (a && (op1 & 0x17) == 0x07 && !b))
07654     return arm_copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 1, 1, 1);
07655 
07656   /* Should be unreachable.  */
07657   return 1;
07658 }
07659 
07660 static int
07661 arm_decode_media (struct gdbarch *gdbarch, uint32_t insn,
07662                   struct displaced_step_closure *dsc)
07663 {
07664   switch (bits (insn, 20, 24))
07665     {
07666     case 0x00: case 0x01: case 0x02: case 0x03:
07667       return arm_copy_unmodified (gdbarch, insn, "parallel add/sub signed", dsc);
07668 
07669     case 0x04: case 0x05: case 0x06: case 0x07:
07670       return arm_copy_unmodified (gdbarch, insn, "parallel add/sub unsigned", dsc);
07671 
07672     case 0x08: case 0x09: case 0x0a: case 0x0b:
07673     case 0x0c: case 0x0d: case 0x0e: case 0x0f:
07674       return arm_copy_unmodified (gdbarch, insn,
07675                               "decode/pack/unpack/saturate/reverse", dsc);
07676 
07677     case 0x18:
07678       if (bits (insn, 5, 7) == 0)  /* op2.  */
07679          {
07680           if (bits (insn, 12, 15) == 0xf)
07681             return arm_copy_unmodified (gdbarch, insn, "usad8", dsc);
07682           else
07683             return arm_copy_unmodified (gdbarch, insn, "usada8", dsc);
07684         }
07685       else
07686          return arm_copy_undef (gdbarch, insn, dsc);
07687 
07688     case 0x1a: case 0x1b:
07689       if (bits (insn, 5, 6) == 0x2)  /* op2[1:0].  */
07690         return arm_copy_unmodified (gdbarch, insn, "sbfx", dsc);
07691       else
07692         return arm_copy_undef (gdbarch, insn, dsc);
07693 
07694     case 0x1c: case 0x1d:
07695       if (bits (insn, 5, 6) == 0x0)  /* op2[1:0].  */
07696          {
07697           if (bits (insn, 0, 3) == 0xf)
07698             return arm_copy_unmodified (gdbarch, insn, "bfc", dsc);
07699           else
07700             return arm_copy_unmodified (gdbarch, insn, "bfi", dsc);
07701         }
07702       else
07703         return arm_copy_undef (gdbarch, insn, dsc);
07704 
07705     case 0x1e: case 0x1f:
07706       if (bits (insn, 5, 6) == 0x2)  /* op2[1:0].  */
07707         return arm_copy_unmodified (gdbarch, insn, "ubfx", dsc);
07708       else
07709         return arm_copy_undef (gdbarch, insn, dsc);
07710     }
07711 
07712   /* Should be unreachable.  */
07713   return 1;
07714 }
07715 
07716 static int
07717 arm_decode_b_bl_ldmstm (struct gdbarch *gdbarch, int32_t insn,
07718                         struct regcache *regs,
07719                         struct displaced_step_closure *dsc)
07720 {
07721   if (bit (insn, 25))
07722     return arm_copy_b_bl_blx (gdbarch, insn, regs, dsc);
07723   else
07724     return arm_copy_block_xfer (gdbarch, insn, regs, dsc);
07725 }
07726 
07727 static int
07728 arm_decode_ext_reg_ld_st (struct gdbarch *gdbarch, uint32_t insn,
07729                           struct regcache *regs,
07730                           struct displaced_step_closure *dsc)
07731 {
07732   unsigned int opcode = bits (insn, 20, 24);
07733 
07734   switch (opcode)
07735     {
07736     case 0x04: case 0x05:  /* VFP/Neon mrrc/mcrr.  */
07737       return arm_copy_unmodified (gdbarch, insn, "vfp/neon mrrc/mcrr", dsc);
07738 
07739     case 0x08: case 0x0a: case 0x0c: case 0x0e:
07740     case 0x12: case 0x16:
07741       return arm_copy_unmodified (gdbarch, insn, "vfp/neon vstm/vpush", dsc);
07742 
07743     case 0x09: case 0x0b: case 0x0d: case 0x0f:
07744     case 0x13: case 0x17:
07745       return arm_copy_unmodified (gdbarch, insn, "vfp/neon vldm/vpop", dsc);
07746 
07747     case 0x10: case 0x14: case 0x18: case 0x1c:  /* vstr.  */
07748     case 0x11: case 0x15: case 0x19: case 0x1d:  /* vldr.  */
07749       /* Note: no writeback for these instructions.  Bit 25 will always be
07750          zero though (via caller), so the following works OK.  */
07751       return arm_copy_copro_load_store (gdbarch, insn, regs, dsc);
07752     }
07753 
07754   /* Should be unreachable.  */
07755   return 1;
07756 }
07757 
07758 /* Decode shifted register instructions.  */
07759 
07760 static int
07761 thumb2_decode_dp_shift_reg (struct gdbarch *gdbarch, uint16_t insn1,
07762                             uint16_t insn2,  struct regcache *regs,
07763                             struct displaced_step_closure *dsc)
07764 {
07765   /* PC is only allowed to be used in instruction MOV.  */
07766 
07767   unsigned int op = bits (insn1, 5, 8);
07768   unsigned int rn = bits (insn1, 0, 3);
07769 
07770   if (op == 0x2 && rn == 0xf) /* MOV */
07771     return thumb2_copy_alu_imm (gdbarch, insn1, insn2, regs, dsc);
07772   else
07773     return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
07774                                         "dp (shift reg)", dsc);
07775 }
07776 
07777 
07778 /* Decode extension register load/store.  Exactly the same as
07779    arm_decode_ext_reg_ld_st.  */
07780 
07781 static int
07782 thumb2_decode_ext_reg_ld_st (struct gdbarch *gdbarch, uint16_t insn1,
07783                              uint16_t insn2,  struct regcache *regs,
07784                              struct displaced_step_closure *dsc)
07785 {
07786   unsigned int opcode = bits (insn1, 4, 8);
07787 
07788   switch (opcode)
07789     {
07790     case 0x04: case 0x05:
07791       return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
07792                                           "vfp/neon vmov", dsc);
07793 
07794     case 0x08: case 0x0c: /* 01x00 */
07795     case 0x0a: case 0x0e: /* 01x10 */
07796     case 0x12: case 0x16: /* 10x10 */
07797       return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
07798                                           "vfp/neon vstm/vpush", dsc);
07799 
07800     case 0x09: case 0x0d: /* 01x01 */
07801     case 0x0b: case 0x0f: /* 01x11 */
07802     case 0x13: case 0x17: /* 10x11 */
07803       return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
07804                                           "vfp/neon vldm/vpop", dsc);
07805 
07806     case 0x10: case 0x14: case 0x18: case 0x1c:  /* vstr.  */
07807       return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
07808                                           "vstr", dsc);
07809     case 0x11: case 0x15: case 0x19: case 0x1d:  /* vldr.  */
07810       return thumb2_copy_copro_load_store (gdbarch, insn1, insn2, regs, dsc);
07811     }
07812 
07813   /* Should be unreachable.  */
07814   return 1;
07815 }
07816 
07817 static int
07818 arm_decode_svc_copro (struct gdbarch *gdbarch, uint32_t insn, CORE_ADDR to,
07819                       struct regcache *regs, struct displaced_step_closure *dsc)
07820 {
07821   unsigned int op1 = bits (insn, 20, 25);
07822   int op = bit (insn, 4);
07823   unsigned int coproc = bits (insn, 8, 11);
07824   unsigned int rn = bits (insn, 16, 19);
07825 
07826   if ((op1 & 0x20) == 0x00 && (op1 & 0x3a) != 0x00 && (coproc & 0xe) == 0xa)
07827     return arm_decode_ext_reg_ld_st (gdbarch, insn, regs, dsc);
07828   else if ((op1 & 0x21) == 0x00 && (op1 & 0x3a) != 0x00
07829            && (coproc & 0xe) != 0xa)
07830     /* stc/stc2.  */
07831     return arm_copy_copro_load_store (gdbarch, insn, regs, dsc);
07832   else if ((op1 & 0x21) == 0x01 && (op1 & 0x3a) != 0x00
07833            && (coproc & 0xe) != 0xa)
07834     /* ldc/ldc2 imm/lit.  */
07835     return arm_copy_copro_load_store (gdbarch, insn, regs, dsc);
07836   else if ((op1 & 0x3e) == 0x00)
07837     return arm_copy_undef (gdbarch, insn, dsc);
07838   else if ((op1 & 0x3e) == 0x04 && (coproc & 0xe) == 0xa)
07839     return arm_copy_unmodified (gdbarch, insn, "neon 64bit xfer", dsc);
07840   else if (op1 == 0x04 && (coproc & 0xe) != 0xa)
07841     return arm_copy_unmodified (gdbarch, insn, "mcrr/mcrr2", dsc);
07842   else if (op1 == 0x05 && (coproc & 0xe) != 0xa)
07843     return arm_copy_unmodified (gdbarch, insn, "mrrc/mrrc2", dsc);
07844   else if ((op1 & 0x30) == 0x20 && !op)
07845     {
07846       if ((coproc & 0xe) == 0xa)
07847         return arm_copy_unmodified (gdbarch, insn, "vfp dataproc", dsc);
07848       else
07849         return arm_copy_unmodified (gdbarch, insn, "cdp/cdp2", dsc);
07850     }
07851   else if ((op1 & 0x30) == 0x20 && op)
07852     return arm_copy_unmodified (gdbarch, insn, "neon 8/16/32 bit xfer", dsc);
07853   else if ((op1 & 0x31) == 0x20 && op && (coproc & 0xe) != 0xa)
07854     return arm_copy_unmodified (gdbarch, insn, "mcr/mcr2", dsc);
07855   else if ((op1 & 0x31) == 0x21 && op && (coproc & 0xe) != 0xa)
07856     return arm_copy_unmodified (gdbarch, insn, "mrc/mrc2", dsc);
07857   else if ((op1 & 0x30) == 0x30)
07858     return arm_copy_svc (gdbarch, insn, regs, dsc);
07859   else
07860     return arm_copy_undef (gdbarch, insn, dsc);  /* Possibly unreachable.  */
07861 }
07862 
07863 static int
07864 thumb2_decode_svc_copro (struct gdbarch *gdbarch, uint16_t insn1,
07865                          uint16_t insn2, struct regcache *regs,
07866                          struct displaced_step_closure *dsc)
07867 {
07868   unsigned int coproc = bits (insn2, 8, 11);
07869   unsigned int op1 = bits (insn1, 4, 9);
07870   unsigned int bit_5_8 = bits (insn1, 5, 8);
07871   unsigned int bit_9 = bit (insn1, 9);
07872   unsigned int bit_4 = bit (insn1, 4);
07873   unsigned int rn = bits (insn1, 0, 3);
07874 
07875   if (bit_9 == 0)
07876     {
07877       if (bit_5_8 == 2)
07878         return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
07879                                             "neon 64bit xfer/mrrc/mrrc2/mcrr/mcrr2",
07880                                             dsc);
07881       else if (bit_5_8 == 0) /* UNDEFINED.  */
07882         return thumb_32bit_copy_undef (gdbarch, insn1, insn2, dsc);
07883       else
07884         {
07885            /*coproc is 101x.  SIMD/VFP, ext registers load/store.  */
07886           if ((coproc & 0xe) == 0xa)
07887             return thumb2_decode_ext_reg_ld_st (gdbarch, insn1, insn2, regs,
07888                                                 dsc);
07889           else /* coproc is not 101x.  */
07890             {
07891               if (bit_4 == 0) /* STC/STC2.  */
07892                 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
07893                                                     "stc/stc2", dsc);
07894               else /* LDC/LDC2 {literal, immeidate}.  */
07895                 return thumb2_copy_copro_load_store (gdbarch, insn1, insn2,
07896                                                      regs, dsc);
07897             }
07898         }
07899     }
07900   else
07901     return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2, "coproc", dsc);
07902 
07903   return 0;
07904 }
07905 
07906 static void
07907 install_pc_relative (struct gdbarch *gdbarch, struct regcache *regs,
07908                      struct displaced_step_closure *dsc, int rd)
07909 {
07910   /* ADR Rd, #imm
07911 
07912      Rewrite as:
07913 
07914      Preparation: Rd <- PC
07915      Insn: ADD Rd, #imm
07916      Cleanup: Null.
07917   */
07918 
07919   /* Rd <- PC */
07920   int val = displaced_read_reg (regs, dsc, ARM_PC_REGNUM);
07921   displaced_write_reg (regs, dsc, rd, val, CANNOT_WRITE_PC);
07922 }
07923 
07924 static int
07925 thumb_copy_pc_relative_16bit (struct gdbarch *gdbarch, struct regcache *regs,
07926                               struct displaced_step_closure *dsc,
07927                               int rd, unsigned int imm)
07928 {
07929 
07930   /* Encoding T2: ADDS Rd, #imm */
07931   dsc->modinsn[0] = (0x3000 | (rd << 8) | imm);
07932 
07933   install_pc_relative (gdbarch, regs, dsc, rd);
07934 
07935   return 0;
07936 }
07937 
07938 static int
07939 thumb_decode_pc_relative_16bit (struct gdbarch *gdbarch, uint16_t insn,
07940                                 struct regcache *regs,
07941                                 struct displaced_step_closure *dsc)
07942 {
07943   unsigned int rd = bits (insn, 8, 10);
07944   unsigned int imm8 = bits (insn, 0, 7);
07945 
07946   if (debug_displaced)
07947     fprintf_unfiltered (gdb_stdlog,
07948                         "displaced: copying thumb adr r%d, #%d insn %.4x\n",
07949                         rd, imm8, insn);
07950 
07951   return thumb_copy_pc_relative_16bit (gdbarch, regs, dsc, rd, imm8);
07952 }
07953 
07954 static int
07955 thumb_copy_pc_relative_32bit (struct gdbarch *gdbarch, uint16_t insn1,
07956                               uint16_t insn2, struct regcache *regs,
07957                               struct displaced_step_closure *dsc)
07958 {
07959   unsigned int rd = bits (insn2, 8, 11);
07960   /* Since immediate has the same encoding in ADR ADD and SUB, so we simply
07961      extract raw immediate encoding rather than computing immediate.  When
07962      generating ADD or SUB instruction, we can simply perform OR operation to
07963      set immediate into ADD.  */
07964   unsigned int imm_3_8 = insn2 & 0x70ff;
07965   unsigned int imm_i = insn1 & 0x0400; /* Clear all bits except bit 10.  */
07966 
07967   if (debug_displaced)
07968     fprintf_unfiltered (gdb_stdlog,
07969                         "displaced: copying thumb adr r%d, #%d:%d insn %.4x%.4x\n",
07970                         rd, imm_i, imm_3_8, insn1, insn2);
07971 
07972   if (bit (insn1, 7)) /* Encoding T2 */
07973     {
07974       /* Encoding T3: SUB Rd, Rd, #imm */
07975       dsc->modinsn[0] = (0xf1a0 | rd | imm_i);
07976       dsc->modinsn[1] = ((rd << 8) | imm_3_8);
07977     }
07978   else /* Encoding T3 */
07979     {
07980       /* Encoding T3: ADD Rd, Rd, #imm */
07981       dsc->modinsn[0] = (0xf100 | rd | imm_i);
07982       dsc->modinsn[1] = ((rd << 8) | imm_3_8);
07983     }
07984   dsc->numinsns = 2;
07985 
07986   install_pc_relative (gdbarch, regs, dsc, rd);
07987 
07988   return 0;
07989 }
07990 
07991 static int
07992 thumb_copy_16bit_ldr_literal (struct gdbarch *gdbarch, unsigned short insn1,
07993                               struct regcache *regs,
07994                               struct displaced_step_closure *dsc)
07995 {
07996   unsigned int rt = bits (insn1, 8, 10);
07997   unsigned int pc;
07998   int imm8 = (bits (insn1, 0, 7) << 2);
07999   CORE_ADDR from = dsc->insn_addr;
08000 
08001   /* LDR Rd, #imm8
08002 
08003      Rwrite as:
08004 
08005      Preparation: tmp0 <- R0, tmp2 <- R2, tmp3 <- R3, R2 <- PC, R3 <- #imm8;
08006 
08007      Insn: LDR R0, [R2, R3];
08008      Cleanup: R2 <- tmp2, R3 <- tmp3, Rd <- R0, R0 <- tmp0 */
08009 
08010   if (debug_displaced)
08011     fprintf_unfiltered (gdb_stdlog,
08012                         "displaced: copying thumb ldr r%d [pc #%d]\n"
08013                         , rt, imm8);
08014 
08015   dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
08016   dsc->tmp[2] = displaced_read_reg (regs, dsc, 2);
08017   dsc->tmp[3] = displaced_read_reg (regs, dsc, 3);
08018   pc = displaced_read_reg (regs, dsc, ARM_PC_REGNUM);
08019   /* The assembler calculates the required value of the offset from the
08020      Align(PC,4) value of this instruction to the label.  */
08021   pc = pc & 0xfffffffc;
08022 
08023   displaced_write_reg (regs, dsc, 2, pc, CANNOT_WRITE_PC);
08024   displaced_write_reg (regs, dsc, 3, imm8, CANNOT_WRITE_PC);
08025 
08026   dsc->rd = rt;
08027   dsc->u.ldst.xfersize = 4;
08028   dsc->u.ldst.rn = 0;
08029   dsc->u.ldst.immed = 0;
08030   dsc->u.ldst.writeback = 0;
08031   dsc->u.ldst.restore_r4 = 0;
08032 
08033   dsc->modinsn[0] = 0x58d0; /* ldr r0, [r2, r3]*/
08034 
08035   dsc->cleanup = &cleanup_load;
08036 
08037   return 0;
08038 }
08039 
08040 /* Copy Thumb cbnz/cbz insruction.  */
08041 
08042 static int
08043 thumb_copy_cbnz_cbz (struct gdbarch *gdbarch, uint16_t insn1,
08044                      struct regcache *regs,
08045                      struct displaced_step_closure *dsc)
08046 {
08047   int non_zero = bit (insn1, 11);
08048   unsigned int imm5 = (bit (insn1, 9) << 6) | (bits (insn1, 3, 7) << 1);
08049   CORE_ADDR from = dsc->insn_addr;
08050   int rn = bits (insn1, 0, 2);
08051   int rn_val = displaced_read_reg (regs, dsc, rn);
08052 
08053   dsc->u.branch.cond = (rn_val && non_zero) || (!rn_val && !non_zero);
08054   /* CBNZ and CBZ do not affect the condition flags.  If condition is true,
08055      set it INST_AL, so cleanup_branch will know branch is taken, otherwise,
08056      condition is false, let it be, cleanup_branch will do nothing.  */
08057   if (dsc->u.branch.cond)
08058     {
08059       dsc->u.branch.cond = INST_AL;
08060       dsc->u.branch.dest = from + 4 + imm5;
08061     }
08062   else
08063       dsc->u.branch.dest = from + 2;
08064 
08065   dsc->u.branch.link = 0;
08066   dsc->u.branch.exchange = 0;
08067 
08068   if (debug_displaced)
08069     fprintf_unfiltered (gdb_stdlog, "displaced: copying %s [r%d = 0x%x]"
08070                         " insn %.4x to %.8lx\n", non_zero ? "cbnz" : "cbz",
08071                         rn, rn_val, insn1, dsc->u.branch.dest);
08072 
08073   dsc->modinsn[0] = THUMB_NOP;
08074 
08075   dsc->cleanup = &cleanup_branch;
08076   return 0;
08077 }
08078 
08079 /* Copy Table Branch Byte/Halfword */
08080 static int
08081 thumb2_copy_table_branch (struct gdbarch *gdbarch, uint16_t insn1,
08082                           uint16_t insn2, struct regcache *regs,
08083                           struct displaced_step_closure *dsc)
08084 {
08085   ULONGEST rn_val, rm_val;
08086   int is_tbh = bit (insn2, 4);
08087   CORE_ADDR halfwords = 0;
08088   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
08089 
08090   rn_val = displaced_read_reg (regs, dsc, bits (insn1, 0, 3));
08091   rm_val = displaced_read_reg (regs, dsc, bits (insn2, 0, 3));
08092 
08093   if (is_tbh)
08094     {
08095       gdb_byte buf[2];
08096 
08097       target_read_memory (rn_val + 2 * rm_val, buf, 2);
08098       halfwords = extract_unsigned_integer (buf, 2, byte_order);
08099     }
08100   else
08101     {
08102       gdb_byte buf[1];
08103 
08104       target_read_memory (rn_val + rm_val, buf, 1);
08105       halfwords = extract_unsigned_integer (buf, 1, byte_order);
08106     }
08107 
08108   if (debug_displaced)
08109     fprintf_unfiltered (gdb_stdlog, "displaced: %s base 0x%x offset 0x%x"
08110                         " offset 0x%x\n", is_tbh ? "tbh" : "tbb",
08111                         (unsigned int) rn_val, (unsigned int) rm_val,
08112                         (unsigned int) halfwords);
08113 
08114   dsc->u.branch.cond = INST_AL;
08115   dsc->u.branch.link = 0;
08116   dsc->u.branch.exchange = 0;
08117   dsc->u.branch.dest = dsc->insn_addr + 4 + 2 * halfwords;
08118 
08119   dsc->cleanup = &cleanup_branch;
08120 
08121   return 0;
08122 }
08123 
08124 static void
08125 cleanup_pop_pc_16bit_all (struct gdbarch *gdbarch, struct regcache *regs,
08126                           struct displaced_step_closure *dsc)
08127 {
08128   /* PC <- r7 */
08129   int val = displaced_read_reg (regs, dsc, 7);
08130   displaced_write_reg (regs, dsc, ARM_PC_REGNUM, val, BX_WRITE_PC);
08131 
08132   /* r7 <- r8 */
08133   val = displaced_read_reg (regs, dsc, 8);
08134   displaced_write_reg (regs, dsc, 7, val, CANNOT_WRITE_PC);
08135 
08136   /* r8 <- tmp[0] */
08137   displaced_write_reg (regs, dsc, 8, dsc->tmp[0], CANNOT_WRITE_PC);
08138 
08139 }
08140 
08141 static int
08142 thumb_copy_pop_pc_16bit (struct gdbarch *gdbarch, unsigned short insn1,
08143                          struct regcache *regs,
08144                          struct displaced_step_closure *dsc)
08145 {
08146   dsc->u.block.regmask = insn1 & 0x00ff;
08147 
08148   /* Rewrite instruction: POP {rX, rY, ...,rZ, PC}
08149      to :
08150 
08151      (1) register list is full, that is, r0-r7 are used.
08152      Prepare: tmp[0] <- r8
08153 
08154      POP {r0, r1, ...., r6, r7}; remove PC from reglist
08155      MOV r8, r7; Move value of r7 to r8;
08156      POP {r7}; Store PC value into r7.
08157 
08158      Cleanup: PC <- r7, r7 <- r8, r8 <-tmp[0]
08159 
08160      (2) register list is not full, supposing there are N registers in
08161      register list (except PC, 0 <= N <= 7).
08162      Prepare: for each i, 0 - N, tmp[i] <- ri.
08163 
08164      POP {r0, r1, ...., rN};
08165 
08166      Cleanup: Set registers in original reglist from r0 - rN.  Restore r0 - rN
08167      from tmp[] properly.
08168   */
08169   if (debug_displaced)
08170     fprintf_unfiltered (gdb_stdlog,
08171                         "displaced: copying thumb pop {%.8x, pc} insn %.4x\n",
08172                         dsc->u.block.regmask, insn1);
08173 
08174   if (dsc->u.block.regmask == 0xff)
08175     {
08176       dsc->tmp[0] = displaced_read_reg (regs, dsc, 8);
08177 
08178       dsc->modinsn[0] = (insn1 & 0xfeff); /* POP {r0,r1,...,r6, r7} */
08179       dsc->modinsn[1] = 0x46b8; /* MOV r8, r7 */
08180       dsc->modinsn[2] = 0xbc80; /* POP {r7} */
08181 
08182       dsc->numinsns = 3;
08183       dsc->cleanup = &cleanup_pop_pc_16bit_all;
08184     }
08185   else
08186     {
08187       unsigned int num_in_list = bitcount (dsc->u.block.regmask);
08188       unsigned int new_regmask, bit = 1;
08189       unsigned int to = 0, from = 0, i, new_rn;
08190 
08191       for (i = 0; i < num_in_list + 1; i++)
08192         dsc->tmp[i] = displaced_read_reg (regs, dsc, i);
08193 
08194       new_regmask = (1 << (num_in_list + 1)) - 1;
08195 
08196       if (debug_displaced)
08197         fprintf_unfiltered (gdb_stdlog, _("displaced: POP "
08198                                           "{..., pc}: original reg list %.4x,"
08199                                           " modified list %.4x\n"),
08200                             (int) dsc->u.block.regmask, new_regmask);
08201 
08202       dsc->u.block.regmask |= 0x8000;
08203       dsc->u.block.writeback = 0;
08204       dsc->u.block.cond = INST_AL;
08205 
08206       dsc->modinsn[0] = (insn1 & ~0x1ff) | (new_regmask & 0xff);
08207 
08208       dsc->cleanup = &cleanup_block_load_pc;
08209     }
08210 
08211   return 0;
08212 }
08213 
08214 static void
08215 thumb_process_displaced_16bit_insn (struct gdbarch *gdbarch, uint16_t insn1,
08216                                     struct regcache *regs,
08217                                     struct displaced_step_closure *dsc)
08218 {
08219   unsigned short op_bit_12_15 = bits (insn1, 12, 15);
08220   unsigned short op_bit_10_11 = bits (insn1, 10, 11);
08221   int err = 0;
08222 
08223   /* 16-bit thumb instructions.  */
08224   switch (op_bit_12_15)
08225     {
08226       /* Shift (imme), add, subtract, move and compare.  */
08227     case 0: case 1: case 2: case 3:
08228       err = thumb_copy_unmodified_16bit (gdbarch, insn1,
08229                                          "shift/add/sub/mov/cmp",
08230                                          dsc);
08231       break;
08232     case 4:
08233       switch (op_bit_10_11)
08234         {
08235         case 0: /* Data-processing */
08236           err = thumb_copy_unmodified_16bit (gdbarch, insn1,
08237                                              "data-processing",
08238                                              dsc);
08239           break;
08240         case 1: /* Special data instructions and branch and exchange.  */
08241           {
08242             unsigned short op = bits (insn1, 7, 9);
08243             if (op == 6 || op == 7) /* BX or BLX */
08244               err = thumb_copy_bx_blx_reg (gdbarch, insn1, regs, dsc);
08245             else if (bits (insn1, 6, 7) != 0) /* ADD/MOV/CMP high registers.  */
08246               err = thumb_copy_alu_reg (gdbarch, insn1, regs, dsc);
08247             else
08248               err = thumb_copy_unmodified_16bit (gdbarch, insn1, "special data",
08249                                                  dsc);
08250           }
08251           break;
08252         default: /* LDR (literal) */
08253           err = thumb_copy_16bit_ldr_literal (gdbarch, insn1, regs, dsc);
08254         }
08255       break;
08256     case 5: case 6: case 7: case 8: case 9: /* Load/Store single data item */
08257       err = thumb_copy_unmodified_16bit (gdbarch, insn1, "ldr/str", dsc);
08258       break;
08259     case 10:
08260       if (op_bit_10_11 < 2) /* Generate PC-relative address */
08261         err = thumb_decode_pc_relative_16bit (gdbarch, insn1, regs, dsc);
08262       else /* Generate SP-relative address */
08263         err = thumb_copy_unmodified_16bit (gdbarch, insn1, "sp-relative", dsc);
08264       break;
08265     case 11: /* Misc 16-bit instructions */
08266       {
08267         switch (bits (insn1, 8, 11))
08268           {
08269           case 1: case 3:  case 9: case 11: /* CBNZ, CBZ */
08270             err = thumb_copy_cbnz_cbz (gdbarch, insn1, regs, dsc);
08271             break;
08272           case 12: case 13: /* POP */
08273             if (bit (insn1, 8)) /* PC is in register list.  */
08274               err = thumb_copy_pop_pc_16bit (gdbarch, insn1, regs, dsc);
08275             else
08276               err = thumb_copy_unmodified_16bit (gdbarch, insn1, "pop", dsc);
08277             break;
08278           case 15: /* If-Then, and hints */
08279             if (bits (insn1, 0, 3))
08280               /* If-Then makes up to four following instructions conditional.
08281                  IT instruction itself is not conditional, so handle it as a
08282                  common unmodified instruction.  */
08283               err = thumb_copy_unmodified_16bit (gdbarch, insn1, "If-Then",
08284                                                  dsc);
08285             else
08286               err = thumb_copy_unmodified_16bit (gdbarch, insn1, "hints", dsc);
08287             break;
08288           default:
08289             err = thumb_copy_unmodified_16bit (gdbarch, insn1, "misc", dsc);
08290           }
08291       }
08292       break;
08293     case 12:
08294       if (op_bit_10_11 < 2) /* Store multiple registers */
08295         err = thumb_copy_unmodified_16bit (gdbarch, insn1, "stm", dsc);
08296       else /* Load multiple registers */
08297         err = thumb_copy_unmodified_16bit (gdbarch, insn1, "ldm", dsc);
08298       break;
08299     case 13: /* Conditional branch and supervisor call */
08300       if (bits (insn1, 9, 11) != 7) /* conditional branch */
08301         err = thumb_copy_b (gdbarch, insn1, dsc);
08302       else
08303         err = thumb_copy_svc (gdbarch, insn1, regs, dsc);
08304       break;
08305     case 14: /* Unconditional branch */
08306       err = thumb_copy_b (gdbarch, insn1, dsc);
08307       break;
08308     default:
08309       err = 1;
08310     }
08311 
08312   if (err)
08313     internal_error (__FILE__, __LINE__,
08314                     _("thumb_process_displaced_16bit_insn: Instruction decode error"));
08315 }
08316 
08317 static int
08318 decode_thumb_32bit_ld_mem_hints (struct gdbarch *gdbarch,
08319                                  uint16_t insn1, uint16_t insn2,
08320                                  struct regcache *regs,
08321                                  struct displaced_step_closure *dsc)
08322 {
08323   int rt = bits (insn2, 12, 15);
08324   int rn = bits (insn1, 0, 3);
08325   int op1 = bits (insn1, 7, 8);
08326   int err = 0;
08327 
08328   switch (bits (insn1, 5, 6))
08329     {
08330     case 0: /* Load byte and memory hints */
08331       if (rt == 0xf) /* PLD/PLI */
08332         {
08333           if (rn == 0xf)
08334             /* PLD literal or Encoding T3 of PLI(immediate, literal).  */
08335             return thumb2_copy_preload (gdbarch, insn1, insn2, regs, dsc);
08336           else
08337             return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
08338                                                 "pli/pld", dsc);
08339         }
08340       else
08341         {
08342           if (rn == 0xf) /* LDRB/LDRSB (literal) */
08343             return thumb2_copy_load_literal (gdbarch, insn1, insn2, regs, dsc,
08344                                              1);
08345           else
08346             return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
08347                                                 "ldrb{reg, immediate}/ldrbt",
08348                                                 dsc);
08349         }
08350 
08351       break;
08352     case 1: /* Load halfword and memory hints.  */
08353       if (rt == 0xf) /* PLD{W} and Unalloc memory hint.  */
08354         return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
08355                                             "pld/unalloc memhint", dsc);
08356       else
08357         {
08358           if (rn == 0xf)
08359             return thumb2_copy_load_literal (gdbarch, insn1, insn2, regs, dsc,
08360                                              2);
08361           else
08362             return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
08363                                                 "ldrh/ldrht", dsc);
08364         }
08365       break;
08366     case 2: /* Load word */
08367       {
08368         int insn2_bit_8_11 = bits (insn2, 8, 11);
08369 
08370         if (rn == 0xf)
08371           return thumb2_copy_load_literal (gdbarch, insn1, insn2, regs, dsc, 4);
08372         else if (op1 == 0x1) /* Encoding T3 */
08373           return thumb2_copy_load_reg_imm (gdbarch, insn1, insn2, regs, dsc,
08374                                            0, 1);
08375         else /* op1 == 0x0 */
08376           {
08377             if (insn2_bit_8_11 == 0xc || (insn2_bit_8_11 & 0x9) == 0x9)
08378               /* LDR (immediate) */
08379               return thumb2_copy_load_reg_imm (gdbarch, insn1, insn2, regs,
08380                                                dsc, bit (insn2, 8), 1);
08381             else if (insn2_bit_8_11 == 0xe) /* LDRT */
08382               return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
08383                                                   "ldrt", dsc);
08384             else
08385               /* LDR (register) */
08386               return thumb2_copy_load_reg_imm (gdbarch, insn1, insn2, regs,
08387                                                dsc, 0, 0);
08388           }
08389         break;
08390       }
08391     default:
08392       return thumb_32bit_copy_undef (gdbarch, insn1, insn2, dsc);
08393       break;
08394     }
08395   return 0;
08396 }
08397 
08398 static void
08399 thumb_process_displaced_32bit_insn (struct gdbarch *gdbarch, uint16_t insn1,
08400                                     uint16_t insn2, struct regcache *regs,
08401                                     struct displaced_step_closure *dsc)
08402 {
08403   int err = 0;
08404   unsigned short op = bit (insn2, 15);
08405   unsigned int op1 = bits (insn1, 11, 12);
08406 
08407   switch (op1)
08408     {
08409     case 1:
08410       {
08411         switch (bits (insn1, 9, 10))
08412           {
08413           case 0:
08414             if (bit (insn1, 6))
08415               {
08416                 /* Load/store {dual, execlusive}, table branch.  */
08417                 if (bits (insn1, 7, 8) == 1 && bits (insn1, 4, 5) == 1
08418                     && bits (insn2, 5, 7) == 0)
08419                   err = thumb2_copy_table_branch (gdbarch, insn1, insn2, regs,
08420                                                   dsc);
08421                 else
08422                   /* PC is not allowed to use in load/store {dual, exclusive}
08423                      instructions.  */
08424                   err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
08425                                                      "load/store dual/ex", dsc);
08426               }
08427             else /* load/store multiple */
08428               {
08429                 switch (bits (insn1, 7, 8))
08430                   {
08431                   case 0: case 3: /* SRS, RFE */
08432                     err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
08433                                                        "srs/rfe", dsc);
08434                     break;
08435                   case 1: case 2: /* LDM/STM/PUSH/POP */
08436                     err = thumb2_copy_block_xfer (gdbarch, insn1, insn2, regs, dsc);
08437                     break;
08438                   }
08439               }
08440             break;
08441 
08442           case 1:
08443             /* Data-processing (shift register).  */
08444             err = thumb2_decode_dp_shift_reg (gdbarch, insn1, insn2, regs,
08445                                               dsc);
08446             break;
08447           default: /* Coprocessor instructions.  */
08448             err = thumb2_decode_svc_copro (gdbarch, insn1, insn2, regs, dsc);
08449             break;
08450           }
08451       break;
08452       }
08453     case 2: /* op1 = 2 */
08454       if (op) /* Branch and misc control.  */
08455         {
08456           if (bit (insn2, 14)  /* BLX/BL */
08457               || bit (insn2, 12) /* Unconditional branch */
08458               || (bits (insn1, 7, 9) != 0x7)) /* Conditional branch */
08459             err = thumb2_copy_b_bl_blx (gdbarch, insn1, insn2, regs, dsc);
08460           else
08461             err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
08462                                                "misc ctrl", dsc);
08463         }
08464       else
08465         {
08466           if (bit (insn1, 9)) /* Data processing (plain binary imm).  */
08467             {
08468               int op = bits (insn1, 4, 8);
08469               int rn = bits (insn1, 0, 3);
08470               if ((op == 0 || op == 0xa) && rn == 0xf)
08471                 err = thumb_copy_pc_relative_32bit (gdbarch, insn1, insn2,
08472                                                     regs, dsc);
08473               else
08474                 err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
08475                                                    "dp/pb", dsc);
08476             }
08477           else /* Data processing (modified immeidate) */
08478             err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
08479                                                "dp/mi", dsc);
08480         }
08481       break;
08482     case 3: /* op1 = 3 */
08483       switch (bits (insn1, 9, 10))
08484         {
08485         case 0:
08486           if (bit (insn1, 4))
08487             err = decode_thumb_32bit_ld_mem_hints (gdbarch, insn1, insn2,
08488                                                    regs, dsc);
08489           else /* NEON Load/Store and Store single data item */
08490             err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
08491                                                "neon elt/struct load/store",
08492                                                dsc);
08493           break;
08494         case 1: /* op1 = 3, bits (9, 10) == 1 */
08495           switch (bits (insn1, 7, 8))
08496             {
08497             case 0: case 1: /* Data processing (register) */
08498               err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
08499                                                  "dp(reg)", dsc);
08500               break;
08501             case 2: /* Multiply and absolute difference */
08502               err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
08503                                                  "mul/mua/diff", dsc);
08504               break;
08505             case 3: /* Long multiply and divide */
08506               err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
08507                                                  "lmul/lmua", dsc);
08508               break;
08509             }
08510           break;
08511         default: /* Coprocessor instructions */
08512           err = thumb2_decode_svc_copro (gdbarch, insn1, insn2, regs, dsc);
08513           break;
08514         }
08515       break;
08516     default:
08517       err = 1;
08518     }
08519 
08520   if (err)
08521     internal_error (__FILE__, __LINE__,
08522                     _("thumb_process_displaced_32bit_insn: Instruction decode error"));
08523 
08524 }
08525 
08526 static void
08527 thumb_process_displaced_insn (struct gdbarch *gdbarch, CORE_ADDR from,
08528                               CORE_ADDR to, struct regcache *regs,
08529                               struct displaced_step_closure *dsc)
08530 {
08531   enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
08532   uint16_t insn1
08533     = read_memory_unsigned_integer (from, 2, byte_order_for_code);
08534 
08535   if (debug_displaced)
08536     fprintf_unfiltered (gdb_stdlog, "displaced: process thumb insn %.4x "
08537                         "at %.8lx\n", insn1, (unsigned long) from);
08538 
08539   dsc->is_thumb = 1;
08540   dsc->insn_size = thumb_insn_size (insn1);
08541   if (thumb_insn_size (insn1) == 4)
08542     {
08543       uint16_t insn2
08544         = read_memory_unsigned_integer (from + 2, 2, byte_order_for_code);
08545       thumb_process_displaced_32bit_insn (gdbarch, insn1, insn2, regs, dsc);
08546     }
08547   else
08548     thumb_process_displaced_16bit_insn (gdbarch, insn1, regs, dsc);
08549 }
08550 
08551 void
08552 arm_process_displaced_insn (struct gdbarch *gdbarch, CORE_ADDR from,
08553                             CORE_ADDR to, struct regcache *regs,
08554                             struct displaced_step_closure *dsc)
08555 {
08556   int err = 0;
08557   enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
08558   uint32_t insn;
08559 
08560   /* Most displaced instructions use a 1-instruction scratch space, so set this
08561      here and override below if/when necessary.  */
08562   dsc->numinsns = 1;
08563   dsc->insn_addr = from;
08564   dsc->scratch_base = to;
08565   dsc->cleanup = NULL;
08566   dsc->wrote_to_pc = 0;
08567 
08568   if (!displaced_in_arm_mode (regs))
08569     return thumb_process_displaced_insn (gdbarch, from, to, regs, dsc);
08570 
08571   dsc->is_thumb = 0;
08572   dsc->insn_size = 4;
08573   insn = read_memory_unsigned_integer (from, 4, byte_order_for_code);
08574   if (debug_displaced)
08575     fprintf_unfiltered (gdb_stdlog, "displaced: stepping insn %.8lx "
08576                         "at %.8lx\n", (unsigned long) insn,
08577                         (unsigned long) from);
08578 
08579   if ((insn & 0xf0000000) == 0xf0000000)
08580     err = arm_decode_unconditional (gdbarch, insn, regs, dsc);
08581   else switch (((insn & 0x10) >> 4) | ((insn & 0xe000000) >> 24))
08582     {
08583     case 0x0: case 0x1: case 0x2: case 0x3:
08584       err = arm_decode_dp_misc (gdbarch, insn, regs, dsc);
08585       break;
08586 
08587     case 0x4: case 0x5: case 0x6:
08588       err = arm_decode_ld_st_word_ubyte (gdbarch, insn, regs, dsc);
08589       break;
08590 
08591     case 0x7:
08592       err = arm_decode_media (gdbarch, insn, dsc);
08593       break;
08594 
08595     case 0x8: case 0x9: case 0xa: case 0xb:
08596       err = arm_decode_b_bl_ldmstm (gdbarch, insn, regs, dsc);
08597       break;
08598 
08599     case 0xc: case 0xd: case 0xe: case 0xf:
08600       err = arm_decode_svc_copro (gdbarch, insn, to, regs, dsc);
08601       break;
08602     }
08603 
08604   if (err)
08605     internal_error (__FILE__, __LINE__,
08606                     _("arm_process_displaced_insn: Instruction decode error"));
08607 }
08608 
08609 /* Actually set up the scratch space for a displaced instruction.  */
08610 
08611 void
08612 arm_displaced_init_closure (struct gdbarch *gdbarch, CORE_ADDR from,
08613                             CORE_ADDR to, struct displaced_step_closure *dsc)
08614 {
08615   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
08616   unsigned int i, len, offset;
08617   enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
08618   int size = dsc->is_thumb? 2 : 4;
08619   const gdb_byte *bkp_insn;
08620 
08621   offset = 0;
08622   /* Poke modified instruction(s).  */
08623   for (i = 0; i < dsc->numinsns; i++)
08624     {
08625       if (debug_displaced)
08626         {
08627           fprintf_unfiltered (gdb_stdlog, "displaced: writing insn ");
08628           if (size == 4)
08629             fprintf_unfiltered (gdb_stdlog, "%.8lx",
08630                                 dsc->modinsn[i]);
08631           else if (size == 2)
08632             fprintf_unfiltered (gdb_stdlog, "%.4x",
08633                                 (unsigned short)dsc->modinsn[i]);
08634 
08635           fprintf_unfiltered (gdb_stdlog, " at %.8lx\n",
08636                               (unsigned long) to + offset);
08637 
08638         }
08639       write_memory_unsigned_integer (to + offset, size,
08640                                      byte_order_for_code,
08641                                      dsc->modinsn[i]);
08642       offset += size;
08643     }
08644 
08645   /* Choose the correct breakpoint instruction.  */
08646   if (dsc->is_thumb)
08647     {
08648       bkp_insn = tdep->thumb_breakpoint;
08649       len = tdep->thumb_breakpoint_size;
08650     }
08651   else
08652     {
08653       bkp_insn = tdep->arm_breakpoint;
08654       len = tdep->arm_breakpoint_size;
08655     }
08656 
08657   /* Put breakpoint afterwards.  */
08658   write_memory (to + offset, bkp_insn, len);
08659 
08660   if (debug_displaced)
08661     fprintf_unfiltered (gdb_stdlog, "displaced: copy %s->%s: ",
08662                         paddress (gdbarch, from), paddress (gdbarch, to));
08663 }
08664 
08665 /* Entry point for copying an instruction into scratch space for displaced
08666    stepping.  */
08667 
08668 struct displaced_step_closure *
08669 arm_displaced_step_copy_insn (struct gdbarch *gdbarch,
08670                               CORE_ADDR from, CORE_ADDR to,
08671                               struct regcache *regs)
08672 {
08673   struct displaced_step_closure *dsc
08674     = xmalloc (sizeof (struct displaced_step_closure));
08675   arm_process_displaced_insn (gdbarch, from, to, regs, dsc);
08676   arm_displaced_init_closure (gdbarch, from, to, dsc);
08677 
08678   return dsc;
08679 }
08680 
08681 /* Entry point for cleaning things up after a displaced instruction has been
08682    single-stepped.  */
08683 
08684 void
08685 arm_displaced_step_fixup (struct gdbarch *gdbarch,
08686                           struct displaced_step_closure *dsc,
08687                           CORE_ADDR from, CORE_ADDR to,
08688                           struct regcache *regs)
08689 {
08690   if (dsc->cleanup)
08691     dsc->cleanup (gdbarch, regs, dsc);
08692 
08693   if (!dsc->wrote_to_pc)
08694     regcache_cooked_write_unsigned (regs, ARM_PC_REGNUM,
08695                                     dsc->insn_addr + dsc->insn_size);
08696 
08697 }
08698 
08699 #include "bfd-in2.h"
08700 #include "libcoff.h"
08701 
08702 static int
08703 gdb_print_insn_arm (bfd_vma memaddr, disassemble_info *info)
08704 {
08705   struct gdbarch *gdbarch = info->application_data;
08706 
08707   if (arm_pc_is_thumb (gdbarch, memaddr))
08708     {
08709       static asymbol *asym;
08710       static combined_entry_type ce;
08711       static struct coff_symbol_struct csym;
08712       static struct bfd fake_bfd;
08713       static bfd_target fake_target;
08714 
08715       if (csym.native == NULL)
08716         {
08717           /* Create a fake symbol vector containing a Thumb symbol.
08718              This is solely so that the code in print_insn_little_arm() 
08719              and print_insn_big_arm() in opcodes/arm-dis.c will detect
08720              the presence of a Thumb symbol and switch to decoding
08721              Thumb instructions.  */
08722 
08723           fake_target.flavour = bfd_target_coff_flavour;
08724           fake_bfd.xvec = &fake_target;
08725           ce.u.syment.n_sclass = C_THUMBEXTFUNC;
08726           csym.native = &ce;
08727           csym.symbol.the_bfd = &fake_bfd;
08728           csym.symbol.name = "fake";
08729           asym = (asymbol *) & csym;
08730         }
08731 
08732       memaddr = UNMAKE_THUMB_ADDR (memaddr);
08733       info->symbols = &asym;
08734     }
08735   else
08736     info->symbols = NULL;
08737 
08738   if (info->endian == BFD_ENDIAN_BIG)
08739     return print_insn_big_arm (memaddr, info);
08740   else
08741     return print_insn_little_arm (memaddr, info);
08742 }
08743 
08744 /* The following define instruction sequences that will cause ARM
08745    cpu's to take an undefined instruction trap.  These are used to
08746    signal a breakpoint to GDB.
08747    
08748    The newer ARMv4T cpu's are capable of operating in ARM or Thumb
08749    modes.  A different instruction is required for each mode.  The ARM
08750    cpu's can also be big or little endian.  Thus four different
08751    instructions are needed to support all cases.
08752    
08753    Note: ARMv4 defines several new instructions that will take the
08754    undefined instruction trap.  ARM7TDMI is nominally ARMv4T, but does
08755    not in fact add the new instructions.  The new undefined
08756    instructions in ARMv4 are all instructions that had no defined
08757    behaviour in earlier chips.  There is no guarantee that they will
08758    raise an exception, but may be treated as NOP's.  In practice, it
08759    may only safe to rely on instructions matching:
08760    
08761    3 3 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1 
08762    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
08763    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
08764    
08765    Even this may only true if the condition predicate is true.  The
08766    following use a condition predicate of ALWAYS so it is always TRUE.
08767    
08768    There are other ways of forcing a breakpoint.  GNU/Linux, RISC iX,
08769    and NetBSD all use a software interrupt rather than an undefined
08770    instruction to force a trap.  This can be handled by by the
08771    abi-specific code during establishment of the gdbarch vector.  */
08772 
08773 #define ARM_LE_BREAKPOINT {0xFE,0xDE,0xFF,0xE7}
08774 #define ARM_BE_BREAKPOINT {0xE7,0xFF,0xDE,0xFE}
08775 #define THUMB_LE_BREAKPOINT {0xbe,0xbe}
08776 #define THUMB_BE_BREAKPOINT {0xbe,0xbe}
08777 
08778 static const gdb_byte arm_default_arm_le_breakpoint[] = ARM_LE_BREAKPOINT;
08779 static const gdb_byte arm_default_arm_be_breakpoint[] = ARM_BE_BREAKPOINT;
08780 static const gdb_byte arm_default_thumb_le_breakpoint[] = THUMB_LE_BREAKPOINT;
08781 static const gdb_byte arm_default_thumb_be_breakpoint[] = THUMB_BE_BREAKPOINT;
08782 
08783 /* Determine the type and size of breakpoint to insert at PCPTR.  Uses
08784    the program counter value to determine whether a 16-bit or 32-bit
08785    breakpoint should be used.  It returns a pointer to a string of
08786    bytes that encode a breakpoint instruction, stores the length of
08787    the string to *lenptr, and adjusts the program counter (if
08788    necessary) to point to the actual memory location where the
08789    breakpoint should be inserted.  */
08790 
08791 static const unsigned char *
08792 arm_breakpoint_from_pc (struct gdbarch *gdbarch, CORE_ADDR *pcptr, int *lenptr)
08793 {
08794   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
08795   enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
08796 
08797   if (arm_pc_is_thumb (gdbarch, *pcptr))
08798     {
08799       *pcptr = UNMAKE_THUMB_ADDR (*pcptr);
08800 
08801       /* If we have a separate 32-bit breakpoint instruction for Thumb-2,
08802          check whether we are replacing a 32-bit instruction.  */
08803       if (tdep->thumb2_breakpoint != NULL)
08804         {
08805           gdb_byte buf[2];
08806           if (target_read_memory (*pcptr, buf, 2) == 0)
08807             {
08808               unsigned short inst1;
08809               inst1 = extract_unsigned_integer (buf, 2, byte_order_for_code);
08810               if (thumb_insn_size (inst1) == 4)
08811                 {
08812                   *lenptr = tdep->thumb2_breakpoint_size;
08813                   return tdep->thumb2_breakpoint;
08814                 }
08815             }
08816         }
08817 
08818       *lenptr = tdep->thumb_breakpoint_size;
08819       return tdep->thumb_breakpoint;
08820     }
08821   else
08822     {
08823       *lenptr = tdep->arm_breakpoint_size;
08824       return tdep->arm_breakpoint;
08825     }
08826 }
08827 
08828 static void
08829 arm_remote_breakpoint_from_pc (struct gdbarch *gdbarch, CORE_ADDR *pcptr,
08830                                int *kindptr)
08831 {
08832   arm_breakpoint_from_pc (gdbarch, pcptr, kindptr);
08833 
08834   if (arm_pc_is_thumb (gdbarch, *pcptr) && *kindptr == 4)
08835     /* The documented magic value for a 32-bit Thumb-2 breakpoint, so
08836        that this is not confused with a 32-bit ARM breakpoint.  */
08837     *kindptr = 3;
08838 }
08839 
08840 /* Extract from an array REGBUF containing the (raw) register state a
08841    function return value of type TYPE, and copy that, in virtual
08842    format, into VALBUF.  */
08843 
08844 static void
08845 arm_extract_return_value (struct type *type, struct regcache *regs,
08846                           gdb_byte *valbuf)
08847 {
08848   struct gdbarch *gdbarch = get_regcache_arch (regs);
08849   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
08850 
08851   if (TYPE_CODE_FLT == TYPE_CODE (type))
08852     {
08853       switch (gdbarch_tdep (gdbarch)->fp_model)
08854         {
08855         case ARM_FLOAT_FPA:
08856           {
08857             /* The value is in register F0 in internal format.  We need to
08858                extract the raw value and then convert it to the desired
08859                internal type.  */
08860             bfd_byte tmpbuf[FP_REGISTER_SIZE];
08861 
08862             regcache_cooked_read (regs, ARM_F0_REGNUM, tmpbuf);
08863             convert_from_extended (floatformat_from_type (type), tmpbuf,
08864                                    valbuf, gdbarch_byte_order (gdbarch));
08865           }
08866           break;
08867 
08868         case ARM_FLOAT_SOFT_FPA:
08869         case ARM_FLOAT_SOFT_VFP:
08870           /* ARM_FLOAT_VFP can arise if this is a variadic function so
08871              not using the VFP ABI code.  */
08872         case ARM_FLOAT_VFP:
08873           regcache_cooked_read (regs, ARM_A1_REGNUM, valbuf);
08874           if (TYPE_LENGTH (type) > 4)
08875             regcache_cooked_read (regs, ARM_A1_REGNUM + 1,
08876                                   valbuf + INT_REGISTER_SIZE);
08877           break;
08878 
08879         default:
08880           internal_error (__FILE__, __LINE__,
08881                           _("arm_extract_return_value: "
08882                             "Floating point model not supported"));
08883           break;
08884         }
08885     }
08886   else if (TYPE_CODE (type) == TYPE_CODE_INT
08887            || TYPE_CODE (type) == TYPE_CODE_CHAR
08888            || TYPE_CODE (type) == TYPE_CODE_BOOL
08889            || TYPE_CODE (type) == TYPE_CODE_PTR
08890            || TYPE_CODE (type) == TYPE_CODE_REF
08891            || TYPE_CODE (type) == TYPE_CODE_ENUM)
08892     {
08893       /* If the type is a plain integer, then the access is
08894          straight-forward.  Otherwise we have to play around a bit
08895          more.  */
08896       int len = TYPE_LENGTH (type);
08897       int regno = ARM_A1_REGNUM;
08898       ULONGEST tmp;
08899 
08900       while (len > 0)
08901         {
08902           /* By using store_unsigned_integer we avoid having to do
08903              anything special for small big-endian values.  */
08904           regcache_cooked_read_unsigned (regs, regno++, &tmp);
08905           store_unsigned_integer (valbuf, 
08906                                   (len > INT_REGISTER_SIZE
08907                                    ? INT_REGISTER_SIZE : len),
08908                                   byte_order, tmp);
08909           len -= INT_REGISTER_SIZE;
08910           valbuf += INT_REGISTER_SIZE;
08911         }
08912     }
08913   else
08914     {
08915       /* For a structure or union the behaviour is as if the value had
08916          been stored to word-aligned memory and then loaded into 
08917          registers with 32-bit load instruction(s).  */
08918       int len = TYPE_LENGTH (type);
08919       int regno = ARM_A1_REGNUM;
08920       bfd_byte tmpbuf[INT_REGISTER_SIZE];
08921 
08922       while (len > 0)
08923         {
08924           regcache_cooked_read (regs, regno++, tmpbuf);
08925           memcpy (valbuf, tmpbuf,
08926                   len > INT_REGISTER_SIZE ? INT_REGISTER_SIZE : len);
08927           len -= INT_REGISTER_SIZE;
08928           valbuf += INT_REGISTER_SIZE;
08929         }
08930     }
08931 }
08932 
08933 
08934 /* Will a function return an aggregate type in memory or in a
08935    register?  Return 0 if an aggregate type can be returned in a
08936    register, 1 if it must be returned in memory.  */
08937 
08938 static int
08939 arm_return_in_memory (struct gdbarch *gdbarch, struct type *type)
08940 {
08941   int nRc;
08942   enum type_code code;
08943 
08944   CHECK_TYPEDEF (type);
08945 
08946   /* In the ARM ABI, "integer" like aggregate types are returned in
08947      registers.  For an aggregate type to be integer like, its size
08948      must be less than or equal to INT_REGISTER_SIZE and the
08949      offset of each addressable subfield must be zero.  Note that bit
08950      fields are not addressable, and all addressable subfields of
08951      unions always start at offset zero.
08952 
08953      This function is based on the behaviour of GCC 2.95.1.
08954      See: gcc/arm.c: arm_return_in_memory() for details.
08955 
08956      Note: All versions of GCC before GCC 2.95.2 do not set up the
08957      parameters correctly for a function returning the following
08958      structure: struct { float f;}; This should be returned in memory,
08959      not a register.  Richard Earnshaw sent me a patch, but I do not
08960      know of any way to detect if a function like the above has been
08961      compiled with the correct calling convention.  */
08962 
08963   /* All aggregate types that won't fit in a register must be returned
08964      in memory.  */
08965   if (TYPE_LENGTH (type) > INT_REGISTER_SIZE)
08966     {
08967       return 1;
08968     }
08969 
08970   /* The AAPCS says all aggregates not larger than a word are returned
08971      in a register.  */
08972   if (gdbarch_tdep (gdbarch)->arm_abi != ARM_ABI_APCS)
08973     return 0;
08974 
08975   /* The only aggregate types that can be returned in a register are
08976      structs and unions.  Arrays must be returned in memory.  */
08977   code = TYPE_CODE (type);
08978   if ((TYPE_CODE_STRUCT != code) && (TYPE_CODE_UNION != code))
08979     {
08980       return 1;
08981     }
08982 
08983   /* Assume all other aggregate types can be returned in a register.
08984      Run a check for structures, unions and arrays.  */
08985   nRc = 0;
08986 
08987   if ((TYPE_CODE_STRUCT == code) || (TYPE_CODE_UNION == code))
08988     {
08989       int i;
08990       /* Need to check if this struct/union is "integer" like.  For
08991          this to be true, its size must be less than or equal to
08992          INT_REGISTER_SIZE and the offset of each addressable
08993          subfield must be zero.  Note that bit fields are not
08994          addressable, and unions always start at offset zero.  If any
08995          of the subfields is a floating point type, the struct/union
08996          cannot be an integer type.  */
08997 
08998       /* For each field in the object, check:
08999          1) Is it FP? --> yes, nRc = 1;
09000          2) Is it addressable (bitpos != 0) and
09001          not packed (bitsize == 0)?
09002          --> yes, nRc = 1  
09003        */
09004 
09005       for (i = 0; i < TYPE_NFIELDS (type); i++)
09006         {
09007           enum type_code field_type_code;
09008           field_type_code = TYPE_CODE (check_typedef (TYPE_FIELD_TYPE (type,
09009                                                                        i)));
09010 
09011           /* Is it a floating point type field?  */
09012           if (field_type_code == TYPE_CODE_FLT)
09013             {
09014               nRc = 1;
09015               break;
09016             }
09017 
09018           /* If bitpos != 0, then we have to care about it.  */
09019           if (TYPE_FIELD_BITPOS (type, i) != 0)
09020             {
09021               /* Bitfields are not addressable.  If the field bitsize is 
09022                  zero, then the field is not packed.  Hence it cannot be
09023                  a bitfield or any other packed type.  */
09024               if (TYPE_FIELD_BITSIZE (type, i) == 0)
09025                 {
09026                   nRc = 1;
09027                   break;
09028                 }
09029             }
09030         }
09031     }
09032 
09033   return nRc;
09034 }
09035 
09036 /* Write into appropriate registers a function return value of type
09037    TYPE, given in virtual format.  */
09038 
09039 static void
09040 arm_store_return_value (struct type *type, struct regcache *regs,
09041                         const gdb_byte *valbuf)
09042 {
09043   struct gdbarch *gdbarch = get_regcache_arch (regs);
09044   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
09045 
09046   if (TYPE_CODE (type) == TYPE_CODE_FLT)
09047     {
09048       gdb_byte buf[MAX_REGISTER_SIZE];
09049 
09050       switch (gdbarch_tdep (gdbarch)->fp_model)
09051         {
09052         case ARM_FLOAT_FPA:
09053 
09054           convert_to_extended (floatformat_from_type (type), buf, valbuf,
09055                                gdbarch_byte_order (gdbarch));
09056           regcache_cooked_write (regs, ARM_F0_REGNUM, buf);
09057           break;
09058 
09059         case ARM_FLOAT_SOFT_FPA:
09060         case ARM_FLOAT_SOFT_VFP:
09061           /* ARM_FLOAT_VFP can arise if this is a variadic function so
09062              not using the VFP ABI code.  */
09063         case ARM_FLOAT_VFP:
09064           regcache_cooked_write (regs, ARM_A1_REGNUM, valbuf);
09065           if (TYPE_LENGTH (type) > 4)
09066             regcache_cooked_write (regs, ARM_A1_REGNUM + 1, 
09067                                    valbuf + INT_REGISTER_SIZE);
09068           break;
09069 
09070         default:
09071           internal_error (__FILE__, __LINE__,
09072                           _("arm_store_return_value: Floating "
09073                             "point model not supported"));
09074           break;
09075         }
09076     }
09077   else if (TYPE_CODE (type) == TYPE_CODE_INT
09078            || TYPE_CODE (type) == TYPE_CODE_CHAR
09079            || TYPE_CODE (type) == TYPE_CODE_BOOL
09080            || TYPE_CODE (type) == TYPE_CODE_PTR
09081            || TYPE_CODE (type) == TYPE_CODE_REF
09082            || TYPE_CODE (type) == TYPE_CODE_ENUM)
09083     {
09084       if (TYPE_LENGTH (type) <= 4)
09085         {
09086           /* Values of one word or less are zero/sign-extended and
09087              returned in r0.  */
09088           bfd_byte tmpbuf[INT_REGISTER_SIZE];
09089           LONGEST val = unpack_long (type, valbuf);
09090 
09091           store_signed_integer (tmpbuf, INT_REGISTER_SIZE, byte_order, val);
09092           regcache_cooked_write (regs, ARM_A1_REGNUM, tmpbuf);
09093         }
09094       else
09095         {
09096           /* Integral values greater than one word are stored in consecutive
09097              registers starting with r0.  This will always be a multiple of
09098              the regiser size.  */
09099           int len = TYPE_LENGTH (type);
09100           int regno = ARM_A1_REGNUM;
09101 
09102           while (len > 0)
09103             {
09104               regcache_cooked_write (regs, regno++, valbuf);
09105               len -= INT_REGISTER_SIZE;
09106               valbuf += INT_REGISTER_SIZE;
09107             }
09108         }
09109     }
09110   else
09111     {
09112       /* For a structure or union the behaviour is as if the value had
09113          been stored to word-aligned memory and then loaded into 
09114          registers with 32-bit load instruction(s).  */
09115       int len = TYPE_LENGTH (type);
09116       int regno = ARM_A1_REGNUM;
09117       bfd_byte tmpbuf[INT_REGISTER_SIZE];
09118 
09119       while (len > 0)
09120         {
09121           memcpy (tmpbuf, valbuf,
09122                   len > INT_REGISTER_SIZE ? INT_REGISTER_SIZE : len);
09123           regcache_cooked_write (regs, regno++, tmpbuf);
09124           len -= INT_REGISTER_SIZE;
09125           valbuf += INT_REGISTER_SIZE;
09126         }
09127     }
09128 }
09129 
09130 
09131 /* Handle function return values.  */
09132 
09133 static enum return_value_convention
09134 arm_return_value (struct gdbarch *gdbarch, struct value *function,
09135                   struct type *valtype, struct regcache *regcache,
09136                   gdb_byte *readbuf, const gdb_byte *writebuf)
09137 {
09138   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
09139   struct type *func_type = function ? value_type (function) : NULL;
09140   enum arm_vfp_cprc_base_type vfp_base_type;
09141   int vfp_base_count;
09142 
09143   if (arm_vfp_abi_for_function (gdbarch, func_type)
09144       && arm_vfp_call_candidate (valtype, &vfp_base_type, &vfp_base_count))
09145     {
09146       int reg_char = arm_vfp_cprc_reg_char (vfp_base_type);
09147       int unit_length = arm_vfp_cprc_unit_length (vfp_base_type);
09148       int i;
09149       for (i = 0; i < vfp_base_count; i++)
09150         {
09151           if (reg_char == 'q')
09152             {
09153               if (writebuf)
09154                 arm_neon_quad_write (gdbarch, regcache, i,
09155                                      writebuf + i * unit_length);
09156 
09157               if (readbuf)
09158                 arm_neon_quad_read (gdbarch, regcache, i,
09159                                     readbuf + i * unit_length);
09160             }
09161           else
09162             {
09163               char name_buf[4];
09164               int regnum;
09165 
09166               xsnprintf (name_buf, sizeof (name_buf), "%c%d", reg_char, i);
09167               regnum = user_reg_map_name_to_regnum (gdbarch, name_buf,
09168                                                     strlen (name_buf));
09169               if (writebuf)
09170                 regcache_cooked_write (regcache, regnum,
09171                                        writebuf + i * unit_length);
09172               if (readbuf)
09173                 regcache_cooked_read (regcache, regnum,
09174                                       readbuf + i * unit_length);
09175             }
09176         }
09177       return RETURN_VALUE_REGISTER_CONVENTION;
09178     }
09179 
09180   if (TYPE_CODE (valtype) == TYPE_CODE_STRUCT
09181       || TYPE_CODE (valtype) == TYPE_CODE_UNION
09182       || TYPE_CODE (valtype) == TYPE_CODE_ARRAY)
09183     {
09184       if (tdep->struct_return == pcc_struct_return
09185           || arm_return_in_memory (gdbarch, valtype))
09186         return RETURN_VALUE_STRUCT_CONVENTION;
09187     }
09188 
09189   /* AAPCS returns complex types longer than a register in memory.  */
09190   if (tdep->arm_abi != ARM_ABI_APCS
09191       && TYPE_CODE (valtype) == TYPE_CODE_COMPLEX
09192       && TYPE_LENGTH (valtype) > INT_REGISTER_SIZE)
09193     return RETURN_VALUE_STRUCT_CONVENTION;
09194 
09195   if (writebuf)
09196     arm_store_return_value (valtype, regcache, writebuf);
09197 
09198   if (readbuf)
09199     arm_extract_return_value (valtype, regcache, readbuf);
09200 
09201   return RETURN_VALUE_REGISTER_CONVENTION;
09202 }
09203 
09204 
09205 static int
09206 arm_get_longjmp_target (struct frame_info *frame, CORE_ADDR *pc)
09207 {
09208   struct gdbarch *gdbarch = get_frame_arch (frame);
09209   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
09210   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
09211   CORE_ADDR jb_addr;
09212   gdb_byte buf[INT_REGISTER_SIZE];
09213   
09214   jb_addr = get_frame_register_unsigned (frame, ARM_A1_REGNUM);
09215 
09216   if (target_read_memory (jb_addr + tdep->jb_pc * tdep->jb_elt_size, buf,
09217                           INT_REGISTER_SIZE))
09218     return 0;
09219 
09220   *pc = extract_unsigned_integer (buf, INT_REGISTER_SIZE, byte_order);
09221   return 1;
09222 }
09223 
09224 /* Recognize GCC and GNU ld's trampolines.  If we are in a trampoline,
09225    return the target PC.  Otherwise return 0.  */
09226 
09227 CORE_ADDR
09228 arm_skip_stub (struct frame_info *frame, CORE_ADDR pc)
09229 {
09230   const char *name;
09231   int namelen;
09232   CORE_ADDR start_addr;
09233 
09234   /* Find the starting address and name of the function containing the PC.  */
09235   if (find_pc_partial_function (pc, &name, &start_addr, NULL) == 0)
09236     return 0;
09237 
09238   /* If PC is in a Thumb call or return stub, return the address of the
09239      target PC, which is in a register.  The thunk functions are called
09240      _call_via_xx, where x is the register name.  The possible names
09241      are r0-r9, sl, fp, ip, sp, and lr.  ARM RealView has similar
09242      functions, named __ARM_call_via_r[0-7].  */
09243   if (strncmp (name, "_call_via_", 10) == 0
09244       || strncmp (name, "__ARM_call_via_", strlen ("__ARM_call_via_")) == 0)
09245     {
09246       /* Use the name suffix to determine which register contains the
09247          target PC.  */
09248       static char *table[15] =
09249       {"r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
09250        "r8", "r9", "sl", "fp", "ip", "sp", "lr"
09251       };
09252       int regno;
09253       int offset = strlen (name) - 2;
09254 
09255       for (regno = 0; regno <= 14; regno++)
09256         if (strcmp (&name[offset], table[regno]) == 0)
09257           return get_frame_register_unsigned (frame, regno);
09258     }
09259 
09260   /* GNU ld generates __foo_from_arm or __foo_from_thumb for
09261      non-interworking calls to foo.  We could decode the stubs
09262      to find the target but it's easier to use the symbol table.  */
09263   namelen = strlen (name);
09264   if (name[0] == '_' && name[1] == '_'
09265       && ((namelen > 2 + strlen ("_from_thumb")
09266            && strncmp (name + namelen - strlen ("_from_thumb"), "_from_thumb",
09267                        strlen ("_from_thumb")) == 0)
09268           || (namelen > 2 + strlen ("_from_arm")
09269               && strncmp (name + namelen - strlen ("_from_arm"), "_from_arm",
09270                           strlen ("_from_arm")) == 0)))
09271     {
09272       char *target_name;
09273       int target_len = namelen - 2;
09274       struct minimal_symbol *minsym;
09275       struct objfile *objfile;
09276       struct obj_section *sec;
09277 
09278       if (name[namelen - 1] == 'b')
09279         target_len -= strlen ("_from_thumb");
09280       else
09281         target_len -= strlen ("_from_arm");
09282 
09283       target_name = alloca (target_len + 1);
09284       memcpy (target_name, name + 2, target_len);
09285       target_name[target_len] = '\0';
09286 
09287       sec = find_pc_section (pc);
09288       objfile = (sec == NULL) ? NULL : sec->objfile;
09289       minsym = lookup_minimal_symbol (target_name, NULL, objfile);
09290       if (minsym != NULL)
09291         return SYMBOL_VALUE_ADDRESS (minsym);
09292       else
09293         return 0;
09294     }
09295 
09296   return 0;                     /* not a stub */
09297 }
09298 
09299 static void
09300 set_arm_command (char *args, int from_tty)
09301 {
09302   printf_unfiltered (_("\
09303 \"set arm\" must be followed by an apporpriate subcommand.\n"));
09304   help_list (setarmcmdlist, "set arm ", all_commands, gdb_stdout);
09305 }
09306 
09307 static void
09308 show_arm_command (char *args, int from_tty)
09309 {
09310   cmd_show_list (showarmcmdlist, from_tty, "");
09311 }
09312 
09313 static void
09314 arm_update_current_architecture (void)
09315 {
09316   struct gdbarch_info info;
09317 
09318   /* If the current architecture is not ARM, we have nothing to do.  */
09319   if (gdbarch_bfd_arch_info (target_gdbarch ())->arch != bfd_arch_arm)
09320     return;
09321 
09322   /* Update the architecture.  */
09323   gdbarch_info_init (&info);
09324 
09325   if (!gdbarch_update_p (info))
09326     internal_error (__FILE__, __LINE__, _("could not update architecture"));
09327 }
09328 
09329 static void
09330 set_fp_model_sfunc (char *args, int from_tty,
09331                     struct cmd_list_element *c)
09332 {
09333   enum arm_float_model fp_model;
09334 
09335   for (fp_model = ARM_FLOAT_AUTO; fp_model != ARM_FLOAT_LAST; fp_model++)
09336     if (strcmp (current_fp_model, fp_model_strings[fp_model]) == 0)
09337       {
09338         arm_fp_model = fp_model;
09339         break;
09340       }
09341 
09342   if (fp_model == ARM_FLOAT_LAST)
09343     internal_error (__FILE__, __LINE__, _("Invalid fp model accepted: %s."),
09344                     current_fp_model);
09345 
09346   arm_update_current_architecture ();
09347 }
09348 
09349 static void
09350 show_fp_model (struct ui_file *file, int from_tty,
09351                struct cmd_list_element *c, const char *value)
09352 {
09353   struct gdbarch_tdep *tdep = gdbarch_tdep (target_gdbarch ());
09354 
09355   if (arm_fp_model == ARM_FLOAT_AUTO
09356       && gdbarch_bfd_arch_info (target_gdbarch ())->arch == bfd_arch_arm)
09357     fprintf_filtered (file, _("\
09358 The current ARM floating point model is \"auto\" (currently \"%s\").\n"),
09359                       fp_model_strings[tdep->fp_model]);
09360   else
09361     fprintf_filtered (file, _("\
09362 The current ARM floating point model is \"%s\".\n"),
09363                       fp_model_strings[arm_fp_model]);
09364 }
09365 
09366 static void
09367 arm_set_abi (char *args, int from_tty,
09368              struct cmd_list_element *c)
09369 {
09370   enum arm_abi_kind arm_abi;
09371 
09372   for (arm_abi = ARM_ABI_AUTO; arm_abi != ARM_ABI_LAST; arm_abi++)
09373     if (strcmp (arm_abi_string, arm_abi_strings[arm_abi]) == 0)
09374       {
09375         arm_abi_global = arm_abi;
09376         break;
09377       }
09378 
09379   if (arm_abi == ARM_ABI_LAST)
09380     internal_error (__FILE__, __LINE__, _("Invalid ABI accepted: %s."),
09381                     arm_abi_string);
09382 
09383   arm_update_current_architecture ();
09384 }
09385 
09386 static void
09387 arm_show_abi (struct ui_file *file, int from_tty,
09388              struct cmd_list_element *c, const char *value)
09389 {
09390   struct gdbarch_tdep *tdep = gdbarch_tdep (target_gdbarch ());
09391 
09392   if (arm_abi_global == ARM_ABI_AUTO
09393       && gdbarch_bfd_arch_info (target_gdbarch ())->arch == bfd_arch_arm)
09394     fprintf_filtered (file, _("\
09395 The current ARM ABI is \"auto\" (currently \"%s\").\n"),
09396                       arm_abi_strings[tdep->arm_abi]);
09397   else
09398     fprintf_filtered (file, _("The current ARM ABI is \"%s\".\n"),
09399                       arm_abi_string);
09400 }
09401 
09402 static void
09403 arm_show_fallback_mode (struct ui_file *file, int from_tty,
09404                         struct cmd_list_element *c, const char *value)
09405 {
09406   fprintf_filtered (file,
09407                     _("The current execution mode assumed "
09408                       "(when symbols are unavailable) is \"%s\".\n"),
09409                     arm_fallback_mode_string);
09410 }
09411 
09412 static void
09413 arm_show_force_mode (struct ui_file *file, int from_tty,
09414                      struct cmd_list_element *c, const char *value)
09415 {
09416   struct gdbarch_tdep *tdep = gdbarch_tdep (target_gdbarch ());
09417 
09418   fprintf_filtered (file,
09419                     _("The current execution mode assumed "
09420                       "(even when symbols are available) is \"%s\".\n"),
09421                     arm_force_mode_string);
09422 }
09423 
09424 /* If the user changes the register disassembly style used for info
09425    register and other commands, we have to also switch the style used
09426    in opcodes for disassembly output.  This function is run in the "set
09427    arm disassembly" command, and does that.  */
09428 
09429 static void
09430 set_disassembly_style_sfunc (char *args, int from_tty,
09431                               struct cmd_list_element *c)
09432 {
09433   set_disassembly_style ();
09434 }
09435 
09436 /* Return the ARM register name corresponding to register I.  */
09437 static const char *
09438 arm_register_name (struct gdbarch *gdbarch, int i)
09439 {
09440   const int num_regs = gdbarch_num_regs (gdbarch);
09441 
09442   if (gdbarch_tdep (gdbarch)->have_vfp_pseudos
09443       && i >= num_regs && i < num_regs + 32)
09444     {
09445       static const char *const vfp_pseudo_names[] = {
09446         "s0", "s1", "s2", "s3", "s4", "s5", "s6", "s7",
09447         "s8", "s9", "s10", "s11", "s12", "s13", "s14", "s15",
09448         "s16", "s17", "s18", "s19", "s20", "s21", "s22", "s23",
09449         "s24", "s25", "s26", "s27", "s28", "s29", "s30", "s31",
09450       };
09451 
09452       return vfp_pseudo_names[i - num_regs];
09453     }
09454 
09455   if (gdbarch_tdep (gdbarch)->have_neon_pseudos
09456       && i >= num_regs + 32 && i < num_regs + 32 + 16)
09457     {
09458       static const char *const neon_pseudo_names[] = {
09459         "q0", "q1", "q2", "q3", "q4", "q5", "q6", "q7",
09460         "q8", "q9", "q10", "q11", "q12", "q13", "q14", "q15",
09461       };
09462 
09463       return neon_pseudo_names[i - num_regs - 32];
09464     }
09465 
09466   if (i >= ARRAY_SIZE (arm_register_names))
09467     /* These registers are only supported on targets which supply
09468        an XML description.  */
09469     return "";
09470 
09471   return arm_register_names[i];
09472 }
09473 
09474 static void
09475 set_disassembly_style (void)
09476 {
09477   int current;
09478 
09479   /* Find the style that the user wants.  */
09480   for (current = 0; current < num_disassembly_options; current++)
09481     if (disassembly_style == valid_disassembly_styles[current])
09482       break;
09483   gdb_assert (current < num_disassembly_options);
09484 
09485   /* Synchronize the disassembler.  */
09486   set_arm_regname_option (current);
09487 }
09488 
09489 /* Test whether the coff symbol specific value corresponds to a Thumb
09490    function.  */
09491 
09492 static int
09493 coff_sym_is_thumb (int val)
09494 {
09495   return (val == C_THUMBEXT
09496           || val == C_THUMBSTAT
09497           || val == C_THUMBEXTFUNC
09498           || val == C_THUMBSTATFUNC
09499           || val == C_THUMBLABEL);
09500 }
09501 
09502 /* arm_coff_make_msymbol_special()
09503    arm_elf_make_msymbol_special()
09504    
09505    These functions test whether the COFF or ELF symbol corresponds to
09506    an address in thumb code, and set a "special" bit in a minimal
09507    symbol to indicate that it does.  */
09508    
09509 static void
09510 arm_elf_make_msymbol_special(asymbol *sym, struct minimal_symbol *msym)
09511 {
09512   if (ARM_SYM_BRANCH_TYPE (&((elf_symbol_type *)sym)->internal_elf_sym)
09513       == ST_BRANCH_TO_THUMB)
09514     MSYMBOL_SET_SPECIAL (msym);
09515 }
09516 
09517 static void
09518 arm_coff_make_msymbol_special(int val, struct minimal_symbol *msym)
09519 {
09520   if (coff_sym_is_thumb (val))
09521     MSYMBOL_SET_SPECIAL (msym);
09522 }
09523 
09524 static void
09525 arm_objfile_data_free (struct objfile *objfile, void *arg)
09526 {
09527   struct arm_per_objfile *data = arg;
09528   unsigned int i;
09529 
09530   for (i = 0; i < objfile->obfd->section_count; i++)
09531     VEC_free (arm_mapping_symbol_s, data->section_maps[i]);
09532 }
09533 
09534 static void
09535 arm_record_special_symbol (struct gdbarch *gdbarch, struct objfile *objfile,
09536                            asymbol *sym)
09537 {
09538   const char *name = bfd_asymbol_name (sym);
09539   struct arm_per_objfile *data;
09540   VEC(arm_mapping_symbol_s) **map_p;
09541   struct arm_mapping_symbol new_map_sym;
09542 
09543   gdb_assert (name[0] == '$');
09544   if (name[1] != 'a' && name[1] != 't' && name[1] != 'd')
09545     return;
09546 
09547   data = objfile_data (objfile, arm_objfile_data_key);
09548   if (data == NULL)
09549     {
09550       data = OBSTACK_ZALLOC (&objfile->objfile_obstack,
09551                              struct arm_per_objfile);
09552       set_objfile_data (objfile, arm_objfile_data_key, data);
09553       data->section_maps = OBSTACK_CALLOC (&objfile->objfile_obstack,
09554                                            objfile->obfd->section_count,
09555                                            VEC(arm_mapping_symbol_s) *);
09556     }
09557   map_p = &data->section_maps[bfd_get_section (sym)->index];
09558 
09559   new_map_sym.value = sym->value;
09560   new_map_sym.type = name[1];
09561 
09562   /* Assume that most mapping symbols appear in order of increasing
09563      value.  If they were randomly distributed, it would be faster to
09564      always push here and then sort at first use.  */
09565   if (!VEC_empty (arm_mapping_symbol_s, *map_p))
09566     {
09567       struct arm_mapping_symbol *prev_map_sym;
09568 
09569       prev_map_sym = VEC_last (arm_mapping_symbol_s, *map_p);
09570       if (prev_map_sym->value >= sym->value)
09571         {
09572           unsigned int idx;
09573           idx = VEC_lower_bound (arm_mapping_symbol_s, *map_p, &new_map_sym,
09574                                  arm_compare_mapping_symbols);
09575           VEC_safe_insert (arm_mapping_symbol_s, *map_p, idx, &new_map_sym);
09576           return;
09577         }
09578     }
09579 
09580   VEC_safe_push (arm_mapping_symbol_s, *map_p, &new_map_sym);
09581 }
09582 
09583 static void
09584 arm_write_pc (struct regcache *regcache, CORE_ADDR pc)
09585 {
09586   struct gdbarch *gdbarch = get_regcache_arch (regcache);
09587   regcache_cooked_write_unsigned (regcache, ARM_PC_REGNUM, pc);
09588 
09589   /* If necessary, set the T bit.  */
09590   if (arm_apcs_32)
09591     {
09592       ULONGEST val, t_bit;
09593       regcache_cooked_read_unsigned (regcache, ARM_PS_REGNUM, &val);
09594       t_bit = arm_psr_thumb_bit (gdbarch);
09595       if (arm_pc_is_thumb (gdbarch, pc))
09596         regcache_cooked_write_unsigned (regcache, ARM_PS_REGNUM,
09597                                         val | t_bit);
09598       else
09599         regcache_cooked_write_unsigned (regcache, ARM_PS_REGNUM,
09600                                         val & ~t_bit);
09601     }
09602 }
09603 
09604 /* Read the contents of a NEON quad register, by reading from two
09605    double registers.  This is used to implement the quad pseudo
09606    registers, and for argument passing in case the quad registers are
09607    missing; vectors are passed in quad registers when using the VFP
09608    ABI, even if a NEON unit is not present.  REGNUM is the index of
09609    the quad register, in [0, 15].  */
09610 
09611 static enum register_status
09612 arm_neon_quad_read (struct gdbarch *gdbarch, struct regcache *regcache,
09613                     int regnum, gdb_byte *buf)
09614 {
09615   char name_buf[4];
09616   gdb_byte reg_buf[8];
09617   int offset, double_regnum;
09618   enum register_status status;
09619 
09620   xsnprintf (name_buf, sizeof (name_buf), "d%d", regnum << 1);
09621   double_regnum = user_reg_map_name_to_regnum (gdbarch, name_buf,
09622                                                strlen (name_buf));
09623 
09624   /* d0 is always the least significant half of q0.  */
09625   if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
09626     offset = 8;
09627   else
09628     offset = 0;
09629 
09630   status = regcache_raw_read (regcache, double_regnum, reg_buf);
09631   if (status != REG_VALID)
09632     return status;
09633   memcpy (buf + offset, reg_buf, 8);
09634 
09635   offset = 8 - offset;
09636   status = regcache_raw_read (regcache, double_regnum + 1, reg_buf);
09637   if (status != REG_VALID)
09638     return status;
09639   memcpy (buf + offset, reg_buf, 8);
09640 
09641   return REG_VALID;
09642 }
09643 
09644 static enum register_status
09645 arm_pseudo_read (struct gdbarch *gdbarch, struct regcache *regcache,
09646                  int regnum, gdb_byte *buf)
09647 {
09648   const int num_regs = gdbarch_num_regs (gdbarch);
09649   char name_buf[4];
09650   gdb_byte reg_buf[8];
09651   int offset, double_regnum;
09652 
09653   gdb_assert (regnum >= num_regs);
09654   regnum -= num_regs;
09655 
09656   if (gdbarch_tdep (gdbarch)->have_neon_pseudos && regnum >= 32 && regnum < 48)
09657     /* Quad-precision register.  */
09658     return arm_neon_quad_read (gdbarch, regcache, regnum - 32, buf);
09659   else
09660     {
09661       enum register_status status;
09662 
09663       /* Single-precision register.  */
09664       gdb_assert (regnum < 32);
09665 
09666       /* s0 is always the least significant half of d0.  */
09667       if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
09668         offset = (regnum & 1) ? 0 : 4;
09669       else
09670         offset = (regnum & 1) ? 4 : 0;
09671 
09672       xsnprintf (name_buf, sizeof (name_buf), "d%d", regnum >> 1);
09673       double_regnum = user_reg_map_name_to_regnum (gdbarch, name_buf,
09674                                                    strlen (name_buf));
09675 
09676       status = regcache_raw_read (regcache, double_regnum, reg_buf);
09677       if (status == REG_VALID)
09678         memcpy (buf, reg_buf + offset, 4);
09679       return status;
09680     }
09681 }
09682 
09683 /* Store the contents of BUF to a NEON quad register, by writing to
09684    two double registers.  This is used to implement the quad pseudo
09685    registers, and for argument passing in case the quad registers are
09686    missing; vectors are passed in quad registers when using the VFP
09687    ABI, even if a NEON unit is not present.  REGNUM is the index
09688    of the quad register, in [0, 15].  */
09689 
09690 static void
09691 arm_neon_quad_write (struct gdbarch *gdbarch, struct regcache *regcache,
09692                      int regnum, const gdb_byte *buf)
09693 {
09694   char name_buf[4];
09695   int offset, double_regnum;
09696 
09697   xsnprintf (name_buf, sizeof (name_buf), "d%d", regnum << 1);
09698   double_regnum = user_reg_map_name_to_regnum (gdbarch, name_buf,
09699                                                strlen (name_buf));
09700 
09701   /* d0 is always the least significant half of q0.  */
09702   if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
09703     offset = 8;
09704   else
09705     offset = 0;
09706 
09707   regcache_raw_write (regcache, double_regnum, buf + offset);
09708   offset = 8 - offset;
09709   regcache_raw_write (regcache, double_regnum + 1, buf + offset);
09710 }
09711 
09712 static void
09713 arm_pseudo_write (struct gdbarch *gdbarch, struct regcache *regcache,
09714                   int regnum, const gdb_byte *buf)
09715 {
09716   const int num_regs = gdbarch_num_regs (gdbarch);
09717   char name_buf[4];
09718   gdb_byte reg_buf[8];
09719   int offset, double_regnum;
09720 
09721   gdb_assert (regnum >= num_regs);
09722   regnum -= num_regs;
09723 
09724   if (gdbarch_tdep (gdbarch)->have_neon_pseudos && regnum >= 32 && regnum < 48)
09725     /* Quad-precision register.  */
09726     arm_neon_quad_write (gdbarch, regcache, regnum - 32, buf);
09727   else
09728     {
09729       /* Single-precision register.  */
09730       gdb_assert (regnum < 32);
09731 
09732       /* s0 is always the least significant half of d0.  */
09733       if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
09734         offset = (regnum & 1) ? 0 : 4;
09735       else
09736         offset = (regnum & 1) ? 4 : 0;
09737 
09738       xsnprintf (name_buf, sizeof (name_buf), "d%d", regnum >> 1);
09739       double_regnum = user_reg_map_name_to_regnum (gdbarch, name_buf,
09740                                                    strlen (name_buf));
09741 
09742       regcache_raw_read (regcache, double_regnum, reg_buf);
09743       memcpy (reg_buf + offset, buf, 4);
09744       regcache_raw_write (regcache, double_regnum, reg_buf);
09745     }
09746 }
09747 
09748 static struct value *
09749 value_of_arm_user_reg (struct frame_info *frame, const void *baton)
09750 {
09751   const int *reg_p = baton;
09752   return value_of_register (*reg_p, frame);
09753 }
09754 
09755 static enum gdb_osabi
09756 arm_elf_osabi_sniffer (bfd *abfd)
09757 {
09758   unsigned int elfosabi;
09759   enum gdb_osabi osabi = GDB_OSABI_UNKNOWN;
09760 
09761   elfosabi = elf_elfheader (abfd)->e_ident[EI_OSABI];
09762 
09763   if (elfosabi == ELFOSABI_ARM)
09764     /* GNU tools use this value.  Check note sections in this case,
09765        as well.  */
09766     bfd_map_over_sections (abfd,
09767                            generic_elf_osabi_sniff_abi_tag_sections, 
09768                            &osabi);
09769 
09770   /* Anything else will be handled by the generic ELF sniffer.  */
09771   return osabi;
09772 }
09773 
09774 static int
09775 arm_register_reggroup_p (struct gdbarch *gdbarch, int regnum,
09776                           struct reggroup *group)
09777 {
09778   /* FPS register's type is INT, but belongs to float_reggroup.  Beside
09779      this, FPS register belongs to save_regroup, restore_reggroup, and
09780      all_reggroup, of course.  */
09781   if (regnum == ARM_FPS_REGNUM)
09782     return (group == float_reggroup
09783             || group == save_reggroup
09784             || group == restore_reggroup
09785             || group == all_reggroup);
09786   else
09787     return default_register_reggroup_p (gdbarch, regnum, group);
09788 }
09789 
09790 
09791 /* For backward-compatibility we allow two 'g' packet lengths with
09792    the remote protocol depending on whether FPA registers are
09793    supplied.  M-profile targets do not have FPA registers, but some
09794    stubs already exist in the wild which use a 'g' packet which
09795    supplies them albeit with dummy values.  The packet format which
09796    includes FPA registers should be considered deprecated for
09797    M-profile targets.  */
09798 
09799 static void
09800 arm_register_g_packet_guesses (struct gdbarch *gdbarch)
09801 {
09802   if (gdbarch_tdep (gdbarch)->is_m)
09803     {
09804       /* If we know from the executable this is an M-profile target,
09805          cater for remote targets whose register set layout is the
09806          same as the FPA layout.  */
09807       register_remote_g_packet_guess (gdbarch,
09808                                       /* r0-r12,sp,lr,pc; f0-f7; fps,xpsr */
09809                                       (16 * INT_REGISTER_SIZE)
09810                                       + (8 * FP_REGISTER_SIZE)
09811                                       + (2 * INT_REGISTER_SIZE),
09812                                       tdesc_arm_with_m_fpa_layout);
09813 
09814       /* The regular M-profile layout.  */
09815       register_remote_g_packet_guess (gdbarch,
09816                                       /* r0-r12,sp,lr,pc; xpsr */
09817                                       (16 * INT_REGISTER_SIZE)
09818                                       + INT_REGISTER_SIZE,
09819                                       tdesc_arm_with_m);
09820 
09821       /* M-profile plus M4F VFP.  */
09822       register_remote_g_packet_guess (gdbarch,
09823                                       /* r0-r12,sp,lr,pc; d0-d15; fpscr,xpsr */
09824                                       (16 * INT_REGISTER_SIZE)
09825                                       + (16 * VFP_REGISTER_SIZE)
09826                                       + (2 * INT_REGISTER_SIZE),
09827                                       tdesc_arm_with_m_vfp_d16);
09828     }
09829 
09830   /* Otherwise we don't have a useful guess.  */
09831 }
09832 
09833 
09834 /* Initialize the current architecture based on INFO.  If possible,
09835    re-use an architecture from ARCHES, which is a list of
09836    architectures already created during this debugging session.
09837 
09838    Called e.g. at program startup, when reading a core file, and when
09839    reading a binary file.  */
09840 
09841 static struct gdbarch *
09842 arm_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
09843 {
09844   struct gdbarch_tdep *tdep;
09845   struct gdbarch *gdbarch;
09846   struct gdbarch_list *best_arch;
09847   enum arm_abi_kind arm_abi = arm_abi_global;
09848   enum arm_float_model fp_model = arm_fp_model;
09849   struct tdesc_arch_data *tdesc_data = NULL;
09850   int i, is_m = 0;
09851   int have_vfp_registers = 0, have_vfp_pseudos = 0, have_neon_pseudos = 0;
09852   int have_neon = 0;
09853   int have_fpa_registers = 1;
09854   const struct target_desc *tdesc = info.target_desc;
09855 
09856   /* If we have an object to base this architecture on, try to determine
09857      its ABI.  */
09858 
09859   if (arm_abi == ARM_ABI_AUTO && info.abfd != NULL)
09860     {
09861       int ei_osabi, e_flags;
09862 
09863       switch (bfd_get_flavour (info.abfd))
09864         {
09865         case bfd_target_aout_flavour:
09866           /* Assume it's an old APCS-style ABI.  */
09867           arm_abi = ARM_ABI_APCS;
09868           break;
09869 
09870         case bfd_target_coff_flavour:
09871           /* Assume it's an old APCS-style ABI.  */
09872           /* XXX WinCE?  */
09873           arm_abi = ARM_ABI_APCS;
09874           break;
09875 
09876         case bfd_target_elf_flavour:
09877           ei_osabi = elf_elfheader (info.abfd)->e_ident[EI_OSABI];
09878           e_flags = elf_elfheader (info.abfd)->e_flags;
09879 
09880           if (ei_osabi == ELFOSABI_ARM)
09881             {
09882               /* GNU tools used to use this value, but do not for EABI
09883                  objects.  There's nowhere to tag an EABI version
09884                  anyway, so assume APCS.  */
09885               arm_abi = ARM_ABI_APCS;
09886             }
09887           else if (ei_osabi == ELFOSABI_NONE)
09888             {
09889               int eabi_ver = EF_ARM_EABI_VERSION (e_flags);
09890               int attr_arch, attr_profile;
09891 
09892               switch (eabi_ver)
09893                 {
09894                 case EF_ARM_EABI_UNKNOWN:
09895                   /* Assume GNU tools.  */
09896                   arm_abi = ARM_ABI_APCS;
09897                   break;
09898 
09899                 case EF_ARM_EABI_VER4:
09900                 case EF_ARM_EABI_VER5:
09901                   arm_abi = ARM_ABI_AAPCS;
09902                   /* EABI binaries default to VFP float ordering.
09903                      They may also contain build attributes that can
09904                      be used to identify if the VFP argument-passing
09905                      ABI is in use.  */
09906                   if (fp_model == ARM_FLOAT_AUTO)
09907                     {
09908 #ifdef HAVE_ELF
09909                       switch (bfd_elf_get_obj_attr_int (info.abfd,
09910                                                         OBJ_ATTR_PROC,
09911                                                         Tag_ABI_VFP_args))
09912                         {
09913                         case 0:
09914                           /* "The user intended FP parameter/result
09915                              passing to conform to AAPCS, base
09916                              variant".  */
09917                           fp_model = ARM_FLOAT_SOFT_VFP;
09918                           break;
09919                         case 1:
09920                           /* "The user intended FP parameter/result
09921                              passing to conform to AAPCS, VFP
09922                              variant".  */
09923                           fp_model = ARM_FLOAT_VFP;
09924                           break;
09925                         case 2:
09926                           /* "The user intended FP parameter/result
09927                              passing to conform to tool chain-specific
09928                              conventions" - we don't know any such
09929                              conventions, so leave it as "auto".  */
09930                           break;
09931                         default:
09932                           /* Attribute value not mentioned in the
09933                              October 2008 ABI, so leave it as
09934                              "auto".  */
09935                           break;
09936                         }
09937 #else
09938                       fp_model = ARM_FLOAT_SOFT_VFP;
09939 #endif
09940                     }
09941                   break;
09942 
09943                 default:
09944                   /* Leave it as "auto".  */
09945                   warning (_("unknown ARM EABI version 0x%x"), eabi_ver);
09946                   break;
09947                 }
09948 
09949 #ifdef HAVE_ELF
09950               /* Detect M-profile programs.  This only works if the
09951                  executable file includes build attributes; GCC does
09952                  copy them to the executable, but e.g. RealView does
09953                  not.  */
09954               attr_arch = bfd_elf_get_obj_attr_int (info.abfd, OBJ_ATTR_PROC,
09955                                                     Tag_CPU_arch);
09956               attr_profile = bfd_elf_get_obj_attr_int (info.abfd,
09957                                                        OBJ_ATTR_PROC,
09958                                                        Tag_CPU_arch_profile);
09959               /* GCC specifies the profile for v6-M; RealView only
09960                  specifies the profile for architectures starting with
09961                  V7 (as opposed to architectures with a tag
09962                  numerically greater than TAG_CPU_ARCH_V7).  */
09963               if (!tdesc_has_registers (tdesc)
09964                   && (attr_arch == TAG_CPU_ARCH_V6_M
09965                       || attr_arch == TAG_CPU_ARCH_V6S_M
09966                       || attr_profile == 'M'))
09967                 is_m = 1;
09968 #endif
09969             }
09970 
09971           if (fp_model == ARM_FLOAT_AUTO)
09972             {
09973               int e_flags = elf_elfheader (info.abfd)->e_flags;
09974 
09975               switch (e_flags & (EF_ARM_SOFT_FLOAT | EF_ARM_VFP_FLOAT))
09976                 {
09977                 case 0:
09978                   /* Leave it as "auto".  Strictly speaking this case
09979                      means FPA, but almost nobody uses that now, and
09980                      many toolchains fail to set the appropriate bits
09981                      for the floating-point model they use.  */
09982                   break;
09983                 case EF_ARM_SOFT_FLOAT:
09984                   fp_model = ARM_FLOAT_SOFT_FPA;
09985                   break;
09986                 case EF_ARM_VFP_FLOAT:
09987                   fp_model = ARM_FLOAT_VFP;
09988                   break;
09989                 case EF_ARM_SOFT_FLOAT | EF_ARM_VFP_FLOAT:
09990                   fp_model = ARM_FLOAT_SOFT_VFP;
09991                   break;
09992                 }
09993             }
09994 
09995           if (e_flags & EF_ARM_BE8)
09996             info.byte_order_for_code = BFD_ENDIAN_LITTLE;
09997 
09998           break;
09999 
10000         default:
10001           /* Leave it as "auto".  */
10002           break;
10003         }
10004     }
10005 
10006   /* Check any target description for validity.  */
10007   if (tdesc_has_registers (tdesc))
10008     {
10009       /* For most registers we require GDB's default names; but also allow
10010          the numeric names for sp / lr / pc, as a convenience.  */
10011       static const char *const arm_sp_names[] = { "r13", "sp", NULL };
10012       static const char *const arm_lr_names[] = { "r14", "lr", NULL };
10013       static const char *const arm_pc_names[] = { "r15", "pc", NULL };
10014 
10015       const struct tdesc_feature *feature;
10016       int valid_p;
10017 
10018       feature = tdesc_find_feature (tdesc,
10019                                     "org.gnu.gdb.arm.core");
10020       if (feature == NULL)
10021         {
10022           feature = tdesc_find_feature (tdesc,
10023                                         "org.gnu.gdb.arm.m-profile");
10024           if (feature == NULL)
10025             return NULL;
10026           else
10027             is_m = 1;
10028         }
10029 
10030       tdesc_data = tdesc_data_alloc ();
10031 
10032       valid_p = 1;
10033       for (i = 0; i < ARM_SP_REGNUM; i++)
10034         valid_p &= tdesc_numbered_register (feature, tdesc_data, i,
10035                                             arm_register_names[i]);
10036       valid_p &= tdesc_numbered_register_choices (feature, tdesc_data,
10037                                                   ARM_SP_REGNUM,
10038                                                   arm_sp_names);
10039       valid_p &= tdesc_numbered_register_choices (feature, tdesc_data,
10040                                                   ARM_LR_REGNUM,
10041                                                   arm_lr_names);
10042       valid_p &= tdesc_numbered_register_choices (feature, tdesc_data,
10043                                                   ARM_PC_REGNUM,
10044                                                   arm_pc_names);
10045       if (is_m)
10046         valid_p &= tdesc_numbered_register (feature, tdesc_data,
10047                                             ARM_PS_REGNUM, "xpsr");
10048       else
10049         valid_p &= tdesc_numbered_register (feature, tdesc_data,
10050                                             ARM_PS_REGNUM, "cpsr");
10051 
10052       if (!valid_p)
10053         {
10054           tdesc_data_cleanup (tdesc_data);
10055           return NULL;
10056         }
10057 
10058       feature = tdesc_find_feature (tdesc,
10059                                     "org.gnu.gdb.arm.fpa");
10060       if (feature != NULL)
10061         {
10062           valid_p = 1;
10063           for (i = ARM_F0_REGNUM; i <= ARM_FPS_REGNUM; i++)
10064             valid_p &= tdesc_numbered_register (feature, tdesc_data, i,
10065                                                 arm_register_names[i]);
10066           if (!valid_p)
10067             {
10068               tdesc_data_cleanup (tdesc_data);
10069               return NULL;
10070             }
10071         }
10072       else
10073         have_fpa_registers = 0;
10074 
10075       feature = tdesc_find_feature (tdesc,
10076                                     "org.gnu.gdb.xscale.iwmmxt");
10077       if (feature != NULL)
10078         {
10079           static const char *const iwmmxt_names[] = {
10080             "wR0", "wR1", "wR2", "wR3", "wR4", "wR5", "wR6", "wR7",
10081             "wR8", "wR9", "wR10", "wR11", "wR12", "wR13", "wR14", "wR15",
10082             "wCID", "wCon", "wCSSF", "wCASF", "", "", "", "",
10083             "wCGR0", "wCGR1", "wCGR2", "wCGR3", "", "", "", "",
10084           };
10085 
10086           valid_p = 1;
10087           for (i = ARM_WR0_REGNUM; i <= ARM_WR15_REGNUM; i++)
10088             valid_p
10089               &= tdesc_numbered_register (feature, tdesc_data, i,
10090                                           iwmmxt_names[i - ARM_WR0_REGNUM]);
10091 
10092           /* Check for the control registers, but do not fail if they
10093              are missing.  */
10094           for (i = ARM_WC0_REGNUM; i <= ARM_WCASF_REGNUM; i++)
10095             tdesc_numbered_register (feature, tdesc_data, i,
10096                                      iwmmxt_names[i - ARM_WR0_REGNUM]);
10097 
10098           for (i = ARM_WCGR0_REGNUM; i <= ARM_WCGR3_REGNUM; i++)
10099             valid_p
10100               &= tdesc_numbered_register (feature, tdesc_data, i,
10101                                           iwmmxt_names[i - ARM_WR0_REGNUM]);
10102 
10103           if (!valid_p)
10104             {
10105               tdesc_data_cleanup (tdesc_data);
10106               return NULL;
10107             }
10108         }
10109 
10110       /* If we have a VFP unit, check whether the single precision registers
10111          are present.  If not, then we will synthesize them as pseudo
10112          registers.  */
10113       feature = tdesc_find_feature (tdesc,
10114                                     "org.gnu.gdb.arm.vfp");
10115       if (feature != NULL)
10116         {
10117           static const char *const vfp_double_names[] = {
10118             "d0", "d1", "d2", "d3", "d4", "d5", "d6", "d7",
10119             "d8", "d9", "d10", "d11", "d12", "d13", "d14", "d15",
10120             "d16", "d17", "d18", "d19", "d20", "d21", "d22", "d23",
10121             "d24", "d25", "d26", "d27", "d28", "d29", "d30", "d31",
10122           };
10123 
10124           /* Require the double precision registers.  There must be either
10125              16 or 32.  */
10126           valid_p = 1;
10127           for (i = 0; i < 32; i++)
10128             {
10129               valid_p &= tdesc_numbered_register (feature, tdesc_data,
10130                                                   ARM_D0_REGNUM + i,
10131                                                   vfp_double_names[i]);
10132               if (!valid_p)
10133                 break;
10134             }
10135           if (!valid_p && i == 16)
10136             valid_p = 1;
10137 
10138           /* Also require FPSCR.  */
10139           valid_p &= tdesc_numbered_register (feature, tdesc_data,
10140                                               ARM_FPSCR_REGNUM, "fpscr");
10141           if (!valid_p)
10142             {
10143               tdesc_data_cleanup (tdesc_data);
10144               return NULL;
10145             }
10146 
10147           if (tdesc_unnumbered_register (feature, "s0") == 0)
10148             have_vfp_pseudos = 1;
10149 
10150           have_vfp_registers = 1;
10151 
10152           /* If we have VFP, also check for NEON.  The architecture allows
10153              NEON without VFP (integer vector operations only), but GDB
10154              does not support that.  */
10155           feature = tdesc_find_feature (tdesc,
10156                                         "org.gnu.gdb.arm.neon");
10157           if (feature != NULL)
10158             {
10159               /* NEON requires 32 double-precision registers.  */
10160               if (i != 32)
10161                 {
10162                   tdesc_data_cleanup (tdesc_data);
10163                   return NULL;
10164                 }
10165 
10166               /* If there are quad registers defined by the stub, use
10167                  their type; otherwise (normally) provide them with
10168                  the default type.  */
10169               if (tdesc_unnumbered_register (feature, "q0") == 0)
10170                 have_neon_pseudos = 1;
10171 
10172               have_neon = 1;
10173             }
10174         }
10175     }
10176 
10177   /* If there is already a candidate, use it.  */
10178   for (best_arch = gdbarch_list_lookup_by_info (arches, &info);
10179        best_arch != NULL;
10180        best_arch = gdbarch_list_lookup_by_info (best_arch->next, &info))
10181     {
10182       if (arm_abi != ARM_ABI_AUTO
10183           && arm_abi != gdbarch_tdep (best_arch->gdbarch)->arm_abi)
10184         continue;
10185 
10186       if (fp_model != ARM_FLOAT_AUTO
10187           && fp_model != gdbarch_tdep (best_arch->gdbarch)->fp_model)
10188         continue;
10189 
10190       /* There are various other properties in tdep that we do not
10191          need to check here: those derived from a target description,
10192          since gdbarches with a different target description are
10193          automatically disqualified.  */
10194 
10195       /* Do check is_m, though, since it might come from the binary.  */
10196       if (is_m != gdbarch_tdep (best_arch->gdbarch)->is_m)
10197         continue;
10198 
10199       /* Found a match.  */
10200       break;
10201     }
10202 
10203   if (best_arch != NULL)
10204     {
10205       if (tdesc_data != NULL)
10206         tdesc_data_cleanup (tdesc_data);
10207       return best_arch->gdbarch;
10208     }
10209 
10210   tdep = xcalloc (1, sizeof (struct gdbarch_tdep));
10211   gdbarch = gdbarch_alloc (&info, tdep);
10212 
10213   /* Record additional information about the architecture we are defining.
10214      These are gdbarch discriminators, like the OSABI.  */
10215   tdep->arm_abi = arm_abi;
10216   tdep->fp_model = fp_model;
10217   tdep->is_m = is_m;
10218   tdep->have_fpa_registers = have_fpa_registers;
10219   tdep->have_vfp_registers = have_vfp_registers;
10220   tdep->have_vfp_pseudos = have_vfp_pseudos;
10221   tdep->have_neon_pseudos = have_neon_pseudos;
10222   tdep->have_neon = have_neon;
10223 
10224   arm_register_g_packet_guesses (gdbarch);
10225 
10226   /* Breakpoints.  */
10227   switch (info.byte_order_for_code)
10228     {
10229     case BFD_ENDIAN_BIG:
10230       tdep->arm_breakpoint = arm_default_arm_be_breakpoint;
10231       tdep->arm_breakpoint_size = sizeof (arm_default_arm_be_breakpoint);
10232       tdep->thumb_breakpoint = arm_default_thumb_be_breakpoint;
10233       tdep->thumb_breakpoint_size = sizeof (arm_default_thumb_be_breakpoint);
10234 
10235       break;
10236 
10237     case BFD_ENDIAN_LITTLE:
10238       tdep->arm_breakpoint = arm_default_arm_le_breakpoint;
10239       tdep->arm_breakpoint_size = sizeof (arm_default_arm_le_breakpoint);
10240       tdep->thumb_breakpoint = arm_default_thumb_le_breakpoint;
10241       tdep->thumb_breakpoint_size = sizeof (arm_default_thumb_le_breakpoint);
10242 
10243       break;
10244 
10245     default:
10246       internal_error (__FILE__, __LINE__,
10247                       _("arm_gdbarch_init: bad byte order for float format"));
10248     }
10249 
10250   /* On ARM targets char defaults to unsigned.  */
10251   set_gdbarch_char_signed (gdbarch, 0);
10252 
10253   /* Note: for displaced stepping, this includes the breakpoint, and one word
10254      of additional scratch space.  This setting isn't used for anything beside
10255      displaced stepping at present.  */
10256   set_gdbarch_max_insn_length (gdbarch, 4 * DISPLACED_MODIFIED_INSNS);
10257 
10258   /* This should be low enough for everything.  */
10259   tdep->lowest_pc = 0x20;
10260   tdep->jb_pc = -1;     /* Longjump support not enabled by default.  */
10261 
10262   /* The default, for both APCS and AAPCS, is to return small
10263      structures in registers.  */
10264   tdep->struct_return = reg_struct_return;
10265 
10266   set_gdbarch_push_dummy_call (gdbarch, arm_push_dummy_call);
10267   set_gdbarch_frame_align (gdbarch, arm_frame_align);
10268 
10269   set_gdbarch_write_pc (gdbarch, arm_write_pc);
10270 
10271   /* Frame handling.  */
10272   set_gdbarch_dummy_id (gdbarch, arm_dummy_id);
10273   set_gdbarch_unwind_pc (gdbarch, arm_unwind_pc);
10274   set_gdbarch_unwind_sp (gdbarch, arm_unwind_sp);
10275 
10276   frame_base_set_default (gdbarch, &arm_normal_base);
10277 
10278   /* Address manipulation.  */
10279   set_gdbarch_addr_bits_remove (gdbarch, arm_addr_bits_remove);
10280 
10281   /* Advance PC across function entry code.  */
10282   set_gdbarch_skip_prologue (gdbarch, arm_skip_prologue);
10283 
10284   /* Detect whether PC is in function epilogue.  */
10285   set_gdbarch_in_function_epilogue_p (gdbarch, arm_in_function_epilogue_p);
10286 
10287   /* Skip trampolines.  */
10288   set_gdbarch_skip_trampoline_code (gdbarch, arm_skip_stub);
10289 
10290   /* The stack grows downward.  */
10291   set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
10292 
10293   /* Breakpoint manipulation.  */
10294   set_gdbarch_breakpoint_from_pc (gdbarch, arm_breakpoint_from_pc);
10295   set_gdbarch_remote_breakpoint_from_pc (gdbarch,
10296                                          arm_remote_breakpoint_from_pc);
10297 
10298   /* Information about registers, etc.  */
10299   set_gdbarch_sp_regnum (gdbarch, ARM_SP_REGNUM);
10300   set_gdbarch_pc_regnum (gdbarch, ARM_PC_REGNUM);
10301   set_gdbarch_num_regs (gdbarch, ARM_NUM_REGS);
10302   set_gdbarch_register_type (gdbarch, arm_register_type);
10303   set_gdbarch_register_reggroup_p (gdbarch, arm_register_reggroup_p);
10304 
10305   /* This "info float" is FPA-specific.  Use the generic version if we
10306      do not have FPA.  */
10307   if (gdbarch_tdep (gdbarch)->have_fpa_registers)
10308     set_gdbarch_print_float_info (gdbarch, arm_print_float_info);
10309 
10310   /* Internal <-> external register number maps.  */
10311   set_gdbarch_dwarf2_reg_to_regnum (gdbarch, arm_dwarf_reg_to_regnum);
10312   set_gdbarch_register_sim_regno (gdbarch, arm_register_sim_regno);
10313 
10314   set_gdbarch_register_name (gdbarch, arm_register_name);
10315 
10316   /* Returning results.  */
10317   set_gdbarch_return_value (gdbarch, arm_return_value);
10318 
10319   /* Disassembly.  */
10320   set_gdbarch_print_insn (gdbarch, gdb_print_insn_arm);
10321 
10322   /* Minsymbol frobbing.  */
10323   set_gdbarch_elf_make_msymbol_special (gdbarch, arm_elf_make_msymbol_special);
10324   set_gdbarch_coff_make_msymbol_special (gdbarch,
10325                                          arm_coff_make_msymbol_special);
10326   set_gdbarch_record_special_symbol (gdbarch, arm_record_special_symbol);
10327 
10328   /* Thumb-2 IT block support.  */
10329   set_gdbarch_adjust_breakpoint_address (gdbarch,
10330                                          arm_adjust_breakpoint_address);
10331 
10332   /* Virtual tables.  */
10333   set_gdbarch_vbit_in_delta (gdbarch, 1);
10334 
10335   /* Hook in the ABI-specific overrides, if they have been registered.  */
10336   gdbarch_init_osabi (info, gdbarch);
10337 
10338   dwarf2_frame_set_init_reg (gdbarch, arm_dwarf2_frame_init_reg);
10339 
10340   /* Add some default predicates.  */
10341   if (is_m)
10342     frame_unwind_append_unwinder (gdbarch, &arm_m_exception_unwind);
10343   frame_unwind_append_unwinder (gdbarch, &arm_stub_unwind);
10344   dwarf2_append_unwinders (gdbarch);
10345   frame_unwind_append_unwinder (gdbarch, &arm_exidx_unwind);
10346   frame_unwind_append_unwinder (gdbarch, &arm_prologue_unwind);
10347 
10348   /* Now we have tuned the configuration, set a few final things,
10349      based on what the OS ABI has told us.  */
10350 
10351   /* If the ABI is not otherwise marked, assume the old GNU APCS.  EABI
10352      binaries are always marked.  */
10353   if (tdep->arm_abi == ARM_ABI_AUTO)
10354     tdep->arm_abi = ARM_ABI_APCS;
10355 
10356   /* Watchpoints are not steppable.  */
10357   set_gdbarch_have_nonsteppable_watchpoint (gdbarch, 1);
10358 
10359   /* We used to default to FPA for generic ARM, but almost nobody
10360      uses that now, and we now provide a way for the user to force
10361      the model.  So default to the most useful variant.  */
10362   if (tdep->fp_model == ARM_FLOAT_AUTO)
10363     tdep->fp_model = ARM_FLOAT_SOFT_FPA;
10364 
10365   if (tdep->jb_pc >= 0)
10366     set_gdbarch_get_longjmp_target (gdbarch, arm_get_longjmp_target);
10367 
10368   /* Floating point sizes and format.  */
10369   set_gdbarch_float_format (gdbarch, floatformats_ieee_single);
10370   if (tdep->fp_model == ARM_FLOAT_SOFT_FPA || tdep->fp_model == ARM_FLOAT_FPA)
10371     {
10372       set_gdbarch_double_format
10373         (gdbarch, floatformats_ieee_double_littlebyte_bigword);
10374       set_gdbarch_long_double_format
10375         (gdbarch, floatformats_ieee_double_littlebyte_bigword);
10376     }
10377   else
10378     {
10379       set_gdbarch_double_format (gdbarch, floatformats_ieee_double);
10380       set_gdbarch_long_double_format (gdbarch, floatformats_ieee_double);
10381     }
10382 
10383   if (have_vfp_pseudos)
10384     {
10385       /* NOTE: These are the only pseudo registers used by
10386          the ARM target at the moment.  If more are added, a
10387          little more care in numbering will be needed.  */
10388 
10389       int num_pseudos = 32;
10390       if (have_neon_pseudos)
10391         num_pseudos += 16;
10392       set_gdbarch_num_pseudo_regs (gdbarch, num_pseudos);
10393       set_gdbarch_pseudo_register_read (gdbarch, arm_pseudo_read);
10394       set_gdbarch_pseudo_register_write (gdbarch, arm_pseudo_write);
10395     }
10396 
10397   if (tdesc_data)
10398     {
10399       set_tdesc_pseudo_register_name (gdbarch, arm_register_name);
10400 
10401       tdesc_use_registers (gdbarch, tdesc, tdesc_data);
10402 
10403       /* Override tdesc_register_type to adjust the types of VFP
10404          registers for NEON.  */
10405       set_gdbarch_register_type (gdbarch, arm_register_type);
10406     }
10407 
10408   /* Add standard register aliases.  We add aliases even for those
10409      nanes which are used by the current architecture - it's simpler,
10410      and does no harm, since nothing ever lists user registers.  */
10411   for (i = 0; i < ARRAY_SIZE (arm_register_aliases); i++)
10412     user_reg_add (gdbarch, arm_register_aliases[i].name,
10413                   value_of_arm_user_reg, &arm_register_aliases[i].regnum);
10414 
10415   return gdbarch;
10416 }
10417 
10418 static void
10419 arm_dump_tdep (struct gdbarch *gdbarch, struct ui_file *file)
10420 {
10421   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
10422 
10423   if (tdep == NULL)
10424     return;
10425 
10426   fprintf_unfiltered (file, _("arm_dump_tdep: Lowest pc = 0x%lx"),
10427                       (unsigned long) tdep->lowest_pc);
10428 }
10429 
10430 extern initialize_file_ftype _initialize_arm_tdep; /* -Wmissing-prototypes */
10431 
10432 void
10433 _initialize_arm_tdep (void)
10434 {
10435   struct ui_file *stb;
10436   long length;
10437   struct cmd_list_element *new_set, *new_show;
10438   const char *setname;
10439   const char *setdesc;
10440   const char *const *regnames;
10441   int numregs, i, j;
10442   static char *helptext;
10443   char regdesc[1024], *rdptr = regdesc;
10444   size_t rest = sizeof (regdesc);
10445 
10446   gdbarch_register (bfd_arch_arm, arm_gdbarch_init, arm_dump_tdep);
10447 
10448   arm_objfile_data_key
10449     = register_objfile_data_with_cleanup (NULL, arm_objfile_data_free);
10450 
10451   /* Add ourselves to objfile event chain.  */
10452   observer_attach_new_objfile (arm_exidx_new_objfile);
10453   arm_exidx_data_key
10454     = register_objfile_data_with_cleanup (NULL, arm_exidx_data_free);
10455 
10456   /* Register an ELF OS ABI sniffer for ARM binaries.  */
10457   gdbarch_register_osabi_sniffer (bfd_arch_arm,
10458                                   bfd_target_elf_flavour,
10459                                   arm_elf_osabi_sniffer);
10460 
10461   /* Initialize the standard target descriptions.  */
10462   initialize_tdesc_arm_with_m ();
10463   initialize_tdesc_arm_with_m_fpa_layout ();
10464   initialize_tdesc_arm_with_m_vfp_d16 ();
10465   initialize_tdesc_arm_with_iwmmxt ();
10466   initialize_tdesc_arm_with_vfpv2 ();
10467   initialize_tdesc_arm_with_vfpv3 ();
10468   initialize_tdesc_arm_with_neon ();
10469 
10470   /* Get the number of possible sets of register names defined in opcodes.  */
10471   num_disassembly_options = get_arm_regname_num_options ();
10472 
10473   /* Add root prefix command for all "set arm"/"show arm" commands.  */
10474   add_prefix_cmd ("arm", no_class, set_arm_command,
10475                   _("Various ARM-specific commands."),
10476                   &setarmcmdlist, "set arm ", 0, &setlist);
10477 
10478   add_prefix_cmd ("arm", no_class, show_arm_command,
10479                   _("Various ARM-specific commands."),
10480                   &showarmcmdlist, "show arm ", 0, &showlist);
10481 
10482   /* Sync the opcode insn printer with our register viewer.  */
10483   parse_arm_disassembler_option ("reg-names-std");
10484 
10485   /* Initialize the array that will be passed to
10486      add_setshow_enum_cmd().  */
10487   valid_disassembly_styles
10488     = xmalloc ((num_disassembly_options + 1) * sizeof (char *));
10489   for (i = 0; i < num_disassembly_options; i++)
10490     {
10491       numregs = get_arm_regnames (i, &setname, &setdesc, &regnames);
10492       valid_disassembly_styles[i] = setname;
10493       length = snprintf (rdptr, rest, "%s - %s\n", setname, setdesc);
10494       rdptr += length;
10495       rest -= length;
10496       /* When we find the default names, tell the disassembler to use
10497          them.  */
10498       if (!strcmp (setname, "std"))
10499         {
10500           disassembly_style = setname;
10501           set_arm_regname_option (i);
10502         }
10503     }
10504   /* Mark the end of valid options.  */
10505   valid_disassembly_styles[num_disassembly_options] = NULL;
10506 
10507   /* Create the help text.  */
10508   stb = mem_fileopen ();
10509   fprintf_unfiltered (stb, "%s%s%s",
10510                       _("The valid values are:\n"),
10511                       regdesc,
10512                       _("The default is \"std\"."));
10513   helptext = ui_file_xstrdup (stb, NULL);
10514   ui_file_delete (stb);
10515 
10516   add_setshow_enum_cmd("disassembler", no_class,
10517                        valid_disassembly_styles, &disassembly_style,
10518                        _("Set the disassembly style."),
10519                        _("Show the disassembly style."),
10520                        helptext,
10521                        set_disassembly_style_sfunc,
10522                        NULL, /* FIXME: i18n: The disassembly style is
10523                                 \"%s\".  */
10524                        &setarmcmdlist, &showarmcmdlist);
10525 
10526   add_setshow_boolean_cmd ("apcs32", no_class, &arm_apcs_32,
10527                            _("Set usage of ARM 32-bit mode."),
10528                            _("Show usage of ARM 32-bit mode."),
10529                            _("When off, a 26-bit PC will be used."),
10530                            NULL,
10531                            NULL, /* FIXME: i18n: Usage of ARM 32-bit
10532                                     mode is %s.  */
10533                            &setarmcmdlist, &showarmcmdlist);
10534 
10535   /* Add a command to allow the user to force the FPU model.  */
10536   add_setshow_enum_cmd ("fpu", no_class, fp_model_strings, &current_fp_model,
10537                         _("Set the floating point type."),
10538                         _("Show the floating point type."),
10539                         _("auto - Determine the FP typefrom the OS-ABI.\n\
10540 softfpa - Software FP, mixed-endian doubles on little-endian ARMs.\n\
10541 fpa - FPA co-processor (GCC compiled).\n\
10542 softvfp - Software FP with pure-endian doubles.\n\
10543 vfp - VFP co-processor."),
10544                         set_fp_model_sfunc, show_fp_model,
10545                         &setarmcmdlist, &showarmcmdlist);
10546 
10547   /* Add a command to allow the user to force the ABI.  */
10548   add_setshow_enum_cmd ("abi", class_support, arm_abi_strings, &arm_abi_string,
10549                         _("Set the ABI."),
10550                         _("Show the ABI."),
10551                         NULL, arm_set_abi, arm_show_abi,
10552                         &setarmcmdlist, &showarmcmdlist);
10553 
10554   /* Add two commands to allow the user to force the assumed
10555      execution mode.  */
10556   add_setshow_enum_cmd ("fallback-mode", class_support,
10557                         arm_mode_strings, &arm_fallback_mode_string,
10558                         _("Set the mode assumed when symbols are unavailable."),
10559                         _("Show the mode assumed when symbols are unavailable."),
10560                         NULL, NULL, arm_show_fallback_mode,
10561                         &setarmcmdlist, &showarmcmdlist);
10562   add_setshow_enum_cmd ("force-mode", class_support,
10563                         arm_mode_strings, &arm_force_mode_string,
10564                         _("Set the mode assumed even when symbols are available."),
10565                         _("Show the mode assumed even when symbols are available."),
10566                         NULL, NULL, arm_show_force_mode,
10567                         &setarmcmdlist, &showarmcmdlist);
10568 
10569   /* Debugging flag.  */
10570   add_setshow_boolean_cmd ("arm", class_maintenance, &arm_debug,
10571                            _("Set ARM debugging."),
10572                            _("Show ARM debugging."),
10573                            _("When on, arm-specific debugging is enabled."),
10574                            NULL,
10575                            NULL, /* FIXME: i18n: "ARM debugging is %s.  */
10576                            &setdebuglist, &showdebuglist);
10577 }
10578 
10579 /* ARM-reversible process record data structures.  */
10580 
10581 #define ARM_INSN_SIZE_BYTES 4    
10582 #define THUMB_INSN_SIZE_BYTES 2
10583 #define THUMB2_INSN_SIZE_BYTES 4
10584 
10585 
10586 #define INSN_S_L_BIT_NUM 20
10587 
10588 #define REG_ALLOC(REGS, LENGTH, RECORD_BUF) \
10589         do  \
10590           { \
10591             unsigned int reg_len = LENGTH; \
10592             if (reg_len) \
10593               { \
10594                 REGS = XNEWVEC (uint32_t, reg_len); \
10595                 memcpy(&REGS[0], &RECORD_BUF[0], sizeof(uint32_t)*LENGTH); \
10596               } \
10597           } \
10598         while (0)
10599 
10600 #define MEM_ALLOC(MEMS, LENGTH, RECORD_BUF) \
10601         do  \
10602           { \
10603             unsigned int mem_len = LENGTH; \
10604             if (mem_len) \
10605             { \
10606               MEMS =  XNEWVEC (struct arm_mem_r, mem_len);  \
10607               memcpy(&MEMS->len, &RECORD_BUF[0], \
10608                      sizeof(struct arm_mem_r) * LENGTH); \
10609             } \
10610           } \
10611           while (0)
10612 
10613 /* Checks whether insn is already recorded or yet to be decoded. (boolean expression).  */
10614 #define INSN_RECORDED(ARM_RECORD) \
10615         (0 != (ARM_RECORD)->reg_rec_count || 0 != (ARM_RECORD)->mem_rec_count)
10616 
10617 /* ARM memory record structure.  */
10618 struct arm_mem_r
10619 {
10620   uint32_t len;    /* Record length.  */
10621   CORE_ADDR addr;  /* Memory address.  */
10622 };
10623 
10624 /* ARM instruction record contains opcode of current insn
10625    and execution state (before entry to decode_insn()),
10626    contains list of to-be-modified registers and
10627    memory blocks (on return from decode_insn()).  */
10628 
10629 typedef struct insn_decode_record_t
10630 {
10631   struct gdbarch *gdbarch;
10632   struct regcache *regcache;
10633   CORE_ADDR this_addr;          /* Address of the insn being decoded.  */
10634   uint32_t arm_insn;            /* Should accommodate thumb.  */
10635   uint32_t cond;                /* Condition code.  */
10636   uint32_t opcode;              /* Insn opcode.  */
10637   uint32_t decode;              /* Insn decode bits.  */
10638   uint32_t mem_rec_count;       /* No of mem records.  */
10639   uint32_t reg_rec_count;       /* No of reg records.  */
10640   uint32_t *arm_regs;           /* Registers to be saved for this record.  */
10641   struct arm_mem_r *arm_mems;   /* Memory to be saved for this record.  */
10642 } insn_decode_record;
10643 
10644 
10645 /* Checks ARM SBZ and SBO mandatory fields.  */
10646 
10647 static int
10648 sbo_sbz (uint32_t insn, uint32_t bit_num, uint32_t len, uint32_t sbo)
10649 {
10650   uint32_t ones = bits (insn, bit_num - 1, (bit_num -1) + (len - 1));
10651 
10652   if (!len)
10653     return 1;
10654 
10655   if (!sbo)
10656     ones = ~ones;
10657 
10658   while (ones)
10659     {
10660       if (!(ones & sbo))
10661         {
10662           return 0;
10663         }
10664       ones = ones >> 1;
10665     }
10666   return 1;
10667 }
10668 
10669 typedef enum
10670 {
10671   ARM_RECORD_STRH=1,
10672   ARM_RECORD_STRD
10673 } arm_record_strx_t;
10674 
10675 typedef enum
10676 {
10677   ARM_RECORD=1,
10678   THUMB_RECORD,
10679   THUMB2_RECORD
10680 } record_type_t;
10681 
10682 
10683 static int
10684 arm_record_strx (insn_decode_record *arm_insn_r, uint32_t *record_buf, 
10685                  uint32_t *record_buf_mem, arm_record_strx_t str_type)
10686 {
10687 
10688   struct regcache *reg_cache = arm_insn_r->regcache;
10689   ULONGEST u_regval[2]= {0};
10690 
10691   uint32_t reg_src1 = 0, reg_src2 = 0;
10692   uint32_t immed_high = 0, immed_low = 0,offset_8 = 0, tgt_mem_addr = 0;
10693   uint32_t opcode1 = 0;
10694 
10695   arm_insn_r->opcode = bits (arm_insn_r->arm_insn, 21, 24);
10696   arm_insn_r->decode = bits (arm_insn_r->arm_insn, 4, 7);
10697   opcode1 = bits (arm_insn_r->arm_insn, 20, 24);
10698 
10699 
10700   if (14 == arm_insn_r->opcode || 10 == arm_insn_r->opcode)
10701     {
10702       /* 1) Handle misc store, immediate offset.  */
10703       immed_low = bits (arm_insn_r->arm_insn, 0, 3);
10704       immed_high = bits (arm_insn_r->arm_insn, 8, 11);
10705       reg_src1 = bits (arm_insn_r->arm_insn, 16, 19);
10706       regcache_raw_read_unsigned (reg_cache, reg_src1,
10707                                   &u_regval[0]);
10708       if (ARM_PC_REGNUM == reg_src1)
10709         {
10710           /* If R15 was used as Rn, hence current PC+8.  */
10711           u_regval[0] = u_regval[0] + 8;
10712         }
10713       offset_8 = (immed_high << 4) | immed_low;
10714       /* Calculate target store address.  */
10715       if (14 == arm_insn_r->opcode)
10716         {
10717           tgt_mem_addr = u_regval[0] + offset_8;
10718         }
10719       else
10720         {
10721           tgt_mem_addr = u_regval[0] - offset_8;
10722         }
10723       if (ARM_RECORD_STRH == str_type)
10724         {
10725           record_buf_mem[0] = 2;
10726           record_buf_mem[1] = tgt_mem_addr;
10727           arm_insn_r->mem_rec_count = 1;
10728         }
10729       else if (ARM_RECORD_STRD == str_type)
10730         {
10731           record_buf_mem[0] = 4;
10732           record_buf_mem[1] = tgt_mem_addr;
10733           record_buf_mem[2] = 4;
10734           record_buf_mem[3] = tgt_mem_addr + 4;
10735           arm_insn_r->mem_rec_count = 2;
10736         }
10737     }
10738   else if (12 == arm_insn_r->opcode || 8 == arm_insn_r->opcode)
10739     {
10740       /* 2) Store, register offset.  */
10741       /* Get Rm.  */
10742       reg_src1 = bits (arm_insn_r->arm_insn, 0, 3);
10743       /* Get Rn.  */
10744       reg_src2 = bits (arm_insn_r->arm_insn, 16, 19);
10745       regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval[0]);
10746       regcache_raw_read_unsigned (reg_cache, reg_src2, &u_regval[1]);
10747       if (15 == reg_src2)
10748         {
10749           /* If R15 was used as Rn, hence current PC+8.  */
10750           u_regval[0] = u_regval[0] + 8;
10751         }
10752       /* Calculate target store address, Rn +/- Rm, register offset.  */
10753       if (12 == arm_insn_r->opcode)
10754         {
10755           tgt_mem_addr = u_regval[0] + u_regval[1];
10756         }
10757       else
10758         {
10759           tgt_mem_addr = u_regval[1] - u_regval[0];
10760         }
10761       if (ARM_RECORD_STRH == str_type)
10762         {
10763           record_buf_mem[0] = 2;
10764           record_buf_mem[1] = tgt_mem_addr;
10765           arm_insn_r->mem_rec_count = 1;
10766         }
10767       else if (ARM_RECORD_STRD == str_type)
10768         {
10769           record_buf_mem[0] = 4;
10770           record_buf_mem[1] = tgt_mem_addr;
10771           record_buf_mem[2] = 4;
10772           record_buf_mem[3] = tgt_mem_addr + 4;
10773           arm_insn_r->mem_rec_count = 2;
10774         }
10775     }
10776   else if (11 == arm_insn_r->opcode || 15 == arm_insn_r->opcode
10777            || 2 == arm_insn_r->opcode  || 6 == arm_insn_r->opcode)
10778     {
10779       /* 3) Store, immediate pre-indexed.  */
10780       /* 5) Store, immediate post-indexed.  */
10781       immed_low = bits (arm_insn_r->arm_insn, 0, 3);
10782       immed_high = bits (arm_insn_r->arm_insn, 8, 11);
10783       offset_8 = (immed_high << 4) | immed_low;
10784       reg_src1 = bits (arm_insn_r->arm_insn, 16, 19);
10785       regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval[0]);
10786       /* Calculate target store address, Rn +/- Rm, register offset.  */
10787       if (15 == arm_insn_r->opcode || 6 == arm_insn_r->opcode)
10788         {
10789           tgt_mem_addr = u_regval[0] + offset_8;
10790         }
10791       else
10792         {
10793           tgt_mem_addr = u_regval[0] - offset_8;
10794         }
10795       if (ARM_RECORD_STRH == str_type)
10796         {
10797           record_buf_mem[0] = 2;
10798           record_buf_mem[1] = tgt_mem_addr;
10799           arm_insn_r->mem_rec_count = 1;
10800         }
10801       else if (ARM_RECORD_STRD == str_type)
10802         {
10803           record_buf_mem[0] = 4;
10804           record_buf_mem[1] = tgt_mem_addr;
10805           record_buf_mem[2] = 4;
10806           record_buf_mem[3] = tgt_mem_addr + 4;
10807           arm_insn_r->mem_rec_count = 2;
10808         }
10809       /* Record Rn also as it changes.  */
10810       *(record_buf) = bits (arm_insn_r->arm_insn, 16, 19);
10811       arm_insn_r->reg_rec_count = 1;
10812     }
10813   else if (9 == arm_insn_r->opcode || 13 == arm_insn_r->opcode
10814            || 0 == arm_insn_r->opcode || 4 == arm_insn_r->opcode)
10815     {
10816       /* 4) Store, register pre-indexed.  */
10817       /* 6) Store, register post -indexed.  */
10818       reg_src1 = bits (arm_insn_r->arm_insn, 0, 3);
10819       reg_src2 = bits (arm_insn_r->arm_insn, 16, 19);
10820       regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval[0]);
10821       regcache_raw_read_unsigned (reg_cache, reg_src2, &u_regval[1]);
10822       /* Calculate target store address, Rn +/- Rm, register offset.  */
10823       if (13 == arm_insn_r->opcode || 4 == arm_insn_r->opcode)
10824         {
10825           tgt_mem_addr = u_regval[0] + u_regval[1];
10826         }
10827       else
10828         {
10829           tgt_mem_addr = u_regval[1] - u_regval[0];
10830         }
10831       if (ARM_RECORD_STRH == str_type)
10832         {
10833           record_buf_mem[0] = 2;
10834           record_buf_mem[1] = tgt_mem_addr;
10835           arm_insn_r->mem_rec_count = 1;
10836         }
10837       else if (ARM_RECORD_STRD == str_type)
10838         {
10839           record_buf_mem[0] = 4;
10840           record_buf_mem[1] = tgt_mem_addr;
10841           record_buf_mem[2] = 4;
10842           record_buf_mem[3] = tgt_mem_addr + 4;
10843           arm_insn_r->mem_rec_count = 2;
10844         }
10845       /* Record Rn also as it changes.  */
10846       *(record_buf) = bits (arm_insn_r->arm_insn, 16, 19);
10847       arm_insn_r->reg_rec_count = 1;
10848     }
10849   return 0;
10850 }
10851 
10852 /* Handling ARM extension space insns.  */
10853 
10854 static int
10855 arm_record_extension_space (insn_decode_record *arm_insn_r)
10856 {
10857   uint32_t ret = 0;  /* Return value: -1:record failure ;  0:success  */
10858   uint32_t opcode1 = 0, opcode2 = 0, insn_op1 = 0;
10859   uint32_t record_buf[8], record_buf_mem[8];
10860   uint32_t reg_src1 = 0;
10861   uint32_t immed_high = 0, immed_low = 0,offset_8 = 0, tgt_mem_addr = 0;
10862   struct regcache *reg_cache = arm_insn_r->regcache;
10863   ULONGEST u_regval = 0;
10864 
10865   gdb_assert (!INSN_RECORDED(arm_insn_r));
10866   /* Handle unconditional insn extension space.  */
10867 
10868   opcode1 = bits (arm_insn_r->arm_insn, 20, 27);
10869   opcode2 = bits (arm_insn_r->arm_insn, 4, 7);
10870   if (arm_insn_r->cond)
10871     {
10872       /* PLD has no affect on architectural state, it just affects
10873          the caches.  */
10874       if (5 == ((opcode1 & 0xE0) >> 5))
10875         {
10876           /* BLX(1) */
10877           record_buf[0] = ARM_PS_REGNUM;
10878           record_buf[1] = ARM_LR_REGNUM;
10879           arm_insn_r->reg_rec_count = 2;
10880         }
10881       /* STC2, LDC2, MCR2, MRC2, CDP2: <TBD>, co-processor insn.  */
10882     }
10883 
10884 
10885   opcode1 = bits (arm_insn_r->arm_insn, 25, 27);
10886   if (3 == opcode1 && bit (arm_insn_r->arm_insn, 4))
10887     {
10888       ret = -1;
10889       /* Undefined instruction on ARM V5; need to handle if later 
10890          versions define it.  */
10891     }
10892 
10893   opcode1 = bits (arm_insn_r->arm_insn, 24, 27);
10894   opcode2 = bits (arm_insn_r->arm_insn, 4, 7);
10895   insn_op1 = bits (arm_insn_r->arm_insn, 20, 23);
10896 
10897   /* Handle arithmetic insn extension space.  */
10898   if (!opcode1 && 9 == opcode2 && 1 != arm_insn_r->cond
10899       && !INSN_RECORDED(arm_insn_r))
10900     {
10901       /* Handle MLA(S) and MUL(S).  */
10902       if (0 <= insn_op1 && 3 >= insn_op1)
10903       {
10904         record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
10905         record_buf[1] = ARM_PS_REGNUM;
10906         arm_insn_r->reg_rec_count = 2;
10907       }
10908       else if (4 <= insn_op1 && 15 >= insn_op1)
10909       {
10910         /* Handle SMLAL(S), SMULL(S), UMLAL(S), UMULL(S).  */
10911         record_buf[0] = bits (arm_insn_r->arm_insn, 16, 19);
10912         record_buf[1] = bits (arm_insn_r->arm_insn, 12, 15);
10913         record_buf[2] = ARM_PS_REGNUM;
10914         arm_insn_r->reg_rec_count = 3;
10915       }
10916     }
10917 
10918   opcode1 = bits (arm_insn_r->arm_insn, 26, 27);
10919   opcode2 = bits (arm_insn_r->arm_insn, 23, 24);
10920   insn_op1 = bits (arm_insn_r->arm_insn, 21, 22);
10921 
10922   /* Handle control insn extension space.  */
10923 
10924   if (!opcode1 && 2 == opcode2 && !bit (arm_insn_r->arm_insn, 20)
10925       && 1 != arm_insn_r->cond && !INSN_RECORDED(arm_insn_r))
10926     {
10927       if (!bit (arm_insn_r->arm_insn,25))
10928         {
10929           if (!bits (arm_insn_r->arm_insn, 4, 7))
10930             {
10931               if ((0 == insn_op1) || (2 == insn_op1))
10932                 {
10933                   /* MRS.  */
10934                   record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
10935                   arm_insn_r->reg_rec_count = 1;
10936                 }
10937               else if (1 == insn_op1)
10938                 {
10939                   /* CSPR is going to be changed.  */
10940                   record_buf[0] = ARM_PS_REGNUM;
10941                   arm_insn_r->reg_rec_count = 1;
10942                 }
10943               else if (3 == insn_op1)
10944                 {
10945                   /* SPSR is going to be changed.  */
10946                   /* We need to get SPSR value, which is yet to be done.  */
10947                   printf_unfiltered (_("Process record does not support "
10948                                      "instruction  0x%0x at address %s.\n"),
10949                                      arm_insn_r->arm_insn,
10950                                      paddress (arm_insn_r->gdbarch, 
10951                                      arm_insn_r->this_addr));
10952                   return -1;
10953                 }
10954             }
10955           else if (1 == bits (arm_insn_r->arm_insn, 4, 7))
10956             {
10957               if (1 == insn_op1)
10958                 {
10959                   /* BX.  */
10960                   record_buf[0] = ARM_PS_REGNUM;
10961                   arm_insn_r->reg_rec_count = 1;
10962                 }
10963               else if (3 == insn_op1)
10964                 {
10965                   /* CLZ.  */
10966                   record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
10967                   arm_insn_r->reg_rec_count = 1;
10968                 }
10969             }
10970           else if (3 == bits (arm_insn_r->arm_insn, 4, 7))
10971             {
10972               /* BLX.  */
10973               record_buf[0] = ARM_PS_REGNUM;
10974               record_buf[1] = ARM_LR_REGNUM;
10975               arm_insn_r->reg_rec_count = 2;
10976             }
10977           else if (5 == bits (arm_insn_r->arm_insn, 4, 7))
10978             {
10979               /* QADD, QSUB, QDADD, QDSUB */
10980               record_buf[0] = ARM_PS_REGNUM;
10981               record_buf[1] = bits (arm_insn_r->arm_insn, 12, 15);
10982               arm_insn_r->reg_rec_count = 2;
10983             }
10984           else if (7 == bits (arm_insn_r->arm_insn, 4, 7))
10985             {
10986               /* BKPT.  */
10987               record_buf[0] = ARM_PS_REGNUM;
10988               record_buf[1] = ARM_LR_REGNUM;
10989               arm_insn_r->reg_rec_count = 2;
10990 
10991               /* Save SPSR also;how?  */
10992               printf_unfiltered (_("Process record does not support "
10993                                   "instruction 0x%0x at address %s.\n"),
10994                                   arm_insn_r->arm_insn,
10995                   paddress (arm_insn_r->gdbarch, arm_insn_r->this_addr));
10996               return -1;
10997             }
10998           else if(8 == bits (arm_insn_r->arm_insn, 4, 7) 
10999                   || 10 == bits (arm_insn_r->arm_insn, 4, 7)
11000                   || 12 == bits (arm_insn_r->arm_insn, 4, 7)
11001                   || 14 == bits (arm_insn_r->arm_insn, 4, 7)
11002                  )
11003             {
11004               if (0 == insn_op1 || 1 == insn_op1)
11005                 {
11006                   /* SMLA<x><y>, SMLAW<y>, SMULW<y>.  */
11007                   /* We dont do optimization for SMULW<y> where we
11008                      need only Rd.  */
11009                   record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11010                   record_buf[1] = ARM_PS_REGNUM;
11011                   arm_insn_r->reg_rec_count = 2;
11012                 }
11013               else if (2 == insn_op1)
11014                 {
11015                   /* SMLAL<x><y>.  */
11016                   record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11017                   record_buf[1] = bits (arm_insn_r->arm_insn, 16, 19);
11018                   arm_insn_r->reg_rec_count = 2;
11019                 }
11020               else if (3 == insn_op1)
11021                 {
11022                   /* SMUL<x><y>.  */
11023                   record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11024                   arm_insn_r->reg_rec_count = 1;
11025                 }
11026             }
11027         }
11028       else
11029         {
11030           /* MSR : immediate form.  */
11031           if (1 == insn_op1)
11032             {
11033               /* CSPR is going to be changed.  */
11034               record_buf[0] = ARM_PS_REGNUM;
11035               arm_insn_r->reg_rec_count = 1;
11036             }
11037           else if (3 == insn_op1)
11038             {
11039               /* SPSR is going to be changed.  */
11040               /* we need to get SPSR value, which is yet to be done  */
11041               printf_unfiltered (_("Process record does not support "
11042                                    "instruction 0x%0x at address %s.\n"),
11043                                     arm_insn_r->arm_insn,
11044                                     paddress (arm_insn_r->gdbarch, 
11045                                     arm_insn_r->this_addr));
11046               return -1;
11047             }
11048         }
11049     }
11050 
11051   opcode1 = bits (arm_insn_r->arm_insn, 25, 27);
11052   opcode2 = bits (arm_insn_r->arm_insn, 20, 24);
11053   insn_op1 = bits (arm_insn_r->arm_insn, 5, 6);
11054 
11055   /* Handle load/store insn extension space.  */
11056 
11057   if (!opcode1 && bit (arm_insn_r->arm_insn, 7) 
11058       && bit (arm_insn_r->arm_insn, 4) && 1 != arm_insn_r->cond
11059       && !INSN_RECORDED(arm_insn_r))
11060     {
11061       /* SWP/SWPB.  */
11062       if (0 == insn_op1)
11063         {
11064           /* These insn, changes register and memory as well.  */
11065           /* SWP or SWPB insn.  */
11066           /* Get memory address given by Rn.  */
11067           reg_src1 = bits (arm_insn_r->arm_insn, 16, 19);
11068           regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval);
11069           /* SWP insn ?, swaps word.  */
11070           if (8 == arm_insn_r->opcode)
11071             {
11072               record_buf_mem[0] = 4;
11073             }
11074           else
11075             {
11076               /* SWPB insn, swaps only byte.  */
11077               record_buf_mem[0] = 1;
11078             }
11079           record_buf_mem[1] = u_regval;
11080           arm_insn_r->mem_rec_count = 1;
11081           record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11082           arm_insn_r->reg_rec_count = 1;
11083         }
11084       else if (1 == insn_op1 && !bit (arm_insn_r->arm_insn, 20))
11085         {
11086           /* STRH.  */
11087           arm_record_strx(arm_insn_r, &record_buf[0], &record_buf_mem[0],
11088                           ARM_RECORD_STRH);
11089         }
11090       else if (2 == insn_op1 && !bit (arm_insn_r->arm_insn, 20))
11091         {
11092           /* LDRD.  */
11093           record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11094           record_buf[1] = record_buf[0] + 1;
11095           arm_insn_r->reg_rec_count = 2;
11096         }
11097       else if (3 == insn_op1 && !bit (arm_insn_r->arm_insn, 20))
11098         {
11099           /* STRD.  */
11100           arm_record_strx(arm_insn_r, &record_buf[0], &record_buf_mem[0],
11101                         ARM_RECORD_STRD);
11102         }
11103       else if (bit (arm_insn_r->arm_insn, 20) && insn_op1 <= 3)
11104         {
11105           /* LDRH, LDRSB, LDRSH.  */
11106           record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11107           arm_insn_r->reg_rec_count = 1;
11108         }
11109 
11110     }
11111 
11112   opcode1 = bits (arm_insn_r->arm_insn, 23, 27);
11113   if (24 == opcode1 && bit (arm_insn_r->arm_insn, 21)
11114       && !INSN_RECORDED(arm_insn_r))
11115     {
11116       ret = -1;
11117       /* Handle coprocessor insn extension space.  */
11118     }
11119 
11120   /* To be done for ARMv5 and later; as of now we return -1.  */
11121   if (-1 == ret)
11122     printf_unfiltered (_("Process record does not support instruction x%0x "
11123                          "at address %s.\n"),arm_insn_r->arm_insn,
11124                          paddress (arm_insn_r->gdbarch, arm_insn_r->this_addr));
11125 
11126 
11127   REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
11128   MEM_ALLOC (arm_insn_r->arm_mems, arm_insn_r->mem_rec_count, record_buf_mem);
11129 
11130   return ret;
11131 }
11132 
11133 /* Handling opcode 000 insns.  */
11134 
11135 static int
11136 arm_record_data_proc_misc_ld_str (insn_decode_record *arm_insn_r)
11137 {
11138   struct regcache *reg_cache = arm_insn_r->regcache;
11139   uint32_t record_buf[8], record_buf_mem[8];
11140   ULONGEST u_regval[2] = {0};
11141 
11142   uint32_t reg_src1 = 0, reg_src2 = 0, reg_dest = 0;
11143   uint32_t immed_high = 0, immed_low = 0, offset_8 = 0, tgt_mem_addr = 0;
11144   uint32_t opcode1 = 0;
11145 
11146   arm_insn_r->opcode = bits (arm_insn_r->arm_insn, 21, 24);
11147   arm_insn_r->decode = bits (arm_insn_r->arm_insn, 4, 7);
11148   opcode1 = bits (arm_insn_r->arm_insn, 20, 24);
11149 
11150   /* Data processing insn /multiply insn.  */
11151   if (9 == arm_insn_r->decode
11152       && ((4 <= arm_insn_r->opcode && 7 >= arm_insn_r->opcode)
11153       ||  (0 == arm_insn_r->opcode || 1 == arm_insn_r->opcode)))
11154     {
11155       /* Handle multiply instructions.  */
11156       /* MLA, MUL, SMLAL, SMULL, UMLAL, UMULL.  */
11157         if (0 == arm_insn_r->opcode || 1 == arm_insn_r->opcode)
11158           {
11159             /* Handle MLA and MUL.  */
11160             record_buf[0] = bits (arm_insn_r->arm_insn, 16, 19);
11161             record_buf[1] = ARM_PS_REGNUM;
11162             arm_insn_r->reg_rec_count = 2;
11163           }
11164         else if (4 <= arm_insn_r->opcode && 7 >= arm_insn_r->opcode)
11165           {
11166             /* Handle SMLAL, SMULL, UMLAL, UMULL.  */
11167             record_buf[0] = bits (arm_insn_r->arm_insn, 16, 19);
11168             record_buf[1] = bits (arm_insn_r->arm_insn, 12, 15);
11169             record_buf[2] = ARM_PS_REGNUM;
11170             arm_insn_r->reg_rec_count = 3;
11171           }
11172     }
11173   else if (bit (arm_insn_r->arm_insn, INSN_S_L_BIT_NUM)
11174            && (11 == arm_insn_r->decode || 13 == arm_insn_r->decode))
11175     {
11176       /* Handle misc load insns, as 20th bit  (L = 1).  */
11177       /* LDR insn has a capability to do branching, if
11178          MOV LR, PC is precceded by LDR insn having Rn as R15
11179          in that case, it emulates branch and link insn, and hence we 
11180          need to save CSPR and PC as well. I am not sure this is right
11181          place; as opcode = 010 LDR insn make this happen, if R15 was
11182          used.  */
11183       reg_dest = bits (arm_insn_r->arm_insn, 12, 15);
11184       if (15 != reg_dest)
11185         {
11186           record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11187           arm_insn_r->reg_rec_count = 1;
11188         }
11189       else
11190         {
11191           record_buf[0] = reg_dest;
11192           record_buf[1] = ARM_PS_REGNUM;
11193           arm_insn_r->reg_rec_count = 2;
11194         }
11195     }
11196   else if ((9 == arm_insn_r->opcode || 11 == arm_insn_r->opcode)
11197            && sbo_sbz (arm_insn_r->arm_insn, 5, 12, 0)
11198            && sbo_sbz (arm_insn_r->arm_insn, 13, 4, 1)
11199            && 2 == bits (arm_insn_r->arm_insn, 20, 21))
11200     {
11201       /* Handle MSR insn.  */
11202       if (9 == arm_insn_r->opcode)
11203         {
11204           /* CSPR is going to be changed.  */
11205           record_buf[0] = ARM_PS_REGNUM;
11206           arm_insn_r->reg_rec_count = 1;
11207         }
11208       else
11209         {
11210           /* SPSR is going to be changed.  */
11211           /* How to read SPSR value?  */
11212           printf_unfiltered (_("Process record does not support instruction "
11213                             "0x%0x at address %s.\n"),
11214                             arm_insn_r->arm_insn,
11215                         paddress (arm_insn_r->gdbarch, arm_insn_r->this_addr));
11216           return -1;
11217         }
11218     }
11219   else if (9 == arm_insn_r->decode
11220            && (8 == arm_insn_r->opcode || 10 == arm_insn_r->opcode)
11221            && !bit (arm_insn_r->arm_insn, INSN_S_L_BIT_NUM))
11222     {
11223       /* Handling SWP, SWPB.  */
11224       /* These insn, changes register and memory as well.  */
11225       /* SWP or SWPB insn.  */
11226 
11227       reg_src1 = bits (arm_insn_r->arm_insn, 16, 19);
11228       regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval[0]);
11229       /* SWP insn ?, swaps word.  */
11230       if (8 == arm_insn_r->opcode)
11231         {
11232           record_buf_mem[0] = 4;
11233         }
11234         else
11235         {
11236           /* SWPB insn, swaps only byte.  */
11237           record_buf_mem[0] = 1;
11238         }
11239       record_buf_mem[1] = u_regval[0];
11240       arm_insn_r->mem_rec_count = 1;
11241       record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11242       arm_insn_r->reg_rec_count = 1;
11243     }
11244   else if (3 == arm_insn_r->decode && 0x12 == opcode1
11245            && sbo_sbz (arm_insn_r->arm_insn, 9, 12, 1))
11246     {
11247       /* Handle BLX, branch and link/exchange.  */
11248       if (9 == arm_insn_r->opcode)
11249       {
11250         /* Branch is chosen by setting T bit of CSPR, bitp[0] of Rm,
11251            and R14 stores the return address.  */
11252         record_buf[0] = ARM_PS_REGNUM;
11253         record_buf[1] = ARM_LR_REGNUM;
11254         arm_insn_r->reg_rec_count = 2;
11255       }
11256     }
11257   else if (7 == arm_insn_r->decode && 0x12 == opcode1)
11258     {
11259       /* Handle enhanced software breakpoint insn, BKPT.  */
11260       /* CPSR is changed to be executed in ARM state,  disabling normal
11261          interrupts, entering abort mode.  */
11262       /* According to high vector configuration PC is set.  */
11263       /* user hit breakpoint and type reverse, in
11264          that case, we need to go back with previous CPSR and
11265          Program Counter.  */
11266       record_buf[0] = ARM_PS_REGNUM;
11267       record_buf[1] = ARM_LR_REGNUM;
11268       arm_insn_r->reg_rec_count = 2;
11269 
11270       /* Save SPSR also; how?  */
11271       printf_unfiltered (_("Process record does not support instruction "
11272                            "0x%0x at address %s.\n"),arm_insn_r->arm_insn,
11273                            paddress (arm_insn_r->gdbarch, 
11274                            arm_insn_r->this_addr));
11275       return -1;
11276     }
11277   else if (11 == arm_insn_r->decode
11278            && !bit (arm_insn_r->arm_insn, INSN_S_L_BIT_NUM))
11279   {
11280     /* Handle enhanced store insns and DSP insns (e.g. LDRD).  */
11281 
11282     /* Handle str(x) insn */
11283     arm_record_strx(arm_insn_r, &record_buf[0], &record_buf_mem[0],
11284                     ARM_RECORD_STRH);
11285   }
11286   else if (1 == arm_insn_r->decode && 0x12 == opcode1
11287            && sbo_sbz (arm_insn_r->arm_insn, 9, 12, 1))
11288     {
11289       /* Handle BX, branch and link/exchange.  */
11290       /* Branch is chosen by setting T bit of CSPR, bitp[0] of Rm.  */
11291       record_buf[0] = ARM_PS_REGNUM;
11292       arm_insn_r->reg_rec_count = 1;
11293     }
11294   else if (1 == arm_insn_r->decode && 0x16 == opcode1
11295            && sbo_sbz (arm_insn_r->arm_insn, 9, 4, 1)
11296            && sbo_sbz (arm_insn_r->arm_insn, 17, 4, 1))
11297     {
11298       /* Count leading zeros: CLZ.  */
11299       record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11300       arm_insn_r->reg_rec_count = 1;
11301     }
11302   else if (!bit (arm_insn_r->arm_insn, INSN_S_L_BIT_NUM)
11303            && (8 == arm_insn_r->opcode || 10 == arm_insn_r->opcode)
11304            && sbo_sbz (arm_insn_r->arm_insn, 17, 4, 1)
11305            && sbo_sbz (arm_insn_r->arm_insn, 1, 12, 0)
11306           )
11307     {
11308       /* Handle MRS insn.  */
11309       record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11310       arm_insn_r->reg_rec_count = 1;
11311     }
11312   else if (arm_insn_r->opcode <= 15)
11313     {
11314       /* Normal data processing insns.  */
11315       /* Out of 11 shifter operands mode, all the insn modifies destination
11316          register, which is specified by 13-16 decode.  */
11317       record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11318       record_buf[1] = ARM_PS_REGNUM;
11319       arm_insn_r->reg_rec_count = 2;
11320     }
11321   else
11322     {
11323       return -1;
11324     }
11325 
11326   REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
11327   MEM_ALLOC (arm_insn_r->arm_mems, arm_insn_r->mem_rec_count, record_buf_mem);
11328   return 0;
11329 }
11330 
11331 /* Handling opcode 001 insns.  */
11332 
11333 static int
11334 arm_record_data_proc_imm (insn_decode_record *arm_insn_r)
11335 {
11336   uint32_t record_buf[8], record_buf_mem[8];
11337 
11338   arm_insn_r->opcode = bits (arm_insn_r->arm_insn, 21, 24);
11339   arm_insn_r->decode = bits (arm_insn_r->arm_insn, 4, 7);
11340 
11341   if ((9 == arm_insn_r->opcode || 11 == arm_insn_r->opcode)
11342       && 2 == bits (arm_insn_r->arm_insn, 20, 21)
11343       && sbo_sbz (arm_insn_r->arm_insn, 13, 4, 1)
11344      )
11345     {
11346       /* Handle MSR insn.  */
11347       if (9 == arm_insn_r->opcode)
11348         {
11349           /* CSPR is going to be changed.  */
11350           record_buf[0] = ARM_PS_REGNUM;
11351           arm_insn_r->reg_rec_count = 1;
11352         }
11353       else
11354         {
11355           /* SPSR is going to be changed.  */
11356         }
11357     }
11358   else if (arm_insn_r->opcode <= 15)
11359     {
11360       /* Normal data processing insns.  */
11361       /* Out of 11 shifter operands mode, all the insn modifies destination
11362          register, which is specified by 13-16 decode.  */
11363       record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11364       record_buf[1] = ARM_PS_REGNUM;
11365       arm_insn_r->reg_rec_count = 2;
11366     }
11367   else
11368     {
11369       return -1;
11370     }
11371 
11372   REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
11373   MEM_ALLOC (arm_insn_r->arm_mems, arm_insn_r->mem_rec_count, record_buf_mem);
11374   return 0;
11375 }
11376 
11377 /* Handling opcode 010 insns.  */
11378 
11379 static int
11380 arm_record_ld_st_imm_offset (insn_decode_record *arm_insn_r)
11381 {
11382   struct regcache *reg_cache = arm_insn_r->regcache;
11383 
11384   uint32_t reg_src1 = 0 , reg_dest = 0;
11385   uint32_t offset_12 = 0, tgt_mem_addr = 0;
11386   uint32_t record_buf[8], record_buf_mem[8];
11387 
11388   ULONGEST u_regval = 0;
11389 
11390   arm_insn_r->opcode = bits (arm_insn_r->arm_insn, 21, 24);
11391   arm_insn_r->decode = bits (arm_insn_r->arm_insn, 4, 7);
11392 
11393   if (bit (arm_insn_r->arm_insn, INSN_S_L_BIT_NUM))
11394     {
11395       reg_dest = bits (arm_insn_r->arm_insn, 12, 15);
11396       /* LDR insn has a capability to do branching, if
11397          MOV LR, PC is precedded by LDR insn having Rn as R15
11398          in that case, it emulates branch and link insn, and hence we
11399          need to save CSPR and PC as well.  */
11400       if (ARM_PC_REGNUM != reg_dest)
11401         {
11402           record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11403           arm_insn_r->reg_rec_count = 1;
11404         }
11405       else
11406         {
11407           record_buf[0] = reg_dest;
11408           record_buf[1] = ARM_PS_REGNUM;
11409           arm_insn_r->reg_rec_count = 2;
11410         }
11411     }
11412   else
11413     {
11414       /* Store, immediate offset, immediate pre-indexed,
11415          immediate post-indexed.  */
11416       reg_src1 = bits (arm_insn_r->arm_insn, 16, 19);
11417       offset_12 = bits (arm_insn_r->arm_insn, 0, 11);
11418       regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval);
11419       /* U == 1 */
11420       if (bit (arm_insn_r->arm_insn, 23))
11421         {
11422           tgt_mem_addr = u_regval + offset_12;
11423         }
11424       else
11425         {
11426           tgt_mem_addr = u_regval - offset_12;
11427         }
11428 
11429       switch (arm_insn_r->opcode)
11430         {
11431           /* STR.  */
11432           case 8:
11433           case 12:
11434           /* STR.  */
11435           case 9:
11436           case 13:
11437           /* STRT.  */    
11438           case 1:
11439           case 5:
11440           /* STR.  */    
11441           case 4:
11442           case 0:
11443             record_buf_mem[0] = 4;
11444           break;
11445 
11446           /* STRB.  */
11447           case 10:
11448           case 14:
11449           /* STRB.  */    
11450           case 11:
11451           case 15:
11452           /* STRBT.  */    
11453           case 3:
11454           case 7:
11455           /* STRB.  */    
11456           case 2:
11457           case 6:
11458             record_buf_mem[0] = 1;
11459           break;
11460 
11461           default:
11462             gdb_assert_not_reached ("no decoding pattern found");
11463           break;
11464         }
11465       record_buf_mem[1] = tgt_mem_addr;
11466       arm_insn_r->mem_rec_count = 1;
11467 
11468       if (9 == arm_insn_r->opcode || 11 == arm_insn_r->opcode
11469           || 13 == arm_insn_r->opcode || 15 == arm_insn_r->opcode
11470           || 0 == arm_insn_r->opcode || 2 == arm_insn_r->opcode
11471           || 4 == arm_insn_r->opcode || 6 == arm_insn_r->opcode
11472           || 1 == arm_insn_r->opcode || 3 == arm_insn_r->opcode
11473           || 5 == arm_insn_r->opcode || 7 == arm_insn_r->opcode
11474          )
11475         {
11476           /* We are handling pre-indexed mode; post-indexed mode;
11477              where Rn is going to be changed.  */
11478           record_buf[0] = reg_src1;
11479           arm_insn_r->reg_rec_count = 1;
11480         }
11481     }
11482 
11483   REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
11484   MEM_ALLOC (arm_insn_r->arm_mems, arm_insn_r->mem_rec_count, record_buf_mem);
11485   return 0;
11486 }
11487 
11488 /* Handling opcode 011 insns.  */
11489 
11490 static int
11491 arm_record_ld_st_reg_offset (insn_decode_record *arm_insn_r)
11492 {
11493   struct regcache *reg_cache = arm_insn_r->regcache;
11494 
11495   uint32_t shift_imm = 0;
11496   uint32_t reg_src1 = 0, reg_src2 = 0, reg_dest = 0;
11497   uint32_t offset_12 = 0, tgt_mem_addr = 0;
11498   uint32_t record_buf[8], record_buf_mem[8];
11499 
11500   LONGEST s_word;
11501   ULONGEST u_regval[2];
11502 
11503   arm_insn_r->opcode = bits (arm_insn_r->arm_insn, 21, 24);
11504   arm_insn_r->decode = bits (arm_insn_r->arm_insn, 4, 7);
11505 
11506   /* Handle enhanced store insns and LDRD DSP insn,
11507      order begins according to addressing modes for store insns
11508      STRH insn.  */
11509 
11510   /* LDR or STR?  */
11511   if (bit (arm_insn_r->arm_insn, INSN_S_L_BIT_NUM))
11512     {
11513       reg_dest = bits (arm_insn_r->arm_insn, 12, 15);
11514       /* LDR insn has a capability to do branching, if
11515          MOV LR, PC is precedded by LDR insn having Rn as R15
11516          in that case, it emulates branch and link insn, and hence we
11517          need to save CSPR and PC as well.  */
11518       if (15 != reg_dest)
11519         {
11520           record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11521           arm_insn_r->reg_rec_count = 1;
11522         }
11523       else
11524         {
11525           record_buf[0] = reg_dest;
11526           record_buf[1] = ARM_PS_REGNUM;
11527           arm_insn_r->reg_rec_count = 2;
11528         }
11529     }
11530   else
11531     {
11532       if (! bits (arm_insn_r->arm_insn, 4, 11))
11533         {
11534           /* Store insn, register offset and register pre-indexed,
11535              register post-indexed.  */
11536           /* Get Rm.  */
11537           reg_src1 = bits (arm_insn_r->arm_insn, 0, 3);
11538           /* Get Rn.  */
11539           reg_src2 = bits (arm_insn_r->arm_insn, 16, 19);
11540           regcache_raw_read_unsigned (reg_cache, reg_src1
11541                                       , &u_regval[0]);
11542           regcache_raw_read_unsigned (reg_cache, reg_src2
11543                                       , &u_regval[1]);
11544           if (15 == reg_src2)
11545             {
11546               /* If R15 was used as Rn, hence current PC+8.  */
11547               /* Pre-indexed mode doesnt reach here ; illegal insn.  */
11548                 u_regval[0] = u_regval[0] + 8;
11549             }
11550           /* Calculate target store address, Rn +/- Rm, register offset.  */
11551           /* U == 1.  */
11552           if (bit (arm_insn_r->arm_insn, 23))
11553             {
11554               tgt_mem_addr = u_regval[0] + u_regval[1];
11555             }
11556           else
11557             {
11558               tgt_mem_addr = u_regval[1] - u_regval[0];
11559             }
11560 
11561           switch (arm_insn_r->opcode)
11562             {
11563               /* STR.  */
11564               case 8:
11565               case 12:
11566               /* STR.  */    
11567               case 9:
11568               case 13:
11569               /* STRT.  */
11570               case 1:
11571               case 5:
11572               /* STR.  */
11573               case 0:
11574               case 4:
11575                 record_buf_mem[0] = 4;
11576               break;
11577 
11578               /* STRB.  */
11579               case 10:
11580               case 14:
11581               /* STRB.  */
11582               case 11:
11583               case 15:
11584               /* STRBT.  */    
11585               case 3:
11586               case 7:
11587               /* STRB.  */
11588               case 2:
11589               case 6:
11590                 record_buf_mem[0] = 1;
11591               break;
11592 
11593               default:
11594                 gdb_assert_not_reached ("no decoding pattern found");
11595               break;
11596             }
11597           record_buf_mem[1] = tgt_mem_addr;
11598           arm_insn_r->mem_rec_count = 1;
11599 
11600           if (9 == arm_insn_r->opcode || 11 == arm_insn_r->opcode
11601               || 13 == arm_insn_r->opcode || 15 == arm_insn_r->opcode
11602               || 0 == arm_insn_r->opcode || 2 == arm_insn_r->opcode
11603               || 4 == arm_insn_r->opcode || 6 == arm_insn_r->opcode
11604               || 1 == arm_insn_r->opcode || 3 == arm_insn_r->opcode
11605               || 5 == arm_insn_r->opcode || 7 == arm_insn_r->opcode
11606              )
11607             {
11608               /* Rn is going to be changed in pre-indexed mode and
11609                  post-indexed mode as well.  */
11610               record_buf[0] = reg_src2;
11611               arm_insn_r->reg_rec_count = 1;
11612             }
11613         }
11614       else
11615         {
11616           /* Store insn, scaled register offset; scaled pre-indexed.  */
11617           offset_12 = bits (arm_insn_r->arm_insn, 5, 6);
11618           /* Get Rm.  */
11619           reg_src1 = bits (arm_insn_r->arm_insn, 0, 3);
11620           /* Get Rn.  */
11621           reg_src2 = bits (arm_insn_r->arm_insn, 16, 19);
11622           /* Get shift_imm.  */
11623           shift_imm = bits (arm_insn_r->arm_insn, 7, 11);
11624           regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval[0]);
11625           regcache_raw_read_signed (reg_cache, reg_src1, &s_word);
11626           regcache_raw_read_unsigned (reg_cache, reg_src2, &u_regval[1]);
11627           /* Offset_12 used as shift.  */
11628           switch (offset_12)
11629             {
11630               case 0:
11631                 /* Offset_12 used as index.  */
11632                 offset_12 = u_regval[0] << shift_imm;
11633               break;
11634 
11635               case 1:
11636                 offset_12 = (!shift_imm)?0:u_regval[0] >> shift_imm;
11637               break;
11638 
11639               case 2:
11640                 if (!shift_imm)
11641                   {
11642                     if (bit (u_regval[0], 31))
11643                       {
11644                         offset_12 = 0xFFFFFFFF;
11645                       }
11646                     else
11647                       {
11648                         offset_12 = 0;
11649                       }
11650                   }
11651                 else
11652                   {
11653                     /* This is arithmetic shift.  */
11654                     offset_12 = s_word >> shift_imm;
11655                   }
11656                 break;
11657 
11658               case 3:
11659                 if (!shift_imm)
11660                   {
11661                     regcache_raw_read_unsigned (reg_cache, ARM_PS_REGNUM,
11662                                                 &u_regval[1]);
11663                     /* Get C flag value and shift it by 31.  */
11664                     offset_12 = (((bit (u_regval[1], 29)) << 31) \
11665                                   | (u_regval[0]) >> 1);
11666                   }
11667                 else
11668                   {
11669                     offset_12 = (u_regval[0] >> shift_imm) \
11670                                 | (u_regval[0] <<
11671                                 (sizeof(uint32_t) - shift_imm));
11672                   }
11673               break;
11674 
11675               default:
11676                 gdb_assert_not_reached ("no decoding pattern found");
11677               break;
11678             }
11679 
11680           regcache_raw_read_unsigned (reg_cache, reg_src2, &u_regval[1]);
11681           /* bit U set.  */
11682           if (bit (arm_insn_r->arm_insn, 23))
11683             {
11684               tgt_mem_addr = u_regval[1] + offset_12;
11685             }
11686           else
11687             {
11688               tgt_mem_addr = u_regval[1] - offset_12;
11689             }
11690 
11691           switch (arm_insn_r->opcode)
11692             {
11693               /* STR.  */
11694               case 8:
11695               case 12:
11696               /* STR.  */    
11697               case 9:
11698               case 13:
11699               /* STRT.  */
11700               case 1:
11701               case 5:
11702               /* STR.  */
11703               case 0:
11704               case 4:
11705                 record_buf_mem[0] = 4;
11706               break;
11707 
11708               /* STRB.  */
11709               case 10:
11710               case 14:
11711               /* STRB.  */
11712               case 11:
11713               case 15:
11714               /* STRBT.  */    
11715               case 3:
11716               case 7:
11717               /* STRB.  */
11718               case 2:
11719               case 6:
11720                 record_buf_mem[0] = 1;
11721               break;
11722 
11723               default:
11724                 gdb_assert_not_reached ("no decoding pattern found");
11725               break;
11726             }
11727           record_buf_mem[1] = tgt_mem_addr;
11728           arm_insn_r->mem_rec_count = 1;
11729 
11730           if (9 == arm_insn_r->opcode || 11 == arm_insn_r->opcode
11731               || 13 == arm_insn_r->opcode || 15 == arm_insn_r->opcode
11732               || 0 == arm_insn_r->opcode || 2 == arm_insn_r->opcode
11733               || 4 == arm_insn_r->opcode || 6 == arm_insn_r->opcode
11734               || 1 == arm_insn_r->opcode || 3 == arm_insn_r->opcode
11735               || 5 == arm_insn_r->opcode || 7 == arm_insn_r->opcode
11736              )
11737             {
11738               /* Rn is going to be changed in register scaled pre-indexed
11739                  mode,and scaled post indexed mode.  */
11740               record_buf[0] = reg_src2;
11741               arm_insn_r->reg_rec_count = 1;
11742             }
11743         }
11744     }
11745 
11746   REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
11747   MEM_ALLOC (arm_insn_r->arm_mems, arm_insn_r->mem_rec_count, record_buf_mem);
11748   return 0;
11749 }
11750 
11751 /* Handling opcode 100 insns.  */
11752 
11753 static int
11754 arm_record_ld_st_multiple (insn_decode_record *arm_insn_r)
11755 {
11756   struct regcache *reg_cache = arm_insn_r->regcache;
11757 
11758   uint32_t register_list[16] = {0}, register_count = 0, register_bits = 0;
11759   uint32_t reg_src1 = 0, addr_mode = 0, no_of_regs = 0;
11760   uint32_t start_address = 0, index = 0;
11761   uint32_t record_buf[24], record_buf_mem[48];
11762 
11763   ULONGEST u_regval[2] = {0};
11764 
11765   /* This mode is exclusively for load and store multiple.  */
11766   /* Handle incremenrt after/before and decrment after.before mode;
11767      Rn is changing depending on W bit, but as of now we store Rn too
11768      without optimization.  */
11769 
11770   if (bit (arm_insn_r->arm_insn, INSN_S_L_BIT_NUM))
11771     {
11772       /* LDM  (1,2,3) where LDM  (3) changes CPSR too.  */
11773 
11774       if (bit (arm_insn_r->arm_insn, 20) && !bit (arm_insn_r->arm_insn, 22))
11775         {
11776           register_bits = bits (arm_insn_r->arm_insn, 0, 15);
11777           no_of_regs = 15;
11778         }
11779       else
11780         {
11781           register_bits = bits (arm_insn_r->arm_insn, 0, 14);
11782           no_of_regs = 14;
11783         }
11784       /* Get Rn.  */
11785       reg_src1 = bits (arm_insn_r->arm_insn, 16, 19);
11786       while (register_bits)
11787       {
11788         if (register_bits & 0x00000001)
11789           register_list[register_count++] = 1;
11790         register_bits = register_bits >> 1;
11791       }
11792 
11793         /* Extra space for Base Register and CPSR; wihtout optimization.  */
11794         record_buf[register_count] = reg_src1;
11795         record_buf[register_count + 1] = ARM_PS_REGNUM;
11796         arm_insn_r->reg_rec_count = register_count + 2;
11797 
11798         for (register_count = 0; register_count < no_of_regs; register_count++)
11799           {
11800             if  (register_list[register_count])
11801               {
11802                 /* Register_count gives total no of registers
11803                 and dually working as reg number.  */
11804                 record_buf[index] = register_count;
11805                 index++;
11806               }
11807           }
11808 
11809     }
11810   else
11811     {
11812       /* It handles both STM(1) and STM(2).  */
11813       addr_mode = bits (arm_insn_r->arm_insn, 23, 24);    
11814 
11815       register_bits = bits (arm_insn_r->arm_insn, 0, 15);
11816       /* Get Rn.  */
11817       reg_src1 = bits (arm_insn_r->arm_insn, 16, 19);
11818       regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval[0]);
11819       while (register_bits)
11820         {
11821           if (register_bits & 0x00000001)
11822             register_count++;
11823           register_bits = register_bits >> 1;
11824         }
11825 
11826       switch (addr_mode)
11827         {
11828           /* Decrement after.  */
11829           case 0:                          
11830             start_address = (u_regval[0]) - (register_count * 4) + 4;
11831             arm_insn_r->mem_rec_count = register_count;
11832             while (register_count)
11833               {
11834                 record_buf_mem[(register_count * 2) - 1] = start_address;
11835                 record_buf_mem[(register_count * 2) - 2] = 4;
11836                 start_address = start_address + 4;
11837                 register_count--;
11838               }
11839           break;    
11840 
11841           /* Increment after.  */
11842           case 1:
11843             start_address = u_regval[0];
11844             arm_insn_r->mem_rec_count = register_count;
11845             while (register_count)
11846               {
11847                 record_buf_mem[(register_count * 2) - 1] = start_address;
11848                 record_buf_mem[(register_count * 2) - 2] = 4;
11849                 start_address = start_address + 4;
11850                 register_count--;
11851               }
11852           break;    
11853 
11854           /* Decrement before.  */
11855           case 2:
11856 
11857             start_address = (u_regval[0]) - (register_count * 4);
11858             arm_insn_r->mem_rec_count = register_count;
11859             while (register_count)
11860               {
11861                 record_buf_mem[(register_count * 2) - 1] = start_address;
11862                 record_buf_mem[(register_count * 2) - 2] = 4;
11863                 start_address = start_address + 4;
11864                 register_count--;
11865               }
11866           break;    
11867 
11868           /* Increment before.  */
11869           case 3:
11870             start_address = u_regval[0] + 4;
11871             arm_insn_r->mem_rec_count = register_count;
11872             while (register_count)
11873               {
11874                 record_buf_mem[(register_count * 2) - 1] = start_address;
11875                 record_buf_mem[(register_count * 2) - 2] = 4;
11876                 start_address = start_address + 4;
11877                 register_count--;
11878               }
11879           break;    
11880 
11881           default:
11882             gdb_assert_not_reached ("no decoding pattern found");
11883           break;    
11884         }
11885 
11886       /* Base register also changes; based on condition and W bit.  */
11887       /* We save it anyway without optimization.  */
11888       record_buf[0] = reg_src1;
11889       arm_insn_r->reg_rec_count = 1;
11890     }
11891 
11892   REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
11893   MEM_ALLOC (arm_insn_r->arm_mems, arm_insn_r->mem_rec_count, record_buf_mem);
11894   return 0;
11895 }
11896 
11897 /* Handling opcode 101 insns.  */
11898 
11899 static int
11900 arm_record_b_bl (insn_decode_record *arm_insn_r)
11901 {
11902   uint32_t record_buf[8];
11903 
11904   /* Handle B, BL, BLX(1) insns.  */
11905   /* B simply branches so we do nothing here.  */
11906   /* Note: BLX(1) doesnt fall here but instead it falls into
11907      extension space.  */
11908   if (bit (arm_insn_r->arm_insn, 24))
11909   {
11910     record_buf[0] = ARM_LR_REGNUM;
11911     arm_insn_r->reg_rec_count = 1;
11912   }
11913 
11914   REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
11915 
11916   return 0;
11917 }
11918 
11919 /* Handling opcode 110 insns.  */
11920 
11921 static int
11922 arm_record_coproc (insn_decode_record *arm_insn_r)
11923 {
11924   printf_unfiltered (_("Process record does not support instruction "
11925                     "0x%0x at address %s.\n"),arm_insn_r->arm_insn,
11926                     paddress (arm_insn_r->gdbarch, arm_insn_r->this_addr));
11927 
11928   return -1;
11929 }
11930 
11931 /* Handling opcode 111 insns.  */
11932 
11933 static int
11934 arm_record_coproc_data_proc (insn_decode_record *arm_insn_r)
11935 {
11936   struct gdbarch_tdep *tdep = gdbarch_tdep (arm_insn_r->gdbarch);
11937   struct regcache *reg_cache = arm_insn_r->regcache;
11938   uint32_t ret = 0; /* function return value: -1:record failure ;  0:success  */
11939 
11940   /* Handle SWI insn; system call would be handled over here.  */
11941 
11942   arm_insn_r->opcode = bits (arm_insn_r->arm_insn, 24, 27);
11943   if (15 == arm_insn_r->opcode)
11944   {
11945     /* Handle arm syscall insn.  */
11946     if (tdep->arm_swi_record != NULL)
11947       {
11948         ret = tdep->arm_swi_record(reg_cache);
11949       }
11950     else
11951       {
11952         printf_unfiltered (_("no syscall record support\n"));
11953         ret = -1;
11954       }
11955   }
11956 
11957   printf_unfiltered (_("Process record does not support instruction "
11958                         "0x%0x at address %s.\n"),arm_insn_r->arm_insn,
11959                         paddress (arm_insn_r->gdbarch, arm_insn_r->this_addr));
11960   return ret;
11961 }
11962 
11963 /* Handling opcode 000 insns.  */
11964 
11965 static int
11966 thumb_record_shift_add_sub (insn_decode_record *thumb_insn_r)
11967 {
11968   uint32_t record_buf[8];
11969   uint32_t reg_src1 = 0;
11970 
11971   reg_src1 = bits (thumb_insn_r->arm_insn, 0, 2);
11972 
11973   record_buf[0] = ARM_PS_REGNUM;
11974   record_buf[1] = reg_src1;
11975   thumb_insn_r->reg_rec_count = 2;
11976 
11977   REG_ALLOC (thumb_insn_r->arm_regs, thumb_insn_r->reg_rec_count, record_buf);
11978 
11979   return 0;
11980 }
11981 
11982 
11983 /* Handling opcode 001 insns.  */
11984 
11985 static int
11986 thumb_record_add_sub_cmp_mov (insn_decode_record *thumb_insn_r)
11987 {
11988   uint32_t record_buf[8];
11989   uint32_t reg_src1 = 0;
11990 
11991   reg_src1 = bits (thumb_insn_r->arm_insn, 8, 10);
11992 
11993   record_buf[0] = ARM_PS_REGNUM;
11994   record_buf[1] = reg_src1;
11995   thumb_insn_r->reg_rec_count = 2;
11996 
11997   REG_ALLOC (thumb_insn_r->arm_regs, thumb_insn_r->reg_rec_count, record_buf);
11998 
11999   return 0;
12000 }
12001 
12002 /* Handling opcode 010 insns.  */
12003 
12004 static int
12005 thumb_record_ld_st_reg_offset (insn_decode_record *thumb_insn_r)
12006 {
12007   struct regcache *reg_cache =  thumb_insn_r->regcache;
12008   uint32_t record_buf[8], record_buf_mem[8];
12009 
12010   uint32_t reg_src1 = 0, reg_src2 = 0;
12011   uint32_t opcode1 = 0, opcode2 = 0, opcode3 = 0;
12012 
12013   ULONGEST u_regval[2] = {0};
12014 
12015   opcode1 = bits (thumb_insn_r->arm_insn, 10, 12);
12016 
12017   if (bit (thumb_insn_r->arm_insn, 12))
12018     {
12019       /* Handle load/store register offset.  */
12020       opcode2 = bits (thumb_insn_r->arm_insn, 9, 10);
12021       if (opcode2 >= 12 && opcode2 <= 15)
12022         {
12023           /* LDR(2), LDRB(2) , LDRH(2), LDRSB, LDRSH.  */
12024           reg_src1 = bits (thumb_insn_r->arm_insn,0, 2);
12025           record_buf[0] = reg_src1;
12026           thumb_insn_r->reg_rec_count = 1;
12027         }
12028       else if (opcode2 >= 8 && opcode2 <= 10)
12029         {
12030           /* STR(2), STRB(2), STRH(2) .  */
12031           reg_src1 = bits (thumb_insn_r->arm_insn, 3, 5);
12032           reg_src2 = bits (thumb_insn_r->arm_insn, 6, 8);
12033           regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval[0]);
12034           regcache_raw_read_unsigned (reg_cache, reg_src2, &u_regval[1]);
12035           if (8 == opcode2)
12036             record_buf_mem[0] = 4;    /* STR (2).  */
12037           else if (10 == opcode2)
12038             record_buf_mem[0] = 1;    /*  STRB (2).  */
12039           else if (9 == opcode2)
12040             record_buf_mem[0] = 2;    /* STRH (2).  */
12041           record_buf_mem[1] = u_regval[0] + u_regval[1];
12042           thumb_insn_r->mem_rec_count = 1;
12043         }
12044     }
12045   else if (bit (thumb_insn_r->arm_insn, 11))
12046     {
12047       /* Handle load from literal pool.  */
12048       /* LDR(3).  */
12049       reg_src1 = bits (thumb_insn_r->arm_insn, 8, 10);
12050       record_buf[0] = reg_src1;
12051       thumb_insn_r->reg_rec_count = 1;
12052     }
12053   else if (opcode1)
12054     {
12055       opcode2 = bits (thumb_insn_r->arm_insn, 8, 9);
12056       opcode3 = bits (thumb_insn_r->arm_insn, 0, 2);
12057       if ((3 == opcode2) && (!opcode3))
12058         {
12059           /* Branch with exchange.  */
12060           record_buf[0] = ARM_PS_REGNUM;
12061           thumb_insn_r->reg_rec_count = 1;
12062         }
12063       else
12064         {
12065           /* Format 8; special data processing insns.  */
12066           reg_src1 = bits (thumb_insn_r->arm_insn, 0, 2);
12067           record_buf[0] = ARM_PS_REGNUM;
12068           record_buf[1] = reg_src1;
12069           thumb_insn_r->reg_rec_count = 2;
12070         }
12071     }
12072   else
12073     {
12074       /* Format 5; data processing insns.  */
12075       reg_src1 = bits (thumb_insn_r->arm_insn, 0, 2);
12076       if (bit (thumb_insn_r->arm_insn, 7))
12077         {
12078           reg_src1 = reg_src1 + 8;
12079         }
12080       record_buf[0] = ARM_PS_REGNUM;
12081       record_buf[1] = reg_src1;
12082       thumb_insn_r->reg_rec_count = 2;
12083     }
12084 
12085   REG_ALLOC (thumb_insn_r->arm_regs, thumb_insn_r->reg_rec_count, record_buf);
12086   MEM_ALLOC (thumb_insn_r->arm_mems, thumb_insn_r->mem_rec_count,
12087              record_buf_mem);
12088 
12089   return 0;
12090 }
12091 
12092 /* Handling opcode 001 insns.  */
12093 
12094 static int
12095 thumb_record_ld_st_imm_offset (insn_decode_record *thumb_insn_r)
12096 {
12097   struct regcache *reg_cache = thumb_insn_r->regcache;
12098   uint32_t record_buf[8], record_buf_mem[8];
12099 
12100   uint32_t reg_src1 = 0;
12101   uint32_t opcode = 0, immed_5 = 0;
12102 
12103   ULONGEST u_regval = 0;
12104 
12105   opcode = bits (thumb_insn_r->arm_insn, 11, 12);
12106 
12107   if (opcode)
12108     {
12109       /* LDR(1).  */
12110       reg_src1 = bits (thumb_insn_r->arm_insn, 0, 2);
12111       record_buf[0] = reg_src1;
12112       thumb_insn_r->reg_rec_count = 1;
12113     }
12114   else
12115     {
12116       /* STR(1).  */
12117       reg_src1 = bits (thumb_insn_r->arm_insn, 3, 5);
12118       immed_5 = bits (thumb_insn_r->arm_insn, 6, 10);
12119       regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval);
12120       record_buf_mem[0] = 4;
12121       record_buf_mem[1] = u_regval + (immed_5 * 4);
12122       thumb_insn_r->mem_rec_count = 1;
12123     }
12124 
12125   REG_ALLOC (thumb_insn_r->arm_regs, thumb_insn_r->reg_rec_count, record_buf);
12126   MEM_ALLOC (thumb_insn_r->arm_mems, thumb_insn_r->mem_rec_count, 
12127              record_buf_mem);
12128 
12129   return 0;
12130 }
12131 
12132 /* Handling opcode 100 insns.  */
12133 
12134 static int
12135 thumb_record_ld_st_stack (insn_decode_record *thumb_insn_r)
12136 {
12137   struct regcache *reg_cache = thumb_insn_r->regcache;
12138   uint32_t record_buf[8], record_buf_mem[8];
12139 
12140   uint32_t reg_src1 = 0;
12141   uint32_t opcode = 0, immed_8 = 0, immed_5 = 0;
12142 
12143   ULONGEST u_regval = 0;
12144 
12145   opcode = bits (thumb_insn_r->arm_insn, 11, 12);
12146 
12147   if (3 == opcode)
12148     {
12149       /* LDR(4).  */
12150       reg_src1 = bits (thumb_insn_r->arm_insn, 8, 10);
12151       record_buf[0] = reg_src1;
12152       thumb_insn_r->reg_rec_count = 1;
12153     }
12154   else if (1 == opcode)
12155     {
12156       /* LDRH(1).  */
12157       reg_src1 = bits (thumb_insn_r->arm_insn, 0, 2);
12158       record_buf[0] = reg_src1;
12159       thumb_insn_r->reg_rec_count = 1;
12160     }
12161   else if (2 == opcode)
12162     {
12163       /* STR(3).  */
12164       immed_8 = bits (thumb_insn_r->arm_insn, 0, 7);
12165       regcache_raw_read_unsigned (reg_cache, ARM_SP_REGNUM, &u_regval);
12166       record_buf_mem[0] = 4;
12167       record_buf_mem[1] = u_regval + (immed_8 * 4);
12168       thumb_insn_r->mem_rec_count = 1;
12169     }
12170   else if (0 == opcode)
12171     {
12172       /* STRH(1).  */
12173       immed_5 = bits (thumb_insn_r->arm_insn, 6, 10);
12174       reg_src1 = bits (thumb_insn_r->arm_insn, 3, 5);
12175       regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval);
12176       record_buf_mem[0] = 2;
12177       record_buf_mem[1] = u_regval + (immed_5 * 2);
12178       thumb_insn_r->mem_rec_count = 1;
12179     }
12180 
12181   REG_ALLOC (thumb_insn_r->arm_regs, thumb_insn_r->reg_rec_count, record_buf);
12182   MEM_ALLOC (thumb_insn_r->arm_mems, thumb_insn_r->mem_rec_count,
12183              record_buf_mem);
12184 
12185   return 0;
12186 }
12187 
12188 /* Handling opcode 101 insns.  */
12189 
12190 static int
12191 thumb_record_misc (insn_decode_record *thumb_insn_r)
12192 {
12193   struct regcache *reg_cache = thumb_insn_r->regcache;
12194 
12195   uint32_t opcode = 0, opcode1 = 0, opcode2 = 0;
12196   uint32_t register_bits = 0, register_count = 0;
12197   uint32_t register_list[8] = {0}, index = 0, start_address = 0;
12198   uint32_t record_buf[24], record_buf_mem[48];
12199   uint32_t reg_src1;
12200 
12201   ULONGEST u_regval = 0;
12202 
12203   opcode = bits (thumb_insn_r->arm_insn, 11, 12);
12204   opcode1 = bits (thumb_insn_r->arm_insn, 8, 12);
12205   opcode2 = bits (thumb_insn_r->arm_insn, 9, 12);
12206 
12207   if (14 == opcode2)
12208     {
12209       /* POP.  */
12210       register_bits = bits (thumb_insn_r->arm_insn, 0, 7);
12211       while (register_bits)
12212         {
12213           if (register_bits & 0x00000001)
12214             register_list[register_count++] = 1;
12215           register_bits = register_bits >> 1;
12216         }
12217       record_buf[register_count] = ARM_PS_REGNUM;
12218       record_buf[register_count + 1] = ARM_SP_REGNUM;
12219       thumb_insn_r->reg_rec_count = register_count + 2;
12220       for (register_count = 0; register_count < 8; register_count++)
12221         {
12222           if  (register_list[register_count])
12223             {
12224               record_buf[index] = register_count;
12225               index++;
12226             }
12227         }
12228     }
12229   else if (10 == opcode2)
12230     {
12231       /* PUSH.  */
12232       register_bits = bits (thumb_insn_r->arm_insn, 0, 7);
12233       regcache_raw_read_unsigned (reg_cache, ARM_PC_REGNUM, &u_regval);
12234       while (register_bits)
12235         {
12236           if (register_bits & 0x00000001)
12237             register_count++;
12238           register_bits = register_bits >> 1;
12239         }
12240       start_address = u_regval -  \
12241                   (4 * (bit (thumb_insn_r->arm_insn, 8) + register_count));
12242       thumb_insn_r->mem_rec_count = register_count;
12243       while (register_count)
12244         {
12245           record_buf_mem[(register_count * 2) - 1] = start_address;
12246           record_buf_mem[(register_count * 2) - 2] = 4;
12247           start_address = start_address + 4;
12248           register_count--;
12249         }
12250       record_buf[0] = ARM_SP_REGNUM;
12251       thumb_insn_r->reg_rec_count = 1;
12252     }
12253   else if (0x1E == opcode1)
12254     {
12255       /* BKPT insn.  */
12256       /* Handle enhanced software breakpoint insn, BKPT.  */
12257       /* CPSR is changed to be executed in ARM state,  disabling normal
12258          interrupts, entering abort mode.  */
12259       /* According to high vector configuration PC is set.  */
12260       /* User hits breakpoint and type reverse, in that case, we need to go back with 
12261       previous CPSR and Program Counter.  */
12262       record_buf[0] = ARM_PS_REGNUM;
12263       record_buf[1] = ARM_LR_REGNUM;
12264       thumb_insn_r->reg_rec_count = 2;
12265       /* We need to save SPSR value, which is not yet done.  */
12266       printf_unfiltered (_("Process record does not support instruction "
12267                            "0x%0x at address %s.\n"),
12268                            thumb_insn_r->arm_insn,
12269                            paddress (thumb_insn_r->gdbarch,
12270                            thumb_insn_r->this_addr));
12271       return -1;
12272     }
12273   else if ((0 == opcode) || (1 == opcode))
12274     {
12275       /* ADD(5), ADD(6).  */
12276       reg_src1 = bits (thumb_insn_r->arm_insn, 8, 10);
12277       record_buf[0] = reg_src1;
12278       thumb_insn_r->reg_rec_count = 1;
12279     }
12280   else if (2 == opcode)
12281     {
12282       /* ADD(7), SUB(4).  */
12283       reg_src1 = bits (thumb_insn_r->arm_insn, 8, 10);
12284       record_buf[0] = ARM_SP_REGNUM;
12285       thumb_insn_r->reg_rec_count = 1;
12286     }
12287 
12288   REG_ALLOC (thumb_insn_r->arm_regs, thumb_insn_r->reg_rec_count, record_buf);
12289   MEM_ALLOC (thumb_insn_r->arm_mems, thumb_insn_r->mem_rec_count,
12290              record_buf_mem);
12291 
12292   return 0;
12293 }
12294 
12295 /* Handling opcode 110 insns.  */
12296 
12297 static int
12298 thumb_record_ldm_stm_swi (insn_decode_record *thumb_insn_r)                
12299 {
12300   struct gdbarch_tdep *tdep = gdbarch_tdep (thumb_insn_r->gdbarch);
12301   struct regcache *reg_cache = thumb_insn_r->regcache;
12302 
12303   uint32_t ret = 0; /* function return value: -1:record failure ;  0:success  */
12304   uint32_t reg_src1 = 0;
12305   uint32_t opcode1 = 0, opcode2 = 0, register_bits = 0, register_count = 0;
12306   uint32_t register_list[8] = {0}, index = 0, start_address = 0;
12307   uint32_t record_buf[24], record_buf_mem[48];
12308 
12309   ULONGEST u_regval = 0;
12310 
12311   opcode1 = bits (thumb_insn_r->arm_insn, 8, 12);
12312   opcode2 = bits (thumb_insn_r->arm_insn, 11, 12);
12313 
12314   if (1 == opcode2)
12315     {
12316 
12317       /* LDMIA.  */
12318       register_bits = bits (thumb_insn_r->arm_insn, 0, 7);
12319       /* Get Rn.  */
12320       reg_src1 = bits (thumb_insn_r->arm_insn, 8, 10);
12321       while (register_bits)
12322         {
12323           if (register_bits & 0x00000001)
12324             register_list[register_count++] = 1;
12325           register_bits = register_bits >> 1;
12326         }
12327       record_buf[register_count] = reg_src1;
12328       thumb_insn_r->reg_rec_count = register_count + 1;
12329       for (register_count = 0; register_count < 8; register_count++)
12330         {
12331           if (register_list[register_count])
12332             {
12333               record_buf[index] = register_count;
12334               index++;
12335             }
12336         }
12337     }
12338   else if (0 == opcode2)
12339     {
12340       /* It handles both STMIA.  */
12341       register_bits = bits (thumb_insn_r->arm_insn, 0, 7);
12342       /* Get Rn.  */
12343       reg_src1 = bits (thumb_insn_r->arm_insn, 8, 10);
12344       regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval);
12345       while (register_bits)
12346         {
12347           if (register_bits & 0x00000001)
12348             register_count++;
12349           register_bits = register_bits >> 1;
12350         }
12351       start_address = u_regval;
12352       thumb_insn_r->mem_rec_count = register_count;
12353       while (register_count)
12354         {
12355           record_buf_mem[(register_count * 2) - 1] = start_address;
12356           record_buf_mem[(register_count * 2) - 2] = 4;
12357           start_address = start_address + 4;
12358           register_count--;
12359         }
12360     }
12361   else if (0x1F == opcode1)
12362     {
12363         /* Handle arm syscall insn.  */
12364         if (tdep->arm_swi_record != NULL)
12365           {
12366             ret = tdep->arm_swi_record(reg_cache);
12367           }
12368         else
12369           {
12370             printf_unfiltered (_("no syscall record support\n"));
12371             return -1;
12372           }
12373     }
12374 
12375   /* B (1), conditional branch is automatically taken care in process_record,
12376     as PC is saved there.  */
12377 
12378   REG_ALLOC (thumb_insn_r->arm_regs, thumb_insn_r->reg_rec_count, record_buf);
12379   MEM_ALLOC (thumb_insn_r->arm_mems, thumb_insn_r->mem_rec_count,
12380              record_buf_mem);
12381 
12382   return ret;
12383 }
12384 
12385 /* Handling opcode 111 insns.  */
12386 
12387 static int
12388 thumb_record_branch (insn_decode_record *thumb_insn_r)
12389 {
12390   uint32_t record_buf[8];
12391   uint32_t bits_h = 0;
12392 
12393   bits_h = bits (thumb_insn_r->arm_insn, 11, 12);
12394 
12395   if (2 == bits_h || 3 == bits_h)
12396     {
12397       /* BL */
12398       record_buf[0] = ARM_LR_REGNUM;
12399       thumb_insn_r->reg_rec_count = 1;
12400     }
12401   else if (1 == bits_h)
12402     {
12403       /* BLX(1). */
12404       record_buf[0] = ARM_PS_REGNUM;
12405       record_buf[1] = ARM_LR_REGNUM;
12406       thumb_insn_r->reg_rec_count = 2;
12407     }
12408 
12409   /* B(2) is automatically taken care in process_record, as PC is 
12410      saved there.  */
12411 
12412   REG_ALLOC (thumb_insn_r->arm_regs, thumb_insn_r->reg_rec_count, record_buf);
12413 
12414   return 0;     
12415 }
12416 
12417 
12418 /* Extracts arm/thumb/thumb2 insn depending on the size, and returns 0 on success 
12419 and positive val on fauilure.  */
12420 
12421 static int
12422 extract_arm_insn (insn_decode_record *insn_record, uint32_t insn_size)
12423 {
12424   gdb_byte buf[insn_size];
12425 
12426   memset (&buf[0], 0, insn_size);
12427   
12428   if (target_read_memory (insn_record->this_addr, &buf[0], insn_size))
12429     return 1;
12430   insn_record->arm_insn = (uint32_t) extract_unsigned_integer (&buf[0],
12431                            insn_size, 
12432                            gdbarch_byte_order (insn_record->gdbarch));
12433   return 0;
12434 }
12435 
12436 typedef int (*sti_arm_hdl_fp_t) (insn_decode_record*);
12437 
12438 /* Decode arm/thumb insn depending on condition cods and opcodes; and
12439    dispatch it.  */
12440 
12441 static int
12442 decode_insn (insn_decode_record *arm_record, record_type_t record_type,
12443                 uint32_t insn_size)
12444 {
12445 
12446   /* (Starting from numerical 0); bits 25, 26, 27 decodes type of arm instruction.  */
12447   static const sti_arm_hdl_fp_t const arm_handle_insn[8] =                    
12448   {
12449     arm_record_data_proc_misc_ld_str,   /* 000.  */
12450     arm_record_data_proc_imm,           /* 001.  */
12451     arm_record_ld_st_imm_offset,        /* 010.  */
12452     arm_record_ld_st_reg_offset,        /* 011.  */
12453     arm_record_ld_st_multiple,          /* 100.  */
12454     arm_record_b_bl,                    /* 101.  */
12455     arm_record_coproc,                  /* 110.  */
12456     arm_record_coproc_data_proc         /* 111.  */
12457   };
12458 
12459   /* (Starting from numerical 0); bits 13,14,15 decodes type of thumb instruction.  */
12460   static const sti_arm_hdl_fp_t const thumb_handle_insn[8] =
12461   { \
12462     thumb_record_shift_add_sub,        /* 000.  */
12463     thumb_record_add_sub_cmp_mov,      /* 001.  */
12464     thumb_record_ld_st_reg_offset,     /* 010.  */
12465     thumb_record_ld_st_imm_offset,     /* 011.  */
12466     thumb_record_ld_st_stack,          /* 100.  */
12467     thumb_record_misc,                 /* 101.  */
12468     thumb_record_ldm_stm_swi,          /* 110.  */
12469     thumb_record_branch                /* 111.  */
12470   };
12471 
12472   uint32_t ret = 0;    /* return value: negative:failure   0:success.  */
12473   uint32_t insn_id = 0;
12474 
12475   if (extract_arm_insn (arm_record, insn_size))
12476     {
12477       if (record_debug)
12478         {
12479           printf_unfiltered (_("Process record: error reading memory at "
12480                               "addr %s len = %d.\n"),
12481           paddress (arm_record->gdbarch, arm_record->this_addr), insn_size);        
12482         }
12483       return -1;
12484     }
12485   else if (ARM_RECORD == record_type)
12486     {
12487       arm_record->cond = bits (arm_record->arm_insn, 28, 31);
12488       insn_id = bits (arm_record->arm_insn, 25, 27);
12489       ret = arm_record_extension_space (arm_record);
12490       /* If this insn has fallen into extension space 
12491          then we need not decode it anymore.  */
12492       if (ret != -1 && !INSN_RECORDED(arm_record))
12493         {
12494           ret = arm_handle_insn[insn_id] (arm_record);
12495         }
12496     }
12497   else if (THUMB_RECORD == record_type)
12498     {
12499       /* As thumb does not have condition codes, we set negative.  */
12500       arm_record->cond = -1;
12501       insn_id = bits (arm_record->arm_insn, 13, 15);
12502       ret = thumb_handle_insn[insn_id] (arm_record);
12503     }
12504   else if (THUMB2_RECORD == record_type)
12505     {
12506       printf_unfiltered (_("Process record doesnt support thumb32 instruction "
12507                            "0x%0x at address %s.\n"),arm_record->arm_insn,
12508                            paddress (arm_record->gdbarch, 
12509                            arm_record->this_addr));
12510       ret = -1;
12511     }
12512   else
12513     {
12514       /* Throw assertion.  */
12515       gdb_assert_not_reached ("not a valid instruction, could not decode");
12516     }
12517 
12518   return ret;
12519 }
12520 
12521 
12522 /* Cleans up local record registers and memory allocations.  */
12523 
12524 static void 
12525 deallocate_reg_mem (insn_decode_record *record)
12526 {
12527   xfree (record->arm_regs);
12528   xfree (record->arm_mems);    
12529 }
12530 
12531 
12532 /* Parse the current instruction and record the values of the registers and    
12533    memory that will be changed in current instruction to record_arch_list".
12534    Return -1 if something is wrong.  */
12535 
12536 int
12537 arm_process_record (struct gdbarch *gdbarch, struct regcache *regcache, 
12538                         CORE_ADDR insn_addr)
12539 {
12540 
12541   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
12542   uint32_t no_of_rec = 0;
12543   uint32_t ret = 0;  /* return value: -1:record failure ;  0:success  */
12544   ULONGEST t_bit = 0, insn_id = 0;
12545 
12546   ULONGEST u_regval = 0;
12547 
12548   insn_decode_record arm_record;
12549 
12550   memset (&arm_record, 0, sizeof (insn_decode_record));
12551   arm_record.regcache = regcache;
12552   arm_record.this_addr = insn_addr;
12553   arm_record.gdbarch = gdbarch;
12554 
12555 
12556   if (record_debug > 1)
12557     {
12558       fprintf_unfiltered (gdb_stdlog, "Process record: arm_process_record "
12559                                       "addr = %s\n",
12560       paddress (gdbarch, arm_record.this_addr));
12561     }
12562 
12563   if (extract_arm_insn (&arm_record, 2))
12564     {
12565       if (record_debug)
12566         {
12567           printf_unfiltered (_("Process record: error reading memory at "
12568                              "addr %s len = %d.\n"),
12569                              paddress (arm_record.gdbarch, 
12570                              arm_record.this_addr), 2);
12571         }
12572       return -1;
12573     }
12574 
12575   /* Check the insn, whether it is thumb or arm one.  */
12576 
12577   t_bit = arm_psr_thumb_bit (arm_record.gdbarch);
12578   regcache_raw_read_unsigned (arm_record.regcache, ARM_PS_REGNUM, &u_regval);
12579 
12580 
12581   if (!(u_regval & t_bit))
12582     {
12583       /* We are decoding arm insn.  */
12584       ret = decode_insn (&arm_record, ARM_RECORD, ARM_INSN_SIZE_BYTES);
12585     }
12586   else
12587     {
12588       insn_id = bits (arm_record.arm_insn, 11, 15);
12589       /* is it thumb2 insn?  */
12590       if ((0x1D == insn_id) || (0x1E == insn_id) || (0x1F == insn_id))
12591         {
12592           ret = decode_insn (&arm_record, THUMB2_RECORD, 
12593                              THUMB2_INSN_SIZE_BYTES);
12594         }
12595       else
12596         {
12597           /* We are decoding thumb insn.  */
12598           ret = decode_insn (&arm_record, THUMB_RECORD, THUMB_INSN_SIZE_BYTES);
12599         }
12600     }
12601 
12602   if (0 == ret)
12603     {
12604       /* Record registers.  */
12605       record_full_arch_list_add_reg (arm_record.regcache, ARM_PC_REGNUM);
12606       if (arm_record.arm_regs)
12607         {
12608           for (no_of_rec = 0; no_of_rec < arm_record.reg_rec_count; no_of_rec++)
12609             {
12610               if (record_full_arch_list_add_reg
12611                   (arm_record.regcache , arm_record.arm_regs[no_of_rec]))
12612               ret = -1;
12613             }
12614         }
12615       /* Record memories.  */
12616       if (arm_record.arm_mems)
12617         {
12618           for (no_of_rec = 0; no_of_rec < arm_record.mem_rec_count; no_of_rec++)
12619             {
12620               if (record_full_arch_list_add_mem
12621                   ((CORE_ADDR)arm_record.arm_mems[no_of_rec].addr,
12622                    arm_record.arm_mems[no_of_rec].len))
12623                 ret = -1;
12624             }
12625         }
12626 
12627       if (record_full_arch_list_add_end ())
12628         ret = -1;
12629     }
12630 
12631 
12632   deallocate_reg_mem (&arm_record);
12633 
12634   return ret;
12635 }
12636 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines