6ede3a93bc462bd9d3cfb6fa25b3d74ab12f19d9
[external/binutils.git] / gdb / arm-tdep.c
1 /* Common target dependent code for GDB on ARM systems.
2
3    Copyright (C) 1988-2016 Free Software Foundation, Inc.
4
5    This file is part of GDB.
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
19
20 #include "defs.h"
21
22 #include <ctype.h>              /* XXX for isupper ().  */
23
24 #include "frame.h"
25 #include "inferior.h"
26 #include "infrun.h"
27 #include "gdbcmd.h"
28 #include "gdbcore.h"
29 #include "dis-asm.h"            /* For register styles.  */
30 #include "regcache.h"
31 #include "reggroups.h"
32 #include "doublest.h"
33 #include "value.h"
34 #include "arch-utils.h"
35 #include "osabi.h"
36 #include "frame-unwind.h"
37 #include "frame-base.h"
38 #include "trad-frame.h"
39 #include "objfiles.h"
40 #include "dwarf2-frame.h"
41 #include "gdbtypes.h"
42 #include "prologue-value.h"
43 #include "remote.h"
44 #include "target-descriptions.h"
45 #include "user-regs.h"
46 #include "observer.h"
47
48 #include "arch/arm.h"
49 #include "arch/arm-get-next-pcs.h"
50 #include "arm-tdep.h"
51 #include "gdb/sim-arm.h"
52
53 #include "elf-bfd.h"
54 #include "coff/internal.h"
55 #include "elf/arm.h"
56
57 #include "vec.h"
58
59 #include "record.h"
60 #include "record-full.h"
61
62 #include "features/arm-with-m.c"
63 #include "features/arm-with-m-fpa-layout.c"
64 #include "features/arm-with-m-vfp-d16.c"
65 #include "features/arm-with-iwmmxt.c"
66 #include "features/arm-with-vfpv2.c"
67 #include "features/arm-with-vfpv3.c"
68 #include "features/arm-with-neon.c"
69
70 static int arm_debug;
71
72 /* Macros for setting and testing a bit in a minimal symbol that marks
73    it as Thumb function.  The MSB of the minimal symbol's "info" field
74    is used for this purpose.
75
76    MSYMBOL_SET_SPECIAL  Actually sets the "special" bit.
77    MSYMBOL_IS_SPECIAL   Tests the "special" bit in a minimal symbol.  */
78
79 #define MSYMBOL_SET_SPECIAL(msym)                               \
80         MSYMBOL_TARGET_FLAG_1 (msym) = 1
81
82 #define MSYMBOL_IS_SPECIAL(msym)                                \
83         MSYMBOL_TARGET_FLAG_1 (msym)
84
85 /* Per-objfile data used for mapping symbols.  */
86 static const struct objfile_data *arm_objfile_data_key;
87
88 struct arm_mapping_symbol
89 {
90   bfd_vma value;
91   char type;
92 };
93 typedef struct arm_mapping_symbol arm_mapping_symbol_s;
94 DEF_VEC_O(arm_mapping_symbol_s);
95
96 struct arm_per_objfile
97 {
98   VEC(arm_mapping_symbol_s) **section_maps;
99 };
100
101 /* The list of available "set arm ..." and "show arm ..." commands.  */
102 static struct cmd_list_element *setarmcmdlist = NULL;
103 static struct cmd_list_element *showarmcmdlist = NULL;
104
105 /* The type of floating-point to use.  Keep this in sync with enum
106    arm_float_model, and the help string in _initialize_arm_tdep.  */
107 static const char *const fp_model_strings[] =
108 {
109   "auto",
110   "softfpa",
111   "fpa",
112   "softvfp",
113   "vfp",
114   NULL
115 };
116
117 /* A variable that can be configured by the user.  */
118 static enum arm_float_model arm_fp_model = ARM_FLOAT_AUTO;
119 static const char *current_fp_model = "auto";
120
121 /* The ABI to use.  Keep this in sync with arm_abi_kind.  */
122 static const char *const arm_abi_strings[] =
123 {
124   "auto",
125   "APCS",
126   "AAPCS",
127   NULL
128 };
129
130 /* A variable that can be configured by the user.  */
131 static enum arm_abi_kind arm_abi_global = ARM_ABI_AUTO;
132 static const char *arm_abi_string = "auto";
133
134 /* The execution mode to assume.  */
135 static const char *const arm_mode_strings[] =
136   {
137     "auto",
138     "arm",
139     "thumb",
140     NULL
141   };
142
143 static const char *arm_fallback_mode_string = "auto";
144 static const char *arm_force_mode_string = "auto";
145
146 /* Internal override of the execution mode.  -1 means no override,
147    0 means override to ARM mode, 1 means override to Thumb mode.
148    The effect is the same as if arm_force_mode has been set by the
149    user (except the internal override has precedence over a user's
150    arm_force_mode override).  */
151 static int arm_override_mode = -1;
152
153 /* Number of different reg name sets (options).  */
154 static int num_disassembly_options;
155
156 /* The standard register names, and all the valid aliases for them.  Note
157    that `fp', `sp' and `pc' are not added in this alias list, because they
158    have been added as builtin user registers in
159    std-regs.c:_initialize_frame_reg.  */
160 static const struct
161 {
162   const char *name;
163   int regnum;
164 } arm_register_aliases[] = {
165   /* Basic register numbers.  */
166   { "r0", 0 },
167   { "r1", 1 },
168   { "r2", 2 },
169   { "r3", 3 },
170   { "r4", 4 },
171   { "r5", 5 },
172   { "r6", 6 },
173   { "r7", 7 },
174   { "r8", 8 },
175   { "r9", 9 },
176   { "r10", 10 },
177   { "r11", 11 },
178   { "r12", 12 },
179   { "r13", 13 },
180   { "r14", 14 },
181   { "r15", 15 },
182   /* Synonyms (argument and variable registers).  */
183   { "a1", 0 },
184   { "a2", 1 },
185   { "a3", 2 },
186   { "a4", 3 },
187   { "v1", 4 },
188   { "v2", 5 },
189   { "v3", 6 },
190   { "v4", 7 },
191   { "v5", 8 },
192   { "v6", 9 },
193   { "v7", 10 },
194   { "v8", 11 },
195   /* Other platform-specific names for r9.  */
196   { "sb", 9 },
197   { "tr", 9 },
198   /* Special names.  */
199   { "ip", 12 },
200   { "lr", 14 },
201   /* Names used by GCC (not listed in the ARM EABI).  */
202   { "sl", 10 },
203   /* A special name from the older ATPCS.  */
204   { "wr", 7 },
205 };
206
207 static const char *const arm_register_names[] =
208 {"r0",  "r1",  "r2",  "r3",     /*  0  1  2  3 */
209  "r4",  "r5",  "r6",  "r7",     /*  4  5  6  7 */
210  "r8",  "r9",  "r10", "r11",    /*  8  9 10 11 */
211  "r12", "sp",  "lr",  "pc",     /* 12 13 14 15 */
212  "f0",  "f1",  "f2",  "f3",     /* 16 17 18 19 */
213  "f4",  "f5",  "f6",  "f7",     /* 20 21 22 23 */
214  "fps", "cpsr" };               /* 24 25       */
215
216 /* Valid register name styles.  */
217 static const char **valid_disassembly_styles;
218
219 /* Disassembly style to use. Default to "std" register names.  */
220 static const char *disassembly_style;
221
222 /* This is used to keep the bfd arch_info in sync with the disassembly
223    style.  */
224 static void set_disassembly_style_sfunc(char *, int,
225                                          struct cmd_list_element *);
226 static void set_disassembly_style (void);
227
228 static void convert_from_extended (const struct floatformat *, const void *,
229                                    void *, int);
230 static void convert_to_extended (const struct floatformat *, void *,
231                                  const void *, int);
232
233 static enum register_status arm_neon_quad_read (struct gdbarch *gdbarch,
234                                                 struct regcache *regcache,
235                                                 int regnum, gdb_byte *buf);
236 static void arm_neon_quad_write (struct gdbarch *gdbarch,
237                                  struct regcache *regcache,
238                                  int regnum, const gdb_byte *buf);
239
240 static CORE_ADDR
241   arm_get_next_pcs_syscall_next_pc (struct arm_get_next_pcs *self);
242
243
244 /* get_next_pcs operations.  */
245 static struct arm_get_next_pcs_ops arm_get_next_pcs_ops = {
246   arm_get_next_pcs_read_memory_unsigned_integer,
247   arm_get_next_pcs_syscall_next_pc,
248   arm_get_next_pcs_addr_bits_remove,
249   arm_get_next_pcs_is_thumb,
250   NULL,
251 };
252
253 struct arm_prologue_cache
254 {
255   /* The stack pointer at the time this frame was created; i.e. the
256      caller's stack pointer when this function was called.  It is used
257      to identify this frame.  */
258   CORE_ADDR prev_sp;
259
260   /* The frame base for this frame is just prev_sp - frame size.
261      FRAMESIZE is the distance from the frame pointer to the
262      initial stack pointer.  */
263
264   int framesize;
265
266   /* The register used to hold the frame pointer for this frame.  */
267   int framereg;
268
269   /* Saved register offsets.  */
270   struct trad_frame_saved_reg *saved_regs;
271 };
272
273 static CORE_ADDR arm_analyze_prologue (struct gdbarch *gdbarch,
274                                        CORE_ADDR prologue_start,
275                                        CORE_ADDR prologue_end,
276                                        struct arm_prologue_cache *cache);
277
278 /* Architecture version for displaced stepping.  This effects the behaviour of
279    certain instructions, and really should not be hard-wired.  */
280
281 #define DISPLACED_STEPPING_ARCH_VERSION         5
282
283 /* Set to true if the 32-bit mode is in use.  */
284
285 int arm_apcs_32 = 1;
286
287 /* Return the bit mask in ARM_PS_REGNUM that indicates Thumb mode.  */
288
289 int
290 arm_psr_thumb_bit (struct gdbarch *gdbarch)
291 {
292   if (gdbarch_tdep (gdbarch)->is_m)
293     return XPSR_T;
294   else
295     return CPSR_T;
296 }
297
298 /* Determine if the processor is currently executing in Thumb mode.  */
299
300 int
301 arm_is_thumb (struct regcache *regcache)
302 {
303   ULONGEST cpsr;
304   ULONGEST t_bit = arm_psr_thumb_bit (get_regcache_arch (regcache));
305
306   cpsr = regcache_raw_get_unsigned (regcache, ARM_PS_REGNUM);
307
308   return (cpsr & t_bit) != 0;
309 }
310
311 /* Determine if FRAME is executing in Thumb mode.  */
312
313 int
314 arm_frame_is_thumb (struct frame_info *frame)
315 {
316   CORE_ADDR cpsr;
317   ULONGEST t_bit = arm_psr_thumb_bit (get_frame_arch (frame));
318
319   /* Every ARM frame unwinder can unwind the T bit of the CPSR, either
320      directly (from a signal frame or dummy frame) or by interpreting
321      the saved LR (from a prologue or DWARF frame).  So consult it and
322      trust the unwinders.  */
323   cpsr = get_frame_register_unsigned (frame, ARM_PS_REGNUM);
324
325   return (cpsr & t_bit) != 0;
326 }
327
328 /* Callback for VEC_lower_bound.  */
329
330 static inline int
331 arm_compare_mapping_symbols (const struct arm_mapping_symbol *lhs,
332                              const struct arm_mapping_symbol *rhs)
333 {
334   return lhs->value < rhs->value;
335 }
336
337 /* Search for the mapping symbol covering MEMADDR.  If one is found,
338    return its type.  Otherwise, return 0.  If START is non-NULL,
339    set *START to the location of the mapping symbol.  */
340
341 static char
342 arm_find_mapping_symbol (CORE_ADDR memaddr, CORE_ADDR *start)
343 {
344   struct obj_section *sec;
345
346   /* If there are mapping symbols, consult them.  */
347   sec = find_pc_section (memaddr);
348   if (sec != NULL)
349     {
350       struct arm_per_objfile *data;
351       VEC(arm_mapping_symbol_s) *map;
352       struct arm_mapping_symbol map_key = { memaddr - obj_section_addr (sec),
353                                             0 };
354       unsigned int idx;
355
356       data = (struct arm_per_objfile *) objfile_data (sec->objfile,
357                                                       arm_objfile_data_key);
358       if (data != NULL)
359         {
360           map = data->section_maps[sec->the_bfd_section->index];
361           if (!VEC_empty (arm_mapping_symbol_s, map))
362             {
363               struct arm_mapping_symbol *map_sym;
364
365               idx = VEC_lower_bound (arm_mapping_symbol_s, map, &map_key,
366                                      arm_compare_mapping_symbols);
367
368               /* VEC_lower_bound finds the earliest ordered insertion
369                  point.  If the following symbol starts at this exact
370                  address, we use that; otherwise, the preceding
371                  mapping symbol covers this address.  */
372               if (idx < VEC_length (arm_mapping_symbol_s, map))
373                 {
374                   map_sym = VEC_index (arm_mapping_symbol_s, map, idx);
375                   if (map_sym->value == map_key.value)
376                     {
377                       if (start)
378                         *start = map_sym->value + obj_section_addr (sec);
379                       return map_sym->type;
380                     }
381                 }
382
383               if (idx > 0)
384                 {
385                   map_sym = VEC_index (arm_mapping_symbol_s, map, idx - 1);
386                   if (start)
387                     *start = map_sym->value + obj_section_addr (sec);
388                   return map_sym->type;
389                 }
390             }
391         }
392     }
393
394   return 0;
395 }
396
397 /* Determine if the program counter specified in MEMADDR is in a Thumb
398    function.  This function should be called for addresses unrelated to
399    any executing frame; otherwise, prefer arm_frame_is_thumb.  */
400
401 int
402 arm_pc_is_thumb (struct gdbarch *gdbarch, CORE_ADDR memaddr)
403 {
404   struct bound_minimal_symbol sym;
405   char type;
406   struct displaced_step_closure* dsc
407     = get_displaced_step_closure_by_addr(memaddr);
408
409   /* If checking the mode of displaced instruction in copy area, the mode
410      should be determined by instruction on the original address.  */
411   if (dsc)
412     {
413       if (debug_displaced)
414         fprintf_unfiltered (gdb_stdlog,
415                             "displaced: check mode of %.8lx instead of %.8lx\n",
416                             (unsigned long) dsc->insn_addr,
417                             (unsigned long) memaddr);
418       memaddr = dsc->insn_addr;
419     }
420
421   /* If bit 0 of the address is set, assume this is a Thumb address.  */
422   if (IS_THUMB_ADDR (memaddr))
423     return 1;
424
425   /* Respect internal mode override if active.  */
426   if (arm_override_mode != -1)
427     return arm_override_mode;
428
429   /* If the user wants to override the symbol table, let him.  */
430   if (strcmp (arm_force_mode_string, "arm") == 0)
431     return 0;
432   if (strcmp (arm_force_mode_string, "thumb") == 0)
433     return 1;
434
435   /* ARM v6-M and v7-M are always in Thumb mode.  */
436   if (gdbarch_tdep (gdbarch)->is_m)
437     return 1;
438
439   /* If there are mapping symbols, consult them.  */
440   type = arm_find_mapping_symbol (memaddr, NULL);
441   if (type)
442     return type == 't';
443
444   /* Thumb functions have a "special" bit set in minimal symbols.  */
445   sym = lookup_minimal_symbol_by_pc (memaddr);
446   if (sym.minsym)
447     return (MSYMBOL_IS_SPECIAL (sym.minsym));
448
449   /* If the user wants to override the fallback mode, let them.  */
450   if (strcmp (arm_fallback_mode_string, "arm") == 0)
451     return 0;
452   if (strcmp (arm_fallback_mode_string, "thumb") == 0)
453     return 1;
454
455   /* If we couldn't find any symbol, but we're talking to a running
456      target, then trust the current value of $cpsr.  This lets
457      "display/i $pc" always show the correct mode (though if there is
458      a symbol table we will not reach here, so it still may not be
459      displayed in the mode it will be executed).  */
460   if (target_has_registers)
461     return arm_frame_is_thumb (get_current_frame ());
462
463   /* Otherwise we're out of luck; we assume ARM.  */
464   return 0;
465 }
466
467 /* Remove useless bits from addresses in a running program.  */
468 static CORE_ADDR
469 arm_addr_bits_remove (struct gdbarch *gdbarch, CORE_ADDR val)
470 {
471   /* On M-profile devices, do not strip the low bit from EXC_RETURN
472      (the magic exception return address).  */
473   if (gdbarch_tdep (gdbarch)->is_m
474       && (val & 0xfffffff0) == 0xfffffff0)
475     return val;
476
477   if (arm_apcs_32)
478     return UNMAKE_THUMB_ADDR (val);
479   else
480     return (val & 0x03fffffc);
481 }
482
483 /* Return 1 if PC is the start of a compiler helper function which
484    can be safely ignored during prologue skipping.  IS_THUMB is true
485    if the function is known to be a Thumb function due to the way it
486    is being called.  */
487 static int
488 skip_prologue_function (struct gdbarch *gdbarch, CORE_ADDR pc, int is_thumb)
489 {
490   enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
491   struct bound_minimal_symbol msym;
492
493   msym = lookup_minimal_symbol_by_pc (pc);
494   if (msym.minsym != NULL
495       && BMSYMBOL_VALUE_ADDRESS (msym) == pc
496       && MSYMBOL_LINKAGE_NAME (msym.minsym) != NULL)
497     {
498       const char *name = MSYMBOL_LINKAGE_NAME (msym.minsym);
499
500       /* The GNU linker's Thumb call stub to foo is named
501          __foo_from_thumb.  */
502       if (strstr (name, "_from_thumb") != NULL)
503         name += 2;
504
505       /* On soft-float targets, __truncdfsf2 is called to convert promoted
506          arguments to their argument types in non-prototyped
507          functions.  */
508       if (startswith (name, "__truncdfsf2"))
509         return 1;
510       if (startswith (name, "__aeabi_d2f"))
511         return 1;
512
513       /* Internal functions related to thread-local storage.  */
514       if (startswith (name, "__tls_get_addr"))
515         return 1;
516       if (startswith (name, "__aeabi_read_tp"))
517         return 1;
518     }
519   else
520     {
521       /* If we run against a stripped glibc, we may be unable to identify
522          special functions by name.  Check for one important case,
523          __aeabi_read_tp, by comparing the *code* against the default
524          implementation (this is hand-written ARM assembler in glibc).  */
525
526       if (!is_thumb
527           && read_memory_unsigned_integer (pc, 4, byte_order_for_code)
528              == 0xe3e00a0f /* mov r0, #0xffff0fff */
529           && read_memory_unsigned_integer (pc + 4, 4, byte_order_for_code)
530              == 0xe240f01f) /* sub pc, r0, #31 */
531         return 1;
532     }
533
534   return 0;
535 }
536
537 /* Extract the immediate from instruction movw/movt of encoding T.  INSN1 is
538    the first 16-bit of instruction, and INSN2 is the second 16-bit of
539    instruction.  */
540 #define EXTRACT_MOVW_MOVT_IMM_T(insn1, insn2) \
541   ((bits ((insn1), 0, 3) << 12)               \
542    | (bits ((insn1), 10, 10) << 11)           \
543    | (bits ((insn2), 12, 14) << 8)            \
544    | bits ((insn2), 0, 7))
545
546 /* Extract the immediate from instruction movw/movt of encoding A.  INSN is
547    the 32-bit instruction.  */
548 #define EXTRACT_MOVW_MOVT_IMM_A(insn) \
549   ((bits ((insn), 16, 19) << 12) \
550    | bits ((insn), 0, 11))
551
552 /* Decode immediate value; implements ThumbExpandImmediate pseudo-op.  */
553
554 static unsigned int
555 thumb_expand_immediate (unsigned int imm)
556 {
557   unsigned int count = imm >> 7;
558
559   if (count < 8)
560     switch (count / 2)
561       {
562       case 0:
563         return imm & 0xff;
564       case 1:
565         return (imm & 0xff) | ((imm & 0xff) << 16);
566       case 2:
567         return ((imm & 0xff) << 8) | ((imm & 0xff) << 24);
568       case 3:
569         return (imm & 0xff) | ((imm & 0xff) << 8)
570                 | ((imm & 0xff) << 16) | ((imm & 0xff) << 24);
571       }
572
573   return (0x80 | (imm & 0x7f)) << (32 - count);
574 }
575
576 /* Return 1 if the 16-bit Thumb instruction INSN restores SP in
577    epilogue, 0 otherwise.  */
578
579 static int
580 thumb_instruction_restores_sp (unsigned short insn)
581 {
582   return (insn == 0x46bd  /* mov sp, r7 */
583           || (insn & 0xff80) == 0xb000  /* add sp, imm */
584           || (insn & 0xfe00) == 0xbc00);  /* pop <registers> */
585 }
586
587 /* Analyze a Thumb prologue, looking for a recognizable stack frame
588    and frame pointer.  Scan until we encounter a store that could
589    clobber the stack frame unexpectedly, or an unknown instruction.
590    Return the last address which is definitely safe to skip for an
591    initial breakpoint.  */
592
593 static CORE_ADDR
594 thumb_analyze_prologue (struct gdbarch *gdbarch,
595                         CORE_ADDR start, CORE_ADDR limit,
596                         struct arm_prologue_cache *cache)
597 {
598   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
599   enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
600   int i;
601   pv_t regs[16];
602   struct pv_area *stack;
603   struct cleanup *back_to;
604   CORE_ADDR offset;
605   CORE_ADDR unrecognized_pc = 0;
606
607   for (i = 0; i < 16; i++)
608     regs[i] = pv_register (i, 0);
609   stack = make_pv_area (ARM_SP_REGNUM, gdbarch_addr_bit (gdbarch));
610   back_to = make_cleanup_free_pv_area (stack);
611
612   while (start < limit)
613     {
614       unsigned short insn;
615
616       insn = read_memory_unsigned_integer (start, 2, byte_order_for_code);
617
618       if ((insn & 0xfe00) == 0xb400)            /* push { rlist } */
619         {
620           int regno;
621           int mask;
622
623           if (pv_area_store_would_trash (stack, regs[ARM_SP_REGNUM]))
624             break;
625
626           /* Bits 0-7 contain a mask for registers R0-R7.  Bit 8 says
627              whether to save LR (R14).  */
628           mask = (insn & 0xff) | ((insn & 0x100) << 6);
629
630           /* Calculate offsets of saved R0-R7 and LR.  */
631           for (regno = ARM_LR_REGNUM; regno >= 0; regno--)
632             if (mask & (1 << regno))
633               {
634                 regs[ARM_SP_REGNUM] = pv_add_constant (regs[ARM_SP_REGNUM],
635                                                        -4);
636                 pv_area_store (stack, regs[ARM_SP_REGNUM], 4, regs[regno]);
637               }
638         }
639       else if ((insn & 0xff80) == 0xb080)       /* sub sp, #imm */
640         {
641           offset = (insn & 0x7f) << 2;          /* get scaled offset */
642           regs[ARM_SP_REGNUM] = pv_add_constant (regs[ARM_SP_REGNUM],
643                                                  -offset);
644         }
645       else if (thumb_instruction_restores_sp (insn))
646         {
647           /* Don't scan past the epilogue.  */
648           break;
649         }
650       else if ((insn & 0xf800) == 0xa800)       /* add Rd, sp, #imm */
651         regs[bits (insn, 8, 10)] = pv_add_constant (regs[ARM_SP_REGNUM],
652                                                     (insn & 0xff) << 2);
653       else if ((insn & 0xfe00) == 0x1c00        /* add Rd, Rn, #imm */
654                && pv_is_register (regs[bits (insn, 3, 5)], ARM_SP_REGNUM))
655         regs[bits (insn, 0, 2)] = pv_add_constant (regs[bits (insn, 3, 5)],
656                                                    bits (insn, 6, 8));
657       else if ((insn & 0xf800) == 0x3000        /* add Rd, #imm */
658                && pv_is_register (regs[bits (insn, 8, 10)], ARM_SP_REGNUM))
659         regs[bits (insn, 8, 10)] = pv_add_constant (regs[bits (insn, 8, 10)],
660                                                     bits (insn, 0, 7));
661       else if ((insn & 0xfe00) == 0x1800        /* add Rd, Rn, Rm */
662                && pv_is_register (regs[bits (insn, 6, 8)], ARM_SP_REGNUM)
663                && pv_is_constant (regs[bits (insn, 3, 5)]))
664         regs[bits (insn, 0, 2)] = pv_add (regs[bits (insn, 3, 5)],
665                                           regs[bits (insn, 6, 8)]);
666       else if ((insn & 0xff00) == 0x4400        /* add Rd, Rm */
667                && pv_is_constant (regs[bits (insn, 3, 6)]))
668         {
669           int rd = (bit (insn, 7) << 3) + bits (insn, 0, 2);
670           int rm = bits (insn, 3, 6);
671           regs[rd] = pv_add (regs[rd], regs[rm]);
672         }
673       else if ((insn & 0xff00) == 0x4600)       /* mov hi, lo or mov lo, hi */
674         {
675           int dst_reg = (insn & 0x7) + ((insn & 0x80) >> 4);
676           int src_reg = (insn & 0x78) >> 3;
677           regs[dst_reg] = regs[src_reg];
678         }
679       else if ((insn & 0xf800) == 0x9000)       /* str rd, [sp, #off] */
680         {
681           /* Handle stores to the stack.  Normally pushes are used,
682              but with GCC -mtpcs-frame, there may be other stores
683              in the prologue to create the frame.  */
684           int regno = (insn >> 8) & 0x7;
685           pv_t addr;
686
687           offset = (insn & 0xff) << 2;
688           addr = pv_add_constant (regs[ARM_SP_REGNUM], offset);
689
690           if (pv_area_store_would_trash (stack, addr))
691             break;
692
693           pv_area_store (stack, addr, 4, regs[regno]);
694         }
695       else if ((insn & 0xf800) == 0x6000)       /* str rd, [rn, #off] */
696         {
697           int rd = bits (insn, 0, 2);
698           int rn = bits (insn, 3, 5);
699           pv_t addr;
700
701           offset = bits (insn, 6, 10) << 2;
702           addr = pv_add_constant (regs[rn], offset);
703
704           if (pv_area_store_would_trash (stack, addr))
705             break;
706
707           pv_area_store (stack, addr, 4, regs[rd]);
708         }
709       else if (((insn & 0xf800) == 0x7000       /* strb Rd, [Rn, #off] */
710                 || (insn & 0xf800) == 0x8000)   /* strh Rd, [Rn, #off] */
711                && pv_is_register (regs[bits (insn, 3, 5)], ARM_SP_REGNUM))
712         /* Ignore stores of argument registers to the stack.  */
713         ;
714       else if ((insn & 0xf800) == 0xc800        /* ldmia Rn!, { registers } */
715                && pv_is_register (regs[bits (insn, 8, 10)], ARM_SP_REGNUM))
716         /* Ignore block loads from the stack, potentially copying
717            parameters from memory.  */
718         ;
719       else if ((insn & 0xf800) == 0x9800        /* ldr Rd, [Rn, #immed] */
720                || ((insn & 0xf800) == 0x6800    /* ldr Rd, [sp, #immed] */
721                    && pv_is_register (regs[bits (insn, 3, 5)], ARM_SP_REGNUM)))
722         /* Similarly ignore single loads from the stack.  */
723         ;
724       else if ((insn & 0xffc0) == 0x0000        /* lsls Rd, Rm, #0 */
725                || (insn & 0xffc0) == 0x1c00)    /* add Rd, Rn, #0 */
726         /* Skip register copies, i.e. saves to another register
727            instead of the stack.  */
728         ;
729       else if ((insn & 0xf800) == 0x2000)       /* movs Rd, #imm */
730         /* Recognize constant loads; even with small stacks these are necessary
731            on Thumb.  */
732         regs[bits (insn, 8, 10)] = pv_constant (bits (insn, 0, 7));
733       else if ((insn & 0xf800) == 0x4800)       /* ldr Rd, [pc, #imm] */
734         {
735           /* Constant pool loads, for the same reason.  */
736           unsigned int constant;
737           CORE_ADDR loc;
738
739           loc = start + 4 + bits (insn, 0, 7) * 4;
740           constant = read_memory_unsigned_integer (loc, 4, byte_order);
741           regs[bits (insn, 8, 10)] = pv_constant (constant);
742         }
743       else if (thumb_insn_size (insn) == 4) /* 32-bit Thumb-2 instructions.  */
744         {
745           unsigned short inst2;
746
747           inst2 = read_memory_unsigned_integer (start + 2, 2,
748                                                 byte_order_for_code);
749
750           if ((insn & 0xf800) == 0xf000 && (inst2 & 0xe800) == 0xe800)
751             {
752               /* BL, BLX.  Allow some special function calls when
753                  skipping the prologue; GCC generates these before
754                  storing arguments to the stack.  */
755               CORE_ADDR nextpc;
756               int j1, j2, imm1, imm2;
757
758               imm1 = sbits (insn, 0, 10);
759               imm2 = bits (inst2, 0, 10);
760               j1 = bit (inst2, 13);
761               j2 = bit (inst2, 11);
762
763               offset = ((imm1 << 12) + (imm2 << 1));
764               offset ^= ((!j2) << 22) | ((!j1) << 23);
765
766               nextpc = start + 4 + offset;
767               /* For BLX make sure to clear the low bits.  */
768               if (bit (inst2, 12) == 0)
769                 nextpc = nextpc & 0xfffffffc;
770
771               if (!skip_prologue_function (gdbarch, nextpc,
772                                            bit (inst2, 12) != 0))
773                 break;
774             }
775
776           else if ((insn & 0xffd0) == 0xe900    /* stmdb Rn{!},
777                                                    { registers } */
778                    && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
779             {
780               pv_t addr = regs[bits (insn, 0, 3)];
781               int regno;
782
783               if (pv_area_store_would_trash (stack, addr))
784                 break;
785
786               /* Calculate offsets of saved registers.  */
787               for (regno = ARM_LR_REGNUM; regno >= 0; regno--)
788                 if (inst2 & (1 << regno))
789                   {
790                     addr = pv_add_constant (addr, -4);
791                     pv_area_store (stack, addr, 4, regs[regno]);
792                   }
793
794               if (insn & 0x0020)
795                 regs[bits (insn, 0, 3)] = addr;
796             }
797
798           else if ((insn & 0xff50) == 0xe940    /* strd Rt, Rt2,
799                                                    [Rn, #+/-imm]{!} */
800                    && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
801             {
802               int regno1 = bits (inst2, 12, 15);
803               int regno2 = bits (inst2, 8, 11);
804               pv_t addr = regs[bits (insn, 0, 3)];
805
806               offset = inst2 & 0xff;
807               if (insn & 0x0080)
808                 addr = pv_add_constant (addr, offset);
809               else
810                 addr = pv_add_constant (addr, -offset);
811
812               if (pv_area_store_would_trash (stack, addr))
813                 break;
814
815               pv_area_store (stack, addr, 4, regs[regno1]);
816               pv_area_store (stack, pv_add_constant (addr, 4),
817                              4, regs[regno2]);
818
819               if (insn & 0x0020)
820                 regs[bits (insn, 0, 3)] = addr;
821             }
822
823           else if ((insn & 0xfff0) == 0xf8c0    /* str Rt,[Rn,+/-#imm]{!} */
824                    && (inst2 & 0x0c00) == 0x0c00
825                    && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
826             {
827               int regno = bits (inst2, 12, 15);
828               pv_t addr = regs[bits (insn, 0, 3)];
829
830               offset = inst2 & 0xff;
831               if (inst2 & 0x0200)
832                 addr = pv_add_constant (addr, offset);
833               else
834                 addr = pv_add_constant (addr, -offset);
835
836               if (pv_area_store_would_trash (stack, addr))
837                 break;
838
839               pv_area_store (stack, addr, 4, regs[regno]);
840
841               if (inst2 & 0x0100)
842                 regs[bits (insn, 0, 3)] = addr;
843             }
844
845           else if ((insn & 0xfff0) == 0xf8c0    /* str.w Rt,[Rn,#imm] */
846                    && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
847             {
848               int regno = bits (inst2, 12, 15);
849               pv_t addr;
850
851               offset = inst2 & 0xfff;
852               addr = pv_add_constant (regs[bits (insn, 0, 3)], offset);
853
854               if (pv_area_store_would_trash (stack, addr))
855                 break;
856
857               pv_area_store (stack, addr, 4, regs[regno]);
858             }
859
860           else if ((insn & 0xffd0) == 0xf880    /* str{bh}.w Rt,[Rn,#imm] */
861                    && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
862             /* Ignore stores of argument registers to the stack.  */
863             ;
864
865           else if ((insn & 0xffd0) == 0xf800    /* str{bh} Rt,[Rn,#+/-imm] */
866                    && (inst2 & 0x0d00) == 0x0c00
867                    && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
868             /* Ignore stores of argument registers to the stack.  */
869             ;
870
871           else if ((insn & 0xffd0) == 0xe890    /* ldmia Rn[!],
872                                                    { registers } */
873                    && (inst2 & 0x8000) == 0x0000
874                    && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
875             /* Ignore block loads from the stack, potentially copying
876                parameters from memory.  */
877             ;
878
879           else if ((insn & 0xffb0) == 0xe950    /* ldrd Rt, Rt2,
880                                                    [Rn, #+/-imm] */
881                    && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
882             /* Similarly ignore dual loads from the stack.  */
883             ;
884
885           else if ((insn & 0xfff0) == 0xf850    /* ldr Rt,[Rn,#+/-imm] */
886                    && (inst2 & 0x0d00) == 0x0c00
887                    && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
888             /* Similarly ignore single loads from the stack.  */
889             ;
890
891           else if ((insn & 0xfff0) == 0xf8d0    /* ldr.w Rt,[Rn,#imm] */
892                    && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
893             /* Similarly ignore single loads from the stack.  */
894             ;
895
896           else if ((insn & 0xfbf0) == 0xf100    /* add.w Rd, Rn, #imm */
897                    && (inst2 & 0x8000) == 0x0000)
898             {
899               unsigned int imm = ((bits (insn, 10, 10) << 11)
900                                   | (bits (inst2, 12, 14) << 8)
901                                   | bits (inst2, 0, 7));
902
903               regs[bits (inst2, 8, 11)]
904                 = pv_add_constant (regs[bits (insn, 0, 3)],
905                                    thumb_expand_immediate (imm));
906             }
907
908           else if ((insn & 0xfbf0) == 0xf200    /* addw Rd, Rn, #imm */
909                    && (inst2 & 0x8000) == 0x0000)
910             {
911               unsigned int imm = ((bits (insn, 10, 10) << 11)
912                                   | (bits (inst2, 12, 14) << 8)
913                                   | bits (inst2, 0, 7));
914
915               regs[bits (inst2, 8, 11)]
916                 = pv_add_constant (regs[bits (insn, 0, 3)], imm);
917             }
918
919           else if ((insn & 0xfbf0) == 0xf1a0    /* sub.w Rd, Rn, #imm */
920                    && (inst2 & 0x8000) == 0x0000)
921             {
922               unsigned int imm = ((bits (insn, 10, 10) << 11)
923                                   | (bits (inst2, 12, 14) << 8)
924                                   | bits (inst2, 0, 7));
925
926               regs[bits (inst2, 8, 11)]
927                 = pv_add_constant (regs[bits (insn, 0, 3)],
928                                    - (CORE_ADDR) thumb_expand_immediate (imm));
929             }
930
931           else if ((insn & 0xfbf0) == 0xf2a0    /* subw Rd, Rn, #imm */
932                    && (inst2 & 0x8000) == 0x0000)
933             {
934               unsigned int imm = ((bits (insn, 10, 10) << 11)
935                                   | (bits (inst2, 12, 14) << 8)
936                                   | bits (inst2, 0, 7));
937
938               regs[bits (inst2, 8, 11)]
939                 = pv_add_constant (regs[bits (insn, 0, 3)], - (CORE_ADDR) imm);
940             }
941
942           else if ((insn & 0xfbff) == 0xf04f)   /* mov.w Rd, #const */
943             {
944               unsigned int imm = ((bits (insn, 10, 10) << 11)
945                                   | (bits (inst2, 12, 14) << 8)
946                                   | bits (inst2, 0, 7));
947
948               regs[bits (inst2, 8, 11)]
949                 = pv_constant (thumb_expand_immediate (imm));
950             }
951
952           else if ((insn & 0xfbf0) == 0xf240)   /* movw Rd, #const */
953             {
954               unsigned int imm
955                 = EXTRACT_MOVW_MOVT_IMM_T (insn, inst2);
956
957               regs[bits (inst2, 8, 11)] = pv_constant (imm);
958             }
959
960           else if (insn == 0xea5f               /* mov.w Rd,Rm */
961                    && (inst2 & 0xf0f0) == 0)
962             {
963               int dst_reg = (inst2 & 0x0f00) >> 8;
964               int src_reg = inst2 & 0xf;
965               regs[dst_reg] = regs[src_reg];
966             }
967
968           else if ((insn & 0xff7f) == 0xf85f)   /* ldr.w Rt,<label> */
969             {
970               /* Constant pool loads.  */
971               unsigned int constant;
972               CORE_ADDR loc;
973
974               offset = bits (inst2, 0, 11);
975               if (insn & 0x0080)
976                 loc = start + 4 + offset;
977               else
978                 loc = start + 4 - offset;
979
980               constant = read_memory_unsigned_integer (loc, 4, byte_order);
981               regs[bits (inst2, 12, 15)] = pv_constant (constant);
982             }
983
984           else if ((insn & 0xff7f) == 0xe95f)   /* ldrd Rt,Rt2,<label> */
985             {
986               /* Constant pool loads.  */
987               unsigned int constant;
988               CORE_ADDR loc;
989
990               offset = bits (inst2, 0, 7) << 2;
991               if (insn & 0x0080)
992                 loc = start + 4 + offset;
993               else
994                 loc = start + 4 - offset;
995
996               constant = read_memory_unsigned_integer (loc, 4, byte_order);
997               regs[bits (inst2, 12, 15)] = pv_constant (constant);
998
999               constant = read_memory_unsigned_integer (loc + 4, 4, byte_order);
1000               regs[bits (inst2, 8, 11)] = pv_constant (constant);
1001             }
1002
1003           else if (thumb2_instruction_changes_pc (insn, inst2))
1004             {
1005               /* Don't scan past anything that might change control flow.  */
1006               break;
1007             }
1008           else
1009             {
1010               /* The optimizer might shove anything into the prologue,
1011                  so we just skip what we don't recognize.  */
1012               unrecognized_pc = start;
1013             }
1014
1015           start += 2;
1016         }
1017       else if (thumb_instruction_changes_pc (insn))
1018         {
1019           /* Don't scan past anything that might change control flow.  */
1020           break;
1021         }
1022       else
1023         {
1024           /* The optimizer might shove anything into the prologue,
1025              so we just skip what we don't recognize.  */
1026           unrecognized_pc = start;
1027         }
1028
1029       start += 2;
1030     }
1031
1032   if (arm_debug)
1033     fprintf_unfiltered (gdb_stdlog, "Prologue scan stopped at %s\n",
1034                         paddress (gdbarch, start));
1035
1036   if (unrecognized_pc == 0)
1037     unrecognized_pc = start;
1038
1039   if (cache == NULL)
1040     {
1041       do_cleanups (back_to);
1042       return unrecognized_pc;
1043     }
1044
1045   if (pv_is_register (regs[ARM_FP_REGNUM], ARM_SP_REGNUM))
1046     {
1047       /* Frame pointer is fp.  Frame size is constant.  */
1048       cache->framereg = ARM_FP_REGNUM;
1049       cache->framesize = -regs[ARM_FP_REGNUM].k;
1050     }
1051   else if (pv_is_register (regs[THUMB_FP_REGNUM], ARM_SP_REGNUM))
1052     {
1053       /* Frame pointer is r7.  Frame size is constant.  */
1054       cache->framereg = THUMB_FP_REGNUM;
1055       cache->framesize = -regs[THUMB_FP_REGNUM].k;
1056     }
1057   else
1058     {
1059       /* Try the stack pointer... this is a bit desperate.  */
1060       cache->framereg = ARM_SP_REGNUM;
1061       cache->framesize = -regs[ARM_SP_REGNUM].k;
1062     }
1063
1064   for (i = 0; i < 16; i++)
1065     if (pv_area_find_reg (stack, gdbarch, i, &offset))
1066       cache->saved_regs[i].addr = offset;
1067
1068   do_cleanups (back_to);
1069   return unrecognized_pc;
1070 }
1071
1072
1073 /* Try to analyze the instructions starting from PC, which load symbol
1074    __stack_chk_guard.  Return the address of instruction after loading this
1075    symbol, set the dest register number to *BASEREG, and set the size of
1076    instructions for loading symbol in OFFSET.  Return 0 if instructions are
1077    not recognized.  */
1078
1079 static CORE_ADDR
1080 arm_analyze_load_stack_chk_guard(CORE_ADDR pc, struct gdbarch *gdbarch,
1081                                  unsigned int *destreg, int *offset)
1082 {
1083   enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
1084   int is_thumb = arm_pc_is_thumb (gdbarch, pc);
1085   unsigned int low, high, address;
1086
1087   address = 0;
1088   if (is_thumb)
1089     {
1090       unsigned short insn1
1091         = read_memory_unsigned_integer (pc, 2, byte_order_for_code);
1092
1093       if ((insn1 & 0xf800) == 0x4800) /* ldr Rd, #immed */
1094         {
1095           *destreg = bits (insn1, 8, 10);
1096           *offset = 2;
1097           address = (pc & 0xfffffffc) + 4 + (bits (insn1, 0, 7) << 2);
1098           address = read_memory_unsigned_integer (address, 4,
1099                                                   byte_order_for_code);
1100         }
1101       else if ((insn1 & 0xfbf0) == 0xf240) /* movw Rd, #const */
1102         {
1103           unsigned short insn2
1104             = read_memory_unsigned_integer (pc + 2, 2, byte_order_for_code);
1105
1106           low = EXTRACT_MOVW_MOVT_IMM_T (insn1, insn2);
1107
1108           insn1
1109             = read_memory_unsigned_integer (pc + 4, 2, byte_order_for_code);
1110           insn2
1111             = read_memory_unsigned_integer (pc + 6, 2, byte_order_for_code);
1112
1113           /* movt Rd, #const */
1114           if ((insn1 & 0xfbc0) == 0xf2c0)
1115             {
1116               high = EXTRACT_MOVW_MOVT_IMM_T (insn1, insn2);
1117               *destreg = bits (insn2, 8, 11);
1118               *offset = 8;
1119               address = (high << 16 | low);
1120             }
1121         }
1122     }
1123   else
1124     {
1125       unsigned int insn
1126         = read_memory_unsigned_integer (pc, 4, byte_order_for_code);
1127
1128       if ((insn & 0x0e5f0000) == 0x041f0000) /* ldr Rd, [PC, #immed] */
1129         {
1130           address = bits (insn, 0, 11) + pc + 8;
1131           address = read_memory_unsigned_integer (address, 4,
1132                                                   byte_order_for_code);
1133
1134           *destreg = bits (insn, 12, 15);
1135           *offset = 4;
1136         }
1137       else if ((insn & 0x0ff00000) == 0x03000000) /* movw Rd, #const */
1138         {
1139           low = EXTRACT_MOVW_MOVT_IMM_A (insn);
1140
1141           insn
1142             = read_memory_unsigned_integer (pc + 4, 4, byte_order_for_code);
1143
1144           if ((insn & 0x0ff00000) == 0x03400000) /* movt Rd, #const */
1145             {
1146               high = EXTRACT_MOVW_MOVT_IMM_A (insn);
1147               *destreg = bits (insn, 12, 15);
1148               *offset = 8;
1149               address = (high << 16 | low);
1150             }
1151         }
1152     }
1153
1154   return address;
1155 }
1156
1157 /* Try to skip a sequence of instructions used for stack protector.  If PC
1158    points to the first instruction of this sequence, return the address of
1159    first instruction after this sequence, otherwise, return original PC.
1160
1161    On arm, this sequence of instructions is composed of mainly three steps,
1162      Step 1: load symbol __stack_chk_guard,
1163      Step 2: load from address of __stack_chk_guard,
1164      Step 3: store it to somewhere else.
1165
1166    Usually, instructions on step 2 and step 3 are the same on various ARM
1167    architectures.  On step 2, it is one instruction 'ldr Rx, [Rn, #0]', and
1168    on step 3, it is also one instruction 'str Rx, [r7, #immd]'.  However,
1169    instructions in step 1 vary from different ARM architectures.  On ARMv7,
1170    they are,
1171
1172         movw    Rn, #:lower16:__stack_chk_guard
1173         movt    Rn, #:upper16:__stack_chk_guard
1174
1175    On ARMv5t, it is,
1176
1177         ldr     Rn, .Label
1178         ....
1179         .Lable:
1180         .word   __stack_chk_guard
1181
1182    Since ldr/str is a very popular instruction, we can't use them as
1183    'fingerprint' or 'signature' of stack protector sequence.  Here we choose
1184    sequence {movw/movt, ldr}/ldr/str plus symbol __stack_chk_guard, if not
1185    stripped, as the 'fingerprint' of a stack protector cdoe sequence.  */
1186
1187 static CORE_ADDR
1188 arm_skip_stack_protector(CORE_ADDR pc, struct gdbarch *gdbarch)
1189 {
1190   enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
1191   unsigned int basereg;
1192   struct bound_minimal_symbol stack_chk_guard;
1193   int offset;
1194   int is_thumb = arm_pc_is_thumb (gdbarch, pc);
1195   CORE_ADDR addr;
1196
1197   /* Try to parse the instructions in Step 1.  */
1198   addr = arm_analyze_load_stack_chk_guard (pc, gdbarch,
1199                                            &basereg, &offset);
1200   if (!addr)
1201     return pc;
1202
1203   stack_chk_guard = lookup_minimal_symbol_by_pc (addr);
1204   /* ADDR must correspond to a symbol whose name is __stack_chk_guard.
1205      Otherwise, this sequence cannot be for stack protector.  */
1206   if (stack_chk_guard.minsym == NULL
1207       || !startswith (MSYMBOL_LINKAGE_NAME (stack_chk_guard.minsym), "__stack_chk_guard"))
1208    return pc;
1209
1210   if (is_thumb)
1211     {
1212       unsigned int destreg;
1213       unsigned short insn
1214         = read_memory_unsigned_integer (pc + offset, 2, byte_order_for_code);
1215
1216       /* Step 2: ldr Rd, [Rn, #immed], encoding T1.  */
1217       if ((insn & 0xf800) != 0x6800)
1218         return pc;
1219       if (bits (insn, 3, 5) != basereg)
1220         return pc;
1221       destreg = bits (insn, 0, 2);
1222
1223       insn = read_memory_unsigned_integer (pc + offset + 2, 2,
1224                                            byte_order_for_code);
1225       /* Step 3: str Rd, [Rn, #immed], encoding T1.  */
1226       if ((insn & 0xf800) != 0x6000)
1227         return pc;
1228       if (destreg != bits (insn, 0, 2))
1229         return pc;
1230     }
1231   else
1232     {
1233       unsigned int destreg;
1234       unsigned int insn
1235         = read_memory_unsigned_integer (pc + offset, 4, byte_order_for_code);
1236
1237       /* Step 2: ldr Rd, [Rn, #immed], encoding A1.  */
1238       if ((insn & 0x0e500000) != 0x04100000)
1239         return pc;
1240       if (bits (insn, 16, 19) != basereg)
1241         return pc;
1242       destreg = bits (insn, 12, 15);
1243       /* Step 3: str Rd, [Rn, #immed], encoding A1.  */
1244       insn = read_memory_unsigned_integer (pc + offset + 4,
1245                                            4, byte_order_for_code);
1246       if ((insn & 0x0e500000) != 0x04000000)
1247         return pc;
1248       if (bits (insn, 12, 15) != destreg)
1249         return pc;
1250     }
1251   /* The size of total two instructions ldr/str is 4 on Thumb-2, while 8
1252      on arm.  */
1253   if (is_thumb)
1254     return pc + offset + 4;
1255   else
1256     return pc + offset + 8;
1257 }
1258
1259 /* Advance the PC across any function entry prologue instructions to
1260    reach some "real" code.
1261
1262    The APCS (ARM Procedure Call Standard) defines the following
1263    prologue:
1264
1265    mov          ip, sp
1266    [stmfd       sp!, {a1,a2,a3,a4}]
1267    stmfd        sp!, {...,fp,ip,lr,pc}
1268    [stfe        f7, [sp, #-12]!]
1269    [stfe        f6, [sp, #-12]!]
1270    [stfe        f5, [sp, #-12]!]
1271    [stfe        f4, [sp, #-12]!]
1272    sub fp, ip, #nn @@ nn == 20 or 4 depending on second insn.  */
1273
1274 static CORE_ADDR
1275 arm_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR pc)
1276 {
1277   CORE_ADDR func_addr, limit_pc;
1278
1279   /* See if we can determine the end of the prologue via the symbol table.
1280      If so, then return either PC, or the PC after the prologue, whichever
1281      is greater.  */
1282   if (find_pc_partial_function (pc, NULL, &func_addr, NULL))
1283     {
1284       CORE_ADDR post_prologue_pc
1285         = skip_prologue_using_sal (gdbarch, func_addr);
1286       struct compunit_symtab *cust = find_pc_compunit_symtab (func_addr);
1287
1288       if (post_prologue_pc)
1289         post_prologue_pc
1290           = arm_skip_stack_protector (post_prologue_pc, gdbarch);
1291
1292
1293       /* GCC always emits a line note before the prologue and another
1294          one after, even if the two are at the same address or on the
1295          same line.  Take advantage of this so that we do not need to
1296          know every instruction that might appear in the prologue.  We
1297          will have producer information for most binaries; if it is
1298          missing (e.g. for -gstabs), assuming the GNU tools.  */
1299       if (post_prologue_pc
1300           && (cust == NULL
1301               || COMPUNIT_PRODUCER (cust) == NULL
1302               || startswith (COMPUNIT_PRODUCER (cust), "GNU ")
1303               || startswith (COMPUNIT_PRODUCER (cust), "clang ")))
1304         return post_prologue_pc;
1305
1306       if (post_prologue_pc != 0)
1307         {
1308           CORE_ADDR analyzed_limit;
1309
1310           /* For non-GCC compilers, make sure the entire line is an
1311              acceptable prologue; GDB will round this function's
1312              return value up to the end of the following line so we
1313              can not skip just part of a line (and we do not want to).
1314
1315              RealView does not treat the prologue specially, but does
1316              associate prologue code with the opening brace; so this
1317              lets us skip the first line if we think it is the opening
1318              brace.  */
1319           if (arm_pc_is_thumb (gdbarch, func_addr))
1320             analyzed_limit = thumb_analyze_prologue (gdbarch, func_addr,
1321                                                      post_prologue_pc, NULL);
1322           else
1323             analyzed_limit = arm_analyze_prologue (gdbarch, func_addr,
1324                                                    post_prologue_pc, NULL);
1325
1326           if (analyzed_limit != post_prologue_pc)
1327             return func_addr;
1328
1329           return post_prologue_pc;
1330         }
1331     }
1332
1333   /* Can't determine prologue from the symbol table, need to examine
1334      instructions.  */
1335
1336   /* Find an upper limit on the function prologue using the debug
1337      information.  If the debug information could not be used to provide
1338      that bound, then use an arbitrary large number as the upper bound.  */
1339   /* Like arm_scan_prologue, stop no later than pc + 64.  */
1340   limit_pc = skip_prologue_using_sal (gdbarch, pc);
1341   if (limit_pc == 0)
1342     limit_pc = pc + 64;          /* Magic.  */
1343
1344
1345   /* Check if this is Thumb code.  */
1346   if (arm_pc_is_thumb (gdbarch, pc))
1347     return thumb_analyze_prologue (gdbarch, pc, limit_pc, NULL);
1348   else
1349     return arm_analyze_prologue (gdbarch, pc, limit_pc, NULL);
1350 }
1351
1352 /* *INDENT-OFF* */
1353 /* Function: thumb_scan_prologue (helper function for arm_scan_prologue)
1354    This function decodes a Thumb function prologue to determine:
1355      1) the size of the stack frame
1356      2) which registers are saved on it
1357      3) the offsets of saved regs
1358      4) the offset from the stack pointer to the frame pointer
1359
1360    A typical Thumb function prologue would create this stack frame
1361    (offsets relative to FP)
1362      old SP ->  24  stack parameters
1363                 20  LR
1364                 16  R7
1365      R7 ->       0  local variables (16 bytes)
1366      SP ->     -12  additional stack space (12 bytes)
1367    The frame size would thus be 36 bytes, and the frame offset would be
1368    12 bytes.  The frame register is R7.
1369    
1370    The comments for thumb_skip_prolog() describe the algorithm we use
1371    to detect the end of the prolog.  */
1372 /* *INDENT-ON* */
1373
1374 static void
1375 thumb_scan_prologue (struct gdbarch *gdbarch, CORE_ADDR prev_pc,
1376                      CORE_ADDR block_addr, struct arm_prologue_cache *cache)
1377 {
1378   CORE_ADDR prologue_start;
1379   CORE_ADDR prologue_end;
1380
1381   if (find_pc_partial_function (block_addr, NULL, &prologue_start,
1382                                 &prologue_end))
1383     {
1384       /* See comment in arm_scan_prologue for an explanation of
1385          this heuristics.  */
1386       if (prologue_end > prologue_start + 64)
1387         {
1388           prologue_end = prologue_start + 64;
1389         }
1390     }
1391   else
1392     /* We're in the boondocks: we have no idea where the start of the
1393        function is.  */
1394     return;
1395
1396   prologue_end = min (prologue_end, prev_pc);
1397
1398   thumb_analyze_prologue (gdbarch, prologue_start, prologue_end, cache);
1399 }
1400
1401 /* Return 1 if the ARM instruction INSN restores SP in epilogue, 0
1402    otherwise.  */
1403
1404 static int
1405 arm_instruction_restores_sp (unsigned int insn)
1406 {
1407   if (bits (insn, 28, 31) != INST_NV)
1408     {
1409       if ((insn & 0x0df0f000) == 0x0080d000
1410           /* ADD SP (register or immediate).  */
1411           || (insn & 0x0df0f000) == 0x0040d000
1412           /* SUB SP (register or immediate).  */
1413           || (insn & 0x0ffffff0) == 0x01a0d000
1414           /* MOV SP.  */
1415           || (insn & 0x0fff0000) == 0x08bd0000
1416           /* POP (LDMIA).  */
1417           || (insn & 0x0fff0000) == 0x049d0000)
1418           /* POP of a single register.  */
1419         return 1;
1420     }
1421
1422   return 0;
1423 }
1424
1425 /* Analyze an ARM mode prologue starting at PROLOGUE_START and
1426    continuing no further than PROLOGUE_END.  If CACHE is non-NULL,
1427    fill it in.  Return the first address not recognized as a prologue
1428    instruction.
1429
1430    We recognize all the instructions typically found in ARM prologues,
1431    plus harmless instructions which can be skipped (either for analysis
1432    purposes, or a more restrictive set that can be skipped when finding
1433    the end of the prologue).  */
1434
1435 static CORE_ADDR
1436 arm_analyze_prologue (struct gdbarch *gdbarch,
1437                       CORE_ADDR prologue_start, CORE_ADDR prologue_end,
1438                       struct arm_prologue_cache *cache)
1439 {
1440   enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
1441   int regno;
1442   CORE_ADDR offset, current_pc;
1443   pv_t regs[ARM_FPS_REGNUM];
1444   struct pv_area *stack;
1445   struct cleanup *back_to;
1446   CORE_ADDR unrecognized_pc = 0;
1447
1448   /* Search the prologue looking for instructions that set up the
1449      frame pointer, adjust the stack pointer, and save registers.
1450
1451      Be careful, however, and if it doesn't look like a prologue,
1452      don't try to scan it.  If, for instance, a frameless function
1453      begins with stmfd sp!, then we will tell ourselves there is
1454      a frame, which will confuse stack traceback, as well as "finish" 
1455      and other operations that rely on a knowledge of the stack
1456      traceback.  */
1457
1458   for (regno = 0; regno < ARM_FPS_REGNUM; regno++)
1459     regs[regno] = pv_register (regno, 0);
1460   stack = make_pv_area (ARM_SP_REGNUM, gdbarch_addr_bit (gdbarch));
1461   back_to = make_cleanup_free_pv_area (stack);
1462
1463   for (current_pc = prologue_start;
1464        current_pc < prologue_end;
1465        current_pc += 4)
1466     {
1467       unsigned int insn
1468         = read_memory_unsigned_integer (current_pc, 4, byte_order_for_code);
1469
1470       if (insn == 0xe1a0c00d)           /* mov ip, sp */
1471         {
1472           regs[ARM_IP_REGNUM] = regs[ARM_SP_REGNUM];
1473           continue;
1474         }
1475       else if ((insn & 0xfff00000) == 0xe2800000        /* add Rd, Rn, #n */
1476                && pv_is_register (regs[bits (insn, 16, 19)], ARM_SP_REGNUM))
1477         {
1478           unsigned imm = insn & 0xff;                   /* immediate value */
1479           unsigned rot = (insn & 0xf00) >> 7;           /* rotate amount */
1480           int rd = bits (insn, 12, 15);
1481           imm = (imm >> rot) | (imm << (32 - rot));
1482           regs[rd] = pv_add_constant (regs[bits (insn, 16, 19)], imm);
1483           continue;
1484         }
1485       else if ((insn & 0xfff00000) == 0xe2400000        /* sub Rd, Rn, #n */
1486                && pv_is_register (regs[bits (insn, 16, 19)], ARM_SP_REGNUM))
1487         {
1488           unsigned imm = insn & 0xff;                   /* immediate value */
1489           unsigned rot = (insn & 0xf00) >> 7;           /* rotate amount */
1490           int rd = bits (insn, 12, 15);
1491           imm = (imm >> rot) | (imm << (32 - rot));
1492           regs[rd] = pv_add_constant (regs[bits (insn, 16, 19)], -imm);
1493           continue;
1494         }
1495       else if ((insn & 0xffff0fff) == 0xe52d0004)       /* str Rd,
1496                                                            [sp, #-4]! */
1497         {
1498           if (pv_area_store_would_trash (stack, regs[ARM_SP_REGNUM]))
1499             break;
1500           regs[ARM_SP_REGNUM] = pv_add_constant (regs[ARM_SP_REGNUM], -4);
1501           pv_area_store (stack, regs[ARM_SP_REGNUM], 4,
1502                          regs[bits (insn, 12, 15)]);
1503           continue;
1504         }
1505       else if ((insn & 0xffff0000) == 0xe92d0000)
1506         /* stmfd sp!, {..., fp, ip, lr, pc}
1507            or
1508            stmfd sp!, {a1, a2, a3, a4}  */
1509         {
1510           int mask = insn & 0xffff;
1511
1512           if (pv_area_store_would_trash (stack, regs[ARM_SP_REGNUM]))
1513             break;
1514
1515           /* Calculate offsets of saved registers.  */
1516           for (regno = ARM_PC_REGNUM; regno >= 0; regno--)
1517             if (mask & (1 << regno))
1518               {
1519                 regs[ARM_SP_REGNUM]
1520                   = pv_add_constant (regs[ARM_SP_REGNUM], -4);
1521                 pv_area_store (stack, regs[ARM_SP_REGNUM], 4, regs[regno]);
1522               }
1523         }
1524       else if ((insn & 0xffff0000) == 0xe54b0000        /* strb rx,[r11,#-n] */
1525                || (insn & 0xffff00f0) == 0xe14b00b0     /* strh rx,[r11,#-n] */
1526                || (insn & 0xffffc000) == 0xe50b0000)    /* str  rx,[r11,#-n] */
1527         {
1528           /* No need to add this to saved_regs -- it's just an arg reg.  */
1529           continue;
1530         }
1531       else if ((insn & 0xffff0000) == 0xe5cd0000        /* strb rx,[sp,#n] */
1532                || (insn & 0xffff00f0) == 0xe1cd00b0     /* strh rx,[sp,#n] */
1533                || (insn & 0xffffc000) == 0xe58d0000)    /* str  rx,[sp,#n] */
1534         {
1535           /* No need to add this to saved_regs -- it's just an arg reg.  */
1536           continue;
1537         }
1538       else if ((insn & 0xfff00000) == 0xe8800000        /* stm Rn,
1539                                                            { registers } */
1540                && pv_is_register (regs[bits (insn, 16, 19)], ARM_SP_REGNUM))
1541         {
1542           /* No need to add this to saved_regs -- it's just arg regs.  */
1543           continue;
1544         }
1545       else if ((insn & 0xfffff000) == 0xe24cb000)       /* sub fp, ip #n */
1546         {
1547           unsigned imm = insn & 0xff;                   /* immediate value */
1548           unsigned rot = (insn & 0xf00) >> 7;           /* rotate amount */
1549           imm = (imm >> rot) | (imm << (32 - rot));
1550           regs[ARM_FP_REGNUM] = pv_add_constant (regs[ARM_IP_REGNUM], -imm);
1551         }
1552       else if ((insn & 0xfffff000) == 0xe24dd000)       /* sub sp, sp #n */
1553         {
1554           unsigned imm = insn & 0xff;                   /* immediate value */
1555           unsigned rot = (insn & 0xf00) >> 7;           /* rotate amount */
1556           imm = (imm >> rot) | (imm << (32 - rot));
1557           regs[ARM_SP_REGNUM] = pv_add_constant (regs[ARM_SP_REGNUM], -imm);
1558         }
1559       else if ((insn & 0xffff7fff) == 0xed6d0103        /* stfe f?,
1560                                                            [sp, -#c]! */
1561                && gdbarch_tdep (gdbarch)->have_fpa_registers)
1562         {
1563           if (pv_area_store_would_trash (stack, regs[ARM_SP_REGNUM]))
1564             break;
1565
1566           regs[ARM_SP_REGNUM] = pv_add_constant (regs[ARM_SP_REGNUM], -12);
1567           regno = ARM_F0_REGNUM + ((insn >> 12) & 0x07);
1568           pv_area_store (stack, regs[ARM_SP_REGNUM], 12, regs[regno]);
1569         }
1570       else if ((insn & 0xffbf0fff) == 0xec2d0200        /* sfmfd f0, 4,
1571                                                            [sp!] */
1572                && gdbarch_tdep (gdbarch)->have_fpa_registers)
1573         {
1574           int n_saved_fp_regs;
1575           unsigned int fp_start_reg, fp_bound_reg;
1576
1577           if (pv_area_store_would_trash (stack, regs[ARM_SP_REGNUM]))
1578             break;
1579
1580           if ((insn & 0x800) == 0x800)          /* N0 is set */
1581             {
1582               if ((insn & 0x40000) == 0x40000)  /* N1 is set */
1583                 n_saved_fp_regs = 3;
1584               else
1585                 n_saved_fp_regs = 1;
1586             }
1587           else
1588             {
1589               if ((insn & 0x40000) == 0x40000)  /* N1 is set */
1590                 n_saved_fp_regs = 2;
1591               else
1592                 n_saved_fp_regs = 4;
1593             }
1594
1595           fp_start_reg = ARM_F0_REGNUM + ((insn >> 12) & 0x7);
1596           fp_bound_reg = fp_start_reg + n_saved_fp_regs;
1597           for (; fp_start_reg < fp_bound_reg; fp_start_reg++)
1598             {
1599               regs[ARM_SP_REGNUM] = pv_add_constant (regs[ARM_SP_REGNUM], -12);
1600               pv_area_store (stack, regs[ARM_SP_REGNUM], 12,
1601                              regs[fp_start_reg++]);
1602             }
1603         }
1604       else if ((insn & 0xff000000) == 0xeb000000 && cache == NULL) /* bl */
1605         {
1606           /* Allow some special function calls when skipping the
1607              prologue; GCC generates these before storing arguments to
1608              the stack.  */
1609           CORE_ADDR dest = BranchDest (current_pc, insn);
1610
1611           if (skip_prologue_function (gdbarch, dest, 0))
1612             continue;
1613           else
1614             break;
1615         }
1616       else if ((insn & 0xf0000000) != 0xe0000000)
1617         break;                  /* Condition not true, exit early.  */
1618       else if (arm_instruction_changes_pc (insn))
1619         /* Don't scan past anything that might change control flow.  */
1620         break;
1621       else if (arm_instruction_restores_sp (insn))
1622         {
1623           /* Don't scan past the epilogue.  */
1624           break;
1625         }
1626       else if ((insn & 0xfe500000) == 0xe8100000        /* ldm */
1627                && pv_is_register (regs[bits (insn, 16, 19)], ARM_SP_REGNUM))
1628         /* Ignore block loads from the stack, potentially copying
1629            parameters from memory.  */
1630         continue;
1631       else if ((insn & 0xfc500000) == 0xe4100000
1632                && pv_is_register (regs[bits (insn, 16, 19)], ARM_SP_REGNUM))
1633         /* Similarly ignore single loads from the stack.  */
1634         continue;
1635       else if ((insn & 0xffff0ff0) == 0xe1a00000)
1636         /* MOV Rd, Rm.  Skip register copies, i.e. saves to another
1637            register instead of the stack.  */
1638         continue;
1639       else
1640         {
1641           /* The optimizer might shove anything into the prologue, if
1642              we build up cache (cache != NULL) from scanning prologue,
1643              we just skip what we don't recognize and scan further to
1644              make cache as complete as possible.  However, if we skip
1645              prologue, we'll stop immediately on unrecognized
1646              instruction.  */
1647           unrecognized_pc = current_pc;
1648           if (cache != NULL)
1649             continue;
1650           else
1651             break;
1652         }
1653     }
1654
1655   if (unrecognized_pc == 0)
1656     unrecognized_pc = current_pc;
1657
1658   if (cache)
1659     {
1660       int framereg, framesize;
1661
1662       /* The frame size is just the distance from the frame register
1663          to the original stack pointer.  */
1664       if (pv_is_register (regs[ARM_FP_REGNUM], ARM_SP_REGNUM))
1665         {
1666           /* Frame pointer is fp.  */
1667           framereg = ARM_FP_REGNUM;
1668           framesize = -regs[ARM_FP_REGNUM].k;
1669         }
1670       else
1671         {
1672           /* Try the stack pointer... this is a bit desperate.  */
1673           framereg = ARM_SP_REGNUM;
1674           framesize = -regs[ARM_SP_REGNUM].k;
1675         }
1676
1677       cache->framereg = framereg;
1678       cache->framesize = framesize;
1679
1680       for (regno = 0; regno < ARM_FPS_REGNUM; regno++)
1681         if (pv_area_find_reg (stack, gdbarch, regno, &offset))
1682           cache->saved_regs[regno].addr = offset;
1683     }
1684
1685   if (arm_debug)
1686     fprintf_unfiltered (gdb_stdlog, "Prologue scan stopped at %s\n",
1687                         paddress (gdbarch, unrecognized_pc));
1688
1689   do_cleanups (back_to);
1690   return unrecognized_pc;
1691 }
1692
1693 static void
1694 arm_scan_prologue (struct frame_info *this_frame,
1695                    struct arm_prologue_cache *cache)
1696 {
1697   struct gdbarch *gdbarch = get_frame_arch (this_frame);
1698   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1699   CORE_ADDR prologue_start, prologue_end;
1700   CORE_ADDR prev_pc = get_frame_pc (this_frame);
1701   CORE_ADDR block_addr = get_frame_address_in_block (this_frame);
1702
1703   /* Assume there is no frame until proven otherwise.  */
1704   cache->framereg = ARM_SP_REGNUM;
1705   cache->framesize = 0;
1706
1707   /* Check for Thumb prologue.  */
1708   if (arm_frame_is_thumb (this_frame))
1709     {
1710       thumb_scan_prologue (gdbarch, prev_pc, block_addr, cache);
1711       return;
1712     }
1713
1714   /* Find the function prologue.  If we can't find the function in
1715      the symbol table, peek in the stack frame to find the PC.  */
1716   if (find_pc_partial_function (block_addr, NULL, &prologue_start,
1717                                 &prologue_end))
1718     {
1719       /* One way to find the end of the prologue (which works well
1720          for unoptimized code) is to do the following:
1721
1722             struct symtab_and_line sal = find_pc_line (prologue_start, 0);
1723
1724             if (sal.line == 0)
1725               prologue_end = prev_pc;
1726             else if (sal.end < prologue_end)
1727               prologue_end = sal.end;
1728
1729          This mechanism is very accurate so long as the optimizer
1730          doesn't move any instructions from the function body into the
1731          prologue.  If this happens, sal.end will be the last
1732          instruction in the first hunk of prologue code just before
1733          the first instruction that the scheduler has moved from
1734          the body to the prologue.
1735
1736          In order to make sure that we scan all of the prologue
1737          instructions, we use a slightly less accurate mechanism which
1738          may scan more than necessary.  To help compensate for this
1739          lack of accuracy, the prologue scanning loop below contains
1740          several clauses which'll cause the loop to terminate early if
1741          an implausible prologue instruction is encountered.
1742
1743          The expression
1744
1745               prologue_start + 64
1746
1747          is a suitable endpoint since it accounts for the largest
1748          possible prologue plus up to five instructions inserted by
1749          the scheduler.  */
1750
1751       if (prologue_end > prologue_start + 64)
1752         {
1753           prologue_end = prologue_start + 64;   /* See above.  */
1754         }
1755     }
1756   else
1757     {
1758       /* We have no symbol information.  Our only option is to assume this
1759          function has a standard stack frame and the normal frame register.
1760          Then, we can find the value of our frame pointer on entrance to
1761          the callee (or at the present moment if this is the innermost frame).
1762          The value stored there should be the address of the stmfd + 8.  */
1763       CORE_ADDR frame_loc;
1764       LONGEST return_value;
1765
1766       frame_loc = get_frame_register_unsigned (this_frame, ARM_FP_REGNUM);
1767       if (!safe_read_memory_integer (frame_loc, 4, byte_order, &return_value))
1768         return;
1769       else
1770         {
1771           prologue_start = gdbarch_addr_bits_remove
1772                              (gdbarch, return_value) - 8;
1773           prologue_end = prologue_start + 64;   /* See above.  */
1774         }
1775     }
1776
1777   if (prev_pc < prologue_end)
1778     prologue_end = prev_pc;
1779
1780   arm_analyze_prologue (gdbarch, prologue_start, prologue_end, cache);
1781 }
1782
1783 static struct arm_prologue_cache *
1784 arm_make_prologue_cache (struct frame_info *this_frame)
1785 {
1786   int reg;
1787   struct arm_prologue_cache *cache;
1788   CORE_ADDR unwound_fp;
1789
1790   cache = FRAME_OBSTACK_ZALLOC (struct arm_prologue_cache);
1791   cache->saved_regs = trad_frame_alloc_saved_regs (this_frame);
1792
1793   arm_scan_prologue (this_frame, cache);
1794
1795   unwound_fp = get_frame_register_unsigned (this_frame, cache->framereg);
1796   if (unwound_fp == 0)
1797     return cache;
1798
1799   cache->prev_sp = unwound_fp + cache->framesize;
1800
1801   /* Calculate actual addresses of saved registers using offsets
1802      determined by arm_scan_prologue.  */
1803   for (reg = 0; reg < gdbarch_num_regs (get_frame_arch (this_frame)); reg++)
1804     if (trad_frame_addr_p (cache->saved_regs, reg))
1805       cache->saved_regs[reg].addr += cache->prev_sp;
1806
1807   return cache;
1808 }
1809
1810 /* Implementation of the stop_reason hook for arm_prologue frames.  */
1811
1812 static enum unwind_stop_reason
1813 arm_prologue_unwind_stop_reason (struct frame_info *this_frame,
1814                                  void **this_cache)
1815 {
1816   struct arm_prologue_cache *cache;
1817   CORE_ADDR pc;
1818
1819   if (*this_cache == NULL)
1820     *this_cache = arm_make_prologue_cache (this_frame);
1821   cache = (struct arm_prologue_cache *) *this_cache;
1822
1823   /* This is meant to halt the backtrace at "_start".  */
1824   pc = get_frame_pc (this_frame);
1825   if (pc <= gdbarch_tdep (get_frame_arch (this_frame))->lowest_pc)
1826     return UNWIND_OUTERMOST;
1827
1828   /* If we've hit a wall, stop.  */
1829   if (cache->prev_sp == 0)
1830     return UNWIND_OUTERMOST;
1831
1832   return UNWIND_NO_REASON;
1833 }
1834
1835 /* Our frame ID for a normal frame is the current function's starting PC
1836    and the caller's SP when we were called.  */
1837
1838 static void
1839 arm_prologue_this_id (struct frame_info *this_frame,
1840                       void **this_cache,
1841                       struct frame_id *this_id)
1842 {
1843   struct arm_prologue_cache *cache;
1844   struct frame_id id;
1845   CORE_ADDR pc, func;
1846
1847   if (*this_cache == NULL)
1848     *this_cache = arm_make_prologue_cache (this_frame);
1849   cache = (struct arm_prologue_cache *) *this_cache;
1850
1851   /* Use function start address as part of the frame ID.  If we cannot
1852      identify the start address (due to missing symbol information),
1853      fall back to just using the current PC.  */
1854   pc = get_frame_pc (this_frame);
1855   func = get_frame_func (this_frame);
1856   if (!func)
1857     func = pc;
1858
1859   id = frame_id_build (cache->prev_sp, func);
1860   *this_id = id;
1861 }
1862
1863 static struct value *
1864 arm_prologue_prev_register (struct frame_info *this_frame,
1865                             void **this_cache,
1866                             int prev_regnum)
1867 {
1868   struct gdbarch *gdbarch = get_frame_arch (this_frame);
1869   struct arm_prologue_cache *cache;
1870
1871   if (*this_cache == NULL)
1872     *this_cache = arm_make_prologue_cache (this_frame);
1873   cache = (struct arm_prologue_cache *) *this_cache;
1874
1875   /* If we are asked to unwind the PC, then we need to return the LR
1876      instead.  The prologue may save PC, but it will point into this
1877      frame's prologue, not the next frame's resume location.  Also
1878      strip the saved T bit.  A valid LR may have the low bit set, but
1879      a valid PC never does.  */
1880   if (prev_regnum == ARM_PC_REGNUM)
1881     {
1882       CORE_ADDR lr;
1883
1884       lr = frame_unwind_register_unsigned (this_frame, ARM_LR_REGNUM);
1885       return frame_unwind_got_constant (this_frame, prev_regnum,
1886                                         arm_addr_bits_remove (gdbarch, lr));
1887     }
1888
1889   /* SP is generally not saved to the stack, but this frame is
1890      identified by the next frame's stack pointer at the time of the call.
1891      The value was already reconstructed into PREV_SP.  */
1892   if (prev_regnum == ARM_SP_REGNUM)
1893     return frame_unwind_got_constant (this_frame, prev_regnum, cache->prev_sp);
1894
1895   /* The CPSR may have been changed by the call instruction and by the
1896      called function.  The only bit we can reconstruct is the T bit,
1897      by checking the low bit of LR as of the call.  This is a reliable
1898      indicator of Thumb-ness except for some ARM v4T pre-interworking
1899      Thumb code, which could get away with a clear low bit as long as
1900      the called function did not use bx.  Guess that all other
1901      bits are unchanged; the condition flags are presumably lost,
1902      but the processor status is likely valid.  */
1903   if (prev_regnum == ARM_PS_REGNUM)
1904     {
1905       CORE_ADDR lr, cpsr;
1906       ULONGEST t_bit = arm_psr_thumb_bit (gdbarch);
1907
1908       cpsr = get_frame_register_unsigned (this_frame, prev_regnum);
1909       lr = frame_unwind_register_unsigned (this_frame, ARM_LR_REGNUM);
1910       if (IS_THUMB_ADDR (lr))
1911         cpsr |= t_bit;
1912       else
1913         cpsr &= ~t_bit;
1914       return frame_unwind_got_constant (this_frame, prev_regnum, cpsr);
1915     }
1916
1917   return trad_frame_get_prev_register (this_frame, cache->saved_regs,
1918                                        prev_regnum);
1919 }
1920
1921 struct frame_unwind arm_prologue_unwind = {
1922   NORMAL_FRAME,
1923   arm_prologue_unwind_stop_reason,
1924   arm_prologue_this_id,
1925   arm_prologue_prev_register,
1926   NULL,
1927   default_frame_sniffer
1928 };
1929
1930 /* Maintain a list of ARM exception table entries per objfile, similar to the
1931    list of mapping symbols.  We only cache entries for standard ARM-defined
1932    personality routines; the cache will contain only the frame unwinding
1933    instructions associated with the entry (not the descriptors).  */
1934
1935 static const struct objfile_data *arm_exidx_data_key;
1936
1937 struct arm_exidx_entry
1938 {
1939   bfd_vma addr;
1940   gdb_byte *entry;
1941 };
1942 typedef struct arm_exidx_entry arm_exidx_entry_s;
1943 DEF_VEC_O(arm_exidx_entry_s);
1944
1945 struct arm_exidx_data
1946 {
1947   VEC(arm_exidx_entry_s) **section_maps;
1948 };
1949
1950 static void
1951 arm_exidx_data_free (struct objfile *objfile, void *arg)
1952 {
1953   struct arm_exidx_data *data = (struct arm_exidx_data *) arg;
1954   unsigned int i;
1955
1956   for (i = 0; i < objfile->obfd->section_count; i++)
1957     VEC_free (arm_exidx_entry_s, data->section_maps[i]);
1958 }
1959
1960 static inline int
1961 arm_compare_exidx_entries (const struct arm_exidx_entry *lhs,
1962                            const struct arm_exidx_entry *rhs)
1963 {
1964   return lhs->addr < rhs->addr;
1965 }
1966
1967 static struct obj_section *
1968 arm_obj_section_from_vma (struct objfile *objfile, bfd_vma vma)
1969 {
1970   struct obj_section *osect;
1971
1972   ALL_OBJFILE_OSECTIONS (objfile, osect)
1973     if (bfd_get_section_flags (objfile->obfd,
1974                                osect->the_bfd_section) & SEC_ALLOC)
1975       {
1976         bfd_vma start, size;
1977         start = bfd_get_section_vma (objfile->obfd, osect->the_bfd_section);
1978         size = bfd_get_section_size (osect->the_bfd_section);
1979
1980         if (start <= vma && vma < start + size)
1981           return osect;
1982       }
1983
1984   return NULL;
1985 }
1986
1987 /* Parse contents of exception table and exception index sections
1988    of OBJFILE, and fill in the exception table entry cache.
1989
1990    For each entry that refers to a standard ARM-defined personality
1991    routine, extract the frame unwinding instructions (from either
1992    the index or the table section).  The unwinding instructions
1993    are normalized by:
1994     - extracting them from the rest of the table data
1995     - converting to host endianness
1996     - appending the implicit 0xb0 ("Finish") code
1997
1998    The extracted and normalized instructions are stored for later
1999    retrieval by the arm_find_exidx_entry routine.  */
2000  
2001 static void
2002 arm_exidx_new_objfile (struct objfile *objfile)
2003 {
2004   struct cleanup *cleanups;
2005   struct arm_exidx_data *data;
2006   asection *exidx, *extab;
2007   bfd_vma exidx_vma = 0, extab_vma = 0;
2008   bfd_size_type exidx_size = 0, extab_size = 0;
2009   gdb_byte *exidx_data = NULL, *extab_data = NULL;
2010   LONGEST i;
2011
2012   /* If we've already touched this file, do nothing.  */
2013   if (!objfile || objfile_data (objfile, arm_exidx_data_key) != NULL)
2014     return;
2015   cleanups = make_cleanup (null_cleanup, NULL);
2016
2017   /* Read contents of exception table and index.  */
2018   exidx = bfd_get_section_by_name (objfile->obfd, ELF_STRING_ARM_unwind);
2019   if (exidx)
2020     {
2021       exidx_vma = bfd_section_vma (objfile->obfd, exidx);
2022       exidx_size = bfd_get_section_size (exidx);
2023       exidx_data = (gdb_byte *) xmalloc (exidx_size);
2024       make_cleanup (xfree, exidx_data);
2025
2026       if (!bfd_get_section_contents (objfile->obfd, exidx,
2027                                      exidx_data, 0, exidx_size))
2028         {
2029           do_cleanups (cleanups);
2030           return;
2031         }
2032     }
2033
2034   extab = bfd_get_section_by_name (objfile->obfd, ".ARM.extab");
2035   if (extab)
2036     {
2037       extab_vma = bfd_section_vma (objfile->obfd, extab);
2038       extab_size = bfd_get_section_size (extab);
2039       extab_data = (gdb_byte *) xmalloc (extab_size);
2040       make_cleanup (xfree, extab_data);
2041
2042       if (!bfd_get_section_contents (objfile->obfd, extab,
2043                                      extab_data, 0, extab_size))
2044         {
2045           do_cleanups (cleanups);
2046           return;
2047         }
2048     }
2049
2050   /* Allocate exception table data structure.  */
2051   data = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct arm_exidx_data);
2052   set_objfile_data (objfile, arm_exidx_data_key, data);
2053   data->section_maps = OBSTACK_CALLOC (&objfile->objfile_obstack,
2054                                        objfile->obfd->section_count,
2055                                        VEC(arm_exidx_entry_s) *);
2056
2057   /* Fill in exception table.  */
2058   for (i = 0; i < exidx_size / 8; i++)
2059     {
2060       struct arm_exidx_entry new_exidx_entry;
2061       bfd_vma idx = bfd_h_get_32 (objfile->obfd, exidx_data + i * 8);
2062       bfd_vma val = bfd_h_get_32 (objfile->obfd, exidx_data + i * 8 + 4);
2063       bfd_vma addr = 0, word = 0;
2064       int n_bytes = 0, n_words = 0;
2065       struct obj_section *sec;
2066       gdb_byte *entry = NULL;
2067
2068       /* Extract address of start of function.  */
2069       idx = ((idx & 0x7fffffff) ^ 0x40000000) - 0x40000000;
2070       idx += exidx_vma + i * 8;
2071
2072       /* Find section containing function and compute section offset.  */
2073       sec = arm_obj_section_from_vma (objfile, idx);
2074       if (sec == NULL)
2075         continue;
2076       idx -= bfd_get_section_vma (objfile->obfd, sec->the_bfd_section);
2077
2078       /* Determine address of exception table entry.  */
2079       if (val == 1)
2080         {
2081           /* EXIDX_CANTUNWIND -- no exception table entry present.  */
2082         }
2083       else if ((val & 0xff000000) == 0x80000000)
2084         {
2085           /* Exception table entry embedded in .ARM.exidx
2086              -- must be short form.  */
2087           word = val;
2088           n_bytes = 3;
2089         }
2090       else if (!(val & 0x80000000))
2091         {
2092           /* Exception table entry in .ARM.extab.  */
2093           addr = ((val & 0x7fffffff) ^ 0x40000000) - 0x40000000;
2094           addr += exidx_vma + i * 8 + 4;
2095
2096           if (addr >= extab_vma && addr + 4 <= extab_vma + extab_size)
2097             {
2098               word = bfd_h_get_32 (objfile->obfd,
2099                                    extab_data + addr - extab_vma);
2100               addr += 4;
2101
2102               if ((word & 0xff000000) == 0x80000000)
2103                 {
2104                   /* Short form.  */
2105                   n_bytes = 3;
2106                 }
2107               else if ((word & 0xff000000) == 0x81000000
2108                        || (word & 0xff000000) == 0x82000000)
2109                 {
2110                   /* Long form.  */
2111                   n_bytes = 2;
2112                   n_words = ((word >> 16) & 0xff);
2113                 }
2114               else if (!(word & 0x80000000))
2115                 {
2116                   bfd_vma pers;
2117                   struct obj_section *pers_sec;
2118                   int gnu_personality = 0;
2119
2120                   /* Custom personality routine.  */
2121                   pers = ((word & 0x7fffffff) ^ 0x40000000) - 0x40000000;
2122                   pers = UNMAKE_THUMB_ADDR (pers + addr - 4);
2123
2124                   /* Check whether we've got one of the variants of the
2125                      GNU personality routines.  */
2126                   pers_sec = arm_obj_section_from_vma (objfile, pers);
2127                   if (pers_sec)
2128                     {
2129                       static const char *personality[] = 
2130                         {
2131                           "__gcc_personality_v0",
2132                           "__gxx_personality_v0",
2133                           "__gcj_personality_v0",
2134                           "__gnu_objc_personality_v0",
2135                           NULL
2136                         };
2137
2138                       CORE_ADDR pc = pers + obj_section_offset (pers_sec);
2139                       int k;
2140
2141                       for (k = 0; personality[k]; k++)
2142                         if (lookup_minimal_symbol_by_pc_name
2143                               (pc, personality[k], objfile))
2144                           {
2145                             gnu_personality = 1;
2146                             break;
2147                           }
2148                     }
2149
2150                   /* If so, the next word contains a word count in the high
2151                      byte, followed by the same unwind instructions as the
2152                      pre-defined forms.  */
2153                   if (gnu_personality
2154                       && addr + 4 <= extab_vma + extab_size)
2155                     {
2156                       word = bfd_h_get_32 (objfile->obfd,
2157                                            extab_data + addr - extab_vma);
2158                       addr += 4;
2159                       n_bytes = 3;
2160                       n_words = ((word >> 24) & 0xff);
2161                     }
2162                 }
2163             }
2164         }
2165
2166       /* Sanity check address.  */
2167       if (n_words)
2168         if (addr < extab_vma || addr + 4 * n_words > extab_vma + extab_size)
2169           n_words = n_bytes = 0;
2170
2171       /* The unwind instructions reside in WORD (only the N_BYTES least
2172          significant bytes are valid), followed by N_WORDS words in the
2173          extab section starting at ADDR.  */
2174       if (n_bytes || n_words)
2175         {
2176           gdb_byte *p = entry
2177             = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack,
2178                                           n_bytes + n_words * 4 + 1);
2179
2180           while (n_bytes--)
2181             *p++ = (gdb_byte) ((word >> (8 * n_bytes)) & 0xff);
2182
2183           while (n_words--)
2184             {
2185               word = bfd_h_get_32 (objfile->obfd,
2186                                    extab_data + addr - extab_vma);
2187               addr += 4;
2188
2189               *p++ = (gdb_byte) ((word >> 24) & 0xff);
2190               *p++ = (gdb_byte) ((word >> 16) & 0xff);
2191               *p++ = (gdb_byte) ((word >> 8) & 0xff);
2192               *p++ = (gdb_byte) (word & 0xff);
2193             }
2194
2195           /* Implied "Finish" to terminate the list.  */
2196           *p++ = 0xb0;
2197         }
2198
2199       /* Push entry onto vector.  They are guaranteed to always
2200          appear in order of increasing addresses.  */
2201       new_exidx_entry.addr = idx;
2202       new_exidx_entry.entry = entry;
2203       VEC_safe_push (arm_exidx_entry_s,
2204                      data->section_maps[sec->the_bfd_section->index],
2205                      &new_exidx_entry);
2206     }
2207
2208   do_cleanups (cleanups);
2209 }
2210
2211 /* Search for the exception table entry covering MEMADDR.  If one is found,
2212    return a pointer to its data.  Otherwise, return 0.  If START is non-NULL,
2213    set *START to the start of the region covered by this entry.  */
2214
2215 static gdb_byte *
2216 arm_find_exidx_entry (CORE_ADDR memaddr, CORE_ADDR *start)
2217 {
2218   struct obj_section *sec;
2219
2220   sec = find_pc_section (memaddr);
2221   if (sec != NULL)
2222     {
2223       struct arm_exidx_data *data;
2224       VEC(arm_exidx_entry_s) *map;
2225       struct arm_exidx_entry map_key = { memaddr - obj_section_addr (sec), 0 };
2226       unsigned int idx;
2227
2228       data = ((struct arm_exidx_data *)
2229               objfile_data (sec->objfile, arm_exidx_data_key));
2230       if (data != NULL)
2231         {
2232           map = data->section_maps[sec->the_bfd_section->index];
2233           if (!VEC_empty (arm_exidx_entry_s, map))
2234             {
2235               struct arm_exidx_entry *map_sym;
2236
2237               idx = VEC_lower_bound (arm_exidx_entry_s, map, &map_key,
2238                                      arm_compare_exidx_entries);
2239
2240               /* VEC_lower_bound finds the earliest ordered insertion
2241                  point.  If the following symbol starts at this exact
2242                  address, we use that; otherwise, the preceding
2243                  exception table entry covers this address.  */
2244               if (idx < VEC_length (arm_exidx_entry_s, map))
2245                 {
2246                   map_sym = VEC_index (arm_exidx_entry_s, map, idx);
2247                   if (map_sym->addr == map_key.addr)
2248                     {
2249                       if (start)
2250                         *start = map_sym->addr + obj_section_addr (sec);
2251                       return map_sym->entry;
2252                     }
2253                 }
2254
2255               if (idx > 0)
2256                 {
2257                   map_sym = VEC_index (arm_exidx_entry_s, map, idx - 1);
2258                   if (start)
2259                     *start = map_sym->addr + obj_section_addr (sec);
2260                   return map_sym->entry;
2261                 }
2262             }
2263         }
2264     }
2265
2266   return NULL;
2267 }
2268
2269 /* Given the current frame THIS_FRAME, and its associated frame unwinding
2270    instruction list from the ARM exception table entry ENTRY, allocate and
2271    return a prologue cache structure describing how to unwind this frame.
2272
2273    Return NULL if the unwinding instruction list contains a "spare",
2274    "reserved" or "refuse to unwind" instruction as defined in section
2275    "9.3 Frame unwinding instructions" of the "Exception Handling ABI
2276    for the ARM Architecture" document.  */
2277
2278 static struct arm_prologue_cache *
2279 arm_exidx_fill_cache (struct frame_info *this_frame, gdb_byte *entry)
2280 {
2281   CORE_ADDR vsp = 0;
2282   int vsp_valid = 0;
2283
2284   struct arm_prologue_cache *cache;
2285   cache = FRAME_OBSTACK_ZALLOC (struct arm_prologue_cache);
2286   cache->saved_regs = trad_frame_alloc_saved_regs (this_frame);
2287
2288   for (;;)
2289     {
2290       gdb_byte insn;
2291
2292       /* Whenever we reload SP, we actually have to retrieve its
2293          actual value in the current frame.  */
2294       if (!vsp_valid)
2295         {
2296           if (trad_frame_realreg_p (cache->saved_regs, ARM_SP_REGNUM))
2297             {
2298               int reg = cache->saved_regs[ARM_SP_REGNUM].realreg;
2299               vsp = get_frame_register_unsigned (this_frame, reg);
2300             }
2301           else
2302             {
2303               CORE_ADDR addr = cache->saved_regs[ARM_SP_REGNUM].addr;
2304               vsp = get_frame_memory_unsigned (this_frame, addr, 4);
2305             }
2306
2307           vsp_valid = 1;
2308         }
2309
2310       /* Decode next unwind instruction.  */
2311       insn = *entry++;
2312
2313       if ((insn & 0xc0) == 0)
2314         {
2315           int offset = insn & 0x3f;
2316           vsp += (offset << 2) + 4;
2317         }
2318       else if ((insn & 0xc0) == 0x40)
2319         {
2320           int offset = insn & 0x3f;
2321           vsp -= (offset << 2) + 4;
2322         }
2323       else if ((insn & 0xf0) == 0x80)
2324         {
2325           int mask = ((insn & 0xf) << 8) | *entry++;
2326           int i;
2327
2328           /* The special case of an all-zero mask identifies
2329              "Refuse to unwind".  We return NULL to fall back
2330              to the prologue analyzer.  */
2331           if (mask == 0)
2332             return NULL;
2333
2334           /* Pop registers r4..r15 under mask.  */
2335           for (i = 0; i < 12; i++)
2336             if (mask & (1 << i))
2337               {
2338                 cache->saved_regs[4 + i].addr = vsp;
2339                 vsp += 4;
2340               }
2341
2342           /* Special-case popping SP -- we need to reload vsp.  */
2343           if (mask & (1 << (ARM_SP_REGNUM - 4)))
2344             vsp_valid = 0;
2345         }
2346       else if ((insn & 0xf0) == 0x90)
2347         {
2348           int reg = insn & 0xf;
2349
2350           /* Reserved cases.  */
2351           if (reg == ARM_SP_REGNUM || reg == ARM_PC_REGNUM)
2352             return NULL;
2353
2354           /* Set SP from another register and mark VSP for reload.  */
2355           cache->saved_regs[ARM_SP_REGNUM] = cache->saved_regs[reg];
2356           vsp_valid = 0;
2357         }
2358       else if ((insn & 0xf0) == 0xa0)
2359         {
2360           int count = insn & 0x7;
2361           int pop_lr = (insn & 0x8) != 0;
2362           int i;
2363
2364           /* Pop r4..r[4+count].  */
2365           for (i = 0; i <= count; i++)
2366             {
2367               cache->saved_regs[4 + i].addr = vsp;
2368               vsp += 4;
2369             }
2370
2371           /* If indicated by flag, pop LR as well.  */
2372           if (pop_lr)
2373             {
2374               cache->saved_regs[ARM_LR_REGNUM].addr = vsp;
2375               vsp += 4;
2376             }
2377         }
2378       else if (insn == 0xb0)
2379         {
2380           /* We could only have updated PC by popping into it; if so, it
2381              will show up as address.  Otherwise, copy LR into PC.  */
2382           if (!trad_frame_addr_p (cache->saved_regs, ARM_PC_REGNUM))
2383             cache->saved_regs[ARM_PC_REGNUM]
2384               = cache->saved_regs[ARM_LR_REGNUM];
2385
2386           /* We're done.  */
2387           break;
2388         }
2389       else if (insn == 0xb1)
2390         {
2391           int mask = *entry++;
2392           int i;
2393
2394           /* All-zero mask and mask >= 16 is "spare".  */
2395           if (mask == 0 || mask >= 16)
2396             return NULL;
2397
2398           /* Pop r0..r3 under mask.  */
2399           for (i = 0; i < 4; i++)
2400             if (mask & (1 << i))
2401               {
2402                 cache->saved_regs[i].addr = vsp;
2403                 vsp += 4;
2404               }
2405         }
2406       else if (insn == 0xb2)
2407         {
2408           ULONGEST offset = 0;
2409           unsigned shift = 0;
2410
2411           do
2412             {
2413               offset |= (*entry & 0x7f) << shift;
2414               shift += 7;
2415             }
2416           while (*entry++ & 0x80);
2417
2418           vsp += 0x204 + (offset << 2);
2419         }
2420       else if (insn == 0xb3)
2421         {
2422           int start = *entry >> 4;
2423           int count = (*entry++) & 0xf;
2424           int i;
2425
2426           /* Only registers D0..D15 are valid here.  */
2427           if (start + count >= 16)
2428             return NULL;
2429
2430           /* Pop VFP double-precision registers D[start]..D[start+count].  */
2431           for (i = 0; i <= count; i++)
2432             {
2433               cache->saved_regs[ARM_D0_REGNUM + start + i].addr = vsp;
2434               vsp += 8;
2435             }
2436
2437           /* Add an extra 4 bytes for FSTMFDX-style stack.  */
2438           vsp += 4;
2439         }
2440       else if ((insn & 0xf8) == 0xb8)
2441         {
2442           int count = insn & 0x7;
2443           int i;
2444
2445           /* Pop VFP double-precision registers D[8]..D[8+count].  */
2446           for (i = 0; i <= count; i++)
2447             {
2448               cache->saved_regs[ARM_D0_REGNUM + 8 + i].addr = vsp;
2449               vsp += 8;
2450             }
2451
2452           /* Add an extra 4 bytes for FSTMFDX-style stack.  */
2453           vsp += 4;
2454         }
2455       else if (insn == 0xc6)
2456         {
2457           int start = *entry >> 4;
2458           int count = (*entry++) & 0xf;
2459           int i;
2460
2461           /* Only registers WR0..WR15 are valid.  */
2462           if (start + count >= 16)
2463             return NULL;
2464
2465           /* Pop iwmmx registers WR[start]..WR[start+count].  */
2466           for (i = 0; i <= count; i++)
2467             {
2468               cache->saved_regs[ARM_WR0_REGNUM + start + i].addr = vsp;
2469               vsp += 8;
2470             }
2471         }
2472       else if (insn == 0xc7)
2473         {
2474           int mask = *entry++;
2475           int i;
2476
2477           /* All-zero mask and mask >= 16 is "spare".  */
2478           if (mask == 0 || mask >= 16)
2479             return NULL;
2480
2481           /* Pop iwmmx general-purpose registers WCGR0..WCGR3 under mask.  */
2482           for (i = 0; i < 4; i++)
2483             if (mask & (1 << i))
2484               {
2485                 cache->saved_regs[ARM_WCGR0_REGNUM + i].addr = vsp;
2486                 vsp += 4;
2487               }
2488         }
2489       else if ((insn & 0xf8) == 0xc0)
2490         {
2491           int count = insn & 0x7;
2492           int i;
2493
2494           /* Pop iwmmx registers WR[10]..WR[10+count].  */
2495           for (i = 0; i <= count; i++)
2496             {
2497               cache->saved_regs[ARM_WR0_REGNUM + 10 + i].addr = vsp;
2498               vsp += 8;
2499             }
2500         }
2501       else if (insn == 0xc8)
2502         {
2503           int start = *entry >> 4;
2504           int count = (*entry++) & 0xf;
2505           int i;
2506
2507           /* Only registers D0..D31 are valid.  */
2508           if (start + count >= 16)
2509             return NULL;
2510
2511           /* Pop VFP double-precision registers
2512              D[16+start]..D[16+start+count].  */
2513           for (i = 0; i <= count; i++)
2514             {
2515               cache->saved_regs[ARM_D0_REGNUM + 16 + start + i].addr = vsp;
2516               vsp += 8;
2517             }
2518         }
2519       else if (insn == 0xc9)
2520         {
2521           int start = *entry >> 4;
2522           int count = (*entry++) & 0xf;
2523           int i;
2524
2525           /* Pop VFP double-precision registers D[start]..D[start+count].  */
2526           for (i = 0; i <= count; i++)
2527             {
2528               cache->saved_regs[ARM_D0_REGNUM + start + i].addr = vsp;
2529               vsp += 8;
2530             }
2531         }
2532       else if ((insn & 0xf8) == 0xd0)
2533         {
2534           int count = insn & 0x7;
2535           int i;
2536
2537           /* Pop VFP double-precision registers D[8]..D[8+count].  */
2538           for (i = 0; i <= count; i++)
2539             {
2540               cache->saved_regs[ARM_D0_REGNUM + 8 + i].addr = vsp;
2541               vsp += 8;
2542             }
2543         }
2544       else
2545         {
2546           /* Everything else is "spare".  */
2547           return NULL;
2548         }
2549     }
2550
2551   /* If we restore SP from a register, assume this was the frame register.
2552      Otherwise just fall back to SP as frame register.  */
2553   if (trad_frame_realreg_p (cache->saved_regs, ARM_SP_REGNUM))
2554     cache->framereg = cache->saved_regs[ARM_SP_REGNUM].realreg;
2555   else
2556     cache->framereg = ARM_SP_REGNUM;
2557
2558   /* Determine offset to previous frame.  */
2559   cache->framesize
2560     = vsp - get_frame_register_unsigned (this_frame, cache->framereg);
2561
2562   /* We already got the previous SP.  */
2563   cache->prev_sp = vsp;
2564
2565   return cache;
2566 }
2567
2568 /* Unwinding via ARM exception table entries.  Note that the sniffer
2569    already computes a filled-in prologue cache, which is then used
2570    with the same arm_prologue_this_id and arm_prologue_prev_register
2571    routines also used for prologue-parsing based unwinding.  */
2572
2573 static int
2574 arm_exidx_unwind_sniffer (const struct frame_unwind *self,
2575                           struct frame_info *this_frame,
2576                           void **this_prologue_cache)
2577 {
2578   struct gdbarch *gdbarch = get_frame_arch (this_frame);
2579   enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
2580   CORE_ADDR addr_in_block, exidx_region, func_start;
2581   struct arm_prologue_cache *cache;
2582   gdb_byte *entry;
2583
2584   /* See if we have an ARM exception table entry covering this address.  */
2585   addr_in_block = get_frame_address_in_block (this_frame);
2586   entry = arm_find_exidx_entry (addr_in_block, &exidx_region);
2587   if (!entry)
2588     return 0;
2589
2590   /* The ARM exception table does not describe unwind information
2591      for arbitrary PC values, but is guaranteed to be correct only
2592      at call sites.  We have to decide here whether we want to use
2593      ARM exception table information for this frame, or fall back
2594      to using prologue parsing.  (Note that if we have DWARF CFI,
2595      this sniffer isn't even called -- CFI is always preferred.)
2596
2597      Before we make this decision, however, we check whether we
2598      actually have *symbol* information for the current frame.
2599      If not, prologue parsing would not work anyway, so we might
2600      as well use the exception table and hope for the best.  */
2601   if (find_pc_partial_function (addr_in_block, NULL, &func_start, NULL))
2602     {
2603       int exc_valid = 0;
2604
2605       /* If the next frame is "normal", we are at a call site in this
2606          frame, so exception information is guaranteed to be valid.  */
2607       if (get_next_frame (this_frame)
2608           && get_frame_type (get_next_frame (this_frame)) == NORMAL_FRAME)
2609         exc_valid = 1;
2610
2611       /* We also assume exception information is valid if we're currently
2612          blocked in a system call.  The system library is supposed to
2613          ensure this, so that e.g. pthread cancellation works.  */
2614       if (arm_frame_is_thumb (this_frame))
2615         {
2616           LONGEST insn;
2617
2618           if (safe_read_memory_integer (get_frame_pc (this_frame) - 2, 2,
2619                                         byte_order_for_code, &insn)
2620               && (insn & 0xff00) == 0xdf00 /* svc */)
2621             exc_valid = 1;
2622         }
2623       else
2624         {
2625           LONGEST insn;
2626
2627           if (safe_read_memory_integer (get_frame_pc (this_frame) - 4, 4,
2628                                         byte_order_for_code, &insn)
2629               && (insn & 0x0f000000) == 0x0f000000 /* svc */)
2630             exc_valid = 1;
2631         }
2632         
2633       /* Bail out if we don't know that exception information is valid.  */
2634       if (!exc_valid)
2635         return 0;
2636
2637      /* The ARM exception index does not mark the *end* of the region
2638         covered by the entry, and some functions will not have any entry.
2639         To correctly recognize the end of the covered region, the linker
2640         should have inserted dummy records with a CANTUNWIND marker.
2641
2642         Unfortunately, current versions of GNU ld do not reliably do
2643         this, and thus we may have found an incorrect entry above.
2644         As a (temporary) sanity check, we only use the entry if it
2645         lies *within* the bounds of the function.  Note that this check
2646         might reject perfectly valid entries that just happen to cover
2647         multiple functions; therefore this check ought to be removed
2648         once the linker is fixed.  */
2649       if (func_start > exidx_region)
2650         return 0;
2651     }
2652
2653   /* Decode the list of unwinding instructions into a prologue cache.
2654      Note that this may fail due to e.g. a "refuse to unwind" code.  */
2655   cache = arm_exidx_fill_cache (this_frame, entry);
2656   if (!cache)
2657     return 0;
2658
2659   *this_prologue_cache = cache;
2660   return 1;
2661 }
2662
2663 struct frame_unwind arm_exidx_unwind = {
2664   NORMAL_FRAME,
2665   default_frame_unwind_stop_reason,
2666   arm_prologue_this_id,
2667   arm_prologue_prev_register,
2668   NULL,
2669   arm_exidx_unwind_sniffer
2670 };
2671
2672 /* Recognize GCC's trampoline for thumb call-indirect.  If we are in a
2673    trampoline, return the target PC.  Otherwise return 0.
2674
2675    void call0a (char c, short s, int i, long l) {}
2676
2677    int main (void)
2678    {
2679      (*pointer_to_call0a) (c, s, i, l);
2680    }
2681
2682    Instead of calling a stub library function  _call_via_xx (xx is
2683    the register name), GCC may inline the trampoline in the object
2684    file as below (register r2 has the address of call0a).
2685
2686    .global main
2687    .type main, %function
2688    ...
2689    bl .L1
2690    ...
2691    .size main, .-main
2692
2693    .L1:
2694    bx r2
2695
2696    The trampoline 'bx r2' doesn't belong to main.  */
2697
2698 static CORE_ADDR
2699 arm_skip_bx_reg (struct frame_info *frame, CORE_ADDR pc)
2700 {
2701   /* The heuristics of recognizing such trampoline is that FRAME is
2702      executing in Thumb mode and the instruction on PC is 'bx Rm'.  */
2703   if (arm_frame_is_thumb (frame))
2704     {
2705       gdb_byte buf[2];
2706
2707       if (target_read_memory (pc, buf, 2) == 0)
2708         {
2709           struct gdbarch *gdbarch = get_frame_arch (frame);
2710           enum bfd_endian byte_order_for_code
2711             = gdbarch_byte_order_for_code (gdbarch);
2712           uint16_t insn
2713             = extract_unsigned_integer (buf, 2, byte_order_for_code);
2714
2715           if ((insn & 0xff80) == 0x4700)  /* bx <Rm> */
2716             {
2717               CORE_ADDR dest
2718                 = get_frame_register_unsigned (frame, bits (insn, 3, 6));
2719
2720               /* Clear the LSB so that gdb core sets step-resume
2721                  breakpoint at the right address.  */
2722               return UNMAKE_THUMB_ADDR (dest);
2723             }
2724         }
2725     }
2726
2727   return 0;
2728 }
2729
2730 static struct arm_prologue_cache *
2731 arm_make_stub_cache (struct frame_info *this_frame)
2732 {
2733   struct arm_prologue_cache *cache;
2734
2735   cache = FRAME_OBSTACK_ZALLOC (struct arm_prologue_cache);
2736   cache->saved_regs = trad_frame_alloc_saved_regs (this_frame);
2737
2738   cache->prev_sp = get_frame_register_unsigned (this_frame, ARM_SP_REGNUM);
2739
2740   return cache;
2741 }
2742
2743 /* Our frame ID for a stub frame is the current SP and LR.  */
2744
2745 static void
2746 arm_stub_this_id (struct frame_info *this_frame,
2747                   void **this_cache,
2748                   struct frame_id *this_id)
2749 {
2750   struct arm_prologue_cache *cache;
2751
2752   if (*this_cache == NULL)
2753     *this_cache = arm_make_stub_cache (this_frame);
2754   cache = (struct arm_prologue_cache *) *this_cache;
2755
2756   *this_id = frame_id_build (cache->prev_sp, get_frame_pc (this_frame));
2757 }
2758
2759 static int
2760 arm_stub_unwind_sniffer (const struct frame_unwind *self,
2761                          struct frame_info *this_frame,
2762                          void **this_prologue_cache)
2763 {
2764   CORE_ADDR addr_in_block;
2765   gdb_byte dummy[4];
2766   CORE_ADDR pc, start_addr;
2767   const char *name;
2768
2769   addr_in_block = get_frame_address_in_block (this_frame);
2770   pc = get_frame_pc (this_frame);
2771   if (in_plt_section (addr_in_block)
2772       /* We also use the stub winder if the target memory is unreadable
2773          to avoid having the prologue unwinder trying to read it.  */
2774       || target_read_memory (pc, dummy, 4) != 0)
2775     return 1;
2776
2777   if (find_pc_partial_function (pc, &name, &start_addr, NULL) == 0
2778       && arm_skip_bx_reg (this_frame, pc) != 0)
2779     return 1;
2780
2781   return 0;
2782 }
2783
2784 struct frame_unwind arm_stub_unwind = {
2785   NORMAL_FRAME,
2786   default_frame_unwind_stop_reason,
2787   arm_stub_this_id,
2788   arm_prologue_prev_register,
2789   NULL,
2790   arm_stub_unwind_sniffer
2791 };
2792
2793 /* Put here the code to store, into CACHE->saved_regs, the addresses
2794    of the saved registers of frame described by THIS_FRAME.  CACHE is
2795    returned.  */
2796
2797 static struct arm_prologue_cache *
2798 arm_m_exception_cache (struct frame_info *this_frame)
2799 {
2800   struct gdbarch *gdbarch = get_frame_arch (this_frame);
2801   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
2802   struct arm_prologue_cache *cache;
2803   CORE_ADDR unwound_sp;
2804   LONGEST xpsr;
2805
2806   cache = FRAME_OBSTACK_ZALLOC (struct arm_prologue_cache);
2807   cache->saved_regs = trad_frame_alloc_saved_regs (this_frame);
2808
2809   unwound_sp = get_frame_register_unsigned (this_frame,
2810                                             ARM_SP_REGNUM);
2811
2812   /* The hardware saves eight 32-bit words, comprising xPSR,
2813      ReturnAddress, LR (R14), R12, R3, R2, R1, R0.  See details in
2814      "B1.5.6 Exception entry behavior" in
2815      "ARMv7-M Architecture Reference Manual".  */
2816   cache->saved_regs[0].addr = unwound_sp;
2817   cache->saved_regs[1].addr = unwound_sp + 4;
2818   cache->saved_regs[2].addr = unwound_sp + 8;
2819   cache->saved_regs[3].addr = unwound_sp + 12;
2820   cache->saved_regs[12].addr = unwound_sp + 16;
2821   cache->saved_regs[14].addr = unwound_sp + 20;
2822   cache->saved_regs[15].addr = unwound_sp + 24;
2823   cache->saved_regs[ARM_PS_REGNUM].addr = unwound_sp + 28;
2824
2825   /* If bit 9 of the saved xPSR is set, then there is a four-byte
2826      aligner between the top of the 32-byte stack frame and the
2827      previous context's stack pointer.  */
2828   cache->prev_sp = unwound_sp + 32;
2829   if (safe_read_memory_integer (unwound_sp + 28, 4, byte_order, &xpsr)
2830       && (xpsr & (1 << 9)) != 0)
2831     cache->prev_sp += 4;
2832
2833   return cache;
2834 }
2835
2836 /* Implementation of function hook 'this_id' in
2837    'struct frame_uwnind'.  */
2838
2839 static void
2840 arm_m_exception_this_id (struct frame_info *this_frame,
2841                          void **this_cache,
2842                          struct frame_id *this_id)
2843 {
2844   struct arm_prologue_cache *cache;
2845
2846   if (*this_cache == NULL)
2847     *this_cache = arm_m_exception_cache (this_frame);
2848   cache = (struct arm_prologue_cache *) *this_cache;
2849
2850   /* Our frame ID for a stub frame is the current SP and LR.  */
2851   *this_id = frame_id_build (cache->prev_sp,
2852                              get_frame_pc (this_frame));
2853 }
2854
2855 /* Implementation of function hook 'prev_register' in
2856    'struct frame_uwnind'.  */
2857
2858 static struct value *
2859 arm_m_exception_prev_register (struct frame_info *this_frame,
2860                                void **this_cache,
2861                                int prev_regnum)
2862 {
2863   struct arm_prologue_cache *cache;
2864
2865   if (*this_cache == NULL)
2866     *this_cache = arm_m_exception_cache (this_frame);
2867   cache = (struct arm_prologue_cache *) *this_cache;
2868
2869   /* The value was already reconstructed into PREV_SP.  */
2870   if (prev_regnum == ARM_SP_REGNUM)
2871     return frame_unwind_got_constant (this_frame, prev_regnum,
2872                                       cache->prev_sp);
2873
2874   return trad_frame_get_prev_register (this_frame, cache->saved_regs,
2875                                        prev_regnum);
2876 }
2877
2878 /* Implementation of function hook 'sniffer' in
2879    'struct frame_uwnind'.  */
2880
2881 static int
2882 arm_m_exception_unwind_sniffer (const struct frame_unwind *self,
2883                                 struct frame_info *this_frame,
2884                                 void **this_prologue_cache)
2885 {
2886   CORE_ADDR this_pc = get_frame_pc (this_frame);
2887
2888   /* No need to check is_m; this sniffer is only registered for
2889      M-profile architectures.  */
2890
2891   /* Exception frames return to one of these magic PCs.  Other values
2892      are not defined as of v7-M.  See details in "B1.5.8 Exception
2893      return behavior" in "ARMv7-M Architecture Reference Manual".  */
2894   if (this_pc == 0xfffffff1 || this_pc == 0xfffffff9
2895       || this_pc == 0xfffffffd)
2896     return 1;
2897
2898   return 0;
2899 }
2900
2901 /* Frame unwinder for M-profile exceptions.  */
2902
2903 struct frame_unwind arm_m_exception_unwind =
2904 {
2905   SIGTRAMP_FRAME,
2906   default_frame_unwind_stop_reason,
2907   arm_m_exception_this_id,
2908   arm_m_exception_prev_register,
2909   NULL,
2910   arm_m_exception_unwind_sniffer
2911 };
2912
2913 static CORE_ADDR
2914 arm_normal_frame_base (struct frame_info *this_frame, void **this_cache)
2915 {
2916   struct arm_prologue_cache *cache;
2917
2918   if (*this_cache == NULL)
2919     *this_cache = arm_make_prologue_cache (this_frame);
2920   cache = (struct arm_prologue_cache *) *this_cache;
2921
2922   return cache->prev_sp - cache->framesize;
2923 }
2924
2925 struct frame_base arm_normal_base = {
2926   &arm_prologue_unwind,
2927   arm_normal_frame_base,
2928   arm_normal_frame_base,
2929   arm_normal_frame_base
2930 };
2931
2932 /* Assuming THIS_FRAME is a dummy, return the frame ID of that
2933    dummy frame.  The frame ID's base needs to match the TOS value
2934    saved by save_dummy_frame_tos() and returned from
2935    arm_push_dummy_call, and the PC needs to match the dummy frame's
2936    breakpoint.  */
2937
2938 static struct frame_id
2939 arm_dummy_id (struct gdbarch *gdbarch, struct frame_info *this_frame)
2940 {
2941   return frame_id_build (get_frame_register_unsigned (this_frame,
2942                                                       ARM_SP_REGNUM),
2943                          get_frame_pc (this_frame));
2944 }
2945
2946 /* Given THIS_FRAME, find the previous frame's resume PC (which will
2947    be used to construct the previous frame's ID, after looking up the
2948    containing function).  */
2949
2950 static CORE_ADDR
2951 arm_unwind_pc (struct gdbarch *gdbarch, struct frame_info *this_frame)
2952 {
2953   CORE_ADDR pc;
2954   pc = frame_unwind_register_unsigned (this_frame, ARM_PC_REGNUM);
2955   return arm_addr_bits_remove (gdbarch, pc);
2956 }
2957
2958 static CORE_ADDR
2959 arm_unwind_sp (struct gdbarch *gdbarch, struct frame_info *this_frame)
2960 {
2961   return frame_unwind_register_unsigned (this_frame, ARM_SP_REGNUM);
2962 }
2963
2964 static struct value *
2965 arm_dwarf2_prev_register (struct frame_info *this_frame, void **this_cache,
2966                           int regnum)
2967 {
2968   struct gdbarch * gdbarch = get_frame_arch (this_frame);
2969   CORE_ADDR lr, cpsr;
2970   ULONGEST t_bit = arm_psr_thumb_bit (gdbarch);
2971
2972   switch (regnum)
2973     {
2974     case ARM_PC_REGNUM:
2975       /* The PC is normally copied from the return column, which
2976          describes saves of LR.  However, that version may have an
2977          extra bit set to indicate Thumb state.  The bit is not
2978          part of the PC.  */
2979       lr = frame_unwind_register_unsigned (this_frame, ARM_LR_REGNUM);
2980       return frame_unwind_got_constant (this_frame, regnum,
2981                                         arm_addr_bits_remove (gdbarch, lr));
2982
2983     case ARM_PS_REGNUM:
2984       /* Reconstruct the T bit; see arm_prologue_prev_register for details.  */
2985       cpsr = get_frame_register_unsigned (this_frame, regnum);
2986       lr = frame_unwind_register_unsigned (this_frame, ARM_LR_REGNUM);
2987       if (IS_THUMB_ADDR (lr))
2988         cpsr |= t_bit;
2989       else
2990         cpsr &= ~t_bit;
2991       return frame_unwind_got_constant (this_frame, regnum, cpsr);
2992
2993     default:
2994       internal_error (__FILE__, __LINE__,
2995                       _("Unexpected register %d"), regnum);
2996     }
2997 }
2998
2999 static void
3000 arm_dwarf2_frame_init_reg (struct gdbarch *gdbarch, int regnum,
3001                            struct dwarf2_frame_state_reg *reg,
3002                            struct frame_info *this_frame)
3003 {
3004   switch (regnum)
3005     {
3006     case ARM_PC_REGNUM:
3007     case ARM_PS_REGNUM:
3008       reg->how = DWARF2_FRAME_REG_FN;
3009       reg->loc.fn = arm_dwarf2_prev_register;
3010       break;
3011     case ARM_SP_REGNUM:
3012       reg->how = DWARF2_FRAME_REG_CFA;
3013       break;
3014     }
3015 }
3016
3017 /* Implement the stack_frame_destroyed_p gdbarch method.  */
3018
3019 static int
3020 thumb_stack_frame_destroyed_p (struct gdbarch *gdbarch, CORE_ADDR pc)
3021 {
3022   enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
3023   unsigned int insn, insn2;
3024   int found_return = 0, found_stack_adjust = 0;
3025   CORE_ADDR func_start, func_end;
3026   CORE_ADDR scan_pc;
3027   gdb_byte buf[4];
3028
3029   if (!find_pc_partial_function (pc, NULL, &func_start, &func_end))
3030     return 0;
3031
3032   /* The epilogue is a sequence of instructions along the following lines:
3033
3034     - add stack frame size to SP or FP
3035     - [if frame pointer used] restore SP from FP
3036     - restore registers from SP [may include PC]
3037     - a return-type instruction [if PC wasn't already restored]
3038
3039     In a first pass, we scan forward from the current PC and verify the
3040     instructions we find as compatible with this sequence, ending in a
3041     return instruction.
3042
3043     However, this is not sufficient to distinguish indirect function calls
3044     within a function from indirect tail calls in the epilogue in some cases.
3045     Therefore, if we didn't already find any SP-changing instruction during
3046     forward scan, we add a backward scanning heuristic to ensure we actually
3047     are in the epilogue.  */
3048
3049   scan_pc = pc;
3050   while (scan_pc < func_end && !found_return)
3051     {
3052       if (target_read_memory (scan_pc, buf, 2))
3053         break;
3054
3055       scan_pc += 2;
3056       insn = extract_unsigned_integer (buf, 2, byte_order_for_code);
3057
3058       if ((insn & 0xff80) == 0x4700)  /* bx <Rm> */
3059         found_return = 1;
3060       else if (insn == 0x46f7)  /* mov pc, lr */
3061         found_return = 1;
3062       else if (thumb_instruction_restores_sp (insn))
3063         {
3064           if ((insn & 0xff00) == 0xbd00)  /* pop <registers, PC> */
3065             found_return = 1;
3066         }
3067       else if (thumb_insn_size (insn) == 4)  /* 32-bit Thumb-2 instruction */
3068         {
3069           if (target_read_memory (scan_pc, buf, 2))
3070             break;
3071
3072           scan_pc += 2;
3073           insn2 = extract_unsigned_integer (buf, 2, byte_order_for_code);
3074
3075           if (insn == 0xe8bd)  /* ldm.w sp!, <registers> */
3076             {
3077               if (insn2 & 0x8000)  /* <registers> include PC.  */
3078                 found_return = 1;
3079             }
3080           else if (insn == 0xf85d  /* ldr.w <Rt>, [sp], #4 */
3081                    && (insn2 & 0x0fff) == 0x0b04)
3082             {
3083               if ((insn2 & 0xf000) == 0xf000) /* <Rt> is PC.  */
3084                 found_return = 1;
3085             }
3086           else if ((insn & 0xffbf) == 0xecbd  /* vldm sp!, <list> */
3087                    && (insn2 & 0x0e00) == 0x0a00)
3088             ;
3089           else
3090             break;
3091         }
3092       else
3093         break;
3094     }
3095
3096   if (!found_return)
3097     return 0;
3098
3099   /* Since any instruction in the epilogue sequence, with the possible
3100      exception of return itself, updates the stack pointer, we need to
3101      scan backwards for at most one instruction.  Try either a 16-bit or
3102      a 32-bit instruction.  This is just a heuristic, so we do not worry
3103      too much about false positives.  */
3104
3105   if (pc - 4 < func_start)
3106     return 0;
3107   if (target_read_memory (pc - 4, buf, 4))
3108     return 0;
3109
3110   insn = extract_unsigned_integer (buf, 2, byte_order_for_code);
3111   insn2 = extract_unsigned_integer (buf + 2, 2, byte_order_for_code);
3112
3113   if (thumb_instruction_restores_sp (insn2))
3114     found_stack_adjust = 1;
3115   else if (insn == 0xe8bd)  /* ldm.w sp!, <registers> */
3116     found_stack_adjust = 1;
3117   else if (insn == 0xf85d  /* ldr.w <Rt>, [sp], #4 */
3118            && (insn2 & 0x0fff) == 0x0b04)
3119     found_stack_adjust = 1;
3120   else if ((insn & 0xffbf) == 0xecbd  /* vldm sp!, <list> */
3121            && (insn2 & 0x0e00) == 0x0a00)
3122     found_stack_adjust = 1;
3123
3124   return found_stack_adjust;
3125 }
3126
3127 static int
3128 arm_stack_frame_destroyed_p_1 (struct gdbarch *gdbarch, CORE_ADDR pc)
3129 {
3130   enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
3131   unsigned int insn;
3132   int found_return;
3133   CORE_ADDR func_start, func_end;
3134
3135   if (!find_pc_partial_function (pc, NULL, &func_start, &func_end))
3136     return 0;
3137
3138   /* We are in the epilogue if the previous instruction was a stack
3139      adjustment and the next instruction is a possible return (bx, mov
3140      pc, or pop).  We could have to scan backwards to find the stack
3141      adjustment, or forwards to find the return, but this is a decent
3142      approximation.  First scan forwards.  */
3143
3144   found_return = 0;
3145   insn = read_memory_unsigned_integer (pc, 4, byte_order_for_code);
3146   if (bits (insn, 28, 31) != INST_NV)
3147     {
3148       if ((insn & 0x0ffffff0) == 0x012fff10)
3149         /* BX.  */
3150         found_return = 1;
3151       else if ((insn & 0x0ffffff0) == 0x01a0f000)
3152         /* MOV PC.  */
3153         found_return = 1;
3154       else if ((insn & 0x0fff0000) == 0x08bd0000
3155           && (insn & 0x0000c000) != 0)
3156         /* POP (LDMIA), including PC or LR.  */
3157         found_return = 1;
3158     }
3159
3160   if (!found_return)
3161     return 0;
3162
3163   /* Scan backwards.  This is just a heuristic, so do not worry about
3164      false positives from mode changes.  */
3165
3166   if (pc < func_start + 4)
3167     return 0;
3168
3169   insn = read_memory_unsigned_integer (pc - 4, 4, byte_order_for_code);
3170   if (arm_instruction_restores_sp (insn))
3171     return 1;
3172
3173   return 0;
3174 }
3175
3176 /* Implement the stack_frame_destroyed_p gdbarch method.  */
3177
3178 static int
3179 arm_stack_frame_destroyed_p (struct gdbarch *gdbarch, CORE_ADDR pc)
3180 {
3181   if (arm_pc_is_thumb (gdbarch, pc))
3182     return thumb_stack_frame_destroyed_p (gdbarch, pc);
3183   else
3184     return arm_stack_frame_destroyed_p_1 (gdbarch, pc);
3185 }
3186
3187 /* When arguments must be pushed onto the stack, they go on in reverse
3188    order.  The code below implements a FILO (stack) to do this.  */
3189
3190 struct stack_item
3191 {
3192   int len;
3193   struct stack_item *prev;
3194   gdb_byte *data;
3195 };
3196
3197 static struct stack_item *
3198 push_stack_item (struct stack_item *prev, const gdb_byte *contents, int len)
3199 {
3200   struct stack_item *si;
3201   si = XNEW (struct stack_item);
3202   si->data = (gdb_byte *) xmalloc (len);
3203   si->len = len;
3204   si->prev = prev;
3205   memcpy (si->data, contents, len);
3206   return si;
3207 }
3208
3209 static struct stack_item *
3210 pop_stack_item (struct stack_item *si)
3211 {
3212   struct stack_item *dead = si;
3213   si = si->prev;
3214   xfree (dead->data);
3215   xfree (dead);
3216   return si;
3217 }
3218
3219
3220 /* Return the alignment (in bytes) of the given type.  */
3221
3222 static int
3223 arm_type_align (struct type *t)
3224 {
3225   int n;
3226   int align;
3227   int falign;
3228
3229   t = check_typedef (t);
3230   switch (TYPE_CODE (t))
3231     {
3232     default:
3233       /* Should never happen.  */
3234       internal_error (__FILE__, __LINE__, _("unknown type alignment"));
3235       return 4;
3236
3237     case TYPE_CODE_PTR:
3238     case TYPE_CODE_ENUM:
3239     case TYPE_CODE_INT:
3240     case TYPE_CODE_FLT:
3241     case TYPE_CODE_SET:
3242     case TYPE_CODE_RANGE:
3243     case TYPE_CODE_REF:
3244     case TYPE_CODE_CHAR:
3245     case TYPE_CODE_BOOL:
3246       return TYPE_LENGTH (t);
3247
3248     case TYPE_CODE_ARRAY:
3249       if (TYPE_VECTOR (t))
3250         {
3251           /* Use the natural alignment for vector types (the same for
3252              scalar type), but the maximum alignment is 64-bit.  */
3253           if (TYPE_LENGTH (t) > 8)
3254             return 8;
3255           else
3256             return TYPE_LENGTH (t);
3257         }
3258       else
3259         return arm_type_align (TYPE_TARGET_TYPE (t));
3260     case TYPE_CODE_COMPLEX:
3261       return arm_type_align (TYPE_TARGET_TYPE (t));
3262
3263     case TYPE_CODE_STRUCT:
3264     case TYPE_CODE_UNION:
3265       align = 1;
3266       for (n = 0; n < TYPE_NFIELDS (t); n++)
3267         {
3268           falign = arm_type_align (TYPE_FIELD_TYPE (t, n));
3269           if (falign > align)
3270             align = falign;
3271         }
3272       return align;
3273     }
3274 }
3275
3276 /* Possible base types for a candidate for passing and returning in
3277    VFP registers.  */
3278
3279 enum arm_vfp_cprc_base_type
3280 {
3281   VFP_CPRC_UNKNOWN,
3282   VFP_CPRC_SINGLE,
3283   VFP_CPRC_DOUBLE,
3284   VFP_CPRC_VEC64,
3285   VFP_CPRC_VEC128
3286 };
3287
3288 /* The length of one element of base type B.  */
3289
3290 static unsigned
3291 arm_vfp_cprc_unit_length (enum arm_vfp_cprc_base_type b)
3292 {
3293   switch (b)
3294     {
3295     case VFP_CPRC_SINGLE:
3296       return 4;
3297     case VFP_CPRC_DOUBLE:
3298       return 8;
3299     case VFP_CPRC_VEC64:
3300       return 8;
3301     case VFP_CPRC_VEC128:
3302       return 16;
3303     default:
3304       internal_error (__FILE__, __LINE__, _("Invalid VFP CPRC type: %d."),
3305                       (int) b);
3306     }
3307 }
3308
3309 /* The character ('s', 'd' or 'q') for the type of VFP register used
3310    for passing base type B.  */
3311
3312 static int
3313 arm_vfp_cprc_reg_char (enum arm_vfp_cprc_base_type b)
3314 {
3315   switch (b)
3316     {
3317     case VFP_CPRC_SINGLE:
3318       return 's';
3319     case VFP_CPRC_DOUBLE:
3320       return 'd';
3321     case VFP_CPRC_VEC64:
3322       return 'd';
3323     case VFP_CPRC_VEC128:
3324       return 'q';
3325     default:
3326       internal_error (__FILE__, __LINE__, _("Invalid VFP CPRC type: %d."),
3327                       (int) b);
3328     }
3329 }
3330
3331 /* Determine whether T may be part of a candidate for passing and
3332    returning in VFP registers, ignoring the limit on the total number
3333    of components.  If *BASE_TYPE is VFP_CPRC_UNKNOWN, set it to the
3334    classification of the first valid component found; if it is not
3335    VFP_CPRC_UNKNOWN, all components must have the same classification
3336    as *BASE_TYPE.  If it is found that T contains a type not permitted
3337    for passing and returning in VFP registers, a type differently
3338    classified from *BASE_TYPE, or two types differently classified
3339    from each other, return -1, otherwise return the total number of
3340    base-type elements found (possibly 0 in an empty structure or
3341    array).  Vector types are not currently supported, matching the
3342    generic AAPCS support.  */
3343
3344 static int
3345 arm_vfp_cprc_sub_candidate (struct type *t,
3346                             enum arm_vfp_cprc_base_type *base_type)
3347 {
3348   t = check_typedef (t);
3349   switch (TYPE_CODE (t))
3350     {
3351     case TYPE_CODE_FLT:
3352       switch (TYPE_LENGTH (t))
3353         {
3354         case 4:
3355           if (*base_type == VFP_CPRC_UNKNOWN)
3356             *base_type = VFP_CPRC_SINGLE;
3357           else if (*base_type != VFP_CPRC_SINGLE)
3358             return -1;
3359           return 1;
3360
3361         case 8:
3362           if (*base_type == VFP_CPRC_UNKNOWN)
3363             *base_type = VFP_CPRC_DOUBLE;
3364           else if (*base_type != VFP_CPRC_DOUBLE)
3365             return -1;
3366           return 1;
3367
3368         default:
3369           return -1;
3370         }
3371       break;
3372
3373     case TYPE_CODE_COMPLEX:
3374       /* Arguments of complex T where T is one of the types float or
3375          double get treated as if they are implemented as:
3376
3377          struct complexT
3378          {
3379            T real;
3380            T imag;
3381          };
3382
3383       */
3384       switch (TYPE_LENGTH (t))
3385         {
3386         case 8:
3387           if (*base_type == VFP_CPRC_UNKNOWN)
3388             *base_type = VFP_CPRC_SINGLE;
3389           else if (*base_type != VFP_CPRC_SINGLE)
3390             return -1;
3391           return 2;
3392
3393         case 16:
3394           if (*base_type == VFP_CPRC_UNKNOWN)
3395             *base_type = VFP_CPRC_DOUBLE;
3396           else if (*base_type != VFP_CPRC_DOUBLE)
3397             return -1;
3398           return 2;
3399
3400         default:
3401           return -1;
3402         }
3403       break;
3404
3405     case TYPE_CODE_ARRAY:
3406       {
3407         if (TYPE_VECTOR (t))
3408           {
3409             /* A 64-bit or 128-bit containerized vector type are VFP
3410                CPRCs.  */
3411             switch (TYPE_LENGTH (t))
3412               {
3413               case 8:
3414                 if (*base_type == VFP_CPRC_UNKNOWN)
3415                   *base_type = VFP_CPRC_VEC64;
3416                 return 1;
3417               case 16:
3418                 if (*base_type == VFP_CPRC_UNKNOWN)
3419                   *base_type = VFP_CPRC_VEC128;
3420                 return 1;
3421               default:
3422                 return -1;
3423               }
3424           }
3425         else
3426           {
3427             int count;
3428             unsigned unitlen;
3429
3430             count = arm_vfp_cprc_sub_candidate (TYPE_TARGET_TYPE (t),
3431                                                 base_type);
3432             if (count == -1)
3433               return -1;
3434             if (TYPE_LENGTH (t) == 0)
3435               {
3436                 gdb_assert (count == 0);
3437                 return 0;
3438               }
3439             else if (count == 0)
3440               return -1;
3441             unitlen = arm_vfp_cprc_unit_length (*base_type);
3442             gdb_assert ((TYPE_LENGTH (t) % unitlen) == 0);
3443             return TYPE_LENGTH (t) / unitlen;
3444           }
3445       }
3446       break;
3447
3448     case TYPE_CODE_STRUCT:
3449       {
3450         int count = 0;
3451         unsigned unitlen;
3452         int i;
3453         for (i = 0; i < TYPE_NFIELDS (t); i++)
3454           {
3455             int sub_count = arm_vfp_cprc_sub_candidate (TYPE_FIELD_TYPE (t, i),
3456                                                         base_type);
3457             if (sub_count == -1)
3458               return -1;
3459             count += sub_count;
3460           }
3461         if (TYPE_LENGTH (t) == 0)
3462           {
3463             gdb_assert (count == 0);
3464             return 0;
3465           }
3466         else if (count == 0)
3467           return -1;
3468         unitlen = arm_vfp_cprc_unit_length (*base_type);
3469         if (TYPE_LENGTH (t) != unitlen * count)
3470           return -1;
3471         return count;
3472       }
3473
3474     case TYPE_CODE_UNION:
3475       {
3476         int count = 0;
3477         unsigned unitlen;
3478         int i;
3479         for (i = 0; i < TYPE_NFIELDS (t); i++)
3480           {
3481             int sub_count = arm_vfp_cprc_sub_candidate (TYPE_FIELD_TYPE (t, i),
3482                                                         base_type);
3483             if (sub_count == -1)
3484               return -1;
3485             count = (count > sub_count ? count : sub_count);
3486           }
3487         if (TYPE_LENGTH (t) == 0)
3488           {
3489             gdb_assert (count == 0);
3490             return 0;
3491           }
3492         else if (count == 0)
3493           return -1;
3494         unitlen = arm_vfp_cprc_unit_length (*base_type);
3495         if (TYPE_LENGTH (t) != unitlen * count)
3496           return -1;
3497         return count;
3498       }
3499
3500     default:
3501       break;
3502     }
3503
3504   return -1;
3505 }
3506
3507 /* Determine whether T is a VFP co-processor register candidate (CPRC)
3508    if passed to or returned from a non-variadic function with the VFP
3509    ABI in effect.  Return 1 if it is, 0 otherwise.  If it is, set
3510    *BASE_TYPE to the base type for T and *COUNT to the number of
3511    elements of that base type before returning.  */
3512
3513 static int
3514 arm_vfp_call_candidate (struct type *t, enum arm_vfp_cprc_base_type *base_type,
3515                         int *count)
3516 {
3517   enum arm_vfp_cprc_base_type b = VFP_CPRC_UNKNOWN;
3518   int c = arm_vfp_cprc_sub_candidate (t, &b);
3519   if (c <= 0 || c > 4)
3520     return 0;
3521   *base_type = b;
3522   *count = c;
3523   return 1;
3524 }
3525
3526 /* Return 1 if the VFP ABI should be used for passing arguments to and
3527    returning values from a function of type FUNC_TYPE, 0
3528    otherwise.  */
3529
3530 static int
3531 arm_vfp_abi_for_function (struct gdbarch *gdbarch, struct type *func_type)
3532 {
3533   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
3534   /* Variadic functions always use the base ABI.  Assume that functions
3535      without debug info are not variadic.  */
3536   if (func_type && TYPE_VARARGS (check_typedef (func_type)))
3537     return 0;
3538   /* The VFP ABI is only supported as a variant of AAPCS.  */
3539   if (tdep->arm_abi != ARM_ABI_AAPCS)
3540     return 0;
3541   return gdbarch_tdep (gdbarch)->fp_model == ARM_FLOAT_VFP;
3542 }
3543
3544 /* We currently only support passing parameters in integer registers, which
3545    conforms with GCC's default model, and VFP argument passing following
3546    the VFP variant of AAPCS.  Several other variants exist and
3547    we should probably support some of them based on the selected ABI.  */
3548
3549 static CORE_ADDR
3550 arm_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
3551                      struct regcache *regcache, CORE_ADDR bp_addr, int nargs,
3552                      struct value **args, CORE_ADDR sp, int struct_return,
3553                      CORE_ADDR struct_addr)
3554 {
3555   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
3556   int argnum;
3557   int argreg;
3558   int nstack;
3559   struct stack_item *si = NULL;
3560   int use_vfp_abi;
3561   struct type *ftype;
3562   unsigned vfp_regs_free = (1 << 16) - 1;
3563
3564   /* Determine the type of this function and whether the VFP ABI
3565      applies.  */
3566   ftype = check_typedef (value_type (function));
3567   if (TYPE_CODE (ftype) == TYPE_CODE_PTR)
3568     ftype = check_typedef (TYPE_TARGET_TYPE (ftype));
3569   use_vfp_abi = arm_vfp_abi_for_function (gdbarch, ftype);
3570
3571   /* Set the return address.  For the ARM, the return breakpoint is
3572      always at BP_ADDR.  */
3573   if (arm_pc_is_thumb (gdbarch, bp_addr))
3574     bp_addr |= 1;
3575   regcache_cooked_write_unsigned (regcache, ARM_LR_REGNUM, bp_addr);
3576
3577   /* Walk through the list of args and determine how large a temporary
3578      stack is required.  Need to take care here as structs may be
3579      passed on the stack, and we have to push them.  */
3580   nstack = 0;
3581
3582   argreg = ARM_A1_REGNUM;
3583   nstack = 0;
3584
3585   /* The struct_return pointer occupies the first parameter
3586      passing register.  */
3587   if (struct_return)
3588     {
3589       if (arm_debug)
3590         fprintf_unfiltered (gdb_stdlog, "struct return in %s = %s\n",
3591                             gdbarch_register_name (gdbarch, argreg),
3592                             paddress (gdbarch, struct_addr));
3593       regcache_cooked_write_unsigned (regcache, argreg, struct_addr);
3594       argreg++;
3595     }
3596
3597   for (argnum = 0; argnum < nargs; argnum++)
3598     {
3599       int len;
3600       struct type *arg_type;
3601       struct type *target_type;
3602       enum type_code typecode;
3603       const bfd_byte *val;
3604       int align;
3605       enum arm_vfp_cprc_base_type vfp_base_type;
3606       int vfp_base_count;
3607       int may_use_core_reg = 1;
3608
3609       arg_type = check_typedef (value_type (args[argnum]));
3610       len = TYPE_LENGTH (arg_type);
3611       target_type = TYPE_TARGET_TYPE (arg_type);
3612       typecode = TYPE_CODE (arg_type);
3613       val = value_contents (args[argnum]);
3614
3615       align = arm_type_align (arg_type);
3616       /* Round alignment up to a whole number of words.  */
3617       align = (align + INT_REGISTER_SIZE - 1) & ~(INT_REGISTER_SIZE - 1);
3618       /* Different ABIs have different maximum alignments.  */
3619       if (gdbarch_tdep (gdbarch)->arm_abi == ARM_ABI_APCS)
3620         {
3621           /* The APCS ABI only requires word alignment.  */
3622           align = INT_REGISTER_SIZE;
3623         }
3624       else
3625         {
3626           /* The AAPCS requires at most doubleword alignment.  */
3627           if (align > INT_REGISTER_SIZE * 2)
3628             align = INT_REGISTER_SIZE * 2;
3629         }
3630
3631       if (use_vfp_abi
3632           && arm_vfp_call_candidate (arg_type, &vfp_base_type,
3633                                      &vfp_base_count))
3634         {
3635           int regno;
3636           int unit_length;
3637           int shift;
3638           unsigned mask;
3639
3640           /* Because this is a CPRC it cannot go in a core register or
3641              cause a core register to be skipped for alignment.
3642              Either it goes in VFP registers and the rest of this loop
3643              iteration is skipped for this argument, or it goes on the
3644              stack (and the stack alignment code is correct for this
3645              case).  */
3646           may_use_core_reg = 0;
3647
3648           unit_length = arm_vfp_cprc_unit_length (vfp_base_type);
3649           shift = unit_length / 4;
3650           mask = (1 << (shift * vfp_base_count)) - 1;
3651           for (regno = 0; regno < 16; regno += shift)
3652             if (((vfp_regs_free >> regno) & mask) == mask)
3653               break;
3654
3655           if (regno < 16)
3656             {
3657               int reg_char;
3658               int reg_scaled;
3659               int i;
3660
3661               vfp_regs_free &= ~(mask << regno);
3662               reg_scaled = regno / shift;
3663               reg_char = arm_vfp_cprc_reg_char (vfp_base_type);
3664               for (i = 0; i < vfp_base_count; i++)
3665                 {
3666                   char name_buf[4];
3667                   int regnum;
3668                   if (reg_char == 'q')
3669                     arm_neon_quad_write (gdbarch, regcache, reg_scaled + i,
3670                                          val + i * unit_length);
3671                   else
3672                     {
3673                       xsnprintf (name_buf, sizeof (name_buf), "%c%d",
3674                                  reg_char, reg_scaled + i);
3675                       regnum = user_reg_map_name_to_regnum (gdbarch, name_buf,
3676                                                             strlen (name_buf));
3677                       regcache_cooked_write (regcache, regnum,
3678                                              val + i * unit_length);
3679                     }
3680                 }
3681               continue;
3682             }
3683           else
3684             {
3685               /* This CPRC could not go in VFP registers, so all VFP
3686                  registers are now marked as used.  */
3687               vfp_regs_free = 0;
3688             }
3689         }
3690
3691       /* Push stack padding for dowubleword alignment.  */
3692       if (nstack & (align - 1))
3693         {
3694           si = push_stack_item (si, val, INT_REGISTER_SIZE);
3695           nstack += INT_REGISTER_SIZE;
3696         }
3697       
3698       /* Doubleword aligned quantities must go in even register pairs.  */
3699       if (may_use_core_reg
3700           && argreg <= ARM_LAST_ARG_REGNUM
3701           && align > INT_REGISTER_SIZE
3702           && argreg & 1)
3703         argreg++;
3704
3705       /* If the argument is a pointer to a function, and it is a
3706          Thumb function, create a LOCAL copy of the value and set
3707          the THUMB bit in it.  */
3708       if (TYPE_CODE_PTR == typecode
3709           && target_type != NULL
3710           && TYPE_CODE_FUNC == TYPE_CODE (check_typedef (target_type)))
3711         {
3712           CORE_ADDR regval = extract_unsigned_integer (val, len, byte_order);
3713           if (arm_pc_is_thumb (gdbarch, regval))
3714             {
3715               bfd_byte *copy = (bfd_byte *) alloca (len);
3716               store_unsigned_integer (copy, len, byte_order,
3717                                       MAKE_THUMB_ADDR (regval));
3718               val = copy;
3719             }
3720         }
3721
3722       /* Copy the argument to general registers or the stack in
3723          register-sized pieces.  Large arguments are split between
3724          registers and stack.  */
3725       while (len > 0)
3726         {
3727           int partial_len = len < INT_REGISTER_SIZE ? len : INT_REGISTER_SIZE;
3728           CORE_ADDR regval
3729             = extract_unsigned_integer (val, partial_len, byte_order);
3730
3731           if (may_use_core_reg && argreg <= ARM_LAST_ARG_REGNUM)
3732             {
3733               /* The argument is being passed in a general purpose
3734                  register.  */
3735               if (byte_order == BFD_ENDIAN_BIG)
3736                 regval <<= (INT_REGISTER_SIZE - partial_len) * 8;
3737               if (arm_debug)
3738                 fprintf_unfiltered (gdb_stdlog, "arg %d in %s = 0x%s\n",
3739                                     argnum,
3740                                     gdbarch_register_name
3741                                       (gdbarch, argreg),
3742                                     phex (regval, INT_REGISTER_SIZE));
3743               regcache_cooked_write_unsigned (regcache, argreg, regval);
3744               argreg++;
3745             }
3746           else
3747             {
3748               gdb_byte buf[INT_REGISTER_SIZE];
3749
3750               memset (buf, 0, sizeof (buf));
3751               store_unsigned_integer (buf, partial_len, byte_order, regval);
3752
3753               /* Push the arguments onto the stack.  */
3754               if (arm_debug)
3755                 fprintf_unfiltered (gdb_stdlog, "arg %d @ sp + %d\n",
3756                                     argnum, nstack);
3757               si = push_stack_item (si, buf, INT_REGISTER_SIZE);
3758               nstack += INT_REGISTER_SIZE;
3759             }
3760               
3761           len -= partial_len;
3762           val += partial_len;
3763         }
3764     }
3765   /* If we have an odd number of words to push, then decrement the stack
3766      by one word now, so first stack argument will be dword aligned.  */
3767   if (nstack & 4)
3768     sp -= 4;
3769
3770   while (si)
3771     {
3772       sp -= si->len;
3773       write_memory (sp, si->data, si->len);
3774       si = pop_stack_item (si);
3775     }
3776
3777   /* Finally, update teh SP register.  */
3778   regcache_cooked_write_unsigned (regcache, ARM_SP_REGNUM, sp);
3779
3780   return sp;
3781 }
3782
3783
3784 /* Always align the frame to an 8-byte boundary.  This is required on
3785    some platforms and harmless on the rest.  */
3786
3787 static CORE_ADDR
3788 arm_frame_align (struct gdbarch *gdbarch, CORE_ADDR sp)
3789 {
3790   /* Align the stack to eight bytes.  */
3791   return sp & ~ (CORE_ADDR) 7;
3792 }
3793
3794 static void
3795 print_fpu_flags (struct ui_file *file, int flags)
3796 {
3797   if (flags & (1 << 0))
3798     fputs_filtered ("IVO ", file);
3799   if (flags & (1 << 1))
3800     fputs_filtered ("DVZ ", file);
3801   if (flags & (1 << 2))
3802     fputs_filtered ("OFL ", file);
3803   if (flags & (1 << 3))
3804     fputs_filtered ("UFL ", file);
3805   if (flags & (1 << 4))
3806     fputs_filtered ("INX ", file);
3807   fputc_filtered ('\n', file);
3808 }
3809
3810 /* Print interesting information about the floating point processor
3811    (if present) or emulator.  */
3812 static void
3813 arm_print_float_info (struct gdbarch *gdbarch, struct ui_file *file,
3814                       struct frame_info *frame, const char *args)
3815 {
3816   unsigned long status = get_frame_register_unsigned (frame, ARM_FPS_REGNUM);
3817   int type;
3818
3819   type = (status >> 24) & 127;
3820   if (status & (1 << 31))
3821     fprintf_filtered (file, _("Hardware FPU type %d\n"), type);
3822   else
3823     fprintf_filtered (file, _("Software FPU type %d\n"), type);
3824   /* i18n: [floating point unit] mask */
3825   fputs_filtered (_("mask: "), file);
3826   print_fpu_flags (file, status >> 16);
3827   /* i18n: [floating point unit] flags */
3828   fputs_filtered (_("flags: "), file);
3829   print_fpu_flags (file, status);
3830 }
3831
3832 /* Construct the ARM extended floating point type.  */
3833 static struct type *
3834 arm_ext_type (struct gdbarch *gdbarch)
3835 {
3836   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
3837
3838   if (!tdep->arm_ext_type)
3839     tdep->arm_ext_type
3840       = arch_float_type (gdbarch, -1, "builtin_type_arm_ext",
3841                          floatformats_arm_ext);
3842
3843   return tdep->arm_ext_type;
3844 }
3845
3846 static struct type *
3847 arm_neon_double_type (struct gdbarch *gdbarch)
3848 {
3849   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
3850
3851   if (tdep->neon_double_type == NULL)
3852     {
3853       struct type *t, *elem;
3854
3855       t = arch_composite_type (gdbarch, "__gdb_builtin_type_neon_d",
3856                                TYPE_CODE_UNION);
3857       elem = builtin_type (gdbarch)->builtin_uint8;
3858       append_composite_type_field (t, "u8", init_vector_type (elem, 8));
3859       elem = builtin_type (gdbarch)->builtin_uint16;
3860       append_composite_type_field (t, "u16", init_vector_type (elem, 4));
3861       elem = builtin_type (gdbarch)->builtin_uint32;
3862       append_composite_type_field (t, "u32", init_vector_type (elem, 2));
3863       elem = builtin_type (gdbarch)->builtin_uint64;
3864       append_composite_type_field (t, "u64", elem);
3865       elem = builtin_type (gdbarch)->builtin_float;
3866       append_composite_type_field (t, "f32", init_vector_type (elem, 2));
3867       elem = builtin_type (gdbarch)->builtin_double;
3868       append_composite_type_field (t, "f64", elem);
3869
3870       TYPE_VECTOR (t) = 1;
3871       TYPE_NAME (t) = "neon_d";
3872       tdep->neon_double_type = t;
3873     }
3874
3875   return tdep->neon_double_type;
3876 }
3877
3878 /* FIXME: The vector types are not correctly ordered on big-endian
3879    targets.  Just as s0 is the low bits of d0, d0[0] is also the low
3880    bits of d0 - regardless of what unit size is being held in d0.  So
3881    the offset of the first uint8 in d0 is 7, but the offset of the
3882    first float is 4.  This code works as-is for little-endian
3883    targets.  */
3884
3885 static struct type *
3886 arm_neon_quad_type (struct gdbarch *gdbarch)
3887 {
3888   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
3889
3890   if (tdep->neon_quad_type == NULL)
3891     {
3892       struct type *t, *elem;
3893
3894       t = arch_composite_type (gdbarch, "__gdb_builtin_type_neon_q",
3895                                TYPE_CODE_UNION);
3896       elem = builtin_type (gdbarch)->builtin_uint8;
3897       append_composite_type_field (t, "u8", init_vector_type (elem, 16));
3898       elem = builtin_type (gdbarch)->builtin_uint16;
3899       append_composite_type_field (t, "u16", init_vector_type (elem, 8));
3900       elem = builtin_type (gdbarch)->builtin_uint32;
3901       append_composite_type_field (t, "u32", init_vector_type (elem, 4));
3902       elem = builtin_type (gdbarch)->builtin_uint64;
3903       append_composite_type_field (t, "u64", init_vector_type (elem, 2));
3904       elem = builtin_type (gdbarch)->builtin_float;
3905       append_composite_type_field (t, "f32", init_vector_type (elem, 4));
3906       elem = builtin_type (gdbarch)->builtin_double;
3907       append_composite_type_field (t, "f64", init_vector_type (elem, 2));
3908
3909       TYPE_VECTOR (t) = 1;
3910       TYPE_NAME (t) = "neon_q";
3911       tdep->neon_quad_type = t;
3912     }
3913
3914   return tdep->neon_quad_type;
3915 }
3916
3917 /* Return the GDB type object for the "standard" data type of data in
3918    register N.  */
3919
3920 static struct type *
3921 arm_register_type (struct gdbarch *gdbarch, int regnum)
3922 {
3923   int num_regs = gdbarch_num_regs (gdbarch);
3924
3925   if (gdbarch_tdep (gdbarch)->have_vfp_pseudos
3926       && regnum >= num_regs && regnum < num_regs + 32)
3927     return builtin_type (gdbarch)->builtin_float;
3928
3929   if (gdbarch_tdep (gdbarch)->have_neon_pseudos
3930       && regnum >= num_regs + 32 && regnum < num_regs + 32 + 16)
3931     return arm_neon_quad_type (gdbarch);
3932
3933   /* If the target description has register information, we are only
3934      in this function so that we can override the types of
3935      double-precision registers for NEON.  */
3936   if (tdesc_has_registers (gdbarch_target_desc (gdbarch)))
3937     {
3938       struct type *t = tdesc_register_type (gdbarch, regnum);
3939
3940       if (regnum >= ARM_D0_REGNUM && regnum < ARM_D0_REGNUM + 32
3941           && TYPE_CODE (t) == TYPE_CODE_FLT
3942           && gdbarch_tdep (gdbarch)->have_neon)
3943         return arm_neon_double_type (gdbarch);
3944       else
3945         return t;
3946     }
3947
3948   if (regnum >= ARM_F0_REGNUM && regnum < ARM_F0_REGNUM + NUM_FREGS)
3949     {
3950       if (!gdbarch_tdep (gdbarch)->have_fpa_registers)
3951         return builtin_type (gdbarch)->builtin_void;
3952
3953       return arm_ext_type (gdbarch);
3954     }
3955   else if (regnum == ARM_SP_REGNUM)
3956     return builtin_type (gdbarch)->builtin_data_ptr;
3957   else if (regnum == ARM_PC_REGNUM)
3958     return builtin_type (gdbarch)->builtin_func_ptr;
3959   else if (regnum >= ARRAY_SIZE (arm_register_names))
3960     /* These registers are only supported on targets which supply
3961        an XML description.  */
3962     return builtin_type (gdbarch)->builtin_int0;
3963   else
3964     return builtin_type (gdbarch)->builtin_uint32;
3965 }
3966
3967 /* Map a DWARF register REGNUM onto the appropriate GDB register
3968    number.  */
3969
3970 static int
3971 arm_dwarf_reg_to_regnum (struct gdbarch *gdbarch, int reg)
3972 {
3973   /* Core integer regs.  */
3974   if (reg >= 0 && reg <= 15)
3975     return reg;
3976
3977   /* Legacy FPA encoding.  These were once used in a way which
3978      overlapped with VFP register numbering, so their use is
3979      discouraged, but GDB doesn't support the ARM toolchain
3980      which used them for VFP.  */
3981   if (reg >= 16 && reg <= 23)
3982     return ARM_F0_REGNUM + reg - 16;
3983
3984   /* New assignments for the FPA registers.  */
3985   if (reg >= 96 && reg <= 103)
3986     return ARM_F0_REGNUM + reg - 96;
3987
3988   /* WMMX register assignments.  */
3989   if (reg >= 104 && reg <= 111)
3990     return ARM_WCGR0_REGNUM + reg - 104;
3991
3992   if (reg >= 112 && reg <= 127)
3993     return ARM_WR0_REGNUM + reg - 112;
3994
3995   if (reg >= 192 && reg <= 199)
3996     return ARM_WC0_REGNUM + reg - 192;
3997
3998   /* VFP v2 registers.  A double precision value is actually
3999      in d1 rather than s2, but the ABI only defines numbering
4000      for the single precision registers.  This will "just work"
4001      in GDB for little endian targets (we'll read eight bytes,
4002      starting in s0 and then progressing to s1), but will be
4003      reversed on big endian targets with VFP.  This won't
4004      be a problem for the new Neon quad registers; you're supposed
4005      to use DW_OP_piece for those.  */
4006   if (reg >= 64 && reg <= 95)
4007     {
4008       char name_buf[4];
4009
4010       xsnprintf (name_buf, sizeof (name_buf), "s%d", reg - 64);
4011       return user_reg_map_name_to_regnum (gdbarch, name_buf,
4012                                           strlen (name_buf));
4013     }
4014
4015   /* VFP v3 / Neon registers.  This range is also used for VFP v2
4016      registers, except that it now describes d0 instead of s0.  */
4017   if (reg >= 256 && reg <= 287)
4018     {
4019       char name_buf[4];
4020
4021       xsnprintf (name_buf, sizeof (name_buf), "d%d", reg - 256);
4022       return user_reg_map_name_to_regnum (gdbarch, name_buf,
4023                                           strlen (name_buf));
4024     }
4025
4026   return -1;
4027 }
4028
4029 /* Map GDB internal REGNUM onto the Arm simulator register numbers.  */
4030 static int
4031 arm_register_sim_regno (struct gdbarch *gdbarch, int regnum)
4032 {
4033   int reg = regnum;
4034   gdb_assert (reg >= 0 && reg < gdbarch_num_regs (gdbarch));
4035
4036   if (regnum >= ARM_WR0_REGNUM && regnum <= ARM_WR15_REGNUM)
4037     return regnum - ARM_WR0_REGNUM + SIM_ARM_IWMMXT_COP0R0_REGNUM;
4038
4039   if (regnum >= ARM_WC0_REGNUM && regnum <= ARM_WC7_REGNUM)
4040     return regnum - ARM_WC0_REGNUM + SIM_ARM_IWMMXT_COP1R0_REGNUM;
4041
4042   if (regnum >= ARM_WCGR0_REGNUM && regnum <= ARM_WCGR7_REGNUM)
4043     return regnum - ARM_WCGR0_REGNUM + SIM_ARM_IWMMXT_COP1R8_REGNUM;
4044
4045   if (reg < NUM_GREGS)
4046     return SIM_ARM_R0_REGNUM + reg;
4047   reg -= NUM_GREGS;
4048
4049   if (reg < NUM_FREGS)
4050     return SIM_ARM_FP0_REGNUM + reg;
4051   reg -= NUM_FREGS;
4052
4053   if (reg < NUM_SREGS)
4054     return SIM_ARM_FPS_REGNUM + reg;
4055   reg -= NUM_SREGS;
4056
4057   internal_error (__FILE__, __LINE__, _("Bad REGNUM %d"), regnum);
4058 }
4059
4060 /* NOTE: cagney/2001-08-20: Both convert_from_extended() and
4061    convert_to_extended() use floatformat_arm_ext_littlebyte_bigword.
4062    It is thought that this is is the floating-point register format on
4063    little-endian systems.  */
4064
4065 static void
4066 convert_from_extended (const struct floatformat *fmt, const void *ptr,
4067                        void *dbl, int endianess)
4068 {
4069   DOUBLEST d;
4070
4071   if (endianess == BFD_ENDIAN_BIG)
4072     floatformat_to_doublest (&floatformat_arm_ext_big, ptr, &d);
4073   else
4074     floatformat_to_doublest (&floatformat_arm_ext_littlebyte_bigword,
4075                              ptr, &d);
4076   floatformat_from_doublest (fmt, &d, dbl);
4077 }
4078
4079 static void
4080 convert_to_extended (const struct floatformat *fmt, void *dbl, const void *ptr,
4081                      int endianess)
4082 {
4083   DOUBLEST d;
4084
4085   floatformat_to_doublest (fmt, ptr, &d);
4086   if (endianess == BFD_ENDIAN_BIG)
4087     floatformat_from_doublest (&floatformat_arm_ext_big, &d, dbl);
4088   else
4089     floatformat_from_doublest (&floatformat_arm_ext_littlebyte_bigword,
4090                                &d, dbl);
4091 }
4092
4093 /* Like insert_single_step_breakpoint, but make sure we use a breakpoint
4094    of the appropriate mode (as encoded in the PC value), even if this
4095    differs from what would be expected according to the symbol tables.  */
4096
4097 void
4098 arm_insert_single_step_breakpoint (struct gdbarch *gdbarch,
4099                                    struct address_space *aspace,
4100                                    CORE_ADDR pc)
4101 {
4102   struct cleanup *old_chain
4103     = make_cleanup_restore_integer (&arm_override_mode);
4104
4105   arm_override_mode = IS_THUMB_ADDR (pc);
4106   pc = gdbarch_addr_bits_remove (gdbarch, pc);
4107
4108   insert_single_step_breakpoint (gdbarch, aspace, pc);
4109
4110   do_cleanups (old_chain);
4111 }
4112
4113 /* Given BUF, which is OLD_LEN bytes ending at ENDADDR, expand
4114    the buffer to be NEW_LEN bytes ending at ENDADDR.  Return
4115    NULL if an error occurs.  BUF is freed.  */
4116
4117 static gdb_byte *
4118 extend_buffer_earlier (gdb_byte *buf, CORE_ADDR endaddr,
4119                        int old_len, int new_len)
4120 {
4121   gdb_byte *new_buf;
4122   int bytes_to_read = new_len - old_len;
4123
4124   new_buf = (gdb_byte *) xmalloc (new_len);
4125   memcpy (new_buf + bytes_to_read, buf, old_len);
4126   xfree (buf);
4127   if (target_read_memory (endaddr - new_len, new_buf, bytes_to_read) != 0)
4128     {
4129       xfree (new_buf);
4130       return NULL;
4131     }
4132   return new_buf;
4133 }
4134
4135 /* An IT block is at most the 2-byte IT instruction followed by
4136    four 4-byte instructions.  The furthest back we must search to
4137    find an IT block that affects the current instruction is thus
4138    2 + 3 * 4 == 14 bytes.  */
4139 #define MAX_IT_BLOCK_PREFIX 14
4140
4141 /* Use a quick scan if there are more than this many bytes of
4142    code.  */
4143 #define IT_SCAN_THRESHOLD 32
4144
4145 /* Adjust a breakpoint's address to move breakpoints out of IT blocks.
4146    A breakpoint in an IT block may not be hit, depending on the
4147    condition flags.  */
4148 static CORE_ADDR
4149 arm_adjust_breakpoint_address (struct gdbarch *gdbarch, CORE_ADDR bpaddr)
4150 {
4151   gdb_byte *buf;
4152   char map_type;
4153   CORE_ADDR boundary, func_start;
4154   int buf_len;
4155   enum bfd_endian order = gdbarch_byte_order_for_code (gdbarch);
4156   int i, any, last_it, last_it_count;
4157
4158   /* If we are using BKPT breakpoints, none of this is necessary.  */
4159   if (gdbarch_tdep (gdbarch)->thumb2_breakpoint == NULL)
4160     return bpaddr;
4161
4162   /* ARM mode does not have this problem.  */
4163   if (!arm_pc_is_thumb (gdbarch, bpaddr))
4164     return bpaddr;
4165
4166   /* We are setting a breakpoint in Thumb code that could potentially
4167      contain an IT block.  The first step is to find how much Thumb
4168      code there is; we do not need to read outside of known Thumb
4169      sequences.  */
4170   map_type = arm_find_mapping_symbol (bpaddr, &boundary);
4171   if (map_type == 0)
4172     /* Thumb-2 code must have mapping symbols to have a chance.  */
4173     return bpaddr;
4174
4175   bpaddr = gdbarch_addr_bits_remove (gdbarch, bpaddr);
4176
4177   if (find_pc_partial_function (bpaddr, NULL, &func_start, NULL)
4178       && func_start > boundary)
4179     boundary = func_start;
4180
4181   /* Search for a candidate IT instruction.  We have to do some fancy
4182      footwork to distinguish a real IT instruction from the second
4183      half of a 32-bit instruction, but there is no need for that if
4184      there's no candidate.  */
4185   buf_len = min (bpaddr - boundary, MAX_IT_BLOCK_PREFIX);
4186   if (buf_len == 0)
4187     /* No room for an IT instruction.  */
4188     return bpaddr;
4189
4190   buf = (gdb_byte *) xmalloc (buf_len);
4191   if (target_read_memory (bpaddr - buf_len, buf, buf_len) != 0)
4192     return bpaddr;
4193   any = 0;
4194   for (i = 0; i < buf_len; i += 2)
4195     {
4196       unsigned short inst1 = extract_unsigned_integer (&buf[i], 2, order);
4197       if ((inst1 & 0xff00) == 0xbf00 && (inst1 & 0x000f) != 0)
4198         {
4199           any = 1;
4200           break;
4201         }
4202     }
4203
4204   if (any == 0)
4205     {
4206       xfree (buf);
4207       return bpaddr;
4208     }
4209
4210   /* OK, the code bytes before this instruction contain at least one
4211      halfword which resembles an IT instruction.  We know that it's
4212      Thumb code, but there are still two possibilities.  Either the
4213      halfword really is an IT instruction, or it is the second half of
4214      a 32-bit Thumb instruction.  The only way we can tell is to
4215      scan forwards from a known instruction boundary.  */
4216   if (bpaddr - boundary > IT_SCAN_THRESHOLD)
4217     {
4218       int definite;
4219
4220       /* There's a lot of code before this instruction.  Start with an
4221          optimistic search; it's easy to recognize halfwords that can
4222          not be the start of a 32-bit instruction, and use that to
4223          lock on to the instruction boundaries.  */
4224       buf = extend_buffer_earlier (buf, bpaddr, buf_len, IT_SCAN_THRESHOLD);
4225       if (buf == NULL)
4226         return bpaddr;
4227       buf_len = IT_SCAN_THRESHOLD;
4228
4229       definite = 0;
4230       for (i = 0; i < buf_len - sizeof (buf) && ! definite; i += 2)
4231         {
4232           unsigned short inst1 = extract_unsigned_integer (&buf[i], 2, order);
4233           if (thumb_insn_size (inst1) == 2)
4234             {
4235               definite = 1;
4236               break;
4237             }
4238         }
4239
4240       /* At this point, if DEFINITE, BUF[I] is the first place we
4241          are sure that we know the instruction boundaries, and it is far
4242          enough from BPADDR that we could not miss an IT instruction
4243          affecting BPADDR.  If ! DEFINITE, give up - start from a
4244          known boundary.  */
4245       if (! definite)
4246         {
4247           buf = extend_buffer_earlier (buf, bpaddr, buf_len,
4248                                        bpaddr - boundary);
4249           if (buf == NULL)
4250             return bpaddr;
4251           buf_len = bpaddr - boundary;
4252           i = 0;
4253         }
4254     }
4255   else
4256     {
4257       buf = extend_buffer_earlier (buf, bpaddr, buf_len, bpaddr - boundary);
4258       if (buf == NULL)
4259         return bpaddr;
4260       buf_len = bpaddr - boundary;
4261       i = 0;
4262     }
4263
4264   /* Scan forwards.  Find the last IT instruction before BPADDR.  */
4265   last_it = -1;
4266   last_it_count = 0;
4267   while (i < buf_len)
4268     {
4269       unsigned short inst1 = extract_unsigned_integer (&buf[i], 2, order);
4270       last_it_count--;
4271       if ((inst1 & 0xff00) == 0xbf00 && (inst1 & 0x000f) != 0)
4272         {
4273           last_it = i;
4274           if (inst1 & 0x0001)
4275             last_it_count = 4;
4276           else if (inst1 & 0x0002)
4277             last_it_count = 3;
4278           else if (inst1 & 0x0004)
4279             last_it_count = 2;
4280           else
4281             last_it_count = 1;
4282         }
4283       i += thumb_insn_size (inst1);
4284     }
4285
4286   xfree (buf);
4287
4288   if (last_it == -1)
4289     /* There wasn't really an IT instruction after all.  */
4290     return bpaddr;
4291
4292   if (last_it_count < 1)
4293     /* It was too far away.  */
4294     return bpaddr;
4295
4296   /* This really is a trouble spot.  Move the breakpoint to the IT
4297      instruction.  */
4298   return bpaddr - buf_len + last_it;
4299 }
4300
4301 /* ARM displaced stepping support.
4302
4303    Generally ARM displaced stepping works as follows:
4304
4305    1. When an instruction is to be single-stepped, it is first decoded by
4306       arm_process_displaced_insn.  Depending on the type of instruction, it is
4307       then copied to a scratch location, possibly in a modified form.  The
4308       copy_* set of functions performs such modification, as necessary.  A
4309       breakpoint is placed after the modified instruction in the scratch space
4310       to return control to GDB.  Note in particular that instructions which
4311       modify the PC will no longer do so after modification.
4312
4313    2. The instruction is single-stepped, by setting the PC to the scratch
4314       location address, and resuming.  Control returns to GDB when the
4315       breakpoint is hit.
4316
4317    3. A cleanup function (cleanup_*) is called corresponding to the copy_*
4318       function used for the current instruction.  This function's job is to
4319       put the CPU/memory state back to what it would have been if the
4320       instruction had been executed unmodified in its original location.  */
4321
4322 /* NOP instruction (mov r0, r0).  */
4323 #define ARM_NOP                         0xe1a00000
4324 #define THUMB_NOP 0x4600
4325
4326 /* Helper for register reads for displaced stepping.  In particular, this
4327    returns the PC as it would be seen by the instruction at its original
4328    location.  */
4329
4330 ULONGEST
4331 displaced_read_reg (struct regcache *regs, struct displaced_step_closure *dsc,
4332                     int regno)
4333 {
4334   ULONGEST ret;
4335   CORE_ADDR from = dsc->insn_addr;
4336
4337   if (regno == ARM_PC_REGNUM)
4338     {
4339       /* Compute pipeline offset:
4340          - When executing an ARM instruction, PC reads as the address of the
4341          current instruction plus 8.
4342          - When executing a Thumb instruction, PC reads as the address of the
4343          current instruction plus 4.  */
4344
4345       if (!dsc->is_thumb)
4346         from += 8;
4347       else
4348         from += 4;
4349
4350       if (debug_displaced)
4351         fprintf_unfiltered (gdb_stdlog, "displaced: read pc value %.8lx\n",
4352                             (unsigned long) from);
4353       return (ULONGEST) from;
4354     }
4355   else
4356     {
4357       regcache_cooked_read_unsigned (regs, regno, &ret);
4358       if (debug_displaced)
4359         fprintf_unfiltered (gdb_stdlog, "displaced: read r%d value %.8lx\n",
4360                             regno, (unsigned long) ret);
4361       return ret;
4362     }
4363 }
4364
4365 static int
4366 displaced_in_arm_mode (struct regcache *regs)
4367 {
4368   ULONGEST ps;
4369   ULONGEST t_bit = arm_psr_thumb_bit (get_regcache_arch (regs));
4370
4371   regcache_cooked_read_unsigned (regs, ARM_PS_REGNUM, &ps);
4372
4373   return (ps & t_bit) == 0;
4374 }
4375
4376 /* Write to the PC as from a branch instruction.  */
4377
4378 static void
4379 branch_write_pc (struct regcache *regs, struct displaced_step_closure *dsc,
4380                  ULONGEST val)
4381 {
4382   if (!dsc->is_thumb)
4383     /* Note: If bits 0/1 are set, this branch would be unpredictable for
4384        architecture versions < 6.  */
4385     regcache_cooked_write_unsigned (regs, ARM_PC_REGNUM,
4386                                     val & ~(ULONGEST) 0x3);
4387   else
4388     regcache_cooked_write_unsigned (regs, ARM_PC_REGNUM,
4389                                     val & ~(ULONGEST) 0x1);
4390 }
4391
4392 /* Write to the PC as from a branch-exchange instruction.  */
4393
4394 static void
4395 bx_write_pc (struct regcache *regs, ULONGEST val)
4396 {
4397   ULONGEST ps;
4398   ULONGEST t_bit = arm_psr_thumb_bit (get_regcache_arch (regs));
4399
4400   regcache_cooked_read_unsigned (regs, ARM_PS_REGNUM, &ps);
4401
4402   if ((val & 1) == 1)
4403     {
4404       regcache_cooked_write_unsigned (regs, ARM_PS_REGNUM, ps | t_bit);
4405       regcache_cooked_write_unsigned (regs, ARM_PC_REGNUM, val & 0xfffffffe);
4406     }
4407   else if ((val & 2) == 0)
4408     {
4409       regcache_cooked_write_unsigned (regs, ARM_PS_REGNUM, ps & ~t_bit);
4410       regcache_cooked_write_unsigned (regs, ARM_PC_REGNUM, val);
4411     }
4412   else
4413     {
4414       /* Unpredictable behaviour.  Try to do something sensible (switch to ARM
4415           mode, align dest to 4 bytes).  */
4416       warning (_("Single-stepping BX to non-word-aligned ARM instruction."));
4417       regcache_cooked_write_unsigned (regs, ARM_PS_REGNUM, ps & ~t_bit);
4418       regcache_cooked_write_unsigned (regs, ARM_PC_REGNUM, val & 0xfffffffc);
4419     }
4420 }
4421
4422 /* Write to the PC as if from a load instruction.  */
4423
4424 static void
4425 load_write_pc (struct regcache *regs, struct displaced_step_closure *dsc,
4426                ULONGEST val)
4427 {
4428   if (DISPLACED_STEPPING_ARCH_VERSION >= 5)
4429     bx_write_pc (regs, val);
4430   else
4431     branch_write_pc (regs, dsc, val);
4432 }
4433
4434 /* Write to the PC as if from an ALU instruction.  */
4435
4436 static void
4437 alu_write_pc (struct regcache *regs, struct displaced_step_closure *dsc,
4438               ULONGEST val)
4439 {
4440   if (DISPLACED_STEPPING_ARCH_VERSION >= 7 && !dsc->is_thumb)
4441     bx_write_pc (regs, val);
4442   else
4443     branch_write_pc (regs, dsc, val);
4444 }
4445
4446 /* Helper for writing to registers for displaced stepping.  Writing to the PC
4447    has a varying effects depending on the instruction which does the write:
4448    this is controlled by the WRITE_PC argument.  */
4449
4450 void
4451 displaced_write_reg (struct regcache *regs, struct displaced_step_closure *dsc,
4452                      int regno, ULONGEST val, enum pc_write_style write_pc)
4453 {
4454   if (regno == ARM_PC_REGNUM)
4455     {
4456       if (debug_displaced)
4457         fprintf_unfiltered (gdb_stdlog, "displaced: writing pc %.8lx\n",
4458                             (unsigned long) val);
4459       switch (write_pc)
4460         {
4461         case BRANCH_WRITE_PC:
4462           branch_write_pc (regs, dsc, val);
4463           break;
4464
4465         case BX_WRITE_PC:
4466           bx_write_pc (regs, val);
4467           break;
4468
4469         case LOAD_WRITE_PC:
4470           load_write_pc (regs, dsc, val);
4471           break;
4472
4473         case ALU_WRITE_PC:
4474           alu_write_pc (regs, dsc, val);
4475           break;
4476
4477         case CANNOT_WRITE_PC:
4478           warning (_("Instruction wrote to PC in an unexpected way when "
4479                      "single-stepping"));
4480           break;
4481
4482         default:
4483           internal_error (__FILE__, __LINE__,
4484                           _("Invalid argument to displaced_write_reg"));
4485         }
4486
4487       dsc->wrote_to_pc = 1;
4488     }
4489   else
4490     {
4491       if (debug_displaced)
4492         fprintf_unfiltered (gdb_stdlog, "displaced: writing r%d value %.8lx\n",
4493                             regno, (unsigned long) val);
4494       regcache_cooked_write_unsigned (regs, regno, val);
4495     }
4496 }
4497
4498 /* This function is used to concisely determine if an instruction INSN
4499    references PC.  Register fields of interest in INSN should have the
4500    corresponding fields of BITMASK set to 0b1111.  The function
4501    returns return 1 if any of these fields in INSN reference the PC
4502    (also 0b1111, r15), else it returns 0.  */
4503
4504 static int
4505 insn_references_pc (uint32_t insn, uint32_t bitmask)
4506 {
4507   uint32_t lowbit = 1;
4508
4509   while (bitmask != 0)
4510     {
4511       uint32_t mask;
4512
4513       for (; lowbit && (bitmask & lowbit) == 0; lowbit <<= 1)
4514         ;
4515
4516       if (!lowbit)
4517         break;
4518
4519       mask = lowbit * 0xf;
4520
4521       if ((insn & mask) == mask)
4522         return 1;
4523
4524       bitmask &= ~mask;
4525     }
4526
4527   return 0;
4528 }
4529
4530 /* The simplest copy function.  Many instructions have the same effect no
4531    matter what address they are executed at: in those cases, use this.  */
4532
4533 static int
4534 arm_copy_unmodified (struct gdbarch *gdbarch, uint32_t insn,
4535                      const char *iname, struct displaced_step_closure *dsc)
4536 {
4537   if (debug_displaced)
4538     fprintf_unfiltered (gdb_stdlog, "displaced: copying insn %.8lx, "
4539                         "opcode/class '%s' unmodified\n", (unsigned long) insn,
4540                         iname);
4541
4542   dsc->modinsn[0] = insn;
4543
4544   return 0;
4545 }
4546
4547 static int
4548 thumb_copy_unmodified_32bit (struct gdbarch *gdbarch, uint16_t insn1,
4549                              uint16_t insn2, const char *iname,
4550                              struct displaced_step_closure *dsc)
4551 {
4552   if (debug_displaced)
4553     fprintf_unfiltered (gdb_stdlog, "displaced: copying insn %.4x %.4x, "
4554                         "opcode/class '%s' unmodified\n", insn1, insn2,
4555                         iname);
4556
4557   dsc->modinsn[0] = insn1;
4558   dsc->modinsn[1] = insn2;
4559   dsc->numinsns = 2;
4560
4561   return 0;
4562 }
4563
4564 /* Copy 16-bit Thumb(Thumb and 16-bit Thumb-2) instruction without any
4565    modification.  */
4566 static int
4567 thumb_copy_unmodified_16bit (struct gdbarch *gdbarch, uint16_t insn,
4568                              const char *iname,
4569                              struct displaced_step_closure *dsc)
4570 {
4571   if (debug_displaced)
4572     fprintf_unfiltered (gdb_stdlog, "displaced: copying insn %.4x, "
4573                         "opcode/class '%s' unmodified\n", insn,
4574                         iname);
4575
4576   dsc->modinsn[0] = insn;
4577
4578   return 0;
4579 }
4580
4581 /* Preload instructions with immediate offset.  */
4582
4583 static void
4584 cleanup_preload (struct gdbarch *gdbarch,
4585                  struct regcache *regs, struct displaced_step_closure *dsc)
4586 {
4587   displaced_write_reg (regs, dsc, 0, dsc->tmp[0], CANNOT_WRITE_PC);
4588   if (!dsc->u.preload.immed)
4589     displaced_write_reg (regs, dsc, 1, dsc->tmp[1], CANNOT_WRITE_PC);
4590 }
4591
4592 static void
4593 install_preload (struct gdbarch *gdbarch, struct regcache *regs,
4594                  struct displaced_step_closure *dsc, unsigned int rn)
4595 {
4596   ULONGEST rn_val;
4597   /* Preload instructions:
4598
4599      {pli/pld} [rn, #+/-imm]
4600      ->
4601      {pli/pld} [r0, #+/-imm].  */
4602
4603   dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
4604   rn_val = displaced_read_reg (regs, dsc, rn);
4605   displaced_write_reg (regs, dsc, 0, rn_val, CANNOT_WRITE_PC);
4606   dsc->u.preload.immed = 1;
4607
4608   dsc->cleanup = &cleanup_preload;
4609 }
4610
4611 static int
4612 arm_copy_preload (struct gdbarch *gdbarch, uint32_t insn, struct regcache *regs,
4613                   struct displaced_step_closure *dsc)
4614 {
4615   unsigned int rn = bits (insn, 16, 19);
4616
4617   if (!insn_references_pc (insn, 0x000f0000ul))
4618     return arm_copy_unmodified (gdbarch, insn, "preload", dsc);
4619
4620   if (debug_displaced)
4621     fprintf_unfiltered (gdb_stdlog, "displaced: copying preload insn %.8lx\n",
4622                         (unsigned long) insn);
4623
4624   dsc->modinsn[0] = insn & 0xfff0ffff;
4625
4626   install_preload (gdbarch, regs, dsc, rn);
4627
4628   return 0;
4629 }
4630
4631 static int
4632 thumb2_copy_preload (struct gdbarch *gdbarch, uint16_t insn1, uint16_t insn2,
4633                      struct regcache *regs, struct displaced_step_closure *dsc)
4634 {
4635   unsigned int rn = bits (insn1, 0, 3);
4636   unsigned int u_bit = bit (insn1, 7);
4637   int imm12 = bits (insn2, 0, 11);
4638   ULONGEST pc_val;
4639
4640   if (rn != ARM_PC_REGNUM)
4641     return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2, "preload", dsc);
4642
4643   /* PC is only allowed to use in PLI (immediate,literal) Encoding T3, and
4644      PLD (literal) Encoding T1.  */
4645   if (debug_displaced)
4646     fprintf_unfiltered (gdb_stdlog,
4647                         "displaced: copying pld/pli pc (0x%x) %c imm12 %.4x\n",
4648                         (unsigned int) dsc->insn_addr, u_bit ? '+' : '-',
4649                         imm12);
4650
4651   if (!u_bit)
4652     imm12 = -1 * imm12;
4653
4654   /* Rewrite instruction {pli/pld} PC imm12 into:
4655      Prepare: tmp[0] <- r0, tmp[1] <- r1, r0 <- pc, r1 <- imm12
4656
4657      {pli/pld} [r0, r1]
4658
4659      Cleanup: r0 <- tmp[0], r1 <- tmp[1].  */
4660
4661   dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
4662   dsc->tmp[1] = displaced_read_reg (regs, dsc, 1);
4663
4664   pc_val = displaced_read_reg (regs, dsc, ARM_PC_REGNUM);
4665
4666   displaced_write_reg (regs, dsc, 0, pc_val, CANNOT_WRITE_PC);
4667   displaced_write_reg (regs, dsc, 1, imm12, CANNOT_WRITE_PC);
4668   dsc->u.preload.immed = 0;
4669
4670   /* {pli/pld} [r0, r1] */
4671   dsc->modinsn[0] = insn1 & 0xfff0;
4672   dsc->modinsn[1] = 0xf001;
4673   dsc->numinsns = 2;
4674
4675   dsc->cleanup = &cleanup_preload;
4676   return 0;
4677 }
4678
4679 /* Preload instructions with register offset.  */
4680
4681 static void
4682 install_preload_reg(struct gdbarch *gdbarch, struct regcache *regs,
4683                     struct displaced_step_closure *dsc, unsigned int rn,
4684                     unsigned int rm)
4685 {
4686   ULONGEST rn_val, rm_val;
4687
4688   /* Preload register-offset instructions:
4689
4690      {pli/pld} [rn, rm {, shift}]
4691      ->
4692      {pli/pld} [r0, r1 {, shift}].  */
4693
4694   dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
4695   dsc->tmp[1] = displaced_read_reg (regs, dsc, 1);
4696   rn_val = displaced_read_reg (regs, dsc, rn);
4697   rm_val = displaced_read_reg (regs, dsc, rm);
4698   displaced_write_reg (regs, dsc, 0, rn_val, CANNOT_WRITE_PC);
4699   displaced_write_reg (regs, dsc, 1, rm_val, CANNOT_WRITE_PC);
4700   dsc->u.preload.immed = 0;
4701
4702   dsc->cleanup = &cleanup_preload;
4703 }
4704
4705 static int
4706 arm_copy_preload_reg (struct gdbarch *gdbarch, uint32_t insn,
4707                       struct regcache *regs,
4708                       struct displaced_step_closure *dsc)
4709 {
4710   unsigned int rn = bits (insn, 16, 19);
4711   unsigned int rm = bits (insn, 0, 3);
4712
4713
4714   if (!insn_references_pc (insn, 0x000f000ful))
4715     return arm_copy_unmodified (gdbarch, insn, "preload reg", dsc);
4716
4717   if (debug_displaced)
4718     fprintf_unfiltered (gdb_stdlog, "displaced: copying preload insn %.8lx\n",
4719                         (unsigned long) insn);
4720
4721   dsc->modinsn[0] = (insn & 0xfff0fff0) | 0x1;
4722
4723   install_preload_reg (gdbarch, regs, dsc, rn, rm);
4724   return 0;
4725 }
4726
4727 /* Copy/cleanup coprocessor load and store instructions.  */
4728
4729 static void
4730 cleanup_copro_load_store (struct gdbarch *gdbarch,
4731                           struct regcache *regs,
4732                           struct displaced_step_closure *dsc)
4733 {
4734   ULONGEST rn_val = displaced_read_reg (regs, dsc, 0);
4735
4736   displaced_write_reg (regs, dsc, 0, dsc->tmp[0], CANNOT_WRITE_PC);
4737
4738   if (dsc->u.ldst.writeback)
4739     displaced_write_reg (regs, dsc, dsc->u.ldst.rn, rn_val, LOAD_WRITE_PC);
4740 }
4741
4742 static void
4743 install_copro_load_store (struct gdbarch *gdbarch, struct regcache *regs,
4744                           struct displaced_step_closure *dsc,
4745                           int writeback, unsigned int rn)
4746 {
4747   ULONGEST rn_val;
4748
4749   /* Coprocessor load/store instructions:
4750
4751      {stc/stc2} [<Rn>, #+/-imm]  (and other immediate addressing modes)
4752      ->
4753      {stc/stc2} [r0, #+/-imm].
4754
4755      ldc/ldc2 are handled identically.  */
4756
4757   dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
4758   rn_val = displaced_read_reg (regs, dsc, rn);
4759   /* PC should be 4-byte aligned.  */
4760   rn_val = rn_val & 0xfffffffc;
4761   displaced_write_reg (regs, dsc, 0, rn_val, CANNOT_WRITE_PC);
4762
4763   dsc->u.ldst.writeback = writeback;
4764   dsc->u.ldst.rn = rn;
4765
4766   dsc->cleanup = &cleanup_copro_load_store;
4767 }
4768
4769 static int
4770 arm_copy_copro_load_store (struct gdbarch *gdbarch, uint32_t insn,
4771                            struct regcache *regs,
4772                            struct displaced_step_closure *dsc)
4773 {
4774   unsigned int rn = bits (insn, 16, 19);
4775
4776   if (!insn_references_pc (insn, 0x000f0000ul))
4777     return arm_copy_unmodified (gdbarch, insn, "copro load/store", dsc);
4778
4779   if (debug_displaced)
4780     fprintf_unfiltered (gdb_stdlog, "displaced: copying coprocessor "
4781                         "load/store insn %.8lx\n", (unsigned long) insn);
4782
4783   dsc->modinsn[0] = insn & 0xfff0ffff;
4784
4785   install_copro_load_store (gdbarch, regs, dsc, bit (insn, 25), rn);
4786
4787   return 0;
4788 }
4789
4790 static int
4791 thumb2_copy_copro_load_store (struct gdbarch *gdbarch, uint16_t insn1,
4792                               uint16_t insn2, struct regcache *regs,
4793                               struct displaced_step_closure *dsc)
4794 {
4795   unsigned int rn = bits (insn1, 0, 3);
4796
4797   if (rn != ARM_PC_REGNUM)
4798     return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
4799                                         "copro load/store", dsc);
4800
4801   if (debug_displaced)
4802     fprintf_unfiltered (gdb_stdlog, "displaced: copying coprocessor "
4803                         "load/store insn %.4x%.4x\n", insn1, insn2);
4804
4805   dsc->modinsn[0] = insn1 & 0xfff0;
4806   dsc->modinsn[1] = insn2;
4807   dsc->numinsns = 2;
4808
4809   /* This function is called for copying instruction LDC/LDC2/VLDR, which
4810      doesn't support writeback, so pass 0.  */
4811   install_copro_load_store (gdbarch, regs, dsc, 0, rn);
4812
4813   return 0;
4814 }
4815
4816 /* Clean up branch instructions (actually perform the branch, by setting
4817    PC).  */
4818
4819 static void
4820 cleanup_branch (struct gdbarch *gdbarch, struct regcache *regs,
4821                 struct displaced_step_closure *dsc)
4822 {
4823   uint32_t status = displaced_read_reg (regs, dsc, ARM_PS_REGNUM);
4824   int branch_taken = condition_true (dsc->u.branch.cond, status);
4825   enum pc_write_style write_pc = dsc->u.branch.exchange
4826                                  ? BX_WRITE_PC : BRANCH_WRITE_PC;
4827
4828   if (!branch_taken)
4829     return;
4830
4831   if (dsc->u.branch.link)
4832     {
4833       /* The value of LR should be the next insn of current one.  In order
4834        not to confuse logic hanlding later insn `bx lr', if current insn mode
4835        is Thumb, the bit 0 of LR value should be set to 1.  */
4836       ULONGEST next_insn_addr = dsc->insn_addr + dsc->insn_size;
4837
4838       if (dsc->is_thumb)
4839         next_insn_addr |= 0x1;
4840
4841       displaced_write_reg (regs, dsc, ARM_LR_REGNUM, next_insn_addr,
4842                            CANNOT_WRITE_PC);
4843     }
4844
4845   displaced_write_reg (regs, dsc, ARM_PC_REGNUM, dsc->u.branch.dest, write_pc);
4846 }
4847
4848 /* Copy B/BL/BLX instructions with immediate destinations.  */
4849
4850 static void
4851 install_b_bl_blx (struct gdbarch *gdbarch, struct regcache *regs,
4852                   struct displaced_step_closure *dsc,
4853                   unsigned int cond, int exchange, int link, long offset)
4854 {
4855   /* Implement "BL<cond> <label>" as:
4856
4857      Preparation: cond <- instruction condition
4858      Insn: mov r0, r0  (nop)
4859      Cleanup: if (condition true) { r14 <- pc; pc <- label }.
4860
4861      B<cond> similar, but don't set r14 in cleanup.  */
4862
4863   dsc->u.branch.cond = cond;
4864   dsc->u.branch.link = link;
4865   dsc->u.branch.exchange = exchange;
4866
4867   dsc->u.branch.dest = dsc->insn_addr;
4868   if (link && exchange)
4869     /* For BLX, offset is computed from the Align (PC, 4).  */
4870     dsc->u.branch.dest = dsc->u.branch.dest & 0xfffffffc;
4871
4872   if (dsc->is_thumb)
4873     dsc->u.branch.dest += 4 + offset;
4874   else
4875     dsc->u.branch.dest += 8 + offset;
4876
4877   dsc->cleanup = &cleanup_branch;
4878 }
4879 static int
4880 arm_copy_b_bl_blx (struct gdbarch *gdbarch, uint32_t insn,
4881                    struct regcache *regs, struct displaced_step_closure *dsc)
4882 {
4883   unsigned int cond = bits (insn, 28, 31);
4884   int exchange = (cond == 0xf);
4885   int link = exchange || bit (insn, 24);
4886   long offset;
4887
4888   if (debug_displaced)
4889     fprintf_unfiltered (gdb_stdlog, "displaced: copying %s immediate insn "
4890                         "%.8lx\n", (exchange) ? "blx" : (link) ? "bl" : "b",
4891                         (unsigned long) insn);
4892   if (exchange)
4893     /* For BLX, set bit 0 of the destination.  The cleanup_branch function will
4894        then arrange the switch into Thumb mode.  */
4895     offset = (bits (insn, 0, 23) << 2) | (bit (insn, 24) << 1) | 1;
4896   else
4897     offset = bits (insn, 0, 23) << 2;
4898
4899   if (bit (offset, 25))
4900     offset = offset | ~0x3ffffff;
4901
4902   dsc->modinsn[0] = ARM_NOP;
4903
4904   install_b_bl_blx (gdbarch, regs, dsc, cond, exchange, link, offset);
4905   return 0;
4906 }
4907
4908 static int
4909 thumb2_copy_b_bl_blx (struct gdbarch *gdbarch, uint16_t insn1,
4910                       uint16_t insn2, struct regcache *regs,
4911                       struct displaced_step_closure *dsc)
4912 {
4913   int link = bit (insn2, 14);
4914   int exchange = link && !bit (insn2, 12);
4915   int cond = INST_AL;
4916   long offset = 0;
4917   int j1 = bit (insn2, 13);
4918   int j2 = bit (insn2, 11);
4919   int s = sbits (insn1, 10, 10);
4920   int i1 = !(j1 ^ bit (insn1, 10));
4921   int i2 = !(j2 ^ bit (insn1, 10));
4922
4923   if (!link && !exchange) /* B */
4924     {
4925       offset = (bits (insn2, 0, 10) << 1);
4926       if (bit (insn2, 12)) /* Encoding T4 */
4927         {
4928           offset |= (bits (insn1, 0, 9) << 12)
4929             | (i2 << 22)
4930             | (i1 << 23)
4931             | (s << 24);
4932           cond = INST_AL;
4933         }
4934       else /* Encoding T3 */
4935         {
4936           offset |= (bits (insn1, 0, 5) << 12)
4937             | (j1 << 18)
4938             | (j2 << 19)
4939             | (s << 20);
4940           cond = bits (insn1, 6, 9);
4941         }
4942     }
4943   else
4944     {
4945       offset = (bits (insn1, 0, 9) << 12);
4946       offset |= ((i2 << 22) | (i1 << 23) | (s << 24));
4947       offset |= exchange ?
4948         (bits (insn2, 1, 10) << 2) : (bits (insn2, 0, 10) << 1);
4949     }
4950
4951   if (debug_displaced)
4952     fprintf_unfiltered (gdb_stdlog, "displaced: copying %s insn "
4953                         "%.4x %.4x with offset %.8lx\n",
4954                         link ? (exchange) ? "blx" : "bl" : "b",
4955                         insn1, insn2, offset);
4956
4957   dsc->modinsn[0] = THUMB_NOP;
4958
4959   install_b_bl_blx (gdbarch, regs, dsc, cond, exchange, link, offset);
4960   return 0;
4961 }
4962
4963 /* Copy B Thumb instructions.  */
4964 static int
4965 thumb_copy_b (struct gdbarch *gdbarch, uint16_t insn,
4966               struct displaced_step_closure *dsc)
4967 {
4968   unsigned int cond = 0;
4969   int offset = 0;
4970   unsigned short bit_12_15 = bits (insn, 12, 15);
4971   CORE_ADDR from = dsc->insn_addr;
4972
4973   if (bit_12_15 == 0xd)
4974     {
4975       /* offset = SignExtend (imm8:0, 32) */
4976       offset = sbits ((insn << 1), 0, 8);
4977       cond = bits (insn, 8, 11);
4978     }
4979   else if (bit_12_15 == 0xe) /* Encoding T2 */
4980     {
4981       offset = sbits ((insn << 1), 0, 11);
4982       cond = INST_AL;
4983     }
4984
4985   if (debug_displaced)
4986     fprintf_unfiltered (gdb_stdlog,
4987                         "displaced: copying b immediate insn %.4x "
4988                         "with offset %d\n", insn, offset);
4989
4990   dsc->u.branch.cond = cond;
4991   dsc->u.branch.link = 0;
4992   dsc->u.branch.exchange = 0;
4993   dsc->u.branch.dest = from + 4 + offset;
4994
4995   dsc->modinsn[0] = THUMB_NOP;
4996
4997   dsc->cleanup = &cleanup_branch;
4998
4999   return 0;
5000 }
5001
5002 /* Copy BX/BLX with register-specified destinations.  */
5003
5004 static void
5005 install_bx_blx_reg (struct gdbarch *gdbarch, struct regcache *regs,
5006                     struct displaced_step_closure *dsc, int link,
5007                     unsigned int cond, unsigned int rm)
5008 {
5009   /* Implement {BX,BLX}<cond> <reg>" as:
5010
5011      Preparation: cond <- instruction condition
5012      Insn: mov r0, r0 (nop)
5013      Cleanup: if (condition true) { r14 <- pc; pc <- dest; }.
5014
5015      Don't set r14 in cleanup for BX.  */
5016
5017   dsc->u.branch.dest = displaced_read_reg (regs, dsc, rm);
5018
5019   dsc->u.branch.cond = cond;
5020   dsc->u.branch.link = link;
5021
5022   dsc->u.branch.exchange = 1;
5023
5024   dsc->cleanup = &cleanup_branch;
5025 }
5026
5027 static int
5028 arm_copy_bx_blx_reg (struct gdbarch *gdbarch, uint32_t insn,
5029                      struct regcache *regs, struct displaced_step_closure *dsc)
5030 {
5031   unsigned int cond = bits (insn, 28, 31);
5032   /* BX:  x12xxx1x
5033      BLX: x12xxx3x.  */
5034   int link = bit (insn, 5);
5035   unsigned int rm = bits (insn, 0, 3);
5036
5037   if (debug_displaced)
5038     fprintf_unfiltered (gdb_stdlog, "displaced: copying insn %.8lx",
5039                         (unsigned long) insn);
5040
5041   dsc->modinsn[0] = ARM_NOP;
5042
5043   install_bx_blx_reg (gdbarch, regs, dsc, link, cond, rm);
5044   return 0;
5045 }
5046
5047 static int
5048 thumb_copy_bx_blx_reg (struct gdbarch *gdbarch, uint16_t insn,
5049                        struct regcache *regs,
5050                        struct displaced_step_closure *dsc)
5051 {
5052   int link = bit (insn, 7);
5053   unsigned int rm = bits (insn, 3, 6);
5054
5055   if (debug_displaced)
5056     fprintf_unfiltered (gdb_stdlog, "displaced: copying insn %.4x",
5057                         (unsigned short) insn);
5058
5059   dsc->modinsn[0] = THUMB_NOP;
5060
5061   install_bx_blx_reg (gdbarch, regs, dsc, link, INST_AL, rm);
5062
5063   return 0;
5064 }
5065
5066
5067 /* Copy/cleanup arithmetic/logic instruction with immediate RHS.  */
5068
5069 static void
5070 cleanup_alu_imm (struct gdbarch *gdbarch,
5071                  struct regcache *regs, struct displaced_step_closure *dsc)
5072 {
5073   ULONGEST rd_val = displaced_read_reg (regs, dsc, 0);
5074   displaced_write_reg (regs, dsc, 0, dsc->tmp[0], CANNOT_WRITE_PC);
5075   displaced_write_reg (regs, dsc, 1, dsc->tmp[1], CANNOT_WRITE_PC);
5076   displaced_write_reg (regs, dsc, dsc->rd, rd_val, ALU_WRITE_PC);
5077 }
5078
5079 static int
5080 arm_copy_alu_imm (struct gdbarch *gdbarch, uint32_t insn, struct regcache *regs,
5081                   struct displaced_step_closure *dsc)
5082 {
5083   unsigned int rn = bits (insn, 16, 19);
5084   unsigned int rd = bits (insn, 12, 15);
5085   unsigned int op = bits (insn, 21, 24);
5086   int is_mov = (op == 0xd);
5087   ULONGEST rd_val, rn_val;
5088
5089   if (!insn_references_pc (insn, 0x000ff000ul))
5090     return arm_copy_unmodified (gdbarch, insn, "ALU immediate", dsc);
5091
5092   if (debug_displaced)
5093     fprintf_unfiltered (gdb_stdlog, "displaced: copying immediate %s insn "
5094                         "%.8lx\n", is_mov ? "move" : "ALU",
5095                         (unsigned long) insn);
5096
5097   /* Instruction is of form:
5098
5099      <op><cond> rd, [rn,] #imm
5100
5101      Rewrite as:
5102
5103      Preparation: tmp1, tmp2 <- r0, r1;
5104                   r0, r1 <- rd, rn
5105      Insn: <op><cond> r0, r1, #imm
5106      Cleanup: rd <- r0; r0 <- tmp1; r1 <- tmp2
5107   */
5108
5109   dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
5110   dsc->tmp[1] = displaced_read_reg (regs, dsc, 1);
5111   rn_val = displaced_read_reg (regs, dsc, rn);
5112   rd_val = displaced_read_reg (regs, dsc, rd);
5113   displaced_write_reg (regs, dsc, 0, rd_val, CANNOT_WRITE_PC);
5114   displaced_write_reg (regs, dsc, 1, rn_val, CANNOT_WRITE_PC);
5115   dsc->rd = rd;
5116
5117   if (is_mov)
5118     dsc->modinsn[0] = insn & 0xfff00fff;
5119   else
5120     dsc->modinsn[0] = (insn & 0xfff00fff) | 0x10000;
5121
5122   dsc->cleanup = &cleanup_alu_imm;
5123
5124   return 0;
5125 }
5126
5127 static int
5128 thumb2_copy_alu_imm (struct gdbarch *gdbarch, uint16_t insn1,
5129                      uint16_t insn2, struct regcache *regs,
5130                      struct displaced_step_closure *dsc)
5131 {
5132   unsigned int op = bits (insn1, 5, 8);
5133   unsigned int rn, rm, rd;
5134   ULONGEST rd_val, rn_val;
5135
5136   rn = bits (insn1, 0, 3); /* Rn */
5137   rm = bits (insn2, 0, 3); /* Rm */
5138   rd = bits (insn2, 8, 11); /* Rd */
5139
5140   /* This routine is only called for instruction MOV.  */
5141   gdb_assert (op == 0x2 && rn == 0xf);
5142
5143   if (rm != ARM_PC_REGNUM && rd != ARM_PC_REGNUM)
5144     return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2, "ALU imm", dsc);
5145
5146   if (debug_displaced)
5147     fprintf_unfiltered (gdb_stdlog, "displaced: copying reg %s insn %.4x%.4x\n",
5148                         "ALU", insn1, insn2);
5149
5150   /* Instruction is of form:
5151
5152      <op><cond> rd, [rn,] #imm
5153
5154      Rewrite as:
5155
5156      Preparation: tmp1, tmp2 <- r0, r1;
5157                   r0, r1 <- rd, rn
5158      Insn: <op><cond> r0, r1, #imm
5159      Cleanup: rd <- r0; r0 <- tmp1; r1 <- tmp2
5160   */
5161
5162   dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
5163   dsc->tmp[1] = displaced_read_reg (regs, dsc, 1);
5164   rn_val = displaced_read_reg (regs, dsc, rn);
5165   rd_val = displaced_read_reg (regs, dsc, rd);
5166   displaced_write_reg (regs, dsc, 0, rd_val, CANNOT_WRITE_PC);
5167   displaced_write_reg (regs, dsc, 1, rn_val, CANNOT_WRITE_PC);
5168   dsc->rd = rd;
5169
5170   dsc->modinsn[0] = insn1;
5171   dsc->modinsn[1] = ((insn2 & 0xf0f0) | 0x1);
5172   dsc->numinsns = 2;
5173
5174   dsc->cleanup = &cleanup_alu_imm;
5175
5176   return 0;
5177 }
5178
5179 /* Copy/cleanup arithmetic/logic insns with register RHS.  */
5180
5181 static void
5182 cleanup_alu_reg (struct gdbarch *gdbarch,
5183                  struct regcache *regs, struct displaced_step_closure *dsc)
5184 {
5185   ULONGEST rd_val;
5186   int i;
5187
5188   rd_val = displaced_read_reg (regs, dsc, 0);
5189
5190   for (i = 0; i < 3; i++)
5191     displaced_write_reg (regs, dsc, i, dsc->tmp[i], CANNOT_WRITE_PC);
5192
5193   displaced_write_reg (regs, dsc, dsc->rd, rd_val, ALU_WRITE_PC);
5194 }
5195
5196 static void
5197 install_alu_reg (struct gdbarch *gdbarch, struct regcache *regs,
5198                  struct displaced_step_closure *dsc,
5199                  unsigned int rd, unsigned int rn, unsigned int rm)
5200 {
5201   ULONGEST rd_val, rn_val, rm_val;
5202
5203   /* Instruction is of form:
5204
5205      <op><cond> rd, [rn,] rm [, <shift>]
5206
5207      Rewrite as:
5208
5209      Preparation: tmp1, tmp2, tmp3 <- r0, r1, r2;
5210                   r0, r1, r2 <- rd, rn, rm
5211      Insn: <op><cond> r0, [r1,] r2 [, <shift>]
5212      Cleanup: rd <- r0; r0, r1, r2 <- tmp1, tmp2, tmp3
5213   */
5214
5215   dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
5216   dsc->tmp[1] = displaced_read_reg (regs, dsc, 1);
5217   dsc->tmp[2] = displaced_read_reg (regs, dsc, 2);
5218   rd_val = displaced_read_reg (regs, dsc, rd);
5219   rn_val = displaced_read_reg (regs, dsc, rn);
5220   rm_val = displaced_read_reg (regs, dsc, rm);
5221   displaced_write_reg (regs, dsc, 0, rd_val, CANNOT_WRITE_PC);
5222   displaced_write_reg (regs, dsc, 1, rn_val, CANNOT_WRITE_PC);
5223   displaced_write_reg (regs, dsc, 2, rm_val, CANNOT_WRITE_PC);
5224   dsc->rd = rd;
5225
5226   dsc->cleanup = &cleanup_alu_reg;
5227 }
5228
5229 static int
5230 arm_copy_alu_reg (struct gdbarch *gdbarch, uint32_t insn, struct regcache *regs,
5231                   struct displaced_step_closure *dsc)
5232 {
5233   unsigned int op = bits (insn, 21, 24);
5234   int is_mov = (op == 0xd);
5235
5236   if (!insn_references_pc (insn, 0x000ff00ful))
5237     return arm_copy_unmodified (gdbarch, insn, "ALU reg", dsc);
5238
5239   if (debug_displaced)
5240     fprintf_unfiltered (gdb_stdlog, "displaced: copying reg %s insn %.8lx\n",
5241                         is_mov ? "move" : "ALU", (unsigned long) insn);
5242
5243   if (is_mov)
5244     dsc->modinsn[0] = (insn & 0xfff00ff0) | 0x2;
5245   else
5246     dsc->modinsn[0] = (insn & 0xfff00ff0) | 0x10002;
5247
5248   install_alu_reg (gdbarch, regs, dsc, bits (insn, 12, 15), bits (insn, 16, 19),
5249                    bits (insn, 0, 3));
5250   return 0;
5251 }
5252
5253 static int
5254 thumb_copy_alu_reg (struct gdbarch *gdbarch, uint16_t insn,
5255                     struct regcache *regs,
5256                     struct displaced_step_closure *dsc)
5257 {
5258   unsigned rm, rd;
5259
5260   rm = bits (insn, 3, 6);
5261   rd = (bit (insn, 7) << 3) | bits (insn, 0, 2);
5262
5263   if (rd != ARM_PC_REGNUM && rm != ARM_PC_REGNUM)
5264     return thumb_copy_unmodified_16bit (gdbarch, insn, "ALU reg", dsc);
5265
5266   if (debug_displaced)
5267     fprintf_unfiltered (gdb_stdlog, "displaced: copying ALU reg insn %.4x\n",
5268                         (unsigned short) insn);
5269
5270   dsc->modinsn[0] = ((insn & 0xff00) | 0x10);
5271
5272   install_alu_reg (gdbarch, regs, dsc, rd, rd, rm);
5273
5274   return 0;
5275 }
5276
5277 /* Cleanup/copy arithmetic/logic insns with shifted register RHS.  */
5278
5279 static void
5280 cleanup_alu_shifted_reg (struct gdbarch *gdbarch,
5281                          struct regcache *regs,
5282                          struct displaced_step_closure *dsc)
5283 {
5284   ULONGEST rd_val = displaced_read_reg (regs, dsc, 0);
5285   int i;
5286
5287   for (i = 0; i < 4; i++)
5288     displaced_write_reg (regs, dsc, i, dsc->tmp[i], CANNOT_WRITE_PC);
5289
5290   displaced_write_reg (regs, dsc, dsc->rd, rd_val, ALU_WRITE_PC);
5291 }
5292
5293 static void
5294 install_alu_shifted_reg (struct gdbarch *gdbarch, struct regcache *regs,
5295                          struct displaced_step_closure *dsc,
5296                          unsigned int rd, unsigned int rn, unsigned int rm,
5297                          unsigned rs)
5298 {
5299   int i;
5300   ULONGEST rd_val, rn_val, rm_val, rs_val;
5301
5302   /* Instruction is of form:
5303
5304      <op><cond> rd, [rn,] rm, <shift> rs
5305
5306      Rewrite as:
5307
5308      Preparation: tmp1, tmp2, tmp3, tmp4 <- r0, r1, r2, r3
5309                   r0, r1, r2, r3 <- rd, rn, rm, rs
5310      Insn: <op><cond> r0, r1, r2, <shift> r3
5311      Cleanup: tmp5 <- r0
5312               r0, r1, r2, r3 <- tmp1, tmp2, tmp3, tmp4
5313               rd <- tmp5
5314   */
5315
5316   for (i = 0; i < 4; i++)
5317     dsc->tmp[i] = displaced_read_reg (regs, dsc, i);
5318
5319   rd_val = displaced_read_reg (regs, dsc, rd);
5320   rn_val = displaced_read_reg (regs, dsc, rn);
5321   rm_val = displaced_read_reg (regs, dsc, rm);
5322   rs_val = displaced_read_reg (regs, dsc, rs);
5323   displaced_write_reg (regs, dsc, 0, rd_val, CANNOT_WRITE_PC);
5324   displaced_write_reg (regs, dsc, 1, rn_val, CANNOT_WRITE_PC);
5325   displaced_write_reg (regs, dsc, 2, rm_val, CANNOT_WRITE_PC);
5326   displaced_write_reg (regs, dsc, 3, rs_val, CANNOT_WRITE_PC);
5327   dsc->rd = rd;
5328   dsc->cleanup = &cleanup_alu_shifted_reg;
5329 }
5330
5331 static int
5332 arm_copy_alu_shifted_reg (struct gdbarch *gdbarch, uint32_t insn,
5333                           struct regcache *regs,
5334                           struct displaced_step_closure *dsc)
5335 {
5336   unsigned int op = bits (insn, 21, 24);
5337   int is_mov = (op == 0xd);
5338   unsigned int rd, rn, rm, rs;
5339
5340   if (!insn_references_pc (insn, 0x000fff0ful))
5341     return arm_copy_unmodified (gdbarch, insn, "ALU shifted reg", dsc);
5342
5343   if (debug_displaced)
5344     fprintf_unfiltered (gdb_stdlog, "displaced: copying shifted reg %s insn "
5345                         "%.8lx\n", is_mov ? "move" : "ALU",
5346                         (unsigned long) insn);
5347
5348   rn = bits (insn, 16, 19);
5349   rm = bits (insn, 0, 3);
5350   rs = bits (insn, 8, 11);
5351   rd = bits (insn, 12, 15);
5352
5353   if (is_mov)
5354     dsc->modinsn[0] = (insn & 0xfff000f0) | 0x302;
5355   else
5356     dsc->modinsn[0] = (insn & 0xfff000f0) | 0x10302;
5357
5358   install_alu_shifted_reg (gdbarch, regs, dsc, rd, rn, rm, rs);
5359
5360   return 0;
5361 }
5362
5363 /* Clean up load instructions.  */
5364
5365 static void
5366 cleanup_load (struct gdbarch *gdbarch, struct regcache *regs,
5367               struct displaced_step_closure *dsc)
5368 {
5369   ULONGEST rt_val, rt_val2 = 0, rn_val;
5370
5371   rt_val = displaced_read_reg (regs, dsc, 0);
5372   if (dsc->u.ldst.xfersize == 8)
5373     rt_val2 = displaced_read_reg (regs, dsc, 1);
5374   rn_val = displaced_read_reg (regs, dsc, 2);
5375
5376   displaced_write_reg (regs, dsc, 0, dsc->tmp[0], CANNOT_WRITE_PC);
5377   if (dsc->u.ldst.xfersize > 4)
5378     displaced_write_reg (regs, dsc, 1, dsc->tmp[1], CANNOT_WRITE_PC);
5379   displaced_write_reg (regs, dsc, 2, dsc->tmp[2], CANNOT_WRITE_PC);
5380   if (!dsc->u.ldst.immed)
5381     displaced_write_reg (regs, dsc, 3, dsc->tmp[3], CANNOT_WRITE_PC);
5382
5383   /* Handle register writeback.  */
5384   if (dsc->u.ldst.writeback)
5385     displaced_write_reg (regs, dsc, dsc->u.ldst.rn, rn_val, CANNOT_WRITE_PC);
5386   /* Put result in right place.  */
5387   displaced_write_reg (regs, dsc, dsc->rd, rt_val, LOAD_WRITE_PC);
5388   if (dsc->u.ldst.xfersize == 8)
5389     displaced_write_reg (regs, dsc, dsc->rd + 1, rt_val2, LOAD_WRITE_PC);
5390 }
5391
5392 /* Clean up store instructions.  */
5393
5394 static void
5395 cleanup_store (struct gdbarch *gdbarch, struct regcache *regs,
5396                struct displaced_step_closure *dsc)
5397 {
5398   ULONGEST rn_val = displaced_read_reg (regs, dsc, 2);
5399
5400   displaced_write_reg (regs, dsc, 0, dsc->tmp[0], CANNOT_WRITE_PC);
5401   if (dsc->u.ldst.xfersize > 4)
5402     displaced_write_reg (regs, dsc, 1, dsc->tmp[1], CANNOT_WRITE_PC);
5403   displaced_write_reg (regs, dsc, 2, dsc->tmp[2], CANNOT_WRITE_PC);
5404   if (!dsc->u.ldst.immed)
5405     displaced_write_reg (regs, dsc, 3, dsc->tmp[3], CANNOT_WRITE_PC);
5406   if (!dsc->u.ldst.restore_r4)
5407     displaced_write_reg (regs, dsc, 4, dsc->tmp[4], CANNOT_WRITE_PC);
5408
5409   /* Writeback.  */
5410   if (dsc->u.ldst.writeback)
5411     displaced_write_reg (regs, dsc, dsc->u.ldst.rn, rn_val, CANNOT_WRITE_PC);
5412 }
5413
5414 /* Copy "extra" load/store instructions.  These are halfword/doubleword
5415    transfers, which have a different encoding to byte/word transfers.  */
5416
5417 static int
5418 arm_copy_extra_ld_st (struct gdbarch *gdbarch, uint32_t insn, int unprivileged,
5419                       struct regcache *regs, struct displaced_step_closure *dsc)
5420 {
5421   unsigned int op1 = bits (insn, 20, 24);
5422   unsigned int op2 = bits (insn, 5, 6);
5423   unsigned int rt = bits (insn, 12, 15);
5424   unsigned int rn = bits (insn, 16, 19);
5425   unsigned int rm = bits (insn, 0, 3);
5426   char load[12]     = {0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1};
5427   char bytesize[12] = {2, 2, 2, 2, 8, 1, 8, 1, 8, 2, 8, 2};
5428   int immed = (op1 & 0x4) != 0;
5429   int opcode;
5430   ULONGEST rt_val, rt_val2 = 0, rn_val, rm_val = 0;
5431
5432   if (!insn_references_pc (insn, 0x000ff00ful))
5433     return arm_copy_unmodified (gdbarch, insn, "extra load/store", dsc);
5434
5435   if (debug_displaced)
5436     fprintf_unfiltered (gdb_stdlog, "displaced: copying %sextra load/store "
5437                         "insn %.8lx\n", unprivileged ? "unprivileged " : "",
5438                         (unsigned long) insn);
5439
5440   opcode = ((op2 << 2) | (op1 & 0x1) | ((op1 & 0x4) >> 1)) - 4;
5441
5442   if (opcode < 0)
5443     internal_error (__FILE__, __LINE__,
5444                     _("copy_extra_ld_st: instruction decode error"));
5445
5446   dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
5447   dsc->tmp[1] = displaced_read_reg (regs, dsc, 1);
5448   dsc->tmp[2] = displaced_read_reg (regs, dsc, 2);
5449   if (!immed)
5450     dsc->tmp[3] = displaced_read_reg (regs, dsc, 3);
5451
5452   rt_val = displaced_read_reg (regs, dsc, rt);
5453   if (bytesize[opcode] == 8)
5454     rt_val2 = displaced_read_reg (regs, dsc, rt + 1);
5455   rn_val = displaced_read_reg (regs, dsc, rn);
5456   if (!immed)
5457     rm_val = displaced_read_reg (regs, dsc, rm);
5458
5459   displaced_write_reg (regs, dsc, 0, rt_val, CANNOT_WRITE_PC);
5460   if (bytesize[opcode] == 8)
5461     displaced_write_reg (regs, dsc, 1, rt_val2, CANNOT_WRITE_PC);
5462   displaced_write_reg (regs, dsc, 2, rn_val, CANNOT_WRITE_PC);
5463   if (!immed)
5464     displaced_write_reg (regs, dsc, 3, rm_val, CANNOT_WRITE_PC);
5465
5466   dsc->rd = rt;
5467   dsc->u.ldst.xfersize = bytesize[opcode];
5468   dsc->u.ldst.rn = rn;
5469   dsc->u.ldst.immed = immed;
5470   dsc->u.ldst.writeback = bit (insn, 24) == 0 || bit (insn, 21) != 0;
5471   dsc->u.ldst.restore_r4 = 0;
5472
5473   if (immed)
5474     /* {ldr,str}<width><cond> rt, [rt2,] [rn, #imm]
5475         ->
5476        {ldr,str}<width><cond> r0, [r1,] [r2, #imm].  */
5477     dsc->modinsn[0] = (insn & 0xfff00fff) | 0x20000;
5478   else
5479     /* {ldr,str}<width><cond> rt, [rt2,] [rn, +/-rm]
5480         ->
5481        {ldr,str}<width><cond> r0, [r1,] [r2, +/-r3].  */
5482     dsc->modinsn[0] = (insn & 0xfff00ff0) | 0x20003;
5483
5484   dsc->cleanup = load[opcode] ? &cleanup_load : &cleanup_store;
5485
5486   return 0;
5487 }
5488
5489 /* Copy byte/half word/word loads and stores.  */
5490
5491 static void
5492 install_load_store (struct gdbarch *gdbarch, struct regcache *regs,
5493                     struct displaced_step_closure *dsc, int load,
5494                     int immed, int writeback, int size, int usermode,
5495                     int rt, int rm, int rn)
5496 {
5497   ULONGEST rt_val, rn_val, rm_val = 0;
5498
5499   dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
5500   dsc->tmp[2] = displaced_read_reg (regs, dsc, 2);
5501   if (!immed)
5502     dsc->tmp[3] = displaced_read_reg (regs, dsc, 3);
5503   if (!load)
5504     dsc->tmp[4] = displaced_read_reg (regs, dsc, 4);
5505
5506   rt_val = displaced_read_reg (regs, dsc, rt);
5507   rn_val = displaced_read_reg (regs, dsc, rn);
5508   if (!immed)
5509     rm_val = displaced_read_reg (regs, dsc, rm);
5510
5511   displaced_write_reg (regs, dsc, 0, rt_val, CANNOT_WRITE_PC);
5512   displaced_write_reg (regs, dsc, 2, rn_val, CANNOT_WRITE_PC);
5513   if (!immed)
5514     displaced_write_reg (regs, dsc, 3, rm_val, CANNOT_WRITE_PC);
5515   dsc->rd = rt;
5516   dsc->u.ldst.xfersize = size;
5517   dsc->u.ldst.rn = rn;
5518   dsc->u.ldst.immed = immed;
5519   dsc->u.ldst.writeback = writeback;
5520
5521   /* To write PC we can do:
5522
5523      Before this sequence of instructions:
5524      r0 is the PC value got from displaced_read_reg, so r0 = from + 8;
5525      r2 is the Rn value got from dispalced_read_reg.
5526
5527      Insn1: push {pc} Write address of STR instruction + offset on stack
5528      Insn2: pop  {r4} Read it back from stack, r4 = addr(Insn1) + offset
5529      Insn3: sub r4, r4, pc   r4 = addr(Insn1) + offset - pc
5530                                 = addr(Insn1) + offset - addr(Insn3) - 8
5531                                 = offset - 16
5532      Insn4: add r4, r4, #8   r4 = offset - 8
5533      Insn5: add r0, r0, r4   r0 = from + 8 + offset - 8
5534                                 = from + offset
5535      Insn6: str r0, [r2, #imm] (or str r0, [r2, r3])
5536
5537      Otherwise we don't know what value to write for PC, since the offset is
5538      architecture-dependent (sometimes PC+8, sometimes PC+12).  More details
5539      of this can be found in Section "Saving from r15" in
5540      http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dui0204g/Cihbjifh.html */
5541
5542   dsc->cleanup = load ? &cleanup_load : &cleanup_store;
5543 }
5544
5545
5546 static int
5547 thumb2_copy_load_literal (struct gdbarch *gdbarch, uint16_t insn1,
5548                           uint16_t insn2, struct regcache *regs,
5549                           struct displaced_step_closure *dsc, int size)
5550 {
5551   unsigned int u_bit = bit (insn1, 7);
5552   unsigned int rt = bits (insn2, 12, 15);
5553   int imm12 = bits (insn2, 0, 11);
5554   ULONGEST pc_val;
5555
5556   if (debug_displaced)
5557     fprintf_unfiltered (gdb_stdlog,
5558                         "displaced: copying ldr pc (0x%x) R%d %c imm12 %.4x\n",
5559                         (unsigned int) dsc->insn_addr, rt, u_bit ? '+' : '-',
5560                         imm12);
5561
5562   if (!u_bit)
5563     imm12 = -1 * imm12;
5564
5565   /* Rewrite instruction LDR Rt imm12 into:
5566
5567      Prepare: tmp[0] <- r0, tmp[1] <- r2, tmp[2] <- r3, r2 <- pc, r3 <- imm12
5568
5569      LDR R0, R2, R3,
5570
5571      Cleanup: rt <- r0, r0 <- tmp[0], r2 <- tmp[1], r3 <- tmp[2].  */
5572
5573
5574   dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
5575   dsc->tmp[2] = displaced_read_reg (regs, dsc, 2);
5576   dsc->tmp[3] = displaced_read_reg (regs, dsc, 3);
5577
5578   pc_val = displaced_read_reg (regs, dsc, ARM_PC_REGNUM);
5579
5580   pc_val = pc_val & 0xfffffffc;
5581
5582   displaced_write_reg (regs, dsc, 2, pc_val, CANNOT_WRITE_PC);
5583   displaced_write_reg (regs, dsc, 3, imm12, CANNOT_WRITE_PC);
5584
5585   dsc->rd = rt;
5586
5587   dsc->u.ldst.xfersize = size;
5588   dsc->u.ldst.immed = 0;
5589   dsc->u.ldst.writeback = 0;
5590   dsc->u.ldst.restore_r4 = 0;
5591
5592   /* LDR R0, R2, R3 */
5593   dsc->modinsn[0] = 0xf852;
5594   dsc->modinsn[1] = 0x3;
5595   dsc->numinsns = 2;
5596
5597   dsc->cleanup = &cleanup_load;
5598
5599   return 0;
5600 }
5601
5602 static int
5603 thumb2_copy_load_reg_imm (struct gdbarch *gdbarch, uint16_t insn1,
5604                           uint16_t insn2, struct regcache *regs,
5605                           struct displaced_step_closure *dsc,
5606                           int writeback, int immed)
5607 {
5608   unsigned int rt = bits (insn2, 12, 15);
5609   unsigned int rn = bits (insn1, 0, 3);
5610   unsigned int rm = bits (insn2, 0, 3);  /* Only valid if !immed.  */
5611   /* In LDR (register), there is also a register Rm, which is not allowed to
5612      be PC, so we don't have to check it.  */
5613
5614   if (rt != ARM_PC_REGNUM && rn != ARM_PC_REGNUM)
5615     return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2, "load",
5616                                         dsc);
5617
5618   if (debug_displaced)
5619     fprintf_unfiltered (gdb_stdlog,
5620                         "displaced: copying ldr r%d [r%d] insn %.4x%.4x\n",
5621                          rt, rn, insn1, insn2);
5622
5623   install_load_store (gdbarch, regs, dsc, 1, immed, writeback, 4,
5624                       0, rt, rm, rn);
5625
5626   dsc->u.ldst.restore_r4 = 0;
5627
5628   if (immed)
5629     /* ldr[b]<cond> rt, [rn, #imm], etc.
5630        ->
5631        ldr[b]<cond> r0, [r2, #imm].  */
5632     {
5633       dsc->modinsn[0] = (insn1 & 0xfff0) | 0x2;
5634       dsc->modinsn[1] = insn2 & 0x0fff;
5635     }
5636   else
5637     /* ldr[b]<cond> rt, [rn, rm], etc.
5638        ->
5639        ldr[b]<cond> r0, [r2, r3].  */
5640     {
5641       dsc->modinsn[0] = (insn1 & 0xfff0) | 0x2;
5642       dsc->modinsn[1] = (insn2 & 0x0ff0) | 0x3;
5643     }
5644
5645   dsc->numinsns = 2;
5646
5647   return 0;
5648 }
5649
5650
5651 static int
5652 arm_copy_ldr_str_ldrb_strb (struct gdbarch *gdbarch, uint32_t insn,
5653                             struct regcache *regs,
5654                             struct displaced_step_closure *dsc,
5655                             int load, int size, int usermode)
5656 {
5657   int immed = !bit (insn, 25);
5658   int writeback = (bit (insn, 24) == 0 || bit (insn, 21) != 0);
5659   unsigned int rt = bits (insn, 12, 15);
5660   unsigned int rn = bits (insn, 16, 19);
5661   unsigned int rm = bits (insn, 0, 3);  /* Only valid if !immed.  */
5662
5663   if (!insn_references_pc (insn, 0x000ff00ful))
5664     return arm_copy_unmodified (gdbarch, insn, "load/store", dsc);
5665
5666   if (debug_displaced)
5667     fprintf_unfiltered (gdb_stdlog,
5668                         "displaced: copying %s%s r%d [r%d] insn %.8lx\n",
5669                         load ? (size == 1 ? "ldrb" : "ldr")
5670                              : (size == 1 ? "strb" : "str"), usermode ? "t" : "",
5671                         rt, rn,
5672                         (unsigned long) insn);
5673
5674   install_load_store (gdbarch, regs, dsc, load, immed, writeback, size,
5675                       usermode, rt, rm, rn);
5676
5677   if (load || rt != ARM_PC_REGNUM)
5678     {
5679       dsc->u.ldst.restore_r4 = 0;
5680
5681       if (immed)
5682         /* {ldr,str}[b]<cond> rt, [rn, #imm], etc.
5683            ->
5684            {ldr,str}[b]<cond> r0, [r2, #imm].  */
5685         dsc->modinsn[0] = (insn & 0xfff00fff) | 0x20000;
5686       else
5687         /* {ldr,str}[b]<cond> rt, [rn, rm], etc.
5688            ->
5689            {ldr,str}[b]<cond> r0, [r2, r3].  */
5690         dsc->modinsn[0] = (insn & 0xfff00ff0) | 0x20003;
5691     }
5692   else
5693     {
5694       /* We need to use r4 as scratch.  Make sure it's restored afterwards.  */
5695       dsc->u.ldst.restore_r4 = 1;
5696       dsc->modinsn[0] = 0xe92d8000;  /* push {pc} */
5697       dsc->modinsn[1] = 0xe8bd0010;  /* pop  {r4} */
5698       dsc->modinsn[2] = 0xe044400f;  /* sub r4, r4, pc.  */
5699       dsc->modinsn[3] = 0xe2844008;  /* add r4, r4, #8.  */
5700       dsc->modinsn[4] = 0xe0800004;  /* add r0, r0, r4.  */
5701
5702       /* As above.  */
5703       if (immed)
5704         dsc->modinsn[5] = (insn & 0xfff00fff) | 0x20000;
5705       else
5706         dsc->modinsn[5] = (insn & 0xfff00ff0) | 0x20003;
5707
5708       dsc->numinsns = 6;
5709     }
5710
5711   dsc->cleanup = load ? &cleanup_load : &cleanup_store;
5712
5713   return 0;
5714 }
5715
5716 /* Cleanup LDM instructions with fully-populated register list.  This is an
5717    unfortunate corner case: it's impossible to implement correctly by modifying
5718    the instruction.  The issue is as follows: we have an instruction,
5719
5720    ldm rN, {r0-r15}
5721
5722    which we must rewrite to avoid loading PC.  A possible solution would be to
5723    do the load in two halves, something like (with suitable cleanup
5724    afterwards):
5725
5726    mov r8, rN
5727    ldm[id][ab] r8!, {r0-r7}
5728    str r7, <temp>
5729    ldm[id][ab] r8, {r7-r14}
5730    <bkpt>
5731
5732    but at present there's no suitable place for <temp>, since the scratch space
5733    is overwritten before the cleanup routine is called.  For now, we simply
5734    emulate the instruction.  */
5735
5736 static void
5737 cleanup_block_load_all (struct gdbarch *gdbarch, struct regcache *regs,
5738                         struct displaced_step_closure *dsc)
5739 {
5740   int inc = dsc->u.block.increment;
5741   int bump_before = dsc->u.block.before ? (inc ? 4 : -4) : 0;
5742   int bump_after = dsc->u.block.before ? 0 : (inc ? 4 : -4);
5743   uint32_t regmask = dsc->u.block.regmask;
5744   int regno = inc ? 0 : 15;
5745   CORE_ADDR xfer_addr = dsc->u.block.xfer_addr;
5746   int exception_return = dsc->u.block.load && dsc->u.block.user
5747                          && (regmask & 0x8000) != 0;
5748   uint32_t status = displaced_read_reg (regs, dsc, ARM_PS_REGNUM);
5749   int do_transfer = condition_true (dsc->u.block.cond, status);
5750   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
5751
5752   if (!do_transfer)
5753     return;
5754
5755   /* If the instruction is ldm rN, {...pc}^, I don't think there's anything
5756      sensible we can do here.  Complain loudly.  */
5757   if (exception_return)
5758     error (_("Cannot single-step exception return"));
5759
5760   /* We don't handle any stores here for now.  */
5761   gdb_assert (dsc->u.block.load != 0);
5762
5763   if (debug_displaced)
5764     fprintf_unfiltered (gdb_stdlog, "displaced: emulating block transfer: "
5765                         "%s %s %s\n", dsc->u.block.load ? "ldm" : "stm",
5766                         dsc->u.block.increment ? "inc" : "dec",
5767                         dsc->u.block.before ? "before" : "after");
5768
5769   while (regmask)
5770     {
5771       uint32_t memword;
5772
5773       if (inc)
5774         while (regno <= ARM_PC_REGNUM && (regmask & (1 << regno)) == 0)
5775           regno++;
5776       else
5777         while (regno >= 0 && (regmask & (1 << regno)) == 0)
5778           regno--;
5779
5780       xfer_addr += bump_before;
5781
5782       memword = read_memory_unsigned_integer (xfer_addr, 4, byte_order);
5783       displaced_write_reg (regs, dsc, regno, memword, LOAD_WRITE_PC);
5784
5785       xfer_addr += bump_after;
5786
5787       regmask &= ~(1 << regno);
5788     }
5789
5790   if (dsc->u.block.writeback)
5791     displaced_write_reg (regs, dsc, dsc->u.block.rn, xfer_addr,
5792                          CANNOT_WRITE_PC);
5793 }
5794
5795 /* Clean up an STM which included the PC in the register list.  */
5796
5797 static void
5798 cleanup_block_store_pc (struct gdbarch *gdbarch, struct regcache *regs,
5799                         struct displaced_step_closure *dsc)
5800 {
5801   uint32_t status = displaced_read_reg (regs, dsc, ARM_PS_REGNUM);
5802   int store_executed = condition_true (dsc->u.block.cond, status);
5803   CORE_ADDR pc_stored_at, transferred_regs = bitcount (dsc->u.block.regmask);
5804   CORE_ADDR stm_insn_addr;
5805   uint32_t pc_val;
5806   long offset;
5807   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
5808
5809   /* If condition code fails, there's nothing else to do.  */
5810   if (!store_executed)
5811     return;
5812
5813   if (dsc->u.block.increment)
5814     {
5815       pc_stored_at = dsc->u.block.xfer_addr + 4 * transferred_regs;
5816
5817       if (dsc->u.block.before)
5818          pc_stored_at += 4;
5819     }
5820   else
5821     {
5822       pc_stored_at = dsc->u.block.xfer_addr;
5823
5824       if (dsc->u.block.before)
5825          pc_stored_at -= 4;
5826     }
5827
5828   pc_val = read_memory_unsigned_integer (pc_stored_at, 4, byte_order);
5829   stm_insn_addr = dsc->scratch_base;
5830   offset = pc_val - stm_insn_addr;
5831
5832   if (debug_displaced)
5833     fprintf_unfiltered (gdb_stdlog, "displaced: detected PC offset %.8lx for "
5834                         "STM instruction\n", offset);
5835
5836   /* Rewrite the stored PC to the proper value for the non-displaced original
5837      instruction.  */
5838   write_memory_unsigned_integer (pc_stored_at, 4, byte_order,
5839                                  dsc->insn_addr + offset);
5840 }
5841
5842 /* Clean up an LDM which includes the PC in the register list.  We clumped all
5843    the registers in the transferred list into a contiguous range r0...rX (to
5844    avoid loading PC directly and losing control of the debugged program), so we
5845    must undo that here.  */
5846
5847 static void
5848 cleanup_block_load_pc (struct gdbarch *gdbarch,
5849                        struct regcache *regs,
5850                        struct displaced_step_closure *dsc)
5851 {
5852   uint32_t status = displaced_read_reg (regs, dsc, ARM_PS_REGNUM);
5853   int load_executed = condition_true (dsc->u.block.cond, status);
5854   unsigned int mask = dsc->u.block.regmask, write_reg = ARM_PC_REGNUM;
5855   unsigned int regs_loaded = bitcount (mask);
5856   unsigned int num_to_shuffle = regs_loaded, clobbered;
5857
5858   /* The method employed here will fail if the register list is fully populated
5859      (we need to avoid loading PC directly).  */
5860   gdb_assert (num_to_shuffle < 16);
5861
5862   if (!load_executed)
5863     return;
5864
5865   clobbered = (1 << num_to_shuffle) - 1;
5866
5867   while (num_to_shuffle > 0)
5868     {
5869       if ((mask & (1 << write_reg)) != 0)
5870         {
5871           unsigned int read_reg = num_to_shuffle - 1;
5872
5873           if (read_reg != write_reg)
5874             {
5875               ULONGEST rval = displaced_read_reg (regs, dsc, read_reg);
5876               displaced_write_reg (regs, dsc, write_reg, rval, LOAD_WRITE_PC);
5877               if (debug_displaced)
5878                 fprintf_unfiltered (gdb_stdlog, _("displaced: LDM: move "
5879                                     "loaded register r%d to r%d\n"), read_reg,
5880                                     write_reg);
5881             }
5882           else if (debug_displaced)
5883             fprintf_unfiltered (gdb_stdlog, _("displaced: LDM: register "
5884                                 "r%d already in the right place\n"),
5885                                 write_reg);
5886
5887           clobbered &= ~(1 << write_reg);
5888
5889           num_to_shuffle--;
5890         }
5891
5892       write_reg--;
5893     }
5894
5895   /* Restore any registers we scribbled over.  */
5896   for (write_reg = 0; clobbered != 0; write_reg++)
5897     {
5898       if ((clobbered & (1 << write_reg)) != 0)
5899         {
5900           displaced_write_reg (regs, dsc, write_reg, dsc->tmp[write_reg],
5901                                CANNOT_WRITE_PC);
5902           if (debug_displaced)
5903             fprintf_unfiltered (gdb_stdlog, _("displaced: LDM: restored "
5904                                 "clobbered register r%d\n"), write_reg);
5905           clobbered &= ~(1 << write_reg);
5906         }
5907     }
5908
5909   /* Perform register writeback manually.  */
5910   if (dsc->u.block.writeback)
5911     {
5912       ULONGEST new_rn_val = dsc->u.block.xfer_addr;
5913
5914       if (dsc->u.block.increment)
5915         new_rn_val += regs_loaded * 4;
5916       else
5917         new_rn_val -= regs_loaded * 4;
5918
5919       displaced_write_reg (regs, dsc, dsc->u.block.rn, new_rn_val,
5920                            CANNOT_WRITE_PC);
5921     }
5922 }
5923
5924 /* Handle ldm/stm, apart from some tricky cases which are unlikely to occur
5925    in user-level code (in particular exception return, ldm rn, {...pc}^).  */
5926
5927 static int
5928 arm_copy_block_xfer (struct gdbarch *gdbarch, uint32_t insn,
5929                      struct regcache *regs,
5930                      struct displaced_step_closure *dsc)
5931 {
5932   int load = bit (insn, 20);
5933   int user = bit (insn, 22);
5934   int increment = bit (insn, 23);
5935   int before = bit (insn, 24);
5936   int writeback = bit (insn, 21);
5937   int rn = bits (insn, 16, 19);
5938
5939   /* Block transfers which don't mention PC can be run directly
5940      out-of-line.  */
5941   if (rn != ARM_PC_REGNUM && (insn & 0x8000) == 0)
5942     return arm_copy_unmodified (gdbarch, insn, "ldm/stm", dsc);
5943
5944   if (rn == ARM_PC_REGNUM)
5945     {
5946       warning (_("displaced: Unpredictable LDM or STM with "
5947                  "base register r15"));
5948       return arm_copy_unmodified (gdbarch, insn, "unpredictable ldm/stm", dsc);
5949     }
5950
5951   if (debug_displaced)
5952     fprintf_unfiltered (gdb_stdlog, "displaced: copying block transfer insn "
5953                         "%.8lx\n", (unsigned long) insn);
5954
5955   dsc->u.block.xfer_addr = displaced_read_reg (regs, dsc, rn);
5956   dsc->u.block.rn = rn;
5957
5958   dsc->u.block.load = load;
5959   dsc->u.block.user = user;
5960   dsc->u.block.increment = increment;
5961   dsc->u.block.before = before;
5962   dsc->u.block.writeback = writeback;
5963   dsc->u.block.cond = bits (insn, 28, 31);
5964
5965   dsc->u.block.regmask = insn & 0xffff;
5966
5967   if (load)
5968     {
5969       if ((insn & 0xffff) == 0xffff)
5970         {
5971           /* LDM with a fully-populated register list.  This case is
5972              particularly tricky.  Implement for now by fully emulating the
5973              instruction (which might not behave perfectly in all cases, but
5974              these instructions should be rare enough for that not to matter
5975              too much).  */
5976           dsc->modinsn[0] = ARM_NOP;
5977
5978           dsc->cleanup = &cleanup_block_load_all;
5979         }
5980       else
5981         {
5982           /* LDM of a list of registers which includes PC.  Implement by
5983              rewriting the list of registers to be transferred into a
5984              contiguous chunk r0...rX before doing the transfer, then shuffling
5985              registers into the correct places in the cleanup routine.  */
5986           unsigned int regmask = insn & 0xffff;
5987           unsigned int num_in_list = bitcount (regmask), new_regmask;
5988           unsigned int i;
5989
5990           for (i = 0; i < num_in_list; i++)
5991             dsc->tmp[i] = displaced_read_reg (regs, dsc, i);
5992
5993           /* Writeback makes things complicated.  We need to avoid clobbering
5994              the base register with one of the registers in our modified
5995              register list, but just using a different register can't work in
5996              all cases, e.g.:
5997
5998                ldm r14!, {r0-r13,pc}
5999
6000              which would need to be rewritten as:
6001
6002                ldm rN!, {r0-r14}
6003
6004              but that can't work, because there's no free register for N.
6005
6006              Solve this by turning off the writeback bit, and emulating
6007              writeback manually in the cleanup routine.  */
6008
6009           if (writeback)
6010             insn &= ~(1 << 21);
6011
6012           new_regmask = (1 << num_in_list) - 1;
6013
6014           if (debug_displaced)
6015             fprintf_unfiltered (gdb_stdlog, _("displaced: LDM r%d%s, "
6016                                 "{..., pc}: original reg list %.4x, modified "
6017                                 "list %.4x\n"), rn, writeback ? "!" : "",
6018                                 (int) insn & 0xffff, new_regmask);
6019
6020           dsc->modinsn[0] = (insn & ~0xffff) | (new_regmask & 0xffff);
6021
6022           dsc->cleanup = &cleanup_block_load_pc;
6023         }
6024     }
6025   else
6026     {
6027       /* STM of a list of registers which includes PC.  Run the instruction
6028          as-is, but out of line: this will store the wrong value for the PC,
6029          so we must manually fix up the memory in the cleanup routine.
6030          Doing things this way has the advantage that we can auto-detect
6031          the offset of the PC write (which is architecture-dependent) in
6032          the cleanup routine.  */
6033       dsc->modinsn[0] = insn;
6034
6035       dsc->cleanup = &cleanup_block_store_pc;
6036     }
6037
6038   return 0;
6039 }
6040
6041 static int
6042 thumb2_copy_block_xfer (struct gdbarch *gdbarch, uint16_t insn1, uint16_t insn2,
6043                         struct regcache *regs,
6044                         struct displaced_step_closure *dsc)
6045 {
6046   int rn = bits (insn1, 0, 3);
6047   int load = bit (insn1, 4);
6048   int writeback = bit (insn1, 5);
6049
6050   /* Block transfers which don't mention PC can be run directly
6051      out-of-line.  */
6052   if (rn != ARM_PC_REGNUM && (insn2 & 0x8000) == 0)
6053     return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2, "ldm/stm", dsc);
6054
6055   if (rn == ARM_PC_REGNUM)
6056     {
6057       warning (_("displaced: Unpredictable LDM or STM with "
6058                  "base register r15"));
6059       return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
6060                                           "unpredictable ldm/stm", dsc);
6061     }
6062
6063   if (debug_displaced)
6064     fprintf_unfiltered (gdb_stdlog, "displaced: copying block transfer insn "
6065                         "%.4x%.4x\n", insn1, insn2);
6066
6067   /* Clear bit 13, since it should be always zero.  */
6068   dsc->u.block.regmask = (insn2 & 0xdfff);
6069   dsc->u.block.rn = rn;
6070
6071   dsc->u.block.load = load;
6072   dsc->u.block.user = 0;
6073   dsc->u.block.increment = bit (insn1, 7);
6074   dsc->u.block.before = bit (insn1, 8);
6075   dsc->u.block.writeback = writeback;
6076   dsc->u.block.cond = INST_AL;
6077   dsc->u.block.xfer_addr = displaced_read_reg (regs, dsc, rn);
6078
6079   if (load)
6080     {
6081       if (dsc->u.block.regmask == 0xffff)
6082         {
6083           /* This branch is impossible to happen.  */
6084           gdb_assert (0);
6085         }
6086       else
6087         {
6088           unsigned int regmask = dsc->u.block.regmask;
6089           unsigned int num_in_list = bitcount (regmask), new_regmask;
6090           unsigned int i;
6091
6092           for (i = 0; i < num_in_list; i++)
6093             dsc->tmp[i] = displaced_read_reg (regs, dsc, i);
6094
6095           if (writeback)
6096             insn1 &= ~(1 << 5);
6097
6098           new_regmask = (1 << num_in_list) - 1;
6099
6100           if (debug_displaced)
6101             fprintf_unfiltered (gdb_stdlog, _("displaced: LDM r%d%s, "
6102                                 "{..., pc}: original reg list %.4x, modified "
6103                                 "list %.4x\n"), rn, writeback ? "!" : "",
6104                                 (int) dsc->u.block.regmask, new_regmask);
6105
6106           dsc->modinsn[0] = insn1;
6107           dsc->modinsn[1] = (new_regmask & 0xffff);
6108           dsc->numinsns = 2;
6109
6110           dsc->cleanup = &cleanup_block_load_pc;
6111         }
6112     }
6113   else
6114     {
6115       dsc->modinsn[0] = insn1;
6116       dsc->modinsn[1] = insn2;
6117       dsc->numinsns = 2;
6118       dsc->cleanup = &cleanup_block_store_pc;
6119     }
6120   return 0;
6121 }
6122
6123 /* Wrapper over read_memory_unsigned_integer for use in arm_get_next_pcs.
6124  This is used to avoid a dependency on BFD's bfd_endian enum.  */
6125
6126 ULONGEST
6127 arm_get_next_pcs_read_memory_unsigned_integer (CORE_ADDR memaddr, int len,
6128                                                int byte_order)
6129 {
6130   return read_memory_unsigned_integer (memaddr, len,
6131                                        (enum bfd_endian) byte_order);
6132 }
6133
6134 /* Wrapper over gdbarch_addr_bits_remove for use in arm_get_next_pcs.  */
6135
6136 CORE_ADDR
6137 arm_get_next_pcs_addr_bits_remove (struct arm_get_next_pcs *self,
6138                                    CORE_ADDR val)
6139 {
6140   return gdbarch_addr_bits_remove (get_regcache_arch (self->regcache), val);
6141 }
6142
6143 /* Wrapper over syscall_next_pc for use in get_next_pcs.  */
6144
6145 static CORE_ADDR
6146 arm_get_next_pcs_syscall_next_pc (struct arm_get_next_pcs *self)
6147 {
6148   return 0;
6149 }
6150
6151 /* Wrapper over arm_is_thumb for use in arm_get_next_pcs.  */
6152
6153 int
6154 arm_get_next_pcs_is_thumb (struct arm_get_next_pcs *self)
6155 {
6156   return arm_is_thumb (self->regcache);
6157 }
6158
6159 /* single_step() is called just before we want to resume the inferior,
6160    if we want to single-step it but there is no hardware or kernel
6161    single-step support.  We find the target of the coming instructions
6162    and breakpoint them.  */
6163
6164 int
6165 arm_software_single_step (struct frame_info *frame)
6166 {
6167   struct regcache *regcache = get_current_regcache ();
6168   struct gdbarch *gdbarch = get_regcache_arch (regcache);
6169   struct address_space *aspace = get_regcache_aspace (regcache);
6170   struct arm_get_next_pcs next_pcs_ctx;
6171   CORE_ADDR pc;
6172   int i;
6173   VEC (CORE_ADDR) *next_pcs = NULL;
6174   struct cleanup *old_chain = make_cleanup (VEC_cleanup (CORE_ADDR), &next_pcs);
6175
6176   arm_get_next_pcs_ctor (&next_pcs_ctx,
6177                          &arm_get_next_pcs_ops,
6178                          gdbarch_byte_order (gdbarch),
6179                          gdbarch_byte_order_for_code (gdbarch),
6180                          0,
6181                          regcache);
6182
6183   next_pcs = arm_get_next_pcs (&next_pcs_ctx);
6184
6185   for (i = 0; VEC_iterate (CORE_ADDR, next_pcs, i, pc); i++)
6186     arm_insert_single_step_breakpoint (gdbarch, aspace, pc);
6187
6188   do_cleanups (old_chain);
6189
6190   return 1;
6191 }
6192
6193 /* Cleanup/copy SVC (SWI) instructions.  These two functions are overridden
6194    for Linux, where some SVC instructions must be treated specially.  */
6195
6196 static void
6197 cleanup_svc (struct gdbarch *gdbarch, struct regcache *regs,
6198              struct displaced_step_closure *dsc)
6199 {
6200   CORE_ADDR resume_addr = dsc->insn_addr + dsc->insn_size;
6201
6202   if (debug_displaced)
6203     fprintf_unfiltered (gdb_stdlog, "displaced: cleanup for svc, resume at "
6204                         "%.8lx\n", (unsigned long) resume_addr);
6205
6206   displaced_write_reg (regs, dsc, ARM_PC_REGNUM, resume_addr, BRANCH_WRITE_PC);
6207 }
6208
6209
6210 /* Common copy routine for svc instruciton.  */
6211
6212 static int
6213 install_svc (struct gdbarch *gdbarch, struct regcache *regs,
6214              struct displaced_step_closure *dsc)
6215 {
6216   /* Preparation: none.
6217      Insn: unmodified svc.
6218      Cleanup: pc <- insn_addr + insn_size.  */
6219
6220   /* Pretend we wrote to the PC, so cleanup doesn't set PC to the next
6221      instruction.  */
6222   dsc->wrote_to_pc = 1;
6223
6224   /* Allow OS-specific code to override SVC handling.  */
6225   if (dsc->u.svc.copy_svc_os)
6226     return dsc->u.svc.copy_svc_os (gdbarch, regs, dsc);
6227   else
6228     {
6229       dsc->cleanup = &cleanup_svc;
6230       return 0;
6231     }
6232 }
6233
6234 static int
6235 arm_copy_svc (struct gdbarch *gdbarch, uint32_t insn,
6236               struct regcache *regs, struct displaced_step_closure *dsc)
6237 {
6238
6239   if (debug_displaced)
6240     fprintf_unfiltered (gdb_stdlog, "displaced: copying svc insn %.8lx\n",
6241                         (unsigned long) insn);
6242
6243   dsc->modinsn[0] = insn;
6244
6245   return install_svc (gdbarch, regs, dsc);
6246 }
6247
6248 static int
6249 thumb_copy_svc (struct gdbarch *gdbarch, uint16_t insn,
6250                 struct regcache *regs, struct displaced_step_closure *dsc)
6251 {
6252
6253   if (debug_displaced)
6254     fprintf_unfiltered (gdb_stdlog, "displaced: copying svc insn %.4x\n",
6255                         insn);
6256
6257   dsc->modinsn[0] = insn;
6258
6259   return install_svc (gdbarch, regs, dsc);
6260 }
6261
6262 /* Copy undefined instructions.  */
6263
6264 static int
6265 arm_copy_undef (struct gdbarch *gdbarch, uint32_t insn,
6266                 struct displaced_step_closure *dsc)
6267 {
6268   if (debug_displaced)
6269     fprintf_unfiltered (gdb_stdlog,
6270                         "displaced: copying undefined insn %.8lx\n",
6271                         (unsigned long) insn);
6272
6273   dsc->modinsn[0] = insn;
6274
6275   return 0;
6276 }
6277
6278 static int
6279 thumb_32bit_copy_undef (struct gdbarch *gdbarch, uint16_t insn1, uint16_t insn2,
6280                        struct displaced_step_closure *dsc)
6281 {
6282
6283   if (debug_displaced)
6284     fprintf_unfiltered (gdb_stdlog, "displaced: copying undefined insn "
6285                        "%.4x %.4x\n", (unsigned short) insn1,
6286                        (unsigned short) insn2);
6287
6288   dsc->modinsn[0] = insn1;
6289   dsc->modinsn[1] = insn2;
6290   dsc->numinsns = 2;
6291
6292   return 0;
6293 }
6294
6295 /* Copy unpredictable instructions.  */
6296
6297 static int
6298 arm_copy_unpred (struct gdbarch *gdbarch, uint32_t insn,
6299                  struct displaced_step_closure *dsc)
6300 {
6301   if (debug_displaced)
6302     fprintf_unfiltered (gdb_stdlog, "displaced: copying unpredictable insn "
6303                         "%.8lx\n", (unsigned long) insn);
6304
6305   dsc->modinsn[0] = insn;
6306
6307   return 0;
6308 }
6309
6310 /* The decode_* functions are instruction decoding helpers.  They mostly follow
6311    the presentation in the ARM ARM.  */
6312
6313 static int
6314 arm_decode_misc_memhint_neon (struct gdbarch *gdbarch, uint32_t insn,
6315                               struct regcache *regs,
6316                               struct displaced_step_closure *dsc)
6317 {
6318   unsigned int op1 = bits (insn, 20, 26), op2 = bits (insn, 4, 7);
6319   unsigned int rn = bits (insn, 16, 19);
6320
6321   if (op1 == 0x10 && (op2 & 0x2) == 0x0 && (rn & 0xe) == 0x0)
6322     return arm_copy_unmodified (gdbarch, insn, "cps", dsc);
6323   else if (op1 == 0x10 && op2 == 0x0 && (rn & 0xe) == 0x1)
6324     return arm_copy_unmodified (gdbarch, insn, "setend", dsc);
6325   else if ((op1 & 0x60) == 0x20)
6326     return arm_copy_unmodified (gdbarch, insn, "neon dataproc", dsc);
6327   else if ((op1 & 0x71) == 0x40)
6328     return arm_copy_unmodified (gdbarch, insn, "neon elt/struct load/store",
6329                                 dsc);
6330   else if ((op1 & 0x77) == 0x41)
6331     return arm_copy_unmodified (gdbarch, insn, "unallocated mem hint", dsc);
6332   else if ((op1 & 0x77) == 0x45)
6333     return arm_copy_preload (gdbarch, insn, regs, dsc);  /* pli.  */
6334   else if ((op1 & 0x77) == 0x51)
6335     {
6336       if (rn != 0xf)
6337         return arm_copy_preload (gdbarch, insn, regs, dsc);  /* pld/pldw.  */
6338       else
6339         return arm_copy_unpred (gdbarch, insn, dsc);
6340     }
6341   else if ((op1 & 0x77) == 0x55)
6342     return arm_copy_preload (gdbarch, insn, regs, dsc);  /* pld/pldw.  */
6343   else if (op1 == 0x57)
6344     switch (op2)
6345       {
6346       case 0x1: return arm_copy_unmodified (gdbarch, insn, "clrex", dsc);
6347       case 0x4: return arm_copy_unmodified (gdbarch, insn, "dsb", dsc);
6348       case 0x5: return arm_copy_unmodified (gdbarch, insn, "dmb", dsc);
6349       case 0x6: return arm_copy_unmodified (gdbarch, insn, "isb", dsc);
6350       default: return arm_copy_unpred (gdbarch, insn, dsc);
6351       }
6352   else if ((op1 & 0x63) == 0x43)
6353     return arm_copy_unpred (gdbarch, insn, dsc);
6354   else if ((op2 & 0x1) == 0x0)
6355     switch (op1 & ~0x80)
6356       {
6357       case 0x61:
6358         return arm_copy_unmodified (gdbarch, insn, "unallocated mem hint", dsc);
6359       case 0x65:
6360         return arm_copy_preload_reg (gdbarch, insn, regs, dsc);  /* pli reg.  */
6361       case 0x71: case 0x75:
6362         /* pld/pldw reg.  */
6363         return arm_copy_preload_reg (gdbarch, insn, regs, dsc);
6364       case 0x63: case 0x67: case 0x73: case 0x77:
6365         return arm_copy_unpred (gdbarch, insn, dsc);
6366       default:
6367         return arm_copy_undef (gdbarch, insn, dsc);
6368       }
6369   else
6370     return arm_copy_undef (gdbarch, insn, dsc);  /* Probably unreachable.  */
6371 }
6372
6373 static int
6374 arm_decode_unconditional (struct gdbarch *gdbarch, uint32_t insn,
6375                           struct regcache *regs,
6376                           struct displaced_step_closure *dsc)
6377 {
6378   if (bit (insn, 27) == 0)
6379     return arm_decode_misc_memhint_neon (gdbarch, insn, regs, dsc);
6380   /* Switch on bits: 0bxxxxx321xxx0xxxxxxxxxxxxxxxxxxxx.  */
6381   else switch (((insn & 0x7000000) >> 23) | ((insn & 0x100000) >> 20))
6382     {
6383     case 0x0: case 0x2:
6384       return arm_copy_unmodified (gdbarch, insn, "srs", dsc);
6385
6386     case 0x1: case 0x3:
6387       return arm_copy_unmodified (gdbarch, insn, "rfe", dsc);
6388
6389     case 0x4: case 0x5: case 0x6: case 0x7:
6390       return arm_copy_b_bl_blx (gdbarch, insn, regs, dsc);
6391
6392     case 0x8:
6393       switch ((insn & 0xe00000) >> 21)
6394         {
6395         case 0x1: case 0x3: case 0x4: case 0x5: case 0x6: case 0x7:
6396           /* stc/stc2.  */
6397           return arm_copy_copro_load_store (gdbarch, insn, regs, dsc);
6398
6399         case 0x2:
6400           return arm_copy_unmodified (gdbarch, insn, "mcrr/mcrr2", dsc);
6401
6402         default:
6403           return arm_copy_undef (gdbarch, insn, dsc);
6404         }
6405
6406     case 0x9:
6407       {
6408          int rn_f = (bits (insn, 16, 19) == 0xf);
6409         switch ((insn & 0xe00000) >> 21)
6410           {
6411           case 0x1: case 0x3:
6412             /* ldc/ldc2 imm (undefined for rn == pc).  */
6413             return rn_f ? arm_copy_undef (gdbarch, insn, dsc)
6414                         : arm_copy_copro_load_store (gdbarch, insn, regs, dsc);
6415
6416           case 0x2:
6417             return arm_copy_unmodified (gdbarch, insn, "mrrc/mrrc2", dsc);
6418
6419           case 0x4: case 0x5: case 0x6: case 0x7:
6420             /* ldc/ldc2 lit (undefined for rn != pc).  */
6421             return rn_f ? arm_copy_copro_load_store (gdbarch, insn, regs, dsc)
6422                         : arm_copy_undef (gdbarch, insn, dsc);
6423
6424           default:
6425             return arm_copy_undef (gdbarch, insn, dsc);
6426           }
6427       }
6428
6429     case 0xa:
6430       return arm_copy_unmodified (gdbarch, insn, "stc/stc2", dsc);
6431
6432     case 0xb:
6433       if (bits (insn, 16, 19) == 0xf)
6434         /* ldc/ldc2 lit.  */
6435         return arm_copy_copro_load_store (gdbarch, insn, regs, dsc);
6436       else
6437         return arm_copy_undef (gdbarch, insn, dsc);
6438
6439     case 0xc:
6440       if (bit (insn, 4))
6441         return arm_copy_unmodified (gdbarch, insn, "mcr/mcr2", dsc);
6442       else
6443         return arm_copy_unmodified (gdbarch, insn, "cdp/cdp2", dsc);
6444
6445     case 0xd:
6446       if (bit (insn, 4))
6447         return arm_copy_unmodified (gdbarch, insn, "mrc/mrc2", dsc);
6448       else
6449         return arm_copy_unmodified (gdbarch, insn, "cdp/cdp2", dsc);
6450
6451     default:
6452       return arm_copy_undef (gdbarch, insn, dsc);
6453     }
6454 }
6455
6456 /* Decode miscellaneous instructions in dp/misc encoding space.  */
6457
6458 static int
6459 arm_decode_miscellaneous (struct gdbarch *gdbarch, uint32_t insn,
6460                           struct regcache *regs,
6461                           struct displaced_step_closure *dsc)
6462 {
6463   unsigned int op2 = bits (insn, 4, 6);
6464   unsigned int op = bits (insn, 21, 22);
6465
6466   switch (op2)
6467     {
6468     case 0x0:
6469       return arm_copy_unmodified (gdbarch, insn, "mrs/msr", dsc);
6470
6471     case 0x1:
6472       if (op == 0x1)  /* bx.  */
6473         return arm_copy_bx_blx_reg (gdbarch, insn, regs, dsc);
6474       else if (op == 0x3)
6475         return arm_copy_unmodified (gdbarch, insn, "clz", dsc);
6476       else
6477         return arm_copy_undef (gdbarch, insn, dsc);
6478
6479     case 0x2:
6480       if (op == 0x1)
6481         /* Not really supported.  */
6482         return arm_copy_unmodified (gdbarch, insn, "bxj", dsc);
6483       else
6484         return arm_copy_undef (gdbarch, insn, dsc);
6485
6486     case 0x3:
6487       if (op == 0x1)
6488         return arm_copy_bx_blx_reg (gdbarch, insn,
6489                                 regs, dsc);  /* blx register.  */
6490       else
6491         return arm_copy_undef (gdbarch, insn, dsc);
6492
6493     case 0x5:
6494       return arm_copy_unmodified (gdbarch, insn, "saturating add/sub", dsc);
6495
6496     case 0x7:
6497       if (op == 0x1)
6498         return arm_copy_unmodified (gdbarch, insn, "bkpt", dsc);
6499       else if (op == 0x3)
6500         /* Not really supported.  */
6501         return arm_copy_unmodified (gdbarch, insn, "smc", dsc);
6502
6503     default:
6504       return arm_copy_undef (gdbarch, insn, dsc);
6505     }
6506 }
6507
6508 static int
6509 arm_decode_dp_misc (struct gdbarch *gdbarch, uint32_t insn,
6510                     struct regcache *regs,
6511                     struct displaced_step_closure *dsc)
6512 {
6513   if (bit (insn, 25))
6514     switch (bits (insn, 20, 24))
6515       {
6516       case 0x10:
6517         return arm_copy_unmodified (gdbarch, insn, "movw", dsc);
6518
6519       case 0x14:
6520         return arm_copy_unmodified (gdbarch, insn, "movt", dsc);
6521
6522       case 0x12: case 0x16:
6523         return arm_copy_unmodified (gdbarch, insn, "msr imm", dsc);
6524
6525       default:
6526         return arm_copy_alu_imm (gdbarch, insn, regs, dsc);
6527       }
6528   else
6529     {
6530       uint32_t op1 = bits (insn, 20, 24), op2 = bits (insn, 4, 7);
6531
6532       if ((op1 & 0x19) != 0x10 && (op2 & 0x1) == 0x0)
6533         return arm_copy_alu_reg (gdbarch, insn, regs, dsc);
6534       else if ((op1 & 0x19) != 0x10 && (op2 & 0x9) == 0x1)
6535         return arm_copy_alu_shifted_reg (gdbarch, insn, regs, dsc);
6536       else if ((op1 & 0x19) == 0x10 && (op2 & 0x8) == 0x0)
6537         return arm_decode_miscellaneous (gdbarch, insn, regs, dsc);
6538       else if ((op1 & 0x19) == 0x10 && (op2 & 0x9) == 0x8)
6539         return arm_copy_unmodified (gdbarch, insn, "halfword mul/mla", dsc);
6540       else if ((op1 & 0x10) == 0x00 && op2 == 0x9)
6541         return arm_copy_unmodified (gdbarch, insn, "mul/mla", dsc);
6542       else if ((op1 & 0x10) == 0x10 && op2 == 0x9)
6543         return arm_copy_unmodified (gdbarch, insn, "synch", dsc);
6544       else if (op2 == 0xb || (op2 & 0xd) == 0xd)
6545         /* 2nd arg means "unprivileged".  */
6546         return arm_copy_extra_ld_st (gdbarch, insn, (op1 & 0x12) == 0x02, regs,
6547                                      dsc);
6548     }
6549
6550   /* Should be unreachable.  */
6551   return 1;
6552 }
6553
6554 static int
6555 arm_decode_ld_st_word_ubyte (struct gdbarch *gdbarch, uint32_t insn,
6556                              struct regcache *regs,
6557                              struct displaced_step_closure *dsc)
6558 {
6559   int a = bit (insn, 25), b = bit (insn, 4);
6560   uint32_t op1 = bits (insn, 20, 24);
6561
6562   if ((!a && (op1 & 0x05) == 0x00 && (op1 & 0x17) != 0x02)
6563       || (a && (op1 & 0x05) == 0x00 && (op1 & 0x17) != 0x02 && !b))
6564     return arm_copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 0, 4, 0);
6565   else if ((!a && (op1 & 0x17) == 0x02)
6566             || (a && (op1 & 0x17) == 0x02 && !b))
6567     return arm_copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 0, 4, 1);
6568   else if ((!a && (op1 & 0x05) == 0x01 && (op1 & 0x17) != 0x03)
6569             || (a && (op1 & 0x05) == 0x01 && (op1 & 0x17) != 0x03 && !b))
6570     return arm_copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 1, 4, 0);
6571   else if ((!a && (op1 & 0x17) == 0x03)
6572            || (a && (op1 & 0x17) == 0x03 && !b))
6573     return arm_copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 1, 4, 1);
6574   else if ((!a && (op1 & 0x05) == 0x04 && (op1 & 0x17) != 0x06)
6575             || (a && (op1 & 0x05) == 0x04 && (op1 & 0x17) != 0x06 && !b))
6576     return arm_copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 0, 1, 0);
6577   else if ((!a && (op1 & 0x17) == 0x06)
6578            || (a && (op1 & 0x17) == 0x06 && !b))
6579     return arm_copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 0, 1, 1);
6580   else if ((!a && (op1 & 0x05) == 0x05 && (op1 & 0x17) != 0x07)
6581            || (a && (op1 & 0x05) == 0x05 && (op1 & 0x17) != 0x07 && !b))
6582     return arm_copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 1, 1, 0);
6583   else if ((!a && (op1 & 0x17) == 0x07)
6584            || (a && (op1 & 0x17) == 0x07 && !b))
6585     return arm_copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 1, 1, 1);
6586
6587   /* Should be unreachable.  */
6588   return 1;
6589 }
6590
6591 static int
6592 arm_decode_media (struct gdbarch *gdbarch, uint32_t insn,
6593                   struct displaced_step_closure *dsc)
6594 {
6595   switch (bits (insn, 20, 24))
6596     {
6597     case 0x00: case 0x01: case 0x02: case 0x03:
6598       return arm_copy_unmodified (gdbarch, insn, "parallel add/sub signed", dsc);
6599
6600     case 0x04: case 0x05: case 0x06: case 0x07:
6601       return arm_copy_unmodified (gdbarch, insn, "parallel add/sub unsigned", dsc);
6602
6603     case 0x08: case 0x09: case 0x0a: case 0x0b:
6604     case 0x0c: case 0x0d: case 0x0e: case 0x0f:
6605       return arm_copy_unmodified (gdbarch, insn,
6606                               "decode/pack/unpack/saturate/reverse", dsc);
6607
6608     case 0x18:
6609       if (bits (insn, 5, 7) == 0)  /* op2.  */
6610          {
6611           if (bits (insn, 12, 15) == 0xf)
6612             return arm_copy_unmodified (gdbarch, insn, "usad8", dsc);
6613           else
6614             return arm_copy_unmodified (gdbarch, insn, "usada8", dsc);
6615         }
6616       else
6617          return arm_copy_undef (gdbarch, insn, dsc);
6618
6619     case 0x1a: case 0x1b:
6620       if (bits (insn, 5, 6) == 0x2)  /* op2[1:0].  */
6621         return arm_copy_unmodified (gdbarch, insn, "sbfx", dsc);
6622       else
6623         return arm_copy_undef (gdbarch, insn, dsc);
6624
6625     case 0x1c: case 0x1d:
6626       if (bits (insn, 5, 6) == 0x0)  /* op2[1:0].  */
6627          {
6628           if (bits (insn, 0, 3) == 0xf)
6629             return arm_copy_unmodified (gdbarch, insn, "bfc", dsc);
6630           else
6631             return arm_copy_unmodified (gdbarch, insn, "bfi", dsc);
6632         }
6633       else
6634         return arm_copy_undef (gdbarch, insn, dsc);
6635
6636     case 0x1e: case 0x1f:
6637       if (bits (insn, 5, 6) == 0x2)  /* op2[1:0].  */
6638         return arm_copy_unmodified (gdbarch, insn, "ubfx", dsc);
6639       else
6640         return arm_copy_undef (gdbarch, insn, dsc);
6641     }
6642
6643   /* Should be unreachable.  */
6644   return 1;
6645 }
6646
6647 static int
6648 arm_decode_b_bl_ldmstm (struct gdbarch *gdbarch, uint32_t insn,
6649                         struct regcache *regs,
6650                         struct displaced_step_closure *dsc)
6651 {
6652   if (bit (insn, 25))
6653     return arm_copy_b_bl_blx (gdbarch, insn, regs, dsc);
6654   else
6655     return arm_copy_block_xfer (gdbarch, insn, regs, dsc);
6656 }
6657
6658 static int
6659 arm_decode_ext_reg_ld_st (struct gdbarch *gdbarch, uint32_t insn,
6660                           struct regcache *regs,
6661                           struct displaced_step_closure *dsc)
6662 {
6663   unsigned int opcode = bits (insn, 20, 24);
6664
6665   switch (opcode)
6666     {
6667     case 0x04: case 0x05:  /* VFP/Neon mrrc/mcrr.  */
6668       return arm_copy_unmodified (gdbarch, insn, "vfp/neon mrrc/mcrr", dsc);
6669
6670     case 0x08: case 0x0a: case 0x0c: case 0x0e:
6671     case 0x12: case 0x16:
6672       return arm_copy_unmodified (gdbarch, insn, "vfp/neon vstm/vpush", dsc);
6673
6674     case 0x09: case 0x0b: case 0x0d: case 0x0f:
6675     case 0x13: case 0x17:
6676       return arm_copy_unmodified (gdbarch, insn, "vfp/neon vldm/vpop", dsc);
6677
6678     case 0x10: case 0x14: case 0x18: case 0x1c:  /* vstr.  */
6679     case 0x11: case 0x15: case 0x19: case 0x1d:  /* vldr.  */
6680       /* Note: no writeback for these instructions.  Bit 25 will always be
6681          zero though (via caller), so the following works OK.  */
6682       return arm_copy_copro_load_store (gdbarch, insn, regs, dsc);
6683     }
6684
6685   /* Should be unreachable.  */
6686   return 1;
6687 }
6688
6689 /* Decode shifted register instructions.  */
6690
6691 static int
6692 thumb2_decode_dp_shift_reg (struct gdbarch *gdbarch, uint16_t insn1,
6693                             uint16_t insn2,  struct regcache *regs,
6694                             struct displaced_step_closure *dsc)
6695 {
6696   /* PC is only allowed to be used in instruction MOV.  */
6697
6698   unsigned int op = bits (insn1, 5, 8);
6699   unsigned int rn = bits (insn1, 0, 3);
6700
6701   if (op == 0x2 && rn == 0xf) /* MOV */
6702     return thumb2_copy_alu_imm (gdbarch, insn1, insn2, regs, dsc);
6703   else
6704     return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
6705                                         "dp (shift reg)", dsc);
6706 }
6707
6708
6709 /* Decode extension register load/store.  Exactly the same as
6710    arm_decode_ext_reg_ld_st.  */
6711
6712 static int
6713 thumb2_decode_ext_reg_ld_st (struct gdbarch *gdbarch, uint16_t insn1,
6714                              uint16_t insn2,  struct regcache *regs,
6715                              struct displaced_step_closure *dsc)
6716 {
6717   unsigned int opcode = bits (insn1, 4, 8);
6718
6719   switch (opcode)
6720     {
6721     case 0x04: case 0x05:
6722       return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
6723                                           "vfp/neon vmov", dsc);
6724
6725     case 0x08: case 0x0c: /* 01x00 */
6726     case 0x0a: case 0x0e: /* 01x10 */
6727     case 0x12: case 0x16: /* 10x10 */
6728       return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
6729                                           "vfp/neon vstm/vpush", dsc);
6730
6731     case 0x09: case 0x0d: /* 01x01 */
6732     case 0x0b: case 0x0f: /* 01x11 */
6733     case 0x13: case 0x17: /* 10x11 */
6734       return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
6735                                           "vfp/neon vldm/vpop", dsc);
6736
6737     case 0x10: case 0x14: case 0x18: case 0x1c:  /* vstr.  */
6738       return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
6739                                           "vstr", dsc);
6740     case 0x11: case 0x15: case 0x19: case 0x1d:  /* vldr.  */
6741       return thumb2_copy_copro_load_store (gdbarch, insn1, insn2, regs, dsc);
6742     }
6743
6744   /* Should be unreachable.  */
6745   return 1;
6746 }
6747
6748 static int
6749 arm_decode_svc_copro (struct gdbarch *gdbarch, uint32_t insn,
6750                       struct regcache *regs, struct displaced_step_closure *dsc)
6751 {
6752   unsigned int op1 = bits (insn, 20, 25);
6753   int op = bit (insn, 4);
6754   unsigned int coproc = bits (insn, 8, 11);
6755
6756   if ((op1 & 0x20) == 0x00 && (op1 & 0x3a) != 0x00 && (coproc & 0xe) == 0xa)
6757     return arm_decode_ext_reg_ld_st (gdbarch, insn, regs, dsc);
6758   else if ((op1 & 0x21) == 0x00 && (op1 & 0x3a) != 0x00
6759            && (coproc & 0xe) != 0xa)
6760     /* stc/stc2.  */
6761     return arm_copy_copro_load_store (gdbarch, insn, regs, dsc);
6762   else if ((op1 & 0x21) == 0x01 && (op1 & 0x3a) != 0x00
6763            && (coproc & 0xe) != 0xa)
6764     /* ldc/ldc2 imm/lit.  */
6765     return arm_copy_copro_load_store (gdbarch, insn, regs, dsc);
6766   else if ((op1 & 0x3e) == 0x00)
6767     return arm_copy_undef (gdbarch, insn, dsc);
6768   else if ((op1 & 0x3e) == 0x04 && (coproc & 0xe) == 0xa)
6769     return arm_copy_unmodified (gdbarch, insn, "neon 64bit xfer", dsc);
6770   else if (op1 == 0x04 && (coproc & 0xe) != 0xa)
6771     return arm_copy_unmodified (gdbarch, insn, "mcrr/mcrr2", dsc);
6772   else if (op1 == 0x05 && (coproc & 0xe) != 0xa)
6773     return arm_copy_unmodified (gdbarch, insn, "mrrc/mrrc2", dsc);
6774   else if ((op1 & 0x30) == 0x20 && !op)
6775     {
6776       if ((coproc & 0xe) == 0xa)
6777         return arm_copy_unmodified (gdbarch, insn, "vfp dataproc", dsc);
6778       else
6779         return arm_copy_unmodified (gdbarch, insn, "cdp/cdp2", dsc);
6780     }
6781   else if ((op1 & 0x30) == 0x20 && op)
6782     return arm_copy_unmodified (gdbarch, insn, "neon 8/16/32 bit xfer", dsc);
6783   else if ((op1 & 0x31) == 0x20 && op && (coproc & 0xe) != 0xa)
6784     return arm_copy_unmodified (gdbarch, insn, "mcr/mcr2", dsc);
6785   else if ((op1 & 0x31) == 0x21 && op && (coproc & 0xe) != 0xa)
6786     return arm_copy_unmodified (gdbarch, insn, "mrc/mrc2", dsc);
6787   else if ((op1 & 0x30) == 0x30)
6788     return arm_copy_svc (gdbarch, insn, regs, dsc);
6789   else
6790     return arm_copy_undef (gdbarch, insn, dsc);  /* Possibly unreachable.  */
6791 }
6792
6793 static int
6794 thumb2_decode_svc_copro (struct gdbarch *gdbarch, uint16_t insn1,
6795                          uint16_t insn2, struct regcache *regs,
6796                          struct displaced_step_closure *dsc)
6797 {
6798   unsigned int coproc = bits (insn2, 8, 11);
6799   unsigned int bit_5_8 = bits (insn1, 5, 8);
6800   unsigned int bit_9 = bit (insn1, 9);
6801   unsigned int bit_4 = bit (insn1, 4);
6802
6803   if (bit_9 == 0)
6804     {
6805       if (bit_5_8 == 2)
6806         return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
6807                                             "neon 64bit xfer/mrrc/mrrc2/mcrr/mcrr2",
6808                                             dsc);
6809       else if (bit_5_8 == 0) /* UNDEFINED.  */
6810         return thumb_32bit_copy_undef (gdbarch, insn1, insn2, dsc);
6811       else
6812         {
6813            /*coproc is 101x.  SIMD/VFP, ext registers load/store.  */
6814           if ((coproc & 0xe) == 0xa)
6815             return thumb2_decode_ext_reg_ld_st (gdbarch, insn1, insn2, regs,
6816                                                 dsc);
6817           else /* coproc is not 101x.  */
6818             {
6819               if (bit_4 == 0) /* STC/STC2.  */
6820                 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
6821                                                     "stc/stc2", dsc);
6822               else /* LDC/LDC2 {literal, immeidate}.  */
6823                 return thumb2_copy_copro_load_store (gdbarch, insn1, insn2,
6824                                                      regs, dsc);
6825             }
6826         }
6827     }
6828   else
6829     return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2, "coproc", dsc);
6830
6831   return 0;
6832 }
6833
6834 static void
6835 install_pc_relative (struct gdbarch *gdbarch, struct regcache *regs,
6836                      struct displaced_step_closure *dsc, int rd)
6837 {
6838   /* ADR Rd, #imm
6839
6840      Rewrite as:
6841
6842      Preparation: Rd <- PC
6843      Insn: ADD Rd, #imm
6844      Cleanup: Null.
6845   */
6846
6847   /* Rd <- PC */
6848   int val = displaced_read_reg (regs, dsc, ARM_PC_REGNUM);
6849   displaced_write_reg (regs, dsc, rd, val, CANNOT_WRITE_PC);
6850 }
6851
6852 static int
6853 thumb_copy_pc_relative_16bit (struct gdbarch *gdbarch, struct regcache *regs,
6854                               struct displaced_step_closure *dsc,
6855                               int rd, unsigned int imm)
6856 {
6857
6858   /* Encoding T2: ADDS Rd, #imm */
6859   dsc->modinsn[0] = (0x3000 | (rd << 8) | imm);
6860
6861   install_pc_relative (gdbarch, regs, dsc, rd);
6862
6863   return 0;
6864 }
6865
6866 static int
6867 thumb_decode_pc_relative_16bit (struct gdbarch *gdbarch, uint16_t insn,
6868                                 struct regcache *regs,
6869                                 struct displaced_step_closure *dsc)
6870 {
6871   unsigned int rd = bits (insn, 8, 10);
6872   unsigned int imm8 = bits (insn, 0, 7);
6873
6874   if (debug_displaced)
6875     fprintf_unfiltered (gdb_stdlog,
6876                         "displaced: copying thumb adr r%d, #%d insn %.4x\n",
6877                         rd, imm8, insn);
6878
6879   return thumb_copy_pc_relative_16bit (gdbarch, regs, dsc, rd, imm8);
6880 }
6881
6882 static int
6883 thumb_copy_pc_relative_32bit (struct gdbarch *gdbarch, uint16_t insn1,
6884                               uint16_t insn2, struct regcache *regs,
6885                               struct displaced_step_closure *dsc)
6886 {
6887   unsigned int rd = bits (insn2, 8, 11);
6888   /* Since immediate has the same encoding in ADR ADD and SUB, so we simply
6889      extract raw immediate encoding rather than computing immediate.  When
6890      generating ADD or SUB instruction, we can simply perform OR operation to
6891      set immediate into ADD.  */
6892   unsigned int imm_3_8 = insn2 & 0x70ff;
6893   unsigned int imm_i = insn1 & 0x0400; /* Clear all bits except bit 10.  */
6894
6895   if (debug_displaced)
6896     fprintf_unfiltered (gdb_stdlog,
6897                         "displaced: copying thumb adr r%d, #%d:%d insn %.4x%.4x\n",
6898                         rd, imm_i, imm_3_8, insn1, insn2);
6899
6900   if (bit (insn1, 7)) /* Encoding T2 */
6901     {
6902       /* Encoding T3: SUB Rd, Rd, #imm */
6903       dsc->modinsn[0] = (0xf1a0 | rd | imm_i);
6904       dsc->modinsn[1] = ((rd << 8) | imm_3_8);
6905     }
6906   else /* Encoding T3 */
6907     {
6908       /* Encoding T3: ADD Rd, Rd, #imm */
6909       dsc->modinsn[0] = (0xf100 | rd | imm_i);
6910       dsc->modinsn[1] = ((rd << 8) | imm_3_8);
6911     }
6912   dsc->numinsns = 2;
6913
6914   install_pc_relative (gdbarch, regs, dsc, rd);
6915
6916   return 0;
6917 }
6918
6919 static int
6920 thumb_copy_16bit_ldr_literal (struct gdbarch *gdbarch, uint16_t insn1,
6921                               struct regcache *regs,
6922                               struct displaced_step_closure *dsc)
6923 {
6924   unsigned int rt = bits (insn1, 8, 10);
6925   unsigned int pc;
6926   int imm8 = (bits (insn1, 0, 7) << 2);
6927
6928   /* LDR Rd, #imm8
6929
6930      Rwrite as:
6931
6932      Preparation: tmp0 <- R0, tmp2 <- R2, tmp3 <- R3, R2 <- PC, R3 <- #imm8;
6933
6934      Insn: LDR R0, [R2, R3];
6935      Cleanup: R2 <- tmp2, R3 <- tmp3, Rd <- R0, R0 <- tmp0 */
6936
6937   if (debug_displaced)
6938     fprintf_unfiltered (gdb_stdlog,
6939                         "displaced: copying thumb ldr r%d [pc #%d]\n"
6940                         , rt, imm8);
6941
6942   dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
6943   dsc->tmp[2] = displaced_read_reg (regs, dsc, 2);
6944   dsc->tmp[3] = displaced_read_reg (regs, dsc, 3);
6945   pc = displaced_read_reg (regs, dsc, ARM_PC_REGNUM);
6946   /* The assembler calculates the required value of the offset from the
6947      Align(PC,4) value of this instruction to the label.  */
6948   pc = pc & 0xfffffffc;
6949
6950   displaced_write_reg (regs, dsc, 2, pc, CANNOT_WRITE_PC);
6951   displaced_write_reg (regs, dsc, 3, imm8, CANNOT_WRITE_PC);
6952
6953   dsc->rd = rt;
6954   dsc->u.ldst.xfersize = 4;
6955   dsc->u.ldst.rn = 0;
6956   dsc->u.ldst.immed = 0;
6957   dsc->u.ldst.writeback = 0;
6958   dsc->u.ldst.restore_r4 = 0;
6959
6960   dsc->modinsn[0] = 0x58d0; /* ldr r0, [r2, r3]*/
6961
6962   dsc->cleanup = &cleanup_load;
6963
6964   return 0;
6965 }
6966
6967 /* Copy Thumb cbnz/cbz insruction.  */
6968
6969 static int
6970 thumb_copy_cbnz_cbz (struct gdbarch *gdbarch, uint16_t insn1,
6971                      struct regcache *regs,
6972                      struct displaced_step_closure *dsc)
6973 {
6974   int non_zero = bit (insn1, 11);
6975   unsigned int imm5 = (bit (insn1, 9) << 6) | (bits (insn1, 3, 7) << 1);
6976   CORE_ADDR from = dsc->insn_addr;
6977   int rn = bits (insn1, 0, 2);
6978   int rn_val = displaced_read_reg (regs, dsc, rn);
6979
6980   dsc->u.branch.cond = (rn_val && non_zero) || (!rn_val && !non_zero);
6981   /* CBNZ and CBZ do not affect the condition flags.  If condition is true,
6982      set it INST_AL, so cleanup_branch will know branch is taken, otherwise,
6983      condition is false, let it be, cleanup_branch will do nothing.  */
6984   if (dsc->u.branch.cond)
6985     {
6986       dsc->u.branch.cond = INST_AL;
6987       dsc->u.branch.dest = from + 4 + imm5;
6988     }
6989   else
6990       dsc->u.branch.dest = from + 2;
6991
6992   dsc->u.branch.link = 0;
6993   dsc->u.branch.exchange = 0;
6994
6995   if (debug_displaced)
6996     fprintf_unfiltered (gdb_stdlog, "displaced: copying %s [r%d = 0x%x]"
6997                         " insn %.4x to %.8lx\n", non_zero ? "cbnz" : "cbz",
6998                         rn, rn_val, insn1, dsc->u.branch.dest);
6999
7000   dsc->modinsn[0] = THUMB_NOP;
7001
7002   dsc->cleanup = &cleanup_branch;
7003   return 0;
7004 }
7005
7006 /* Copy Table Branch Byte/Halfword */
7007 static int
7008 thumb2_copy_table_branch (struct gdbarch *gdbarch, uint16_t insn1,
7009                           uint16_t insn2, struct regcache *regs,
7010                           struct displaced_step_closure *dsc)
7011 {
7012   ULONGEST rn_val, rm_val;
7013   int is_tbh = bit (insn2, 4);
7014   CORE_ADDR halfwords = 0;
7015   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
7016
7017   rn_val = displaced_read_reg (regs, dsc, bits (insn1, 0, 3));
7018   rm_val = displaced_read_reg (regs, dsc, bits (insn2, 0, 3));
7019
7020   if (is_tbh)
7021     {
7022       gdb_byte buf[2];
7023
7024       target_read_memory (rn_val + 2 * rm_val, buf, 2);
7025       halfwords = extract_unsigned_integer (buf, 2, byte_order);
7026     }
7027   else
7028     {
7029       gdb_byte buf[1];
7030
7031       target_read_memory (rn_val + rm_val, buf, 1);
7032       halfwords = extract_unsigned_integer (buf, 1, byte_order);
7033     }
7034
7035   if (debug_displaced)
7036     fprintf_unfiltered (gdb_stdlog, "displaced: %s base 0x%x offset 0x%x"
7037                         " offset 0x%x\n", is_tbh ? "tbh" : "tbb",
7038                         (unsigned int) rn_val, (unsigned int) rm_val,
7039                         (unsigned int) halfwords);
7040
7041   dsc->u.branch.cond = INST_AL;
7042   dsc->u.branch.link = 0;
7043   dsc->u.branch.exchange = 0;
7044   dsc->u.branch.dest = dsc->insn_addr + 4 + 2 * halfwords;
7045
7046   dsc->cleanup = &cleanup_branch;
7047
7048   return 0;
7049 }
7050
7051 static void
7052 cleanup_pop_pc_16bit_all (struct gdbarch *gdbarch, struct regcache *regs,
7053                           struct displaced_step_closure *dsc)
7054 {
7055   /* PC <- r7 */
7056   int val = displaced_read_reg (regs, dsc, 7);
7057   displaced_write_reg (regs, dsc, ARM_PC_REGNUM, val, BX_WRITE_PC);
7058
7059   /* r7 <- r8 */
7060   val = displaced_read_reg (regs, dsc, 8);
7061   displaced_write_reg (regs, dsc, 7, val, CANNOT_WRITE_PC);
7062
7063   /* r8 <- tmp[0] */
7064   displaced_write_reg (regs, dsc, 8, dsc->tmp[0], CANNOT_WRITE_PC);
7065
7066 }
7067
7068 static int
7069 thumb_copy_pop_pc_16bit (struct gdbarch *gdbarch, uint16_t insn1,
7070                          struct regcache *regs,
7071                          struct displaced_step_closure *dsc)
7072 {
7073   dsc->u.block.regmask = insn1 & 0x00ff;
7074
7075   /* Rewrite instruction: POP {rX, rY, ...,rZ, PC}
7076      to :
7077
7078      (1) register list is full, that is, r0-r7 are used.
7079      Prepare: tmp[0] <- r8
7080
7081      POP {r0, r1, ...., r6, r7}; remove PC from reglist
7082      MOV r8, r7; Move value of r7 to r8;
7083      POP {r7}; Store PC value into r7.
7084
7085      Cleanup: PC <- r7, r7 <- r8, r8 <-tmp[0]
7086
7087      (2) register list is not full, supposing there are N registers in
7088      register list (except PC, 0 <= N <= 7).
7089      Prepare: for each i, 0 - N, tmp[i] <- ri.
7090
7091      POP {r0, r1, ...., rN};
7092
7093      Cleanup: Set registers in original reglist from r0 - rN.  Restore r0 - rN
7094      from tmp[] properly.
7095   */
7096   if (debug_displaced)
7097     fprintf_unfiltered (gdb_stdlog,
7098                         "displaced: copying thumb pop {%.8x, pc} insn %.4x\n",
7099                         dsc->u.block.regmask, insn1);
7100
7101   if (dsc->u.block.regmask == 0xff)
7102     {
7103       dsc->tmp[0] = displaced_read_reg (regs, dsc, 8);
7104
7105       dsc->modinsn[0] = (insn1 & 0xfeff); /* POP {r0,r1,...,r6, r7} */
7106       dsc->modinsn[1] = 0x46b8; /* MOV r8, r7 */
7107       dsc->modinsn[2] = 0xbc80; /* POP {r7} */
7108
7109       dsc->numinsns = 3;
7110       dsc->cleanup = &cleanup_pop_pc_16bit_all;
7111     }
7112   else
7113     {
7114       unsigned int num_in_list = bitcount (dsc->u.block.regmask);
7115       unsigned int i;
7116       unsigned int new_regmask;
7117
7118       for (i = 0; i < num_in_list + 1; i++)
7119         dsc->tmp[i] = displaced_read_reg (regs, dsc, i);
7120
7121       new_regmask = (1 << (num_in_list + 1)) - 1;
7122
7123       if (debug_displaced)
7124         fprintf_unfiltered (gdb_stdlog, _("displaced: POP "
7125                                           "{..., pc}: original reg list %.4x,"
7126                                           " modified list %.4x\n"),
7127                             (int) dsc->u.block.regmask, new_regmask);
7128
7129       dsc->u.block.regmask |= 0x8000;
7130       dsc->u.block.writeback = 0;
7131       dsc->u.block.cond = INST_AL;
7132
7133       dsc->modinsn[0] = (insn1 & ~0x1ff) | (new_regmask & 0xff);
7134
7135       dsc->cleanup = &cleanup_block_load_pc;
7136     }
7137
7138   return 0;
7139 }
7140
7141 static void
7142 thumb_process_displaced_16bit_insn (struct gdbarch *gdbarch, uint16_t insn1,
7143                                     struct regcache *regs,
7144                                     struct displaced_step_closure *dsc)
7145 {
7146   unsigned short op_bit_12_15 = bits (insn1, 12, 15);
7147   unsigned short op_bit_10_11 = bits (insn1, 10, 11);
7148   int err = 0;
7149
7150   /* 16-bit thumb instructions.  */
7151   switch (op_bit_12_15)
7152     {
7153       /* Shift (imme), add, subtract, move and compare.  */
7154     case 0: case 1: case 2: case 3:
7155       err = thumb_copy_unmodified_16bit (gdbarch, insn1,
7156                                          "shift/add/sub/mov/cmp",
7157                                          dsc);
7158       break;
7159     case 4:
7160       switch (op_bit_10_11)
7161         {
7162         case 0: /* Data-processing */
7163           err = thumb_copy_unmodified_16bit (gdbarch, insn1,
7164                                              "data-processing",
7165                                              dsc);
7166           break;
7167         case 1: /* Special data instructions and branch and exchange.  */
7168           {
7169             unsigned short op = bits (insn1, 7, 9);
7170             if (op == 6 || op == 7) /* BX or BLX */
7171               err = thumb_copy_bx_blx_reg (gdbarch, insn1, regs, dsc);
7172             else if (bits (insn1, 6, 7) != 0) /* ADD/MOV/CMP high registers.  */
7173               err = thumb_copy_alu_reg (gdbarch, insn1, regs, dsc);
7174             else
7175               err = thumb_copy_unmodified_16bit (gdbarch, insn1, "special data",
7176                                                  dsc);
7177           }
7178           break;
7179         default: /* LDR (literal) */
7180           err = thumb_copy_16bit_ldr_literal (gdbarch, insn1, regs, dsc);
7181         }
7182       break;
7183     case 5: case 6: case 7: case 8: case 9: /* Load/Store single data item */
7184       err = thumb_copy_unmodified_16bit (gdbarch, insn1, "ldr/str", dsc);
7185       break;
7186     case 10:
7187       if (op_bit_10_11 < 2) /* Generate PC-relative address */
7188         err = thumb_decode_pc_relative_16bit (gdbarch, insn1, regs, dsc);
7189       else /* Generate SP-relative address */
7190         err = thumb_copy_unmodified_16bit (gdbarch, insn1, "sp-relative", dsc);
7191       break;
7192     case 11: /* Misc 16-bit instructions */
7193       {
7194         switch (bits (insn1, 8, 11))
7195           {
7196           case 1: case 3:  case 9: case 11: /* CBNZ, CBZ */
7197             err = thumb_copy_cbnz_cbz (gdbarch, insn1, regs, dsc);
7198             break;
7199           case 12: case 13: /* POP */
7200             if (bit (insn1, 8)) /* PC is in register list.  */
7201               err = thumb_copy_pop_pc_16bit (gdbarch, insn1, regs, dsc);
7202             else
7203               err = thumb_copy_unmodified_16bit (gdbarch, insn1, "pop", dsc);
7204             break;
7205           case 15: /* If-Then, and hints */
7206             if (bits (insn1, 0, 3))
7207               /* If-Then makes up to four following instructions conditional.
7208                  IT instruction itself is not conditional, so handle it as a
7209                  common unmodified instruction.  */
7210               err = thumb_copy_unmodified_16bit (gdbarch, insn1, "If-Then",
7211                                                  dsc);
7212             else
7213               err = thumb_copy_unmodified_16bit (gdbarch, insn1, "hints", dsc);
7214             break;
7215           default:
7216             err = thumb_copy_unmodified_16bit (gdbarch, insn1, "misc", dsc);
7217           }
7218       }
7219       break;
7220     case 12:
7221       if (op_bit_10_11 < 2) /* Store multiple registers */
7222         err = thumb_copy_unmodified_16bit (gdbarch, insn1, "stm", dsc);
7223       else /* Load multiple registers */
7224         err = thumb_copy_unmodified_16bit (gdbarch, insn1, "ldm", dsc);
7225       break;
7226     case 13: /* Conditional branch and supervisor call */
7227       if (bits (insn1, 9, 11) != 7) /* conditional branch */
7228         err = thumb_copy_b (gdbarch, insn1, dsc);
7229       else
7230         err = thumb_copy_svc (gdbarch, insn1, regs, dsc);
7231       break;
7232     case 14: /* Unconditional branch */
7233       err = thumb_copy_b (gdbarch, insn1, dsc);
7234       break;
7235     default:
7236       err = 1;
7237     }
7238
7239   if (err)
7240     internal_error (__FILE__, __LINE__,
7241                     _("thumb_process_displaced_16bit_insn: Instruction decode error"));
7242 }
7243
7244 static int
7245 decode_thumb_32bit_ld_mem_hints (struct gdbarch *gdbarch,
7246                                  uint16_t insn1, uint16_t insn2,
7247                                  struct regcache *regs,
7248                                  struct displaced_step_closure *dsc)
7249 {
7250   int rt = bits (insn2, 12, 15);
7251   int rn = bits (insn1, 0, 3);
7252   int op1 = bits (insn1, 7, 8);
7253
7254   switch (bits (insn1, 5, 6))
7255     {
7256     case 0: /* Load byte and memory hints */
7257       if (rt == 0xf) /* PLD/PLI */
7258         {
7259           if (rn == 0xf)
7260             /* PLD literal or Encoding T3 of PLI(immediate, literal).  */
7261             return thumb2_copy_preload (gdbarch, insn1, insn2, regs, dsc);
7262           else
7263             return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
7264                                                 "pli/pld", dsc);
7265         }
7266       else
7267         {
7268           if (rn == 0xf) /* LDRB/LDRSB (literal) */
7269             return thumb2_copy_load_literal (gdbarch, insn1, insn2, regs, dsc,
7270                                              1);
7271           else
7272             return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
7273                                                 "ldrb{reg, immediate}/ldrbt",
7274                                                 dsc);
7275         }
7276
7277       break;
7278     case 1: /* Load halfword and memory hints.  */
7279       if (rt == 0xf) /* PLD{W} and Unalloc memory hint.  */
7280         return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
7281                                             "pld/unalloc memhint", dsc);
7282       else
7283         {
7284           if (rn == 0xf)
7285             return thumb2_copy_load_literal (gdbarch, insn1, insn2, regs, dsc,
7286                                              2);
7287           else
7288             return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
7289                                                 "ldrh/ldrht", dsc);
7290         }
7291       break;
7292     case 2: /* Load word */
7293       {
7294         int insn2_bit_8_11 = bits (insn2, 8, 11);
7295
7296         if (rn == 0xf)
7297           return thumb2_copy_load_literal (gdbarch, insn1, insn2, regs, dsc, 4);
7298         else if (op1 == 0x1) /* Encoding T3 */
7299           return thumb2_copy_load_reg_imm (gdbarch, insn1, insn2, regs, dsc,
7300                                            0, 1);
7301         else /* op1 == 0x0 */
7302           {
7303             if (insn2_bit_8_11 == 0xc || (insn2_bit_8_11 & 0x9) == 0x9)
7304               /* LDR (immediate) */
7305               return thumb2_copy_load_reg_imm (gdbarch, insn1, insn2, regs,
7306                                                dsc, bit (insn2, 8), 1);
7307             else if (insn2_bit_8_11 == 0xe) /* LDRT */
7308               return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
7309                                                   "ldrt", dsc);
7310             else
7311               /* LDR (register) */
7312               return thumb2_copy_load_reg_imm (gdbarch, insn1, insn2, regs,
7313                                                dsc, 0, 0);
7314           }
7315         break;
7316       }
7317     default:
7318       return thumb_32bit_copy_undef (gdbarch, insn1, insn2, dsc);
7319       break;
7320     }
7321   return 0;
7322 }
7323
7324 static void
7325 thumb_process_displaced_32bit_insn (struct gdbarch *gdbarch, uint16_t insn1,
7326                                     uint16_t insn2, struct regcache *regs,
7327                                     struct displaced_step_closure *dsc)
7328 {
7329   int err = 0;
7330   unsigned short op = bit (insn2, 15);
7331   unsigned int op1 = bits (insn1, 11, 12);
7332
7333   switch (op1)
7334     {
7335     case 1:
7336       {
7337         switch (bits (insn1, 9, 10))
7338           {
7339           case 0:
7340             if (bit (insn1, 6))
7341               {
7342                 /* Load/store {dual, execlusive}, table branch.  */
7343                 if (bits (insn1, 7, 8) == 1 && bits (insn1, 4, 5) == 1
7344                     && bits (insn2, 5, 7) == 0)
7345                   err = thumb2_copy_table_branch (gdbarch, insn1, insn2, regs,
7346                                                   dsc);
7347                 else
7348                   /* PC is not allowed to use in load/store {dual, exclusive}
7349                      instructions.  */
7350                   err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
7351                                                      "load/store dual/ex", dsc);
7352               }
7353             else /* load/store multiple */
7354               {
7355                 switch (bits (insn1, 7, 8))
7356                   {
7357                   case 0: case 3: /* SRS, RFE */
7358                     err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
7359                                                        "srs/rfe", dsc);
7360                     break;
7361                   case 1: case 2: /* LDM/STM/PUSH/POP */
7362                     err = thumb2_copy_block_xfer (gdbarch, insn1, insn2, regs, dsc);
7363                     break;
7364                   }
7365               }
7366             break;
7367
7368           case 1:
7369             /* Data-processing (shift register).  */
7370             err = thumb2_decode_dp_shift_reg (gdbarch, insn1, insn2, regs,
7371                                               dsc);
7372             break;
7373           default: /* Coprocessor instructions.  */
7374             err = thumb2_decode_svc_copro (gdbarch, insn1, insn2, regs, dsc);
7375             break;
7376           }
7377       break;
7378       }
7379     case 2: /* op1 = 2 */
7380       if (op) /* Branch and misc control.  */
7381         {
7382           if (bit (insn2, 14)  /* BLX/BL */
7383               || bit (insn2, 12) /* Unconditional branch */
7384               || (bits (insn1, 7, 9) != 0x7)) /* Conditional branch */
7385             err = thumb2_copy_b_bl_blx (gdbarch, insn1, insn2, regs, dsc);
7386           else
7387             err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
7388                                                "misc ctrl", dsc);
7389         }
7390       else
7391         {
7392           if (bit (insn1, 9)) /* Data processing (plain binary imm).  */
7393             {
7394               int op = bits (insn1, 4, 8);
7395               int rn = bits (insn1, 0, 3);
7396               if ((op == 0 || op == 0xa) && rn == 0xf)
7397                 err = thumb_copy_pc_relative_32bit (gdbarch, insn1, insn2,
7398                                                     regs, dsc);
7399               else
7400                 err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
7401                                                    "dp/pb", dsc);
7402             }
7403           else /* Data processing (modified immeidate) */
7404             err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
7405                                                "dp/mi", dsc);
7406         }
7407       break;
7408     case 3: /* op1 = 3 */
7409       switch (bits (insn1, 9, 10))
7410         {
7411         case 0:
7412           if (bit (insn1, 4))
7413             err = decode_thumb_32bit_ld_mem_hints (gdbarch, insn1, insn2,
7414                                                    regs, dsc);
7415           else /* NEON Load/Store and Store single data item */
7416             err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
7417                                                "neon elt/struct load/store",
7418                                                dsc);
7419           break;
7420         case 1: /* op1 = 3, bits (9, 10) == 1 */
7421           switch (bits (insn1, 7, 8))
7422             {
7423             case 0: case 1: /* Data processing (register) */
7424               err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
7425                                                  "dp(reg)", dsc);
7426               break;
7427             case 2: /* Multiply and absolute difference */
7428               err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
7429                                                  "mul/mua/diff", dsc);
7430               break;
7431             case 3: /* Long multiply and divide */
7432               err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
7433                                                  "lmul/lmua", dsc);
7434               break;
7435             }
7436           break;
7437         default: /* Coprocessor instructions */
7438           err = thumb2_decode_svc_copro (gdbarch, insn1, insn2, regs, dsc);
7439           break;
7440         }
7441       break;
7442     default:
7443       err = 1;
7444     }
7445
7446   if (err)
7447     internal_error (__FILE__, __LINE__,
7448                     _("thumb_process_displaced_32bit_insn: Instruction decode error"));
7449
7450 }
7451
7452 static void
7453 thumb_process_displaced_insn (struct gdbarch *gdbarch, CORE_ADDR from,
7454                               struct regcache *regs,
7455                               struct displaced_step_closure *dsc)
7456 {
7457   enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
7458   uint16_t insn1
7459     = read_memory_unsigned_integer (from, 2, byte_order_for_code);
7460
7461   if (debug_displaced)
7462     fprintf_unfiltered (gdb_stdlog, "displaced: process thumb insn %.4x "
7463                         "at %.8lx\n", insn1, (unsigned long) from);
7464
7465   dsc->is_thumb = 1;
7466   dsc->insn_size = thumb_insn_size (insn1);
7467   if (thumb_insn_size (insn1) == 4)
7468     {
7469       uint16_t insn2
7470         = read_memory_unsigned_integer (from + 2, 2, byte_order_for_code);
7471       thumb_process_displaced_32bit_insn (gdbarch, insn1, insn2, regs, dsc);
7472     }
7473   else
7474     thumb_process_displaced_16bit_insn (gdbarch, insn1, regs, dsc);
7475 }
7476
7477 void
7478 arm_process_displaced_insn (struct gdbarch *gdbarch, CORE_ADDR from,
7479                             CORE_ADDR to, struct regcache *regs,
7480                             struct displaced_step_closure *dsc)
7481 {
7482   int err = 0;
7483   enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
7484   uint32_t insn;
7485
7486   /* Most displaced instructions use a 1-instruction scratch space, so set this
7487      here and override below if/when necessary.  */
7488   dsc->numinsns = 1;
7489   dsc->insn_addr = from;
7490   dsc->scratch_base = to;
7491   dsc->cleanup = NULL;
7492   dsc->wrote_to_pc = 0;
7493
7494   if (!displaced_in_arm_mode (regs))
7495     return thumb_process_displaced_insn (gdbarch, from, regs, dsc);
7496
7497   dsc->is_thumb = 0;
7498   dsc->insn_size = 4;
7499   insn = read_memory_unsigned_integer (from, 4, byte_order_for_code);
7500   if (debug_displaced)
7501     fprintf_unfiltered (gdb_stdlog, "displaced: stepping insn %.8lx "
7502                         "at %.8lx\n", (unsigned long) insn,
7503                         (unsigned long) from);
7504
7505   if ((insn & 0xf0000000) == 0xf0000000)
7506     err = arm_decode_unconditional (gdbarch, insn, regs, dsc);
7507   else switch (((insn & 0x10) >> 4) | ((insn & 0xe000000) >> 24))
7508     {
7509     case 0x0: case 0x1: case 0x2: case 0x3:
7510       err = arm_decode_dp_misc (gdbarch, insn, regs, dsc);
7511       break;
7512
7513     case 0x4: case 0x5: case 0x6:
7514       err = arm_decode_ld_st_word_ubyte (gdbarch, insn, regs, dsc);
7515       break;
7516
7517     case 0x7:
7518       err = arm_decode_media (gdbarch, insn, dsc);
7519       break;
7520
7521     case 0x8: case 0x9: case 0xa: case 0xb:
7522       err = arm_decode_b_bl_ldmstm (gdbarch, insn, regs, dsc);
7523       break;
7524
7525     case 0xc: case 0xd: case 0xe: case 0xf:
7526       err = arm_decode_svc_copro (gdbarch, insn, regs, dsc);
7527       break;
7528     }
7529
7530   if (err)
7531     internal_error (__FILE__, __LINE__,
7532                     _("arm_process_displaced_insn: Instruction decode error"));
7533 }
7534
7535 /* Actually set up the scratch space for a displaced instruction.  */
7536
7537 void
7538 arm_displaced_init_closure (struct gdbarch *gdbarch, CORE_ADDR from,
7539                             CORE_ADDR to, struct displaced_step_closure *dsc)
7540 {
7541   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
7542   unsigned int i, len, offset;
7543   enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
7544   int size = dsc->is_thumb? 2 : 4;
7545   const gdb_byte *bkp_insn;
7546
7547   offset = 0;
7548   /* Poke modified instruction(s).  */
7549   for (i = 0; i < dsc->numinsns; i++)
7550     {
7551       if (debug_displaced)
7552         {
7553           fprintf_unfiltered (gdb_stdlog, "displaced: writing insn ");
7554           if (size == 4)
7555             fprintf_unfiltered (gdb_stdlog, "%.8lx",
7556                                 dsc->modinsn[i]);
7557           else if (size == 2)
7558             fprintf_unfiltered (gdb_stdlog, "%.4x",
7559                                 (unsigned short)dsc->modinsn[i]);
7560
7561           fprintf_unfiltered (gdb_stdlog, " at %.8lx\n",
7562                               (unsigned long) to + offset);
7563
7564         }
7565       write_memory_unsigned_integer (to + offset, size,
7566                                      byte_order_for_code,
7567                                      dsc->modinsn[i]);
7568       offset += size;
7569     }
7570
7571   /* Choose the correct breakpoint instruction.  */
7572   if (dsc->is_thumb)
7573     {
7574       bkp_insn = tdep->thumb_breakpoint;
7575       len = tdep->thumb_breakpoint_size;
7576     }
7577   else
7578     {
7579       bkp_insn = tdep->arm_breakpoint;
7580       len = tdep->arm_breakpoint_size;
7581     }
7582
7583   /* Put breakpoint afterwards.  */
7584   write_memory (to + offset, bkp_insn, len);
7585
7586   if (debug_displaced)
7587     fprintf_unfiltered (gdb_stdlog, "displaced: copy %s->%s: ",
7588                         paddress (gdbarch, from), paddress (gdbarch, to));
7589 }
7590
7591 /* Entry point for cleaning things up after a displaced instruction has been
7592    single-stepped.  */
7593
7594 void
7595 arm_displaced_step_fixup (struct gdbarch *gdbarch,
7596                           struct displaced_step_closure *dsc,
7597                           CORE_ADDR from, CORE_ADDR to,
7598                           struct regcache *regs)
7599 {
7600   if (dsc->cleanup)
7601     dsc->cleanup (gdbarch, regs, dsc);
7602
7603   if (!dsc->wrote_to_pc)
7604     regcache_cooked_write_unsigned (regs, ARM_PC_REGNUM,
7605                                     dsc->insn_addr + dsc->insn_size);
7606
7607 }
7608
7609 #include "bfd-in2.h"
7610 #include "libcoff.h"
7611
7612 static int
7613 gdb_print_insn_arm (bfd_vma memaddr, disassemble_info *info)
7614 {
7615   struct gdbarch *gdbarch = (struct gdbarch *) info->application_data;
7616
7617   if (arm_pc_is_thumb (gdbarch, memaddr))
7618     {
7619       static asymbol *asym;
7620       static combined_entry_type ce;
7621       static struct coff_symbol_struct csym;
7622       static struct bfd fake_bfd;
7623       static bfd_target fake_target;
7624
7625       if (csym.native == NULL)
7626         {
7627           /* Create a fake symbol vector containing a Thumb symbol.
7628              This is solely so that the code in print_insn_little_arm() 
7629              and print_insn_big_arm() in opcodes/arm-dis.c will detect
7630              the presence of a Thumb symbol and switch to decoding
7631              Thumb instructions.  */
7632
7633           fake_target.flavour = bfd_target_coff_flavour;
7634           fake_bfd.xvec = &fake_target;
7635           ce.u.syment.n_sclass = C_THUMBEXTFUNC;
7636           csym.native = &ce;
7637           csym.symbol.the_bfd = &fake_bfd;
7638           csym.symbol.name = "fake";
7639           asym = (asymbol *) & csym;
7640         }
7641
7642       memaddr = UNMAKE_THUMB_ADDR (memaddr);
7643       info->symbols = &asym;
7644     }
7645   else
7646     info->symbols = NULL;
7647
7648   if (info->endian == BFD_ENDIAN_BIG)
7649     return print_insn_big_arm (memaddr, info);
7650   else
7651     return print_insn_little_arm (memaddr, info);
7652 }
7653
7654 /* The following define instruction sequences that will cause ARM
7655    cpu's to take an undefined instruction trap.  These are used to
7656    signal a breakpoint to GDB.
7657    
7658    The newer ARMv4T cpu's are capable of operating in ARM or Thumb
7659    modes.  A different instruction is required for each mode.  The ARM
7660    cpu's can also be big or little endian.  Thus four different
7661    instructions are needed to support all cases.
7662    
7663    Note: ARMv4 defines several new instructions that will take the
7664    undefined instruction trap.  ARM7TDMI is nominally ARMv4T, but does
7665    not in fact add the new instructions.  The new undefined
7666    instructions in ARMv4 are all instructions that had no defined
7667    behaviour in earlier chips.  There is no guarantee that they will
7668    raise an exception, but may be treated as NOP's.  In practice, it
7669    may only safe to rely on instructions matching:
7670    
7671    3 3 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1 
7672    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
7673    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
7674    
7675    Even this may only true if the condition predicate is true.  The
7676    following use a condition predicate of ALWAYS so it is always TRUE.
7677    
7678    There are other ways of forcing a breakpoint.  GNU/Linux, RISC iX,
7679    and NetBSD all use a software interrupt rather than an undefined
7680    instruction to force a trap.  This can be handled by by the
7681    abi-specific code during establishment of the gdbarch vector.  */
7682
7683 #define ARM_LE_BREAKPOINT {0xFE,0xDE,0xFF,0xE7}
7684 #define ARM_BE_BREAKPOINT {0xE7,0xFF,0xDE,0xFE}
7685 #define THUMB_LE_BREAKPOINT {0xbe,0xbe}
7686 #define THUMB_BE_BREAKPOINT {0xbe,0xbe}
7687
7688 static const gdb_byte arm_default_arm_le_breakpoint[] = ARM_LE_BREAKPOINT;
7689 static const gdb_byte arm_default_arm_be_breakpoint[] = ARM_BE_BREAKPOINT;
7690 static const gdb_byte arm_default_thumb_le_breakpoint[] = THUMB_LE_BREAKPOINT;
7691 static const gdb_byte arm_default_thumb_be_breakpoint[] = THUMB_BE_BREAKPOINT;
7692
7693 /* Determine the type and size of breakpoint to insert at PCPTR.  Uses
7694    the program counter value to determine whether a 16-bit or 32-bit
7695    breakpoint should be used.  It returns a pointer to a string of
7696    bytes that encode a breakpoint instruction, stores the length of
7697    the string to *lenptr, and adjusts the program counter (if
7698    necessary) to point to the actual memory location where the
7699    breakpoint should be inserted.  */
7700
7701 static const unsigned char *
7702 arm_breakpoint_from_pc (struct gdbarch *gdbarch, CORE_ADDR *pcptr, int *lenptr)
7703 {
7704   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
7705   enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
7706
7707   if (arm_pc_is_thumb (gdbarch, *pcptr))
7708     {
7709       *pcptr = UNMAKE_THUMB_ADDR (*pcptr);
7710
7711       /* If we have a separate 32-bit breakpoint instruction for Thumb-2,
7712          check whether we are replacing a 32-bit instruction.  */
7713       if (tdep->thumb2_breakpoint != NULL)
7714         {
7715           gdb_byte buf[2];
7716           if (target_read_memory (*pcptr, buf, 2) == 0)
7717             {
7718               unsigned short inst1;
7719               inst1 = extract_unsigned_integer (buf, 2, byte_order_for_code);
7720               if (thumb_insn_size (inst1) == 4)
7721                 {
7722                   *lenptr = tdep->thumb2_breakpoint_size;
7723                   return tdep->thumb2_breakpoint;
7724                 }
7725             }
7726         }
7727
7728       *lenptr = tdep->thumb_breakpoint_size;
7729       return tdep->thumb_breakpoint;
7730     }
7731   else
7732     {
7733       *lenptr = tdep->arm_breakpoint_size;
7734       return tdep->arm_breakpoint;
7735     }
7736 }
7737
7738 static void
7739 arm_remote_breakpoint_from_pc (struct gdbarch *gdbarch, CORE_ADDR *pcptr,
7740                                int *kindptr)
7741 {
7742   arm_breakpoint_from_pc (gdbarch, pcptr, kindptr);
7743
7744   if (arm_pc_is_thumb (gdbarch, *pcptr) && *kindptr == 4)
7745     /* The documented magic value for a 32-bit Thumb-2 breakpoint, so
7746        that this is not confused with a 32-bit ARM breakpoint.  */
7747     *kindptr = 3;
7748 }
7749
7750 /* Extract from an array REGBUF containing the (raw) register state a
7751    function return value of type TYPE, and copy that, in virtual
7752    format, into VALBUF.  */
7753
7754 static void
7755 arm_extract_return_value (struct type *type, struct regcache *regs,
7756                           gdb_byte *valbuf)
7757 {
7758   struct gdbarch *gdbarch = get_regcache_arch (regs);
7759   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
7760
7761   if (TYPE_CODE_FLT == TYPE_CODE (type))
7762     {
7763       switch (gdbarch_tdep (gdbarch)->fp_model)
7764         {
7765         case ARM_FLOAT_FPA:
7766           {
7767             /* The value is in register F0 in internal format.  We need to
7768                extract the raw value and then convert it to the desired
7769                internal type.  */
7770             bfd_byte tmpbuf[FP_REGISTER_SIZE];
7771
7772             regcache_cooked_read (regs, ARM_F0_REGNUM, tmpbuf);
7773             convert_from_extended (floatformat_from_type (type), tmpbuf,
7774                                    valbuf, gdbarch_byte_order (gdbarch));
7775           }
7776           break;
7777
7778         case ARM_FLOAT_SOFT_FPA:
7779         case ARM_FLOAT_SOFT_VFP:
7780           /* ARM_FLOAT_VFP can arise if this is a variadic function so
7781              not using the VFP ABI code.  */
7782         case ARM_FLOAT_VFP:
7783           regcache_cooked_read (regs, ARM_A1_REGNUM, valbuf);
7784           if (TYPE_LENGTH (type) > 4)
7785             regcache_cooked_read (regs, ARM_A1_REGNUM + 1,
7786                                   valbuf + INT_REGISTER_SIZE);
7787           break;
7788
7789         default:
7790           internal_error (__FILE__, __LINE__,
7791                           _("arm_extract_return_value: "
7792                             "Floating point model not supported"));
7793           break;
7794         }
7795     }
7796   else if (TYPE_CODE (type) == TYPE_CODE_INT
7797            || TYPE_CODE (type) == TYPE_CODE_CHAR
7798            || TYPE_CODE (type) == TYPE_CODE_BOOL
7799            || TYPE_CODE (type) == TYPE_CODE_PTR
7800            || TYPE_CODE (type) == TYPE_CODE_REF
7801            || TYPE_CODE (type) == TYPE_CODE_ENUM)
7802     {
7803       /* If the type is a plain integer, then the access is
7804          straight-forward.  Otherwise we have to play around a bit
7805          more.  */
7806       int len = TYPE_LENGTH (type);
7807       int regno = ARM_A1_REGNUM;
7808       ULONGEST tmp;
7809
7810       while (len > 0)
7811         {
7812           /* By using store_unsigned_integer we avoid having to do
7813              anything special for small big-endian values.  */
7814           regcache_cooked_read_unsigned (regs, regno++, &tmp);
7815           store_unsigned_integer (valbuf, 
7816                                   (len > INT_REGISTER_SIZE
7817                                    ? INT_REGISTER_SIZE : len),
7818                                   byte_order, tmp);
7819           len -= INT_REGISTER_SIZE;
7820           valbuf += INT_REGISTER_SIZE;
7821         }
7822     }
7823   else
7824     {
7825       /* For a structure or union the behaviour is as if the value had
7826          been stored to word-aligned memory and then loaded into 
7827          registers with 32-bit load instruction(s).  */
7828       int len = TYPE_LENGTH (type);
7829       int regno = ARM_A1_REGNUM;
7830       bfd_byte tmpbuf[INT_REGISTER_SIZE];
7831
7832       while (len > 0)
7833         {
7834           regcache_cooked_read (regs, regno++, tmpbuf);
7835           memcpy (valbuf, tmpbuf,
7836                   len > INT_REGISTER_SIZE ? INT_REGISTER_SIZE : len);
7837           len -= INT_REGISTER_SIZE;
7838           valbuf += INT_REGISTER_SIZE;
7839         }
7840     }
7841 }
7842
7843
7844 /* Will a function return an aggregate type in memory or in a
7845    register?  Return 0 if an aggregate type can be returned in a
7846    register, 1 if it must be returned in memory.  */
7847
7848 static int
7849 arm_return_in_memory (struct gdbarch *gdbarch, struct type *type)
7850 {
7851   enum type_code code;
7852
7853   type = check_typedef (type);
7854
7855   /* Simple, non-aggregate types (ie not including vectors and
7856      complex) are always returned in a register (or registers).  */
7857   code = TYPE_CODE (type);
7858   if (TYPE_CODE_STRUCT != code && TYPE_CODE_UNION != code
7859       && TYPE_CODE_ARRAY != code && TYPE_CODE_COMPLEX != code)
7860     return 0;
7861
7862   if (TYPE_CODE_ARRAY == code && TYPE_VECTOR (type))
7863     {
7864       /* Vector values should be returned using ARM registers if they
7865          are not over 16 bytes.  */
7866       return (TYPE_LENGTH (type) > 16);
7867     }
7868
7869   if (gdbarch_tdep (gdbarch)->arm_abi != ARM_ABI_APCS)
7870     {
7871       /* The AAPCS says all aggregates not larger than a word are returned
7872          in a register.  */
7873       if (TYPE_LENGTH (type) <= INT_REGISTER_SIZE)
7874         return 0;
7875
7876       return 1;
7877     }
7878   else
7879     {
7880       int nRc;
7881
7882       /* All aggregate types that won't fit in a register must be returned
7883          in memory.  */
7884       if (TYPE_LENGTH (type) > INT_REGISTER_SIZE)
7885         return 1;
7886
7887       /* In the ARM ABI, "integer" like aggregate types are returned in
7888          registers.  For an aggregate type to be integer like, its size
7889          must be less than or equal to INT_REGISTER_SIZE and the
7890          offset of each addressable subfield must be zero.  Note that bit
7891          fields are not addressable, and all addressable subfields of
7892          unions always start at offset zero.
7893
7894          This function is based on the behaviour of GCC 2.95.1.
7895          See: gcc/arm.c: arm_return_in_memory() for details.
7896
7897          Note: All versions of GCC before GCC 2.95.2 do not set up the
7898          parameters correctly for a function returning the following
7899          structure: struct { float f;}; This should be returned in memory,
7900          not a register.  Richard Earnshaw sent me a patch, but I do not
7901          know of any way to detect if a function like the above has been
7902          compiled with the correct calling convention.  */
7903
7904       /* Assume all other aggregate types can be returned in a register.
7905          Run a check for structures, unions and arrays.  */
7906       nRc = 0;
7907
7908       if ((TYPE_CODE_STRUCT == code) || (TYPE_CODE_UNION == code))
7909         {
7910           int i;
7911           /* Need to check if this struct/union is "integer" like.  For
7912              this to be true, its size must be less than or equal to
7913              INT_REGISTER_SIZE and the offset of each addressable
7914              subfield must be zero.  Note that bit fields are not
7915              addressable, and unions always start at offset zero.  If any
7916              of the subfields is a floating point type, the struct/union
7917              cannot be an integer type.  */
7918
7919           /* For each field in the object, check:
7920              1) Is it FP? --> yes, nRc = 1;
7921              2) Is it addressable (bitpos != 0) and
7922              not packed (bitsize == 0)?
7923              --> yes, nRc = 1
7924           */
7925
7926           for (i = 0; i < TYPE_NFIELDS (type); i++)
7927             {
7928               enum type_code field_type_code;
7929
7930               field_type_code
7931                 = TYPE_CODE (check_typedef (TYPE_FIELD_TYPE (type,
7932                                                              i)));
7933
7934               /* Is it a floating point type field?  */
7935               if (field_type_code == TYPE_CODE_FLT)
7936                 {
7937                   nRc = 1;
7938                   break;
7939                 }
7940
7941               /* If bitpos != 0, then we have to care about it.  */
7942               if (TYPE_FIELD_BITPOS (type, i) != 0)
7943                 {
7944                   /* Bitfields are not addressable.  If the field bitsize is 
7945                      zero, then the field is not packed.  Hence it cannot be
7946                      a bitfield or any other packed type.  */
7947                   if (TYPE_FIELD_BITSIZE (type, i) == 0)
7948                     {
7949                       nRc = 1;
7950                       break;
7951                     }
7952                 }
7953             }
7954         }
7955
7956       return nRc;
7957     }
7958 }
7959
7960 /* Write into appropriate registers a function return value of type
7961    TYPE, given in virtual format.  */
7962
7963 static void
7964 arm_store_return_value (struct type *type, struct regcache *regs,
7965                         const gdb_byte *valbuf)
7966 {
7967   struct gdbarch *gdbarch = get_regcache_arch (regs);
7968   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
7969
7970   if (TYPE_CODE (type) == TYPE_CODE_FLT)
7971     {
7972       gdb_byte buf[MAX_REGISTER_SIZE];
7973
7974       switch (gdbarch_tdep (gdbarch)->fp_model)
7975         {
7976         case ARM_FLOAT_FPA:
7977
7978           convert_to_extended (floatformat_from_type (type), buf, valbuf,
7979                                gdbarch_byte_order (gdbarch));
7980           regcache_cooked_write (regs, ARM_F0_REGNUM, buf);
7981           break;
7982
7983         case ARM_FLOAT_SOFT_FPA:
7984         case ARM_FLOAT_SOFT_VFP:
7985           /* ARM_FLOAT_VFP can arise if this is a variadic function so
7986              not using the VFP ABI code.  */
7987         case ARM_FLOAT_VFP:
7988           regcache_cooked_write (regs, ARM_A1_REGNUM, valbuf);
7989           if (TYPE_LENGTH (type) > 4)
7990             regcache_cooked_write (regs, ARM_A1_REGNUM + 1, 
7991                                    valbuf + INT_REGISTER_SIZE);
7992           break;
7993
7994         default:
7995           internal_error (__FILE__, __LINE__,
7996                           _("arm_store_return_value: Floating "
7997                             "point model not supported"));
7998           break;
7999         }
8000     }
8001   else if (TYPE_CODE (type) == TYPE_CODE_INT
8002            || TYPE_CODE (type) == TYPE_CODE_CHAR
8003            || TYPE_CODE (type) == TYPE_CODE_BOOL
8004            || TYPE_CODE (type) == TYPE_CODE_PTR
8005            || TYPE_CODE (type) == TYPE_CODE_REF
8006            || TYPE_CODE (type) == TYPE_CODE_ENUM)
8007     {
8008       if (TYPE_LENGTH (type) <= 4)
8009         {
8010           /* Values of one word or less are zero/sign-extended and
8011              returned in r0.  */
8012           bfd_byte tmpbuf[INT_REGISTER_SIZE];
8013           LONGEST val = unpack_long (type, valbuf);
8014
8015           store_signed_integer (tmpbuf, INT_REGISTER_SIZE, byte_order, val);
8016           regcache_cooked_write (regs, ARM_A1_REGNUM, tmpbuf);
8017         }
8018       else
8019         {
8020           /* Integral values greater than one word are stored in consecutive
8021              registers starting with r0.  This will always be a multiple of
8022              the regiser size.  */
8023           int len = TYPE_LENGTH (type);
8024           int regno = ARM_A1_REGNUM;
8025
8026           while (len > 0)
8027             {
8028               regcache_cooked_write (regs, regno++, valbuf);
8029               len -= INT_REGISTER_SIZE;
8030               valbuf += INT_REGISTER_SIZE;
8031             }
8032         }
8033     }
8034   else
8035     {
8036       /* For a structure or union the behaviour is as if the value had
8037          been stored to word-aligned memory and then loaded into 
8038          registers with 32-bit load instruction(s).  */
8039       int len = TYPE_LENGTH (type);
8040       int regno = ARM_A1_REGNUM;
8041       bfd_byte tmpbuf[INT_REGISTER_SIZE];
8042
8043       while (len > 0)
8044         {
8045           memcpy (tmpbuf, valbuf,
8046                   len > INT_REGISTER_SIZE ? INT_REGISTER_SIZE : len);
8047           regcache_cooked_write (regs, regno++, tmpbuf);
8048           len -= INT_REGISTER_SIZE;
8049           valbuf += INT_REGISTER_SIZE;
8050         }
8051     }
8052 }
8053
8054
8055 /* Handle function return values.  */
8056
8057 static enum return_value_convention
8058 arm_return_value (struct gdbarch *gdbarch, struct value *function,
8059                   struct type *valtype, struct regcache *regcache,
8060                   gdb_byte *readbuf, const gdb_byte *writebuf)
8061 {
8062   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
8063   struct type *func_type = function ? value_type (function) : NULL;
8064   enum arm_vfp_cprc_base_type vfp_base_type;
8065   int vfp_base_count;
8066
8067   if (arm_vfp_abi_for_function (gdbarch, func_type)
8068       && arm_vfp_call_candidate (valtype, &vfp_base_type, &vfp_base_count))
8069     {
8070       int reg_char = arm_vfp_cprc_reg_char (vfp_base_type);
8071       int unit_length = arm_vfp_cprc_unit_length (vfp_base_type);
8072       int i;
8073       for (i = 0; i < vfp_base_count; i++)
8074         {
8075           if (reg_char == 'q')
8076             {
8077               if (writebuf)
8078                 arm_neon_quad_write (gdbarch, regcache, i,
8079                                      writebuf + i * unit_length);
8080
8081               if (readbuf)
8082                 arm_neon_quad_read (gdbarch, regcache, i,
8083                                     readbuf + i * unit_length);
8084             }
8085           else
8086             {
8087               char name_buf[4];
8088               int regnum;
8089
8090               xsnprintf (name_buf, sizeof (name_buf), "%c%d", reg_char, i);
8091               regnum = user_reg_map_name_to_regnum (gdbarch, name_buf,
8092                                                     strlen (name_buf));
8093               if (writebuf)
8094                 regcache_cooked_write (regcache, regnum,
8095                                        writebuf + i * unit_length);
8096               if (readbuf)
8097                 regcache_cooked_read (regcache, regnum,
8098                                       readbuf + i * unit_length);
8099             }
8100         }
8101       return RETURN_VALUE_REGISTER_CONVENTION;
8102     }
8103
8104   if (TYPE_CODE (valtype) == TYPE_CODE_STRUCT
8105       || TYPE_CODE (valtype) == TYPE_CODE_UNION
8106       || TYPE_CODE (valtype) == TYPE_CODE_ARRAY)
8107     {
8108       if (tdep->struct_return == pcc_struct_return
8109           || arm_return_in_memory (gdbarch, valtype))
8110         return RETURN_VALUE_STRUCT_CONVENTION;
8111     }
8112   else if (TYPE_CODE (valtype) == TYPE_CODE_COMPLEX)
8113     {
8114       if (arm_return_in_memory (gdbarch, valtype))
8115         return RETURN_VALUE_STRUCT_CONVENTION;
8116     }
8117
8118   if (writebuf)
8119     arm_store_return_value (valtype, regcache, writebuf);
8120
8121   if (readbuf)
8122     arm_extract_return_value (valtype, regcache, readbuf);
8123
8124   return RETURN_VALUE_REGISTER_CONVENTION;
8125 }
8126
8127
8128 static int
8129 arm_get_longjmp_target (struct frame_info *frame, CORE_ADDR *pc)
8130 {
8131   struct gdbarch *gdbarch = get_frame_arch (frame);
8132   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
8133   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
8134   CORE_ADDR jb_addr;
8135   gdb_byte buf[INT_REGISTER_SIZE];
8136   
8137   jb_addr = get_frame_register_unsigned (frame, ARM_A1_REGNUM);
8138
8139   if (target_read_memory (jb_addr + tdep->jb_pc * tdep->jb_elt_size, buf,
8140                           INT_REGISTER_SIZE))
8141     return 0;
8142
8143   *pc = extract_unsigned_integer (buf, INT_REGISTER_SIZE, byte_order);
8144   return 1;
8145 }
8146
8147 /* Recognize GCC and GNU ld's trampolines.  If we are in a trampoline,
8148    return the target PC.  Otherwise return 0.  */
8149
8150 CORE_ADDR
8151 arm_skip_stub (struct frame_info *frame, CORE_ADDR pc)
8152 {
8153   const char *name;
8154   int namelen;
8155   CORE_ADDR start_addr;
8156
8157   /* Find the starting address and name of the function containing the PC.  */
8158   if (find_pc_partial_function (pc, &name, &start_addr, NULL) == 0)
8159     {
8160       /* Trampoline 'bx reg' doesn't belong to any functions.  Do the
8161          check here.  */
8162       start_addr = arm_skip_bx_reg (frame, pc);
8163       if (start_addr != 0)
8164         return start_addr;
8165
8166       return 0;
8167     }
8168
8169   /* If PC is in a Thumb call or return stub, return the address of the
8170      target PC, which is in a register.  The thunk functions are called
8171      _call_via_xx, where x is the register name.  The possible names
8172      are r0-r9, sl, fp, ip, sp, and lr.  ARM RealView has similar
8173      functions, named __ARM_call_via_r[0-7].  */
8174   if (startswith (name, "_call_via_")
8175       || startswith (name, "__ARM_call_via_"))
8176     {
8177       /* Use the name suffix to determine which register contains the
8178          target PC.  */
8179       static char *table[15] =
8180       {"r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
8181        "r8", "r9", "sl", "fp", "ip", "sp", "lr"
8182       };
8183       int regno;
8184       int offset = strlen (name) - 2;
8185
8186       for (regno = 0; regno <= 14; regno++)
8187         if (strcmp (&name[offset], table[regno]) == 0)
8188           return get_frame_register_unsigned (frame, regno);
8189     }
8190
8191   /* GNU ld generates __foo_from_arm or __foo_from_thumb for
8192      non-interworking calls to foo.  We could decode the stubs
8193      to find the target but it's easier to use the symbol table.  */
8194   namelen = strlen (name);
8195   if (name[0] == '_' && name[1] == '_'
8196       && ((namelen > 2 + strlen ("_from_thumb")
8197            && startswith (name + namelen - strlen ("_from_thumb"), "_from_thumb"))
8198           || (namelen > 2 + strlen ("_from_arm")
8199               && startswith (name + namelen - strlen ("_from_arm"), "_from_arm"))))
8200     {
8201       char *target_name;
8202       int target_len = namelen - 2;
8203       struct bound_minimal_symbol minsym;
8204       struct objfile *objfile;
8205       struct obj_section *sec;
8206
8207       if (name[namelen - 1] == 'b')
8208         target_len -= strlen ("_from_thumb");
8209       else
8210         target_len -= strlen ("_from_arm");
8211
8212       target_name = (char *) alloca (target_len + 1);
8213       memcpy (target_name, name + 2, target_len);
8214       target_name[target_len] = '\0';
8215
8216       sec = find_pc_section (pc);
8217       objfile = (sec == NULL) ? NULL : sec->objfile;
8218       minsym = lookup_minimal_symbol (target_name, NULL, objfile);
8219       if (minsym.minsym != NULL)
8220         return BMSYMBOL_VALUE_ADDRESS (minsym);
8221       else
8222         return 0;
8223     }
8224
8225   return 0;                     /* not a stub */
8226 }
8227
8228 static void
8229 set_arm_command (char *args, int from_tty)
8230 {
8231   printf_unfiltered (_("\
8232 \"set arm\" must be followed by an apporpriate subcommand.\n"));
8233   help_list (setarmcmdlist, "set arm ", all_commands, gdb_stdout);
8234 }
8235
8236 static void
8237 show_arm_command (char *args, int from_tty)
8238 {
8239   cmd_show_list (showarmcmdlist, from_tty, "");
8240 }
8241
8242 static void
8243 arm_update_current_architecture (void)
8244 {
8245   struct gdbarch_info info;
8246
8247   /* If the current architecture is not ARM, we have nothing to do.  */
8248   if (gdbarch_bfd_arch_info (target_gdbarch ())->arch != bfd_arch_arm)
8249     return;
8250
8251   /* Update the architecture.  */
8252   gdbarch_info_init (&info);
8253
8254   if (!gdbarch_update_p (info))
8255     internal_error (__FILE__, __LINE__, _("could not update architecture"));
8256 }
8257
8258 static void
8259 set_fp_model_sfunc (char *args, int from_tty,
8260                     struct cmd_list_element *c)
8261 {
8262   int fp_model;
8263
8264   for (fp_model = ARM_FLOAT_AUTO; fp_model != ARM_FLOAT_LAST; fp_model++)
8265     if (strcmp (current_fp_model, fp_model_strings[fp_model]) == 0)
8266       {
8267         arm_fp_model = (enum arm_float_model) fp_model;
8268         break;
8269       }
8270
8271   if (fp_model == ARM_FLOAT_LAST)
8272     internal_error (__FILE__, __LINE__, _("Invalid fp model accepted: %s."),
8273                     current_fp_model);
8274
8275   arm_update_current_architecture ();
8276 }
8277
8278 static void
8279 show_fp_model (struct ui_file *file, int from_tty,
8280                struct cmd_list_element *c, const char *value)
8281 {
8282   struct gdbarch_tdep *tdep = gdbarch_tdep (target_gdbarch ());
8283
8284   if (arm_fp_model == ARM_FLOAT_AUTO
8285       && gdbarch_bfd_arch_info (target_gdbarch ())->arch == bfd_arch_arm)
8286     fprintf_filtered (file, _("\
8287 The current ARM floating point model is \"auto\" (currently \"%s\").\n"),
8288                       fp_model_strings[tdep->fp_model]);
8289   else
8290     fprintf_filtered (file, _("\
8291 The current ARM floating point model is \"%s\".\n"),
8292                       fp_model_strings[arm_fp_model]);
8293 }
8294
8295 static void
8296 arm_set_abi (char *args, int from_tty,
8297              struct cmd_list_element *c)
8298 {
8299   int arm_abi;
8300
8301   for (arm_abi = ARM_ABI_AUTO; arm_abi != ARM_ABI_LAST; arm_abi++)
8302     if (strcmp (arm_abi_string, arm_abi_strings[arm_abi]) == 0)
8303       {
8304         arm_abi_global = (enum arm_abi_kind) arm_abi;
8305         break;
8306       }
8307
8308   if (arm_abi == ARM_ABI_LAST)
8309     internal_error (__FILE__, __LINE__, _("Invalid ABI accepted: %s."),
8310                     arm_abi_string);
8311
8312   arm_update_current_architecture ();
8313 }
8314
8315 static void
8316 arm_show_abi (struct ui_file *file, int from_tty,
8317              struct cmd_list_element *c, const char *value)
8318 {
8319   struct gdbarch_tdep *tdep = gdbarch_tdep (target_gdbarch ());
8320
8321   if (arm_abi_global == ARM_ABI_AUTO
8322       && gdbarch_bfd_arch_info (target_gdbarch ())->arch == bfd_arch_arm)
8323     fprintf_filtered (file, _("\
8324 The current ARM ABI is \"auto\" (currently \"%s\").\n"),
8325                       arm_abi_strings[tdep->arm_abi]);
8326   else
8327     fprintf_filtered (file, _("The current ARM ABI is \"%s\".\n"),
8328                       arm_abi_string);
8329 }
8330
8331 static void
8332 arm_show_fallback_mode (struct ui_file *file, int from_tty,
8333                         struct cmd_list_element *c, const char *value)
8334 {
8335   fprintf_filtered (file,
8336                     _("The current execution mode assumed "
8337                       "(when symbols are unavailable) is \"%s\".\n"),
8338                     arm_fallback_mode_string);
8339 }
8340
8341 static void
8342 arm_show_force_mode (struct ui_file *file, int from_tty,
8343                      struct cmd_list_element *c, const char *value)
8344 {
8345   fprintf_filtered (file,
8346                     _("The current execution mode assumed "
8347                       "(even when symbols are available) is \"%s\".\n"),
8348                     arm_force_mode_string);
8349 }
8350
8351 /* If the user changes the register disassembly style used for info
8352    register and other commands, we have to also switch the style used
8353    in opcodes for disassembly output.  This function is run in the "set
8354    arm disassembly" command, and does that.  */
8355
8356 static void
8357 set_disassembly_style_sfunc (char *args, int from_tty,
8358                               struct cmd_list_element *c)
8359 {
8360   set_disassembly_style ();
8361 }
8362 \f
8363 /* Return the ARM register name corresponding to register I.  */
8364 static const char *
8365 arm_register_name (struct gdbarch *gdbarch, int i)
8366 {
8367   const int num_regs = gdbarch_num_regs (gdbarch);
8368
8369   if (gdbarch_tdep (gdbarch)->have_vfp_pseudos
8370       && i >= num_regs && i < num_regs + 32)
8371     {
8372       static const char *const vfp_pseudo_names[] = {
8373         "s0", "s1", "s2", "s3", "s4", "s5", "s6", "s7",
8374         "s8", "s9", "s10", "s11", "s12", "s13", "s14", "s15",
8375         "s16", "s17", "s18", "s19", "s20", "s21", "s22", "s23",
8376         "s24", "s25", "s26", "s27", "s28", "s29", "s30", "s31",
8377       };
8378
8379       return vfp_pseudo_names[i - num_regs];
8380     }
8381
8382   if (gdbarch_tdep (gdbarch)->have_neon_pseudos
8383       && i >= num_regs + 32 && i < num_regs + 32 + 16)
8384     {
8385       static const char *const neon_pseudo_names[] = {
8386         "q0", "q1", "q2", "q3", "q4", "q5", "q6", "q7",
8387         "q8", "q9", "q10", "q11", "q12", "q13", "q14", "q15",
8388       };
8389
8390       return neon_pseudo_names[i - num_regs - 32];
8391     }
8392
8393   if (i >= ARRAY_SIZE (arm_register_names))
8394     /* These registers are only supported on targets which supply
8395        an XML description.  */
8396     return "";
8397
8398   return arm_register_names[i];
8399 }
8400
8401 static void
8402 set_disassembly_style (void)
8403 {
8404   int current;
8405
8406   /* Find the style that the user wants.  */
8407   for (current = 0; current < num_disassembly_options; current++)
8408     if (disassembly_style == valid_disassembly_styles[current])
8409       break;
8410   gdb_assert (current < num_disassembly_options);
8411
8412   /* Synchronize the disassembler.  */
8413   set_arm_regname_option (current);
8414 }
8415
8416 /* Test whether the coff symbol specific value corresponds to a Thumb
8417    function.  */
8418
8419 static int
8420 coff_sym_is_thumb (int val)
8421 {
8422   return (val == C_THUMBEXT
8423           || val == C_THUMBSTAT
8424           || val == C_THUMBEXTFUNC
8425           || val == C_THUMBSTATFUNC
8426           || val == C_THUMBLABEL);
8427 }
8428
8429 /* arm_coff_make_msymbol_special()
8430    arm_elf_make_msymbol_special()
8431    
8432    These functions test whether the COFF or ELF symbol corresponds to
8433    an address in thumb code, and set a "special" bit in a minimal
8434    symbol to indicate that it does.  */
8435    
8436 static void
8437 arm_elf_make_msymbol_special(asymbol *sym, struct minimal_symbol *msym)
8438 {
8439   if (ARM_SYM_BRANCH_TYPE (&((elf_symbol_type *)sym)->internal_elf_sym)
8440       == ST_BRANCH_TO_THUMB)
8441     MSYMBOL_SET_SPECIAL (msym);
8442 }
8443
8444 static void
8445 arm_coff_make_msymbol_special(int val, struct minimal_symbol *msym)
8446 {
8447   if (coff_sym_is_thumb (val))
8448     MSYMBOL_SET_SPECIAL (msym);
8449 }
8450
8451 static void
8452 arm_objfile_data_free (struct objfile *objfile, void *arg)
8453 {
8454   struct arm_per_objfile *data = (struct arm_per_objfile *) arg;
8455   unsigned int i;
8456
8457   for (i = 0; i < objfile->obfd->section_count; i++)
8458     VEC_free (arm_mapping_symbol_s, data->section_maps[i]);
8459 }
8460
8461 static void
8462 arm_record_special_symbol (struct gdbarch *gdbarch, struct objfile *objfile,
8463                            asymbol *sym)
8464 {
8465   const char *name = bfd_asymbol_name (sym);
8466   struct arm_per_objfile *data;
8467   VEC(arm_mapping_symbol_s) **map_p;
8468   struct arm_mapping_symbol new_map_sym;
8469
8470   gdb_assert (name[0] == '$');
8471   if (name[1] != 'a' && name[1] != 't' && name[1] != 'd')
8472     return;
8473
8474   data = (struct arm_per_objfile *) objfile_data (objfile,
8475                                                   arm_objfile_data_key);
8476   if (data == NULL)
8477     {
8478       data = OBSTACK_ZALLOC (&objfile->objfile_obstack,
8479                              struct arm_per_objfile);
8480       set_objfile_data (objfile, arm_objfile_data_key, data);
8481       data->section_maps = OBSTACK_CALLOC (&objfile->objfile_obstack,
8482                                            objfile->obfd->section_count,
8483                                            VEC(arm_mapping_symbol_s) *);
8484     }
8485   map_p = &data->section_maps[bfd_get_section (sym)->index];
8486
8487   new_map_sym.value = sym->value;
8488   new_map_sym.type = name[1];
8489
8490   /* Assume that most mapping symbols appear in order of increasing
8491      value.  If they were randomly distributed, it would be faster to
8492      always push here and then sort at first use.  */
8493   if (!VEC_empty (arm_mapping_symbol_s, *map_p))
8494     {
8495       struct arm_mapping_symbol *prev_map_sym;
8496
8497       prev_map_sym = VEC_last (arm_mapping_symbol_s, *map_p);
8498       if (prev_map_sym->value >= sym->value)
8499         {
8500           unsigned int idx;
8501           idx = VEC_lower_bound (arm_mapping_symbol_s, *map_p, &new_map_sym,
8502                                  arm_compare_mapping_symbols);
8503           VEC_safe_insert (arm_mapping_symbol_s, *map_p, idx, &new_map_sym);
8504           return;
8505         }
8506     }
8507
8508   VEC_safe_push (arm_mapping_symbol_s, *map_p, &new_map_sym);
8509 }
8510
8511 static void
8512 arm_write_pc (struct regcache *regcache, CORE_ADDR pc)
8513 {
8514   struct gdbarch *gdbarch = get_regcache_arch (regcache);
8515   regcache_cooked_write_unsigned (regcache, ARM_PC_REGNUM, pc);
8516
8517   /* If necessary, set the T bit.  */
8518   if (arm_apcs_32)
8519     {
8520       ULONGEST val, t_bit;
8521       regcache_cooked_read_unsigned (regcache, ARM_PS_REGNUM, &val);
8522       t_bit = arm_psr_thumb_bit (gdbarch);
8523       if (arm_pc_is_thumb (gdbarch, pc))
8524         regcache_cooked_write_unsigned (regcache, ARM_PS_REGNUM,
8525                                         val | t_bit);
8526       else
8527         regcache_cooked_write_unsigned (regcache, ARM_PS_REGNUM,
8528                                         val & ~t_bit);
8529     }
8530 }
8531
8532 /* Read the contents of a NEON quad register, by reading from two
8533    double registers.  This is used to implement the quad pseudo
8534    registers, and for argument passing in case the quad registers are
8535    missing; vectors are passed in quad registers when using the VFP
8536    ABI, even if a NEON unit is not present.  REGNUM is the index of
8537    the quad register, in [0, 15].  */
8538
8539 static enum register_status
8540 arm_neon_quad_read (struct gdbarch *gdbarch, struct regcache *regcache,
8541                     int regnum, gdb_byte *buf)
8542 {
8543   char name_buf[4];
8544   gdb_byte reg_buf[8];
8545   int offset, double_regnum;
8546   enum register_status status;
8547
8548   xsnprintf (name_buf, sizeof (name_buf), "d%d", regnum << 1);
8549   double_regnum = user_reg_map_name_to_regnum (gdbarch, name_buf,
8550                                                strlen (name_buf));
8551
8552   /* d0 is always the least significant half of q0.  */
8553   if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
8554     offset = 8;
8555   else
8556     offset = 0;
8557
8558   status = regcache_raw_read (regcache, double_regnum, reg_buf);
8559   if (status != REG_VALID)
8560     return status;
8561   memcpy (buf + offset, reg_buf, 8);
8562
8563   offset = 8 - offset;
8564   status = regcache_raw_read (regcache, double_regnum + 1, reg_buf);
8565   if (status != REG_VALID)
8566     return status;
8567   memcpy (buf + offset, reg_buf, 8);
8568
8569   return REG_VALID;
8570 }
8571
8572 static enum register_status
8573 arm_pseudo_read (struct gdbarch *gdbarch, struct regcache *regcache,
8574                  int regnum, gdb_byte *buf)
8575 {
8576   const int num_regs = gdbarch_num_regs (gdbarch);
8577   char name_buf[4];
8578   gdb_byte reg_buf[8];
8579   int offset, double_regnum;
8580
8581   gdb_assert (regnum >= num_regs);
8582   regnum -= num_regs;
8583
8584   if (gdbarch_tdep (gdbarch)->have_neon_pseudos && regnum >= 32 && regnum < 48)
8585     /* Quad-precision register.  */
8586     return arm_neon_quad_read (gdbarch, regcache, regnum - 32, buf);
8587   else
8588     {
8589       enum register_status status;
8590
8591       /* Single-precision register.  */
8592       gdb_assert (regnum < 32);
8593
8594       /* s0 is always the least significant half of d0.  */
8595       if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
8596         offset = (regnum & 1) ? 0 : 4;
8597       else
8598         offset = (regnum & 1) ? 4 : 0;
8599
8600       xsnprintf (name_buf, sizeof (name_buf), "d%d", regnum >> 1);
8601       double_regnum = user_reg_map_name_to_regnum (gdbarch, name_buf,
8602                                                    strlen (name_buf));
8603
8604       status = regcache_raw_read (regcache, double_regnum, reg_buf);
8605       if (status == REG_VALID)
8606         memcpy (buf, reg_buf + offset, 4);
8607       return status;
8608     }
8609 }
8610
8611 /* Store the contents of BUF to a NEON quad register, by writing to
8612    two double registers.  This is used to implement the quad pseudo
8613    registers, and for argument passing in case the quad registers are
8614    missing; vectors are passed in quad registers when using the VFP
8615    ABI, even if a NEON unit is not present.  REGNUM is the index
8616    of the quad register, in [0, 15].  */
8617
8618 static void
8619 arm_neon_quad_write (struct gdbarch *gdbarch, struct regcache *regcache,
8620                      int regnum, const gdb_byte *buf)
8621 {
8622   char name_buf[4];
8623   int offset, double_regnum;
8624
8625   xsnprintf (name_buf, sizeof (name_buf), "d%d", regnum << 1);
8626   double_regnum = user_reg_map_name_to_regnum (gdbarch, name_buf,
8627                                                strlen (name_buf));
8628
8629   /* d0 is always the least significant half of q0.  */
8630   if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
8631     offset = 8;
8632   else
8633     offset = 0;
8634
8635   regcache_raw_write (regcache, double_regnum, buf + offset);
8636   offset = 8 - offset;
8637   regcache_raw_write (regcache, double_regnum + 1, buf + offset);
8638 }
8639
8640 static void
8641 arm_pseudo_write (struct gdbarch *gdbarch, struct regcache *regcache,
8642                   int regnum, const gdb_byte *buf)
8643 {
8644   const int num_regs = gdbarch_num_regs (gdbarch);
8645   char name_buf[4];
8646   gdb_byte reg_buf[8];
8647   int offset, double_regnum;
8648
8649   gdb_assert (regnum >= num_regs);
8650   regnum -= num_regs;
8651
8652   if (gdbarch_tdep (gdbarch)->have_neon_pseudos && regnum >= 32 && regnum < 48)
8653     /* Quad-precision register.  */
8654     arm_neon_quad_write (gdbarch, regcache, regnum - 32, buf);
8655   else
8656     {
8657       /* Single-precision register.  */
8658       gdb_assert (regnum < 32);
8659
8660       /* s0 is always the least significant half of d0.  */
8661       if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
8662         offset = (regnum & 1) ? 0 : 4;
8663       else
8664         offset = (regnum & 1) ? 4 : 0;
8665
8666       xsnprintf (name_buf, sizeof (name_buf), "d%d", regnum >> 1);
8667       double_regnum = user_reg_map_name_to_regnum (gdbarch, name_buf,
8668                                                    strlen (name_buf));
8669
8670       regcache_raw_read (regcache, double_regnum, reg_buf);
8671       memcpy (reg_buf + offset, buf, 4);
8672       regcache_raw_write (regcache, double_regnum, reg_buf);
8673     }
8674 }
8675
8676 static struct value *
8677 value_of_arm_user_reg (struct frame_info *frame, const void *baton)
8678 {
8679   const int *reg_p = (const int *) baton;
8680   return value_of_register (*reg_p, frame);
8681 }
8682 \f
8683 static enum gdb_osabi
8684 arm_elf_osabi_sniffer (bfd *abfd)
8685 {
8686   unsigned int elfosabi;
8687   enum gdb_osabi osabi = GDB_OSABI_UNKNOWN;
8688
8689   elfosabi = elf_elfheader (abfd)->e_ident[EI_OSABI];
8690
8691   if (elfosabi == ELFOSABI_ARM)
8692     /* GNU tools use this value.  Check note sections in this case,
8693        as well.  */
8694     bfd_map_over_sections (abfd,
8695                            generic_elf_osabi_sniff_abi_tag_sections, 
8696                            &osabi);
8697
8698   /* Anything else will be handled by the generic ELF sniffer.  */
8699   return osabi;
8700 }
8701
8702 static int
8703 arm_register_reggroup_p (struct gdbarch *gdbarch, int regnum,
8704                           struct reggroup *group)
8705 {
8706   /* FPS register's type is INT, but belongs to float_reggroup.  Beside
8707      this, FPS register belongs to save_regroup, restore_reggroup, and
8708      all_reggroup, of course.  */
8709   if (regnum == ARM_FPS_REGNUM)
8710     return (group == float_reggroup
8711             || group == save_reggroup
8712             || group == restore_reggroup
8713             || group == all_reggroup);
8714   else
8715     return default_register_reggroup_p (gdbarch, regnum, group);
8716 }
8717
8718 \f
8719 /* For backward-compatibility we allow two 'g' packet lengths with
8720    the remote protocol depending on whether FPA registers are
8721    supplied.  M-profile targets do not have FPA registers, but some
8722    stubs already exist in the wild which use a 'g' packet which
8723    supplies them albeit with dummy values.  The packet format which
8724    includes FPA registers should be considered deprecated for
8725    M-profile targets.  */
8726
8727 static void
8728 arm_register_g_packet_guesses (struct gdbarch *gdbarch)
8729 {
8730   if (gdbarch_tdep (gdbarch)->is_m)
8731     {
8732       /* If we know from the executable this is an M-profile target,
8733          cater for remote targets whose register set layout is the
8734          same as the FPA layout.  */
8735       register_remote_g_packet_guess (gdbarch,
8736                                       /* r0-r12,sp,lr,pc; f0-f7; fps,xpsr */
8737                                       (16 * INT_REGISTER_SIZE)
8738                                       + (8 * FP_REGISTER_SIZE)
8739                                       + (2 * INT_REGISTER_SIZE),
8740                                       tdesc_arm_with_m_fpa_layout);
8741
8742       /* The regular M-profile layout.  */
8743       register_remote_g_packet_guess (gdbarch,
8744                                       /* r0-r12,sp,lr,pc; xpsr */
8745                                       (16 * INT_REGISTER_SIZE)
8746                                       + INT_REGISTER_SIZE,
8747                                       tdesc_arm_with_m);
8748
8749       /* M-profile plus M4F VFP.  */
8750       register_remote_g_packet_guess (gdbarch,
8751                                       /* r0-r12,sp,lr,pc; d0-d15; fpscr,xpsr */
8752                                       (16 * INT_REGISTER_SIZE)
8753                                       + (16 * VFP_REGISTER_SIZE)
8754                                       + (2 * INT_REGISTER_SIZE),
8755                                       tdesc_arm_with_m_vfp_d16);
8756     }
8757
8758   /* Otherwise we don't have a useful guess.  */
8759 }
8760
8761 \f
8762 /* Initialize the current architecture based on INFO.  If possible,
8763    re-use an architecture from ARCHES, which is a list of
8764    architectures already created during this debugging session.
8765
8766    Called e.g. at program startup, when reading a core file, and when
8767    reading a binary file.  */
8768
8769 static struct gdbarch *
8770 arm_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
8771 {
8772   struct gdbarch_tdep *tdep;
8773   struct gdbarch *gdbarch;
8774   struct gdbarch_list *best_arch;
8775   enum arm_abi_kind arm_abi = arm_abi_global;
8776   enum arm_float_model fp_model = arm_fp_model;
8777   struct tdesc_arch_data *tdesc_data = NULL;
8778   int i, is_m = 0;
8779   int vfp_register_count = 0, have_vfp_pseudos = 0, have_neon_pseudos = 0;
8780   int have_wmmx_registers = 0;
8781   int have_neon = 0;
8782   int have_fpa_registers = 1;
8783   const struct target_desc *tdesc = info.target_desc;
8784
8785   /* If we have an object to base this architecture on, try to determine
8786      its ABI.  */
8787
8788   if (arm_abi == ARM_ABI_AUTO && info.abfd != NULL)
8789     {
8790       int ei_osabi, e_flags;
8791
8792       switch (bfd_get_flavour (info.abfd))
8793         {
8794         case bfd_target_aout_flavour:
8795           /* Assume it's an old APCS-style ABI.  */
8796           arm_abi = ARM_ABI_APCS;
8797           break;
8798
8799         case bfd_target_coff_flavour:
8800           /* Assume it's an old APCS-style ABI.  */
8801           /* XXX WinCE?  */
8802           arm_abi = ARM_ABI_APCS;
8803           break;
8804
8805         case bfd_target_elf_flavour:
8806           ei_osabi = elf_elfheader (info.abfd)->e_ident[EI_OSABI];
8807           e_flags = elf_elfheader (info.abfd)->e_flags;
8808
8809           if (ei_osabi == ELFOSABI_ARM)
8810             {
8811               /* GNU tools used to use this value, but do not for EABI
8812                  objects.  There's nowhere to tag an EABI version
8813                  anyway, so assume APCS.  */
8814               arm_abi = ARM_ABI_APCS;
8815             }
8816           else if (ei_osabi == ELFOSABI_NONE || ei_osabi == ELFOSABI_GNU)
8817             {
8818               int eabi_ver = EF_ARM_EABI_VERSION (e_flags);
8819               int attr_arch, attr_profile;
8820
8821               switch (eabi_ver)
8822                 {
8823                 case EF_ARM_EABI_UNKNOWN:
8824                   /* Assume GNU tools.  */
8825                   arm_abi = ARM_ABI_APCS;
8826                   break;
8827
8828                 case EF_ARM_EABI_VER4:
8829                 case EF_ARM_EABI_VER5:
8830                   arm_abi = ARM_ABI_AAPCS;
8831                   /* EABI binaries default to VFP float ordering.
8832                      They may also contain build attributes that can
8833                      be used to identify if the VFP argument-passing
8834                      ABI is in use.  */
8835                   if (fp_model == ARM_FLOAT_AUTO)
8836                     {
8837 #ifdef HAVE_ELF
8838                       switch (bfd_elf_get_obj_attr_int (info.abfd,
8839                                                         OBJ_ATTR_PROC,
8840                                                         Tag_ABI_VFP_args))
8841                         {
8842                         case AEABI_VFP_args_base:
8843                           /* "The user intended FP parameter/result
8844                              passing to conform to AAPCS, base
8845                              variant".  */
8846                           fp_model = ARM_FLOAT_SOFT_VFP;
8847                           break;
8848                         case AEABI_VFP_args_vfp:
8849                           /* "The user intended FP parameter/result
8850                              passing to conform to AAPCS, VFP
8851                              variant".  */
8852                           fp_model = ARM_FLOAT_VFP;
8853                           break;
8854                         case AEABI_VFP_args_toolchain:
8855                           /* "The user intended FP parameter/result
8856                              passing to conform to tool chain-specific
8857                              conventions" - we don't know any such
8858                              conventions, so leave it as "auto".  */
8859                           break;
8860                         case AEABI_VFP_args_compatible:
8861                           /* "Code is compatible with both the base
8862                              and VFP variants; the user did not permit
8863                              non-variadic functions to pass FP
8864                              parameters/results" - leave it as
8865                              "auto".  */
8866                           break;
8867                         default:
8868                           /* Attribute value not mentioned in the
8869                              November 2012 ABI, so leave it as
8870                              "auto".  */
8871                           break;
8872                         }
8873 #else
8874                       fp_model = ARM_FLOAT_SOFT_VFP;
8875 #endif
8876                     }
8877                   break;
8878
8879                 default:
8880                   /* Leave it as "auto".  */
8881                   warning (_("unknown ARM EABI version 0x%x"), eabi_ver);
8882                   break;
8883                 }
8884
8885 #ifdef HAVE_ELF
8886               /* Detect M-profile programs.  This only works if the
8887                  executable file includes build attributes; GCC does
8888                  copy them to the executable, but e.g. RealView does
8889                  not.  */
8890               attr_arch = bfd_elf_get_obj_attr_int (info.abfd, OBJ_ATTR_PROC,
8891                                                     Tag_CPU_arch);
8892               attr_profile = bfd_elf_get_obj_attr_int (info.abfd,
8893                                                        OBJ_ATTR_PROC,
8894                                                        Tag_CPU_arch_profile);
8895               /* GCC specifies the profile for v6-M; RealView only
8896                  specifies the profile for architectures starting with
8897                  V7 (as opposed to architectures with a tag
8898                  numerically greater than TAG_CPU_ARCH_V7).  */
8899               if (!tdesc_has_registers (tdesc)
8900                   && (attr_arch == TAG_CPU_ARCH_V6_M
8901                       || attr_arch == TAG_CPU_ARCH_V6S_M
8902                       || attr_profile == 'M'))
8903                 is_m = 1;
8904 #endif
8905             }
8906
8907           if (fp_model == ARM_FLOAT_AUTO)
8908             {
8909               int e_flags = elf_elfheader (info.abfd)->e_flags;
8910
8911               switch (e_flags & (EF_ARM_SOFT_FLOAT | EF_ARM_VFP_FLOAT))
8912                 {
8913                 case 0:
8914                   /* Leave it as "auto".  Strictly speaking this case
8915                      means FPA, but almost nobody uses that now, and
8916                      many toolchains fail to set the appropriate bits
8917                      for the floating-point model they use.  */
8918                   break;
8919                 case EF_ARM_SOFT_FLOAT:
8920                   fp_model = ARM_FLOAT_SOFT_FPA;
8921                   break;
8922                 case EF_ARM_VFP_FLOAT:
8923                   fp_model = ARM_FLOAT_VFP;
8924                   break;
8925                 case EF_ARM_SOFT_FLOAT | EF_ARM_VFP_FLOAT:
8926                   fp_model = ARM_FLOAT_SOFT_VFP;
8927                   break;
8928                 }
8929             }
8930
8931           if (e_flags & EF_ARM_BE8)
8932             info.byte_order_for_code = BFD_ENDIAN_LITTLE;
8933
8934           break;
8935
8936         default:
8937           /* Leave it as "auto".  */
8938           break;
8939         }
8940     }
8941
8942   /* Check any target description for validity.  */
8943   if (tdesc_has_registers (tdesc))
8944     {
8945       /* For most registers we require GDB's default names; but also allow
8946          the numeric names for sp / lr / pc, as a convenience.  */
8947       static const char *const arm_sp_names[] = { "r13", "sp", NULL };
8948       static const char *const arm_lr_names[] = { "r14", "lr", NULL };
8949       static const char *const arm_pc_names[] = { "r15", "pc", NULL };
8950
8951       const struct tdesc_feature *feature;
8952       int valid_p;
8953
8954       feature = tdesc_find_feature (tdesc,
8955                                     "org.gnu.gdb.arm.core");
8956       if (feature == NULL)
8957         {
8958           feature = tdesc_find_feature (tdesc,
8959                                         "org.gnu.gdb.arm.m-profile");
8960           if (feature == NULL)
8961             return NULL;
8962           else
8963             is_m = 1;
8964         }
8965
8966       tdesc_data = tdesc_data_alloc ();
8967
8968       valid_p = 1;
8969       for (i = 0; i < ARM_SP_REGNUM; i++)
8970         valid_p &= tdesc_numbered_register (feature, tdesc_data, i,
8971                                             arm_register_names[i]);
8972       valid_p &= tdesc_numbered_register_choices (feature, tdesc_data,
8973                                                   ARM_SP_REGNUM,
8974                                                   arm_sp_names);
8975       valid_p &= tdesc_numbered_register_choices (feature, tdesc_data,
8976                                                   ARM_LR_REGNUM,
8977                                                   arm_lr_names);
8978       valid_p &= tdesc_numbered_register_choices (feature, tdesc_data,
8979                                                   ARM_PC_REGNUM,
8980                                                   arm_pc_names);
8981       if (is_m)
8982         valid_p &= tdesc_numbered_register (feature, tdesc_data,
8983                                             ARM_PS_REGNUM, "xpsr");
8984       else
8985         valid_p &= tdesc_numbered_register (feature, tdesc_data,
8986                                             ARM_PS_REGNUM, "cpsr");
8987
8988       if (!valid_p)
8989         {
8990           tdesc_data_cleanup (tdesc_data);
8991           return NULL;
8992         }
8993
8994       feature = tdesc_find_feature (tdesc,
8995                                     "org.gnu.gdb.arm.fpa");
8996       if (feature != NULL)
8997         {
8998           valid_p = 1;
8999           for (i = ARM_F0_REGNUM; i <= ARM_FPS_REGNUM; i++)
9000             valid_p &= tdesc_numbered_register (feature, tdesc_data, i,
9001                                                 arm_register_names[i]);
9002           if (!valid_p)
9003             {
9004               tdesc_data_cleanup (tdesc_data);
9005               return NULL;
9006             }
9007         }
9008       else
9009         have_fpa_registers = 0;
9010
9011       feature = tdesc_find_feature (tdesc,
9012                                     "org.gnu.gdb.xscale.iwmmxt");
9013       if (feature != NULL)
9014         {
9015           static const char *const iwmmxt_names[] = {
9016             "wR0", "wR1", "wR2", "wR3", "wR4", "wR5", "wR6", "wR7",
9017             "wR8", "wR9", "wR10", "wR11", "wR12", "wR13", "wR14", "wR15",
9018             "wCID", "wCon", "wCSSF", "wCASF", "", "", "", "",
9019             "wCGR0", "wCGR1", "wCGR2", "wCGR3", "", "", "", "",
9020           };
9021
9022           valid_p = 1;
9023           for (i = ARM_WR0_REGNUM; i <= ARM_WR15_REGNUM; i++)
9024             valid_p
9025               &= tdesc_numbered_register (feature, tdesc_data, i,
9026                                           iwmmxt_names[i - ARM_WR0_REGNUM]);
9027
9028           /* Check for the control registers, but do not fail if they
9029              are missing.  */
9030           for (i = ARM_WC0_REGNUM; i <= ARM_WCASF_REGNUM; i++)
9031             tdesc_numbered_register (feature, tdesc_data, i,
9032                                      iwmmxt_names[i - ARM_WR0_REGNUM]);
9033
9034           for (i = ARM_WCGR0_REGNUM; i <= ARM_WCGR3_REGNUM; i++)
9035             valid_p
9036               &= tdesc_numbered_register (feature, tdesc_data, i,
9037                                           iwmmxt_names[i - ARM_WR0_REGNUM]);
9038
9039           if (!valid_p)
9040             {
9041               tdesc_data_cleanup (tdesc_data);
9042               return NULL;
9043             }
9044
9045           have_wmmx_registers = 1;
9046         }
9047
9048       /* If we have a VFP unit, check whether the single precision registers
9049          are present.  If not, then we will synthesize them as pseudo
9050          registers.  */
9051       feature = tdesc_find_feature (tdesc,
9052                                     "org.gnu.gdb.arm.vfp");
9053       if (feature != NULL)
9054         {
9055           static const char *const vfp_double_names[] = {
9056             "d0", "d1", "d2", "d3", "d4", "d5", "d6", "d7",
9057             "d8", "d9", "d10", "d11", "d12", "d13", "d14", "d15",
9058             "d16", "d17", "d18", "d19", "d20", "d21", "d22", "d23",
9059             "d24", "d25", "d26", "d27", "d28", "d29", "d30", "d31",
9060           };
9061
9062           /* Require the double precision registers.  There must be either
9063              16 or 32.  */
9064           valid_p = 1;
9065           for (i = 0; i < 32; i++)
9066             {
9067               valid_p &= tdesc_numbered_register (feature, tdesc_data,
9068                                                   ARM_D0_REGNUM + i,
9069                                                   vfp_double_names[i]);
9070               if (!valid_p)
9071                 break;
9072             }
9073           if (!valid_p && i == 16)
9074             valid_p = 1;
9075
9076           /* Also require FPSCR.  */
9077           valid_p &= tdesc_numbered_register (feature, tdesc_data,
9078                                               ARM_FPSCR_REGNUM, "fpscr");
9079           if (!valid_p)
9080             {
9081               tdesc_data_cleanup (tdesc_data);
9082               return NULL;
9083             }
9084
9085           if (tdesc_unnumbered_register (feature, "s0") == 0)
9086             have_vfp_pseudos = 1;
9087
9088           vfp_register_count = i;
9089
9090           /* If we have VFP, also check for NEON.  The architecture allows
9091              NEON without VFP (integer vector operations only), but GDB
9092              does not support that.  */
9093           feature = tdesc_find_feature (tdesc,
9094                                         "org.gnu.gdb.arm.neon");
9095           if (feature != NULL)
9096             {
9097               /* NEON requires 32 double-precision registers.  */
9098               if (i != 32)
9099                 {
9100                   tdesc_data_cleanup (tdesc_data);
9101                   return NULL;
9102                 }
9103
9104               /* If there are quad registers defined by the stub, use
9105                  their type; otherwise (normally) provide them with
9106                  the default type.  */
9107               if (tdesc_unnumbered_register (feature, "q0") == 0)
9108                 have_neon_pseudos = 1;
9109
9110               have_neon = 1;
9111             }
9112         }
9113     }
9114
9115   /* If there is already a candidate, use it.  */
9116   for (best_arch = gdbarch_list_lookup_by_info (arches, &info);
9117        best_arch != NULL;
9118        best_arch = gdbarch_list_lookup_by_info (best_arch->next, &info))
9119     {
9120       if (arm_abi != ARM_ABI_AUTO
9121           && arm_abi != gdbarch_tdep (best_arch->gdbarch)->arm_abi)
9122         continue;
9123
9124       if (fp_model != ARM_FLOAT_AUTO
9125           && fp_model != gdbarch_tdep (best_arch->gdbarch)->fp_model)
9126         continue;
9127
9128       /* There are various other properties in tdep that we do not
9129          need to check here: those derived from a target description,
9130          since gdbarches with a different target description are
9131          automatically disqualified.  */
9132
9133       /* Do check is_m, though, since it might come from the binary.  */
9134       if (is_m != gdbarch_tdep (best_arch->gdbarch)->is_m)
9135         continue;
9136
9137       /* Found a match.  */
9138       break;
9139     }
9140
9141   if (best_arch != NULL)
9142     {
9143       if (tdesc_data != NULL)
9144         tdesc_data_cleanup (tdesc_data);
9145       return best_arch->gdbarch;
9146     }
9147
9148   tdep = XCNEW (struct gdbarch_tdep);
9149   gdbarch = gdbarch_alloc (&info, tdep);
9150
9151   /* Record additional information about the architecture we are defining.
9152      These are gdbarch discriminators, like the OSABI.  */
9153   tdep->arm_abi = arm_abi;
9154   tdep->fp_model = fp_model;
9155   tdep->is_m = is_m;
9156   tdep->have_fpa_registers = have_fpa_registers;
9157   tdep->have_wmmx_registers = have_wmmx_registers;
9158   gdb_assert (vfp_register_count == 0
9159               || vfp_register_count == 16
9160               || vfp_register_count == 32);
9161   tdep->vfp_register_count = vfp_register_count;
9162   tdep->have_vfp_pseudos = have_vfp_pseudos;
9163   tdep->have_neon_pseudos = have_neon_pseudos;
9164   tdep->have_neon = have_neon;
9165
9166   arm_register_g_packet_guesses (gdbarch);
9167
9168   /* Breakpoints.  */
9169   switch (info.byte_order_for_code)
9170     {
9171     case BFD_ENDIAN_BIG:
9172       tdep->arm_breakpoint = arm_default_arm_be_breakpoint;
9173       tdep->arm_breakpoint_size = sizeof (arm_default_arm_be_breakpoint);
9174       tdep->thumb_breakpoint = arm_default_thumb_be_breakpoint;
9175       tdep->thumb_breakpoint_size = sizeof (arm_default_thumb_be_breakpoint);
9176
9177       break;
9178
9179     case BFD_ENDIAN_LITTLE:
9180       tdep->arm_breakpoint = arm_default_arm_le_breakpoint;
9181       tdep->arm_breakpoint_size = sizeof (arm_default_arm_le_breakpoint);
9182       tdep->thumb_breakpoint = arm_default_thumb_le_breakpoint;
9183       tdep->thumb_breakpoint_size = sizeof (arm_default_thumb_le_breakpoint);
9184
9185       break;
9186
9187     default:
9188       internal_error (__FILE__, __LINE__,
9189                       _("arm_gdbarch_init: bad byte order for float format"));
9190     }
9191
9192   /* On ARM targets char defaults to unsigned.  */
9193   set_gdbarch_char_signed (gdbarch, 0);
9194
9195   /* Note: for displaced stepping, this includes the breakpoint, and one word
9196      of additional scratch space.  This setting isn't used for anything beside
9197      displaced stepping at present.  */
9198   set_gdbarch_max_insn_length (gdbarch, 4 * DISPLACED_MODIFIED_INSNS);
9199
9200   /* This should be low enough for everything.  */
9201   tdep->lowest_pc = 0x20;
9202   tdep->jb_pc = -1;     /* Longjump support not enabled by default.  */
9203
9204   /* The default, for both APCS and AAPCS, is to return small
9205      structures in registers.  */
9206   tdep->struct_return = reg_struct_return;
9207
9208   set_gdbarch_push_dummy_call (gdbarch, arm_push_dummy_call);
9209   set_gdbarch_frame_align (gdbarch, arm_frame_align);
9210
9211   set_gdbarch_write_pc (gdbarch, arm_write_pc);
9212
9213   /* Frame handling.  */
9214   set_gdbarch_dummy_id (gdbarch, arm_dummy_id);
9215   set_gdbarch_unwind_pc (gdbarch, arm_unwind_pc);
9216   set_gdbarch_unwind_sp (gdbarch, arm_unwind_sp);
9217
9218   frame_base_set_default (gdbarch, &arm_normal_base);
9219
9220   /* Address manipulation.  */
9221   set_gdbarch_addr_bits_remove (gdbarch, arm_addr_bits_remove);
9222
9223   /* Advance PC across function entry code.  */
9224   set_gdbarch_skip_prologue (gdbarch, arm_skip_prologue);
9225
9226   /* Detect whether PC is at a point where the stack has been destroyed.  */
9227   set_gdbarch_stack_frame_destroyed_p (gdbarch, arm_stack_frame_destroyed_p);
9228
9229   /* Skip trampolines.  */
9230   set_gdbarch_skip_trampoline_code (gdbarch, arm_skip_stub);
9231
9232   /* The stack grows downward.  */
9233   set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
9234
9235   /* Breakpoint manipulation.  */
9236   set_gdbarch_breakpoint_from_pc (gdbarch, arm_breakpoint_from_pc);
9237   set_gdbarch_remote_breakpoint_from_pc (gdbarch,
9238                                          arm_remote_breakpoint_from_pc);
9239
9240   /* Information about registers, etc.  */
9241   set_gdbarch_sp_regnum (gdbarch, ARM_SP_REGNUM);
9242   set_gdbarch_pc_regnum (gdbarch, ARM_PC_REGNUM);
9243   set_gdbarch_num_regs (gdbarch, ARM_NUM_REGS);
9244   set_gdbarch_register_type (gdbarch, arm_register_type);
9245   set_gdbarch_register_reggroup_p (gdbarch, arm_register_reggroup_p);
9246
9247   /* This "info float" is FPA-specific.  Use the generic version if we
9248      do not have FPA.  */
9249   if (gdbarch_tdep (gdbarch)->have_fpa_registers)
9250     set_gdbarch_print_float_info (gdbarch, arm_print_float_info);
9251
9252   /* Internal <-> external register number maps.  */
9253   set_gdbarch_dwarf2_reg_to_regnum (gdbarch, arm_dwarf_reg_to_regnum);
9254   set_gdbarch_register_sim_regno (gdbarch, arm_register_sim_regno);
9255
9256   set_gdbarch_register_name (gdbarch, arm_register_name);
9257
9258   /* Returning results.  */
9259   set_gdbarch_return_value (gdbarch, arm_return_value);
9260
9261   /* Disassembly.  */
9262   set_gdbarch_print_insn (gdbarch, gdb_print_insn_arm);
9263
9264   /* Minsymbol frobbing.  */
9265   set_gdbarch_elf_make_msymbol_special (gdbarch, arm_elf_make_msymbol_special);
9266   set_gdbarch_coff_make_msymbol_special (gdbarch,
9267                                          arm_coff_make_msymbol_special);
9268   set_gdbarch_record_special_symbol (gdbarch, arm_record_special_symbol);
9269
9270   /* Thumb-2 IT block support.  */
9271   set_gdbarch_adjust_breakpoint_address (gdbarch,
9272                                          arm_adjust_breakpoint_address);
9273
9274   /* Virtual tables.  */
9275   set_gdbarch_vbit_in_delta (gdbarch, 1);
9276
9277   /* Hook in the ABI-specific overrides, if they have been registered.  */
9278   gdbarch_init_osabi (info, gdbarch);
9279
9280   dwarf2_frame_set_init_reg (gdbarch, arm_dwarf2_frame_init_reg);
9281
9282   /* Add some default predicates.  */
9283   if (is_m)
9284     frame_unwind_append_unwinder (gdbarch, &arm_m_exception_unwind);
9285   frame_unwind_append_unwinder (gdbarch, &arm_stub_unwind);
9286   dwarf2_append_unwinders (gdbarch);
9287   frame_unwind_append_unwinder (gdbarch, &arm_exidx_unwind);
9288   frame_unwind_append_unwinder (gdbarch, &arm_prologue_unwind);
9289
9290   /* Now we have tuned the configuration, set a few final things,
9291      based on what the OS ABI has told us.  */
9292
9293   /* If the ABI is not otherwise marked, assume the old GNU APCS.  EABI
9294      binaries are always marked.  */
9295   if (tdep->arm_abi == ARM_ABI_AUTO)
9296     tdep->arm_abi = ARM_ABI_APCS;
9297
9298   /* Watchpoints are not steppable.  */
9299   set_gdbarch_have_nonsteppable_watchpoint (gdbarch, 1);
9300
9301   /* We used to default to FPA for generic ARM, but almost nobody
9302      uses that now, and we now provide a way for the user to force
9303      the model.  So default to the most useful variant.  */
9304   if (tdep->fp_model == ARM_FLOAT_AUTO)
9305     tdep->fp_model = ARM_FLOAT_SOFT_FPA;
9306
9307   if (tdep->jb_pc >= 0)
9308     set_gdbarch_get_longjmp_target (gdbarch, arm_get_longjmp_target);
9309
9310   /* Floating point sizes and format.  */
9311   set_gdbarch_float_format (gdbarch, floatformats_ieee_single);
9312   if (tdep->fp_model == ARM_FLOAT_SOFT_FPA || tdep->fp_model == ARM_FLOAT_FPA)
9313     {
9314       set_gdbarch_double_format
9315         (gdbarch, floatformats_ieee_double_littlebyte_bigword);
9316       set_gdbarch_long_double_format
9317         (gdbarch, floatformats_ieee_double_littlebyte_bigword);
9318     }
9319   else
9320     {
9321       set_gdbarch_double_format (gdbarch, floatformats_ieee_double);
9322       set_gdbarch_long_double_format (gdbarch, floatformats_ieee_double);
9323     }
9324
9325   if (have_vfp_pseudos)
9326     {
9327       /* NOTE: These are the only pseudo registers used by
9328          the ARM target at the moment.  If more are added, a
9329          little more care in numbering will be needed.  */
9330
9331       int num_pseudos = 32;
9332       if (have_neon_pseudos)
9333         num_pseudos += 16;
9334       set_gdbarch_num_pseudo_regs (gdbarch, num_pseudos);
9335       set_gdbarch_pseudo_register_read (gdbarch, arm_pseudo_read);
9336       set_gdbarch_pseudo_register_write (gdbarch, arm_pseudo_write);
9337     }
9338
9339   if (tdesc_data)
9340     {
9341       set_tdesc_pseudo_register_name (gdbarch, arm_register_name);
9342
9343       tdesc_use_registers (gdbarch, tdesc, tdesc_data);
9344
9345       /* Override tdesc_register_type to adjust the types of VFP
9346          registers for NEON.  */
9347       set_gdbarch_register_type (gdbarch, arm_register_type);
9348     }
9349
9350   /* Add standard register aliases.  We add aliases even for those
9351      nanes which are used by the current architecture - it's simpler,
9352      and does no harm, since nothing ever lists user registers.  */
9353   for (i = 0; i < ARRAY_SIZE (arm_register_aliases); i++)
9354     user_reg_add (gdbarch, arm_register_aliases[i].name,
9355                   value_of_arm_user_reg, &arm_register_aliases[i].regnum);
9356
9357   return gdbarch;
9358 }
9359
9360 static void
9361 arm_dump_tdep (struct gdbarch *gdbarch, struct ui_file *file)
9362 {
9363   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
9364
9365   if (tdep == NULL)
9366     return;
9367
9368   fprintf_unfiltered (file, _("arm_dump_tdep: Lowest pc = 0x%lx"),
9369                       (unsigned long) tdep->lowest_pc);
9370 }
9371
9372 extern initialize_file_ftype _initialize_arm_tdep; /* -Wmissing-prototypes */
9373
9374 void
9375 _initialize_arm_tdep (void)
9376 {
9377   struct ui_file *stb;
9378   long length;
9379   const char *setname;
9380   const char *setdesc;
9381   const char *const *regnames;
9382   int i;
9383   static char *helptext;
9384   char regdesc[1024], *rdptr = regdesc;
9385   size_t rest = sizeof (regdesc);
9386
9387   gdbarch_register (bfd_arch_arm, arm_gdbarch_init, arm_dump_tdep);
9388
9389   arm_objfile_data_key
9390     = register_objfile_data_with_cleanup (NULL, arm_objfile_data_free);
9391
9392   /* Add ourselves to objfile event chain.  */
9393   observer_attach_new_objfile (arm_exidx_new_objfile);
9394   arm_exidx_data_key
9395     = register_objfile_data_with_cleanup (NULL, arm_exidx_data_free);
9396
9397   /* Register an ELF OS ABI sniffer for ARM binaries.  */
9398   gdbarch_register_osabi_sniffer (bfd_arch_arm,
9399                                   bfd_target_elf_flavour,
9400                                   arm_elf_osabi_sniffer);
9401
9402   /* Initialize the standard target descriptions.  */
9403   initialize_tdesc_arm_with_m ();
9404   initialize_tdesc_arm_with_m_fpa_layout ();
9405   initialize_tdesc_arm_with_m_vfp_d16 ();
9406   initialize_tdesc_arm_with_iwmmxt ();
9407   initialize_tdesc_arm_with_vfpv2 ();
9408   initialize_tdesc_arm_with_vfpv3 ();
9409   initialize_tdesc_arm_with_neon ();
9410
9411   /* Get the number of possible sets of register names defined in opcodes.  */
9412   num_disassembly_options = get_arm_regname_num_options ();
9413
9414   /* Add root prefix command for all "set arm"/"show arm" commands.  */
9415   add_prefix_cmd ("arm", no_class, set_arm_command,
9416                   _("Various ARM-specific commands."),
9417                   &setarmcmdlist, "set arm ", 0, &setlist);
9418
9419   add_prefix_cmd ("arm", no_class, show_arm_command,
9420                   _("Various ARM-specific commands."),
9421                   &showarmcmdlist, "show arm ", 0, &showlist);
9422
9423   /* Sync the opcode insn printer with our register viewer.  */
9424   parse_arm_disassembler_option ("reg-names-std");
9425
9426   /* Initialize the array that will be passed to
9427      add_setshow_enum_cmd().  */
9428   valid_disassembly_styles = XNEWVEC (const char *,
9429                                       num_disassembly_options + 1);
9430   for (i = 0; i < num_disassembly_options; i++)
9431     {
9432       get_arm_regnames (i, &setname, &setdesc, &regnames);
9433       valid_disassembly_styles[i] = setname;
9434       length = snprintf (rdptr, rest, "%s - %s\n", setname, setdesc);
9435       rdptr += length;
9436       rest -= length;
9437       /* When we find the default names, tell the disassembler to use
9438          them.  */
9439       if (!strcmp (setname, "std"))
9440         {
9441           disassembly_style = setname;
9442           set_arm_regname_option (i);
9443         }
9444     }
9445   /* Mark the end of valid options.  */
9446   valid_disassembly_styles[num_disassembly_options] = NULL;
9447
9448   /* Create the help text.  */
9449   stb = mem_fileopen ();
9450   fprintf_unfiltered (stb, "%s%s%s",
9451                       _("The valid values are:\n"),
9452                       regdesc,
9453                       _("The default is \"std\"."));
9454   helptext = ui_file_xstrdup (stb, NULL);
9455   ui_file_delete (stb);
9456
9457   add_setshow_enum_cmd("disassembler", no_class,
9458                        valid_disassembly_styles, &disassembly_style,
9459                        _("Set the disassembly style."),
9460                        _("Show the disassembly style."),
9461                        helptext,
9462                        set_disassembly_style_sfunc,
9463                        NULL, /* FIXME: i18n: The disassembly style is
9464                                 \"%s\".  */
9465                        &setarmcmdlist, &showarmcmdlist);
9466
9467   add_setshow_boolean_cmd ("apcs32", no_class, &arm_apcs_32,
9468                            _("Set usage of ARM 32-bit mode."),
9469                            _("Show usage of ARM 32-bit mode."),
9470                            _("When off, a 26-bit PC will be used."),
9471                            NULL,
9472                            NULL, /* FIXME: i18n: Usage of ARM 32-bit
9473                                     mode is %s.  */
9474                            &setarmcmdlist, &showarmcmdlist);
9475
9476   /* Add a command to allow the user to force the FPU model.  */
9477   add_setshow_enum_cmd ("fpu", no_class, fp_model_strings, &current_fp_model,
9478                         _("Set the floating point type."),
9479                         _("Show the floating point type."),
9480                         _("auto - Determine the FP typefrom the OS-ABI.\n\
9481 softfpa - Software FP, mixed-endian doubles on little-endian ARMs.\n\
9482 fpa - FPA co-processor (GCC compiled).\n\
9483 softvfp - Software FP with pure-endian doubles.\n\
9484 vfp - VFP co-processor."),
9485                         set_fp_model_sfunc, show_fp_model,
9486                         &setarmcmdlist, &showarmcmdlist);
9487
9488   /* Add a command to allow the user to force the ABI.  */
9489   add_setshow_enum_cmd ("abi", class_support, arm_abi_strings, &arm_abi_string,
9490                         _("Set the ABI."),
9491                         _("Show the ABI."),
9492                         NULL, arm_set_abi, arm_show_abi,
9493                         &setarmcmdlist, &showarmcmdlist);
9494
9495   /* Add two commands to allow the user to force the assumed
9496      execution mode.  */
9497   add_setshow_enum_cmd ("fallback-mode", class_support,
9498                         arm_mode_strings, &arm_fallback_mode_string,
9499                         _("Set the mode assumed when symbols are unavailable."),
9500                         _("Show the mode assumed when symbols are unavailable."),
9501                         NULL, NULL, arm_show_fallback_mode,
9502                         &setarmcmdlist, &showarmcmdlist);
9503   add_setshow_enum_cmd ("force-mode", class_support,
9504                         arm_mode_strings, &arm_force_mode_string,
9505                         _("Set the mode assumed even when symbols are available."),
9506                         _("Show the mode assumed even when symbols are available."),
9507                         NULL, NULL, arm_show_force_mode,
9508                         &setarmcmdlist, &showarmcmdlist);
9509
9510   /* Debugging flag.  */
9511   add_setshow_boolean_cmd ("arm", class_maintenance, &arm_debug,
9512                            _("Set ARM debugging."),
9513                            _("Show ARM debugging."),
9514                            _("When on, arm-specific debugging is enabled."),
9515                            NULL,
9516                            NULL, /* FIXME: i18n: "ARM debugging is %s.  */
9517                            &setdebuglist, &showdebuglist);
9518 }
9519
9520 /* ARM-reversible process record data structures.  */
9521
9522 #define ARM_INSN_SIZE_BYTES 4    
9523 #define THUMB_INSN_SIZE_BYTES 2
9524 #define THUMB2_INSN_SIZE_BYTES 4
9525
9526
9527 /* Position of the bit within a 32-bit ARM instruction
9528    that defines whether the instruction is a load or store.  */
9529 #define INSN_S_L_BIT_NUM 20
9530
9531 #define REG_ALLOC(REGS, LENGTH, RECORD_BUF) \
9532         do  \
9533           { \
9534             unsigned int reg_len = LENGTH; \
9535             if (reg_len) \
9536               { \
9537                 REGS = XNEWVEC (uint32_t, reg_len); \
9538                 memcpy(&REGS[0], &RECORD_BUF[0], sizeof(uint32_t)*LENGTH); \
9539               } \
9540           } \
9541         while (0)
9542
9543 #define MEM_ALLOC(MEMS, LENGTH, RECORD_BUF) \
9544         do  \
9545           { \
9546             unsigned int mem_len = LENGTH; \
9547             if (mem_len) \
9548             { \
9549               MEMS =  XNEWVEC (struct arm_mem_r, mem_len);  \
9550               memcpy(&MEMS->len, &RECORD_BUF[0], \
9551                      sizeof(struct arm_mem_r) * LENGTH); \
9552             } \
9553           } \
9554           while (0)
9555
9556 /* Checks whether insn is already recorded or yet to be decoded. (boolean expression).  */
9557 #define INSN_RECORDED(ARM_RECORD) \
9558         (0 != (ARM_RECORD)->reg_rec_count || 0 != (ARM_RECORD)->mem_rec_count)
9559
9560 /* ARM memory record structure.  */
9561 struct arm_mem_r
9562 {
9563   uint32_t len;    /* Record length.  */
9564   uint32_t addr;   /* Memory address.  */
9565 };
9566
9567 /* ARM instruction record contains opcode of current insn
9568    and execution state (before entry to decode_insn()),
9569    contains list of to-be-modified registers and
9570    memory blocks (on return from decode_insn()).  */
9571
9572 typedef struct insn_decode_record_t
9573 {
9574   struct gdbarch *gdbarch;
9575   struct regcache *regcache;
9576   CORE_ADDR this_addr;          /* Address of the insn being decoded.  */
9577   uint32_t arm_insn;            /* Should accommodate thumb.  */
9578   uint32_t cond;                /* Condition code.  */
9579   uint32_t opcode;              /* Insn opcode.  */
9580   uint32_t decode;              /* Insn decode bits.  */
9581   uint32_t mem_rec_count;       /* No of mem records.  */
9582   uint32_t reg_rec_count;       /* No of reg records.  */
9583   uint32_t *arm_regs;           /* Registers to be saved for this record.  */
9584   struct arm_mem_r *arm_mems;   /* Memory to be saved for this record.  */
9585 } insn_decode_record;
9586
9587
9588 /* Checks ARM SBZ and SBO mandatory fields.  */
9589
9590 static int
9591 sbo_sbz (uint32_t insn, uint32_t bit_num, uint32_t len, uint32_t sbo)
9592 {
9593   uint32_t ones = bits (insn, bit_num - 1, (bit_num -1) + (len - 1));
9594
9595   if (!len)
9596     return 1;
9597
9598   if (!sbo)
9599     ones = ~ones;
9600
9601   while (ones)
9602     {
9603       if (!(ones & sbo))
9604         {
9605           return 0;
9606         }
9607       ones = ones >> 1;
9608     }
9609   return 1;
9610 }
9611
9612 enum arm_record_result
9613 {
9614   ARM_RECORD_SUCCESS = 0,
9615   ARM_RECORD_FAILURE = 1
9616 };
9617
9618 typedef enum
9619 {
9620   ARM_RECORD_STRH=1,
9621   ARM_RECORD_STRD
9622 } arm_record_strx_t;
9623
9624 typedef enum
9625 {
9626   ARM_RECORD=1,
9627   THUMB_RECORD,
9628   THUMB2_RECORD
9629 } record_type_t;
9630
9631
9632 static int
9633 arm_record_strx (insn_decode_record *arm_insn_r, uint32_t *record_buf, 
9634                  uint32_t *record_buf_mem, arm_record_strx_t str_type)
9635 {
9636
9637   struct regcache *reg_cache = arm_insn_r->regcache;
9638   ULONGEST u_regval[2]= {0};
9639
9640   uint32_t reg_src1 = 0, reg_src2 = 0;
9641   uint32_t immed_high = 0, immed_low = 0,offset_8 = 0, tgt_mem_addr = 0;
9642
9643   arm_insn_r->opcode = bits (arm_insn_r->arm_insn, 21, 24);
9644   arm_insn_r->decode = bits (arm_insn_r->arm_insn, 4, 7);
9645
9646   if (14 == arm_insn_r->opcode || 10 == arm_insn_r->opcode)
9647     {
9648       /* 1) Handle misc store, immediate offset.  */
9649       immed_low = bits (arm_insn_r->arm_insn, 0, 3);
9650       immed_high = bits (arm_insn_r->arm_insn, 8, 11);
9651       reg_src1 = bits (arm_insn_r->arm_insn, 16, 19);
9652       regcache_raw_read_unsigned (reg_cache, reg_src1,
9653                                   &u_regval[0]);
9654       if (ARM_PC_REGNUM == reg_src1)
9655         {
9656           /* If R15 was used as Rn, hence current PC+8.  */
9657           u_regval[0] = u_regval[0] + 8;
9658         }
9659       offset_8 = (immed_high << 4) | immed_low;
9660       /* Calculate target store address.  */
9661       if (14 == arm_insn_r->opcode)
9662         {
9663           tgt_mem_addr = u_regval[0] + offset_8;
9664         }
9665       else
9666         {
9667           tgt_mem_addr = u_regval[0] - offset_8;
9668         }
9669       if (ARM_RECORD_STRH == str_type)
9670         {
9671           record_buf_mem[0] = 2;
9672           record_buf_mem[1] = tgt_mem_addr;
9673           arm_insn_r->mem_rec_count = 1;
9674         }
9675       else if (ARM_RECORD_STRD == str_type)
9676         {
9677           record_buf_mem[0] = 4;
9678           record_buf_mem[1] = tgt_mem_addr;
9679           record_buf_mem[2] = 4;
9680           record_buf_mem[3] = tgt_mem_addr + 4;
9681           arm_insn_r->mem_rec_count = 2;
9682         }
9683     }
9684   else if (12 == arm_insn_r->opcode || 8 == arm_insn_r->opcode)
9685     {
9686       /* 2) Store, register offset.  */
9687       /* Get Rm.  */
9688       reg_src1 = bits (arm_insn_r->arm_insn, 0, 3);
9689       /* Get Rn.  */
9690       reg_src2 = bits (arm_insn_r->arm_insn, 16, 19);
9691       regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval[0]);
9692       regcache_raw_read_unsigned (reg_cache, reg_src2, &u_regval[1]);
9693       if (15 == reg_src2)
9694         {
9695           /* If R15 was used as Rn, hence current PC+8.  */
9696           u_regval[0] = u_regval[0] + 8;
9697         }
9698       /* Calculate target store address, Rn +/- Rm, register offset.  */
9699       if (12 == arm_insn_r->opcode)
9700         {
9701           tgt_mem_addr = u_regval[0] + u_regval[1];
9702         }
9703       else
9704         {
9705           tgt_mem_addr = u_regval[1] - u_regval[0];
9706         }
9707       if (ARM_RECORD_STRH == str_type)
9708         {
9709           record_buf_mem[0] = 2;
9710           record_buf_mem[1] = tgt_mem_addr;
9711           arm_insn_r->mem_rec_count = 1;
9712         }
9713       else if (ARM_RECORD_STRD == str_type)
9714         {
9715           record_buf_mem[0] = 4;
9716           record_buf_mem[1] = tgt_mem_addr;
9717           record_buf_mem[2] = 4;
9718           record_buf_mem[3] = tgt_mem_addr + 4;
9719           arm_insn_r->mem_rec_count = 2;
9720         }
9721     }
9722   else if (11 == arm_insn_r->opcode || 15 == arm_insn_r->opcode
9723            || 2 == arm_insn_r->opcode  || 6 == arm_insn_r->opcode)
9724     {
9725       /* 3) Store, immediate pre-indexed.  */
9726       /* 5) Store, immediate post-indexed.  */
9727       immed_low = bits (arm_insn_r->arm_insn, 0, 3);
9728       immed_high = bits (arm_insn_r->arm_insn, 8, 11);
9729       offset_8 = (immed_high << 4) | immed_low;
9730       reg_src1 = bits (arm_insn_r->arm_insn, 16, 19);
9731       regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval[0]);
9732       /* Calculate target store address, Rn +/- Rm, register offset.  */
9733       if (15 == arm_insn_r->opcode || 6 == arm_insn_r->opcode)
9734         {
9735           tgt_mem_addr = u_regval[0] + offset_8;
9736         }
9737       else
9738         {
9739           tgt_mem_addr = u_regval[0] - offset_8;
9740         }
9741       if (ARM_RECORD_STRH == str_type)
9742         {
9743           record_buf_mem[0] = 2;
9744           record_buf_mem[1] = tgt_mem_addr;
9745           arm_insn_r->mem_rec_count = 1;
9746         }
9747       else if (ARM_RECORD_STRD == str_type)
9748         {
9749           record_buf_mem[0] = 4;
9750           record_buf_mem[1] = tgt_mem_addr;
9751           record_buf_mem[2] = 4;
9752           record_buf_mem[3] = tgt_mem_addr + 4;
9753           arm_insn_r->mem_rec_count = 2;
9754         }
9755       /* Record Rn also as it changes.  */
9756       *(record_buf) = bits (arm_insn_r->arm_insn, 16, 19);
9757       arm_insn_r->reg_rec_count = 1;
9758     }
9759   else if (9 == arm_insn_r->opcode || 13 == arm_insn_r->opcode
9760            || 0 == arm_insn_r->opcode || 4 == arm_insn_r->opcode)
9761     {
9762       /* 4) Store, register pre-indexed.  */
9763       /* 6) Store, register post -indexed.  */
9764       reg_src1 = bits (arm_insn_r->arm_insn, 0, 3);
9765       reg_src2 = bits (arm_insn_r->arm_insn, 16, 19);
9766       regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval[0]);
9767       regcache_raw_read_unsigned (reg_cache, reg_src2, &u_regval[1]);
9768       /* Calculate target store address, Rn +/- Rm, register offset.  */
9769       if (13 == arm_insn_r->opcode || 4 == arm_insn_r->opcode)
9770         {
9771           tgt_mem_addr = u_regval[0] + u_regval[1];
9772         }
9773       else
9774         {
9775           tgt_mem_addr = u_regval[1] - u_regval[0];
9776         }
9777       if (ARM_RECORD_STRH == str_type)
9778         {
9779           record_buf_mem[0] = 2;
9780           record_buf_mem[1] = tgt_mem_addr;
9781           arm_insn_r->mem_rec_count = 1;
9782         }
9783       else if (ARM_RECORD_STRD == str_type)
9784         {
9785           record_buf_mem[0] = 4;
9786           record_buf_mem[1] = tgt_mem_addr;
9787           record_buf_mem[2] = 4;
9788           record_buf_mem[3] = tgt_mem_addr + 4;
9789           arm_insn_r->mem_rec_count = 2;
9790         }
9791       /* Record Rn also as it changes.  */
9792       *(record_buf) = bits (arm_insn_r->arm_insn, 16, 19);
9793       arm_insn_r->reg_rec_count = 1;
9794     }
9795   return 0;
9796 }
9797
9798 /* Handling ARM extension space insns.  */
9799
9800 static int
9801 arm_record_extension_space (insn_decode_record *arm_insn_r)
9802 {
9803   uint32_t ret = 0;  /* Return value: -1:record failure ;  0:success  */
9804   uint32_t opcode1 = 0, opcode2 = 0, insn_op1 = 0;
9805   uint32_t record_buf[8], record_buf_mem[8];
9806   uint32_t reg_src1 = 0;
9807   struct regcache *reg_cache = arm_insn_r->regcache;
9808   ULONGEST u_regval = 0;
9809
9810   gdb_assert (!INSN_RECORDED(arm_insn_r));
9811   /* Handle unconditional insn extension space.  */
9812
9813   opcode1 = bits (arm_insn_r->arm_insn, 20, 27);
9814   opcode2 = bits (arm_insn_r->arm_insn, 4, 7);
9815   if (arm_insn_r->cond)
9816     {
9817       /* PLD has no affect on architectural state, it just affects
9818          the caches.  */
9819       if (5 == ((opcode1 & 0xE0) >> 5))
9820         {
9821           /* BLX(1) */
9822           record_buf[0] = ARM_PS_REGNUM;
9823           record_buf[1] = ARM_LR_REGNUM;
9824           arm_insn_r->reg_rec_count = 2;
9825         }
9826       /* STC2, LDC2, MCR2, MRC2, CDP2: <TBD>, co-processor insn.  */
9827     }
9828
9829
9830   opcode1 = bits (arm_insn_r->arm_insn, 25, 27);
9831   if (3 == opcode1 && bit (arm_insn_r->arm_insn, 4))
9832     {
9833       ret = -1;
9834       /* Undefined instruction on ARM V5; need to handle if later 
9835          versions define it.  */
9836     }
9837
9838   opcode1 = bits (arm_insn_r->arm_insn, 24, 27);
9839   opcode2 = bits (arm_insn_r->arm_insn, 4, 7);
9840   insn_op1 = bits (arm_insn_r->arm_insn, 20, 23);
9841
9842   /* Handle arithmetic insn extension space.  */
9843   if (!opcode1 && 9 == opcode2 && 1 != arm_insn_r->cond
9844       && !INSN_RECORDED(arm_insn_r))
9845     {
9846       /* Handle MLA(S) and MUL(S).  */
9847       if (0 <= insn_op1 && 3 >= insn_op1)
9848       {
9849         record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
9850         record_buf[1] = ARM_PS_REGNUM;
9851         arm_insn_r->reg_rec_count = 2;
9852       }
9853       else if (4 <= insn_op1 && 15 >= insn_op1)
9854       {
9855         /* Handle SMLAL(S), SMULL(S), UMLAL(S), UMULL(S).  */
9856         record_buf[0] = bits (arm_insn_r->arm_insn, 16, 19);
9857         record_buf[1] = bits (arm_insn_r->arm_insn, 12, 15);
9858         record_buf[2] = ARM_PS_REGNUM;
9859         arm_insn_r->reg_rec_count = 3;
9860       }
9861     }
9862
9863   opcode1 = bits (arm_insn_r->arm_insn, 26, 27);
9864   opcode2 = bits (arm_insn_r->arm_insn, 23, 24);
9865   insn_op1 = bits (arm_insn_r->arm_insn, 21, 22);
9866
9867   /* Handle control insn extension space.  */
9868
9869   if (!opcode1 && 2 == opcode2 && !bit (arm_insn_r->arm_insn, 20)
9870       && 1 != arm_insn_r->cond && !INSN_RECORDED(arm_insn_r))
9871     {
9872       if (!bit (arm_insn_r->arm_insn,25))
9873         {
9874           if (!bits (arm_insn_r->arm_insn, 4, 7))
9875             {
9876               if ((0 == insn_op1) || (2 == insn_op1))
9877                 {
9878                   /* MRS.  */
9879                   record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
9880                   arm_insn_r->reg_rec_count = 1;
9881                 }
9882               else if (1 == insn_op1)
9883                 {
9884                   /* CSPR is going to be changed.  */
9885                   record_buf[0] = ARM_PS_REGNUM;
9886                   arm_insn_r->reg_rec_count = 1;
9887                 }
9888               else if (3 == insn_op1)
9889                 {
9890                   /* SPSR is going to be changed.  */
9891                   /* We need to get SPSR value, which is yet to be done.  */
9892                   return -1;
9893                 }
9894             }
9895           else if (1 == bits (arm_insn_r->arm_insn, 4, 7))
9896             {
9897               if (1 == insn_op1)
9898                 {
9899                   /* BX.  */
9900                   record_buf[0] = ARM_PS_REGNUM;
9901                   arm_insn_r->reg_rec_count = 1;
9902                 }
9903               else if (3 == insn_op1)
9904                 {
9905                   /* CLZ.  */
9906                   record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
9907                   arm_insn_r->reg_rec_count = 1;
9908                 }
9909             }
9910           else if (3 == bits (arm_insn_r->arm_insn, 4, 7))
9911             {
9912               /* BLX.  */
9913               record_buf[0] = ARM_PS_REGNUM;
9914               record_buf[1] = ARM_LR_REGNUM;
9915               arm_insn_r->reg_rec_count = 2;
9916             }
9917           else if (5 == bits (arm_insn_r->arm_insn, 4, 7))
9918             {
9919               /* QADD, QSUB, QDADD, QDSUB */
9920               record_buf[0] = ARM_PS_REGNUM;
9921               record_buf[1] = bits (arm_insn_r->arm_insn, 12, 15);
9922               arm_insn_r->reg_rec_count = 2;
9923             }
9924           else if (7 == bits (arm_insn_r->arm_insn, 4, 7))
9925             {
9926               /* BKPT.  */
9927               record_buf[0] = ARM_PS_REGNUM;
9928               record_buf[1] = ARM_LR_REGNUM;
9929               arm_insn_r->reg_rec_count = 2;
9930
9931               /* Save SPSR also;how?  */
9932               return -1;
9933             }
9934           else if(8 == bits (arm_insn_r->arm_insn, 4, 7) 
9935                   || 10 == bits (arm_insn_r->arm_insn, 4, 7)
9936                   || 12 == bits (arm_insn_r->arm_insn, 4, 7)
9937                   || 14 == bits (arm_insn_r->arm_insn, 4, 7)
9938                  )
9939             {
9940               if (0 == insn_op1 || 1 == insn_op1)
9941                 {
9942                   /* SMLA<x><y>, SMLAW<y>, SMULW<y>.  */
9943                   /* We dont do optimization for SMULW<y> where we
9944                      need only Rd.  */
9945                   record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
9946                   record_buf[1] = ARM_PS_REGNUM;
9947                   arm_insn_r->reg_rec_count = 2;
9948                 }
9949               else if (2 == insn_op1)
9950                 {
9951                   /* SMLAL<x><y>.  */
9952                   record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
9953                   record_buf[1] = bits (arm_insn_r->arm_insn, 16, 19);
9954                   arm_insn_r->reg_rec_count = 2;
9955                 }
9956               else if (3 == insn_op1)
9957                 {
9958                   /* SMUL<x><y>.  */
9959                   record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
9960                   arm_insn_r->reg_rec_count = 1;
9961                 }
9962             }
9963         }
9964       else
9965         {
9966           /* MSR : immediate form.  */
9967           if (1 == insn_op1)
9968             {
9969               /* CSPR is going to be changed.  */
9970               record_buf[0] = ARM_PS_REGNUM;
9971               arm_insn_r->reg_rec_count = 1;
9972             }
9973           else if (3 == insn_op1)
9974             {
9975               /* SPSR is going to be changed.  */
9976               /* we need to get SPSR value, which is yet to be done  */
9977               return -1;
9978             }
9979         }
9980     }
9981
9982   opcode1 = bits (arm_insn_r->arm_insn, 25, 27);
9983   opcode2 = bits (arm_insn_r->arm_insn, 20, 24);
9984   insn_op1 = bits (arm_insn_r->arm_insn, 5, 6);
9985
9986   /* Handle load/store insn extension space.  */
9987
9988   if (!opcode1 && bit (arm_insn_r->arm_insn, 7) 
9989       && bit (arm_insn_r->arm_insn, 4) && 1 != arm_insn_r->cond
9990       && !INSN_RECORDED(arm_insn_r))
9991     {
9992       /* SWP/SWPB.  */
9993       if (0 == insn_op1)
9994         {
9995           /* These insn, changes register and memory as well.  */
9996           /* SWP or SWPB insn.  */
9997           /* Get memory address given by Rn.  */
9998           reg_src1 = bits (arm_insn_r->arm_insn, 16, 19);
9999           regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval);
10000           /* SWP insn ?, swaps word.  */
10001           if (8 == arm_insn_r->opcode)
10002             {
10003               record_buf_mem[0] = 4;
10004             }
10005           else
10006             {
10007               /* SWPB insn, swaps only byte.  */
10008               record_buf_mem[0] = 1;
10009             }
10010           record_buf_mem[1] = u_regval;
10011           arm_insn_r->mem_rec_count = 1;
10012           record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
10013           arm_insn_r->reg_rec_count = 1;
10014         }
10015       else if (1 == insn_op1 && !bit (arm_insn_r->arm_insn, 20))
10016         {
10017           /* STRH.  */
10018           arm_record_strx(arm_insn_r, &record_buf[0], &record_buf_mem[0],
10019                           ARM_RECORD_STRH);
10020         }
10021       else if (2 == insn_op1 && !bit (arm_insn_r->arm_insn, 20))
10022         {
10023           /* LDRD.  */
10024           record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
10025           record_buf[1] = record_buf[0] + 1;
10026           arm_insn_r->reg_rec_count = 2;
10027         }
10028       else if (3 == insn_op1 && !bit (arm_insn_r->arm_insn, 20))
10029         {
10030           /* STRD.  */
10031           arm_record_strx(arm_insn_r, &record_buf[0], &record_buf_mem[0],
10032                         ARM_RECORD_STRD);
10033         }
10034       else if (bit (arm_insn_r->arm_insn, 20) && insn_op1 <= 3)
10035         {
10036           /* LDRH, LDRSB, LDRSH.  */
10037           record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
10038           arm_insn_r->reg_rec_count = 1;
10039         }
10040
10041     }
10042
10043   opcode1 = bits (arm_insn_r->arm_insn, 23, 27);
10044   if (24 == opcode1 && bit (arm_insn_r->arm_insn, 21)
10045       && !INSN_RECORDED(arm_insn_r))
10046     {
10047       ret = -1;
10048       /* Handle coprocessor insn extension space.  */
10049     }
10050
10051   /* To be done for ARMv5 and later; as of now we return -1.  */
10052   if (-1 == ret)
10053     return ret;
10054
10055   REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
10056   MEM_ALLOC (arm_insn_r->arm_mems, arm_insn_r->mem_rec_count, record_buf_mem);
10057
10058   return ret;
10059 }
10060
10061 /* Handling opcode 000 insns.  */
10062
10063 static int
10064 arm_record_data_proc_misc_ld_str (insn_decode_record *arm_insn_r)
10065 {
10066   struct regcache *reg_cache = arm_insn_r->regcache;
10067   uint32_t record_buf[8], record_buf_mem[8];
10068   ULONGEST u_regval[2] = {0};
10069
10070   uint32_t reg_src1 = 0, reg_dest = 0;
10071   uint32_t opcode1 = 0;
10072
10073   arm_insn_r->opcode = bits (arm_insn_r->arm_insn, 21, 24);
10074   arm_insn_r->decode = bits (arm_insn_r->arm_insn, 4, 7);
10075   opcode1 = bits (arm_insn_r->arm_insn, 20, 24);
10076
10077   /* Data processing insn /multiply insn.  */
10078   if (9 == arm_insn_r->decode
10079       && ((4 <= arm_insn_r->opcode && 7 >= arm_insn_r->opcode)
10080       ||  (0 == arm_insn_r->opcode || 1 == arm_insn_r->opcode)))
10081     {
10082       /* Handle multiply instructions.  */
10083       /* MLA, MUL, SMLAL, SMULL, UMLAL, UMULL.  */
10084         if (0 == arm_insn_r->opcode || 1 == arm_insn_r->opcode)
10085           {
10086             /* Handle MLA and MUL.  */
10087             record_buf[0] = bits (arm_insn_r->arm_insn, 16, 19);
10088             record_buf[1] = ARM_PS_REGNUM;
10089             arm_insn_r->reg_rec_count = 2;
10090           }
10091         else if (4 <= arm_insn_r->opcode && 7 >= arm_insn_r->opcode)
10092           {
10093             /* Handle SMLAL, SMULL, UMLAL, UMULL.  */
10094             record_buf[0] = bits (arm_insn_r->arm_insn, 16, 19);
10095             record_buf[1] = bits (arm_insn_r->arm_insn, 12, 15);
10096             record_buf[2] = ARM_PS_REGNUM;
10097             arm_insn_r->reg_rec_count = 3;
10098           }
10099     }
10100   else if (bit (arm_insn_r->arm_insn, INSN_S_L_BIT_NUM)
10101            && (11 == arm_insn_r->decode || 13 == arm_insn_r->decode))
10102     {
10103       /* Handle misc load insns, as 20th bit  (L = 1).  */
10104       /* LDR insn has a capability to do branching, if
10105          MOV LR, PC is precceded by LDR insn having Rn as R15
10106          in that case, it emulates branch and link insn, and hence we 
10107          need to save CSPR and PC as well. I am not sure this is right
10108          place; as opcode = 010 LDR insn make this happen, if R15 was
10109          used.  */
10110       reg_dest = bits (arm_insn_r->arm_insn, 12, 15);
10111       if (15 != reg_dest)
10112         {
10113           record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
10114           arm_insn_r->reg_rec_count = 1;
10115         }
10116       else
10117         {
10118           record_buf[0] = reg_dest;
10119           record_buf[1] = ARM_PS_REGNUM;
10120           arm_insn_r->reg_rec_count = 2;
10121         }
10122     }
10123   else if ((9 == arm_insn_r->opcode || 11 == arm_insn_r->opcode)
10124            && sbo_sbz (arm_insn_r->arm_insn, 5, 12, 0)
10125            && sbo_sbz (arm_insn_r->arm_insn, 13, 4, 1)
10126            && 2 == bits (arm_insn_r->arm_insn, 20, 21))
10127     {
10128       /* Handle MSR insn.  */
10129       if (9 == arm_insn_r->opcode)
10130         {
10131           /* CSPR is going to be changed.  */
10132           record_buf[0] = ARM_PS_REGNUM;
10133           arm_insn_r->reg_rec_count = 1;
10134         }
10135       else
10136         {
10137           /* SPSR is going to be changed.  */
10138           /* How to read SPSR value?  */
10139           return -1;
10140         }
10141     }
10142   else if (9 == arm_insn_r->decode
10143            && (8 == arm_insn_r->opcode || 10 == arm_insn_r->opcode)
10144            && !bit (arm_insn_r->arm_insn, INSN_S_L_BIT_NUM))
10145     {
10146       /* Handling SWP, SWPB.  */
10147       /* These insn, changes register and memory as well.  */
10148       /* SWP or SWPB insn.  */
10149
10150       reg_src1 = bits (arm_insn_r->arm_insn, 16, 19);
10151       regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval[0]);
10152       /* SWP insn ?, swaps word.  */
10153       if (8 == arm_insn_r->opcode)
10154         {
10155           record_buf_mem[0] = 4;
10156         }
10157         else
10158         {
10159           /* SWPB insn, swaps only byte.  */
10160           record_buf_mem[0] = 1;
10161         }
10162       record_buf_mem[1] = u_regval[0];
10163       arm_insn_r->mem_rec_count = 1;
10164       record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
10165       arm_insn_r->reg_rec_count = 1;
10166     }
10167   else if (3 == arm_insn_r->decode && 0x12 == opcode1
10168            && sbo_sbz (arm_insn_r->arm_insn, 9, 12, 1))
10169     {
10170       /* Handle BLX, branch and link/exchange.  */
10171       if (9 == arm_insn_r->opcode)
10172       {
10173         /* Branch is chosen by setting T bit of CSPR, bitp[0] of Rm,
10174            and R14 stores the return address.  */
10175         record_buf[0] = ARM_PS_REGNUM;
10176         record_buf[1] = ARM_LR_REGNUM;
10177         arm_insn_r->reg_rec_count = 2;
10178       }
10179     }
10180   else if (7 == arm_insn_r->decode && 0x12 == opcode1)
10181     {
10182       /* Handle enhanced software breakpoint insn, BKPT.  */
10183       /* CPSR is changed to be executed in ARM state,  disabling normal
10184          interrupts, entering abort mode.  */
10185       /* According to high vector configuration PC is set.  */
10186       /* user hit breakpoint and type reverse, in
10187          that case, we need to go back with previous CPSR and
10188          Program Counter.  */
10189       record_buf[0] = ARM_PS_REGNUM;
10190       record_buf[1] = ARM_LR_REGNUM;
10191       arm_insn_r->reg_rec_count = 2;
10192
10193       /* Save SPSR also; how?  */
10194       return -1;
10195     }
10196   else if (11 == arm_insn_r->decode
10197            && !bit (arm_insn_r->arm_insn, INSN_S_L_BIT_NUM))
10198   {
10199     /* Handle enhanced store insns and DSP insns (e.g. LDRD).  */
10200
10201     /* Handle str(x) insn */
10202     arm_record_strx(arm_insn_r, &record_buf[0], &record_buf_mem[0],
10203                     ARM_RECORD_STRH);
10204   }
10205   else if (1 == arm_insn_r->decode && 0x12 == opcode1
10206            && sbo_sbz (arm_insn_r->arm_insn, 9, 12, 1))
10207     {
10208       /* Handle BX, branch and link/exchange.  */
10209       /* Branch is chosen by setting T bit of CSPR, bitp[0] of Rm.  */
10210       record_buf[0] = ARM_PS_REGNUM;
10211       arm_insn_r->reg_rec_count = 1;
10212     }
10213   else if (1 == arm_insn_r->decode && 0x16 == opcode1
10214            && sbo_sbz (arm_insn_r->arm_insn, 9, 4, 1)
10215            && sbo_sbz (arm_insn_r->arm_insn, 17, 4, 1))
10216     {
10217       /* Count leading zeros: CLZ.  */
10218       record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
10219       arm_insn_r->reg_rec_count = 1;
10220     }
10221   else if (!bit (arm_insn_r->arm_insn, INSN_S_L_BIT_NUM)
10222            && (8 == arm_insn_r->opcode || 10 == arm_insn_r->opcode)
10223            && sbo_sbz (arm_insn_r->arm_insn, 17, 4, 1)
10224            && sbo_sbz (arm_insn_r->arm_insn, 1, 12, 0)
10225           )
10226     {
10227       /* Handle MRS insn.  */
10228       record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
10229       arm_insn_r->reg_rec_count = 1;
10230     }
10231   else if (arm_insn_r->opcode <= 15)
10232     {
10233       /* Normal data processing insns.  */
10234       /* Out of 11 shifter operands mode, all the insn modifies destination
10235          register, which is specified by 13-16 decode.  */
10236       record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
10237       record_buf[1] = ARM_PS_REGNUM;
10238       arm_insn_r->reg_rec_count = 2;
10239     }
10240   else
10241     {
10242       return -1;
10243     }
10244
10245   REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
10246   MEM_ALLOC (arm_insn_r->arm_mems, arm_insn_r->mem_rec_count, record_buf_mem);
10247   return 0;
10248 }
10249
10250 /* Handling opcode 001 insns.  */
10251
10252 static int
10253 arm_record_data_proc_imm (insn_decode_record *arm_insn_r)
10254 {
10255   uint32_t record_buf[8], record_buf_mem[8];
10256
10257   arm_insn_r->opcode = bits (arm_insn_r->arm_insn, 21, 24);
10258   arm_insn_r->decode = bits (arm_insn_r->arm_insn, 4, 7);
10259
10260   if ((9 == arm_insn_r->opcode || 11 == arm_insn_r->opcode)
10261       && 2 == bits (arm_insn_r->arm_insn, 20, 21)
10262       && sbo_sbz (arm_insn_r->arm_insn, 13, 4, 1)
10263      )
10264     {
10265       /* Handle MSR insn.  */
10266       if (9 == arm_insn_r->opcode)
10267         {
10268           /* CSPR is going to be changed.  */
10269           record_buf[0] = ARM_PS_REGNUM;
10270           arm_insn_r->reg_rec_count = 1;
10271         }
10272       else
10273         {
10274           /* SPSR is going to be changed.  */
10275         }
10276     }
10277   else if (arm_insn_r->opcode <= 15)
10278     {
10279       /* Normal data processing insns.  */
10280       /* Out of 11 shifter operands mode, all the insn modifies destination
10281          register, which is specified by 13-16 decode.  */
10282       record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
10283       record_buf[1] = ARM_PS_REGNUM;
10284       arm_insn_r->reg_rec_count = 2;
10285     }
10286   else
10287     {
10288       return -1;
10289     }
10290
10291   REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
10292   MEM_ALLOC (arm_insn_r->arm_mems, arm_insn_r->mem_rec_count, record_buf_mem);
10293   return 0;
10294 }
10295
10296 static int
10297 arm_record_media (insn_decode_record *arm_insn_r)
10298 {
10299   uint32_t record_buf[8];
10300
10301   switch (bits (arm_insn_r->arm_insn, 22, 24))
10302     {
10303     case 0:
10304       /* Parallel addition and subtraction, signed */
10305     case 1:
10306       /* Parallel addition and subtraction, unsigned */
10307     case 2:
10308     case 3:
10309       /* Packing, unpacking, saturation and reversal */
10310       {
10311         int rd = bits (arm_insn_r->arm_insn, 12, 15);
10312
10313         record_buf[arm_insn_r->reg_rec_count++] = rd;
10314       }
10315       break;
10316
10317     case 4:
10318     case 5:
10319       /* Signed multiplies */
10320       {
10321         int rd = bits (arm_insn_r->arm_insn, 16, 19);
10322         unsigned int op1 = bits (arm_insn_r->arm_insn, 20, 22);
10323
10324         record_buf[arm_insn_r->reg_rec_count++] = rd;
10325         if (op1 == 0x0)
10326           record_buf[arm_insn_r->reg_rec_count++] = ARM_PS_REGNUM;
10327         else if (op1 == 0x4)
10328           record_buf[arm_insn_r->reg_rec_count++]
10329             = bits (arm_insn_r->arm_insn, 12, 15);
10330       }
10331       break;
10332
10333     case 6:
10334       {
10335         if (bit (arm_insn_r->arm_insn, 21)
10336             && bits (arm_insn_r->arm_insn, 5, 6) == 0x2)
10337           {
10338             /* SBFX */
10339             record_buf[arm_insn_r->reg_rec_count++]
10340               = bits (arm_insn_r->arm_insn, 12, 15);
10341           }
10342         else if (bits (arm_insn_r->arm_insn, 20, 21) == 0x0
10343                  && bits (arm_insn_r->arm_insn, 5, 7) == 0x0)
10344           {
10345             /* USAD8 and USADA8 */
10346             record_buf[arm_insn_r->reg_rec_count++]
10347               = bits (arm_insn_r->arm_insn, 16, 19);
10348           }
10349       }
10350       break;
10351
10352     case 7:
10353       {
10354         if (bits (arm_insn_r->arm_insn, 20, 21) == 0x3
10355             && bits (arm_insn_r->arm_insn, 5, 7) == 0x7)
10356           {
10357             /* Permanently UNDEFINED */
10358             return -1;
10359           }
10360         else
10361           {
10362             /* BFC, BFI and UBFX */
10363             record_buf[arm_insn_r->reg_rec_count++]
10364               = bits (arm_insn_r->arm_insn, 12, 15);
10365           }
10366       }
10367       break;
10368
10369     default:
10370       return -1;
10371     }
10372
10373   REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
10374
10375   return 0;
10376 }
10377
10378 /* Handle ARM mode instructions with opcode 010.  */
10379
10380 static int
10381 arm_record_ld_st_imm_offset (insn_decode_record *arm_insn_r)
10382 {
10383   struct regcache *reg_cache = arm_insn_r->regcache;
10384
10385   uint32_t reg_base , reg_dest;
10386   uint32_t offset_12, tgt_mem_addr;
10387   uint32_t record_buf[8], record_buf_mem[8];
10388   unsigned char wback;
10389   ULONGEST u_regval;
10390
10391   /* Calculate wback.  */
10392   wback = (bit (arm_insn_r->arm_insn, 24) == 0)
10393           || (bit (arm_insn_r->arm_insn, 21) == 1);
10394
10395   arm_insn_r->reg_rec_count = 0;
10396   reg_base = bits (arm_insn_r->arm_insn, 16, 19);
10397
10398   if (bit (arm_insn_r->arm_insn, INSN_S_L_BIT_NUM))
10399     {
10400       /* LDR (immediate), LDR (literal), LDRB (immediate), LDRB (literal), LDRBT
10401          and LDRT.  */
10402
10403       reg_dest = bits (arm_insn_r->arm_insn, 12, 15);
10404       record_buf[arm_insn_r->reg_rec_count++] = reg_dest;
10405
10406       /* The LDR instruction is capable of doing branching.  If MOV LR, PC
10407          preceeds a LDR instruction having R15 as reg_base, it
10408          emulates a branch and link instruction, and hence we need to save
10409          CPSR and PC as well.  */
10410       if (ARM_PC_REGNUM == reg_dest)
10411         record_buf[arm_insn_r->reg_rec_count++] = ARM_PS_REGNUM;
10412
10413       /* If wback is true, also save the base register, which is going to be
10414          written to.  */
10415       if (wback)
10416         record_buf[arm_insn_r->reg_rec_count++] = reg_base;
10417     }
10418   else
10419     {
10420       /* STR (immediate), STRB (immediate), STRBT and STRT.  */
10421
10422       offset_12 = bits (arm_insn_r->arm_insn, 0, 11);
10423       regcache_raw_read_unsigned (reg_cache, reg_base, &u_regval);
10424
10425       /* Handle bit U.  */
10426       if (bit (arm_insn_r->arm_insn, 23))
10427         {
10428           /* U == 1: Add the offset. */
10429           tgt_mem_addr = (uint32_t) u_regval + offset_12;
10430         }
10431       else
10432         {
10433           /* U == 0: subtract the offset. */
10434           tgt_mem_addr = (uint32_t) u_regval - offset_12;
10435         }
10436
10437       /* Bit 22 tells us whether the store instruction writes 1 byte or 4
10438          bytes.  */
10439       if (bit (arm_insn_r->arm_insn, 22))
10440         {
10441           /* STRB and STRBT: 1 byte.  */
10442           record_buf_mem[0] = 1;
10443         }
10444       else
10445         {
10446           /* STR and STRT: 4 bytes.  */
10447           record_buf_mem[0] = 4;
10448         }
10449
10450       /* Handle bit P.  */
10451       if (bit (arm_insn_r->arm_insn, 24))
10452         record_buf_mem[1] = tgt_mem_addr;
10453       else
10454         record_buf_mem[1] = (uint32_t) u_regval;
10455
10456       arm_insn_r->mem_rec_count = 1;
10457
10458       /* If wback is true, also save the base register, which is going to be
10459          written to.  */
10460       if (wback)
10461         record_buf[arm_insn_r->reg_rec_count++] = reg_base;
10462     }
10463
10464   REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
10465   MEM_ALLOC (arm_insn_r->arm_mems, arm_insn_r->mem_rec_count, record_buf_mem);
10466   return 0;
10467 }
10468
10469 /* Handling opcode 011 insns.  */
10470
10471 static int
10472 arm_record_ld_st_reg_offset (insn_decode_record *arm_insn_r)
10473 {
10474   struct regcache *reg_cache = arm_insn_r->regcache;
10475
10476   uint32_t shift_imm = 0;
10477   uint32_t reg_src1 = 0, reg_src2 = 0, reg_dest = 0;
10478   uint32_t offset_12 = 0, tgt_mem_addr = 0;
10479   uint32_t record_buf[8], record_buf_mem[8];
10480
10481   LONGEST s_word;
10482   ULONGEST u_regval[2];
10483
10484   if (bit (arm_insn_r->arm_insn, 4))
10485     return arm_record_media (arm_insn_r);
10486
10487   arm_insn_r->opcode = bits (arm_insn_r->arm_insn, 21, 24);
10488   arm_insn_r->decode = bits (arm_insn_r->arm_insn, 4, 7);
10489
10490   /* Handle enhanced store insns and LDRD DSP insn,
10491      order begins according to addressing modes for store insns
10492      STRH insn.  */
10493
10494   /* LDR or STR?  */
10495   if (bit (arm_insn_r->arm_insn, INSN_S_L_BIT_NUM))
10496     {
10497       reg_dest = bits (arm_insn_r->arm_insn, 12, 15);
10498       /* LDR insn has a capability to do branching, if
10499          MOV LR, PC is precedded by LDR insn having Rn as R15
10500          in that case, it emulates branch and link insn, and hence we
10501          need to save CSPR and PC as well.  */
10502       if (15 != reg_dest)
10503         {
10504           record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
10505           arm_insn_r->reg_rec_count = 1;
10506         }
10507       else
10508         {
10509           record_buf[0] = reg_dest;
10510           record_buf[1] = ARM_PS_REGNUM;
10511           arm_insn_r->reg_rec_count = 2;
10512         }
10513     }
10514   else
10515     {
10516       if (! bits (arm_insn_r->arm_insn, 4, 11))
10517         {
10518           /* Store insn, register offset and register pre-indexed,
10519              register post-indexed.  */
10520           /* Get Rm.  */
10521           reg_src1 = bits (arm_insn_r->arm_insn, 0, 3);
10522           /* Get Rn.  */
10523           reg_src2 = bits (arm_insn_r->arm_insn, 16, 19);
10524           regcache_raw_read_unsigned (reg_cache, reg_src1
10525                                       , &u_regval[0]);
10526           regcache_raw_read_unsigned (reg_cache, reg_src2
10527                                       , &u_regval[1]);
10528           if (15 == reg_src2)
10529             {
10530               /* If R15 was used as Rn, hence current PC+8.  */
10531               /* Pre-indexed mode doesnt reach here ; illegal insn.  */
10532                 u_regval[0] = u_regval[0] + 8;
10533             }
10534           /* Calculate target store address, Rn +/- Rm, register offset.  */
10535           /* U == 1.  */
10536           if (bit (arm_insn_r->arm_insn, 23))
10537             {
10538               tgt_mem_addr = u_regval[0] + u_regval[1];
10539             }
10540           else
10541             {
10542               tgt_mem_addr = u_regval[1] - u_regval[0];
10543             }
10544
10545           switch (arm_insn_r->opcode)
10546             {
10547               /* STR.  */
10548               case 8:
10549               case 12:
10550               /* STR.  */    
10551               case 9:
10552               case 13:
10553               /* STRT.  */
10554               case 1:
10555               case 5:
10556               /* STR.  */
10557               case 0:
10558               case 4:
10559                 record_buf_mem[0] = 4;
10560               break;
10561
10562               /* STRB.  */
10563               case 10:
10564               case 14:
10565               /* STRB.  */
10566               case 11:
10567               case 15:
10568               /* STRBT.  */    
10569               case 3:
10570               case 7:
10571               /* STRB.  */
10572               case 2:
10573               case 6:
10574                 record_buf_mem[0] = 1;
10575               break;
10576
10577               default:
10578                 gdb_assert_not_reached ("no decoding pattern found");
10579               break;
10580             }
10581           record_buf_mem[1] = tgt_mem_addr;
10582           arm_insn_r->mem_rec_count = 1;
10583
10584           if (9 == arm_insn_r->opcode || 11 == arm_insn_r->opcode
10585               || 13 == arm_insn_r->opcode || 15 == arm_insn_r->opcode
10586               || 0 == arm_insn_r->opcode || 2 == arm_insn_r->opcode
10587               || 4 == arm_insn_r->opcode || 6 == arm_insn_r->opcode
10588               || 1 == arm_insn_r->opcode || 3 == arm_insn_r->opcode
10589               || 5 == arm_insn_r->opcode || 7 == arm_insn_r->opcode
10590              )
10591             {
10592               /* Rn is going to be changed in pre-indexed mode and
10593                  post-indexed mode as well.  */
10594               record_buf[0] = reg_src2;
10595               arm_insn_r->reg_rec_count = 1;
10596             }
10597         }
10598       else
10599         {
10600           /* Store insn, scaled register offset; scaled pre-indexed.  */
10601           offset_12 = bits (arm_insn_r->arm_insn, 5, 6);
10602           /* Get Rm.  */
10603           reg_src1 = bits (arm_insn_r->arm_insn, 0, 3);
10604           /* Get Rn.  */
10605           reg_src2 = bits (arm_insn_r->arm_insn, 16, 19);
10606           /* Get shift_imm.  */
10607           shift_imm = bits (arm_insn_r->arm_insn, 7, 11);
10608           regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval[0]);
10609           regcache_raw_read_signed (reg_cache, reg_src1, &s_word);
10610           regcache_raw_read_unsigned (reg_cache, reg_src2, &u_regval[1]);
10611           /* Offset_12 used as shift.  */
10612           switch (offset_12)
10613             {
10614               case 0:
10615                 /* Offset_12 used as index.  */
10616                 offset_12 = u_regval[0] << shift_imm;
10617               break;
10618
10619               case 1:
10620                 offset_12 = (!shift_imm)?0:u_regval[0] >> shift_imm;
10621               break;
10622
10623               case 2:
10624                 if (!shift_imm)
10625                   {
10626                     if (bit (u_regval[0], 31))
10627                       {
10628                         offset_12 = 0xFFFFFFFF;
10629                       }
10630                     else
10631                       {
10632                         offset_12 = 0;
10633                       }
10634                   }
10635                 else
10636                   {
10637                     /* This is arithmetic shift.  */
10638                     offset_12 = s_word >> shift_imm;
10639                   }
10640                 break;
10641
10642               case 3:
10643                 if (!shift_imm)
10644                   {
10645                     regcache_raw_read_unsigned (reg_cache, ARM_PS_REGNUM,
10646                                                 &u_regval[1]);
10647                     /* Get C flag value and shift it by 31.  */
10648                     offset_12 = (((bit (u_regval[1], 29)) << 31) \
10649                                   | (u_regval[0]) >> 1);
10650                   }
10651                 else
10652                   {
10653                     offset_12 = (u_regval[0] >> shift_imm) \
10654                                 | (u_regval[0] <<
10655                                 (sizeof(uint32_t) - shift_imm));
10656                   }
10657               break;
10658
10659               default:
10660                 gdb_assert_not_reached ("no decoding pattern found");
10661               break;
10662             }
10663
10664           regcache_raw_read_unsigned (reg_cache, reg_src2, &u_regval[1]);
10665           /* bit U set.  */
10666           if (bit (arm_insn_r->arm_insn, 23))
10667             {
10668               tgt_mem_addr = u_regval[1] + offset_12;
10669             }
10670           else
10671             {
10672               tgt_mem_addr = u_regval[1] - offset_12;
10673             }
10674
10675           switch (arm_insn_r->opcode)
10676             {
10677               /* STR.  */
10678               case 8:
10679               case 12:
10680               /* STR.  */    
10681               case 9:
10682               case 13:
10683               /* STRT.  */
10684               case 1:
10685               case 5:
10686               /* STR.  */
10687               case 0:
10688               case 4:
10689                 record_buf_mem[0] = 4;
10690               break;
10691
10692               /* STRB.  */
10693               case 10:
10694               case 14:
10695               /* STRB.  */
10696               case 11:
10697               case 15:
10698               /* STRBT.  */    
10699               case 3:
10700               case 7:
10701               /* STRB.  */
10702               case 2:
10703               case 6:
10704                 record_buf_mem[0] = 1;
10705               break;
10706
10707               default:
10708                 gdb_assert_not_reached ("no decoding pattern found");
10709               break;
10710             }
10711           record_buf_mem[1] = tgt_mem_addr;
10712           arm_insn_r->mem_rec_count = 1;
10713
10714           if (9 == arm_insn_r->opcode || 11 == arm_insn_r->opcode
10715               || 13 == arm_insn_r->opcode || 15 == arm_insn_r->opcode
10716               || 0 == arm_insn_r->opcode || 2 == arm_insn_r->opcode
10717               || 4 == arm_insn_r->opcode || 6 == arm_insn_r->opcode
10718               || 1 == arm_insn_r->opcode || 3 == arm_insn_r->opcode
10719               || 5 == arm_insn_r->opcode || 7 == arm_insn_r->opcode
10720              )
10721             {
10722               /* Rn is going to be changed in register scaled pre-indexed
10723                  mode,and scaled post indexed mode.  */
10724               record_buf[0] = reg_src2;
10725               arm_insn_r->reg_rec_count = 1;
10726             }
10727         }
10728     }
10729
10730   REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
10731   MEM_ALLOC (arm_insn_r->arm_mems, arm_insn_r->mem_rec_count, record_buf_mem);
10732   return 0;
10733 }
10734
10735 /* Handle ARM mode instructions with opcode 100.  */
10736
10737 static int
10738 arm_record_ld_st_multiple (insn_decode_record *arm_insn_r)
10739 {
10740   struct regcache *reg_cache = arm_insn_r->regcache;
10741   uint32_t register_count = 0, register_bits;
10742   uint32_t reg_base, addr_mode;
10743   uint32_t record_buf[24], record_buf_mem[48];
10744   uint32_t wback;
10745   ULONGEST u_regval;
10746
10747   /* Fetch the list of registers.  */
10748   register_bits = bits (arm_insn_r->arm_insn, 0, 15);
10749   arm_insn_r->reg_rec_count = 0;
10750
10751   /* Fetch the base register that contains the address we are loading data
10752      to.  */
10753   reg_base = bits (arm_insn_r->arm_insn, 16, 19);
10754
10755   /* Calculate wback.  */
10756   wback = (bit (arm_insn_r->arm_insn, 21) == 1);
10757
10758   if (bit (arm_insn_r->arm_insn, INSN_S_L_BIT_NUM))
10759     {
10760       /* LDM/LDMIA/LDMFD, LDMDA/LDMFA, LDMDB and LDMIB.  */
10761
10762       /* Find out which registers are going to be loaded from memory.  */
10763       while (register_bits)
10764         {
10765           if (register_bits & 0x00000001)
10766             record_buf[arm_insn_r->reg_rec_count++] = register_count;
10767           register_bits = register_bits >> 1;
10768           register_count++;
10769         }
10770
10771   
10772       /* If wback is true, also save the base register, which is going to be
10773          written to.  */
10774       if (wback)
10775         record_buf[arm_insn_r->reg_rec_count++] = reg_base;
10776
10777       /* Save the CPSR register.  */
10778       record_buf[arm_insn_r->reg_rec_count++] = ARM_PS_REGNUM;
10779     }
10780   else
10781     {
10782       /* STM (STMIA, STMEA), STMDA (STMED), STMDB (STMFD) and STMIB (STMFA).  */
10783
10784       addr_mode = bits (arm_insn_r->arm_insn, 23, 24); 
10785
10786       regcache_raw_read_unsigned (reg_cache, reg_base, &u_regval);
10787
10788       /* Find out how many registers are going to be stored to memory.  */
10789       while (register_bits)
10790         {
10791           if (register_bits & 0x00000001)
10792             register_count++;
10793           register_bits = register_bits >> 1;
10794         }
10795
10796       switch (addr_mode)
10797         {
10798           /* STMDA (STMED): Decrement after.  */
10799           case 0:
10800           record_buf_mem[1] = (uint32_t) u_regval
10801                               - register_count * INT_REGISTER_SIZE + 4;
10802           break;
10803           /* STM (STMIA, STMEA): Increment after.  */
10804           case 1:
10805           record_buf_mem[1] = (uint32_t) u_regval;
10806           break;
10807           /* STMDB (STMFD): Decrement before.  */
10808           case 2:
10809           record_buf_mem[1] = (uint32_t) u_regval
10810                               - register_count * INT_REGISTER_SIZE;
10811           break;
10812           /* STMIB (STMFA): Increment before.  */
10813           case 3:
10814           record_buf_mem[1] = (uint32_t) u_regval + INT_REGISTER_SIZE;
10815           break;
10816           default:
10817             gdb_assert_not_reached ("no decoding pattern found");
10818           break;
10819         }
10820
10821       record_buf_mem[0] = register_count * INT_REGISTER_SIZE;
10822       arm_insn_r->mem_rec_count = 1;
10823
10824       /* If wback is true, also save the base register, which is going to be
10825          written to.  */
10826       if (wback)
10827         record_buf[arm_insn_r->reg_rec_count++] = reg_base;
10828     }
10829
10830   REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
10831   MEM_ALLOC (arm_insn_r->arm_mems, arm_insn_r->mem_rec_count, record_buf_mem);
10832   return 0;
10833 }
10834
10835 /* Handling opcode 101 insns.  */
10836
10837 static int
10838 arm_record_b_bl (insn_decode_record *arm_insn_r)
10839 {
10840   uint32_t record_buf[8];
10841
10842   /* Handle B, BL, BLX(1) insns.  */
10843   /* B simply branches so we do nothing here.  */
10844   /* Note: BLX(1) doesnt fall here but instead it falls into
10845      extension space.  */
10846   if (bit (arm_insn_r->arm_insn, 24))
10847   {
10848     record_buf[0] = ARM_LR_REGNUM;
10849     arm_insn_r->reg_rec_count = 1;
10850   }
10851
10852   REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
10853
10854   return 0;
10855 }
10856
10857 static int
10858 arm_record_unsupported_insn (insn_decode_record *arm_insn_r)
10859 {
10860   printf_unfiltered (_("Process record does not support instruction "
10861                        "0x%0x at address %s.\n"),arm_insn_r->arm_insn,
10862                      paddress (arm_insn_r->gdbarch, arm_insn_r->this_addr));
10863
10864   return -1;
10865 }
10866
10867 /* Record handler for vector data transfer instructions.  */
10868
10869 static int
10870 arm_record_vdata_transfer_insn (insn_decode_record *arm_insn_r)
10871 {
10872   uint32_t bits_a, bit_c, bit_l, reg_t, reg_v;
10873   uint32_t record_buf[4];
10874
10875   const int num_regs = gdbarch_num_regs (arm_insn_r->gdbarch);
10876   reg_t = bits (arm_insn_r->arm_insn, 12, 15);
10877   reg_v = bits (arm_insn_r->arm_insn, 21, 23);
10878   bits_a = bits (arm_insn_r->arm_insn, 21, 23);
10879   bit_l = bit (arm_insn_r->arm_insn, 20);
10880   bit_c = bit (arm_insn_r->arm_insn, 8);
10881
10882   /* Handle VMOV instruction.  */
10883   if (bit_l && bit_c)
10884     {
10885       record_buf[0] = reg_t;
10886       arm_insn_r->reg_rec_count = 1;
10887     }
10888   else if (bit_l && !bit_c)
10889     {
10890       /* Handle VMOV instruction.  */
10891       if (bits_a == 0x00)
10892         {
10893           record_buf[0] = reg_t;
10894           arm_insn_r->reg_rec_count = 1;
10895         }
10896       /* Handle VMRS instruction.  */
10897       else if (bits_a == 0x07)
10898         {
10899           if (reg_t == 15)
10900             reg_t = ARM_PS_REGNUM;
10901
10902           record_buf[0] = reg_t;
10903           arm_insn_r->reg_rec_count = 1;
10904         }
10905     }
10906   else if (!bit_l && !bit_c)
10907     {
10908       /* Handle VMOV instruction.  */
10909       if (bits_a == 0x00)
10910         {
10911           record_buf[0] = ARM_D0_REGNUM + reg_v;
10912
10913           arm_insn_r->reg_rec_count = 1;
10914         }
10915       /* Handle VMSR instruction.  */
10916       else if (bits_a == 0x07)
10917         {
10918           record_buf[0] = ARM_FPSCR_REGNUM;
10919           arm_insn_r->reg_rec_count = 1;
10920         }
10921     }
10922   else if (!bit_l && bit_c)
10923     {
10924       /* Handle VMOV instruction.  */
10925       if (!(bits_a & 0x04))
10926         {
10927           record_buf[0] = (reg_v | (bit (arm_insn_r->arm_insn, 7) << 4))
10928                           + ARM_D0_REGNUM;
10929           arm_insn_r->reg_rec_count = 1;
10930         }
10931       /* Handle VDUP instruction.  */
10932       else
10933         {
10934           if (bit (arm_insn_r->arm_insn, 21))
10935             {
10936               reg_v = reg_v | (bit (arm_insn_r->arm_insn, 7) << 4);
10937               record_buf[0] = reg_v + ARM_D0_REGNUM;
10938               record_buf[1] = reg_v + ARM_D0_REGNUM + 1;
10939               arm_insn_r->reg_rec_count = 2;
10940             }
10941           else
10942             {
10943               reg_v = reg_v | (bit (arm_insn_r->arm_insn, 7) << 4);
10944               record_buf[0] = reg_v + ARM_D0_REGNUM;
10945               arm_insn_r->reg_rec_count = 1;
10946             }
10947         }
10948     }
10949
10950   REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
10951   return 0;
10952 }
10953
10954 /* Record handler for extension register load/store instructions.  */
10955
10956 static int
10957 arm_record_exreg_ld_st_insn (insn_decode_record *arm_insn_r)
10958 {
10959   uint32_t opcode, single_reg;
10960   uint8_t op_vldm_vstm;
10961   uint32_t record_buf[8], record_buf_mem[128];
10962   ULONGEST u_regval = 0;
10963
10964   struct regcache *reg_cache = arm_insn_r->regcache;
10965   const int num_regs = gdbarch_num_regs (arm_insn_r->gdbarch);
10966
10967   opcode = bits (arm_insn_r->arm_insn, 20, 24);
10968   single_reg = !bit (arm_insn_r->arm_insn, 8);
10969   op_vldm_vstm = opcode & 0x1b;
10970
10971   /* Handle VMOV instructions.  */
10972   if ((opcode & 0x1e) == 0x04)
10973     {
10974       if (bit (arm_insn_r->arm_insn, 20)) /* to_arm_registers bit 20? */
10975         {
10976           record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
10977           record_buf[1] = bits (arm_insn_r->arm_insn, 16, 19);
10978           arm_insn_r->reg_rec_count = 2;
10979         }
10980       else
10981         {
10982           uint8_t reg_m = bits (arm_insn_r->arm_insn, 0, 3);
10983           uint8_t bit_m = bit (arm_insn_r->arm_insn, 5);
10984
10985           if (single_reg)
10986             {
10987               /* The first S register number m is REG_M:M (M is bit 5),
10988                  the corresponding D register number is REG_M:M / 2, which
10989                  is REG_M.  */
10990               record_buf[arm_insn_r->reg_rec_count++] = ARM_D0_REGNUM + reg_m;
10991               /* The second S register number is REG_M:M + 1, the
10992                  corresponding D register number is (REG_M:M + 1) / 2.
10993                  IOW, if bit M is 1, the first and second S registers
10994                  are mapped to different D registers, otherwise, they are
10995                  in the same D register.  */
10996               if (bit_m)
10997                 {
10998                   record_buf[arm_insn_r->reg_rec_count++]
10999                     = ARM_D0_REGNUM + reg_m + 1;
11000                 }
11001             }
11002           else
11003             {
11004               record_buf[0] = ((bit_m << 4) + reg_m + ARM_D0_REGNUM);
11005               arm_insn_r->reg_rec_count = 1;
11006             }
11007         }
11008     }
11009   /* Handle VSTM and VPUSH instructions.  */
11010   else if (op_vldm_vstm == 0x08 || op_vldm_vstm == 0x0a
11011            || op_vldm_vstm == 0x12)
11012     {
11013       uint32_t start_address, reg_rn, imm_off32, imm_off8, memory_count;
11014       uint32_t memory_index = 0;
11015
11016       reg_rn = bits (arm_insn_r->arm_insn, 16, 19);
11017       regcache_raw_read_unsigned (reg_cache, reg_rn, &u_regval);
11018       imm_off8 = bits (arm_insn_r->arm_insn, 0, 7);
11019       imm_off32 = imm_off8 << 2;
11020       memory_count = imm_off8;
11021
11022       if (bit (arm_insn_r->arm_insn, 23))
11023         start_address = u_regval;
11024       else
11025         start_address = u_regval - imm_off32;
11026
11027       if (bit (arm_insn_r->arm_insn, 21))
11028         {
11029           record_buf[0] = reg_rn;
11030           arm_insn_r->reg_rec_count = 1;
11031         }
11032
11033       while (memory_count > 0)
11034         {
11035           if (single_reg)
11036             {
11037               record_buf_mem[memory_index] = 4;
11038               record_buf_mem[memory_index + 1] = start_address;
11039               start_address = start_address + 4;
11040               memory_index = memory_index + 2;
11041             }
11042           else
11043             {
11044               record_buf_mem[memory_index] = 4;
11045               record_buf_mem[memory_index + 1] = start_address;
11046               record_buf_mem[memory_index + 2] = 4;
11047               record_buf_mem[memory_index + 3] = start_address + 4;
11048               start_address = start_address + 8;
11049               memory_index = memory_index + 4;
11050             }
11051           memory_count--;
11052         }
11053       arm_insn_r->mem_rec_count = (memory_index >> 1);
11054     }
11055   /* Handle VLDM instructions.  */
11056   else if (op_vldm_vstm == 0x09 || op_vldm_vstm == 0x0b
11057            || op_vldm_vstm == 0x13)
11058     {
11059       uint32_t reg_count, reg_vd;
11060       uint32_t reg_index = 0;
11061       uint32_t bit_d = bit (arm_insn_r->arm_insn, 22);
11062
11063       reg_vd = bits (arm_insn_r->arm_insn, 12, 15);
11064       reg_count = bits (arm_insn_r->arm_insn, 0, 7);
11065
11066       /* REG_VD is the first D register number.  If the instruction
11067          loads memory to S registers (SINGLE_REG is TRUE), the register
11068          number is (REG_VD << 1 | bit D), so the corresponding D
11069          register number is (REG_VD << 1 | bit D) / 2 = REG_VD.  */
11070       if (!single_reg)
11071         reg_vd = reg_vd | (bit_d << 4);
11072
11073       if (bit (arm_insn_r->arm_insn, 21) /* write back */)
11074         record_buf[reg_index++] = bits (arm_insn_r->arm_insn, 16, 19);
11075
11076       /* If the instruction loads memory to D register, REG_COUNT should
11077          be divided by 2, according to the ARM Architecture Reference
11078          Manual.  If the instruction loads memory to S register, divide by
11079          2 as well because two S registers are mapped to D register.  */
11080       reg_count  = reg_count / 2;
11081       if (single_reg && bit_d)
11082         {
11083           /* Increase the register count if S register list starts from
11084              an odd number (bit d is one).  */
11085           reg_count++;
11086         }
11087
11088       while (reg_count > 0)
11089         {
11090           record_buf[reg_index++] = ARM_D0_REGNUM + reg_vd + reg_count - 1;
11091           reg_count--;
11092         }
11093       arm_insn_r->reg_rec_count = reg_index;
11094     }
11095   /* VSTR Vector store register.  */
11096   else if ((opcode & 0x13) == 0x10)
11097     {
11098       uint32_t start_address, reg_rn, imm_off32, imm_off8;
11099       uint32_t memory_index = 0;
11100
11101       reg_rn = bits (arm_insn_r->arm_insn, 16, 19);
11102       regcache_raw_read_unsigned (reg_cache, reg_rn, &u_regval);
11103       imm_off8 = bits (arm_insn_r->arm_insn, 0, 7);
11104       imm_off32 = imm_off8 << 2;
11105
11106       if (bit (arm_insn_r->arm_insn, 23))
11107         start_address = u_regval + imm_off32;
11108       else
11109         start_address = u_regval - imm_off32;
11110
11111       if (single_reg)
11112         {
11113           record_buf_mem[memory_index] = 4;
11114           record_buf_mem[memory_index + 1] = start_address;
11115           arm_insn_r->mem_rec_count = 1;
11116         }
11117       else
11118         {
11119           record_buf_mem[memory_index] = 4;
11120           record_buf_mem[memory_index + 1] = start_address;
11121           record_buf_mem[memory_index + 2] = 4;
11122           record_buf_mem[memory_index + 3] = start_address + 4;
11123           arm_insn_r->mem_rec_count = 2;
11124         }
11125     }
11126   /* VLDR Vector load register.  */
11127   else if ((opcode & 0x13) == 0x11)
11128     {
11129       uint32_t reg_vd = bits (arm_insn_r->arm_insn, 12, 15);
11130
11131       if (!single_reg)
11132         {
11133           reg_vd = reg_vd | (bit (arm_insn_r->arm_insn, 22) << 4);
11134           record_buf[0] = ARM_D0_REGNUM + reg_vd;
11135         }
11136       else
11137         {
11138           reg_vd = (reg_vd << 1) | bit (arm_insn_r->arm_insn, 22);
11139           /* Record register D rather than pseudo register S.  */
11140           record_buf[0] = ARM_D0_REGNUM + reg_vd / 2;
11141         }
11142       arm_insn_r->reg_rec_count = 1;
11143     }
11144
11145   REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
11146   MEM_ALLOC (arm_insn_r->arm_mems, arm_insn_r->mem_rec_count, record_buf_mem);
11147   return 0;
11148 }
11149
11150 /* Record handler for arm/thumb mode VFP data processing instructions.  */
11151
11152 static int
11153 arm_record_vfp_data_proc_insn (insn_decode_record *arm_insn_r)
11154 {
11155   uint32_t opc1, opc2, opc3, dp_op_sz, bit_d, reg_vd;
11156   uint32_t record_buf[4];
11157   enum insn_types {INSN_T0, INSN_T1, INSN_T2, INSN_T3, INSN_INV};
11158   enum insn_types curr_insn_type = INSN_INV;
11159
11160   reg_vd = bits (arm_insn_r->arm_insn, 12, 15);
11161   opc1 = bits (arm_insn_r->arm_insn, 20, 23);
11162   opc2 = bits (arm_insn_r->arm_insn, 16, 19);
11163   opc3 = bits (arm_insn_r->arm_insn, 6, 7);
11164   dp_op_sz = bit (arm_insn_r->arm_insn, 8);
11165   bit_d = bit (arm_insn_r->arm_insn, 22);
11166   opc1 = opc1 & 0x04;
11167
11168   /* Handle VMLA, VMLS.  */
11169   if (opc1 == 0x00)
11170     {
11171       if (bit (arm_insn_r->arm_insn, 10))
11172         {
11173           if (bit (arm_insn_r->arm_insn, 6))
11174             curr_insn_type = INSN_T0;
11175           else
11176             curr_insn_type = INSN_T1;
11177         }
11178       else
11179         {
11180           if (dp_op_sz)
11181             curr_insn_type = INSN_T1;
11182           else
11183             curr_insn_type = INSN_T2;
11184         }
11185     }
11186   /* Handle VNMLA, VNMLS, VNMUL.  */
11187   else if (opc1 == 0x01)
11188     {
11189       if (dp_op_sz)
11190         curr_insn_type = INSN_T1;
11191       else
11192         curr_insn_type = INSN_T2;
11193     }
11194   /* Handle VMUL.  */
11195   else if (opc1 == 0x02 && !(opc3 & 0x01))
11196     {
11197       if (bit (arm_insn_r->arm_insn, 10))
11198         {
11199           if (bit (arm_insn_r->arm_insn, 6))
11200             curr_insn_type = INSN_T0;
11201           else
11202             curr_insn_type = INSN_T1;
11203         }
11204       else
11205         {
11206           if (dp_op_sz)
11207             curr_insn_type = INSN_T1;
11208           else
11209             curr_insn_type = INSN_T2;
11210         }
11211     }
11212   /* Handle VADD, VSUB.  */
11213   else if (opc1 == 0x03)
11214     {
11215       if (!bit (arm_insn_r->arm_insn, 9))
11216         {
11217           if (bit (arm_insn_r->arm_insn, 6))
11218             curr_insn_type = INSN_T0;
11219           else
11220             curr_insn_type = INSN_T1;
11221         }
11222       else
11223         {
11224           if (dp_op_sz)
11225             curr_insn_type = INSN_T1;
11226           else
11227             curr_insn_type = INSN_T2;
11228         }
11229     }
11230   /* Handle VDIV.  */
11231   else if (opc1 == 0x0b)
11232     {
11233       if (dp_op_sz)
11234         curr_insn_type = INSN_T1;
11235       else
11236         curr_insn_type = INSN_T2;
11237     }
11238   /* Handle all other vfp data processing instructions.  */
11239   else if (opc1 == 0x0b)
11240     {
11241       /* Handle VMOV.  */
11242       if (!(opc3 & 0x01) || (opc2 == 0x00 && opc3 == 0x01))
11243         {
11244           if (bit (arm_insn_r->arm_insn, 4))
11245             {
11246               if (bit (arm_insn_r->arm_insn, 6))
11247                 curr_insn_type = INSN_T0;
11248               else
11249                 curr_insn_type = INSN_T1;
11250             }
11251           else
11252             {
11253               if (dp_op_sz)
11254                 curr_insn_type = INSN_T1;
11255               else
11256                 curr_insn_type = INSN_T2;
11257             }
11258         }
11259       /* Handle VNEG and VABS.  */
11260       else if ((opc2 == 0x01 && opc3 == 0x01)
11261               || (opc2 == 0x00 && opc3 == 0x03))
11262         {
11263           if (!bit (arm_insn_r->arm_insn, 11))
11264             {
11265               if (bit (arm_insn_r->arm_insn, 6))
11266                 curr_insn_type = INSN_T0;
11267               else
11268                 curr_insn_type = INSN_T1;
11269             }
11270           else
11271             {
11272               if (dp_op_sz)
11273                 curr_insn_type = INSN_T1;
11274               else
11275                 curr_insn_type = INSN_T2;
11276             }
11277         }
11278       /* Handle VSQRT.  */
11279       else if (opc2 == 0x01 && opc3 == 0x03)
11280         {
11281           if (dp_op_sz)
11282             curr_insn_type = INSN_T1;
11283           else
11284             curr_insn_type = INSN_T2;
11285         }
11286       /* Handle VCVT.  */
11287       else if (opc2 == 0x07 && opc3 == 0x03)
11288         {
11289           if (!dp_op_sz)
11290             curr_insn_type = INSN_T1;
11291           else
11292             curr_insn_type = INSN_T2;
11293         }
11294       else if (opc3 & 0x01)
11295         {
11296           /* Handle VCVT.  */
11297           if ((opc2 == 0x08) || (opc2 & 0x0e) == 0x0c)
11298             {
11299               if (!bit (arm_insn_r->arm_insn, 18))
11300                 curr_insn_type = INSN_T2;
11301               else
11302                 {
11303                   if (dp_op_sz)
11304                     curr_insn_type = INSN_T1;
11305                   else
11306                     curr_insn_type = INSN_T2;
11307                 }
11308             }
11309           /* Handle VCVT.  */
11310           else if ((opc2 & 0x0e) == 0x0a || (opc2 & 0x0e) == 0x0e)
11311             {
11312               if (dp_op_sz)
11313                 curr_insn_type = INSN_T1;
11314               else
11315                 curr_insn_type = INSN_T2;
11316             }
11317           /* Handle VCVTB, VCVTT.  */
11318           else if ((opc2 & 0x0e) == 0x02)
11319             curr_insn_type = INSN_T2;
11320           /* Handle VCMP, VCMPE.  */
11321           else if ((opc2 & 0x0e) == 0x04)
11322             curr_insn_type = INSN_T3;
11323         }
11324     }
11325
11326   switch (curr_insn_type)
11327     {
11328       case INSN_T0:
11329         reg_vd = reg_vd | (bit_d << 4);
11330         record_buf[0] = reg_vd + ARM_D0_REGNUM;
11331         record_buf[1] = reg_vd + ARM_D0_REGNUM + 1;
11332         arm_insn_r->reg_rec_count = 2;
11333         break;
11334
11335       case INSN_T1:
11336         reg_vd = reg_vd | (bit_d << 4);
11337         record_buf[0] = reg_vd + ARM_D0_REGNUM;
11338         arm_insn_r->reg_rec_count = 1;
11339         break;
11340
11341       case INSN_T2:
11342         reg_vd = (reg_vd << 1) | bit_d;
11343         record_buf[0] = reg_vd + ARM_D0_REGNUM;
11344         arm_insn_r->reg_rec_count = 1;
11345         break;
11346
11347       case INSN_T3:
11348         record_buf[0] = ARM_FPSCR_REGNUM;
11349         arm_insn_r->reg_rec_count = 1;
11350         break;
11351
11352       default:
11353         gdb_assert_not_reached ("no decoding pattern found");
11354         break;
11355     }
11356
11357   REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
11358   return 0;
11359 }
11360
11361 /* Handling opcode 110 insns.  */
11362
11363 static int
11364 arm_record_asimd_vfp_coproc (insn_decode_record *arm_insn_r)
11365 {
11366   uint32_t op1, op1_ebit, coproc;
11367
11368   coproc = bits (arm_insn_r->arm_insn, 8, 11);
11369   op1 = bits (arm_insn_r->arm_insn, 20, 25);
11370   op1_ebit = bit (arm_insn_r->arm_insn, 20);
11371
11372   if ((coproc & 0x0e) == 0x0a)
11373     {
11374       /* Handle extension register ld/st instructions.  */
11375       if (!(op1 & 0x20))
11376         return arm_record_exreg_ld_st_insn (arm_insn_r);
11377
11378       /* 64-bit transfers between arm core and extension registers.  */
11379       if ((op1 & 0x3e) == 0x04)
11380         return arm_record_exreg_ld_st_insn (arm_insn_r);
11381     }
11382   else
11383     {
11384       /* Handle coprocessor ld/st instructions.  */
11385       if (!(op1 & 0x3a))
11386         {
11387           /* Store.  */
11388           if (!op1_ebit)
11389             return arm_record_unsupported_insn (arm_insn_r);
11390           else
11391             /* Load.  */
11392             return arm_record_unsupported_insn (arm_insn_r);
11393         }
11394
11395       /* Move to coprocessor from two arm core registers.  */
11396       if (op1 == 0x4)
11397         return arm_record_unsupported_insn (arm_insn_r);
11398
11399       /* Move to two arm core registers from coprocessor.  */
11400       if (op1 == 0x5)
11401         {
11402           uint32_t reg_t[2];
11403
11404           reg_t[0] = bits (arm_insn_r->arm_insn, 12, 15);
11405           reg_t[1] = bits (arm_insn_r->arm_insn, 16, 19);
11406           arm_insn_r->reg_rec_count = 2;
11407
11408           REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, reg_t);
11409           return 0;
11410        }
11411     }
11412   return arm_record_unsupported_insn (arm_insn_r);
11413 }
11414
11415 /* Handling opcode 111 insns.  */
11416
11417 static int
11418 arm_record_coproc_data_proc (insn_decode_record *arm_insn_r)
11419 {
11420   uint32_t op, op1_sbit, op1_ebit, coproc;
11421   struct gdbarch_tdep *tdep = gdbarch_tdep (arm_insn_r->gdbarch);
11422   struct regcache *reg_cache = arm_insn_r->regcache;
11423
11424   arm_insn_r->opcode = bits (arm_insn_r->arm_insn, 24, 27);
11425   coproc = bits (arm_insn_r->arm_insn, 8, 11);
11426   op1_sbit = bit (arm_insn_r->arm_insn, 24);
11427   op1_ebit = bit (arm_insn_r->arm_insn, 20);
11428   op = bit (arm_insn_r->arm_insn, 4);
11429
11430   /* Handle arm SWI/SVC system call instructions.  */
11431   if (op1_sbit)
11432     {
11433       if (tdep->arm_syscall_record != NULL)
11434         {
11435           ULONGEST svc_operand, svc_number;
11436
11437           svc_operand = (0x00ffffff & arm_insn_r->arm_insn);
11438
11439           if (svc_operand)  /* OABI.  */
11440             svc_number = svc_operand - 0x900000;
11441           else /* EABI.  */
11442             regcache_raw_read_unsigned (reg_cache, 7, &svc_number);
11443
11444           return tdep->arm_syscall_record (reg_cache, svc_number);
11445         }
11446       else
11447         {
11448           printf_unfiltered (_("no syscall record support\n"));
11449           return -1;
11450         }
11451     }
11452
11453   if ((coproc & 0x0e) == 0x0a)
11454     {
11455       /* VFP data-processing instructions.  */
11456       if (!op1_sbit && !op)
11457         return arm_record_vfp_data_proc_insn (arm_insn_r);
11458
11459       /* Advanced SIMD, VFP instructions.  */
11460       if (!op1_sbit && op)
11461         return arm_record_vdata_transfer_insn (arm_insn_r);
11462     }
11463   else
11464     {
11465       /* Coprocessor data operations.  */
11466       if (!op1_sbit && !op)
11467         return arm_record_unsupported_insn (arm_insn_r);
11468
11469       /* Move to Coprocessor from ARM core register.  */
11470       if (!op1_sbit && !op1_ebit && op)
11471         return arm_record_unsupported_insn (arm_insn_r);
11472
11473       /* Move to arm core register from coprocessor.  */
11474       if (!op1_sbit && op1_ebit && op)
11475         {
11476           uint32_t record_buf[1];
11477
11478           record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11479           if (record_buf[0] == 15)
11480             record_buf[0] = ARM_PS_REGNUM;
11481
11482           arm_insn_r->reg_rec_count = 1;
11483           REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count,
11484                      record_buf);
11485           return 0;
11486         }
11487     }
11488
11489   return arm_record_unsupported_insn (arm_insn_r);
11490 }
11491
11492 /* Handling opcode 000 insns.  */
11493
11494 static int
11495 thumb_record_shift_add_sub (insn_decode_record *thumb_insn_r)
11496 {
11497   uint32_t record_buf[8];
11498   uint32_t reg_src1 = 0;
11499
11500   reg_src1 = bits (thumb_insn_r->arm_insn, 0, 2);
11501
11502   record_buf[0] = ARM_PS_REGNUM;
11503   record_buf[1] = reg_src1;
11504   thumb_insn_r->reg_rec_count = 2;
11505
11506   REG_ALLOC (thumb_insn_r->arm_regs, thumb_insn_r->reg_rec_count, record_buf);
11507
11508   return 0;
11509 }
11510
11511
11512 /* Handling opcode 001 insns.  */
11513
11514 static int
11515 thumb_record_add_sub_cmp_mov (insn_decode_record *thumb_insn_r)
11516 {
11517   uint32_t record_buf[8];
11518   uint32_t reg_src1 = 0;
11519
11520   reg_src1 = bits (thumb_insn_r->arm_insn, 8, 10);
11521
11522   record_buf[0] = ARM_PS_REGNUM;
11523   record_buf[1] = reg_src1;
11524   thumb_insn_r->reg_rec_count = 2;
11525
11526   REG_ALLOC (thumb_insn_r->arm_regs, thumb_insn_r->reg_rec_count, record_buf);
11527
11528   return 0;
11529 }
11530
11531 /* Handling opcode 010 insns.  */
11532
11533 static int
11534 thumb_record_ld_st_reg_offset (insn_decode_record *thumb_insn_r)
11535 {
11536   struct regcache *reg_cache =  thumb_insn_r->regcache;
11537   uint32_t record_buf[8], record_buf_mem[8];
11538
11539   uint32_t reg_src1 = 0, reg_src2 = 0;
11540   uint32_t opcode1 = 0, opcode2 = 0, opcode3 = 0;
11541
11542   ULONGEST u_regval[2] = {0};
11543
11544   opcode1 = bits (thumb_insn_r->arm_insn, 10, 12);
11545
11546   if (bit (thumb_insn_r->arm_insn, 12))
11547     {
11548       /* Handle load/store register offset.  */
11549       opcode2 = bits (thumb_insn_r->arm_insn, 9, 10);
11550       if (opcode2 >= 12 && opcode2 <= 15)
11551         {
11552           /* LDR(2), LDRB(2) , LDRH(2), LDRSB, LDRSH.  */
11553           reg_src1 = bits (thumb_insn_r->arm_insn,0, 2);
11554           record_buf[0] = reg_src1;
11555           thumb_insn_r->reg_rec_count = 1;
11556         }
11557       else if (opcode2 >= 8 && opcode2 <= 10)
11558         {
11559           /* STR(2), STRB(2), STRH(2) .  */
11560           reg_src1 = bits (thumb_insn_r->arm_insn, 3, 5);
11561           reg_src2 = bits (thumb_insn_r->arm_insn, 6, 8);
11562           regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval[0]);
11563           regcache_raw_read_unsigned (reg_cache, reg_src2, &u_regval[1]);
11564           if (8 == opcode2)
11565             record_buf_mem[0] = 4;    /* STR (2).  */
11566           else if (10 == opcode2)
11567             record_buf_mem[0] = 1;    /*  STRB (2).  */
11568           else if (9 == opcode2)
11569             record_buf_mem[0] = 2;    /* STRH (2).  */
11570           record_buf_mem[1] = u_regval[0] + u_regval[1];
11571           thumb_insn_r->mem_rec_count = 1;
11572         }
11573     }
11574   else if (bit (thumb_insn_r->arm_insn, 11))
11575     {
11576       /* Handle load from literal pool.  */
11577       /* LDR(3).  */
11578       reg_src1 = bits (thumb_insn_r->arm_insn, 8, 10);
11579       record_buf[0] = reg_src1;
11580       thumb_insn_r->reg_rec_count = 1;
11581     }
11582   else if (opcode1)
11583     {
11584       opcode2 = bits (thumb_insn_r->arm_insn, 8, 9);
11585       opcode3 = bits (thumb_insn_r->arm_insn, 0, 2);
11586       if ((3 == opcode2) && (!opcode3))
11587         {
11588           /* Branch with exchange.  */
11589           record_buf[0] = ARM_PS_REGNUM;
11590           thumb_insn_r->reg_rec_count = 1;
11591         }
11592       else
11593         {
11594           /* Format 8; special data processing insns.  */
11595           record_buf[0] = ARM_PS_REGNUM;
11596           record_buf[1] = (bit (thumb_insn_r->arm_insn, 7) << 3
11597                            | bits (thumb_insn_r->arm_insn, 0, 2));
11598           thumb_insn_r->reg_rec_count = 2;
11599         }
11600     }
11601   else
11602     {
11603       /* Format 5; data processing insns.  */
11604       reg_src1 = bits (thumb_insn_r->arm_insn, 0, 2);
11605       if (bit (thumb_insn_r->arm_insn, 7))
11606         {
11607           reg_src1 = reg_src1 + 8;
11608         }
11609       record_buf[0] = ARM_PS_REGNUM;
11610       record_buf[1] = reg_src1;
11611       thumb_insn_r->reg_rec_count = 2;
11612     }
11613
11614   REG_ALLOC (thumb_insn_r->arm_regs, thumb_insn_r->reg_rec_count, record_buf);
11615   MEM_ALLOC (thumb_insn_r->arm_mems, thumb_insn_r->mem_rec_count,
11616              record_buf_mem);
11617
11618   return 0;
11619 }
11620
11621 /* Handling opcode 001 insns.  */
11622
11623 static int
11624 thumb_record_ld_st_imm_offset (insn_decode_record *thumb_insn_r)
11625 {
11626   struct regcache *reg_cache = thumb_insn_r->regcache;
11627   uint32_t record_buf[8], record_buf_mem[8];
11628
11629   uint32_t reg_src1 = 0;
11630   uint32_t opcode = 0, immed_5 = 0;
11631
11632   ULONGEST u_regval = 0;
11633
11634   opcode = bits (thumb_insn_r->arm_insn, 11, 12);
11635
11636   if (opcode)
11637     {
11638       /* LDR(1).  */
11639       reg_src1 = bits (thumb_insn_r->arm_insn, 0, 2);
11640       record_buf[0] = reg_src1;
11641       thumb_insn_r->reg_rec_count = 1;
11642     }
11643   else
11644     {
11645       /* STR(1).  */
11646       reg_src1 = bits (thumb_insn_r->arm_insn, 3, 5);
11647       immed_5 = bits (thumb_insn_r->arm_insn, 6, 10);
11648       regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval);
11649       record_buf_mem[0] = 4;
11650       record_buf_mem[1] = u_regval + (immed_5 * 4);
11651       thumb_insn_r->mem_rec_count = 1;
11652     }
11653
11654   REG_ALLOC (thumb_insn_r->arm_regs, thumb_insn_r->reg_rec_count, record_buf);
11655   MEM_ALLOC (thumb_insn_r->arm_mems, thumb_insn_r->mem_rec_count, 
11656              record_buf_mem);
11657
11658   return 0;
11659 }
11660
11661 /* Handling opcode 100 insns.  */
11662
11663 static int
11664 thumb_record_ld_st_stack (insn_decode_record *thumb_insn_r)
11665 {
11666   struct regcache *reg_cache = thumb_insn_r->regcache;
11667   uint32_t record_buf[8], record_buf_mem[8];
11668
11669   uint32_t reg_src1 = 0;
11670   uint32_t opcode = 0, immed_8 = 0, immed_5 = 0;
11671
11672   ULONGEST u_regval = 0;
11673
11674   opcode = bits (thumb_insn_r->arm_insn, 11, 12);
11675
11676   if (3 == opcode)
11677     {
11678       /* LDR(4).  */
11679       reg_src1 = bits (thumb_insn_r->arm_insn, 8, 10);
11680       record_buf[0] = reg_src1;
11681       thumb_insn_r->reg_rec_count = 1;
11682     }
11683   else if (1 == opcode)
11684     {
11685       /* LDRH(1).  */
11686       reg_src1 = bits (thumb_insn_r->arm_insn, 0, 2);
11687       record_buf[0] = reg_src1;
11688       thumb_insn_r->reg_rec_count = 1;
11689     }
11690   else if (2 == opcode)
11691     {
11692       /* STR(3).  */
11693       immed_8 = bits (thumb_insn_r->arm_insn, 0, 7);
11694       regcache_raw_read_unsigned (reg_cache, ARM_SP_REGNUM, &u_regval);
11695       record_buf_mem[0] = 4;
11696       record_buf_mem[1] = u_regval + (immed_8 * 4);
11697       thumb_insn_r->mem_rec_count = 1;
11698     }
11699   else if (0 == opcode)
11700     {
11701       /* STRH(1).  */
11702       immed_5 = bits (thumb_insn_r->arm_insn, 6, 10);
11703       reg_src1 = bits (thumb_insn_r->arm_insn, 3, 5);
11704       regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval);
11705       record_buf_mem[0] = 2;
11706       record_buf_mem[1] = u_regval + (immed_5 * 2);
11707       thumb_insn_r->mem_rec_count = 1;
11708     }
11709
11710   REG_ALLOC (thumb_insn_r->arm_regs, thumb_insn_r->reg_rec_count, record_buf);
11711   MEM_ALLOC (thumb_insn_r->arm_mems, thumb_insn_r->mem_rec_count,
11712              record_buf_mem);
11713
11714   return 0;
11715 }
11716
11717 /* Handling opcode 101 insns.  */
11718
11719 static int
11720 thumb_record_misc (insn_decode_record *thumb_insn_r)
11721 {
11722   struct regcache *reg_cache = thumb_insn_r->regcache;
11723
11724   uint32_t opcode = 0, opcode1 = 0, opcode2 = 0;
11725   uint32_t register_bits = 0, register_count = 0;
11726   uint32_t index = 0, start_address = 0;
11727   uint32_t record_buf[24], record_buf_mem[48];
11728   uint32_t reg_src1;
11729
11730   ULONGEST u_regval = 0;
11731
11732   opcode = bits (thumb_insn_r->arm_insn, 11, 12);
11733   opcode1 = bits (thumb_insn_r->arm_insn, 8, 12);
11734   opcode2 = bits (thumb_insn_r->arm_insn, 9, 12);
11735
11736   if (14 == opcode2)
11737     {
11738       /* POP.  */
11739       register_bits = bits (thumb_insn_r->arm_insn, 0, 7);
11740       while (register_bits)
11741       {
11742         if (register_bits & 0x00000001)
11743           record_buf[index++] = register_count;
11744         register_bits = register_bits >> 1;
11745         register_count++;
11746       }
11747       record_buf[index++] = ARM_PS_REGNUM;
11748       record_buf[index++] = ARM_SP_REGNUM;
11749       thumb_insn_r->reg_rec_count = index;
11750     }
11751   else if (10 == opcode2)
11752     {
11753       /* PUSH.  */
11754       register_bits = bits (thumb_insn_r->arm_insn, 0, 7);
11755       regcache_raw_read_unsigned (reg_cache, ARM_SP_REGNUM, &u_regval);
11756       while (register_bits)
11757         {
11758           if (register_bits & 0x00000001)
11759             register_count++;
11760           register_bits = register_bits >> 1;
11761         }
11762       start_address = u_regval -  \
11763                   (4 * (bit (thumb_insn_r->arm_insn, 8) + register_count));
11764       thumb_insn_r->mem_rec_count = register_count;
11765       while (register_count)
11766         {
11767           record_buf_mem[(register_count * 2) - 1] = start_address;
11768           record_buf_mem[(register_count * 2) - 2] = 4;
11769           start_address = start_address + 4;
11770           register_count--;
11771         }
11772       record_buf[0] = ARM_SP_REGNUM;
11773       thumb_insn_r->reg_rec_count = 1;
11774     }
11775   else if (0x1E == opcode1)
11776     {
11777       /* BKPT insn.  */
11778       /* Handle enhanced software breakpoint insn, BKPT.  */
11779       /* CPSR is changed to be executed in ARM state,  disabling normal
11780          interrupts, entering abort mode.  */
11781       /* According to high vector configuration PC is set.  */
11782       /* User hits breakpoint and type reverse, in that case, we need to go back with 
11783       previous CPSR and Program Counter.  */
11784       record_buf[0] = ARM_PS_REGNUM;
11785       record_buf[1] = ARM_LR_REGNUM;
11786       thumb_insn_r->reg_rec_count = 2;
11787       /* We need to save SPSR value, which is not yet done.  */
11788       printf_unfiltered (_("Process record does not support instruction "
11789                            "0x%0x at address %s.\n"),
11790                            thumb_insn_r->arm_insn,
11791                            paddress (thumb_insn_r->gdbarch,
11792                            thumb_insn_r->this_addr));
11793       return -1;
11794     }
11795   else if ((0 == opcode) || (1 == opcode))
11796     {
11797       /* ADD(5), ADD(6).  */
11798       reg_src1 = bits (thumb_insn_r->arm_insn, 8, 10);
11799       record_buf[0] = reg_src1;
11800       thumb_insn_r->reg_rec_count = 1;
11801     }
11802   else if (2 == opcode)
11803     {
11804       /* ADD(7), SUB(4).  */
11805       reg_src1 = bits (thumb_insn_r->arm_insn, 8, 10);
11806       record_buf[0] = ARM_SP_REGNUM;
11807       thumb_insn_r->reg_rec_count = 1;
11808     }
11809
11810   REG_ALLOC (thumb_insn_r->arm_regs, thumb_insn_r->reg_rec_count, record_buf);
11811   MEM_ALLOC (thumb_insn_r->arm_mems, thumb_insn_r->mem_rec_count,
11812              record_buf_mem);
11813
11814   return 0;
11815 }
11816
11817 /* Handling opcode 110 insns.  */
11818
11819 static int
11820 thumb_record_ldm_stm_swi (insn_decode_record *thumb_insn_r)                
11821 {
11822   struct gdbarch_tdep *tdep = gdbarch_tdep (thumb_insn_r->gdbarch);
11823   struct regcache *reg_cache = thumb_insn_r->regcache;
11824
11825   uint32_t ret = 0; /* function return value: -1:record failure ;  0:success  */
11826   uint32_t reg_src1 = 0;
11827   uint32_t opcode1 = 0, opcode2 = 0, register_bits = 0, register_count = 0;
11828   uint32_t index = 0, start_address = 0;
11829   uint32_t record_buf[24], record_buf_mem[48];
11830
11831   ULONGEST u_regval = 0;
11832
11833   opcode1 = bits (thumb_insn_r->arm_insn, 8, 12);
11834   opcode2 = bits (thumb_insn_r->arm_insn, 11, 12);
11835
11836   if (1 == opcode2)
11837     {
11838
11839       /* LDMIA.  */
11840       register_bits = bits (thumb_insn_r->arm_insn, 0, 7);
11841       /* Get Rn.  */
11842       reg_src1 = bits (thumb_insn_r->arm_insn, 8, 10);
11843       while (register_bits)
11844         {
11845           if (register_bits & 0x00000001)
11846             record_buf[index++] = register_count;
11847           register_bits = register_bits >> 1;
11848           register_count++;
11849         }
11850       record_buf[index++] = reg_src1;
11851       thumb_insn_r->reg_rec_count = index;
11852     }
11853   else if (0 == opcode2)
11854     {
11855       /* It handles both STMIA.  */
11856       register_bits = bits (thumb_insn_r->arm_insn, 0, 7);
11857       /* Get Rn.  */
11858       reg_src1 = bits (thumb_insn_r->arm_insn, 8, 10);
11859       regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval);
11860       while (register_bits)
11861         {
11862           if (register_bits & 0x00000001)
11863             register_count++;
11864           register_bits = register_bits >> 1;
11865         }
11866       start_address = u_regval;
11867       thumb_insn_r->mem_rec_count = register_count;
11868       while (register_count)
11869         {
11870           record_buf_mem[(register_count * 2) - 1] = start_address;
11871           record_buf_mem[(register_count * 2) - 2] = 4;
11872           start_address = start_address + 4;
11873           register_count--;
11874         }
11875     }
11876   else if (0x1F == opcode1)
11877     {
11878         /* Handle arm syscall insn.  */
11879         if (tdep->arm_syscall_record != NULL)
11880           {
11881             regcache_raw_read_unsigned (reg_cache, 7, &u_regval);
11882             ret = tdep->arm_syscall_record (reg_cache, u_regval);
11883           }
11884         else
11885           {
11886             printf_unfiltered (_("no syscall record support\n"));
11887             return -1;
11888           }
11889     }
11890
11891   /* B (1), conditional branch is automatically taken care in process_record,
11892     as PC is saved there.  */
11893
11894   REG_ALLOC (thumb_insn_r->arm_regs, thumb_insn_r->reg_rec_count, record_buf);
11895   MEM_ALLOC (thumb_insn_r->arm_mems, thumb_insn_r->mem_rec_count,
11896              record_buf_mem);
11897
11898   return ret;
11899 }
11900
11901 /* Handling opcode 111 insns.  */
11902
11903 static int
11904 thumb_record_branch (insn_decode_record *thumb_insn_r)
11905 {
11906   uint32_t record_buf[8];
11907   uint32_t bits_h = 0;
11908
11909   bits_h = bits (thumb_insn_r->arm_insn, 11, 12);
11910
11911   if (2 == bits_h || 3 == bits_h)
11912     {
11913       /* BL */
11914       record_buf[0] = ARM_LR_REGNUM;
11915       thumb_insn_r->reg_rec_count = 1;
11916     }
11917   else if (1 == bits_h)
11918     {
11919       /* BLX(1). */
11920       record_buf[0] = ARM_PS_REGNUM;
11921       record_buf[1] = ARM_LR_REGNUM;
11922       thumb_insn_r->reg_rec_count = 2;
11923     }
11924
11925   /* B(2) is automatically taken care in process_record, as PC is 
11926      saved there.  */
11927
11928   REG_ALLOC (thumb_insn_r->arm_regs, thumb_insn_r->reg_rec_count, record_buf);
11929
11930   return 0;     
11931 }
11932
11933 /* Handler for thumb2 load/store multiple instructions.  */
11934
11935 static int
11936 thumb2_record_ld_st_multiple (insn_decode_record *thumb2_insn_r)
11937 {
11938   struct regcache *reg_cache = thumb2_insn_r->regcache;
11939
11940   uint32_t reg_rn, op;
11941   uint32_t register_bits = 0, register_count = 0;
11942   uint32_t index = 0, start_address = 0;
11943   uint32_t record_buf[24], record_buf_mem[48];
11944
11945   ULONGEST u_regval = 0;
11946
11947   reg_rn = bits (thumb2_insn_r->arm_insn, 16, 19);
11948   op = bits (thumb2_insn_r->arm_insn, 23, 24);
11949
11950   if (0 == op || 3 == op)
11951     {
11952       if (bit (thumb2_insn_r->arm_insn, INSN_S_L_BIT_NUM))
11953         {
11954           /* Handle RFE instruction.  */
11955           record_buf[0] = ARM_PS_REGNUM;
11956           thumb2_insn_r->reg_rec_count = 1;
11957         }
11958       else
11959         {
11960           /* Handle SRS instruction after reading banked SP.  */
11961           return arm_record_unsupported_insn (thumb2_insn_r);
11962         }
11963     }
11964   else if (1 == op || 2 == op)
11965     {
11966       if (bit (thumb2_insn_r->arm_insn, INSN_S_L_BIT_NUM))
11967         {
11968           /* Handle LDM/LDMIA/LDMFD and LDMDB/LDMEA instructions.  */
11969           register_bits = bits (thumb2_insn_r->arm_insn, 0, 15);
11970           while (register_bits)
11971             {
11972               if (register_bits & 0x00000001)
11973                 record_buf[index++] = register_count;
11974
11975               register_count++;
11976               register_bits = register_bits >> 1;
11977             }
11978           record_buf[index++] = reg_rn;
11979           record_buf[index++] = ARM_PS_REGNUM;
11980           thumb2_insn_r->reg_rec_count = index;
11981         }
11982       else
11983         {
11984           /* Handle STM/STMIA/STMEA and STMDB/STMFD.  */
11985           register_bits = bits (thumb2_insn_r->arm_insn, 0, 15);
11986           regcache_raw_read_unsigned (reg_cache, reg_rn, &u_regval);
11987           while (register_bits)
11988             {
11989               if (register_bits & 0x00000001)
11990                 register_count++;
11991
11992               register_bits = register_bits >> 1;
11993             }
11994
11995           if (1 == op)
11996             {
11997               /* Start address calculation for LDMDB/LDMEA.  */
11998               start_address = u_regval;
11999             }
12000           else if (2 == op)
12001             {
12002               /* Start address calculation for LDMDB/LDMEA.  */
12003               start_address = u_regval - register_count * 4;
12004             }
12005
12006           thumb2_insn_r->mem_rec_count = register_count;
12007           while (register_count)
12008             {
12009               record_buf_mem[register_count * 2 - 1] = start_address;
12010               record_buf_mem[register_count * 2 - 2] = 4;
12011               start_address = start_address + 4;
12012               register_count--;
12013             }
12014           record_buf[0] = reg_rn;
12015           record_buf[1] = ARM_PS_REGNUM;
12016           thumb2_insn_r->reg_rec_count = 2;
12017         }
12018     }
12019
12020   MEM_ALLOC (thumb2_insn_r->arm_mems, thumb2_insn_r->mem_rec_count,
12021             record_buf_mem);
12022   REG_ALLOC (thumb2_insn_r->arm_regs, thumb2_insn_r->reg_rec_count,
12023             record_buf);
12024   return ARM_RECORD_SUCCESS;
12025 }
12026
12027 /* Handler for thumb2 load/store (dual/exclusive) and table branch
12028    instructions.  */
12029
12030 static int
12031 thumb2_record_ld_st_dual_ex_tbb (insn_decode_record *thumb2_insn_r)
12032 {
12033   struct regcache *reg_cache = thumb2_insn_r->regcache;
12034
12035   uint32_t reg_rd, reg_rn, offset_imm;
12036   uint32_t reg_dest1, reg_dest2;
12037   uint32_t address, offset_addr;
12038   uint32_t record_buf[8], record_buf_mem[8];
12039   uint32_t op1, op2, op3;
12040
12041   ULONGEST u_regval[2];
12042
12043   op1 = bits (thumb2_insn_r->arm_insn, 23, 24);
12044   op2 = bits (thumb2_insn_r->arm_insn, 20, 21);
12045   op3 = bits (thumb2_insn_r->arm_insn, 4, 7);
12046
12047   if (bit (thumb2_insn_r->arm_insn, INSN_S_L_BIT_NUM))
12048     {
12049       if(!(1 == op1 && 1 == op2 && (0 == op3 || 1 == op3)))
12050         {
12051           reg_dest1 = bits (thumb2_insn_r->arm_insn, 12, 15);
12052           record_buf[0] = reg_dest1;
12053           record_buf[1] = ARM_PS_REGNUM;
12054           thumb2_insn_r->reg_rec_count = 2;
12055         }
12056
12057       if (3 == op2 || (op1 & 2) || (1 == op1 && 1 == op2 && 7 == op3))
12058         {
12059           reg_dest2 = bits (thumb2_insn_r->arm_insn, 8, 11);
12060           record_buf[2] = reg_dest2;
12061           thumb2_insn_r->reg_rec_count = 3;
12062         }
12063     }
12064   else
12065     {
12066       reg_rn = bits (thumb2_insn_r->arm_insn, 16, 19);
12067       regcache_raw_read_unsigned (reg_cache, reg_rn, &u_regval[0]);
12068
12069       if (0 == op1 && 0 == op2)
12070         {
12071           /* Handle STREX.  */
12072           offset_imm = bits (thumb2_insn_r->arm_insn, 0, 7);
12073           address = u_regval[0] + (offset_imm * 4);
12074           record_buf_mem[0] = 4;
12075           record_buf_mem[1] = address;
12076           thumb2_insn_r->mem_rec_count = 1;
12077           reg_rd = bits (thumb2_insn_r->arm_insn, 0, 3);
12078           record_buf[0] = reg_rd;
12079           thumb2_insn_r->reg_rec_count = 1;
12080         }
12081       else if (1 == op1 && 0 == op2)
12082         {
12083           reg_rd = bits (thumb2_insn_r->arm_insn, 0, 3);
12084           record_buf[0] = reg_rd;
12085           thumb2_insn_r->reg_rec_count = 1;
12086           address = u_regval[0];
12087           record_buf_mem[1] = address;
12088
12089           if (4 == op3)
12090             {
12091               /* Handle STREXB.  */
12092               record_buf_mem[0] = 1;
12093               thumb2_insn_r->mem_rec_count = 1;
12094             }
12095           else if (5 == op3)
12096             {
12097               /* Handle STREXH.  */
12098               record_buf_mem[0] = 2 ;
12099               thumb2_insn_r->mem_rec_count = 1;
12100             }
12101           else if (7 == op3)
12102             {
12103               /* Handle STREXD.  */
12104               address = u_regval[0];
12105               record_buf_mem[0] = 4;
12106               record_buf_mem[2] = 4;
12107               record_buf_mem[3] = address + 4;
12108               thumb2_insn_r->mem_rec_count = 2;
12109             }
12110         }
12111       else
12112         {
12113           offset_imm = bits (thumb2_insn_r->arm_insn, 0, 7);
12114
12115           if (bit (thumb2_insn_r->arm_insn, 24))
12116             {
12117               if (bit (thumb2_insn_r->arm_insn, 23))
12118                 offset_addr = u_regval[0] + (offset_imm * 4);
12119               else
12120                 offset_addr = u_regval[0] - (offset_imm * 4);
12121
12122               address = offset_addr;
12123             }
12124           else
12125             address = u_regval[0];
12126
12127           record_buf_mem[0] = 4;
12128           record_buf_mem[1] = address;
12129           record_buf_mem[2] = 4;
12130           record_buf_mem[3] = address + 4;
12131           thumb2_insn_r->mem_rec_count = 2;
12132           record_buf[0] = reg_rn;
12133           thumb2_insn_r->reg_rec_count = 1;
12134         }
12135     }
12136
12137   REG_ALLOC (thumb2_insn_r->arm_regs, thumb2_insn_r->reg_rec_count,
12138             record_buf);
12139   MEM_ALLOC (thumb2_insn_r->arm_mems, thumb2_insn_r->mem_rec_count,
12140             record_buf_mem);
12141   return ARM_RECORD_SUCCESS;
12142 }
12143
12144 /* Handler for thumb2 data processing (shift register and modified immediate)
12145    instructions.  */
12146
12147 static int
12148 thumb2_record_data_proc_sreg_mimm (insn_decode_record *thumb2_insn_r)
12149 {
12150   uint32_t reg_rd, op;
12151   uint32_t record_buf[8];
12152
12153   op = bits (thumb2_insn_r->arm_insn, 21, 24);
12154   reg_rd = bits (thumb2_insn_r->arm_insn, 8, 11);
12155
12156   if ((0 == op || 4 == op || 8 == op || 13 == op) && 15 == reg_rd)
12157     {
12158       record_buf[0] = ARM_PS_REGNUM;
12159       thumb2_insn_r->reg_rec_count = 1;
12160     }
12161   else
12162     {
12163       record_buf[0] = reg_rd;
12164       record_buf[1] = ARM_PS_REGNUM;
12165       thumb2_insn_r->reg_rec_count = 2;
12166     }
12167
12168   REG_ALLOC (thumb2_insn_r->arm_regs, thumb2_insn_r->reg_rec_count,
12169             record_buf);
12170   return ARM_RECORD_SUCCESS;
12171 }
12172
12173 /* Generic handler for thumb2 instructions which effect destination and PS
12174    registers.  */
12175
12176 static int
12177 thumb2_record_ps_dest_generic (insn_decode_record *thumb2_insn_r)
12178 {
12179   uint32_t reg_rd;
12180   uint32_t record_buf[8];
12181
12182   reg_rd = bits (thumb2_insn_r->arm_insn, 8, 11);
12183
12184   record_buf[0] = reg_rd;
12185   record_buf[1] = ARM_PS_REGNUM;
12186   thumb2_insn_r->reg_rec_count = 2;
12187
12188   REG_ALLOC (thumb2_insn_r->arm_regs, thumb2_insn_r->reg_rec_count,
12189             record_buf);
12190   return ARM_RECORD_SUCCESS;
12191 }
12192
12193 /* Handler for thumb2 branch and miscellaneous control instructions.  */
12194
12195 static int
12196 thumb2_record_branch_misc_cntrl (insn_decode_record *thumb2_insn_r)
12197 {
12198   uint32_t op, op1, op2;
12199   uint32_t record_buf[8];
12200
12201   op = bits (thumb2_insn_r->arm_insn, 20, 26);
12202   op1 = bits (thumb2_insn_r->arm_insn, 12, 14);
12203   op2 = bits (thumb2_insn_r->arm_insn, 8, 11);
12204
12205   /* Handle MSR insn.  */
12206   if (!(op1 & 0x2) && 0x38 == op)
12207     {
12208       if (!(op2 & 0x3))
12209         {
12210           /* CPSR is going to be changed.  */
12211           record_buf[0] = ARM_PS_REGNUM;
12212           thumb2_insn_r->reg_rec_count = 1;
12213         }
12214       else
12215         {
12216           arm_record_unsupported_insn(thumb2_insn_r);
12217           return -1;
12218         }
12219     }
12220   else if (4 == (op1 & 0x5) || 5 == (op1 & 0x5))
12221     {
12222       /* BLX.  */
12223       record_buf[0] = ARM_PS_REGNUM;
12224       record_buf[1] = ARM_LR_REGNUM;
12225       thumb2_insn_r->reg_rec_count = 2;
12226     }
12227
12228   REG_ALLOC (thumb2_insn_r->arm_regs, thumb2_insn_r->reg_rec_count,
12229             record_buf);
12230   return ARM_RECORD_SUCCESS;
12231 }
12232
12233 /* Handler for thumb2 store single data item instructions.  */
12234
12235 static int
12236 thumb2_record_str_single_data (insn_decode_record *thumb2_insn_r)
12237 {
12238   struct regcache *reg_cache = thumb2_insn_r->regcache;
12239
12240   uint32_t reg_rn, reg_rm, offset_imm, shift_imm;
12241   uint32_t address, offset_addr;
12242   uint32_t record_buf[8], record_buf_mem[8];
12243   uint32_t op1, op2;
12244
12245   ULONGEST u_regval[2];
12246
12247   op1 = bits (thumb2_insn_r->arm_insn, 21, 23);
12248   op2 = bits (thumb2_insn_r->arm_insn, 6, 11);
12249   reg_rn = bits (thumb2_insn_r->arm_insn, 16, 19);
12250   regcache_raw_read_unsigned (reg_cache, reg_rn, &u_regval[0]);
12251
12252   if (bit (thumb2_insn_r->arm_insn, 23))
12253     {
12254       /* T2 encoding.  */
12255       offset_imm = bits (thumb2_insn_r->arm_insn, 0, 11);
12256       offset_addr = u_regval[0] + offset_imm;
12257       address = offset_addr;
12258     }
12259   else
12260     {
12261       /* T3 encoding.  */
12262       if ((0 == op1 || 1 == op1 || 2 == op1) && !(op2 & 0x20))
12263         {
12264           /* Handle STRB (register).  */
12265           reg_rm = bits (thumb2_insn_r->arm_insn, 0, 3);
12266           regcache_raw_read_unsigned (reg_cache, reg_rm, &u_regval[1]);
12267           shift_imm = bits (thumb2_insn_r->arm_insn, 4, 5);
12268           offset_addr = u_regval[1] << shift_imm;
12269           address = u_regval[0] + offset_addr;
12270         }
12271       else
12272         {
12273           offset_imm = bits (thumb2_insn_r->arm_insn, 0, 7);
12274           if (bit (thumb2_insn_r->arm_insn, 10))
12275             {
12276               if (bit (thumb2_insn_r->arm_insn, 9))
12277                 offset_addr = u_regval[0] + offset_imm;
12278               else
12279                 offset_addr = u_regval[0] - offset_imm;
12280
12281               address = offset_addr;
12282             }
12283           else
12284             address = u_regval[0];
12285         }
12286     }
12287
12288   switch (op1)
12289     {
12290       /* Store byte instructions.  */
12291       case 4:
12292       case 0:
12293         record_buf_mem[0] = 1;
12294         break;
12295       /* Store half word instructions.  */
12296       case 1:
12297       case 5:
12298         record_buf_mem[0] = 2;
12299         break;
12300       /* Store word instructions.  */
12301       case 2:
12302       case 6:
12303         record_buf_mem[0] = 4;
12304         break;
12305
12306       default:
12307         gdb_assert_not_reached ("no decoding pattern found");
12308         break;
12309     }
12310
12311   record_buf_mem[1] = address;
12312   thumb2_insn_r->mem_rec_count = 1;
12313   record_buf[0] = reg_rn;
12314   thumb2_insn_r->reg_rec_count = 1;
12315
12316   REG_ALLOC (thumb2_insn_r->arm_regs, thumb2_insn_r->reg_rec_count,
12317             record_buf);
12318   MEM_ALLOC (thumb2_insn_r->arm_mems, thumb2_insn_r->mem_rec_count,
12319             record_buf_mem);
12320   return ARM_RECORD_SUCCESS;
12321 }
12322
12323 /* Handler for thumb2 load memory hints instructions.  */
12324
12325 static int
12326 thumb2_record_ld_mem_hints (insn_decode_record *thumb2_insn_r)
12327 {
12328   uint32_t record_buf[8];
12329   uint32_t reg_rt, reg_rn;
12330
12331   reg_rt = bits (thumb2_insn_r->arm_insn, 12, 15);
12332   reg_rn = bits (thumb2_insn_r->arm_insn, 16, 19);
12333
12334   if (ARM_PC_REGNUM != reg_rt)
12335     {
12336       record_buf[0] = reg_rt;
12337       record_buf[1] = reg_rn;
12338       record_buf[2] = ARM_PS_REGNUM;
12339       thumb2_insn_r->reg_rec_count = 3;
12340
12341       REG_ALLOC (thumb2_insn_r->arm_regs, thumb2_insn_r->reg_rec_count,
12342                 record_buf);
12343       return ARM_RECORD_SUCCESS;
12344     }
12345
12346   return ARM_RECORD_FAILURE;
12347 }
12348
12349 /* Handler for thumb2 load word instructions.  */
12350
12351 static int
12352 thumb2_record_ld_word (insn_decode_record *thumb2_insn_r)
12353 {
12354   uint32_t record_buf[8];
12355
12356   record_buf[0] = bits (thumb2_insn_r->arm_insn, 12, 15);
12357   record_buf[1] = ARM_PS_REGNUM;
12358   thumb2_insn_r->reg_rec_count = 2;
12359
12360   REG_ALLOC (thumb2_insn_r->arm_regs, thumb2_insn_r->reg_rec_count,
12361             record_buf);
12362   return ARM_RECORD_SUCCESS;
12363 }
12364
12365 /* Handler for thumb2 long multiply, long multiply accumulate, and
12366    divide instructions.  */
12367
12368 static int
12369 thumb2_record_lmul_lmla_div (insn_decode_record *thumb2_insn_r)
12370 {
12371   uint32_t opcode1 = 0, opcode2 = 0;
12372   uint32_t record_buf[8];
12373
12374   opcode1 = bits (thumb2_insn_r->arm_insn, 20, 22);
12375   opcode2 = bits (thumb2_insn_r->arm_insn, 4, 7);
12376
12377   if (0 == opcode1 || 2 == opcode1 || (opcode1 >= 4 && opcode1 <= 6))
12378     {
12379       /* Handle SMULL, UMULL, SMULAL.  */
12380       /* Handle SMLAL(S), SMULL(S), UMLAL(S), UMULL(S).  */
12381       record_buf[0] = bits (thumb2_insn_r->arm_insn, 16, 19);
12382       record_buf[1] = bits (thumb2_insn_r->arm_insn, 12, 15);
12383       record_buf[2] = ARM_PS_REGNUM;
12384       thumb2_insn_r->reg_rec_count = 3;
12385     }
12386   else if (1 == opcode1 || 3 == opcode2)
12387     {
12388       /* Handle SDIV and UDIV.  */
12389       record_buf[0] = bits (thumb2_insn_r->arm_insn, 16, 19);
12390       record_buf[1] = bits (thumb2_insn_r->arm_insn, 12, 15);
12391       record_buf[2] = ARM_PS_REGNUM;
12392       thumb2_insn_r->reg_rec_count = 3;
12393     }
12394   else
12395     return ARM_RECORD_FAILURE;
12396
12397   REG_ALLOC (thumb2_insn_r->arm_regs, thumb2_insn_r->reg_rec_count,
12398             record_buf);
12399   return ARM_RECORD_SUCCESS;
12400 }
12401
12402 /* Record handler for thumb32 coprocessor instructions.  */
12403
12404 static int
12405 thumb2_record_coproc_insn (insn_decode_record *thumb2_insn_r)
12406 {
12407   if (bit (thumb2_insn_r->arm_insn, 25))
12408     return arm_record_coproc_data_proc (thumb2_insn_r);
12409   else
12410     return arm_record_asimd_vfp_coproc (thumb2_insn_r);
12411 }
12412
12413 /* Record handler for advance SIMD structure load/store instructions.  */
12414
12415 static int
12416 thumb2_record_asimd_struct_ld_st (insn_decode_record *thumb2_insn_r)
12417 {
12418   struct regcache *reg_cache = thumb2_insn_r->regcache;
12419   uint32_t l_bit, a_bit, b_bits;
12420   uint32_t record_buf[128], record_buf_mem[128];
12421   uint32_t reg_rn, reg_vd, address, f_elem;
12422   uint32_t index_r = 0, index_e = 0, bf_regs = 0, index_m = 0, loop_t = 0;
12423   uint8_t f_ebytes;
12424
12425   l_bit = bit (thumb2_insn_r->arm_insn, 21);
12426   a_bit = bit (thumb2_insn_r->arm_insn, 23);
12427   b_bits = bits (thumb2_insn_r->arm_insn, 8, 11);
12428   reg_rn = bits (thumb2_insn_r->arm_insn, 16, 19);
12429   reg_vd = bits (thumb2_insn_r->arm_insn, 12, 15);
12430   reg_vd = (bit (thumb2_insn_r->arm_insn, 22) << 4) | reg_vd;
12431   f_ebytes = (1 << bits (thumb2_insn_r->arm_insn, 6, 7));
12432   f_elem = 8 / f_ebytes;
12433
12434   if (!l_bit)
12435     {
12436       ULONGEST u_regval = 0;
12437       regcache_raw_read_unsigned (reg_cache, reg_rn, &u_regval);
12438       address = u_regval;
12439
12440       if (!a_bit)
12441         {
12442           /* Handle VST1.  */
12443           if (b_bits == 0x02 || b_bits == 0x0a || (b_bits & 0x0e) == 0x06)
12444             {
12445               if (b_bits == 0x07)
12446                 bf_regs = 1;
12447               else if (b_bits == 0x0a)
12448                 bf_regs = 2;
12449               else if (b_bits == 0x06)
12450                 bf_regs = 3;
12451               else if (b_bits == 0x02)
12452                 bf_regs = 4;
12453               else
12454                 bf_regs = 0;
12455
12456               for (index_r = 0; index_r < bf_regs; index_r++)
12457                 {
12458                   for (index_e = 0; index_e < f_elem; index_e++)
12459                     {
12460                       record_buf_mem[index_m++] = f_ebytes;
12461                       record_buf_mem[index_m++] = address;
12462                       address = address + f_ebytes;
12463                       thumb2_insn_r->mem_rec_count += 1;
12464                     }
12465                 }
12466             }
12467           /* Handle VST2.  */
12468           else if (b_bits == 0x03 || (b_bits & 0x0e) == 0x08)
12469             {
12470               if (b_bits == 0x09 || b_bits == 0x08)
12471                 bf_regs = 1;
12472               else if (b_bits == 0x03)
12473                 bf_regs = 2;
12474               else
12475                 bf_regs = 0;
12476
12477               for (index_r = 0; index_r < bf_regs; index_r++)
12478                 for (index_e = 0; index_e < f_elem; index_e++)
12479                   {
12480                     for (loop_t = 0; loop_t < 2; loop_t++)
12481                       {
12482                         record_buf_mem[index_m++] = f_ebytes;
12483                         record_buf_mem[index_m++] = address + (loop_t * f_ebytes);
12484                         thumb2_insn_r->mem_rec_count += 1;
12485                       }
12486                     address = address + (2 * f_ebytes);
12487                   }
12488             }
12489           /* Handle VST3.  */
12490           else if ((b_bits & 0x0e) == 0x04)
12491             {
12492               for (index_e = 0; index_e < f_elem; index_e++)
12493                 {
12494                   for (loop_t = 0; loop_t < 3; loop_t++)
12495                     {
12496                       record_buf_mem[index_m++] = f_ebytes;
12497                       record_buf_mem[index_m++] = address + (loop_t * f_ebytes);
12498                       thumb2_insn_r->mem_rec_count += 1;
12499                     }
12500                   address = address + (3 * f_ebytes);
12501                 }
12502             }
12503           /* Handle VST4.  */
12504           else if (!(b_bits & 0x0e))
12505             {
12506               for (index_e = 0; index_e < f_elem; index_e++)
12507                 {
12508                   for (loop_t = 0; loop_t < 4; loop_t++)
12509                     {
12510                       record_buf_mem[index_m++] = f_ebytes;
12511                       record_buf_mem[index_m++] = address + (loop_t * f_ebytes);
12512                       thumb2_insn_r->mem_rec_count += 1;
12513                     }
12514                   address = address + (4 * f_ebytes);
12515                 }
12516             }
12517         }
12518       else
12519         {
12520           uint8_t bft_size = bits (thumb2_insn_r->arm_insn, 10, 11);
12521
12522           if (bft_size == 0x00)
12523             f_ebytes = 1;
12524           else if (bft_size == 0x01)
12525             f_ebytes = 2;
12526           else if (bft_size == 0x02)
12527             f_ebytes = 4;
12528           else
12529             f_ebytes = 0;
12530
12531           /* Handle VST1.  */
12532           if (!(b_bits & 0x0b) || b_bits == 0x08)
12533             thumb2_insn_r->mem_rec_count = 1;
12534           /* Handle VST2.  */
12535           else if ((b_bits & 0x0b) == 0x01 || b_bits == 0x09)
12536             thumb2_insn_r->mem_rec_count = 2;
12537           /* Handle VST3.  */
12538           else if ((b_bits & 0x0b) == 0x02 || b_bits == 0x0a)
12539             thumb2_insn_r->mem_rec_count = 3;
12540           /* Handle VST4.  */
12541           else if ((b_bits & 0x0b) == 0x03 || b_bits == 0x0b)
12542             thumb2_insn_r->mem_rec_count = 4;
12543
12544           for (index_m = 0; index_m < thumb2_insn_r->mem_rec_count; index_m++)
12545             {
12546               record_buf_mem[index_m] = f_ebytes;
12547               record_buf_mem[index_m] = address + (index_m * f_ebytes);
12548             }
12549         }
12550     }
12551   else
12552     {
12553       if (!a_bit)
12554         {
12555           /* Handle VLD1.  */
12556           if (b_bits == 0x02 || b_bits == 0x0a || (b_bits & 0x0e) == 0x06)
12557             thumb2_insn_r->reg_rec_count = 1;
12558           /* Handle VLD2.  */
12559           else if (b_bits == 0x03 || (b_bits & 0x0e) == 0x08)
12560             thumb2_insn_r->reg_rec_count = 2;
12561           /* Handle VLD3.  */
12562           else if ((b_bits & 0x0e) == 0x04)
12563             thumb2_insn_r->reg_rec_count = 3;
12564           /* Handle VLD4.  */
12565           else if (!(b_bits & 0x0e))
12566             thumb2_insn_r->reg_rec_count = 4;
12567         }
12568       else
12569         {
12570           /* Handle VLD1.  */
12571           if (!(b_bits & 0x0b) || b_bits == 0x08 || b_bits == 0x0c)
12572             thumb2_insn_r->reg_rec_count = 1;
12573           /* Handle VLD2.  */
12574           else if ((b_bits & 0x0b) == 0x01 || b_bits == 0x09 || b_bits == 0x0d)
12575             thumb2_insn_r->reg_rec_count = 2;
12576           /* Handle VLD3.  */
12577           else if ((b_bits & 0x0b) == 0x02 || b_bits == 0x0a || b_bits == 0x0e)
12578             thumb2_insn_r->reg_rec_count = 3;
12579           /* Handle VLD4.  */
12580           else if ((b_bits & 0x0b) == 0x03 || b_bits == 0x0b || b_bits == 0x0f)
12581             thumb2_insn_r->reg_rec_count = 4;
12582
12583           for (index_r = 0; index_r < thumb2_insn_r->reg_rec_count; index_r++)
12584             record_buf[index_r] = reg_vd + ARM_D0_REGNUM + index_r;
12585         }
12586     }
12587
12588   if (bits (thumb2_insn_r->arm_insn, 0, 3) != 15)
12589     {
12590       record_buf[index_r] = reg_rn;
12591       thumb2_insn_r->reg_rec_count += 1;
12592     }
12593
12594   REG_ALLOC (thumb2_insn_r->arm_regs, thumb2_insn_r->reg_rec_count,
12595             record_buf);
12596   MEM_ALLOC (thumb2_insn_r->arm_mems, thumb2_insn_r->mem_rec_count,
12597             record_buf_mem);
12598   return 0;
12599 }
12600
12601 /* Decodes thumb2 instruction type and invokes its record handler.  */
12602
12603 static unsigned int
12604 thumb2_record_decode_insn_handler (insn_decode_record *thumb2_insn_r)
12605 {
12606   uint32_t op, op1, op2;
12607
12608   op = bit (thumb2_insn_r->arm_insn, 15);
12609   op1 = bits (thumb2_insn_r->arm_insn, 27, 28);
12610   op2 = bits (thumb2_insn_r->arm_insn, 20, 26);
12611
12612   if (op1 == 0x01)
12613     {
12614       if (!(op2 & 0x64 ))
12615         {
12616           /* Load/store multiple instruction.  */
12617           return thumb2_record_ld_st_multiple (thumb2_insn_r);
12618         }
12619       else if (!((op2 & 0x64) ^ 0x04))
12620         {
12621           /* Load/store (dual/exclusive) and table branch instruction.  */
12622           return thumb2_record_ld_st_dual_ex_tbb (thumb2_insn_r);
12623         }
12624       else if (!((op2 & 0x20) ^ 0x20))
12625         {
12626           /* Data-processing (shifted register).  */
12627           return thumb2_record_data_proc_sreg_mimm (thumb2_insn_r);
12628         }
12629       else if (op2 & 0x40)
12630         {
12631           /* Co-processor instructions.  */
12632           return thumb2_record_coproc_insn (thumb2_insn_r);
12633         }
12634     }
12635   else if (op1 == 0x02)
12636     {
12637       if (op)
12638         {
12639           /* Branches and miscellaneous control instructions.  */
12640           return thumb2_record_branch_misc_cntrl (thumb2_insn_r);
12641         }
12642       else if (op2 & 0x20)
12643         {
12644           /* Data-processing (plain binary immediate) instruction.  */
12645           return thumb2_record_ps_dest_generic (thumb2_insn_r);
12646         }
12647       else
12648         {
12649           /* Data-processing (modified immediate).  */
12650           return thumb2_record_data_proc_sreg_mimm (thumb2_insn_r);
12651         }
12652     }
12653   else if (op1 == 0x03)
12654     {
12655       if (!(op2 & 0x71 ))
12656         {
12657           /* Store single data item.  */
12658           return thumb2_record_str_single_data (thumb2_insn_r);
12659         }
12660       else if (!((op2 & 0x71) ^ 0x10))
12661         {
12662           /* Advanced SIMD or structure load/store instructions.  */
12663           return thumb2_record_asimd_struct_ld_st (thumb2_insn_r);
12664         }
12665       else if (!((op2 & 0x67) ^ 0x01))
12666         {
12667           /* Load byte, memory hints instruction.  */
12668           return thumb2_record_ld_mem_hints (thumb2_insn_r);
12669         }
12670       else if (!((op2 & 0x67) ^ 0x03))
12671         {
12672           /* Load halfword, memory hints instruction.  */
12673           return thumb2_record_ld_mem_hints (thumb2_insn_r);
12674         }
12675       else if (!((op2 & 0x67) ^ 0x05))
12676         {
12677           /* Load word instruction.  */
12678           return thumb2_record_ld_word (thumb2_insn_r);
12679         }
12680       else if (!((op2 & 0x70) ^ 0x20))
12681         {
12682           /* Data-processing (register) instruction.  */
12683           return thumb2_record_ps_dest_generic (thumb2_insn_r);
12684         }
12685       else if (!((op2 & 0x78) ^ 0x30))
12686         {
12687           /* Multiply, multiply accumulate, abs diff instruction.  */
12688           return thumb2_record_ps_dest_generic (thumb2_insn_r);
12689         }
12690       else if (!((op2 & 0x78) ^ 0x38))
12691         {
12692           /* Long multiply, long multiply accumulate, and divide.  */
12693           return thumb2_record_lmul_lmla_div (thumb2_insn_r);
12694         }
12695       else if (op2 & 0x40)
12696         {
12697           /* Co-processor instructions.  */
12698           return thumb2_record_coproc_insn (thumb2_insn_r);
12699         }
12700    }
12701
12702   return -1;
12703 }
12704
12705 /* Extracts arm/thumb/thumb2 insn depending on the size, and returns 0 on success 
12706 and positive val on fauilure.  */
12707
12708 static int
12709 extract_arm_insn (insn_decode_record *insn_record, uint32_t insn_size)
12710 {
12711   gdb_byte buf[insn_size];
12712
12713   memset (&buf[0], 0, insn_size);
12714   
12715   if (target_read_memory (insn_record->this_addr, &buf[0], insn_size))
12716     return 1;
12717   insn_record->arm_insn = (uint32_t) extract_unsigned_integer (&buf[0],
12718                            insn_size, 
12719                            gdbarch_byte_order_for_code (insn_record->gdbarch));
12720   return 0;
12721 }
12722
12723 typedef int (*sti_arm_hdl_fp_t) (insn_decode_record*);
12724
12725 /* Decode arm/thumb insn depending on condition cods and opcodes; and
12726    dispatch it.  */
12727
12728 static int
12729 decode_insn (insn_decode_record *arm_record, record_type_t record_type,
12730              uint32_t insn_size)
12731 {
12732
12733   /* (Starting from numerical 0); bits 25, 26, 27 decodes type of arm
12734      instruction.  */
12735   static const sti_arm_hdl_fp_t arm_handle_insn[8] =
12736   {
12737     arm_record_data_proc_misc_ld_str,   /* 000.  */
12738     arm_record_data_proc_imm,           /* 001.  */
12739     arm_record_ld_st_imm_offset,        /* 010.  */
12740     arm_record_ld_st_reg_offset,        /* 011.  */
12741     arm_record_ld_st_multiple,          /* 100.  */
12742     arm_record_b_bl,                    /* 101.  */
12743     arm_record_asimd_vfp_coproc,        /* 110.  */
12744     arm_record_coproc_data_proc         /* 111.  */
12745   };
12746
12747   /* (Starting from numerical 0); bits 13,14,15 decodes type of thumb
12748      instruction.  */
12749   static const sti_arm_hdl_fp_t thumb_handle_insn[8] =
12750   { \
12751     thumb_record_shift_add_sub,        /* 000.  */
12752     thumb_record_add_sub_cmp_mov,      /* 001.  */
12753     thumb_record_ld_st_reg_offset,     /* 010.  */
12754     thumb_record_ld_st_imm_offset,     /* 011.  */
12755     thumb_record_ld_st_stack,          /* 100.  */
12756     thumb_record_misc,                 /* 101.  */
12757     thumb_record_ldm_stm_swi,          /* 110.  */
12758     thumb_record_branch                /* 111.  */
12759   };
12760
12761   uint32_t ret = 0;    /* return value: negative:failure   0:success.  */
12762   uint32_t insn_id = 0;
12763
12764   if (extract_arm_insn (arm_record, insn_size))
12765     {
12766       if (record_debug)
12767         {
12768           printf_unfiltered (_("Process record: error reading memory at "
12769                                "addr %s len = %d.\n"),
12770                              paddress (arm_record->gdbarch,
12771                                        arm_record->this_addr), insn_size);
12772         }
12773       return -1;
12774     }
12775   else if (ARM_RECORD == record_type)
12776     {
12777       arm_record->cond = bits (arm_record->arm_insn, 28, 31);
12778       insn_id = bits (arm_record->arm_insn, 25, 27);
12779
12780       if (arm_record->cond == 0xf)
12781         ret = arm_record_extension_space (arm_record);
12782       else
12783         {
12784           /* If this insn has fallen into extension space
12785              then we need not decode it anymore.  */
12786           ret = arm_handle_insn[insn_id] (arm_record);
12787         }
12788       if (ret != ARM_RECORD_SUCCESS)
12789         {
12790           arm_record_unsupported_insn (arm_record);
12791           ret = -1;
12792         }
12793     }
12794   else if (THUMB_RECORD == record_type)
12795     {
12796       /* As thumb does not have condition codes, we set negative.  */
12797       arm_record->cond = -1;
12798       insn_id = bits (arm_record->arm_insn, 13, 15);
12799       ret = thumb_handle_insn[insn_id] (arm_record);
12800       if (ret != ARM_RECORD_SUCCESS)
12801         {
12802           arm_record_unsupported_insn (arm_record);
12803           ret = -1;
12804         }
12805     }
12806   else if (THUMB2_RECORD == record_type)
12807     {
12808       /* As thumb does not have condition codes, we set negative.  */
12809       arm_record->cond = -1;
12810
12811       /* Swap first half of 32bit thumb instruction with second half.  */
12812       arm_record->arm_insn
12813         = (arm_record->arm_insn >> 16) | (arm_record->arm_insn << 16);
12814
12815       ret = thumb2_record_decode_insn_handler (arm_record);
12816
12817       if (ret != ARM_RECORD_SUCCESS)
12818         {
12819           arm_record_unsupported_insn (arm_record);
12820           ret = -1;
12821         }
12822     }
12823   else
12824     {
12825       /* Throw assertion.  */
12826       gdb_assert_not_reached ("not a valid instruction, could not decode");
12827     }
12828
12829   return ret;
12830 }
12831
12832
12833 /* Cleans up local record registers and memory allocations.  */
12834
12835 static void 
12836 deallocate_reg_mem (insn_decode_record *record)
12837 {
12838   xfree (record->arm_regs);
12839   xfree (record->arm_mems);    
12840 }
12841
12842
12843 /* Parse the current instruction and record the values of the registers and
12844    memory that will be changed in current instruction to record_arch_list".
12845    Return -1 if something is wrong.  */
12846
12847 int
12848 arm_process_record (struct gdbarch *gdbarch, struct regcache *regcache,
12849                     CORE_ADDR insn_addr)
12850 {
12851
12852   uint32_t no_of_rec = 0;
12853   uint32_t ret = 0;  /* return value: -1:record failure ;  0:success  */
12854   ULONGEST t_bit = 0, insn_id = 0;
12855
12856   ULONGEST u_regval = 0;
12857
12858   insn_decode_record arm_record;
12859
12860   memset (&arm_record, 0, sizeof (insn_decode_record));
12861   arm_record.regcache = regcache;
12862   arm_record.this_addr = insn_addr;
12863   arm_record.gdbarch = gdbarch;
12864
12865
12866   if (record_debug > 1)
12867     {
12868       fprintf_unfiltered (gdb_stdlog, "Process record: arm_process_record "
12869                           "addr = %s\n",
12870       paddress (gdbarch, arm_record.this_addr));
12871     }
12872
12873   if (extract_arm_insn (&arm_record, 2))
12874     {
12875       if (record_debug)
12876         {
12877           printf_unfiltered (_("Process record: error reading memory at "
12878                                "addr %s len = %d.\n"),
12879                              paddress (arm_record.gdbarch,
12880                                        arm_record.this_addr), 2);
12881         }
12882       return -1;
12883     }
12884
12885   /* Check the insn, whether it is thumb or arm one.  */
12886
12887   t_bit = arm_psr_thumb_bit (arm_record.gdbarch);
12888   regcache_raw_read_unsigned (arm_record.regcache, ARM_PS_REGNUM, &u_regval);
12889
12890
12891   if (!(u_regval & t_bit))
12892     {
12893       /* We are decoding arm insn.  */
12894       ret = decode_insn (&arm_record, ARM_RECORD, ARM_INSN_SIZE_BYTES);
12895     }
12896   else
12897     {
12898       insn_id = bits (arm_record.arm_insn, 11, 15);
12899       /* is it thumb2 insn?  */
12900       if ((0x1D == insn_id) || (0x1E == insn_id) || (0x1F == insn_id))
12901         {
12902           ret = decode_insn (&arm_record, THUMB2_RECORD,
12903                              THUMB2_INSN_SIZE_BYTES);
12904         }
12905       else
12906         {
12907           /* We are decoding thumb insn.  */
12908           ret = decode_insn (&arm_record, THUMB_RECORD, THUMB_INSN_SIZE_BYTES);
12909         }
12910     }
12911
12912   if (0 == ret)
12913     {
12914       /* Record registers.  */
12915       record_full_arch_list_add_reg (arm_record.regcache, ARM_PC_REGNUM);
12916       if (arm_record.arm_regs)
12917         {
12918           for (no_of_rec = 0; no_of_rec < arm_record.reg_rec_count; no_of_rec++)
12919             {
12920               if (record_full_arch_list_add_reg
12921                   (arm_record.regcache , arm_record.arm_regs[no_of_rec]))
12922                 ret = -1;
12923             }
12924         }
12925       /* Record memories.  */
12926       if (arm_record.arm_mems)
12927         {
12928           for (no_of_rec = 0; no_of_rec < arm_record.mem_rec_count; no_of_rec++)
12929             {
12930               if (record_full_arch_list_add_mem
12931                   ((CORE_ADDR)arm_record.arm_mems[no_of_rec].addr,
12932                    arm_record.arm_mems[no_of_rec].len))
12933                 ret = -1;
12934             }
12935         }
12936
12937       if (record_full_arch_list_add_end ())
12938         ret = -1;
12939     }
12940
12941
12942   deallocate_reg_mem (&arm_record);
12943
12944   return ret;
12945 }