Add AArch64 Pointer Authentication to the NEWS file
[external/binutils.git] / gdb / arm-tdep.c
1 /* Common target dependent code for GDB on ARM systems.
2
3    Copyright (C) 1988-2019 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 "disasm.h"
31 #include "regcache.h"
32 #include "reggroups.h"
33 #include "target-float.h"
34 #include "value.h"
35 #include "arch-utils.h"
36 #include "osabi.h"
37 #include "frame-unwind.h"
38 #include "frame-base.h"
39 #include "trad-frame.h"
40 #include "objfiles.h"
41 #include "dwarf2-frame.h"
42 #include "gdbtypes.h"
43 #include "prologue-value.h"
44 #include "remote.h"
45 #include "target-descriptions.h"
46 #include "user-regs.h"
47 #include "observable.h"
48
49 #include "arch/arm.h"
50 #include "arch/arm-get-next-pcs.h"
51 #include "arm-tdep.h"
52 #include "gdb/sim-arm.h"
53
54 #include "elf-bfd.h"
55 #include "coff/internal.h"
56 #include "elf/arm.h"
57
58 #include "common/vec.h"
59
60 #include "record.h"
61 #include "record-full.h"
62 #include <algorithm>
63
64 #include "features/arm/arm-with-m.c"
65 #include "features/arm/arm-with-m-fpa-layout.c"
66 #include "features/arm/arm-with-m-vfp-d16.c"
67 #include "features/arm/arm-with-iwmmxt.c"
68 #include "features/arm/arm-with-vfpv2.c"
69 #include "features/arm/arm-with-vfpv3.c"
70 #include "features/arm/arm-with-neon.c"
71
72 #if GDB_SELF_TEST
73 #include "common/selftest.h"
74 #endif
75
76 static int arm_debug;
77
78 /* Macros for setting and testing a bit in a minimal symbol that marks
79    it as Thumb function.  The MSB of the minimal symbol's "info" field
80    is used for this purpose.
81
82    MSYMBOL_SET_SPECIAL  Actually sets the "special" bit.
83    MSYMBOL_IS_SPECIAL   Tests the "special" bit in a minimal symbol.  */
84
85 #define MSYMBOL_SET_SPECIAL(msym)                               \
86         MSYMBOL_TARGET_FLAG_1 (msym) = 1
87
88 #define MSYMBOL_IS_SPECIAL(msym)                                \
89         MSYMBOL_TARGET_FLAG_1 (msym)
90
91 /* Per-objfile data used for mapping symbols.  */
92 static const struct objfile_data *arm_objfile_data_key;
93
94 struct arm_mapping_symbol
95 {
96   bfd_vma value;
97   char type;
98 };
99 typedef struct arm_mapping_symbol arm_mapping_symbol_s;
100 DEF_VEC_O(arm_mapping_symbol_s);
101
102 struct arm_per_objfile
103 {
104   VEC(arm_mapping_symbol_s) **section_maps;
105 };
106
107 /* The list of available "set arm ..." and "show arm ..." commands.  */
108 static struct cmd_list_element *setarmcmdlist = NULL;
109 static struct cmd_list_element *showarmcmdlist = NULL;
110
111 /* The type of floating-point to use.  Keep this in sync with enum
112    arm_float_model, and the help string in _initialize_arm_tdep.  */
113 static const char *const fp_model_strings[] =
114 {
115   "auto",
116   "softfpa",
117   "fpa",
118   "softvfp",
119   "vfp",
120   NULL
121 };
122
123 /* A variable that can be configured by the user.  */
124 static enum arm_float_model arm_fp_model = ARM_FLOAT_AUTO;
125 static const char *current_fp_model = "auto";
126
127 /* The ABI to use.  Keep this in sync with arm_abi_kind.  */
128 static const char *const arm_abi_strings[] =
129 {
130   "auto",
131   "APCS",
132   "AAPCS",
133   NULL
134 };
135
136 /* A variable that can be configured by the user.  */
137 static enum arm_abi_kind arm_abi_global = ARM_ABI_AUTO;
138 static const char *arm_abi_string = "auto";
139
140 /* The execution mode to assume.  */
141 static const char *const arm_mode_strings[] =
142   {
143     "auto",
144     "arm",
145     "thumb",
146     NULL
147   };
148
149 static const char *arm_fallback_mode_string = "auto";
150 static const char *arm_force_mode_string = "auto";
151
152 /* The standard register names, and all the valid aliases for them.  Note
153    that `fp', `sp' and `pc' are not added in this alias list, because they
154    have been added as builtin user registers in
155    std-regs.c:_initialize_frame_reg.  */
156 static const struct
157 {
158   const char *name;
159   int regnum;
160 } arm_register_aliases[] = {
161   /* Basic register numbers.  */
162   { "r0", 0 },
163   { "r1", 1 },
164   { "r2", 2 },
165   { "r3", 3 },
166   { "r4", 4 },
167   { "r5", 5 },
168   { "r6", 6 },
169   { "r7", 7 },
170   { "r8", 8 },
171   { "r9", 9 },
172   { "r10", 10 },
173   { "r11", 11 },
174   { "r12", 12 },
175   { "r13", 13 },
176   { "r14", 14 },
177   { "r15", 15 },
178   /* Synonyms (argument and variable registers).  */
179   { "a1", 0 },
180   { "a2", 1 },
181   { "a3", 2 },
182   { "a4", 3 },
183   { "v1", 4 },
184   { "v2", 5 },
185   { "v3", 6 },
186   { "v4", 7 },
187   { "v5", 8 },
188   { "v6", 9 },
189   { "v7", 10 },
190   { "v8", 11 },
191   /* Other platform-specific names for r9.  */
192   { "sb", 9 },
193   { "tr", 9 },
194   /* Special names.  */
195   { "ip", 12 },
196   { "lr", 14 },
197   /* Names used by GCC (not listed in the ARM EABI).  */
198   { "sl", 10 },
199   /* A special name from the older ATPCS.  */
200   { "wr", 7 },
201 };
202
203 static const char *const arm_register_names[] =
204 {"r0",  "r1",  "r2",  "r3",     /*  0  1  2  3 */
205  "r4",  "r5",  "r6",  "r7",     /*  4  5  6  7 */
206  "r8",  "r9",  "r10", "r11",    /*  8  9 10 11 */
207  "r12", "sp",  "lr",  "pc",     /* 12 13 14 15 */
208  "f0",  "f1",  "f2",  "f3",     /* 16 17 18 19 */
209  "f4",  "f5",  "f6",  "f7",     /* 20 21 22 23 */
210  "fps", "cpsr" };               /* 24 25       */
211
212 /* Holds the current set of options to be passed to the disassembler.  */
213 static char *arm_disassembler_options;
214
215 /* Valid register name styles.  */
216 static const char **valid_disassembly_styles;
217
218 /* Disassembly style to use. Default to "std" register names.  */
219 static const char *disassembly_style;
220
221 /* This is used to keep the bfd arch_info in sync with the disassembly
222    style.  */
223 static void set_disassembly_style_sfunc (const char *, int,
224                                          struct cmd_list_element *);
225 static void show_disassembly_style_sfunc (struct ui_file *, int,
226                                           struct cmd_list_element *,
227                                           const char *);
228
229 static enum register_status arm_neon_quad_read (struct gdbarch *gdbarch,
230                                                 readable_regcache *regcache,
231                                                 int regnum, gdb_byte *buf);
232 static void arm_neon_quad_write (struct gdbarch *gdbarch,
233                                  struct regcache *regcache,
234                                  int regnum, const gdb_byte *buf);
235
236 static CORE_ADDR
237   arm_get_next_pcs_syscall_next_pc (struct arm_get_next_pcs *self);
238
239
240 /* get_next_pcs operations.  */
241 static struct arm_get_next_pcs_ops arm_get_next_pcs_ops = {
242   arm_get_next_pcs_read_memory_unsigned_integer,
243   arm_get_next_pcs_syscall_next_pc,
244   arm_get_next_pcs_addr_bits_remove,
245   arm_get_next_pcs_is_thumb,
246   NULL,
247 };
248
249 struct arm_prologue_cache
250 {
251   /* The stack pointer at the time this frame was created; i.e. the
252      caller's stack pointer when this function was called.  It is used
253      to identify this frame.  */
254   CORE_ADDR prev_sp;
255
256   /* The frame base for this frame is just prev_sp - frame size.
257      FRAMESIZE is the distance from the frame pointer to the
258      initial stack pointer.  */
259
260   int framesize;
261
262   /* The register used to hold the frame pointer for this frame.  */
263   int framereg;
264
265   /* Saved register offsets.  */
266   struct trad_frame_saved_reg *saved_regs;
267 };
268
269 static CORE_ADDR arm_analyze_prologue (struct gdbarch *gdbarch,
270                                        CORE_ADDR prologue_start,
271                                        CORE_ADDR prologue_end,
272                                        struct arm_prologue_cache *cache);
273
274 /* Architecture version for displaced stepping.  This effects the behaviour of
275    certain instructions, and really should not be hard-wired.  */
276
277 #define DISPLACED_STEPPING_ARCH_VERSION         5
278
279 /* Set to true if the 32-bit mode is in use.  */
280
281 int arm_apcs_32 = 1;
282
283 /* Return the bit mask in ARM_PS_REGNUM that indicates Thumb mode.  */
284
285 int
286 arm_psr_thumb_bit (struct gdbarch *gdbarch)
287 {
288   if (gdbarch_tdep (gdbarch)->is_m)
289     return XPSR_T;
290   else
291     return CPSR_T;
292 }
293
294 /* Determine if the processor is currently executing in Thumb mode.  */
295
296 int
297 arm_is_thumb (struct regcache *regcache)
298 {
299   ULONGEST cpsr;
300   ULONGEST t_bit = arm_psr_thumb_bit (regcache->arch ());
301
302   cpsr = regcache_raw_get_unsigned (regcache, ARM_PS_REGNUM);
303
304   return (cpsr & t_bit) != 0;
305 }
306
307 /* Determine if FRAME is executing in Thumb mode.  */
308
309 int
310 arm_frame_is_thumb (struct frame_info *frame)
311 {
312   CORE_ADDR cpsr;
313   ULONGEST t_bit = arm_psr_thumb_bit (get_frame_arch (frame));
314
315   /* Every ARM frame unwinder can unwind the T bit of the CPSR, either
316      directly (from a signal frame or dummy frame) or by interpreting
317      the saved LR (from a prologue or DWARF frame).  So consult it and
318      trust the unwinders.  */
319   cpsr = get_frame_register_unsigned (frame, ARM_PS_REGNUM);
320
321   return (cpsr & t_bit) != 0;
322 }
323
324 /* Callback for VEC_lower_bound.  */
325
326 static inline int
327 arm_compare_mapping_symbols (const struct arm_mapping_symbol *lhs,
328                              const struct arm_mapping_symbol *rhs)
329 {
330   return lhs->value < rhs->value;
331 }
332
333 /* Search for the mapping symbol covering MEMADDR.  If one is found,
334    return its type.  Otherwise, return 0.  If START is non-NULL,
335    set *START to the location of the mapping symbol.  */
336
337 static char
338 arm_find_mapping_symbol (CORE_ADDR memaddr, CORE_ADDR *start)
339 {
340   struct obj_section *sec;
341
342   /* If there are mapping symbols, consult them.  */
343   sec = find_pc_section (memaddr);
344   if (sec != NULL)
345     {
346       struct arm_per_objfile *data;
347       VEC(arm_mapping_symbol_s) *map;
348       struct arm_mapping_symbol map_key = { memaddr - obj_section_addr (sec),
349                                             0 };
350       unsigned int idx;
351
352       data = (struct arm_per_objfile *) objfile_data (sec->objfile,
353                                                       arm_objfile_data_key);
354       if (data != NULL)
355         {
356           map = data->section_maps[sec->the_bfd_section->index];
357           if (!VEC_empty (arm_mapping_symbol_s, map))
358             {
359               struct arm_mapping_symbol *map_sym;
360
361               idx = VEC_lower_bound (arm_mapping_symbol_s, map, &map_key,
362                                      arm_compare_mapping_symbols);
363
364               /* VEC_lower_bound finds the earliest ordered insertion
365                  point.  If the following symbol starts at this exact
366                  address, we use that; otherwise, the preceding
367                  mapping symbol covers this address.  */
368               if (idx < VEC_length (arm_mapping_symbol_s, map))
369                 {
370                   map_sym = VEC_index (arm_mapping_symbol_s, map, idx);
371                   if (map_sym->value == map_key.value)
372                     {
373                       if (start)
374                         *start = map_sym->value + obj_section_addr (sec);
375                       return map_sym->type;
376                     }
377                 }
378
379               if (idx > 0)
380                 {
381                   map_sym = VEC_index (arm_mapping_symbol_s, map, idx - 1);
382                   if (start)
383                     *start = map_sym->value + obj_section_addr (sec);
384                   return map_sym->type;
385                 }
386             }
387         }
388     }
389
390   return 0;
391 }
392
393 /* Determine if the program counter specified in MEMADDR is in a Thumb
394    function.  This function should be called for addresses unrelated to
395    any executing frame; otherwise, prefer arm_frame_is_thumb.  */
396
397 int
398 arm_pc_is_thumb (struct gdbarch *gdbarch, CORE_ADDR memaddr)
399 {
400   struct bound_minimal_symbol sym;
401   char type;
402   arm_displaced_step_closure *dsc
403     = ((arm_displaced_step_closure * )
404         get_displaced_step_closure_by_addr (memaddr));
405
406   /* If checking the mode of displaced instruction in copy area, the mode
407      should be determined by instruction on the original address.  */
408   if (dsc)
409     {
410       if (debug_displaced)
411         fprintf_unfiltered (gdb_stdlog,
412                             "displaced: check mode of %.8lx instead of %.8lx\n",
413                             (unsigned long) dsc->insn_addr,
414                             (unsigned long) memaddr);
415       memaddr = dsc->insn_addr;
416     }
417
418   /* If bit 0 of the address is set, assume this is a Thumb address.  */
419   if (IS_THUMB_ADDR (memaddr))
420     return 1;
421
422   /* If the user wants to override the symbol table, let him.  */
423   if (strcmp (arm_force_mode_string, "arm") == 0)
424     return 0;
425   if (strcmp (arm_force_mode_string, "thumb") == 0)
426     return 1;
427
428   /* ARM v6-M and v7-M are always in Thumb mode.  */
429   if (gdbarch_tdep (gdbarch)->is_m)
430     return 1;
431
432   /* If there are mapping symbols, consult them.  */
433   type = arm_find_mapping_symbol (memaddr, NULL);
434   if (type)
435     return type == 't';
436
437   /* Thumb functions have a "special" bit set in minimal symbols.  */
438   sym = lookup_minimal_symbol_by_pc (memaddr);
439   if (sym.minsym)
440     return (MSYMBOL_IS_SPECIAL (sym.minsym));
441
442   /* If the user wants to override the fallback mode, let them.  */
443   if (strcmp (arm_fallback_mode_string, "arm") == 0)
444     return 0;
445   if (strcmp (arm_fallback_mode_string, "thumb") == 0)
446     return 1;
447
448   /* If we couldn't find any symbol, but we're talking to a running
449      target, then trust the current value of $cpsr.  This lets
450      "display/i $pc" always show the correct mode (though if there is
451      a symbol table we will not reach here, so it still may not be
452      displayed in the mode it will be executed).  */
453   if (target_has_registers)
454     return arm_frame_is_thumb (get_current_frame ());
455
456   /* Otherwise we're out of luck; we assume ARM.  */
457   return 0;
458 }
459
460 /* Determine if the address specified equals any of these magic return
461    values, called EXC_RETURN, defined by the ARM v6-M and v7-M
462    architectures.
463
464    From ARMv6-M Reference Manual B1.5.8
465    Table B1-5 Exception return behavior
466
467    EXC_RETURN    Return To        Return Stack
468    0xFFFFFFF1    Handler mode     Main
469    0xFFFFFFF9    Thread mode      Main
470    0xFFFFFFFD    Thread mode      Process
471
472    From ARMv7-M Reference Manual B1.5.8
473    Table B1-8 EXC_RETURN definition of exception return behavior, no FP
474
475    EXC_RETURN    Return To        Return Stack
476    0xFFFFFFF1    Handler mode     Main
477    0xFFFFFFF9    Thread mode      Main
478    0xFFFFFFFD    Thread mode      Process
479
480    Table B1-9 EXC_RETURN definition of exception return behavior, with
481    FP
482
483    EXC_RETURN    Return To        Return Stack    Frame Type
484    0xFFFFFFE1    Handler mode     Main            Extended
485    0xFFFFFFE9    Thread mode      Main            Extended
486    0xFFFFFFED    Thread mode      Process         Extended
487    0xFFFFFFF1    Handler mode     Main            Basic
488    0xFFFFFFF9    Thread mode      Main            Basic
489    0xFFFFFFFD    Thread mode      Process         Basic
490
491    For more details see "B1.5.8 Exception return behavior"
492    in both ARMv6-M and ARMv7-M Architecture Reference Manuals.  */
493
494 static int
495 arm_m_addr_is_magic (CORE_ADDR addr)
496 {
497   switch (addr)
498     {
499       /* Values from Tables in B1.5.8 the EXC_RETURN definitions of
500          the exception return behavior.  */
501       case 0xffffffe1:
502       case 0xffffffe9:
503       case 0xffffffed:
504       case 0xfffffff1:
505       case 0xfffffff9:
506       case 0xfffffffd:
507         /* Address is magic.  */
508         return 1;
509
510       default:
511         /* Address is not magic.  */
512         return 0;
513     }
514 }
515
516 /* Remove useless bits from addresses in a running program.  */
517 static CORE_ADDR
518 arm_addr_bits_remove (struct gdbarch *gdbarch, CORE_ADDR val)
519 {
520   /* On M-profile devices, do not strip the low bit from EXC_RETURN
521      (the magic exception return address).  */
522   if (gdbarch_tdep (gdbarch)->is_m
523       && arm_m_addr_is_magic (val))
524     return val;
525
526   if (arm_apcs_32)
527     return UNMAKE_THUMB_ADDR (val);
528   else
529     return (val & 0x03fffffc);
530 }
531
532 /* Return 1 if PC is the start of a compiler helper function which
533    can be safely ignored during prologue skipping.  IS_THUMB is true
534    if the function is known to be a Thumb function due to the way it
535    is being called.  */
536 static int
537 skip_prologue_function (struct gdbarch *gdbarch, CORE_ADDR pc, int is_thumb)
538 {
539   enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
540   struct bound_minimal_symbol msym;
541
542   msym = lookup_minimal_symbol_by_pc (pc);
543   if (msym.minsym != NULL
544       && BMSYMBOL_VALUE_ADDRESS (msym) == pc
545       && MSYMBOL_LINKAGE_NAME (msym.minsym) != NULL)
546     {
547       const char *name = MSYMBOL_LINKAGE_NAME (msym.minsym);
548
549       /* The GNU linker's Thumb call stub to foo is named
550          __foo_from_thumb.  */
551       if (strstr (name, "_from_thumb") != NULL)
552         name += 2;
553
554       /* On soft-float targets, __truncdfsf2 is called to convert promoted
555          arguments to their argument types in non-prototyped
556          functions.  */
557       if (startswith (name, "__truncdfsf2"))
558         return 1;
559       if (startswith (name, "__aeabi_d2f"))
560         return 1;
561
562       /* Internal functions related to thread-local storage.  */
563       if (startswith (name, "__tls_get_addr"))
564         return 1;
565       if (startswith (name, "__aeabi_read_tp"))
566         return 1;
567     }
568   else
569     {
570       /* If we run against a stripped glibc, we may be unable to identify
571          special functions by name.  Check for one important case,
572          __aeabi_read_tp, by comparing the *code* against the default
573          implementation (this is hand-written ARM assembler in glibc).  */
574
575       if (!is_thumb
576           && read_code_unsigned_integer (pc, 4, byte_order_for_code)
577              == 0xe3e00a0f /* mov r0, #0xffff0fff */
578           && read_code_unsigned_integer (pc + 4, 4, byte_order_for_code)
579              == 0xe240f01f) /* sub pc, r0, #31 */
580         return 1;
581     }
582
583   return 0;
584 }
585
586 /* Extract the immediate from instruction movw/movt of encoding T.  INSN1 is
587    the first 16-bit of instruction, and INSN2 is the second 16-bit of
588    instruction.  */
589 #define EXTRACT_MOVW_MOVT_IMM_T(insn1, insn2) \
590   ((bits ((insn1), 0, 3) << 12)               \
591    | (bits ((insn1), 10, 10) << 11)           \
592    | (bits ((insn2), 12, 14) << 8)            \
593    | bits ((insn2), 0, 7))
594
595 /* Extract the immediate from instruction movw/movt of encoding A.  INSN is
596    the 32-bit instruction.  */
597 #define EXTRACT_MOVW_MOVT_IMM_A(insn) \
598   ((bits ((insn), 16, 19) << 12) \
599    | bits ((insn), 0, 11))
600
601 /* Decode immediate value; implements ThumbExpandImmediate pseudo-op.  */
602
603 static unsigned int
604 thumb_expand_immediate (unsigned int imm)
605 {
606   unsigned int count = imm >> 7;
607
608   if (count < 8)
609     switch (count / 2)
610       {
611       case 0:
612         return imm & 0xff;
613       case 1:
614         return (imm & 0xff) | ((imm & 0xff) << 16);
615       case 2:
616         return ((imm & 0xff) << 8) | ((imm & 0xff) << 24);
617       case 3:
618         return (imm & 0xff) | ((imm & 0xff) << 8)
619                 | ((imm & 0xff) << 16) | ((imm & 0xff) << 24);
620       }
621
622   return (0x80 | (imm & 0x7f)) << (32 - count);
623 }
624
625 /* Return 1 if the 16-bit Thumb instruction INSN restores SP in
626    epilogue, 0 otherwise.  */
627
628 static int
629 thumb_instruction_restores_sp (unsigned short insn)
630 {
631   return (insn == 0x46bd  /* mov sp, r7 */
632           || (insn & 0xff80) == 0xb000  /* add sp, imm */
633           || (insn & 0xfe00) == 0xbc00);  /* pop <registers> */
634 }
635
636 /* Analyze a Thumb prologue, looking for a recognizable stack frame
637    and frame pointer.  Scan until we encounter a store that could
638    clobber the stack frame unexpectedly, or an unknown instruction.
639    Return the last address which is definitely safe to skip for an
640    initial breakpoint.  */
641
642 static CORE_ADDR
643 thumb_analyze_prologue (struct gdbarch *gdbarch,
644                         CORE_ADDR start, CORE_ADDR limit,
645                         struct arm_prologue_cache *cache)
646 {
647   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
648   enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
649   int i;
650   pv_t regs[16];
651   CORE_ADDR offset;
652   CORE_ADDR unrecognized_pc = 0;
653
654   for (i = 0; i < 16; i++)
655     regs[i] = pv_register (i, 0);
656   pv_area stack (ARM_SP_REGNUM, gdbarch_addr_bit (gdbarch));
657
658   while (start < limit)
659     {
660       unsigned short insn;
661
662       insn = read_code_unsigned_integer (start, 2, byte_order_for_code);
663
664       if ((insn & 0xfe00) == 0xb400)            /* push { rlist } */
665         {
666           int regno;
667           int mask;
668
669           if (stack.store_would_trash (regs[ARM_SP_REGNUM]))
670             break;
671
672           /* Bits 0-7 contain a mask for registers R0-R7.  Bit 8 says
673              whether to save LR (R14).  */
674           mask = (insn & 0xff) | ((insn & 0x100) << 6);
675
676           /* Calculate offsets of saved R0-R7 and LR.  */
677           for (regno = ARM_LR_REGNUM; regno >= 0; regno--)
678             if (mask & (1 << regno))
679               {
680                 regs[ARM_SP_REGNUM] = pv_add_constant (regs[ARM_SP_REGNUM],
681                                                        -4);
682                 stack.store (regs[ARM_SP_REGNUM], 4, regs[regno]);
683               }
684         }
685       else if ((insn & 0xff80) == 0xb080)       /* sub sp, #imm */
686         {
687           offset = (insn & 0x7f) << 2;          /* get scaled offset */
688           regs[ARM_SP_REGNUM] = pv_add_constant (regs[ARM_SP_REGNUM],
689                                                  -offset);
690         }
691       else if (thumb_instruction_restores_sp (insn))
692         {
693           /* Don't scan past the epilogue.  */
694           break;
695         }
696       else if ((insn & 0xf800) == 0xa800)       /* add Rd, sp, #imm */
697         regs[bits (insn, 8, 10)] = pv_add_constant (regs[ARM_SP_REGNUM],
698                                                     (insn & 0xff) << 2);
699       else if ((insn & 0xfe00) == 0x1c00        /* add Rd, Rn, #imm */
700                && pv_is_register (regs[bits (insn, 3, 5)], ARM_SP_REGNUM))
701         regs[bits (insn, 0, 2)] = pv_add_constant (regs[bits (insn, 3, 5)],
702                                                    bits (insn, 6, 8));
703       else if ((insn & 0xf800) == 0x3000        /* add Rd, #imm */
704                && pv_is_register (regs[bits (insn, 8, 10)], ARM_SP_REGNUM))
705         regs[bits (insn, 8, 10)] = pv_add_constant (regs[bits (insn, 8, 10)],
706                                                     bits (insn, 0, 7));
707       else if ((insn & 0xfe00) == 0x1800        /* add Rd, Rn, Rm */
708                && pv_is_register (regs[bits (insn, 6, 8)], ARM_SP_REGNUM)
709                && pv_is_constant (regs[bits (insn, 3, 5)]))
710         regs[bits (insn, 0, 2)] = pv_add (regs[bits (insn, 3, 5)],
711                                           regs[bits (insn, 6, 8)]);
712       else if ((insn & 0xff00) == 0x4400        /* add Rd, Rm */
713                && pv_is_constant (regs[bits (insn, 3, 6)]))
714         {
715           int rd = (bit (insn, 7) << 3) + bits (insn, 0, 2);
716           int rm = bits (insn, 3, 6);
717           regs[rd] = pv_add (regs[rd], regs[rm]);
718         }
719       else if ((insn & 0xff00) == 0x4600)       /* mov hi, lo or mov lo, hi */
720         {
721           int dst_reg = (insn & 0x7) + ((insn & 0x80) >> 4);
722           int src_reg = (insn & 0x78) >> 3;
723           regs[dst_reg] = regs[src_reg];
724         }
725       else if ((insn & 0xf800) == 0x9000)       /* str rd, [sp, #off] */
726         {
727           /* Handle stores to the stack.  Normally pushes are used,
728              but with GCC -mtpcs-frame, there may be other stores
729              in the prologue to create the frame.  */
730           int regno = (insn >> 8) & 0x7;
731           pv_t addr;
732
733           offset = (insn & 0xff) << 2;
734           addr = pv_add_constant (regs[ARM_SP_REGNUM], offset);
735
736           if (stack.store_would_trash (addr))
737             break;
738
739           stack.store (addr, 4, regs[regno]);
740         }
741       else if ((insn & 0xf800) == 0x6000)       /* str rd, [rn, #off] */
742         {
743           int rd = bits (insn, 0, 2);
744           int rn = bits (insn, 3, 5);
745           pv_t addr;
746
747           offset = bits (insn, 6, 10) << 2;
748           addr = pv_add_constant (regs[rn], offset);
749
750           if (stack.store_would_trash (addr))
751             break;
752
753           stack.store (addr, 4, regs[rd]);
754         }
755       else if (((insn & 0xf800) == 0x7000       /* strb Rd, [Rn, #off] */
756                 || (insn & 0xf800) == 0x8000)   /* strh Rd, [Rn, #off] */
757                && pv_is_register (regs[bits (insn, 3, 5)], ARM_SP_REGNUM))
758         /* Ignore stores of argument registers to the stack.  */
759         ;
760       else if ((insn & 0xf800) == 0xc800        /* ldmia Rn!, { registers } */
761                && pv_is_register (regs[bits (insn, 8, 10)], ARM_SP_REGNUM))
762         /* Ignore block loads from the stack, potentially copying
763            parameters from memory.  */
764         ;
765       else if ((insn & 0xf800) == 0x9800        /* ldr Rd, [Rn, #immed] */
766                || ((insn & 0xf800) == 0x6800    /* ldr Rd, [sp, #immed] */
767                    && pv_is_register (regs[bits (insn, 3, 5)], ARM_SP_REGNUM)))
768         /* Similarly ignore single loads from the stack.  */
769         ;
770       else if ((insn & 0xffc0) == 0x0000        /* lsls Rd, Rm, #0 */
771                || (insn & 0xffc0) == 0x1c00)    /* add Rd, Rn, #0 */
772         /* Skip register copies, i.e. saves to another register
773            instead of the stack.  */
774         ;
775       else if ((insn & 0xf800) == 0x2000)       /* movs Rd, #imm */
776         /* Recognize constant loads; even with small stacks these are necessary
777            on Thumb.  */
778         regs[bits (insn, 8, 10)] = pv_constant (bits (insn, 0, 7));
779       else if ((insn & 0xf800) == 0x4800)       /* ldr Rd, [pc, #imm] */
780         {
781           /* Constant pool loads, for the same reason.  */
782           unsigned int constant;
783           CORE_ADDR loc;
784
785           loc = start + 4 + bits (insn, 0, 7) * 4;
786           constant = read_memory_unsigned_integer (loc, 4, byte_order);
787           regs[bits (insn, 8, 10)] = pv_constant (constant);
788         }
789       else if (thumb_insn_size (insn) == 4) /* 32-bit Thumb-2 instructions.  */
790         {
791           unsigned short inst2;
792
793           inst2 = read_code_unsigned_integer (start + 2, 2,
794                                               byte_order_for_code);
795
796           if ((insn & 0xf800) == 0xf000 && (inst2 & 0xe800) == 0xe800)
797             {
798               /* BL, BLX.  Allow some special function calls when
799                  skipping the prologue; GCC generates these before
800                  storing arguments to the stack.  */
801               CORE_ADDR nextpc;
802               int j1, j2, imm1, imm2;
803
804               imm1 = sbits (insn, 0, 10);
805               imm2 = bits (inst2, 0, 10);
806               j1 = bit (inst2, 13);
807               j2 = bit (inst2, 11);
808
809               offset = ((imm1 << 12) + (imm2 << 1));
810               offset ^= ((!j2) << 22) | ((!j1) << 23);
811
812               nextpc = start + 4 + offset;
813               /* For BLX make sure to clear the low bits.  */
814               if (bit (inst2, 12) == 0)
815                 nextpc = nextpc & 0xfffffffc;
816
817               if (!skip_prologue_function (gdbarch, nextpc,
818                                            bit (inst2, 12) != 0))
819                 break;
820             }
821
822           else if ((insn & 0xffd0) == 0xe900    /* stmdb Rn{!},
823                                                    { registers } */
824                    && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
825             {
826               pv_t addr = regs[bits (insn, 0, 3)];
827               int regno;
828
829               if (stack.store_would_trash (addr))
830                 break;
831
832               /* Calculate offsets of saved registers.  */
833               for (regno = ARM_LR_REGNUM; regno >= 0; regno--)
834                 if (inst2 & (1 << regno))
835                   {
836                     addr = pv_add_constant (addr, -4);
837                     stack.store (addr, 4, regs[regno]);
838                   }
839
840               if (insn & 0x0020)
841                 regs[bits (insn, 0, 3)] = addr;
842             }
843
844           else if ((insn & 0xff50) == 0xe940    /* strd Rt, Rt2,
845                                                    [Rn, #+/-imm]{!} */
846                    && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
847             {
848               int regno1 = bits (inst2, 12, 15);
849               int regno2 = bits (inst2, 8, 11);
850               pv_t addr = regs[bits (insn, 0, 3)];
851
852               offset = inst2 & 0xff;
853               if (insn & 0x0080)
854                 addr = pv_add_constant (addr, offset);
855               else
856                 addr = pv_add_constant (addr, -offset);
857
858               if (stack.store_would_trash (addr))
859                 break;
860
861               stack.store (addr, 4, regs[regno1]);
862               stack.store (pv_add_constant (addr, 4),
863                            4, regs[regno2]);
864
865               if (insn & 0x0020)
866                 regs[bits (insn, 0, 3)] = addr;
867             }
868
869           else if ((insn & 0xfff0) == 0xf8c0    /* str Rt,[Rn,+/-#imm]{!} */
870                    && (inst2 & 0x0c00) == 0x0c00
871                    && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
872             {
873               int regno = bits (inst2, 12, 15);
874               pv_t addr = regs[bits (insn, 0, 3)];
875
876               offset = inst2 & 0xff;
877               if (inst2 & 0x0200)
878                 addr = pv_add_constant (addr, offset);
879               else
880                 addr = pv_add_constant (addr, -offset);
881
882               if (stack.store_would_trash (addr))
883                 break;
884
885               stack.store (addr, 4, regs[regno]);
886
887               if (inst2 & 0x0100)
888                 regs[bits (insn, 0, 3)] = addr;
889             }
890
891           else if ((insn & 0xfff0) == 0xf8c0    /* str.w Rt,[Rn,#imm] */
892                    && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
893             {
894               int regno = bits (inst2, 12, 15);
895               pv_t addr;
896
897               offset = inst2 & 0xfff;
898               addr = pv_add_constant (regs[bits (insn, 0, 3)], offset);
899
900               if (stack.store_would_trash (addr))
901                 break;
902
903               stack.store (addr, 4, regs[regno]);
904             }
905
906           else if ((insn & 0xffd0) == 0xf880    /* str{bh}.w Rt,[Rn,#imm] */
907                    && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
908             /* Ignore stores of argument registers to the stack.  */
909             ;
910
911           else if ((insn & 0xffd0) == 0xf800    /* str{bh} Rt,[Rn,#+/-imm] */
912                    && (inst2 & 0x0d00) == 0x0c00
913                    && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
914             /* Ignore stores of argument registers to the stack.  */
915             ;
916
917           else if ((insn & 0xffd0) == 0xe890    /* ldmia Rn[!],
918                                                    { registers } */
919                    && (inst2 & 0x8000) == 0x0000
920                    && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
921             /* Ignore block loads from the stack, potentially copying
922                parameters from memory.  */
923             ;
924
925           else if ((insn & 0xffb0) == 0xe950    /* ldrd Rt, Rt2,
926                                                    [Rn, #+/-imm] */
927                    && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
928             /* Similarly ignore dual loads from the stack.  */
929             ;
930
931           else if ((insn & 0xfff0) == 0xf850    /* ldr Rt,[Rn,#+/-imm] */
932                    && (inst2 & 0x0d00) == 0x0c00
933                    && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
934             /* Similarly ignore single loads from the stack.  */
935             ;
936
937           else if ((insn & 0xfff0) == 0xf8d0    /* ldr.w Rt,[Rn,#imm] */
938                    && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
939             /* Similarly ignore single loads from the stack.  */
940             ;
941
942           else if ((insn & 0xfbf0) == 0xf100    /* add.w Rd, Rn, #imm */
943                    && (inst2 & 0x8000) == 0x0000)
944             {
945               unsigned int imm = ((bits (insn, 10, 10) << 11)
946                                   | (bits (inst2, 12, 14) << 8)
947                                   | bits (inst2, 0, 7));
948
949               regs[bits (inst2, 8, 11)]
950                 = pv_add_constant (regs[bits (insn, 0, 3)],
951                                    thumb_expand_immediate (imm));
952             }
953
954           else if ((insn & 0xfbf0) == 0xf200    /* addw Rd, Rn, #imm */
955                    && (inst2 & 0x8000) == 0x0000)
956             {
957               unsigned int imm = ((bits (insn, 10, 10) << 11)
958                                   | (bits (inst2, 12, 14) << 8)
959                                   | bits (inst2, 0, 7));
960
961               regs[bits (inst2, 8, 11)]
962                 = pv_add_constant (regs[bits (insn, 0, 3)], imm);
963             }
964
965           else if ((insn & 0xfbf0) == 0xf1a0    /* sub.w Rd, Rn, #imm */
966                    && (inst2 & 0x8000) == 0x0000)
967             {
968               unsigned int imm = ((bits (insn, 10, 10) << 11)
969                                   | (bits (inst2, 12, 14) << 8)
970                                   | bits (inst2, 0, 7));
971
972               regs[bits (inst2, 8, 11)]
973                 = pv_add_constant (regs[bits (insn, 0, 3)],
974                                    - (CORE_ADDR) thumb_expand_immediate (imm));
975             }
976
977           else if ((insn & 0xfbf0) == 0xf2a0    /* subw Rd, Rn, #imm */
978                    && (inst2 & 0x8000) == 0x0000)
979             {
980               unsigned int imm = ((bits (insn, 10, 10) << 11)
981                                   | (bits (inst2, 12, 14) << 8)
982                                   | bits (inst2, 0, 7));
983
984               regs[bits (inst2, 8, 11)]
985                 = pv_add_constant (regs[bits (insn, 0, 3)], - (CORE_ADDR) imm);
986             }
987
988           else if ((insn & 0xfbff) == 0xf04f)   /* mov.w Rd, #const */
989             {
990               unsigned int imm = ((bits (insn, 10, 10) << 11)
991                                   | (bits (inst2, 12, 14) << 8)
992                                   | bits (inst2, 0, 7));
993
994               regs[bits (inst2, 8, 11)]
995                 = pv_constant (thumb_expand_immediate (imm));
996             }
997
998           else if ((insn & 0xfbf0) == 0xf240)   /* movw Rd, #const */
999             {
1000               unsigned int imm
1001                 = EXTRACT_MOVW_MOVT_IMM_T (insn, inst2);
1002
1003               regs[bits (inst2, 8, 11)] = pv_constant (imm);
1004             }
1005
1006           else if (insn == 0xea5f               /* mov.w Rd,Rm */
1007                    && (inst2 & 0xf0f0) == 0)
1008             {
1009               int dst_reg = (inst2 & 0x0f00) >> 8;
1010               int src_reg = inst2 & 0xf;
1011               regs[dst_reg] = regs[src_reg];
1012             }
1013
1014           else if ((insn & 0xff7f) == 0xf85f)   /* ldr.w Rt,<label> */
1015             {
1016               /* Constant pool loads.  */
1017               unsigned int constant;
1018               CORE_ADDR loc;
1019
1020               offset = bits (inst2, 0, 11);
1021               if (insn & 0x0080)
1022                 loc = start + 4 + offset;
1023               else
1024                 loc = start + 4 - offset;
1025
1026               constant = read_memory_unsigned_integer (loc, 4, byte_order);
1027               regs[bits (inst2, 12, 15)] = pv_constant (constant);
1028             }
1029
1030           else if ((insn & 0xff7f) == 0xe95f)   /* ldrd Rt,Rt2,<label> */
1031             {
1032               /* Constant pool loads.  */
1033               unsigned int constant;
1034               CORE_ADDR loc;
1035
1036               offset = bits (inst2, 0, 7) << 2;
1037               if (insn & 0x0080)
1038                 loc = start + 4 + offset;
1039               else
1040                 loc = start + 4 - offset;
1041
1042               constant = read_memory_unsigned_integer (loc, 4, byte_order);
1043               regs[bits (inst2, 12, 15)] = pv_constant (constant);
1044
1045               constant = read_memory_unsigned_integer (loc + 4, 4, byte_order);
1046               regs[bits (inst2, 8, 11)] = pv_constant (constant);
1047             }
1048
1049           else if (thumb2_instruction_changes_pc (insn, inst2))
1050             {
1051               /* Don't scan past anything that might change control flow.  */
1052               break;
1053             }
1054           else
1055             {
1056               /* The optimizer might shove anything into the prologue,
1057                  so we just skip what we don't recognize.  */
1058               unrecognized_pc = start;
1059             }
1060
1061           start += 2;
1062         }
1063       else if (thumb_instruction_changes_pc (insn))
1064         {
1065           /* Don't scan past anything that might change control flow.  */
1066           break;
1067         }
1068       else
1069         {
1070           /* The optimizer might shove anything into the prologue,
1071              so we just skip what we don't recognize.  */
1072           unrecognized_pc = start;
1073         }
1074
1075       start += 2;
1076     }
1077
1078   if (arm_debug)
1079     fprintf_unfiltered (gdb_stdlog, "Prologue scan stopped at %s\n",
1080                         paddress (gdbarch, start));
1081
1082   if (unrecognized_pc == 0)
1083     unrecognized_pc = start;
1084
1085   if (cache == NULL)
1086     return unrecognized_pc;
1087
1088   if (pv_is_register (regs[ARM_FP_REGNUM], ARM_SP_REGNUM))
1089     {
1090       /* Frame pointer is fp.  Frame size is constant.  */
1091       cache->framereg = ARM_FP_REGNUM;
1092       cache->framesize = -regs[ARM_FP_REGNUM].k;
1093     }
1094   else if (pv_is_register (regs[THUMB_FP_REGNUM], ARM_SP_REGNUM))
1095     {
1096       /* Frame pointer is r7.  Frame size is constant.  */
1097       cache->framereg = THUMB_FP_REGNUM;
1098       cache->framesize = -regs[THUMB_FP_REGNUM].k;
1099     }
1100   else
1101     {
1102       /* Try the stack pointer... this is a bit desperate.  */
1103       cache->framereg = ARM_SP_REGNUM;
1104       cache->framesize = -regs[ARM_SP_REGNUM].k;
1105     }
1106
1107   for (i = 0; i < 16; i++)
1108     if (stack.find_reg (gdbarch, i, &offset))
1109       cache->saved_regs[i].addr = offset;
1110
1111   return unrecognized_pc;
1112 }
1113
1114
1115 /* Try to analyze the instructions starting from PC, which load symbol
1116    __stack_chk_guard.  Return the address of instruction after loading this
1117    symbol, set the dest register number to *BASEREG, and set the size of
1118    instructions for loading symbol in OFFSET.  Return 0 if instructions are
1119    not recognized.  */
1120
1121 static CORE_ADDR
1122 arm_analyze_load_stack_chk_guard(CORE_ADDR pc, struct gdbarch *gdbarch,
1123                                  unsigned int *destreg, int *offset)
1124 {
1125   enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
1126   int is_thumb = arm_pc_is_thumb (gdbarch, pc);
1127   unsigned int low, high, address;
1128
1129   address = 0;
1130   if (is_thumb)
1131     {
1132       unsigned short insn1
1133         = read_code_unsigned_integer (pc, 2, byte_order_for_code);
1134
1135       if ((insn1 & 0xf800) == 0x4800) /* ldr Rd, #immed */
1136         {
1137           *destreg = bits (insn1, 8, 10);
1138           *offset = 2;
1139           address = (pc & 0xfffffffc) + 4 + (bits (insn1, 0, 7) << 2);
1140           address = read_memory_unsigned_integer (address, 4,
1141                                                   byte_order_for_code);
1142         }
1143       else if ((insn1 & 0xfbf0) == 0xf240) /* movw Rd, #const */
1144         {
1145           unsigned short insn2
1146             = read_code_unsigned_integer (pc + 2, 2, byte_order_for_code);
1147
1148           low = EXTRACT_MOVW_MOVT_IMM_T (insn1, insn2);
1149
1150           insn1
1151             = read_code_unsigned_integer (pc + 4, 2, byte_order_for_code);
1152           insn2
1153             = read_code_unsigned_integer (pc + 6, 2, byte_order_for_code);
1154
1155           /* movt Rd, #const */
1156           if ((insn1 & 0xfbc0) == 0xf2c0)
1157             {
1158               high = EXTRACT_MOVW_MOVT_IMM_T (insn1, insn2);
1159               *destreg = bits (insn2, 8, 11);
1160               *offset = 8;
1161               address = (high << 16 | low);
1162             }
1163         }
1164     }
1165   else
1166     {
1167       unsigned int insn
1168         = read_code_unsigned_integer (pc, 4, byte_order_for_code);
1169
1170       if ((insn & 0x0e5f0000) == 0x041f0000) /* ldr Rd, [PC, #immed] */
1171         {
1172           address = bits (insn, 0, 11) + pc + 8;
1173           address = read_memory_unsigned_integer (address, 4,
1174                                                   byte_order_for_code);
1175
1176           *destreg = bits (insn, 12, 15);
1177           *offset = 4;
1178         }
1179       else if ((insn & 0x0ff00000) == 0x03000000) /* movw Rd, #const */
1180         {
1181           low = EXTRACT_MOVW_MOVT_IMM_A (insn);
1182
1183           insn
1184             = read_code_unsigned_integer (pc + 4, 4, byte_order_for_code);
1185
1186           if ((insn & 0x0ff00000) == 0x03400000) /* movt Rd, #const */
1187             {
1188               high = EXTRACT_MOVW_MOVT_IMM_A (insn);
1189               *destreg = bits (insn, 12, 15);
1190               *offset = 8;
1191               address = (high << 16 | low);
1192             }
1193         }
1194     }
1195
1196   return address;
1197 }
1198
1199 /* Try to skip a sequence of instructions used for stack protector.  If PC
1200    points to the first instruction of this sequence, return the address of
1201    first instruction after this sequence, otherwise, return original PC.
1202
1203    On arm, this sequence of instructions is composed of mainly three steps,
1204      Step 1: load symbol __stack_chk_guard,
1205      Step 2: load from address of __stack_chk_guard,
1206      Step 3: store it to somewhere else.
1207
1208    Usually, instructions on step 2 and step 3 are the same on various ARM
1209    architectures.  On step 2, it is one instruction 'ldr Rx, [Rn, #0]', and
1210    on step 3, it is also one instruction 'str Rx, [r7, #immd]'.  However,
1211    instructions in step 1 vary from different ARM architectures.  On ARMv7,
1212    they are,
1213
1214         movw    Rn, #:lower16:__stack_chk_guard
1215         movt    Rn, #:upper16:__stack_chk_guard
1216
1217    On ARMv5t, it is,
1218
1219         ldr     Rn, .Label
1220         ....
1221         .Lable:
1222         .word   __stack_chk_guard
1223
1224    Since ldr/str is a very popular instruction, we can't use them as
1225    'fingerprint' or 'signature' of stack protector sequence.  Here we choose
1226    sequence {movw/movt, ldr}/ldr/str plus symbol __stack_chk_guard, if not
1227    stripped, as the 'fingerprint' of a stack protector cdoe sequence.  */
1228
1229 static CORE_ADDR
1230 arm_skip_stack_protector(CORE_ADDR pc, struct gdbarch *gdbarch)
1231 {
1232   enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
1233   unsigned int basereg;
1234   struct bound_minimal_symbol stack_chk_guard;
1235   int offset;
1236   int is_thumb = arm_pc_is_thumb (gdbarch, pc);
1237   CORE_ADDR addr;
1238
1239   /* Try to parse the instructions in Step 1.  */
1240   addr = arm_analyze_load_stack_chk_guard (pc, gdbarch,
1241                                            &basereg, &offset);
1242   if (!addr)
1243     return pc;
1244
1245   stack_chk_guard = lookup_minimal_symbol_by_pc (addr);
1246   /* ADDR must correspond to a symbol whose name is __stack_chk_guard.
1247      Otherwise, this sequence cannot be for stack protector.  */
1248   if (stack_chk_guard.minsym == NULL
1249       || !startswith (MSYMBOL_LINKAGE_NAME (stack_chk_guard.minsym), "__stack_chk_guard"))
1250    return pc;
1251
1252   if (is_thumb)
1253     {
1254       unsigned int destreg;
1255       unsigned short insn
1256         = read_code_unsigned_integer (pc + offset, 2, byte_order_for_code);
1257
1258       /* Step 2: ldr Rd, [Rn, #immed], encoding T1.  */
1259       if ((insn & 0xf800) != 0x6800)
1260         return pc;
1261       if (bits (insn, 3, 5) != basereg)
1262         return pc;
1263       destreg = bits (insn, 0, 2);
1264
1265       insn = read_code_unsigned_integer (pc + offset + 2, 2,
1266                                          byte_order_for_code);
1267       /* Step 3: str Rd, [Rn, #immed], encoding T1.  */
1268       if ((insn & 0xf800) != 0x6000)
1269         return pc;
1270       if (destreg != bits (insn, 0, 2))
1271         return pc;
1272     }
1273   else
1274     {
1275       unsigned int destreg;
1276       unsigned int insn
1277         = read_code_unsigned_integer (pc + offset, 4, byte_order_for_code);
1278
1279       /* Step 2: ldr Rd, [Rn, #immed], encoding A1.  */
1280       if ((insn & 0x0e500000) != 0x04100000)
1281         return pc;
1282       if (bits (insn, 16, 19) != basereg)
1283         return pc;
1284       destreg = bits (insn, 12, 15);
1285       /* Step 3: str Rd, [Rn, #immed], encoding A1.  */
1286       insn = read_code_unsigned_integer (pc + offset + 4,
1287                                            4, byte_order_for_code);
1288       if ((insn & 0x0e500000) != 0x04000000)
1289         return pc;
1290       if (bits (insn, 12, 15) != destreg)
1291         return pc;
1292     }
1293   /* The size of total two instructions ldr/str is 4 on Thumb-2, while 8
1294      on arm.  */
1295   if (is_thumb)
1296     return pc + offset + 4;
1297   else
1298     return pc + offset + 8;
1299 }
1300
1301 /* Advance the PC across any function entry prologue instructions to
1302    reach some "real" code.
1303
1304    The APCS (ARM Procedure Call Standard) defines the following
1305    prologue:
1306
1307    mov          ip, sp
1308    [stmfd       sp!, {a1,a2,a3,a4}]
1309    stmfd        sp!, {...,fp,ip,lr,pc}
1310    [stfe        f7, [sp, #-12]!]
1311    [stfe        f6, [sp, #-12]!]
1312    [stfe        f5, [sp, #-12]!]
1313    [stfe        f4, [sp, #-12]!]
1314    sub fp, ip, #nn @@ nn == 20 or 4 depending on second insn.  */
1315
1316 static CORE_ADDR
1317 arm_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR pc)
1318 {
1319   CORE_ADDR func_addr, limit_pc;
1320
1321   /* See if we can determine the end of the prologue via the symbol table.
1322      If so, then return either PC, or the PC after the prologue, whichever
1323      is greater.  */
1324   if (find_pc_partial_function (pc, NULL, &func_addr, NULL))
1325     {
1326       CORE_ADDR post_prologue_pc
1327         = skip_prologue_using_sal (gdbarch, func_addr);
1328       struct compunit_symtab *cust = find_pc_compunit_symtab (func_addr);
1329
1330       if (post_prologue_pc)
1331         post_prologue_pc
1332           = arm_skip_stack_protector (post_prologue_pc, gdbarch);
1333
1334
1335       /* GCC always emits a line note before the prologue and another
1336          one after, even if the two are at the same address or on the
1337          same line.  Take advantage of this so that we do not need to
1338          know every instruction that might appear in the prologue.  We
1339          will have producer information for most binaries; if it is
1340          missing (e.g. for -gstabs), assuming the GNU tools.  */
1341       if (post_prologue_pc
1342           && (cust == NULL
1343               || COMPUNIT_PRODUCER (cust) == NULL
1344               || startswith (COMPUNIT_PRODUCER (cust), "GNU ")
1345               || startswith (COMPUNIT_PRODUCER (cust), "clang ")))
1346         return post_prologue_pc;
1347
1348       if (post_prologue_pc != 0)
1349         {
1350           CORE_ADDR analyzed_limit;
1351
1352           /* For non-GCC compilers, make sure the entire line is an
1353              acceptable prologue; GDB will round this function's
1354              return value up to the end of the following line so we
1355              can not skip just part of a line (and we do not want to).
1356
1357              RealView does not treat the prologue specially, but does
1358              associate prologue code with the opening brace; so this
1359              lets us skip the first line if we think it is the opening
1360              brace.  */
1361           if (arm_pc_is_thumb (gdbarch, func_addr))
1362             analyzed_limit = thumb_analyze_prologue (gdbarch, func_addr,
1363                                                      post_prologue_pc, NULL);
1364           else
1365             analyzed_limit = arm_analyze_prologue (gdbarch, func_addr,
1366                                                    post_prologue_pc, NULL);
1367
1368           if (analyzed_limit != post_prologue_pc)
1369             return func_addr;
1370
1371           return post_prologue_pc;
1372         }
1373     }
1374
1375   /* Can't determine prologue from the symbol table, need to examine
1376      instructions.  */
1377
1378   /* Find an upper limit on the function prologue using the debug
1379      information.  If the debug information could not be used to provide
1380      that bound, then use an arbitrary large number as the upper bound.  */
1381   /* Like arm_scan_prologue, stop no later than pc + 64.  */
1382   limit_pc = skip_prologue_using_sal (gdbarch, pc);
1383   if (limit_pc == 0)
1384     limit_pc = pc + 64;          /* Magic.  */
1385
1386
1387   /* Check if this is Thumb code.  */
1388   if (arm_pc_is_thumb (gdbarch, pc))
1389     return thumb_analyze_prologue (gdbarch, pc, limit_pc, NULL);
1390   else
1391     return arm_analyze_prologue (gdbarch, pc, limit_pc, NULL);
1392 }
1393
1394 /* *INDENT-OFF* */
1395 /* Function: thumb_scan_prologue (helper function for arm_scan_prologue)
1396    This function decodes a Thumb function prologue to determine:
1397      1) the size of the stack frame
1398      2) which registers are saved on it
1399      3) the offsets of saved regs
1400      4) the offset from the stack pointer to the frame pointer
1401
1402    A typical Thumb function prologue would create this stack frame
1403    (offsets relative to FP)
1404      old SP ->  24  stack parameters
1405                 20  LR
1406                 16  R7
1407      R7 ->       0  local variables (16 bytes)
1408      SP ->     -12  additional stack space (12 bytes)
1409    The frame size would thus be 36 bytes, and the frame offset would be
1410    12 bytes.  The frame register is R7.
1411    
1412    The comments for thumb_skip_prolog() describe the algorithm we use
1413    to detect the end of the prolog.  */
1414 /* *INDENT-ON* */
1415
1416 static void
1417 thumb_scan_prologue (struct gdbarch *gdbarch, CORE_ADDR prev_pc,
1418                      CORE_ADDR block_addr, struct arm_prologue_cache *cache)
1419 {
1420   CORE_ADDR prologue_start;
1421   CORE_ADDR prologue_end;
1422
1423   if (find_pc_partial_function (block_addr, NULL, &prologue_start,
1424                                 &prologue_end))
1425     {
1426       /* See comment in arm_scan_prologue for an explanation of
1427          this heuristics.  */
1428       if (prologue_end > prologue_start + 64)
1429         {
1430           prologue_end = prologue_start + 64;
1431         }
1432     }
1433   else
1434     /* We're in the boondocks: we have no idea where the start of the
1435        function is.  */
1436     return;
1437
1438   prologue_end = std::min (prologue_end, prev_pc);
1439
1440   thumb_analyze_prologue (gdbarch, prologue_start, prologue_end, cache);
1441 }
1442
1443 /* Return 1 if the ARM instruction INSN restores SP in epilogue, 0
1444    otherwise.  */
1445
1446 static int
1447 arm_instruction_restores_sp (unsigned int insn)
1448 {
1449   if (bits (insn, 28, 31) != INST_NV)
1450     {
1451       if ((insn & 0x0df0f000) == 0x0080d000
1452           /* ADD SP (register or immediate).  */
1453           || (insn & 0x0df0f000) == 0x0040d000
1454           /* SUB SP (register or immediate).  */
1455           || (insn & 0x0ffffff0) == 0x01a0d000
1456           /* MOV SP.  */
1457           || (insn & 0x0fff0000) == 0x08bd0000
1458           /* POP (LDMIA).  */
1459           || (insn & 0x0fff0000) == 0x049d0000)
1460           /* POP of a single register.  */
1461         return 1;
1462     }
1463
1464   return 0;
1465 }
1466
1467 /* Analyze an ARM mode prologue starting at PROLOGUE_START and
1468    continuing no further than PROLOGUE_END.  If CACHE is non-NULL,
1469    fill it in.  Return the first address not recognized as a prologue
1470    instruction.
1471
1472    We recognize all the instructions typically found in ARM prologues,
1473    plus harmless instructions which can be skipped (either for analysis
1474    purposes, or a more restrictive set that can be skipped when finding
1475    the end of the prologue).  */
1476
1477 static CORE_ADDR
1478 arm_analyze_prologue (struct gdbarch *gdbarch,
1479                       CORE_ADDR prologue_start, CORE_ADDR prologue_end,
1480                       struct arm_prologue_cache *cache)
1481 {
1482   enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
1483   int regno;
1484   CORE_ADDR offset, current_pc;
1485   pv_t regs[ARM_FPS_REGNUM];
1486   CORE_ADDR unrecognized_pc = 0;
1487
1488   /* Search the prologue looking for instructions that set up the
1489      frame pointer, adjust the stack pointer, and save registers.
1490
1491      Be careful, however, and if it doesn't look like a prologue,
1492      don't try to scan it.  If, for instance, a frameless function
1493      begins with stmfd sp!, then we will tell ourselves there is
1494      a frame, which will confuse stack traceback, as well as "finish" 
1495      and other operations that rely on a knowledge of the stack
1496      traceback.  */
1497
1498   for (regno = 0; regno < ARM_FPS_REGNUM; regno++)
1499     regs[regno] = pv_register (regno, 0);
1500   pv_area stack (ARM_SP_REGNUM, gdbarch_addr_bit (gdbarch));
1501
1502   for (current_pc = prologue_start;
1503        current_pc < prologue_end;
1504        current_pc += 4)
1505     {
1506       unsigned int insn
1507         = read_code_unsigned_integer (current_pc, 4, byte_order_for_code);
1508
1509       if (insn == 0xe1a0c00d)           /* mov ip, sp */
1510         {
1511           regs[ARM_IP_REGNUM] = regs[ARM_SP_REGNUM];
1512           continue;
1513         }
1514       else if ((insn & 0xfff00000) == 0xe2800000        /* add Rd, Rn, #n */
1515                && pv_is_register (regs[bits (insn, 16, 19)], ARM_SP_REGNUM))
1516         {
1517           unsigned imm = insn & 0xff;                   /* immediate value */
1518           unsigned rot = (insn & 0xf00) >> 7;           /* rotate amount */
1519           int rd = bits (insn, 12, 15);
1520           imm = (imm >> rot) | (imm << (32 - rot));
1521           regs[rd] = pv_add_constant (regs[bits (insn, 16, 19)], imm);
1522           continue;
1523         }
1524       else if ((insn & 0xfff00000) == 0xe2400000        /* sub Rd, Rn, #n */
1525                && pv_is_register (regs[bits (insn, 16, 19)], ARM_SP_REGNUM))
1526         {
1527           unsigned imm = insn & 0xff;                   /* immediate value */
1528           unsigned rot = (insn & 0xf00) >> 7;           /* rotate amount */
1529           int rd = bits (insn, 12, 15);
1530           imm = (imm >> rot) | (imm << (32 - rot));
1531           regs[rd] = pv_add_constant (regs[bits (insn, 16, 19)], -imm);
1532           continue;
1533         }
1534       else if ((insn & 0xffff0fff) == 0xe52d0004)       /* str Rd,
1535                                                            [sp, #-4]! */
1536         {
1537           if (stack.store_would_trash (regs[ARM_SP_REGNUM]))
1538             break;
1539           regs[ARM_SP_REGNUM] = pv_add_constant (regs[ARM_SP_REGNUM], -4);
1540           stack.store (regs[ARM_SP_REGNUM], 4,
1541                        regs[bits (insn, 12, 15)]);
1542           continue;
1543         }
1544       else if ((insn & 0xffff0000) == 0xe92d0000)
1545         /* stmfd sp!, {..., fp, ip, lr, pc}
1546            or
1547            stmfd sp!, {a1, a2, a3, a4}  */
1548         {
1549           int mask = insn & 0xffff;
1550
1551           if (stack.store_would_trash (regs[ARM_SP_REGNUM]))
1552             break;
1553
1554           /* Calculate offsets of saved registers.  */
1555           for (regno = ARM_PC_REGNUM; regno >= 0; regno--)
1556             if (mask & (1 << regno))
1557               {
1558                 regs[ARM_SP_REGNUM]
1559                   = pv_add_constant (regs[ARM_SP_REGNUM], -4);
1560                 stack.store (regs[ARM_SP_REGNUM], 4, regs[regno]);
1561               }
1562         }
1563       else if ((insn & 0xffff0000) == 0xe54b0000        /* strb rx,[r11,#-n] */
1564                || (insn & 0xffff00f0) == 0xe14b00b0     /* strh rx,[r11,#-n] */
1565                || (insn & 0xffffc000) == 0xe50b0000)    /* str  rx,[r11,#-n] */
1566         {
1567           /* No need to add this to saved_regs -- it's just an arg reg.  */
1568           continue;
1569         }
1570       else if ((insn & 0xffff0000) == 0xe5cd0000        /* strb rx,[sp,#n] */
1571                || (insn & 0xffff00f0) == 0xe1cd00b0     /* strh rx,[sp,#n] */
1572                || (insn & 0xffffc000) == 0xe58d0000)    /* str  rx,[sp,#n] */
1573         {
1574           /* No need to add this to saved_regs -- it's just an arg reg.  */
1575           continue;
1576         }
1577       else if ((insn & 0xfff00000) == 0xe8800000        /* stm Rn,
1578                                                            { registers } */
1579                && pv_is_register (regs[bits (insn, 16, 19)], ARM_SP_REGNUM))
1580         {
1581           /* No need to add this to saved_regs -- it's just arg regs.  */
1582           continue;
1583         }
1584       else if ((insn & 0xfffff000) == 0xe24cb000)       /* sub fp, ip #n */
1585         {
1586           unsigned imm = insn & 0xff;                   /* immediate value */
1587           unsigned rot = (insn & 0xf00) >> 7;           /* rotate amount */
1588           imm = (imm >> rot) | (imm << (32 - rot));
1589           regs[ARM_FP_REGNUM] = pv_add_constant (regs[ARM_IP_REGNUM], -imm);
1590         }
1591       else if ((insn & 0xfffff000) == 0xe24dd000)       /* sub sp, sp #n */
1592         {
1593           unsigned imm = insn & 0xff;                   /* immediate value */
1594           unsigned rot = (insn & 0xf00) >> 7;           /* rotate amount */
1595           imm = (imm >> rot) | (imm << (32 - rot));
1596           regs[ARM_SP_REGNUM] = pv_add_constant (regs[ARM_SP_REGNUM], -imm);
1597         }
1598       else if ((insn & 0xffff7fff) == 0xed6d0103        /* stfe f?,
1599                                                            [sp, -#c]! */
1600                && gdbarch_tdep (gdbarch)->have_fpa_registers)
1601         {
1602           if (stack.store_would_trash (regs[ARM_SP_REGNUM]))
1603             break;
1604
1605           regs[ARM_SP_REGNUM] = pv_add_constant (regs[ARM_SP_REGNUM], -12);
1606           regno = ARM_F0_REGNUM + ((insn >> 12) & 0x07);
1607           stack.store (regs[ARM_SP_REGNUM], 12, regs[regno]);
1608         }
1609       else if ((insn & 0xffbf0fff) == 0xec2d0200        /* sfmfd f0, 4,
1610                                                            [sp!] */
1611                && gdbarch_tdep (gdbarch)->have_fpa_registers)
1612         {
1613           int n_saved_fp_regs;
1614           unsigned int fp_start_reg, fp_bound_reg;
1615
1616           if (stack.store_would_trash (regs[ARM_SP_REGNUM]))
1617             break;
1618
1619           if ((insn & 0x800) == 0x800)          /* N0 is set */
1620             {
1621               if ((insn & 0x40000) == 0x40000)  /* N1 is set */
1622                 n_saved_fp_regs = 3;
1623               else
1624                 n_saved_fp_regs = 1;
1625             }
1626           else
1627             {
1628               if ((insn & 0x40000) == 0x40000)  /* N1 is set */
1629                 n_saved_fp_regs = 2;
1630               else
1631                 n_saved_fp_regs = 4;
1632             }
1633
1634           fp_start_reg = ARM_F0_REGNUM + ((insn >> 12) & 0x7);
1635           fp_bound_reg = fp_start_reg + n_saved_fp_regs;
1636           for (; fp_start_reg < fp_bound_reg; fp_start_reg++)
1637             {
1638               regs[ARM_SP_REGNUM] = pv_add_constant (regs[ARM_SP_REGNUM], -12);
1639               stack.store (regs[ARM_SP_REGNUM], 12,
1640                            regs[fp_start_reg++]);
1641             }
1642         }
1643       else if ((insn & 0xff000000) == 0xeb000000 && cache == NULL) /* bl */
1644         {
1645           /* Allow some special function calls when skipping the
1646              prologue; GCC generates these before storing arguments to
1647              the stack.  */
1648           CORE_ADDR dest = BranchDest (current_pc, insn);
1649
1650           if (skip_prologue_function (gdbarch, dest, 0))
1651             continue;
1652           else
1653             break;
1654         }
1655       else if ((insn & 0xf0000000) != 0xe0000000)
1656         break;                  /* Condition not true, exit early.  */
1657       else if (arm_instruction_changes_pc (insn))
1658         /* Don't scan past anything that might change control flow.  */
1659         break;
1660       else if (arm_instruction_restores_sp (insn))
1661         {
1662           /* Don't scan past the epilogue.  */
1663           break;
1664         }
1665       else if ((insn & 0xfe500000) == 0xe8100000        /* ldm */
1666                && pv_is_register (regs[bits (insn, 16, 19)], ARM_SP_REGNUM))
1667         /* Ignore block loads from the stack, potentially copying
1668            parameters from memory.  */
1669         continue;
1670       else if ((insn & 0xfc500000) == 0xe4100000
1671                && pv_is_register (regs[bits (insn, 16, 19)], ARM_SP_REGNUM))
1672         /* Similarly ignore single loads from the stack.  */
1673         continue;
1674       else if ((insn & 0xffff0ff0) == 0xe1a00000)
1675         /* MOV Rd, Rm.  Skip register copies, i.e. saves to another
1676            register instead of the stack.  */
1677         continue;
1678       else
1679         {
1680           /* The optimizer might shove anything into the prologue, if
1681              we build up cache (cache != NULL) from scanning prologue,
1682              we just skip what we don't recognize and scan further to
1683              make cache as complete as possible.  However, if we skip
1684              prologue, we'll stop immediately on unrecognized
1685              instruction.  */
1686           unrecognized_pc = current_pc;
1687           if (cache != NULL)
1688             continue;
1689           else
1690             break;
1691         }
1692     }
1693
1694   if (unrecognized_pc == 0)
1695     unrecognized_pc = current_pc;
1696
1697   if (cache)
1698     {
1699       int framereg, framesize;
1700
1701       /* The frame size is just the distance from the frame register
1702          to the original stack pointer.  */
1703       if (pv_is_register (regs[ARM_FP_REGNUM], ARM_SP_REGNUM))
1704         {
1705           /* Frame pointer is fp.  */
1706           framereg = ARM_FP_REGNUM;
1707           framesize = -regs[ARM_FP_REGNUM].k;
1708         }
1709       else
1710         {
1711           /* Try the stack pointer... this is a bit desperate.  */
1712           framereg = ARM_SP_REGNUM;
1713           framesize = -regs[ARM_SP_REGNUM].k;
1714         }
1715
1716       cache->framereg = framereg;
1717       cache->framesize = framesize;
1718
1719       for (regno = 0; regno < ARM_FPS_REGNUM; regno++)
1720         if (stack.find_reg (gdbarch, regno, &offset))
1721           cache->saved_regs[regno].addr = offset;
1722     }
1723
1724   if (arm_debug)
1725     fprintf_unfiltered (gdb_stdlog, "Prologue scan stopped at %s\n",
1726                         paddress (gdbarch, unrecognized_pc));
1727
1728   return unrecognized_pc;
1729 }
1730
1731 static void
1732 arm_scan_prologue (struct frame_info *this_frame,
1733                    struct arm_prologue_cache *cache)
1734 {
1735   struct gdbarch *gdbarch = get_frame_arch (this_frame);
1736   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1737   CORE_ADDR prologue_start, prologue_end;
1738   CORE_ADDR prev_pc = get_frame_pc (this_frame);
1739   CORE_ADDR block_addr = get_frame_address_in_block (this_frame);
1740
1741   /* Assume there is no frame until proven otherwise.  */
1742   cache->framereg = ARM_SP_REGNUM;
1743   cache->framesize = 0;
1744
1745   /* Check for Thumb prologue.  */
1746   if (arm_frame_is_thumb (this_frame))
1747     {
1748       thumb_scan_prologue (gdbarch, prev_pc, block_addr, cache);
1749       return;
1750     }
1751
1752   /* Find the function prologue.  If we can't find the function in
1753      the symbol table, peek in the stack frame to find the PC.  */
1754   if (find_pc_partial_function (block_addr, NULL, &prologue_start,
1755                                 &prologue_end))
1756     {
1757       /* One way to find the end of the prologue (which works well
1758          for unoptimized code) is to do the following:
1759
1760             struct symtab_and_line sal = find_pc_line (prologue_start, 0);
1761
1762             if (sal.line == 0)
1763               prologue_end = prev_pc;
1764             else if (sal.end < prologue_end)
1765               prologue_end = sal.end;
1766
1767          This mechanism is very accurate so long as the optimizer
1768          doesn't move any instructions from the function body into the
1769          prologue.  If this happens, sal.end will be the last
1770          instruction in the first hunk of prologue code just before
1771          the first instruction that the scheduler has moved from
1772          the body to the prologue.
1773
1774          In order to make sure that we scan all of the prologue
1775          instructions, we use a slightly less accurate mechanism which
1776          may scan more than necessary.  To help compensate for this
1777          lack of accuracy, the prologue scanning loop below contains
1778          several clauses which'll cause the loop to terminate early if
1779          an implausible prologue instruction is encountered.
1780
1781          The expression
1782
1783               prologue_start + 64
1784
1785          is a suitable endpoint since it accounts for the largest
1786          possible prologue plus up to five instructions inserted by
1787          the scheduler.  */
1788
1789       if (prologue_end > prologue_start + 64)
1790         {
1791           prologue_end = prologue_start + 64;   /* See above.  */
1792         }
1793     }
1794   else
1795     {
1796       /* We have no symbol information.  Our only option is to assume this
1797          function has a standard stack frame and the normal frame register.
1798          Then, we can find the value of our frame pointer on entrance to
1799          the callee (or at the present moment if this is the innermost frame).
1800          The value stored there should be the address of the stmfd + 8.  */
1801       CORE_ADDR frame_loc;
1802       ULONGEST return_value;
1803
1804       /* AAPCS does not use a frame register, so we can abort here.  */
1805       if (gdbarch_tdep (gdbarch)->arm_abi == ARM_ABI_AAPCS)
1806         return;
1807
1808       frame_loc = get_frame_register_unsigned (this_frame, ARM_FP_REGNUM);
1809       if (!safe_read_memory_unsigned_integer (frame_loc, 4, byte_order,
1810                                               &return_value))
1811         return;
1812       else
1813         {
1814           prologue_start = gdbarch_addr_bits_remove
1815                              (gdbarch, return_value) - 8;
1816           prologue_end = prologue_start + 64;   /* See above.  */
1817         }
1818     }
1819
1820   if (prev_pc < prologue_end)
1821     prologue_end = prev_pc;
1822
1823   arm_analyze_prologue (gdbarch, prologue_start, prologue_end, cache);
1824 }
1825
1826 static struct arm_prologue_cache *
1827 arm_make_prologue_cache (struct frame_info *this_frame)
1828 {
1829   int reg;
1830   struct arm_prologue_cache *cache;
1831   CORE_ADDR unwound_fp;
1832
1833   cache = FRAME_OBSTACK_ZALLOC (struct arm_prologue_cache);
1834   cache->saved_regs = trad_frame_alloc_saved_regs (this_frame);
1835
1836   arm_scan_prologue (this_frame, cache);
1837
1838   unwound_fp = get_frame_register_unsigned (this_frame, cache->framereg);
1839   if (unwound_fp == 0)
1840     return cache;
1841
1842   cache->prev_sp = unwound_fp + cache->framesize;
1843
1844   /* Calculate actual addresses of saved registers using offsets
1845      determined by arm_scan_prologue.  */
1846   for (reg = 0; reg < gdbarch_num_regs (get_frame_arch (this_frame)); reg++)
1847     if (trad_frame_addr_p (cache->saved_regs, reg))
1848       cache->saved_regs[reg].addr += cache->prev_sp;
1849
1850   return cache;
1851 }
1852
1853 /* Implementation of the stop_reason hook for arm_prologue frames.  */
1854
1855 static enum unwind_stop_reason
1856 arm_prologue_unwind_stop_reason (struct frame_info *this_frame,
1857                                  void **this_cache)
1858 {
1859   struct arm_prologue_cache *cache;
1860   CORE_ADDR pc;
1861
1862   if (*this_cache == NULL)
1863     *this_cache = arm_make_prologue_cache (this_frame);
1864   cache = (struct arm_prologue_cache *) *this_cache;
1865
1866   /* This is meant to halt the backtrace at "_start".  */
1867   pc = get_frame_pc (this_frame);
1868   if (pc <= gdbarch_tdep (get_frame_arch (this_frame))->lowest_pc)
1869     return UNWIND_OUTERMOST;
1870
1871   /* If we've hit a wall, stop.  */
1872   if (cache->prev_sp == 0)
1873     return UNWIND_OUTERMOST;
1874
1875   return UNWIND_NO_REASON;
1876 }
1877
1878 /* Our frame ID for a normal frame is the current function's starting PC
1879    and the caller's SP when we were called.  */
1880
1881 static void
1882 arm_prologue_this_id (struct frame_info *this_frame,
1883                       void **this_cache,
1884                       struct frame_id *this_id)
1885 {
1886   struct arm_prologue_cache *cache;
1887   struct frame_id id;
1888   CORE_ADDR pc, func;
1889
1890   if (*this_cache == NULL)
1891     *this_cache = arm_make_prologue_cache (this_frame);
1892   cache = (struct arm_prologue_cache *) *this_cache;
1893
1894   /* Use function start address as part of the frame ID.  If we cannot
1895      identify the start address (due to missing symbol information),
1896      fall back to just using the current PC.  */
1897   pc = get_frame_pc (this_frame);
1898   func = get_frame_func (this_frame);
1899   if (!func)
1900     func = pc;
1901
1902   id = frame_id_build (cache->prev_sp, func);
1903   *this_id = id;
1904 }
1905
1906 static struct value *
1907 arm_prologue_prev_register (struct frame_info *this_frame,
1908                             void **this_cache,
1909                             int prev_regnum)
1910 {
1911   struct gdbarch *gdbarch = get_frame_arch (this_frame);
1912   struct arm_prologue_cache *cache;
1913
1914   if (*this_cache == NULL)
1915     *this_cache = arm_make_prologue_cache (this_frame);
1916   cache = (struct arm_prologue_cache *) *this_cache;
1917
1918   /* If we are asked to unwind the PC, then we need to return the LR
1919      instead.  The prologue may save PC, but it will point into this
1920      frame's prologue, not the next frame's resume location.  Also
1921      strip the saved T bit.  A valid LR may have the low bit set, but
1922      a valid PC never does.  */
1923   if (prev_regnum == ARM_PC_REGNUM)
1924     {
1925       CORE_ADDR lr;
1926
1927       lr = frame_unwind_register_unsigned (this_frame, ARM_LR_REGNUM);
1928       return frame_unwind_got_constant (this_frame, prev_regnum,
1929                                         arm_addr_bits_remove (gdbarch, lr));
1930     }
1931
1932   /* SP is generally not saved to the stack, but this frame is
1933      identified by the next frame's stack pointer at the time of the call.
1934      The value was already reconstructed into PREV_SP.  */
1935   if (prev_regnum == ARM_SP_REGNUM)
1936     return frame_unwind_got_constant (this_frame, prev_regnum, cache->prev_sp);
1937
1938   /* The CPSR may have been changed by the call instruction and by the
1939      called function.  The only bit we can reconstruct is the T bit,
1940      by checking the low bit of LR as of the call.  This is a reliable
1941      indicator of Thumb-ness except for some ARM v4T pre-interworking
1942      Thumb code, which could get away with a clear low bit as long as
1943      the called function did not use bx.  Guess that all other
1944      bits are unchanged; the condition flags are presumably lost,
1945      but the processor status is likely valid.  */
1946   if (prev_regnum == ARM_PS_REGNUM)
1947     {
1948       CORE_ADDR lr, cpsr;
1949       ULONGEST t_bit = arm_psr_thumb_bit (gdbarch);
1950
1951       cpsr = get_frame_register_unsigned (this_frame, prev_regnum);
1952       lr = frame_unwind_register_unsigned (this_frame, ARM_LR_REGNUM);
1953       if (IS_THUMB_ADDR (lr))
1954         cpsr |= t_bit;
1955       else
1956         cpsr &= ~t_bit;
1957       return frame_unwind_got_constant (this_frame, prev_regnum, cpsr);
1958     }
1959
1960   return trad_frame_get_prev_register (this_frame, cache->saved_regs,
1961                                        prev_regnum);
1962 }
1963
1964 struct frame_unwind arm_prologue_unwind = {
1965   NORMAL_FRAME,
1966   arm_prologue_unwind_stop_reason,
1967   arm_prologue_this_id,
1968   arm_prologue_prev_register,
1969   NULL,
1970   default_frame_sniffer
1971 };
1972
1973 /* Maintain a list of ARM exception table entries per objfile, similar to the
1974    list of mapping symbols.  We only cache entries for standard ARM-defined
1975    personality routines; the cache will contain only the frame unwinding
1976    instructions associated with the entry (not the descriptors).  */
1977
1978 static const struct objfile_data *arm_exidx_data_key;
1979
1980 struct arm_exidx_entry
1981 {
1982   bfd_vma addr;
1983   gdb_byte *entry;
1984 };
1985 typedef struct arm_exidx_entry arm_exidx_entry_s;
1986 DEF_VEC_O(arm_exidx_entry_s);
1987
1988 struct arm_exidx_data
1989 {
1990   VEC(arm_exidx_entry_s) **section_maps;
1991 };
1992
1993 static void
1994 arm_exidx_data_free (struct objfile *objfile, void *arg)
1995 {
1996   struct arm_exidx_data *data = (struct arm_exidx_data *) arg;
1997   unsigned int i;
1998
1999   for (i = 0; i < objfile->obfd->section_count; i++)
2000     VEC_free (arm_exidx_entry_s, data->section_maps[i]);
2001 }
2002
2003 static inline int
2004 arm_compare_exidx_entries (const struct arm_exidx_entry *lhs,
2005                            const struct arm_exidx_entry *rhs)
2006 {
2007   return lhs->addr < rhs->addr;
2008 }
2009
2010 static struct obj_section *
2011 arm_obj_section_from_vma (struct objfile *objfile, bfd_vma vma)
2012 {
2013   struct obj_section *osect;
2014
2015   ALL_OBJFILE_OSECTIONS (objfile, osect)
2016     if (bfd_get_section_flags (objfile->obfd,
2017                                osect->the_bfd_section) & SEC_ALLOC)
2018       {
2019         bfd_vma start, size;
2020         start = bfd_get_section_vma (objfile->obfd, osect->the_bfd_section);
2021         size = bfd_get_section_size (osect->the_bfd_section);
2022
2023         if (start <= vma && vma < start + size)
2024           return osect;
2025       }
2026
2027   return NULL;
2028 }
2029
2030 /* Parse contents of exception table and exception index sections
2031    of OBJFILE, and fill in the exception table entry cache.
2032
2033    For each entry that refers to a standard ARM-defined personality
2034    routine, extract the frame unwinding instructions (from either
2035    the index or the table section).  The unwinding instructions
2036    are normalized by:
2037     - extracting them from the rest of the table data
2038     - converting to host endianness
2039     - appending the implicit 0xb0 ("Finish") code
2040
2041    The extracted and normalized instructions are stored for later
2042    retrieval by the arm_find_exidx_entry routine.  */
2043  
2044 static void
2045 arm_exidx_new_objfile (struct objfile *objfile)
2046 {
2047   struct arm_exidx_data *data;
2048   asection *exidx, *extab;
2049   bfd_vma exidx_vma = 0, extab_vma = 0;
2050   LONGEST i;
2051
2052   /* If we've already touched this file, do nothing.  */
2053   if (!objfile || objfile_data (objfile, arm_exidx_data_key) != NULL)
2054     return;
2055
2056   /* Read contents of exception table and index.  */
2057   exidx = bfd_get_section_by_name (objfile->obfd, ELF_STRING_ARM_unwind);
2058   gdb::byte_vector exidx_data;
2059   if (exidx)
2060     {
2061       exidx_vma = bfd_section_vma (objfile->obfd, exidx);
2062       exidx_data.resize (bfd_get_section_size (exidx));
2063
2064       if (!bfd_get_section_contents (objfile->obfd, exidx,
2065                                      exidx_data.data (), 0,
2066                                      exidx_data.size ()))
2067         return;
2068     }
2069
2070   extab = bfd_get_section_by_name (objfile->obfd, ".ARM.extab");
2071   gdb::byte_vector extab_data;
2072   if (extab)
2073     {
2074       extab_vma = bfd_section_vma (objfile->obfd, extab);
2075       extab_data.resize (bfd_get_section_size (extab));
2076
2077       if (!bfd_get_section_contents (objfile->obfd, extab,
2078                                      extab_data.data (), 0,
2079                                      extab_data.size ()))
2080         return;
2081     }
2082
2083   /* Allocate exception table data structure.  */
2084   data = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct arm_exidx_data);
2085   set_objfile_data (objfile, arm_exidx_data_key, data);
2086   data->section_maps = OBSTACK_CALLOC (&objfile->objfile_obstack,
2087                                        objfile->obfd->section_count,
2088                                        VEC(arm_exidx_entry_s) *);
2089
2090   /* Fill in exception table.  */
2091   for (i = 0; i < exidx_data.size () / 8; i++)
2092     {
2093       struct arm_exidx_entry new_exidx_entry;
2094       bfd_vma idx = bfd_h_get_32 (objfile->obfd, exidx_data.data () + i * 8);
2095       bfd_vma val = bfd_h_get_32 (objfile->obfd,
2096                                   exidx_data.data () + i * 8 + 4);
2097       bfd_vma addr = 0, word = 0;
2098       int n_bytes = 0, n_words = 0;
2099       struct obj_section *sec;
2100       gdb_byte *entry = NULL;
2101
2102       /* Extract address of start of function.  */
2103       idx = ((idx & 0x7fffffff) ^ 0x40000000) - 0x40000000;
2104       idx += exidx_vma + i * 8;
2105
2106       /* Find section containing function and compute section offset.  */
2107       sec = arm_obj_section_from_vma (objfile, idx);
2108       if (sec == NULL)
2109         continue;
2110       idx -= bfd_get_section_vma (objfile->obfd, sec->the_bfd_section);
2111
2112       /* Determine address of exception table entry.  */
2113       if (val == 1)
2114         {
2115           /* EXIDX_CANTUNWIND -- no exception table entry present.  */
2116         }
2117       else if ((val & 0xff000000) == 0x80000000)
2118         {
2119           /* Exception table entry embedded in .ARM.exidx
2120              -- must be short form.  */
2121           word = val;
2122           n_bytes = 3;
2123         }
2124       else if (!(val & 0x80000000))
2125         {
2126           /* Exception table entry in .ARM.extab.  */
2127           addr = ((val & 0x7fffffff) ^ 0x40000000) - 0x40000000;
2128           addr += exidx_vma + i * 8 + 4;
2129
2130           if (addr >= extab_vma && addr + 4 <= extab_vma + extab_data.size ())
2131             {
2132               word = bfd_h_get_32 (objfile->obfd,
2133                                    extab_data.data () + addr - extab_vma);
2134               addr += 4;
2135
2136               if ((word & 0xff000000) == 0x80000000)
2137                 {
2138                   /* Short form.  */
2139                   n_bytes = 3;
2140                 }
2141               else if ((word & 0xff000000) == 0x81000000
2142                        || (word & 0xff000000) == 0x82000000)
2143                 {
2144                   /* Long form.  */
2145                   n_bytes = 2;
2146                   n_words = ((word >> 16) & 0xff);
2147                 }
2148               else if (!(word & 0x80000000))
2149                 {
2150                   bfd_vma pers;
2151                   struct obj_section *pers_sec;
2152                   int gnu_personality = 0;
2153
2154                   /* Custom personality routine.  */
2155                   pers = ((word & 0x7fffffff) ^ 0x40000000) - 0x40000000;
2156                   pers = UNMAKE_THUMB_ADDR (pers + addr - 4);
2157
2158                   /* Check whether we've got one of the variants of the
2159                      GNU personality routines.  */
2160                   pers_sec = arm_obj_section_from_vma (objfile, pers);
2161                   if (pers_sec)
2162                     {
2163                       static const char *personality[] = 
2164                         {
2165                           "__gcc_personality_v0",
2166                           "__gxx_personality_v0",
2167                           "__gcj_personality_v0",
2168                           "__gnu_objc_personality_v0",
2169                           NULL
2170                         };
2171
2172                       CORE_ADDR pc = pers + obj_section_offset (pers_sec);
2173                       int k;
2174
2175                       for (k = 0; personality[k]; k++)
2176                         if (lookup_minimal_symbol_by_pc_name
2177                               (pc, personality[k], objfile))
2178                           {
2179                             gnu_personality = 1;
2180                             break;
2181                           }
2182                     }
2183
2184                   /* If so, the next word contains a word count in the high
2185                      byte, followed by the same unwind instructions as the
2186                      pre-defined forms.  */
2187                   if (gnu_personality
2188                       && addr + 4 <= extab_vma + extab_data.size ())
2189                     {
2190                       word = bfd_h_get_32 (objfile->obfd,
2191                                            (extab_data.data ()
2192                                             + addr - extab_vma));
2193                       addr += 4;
2194                       n_bytes = 3;
2195                       n_words = ((word >> 24) & 0xff);
2196                     }
2197                 }
2198             }
2199         }
2200
2201       /* Sanity check address.  */
2202       if (n_words)
2203         if (addr < extab_vma
2204             || addr + 4 * n_words > extab_vma + extab_data.size ())
2205           n_words = n_bytes = 0;
2206
2207       /* The unwind instructions reside in WORD (only the N_BYTES least
2208          significant bytes are valid), followed by N_WORDS words in the
2209          extab section starting at ADDR.  */
2210       if (n_bytes || n_words)
2211         {
2212           gdb_byte *p = entry
2213             = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack,
2214                                           n_bytes + n_words * 4 + 1);
2215
2216           while (n_bytes--)
2217             *p++ = (gdb_byte) ((word >> (8 * n_bytes)) & 0xff);
2218
2219           while (n_words--)
2220             {
2221               word = bfd_h_get_32 (objfile->obfd,
2222                                    extab_data.data () + addr - extab_vma);
2223               addr += 4;
2224
2225               *p++ = (gdb_byte) ((word >> 24) & 0xff);
2226               *p++ = (gdb_byte) ((word >> 16) & 0xff);
2227               *p++ = (gdb_byte) ((word >> 8) & 0xff);
2228               *p++ = (gdb_byte) (word & 0xff);
2229             }
2230
2231           /* Implied "Finish" to terminate the list.  */
2232           *p++ = 0xb0;
2233         }
2234
2235       /* Push entry onto vector.  They are guaranteed to always
2236          appear in order of increasing addresses.  */
2237       new_exidx_entry.addr = idx;
2238       new_exidx_entry.entry = entry;
2239       VEC_safe_push (arm_exidx_entry_s,
2240                      data->section_maps[sec->the_bfd_section->index],
2241                      &new_exidx_entry);
2242     }
2243 }
2244
2245 /* Search for the exception table entry covering MEMADDR.  If one is found,
2246    return a pointer to its data.  Otherwise, return 0.  If START is non-NULL,
2247    set *START to the start of the region covered by this entry.  */
2248
2249 static gdb_byte *
2250 arm_find_exidx_entry (CORE_ADDR memaddr, CORE_ADDR *start)
2251 {
2252   struct obj_section *sec;
2253
2254   sec = find_pc_section (memaddr);
2255   if (sec != NULL)
2256     {
2257       struct arm_exidx_data *data;
2258       VEC(arm_exidx_entry_s) *map;
2259       struct arm_exidx_entry map_key = { memaddr - obj_section_addr (sec), 0 };
2260       unsigned int idx;
2261
2262       data = ((struct arm_exidx_data *)
2263               objfile_data (sec->objfile, arm_exidx_data_key));
2264       if (data != NULL)
2265         {
2266           map = data->section_maps[sec->the_bfd_section->index];
2267           if (!VEC_empty (arm_exidx_entry_s, map))
2268             {
2269               struct arm_exidx_entry *map_sym;
2270
2271               idx = VEC_lower_bound (arm_exidx_entry_s, map, &map_key,
2272                                      arm_compare_exidx_entries);
2273
2274               /* VEC_lower_bound finds the earliest ordered insertion
2275                  point.  If the following symbol starts at this exact
2276                  address, we use that; otherwise, the preceding
2277                  exception table entry covers this address.  */
2278               if (idx < VEC_length (arm_exidx_entry_s, map))
2279                 {
2280                   map_sym = VEC_index (arm_exidx_entry_s, map, idx);
2281                   if (map_sym->addr == map_key.addr)
2282                     {
2283                       if (start)
2284                         *start = map_sym->addr + obj_section_addr (sec);
2285                       return map_sym->entry;
2286                     }
2287                 }
2288
2289               if (idx > 0)
2290                 {
2291                   map_sym = VEC_index (arm_exidx_entry_s, map, idx - 1);
2292                   if (start)
2293                     *start = map_sym->addr + obj_section_addr (sec);
2294                   return map_sym->entry;
2295                 }
2296             }
2297         }
2298     }
2299
2300   return NULL;
2301 }
2302
2303 /* Given the current frame THIS_FRAME, and its associated frame unwinding
2304    instruction list from the ARM exception table entry ENTRY, allocate and
2305    return a prologue cache structure describing how to unwind this frame.
2306
2307    Return NULL if the unwinding instruction list contains a "spare",
2308    "reserved" or "refuse to unwind" instruction as defined in section
2309    "9.3 Frame unwinding instructions" of the "Exception Handling ABI
2310    for the ARM Architecture" document.  */
2311
2312 static struct arm_prologue_cache *
2313 arm_exidx_fill_cache (struct frame_info *this_frame, gdb_byte *entry)
2314 {
2315   CORE_ADDR vsp = 0;
2316   int vsp_valid = 0;
2317
2318   struct arm_prologue_cache *cache;
2319   cache = FRAME_OBSTACK_ZALLOC (struct arm_prologue_cache);
2320   cache->saved_regs = trad_frame_alloc_saved_regs (this_frame);
2321
2322   for (;;)
2323     {
2324       gdb_byte insn;
2325
2326       /* Whenever we reload SP, we actually have to retrieve its
2327          actual value in the current frame.  */
2328       if (!vsp_valid)
2329         {
2330           if (trad_frame_realreg_p (cache->saved_regs, ARM_SP_REGNUM))
2331             {
2332               int reg = cache->saved_regs[ARM_SP_REGNUM].realreg;
2333               vsp = get_frame_register_unsigned (this_frame, reg);
2334             }
2335           else
2336             {
2337               CORE_ADDR addr = cache->saved_regs[ARM_SP_REGNUM].addr;
2338               vsp = get_frame_memory_unsigned (this_frame, addr, 4);
2339             }
2340
2341           vsp_valid = 1;
2342         }
2343
2344       /* Decode next unwind instruction.  */
2345       insn = *entry++;
2346
2347       if ((insn & 0xc0) == 0)
2348         {
2349           int offset = insn & 0x3f;
2350           vsp += (offset << 2) + 4;
2351         }
2352       else if ((insn & 0xc0) == 0x40)
2353         {
2354           int offset = insn & 0x3f;
2355           vsp -= (offset << 2) + 4;
2356         }
2357       else if ((insn & 0xf0) == 0x80)
2358         {
2359           int mask = ((insn & 0xf) << 8) | *entry++;
2360           int i;
2361
2362           /* The special case of an all-zero mask identifies
2363              "Refuse to unwind".  We return NULL to fall back
2364              to the prologue analyzer.  */
2365           if (mask == 0)
2366             return NULL;
2367
2368           /* Pop registers r4..r15 under mask.  */
2369           for (i = 0; i < 12; i++)
2370             if (mask & (1 << i))
2371               {
2372                 cache->saved_regs[4 + i].addr = vsp;
2373                 vsp += 4;
2374               }
2375
2376           /* Special-case popping SP -- we need to reload vsp.  */
2377           if (mask & (1 << (ARM_SP_REGNUM - 4)))
2378             vsp_valid = 0;
2379         }
2380       else if ((insn & 0xf0) == 0x90)
2381         {
2382           int reg = insn & 0xf;
2383
2384           /* Reserved cases.  */
2385           if (reg == ARM_SP_REGNUM || reg == ARM_PC_REGNUM)
2386             return NULL;
2387
2388           /* Set SP from another register and mark VSP for reload.  */
2389           cache->saved_regs[ARM_SP_REGNUM] = cache->saved_regs[reg];
2390           vsp_valid = 0;
2391         }
2392       else if ((insn & 0xf0) == 0xa0)
2393         {
2394           int count = insn & 0x7;
2395           int pop_lr = (insn & 0x8) != 0;
2396           int i;
2397
2398           /* Pop r4..r[4+count].  */
2399           for (i = 0; i <= count; i++)
2400             {
2401               cache->saved_regs[4 + i].addr = vsp;
2402               vsp += 4;
2403             }
2404
2405           /* If indicated by flag, pop LR as well.  */
2406           if (pop_lr)
2407             {
2408               cache->saved_regs[ARM_LR_REGNUM].addr = vsp;
2409               vsp += 4;
2410             }
2411         }
2412       else if (insn == 0xb0)
2413         {
2414           /* We could only have updated PC by popping into it; if so, it
2415              will show up as address.  Otherwise, copy LR into PC.  */
2416           if (!trad_frame_addr_p (cache->saved_regs, ARM_PC_REGNUM))
2417             cache->saved_regs[ARM_PC_REGNUM]
2418               = cache->saved_regs[ARM_LR_REGNUM];
2419
2420           /* We're done.  */
2421           break;
2422         }
2423       else if (insn == 0xb1)
2424         {
2425           int mask = *entry++;
2426           int i;
2427
2428           /* All-zero mask and mask >= 16 is "spare".  */
2429           if (mask == 0 || mask >= 16)
2430             return NULL;
2431
2432           /* Pop r0..r3 under mask.  */
2433           for (i = 0; i < 4; i++)
2434             if (mask & (1 << i))
2435               {
2436                 cache->saved_regs[i].addr = vsp;
2437                 vsp += 4;
2438               }
2439         }
2440       else if (insn == 0xb2)
2441         {
2442           ULONGEST offset = 0;
2443           unsigned shift = 0;
2444
2445           do
2446             {
2447               offset |= (*entry & 0x7f) << shift;
2448               shift += 7;
2449             }
2450           while (*entry++ & 0x80);
2451
2452           vsp += 0x204 + (offset << 2);
2453         }
2454       else if (insn == 0xb3)
2455         {
2456           int start = *entry >> 4;
2457           int count = (*entry++) & 0xf;
2458           int i;
2459
2460           /* Only registers D0..D15 are valid here.  */
2461           if (start + count >= 16)
2462             return NULL;
2463
2464           /* Pop VFP double-precision registers D[start]..D[start+count].  */
2465           for (i = 0; i <= count; i++)
2466             {
2467               cache->saved_regs[ARM_D0_REGNUM + start + i].addr = vsp;
2468               vsp += 8;
2469             }
2470
2471           /* Add an extra 4 bytes for FSTMFDX-style stack.  */
2472           vsp += 4;
2473         }
2474       else if ((insn & 0xf8) == 0xb8)
2475         {
2476           int count = insn & 0x7;
2477           int i;
2478
2479           /* Pop VFP double-precision registers D[8]..D[8+count].  */
2480           for (i = 0; i <= count; i++)
2481             {
2482               cache->saved_regs[ARM_D0_REGNUM + 8 + i].addr = vsp;
2483               vsp += 8;
2484             }
2485
2486           /* Add an extra 4 bytes for FSTMFDX-style stack.  */
2487           vsp += 4;
2488         }
2489       else if (insn == 0xc6)
2490         {
2491           int start = *entry >> 4;
2492           int count = (*entry++) & 0xf;
2493           int i;
2494
2495           /* Only registers WR0..WR15 are valid.  */
2496           if (start + count >= 16)
2497             return NULL;
2498
2499           /* Pop iwmmx registers WR[start]..WR[start+count].  */
2500           for (i = 0; i <= count; i++)
2501             {
2502               cache->saved_regs[ARM_WR0_REGNUM + start + i].addr = vsp;
2503               vsp += 8;
2504             }
2505         }
2506       else if (insn == 0xc7)
2507         {
2508           int mask = *entry++;
2509           int i;
2510
2511           /* All-zero mask and mask >= 16 is "spare".  */
2512           if (mask == 0 || mask >= 16)
2513             return NULL;
2514
2515           /* Pop iwmmx general-purpose registers WCGR0..WCGR3 under mask.  */
2516           for (i = 0; i < 4; i++)
2517             if (mask & (1 << i))
2518               {
2519                 cache->saved_regs[ARM_WCGR0_REGNUM + i].addr = vsp;
2520                 vsp += 4;
2521               }
2522         }
2523       else if ((insn & 0xf8) == 0xc0)
2524         {
2525           int count = insn & 0x7;
2526           int i;
2527
2528           /* Pop iwmmx registers WR[10]..WR[10+count].  */
2529           for (i = 0; i <= count; i++)
2530             {
2531               cache->saved_regs[ARM_WR0_REGNUM + 10 + i].addr = vsp;
2532               vsp += 8;
2533             }
2534         }
2535       else if (insn == 0xc8)
2536         {
2537           int start = *entry >> 4;
2538           int count = (*entry++) & 0xf;
2539           int i;
2540
2541           /* Only registers D0..D31 are valid.  */
2542           if (start + count >= 16)
2543             return NULL;
2544
2545           /* Pop VFP double-precision registers
2546              D[16+start]..D[16+start+count].  */
2547           for (i = 0; i <= count; i++)
2548             {
2549               cache->saved_regs[ARM_D0_REGNUM + 16 + start + i].addr = vsp;
2550               vsp += 8;
2551             }
2552         }
2553       else if (insn == 0xc9)
2554         {
2555           int start = *entry >> 4;
2556           int count = (*entry++) & 0xf;
2557           int i;
2558
2559           /* Pop VFP double-precision registers D[start]..D[start+count].  */
2560           for (i = 0; i <= count; i++)
2561             {
2562               cache->saved_regs[ARM_D0_REGNUM + start + i].addr = vsp;
2563               vsp += 8;
2564             }
2565         }
2566       else if ((insn & 0xf8) == 0xd0)
2567         {
2568           int count = insn & 0x7;
2569           int i;
2570
2571           /* Pop VFP double-precision registers D[8]..D[8+count].  */
2572           for (i = 0; i <= count; i++)
2573             {
2574               cache->saved_regs[ARM_D0_REGNUM + 8 + i].addr = vsp;
2575               vsp += 8;
2576             }
2577         }
2578       else
2579         {
2580           /* Everything else is "spare".  */
2581           return NULL;
2582         }
2583     }
2584
2585   /* If we restore SP from a register, assume this was the frame register.
2586      Otherwise just fall back to SP as frame register.  */
2587   if (trad_frame_realreg_p (cache->saved_regs, ARM_SP_REGNUM))
2588     cache->framereg = cache->saved_regs[ARM_SP_REGNUM].realreg;
2589   else
2590     cache->framereg = ARM_SP_REGNUM;
2591
2592   /* Determine offset to previous frame.  */
2593   cache->framesize
2594     = vsp - get_frame_register_unsigned (this_frame, cache->framereg);
2595
2596   /* We already got the previous SP.  */
2597   cache->prev_sp = vsp;
2598
2599   return cache;
2600 }
2601
2602 /* Unwinding via ARM exception table entries.  Note that the sniffer
2603    already computes a filled-in prologue cache, which is then used
2604    with the same arm_prologue_this_id and arm_prologue_prev_register
2605    routines also used for prologue-parsing based unwinding.  */
2606
2607 static int
2608 arm_exidx_unwind_sniffer (const struct frame_unwind *self,
2609                           struct frame_info *this_frame,
2610                           void **this_prologue_cache)
2611 {
2612   struct gdbarch *gdbarch = get_frame_arch (this_frame);
2613   enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
2614   CORE_ADDR addr_in_block, exidx_region, func_start;
2615   struct arm_prologue_cache *cache;
2616   gdb_byte *entry;
2617
2618   /* See if we have an ARM exception table entry covering this address.  */
2619   addr_in_block = get_frame_address_in_block (this_frame);
2620   entry = arm_find_exidx_entry (addr_in_block, &exidx_region);
2621   if (!entry)
2622     return 0;
2623
2624   /* The ARM exception table does not describe unwind information
2625      for arbitrary PC values, but is guaranteed to be correct only
2626      at call sites.  We have to decide here whether we want to use
2627      ARM exception table information for this frame, or fall back
2628      to using prologue parsing.  (Note that if we have DWARF CFI,
2629      this sniffer isn't even called -- CFI is always preferred.)
2630
2631      Before we make this decision, however, we check whether we
2632      actually have *symbol* information for the current frame.
2633      If not, prologue parsing would not work anyway, so we might
2634      as well use the exception table and hope for the best.  */
2635   if (find_pc_partial_function (addr_in_block, NULL, &func_start, NULL))
2636     {
2637       int exc_valid = 0;
2638
2639       /* If the next frame is "normal", we are at a call site in this
2640          frame, so exception information is guaranteed to be valid.  */
2641       if (get_next_frame (this_frame)
2642           && get_frame_type (get_next_frame (this_frame)) == NORMAL_FRAME)
2643         exc_valid = 1;
2644
2645       /* We also assume exception information is valid if we're currently
2646          blocked in a system call.  The system library is supposed to
2647          ensure this, so that e.g. pthread cancellation works.  */
2648       if (arm_frame_is_thumb (this_frame))
2649         {
2650           ULONGEST insn;
2651
2652           if (safe_read_memory_unsigned_integer (get_frame_pc (this_frame) - 2,
2653                                                  2, byte_order_for_code, &insn)
2654               && (insn & 0xff00) == 0xdf00 /* svc */)
2655             exc_valid = 1;
2656         }
2657       else
2658         {
2659           ULONGEST insn;
2660
2661           if (safe_read_memory_unsigned_integer (get_frame_pc (this_frame) - 4,
2662                                                  4, byte_order_for_code, &insn)
2663               && (insn & 0x0f000000) == 0x0f000000 /* svc */)
2664             exc_valid = 1;
2665         }
2666         
2667       /* Bail out if we don't know that exception information is valid.  */
2668       if (!exc_valid)
2669         return 0;
2670
2671      /* The ARM exception index does not mark the *end* of the region
2672         covered by the entry, and some functions will not have any entry.
2673         To correctly recognize the end of the covered region, the linker
2674         should have inserted dummy records with a CANTUNWIND marker.
2675
2676         Unfortunately, current versions of GNU ld do not reliably do
2677         this, and thus we may have found an incorrect entry above.
2678         As a (temporary) sanity check, we only use the entry if it
2679         lies *within* the bounds of the function.  Note that this check
2680         might reject perfectly valid entries that just happen to cover
2681         multiple functions; therefore this check ought to be removed
2682         once the linker is fixed.  */
2683       if (func_start > exidx_region)
2684         return 0;
2685     }
2686
2687   /* Decode the list of unwinding instructions into a prologue cache.
2688      Note that this may fail due to e.g. a "refuse to unwind" code.  */
2689   cache = arm_exidx_fill_cache (this_frame, entry);
2690   if (!cache)
2691     return 0;
2692
2693   *this_prologue_cache = cache;
2694   return 1;
2695 }
2696
2697 struct frame_unwind arm_exidx_unwind = {
2698   NORMAL_FRAME,
2699   default_frame_unwind_stop_reason,
2700   arm_prologue_this_id,
2701   arm_prologue_prev_register,
2702   NULL,
2703   arm_exidx_unwind_sniffer
2704 };
2705
2706 static struct arm_prologue_cache *
2707 arm_make_epilogue_frame_cache (struct frame_info *this_frame)
2708 {
2709   struct arm_prologue_cache *cache;
2710   int reg;
2711
2712   cache = FRAME_OBSTACK_ZALLOC (struct arm_prologue_cache);
2713   cache->saved_regs = trad_frame_alloc_saved_regs (this_frame);
2714
2715   /* Still rely on the offset calculated from prologue.  */
2716   arm_scan_prologue (this_frame, cache);
2717
2718   /* Since we are in epilogue, the SP has been restored.  */
2719   cache->prev_sp = get_frame_register_unsigned (this_frame, ARM_SP_REGNUM);
2720
2721   /* Calculate actual addresses of saved registers using offsets
2722      determined by arm_scan_prologue.  */
2723   for (reg = 0; reg < gdbarch_num_regs (get_frame_arch (this_frame)); reg++)
2724     if (trad_frame_addr_p (cache->saved_regs, reg))
2725       cache->saved_regs[reg].addr += cache->prev_sp;
2726
2727   return cache;
2728 }
2729
2730 /* Implementation of function hook 'this_id' in
2731    'struct frame_uwnind' for epilogue unwinder.  */
2732
2733 static void
2734 arm_epilogue_frame_this_id (struct frame_info *this_frame,
2735                             void **this_cache,
2736                             struct frame_id *this_id)
2737 {
2738   struct arm_prologue_cache *cache;
2739   CORE_ADDR pc, func;
2740
2741   if (*this_cache == NULL)
2742     *this_cache = arm_make_epilogue_frame_cache (this_frame);
2743   cache = (struct arm_prologue_cache *) *this_cache;
2744
2745   /* Use function start address as part of the frame ID.  If we cannot
2746      identify the start address (due to missing symbol information),
2747      fall back to just using the current PC.  */
2748   pc = get_frame_pc (this_frame);
2749   func = get_frame_func (this_frame);
2750   if (func == 0)
2751     func = pc;
2752
2753   (*this_id) = frame_id_build (cache->prev_sp, pc);
2754 }
2755
2756 /* Implementation of function hook 'prev_register' in
2757    'struct frame_uwnind' for epilogue unwinder.  */
2758
2759 static struct value *
2760 arm_epilogue_frame_prev_register (struct frame_info *this_frame,
2761                                   void **this_cache, int regnum)
2762 {
2763   if (*this_cache == NULL)
2764     *this_cache = arm_make_epilogue_frame_cache (this_frame);
2765
2766   return arm_prologue_prev_register (this_frame, this_cache, regnum);
2767 }
2768
2769 static int arm_stack_frame_destroyed_p_1 (struct gdbarch *gdbarch,
2770                                           CORE_ADDR pc);
2771 static int thumb_stack_frame_destroyed_p (struct gdbarch *gdbarch,
2772                                           CORE_ADDR pc);
2773
2774 /* Implementation of function hook 'sniffer' in
2775    'struct frame_uwnind' for epilogue unwinder.  */
2776
2777 static int
2778 arm_epilogue_frame_sniffer (const struct frame_unwind *self,
2779                             struct frame_info *this_frame,
2780                             void **this_prologue_cache)
2781 {
2782   if (frame_relative_level (this_frame) == 0)
2783     {
2784       struct gdbarch *gdbarch = get_frame_arch (this_frame);
2785       CORE_ADDR pc = get_frame_pc (this_frame);
2786
2787       if (arm_frame_is_thumb (this_frame))
2788         return thumb_stack_frame_destroyed_p (gdbarch, pc);
2789       else
2790         return arm_stack_frame_destroyed_p_1 (gdbarch, pc);
2791     }
2792   else
2793     return 0;
2794 }
2795
2796 /* Frame unwinder from epilogue.  */
2797
2798 static const struct frame_unwind arm_epilogue_frame_unwind =
2799 {
2800   NORMAL_FRAME,
2801   default_frame_unwind_stop_reason,
2802   arm_epilogue_frame_this_id,
2803   arm_epilogue_frame_prev_register,
2804   NULL,
2805   arm_epilogue_frame_sniffer,
2806 };
2807
2808 /* Recognize GCC's trampoline for thumb call-indirect.  If we are in a
2809    trampoline, return the target PC.  Otherwise return 0.
2810
2811    void call0a (char c, short s, int i, long l) {}
2812
2813    int main (void)
2814    {
2815      (*pointer_to_call0a) (c, s, i, l);
2816    }
2817
2818    Instead of calling a stub library function  _call_via_xx (xx is
2819    the register name), GCC may inline the trampoline in the object
2820    file as below (register r2 has the address of call0a).
2821
2822    .global main
2823    .type main, %function
2824    ...
2825    bl .L1
2826    ...
2827    .size main, .-main
2828
2829    .L1:
2830    bx r2
2831
2832    The trampoline 'bx r2' doesn't belong to main.  */
2833
2834 static CORE_ADDR
2835 arm_skip_bx_reg (struct frame_info *frame, CORE_ADDR pc)
2836 {
2837   /* The heuristics of recognizing such trampoline is that FRAME is
2838      executing in Thumb mode and the instruction on PC is 'bx Rm'.  */
2839   if (arm_frame_is_thumb (frame))
2840     {
2841       gdb_byte buf[2];
2842
2843       if (target_read_memory (pc, buf, 2) == 0)
2844         {
2845           struct gdbarch *gdbarch = get_frame_arch (frame);
2846           enum bfd_endian byte_order_for_code
2847             = gdbarch_byte_order_for_code (gdbarch);
2848           uint16_t insn
2849             = extract_unsigned_integer (buf, 2, byte_order_for_code);
2850
2851           if ((insn & 0xff80) == 0x4700)  /* bx <Rm> */
2852             {
2853               CORE_ADDR dest
2854                 = get_frame_register_unsigned (frame, bits (insn, 3, 6));
2855
2856               /* Clear the LSB so that gdb core sets step-resume
2857                  breakpoint at the right address.  */
2858               return UNMAKE_THUMB_ADDR (dest);
2859             }
2860         }
2861     }
2862
2863   return 0;
2864 }
2865
2866 static struct arm_prologue_cache *
2867 arm_make_stub_cache (struct frame_info *this_frame)
2868 {
2869   struct arm_prologue_cache *cache;
2870
2871   cache = FRAME_OBSTACK_ZALLOC (struct arm_prologue_cache);
2872   cache->saved_regs = trad_frame_alloc_saved_regs (this_frame);
2873
2874   cache->prev_sp = get_frame_register_unsigned (this_frame, ARM_SP_REGNUM);
2875
2876   return cache;
2877 }
2878
2879 /* Our frame ID for a stub frame is the current SP and LR.  */
2880
2881 static void
2882 arm_stub_this_id (struct frame_info *this_frame,
2883                   void **this_cache,
2884                   struct frame_id *this_id)
2885 {
2886   struct arm_prologue_cache *cache;
2887
2888   if (*this_cache == NULL)
2889     *this_cache = arm_make_stub_cache (this_frame);
2890   cache = (struct arm_prologue_cache *) *this_cache;
2891
2892   *this_id = frame_id_build (cache->prev_sp, get_frame_pc (this_frame));
2893 }
2894
2895 static int
2896 arm_stub_unwind_sniffer (const struct frame_unwind *self,
2897                          struct frame_info *this_frame,
2898                          void **this_prologue_cache)
2899 {
2900   CORE_ADDR addr_in_block;
2901   gdb_byte dummy[4];
2902   CORE_ADDR pc, start_addr;
2903   const char *name;
2904
2905   addr_in_block = get_frame_address_in_block (this_frame);
2906   pc = get_frame_pc (this_frame);
2907   if (in_plt_section (addr_in_block)
2908       /* We also use the stub winder if the target memory is unreadable
2909          to avoid having the prologue unwinder trying to read it.  */
2910       || target_read_memory (pc, dummy, 4) != 0)
2911     return 1;
2912
2913   if (find_pc_partial_function (pc, &name, &start_addr, NULL) == 0
2914       && arm_skip_bx_reg (this_frame, pc) != 0)
2915     return 1;
2916
2917   return 0;
2918 }
2919
2920 struct frame_unwind arm_stub_unwind = {
2921   NORMAL_FRAME,
2922   default_frame_unwind_stop_reason,
2923   arm_stub_this_id,
2924   arm_prologue_prev_register,
2925   NULL,
2926   arm_stub_unwind_sniffer
2927 };
2928
2929 /* Put here the code to store, into CACHE->saved_regs, the addresses
2930    of the saved registers of frame described by THIS_FRAME.  CACHE is
2931    returned.  */
2932
2933 static struct arm_prologue_cache *
2934 arm_m_exception_cache (struct frame_info *this_frame)
2935 {
2936   struct gdbarch *gdbarch = get_frame_arch (this_frame);
2937   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
2938   struct arm_prologue_cache *cache;
2939   CORE_ADDR unwound_sp;
2940   LONGEST xpsr;
2941
2942   cache = FRAME_OBSTACK_ZALLOC (struct arm_prologue_cache);
2943   cache->saved_regs = trad_frame_alloc_saved_regs (this_frame);
2944
2945   unwound_sp = get_frame_register_unsigned (this_frame,
2946                                             ARM_SP_REGNUM);
2947
2948   /* The hardware saves eight 32-bit words, comprising xPSR,
2949      ReturnAddress, LR (R14), R12, R3, R2, R1, R0.  See details in
2950      "B1.5.6 Exception entry behavior" in
2951      "ARMv7-M Architecture Reference Manual".  */
2952   cache->saved_regs[0].addr = unwound_sp;
2953   cache->saved_regs[1].addr = unwound_sp + 4;
2954   cache->saved_regs[2].addr = unwound_sp + 8;
2955   cache->saved_regs[3].addr = unwound_sp + 12;
2956   cache->saved_regs[12].addr = unwound_sp + 16;
2957   cache->saved_regs[14].addr = unwound_sp + 20;
2958   cache->saved_regs[15].addr = unwound_sp + 24;
2959   cache->saved_regs[ARM_PS_REGNUM].addr = unwound_sp + 28;
2960
2961   /* If bit 9 of the saved xPSR is set, then there is a four-byte
2962      aligner between the top of the 32-byte stack frame and the
2963      previous context's stack pointer.  */
2964   cache->prev_sp = unwound_sp + 32;
2965   if (safe_read_memory_integer (unwound_sp + 28, 4, byte_order, &xpsr)
2966       && (xpsr & (1 << 9)) != 0)
2967     cache->prev_sp += 4;
2968
2969   return cache;
2970 }
2971
2972 /* Implementation of function hook 'this_id' in
2973    'struct frame_uwnind'.  */
2974
2975 static void
2976 arm_m_exception_this_id (struct frame_info *this_frame,
2977                          void **this_cache,
2978                          struct frame_id *this_id)
2979 {
2980   struct arm_prologue_cache *cache;
2981
2982   if (*this_cache == NULL)
2983     *this_cache = arm_m_exception_cache (this_frame);
2984   cache = (struct arm_prologue_cache *) *this_cache;
2985
2986   /* Our frame ID for a stub frame is the current SP and LR.  */
2987   *this_id = frame_id_build (cache->prev_sp,
2988                              get_frame_pc (this_frame));
2989 }
2990
2991 /* Implementation of function hook 'prev_register' in
2992    'struct frame_uwnind'.  */
2993
2994 static struct value *
2995 arm_m_exception_prev_register (struct frame_info *this_frame,
2996                                void **this_cache,
2997                                int prev_regnum)
2998 {
2999   struct arm_prologue_cache *cache;
3000
3001   if (*this_cache == NULL)
3002     *this_cache = arm_m_exception_cache (this_frame);
3003   cache = (struct arm_prologue_cache *) *this_cache;
3004
3005   /* The value was already reconstructed into PREV_SP.  */
3006   if (prev_regnum == ARM_SP_REGNUM)
3007     return frame_unwind_got_constant (this_frame, prev_regnum,
3008                                       cache->prev_sp);
3009
3010   return trad_frame_get_prev_register (this_frame, cache->saved_regs,
3011                                        prev_regnum);
3012 }
3013
3014 /* Implementation of function hook 'sniffer' in
3015    'struct frame_uwnind'.  */
3016
3017 static int
3018 arm_m_exception_unwind_sniffer (const struct frame_unwind *self,
3019                                 struct frame_info *this_frame,
3020                                 void **this_prologue_cache)
3021 {
3022   CORE_ADDR this_pc = get_frame_pc (this_frame);
3023
3024   /* No need to check is_m; this sniffer is only registered for
3025      M-profile architectures.  */
3026
3027   /* Check if exception frame returns to a magic PC value.  */
3028   return arm_m_addr_is_magic (this_pc);
3029 }
3030
3031 /* Frame unwinder for M-profile exceptions.  */
3032
3033 struct frame_unwind arm_m_exception_unwind =
3034 {
3035   SIGTRAMP_FRAME,
3036   default_frame_unwind_stop_reason,
3037   arm_m_exception_this_id,
3038   arm_m_exception_prev_register,
3039   NULL,
3040   arm_m_exception_unwind_sniffer
3041 };
3042
3043 static CORE_ADDR
3044 arm_normal_frame_base (struct frame_info *this_frame, void **this_cache)
3045 {
3046   struct arm_prologue_cache *cache;
3047
3048   if (*this_cache == NULL)
3049     *this_cache = arm_make_prologue_cache (this_frame);
3050   cache = (struct arm_prologue_cache *) *this_cache;
3051
3052   return cache->prev_sp - cache->framesize;
3053 }
3054
3055 struct frame_base arm_normal_base = {
3056   &arm_prologue_unwind,
3057   arm_normal_frame_base,
3058   arm_normal_frame_base,
3059   arm_normal_frame_base
3060 };
3061
3062 static struct value *
3063 arm_dwarf2_prev_register (struct frame_info *this_frame, void **this_cache,
3064                           int regnum)
3065 {
3066   struct gdbarch * gdbarch = get_frame_arch (this_frame);
3067   CORE_ADDR lr, cpsr;
3068   ULONGEST t_bit = arm_psr_thumb_bit (gdbarch);
3069
3070   switch (regnum)
3071     {
3072     case ARM_PC_REGNUM:
3073       /* The PC is normally copied from the return column, which
3074          describes saves of LR.  However, that version may have an
3075          extra bit set to indicate Thumb state.  The bit is not
3076          part of the PC.  */
3077       lr = frame_unwind_register_unsigned (this_frame, ARM_LR_REGNUM);
3078       return frame_unwind_got_constant (this_frame, regnum,
3079                                         arm_addr_bits_remove (gdbarch, lr));
3080
3081     case ARM_PS_REGNUM:
3082       /* Reconstruct the T bit; see arm_prologue_prev_register for details.  */
3083       cpsr = get_frame_register_unsigned (this_frame, regnum);
3084       lr = frame_unwind_register_unsigned (this_frame, ARM_LR_REGNUM);
3085       if (IS_THUMB_ADDR (lr))
3086         cpsr |= t_bit;
3087       else
3088         cpsr &= ~t_bit;
3089       return frame_unwind_got_constant (this_frame, regnum, cpsr);
3090
3091     default:
3092       internal_error (__FILE__, __LINE__,
3093                       _("Unexpected register %d"), regnum);
3094     }
3095 }
3096
3097 static void
3098 arm_dwarf2_frame_init_reg (struct gdbarch *gdbarch, int regnum,
3099                            struct dwarf2_frame_state_reg *reg,
3100                            struct frame_info *this_frame)
3101 {
3102   switch (regnum)
3103     {
3104     case ARM_PC_REGNUM:
3105     case ARM_PS_REGNUM:
3106       reg->how = DWARF2_FRAME_REG_FN;
3107       reg->loc.fn = arm_dwarf2_prev_register;
3108       break;
3109     case ARM_SP_REGNUM:
3110       reg->how = DWARF2_FRAME_REG_CFA;
3111       break;
3112     }
3113 }
3114
3115 /* Implement the stack_frame_destroyed_p gdbarch method.  */
3116
3117 static int
3118 thumb_stack_frame_destroyed_p (struct gdbarch *gdbarch, CORE_ADDR pc)
3119 {
3120   enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
3121   unsigned int insn, insn2;
3122   int found_return = 0, found_stack_adjust = 0;
3123   CORE_ADDR func_start, func_end;
3124   CORE_ADDR scan_pc;
3125   gdb_byte buf[4];
3126
3127   if (!find_pc_partial_function (pc, NULL, &func_start, &func_end))
3128     return 0;
3129
3130   /* The epilogue is a sequence of instructions along the following lines:
3131
3132     - add stack frame size to SP or FP
3133     - [if frame pointer used] restore SP from FP
3134     - restore registers from SP [may include PC]
3135     - a return-type instruction [if PC wasn't already restored]
3136
3137     In a first pass, we scan forward from the current PC and verify the
3138     instructions we find as compatible with this sequence, ending in a
3139     return instruction.
3140
3141     However, this is not sufficient to distinguish indirect function calls
3142     within a function from indirect tail calls in the epilogue in some cases.
3143     Therefore, if we didn't already find any SP-changing instruction during
3144     forward scan, we add a backward scanning heuristic to ensure we actually
3145     are in the epilogue.  */
3146
3147   scan_pc = pc;
3148   while (scan_pc < func_end && !found_return)
3149     {
3150       if (target_read_memory (scan_pc, buf, 2))
3151         break;
3152
3153       scan_pc += 2;
3154       insn = extract_unsigned_integer (buf, 2, byte_order_for_code);
3155
3156       if ((insn & 0xff80) == 0x4700)  /* bx <Rm> */
3157         found_return = 1;
3158       else if (insn == 0x46f7)  /* mov pc, lr */
3159         found_return = 1;
3160       else if (thumb_instruction_restores_sp (insn))
3161         {
3162           if ((insn & 0xff00) == 0xbd00)  /* pop <registers, PC> */
3163             found_return = 1;
3164         }
3165       else if (thumb_insn_size (insn) == 4)  /* 32-bit Thumb-2 instruction */
3166         {
3167           if (target_read_memory (scan_pc, buf, 2))
3168             break;
3169
3170           scan_pc += 2;
3171           insn2 = extract_unsigned_integer (buf, 2, byte_order_for_code);
3172
3173           if (insn == 0xe8bd)  /* ldm.w sp!, <registers> */
3174             {
3175               if (insn2 & 0x8000)  /* <registers> include PC.  */
3176                 found_return = 1;
3177             }
3178           else if (insn == 0xf85d  /* ldr.w <Rt>, [sp], #4 */
3179                    && (insn2 & 0x0fff) == 0x0b04)
3180             {
3181               if ((insn2 & 0xf000) == 0xf000) /* <Rt> is PC.  */
3182                 found_return = 1;
3183             }
3184           else if ((insn & 0xffbf) == 0xecbd  /* vldm sp!, <list> */
3185                    && (insn2 & 0x0e00) == 0x0a00)
3186             ;
3187           else
3188             break;
3189         }
3190       else
3191         break;
3192     }
3193
3194   if (!found_return)
3195     return 0;
3196
3197   /* Since any instruction in the epilogue sequence, with the possible
3198      exception of return itself, updates the stack pointer, we need to
3199      scan backwards for at most one instruction.  Try either a 16-bit or
3200      a 32-bit instruction.  This is just a heuristic, so we do not worry
3201      too much about false positives.  */
3202
3203   if (pc - 4 < func_start)
3204     return 0;
3205   if (target_read_memory (pc - 4, buf, 4))
3206     return 0;
3207
3208   insn = extract_unsigned_integer (buf, 2, byte_order_for_code);
3209   insn2 = extract_unsigned_integer (buf + 2, 2, byte_order_for_code);
3210
3211   if (thumb_instruction_restores_sp (insn2))
3212     found_stack_adjust = 1;
3213   else if (insn == 0xe8bd)  /* ldm.w sp!, <registers> */
3214     found_stack_adjust = 1;
3215   else if (insn == 0xf85d  /* ldr.w <Rt>, [sp], #4 */
3216            && (insn2 & 0x0fff) == 0x0b04)
3217     found_stack_adjust = 1;
3218   else if ((insn & 0xffbf) == 0xecbd  /* vldm sp!, <list> */
3219            && (insn2 & 0x0e00) == 0x0a00)
3220     found_stack_adjust = 1;
3221
3222   return found_stack_adjust;
3223 }
3224
3225 static int
3226 arm_stack_frame_destroyed_p_1 (struct gdbarch *gdbarch, CORE_ADDR pc)
3227 {
3228   enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
3229   unsigned int insn;
3230   int found_return;
3231   CORE_ADDR func_start, func_end;
3232
3233   if (!find_pc_partial_function (pc, NULL, &func_start, &func_end))
3234     return 0;
3235
3236   /* We are in the epilogue if the previous instruction was a stack
3237      adjustment and the next instruction is a possible return (bx, mov
3238      pc, or pop).  We could have to scan backwards to find the stack
3239      adjustment, or forwards to find the return, but this is a decent
3240      approximation.  First scan forwards.  */
3241
3242   found_return = 0;
3243   insn = read_memory_unsigned_integer (pc, 4, byte_order_for_code);
3244   if (bits (insn, 28, 31) != INST_NV)
3245     {
3246       if ((insn & 0x0ffffff0) == 0x012fff10)
3247         /* BX.  */
3248         found_return = 1;
3249       else if ((insn & 0x0ffffff0) == 0x01a0f000)
3250         /* MOV PC.  */
3251         found_return = 1;
3252       else if ((insn & 0x0fff0000) == 0x08bd0000
3253           && (insn & 0x0000c000) != 0)
3254         /* POP (LDMIA), including PC or LR.  */
3255         found_return = 1;
3256     }
3257
3258   if (!found_return)
3259     return 0;
3260
3261   /* Scan backwards.  This is just a heuristic, so do not worry about
3262      false positives from mode changes.  */
3263
3264   if (pc < func_start + 4)
3265     return 0;
3266
3267   insn = read_memory_unsigned_integer (pc - 4, 4, byte_order_for_code);
3268   if (arm_instruction_restores_sp (insn))
3269     return 1;
3270
3271   return 0;
3272 }
3273
3274 /* Implement the stack_frame_destroyed_p gdbarch method.  */
3275
3276 static int
3277 arm_stack_frame_destroyed_p (struct gdbarch *gdbarch, CORE_ADDR pc)
3278 {
3279   if (arm_pc_is_thumb (gdbarch, pc))
3280     return thumb_stack_frame_destroyed_p (gdbarch, pc);
3281   else
3282     return arm_stack_frame_destroyed_p_1 (gdbarch, pc);
3283 }
3284
3285 /* When arguments must be pushed onto the stack, they go on in reverse
3286    order.  The code below implements a FILO (stack) to do this.  */
3287
3288 struct stack_item
3289 {
3290   int len;
3291   struct stack_item *prev;
3292   gdb_byte *data;
3293 };
3294
3295 static struct stack_item *
3296 push_stack_item (struct stack_item *prev, const gdb_byte *contents, int len)
3297 {
3298   struct stack_item *si;
3299   si = XNEW (struct stack_item);
3300   si->data = (gdb_byte *) xmalloc (len);
3301   si->len = len;
3302   si->prev = prev;
3303   memcpy (si->data, contents, len);
3304   return si;
3305 }
3306
3307 static struct stack_item *
3308 pop_stack_item (struct stack_item *si)
3309 {
3310   struct stack_item *dead = si;
3311   si = si->prev;
3312   xfree (dead->data);
3313   xfree (dead);
3314   return si;
3315 }
3316
3317
3318 /* Return the alignment (in bytes) of the given type.  */
3319
3320 static int
3321 arm_type_align (struct type *t)
3322 {
3323   int n;
3324   int align;
3325   int falign;
3326
3327   t = check_typedef (t);
3328   switch (TYPE_CODE (t))
3329     {
3330     default:
3331       /* Should never happen.  */
3332       internal_error (__FILE__, __LINE__, _("unknown type alignment"));
3333       return 4;
3334
3335     case TYPE_CODE_PTR:
3336     case TYPE_CODE_ENUM:
3337     case TYPE_CODE_INT:
3338     case TYPE_CODE_FLT:
3339     case TYPE_CODE_SET:
3340     case TYPE_CODE_RANGE:
3341     case TYPE_CODE_REF:
3342     case TYPE_CODE_RVALUE_REF:
3343     case TYPE_CODE_CHAR:
3344     case TYPE_CODE_BOOL:
3345       return TYPE_LENGTH (t);
3346
3347     case TYPE_CODE_ARRAY:
3348       if (TYPE_VECTOR (t))
3349         {
3350           /* Use the natural alignment for vector types (the same for
3351              scalar type), but the maximum alignment is 64-bit.  */
3352           if (TYPE_LENGTH (t) > 8)
3353             return 8;
3354           else
3355             return TYPE_LENGTH (t);
3356         }
3357       else
3358         return arm_type_align (TYPE_TARGET_TYPE (t));
3359     case TYPE_CODE_COMPLEX:
3360       return arm_type_align (TYPE_TARGET_TYPE (t));
3361
3362     case TYPE_CODE_STRUCT:
3363     case TYPE_CODE_UNION:
3364       align = 1;
3365       for (n = 0; n < TYPE_NFIELDS (t); n++)
3366         {
3367           falign = arm_type_align (TYPE_FIELD_TYPE (t, n));
3368           if (falign > align)
3369             align = falign;
3370         }
3371       return align;
3372     }
3373 }
3374
3375 /* Possible base types for a candidate for passing and returning in
3376    VFP registers.  */
3377
3378 enum arm_vfp_cprc_base_type
3379 {
3380   VFP_CPRC_UNKNOWN,
3381   VFP_CPRC_SINGLE,
3382   VFP_CPRC_DOUBLE,
3383   VFP_CPRC_VEC64,
3384   VFP_CPRC_VEC128
3385 };
3386
3387 /* The length of one element of base type B.  */
3388
3389 static unsigned
3390 arm_vfp_cprc_unit_length (enum arm_vfp_cprc_base_type b)
3391 {
3392   switch (b)
3393     {
3394     case VFP_CPRC_SINGLE:
3395       return 4;
3396     case VFP_CPRC_DOUBLE:
3397       return 8;
3398     case VFP_CPRC_VEC64:
3399       return 8;
3400     case VFP_CPRC_VEC128:
3401       return 16;
3402     default:
3403       internal_error (__FILE__, __LINE__, _("Invalid VFP CPRC type: %d."),
3404                       (int) b);
3405     }
3406 }
3407
3408 /* The character ('s', 'd' or 'q') for the type of VFP register used
3409    for passing base type B.  */
3410
3411 static int
3412 arm_vfp_cprc_reg_char (enum arm_vfp_cprc_base_type b)
3413 {
3414   switch (b)
3415     {
3416     case VFP_CPRC_SINGLE:
3417       return 's';
3418     case VFP_CPRC_DOUBLE:
3419       return 'd';
3420     case VFP_CPRC_VEC64:
3421       return 'd';
3422     case VFP_CPRC_VEC128:
3423       return 'q';
3424     default:
3425       internal_error (__FILE__, __LINE__, _("Invalid VFP CPRC type: %d."),
3426                       (int) b);
3427     }
3428 }
3429
3430 /* Determine whether T may be part of a candidate for passing and
3431    returning in VFP registers, ignoring the limit on the total number
3432    of components.  If *BASE_TYPE is VFP_CPRC_UNKNOWN, set it to the
3433    classification of the first valid component found; if it is not
3434    VFP_CPRC_UNKNOWN, all components must have the same classification
3435    as *BASE_TYPE.  If it is found that T contains a type not permitted
3436    for passing and returning in VFP registers, a type differently
3437    classified from *BASE_TYPE, or two types differently classified
3438    from each other, return -1, otherwise return the total number of
3439    base-type elements found (possibly 0 in an empty structure or
3440    array).  Vector types are not currently supported, matching the
3441    generic AAPCS support.  */
3442
3443 static int
3444 arm_vfp_cprc_sub_candidate (struct type *t,
3445                             enum arm_vfp_cprc_base_type *base_type)
3446 {
3447   t = check_typedef (t);
3448   switch (TYPE_CODE (t))
3449     {
3450     case TYPE_CODE_FLT:
3451       switch (TYPE_LENGTH (t))
3452         {
3453         case 4:
3454           if (*base_type == VFP_CPRC_UNKNOWN)
3455             *base_type = VFP_CPRC_SINGLE;
3456           else if (*base_type != VFP_CPRC_SINGLE)
3457             return -1;
3458           return 1;
3459
3460         case 8:
3461           if (*base_type == VFP_CPRC_UNKNOWN)
3462             *base_type = VFP_CPRC_DOUBLE;
3463           else if (*base_type != VFP_CPRC_DOUBLE)
3464             return -1;
3465           return 1;
3466
3467         default:
3468           return -1;
3469         }
3470       break;
3471
3472     case TYPE_CODE_COMPLEX:
3473       /* Arguments of complex T where T is one of the types float or
3474          double get treated as if they are implemented as:
3475
3476          struct complexT
3477          {
3478            T real;
3479            T imag;
3480          };
3481
3482       */
3483       switch (TYPE_LENGTH (t))
3484         {
3485         case 8:
3486           if (*base_type == VFP_CPRC_UNKNOWN)
3487             *base_type = VFP_CPRC_SINGLE;
3488           else if (*base_type != VFP_CPRC_SINGLE)
3489             return -1;
3490           return 2;
3491
3492         case 16:
3493           if (*base_type == VFP_CPRC_UNKNOWN)
3494             *base_type = VFP_CPRC_DOUBLE;
3495           else if (*base_type != VFP_CPRC_DOUBLE)
3496             return -1;
3497           return 2;
3498
3499         default:
3500           return -1;
3501         }
3502       break;
3503
3504     case TYPE_CODE_ARRAY:
3505       {
3506         if (TYPE_VECTOR (t))
3507           {
3508             /* A 64-bit or 128-bit containerized vector type are VFP
3509                CPRCs.  */
3510             switch (TYPE_LENGTH (t))
3511               {
3512               case 8:
3513                 if (*base_type == VFP_CPRC_UNKNOWN)
3514                   *base_type = VFP_CPRC_VEC64;
3515                 return 1;
3516               case 16:
3517                 if (*base_type == VFP_CPRC_UNKNOWN)
3518                   *base_type = VFP_CPRC_VEC128;
3519                 return 1;
3520               default:
3521                 return -1;
3522               }
3523           }
3524         else
3525           {
3526             int count;
3527             unsigned unitlen;
3528
3529             count = arm_vfp_cprc_sub_candidate (TYPE_TARGET_TYPE (t),
3530                                                 base_type);
3531             if (count == -1)
3532               return -1;
3533             if (TYPE_LENGTH (t) == 0)
3534               {
3535                 gdb_assert (count == 0);
3536                 return 0;
3537               }
3538             else if (count == 0)
3539               return -1;
3540             unitlen = arm_vfp_cprc_unit_length (*base_type);
3541             gdb_assert ((TYPE_LENGTH (t) % unitlen) == 0);
3542             return TYPE_LENGTH (t) / unitlen;
3543           }
3544       }
3545       break;
3546
3547     case TYPE_CODE_STRUCT:
3548       {
3549         int count = 0;
3550         unsigned unitlen;
3551         int i;
3552         for (i = 0; i < TYPE_NFIELDS (t); i++)
3553           {
3554             int sub_count = 0;
3555
3556             if (!field_is_static (&TYPE_FIELD (t, i)))
3557               sub_count = arm_vfp_cprc_sub_candidate (TYPE_FIELD_TYPE (t, i),
3558                                                       base_type);
3559             if (sub_count == -1)
3560               return -1;
3561             count += sub_count;
3562           }
3563         if (TYPE_LENGTH (t) == 0)
3564           {
3565             gdb_assert (count == 0);
3566             return 0;
3567           }
3568         else if (count == 0)
3569           return -1;
3570         unitlen = arm_vfp_cprc_unit_length (*base_type);
3571         if (TYPE_LENGTH (t) != unitlen * count)
3572           return -1;
3573         return count;
3574       }
3575
3576     case TYPE_CODE_UNION:
3577       {
3578         int count = 0;
3579         unsigned unitlen;
3580         int i;
3581         for (i = 0; i < TYPE_NFIELDS (t); i++)
3582           {
3583             int sub_count = arm_vfp_cprc_sub_candidate (TYPE_FIELD_TYPE (t, i),
3584                                                         base_type);
3585             if (sub_count == -1)
3586               return -1;
3587             count = (count > sub_count ? count : sub_count);
3588           }
3589         if (TYPE_LENGTH (t) == 0)
3590           {
3591             gdb_assert (count == 0);
3592             return 0;
3593           }
3594         else if (count == 0)
3595           return -1;
3596         unitlen = arm_vfp_cprc_unit_length (*base_type);
3597         if (TYPE_LENGTH (t) != unitlen * count)
3598           return -1;
3599         return count;
3600       }
3601
3602     default:
3603       break;
3604     }
3605
3606   return -1;
3607 }
3608
3609 /* Determine whether T is a VFP co-processor register candidate (CPRC)
3610    if passed to or returned from a non-variadic function with the VFP
3611    ABI in effect.  Return 1 if it is, 0 otherwise.  If it is, set
3612    *BASE_TYPE to the base type for T and *COUNT to the number of
3613    elements of that base type before returning.  */
3614
3615 static int
3616 arm_vfp_call_candidate (struct type *t, enum arm_vfp_cprc_base_type *base_type,
3617                         int *count)
3618 {
3619   enum arm_vfp_cprc_base_type b = VFP_CPRC_UNKNOWN;
3620   int c = arm_vfp_cprc_sub_candidate (t, &b);
3621   if (c <= 0 || c > 4)
3622     return 0;
3623   *base_type = b;
3624   *count = c;
3625   return 1;
3626 }
3627
3628 /* Return 1 if the VFP ABI should be used for passing arguments to and
3629    returning values from a function of type FUNC_TYPE, 0
3630    otherwise.  */
3631
3632 static int
3633 arm_vfp_abi_for_function (struct gdbarch *gdbarch, struct type *func_type)
3634 {
3635   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
3636   /* Variadic functions always use the base ABI.  Assume that functions
3637      without debug info are not variadic.  */
3638   if (func_type && TYPE_VARARGS (check_typedef (func_type)))
3639     return 0;
3640   /* The VFP ABI is only supported as a variant of AAPCS.  */
3641   if (tdep->arm_abi != ARM_ABI_AAPCS)
3642     return 0;
3643   return gdbarch_tdep (gdbarch)->fp_model == ARM_FLOAT_VFP;
3644 }
3645
3646 /* We currently only support passing parameters in integer registers, which
3647    conforms with GCC's default model, and VFP argument passing following
3648    the VFP variant of AAPCS.  Several other variants exist and
3649    we should probably support some of them based on the selected ABI.  */
3650
3651 static CORE_ADDR
3652 arm_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
3653                      struct regcache *regcache, CORE_ADDR bp_addr, int nargs,
3654                      struct value **args, CORE_ADDR sp,
3655                      function_call_return_method return_method,
3656                      CORE_ADDR struct_addr)
3657 {
3658   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
3659   int argnum;
3660   int argreg;
3661   int nstack;
3662   struct stack_item *si = NULL;
3663   int use_vfp_abi;
3664   struct type *ftype;
3665   unsigned vfp_regs_free = (1 << 16) - 1;
3666
3667   /* Determine the type of this function and whether the VFP ABI
3668      applies.  */
3669   ftype = check_typedef (value_type (function));
3670   if (TYPE_CODE (ftype) == TYPE_CODE_PTR)
3671     ftype = check_typedef (TYPE_TARGET_TYPE (ftype));
3672   use_vfp_abi = arm_vfp_abi_for_function (gdbarch, ftype);
3673
3674   /* Set the return address.  For the ARM, the return breakpoint is
3675      always at BP_ADDR.  */
3676   if (arm_pc_is_thumb (gdbarch, bp_addr))
3677     bp_addr |= 1;
3678   regcache_cooked_write_unsigned (regcache, ARM_LR_REGNUM, bp_addr);
3679
3680   /* Walk through the list of args and determine how large a temporary
3681      stack is required.  Need to take care here as structs may be
3682      passed on the stack, and we have to push them.  */
3683   nstack = 0;
3684
3685   argreg = ARM_A1_REGNUM;
3686   nstack = 0;
3687
3688   /* The struct_return pointer occupies the first parameter
3689      passing register.  */
3690   if (return_method == return_method_struct)
3691     {
3692       if (arm_debug)
3693         fprintf_unfiltered (gdb_stdlog, "struct return in %s = %s\n",
3694                             gdbarch_register_name (gdbarch, argreg),
3695                             paddress (gdbarch, struct_addr));
3696       regcache_cooked_write_unsigned (regcache, argreg, struct_addr);
3697       argreg++;
3698     }
3699
3700   for (argnum = 0; argnum < nargs; argnum++)
3701     {
3702       int len;
3703       struct type *arg_type;
3704       struct type *target_type;
3705       enum type_code typecode;
3706       const bfd_byte *val;
3707       int align;
3708       enum arm_vfp_cprc_base_type vfp_base_type;
3709       int vfp_base_count;
3710       int may_use_core_reg = 1;
3711
3712       arg_type = check_typedef (value_type (args[argnum]));
3713       len = TYPE_LENGTH (arg_type);
3714       target_type = TYPE_TARGET_TYPE (arg_type);
3715       typecode = TYPE_CODE (arg_type);
3716       val = value_contents (args[argnum]);
3717
3718       align = arm_type_align (arg_type);
3719       /* Round alignment up to a whole number of words.  */
3720       align = (align + INT_REGISTER_SIZE - 1) & ~(INT_REGISTER_SIZE - 1);
3721       /* Different ABIs have different maximum alignments.  */
3722       if (gdbarch_tdep (gdbarch)->arm_abi == ARM_ABI_APCS)
3723         {
3724           /* The APCS ABI only requires word alignment.  */
3725           align = INT_REGISTER_SIZE;
3726         }
3727       else
3728         {
3729           /* The AAPCS requires at most doubleword alignment.  */
3730           if (align > INT_REGISTER_SIZE * 2)
3731             align = INT_REGISTER_SIZE * 2;
3732         }
3733
3734       if (use_vfp_abi
3735           && arm_vfp_call_candidate (arg_type, &vfp_base_type,
3736                                      &vfp_base_count))
3737         {
3738           int regno;
3739           int unit_length;
3740           int shift;
3741           unsigned mask;
3742
3743           /* Because this is a CPRC it cannot go in a core register or
3744              cause a core register to be skipped for alignment.
3745              Either it goes in VFP registers and the rest of this loop
3746              iteration is skipped for this argument, or it goes on the
3747              stack (and the stack alignment code is correct for this
3748              case).  */
3749           may_use_core_reg = 0;
3750
3751           unit_length = arm_vfp_cprc_unit_length (vfp_base_type);
3752           shift = unit_length / 4;
3753           mask = (1 << (shift * vfp_base_count)) - 1;
3754           for (regno = 0; regno < 16; regno += shift)
3755             if (((vfp_regs_free >> regno) & mask) == mask)
3756               break;
3757
3758           if (regno < 16)
3759             {
3760               int reg_char;
3761               int reg_scaled;
3762               int i;
3763
3764               vfp_regs_free &= ~(mask << regno);
3765               reg_scaled = regno / shift;
3766               reg_char = arm_vfp_cprc_reg_char (vfp_base_type);
3767               for (i = 0; i < vfp_base_count; i++)
3768                 {
3769                   char name_buf[4];
3770                   int regnum;
3771                   if (reg_char == 'q')
3772                     arm_neon_quad_write (gdbarch, regcache, reg_scaled + i,
3773                                          val + i * unit_length);
3774                   else
3775                     {
3776                       xsnprintf (name_buf, sizeof (name_buf), "%c%d",
3777                                  reg_char, reg_scaled + i);
3778                       regnum = user_reg_map_name_to_regnum (gdbarch, name_buf,
3779                                                             strlen (name_buf));
3780                       regcache->cooked_write (regnum, val + i * unit_length);
3781                     }
3782                 }
3783               continue;
3784             }
3785           else
3786             {
3787               /* This CPRC could not go in VFP registers, so all VFP
3788                  registers are now marked as used.  */
3789               vfp_regs_free = 0;
3790             }
3791         }
3792
3793       /* Push stack padding for dowubleword alignment.  */
3794       if (nstack & (align - 1))
3795         {
3796           si = push_stack_item (si, val, INT_REGISTER_SIZE);
3797           nstack += INT_REGISTER_SIZE;
3798         }
3799       
3800       /* Doubleword aligned quantities must go in even register pairs.  */
3801       if (may_use_core_reg
3802           && argreg <= ARM_LAST_ARG_REGNUM
3803           && align > INT_REGISTER_SIZE
3804           && argreg & 1)
3805         argreg++;
3806
3807       /* If the argument is a pointer to a function, and it is a
3808          Thumb function, create a LOCAL copy of the value and set
3809          the THUMB bit in it.  */
3810       if (TYPE_CODE_PTR == typecode
3811           && target_type != NULL
3812           && TYPE_CODE_FUNC == TYPE_CODE (check_typedef (target_type)))
3813         {
3814           CORE_ADDR regval = extract_unsigned_integer (val, len, byte_order);
3815           if (arm_pc_is_thumb (gdbarch, regval))
3816             {
3817               bfd_byte *copy = (bfd_byte *) alloca (len);
3818               store_unsigned_integer (copy, len, byte_order,
3819                                       MAKE_THUMB_ADDR (regval));
3820               val = copy;
3821             }
3822         }
3823
3824       /* Copy the argument to general registers or the stack in
3825          register-sized pieces.  Large arguments are split between
3826          registers and stack.  */
3827       while (len > 0)
3828         {
3829           int partial_len = len < INT_REGISTER_SIZE ? len : INT_REGISTER_SIZE;
3830           CORE_ADDR regval
3831             = extract_unsigned_integer (val, partial_len, byte_order);
3832
3833           if (may_use_core_reg && argreg <= ARM_LAST_ARG_REGNUM)
3834             {
3835               /* The argument is being passed in a general purpose
3836                  register.  */
3837               if (byte_order == BFD_ENDIAN_BIG)
3838                 regval <<= (INT_REGISTER_SIZE - partial_len) * 8;
3839               if (arm_debug)
3840                 fprintf_unfiltered (gdb_stdlog, "arg %d in %s = 0x%s\n",
3841                                     argnum,
3842                                     gdbarch_register_name
3843                                       (gdbarch, argreg),
3844                                     phex (regval, INT_REGISTER_SIZE));
3845               regcache_cooked_write_unsigned (regcache, argreg, regval);
3846               argreg++;
3847             }
3848           else
3849             {
3850               gdb_byte buf[INT_REGISTER_SIZE];
3851
3852               memset (buf, 0, sizeof (buf));
3853               store_unsigned_integer (buf, partial_len, byte_order, regval);
3854
3855               /* Push the arguments onto the stack.  */
3856               if (arm_debug)
3857                 fprintf_unfiltered (gdb_stdlog, "arg %d @ sp + %d\n",
3858                                     argnum, nstack);
3859               si = push_stack_item (si, buf, INT_REGISTER_SIZE);
3860               nstack += INT_REGISTER_SIZE;
3861             }
3862               
3863           len -= partial_len;
3864           val += partial_len;
3865         }
3866     }
3867   /* If we have an odd number of words to push, then decrement the stack
3868      by one word now, so first stack argument will be dword aligned.  */
3869   if (nstack & 4)
3870     sp -= 4;
3871
3872   while (si)
3873     {
3874       sp -= si->len;
3875       write_memory (sp, si->data, si->len);
3876       si = pop_stack_item (si);
3877     }
3878
3879   /* Finally, update teh SP register.  */
3880   regcache_cooked_write_unsigned (regcache, ARM_SP_REGNUM, sp);
3881
3882   return sp;
3883 }
3884
3885
3886 /* Always align the frame to an 8-byte boundary.  This is required on
3887    some platforms and harmless on the rest.  */
3888
3889 static CORE_ADDR
3890 arm_frame_align (struct gdbarch *gdbarch, CORE_ADDR sp)
3891 {
3892   /* Align the stack to eight bytes.  */
3893   return sp & ~ (CORE_ADDR) 7;
3894 }
3895
3896 static void
3897 print_fpu_flags (struct ui_file *file, int flags)
3898 {
3899   if (flags & (1 << 0))
3900     fputs_filtered ("IVO ", file);
3901   if (flags & (1 << 1))
3902     fputs_filtered ("DVZ ", file);
3903   if (flags & (1 << 2))
3904     fputs_filtered ("OFL ", file);
3905   if (flags & (1 << 3))
3906     fputs_filtered ("UFL ", file);
3907   if (flags & (1 << 4))
3908     fputs_filtered ("INX ", file);
3909   fputc_filtered ('\n', file);
3910 }
3911
3912 /* Print interesting information about the floating point processor
3913    (if present) or emulator.  */
3914 static void
3915 arm_print_float_info (struct gdbarch *gdbarch, struct ui_file *file,
3916                       struct frame_info *frame, const char *args)
3917 {
3918   unsigned long status = get_frame_register_unsigned (frame, ARM_FPS_REGNUM);
3919   int type;
3920
3921   type = (status >> 24) & 127;
3922   if (status & (1 << 31))
3923     fprintf_filtered (file, _("Hardware FPU type %d\n"), type);
3924   else
3925     fprintf_filtered (file, _("Software FPU type %d\n"), type);
3926   /* i18n: [floating point unit] mask */
3927   fputs_filtered (_("mask: "), file);
3928   print_fpu_flags (file, status >> 16);
3929   /* i18n: [floating point unit] flags */
3930   fputs_filtered (_("flags: "), file);
3931   print_fpu_flags (file, status);
3932 }
3933
3934 /* Construct the ARM extended floating point type.  */
3935 static struct type *
3936 arm_ext_type (struct gdbarch *gdbarch)
3937 {
3938   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
3939
3940   if (!tdep->arm_ext_type)
3941     tdep->arm_ext_type
3942       = arch_float_type (gdbarch, -1, "builtin_type_arm_ext",
3943                          floatformats_arm_ext);
3944
3945   return tdep->arm_ext_type;
3946 }
3947
3948 static struct type *
3949 arm_neon_double_type (struct gdbarch *gdbarch)
3950 {
3951   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
3952
3953   if (tdep->neon_double_type == NULL)
3954     {
3955       struct type *t, *elem;
3956
3957       t = arch_composite_type (gdbarch, "__gdb_builtin_type_neon_d",
3958                                TYPE_CODE_UNION);
3959       elem = builtin_type (gdbarch)->builtin_uint8;
3960       append_composite_type_field (t, "u8", init_vector_type (elem, 8));
3961       elem = builtin_type (gdbarch)->builtin_uint16;
3962       append_composite_type_field (t, "u16", init_vector_type (elem, 4));
3963       elem = builtin_type (gdbarch)->builtin_uint32;
3964       append_composite_type_field (t, "u32", init_vector_type (elem, 2));
3965       elem = builtin_type (gdbarch)->builtin_uint64;
3966       append_composite_type_field (t, "u64", elem);
3967       elem = builtin_type (gdbarch)->builtin_float;
3968       append_composite_type_field (t, "f32", init_vector_type (elem, 2));
3969       elem = builtin_type (gdbarch)->builtin_double;
3970       append_composite_type_field (t, "f64", elem);
3971
3972       TYPE_VECTOR (t) = 1;
3973       TYPE_NAME (t) = "neon_d";
3974       tdep->neon_double_type = t;
3975     }
3976
3977   return tdep->neon_double_type;
3978 }
3979
3980 /* FIXME: The vector types are not correctly ordered on big-endian
3981    targets.  Just as s0 is the low bits of d0, d0[0] is also the low
3982    bits of d0 - regardless of what unit size is being held in d0.  So
3983    the offset of the first uint8 in d0 is 7, but the offset of the
3984    first float is 4.  This code works as-is for little-endian
3985    targets.  */
3986
3987 static struct type *
3988 arm_neon_quad_type (struct gdbarch *gdbarch)
3989 {
3990   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
3991
3992   if (tdep->neon_quad_type == NULL)
3993     {
3994       struct type *t, *elem;
3995
3996       t = arch_composite_type (gdbarch, "__gdb_builtin_type_neon_q",
3997                                TYPE_CODE_UNION);
3998       elem = builtin_type (gdbarch)->builtin_uint8;
3999       append_composite_type_field (t, "u8", init_vector_type (elem, 16));
4000       elem = builtin_type (gdbarch)->builtin_uint16;
4001       append_composite_type_field (t, "u16", init_vector_type (elem, 8));
4002       elem = builtin_type (gdbarch)->builtin_uint32;
4003       append_composite_type_field (t, "u32", init_vector_type (elem, 4));
4004       elem = builtin_type (gdbarch)->builtin_uint64;
4005       append_composite_type_field (t, "u64", init_vector_type (elem, 2));
4006       elem = builtin_type (gdbarch)->builtin_float;
4007       append_composite_type_field (t, "f32", init_vector_type (elem, 4));
4008       elem = builtin_type (gdbarch)->builtin_double;
4009       append_composite_type_field (t, "f64", init_vector_type (elem, 2));
4010
4011       TYPE_VECTOR (t) = 1;
4012       TYPE_NAME (t) = "neon_q";
4013       tdep->neon_quad_type = t;
4014     }
4015
4016   return tdep->neon_quad_type;
4017 }
4018
4019 /* Return the GDB type object for the "standard" data type of data in
4020    register N.  */
4021
4022 static struct type *
4023 arm_register_type (struct gdbarch *gdbarch, int regnum)
4024 {
4025   int num_regs = gdbarch_num_regs (gdbarch);
4026
4027   if (gdbarch_tdep (gdbarch)->have_vfp_pseudos
4028       && regnum >= num_regs && regnum < num_regs + 32)
4029     return builtin_type (gdbarch)->builtin_float;
4030
4031   if (gdbarch_tdep (gdbarch)->have_neon_pseudos
4032       && regnum >= num_regs + 32 && regnum < num_regs + 32 + 16)
4033     return arm_neon_quad_type (gdbarch);
4034
4035   /* If the target description has register information, we are only
4036      in this function so that we can override the types of
4037      double-precision registers for NEON.  */
4038   if (tdesc_has_registers (gdbarch_target_desc (gdbarch)))
4039     {
4040       struct type *t = tdesc_register_type (gdbarch, regnum);
4041
4042       if (regnum >= ARM_D0_REGNUM && regnum < ARM_D0_REGNUM + 32
4043           && TYPE_CODE (t) == TYPE_CODE_FLT
4044           && gdbarch_tdep (gdbarch)->have_neon)
4045         return arm_neon_double_type (gdbarch);
4046       else
4047         return t;
4048     }
4049
4050   if (regnum >= ARM_F0_REGNUM && regnum < ARM_F0_REGNUM + NUM_FREGS)
4051     {
4052       if (!gdbarch_tdep (gdbarch)->have_fpa_registers)
4053         return builtin_type (gdbarch)->builtin_void;
4054
4055       return arm_ext_type (gdbarch);
4056     }
4057   else if (regnum == ARM_SP_REGNUM)
4058     return builtin_type (gdbarch)->builtin_data_ptr;
4059   else if (regnum == ARM_PC_REGNUM)
4060     return builtin_type (gdbarch)->builtin_func_ptr;
4061   else if (regnum >= ARRAY_SIZE (arm_register_names))
4062     /* These registers are only supported on targets which supply
4063        an XML description.  */
4064     return builtin_type (gdbarch)->builtin_int0;
4065   else
4066     return builtin_type (gdbarch)->builtin_uint32;
4067 }
4068
4069 /* Map a DWARF register REGNUM onto the appropriate GDB register
4070    number.  */
4071
4072 static int
4073 arm_dwarf_reg_to_regnum (struct gdbarch *gdbarch, int reg)
4074 {
4075   /* Core integer regs.  */
4076   if (reg >= 0 && reg <= 15)
4077     return reg;
4078
4079   /* Legacy FPA encoding.  These were once used in a way which
4080      overlapped with VFP register numbering, so their use is
4081      discouraged, but GDB doesn't support the ARM toolchain
4082      which used them for VFP.  */
4083   if (reg >= 16 && reg <= 23)
4084     return ARM_F0_REGNUM + reg - 16;
4085
4086   /* New assignments for the FPA registers.  */
4087   if (reg >= 96 && reg <= 103)
4088     return ARM_F0_REGNUM + reg - 96;
4089
4090   /* WMMX register assignments.  */
4091   if (reg >= 104 && reg <= 111)
4092     return ARM_WCGR0_REGNUM + reg - 104;
4093
4094   if (reg >= 112 && reg <= 127)
4095     return ARM_WR0_REGNUM + reg - 112;
4096
4097   if (reg >= 192 && reg <= 199)
4098     return ARM_WC0_REGNUM + reg - 192;
4099
4100   /* VFP v2 registers.  A double precision value is actually
4101      in d1 rather than s2, but the ABI only defines numbering
4102      for the single precision registers.  This will "just work"
4103      in GDB for little endian targets (we'll read eight bytes,
4104      starting in s0 and then progressing to s1), but will be
4105      reversed on big endian targets with VFP.  This won't
4106      be a problem for the new Neon quad registers; you're supposed
4107      to use DW_OP_piece for those.  */
4108   if (reg >= 64 && reg <= 95)
4109     {
4110       char name_buf[4];
4111
4112       xsnprintf (name_buf, sizeof (name_buf), "s%d", reg - 64);
4113       return user_reg_map_name_to_regnum (gdbarch, name_buf,
4114                                           strlen (name_buf));
4115     }
4116
4117   /* VFP v3 / Neon registers.  This range is also used for VFP v2
4118      registers, except that it now describes d0 instead of s0.  */
4119   if (reg >= 256 && reg <= 287)
4120     {
4121       char name_buf[4];
4122
4123       xsnprintf (name_buf, sizeof (name_buf), "d%d", reg - 256);
4124       return user_reg_map_name_to_regnum (gdbarch, name_buf,
4125                                           strlen (name_buf));
4126     }
4127
4128   return -1;
4129 }
4130
4131 /* Map GDB internal REGNUM onto the Arm simulator register numbers.  */
4132 static int
4133 arm_register_sim_regno (struct gdbarch *gdbarch, int regnum)
4134 {
4135   int reg = regnum;
4136   gdb_assert (reg >= 0 && reg < gdbarch_num_regs (gdbarch));
4137
4138   if (regnum >= ARM_WR0_REGNUM && regnum <= ARM_WR15_REGNUM)
4139     return regnum - ARM_WR0_REGNUM + SIM_ARM_IWMMXT_COP0R0_REGNUM;
4140
4141   if (regnum >= ARM_WC0_REGNUM && regnum <= ARM_WC7_REGNUM)
4142     return regnum - ARM_WC0_REGNUM + SIM_ARM_IWMMXT_COP1R0_REGNUM;
4143
4144   if (regnum >= ARM_WCGR0_REGNUM && regnum <= ARM_WCGR7_REGNUM)
4145     return regnum - ARM_WCGR0_REGNUM + SIM_ARM_IWMMXT_COP1R8_REGNUM;
4146
4147   if (reg < NUM_GREGS)
4148     return SIM_ARM_R0_REGNUM + reg;
4149   reg -= NUM_GREGS;
4150
4151   if (reg < NUM_FREGS)
4152     return SIM_ARM_FP0_REGNUM + reg;
4153   reg -= NUM_FREGS;
4154
4155   if (reg < NUM_SREGS)
4156     return SIM_ARM_FPS_REGNUM + reg;
4157   reg -= NUM_SREGS;
4158
4159   internal_error (__FILE__, __LINE__, _("Bad REGNUM %d"), regnum);
4160 }
4161
4162 /* Given BUF, which is OLD_LEN bytes ending at ENDADDR, expand
4163    the buffer to be NEW_LEN bytes ending at ENDADDR.  Return
4164    NULL if an error occurs.  BUF is freed.  */
4165
4166 static gdb_byte *
4167 extend_buffer_earlier (gdb_byte *buf, CORE_ADDR endaddr,
4168                        int old_len, int new_len)
4169 {
4170   gdb_byte *new_buf;
4171   int bytes_to_read = new_len - old_len;
4172
4173   new_buf = (gdb_byte *) xmalloc (new_len);
4174   memcpy (new_buf + bytes_to_read, buf, old_len);
4175   xfree (buf);
4176   if (target_read_code (endaddr - new_len, new_buf, bytes_to_read) != 0)
4177     {
4178       xfree (new_buf);
4179       return NULL;
4180     }
4181   return new_buf;
4182 }
4183
4184 /* An IT block is at most the 2-byte IT instruction followed by
4185    four 4-byte instructions.  The furthest back we must search to
4186    find an IT block that affects the current instruction is thus
4187    2 + 3 * 4 == 14 bytes.  */
4188 #define MAX_IT_BLOCK_PREFIX 14
4189
4190 /* Use a quick scan if there are more than this many bytes of
4191    code.  */
4192 #define IT_SCAN_THRESHOLD 32
4193
4194 /* Adjust a breakpoint's address to move breakpoints out of IT blocks.
4195    A breakpoint in an IT block may not be hit, depending on the
4196    condition flags.  */
4197 static CORE_ADDR
4198 arm_adjust_breakpoint_address (struct gdbarch *gdbarch, CORE_ADDR bpaddr)
4199 {
4200   gdb_byte *buf;
4201   char map_type;
4202   CORE_ADDR boundary, func_start;
4203   int buf_len;
4204   enum bfd_endian order = gdbarch_byte_order_for_code (gdbarch);
4205   int i, any, last_it, last_it_count;
4206
4207   /* If we are using BKPT breakpoints, none of this is necessary.  */
4208   if (gdbarch_tdep (gdbarch)->thumb2_breakpoint == NULL)
4209     return bpaddr;
4210
4211   /* ARM mode does not have this problem.  */
4212   if (!arm_pc_is_thumb (gdbarch, bpaddr))
4213     return bpaddr;
4214
4215   /* We are setting a breakpoint in Thumb code that could potentially
4216      contain an IT block.  The first step is to find how much Thumb
4217      code there is; we do not need to read outside of known Thumb
4218      sequences.  */
4219   map_type = arm_find_mapping_symbol (bpaddr, &boundary);
4220   if (map_type == 0)
4221     /* Thumb-2 code must have mapping symbols to have a chance.  */
4222     return bpaddr;
4223
4224   bpaddr = gdbarch_addr_bits_remove (gdbarch, bpaddr);
4225
4226   if (find_pc_partial_function (bpaddr, NULL, &func_start, NULL)
4227       && func_start > boundary)
4228     boundary = func_start;
4229
4230   /* Search for a candidate IT instruction.  We have to do some fancy
4231      footwork to distinguish a real IT instruction from the second
4232      half of a 32-bit instruction, but there is no need for that if
4233      there's no candidate.  */
4234   buf_len = std::min (bpaddr - boundary, (CORE_ADDR) MAX_IT_BLOCK_PREFIX);
4235   if (buf_len == 0)
4236     /* No room for an IT instruction.  */
4237     return bpaddr;
4238
4239   buf = (gdb_byte *) xmalloc (buf_len);
4240   if (target_read_code (bpaddr - buf_len, buf, buf_len) != 0)
4241     return bpaddr;
4242   any = 0;
4243   for (i = 0; i < buf_len; i += 2)
4244     {
4245       unsigned short inst1 = extract_unsigned_integer (&buf[i], 2, order);
4246       if ((inst1 & 0xff00) == 0xbf00 && (inst1 & 0x000f) != 0)
4247         {
4248           any = 1;
4249           break;
4250         }
4251     }
4252
4253   if (any == 0)
4254     {
4255       xfree (buf);
4256       return bpaddr;
4257     }
4258
4259   /* OK, the code bytes before this instruction contain at least one
4260      halfword which resembles an IT instruction.  We know that it's
4261      Thumb code, but there are still two possibilities.  Either the
4262      halfword really is an IT instruction, or it is the second half of
4263      a 32-bit Thumb instruction.  The only way we can tell is to
4264      scan forwards from a known instruction boundary.  */
4265   if (bpaddr - boundary > IT_SCAN_THRESHOLD)
4266     {
4267       int definite;
4268
4269       /* There's a lot of code before this instruction.  Start with an
4270          optimistic search; it's easy to recognize halfwords that can
4271          not be the start of a 32-bit instruction, and use that to
4272          lock on to the instruction boundaries.  */
4273       buf = extend_buffer_earlier (buf, bpaddr, buf_len, IT_SCAN_THRESHOLD);
4274       if (buf == NULL)
4275         return bpaddr;
4276       buf_len = IT_SCAN_THRESHOLD;
4277
4278       definite = 0;
4279       for (i = 0; i < buf_len - sizeof (buf) && ! definite; i += 2)
4280         {
4281           unsigned short inst1 = extract_unsigned_integer (&buf[i], 2, order);
4282           if (thumb_insn_size (inst1) == 2)
4283             {
4284               definite = 1;
4285               break;
4286             }
4287         }
4288
4289       /* At this point, if DEFINITE, BUF[I] is the first place we
4290          are sure that we know the instruction boundaries, and it is far
4291          enough from BPADDR that we could not miss an IT instruction
4292          affecting BPADDR.  If ! DEFINITE, give up - start from a
4293          known boundary.  */
4294       if (! definite)
4295         {
4296           buf = extend_buffer_earlier (buf, bpaddr, buf_len,
4297                                        bpaddr - boundary);
4298           if (buf == NULL)
4299             return bpaddr;
4300           buf_len = bpaddr - boundary;
4301           i = 0;
4302         }
4303     }
4304   else
4305     {
4306       buf = extend_buffer_earlier (buf, bpaddr, buf_len, bpaddr - boundary);
4307       if (buf == NULL)
4308         return bpaddr;
4309       buf_len = bpaddr - boundary;
4310       i = 0;
4311     }
4312
4313   /* Scan forwards.  Find the last IT instruction before BPADDR.  */
4314   last_it = -1;
4315   last_it_count = 0;
4316   while (i < buf_len)
4317     {
4318       unsigned short inst1 = extract_unsigned_integer (&buf[i], 2, order);
4319       last_it_count--;
4320       if ((inst1 & 0xff00) == 0xbf00 && (inst1 & 0x000f) != 0)
4321         {
4322           last_it = i;
4323           if (inst1 & 0x0001)
4324             last_it_count = 4;
4325           else if (inst1 & 0x0002)
4326             last_it_count = 3;
4327           else if (inst1 & 0x0004)
4328             last_it_count = 2;
4329           else
4330             last_it_count = 1;
4331         }
4332       i += thumb_insn_size (inst1);
4333     }
4334
4335   xfree (buf);
4336
4337   if (last_it == -1)
4338     /* There wasn't really an IT instruction after all.  */
4339     return bpaddr;
4340
4341   if (last_it_count < 1)
4342     /* It was too far away.  */
4343     return bpaddr;
4344
4345   /* This really is a trouble spot.  Move the breakpoint to the IT
4346      instruction.  */
4347   return bpaddr - buf_len + last_it;
4348 }
4349
4350 /* ARM displaced stepping support.
4351
4352    Generally ARM displaced stepping works as follows:
4353
4354    1. When an instruction is to be single-stepped, it is first decoded by
4355       arm_process_displaced_insn.  Depending on the type of instruction, it is
4356       then copied to a scratch location, possibly in a modified form.  The
4357       copy_* set of functions performs such modification, as necessary.  A
4358       breakpoint is placed after the modified instruction in the scratch space
4359       to return control to GDB.  Note in particular that instructions which
4360       modify the PC will no longer do so after modification.
4361
4362    2. The instruction is single-stepped, by setting the PC to the scratch
4363       location address, and resuming.  Control returns to GDB when the
4364       breakpoint is hit.
4365
4366    3. A cleanup function (cleanup_*) is called corresponding to the copy_*
4367       function used for the current instruction.  This function's job is to
4368       put the CPU/memory state back to what it would have been if the
4369       instruction had been executed unmodified in its original location.  */
4370
4371 /* NOP instruction (mov r0, r0).  */
4372 #define ARM_NOP                         0xe1a00000
4373 #define THUMB_NOP 0x4600
4374
4375 /* Helper for register reads for displaced stepping.  In particular, this
4376    returns the PC as it would be seen by the instruction at its original
4377    location.  */
4378
4379 ULONGEST
4380 displaced_read_reg (struct regcache *regs, arm_displaced_step_closure *dsc,
4381                     int regno)
4382 {
4383   ULONGEST ret;
4384   CORE_ADDR from = dsc->insn_addr;
4385
4386   if (regno == ARM_PC_REGNUM)
4387     {
4388       /* Compute pipeline offset:
4389          - When executing an ARM instruction, PC reads as the address of the
4390          current instruction plus 8.
4391          - When executing a Thumb instruction, PC reads as the address of the
4392          current instruction plus 4.  */
4393
4394       if (!dsc->is_thumb)
4395         from += 8;
4396       else
4397         from += 4;
4398
4399       if (debug_displaced)
4400         fprintf_unfiltered (gdb_stdlog, "displaced: read pc value %.8lx\n",
4401                             (unsigned long) from);
4402       return (ULONGEST) from;
4403     }
4404   else
4405     {
4406       regcache_cooked_read_unsigned (regs, regno, &ret);
4407       if (debug_displaced)
4408         fprintf_unfiltered (gdb_stdlog, "displaced: read r%d value %.8lx\n",
4409                             regno, (unsigned long) ret);
4410       return ret;
4411     }
4412 }
4413
4414 static int
4415 displaced_in_arm_mode (struct regcache *regs)
4416 {
4417   ULONGEST ps;
4418   ULONGEST t_bit = arm_psr_thumb_bit (regs->arch ());
4419
4420   regcache_cooked_read_unsigned (regs, ARM_PS_REGNUM, &ps);
4421
4422   return (ps & t_bit) == 0;
4423 }
4424
4425 /* Write to the PC as from a branch instruction.  */
4426
4427 static void
4428 branch_write_pc (struct regcache *regs, arm_displaced_step_closure *dsc,
4429                  ULONGEST val)
4430 {
4431   if (!dsc->is_thumb)
4432     /* Note: If bits 0/1 are set, this branch would be unpredictable for
4433        architecture versions < 6.  */
4434     regcache_cooked_write_unsigned (regs, ARM_PC_REGNUM,
4435                                     val & ~(ULONGEST) 0x3);
4436   else
4437     regcache_cooked_write_unsigned (regs, ARM_PC_REGNUM,
4438                                     val & ~(ULONGEST) 0x1);
4439 }
4440
4441 /* Write to the PC as from a branch-exchange instruction.  */
4442
4443 static void
4444 bx_write_pc (struct regcache *regs, ULONGEST val)
4445 {
4446   ULONGEST ps;
4447   ULONGEST t_bit = arm_psr_thumb_bit (regs->arch ());
4448
4449   regcache_cooked_read_unsigned (regs, ARM_PS_REGNUM, &ps);
4450
4451   if ((val & 1) == 1)
4452     {
4453       regcache_cooked_write_unsigned (regs, ARM_PS_REGNUM, ps | t_bit);
4454       regcache_cooked_write_unsigned (regs, ARM_PC_REGNUM, val & 0xfffffffe);
4455     }
4456   else if ((val & 2) == 0)
4457     {
4458       regcache_cooked_write_unsigned (regs, ARM_PS_REGNUM, ps & ~t_bit);
4459       regcache_cooked_write_unsigned (regs, ARM_PC_REGNUM, val);
4460     }
4461   else
4462     {
4463       /* Unpredictable behaviour.  Try to do something sensible (switch to ARM
4464           mode, align dest to 4 bytes).  */
4465       warning (_("Single-stepping BX to non-word-aligned ARM instruction."));
4466       regcache_cooked_write_unsigned (regs, ARM_PS_REGNUM, ps & ~t_bit);
4467       regcache_cooked_write_unsigned (regs, ARM_PC_REGNUM, val & 0xfffffffc);
4468     }
4469 }
4470
4471 /* Write to the PC as if from a load instruction.  */
4472
4473 static void
4474 load_write_pc (struct regcache *regs, arm_displaced_step_closure *dsc,
4475                ULONGEST val)
4476 {
4477   if (DISPLACED_STEPPING_ARCH_VERSION >= 5)
4478     bx_write_pc (regs, val);
4479   else
4480     branch_write_pc (regs, dsc, val);
4481 }
4482
4483 /* Write to the PC as if from an ALU instruction.  */
4484
4485 static void
4486 alu_write_pc (struct regcache *regs, arm_displaced_step_closure *dsc,
4487               ULONGEST val)
4488 {
4489   if (DISPLACED_STEPPING_ARCH_VERSION >= 7 && !dsc->is_thumb)
4490     bx_write_pc (regs, val);
4491   else
4492     branch_write_pc (regs, dsc, val);
4493 }
4494
4495 /* Helper for writing to registers for displaced stepping.  Writing to the PC
4496    has a varying effects depending on the instruction which does the write:
4497    this is controlled by the WRITE_PC argument.  */
4498
4499 void
4500 displaced_write_reg (struct regcache *regs, arm_displaced_step_closure *dsc,
4501                      int regno, ULONGEST val, enum pc_write_style write_pc)
4502 {
4503   if (regno == ARM_PC_REGNUM)
4504     {
4505       if (debug_displaced)
4506         fprintf_unfiltered (gdb_stdlog, "displaced: writing pc %.8lx\n",
4507                             (unsigned long) val);
4508       switch (write_pc)
4509         {
4510         case BRANCH_WRITE_PC:
4511           branch_write_pc (regs, dsc, val);
4512           break;
4513
4514         case BX_WRITE_PC:
4515           bx_write_pc (regs, val);
4516           break;
4517
4518         case LOAD_WRITE_PC:
4519           load_write_pc (regs, dsc, val);
4520           break;
4521
4522         case ALU_WRITE_PC:
4523           alu_write_pc (regs, dsc, val);
4524           break;
4525
4526         case CANNOT_WRITE_PC:
4527           warning (_("Instruction wrote to PC in an unexpected way when "
4528                      "single-stepping"));
4529           break;
4530
4531         default:
4532           internal_error (__FILE__, __LINE__,
4533                           _("Invalid argument to displaced_write_reg"));
4534         }
4535
4536       dsc->wrote_to_pc = 1;
4537     }
4538   else
4539     {
4540       if (debug_displaced)
4541         fprintf_unfiltered (gdb_stdlog, "displaced: writing r%d value %.8lx\n",
4542                             regno, (unsigned long) val);
4543       regcache_cooked_write_unsigned (regs, regno, val);
4544     }
4545 }
4546
4547 /* This function is used to concisely determine if an instruction INSN
4548    references PC.  Register fields of interest in INSN should have the
4549    corresponding fields of BITMASK set to 0b1111.  The function
4550    returns return 1 if any of these fields in INSN reference the PC
4551    (also 0b1111, r15), else it returns 0.  */
4552
4553 static int
4554 insn_references_pc (uint32_t insn, uint32_t bitmask)
4555 {
4556   uint32_t lowbit = 1;
4557
4558   while (bitmask != 0)
4559     {
4560       uint32_t mask;
4561
4562       for (; lowbit && (bitmask & lowbit) == 0; lowbit <<= 1)
4563         ;
4564
4565       if (!lowbit)
4566         break;
4567
4568       mask = lowbit * 0xf;
4569
4570       if ((insn & mask) == mask)
4571         return 1;
4572
4573       bitmask &= ~mask;
4574     }
4575
4576   return 0;
4577 }
4578
4579 /* The simplest copy function.  Many instructions have the same effect no
4580    matter what address they are executed at: in those cases, use this.  */
4581
4582 static int
4583 arm_copy_unmodified (struct gdbarch *gdbarch, uint32_t insn,
4584                      const char *iname, arm_displaced_step_closure *dsc)
4585 {
4586   if (debug_displaced)
4587     fprintf_unfiltered (gdb_stdlog, "displaced: copying insn %.8lx, "
4588                         "opcode/class '%s' unmodified\n", (unsigned long) insn,
4589                         iname);
4590
4591   dsc->modinsn[0] = insn;
4592
4593   return 0;
4594 }
4595
4596 static int
4597 thumb_copy_unmodified_32bit (struct gdbarch *gdbarch, uint16_t insn1,
4598                              uint16_t insn2, const char *iname,
4599                              arm_displaced_step_closure *dsc)
4600 {
4601   if (debug_displaced)
4602     fprintf_unfiltered (gdb_stdlog, "displaced: copying insn %.4x %.4x, "
4603                         "opcode/class '%s' unmodified\n", insn1, insn2,
4604                         iname);
4605
4606   dsc->modinsn[0] = insn1;
4607   dsc->modinsn[1] = insn2;
4608   dsc->numinsns = 2;
4609
4610   return 0;
4611 }
4612
4613 /* Copy 16-bit Thumb(Thumb and 16-bit Thumb-2) instruction without any
4614    modification.  */
4615 static int
4616 thumb_copy_unmodified_16bit (struct gdbarch *gdbarch, uint16_t insn,
4617                              const char *iname,
4618                              arm_displaced_step_closure *dsc)
4619 {
4620   if (debug_displaced)
4621     fprintf_unfiltered (gdb_stdlog, "displaced: copying insn %.4x, "
4622                         "opcode/class '%s' unmodified\n", insn,
4623                         iname);
4624
4625   dsc->modinsn[0] = insn;
4626
4627   return 0;
4628 }
4629
4630 /* Preload instructions with immediate offset.  */
4631
4632 static void
4633 cleanup_preload (struct gdbarch *gdbarch,
4634                  struct regcache *regs, arm_displaced_step_closure *dsc)
4635 {
4636   displaced_write_reg (regs, dsc, 0, dsc->tmp[0], CANNOT_WRITE_PC);
4637   if (!dsc->u.preload.immed)
4638     displaced_write_reg (regs, dsc, 1, dsc->tmp[1], CANNOT_WRITE_PC);
4639 }
4640
4641 static void
4642 install_preload (struct gdbarch *gdbarch, struct regcache *regs,
4643                  arm_displaced_step_closure *dsc, unsigned int rn)
4644 {
4645   ULONGEST rn_val;
4646   /* Preload instructions:
4647
4648      {pli/pld} [rn, #+/-imm]
4649      ->
4650      {pli/pld} [r0, #+/-imm].  */
4651
4652   dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
4653   rn_val = displaced_read_reg (regs, dsc, rn);
4654   displaced_write_reg (regs, dsc, 0, rn_val, CANNOT_WRITE_PC);
4655   dsc->u.preload.immed = 1;
4656
4657   dsc->cleanup = &cleanup_preload;
4658 }
4659
4660 static int
4661 arm_copy_preload (struct gdbarch *gdbarch, uint32_t insn, struct regcache *regs,
4662                   arm_displaced_step_closure *dsc)
4663 {
4664   unsigned int rn = bits (insn, 16, 19);
4665
4666   if (!insn_references_pc (insn, 0x000f0000ul))
4667     return arm_copy_unmodified (gdbarch, insn, "preload", dsc);
4668
4669   if (debug_displaced)
4670     fprintf_unfiltered (gdb_stdlog, "displaced: copying preload insn %.8lx\n",
4671                         (unsigned long) insn);
4672
4673   dsc->modinsn[0] = insn & 0xfff0ffff;
4674
4675   install_preload (gdbarch, regs, dsc, rn);
4676
4677   return 0;
4678 }
4679
4680 static int
4681 thumb2_copy_preload (struct gdbarch *gdbarch, uint16_t insn1, uint16_t insn2,
4682                      struct regcache *regs, arm_displaced_step_closure *dsc)
4683 {
4684   unsigned int rn = bits (insn1, 0, 3);
4685   unsigned int u_bit = bit (insn1, 7);
4686   int imm12 = bits (insn2, 0, 11);
4687   ULONGEST pc_val;
4688
4689   if (rn != ARM_PC_REGNUM)
4690     return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2, "preload", dsc);
4691
4692   /* PC is only allowed to use in PLI (immediate,literal) Encoding T3, and
4693      PLD (literal) Encoding T1.  */
4694   if (debug_displaced)
4695     fprintf_unfiltered (gdb_stdlog,
4696                         "displaced: copying pld/pli pc (0x%x) %c imm12 %.4x\n",
4697                         (unsigned int) dsc->insn_addr, u_bit ? '+' : '-',
4698                         imm12);
4699
4700   if (!u_bit)
4701     imm12 = -1 * imm12;
4702
4703   /* Rewrite instruction {pli/pld} PC imm12 into:
4704      Prepare: tmp[0] <- r0, tmp[1] <- r1, r0 <- pc, r1 <- imm12
4705
4706      {pli/pld} [r0, r1]
4707
4708      Cleanup: r0 <- tmp[0], r1 <- tmp[1].  */
4709
4710   dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
4711   dsc->tmp[1] = displaced_read_reg (regs, dsc, 1);
4712
4713   pc_val = displaced_read_reg (regs, dsc, ARM_PC_REGNUM);
4714
4715   displaced_write_reg (regs, dsc, 0, pc_val, CANNOT_WRITE_PC);
4716   displaced_write_reg (regs, dsc, 1, imm12, CANNOT_WRITE_PC);
4717   dsc->u.preload.immed = 0;
4718
4719   /* {pli/pld} [r0, r1] */
4720   dsc->modinsn[0] = insn1 & 0xfff0;
4721   dsc->modinsn[1] = 0xf001;
4722   dsc->numinsns = 2;
4723
4724   dsc->cleanup = &cleanup_preload;
4725   return 0;
4726 }
4727
4728 /* Preload instructions with register offset.  */
4729
4730 static void
4731 install_preload_reg(struct gdbarch *gdbarch, struct regcache *regs,
4732                     arm_displaced_step_closure *dsc, unsigned int rn,
4733                     unsigned int rm)
4734 {
4735   ULONGEST rn_val, rm_val;
4736
4737   /* Preload register-offset instructions:
4738
4739      {pli/pld} [rn, rm {, shift}]
4740      ->
4741      {pli/pld} [r0, r1 {, shift}].  */
4742
4743   dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
4744   dsc->tmp[1] = displaced_read_reg (regs, dsc, 1);
4745   rn_val = displaced_read_reg (regs, dsc, rn);
4746   rm_val = displaced_read_reg (regs, dsc, rm);
4747   displaced_write_reg (regs, dsc, 0, rn_val, CANNOT_WRITE_PC);
4748   displaced_write_reg (regs, dsc, 1, rm_val, CANNOT_WRITE_PC);
4749   dsc->u.preload.immed = 0;
4750
4751   dsc->cleanup = &cleanup_preload;
4752 }
4753
4754 static int
4755 arm_copy_preload_reg (struct gdbarch *gdbarch, uint32_t insn,
4756                       struct regcache *regs,
4757                       arm_displaced_step_closure *dsc)
4758 {
4759   unsigned int rn = bits (insn, 16, 19);
4760   unsigned int rm = bits (insn, 0, 3);
4761
4762
4763   if (!insn_references_pc (insn, 0x000f000ful))
4764     return arm_copy_unmodified (gdbarch, insn, "preload reg", dsc);
4765
4766   if (debug_displaced)
4767     fprintf_unfiltered (gdb_stdlog, "displaced: copying preload insn %.8lx\n",
4768                         (unsigned long) insn);
4769
4770   dsc->modinsn[0] = (insn & 0xfff0fff0) | 0x1;
4771
4772   install_preload_reg (gdbarch, regs, dsc, rn, rm);
4773   return 0;
4774 }
4775
4776 /* Copy/cleanup coprocessor load and store instructions.  */
4777
4778 static void
4779 cleanup_copro_load_store (struct gdbarch *gdbarch,
4780                           struct regcache *regs,
4781                           arm_displaced_step_closure *dsc)
4782 {
4783   ULONGEST rn_val = displaced_read_reg (regs, dsc, 0);
4784
4785   displaced_write_reg (regs, dsc, 0, dsc->tmp[0], CANNOT_WRITE_PC);
4786
4787   if (dsc->u.ldst.writeback)
4788     displaced_write_reg (regs, dsc, dsc->u.ldst.rn, rn_val, LOAD_WRITE_PC);
4789 }
4790
4791 static void
4792 install_copro_load_store (struct gdbarch *gdbarch, struct regcache *regs,
4793                           arm_displaced_step_closure *dsc,
4794                           int writeback, unsigned int rn)
4795 {
4796   ULONGEST rn_val;
4797
4798   /* Coprocessor load/store instructions:
4799
4800      {stc/stc2} [<Rn>, #+/-imm]  (and other immediate addressing modes)
4801      ->
4802      {stc/stc2} [r0, #+/-imm].
4803
4804      ldc/ldc2 are handled identically.  */
4805
4806   dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
4807   rn_val = displaced_read_reg (regs, dsc, rn);
4808   /* PC should be 4-byte aligned.  */
4809   rn_val = rn_val & 0xfffffffc;
4810   displaced_write_reg (regs, dsc, 0, rn_val, CANNOT_WRITE_PC);
4811
4812   dsc->u.ldst.writeback = writeback;
4813   dsc->u.ldst.rn = rn;
4814
4815   dsc->cleanup = &cleanup_copro_load_store;
4816 }
4817
4818 static int
4819 arm_copy_copro_load_store (struct gdbarch *gdbarch, uint32_t insn,
4820                            struct regcache *regs,
4821                            arm_displaced_step_closure *dsc)
4822 {
4823   unsigned int rn = bits (insn, 16, 19);
4824
4825   if (!insn_references_pc (insn, 0x000f0000ul))
4826     return arm_copy_unmodified (gdbarch, insn, "copro load/store", dsc);
4827
4828   if (debug_displaced)
4829     fprintf_unfiltered (gdb_stdlog, "displaced: copying coprocessor "
4830                         "load/store insn %.8lx\n", (unsigned long) insn);
4831
4832   dsc->modinsn[0] = insn & 0xfff0ffff;
4833
4834   install_copro_load_store (gdbarch, regs, dsc, bit (insn, 25), rn);
4835
4836   return 0;
4837 }
4838
4839 static int
4840 thumb2_copy_copro_load_store (struct gdbarch *gdbarch, uint16_t insn1,
4841                               uint16_t insn2, struct regcache *regs,
4842                               arm_displaced_step_closure *dsc)
4843 {
4844   unsigned int rn = bits (insn1, 0, 3);
4845
4846   if (rn != ARM_PC_REGNUM)
4847     return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
4848                                         "copro load/store", dsc);
4849
4850   if (debug_displaced)
4851     fprintf_unfiltered (gdb_stdlog, "displaced: copying coprocessor "
4852                         "load/store insn %.4x%.4x\n", insn1, insn2);
4853
4854   dsc->modinsn[0] = insn1 & 0xfff0;
4855   dsc->modinsn[1] = insn2;
4856   dsc->numinsns = 2;
4857
4858   /* This function is called for copying instruction LDC/LDC2/VLDR, which
4859      doesn't support writeback, so pass 0.  */
4860   install_copro_load_store (gdbarch, regs, dsc, 0, rn);
4861
4862   return 0;
4863 }
4864
4865 /* Clean up branch instructions (actually perform the branch, by setting
4866    PC).  */
4867
4868 static void
4869 cleanup_branch (struct gdbarch *gdbarch, struct regcache *regs,
4870                 arm_displaced_step_closure *dsc)
4871 {
4872   uint32_t status = displaced_read_reg (regs, dsc, ARM_PS_REGNUM);
4873   int branch_taken = condition_true (dsc->u.branch.cond, status);
4874   enum pc_write_style write_pc = dsc->u.branch.exchange
4875                                  ? BX_WRITE_PC : BRANCH_WRITE_PC;
4876
4877   if (!branch_taken)
4878     return;
4879
4880   if (dsc->u.branch.link)
4881     {
4882       /* The value of LR should be the next insn of current one.  In order
4883        not to confuse logic hanlding later insn `bx lr', if current insn mode
4884        is Thumb, the bit 0 of LR value should be set to 1.  */
4885       ULONGEST next_insn_addr = dsc->insn_addr + dsc->insn_size;
4886
4887       if (dsc->is_thumb)
4888         next_insn_addr |= 0x1;
4889
4890       displaced_write_reg (regs, dsc, ARM_LR_REGNUM, next_insn_addr,
4891                            CANNOT_WRITE_PC);
4892     }
4893
4894   displaced_write_reg (regs, dsc, ARM_PC_REGNUM, dsc->u.branch.dest, write_pc);
4895 }
4896
4897 /* Copy B/BL/BLX instructions with immediate destinations.  */
4898
4899 static void
4900 install_b_bl_blx (struct gdbarch *gdbarch, struct regcache *regs,
4901                   arm_displaced_step_closure *dsc,
4902                   unsigned int cond, int exchange, int link, long offset)
4903 {
4904   /* Implement "BL<cond> <label>" as:
4905
4906      Preparation: cond <- instruction condition
4907      Insn: mov r0, r0  (nop)
4908      Cleanup: if (condition true) { r14 <- pc; pc <- label }.
4909
4910      B<cond> similar, but don't set r14 in cleanup.  */
4911
4912   dsc->u.branch.cond = cond;
4913   dsc->u.branch.link = link;
4914   dsc->u.branch.exchange = exchange;
4915
4916   dsc->u.branch.dest = dsc->insn_addr;
4917   if (link && exchange)
4918     /* For BLX, offset is computed from the Align (PC, 4).  */
4919     dsc->u.branch.dest = dsc->u.branch.dest & 0xfffffffc;
4920
4921   if (dsc->is_thumb)
4922     dsc->u.branch.dest += 4 + offset;
4923   else
4924     dsc->u.branch.dest += 8 + offset;
4925
4926   dsc->cleanup = &cleanup_branch;
4927 }
4928 static int
4929 arm_copy_b_bl_blx (struct gdbarch *gdbarch, uint32_t insn,
4930                    struct regcache *regs, arm_displaced_step_closure *dsc)
4931 {
4932   unsigned int cond = bits (insn, 28, 31);
4933   int exchange = (cond == 0xf);
4934   int link = exchange || bit (insn, 24);
4935   long offset;
4936
4937   if (debug_displaced)
4938     fprintf_unfiltered (gdb_stdlog, "displaced: copying %s immediate insn "
4939                         "%.8lx\n", (exchange) ? "blx" : (link) ? "bl" : "b",
4940                         (unsigned long) insn);
4941   if (exchange)
4942     /* For BLX, set bit 0 of the destination.  The cleanup_branch function will
4943        then arrange the switch into Thumb mode.  */
4944     offset = (bits (insn, 0, 23) << 2) | (bit (insn, 24) << 1) | 1;
4945   else
4946     offset = bits (insn, 0, 23) << 2;
4947
4948   if (bit (offset, 25))
4949     offset = offset | ~0x3ffffff;
4950
4951   dsc->modinsn[0] = ARM_NOP;
4952
4953   install_b_bl_blx (gdbarch, regs, dsc, cond, exchange, link, offset);
4954   return 0;
4955 }
4956
4957 static int
4958 thumb2_copy_b_bl_blx (struct gdbarch *gdbarch, uint16_t insn1,
4959                       uint16_t insn2, struct regcache *regs,
4960                       arm_displaced_step_closure *dsc)
4961 {
4962   int link = bit (insn2, 14);
4963   int exchange = link && !bit (insn2, 12);
4964   int cond = INST_AL;
4965   long offset = 0;
4966   int j1 = bit (insn2, 13);
4967   int j2 = bit (insn2, 11);
4968   int s = sbits (insn1, 10, 10);
4969   int i1 = !(j1 ^ bit (insn1, 10));
4970   int i2 = !(j2 ^ bit (insn1, 10));
4971
4972   if (!link && !exchange) /* B */
4973     {
4974       offset = (bits (insn2, 0, 10) << 1);
4975       if (bit (insn2, 12)) /* Encoding T4 */
4976         {
4977           offset |= (bits (insn1, 0, 9) << 12)
4978             | (i2 << 22)
4979             | (i1 << 23)
4980             | (s << 24);
4981           cond = INST_AL;
4982         }
4983       else /* Encoding T3 */
4984         {
4985           offset |= (bits (insn1, 0, 5) << 12)
4986             | (j1 << 18)
4987             | (j2 << 19)
4988             | (s << 20);
4989           cond = bits (insn1, 6, 9);
4990         }
4991     }
4992   else
4993     {
4994       offset = (bits (insn1, 0, 9) << 12);
4995       offset |= ((i2 << 22) | (i1 << 23) | (s << 24));
4996       offset |= exchange ?
4997         (bits (insn2, 1, 10) << 2) : (bits (insn2, 0, 10) << 1);
4998     }
4999
5000   if (debug_displaced)
5001     fprintf_unfiltered (gdb_stdlog, "displaced: copying %s insn "
5002                         "%.4x %.4x with offset %.8lx\n",
5003                         link ? (exchange) ? "blx" : "bl" : "b",
5004                         insn1, insn2, offset);
5005
5006   dsc->modinsn[0] = THUMB_NOP;
5007
5008   install_b_bl_blx (gdbarch, regs, dsc, cond, exchange, link, offset);
5009   return 0;
5010 }
5011
5012 /* Copy B Thumb instructions.  */
5013 static int
5014 thumb_copy_b (struct gdbarch *gdbarch, uint16_t insn,
5015               arm_displaced_step_closure *dsc)
5016 {
5017   unsigned int cond = 0;
5018   int offset = 0;
5019   unsigned short bit_12_15 = bits (insn, 12, 15);
5020   CORE_ADDR from = dsc->insn_addr;
5021
5022   if (bit_12_15 == 0xd)
5023     {
5024       /* offset = SignExtend (imm8:0, 32) */
5025       offset = sbits ((insn << 1), 0, 8);
5026       cond = bits (insn, 8, 11);
5027     }
5028   else if (bit_12_15 == 0xe) /* Encoding T2 */
5029     {
5030       offset = sbits ((insn << 1), 0, 11);
5031       cond = INST_AL;
5032     }
5033
5034   if (debug_displaced)
5035     fprintf_unfiltered (gdb_stdlog,
5036                         "displaced: copying b immediate insn %.4x "
5037                         "with offset %d\n", insn, offset);
5038
5039   dsc->u.branch.cond = cond;
5040   dsc->u.branch.link = 0;
5041   dsc->u.branch.exchange = 0;
5042   dsc->u.branch.dest = from + 4 + offset;
5043
5044   dsc->modinsn[0] = THUMB_NOP;
5045
5046   dsc->cleanup = &cleanup_branch;
5047
5048   return 0;
5049 }
5050
5051 /* Copy BX/BLX with register-specified destinations.  */
5052
5053 static void
5054 install_bx_blx_reg (struct gdbarch *gdbarch, struct regcache *regs,
5055                     arm_displaced_step_closure *dsc, int link,
5056                     unsigned int cond, unsigned int rm)
5057 {
5058   /* Implement {BX,BLX}<cond> <reg>" as:
5059
5060      Preparation: cond <- instruction condition
5061      Insn: mov r0, r0 (nop)
5062      Cleanup: if (condition true) { r14 <- pc; pc <- dest; }.
5063
5064      Don't set r14 in cleanup for BX.  */
5065
5066   dsc->u.branch.dest = displaced_read_reg (regs, dsc, rm);
5067
5068   dsc->u.branch.cond = cond;
5069   dsc->u.branch.link = link;
5070
5071   dsc->u.branch.exchange = 1;
5072
5073   dsc->cleanup = &cleanup_branch;
5074 }
5075
5076 static int
5077 arm_copy_bx_blx_reg (struct gdbarch *gdbarch, uint32_t insn,
5078                      struct regcache *regs, arm_displaced_step_closure *dsc)
5079 {
5080   unsigned int cond = bits (insn, 28, 31);
5081   /* BX:  x12xxx1x
5082      BLX: x12xxx3x.  */
5083   int link = bit (insn, 5);
5084   unsigned int rm = bits (insn, 0, 3);
5085
5086   if (debug_displaced)
5087     fprintf_unfiltered (gdb_stdlog, "displaced: copying insn %.8lx",
5088                         (unsigned long) insn);
5089
5090   dsc->modinsn[0] = ARM_NOP;
5091
5092   install_bx_blx_reg (gdbarch, regs, dsc, link, cond, rm);
5093   return 0;
5094 }
5095
5096 static int
5097 thumb_copy_bx_blx_reg (struct gdbarch *gdbarch, uint16_t insn,
5098                        struct regcache *regs,
5099                        arm_displaced_step_closure *dsc)
5100 {
5101   int link = bit (insn, 7);
5102   unsigned int rm = bits (insn, 3, 6);
5103
5104   if (debug_displaced)
5105     fprintf_unfiltered (gdb_stdlog, "displaced: copying insn %.4x",
5106                         (unsigned short) insn);
5107
5108   dsc->modinsn[0] = THUMB_NOP;
5109
5110   install_bx_blx_reg (gdbarch, regs, dsc, link, INST_AL, rm);
5111
5112   return 0;
5113 }
5114
5115
5116 /* Copy/cleanup arithmetic/logic instruction with immediate RHS.  */
5117
5118 static void
5119 cleanup_alu_imm (struct gdbarch *gdbarch,
5120                  struct regcache *regs, arm_displaced_step_closure *dsc)
5121 {
5122   ULONGEST rd_val = displaced_read_reg (regs, dsc, 0);
5123   displaced_write_reg (regs, dsc, 0, dsc->tmp[0], CANNOT_WRITE_PC);
5124   displaced_write_reg (regs, dsc, 1, dsc->tmp[1], CANNOT_WRITE_PC);
5125   displaced_write_reg (regs, dsc, dsc->rd, rd_val, ALU_WRITE_PC);
5126 }
5127
5128 static int
5129 arm_copy_alu_imm (struct gdbarch *gdbarch, uint32_t insn, struct regcache *regs,
5130                   arm_displaced_step_closure *dsc)
5131 {
5132   unsigned int rn = bits (insn, 16, 19);
5133   unsigned int rd = bits (insn, 12, 15);
5134   unsigned int op = bits (insn, 21, 24);
5135   int is_mov = (op == 0xd);
5136   ULONGEST rd_val, rn_val;
5137
5138   if (!insn_references_pc (insn, 0x000ff000ul))
5139     return arm_copy_unmodified (gdbarch, insn, "ALU immediate", dsc);
5140
5141   if (debug_displaced)
5142     fprintf_unfiltered (gdb_stdlog, "displaced: copying immediate %s insn "
5143                         "%.8lx\n", is_mov ? "move" : "ALU",
5144                         (unsigned long) insn);
5145
5146   /* Instruction is of form:
5147
5148      <op><cond> rd, [rn,] #imm
5149
5150      Rewrite as:
5151
5152      Preparation: tmp1, tmp2 <- r0, r1;
5153                   r0, r1 <- rd, rn
5154      Insn: <op><cond> r0, r1, #imm
5155      Cleanup: rd <- r0; r0 <- tmp1; r1 <- tmp2
5156   */
5157
5158   dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
5159   dsc->tmp[1] = displaced_read_reg (regs, dsc, 1);
5160   rn_val = displaced_read_reg (regs, dsc, rn);
5161   rd_val = displaced_read_reg (regs, dsc, rd);
5162   displaced_write_reg (regs, dsc, 0, rd_val, CANNOT_WRITE_PC);
5163   displaced_write_reg (regs, dsc, 1, rn_val, CANNOT_WRITE_PC);
5164   dsc->rd = rd;
5165
5166   if (is_mov)
5167     dsc->modinsn[0] = insn & 0xfff00fff;
5168   else
5169     dsc->modinsn[0] = (insn & 0xfff00fff) | 0x10000;
5170
5171   dsc->cleanup = &cleanup_alu_imm;
5172
5173   return 0;
5174 }
5175
5176 static int
5177 thumb2_copy_alu_imm (struct gdbarch *gdbarch, uint16_t insn1,
5178                      uint16_t insn2, struct regcache *regs,
5179                      arm_displaced_step_closure *dsc)
5180 {
5181   unsigned int op = bits (insn1, 5, 8);
5182   unsigned int rn, rm, rd;
5183   ULONGEST rd_val, rn_val;
5184
5185   rn = bits (insn1, 0, 3); /* Rn */
5186   rm = bits (insn2, 0, 3); /* Rm */
5187   rd = bits (insn2, 8, 11); /* Rd */
5188
5189   /* This routine is only called for instruction MOV.  */
5190   gdb_assert (op == 0x2 && rn == 0xf);
5191
5192   if (rm != ARM_PC_REGNUM && rd != ARM_PC_REGNUM)
5193     return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2, "ALU imm", dsc);
5194
5195   if (debug_displaced)
5196     fprintf_unfiltered (gdb_stdlog, "displaced: copying reg %s insn %.4x%.4x\n",
5197                         "ALU", insn1, insn2);
5198
5199   /* Instruction is of form:
5200
5201      <op><cond> rd, [rn,] #imm
5202
5203      Rewrite as:
5204
5205      Preparation: tmp1, tmp2 <- r0, r1;
5206                   r0, r1 <- rd, rn
5207      Insn: <op><cond> r0, r1, #imm
5208      Cleanup: rd <- r0; r0 <- tmp1; r1 <- tmp2
5209   */
5210
5211   dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
5212   dsc->tmp[1] = displaced_read_reg (regs, dsc, 1);
5213   rn_val = displaced_read_reg (regs, dsc, rn);
5214   rd_val = displaced_read_reg (regs, dsc, rd);
5215   displaced_write_reg (regs, dsc, 0, rd_val, CANNOT_WRITE_PC);
5216   displaced_write_reg (regs, dsc, 1, rn_val, CANNOT_WRITE_PC);
5217   dsc->rd = rd;
5218
5219   dsc->modinsn[0] = insn1;
5220   dsc->modinsn[1] = ((insn2 & 0xf0f0) | 0x1);
5221   dsc->numinsns = 2;
5222
5223   dsc->cleanup = &cleanup_alu_imm;
5224
5225   return 0;
5226 }
5227
5228 /* Copy/cleanup arithmetic/logic insns with register RHS.  */
5229
5230 static void
5231 cleanup_alu_reg (struct gdbarch *gdbarch,
5232                  struct regcache *regs, arm_displaced_step_closure *dsc)
5233 {
5234   ULONGEST rd_val;
5235   int i;
5236
5237   rd_val = displaced_read_reg (regs, dsc, 0);
5238
5239   for (i = 0; i < 3; i++)
5240     displaced_write_reg (regs, dsc, i, dsc->tmp[i], CANNOT_WRITE_PC);
5241
5242   displaced_write_reg (regs, dsc, dsc->rd, rd_val, ALU_WRITE_PC);
5243 }
5244
5245 static void
5246 install_alu_reg (struct gdbarch *gdbarch, struct regcache *regs,
5247                  arm_displaced_step_closure *dsc,
5248                  unsigned int rd, unsigned int rn, unsigned int rm)
5249 {
5250   ULONGEST rd_val, rn_val, rm_val;
5251
5252   /* Instruction is of form:
5253
5254      <op><cond> rd, [rn,] rm [, <shift>]
5255
5256      Rewrite as:
5257
5258      Preparation: tmp1, tmp2, tmp3 <- r0, r1, r2;
5259                   r0, r1, r2 <- rd, rn, rm
5260      Insn: <op><cond> r0, [r1,] r2 [, <shift>]
5261      Cleanup: rd <- r0; r0, r1, r2 <- tmp1, tmp2, tmp3
5262   */
5263
5264   dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
5265   dsc->tmp[1] = displaced_read_reg (regs, dsc, 1);
5266   dsc->tmp[2] = displaced_read_reg (regs, dsc, 2);
5267   rd_val = displaced_read_reg (regs, dsc, rd);
5268   rn_val = displaced_read_reg (regs, dsc, rn);
5269   rm_val = displaced_read_reg (regs, dsc, rm);
5270   displaced_write_reg (regs, dsc, 0, rd_val, CANNOT_WRITE_PC);
5271   displaced_write_reg (regs, dsc, 1, rn_val, CANNOT_WRITE_PC);
5272   displaced_write_reg (regs, dsc, 2, rm_val, CANNOT_WRITE_PC);
5273   dsc->rd = rd;
5274
5275   dsc->cleanup = &cleanup_alu_reg;
5276 }
5277
5278 static int
5279 arm_copy_alu_reg (struct gdbarch *gdbarch, uint32_t insn, struct regcache *regs,
5280                   arm_displaced_step_closure *dsc)
5281 {
5282   unsigned int op = bits (insn, 21, 24);
5283   int is_mov = (op == 0xd);
5284
5285   if (!insn_references_pc (insn, 0x000ff00ful))
5286     return arm_copy_unmodified (gdbarch, insn, "ALU reg", dsc);
5287
5288   if (debug_displaced)
5289     fprintf_unfiltered (gdb_stdlog, "displaced: copying reg %s insn %.8lx\n",
5290                         is_mov ? "move" : "ALU", (unsigned long) insn);
5291
5292   if (is_mov)
5293     dsc->modinsn[0] = (insn & 0xfff00ff0) | 0x2;
5294   else
5295     dsc->modinsn[0] = (insn & 0xfff00ff0) | 0x10002;
5296
5297   install_alu_reg (gdbarch, regs, dsc, bits (insn, 12, 15), bits (insn, 16, 19),
5298                    bits (insn, 0, 3));
5299   return 0;
5300 }
5301
5302 static int
5303 thumb_copy_alu_reg (struct gdbarch *gdbarch, uint16_t insn,
5304                     struct regcache *regs,
5305                     arm_displaced_step_closure *dsc)
5306 {
5307   unsigned rm, rd;
5308
5309   rm = bits (insn, 3, 6);
5310   rd = (bit (insn, 7) << 3) | bits (insn, 0, 2);
5311
5312   if (rd != ARM_PC_REGNUM && rm != ARM_PC_REGNUM)
5313     return thumb_copy_unmodified_16bit (gdbarch, insn, "ALU reg", dsc);
5314
5315   if (debug_displaced)
5316     fprintf_unfiltered (gdb_stdlog, "displaced: copying ALU reg insn %.4x\n",
5317                         (unsigned short) insn);
5318
5319   dsc->modinsn[0] = ((insn & 0xff00) | 0x10);
5320
5321   install_alu_reg (gdbarch, regs, dsc, rd, rd, rm);
5322
5323   return 0;
5324 }
5325
5326 /* Cleanup/copy arithmetic/logic insns with shifted register RHS.  */
5327
5328 static void
5329 cleanup_alu_shifted_reg (struct gdbarch *gdbarch,
5330                          struct regcache *regs,
5331                          arm_displaced_step_closure *dsc)
5332 {
5333   ULONGEST rd_val = displaced_read_reg (regs, dsc, 0);
5334   int i;
5335
5336   for (i = 0; i < 4; i++)
5337     displaced_write_reg (regs, dsc, i, dsc->tmp[i], CANNOT_WRITE_PC);
5338
5339   displaced_write_reg (regs, dsc, dsc->rd, rd_val, ALU_WRITE_PC);
5340 }
5341
5342 static void
5343 install_alu_shifted_reg (struct gdbarch *gdbarch, struct regcache *regs,
5344                          arm_displaced_step_closure *dsc,
5345                          unsigned int rd, unsigned int rn, unsigned int rm,
5346                          unsigned rs)
5347 {
5348   int i;
5349   ULONGEST rd_val, rn_val, rm_val, rs_val;
5350
5351   /* Instruction is of form:
5352
5353      <op><cond> rd, [rn,] rm, <shift> rs
5354
5355      Rewrite as:
5356
5357      Preparation: tmp1, tmp2, tmp3, tmp4 <- r0, r1, r2, r3
5358                   r0, r1, r2, r3 <- rd, rn, rm, rs
5359      Insn: <op><cond> r0, r1, r2, <shift> r3
5360      Cleanup: tmp5 <- r0
5361               r0, r1, r2, r3 <- tmp1, tmp2, tmp3, tmp4
5362               rd <- tmp5
5363   */
5364
5365   for (i = 0; i < 4; i++)
5366     dsc->tmp[i] = displaced_read_reg (regs, dsc, i);
5367
5368   rd_val = displaced_read_reg (regs, dsc, rd);
5369   rn_val = displaced_read_reg (regs, dsc, rn);
5370   rm_val = displaced_read_reg (regs, dsc, rm);
5371   rs_val = displaced_read_reg (regs, dsc, rs);
5372   displaced_write_reg (regs, dsc, 0, rd_val, CANNOT_WRITE_PC);
5373   displaced_write_reg (regs, dsc, 1, rn_val, CANNOT_WRITE_PC);
5374   displaced_write_reg (regs, dsc, 2, rm_val, CANNOT_WRITE_PC);
5375   displaced_write_reg (regs, dsc, 3, rs_val, CANNOT_WRITE_PC);
5376   dsc->rd = rd;
5377   dsc->cleanup = &cleanup_alu_shifted_reg;
5378 }
5379
5380 static int
5381 arm_copy_alu_shifted_reg (struct gdbarch *gdbarch, uint32_t insn,
5382                           struct regcache *regs,
5383                           arm_displaced_step_closure *dsc)
5384 {
5385   unsigned int op = bits (insn, 21, 24);
5386   int is_mov = (op == 0xd);
5387   unsigned int rd, rn, rm, rs;
5388
5389   if (!insn_references_pc (insn, 0x000fff0ful))
5390     return arm_copy_unmodified (gdbarch, insn, "ALU shifted reg", dsc);
5391
5392   if (debug_displaced)
5393     fprintf_unfiltered (gdb_stdlog, "displaced: copying shifted reg %s insn "
5394                         "%.8lx\n", is_mov ? "move" : "ALU",
5395                         (unsigned long) insn);
5396
5397   rn = bits (insn, 16, 19);
5398   rm = bits (insn, 0, 3);
5399   rs = bits (insn, 8, 11);
5400   rd = bits (insn, 12, 15);
5401
5402   if (is_mov)
5403     dsc->modinsn[0] = (insn & 0xfff000f0) | 0x302;
5404   else
5405     dsc->modinsn[0] = (insn & 0xfff000f0) | 0x10302;
5406
5407   install_alu_shifted_reg (gdbarch, regs, dsc, rd, rn, rm, rs);
5408
5409   return 0;
5410 }
5411
5412 /* Clean up load instructions.  */
5413
5414 static void
5415 cleanup_load (struct gdbarch *gdbarch, struct regcache *regs,
5416               arm_displaced_step_closure *dsc)
5417 {
5418   ULONGEST rt_val, rt_val2 = 0, rn_val;
5419
5420   rt_val = displaced_read_reg (regs, dsc, 0);
5421   if (dsc->u.ldst.xfersize == 8)
5422     rt_val2 = displaced_read_reg (regs, dsc, 1);
5423   rn_val = displaced_read_reg (regs, dsc, 2);
5424
5425   displaced_write_reg (regs, dsc, 0, dsc->tmp[0], CANNOT_WRITE_PC);
5426   if (dsc->u.ldst.xfersize > 4)
5427     displaced_write_reg (regs, dsc, 1, dsc->tmp[1], CANNOT_WRITE_PC);
5428   displaced_write_reg (regs, dsc, 2, dsc->tmp[2], CANNOT_WRITE_PC);
5429   if (!dsc->u.ldst.immed)
5430     displaced_write_reg (regs, dsc, 3, dsc->tmp[3], CANNOT_WRITE_PC);
5431
5432   /* Handle register writeback.  */
5433   if (dsc->u.ldst.writeback)
5434     displaced_write_reg (regs, dsc, dsc->u.ldst.rn, rn_val, CANNOT_WRITE_PC);
5435   /* Put result in right place.  */
5436   displaced_write_reg (regs, dsc, dsc->rd, rt_val, LOAD_WRITE_PC);
5437   if (dsc->u.ldst.xfersize == 8)
5438     displaced_write_reg (regs, dsc, dsc->rd + 1, rt_val2, LOAD_WRITE_PC);
5439 }
5440
5441 /* Clean up store instructions.  */
5442
5443 static void
5444 cleanup_store (struct gdbarch *gdbarch, struct regcache *regs,
5445                arm_displaced_step_closure *dsc)
5446 {
5447   ULONGEST rn_val = displaced_read_reg (regs, dsc, 2);
5448
5449   displaced_write_reg (regs, dsc, 0, dsc->tmp[0], CANNOT_WRITE_PC);
5450   if (dsc->u.ldst.xfersize > 4)
5451     displaced_write_reg (regs, dsc, 1, dsc->tmp[1], CANNOT_WRITE_PC);
5452   displaced_write_reg (regs, dsc, 2, dsc->tmp[2], CANNOT_WRITE_PC);
5453   if (!dsc->u.ldst.immed)
5454     displaced_write_reg (regs, dsc, 3, dsc->tmp[3], CANNOT_WRITE_PC);
5455   if (!dsc->u.ldst.restore_r4)
5456     displaced_write_reg (regs, dsc, 4, dsc->tmp[4], CANNOT_WRITE_PC);
5457
5458   /* Writeback.  */
5459   if (dsc->u.ldst.writeback)
5460     displaced_write_reg (regs, dsc, dsc->u.ldst.rn, rn_val, CANNOT_WRITE_PC);
5461 }
5462
5463 /* Copy "extra" load/store instructions.  These are halfword/doubleword
5464    transfers, which have a different encoding to byte/word transfers.  */
5465
5466 static int
5467 arm_copy_extra_ld_st (struct gdbarch *gdbarch, uint32_t insn, int unprivileged,
5468                       struct regcache *regs, arm_displaced_step_closure *dsc)
5469 {
5470   unsigned int op1 = bits (insn, 20, 24);
5471   unsigned int op2 = bits (insn, 5, 6);
5472   unsigned int rt = bits (insn, 12, 15);
5473   unsigned int rn = bits (insn, 16, 19);
5474   unsigned int rm = bits (insn, 0, 3);
5475   char load[12]     = {0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1};
5476   char bytesize[12] = {2, 2, 2, 2, 8, 1, 8, 1, 8, 2, 8, 2};
5477   int immed = (op1 & 0x4) != 0;
5478   int opcode;
5479   ULONGEST rt_val, rt_val2 = 0, rn_val, rm_val = 0;
5480
5481   if (!insn_references_pc (insn, 0x000ff00ful))
5482     return arm_copy_unmodified (gdbarch, insn, "extra load/store", dsc);
5483
5484   if (debug_displaced)
5485     fprintf_unfiltered (gdb_stdlog, "displaced: copying %sextra load/store "
5486                         "insn %.8lx\n", unprivileged ? "unprivileged " : "",
5487                         (unsigned long) insn);
5488
5489   opcode = ((op2 << 2) | (op1 & 0x1) | ((op1 & 0x4) >> 1)) - 4;
5490
5491   if (opcode < 0)
5492     internal_error (__FILE__, __LINE__,
5493                     _("copy_extra_ld_st: instruction decode error"));
5494
5495   dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
5496   dsc->tmp[1] = displaced_read_reg (regs, dsc, 1);
5497   dsc->tmp[2] = displaced_read_reg (regs, dsc, 2);
5498   if (!immed)
5499     dsc->tmp[3] = displaced_read_reg (regs, dsc, 3);
5500
5501   rt_val = displaced_read_reg (regs, dsc, rt);
5502   if (bytesize[opcode] == 8)
5503     rt_val2 = displaced_read_reg (regs, dsc, rt + 1);
5504   rn_val = displaced_read_reg (regs, dsc, rn);
5505   if (!immed)
5506     rm_val = displaced_read_reg (regs, dsc, rm);
5507
5508   displaced_write_reg (regs, dsc, 0, rt_val, CANNOT_WRITE_PC);
5509   if (bytesize[opcode] == 8)
5510     displaced_write_reg (regs, dsc, 1, rt_val2, CANNOT_WRITE_PC);
5511   displaced_write_reg (regs, dsc, 2, rn_val, CANNOT_WRITE_PC);
5512   if (!immed)
5513     displaced_write_reg (regs, dsc, 3, rm_val, CANNOT_WRITE_PC);
5514
5515   dsc->rd = rt;
5516   dsc->u.ldst.xfersize = bytesize[opcode];
5517   dsc->u.ldst.rn = rn;
5518   dsc->u.ldst.immed = immed;
5519   dsc->u.ldst.writeback = bit (insn, 24) == 0 || bit (insn, 21) != 0;
5520   dsc->u.ldst.restore_r4 = 0;
5521
5522   if (immed)
5523     /* {ldr,str}<width><cond> rt, [rt2,] [rn, #imm]
5524         ->
5525        {ldr,str}<width><cond> r0, [r1,] [r2, #imm].  */
5526     dsc->modinsn[0] = (insn & 0xfff00fff) | 0x20000;
5527   else
5528     /* {ldr,str}<width><cond> rt, [rt2,] [rn, +/-rm]
5529         ->
5530        {ldr,str}<width><cond> r0, [r1,] [r2, +/-r3].  */
5531     dsc->modinsn[0] = (insn & 0xfff00ff0) | 0x20003;
5532
5533   dsc->cleanup = load[opcode] ? &cleanup_load : &cleanup_store;
5534
5535   return 0;
5536 }
5537
5538 /* Copy byte/half word/word loads and stores.  */
5539
5540 static void
5541 install_load_store (struct gdbarch *gdbarch, struct regcache *regs,
5542                     arm_displaced_step_closure *dsc, int load,
5543                     int immed, int writeback, int size, int usermode,
5544                     int rt, int rm, int rn)
5545 {
5546   ULONGEST rt_val, rn_val, rm_val = 0;
5547
5548   dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
5549   dsc->tmp[2] = displaced_read_reg (regs, dsc, 2);
5550   if (!immed)
5551     dsc->tmp[3] = displaced_read_reg (regs, dsc, 3);
5552   if (!load)
5553     dsc->tmp[4] = displaced_read_reg (regs, dsc, 4);
5554
5555   rt_val = displaced_read_reg (regs, dsc, rt);
5556   rn_val = displaced_read_reg (regs, dsc, rn);
5557   if (!immed)
5558     rm_val = displaced_read_reg (regs, dsc, rm);
5559
5560   displaced_write_reg (regs, dsc, 0, rt_val, CANNOT_WRITE_PC);
5561   displaced_write_reg (regs, dsc, 2, rn_val, CANNOT_WRITE_PC);
5562   if (!immed)
5563     displaced_write_reg (regs, dsc, 3, rm_val, CANNOT_WRITE_PC);
5564   dsc->rd = rt;
5565   dsc->u.ldst.xfersize = size;
5566   dsc->u.ldst.rn = rn;
5567   dsc->u.ldst.immed = immed;
5568   dsc->u.ldst.writeback = writeback;
5569
5570   /* To write PC we can do:
5571
5572      Before this sequence of instructions:
5573      r0 is the PC value got from displaced_read_reg, so r0 = from + 8;
5574      r2 is the Rn value got from dispalced_read_reg.
5575
5576      Insn1: push {pc} Write address of STR instruction + offset on stack
5577      Insn2: pop  {r4} Read it back from stack, r4 = addr(Insn1) + offset
5578      Insn3: sub r4, r4, pc   r4 = addr(Insn1) + offset - pc
5579                                 = addr(Insn1) + offset - addr(Insn3) - 8
5580                                 = offset - 16
5581      Insn4: add r4, r4, #8   r4 = offset - 8
5582      Insn5: add r0, r0, r4   r0 = from + 8 + offset - 8
5583                                 = from + offset
5584      Insn6: str r0, [r2, #imm] (or str r0, [r2, r3])
5585
5586      Otherwise we don't know what value to write for PC, since the offset is
5587      architecture-dependent (sometimes PC+8, sometimes PC+12).  More details
5588      of this can be found in Section "Saving from r15" in
5589      http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dui0204g/Cihbjifh.html */
5590
5591   dsc->cleanup = load ? &cleanup_load : &cleanup_store;
5592 }
5593
5594
5595 static int
5596 thumb2_copy_load_literal (struct gdbarch *gdbarch, uint16_t insn1,
5597                           uint16_t insn2, struct regcache *regs,
5598                           arm_displaced_step_closure *dsc, int size)
5599 {
5600   unsigned int u_bit = bit (insn1, 7);
5601   unsigned int rt = bits (insn2, 12, 15);
5602   int imm12 = bits (insn2, 0, 11);
5603   ULONGEST pc_val;
5604
5605   if (debug_displaced)
5606     fprintf_unfiltered (gdb_stdlog,
5607                         "displaced: copying ldr pc (0x%x) R%d %c imm12 %.4x\n",
5608                         (unsigned int) dsc->insn_addr, rt, u_bit ? '+' : '-',
5609                         imm12);
5610
5611   if (!u_bit)
5612     imm12 = -1 * imm12;
5613
5614   /* Rewrite instruction LDR Rt imm12 into:
5615
5616      Prepare: tmp[0] <- r0, tmp[1] <- r2, tmp[2] <- r3, r2 <- pc, r3 <- imm12
5617
5618      LDR R0, R2, R3,
5619
5620      Cleanup: rt <- r0, r0 <- tmp[0], r2 <- tmp[1], r3 <- tmp[2].  */
5621
5622
5623   dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
5624   dsc->tmp[2] = displaced_read_reg (regs, dsc, 2);
5625   dsc->tmp[3] = displaced_read_reg (regs, dsc, 3);
5626
5627   pc_val = displaced_read_reg (regs, dsc, ARM_PC_REGNUM);
5628
5629   pc_val = pc_val & 0xfffffffc;
5630
5631   displaced_write_reg (regs, dsc, 2, pc_val, CANNOT_WRITE_PC);
5632   displaced_write_reg (regs, dsc, 3, imm12, CANNOT_WRITE_PC);
5633
5634   dsc->rd = rt;
5635
5636   dsc->u.ldst.xfersize = size;
5637   dsc->u.ldst.immed = 0;
5638   dsc->u.ldst.writeback = 0;
5639   dsc->u.ldst.restore_r4 = 0;
5640
5641   /* LDR R0, R2, R3 */
5642   dsc->modinsn[0] = 0xf852;
5643   dsc->modinsn[1] = 0x3;
5644   dsc->numinsns = 2;
5645
5646   dsc->cleanup = &cleanup_load;
5647
5648   return 0;
5649 }
5650
5651 static int
5652 thumb2_copy_load_reg_imm (struct gdbarch *gdbarch, uint16_t insn1,
5653                           uint16_t insn2, struct regcache *regs,
5654                           arm_displaced_step_closure *dsc,
5655                           int writeback, int immed)
5656 {
5657   unsigned int rt = bits (insn2, 12, 15);
5658   unsigned int rn = bits (insn1, 0, 3);
5659   unsigned int rm = bits (insn2, 0, 3);  /* Only valid if !immed.  */
5660   /* In LDR (register), there is also a register Rm, which is not allowed to
5661      be PC, so we don't have to check it.  */
5662
5663   if (rt != ARM_PC_REGNUM && rn != ARM_PC_REGNUM)
5664     return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2, "load",
5665                                         dsc);
5666
5667   if (debug_displaced)
5668     fprintf_unfiltered (gdb_stdlog,
5669                         "displaced: copying ldr r%d [r%d] insn %.4x%.4x\n",
5670                          rt, rn, insn1, insn2);
5671
5672   install_load_store (gdbarch, regs, dsc, 1, immed, writeback, 4,
5673                       0, rt, rm, rn);
5674
5675   dsc->u.ldst.restore_r4 = 0;
5676
5677   if (immed)
5678     /* ldr[b]<cond> rt, [rn, #imm], etc.
5679        ->
5680        ldr[b]<cond> r0, [r2, #imm].  */
5681     {
5682       dsc->modinsn[0] = (insn1 & 0xfff0) | 0x2;
5683       dsc->modinsn[1] = insn2 & 0x0fff;
5684     }
5685   else
5686     /* ldr[b]<cond> rt, [rn, rm], etc.
5687        ->
5688        ldr[b]<cond> r0, [r2, r3].  */
5689     {
5690       dsc->modinsn[0] = (insn1 & 0xfff0) | 0x2;
5691       dsc->modinsn[1] = (insn2 & 0x0ff0) | 0x3;
5692     }
5693
5694   dsc->numinsns = 2;
5695
5696   return 0;
5697 }
5698
5699
5700 static int
5701 arm_copy_ldr_str_ldrb_strb (struct gdbarch *gdbarch, uint32_t insn,
5702                             struct regcache *regs,
5703                             arm_displaced_step_closure *dsc,
5704                             int load, int size, int usermode)
5705 {
5706   int immed = !bit (insn, 25);
5707   int writeback = (bit (insn, 24) == 0 || bit (insn, 21) != 0);
5708   unsigned int rt = bits (insn, 12, 15);
5709   unsigned int rn = bits (insn, 16, 19);
5710   unsigned int rm = bits (insn, 0, 3);  /* Only valid if !immed.  */
5711
5712   if (!insn_references_pc (insn, 0x000ff00ful))
5713     return arm_copy_unmodified (gdbarch, insn, "load/store", dsc);
5714
5715   if (debug_displaced)
5716     fprintf_unfiltered (gdb_stdlog,
5717                         "displaced: copying %s%s r%d [r%d] insn %.8lx\n",
5718                         load ? (size == 1 ? "ldrb" : "ldr")
5719                              : (size == 1 ? "strb" : "str"), usermode ? "t" : "",
5720                         rt, rn,
5721                         (unsigned long) insn);
5722
5723   install_load_store (gdbarch, regs, dsc, load, immed, writeback, size,
5724                       usermode, rt, rm, rn);
5725
5726   if (load || rt != ARM_PC_REGNUM)
5727     {
5728       dsc->u.ldst.restore_r4 = 0;
5729
5730       if (immed)
5731         /* {ldr,str}[b]<cond> rt, [rn, #imm], etc.
5732            ->
5733            {ldr,str}[b]<cond> r0, [r2, #imm].  */
5734         dsc->modinsn[0] = (insn & 0xfff00fff) | 0x20000;
5735       else
5736         /* {ldr,str}[b]<cond> rt, [rn, rm], etc.
5737            ->
5738            {ldr,str}[b]<cond> r0, [r2, r3].  */
5739         dsc->modinsn[0] = (insn & 0xfff00ff0) | 0x20003;
5740     }
5741   else
5742     {
5743       /* We need to use r4 as scratch.  Make sure it's restored afterwards.  */
5744       dsc->u.ldst.restore_r4 = 1;
5745       dsc->modinsn[0] = 0xe92d8000;  /* push {pc} */
5746       dsc->modinsn[1] = 0xe8bd0010;  /* pop  {r4} */
5747       dsc->modinsn[2] = 0xe044400f;  /* sub r4, r4, pc.  */
5748       dsc->modinsn[3] = 0xe2844008;  /* add r4, r4, #8.  */
5749       dsc->modinsn[4] = 0xe0800004;  /* add r0, r0, r4.  */
5750
5751       /* As above.  */
5752       if (immed)
5753         dsc->modinsn[5] = (insn & 0xfff00fff) | 0x20000;
5754       else
5755         dsc->modinsn[5] = (insn & 0xfff00ff0) | 0x20003;
5756
5757       dsc->numinsns = 6;
5758     }
5759
5760   dsc->cleanup = load ? &cleanup_load : &cleanup_store;
5761
5762   return 0;
5763 }
5764
5765 /* Cleanup LDM instructions with fully-populated register list.  This is an
5766    unfortunate corner case: it's impossible to implement correctly by modifying
5767    the instruction.  The issue is as follows: we have an instruction,
5768
5769    ldm rN, {r0-r15}
5770
5771    which we must rewrite to avoid loading PC.  A possible solution would be to
5772    do the load in two halves, something like (with suitable cleanup
5773    afterwards):
5774
5775    mov r8, rN
5776    ldm[id][ab] r8!, {r0-r7}
5777    str r7, <temp>
5778    ldm[id][ab] r8, {r7-r14}
5779    <bkpt>
5780
5781    but at present there's no suitable place for <temp>, since the scratch space
5782    is overwritten before the cleanup routine is called.  For now, we simply
5783    emulate the instruction.  */
5784
5785 static void
5786 cleanup_block_load_all (struct gdbarch *gdbarch, struct regcache *regs,
5787                         arm_displaced_step_closure *dsc)
5788 {
5789   int inc = dsc->u.block.increment;
5790   int bump_before = dsc->u.block.before ? (inc ? 4 : -4) : 0;
5791   int bump_after = dsc->u.block.before ? 0 : (inc ? 4 : -4);
5792   uint32_t regmask = dsc->u.block.regmask;
5793   int regno = inc ? 0 : 15;
5794   CORE_ADDR xfer_addr = dsc->u.block.xfer_addr;
5795   int exception_return = dsc->u.block.load && dsc->u.block.user
5796                          && (regmask & 0x8000) != 0;
5797   uint32_t status = displaced_read_reg (regs, dsc, ARM_PS_REGNUM);
5798   int do_transfer = condition_true (dsc->u.block.cond, status);
5799   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
5800
5801   if (!do_transfer)
5802     return;
5803
5804   /* If the instruction is ldm rN, {...pc}^, I don't think there's anything
5805      sensible we can do here.  Complain loudly.  */
5806   if (exception_return)
5807     error (_("Cannot single-step exception return"));
5808
5809   /* We don't handle any stores here for now.  */
5810   gdb_assert (dsc->u.block.load != 0);
5811
5812   if (debug_displaced)
5813     fprintf_unfiltered (gdb_stdlog, "displaced: emulating block transfer: "
5814                         "%s %s %s\n", dsc->u.block.load ? "ldm" : "stm",
5815                         dsc->u.block.increment ? "inc" : "dec",
5816                         dsc->u.block.before ? "before" : "after");
5817
5818   while (regmask)
5819     {
5820       uint32_t memword;
5821
5822       if (inc)
5823         while (regno <= ARM_PC_REGNUM && (regmask & (1 << regno)) == 0)
5824           regno++;
5825       else
5826         while (regno >= 0 && (regmask & (1 << regno)) == 0)
5827           regno--;
5828
5829       xfer_addr += bump_before;
5830
5831       memword = read_memory_unsigned_integer (xfer_addr, 4, byte_order);
5832       displaced_write_reg (regs, dsc, regno, memword, LOAD_WRITE_PC);
5833
5834       xfer_addr += bump_after;
5835
5836       regmask &= ~(1 << regno);
5837     }
5838
5839   if (dsc->u.block.writeback)
5840     displaced_write_reg (regs, dsc, dsc->u.block.rn, xfer_addr,
5841                          CANNOT_WRITE_PC);
5842 }
5843
5844 /* Clean up an STM which included the PC in the register list.  */
5845
5846 static void
5847 cleanup_block_store_pc (struct gdbarch *gdbarch, struct regcache *regs,
5848                         arm_displaced_step_closure *dsc)
5849 {
5850   uint32_t status = displaced_read_reg (regs, dsc, ARM_PS_REGNUM);
5851   int store_executed = condition_true (dsc->u.block.cond, status);
5852   CORE_ADDR pc_stored_at, transferred_regs = bitcount (dsc->u.block.regmask);
5853   CORE_ADDR stm_insn_addr;
5854   uint32_t pc_val;
5855   long offset;
5856   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
5857
5858   /* If condition code fails, there's nothing else to do.  */
5859   if (!store_executed)
5860     return;
5861
5862   if (dsc->u.block.increment)
5863     {
5864       pc_stored_at = dsc->u.block.xfer_addr + 4 * transferred_regs;
5865
5866       if (dsc->u.block.before)
5867          pc_stored_at += 4;
5868     }
5869   else
5870     {
5871       pc_stored_at = dsc->u.block.xfer_addr;
5872
5873       if (dsc->u.block.before)
5874          pc_stored_at -= 4;
5875     }
5876
5877   pc_val = read_memory_unsigned_integer (pc_stored_at, 4, byte_order);
5878   stm_insn_addr = dsc->scratch_base;
5879   offset = pc_val - stm_insn_addr;
5880
5881   if (debug_displaced)
5882     fprintf_unfiltered (gdb_stdlog, "displaced: detected PC offset %.8lx for "
5883                         "STM instruction\n", offset);
5884
5885   /* Rewrite the stored PC to the proper value for the non-displaced original
5886      instruction.  */
5887   write_memory_unsigned_integer (pc_stored_at, 4, byte_order,
5888                                  dsc->insn_addr + offset);
5889 }
5890
5891 /* Clean up an LDM which includes the PC in the register list.  We clumped all
5892    the registers in the transferred list into a contiguous range r0...rX (to
5893    avoid loading PC directly and losing control of the debugged program), so we
5894    must undo that here.  */
5895
5896 static void
5897 cleanup_block_load_pc (struct gdbarch *gdbarch,
5898                        struct regcache *regs,
5899                        arm_displaced_step_closure *dsc)
5900 {
5901   uint32_t status = displaced_read_reg (regs, dsc, ARM_PS_REGNUM);
5902   int load_executed = condition_true (dsc->u.block.cond, status);
5903   unsigned int mask = dsc->u.block.regmask, write_reg = ARM_PC_REGNUM;
5904   unsigned int regs_loaded = bitcount (mask);
5905   unsigned int num_to_shuffle = regs_loaded, clobbered;
5906
5907   /* The method employed here will fail if the register list is fully populated
5908      (we need to avoid loading PC directly).  */
5909   gdb_assert (num_to_shuffle < 16);
5910
5911   if (!load_executed)
5912     return;
5913
5914   clobbered = (1 << num_to_shuffle) - 1;
5915
5916   while (num_to_shuffle > 0)
5917     {
5918       if ((mask & (1 << write_reg)) != 0)
5919         {
5920           unsigned int read_reg = num_to_shuffle - 1;
5921
5922           if (read_reg != write_reg)
5923             {
5924               ULONGEST rval = displaced_read_reg (regs, dsc, read_reg);
5925               displaced_write_reg (regs, dsc, write_reg, rval, LOAD_WRITE_PC);
5926               if (debug_displaced)
5927                 fprintf_unfiltered (gdb_stdlog, _("displaced: LDM: move "
5928                                     "loaded register r%d to r%d\n"), read_reg,
5929                                     write_reg);
5930             }
5931           else if (debug_displaced)
5932             fprintf_unfiltered (gdb_stdlog, _("displaced: LDM: register "
5933                                 "r%d already in the right place\n"),
5934                                 write_reg);
5935
5936           clobbered &= ~(1 << write_reg);
5937
5938           num_to_shuffle--;
5939         }
5940
5941       write_reg--;
5942     }
5943
5944   /* Restore any registers we scribbled over.  */
5945   for (write_reg = 0; clobbered != 0; write_reg++)
5946     {
5947       if ((clobbered & (1 << write_reg)) != 0)
5948         {
5949           displaced_write_reg (regs, dsc, write_reg, dsc->tmp[write_reg],
5950                                CANNOT_WRITE_PC);
5951           if (debug_displaced)
5952             fprintf_unfiltered (gdb_stdlog, _("displaced: LDM: restored "
5953                                 "clobbered register r%d\n"), write_reg);
5954           clobbered &= ~(1 << write_reg);
5955         }
5956     }
5957
5958   /* Perform register writeback manually.  */
5959   if (dsc->u.block.writeback)
5960     {
5961       ULONGEST new_rn_val = dsc->u.block.xfer_addr;
5962
5963       if (dsc->u.block.increment)
5964         new_rn_val += regs_loaded * 4;
5965       else
5966         new_rn_val -= regs_loaded * 4;
5967
5968       displaced_write_reg (regs, dsc, dsc->u.block.rn, new_rn_val,
5969                            CANNOT_WRITE_PC);
5970     }
5971 }
5972
5973 /* Handle ldm/stm, apart from some tricky cases which are unlikely to occur
5974    in user-level code (in particular exception return, ldm rn, {...pc}^).  */
5975
5976 static int
5977 arm_copy_block_xfer (struct gdbarch *gdbarch, uint32_t insn,
5978                      struct regcache *regs,
5979                      arm_displaced_step_closure *dsc)
5980 {
5981   int load = bit (insn, 20);
5982   int user = bit (insn, 22);
5983   int increment = bit (insn, 23);
5984   int before = bit (insn, 24);
5985   int writeback = bit (insn, 21);
5986   int rn = bits (insn, 16, 19);
5987
5988   /* Block transfers which don't mention PC can be run directly
5989      out-of-line.  */
5990   if (rn != ARM_PC_REGNUM && (insn & 0x8000) == 0)
5991     return arm_copy_unmodified (gdbarch, insn, "ldm/stm", dsc);
5992
5993   if (rn == ARM_PC_REGNUM)
5994     {
5995       warning (_("displaced: Unpredictable LDM or STM with "
5996                  "base register r15"));
5997       return arm_copy_unmodified (gdbarch, insn, "unpredictable ldm/stm", dsc);
5998     }
5999
6000   if (debug_displaced)
6001     fprintf_unfiltered (gdb_stdlog, "displaced: copying block transfer insn "
6002                         "%.8lx\n", (unsigned long) insn);
6003
6004   dsc->u.block.xfer_addr = displaced_read_reg (regs, dsc, rn);
6005   dsc->u.block.rn = rn;
6006
6007   dsc->u.block.load = load;
6008   dsc->u.block.user = user;
6009   dsc->u.block.increment = increment;
6010   dsc->u.block.before = before;
6011   dsc->u.block.writeback = writeback;
6012   dsc->u.block.cond = bits (insn, 28, 31);
6013
6014   dsc->u.block.regmask = insn & 0xffff;
6015
6016   if (load)
6017     {
6018       if ((insn & 0xffff) == 0xffff)
6019         {
6020           /* LDM with a fully-populated register list.  This case is
6021              particularly tricky.  Implement for now by fully emulating the
6022              instruction (which might not behave perfectly in all cases, but
6023              these instructions should be rare enough for that not to matter
6024              too much).  */
6025           dsc->modinsn[0] = ARM_NOP;
6026
6027           dsc->cleanup = &cleanup_block_load_all;
6028         }
6029       else
6030         {
6031           /* LDM of a list of registers which includes PC.  Implement by
6032              rewriting the list of registers to be transferred into a
6033              contiguous chunk r0...rX before doing the transfer, then shuffling
6034              registers into the correct places in the cleanup routine.  */
6035           unsigned int regmask = insn & 0xffff;
6036           unsigned int num_in_list = bitcount (regmask), new_regmask;
6037           unsigned int i;
6038
6039           for (i = 0; i < num_in_list; i++)
6040             dsc->tmp[i] = displaced_read_reg (regs, dsc, i);
6041
6042           /* Writeback makes things complicated.  We need to avoid clobbering
6043              the base register with one of the registers in our modified
6044              register list, but just using a different register can't work in
6045              all cases, e.g.:
6046
6047                ldm r14!, {r0-r13,pc}
6048
6049              which would need to be rewritten as:
6050
6051                ldm rN!, {r0-r14}
6052
6053              but that can't work, because there's no free register for N.
6054
6055              Solve this by turning off the writeback bit, and emulating
6056              writeback manually in the cleanup routine.  */
6057
6058           if (writeback)
6059             insn &= ~(1 << 21);
6060
6061           new_regmask = (1 << num_in_list) - 1;
6062
6063           if (debug_displaced)
6064             fprintf_unfiltered (gdb_stdlog, _("displaced: LDM r%d%s, "
6065                                 "{..., pc}: original reg list %.4x, modified "
6066                                 "list %.4x\n"), rn, writeback ? "!" : "",
6067                                 (int) insn & 0xffff, new_regmask);
6068
6069           dsc->modinsn[0] = (insn & ~0xffff) | (new_regmask & 0xffff);
6070
6071           dsc->cleanup = &cleanup_block_load_pc;
6072         }
6073     }
6074   else
6075     {
6076       /* STM of a list of registers which includes PC.  Run the instruction
6077          as-is, but out of line: this will store the wrong value for the PC,
6078          so we must manually fix up the memory in the cleanup routine.
6079          Doing things this way has the advantage that we can auto-detect
6080          the offset of the PC write (which is architecture-dependent) in
6081          the cleanup routine.  */
6082       dsc->modinsn[0] = insn;
6083
6084       dsc->cleanup = &cleanup_block_store_pc;
6085     }
6086
6087   return 0;
6088 }
6089
6090 static int
6091 thumb2_copy_block_xfer (struct gdbarch *gdbarch, uint16_t insn1, uint16_t insn2,
6092                         struct regcache *regs,
6093                         arm_displaced_step_closure *dsc)
6094 {
6095   int rn = bits (insn1, 0, 3);
6096   int load = bit (insn1, 4);
6097   int writeback = bit (insn1, 5);
6098
6099   /* Block transfers which don't mention PC can be run directly
6100      out-of-line.  */
6101   if (rn != ARM_PC_REGNUM && (insn2 & 0x8000) == 0)
6102     return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2, "ldm/stm", dsc);
6103
6104   if (rn == ARM_PC_REGNUM)
6105     {
6106       warning (_("displaced: Unpredictable LDM or STM with "
6107                  "base register r15"));
6108       return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
6109                                           "unpredictable ldm/stm", dsc);
6110     }
6111
6112   if (debug_displaced)
6113     fprintf_unfiltered (gdb_stdlog, "displaced: copying block transfer insn "
6114                         "%.4x%.4x\n", insn1, insn2);
6115
6116   /* Clear bit 13, since it should be always zero.  */
6117   dsc->u.block.regmask = (insn2 & 0xdfff);
6118   dsc->u.block.rn = rn;
6119
6120   dsc->u.block.load = load;
6121   dsc->u.block.user = 0;
6122   dsc->u.block.increment = bit (insn1, 7);
6123   dsc->u.block.before = bit (insn1, 8);
6124   dsc->u.block.writeback = writeback;
6125   dsc->u.block.cond = INST_AL;
6126   dsc->u.block.xfer_addr = displaced_read_reg (regs, dsc, rn);
6127
6128   if (load)
6129     {
6130       if (dsc->u.block.regmask == 0xffff)
6131         {
6132           /* This branch is impossible to happen.  */
6133           gdb_assert (0);
6134         }
6135       else
6136         {
6137           unsigned int regmask = dsc->u.block.regmask;
6138           unsigned int num_in_list = bitcount (regmask), new_regmask;
6139           unsigned int i;
6140
6141           for (i = 0; i < num_in_list; i++)
6142             dsc->tmp[i] = displaced_read_reg (regs, dsc, i);
6143
6144           if (writeback)
6145             insn1 &= ~(1 << 5);
6146
6147           new_regmask = (1 << num_in_list) - 1;
6148
6149           if (debug_displaced)
6150             fprintf_unfiltered (gdb_stdlog, _("displaced: LDM r%d%s, "
6151                                 "{..., pc}: original reg list %.4x, modified "
6152                                 "list %.4x\n"), rn, writeback ? "!" : "",
6153                                 (int) dsc->u.block.regmask, new_regmask);
6154
6155           dsc->modinsn[0] = insn1;
6156           dsc->modinsn[1] = (new_regmask & 0xffff);
6157           dsc->numinsns = 2;
6158
6159           dsc->cleanup = &cleanup_block_load_pc;
6160         }
6161     }
6162   else
6163     {
6164       dsc->modinsn[0] = insn1;
6165       dsc->modinsn[1] = insn2;
6166       dsc->numinsns = 2;
6167       dsc->cleanup = &cleanup_block_store_pc;
6168     }
6169   return 0;
6170 }
6171
6172 /* Wrapper over read_memory_unsigned_integer for use in arm_get_next_pcs.
6173  This is used to avoid a dependency on BFD's bfd_endian enum.  */
6174
6175 ULONGEST
6176 arm_get_next_pcs_read_memory_unsigned_integer (CORE_ADDR memaddr, int len,
6177                                                int byte_order)
6178 {
6179   return read_memory_unsigned_integer (memaddr, len,
6180                                        (enum bfd_endian) byte_order);
6181 }
6182
6183 /* Wrapper over gdbarch_addr_bits_remove for use in arm_get_next_pcs.  */
6184
6185 CORE_ADDR
6186 arm_get_next_pcs_addr_bits_remove (struct arm_get_next_pcs *self,
6187                                    CORE_ADDR val)
6188 {
6189   return gdbarch_addr_bits_remove (self->regcache->arch (), val);
6190 }
6191
6192 /* Wrapper over syscall_next_pc for use in get_next_pcs.  */
6193
6194 static CORE_ADDR
6195 arm_get_next_pcs_syscall_next_pc (struct arm_get_next_pcs *self)
6196 {
6197   return 0;
6198 }
6199
6200 /* Wrapper over arm_is_thumb for use in arm_get_next_pcs.  */
6201
6202 int
6203 arm_get_next_pcs_is_thumb (struct arm_get_next_pcs *self)
6204 {
6205   return arm_is_thumb (self->regcache);
6206 }
6207
6208 /* single_step() is called just before we want to resume the inferior,
6209    if we want to single-step it but there is no hardware or kernel
6210    single-step support.  We find the target of the coming instructions
6211    and breakpoint them.  */
6212
6213 std::vector<CORE_ADDR>
6214 arm_software_single_step (struct regcache *regcache)
6215 {
6216   struct gdbarch *gdbarch = regcache->arch ();
6217   struct arm_get_next_pcs next_pcs_ctx;
6218
6219   arm_get_next_pcs_ctor (&next_pcs_ctx,
6220                          &arm_get_next_pcs_ops,
6221                          gdbarch_byte_order (gdbarch),
6222                          gdbarch_byte_order_for_code (gdbarch),
6223                          0,
6224                          regcache);
6225
6226   std::vector<CORE_ADDR> next_pcs = arm_get_next_pcs (&next_pcs_ctx);
6227
6228   for (CORE_ADDR &pc_ref : next_pcs)
6229     pc_ref = gdbarch_addr_bits_remove (gdbarch, pc_ref);
6230
6231   return next_pcs;
6232 }
6233
6234 /* Cleanup/copy SVC (SWI) instructions.  These two functions are overridden
6235    for Linux, where some SVC instructions must be treated specially.  */
6236
6237 static void
6238 cleanup_svc (struct gdbarch *gdbarch, struct regcache *regs,
6239              arm_displaced_step_closure *dsc)
6240 {
6241   CORE_ADDR resume_addr = dsc->insn_addr + dsc->insn_size;
6242
6243   if (debug_displaced)
6244     fprintf_unfiltered (gdb_stdlog, "displaced: cleanup for svc, resume at "
6245                         "%.8lx\n", (unsigned long) resume_addr);
6246
6247   displaced_write_reg (regs, dsc, ARM_PC_REGNUM, resume_addr, BRANCH_WRITE_PC);
6248 }
6249
6250
6251 /* Common copy routine for svc instruciton.  */
6252
6253 static int
6254 install_svc (struct gdbarch *gdbarch, struct regcache *regs,
6255              arm_displaced_step_closure *dsc)
6256 {
6257   /* Preparation: none.
6258      Insn: unmodified svc.
6259      Cleanup: pc <- insn_addr + insn_size.  */
6260
6261   /* Pretend we wrote to the PC, so cleanup doesn't set PC to the next
6262      instruction.  */
6263   dsc->wrote_to_pc = 1;
6264
6265   /* Allow OS-specific code to override SVC handling.  */
6266   if (dsc->u.svc.copy_svc_os)
6267     return dsc->u.svc.copy_svc_os (gdbarch, regs, dsc);
6268   else
6269     {
6270       dsc->cleanup = &cleanup_svc;
6271       return 0;
6272     }
6273 }
6274
6275 static int
6276 arm_copy_svc (struct gdbarch *gdbarch, uint32_t insn,
6277               struct regcache *regs, arm_displaced_step_closure *dsc)
6278 {
6279
6280   if (debug_displaced)
6281     fprintf_unfiltered (gdb_stdlog, "displaced: copying svc insn %.8lx\n",
6282                         (unsigned long) insn);
6283
6284   dsc->modinsn[0] = insn;
6285
6286   return install_svc (gdbarch, regs, dsc);
6287 }
6288
6289 static int
6290 thumb_copy_svc (struct gdbarch *gdbarch, uint16_t insn,
6291                 struct regcache *regs, arm_displaced_step_closure *dsc)
6292 {
6293
6294   if (debug_displaced)
6295     fprintf_unfiltered (gdb_stdlog, "displaced: copying svc insn %.4x\n",
6296                         insn);
6297
6298   dsc->modinsn[0] = insn;
6299
6300   return install_svc (gdbarch, regs, dsc);
6301 }
6302
6303 /* Copy undefined instructions.  */
6304
6305 static int
6306 arm_copy_undef (struct gdbarch *gdbarch, uint32_t insn,
6307                 arm_displaced_step_closure *dsc)
6308 {
6309   if (debug_displaced)
6310     fprintf_unfiltered (gdb_stdlog,
6311                         "displaced: copying undefined insn %.8lx\n",
6312                         (unsigned long) insn);
6313
6314   dsc->modinsn[0] = insn;
6315
6316   return 0;
6317 }
6318
6319 static int
6320 thumb_32bit_copy_undef (struct gdbarch *gdbarch, uint16_t insn1, uint16_t insn2,
6321                        arm_displaced_step_closure *dsc)
6322 {
6323
6324   if (debug_displaced)
6325     fprintf_unfiltered (gdb_stdlog, "displaced: copying undefined insn "
6326                        "%.4x %.4x\n", (unsigned short) insn1,
6327                        (unsigned short) insn2);
6328
6329   dsc->modinsn[0] = insn1;
6330   dsc->modinsn[1] = insn2;
6331   dsc->numinsns = 2;
6332
6333   return 0;
6334 }
6335
6336 /* Copy unpredictable instructions.  */
6337
6338 static int
6339 arm_copy_unpred (struct gdbarch *gdbarch, uint32_t insn,
6340                  arm_displaced_step_closure *dsc)
6341 {
6342   if (debug_displaced)
6343     fprintf_unfiltered (gdb_stdlog, "displaced: copying unpredictable insn "
6344                         "%.8lx\n", (unsigned long) insn);
6345
6346   dsc->modinsn[0] = insn;
6347
6348   return 0;
6349 }
6350
6351 /* The decode_* functions are instruction decoding helpers.  They mostly follow
6352    the presentation in the ARM ARM.  */
6353
6354 static int
6355 arm_decode_misc_memhint_neon (struct gdbarch *gdbarch, uint32_t insn,
6356                               struct regcache *regs,
6357                               arm_displaced_step_closure *dsc)
6358 {
6359   unsigned int op1 = bits (insn, 20, 26), op2 = bits (insn, 4, 7);
6360   unsigned int rn = bits (insn, 16, 19);
6361
6362   if (op1 == 0x10 && (op2 & 0x2) == 0x0 && (rn & 0x1) == 0x0)
6363     return arm_copy_unmodified (gdbarch, insn, "cps", dsc);
6364   else if (op1 == 0x10 && op2 == 0x0 && (rn & 0x1) == 0x1)
6365     return arm_copy_unmodified (gdbarch, insn, "setend", dsc);
6366   else if ((op1 & 0x60) == 0x20)
6367     return arm_copy_unmodified (gdbarch, insn, "neon dataproc", dsc);
6368   else if ((op1 & 0x71) == 0x40)
6369     return arm_copy_unmodified (gdbarch, insn, "neon elt/struct load/store",
6370                                 dsc);
6371   else if ((op1 & 0x77) == 0x41)
6372     return arm_copy_unmodified (gdbarch, insn, "unallocated mem hint", dsc);
6373   else if ((op1 & 0x77) == 0x45)
6374     return arm_copy_preload (gdbarch, insn, regs, dsc);  /* pli.  */
6375   else if ((op1 & 0x77) == 0x51)
6376     {
6377       if (rn != 0xf)
6378         return arm_copy_preload (gdbarch, insn, regs, dsc);  /* pld/pldw.  */
6379       else
6380         return arm_copy_unpred (gdbarch, insn, dsc);
6381     }
6382   else if ((op1 & 0x77) == 0x55)
6383     return arm_copy_preload (gdbarch, insn, regs, dsc);  /* pld/pldw.  */
6384   else if (op1 == 0x57)
6385     switch (op2)
6386       {
6387       case 0x1: return arm_copy_unmodified (gdbarch, insn, "clrex", dsc);
6388       case 0x4: return arm_copy_unmodified (gdbarch, insn, "dsb", dsc);
6389       case 0x5: return arm_copy_unmodified (gdbarch, insn, "dmb", dsc);
6390       case 0x6: return arm_copy_unmodified (gdbarch, insn, "isb", dsc);
6391       default: return arm_copy_unpred (gdbarch, insn, dsc);
6392       }
6393   else if ((op1 & 0x63) == 0x43)
6394     return arm_copy_unpred (gdbarch, insn, dsc);
6395   else if ((op2 & 0x1) == 0x0)
6396     switch (op1 & ~0x80)
6397       {
6398       case 0x61:
6399         return arm_copy_unmodified (gdbarch, insn, "unallocated mem hint", dsc);
6400       case 0x65:
6401         return arm_copy_preload_reg (gdbarch, insn, regs, dsc);  /* pli reg.  */
6402       case 0x71: case 0x75:
6403         /* pld/pldw reg.  */
6404         return arm_copy_preload_reg (gdbarch, insn, regs, dsc);
6405       case 0x63: case 0x67: case 0x73: case 0x77:
6406         return arm_copy_unpred (gdbarch, insn, dsc);
6407       default:
6408         return arm_copy_undef (gdbarch, insn, dsc);
6409       }
6410   else
6411     return arm_copy_undef (gdbarch, insn, dsc);  /* Probably unreachable.  */
6412 }
6413
6414 static int
6415 arm_decode_unconditional (struct gdbarch *gdbarch, uint32_t insn,
6416                           struct regcache *regs,
6417                           arm_displaced_step_closure *dsc)
6418 {
6419   if (bit (insn, 27) == 0)
6420     return arm_decode_misc_memhint_neon (gdbarch, insn, regs, dsc);
6421   /* Switch on bits: 0bxxxxx321xxx0xxxxxxxxxxxxxxxxxxxx.  */
6422   else switch (((insn & 0x7000000) >> 23) | ((insn & 0x100000) >> 20))
6423     {
6424     case 0x0: case 0x2:
6425       return arm_copy_unmodified (gdbarch, insn, "srs", dsc);
6426
6427     case 0x1: case 0x3:
6428       return arm_copy_unmodified (gdbarch, insn, "rfe", dsc);
6429
6430     case 0x4: case 0x5: case 0x6: case 0x7:
6431       return arm_copy_b_bl_blx (gdbarch, insn, regs, dsc);
6432
6433     case 0x8:
6434       switch ((insn & 0xe00000) >> 21)
6435         {
6436         case 0x1: case 0x3: case 0x4: case 0x5: case 0x6: case 0x7:
6437           /* stc/stc2.  */
6438           return arm_copy_copro_load_store (gdbarch, insn, regs, dsc);
6439
6440         case 0x2:
6441           return arm_copy_unmodified (gdbarch, insn, "mcrr/mcrr2", dsc);
6442
6443         default:
6444           return arm_copy_undef (gdbarch, insn, dsc);
6445         }
6446
6447     case 0x9:
6448       {
6449          int rn_f = (bits (insn, 16, 19) == 0xf);
6450         switch ((insn & 0xe00000) >> 21)
6451           {
6452           case 0x1: case 0x3:
6453             /* ldc/ldc2 imm (undefined for rn == pc).  */
6454             return rn_f ? arm_copy_undef (gdbarch, insn, dsc)
6455                         : arm_copy_copro_load_store (gdbarch, insn, regs, dsc);
6456
6457           case 0x2:
6458             return arm_copy_unmodified (gdbarch, insn, "mrrc/mrrc2", dsc);
6459
6460           case 0x4: case 0x5: case 0x6: case 0x7:
6461             /* ldc/ldc2 lit (undefined for rn != pc).  */
6462             return rn_f ? arm_copy_copro_load_store (gdbarch, insn, regs, dsc)
6463                         : arm_copy_undef (gdbarch, insn, dsc);
6464
6465           default:
6466             return arm_copy_undef (gdbarch, insn, dsc);
6467           }
6468       }
6469
6470     case 0xa:
6471       return arm_copy_unmodified (gdbarch, insn, "stc/stc2", dsc);
6472
6473     case 0xb:
6474       if (bits (insn, 16, 19) == 0xf)
6475         /* ldc/ldc2 lit.  */
6476         return arm_copy_copro_load_store (gdbarch, insn, regs, dsc);
6477       else
6478         return arm_copy_undef (gdbarch, insn, dsc);
6479
6480     case 0xc:
6481       if (bit (insn, 4))
6482         return arm_copy_unmodified (gdbarch, insn, "mcr/mcr2", dsc);
6483       else
6484         return arm_copy_unmodified (gdbarch, insn, "cdp/cdp2", dsc);
6485
6486     case 0xd:
6487       if (bit (insn, 4))
6488         return arm_copy_unmodified (gdbarch, insn, "mrc/mrc2", dsc);
6489       else
6490         return arm_copy_unmodified (gdbarch, insn, "cdp/cdp2", dsc);
6491
6492     default:
6493       return arm_copy_undef (gdbarch, insn, dsc);
6494     }
6495 }
6496
6497 /* Decode miscellaneous instructions in dp/misc encoding space.  */
6498
6499 static int
6500 arm_decode_miscellaneous (struct gdbarch *gdbarch, uint32_t insn,
6501                           struct regcache *regs,
6502                           arm_displaced_step_closure *dsc)
6503 {
6504   unsigned int op2 = bits (insn, 4, 6);
6505   unsigned int op = bits (insn, 21, 22);
6506
6507   switch (op2)
6508     {
6509     case 0x0:
6510       return arm_copy_unmodified (gdbarch, insn, "mrs/msr", dsc);
6511
6512     case 0x1:
6513       if (op == 0x1)  /* bx.  */
6514         return arm_copy_bx_blx_reg (gdbarch, insn, regs, dsc);
6515       else if (op == 0x3)
6516         return arm_copy_unmodified (gdbarch, insn, "clz", dsc);
6517       else
6518         return arm_copy_undef (gdbarch, insn, dsc);
6519
6520     case 0x2:
6521       if (op == 0x1)
6522         /* Not really supported.  */
6523         return arm_copy_unmodified (gdbarch, insn, "bxj", dsc);
6524       else
6525         return arm_copy_undef (gdbarch, insn, dsc);
6526
6527     case 0x3:
6528       if (op == 0x1)
6529         return arm_copy_bx_blx_reg (gdbarch, insn,
6530                                 regs, dsc);  /* blx register.  */
6531       else
6532         return arm_copy_undef (gdbarch, insn, dsc);
6533
6534     case 0x5:
6535       return arm_copy_unmodified (gdbarch, insn, "saturating add/sub", dsc);
6536
6537     case 0x7:
6538       if (op == 0x1)
6539         return arm_copy_unmodified (gdbarch, insn, "bkpt", dsc);
6540       else if (op == 0x3)
6541         /* Not really supported.  */
6542         return arm_copy_unmodified (gdbarch, insn, "smc", dsc);
6543       /* Fall through.  */
6544
6545     default:
6546       return arm_copy_undef (gdbarch, insn, dsc);
6547     }
6548 }
6549
6550 static int
6551 arm_decode_dp_misc (struct gdbarch *gdbarch, uint32_t insn,
6552                     struct regcache *regs,
6553                     arm_displaced_step_closure *dsc)
6554 {
6555   if (bit (insn, 25))
6556     switch (bits (insn, 20, 24))
6557       {
6558       case 0x10:
6559         return arm_copy_unmodified (gdbarch, insn, "movw", dsc);
6560
6561       case 0x14:
6562         return arm_copy_unmodified (gdbarch, insn, "movt", dsc);
6563
6564       case 0x12: case 0x16:
6565         return arm_copy_unmodified (gdbarch, insn, "msr imm", dsc);
6566
6567       default:
6568         return arm_copy_alu_imm (gdbarch, insn, regs, dsc);
6569       }
6570   else
6571     {
6572       uint32_t op1 = bits (insn, 20, 24), op2 = bits (insn, 4, 7);
6573
6574       if ((op1 & 0x19) != 0x10 && (op2 & 0x1) == 0x0)
6575         return arm_copy_alu_reg (gdbarch, insn, regs, dsc);
6576       else if ((op1 & 0x19) != 0x10 && (op2 & 0x9) == 0x1)
6577         return arm_copy_alu_shifted_reg (gdbarch, insn, regs, dsc);
6578       else if ((op1 & 0x19) == 0x10 && (op2 & 0x8) == 0x0)
6579         return arm_decode_miscellaneous (gdbarch, insn, regs, dsc);
6580       else if ((op1 & 0x19) == 0x10 && (op2 & 0x9) == 0x8)
6581         return arm_copy_unmodified (gdbarch, insn, "halfword mul/mla", dsc);
6582       else if ((op1 & 0x10) == 0x00 && op2 == 0x9)
6583         return arm_copy_unmodified (gdbarch, insn, "mul/mla", dsc);
6584       else if ((op1 & 0x10) == 0x10 && op2 == 0x9)
6585         return arm_copy_unmodified (gdbarch, insn, "synch", dsc);
6586       else if (op2 == 0xb || (op2 & 0xd) == 0xd)
6587         /* 2nd arg means "unprivileged".  */
6588         return arm_copy_extra_ld_st (gdbarch, insn, (op1 & 0x12) == 0x02, regs,
6589                                      dsc);
6590     }
6591
6592   /* Should be unreachable.  */
6593   return 1;
6594 }
6595
6596 static int
6597 arm_decode_ld_st_word_ubyte (struct gdbarch *gdbarch, uint32_t insn,
6598                              struct regcache *regs,
6599                              arm_displaced_step_closure *dsc)
6600 {
6601   int a = bit (insn, 25), b = bit (insn, 4);
6602   uint32_t op1 = bits (insn, 20, 24);
6603
6604   if ((!a && (op1 & 0x05) == 0x00 && (op1 & 0x17) != 0x02)
6605       || (a && (op1 & 0x05) == 0x00 && (op1 & 0x17) != 0x02 && !b))
6606     return arm_copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 0, 4, 0);
6607   else if ((!a && (op1 & 0x17) == 0x02)
6608             || (a && (op1 & 0x17) == 0x02 && !b))
6609     return arm_copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 0, 4, 1);
6610   else if ((!a && (op1 & 0x05) == 0x01 && (op1 & 0x17) != 0x03)
6611             || (a && (op1 & 0x05) == 0x01 && (op1 & 0x17) != 0x03 && !b))
6612     return arm_copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 1, 4, 0);
6613   else if ((!a && (op1 & 0x17) == 0x03)
6614            || (a && (op1 & 0x17) == 0x03 && !b))
6615     return arm_copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 1, 4, 1);
6616   else if ((!a && (op1 & 0x05) == 0x04 && (op1 & 0x17) != 0x06)
6617             || (a && (op1 & 0x05) == 0x04 && (op1 & 0x17) != 0x06 && !b))
6618     return arm_copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 0, 1, 0);
6619   else if ((!a && (op1 & 0x17) == 0x06)
6620            || (a && (op1 & 0x17) == 0x06 && !b))
6621     return arm_copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 0, 1, 1);
6622   else if ((!a && (op1 & 0x05) == 0x05 && (op1 & 0x17) != 0x07)
6623            || (a && (op1 & 0x05) == 0x05 && (op1 & 0x17) != 0x07 && !b))
6624     return arm_copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 1, 1, 0);
6625   else if ((!a && (op1 & 0x17) == 0x07)
6626            || (a && (op1 & 0x17) == 0x07 && !b))
6627     return arm_copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 1, 1, 1);
6628
6629   /* Should be unreachable.  */
6630   return 1;
6631 }
6632
6633 static int
6634 arm_decode_media (struct gdbarch *gdbarch, uint32_t insn,
6635                   arm_displaced_step_closure *dsc)
6636 {
6637   switch (bits (insn, 20, 24))
6638     {
6639     case 0x00: case 0x01: case 0x02: case 0x03:
6640       return arm_copy_unmodified (gdbarch, insn, "parallel add/sub signed", dsc);
6641
6642     case 0x04: case 0x05: case 0x06: case 0x07:
6643       return arm_copy_unmodified (gdbarch, insn, "parallel add/sub unsigned", dsc);
6644
6645     case 0x08: case 0x09: case 0x0a: case 0x0b:
6646     case 0x0c: case 0x0d: case 0x0e: case 0x0f:
6647       return arm_copy_unmodified (gdbarch, insn,
6648                               "decode/pack/unpack/saturate/reverse", dsc);
6649
6650     case 0x18:
6651       if (bits (insn, 5, 7) == 0)  /* op2.  */
6652          {
6653           if (bits (insn, 12, 15) == 0xf)
6654             return arm_copy_unmodified (gdbarch, insn, "usad8", dsc);
6655           else
6656             return arm_copy_unmodified (gdbarch, insn, "usada8", dsc);
6657         }
6658       else
6659          return arm_copy_undef (gdbarch, insn, dsc);
6660
6661     case 0x1a: case 0x1b:
6662       if (bits (insn, 5, 6) == 0x2)  /* op2[1:0].  */
6663         return arm_copy_unmodified (gdbarch, insn, "sbfx", dsc);
6664       else
6665         return arm_copy_undef (gdbarch, insn, dsc);
6666
6667     case 0x1c: case 0x1d:
6668       if (bits (insn, 5, 6) == 0x0)  /* op2[1:0].  */
6669          {
6670           if (bits (insn, 0, 3) == 0xf)
6671             return arm_copy_unmodified (gdbarch, insn, "bfc", dsc);
6672           else
6673             return arm_copy_unmodified (gdbarch, insn, "bfi", dsc);
6674         }
6675       else
6676         return arm_copy_undef (gdbarch, insn, dsc);
6677
6678     case 0x1e: case 0x1f:
6679       if (bits (insn, 5, 6) == 0x2)  /* op2[1:0].  */
6680         return arm_copy_unmodified (gdbarch, insn, "ubfx", dsc);
6681       else
6682         return arm_copy_undef (gdbarch, insn, dsc);
6683     }
6684
6685   /* Should be unreachable.  */
6686   return 1;
6687 }
6688
6689 static int
6690 arm_decode_b_bl_ldmstm (struct gdbarch *gdbarch, uint32_t insn,
6691                         struct regcache *regs,
6692                         arm_displaced_step_closure *dsc)
6693 {
6694   if (bit (insn, 25))
6695     return arm_copy_b_bl_blx (gdbarch, insn, regs, dsc);
6696   else
6697     return arm_copy_block_xfer (gdbarch, insn, regs, dsc);
6698 }
6699
6700 static int
6701 arm_decode_ext_reg_ld_st (struct gdbarch *gdbarch, uint32_t insn,
6702                           struct regcache *regs,
6703                           arm_displaced_step_closure *dsc)
6704 {
6705   unsigned int opcode = bits (insn, 20, 24);
6706
6707   switch (opcode)
6708     {
6709     case 0x04: case 0x05:  /* VFP/Neon mrrc/mcrr.  */
6710       return arm_copy_unmodified (gdbarch, insn, "vfp/neon mrrc/mcrr", dsc);
6711
6712     case 0x08: case 0x0a: case 0x0c: case 0x0e:
6713     case 0x12: case 0x16:
6714       return arm_copy_unmodified (gdbarch, insn, "vfp/neon vstm/vpush", dsc);
6715
6716     case 0x09: case 0x0b: case 0x0d: case 0x0f:
6717     case 0x13: case 0x17:
6718       return arm_copy_unmodified (gdbarch, insn, "vfp/neon vldm/vpop", dsc);
6719
6720     case 0x10: case 0x14: case 0x18: case 0x1c:  /* vstr.  */
6721     case 0x11: case 0x15: case 0x19: case 0x1d:  /* vldr.  */
6722       /* Note: no writeback for these instructions.  Bit 25 will always be
6723          zero though (via caller), so the following works OK.  */
6724       return arm_copy_copro_load_store (gdbarch, insn, regs, dsc);
6725     }
6726
6727   /* Should be unreachable.  */
6728   return 1;
6729 }
6730
6731 /* Decode shifted register instructions.  */
6732
6733 static int
6734 thumb2_decode_dp_shift_reg (struct gdbarch *gdbarch, uint16_t insn1,
6735                             uint16_t insn2,  struct regcache *regs,
6736                             arm_displaced_step_closure *dsc)
6737 {
6738   /* PC is only allowed to be used in instruction MOV.  */
6739
6740   unsigned int op = bits (insn1, 5, 8);
6741   unsigned int rn = bits (insn1, 0, 3);
6742
6743   if (op == 0x2 && rn == 0xf) /* MOV */
6744     return thumb2_copy_alu_imm (gdbarch, insn1, insn2, regs, dsc);
6745   else
6746     return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
6747                                         "dp (shift reg)", dsc);
6748 }
6749
6750
6751 /* Decode extension register load/store.  Exactly the same as
6752    arm_decode_ext_reg_ld_st.  */
6753
6754 static int
6755 thumb2_decode_ext_reg_ld_st (struct gdbarch *gdbarch, uint16_t insn1,
6756                              uint16_t insn2,  struct regcache *regs,
6757                              arm_displaced_step_closure *dsc)
6758 {
6759   unsigned int opcode = bits (insn1, 4, 8);
6760
6761   switch (opcode)
6762     {
6763     case 0x04: case 0x05:
6764       return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
6765                                           "vfp/neon vmov", dsc);
6766
6767     case 0x08: case 0x0c: /* 01x00 */
6768     case 0x0a: case 0x0e: /* 01x10 */
6769     case 0x12: case 0x16: /* 10x10 */
6770       return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
6771                                           "vfp/neon vstm/vpush", dsc);
6772
6773     case 0x09: case 0x0d: /* 01x01 */
6774     case 0x0b: case 0x0f: /* 01x11 */
6775     case 0x13: case 0x17: /* 10x11 */
6776       return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
6777                                           "vfp/neon vldm/vpop", dsc);
6778
6779     case 0x10: case 0x14: case 0x18: case 0x1c:  /* vstr.  */
6780       return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
6781                                           "vstr", dsc);
6782     case 0x11: case 0x15: case 0x19: case 0x1d:  /* vldr.  */
6783       return thumb2_copy_copro_load_store (gdbarch, insn1, insn2, regs, dsc);
6784     }
6785
6786   /* Should be unreachable.  */
6787   return 1;
6788 }
6789
6790 static int
6791 arm_decode_svc_copro (struct gdbarch *gdbarch, uint32_t insn,
6792                       struct regcache *regs, arm_displaced_step_closure *dsc)
6793 {
6794   unsigned int op1 = bits (insn, 20, 25);
6795   int op = bit (insn, 4);
6796   unsigned int coproc = bits (insn, 8, 11);
6797
6798   if ((op1 & 0x20) == 0x00 && (op1 & 0x3a) != 0x00 && (coproc & 0xe) == 0xa)
6799     return arm_decode_ext_reg_ld_st (gdbarch, insn, regs, dsc);
6800   else if ((op1 & 0x21) == 0x00 && (op1 & 0x3a) != 0x00
6801            && (coproc & 0xe) != 0xa)
6802     /* stc/stc2.  */
6803     return arm_copy_copro_load_store (gdbarch, insn, regs, dsc);
6804   else if ((op1 & 0x21) == 0x01 && (op1 & 0x3a) != 0x00
6805            && (coproc & 0xe) != 0xa)
6806     /* ldc/ldc2 imm/lit.  */
6807     return arm_copy_copro_load_store (gdbarch, insn, regs, dsc);
6808   else if ((op1 & 0x3e) == 0x00)
6809     return arm_copy_undef (gdbarch, insn, dsc);
6810   else if ((op1 & 0x3e) == 0x04 && (coproc & 0xe) == 0xa)
6811     return arm_copy_unmodified (gdbarch, insn, "neon 64bit xfer", dsc);
6812   else if (op1 == 0x04 && (coproc & 0xe) != 0xa)
6813     return arm_copy_unmodified (gdbarch, insn, "mcrr/mcrr2", dsc);
6814   else if (op1 == 0x05 && (coproc & 0xe) != 0xa)
6815     return arm_copy_unmodified (gdbarch, insn, "mrrc/mrrc2", dsc);
6816   else if ((op1 & 0x30) == 0x20 && !op)
6817     {
6818       if ((coproc & 0xe) == 0xa)
6819         return arm_copy_unmodified (gdbarch, insn, "vfp dataproc", dsc);
6820       else
6821         return arm_copy_unmodified (gdbarch, insn, "cdp/cdp2", dsc);
6822     }
6823   else if ((op1 & 0x30) == 0x20 && op)
6824     return arm_copy_unmodified (gdbarch, insn, "neon 8/16/32 bit xfer", dsc);
6825   else if ((op1 & 0x31) == 0x20 && op && (coproc & 0xe) != 0xa)
6826     return arm_copy_unmodified (gdbarch, insn, "mcr/mcr2", dsc);
6827   else if ((op1 & 0x31) == 0x21 && op && (coproc & 0xe) != 0xa)
6828     return arm_copy_unmodified (gdbarch, insn, "mrc/mrc2", dsc);
6829   else if ((op1 & 0x30) == 0x30)
6830     return arm_copy_svc (gdbarch, insn, regs, dsc);
6831   else
6832     return arm_copy_undef (gdbarch, insn, dsc);  /* Possibly unreachable.  */
6833 }
6834
6835 static int
6836 thumb2_decode_svc_copro (struct gdbarch *gdbarch, uint16_t insn1,
6837                          uint16_t insn2, struct regcache *regs,
6838                          arm_displaced_step_closure *dsc)
6839 {
6840   unsigned int coproc = bits (insn2, 8, 11);
6841   unsigned int bit_5_8 = bits (insn1, 5, 8);
6842   unsigned int bit_9 = bit (insn1, 9);
6843   unsigned int bit_4 = bit (insn1, 4);
6844
6845   if (bit_9 == 0)
6846     {
6847       if (bit_5_8 == 2)
6848         return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
6849                                             "neon 64bit xfer/mrrc/mrrc2/mcrr/mcrr2",
6850                                             dsc);
6851       else if (bit_5_8 == 0) /* UNDEFINED.  */
6852         return thumb_32bit_copy_undef (gdbarch, insn1, insn2, dsc);
6853       else
6854         {
6855            /*coproc is 101x.  SIMD/VFP, ext registers load/store.  */
6856           if ((coproc & 0xe) == 0xa)
6857             return thumb2_decode_ext_reg_ld_st (gdbarch, insn1, insn2, regs,
6858                                                 dsc);
6859           else /* coproc is not 101x.  */
6860             {
6861               if (bit_4 == 0) /* STC/STC2.  */
6862                 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
6863                                                     "stc/stc2", dsc);
6864               else /* LDC/LDC2 {literal, immeidate}.  */
6865                 return thumb2_copy_copro_load_store (gdbarch, insn1, insn2,
6866                                                      regs, dsc);
6867             }
6868         }
6869     }
6870   else
6871     return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2, "coproc", dsc);
6872
6873   return 0;
6874 }
6875
6876 static void
6877 install_pc_relative (struct gdbarch *gdbarch, struct regcache *regs,
6878                      arm_displaced_step_closure *dsc, int rd)
6879 {
6880   /* ADR Rd, #imm
6881
6882      Rewrite as:
6883
6884      Preparation: Rd <- PC
6885      Insn: ADD Rd, #imm
6886      Cleanup: Null.
6887   */
6888
6889   /* Rd <- PC */
6890   int val = displaced_read_reg (regs, dsc, ARM_PC_REGNUM);
6891   displaced_write_reg (regs, dsc, rd, val, CANNOT_WRITE_PC);
6892 }
6893
6894 static int
6895 thumb_copy_pc_relative_16bit (struct gdbarch *gdbarch, struct regcache *regs,
6896                               arm_displaced_step_closure *dsc,
6897                               int rd, unsigned int imm)
6898 {
6899
6900   /* Encoding T2: ADDS Rd, #imm */
6901   dsc->modinsn[0] = (0x3000 | (rd << 8) | imm);
6902
6903   install_pc_relative (gdbarch, regs, dsc, rd);
6904
6905   return 0;
6906 }
6907
6908 static int
6909 thumb_decode_pc_relative_16bit (struct gdbarch *gdbarch, uint16_t insn,
6910                                 struct regcache *regs,
6911                                 arm_displaced_step_closure *dsc)
6912 {
6913   unsigned int rd = bits (insn, 8, 10);
6914   unsigned int imm8 = bits (insn, 0, 7);
6915
6916   if (debug_displaced)
6917     fprintf_unfiltered (gdb_stdlog,
6918                         "displaced: copying thumb adr r%d, #%d insn %.4x\n",
6919                         rd, imm8, insn);
6920
6921   return thumb_copy_pc_relative_16bit (gdbarch, regs, dsc, rd, imm8);
6922 }
6923
6924 static int
6925 thumb_copy_pc_relative_32bit (struct gdbarch *gdbarch, uint16_t insn1,
6926                               uint16_t insn2, struct regcache *regs,
6927                               arm_displaced_step_closure *dsc)
6928 {
6929   unsigned int rd = bits (insn2, 8, 11);
6930   /* Since immediate has the same encoding in ADR ADD and SUB, so we simply
6931      extract raw immediate encoding rather than computing immediate.  When
6932      generating ADD or SUB instruction, we can simply perform OR operation to
6933      set immediate into ADD.  */
6934   unsigned int imm_3_8 = insn2 & 0x70ff;
6935   unsigned int imm_i = insn1 & 0x0400; /* Clear all bits except bit 10.  */
6936
6937   if (debug_displaced)
6938     fprintf_unfiltered (gdb_stdlog,
6939                         "displaced: copying thumb adr r%d, #%d:%d insn %.4x%.4x\n",
6940                         rd, imm_i, imm_3_8, insn1, insn2);
6941
6942   if (bit (insn1, 7)) /* Encoding T2 */
6943     {
6944       /* Encoding T3: SUB Rd, Rd, #imm */
6945       dsc->modinsn[0] = (0xf1a0 | rd | imm_i);
6946       dsc->modinsn[1] = ((rd << 8) | imm_3_8);
6947     }
6948   else /* Encoding T3 */
6949     {
6950       /* Encoding T3: ADD Rd, Rd, #imm */
6951       dsc->modinsn[0] = (0xf100 | rd | imm_i);
6952       dsc->modinsn[1] = ((rd << 8) | imm_3_8);
6953     }
6954   dsc->numinsns = 2;
6955
6956   install_pc_relative (gdbarch, regs, dsc, rd);
6957
6958   return 0;
6959 }
6960
6961 static int
6962 thumb_copy_16bit_ldr_literal (struct gdbarch *gdbarch, uint16_t insn1,
6963                               struct regcache *regs,
6964                               arm_displaced_step_closure *dsc)
6965 {
6966   unsigned int rt = bits (insn1, 8, 10);
6967   unsigned int pc;
6968   int imm8 = (bits (insn1, 0, 7) << 2);
6969
6970   /* LDR Rd, #imm8
6971
6972      Rwrite as:
6973
6974      Preparation: tmp0 <- R0, tmp2 <- R2, tmp3 <- R3, R2 <- PC, R3 <- #imm8;
6975
6976      Insn: LDR R0, [R2, R3];
6977      Cleanup: R2 <- tmp2, R3 <- tmp3, Rd <- R0, R0 <- tmp0 */
6978
6979   if (debug_displaced)
6980     fprintf_unfiltered (gdb_stdlog,
6981                         "displaced: copying thumb ldr r%d [pc #%d]\n"
6982                         , rt, imm8);
6983
6984   dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
6985   dsc->tmp[2] = displaced_read_reg (regs, dsc, 2);
6986   dsc->tmp[3] = displaced_read_reg (regs, dsc, 3);
6987   pc = displaced_read_reg (regs, dsc, ARM_PC_REGNUM);
6988   /* The assembler calculates the required value of the offset from the
6989      Align(PC,4) value of this instruction to the label.  */
6990   pc = pc & 0xfffffffc;
6991
6992   displaced_write_reg (regs, dsc, 2, pc, CANNOT_WRITE_PC);
6993   displaced_write_reg (regs, dsc, 3, imm8, CANNOT_WRITE_PC);
6994
6995   dsc->rd = rt;
6996   dsc->u.ldst.xfersize = 4;
6997   dsc->u.ldst.rn = 0;
6998   dsc->u.ldst.immed = 0;
6999   dsc->u.ldst.writeback = 0;
7000   dsc->u.ldst.restore_r4 = 0;
7001
7002   dsc->modinsn[0] = 0x58d0; /* ldr r0, [r2, r3]*/
7003
7004   dsc->cleanup = &cleanup_load;
7005
7006   return 0;
7007 }
7008
7009 /* Copy Thumb cbnz/cbz insruction.  */
7010
7011 static int
7012 thumb_copy_cbnz_cbz (struct gdbarch *gdbarch, uint16_t insn1,
7013                      struct regcache *regs,
7014                      arm_displaced_step_closure *dsc)
7015 {
7016   int non_zero = bit (insn1, 11);
7017   unsigned int imm5 = (bit (insn1, 9) << 6) | (bits (insn1, 3, 7) << 1);
7018   CORE_ADDR from = dsc->insn_addr;
7019   int rn = bits (insn1, 0, 2);
7020   int rn_val = displaced_read_reg (regs, dsc, rn);
7021
7022   dsc->u.branch.cond = (rn_val && non_zero) || (!rn_val && !non_zero);
7023   /* CBNZ and CBZ do not affect the condition flags.  If condition is true,
7024      set it INST_AL, so cleanup_branch will know branch is taken, otherwise,
7025      condition is false, let it be, cleanup_branch will do nothing.  */
7026   if (dsc->u.branch.cond)
7027     {
7028       dsc->u.branch.cond = INST_AL;
7029       dsc->u.branch.dest = from + 4 + imm5;
7030     }
7031   else
7032       dsc->u.branch.dest = from + 2;
7033
7034   dsc->u.branch.link = 0;
7035   dsc->u.branch.exchange = 0;
7036
7037   if (debug_displaced)
7038     fprintf_unfiltered (gdb_stdlog, "displaced: copying %s [r%d = 0x%x]"
7039                         " insn %.4x to %.8lx\n", non_zero ? "cbnz" : "cbz",
7040                         rn, rn_val, insn1, dsc->u.branch.dest);
7041
7042   dsc->modinsn[0] = THUMB_NOP;
7043
7044   dsc->cleanup = &cleanup_branch;
7045   return 0;
7046 }
7047
7048 /* Copy Table Branch Byte/Halfword */
7049 static int
7050 thumb2_copy_table_branch (struct gdbarch *gdbarch, uint16_t insn1,
7051                           uint16_t insn2, struct regcache *regs,
7052                           arm_displaced_step_closure *dsc)
7053 {
7054   ULONGEST rn_val, rm_val;
7055   int is_tbh = bit (insn2, 4);
7056   CORE_ADDR halfwords = 0;
7057   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
7058
7059   rn_val = displaced_read_reg (regs, dsc, bits (insn1, 0, 3));
7060   rm_val = displaced_read_reg (regs, dsc, bits (insn2, 0, 3));
7061
7062   if (is_tbh)
7063     {
7064       gdb_byte buf[2];
7065
7066       target_read_memory (rn_val + 2 * rm_val, buf, 2);
7067       halfwords = extract_unsigned_integer (buf, 2, byte_order);
7068     }
7069   else
7070     {
7071       gdb_byte buf[1];
7072
7073       target_read_memory (rn_val + rm_val, buf, 1);
7074       halfwords = extract_unsigned_integer (buf, 1, byte_order);
7075     }
7076
7077   if (debug_displaced)
7078     fprintf_unfiltered (gdb_stdlog, "displaced: %s base 0x%x offset 0x%x"
7079                         " offset 0x%x\n", is_tbh ? "tbh" : "tbb",
7080                         (unsigned int) rn_val, (unsigned int) rm_val,
7081                         (unsigned int) halfwords);
7082
7083   dsc->u.branch.cond = INST_AL;
7084   dsc->u.branch.link = 0;
7085   dsc->u.branch.exchange = 0;
7086   dsc->u.branch.dest = dsc->insn_addr + 4 + 2 * halfwords;
7087
7088   dsc->cleanup = &cleanup_branch;
7089
7090   return 0;
7091 }
7092
7093 static void
7094 cleanup_pop_pc_16bit_all (struct gdbarch *gdbarch, struct regcache *regs,
7095                           arm_displaced_step_closure *dsc)
7096 {
7097   /* PC <- r7 */
7098   int val = displaced_read_reg (regs, dsc, 7);
7099   displaced_write_reg (regs, dsc, ARM_PC_REGNUM, val, BX_WRITE_PC);
7100
7101   /* r7 <- r8 */
7102   val = displaced_read_reg (regs, dsc, 8);
7103   displaced_write_reg (regs, dsc, 7, val, CANNOT_WRITE_PC);
7104
7105   /* r8 <- tmp[0] */
7106   displaced_write_reg (regs, dsc, 8, dsc->tmp[0], CANNOT_WRITE_PC);
7107
7108 }
7109
7110 static int
7111 thumb_copy_pop_pc_16bit (struct gdbarch *gdbarch, uint16_t insn1,
7112                          struct regcache *regs,
7113                          arm_displaced_step_closure *dsc)
7114 {
7115   dsc->u.block.regmask = insn1 & 0x00ff;
7116
7117   /* Rewrite instruction: POP {rX, rY, ...,rZ, PC}
7118      to :
7119
7120      (1) register list is full, that is, r0-r7 are used.
7121      Prepare: tmp[0] <- r8
7122
7123      POP {r0, r1, ...., r6, r7}; remove PC from reglist
7124      MOV r8, r7; Move value of r7 to r8;
7125      POP {r7}; Store PC value into r7.
7126
7127      Cleanup: PC <- r7, r7 <- r8, r8 <-tmp[0]
7128
7129      (2) register list is not full, supposing there are N registers in
7130      register list (except PC, 0 <= N <= 7).
7131      Prepare: for each i, 0 - N, tmp[i] <- ri.
7132
7133      POP {r0, r1, ...., rN};
7134
7135      Cleanup: Set registers in original reglist from r0 - rN.  Restore r0 - rN
7136      from tmp[] properly.
7137   */
7138   if (debug_displaced)
7139     fprintf_unfiltered (gdb_stdlog,
7140                         "displaced: copying thumb pop {%.8x, pc} insn %.4x\n",
7141                         dsc->u.block.regmask, insn1);
7142
7143   if (dsc->u.block.regmask == 0xff)
7144     {
7145       dsc->tmp[0] = displaced_read_reg (regs, dsc, 8);
7146
7147       dsc->modinsn[0] = (insn1 & 0xfeff); /* POP {r0,r1,...,r6, r7} */
7148       dsc->modinsn[1] = 0x46b8; /* MOV r8, r7 */
7149       dsc->modinsn[2] = 0xbc80; /* POP {r7} */
7150
7151       dsc->numinsns = 3;
7152       dsc->cleanup = &cleanup_pop_pc_16bit_all;
7153     }
7154   else
7155     {
7156       unsigned int num_in_list = bitcount (dsc->u.block.regmask);
7157       unsigned int i;
7158       unsigned int new_regmask;
7159
7160       for (i = 0; i < num_in_list + 1; i++)
7161         dsc->tmp[i] = displaced_read_reg (regs, dsc, i);
7162
7163       new_regmask = (1 << (num_in_list + 1)) - 1;
7164
7165       if (debug_displaced)
7166         fprintf_unfiltered (gdb_stdlog, _("displaced: POP "
7167                                           "{..., pc}: original reg list %.4x,"
7168                                           " modified list %.4x\n"),
7169                             (int) dsc->u.block.regmask, new_regmask);
7170
7171       dsc->u.block.regmask |= 0x8000;
7172       dsc->u.block.writeback = 0;
7173       dsc->u.block.cond = INST_AL;
7174
7175       dsc->modinsn[0] = (insn1 & ~0x1ff) | (new_regmask & 0xff);
7176
7177       dsc->cleanup = &cleanup_block_load_pc;
7178     }
7179
7180   return 0;
7181 }
7182
7183 static void
7184 thumb_process_displaced_16bit_insn (struct gdbarch *gdbarch, uint16_t insn1,
7185                                     struct regcache *regs,
7186                                     arm_displaced_step_closure *dsc)
7187 {
7188   unsigned short op_bit_12_15 = bits (insn1, 12, 15);
7189   unsigned short op_bit_10_11 = bits (insn1, 10, 11);
7190   int err = 0;
7191
7192   /* 16-bit thumb instructions.  */
7193   switch (op_bit_12_15)
7194     {
7195       /* Shift (imme), add, subtract, move and compare.  */
7196     case 0: case 1: case 2: case 3:
7197       err = thumb_copy_unmodified_16bit (gdbarch, insn1,
7198                                          "shift/add/sub/mov/cmp",
7199                                          dsc);
7200       break;
7201     case 4:
7202       switch (op_bit_10_11)
7203         {
7204         case 0: /* Data-processing */
7205           err = thumb_copy_unmodified_16bit (gdbarch, insn1,
7206                                              "data-processing",
7207                                              dsc);
7208           break;
7209         case 1: /* Special data instructions and branch and exchange.  */
7210           {
7211             unsigned short op = bits (insn1, 7, 9);
7212             if (op == 6 || op == 7) /* BX or BLX */
7213               err = thumb_copy_bx_blx_reg (gdbarch, insn1, regs, dsc);
7214             else if (bits (insn1, 6, 7) != 0) /* ADD/MOV/CMP high registers.  */
7215               err = thumb_copy_alu_reg (gdbarch, insn1, regs, dsc);
7216             else
7217               err = thumb_copy_unmodified_16bit (gdbarch, insn1, "special data",
7218                                                  dsc);
7219           }
7220           break;
7221         default: /* LDR (literal) */
7222           err = thumb_copy_16bit_ldr_literal (gdbarch, insn1, regs, dsc);
7223         }
7224       break;
7225     case 5: case 6: case 7: case 8: case 9: /* Load/Store single data item */
7226       err = thumb_copy_unmodified_16bit (gdbarch, insn1, "ldr/str", dsc);
7227       break;
7228     case 10:
7229       if (op_bit_10_11 < 2) /* Generate PC-relative address */
7230         err = thumb_decode_pc_relative_16bit (gdbarch, insn1, regs, dsc);
7231       else /* Generate SP-relative address */
7232         err = thumb_copy_unmodified_16bit (gdbarch, insn1, "sp-relative", dsc);
7233       break;
7234     case 11: /* Misc 16-bit instructions */
7235       {
7236         switch (bits (insn1, 8, 11))
7237           {
7238           case 1: case 3:  case 9: case 11: /* CBNZ, CBZ */
7239             err = thumb_copy_cbnz_cbz (gdbarch, insn1, regs, dsc);
7240             break;
7241           case 12: case 13: /* POP */
7242             if (bit (insn1, 8)) /* PC is in register list.  */
7243               err = thumb_copy_pop_pc_16bit (gdbarch, insn1, regs, dsc);
7244             else
7245               err = thumb_copy_unmodified_16bit (gdbarch, insn1, "pop", dsc);
7246             break;
7247           case 15: /* If-Then, and hints */
7248             if (bits (insn1, 0, 3))
7249               /* If-Then makes up to four following instructions conditional.
7250                  IT instruction itself is not conditional, so handle it as a
7251                  common unmodified instruction.  */
7252               err = thumb_copy_unmodified_16bit (gdbarch, insn1, "If-Then",
7253                                                  dsc);
7254             else
7255               err = thumb_copy_unmodified_16bit (gdbarch, insn1, "hints", dsc);
7256             break;
7257           default:
7258             err = thumb_copy_unmodified_16bit (gdbarch, insn1, "misc", dsc);
7259           }
7260       }
7261       break;
7262     case 12:
7263       if (op_bit_10_11 < 2) /* Store multiple registers */
7264         err = thumb_copy_unmodified_16bit (gdbarch, insn1, "stm", dsc);
7265       else /* Load multiple registers */
7266         err = thumb_copy_unmodified_16bit (gdbarch, insn1, "ldm", dsc);
7267       break;
7268     case 13: /* Conditional branch and supervisor call */
7269       if (bits (insn1, 9, 11) != 7) /* conditional branch */
7270         err = thumb_copy_b (gdbarch, insn1, dsc);
7271       else
7272         err = thumb_copy_svc (gdbarch, insn1, regs, dsc);
7273       break;
7274     case 14: /* Unconditional branch */
7275       err = thumb_copy_b (gdbarch, insn1, dsc);
7276       break;
7277     default:
7278       err = 1;
7279     }
7280
7281   if (err)
7282     internal_error (__FILE__, __LINE__,
7283                     _("thumb_process_displaced_16bit_insn: Instruction decode error"));
7284 }
7285
7286 static int
7287 decode_thumb_32bit_ld_mem_hints (struct gdbarch *gdbarch,
7288                                  uint16_t insn1, uint16_t insn2,
7289                                  struct regcache *regs,
7290                                  arm_displaced_step_closure *dsc)
7291 {
7292   int rt = bits (insn2, 12, 15);
7293   int rn = bits (insn1, 0, 3);
7294   int op1 = bits (insn1, 7, 8);
7295
7296   switch (bits (insn1, 5, 6))
7297     {
7298     case 0: /* Load byte and memory hints */
7299       if (rt == 0xf) /* PLD/PLI */
7300         {
7301           if (rn == 0xf)
7302             /* PLD literal or Encoding T3 of PLI(immediate, literal).  */
7303             return thumb2_copy_preload (gdbarch, insn1, insn2, regs, dsc);
7304           else
7305             return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
7306                                                 "pli/pld", dsc);
7307         }
7308       else
7309         {
7310           if (rn == 0xf) /* LDRB/LDRSB (literal) */
7311             return thumb2_copy_load_literal (gdbarch, insn1, insn2, regs, dsc,
7312                                              1);
7313           else
7314             return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
7315                                                 "ldrb{reg, immediate}/ldrbt",
7316                                                 dsc);
7317         }
7318
7319       break;
7320     case 1: /* Load halfword and memory hints.  */
7321       if (rt == 0xf) /* PLD{W} and Unalloc memory hint.  */
7322         return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
7323                                             "pld/unalloc memhint", dsc);
7324       else
7325         {
7326           if (rn == 0xf)
7327             return thumb2_copy_load_literal (gdbarch, insn1, insn2, regs, dsc,
7328                                              2);
7329           else
7330             return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
7331                                                 "ldrh/ldrht", dsc);
7332         }
7333       break;
7334     case 2: /* Load word */
7335       {
7336         int insn2_bit_8_11 = bits (insn2, 8, 11);
7337
7338         if (rn == 0xf)
7339           return thumb2_copy_load_literal (gdbarch, insn1, insn2, regs, dsc, 4);
7340         else if (op1 == 0x1) /* Encoding T3 */
7341           return thumb2_copy_load_reg_imm (gdbarch, insn1, insn2, regs, dsc,
7342                                            0, 1);
7343         else /* op1 == 0x0 */
7344           {
7345             if (insn2_bit_8_11 == 0xc || (insn2_bit_8_11 & 0x9) == 0x9)
7346               /* LDR (immediate) */
7347               return thumb2_copy_load_reg_imm (gdbarch, insn1, insn2, regs,
7348                                                dsc, bit (insn2, 8), 1);
7349             else if (insn2_bit_8_11 == 0xe) /* LDRT */
7350               return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
7351                                                   "ldrt", dsc);
7352             else
7353               /* LDR (register) */
7354               return thumb2_copy_load_reg_imm (gdbarch, insn1, insn2, regs,
7355                                                dsc, 0, 0);
7356           }
7357         break;
7358       }
7359     default:
7360       return thumb_32bit_copy_undef (gdbarch, insn1, insn2, dsc);
7361       break;
7362     }
7363   return 0;
7364 }
7365
7366 static void
7367 thumb_process_displaced_32bit_insn (struct gdbarch *gdbarch, uint16_t insn1,
7368                                     uint16_t insn2, struct regcache *regs,
7369                                     arm_displaced_step_closure *dsc)
7370 {
7371   int err = 0;
7372   unsigned short op = bit (insn2, 15);
7373   unsigned int op1 = bits (insn1, 11, 12);
7374
7375   switch (op1)
7376     {
7377     case 1:
7378       {
7379         switch (bits (insn1, 9, 10))
7380           {
7381           case 0:
7382             if (bit (insn1, 6))
7383               {
7384                 /* Load/store {dual, execlusive}, table branch.  */
7385                 if (bits (insn1, 7, 8) == 1 && bits (insn1, 4, 5) == 1
7386                     && bits (insn2, 5, 7) == 0)
7387                   err = thumb2_copy_table_branch (gdbarch, insn1, insn2, regs,
7388                                                   dsc);
7389                 else
7390                   /* PC is not allowed to use in load/store {dual, exclusive}
7391                      instructions.  */
7392                   err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
7393                                                      "load/store dual/ex", dsc);
7394               }
7395             else /* load/store multiple */
7396               {
7397                 switch (bits (insn1, 7, 8))
7398                   {
7399                   case 0: case 3: /* SRS, RFE */
7400                     err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
7401                                                        "srs/rfe", dsc);
7402                     break;
7403                   case 1: case 2: /* LDM/STM/PUSH/POP */
7404                     err = thumb2_copy_block_xfer (gdbarch, insn1, insn2, regs, dsc);
7405                     break;
7406                   }
7407               }
7408             break;
7409
7410           case 1:
7411             /* Data-processing (shift register).  */
7412             err = thumb2_decode_dp_shift_reg (gdbarch, insn1, insn2, regs,
7413                                               dsc);
7414             break;
7415           default: /* Coprocessor instructions.  */
7416             err = thumb2_decode_svc_copro (gdbarch, insn1, insn2, regs, dsc);
7417             break;
7418           }
7419       break;
7420       }
7421     case 2: /* op1 = 2 */
7422       if (op) /* Branch and misc control.  */
7423         {
7424           if (bit (insn2, 14)  /* BLX/BL */
7425               || bit (insn2, 12) /* Unconditional branch */
7426               || (bits (insn1, 7, 9) != 0x7)) /* Conditional branch */
7427             err = thumb2_copy_b_bl_blx (gdbarch, insn1, insn2, regs, dsc);
7428           else
7429             err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
7430                                                "misc ctrl", dsc);
7431         }
7432       else
7433         {
7434           if (bit (insn1, 9)) /* Data processing (plain binary imm).  */
7435             {
7436               int dp_op = bits (insn1, 4, 8);
7437               int rn = bits (insn1, 0, 3);
7438               if ((dp_op == 0 || dp_op == 0xa) && rn == 0xf)
7439                 err = thumb_copy_pc_relative_32bit (gdbarch, insn1, insn2,
7440                                                     regs, dsc);
7441               else
7442                 err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
7443                                                    "dp/pb", dsc);
7444             }
7445           else /* Data processing (modified immeidate) */
7446             err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
7447                                                "dp/mi", dsc);
7448         }
7449       break;
7450     case 3: /* op1 = 3 */
7451       switch (bits (insn1, 9, 10))
7452         {
7453         case 0:
7454           if (bit (insn1, 4))
7455             err = decode_thumb_32bit_ld_mem_hints (gdbarch, insn1, insn2,
7456                                                    regs, dsc);
7457           else /* NEON Load/Store and Store single data item */
7458             err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
7459                                                "neon elt/struct load/store",
7460                                                dsc);
7461           break;
7462         case 1: /* op1 = 3, bits (9, 10) == 1 */
7463           switch (bits (insn1, 7, 8))
7464             {
7465             case 0: case 1: /* Data processing (register) */
7466               err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
7467                                                  "dp(reg)", dsc);
7468               break;
7469             case 2: /* Multiply and absolute difference */
7470               err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
7471                                                  "mul/mua/diff", dsc);
7472               break;
7473             case 3: /* Long multiply and divide */
7474               err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
7475                                                  "lmul/lmua", dsc);
7476               break;
7477             }
7478           break;
7479         default: /* Coprocessor instructions */
7480           err = thumb2_decode_svc_copro (gdbarch, insn1, insn2, regs, dsc);
7481           break;
7482         }
7483       break;
7484     default:
7485       err = 1;
7486     }
7487
7488   if (err)
7489     internal_error (__FILE__, __LINE__,
7490                     _("thumb_process_displaced_32bit_insn: Instruction decode error"));
7491
7492 }
7493
7494 static void
7495 thumb_process_displaced_insn (struct gdbarch *gdbarch, CORE_ADDR from,
7496                               struct regcache *regs,
7497                               arm_displaced_step_closure *dsc)
7498 {
7499   enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
7500   uint16_t insn1
7501     = read_memory_unsigned_integer (from, 2, byte_order_for_code);
7502
7503   if (debug_displaced)
7504     fprintf_unfiltered (gdb_stdlog, "displaced: process thumb insn %.4x "
7505                         "at %.8lx\n", insn1, (unsigned long) from);
7506
7507   dsc->is_thumb = 1;
7508   dsc->insn_size = thumb_insn_size (insn1);
7509   if (thumb_insn_size (insn1) == 4)
7510     {
7511       uint16_t insn2
7512         = read_memory_unsigned_integer (from + 2, 2, byte_order_for_code);
7513       thumb_process_displaced_32bit_insn (gdbarch, insn1, insn2, regs, dsc);
7514     }
7515   else
7516     thumb_process_displaced_16bit_insn (gdbarch, insn1, regs, dsc);
7517 }
7518
7519 void
7520 arm_process_displaced_insn (struct gdbarch *gdbarch, CORE_ADDR from,
7521                             CORE_ADDR to, struct regcache *regs,
7522                             arm_displaced_step_closure *dsc)
7523 {
7524   int err = 0;
7525   enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
7526   uint32_t insn;
7527
7528   /* Most displaced instructions use a 1-instruction scratch space, so set this
7529      here and override below if/when necessary.  */
7530   dsc->numinsns = 1;
7531   dsc->insn_addr = from;
7532   dsc->scratch_base = to;
7533   dsc->cleanup = NULL;
7534   dsc->wrote_to_pc = 0;
7535
7536   if (!displaced_in_arm_mode (regs))
7537     return thumb_process_displaced_insn (gdbarch, from, regs, dsc);
7538
7539   dsc->is_thumb = 0;
7540   dsc->insn_size = 4;
7541   insn = read_memory_unsigned_integer (from, 4, byte_order_for_code);
7542   if (debug_displaced)
7543     fprintf_unfiltered (gdb_stdlog, "displaced: stepping insn %.8lx "
7544                         "at %.8lx\n", (unsigned long) insn,
7545                         (unsigned long) from);
7546
7547   if ((insn & 0xf0000000) == 0xf0000000)
7548     err = arm_decode_unconditional (gdbarch, insn, regs, dsc);
7549   else switch (((insn & 0x10) >> 4) | ((insn & 0xe000000) >> 24))
7550     {
7551     case 0x0: case 0x1: case 0x2: case 0x3:
7552       err = arm_decode_dp_misc (gdbarch, insn, regs, dsc);
7553       break;
7554
7555     case 0x4: case 0x5: case 0x6:
7556       err = arm_decode_ld_st_word_ubyte (gdbarch, insn, regs, dsc);
7557       break;
7558
7559     case 0x7:
7560       err = arm_decode_media (gdbarch, insn, dsc);
7561       break;
7562
7563     case 0x8: case 0x9: case 0xa: case 0xb:
7564       err = arm_decode_b_bl_ldmstm (gdbarch, insn, regs, dsc);
7565       break;
7566
7567     case 0xc: case 0xd: case 0xe: case 0xf:
7568       err = arm_decode_svc_copro (gdbarch, insn, regs, dsc);
7569       break;
7570     }
7571
7572   if (err)
7573     internal_error (__FILE__, __LINE__,
7574                     _("arm_process_displaced_insn: Instruction decode error"));
7575 }
7576
7577 /* Actually set up the scratch space for a displaced instruction.  */
7578
7579 void
7580 arm_displaced_init_closure (struct gdbarch *gdbarch, CORE_ADDR from,
7581                             CORE_ADDR to, arm_displaced_step_closure *dsc)
7582 {
7583   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
7584   unsigned int i, len, offset;
7585   enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
7586   int size = dsc->is_thumb? 2 : 4;
7587   const gdb_byte *bkp_insn;
7588
7589   offset = 0;
7590   /* Poke modified instruction(s).  */
7591   for (i = 0; i < dsc->numinsns; i++)
7592     {
7593       if (debug_displaced)
7594         {
7595           fprintf_unfiltered (gdb_stdlog, "displaced: writing insn ");
7596           if (size == 4)
7597             fprintf_unfiltered (gdb_stdlog, "%.8lx",
7598                                 dsc->modinsn[i]);
7599           else if (size == 2)
7600             fprintf_unfiltered (gdb_stdlog, "%.4x",
7601                                 (unsigned short)dsc->modinsn[i]);
7602
7603           fprintf_unfiltered (gdb_stdlog, " at %.8lx\n",
7604                               (unsigned long) to + offset);
7605
7606         }
7607       write_memory_unsigned_integer (to + offset, size,
7608                                      byte_order_for_code,
7609                                      dsc->modinsn[i]);
7610       offset += size;
7611     }
7612
7613   /* Choose the correct breakpoint instruction.  */
7614   if (dsc->is_thumb)
7615     {
7616       bkp_insn = tdep->thumb_breakpoint;
7617       len = tdep->thumb_breakpoint_size;
7618     }
7619   else
7620     {
7621       bkp_insn = tdep->arm_breakpoint;
7622       len = tdep->arm_breakpoint_size;
7623     }
7624
7625   /* Put breakpoint afterwards.  */
7626   write_memory (to + offset, bkp_insn, len);
7627
7628   if (debug_displaced)
7629     fprintf_unfiltered (gdb_stdlog, "displaced: copy %s->%s: ",
7630                         paddress (gdbarch, from), paddress (gdbarch, to));
7631 }
7632
7633 /* Entry point for cleaning things up after a displaced instruction has been
7634    single-stepped.  */
7635
7636 void
7637 arm_displaced_step_fixup (struct gdbarch *gdbarch,
7638                           struct displaced_step_closure *dsc_,
7639                           CORE_ADDR from, CORE_ADDR to,
7640                           struct regcache *regs)
7641 {
7642   arm_displaced_step_closure *dsc = (arm_displaced_step_closure *) dsc_;
7643
7644   if (dsc->cleanup)
7645     dsc->cleanup (gdbarch, regs, dsc);
7646
7647   if (!dsc->wrote_to_pc)
7648     regcache_cooked_write_unsigned (regs, ARM_PC_REGNUM,
7649                                     dsc->insn_addr + dsc->insn_size);
7650
7651 }
7652
7653 #include "bfd-in2.h"
7654 #include "libcoff.h"
7655
7656 static int
7657 gdb_print_insn_arm (bfd_vma memaddr, disassemble_info *info)
7658 {
7659   gdb_disassembler *di
7660     = static_cast<gdb_disassembler *>(info->application_data);
7661   struct gdbarch *gdbarch = di->arch ();
7662
7663   if (arm_pc_is_thumb (gdbarch, memaddr))
7664     {
7665       static asymbol *asym;
7666       static combined_entry_type ce;
7667       static struct coff_symbol_struct csym;
7668       static struct bfd fake_bfd;
7669       static bfd_target fake_target;
7670
7671       if (csym.native == NULL)
7672         {
7673           /* Create a fake symbol vector containing a Thumb symbol.
7674              This is solely so that the code in print_insn_little_arm() 
7675              and print_insn_big_arm() in opcodes/arm-dis.c will detect
7676              the presence of a Thumb symbol and switch to decoding
7677              Thumb instructions.  */
7678
7679           fake_target.flavour = bfd_target_coff_flavour;
7680           fake_bfd.xvec = &fake_target;
7681           ce.u.syment.n_sclass = C_THUMBEXTFUNC;
7682           csym.native = &ce;
7683           csym.symbol.the_bfd = &fake_bfd;
7684           csym.symbol.name = "fake";
7685           asym = (asymbol *) & csym;
7686         }
7687
7688       memaddr = UNMAKE_THUMB_ADDR (memaddr);
7689       info->symbols = &asym;
7690     }
7691   else
7692     info->symbols = NULL;
7693
7694   /* GDB is able to get bfd_mach from the exe_bfd, info->mach is
7695      accurate, so mark USER_SPECIFIED_MACHINE_TYPE bit.  Otherwise,
7696      opcodes/arm-dis.c:print_insn reset info->mach, and it will trigger
7697      the assert on the mismatch of info->mach and bfd_get_mach (exec_bfd)
7698      in default_print_insn.  */
7699   if (exec_bfd != NULL)
7700     info->flags |= USER_SPECIFIED_MACHINE_TYPE;
7701
7702   return default_print_insn (memaddr, info);
7703 }
7704
7705 /* The following define instruction sequences that will cause ARM
7706    cpu's to take an undefined instruction trap.  These are used to
7707    signal a breakpoint to GDB.
7708    
7709    The newer ARMv4T cpu's are capable of operating in ARM or Thumb
7710    modes.  A different instruction is required for each mode.  The ARM
7711    cpu's can also be big or little endian.  Thus four different
7712    instructions are needed to support all cases.
7713    
7714    Note: ARMv4 defines several new instructions that will take the
7715    undefined instruction trap.  ARM7TDMI is nominally ARMv4T, but does
7716    not in fact add the new instructions.  The new undefined
7717    instructions in ARMv4 are all instructions that had no defined
7718    behaviour in earlier chips.  There is no guarantee that they will
7719    raise an exception, but may be treated as NOP's.  In practice, it
7720    may only safe to rely on instructions matching:
7721    
7722    3 3 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1 
7723    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
7724    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
7725    
7726    Even this may only true if the condition predicate is true.  The
7727    following use a condition predicate of ALWAYS so it is always TRUE.
7728    
7729    There are other ways of forcing a breakpoint.  GNU/Linux, RISC iX,
7730    and NetBSD all use a software interrupt rather than an undefined
7731    instruction to force a trap.  This can be handled by by the
7732    abi-specific code during establishment of the gdbarch vector.  */
7733
7734 #define ARM_LE_BREAKPOINT {0xFE,0xDE,0xFF,0xE7}
7735 #define ARM_BE_BREAKPOINT {0xE7,0xFF,0xDE,0xFE}
7736 #define THUMB_LE_BREAKPOINT {0xbe,0xbe}
7737 #define THUMB_BE_BREAKPOINT {0xbe,0xbe}
7738
7739 static const gdb_byte arm_default_arm_le_breakpoint[] = ARM_LE_BREAKPOINT;
7740 static const gdb_byte arm_default_arm_be_breakpoint[] = ARM_BE_BREAKPOINT;
7741 static const gdb_byte arm_default_thumb_le_breakpoint[] = THUMB_LE_BREAKPOINT;
7742 static const gdb_byte arm_default_thumb_be_breakpoint[] = THUMB_BE_BREAKPOINT;
7743
7744 /* Implement the breakpoint_kind_from_pc gdbarch method.  */
7745
7746 static int
7747 arm_breakpoint_kind_from_pc (struct gdbarch *gdbarch, CORE_ADDR *pcptr)
7748 {
7749   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
7750   enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
7751
7752   if (arm_pc_is_thumb (gdbarch, *pcptr))
7753     {
7754       *pcptr = UNMAKE_THUMB_ADDR (*pcptr);
7755
7756       /* If we have a separate 32-bit breakpoint instruction for Thumb-2,
7757          check whether we are replacing a 32-bit instruction.  */
7758       if (tdep->thumb2_breakpoint != NULL)
7759         {
7760           gdb_byte buf[2];
7761
7762           if (target_read_memory (*pcptr, buf, 2) == 0)
7763             {
7764               unsigned short inst1;
7765
7766               inst1 = extract_unsigned_integer (buf, 2, byte_order_for_code);
7767               if (thumb_insn_size (inst1) == 4)
7768                 return ARM_BP_KIND_THUMB2;
7769             }
7770         }
7771
7772       return ARM_BP_KIND_THUMB;
7773     }
7774   else
7775     return ARM_BP_KIND_ARM;
7776
7777 }
7778
7779 /* Implement the sw_breakpoint_from_kind gdbarch method.  */
7780
7781 static const gdb_byte *
7782 arm_sw_breakpoint_from_kind (struct gdbarch *gdbarch, int kind, int *size)
7783 {
7784   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
7785
7786   switch (kind)
7787     {
7788     case ARM_BP_KIND_ARM:
7789       *size = tdep->arm_breakpoint_size;
7790       return tdep->arm_breakpoint;
7791     case ARM_BP_KIND_THUMB:
7792       *size = tdep->thumb_breakpoint_size;
7793       return tdep->thumb_breakpoint;
7794     case ARM_BP_KIND_THUMB2:
7795       *size = tdep->thumb2_breakpoint_size;
7796       return tdep->thumb2_breakpoint;
7797     default:
7798       gdb_assert_not_reached ("unexpected arm breakpoint kind");
7799     }
7800 }
7801
7802 /* Implement the breakpoint_kind_from_current_state gdbarch method.  */
7803
7804 static int
7805 arm_breakpoint_kind_from_current_state (struct gdbarch *gdbarch,
7806                                         struct regcache *regcache,
7807                                         CORE_ADDR *pcptr)
7808 {
7809   gdb_byte buf[4];
7810
7811   /* Check the memory pointed by PC is readable.  */
7812   if (target_read_memory (regcache_read_pc (regcache), buf, 4) == 0)
7813     {
7814       struct arm_get_next_pcs next_pcs_ctx;
7815
7816       arm_get_next_pcs_ctor (&next_pcs_ctx,
7817                              &arm_get_next_pcs_ops,
7818                              gdbarch_byte_order (gdbarch),
7819                              gdbarch_byte_order_for_code (gdbarch),
7820                              0,
7821                              regcache);
7822
7823       std::vector<CORE_ADDR> next_pcs = arm_get_next_pcs (&next_pcs_ctx);
7824
7825       /* If MEMADDR is the next instruction of current pc, do the
7826          software single step computation, and get the thumb mode by
7827          the destination address.  */
7828       for (CORE_ADDR pc : next_pcs)
7829         {
7830           if (UNMAKE_THUMB_ADDR (pc) == *pcptr)
7831             {
7832               if (IS_THUMB_ADDR (pc))
7833                 {
7834                   *pcptr = MAKE_THUMB_ADDR (*pcptr);
7835                   return arm_breakpoint_kind_from_pc (gdbarch, pcptr);
7836                 }
7837               else
7838                 return ARM_BP_KIND_ARM;
7839             }
7840         }
7841     }
7842
7843   return arm_breakpoint_kind_from_pc (gdbarch, pcptr);
7844 }
7845
7846 /* Extract from an array REGBUF containing the (raw) register state a
7847    function return value of type TYPE, and copy that, in virtual
7848    format, into VALBUF.  */
7849
7850 static void
7851 arm_extract_return_value (struct type *type, struct regcache *regs,
7852                           gdb_byte *valbuf)
7853 {
7854   struct gdbarch *gdbarch = regs->arch ();
7855   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
7856
7857   if (TYPE_CODE_FLT == TYPE_CODE (type))
7858     {
7859       switch (gdbarch_tdep (gdbarch)->fp_model)
7860         {
7861         case ARM_FLOAT_FPA:
7862           {
7863             /* The value is in register F0 in internal format.  We need to
7864                extract the raw value and then convert it to the desired
7865                internal type.  */
7866             bfd_byte tmpbuf[FP_REGISTER_SIZE];
7867
7868             regs->cooked_read (ARM_F0_REGNUM, tmpbuf);
7869             target_float_convert (tmpbuf, arm_ext_type (gdbarch),
7870                                   valbuf, type);
7871           }
7872           break;
7873
7874         case ARM_FLOAT_SOFT_FPA:
7875         case ARM_FLOAT_SOFT_VFP:
7876           /* ARM_FLOAT_VFP can arise if this is a variadic function so
7877              not using the VFP ABI code.  */
7878         case ARM_FLOAT_VFP:
7879           regs->cooked_read (ARM_A1_REGNUM, valbuf);
7880           if (TYPE_LENGTH (type) > 4)
7881             regs->cooked_read (ARM_A1_REGNUM + 1, valbuf + INT_REGISTER_SIZE);
7882           break;
7883
7884         default:
7885           internal_error (__FILE__, __LINE__,
7886                           _("arm_extract_return_value: "
7887                             "Floating point model not supported"));
7888           break;
7889         }
7890     }
7891   else if (TYPE_CODE (type) == TYPE_CODE_INT
7892            || TYPE_CODE (type) == TYPE_CODE_CHAR
7893            || TYPE_CODE (type) == TYPE_CODE_BOOL
7894            || TYPE_CODE (type) == TYPE_CODE_PTR
7895            || TYPE_IS_REFERENCE (type)
7896            || TYPE_CODE (type) == TYPE_CODE_ENUM)
7897     {
7898       /* If the type is a plain integer, then the access is
7899          straight-forward.  Otherwise we have to play around a bit
7900          more.  */
7901       int len = TYPE_LENGTH (type);
7902       int regno = ARM_A1_REGNUM;
7903       ULONGEST tmp;
7904
7905       while (len > 0)
7906         {
7907           /* By using store_unsigned_integer we avoid having to do
7908              anything special for small big-endian values.  */
7909           regcache_cooked_read_unsigned (regs, regno++, &tmp);
7910           store_unsigned_integer (valbuf, 
7911                                   (len > INT_REGISTER_SIZE
7912                                    ? INT_REGISTER_SIZE : len),
7913                                   byte_order, tmp);
7914           len -= INT_REGISTER_SIZE;
7915           valbuf += INT_REGISTER_SIZE;
7916         }
7917     }
7918   else
7919     {
7920       /* For a structure or union the behaviour is as if the value had
7921          been stored to word-aligned memory and then loaded into 
7922          registers with 32-bit load instruction(s).  */
7923       int len = TYPE_LENGTH (type);
7924       int regno = ARM_A1_REGNUM;
7925       bfd_byte tmpbuf[INT_REGISTER_SIZE];
7926
7927       while (len > 0)
7928         {
7929           regs->cooked_read (regno++, tmpbuf);
7930           memcpy (valbuf, tmpbuf,
7931                   len > INT_REGISTER_SIZE ? INT_REGISTER_SIZE : len);
7932           len -= INT_REGISTER_SIZE;
7933           valbuf += INT_REGISTER_SIZE;
7934         }
7935     }
7936 }
7937
7938
7939 /* Will a function return an aggregate type in memory or in a
7940    register?  Return 0 if an aggregate type can be returned in a
7941    register, 1 if it must be returned in memory.  */
7942
7943 static int
7944 arm_return_in_memory (struct gdbarch *gdbarch, struct type *type)
7945 {
7946   enum type_code code;
7947
7948   type = check_typedef (type);
7949
7950   /* Simple, non-aggregate types (ie not including vectors and
7951      complex) are always returned in a register (or registers).  */
7952   code = TYPE_CODE (type);
7953   if (TYPE_CODE_STRUCT != code && TYPE_CODE_UNION != code
7954       && TYPE_CODE_ARRAY != code && TYPE_CODE_COMPLEX != code)
7955     return 0;
7956
7957   if (TYPE_CODE_ARRAY == code && TYPE_VECTOR (type))
7958     {
7959       /* Vector values should be returned using ARM registers if they
7960          are not over 16 bytes.  */
7961       return (TYPE_LENGTH (type) > 16);
7962     }
7963
7964   if (gdbarch_tdep (gdbarch)->arm_abi != ARM_ABI_APCS)
7965     {
7966       /* The AAPCS says all aggregates not larger than a word are returned
7967          in a register.  */
7968       if (TYPE_LENGTH (type) <= INT_REGISTER_SIZE)
7969         return 0;
7970
7971       return 1;
7972     }
7973   else
7974     {
7975       int nRc;
7976
7977       /* All aggregate types that won't fit in a register must be returned
7978          in memory.  */
7979       if (TYPE_LENGTH (type) > INT_REGISTER_SIZE)
7980         return 1;
7981
7982       /* In the ARM ABI, "integer" like aggregate types are returned in
7983          registers.  For an aggregate type to be integer like, its size
7984          must be less than or equal to INT_REGISTER_SIZE and the
7985          offset of each addressable subfield must be zero.  Note that bit
7986          fields are not addressable, and all addressable subfields of
7987          unions always start at offset zero.
7988
7989          This function is based on the behaviour of GCC 2.95.1.
7990          See: gcc/arm.c: arm_return_in_memory() for details.
7991
7992          Note: All versions of GCC before GCC 2.95.2 do not set up the
7993          parameters correctly for a function returning the following
7994          structure: struct { float f;}; This should be returned in memory,
7995          not a register.  Richard Earnshaw sent me a patch, but I do not
7996          know of any way to detect if a function like the above has been
7997          compiled with the correct calling convention.  */
7998
7999       /* Assume all other aggregate types can be returned in a register.
8000          Run a check for structures, unions and arrays.  */
8001       nRc = 0;
8002
8003       if ((TYPE_CODE_STRUCT == code) || (TYPE_CODE_UNION == code))
8004         {
8005           int i;
8006           /* Need to check if this struct/union is "integer" like.  For
8007              this to be true, its size must be less than or equal to
8008              INT_REGISTER_SIZE and the offset of each addressable
8009              subfield must be zero.  Note that bit fields are not
8010              addressable, and unions always start at offset zero.  If any
8011              of the subfields is a floating point type, the struct/union
8012              cannot be an integer type.  */
8013
8014           /* For each field in the object, check:
8015              1) Is it FP? --> yes, nRc = 1;
8016              2) Is it addressable (bitpos != 0) and
8017              not packed (bitsize == 0)?
8018              --> yes, nRc = 1
8019           */
8020
8021           for (i = 0; i < TYPE_NFIELDS (type); i++)
8022             {
8023               enum type_code field_type_code;
8024
8025               field_type_code
8026                 = TYPE_CODE (check_typedef (TYPE_FIELD_TYPE (type,
8027                                                              i)));
8028
8029               /* Is it a floating point type field?  */
8030               if (field_type_code == TYPE_CODE_FLT)
8031                 {
8032                   nRc = 1;
8033                   break;
8034                 }
8035
8036               /* If bitpos != 0, then we have to care about it.  */
8037               if (TYPE_FIELD_BITPOS (type, i) != 0)
8038                 {
8039                   /* Bitfields are not addressable.  If the field bitsize is 
8040                      zero, then the field is not packed.  Hence it cannot be
8041                      a bitfield or any other packed type.  */
8042                   if (TYPE_FIELD_BITSIZE (type, i) == 0)
8043                     {
8044                       nRc = 1;
8045                       break;
8046                     }
8047                 }
8048             }
8049         }
8050
8051       return nRc;
8052     }
8053 }
8054
8055 /* Write into appropriate registers a function return value of type
8056    TYPE, given in virtual format.  */
8057
8058 static void
8059 arm_store_return_value (struct type *type, struct regcache *regs,
8060                         const gdb_byte *valbuf)
8061 {
8062   struct gdbarch *gdbarch = regs->arch ();
8063   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
8064
8065   if (TYPE_CODE (type) == TYPE_CODE_FLT)
8066     {
8067       gdb_byte buf[FP_REGISTER_SIZE];
8068
8069       switch (gdbarch_tdep (gdbarch)->fp_model)
8070         {
8071         case ARM_FLOAT_FPA:
8072
8073           target_float_convert (valbuf, type, buf, arm_ext_type (gdbarch));
8074           regs->cooked_write (ARM_F0_REGNUM, buf);
8075           break;
8076
8077         case ARM_FLOAT_SOFT_FPA:
8078         case ARM_FLOAT_SOFT_VFP:
8079           /* ARM_FLOAT_VFP can arise if this is a variadic function so
8080              not using the VFP ABI code.  */
8081         case ARM_FLOAT_VFP:
8082           regs->cooked_write (ARM_A1_REGNUM, valbuf);
8083           if (TYPE_LENGTH (type) > 4)
8084             regs->cooked_write (ARM_A1_REGNUM + 1, valbuf + INT_REGISTER_SIZE);
8085           break;
8086
8087         default:
8088           internal_error (__FILE__, __LINE__,
8089                           _("arm_store_return_value: Floating "
8090                             "point model not supported"));
8091           break;
8092         }
8093     }
8094   else if (TYPE_CODE (type) == TYPE_CODE_INT
8095            || TYPE_CODE (type) == TYPE_CODE_CHAR
8096            || TYPE_CODE (type) == TYPE_CODE_BOOL
8097            || TYPE_CODE (type) == TYPE_CODE_PTR
8098            || TYPE_IS_REFERENCE (type)
8099            || TYPE_CODE (type) == TYPE_CODE_ENUM)
8100     {
8101       if (TYPE_LENGTH (type) <= 4)
8102         {
8103           /* Values of one word or less are zero/sign-extended and
8104              returned in r0.  */
8105           bfd_byte tmpbuf[INT_REGISTER_SIZE];
8106           LONGEST val = unpack_long (type, valbuf);
8107
8108           store_signed_integer (tmpbuf, INT_REGISTER_SIZE, byte_order, val);
8109           regs->cooked_write (ARM_A1_REGNUM, tmpbuf);
8110         }
8111       else
8112         {
8113           /* Integral values greater than one word are stored in consecutive
8114              registers starting with r0.  This will always be a multiple of
8115              the regiser size.  */
8116           int len = TYPE_LENGTH (type);
8117           int regno = ARM_A1_REGNUM;
8118
8119           while (len > 0)
8120             {
8121               regs->cooked_write (regno++, valbuf);
8122               len -= INT_REGISTER_SIZE;
8123               valbuf += INT_REGISTER_SIZE;
8124             }
8125         }
8126     }
8127   else
8128     {
8129       /* For a structure or union the behaviour is as if the value had
8130          been stored to word-aligned memory and then loaded into 
8131          registers with 32-bit load instruction(s).  */
8132       int len = TYPE_LENGTH (type);
8133       int regno = ARM_A1_REGNUM;
8134       bfd_byte tmpbuf[INT_REGISTER_SIZE];
8135
8136       while (len > 0)
8137         {
8138           memcpy (tmpbuf, valbuf,
8139                   len > INT_REGISTER_SIZE ? INT_REGISTER_SIZE : len);
8140           regs->cooked_write (regno++, tmpbuf);
8141           len -= INT_REGISTER_SIZE;
8142           valbuf += INT_REGISTER_SIZE;
8143         }
8144     }
8145 }
8146
8147
8148 /* Handle function return values.  */
8149
8150 static enum return_value_convention
8151 arm_return_value (struct gdbarch *gdbarch, struct value *function,
8152                   struct type *valtype, struct regcache *regcache,
8153                   gdb_byte *readbuf, const gdb_byte *writebuf)
8154 {
8155   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
8156   struct type *func_type = function ? value_type (function) : NULL;
8157   enum arm_vfp_cprc_base_type vfp_base_type;
8158   int vfp_base_count;
8159
8160   if (arm_vfp_abi_for_function (gdbarch, func_type)
8161       && arm_vfp_call_candidate (valtype, &vfp_base_type, &vfp_base_count))
8162     {
8163       int reg_char = arm_vfp_cprc_reg_char (vfp_base_type);
8164       int unit_length = arm_vfp_cprc_unit_length (vfp_base_type);
8165       int i;
8166       for (i = 0; i < vfp_base_count; i++)
8167         {
8168           if (reg_char == 'q')
8169             {
8170               if (writebuf)
8171                 arm_neon_quad_write (gdbarch, regcache, i,
8172                                      writebuf + i * unit_length);
8173
8174               if (readbuf)
8175                 arm_neon_quad_read (gdbarch, regcache, i,
8176                                     readbuf + i * unit_length);
8177             }
8178           else
8179             {
8180               char name_buf[4];
8181               int regnum;
8182
8183               xsnprintf (name_buf, sizeof (name_buf), "%c%d", reg_char, i);
8184               regnum = user_reg_map_name_to_regnum (gdbarch, name_buf,
8185                                                     strlen (name_buf));
8186               if (writebuf)
8187                 regcache->cooked_write (regnum, writebuf + i * unit_length);
8188               if (readbuf)
8189                 regcache->cooked_read (regnum, readbuf + i * unit_length);
8190             }
8191         }
8192       return RETURN_VALUE_REGISTER_CONVENTION;
8193     }
8194
8195   if (TYPE_CODE (valtype) == TYPE_CODE_STRUCT
8196       || TYPE_CODE (valtype) == TYPE_CODE_UNION
8197       || TYPE_CODE (valtype) == TYPE_CODE_ARRAY)
8198     {
8199       if (tdep->struct_return == pcc_struct_return
8200           || arm_return_in_memory (gdbarch, valtype))
8201         return RETURN_VALUE_STRUCT_CONVENTION;
8202     }
8203   else if (TYPE_CODE (valtype) == TYPE_CODE_COMPLEX)
8204     {
8205       if (arm_return_in_memory (gdbarch, valtype))
8206         return RETURN_VALUE_STRUCT_CONVENTION;
8207     }
8208
8209   if (writebuf)
8210     arm_store_return_value (valtype, regcache, writebuf);
8211
8212   if (readbuf)
8213     arm_extract_return_value (valtype, regcache, readbuf);
8214
8215   return RETURN_VALUE_REGISTER_CONVENTION;
8216 }
8217
8218
8219 static int
8220 arm_get_longjmp_target (struct frame_info *frame, CORE_ADDR *pc)
8221 {
8222   struct gdbarch *gdbarch = get_frame_arch (frame);
8223   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
8224   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
8225   CORE_ADDR jb_addr;
8226   gdb_byte buf[INT_REGISTER_SIZE];
8227   
8228   jb_addr = get_frame_register_unsigned (frame, ARM_A1_REGNUM);
8229
8230   if (target_read_memory (jb_addr + tdep->jb_pc * tdep->jb_elt_size, buf,
8231                           INT_REGISTER_SIZE))
8232     return 0;
8233
8234   *pc = extract_unsigned_integer (buf, INT_REGISTER_SIZE, byte_order);
8235   return 1;
8236 }
8237
8238 /* Recognize GCC and GNU ld's trampolines.  If we are in a trampoline,
8239    return the target PC.  Otherwise return 0.  */
8240
8241 CORE_ADDR
8242 arm_skip_stub (struct frame_info *frame, CORE_ADDR pc)
8243 {
8244   const char *name;
8245   int namelen;
8246   CORE_ADDR start_addr;
8247
8248   /* Find the starting address and name of the function containing the PC.  */
8249   if (find_pc_partial_function (pc, &name, &start_addr, NULL) == 0)
8250     {
8251       /* Trampoline 'bx reg' doesn't belong to any functions.  Do the
8252          check here.  */
8253       start_addr = arm_skip_bx_reg (frame, pc);
8254       if (start_addr != 0)
8255         return start_addr;
8256
8257       return 0;
8258     }
8259
8260   /* If PC is in a Thumb call or return stub, return the address of the
8261      target PC, which is in a register.  The thunk functions are called
8262      _call_via_xx, where x is the register name.  The possible names
8263      are r0-r9, sl, fp, ip, sp, and lr.  ARM RealView has similar
8264      functions, named __ARM_call_via_r[0-7].  */
8265   if (startswith (name, "_call_via_")
8266       || startswith (name, "__ARM_call_via_"))
8267     {
8268       /* Use the name suffix to determine which register contains the
8269          target PC.  */
8270       static const char *table[15] =
8271       {"r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
8272        "r8", "r9", "sl", "fp", "ip", "sp", "lr"
8273       };
8274       int regno;
8275       int offset = strlen (name) - 2;
8276
8277       for (regno = 0; regno <= 14; regno++)
8278         if (strcmp (&name[offset], table[regno]) == 0)
8279           return get_frame_register_unsigned (frame, regno);
8280     }
8281
8282   /* GNU ld generates __foo_from_arm or __foo_from_thumb for
8283      non-interworking calls to foo.  We could decode the stubs
8284      to find the target but it's easier to use the symbol table.  */
8285   namelen = strlen (name);
8286   if (name[0] == '_' && name[1] == '_'
8287       && ((namelen > 2 + strlen ("_from_thumb")
8288            && startswith (name + namelen - strlen ("_from_thumb"), "_from_thumb"))
8289           || (namelen > 2 + strlen ("_from_arm")
8290               && startswith (name + namelen - strlen ("_from_arm"), "_from_arm"))))
8291     {
8292       char *target_name;
8293       int target_len = namelen - 2;
8294       struct bound_minimal_symbol minsym;
8295       struct objfile *objfile;
8296       struct obj_section *sec;
8297
8298       if (name[namelen - 1] == 'b')
8299         target_len -= strlen ("_from_thumb");
8300       else
8301         target_len -= strlen ("_from_arm");
8302
8303       target_name = (char *) alloca (target_len + 1);
8304       memcpy (target_name, name + 2, target_len);
8305       target_name[target_len] = '\0';
8306
8307       sec = find_pc_section (pc);
8308       objfile = (sec == NULL) ? NULL : sec->objfile;
8309       minsym = lookup_minimal_symbol (target_name, NULL, objfile);
8310       if (minsym.minsym != NULL)
8311         return BMSYMBOL_VALUE_ADDRESS (minsym);
8312       else
8313         return 0;
8314     }
8315
8316   return 0;                     /* not a stub */
8317 }
8318
8319 static void
8320 set_arm_command (const char *args, int from_tty)
8321 {
8322   printf_unfiltered (_("\
8323 \"set arm\" must be followed by an apporpriate subcommand.\n"));
8324   help_list (setarmcmdlist, "set arm ", all_commands, gdb_stdout);
8325 }
8326
8327 static void
8328 show_arm_command (const char *args, int from_tty)
8329 {
8330   cmd_show_list (showarmcmdlist, from_tty, "");
8331 }
8332
8333 static void
8334 arm_update_current_architecture (void)
8335 {
8336   struct gdbarch_info info;
8337
8338   /* If the current architecture is not ARM, we have nothing to do.  */
8339   if (gdbarch_bfd_arch_info (target_gdbarch ())->arch != bfd_arch_arm)
8340     return;
8341
8342   /* Update the architecture.  */
8343   gdbarch_info_init (&info);
8344
8345   if (!gdbarch_update_p (info))
8346     internal_error (__FILE__, __LINE__, _("could not update architecture"));
8347 }
8348
8349 static void
8350 set_fp_model_sfunc (const char *args, int from_tty,
8351                     struct cmd_list_element *c)
8352 {
8353   int fp_model;
8354
8355   for (fp_model = ARM_FLOAT_AUTO; fp_model != ARM_FLOAT_LAST; fp_model++)
8356     if (strcmp (current_fp_model, fp_model_strings[fp_model]) == 0)
8357       {
8358         arm_fp_model = (enum arm_float_model) fp_model;
8359         break;
8360       }
8361
8362   if (fp_model == ARM_FLOAT_LAST)
8363     internal_error (__FILE__, __LINE__, _("Invalid fp model accepted: %s."),
8364                     current_fp_model);
8365
8366   arm_update_current_architecture ();
8367 }
8368
8369 static void
8370 show_fp_model (struct ui_file *file, int from_tty,
8371                struct cmd_list_element *c, const char *value)
8372 {
8373   struct gdbarch_tdep *tdep = gdbarch_tdep (target_gdbarch ());
8374
8375   if (arm_fp_model == ARM_FLOAT_AUTO
8376       && gdbarch_bfd_arch_info (target_gdbarch ())->arch == bfd_arch_arm)
8377     fprintf_filtered (file, _("\
8378 The current ARM floating point model is \"auto\" (currently \"%s\").\n"),
8379                       fp_model_strings[tdep->fp_model]);
8380   else
8381     fprintf_filtered (file, _("\
8382 The current ARM floating point model is \"%s\".\n"),
8383                       fp_model_strings[arm_fp_model]);
8384 }
8385
8386 static void
8387 arm_set_abi (const char *args, int from_tty,
8388              struct cmd_list_element *c)
8389 {
8390   int arm_abi;
8391
8392   for (arm_abi = ARM_ABI_AUTO; arm_abi != ARM_ABI_LAST; arm_abi++)
8393     if (strcmp (arm_abi_string, arm_abi_strings[arm_abi]) == 0)
8394       {
8395         arm_abi_global = (enum arm_abi_kind) arm_abi;
8396         break;
8397       }
8398
8399   if (arm_abi == ARM_ABI_LAST)
8400     internal_error (__FILE__, __LINE__, _("Invalid ABI accepted: %s."),
8401                     arm_abi_string);
8402
8403   arm_update_current_architecture ();
8404 }
8405
8406 static void
8407 arm_show_abi (struct ui_file *file, int from_tty,
8408              struct cmd_list_element *c, const char *value)
8409 {
8410   struct gdbarch_tdep *tdep = gdbarch_tdep (target_gdbarch ());
8411
8412   if (arm_abi_global == ARM_ABI_AUTO
8413       && gdbarch_bfd_arch_info (target_gdbarch ())->arch == bfd_arch_arm)
8414     fprintf_filtered (file, _("\
8415 The current ARM ABI is \"auto\" (currently \"%s\").\n"),
8416                       arm_abi_strings[tdep->arm_abi]);
8417   else
8418     fprintf_filtered (file, _("The current ARM ABI is \"%s\".\n"),
8419                       arm_abi_string);
8420 }
8421
8422 static void
8423 arm_show_fallback_mode (struct ui_file *file, int from_tty,
8424                         struct cmd_list_element *c, const char *value)
8425 {
8426   fprintf_filtered (file,
8427                     _("The current execution mode assumed "
8428                       "(when symbols are unavailable) is \"%s\".\n"),
8429                     arm_fallback_mode_string);
8430 }
8431
8432 static void
8433 arm_show_force_mode (struct ui_file *file, int from_tty,
8434                      struct cmd_list_element *c, const char *value)
8435 {
8436   fprintf_filtered (file,
8437                     _("The current execution mode assumed "
8438                       "(even when symbols are available) is \"%s\".\n"),
8439                     arm_force_mode_string);
8440 }
8441
8442 /* If the user changes the register disassembly style used for info
8443    register and other commands, we have to also switch the style used
8444    in opcodes for disassembly output.  This function is run in the "set
8445    arm disassembly" command, and does that.  */
8446
8447 static void
8448 set_disassembly_style_sfunc (const char *args, int from_tty,
8449                              struct cmd_list_element *c)
8450 {
8451   /* Convert the short style name into the long style name (eg, reg-names-*)
8452      before calling the generic set_disassembler_options() function.  */
8453   std::string long_name = std::string ("reg-names-") + disassembly_style;
8454   set_disassembler_options (&long_name[0]);
8455 }
8456
8457 static void
8458 show_disassembly_style_sfunc (struct ui_file *file, int from_tty,
8459                               struct cmd_list_element *c, const char *value)
8460 {
8461   struct gdbarch *gdbarch = get_current_arch ();
8462   char *options = get_disassembler_options (gdbarch);
8463   const char *style = "";
8464   int len = 0;
8465   const char *opt;
8466
8467   FOR_EACH_DISASSEMBLER_OPTION (opt, options)
8468     if (CONST_STRNEQ (opt, "reg-names-"))
8469       {
8470         style = &opt[strlen ("reg-names-")];
8471         len = strcspn (style, ",");
8472       }
8473
8474   fprintf_unfiltered (file, "The disassembly style is \"%.*s\".\n", len, style);
8475 }
8476 \f
8477 /* Return the ARM register name corresponding to register I.  */
8478 static const char *
8479 arm_register_name (struct gdbarch *gdbarch, int i)
8480 {
8481   const int num_regs = gdbarch_num_regs (gdbarch);
8482
8483   if (gdbarch_tdep (gdbarch)->have_vfp_pseudos
8484       && i >= num_regs && i < num_regs + 32)
8485     {
8486       static const char *const vfp_pseudo_names[] = {
8487         "s0", "s1", "s2", "s3", "s4", "s5", "s6", "s7",
8488         "s8", "s9", "s10", "s11", "s12", "s13", "s14", "s15",
8489         "s16", "s17", "s18", "s19", "s20", "s21", "s22", "s23",
8490         "s24", "s25", "s26", "s27", "s28", "s29", "s30", "s31",
8491       };
8492
8493       return vfp_pseudo_names[i - num_regs];
8494     }
8495
8496   if (gdbarch_tdep (gdbarch)->have_neon_pseudos
8497       && i >= num_regs + 32 && i < num_regs + 32 + 16)
8498     {
8499       static const char *const neon_pseudo_names[] = {
8500         "q0", "q1", "q2", "q3", "q4", "q5", "q6", "q7",
8501         "q8", "q9", "q10", "q11", "q12", "q13", "q14", "q15",
8502       };
8503
8504       return neon_pseudo_names[i - num_regs - 32];
8505     }
8506
8507   if (i >= ARRAY_SIZE (arm_register_names))
8508     /* These registers are only supported on targets which supply
8509        an XML description.  */
8510     return "";
8511
8512   return arm_register_names[i];
8513 }
8514
8515 /* Test whether the coff symbol specific value corresponds to a Thumb
8516    function.  */
8517
8518 static int
8519 coff_sym_is_thumb (int val)
8520 {
8521   return (val == C_THUMBEXT
8522           || val == C_THUMBSTAT
8523           || val == C_THUMBEXTFUNC
8524           || val == C_THUMBSTATFUNC
8525           || val == C_THUMBLABEL);
8526 }
8527
8528 /* arm_coff_make_msymbol_special()
8529    arm_elf_make_msymbol_special()
8530    
8531    These functions test whether the COFF or ELF symbol corresponds to
8532    an address in thumb code, and set a "special" bit in a minimal
8533    symbol to indicate that it does.  */
8534    
8535 static void
8536 arm_elf_make_msymbol_special(asymbol *sym, struct minimal_symbol *msym)
8537 {
8538   elf_symbol_type *elfsym = (elf_symbol_type *) sym;
8539
8540   if (ARM_GET_SYM_BRANCH_TYPE (elfsym->internal_elf_sym.st_target_internal)
8541       == ST_BRANCH_TO_THUMB)
8542     MSYMBOL_SET_SPECIAL (msym);
8543 }
8544
8545 static void
8546 arm_coff_make_msymbol_special(int val, struct minimal_symbol *msym)
8547 {
8548   if (coff_sym_is_thumb (val))
8549     MSYMBOL_SET_SPECIAL (msym);
8550 }
8551
8552 static void
8553 arm_objfile_data_free (struct objfile *objfile, void *arg)
8554 {
8555   struct arm_per_objfile *data = (struct arm_per_objfile *) arg;
8556   unsigned int i;
8557
8558   for (i = 0; i < objfile->obfd->section_count; i++)
8559     VEC_free (arm_mapping_symbol_s, data->section_maps[i]);
8560 }
8561
8562 static void
8563 arm_record_special_symbol (struct gdbarch *gdbarch, struct objfile *objfile,
8564                            asymbol *sym)
8565 {
8566   const char *name = bfd_asymbol_name (sym);
8567   struct arm_per_objfile *data;
8568   VEC(arm_mapping_symbol_s) **map_p;
8569   struct arm_mapping_symbol new_map_sym;
8570
8571   gdb_assert (name[0] == '$');
8572   if (name[1] != 'a' && name[1] != 't' && name[1] != 'd')
8573     return;
8574
8575   data = (struct arm_per_objfile *) objfile_data (objfile,
8576                                                   arm_objfile_data_key);
8577   if (data == NULL)
8578     {
8579       data = OBSTACK_ZALLOC (&objfile->objfile_obstack,
8580                              struct arm_per_objfile);
8581       set_objfile_data (objfile, arm_objfile_data_key, data);
8582       data->section_maps = OBSTACK_CALLOC (&objfile->objfile_obstack,
8583                                            objfile->obfd->section_count,
8584                                            VEC(arm_mapping_symbol_s) *);
8585     }
8586   map_p = &data->section_maps[bfd_get_section (sym)->index];
8587
8588   new_map_sym.value = sym->value;
8589   new_map_sym.type = name[1];
8590
8591   /* Assume that most mapping symbols appear in order of increasing
8592      value.  If they were randomly distributed, it would be faster to
8593      always push here and then sort at first use.  */
8594   if (!VEC_empty (arm_mapping_symbol_s, *map_p))
8595     {
8596       struct arm_mapping_symbol *prev_map_sym;
8597
8598       prev_map_sym = VEC_last (arm_mapping_symbol_s, *map_p);
8599       if (prev_map_sym->value >= sym->value)
8600         {
8601           unsigned int idx;
8602           idx = VEC_lower_bound (arm_mapping_symbol_s, *map_p, &new_map_sym,
8603                                  arm_compare_mapping_symbols);
8604           VEC_safe_insert (arm_mapping_symbol_s, *map_p, idx, &new_map_sym);
8605           return;
8606         }
8607     }
8608
8609   VEC_safe_push (arm_mapping_symbol_s, *map_p, &new_map_sym);
8610 }
8611
8612 static void
8613 arm_write_pc (struct regcache *regcache, CORE_ADDR pc)
8614 {
8615   struct gdbarch *gdbarch = regcache->arch ();
8616   regcache_cooked_write_unsigned (regcache, ARM_PC_REGNUM, pc);
8617
8618   /* If necessary, set the T bit.  */
8619   if (arm_apcs_32)
8620     {
8621       ULONGEST val, t_bit;
8622       regcache_cooked_read_unsigned (regcache, ARM_PS_REGNUM, &val);
8623       t_bit = arm_psr_thumb_bit (gdbarch);
8624       if (arm_pc_is_thumb (gdbarch, pc))
8625         regcache_cooked_write_unsigned (regcache, ARM_PS_REGNUM,
8626                                         val | t_bit);
8627       else
8628         regcache_cooked_write_unsigned (regcache, ARM_PS_REGNUM,
8629                                         val & ~t_bit);
8630     }
8631 }
8632
8633 /* Read the contents of a NEON quad register, by reading from two
8634    double registers.  This is used to implement the quad pseudo
8635    registers, and for argument passing in case the quad registers are
8636    missing; vectors are passed in quad registers when using the VFP
8637    ABI, even if a NEON unit is not present.  REGNUM is the index of
8638    the quad register, in [0, 15].  */
8639
8640 static enum register_status
8641 arm_neon_quad_read (struct gdbarch *gdbarch, readable_regcache *regcache,
8642                     int regnum, gdb_byte *buf)
8643 {
8644   char name_buf[4];
8645   gdb_byte reg_buf[8];
8646   int offset, double_regnum;
8647   enum register_status status;
8648
8649   xsnprintf (name_buf, sizeof (name_buf), "d%d", regnum << 1);
8650   double_regnum = user_reg_map_name_to_regnum (gdbarch, name_buf,
8651                                                strlen (name_buf));
8652
8653   /* d0 is always the least significant half of q0.  */
8654   if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
8655     offset = 8;
8656   else
8657     offset = 0;
8658
8659   status = regcache->raw_read (double_regnum, reg_buf);
8660   if (status != REG_VALID)
8661     return status;
8662   memcpy (buf + offset, reg_buf, 8);
8663
8664   offset = 8 - offset;
8665   status = regcache->raw_read (double_regnum + 1, reg_buf);
8666   if (status != REG_VALID)
8667     return status;
8668   memcpy (buf + offset, reg_buf, 8);
8669
8670   return REG_VALID;
8671 }
8672
8673 static enum register_status
8674 arm_pseudo_read (struct gdbarch *gdbarch, readable_regcache *regcache,
8675                  int regnum, gdb_byte *buf)
8676 {
8677   const int num_regs = gdbarch_num_regs (gdbarch);
8678   char name_buf[4];
8679   gdb_byte reg_buf[8];
8680   int offset, double_regnum;
8681
8682   gdb_assert (regnum >= num_regs);
8683   regnum -= num_regs;
8684
8685   if (gdbarch_tdep (gdbarch)->have_neon_pseudos && regnum >= 32 && regnum < 48)
8686     /* Quad-precision register.  */
8687     return arm_neon_quad_read (gdbarch, regcache, regnum - 32, buf);
8688   else
8689     {
8690       enum register_status status;
8691
8692       /* Single-precision register.  */
8693       gdb_assert (regnum < 32);
8694
8695       /* s0 is always the least significant half of d0.  */
8696       if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
8697         offset = (regnum & 1) ? 0 : 4;
8698       else
8699         offset = (regnum & 1) ? 4 : 0;
8700
8701       xsnprintf (name_buf, sizeof (name_buf), "d%d", regnum >> 1);
8702       double_regnum = user_reg_map_name_to_regnum (gdbarch, name_buf,
8703                                                    strlen (name_buf));
8704
8705       status = regcache->raw_read (double_regnum, reg_buf);
8706       if (status == REG_VALID)
8707         memcpy (buf, reg_buf + offset, 4);
8708       return status;
8709     }
8710 }
8711
8712 /* Store the contents of BUF to a NEON quad register, by writing to
8713    two double registers.  This is used to implement the quad pseudo
8714    registers, and for argument passing in case the quad registers are
8715    missing; vectors are passed in quad registers when using the VFP
8716    ABI, even if a NEON unit is not present.  REGNUM is the index
8717    of the quad register, in [0, 15].  */
8718
8719 static void
8720 arm_neon_quad_write (struct gdbarch *gdbarch, struct regcache *regcache,
8721                      int regnum, const gdb_byte *buf)
8722 {
8723   char name_buf[4];
8724   int offset, double_regnum;
8725
8726   xsnprintf (name_buf, sizeof (name_buf), "d%d", regnum << 1);
8727   double_regnum = user_reg_map_name_to_regnum (gdbarch, name_buf,
8728                                                strlen (name_buf));
8729
8730   /* d0 is always the least significant half of q0.  */
8731   if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
8732     offset = 8;
8733   else
8734     offset = 0;
8735
8736   regcache->raw_write (double_regnum, buf + offset);
8737   offset = 8 - offset;
8738   regcache->raw_write (double_regnum + 1, buf + offset);
8739 }
8740
8741 static void
8742 arm_pseudo_write (struct gdbarch *gdbarch, struct regcache *regcache,
8743                   int regnum, const gdb_byte *buf)
8744 {
8745   const int num_regs = gdbarch_num_regs (gdbarch);
8746   char name_buf[4];
8747   gdb_byte reg_buf[8];
8748   int offset, double_regnum;
8749
8750   gdb_assert (regnum >= num_regs);
8751   regnum -= num_regs;
8752
8753   if (gdbarch_tdep (gdbarch)->have_neon_pseudos && regnum >= 32 && regnum < 48)
8754     /* Quad-precision register.  */
8755     arm_neon_quad_write (gdbarch, regcache, regnum - 32, buf);
8756   else
8757     {
8758       /* Single-precision register.  */
8759       gdb_assert (regnum < 32);
8760
8761       /* s0 is always the least significant half of d0.  */
8762       if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
8763         offset = (regnum & 1) ? 0 : 4;
8764       else
8765         offset = (regnum & 1) ? 4 : 0;
8766
8767       xsnprintf (name_buf, sizeof (name_buf), "d%d", regnum >> 1);
8768       double_regnum = user_reg_map_name_to_regnum (gdbarch, name_buf,
8769                                                    strlen (name_buf));
8770
8771       regcache->raw_read (double_regnum, reg_buf);
8772       memcpy (reg_buf + offset, buf, 4);
8773       regcache->raw_write (double_regnum, reg_buf);
8774     }
8775 }
8776
8777 static struct value *
8778 value_of_arm_user_reg (struct frame_info *frame, const void *baton)
8779 {
8780   const int *reg_p = (const int *) baton;
8781   return value_of_register (*reg_p, frame);
8782 }
8783 \f
8784 static enum gdb_osabi
8785 arm_elf_osabi_sniffer (bfd *abfd)
8786 {
8787   unsigned int elfosabi;
8788   enum gdb_osabi osabi = GDB_OSABI_UNKNOWN;
8789
8790   elfosabi = elf_elfheader (abfd)->e_ident[EI_OSABI];
8791
8792   if (elfosabi == ELFOSABI_ARM)
8793     /* GNU tools use this value.  Check note sections in this case,
8794        as well.  */
8795     bfd_map_over_sections (abfd,
8796                            generic_elf_osabi_sniff_abi_tag_sections, 
8797                            &osabi);
8798
8799   /* Anything else will be handled by the generic ELF sniffer.  */
8800   return osabi;
8801 }
8802
8803 static int
8804 arm_register_reggroup_p (struct gdbarch *gdbarch, int regnum,
8805                           struct reggroup *group)
8806 {
8807   /* FPS register's type is INT, but belongs to float_reggroup.  Beside
8808      this, FPS register belongs to save_regroup, restore_reggroup, and
8809      all_reggroup, of course.  */
8810   if (regnum == ARM_FPS_REGNUM)
8811     return (group == float_reggroup
8812             || group == save_reggroup
8813             || group == restore_reggroup
8814             || group == all_reggroup);
8815   else
8816     return default_register_reggroup_p (gdbarch, regnum, group);
8817 }
8818
8819 \f
8820 /* For backward-compatibility we allow two 'g' packet lengths with
8821    the remote protocol depending on whether FPA registers are
8822    supplied.  M-profile targets do not have FPA registers, but some
8823    stubs already exist in the wild which use a 'g' packet which
8824    supplies them albeit with dummy values.  The packet format which
8825    includes FPA registers should be considered deprecated for
8826    M-profile targets.  */
8827
8828 static void
8829 arm_register_g_packet_guesses (struct gdbarch *gdbarch)
8830 {
8831   if (gdbarch_tdep (gdbarch)->is_m)
8832     {
8833       /* If we know from the executable this is an M-profile target,
8834          cater for remote targets whose register set layout is the
8835          same as the FPA layout.  */
8836       register_remote_g_packet_guess (gdbarch,
8837                                       /* r0-r12,sp,lr,pc; f0-f7; fps,xpsr */
8838                                       (16 * INT_REGISTER_SIZE)
8839                                       + (8 * FP_REGISTER_SIZE)
8840                                       + (2 * INT_REGISTER_SIZE),
8841                                       tdesc_arm_with_m_fpa_layout);
8842
8843       /* The regular M-profile layout.  */
8844       register_remote_g_packet_guess (gdbarch,
8845                                       /* r0-r12,sp,lr,pc; xpsr */
8846                                       (16 * INT_REGISTER_SIZE)
8847                                       + INT_REGISTER_SIZE,
8848                                       tdesc_arm_with_m);
8849
8850       /* M-profile plus M4F VFP.  */
8851       register_remote_g_packet_guess (gdbarch,
8852                                       /* r0-r12,sp,lr,pc; d0-d15; fpscr,xpsr */
8853                                       (16 * INT_REGISTER_SIZE)
8854                                       + (16 * VFP_REGISTER_SIZE)
8855                                       + (2 * INT_REGISTER_SIZE),
8856                                       tdesc_arm_with_m_vfp_d16);
8857     }
8858
8859   /* Otherwise we don't have a useful guess.  */
8860 }
8861
8862 /* Implement the code_of_frame_writable gdbarch method.  */
8863
8864 static int
8865 arm_code_of_frame_writable (struct gdbarch *gdbarch, struct frame_info *frame)
8866 {
8867   if (gdbarch_tdep (gdbarch)->is_m
8868       && get_frame_type (frame) == SIGTRAMP_FRAME)
8869     {
8870       /* M-profile exception frames return to some magic PCs, where
8871          isn't writable at all.  */
8872       return 0;
8873     }
8874   else
8875     return 1;
8876 }
8877
8878 \f
8879 /* Initialize the current architecture based on INFO.  If possible,
8880    re-use an architecture from ARCHES, which is a list of
8881    architectures already created during this debugging session.
8882
8883    Called e.g. at program startup, when reading a core file, and when
8884    reading a binary file.  */
8885
8886 static struct gdbarch *
8887 arm_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
8888 {
8889   struct gdbarch_tdep *tdep;
8890   struct gdbarch *gdbarch;
8891   struct gdbarch_list *best_arch;
8892   enum arm_abi_kind arm_abi = arm_abi_global;
8893   enum arm_float_model fp_model = arm_fp_model;
8894   struct tdesc_arch_data *tdesc_data = NULL;
8895   int i, is_m = 0;
8896   int vfp_register_count = 0, have_vfp_pseudos = 0, have_neon_pseudos = 0;
8897   int have_wmmx_registers = 0;
8898   int have_neon = 0;
8899   int have_fpa_registers = 1;
8900   const struct target_desc *tdesc = info.target_desc;
8901
8902   /* If we have an object to base this architecture on, try to determine
8903      its ABI.  */
8904
8905   if (arm_abi == ARM_ABI_AUTO && info.abfd != NULL)
8906     {
8907       int ei_osabi, e_flags;
8908
8909       switch (bfd_get_flavour (info.abfd))
8910         {
8911         case bfd_target_coff_flavour:
8912           /* Assume it's an old APCS-style ABI.  */
8913           /* XXX WinCE?  */
8914           arm_abi = ARM_ABI_APCS;
8915           break;
8916
8917         case bfd_target_elf_flavour:
8918           ei_osabi = elf_elfheader (info.abfd)->e_ident[EI_OSABI];
8919           e_flags = elf_elfheader (info.abfd)->e_flags;
8920
8921           if (ei_osabi == ELFOSABI_ARM)
8922             {
8923               /* GNU tools used to use this value, but do not for EABI
8924                  objects.  There's nowhere to tag an EABI version
8925                  anyway, so assume APCS.  */
8926               arm_abi = ARM_ABI_APCS;
8927             }
8928           else if (ei_osabi == ELFOSABI_NONE || ei_osabi == ELFOSABI_GNU)
8929             {
8930               int eabi_ver = EF_ARM_EABI_VERSION (e_flags);
8931
8932               switch (eabi_ver)
8933                 {
8934                 case EF_ARM_EABI_UNKNOWN:
8935                   /* Assume GNU tools.  */
8936                   arm_abi = ARM_ABI_APCS;
8937                   break;
8938
8939                 case EF_ARM_EABI_VER4:
8940                 case EF_ARM_EABI_VER5:
8941                   arm_abi = ARM_ABI_AAPCS;
8942                   /* EABI binaries default to VFP float ordering.
8943                      They may also contain build attributes that can
8944                      be used to identify if the VFP argument-passing
8945                      ABI is in use.  */
8946                   if (fp_model == ARM_FLOAT_AUTO)
8947                     {
8948 #ifdef HAVE_ELF
8949                       switch (bfd_elf_get_obj_attr_int (info.abfd,
8950                                                         OBJ_ATTR_PROC,
8951                                                         Tag_ABI_VFP_args))
8952                         {
8953                         case AEABI_VFP_args_base:
8954                           /* "The user intended FP parameter/result
8955                              passing to conform to AAPCS, base
8956                              variant".  */
8957                           fp_model = ARM_FLOAT_SOFT_VFP;
8958                           break;
8959                         case AEABI_VFP_args_vfp:
8960                           /* "The user intended FP parameter/result
8961                              passing to conform to AAPCS, VFP
8962                              variant".  */
8963                           fp_model = ARM_FLOAT_VFP;
8964                           break;
8965                         case AEABI_VFP_args_toolchain:
8966                           /* "The user intended FP parameter/result
8967                              passing to conform to tool chain-specific
8968                              conventions" - we don't know any such
8969                              conventions, so leave it as "auto".  */
8970                           break;
8971                         case AEABI_VFP_args_compatible:
8972                           /* "Code is compatible with both the base
8973                              and VFP variants; the user did not permit
8974                              non-variadic functions to pass FP
8975                              parameters/results" - leave it as
8976                              "auto".  */
8977                           break;
8978                         default:
8979                           /* Attribute value not mentioned in the
8980                              November 2012 ABI, so leave it as
8981                              "auto".  */
8982                           break;
8983                         }
8984 #else
8985                       fp_model = ARM_FLOAT_SOFT_VFP;
8986 #endif
8987                     }
8988                   break;
8989
8990                 default:
8991                   /* Leave it as "auto".  */
8992                   warning (_("unknown ARM EABI version 0x%x"), eabi_ver);
8993                   break;
8994                 }
8995
8996 #ifdef HAVE_ELF
8997               /* Detect M-profile programs.  This only works if the
8998                  executable file includes build attributes; GCC does
8999                  copy them to the executable, but e.g. RealView does
9000                  not.  */
9001               int attr_arch
9002                 = bfd_elf_get_obj_attr_int (info.abfd, OBJ_ATTR_PROC,
9003                                             Tag_CPU_arch);
9004               int attr_profile
9005                 = bfd_elf_get_obj_attr_int (info.abfd, OBJ_ATTR_PROC,
9006                                             Tag_CPU_arch_profile);
9007
9008               /* GCC specifies the profile for v6-M; RealView only
9009                  specifies the profile for architectures starting with
9010                  V7 (as opposed to architectures with a tag
9011                  numerically greater than TAG_CPU_ARCH_V7).  */
9012               if (!tdesc_has_registers (tdesc)
9013                   && (attr_arch == TAG_CPU_ARCH_V6_M
9014                       || attr_arch == TAG_CPU_ARCH_V6S_M
9015                       || attr_profile == 'M'))
9016                 is_m = 1;
9017 #endif
9018             }
9019
9020           if (fp_model == ARM_FLOAT_AUTO)
9021             {
9022               switch (e_flags & (EF_ARM_SOFT_FLOAT | EF_ARM_VFP_FLOAT))
9023                 {
9024                 case 0:
9025                   /* Leave it as "auto".  Strictly speaking this case
9026                      means FPA, but almost nobody uses that now, and
9027                      many toolchains fail to set the appropriate bits
9028                      for the floating-point model they use.  */
9029                   break;
9030                 case EF_ARM_SOFT_FLOAT:
9031                   fp_model = ARM_FLOAT_SOFT_FPA;
9032                   break;
9033                 case EF_ARM_VFP_FLOAT:
9034                   fp_model = ARM_FLOAT_VFP;
9035                   break;
9036                 case EF_ARM_SOFT_FLOAT | EF_ARM_VFP_FLOAT:
9037                   fp_model = ARM_FLOAT_SOFT_VFP;
9038                   break;
9039                 }
9040             }
9041
9042           if (e_flags & EF_ARM_BE8)
9043             info.byte_order_for_code = BFD_ENDIAN_LITTLE;
9044
9045           break;
9046
9047         default:
9048           /* Leave it as "auto".  */
9049           break;
9050         }
9051     }
9052
9053   /* Check any target description for validity.  */
9054   if (tdesc_has_registers (tdesc))
9055     {
9056       /* For most registers we require GDB's default names; but also allow
9057          the numeric names for sp / lr / pc, as a convenience.  */
9058       static const char *const arm_sp_names[] = { "r13", "sp", NULL };
9059       static const char *const arm_lr_names[] = { "r14", "lr", NULL };
9060       static const char *const arm_pc_names[] = { "r15", "pc", NULL };
9061
9062       const struct tdesc_feature *feature;
9063       int valid_p;
9064
9065       feature = tdesc_find_feature (tdesc,
9066                                     "org.gnu.gdb.arm.core");
9067       if (feature == NULL)
9068         {
9069           feature = tdesc_find_feature (tdesc,
9070                                         "org.gnu.gdb.arm.m-profile");
9071           if (feature == NULL)
9072             return NULL;
9073           else
9074             is_m = 1;
9075         }
9076
9077       tdesc_data = tdesc_data_alloc ();
9078
9079       valid_p = 1;
9080       for (i = 0; i < ARM_SP_REGNUM; i++)
9081         valid_p &= tdesc_numbered_register (feature, tdesc_data, i,
9082                                             arm_register_names[i]);
9083       valid_p &= tdesc_numbered_register_choices (feature, tdesc_data,
9084                                                   ARM_SP_REGNUM,
9085                                                   arm_sp_names);
9086       valid_p &= tdesc_numbered_register_choices (feature, tdesc_data,
9087                                                   ARM_LR_REGNUM,
9088                                                   arm_lr_names);
9089       valid_p &= tdesc_numbered_register_choices (feature, tdesc_data,
9090                                                   ARM_PC_REGNUM,
9091                                                   arm_pc_names);
9092       if (is_m)
9093         valid_p &= tdesc_numbered_register (feature, tdesc_data,
9094                                             ARM_PS_REGNUM, "xpsr");
9095       else
9096         valid_p &= tdesc_numbered_register (feature, tdesc_data,
9097                                             ARM_PS_REGNUM, "cpsr");
9098
9099       if (!valid_p)
9100         {
9101           tdesc_data_cleanup (tdesc_data);
9102           return NULL;
9103         }
9104
9105       feature = tdesc_find_feature (tdesc,
9106                                     "org.gnu.gdb.arm.fpa");
9107       if (feature != NULL)
9108         {
9109           valid_p = 1;
9110           for (i = ARM_F0_REGNUM; i <= ARM_FPS_REGNUM; i++)
9111             valid_p &= tdesc_numbered_register (feature, tdesc_data, i,
9112                                                 arm_register_names[i]);
9113           if (!valid_p)
9114             {
9115               tdesc_data_cleanup (tdesc_data);
9116               return NULL;
9117             }
9118         }
9119       else
9120         have_fpa_registers = 0;
9121
9122       feature = tdesc_find_feature (tdesc,
9123                                     "org.gnu.gdb.xscale.iwmmxt");
9124       if (feature != NULL)
9125         {
9126           static const char *const iwmmxt_names[] = {
9127             "wR0", "wR1", "wR2", "wR3", "wR4", "wR5", "wR6", "wR7",
9128             "wR8", "wR9", "wR10", "wR11", "wR12", "wR13", "wR14", "wR15",
9129             "wCID", "wCon", "wCSSF", "wCASF", "", "", "", "",
9130             "wCGR0", "wCGR1", "wCGR2", "wCGR3", "", "", "", "",
9131           };
9132
9133           valid_p = 1;
9134           for (i = ARM_WR0_REGNUM; i <= ARM_WR15_REGNUM; i++)
9135             valid_p
9136               &= tdesc_numbered_register (feature, tdesc_data, i,
9137                                           iwmmxt_names[i - ARM_WR0_REGNUM]);
9138
9139           /* Check for the control registers, but do not fail if they
9140              are missing.  */
9141           for (i = ARM_WC0_REGNUM; i <= ARM_WCASF_REGNUM; i++)
9142             tdesc_numbered_register (feature, tdesc_data, i,
9143                                      iwmmxt_names[i - ARM_WR0_REGNUM]);
9144
9145           for (i = ARM_WCGR0_REGNUM; i <= ARM_WCGR3_REGNUM; i++)
9146             valid_p
9147               &= tdesc_numbered_register (feature, tdesc_data, i,
9148                                           iwmmxt_names[i - ARM_WR0_REGNUM]);
9149
9150           if (!valid_p)
9151             {
9152               tdesc_data_cleanup (tdesc_data);
9153               return NULL;
9154             }
9155
9156           have_wmmx_registers = 1;
9157         }
9158
9159       /* If we have a VFP unit, check whether the single precision registers
9160          are present.  If not, then we will synthesize them as pseudo
9161          registers.  */
9162       feature = tdesc_find_feature (tdesc,
9163                                     "org.gnu.gdb.arm.vfp");
9164       if (feature != NULL)
9165         {
9166           static const char *const vfp_double_names[] = {
9167             "d0", "d1", "d2", "d3", "d4", "d5", "d6", "d7",
9168             "d8", "d9", "d10", "d11", "d12", "d13", "d14", "d15",
9169             "d16", "d17", "d18", "d19", "d20", "d21", "d22", "d23",
9170             "d24", "d25", "d26", "d27", "d28", "d29", "d30", "d31",
9171           };
9172
9173           /* Require the double precision registers.  There must be either
9174              16 or 32.  */
9175           valid_p = 1;
9176           for (i = 0; i < 32; i++)
9177             {
9178               valid_p &= tdesc_numbered_register (feature, tdesc_data,
9179                                                   ARM_D0_REGNUM + i,
9180                                                   vfp_double_names[i]);
9181               if (!valid_p)
9182                 break;
9183             }
9184           if (!valid_p && i == 16)
9185             valid_p = 1;
9186
9187           /* Also require FPSCR.  */
9188           valid_p &= tdesc_numbered_register (feature, tdesc_data,
9189                                               ARM_FPSCR_REGNUM, "fpscr");
9190           if (!valid_p)
9191             {
9192               tdesc_data_cleanup (tdesc_data);
9193               return NULL;
9194             }
9195
9196           if (tdesc_unnumbered_register (feature, "s0") == 0)
9197             have_vfp_pseudos = 1;
9198
9199           vfp_register_count = i;
9200
9201           /* If we have VFP, also check for NEON.  The architecture allows
9202              NEON without VFP (integer vector operations only), but GDB
9203              does not support that.  */
9204           feature = tdesc_find_feature (tdesc,
9205                                         "org.gnu.gdb.arm.neon");
9206           if (feature != NULL)
9207             {
9208               /* NEON requires 32 double-precision registers.  */
9209               if (i != 32)
9210                 {
9211                   tdesc_data_cleanup (tdesc_data);
9212                   return NULL;
9213                 }
9214
9215               /* If there are quad registers defined by the stub, use
9216                  their type; otherwise (normally) provide them with
9217                  the default type.  */
9218               if (tdesc_unnumbered_register (feature, "q0") == 0)
9219                 have_neon_pseudos = 1;
9220
9221               have_neon = 1;
9222             }
9223         }
9224     }
9225
9226   /* If there is already a candidate, use it.  */
9227   for (best_arch = gdbarch_list_lookup_by_info (arches, &info);
9228        best_arch != NULL;
9229        best_arch = gdbarch_list_lookup_by_info (best_arch->next, &info))
9230     {
9231       if (arm_abi != ARM_ABI_AUTO
9232           && arm_abi != gdbarch_tdep (best_arch->gdbarch)->arm_abi)
9233         continue;
9234
9235       if (fp_model != ARM_FLOAT_AUTO
9236           && fp_model != gdbarch_tdep (best_arch->gdbarch)->fp_model)
9237         continue;
9238
9239       /* There are various other properties in tdep that we do not
9240          need to check here: those derived from a target description,
9241          since gdbarches with a different target description are
9242          automatically disqualified.  */
9243
9244       /* Do check is_m, though, since it might come from the binary.  */
9245       if (is_m != gdbarch_tdep (best_arch->gdbarch)->is_m)
9246         continue;
9247
9248       /* Found a match.  */
9249       break;
9250     }
9251
9252   if (best_arch != NULL)
9253     {
9254       if (tdesc_data != NULL)
9255         tdesc_data_cleanup (tdesc_data);
9256       return best_arch->gdbarch;
9257     }
9258
9259   tdep = XCNEW (struct gdbarch_tdep);
9260   gdbarch = gdbarch_alloc (&info, tdep);
9261
9262   /* Record additional information about the architecture we are defining.
9263      These are gdbarch discriminators, like the OSABI.  */
9264   tdep->arm_abi = arm_abi;
9265   tdep->fp_model = fp_model;
9266   tdep->is_m = is_m;
9267   tdep->have_fpa_registers = have_fpa_registers;
9268   tdep->have_wmmx_registers = have_wmmx_registers;
9269   gdb_assert (vfp_register_count == 0
9270               || vfp_register_count == 16
9271               || vfp_register_count == 32);
9272   tdep->vfp_register_count = vfp_register_count;
9273   tdep->have_vfp_pseudos = have_vfp_pseudos;
9274   tdep->have_neon_pseudos = have_neon_pseudos;
9275   tdep->have_neon = have_neon;
9276
9277   arm_register_g_packet_guesses (gdbarch);
9278
9279   /* Breakpoints.  */
9280   switch (info.byte_order_for_code)
9281     {
9282     case BFD_ENDIAN_BIG:
9283       tdep->arm_breakpoint = arm_default_arm_be_breakpoint;
9284       tdep->arm_breakpoint_size = sizeof (arm_default_arm_be_breakpoint);
9285       tdep->thumb_breakpoint = arm_default_thumb_be_breakpoint;
9286       tdep->thumb_breakpoint_size = sizeof (arm_default_thumb_be_breakpoint);
9287
9288       break;
9289
9290     case BFD_ENDIAN_LITTLE:
9291       tdep->arm_breakpoint = arm_default_arm_le_breakpoint;
9292       tdep->arm_breakpoint_size = sizeof (arm_default_arm_le_breakpoint);
9293       tdep->thumb_breakpoint = arm_default_thumb_le_breakpoint;
9294       tdep->thumb_breakpoint_size = sizeof (arm_default_thumb_le_breakpoint);
9295
9296       break;
9297
9298     default:
9299       internal_error (__FILE__, __LINE__,
9300                       _("arm_gdbarch_init: bad byte order for float format"));
9301     }
9302
9303   /* On ARM targets char defaults to unsigned.  */
9304   set_gdbarch_char_signed (gdbarch, 0);
9305
9306   /* wchar_t is unsigned under the AAPCS.  */
9307   if (tdep->arm_abi == ARM_ABI_AAPCS)
9308     set_gdbarch_wchar_signed (gdbarch, 0);
9309   else
9310     set_gdbarch_wchar_signed (gdbarch, 1);
9311
9312   /* Note: for displaced stepping, this includes the breakpoint, and one word
9313      of additional scratch space.  This setting isn't used for anything beside
9314      displaced stepping at present.  */
9315   set_gdbarch_max_insn_length (gdbarch, 4 * DISPLACED_MODIFIED_INSNS);
9316
9317   /* This should be low enough for everything.  */
9318   tdep->lowest_pc = 0x20;
9319   tdep->jb_pc = -1;     /* Longjump support not enabled by default.  */
9320
9321   /* The default, for both APCS and AAPCS, is to return small
9322      structures in registers.  */
9323   tdep->struct_return = reg_struct_return;
9324
9325   set_gdbarch_push_dummy_call (gdbarch, arm_push_dummy_call);
9326   set_gdbarch_frame_align (gdbarch, arm_frame_align);
9327
9328   if (is_m)
9329     set_gdbarch_code_of_frame_writable (gdbarch, arm_code_of_frame_writable);
9330
9331   set_gdbarch_write_pc (gdbarch, arm_write_pc);
9332
9333   frame_base_set_default (gdbarch, &arm_normal_base);
9334
9335   /* Address manipulation.  */
9336   set_gdbarch_addr_bits_remove (gdbarch, arm_addr_bits_remove);
9337
9338   /* Advance PC across function entry code.  */
9339   set_gdbarch_skip_prologue (gdbarch, arm_skip_prologue);
9340
9341   /* Detect whether PC is at a point where the stack has been destroyed.  */
9342   set_gdbarch_stack_frame_destroyed_p (gdbarch, arm_stack_frame_destroyed_p);
9343
9344   /* Skip trampolines.  */
9345   set_gdbarch_skip_trampoline_code (gdbarch, arm_skip_stub);
9346
9347   /* The stack grows downward.  */
9348   set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
9349
9350   /* Breakpoint manipulation.  */
9351   set_gdbarch_breakpoint_kind_from_pc (gdbarch, arm_breakpoint_kind_from_pc);
9352   set_gdbarch_sw_breakpoint_from_kind (gdbarch, arm_sw_breakpoint_from_kind);
9353   set_gdbarch_breakpoint_kind_from_current_state (gdbarch,
9354                                                   arm_breakpoint_kind_from_current_state);
9355
9356   /* Information about registers, etc.  */
9357   set_gdbarch_sp_regnum (gdbarch, ARM_SP_REGNUM);
9358   set_gdbarch_pc_regnum (gdbarch, ARM_PC_REGNUM);
9359   set_gdbarch_num_regs (gdbarch, ARM_NUM_REGS);
9360   set_gdbarch_register_type (gdbarch, arm_register_type);
9361   set_gdbarch_register_reggroup_p (gdbarch, arm_register_reggroup_p);
9362
9363   /* This "info float" is FPA-specific.  Use the generic version if we
9364      do not have FPA.  */
9365   if (gdbarch_tdep (gdbarch)->have_fpa_registers)
9366     set_gdbarch_print_float_info (gdbarch, arm_print_float_info);
9367
9368   /* Internal <-> external register number maps.  */
9369   set_gdbarch_dwarf2_reg_to_regnum (gdbarch, arm_dwarf_reg_to_regnum);
9370   set_gdbarch_register_sim_regno (gdbarch, arm_register_sim_regno);
9371
9372   set_gdbarch_register_name (gdbarch, arm_register_name);
9373
9374   /* Returning results.  */
9375   set_gdbarch_return_value (gdbarch, arm_return_value);
9376
9377   /* Disassembly.  */
9378   set_gdbarch_print_insn (gdbarch, gdb_print_insn_arm);
9379
9380   /* Minsymbol frobbing.  */
9381   set_gdbarch_elf_make_msymbol_special (gdbarch, arm_elf_make_msymbol_special);
9382   set_gdbarch_coff_make_msymbol_special (gdbarch,
9383                                          arm_coff_make_msymbol_special);
9384   set_gdbarch_record_special_symbol (gdbarch, arm_record_special_symbol);
9385
9386   /* Thumb-2 IT block support.  */
9387   set_gdbarch_adjust_breakpoint_address (gdbarch,
9388                                          arm_adjust_breakpoint_address);
9389
9390   /* Virtual tables.  */
9391   set_gdbarch_vbit_in_delta (gdbarch, 1);
9392
9393   /* Hook in the ABI-specific overrides, if they have been registered.  */
9394   gdbarch_init_osabi (info, gdbarch);
9395
9396   dwarf2_frame_set_init_reg (gdbarch, arm_dwarf2_frame_init_reg);
9397
9398   /* Add some default predicates.  */
9399   if (is_m)
9400     frame_unwind_append_unwinder (gdbarch, &arm_m_exception_unwind);
9401   frame_unwind_append_unwinder (gdbarch, &arm_stub_unwind);
9402   dwarf2_append_unwinders (gdbarch);
9403   frame_unwind_append_unwinder (gdbarch, &arm_exidx_unwind);
9404   frame_unwind_append_unwinder (gdbarch, &arm_epilogue_frame_unwind);
9405   frame_unwind_append_unwinder (gdbarch, &arm_prologue_unwind);
9406
9407   /* Now we have tuned the configuration, set a few final things,
9408      based on what the OS ABI has told us.  */
9409
9410   /* If the ABI is not otherwise marked, assume the old GNU APCS.  EABI
9411      binaries are always marked.  */
9412   if (tdep->arm_abi == ARM_ABI_AUTO)
9413     tdep->arm_abi = ARM_ABI_APCS;
9414
9415   /* Watchpoints are not steppable.  */
9416   set_gdbarch_have_nonsteppable_watchpoint (gdbarch, 1);
9417
9418   /* We used to default to FPA for generic ARM, but almost nobody
9419      uses that now, and we now provide a way for the user to force
9420      the model.  So default to the most useful variant.  */
9421   if (tdep->fp_model == ARM_FLOAT_AUTO)
9422     tdep->fp_model = ARM_FLOAT_SOFT_FPA;
9423
9424   if (tdep->jb_pc >= 0)
9425     set_gdbarch_get_longjmp_target (gdbarch, arm_get_longjmp_target);
9426
9427   /* Floating point sizes and format.  */
9428   set_gdbarch_float_format (gdbarch, floatformats_ieee_single);
9429   if (tdep->fp_model == ARM_FLOAT_SOFT_FPA || tdep->fp_model == ARM_FLOAT_FPA)
9430     {
9431       set_gdbarch_double_format
9432         (gdbarch, floatformats_ieee_double_littlebyte_bigword);
9433       set_gdbarch_long_double_format
9434         (gdbarch, floatformats_ieee_double_littlebyte_bigword);
9435     }
9436   else
9437     {
9438       set_gdbarch_double_format (gdbarch, floatformats_ieee_double);
9439       set_gdbarch_long_double_format (gdbarch, floatformats_ieee_double);
9440     }
9441
9442   if (have_vfp_pseudos)
9443     {
9444       /* NOTE: These are the only pseudo registers used by
9445          the ARM target at the moment.  If more are added, a
9446          little more care in numbering will be needed.  */
9447
9448       int num_pseudos = 32;
9449       if (have_neon_pseudos)
9450         num_pseudos += 16;
9451       set_gdbarch_num_pseudo_regs (gdbarch, num_pseudos);
9452       set_gdbarch_pseudo_register_read (gdbarch, arm_pseudo_read);
9453       set_gdbarch_pseudo_register_write (gdbarch, arm_pseudo_write);
9454     }
9455
9456   if (tdesc_data)
9457     {
9458       set_tdesc_pseudo_register_name (gdbarch, arm_register_name);
9459
9460       tdesc_use_registers (gdbarch, tdesc, tdesc_data);
9461
9462       /* Override tdesc_register_type to adjust the types of VFP
9463          registers for NEON.  */
9464       set_gdbarch_register_type (gdbarch, arm_register_type);
9465     }
9466
9467   /* Add standard register aliases.  We add aliases even for those
9468      nanes which are used by the current architecture - it's simpler,
9469      and does no harm, since nothing ever lists user registers.  */
9470   for (i = 0; i < ARRAY_SIZE (arm_register_aliases); i++)
9471     user_reg_add (gdbarch, arm_register_aliases[i].name,
9472                   value_of_arm_user_reg, &arm_register_aliases[i].regnum);
9473
9474   set_gdbarch_disassembler_options (gdbarch, &arm_disassembler_options);
9475   set_gdbarch_valid_disassembler_options (gdbarch, disassembler_options_arm ());
9476
9477   return gdbarch;
9478 }
9479
9480 static void
9481 arm_dump_tdep (struct gdbarch *gdbarch, struct ui_file *file)
9482 {
9483   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
9484
9485   if (tdep == NULL)
9486     return;
9487
9488   fprintf_unfiltered (file, _("arm_dump_tdep: Lowest pc = 0x%lx"),
9489                       (unsigned long) tdep->lowest_pc);
9490 }
9491
9492 #if GDB_SELF_TEST
9493 namespace selftests
9494 {
9495 static void arm_record_test (void);
9496 }
9497 #endif
9498
9499 void
9500 _initialize_arm_tdep (void)
9501 {
9502   long length;
9503   int i, j;
9504   char regdesc[1024], *rdptr = regdesc;
9505   size_t rest = sizeof (regdesc);
9506
9507   gdbarch_register (bfd_arch_arm, arm_gdbarch_init, arm_dump_tdep);
9508
9509   arm_objfile_data_key
9510     = register_objfile_data_with_cleanup (NULL, arm_objfile_data_free);
9511
9512   /* Add ourselves to objfile event chain.  */
9513   gdb::observers::new_objfile.attach (arm_exidx_new_objfile);
9514   arm_exidx_data_key
9515     = register_objfile_data_with_cleanup (NULL, arm_exidx_data_free);
9516
9517   /* Register an ELF OS ABI sniffer for ARM binaries.  */
9518   gdbarch_register_osabi_sniffer (bfd_arch_arm,
9519                                   bfd_target_elf_flavour,
9520                                   arm_elf_osabi_sniffer);
9521
9522   /* Initialize the standard target descriptions.  */
9523   initialize_tdesc_arm_with_m ();
9524   initialize_tdesc_arm_with_m_fpa_layout ();
9525   initialize_tdesc_arm_with_m_vfp_d16 ();
9526   initialize_tdesc_arm_with_iwmmxt ();
9527   initialize_tdesc_arm_with_vfpv2 ();
9528   initialize_tdesc_arm_with_vfpv3 ();
9529   initialize_tdesc_arm_with_neon ();
9530
9531   /* Add root prefix command for all "set arm"/"show arm" commands.  */
9532   add_prefix_cmd ("arm", no_class, set_arm_command,
9533                   _("Various ARM-specific commands."),
9534                   &setarmcmdlist, "set arm ", 0, &setlist);
9535
9536   add_prefix_cmd ("arm", no_class, show_arm_command,
9537                   _("Various ARM-specific commands."),
9538                   &showarmcmdlist, "show arm ", 0, &showlist);
9539
9540
9541   arm_disassembler_options = xstrdup ("reg-names-std");
9542   const disasm_options_t *disasm_options
9543     = &disassembler_options_arm ()->options;
9544   int num_disassembly_styles = 0;
9545   for (i = 0; disasm_options->name[i] != NULL; i++)
9546     if (CONST_STRNEQ (disasm_options->name[i], "reg-names-"))
9547       num_disassembly_styles++;
9548
9549   /* Initialize the array that will be passed to add_setshow_enum_cmd().  */
9550   valid_disassembly_styles = XNEWVEC (const char *,
9551                                       num_disassembly_styles + 1);
9552   for (i = j = 0; disasm_options->name[i] != NULL; i++)
9553     if (CONST_STRNEQ (disasm_options->name[i], "reg-names-"))
9554       {
9555         size_t offset = strlen ("reg-names-");
9556         const char *style = disasm_options->name[i];
9557         valid_disassembly_styles[j++] = &style[offset];
9558         length = snprintf (rdptr, rest, "%s - %s\n", &style[offset],
9559                            disasm_options->description[i]);
9560         rdptr += length;
9561         rest -= length;
9562       }
9563   /* Mark the end of valid options.  */
9564   valid_disassembly_styles[num_disassembly_styles] = NULL;
9565
9566   /* Create the help text.  */
9567   std::string helptext = string_printf ("%s%s%s",
9568                                         _("The valid values are:\n"),
9569                                         regdesc,
9570                                         _("The default is \"std\"."));
9571
9572   add_setshow_enum_cmd("disassembler", no_class,
9573                        valid_disassembly_styles, &disassembly_style,
9574                        _("Set the disassembly style."),
9575                        _("Show the disassembly style."),
9576                        helptext.c_str (),
9577                        set_disassembly_style_sfunc,
9578                        show_disassembly_style_sfunc,
9579                        &setarmcmdlist, &showarmcmdlist);
9580
9581   add_setshow_boolean_cmd ("apcs32", no_class, &arm_apcs_32,
9582                            _("Set usage of ARM 32-bit mode."),
9583                            _("Show usage of ARM 32-bit mode."),
9584                            _("When off, a 26-bit PC will be used."),
9585                            NULL,
9586                            NULL, /* FIXME: i18n: Usage of ARM 32-bit
9587                                     mode is %s.  */
9588                            &setarmcmdlist, &showarmcmdlist);
9589
9590   /* Add a command to allow the user to force the FPU model.  */
9591   add_setshow_enum_cmd ("fpu", no_class, fp_model_strings, &current_fp_model,
9592                         _("Set the floating point type."),
9593                         _("Show the floating point type."),
9594                         _("auto - Determine the FP typefrom the OS-ABI.\n\
9595 softfpa - Software FP, mixed-endian doubles on little-endian ARMs.\n\
9596 fpa - FPA co-processor (GCC compiled).\n\
9597 softvfp - Software FP with pure-endian doubles.\n\
9598 vfp - VFP co-processor."),
9599                         set_fp_model_sfunc, show_fp_model,
9600                         &setarmcmdlist, &showarmcmdlist);
9601
9602   /* Add a command to allow the user to force the ABI.  */
9603   add_setshow_enum_cmd ("abi", class_support, arm_abi_strings, &arm_abi_string,
9604                         _("Set the ABI."),
9605                         _("Show the ABI."),
9606                         NULL, arm_set_abi, arm_show_abi,
9607                         &setarmcmdlist, &showarmcmdlist);
9608
9609   /* Add two commands to allow the user to force the assumed
9610      execution mode.  */
9611   add_setshow_enum_cmd ("fallback-mode", class_support,
9612                         arm_mode_strings, &arm_fallback_mode_string,
9613                         _("Set the mode assumed when symbols are unavailable."),
9614                         _("Show the mode assumed when symbols are unavailable."),
9615                         NULL, NULL, arm_show_fallback_mode,
9616                         &setarmcmdlist, &showarmcmdlist);
9617   add_setshow_enum_cmd ("force-mode", class_support,
9618                         arm_mode_strings, &arm_force_mode_string,
9619                         _("Set the mode assumed even when symbols are available."),
9620                         _("Show the mode assumed even when symbols are available."),
9621                         NULL, NULL, arm_show_force_mode,
9622                         &setarmcmdlist, &showarmcmdlist);
9623
9624   /* Debugging flag.  */
9625   add_setshow_boolean_cmd ("arm", class_maintenance, &arm_debug,
9626                            _("Set ARM debugging."),
9627                            _("Show ARM debugging."),
9628                            _("When on, arm-specific debugging is enabled."),
9629                            NULL,
9630                            NULL, /* FIXME: i18n: "ARM debugging is %s.  */
9631                            &setdebuglist, &showdebuglist);
9632
9633 #if GDB_SELF_TEST
9634   selftests::register_test ("arm-record", selftests::arm_record_test);
9635 #endif
9636
9637 }
9638
9639 /* ARM-reversible process record data structures.  */
9640
9641 #define ARM_INSN_SIZE_BYTES 4    
9642 #define THUMB_INSN_SIZE_BYTES 2
9643 #define THUMB2_INSN_SIZE_BYTES 4
9644
9645
9646 /* Position of the bit within a 32-bit ARM instruction
9647    that defines whether the instruction is a load or store.  */
9648 #define INSN_S_L_BIT_NUM 20
9649
9650 #define REG_ALLOC(REGS, LENGTH, RECORD_BUF) \
9651         do  \
9652           { \
9653             unsigned int reg_len = LENGTH; \
9654             if (reg_len) \
9655               { \
9656                 REGS = XNEWVEC (uint32_t, reg_len); \
9657                 memcpy(&REGS[0], &RECORD_BUF[0], sizeof(uint32_t)*LENGTH); \
9658               } \
9659           } \
9660         while (0)
9661
9662 #define MEM_ALLOC(MEMS, LENGTH, RECORD_BUF) \
9663         do  \
9664           { \
9665             unsigned int mem_len = LENGTH; \
9666             if (mem_len) \
9667             { \
9668               MEMS =  XNEWVEC (struct arm_mem_r, mem_len);  \
9669               memcpy(&MEMS->len, &RECORD_BUF[0], \
9670                      sizeof(struct arm_mem_r) * LENGTH); \
9671             } \
9672           } \
9673           while (0)
9674
9675 /* Checks whether insn is already recorded or yet to be decoded. (boolean expression).  */
9676 #define INSN_RECORDED(ARM_RECORD) \
9677         (0 != (ARM_RECORD)->reg_rec_count || 0 != (ARM_RECORD)->mem_rec_count)
9678
9679 /* ARM memory record structure.  */
9680 struct arm_mem_r
9681 {
9682   uint32_t len;    /* Record length.  */
9683   uint32_t addr;   /* Memory address.  */
9684 };
9685
9686 /* ARM instruction record contains opcode of current insn
9687    and execution state (before entry to decode_insn()),
9688    contains list of to-be-modified registers and
9689    memory blocks (on return from decode_insn()).  */
9690
9691 typedef struct insn_decode_record_t
9692 {
9693   struct gdbarch *gdbarch;
9694   struct regcache *regcache;
9695   CORE_ADDR this_addr;          /* Address of the insn being decoded.  */
9696   uint32_t arm_insn;            /* Should accommodate thumb.  */
9697   uint32_t cond;                /* Condition code.  */
9698   uint32_t opcode;              /* Insn opcode.  */
9699   uint32_t decode;              /* Insn decode bits.  */
9700   uint32_t mem_rec_count;       /* No of mem records.  */
9701   uint32_t reg_rec_count;       /* No of reg records.  */
9702   uint32_t *arm_regs;           /* Registers to be saved for this record.  */
9703   struct arm_mem_r *arm_mems;   /* Memory to be saved for this record.  */
9704 } insn_decode_record;
9705
9706
9707 /* Checks ARM SBZ and SBO mandatory fields.  */
9708
9709 static int
9710 sbo_sbz (uint32_t insn, uint32_t bit_num, uint32_t len, uint32_t sbo)
9711 {
9712   uint32_t ones = bits (insn, bit_num - 1, (bit_num -1) + (len - 1));
9713
9714   if (!len)
9715     return 1;
9716
9717   if (!sbo)
9718     ones = ~ones;
9719
9720   while (ones)
9721     {
9722       if (!(ones & sbo))
9723         {
9724           return 0;
9725         }
9726       ones = ones >> 1;
9727     }
9728   return 1;
9729 }
9730
9731 enum arm_record_result
9732 {
9733   ARM_RECORD_SUCCESS = 0,
9734   ARM_RECORD_FAILURE = 1
9735 };
9736
9737 typedef enum
9738 {
9739   ARM_RECORD_STRH=1,
9740   ARM_RECORD_STRD
9741 } arm_record_strx_t;
9742
9743 typedef enum
9744 {
9745   ARM_RECORD=1,
9746   THUMB_RECORD,
9747   THUMB2_RECORD
9748 } record_type_t;
9749
9750
9751 static int
9752 arm_record_strx (insn_decode_record *arm_insn_r, uint32_t *record_buf, 
9753                  uint32_t *record_buf_mem, arm_record_strx_t str_type)
9754 {
9755
9756   struct regcache *reg_cache = arm_insn_r->regcache;
9757   ULONGEST u_regval[2]= {0};
9758
9759   uint32_t reg_src1 = 0, reg_src2 = 0;
9760   uint32_t immed_high = 0, immed_low = 0,offset_8 = 0, tgt_mem_addr = 0;
9761
9762   arm_insn_r->opcode = bits (arm_insn_r->arm_insn, 21, 24);
9763   arm_insn_r->decode = bits (arm_insn_r->arm_insn, 4, 7);
9764
9765   if (14 == arm_insn_r->opcode || 10 == arm_insn_r->opcode)
9766     {
9767       /* 1) Handle misc store, immediate offset.  */
9768       immed_low = bits (arm_insn_r->arm_insn, 0, 3);
9769       immed_high = bits (arm_insn_r->arm_insn, 8, 11);
9770       reg_src1 = bits (arm_insn_r->arm_insn, 16, 19);
9771       regcache_raw_read_unsigned (reg_cache, reg_src1,
9772                                   &u_regval[0]);
9773       if (ARM_PC_REGNUM == reg_src1)
9774         {
9775           /* If R15 was used as Rn, hence current PC+8.  */
9776           u_regval[0] = u_regval[0] + 8;
9777         }
9778       offset_8 = (immed_high << 4) | immed_low;
9779       /* Calculate target store address.  */
9780       if (14 == arm_insn_r->opcode)
9781         {
9782           tgt_mem_addr = u_regval[0] + offset_8;
9783         }
9784       else
9785         {
9786           tgt_mem_addr = u_regval[0] - offset_8;
9787         }
9788       if (ARM_RECORD_STRH == str_type)
9789         {
9790           record_buf_mem[0] = 2;
9791           record_buf_mem[1] = tgt_mem_addr;
9792           arm_insn_r->mem_rec_count = 1;
9793         }
9794       else if (ARM_RECORD_STRD == str_type)
9795         {
9796           record_buf_mem[0] = 4;
9797           record_buf_mem[1] = tgt_mem_addr;
9798           record_buf_mem[2] = 4;
9799           record_buf_mem[3] = tgt_mem_addr + 4;
9800           arm_insn_r->mem_rec_count = 2;
9801         }
9802     }
9803   else if (12 == arm_insn_r->opcode || 8 == arm_insn_r->opcode)
9804     {
9805       /* 2) Store, register offset.  */
9806       /* Get Rm.  */
9807       reg_src1 = bits (arm_insn_r->arm_insn, 0, 3);
9808       /* Get Rn.  */
9809       reg_src2 = bits (arm_insn_r->arm_insn, 16, 19);
9810       regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval[0]);
9811       regcache_raw_read_unsigned (reg_cache, reg_src2, &u_regval[1]);
9812       if (15 == reg_src2)
9813         {
9814           /* If R15 was used as Rn, hence current PC+8.  */
9815           u_regval[0] = u_regval[0] + 8;
9816         }
9817       /* Calculate target store address, Rn +/- Rm, register offset.  */
9818       if (12 == arm_insn_r->opcode)
9819         {
9820           tgt_mem_addr = u_regval[0] + u_regval[1];
9821         }
9822       else
9823         {
9824           tgt_mem_addr = u_regval[1] - u_regval[0];
9825         }
9826       if (ARM_RECORD_STRH == str_type)
9827         {
9828           record_buf_mem[0] = 2;
9829           record_buf_mem[1] = tgt_mem_addr;
9830           arm_insn_r->mem_rec_count = 1;
9831         }
9832       else if (ARM_RECORD_STRD == str_type)
9833         {
9834           record_buf_mem[0] = 4;
9835           record_buf_mem[1] = tgt_mem_addr;
9836           record_buf_mem[2] = 4;
9837           record_buf_mem[3] = tgt_mem_addr + 4;
9838           arm_insn_r->mem_rec_count = 2;
9839         }
9840     }
9841   else if (11 == arm_insn_r->opcode || 15 == arm_insn_r->opcode
9842            || 2 == arm_insn_r->opcode  || 6 == arm_insn_r->opcode)
9843     {
9844       /* 3) Store, immediate pre-indexed.  */
9845       /* 5) Store, immediate post-indexed.  */
9846       immed_low = bits (arm_insn_r->arm_insn, 0, 3);
9847       immed_high = bits (arm_insn_r->arm_insn, 8, 11);
9848       offset_8 = (immed_high << 4) | immed_low;
9849       reg_src1 = bits (arm_insn_r->arm_insn, 16, 19);
9850       regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval[0]);
9851       /* Calculate target store address, Rn +/- Rm, register offset.  */
9852       if (15 == arm_insn_r->opcode || 6 == arm_insn_r->opcode)
9853         {
9854           tgt_mem_addr = u_regval[0] + offset_8;
9855         }
9856       else
9857         {
9858           tgt_mem_addr = u_regval[0] - offset_8;
9859         }
9860       if (ARM_RECORD_STRH == str_type)
9861         {
9862           record_buf_mem[0] = 2;
9863           record_buf_mem[1] = tgt_mem_addr;
9864           arm_insn_r->mem_rec_count = 1;
9865         }
9866       else if (ARM_RECORD_STRD == str_type)
9867         {
9868           record_buf_mem[0] = 4;
9869           record_buf_mem[1] = tgt_mem_addr;
9870           record_buf_mem[2] = 4;
9871           record_buf_mem[3] = tgt_mem_addr + 4;
9872           arm_insn_r->mem_rec_count = 2;
9873         }
9874       /* Record Rn also as it changes.  */
9875       *(record_buf) = bits (arm_insn_r->arm_insn, 16, 19);
9876       arm_insn_r->reg_rec_count = 1;
9877     }
9878   else if (9 == arm_insn_r->opcode || 13 == arm_insn_r->opcode
9879            || 0 == arm_insn_r->opcode || 4 == arm_insn_r->opcode)
9880     {
9881       /* 4) Store, register pre-indexed.  */
9882       /* 6) Store, register post -indexed.  */
9883       reg_src1 = bits (arm_insn_r->arm_insn, 0, 3);
9884       reg_src2 = bits (arm_insn_r->arm_insn, 16, 19);
9885       regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval[0]);
9886       regcache_raw_read_unsigned (reg_cache, reg_src2, &u_regval[1]);
9887       /* Calculate target store address, Rn +/- Rm, register offset.  */
9888       if (13 == arm_insn_r->opcode || 4 == arm_insn_r->opcode)
9889         {
9890           tgt_mem_addr = u_regval[0] + u_regval[1];
9891         }
9892       else
9893         {
9894           tgt_mem_addr = u_regval[1] - u_regval[0];
9895         }
9896       if (ARM_RECORD_STRH == str_type)
9897         {
9898           record_buf_mem[0] = 2;
9899           record_buf_mem[1] = tgt_mem_addr;
9900           arm_insn_r->mem_rec_count = 1;
9901         }
9902       else if (ARM_RECORD_STRD == str_type)
9903         {
9904           record_buf_mem[0] = 4;
9905           record_buf_mem[1] = tgt_mem_addr;
9906           record_buf_mem[2] = 4;
9907           record_buf_mem[3] = tgt_mem_addr + 4;
9908           arm_insn_r->mem_rec_count = 2;
9909         }
9910       /* Record Rn also as it changes.  */
9911       *(record_buf) = bits (arm_insn_r->arm_insn, 16, 19);
9912       arm_insn_r->reg_rec_count = 1;
9913     }
9914   return 0;
9915 }
9916
9917 /* Handling ARM extension space insns.  */
9918
9919 static int
9920 arm_record_extension_space (insn_decode_record *arm_insn_r)
9921 {
9922   int ret = 0;  /* Return value: -1:record failure ;  0:success  */
9923   uint32_t opcode1 = 0, opcode2 = 0, insn_op1 = 0;
9924   uint32_t record_buf[8], record_buf_mem[8];
9925   uint32_t reg_src1 = 0;
9926   struct regcache *reg_cache = arm_insn_r->regcache;
9927   ULONGEST u_regval = 0;
9928
9929   gdb_assert (!INSN_RECORDED(arm_insn_r));
9930   /* Handle unconditional insn extension space.  */
9931
9932   opcode1 = bits (arm_insn_r->arm_insn, 20, 27);
9933   opcode2 = bits (arm_insn_r->arm_insn, 4, 7);
9934   if (arm_insn_r->cond)
9935     {
9936       /* PLD has no affect on architectural state, it just affects
9937          the caches.  */
9938       if (5 == ((opcode1 & 0xE0) >> 5))
9939         {
9940           /* BLX(1) */
9941           record_buf[0] = ARM_PS_REGNUM;
9942           record_buf[1] = ARM_LR_REGNUM;
9943           arm_insn_r->reg_rec_count = 2;
9944         }
9945       /* STC2, LDC2, MCR2, MRC2, CDP2: <TBD>, co-processor insn.  */
9946     }
9947
9948
9949   opcode1 = bits (arm_insn_r->arm_insn, 25, 27);
9950   if (3 == opcode1 && bit (arm_insn_r->arm_insn, 4))
9951     {
9952       ret = -1;
9953       /* Undefined instruction on ARM V5; need to handle if later 
9954          versions define it.  */
9955     }
9956
9957   opcode1 = bits (arm_insn_r->arm_insn, 24, 27);
9958   opcode2 = bits (arm_insn_r->arm_insn, 4, 7);
9959   insn_op1 = bits (arm_insn_r->arm_insn, 20, 23);
9960
9961   /* Handle arithmetic insn extension space.  */
9962   if (!opcode1 && 9 == opcode2 && 1 != arm_insn_r->cond
9963       && !INSN_RECORDED(arm_insn_r))
9964     {
9965       /* Handle MLA(S) and MUL(S).  */
9966       if (in_inclusive_range (insn_op1, 0U, 3U))
9967       {
9968         record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
9969         record_buf[1] = ARM_PS_REGNUM;
9970         arm_insn_r->reg_rec_count = 2;
9971       }
9972       else if (in_inclusive_range (insn_op1, 4U, 15U))
9973       {
9974         /* Handle SMLAL(S), SMULL(S), UMLAL(S), UMULL(S).  */
9975         record_buf[0] = bits (arm_insn_r->arm_insn, 16, 19);
9976         record_buf[1] = bits (arm_insn_r->arm_insn, 12, 15);
9977         record_buf[2] = ARM_PS_REGNUM;
9978         arm_insn_r->reg_rec_count = 3;
9979       }
9980     }
9981
9982   opcode1 = bits (arm_insn_r->arm_insn, 26, 27);
9983   opcode2 = bits (arm_insn_r->arm_insn, 23, 24);
9984   insn_op1 = bits (arm_insn_r->arm_insn, 21, 22);
9985
9986   /* Handle control insn extension space.  */
9987
9988   if (!opcode1 && 2 == opcode2 && !bit (arm_insn_r->arm_insn, 20)
9989       && 1 != arm_insn_r->cond && !INSN_RECORDED(arm_insn_r))
9990     {
9991       if (!bit (arm_insn_r->arm_insn,25))
9992         {
9993           if (!bits (arm_insn_r->arm_insn, 4, 7))
9994             {
9995               if ((0 == insn_op1) || (2 == insn_op1))
9996                 {
9997                   /* MRS.  */
9998                   record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
9999                   arm_insn_r->reg_rec_count = 1;
10000                 }
10001               else if (1 == insn_op1)
10002                 {
10003                   /* CSPR is going to be changed.  */
10004                   record_buf[0] = ARM_PS_REGNUM;
10005                   arm_insn_r->reg_rec_count = 1;
10006                 }
10007               else if (3 == insn_op1)
10008                 {
10009                   /* SPSR is going to be changed.  */
10010                   /* We need to get SPSR value, which is yet to be done.  */
10011                   return -1;
10012                 }
10013             }
10014           else if (1 == bits (arm_insn_r->arm_insn, 4, 7))
10015             {
10016               if (1 == insn_op1)
10017                 {
10018                   /* BX.  */
10019                   record_buf[0] = ARM_PS_REGNUM;
10020                   arm_insn_r->reg_rec_count = 1;
10021                 }
10022               else if (3 == insn_op1)
10023                 {
10024                   /* CLZ.  */
10025                   record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
10026                   arm_insn_r->reg_rec_count = 1;
10027                 }
10028             }
10029           else if (3 == bits (arm_insn_r->arm_insn, 4, 7))
10030             {
10031               /* BLX.  */
10032               record_buf[0] = ARM_PS_REGNUM;
10033               record_buf[1] = ARM_LR_REGNUM;
10034               arm_insn_r->reg_rec_count = 2;
10035             }
10036           else if (5 == bits (arm_insn_r->arm_insn, 4, 7))
10037             {
10038               /* QADD, QSUB, QDADD, QDSUB */
10039               record_buf[0] = ARM_PS_REGNUM;
10040               record_buf[1] = bits (arm_insn_r->arm_insn, 12, 15);
10041               arm_insn_r->reg_rec_count = 2;
10042             }
10043           else if (7 == bits (arm_insn_r->arm_insn, 4, 7))
10044             {
10045               /* BKPT.  */
10046               record_buf[0] = ARM_PS_REGNUM;
10047               record_buf[1] = ARM_LR_REGNUM;
10048               arm_insn_r->reg_rec_count = 2;
10049
10050               /* Save SPSR also;how?  */
10051               return -1;
10052             }
10053           else if(8 == bits (arm_insn_r->arm_insn, 4, 7) 
10054                   || 10 == bits (arm_insn_r->arm_insn, 4, 7)
10055                   || 12 == bits (arm_insn_r->arm_insn, 4, 7)
10056                   || 14 == bits (arm_insn_r->arm_insn, 4, 7)
10057                  )
10058             {
10059               if (0 == insn_op1 || 1 == insn_op1)
10060                 {
10061                   /* SMLA<x><y>, SMLAW<y>, SMULW<y>.  */
10062                   /* We dont do optimization for SMULW<y> where we
10063                      need only Rd.  */
10064                   record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
10065                   record_buf[1] = ARM_PS_REGNUM;
10066                   arm_insn_r->reg_rec_count = 2;
10067                 }
10068               else if (2 == insn_op1)
10069                 {
10070                   /* SMLAL<x><y>.  */
10071                   record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
10072                   record_buf[1] = bits (arm_insn_r->arm_insn, 16, 19);
10073                   arm_insn_r->reg_rec_count = 2;
10074                 }
10075               else if (3 == insn_op1)
10076                 {
10077                   /* SMUL<x><y>.  */
10078                   record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
10079                   arm_insn_r->reg_rec_count = 1;
10080                 }
10081             }
10082         }
10083       else
10084         {
10085           /* MSR : immediate form.  */
10086           if (1 == insn_op1)
10087             {
10088               /* CSPR is going to be changed.  */
10089               record_buf[0] = ARM_PS_REGNUM;
10090               arm_insn_r->reg_rec_count = 1;
10091             }
10092           else if (3 == insn_op1)
10093             {
10094               /* SPSR is going to be changed.  */
10095               /* we need to get SPSR value, which is yet to be done  */
10096               return -1;
10097             }
10098         }
10099     }
10100
10101   opcode1 = bits (arm_insn_r->arm_insn, 25, 27);
10102   opcode2 = bits (arm_insn_r->arm_insn, 20, 24);
10103   insn_op1 = bits (arm_insn_r->arm_insn, 5, 6);
10104
10105   /* Handle load/store insn extension space.  */
10106
10107   if (!opcode1 && bit (arm_insn_r->arm_insn, 7) 
10108       && bit (arm_insn_r->arm_insn, 4) && 1 != arm_insn_r->cond
10109       && !INSN_RECORDED(arm_insn_r))
10110     {
10111       /* SWP/SWPB.  */
10112       if (0 == insn_op1)
10113         {
10114           /* These insn, changes register and memory as well.  */
10115           /* SWP or SWPB insn.  */
10116           /* Get memory address given by Rn.  */
10117           reg_src1 = bits (arm_insn_r->arm_insn, 16, 19);
10118           regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval);
10119           /* SWP insn ?, swaps word.  */
10120           if (8 == arm_insn_r->opcode)
10121             {
10122               record_buf_mem[0] = 4;
10123             }
10124           else
10125             {
10126               /* SWPB insn, swaps only byte.  */
10127               record_buf_mem[0] = 1;
10128             }
10129           record_buf_mem[1] = u_regval;
10130           arm_insn_r->mem_rec_count = 1;
10131           record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
10132           arm_insn_r->reg_rec_count = 1;
10133         }
10134       else if (1 == insn_op1 && !bit (arm_insn_r->arm_insn, 20))
10135         {
10136           /* STRH.  */
10137           arm_record_strx(arm_insn_r, &record_buf[0], &record_buf_mem[0],
10138                           ARM_RECORD_STRH);
10139         }
10140       else if (2 == insn_op1 && !bit (arm_insn_r->arm_insn, 20))
10141         {
10142           /* LDRD.  */
10143           record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
10144           record_buf[1] = record_buf[0] + 1;
10145           arm_insn_r->reg_rec_count = 2;
10146         }
10147       else if (3 == insn_op1 && !bit (arm_insn_r->arm_insn, 20))
10148         {
10149           /* STRD.  */
10150           arm_record_strx(arm_insn_r, &record_buf[0], &record_buf_mem[0],
10151                         ARM_RECORD_STRD);
10152         }
10153       else if (bit (arm_insn_r->arm_insn, 20) && insn_op1 <= 3)
10154         {
10155           /* LDRH, LDRSB, LDRSH.  */
10156           record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
10157           arm_insn_r->reg_rec_count = 1;
10158         }
10159
10160     }
10161
10162   opcode1 = bits (arm_insn_r->arm_insn, 23, 27);
10163   if (24 == opcode1 && bit (arm_insn_r->arm_insn, 21)
10164       && !INSN_RECORDED(arm_insn_r))
10165     {
10166       ret = -1;
10167       /* Handle coprocessor insn extension space.  */
10168     }
10169
10170   /* To be done for ARMv5 and later; as of now we return -1.  */
10171   if (-1 == ret)
10172     return ret;
10173
10174   REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
10175   MEM_ALLOC (arm_insn_r->arm_mems, arm_insn_r->mem_rec_count, record_buf_mem);
10176
10177   return ret;
10178 }
10179
10180 /* Handling opcode 000 insns.  */
10181
10182 static int
10183 arm_record_data_proc_misc_ld_str (insn_decode_record *arm_insn_r)
10184 {
10185   struct regcache *reg_cache = arm_insn_r->regcache;
10186   uint32_t record_buf[8], record_buf_mem[8];
10187   ULONGEST u_regval[2] = {0};
10188
10189   uint32_t reg_src1 = 0;
10190   uint32_t opcode1 = 0;
10191
10192   arm_insn_r->opcode = bits (arm_insn_r->arm_insn, 21, 24);
10193   arm_insn_r->decode = bits (arm_insn_r->arm_insn, 4, 7);
10194   opcode1 = bits (arm_insn_r->arm_insn, 20, 24);
10195
10196   if (!((opcode1 & 0x19) == 0x10))
10197     {
10198       /* Data-processing (register) and Data-processing (register-shifted
10199          register */
10200       /* Out of 11 shifter operands mode, all the insn modifies destination
10201          register, which is specified by 13-16 decode.  */
10202       record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
10203       record_buf[1] = ARM_PS_REGNUM;
10204       arm_insn_r->reg_rec_count = 2;
10205     }
10206   else if ((arm_insn_r->decode < 8) && ((opcode1 & 0x19) == 0x10))
10207     {
10208       /* Miscellaneous instructions */
10209
10210       if (3 == arm_insn_r->decode && 0x12 == opcode1
10211           && sbo_sbz (arm_insn_r->arm_insn, 9, 12, 1))
10212         {
10213           /* Handle BLX, branch and link/exchange.  */
10214           if (9 == arm_insn_r->opcode)
10215             {
10216               /* Branch is chosen by setting T bit of CSPR, bitp[0] of Rm,
10217                  and R14 stores the return address.  */
10218               record_buf[0] = ARM_PS_REGNUM;
10219               record_buf[1] = ARM_LR_REGNUM;
10220               arm_insn_r->reg_rec_count = 2;
10221             }
10222         }
10223       else if (7 == arm_insn_r->decode && 0x12 == opcode1)
10224         {
10225           /* Handle enhanced software breakpoint insn, BKPT.  */
10226           /* CPSR is changed to be executed in ARM state,  disabling normal
10227              interrupts, entering abort mode.  */
10228           /* According to high vector configuration PC is set.  */
10229           /* user hit breakpoint and type reverse, in
10230              that case, we need to go back with previous CPSR and
10231              Program Counter.  */
10232           record_buf[0] = ARM_PS_REGNUM;
10233           record_buf[1] = ARM_LR_REGNUM;
10234           arm_insn_r->reg_rec_count = 2;
10235
10236           /* Save SPSR also; how?  */
10237           return -1;
10238         }
10239       else if (1 == arm_insn_r->decode && 0x12 == opcode1
10240                && sbo_sbz (arm_insn_r->arm_insn, 9, 12, 1))
10241         {
10242           /* Handle BX, branch and link/exchange.  */
10243           /* Branch is chosen by setting T bit of CSPR, bitp[0] of Rm.  */
10244           record_buf[0] = ARM_PS_REGNUM;
10245           arm_insn_r->reg_rec_count = 1;
10246         }
10247       else if (1 == arm_insn_r->decode && 0x16 == opcode1
10248                && sbo_sbz (arm_insn_r->arm_insn, 9, 4, 1)
10249                && sbo_sbz (arm_insn_r->arm_insn, 17, 4, 1))
10250         {
10251           /* Count leading zeros: CLZ.  */
10252           record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
10253           arm_insn_r->reg_rec_count = 1;
10254         }
10255       else if (!bit (arm_insn_r->arm_insn, INSN_S_L_BIT_NUM)
10256                && (8 == arm_insn_r->opcode || 10 == arm_insn_r->opcode)
10257                && sbo_sbz (arm_insn_r->arm_insn, 17, 4, 1)
10258                && sbo_sbz (arm_insn_r->arm_insn, 1, 12, 0))
10259         {
10260           /* Handle MRS insn.  */
10261           record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
10262           arm_insn_r->reg_rec_count = 1;
10263         }
10264     }
10265   else if (9 == arm_insn_r->decode && opcode1 < 0x10)
10266     {
10267       /* Multiply and multiply-accumulate */
10268
10269       /* Handle multiply instructions.  */
10270       /* MLA, MUL, SMLAL, SMULL, UMLAL, UMULL.  */
10271       if (0 == arm_insn_r->opcode || 1 == arm_insn_r->opcode)
10272           {
10273             /* Handle MLA and MUL.  */
10274             record_buf[0] = bits (arm_insn_r->arm_insn, 16, 19);
10275             record_buf[1] = ARM_PS_REGNUM;
10276             arm_insn_r->reg_rec_count = 2;
10277           }
10278         else if (4 <= arm_insn_r->opcode && 7 >= arm_insn_r->opcode)
10279           {
10280             /* Handle SMLAL, SMULL, UMLAL, UMULL.  */
10281             record_buf[0] = bits (arm_insn_r->arm_insn, 16, 19);
10282             record_buf[1] = bits (arm_insn_r->arm_insn, 12, 15);
10283             record_buf[2] = ARM_PS_REGNUM;
10284             arm_insn_r->reg_rec_count = 3;
10285           }
10286     }
10287   else if (9 == arm_insn_r->decode  && opcode1 > 0x10)
10288     {
10289       /* Synchronization primitives */
10290
10291       /* Handling SWP, SWPB.  */
10292       /* These insn, changes register and memory as well.  */
10293       /* SWP or SWPB insn.  */
10294
10295       reg_src1 = bits (arm_insn_r->arm_insn, 16, 19);
10296       regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval[0]);
10297       /* SWP insn ?, swaps word.  */
10298       if (8 == arm_insn_r->opcode)
10299         {
10300           record_buf_mem[0] = 4;
10301         }
10302       else
10303         {
10304           /* SWPB insn, swaps only byte.  */
10305           record_buf_mem[0] = 1;
10306         }
10307       record_buf_mem[1] = u_regval[0];
10308       arm_insn_r->mem_rec_count = 1;
10309       record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
10310       arm_insn_r->reg_rec_count = 1;
10311     }
10312   else if (11 == arm_insn_r->decode || 13 == arm_insn_r->decode
10313            || 15 == arm_insn_r->decode)
10314     {
10315       if ((opcode1 & 0x12) == 2)
10316         {
10317           /* Extra load/store (unprivileged) */
10318           return -1;
10319         }
10320       else
10321         {
10322           /* Extra load/store */
10323           switch (bits (arm_insn_r->arm_insn, 5, 6))
10324             {
10325             case 1:
10326               if ((opcode1 & 0x05) == 0x0 || (opcode1 & 0x05) == 0x4)
10327                 {
10328                   /* STRH (register), STRH (immediate) */
10329                   arm_record_strx (arm_insn_r, &record_buf[0],
10330                                    &record_buf_mem[0], ARM_RECORD_STRH);
10331                 }
10332               else if ((opcode1 & 0x05) == 0x1)
10333                 {
10334                   /* LDRH (register) */
10335                   record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
10336                   arm_insn_r->reg_rec_count = 1;
10337
10338                   if (bit (arm_insn_r->arm_insn, 21))
10339                     {
10340                       /* Write back to Rn.  */
10341                       record_buf[arm_insn_r->reg_rec_count++]
10342                         = bits (arm_insn_r->arm_insn, 16, 19);
10343                     }
10344                 }
10345               else if ((opcode1 & 0x05) == 0x5)
10346                 {
10347                   /* LDRH (immediate), LDRH (literal) */
10348                   int rn = bits (arm_insn_r->arm_insn, 16, 19);
10349
10350                   record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
10351                   arm_insn_r->reg_rec_count = 1;
10352
10353                   if (rn != 15)
10354                     {
10355                       /*LDRH (immediate) */
10356                       if (bit (arm_insn_r->arm_insn, 21))
10357                         {
10358                           /* Write back to Rn.  */
10359                           record_buf[arm_insn_r->reg_rec_count++] = rn;
10360                         }
10361                     }
10362                 }
10363               else
10364                 return -1;
10365               break;
10366             case 2:
10367               if ((opcode1 & 0x05) == 0x0)
10368                 {
10369                   /* LDRD (register) */
10370                   record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
10371                   record_buf[1] = record_buf[0] + 1;
10372                   arm_insn_r->reg_rec_count = 2;
10373
10374                   if (bit (arm_insn_r->arm_insn, 21))
10375                     {
10376                       /* Write back to Rn.  */
10377                       record_buf[arm_insn_r->reg_rec_count++]
10378                         = bits (arm_insn_r->arm_insn, 16, 19);
10379                     }
10380                 }
10381               else if ((opcode1 & 0x05) == 0x1)
10382                 {
10383                   /* LDRSB (register) */
10384                   record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
10385                   arm_insn_r->reg_rec_count = 1;
10386
10387                   if (bit (arm_insn_r->arm_insn, 21))
10388                     {
10389                       /* Write back to Rn.  */
10390                       record_buf[arm_insn_r->reg_rec_count++]
10391                         = bits (arm_insn_r->arm_insn, 16, 19);
10392                     }
10393                 }
10394               else if ((opcode1 & 0x05) == 0x4 || (opcode1 & 0x05) == 0x5)
10395                 {
10396                   /* LDRD (immediate), LDRD (literal), LDRSB (immediate),
10397                      LDRSB (literal) */
10398                   int rn = bits (arm_insn_r->arm_insn, 16, 19);
10399
10400                   record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
10401                   arm_insn_r->reg_rec_count = 1;
10402
10403                   if (rn != 15)
10404                     {
10405                       /*LDRD (immediate), LDRSB (immediate) */
10406                       if (bit (arm_insn_r->arm_insn, 21))
10407                         {
10408                           /* Write back to Rn.  */
10409                           record_buf[arm_insn_r->reg_rec_count++] = rn;
10410                         }
10411                     }
10412                 }
10413               else
10414                 return -1;
10415               break;
10416             case 3:
10417               if ((opcode1 & 0x05) == 0x0)
10418                 {
10419                   /* STRD (register) */
10420                   arm_record_strx (arm_insn_r, &record_buf[0],
10421                                    &record_buf_mem[0], ARM_RECORD_STRD);
10422                 }
10423               else if ((opcode1 & 0x05) == 0x1)
10424                 {
10425                   /* LDRSH (register) */
10426                   record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
10427                   arm_insn_r->reg_rec_count = 1;
10428
10429                   if (bit (arm_insn_r->arm_insn, 21))
10430                     {
10431                       /* Write back to Rn.  */
10432                       record_buf[arm_insn_r->reg_rec_count++]
10433                         = bits (arm_insn_r->arm_insn, 16, 19);
10434                     }
10435                 }
10436               else if ((opcode1 & 0x05) == 0x4)
10437                 {
10438                   /* STRD (immediate) */
10439                   arm_record_strx (arm_insn_r, &record_buf[0],
10440                                    &record_buf_mem[0], ARM_RECORD_STRD);
10441                 }
10442               else if ((opcode1 & 0x05) == 0x5)
10443                 {
10444                   /* LDRSH (immediate), LDRSH (literal) */
10445                   record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
10446                   arm_insn_r->reg_rec_count = 1;
10447
10448                   if (bit (arm_insn_r->arm_insn, 21))
10449                     {
10450                       /* Write back to Rn.  */
10451                       record_buf[arm_insn_r->reg_rec_count++]
10452                         = bits (arm_insn_r->arm_insn, 16, 19);
10453                     }
10454                 }
10455               else
10456                 return -1;
10457               break;
10458             default:
10459               return -1;
10460             }
10461         }
10462     }
10463   else
10464     {
10465       return -1;
10466     }
10467
10468   REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
10469   MEM_ALLOC (arm_insn_r->arm_mems, arm_insn_r->mem_rec_count, record_buf_mem);
10470   return 0;
10471 }
10472
10473 /* Handling opcode 001 insns.  */
10474
10475 static int
10476 arm_record_data_proc_imm (insn_decode_record *arm_insn_r)
10477 {
10478   uint32_t record_buf[8], record_buf_mem[8];
10479
10480   arm_insn_r->opcode = bits (arm_insn_r->arm_insn, 21, 24);
10481   arm_insn_r->decode = bits (arm_insn_r->arm_insn, 4, 7);
10482
10483   if ((9 == arm_insn_r->opcode || 11 == arm_insn_r->opcode)
10484       && 2 == bits (arm_insn_r->arm_insn, 20, 21)
10485       && sbo_sbz (arm_insn_r->arm_insn, 13, 4, 1)
10486      )
10487     {
10488       /* Handle MSR insn.  */
10489       if (9 == arm_insn_r->opcode)
10490         {
10491           /* CSPR is going to be changed.  */
10492           record_buf[0] = ARM_PS_REGNUM;
10493           arm_insn_r->reg_rec_count = 1;
10494         }
10495       else
10496         {
10497           /* SPSR is going to be changed.  */
10498         }
10499     }
10500   else if (arm_insn_r->opcode <= 15)
10501     {
10502       /* Normal data processing insns.  */
10503       /* Out of 11 shifter operands mode, all the insn modifies destination
10504          register, which is specified by 13-16 decode.  */
10505       record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
10506       record_buf[1] = ARM_PS_REGNUM;
10507       arm_insn_r->reg_rec_count = 2;
10508     }
10509   else
10510     {
10511       return -1;
10512     }
10513
10514   REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
10515   MEM_ALLOC (arm_insn_r->arm_mems, arm_insn_r->mem_rec_count, record_buf_mem);
10516   return 0;
10517 }
10518
10519 static int
10520 arm_record_media (insn_decode_record *arm_insn_r)
10521 {
10522   uint32_t record_buf[8];
10523
10524   switch (bits (arm_insn_r->arm_insn, 22, 24))
10525     {
10526     case 0:
10527       /* Parallel addition and subtraction, signed */
10528     case 1:
10529       /* Parallel addition and subtraction, unsigned */
10530     case 2:
10531     case 3:
10532       /* Packing, unpacking, saturation and reversal */
10533       {
10534         int rd = bits (arm_insn_r->arm_insn, 12, 15);
10535
10536         record_buf[arm_insn_r->reg_rec_count++] = rd;
10537       }
10538       break;
10539
10540     case 4:
10541     case 5:
10542       /* Signed multiplies */
10543       {
10544         int rd = bits (arm_insn_r->arm_insn, 16, 19);
10545         unsigned int op1 = bits (arm_insn_r->arm_insn, 20, 22);
10546
10547         record_buf[arm_insn_r->reg_rec_count++] = rd;
10548         if (op1 == 0x0)
10549           record_buf[arm_insn_r->reg_rec_count++] = ARM_PS_REGNUM;
10550         else if (op1 == 0x4)
10551           record_buf[arm_insn_r->reg_rec_count++]
10552             = bits (arm_insn_r->arm_insn, 12, 15);
10553       }
10554       break;
10555
10556     case 6:
10557       {
10558         if (bit (arm_insn_r->arm_insn, 21)
10559             && bits (arm_insn_r->arm_insn, 5, 6) == 0x2)
10560           {
10561             /* SBFX */
10562             record_buf[arm_insn_r->reg_rec_count++]
10563               = bits (arm_insn_r->arm_insn, 12, 15);
10564           }
10565         else if (bits (arm_insn_r->arm_insn, 20, 21) == 0x0
10566                  && bits (arm_insn_r->arm_insn, 5, 7) == 0x0)
10567           {
10568             /* USAD8 and USADA8 */
10569             record_buf[arm_insn_r->reg_rec_count++]
10570               = bits (arm_insn_r->arm_insn, 16, 19);
10571           }
10572       }
10573       break;
10574
10575     case 7:
10576       {
10577         if (bits (arm_insn_r->arm_insn, 20, 21) == 0x3
10578             && bits (arm_insn_r->arm_insn, 5, 7) == 0x7)
10579           {
10580             /* Permanently UNDEFINED */
10581             return -1;
10582           }
10583         else
10584           {
10585             /* BFC, BFI and UBFX */
10586             record_buf[arm_insn_r->reg_rec_count++]
10587               = bits (arm_insn_r->arm_insn, 12, 15);
10588           }
10589       }
10590       break;
10591
10592     default:
10593       return -1;
10594     }
10595
10596   REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
10597
10598   return 0;
10599 }
10600
10601 /* Handle ARM mode instructions with opcode 010.  */
10602
10603 static int
10604 arm_record_ld_st_imm_offset (insn_decode_record *arm_insn_r)
10605 {
10606   struct regcache *reg_cache = arm_insn_r->regcache;
10607
10608   uint32_t reg_base , reg_dest;
10609   uint32_t offset_12, tgt_mem_addr;
10610   uint32_t record_buf[8], record_buf_mem[8];
10611   unsigned char wback;
10612   ULONGEST u_regval;
10613
10614   /* Calculate wback.  */
10615   wback = (bit (arm_insn_r->arm_insn, 24) == 0)
10616           || (bit (arm_insn_r->arm_insn, 21) == 1);
10617
10618   arm_insn_r->reg_rec_count = 0;
10619   reg_base = bits (arm_insn_r->arm_insn, 16, 19);
10620
10621   if (bit (arm_insn_r->arm_insn, INSN_S_L_BIT_NUM))
10622     {
10623       /* LDR (immediate), LDR (literal), LDRB (immediate), LDRB (literal), LDRBT
10624          and LDRT.  */
10625
10626       reg_dest = bits (arm_insn_r->arm_insn, 12, 15);
10627       record_buf[arm_insn_r->reg_rec_count++] = reg_dest;
10628
10629       /* The LDR instruction is capable of doing branching.  If MOV LR, PC
10630          preceeds a LDR instruction having R15 as reg_base, it
10631          emulates a branch and link instruction, and hence we need to save
10632          CPSR and PC as well.  */
10633       if (ARM_PC_REGNUM == reg_dest)
10634         record_buf[arm_insn_r->reg_rec_count++] = ARM_PS_REGNUM;
10635
10636       /* If wback is true, also save the base register, which is going to be
10637          written to.  */
10638       if (wback)
10639         record_buf[arm_insn_r->reg_rec_count++] = reg_base;
10640     }
10641   else
10642     {
10643       /* STR (immediate), STRB (immediate), STRBT and STRT.  */
10644
10645       offset_12 = bits (arm_insn_r->arm_insn, 0, 11);
10646       regcache_raw_read_unsigned (reg_cache, reg_base, &u_regval);
10647
10648       /* Handle bit U.  */
10649       if (bit (arm_insn_r->arm_insn, 23))
10650         {
10651           /* U == 1: Add the offset. */
10652           tgt_mem_addr = (uint32_t) u_regval + offset_12;
10653         }
10654       else
10655         {
10656           /* U == 0: subtract the offset. */
10657           tgt_mem_addr = (uint32_t) u_regval - offset_12;
10658         }
10659
10660       /* Bit 22 tells us whether the store instruction writes 1 byte or 4
10661          bytes.  */
10662       if (bit (arm_insn_r->arm_insn, 22))
10663         {
10664           /* STRB and STRBT: 1 byte.  */
10665           record_buf_mem[0] = 1;
10666         }
10667       else
10668         {
10669           /* STR and STRT: 4 bytes.  */
10670           record_buf_mem[0] = 4;
10671         }
10672
10673       /* Handle bit P.  */
10674       if (bit (arm_insn_r->arm_insn, 24))
10675         record_buf_mem[1] = tgt_mem_addr;
10676       else
10677         record_buf_mem[1] = (uint32_t) u_regval;
10678
10679       arm_insn_r->mem_rec_count = 1;
10680
10681       /* If wback is true, also save the base register, which is going to be
10682          written to.  */
10683       if (wback)
10684         record_buf[arm_insn_r->reg_rec_count++] = reg_base;
10685     }
10686
10687   REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
10688   MEM_ALLOC (arm_insn_r->arm_mems, arm_insn_r->mem_rec_count, record_buf_mem);
10689   return 0;
10690 }
10691
10692 /* Handling opcode 011 insns.  */
10693
10694 static int
10695 arm_record_ld_st_reg_offset (insn_decode_record *arm_insn_r)
10696 {
10697   struct regcache *reg_cache = arm_insn_r->regcache;
10698
10699   uint32_t shift_imm = 0;
10700   uint32_t reg_src1 = 0, reg_src2 = 0, reg_dest = 0;
10701   uint32_t offset_12 = 0, tgt_mem_addr = 0;
10702   uint32_t record_buf[8], record_buf_mem[8];
10703
10704   LONGEST s_word;
10705   ULONGEST u_regval[2];
10706
10707   if (bit (arm_insn_r->arm_insn, 4))
10708     return arm_record_media (arm_insn_r);
10709
10710   arm_insn_r->opcode = bits (arm_insn_r->arm_insn, 21, 24);
10711   arm_insn_r->decode = bits (arm_insn_r->arm_insn, 4, 7);
10712
10713   /* Handle enhanced store insns and LDRD DSP insn,
10714      order begins according to addressing modes for store insns
10715      STRH insn.  */
10716
10717   /* LDR or STR?  */
10718   if (bit (arm_insn_r->arm_insn, INSN_S_L_BIT_NUM))
10719     {
10720       reg_dest = bits (arm_insn_r->arm_insn, 12, 15);
10721       /* LDR insn has a capability to do branching, if
10722          MOV LR, PC is precedded by LDR insn having Rn as R15
10723          in that case, it emulates branch and link insn, and hence we
10724          need to save CSPR and PC as well.  */
10725       if (15 != reg_dest)
10726         {
10727           record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
10728           arm_insn_r->reg_rec_count = 1;
10729         }
10730       else
10731         {
10732           record_buf[0] = reg_dest;
10733           record_buf[1] = ARM_PS_REGNUM;
10734           arm_insn_r->reg_rec_count = 2;
10735         }
10736     }
10737   else
10738     {
10739       if (! bits (arm_insn_r->arm_insn, 4, 11))
10740         {
10741           /* Store insn, register offset and register pre-indexed,
10742              register post-indexed.  */
10743           /* Get Rm.  */
10744           reg_src1 = bits (arm_insn_r->arm_insn, 0, 3);
10745           /* Get Rn.  */
10746           reg_src2 = bits (arm_insn_r->arm_insn, 16, 19);
10747           regcache_raw_read_unsigned (reg_cache, reg_src1
10748                                       , &u_regval[0]);
10749           regcache_raw_read_unsigned (reg_cache, reg_src2
10750                                       , &u_regval[1]);
10751           if (15 == reg_src2)
10752             {
10753               /* If R15 was used as Rn, hence current PC+8.  */
10754               /* Pre-indexed mode doesnt reach here ; illegal insn.  */
10755                 u_regval[0] = u_regval[0] + 8;
10756             }
10757           /* Calculate target store address, Rn +/- Rm, register offset.  */
10758           /* U == 1.  */
10759           if (bit (arm_insn_r->arm_insn, 23))
10760             {
10761               tgt_mem_addr = u_regval[0] + u_regval[1];
10762             }
10763           else
10764             {
10765               tgt_mem_addr = u_regval[1] - u_regval[0];
10766             }
10767
10768           switch (arm_insn_r->opcode)
10769             {
10770               /* STR.  */
10771               case 8:
10772               case 12:
10773               /* STR.  */    
10774               case 9:
10775               case 13:
10776               /* STRT.  */
10777               case 1:
10778               case 5:
10779               /* STR.  */
10780               case 0:
10781               case 4:
10782                 record_buf_mem[0] = 4;
10783               break;
10784
10785               /* STRB.  */
10786               case 10:
10787               case 14:
10788               /* STRB.  */
10789               case 11:
10790               case 15:
10791               /* STRBT.  */    
10792               case 3:
10793               case 7:
10794               /* STRB.  */
10795               case 2:
10796               case 6:
10797                 record_buf_mem[0] = 1;
10798               break;
10799
10800               default:
10801                 gdb_assert_not_reached ("no decoding pattern found");
10802               break;
10803             }
10804           record_buf_mem[1] = tgt_mem_addr;
10805           arm_insn_r->mem_rec_count = 1;
10806
10807           if (9 == arm_insn_r->opcode || 11 == arm_insn_r->opcode
10808               || 13 == arm_insn_r->opcode || 15 == arm_insn_r->opcode
10809               || 0 == arm_insn_r->opcode || 2 == arm_insn_r->opcode
10810               || 4 == arm_insn_r->opcode || 6 == arm_insn_r->opcode
10811               || 1 == arm_insn_r->opcode || 3 == arm_insn_r->opcode
10812               || 5 == arm_insn_r->opcode || 7 == arm_insn_r->opcode
10813              )
10814             {
10815               /* Rn is going to be changed in pre-indexed mode and
10816                  post-indexed mode as well.  */
10817               record_buf[0] = reg_src2;
10818               arm_insn_r->reg_rec_count = 1;
10819             }
10820         }
10821       else
10822         {
10823           /* Store insn, scaled register offset; scaled pre-indexed.  */
10824           offset_12 = bits (arm_insn_r->arm_insn, 5, 6);
10825           /* Get Rm.  */
10826           reg_src1 = bits (arm_insn_r->arm_insn, 0, 3);
10827           /* Get Rn.  */
10828           reg_src2 = bits (arm_insn_r->arm_insn, 16, 19);
10829           /* Get shift_imm.  */
10830           shift_imm = bits (arm_insn_r->arm_insn, 7, 11);
10831           regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval[0]);
10832           regcache_raw_read_signed (reg_cache, reg_src1, &s_word);
10833           regcache_raw_read_unsigned (reg_cache, reg_src2, &u_regval[1]);
10834           /* Offset_12 used as shift.  */
10835           switch (offset_12)
10836             {
10837               case 0:
10838                 /* Offset_12 used as index.  */
10839                 offset_12 = u_regval[0] << shift_imm;
10840               break;
10841
10842               case 1:
10843                 offset_12 = (!shift_imm)?0:u_regval[0] >> shift_imm;
10844               break;
10845
10846               case 2:
10847                 if (!shift_imm)
10848                   {
10849                     if (bit (u_regval[0], 31))
10850                       {
10851                         offset_12 = 0xFFFFFFFF;
10852                       }
10853                     else
10854                       {
10855                         offset_12 = 0;
10856                       }
10857                   }
10858                 else
10859                   {
10860                     /* This is arithmetic shift.  */
10861                     offset_12 = s_word >> shift_imm;
10862                   }
10863                 break;
10864
10865               case 3:
10866                 if (!shift_imm)
10867                   {
10868                     regcache_raw_read_unsigned (reg_cache, ARM_PS_REGNUM,
10869                                                 &u_regval[1]);
10870                     /* Get C flag value and shift it by 31.  */
10871                     offset_12 = (((bit (u_regval[1], 29)) << 31) \
10872                                   | (u_regval[0]) >> 1);
10873                   }
10874                 else
10875                   {
10876                     offset_12 = (u_regval[0] >> shift_imm) \
10877                                 | (u_regval[0] <<
10878                                 (sizeof(uint32_t) - shift_imm));
10879                   }
10880               break;
10881
10882               default:
10883                 gdb_assert_not_reached ("no decoding pattern found");
10884               break;
10885             }
10886
10887           regcache_raw_read_unsigned (reg_cache, reg_src2, &u_regval[1]);
10888           /* bit U set.  */
10889           if (bit (arm_insn_r->arm_insn, 23))
10890             {
10891               tgt_mem_addr = u_regval[1] + offset_12;
10892             }
10893           else
10894             {
10895               tgt_mem_addr = u_regval[1] - offset_12;
10896             }
10897
10898           switch (arm_insn_r->opcode)
10899             {
10900               /* STR.  */
10901               case 8:
10902               case 12:
10903               /* STR.  */    
10904               case 9:
10905               case 13:
10906               /* STRT.  */
10907               case 1:
10908               case 5:
10909               /* STR.  */
10910               case 0:
10911               case 4:
10912                 record_buf_mem[0] = 4;
10913               break;
10914
10915               /* STRB.  */
10916               case 10:
10917               case 14:
10918               /* STRB.  */
10919               case 11:
10920               case 15:
10921               /* STRBT.  */    
10922               case 3:
10923               case 7:
10924               /* STRB.  */
10925               case 2:
10926               case 6:
10927                 record_buf_mem[0] = 1;
10928               break;
10929
10930               default:
10931                 gdb_assert_not_reached ("no decoding pattern found");
10932               break;
10933             }
10934           record_buf_mem[1] = tgt_mem_addr;
10935           arm_insn_r->mem_rec_count = 1;
10936
10937           if (9 == arm_insn_r->opcode || 11 == arm_insn_r->opcode
10938               || 13 == arm_insn_r->opcode || 15 == arm_insn_r->opcode
10939               || 0 == arm_insn_r->opcode || 2 == arm_insn_r->opcode
10940               || 4 == arm_insn_r->opcode || 6 == arm_insn_r->opcode
10941               || 1 == arm_insn_r->opcode || 3 == arm_insn_r->opcode
10942               || 5 == arm_insn_r->opcode || 7 == arm_insn_r->opcode
10943              )
10944             {
10945               /* Rn is going to be changed in register scaled pre-indexed
10946                  mode,and scaled post indexed mode.  */
10947               record_buf[0] = reg_src2;
10948               arm_insn_r->reg_rec_count = 1;
10949             }
10950         }
10951     }
10952
10953   REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
10954   MEM_ALLOC (arm_insn_r->arm_mems, arm_insn_r->mem_rec_count, record_buf_mem);
10955   return 0;
10956 }
10957
10958 /* Handle ARM mode instructions with opcode 100.  */
10959
10960 static int
10961 arm_record_ld_st_multiple (insn_decode_record *arm_insn_r)
10962 {
10963   struct regcache *reg_cache = arm_insn_r->regcache;
10964   uint32_t register_count = 0, register_bits;
10965   uint32_t reg_base, addr_mode;
10966   uint32_t record_buf[24], record_buf_mem[48];
10967   uint32_t wback;
10968   ULONGEST u_regval;
10969
10970   /* Fetch the list of registers.  */
10971   register_bits = bits (arm_insn_r->arm_insn, 0, 15);
10972   arm_insn_r->reg_rec_count = 0;
10973
10974   /* Fetch the base register that contains the address we are loading data
10975      to.  */
10976   reg_base = bits (arm_insn_r->arm_insn, 16, 19);
10977
10978   /* Calculate wback.  */
10979   wback = (bit (arm_insn_r->arm_insn, 21) == 1);
10980
10981   if (bit (arm_insn_r->arm_insn, INSN_S_L_BIT_NUM))
10982     {
10983       /* LDM/LDMIA/LDMFD, LDMDA/LDMFA, LDMDB and LDMIB.  */
10984
10985       /* Find out which registers are going to be loaded from memory.  */
10986       while (register_bits)
10987         {
10988           if (register_bits & 0x00000001)
10989             record_buf[arm_insn_r->reg_rec_count++] = register_count;
10990           register_bits = register_bits >> 1;
10991           register_count++;
10992         }
10993
10994   
10995       /* If wback is true, also save the base register, which is going to be
10996          written to.  */
10997       if (wback)
10998         record_buf[arm_insn_r->reg_rec_count++] = reg_base;
10999
11000       /* Save the CPSR register.  */
11001       record_buf[arm_insn_r->reg_rec_count++] = ARM_PS_REGNUM;
11002     }
11003   else
11004     {
11005       /* STM (STMIA, STMEA), STMDA (STMED), STMDB (STMFD) and STMIB (STMFA).  */
11006
11007       addr_mode = bits (arm_insn_r->arm_insn, 23, 24); 
11008
11009       regcache_raw_read_unsigned (reg_cache, reg_base, &u_regval);
11010
11011       /* Find out how many registers are going to be stored to memory.  */
11012       while (register_bits)
11013         {
11014           if (register_bits & 0x00000001)
11015             register_count++;
11016           register_bits = register_bits >> 1;
11017         }
11018
11019       switch (addr_mode)
11020         {
11021           /* STMDA (STMED): Decrement after.  */
11022           case 0:
11023           record_buf_mem[1] = (uint32_t) u_regval
11024                               - register_count * INT_REGISTER_SIZE + 4;
11025           break;
11026           /* STM (STMIA, STMEA): Increment after.  */
11027           case 1:
11028           record_buf_mem[1] = (uint32_t) u_regval;
11029           break;
11030           /* STMDB (STMFD): Decrement before.  */
11031           case 2:
11032           record_buf_mem[1] = (uint32_t) u_regval
11033                               - register_count * INT_REGISTER_SIZE;
11034           break;
11035           /* STMIB (STMFA): Increment before.  */
11036           case 3:
11037           record_buf_mem[1] = (uint32_t) u_regval + INT_REGISTER_SIZE;
11038           break;
11039           default:
11040             gdb_assert_not_reached ("no decoding pattern found");
11041           break;
11042         }
11043
11044       record_buf_mem[0] = register_count * INT_REGISTER_SIZE;
11045       arm_insn_r->mem_rec_count = 1;
11046
11047       /* If wback is true, also save the base register, which is going to be
11048          written to.  */
11049       if (wback)
11050         record_buf[arm_insn_r->reg_rec_count++] = reg_base;
11051     }
11052
11053   REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
11054   MEM_ALLOC (arm_insn_r->arm_mems, arm_insn_r->mem_rec_count, record_buf_mem);
11055   return 0;
11056 }
11057
11058 /* Handling opcode 101 insns.  */
11059
11060 static int
11061 arm_record_b_bl (insn_decode_record *arm_insn_r)
11062 {
11063   uint32_t record_buf[8];
11064
11065   /* Handle B, BL, BLX(1) insns.  */
11066   /* B simply branches so we do nothing here.  */
11067   /* Note: BLX(1) doesnt fall here but instead it falls into
11068      extension space.  */
11069   if (bit (arm_insn_r->arm_insn, 24))
11070   {
11071     record_buf[0] = ARM_LR_REGNUM;
11072     arm_insn_r->reg_rec_count = 1;
11073   }
11074
11075   REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
11076
11077   return 0;
11078 }
11079
11080 static int
11081 arm_record_unsupported_insn (insn_decode_record *arm_insn_r)
11082 {
11083   printf_unfiltered (_("Process record does not support instruction "
11084                        "0x%0x at address %s.\n"),arm_insn_r->arm_insn,
11085                      paddress (arm_insn_r->gdbarch, arm_insn_r->this_addr));
11086
11087   return -1;
11088 }
11089
11090 /* Record handler for vector data transfer instructions.  */
11091
11092 static int
11093 arm_record_vdata_transfer_insn (insn_decode_record *arm_insn_r)
11094 {
11095   uint32_t bits_a, bit_c, bit_l, reg_t, reg_v;
11096   uint32_t record_buf[4];
11097
11098   reg_t = bits (arm_insn_r->arm_insn, 12, 15);
11099   reg_v = bits (arm_insn_r->arm_insn, 21, 23);
11100   bits_a = bits (arm_insn_r->arm_insn, 21, 23);
11101   bit_l = bit (arm_insn_r->arm_insn, 20);
11102   bit_c = bit (arm_insn_r->arm_insn, 8);
11103
11104   /* Handle VMOV instruction.  */
11105   if (bit_l && bit_c)
11106     {
11107       record_buf[0] = reg_t;
11108       arm_insn_r->reg_rec_count = 1;
11109     }
11110   else if (bit_l && !bit_c)
11111     {
11112       /* Handle VMOV instruction.  */
11113       if (bits_a == 0x00)
11114         {
11115           record_buf[0] = reg_t;
11116           arm_insn_r->reg_rec_count = 1;
11117         }
11118       /* Handle VMRS instruction.  */
11119       else if (bits_a == 0x07)
11120         {
11121           if (reg_t == 15)
11122             reg_t = ARM_PS_REGNUM;
11123
11124           record_buf[0] = reg_t;
11125           arm_insn_r->reg_rec_count = 1;
11126         }
11127     }
11128   else if (!bit_l && !bit_c)
11129     {
11130       /* Handle VMOV instruction.  */
11131       if (bits_a == 0x00)
11132         {
11133           record_buf[0] = ARM_D0_REGNUM + reg_v;
11134
11135           arm_insn_r->reg_rec_count = 1;
11136         }
11137       /* Handle VMSR instruction.  */
11138       else if (bits_a == 0x07)
11139         {
11140           record_buf[0] = ARM_FPSCR_REGNUM;
11141           arm_insn_r->reg_rec_count = 1;
11142         }
11143     }
11144   else if (!bit_l && bit_c)
11145     {
11146       /* Handle VMOV instruction.  */
11147       if (!(bits_a & 0x04))
11148         {
11149           record_buf[0] = (reg_v | (bit (arm_insn_r->arm_insn, 7) << 4))
11150                           + ARM_D0_REGNUM;
11151           arm_insn_r->reg_rec_count = 1;
11152         }
11153       /* Handle VDUP instruction.  */
11154       else
11155         {
11156           if (bit (arm_insn_r->arm_insn, 21))
11157             {
11158               reg_v = reg_v | (bit (arm_insn_r->arm_insn, 7) << 4);
11159               record_buf[0] = reg_v + ARM_D0_REGNUM;
11160               record_buf[1] = reg_v + ARM_D0_REGNUM + 1;
11161               arm_insn_r->reg_rec_count = 2;
11162             }
11163           else
11164             {
11165               reg_v = reg_v | (bit (arm_insn_r->arm_insn, 7) << 4);
11166               record_buf[0] = reg_v + ARM_D0_REGNUM;
11167               arm_insn_r->reg_rec_count = 1;
11168             }
11169         }
11170     }
11171
11172   REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
11173   return 0;
11174 }
11175
11176 /* Record handler for extension register load/store instructions.  */
11177
11178 static int
11179 arm_record_exreg_ld_st_insn (insn_decode_record *arm_insn_r)
11180 {
11181   uint32_t opcode, single_reg;
11182   uint8_t op_vldm_vstm;
11183   uint32_t record_buf[8], record_buf_mem[128];
11184   ULONGEST u_regval = 0;
11185
11186   struct regcache *reg_cache = arm_insn_r->regcache;
11187
11188   opcode = bits (arm_insn_r->arm_insn, 20, 24);
11189   single_reg = !bit (arm_insn_r->arm_insn, 8);
11190   op_vldm_vstm = opcode & 0x1b;
11191
11192   /* Handle VMOV instructions.  */
11193   if ((opcode & 0x1e) == 0x04)
11194     {
11195       if (bit (arm_insn_r->arm_insn, 20)) /* to_arm_registers bit 20? */
11196         {
11197           record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11198           record_buf[1] = bits (arm_insn_r->arm_insn, 16, 19);
11199           arm_insn_r->reg_rec_count = 2;
11200         }
11201       else
11202         {
11203           uint8_t reg_m = bits (arm_insn_r->arm_insn, 0, 3);
11204           uint8_t bit_m = bit (arm_insn_r->arm_insn, 5);
11205
11206           if (single_reg)
11207             {
11208               /* The first S register number m is REG_M:M (M is bit 5),
11209                  the corresponding D register number is REG_M:M / 2, which
11210                  is REG_M.  */
11211               record_buf[arm_insn_r->reg_rec_count++] = ARM_D0_REGNUM + reg_m;
11212               /* The second S register number is REG_M:M + 1, the
11213                  corresponding D register number is (REG_M:M + 1) / 2.
11214                  IOW, if bit M is 1, the first and second S registers
11215                  are mapped to different D registers, otherwise, they are
11216                  in the same D register.  */
11217               if (bit_m)
11218                 {
11219                   record_buf[arm_insn_r->reg_rec_count++]
11220                     = ARM_D0_REGNUM + reg_m + 1;
11221                 }
11222             }
11223           else
11224             {
11225               record_buf[0] = ((bit_m << 4) + reg_m + ARM_D0_REGNUM);
11226               arm_insn_r->reg_rec_count = 1;
11227             }
11228         }
11229     }
11230   /* Handle VSTM and VPUSH instructions.  */
11231   else if (op_vldm_vstm == 0x08 || op_vldm_vstm == 0x0a
11232            || op_vldm_vstm == 0x12)
11233     {
11234       uint32_t start_address, reg_rn, imm_off32, imm_off8, memory_count;
11235       uint32_t memory_index = 0;
11236
11237       reg_rn = bits (arm_insn_r->arm_insn, 16, 19);
11238       regcache_raw_read_unsigned (reg_cache, reg_rn, &u_regval);
11239       imm_off8 = bits (arm_insn_r->arm_insn, 0, 7);
11240       imm_off32 = imm_off8 << 2;
11241       memory_count = imm_off8;
11242
11243       if (bit (arm_insn_r->arm_insn, 23))
11244         start_address = u_regval;
11245       else
11246         start_address = u_regval - imm_off32;
11247
11248       if (bit (arm_insn_r->arm_insn, 21))
11249         {
11250           record_buf[0] = reg_rn;
11251           arm_insn_r->reg_rec_count = 1;
11252         }
11253
11254       while (memory_count > 0)
11255         {
11256           if (single_reg)
11257             {
11258               record_buf_mem[memory_index] = 4;
11259               record_buf_mem[memory_index + 1] = start_address;
11260               start_address = start_address + 4;
11261               memory_index = memory_index + 2;
11262             }
11263           else
11264             {
11265               record_buf_mem[memory_index] = 4;
11266               record_buf_mem[memory_index + 1] = start_address;
11267               record_buf_mem[memory_index + 2] = 4;
11268               record_buf_mem[memory_index + 3] = start_address + 4;
11269               start_address = start_address + 8;
11270               memory_index = memory_index + 4;
11271             }
11272           memory_count--;
11273         }
11274       arm_insn_r->mem_rec_count = (memory_index >> 1);
11275     }
11276   /* Handle VLDM instructions.  */
11277   else if (op_vldm_vstm == 0x09 || op_vldm_vstm == 0x0b
11278            || op_vldm_vstm == 0x13)
11279     {
11280       uint32_t reg_count, reg_vd;
11281       uint32_t reg_index = 0;
11282       uint32_t bit_d = bit (arm_insn_r->arm_insn, 22);
11283
11284       reg_vd = bits (arm_insn_r->arm_insn, 12, 15);
11285       reg_count = bits (arm_insn_r->arm_insn, 0, 7);
11286
11287       /* REG_VD is the first D register number.  If the instruction
11288          loads memory to S registers (SINGLE_REG is TRUE), the register
11289          number is (REG_VD << 1 | bit D), so the corresponding D
11290          register number is (REG_VD << 1 | bit D) / 2 = REG_VD.  */
11291       if (!single_reg)
11292         reg_vd = reg_vd | (bit_d << 4);
11293
11294       if (bit (arm_insn_r->arm_insn, 21) /* write back */)
11295         record_buf[reg_index++] = bits (arm_insn_r->arm_insn, 16, 19);
11296
11297       /* If the instruction loads memory to D register, REG_COUNT should
11298          be divided by 2, according to the ARM Architecture Reference
11299          Manual.  If the instruction loads memory to S register, divide by
11300          2 as well because two S registers are mapped to D register.  */
11301       reg_count  = reg_count / 2;
11302       if (single_reg && bit_d)
11303         {
11304           /* Increase the register count if S register list starts from
11305              an odd number (bit d is one).  */
11306           reg_count++;
11307         }
11308
11309       while (reg_count > 0)
11310         {
11311           record_buf[reg_index++] = ARM_D0_REGNUM + reg_vd + reg_count - 1;
11312           reg_count--;
11313         }
11314       arm_insn_r->reg_rec_count = reg_index;
11315     }
11316   /* VSTR Vector store register.  */
11317   else if ((opcode & 0x13) == 0x10)
11318     {
11319       uint32_t start_address, reg_rn, imm_off32, imm_off8;
11320       uint32_t memory_index = 0;
11321
11322       reg_rn = bits (arm_insn_r->arm_insn, 16, 19);
11323       regcache_raw_read_unsigned (reg_cache, reg_rn, &u_regval);
11324       imm_off8 = bits (arm_insn_r->arm_insn, 0, 7);
11325       imm_off32 = imm_off8 << 2;
11326
11327       if (bit (arm_insn_r->arm_insn, 23))
11328         start_address = u_regval + imm_off32;
11329       else
11330         start_address = u_regval - imm_off32;
11331
11332       if (single_reg)
11333         {
11334           record_buf_mem[memory_index] = 4;
11335           record_buf_mem[memory_index + 1] = start_address;
11336           arm_insn_r->mem_rec_count = 1;
11337         }
11338       else
11339         {
11340           record_buf_mem[memory_index] = 4;
11341           record_buf_mem[memory_index + 1] = start_address;
11342           record_buf_mem[memory_index + 2] = 4;
11343           record_buf_mem[memory_index + 3] = start_address + 4;
11344           arm_insn_r->mem_rec_count = 2;
11345         }
11346     }
11347   /* VLDR Vector load register.  */
11348   else if ((opcode & 0x13) == 0x11)
11349     {
11350       uint32_t reg_vd = bits (arm_insn_r->arm_insn, 12, 15);
11351
11352       if (!single_reg)
11353         {
11354           reg_vd = reg_vd | (bit (arm_insn_r->arm_insn, 22) << 4);
11355           record_buf[0] = ARM_D0_REGNUM + reg_vd;
11356         }
11357       else
11358         {
11359           reg_vd = (reg_vd << 1) | bit (arm_insn_r->arm_insn, 22);
11360           /* Record register D rather than pseudo register S.  */
11361           record_buf[0] = ARM_D0_REGNUM + reg_vd / 2;
11362         }
11363       arm_insn_r->reg_rec_count = 1;
11364     }
11365
11366   REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
11367   MEM_ALLOC (arm_insn_r->arm_mems, arm_insn_r->mem_rec_count, record_buf_mem);
11368   return 0;
11369 }
11370
11371 /* Record handler for arm/thumb mode VFP data processing instructions.  */
11372
11373 static int
11374 arm_record_vfp_data_proc_insn (insn_decode_record *arm_insn_r)
11375 {
11376   uint32_t opc1, opc2, opc3, dp_op_sz, bit_d, reg_vd;
11377   uint32_t record_buf[4];
11378   enum insn_types {INSN_T0, INSN_T1, INSN_T2, INSN_T3, INSN_INV};
11379   enum insn_types curr_insn_type = INSN_INV;
11380
11381   reg_vd = bits (arm_insn_r->arm_insn, 12, 15);
11382   opc1 = bits (arm_insn_r->arm_insn, 20, 23);
11383   opc2 = bits (arm_insn_r->arm_insn, 16, 19);
11384   opc3 = bits (arm_insn_r->arm_insn, 6, 7);
11385   dp_op_sz = bit (arm_insn_r->arm_insn, 8);
11386   bit_d = bit (arm_insn_r->arm_insn, 22);
11387   /* Mask off the "D" bit.  */
11388   opc1 = opc1 & ~0x04;
11389
11390   /* Handle VMLA, VMLS.  */
11391   if (opc1 == 0x00)
11392     {
11393       if (bit (arm_insn_r->arm_insn, 10))
11394         {
11395           if (bit (arm_insn_r->arm_insn, 6))
11396             curr_insn_type = INSN_T0;
11397           else
11398             curr_insn_type = INSN_T1;
11399         }
11400       else
11401         {
11402           if (dp_op_sz)
11403             curr_insn_type = INSN_T1;
11404           else
11405             curr_insn_type = INSN_T2;
11406         }
11407     }
11408   /* Handle VNMLA, VNMLS, VNMUL.  */
11409   else if (opc1 == 0x01)
11410     {
11411       if (dp_op_sz)
11412         curr_insn_type = INSN_T1;
11413       else
11414         curr_insn_type = INSN_T2;
11415     }
11416   /* Handle VMUL.  */
11417   else if (opc1 == 0x02 && !(opc3 & 0x01))
11418     {
11419       if (bit (arm_insn_r->arm_insn, 10))
11420         {
11421           if (bit (arm_insn_r->arm_insn, 6))
11422             curr_insn_type = INSN_T0;
11423           else
11424             curr_insn_type = INSN_T1;
11425         }
11426       else
11427         {
11428           if (dp_op_sz)
11429             curr_insn_type = INSN_T1;
11430           else
11431             curr_insn_type = INSN_T2;
11432         }
11433     }
11434   /* Handle VADD, VSUB.  */
11435   else if (opc1 == 0x03)
11436     {
11437       if (!bit (arm_insn_r->arm_insn, 9))
11438         {
11439           if (bit (arm_insn_r->arm_insn, 6))
11440             curr_insn_type = INSN_T0;
11441           else
11442             curr_insn_type = INSN_T1;
11443         }
11444       else
11445         {
11446           if (dp_op_sz)
11447             curr_insn_type = INSN_T1;
11448           else
11449             curr_insn_type = INSN_T2;
11450         }
11451     }
11452   /* Handle VDIV.  */
11453   else if (opc1 == 0x08)
11454     {
11455       if (dp_op_sz)
11456         curr_insn_type = INSN_T1;
11457       else
11458         curr_insn_type = INSN_T2;
11459     }
11460   /* Handle all other vfp data processing instructions.  */
11461   else if (opc1 == 0x0b)
11462     {
11463       /* Handle VMOV.  */
11464       if (!(opc3 & 0x01) || (opc2 == 0x00 && opc3 == 0x01))
11465         {
11466           if (bit (arm_insn_r->arm_insn, 4))
11467             {
11468               if (bit (arm_insn_r->arm_insn, 6))
11469                 curr_insn_type = INSN_T0;
11470               else
11471                 curr_insn_type = INSN_T1;
11472             }
11473           else
11474             {
11475               if (dp_op_sz)
11476                 curr_insn_type = INSN_T1;
11477               else
11478                 curr_insn_type = INSN_T2;
11479             }
11480         }
11481       /* Handle VNEG and VABS.  */
11482       else if ((opc2 == 0x01 && opc3 == 0x01)
11483               || (opc2 == 0x00 && opc3 == 0x03))
11484         {
11485           if (!bit (arm_insn_r->arm_insn, 11))
11486             {
11487               if (bit (arm_insn_r->arm_insn, 6))
11488                 curr_insn_type = INSN_T0;
11489               else
11490                 curr_insn_type = INSN_T1;
11491             }
11492           else
11493             {
11494               if (dp_op_sz)
11495                 curr_insn_type = INSN_T1;
11496               else
11497                 curr_insn_type = INSN_T2;
11498             }
11499         }
11500       /* Handle VSQRT.  */
11501       else if (opc2 == 0x01 && opc3 == 0x03)
11502         {
11503           if (dp_op_sz)
11504             curr_insn_type = INSN_T1;
11505           else
11506             curr_insn_type = INSN_T2;
11507         }
11508       /* Handle VCVT.  */
11509       else if (opc2 == 0x07 && opc3 == 0x03)
11510         {
11511           if (!dp_op_sz)
11512             curr_insn_type = INSN_T1;
11513           else
11514             curr_insn_type = INSN_T2;
11515         }
11516       else if (opc3 & 0x01)
11517         {
11518           /* Handle VCVT.  */
11519           if ((opc2 == 0x08) || (opc2 & 0x0e) == 0x0c)
11520             {
11521               if (!bit (arm_insn_r->arm_insn, 18))
11522                 curr_insn_type = INSN_T2;
11523               else
11524                 {
11525                   if (dp_op_sz)
11526                     curr_insn_type = INSN_T1;
11527                   else
11528                     curr_insn_type = INSN_T2;
11529                 }
11530             }
11531           /* Handle VCVT.  */
11532           else if ((opc2 & 0x0e) == 0x0a || (opc2 & 0x0e) == 0x0e)
11533             {
11534               if (dp_op_sz)
11535                 curr_insn_type = INSN_T1;
11536               else
11537                 curr_insn_type = INSN_T2;
11538             }
11539           /* Handle VCVTB, VCVTT.  */
11540           else if ((opc2 & 0x0e) == 0x02)
11541             curr_insn_type = INSN_T2;
11542           /* Handle VCMP, VCMPE.  */
11543           else if ((opc2 & 0x0e) == 0x04)
11544             curr_insn_type = INSN_T3;
11545         }
11546     }
11547
11548   switch (curr_insn_type)
11549     {
11550       case INSN_T0:
11551         reg_vd = reg_vd | (bit_d << 4);
11552         record_buf[0] = reg_vd + ARM_D0_REGNUM;
11553         record_buf[1] = reg_vd + ARM_D0_REGNUM + 1;
11554         arm_insn_r->reg_rec_count = 2;
11555         break;
11556
11557       case INSN_T1:
11558         reg_vd = reg_vd | (bit_d << 4);
11559         record_buf[0] = reg_vd + ARM_D0_REGNUM;
11560         arm_insn_r->reg_rec_count = 1;
11561         break;
11562
11563       case INSN_T2:
11564         reg_vd = (reg_vd << 1) | bit_d;
11565         record_buf[0] = reg_vd + ARM_D0_REGNUM;
11566         arm_insn_r->reg_rec_count = 1;
11567         break;
11568
11569       case INSN_T3:
11570         record_buf[0] = ARM_FPSCR_REGNUM;
11571         arm_insn_r->reg_rec_count = 1;
11572         break;
11573
11574       default:
11575         gdb_assert_not_reached ("no decoding pattern found");
11576         break;
11577     }
11578
11579   REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
11580   return 0;
11581 }
11582
11583 /* Handling opcode 110 insns.  */
11584
11585 static int
11586 arm_record_asimd_vfp_coproc (insn_decode_record *arm_insn_r)
11587 {
11588   uint32_t op1, op1_ebit, coproc;
11589
11590   coproc = bits (arm_insn_r->arm_insn, 8, 11);
11591   op1 = bits (arm_insn_r->arm_insn, 20, 25);
11592   op1_ebit = bit (arm_insn_r->arm_insn, 20);
11593
11594   if ((coproc & 0x0e) == 0x0a)
11595     {
11596       /* Handle extension register ld/st instructions.  */
11597       if (!(op1 & 0x20))
11598         return arm_record_exreg_ld_st_insn (arm_insn_r);
11599
11600       /* 64-bit transfers between arm core and extension registers.  */
11601       if ((op1 & 0x3e) == 0x04)
11602         return arm_record_exreg_ld_st_insn (arm_insn_r);
11603     }
11604   else
11605     {
11606       /* Handle coprocessor ld/st instructions.  */
11607       if (!(op1 & 0x3a))
11608         {
11609           /* Store.  */
11610           if (!op1_ebit)
11611             return arm_record_unsupported_insn (arm_insn_r);
11612           else
11613             /* Load.  */
11614             return arm_record_unsupported_insn (arm_insn_r);
11615         }
11616
11617       /* Move to coprocessor from two arm core registers.  */
11618       if (op1 == 0x4)
11619         return arm_record_unsupported_insn (arm_insn_r);
11620
11621       /* Move to two arm core registers from coprocessor.  */
11622       if (op1 == 0x5)
11623         {
11624           uint32_t reg_t[2];
11625
11626           reg_t[0] = bits (arm_insn_r->arm_insn, 12, 15);
11627           reg_t[1] = bits (arm_insn_r->arm_insn, 16, 19);
11628           arm_insn_r->reg_rec_count = 2;
11629
11630           REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, reg_t);
11631           return 0;
11632        }
11633     }
11634   return arm_record_unsupported_insn (arm_insn_r);
11635 }
11636
11637 /* Handling opcode 111 insns.  */
11638
11639 static int
11640 arm_record_coproc_data_proc (insn_decode_record *arm_insn_r)
11641 {
11642   uint32_t op, op1_ebit, coproc, bits_24_25;
11643   struct gdbarch_tdep *tdep = gdbarch_tdep (arm_insn_r->gdbarch);
11644   struct regcache *reg_cache = arm_insn_r->regcache;
11645
11646   arm_insn_r->opcode = bits (arm_insn_r->arm_insn, 24, 27);
11647   coproc = bits (arm_insn_r->arm_insn, 8, 11);
11648   op1_ebit = bit (arm_insn_r->arm_insn, 20);
11649   op = bit (arm_insn_r->arm_insn, 4);
11650   bits_24_25 = bits (arm_insn_r->arm_insn, 24, 25);
11651
11652   /* Handle arm SWI/SVC system call instructions.  */
11653   if (bits_24_25 == 0x3)
11654     {
11655       if (tdep->arm_syscall_record != NULL)
11656         {
11657           ULONGEST svc_operand, svc_number;
11658
11659           svc_operand = (0x00ffffff & arm_insn_r->arm_insn);
11660
11661           if (svc_operand)  /* OABI.  */
11662             svc_number = svc_operand - 0x900000;
11663           else /* EABI.  */
11664             regcache_raw_read_unsigned (reg_cache, 7, &svc_number);
11665
11666           return tdep->arm_syscall_record (reg_cache, svc_number);
11667         }
11668       else
11669         {
11670           printf_unfiltered (_("no syscall record support\n"));
11671           return -1;
11672         }
11673     }
11674   else if (bits_24_25 == 0x02)
11675     {
11676       if (op)
11677         {
11678           if ((coproc & 0x0e) == 0x0a)
11679             {
11680               /* 8, 16, and 32-bit transfer */
11681               return arm_record_vdata_transfer_insn (arm_insn_r);
11682             }
11683           else
11684             {
11685               if (op1_ebit)
11686                 {
11687                   /* MRC, MRC2 */
11688                   uint32_t record_buf[1];
11689
11690                   record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11691                   if (record_buf[0] == 15)
11692                     record_buf[0] = ARM_PS_REGNUM;
11693
11694                   arm_insn_r->reg_rec_count = 1;
11695                   REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count,
11696                              record_buf);
11697                   return 0;
11698                 }
11699               else
11700                 {
11701                   /* MCR, MCR2 */
11702                   return -1;
11703                 }
11704             }
11705         }
11706       else
11707         {
11708           if ((coproc & 0x0e) == 0x0a)
11709             {
11710               /* VFP data-processing instructions.  */
11711               return arm_record_vfp_data_proc_insn (arm_insn_r);
11712             }
11713           else
11714             {
11715               /* CDP, CDP2 */
11716               return -1;
11717             }
11718         }
11719     }
11720   else
11721     {
11722       unsigned int op1 = bits (arm_insn_r->arm_insn, 20, 25);
11723
11724       if (op1 == 5)
11725         {
11726           if ((coproc & 0x0e) != 0x0a)
11727             {
11728               /* MRRC, MRRC2 */
11729               return -1;
11730             }
11731         }
11732       else if (op1 == 4 || op1 == 5)
11733         {
11734           if ((coproc & 0x0e) == 0x0a)
11735             {
11736               /* 64-bit transfers between ARM core and extension */
11737               return -1;
11738             }
11739           else if (op1 == 4)
11740             {
11741               /* MCRR, MCRR2 */
11742               return -1;
11743             }
11744         }
11745       else if (op1 == 0 || op1 == 1)
11746         {
11747           /* UNDEFINED */
11748           return -1;
11749         }
11750       else
11751         {
11752           if ((coproc & 0x0e) == 0x0a)
11753             {
11754               /* Extension register load/store */
11755             }
11756           else
11757             {
11758               /* STC, STC2, LDC, LDC2 */
11759             }
11760           return -1;
11761         }
11762     }
11763
11764   return -1;
11765 }
11766
11767 /* Handling opcode 000 insns.  */
11768
11769 static int
11770 thumb_record_shift_add_sub (insn_decode_record *thumb_insn_r)
11771 {
11772   uint32_t record_buf[8];
11773   uint32_t reg_src1 = 0;
11774
11775   reg_src1 = bits (thumb_insn_r->arm_insn, 0, 2);
11776
11777   record_buf[0] = ARM_PS_REGNUM;
11778   record_buf[1] = reg_src1;
11779   thumb_insn_r->reg_rec_count = 2;
11780
11781   REG_ALLOC (thumb_insn_r->arm_regs, thumb_insn_r->reg_rec_count, record_buf);
11782
11783   return 0;
11784 }
11785
11786
11787 /* Handling opcode 001 insns.  */
11788
11789 static int
11790 thumb_record_add_sub_cmp_mov (insn_decode_record *thumb_insn_r)
11791 {
11792   uint32_t record_buf[8];
11793   uint32_t reg_src1 = 0;
11794
11795   reg_src1 = bits (thumb_insn_r->arm_insn, 8, 10);
11796
11797   record_buf[0] = ARM_PS_REGNUM;
11798   record_buf[1] = reg_src1;
11799   thumb_insn_r->reg_rec_count = 2;
11800
11801   REG_ALLOC (thumb_insn_r->arm_regs, thumb_insn_r->reg_rec_count, record_buf);
11802
11803   return 0;
11804 }
11805
11806 /* Handling opcode 010 insns.  */
11807
11808 static int
11809 thumb_record_ld_st_reg_offset (insn_decode_record *thumb_insn_r)
11810 {
11811   struct regcache *reg_cache =  thumb_insn_r->regcache;
11812   uint32_t record_buf[8], record_buf_mem[8];
11813
11814   uint32_t reg_src1 = 0, reg_src2 = 0;
11815   uint32_t opcode1 = 0, opcode2 = 0, opcode3 = 0;
11816
11817   ULONGEST u_regval[2] = {0};
11818
11819   opcode1 = bits (thumb_insn_r->arm_insn, 10, 12);
11820
11821   if (bit (thumb_insn_r->arm_insn, 12))
11822     {
11823       /* Handle load/store register offset.  */
11824       uint32_t opB = bits (thumb_insn_r->arm_insn, 9, 11);
11825
11826       if (in_inclusive_range (opB, 4U, 7U))
11827         {
11828           /* LDR(2), LDRB(2) , LDRH(2), LDRSB, LDRSH.  */
11829           reg_src1 = bits (thumb_insn_r->arm_insn,0, 2);
11830           record_buf[0] = reg_src1;
11831           thumb_insn_r->reg_rec_count = 1;
11832         }
11833       else if (in_inclusive_range (opB, 0U, 2U))
11834         {
11835           /* STR(2), STRB(2), STRH(2) .  */
11836           reg_src1 = bits (thumb_insn_r->arm_insn, 3, 5);
11837           reg_src2 = bits (thumb_insn_r->arm_insn, 6, 8);
11838           regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval[0]);
11839           regcache_raw_read_unsigned (reg_cache, reg_src2, &u_regval[1]);
11840           if (0 == opB)
11841             record_buf_mem[0] = 4;    /* STR (2).  */
11842           else if (2 == opB)
11843             record_buf_mem[0] = 1;    /*  STRB (2).  */
11844           else if (1 == opB)
11845             record_buf_mem[0] = 2;    /* STRH (2).  */
11846           record_buf_mem[1] = u_regval[0] + u_regval[1];
11847           thumb_insn_r->mem_rec_count = 1;
11848         }
11849     }
11850   else if (bit (thumb_insn_r->arm_insn, 11))
11851     {
11852       /* Handle load from literal pool.  */
11853       /* LDR(3).  */
11854       reg_src1 = bits (thumb_insn_r->arm_insn, 8, 10);
11855       record_buf[0] = reg_src1;
11856       thumb_insn_r->reg_rec_count = 1;
11857     }
11858   else if (opcode1)
11859     {
11860       /* Special data instructions and branch and exchange */
11861       opcode2 = bits (thumb_insn_r->arm_insn, 8, 9);
11862       opcode3 = bits (thumb_insn_r->arm_insn, 0, 2);
11863       if ((3 == opcode2) && (!opcode3))
11864         {
11865           /* Branch with exchange.  */
11866           record_buf[0] = ARM_PS_REGNUM;
11867           thumb_insn_r->reg_rec_count = 1;
11868         }
11869       else
11870         {
11871           /* Format 8; special data processing insns.  */
11872           record_buf[0] = ARM_PS_REGNUM;
11873           record_buf[1] = (bit (thumb_insn_r->arm_insn, 7) << 3
11874                            | bits (thumb_insn_r->arm_insn, 0, 2));
11875           thumb_insn_r->reg_rec_count = 2;
11876         }
11877     }
11878   else
11879     {
11880       /* Format 5; data processing insns.  */
11881       reg_src1 = bits (thumb_insn_r->arm_insn, 0, 2);
11882       if (bit (thumb_insn_r->arm_insn, 7))
11883         {
11884           reg_src1 = reg_src1 + 8;
11885         }
11886       record_buf[0] = ARM_PS_REGNUM;
11887       record_buf[1] = reg_src1;
11888       thumb_insn_r->reg_rec_count = 2;
11889     }
11890
11891   REG_ALLOC (thumb_insn_r->arm_regs, thumb_insn_r->reg_rec_count, record_buf);
11892   MEM_ALLOC (thumb_insn_r->arm_mems, thumb_insn_r->mem_rec_count,
11893              record_buf_mem);
11894
11895   return 0;
11896 }
11897
11898 /* Handling opcode 001 insns.  */
11899
11900 static int
11901 thumb_record_ld_st_imm_offset (insn_decode_record *thumb_insn_r)
11902 {
11903   struct regcache *reg_cache = thumb_insn_r->regcache;
11904   uint32_t record_buf[8], record_buf_mem[8];
11905
11906   uint32_t reg_src1 = 0;
11907   uint32_t opcode = 0, immed_5 = 0;
11908
11909   ULONGEST u_regval = 0;
11910
11911   opcode = bits (thumb_insn_r->arm_insn, 11, 12);
11912
11913   if (opcode)
11914     {
11915       /* LDR(1).  */
11916       reg_src1 = bits (thumb_insn_r->arm_insn, 0, 2);
11917       record_buf[0] = reg_src1;
11918       thumb_insn_r->reg_rec_count = 1;
11919     }
11920   else
11921     {
11922       /* STR(1).  */
11923       reg_src1 = bits (thumb_insn_r->arm_insn, 3, 5);
11924       immed_5 = bits (thumb_insn_r->arm_insn, 6, 10);
11925       regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval);
11926       record_buf_mem[0] = 4;
11927       record_buf_mem[1] = u_regval + (immed_5 * 4);
11928       thumb_insn_r->mem_rec_count = 1;
11929     }
11930
11931   REG_ALLOC (thumb_insn_r->arm_regs, thumb_insn_r->reg_rec_count, record_buf);
11932   MEM_ALLOC (thumb_insn_r->arm_mems, thumb_insn_r->mem_rec_count, 
11933              record_buf_mem);
11934
11935   return 0;
11936 }
11937
11938 /* Handling opcode 100 insns.  */
11939
11940 static int
11941 thumb_record_ld_st_stack (insn_decode_record *thumb_insn_r)
11942 {
11943   struct regcache *reg_cache = thumb_insn_r->regcache;
11944   uint32_t record_buf[8], record_buf_mem[8];
11945
11946   uint32_t reg_src1 = 0;
11947   uint32_t opcode = 0, immed_8 = 0, immed_5 = 0;
11948
11949   ULONGEST u_regval = 0;
11950
11951   opcode = bits (thumb_insn_r->arm_insn, 11, 12);
11952
11953   if (3 == opcode)
11954     {
11955       /* LDR(4).  */
11956       reg_src1 = bits (thumb_insn_r->arm_insn, 8, 10);
11957       record_buf[0] = reg_src1;
11958       thumb_insn_r->reg_rec_count = 1;
11959     }
11960   else if (1 == opcode)
11961     {
11962       /* LDRH(1).  */
11963       reg_src1 = bits (thumb_insn_r->arm_insn, 0, 2);
11964       record_buf[0] = reg_src1;
11965       thumb_insn_r->reg_rec_count = 1;
11966     }
11967   else if (2 == opcode)
11968     {
11969       /* STR(3).  */
11970       immed_8 = bits (thumb_insn_r->arm_insn, 0, 7);
11971       regcache_raw_read_unsigned (reg_cache, ARM_SP_REGNUM, &u_regval);
11972       record_buf_mem[0] = 4;
11973       record_buf_mem[1] = u_regval + (immed_8 * 4);
11974       thumb_insn_r->mem_rec_count = 1;
11975     }
11976   else if (0 == opcode)
11977     {
11978       /* STRH(1).  */
11979       immed_5 = bits (thumb_insn_r->arm_insn, 6, 10);
11980       reg_src1 = bits (thumb_insn_r->arm_insn, 3, 5);
11981       regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval);
11982       record_buf_mem[0] = 2;
11983       record_buf_mem[1] = u_regval + (immed_5 * 2);
11984       thumb_insn_r->mem_rec_count = 1;
11985     }
11986
11987   REG_ALLOC (thumb_insn_r->arm_regs, thumb_insn_r->reg_rec_count, record_buf);
11988   MEM_ALLOC (thumb_insn_r->arm_mems, thumb_insn_r->mem_rec_count,
11989              record_buf_mem);
11990
11991   return 0;
11992 }
11993
11994 /* Handling opcode 101 insns.  */
11995
11996 static int
11997 thumb_record_misc (insn_decode_record *thumb_insn_r)
11998 {
11999   struct regcache *reg_cache = thumb_insn_r->regcache;
12000
12001   uint32_t opcode = 0;
12002   uint32_t register_bits = 0, register_count = 0;
12003   uint32_t index = 0, start_address = 0;
12004   uint32_t record_buf[24], record_buf_mem[48];
12005   uint32_t reg_src1;
12006
12007   ULONGEST u_regval = 0;
12008
12009   opcode = bits (thumb_insn_r->arm_insn, 11, 12);
12010
12011   if (opcode == 0 || opcode == 1)
12012     {
12013       /* ADR and ADD (SP plus immediate) */
12014
12015       reg_src1 = bits (thumb_insn_r->arm_insn, 8, 10);
12016       record_buf[0] = reg_src1;
12017       thumb_insn_r->reg_rec_count = 1;
12018     }
12019   else
12020     {
12021       /* Miscellaneous 16-bit instructions */
12022       uint32_t opcode2 = bits (thumb_insn_r->arm_insn, 8, 11);
12023
12024       switch (opcode2)
12025         {
12026         case 6:
12027           /* SETEND and CPS */
12028           break;
12029         case 0:
12030           /* ADD/SUB (SP plus immediate)  */
12031           reg_src1 = bits (thumb_insn_r->arm_insn, 8, 10);
12032           record_buf[0] = ARM_SP_REGNUM;
12033           thumb_insn_r->reg_rec_count = 1;
12034           break;
12035         case 1: /* fall through  */
12036         case 3: /* fall through  */
12037         case 9: /* fall through  */
12038         case 11:
12039           /* CBNZ, CBZ */
12040           break;
12041         case 2:
12042           /* SXTH, SXTB, UXTH, UXTB */
12043           record_buf[0] = bits (thumb_insn_r->arm_insn, 0, 2);
12044           thumb_insn_r->reg_rec_count = 1;
12045           break;
12046         case 4: /* fall through  */
12047         case 5:
12048           /* PUSH.  */
12049           register_bits = bits (thumb_insn_r->arm_insn, 0, 7);
12050           regcache_raw_read_unsigned (reg_cache, ARM_SP_REGNUM, &u_regval);
12051           while (register_bits)
12052             {
12053               if (register_bits & 0x00000001)
12054                 register_count++;
12055               register_bits = register_bits >> 1;
12056             }
12057           start_address = u_regval -  \
12058             (4 * (bit (thumb_insn_r->arm_insn, 8) + register_count));
12059           thumb_insn_r->mem_rec_count = register_count;
12060           while (register_count)
12061             {
12062               record_buf_mem[(register_count * 2) - 1] = start_address;
12063               record_buf_mem[(register_count * 2) - 2] = 4;
12064               start_address = start_address + 4;
12065               register_count--;
12066             }
12067           record_buf[0] = ARM_SP_REGNUM;
12068           thumb_insn_r->reg_rec_count = 1;
12069           break;
12070         case 10:
12071           /* REV, REV16, REVSH */
12072           record_buf[0] = bits (thumb_insn_r->arm_insn, 0, 2);
12073           thumb_insn_r->reg_rec_count = 1;
12074           break;
12075         case 12: /* fall through  */
12076         case 13:
12077           /* POP.  */
12078           register_bits = bits (thumb_insn_r->arm_insn, 0, 7);
12079           while (register_bits)
12080             {
12081               if (register_bits & 0x00000001)
12082                 record_buf[index++] = register_count;
12083               register_bits = register_bits >> 1;
12084               register_count++;
12085             }
12086           record_buf[index++] = ARM_PS_REGNUM;
12087           record_buf[index++] = ARM_SP_REGNUM;
12088           thumb_insn_r->reg_rec_count = index;
12089           break;
12090         case 0xe:
12091           /* BKPT insn.  */
12092           /* Handle enhanced software breakpoint insn, BKPT.  */
12093           /* CPSR is changed to be executed in ARM state,  disabling normal
12094              interrupts, entering abort mode.  */
12095           /* According to high vector configuration PC is set.  */
12096           /* User hits breakpoint and type reverse, in that case, we need to go back with 
12097              previous CPSR and Program Counter.  */
12098           record_buf[0] = ARM_PS_REGNUM;
12099           record_buf[1] = ARM_LR_REGNUM;
12100           thumb_insn_r->reg_rec_count = 2;
12101           /* We need to save SPSR value, which is not yet done.  */
12102           printf_unfiltered (_("Process record does not support instruction "
12103                                "0x%0x at address %s.\n"),
12104                              thumb_insn_r->arm_insn,
12105                              paddress (thumb_insn_r->gdbarch,
12106                                        thumb_insn_r->this_addr));
12107           return -1;
12108
12109         case 0xf:
12110           /* If-Then, and hints */
12111           break;
12112         default:
12113           return -1;
12114         };
12115     }
12116
12117   REG_ALLOC (thumb_insn_r->arm_regs, thumb_insn_r->reg_rec_count, record_buf);
12118   MEM_ALLOC (thumb_insn_r->arm_mems, thumb_insn_r->mem_rec_count,
12119              record_buf_mem);
12120
12121   return 0;
12122 }
12123
12124 /* Handling opcode 110 insns.  */
12125
12126 static int
12127 thumb_record_ldm_stm_swi (insn_decode_record *thumb_insn_r)                
12128 {
12129   struct gdbarch_tdep *tdep = gdbarch_tdep (thumb_insn_r->gdbarch);
12130   struct regcache *reg_cache = thumb_insn_r->regcache;
12131
12132   uint32_t ret = 0; /* function return value: -1:record failure ;  0:success  */
12133   uint32_t reg_src1 = 0;
12134   uint32_t opcode1 = 0, opcode2 = 0, register_bits = 0, register_count = 0;
12135   uint32_t index = 0, start_address = 0;
12136   uint32_t record_buf[24], record_buf_mem[48];
12137
12138   ULONGEST u_regval = 0;
12139
12140   opcode1 = bits (thumb_insn_r->arm_insn, 8, 12);
12141   opcode2 = bits (thumb_insn_r->arm_insn, 11, 12);
12142
12143   if (1 == opcode2)
12144     {
12145
12146       /* LDMIA.  */
12147       register_bits = bits (thumb_insn_r->arm_insn, 0, 7);
12148       /* Get Rn.  */
12149       reg_src1 = bits (thumb_insn_r->arm_insn, 8, 10);
12150       while (register_bits)
12151         {
12152           if (register_bits & 0x00000001)
12153             record_buf[index++] = register_count;
12154           register_bits = register_bits >> 1;
12155           register_count++;
12156         }
12157       record_buf[index++] = reg_src1;
12158       thumb_insn_r->reg_rec_count = index;
12159     }
12160   else if (0 == opcode2)
12161     {
12162       /* It handles both STMIA.  */
12163       register_bits = bits (thumb_insn_r->arm_insn, 0, 7);
12164       /* Get Rn.  */
12165       reg_src1 = bits (thumb_insn_r->arm_insn, 8, 10);
12166       regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval);
12167       while (register_bits)
12168         {
12169           if (register_bits & 0x00000001)
12170             register_count++;
12171           register_bits = register_bits >> 1;
12172         }
12173       start_address = u_regval;
12174       thumb_insn_r->mem_rec_count = register_count;
12175       while (register_count)
12176         {
12177           record_buf_mem[(register_count * 2) - 1] = start_address;
12178           record_buf_mem[(register_count * 2) - 2] = 4;
12179           start_address = start_address + 4;
12180           register_count--;
12181         }
12182     }
12183   else if (0x1F == opcode1)
12184     {
12185         /* Handle arm syscall insn.  */
12186         if (tdep->arm_syscall_record != NULL)
12187           {
12188             regcache_raw_read_unsigned (reg_cache, 7, &u_regval);
12189             ret = tdep->arm_syscall_record (reg_cache, u_regval);
12190           }
12191         else
12192           {
12193             printf_unfiltered (_("no syscall record support\n"));
12194             return -1;
12195           }
12196     }
12197
12198   /* B (1), conditional branch is automatically taken care in process_record,
12199     as PC is saved there.  */
12200
12201   REG_ALLOC (thumb_insn_r->arm_regs, thumb_insn_r->reg_rec_count, record_buf);
12202   MEM_ALLOC (thumb_insn_r->arm_mems, thumb_insn_r->mem_rec_count,
12203              record_buf_mem);
12204
12205   return ret;
12206 }
12207
12208 /* Handling opcode 111 insns.  */
12209
12210 static int
12211 thumb_record_branch (insn_decode_record *thumb_insn_r)
12212 {
12213   uint32_t record_buf[8];
12214   uint32_t bits_h = 0;
12215
12216   bits_h = bits (thumb_insn_r->arm_insn, 11, 12);
12217
12218   if (2 == bits_h || 3 == bits_h)
12219     {
12220       /* BL */
12221       record_buf[0] = ARM_LR_REGNUM;
12222       thumb_insn_r->reg_rec_count = 1;
12223     }
12224   else if (1 == bits_h)
12225     {
12226       /* BLX(1). */
12227       record_buf[0] = ARM_PS_REGNUM;
12228       record_buf[1] = ARM_LR_REGNUM;
12229       thumb_insn_r->reg_rec_count = 2;
12230     }
12231
12232   /* B(2) is automatically taken care in process_record, as PC is 
12233      saved there.  */
12234
12235   REG_ALLOC (thumb_insn_r->arm_regs, thumb_insn_r->reg_rec_count, record_buf);
12236
12237   return 0;     
12238 }
12239
12240 /* Handler for thumb2 load/store multiple instructions.  */
12241
12242 static int
12243 thumb2_record_ld_st_multiple (insn_decode_record *thumb2_insn_r)
12244 {
12245   struct regcache *reg_cache = thumb2_insn_r->regcache;
12246
12247   uint32_t reg_rn, op;
12248   uint32_t register_bits = 0, register_count = 0;
12249   uint32_t index = 0, start_address = 0;
12250   uint32_t record_buf[24], record_buf_mem[48];
12251
12252   ULONGEST u_regval = 0;
12253
12254   reg_rn = bits (thumb2_insn_r->arm_insn, 16, 19);
12255   op = bits (thumb2_insn_r->arm_insn, 23, 24);
12256
12257   if (0 == op || 3 == op)
12258     {
12259       if (bit (thumb2_insn_r->arm_insn, INSN_S_L_BIT_NUM))
12260         {
12261           /* Handle RFE instruction.  */
12262           record_buf[0] = ARM_PS_REGNUM;
12263           thumb2_insn_r->reg_rec_count = 1;
12264         }
12265       else
12266         {
12267           /* Handle SRS instruction after reading banked SP.  */
12268           return arm_record_unsupported_insn (thumb2_insn_r);
12269         }
12270     }
12271   else if (1 == op || 2 == op)
12272     {
12273       if (bit (thumb2_insn_r->arm_insn, INSN_S_L_BIT_NUM))
12274         {
12275           /* Handle LDM/LDMIA/LDMFD and LDMDB/LDMEA instructions.  */
12276           register_bits = bits (thumb2_insn_r->arm_insn, 0, 15);
12277           while (register_bits)
12278             {
12279               if (register_bits & 0x00000001)
12280                 record_buf[index++] = register_count;
12281
12282               register_count++;
12283               register_bits = register_bits >> 1;
12284             }
12285           record_buf[index++] = reg_rn;
12286           record_buf[index++] = ARM_PS_REGNUM;
12287           thumb2_insn_r->reg_rec_count = index;
12288         }
12289       else
12290         {
12291           /* Handle STM/STMIA/STMEA and STMDB/STMFD.  */
12292           register_bits = bits (thumb2_insn_r->arm_insn, 0, 15);
12293           regcache_raw_read_unsigned (reg_cache, reg_rn, &u_regval);
12294           while (register_bits)
12295             {
12296               if (register_bits & 0x00000001)
12297                 register_count++;
12298
12299               register_bits = register_bits >> 1;
12300             }
12301
12302           if (1 == op)
12303             {
12304               /* Start address calculation for LDMDB/LDMEA.  */
12305               start_address = u_regval;
12306             }
12307           else if (2 == op)
12308             {
12309               /* Start address calculation for LDMDB/LDMEA.  */
12310               start_address = u_regval - register_count * 4;
12311             }
12312
12313           thumb2_insn_r->mem_rec_count = register_count;
12314           while (register_count)
12315             {
12316               record_buf_mem[register_count * 2 - 1] = start_address;
12317               record_buf_mem[register_count * 2 - 2] = 4;
12318               start_address = start_address + 4;
12319               register_count--;
12320             }
12321           record_buf[0] = reg_rn;
12322           record_buf[1] = ARM_PS_REGNUM;
12323           thumb2_insn_r->reg_rec_count = 2;
12324         }
12325     }
12326
12327   MEM_ALLOC (thumb2_insn_r->arm_mems, thumb2_insn_r->mem_rec_count,
12328             record_buf_mem);
12329   REG_ALLOC (thumb2_insn_r->arm_regs, thumb2_insn_r->reg_rec_count,
12330             record_buf);
12331   return ARM_RECORD_SUCCESS;
12332 }
12333
12334 /* Handler for thumb2 load/store (dual/exclusive) and table branch
12335    instructions.  */
12336
12337 static int
12338 thumb2_record_ld_st_dual_ex_tbb (insn_decode_record *thumb2_insn_r)
12339 {
12340   struct regcache *reg_cache = thumb2_insn_r->regcache;
12341
12342   uint32_t reg_rd, reg_rn, offset_imm;
12343   uint32_t reg_dest1, reg_dest2;
12344   uint32_t address, offset_addr;
12345   uint32_t record_buf[8], record_buf_mem[8];
12346   uint32_t op1, op2, op3;
12347
12348   ULONGEST u_regval[2];
12349
12350   op1 = bits (thumb2_insn_r->arm_insn, 23, 24);
12351   op2 = bits (thumb2_insn_r->arm_insn, 20, 21);
12352   op3 = bits (thumb2_insn_r->arm_insn, 4, 7);
12353
12354   if (bit (thumb2_insn_r->arm_insn, INSN_S_L_BIT_NUM))
12355     {
12356       if(!(1 == op1 && 1 == op2 && (0 == op3 || 1 == op3)))
12357         {
12358           reg_dest1 = bits (thumb2_insn_r->arm_insn, 12, 15);
12359           record_buf[0] = reg_dest1;
12360           record_buf[1] = ARM_PS_REGNUM;
12361           thumb2_insn_r->reg_rec_count = 2;
12362         }
12363
12364       if (3 == op2 || (op1 & 2) || (1 == op1 && 1 == op2 && 7 == op3))
12365         {
12366           reg_dest2 = bits (thumb2_insn_r->arm_insn, 8, 11);
12367           record_buf[2] = reg_dest2;
12368           thumb2_insn_r->reg_rec_count = 3;
12369         }
12370     }
12371   else
12372     {
12373       reg_rn = bits (thumb2_insn_r->arm_insn, 16, 19);
12374       regcache_raw_read_unsigned (reg_cache, reg_rn, &u_regval[0]);
12375
12376       if (0 == op1 && 0 == op2)
12377         {
12378           /* Handle STREX.  */
12379           offset_imm = bits (thumb2_insn_r->arm_insn, 0, 7);
12380           address = u_regval[0] + (offset_imm * 4);
12381           record_buf_mem[0] = 4;
12382           record_buf_mem[1] = address;
12383           thumb2_insn_r->mem_rec_count = 1;
12384           reg_rd = bits (thumb2_insn_r->arm_insn, 0, 3);
12385           record_buf[0] = reg_rd;
12386           thumb2_insn_r->reg_rec_count = 1;
12387         }
12388       else if (1 == op1 && 0 == op2)
12389         {
12390           reg_rd = bits (thumb2_insn_r->arm_insn, 0, 3);
12391           record_buf[0] = reg_rd;
12392           thumb2_insn_r->reg_rec_count = 1;
12393           address = u_regval[0];
12394           record_buf_mem[1] = address;
12395
12396           if (4 == op3)
12397             {
12398               /* Handle STREXB.  */
12399               record_buf_mem[0] = 1;
12400               thumb2_insn_r->mem_rec_count = 1;
12401             }
12402           else if (5 == op3)
12403             {
12404               /* Handle STREXH.  */
12405               record_buf_mem[0] = 2 ;
12406               thumb2_insn_r->mem_rec_count = 1;
12407             }
12408           else if (7 == op3)
12409             {
12410               /* Handle STREXD.  */
12411               address = u_regval[0];
12412               record_buf_mem[0] = 4;
12413               record_buf_mem[2] = 4;
12414               record_buf_mem[3] = address + 4;
12415               thumb2_insn_r->mem_rec_count = 2;
12416             }
12417         }
12418       else
12419         {
12420           offset_imm = bits (thumb2_insn_r->arm_insn, 0, 7);
12421
12422           if (bit (thumb2_insn_r->arm_insn, 24))
12423             {
12424               if (bit (thumb2_insn_r->arm_insn, 23))
12425                 offset_addr = u_regval[0] + (offset_imm * 4);
12426               else
12427                 offset_addr = u_regval[0] - (offset_imm * 4);
12428
12429               address = offset_addr;
12430             }
12431           else
12432             address = u_regval[0];
12433
12434           record_buf_mem[0] = 4;
12435           record_buf_mem[1] = address;
12436           record_buf_mem[2] = 4;
12437           record_buf_mem[3] = address + 4;
12438           thumb2_insn_r->mem_rec_count = 2;
12439           record_buf[0] = reg_rn;
12440           thumb2_insn_r->reg_rec_count = 1;
12441         }
12442     }
12443
12444   REG_ALLOC (thumb2_insn_r->arm_regs, thumb2_insn_r->reg_rec_count,
12445             record_buf);
12446   MEM_ALLOC (thumb2_insn_r->arm_mems, thumb2_insn_r->mem_rec_count,
12447             record_buf_mem);
12448   return ARM_RECORD_SUCCESS;
12449 }
12450
12451 /* Handler for thumb2 data processing (shift register and modified immediate)
12452    instructions.  */
12453
12454 static int
12455 thumb2_record_data_proc_sreg_mimm (insn_decode_record *thumb2_insn_r)
12456 {
12457   uint32_t reg_rd, op;
12458   uint32_t record_buf[8];
12459
12460   op = bits (thumb2_insn_r->arm_insn, 21, 24);
12461   reg_rd = bits (thumb2_insn_r->arm_insn, 8, 11);
12462
12463   if ((0 == op || 4 == op || 8 == op || 13 == op) && 15 == reg_rd)
12464     {
12465       record_buf[0] = ARM_PS_REGNUM;
12466       thumb2_insn_r->reg_rec_count = 1;
12467     }
12468   else
12469     {
12470       record_buf[0] = reg_rd;
12471       record_buf[1] = ARM_PS_REGNUM;
12472       thumb2_insn_r->reg_rec_count = 2;
12473     }
12474
12475   REG_ALLOC (thumb2_insn_r->arm_regs, thumb2_insn_r->reg_rec_count,
12476             record_buf);
12477   return ARM_RECORD_SUCCESS;
12478 }
12479
12480 /* Generic handler for thumb2 instructions which effect destination and PS
12481    registers.  */
12482
12483 static int
12484 thumb2_record_ps_dest_generic (insn_decode_record *thumb2_insn_r)
12485 {
12486   uint32_t reg_rd;
12487   uint32_t record_buf[8];
12488
12489   reg_rd = bits (thumb2_insn_r->arm_insn, 8, 11);
12490
12491   record_buf[0] = reg_rd;
12492   record_buf[1] = ARM_PS_REGNUM;
12493   thumb2_insn_r->reg_rec_count = 2;
12494
12495   REG_ALLOC (thumb2_insn_r->arm_regs, thumb2_insn_r->reg_rec_count,
12496             record_buf);
12497   return ARM_RECORD_SUCCESS;
12498 }
12499
12500 /* Handler for thumb2 branch and miscellaneous control instructions.  */
12501
12502 static int
12503 thumb2_record_branch_misc_cntrl (insn_decode_record *thumb2_insn_r)
12504 {
12505   uint32_t op, op1, op2;
12506   uint32_t record_buf[8];
12507
12508   op = bits (thumb2_insn_r->arm_insn, 20, 26);
12509   op1 = bits (thumb2_insn_r->arm_insn, 12, 14);
12510   op2 = bits (thumb2_insn_r->arm_insn, 8, 11);
12511
12512   /* Handle MSR insn.  */
12513   if (!(op1 & 0x2) && 0x38 == op)
12514     {
12515       if (!(op2 & 0x3))
12516         {
12517           /* CPSR is going to be changed.  */
12518           record_buf[0] = ARM_PS_REGNUM;
12519           thumb2_insn_r->reg_rec_count = 1;
12520         }
12521       else
12522         {
12523           arm_record_unsupported_insn(thumb2_insn_r);
12524           return -1;
12525         }
12526     }
12527   else if (4 == (op1 & 0x5) || 5 == (op1 & 0x5))
12528     {
12529       /* BLX.  */
12530       record_buf[0] = ARM_PS_REGNUM;
12531       record_buf[1] = ARM_LR_REGNUM;
12532       thumb2_insn_r->reg_rec_count = 2;
12533     }
12534
12535   REG_ALLOC (thumb2_insn_r->arm_regs, thumb2_insn_r->reg_rec_count,
12536             record_buf);
12537   return ARM_RECORD_SUCCESS;
12538 }
12539
12540 /* Handler for thumb2 store single data item instructions.  */
12541
12542 static int
12543 thumb2_record_str_single_data (insn_decode_record *thumb2_insn_r)
12544 {
12545   struct regcache *reg_cache = thumb2_insn_r->regcache;
12546
12547   uint32_t reg_rn, reg_rm, offset_imm, shift_imm;
12548   uint32_t address, offset_addr;
12549   uint32_t record_buf[8], record_buf_mem[8];
12550   uint32_t op1, op2;
12551
12552   ULONGEST u_regval[2];
12553
12554   op1 = bits (thumb2_insn_r->arm_insn, 21, 23);
12555   op2 = bits (thumb2_insn_r->arm_insn, 6, 11);
12556   reg_rn = bits (thumb2_insn_r->arm_insn, 16, 19);
12557   regcache_raw_read_unsigned (reg_cache, reg_rn, &u_regval[0]);
12558
12559   if (bit (thumb2_insn_r->arm_insn, 23))
12560     {
12561       /* T2 encoding.  */
12562       offset_imm = bits (thumb2_insn_r->arm_insn, 0, 11);
12563       offset_addr = u_regval[0] + offset_imm;
12564       address = offset_addr;
12565     }
12566   else
12567     {
12568       /* T3 encoding.  */
12569       if ((0 == op1 || 1 == op1 || 2 == op1) && !(op2 & 0x20))
12570         {
12571           /* Handle STRB (register).  */
12572           reg_rm = bits (thumb2_insn_r->arm_insn, 0, 3);
12573           regcache_raw_read_unsigned (reg_cache, reg_rm, &u_regval[1]);
12574           shift_imm = bits (thumb2_insn_r->arm_insn, 4, 5);
12575           offset_addr = u_regval[1] << shift_imm;
12576           address = u_regval[0] + offset_addr;
12577         }
12578       else
12579         {
12580           offset_imm = bits (thumb2_insn_r->arm_insn, 0, 7);
12581           if (bit (thumb2_insn_r->arm_insn, 10))
12582             {
12583               if (bit (thumb2_insn_r->arm_insn, 9))
12584                 offset_addr = u_regval[0] + offset_imm;
12585               else
12586                 offset_addr = u_regval[0] - offset_imm;
12587
12588               address = offset_addr;
12589             }
12590           else
12591             address = u_regval[0];
12592         }
12593     }
12594
12595   switch (op1)
12596     {
12597       /* Store byte instructions.  */
12598       case 4:
12599       case 0:
12600         record_buf_mem[0] = 1;
12601         break;
12602       /* Store half word instructions.  */
12603       case 1:
12604       case 5:
12605         record_buf_mem[0] = 2;
12606         break;
12607       /* Store word instructions.  */
12608       case 2:
12609       case 6:
12610         record_buf_mem[0] = 4;
12611         break;
12612
12613       default:
12614         gdb_assert_not_reached ("no decoding pattern found");
12615         break;
12616     }
12617
12618   record_buf_mem[1] = address;
12619   thumb2_insn_r->mem_rec_count = 1;
12620   record_buf[0] = reg_rn;
12621   thumb2_insn_r->reg_rec_count = 1;
12622
12623   REG_ALLOC (thumb2_insn_r->arm_regs, thumb2_insn_r->reg_rec_count,
12624             record_buf);
12625   MEM_ALLOC (thumb2_insn_r->arm_mems, thumb2_insn_r->mem_rec_count,
12626             record_buf_mem);
12627   return ARM_RECORD_SUCCESS;
12628 }
12629
12630 /* Handler for thumb2 load memory hints instructions.  */
12631
12632 static int
12633 thumb2_record_ld_mem_hints (insn_decode_record *thumb2_insn_r)
12634 {
12635   uint32_t record_buf[8];
12636   uint32_t reg_rt, reg_rn;
12637
12638   reg_rt = bits (thumb2_insn_r->arm_insn, 12, 15);
12639   reg_rn = bits (thumb2_insn_r->arm_insn, 16, 19);
12640
12641   if (ARM_PC_REGNUM != reg_rt)
12642     {
12643       record_buf[0] = reg_rt;
12644       record_buf[1] = reg_rn;
12645       record_buf[2] = ARM_PS_REGNUM;
12646       thumb2_insn_r->reg_rec_count = 3;
12647
12648       REG_ALLOC (thumb2_insn_r->arm_regs, thumb2_insn_r->reg_rec_count,
12649                 record_buf);
12650       return ARM_RECORD_SUCCESS;
12651     }
12652
12653   return ARM_RECORD_FAILURE;
12654 }
12655
12656 /* Handler for thumb2 load word instructions.  */
12657
12658 static int
12659 thumb2_record_ld_word (insn_decode_record *thumb2_insn_r)
12660 {
12661   uint32_t record_buf[8];
12662
12663   record_buf[0] = bits (thumb2_insn_r->arm_insn, 12, 15);
12664   record_buf[1] = ARM_PS_REGNUM;
12665   thumb2_insn_r->reg_rec_count = 2;
12666
12667   REG_ALLOC (thumb2_insn_r->arm_regs, thumb2_insn_r->reg_rec_count,
12668             record_buf);
12669   return ARM_RECORD_SUCCESS;
12670 }
12671
12672 /* Handler for thumb2 long multiply, long multiply accumulate, and
12673    divide instructions.  */
12674
12675 static int
12676 thumb2_record_lmul_lmla_div (insn_decode_record *thumb2_insn_r)
12677 {
12678   uint32_t opcode1 = 0, opcode2 = 0;
12679   uint32_t record_buf[8];
12680
12681   opcode1 = bits (thumb2_insn_r->arm_insn, 20, 22);
12682   opcode2 = bits (thumb2_insn_r->arm_insn, 4, 7);
12683
12684   if (0 == opcode1 || 2 == opcode1 || (opcode1 >= 4 && opcode1 <= 6))
12685     {
12686       /* Handle SMULL, UMULL, SMULAL.  */
12687       /* Handle SMLAL(S), SMULL(S), UMLAL(S), UMULL(S).  */
12688       record_buf[0] = bits (thumb2_insn_r->arm_insn, 16, 19);
12689       record_buf[1] = bits (thumb2_insn_r->arm_insn, 12, 15);
12690       record_buf[2] = ARM_PS_REGNUM;
12691       thumb2_insn_r->reg_rec_count = 3;
12692     }
12693   else if (1 == opcode1 || 3 == opcode2)
12694     {
12695       /* Handle SDIV and UDIV.  */
12696       record_buf[0] = bits (thumb2_insn_r->arm_insn, 16, 19);
12697       record_buf[1] = bits (thumb2_insn_r->arm_insn, 12, 15);
12698       record_buf[2] = ARM_PS_REGNUM;
12699       thumb2_insn_r->reg_rec_count = 3;
12700     }
12701   else
12702     return ARM_RECORD_FAILURE;
12703
12704   REG_ALLOC (thumb2_insn_r->arm_regs, thumb2_insn_r->reg_rec_count,
12705             record_buf);
12706   return ARM_RECORD_SUCCESS;
12707 }
12708
12709 /* Record handler for thumb32 coprocessor instructions.  */
12710
12711 static int
12712 thumb2_record_coproc_insn (insn_decode_record *thumb2_insn_r)
12713 {
12714   if (bit (thumb2_insn_r->arm_insn, 25))
12715     return arm_record_coproc_data_proc (thumb2_insn_r);
12716   else
12717     return arm_record_asimd_vfp_coproc (thumb2_insn_r);
12718 }
12719
12720 /* Record handler for advance SIMD structure load/store instructions.  */
12721
12722 static int
12723 thumb2_record_asimd_struct_ld_st (insn_decode_record *thumb2_insn_r)
12724 {
12725   struct regcache *reg_cache = thumb2_insn_r->regcache;
12726   uint32_t l_bit, a_bit, b_bits;
12727   uint32_t record_buf[128], record_buf_mem[128];
12728   uint32_t reg_rn, reg_vd, address, f_elem;
12729   uint32_t index_r = 0, index_e = 0, bf_regs = 0, index_m = 0, loop_t = 0;
12730   uint8_t f_ebytes;
12731
12732   l_bit = bit (thumb2_insn_r->arm_insn, 21);
12733   a_bit = bit (thumb2_insn_r->arm_insn, 23);
12734   b_bits = bits (thumb2_insn_r->arm_insn, 8, 11);
12735   reg_rn = bits (thumb2_insn_r->arm_insn, 16, 19);
12736   reg_vd = bits (thumb2_insn_r->arm_insn, 12, 15);
12737   reg_vd = (bit (thumb2_insn_r->arm_insn, 22) << 4) | reg_vd;
12738   f_ebytes = (1 << bits (thumb2_insn_r->arm_insn, 6, 7));
12739   f_elem = 8 / f_ebytes;
12740
12741   if (!l_bit)
12742     {
12743       ULONGEST u_regval = 0;
12744       regcache_raw_read_unsigned (reg_cache, reg_rn, &u_regval);
12745       address = u_regval;
12746
12747       if (!a_bit)
12748         {
12749           /* Handle VST1.  */
12750           if (b_bits == 0x02 || b_bits == 0x0a || (b_bits & 0x0e) == 0x06)
12751             {
12752               if (b_bits == 0x07)
12753                 bf_regs = 1;
12754               else if (b_bits == 0x0a)
12755                 bf_regs = 2;
12756               else if (b_bits == 0x06)
12757                 bf_regs = 3;
12758               else if (b_bits == 0x02)
12759                 bf_regs = 4;
12760               else
12761                 bf_regs = 0;
12762
12763               for (index_r = 0; index_r < bf_regs; index_r++)
12764                 {
12765                   for (index_e = 0; index_e < f_elem; index_e++)
12766                     {
12767                       record_buf_mem[index_m++] = f_ebytes;
12768                       record_buf_mem[index_m++] = address;
12769                       address = address + f_ebytes;
12770                       thumb2_insn_r->mem_rec_count += 1;
12771                     }
12772                 }
12773             }
12774           /* Handle VST2.  */
12775           else if (b_bits == 0x03 || (b_bits & 0x0e) == 0x08)
12776             {
12777               if (b_bits == 0x09 || b_bits == 0x08)
12778                 bf_regs = 1;
12779               else if (b_bits == 0x03)
12780                 bf_regs = 2;
12781               else
12782                 bf_regs = 0;
12783
12784               for (index_r = 0; index_r < bf_regs; index_r++)
12785                 for (index_e = 0; index_e < f_elem; index_e++)
12786                   {
12787                     for (loop_t = 0; loop_t < 2; loop_t++)
12788                       {
12789                         record_buf_mem[index_m++] = f_ebytes;
12790                         record_buf_mem[index_m++] = address + (loop_t * f_ebytes);
12791                         thumb2_insn_r->mem_rec_count += 1;
12792                       }
12793                     address = address + (2 * f_ebytes);
12794                   }
12795             }
12796           /* Handle VST3.  */
12797           else if ((b_bits & 0x0e) == 0x04)
12798             {
12799               for (index_e = 0; index_e < f_elem; index_e++)
12800                 {
12801                   for (loop_t = 0; loop_t < 3; loop_t++)
12802                     {
12803                       record_buf_mem[index_m++] = f_ebytes;
12804                       record_buf_mem[index_m++] = address + (loop_t * f_ebytes);
12805                       thumb2_insn_r->mem_rec_count += 1;
12806                     }
12807                   address = address + (3 * f_ebytes);
12808                 }
12809             }
12810           /* Handle VST4.  */
12811           else if (!(b_bits & 0x0e))
12812             {
12813               for (index_e = 0; index_e < f_elem; index_e++)
12814                 {
12815                   for (loop_t = 0; loop_t < 4; loop_t++)
12816                     {
12817                       record_buf_mem[index_m++] = f_ebytes;
12818                       record_buf_mem[index_m++] = address + (loop_t * f_ebytes);
12819                       thumb2_insn_r->mem_rec_count += 1;
12820                     }
12821                   address = address + (4 * f_ebytes);
12822                 }
12823             }
12824         }
12825       else
12826         {
12827           uint8_t bft_size = bits (thumb2_insn_r->arm_insn, 10, 11);
12828
12829           if (bft_size == 0x00)
12830             f_ebytes = 1;
12831           else if (bft_size == 0x01)
12832             f_ebytes = 2;
12833           else if (bft_size == 0x02)
12834             f_ebytes = 4;
12835           else
12836             f_ebytes = 0;
12837
12838           /* Handle VST1.  */
12839           if (!(b_bits & 0x0b) || b_bits == 0x08)
12840             thumb2_insn_r->mem_rec_count = 1;
12841           /* Handle VST2.  */
12842           else if ((b_bits & 0x0b) == 0x01 || b_bits == 0x09)
12843             thumb2_insn_r->mem_rec_count = 2;
12844           /* Handle VST3.  */
12845           else if ((b_bits & 0x0b) == 0x02 || b_bits == 0x0a)
12846             thumb2_insn_r->mem_rec_count = 3;
12847           /* Handle VST4.  */
12848           else if ((b_bits & 0x0b) == 0x03 || b_bits == 0x0b)
12849             thumb2_insn_r->mem_rec_count = 4;
12850
12851           for (index_m = 0; index_m < thumb2_insn_r->mem_rec_count; index_m++)
12852             {
12853               record_buf_mem[index_m] = f_ebytes;
12854               record_buf_mem[index_m] = address + (index_m * f_ebytes);
12855             }
12856         }
12857     }
12858   else
12859     {
12860       if (!a_bit)
12861         {
12862           /* Handle VLD1.  */
12863           if (b_bits == 0x02 || b_bits == 0x0a || (b_bits & 0x0e) == 0x06)
12864             thumb2_insn_r->reg_rec_count = 1;
12865           /* Handle VLD2.  */
12866           else if (b_bits == 0x03 || (b_bits & 0x0e) == 0x08)
12867             thumb2_insn_r->reg_rec_count = 2;
12868           /* Handle VLD3.  */
12869           else if ((b_bits & 0x0e) == 0x04)
12870             thumb2_insn_r->reg_rec_count = 3;
12871           /* Handle VLD4.  */
12872           else if (!(b_bits & 0x0e))
12873             thumb2_insn_r->reg_rec_count = 4;
12874         }
12875       else
12876         {
12877           /* Handle VLD1.  */
12878           if (!(b_bits & 0x0b) || b_bits == 0x08 || b_bits == 0x0c)
12879             thumb2_insn_r->reg_rec_count = 1;
12880           /* Handle VLD2.  */
12881           else if ((b_bits & 0x0b) == 0x01 || b_bits == 0x09 || b_bits == 0x0d)
12882             thumb2_insn_r->reg_rec_count = 2;
12883           /* Handle VLD3.  */
12884           else if ((b_bits & 0x0b) == 0x02 || b_bits == 0x0a || b_bits == 0x0e)
12885             thumb2_insn_r->reg_rec_count = 3;
12886           /* Handle VLD4.  */
12887           else if ((b_bits & 0x0b) == 0x03 || b_bits == 0x0b || b_bits == 0x0f)
12888             thumb2_insn_r->reg_rec_count = 4;
12889
12890           for (index_r = 0; index_r < thumb2_insn_r->reg_rec_count; index_r++)
12891             record_buf[index_r] = reg_vd + ARM_D0_REGNUM + index_r;
12892         }
12893     }
12894
12895   if (bits (thumb2_insn_r->arm_insn, 0, 3) != 15)
12896     {
12897       record_buf[index_r] = reg_rn;
12898       thumb2_insn_r->reg_rec_count += 1;
12899     }
12900
12901   REG_ALLOC (thumb2_insn_r->arm_regs, thumb2_insn_r->reg_rec_count,
12902             record_buf);
12903   MEM_ALLOC (thumb2_insn_r->arm_mems, thumb2_insn_r->mem_rec_count,
12904             record_buf_mem);
12905   return 0;
12906 }
12907
12908 /* Decodes thumb2 instruction type and invokes its record handler.  */
12909
12910 static unsigned int
12911 thumb2_record_decode_insn_handler (insn_decode_record *thumb2_insn_r)
12912 {
12913   uint32_t op, op1, op2;
12914
12915   op = bit (thumb2_insn_r->arm_insn, 15);
12916   op1 = bits (thumb2_insn_r->arm_insn, 27, 28);
12917   op2 = bits (thumb2_insn_r->arm_insn, 20, 26);
12918
12919   if (op1 == 0x01)
12920     {
12921       if (!(op2 & 0x64 ))
12922         {
12923           /* Load/store multiple instruction.  */
12924           return thumb2_record_ld_st_multiple (thumb2_insn_r);
12925         }
12926       else if ((op2 & 0x64) == 0x4)
12927         {
12928           /* Load/store (dual/exclusive) and table branch instruction.  */
12929           return thumb2_record_ld_st_dual_ex_tbb (thumb2_insn_r);
12930         }
12931       else if ((op2 & 0x60) == 0x20)
12932         {
12933           /* Data-processing (shifted register).  */
12934           return thumb2_record_data_proc_sreg_mimm (thumb2_insn_r);
12935         }
12936       else if (op2 & 0x40)
12937         {
12938           /* Co-processor instructions.  */
12939           return thumb2_record_coproc_insn (thumb2_insn_r);
12940         }
12941     }
12942   else if (op1 == 0x02)
12943     {
12944       if (op)
12945         {
12946           /* Branches and miscellaneous control instructions.  */
12947           return thumb2_record_branch_misc_cntrl (thumb2_insn_r);
12948         }
12949       else if (op2 & 0x20)
12950         {
12951           /* Data-processing (plain binary immediate) instruction.  */
12952           return thumb2_record_ps_dest_generic (thumb2_insn_r);
12953         }
12954       else
12955         {
12956           /* Data-processing (modified immediate).  */
12957           return thumb2_record_data_proc_sreg_mimm (thumb2_insn_r);
12958         }
12959     }
12960   else if (op1 == 0x03)
12961     {
12962       if (!(op2 & 0x71 ))
12963         {
12964           /* Store single data item.  */
12965           return thumb2_record_str_single_data (thumb2_insn_r);
12966         }
12967       else if (!((op2 & 0x71) ^ 0x10))
12968         {
12969           /* Advanced SIMD or structure load/store instructions.  */
12970           return thumb2_record_asimd_struct_ld_st (thumb2_insn_r);
12971         }
12972       else if (!((op2 & 0x67) ^ 0x01))
12973         {
12974           /* Load byte, memory hints instruction.  */
12975           return thumb2_record_ld_mem_hints (thumb2_insn_r);
12976         }
12977       else if (!((op2 & 0x67) ^ 0x03))
12978         {
12979           /* Load halfword, memory hints instruction.  */
12980           return thumb2_record_ld_mem_hints (thumb2_insn_r);
12981         }
12982       else if (!((op2 & 0x67) ^ 0x05))
12983         {
12984           /* Load word instruction.  */
12985           return thumb2_record_ld_word (thumb2_insn_r);
12986         }
12987       else if (!((op2 & 0x70) ^ 0x20))
12988         {
12989           /* Data-processing (register) instruction.  */
12990           return thumb2_record_ps_dest_generic (thumb2_insn_r);
12991         }
12992       else if (!((op2 & 0x78) ^ 0x30))
12993         {
12994           /* Multiply, multiply accumulate, abs diff instruction.  */
12995           return thumb2_record_ps_dest_generic (thumb2_insn_r);
12996         }
12997       else if (!((op2 & 0x78) ^ 0x38))
12998         {
12999           /* Long multiply, long multiply accumulate, and divide.  */
13000           return thumb2_record_lmul_lmla_div (thumb2_insn_r);
13001         }
13002       else if (op2 & 0x40)
13003         {
13004           /* Co-processor instructions.  */
13005           return thumb2_record_coproc_insn (thumb2_insn_r);
13006         }
13007    }
13008
13009   return -1;
13010 }
13011
13012 namespace {
13013 /* Abstract memory reader.  */
13014
13015 class abstract_memory_reader
13016 {
13017 public:
13018   /* Read LEN bytes of target memory at address MEMADDR, placing the
13019      results in GDB's memory at BUF.  Return true on success.  */
13020
13021   virtual bool read (CORE_ADDR memaddr, gdb_byte *buf, const size_t len) = 0;
13022 };
13023
13024 /* Instruction reader from real target.  */
13025
13026 class instruction_reader : public abstract_memory_reader
13027 {
13028  public:
13029   bool read (CORE_ADDR memaddr, gdb_byte *buf, const size_t len) override
13030   {
13031     if (target_read_memory (memaddr, buf, len))
13032       return false;
13033     else
13034       return true;
13035   }
13036 };
13037
13038 } // namespace
13039
13040 /* Extracts arm/thumb/thumb2 insn depending on the size, and returns 0 on success 
13041 and positive val on fauilure.  */
13042
13043 static int
13044 extract_arm_insn (abstract_memory_reader& reader,
13045                   insn_decode_record *insn_record, uint32_t insn_size)
13046 {
13047   gdb_byte buf[insn_size];
13048
13049   memset (&buf[0], 0, insn_size);
13050   
13051   if (!reader.read (insn_record->this_addr, buf, insn_size))
13052     return 1;
13053   insn_record->arm_insn = (uint32_t) extract_unsigned_integer (&buf[0],
13054                            insn_size, 
13055                            gdbarch_byte_order_for_code (insn_record->gdbarch));
13056   return 0;
13057 }
13058
13059 typedef int (*sti_arm_hdl_fp_t) (insn_decode_record*);
13060
13061 /* Decode arm/thumb insn depending on condition cods and opcodes; and
13062    dispatch it.  */
13063
13064 static int
13065 decode_insn (abstract_memory_reader &reader, insn_decode_record *arm_record,
13066              record_type_t record_type, uint32_t insn_size)
13067 {
13068
13069   /* (Starting from numerical 0); bits 25, 26, 27 decodes type of arm
13070      instruction.  */
13071   static const sti_arm_hdl_fp_t arm_handle_insn[8] =
13072   {
13073     arm_record_data_proc_misc_ld_str,   /* 000.  */
13074     arm_record_data_proc_imm,           /* 001.  */
13075     arm_record_ld_st_imm_offset,        /* 010.  */
13076     arm_record_ld_st_reg_offset,        /* 011.  */
13077     arm_record_ld_st_multiple,          /* 100.  */
13078     arm_record_b_bl,                    /* 101.  */
13079     arm_record_asimd_vfp_coproc,        /* 110.  */
13080     arm_record_coproc_data_proc         /* 111.  */
13081   };
13082
13083   /* (Starting from numerical 0); bits 13,14,15 decodes type of thumb
13084      instruction.  */
13085   static const sti_arm_hdl_fp_t thumb_handle_insn[8] =
13086   { \
13087     thumb_record_shift_add_sub,        /* 000.  */
13088     thumb_record_add_sub_cmp_mov,      /* 001.  */
13089     thumb_record_ld_st_reg_offset,     /* 010.  */
13090     thumb_record_ld_st_imm_offset,     /* 011.  */
13091     thumb_record_ld_st_stack,          /* 100.  */
13092     thumb_record_misc,                 /* 101.  */
13093     thumb_record_ldm_stm_swi,          /* 110.  */
13094     thumb_record_branch                /* 111.  */
13095   };
13096
13097   uint32_t ret = 0;    /* return value: negative:failure   0:success.  */
13098   uint32_t insn_id = 0;
13099
13100   if (extract_arm_insn (reader, arm_record, insn_size))
13101     {
13102       if (record_debug)
13103         {
13104           printf_unfiltered (_("Process record: error reading memory at "
13105                                "addr %s len = %d.\n"),
13106                              paddress (arm_record->gdbarch,
13107                                        arm_record->this_addr), insn_size);
13108         }
13109       return -1;
13110     }
13111   else if (ARM_RECORD == record_type)
13112     {
13113       arm_record->cond = bits (arm_record->arm_insn, 28, 31);
13114       insn_id = bits (arm_record->arm_insn, 25, 27);
13115
13116       if (arm_record->cond == 0xf)
13117         ret = arm_record_extension_space (arm_record);
13118       else
13119         {
13120           /* If this insn has fallen into extension space
13121              then we need not decode it anymore.  */
13122           ret = arm_handle_insn[insn_id] (arm_record);
13123         }
13124       if (ret != ARM_RECORD_SUCCESS)
13125         {
13126           arm_record_unsupported_insn (arm_record);
13127           ret = -1;
13128         }
13129     }
13130   else if (THUMB_RECORD == record_type)
13131     {
13132       /* As thumb does not have condition codes, we set negative.  */
13133       arm_record->cond = -1;
13134       insn_id = bits (arm_record->arm_insn, 13, 15);
13135       ret = thumb_handle_insn[insn_id] (arm_record);
13136       if (ret != ARM_RECORD_SUCCESS)
13137         {
13138           arm_record_unsupported_insn (arm_record);
13139           ret = -1;
13140         }
13141     }
13142   else if (THUMB2_RECORD == record_type)
13143     {
13144       /* As thumb does not have condition codes, we set negative.  */
13145       arm_record->cond = -1;
13146
13147       /* Swap first half of 32bit thumb instruction with second half.  */
13148       arm_record->arm_insn
13149         = (arm_record->arm_insn >> 16) | (arm_record->arm_insn << 16);
13150
13151       ret = thumb2_record_decode_insn_handler (arm_record);
13152
13153       if (ret != ARM_RECORD_SUCCESS)
13154         {
13155           arm_record_unsupported_insn (arm_record);
13156           ret = -1;
13157         }
13158     }
13159   else
13160     {
13161       /* Throw assertion.  */
13162       gdb_assert_not_reached ("not a valid instruction, could not decode");
13163     }
13164
13165   return ret;
13166 }
13167
13168 #if GDB_SELF_TEST
13169 namespace selftests {
13170
13171 /* Provide both 16-bit and 32-bit thumb instructions.  */
13172
13173 class instruction_reader_thumb : public abstract_memory_reader
13174 {
13175 public:
13176   template<size_t SIZE>
13177   instruction_reader_thumb (enum bfd_endian endian,
13178                             const uint16_t (&insns)[SIZE])
13179     : m_endian (endian), m_insns (insns), m_insns_size (SIZE)
13180   {}
13181
13182   bool read (CORE_ADDR memaddr, gdb_byte *buf, const size_t len) override
13183   {
13184     SELF_CHECK (len == 4 || len == 2);
13185     SELF_CHECK (memaddr % 2 == 0);
13186     SELF_CHECK ((memaddr / 2) < m_insns_size);
13187
13188     store_unsigned_integer (buf, 2, m_endian, m_insns[memaddr / 2]);
13189     if (len == 4)
13190       {
13191         store_unsigned_integer (&buf[2], 2, m_endian,
13192                                 m_insns[memaddr / 2 + 1]);
13193       }
13194     return true;
13195   }
13196
13197 private:
13198   enum bfd_endian m_endian;
13199   const uint16_t *m_insns;
13200   size_t m_insns_size;
13201 };
13202
13203 static void
13204 arm_record_test (void)
13205 {
13206   struct gdbarch_info info;
13207   gdbarch_info_init (&info);
13208   info.bfd_arch_info = bfd_scan_arch ("arm");
13209
13210   struct gdbarch *gdbarch = gdbarch_find_by_info (info);
13211
13212   SELF_CHECK (gdbarch != NULL);
13213
13214   /* 16-bit Thumb instructions.  */
13215   {
13216     insn_decode_record arm_record;
13217
13218     memset (&arm_record, 0, sizeof (insn_decode_record));
13219     arm_record.gdbarch = gdbarch;
13220
13221     static const uint16_t insns[] = {
13222       /* db b2  uxtb    r3, r3 */
13223       0xb2db,
13224       /* cd 58  ldr     r5, [r1, r3] */
13225       0x58cd,
13226     };
13227
13228     enum bfd_endian endian = gdbarch_byte_order_for_code (arm_record.gdbarch);
13229     instruction_reader_thumb reader (endian, insns);
13230     int ret = decode_insn (reader, &arm_record, THUMB_RECORD,
13231                            THUMB_INSN_SIZE_BYTES);
13232
13233     SELF_CHECK (ret == 0);
13234     SELF_CHECK (arm_record.mem_rec_count == 0);
13235     SELF_CHECK (arm_record.reg_rec_count == 1);
13236     SELF_CHECK (arm_record.arm_regs[0] == 3);
13237
13238     arm_record.this_addr += 2;
13239     ret = decode_insn (reader, &arm_record, THUMB_RECORD,
13240                        THUMB_INSN_SIZE_BYTES);
13241
13242     SELF_CHECK (ret == 0);
13243     SELF_CHECK (arm_record.mem_rec_count == 0);
13244     SELF_CHECK (arm_record.reg_rec_count == 1);
13245     SELF_CHECK (arm_record.arm_regs[0] == 5);
13246   }
13247
13248   /* 32-bit Thumb-2 instructions.  */
13249   {
13250     insn_decode_record arm_record;
13251
13252     memset (&arm_record, 0, sizeof (insn_decode_record));
13253     arm_record.gdbarch = gdbarch;
13254
13255     static const uint16_t insns[] = {
13256       /* 1d ee 70 7f     mrc    15, 0, r7, cr13, cr0, {3} */
13257       0xee1d, 0x7f70,
13258     };
13259
13260     enum bfd_endian endian = gdbarch_byte_order_for_code (arm_record.gdbarch);
13261     instruction_reader_thumb reader (endian, insns);
13262     int ret = decode_insn (reader, &arm_record, THUMB2_RECORD,
13263                            THUMB2_INSN_SIZE_BYTES);
13264
13265     SELF_CHECK (ret == 0);
13266     SELF_CHECK (arm_record.mem_rec_count == 0);
13267     SELF_CHECK (arm_record.reg_rec_count == 1);
13268     SELF_CHECK (arm_record.arm_regs[0] == 7);
13269   }
13270 }
13271 } // namespace selftests
13272 #endif /* GDB_SELF_TEST */
13273
13274 /* Cleans up local record registers and memory allocations.  */
13275
13276 static void 
13277 deallocate_reg_mem (insn_decode_record *record)
13278 {
13279   xfree (record->arm_regs);
13280   xfree (record->arm_mems);    
13281 }
13282
13283
13284 /* Parse the current instruction and record the values of the registers and
13285    memory that will be changed in current instruction to record_arch_list".
13286    Return -1 if something is wrong.  */
13287
13288 int
13289 arm_process_record (struct gdbarch *gdbarch, struct regcache *regcache,
13290                     CORE_ADDR insn_addr)
13291 {
13292
13293   uint32_t no_of_rec = 0;
13294   uint32_t ret = 0;  /* return value: -1:record failure ;  0:success  */
13295   ULONGEST t_bit = 0, insn_id = 0;
13296
13297   ULONGEST u_regval = 0;
13298
13299   insn_decode_record arm_record;
13300
13301   memset (&arm_record, 0, sizeof (insn_decode_record));
13302   arm_record.regcache = regcache;
13303   arm_record.this_addr = insn_addr;
13304   arm_record.gdbarch = gdbarch;
13305
13306
13307   if (record_debug > 1)
13308     {
13309       fprintf_unfiltered (gdb_stdlog, "Process record: arm_process_record "
13310                           "addr = %s\n",
13311       paddress (gdbarch, arm_record.this_addr));
13312     }
13313
13314   instruction_reader reader;
13315   if (extract_arm_insn (reader, &arm_record, 2))
13316     {
13317       if (record_debug)
13318         {
13319           printf_unfiltered (_("Process record: error reading memory at "
13320                                "addr %s len = %d.\n"),
13321                              paddress (arm_record.gdbarch,
13322                                        arm_record.this_addr), 2);
13323         }
13324       return -1;
13325     }
13326
13327   /* Check the insn, whether it is thumb or arm one.  */
13328
13329   t_bit = arm_psr_thumb_bit (arm_record.gdbarch);
13330   regcache_raw_read_unsigned (arm_record.regcache, ARM_PS_REGNUM, &u_regval);
13331
13332
13333   if (!(u_regval & t_bit))
13334     {
13335       /* We are decoding arm insn.  */
13336       ret = decode_insn (reader, &arm_record, ARM_RECORD, ARM_INSN_SIZE_BYTES);
13337     }
13338   else
13339     {
13340       insn_id = bits (arm_record.arm_insn, 11, 15);
13341       /* is it thumb2 insn?  */
13342       if ((0x1D == insn_id) || (0x1E == insn_id) || (0x1F == insn_id))
13343         {
13344           ret = decode_insn (reader, &arm_record, THUMB2_RECORD,
13345                              THUMB2_INSN_SIZE_BYTES);
13346         }
13347       else
13348         {
13349           /* We are decoding thumb insn.  */
13350           ret = decode_insn (reader, &arm_record, THUMB_RECORD,
13351                              THUMB_INSN_SIZE_BYTES);
13352         }
13353     }
13354
13355   if (0 == ret)
13356     {
13357       /* Record registers.  */
13358       record_full_arch_list_add_reg (arm_record.regcache, ARM_PC_REGNUM);
13359       if (arm_record.arm_regs)
13360         {
13361           for (no_of_rec = 0; no_of_rec < arm_record.reg_rec_count; no_of_rec++)
13362             {
13363               if (record_full_arch_list_add_reg
13364                   (arm_record.regcache , arm_record.arm_regs[no_of_rec]))
13365                 ret = -1;
13366             }
13367         }
13368       /* Record memories.  */
13369       if (arm_record.arm_mems)
13370         {
13371           for (no_of_rec = 0; no_of_rec < arm_record.mem_rec_count; no_of_rec++)
13372             {
13373               if (record_full_arch_list_add_mem
13374                   ((CORE_ADDR)arm_record.arm_mems[no_of_rec].addr,
13375                    arm_record.arm_mems[no_of_rec].len))
13376                 ret = -1;
13377             }
13378         }
13379
13380       if (record_full_arch_list_add_end ())
13381         ret = -1;
13382     }
13383
13384
13385   deallocate_reg_mem (&arm_record);
13386
13387   return ret;
13388 }