Remove global variable arm_linux_has_wmmx_registers in arm-linux-nat.c
[external/binutils.git] / gdb / arm-tdep.c
1 /* Common target dependent code for GDB on ARM systems.
2
3    Copyright (C) 1988-2015 Free Software Foundation, Inc.
4
5    This file is part of GDB.
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
19
20 #include "defs.h"
21
22 #include <ctype.h>              /* XXX for isupper ().  */
23
24 #include "frame.h"
25 #include "inferior.h"
26 #include "infrun.h"
27 #include "gdbcmd.h"
28 #include "gdbcore.h"
29 #include "dis-asm.h"            /* For register styles.  */
30 #include "regcache.h"
31 #include "reggroups.h"
32 #include "doublest.h"
33 #include "value.h"
34 #include "arch-utils.h"
35 #include "osabi.h"
36 #include "frame-unwind.h"
37 #include "frame-base.h"
38 #include "trad-frame.h"
39 #include "objfiles.h"
40 #include "dwarf2-frame.h"
41 #include "gdbtypes.h"
42 #include "prologue-value.h"
43 #include "remote.h"
44 #include "target-descriptions.h"
45 #include "user-regs.h"
46 #include "observer.h"
47
48 #include "arm-tdep.h"
49 #include "gdb/sim-arm.h"
50
51 #include "elf-bfd.h"
52 #include "coff/internal.h"
53 #include "elf/arm.h"
54
55 #include "vec.h"
56
57 #include "record.h"
58 #include "record-full.h"
59
60 #include "features/arm-with-m.c"
61 #include "features/arm-with-m-fpa-layout.c"
62 #include "features/arm-with-m-vfp-d16.c"
63 #include "features/arm-with-iwmmxt.c"
64 #include "features/arm-with-vfpv2.c"
65 #include "features/arm-with-vfpv3.c"
66 #include "features/arm-with-neon.c"
67
68 static int arm_debug;
69
70 /* Macros for setting and testing a bit in a minimal symbol that marks
71    it as Thumb function.  The MSB of the minimal symbol's "info" field
72    is used for this purpose.
73
74    MSYMBOL_SET_SPECIAL  Actually sets the "special" bit.
75    MSYMBOL_IS_SPECIAL   Tests the "special" bit in a minimal symbol.  */
76
77 #define MSYMBOL_SET_SPECIAL(msym)                               \
78         MSYMBOL_TARGET_FLAG_1 (msym) = 1
79
80 #define MSYMBOL_IS_SPECIAL(msym)                                \
81         MSYMBOL_TARGET_FLAG_1 (msym)
82
83 /* Per-objfile data used for mapping symbols.  */
84 static const struct objfile_data *arm_objfile_data_key;
85
86 struct arm_mapping_symbol
87 {
88   bfd_vma value;
89   char type;
90 };
91 typedef struct arm_mapping_symbol arm_mapping_symbol_s;
92 DEF_VEC_O(arm_mapping_symbol_s);
93
94 struct arm_per_objfile
95 {
96   VEC(arm_mapping_symbol_s) **section_maps;
97 };
98
99 /* The list of available "set arm ..." and "show arm ..." commands.  */
100 static struct cmd_list_element *setarmcmdlist = NULL;
101 static struct cmd_list_element *showarmcmdlist = NULL;
102
103 /* The type of floating-point to use.  Keep this in sync with enum
104    arm_float_model, and the help string in _initialize_arm_tdep.  */
105 static const char *const fp_model_strings[] =
106 {
107   "auto",
108   "softfpa",
109   "fpa",
110   "softvfp",
111   "vfp",
112   NULL
113 };
114
115 /* A variable that can be configured by the user.  */
116 static enum arm_float_model arm_fp_model = ARM_FLOAT_AUTO;
117 static const char *current_fp_model = "auto";
118
119 /* The ABI to use.  Keep this in sync with arm_abi_kind.  */
120 static const char *const arm_abi_strings[] =
121 {
122   "auto",
123   "APCS",
124   "AAPCS",
125   NULL
126 };
127
128 /* A variable that can be configured by the user.  */
129 static enum arm_abi_kind arm_abi_global = ARM_ABI_AUTO;
130 static const char *arm_abi_string = "auto";
131
132 /* The execution mode to assume.  */
133 static const char *const arm_mode_strings[] =
134   {
135     "auto",
136     "arm",
137     "thumb",
138     NULL
139   };
140
141 static const char *arm_fallback_mode_string = "auto";
142 static const char *arm_force_mode_string = "auto";
143
144 /* Internal override of the execution mode.  -1 means no override,
145    0 means override to ARM mode, 1 means override to Thumb mode.
146    The effect is the same as if arm_force_mode has been set by the
147    user (except the internal override has precedence over a user's
148    arm_force_mode override).  */
149 static int arm_override_mode = -1;
150
151 /* Number of different reg name sets (options).  */
152 static int num_disassembly_options;
153
154 /* The standard register names, and all the valid aliases for them.  Note
155    that `fp', `sp' and `pc' are not added in this alias list, because they
156    have been added as builtin user registers in
157    std-regs.c:_initialize_frame_reg.  */
158 static const struct
159 {
160   const char *name;
161   int regnum;
162 } arm_register_aliases[] = {
163   /* Basic register numbers.  */
164   { "r0", 0 },
165   { "r1", 1 },
166   { "r2", 2 },
167   { "r3", 3 },
168   { "r4", 4 },
169   { "r5", 5 },
170   { "r6", 6 },
171   { "r7", 7 },
172   { "r8", 8 },
173   { "r9", 9 },
174   { "r10", 10 },
175   { "r11", 11 },
176   { "r12", 12 },
177   { "r13", 13 },
178   { "r14", 14 },
179   { "r15", 15 },
180   /* Synonyms (argument and variable registers).  */
181   { "a1", 0 },
182   { "a2", 1 },
183   { "a3", 2 },
184   { "a4", 3 },
185   { "v1", 4 },
186   { "v2", 5 },
187   { "v3", 6 },
188   { "v4", 7 },
189   { "v5", 8 },
190   { "v6", 9 },
191   { "v7", 10 },
192   { "v8", 11 },
193   /* Other platform-specific names for r9.  */
194   { "sb", 9 },
195   { "tr", 9 },
196   /* Special names.  */
197   { "ip", 12 },
198   { "lr", 14 },
199   /* Names used by GCC (not listed in the ARM EABI).  */
200   { "sl", 10 },
201   /* A special name from the older ATPCS.  */
202   { "wr", 7 },
203 };
204
205 static const char *const arm_register_names[] =
206 {"r0",  "r1",  "r2",  "r3",     /*  0  1  2  3 */
207  "r4",  "r5",  "r6",  "r7",     /*  4  5  6  7 */
208  "r8",  "r9",  "r10", "r11",    /*  8  9 10 11 */
209  "r12", "sp",  "lr",  "pc",     /* 12 13 14 15 */
210  "f0",  "f1",  "f2",  "f3",     /* 16 17 18 19 */
211  "f4",  "f5",  "f6",  "f7",     /* 20 21 22 23 */
212  "fps", "cpsr" };               /* 24 25       */
213
214 /* Valid register name styles.  */
215 static const char **valid_disassembly_styles;
216
217 /* Disassembly style to use. Default to "std" register names.  */
218 static const char *disassembly_style;
219
220 /* This is used to keep the bfd arch_info in sync with the disassembly
221    style.  */
222 static void set_disassembly_style_sfunc(char *, int,
223                                          struct cmd_list_element *);
224 static void set_disassembly_style (void);
225
226 static void convert_from_extended (const struct floatformat *, const void *,
227                                    void *, int);
228 static void convert_to_extended (const struct floatformat *, void *,
229                                  const void *, int);
230
231 static enum register_status arm_neon_quad_read (struct gdbarch *gdbarch,
232                                                 struct regcache *regcache,
233                                                 int regnum, gdb_byte *buf);
234 static void arm_neon_quad_write (struct gdbarch *gdbarch,
235                                  struct regcache *regcache,
236                                  int regnum, const gdb_byte *buf);
237
238 static int thumb_insn_size (unsigned short inst1);
239
240 struct arm_prologue_cache
241 {
242   /* The stack pointer at the time this frame was created; i.e. the
243      caller's stack pointer when this function was called.  It is used
244      to identify this frame.  */
245   CORE_ADDR prev_sp;
246
247   /* The frame base for this frame is just prev_sp - frame size.
248      FRAMESIZE is the distance from the frame pointer to the
249      initial stack pointer.  */
250
251   int framesize;
252
253   /* The register used to hold the frame pointer for this frame.  */
254   int framereg;
255
256   /* Saved register offsets.  */
257   struct trad_frame_saved_reg *saved_regs;
258 };
259
260 static CORE_ADDR arm_analyze_prologue (struct gdbarch *gdbarch,
261                                        CORE_ADDR prologue_start,
262                                        CORE_ADDR prologue_end,
263                                        struct arm_prologue_cache *cache);
264
265 /* Architecture version for displaced stepping.  This effects the behaviour of
266    certain instructions, and really should not be hard-wired.  */
267
268 #define DISPLACED_STEPPING_ARCH_VERSION         5
269
270 /* Addresses for calling Thumb functions have the bit 0 set.
271    Here are some macros to test, set, or clear bit 0 of addresses.  */
272 #define IS_THUMB_ADDR(addr)     ((addr) & 1)
273 #define MAKE_THUMB_ADDR(addr)   ((addr) | 1)
274 #define UNMAKE_THUMB_ADDR(addr) ((addr) & ~1)
275
276 /* Set to true if the 32-bit mode is in use.  */
277
278 int arm_apcs_32 = 1;
279
280 /* Return the bit mask in ARM_PS_REGNUM that indicates Thumb mode.  */
281
282 int
283 arm_psr_thumb_bit (struct gdbarch *gdbarch)
284 {
285   if (gdbarch_tdep (gdbarch)->is_m)
286     return XPSR_T;
287   else
288     return CPSR_T;
289 }
290
291 /* Determine if FRAME is executing in Thumb mode.  */
292
293 int
294 arm_frame_is_thumb (struct frame_info *frame)
295 {
296   CORE_ADDR cpsr;
297   ULONGEST t_bit = arm_psr_thumb_bit (get_frame_arch (frame));
298
299   /* Every ARM frame unwinder can unwind the T bit of the CPSR, either
300      directly (from a signal frame or dummy frame) or by interpreting
301      the saved LR (from a prologue or DWARF frame).  So consult it and
302      trust the unwinders.  */
303   cpsr = get_frame_register_unsigned (frame, ARM_PS_REGNUM);
304
305   return (cpsr & t_bit) != 0;
306 }
307
308 /* Callback for VEC_lower_bound.  */
309
310 static inline int
311 arm_compare_mapping_symbols (const struct arm_mapping_symbol *lhs,
312                              const struct arm_mapping_symbol *rhs)
313 {
314   return lhs->value < rhs->value;
315 }
316
317 /* Search for the mapping symbol covering MEMADDR.  If one is found,
318    return its type.  Otherwise, return 0.  If START is non-NULL,
319    set *START to the location of the mapping symbol.  */
320
321 static char
322 arm_find_mapping_symbol (CORE_ADDR memaddr, CORE_ADDR *start)
323 {
324   struct obj_section *sec;
325
326   /* If there are mapping symbols, consult them.  */
327   sec = find_pc_section (memaddr);
328   if (sec != NULL)
329     {
330       struct arm_per_objfile *data;
331       VEC(arm_mapping_symbol_s) *map;
332       struct arm_mapping_symbol map_key = { memaddr - obj_section_addr (sec),
333                                             0 };
334       unsigned int idx;
335
336       data = objfile_data (sec->objfile, arm_objfile_data_key);
337       if (data != NULL)
338         {
339           map = data->section_maps[sec->the_bfd_section->index];
340           if (!VEC_empty (arm_mapping_symbol_s, map))
341             {
342               struct arm_mapping_symbol *map_sym;
343
344               idx = VEC_lower_bound (arm_mapping_symbol_s, map, &map_key,
345                                      arm_compare_mapping_symbols);
346
347               /* VEC_lower_bound finds the earliest ordered insertion
348                  point.  If the following symbol starts at this exact
349                  address, we use that; otherwise, the preceding
350                  mapping symbol covers this address.  */
351               if (idx < VEC_length (arm_mapping_symbol_s, map))
352                 {
353                   map_sym = VEC_index (arm_mapping_symbol_s, map, idx);
354                   if (map_sym->value == map_key.value)
355                     {
356                       if (start)
357                         *start = map_sym->value + obj_section_addr (sec);
358                       return map_sym->type;
359                     }
360                 }
361
362               if (idx > 0)
363                 {
364                   map_sym = VEC_index (arm_mapping_symbol_s, map, idx - 1);
365                   if (start)
366                     *start = map_sym->value + obj_section_addr (sec);
367                   return map_sym->type;
368                 }
369             }
370         }
371     }
372
373   return 0;
374 }
375
376 /* Determine if the program counter specified in MEMADDR is in a Thumb
377    function.  This function should be called for addresses unrelated to
378    any executing frame; otherwise, prefer arm_frame_is_thumb.  */
379
380 int
381 arm_pc_is_thumb (struct gdbarch *gdbarch, CORE_ADDR memaddr)
382 {
383   struct bound_minimal_symbol sym;
384   char type;
385   struct displaced_step_closure* dsc
386     = get_displaced_step_closure_by_addr(memaddr);
387
388   /* If checking the mode of displaced instruction in copy area, the mode
389      should be determined by instruction on the original address.  */
390   if (dsc)
391     {
392       if (debug_displaced)
393         fprintf_unfiltered (gdb_stdlog,
394                             "displaced: check mode of %.8lx instead of %.8lx\n",
395                             (unsigned long) dsc->insn_addr,
396                             (unsigned long) memaddr);
397       memaddr = dsc->insn_addr;
398     }
399
400   /* If bit 0 of the address is set, assume this is a Thumb address.  */
401   if (IS_THUMB_ADDR (memaddr))
402     return 1;
403
404   /* Respect internal mode override if active.  */
405   if (arm_override_mode != -1)
406     return arm_override_mode;
407
408   /* If the user wants to override the symbol table, let him.  */
409   if (strcmp (arm_force_mode_string, "arm") == 0)
410     return 0;
411   if (strcmp (arm_force_mode_string, "thumb") == 0)
412     return 1;
413
414   /* ARM v6-M and v7-M are always in Thumb mode.  */
415   if (gdbarch_tdep (gdbarch)->is_m)
416     return 1;
417
418   /* If there are mapping symbols, consult them.  */
419   type = arm_find_mapping_symbol (memaddr, NULL);
420   if (type)
421     return type == 't';
422
423   /* Thumb functions have a "special" bit set in minimal symbols.  */
424   sym = lookup_minimal_symbol_by_pc (memaddr);
425   if (sym.minsym)
426     return (MSYMBOL_IS_SPECIAL (sym.minsym));
427
428   /* If the user wants to override the fallback mode, let them.  */
429   if (strcmp (arm_fallback_mode_string, "arm") == 0)
430     return 0;
431   if (strcmp (arm_fallback_mode_string, "thumb") == 0)
432     return 1;
433
434   /* If we couldn't find any symbol, but we're talking to a running
435      target, then trust the current value of $cpsr.  This lets
436      "display/i $pc" always show the correct mode (though if there is
437      a symbol table we will not reach here, so it still may not be
438      displayed in the mode it will be executed).  */
439   if (target_has_registers)
440     return arm_frame_is_thumb (get_current_frame ());
441
442   /* Otherwise we're out of luck; we assume ARM.  */
443   return 0;
444 }
445
446 /* Remove useless bits from addresses in a running program.  */
447 static CORE_ADDR
448 arm_addr_bits_remove (struct gdbarch *gdbarch, CORE_ADDR val)
449 {
450   /* On M-profile devices, do not strip the low bit from EXC_RETURN
451      (the magic exception return address).  */
452   if (gdbarch_tdep (gdbarch)->is_m
453       && (val & 0xfffffff0) == 0xfffffff0)
454     return val;
455
456   if (arm_apcs_32)
457     return UNMAKE_THUMB_ADDR (val);
458   else
459     return (val & 0x03fffffc);
460 }
461
462 /* Return 1 if PC is the start of a compiler helper function which
463    can be safely ignored during prologue skipping.  IS_THUMB is true
464    if the function is known to be a Thumb function due to the way it
465    is being called.  */
466 static int
467 skip_prologue_function (struct gdbarch *gdbarch, CORE_ADDR pc, int is_thumb)
468 {
469   enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
470   struct bound_minimal_symbol msym;
471
472   msym = lookup_minimal_symbol_by_pc (pc);
473   if (msym.minsym != NULL
474       && BMSYMBOL_VALUE_ADDRESS (msym) == pc
475       && MSYMBOL_LINKAGE_NAME (msym.minsym) != NULL)
476     {
477       const char *name = MSYMBOL_LINKAGE_NAME (msym.minsym);
478
479       /* The GNU linker's Thumb call stub to foo is named
480          __foo_from_thumb.  */
481       if (strstr (name, "_from_thumb") != NULL)
482         name += 2;
483
484       /* On soft-float targets, __truncdfsf2 is called to convert promoted
485          arguments to their argument types in non-prototyped
486          functions.  */
487       if (startswith (name, "__truncdfsf2"))
488         return 1;
489       if (startswith (name, "__aeabi_d2f"))
490         return 1;
491
492       /* Internal functions related to thread-local storage.  */
493       if (startswith (name, "__tls_get_addr"))
494         return 1;
495       if (startswith (name, "__aeabi_read_tp"))
496         return 1;
497     }
498   else
499     {
500       /* If we run against a stripped glibc, we may be unable to identify
501          special functions by name.  Check for one important case,
502          __aeabi_read_tp, by comparing the *code* against the default
503          implementation (this is hand-written ARM assembler in glibc).  */
504
505       if (!is_thumb
506           && read_memory_unsigned_integer (pc, 4, byte_order_for_code)
507              == 0xe3e00a0f /* mov r0, #0xffff0fff */
508           && read_memory_unsigned_integer (pc + 4, 4, byte_order_for_code)
509              == 0xe240f01f) /* sub pc, r0, #31 */
510         return 1;
511     }
512
513   return 0;
514 }
515
516 /* Support routines for instruction parsing.  */
517 #define submask(x) ((1L << ((x) + 1)) - 1)
518 #define bit(obj,st) (((obj) >> (st)) & 1)
519 #define bits(obj,st,fn) (((obj) >> (st)) & submask ((fn) - (st)))
520 #define sbits(obj,st,fn) \
521   ((long) (bits(obj,st,fn) | ((long) bit(obj,fn) * ~ submask (fn - st))))
522 #define BranchDest(addr,instr) \
523   ((CORE_ADDR) (((unsigned long) (addr)) + 8 + (sbits (instr, 0, 23) << 2)))
524
525 /* Extract the immediate from instruction movw/movt of encoding T.  INSN1 is
526    the first 16-bit of instruction, and INSN2 is the second 16-bit of
527    instruction.  */
528 #define EXTRACT_MOVW_MOVT_IMM_T(insn1, insn2) \
529   ((bits ((insn1), 0, 3) << 12)               \
530    | (bits ((insn1), 10, 10) << 11)           \
531    | (bits ((insn2), 12, 14) << 8)            \
532    | bits ((insn2), 0, 7))
533
534 /* Extract the immediate from instruction movw/movt of encoding A.  INSN is
535    the 32-bit instruction.  */
536 #define EXTRACT_MOVW_MOVT_IMM_A(insn) \
537   ((bits ((insn), 16, 19) << 12) \
538    | bits ((insn), 0, 11))
539
540 /* Decode immediate value; implements ThumbExpandImmediate pseudo-op.  */
541
542 static unsigned int
543 thumb_expand_immediate (unsigned int imm)
544 {
545   unsigned int count = imm >> 7;
546
547   if (count < 8)
548     switch (count / 2)
549       {
550       case 0:
551         return imm & 0xff;
552       case 1:
553         return (imm & 0xff) | ((imm & 0xff) << 16);
554       case 2:
555         return ((imm & 0xff) << 8) | ((imm & 0xff) << 24);
556       case 3:
557         return (imm & 0xff) | ((imm & 0xff) << 8)
558                 | ((imm & 0xff) << 16) | ((imm & 0xff) << 24);
559       }
560
561   return (0x80 | (imm & 0x7f)) << (32 - count);
562 }
563
564 /* Return 1 if the 16-bit Thumb instruction INST might change
565    control flow, 0 otherwise.  */
566
567 static int
568 thumb_instruction_changes_pc (unsigned short inst)
569 {
570   if ((inst & 0xff00) == 0xbd00)        /* pop {rlist, pc} */
571     return 1;
572
573   if ((inst & 0xf000) == 0xd000)        /* conditional branch */
574     return 1;
575
576   if ((inst & 0xf800) == 0xe000)        /* unconditional branch */
577     return 1;
578
579   if ((inst & 0xff00) == 0x4700)        /* bx REG, blx REG */
580     return 1;
581
582   if ((inst & 0xff87) == 0x4687)        /* mov pc, REG */
583     return 1;
584
585   if ((inst & 0xf500) == 0xb100)        /* CBNZ or CBZ.  */
586     return 1;
587
588   return 0;
589 }
590
591 /* Return 1 if the 32-bit Thumb instruction in INST1 and INST2
592    might change control flow, 0 otherwise.  */
593
594 static int
595 thumb2_instruction_changes_pc (unsigned short inst1, unsigned short inst2)
596 {
597   if ((inst1 & 0xf800) == 0xf000 && (inst2 & 0x8000) == 0x8000)
598     {
599       /* Branches and miscellaneous control instructions.  */
600
601       if ((inst2 & 0x1000) != 0 || (inst2 & 0xd001) == 0xc000)
602         {
603           /* B, BL, BLX.  */
604           return 1;
605         }
606       else if (inst1 == 0xf3de && (inst2 & 0xff00) == 0x3f00)
607         {
608           /* SUBS PC, LR, #imm8.  */
609           return 1;
610         }
611       else if ((inst2 & 0xd000) == 0x8000 && (inst1 & 0x0380) != 0x0380)
612         {
613           /* Conditional branch.  */
614           return 1;
615         }
616
617       return 0;
618     }
619
620   if ((inst1 & 0xfe50) == 0xe810)
621     {
622       /* Load multiple or RFE.  */
623
624       if (bit (inst1, 7) && !bit (inst1, 8))
625         {
626           /* LDMIA or POP */
627           if (bit (inst2, 15))
628             return 1;
629         }
630       else if (!bit (inst1, 7) && bit (inst1, 8))
631         {
632           /* LDMDB */
633           if (bit (inst2, 15))
634             return 1;
635         }
636       else if (bit (inst1, 7) && bit (inst1, 8))
637         {
638           /* RFEIA */
639           return 1;
640         }
641       else if (!bit (inst1, 7) && !bit (inst1, 8))
642         {
643           /* RFEDB */
644           return 1;
645         }
646
647       return 0;
648     }
649
650   if ((inst1 & 0xffef) == 0xea4f && (inst2 & 0xfff0) == 0x0f00)
651     {
652       /* MOV PC or MOVS PC.  */
653       return 1;
654     }
655
656   if ((inst1 & 0xff70) == 0xf850 && (inst2 & 0xf000) == 0xf000)
657     {
658       /* LDR PC.  */
659       if (bits (inst1, 0, 3) == 15)
660         return 1;
661       if (bit (inst1, 7))
662         return 1;
663       if (bit (inst2, 11))
664         return 1;
665       if ((inst2 & 0x0fc0) == 0x0000)
666         return 1;       
667
668       return 0;
669     }
670
671   if ((inst1 & 0xfff0) == 0xe8d0 && (inst2 & 0xfff0) == 0xf000)
672     {
673       /* TBB.  */
674       return 1;
675     }
676
677   if ((inst1 & 0xfff0) == 0xe8d0 && (inst2 & 0xfff0) == 0xf010)
678     {
679       /* TBH.  */
680       return 1;
681     }
682
683   return 0;
684 }
685
686 /* Return 1 if the 16-bit Thumb instruction INSN restores SP in
687    epilogue, 0 otherwise.  */
688
689 static int
690 thumb_instruction_restores_sp (unsigned short insn)
691 {
692   return (insn == 0x46bd  /* mov sp, r7 */
693           || (insn & 0xff80) == 0xb000  /* add sp, imm */
694           || (insn & 0xfe00) == 0xbc00);  /* pop <registers> */
695 }
696
697 /* Analyze a Thumb prologue, looking for a recognizable stack frame
698    and frame pointer.  Scan until we encounter a store that could
699    clobber the stack frame unexpectedly, or an unknown instruction.
700    Return the last address which is definitely safe to skip for an
701    initial breakpoint.  */
702
703 static CORE_ADDR
704 thumb_analyze_prologue (struct gdbarch *gdbarch,
705                         CORE_ADDR start, CORE_ADDR limit,
706                         struct arm_prologue_cache *cache)
707 {
708   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
709   enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
710   int i;
711   pv_t regs[16];
712   struct pv_area *stack;
713   struct cleanup *back_to;
714   CORE_ADDR offset;
715   CORE_ADDR unrecognized_pc = 0;
716
717   for (i = 0; i < 16; i++)
718     regs[i] = pv_register (i, 0);
719   stack = make_pv_area (ARM_SP_REGNUM, gdbarch_addr_bit (gdbarch));
720   back_to = make_cleanup_free_pv_area (stack);
721
722   while (start < limit)
723     {
724       unsigned short insn;
725
726       insn = read_memory_unsigned_integer (start, 2, byte_order_for_code);
727
728       if ((insn & 0xfe00) == 0xb400)            /* push { rlist } */
729         {
730           int regno;
731           int mask;
732
733           if (pv_area_store_would_trash (stack, regs[ARM_SP_REGNUM]))
734             break;
735
736           /* Bits 0-7 contain a mask for registers R0-R7.  Bit 8 says
737              whether to save LR (R14).  */
738           mask = (insn & 0xff) | ((insn & 0x100) << 6);
739
740           /* Calculate offsets of saved R0-R7 and LR.  */
741           for (regno = ARM_LR_REGNUM; regno >= 0; regno--)
742             if (mask & (1 << regno))
743               {
744                 regs[ARM_SP_REGNUM] = pv_add_constant (regs[ARM_SP_REGNUM],
745                                                        -4);
746                 pv_area_store (stack, regs[ARM_SP_REGNUM], 4, regs[regno]);
747               }
748         }
749       else if ((insn & 0xff80) == 0xb080)       /* sub sp, #imm */
750         {
751           offset = (insn & 0x7f) << 2;          /* get scaled offset */
752           regs[ARM_SP_REGNUM] = pv_add_constant (regs[ARM_SP_REGNUM],
753                                                  -offset);
754         }
755       else if (thumb_instruction_restores_sp (insn))
756         {
757           /* Don't scan past the epilogue.  */
758           break;
759         }
760       else if ((insn & 0xf800) == 0xa800)       /* add Rd, sp, #imm */
761         regs[bits (insn, 8, 10)] = pv_add_constant (regs[ARM_SP_REGNUM],
762                                                     (insn & 0xff) << 2);
763       else if ((insn & 0xfe00) == 0x1c00        /* add Rd, Rn, #imm */
764                && pv_is_register (regs[bits (insn, 3, 5)], ARM_SP_REGNUM))
765         regs[bits (insn, 0, 2)] = pv_add_constant (regs[bits (insn, 3, 5)],
766                                                    bits (insn, 6, 8));
767       else if ((insn & 0xf800) == 0x3000        /* add Rd, #imm */
768                && pv_is_register (regs[bits (insn, 8, 10)], ARM_SP_REGNUM))
769         regs[bits (insn, 8, 10)] = pv_add_constant (regs[bits (insn, 8, 10)],
770                                                     bits (insn, 0, 7));
771       else if ((insn & 0xfe00) == 0x1800        /* add Rd, Rn, Rm */
772                && pv_is_register (regs[bits (insn, 6, 8)], ARM_SP_REGNUM)
773                && pv_is_constant (regs[bits (insn, 3, 5)]))
774         regs[bits (insn, 0, 2)] = pv_add (regs[bits (insn, 3, 5)],
775                                           regs[bits (insn, 6, 8)]);
776       else if ((insn & 0xff00) == 0x4400        /* add Rd, Rm */
777                && pv_is_constant (regs[bits (insn, 3, 6)]))
778         {
779           int rd = (bit (insn, 7) << 3) + bits (insn, 0, 2);
780           int rm = bits (insn, 3, 6);
781           regs[rd] = pv_add (regs[rd], regs[rm]);
782         }
783       else if ((insn & 0xff00) == 0x4600)       /* mov hi, lo or mov lo, hi */
784         {
785           int dst_reg = (insn & 0x7) + ((insn & 0x80) >> 4);
786           int src_reg = (insn & 0x78) >> 3;
787           regs[dst_reg] = regs[src_reg];
788         }
789       else if ((insn & 0xf800) == 0x9000)       /* str rd, [sp, #off] */
790         {
791           /* Handle stores to the stack.  Normally pushes are used,
792              but with GCC -mtpcs-frame, there may be other stores
793              in the prologue to create the frame.  */
794           int regno = (insn >> 8) & 0x7;
795           pv_t addr;
796
797           offset = (insn & 0xff) << 2;
798           addr = pv_add_constant (regs[ARM_SP_REGNUM], offset);
799
800           if (pv_area_store_would_trash (stack, addr))
801             break;
802
803           pv_area_store (stack, addr, 4, regs[regno]);
804         }
805       else if ((insn & 0xf800) == 0x6000)       /* str rd, [rn, #off] */
806         {
807           int rd = bits (insn, 0, 2);
808           int rn = bits (insn, 3, 5);
809           pv_t addr;
810
811           offset = bits (insn, 6, 10) << 2;
812           addr = pv_add_constant (regs[rn], offset);
813
814           if (pv_area_store_would_trash (stack, addr))
815             break;
816
817           pv_area_store (stack, addr, 4, regs[rd]);
818         }
819       else if (((insn & 0xf800) == 0x7000       /* strb Rd, [Rn, #off] */
820                 || (insn & 0xf800) == 0x8000)   /* strh Rd, [Rn, #off] */
821                && pv_is_register (regs[bits (insn, 3, 5)], ARM_SP_REGNUM))
822         /* Ignore stores of argument registers to the stack.  */
823         ;
824       else if ((insn & 0xf800) == 0xc800        /* ldmia Rn!, { registers } */
825                && pv_is_register (regs[bits (insn, 8, 10)], ARM_SP_REGNUM))
826         /* Ignore block loads from the stack, potentially copying
827            parameters from memory.  */
828         ;
829       else if ((insn & 0xf800) == 0x9800        /* ldr Rd, [Rn, #immed] */
830                || ((insn & 0xf800) == 0x6800    /* ldr Rd, [sp, #immed] */
831                    && pv_is_register (regs[bits (insn, 3, 5)], ARM_SP_REGNUM)))
832         /* Similarly ignore single loads from the stack.  */
833         ;
834       else if ((insn & 0xffc0) == 0x0000        /* lsls Rd, Rm, #0 */
835                || (insn & 0xffc0) == 0x1c00)    /* add Rd, Rn, #0 */
836         /* Skip register copies, i.e. saves to another register
837            instead of the stack.  */
838         ;
839       else if ((insn & 0xf800) == 0x2000)       /* movs Rd, #imm */
840         /* Recognize constant loads; even with small stacks these are necessary
841            on Thumb.  */
842         regs[bits (insn, 8, 10)] = pv_constant (bits (insn, 0, 7));
843       else if ((insn & 0xf800) == 0x4800)       /* ldr Rd, [pc, #imm] */
844         {
845           /* Constant pool loads, for the same reason.  */
846           unsigned int constant;
847           CORE_ADDR loc;
848
849           loc = start + 4 + bits (insn, 0, 7) * 4;
850           constant = read_memory_unsigned_integer (loc, 4, byte_order);
851           regs[bits (insn, 8, 10)] = pv_constant (constant);
852         }
853       else if (thumb_insn_size (insn) == 4) /* 32-bit Thumb-2 instructions.  */
854         {
855           unsigned short inst2;
856
857           inst2 = read_memory_unsigned_integer (start + 2, 2,
858                                                 byte_order_for_code);
859
860           if ((insn & 0xf800) == 0xf000 && (inst2 & 0xe800) == 0xe800)
861             {
862               /* BL, BLX.  Allow some special function calls when
863                  skipping the prologue; GCC generates these before
864                  storing arguments to the stack.  */
865               CORE_ADDR nextpc;
866               int j1, j2, imm1, imm2;
867
868               imm1 = sbits (insn, 0, 10);
869               imm2 = bits (inst2, 0, 10);
870               j1 = bit (inst2, 13);
871               j2 = bit (inst2, 11);
872
873               offset = ((imm1 << 12) + (imm2 << 1));
874               offset ^= ((!j2) << 22) | ((!j1) << 23);
875
876               nextpc = start + 4 + offset;
877               /* For BLX make sure to clear the low bits.  */
878               if (bit (inst2, 12) == 0)
879                 nextpc = nextpc & 0xfffffffc;
880
881               if (!skip_prologue_function (gdbarch, nextpc,
882                                            bit (inst2, 12) != 0))
883                 break;
884             }
885
886           else if ((insn & 0xffd0) == 0xe900    /* stmdb Rn{!},
887                                                    { registers } */
888                    && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
889             {
890               pv_t addr = regs[bits (insn, 0, 3)];
891               int regno;
892
893               if (pv_area_store_would_trash (stack, addr))
894                 break;
895
896               /* Calculate offsets of saved registers.  */
897               for (regno = ARM_LR_REGNUM; regno >= 0; regno--)
898                 if (inst2 & (1 << regno))
899                   {
900                     addr = pv_add_constant (addr, -4);
901                     pv_area_store (stack, addr, 4, regs[regno]);
902                   }
903
904               if (insn & 0x0020)
905                 regs[bits (insn, 0, 3)] = addr;
906             }
907
908           else if ((insn & 0xff50) == 0xe940    /* strd Rt, Rt2,
909                                                    [Rn, #+/-imm]{!} */
910                    && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
911             {
912               int regno1 = bits (inst2, 12, 15);
913               int regno2 = bits (inst2, 8, 11);
914               pv_t addr = regs[bits (insn, 0, 3)];
915
916               offset = inst2 & 0xff;
917               if (insn & 0x0080)
918                 addr = pv_add_constant (addr, offset);
919               else
920                 addr = pv_add_constant (addr, -offset);
921
922               if (pv_area_store_would_trash (stack, addr))
923                 break;
924
925               pv_area_store (stack, addr, 4, regs[regno1]);
926               pv_area_store (stack, pv_add_constant (addr, 4),
927                              4, regs[regno2]);
928
929               if (insn & 0x0020)
930                 regs[bits (insn, 0, 3)] = addr;
931             }
932
933           else if ((insn & 0xfff0) == 0xf8c0    /* str Rt,[Rn,+/-#imm]{!} */
934                    && (inst2 & 0x0c00) == 0x0c00
935                    && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
936             {
937               int regno = bits (inst2, 12, 15);
938               pv_t addr = regs[bits (insn, 0, 3)];
939
940               offset = inst2 & 0xff;
941               if (inst2 & 0x0200)
942                 addr = pv_add_constant (addr, offset);
943               else
944                 addr = pv_add_constant (addr, -offset);
945
946               if (pv_area_store_would_trash (stack, addr))
947                 break;
948
949               pv_area_store (stack, addr, 4, regs[regno]);
950
951               if (inst2 & 0x0100)
952                 regs[bits (insn, 0, 3)] = addr;
953             }
954
955           else if ((insn & 0xfff0) == 0xf8c0    /* str.w Rt,[Rn,#imm] */
956                    && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
957             {
958               int regno = bits (inst2, 12, 15);
959               pv_t addr;
960
961               offset = inst2 & 0xfff;
962               addr = pv_add_constant (regs[bits (insn, 0, 3)], offset);
963
964               if (pv_area_store_would_trash (stack, addr))
965                 break;
966
967               pv_area_store (stack, addr, 4, regs[regno]);
968             }
969
970           else if ((insn & 0xffd0) == 0xf880    /* str{bh}.w Rt,[Rn,#imm] */
971                    && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
972             /* Ignore stores of argument registers to the stack.  */
973             ;
974
975           else if ((insn & 0xffd0) == 0xf800    /* str{bh} Rt,[Rn,#+/-imm] */
976                    && (inst2 & 0x0d00) == 0x0c00
977                    && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
978             /* Ignore stores of argument registers to the stack.  */
979             ;
980
981           else if ((insn & 0xffd0) == 0xe890    /* ldmia Rn[!],
982                                                    { registers } */
983                    && (inst2 & 0x8000) == 0x0000
984                    && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
985             /* Ignore block loads from the stack, potentially copying
986                parameters from memory.  */
987             ;
988
989           else if ((insn & 0xffb0) == 0xe950    /* ldrd Rt, Rt2,
990                                                    [Rn, #+/-imm] */
991                    && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
992             /* Similarly ignore dual loads from the stack.  */
993             ;
994
995           else if ((insn & 0xfff0) == 0xf850    /* ldr Rt,[Rn,#+/-imm] */
996                    && (inst2 & 0x0d00) == 0x0c00
997                    && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
998             /* Similarly ignore single loads from the stack.  */
999             ;
1000
1001           else if ((insn & 0xfff0) == 0xf8d0    /* ldr.w Rt,[Rn,#imm] */
1002                    && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
1003             /* Similarly ignore single loads from the stack.  */
1004             ;
1005
1006           else if ((insn & 0xfbf0) == 0xf100    /* add.w Rd, Rn, #imm */
1007                    && (inst2 & 0x8000) == 0x0000)
1008             {
1009               unsigned int imm = ((bits (insn, 10, 10) << 11)
1010                                   | (bits (inst2, 12, 14) << 8)
1011                                   | bits (inst2, 0, 7));
1012
1013               regs[bits (inst2, 8, 11)]
1014                 = pv_add_constant (regs[bits (insn, 0, 3)],
1015                                    thumb_expand_immediate (imm));
1016             }
1017
1018           else if ((insn & 0xfbf0) == 0xf200    /* addw Rd, Rn, #imm */
1019                    && (inst2 & 0x8000) == 0x0000)
1020             {
1021               unsigned int imm = ((bits (insn, 10, 10) << 11)
1022                                   | (bits (inst2, 12, 14) << 8)
1023                                   | bits (inst2, 0, 7));
1024
1025               regs[bits (inst2, 8, 11)]
1026                 = pv_add_constant (regs[bits (insn, 0, 3)], imm);
1027             }
1028
1029           else if ((insn & 0xfbf0) == 0xf1a0    /* sub.w Rd, Rn, #imm */
1030                    && (inst2 & 0x8000) == 0x0000)
1031             {
1032               unsigned int imm = ((bits (insn, 10, 10) << 11)
1033                                   | (bits (inst2, 12, 14) << 8)
1034                                   | bits (inst2, 0, 7));
1035
1036               regs[bits (inst2, 8, 11)]
1037                 = pv_add_constant (regs[bits (insn, 0, 3)],
1038                                    - (CORE_ADDR) thumb_expand_immediate (imm));
1039             }
1040
1041           else if ((insn & 0xfbf0) == 0xf2a0    /* subw Rd, Rn, #imm */
1042                    && (inst2 & 0x8000) == 0x0000)
1043             {
1044               unsigned int imm = ((bits (insn, 10, 10) << 11)
1045                                   | (bits (inst2, 12, 14) << 8)
1046                                   | bits (inst2, 0, 7));
1047
1048               regs[bits (inst2, 8, 11)]
1049                 = pv_add_constant (regs[bits (insn, 0, 3)], - (CORE_ADDR) imm);
1050             }
1051
1052           else if ((insn & 0xfbff) == 0xf04f)   /* mov.w Rd, #const */
1053             {
1054               unsigned int imm = ((bits (insn, 10, 10) << 11)
1055                                   | (bits (inst2, 12, 14) << 8)
1056                                   | bits (inst2, 0, 7));
1057
1058               regs[bits (inst2, 8, 11)]
1059                 = pv_constant (thumb_expand_immediate (imm));
1060             }
1061
1062           else if ((insn & 0xfbf0) == 0xf240)   /* movw Rd, #const */
1063             {
1064               unsigned int imm
1065                 = EXTRACT_MOVW_MOVT_IMM_T (insn, inst2);
1066
1067               regs[bits (inst2, 8, 11)] = pv_constant (imm);
1068             }
1069
1070           else if (insn == 0xea5f               /* mov.w Rd,Rm */
1071                    && (inst2 & 0xf0f0) == 0)
1072             {
1073               int dst_reg = (inst2 & 0x0f00) >> 8;
1074               int src_reg = inst2 & 0xf;
1075               regs[dst_reg] = regs[src_reg];
1076             }
1077
1078           else if ((insn & 0xff7f) == 0xf85f)   /* ldr.w Rt,<label> */
1079             {
1080               /* Constant pool loads.  */
1081               unsigned int constant;
1082               CORE_ADDR loc;
1083
1084               offset = bits (inst2, 0, 11);
1085               if (insn & 0x0080)
1086                 loc = start + 4 + offset;
1087               else
1088                 loc = start + 4 - offset;
1089
1090               constant = read_memory_unsigned_integer (loc, 4, byte_order);
1091               regs[bits (inst2, 12, 15)] = pv_constant (constant);
1092             }
1093
1094           else if ((insn & 0xff7f) == 0xe95f)   /* ldrd Rt,Rt2,<label> */
1095             {
1096               /* Constant pool loads.  */
1097               unsigned int constant;
1098               CORE_ADDR loc;
1099
1100               offset = bits (inst2, 0, 7) << 2;
1101               if (insn & 0x0080)
1102                 loc = start + 4 + offset;
1103               else
1104                 loc = start + 4 - offset;
1105
1106               constant = read_memory_unsigned_integer (loc, 4, byte_order);
1107               regs[bits (inst2, 12, 15)] = pv_constant (constant);
1108
1109               constant = read_memory_unsigned_integer (loc + 4, 4, byte_order);
1110               regs[bits (inst2, 8, 11)] = pv_constant (constant);
1111             }
1112
1113           else if (thumb2_instruction_changes_pc (insn, inst2))
1114             {
1115               /* Don't scan past anything that might change control flow.  */
1116               break;
1117             }
1118           else
1119             {
1120               /* The optimizer might shove anything into the prologue,
1121                  so we just skip what we don't recognize.  */
1122               unrecognized_pc = start;
1123             }
1124
1125           start += 2;
1126         }
1127       else if (thumb_instruction_changes_pc (insn))
1128         {
1129           /* Don't scan past anything that might change control flow.  */
1130           break;
1131         }
1132       else
1133         {
1134           /* The optimizer might shove anything into the prologue,
1135              so we just skip what we don't recognize.  */
1136           unrecognized_pc = start;
1137         }
1138
1139       start += 2;
1140     }
1141
1142   if (arm_debug)
1143     fprintf_unfiltered (gdb_stdlog, "Prologue scan stopped at %s\n",
1144                         paddress (gdbarch, start));
1145
1146   if (unrecognized_pc == 0)
1147     unrecognized_pc = start;
1148
1149   if (cache == NULL)
1150     {
1151       do_cleanups (back_to);
1152       return unrecognized_pc;
1153     }
1154
1155   if (pv_is_register (regs[ARM_FP_REGNUM], ARM_SP_REGNUM))
1156     {
1157       /* Frame pointer is fp.  Frame size is constant.  */
1158       cache->framereg = ARM_FP_REGNUM;
1159       cache->framesize = -regs[ARM_FP_REGNUM].k;
1160     }
1161   else if (pv_is_register (regs[THUMB_FP_REGNUM], ARM_SP_REGNUM))
1162     {
1163       /* Frame pointer is r7.  Frame size is constant.  */
1164       cache->framereg = THUMB_FP_REGNUM;
1165       cache->framesize = -regs[THUMB_FP_REGNUM].k;
1166     }
1167   else
1168     {
1169       /* Try the stack pointer... this is a bit desperate.  */
1170       cache->framereg = ARM_SP_REGNUM;
1171       cache->framesize = -regs[ARM_SP_REGNUM].k;
1172     }
1173
1174   for (i = 0; i < 16; i++)
1175     if (pv_area_find_reg (stack, gdbarch, i, &offset))
1176       cache->saved_regs[i].addr = offset;
1177
1178   do_cleanups (back_to);
1179   return unrecognized_pc;
1180 }
1181
1182
1183 /* Try to analyze the instructions starting from PC, which load symbol
1184    __stack_chk_guard.  Return the address of instruction after loading this
1185    symbol, set the dest register number to *BASEREG, and set the size of
1186    instructions for loading symbol in OFFSET.  Return 0 if instructions are
1187    not recognized.  */
1188
1189 static CORE_ADDR
1190 arm_analyze_load_stack_chk_guard(CORE_ADDR pc, struct gdbarch *gdbarch,
1191                                  unsigned int *destreg, int *offset)
1192 {
1193   enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
1194   int is_thumb = arm_pc_is_thumb (gdbarch, pc);
1195   unsigned int low, high, address;
1196
1197   address = 0;
1198   if (is_thumb)
1199     {
1200       unsigned short insn1
1201         = read_memory_unsigned_integer (pc, 2, byte_order_for_code);
1202
1203       if ((insn1 & 0xf800) == 0x4800) /* ldr Rd, #immed */
1204         {
1205           *destreg = bits (insn1, 8, 10);
1206           *offset = 2;
1207           address = (pc & 0xfffffffc) + 4 + (bits (insn1, 0, 7) << 2);
1208           address = read_memory_unsigned_integer (address, 4,
1209                                                   byte_order_for_code);
1210         }
1211       else if ((insn1 & 0xfbf0) == 0xf240) /* movw Rd, #const */
1212         {
1213           unsigned short insn2
1214             = read_memory_unsigned_integer (pc + 2, 2, byte_order_for_code);
1215
1216           low = EXTRACT_MOVW_MOVT_IMM_T (insn1, insn2);
1217
1218           insn1
1219             = read_memory_unsigned_integer (pc + 4, 2, byte_order_for_code);
1220           insn2
1221             = read_memory_unsigned_integer (pc + 6, 2, byte_order_for_code);
1222
1223           /* movt Rd, #const */
1224           if ((insn1 & 0xfbc0) == 0xf2c0)
1225             {
1226               high = EXTRACT_MOVW_MOVT_IMM_T (insn1, insn2);
1227               *destreg = bits (insn2, 8, 11);
1228               *offset = 8;
1229               address = (high << 16 | low);
1230             }
1231         }
1232     }
1233   else
1234     {
1235       unsigned int insn
1236         = read_memory_unsigned_integer (pc, 4, byte_order_for_code);
1237
1238       if ((insn & 0x0e5f0000) == 0x041f0000) /* ldr Rd, [PC, #immed] */
1239         {
1240           address = bits (insn, 0, 11) + pc + 8;
1241           address = read_memory_unsigned_integer (address, 4,
1242                                                   byte_order_for_code);
1243
1244           *destreg = bits (insn, 12, 15);
1245           *offset = 4;
1246         }
1247       else if ((insn & 0x0ff00000) == 0x03000000) /* movw Rd, #const */
1248         {
1249           low = EXTRACT_MOVW_MOVT_IMM_A (insn);
1250
1251           insn
1252             = read_memory_unsigned_integer (pc + 4, 4, byte_order_for_code);
1253
1254           if ((insn & 0x0ff00000) == 0x03400000) /* movt Rd, #const */
1255             {
1256               high = EXTRACT_MOVW_MOVT_IMM_A (insn);
1257               *destreg = bits (insn, 12, 15);
1258               *offset = 8;
1259               address = (high << 16 | low);
1260             }
1261         }
1262     }
1263
1264   return address;
1265 }
1266
1267 /* Try to skip a sequence of instructions used for stack protector.  If PC
1268    points to the first instruction of this sequence, return the address of
1269    first instruction after this sequence, otherwise, return original PC.
1270
1271    On arm, this sequence of instructions is composed of mainly three steps,
1272      Step 1: load symbol __stack_chk_guard,
1273      Step 2: load from address of __stack_chk_guard,
1274      Step 3: store it to somewhere else.
1275
1276    Usually, instructions on step 2 and step 3 are the same on various ARM
1277    architectures.  On step 2, it is one instruction 'ldr Rx, [Rn, #0]', and
1278    on step 3, it is also one instruction 'str Rx, [r7, #immd]'.  However,
1279    instructions in step 1 vary from different ARM architectures.  On ARMv7,
1280    they are,
1281
1282         movw    Rn, #:lower16:__stack_chk_guard
1283         movt    Rn, #:upper16:__stack_chk_guard
1284
1285    On ARMv5t, it is,
1286
1287         ldr     Rn, .Label
1288         ....
1289         .Lable:
1290         .word   __stack_chk_guard
1291
1292    Since ldr/str is a very popular instruction, we can't use them as
1293    'fingerprint' or 'signature' of stack protector sequence.  Here we choose
1294    sequence {movw/movt, ldr}/ldr/str plus symbol __stack_chk_guard, if not
1295    stripped, as the 'fingerprint' of a stack protector cdoe sequence.  */
1296
1297 static CORE_ADDR
1298 arm_skip_stack_protector(CORE_ADDR pc, struct gdbarch *gdbarch)
1299 {
1300   enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
1301   unsigned int basereg;
1302   struct bound_minimal_symbol stack_chk_guard;
1303   int offset;
1304   int is_thumb = arm_pc_is_thumb (gdbarch, pc);
1305   CORE_ADDR addr;
1306
1307   /* Try to parse the instructions in Step 1.  */
1308   addr = arm_analyze_load_stack_chk_guard (pc, gdbarch,
1309                                            &basereg, &offset);
1310   if (!addr)
1311     return pc;
1312
1313   stack_chk_guard = lookup_minimal_symbol_by_pc (addr);
1314   /* ADDR must correspond to a symbol whose name is __stack_chk_guard.
1315      Otherwise, this sequence cannot be for stack protector.  */
1316   if (stack_chk_guard.minsym == NULL
1317       || !startswith (MSYMBOL_LINKAGE_NAME (stack_chk_guard.minsym), "__stack_chk_guard"))
1318    return pc;
1319
1320   if (is_thumb)
1321     {
1322       unsigned int destreg;
1323       unsigned short insn
1324         = read_memory_unsigned_integer (pc + offset, 2, byte_order_for_code);
1325
1326       /* Step 2: ldr Rd, [Rn, #immed], encoding T1.  */
1327       if ((insn & 0xf800) != 0x6800)
1328         return pc;
1329       if (bits (insn, 3, 5) != basereg)
1330         return pc;
1331       destreg = bits (insn, 0, 2);
1332
1333       insn = read_memory_unsigned_integer (pc + offset + 2, 2,
1334                                            byte_order_for_code);
1335       /* Step 3: str Rd, [Rn, #immed], encoding T1.  */
1336       if ((insn & 0xf800) != 0x6000)
1337         return pc;
1338       if (destreg != bits (insn, 0, 2))
1339         return pc;
1340     }
1341   else
1342     {
1343       unsigned int destreg;
1344       unsigned int insn
1345         = read_memory_unsigned_integer (pc + offset, 4, byte_order_for_code);
1346
1347       /* Step 2: ldr Rd, [Rn, #immed], encoding A1.  */
1348       if ((insn & 0x0e500000) != 0x04100000)
1349         return pc;
1350       if (bits (insn, 16, 19) != basereg)
1351         return pc;
1352       destreg = bits (insn, 12, 15);
1353       /* Step 3: str Rd, [Rn, #immed], encoding A1.  */
1354       insn = read_memory_unsigned_integer (pc + offset + 4,
1355                                            4, byte_order_for_code);
1356       if ((insn & 0x0e500000) != 0x04000000)
1357         return pc;
1358       if (bits (insn, 12, 15) != destreg)
1359         return pc;
1360     }
1361   /* The size of total two instructions ldr/str is 4 on Thumb-2, while 8
1362      on arm.  */
1363   if (is_thumb)
1364     return pc + offset + 4;
1365   else
1366     return pc + offset + 8;
1367 }
1368
1369 /* Advance the PC across any function entry prologue instructions to
1370    reach some "real" code.
1371
1372    The APCS (ARM Procedure Call Standard) defines the following
1373    prologue:
1374
1375    mov          ip, sp
1376    [stmfd       sp!, {a1,a2,a3,a4}]
1377    stmfd        sp!, {...,fp,ip,lr,pc}
1378    [stfe        f7, [sp, #-12]!]
1379    [stfe        f6, [sp, #-12]!]
1380    [stfe        f5, [sp, #-12]!]
1381    [stfe        f4, [sp, #-12]!]
1382    sub fp, ip, #nn @@ nn == 20 or 4 depending on second insn.  */
1383
1384 static CORE_ADDR
1385 arm_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR pc)
1386 {
1387   enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
1388   unsigned long inst;
1389   CORE_ADDR func_addr, limit_pc;
1390
1391   /* See if we can determine the end of the prologue via the symbol table.
1392      If so, then return either PC, or the PC after the prologue, whichever
1393      is greater.  */
1394   if (find_pc_partial_function (pc, NULL, &func_addr, NULL))
1395     {
1396       CORE_ADDR post_prologue_pc
1397         = skip_prologue_using_sal (gdbarch, func_addr);
1398       struct compunit_symtab *cust = find_pc_compunit_symtab (func_addr);
1399
1400       if (post_prologue_pc)
1401         post_prologue_pc
1402           = arm_skip_stack_protector (post_prologue_pc, gdbarch);
1403
1404
1405       /* GCC always emits a line note before the prologue and another
1406          one after, even if the two are at the same address or on the
1407          same line.  Take advantage of this so that we do not need to
1408          know every instruction that might appear in the prologue.  We
1409          will have producer information for most binaries; if it is
1410          missing (e.g. for -gstabs), assuming the GNU tools.  */
1411       if (post_prologue_pc
1412           && (cust == NULL
1413               || COMPUNIT_PRODUCER (cust) == NULL
1414               || startswith (COMPUNIT_PRODUCER (cust), "GNU ")
1415               || startswith (COMPUNIT_PRODUCER (cust), "clang ")))
1416         return post_prologue_pc;
1417
1418       if (post_prologue_pc != 0)
1419         {
1420           CORE_ADDR analyzed_limit;
1421
1422           /* For non-GCC compilers, make sure the entire line is an
1423              acceptable prologue; GDB will round this function's
1424              return value up to the end of the following line so we
1425              can not skip just part of a line (and we do not want to).
1426
1427              RealView does not treat the prologue specially, but does
1428              associate prologue code with the opening brace; so this
1429              lets us skip the first line if we think it is the opening
1430              brace.  */
1431           if (arm_pc_is_thumb (gdbarch, func_addr))
1432             analyzed_limit = thumb_analyze_prologue (gdbarch, func_addr,
1433                                                      post_prologue_pc, NULL);
1434           else
1435             analyzed_limit = arm_analyze_prologue (gdbarch, func_addr,
1436                                                    post_prologue_pc, NULL);
1437
1438           if (analyzed_limit != post_prologue_pc)
1439             return func_addr;
1440
1441           return post_prologue_pc;
1442         }
1443     }
1444
1445   /* Can't determine prologue from the symbol table, need to examine
1446      instructions.  */
1447
1448   /* Find an upper limit on the function prologue using the debug
1449      information.  If the debug information could not be used to provide
1450      that bound, then use an arbitrary large number as the upper bound.  */
1451   /* Like arm_scan_prologue, stop no later than pc + 64.  */
1452   limit_pc = skip_prologue_using_sal (gdbarch, pc);
1453   if (limit_pc == 0)
1454     limit_pc = pc + 64;          /* Magic.  */
1455
1456
1457   /* Check if this is Thumb code.  */
1458   if (arm_pc_is_thumb (gdbarch, pc))
1459     return thumb_analyze_prologue (gdbarch, pc, limit_pc, NULL);
1460   else
1461     return arm_analyze_prologue (gdbarch, pc, limit_pc, NULL);
1462 }
1463
1464 /* *INDENT-OFF* */
1465 /* Function: thumb_scan_prologue (helper function for arm_scan_prologue)
1466    This function decodes a Thumb function prologue to determine:
1467      1) the size of the stack frame
1468      2) which registers are saved on it
1469      3) the offsets of saved regs
1470      4) the offset from the stack pointer to the frame pointer
1471
1472    A typical Thumb function prologue would create this stack frame
1473    (offsets relative to FP)
1474      old SP ->  24  stack parameters
1475                 20  LR
1476                 16  R7
1477      R7 ->       0  local variables (16 bytes)
1478      SP ->     -12  additional stack space (12 bytes)
1479    The frame size would thus be 36 bytes, and the frame offset would be
1480    12 bytes.  The frame register is R7.
1481    
1482    The comments for thumb_skip_prolog() describe the algorithm we use
1483    to detect the end of the prolog.  */
1484 /* *INDENT-ON* */
1485
1486 static void
1487 thumb_scan_prologue (struct gdbarch *gdbarch, CORE_ADDR prev_pc,
1488                      CORE_ADDR block_addr, struct arm_prologue_cache *cache)
1489 {
1490   CORE_ADDR prologue_start;
1491   CORE_ADDR prologue_end;
1492
1493   if (find_pc_partial_function (block_addr, NULL, &prologue_start,
1494                                 &prologue_end))
1495     {
1496       /* See comment in arm_scan_prologue for an explanation of
1497          this heuristics.  */
1498       if (prologue_end > prologue_start + 64)
1499         {
1500           prologue_end = prologue_start + 64;
1501         }
1502     }
1503   else
1504     /* We're in the boondocks: we have no idea where the start of the
1505        function is.  */
1506     return;
1507
1508   prologue_end = min (prologue_end, prev_pc);
1509
1510   thumb_analyze_prologue (gdbarch, prologue_start, prologue_end, cache);
1511 }
1512
1513 /* Return 1 if THIS_INSTR might change control flow, 0 otherwise.  */
1514
1515 static int
1516 arm_instruction_changes_pc (uint32_t this_instr)
1517 {
1518   if (bits (this_instr, 28, 31) == INST_NV)
1519     /* Unconditional instructions.  */
1520     switch (bits (this_instr, 24, 27))
1521       {
1522       case 0xa:
1523       case 0xb:
1524         /* Branch with Link and change to Thumb.  */
1525         return 1;
1526       case 0xc:
1527       case 0xd:
1528       case 0xe:
1529         /* Coprocessor register transfer.  */
1530         if (bits (this_instr, 12, 15) == 15)
1531           error (_("Invalid update to pc in instruction"));
1532         return 0;
1533       default:
1534         return 0;
1535       }
1536   else
1537     switch (bits (this_instr, 25, 27))
1538       {
1539       case 0x0:
1540         if (bits (this_instr, 23, 24) == 2 && bit (this_instr, 20) == 0)
1541           {
1542             /* Multiplies and extra load/stores.  */
1543             if (bit (this_instr, 4) == 1 && bit (this_instr, 7) == 1)
1544               /* Neither multiplies nor extension load/stores are allowed
1545                  to modify PC.  */
1546               return 0;
1547
1548             /* Otherwise, miscellaneous instructions.  */
1549
1550             /* BX <reg>, BXJ <reg>, BLX <reg> */
1551             if (bits (this_instr, 4, 27) == 0x12fff1
1552                 || bits (this_instr, 4, 27) == 0x12fff2
1553                 || bits (this_instr, 4, 27) == 0x12fff3)
1554               return 1;
1555
1556             /* Other miscellaneous instructions are unpredictable if they
1557                modify PC.  */
1558             return 0;
1559           }
1560         /* Data processing instruction.  Fall through.  */
1561
1562       case 0x1:
1563         if (bits (this_instr, 12, 15) == 15)
1564           return 1;
1565         else
1566           return 0;
1567
1568       case 0x2:
1569       case 0x3:
1570         /* Media instructions and architecturally undefined instructions.  */
1571         if (bits (this_instr, 25, 27) == 3 && bit (this_instr, 4) == 1)
1572           return 0;
1573
1574         /* Stores.  */
1575         if (bit (this_instr, 20) == 0)
1576           return 0;
1577
1578         /* Loads.  */
1579         if (bits (this_instr, 12, 15) == ARM_PC_REGNUM)
1580           return 1;
1581         else
1582           return 0;
1583
1584       case 0x4:
1585         /* Load/store multiple.  */
1586         if (bit (this_instr, 20) == 1 && bit (this_instr, 15) == 1)
1587           return 1;
1588         else
1589           return 0;
1590
1591       case 0x5:
1592         /* Branch and branch with link.  */
1593         return 1;
1594
1595       case 0x6:
1596       case 0x7:
1597         /* Coprocessor transfers or SWIs can not affect PC.  */
1598         return 0;
1599
1600       default:
1601         internal_error (__FILE__, __LINE__, _("bad value in switch"));
1602       }
1603 }
1604
1605 /* Return 1 if the ARM instruction INSN restores SP in epilogue, 0
1606    otherwise.  */
1607
1608 static int
1609 arm_instruction_restores_sp (unsigned int insn)
1610 {
1611   if (bits (insn, 28, 31) != INST_NV)
1612     {
1613       if ((insn & 0x0df0f000) == 0x0080d000
1614           /* ADD SP (register or immediate).  */
1615           || (insn & 0x0df0f000) == 0x0040d000
1616           /* SUB SP (register or immediate).  */
1617           || (insn & 0x0ffffff0) == 0x01a0d000
1618           /* MOV SP.  */
1619           || (insn & 0x0fff0000) == 0x08bd0000
1620           /* POP (LDMIA).  */
1621           || (insn & 0x0fff0000) == 0x049d0000)
1622           /* POP of a single register.  */
1623         return 1;
1624     }
1625
1626   return 0;
1627 }
1628
1629 /* Analyze an ARM mode prologue starting at PROLOGUE_START and
1630    continuing no further than PROLOGUE_END.  If CACHE is non-NULL,
1631    fill it in.  Return the first address not recognized as a prologue
1632    instruction.
1633
1634    We recognize all the instructions typically found in ARM prologues,
1635    plus harmless instructions which can be skipped (either for analysis
1636    purposes, or a more restrictive set that can be skipped when finding
1637    the end of the prologue).  */
1638
1639 static CORE_ADDR
1640 arm_analyze_prologue (struct gdbarch *gdbarch,
1641                       CORE_ADDR prologue_start, CORE_ADDR prologue_end,
1642                       struct arm_prologue_cache *cache)
1643 {
1644   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1645   enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
1646   int regno;
1647   CORE_ADDR offset, current_pc;
1648   pv_t regs[ARM_FPS_REGNUM];
1649   struct pv_area *stack;
1650   struct cleanup *back_to;
1651   CORE_ADDR unrecognized_pc = 0;
1652
1653   /* Search the prologue looking for instructions that set up the
1654      frame pointer, adjust the stack pointer, and save registers.
1655
1656      Be careful, however, and if it doesn't look like a prologue,
1657      don't try to scan it.  If, for instance, a frameless function
1658      begins with stmfd sp!, then we will tell ourselves there is
1659      a frame, which will confuse stack traceback, as well as "finish" 
1660      and other operations that rely on a knowledge of the stack
1661      traceback.  */
1662
1663   for (regno = 0; regno < ARM_FPS_REGNUM; regno++)
1664     regs[regno] = pv_register (regno, 0);
1665   stack = make_pv_area (ARM_SP_REGNUM, gdbarch_addr_bit (gdbarch));
1666   back_to = make_cleanup_free_pv_area (stack);
1667
1668   for (current_pc = prologue_start;
1669        current_pc < prologue_end;
1670        current_pc += 4)
1671     {
1672       unsigned int insn
1673         = read_memory_unsigned_integer (current_pc, 4, byte_order_for_code);
1674
1675       if (insn == 0xe1a0c00d)           /* mov ip, sp */
1676         {
1677           regs[ARM_IP_REGNUM] = regs[ARM_SP_REGNUM];
1678           continue;
1679         }
1680       else if ((insn & 0xfff00000) == 0xe2800000        /* add Rd, Rn, #n */
1681                && pv_is_register (regs[bits (insn, 16, 19)], ARM_SP_REGNUM))
1682         {
1683           unsigned imm = insn & 0xff;                   /* immediate value */
1684           unsigned rot = (insn & 0xf00) >> 7;           /* rotate amount */
1685           int rd = bits (insn, 12, 15);
1686           imm = (imm >> rot) | (imm << (32 - rot));
1687           regs[rd] = pv_add_constant (regs[bits (insn, 16, 19)], imm);
1688           continue;
1689         }
1690       else if ((insn & 0xfff00000) == 0xe2400000        /* sub Rd, Rn, #n */
1691                && pv_is_register (regs[bits (insn, 16, 19)], ARM_SP_REGNUM))
1692         {
1693           unsigned imm = insn & 0xff;                   /* immediate value */
1694           unsigned rot = (insn & 0xf00) >> 7;           /* rotate amount */
1695           int rd = bits (insn, 12, 15);
1696           imm = (imm >> rot) | (imm << (32 - rot));
1697           regs[rd] = pv_add_constant (regs[bits (insn, 16, 19)], -imm);
1698           continue;
1699         }
1700       else if ((insn & 0xffff0fff) == 0xe52d0004)       /* str Rd,
1701                                                            [sp, #-4]! */
1702         {
1703           if (pv_area_store_would_trash (stack, regs[ARM_SP_REGNUM]))
1704             break;
1705           regs[ARM_SP_REGNUM] = pv_add_constant (regs[ARM_SP_REGNUM], -4);
1706           pv_area_store (stack, regs[ARM_SP_REGNUM], 4,
1707                          regs[bits (insn, 12, 15)]);
1708           continue;
1709         }
1710       else if ((insn & 0xffff0000) == 0xe92d0000)
1711         /* stmfd sp!, {..., fp, ip, lr, pc}
1712            or
1713            stmfd sp!, {a1, a2, a3, a4}  */
1714         {
1715           int mask = insn & 0xffff;
1716
1717           if (pv_area_store_would_trash (stack, regs[ARM_SP_REGNUM]))
1718             break;
1719
1720           /* Calculate offsets of saved registers.  */
1721           for (regno = ARM_PC_REGNUM; regno >= 0; regno--)
1722             if (mask & (1 << regno))
1723               {
1724                 regs[ARM_SP_REGNUM]
1725                   = pv_add_constant (regs[ARM_SP_REGNUM], -4);
1726                 pv_area_store (stack, regs[ARM_SP_REGNUM], 4, regs[regno]);
1727               }
1728         }
1729       else if ((insn & 0xffff0000) == 0xe54b0000        /* strb rx,[r11,#-n] */
1730                || (insn & 0xffff00f0) == 0xe14b00b0     /* strh rx,[r11,#-n] */
1731                || (insn & 0xffffc000) == 0xe50b0000)    /* str  rx,[r11,#-n] */
1732         {
1733           /* No need to add this to saved_regs -- it's just an arg reg.  */
1734           continue;
1735         }
1736       else if ((insn & 0xffff0000) == 0xe5cd0000        /* strb rx,[sp,#n] */
1737                || (insn & 0xffff00f0) == 0xe1cd00b0     /* strh rx,[sp,#n] */
1738                || (insn & 0xffffc000) == 0xe58d0000)    /* str  rx,[sp,#n] */
1739         {
1740           /* No need to add this to saved_regs -- it's just an arg reg.  */
1741           continue;
1742         }
1743       else if ((insn & 0xfff00000) == 0xe8800000        /* stm Rn,
1744                                                            { registers } */
1745                && pv_is_register (regs[bits (insn, 16, 19)], ARM_SP_REGNUM))
1746         {
1747           /* No need to add this to saved_regs -- it's just arg regs.  */
1748           continue;
1749         }
1750       else if ((insn & 0xfffff000) == 0xe24cb000)       /* sub fp, ip #n */
1751         {
1752           unsigned imm = insn & 0xff;                   /* immediate value */
1753           unsigned rot = (insn & 0xf00) >> 7;           /* rotate amount */
1754           imm = (imm >> rot) | (imm << (32 - rot));
1755           regs[ARM_FP_REGNUM] = pv_add_constant (regs[ARM_IP_REGNUM], -imm);
1756         }
1757       else if ((insn & 0xfffff000) == 0xe24dd000)       /* sub sp, sp #n */
1758         {
1759           unsigned imm = insn & 0xff;                   /* immediate value */
1760           unsigned rot = (insn & 0xf00) >> 7;           /* rotate amount */
1761           imm = (imm >> rot) | (imm << (32 - rot));
1762           regs[ARM_SP_REGNUM] = pv_add_constant (regs[ARM_SP_REGNUM], -imm);
1763         }
1764       else if ((insn & 0xffff7fff) == 0xed6d0103        /* stfe f?,
1765                                                            [sp, -#c]! */
1766                && gdbarch_tdep (gdbarch)->have_fpa_registers)
1767         {
1768           if (pv_area_store_would_trash (stack, regs[ARM_SP_REGNUM]))
1769             break;
1770
1771           regs[ARM_SP_REGNUM] = pv_add_constant (regs[ARM_SP_REGNUM], -12);
1772           regno = ARM_F0_REGNUM + ((insn >> 12) & 0x07);
1773           pv_area_store (stack, regs[ARM_SP_REGNUM], 12, regs[regno]);
1774         }
1775       else if ((insn & 0xffbf0fff) == 0xec2d0200        /* sfmfd f0, 4,
1776                                                            [sp!] */
1777                && gdbarch_tdep (gdbarch)->have_fpa_registers)
1778         {
1779           int n_saved_fp_regs;
1780           unsigned int fp_start_reg, fp_bound_reg;
1781
1782           if (pv_area_store_would_trash (stack, regs[ARM_SP_REGNUM]))
1783             break;
1784
1785           if ((insn & 0x800) == 0x800)          /* N0 is set */
1786             {
1787               if ((insn & 0x40000) == 0x40000)  /* N1 is set */
1788                 n_saved_fp_regs = 3;
1789               else
1790                 n_saved_fp_regs = 1;
1791             }
1792           else
1793             {
1794               if ((insn & 0x40000) == 0x40000)  /* N1 is set */
1795                 n_saved_fp_regs = 2;
1796               else
1797                 n_saved_fp_regs = 4;
1798             }
1799
1800           fp_start_reg = ARM_F0_REGNUM + ((insn >> 12) & 0x7);
1801           fp_bound_reg = fp_start_reg + n_saved_fp_regs;
1802           for (; fp_start_reg < fp_bound_reg; fp_start_reg++)
1803             {
1804               regs[ARM_SP_REGNUM] = pv_add_constant (regs[ARM_SP_REGNUM], -12);
1805               pv_area_store (stack, regs[ARM_SP_REGNUM], 12,
1806                              regs[fp_start_reg++]);
1807             }
1808         }
1809       else if ((insn & 0xff000000) == 0xeb000000 && cache == NULL) /* bl */
1810         {
1811           /* Allow some special function calls when skipping the
1812              prologue; GCC generates these before storing arguments to
1813              the stack.  */
1814           CORE_ADDR dest = BranchDest (current_pc, insn);
1815
1816           if (skip_prologue_function (gdbarch, dest, 0))
1817             continue;
1818           else
1819             break;
1820         }
1821       else if ((insn & 0xf0000000) != 0xe0000000)
1822         break;                  /* Condition not true, exit early.  */
1823       else if (arm_instruction_changes_pc (insn))
1824         /* Don't scan past anything that might change control flow.  */
1825         break;
1826       else if (arm_instruction_restores_sp (insn))
1827         {
1828           /* Don't scan past the epilogue.  */
1829           break;
1830         }
1831       else if ((insn & 0xfe500000) == 0xe8100000        /* ldm */
1832                && pv_is_register (regs[bits (insn, 16, 19)], ARM_SP_REGNUM))
1833         /* Ignore block loads from the stack, potentially copying
1834            parameters from memory.  */
1835         continue;
1836       else if ((insn & 0xfc500000) == 0xe4100000
1837                && pv_is_register (regs[bits (insn, 16, 19)], ARM_SP_REGNUM))
1838         /* Similarly ignore single loads from the stack.  */
1839         continue;
1840       else if ((insn & 0xffff0ff0) == 0xe1a00000)
1841         /* MOV Rd, Rm.  Skip register copies, i.e. saves to another
1842            register instead of the stack.  */
1843         continue;
1844       else
1845         {
1846           /* The optimizer might shove anything into the prologue, if
1847              we build up cache (cache != NULL) from scanning prologue,
1848              we just skip what we don't recognize and scan further to
1849              make cache as complete as possible.  However, if we skip
1850              prologue, we'll stop immediately on unrecognized
1851              instruction.  */
1852           unrecognized_pc = current_pc;
1853           if (cache != NULL)
1854             continue;
1855           else
1856             break;
1857         }
1858     }
1859
1860   if (unrecognized_pc == 0)
1861     unrecognized_pc = current_pc;
1862
1863   if (cache)
1864     {
1865       int framereg, framesize;
1866
1867       /* The frame size is just the distance from the frame register
1868          to the original stack pointer.  */
1869       if (pv_is_register (regs[ARM_FP_REGNUM], ARM_SP_REGNUM))
1870         {
1871           /* Frame pointer is fp.  */
1872           framereg = ARM_FP_REGNUM;
1873           framesize = -regs[ARM_FP_REGNUM].k;
1874         }
1875       else
1876         {
1877           /* Try the stack pointer... this is a bit desperate.  */
1878           framereg = ARM_SP_REGNUM;
1879           framesize = -regs[ARM_SP_REGNUM].k;
1880         }
1881
1882       cache->framereg = framereg;
1883       cache->framesize = framesize;
1884
1885       for (regno = 0; regno < ARM_FPS_REGNUM; regno++)
1886         if (pv_area_find_reg (stack, gdbarch, regno, &offset))
1887           cache->saved_regs[regno].addr = offset;
1888     }
1889
1890   if (arm_debug)
1891     fprintf_unfiltered (gdb_stdlog, "Prologue scan stopped at %s\n",
1892                         paddress (gdbarch, unrecognized_pc));
1893
1894   do_cleanups (back_to);
1895   return unrecognized_pc;
1896 }
1897
1898 static void
1899 arm_scan_prologue (struct frame_info *this_frame,
1900                    struct arm_prologue_cache *cache)
1901 {
1902   struct gdbarch *gdbarch = get_frame_arch (this_frame);
1903   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1904   int regno;
1905   CORE_ADDR prologue_start, prologue_end, current_pc;
1906   CORE_ADDR prev_pc = get_frame_pc (this_frame);
1907   CORE_ADDR block_addr = get_frame_address_in_block (this_frame);
1908   pv_t regs[ARM_FPS_REGNUM];
1909   struct pv_area *stack;
1910   struct cleanup *back_to;
1911   CORE_ADDR offset;
1912
1913   /* Assume there is no frame until proven otherwise.  */
1914   cache->framereg = ARM_SP_REGNUM;
1915   cache->framesize = 0;
1916
1917   /* Check for Thumb prologue.  */
1918   if (arm_frame_is_thumb (this_frame))
1919     {
1920       thumb_scan_prologue (gdbarch, prev_pc, block_addr, cache);
1921       return;
1922     }
1923
1924   /* Find the function prologue.  If we can't find the function in
1925      the symbol table, peek in the stack frame to find the PC.  */
1926   if (find_pc_partial_function (block_addr, NULL, &prologue_start,
1927                                 &prologue_end))
1928     {
1929       /* One way to find the end of the prologue (which works well
1930          for unoptimized code) is to do the following:
1931
1932             struct symtab_and_line sal = find_pc_line (prologue_start, 0);
1933
1934             if (sal.line == 0)
1935               prologue_end = prev_pc;
1936             else if (sal.end < prologue_end)
1937               prologue_end = sal.end;
1938
1939          This mechanism is very accurate so long as the optimizer
1940          doesn't move any instructions from the function body into the
1941          prologue.  If this happens, sal.end will be the last
1942          instruction in the first hunk of prologue code just before
1943          the first instruction that the scheduler has moved from
1944          the body to the prologue.
1945
1946          In order to make sure that we scan all of the prologue
1947          instructions, we use a slightly less accurate mechanism which
1948          may scan more than necessary.  To help compensate for this
1949          lack of accuracy, the prologue scanning loop below contains
1950          several clauses which'll cause the loop to terminate early if
1951          an implausible prologue instruction is encountered.
1952
1953          The expression
1954
1955               prologue_start + 64
1956
1957          is a suitable endpoint since it accounts for the largest
1958          possible prologue plus up to five instructions inserted by
1959          the scheduler.  */
1960
1961       if (prologue_end > prologue_start + 64)
1962         {
1963           prologue_end = prologue_start + 64;   /* See above.  */
1964         }
1965     }
1966   else
1967     {
1968       /* We have no symbol information.  Our only option is to assume this
1969          function has a standard stack frame and the normal frame register.
1970          Then, we can find the value of our frame pointer on entrance to
1971          the callee (or at the present moment if this is the innermost frame).
1972          The value stored there should be the address of the stmfd + 8.  */
1973       CORE_ADDR frame_loc;
1974       LONGEST return_value;
1975
1976       frame_loc = get_frame_register_unsigned (this_frame, ARM_FP_REGNUM);
1977       if (!safe_read_memory_integer (frame_loc, 4, byte_order, &return_value))
1978         return;
1979       else
1980         {
1981           prologue_start = gdbarch_addr_bits_remove
1982                              (gdbarch, return_value) - 8;
1983           prologue_end = prologue_start + 64;   /* See above.  */
1984         }
1985     }
1986
1987   if (prev_pc < prologue_end)
1988     prologue_end = prev_pc;
1989
1990   arm_analyze_prologue (gdbarch, prologue_start, prologue_end, cache);
1991 }
1992
1993 static struct arm_prologue_cache *
1994 arm_make_prologue_cache (struct frame_info *this_frame)
1995 {
1996   int reg;
1997   struct arm_prologue_cache *cache;
1998   CORE_ADDR unwound_fp;
1999
2000   cache = FRAME_OBSTACK_ZALLOC (struct arm_prologue_cache);
2001   cache->saved_regs = trad_frame_alloc_saved_regs (this_frame);
2002
2003   arm_scan_prologue (this_frame, cache);
2004
2005   unwound_fp = get_frame_register_unsigned (this_frame, cache->framereg);
2006   if (unwound_fp == 0)
2007     return cache;
2008
2009   cache->prev_sp = unwound_fp + cache->framesize;
2010
2011   /* Calculate actual addresses of saved registers using offsets
2012      determined by arm_scan_prologue.  */
2013   for (reg = 0; reg < gdbarch_num_regs (get_frame_arch (this_frame)); reg++)
2014     if (trad_frame_addr_p (cache->saved_regs, reg))
2015       cache->saved_regs[reg].addr += cache->prev_sp;
2016
2017   return cache;
2018 }
2019
2020 /* Implementation of the stop_reason hook for arm_prologue frames.  */
2021
2022 static enum unwind_stop_reason
2023 arm_prologue_unwind_stop_reason (struct frame_info *this_frame,
2024                                  void **this_cache)
2025 {
2026   struct arm_prologue_cache *cache;
2027   CORE_ADDR pc;
2028
2029   if (*this_cache == NULL)
2030     *this_cache = arm_make_prologue_cache (this_frame);
2031   cache = *this_cache;
2032
2033   /* This is meant to halt the backtrace at "_start".  */
2034   pc = get_frame_pc (this_frame);
2035   if (pc <= gdbarch_tdep (get_frame_arch (this_frame))->lowest_pc)
2036     return UNWIND_OUTERMOST;
2037
2038   /* If we've hit a wall, stop.  */
2039   if (cache->prev_sp == 0)
2040     return UNWIND_OUTERMOST;
2041
2042   return UNWIND_NO_REASON;
2043 }
2044
2045 /* Our frame ID for a normal frame is the current function's starting PC
2046    and the caller's SP when we were called.  */
2047
2048 static void
2049 arm_prologue_this_id (struct frame_info *this_frame,
2050                       void **this_cache,
2051                       struct frame_id *this_id)
2052 {
2053   struct arm_prologue_cache *cache;
2054   struct frame_id id;
2055   CORE_ADDR pc, func;
2056
2057   if (*this_cache == NULL)
2058     *this_cache = arm_make_prologue_cache (this_frame);
2059   cache = *this_cache;
2060
2061   /* Use function start address as part of the frame ID.  If we cannot
2062      identify the start address (due to missing symbol information),
2063      fall back to just using the current PC.  */
2064   pc = get_frame_pc (this_frame);
2065   func = get_frame_func (this_frame);
2066   if (!func)
2067     func = pc;
2068
2069   id = frame_id_build (cache->prev_sp, func);
2070   *this_id = id;
2071 }
2072
2073 static struct value *
2074 arm_prologue_prev_register (struct frame_info *this_frame,
2075                             void **this_cache,
2076                             int prev_regnum)
2077 {
2078   struct gdbarch *gdbarch = get_frame_arch (this_frame);
2079   struct arm_prologue_cache *cache;
2080
2081   if (*this_cache == NULL)
2082     *this_cache = arm_make_prologue_cache (this_frame);
2083   cache = *this_cache;
2084
2085   /* If we are asked to unwind the PC, then we need to return the LR
2086      instead.  The prologue may save PC, but it will point into this
2087      frame's prologue, not the next frame's resume location.  Also
2088      strip the saved T bit.  A valid LR may have the low bit set, but
2089      a valid PC never does.  */
2090   if (prev_regnum == ARM_PC_REGNUM)
2091     {
2092       CORE_ADDR lr;
2093
2094       lr = frame_unwind_register_unsigned (this_frame, ARM_LR_REGNUM);
2095       return frame_unwind_got_constant (this_frame, prev_regnum,
2096                                         arm_addr_bits_remove (gdbarch, lr));
2097     }
2098
2099   /* SP is generally not saved to the stack, but this frame is
2100      identified by the next frame's stack pointer at the time of the call.
2101      The value was already reconstructed into PREV_SP.  */
2102   if (prev_regnum == ARM_SP_REGNUM)
2103     return frame_unwind_got_constant (this_frame, prev_regnum, cache->prev_sp);
2104
2105   /* The CPSR may have been changed by the call instruction and by the
2106      called function.  The only bit we can reconstruct is the T bit,
2107      by checking the low bit of LR as of the call.  This is a reliable
2108      indicator of Thumb-ness except for some ARM v4T pre-interworking
2109      Thumb code, which could get away with a clear low bit as long as
2110      the called function did not use bx.  Guess that all other
2111      bits are unchanged; the condition flags are presumably lost,
2112      but the processor status is likely valid.  */
2113   if (prev_regnum == ARM_PS_REGNUM)
2114     {
2115       CORE_ADDR lr, cpsr;
2116       ULONGEST t_bit = arm_psr_thumb_bit (gdbarch);
2117
2118       cpsr = get_frame_register_unsigned (this_frame, prev_regnum);
2119       lr = frame_unwind_register_unsigned (this_frame, ARM_LR_REGNUM);
2120       if (IS_THUMB_ADDR (lr))
2121         cpsr |= t_bit;
2122       else
2123         cpsr &= ~t_bit;
2124       return frame_unwind_got_constant (this_frame, prev_regnum, cpsr);
2125     }
2126
2127   return trad_frame_get_prev_register (this_frame, cache->saved_regs,
2128                                        prev_regnum);
2129 }
2130
2131 struct frame_unwind arm_prologue_unwind = {
2132   NORMAL_FRAME,
2133   arm_prologue_unwind_stop_reason,
2134   arm_prologue_this_id,
2135   arm_prologue_prev_register,
2136   NULL,
2137   default_frame_sniffer
2138 };
2139
2140 /* Maintain a list of ARM exception table entries per objfile, similar to the
2141    list of mapping symbols.  We only cache entries for standard ARM-defined
2142    personality routines; the cache will contain only the frame unwinding
2143    instructions associated with the entry (not the descriptors).  */
2144
2145 static const struct objfile_data *arm_exidx_data_key;
2146
2147 struct arm_exidx_entry
2148 {
2149   bfd_vma addr;
2150   gdb_byte *entry;
2151 };
2152 typedef struct arm_exidx_entry arm_exidx_entry_s;
2153 DEF_VEC_O(arm_exidx_entry_s);
2154
2155 struct arm_exidx_data
2156 {
2157   VEC(arm_exidx_entry_s) **section_maps;
2158 };
2159
2160 static void
2161 arm_exidx_data_free (struct objfile *objfile, void *arg)
2162 {
2163   struct arm_exidx_data *data = arg;
2164   unsigned int i;
2165
2166   for (i = 0; i < objfile->obfd->section_count; i++)
2167     VEC_free (arm_exidx_entry_s, data->section_maps[i]);
2168 }
2169
2170 static inline int
2171 arm_compare_exidx_entries (const struct arm_exidx_entry *lhs,
2172                            const struct arm_exidx_entry *rhs)
2173 {
2174   return lhs->addr < rhs->addr;
2175 }
2176
2177 static struct obj_section *
2178 arm_obj_section_from_vma (struct objfile *objfile, bfd_vma vma)
2179 {
2180   struct obj_section *osect;
2181
2182   ALL_OBJFILE_OSECTIONS (objfile, osect)
2183     if (bfd_get_section_flags (objfile->obfd,
2184                                osect->the_bfd_section) & SEC_ALLOC)
2185       {
2186         bfd_vma start, size;
2187         start = bfd_get_section_vma (objfile->obfd, osect->the_bfd_section);
2188         size = bfd_get_section_size (osect->the_bfd_section);
2189
2190         if (start <= vma && vma < start + size)
2191           return osect;
2192       }
2193
2194   return NULL;
2195 }
2196
2197 /* Parse contents of exception table and exception index sections
2198    of OBJFILE, and fill in the exception table entry cache.
2199
2200    For each entry that refers to a standard ARM-defined personality
2201    routine, extract the frame unwinding instructions (from either
2202    the index or the table section).  The unwinding instructions
2203    are normalized by:
2204     - extracting them from the rest of the table data
2205     - converting to host endianness
2206     - appending the implicit 0xb0 ("Finish") code
2207
2208    The extracted and normalized instructions are stored for later
2209    retrieval by the arm_find_exidx_entry routine.  */
2210  
2211 static void
2212 arm_exidx_new_objfile (struct objfile *objfile)
2213 {
2214   struct cleanup *cleanups;
2215   struct arm_exidx_data *data;
2216   asection *exidx, *extab;
2217   bfd_vma exidx_vma = 0, extab_vma = 0;
2218   bfd_size_type exidx_size = 0, extab_size = 0;
2219   gdb_byte *exidx_data = NULL, *extab_data = NULL;
2220   LONGEST i;
2221
2222   /* If we've already touched this file, do nothing.  */
2223   if (!objfile || objfile_data (objfile, arm_exidx_data_key) != NULL)
2224     return;
2225   cleanups = make_cleanup (null_cleanup, NULL);
2226
2227   /* Read contents of exception table and index.  */
2228   exidx = bfd_get_section_by_name (objfile->obfd, ".ARM.exidx");
2229   if (exidx)
2230     {
2231       exidx_vma = bfd_section_vma (objfile->obfd, exidx);
2232       exidx_size = bfd_get_section_size (exidx);
2233       exidx_data = xmalloc (exidx_size);
2234       make_cleanup (xfree, exidx_data);
2235
2236       if (!bfd_get_section_contents (objfile->obfd, exidx,
2237                                      exidx_data, 0, exidx_size))
2238         {
2239           do_cleanups (cleanups);
2240           return;
2241         }
2242     }
2243
2244   extab = bfd_get_section_by_name (objfile->obfd, ".ARM.extab");
2245   if (extab)
2246     {
2247       extab_vma = bfd_section_vma (objfile->obfd, extab);
2248       extab_size = bfd_get_section_size (extab);
2249       extab_data = xmalloc (extab_size);
2250       make_cleanup (xfree, extab_data);
2251
2252       if (!bfd_get_section_contents (objfile->obfd, extab,
2253                                      extab_data, 0, extab_size))
2254         {
2255           do_cleanups (cleanups);
2256           return;
2257         }
2258     }
2259
2260   /* Allocate exception table data structure.  */
2261   data = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct arm_exidx_data);
2262   set_objfile_data (objfile, arm_exidx_data_key, data);
2263   data->section_maps = OBSTACK_CALLOC (&objfile->objfile_obstack,
2264                                        objfile->obfd->section_count,
2265                                        VEC(arm_exidx_entry_s) *);
2266
2267   /* Fill in exception table.  */
2268   for (i = 0; i < exidx_size / 8; i++)
2269     {
2270       struct arm_exidx_entry new_exidx_entry;
2271       bfd_vma idx = bfd_h_get_32 (objfile->obfd, exidx_data + i * 8);
2272       bfd_vma val = bfd_h_get_32 (objfile->obfd, exidx_data + i * 8 + 4);
2273       bfd_vma addr = 0, word = 0;
2274       int n_bytes = 0, n_words = 0;
2275       struct obj_section *sec;
2276       gdb_byte *entry = NULL;
2277
2278       /* Extract address of start of function.  */
2279       idx = ((idx & 0x7fffffff) ^ 0x40000000) - 0x40000000;
2280       idx += exidx_vma + i * 8;
2281
2282       /* Find section containing function and compute section offset.  */
2283       sec = arm_obj_section_from_vma (objfile, idx);
2284       if (sec == NULL)
2285         continue;
2286       idx -= bfd_get_section_vma (objfile->obfd, sec->the_bfd_section);
2287
2288       /* Determine address of exception table entry.  */
2289       if (val == 1)
2290         {
2291           /* EXIDX_CANTUNWIND -- no exception table entry present.  */
2292         }
2293       else if ((val & 0xff000000) == 0x80000000)
2294         {
2295           /* Exception table entry embedded in .ARM.exidx
2296              -- must be short form.  */
2297           word = val;
2298           n_bytes = 3;
2299         }
2300       else if (!(val & 0x80000000))
2301         {
2302           /* Exception table entry in .ARM.extab.  */
2303           addr = ((val & 0x7fffffff) ^ 0x40000000) - 0x40000000;
2304           addr += exidx_vma + i * 8 + 4;
2305
2306           if (addr >= extab_vma && addr + 4 <= extab_vma + extab_size)
2307             {
2308               word = bfd_h_get_32 (objfile->obfd,
2309                                    extab_data + addr - extab_vma);
2310               addr += 4;
2311
2312               if ((word & 0xff000000) == 0x80000000)
2313                 {
2314                   /* Short form.  */
2315                   n_bytes = 3;
2316                 }
2317               else if ((word & 0xff000000) == 0x81000000
2318                        || (word & 0xff000000) == 0x82000000)
2319                 {
2320                   /* Long form.  */
2321                   n_bytes = 2;
2322                   n_words = ((word >> 16) & 0xff);
2323                 }
2324               else if (!(word & 0x80000000))
2325                 {
2326                   bfd_vma pers;
2327                   struct obj_section *pers_sec;
2328                   int gnu_personality = 0;
2329
2330                   /* Custom personality routine.  */
2331                   pers = ((word & 0x7fffffff) ^ 0x40000000) - 0x40000000;
2332                   pers = UNMAKE_THUMB_ADDR (pers + addr - 4);
2333
2334                   /* Check whether we've got one of the variants of the
2335                      GNU personality routines.  */
2336                   pers_sec = arm_obj_section_from_vma (objfile, pers);
2337                   if (pers_sec)
2338                     {
2339                       static const char *personality[] = 
2340                         {
2341                           "__gcc_personality_v0",
2342                           "__gxx_personality_v0",
2343                           "__gcj_personality_v0",
2344                           "__gnu_objc_personality_v0",
2345                           NULL
2346                         };
2347
2348                       CORE_ADDR pc = pers + obj_section_offset (pers_sec);
2349                       int k;
2350
2351                       for (k = 0; personality[k]; k++)
2352                         if (lookup_minimal_symbol_by_pc_name
2353                               (pc, personality[k], objfile))
2354                           {
2355                             gnu_personality = 1;
2356                             break;
2357                           }
2358                     }
2359
2360                   /* If so, the next word contains a word count in the high
2361                      byte, followed by the same unwind instructions as the
2362                      pre-defined forms.  */
2363                   if (gnu_personality
2364                       && addr + 4 <= extab_vma + extab_size)
2365                     {
2366                       word = bfd_h_get_32 (objfile->obfd,
2367                                            extab_data + addr - extab_vma);
2368                       addr += 4;
2369                       n_bytes = 3;
2370                       n_words = ((word >> 24) & 0xff);
2371                     }
2372                 }
2373             }
2374         }
2375
2376       /* Sanity check address.  */
2377       if (n_words)
2378         if (addr < extab_vma || addr + 4 * n_words > extab_vma + extab_size)
2379           n_words = n_bytes = 0;
2380
2381       /* The unwind instructions reside in WORD (only the N_BYTES least
2382          significant bytes are valid), followed by N_WORDS words in the
2383          extab section starting at ADDR.  */
2384       if (n_bytes || n_words)
2385         {
2386           gdb_byte *p = entry = obstack_alloc (&objfile->objfile_obstack,
2387                                                n_bytes + n_words * 4 + 1);
2388
2389           while (n_bytes--)
2390             *p++ = (gdb_byte) ((word >> (8 * n_bytes)) & 0xff);
2391
2392           while (n_words--)
2393             {
2394               word = bfd_h_get_32 (objfile->obfd,
2395                                    extab_data + addr - extab_vma);
2396               addr += 4;
2397
2398               *p++ = (gdb_byte) ((word >> 24) & 0xff);
2399               *p++ = (gdb_byte) ((word >> 16) & 0xff);
2400               *p++ = (gdb_byte) ((word >> 8) & 0xff);
2401               *p++ = (gdb_byte) (word & 0xff);
2402             }
2403
2404           /* Implied "Finish" to terminate the list.  */
2405           *p++ = 0xb0;
2406         }
2407
2408       /* Push entry onto vector.  They are guaranteed to always
2409          appear in order of increasing addresses.  */
2410       new_exidx_entry.addr = idx;
2411       new_exidx_entry.entry = entry;
2412       VEC_safe_push (arm_exidx_entry_s,
2413                      data->section_maps[sec->the_bfd_section->index],
2414                      &new_exidx_entry);
2415     }
2416
2417   do_cleanups (cleanups);
2418 }
2419
2420 /* Search for the exception table entry covering MEMADDR.  If one is found,
2421    return a pointer to its data.  Otherwise, return 0.  If START is non-NULL,
2422    set *START to the start of the region covered by this entry.  */
2423
2424 static gdb_byte *
2425 arm_find_exidx_entry (CORE_ADDR memaddr, CORE_ADDR *start)
2426 {
2427   struct obj_section *sec;
2428
2429   sec = find_pc_section (memaddr);
2430   if (sec != NULL)
2431     {
2432       struct arm_exidx_data *data;
2433       VEC(arm_exidx_entry_s) *map;
2434       struct arm_exidx_entry map_key = { memaddr - obj_section_addr (sec), 0 };
2435       unsigned int idx;
2436
2437       data = objfile_data (sec->objfile, arm_exidx_data_key);
2438       if (data != NULL)
2439         {
2440           map = data->section_maps[sec->the_bfd_section->index];
2441           if (!VEC_empty (arm_exidx_entry_s, map))
2442             {
2443               struct arm_exidx_entry *map_sym;
2444
2445               idx = VEC_lower_bound (arm_exidx_entry_s, map, &map_key,
2446                                      arm_compare_exidx_entries);
2447
2448               /* VEC_lower_bound finds the earliest ordered insertion
2449                  point.  If the following symbol starts at this exact
2450                  address, we use that; otherwise, the preceding
2451                  exception table entry covers this address.  */
2452               if (idx < VEC_length (arm_exidx_entry_s, map))
2453                 {
2454                   map_sym = VEC_index (arm_exidx_entry_s, map, idx);
2455                   if (map_sym->addr == map_key.addr)
2456                     {
2457                       if (start)
2458                         *start = map_sym->addr + obj_section_addr (sec);
2459                       return map_sym->entry;
2460                     }
2461                 }
2462
2463               if (idx > 0)
2464                 {
2465                   map_sym = VEC_index (arm_exidx_entry_s, map, idx - 1);
2466                   if (start)
2467                     *start = map_sym->addr + obj_section_addr (sec);
2468                   return map_sym->entry;
2469                 }
2470             }
2471         }
2472     }
2473
2474   return NULL;
2475 }
2476
2477 /* Given the current frame THIS_FRAME, and its associated frame unwinding
2478    instruction list from the ARM exception table entry ENTRY, allocate and
2479    return a prologue cache structure describing how to unwind this frame.
2480
2481    Return NULL if the unwinding instruction list contains a "spare",
2482    "reserved" or "refuse to unwind" instruction as defined in section
2483    "9.3 Frame unwinding instructions" of the "Exception Handling ABI
2484    for the ARM Architecture" document.  */
2485
2486 static struct arm_prologue_cache *
2487 arm_exidx_fill_cache (struct frame_info *this_frame, gdb_byte *entry)
2488 {
2489   CORE_ADDR vsp = 0;
2490   int vsp_valid = 0;
2491
2492   struct arm_prologue_cache *cache;
2493   cache = FRAME_OBSTACK_ZALLOC (struct arm_prologue_cache);
2494   cache->saved_regs = trad_frame_alloc_saved_regs (this_frame);
2495
2496   for (;;)
2497     {
2498       gdb_byte insn;
2499
2500       /* Whenever we reload SP, we actually have to retrieve its
2501          actual value in the current frame.  */
2502       if (!vsp_valid)
2503         {
2504           if (trad_frame_realreg_p (cache->saved_regs, ARM_SP_REGNUM))
2505             {
2506               int reg = cache->saved_regs[ARM_SP_REGNUM].realreg;
2507               vsp = get_frame_register_unsigned (this_frame, reg);
2508             }
2509           else
2510             {
2511               CORE_ADDR addr = cache->saved_regs[ARM_SP_REGNUM].addr;
2512               vsp = get_frame_memory_unsigned (this_frame, addr, 4);
2513             }
2514
2515           vsp_valid = 1;
2516         }
2517
2518       /* Decode next unwind instruction.  */
2519       insn = *entry++;
2520
2521       if ((insn & 0xc0) == 0)
2522         {
2523           int offset = insn & 0x3f;
2524           vsp += (offset << 2) + 4;
2525         }
2526       else if ((insn & 0xc0) == 0x40)
2527         {
2528           int offset = insn & 0x3f;
2529           vsp -= (offset << 2) + 4;
2530         }
2531       else if ((insn & 0xf0) == 0x80)
2532         {
2533           int mask = ((insn & 0xf) << 8) | *entry++;
2534           int i;
2535
2536           /* The special case of an all-zero mask identifies
2537              "Refuse to unwind".  We return NULL to fall back
2538              to the prologue analyzer.  */
2539           if (mask == 0)
2540             return NULL;
2541
2542           /* Pop registers r4..r15 under mask.  */
2543           for (i = 0; i < 12; i++)
2544             if (mask & (1 << i))
2545               {
2546                 cache->saved_regs[4 + i].addr = vsp;
2547                 vsp += 4;
2548               }
2549
2550           /* Special-case popping SP -- we need to reload vsp.  */
2551           if (mask & (1 << (ARM_SP_REGNUM - 4)))
2552             vsp_valid = 0;
2553         }
2554       else if ((insn & 0xf0) == 0x90)
2555         {
2556           int reg = insn & 0xf;
2557
2558           /* Reserved cases.  */
2559           if (reg == ARM_SP_REGNUM || reg == ARM_PC_REGNUM)
2560             return NULL;
2561
2562           /* Set SP from another register and mark VSP for reload.  */
2563           cache->saved_regs[ARM_SP_REGNUM] = cache->saved_regs[reg];
2564           vsp_valid = 0;
2565         }
2566       else if ((insn & 0xf0) == 0xa0)
2567         {
2568           int count = insn & 0x7;
2569           int pop_lr = (insn & 0x8) != 0;
2570           int i;
2571
2572           /* Pop r4..r[4+count].  */
2573           for (i = 0; i <= count; i++)
2574             {
2575               cache->saved_regs[4 + i].addr = vsp;
2576               vsp += 4;
2577             }
2578
2579           /* If indicated by flag, pop LR as well.  */
2580           if (pop_lr)
2581             {
2582               cache->saved_regs[ARM_LR_REGNUM].addr = vsp;
2583               vsp += 4;
2584             }
2585         }
2586       else if (insn == 0xb0)
2587         {
2588           /* We could only have updated PC by popping into it; if so, it
2589              will show up as address.  Otherwise, copy LR into PC.  */
2590           if (!trad_frame_addr_p (cache->saved_regs, ARM_PC_REGNUM))
2591             cache->saved_regs[ARM_PC_REGNUM]
2592               = cache->saved_regs[ARM_LR_REGNUM];
2593
2594           /* We're done.  */
2595           break;
2596         }
2597       else if (insn == 0xb1)
2598         {
2599           int mask = *entry++;
2600           int i;
2601
2602           /* All-zero mask and mask >= 16 is "spare".  */
2603           if (mask == 0 || mask >= 16)
2604             return NULL;
2605
2606           /* Pop r0..r3 under mask.  */
2607           for (i = 0; i < 4; i++)
2608             if (mask & (1 << i))
2609               {
2610                 cache->saved_regs[i].addr = vsp;
2611                 vsp += 4;
2612               }
2613         }
2614       else if (insn == 0xb2)
2615         {
2616           ULONGEST offset = 0;
2617           unsigned shift = 0;
2618
2619           do
2620             {
2621               offset |= (*entry & 0x7f) << shift;
2622               shift += 7;
2623             }
2624           while (*entry++ & 0x80);
2625
2626           vsp += 0x204 + (offset << 2);
2627         }
2628       else if (insn == 0xb3)
2629         {
2630           int start = *entry >> 4;
2631           int count = (*entry++) & 0xf;
2632           int i;
2633
2634           /* Only registers D0..D15 are valid here.  */
2635           if (start + count >= 16)
2636             return NULL;
2637
2638           /* Pop VFP double-precision registers D[start]..D[start+count].  */
2639           for (i = 0; i <= count; i++)
2640             {
2641               cache->saved_regs[ARM_D0_REGNUM + start + i].addr = vsp;
2642               vsp += 8;
2643             }
2644
2645           /* Add an extra 4 bytes for FSTMFDX-style stack.  */
2646           vsp += 4;
2647         }
2648       else if ((insn & 0xf8) == 0xb8)
2649         {
2650           int count = insn & 0x7;
2651           int i;
2652
2653           /* Pop VFP double-precision registers D[8]..D[8+count].  */
2654           for (i = 0; i <= count; i++)
2655             {
2656               cache->saved_regs[ARM_D0_REGNUM + 8 + i].addr = vsp;
2657               vsp += 8;
2658             }
2659
2660           /* Add an extra 4 bytes for FSTMFDX-style stack.  */
2661           vsp += 4;
2662         }
2663       else if (insn == 0xc6)
2664         {
2665           int start = *entry >> 4;
2666           int count = (*entry++) & 0xf;
2667           int i;
2668
2669           /* Only registers WR0..WR15 are valid.  */
2670           if (start + count >= 16)
2671             return NULL;
2672
2673           /* Pop iwmmx registers WR[start]..WR[start+count].  */
2674           for (i = 0; i <= count; i++)
2675             {
2676               cache->saved_regs[ARM_WR0_REGNUM + start + i].addr = vsp;
2677               vsp += 8;
2678             }
2679         }
2680       else if (insn == 0xc7)
2681         {
2682           int mask = *entry++;
2683           int i;
2684
2685           /* All-zero mask and mask >= 16 is "spare".  */
2686           if (mask == 0 || mask >= 16)
2687             return NULL;
2688
2689           /* Pop iwmmx general-purpose registers WCGR0..WCGR3 under mask.  */
2690           for (i = 0; i < 4; i++)
2691             if (mask & (1 << i))
2692               {
2693                 cache->saved_regs[ARM_WCGR0_REGNUM + i].addr = vsp;
2694                 vsp += 4;
2695               }
2696         }
2697       else if ((insn & 0xf8) == 0xc0)
2698         {
2699           int count = insn & 0x7;
2700           int i;
2701
2702           /* Pop iwmmx registers WR[10]..WR[10+count].  */
2703           for (i = 0; i <= count; i++)
2704             {
2705               cache->saved_regs[ARM_WR0_REGNUM + 10 + i].addr = vsp;
2706               vsp += 8;
2707             }
2708         }
2709       else if (insn == 0xc8)
2710         {
2711           int start = *entry >> 4;
2712           int count = (*entry++) & 0xf;
2713           int i;
2714
2715           /* Only registers D0..D31 are valid.  */
2716           if (start + count >= 16)
2717             return NULL;
2718
2719           /* Pop VFP double-precision registers
2720              D[16+start]..D[16+start+count].  */
2721           for (i = 0; i <= count; i++)
2722             {
2723               cache->saved_regs[ARM_D0_REGNUM + 16 + start + i].addr = vsp;
2724               vsp += 8;
2725             }
2726         }
2727       else if (insn == 0xc9)
2728         {
2729           int start = *entry >> 4;
2730           int count = (*entry++) & 0xf;
2731           int i;
2732
2733           /* Pop VFP double-precision registers D[start]..D[start+count].  */
2734           for (i = 0; i <= count; i++)
2735             {
2736               cache->saved_regs[ARM_D0_REGNUM + start + i].addr = vsp;
2737               vsp += 8;
2738             }
2739         }
2740       else if ((insn & 0xf8) == 0xd0)
2741         {
2742           int count = insn & 0x7;
2743           int i;
2744
2745           /* Pop VFP double-precision registers D[8]..D[8+count].  */
2746           for (i = 0; i <= count; i++)
2747             {
2748               cache->saved_regs[ARM_D0_REGNUM + 8 + i].addr = vsp;
2749               vsp += 8;
2750             }
2751         }
2752       else
2753         {
2754           /* Everything else is "spare".  */
2755           return NULL;
2756         }
2757     }
2758
2759   /* If we restore SP from a register, assume this was the frame register.
2760      Otherwise just fall back to SP as frame register.  */
2761   if (trad_frame_realreg_p (cache->saved_regs, ARM_SP_REGNUM))
2762     cache->framereg = cache->saved_regs[ARM_SP_REGNUM].realreg;
2763   else
2764     cache->framereg = ARM_SP_REGNUM;
2765
2766   /* Determine offset to previous frame.  */
2767   cache->framesize
2768     = vsp - get_frame_register_unsigned (this_frame, cache->framereg);
2769
2770   /* We already got the previous SP.  */
2771   cache->prev_sp = vsp;
2772
2773   return cache;
2774 }
2775
2776 /* Unwinding via ARM exception table entries.  Note that the sniffer
2777    already computes a filled-in prologue cache, which is then used
2778    with the same arm_prologue_this_id and arm_prologue_prev_register
2779    routines also used for prologue-parsing based unwinding.  */
2780
2781 static int
2782 arm_exidx_unwind_sniffer (const struct frame_unwind *self,
2783                           struct frame_info *this_frame,
2784                           void **this_prologue_cache)
2785 {
2786   struct gdbarch *gdbarch = get_frame_arch (this_frame);
2787   enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
2788   CORE_ADDR addr_in_block, exidx_region, func_start;
2789   struct arm_prologue_cache *cache;
2790   gdb_byte *entry;
2791
2792   /* See if we have an ARM exception table entry covering this address.  */
2793   addr_in_block = get_frame_address_in_block (this_frame);
2794   entry = arm_find_exidx_entry (addr_in_block, &exidx_region);
2795   if (!entry)
2796     return 0;
2797
2798   /* The ARM exception table does not describe unwind information
2799      for arbitrary PC values, but is guaranteed to be correct only
2800      at call sites.  We have to decide here whether we want to use
2801      ARM exception table information for this frame, or fall back
2802      to using prologue parsing.  (Note that if we have DWARF CFI,
2803      this sniffer isn't even called -- CFI is always preferred.)
2804
2805      Before we make this decision, however, we check whether we
2806      actually have *symbol* information for the current frame.
2807      If not, prologue parsing would not work anyway, so we might
2808      as well use the exception table and hope for the best.  */
2809   if (find_pc_partial_function (addr_in_block, NULL, &func_start, NULL))
2810     {
2811       int exc_valid = 0;
2812
2813       /* If the next frame is "normal", we are at a call site in this
2814          frame, so exception information is guaranteed to be valid.  */
2815       if (get_next_frame (this_frame)
2816           && get_frame_type (get_next_frame (this_frame)) == NORMAL_FRAME)
2817         exc_valid = 1;
2818
2819       /* We also assume exception information is valid if we're currently
2820          blocked in a system call.  The system library is supposed to
2821          ensure this, so that e.g. pthread cancellation works.  */
2822       if (arm_frame_is_thumb (this_frame))
2823         {
2824           LONGEST insn;
2825
2826           if (safe_read_memory_integer (get_frame_pc (this_frame) - 2, 2,
2827                                         byte_order_for_code, &insn)
2828               && (insn & 0xff00) == 0xdf00 /* svc */)
2829             exc_valid = 1;
2830         }
2831       else
2832         {
2833           LONGEST insn;
2834
2835           if (safe_read_memory_integer (get_frame_pc (this_frame) - 4, 4,
2836                                         byte_order_for_code, &insn)
2837               && (insn & 0x0f000000) == 0x0f000000 /* svc */)
2838             exc_valid = 1;
2839         }
2840         
2841       /* Bail out if we don't know that exception information is valid.  */
2842       if (!exc_valid)
2843         return 0;
2844
2845      /* The ARM exception index does not mark the *end* of the region
2846         covered by the entry, and some functions will not have any entry.
2847         To correctly recognize the end of the covered region, the linker
2848         should have inserted dummy records with a CANTUNWIND marker.
2849
2850         Unfortunately, current versions of GNU ld do not reliably do
2851         this, and thus we may have found an incorrect entry above.
2852         As a (temporary) sanity check, we only use the entry if it
2853         lies *within* the bounds of the function.  Note that this check
2854         might reject perfectly valid entries that just happen to cover
2855         multiple functions; therefore this check ought to be removed
2856         once the linker is fixed.  */
2857       if (func_start > exidx_region)
2858         return 0;
2859     }
2860
2861   /* Decode the list of unwinding instructions into a prologue cache.
2862      Note that this may fail due to e.g. a "refuse to unwind" code.  */
2863   cache = arm_exidx_fill_cache (this_frame, entry);
2864   if (!cache)
2865     return 0;
2866
2867   *this_prologue_cache = cache;
2868   return 1;
2869 }
2870
2871 struct frame_unwind arm_exidx_unwind = {
2872   NORMAL_FRAME,
2873   default_frame_unwind_stop_reason,
2874   arm_prologue_this_id,
2875   arm_prologue_prev_register,
2876   NULL,
2877   arm_exidx_unwind_sniffer
2878 };
2879
2880 /* Recognize GCC's trampoline for thumb call-indirect.  If we are in a
2881    trampoline, return the target PC.  Otherwise return 0.
2882
2883    void call0a (char c, short s, int i, long l) {}
2884
2885    int main (void)
2886    {
2887      (*pointer_to_call0a) (c, s, i, l);
2888    }
2889
2890    Instead of calling a stub library function  _call_via_xx (xx is
2891    the register name), GCC may inline the trampoline in the object
2892    file as below (register r2 has the address of call0a).
2893
2894    .global main
2895    .type main, %function
2896    ...
2897    bl .L1
2898    ...
2899    .size main, .-main
2900
2901    .L1:
2902    bx r2
2903
2904    The trampoline 'bx r2' doesn't belong to main.  */
2905
2906 static CORE_ADDR
2907 arm_skip_bx_reg (struct frame_info *frame, CORE_ADDR pc)
2908 {
2909   /* The heuristics of recognizing such trampoline is that FRAME is
2910      executing in Thumb mode and the instruction on PC is 'bx Rm'.  */
2911   if (arm_frame_is_thumb (frame))
2912     {
2913       gdb_byte buf[2];
2914
2915       if (target_read_memory (pc, buf, 2) == 0)
2916         {
2917           struct gdbarch *gdbarch = get_frame_arch (frame);
2918           enum bfd_endian byte_order_for_code
2919             = gdbarch_byte_order_for_code (gdbarch);
2920           uint16_t insn
2921             = extract_unsigned_integer (buf, 2, byte_order_for_code);
2922
2923           if ((insn & 0xff80) == 0x4700)  /* bx <Rm> */
2924             {
2925               CORE_ADDR dest
2926                 = get_frame_register_unsigned (frame, bits (insn, 3, 6));
2927
2928               /* Clear the LSB so that gdb core sets step-resume
2929                  breakpoint at the right address.  */
2930               return UNMAKE_THUMB_ADDR (dest);
2931             }
2932         }
2933     }
2934
2935   return 0;
2936 }
2937
2938 static struct arm_prologue_cache *
2939 arm_make_stub_cache (struct frame_info *this_frame)
2940 {
2941   struct arm_prologue_cache *cache;
2942
2943   cache = FRAME_OBSTACK_ZALLOC (struct arm_prologue_cache);
2944   cache->saved_regs = trad_frame_alloc_saved_regs (this_frame);
2945
2946   cache->prev_sp = get_frame_register_unsigned (this_frame, ARM_SP_REGNUM);
2947
2948   return cache;
2949 }
2950
2951 /* Our frame ID for a stub frame is the current SP and LR.  */
2952
2953 static void
2954 arm_stub_this_id (struct frame_info *this_frame,
2955                   void **this_cache,
2956                   struct frame_id *this_id)
2957 {
2958   struct arm_prologue_cache *cache;
2959
2960   if (*this_cache == NULL)
2961     *this_cache = arm_make_stub_cache (this_frame);
2962   cache = *this_cache;
2963
2964   *this_id = frame_id_build (cache->prev_sp, get_frame_pc (this_frame));
2965 }
2966
2967 static int
2968 arm_stub_unwind_sniffer (const struct frame_unwind *self,
2969                          struct frame_info *this_frame,
2970                          void **this_prologue_cache)
2971 {
2972   CORE_ADDR addr_in_block;
2973   gdb_byte dummy[4];
2974   CORE_ADDR pc, start_addr;
2975   const char *name;
2976
2977   addr_in_block = get_frame_address_in_block (this_frame);
2978   pc = get_frame_pc (this_frame);
2979   if (in_plt_section (addr_in_block)
2980       /* We also use the stub winder if the target memory is unreadable
2981          to avoid having the prologue unwinder trying to read it.  */
2982       || target_read_memory (pc, dummy, 4) != 0)
2983     return 1;
2984
2985   if (find_pc_partial_function (pc, &name, &start_addr, NULL) == 0
2986       && arm_skip_bx_reg (this_frame, pc) != 0)
2987     return 1;
2988
2989   return 0;
2990 }
2991
2992 struct frame_unwind arm_stub_unwind = {
2993   NORMAL_FRAME,
2994   default_frame_unwind_stop_reason,
2995   arm_stub_this_id,
2996   arm_prologue_prev_register,
2997   NULL,
2998   arm_stub_unwind_sniffer
2999 };
3000
3001 /* Put here the code to store, into CACHE->saved_regs, the addresses
3002    of the saved registers of frame described by THIS_FRAME.  CACHE is
3003    returned.  */
3004
3005 static struct arm_prologue_cache *
3006 arm_m_exception_cache (struct frame_info *this_frame)
3007 {
3008   struct gdbarch *gdbarch = get_frame_arch (this_frame);
3009   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
3010   struct arm_prologue_cache *cache;
3011   CORE_ADDR unwound_sp;
3012   LONGEST xpsr;
3013
3014   cache = FRAME_OBSTACK_ZALLOC (struct arm_prologue_cache);
3015   cache->saved_regs = trad_frame_alloc_saved_regs (this_frame);
3016
3017   unwound_sp = get_frame_register_unsigned (this_frame,
3018                                             ARM_SP_REGNUM);
3019
3020   /* The hardware saves eight 32-bit words, comprising xPSR,
3021      ReturnAddress, LR (R14), R12, R3, R2, R1, R0.  See details in
3022      "B1.5.6 Exception entry behavior" in
3023      "ARMv7-M Architecture Reference Manual".  */
3024   cache->saved_regs[0].addr = unwound_sp;
3025   cache->saved_regs[1].addr = unwound_sp + 4;
3026   cache->saved_regs[2].addr = unwound_sp + 8;
3027   cache->saved_regs[3].addr = unwound_sp + 12;
3028   cache->saved_regs[12].addr = unwound_sp + 16;
3029   cache->saved_regs[14].addr = unwound_sp + 20;
3030   cache->saved_regs[15].addr = unwound_sp + 24;
3031   cache->saved_regs[ARM_PS_REGNUM].addr = unwound_sp + 28;
3032
3033   /* If bit 9 of the saved xPSR is set, then there is a four-byte
3034      aligner between the top of the 32-byte stack frame and the
3035      previous context's stack pointer.  */
3036   cache->prev_sp = unwound_sp + 32;
3037   if (safe_read_memory_integer (unwound_sp + 28, 4, byte_order, &xpsr)
3038       && (xpsr & (1 << 9)) != 0)
3039     cache->prev_sp += 4;
3040
3041   return cache;
3042 }
3043
3044 /* Implementation of function hook 'this_id' in
3045    'struct frame_uwnind'.  */
3046
3047 static void
3048 arm_m_exception_this_id (struct frame_info *this_frame,
3049                          void **this_cache,
3050                          struct frame_id *this_id)
3051 {
3052   struct arm_prologue_cache *cache;
3053
3054   if (*this_cache == NULL)
3055     *this_cache = arm_m_exception_cache (this_frame);
3056   cache = *this_cache;
3057
3058   /* Our frame ID for a stub frame is the current SP and LR.  */
3059   *this_id = frame_id_build (cache->prev_sp,
3060                              get_frame_pc (this_frame));
3061 }
3062
3063 /* Implementation of function hook 'prev_register' in
3064    'struct frame_uwnind'.  */
3065
3066 static struct value *
3067 arm_m_exception_prev_register (struct frame_info *this_frame,
3068                                void **this_cache,
3069                                int prev_regnum)
3070 {
3071   struct gdbarch *gdbarch = get_frame_arch (this_frame);
3072   struct arm_prologue_cache *cache;
3073
3074   if (*this_cache == NULL)
3075     *this_cache = arm_m_exception_cache (this_frame);
3076   cache = *this_cache;
3077
3078   /* The value was already reconstructed into PREV_SP.  */
3079   if (prev_regnum == ARM_SP_REGNUM)
3080     return frame_unwind_got_constant (this_frame, prev_regnum,
3081                                       cache->prev_sp);
3082
3083   return trad_frame_get_prev_register (this_frame, cache->saved_regs,
3084                                        prev_regnum);
3085 }
3086
3087 /* Implementation of function hook 'sniffer' in
3088    'struct frame_uwnind'.  */
3089
3090 static int
3091 arm_m_exception_unwind_sniffer (const struct frame_unwind *self,
3092                                 struct frame_info *this_frame,
3093                                 void **this_prologue_cache)
3094 {
3095   CORE_ADDR this_pc = get_frame_pc (this_frame);
3096
3097   /* No need to check is_m; this sniffer is only registered for
3098      M-profile architectures.  */
3099
3100   /* Exception frames return to one of these magic PCs.  Other values
3101      are not defined as of v7-M.  See details in "B1.5.8 Exception
3102      return behavior" in "ARMv7-M Architecture Reference Manual".  */
3103   if (this_pc == 0xfffffff1 || this_pc == 0xfffffff9
3104       || this_pc == 0xfffffffd)
3105     return 1;
3106
3107   return 0;
3108 }
3109
3110 /* Frame unwinder for M-profile exceptions.  */
3111
3112 struct frame_unwind arm_m_exception_unwind =
3113 {
3114   SIGTRAMP_FRAME,
3115   default_frame_unwind_stop_reason,
3116   arm_m_exception_this_id,
3117   arm_m_exception_prev_register,
3118   NULL,
3119   arm_m_exception_unwind_sniffer
3120 };
3121
3122 static CORE_ADDR
3123 arm_normal_frame_base (struct frame_info *this_frame, void **this_cache)
3124 {
3125   struct arm_prologue_cache *cache;
3126
3127   if (*this_cache == NULL)
3128     *this_cache = arm_make_prologue_cache (this_frame);
3129   cache = *this_cache;
3130
3131   return cache->prev_sp - cache->framesize;
3132 }
3133
3134 struct frame_base arm_normal_base = {
3135   &arm_prologue_unwind,
3136   arm_normal_frame_base,
3137   arm_normal_frame_base,
3138   arm_normal_frame_base
3139 };
3140
3141 /* Assuming THIS_FRAME is a dummy, return the frame ID of that
3142    dummy frame.  The frame ID's base needs to match the TOS value
3143    saved by save_dummy_frame_tos() and returned from
3144    arm_push_dummy_call, and the PC needs to match the dummy frame's
3145    breakpoint.  */
3146
3147 static struct frame_id
3148 arm_dummy_id (struct gdbarch *gdbarch, struct frame_info *this_frame)
3149 {
3150   return frame_id_build (get_frame_register_unsigned (this_frame,
3151                                                       ARM_SP_REGNUM),
3152                          get_frame_pc (this_frame));
3153 }
3154
3155 /* Given THIS_FRAME, find the previous frame's resume PC (which will
3156    be used to construct the previous frame's ID, after looking up the
3157    containing function).  */
3158
3159 static CORE_ADDR
3160 arm_unwind_pc (struct gdbarch *gdbarch, struct frame_info *this_frame)
3161 {
3162   CORE_ADDR pc;
3163   pc = frame_unwind_register_unsigned (this_frame, ARM_PC_REGNUM);
3164   return arm_addr_bits_remove (gdbarch, pc);
3165 }
3166
3167 static CORE_ADDR
3168 arm_unwind_sp (struct gdbarch *gdbarch, struct frame_info *this_frame)
3169 {
3170   return frame_unwind_register_unsigned (this_frame, ARM_SP_REGNUM);
3171 }
3172
3173 static struct value *
3174 arm_dwarf2_prev_register (struct frame_info *this_frame, void **this_cache,
3175                           int regnum)
3176 {
3177   struct gdbarch * gdbarch = get_frame_arch (this_frame);
3178   CORE_ADDR lr, cpsr;
3179   ULONGEST t_bit = arm_psr_thumb_bit (gdbarch);
3180
3181   switch (regnum)
3182     {
3183     case ARM_PC_REGNUM:
3184       /* The PC is normally copied from the return column, which
3185          describes saves of LR.  However, that version may have an
3186          extra bit set to indicate Thumb state.  The bit is not
3187          part of the PC.  */
3188       lr = frame_unwind_register_unsigned (this_frame, ARM_LR_REGNUM);
3189       return frame_unwind_got_constant (this_frame, regnum,
3190                                         arm_addr_bits_remove (gdbarch, lr));
3191
3192     case ARM_PS_REGNUM:
3193       /* Reconstruct the T bit; see arm_prologue_prev_register for details.  */
3194       cpsr = get_frame_register_unsigned (this_frame, regnum);
3195       lr = frame_unwind_register_unsigned (this_frame, ARM_LR_REGNUM);
3196       if (IS_THUMB_ADDR (lr))
3197         cpsr |= t_bit;
3198       else
3199         cpsr &= ~t_bit;
3200       return frame_unwind_got_constant (this_frame, regnum, cpsr);
3201
3202     default:
3203       internal_error (__FILE__, __LINE__,
3204                       _("Unexpected register %d"), regnum);
3205     }
3206 }
3207
3208 static void
3209 arm_dwarf2_frame_init_reg (struct gdbarch *gdbarch, int regnum,
3210                            struct dwarf2_frame_state_reg *reg,
3211                            struct frame_info *this_frame)
3212 {
3213   switch (regnum)
3214     {
3215     case ARM_PC_REGNUM:
3216     case ARM_PS_REGNUM:
3217       reg->how = DWARF2_FRAME_REG_FN;
3218       reg->loc.fn = arm_dwarf2_prev_register;
3219       break;
3220     case ARM_SP_REGNUM:
3221       reg->how = DWARF2_FRAME_REG_CFA;
3222       break;
3223     }
3224 }
3225
3226 /* Implement the stack_frame_destroyed_p gdbarch method.  */
3227
3228 static int
3229 thumb_stack_frame_destroyed_p (struct gdbarch *gdbarch, CORE_ADDR pc)
3230 {
3231   enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
3232   unsigned int insn, insn2;
3233   int found_return = 0, found_stack_adjust = 0;
3234   CORE_ADDR func_start, func_end;
3235   CORE_ADDR scan_pc;
3236   gdb_byte buf[4];
3237
3238   if (!find_pc_partial_function (pc, NULL, &func_start, &func_end))
3239     return 0;
3240
3241   /* The epilogue is a sequence of instructions along the following lines:
3242
3243     - add stack frame size to SP or FP
3244     - [if frame pointer used] restore SP from FP
3245     - restore registers from SP [may include PC]
3246     - a return-type instruction [if PC wasn't already restored]
3247
3248     In a first pass, we scan forward from the current PC and verify the
3249     instructions we find as compatible with this sequence, ending in a
3250     return instruction.
3251
3252     However, this is not sufficient to distinguish indirect function calls
3253     within a function from indirect tail calls in the epilogue in some cases.
3254     Therefore, if we didn't already find any SP-changing instruction during
3255     forward scan, we add a backward scanning heuristic to ensure we actually
3256     are in the epilogue.  */
3257
3258   scan_pc = pc;
3259   while (scan_pc < func_end && !found_return)
3260     {
3261       if (target_read_memory (scan_pc, buf, 2))
3262         break;
3263
3264       scan_pc += 2;
3265       insn = extract_unsigned_integer (buf, 2, byte_order_for_code);
3266
3267       if ((insn & 0xff80) == 0x4700)  /* bx <Rm> */
3268         found_return = 1;
3269       else if (insn == 0x46f7)  /* mov pc, lr */
3270         found_return = 1;
3271       else if (thumb_instruction_restores_sp (insn))
3272         {
3273           if ((insn & 0xff00) == 0xbd00)  /* pop <registers, PC> */
3274             found_return = 1;
3275         }
3276       else if (thumb_insn_size (insn) == 4)  /* 32-bit Thumb-2 instruction */
3277         {
3278           if (target_read_memory (scan_pc, buf, 2))
3279             break;
3280
3281           scan_pc += 2;
3282           insn2 = extract_unsigned_integer (buf, 2, byte_order_for_code);
3283
3284           if (insn == 0xe8bd)  /* ldm.w sp!, <registers> */
3285             {
3286               if (insn2 & 0x8000)  /* <registers> include PC.  */
3287                 found_return = 1;
3288             }
3289           else if (insn == 0xf85d  /* ldr.w <Rt>, [sp], #4 */
3290                    && (insn2 & 0x0fff) == 0x0b04)
3291             {
3292               if ((insn2 & 0xf000) == 0xf000) /* <Rt> is PC.  */
3293                 found_return = 1;
3294             }
3295           else if ((insn & 0xffbf) == 0xecbd  /* vldm sp!, <list> */
3296                    && (insn2 & 0x0e00) == 0x0a00)
3297             ;
3298           else
3299             break;
3300         }
3301       else
3302         break;
3303     }
3304
3305   if (!found_return)
3306     return 0;
3307
3308   /* Since any instruction in the epilogue sequence, with the possible
3309      exception of return itself, updates the stack pointer, we need to
3310      scan backwards for at most one instruction.  Try either a 16-bit or
3311      a 32-bit instruction.  This is just a heuristic, so we do not worry
3312      too much about false positives.  */
3313
3314   if (pc - 4 < func_start)
3315     return 0;
3316   if (target_read_memory (pc - 4, buf, 4))
3317     return 0;
3318
3319   insn = extract_unsigned_integer (buf, 2, byte_order_for_code);
3320   insn2 = extract_unsigned_integer (buf + 2, 2, byte_order_for_code);
3321
3322   if (thumb_instruction_restores_sp (insn2))
3323     found_stack_adjust = 1;
3324   else if (insn == 0xe8bd)  /* ldm.w sp!, <registers> */
3325     found_stack_adjust = 1;
3326   else if (insn == 0xf85d  /* ldr.w <Rt>, [sp], #4 */
3327            && (insn2 & 0x0fff) == 0x0b04)
3328     found_stack_adjust = 1;
3329   else if ((insn & 0xffbf) == 0xecbd  /* vldm sp!, <list> */
3330            && (insn2 & 0x0e00) == 0x0a00)
3331     found_stack_adjust = 1;
3332
3333   return found_stack_adjust;
3334 }
3335
3336 /* Implement the stack_frame_destroyed_p gdbarch method.  */
3337
3338 static int
3339 arm_stack_frame_destroyed_p (struct gdbarch *gdbarch, CORE_ADDR pc)
3340 {
3341   enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
3342   unsigned int insn;
3343   int found_return;
3344   CORE_ADDR func_start, func_end;
3345
3346   if (arm_pc_is_thumb (gdbarch, pc))
3347     return thumb_stack_frame_destroyed_p (gdbarch, pc);
3348
3349   if (!find_pc_partial_function (pc, NULL, &func_start, &func_end))
3350     return 0;
3351
3352   /* We are in the epilogue if the previous instruction was a stack
3353      adjustment and the next instruction is a possible return (bx, mov
3354      pc, or pop).  We could have to scan backwards to find the stack
3355      adjustment, or forwards to find the return, but this is a decent
3356      approximation.  First scan forwards.  */
3357
3358   found_return = 0;
3359   insn = read_memory_unsigned_integer (pc, 4, byte_order_for_code);
3360   if (bits (insn, 28, 31) != INST_NV)
3361     {
3362       if ((insn & 0x0ffffff0) == 0x012fff10)
3363         /* BX.  */
3364         found_return = 1;
3365       else if ((insn & 0x0ffffff0) == 0x01a0f000)
3366         /* MOV PC.  */
3367         found_return = 1;
3368       else if ((insn & 0x0fff0000) == 0x08bd0000
3369           && (insn & 0x0000c000) != 0)
3370         /* POP (LDMIA), including PC or LR.  */
3371         found_return = 1;
3372     }
3373
3374   if (!found_return)
3375     return 0;
3376
3377   /* Scan backwards.  This is just a heuristic, so do not worry about
3378      false positives from mode changes.  */
3379
3380   if (pc < func_start + 4)
3381     return 0;
3382
3383   insn = read_memory_unsigned_integer (pc - 4, 4, byte_order_for_code);
3384   if (arm_instruction_restores_sp (insn))
3385     return 1;
3386
3387   return 0;
3388 }
3389
3390
3391 /* When arguments must be pushed onto the stack, they go on in reverse
3392    order.  The code below implements a FILO (stack) to do this.  */
3393
3394 struct stack_item
3395 {
3396   int len;
3397   struct stack_item *prev;
3398   void *data;
3399 };
3400
3401 static struct stack_item *
3402 push_stack_item (struct stack_item *prev, const void *contents, int len)
3403 {
3404   struct stack_item *si;
3405   si = xmalloc (sizeof (struct stack_item));
3406   si->data = xmalloc (len);
3407   si->len = len;
3408   si->prev = prev;
3409   memcpy (si->data, contents, len);
3410   return si;
3411 }
3412
3413 static struct stack_item *
3414 pop_stack_item (struct stack_item *si)
3415 {
3416   struct stack_item *dead = si;
3417   si = si->prev;
3418   xfree (dead->data);
3419   xfree (dead);
3420   return si;
3421 }
3422
3423
3424 /* Return the alignment (in bytes) of the given type.  */
3425
3426 static int
3427 arm_type_align (struct type *t)
3428 {
3429   int n;
3430   int align;
3431   int falign;
3432
3433   t = check_typedef (t);
3434   switch (TYPE_CODE (t))
3435     {
3436     default:
3437       /* Should never happen.  */
3438       internal_error (__FILE__, __LINE__, _("unknown type alignment"));
3439       return 4;
3440
3441     case TYPE_CODE_PTR:
3442     case TYPE_CODE_ENUM:
3443     case TYPE_CODE_INT:
3444     case TYPE_CODE_FLT:
3445     case TYPE_CODE_SET:
3446     case TYPE_CODE_RANGE:
3447     case TYPE_CODE_REF:
3448     case TYPE_CODE_CHAR:
3449     case TYPE_CODE_BOOL:
3450       return TYPE_LENGTH (t);
3451
3452     case TYPE_CODE_ARRAY:
3453     case TYPE_CODE_COMPLEX:
3454       /* TODO: What about vector types?  */
3455       return arm_type_align (TYPE_TARGET_TYPE (t));
3456
3457     case TYPE_CODE_STRUCT:
3458     case TYPE_CODE_UNION:
3459       align = 1;
3460       for (n = 0; n < TYPE_NFIELDS (t); n++)
3461         {
3462           falign = arm_type_align (TYPE_FIELD_TYPE (t, n));
3463           if (falign > align)
3464             align = falign;
3465         }
3466       return align;
3467     }
3468 }
3469
3470 /* Possible base types for a candidate for passing and returning in
3471    VFP registers.  */
3472
3473 enum arm_vfp_cprc_base_type
3474 {
3475   VFP_CPRC_UNKNOWN,
3476   VFP_CPRC_SINGLE,
3477   VFP_CPRC_DOUBLE,
3478   VFP_CPRC_VEC64,
3479   VFP_CPRC_VEC128
3480 };
3481
3482 /* The length of one element of base type B.  */
3483
3484 static unsigned
3485 arm_vfp_cprc_unit_length (enum arm_vfp_cprc_base_type b)
3486 {
3487   switch (b)
3488     {
3489     case VFP_CPRC_SINGLE:
3490       return 4;
3491     case VFP_CPRC_DOUBLE:
3492       return 8;
3493     case VFP_CPRC_VEC64:
3494       return 8;
3495     case VFP_CPRC_VEC128:
3496       return 16;
3497     default:
3498       internal_error (__FILE__, __LINE__, _("Invalid VFP CPRC type: %d."),
3499                       (int) b);
3500     }
3501 }
3502
3503 /* The character ('s', 'd' or 'q') for the type of VFP register used
3504    for passing base type B.  */
3505
3506 static int
3507 arm_vfp_cprc_reg_char (enum arm_vfp_cprc_base_type b)
3508 {
3509   switch (b)
3510     {
3511     case VFP_CPRC_SINGLE:
3512       return 's';
3513     case VFP_CPRC_DOUBLE:
3514       return 'd';
3515     case VFP_CPRC_VEC64:
3516       return 'd';
3517     case VFP_CPRC_VEC128:
3518       return 'q';
3519     default:
3520       internal_error (__FILE__, __LINE__, _("Invalid VFP CPRC type: %d."),
3521                       (int) b);
3522     }
3523 }
3524
3525 /* Determine whether T may be part of a candidate for passing and
3526    returning in VFP registers, ignoring the limit on the total number
3527    of components.  If *BASE_TYPE is VFP_CPRC_UNKNOWN, set it to the
3528    classification of the first valid component found; if it is not
3529    VFP_CPRC_UNKNOWN, all components must have the same classification
3530    as *BASE_TYPE.  If it is found that T contains a type not permitted
3531    for passing and returning in VFP registers, a type differently
3532    classified from *BASE_TYPE, or two types differently classified
3533    from each other, return -1, otherwise return the total number of
3534    base-type elements found (possibly 0 in an empty structure or
3535    array).  Vector types are not currently supported, matching the
3536    generic AAPCS support.  */
3537
3538 static int
3539 arm_vfp_cprc_sub_candidate (struct type *t,
3540                             enum arm_vfp_cprc_base_type *base_type)
3541 {
3542   t = check_typedef (t);
3543   switch (TYPE_CODE (t))
3544     {
3545     case TYPE_CODE_FLT:
3546       switch (TYPE_LENGTH (t))
3547         {
3548         case 4:
3549           if (*base_type == VFP_CPRC_UNKNOWN)
3550             *base_type = VFP_CPRC_SINGLE;
3551           else if (*base_type != VFP_CPRC_SINGLE)
3552             return -1;
3553           return 1;
3554
3555         case 8:
3556           if (*base_type == VFP_CPRC_UNKNOWN)
3557             *base_type = VFP_CPRC_DOUBLE;
3558           else if (*base_type != VFP_CPRC_DOUBLE)
3559             return -1;
3560           return 1;
3561
3562         default:
3563           return -1;
3564         }
3565       break;
3566
3567     case TYPE_CODE_COMPLEX:
3568       /* Arguments of complex T where T is one of the types float or
3569          double get treated as if they are implemented as:
3570
3571          struct complexT
3572          {
3573            T real;
3574            T imag;
3575          };
3576
3577       */
3578       switch (TYPE_LENGTH (t))
3579         {
3580         case 8:
3581           if (*base_type == VFP_CPRC_UNKNOWN)
3582             *base_type = VFP_CPRC_SINGLE;
3583           else if (*base_type != VFP_CPRC_SINGLE)
3584             return -1;
3585           return 2;
3586
3587         case 16:
3588           if (*base_type == VFP_CPRC_UNKNOWN)
3589             *base_type = VFP_CPRC_DOUBLE;
3590           else if (*base_type != VFP_CPRC_DOUBLE)
3591             return -1;
3592           return 2;
3593
3594         default:
3595           return -1;
3596         }
3597       break;
3598
3599     case TYPE_CODE_ARRAY:
3600       {
3601         int count;
3602         unsigned unitlen;
3603         count = arm_vfp_cprc_sub_candidate (TYPE_TARGET_TYPE (t), base_type);
3604         if (count == -1)
3605           return -1;
3606         if (TYPE_LENGTH (t) == 0)
3607           {
3608             gdb_assert (count == 0);
3609             return 0;
3610           }
3611         else if (count == 0)
3612           return -1;
3613         unitlen = arm_vfp_cprc_unit_length (*base_type);
3614         gdb_assert ((TYPE_LENGTH (t) % unitlen) == 0);
3615         return TYPE_LENGTH (t) / unitlen;
3616       }
3617       break;
3618
3619     case TYPE_CODE_STRUCT:
3620       {
3621         int count = 0;
3622         unsigned unitlen;
3623         int i;
3624         for (i = 0; i < TYPE_NFIELDS (t); i++)
3625           {
3626             int sub_count = arm_vfp_cprc_sub_candidate (TYPE_FIELD_TYPE (t, i),
3627                                                         base_type);
3628             if (sub_count == -1)
3629               return -1;
3630             count += sub_count;
3631           }
3632         if (TYPE_LENGTH (t) == 0)
3633           {
3634             gdb_assert (count == 0);
3635             return 0;
3636           }
3637         else if (count == 0)
3638           return -1;
3639         unitlen = arm_vfp_cprc_unit_length (*base_type);
3640         if (TYPE_LENGTH (t) != unitlen * count)
3641           return -1;
3642         return count;
3643       }
3644
3645     case TYPE_CODE_UNION:
3646       {
3647         int count = 0;
3648         unsigned unitlen;
3649         int i;
3650         for (i = 0; i < TYPE_NFIELDS (t); i++)
3651           {
3652             int sub_count = arm_vfp_cprc_sub_candidate (TYPE_FIELD_TYPE (t, i),
3653                                                         base_type);
3654             if (sub_count == -1)
3655               return -1;
3656             count = (count > sub_count ? count : sub_count);
3657           }
3658         if (TYPE_LENGTH (t) == 0)
3659           {
3660             gdb_assert (count == 0);
3661             return 0;
3662           }
3663         else if (count == 0)
3664           return -1;
3665         unitlen = arm_vfp_cprc_unit_length (*base_type);
3666         if (TYPE_LENGTH (t) != unitlen * count)
3667           return -1;
3668         return count;
3669       }
3670
3671     default:
3672       break;
3673     }
3674
3675   return -1;
3676 }
3677
3678 /* Determine whether T is a VFP co-processor register candidate (CPRC)
3679    if passed to or returned from a non-variadic function with the VFP
3680    ABI in effect.  Return 1 if it is, 0 otherwise.  If it is, set
3681    *BASE_TYPE to the base type for T and *COUNT to the number of
3682    elements of that base type before returning.  */
3683
3684 static int
3685 arm_vfp_call_candidate (struct type *t, enum arm_vfp_cprc_base_type *base_type,
3686                         int *count)
3687 {
3688   enum arm_vfp_cprc_base_type b = VFP_CPRC_UNKNOWN;
3689   int c = arm_vfp_cprc_sub_candidate (t, &b);
3690   if (c <= 0 || c > 4)
3691     return 0;
3692   *base_type = b;
3693   *count = c;
3694   return 1;
3695 }
3696
3697 /* Return 1 if the VFP ABI should be used for passing arguments to and
3698    returning values from a function of type FUNC_TYPE, 0
3699    otherwise.  */
3700
3701 static int
3702 arm_vfp_abi_for_function (struct gdbarch *gdbarch, struct type *func_type)
3703 {
3704   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
3705   /* Variadic functions always use the base ABI.  Assume that functions
3706      without debug info are not variadic.  */
3707   if (func_type && TYPE_VARARGS (check_typedef (func_type)))
3708     return 0;
3709   /* The VFP ABI is only supported as a variant of AAPCS.  */
3710   if (tdep->arm_abi != ARM_ABI_AAPCS)
3711     return 0;
3712   return gdbarch_tdep (gdbarch)->fp_model == ARM_FLOAT_VFP;
3713 }
3714
3715 /* We currently only support passing parameters in integer registers, which
3716    conforms with GCC's default model, and VFP argument passing following
3717    the VFP variant of AAPCS.  Several other variants exist and
3718    we should probably support some of them based on the selected ABI.  */
3719
3720 static CORE_ADDR
3721 arm_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
3722                      struct regcache *regcache, CORE_ADDR bp_addr, int nargs,
3723                      struct value **args, CORE_ADDR sp, int struct_return,
3724                      CORE_ADDR struct_addr)
3725 {
3726   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
3727   int argnum;
3728   int argreg;
3729   int nstack;
3730   struct stack_item *si = NULL;
3731   int use_vfp_abi;
3732   struct type *ftype;
3733   unsigned vfp_regs_free = (1 << 16) - 1;
3734
3735   /* Determine the type of this function and whether the VFP ABI
3736      applies.  */
3737   ftype = check_typedef (value_type (function));
3738   if (TYPE_CODE (ftype) == TYPE_CODE_PTR)
3739     ftype = check_typedef (TYPE_TARGET_TYPE (ftype));
3740   use_vfp_abi = arm_vfp_abi_for_function (gdbarch, ftype);
3741
3742   /* Set the return address.  For the ARM, the return breakpoint is
3743      always at BP_ADDR.  */
3744   if (arm_pc_is_thumb (gdbarch, bp_addr))
3745     bp_addr |= 1;
3746   regcache_cooked_write_unsigned (regcache, ARM_LR_REGNUM, bp_addr);
3747
3748   /* Walk through the list of args and determine how large a temporary
3749      stack is required.  Need to take care here as structs may be
3750      passed on the stack, and we have to push them.  */
3751   nstack = 0;
3752
3753   argreg = ARM_A1_REGNUM;
3754   nstack = 0;
3755
3756   /* The struct_return pointer occupies the first parameter
3757      passing register.  */
3758   if (struct_return)
3759     {
3760       if (arm_debug)
3761         fprintf_unfiltered (gdb_stdlog, "struct return in %s = %s\n",
3762                             gdbarch_register_name (gdbarch, argreg),
3763                             paddress (gdbarch, struct_addr));
3764       regcache_cooked_write_unsigned (regcache, argreg, struct_addr);
3765       argreg++;
3766     }
3767
3768   for (argnum = 0; argnum < nargs; argnum++)
3769     {
3770       int len;
3771       struct type *arg_type;
3772       struct type *target_type;
3773       enum type_code typecode;
3774       const bfd_byte *val;
3775       int align;
3776       enum arm_vfp_cprc_base_type vfp_base_type;
3777       int vfp_base_count;
3778       int may_use_core_reg = 1;
3779
3780       arg_type = check_typedef (value_type (args[argnum]));
3781       len = TYPE_LENGTH (arg_type);
3782       target_type = TYPE_TARGET_TYPE (arg_type);
3783       typecode = TYPE_CODE (arg_type);
3784       val = value_contents (args[argnum]);
3785
3786       align = arm_type_align (arg_type);
3787       /* Round alignment up to a whole number of words.  */
3788       align = (align + INT_REGISTER_SIZE - 1) & ~(INT_REGISTER_SIZE - 1);
3789       /* Different ABIs have different maximum alignments.  */
3790       if (gdbarch_tdep (gdbarch)->arm_abi == ARM_ABI_APCS)
3791         {
3792           /* The APCS ABI only requires word alignment.  */
3793           align = INT_REGISTER_SIZE;
3794         }
3795       else
3796         {
3797           /* The AAPCS requires at most doubleword alignment.  */
3798           if (align > INT_REGISTER_SIZE * 2)
3799             align = INT_REGISTER_SIZE * 2;
3800         }
3801
3802       if (use_vfp_abi
3803           && arm_vfp_call_candidate (arg_type, &vfp_base_type,
3804                                      &vfp_base_count))
3805         {
3806           int regno;
3807           int unit_length;
3808           int shift;
3809           unsigned mask;
3810
3811           /* Because this is a CPRC it cannot go in a core register or
3812              cause a core register to be skipped for alignment.
3813              Either it goes in VFP registers and the rest of this loop
3814              iteration is skipped for this argument, or it goes on the
3815              stack (and the stack alignment code is correct for this
3816              case).  */
3817           may_use_core_reg = 0;
3818
3819           unit_length = arm_vfp_cprc_unit_length (vfp_base_type);
3820           shift = unit_length / 4;
3821           mask = (1 << (shift * vfp_base_count)) - 1;
3822           for (regno = 0; regno < 16; regno += shift)
3823             if (((vfp_regs_free >> regno) & mask) == mask)
3824               break;
3825
3826           if (regno < 16)
3827             {
3828               int reg_char;
3829               int reg_scaled;
3830               int i;
3831
3832               vfp_regs_free &= ~(mask << regno);
3833               reg_scaled = regno / shift;
3834               reg_char = arm_vfp_cprc_reg_char (vfp_base_type);
3835               for (i = 0; i < vfp_base_count; i++)
3836                 {
3837                   char name_buf[4];
3838                   int regnum;
3839                   if (reg_char == 'q')
3840                     arm_neon_quad_write (gdbarch, regcache, reg_scaled + i,
3841                                          val + i * unit_length);
3842                   else
3843                     {
3844                       xsnprintf (name_buf, sizeof (name_buf), "%c%d",
3845                                  reg_char, reg_scaled + i);
3846                       regnum = user_reg_map_name_to_regnum (gdbarch, name_buf,
3847                                                             strlen (name_buf));
3848                       regcache_cooked_write (regcache, regnum,
3849                                              val + i * unit_length);
3850                     }
3851                 }
3852               continue;
3853             }
3854           else
3855             {
3856               /* This CPRC could not go in VFP registers, so all VFP
3857                  registers are now marked as used.  */
3858               vfp_regs_free = 0;
3859             }
3860         }
3861
3862       /* Push stack padding for dowubleword alignment.  */
3863       if (nstack & (align - 1))
3864         {
3865           si = push_stack_item (si, val, INT_REGISTER_SIZE);
3866           nstack += INT_REGISTER_SIZE;
3867         }
3868       
3869       /* Doubleword aligned quantities must go in even register pairs.  */
3870       if (may_use_core_reg
3871           && argreg <= ARM_LAST_ARG_REGNUM
3872           && align > INT_REGISTER_SIZE
3873           && argreg & 1)
3874         argreg++;
3875
3876       /* If the argument is a pointer to a function, and it is a
3877          Thumb function, create a LOCAL copy of the value and set
3878          the THUMB bit in it.  */
3879       if (TYPE_CODE_PTR == typecode
3880           && target_type != NULL
3881           && TYPE_CODE_FUNC == TYPE_CODE (check_typedef (target_type)))
3882         {
3883           CORE_ADDR regval = extract_unsigned_integer (val, len, byte_order);
3884           if (arm_pc_is_thumb (gdbarch, regval))
3885             {
3886               bfd_byte *copy = alloca (len);
3887               store_unsigned_integer (copy, len, byte_order,
3888                                       MAKE_THUMB_ADDR (regval));
3889               val = copy;
3890             }
3891         }
3892
3893       /* Copy the argument to general registers or the stack in
3894          register-sized pieces.  Large arguments are split between
3895          registers and stack.  */
3896       while (len > 0)
3897         {
3898           int partial_len = len < INT_REGISTER_SIZE ? len : INT_REGISTER_SIZE;
3899
3900           if (may_use_core_reg && argreg <= ARM_LAST_ARG_REGNUM)
3901             {
3902               /* The argument is being passed in a general purpose
3903                  register.  */
3904               CORE_ADDR regval
3905                 = extract_unsigned_integer (val, partial_len, byte_order);
3906               if (byte_order == BFD_ENDIAN_BIG)
3907                 regval <<= (INT_REGISTER_SIZE - partial_len) * 8;
3908               if (arm_debug)
3909                 fprintf_unfiltered (gdb_stdlog, "arg %d in %s = 0x%s\n",
3910                                     argnum,
3911                                     gdbarch_register_name
3912                                       (gdbarch, argreg),
3913                                     phex (regval, INT_REGISTER_SIZE));
3914               regcache_cooked_write_unsigned (regcache, argreg, regval);
3915               argreg++;
3916             }
3917           else
3918             {
3919               /* Push the arguments onto the stack.  */
3920               if (arm_debug)
3921                 fprintf_unfiltered (gdb_stdlog, "arg %d @ sp + %d\n",
3922                                     argnum, nstack);
3923               si = push_stack_item (si, val, INT_REGISTER_SIZE);
3924               nstack += INT_REGISTER_SIZE;
3925             }
3926               
3927           len -= partial_len;
3928           val += partial_len;
3929         }
3930     }
3931   /* If we have an odd number of words to push, then decrement the stack
3932      by one word now, so first stack argument will be dword aligned.  */
3933   if (nstack & 4)
3934     sp -= 4;
3935
3936   while (si)
3937     {
3938       sp -= si->len;
3939       write_memory (sp, si->data, si->len);
3940       si = pop_stack_item (si);
3941     }
3942
3943   /* Finally, update teh SP register.  */
3944   regcache_cooked_write_unsigned (regcache, ARM_SP_REGNUM, sp);
3945
3946   return sp;
3947 }
3948
3949
3950 /* Always align the frame to an 8-byte boundary.  This is required on
3951    some platforms and harmless on the rest.  */
3952
3953 static CORE_ADDR
3954 arm_frame_align (struct gdbarch *gdbarch, CORE_ADDR sp)
3955 {
3956   /* Align the stack to eight bytes.  */
3957   return sp & ~ (CORE_ADDR) 7;
3958 }
3959
3960 static void
3961 print_fpu_flags (struct ui_file *file, int flags)
3962 {
3963   if (flags & (1 << 0))
3964     fputs_filtered ("IVO ", file);
3965   if (flags & (1 << 1))
3966     fputs_filtered ("DVZ ", file);
3967   if (flags & (1 << 2))
3968     fputs_filtered ("OFL ", file);
3969   if (flags & (1 << 3))
3970     fputs_filtered ("UFL ", file);
3971   if (flags & (1 << 4))
3972     fputs_filtered ("INX ", file);
3973   fputc_filtered ('\n', file);
3974 }
3975
3976 /* Print interesting information about the floating point processor
3977    (if present) or emulator.  */
3978 static void
3979 arm_print_float_info (struct gdbarch *gdbarch, struct ui_file *file,
3980                       struct frame_info *frame, const char *args)
3981 {
3982   unsigned long status = get_frame_register_unsigned (frame, ARM_FPS_REGNUM);
3983   int type;
3984
3985   type = (status >> 24) & 127;
3986   if (status & (1 << 31))
3987     fprintf_filtered (file, _("Hardware FPU type %d\n"), type);
3988   else
3989     fprintf_filtered (file, _("Software FPU type %d\n"), type);
3990   /* i18n: [floating point unit] mask */
3991   fputs_filtered (_("mask: "), file);
3992   print_fpu_flags (file, status >> 16);
3993   /* i18n: [floating point unit] flags */
3994   fputs_filtered (_("flags: "), file);
3995   print_fpu_flags (file, status);
3996 }
3997
3998 /* Construct the ARM extended floating point type.  */
3999 static struct type *
4000 arm_ext_type (struct gdbarch *gdbarch)
4001 {
4002   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
4003
4004   if (!tdep->arm_ext_type)
4005     tdep->arm_ext_type
4006       = arch_float_type (gdbarch, -1, "builtin_type_arm_ext",
4007                          floatformats_arm_ext);
4008
4009   return tdep->arm_ext_type;
4010 }
4011
4012 static struct type *
4013 arm_neon_double_type (struct gdbarch *gdbarch)
4014 {
4015   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
4016
4017   if (tdep->neon_double_type == NULL)
4018     {
4019       struct type *t, *elem;
4020
4021       t = arch_composite_type (gdbarch, "__gdb_builtin_type_neon_d",
4022                                TYPE_CODE_UNION);
4023       elem = builtin_type (gdbarch)->builtin_uint8;
4024       append_composite_type_field (t, "u8", init_vector_type (elem, 8));
4025       elem = builtin_type (gdbarch)->builtin_uint16;
4026       append_composite_type_field (t, "u16", init_vector_type (elem, 4));
4027       elem = builtin_type (gdbarch)->builtin_uint32;
4028       append_composite_type_field (t, "u32", init_vector_type (elem, 2));
4029       elem = builtin_type (gdbarch)->builtin_uint64;
4030       append_composite_type_field (t, "u64", elem);
4031       elem = builtin_type (gdbarch)->builtin_float;
4032       append_composite_type_field (t, "f32", init_vector_type (elem, 2));
4033       elem = builtin_type (gdbarch)->builtin_double;
4034       append_composite_type_field (t, "f64", elem);
4035
4036       TYPE_VECTOR (t) = 1;
4037       TYPE_NAME (t) = "neon_d";
4038       tdep->neon_double_type = t;
4039     }
4040
4041   return tdep->neon_double_type;
4042 }
4043
4044 /* FIXME: The vector types are not correctly ordered on big-endian
4045    targets.  Just as s0 is the low bits of d0, d0[0] is also the low
4046    bits of d0 - regardless of what unit size is being held in d0.  So
4047    the offset of the first uint8 in d0 is 7, but the offset of the
4048    first float is 4.  This code works as-is for little-endian
4049    targets.  */
4050
4051 static struct type *
4052 arm_neon_quad_type (struct gdbarch *gdbarch)
4053 {
4054   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
4055
4056   if (tdep->neon_quad_type == NULL)
4057     {
4058       struct type *t, *elem;
4059
4060       t = arch_composite_type (gdbarch, "__gdb_builtin_type_neon_q",
4061                                TYPE_CODE_UNION);
4062       elem = builtin_type (gdbarch)->builtin_uint8;
4063       append_composite_type_field (t, "u8", init_vector_type (elem, 16));
4064       elem = builtin_type (gdbarch)->builtin_uint16;
4065       append_composite_type_field (t, "u16", init_vector_type (elem, 8));
4066       elem = builtin_type (gdbarch)->builtin_uint32;
4067       append_composite_type_field (t, "u32", init_vector_type (elem, 4));
4068       elem = builtin_type (gdbarch)->builtin_uint64;
4069       append_composite_type_field (t, "u64", init_vector_type (elem, 2));
4070       elem = builtin_type (gdbarch)->builtin_float;
4071       append_composite_type_field (t, "f32", init_vector_type (elem, 4));
4072       elem = builtin_type (gdbarch)->builtin_double;
4073       append_composite_type_field (t, "f64", init_vector_type (elem, 2));
4074
4075       TYPE_VECTOR (t) = 1;
4076       TYPE_NAME (t) = "neon_q";
4077       tdep->neon_quad_type = t;
4078     }
4079
4080   return tdep->neon_quad_type;
4081 }
4082
4083 /* Return the GDB type object for the "standard" data type of data in
4084    register N.  */
4085
4086 static struct type *
4087 arm_register_type (struct gdbarch *gdbarch, int regnum)
4088 {
4089   int num_regs = gdbarch_num_regs (gdbarch);
4090
4091   if (gdbarch_tdep (gdbarch)->have_vfp_pseudos
4092       && regnum >= num_regs && regnum < num_regs + 32)
4093     return builtin_type (gdbarch)->builtin_float;
4094
4095   if (gdbarch_tdep (gdbarch)->have_neon_pseudos
4096       && regnum >= num_regs + 32 && regnum < num_regs + 32 + 16)
4097     return arm_neon_quad_type (gdbarch);
4098
4099   /* If the target description has register information, we are only
4100      in this function so that we can override the types of
4101      double-precision registers for NEON.  */
4102   if (tdesc_has_registers (gdbarch_target_desc (gdbarch)))
4103     {
4104       struct type *t = tdesc_register_type (gdbarch, regnum);
4105
4106       if (regnum >= ARM_D0_REGNUM && regnum < ARM_D0_REGNUM + 32
4107           && TYPE_CODE (t) == TYPE_CODE_FLT
4108           && gdbarch_tdep (gdbarch)->have_neon)
4109         return arm_neon_double_type (gdbarch);
4110       else
4111         return t;
4112     }
4113
4114   if (regnum >= ARM_F0_REGNUM && regnum < ARM_F0_REGNUM + NUM_FREGS)
4115     {
4116       if (!gdbarch_tdep (gdbarch)->have_fpa_registers)
4117         return builtin_type (gdbarch)->builtin_void;
4118
4119       return arm_ext_type (gdbarch);
4120     }
4121   else if (regnum == ARM_SP_REGNUM)
4122     return builtin_type (gdbarch)->builtin_data_ptr;
4123   else if (regnum == ARM_PC_REGNUM)
4124     return builtin_type (gdbarch)->builtin_func_ptr;
4125   else if (regnum >= ARRAY_SIZE (arm_register_names))
4126     /* These registers are only supported on targets which supply
4127        an XML description.  */
4128     return builtin_type (gdbarch)->builtin_int0;
4129   else
4130     return builtin_type (gdbarch)->builtin_uint32;
4131 }
4132
4133 /* Map a DWARF register REGNUM onto the appropriate GDB register
4134    number.  */
4135
4136 static int
4137 arm_dwarf_reg_to_regnum (struct gdbarch *gdbarch, int reg)
4138 {
4139   /* Core integer regs.  */
4140   if (reg >= 0 && reg <= 15)
4141     return reg;
4142
4143   /* Legacy FPA encoding.  These were once used in a way which
4144      overlapped with VFP register numbering, so their use is
4145      discouraged, but GDB doesn't support the ARM toolchain
4146      which used them for VFP.  */
4147   if (reg >= 16 && reg <= 23)
4148     return ARM_F0_REGNUM + reg - 16;
4149
4150   /* New assignments for the FPA registers.  */
4151   if (reg >= 96 && reg <= 103)
4152     return ARM_F0_REGNUM + reg - 96;
4153
4154   /* WMMX register assignments.  */
4155   if (reg >= 104 && reg <= 111)
4156     return ARM_WCGR0_REGNUM + reg - 104;
4157
4158   if (reg >= 112 && reg <= 127)
4159     return ARM_WR0_REGNUM + reg - 112;
4160
4161   if (reg >= 192 && reg <= 199)
4162     return ARM_WC0_REGNUM + reg - 192;
4163
4164   /* VFP v2 registers.  A double precision value is actually
4165      in d1 rather than s2, but the ABI only defines numbering
4166      for the single precision registers.  This will "just work"
4167      in GDB for little endian targets (we'll read eight bytes,
4168      starting in s0 and then progressing to s1), but will be
4169      reversed on big endian targets with VFP.  This won't
4170      be a problem for the new Neon quad registers; you're supposed
4171      to use DW_OP_piece for those.  */
4172   if (reg >= 64 && reg <= 95)
4173     {
4174       char name_buf[4];
4175
4176       xsnprintf (name_buf, sizeof (name_buf), "s%d", reg - 64);
4177       return user_reg_map_name_to_regnum (gdbarch, name_buf,
4178                                           strlen (name_buf));
4179     }
4180
4181   /* VFP v3 / Neon registers.  This range is also used for VFP v2
4182      registers, except that it now describes d0 instead of s0.  */
4183   if (reg >= 256 && reg <= 287)
4184     {
4185       char name_buf[4];
4186
4187       xsnprintf (name_buf, sizeof (name_buf), "d%d", reg - 256);
4188       return user_reg_map_name_to_regnum (gdbarch, name_buf,
4189                                           strlen (name_buf));
4190     }
4191
4192   return -1;
4193 }
4194
4195 /* Map GDB internal REGNUM onto the Arm simulator register numbers.  */
4196 static int
4197 arm_register_sim_regno (struct gdbarch *gdbarch, int regnum)
4198 {
4199   int reg = regnum;
4200   gdb_assert (reg >= 0 && reg < gdbarch_num_regs (gdbarch));
4201
4202   if (regnum >= ARM_WR0_REGNUM && regnum <= ARM_WR15_REGNUM)
4203     return regnum - ARM_WR0_REGNUM + SIM_ARM_IWMMXT_COP0R0_REGNUM;
4204
4205   if (regnum >= ARM_WC0_REGNUM && regnum <= ARM_WC7_REGNUM)
4206     return regnum - ARM_WC0_REGNUM + SIM_ARM_IWMMXT_COP1R0_REGNUM;
4207
4208   if (regnum >= ARM_WCGR0_REGNUM && regnum <= ARM_WCGR7_REGNUM)
4209     return regnum - ARM_WCGR0_REGNUM + SIM_ARM_IWMMXT_COP1R8_REGNUM;
4210
4211   if (reg < NUM_GREGS)
4212     return SIM_ARM_R0_REGNUM + reg;
4213   reg -= NUM_GREGS;
4214
4215   if (reg < NUM_FREGS)
4216     return SIM_ARM_FP0_REGNUM + reg;
4217   reg -= NUM_FREGS;
4218
4219   if (reg < NUM_SREGS)
4220     return SIM_ARM_FPS_REGNUM + reg;
4221   reg -= NUM_SREGS;
4222
4223   internal_error (__FILE__, __LINE__, _("Bad REGNUM %d"), regnum);
4224 }
4225
4226 /* NOTE: cagney/2001-08-20: Both convert_from_extended() and
4227    convert_to_extended() use floatformat_arm_ext_littlebyte_bigword.
4228    It is thought that this is is the floating-point register format on
4229    little-endian systems.  */
4230
4231 static void
4232 convert_from_extended (const struct floatformat *fmt, const void *ptr,
4233                        void *dbl, int endianess)
4234 {
4235   DOUBLEST d;
4236
4237   if (endianess == BFD_ENDIAN_BIG)
4238     floatformat_to_doublest (&floatformat_arm_ext_big, ptr, &d);
4239   else
4240     floatformat_to_doublest (&floatformat_arm_ext_littlebyte_bigword,
4241                              ptr, &d);
4242   floatformat_from_doublest (fmt, &d, dbl);
4243 }
4244
4245 static void
4246 convert_to_extended (const struct floatformat *fmt, void *dbl, const void *ptr,
4247                      int endianess)
4248 {
4249   DOUBLEST d;
4250
4251   floatformat_to_doublest (fmt, ptr, &d);
4252   if (endianess == BFD_ENDIAN_BIG)
4253     floatformat_from_doublest (&floatformat_arm_ext_big, &d, dbl);
4254   else
4255     floatformat_from_doublest (&floatformat_arm_ext_littlebyte_bigword,
4256                                &d, dbl);
4257 }
4258
4259 static int
4260 condition_true (unsigned long cond, unsigned long status_reg)
4261 {
4262   if (cond == INST_AL || cond == INST_NV)
4263     return 1;
4264
4265   switch (cond)
4266     {
4267     case INST_EQ:
4268       return ((status_reg & FLAG_Z) != 0);
4269     case INST_NE:
4270       return ((status_reg & FLAG_Z) == 0);
4271     case INST_CS:
4272       return ((status_reg & FLAG_C) != 0);
4273     case INST_CC:
4274       return ((status_reg & FLAG_C) == 0);
4275     case INST_MI:
4276       return ((status_reg & FLAG_N) != 0);
4277     case INST_PL:
4278       return ((status_reg & FLAG_N) == 0);
4279     case INST_VS:
4280       return ((status_reg & FLAG_V) != 0);
4281     case INST_VC:
4282       return ((status_reg & FLAG_V) == 0);
4283     case INST_HI:
4284       return ((status_reg & (FLAG_C | FLAG_Z)) == FLAG_C);
4285     case INST_LS:
4286       return ((status_reg & (FLAG_C | FLAG_Z)) != FLAG_C);
4287     case INST_GE:
4288       return (((status_reg & FLAG_N) == 0) == ((status_reg & FLAG_V) == 0));
4289     case INST_LT:
4290       return (((status_reg & FLAG_N) == 0) != ((status_reg & FLAG_V) == 0));
4291     case INST_GT:
4292       return (((status_reg & FLAG_Z) == 0)
4293               && (((status_reg & FLAG_N) == 0)
4294                   == ((status_reg & FLAG_V) == 0)));
4295     case INST_LE:
4296       return (((status_reg & FLAG_Z) != 0)
4297               || (((status_reg & FLAG_N) == 0)
4298                   != ((status_reg & FLAG_V) == 0)));
4299     }
4300   return 1;
4301 }
4302
4303 static unsigned long
4304 shifted_reg_val (struct frame_info *frame, unsigned long inst, int carry,
4305                  unsigned long pc_val, unsigned long status_reg)
4306 {
4307   unsigned long res, shift;
4308   int rm = bits (inst, 0, 3);
4309   unsigned long shifttype = bits (inst, 5, 6);
4310
4311   if (bit (inst, 4))
4312     {
4313       int rs = bits (inst, 8, 11);
4314       shift = (rs == 15 ? pc_val + 8
4315                         : get_frame_register_unsigned (frame, rs)) & 0xFF;
4316     }
4317   else
4318     shift = bits (inst, 7, 11);
4319
4320   res = (rm == ARM_PC_REGNUM
4321          ? (pc_val + (bit (inst, 4) ? 12 : 8))
4322          : get_frame_register_unsigned (frame, rm));
4323
4324   switch (shifttype)
4325     {
4326     case 0:                     /* LSL */
4327       res = shift >= 32 ? 0 : res << shift;
4328       break;
4329
4330     case 1:                     /* LSR */
4331       res = shift >= 32 ? 0 : res >> shift;
4332       break;
4333
4334     case 2:                     /* ASR */
4335       if (shift >= 32)
4336         shift = 31;
4337       res = ((res & 0x80000000L)
4338              ? ~((~res) >> shift) : res >> shift);
4339       break;
4340
4341     case 3:                     /* ROR/RRX */
4342       shift &= 31;
4343       if (shift == 0)
4344         res = (res >> 1) | (carry ? 0x80000000L : 0);
4345       else
4346         res = (res >> shift) | (res << (32 - shift));
4347       break;
4348     }
4349
4350   return res & 0xffffffff;
4351 }
4352
4353 /* Return number of 1-bits in VAL.  */
4354
4355 static int
4356 bitcount (unsigned long val)
4357 {
4358   int nbits;
4359   for (nbits = 0; val != 0; nbits++)
4360     val &= val - 1;             /* Delete rightmost 1-bit in val.  */
4361   return nbits;
4362 }
4363
4364 /* Return the size in bytes of the complete Thumb instruction whose
4365    first halfword is INST1.  */
4366
4367 static int
4368 thumb_insn_size (unsigned short inst1)
4369 {
4370   if ((inst1 & 0xe000) == 0xe000 && (inst1 & 0x1800) != 0)
4371     return 4;
4372   else
4373     return 2;
4374 }
4375
4376 static int
4377 thumb_advance_itstate (unsigned int itstate)
4378 {
4379   /* Preserve IT[7:5], the first three bits of the condition.  Shift
4380      the upcoming condition flags left by one bit.  */
4381   itstate = (itstate & 0xe0) | ((itstate << 1) & 0x1f);
4382
4383   /* If we have finished the IT block, clear the state.  */
4384   if ((itstate & 0x0f) == 0)
4385     itstate = 0;
4386
4387   return itstate;
4388 }
4389
4390 /* Find the next PC after the current instruction executes.  In some
4391    cases we can not statically determine the answer (see the IT state
4392    handling in this function); in that case, a breakpoint may be
4393    inserted in addition to the returned PC, which will be used to set
4394    another breakpoint by our caller.  */
4395
4396 static CORE_ADDR
4397 thumb_get_next_pc_raw (struct frame_info *frame, CORE_ADDR pc)
4398 {
4399   struct gdbarch *gdbarch = get_frame_arch (frame);
4400   struct address_space *aspace = get_frame_address_space (frame);
4401   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
4402   enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
4403   unsigned long pc_val = ((unsigned long) pc) + 4;      /* PC after prefetch */
4404   unsigned short inst1;
4405   CORE_ADDR nextpc = pc + 2;            /* Default is next instruction.  */
4406   unsigned long offset;
4407   ULONGEST status, itstate;
4408
4409   nextpc = MAKE_THUMB_ADDR (nextpc);
4410   pc_val = MAKE_THUMB_ADDR (pc_val);
4411
4412   inst1 = read_memory_unsigned_integer (pc, 2, byte_order_for_code);
4413
4414   /* Thumb-2 conditional execution support.  There are eight bits in
4415      the CPSR which describe conditional execution state.  Once
4416      reconstructed (they're in a funny order), the low five bits
4417      describe the low bit of the condition for each instruction and
4418      how many instructions remain.  The high three bits describe the
4419      base condition.  One of the low four bits will be set if an IT
4420      block is active.  These bits read as zero on earlier
4421      processors.  */
4422   status = get_frame_register_unsigned (frame, ARM_PS_REGNUM);
4423   itstate = ((status >> 8) & 0xfc) | ((status >> 25) & 0x3);
4424
4425   /* If-Then handling.  On GNU/Linux, where this routine is used, we
4426      use an undefined instruction as a breakpoint.  Unlike BKPT, IT
4427      can disable execution of the undefined instruction.  So we might
4428      miss the breakpoint if we set it on a skipped conditional
4429      instruction.  Because conditional instructions can change the
4430      flags, affecting the execution of further instructions, we may
4431      need to set two breakpoints.  */
4432
4433   if (gdbarch_tdep (gdbarch)->thumb2_breakpoint != NULL)
4434     {
4435       if ((inst1 & 0xff00) == 0xbf00 && (inst1 & 0x000f) != 0)
4436         {
4437           /* An IT instruction.  Because this instruction does not
4438              modify the flags, we can accurately predict the next
4439              executed instruction.  */
4440           itstate = inst1 & 0x00ff;
4441           pc += thumb_insn_size (inst1);
4442
4443           while (itstate != 0 && ! condition_true (itstate >> 4, status))
4444             {
4445               inst1 = read_memory_unsigned_integer (pc, 2,
4446                                                     byte_order_for_code);
4447               pc += thumb_insn_size (inst1);
4448               itstate = thumb_advance_itstate (itstate);
4449             }
4450
4451           return MAKE_THUMB_ADDR (pc);
4452         }
4453       else if (itstate != 0)
4454         {
4455           /* We are in a conditional block.  Check the condition.  */
4456           if (! condition_true (itstate >> 4, status))
4457             {
4458               /* Advance to the next executed instruction.  */
4459               pc += thumb_insn_size (inst1);
4460               itstate = thumb_advance_itstate (itstate);
4461
4462               while (itstate != 0 && ! condition_true (itstate >> 4, status))
4463                 {
4464                   inst1 = read_memory_unsigned_integer (pc, 2, 
4465                                                         byte_order_for_code);
4466                   pc += thumb_insn_size (inst1);
4467                   itstate = thumb_advance_itstate (itstate);
4468                 }
4469
4470               return MAKE_THUMB_ADDR (pc);
4471             }
4472           else if ((itstate & 0x0f) == 0x08)
4473             {
4474               /* This is the last instruction of the conditional
4475                  block, and it is executed.  We can handle it normally
4476                  because the following instruction is not conditional,
4477                  and we must handle it normally because it is
4478                  permitted to branch.  Fall through.  */
4479             }
4480           else
4481             {
4482               int cond_negated;
4483
4484               /* There are conditional instructions after this one.
4485                  If this instruction modifies the flags, then we can
4486                  not predict what the next executed instruction will
4487                  be.  Fortunately, this instruction is architecturally
4488                  forbidden to branch; we know it will fall through.
4489                  Start by skipping past it.  */
4490               pc += thumb_insn_size (inst1);
4491               itstate = thumb_advance_itstate (itstate);
4492
4493               /* Set a breakpoint on the following instruction.  */
4494               gdb_assert ((itstate & 0x0f) != 0);
4495               arm_insert_single_step_breakpoint (gdbarch, aspace,
4496                                                  MAKE_THUMB_ADDR (pc));
4497               cond_negated = (itstate >> 4) & 1;
4498
4499               /* Skip all following instructions with the same
4500                  condition.  If there is a later instruction in the IT
4501                  block with the opposite condition, set the other
4502                  breakpoint there.  If not, then set a breakpoint on
4503                  the instruction after the IT block.  */
4504               do
4505                 {
4506                   inst1 = read_memory_unsigned_integer (pc, 2,
4507                                                         byte_order_for_code);
4508                   pc += thumb_insn_size (inst1);
4509                   itstate = thumb_advance_itstate (itstate);
4510                 }
4511               while (itstate != 0 && ((itstate >> 4) & 1) == cond_negated);
4512
4513               return MAKE_THUMB_ADDR (pc);
4514             }
4515         }
4516     }
4517   else if (itstate & 0x0f)
4518     {
4519       /* We are in a conditional block.  Check the condition.  */
4520       int cond = itstate >> 4;
4521
4522       if (! condition_true (cond, status))
4523         /* Advance to the next instruction.  All the 32-bit
4524            instructions share a common prefix.  */
4525         return MAKE_THUMB_ADDR (pc + thumb_insn_size (inst1));
4526
4527       /* Otherwise, handle the instruction normally.  */
4528     }
4529
4530   if ((inst1 & 0xff00) == 0xbd00)       /* pop {rlist, pc} */
4531     {
4532       CORE_ADDR sp;
4533
4534       /* Fetch the saved PC from the stack.  It's stored above
4535          all of the other registers.  */
4536       offset = bitcount (bits (inst1, 0, 7)) * INT_REGISTER_SIZE;
4537       sp = get_frame_register_unsigned (frame, ARM_SP_REGNUM);
4538       nextpc = read_memory_unsigned_integer (sp + offset, 4, byte_order);
4539     }
4540   else if ((inst1 & 0xf000) == 0xd000)  /* conditional branch */
4541     {
4542       unsigned long cond = bits (inst1, 8, 11);
4543       if (cond == 0x0f)  /* 0x0f = SWI */
4544         {
4545           struct gdbarch_tdep *tdep;
4546           tdep = gdbarch_tdep (gdbarch);
4547
4548           if (tdep->syscall_next_pc != NULL)
4549             nextpc = tdep->syscall_next_pc (frame);
4550
4551         }
4552       else if (cond != 0x0f && condition_true (cond, status))
4553         nextpc = pc_val + (sbits (inst1, 0, 7) << 1);
4554     }
4555   else if ((inst1 & 0xf800) == 0xe000)  /* unconditional branch */
4556     {
4557       nextpc = pc_val + (sbits (inst1, 0, 10) << 1);
4558     }
4559   else if (thumb_insn_size (inst1) == 4) /* 32-bit instruction */
4560     {
4561       unsigned short inst2;
4562       inst2 = read_memory_unsigned_integer (pc + 2, 2, byte_order_for_code);
4563
4564       /* Default to the next instruction.  */
4565       nextpc = pc + 4;
4566       nextpc = MAKE_THUMB_ADDR (nextpc);
4567
4568       if ((inst1 & 0xf800) == 0xf000 && (inst2 & 0x8000) == 0x8000)
4569         {
4570           /* Branches and miscellaneous control instructions.  */
4571
4572           if ((inst2 & 0x1000) != 0 || (inst2 & 0xd001) == 0xc000)
4573             {
4574               /* B, BL, BLX.  */
4575               int j1, j2, imm1, imm2;
4576
4577               imm1 = sbits (inst1, 0, 10);
4578               imm2 = bits (inst2, 0, 10);
4579               j1 = bit (inst2, 13);
4580               j2 = bit (inst2, 11);
4581
4582               offset = ((imm1 << 12) + (imm2 << 1));
4583               offset ^= ((!j2) << 22) | ((!j1) << 23);
4584
4585               nextpc = pc_val + offset;
4586               /* For BLX make sure to clear the low bits.  */
4587               if (bit (inst2, 12) == 0)
4588                 nextpc = nextpc & 0xfffffffc;
4589             }
4590           else if (inst1 == 0xf3de && (inst2 & 0xff00) == 0x3f00)
4591             {
4592               /* SUBS PC, LR, #imm8.  */
4593               nextpc = get_frame_register_unsigned (frame, ARM_LR_REGNUM);
4594               nextpc -= inst2 & 0x00ff;
4595             }
4596           else if ((inst2 & 0xd000) == 0x8000 && (inst1 & 0x0380) != 0x0380)
4597             {
4598               /* Conditional branch.  */
4599               if (condition_true (bits (inst1, 6, 9), status))
4600                 {
4601                   int sign, j1, j2, imm1, imm2;
4602
4603                   sign = sbits (inst1, 10, 10);
4604                   imm1 = bits (inst1, 0, 5);
4605                   imm2 = bits (inst2, 0, 10);
4606                   j1 = bit (inst2, 13);
4607                   j2 = bit (inst2, 11);
4608
4609                   offset = (sign << 20) + (j2 << 19) + (j1 << 18);
4610                   offset += (imm1 << 12) + (imm2 << 1);
4611
4612                   nextpc = pc_val + offset;
4613                 }
4614             }
4615         }
4616       else if ((inst1 & 0xfe50) == 0xe810)
4617         {
4618           /* Load multiple or RFE.  */
4619           int rn, offset, load_pc = 1;
4620
4621           rn = bits (inst1, 0, 3);
4622           if (bit (inst1, 7) && !bit (inst1, 8))
4623             {
4624               /* LDMIA or POP */
4625               if (!bit (inst2, 15))
4626                 load_pc = 0;
4627               offset = bitcount (inst2) * 4 - 4;
4628             }
4629           else if (!bit (inst1, 7) && bit (inst1, 8))
4630             {
4631               /* LDMDB */
4632               if (!bit (inst2, 15))
4633                 load_pc = 0;
4634               offset = -4;
4635             }
4636           else if (bit (inst1, 7) && bit (inst1, 8))
4637             {
4638               /* RFEIA */
4639               offset = 0;
4640             }
4641           else if (!bit (inst1, 7) && !bit (inst1, 8))
4642             {
4643               /* RFEDB */
4644               offset = -8;
4645             }
4646           else
4647             load_pc = 0;
4648
4649           if (load_pc)
4650             {
4651               CORE_ADDR addr = get_frame_register_unsigned (frame, rn);
4652               nextpc = get_frame_memory_unsigned (frame, addr + offset, 4);
4653             }
4654         }
4655       else if ((inst1 & 0xffef) == 0xea4f && (inst2 & 0xfff0) == 0x0f00)
4656         {
4657           /* MOV PC or MOVS PC.  */
4658           nextpc = get_frame_register_unsigned (frame, bits (inst2, 0, 3));
4659           nextpc = MAKE_THUMB_ADDR (nextpc);
4660         }
4661       else if ((inst1 & 0xff70) == 0xf850 && (inst2 & 0xf000) == 0xf000)
4662         {
4663           /* LDR PC.  */
4664           CORE_ADDR base;
4665           int rn, load_pc = 1;
4666
4667           rn = bits (inst1, 0, 3);
4668           base = get_frame_register_unsigned (frame, rn);
4669           if (rn == ARM_PC_REGNUM)
4670             {
4671               base = (base + 4) & ~(CORE_ADDR) 0x3;
4672               if (bit (inst1, 7))
4673                 base += bits (inst2, 0, 11);
4674               else
4675                 base -= bits (inst2, 0, 11);
4676             }
4677           else if (bit (inst1, 7))
4678             base += bits (inst2, 0, 11);
4679           else if (bit (inst2, 11))
4680             {
4681               if (bit (inst2, 10))
4682                 {
4683                   if (bit (inst2, 9))
4684                     base += bits (inst2, 0, 7);
4685                   else
4686                     base -= bits (inst2, 0, 7);
4687                 }
4688             }
4689           else if ((inst2 & 0x0fc0) == 0x0000)
4690             {
4691               int shift = bits (inst2, 4, 5), rm = bits (inst2, 0, 3);
4692               base += get_frame_register_unsigned (frame, rm) << shift;
4693             }
4694           else
4695             /* Reserved.  */
4696             load_pc = 0;
4697
4698           if (load_pc)
4699             nextpc = get_frame_memory_unsigned (frame, base, 4);
4700         }
4701       else if ((inst1 & 0xfff0) == 0xe8d0 && (inst2 & 0xfff0) == 0xf000)
4702         {
4703           /* TBB.  */
4704           CORE_ADDR tbl_reg, table, offset, length;
4705
4706           tbl_reg = bits (inst1, 0, 3);
4707           if (tbl_reg == 0x0f)
4708             table = pc + 4;  /* Regcache copy of PC isn't right yet.  */
4709           else
4710             table = get_frame_register_unsigned (frame, tbl_reg);
4711
4712           offset = get_frame_register_unsigned (frame, bits (inst2, 0, 3));
4713           length = 2 * get_frame_memory_unsigned (frame, table + offset, 1);
4714           nextpc = pc_val + length;
4715         }
4716       else if ((inst1 & 0xfff0) == 0xe8d0 && (inst2 & 0xfff0) == 0xf010)
4717         {
4718           /* TBH.  */
4719           CORE_ADDR tbl_reg, table, offset, length;
4720
4721           tbl_reg = bits (inst1, 0, 3);
4722           if (tbl_reg == 0x0f)
4723             table = pc + 4;  /* Regcache copy of PC isn't right yet.  */
4724           else
4725             table = get_frame_register_unsigned (frame, tbl_reg);
4726
4727           offset = 2 * get_frame_register_unsigned (frame, bits (inst2, 0, 3));
4728           length = 2 * get_frame_memory_unsigned (frame, table + offset, 2);
4729           nextpc = pc_val + length;
4730         }
4731     }
4732   else if ((inst1 & 0xff00) == 0x4700)  /* bx REG, blx REG */
4733     {
4734       if (bits (inst1, 3, 6) == 0x0f)
4735         nextpc = UNMAKE_THUMB_ADDR (pc_val);
4736       else
4737         nextpc = get_frame_register_unsigned (frame, bits (inst1, 3, 6));
4738     }
4739   else if ((inst1 & 0xff87) == 0x4687)  /* mov pc, REG */
4740     {
4741       if (bits (inst1, 3, 6) == 0x0f)
4742         nextpc = pc_val;
4743       else
4744         nextpc = get_frame_register_unsigned (frame, bits (inst1, 3, 6));
4745
4746       nextpc = MAKE_THUMB_ADDR (nextpc);
4747     }
4748   else if ((inst1 & 0xf500) == 0xb100)
4749     {
4750       /* CBNZ or CBZ.  */
4751       int imm = (bit (inst1, 9) << 6) + (bits (inst1, 3, 7) << 1);
4752       ULONGEST reg = get_frame_register_unsigned (frame, bits (inst1, 0, 2));
4753
4754       if (bit (inst1, 11) && reg != 0)
4755         nextpc = pc_val + imm;
4756       else if (!bit (inst1, 11) && reg == 0)
4757         nextpc = pc_val + imm;
4758     }
4759   return nextpc;
4760 }
4761
4762 /* Get the raw next address.  PC is the current program counter, in 
4763    FRAME, which is assumed to be executing in ARM mode.
4764
4765    The value returned has the execution state of the next instruction 
4766    encoded in it.  Use IS_THUMB_ADDR () to see whether the instruction is
4767    in Thumb-State, and gdbarch_addr_bits_remove () to get the plain memory
4768    address.  */
4769
4770 static CORE_ADDR
4771 arm_get_next_pc_raw (struct frame_info *frame, CORE_ADDR pc)
4772 {
4773   struct gdbarch *gdbarch = get_frame_arch (frame);
4774   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
4775   enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
4776   unsigned long pc_val;
4777   unsigned long this_instr;
4778   unsigned long status;
4779   CORE_ADDR nextpc;
4780
4781   pc_val = (unsigned long) pc;
4782   this_instr = read_memory_unsigned_integer (pc, 4, byte_order_for_code);
4783
4784   status = get_frame_register_unsigned (frame, ARM_PS_REGNUM);
4785   nextpc = (CORE_ADDR) (pc_val + 4);    /* Default case */
4786
4787   if (bits (this_instr, 28, 31) == INST_NV)
4788     switch (bits (this_instr, 24, 27))
4789       {
4790       case 0xa:
4791       case 0xb:
4792         {
4793           /* Branch with Link and change to Thumb.  */
4794           nextpc = BranchDest (pc, this_instr);
4795           nextpc |= bit (this_instr, 24) << 1;
4796           nextpc = MAKE_THUMB_ADDR (nextpc);
4797           break;
4798         }
4799       case 0xc:
4800       case 0xd:
4801       case 0xe:
4802         /* Coprocessor register transfer.  */
4803         if (bits (this_instr, 12, 15) == 15)
4804           error (_("Invalid update to pc in instruction"));
4805         break;
4806       }
4807   else if (condition_true (bits (this_instr, 28, 31), status))
4808     {
4809       switch (bits (this_instr, 24, 27))
4810         {
4811         case 0x0:
4812         case 0x1:                       /* data processing */
4813         case 0x2:
4814         case 0x3:
4815           {
4816             unsigned long operand1, operand2, result = 0;
4817             unsigned long rn;
4818             int c;
4819
4820             if (bits (this_instr, 12, 15) != 15)
4821               break;
4822
4823             if (bits (this_instr, 22, 25) == 0
4824                 && bits (this_instr, 4, 7) == 9)        /* multiply */
4825               error (_("Invalid update to pc in instruction"));
4826
4827             /* BX <reg>, BLX <reg> */
4828             if (bits (this_instr, 4, 27) == 0x12fff1
4829                 || bits (this_instr, 4, 27) == 0x12fff3)
4830               {
4831                 rn = bits (this_instr, 0, 3);
4832                 nextpc = ((rn == ARM_PC_REGNUM)
4833                           ? (pc_val + 8)
4834                           : get_frame_register_unsigned (frame, rn));
4835
4836                 return nextpc;
4837               }
4838
4839             /* Multiply into PC.  */
4840             c = (status & FLAG_C) ? 1 : 0;
4841             rn = bits (this_instr, 16, 19);
4842             operand1 = ((rn == ARM_PC_REGNUM)
4843                         ? (pc_val + 8)
4844                         : get_frame_register_unsigned (frame, rn));
4845
4846             if (bit (this_instr, 25))
4847               {
4848                 unsigned long immval = bits (this_instr, 0, 7);
4849                 unsigned long rotate = 2 * bits (this_instr, 8, 11);
4850                 operand2 = ((immval >> rotate) | (immval << (32 - rotate)))
4851                   & 0xffffffff;
4852               }
4853             else                /* operand 2 is a shifted register.  */
4854               operand2 = shifted_reg_val (frame, this_instr, c,
4855                                           pc_val, status);
4856
4857             switch (bits (this_instr, 21, 24))
4858               {
4859               case 0x0: /*and */
4860                 result = operand1 & operand2;
4861                 break;
4862
4863               case 0x1: /*eor */
4864                 result = operand1 ^ operand2;
4865                 break;
4866
4867               case 0x2: /*sub */
4868                 result = operand1 - operand2;
4869                 break;
4870
4871               case 0x3: /*rsb */
4872                 result = operand2 - operand1;
4873                 break;
4874
4875               case 0x4: /*add */
4876                 result = operand1 + operand2;
4877                 break;
4878
4879               case 0x5: /*adc */
4880                 result = operand1 + operand2 + c;
4881                 break;
4882
4883               case 0x6: /*sbc */
4884                 result = operand1 - operand2 + c;
4885                 break;
4886
4887               case 0x7: /*rsc */
4888                 result = operand2 - operand1 + c;
4889                 break;
4890
4891               case 0x8:
4892               case 0x9:
4893               case 0xa:
4894               case 0xb: /* tst, teq, cmp, cmn */
4895                 result = (unsigned long) nextpc;
4896                 break;
4897
4898               case 0xc: /*orr */
4899                 result = operand1 | operand2;
4900                 break;
4901
4902               case 0xd: /*mov */
4903                 /* Always step into a function.  */
4904                 result = operand2;
4905                 break;
4906
4907               case 0xe: /*bic */
4908                 result = operand1 & ~operand2;
4909                 break;
4910
4911               case 0xf: /*mvn */
4912                 result = ~operand2;
4913                 break;
4914               }
4915
4916             /* In 26-bit APCS the bottom two bits of the result are 
4917                ignored, and we always end up in ARM state.  */
4918             if (!arm_apcs_32)
4919               nextpc = arm_addr_bits_remove (gdbarch, result);
4920             else
4921               nextpc = result;
4922
4923             break;
4924           }
4925
4926         case 0x4:
4927         case 0x5:               /* data transfer */
4928         case 0x6:
4929         case 0x7:
4930           if (bit (this_instr, 20))
4931             {
4932               /* load */
4933               if (bits (this_instr, 12, 15) == 15)
4934                 {
4935                   /* rd == pc */
4936                   unsigned long rn;
4937                   unsigned long base;
4938
4939                   if (bit (this_instr, 22))
4940                     error (_("Invalid update to pc in instruction"));
4941
4942                   /* byte write to PC */
4943                   rn = bits (this_instr, 16, 19);
4944                   base = ((rn == ARM_PC_REGNUM)
4945                           ? (pc_val + 8)
4946                           : get_frame_register_unsigned (frame, rn));
4947
4948                   if (bit (this_instr, 24))
4949                     {
4950                       /* pre-indexed */
4951                       int c = (status & FLAG_C) ? 1 : 0;
4952                       unsigned long offset =
4953                       (bit (this_instr, 25)
4954                        ? shifted_reg_val (frame, this_instr, c, pc_val, status)
4955                        : bits (this_instr, 0, 11));
4956
4957                       if (bit (this_instr, 23))
4958                         base += offset;
4959                       else
4960                         base -= offset;
4961                     }
4962                   nextpc =
4963                     (CORE_ADDR) read_memory_unsigned_integer ((CORE_ADDR) base,
4964                                                               4, byte_order);
4965                 }
4966             }
4967           break;
4968
4969         case 0x8:
4970         case 0x9:               /* block transfer */
4971           if (bit (this_instr, 20))
4972             {
4973               /* LDM */
4974               if (bit (this_instr, 15))
4975                 {
4976                   /* loading pc */
4977                   int offset = 0;
4978                   unsigned long rn_val
4979                     = get_frame_register_unsigned (frame,
4980                                                    bits (this_instr, 16, 19));
4981
4982                   if (bit (this_instr, 23))
4983                     {
4984                       /* up */
4985                       unsigned long reglist = bits (this_instr, 0, 14);
4986                       offset = bitcount (reglist) * 4;
4987                       if (bit (this_instr, 24))         /* pre */
4988                         offset += 4;
4989                     }
4990                   else if (bit (this_instr, 24))
4991                     offset = -4;
4992
4993                   nextpc =
4994                     (CORE_ADDR) read_memory_unsigned_integer ((CORE_ADDR)
4995                                                               (rn_val + offset),
4996                                                               4, byte_order);
4997                 }
4998             }
4999           break;
5000
5001         case 0xb:               /* branch & link */
5002         case 0xa:               /* branch */
5003           {
5004             nextpc = BranchDest (pc, this_instr);
5005             break;
5006           }
5007
5008         case 0xc:
5009         case 0xd:
5010         case 0xe:               /* coproc ops */
5011           break;
5012         case 0xf:               /* SWI */
5013           {
5014             struct gdbarch_tdep *tdep;
5015             tdep = gdbarch_tdep (gdbarch);
5016
5017             if (tdep->syscall_next_pc != NULL)
5018               nextpc = tdep->syscall_next_pc (frame);
5019
5020           }
5021           break;
5022
5023         default:
5024           fprintf_filtered (gdb_stderr, _("Bad bit-field extraction\n"));
5025           return (pc);
5026         }
5027     }
5028
5029   return nextpc;
5030 }
5031
5032 /* Determine next PC after current instruction executes.  Will call either
5033    arm_get_next_pc_raw or thumb_get_next_pc_raw.  Error out if infinite
5034    loop is detected.  */
5035
5036 CORE_ADDR
5037 arm_get_next_pc (struct frame_info *frame, CORE_ADDR pc)
5038 {
5039   CORE_ADDR nextpc;
5040
5041   if (arm_frame_is_thumb (frame))
5042     nextpc = thumb_get_next_pc_raw (frame, pc);
5043   else
5044     nextpc = arm_get_next_pc_raw (frame, pc);
5045
5046   return nextpc;
5047 }
5048
5049 /* Like insert_single_step_breakpoint, but make sure we use a breakpoint
5050    of the appropriate mode (as encoded in the PC value), even if this
5051    differs from what would be expected according to the symbol tables.  */
5052
5053 void
5054 arm_insert_single_step_breakpoint (struct gdbarch *gdbarch,
5055                                    struct address_space *aspace,
5056                                    CORE_ADDR pc)
5057 {
5058   struct cleanup *old_chain
5059     = make_cleanup_restore_integer (&arm_override_mode);
5060
5061   arm_override_mode = IS_THUMB_ADDR (pc);
5062   pc = gdbarch_addr_bits_remove (gdbarch, pc);
5063
5064   insert_single_step_breakpoint (gdbarch, aspace, pc);
5065
5066   do_cleanups (old_chain);
5067 }
5068
5069 /* Checks for an atomic sequence of instructions beginning with a LDREX{,B,H,D}
5070    instruction and ending with a STREX{,B,H,D} instruction.  If such a sequence
5071    is found, attempt to step through it.  A breakpoint is placed at the end of
5072    the sequence.  */
5073
5074 static int
5075 thumb_deal_with_atomic_sequence_raw (struct frame_info *frame)
5076 {
5077   struct gdbarch *gdbarch = get_frame_arch (frame);
5078   struct address_space *aspace = get_frame_address_space (frame);
5079   enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
5080   CORE_ADDR pc = get_frame_pc (frame);
5081   CORE_ADDR breaks[2] = {-1, -1};
5082   CORE_ADDR loc = pc;
5083   unsigned short insn1, insn2;
5084   int insn_count;
5085   int index;
5086   int last_breakpoint = 0; /* Defaults to 0 (no breakpoints placed).  */
5087   const int atomic_sequence_length = 16; /* Instruction sequence length.  */
5088   ULONGEST status, itstate;
5089
5090   /* We currently do not support atomic sequences within an IT block.  */
5091   status = get_frame_register_unsigned (frame, ARM_PS_REGNUM);
5092   itstate = ((status >> 8) & 0xfc) | ((status >> 25) & 0x3);
5093   if (itstate & 0x0f)
5094     return 0;
5095
5096   /* Assume all atomic sequences start with a ldrex{,b,h,d} instruction.  */
5097   insn1 = read_memory_unsigned_integer (loc, 2, byte_order_for_code);
5098   loc += 2;
5099   if (thumb_insn_size (insn1) != 4)
5100     return 0;
5101
5102   insn2 = read_memory_unsigned_integer (loc, 2, byte_order_for_code);
5103   loc += 2;
5104   if (!((insn1 & 0xfff0) == 0xe850
5105         || ((insn1 & 0xfff0) == 0xe8d0 && (insn2 & 0x00c0) == 0x0040)))
5106     return 0;
5107
5108   /* Assume that no atomic sequence is longer than "atomic_sequence_length"
5109      instructions.  */
5110   for (insn_count = 0; insn_count < atomic_sequence_length; ++insn_count)
5111     {
5112       insn1 = read_memory_unsigned_integer (loc, 2, byte_order_for_code);
5113       loc += 2;
5114
5115       if (thumb_insn_size (insn1) != 4)
5116         {
5117           /* Assume that there is at most one conditional branch in the
5118              atomic sequence.  If a conditional branch is found, put a
5119              breakpoint in its destination address.  */
5120           if ((insn1 & 0xf000) == 0xd000 && bits (insn1, 8, 11) != 0x0f)
5121             {
5122               if (last_breakpoint > 0)
5123                 return 0; /* More than one conditional branch found,
5124                              fallback to the standard code.  */
5125
5126               breaks[1] = loc + 2 + (sbits (insn1, 0, 7) << 1);
5127               last_breakpoint++;
5128             }
5129
5130           /* We do not support atomic sequences that use any *other*
5131              instructions but conditional branches to change the PC.
5132              Fall back to standard code to avoid losing control of
5133              execution.  */
5134           else if (thumb_instruction_changes_pc (insn1))
5135             return 0;
5136         }
5137       else
5138         {
5139           insn2 = read_memory_unsigned_integer (loc, 2, byte_order_for_code);
5140           loc += 2;
5141
5142           /* Assume that there is at most one conditional branch in the
5143              atomic sequence.  If a conditional branch is found, put a
5144              breakpoint in its destination address.  */
5145           if ((insn1 & 0xf800) == 0xf000
5146               && (insn2 & 0xd000) == 0x8000
5147               && (insn1 & 0x0380) != 0x0380)
5148             {
5149               int sign, j1, j2, imm1, imm2;
5150               unsigned int offset;
5151
5152               sign = sbits (insn1, 10, 10);
5153               imm1 = bits (insn1, 0, 5);
5154               imm2 = bits (insn2, 0, 10);
5155               j1 = bit (insn2, 13);
5156               j2 = bit (insn2, 11);
5157
5158               offset = (sign << 20) + (j2 << 19) + (j1 << 18);
5159               offset += (imm1 << 12) + (imm2 << 1);
5160
5161               if (last_breakpoint > 0)
5162                 return 0; /* More than one conditional branch found,
5163                              fallback to the standard code.  */
5164
5165               breaks[1] = loc + offset;
5166               last_breakpoint++;
5167             }
5168
5169           /* We do not support atomic sequences that use any *other*
5170              instructions but conditional branches to change the PC.
5171              Fall back to standard code to avoid losing control of
5172              execution.  */
5173           else if (thumb2_instruction_changes_pc (insn1, insn2))
5174             return 0;
5175
5176           /* If we find a strex{,b,h,d}, we're done.  */
5177           if ((insn1 & 0xfff0) == 0xe840
5178               || ((insn1 & 0xfff0) == 0xe8c0 && (insn2 & 0x00c0) == 0x0040))
5179             break;
5180         }
5181     }
5182
5183   /* If we didn't find the strex{,b,h,d}, we cannot handle the sequence.  */
5184   if (insn_count == atomic_sequence_length)
5185     return 0;
5186
5187   /* Insert a breakpoint right after the end of the atomic sequence.  */
5188   breaks[0] = loc;
5189
5190   /* Check for duplicated breakpoints.  Check also for a breakpoint
5191      placed (branch instruction's destination) anywhere in sequence.  */
5192   if (last_breakpoint
5193       && (breaks[1] == breaks[0]
5194           || (breaks[1] >= pc && breaks[1] < loc)))
5195     last_breakpoint = 0;
5196
5197   /* Effectively inserts the breakpoints.  */
5198   for (index = 0; index <= last_breakpoint; index++)
5199     arm_insert_single_step_breakpoint (gdbarch, aspace,
5200                                        MAKE_THUMB_ADDR (breaks[index]));
5201
5202   return 1;
5203 }
5204
5205 static int
5206 arm_deal_with_atomic_sequence_raw (struct frame_info *frame)
5207 {
5208   struct gdbarch *gdbarch = get_frame_arch (frame);
5209   struct address_space *aspace = get_frame_address_space (frame);
5210   enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
5211   CORE_ADDR pc = get_frame_pc (frame);
5212   CORE_ADDR breaks[2] = {-1, -1};
5213   CORE_ADDR loc = pc;
5214   unsigned int insn;
5215   int insn_count;
5216   int index;
5217   int last_breakpoint = 0; /* Defaults to 0 (no breakpoints placed).  */
5218   const int atomic_sequence_length = 16; /* Instruction sequence length.  */
5219
5220   /* Assume all atomic sequences start with a ldrex{,b,h,d} instruction.
5221      Note that we do not currently support conditionally executed atomic
5222      instructions.  */
5223   insn = read_memory_unsigned_integer (loc, 4, byte_order_for_code);
5224   loc += 4;
5225   if ((insn & 0xff9000f0) != 0xe1900090)
5226     return 0;
5227
5228   /* Assume that no atomic sequence is longer than "atomic_sequence_length"
5229      instructions.  */
5230   for (insn_count = 0; insn_count < atomic_sequence_length; ++insn_count)
5231     {
5232       insn = read_memory_unsigned_integer (loc, 4, byte_order_for_code);
5233       loc += 4;
5234
5235       /* Assume that there is at most one conditional branch in the atomic
5236          sequence.  If a conditional branch is found, put a breakpoint in
5237          its destination address.  */
5238       if (bits (insn, 24, 27) == 0xa)
5239         {
5240           if (last_breakpoint > 0)
5241             return 0; /* More than one conditional branch found, fallback
5242                          to the standard single-step code.  */
5243
5244           breaks[1] = BranchDest (loc - 4, insn);
5245           last_breakpoint++;
5246         }
5247
5248       /* We do not support atomic sequences that use any *other* instructions
5249          but conditional branches to change the PC.  Fall back to standard
5250          code to avoid losing control of execution.  */
5251       else if (arm_instruction_changes_pc (insn))
5252         return 0;
5253
5254       /* If we find a strex{,b,h,d}, we're done.  */
5255       if ((insn & 0xff9000f0) == 0xe1800090)
5256         break;
5257     }
5258
5259   /* If we didn't find the strex{,b,h,d}, we cannot handle the sequence.  */
5260   if (insn_count == atomic_sequence_length)
5261     return 0;
5262
5263   /* Insert a breakpoint right after the end of the atomic sequence.  */
5264   breaks[0] = loc;
5265
5266   /* Check for duplicated breakpoints.  Check also for a breakpoint
5267      placed (branch instruction's destination) anywhere in sequence.  */
5268   if (last_breakpoint
5269       && (breaks[1] == breaks[0]
5270           || (breaks[1] >= pc && breaks[1] < loc)))
5271     last_breakpoint = 0;
5272
5273   /* Effectively inserts the breakpoints.  */
5274   for (index = 0; index <= last_breakpoint; index++)
5275     arm_insert_single_step_breakpoint (gdbarch, aspace, breaks[index]);
5276
5277   return 1;
5278 }
5279
5280 int
5281 arm_deal_with_atomic_sequence (struct frame_info *frame)
5282 {
5283   if (arm_frame_is_thumb (frame))
5284     return thumb_deal_with_atomic_sequence_raw (frame);
5285   else
5286     return arm_deal_with_atomic_sequence_raw (frame);
5287 }
5288
5289 /* single_step() is called just before we want to resume the inferior,
5290    if we want to single-step it but there is no hardware or kernel
5291    single-step support.  We find the target of the coming instruction
5292    and breakpoint it.  */
5293
5294 int
5295 arm_software_single_step (struct frame_info *frame)
5296 {
5297   struct gdbarch *gdbarch = get_frame_arch (frame);
5298   struct address_space *aspace = get_frame_address_space (frame);
5299   CORE_ADDR next_pc;
5300
5301   if (arm_deal_with_atomic_sequence (frame))
5302     return 1;
5303
5304   next_pc = arm_get_next_pc (frame, get_frame_pc (frame));
5305   arm_insert_single_step_breakpoint (gdbarch, aspace, next_pc);
5306
5307   return 1;
5308 }
5309
5310 /* Given BUF, which is OLD_LEN bytes ending at ENDADDR, expand
5311    the buffer to be NEW_LEN bytes ending at ENDADDR.  Return
5312    NULL if an error occurs.  BUF is freed.  */
5313
5314 static gdb_byte *
5315 extend_buffer_earlier (gdb_byte *buf, CORE_ADDR endaddr,
5316                        int old_len, int new_len)
5317 {
5318   gdb_byte *new_buf;
5319   int bytes_to_read = new_len - old_len;
5320
5321   new_buf = xmalloc (new_len);
5322   memcpy (new_buf + bytes_to_read, buf, old_len);
5323   xfree (buf);
5324   if (target_read_memory (endaddr - new_len, new_buf, bytes_to_read) != 0)
5325     {
5326       xfree (new_buf);
5327       return NULL;
5328     }
5329   return new_buf;
5330 }
5331
5332 /* An IT block is at most the 2-byte IT instruction followed by
5333    four 4-byte instructions.  The furthest back we must search to
5334    find an IT block that affects the current instruction is thus
5335    2 + 3 * 4 == 14 bytes.  */
5336 #define MAX_IT_BLOCK_PREFIX 14
5337
5338 /* Use a quick scan if there are more than this many bytes of
5339    code.  */
5340 #define IT_SCAN_THRESHOLD 32
5341
5342 /* Adjust a breakpoint's address to move breakpoints out of IT blocks.
5343    A breakpoint in an IT block may not be hit, depending on the
5344    condition flags.  */
5345 static CORE_ADDR
5346 arm_adjust_breakpoint_address (struct gdbarch *gdbarch, CORE_ADDR bpaddr)
5347 {
5348   gdb_byte *buf;
5349   char map_type;
5350   CORE_ADDR boundary, func_start;
5351   int buf_len;
5352   enum bfd_endian order = gdbarch_byte_order_for_code (gdbarch);
5353   int i, any, last_it, last_it_count;
5354
5355   /* If we are using BKPT breakpoints, none of this is necessary.  */
5356   if (gdbarch_tdep (gdbarch)->thumb2_breakpoint == NULL)
5357     return bpaddr;
5358
5359   /* ARM mode does not have this problem.  */
5360   if (!arm_pc_is_thumb (gdbarch, bpaddr))
5361     return bpaddr;
5362
5363   /* We are setting a breakpoint in Thumb code that could potentially
5364      contain an IT block.  The first step is to find how much Thumb
5365      code there is; we do not need to read outside of known Thumb
5366      sequences.  */
5367   map_type = arm_find_mapping_symbol (bpaddr, &boundary);
5368   if (map_type == 0)
5369     /* Thumb-2 code must have mapping symbols to have a chance.  */
5370     return bpaddr;
5371
5372   bpaddr = gdbarch_addr_bits_remove (gdbarch, bpaddr);
5373
5374   if (find_pc_partial_function (bpaddr, NULL, &func_start, NULL)
5375       && func_start > boundary)
5376     boundary = func_start;
5377
5378   /* Search for a candidate IT instruction.  We have to do some fancy
5379      footwork to distinguish a real IT instruction from the second
5380      half of a 32-bit instruction, but there is no need for that if
5381      there's no candidate.  */
5382   buf_len = min (bpaddr - boundary, MAX_IT_BLOCK_PREFIX);
5383   if (buf_len == 0)
5384     /* No room for an IT instruction.  */
5385     return bpaddr;
5386
5387   buf = xmalloc (buf_len);
5388   if (target_read_memory (bpaddr - buf_len, buf, buf_len) != 0)
5389     return bpaddr;
5390   any = 0;
5391   for (i = 0; i < buf_len; i += 2)
5392     {
5393       unsigned short inst1 = extract_unsigned_integer (&buf[i], 2, order);
5394       if ((inst1 & 0xff00) == 0xbf00 && (inst1 & 0x000f) != 0)
5395         {
5396           any = 1;
5397           break;
5398         }
5399     }
5400   if (any == 0)
5401     {
5402       xfree (buf);
5403       return bpaddr;
5404     }
5405
5406   /* OK, the code bytes before this instruction contain at least one
5407      halfword which resembles an IT instruction.  We know that it's
5408      Thumb code, but there are still two possibilities.  Either the
5409      halfword really is an IT instruction, or it is the second half of
5410      a 32-bit Thumb instruction.  The only way we can tell is to
5411      scan forwards from a known instruction boundary.  */
5412   if (bpaddr - boundary > IT_SCAN_THRESHOLD)
5413     {
5414       int definite;
5415
5416       /* There's a lot of code before this instruction.  Start with an
5417          optimistic search; it's easy to recognize halfwords that can
5418          not be the start of a 32-bit instruction, and use that to
5419          lock on to the instruction boundaries.  */
5420       buf = extend_buffer_earlier (buf, bpaddr, buf_len, IT_SCAN_THRESHOLD);
5421       if (buf == NULL)
5422         return bpaddr;
5423       buf_len = IT_SCAN_THRESHOLD;
5424
5425       definite = 0;
5426       for (i = 0; i < buf_len - sizeof (buf) && ! definite; i += 2)
5427         {
5428           unsigned short inst1 = extract_unsigned_integer (&buf[i], 2, order);
5429           if (thumb_insn_size (inst1) == 2)
5430             {
5431               definite = 1;
5432               break;
5433             }
5434         }
5435
5436       /* At this point, if DEFINITE, BUF[I] is the first place we
5437          are sure that we know the instruction boundaries, and it is far
5438          enough from BPADDR that we could not miss an IT instruction
5439          affecting BPADDR.  If ! DEFINITE, give up - start from a
5440          known boundary.  */
5441       if (! definite)
5442         {
5443           buf = extend_buffer_earlier (buf, bpaddr, buf_len,
5444                                        bpaddr - boundary);
5445           if (buf == NULL)
5446             return bpaddr;
5447           buf_len = bpaddr - boundary;
5448           i = 0;
5449         }
5450     }
5451   else
5452     {
5453       buf = extend_buffer_earlier (buf, bpaddr, buf_len, bpaddr - boundary);
5454       if (buf == NULL)
5455         return bpaddr;
5456       buf_len = bpaddr - boundary;
5457       i = 0;
5458     }
5459
5460   /* Scan forwards.  Find the last IT instruction before BPADDR.  */
5461   last_it = -1;
5462   last_it_count = 0;
5463   while (i < buf_len)
5464     {
5465       unsigned short inst1 = extract_unsigned_integer (&buf[i], 2, order);
5466       last_it_count--;
5467       if ((inst1 & 0xff00) == 0xbf00 && (inst1 & 0x000f) != 0)
5468         {
5469           last_it = i;
5470           if (inst1 & 0x0001)
5471             last_it_count = 4;
5472           else if (inst1 & 0x0002)
5473             last_it_count = 3;
5474           else if (inst1 & 0x0004)
5475             last_it_count = 2;
5476           else
5477             last_it_count = 1;
5478         }
5479       i += thumb_insn_size (inst1);
5480     }
5481
5482   xfree (buf);
5483
5484   if (last_it == -1)
5485     /* There wasn't really an IT instruction after all.  */
5486     return bpaddr;
5487
5488   if (last_it_count < 1)
5489     /* It was too far away.  */
5490     return bpaddr;
5491
5492   /* This really is a trouble spot.  Move the breakpoint to the IT
5493      instruction.  */
5494   return bpaddr - buf_len + last_it;
5495 }
5496
5497 /* ARM displaced stepping support.
5498
5499    Generally ARM displaced stepping works as follows:
5500
5501    1. When an instruction is to be single-stepped, it is first decoded by
5502       arm_process_displaced_insn (called from arm_displaced_step_copy_insn).
5503       Depending on the type of instruction, it is then copied to a scratch
5504       location, possibly in a modified form.  The copy_* set of functions
5505       performs such modification, as necessary.  A breakpoint is placed after
5506       the modified instruction in the scratch space to return control to GDB.
5507       Note in particular that instructions which modify the PC will no longer
5508       do so after modification.
5509
5510    2. The instruction is single-stepped, by setting the PC to the scratch
5511       location address, and resuming.  Control returns to GDB when the
5512       breakpoint is hit.
5513
5514    3. A cleanup function (cleanup_*) is called corresponding to the copy_*
5515       function used for the current instruction.  This function's job is to
5516       put the CPU/memory state back to what it would have been if the
5517       instruction had been executed unmodified in its original location.  */
5518
5519 /* NOP instruction (mov r0, r0).  */
5520 #define ARM_NOP                         0xe1a00000
5521 #define THUMB_NOP 0x4600
5522
5523 /* Helper for register reads for displaced stepping.  In particular, this
5524    returns the PC as it would be seen by the instruction at its original
5525    location.  */
5526
5527 ULONGEST
5528 displaced_read_reg (struct regcache *regs, struct displaced_step_closure *dsc,
5529                     int regno)
5530 {
5531   ULONGEST ret;
5532   CORE_ADDR from = dsc->insn_addr;
5533
5534   if (regno == ARM_PC_REGNUM)
5535     {
5536       /* Compute pipeline offset:
5537          - When executing an ARM instruction, PC reads as the address of the
5538          current instruction plus 8.
5539          - When executing a Thumb instruction, PC reads as the address of the
5540          current instruction plus 4.  */
5541
5542       if (!dsc->is_thumb)
5543         from += 8;
5544       else
5545         from += 4;
5546
5547       if (debug_displaced)
5548         fprintf_unfiltered (gdb_stdlog, "displaced: read pc value %.8lx\n",
5549                             (unsigned long) from);
5550       return (ULONGEST) from;
5551     }
5552   else
5553     {
5554       regcache_cooked_read_unsigned (regs, regno, &ret);
5555       if (debug_displaced)
5556         fprintf_unfiltered (gdb_stdlog, "displaced: read r%d value %.8lx\n",
5557                             regno, (unsigned long) ret);
5558       return ret;
5559     }
5560 }
5561
5562 static int
5563 displaced_in_arm_mode (struct regcache *regs)
5564 {
5565   ULONGEST ps;
5566   ULONGEST t_bit = arm_psr_thumb_bit (get_regcache_arch (regs));
5567
5568   regcache_cooked_read_unsigned (regs, ARM_PS_REGNUM, &ps);
5569
5570   return (ps & t_bit) == 0;
5571 }
5572
5573 /* Write to the PC as from a branch instruction.  */
5574
5575 static void
5576 branch_write_pc (struct regcache *regs, struct displaced_step_closure *dsc,
5577                  ULONGEST val)
5578 {
5579   if (!dsc->is_thumb)
5580     /* Note: If bits 0/1 are set, this branch would be unpredictable for
5581        architecture versions < 6.  */
5582     regcache_cooked_write_unsigned (regs, ARM_PC_REGNUM,
5583                                     val & ~(ULONGEST) 0x3);
5584   else
5585     regcache_cooked_write_unsigned (regs, ARM_PC_REGNUM,
5586                                     val & ~(ULONGEST) 0x1);
5587 }
5588
5589 /* Write to the PC as from a branch-exchange instruction.  */
5590
5591 static void
5592 bx_write_pc (struct regcache *regs, ULONGEST val)
5593 {
5594   ULONGEST ps;
5595   ULONGEST t_bit = arm_psr_thumb_bit (get_regcache_arch (regs));
5596
5597   regcache_cooked_read_unsigned (regs, ARM_PS_REGNUM, &ps);
5598
5599   if ((val & 1) == 1)
5600     {
5601       regcache_cooked_write_unsigned (regs, ARM_PS_REGNUM, ps | t_bit);
5602       regcache_cooked_write_unsigned (regs, ARM_PC_REGNUM, val & 0xfffffffe);
5603     }
5604   else if ((val & 2) == 0)
5605     {
5606       regcache_cooked_write_unsigned (regs, ARM_PS_REGNUM, ps & ~t_bit);
5607       regcache_cooked_write_unsigned (regs, ARM_PC_REGNUM, val);
5608     }
5609   else
5610     {
5611       /* Unpredictable behaviour.  Try to do something sensible (switch to ARM
5612           mode, align dest to 4 bytes).  */
5613       warning (_("Single-stepping BX to non-word-aligned ARM instruction."));
5614       regcache_cooked_write_unsigned (regs, ARM_PS_REGNUM, ps & ~t_bit);
5615       regcache_cooked_write_unsigned (regs, ARM_PC_REGNUM, val & 0xfffffffc);
5616     }
5617 }
5618
5619 /* Write to the PC as if from a load instruction.  */
5620
5621 static void
5622 load_write_pc (struct regcache *regs, struct displaced_step_closure *dsc,
5623                ULONGEST val)
5624 {
5625   if (DISPLACED_STEPPING_ARCH_VERSION >= 5)
5626     bx_write_pc (regs, val);
5627   else
5628     branch_write_pc (regs, dsc, val);
5629 }
5630
5631 /* Write to the PC as if from an ALU instruction.  */
5632
5633 static void
5634 alu_write_pc (struct regcache *regs, struct displaced_step_closure *dsc,
5635               ULONGEST val)
5636 {
5637   if (DISPLACED_STEPPING_ARCH_VERSION >= 7 && !dsc->is_thumb)
5638     bx_write_pc (regs, val);
5639   else
5640     branch_write_pc (regs, dsc, val);
5641 }
5642
5643 /* Helper for writing to registers for displaced stepping.  Writing to the PC
5644    has a varying effects depending on the instruction which does the write:
5645    this is controlled by the WRITE_PC argument.  */
5646
5647 void
5648 displaced_write_reg (struct regcache *regs, struct displaced_step_closure *dsc,
5649                      int regno, ULONGEST val, enum pc_write_style write_pc)
5650 {
5651   if (regno == ARM_PC_REGNUM)
5652     {
5653       if (debug_displaced)
5654         fprintf_unfiltered (gdb_stdlog, "displaced: writing pc %.8lx\n",
5655                             (unsigned long) val);
5656       switch (write_pc)
5657         {
5658         case BRANCH_WRITE_PC:
5659           branch_write_pc (regs, dsc, val);
5660           break;
5661
5662         case BX_WRITE_PC:
5663           bx_write_pc (regs, val);
5664           break;
5665
5666         case LOAD_WRITE_PC:
5667           load_write_pc (regs, dsc, val);
5668           break;
5669
5670         case ALU_WRITE_PC:
5671           alu_write_pc (regs, dsc, val);
5672           break;
5673
5674         case CANNOT_WRITE_PC:
5675           warning (_("Instruction wrote to PC in an unexpected way when "
5676                      "single-stepping"));
5677           break;
5678
5679         default:
5680           internal_error (__FILE__, __LINE__,
5681                           _("Invalid argument to displaced_write_reg"));
5682         }
5683
5684       dsc->wrote_to_pc = 1;
5685     }
5686   else
5687     {
5688       if (debug_displaced)
5689         fprintf_unfiltered (gdb_stdlog, "displaced: writing r%d value %.8lx\n",
5690                             regno, (unsigned long) val);
5691       regcache_cooked_write_unsigned (regs, regno, val);
5692     }
5693 }
5694
5695 /* This function is used to concisely determine if an instruction INSN
5696    references PC.  Register fields of interest in INSN should have the
5697    corresponding fields of BITMASK set to 0b1111.  The function
5698    returns return 1 if any of these fields in INSN reference the PC
5699    (also 0b1111, r15), else it returns 0.  */
5700
5701 static int
5702 insn_references_pc (uint32_t insn, uint32_t bitmask)
5703 {
5704   uint32_t lowbit = 1;
5705
5706   while (bitmask != 0)
5707     {
5708       uint32_t mask;
5709
5710       for (; lowbit && (bitmask & lowbit) == 0; lowbit <<= 1)
5711         ;
5712
5713       if (!lowbit)
5714         break;
5715
5716       mask = lowbit * 0xf;
5717
5718       if ((insn & mask) == mask)
5719         return 1;
5720
5721       bitmask &= ~mask;
5722     }
5723
5724   return 0;
5725 }
5726
5727 /* The simplest copy function.  Many instructions have the same effect no
5728    matter what address they are executed at: in those cases, use this.  */
5729
5730 static int
5731 arm_copy_unmodified (struct gdbarch *gdbarch, uint32_t insn,
5732                      const char *iname, struct displaced_step_closure *dsc)
5733 {
5734   if (debug_displaced)
5735     fprintf_unfiltered (gdb_stdlog, "displaced: copying insn %.8lx, "
5736                         "opcode/class '%s' unmodified\n", (unsigned long) insn,
5737                         iname);
5738
5739   dsc->modinsn[0] = insn;
5740
5741   return 0;
5742 }
5743
5744 static int
5745 thumb_copy_unmodified_32bit (struct gdbarch *gdbarch, uint16_t insn1,
5746                              uint16_t insn2, const char *iname,
5747                              struct displaced_step_closure *dsc)
5748 {
5749   if (debug_displaced)
5750     fprintf_unfiltered (gdb_stdlog, "displaced: copying insn %.4x %.4x, "
5751                         "opcode/class '%s' unmodified\n", insn1, insn2,
5752                         iname);
5753
5754   dsc->modinsn[0] = insn1;
5755   dsc->modinsn[1] = insn2;
5756   dsc->numinsns = 2;
5757
5758   return 0;
5759 }
5760
5761 /* Copy 16-bit Thumb(Thumb and 16-bit Thumb-2) instruction without any
5762    modification.  */
5763 static int
5764 thumb_copy_unmodified_16bit (struct gdbarch *gdbarch, unsigned int insn,
5765                              const char *iname,
5766                              struct displaced_step_closure *dsc)
5767 {
5768   if (debug_displaced)
5769     fprintf_unfiltered (gdb_stdlog, "displaced: copying insn %.4x, "
5770                         "opcode/class '%s' unmodified\n", insn,
5771                         iname);
5772
5773   dsc->modinsn[0] = insn;
5774
5775   return 0;
5776 }
5777
5778 /* Preload instructions with immediate offset.  */
5779
5780 static void
5781 cleanup_preload (struct gdbarch *gdbarch,
5782                  struct regcache *regs, struct displaced_step_closure *dsc)
5783 {
5784   displaced_write_reg (regs, dsc, 0, dsc->tmp[0], CANNOT_WRITE_PC);
5785   if (!dsc->u.preload.immed)
5786     displaced_write_reg (regs, dsc, 1, dsc->tmp[1], CANNOT_WRITE_PC);
5787 }
5788
5789 static void
5790 install_preload (struct gdbarch *gdbarch, struct regcache *regs,
5791                  struct displaced_step_closure *dsc, unsigned int rn)
5792 {
5793   ULONGEST rn_val;
5794   /* Preload instructions:
5795
5796      {pli/pld} [rn, #+/-imm]
5797      ->
5798      {pli/pld} [r0, #+/-imm].  */
5799
5800   dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
5801   rn_val = displaced_read_reg (regs, dsc, rn);
5802   displaced_write_reg (regs, dsc, 0, rn_val, CANNOT_WRITE_PC);
5803   dsc->u.preload.immed = 1;
5804
5805   dsc->cleanup = &cleanup_preload;
5806 }
5807
5808 static int
5809 arm_copy_preload (struct gdbarch *gdbarch, uint32_t insn, struct regcache *regs,
5810                   struct displaced_step_closure *dsc)
5811 {
5812   unsigned int rn = bits (insn, 16, 19);
5813
5814   if (!insn_references_pc (insn, 0x000f0000ul))
5815     return arm_copy_unmodified (gdbarch, insn, "preload", dsc);
5816
5817   if (debug_displaced)
5818     fprintf_unfiltered (gdb_stdlog, "displaced: copying preload insn %.8lx\n",
5819                         (unsigned long) insn);
5820
5821   dsc->modinsn[0] = insn & 0xfff0ffff;
5822
5823   install_preload (gdbarch, regs, dsc, rn);
5824
5825   return 0;
5826 }
5827
5828 static int
5829 thumb2_copy_preload (struct gdbarch *gdbarch, uint16_t insn1, uint16_t insn2,
5830                      struct regcache *regs, struct displaced_step_closure *dsc)
5831 {
5832   unsigned int rn = bits (insn1, 0, 3);
5833   unsigned int u_bit = bit (insn1, 7);
5834   int imm12 = bits (insn2, 0, 11);
5835   ULONGEST pc_val;
5836
5837   if (rn != ARM_PC_REGNUM)
5838     return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2, "preload", dsc);
5839
5840   /* PC is only allowed to use in PLI (immediate,literal) Encoding T3, and
5841      PLD (literal) Encoding T1.  */
5842   if (debug_displaced)
5843     fprintf_unfiltered (gdb_stdlog,
5844                         "displaced: copying pld/pli pc (0x%x) %c imm12 %.4x\n",
5845                         (unsigned int) dsc->insn_addr, u_bit ? '+' : '-',
5846                         imm12);
5847
5848   if (!u_bit)
5849     imm12 = -1 * imm12;
5850
5851   /* Rewrite instruction {pli/pld} PC imm12 into:
5852      Prepare: tmp[0] <- r0, tmp[1] <- r1, r0 <- pc, r1 <- imm12
5853
5854      {pli/pld} [r0, r1]
5855
5856      Cleanup: r0 <- tmp[0], r1 <- tmp[1].  */
5857
5858   dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
5859   dsc->tmp[1] = displaced_read_reg (regs, dsc, 1);
5860
5861   pc_val = displaced_read_reg (regs, dsc, ARM_PC_REGNUM);
5862
5863   displaced_write_reg (regs, dsc, 0, pc_val, CANNOT_WRITE_PC);
5864   displaced_write_reg (regs, dsc, 1, imm12, CANNOT_WRITE_PC);
5865   dsc->u.preload.immed = 0;
5866
5867   /* {pli/pld} [r0, r1] */
5868   dsc->modinsn[0] = insn1 & 0xfff0;
5869   dsc->modinsn[1] = 0xf001;
5870   dsc->numinsns = 2;
5871
5872   dsc->cleanup = &cleanup_preload;
5873   return 0;
5874 }
5875
5876 /* Preload instructions with register offset.  */
5877
5878 static void
5879 install_preload_reg(struct gdbarch *gdbarch, struct regcache *regs,
5880                     struct displaced_step_closure *dsc, unsigned int rn,
5881                     unsigned int rm)
5882 {
5883   ULONGEST rn_val, rm_val;
5884
5885   /* Preload register-offset instructions:
5886
5887      {pli/pld} [rn, rm {, shift}]
5888      ->
5889      {pli/pld} [r0, r1 {, shift}].  */
5890
5891   dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
5892   dsc->tmp[1] = displaced_read_reg (regs, dsc, 1);
5893   rn_val = displaced_read_reg (regs, dsc, rn);
5894   rm_val = displaced_read_reg (regs, dsc, rm);
5895   displaced_write_reg (regs, dsc, 0, rn_val, CANNOT_WRITE_PC);
5896   displaced_write_reg (regs, dsc, 1, rm_val, CANNOT_WRITE_PC);
5897   dsc->u.preload.immed = 0;
5898
5899   dsc->cleanup = &cleanup_preload;
5900 }
5901
5902 static int
5903 arm_copy_preload_reg (struct gdbarch *gdbarch, uint32_t insn,
5904                       struct regcache *regs,
5905                       struct displaced_step_closure *dsc)
5906 {
5907   unsigned int rn = bits (insn, 16, 19);
5908   unsigned int rm = bits (insn, 0, 3);
5909
5910
5911   if (!insn_references_pc (insn, 0x000f000ful))
5912     return arm_copy_unmodified (gdbarch, insn, "preload reg", dsc);
5913
5914   if (debug_displaced)
5915     fprintf_unfiltered (gdb_stdlog, "displaced: copying preload insn %.8lx\n",
5916                         (unsigned long) insn);
5917
5918   dsc->modinsn[0] = (insn & 0xfff0fff0) | 0x1;
5919
5920   install_preload_reg (gdbarch, regs, dsc, rn, rm);
5921   return 0;
5922 }
5923
5924 /* Copy/cleanup coprocessor load and store instructions.  */
5925
5926 static void
5927 cleanup_copro_load_store (struct gdbarch *gdbarch,
5928                           struct regcache *regs,
5929                           struct displaced_step_closure *dsc)
5930 {
5931   ULONGEST rn_val = displaced_read_reg (regs, dsc, 0);
5932
5933   displaced_write_reg (regs, dsc, 0, dsc->tmp[0], CANNOT_WRITE_PC);
5934
5935   if (dsc->u.ldst.writeback)
5936     displaced_write_reg (regs, dsc, dsc->u.ldst.rn, rn_val, LOAD_WRITE_PC);
5937 }
5938
5939 static void
5940 install_copro_load_store (struct gdbarch *gdbarch, struct regcache *regs,
5941                           struct displaced_step_closure *dsc,
5942                           int writeback, unsigned int rn)
5943 {
5944   ULONGEST rn_val;
5945
5946   /* Coprocessor load/store instructions:
5947
5948      {stc/stc2} [<Rn>, #+/-imm]  (and other immediate addressing modes)
5949      ->
5950      {stc/stc2} [r0, #+/-imm].
5951
5952      ldc/ldc2 are handled identically.  */
5953
5954   dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
5955   rn_val = displaced_read_reg (regs, dsc, rn);
5956   /* PC should be 4-byte aligned.  */
5957   rn_val = rn_val & 0xfffffffc;
5958   displaced_write_reg (regs, dsc, 0, rn_val, CANNOT_WRITE_PC);
5959
5960   dsc->u.ldst.writeback = writeback;
5961   dsc->u.ldst.rn = rn;
5962
5963   dsc->cleanup = &cleanup_copro_load_store;
5964 }
5965
5966 static int
5967 arm_copy_copro_load_store (struct gdbarch *gdbarch, uint32_t insn,
5968                            struct regcache *regs,
5969                            struct displaced_step_closure *dsc)
5970 {
5971   unsigned int rn = bits (insn, 16, 19);
5972
5973   if (!insn_references_pc (insn, 0x000f0000ul))
5974     return arm_copy_unmodified (gdbarch, insn, "copro load/store", dsc);
5975
5976   if (debug_displaced)
5977     fprintf_unfiltered (gdb_stdlog, "displaced: copying coprocessor "
5978                         "load/store insn %.8lx\n", (unsigned long) insn);
5979
5980   dsc->modinsn[0] = insn & 0xfff0ffff;
5981
5982   install_copro_load_store (gdbarch, regs, dsc, bit (insn, 25), rn);
5983
5984   return 0;
5985 }
5986
5987 static int
5988 thumb2_copy_copro_load_store (struct gdbarch *gdbarch, uint16_t insn1,
5989                               uint16_t insn2, struct regcache *regs,
5990                               struct displaced_step_closure *dsc)
5991 {
5992   unsigned int rn = bits (insn1, 0, 3);
5993
5994   if (rn != ARM_PC_REGNUM)
5995     return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
5996                                         "copro load/store", dsc);
5997
5998   if (debug_displaced)
5999     fprintf_unfiltered (gdb_stdlog, "displaced: copying coprocessor "
6000                         "load/store insn %.4x%.4x\n", insn1, insn2);
6001
6002   dsc->modinsn[0] = insn1 & 0xfff0;
6003   dsc->modinsn[1] = insn2;
6004   dsc->numinsns = 2;
6005
6006   /* This function is called for copying instruction LDC/LDC2/VLDR, which
6007      doesn't support writeback, so pass 0.  */
6008   install_copro_load_store (gdbarch, regs, dsc, 0, rn);
6009
6010   return 0;
6011 }
6012
6013 /* Clean up branch instructions (actually perform the branch, by setting
6014    PC).  */
6015
6016 static void
6017 cleanup_branch (struct gdbarch *gdbarch, struct regcache *regs,
6018                 struct displaced_step_closure *dsc)
6019 {
6020   uint32_t status = displaced_read_reg (regs, dsc, ARM_PS_REGNUM);
6021   int branch_taken = condition_true (dsc->u.branch.cond, status);
6022   enum pc_write_style write_pc = dsc->u.branch.exchange
6023                                  ? BX_WRITE_PC : BRANCH_WRITE_PC;
6024
6025   if (!branch_taken)
6026     return;
6027
6028   if (dsc->u.branch.link)
6029     {
6030       /* The value of LR should be the next insn of current one.  In order
6031        not to confuse logic hanlding later insn `bx lr', if current insn mode
6032        is Thumb, the bit 0 of LR value should be set to 1.  */
6033       ULONGEST next_insn_addr = dsc->insn_addr + dsc->insn_size;
6034
6035       if (dsc->is_thumb)
6036         next_insn_addr |= 0x1;
6037
6038       displaced_write_reg (regs, dsc, ARM_LR_REGNUM, next_insn_addr,
6039                            CANNOT_WRITE_PC);
6040     }
6041
6042   displaced_write_reg (regs, dsc, ARM_PC_REGNUM, dsc->u.branch.dest, write_pc);
6043 }
6044
6045 /* Copy B/BL/BLX instructions with immediate destinations.  */
6046
6047 static void
6048 install_b_bl_blx (struct gdbarch *gdbarch, struct regcache *regs,
6049                   struct displaced_step_closure *dsc,
6050                   unsigned int cond, int exchange, int link, long offset)
6051 {
6052   /* Implement "BL<cond> <label>" as:
6053
6054      Preparation: cond <- instruction condition
6055      Insn: mov r0, r0  (nop)
6056      Cleanup: if (condition true) { r14 <- pc; pc <- label }.
6057
6058      B<cond> similar, but don't set r14 in cleanup.  */
6059
6060   dsc->u.branch.cond = cond;
6061   dsc->u.branch.link = link;
6062   dsc->u.branch.exchange = exchange;
6063
6064   dsc->u.branch.dest = dsc->insn_addr;
6065   if (link && exchange)
6066     /* For BLX, offset is computed from the Align (PC, 4).  */
6067     dsc->u.branch.dest = dsc->u.branch.dest & 0xfffffffc;
6068
6069   if (dsc->is_thumb)
6070     dsc->u.branch.dest += 4 + offset;
6071   else
6072     dsc->u.branch.dest += 8 + offset;
6073
6074   dsc->cleanup = &cleanup_branch;
6075 }
6076 static int
6077 arm_copy_b_bl_blx (struct gdbarch *gdbarch, uint32_t insn,
6078                    struct regcache *regs, struct displaced_step_closure *dsc)
6079 {
6080   unsigned int cond = bits (insn, 28, 31);
6081   int exchange = (cond == 0xf);
6082   int link = exchange || bit (insn, 24);
6083   long offset;
6084
6085   if (debug_displaced)
6086     fprintf_unfiltered (gdb_stdlog, "displaced: copying %s immediate insn "
6087                         "%.8lx\n", (exchange) ? "blx" : (link) ? "bl" : "b",
6088                         (unsigned long) insn);
6089   if (exchange)
6090     /* For BLX, set bit 0 of the destination.  The cleanup_branch function will
6091        then arrange the switch into Thumb mode.  */
6092     offset = (bits (insn, 0, 23) << 2) | (bit (insn, 24) << 1) | 1;
6093   else
6094     offset = bits (insn, 0, 23) << 2;
6095
6096   if (bit (offset, 25))
6097     offset = offset | ~0x3ffffff;
6098
6099   dsc->modinsn[0] = ARM_NOP;
6100
6101   install_b_bl_blx (gdbarch, regs, dsc, cond, exchange, link, offset);
6102   return 0;
6103 }
6104
6105 static int
6106 thumb2_copy_b_bl_blx (struct gdbarch *gdbarch, uint16_t insn1,
6107                       uint16_t insn2, struct regcache *regs,
6108                       struct displaced_step_closure *dsc)
6109 {
6110   int link = bit (insn2, 14);
6111   int exchange = link && !bit (insn2, 12);
6112   int cond = INST_AL;
6113   long offset = 0;
6114   int j1 = bit (insn2, 13);
6115   int j2 = bit (insn2, 11);
6116   int s = sbits (insn1, 10, 10);
6117   int i1 = !(j1 ^ bit (insn1, 10));
6118   int i2 = !(j2 ^ bit (insn1, 10));
6119
6120   if (!link && !exchange) /* B */
6121     {
6122       offset = (bits (insn2, 0, 10) << 1);
6123       if (bit (insn2, 12)) /* Encoding T4 */
6124         {
6125           offset |= (bits (insn1, 0, 9) << 12)
6126             | (i2 << 22)
6127             | (i1 << 23)
6128             | (s << 24);
6129           cond = INST_AL;
6130         }
6131       else /* Encoding T3 */
6132         {
6133           offset |= (bits (insn1, 0, 5) << 12)
6134             | (j1 << 18)
6135             | (j2 << 19)
6136             | (s << 20);
6137           cond = bits (insn1, 6, 9);
6138         }
6139     }
6140   else
6141     {
6142       offset = (bits (insn1, 0, 9) << 12);
6143       offset |= ((i2 << 22) | (i1 << 23) | (s << 24));
6144       offset |= exchange ?
6145         (bits (insn2, 1, 10) << 2) : (bits (insn2, 0, 10) << 1);
6146     }
6147
6148   if (debug_displaced)
6149     fprintf_unfiltered (gdb_stdlog, "displaced: copying %s insn "
6150                         "%.4x %.4x with offset %.8lx\n",
6151                         link ? (exchange) ? "blx" : "bl" : "b",
6152                         insn1, insn2, offset);
6153
6154   dsc->modinsn[0] = THUMB_NOP;
6155
6156   install_b_bl_blx (gdbarch, regs, dsc, cond, exchange, link, offset);
6157   return 0;
6158 }
6159
6160 /* Copy B Thumb instructions.  */
6161 static int
6162 thumb_copy_b (struct gdbarch *gdbarch, unsigned short insn,
6163               struct displaced_step_closure *dsc)
6164 {
6165   unsigned int cond = 0;
6166   int offset = 0;
6167   unsigned short bit_12_15 = bits (insn, 12, 15);
6168   CORE_ADDR from = dsc->insn_addr;
6169
6170   if (bit_12_15 == 0xd)
6171     {
6172       /* offset = SignExtend (imm8:0, 32) */
6173       offset = sbits ((insn << 1), 0, 8);
6174       cond = bits (insn, 8, 11);
6175     }
6176   else if (bit_12_15 == 0xe) /* Encoding T2 */
6177     {
6178       offset = sbits ((insn << 1), 0, 11);
6179       cond = INST_AL;
6180     }
6181
6182   if (debug_displaced)
6183     fprintf_unfiltered (gdb_stdlog,
6184                         "displaced: copying b immediate insn %.4x "
6185                         "with offset %d\n", insn, offset);
6186
6187   dsc->u.branch.cond = cond;
6188   dsc->u.branch.link = 0;
6189   dsc->u.branch.exchange = 0;
6190   dsc->u.branch.dest = from + 4 + offset;
6191
6192   dsc->modinsn[0] = THUMB_NOP;
6193
6194   dsc->cleanup = &cleanup_branch;
6195
6196   return 0;
6197 }
6198
6199 /* Copy BX/BLX with register-specified destinations.  */
6200
6201 static void
6202 install_bx_blx_reg (struct gdbarch *gdbarch, struct regcache *regs,
6203                     struct displaced_step_closure *dsc, int link,
6204                     unsigned int cond, unsigned int rm)
6205 {
6206   /* Implement {BX,BLX}<cond> <reg>" as:
6207
6208      Preparation: cond <- instruction condition
6209      Insn: mov r0, r0 (nop)
6210      Cleanup: if (condition true) { r14 <- pc; pc <- dest; }.
6211
6212      Don't set r14 in cleanup for BX.  */
6213
6214   dsc->u.branch.dest = displaced_read_reg (regs, dsc, rm);
6215
6216   dsc->u.branch.cond = cond;
6217   dsc->u.branch.link = link;
6218
6219   dsc->u.branch.exchange = 1;
6220
6221   dsc->cleanup = &cleanup_branch;
6222 }
6223
6224 static int
6225 arm_copy_bx_blx_reg (struct gdbarch *gdbarch, uint32_t insn,
6226                      struct regcache *regs, struct displaced_step_closure *dsc)
6227 {
6228   unsigned int cond = bits (insn, 28, 31);
6229   /* BX:  x12xxx1x
6230      BLX: x12xxx3x.  */
6231   int link = bit (insn, 5);
6232   unsigned int rm = bits (insn, 0, 3);
6233
6234   if (debug_displaced)
6235     fprintf_unfiltered (gdb_stdlog, "displaced: copying insn %.8lx",
6236                         (unsigned long) insn);
6237
6238   dsc->modinsn[0] = ARM_NOP;
6239
6240   install_bx_blx_reg (gdbarch, regs, dsc, link, cond, rm);
6241   return 0;
6242 }
6243
6244 static int
6245 thumb_copy_bx_blx_reg (struct gdbarch *gdbarch, uint16_t insn,
6246                        struct regcache *regs,
6247                        struct displaced_step_closure *dsc)
6248 {
6249   int link = bit (insn, 7);
6250   unsigned int rm = bits (insn, 3, 6);
6251
6252   if (debug_displaced)
6253     fprintf_unfiltered (gdb_stdlog, "displaced: copying insn %.4x",
6254                         (unsigned short) insn);
6255
6256   dsc->modinsn[0] = THUMB_NOP;
6257
6258   install_bx_blx_reg (gdbarch, regs, dsc, link, INST_AL, rm);
6259
6260   return 0;
6261 }
6262
6263
6264 /* Copy/cleanup arithmetic/logic instruction with immediate RHS.  */
6265
6266 static void
6267 cleanup_alu_imm (struct gdbarch *gdbarch,
6268                  struct regcache *regs, struct displaced_step_closure *dsc)
6269 {
6270   ULONGEST rd_val = displaced_read_reg (regs, dsc, 0);
6271   displaced_write_reg (regs, dsc, 0, dsc->tmp[0], CANNOT_WRITE_PC);
6272   displaced_write_reg (regs, dsc, 1, dsc->tmp[1], CANNOT_WRITE_PC);
6273   displaced_write_reg (regs, dsc, dsc->rd, rd_val, ALU_WRITE_PC);
6274 }
6275
6276 static int
6277 arm_copy_alu_imm (struct gdbarch *gdbarch, uint32_t insn, struct regcache *regs,
6278                   struct displaced_step_closure *dsc)
6279 {
6280   unsigned int rn = bits (insn, 16, 19);
6281   unsigned int rd = bits (insn, 12, 15);
6282   unsigned int op = bits (insn, 21, 24);
6283   int is_mov = (op == 0xd);
6284   ULONGEST rd_val, rn_val;
6285
6286   if (!insn_references_pc (insn, 0x000ff000ul))
6287     return arm_copy_unmodified (gdbarch, insn, "ALU immediate", dsc);
6288
6289   if (debug_displaced)
6290     fprintf_unfiltered (gdb_stdlog, "displaced: copying immediate %s insn "
6291                         "%.8lx\n", is_mov ? "move" : "ALU",
6292                         (unsigned long) insn);
6293
6294   /* Instruction is of form:
6295
6296      <op><cond> rd, [rn,] #imm
6297
6298      Rewrite as:
6299
6300      Preparation: tmp1, tmp2 <- r0, r1;
6301                   r0, r1 <- rd, rn
6302      Insn: <op><cond> r0, r1, #imm
6303      Cleanup: rd <- r0; r0 <- tmp1; r1 <- tmp2
6304   */
6305
6306   dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
6307   dsc->tmp[1] = displaced_read_reg (regs, dsc, 1);
6308   rn_val = displaced_read_reg (regs, dsc, rn);
6309   rd_val = displaced_read_reg (regs, dsc, rd);
6310   displaced_write_reg (regs, dsc, 0, rd_val, CANNOT_WRITE_PC);
6311   displaced_write_reg (regs, dsc, 1, rn_val, CANNOT_WRITE_PC);
6312   dsc->rd = rd;
6313
6314   if (is_mov)
6315     dsc->modinsn[0] = insn & 0xfff00fff;
6316   else
6317     dsc->modinsn[0] = (insn & 0xfff00fff) | 0x10000;
6318
6319   dsc->cleanup = &cleanup_alu_imm;
6320
6321   return 0;
6322 }
6323
6324 static int
6325 thumb2_copy_alu_imm (struct gdbarch *gdbarch, uint16_t insn1,
6326                      uint16_t insn2, struct regcache *regs,
6327                      struct displaced_step_closure *dsc)
6328 {
6329   unsigned int op = bits (insn1, 5, 8);
6330   unsigned int rn, rm, rd;
6331   ULONGEST rd_val, rn_val;
6332
6333   rn = bits (insn1, 0, 3); /* Rn */
6334   rm = bits (insn2, 0, 3); /* Rm */
6335   rd = bits (insn2, 8, 11); /* Rd */
6336
6337   /* This routine is only called for instruction MOV.  */
6338   gdb_assert (op == 0x2 && rn == 0xf);
6339
6340   if (rm != ARM_PC_REGNUM && rd != ARM_PC_REGNUM)
6341     return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2, "ALU imm", dsc);
6342
6343   if (debug_displaced)
6344     fprintf_unfiltered (gdb_stdlog, "displaced: copying reg %s insn %.4x%.4x\n",
6345                         "ALU", insn1, insn2);
6346
6347   /* Instruction is of form:
6348
6349      <op><cond> rd, [rn,] #imm
6350
6351      Rewrite as:
6352
6353      Preparation: tmp1, tmp2 <- r0, r1;
6354                   r0, r1 <- rd, rn
6355      Insn: <op><cond> r0, r1, #imm
6356      Cleanup: rd <- r0; r0 <- tmp1; r1 <- tmp2
6357   */
6358
6359   dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
6360   dsc->tmp[1] = displaced_read_reg (regs, dsc, 1);
6361   rn_val = displaced_read_reg (regs, dsc, rn);
6362   rd_val = displaced_read_reg (regs, dsc, rd);
6363   displaced_write_reg (regs, dsc, 0, rd_val, CANNOT_WRITE_PC);
6364   displaced_write_reg (regs, dsc, 1, rn_val, CANNOT_WRITE_PC);
6365   dsc->rd = rd;
6366
6367   dsc->modinsn[0] = insn1;
6368   dsc->modinsn[1] = ((insn2 & 0xf0f0) | 0x1);
6369   dsc->numinsns = 2;
6370
6371   dsc->cleanup = &cleanup_alu_imm;
6372
6373   return 0;
6374 }
6375
6376 /* Copy/cleanup arithmetic/logic insns with register RHS.  */
6377
6378 static void
6379 cleanup_alu_reg (struct gdbarch *gdbarch,
6380                  struct regcache *regs, struct displaced_step_closure *dsc)
6381 {
6382   ULONGEST rd_val;
6383   int i;
6384
6385   rd_val = displaced_read_reg (regs, dsc, 0);
6386
6387   for (i = 0; i < 3; i++)
6388     displaced_write_reg (regs, dsc, i, dsc->tmp[i], CANNOT_WRITE_PC);
6389
6390   displaced_write_reg (regs, dsc, dsc->rd, rd_val, ALU_WRITE_PC);
6391 }
6392
6393 static void
6394 install_alu_reg (struct gdbarch *gdbarch, struct regcache *regs,
6395                  struct displaced_step_closure *dsc,
6396                  unsigned int rd, unsigned int rn, unsigned int rm)
6397 {
6398   ULONGEST rd_val, rn_val, rm_val;
6399
6400   /* Instruction is of form:
6401
6402      <op><cond> rd, [rn,] rm [, <shift>]
6403
6404      Rewrite as:
6405
6406      Preparation: tmp1, tmp2, tmp3 <- r0, r1, r2;
6407                   r0, r1, r2 <- rd, rn, rm
6408      Insn: <op><cond> r0, [r1,] r2 [, <shift>]
6409      Cleanup: rd <- r0; r0, r1, r2 <- tmp1, tmp2, tmp3
6410   */
6411
6412   dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
6413   dsc->tmp[1] = displaced_read_reg (regs, dsc, 1);
6414   dsc->tmp[2] = displaced_read_reg (regs, dsc, 2);
6415   rd_val = displaced_read_reg (regs, dsc, rd);
6416   rn_val = displaced_read_reg (regs, dsc, rn);
6417   rm_val = displaced_read_reg (regs, dsc, rm);
6418   displaced_write_reg (regs, dsc, 0, rd_val, CANNOT_WRITE_PC);
6419   displaced_write_reg (regs, dsc, 1, rn_val, CANNOT_WRITE_PC);
6420   displaced_write_reg (regs, dsc, 2, rm_val, CANNOT_WRITE_PC);
6421   dsc->rd = rd;
6422
6423   dsc->cleanup = &cleanup_alu_reg;
6424 }
6425
6426 static int
6427 arm_copy_alu_reg (struct gdbarch *gdbarch, uint32_t insn, struct regcache *regs,
6428                   struct displaced_step_closure *dsc)
6429 {
6430   unsigned int op = bits (insn, 21, 24);
6431   int is_mov = (op == 0xd);
6432
6433   if (!insn_references_pc (insn, 0x000ff00ful))
6434     return arm_copy_unmodified (gdbarch, insn, "ALU reg", dsc);
6435
6436   if (debug_displaced)
6437     fprintf_unfiltered (gdb_stdlog, "displaced: copying reg %s insn %.8lx\n",
6438                         is_mov ? "move" : "ALU", (unsigned long) insn);
6439
6440   if (is_mov)
6441     dsc->modinsn[0] = (insn & 0xfff00ff0) | 0x2;
6442   else
6443     dsc->modinsn[0] = (insn & 0xfff00ff0) | 0x10002;
6444
6445   install_alu_reg (gdbarch, regs, dsc, bits (insn, 12, 15), bits (insn, 16, 19),
6446                    bits (insn, 0, 3));
6447   return 0;
6448 }
6449
6450 static int
6451 thumb_copy_alu_reg (struct gdbarch *gdbarch, uint16_t insn,
6452                     struct regcache *regs,
6453                     struct displaced_step_closure *dsc)
6454 {
6455   unsigned rm, rd;
6456
6457   rm = bits (insn, 3, 6);
6458   rd = (bit (insn, 7) << 3) | bits (insn, 0, 2);
6459
6460   if (rd != ARM_PC_REGNUM && rm != ARM_PC_REGNUM)
6461     return thumb_copy_unmodified_16bit (gdbarch, insn, "ALU reg", dsc);
6462
6463   if (debug_displaced)
6464     fprintf_unfiltered (gdb_stdlog, "displaced: copying ALU reg insn %.4x\n",
6465                         (unsigned short) insn);
6466
6467   dsc->modinsn[0] = ((insn & 0xff00) | 0x10);
6468
6469   install_alu_reg (gdbarch, regs, dsc, rd, rd, rm);
6470
6471   return 0;
6472 }
6473
6474 /* Cleanup/copy arithmetic/logic insns with shifted register RHS.  */
6475
6476 static void
6477 cleanup_alu_shifted_reg (struct gdbarch *gdbarch,
6478                          struct regcache *regs,
6479                          struct displaced_step_closure *dsc)
6480 {
6481   ULONGEST rd_val = displaced_read_reg (regs, dsc, 0);
6482   int i;
6483
6484   for (i = 0; i < 4; i++)
6485     displaced_write_reg (regs, dsc, i, dsc->tmp[i], CANNOT_WRITE_PC);
6486
6487   displaced_write_reg (regs, dsc, dsc->rd, rd_val, ALU_WRITE_PC);
6488 }
6489
6490 static void
6491 install_alu_shifted_reg (struct gdbarch *gdbarch, struct regcache *regs,
6492                          struct displaced_step_closure *dsc,
6493                          unsigned int rd, unsigned int rn, unsigned int rm,
6494                          unsigned rs)
6495 {
6496   int i;
6497   ULONGEST rd_val, rn_val, rm_val, rs_val;
6498
6499   /* Instruction is of form:
6500
6501      <op><cond> rd, [rn,] rm, <shift> rs
6502
6503      Rewrite as:
6504
6505      Preparation: tmp1, tmp2, tmp3, tmp4 <- r0, r1, r2, r3
6506                   r0, r1, r2, r3 <- rd, rn, rm, rs
6507      Insn: <op><cond> r0, r1, r2, <shift> r3
6508      Cleanup: tmp5 <- r0
6509               r0, r1, r2, r3 <- tmp1, tmp2, tmp3, tmp4
6510               rd <- tmp5
6511   */
6512
6513   for (i = 0; i < 4; i++)
6514     dsc->tmp[i] = displaced_read_reg (regs, dsc, i);
6515
6516   rd_val = displaced_read_reg (regs, dsc, rd);
6517   rn_val = displaced_read_reg (regs, dsc, rn);
6518   rm_val = displaced_read_reg (regs, dsc, rm);
6519   rs_val = displaced_read_reg (regs, dsc, rs);
6520   displaced_write_reg (regs, dsc, 0, rd_val, CANNOT_WRITE_PC);
6521   displaced_write_reg (regs, dsc, 1, rn_val, CANNOT_WRITE_PC);
6522   displaced_write_reg (regs, dsc, 2, rm_val, CANNOT_WRITE_PC);
6523   displaced_write_reg (regs, dsc, 3, rs_val, CANNOT_WRITE_PC);
6524   dsc->rd = rd;
6525   dsc->cleanup = &cleanup_alu_shifted_reg;
6526 }
6527
6528 static int
6529 arm_copy_alu_shifted_reg (struct gdbarch *gdbarch, uint32_t insn,
6530                           struct regcache *regs,
6531                           struct displaced_step_closure *dsc)
6532 {
6533   unsigned int op = bits (insn, 21, 24);
6534   int is_mov = (op == 0xd);
6535   unsigned int rd, rn, rm, rs;
6536
6537   if (!insn_references_pc (insn, 0x000fff0ful))
6538     return arm_copy_unmodified (gdbarch, insn, "ALU shifted reg", dsc);
6539
6540   if (debug_displaced)
6541     fprintf_unfiltered (gdb_stdlog, "displaced: copying shifted reg %s insn "
6542                         "%.8lx\n", is_mov ? "move" : "ALU",
6543                         (unsigned long) insn);
6544
6545   rn = bits (insn, 16, 19);
6546   rm = bits (insn, 0, 3);
6547   rs = bits (insn, 8, 11);
6548   rd = bits (insn, 12, 15);
6549
6550   if (is_mov)
6551     dsc->modinsn[0] = (insn & 0xfff000f0) | 0x302;
6552   else
6553     dsc->modinsn[0] = (insn & 0xfff000f0) | 0x10302;
6554
6555   install_alu_shifted_reg (gdbarch, regs, dsc, rd, rn, rm, rs);
6556
6557   return 0;
6558 }
6559
6560 /* Clean up load instructions.  */
6561
6562 static void
6563 cleanup_load (struct gdbarch *gdbarch, struct regcache *regs,
6564               struct displaced_step_closure *dsc)
6565 {
6566   ULONGEST rt_val, rt_val2 = 0, rn_val;
6567
6568   rt_val = displaced_read_reg (regs, dsc, 0);
6569   if (dsc->u.ldst.xfersize == 8)
6570     rt_val2 = displaced_read_reg (regs, dsc, 1);
6571   rn_val = displaced_read_reg (regs, dsc, 2);
6572
6573   displaced_write_reg (regs, dsc, 0, dsc->tmp[0], CANNOT_WRITE_PC);
6574   if (dsc->u.ldst.xfersize > 4)
6575     displaced_write_reg (regs, dsc, 1, dsc->tmp[1], CANNOT_WRITE_PC);
6576   displaced_write_reg (regs, dsc, 2, dsc->tmp[2], CANNOT_WRITE_PC);
6577   if (!dsc->u.ldst.immed)
6578     displaced_write_reg (regs, dsc, 3, dsc->tmp[3], CANNOT_WRITE_PC);
6579
6580   /* Handle register writeback.  */
6581   if (dsc->u.ldst.writeback)
6582     displaced_write_reg (regs, dsc, dsc->u.ldst.rn, rn_val, CANNOT_WRITE_PC);
6583   /* Put result in right place.  */
6584   displaced_write_reg (regs, dsc, dsc->rd, rt_val, LOAD_WRITE_PC);
6585   if (dsc->u.ldst.xfersize == 8)
6586     displaced_write_reg (regs, dsc, dsc->rd + 1, rt_val2, LOAD_WRITE_PC);
6587 }
6588
6589 /* Clean up store instructions.  */
6590
6591 static void
6592 cleanup_store (struct gdbarch *gdbarch, struct regcache *regs,
6593                struct displaced_step_closure *dsc)
6594 {
6595   ULONGEST rn_val = displaced_read_reg (regs, dsc, 2);
6596
6597   displaced_write_reg (regs, dsc, 0, dsc->tmp[0], CANNOT_WRITE_PC);
6598   if (dsc->u.ldst.xfersize > 4)
6599     displaced_write_reg (regs, dsc, 1, dsc->tmp[1], CANNOT_WRITE_PC);
6600   displaced_write_reg (regs, dsc, 2, dsc->tmp[2], CANNOT_WRITE_PC);
6601   if (!dsc->u.ldst.immed)
6602     displaced_write_reg (regs, dsc, 3, dsc->tmp[3], CANNOT_WRITE_PC);
6603   if (!dsc->u.ldst.restore_r4)
6604     displaced_write_reg (regs, dsc, 4, dsc->tmp[4], CANNOT_WRITE_PC);
6605
6606   /* Writeback.  */
6607   if (dsc->u.ldst.writeback)
6608     displaced_write_reg (regs, dsc, dsc->u.ldst.rn, rn_val, CANNOT_WRITE_PC);
6609 }
6610
6611 /* Copy "extra" load/store instructions.  These are halfword/doubleword
6612    transfers, which have a different encoding to byte/word transfers.  */
6613
6614 static int
6615 arm_copy_extra_ld_st (struct gdbarch *gdbarch, uint32_t insn, int unpriveleged,
6616                       struct regcache *regs, struct displaced_step_closure *dsc)
6617 {
6618   unsigned int op1 = bits (insn, 20, 24);
6619   unsigned int op2 = bits (insn, 5, 6);
6620   unsigned int rt = bits (insn, 12, 15);
6621   unsigned int rn = bits (insn, 16, 19);
6622   unsigned int rm = bits (insn, 0, 3);
6623   char load[12]     = {0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1};
6624   char bytesize[12] = {2, 2, 2, 2, 8, 1, 8, 1, 8, 2, 8, 2};
6625   int immed = (op1 & 0x4) != 0;
6626   int opcode;
6627   ULONGEST rt_val, rt_val2 = 0, rn_val, rm_val = 0;
6628
6629   if (!insn_references_pc (insn, 0x000ff00ful))
6630     return arm_copy_unmodified (gdbarch, insn, "extra load/store", dsc);
6631
6632   if (debug_displaced)
6633     fprintf_unfiltered (gdb_stdlog, "displaced: copying %sextra load/store "
6634                         "insn %.8lx\n", unpriveleged ? "unpriveleged " : "",
6635                         (unsigned long) insn);
6636
6637   opcode = ((op2 << 2) | (op1 & 0x1) | ((op1 & 0x4) >> 1)) - 4;
6638
6639   if (opcode < 0)
6640     internal_error (__FILE__, __LINE__,
6641                     _("copy_extra_ld_st: instruction decode error"));
6642
6643   dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
6644   dsc->tmp[1] = displaced_read_reg (regs, dsc, 1);
6645   dsc->tmp[2] = displaced_read_reg (regs, dsc, 2);
6646   if (!immed)
6647     dsc->tmp[3] = displaced_read_reg (regs, dsc, 3);
6648
6649   rt_val = displaced_read_reg (regs, dsc, rt);
6650   if (bytesize[opcode] == 8)
6651     rt_val2 = displaced_read_reg (regs, dsc, rt + 1);
6652   rn_val = displaced_read_reg (regs, dsc, rn);
6653   if (!immed)
6654     rm_val = displaced_read_reg (regs, dsc, rm);
6655
6656   displaced_write_reg (regs, dsc, 0, rt_val, CANNOT_WRITE_PC);
6657   if (bytesize[opcode] == 8)
6658     displaced_write_reg (regs, dsc, 1, rt_val2, CANNOT_WRITE_PC);
6659   displaced_write_reg (regs, dsc, 2, rn_val, CANNOT_WRITE_PC);
6660   if (!immed)
6661     displaced_write_reg (regs, dsc, 3, rm_val, CANNOT_WRITE_PC);
6662
6663   dsc->rd = rt;
6664   dsc->u.ldst.xfersize = bytesize[opcode];
6665   dsc->u.ldst.rn = rn;
6666   dsc->u.ldst.immed = immed;
6667   dsc->u.ldst.writeback = bit (insn, 24) == 0 || bit (insn, 21) != 0;
6668   dsc->u.ldst.restore_r4 = 0;
6669
6670   if (immed)
6671     /* {ldr,str}<width><cond> rt, [rt2,] [rn, #imm]
6672         ->
6673        {ldr,str}<width><cond> r0, [r1,] [r2, #imm].  */
6674     dsc->modinsn[0] = (insn & 0xfff00fff) | 0x20000;
6675   else
6676     /* {ldr,str}<width><cond> rt, [rt2,] [rn, +/-rm]
6677         ->
6678        {ldr,str}<width><cond> r0, [r1,] [r2, +/-r3].  */
6679     dsc->modinsn[0] = (insn & 0xfff00ff0) | 0x20003;
6680
6681   dsc->cleanup = load[opcode] ? &cleanup_load : &cleanup_store;
6682
6683   return 0;
6684 }
6685
6686 /* Copy byte/half word/word loads and stores.  */
6687
6688 static void
6689 install_load_store (struct gdbarch *gdbarch, struct regcache *regs,
6690                     struct displaced_step_closure *dsc, int load,
6691                     int immed, int writeback, int size, int usermode,
6692                     int rt, int rm, int rn)
6693 {
6694   ULONGEST rt_val, rn_val, rm_val = 0;
6695
6696   dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
6697   dsc->tmp[2] = displaced_read_reg (regs, dsc, 2);
6698   if (!immed)
6699     dsc->tmp[3] = displaced_read_reg (regs, dsc, 3);
6700   if (!load)
6701     dsc->tmp[4] = displaced_read_reg (regs, dsc, 4);
6702
6703   rt_val = displaced_read_reg (regs, dsc, rt);
6704   rn_val = displaced_read_reg (regs, dsc, rn);
6705   if (!immed)
6706     rm_val = displaced_read_reg (regs, dsc, rm);
6707
6708   displaced_write_reg (regs, dsc, 0, rt_val, CANNOT_WRITE_PC);
6709   displaced_write_reg (regs, dsc, 2, rn_val, CANNOT_WRITE_PC);
6710   if (!immed)
6711     displaced_write_reg (regs, dsc, 3, rm_val, CANNOT_WRITE_PC);
6712   dsc->rd = rt;
6713   dsc->u.ldst.xfersize = size;
6714   dsc->u.ldst.rn = rn;
6715   dsc->u.ldst.immed = immed;
6716   dsc->u.ldst.writeback = writeback;
6717
6718   /* To write PC we can do:
6719
6720      Before this sequence of instructions:
6721      r0 is the PC value got from displaced_read_reg, so r0 = from + 8;
6722      r2 is the Rn value got from dispalced_read_reg.
6723
6724      Insn1: push {pc} Write address of STR instruction + offset on stack
6725      Insn2: pop  {r4} Read it back from stack, r4 = addr(Insn1) + offset
6726      Insn3: sub r4, r4, pc   r4 = addr(Insn1) + offset - pc
6727                                 = addr(Insn1) + offset - addr(Insn3) - 8
6728                                 = offset - 16
6729      Insn4: add r4, r4, #8   r4 = offset - 8
6730      Insn5: add r0, r0, r4   r0 = from + 8 + offset - 8
6731                                 = from + offset
6732      Insn6: str r0, [r2, #imm] (or str r0, [r2, r3])
6733
6734      Otherwise we don't know what value to write for PC, since the offset is
6735      architecture-dependent (sometimes PC+8, sometimes PC+12).  More details
6736      of this can be found in Section "Saving from r15" in
6737      http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dui0204g/Cihbjifh.html */
6738
6739   dsc->cleanup = load ? &cleanup_load : &cleanup_store;
6740 }
6741
6742
6743 static int
6744 thumb2_copy_load_literal (struct gdbarch *gdbarch, uint16_t insn1,
6745                           uint16_t insn2, struct regcache *regs,
6746                           struct displaced_step_closure *dsc, int size)
6747 {
6748   unsigned int u_bit = bit (insn1, 7);
6749   unsigned int rt = bits (insn2, 12, 15);
6750   int imm12 = bits (insn2, 0, 11);
6751   ULONGEST pc_val;
6752
6753   if (debug_displaced)
6754     fprintf_unfiltered (gdb_stdlog,
6755                         "displaced: copying ldr pc (0x%x) R%d %c imm12 %.4x\n",
6756                         (unsigned int) dsc->insn_addr, rt, u_bit ? '+' : '-',
6757                         imm12);
6758
6759   if (!u_bit)
6760     imm12 = -1 * imm12;
6761
6762   /* Rewrite instruction LDR Rt imm12 into:
6763
6764      Prepare: tmp[0] <- r0, tmp[1] <- r2, tmp[2] <- r3, r2 <- pc, r3 <- imm12
6765
6766      LDR R0, R2, R3,
6767
6768      Cleanup: rt <- r0, r0 <- tmp[0], r2 <- tmp[1], r3 <- tmp[2].  */
6769
6770
6771   dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
6772   dsc->tmp[2] = displaced_read_reg (regs, dsc, 2);
6773   dsc->tmp[3] = displaced_read_reg (regs, dsc, 3);
6774
6775   pc_val = displaced_read_reg (regs, dsc, ARM_PC_REGNUM);
6776
6777   pc_val = pc_val & 0xfffffffc;
6778
6779   displaced_write_reg (regs, dsc, 2, pc_val, CANNOT_WRITE_PC);
6780   displaced_write_reg (regs, dsc, 3, imm12, CANNOT_WRITE_PC);
6781
6782   dsc->rd = rt;
6783
6784   dsc->u.ldst.xfersize = size;
6785   dsc->u.ldst.immed = 0;
6786   dsc->u.ldst.writeback = 0;
6787   dsc->u.ldst.restore_r4 = 0;
6788
6789   /* LDR R0, R2, R3 */
6790   dsc->modinsn[0] = 0xf852;
6791   dsc->modinsn[1] = 0x3;
6792   dsc->numinsns = 2;
6793
6794   dsc->cleanup = &cleanup_load;
6795
6796   return 0;
6797 }
6798
6799 static int
6800 thumb2_copy_load_reg_imm (struct gdbarch *gdbarch, uint16_t insn1,
6801                           uint16_t insn2, struct regcache *regs,
6802                           struct displaced_step_closure *dsc,
6803                           int writeback, int immed)
6804 {
6805   unsigned int rt = bits (insn2, 12, 15);
6806   unsigned int rn = bits (insn1, 0, 3);
6807   unsigned int rm = bits (insn2, 0, 3);  /* Only valid if !immed.  */
6808   /* In LDR (register), there is also a register Rm, which is not allowed to
6809      be PC, so we don't have to check it.  */
6810
6811   if (rt != ARM_PC_REGNUM && rn != ARM_PC_REGNUM)
6812     return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2, "load",
6813                                         dsc);
6814
6815   if (debug_displaced)
6816     fprintf_unfiltered (gdb_stdlog,
6817                         "displaced: copying ldr r%d [r%d] insn %.4x%.4x\n",
6818                          rt, rn, insn1, insn2);
6819
6820   install_load_store (gdbarch, regs, dsc, 1, immed, writeback, 4,
6821                       0, rt, rm, rn);
6822
6823   dsc->u.ldst.restore_r4 = 0;
6824
6825   if (immed)
6826     /* ldr[b]<cond> rt, [rn, #imm], etc.
6827        ->
6828        ldr[b]<cond> r0, [r2, #imm].  */
6829     {
6830       dsc->modinsn[0] = (insn1 & 0xfff0) | 0x2;
6831       dsc->modinsn[1] = insn2 & 0x0fff;
6832     }
6833   else
6834     /* ldr[b]<cond> rt, [rn, rm], etc.
6835        ->
6836        ldr[b]<cond> r0, [r2, r3].  */
6837     {
6838       dsc->modinsn[0] = (insn1 & 0xfff0) | 0x2;
6839       dsc->modinsn[1] = (insn2 & 0x0ff0) | 0x3;
6840     }
6841
6842   dsc->numinsns = 2;
6843
6844   return 0;
6845 }
6846
6847
6848 static int
6849 arm_copy_ldr_str_ldrb_strb (struct gdbarch *gdbarch, uint32_t insn,
6850                             struct regcache *regs,
6851                             struct displaced_step_closure *dsc,
6852                             int load, int size, int usermode)
6853 {
6854   int immed = !bit (insn, 25);
6855   int writeback = (bit (insn, 24) == 0 || bit (insn, 21) != 0);
6856   unsigned int rt = bits (insn, 12, 15);
6857   unsigned int rn = bits (insn, 16, 19);
6858   unsigned int rm = bits (insn, 0, 3);  /* Only valid if !immed.  */
6859
6860   if (!insn_references_pc (insn, 0x000ff00ful))
6861     return arm_copy_unmodified (gdbarch, insn, "load/store", dsc);
6862
6863   if (debug_displaced)
6864     fprintf_unfiltered (gdb_stdlog,
6865                         "displaced: copying %s%s r%d [r%d] insn %.8lx\n",
6866                         load ? (size == 1 ? "ldrb" : "ldr")
6867                              : (size == 1 ? "strb" : "str"), usermode ? "t" : "",
6868                         rt, rn,
6869                         (unsigned long) insn);
6870
6871   install_load_store (gdbarch, regs, dsc, load, immed, writeback, size,
6872                       usermode, rt, rm, rn);
6873
6874   if (load || rt != ARM_PC_REGNUM)
6875     {
6876       dsc->u.ldst.restore_r4 = 0;
6877
6878       if (immed)
6879         /* {ldr,str}[b]<cond> rt, [rn, #imm], etc.
6880            ->
6881            {ldr,str}[b]<cond> r0, [r2, #imm].  */
6882         dsc->modinsn[0] = (insn & 0xfff00fff) | 0x20000;
6883       else
6884         /* {ldr,str}[b]<cond> rt, [rn, rm], etc.
6885            ->
6886            {ldr,str}[b]<cond> r0, [r2, r3].  */
6887         dsc->modinsn[0] = (insn & 0xfff00ff0) | 0x20003;
6888     }
6889   else
6890     {
6891       /* We need to use r4 as scratch.  Make sure it's restored afterwards.  */
6892       dsc->u.ldst.restore_r4 = 1;
6893       dsc->modinsn[0] = 0xe92d8000;  /* push {pc} */
6894       dsc->modinsn[1] = 0xe8bd0010;  /* pop  {r4} */
6895       dsc->modinsn[2] = 0xe044400f;  /* sub r4, r4, pc.  */
6896       dsc->modinsn[3] = 0xe2844008;  /* add r4, r4, #8.  */
6897       dsc->modinsn[4] = 0xe0800004;  /* add r0, r0, r4.  */
6898
6899       /* As above.  */
6900       if (immed)
6901         dsc->modinsn[5] = (insn & 0xfff00fff) | 0x20000;
6902       else
6903         dsc->modinsn[5] = (insn & 0xfff00ff0) | 0x20003;
6904
6905       dsc->numinsns = 6;
6906     }
6907
6908   dsc->cleanup = load ? &cleanup_load : &cleanup_store;
6909
6910   return 0;
6911 }
6912
6913 /* Cleanup LDM instructions with fully-populated register list.  This is an
6914    unfortunate corner case: it's impossible to implement correctly by modifying
6915    the instruction.  The issue is as follows: we have an instruction,
6916
6917    ldm rN, {r0-r15}
6918
6919    which we must rewrite to avoid loading PC.  A possible solution would be to
6920    do the load in two halves, something like (with suitable cleanup
6921    afterwards):
6922
6923    mov r8, rN
6924    ldm[id][ab] r8!, {r0-r7}
6925    str r7, <temp>
6926    ldm[id][ab] r8, {r7-r14}
6927    <bkpt>
6928
6929    but at present there's no suitable place for <temp>, since the scratch space
6930    is overwritten before the cleanup routine is called.  For now, we simply
6931    emulate the instruction.  */
6932
6933 static void
6934 cleanup_block_load_all (struct gdbarch *gdbarch, struct regcache *regs,
6935                         struct displaced_step_closure *dsc)
6936 {
6937   int inc = dsc->u.block.increment;
6938   int bump_before = dsc->u.block.before ? (inc ? 4 : -4) : 0;
6939   int bump_after = dsc->u.block.before ? 0 : (inc ? 4 : -4);
6940   uint32_t regmask = dsc->u.block.regmask;
6941   int regno = inc ? 0 : 15;
6942   CORE_ADDR xfer_addr = dsc->u.block.xfer_addr;
6943   int exception_return = dsc->u.block.load && dsc->u.block.user
6944                          && (regmask & 0x8000) != 0;
6945   uint32_t status = displaced_read_reg (regs, dsc, ARM_PS_REGNUM);
6946   int do_transfer = condition_true (dsc->u.block.cond, status);
6947   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
6948
6949   if (!do_transfer)
6950     return;
6951
6952   /* If the instruction is ldm rN, {...pc}^, I don't think there's anything
6953      sensible we can do here.  Complain loudly.  */
6954   if (exception_return)
6955     error (_("Cannot single-step exception return"));
6956
6957   /* We don't handle any stores here for now.  */
6958   gdb_assert (dsc->u.block.load != 0);
6959
6960   if (debug_displaced)
6961     fprintf_unfiltered (gdb_stdlog, "displaced: emulating block transfer: "
6962                         "%s %s %s\n", dsc->u.block.load ? "ldm" : "stm",
6963                         dsc->u.block.increment ? "inc" : "dec",
6964                         dsc->u.block.before ? "before" : "after");
6965
6966   while (regmask)
6967     {
6968       uint32_t memword;
6969
6970       if (inc)
6971         while (regno <= ARM_PC_REGNUM && (regmask & (1 << regno)) == 0)
6972           regno++;
6973       else
6974         while (regno >= 0 && (regmask & (1 << regno)) == 0)
6975           regno--;
6976
6977       xfer_addr += bump_before;
6978
6979       memword = read_memory_unsigned_integer (xfer_addr, 4, byte_order);
6980       displaced_write_reg (regs, dsc, regno, memword, LOAD_WRITE_PC);
6981
6982       xfer_addr += bump_after;
6983
6984       regmask &= ~(1 << regno);
6985     }
6986
6987   if (dsc->u.block.writeback)
6988     displaced_write_reg (regs, dsc, dsc->u.block.rn, xfer_addr,
6989                          CANNOT_WRITE_PC);
6990 }
6991
6992 /* Clean up an STM which included the PC in the register list.  */
6993
6994 static void
6995 cleanup_block_store_pc (struct gdbarch *gdbarch, struct regcache *regs,
6996                         struct displaced_step_closure *dsc)
6997 {
6998   uint32_t status = displaced_read_reg (regs, dsc, ARM_PS_REGNUM);
6999   int store_executed = condition_true (dsc->u.block.cond, status);
7000   CORE_ADDR pc_stored_at, transferred_regs = bitcount (dsc->u.block.regmask);
7001   CORE_ADDR stm_insn_addr;
7002   uint32_t pc_val;
7003   long offset;
7004   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
7005
7006   /* If condition code fails, there's nothing else to do.  */
7007   if (!store_executed)
7008     return;
7009
7010   if (dsc->u.block.increment)
7011     {
7012       pc_stored_at = dsc->u.block.xfer_addr + 4 * transferred_regs;
7013
7014       if (dsc->u.block.before)
7015          pc_stored_at += 4;
7016     }
7017   else
7018     {
7019       pc_stored_at = dsc->u.block.xfer_addr;
7020
7021       if (dsc->u.block.before)
7022          pc_stored_at -= 4;
7023     }
7024
7025   pc_val = read_memory_unsigned_integer (pc_stored_at, 4, byte_order);
7026   stm_insn_addr = dsc->scratch_base;
7027   offset = pc_val - stm_insn_addr;
7028
7029   if (debug_displaced)
7030     fprintf_unfiltered (gdb_stdlog, "displaced: detected PC offset %.8lx for "
7031                         "STM instruction\n", offset);
7032
7033   /* Rewrite the stored PC to the proper value for the non-displaced original
7034      instruction.  */
7035   write_memory_unsigned_integer (pc_stored_at, 4, byte_order,
7036                                  dsc->insn_addr + offset);
7037 }
7038
7039 /* Clean up an LDM which includes the PC in the register list.  We clumped all
7040    the registers in the transferred list into a contiguous range r0...rX (to
7041    avoid loading PC directly and losing control of the debugged program), so we
7042    must undo that here.  */
7043
7044 static void
7045 cleanup_block_load_pc (struct gdbarch *gdbarch,
7046                        struct regcache *regs,
7047                        struct displaced_step_closure *dsc)
7048 {
7049   uint32_t status = displaced_read_reg (regs, dsc, ARM_PS_REGNUM);
7050   int load_executed = condition_true (dsc->u.block.cond, status);
7051   unsigned int mask = dsc->u.block.regmask, write_reg = ARM_PC_REGNUM;
7052   unsigned int regs_loaded = bitcount (mask);
7053   unsigned int num_to_shuffle = regs_loaded, clobbered;
7054
7055   /* The method employed here will fail if the register list is fully populated
7056      (we need to avoid loading PC directly).  */
7057   gdb_assert (num_to_shuffle < 16);
7058
7059   if (!load_executed)
7060     return;
7061
7062   clobbered = (1 << num_to_shuffle) - 1;
7063
7064   while (num_to_shuffle > 0)
7065     {
7066       if ((mask & (1 << write_reg)) != 0)
7067         {
7068           unsigned int read_reg = num_to_shuffle - 1;
7069
7070           if (read_reg != write_reg)
7071             {
7072               ULONGEST rval = displaced_read_reg (regs, dsc, read_reg);
7073               displaced_write_reg (regs, dsc, write_reg, rval, LOAD_WRITE_PC);
7074               if (debug_displaced)
7075                 fprintf_unfiltered (gdb_stdlog, _("displaced: LDM: move "
7076                                     "loaded register r%d to r%d\n"), read_reg,
7077                                     write_reg);
7078             }
7079           else if (debug_displaced)
7080             fprintf_unfiltered (gdb_stdlog, _("displaced: LDM: register "
7081                                 "r%d already in the right place\n"),
7082                                 write_reg);
7083
7084           clobbered &= ~(1 << write_reg);
7085
7086           num_to_shuffle--;
7087         }
7088
7089       write_reg--;
7090     }
7091
7092   /* Restore any registers we scribbled over.  */
7093   for (write_reg = 0; clobbered != 0; write_reg++)
7094     {
7095       if ((clobbered & (1 << write_reg)) != 0)
7096         {
7097           displaced_write_reg (regs, dsc, write_reg, dsc->tmp[write_reg],
7098                                CANNOT_WRITE_PC);
7099           if (debug_displaced)
7100             fprintf_unfiltered (gdb_stdlog, _("displaced: LDM: restored "
7101                                 "clobbered register r%d\n"), write_reg);
7102           clobbered &= ~(1 << write_reg);
7103         }
7104     }
7105
7106   /* Perform register writeback manually.  */
7107   if (dsc->u.block.writeback)
7108     {
7109       ULONGEST new_rn_val = dsc->u.block.xfer_addr;
7110
7111       if (dsc->u.block.increment)
7112         new_rn_val += regs_loaded * 4;
7113       else
7114         new_rn_val -= regs_loaded * 4;
7115
7116       displaced_write_reg (regs, dsc, dsc->u.block.rn, new_rn_val,
7117                            CANNOT_WRITE_PC);
7118     }
7119 }
7120
7121 /* Handle ldm/stm, apart from some tricky cases which are unlikely to occur
7122    in user-level code (in particular exception return, ldm rn, {...pc}^).  */
7123
7124 static int
7125 arm_copy_block_xfer (struct gdbarch *gdbarch, uint32_t insn,
7126                      struct regcache *regs,
7127                      struct displaced_step_closure *dsc)
7128 {
7129   int load = bit (insn, 20);
7130   int user = bit (insn, 22);
7131   int increment = bit (insn, 23);
7132   int before = bit (insn, 24);
7133   int writeback = bit (insn, 21);
7134   int rn = bits (insn, 16, 19);
7135
7136   /* Block transfers which don't mention PC can be run directly
7137      out-of-line.  */
7138   if (rn != ARM_PC_REGNUM && (insn & 0x8000) == 0)
7139     return arm_copy_unmodified (gdbarch, insn, "ldm/stm", dsc);
7140
7141   if (rn == ARM_PC_REGNUM)
7142     {
7143       warning (_("displaced: Unpredictable LDM or STM with "
7144                  "base register r15"));
7145       return arm_copy_unmodified (gdbarch, insn, "unpredictable ldm/stm", dsc);
7146     }
7147
7148   if (debug_displaced)
7149     fprintf_unfiltered (gdb_stdlog, "displaced: copying block transfer insn "
7150                         "%.8lx\n", (unsigned long) insn);
7151
7152   dsc->u.block.xfer_addr = displaced_read_reg (regs, dsc, rn);
7153   dsc->u.block.rn = rn;
7154
7155   dsc->u.block.load = load;
7156   dsc->u.block.user = user;
7157   dsc->u.block.increment = increment;
7158   dsc->u.block.before = before;
7159   dsc->u.block.writeback = writeback;
7160   dsc->u.block.cond = bits (insn, 28, 31);
7161
7162   dsc->u.block.regmask = insn & 0xffff;
7163
7164   if (load)
7165     {
7166       if ((insn & 0xffff) == 0xffff)
7167         {
7168           /* LDM with a fully-populated register list.  This case is
7169              particularly tricky.  Implement for now by fully emulating the
7170              instruction (which might not behave perfectly in all cases, but
7171              these instructions should be rare enough for that not to matter
7172              too much).  */
7173           dsc->modinsn[0] = ARM_NOP;
7174
7175           dsc->cleanup = &cleanup_block_load_all;
7176         }
7177       else
7178         {
7179           /* LDM of a list of registers which includes PC.  Implement by
7180              rewriting the list of registers to be transferred into a
7181              contiguous chunk r0...rX before doing the transfer, then shuffling
7182              registers into the correct places in the cleanup routine.  */
7183           unsigned int regmask = insn & 0xffff;
7184           unsigned int num_in_list = bitcount (regmask), new_regmask, bit = 1;
7185           unsigned int to = 0, from = 0, i, new_rn;
7186
7187           for (i = 0; i < num_in_list; i++)
7188             dsc->tmp[i] = displaced_read_reg (regs, dsc, i);
7189
7190           /* Writeback makes things complicated.  We need to avoid clobbering
7191              the base register with one of the registers in our modified
7192              register list, but just using a different register can't work in
7193              all cases, e.g.:
7194
7195                ldm r14!, {r0-r13,pc}
7196
7197              which would need to be rewritten as:
7198
7199                ldm rN!, {r0-r14}
7200
7201              but that can't work, because there's no free register for N.
7202
7203              Solve this by turning off the writeback bit, and emulating
7204              writeback manually in the cleanup routine.  */
7205
7206           if (writeback)
7207             insn &= ~(1 << 21);
7208
7209           new_regmask = (1 << num_in_list) - 1;
7210
7211           if (debug_displaced)
7212             fprintf_unfiltered (gdb_stdlog, _("displaced: LDM r%d%s, "
7213                                 "{..., pc}: original reg list %.4x, modified "
7214                                 "list %.4x\n"), rn, writeback ? "!" : "",
7215                                 (int) insn & 0xffff, new_regmask);
7216
7217           dsc->modinsn[0] = (insn & ~0xffff) | (new_regmask & 0xffff);
7218
7219           dsc->cleanup = &cleanup_block_load_pc;
7220         }
7221     }
7222   else
7223     {
7224       /* STM of a list of registers which includes PC.  Run the instruction
7225          as-is, but out of line: this will store the wrong value for the PC,
7226          so we must manually fix up the memory in the cleanup routine.
7227          Doing things this way has the advantage that we can auto-detect
7228          the offset of the PC write (which is architecture-dependent) in
7229          the cleanup routine.  */
7230       dsc->modinsn[0] = insn;
7231
7232       dsc->cleanup = &cleanup_block_store_pc;
7233     }
7234
7235   return 0;
7236 }
7237
7238 static int
7239 thumb2_copy_block_xfer (struct gdbarch *gdbarch, uint16_t insn1, uint16_t insn2,
7240                         struct regcache *regs,
7241                         struct displaced_step_closure *dsc)
7242 {
7243   int rn = bits (insn1, 0, 3);
7244   int load = bit (insn1, 4);
7245   int writeback = bit (insn1, 5);
7246
7247   /* Block transfers which don't mention PC can be run directly
7248      out-of-line.  */
7249   if (rn != ARM_PC_REGNUM && (insn2 & 0x8000) == 0)
7250     return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2, "ldm/stm", dsc);
7251
7252   if (rn == ARM_PC_REGNUM)
7253     {
7254       warning (_("displaced: Unpredictable LDM or STM with "
7255                  "base register r15"));
7256       return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
7257                                           "unpredictable ldm/stm", dsc);
7258     }
7259
7260   if (debug_displaced)
7261     fprintf_unfiltered (gdb_stdlog, "displaced: copying block transfer insn "
7262                         "%.4x%.4x\n", insn1, insn2);
7263
7264   /* Clear bit 13, since it should be always zero.  */
7265   dsc->u.block.regmask = (insn2 & 0xdfff);
7266   dsc->u.block.rn = rn;
7267
7268   dsc->u.block.load = load;
7269   dsc->u.block.user = 0;
7270   dsc->u.block.increment = bit (insn1, 7);
7271   dsc->u.block.before = bit (insn1, 8);
7272   dsc->u.block.writeback = writeback;
7273   dsc->u.block.cond = INST_AL;
7274   dsc->u.block.xfer_addr = displaced_read_reg (regs, dsc, rn);
7275
7276   if (load)
7277     {
7278       if (dsc->u.block.regmask == 0xffff)
7279         {
7280           /* This branch is impossible to happen.  */
7281           gdb_assert (0);
7282         }
7283       else
7284         {
7285           unsigned int regmask = dsc->u.block.regmask;
7286           unsigned int num_in_list = bitcount (regmask), new_regmask, bit = 1;
7287           unsigned int to = 0, from = 0, i, new_rn;
7288
7289           for (i = 0; i < num_in_list; i++)
7290             dsc->tmp[i] = displaced_read_reg (regs, dsc, i);
7291
7292           if (writeback)
7293             insn1 &= ~(1 << 5);
7294
7295           new_regmask = (1 << num_in_list) - 1;
7296
7297           if (debug_displaced)
7298             fprintf_unfiltered (gdb_stdlog, _("displaced: LDM r%d%s, "
7299                                 "{..., pc}: original reg list %.4x, modified "
7300                                 "list %.4x\n"), rn, writeback ? "!" : "",
7301                                 (int) dsc->u.block.regmask, new_regmask);
7302
7303           dsc->modinsn[0] = insn1;
7304           dsc->modinsn[1] = (new_regmask & 0xffff);
7305           dsc->numinsns = 2;
7306
7307           dsc->cleanup = &cleanup_block_load_pc;
7308         }
7309     }
7310   else
7311     {
7312       dsc->modinsn[0] = insn1;
7313       dsc->modinsn[1] = insn2;
7314       dsc->numinsns = 2;
7315       dsc->cleanup = &cleanup_block_store_pc;
7316     }
7317   return 0;
7318 }
7319
7320 /* Cleanup/copy SVC (SWI) instructions.  These two functions are overridden
7321    for Linux, where some SVC instructions must be treated specially.  */
7322
7323 static void
7324 cleanup_svc (struct gdbarch *gdbarch, struct regcache *regs,
7325              struct displaced_step_closure *dsc)
7326 {
7327   CORE_ADDR resume_addr = dsc->insn_addr + dsc->insn_size;
7328
7329   if (debug_displaced)
7330     fprintf_unfiltered (gdb_stdlog, "displaced: cleanup for svc, resume at "
7331                         "%.8lx\n", (unsigned long) resume_addr);
7332
7333   displaced_write_reg (regs, dsc, ARM_PC_REGNUM, resume_addr, BRANCH_WRITE_PC);
7334 }
7335
7336
7337 /* Common copy routine for svc instruciton.  */
7338
7339 static int
7340 install_svc (struct gdbarch *gdbarch, struct regcache *regs,
7341              struct displaced_step_closure *dsc)
7342 {
7343   /* Preparation: none.
7344      Insn: unmodified svc.
7345      Cleanup: pc <- insn_addr + insn_size.  */
7346
7347   /* Pretend we wrote to the PC, so cleanup doesn't set PC to the next
7348      instruction.  */
7349   dsc->wrote_to_pc = 1;
7350
7351   /* Allow OS-specific code to override SVC handling.  */
7352   if (dsc->u.svc.copy_svc_os)
7353     return dsc->u.svc.copy_svc_os (gdbarch, regs, dsc);
7354   else
7355     {
7356       dsc->cleanup = &cleanup_svc;
7357       return 0;
7358     }
7359 }
7360
7361 static int
7362 arm_copy_svc (struct gdbarch *gdbarch, uint32_t insn,
7363               struct regcache *regs, struct displaced_step_closure *dsc)
7364 {
7365
7366   if (debug_displaced)
7367     fprintf_unfiltered (gdb_stdlog, "displaced: copying svc insn %.8lx\n",
7368                         (unsigned long) insn);
7369
7370   dsc->modinsn[0] = insn;
7371
7372   return install_svc (gdbarch, regs, dsc);
7373 }
7374
7375 static int
7376 thumb_copy_svc (struct gdbarch *gdbarch, uint16_t insn,
7377                 struct regcache *regs, struct displaced_step_closure *dsc)
7378 {
7379
7380   if (debug_displaced)
7381     fprintf_unfiltered (gdb_stdlog, "displaced: copying svc insn %.4x\n",
7382                         insn);
7383
7384   dsc->modinsn[0] = insn;
7385
7386   return install_svc (gdbarch, regs, dsc);
7387 }
7388
7389 /* Copy undefined instructions.  */
7390
7391 static int
7392 arm_copy_undef (struct gdbarch *gdbarch, uint32_t insn,
7393                 struct displaced_step_closure *dsc)
7394 {
7395   if (debug_displaced)
7396     fprintf_unfiltered (gdb_stdlog,
7397                         "displaced: copying undefined insn %.8lx\n",
7398                         (unsigned long) insn);
7399
7400   dsc->modinsn[0] = insn;
7401
7402   return 0;
7403 }
7404
7405 static int
7406 thumb_32bit_copy_undef (struct gdbarch *gdbarch, uint16_t insn1, uint16_t insn2,
7407                        struct displaced_step_closure *dsc)
7408 {
7409
7410   if (debug_displaced)
7411     fprintf_unfiltered (gdb_stdlog, "displaced: copying undefined insn "
7412                        "%.4x %.4x\n", (unsigned short) insn1,
7413                        (unsigned short) insn2);
7414
7415   dsc->modinsn[0] = insn1;
7416   dsc->modinsn[1] = insn2;
7417   dsc->numinsns = 2;
7418
7419   return 0;
7420 }
7421
7422 /* Copy unpredictable instructions.  */
7423
7424 static int
7425 arm_copy_unpred (struct gdbarch *gdbarch, uint32_t insn,
7426                  struct displaced_step_closure *dsc)
7427 {
7428   if (debug_displaced)
7429     fprintf_unfiltered (gdb_stdlog, "displaced: copying unpredictable insn "
7430                         "%.8lx\n", (unsigned long) insn);
7431
7432   dsc->modinsn[0] = insn;
7433
7434   return 0;
7435 }
7436
7437 /* The decode_* functions are instruction decoding helpers.  They mostly follow
7438    the presentation in the ARM ARM.  */
7439
7440 static int
7441 arm_decode_misc_memhint_neon (struct gdbarch *gdbarch, uint32_t insn,
7442                               struct regcache *regs,
7443                               struct displaced_step_closure *dsc)
7444 {
7445   unsigned int op1 = bits (insn, 20, 26), op2 = bits (insn, 4, 7);
7446   unsigned int rn = bits (insn, 16, 19);
7447
7448   if (op1 == 0x10 && (op2 & 0x2) == 0x0 && (rn & 0xe) == 0x0)
7449     return arm_copy_unmodified (gdbarch, insn, "cps", dsc);
7450   else if (op1 == 0x10 && op2 == 0x0 && (rn & 0xe) == 0x1)
7451     return arm_copy_unmodified (gdbarch, insn, "setend", dsc);
7452   else if ((op1 & 0x60) == 0x20)
7453     return arm_copy_unmodified (gdbarch, insn, "neon dataproc", dsc);
7454   else if ((op1 & 0x71) == 0x40)
7455     return arm_copy_unmodified (gdbarch, insn, "neon elt/struct load/store",
7456                                 dsc);
7457   else if ((op1 & 0x77) == 0x41)
7458     return arm_copy_unmodified (gdbarch, insn, "unallocated mem hint", dsc);
7459   else if ((op1 & 0x77) == 0x45)
7460     return arm_copy_preload (gdbarch, insn, regs, dsc);  /* pli.  */
7461   else if ((op1 & 0x77) == 0x51)
7462     {
7463       if (rn != 0xf)
7464         return arm_copy_preload (gdbarch, insn, regs, dsc);  /* pld/pldw.  */
7465       else
7466         return arm_copy_unpred (gdbarch, insn, dsc);
7467     }
7468   else if ((op1 & 0x77) == 0x55)
7469     return arm_copy_preload (gdbarch, insn, regs, dsc);  /* pld/pldw.  */
7470   else if (op1 == 0x57)
7471     switch (op2)
7472       {
7473       case 0x1: return arm_copy_unmodified (gdbarch, insn, "clrex", dsc);
7474       case 0x4: return arm_copy_unmodified (gdbarch, insn, "dsb", dsc);
7475       case 0x5: return arm_copy_unmodified (gdbarch, insn, "dmb", dsc);
7476       case 0x6: return arm_copy_unmodified (gdbarch, insn, "isb", dsc);
7477       default: return arm_copy_unpred (gdbarch, insn, dsc);
7478       }
7479   else if ((op1 & 0x63) == 0x43)
7480     return arm_copy_unpred (gdbarch, insn, dsc);
7481   else if ((op2 & 0x1) == 0x0)
7482     switch (op1 & ~0x80)
7483       {
7484       case 0x61:
7485         return arm_copy_unmodified (gdbarch, insn, "unallocated mem hint", dsc);
7486       case 0x65:
7487         return arm_copy_preload_reg (gdbarch, insn, regs, dsc);  /* pli reg.  */
7488       case 0x71: case 0x75:
7489         /* pld/pldw reg.  */
7490         return arm_copy_preload_reg (gdbarch, insn, regs, dsc);
7491       case 0x63: case 0x67: case 0x73: case 0x77:
7492         return arm_copy_unpred (gdbarch, insn, dsc);
7493       default:
7494         return arm_copy_undef (gdbarch, insn, dsc);
7495       }
7496   else
7497     return arm_copy_undef (gdbarch, insn, dsc);  /* Probably unreachable.  */
7498 }
7499
7500 static int
7501 arm_decode_unconditional (struct gdbarch *gdbarch, uint32_t insn,
7502                           struct regcache *regs,
7503                           struct displaced_step_closure *dsc)
7504 {
7505   if (bit (insn, 27) == 0)
7506     return arm_decode_misc_memhint_neon (gdbarch, insn, regs, dsc);
7507   /* Switch on bits: 0bxxxxx321xxx0xxxxxxxxxxxxxxxxxxxx.  */
7508   else switch (((insn & 0x7000000) >> 23) | ((insn & 0x100000) >> 20))
7509     {
7510     case 0x0: case 0x2:
7511       return arm_copy_unmodified (gdbarch, insn, "srs", dsc);
7512
7513     case 0x1: case 0x3:
7514       return arm_copy_unmodified (gdbarch, insn, "rfe", dsc);
7515
7516     case 0x4: case 0x5: case 0x6: case 0x7:
7517       return arm_copy_b_bl_blx (gdbarch, insn, regs, dsc);
7518
7519     case 0x8:
7520       switch ((insn & 0xe00000) >> 21)
7521         {
7522         case 0x1: case 0x3: case 0x4: case 0x5: case 0x6: case 0x7:
7523           /* stc/stc2.  */
7524           return arm_copy_copro_load_store (gdbarch, insn, regs, dsc);
7525
7526         case 0x2:
7527           return arm_copy_unmodified (gdbarch, insn, "mcrr/mcrr2", dsc);
7528
7529         default:
7530           return arm_copy_undef (gdbarch, insn, dsc);
7531         }
7532
7533     case 0x9:
7534       {
7535          int rn_f = (bits (insn, 16, 19) == 0xf);
7536         switch ((insn & 0xe00000) >> 21)
7537           {
7538           case 0x1: case 0x3:
7539             /* ldc/ldc2 imm (undefined for rn == pc).  */
7540             return rn_f ? arm_copy_undef (gdbarch, insn, dsc)
7541                         : arm_copy_copro_load_store (gdbarch, insn, regs, dsc);
7542
7543           case 0x2:
7544             return arm_copy_unmodified (gdbarch, insn, "mrrc/mrrc2", dsc);
7545
7546           case 0x4: case 0x5: case 0x6: case 0x7:
7547             /* ldc/ldc2 lit (undefined for rn != pc).  */
7548             return rn_f ? arm_copy_copro_load_store (gdbarch, insn, regs, dsc)
7549                         : arm_copy_undef (gdbarch, insn, dsc);
7550
7551           default:
7552             return arm_copy_undef (gdbarch, insn, dsc);
7553           }
7554       }
7555
7556     case 0xa:
7557       return arm_copy_unmodified (gdbarch, insn, "stc/stc2", dsc);
7558
7559     case 0xb:
7560       if (bits (insn, 16, 19) == 0xf)
7561         /* ldc/ldc2 lit.  */
7562         return arm_copy_copro_load_store (gdbarch, insn, regs, dsc);
7563       else
7564         return arm_copy_undef (gdbarch, insn, dsc);
7565
7566     case 0xc:
7567       if (bit (insn, 4))
7568         return arm_copy_unmodified (gdbarch, insn, "mcr/mcr2", dsc);
7569       else
7570         return arm_copy_unmodified (gdbarch, insn, "cdp/cdp2", dsc);
7571
7572     case 0xd:
7573       if (bit (insn, 4))
7574         return arm_copy_unmodified (gdbarch, insn, "mrc/mrc2", dsc);
7575       else
7576         return arm_copy_unmodified (gdbarch, insn, "cdp/cdp2", dsc);
7577
7578     default:
7579       return arm_copy_undef (gdbarch, insn, dsc);
7580     }
7581 }
7582
7583 /* Decode miscellaneous instructions in dp/misc encoding space.  */
7584
7585 static int
7586 arm_decode_miscellaneous (struct gdbarch *gdbarch, uint32_t insn,
7587                           struct regcache *regs,
7588                           struct displaced_step_closure *dsc)
7589 {
7590   unsigned int op2 = bits (insn, 4, 6);
7591   unsigned int op = bits (insn, 21, 22);
7592   unsigned int op1 = bits (insn, 16, 19);
7593
7594   switch (op2)
7595     {
7596     case 0x0:
7597       return arm_copy_unmodified (gdbarch, insn, "mrs/msr", dsc);
7598
7599     case 0x1:
7600       if (op == 0x1)  /* bx.  */
7601         return arm_copy_bx_blx_reg (gdbarch, insn, regs, dsc);
7602       else if (op == 0x3)
7603         return arm_copy_unmodified (gdbarch, insn, "clz", dsc);
7604       else
7605         return arm_copy_undef (gdbarch, insn, dsc);
7606
7607     case 0x2:
7608       if (op == 0x1)
7609         /* Not really supported.  */
7610         return arm_copy_unmodified (gdbarch, insn, "bxj", dsc);
7611       else
7612         return arm_copy_undef (gdbarch, insn, dsc);
7613
7614     case 0x3:
7615       if (op == 0x1)
7616         return arm_copy_bx_blx_reg (gdbarch, insn,
7617                                 regs, dsc);  /* blx register.  */
7618       else
7619         return arm_copy_undef (gdbarch, insn, dsc);
7620
7621     case 0x5:
7622       return arm_copy_unmodified (gdbarch, insn, "saturating add/sub", dsc);
7623
7624     case 0x7:
7625       if (op == 0x1)
7626         return arm_copy_unmodified (gdbarch, insn, "bkpt", dsc);
7627       else if (op == 0x3)
7628         /* Not really supported.  */
7629         return arm_copy_unmodified (gdbarch, insn, "smc", dsc);
7630
7631     default:
7632       return arm_copy_undef (gdbarch, insn, dsc);
7633     }
7634 }
7635
7636 static int
7637 arm_decode_dp_misc (struct gdbarch *gdbarch, uint32_t insn,
7638                     struct regcache *regs,
7639                     struct displaced_step_closure *dsc)
7640 {
7641   if (bit (insn, 25))
7642     switch (bits (insn, 20, 24))
7643       {
7644       case 0x10:
7645         return arm_copy_unmodified (gdbarch, insn, "movw", dsc);
7646
7647       case 0x14:
7648         return arm_copy_unmodified (gdbarch, insn, "movt", dsc);
7649
7650       case 0x12: case 0x16:
7651         return arm_copy_unmodified (gdbarch, insn, "msr imm", dsc);
7652
7653       default:
7654         return arm_copy_alu_imm (gdbarch, insn, regs, dsc);
7655       }
7656   else
7657     {
7658       uint32_t op1 = bits (insn, 20, 24), op2 = bits (insn, 4, 7);
7659
7660       if ((op1 & 0x19) != 0x10 && (op2 & 0x1) == 0x0)
7661         return arm_copy_alu_reg (gdbarch, insn, regs, dsc);
7662       else if ((op1 & 0x19) != 0x10 && (op2 & 0x9) == 0x1)
7663         return arm_copy_alu_shifted_reg (gdbarch, insn, regs, dsc);
7664       else if ((op1 & 0x19) == 0x10 && (op2 & 0x8) == 0x0)
7665         return arm_decode_miscellaneous (gdbarch, insn, regs, dsc);
7666       else if ((op1 & 0x19) == 0x10 && (op2 & 0x9) == 0x8)
7667         return arm_copy_unmodified (gdbarch, insn, "halfword mul/mla", dsc);
7668       else if ((op1 & 0x10) == 0x00 && op2 == 0x9)
7669         return arm_copy_unmodified (gdbarch, insn, "mul/mla", dsc);
7670       else if ((op1 & 0x10) == 0x10 && op2 == 0x9)
7671         return arm_copy_unmodified (gdbarch, insn, "synch", dsc);
7672       else if (op2 == 0xb || (op2 & 0xd) == 0xd)
7673         /* 2nd arg means "unpriveleged".  */
7674         return arm_copy_extra_ld_st (gdbarch, insn, (op1 & 0x12) == 0x02, regs,
7675                                      dsc);
7676     }
7677
7678   /* Should be unreachable.  */
7679   return 1;
7680 }
7681
7682 static int
7683 arm_decode_ld_st_word_ubyte (struct gdbarch *gdbarch, uint32_t insn,
7684                              struct regcache *regs,
7685                              struct displaced_step_closure *dsc)
7686 {
7687   int a = bit (insn, 25), b = bit (insn, 4);
7688   uint32_t op1 = bits (insn, 20, 24);
7689   int rn_f = bits (insn, 16, 19) == 0xf;
7690
7691   if ((!a && (op1 & 0x05) == 0x00 && (op1 & 0x17) != 0x02)
7692       || (a && (op1 & 0x05) == 0x00 && (op1 & 0x17) != 0x02 && !b))
7693     return arm_copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 0, 4, 0);
7694   else if ((!a && (op1 & 0x17) == 0x02)
7695             || (a && (op1 & 0x17) == 0x02 && !b))
7696     return arm_copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 0, 4, 1);
7697   else if ((!a && (op1 & 0x05) == 0x01 && (op1 & 0x17) != 0x03)
7698             || (a && (op1 & 0x05) == 0x01 && (op1 & 0x17) != 0x03 && !b))
7699     return arm_copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 1, 4, 0);
7700   else if ((!a && (op1 & 0x17) == 0x03)
7701            || (a && (op1 & 0x17) == 0x03 && !b))
7702     return arm_copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 1, 4, 1);
7703   else if ((!a && (op1 & 0x05) == 0x04 && (op1 & 0x17) != 0x06)
7704             || (a && (op1 & 0x05) == 0x04 && (op1 & 0x17) != 0x06 && !b))
7705     return arm_copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 0, 1, 0);
7706   else if ((!a && (op1 & 0x17) == 0x06)
7707            || (a && (op1 & 0x17) == 0x06 && !b))
7708     return arm_copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 0, 1, 1);
7709   else if ((!a && (op1 & 0x05) == 0x05 && (op1 & 0x17) != 0x07)
7710            || (a && (op1 & 0x05) == 0x05 && (op1 & 0x17) != 0x07 && !b))
7711     return arm_copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 1, 1, 0);
7712   else if ((!a && (op1 & 0x17) == 0x07)
7713            || (a && (op1 & 0x17) == 0x07 && !b))
7714     return arm_copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 1, 1, 1);
7715
7716   /* Should be unreachable.  */
7717   return 1;
7718 }
7719
7720 static int
7721 arm_decode_media (struct gdbarch *gdbarch, uint32_t insn,
7722                   struct displaced_step_closure *dsc)
7723 {
7724   switch (bits (insn, 20, 24))
7725     {
7726     case 0x00: case 0x01: case 0x02: case 0x03:
7727       return arm_copy_unmodified (gdbarch, insn, "parallel add/sub signed", dsc);
7728
7729     case 0x04: case 0x05: case 0x06: case 0x07:
7730       return arm_copy_unmodified (gdbarch, insn, "parallel add/sub unsigned", dsc);
7731
7732     case 0x08: case 0x09: case 0x0a: case 0x0b:
7733     case 0x0c: case 0x0d: case 0x0e: case 0x0f:
7734       return arm_copy_unmodified (gdbarch, insn,
7735                               "decode/pack/unpack/saturate/reverse", dsc);
7736
7737     case 0x18:
7738       if (bits (insn, 5, 7) == 0)  /* op2.  */
7739          {
7740           if (bits (insn, 12, 15) == 0xf)
7741             return arm_copy_unmodified (gdbarch, insn, "usad8", dsc);
7742           else
7743             return arm_copy_unmodified (gdbarch, insn, "usada8", dsc);
7744         }
7745       else
7746          return arm_copy_undef (gdbarch, insn, dsc);
7747
7748     case 0x1a: case 0x1b:
7749       if (bits (insn, 5, 6) == 0x2)  /* op2[1:0].  */
7750         return arm_copy_unmodified (gdbarch, insn, "sbfx", dsc);
7751       else
7752         return arm_copy_undef (gdbarch, insn, dsc);
7753
7754     case 0x1c: case 0x1d:
7755       if (bits (insn, 5, 6) == 0x0)  /* op2[1:0].  */
7756          {
7757           if (bits (insn, 0, 3) == 0xf)
7758             return arm_copy_unmodified (gdbarch, insn, "bfc", dsc);
7759           else
7760             return arm_copy_unmodified (gdbarch, insn, "bfi", dsc);
7761         }
7762       else
7763         return arm_copy_undef (gdbarch, insn, dsc);
7764
7765     case 0x1e: case 0x1f:
7766       if (bits (insn, 5, 6) == 0x2)  /* op2[1:0].  */
7767         return arm_copy_unmodified (gdbarch, insn, "ubfx", dsc);
7768       else
7769         return arm_copy_undef (gdbarch, insn, dsc);
7770     }
7771
7772   /* Should be unreachable.  */
7773   return 1;
7774 }
7775
7776 static int
7777 arm_decode_b_bl_ldmstm (struct gdbarch *gdbarch, int32_t insn,
7778                         struct regcache *regs,
7779                         struct displaced_step_closure *dsc)
7780 {
7781   if (bit (insn, 25))
7782     return arm_copy_b_bl_blx (gdbarch, insn, regs, dsc);
7783   else
7784     return arm_copy_block_xfer (gdbarch, insn, regs, dsc);
7785 }
7786
7787 static int
7788 arm_decode_ext_reg_ld_st (struct gdbarch *gdbarch, uint32_t insn,
7789                           struct regcache *regs,
7790                           struct displaced_step_closure *dsc)
7791 {
7792   unsigned int opcode = bits (insn, 20, 24);
7793
7794   switch (opcode)
7795     {
7796     case 0x04: case 0x05:  /* VFP/Neon mrrc/mcrr.  */
7797       return arm_copy_unmodified (gdbarch, insn, "vfp/neon mrrc/mcrr", dsc);
7798
7799     case 0x08: case 0x0a: case 0x0c: case 0x0e:
7800     case 0x12: case 0x16:
7801       return arm_copy_unmodified (gdbarch, insn, "vfp/neon vstm/vpush", dsc);
7802
7803     case 0x09: case 0x0b: case 0x0d: case 0x0f:
7804     case 0x13: case 0x17:
7805       return arm_copy_unmodified (gdbarch, insn, "vfp/neon vldm/vpop", dsc);
7806
7807     case 0x10: case 0x14: case 0x18: case 0x1c:  /* vstr.  */
7808     case 0x11: case 0x15: case 0x19: case 0x1d:  /* vldr.  */
7809       /* Note: no writeback for these instructions.  Bit 25 will always be
7810          zero though (via caller), so the following works OK.  */
7811       return arm_copy_copro_load_store (gdbarch, insn, regs, dsc);
7812     }
7813
7814   /* Should be unreachable.  */
7815   return 1;
7816 }
7817
7818 /* Decode shifted register instructions.  */
7819
7820 static int
7821 thumb2_decode_dp_shift_reg (struct gdbarch *gdbarch, uint16_t insn1,
7822                             uint16_t insn2,  struct regcache *regs,
7823                             struct displaced_step_closure *dsc)
7824 {
7825   /* PC is only allowed to be used in instruction MOV.  */
7826
7827   unsigned int op = bits (insn1, 5, 8);
7828   unsigned int rn = bits (insn1, 0, 3);
7829
7830   if (op == 0x2 && rn == 0xf) /* MOV */
7831     return thumb2_copy_alu_imm (gdbarch, insn1, insn2, regs, dsc);
7832   else
7833     return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
7834                                         "dp (shift reg)", dsc);
7835 }
7836
7837
7838 /* Decode extension register load/store.  Exactly the same as
7839    arm_decode_ext_reg_ld_st.  */
7840
7841 static int
7842 thumb2_decode_ext_reg_ld_st (struct gdbarch *gdbarch, uint16_t insn1,
7843                              uint16_t insn2,  struct regcache *regs,
7844                              struct displaced_step_closure *dsc)
7845 {
7846   unsigned int opcode = bits (insn1, 4, 8);
7847
7848   switch (opcode)
7849     {
7850     case 0x04: case 0x05:
7851       return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
7852                                           "vfp/neon vmov", dsc);
7853
7854     case 0x08: case 0x0c: /* 01x00 */
7855     case 0x0a: case 0x0e: /* 01x10 */
7856     case 0x12: case 0x16: /* 10x10 */
7857       return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
7858                                           "vfp/neon vstm/vpush", dsc);
7859
7860     case 0x09: case 0x0d: /* 01x01 */
7861     case 0x0b: case 0x0f: /* 01x11 */
7862     case 0x13: case 0x17: /* 10x11 */
7863       return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
7864                                           "vfp/neon vldm/vpop", dsc);
7865
7866     case 0x10: case 0x14: case 0x18: case 0x1c:  /* vstr.  */
7867       return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
7868                                           "vstr", dsc);
7869     case 0x11: case 0x15: case 0x19: case 0x1d:  /* vldr.  */
7870       return thumb2_copy_copro_load_store (gdbarch, insn1, insn2, regs, dsc);
7871     }
7872
7873   /* Should be unreachable.  */
7874   return 1;
7875 }
7876
7877 static int
7878 arm_decode_svc_copro (struct gdbarch *gdbarch, uint32_t insn, CORE_ADDR to,
7879                       struct regcache *regs, struct displaced_step_closure *dsc)
7880 {
7881   unsigned int op1 = bits (insn, 20, 25);
7882   int op = bit (insn, 4);
7883   unsigned int coproc = bits (insn, 8, 11);
7884   unsigned int rn = bits (insn, 16, 19);
7885
7886   if ((op1 & 0x20) == 0x00 && (op1 & 0x3a) != 0x00 && (coproc & 0xe) == 0xa)
7887     return arm_decode_ext_reg_ld_st (gdbarch, insn, regs, dsc);
7888   else if ((op1 & 0x21) == 0x00 && (op1 & 0x3a) != 0x00
7889            && (coproc & 0xe) != 0xa)
7890     /* stc/stc2.  */
7891     return arm_copy_copro_load_store (gdbarch, insn, regs, dsc);
7892   else if ((op1 & 0x21) == 0x01 && (op1 & 0x3a) != 0x00
7893            && (coproc & 0xe) != 0xa)
7894     /* ldc/ldc2 imm/lit.  */
7895     return arm_copy_copro_load_store (gdbarch, insn, regs, dsc);
7896   else if ((op1 & 0x3e) == 0x00)
7897     return arm_copy_undef (gdbarch, insn, dsc);
7898   else if ((op1 & 0x3e) == 0x04 && (coproc & 0xe) == 0xa)
7899     return arm_copy_unmodified (gdbarch, insn, "neon 64bit xfer", dsc);
7900   else if (op1 == 0x04 && (coproc & 0xe) != 0xa)
7901     return arm_copy_unmodified (gdbarch, insn, "mcrr/mcrr2", dsc);
7902   else if (op1 == 0x05 && (coproc & 0xe) != 0xa)
7903     return arm_copy_unmodified (gdbarch, insn, "mrrc/mrrc2", dsc);
7904   else if ((op1 & 0x30) == 0x20 && !op)
7905     {
7906       if ((coproc & 0xe) == 0xa)
7907         return arm_copy_unmodified (gdbarch, insn, "vfp dataproc", dsc);
7908       else
7909         return arm_copy_unmodified (gdbarch, insn, "cdp/cdp2", dsc);
7910     }
7911   else if ((op1 & 0x30) == 0x20 && op)
7912     return arm_copy_unmodified (gdbarch, insn, "neon 8/16/32 bit xfer", dsc);
7913   else if ((op1 & 0x31) == 0x20 && op && (coproc & 0xe) != 0xa)
7914     return arm_copy_unmodified (gdbarch, insn, "mcr/mcr2", dsc);
7915   else if ((op1 & 0x31) == 0x21 && op && (coproc & 0xe) != 0xa)
7916     return arm_copy_unmodified (gdbarch, insn, "mrc/mrc2", dsc);
7917   else if ((op1 & 0x30) == 0x30)
7918     return arm_copy_svc (gdbarch, insn, regs, dsc);
7919   else
7920     return arm_copy_undef (gdbarch, insn, dsc);  /* Possibly unreachable.  */
7921 }
7922
7923 static int
7924 thumb2_decode_svc_copro (struct gdbarch *gdbarch, uint16_t insn1,
7925                          uint16_t insn2, struct regcache *regs,
7926                          struct displaced_step_closure *dsc)
7927 {
7928   unsigned int coproc = bits (insn2, 8, 11);
7929   unsigned int op1 = bits (insn1, 4, 9);
7930   unsigned int bit_5_8 = bits (insn1, 5, 8);
7931   unsigned int bit_9 = bit (insn1, 9);
7932   unsigned int bit_4 = bit (insn1, 4);
7933   unsigned int rn = bits (insn1, 0, 3);
7934
7935   if (bit_9 == 0)
7936     {
7937       if (bit_5_8 == 2)
7938         return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
7939                                             "neon 64bit xfer/mrrc/mrrc2/mcrr/mcrr2",
7940                                             dsc);
7941       else if (bit_5_8 == 0) /* UNDEFINED.  */
7942         return thumb_32bit_copy_undef (gdbarch, insn1, insn2, dsc);
7943       else
7944         {
7945            /*coproc is 101x.  SIMD/VFP, ext registers load/store.  */
7946           if ((coproc & 0xe) == 0xa)
7947             return thumb2_decode_ext_reg_ld_st (gdbarch, insn1, insn2, regs,
7948                                                 dsc);
7949           else /* coproc is not 101x.  */
7950             {
7951               if (bit_4 == 0) /* STC/STC2.  */
7952                 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
7953                                                     "stc/stc2", dsc);
7954               else /* LDC/LDC2 {literal, immeidate}.  */
7955                 return thumb2_copy_copro_load_store (gdbarch, insn1, insn2,
7956                                                      regs, dsc);
7957             }
7958         }
7959     }
7960   else
7961     return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2, "coproc", dsc);
7962
7963   return 0;
7964 }
7965
7966 static void
7967 install_pc_relative (struct gdbarch *gdbarch, struct regcache *regs,
7968                      struct displaced_step_closure *dsc, int rd)
7969 {
7970   /* ADR Rd, #imm
7971
7972      Rewrite as:
7973
7974      Preparation: Rd <- PC
7975      Insn: ADD Rd, #imm
7976      Cleanup: Null.
7977   */
7978
7979   /* Rd <- PC */
7980   int val = displaced_read_reg (regs, dsc, ARM_PC_REGNUM);
7981   displaced_write_reg (regs, dsc, rd, val, CANNOT_WRITE_PC);
7982 }
7983
7984 static int
7985 thumb_copy_pc_relative_16bit (struct gdbarch *gdbarch, struct regcache *regs,
7986                               struct displaced_step_closure *dsc,
7987                               int rd, unsigned int imm)
7988 {
7989
7990   /* Encoding T2: ADDS Rd, #imm */
7991   dsc->modinsn[0] = (0x3000 | (rd << 8) | imm);
7992
7993   install_pc_relative (gdbarch, regs, dsc, rd);
7994
7995   return 0;
7996 }
7997
7998 static int
7999 thumb_decode_pc_relative_16bit (struct gdbarch *gdbarch, uint16_t insn,
8000                                 struct regcache *regs,
8001                                 struct displaced_step_closure *dsc)
8002 {
8003   unsigned int rd = bits (insn, 8, 10);
8004   unsigned int imm8 = bits (insn, 0, 7);
8005
8006   if (debug_displaced)
8007     fprintf_unfiltered (gdb_stdlog,
8008                         "displaced: copying thumb adr r%d, #%d insn %.4x\n",
8009                         rd, imm8, insn);
8010
8011   return thumb_copy_pc_relative_16bit (gdbarch, regs, dsc, rd, imm8);
8012 }
8013
8014 static int
8015 thumb_copy_pc_relative_32bit (struct gdbarch *gdbarch, uint16_t insn1,
8016                               uint16_t insn2, struct regcache *regs,
8017                               struct displaced_step_closure *dsc)
8018 {
8019   unsigned int rd = bits (insn2, 8, 11);
8020   /* Since immediate has the same encoding in ADR ADD and SUB, so we simply
8021      extract raw immediate encoding rather than computing immediate.  When
8022      generating ADD or SUB instruction, we can simply perform OR operation to
8023      set immediate into ADD.  */
8024   unsigned int imm_3_8 = insn2 & 0x70ff;
8025   unsigned int imm_i = insn1 & 0x0400; /* Clear all bits except bit 10.  */
8026
8027   if (debug_displaced)
8028     fprintf_unfiltered (gdb_stdlog,
8029                         "displaced: copying thumb adr r%d, #%d:%d insn %.4x%.4x\n",
8030                         rd, imm_i, imm_3_8, insn1, insn2);
8031
8032   if (bit (insn1, 7)) /* Encoding T2 */
8033     {
8034       /* Encoding T3: SUB Rd, Rd, #imm */
8035       dsc->modinsn[0] = (0xf1a0 | rd | imm_i);
8036       dsc->modinsn[1] = ((rd << 8) | imm_3_8);
8037     }
8038   else /* Encoding T3 */
8039     {
8040       /* Encoding T3: ADD Rd, Rd, #imm */
8041       dsc->modinsn[0] = (0xf100 | rd | imm_i);
8042       dsc->modinsn[1] = ((rd << 8) | imm_3_8);
8043     }
8044   dsc->numinsns = 2;
8045
8046   install_pc_relative (gdbarch, regs, dsc, rd);
8047
8048   return 0;
8049 }
8050
8051 static int
8052 thumb_copy_16bit_ldr_literal (struct gdbarch *gdbarch, unsigned short insn1,
8053                               struct regcache *regs,
8054                               struct displaced_step_closure *dsc)
8055 {
8056   unsigned int rt = bits (insn1, 8, 10);
8057   unsigned int pc;
8058   int imm8 = (bits (insn1, 0, 7) << 2);
8059   CORE_ADDR from = dsc->insn_addr;
8060
8061   /* LDR Rd, #imm8
8062
8063      Rwrite as:
8064
8065      Preparation: tmp0 <- R0, tmp2 <- R2, tmp3 <- R3, R2 <- PC, R3 <- #imm8;
8066
8067      Insn: LDR R0, [R2, R3];
8068      Cleanup: R2 <- tmp2, R3 <- tmp3, Rd <- R0, R0 <- tmp0 */
8069
8070   if (debug_displaced)
8071     fprintf_unfiltered (gdb_stdlog,
8072                         "displaced: copying thumb ldr r%d [pc #%d]\n"
8073                         , rt, imm8);
8074
8075   dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
8076   dsc->tmp[2] = displaced_read_reg (regs, dsc, 2);
8077   dsc->tmp[3] = displaced_read_reg (regs, dsc, 3);
8078   pc = displaced_read_reg (regs, dsc, ARM_PC_REGNUM);
8079   /* The assembler calculates the required value of the offset from the
8080      Align(PC,4) value of this instruction to the label.  */
8081   pc = pc & 0xfffffffc;
8082
8083   displaced_write_reg (regs, dsc, 2, pc, CANNOT_WRITE_PC);
8084   displaced_write_reg (regs, dsc, 3, imm8, CANNOT_WRITE_PC);
8085
8086   dsc->rd = rt;
8087   dsc->u.ldst.xfersize = 4;
8088   dsc->u.ldst.rn = 0;
8089   dsc->u.ldst.immed = 0;
8090   dsc->u.ldst.writeback = 0;
8091   dsc->u.ldst.restore_r4 = 0;
8092
8093   dsc->modinsn[0] = 0x58d0; /* ldr r0, [r2, r3]*/
8094
8095   dsc->cleanup = &cleanup_load;
8096
8097   return 0;
8098 }
8099
8100 /* Copy Thumb cbnz/cbz insruction.  */
8101
8102 static int
8103 thumb_copy_cbnz_cbz (struct gdbarch *gdbarch, uint16_t insn1,
8104                      struct regcache *regs,
8105                      struct displaced_step_closure *dsc)
8106 {
8107   int non_zero = bit (insn1, 11);
8108   unsigned int imm5 = (bit (insn1, 9) << 6) | (bits (insn1, 3, 7) << 1);
8109   CORE_ADDR from = dsc->insn_addr;
8110   int rn = bits (insn1, 0, 2);
8111   int rn_val = displaced_read_reg (regs, dsc, rn);
8112
8113   dsc->u.branch.cond = (rn_val && non_zero) || (!rn_val && !non_zero);
8114   /* CBNZ and CBZ do not affect the condition flags.  If condition is true,
8115      set it INST_AL, so cleanup_branch will know branch is taken, otherwise,
8116      condition is false, let it be, cleanup_branch will do nothing.  */
8117   if (dsc->u.branch.cond)
8118     {
8119       dsc->u.branch.cond = INST_AL;
8120       dsc->u.branch.dest = from + 4 + imm5;
8121     }
8122   else
8123       dsc->u.branch.dest = from + 2;
8124
8125   dsc->u.branch.link = 0;
8126   dsc->u.branch.exchange = 0;
8127
8128   if (debug_displaced)
8129     fprintf_unfiltered (gdb_stdlog, "displaced: copying %s [r%d = 0x%x]"
8130                         " insn %.4x to %.8lx\n", non_zero ? "cbnz" : "cbz",
8131                         rn, rn_val, insn1, dsc->u.branch.dest);
8132
8133   dsc->modinsn[0] = THUMB_NOP;
8134
8135   dsc->cleanup = &cleanup_branch;
8136   return 0;
8137 }
8138
8139 /* Copy Table Branch Byte/Halfword */
8140 static int
8141 thumb2_copy_table_branch (struct gdbarch *gdbarch, uint16_t insn1,
8142                           uint16_t insn2, struct regcache *regs,
8143                           struct displaced_step_closure *dsc)
8144 {
8145   ULONGEST rn_val, rm_val;
8146   int is_tbh = bit (insn2, 4);
8147   CORE_ADDR halfwords = 0;
8148   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
8149
8150   rn_val = displaced_read_reg (regs, dsc, bits (insn1, 0, 3));
8151   rm_val = displaced_read_reg (regs, dsc, bits (insn2, 0, 3));
8152
8153   if (is_tbh)
8154     {
8155       gdb_byte buf[2];
8156
8157       target_read_memory (rn_val + 2 * rm_val, buf, 2);
8158       halfwords = extract_unsigned_integer (buf, 2, byte_order);
8159     }
8160   else
8161     {
8162       gdb_byte buf[1];
8163
8164       target_read_memory (rn_val + rm_val, buf, 1);
8165       halfwords = extract_unsigned_integer (buf, 1, byte_order);
8166     }
8167
8168   if (debug_displaced)
8169     fprintf_unfiltered (gdb_stdlog, "displaced: %s base 0x%x offset 0x%x"
8170                         " offset 0x%x\n", is_tbh ? "tbh" : "tbb",
8171                         (unsigned int) rn_val, (unsigned int) rm_val,
8172                         (unsigned int) halfwords);
8173
8174   dsc->u.branch.cond = INST_AL;
8175   dsc->u.branch.link = 0;
8176   dsc->u.branch.exchange = 0;
8177   dsc->u.branch.dest = dsc->insn_addr + 4 + 2 * halfwords;
8178
8179   dsc->cleanup = &cleanup_branch;
8180
8181   return 0;
8182 }
8183
8184 static void
8185 cleanup_pop_pc_16bit_all (struct gdbarch *gdbarch, struct regcache *regs,
8186                           struct displaced_step_closure *dsc)
8187 {
8188   /* PC <- r7 */
8189   int val = displaced_read_reg (regs, dsc, 7);
8190   displaced_write_reg (regs, dsc, ARM_PC_REGNUM, val, BX_WRITE_PC);
8191
8192   /* r7 <- r8 */
8193   val = displaced_read_reg (regs, dsc, 8);
8194   displaced_write_reg (regs, dsc, 7, val, CANNOT_WRITE_PC);
8195
8196   /* r8 <- tmp[0] */
8197   displaced_write_reg (regs, dsc, 8, dsc->tmp[0], CANNOT_WRITE_PC);
8198
8199 }
8200
8201 static int
8202 thumb_copy_pop_pc_16bit (struct gdbarch *gdbarch, unsigned short insn1,
8203                          struct regcache *regs,
8204                          struct displaced_step_closure *dsc)
8205 {
8206   dsc->u.block.regmask = insn1 & 0x00ff;
8207
8208   /* Rewrite instruction: POP {rX, rY, ...,rZ, PC}
8209      to :
8210
8211      (1) register list is full, that is, r0-r7 are used.
8212      Prepare: tmp[0] <- r8
8213
8214      POP {r0, r1, ...., r6, r7}; remove PC from reglist
8215      MOV r8, r7; Move value of r7 to r8;
8216      POP {r7}; Store PC value into r7.
8217
8218      Cleanup: PC <- r7, r7 <- r8, r8 <-tmp[0]
8219
8220      (2) register list is not full, supposing there are N registers in
8221      register list (except PC, 0 <= N <= 7).
8222      Prepare: for each i, 0 - N, tmp[i] <- ri.
8223
8224      POP {r0, r1, ...., rN};
8225
8226      Cleanup: Set registers in original reglist from r0 - rN.  Restore r0 - rN
8227      from tmp[] properly.
8228   */
8229   if (debug_displaced)
8230     fprintf_unfiltered (gdb_stdlog,
8231                         "displaced: copying thumb pop {%.8x, pc} insn %.4x\n",
8232                         dsc->u.block.regmask, insn1);
8233
8234   if (dsc->u.block.regmask == 0xff)
8235     {
8236       dsc->tmp[0] = displaced_read_reg (regs, dsc, 8);
8237
8238       dsc->modinsn[0] = (insn1 & 0xfeff); /* POP {r0,r1,...,r6, r7} */
8239       dsc->modinsn[1] = 0x46b8; /* MOV r8, r7 */
8240       dsc->modinsn[2] = 0xbc80; /* POP {r7} */
8241
8242       dsc->numinsns = 3;
8243       dsc->cleanup = &cleanup_pop_pc_16bit_all;
8244     }
8245   else
8246     {
8247       unsigned int num_in_list = bitcount (dsc->u.block.regmask);
8248       unsigned int new_regmask, bit = 1;
8249       unsigned int to = 0, from = 0, i, new_rn;
8250
8251       for (i = 0; i < num_in_list + 1; i++)
8252         dsc->tmp[i] = displaced_read_reg (regs, dsc, i);
8253
8254       new_regmask = (1 << (num_in_list + 1)) - 1;
8255
8256       if (debug_displaced)
8257         fprintf_unfiltered (gdb_stdlog, _("displaced: POP "
8258                                           "{..., pc}: original reg list %.4x,"
8259                                           " modified list %.4x\n"),
8260                             (int) dsc->u.block.regmask, new_regmask);
8261
8262       dsc->u.block.regmask |= 0x8000;
8263       dsc->u.block.writeback = 0;
8264       dsc->u.block.cond = INST_AL;
8265
8266       dsc->modinsn[0] = (insn1 & ~0x1ff) | (new_regmask & 0xff);
8267
8268       dsc->cleanup = &cleanup_block_load_pc;
8269     }
8270
8271   return 0;
8272 }
8273
8274 static void
8275 thumb_process_displaced_16bit_insn (struct gdbarch *gdbarch, uint16_t insn1,
8276                                     struct regcache *regs,
8277                                     struct displaced_step_closure *dsc)
8278 {
8279   unsigned short op_bit_12_15 = bits (insn1, 12, 15);
8280   unsigned short op_bit_10_11 = bits (insn1, 10, 11);
8281   int err = 0;
8282
8283   /* 16-bit thumb instructions.  */
8284   switch (op_bit_12_15)
8285     {
8286       /* Shift (imme), add, subtract, move and compare.  */
8287     case 0: case 1: case 2: case 3:
8288       err = thumb_copy_unmodified_16bit (gdbarch, insn1,
8289                                          "shift/add/sub/mov/cmp",
8290                                          dsc);
8291       break;
8292     case 4:
8293       switch (op_bit_10_11)
8294         {
8295         case 0: /* Data-processing */
8296           err = thumb_copy_unmodified_16bit (gdbarch, insn1,
8297                                              "data-processing",
8298                                              dsc);
8299           break;
8300         case 1: /* Special data instructions and branch and exchange.  */
8301           {
8302             unsigned short op = bits (insn1, 7, 9);
8303             if (op == 6 || op == 7) /* BX or BLX */
8304               err = thumb_copy_bx_blx_reg (gdbarch, insn1, regs, dsc);
8305             else if (bits (insn1, 6, 7) != 0) /* ADD/MOV/CMP high registers.  */
8306               err = thumb_copy_alu_reg (gdbarch, insn1, regs, dsc);
8307             else
8308               err = thumb_copy_unmodified_16bit (gdbarch, insn1, "special data",
8309                                                  dsc);
8310           }
8311           break;
8312         default: /* LDR (literal) */
8313           err = thumb_copy_16bit_ldr_literal (gdbarch, insn1, regs, dsc);
8314         }
8315       break;
8316     case 5: case 6: case 7: case 8: case 9: /* Load/Store single data item */
8317       err = thumb_copy_unmodified_16bit (gdbarch, insn1, "ldr/str", dsc);
8318       break;
8319     case 10:
8320       if (op_bit_10_11 < 2) /* Generate PC-relative address */
8321         err = thumb_decode_pc_relative_16bit (gdbarch, insn1, regs, dsc);
8322       else /* Generate SP-relative address */
8323         err = thumb_copy_unmodified_16bit (gdbarch, insn1, "sp-relative", dsc);
8324       break;
8325     case 11: /* Misc 16-bit instructions */
8326       {
8327         switch (bits (insn1, 8, 11))
8328           {
8329           case 1: case 3:  case 9: case 11: /* CBNZ, CBZ */
8330             err = thumb_copy_cbnz_cbz (gdbarch, insn1, regs, dsc);
8331             break;
8332           case 12: case 13: /* POP */
8333             if (bit (insn1, 8)) /* PC is in register list.  */
8334               err = thumb_copy_pop_pc_16bit (gdbarch, insn1, regs, dsc);
8335             else
8336               err = thumb_copy_unmodified_16bit (gdbarch, insn1, "pop", dsc);
8337             break;
8338           case 15: /* If-Then, and hints */
8339             if (bits (insn1, 0, 3))
8340               /* If-Then makes up to four following instructions conditional.
8341                  IT instruction itself is not conditional, so handle it as a
8342                  common unmodified instruction.  */
8343               err = thumb_copy_unmodified_16bit (gdbarch, insn1, "If-Then",
8344                                                  dsc);
8345             else
8346               err = thumb_copy_unmodified_16bit (gdbarch, insn1, "hints", dsc);
8347             break;
8348           default:
8349             err = thumb_copy_unmodified_16bit (gdbarch, insn1, "misc", dsc);
8350           }
8351       }
8352       break;
8353     case 12:
8354       if (op_bit_10_11 < 2) /* Store multiple registers */
8355         err = thumb_copy_unmodified_16bit (gdbarch, insn1, "stm", dsc);
8356       else /* Load multiple registers */
8357         err = thumb_copy_unmodified_16bit (gdbarch, insn1, "ldm", dsc);
8358       break;
8359     case 13: /* Conditional branch and supervisor call */
8360       if (bits (insn1, 9, 11) != 7) /* conditional branch */
8361         err = thumb_copy_b (gdbarch, insn1, dsc);
8362       else
8363         err = thumb_copy_svc (gdbarch, insn1, regs, dsc);
8364       break;
8365     case 14: /* Unconditional branch */
8366       err = thumb_copy_b (gdbarch, insn1, dsc);
8367       break;
8368     default:
8369       err = 1;
8370     }
8371
8372   if (err)
8373     internal_error (__FILE__, __LINE__,
8374                     _("thumb_process_displaced_16bit_insn: Instruction decode error"));
8375 }
8376
8377 static int
8378 decode_thumb_32bit_ld_mem_hints (struct gdbarch *gdbarch,
8379                                  uint16_t insn1, uint16_t insn2,
8380                                  struct regcache *regs,
8381                                  struct displaced_step_closure *dsc)
8382 {
8383   int rt = bits (insn2, 12, 15);
8384   int rn = bits (insn1, 0, 3);
8385   int op1 = bits (insn1, 7, 8);
8386   int err = 0;
8387
8388   switch (bits (insn1, 5, 6))
8389     {
8390     case 0: /* Load byte and memory hints */
8391       if (rt == 0xf) /* PLD/PLI */
8392         {
8393           if (rn == 0xf)
8394             /* PLD literal or Encoding T3 of PLI(immediate, literal).  */
8395             return thumb2_copy_preload (gdbarch, insn1, insn2, regs, dsc);
8396           else
8397             return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8398                                                 "pli/pld", dsc);
8399         }
8400       else
8401         {
8402           if (rn == 0xf) /* LDRB/LDRSB (literal) */
8403             return thumb2_copy_load_literal (gdbarch, insn1, insn2, regs, dsc,
8404                                              1);
8405           else
8406             return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8407                                                 "ldrb{reg, immediate}/ldrbt",
8408                                                 dsc);
8409         }
8410
8411       break;
8412     case 1: /* Load halfword and memory hints.  */
8413       if (rt == 0xf) /* PLD{W} and Unalloc memory hint.  */
8414         return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8415                                             "pld/unalloc memhint", dsc);
8416       else
8417         {
8418           if (rn == 0xf)
8419             return thumb2_copy_load_literal (gdbarch, insn1, insn2, regs, dsc,
8420                                              2);
8421           else
8422             return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8423                                                 "ldrh/ldrht", dsc);
8424         }
8425       break;
8426     case 2: /* Load word */
8427       {
8428         int insn2_bit_8_11 = bits (insn2, 8, 11);
8429
8430         if (rn == 0xf)
8431           return thumb2_copy_load_literal (gdbarch, insn1, insn2, regs, dsc, 4);
8432         else if (op1 == 0x1) /* Encoding T3 */
8433           return thumb2_copy_load_reg_imm (gdbarch, insn1, insn2, regs, dsc,
8434                                            0, 1);
8435         else /* op1 == 0x0 */
8436           {
8437             if (insn2_bit_8_11 == 0xc || (insn2_bit_8_11 & 0x9) == 0x9)
8438               /* LDR (immediate) */
8439               return thumb2_copy_load_reg_imm (gdbarch, insn1, insn2, regs,
8440                                                dsc, bit (insn2, 8), 1);
8441             else if (insn2_bit_8_11 == 0xe) /* LDRT */
8442               return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8443                                                   "ldrt", dsc);
8444             else
8445               /* LDR (register) */
8446               return thumb2_copy_load_reg_imm (gdbarch, insn1, insn2, regs,
8447                                                dsc, 0, 0);
8448           }
8449         break;
8450       }
8451     default:
8452       return thumb_32bit_copy_undef (gdbarch, insn1, insn2, dsc);
8453       break;
8454     }
8455   return 0;
8456 }
8457
8458 static void
8459 thumb_process_displaced_32bit_insn (struct gdbarch *gdbarch, uint16_t insn1,
8460                                     uint16_t insn2, struct regcache *regs,
8461                                     struct displaced_step_closure *dsc)
8462 {
8463   int err = 0;
8464   unsigned short op = bit (insn2, 15);
8465   unsigned int op1 = bits (insn1, 11, 12);
8466
8467   switch (op1)
8468     {
8469     case 1:
8470       {
8471         switch (bits (insn1, 9, 10))
8472           {
8473           case 0:
8474             if (bit (insn1, 6))
8475               {
8476                 /* Load/store {dual, execlusive}, table branch.  */
8477                 if (bits (insn1, 7, 8) == 1 && bits (insn1, 4, 5) == 1
8478                     && bits (insn2, 5, 7) == 0)
8479                   err = thumb2_copy_table_branch (gdbarch, insn1, insn2, regs,
8480                                                   dsc);
8481                 else
8482                   /* PC is not allowed to use in load/store {dual, exclusive}
8483                      instructions.  */
8484                   err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8485                                                      "load/store dual/ex", dsc);
8486               }
8487             else /* load/store multiple */
8488               {
8489                 switch (bits (insn1, 7, 8))
8490                   {
8491                   case 0: case 3: /* SRS, RFE */
8492                     err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8493                                                        "srs/rfe", dsc);
8494                     break;
8495                   case 1: case 2: /* LDM/STM/PUSH/POP */
8496                     err = thumb2_copy_block_xfer (gdbarch, insn1, insn2, regs, dsc);
8497                     break;
8498                   }
8499               }
8500             break;
8501
8502           case 1:
8503             /* Data-processing (shift register).  */
8504             err = thumb2_decode_dp_shift_reg (gdbarch, insn1, insn2, regs,
8505                                               dsc);
8506             break;
8507           default: /* Coprocessor instructions.  */
8508             err = thumb2_decode_svc_copro (gdbarch, insn1, insn2, regs, dsc);
8509             break;
8510           }
8511       break;
8512       }
8513     case 2: /* op1 = 2 */
8514       if (op) /* Branch and misc control.  */
8515         {
8516           if (bit (insn2, 14)  /* BLX/BL */
8517               || bit (insn2, 12) /* Unconditional branch */
8518               || (bits (insn1, 7, 9) != 0x7)) /* Conditional branch */
8519             err = thumb2_copy_b_bl_blx (gdbarch, insn1, insn2, regs, dsc);
8520           else
8521             err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8522                                                "misc ctrl", dsc);
8523         }
8524       else
8525         {
8526           if (bit (insn1, 9)) /* Data processing (plain binary imm).  */
8527             {
8528               int op = bits (insn1, 4, 8);
8529               int rn = bits (insn1, 0, 3);
8530               if ((op == 0 || op == 0xa) && rn == 0xf)
8531                 err = thumb_copy_pc_relative_32bit (gdbarch, insn1, insn2,
8532                                                     regs, dsc);
8533               else
8534                 err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8535                                                    "dp/pb", dsc);
8536             }
8537           else /* Data processing (modified immeidate) */
8538             err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8539                                                "dp/mi", dsc);
8540         }
8541       break;
8542     case 3: /* op1 = 3 */
8543       switch (bits (insn1, 9, 10))
8544         {
8545         case 0:
8546           if (bit (insn1, 4))
8547             err = decode_thumb_32bit_ld_mem_hints (gdbarch, insn1, insn2,
8548                                                    regs, dsc);
8549           else /* NEON Load/Store and Store single data item */
8550             err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8551                                                "neon elt/struct load/store",
8552                                                dsc);
8553           break;
8554         case 1: /* op1 = 3, bits (9, 10) == 1 */
8555           switch (bits (insn1, 7, 8))
8556             {
8557             case 0: case 1: /* Data processing (register) */
8558               err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8559                                                  "dp(reg)", dsc);
8560               break;
8561             case 2: /* Multiply and absolute difference */
8562               err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8563                                                  "mul/mua/diff", dsc);
8564               break;
8565             case 3: /* Long multiply and divide */
8566               err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8567                                                  "lmul/lmua", dsc);
8568               break;
8569             }
8570           break;
8571         default: /* Coprocessor instructions */
8572           err = thumb2_decode_svc_copro (gdbarch, insn1, insn2, regs, dsc);
8573           break;
8574         }
8575       break;
8576     default:
8577       err = 1;
8578     }
8579
8580   if (err)
8581     internal_error (__FILE__, __LINE__,
8582                     _("thumb_process_displaced_32bit_insn: Instruction decode error"));
8583
8584 }
8585
8586 static void
8587 thumb_process_displaced_insn (struct gdbarch *gdbarch, CORE_ADDR from,
8588                               CORE_ADDR to, struct regcache *regs,
8589                               struct displaced_step_closure *dsc)
8590 {
8591   enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
8592   uint16_t insn1
8593     = read_memory_unsigned_integer (from, 2, byte_order_for_code);
8594
8595   if (debug_displaced)
8596     fprintf_unfiltered (gdb_stdlog, "displaced: process thumb insn %.4x "
8597                         "at %.8lx\n", insn1, (unsigned long) from);
8598
8599   dsc->is_thumb = 1;
8600   dsc->insn_size = thumb_insn_size (insn1);
8601   if (thumb_insn_size (insn1) == 4)
8602     {
8603       uint16_t insn2
8604         = read_memory_unsigned_integer (from + 2, 2, byte_order_for_code);
8605       thumb_process_displaced_32bit_insn (gdbarch, insn1, insn2, regs, dsc);
8606     }
8607   else
8608     thumb_process_displaced_16bit_insn (gdbarch, insn1, regs, dsc);
8609 }
8610
8611 void
8612 arm_process_displaced_insn (struct gdbarch *gdbarch, CORE_ADDR from,
8613                             CORE_ADDR to, struct regcache *regs,
8614                             struct displaced_step_closure *dsc)
8615 {
8616   int err = 0;
8617   enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
8618   uint32_t insn;
8619
8620   /* Most displaced instructions use a 1-instruction scratch space, so set this
8621      here and override below if/when necessary.  */
8622   dsc->numinsns = 1;
8623   dsc->insn_addr = from;
8624   dsc->scratch_base = to;
8625   dsc->cleanup = NULL;
8626   dsc->wrote_to_pc = 0;
8627
8628   if (!displaced_in_arm_mode (regs))
8629     return thumb_process_displaced_insn (gdbarch, from, to, regs, dsc);
8630
8631   dsc->is_thumb = 0;
8632   dsc->insn_size = 4;
8633   insn = read_memory_unsigned_integer (from, 4, byte_order_for_code);
8634   if (debug_displaced)
8635     fprintf_unfiltered (gdb_stdlog, "displaced: stepping insn %.8lx "
8636                         "at %.8lx\n", (unsigned long) insn,
8637                         (unsigned long) from);
8638
8639   if ((insn & 0xf0000000) == 0xf0000000)
8640     err = arm_decode_unconditional (gdbarch, insn, regs, dsc);
8641   else switch (((insn & 0x10) >> 4) | ((insn & 0xe000000) >> 24))
8642     {
8643     case 0x0: case 0x1: case 0x2: case 0x3:
8644       err = arm_decode_dp_misc (gdbarch, insn, regs, dsc);
8645       break;
8646
8647     case 0x4: case 0x5: case 0x6:
8648       err = arm_decode_ld_st_word_ubyte (gdbarch, insn, regs, dsc);
8649       break;
8650
8651     case 0x7:
8652       err = arm_decode_media (gdbarch, insn, dsc);
8653       break;
8654
8655     case 0x8: case 0x9: case 0xa: case 0xb:
8656       err = arm_decode_b_bl_ldmstm (gdbarch, insn, regs, dsc);
8657       break;
8658
8659     case 0xc: case 0xd: case 0xe: case 0xf:
8660       err = arm_decode_svc_copro (gdbarch, insn, to, regs, dsc);
8661       break;
8662     }
8663
8664   if (err)
8665     internal_error (__FILE__, __LINE__,
8666                     _("arm_process_displaced_insn: Instruction decode error"));
8667 }
8668
8669 /* Actually set up the scratch space for a displaced instruction.  */
8670
8671 void
8672 arm_displaced_init_closure (struct gdbarch *gdbarch, CORE_ADDR from,
8673                             CORE_ADDR to, struct displaced_step_closure *dsc)
8674 {
8675   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
8676   unsigned int i, len, offset;
8677   enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
8678   int size = dsc->is_thumb? 2 : 4;
8679   const gdb_byte *bkp_insn;
8680
8681   offset = 0;
8682   /* Poke modified instruction(s).  */
8683   for (i = 0; i < dsc->numinsns; i++)
8684     {
8685       if (debug_displaced)
8686         {
8687           fprintf_unfiltered (gdb_stdlog, "displaced: writing insn ");
8688           if (size == 4)
8689             fprintf_unfiltered (gdb_stdlog, "%.8lx",
8690                                 dsc->modinsn[i]);
8691           else if (size == 2)
8692             fprintf_unfiltered (gdb_stdlog, "%.4x",
8693                                 (unsigned short)dsc->modinsn[i]);
8694
8695           fprintf_unfiltered (gdb_stdlog, " at %.8lx\n",
8696                               (unsigned long) to + offset);
8697
8698         }
8699       write_memory_unsigned_integer (to + offset, size,
8700                                      byte_order_for_code,
8701                                      dsc->modinsn[i]);
8702       offset += size;
8703     }
8704
8705   /* Choose the correct breakpoint instruction.  */
8706   if (dsc->is_thumb)
8707     {
8708       bkp_insn = tdep->thumb_breakpoint;
8709       len = tdep->thumb_breakpoint_size;
8710     }
8711   else
8712     {
8713       bkp_insn = tdep->arm_breakpoint;
8714       len = tdep->arm_breakpoint_size;
8715     }
8716
8717   /* Put breakpoint afterwards.  */
8718   write_memory (to + offset, bkp_insn, len);
8719
8720   if (debug_displaced)
8721     fprintf_unfiltered (gdb_stdlog, "displaced: copy %s->%s: ",
8722                         paddress (gdbarch, from), paddress (gdbarch, to));
8723 }
8724
8725 /* Entry point for copying an instruction into scratch space for displaced
8726    stepping.  */
8727
8728 struct displaced_step_closure *
8729 arm_displaced_step_copy_insn (struct gdbarch *gdbarch,
8730                               CORE_ADDR from, CORE_ADDR to,
8731                               struct regcache *regs)
8732 {
8733   struct displaced_step_closure *dsc
8734     = xmalloc (sizeof (struct displaced_step_closure));
8735   arm_process_displaced_insn (gdbarch, from, to, regs, dsc);
8736   arm_displaced_init_closure (gdbarch, from, to, dsc);
8737
8738   return dsc;
8739 }
8740
8741 /* Entry point for cleaning things up after a displaced instruction has been
8742    single-stepped.  */
8743
8744 void
8745 arm_displaced_step_fixup (struct gdbarch *gdbarch,
8746                           struct displaced_step_closure *dsc,
8747                           CORE_ADDR from, CORE_ADDR to,
8748                           struct regcache *regs)
8749 {
8750   if (dsc->cleanup)
8751     dsc->cleanup (gdbarch, regs, dsc);
8752
8753   if (!dsc->wrote_to_pc)
8754     regcache_cooked_write_unsigned (regs, ARM_PC_REGNUM,
8755                                     dsc->insn_addr + dsc->insn_size);
8756
8757 }
8758
8759 #include "bfd-in2.h"
8760 #include "libcoff.h"
8761
8762 static int
8763 gdb_print_insn_arm (bfd_vma memaddr, disassemble_info *info)
8764 {
8765   struct gdbarch *gdbarch = info->application_data;
8766
8767   if (arm_pc_is_thumb (gdbarch, memaddr))
8768     {
8769       static asymbol *asym;
8770       static combined_entry_type ce;
8771       static struct coff_symbol_struct csym;
8772       static struct bfd fake_bfd;
8773       static bfd_target fake_target;
8774
8775       if (csym.native == NULL)
8776         {
8777           /* Create a fake symbol vector containing a Thumb symbol.
8778              This is solely so that the code in print_insn_little_arm() 
8779              and print_insn_big_arm() in opcodes/arm-dis.c will detect
8780              the presence of a Thumb symbol and switch to decoding
8781              Thumb instructions.  */
8782
8783           fake_target.flavour = bfd_target_coff_flavour;
8784           fake_bfd.xvec = &fake_target;
8785           ce.u.syment.n_sclass = C_THUMBEXTFUNC;
8786           csym.native = &ce;
8787           csym.symbol.the_bfd = &fake_bfd;
8788           csym.symbol.name = "fake";
8789           asym = (asymbol *) & csym;
8790         }
8791
8792       memaddr = UNMAKE_THUMB_ADDR (memaddr);
8793       info->symbols = &asym;
8794     }
8795   else
8796     info->symbols = NULL;
8797
8798   if (info->endian == BFD_ENDIAN_BIG)
8799     return print_insn_big_arm (memaddr, info);
8800   else
8801     return print_insn_little_arm (memaddr, info);
8802 }
8803
8804 /* The following define instruction sequences that will cause ARM
8805    cpu's to take an undefined instruction trap.  These are used to
8806    signal a breakpoint to GDB.
8807    
8808    The newer ARMv4T cpu's are capable of operating in ARM or Thumb
8809    modes.  A different instruction is required for each mode.  The ARM
8810    cpu's can also be big or little endian.  Thus four different
8811    instructions are needed to support all cases.
8812    
8813    Note: ARMv4 defines several new instructions that will take the
8814    undefined instruction trap.  ARM7TDMI is nominally ARMv4T, but does
8815    not in fact add the new instructions.  The new undefined
8816    instructions in ARMv4 are all instructions that had no defined
8817    behaviour in earlier chips.  There is no guarantee that they will
8818    raise an exception, but may be treated as NOP's.  In practice, it
8819    may only safe to rely on instructions matching:
8820    
8821    3 3 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1 
8822    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
8823    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
8824    
8825    Even this may only true if the condition predicate is true.  The
8826    following use a condition predicate of ALWAYS so it is always TRUE.
8827    
8828    There are other ways of forcing a breakpoint.  GNU/Linux, RISC iX,
8829    and NetBSD all use a software interrupt rather than an undefined
8830    instruction to force a trap.  This can be handled by by the
8831    abi-specific code during establishment of the gdbarch vector.  */
8832
8833 #define ARM_LE_BREAKPOINT {0xFE,0xDE,0xFF,0xE7}
8834 #define ARM_BE_BREAKPOINT {0xE7,0xFF,0xDE,0xFE}
8835 #define THUMB_LE_BREAKPOINT {0xbe,0xbe}
8836 #define THUMB_BE_BREAKPOINT {0xbe,0xbe}
8837
8838 static const gdb_byte arm_default_arm_le_breakpoint[] = ARM_LE_BREAKPOINT;
8839 static const gdb_byte arm_default_arm_be_breakpoint[] = ARM_BE_BREAKPOINT;
8840 static const gdb_byte arm_default_thumb_le_breakpoint[] = THUMB_LE_BREAKPOINT;
8841 static const gdb_byte arm_default_thumb_be_breakpoint[] = THUMB_BE_BREAKPOINT;
8842
8843 /* Determine the type and size of breakpoint to insert at PCPTR.  Uses
8844    the program counter value to determine whether a 16-bit or 32-bit
8845    breakpoint should be used.  It returns a pointer to a string of
8846    bytes that encode a breakpoint instruction, stores the length of
8847    the string to *lenptr, and adjusts the program counter (if
8848    necessary) to point to the actual memory location where the
8849    breakpoint should be inserted.  */
8850
8851 static const unsigned char *
8852 arm_breakpoint_from_pc (struct gdbarch *gdbarch, CORE_ADDR *pcptr, int *lenptr)
8853 {
8854   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
8855   enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
8856
8857   if (arm_pc_is_thumb (gdbarch, *pcptr))
8858     {
8859       *pcptr = UNMAKE_THUMB_ADDR (*pcptr);
8860
8861       /* If we have a separate 32-bit breakpoint instruction for Thumb-2,
8862          check whether we are replacing a 32-bit instruction.  */
8863       if (tdep->thumb2_breakpoint != NULL)
8864         {
8865           gdb_byte buf[2];
8866           if (target_read_memory (*pcptr, buf, 2) == 0)
8867             {
8868               unsigned short inst1;
8869               inst1 = extract_unsigned_integer (buf, 2, byte_order_for_code);
8870               if (thumb_insn_size (inst1) == 4)
8871                 {
8872                   *lenptr = tdep->thumb2_breakpoint_size;
8873                   return tdep->thumb2_breakpoint;
8874                 }
8875             }
8876         }
8877
8878       *lenptr = tdep->thumb_breakpoint_size;
8879       return tdep->thumb_breakpoint;
8880     }
8881   else
8882     {
8883       *lenptr = tdep->arm_breakpoint_size;
8884       return tdep->arm_breakpoint;
8885     }
8886 }
8887
8888 static void
8889 arm_remote_breakpoint_from_pc (struct gdbarch *gdbarch, CORE_ADDR *pcptr,
8890                                int *kindptr)
8891 {
8892   arm_breakpoint_from_pc (gdbarch, pcptr, kindptr);
8893
8894   if (arm_pc_is_thumb (gdbarch, *pcptr) && *kindptr == 4)
8895     /* The documented magic value for a 32-bit Thumb-2 breakpoint, so
8896        that this is not confused with a 32-bit ARM breakpoint.  */
8897     *kindptr = 3;
8898 }
8899
8900 /* Extract from an array REGBUF containing the (raw) register state a
8901    function return value of type TYPE, and copy that, in virtual
8902    format, into VALBUF.  */
8903
8904 static void
8905 arm_extract_return_value (struct type *type, struct regcache *regs,
8906                           gdb_byte *valbuf)
8907 {
8908   struct gdbarch *gdbarch = get_regcache_arch (regs);
8909   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
8910
8911   if (TYPE_CODE_FLT == TYPE_CODE (type))
8912     {
8913       switch (gdbarch_tdep (gdbarch)->fp_model)
8914         {
8915         case ARM_FLOAT_FPA:
8916           {
8917             /* The value is in register F0 in internal format.  We need to
8918                extract the raw value and then convert it to the desired
8919                internal type.  */
8920             bfd_byte tmpbuf[FP_REGISTER_SIZE];
8921
8922             regcache_cooked_read (regs, ARM_F0_REGNUM, tmpbuf);
8923             convert_from_extended (floatformat_from_type (type), tmpbuf,
8924                                    valbuf, gdbarch_byte_order (gdbarch));
8925           }
8926           break;
8927
8928         case ARM_FLOAT_SOFT_FPA:
8929         case ARM_FLOAT_SOFT_VFP:
8930           /* ARM_FLOAT_VFP can arise if this is a variadic function so
8931              not using the VFP ABI code.  */
8932         case ARM_FLOAT_VFP:
8933           regcache_cooked_read (regs, ARM_A1_REGNUM, valbuf);
8934           if (TYPE_LENGTH (type) > 4)
8935             regcache_cooked_read (regs, ARM_A1_REGNUM + 1,
8936                                   valbuf + INT_REGISTER_SIZE);
8937           break;
8938
8939         default:
8940           internal_error (__FILE__, __LINE__,
8941                           _("arm_extract_return_value: "
8942                             "Floating point model not supported"));
8943           break;
8944         }
8945     }
8946   else if (TYPE_CODE (type) == TYPE_CODE_INT
8947            || TYPE_CODE (type) == TYPE_CODE_CHAR
8948            || TYPE_CODE (type) == TYPE_CODE_BOOL
8949            || TYPE_CODE (type) == TYPE_CODE_PTR
8950            || TYPE_CODE (type) == TYPE_CODE_REF
8951            || TYPE_CODE (type) == TYPE_CODE_ENUM)
8952     {
8953       /* If the type is a plain integer, then the access is
8954          straight-forward.  Otherwise we have to play around a bit
8955          more.  */
8956       int len = TYPE_LENGTH (type);
8957       int regno = ARM_A1_REGNUM;
8958       ULONGEST tmp;
8959
8960       while (len > 0)
8961         {
8962           /* By using store_unsigned_integer we avoid having to do
8963              anything special for small big-endian values.  */
8964           regcache_cooked_read_unsigned (regs, regno++, &tmp);
8965           store_unsigned_integer (valbuf, 
8966                                   (len > INT_REGISTER_SIZE
8967                                    ? INT_REGISTER_SIZE : len),
8968                                   byte_order, tmp);
8969           len -= INT_REGISTER_SIZE;
8970           valbuf += INT_REGISTER_SIZE;
8971         }
8972     }
8973   else
8974     {
8975       /* For a structure or union the behaviour is as if the value had
8976          been stored to word-aligned memory and then loaded into 
8977          registers with 32-bit load instruction(s).  */
8978       int len = TYPE_LENGTH (type);
8979       int regno = ARM_A1_REGNUM;
8980       bfd_byte tmpbuf[INT_REGISTER_SIZE];
8981
8982       while (len > 0)
8983         {
8984           regcache_cooked_read (regs, regno++, tmpbuf);
8985           memcpy (valbuf, tmpbuf,
8986                   len > INT_REGISTER_SIZE ? INT_REGISTER_SIZE : len);
8987           len -= INT_REGISTER_SIZE;
8988           valbuf += INT_REGISTER_SIZE;
8989         }
8990     }
8991 }
8992
8993
8994 /* Will a function return an aggregate type in memory or in a
8995    register?  Return 0 if an aggregate type can be returned in a
8996    register, 1 if it must be returned in memory.  */
8997
8998 static int
8999 arm_return_in_memory (struct gdbarch *gdbarch, struct type *type)
9000 {
9001   int nRc;
9002   enum type_code code;
9003
9004   CHECK_TYPEDEF (type);
9005
9006   /* In the ARM ABI, "integer" like aggregate types are returned in
9007      registers.  For an aggregate type to be integer like, its size
9008      must be less than or equal to INT_REGISTER_SIZE and the
9009      offset of each addressable subfield must be zero.  Note that bit
9010      fields are not addressable, and all addressable subfields of
9011      unions always start at offset zero.
9012
9013      This function is based on the behaviour of GCC 2.95.1.
9014      See: gcc/arm.c: arm_return_in_memory() for details.
9015
9016      Note: All versions of GCC before GCC 2.95.2 do not set up the
9017      parameters correctly for a function returning the following
9018      structure: struct { float f;}; This should be returned in memory,
9019      not a register.  Richard Earnshaw sent me a patch, but I do not
9020      know of any way to detect if a function like the above has been
9021      compiled with the correct calling convention.  */
9022
9023   /* All aggregate types that won't fit in a register must be returned
9024      in memory.  */
9025   if (TYPE_LENGTH (type) > INT_REGISTER_SIZE)
9026     {
9027       return 1;
9028     }
9029
9030   /* The AAPCS says all aggregates not larger than a word are returned
9031      in a register.  */
9032   if (gdbarch_tdep (gdbarch)->arm_abi != ARM_ABI_APCS)
9033     return 0;
9034
9035   /* The only aggregate types that can be returned in a register are
9036      structs and unions.  Arrays must be returned in memory.  */
9037   code = TYPE_CODE (type);
9038   if ((TYPE_CODE_STRUCT != code) && (TYPE_CODE_UNION != code))
9039     {
9040       return 1;
9041     }
9042
9043   /* Assume all other aggregate types can be returned in a register.
9044      Run a check for structures, unions and arrays.  */
9045   nRc = 0;
9046
9047   if ((TYPE_CODE_STRUCT == code) || (TYPE_CODE_UNION == code))
9048     {
9049       int i;
9050       /* Need to check if this struct/union is "integer" like.  For
9051          this to be true, its size must be less than or equal to
9052          INT_REGISTER_SIZE and the offset of each addressable
9053          subfield must be zero.  Note that bit fields are not
9054          addressable, and unions always start at offset zero.  If any
9055          of the subfields is a floating point type, the struct/union
9056          cannot be an integer type.  */
9057
9058       /* For each field in the object, check:
9059          1) Is it FP? --> yes, nRc = 1;
9060          2) Is it addressable (bitpos != 0) and
9061          not packed (bitsize == 0)?
9062          --> yes, nRc = 1  
9063        */
9064
9065       for (i = 0; i < TYPE_NFIELDS (type); i++)
9066         {
9067           enum type_code field_type_code;
9068           field_type_code = TYPE_CODE (check_typedef (TYPE_FIELD_TYPE (type,
9069                                                                        i)));
9070
9071           /* Is it a floating point type field?  */
9072           if (field_type_code == TYPE_CODE_FLT)
9073             {
9074               nRc = 1;
9075               break;
9076             }
9077
9078           /* If bitpos != 0, then we have to care about it.  */
9079           if (TYPE_FIELD_BITPOS (type, i) != 0)
9080             {
9081               /* Bitfields are not addressable.  If the field bitsize is 
9082                  zero, then the field is not packed.  Hence it cannot be
9083                  a bitfield or any other packed type.  */
9084               if (TYPE_FIELD_BITSIZE (type, i) == 0)
9085                 {
9086                   nRc = 1;
9087                   break;
9088                 }
9089             }
9090         }
9091     }
9092
9093   return nRc;
9094 }
9095
9096 /* Write into appropriate registers a function return value of type
9097    TYPE, given in virtual format.  */
9098
9099 static void
9100 arm_store_return_value (struct type *type, struct regcache *regs,
9101                         const gdb_byte *valbuf)
9102 {
9103   struct gdbarch *gdbarch = get_regcache_arch (regs);
9104   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
9105
9106   if (TYPE_CODE (type) == TYPE_CODE_FLT)
9107     {
9108       gdb_byte buf[MAX_REGISTER_SIZE];
9109
9110       switch (gdbarch_tdep (gdbarch)->fp_model)
9111         {
9112         case ARM_FLOAT_FPA:
9113
9114           convert_to_extended (floatformat_from_type (type), buf, valbuf,
9115                                gdbarch_byte_order (gdbarch));
9116           regcache_cooked_write (regs, ARM_F0_REGNUM, buf);
9117           break;
9118
9119         case ARM_FLOAT_SOFT_FPA:
9120         case ARM_FLOAT_SOFT_VFP:
9121           /* ARM_FLOAT_VFP can arise if this is a variadic function so
9122              not using the VFP ABI code.  */
9123         case ARM_FLOAT_VFP:
9124           regcache_cooked_write (regs, ARM_A1_REGNUM, valbuf);
9125           if (TYPE_LENGTH (type) > 4)
9126             regcache_cooked_write (regs, ARM_A1_REGNUM + 1, 
9127                                    valbuf + INT_REGISTER_SIZE);
9128           break;
9129
9130         default:
9131           internal_error (__FILE__, __LINE__,
9132                           _("arm_store_return_value: Floating "
9133                             "point model not supported"));
9134           break;
9135         }
9136     }
9137   else if (TYPE_CODE (type) == TYPE_CODE_INT
9138            || TYPE_CODE (type) == TYPE_CODE_CHAR
9139            || TYPE_CODE (type) == TYPE_CODE_BOOL
9140            || TYPE_CODE (type) == TYPE_CODE_PTR
9141            || TYPE_CODE (type) == TYPE_CODE_REF
9142            || TYPE_CODE (type) == TYPE_CODE_ENUM)
9143     {
9144       if (TYPE_LENGTH (type) <= 4)
9145         {
9146           /* Values of one word or less are zero/sign-extended and
9147              returned in r0.  */
9148           bfd_byte tmpbuf[INT_REGISTER_SIZE];
9149           LONGEST val = unpack_long (type, valbuf);
9150
9151           store_signed_integer (tmpbuf, INT_REGISTER_SIZE, byte_order, val);
9152           regcache_cooked_write (regs, ARM_A1_REGNUM, tmpbuf);
9153         }
9154       else
9155         {
9156           /* Integral values greater than one word are stored in consecutive
9157              registers starting with r0.  This will always be a multiple of
9158              the regiser size.  */
9159           int len = TYPE_LENGTH (type);
9160           int regno = ARM_A1_REGNUM;
9161
9162           while (len > 0)
9163             {
9164               regcache_cooked_write (regs, regno++, valbuf);
9165               len -= INT_REGISTER_SIZE;
9166               valbuf += INT_REGISTER_SIZE;
9167             }
9168         }
9169     }
9170   else
9171     {
9172       /* For a structure or union the behaviour is as if the value had
9173          been stored to word-aligned memory and then loaded into 
9174          registers with 32-bit load instruction(s).  */
9175       int len = TYPE_LENGTH (type);
9176       int regno = ARM_A1_REGNUM;
9177       bfd_byte tmpbuf[INT_REGISTER_SIZE];
9178
9179       while (len > 0)
9180         {
9181           memcpy (tmpbuf, valbuf,
9182                   len > INT_REGISTER_SIZE ? INT_REGISTER_SIZE : len);
9183           regcache_cooked_write (regs, regno++, tmpbuf);
9184           len -= INT_REGISTER_SIZE;
9185           valbuf += INT_REGISTER_SIZE;
9186         }
9187     }
9188 }
9189
9190
9191 /* Handle function return values.  */
9192
9193 static enum return_value_convention
9194 arm_return_value (struct gdbarch *gdbarch, struct value *function,
9195                   struct type *valtype, struct regcache *regcache,
9196                   gdb_byte *readbuf, const gdb_byte *writebuf)
9197 {
9198   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
9199   struct type *func_type = function ? value_type (function) : NULL;
9200   enum arm_vfp_cprc_base_type vfp_base_type;
9201   int vfp_base_count;
9202
9203   if (arm_vfp_abi_for_function (gdbarch, func_type)
9204       && arm_vfp_call_candidate (valtype, &vfp_base_type, &vfp_base_count))
9205     {
9206       int reg_char = arm_vfp_cprc_reg_char (vfp_base_type);
9207       int unit_length = arm_vfp_cprc_unit_length (vfp_base_type);
9208       int i;
9209       for (i = 0; i < vfp_base_count; i++)
9210         {
9211           if (reg_char == 'q')
9212             {
9213               if (writebuf)
9214                 arm_neon_quad_write (gdbarch, regcache, i,
9215                                      writebuf + i * unit_length);
9216
9217               if (readbuf)
9218                 arm_neon_quad_read (gdbarch, regcache, i,
9219                                     readbuf + i * unit_length);
9220             }
9221           else
9222             {
9223               char name_buf[4];
9224               int regnum;
9225
9226               xsnprintf (name_buf, sizeof (name_buf), "%c%d", reg_char, i);
9227               regnum = user_reg_map_name_to_regnum (gdbarch, name_buf,
9228                                                     strlen (name_buf));
9229               if (writebuf)
9230                 regcache_cooked_write (regcache, regnum,
9231                                        writebuf + i * unit_length);
9232               if (readbuf)
9233                 regcache_cooked_read (regcache, regnum,
9234                                       readbuf + i * unit_length);
9235             }
9236         }
9237       return RETURN_VALUE_REGISTER_CONVENTION;
9238     }
9239
9240   if (TYPE_CODE (valtype) == TYPE_CODE_STRUCT
9241       || TYPE_CODE (valtype) == TYPE_CODE_UNION
9242       || TYPE_CODE (valtype) == TYPE_CODE_ARRAY)
9243     {
9244       if (tdep->struct_return == pcc_struct_return
9245           || arm_return_in_memory (gdbarch, valtype))
9246         return RETURN_VALUE_STRUCT_CONVENTION;
9247     }
9248
9249   /* AAPCS returns complex types longer than a register in memory.  */
9250   if (tdep->arm_abi != ARM_ABI_APCS
9251       && TYPE_CODE (valtype) == TYPE_CODE_COMPLEX
9252       && TYPE_LENGTH (valtype) > INT_REGISTER_SIZE)
9253     return RETURN_VALUE_STRUCT_CONVENTION;
9254
9255   if (writebuf)
9256     arm_store_return_value (valtype, regcache, writebuf);
9257
9258   if (readbuf)
9259     arm_extract_return_value (valtype, regcache, readbuf);
9260
9261   return RETURN_VALUE_REGISTER_CONVENTION;
9262 }
9263
9264
9265 static int
9266 arm_get_longjmp_target (struct frame_info *frame, CORE_ADDR *pc)
9267 {
9268   struct gdbarch *gdbarch = get_frame_arch (frame);
9269   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
9270   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
9271   CORE_ADDR jb_addr;
9272   gdb_byte buf[INT_REGISTER_SIZE];
9273   
9274   jb_addr = get_frame_register_unsigned (frame, ARM_A1_REGNUM);
9275
9276   if (target_read_memory (jb_addr + tdep->jb_pc * tdep->jb_elt_size, buf,
9277                           INT_REGISTER_SIZE))
9278     return 0;
9279
9280   *pc = extract_unsigned_integer (buf, INT_REGISTER_SIZE, byte_order);
9281   return 1;
9282 }
9283
9284 /* Recognize GCC and GNU ld's trampolines.  If we are in a trampoline,
9285    return the target PC.  Otherwise return 0.  */
9286
9287 CORE_ADDR
9288 arm_skip_stub (struct frame_info *frame, CORE_ADDR pc)
9289 {
9290   const char *name;
9291   int namelen;
9292   CORE_ADDR start_addr;
9293
9294   /* Find the starting address and name of the function containing the PC.  */
9295   if (find_pc_partial_function (pc, &name, &start_addr, NULL) == 0)
9296     {
9297       /* Trampoline 'bx reg' doesn't belong to any functions.  Do the
9298          check here.  */
9299       start_addr = arm_skip_bx_reg (frame, pc);
9300       if (start_addr != 0)
9301         return start_addr;
9302
9303       return 0;
9304     }
9305
9306   /* If PC is in a Thumb call or return stub, return the address of the
9307      target PC, which is in a register.  The thunk functions are called
9308      _call_via_xx, where x is the register name.  The possible names
9309      are r0-r9, sl, fp, ip, sp, and lr.  ARM RealView has similar
9310      functions, named __ARM_call_via_r[0-7].  */
9311   if (startswith (name, "_call_via_")
9312       || startswith (name, "__ARM_call_via_"))
9313     {
9314       /* Use the name suffix to determine which register contains the
9315          target PC.  */
9316       static char *table[15] =
9317       {"r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
9318        "r8", "r9", "sl", "fp", "ip", "sp", "lr"
9319       };
9320       int regno;
9321       int offset = strlen (name) - 2;
9322
9323       for (regno = 0; regno <= 14; regno++)
9324         if (strcmp (&name[offset], table[regno]) == 0)
9325           return get_frame_register_unsigned (frame, regno);
9326     }
9327
9328   /* GNU ld generates __foo_from_arm or __foo_from_thumb for
9329      non-interworking calls to foo.  We could decode the stubs
9330      to find the target but it's easier to use the symbol table.  */
9331   namelen = strlen (name);
9332   if (name[0] == '_' && name[1] == '_'
9333       && ((namelen > 2 + strlen ("_from_thumb")
9334            && startswith (name + namelen - strlen ("_from_thumb"), "_from_thumb"))
9335           || (namelen > 2 + strlen ("_from_arm")
9336               && startswith (name + namelen - strlen ("_from_arm"), "_from_arm"))))
9337     {
9338       char *target_name;
9339       int target_len = namelen - 2;
9340       struct bound_minimal_symbol minsym;
9341       struct objfile *objfile;
9342       struct obj_section *sec;
9343
9344       if (name[namelen - 1] == 'b')
9345         target_len -= strlen ("_from_thumb");
9346       else
9347         target_len -= strlen ("_from_arm");
9348
9349       target_name = alloca (target_len + 1);
9350       memcpy (target_name, name + 2, target_len);
9351       target_name[target_len] = '\0';
9352
9353       sec = find_pc_section (pc);
9354       objfile = (sec == NULL) ? NULL : sec->objfile;
9355       minsym = lookup_minimal_symbol (target_name, NULL, objfile);
9356       if (minsym.minsym != NULL)
9357         return BMSYMBOL_VALUE_ADDRESS (minsym);
9358       else
9359         return 0;
9360     }
9361
9362   return 0;                     /* not a stub */
9363 }
9364
9365 static void
9366 set_arm_command (char *args, int from_tty)
9367 {
9368   printf_unfiltered (_("\
9369 \"set arm\" must be followed by an apporpriate subcommand.\n"));
9370   help_list (setarmcmdlist, "set arm ", all_commands, gdb_stdout);
9371 }
9372
9373 static void
9374 show_arm_command (char *args, int from_tty)
9375 {
9376   cmd_show_list (showarmcmdlist, from_tty, "");
9377 }
9378
9379 static void
9380 arm_update_current_architecture (void)
9381 {
9382   struct gdbarch_info info;
9383
9384   /* If the current architecture is not ARM, we have nothing to do.  */
9385   if (gdbarch_bfd_arch_info (target_gdbarch ())->arch != bfd_arch_arm)
9386     return;
9387
9388   /* Update the architecture.  */
9389   gdbarch_info_init (&info);
9390
9391   if (!gdbarch_update_p (info))
9392     internal_error (__FILE__, __LINE__, _("could not update architecture"));
9393 }
9394
9395 static void
9396 set_fp_model_sfunc (char *args, int from_tty,
9397                     struct cmd_list_element *c)
9398 {
9399   int fp_model;
9400
9401   for (fp_model = ARM_FLOAT_AUTO; fp_model != ARM_FLOAT_LAST; fp_model++)
9402     if (strcmp (current_fp_model, fp_model_strings[fp_model]) == 0)
9403       {
9404         arm_fp_model = fp_model;
9405         break;
9406       }
9407
9408   if (fp_model == ARM_FLOAT_LAST)
9409     internal_error (__FILE__, __LINE__, _("Invalid fp model accepted: %s."),
9410                     current_fp_model);
9411
9412   arm_update_current_architecture ();
9413 }
9414
9415 static void
9416 show_fp_model (struct ui_file *file, int from_tty,
9417                struct cmd_list_element *c, const char *value)
9418 {
9419   struct gdbarch_tdep *tdep = gdbarch_tdep (target_gdbarch ());
9420
9421   if (arm_fp_model == ARM_FLOAT_AUTO
9422       && gdbarch_bfd_arch_info (target_gdbarch ())->arch == bfd_arch_arm)
9423     fprintf_filtered (file, _("\
9424 The current ARM floating point model is \"auto\" (currently \"%s\").\n"),
9425                       fp_model_strings[tdep->fp_model]);
9426   else
9427     fprintf_filtered (file, _("\
9428 The current ARM floating point model is \"%s\".\n"),
9429                       fp_model_strings[arm_fp_model]);
9430 }
9431
9432 static void
9433 arm_set_abi (char *args, int from_tty,
9434              struct cmd_list_element *c)
9435 {
9436   int arm_abi;
9437
9438   for (arm_abi = ARM_ABI_AUTO; arm_abi != ARM_ABI_LAST; arm_abi++)
9439     if (strcmp (arm_abi_string, arm_abi_strings[arm_abi]) == 0)
9440       {
9441         arm_abi_global = arm_abi;
9442         break;
9443       }
9444
9445   if (arm_abi == ARM_ABI_LAST)
9446     internal_error (__FILE__, __LINE__, _("Invalid ABI accepted: %s."),
9447                     arm_abi_string);
9448
9449   arm_update_current_architecture ();
9450 }
9451
9452 static void
9453 arm_show_abi (struct ui_file *file, int from_tty,
9454              struct cmd_list_element *c, const char *value)
9455 {
9456   struct gdbarch_tdep *tdep = gdbarch_tdep (target_gdbarch ());
9457
9458   if (arm_abi_global == ARM_ABI_AUTO
9459       && gdbarch_bfd_arch_info (target_gdbarch ())->arch == bfd_arch_arm)
9460     fprintf_filtered (file, _("\
9461 The current ARM ABI is \"auto\" (currently \"%s\").\n"),
9462                       arm_abi_strings[tdep->arm_abi]);
9463   else
9464     fprintf_filtered (file, _("The current ARM ABI is \"%s\".\n"),
9465                       arm_abi_string);
9466 }
9467
9468 static void
9469 arm_show_fallback_mode (struct ui_file *file, int from_tty,
9470                         struct cmd_list_element *c, const char *value)
9471 {
9472   fprintf_filtered (file,
9473                     _("The current execution mode assumed "
9474                       "(when symbols are unavailable) is \"%s\".\n"),
9475                     arm_fallback_mode_string);
9476 }
9477
9478 static void
9479 arm_show_force_mode (struct ui_file *file, int from_tty,
9480                      struct cmd_list_element *c, const char *value)
9481 {
9482   struct gdbarch_tdep *tdep = gdbarch_tdep (target_gdbarch ());
9483
9484   fprintf_filtered (file,
9485                     _("The current execution mode assumed "
9486                       "(even when symbols are available) is \"%s\".\n"),
9487                     arm_force_mode_string);
9488 }
9489
9490 /* If the user changes the register disassembly style used for info
9491    register and other commands, we have to also switch the style used
9492    in opcodes for disassembly output.  This function is run in the "set
9493    arm disassembly" command, and does that.  */
9494
9495 static void
9496 set_disassembly_style_sfunc (char *args, int from_tty,
9497                               struct cmd_list_element *c)
9498 {
9499   set_disassembly_style ();
9500 }
9501 \f
9502 /* Return the ARM register name corresponding to register I.  */
9503 static const char *
9504 arm_register_name (struct gdbarch *gdbarch, int i)
9505 {
9506   const int num_regs = gdbarch_num_regs (gdbarch);
9507
9508   if (gdbarch_tdep (gdbarch)->have_vfp_pseudos
9509       && i >= num_regs && i < num_regs + 32)
9510     {
9511       static const char *const vfp_pseudo_names[] = {
9512         "s0", "s1", "s2", "s3", "s4", "s5", "s6", "s7",
9513         "s8", "s9", "s10", "s11", "s12", "s13", "s14", "s15",
9514         "s16", "s17", "s18", "s19", "s20", "s21", "s22", "s23",
9515         "s24", "s25", "s26", "s27", "s28", "s29", "s30", "s31",
9516       };
9517
9518       return vfp_pseudo_names[i - num_regs];
9519     }
9520
9521   if (gdbarch_tdep (gdbarch)->have_neon_pseudos
9522       && i >= num_regs + 32 && i < num_regs + 32 + 16)
9523     {
9524       static const char *const neon_pseudo_names[] = {
9525         "q0", "q1", "q2", "q3", "q4", "q5", "q6", "q7",
9526         "q8", "q9", "q10", "q11", "q12", "q13", "q14", "q15",
9527       };
9528
9529       return neon_pseudo_names[i - num_regs - 32];
9530     }
9531
9532   if (i >= ARRAY_SIZE (arm_register_names))
9533     /* These registers are only supported on targets which supply
9534        an XML description.  */
9535     return "";
9536
9537   return arm_register_names[i];
9538 }
9539
9540 static void
9541 set_disassembly_style (void)
9542 {
9543   int current;
9544
9545   /* Find the style that the user wants.  */
9546   for (current = 0; current < num_disassembly_options; current++)
9547     if (disassembly_style == valid_disassembly_styles[current])
9548       break;
9549   gdb_assert (current < num_disassembly_options);
9550
9551   /* Synchronize the disassembler.  */
9552   set_arm_regname_option (current);
9553 }
9554
9555 /* Test whether the coff symbol specific value corresponds to a Thumb
9556    function.  */
9557
9558 static int
9559 coff_sym_is_thumb (int val)
9560 {
9561   return (val == C_THUMBEXT
9562           || val == C_THUMBSTAT
9563           || val == C_THUMBEXTFUNC
9564           || val == C_THUMBSTATFUNC
9565           || val == C_THUMBLABEL);
9566 }
9567
9568 /* arm_coff_make_msymbol_special()
9569    arm_elf_make_msymbol_special()
9570    
9571    These functions test whether the COFF or ELF symbol corresponds to
9572    an address in thumb code, and set a "special" bit in a minimal
9573    symbol to indicate that it does.  */
9574    
9575 static void
9576 arm_elf_make_msymbol_special(asymbol *sym, struct minimal_symbol *msym)
9577 {
9578   if (ARM_SYM_BRANCH_TYPE (&((elf_symbol_type *)sym)->internal_elf_sym)
9579       == ST_BRANCH_TO_THUMB)
9580     MSYMBOL_SET_SPECIAL (msym);
9581 }
9582
9583 static void
9584 arm_coff_make_msymbol_special(int val, struct minimal_symbol *msym)
9585 {
9586   if (coff_sym_is_thumb (val))
9587     MSYMBOL_SET_SPECIAL (msym);
9588 }
9589
9590 static void
9591 arm_objfile_data_free (struct objfile *objfile, void *arg)
9592 {
9593   struct arm_per_objfile *data = arg;
9594   unsigned int i;
9595
9596   for (i = 0; i < objfile->obfd->section_count; i++)
9597     VEC_free (arm_mapping_symbol_s, data->section_maps[i]);
9598 }
9599
9600 static void
9601 arm_record_special_symbol (struct gdbarch *gdbarch, struct objfile *objfile,
9602                            asymbol *sym)
9603 {
9604   const char *name = bfd_asymbol_name (sym);
9605   struct arm_per_objfile *data;
9606   VEC(arm_mapping_symbol_s) **map_p;
9607   struct arm_mapping_symbol new_map_sym;
9608
9609   gdb_assert (name[0] == '$');
9610   if (name[1] != 'a' && name[1] != 't' && name[1] != 'd')
9611     return;
9612
9613   data = objfile_data (objfile, arm_objfile_data_key);
9614   if (data == NULL)
9615     {
9616       data = OBSTACK_ZALLOC (&objfile->objfile_obstack,
9617                              struct arm_per_objfile);
9618       set_objfile_data (objfile, arm_objfile_data_key, data);
9619       data->section_maps = OBSTACK_CALLOC (&objfile->objfile_obstack,
9620                                            objfile->obfd->section_count,
9621                                            VEC(arm_mapping_symbol_s) *);
9622     }
9623   map_p = &data->section_maps[bfd_get_section (sym)->index];
9624
9625   new_map_sym.value = sym->value;
9626   new_map_sym.type = name[1];
9627
9628   /* Assume that most mapping symbols appear in order of increasing
9629      value.  If they were randomly distributed, it would be faster to
9630      always push here and then sort at first use.  */
9631   if (!VEC_empty (arm_mapping_symbol_s, *map_p))
9632     {
9633       struct arm_mapping_symbol *prev_map_sym;
9634
9635       prev_map_sym = VEC_last (arm_mapping_symbol_s, *map_p);
9636       if (prev_map_sym->value >= sym->value)
9637         {
9638           unsigned int idx;
9639           idx = VEC_lower_bound (arm_mapping_symbol_s, *map_p, &new_map_sym,
9640                                  arm_compare_mapping_symbols);
9641           VEC_safe_insert (arm_mapping_symbol_s, *map_p, idx, &new_map_sym);
9642           return;
9643         }
9644     }
9645
9646   VEC_safe_push (arm_mapping_symbol_s, *map_p, &new_map_sym);
9647 }
9648
9649 static void
9650 arm_write_pc (struct regcache *regcache, CORE_ADDR pc)
9651 {
9652   struct gdbarch *gdbarch = get_regcache_arch (regcache);
9653   regcache_cooked_write_unsigned (regcache, ARM_PC_REGNUM, pc);
9654
9655   /* If necessary, set the T bit.  */
9656   if (arm_apcs_32)
9657     {
9658       ULONGEST val, t_bit;
9659       regcache_cooked_read_unsigned (regcache, ARM_PS_REGNUM, &val);
9660       t_bit = arm_psr_thumb_bit (gdbarch);
9661       if (arm_pc_is_thumb (gdbarch, pc))
9662         regcache_cooked_write_unsigned (regcache, ARM_PS_REGNUM,
9663                                         val | t_bit);
9664       else
9665         regcache_cooked_write_unsigned (regcache, ARM_PS_REGNUM,
9666                                         val & ~t_bit);
9667     }
9668 }
9669
9670 /* Read the contents of a NEON quad register, by reading from two
9671    double registers.  This is used to implement the quad pseudo
9672    registers, and for argument passing in case the quad registers are
9673    missing; vectors are passed in quad registers when using the VFP
9674    ABI, even if a NEON unit is not present.  REGNUM is the index of
9675    the quad register, in [0, 15].  */
9676
9677 static enum register_status
9678 arm_neon_quad_read (struct gdbarch *gdbarch, struct regcache *regcache,
9679                     int regnum, gdb_byte *buf)
9680 {
9681   char name_buf[4];
9682   gdb_byte reg_buf[8];
9683   int offset, double_regnum;
9684   enum register_status status;
9685
9686   xsnprintf (name_buf, sizeof (name_buf), "d%d", regnum << 1);
9687   double_regnum = user_reg_map_name_to_regnum (gdbarch, name_buf,
9688                                                strlen (name_buf));
9689
9690   /* d0 is always the least significant half of q0.  */
9691   if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
9692     offset = 8;
9693   else
9694     offset = 0;
9695
9696   status = regcache_raw_read (regcache, double_regnum, reg_buf);
9697   if (status != REG_VALID)
9698     return status;
9699   memcpy (buf + offset, reg_buf, 8);
9700
9701   offset = 8 - offset;
9702   status = regcache_raw_read (regcache, double_regnum + 1, reg_buf);
9703   if (status != REG_VALID)
9704     return status;
9705   memcpy (buf + offset, reg_buf, 8);
9706
9707   return REG_VALID;
9708 }
9709
9710 static enum register_status
9711 arm_pseudo_read (struct gdbarch *gdbarch, struct regcache *regcache,
9712                  int regnum, gdb_byte *buf)
9713 {
9714   const int num_regs = gdbarch_num_regs (gdbarch);
9715   char name_buf[4];
9716   gdb_byte reg_buf[8];
9717   int offset, double_regnum;
9718
9719   gdb_assert (regnum >= num_regs);
9720   regnum -= num_regs;
9721
9722   if (gdbarch_tdep (gdbarch)->have_neon_pseudos && regnum >= 32 && regnum < 48)
9723     /* Quad-precision register.  */
9724     return arm_neon_quad_read (gdbarch, regcache, regnum - 32, buf);
9725   else
9726     {
9727       enum register_status status;
9728
9729       /* Single-precision register.  */
9730       gdb_assert (regnum < 32);
9731
9732       /* s0 is always the least significant half of d0.  */
9733       if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
9734         offset = (regnum & 1) ? 0 : 4;
9735       else
9736         offset = (regnum & 1) ? 4 : 0;
9737
9738       xsnprintf (name_buf, sizeof (name_buf), "d%d", regnum >> 1);
9739       double_regnum = user_reg_map_name_to_regnum (gdbarch, name_buf,
9740                                                    strlen (name_buf));
9741
9742       status = regcache_raw_read (regcache, double_regnum, reg_buf);
9743       if (status == REG_VALID)
9744         memcpy (buf, reg_buf + offset, 4);
9745       return status;
9746     }
9747 }
9748
9749 /* Store the contents of BUF to a NEON quad register, by writing to
9750    two double registers.  This is used to implement the quad pseudo
9751    registers, and for argument passing in case the quad registers are
9752    missing; vectors are passed in quad registers when using the VFP
9753    ABI, even if a NEON unit is not present.  REGNUM is the index
9754    of the quad register, in [0, 15].  */
9755
9756 static void
9757 arm_neon_quad_write (struct gdbarch *gdbarch, struct regcache *regcache,
9758                      int regnum, const gdb_byte *buf)
9759 {
9760   char name_buf[4];
9761   int offset, double_regnum;
9762
9763   xsnprintf (name_buf, sizeof (name_buf), "d%d", regnum << 1);
9764   double_regnum = user_reg_map_name_to_regnum (gdbarch, name_buf,
9765                                                strlen (name_buf));
9766
9767   /* d0 is always the least significant half of q0.  */
9768   if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
9769     offset = 8;
9770   else
9771     offset = 0;
9772
9773   regcache_raw_write (regcache, double_regnum, buf + offset);
9774   offset = 8 - offset;
9775   regcache_raw_write (regcache, double_regnum + 1, buf + offset);
9776 }
9777
9778 static void
9779 arm_pseudo_write (struct gdbarch *gdbarch, struct regcache *regcache,
9780                   int regnum, const gdb_byte *buf)
9781 {
9782   const int num_regs = gdbarch_num_regs (gdbarch);
9783   char name_buf[4];
9784   gdb_byte reg_buf[8];
9785   int offset, double_regnum;
9786
9787   gdb_assert (regnum >= num_regs);
9788   regnum -= num_regs;
9789
9790   if (gdbarch_tdep (gdbarch)->have_neon_pseudos && regnum >= 32 && regnum < 48)
9791     /* Quad-precision register.  */
9792     arm_neon_quad_write (gdbarch, regcache, regnum - 32, buf);
9793   else
9794     {
9795       /* Single-precision register.  */
9796       gdb_assert (regnum < 32);
9797
9798       /* s0 is always the least significant half of d0.  */
9799       if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
9800         offset = (regnum & 1) ? 0 : 4;
9801       else
9802         offset = (regnum & 1) ? 4 : 0;
9803
9804       xsnprintf (name_buf, sizeof (name_buf), "d%d", regnum >> 1);
9805       double_regnum = user_reg_map_name_to_regnum (gdbarch, name_buf,
9806                                                    strlen (name_buf));
9807
9808       regcache_raw_read (regcache, double_regnum, reg_buf);
9809       memcpy (reg_buf + offset, buf, 4);
9810       regcache_raw_write (regcache, double_regnum, reg_buf);
9811     }
9812 }
9813
9814 static struct value *
9815 value_of_arm_user_reg (struct frame_info *frame, const void *baton)
9816 {
9817   const int *reg_p = baton;
9818   return value_of_register (*reg_p, frame);
9819 }
9820 \f
9821 static enum gdb_osabi
9822 arm_elf_osabi_sniffer (bfd *abfd)
9823 {
9824   unsigned int elfosabi;
9825   enum gdb_osabi osabi = GDB_OSABI_UNKNOWN;
9826
9827   elfosabi = elf_elfheader (abfd)->e_ident[EI_OSABI];
9828
9829   if (elfosabi == ELFOSABI_ARM)
9830     /* GNU tools use this value.  Check note sections in this case,
9831        as well.  */
9832     bfd_map_over_sections (abfd,
9833                            generic_elf_osabi_sniff_abi_tag_sections, 
9834                            &osabi);
9835
9836   /* Anything else will be handled by the generic ELF sniffer.  */
9837   return osabi;
9838 }
9839
9840 static int
9841 arm_register_reggroup_p (struct gdbarch *gdbarch, int regnum,
9842                           struct reggroup *group)
9843 {
9844   /* FPS register's type is INT, but belongs to float_reggroup.  Beside
9845      this, FPS register belongs to save_regroup, restore_reggroup, and
9846      all_reggroup, of course.  */
9847   if (regnum == ARM_FPS_REGNUM)
9848     return (group == float_reggroup
9849             || group == save_reggroup
9850             || group == restore_reggroup
9851             || group == all_reggroup);
9852   else
9853     return default_register_reggroup_p (gdbarch, regnum, group);
9854 }
9855
9856 \f
9857 /* For backward-compatibility we allow two 'g' packet lengths with
9858    the remote protocol depending on whether FPA registers are
9859    supplied.  M-profile targets do not have FPA registers, but some
9860    stubs already exist in the wild which use a 'g' packet which
9861    supplies them albeit with dummy values.  The packet format which
9862    includes FPA registers should be considered deprecated for
9863    M-profile targets.  */
9864
9865 static void
9866 arm_register_g_packet_guesses (struct gdbarch *gdbarch)
9867 {
9868   if (gdbarch_tdep (gdbarch)->is_m)
9869     {
9870       /* If we know from the executable this is an M-profile target,
9871          cater for remote targets whose register set layout is the
9872          same as the FPA layout.  */
9873       register_remote_g_packet_guess (gdbarch,
9874                                       /* r0-r12,sp,lr,pc; f0-f7; fps,xpsr */
9875                                       (16 * INT_REGISTER_SIZE)
9876                                       + (8 * FP_REGISTER_SIZE)
9877                                       + (2 * INT_REGISTER_SIZE),
9878                                       tdesc_arm_with_m_fpa_layout);
9879
9880       /* The regular M-profile layout.  */
9881       register_remote_g_packet_guess (gdbarch,
9882                                       /* r0-r12,sp,lr,pc; xpsr */
9883                                       (16 * INT_REGISTER_SIZE)
9884                                       + INT_REGISTER_SIZE,
9885                                       tdesc_arm_with_m);
9886
9887       /* M-profile plus M4F VFP.  */
9888       register_remote_g_packet_guess (gdbarch,
9889                                       /* r0-r12,sp,lr,pc; d0-d15; fpscr,xpsr */
9890                                       (16 * INT_REGISTER_SIZE)
9891                                       + (16 * VFP_REGISTER_SIZE)
9892                                       + (2 * INT_REGISTER_SIZE),
9893                                       tdesc_arm_with_m_vfp_d16);
9894     }
9895
9896   /* Otherwise we don't have a useful guess.  */
9897 }
9898
9899 \f
9900 /* Initialize the current architecture based on INFO.  If possible,
9901    re-use an architecture from ARCHES, which is a list of
9902    architectures already created during this debugging session.
9903
9904    Called e.g. at program startup, when reading a core file, and when
9905    reading a binary file.  */
9906
9907 static struct gdbarch *
9908 arm_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
9909 {
9910   struct gdbarch_tdep *tdep;
9911   struct gdbarch *gdbarch;
9912   struct gdbarch_list *best_arch;
9913   enum arm_abi_kind arm_abi = arm_abi_global;
9914   enum arm_float_model fp_model = arm_fp_model;
9915   struct tdesc_arch_data *tdesc_data = NULL;
9916   int i, is_m = 0;
9917   int vfp_register_count = 0, have_vfp_pseudos = 0, have_neon_pseudos = 0;
9918   int have_wmmx_registers = 0;
9919   int have_neon = 0;
9920   int have_fpa_registers = 1;
9921   const struct target_desc *tdesc = info.target_desc;
9922
9923   /* If we have an object to base this architecture on, try to determine
9924      its ABI.  */
9925
9926   if (arm_abi == ARM_ABI_AUTO && info.abfd != NULL)
9927     {
9928       int ei_osabi, e_flags;
9929
9930       switch (bfd_get_flavour (info.abfd))
9931         {
9932         case bfd_target_aout_flavour:
9933           /* Assume it's an old APCS-style ABI.  */
9934           arm_abi = ARM_ABI_APCS;
9935           break;
9936
9937         case bfd_target_coff_flavour:
9938           /* Assume it's an old APCS-style ABI.  */
9939           /* XXX WinCE?  */
9940           arm_abi = ARM_ABI_APCS;
9941           break;
9942
9943         case bfd_target_elf_flavour:
9944           ei_osabi = elf_elfheader (info.abfd)->e_ident[EI_OSABI];
9945           e_flags = elf_elfheader (info.abfd)->e_flags;
9946
9947           if (ei_osabi == ELFOSABI_ARM)
9948             {
9949               /* GNU tools used to use this value, but do not for EABI
9950                  objects.  There's nowhere to tag an EABI version
9951                  anyway, so assume APCS.  */
9952               arm_abi = ARM_ABI_APCS;
9953             }
9954           else if (ei_osabi == ELFOSABI_NONE || ei_osabi == ELFOSABI_GNU)
9955             {
9956               int eabi_ver = EF_ARM_EABI_VERSION (e_flags);
9957               int attr_arch, attr_profile;
9958
9959               switch (eabi_ver)
9960                 {
9961                 case EF_ARM_EABI_UNKNOWN:
9962                   /* Assume GNU tools.  */
9963                   arm_abi = ARM_ABI_APCS;
9964                   break;
9965
9966                 case EF_ARM_EABI_VER4:
9967                 case EF_ARM_EABI_VER5:
9968                   arm_abi = ARM_ABI_AAPCS;
9969                   /* EABI binaries default to VFP float ordering.
9970                      They may also contain build attributes that can
9971                      be used to identify if the VFP argument-passing
9972                      ABI is in use.  */
9973                   if (fp_model == ARM_FLOAT_AUTO)
9974                     {
9975 #ifdef HAVE_ELF
9976                       switch (bfd_elf_get_obj_attr_int (info.abfd,
9977                                                         OBJ_ATTR_PROC,
9978                                                         Tag_ABI_VFP_args))
9979                         {
9980                         case AEABI_VFP_args_base:
9981                           /* "The user intended FP parameter/result
9982                              passing to conform to AAPCS, base
9983                              variant".  */
9984                           fp_model = ARM_FLOAT_SOFT_VFP;
9985                           break;
9986                         case AEABI_VFP_args_vfp:
9987                           /* "The user intended FP parameter/result
9988                              passing to conform to AAPCS, VFP
9989                              variant".  */
9990                           fp_model = ARM_FLOAT_VFP;
9991                           break;
9992                         case AEABI_VFP_args_toolchain:
9993                           /* "The user intended FP parameter/result
9994                              passing to conform to tool chain-specific
9995                              conventions" - we don't know any such
9996                              conventions, so leave it as "auto".  */
9997                           break;
9998                         case AEABI_VFP_args_compatible:
9999                           /* "Code is compatible with both the base
10000                              and VFP variants; the user did not permit
10001                              non-variadic functions to pass FP
10002                              parameters/results" - leave it as
10003                              "auto".  */
10004                           break;
10005                         default:
10006                           /* Attribute value not mentioned in the
10007                              November 2012 ABI, so leave it as
10008                              "auto".  */
10009                           break;
10010                         }
10011 #else
10012                       fp_model = ARM_FLOAT_SOFT_VFP;
10013 #endif
10014                     }
10015                   break;
10016
10017                 default:
10018                   /* Leave it as "auto".  */
10019                   warning (_("unknown ARM EABI version 0x%x"), eabi_ver);
10020                   break;
10021                 }
10022
10023 #ifdef HAVE_ELF
10024               /* Detect M-profile programs.  This only works if the
10025                  executable file includes build attributes; GCC does
10026                  copy them to the executable, but e.g. RealView does
10027                  not.  */
10028               attr_arch = bfd_elf_get_obj_attr_int (info.abfd, OBJ_ATTR_PROC,
10029                                                     Tag_CPU_arch);
10030               attr_profile = bfd_elf_get_obj_attr_int (info.abfd,
10031                                                        OBJ_ATTR_PROC,
10032                                                        Tag_CPU_arch_profile);
10033               /* GCC specifies the profile for v6-M; RealView only
10034                  specifies the profile for architectures starting with
10035                  V7 (as opposed to architectures with a tag
10036                  numerically greater than TAG_CPU_ARCH_V7).  */
10037               if (!tdesc_has_registers (tdesc)
10038                   && (attr_arch == TAG_CPU_ARCH_V6_M
10039                       || attr_arch == TAG_CPU_ARCH_V6S_M
10040                       || attr_profile == 'M'))
10041                 is_m = 1;
10042 #endif
10043             }
10044
10045           if (fp_model == ARM_FLOAT_AUTO)
10046             {
10047               int e_flags = elf_elfheader (info.abfd)->e_flags;
10048
10049               switch (e_flags & (EF_ARM_SOFT_FLOAT | EF_ARM_VFP_FLOAT))
10050                 {
10051                 case 0:
10052                   /* Leave it as "auto".  Strictly speaking this case
10053                      means FPA, but almost nobody uses that now, and
10054                      many toolchains fail to set the appropriate bits
10055                      for the floating-point model they use.  */
10056                   break;
10057                 case EF_ARM_SOFT_FLOAT:
10058                   fp_model = ARM_FLOAT_SOFT_FPA;
10059                   break;
10060                 case EF_ARM_VFP_FLOAT:
10061                   fp_model = ARM_FLOAT_VFP;
10062                   break;
10063                 case EF_ARM_SOFT_FLOAT | EF_ARM_VFP_FLOAT:
10064                   fp_model = ARM_FLOAT_SOFT_VFP;
10065                   break;
10066                 }
10067             }
10068
10069           if (e_flags & EF_ARM_BE8)
10070             info.byte_order_for_code = BFD_ENDIAN_LITTLE;
10071
10072           break;
10073
10074         default:
10075           /* Leave it as "auto".  */
10076           break;
10077         }
10078     }
10079
10080   /* Check any target description for validity.  */
10081   if (tdesc_has_registers (tdesc))
10082     {
10083       /* For most registers we require GDB's default names; but also allow
10084          the numeric names for sp / lr / pc, as a convenience.  */
10085       static const char *const arm_sp_names[] = { "r13", "sp", NULL };
10086       static const char *const arm_lr_names[] = { "r14", "lr", NULL };
10087       static const char *const arm_pc_names[] = { "r15", "pc", NULL };
10088
10089       const struct tdesc_feature *feature;
10090       int valid_p;
10091
10092       feature = tdesc_find_feature (tdesc,
10093                                     "org.gnu.gdb.arm.core");
10094       if (feature == NULL)
10095         {
10096           feature = tdesc_find_feature (tdesc,
10097                                         "org.gnu.gdb.arm.m-profile");
10098           if (feature == NULL)
10099             return NULL;
10100           else
10101             is_m = 1;
10102         }
10103
10104       tdesc_data = tdesc_data_alloc ();
10105
10106       valid_p = 1;
10107       for (i = 0; i < ARM_SP_REGNUM; i++)
10108         valid_p &= tdesc_numbered_register (feature, tdesc_data, i,
10109                                             arm_register_names[i]);
10110       valid_p &= tdesc_numbered_register_choices (feature, tdesc_data,
10111                                                   ARM_SP_REGNUM,
10112                                                   arm_sp_names);
10113       valid_p &= tdesc_numbered_register_choices (feature, tdesc_data,
10114                                                   ARM_LR_REGNUM,
10115                                                   arm_lr_names);
10116       valid_p &= tdesc_numbered_register_choices (feature, tdesc_data,
10117                                                   ARM_PC_REGNUM,
10118                                                   arm_pc_names);
10119       if (is_m)
10120         valid_p &= tdesc_numbered_register (feature, tdesc_data,
10121                                             ARM_PS_REGNUM, "xpsr");
10122       else
10123         valid_p &= tdesc_numbered_register (feature, tdesc_data,
10124                                             ARM_PS_REGNUM, "cpsr");
10125
10126       if (!valid_p)
10127         {
10128           tdesc_data_cleanup (tdesc_data);
10129           return NULL;
10130         }
10131
10132       feature = tdesc_find_feature (tdesc,
10133                                     "org.gnu.gdb.arm.fpa");
10134       if (feature != NULL)
10135         {
10136           valid_p = 1;
10137           for (i = ARM_F0_REGNUM; i <= ARM_FPS_REGNUM; i++)
10138             valid_p &= tdesc_numbered_register (feature, tdesc_data, i,
10139                                                 arm_register_names[i]);
10140           if (!valid_p)
10141             {
10142               tdesc_data_cleanup (tdesc_data);
10143               return NULL;
10144             }
10145         }
10146       else
10147         have_fpa_registers = 0;
10148
10149       feature = tdesc_find_feature (tdesc,
10150                                     "org.gnu.gdb.xscale.iwmmxt");
10151       if (feature != NULL)
10152         {
10153           static const char *const iwmmxt_names[] = {
10154             "wR0", "wR1", "wR2", "wR3", "wR4", "wR5", "wR6", "wR7",
10155             "wR8", "wR9", "wR10", "wR11", "wR12", "wR13", "wR14", "wR15",
10156             "wCID", "wCon", "wCSSF", "wCASF", "", "", "", "",
10157             "wCGR0", "wCGR1", "wCGR2", "wCGR3", "", "", "", "",
10158           };
10159
10160           valid_p = 1;
10161           for (i = ARM_WR0_REGNUM; i <= ARM_WR15_REGNUM; i++)
10162             valid_p
10163               &= tdesc_numbered_register (feature, tdesc_data, i,
10164                                           iwmmxt_names[i - ARM_WR0_REGNUM]);
10165
10166           /* Check for the control registers, but do not fail if they
10167              are missing.  */
10168           for (i = ARM_WC0_REGNUM; i <= ARM_WCASF_REGNUM; i++)
10169             tdesc_numbered_register (feature, tdesc_data, i,
10170                                      iwmmxt_names[i - ARM_WR0_REGNUM]);
10171
10172           for (i = ARM_WCGR0_REGNUM; i <= ARM_WCGR3_REGNUM; i++)
10173             valid_p
10174               &= tdesc_numbered_register (feature, tdesc_data, i,
10175                                           iwmmxt_names[i - ARM_WR0_REGNUM]);
10176
10177           if (!valid_p)
10178             {
10179               tdesc_data_cleanup (tdesc_data);
10180               return NULL;
10181             }
10182
10183           have_wmmx_registers = 1;
10184         }
10185
10186       /* If we have a VFP unit, check whether the single precision registers
10187          are present.  If not, then we will synthesize them as pseudo
10188          registers.  */
10189       feature = tdesc_find_feature (tdesc,
10190                                     "org.gnu.gdb.arm.vfp");
10191       if (feature != NULL)
10192         {
10193           static const char *const vfp_double_names[] = {
10194             "d0", "d1", "d2", "d3", "d4", "d5", "d6", "d7",
10195             "d8", "d9", "d10", "d11", "d12", "d13", "d14", "d15",
10196             "d16", "d17", "d18", "d19", "d20", "d21", "d22", "d23",
10197             "d24", "d25", "d26", "d27", "d28", "d29", "d30", "d31",
10198           };
10199
10200           /* Require the double precision registers.  There must be either
10201              16 or 32.  */
10202           valid_p = 1;
10203           for (i = 0; i < 32; i++)
10204             {
10205               valid_p &= tdesc_numbered_register (feature, tdesc_data,
10206                                                   ARM_D0_REGNUM + i,
10207                                                   vfp_double_names[i]);
10208               if (!valid_p)
10209                 break;
10210             }
10211           if (!valid_p && i == 16)
10212             valid_p = 1;
10213
10214           /* Also require FPSCR.  */
10215           valid_p &= tdesc_numbered_register (feature, tdesc_data,
10216                                               ARM_FPSCR_REGNUM, "fpscr");
10217           if (!valid_p)
10218             {
10219               tdesc_data_cleanup (tdesc_data);
10220               return NULL;
10221             }
10222
10223           if (tdesc_unnumbered_register (feature, "s0") == 0)
10224             have_vfp_pseudos = 1;
10225
10226           vfp_register_count = i;
10227
10228           /* If we have VFP, also check for NEON.  The architecture allows
10229              NEON without VFP (integer vector operations only), but GDB
10230              does not support that.  */
10231           feature = tdesc_find_feature (tdesc,
10232                                         "org.gnu.gdb.arm.neon");
10233           if (feature != NULL)
10234             {
10235               /* NEON requires 32 double-precision registers.  */
10236               if (i != 32)
10237                 {
10238                   tdesc_data_cleanup (tdesc_data);
10239                   return NULL;
10240                 }
10241
10242               /* If there are quad registers defined by the stub, use
10243                  their type; otherwise (normally) provide them with
10244                  the default type.  */
10245               if (tdesc_unnumbered_register (feature, "q0") == 0)
10246                 have_neon_pseudos = 1;
10247
10248               have_neon = 1;
10249             }
10250         }
10251     }
10252
10253   /* If there is already a candidate, use it.  */
10254   for (best_arch = gdbarch_list_lookup_by_info (arches, &info);
10255        best_arch != NULL;
10256        best_arch = gdbarch_list_lookup_by_info (best_arch->next, &info))
10257     {
10258       if (arm_abi != ARM_ABI_AUTO
10259           && arm_abi != gdbarch_tdep (best_arch->gdbarch)->arm_abi)
10260         continue;
10261
10262       if (fp_model != ARM_FLOAT_AUTO
10263           && fp_model != gdbarch_tdep (best_arch->gdbarch)->fp_model)
10264         continue;
10265
10266       /* There are various other properties in tdep that we do not
10267          need to check here: those derived from a target description,
10268          since gdbarches with a different target description are
10269          automatically disqualified.  */
10270
10271       /* Do check is_m, though, since it might come from the binary.  */
10272       if (is_m != gdbarch_tdep (best_arch->gdbarch)->is_m)
10273         continue;
10274
10275       /* Found a match.  */
10276       break;
10277     }
10278
10279   if (best_arch != NULL)
10280     {
10281       if (tdesc_data != NULL)
10282         tdesc_data_cleanup (tdesc_data);
10283       return best_arch->gdbarch;
10284     }
10285
10286   tdep = xcalloc (1, sizeof (struct gdbarch_tdep));
10287   gdbarch = gdbarch_alloc (&info, tdep);
10288
10289   /* Record additional information about the architecture we are defining.
10290      These are gdbarch discriminators, like the OSABI.  */
10291   tdep->arm_abi = arm_abi;
10292   tdep->fp_model = fp_model;
10293   tdep->is_m = is_m;
10294   tdep->have_fpa_registers = have_fpa_registers;
10295   tdep->have_wmmx_registers = have_wmmx_registers;
10296   gdb_assert (vfp_register_count == 0
10297               || vfp_register_count == 16
10298               || vfp_register_count == 32);
10299   tdep->vfp_register_count = vfp_register_count;
10300   tdep->have_vfp_pseudos = have_vfp_pseudos;
10301   tdep->have_neon_pseudos = have_neon_pseudos;
10302   tdep->have_neon = have_neon;
10303
10304   arm_register_g_packet_guesses (gdbarch);
10305
10306   /* Breakpoints.  */
10307   switch (info.byte_order_for_code)
10308     {
10309     case BFD_ENDIAN_BIG:
10310       tdep->arm_breakpoint = arm_default_arm_be_breakpoint;
10311       tdep->arm_breakpoint_size = sizeof (arm_default_arm_be_breakpoint);
10312       tdep->thumb_breakpoint = arm_default_thumb_be_breakpoint;
10313       tdep->thumb_breakpoint_size = sizeof (arm_default_thumb_be_breakpoint);
10314
10315       break;
10316
10317     case BFD_ENDIAN_LITTLE:
10318       tdep->arm_breakpoint = arm_default_arm_le_breakpoint;
10319       tdep->arm_breakpoint_size = sizeof (arm_default_arm_le_breakpoint);
10320       tdep->thumb_breakpoint = arm_default_thumb_le_breakpoint;
10321       tdep->thumb_breakpoint_size = sizeof (arm_default_thumb_le_breakpoint);
10322
10323       break;
10324
10325     default:
10326       internal_error (__FILE__, __LINE__,
10327                       _("arm_gdbarch_init: bad byte order for float format"));
10328     }
10329
10330   /* On ARM targets char defaults to unsigned.  */
10331   set_gdbarch_char_signed (gdbarch, 0);
10332
10333   /* Note: for displaced stepping, this includes the breakpoint, and one word
10334      of additional scratch space.  This setting isn't used for anything beside
10335      displaced stepping at present.  */
10336   set_gdbarch_max_insn_length (gdbarch, 4 * DISPLACED_MODIFIED_INSNS);
10337
10338   /* This should be low enough for everything.  */
10339   tdep->lowest_pc = 0x20;
10340   tdep->jb_pc = -1;     /* Longjump support not enabled by default.  */
10341
10342   /* The default, for both APCS and AAPCS, is to return small
10343      structures in registers.  */
10344   tdep->struct_return = reg_struct_return;
10345
10346   set_gdbarch_push_dummy_call (gdbarch, arm_push_dummy_call);
10347   set_gdbarch_frame_align (gdbarch, arm_frame_align);
10348
10349   set_gdbarch_write_pc (gdbarch, arm_write_pc);
10350
10351   /* Frame handling.  */
10352   set_gdbarch_dummy_id (gdbarch, arm_dummy_id);
10353   set_gdbarch_unwind_pc (gdbarch, arm_unwind_pc);
10354   set_gdbarch_unwind_sp (gdbarch, arm_unwind_sp);
10355
10356   frame_base_set_default (gdbarch, &arm_normal_base);
10357
10358   /* Address manipulation.  */
10359   set_gdbarch_addr_bits_remove (gdbarch, arm_addr_bits_remove);
10360
10361   /* Advance PC across function entry code.  */
10362   set_gdbarch_skip_prologue (gdbarch, arm_skip_prologue);
10363
10364   /* Detect whether PC is at a point where the stack has been destroyed.  */
10365   set_gdbarch_stack_frame_destroyed_p (gdbarch, arm_stack_frame_destroyed_p);
10366
10367   /* Skip trampolines.  */
10368   set_gdbarch_skip_trampoline_code (gdbarch, arm_skip_stub);
10369
10370   /* The stack grows downward.  */
10371   set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
10372
10373   /* Breakpoint manipulation.  */
10374   set_gdbarch_breakpoint_from_pc (gdbarch, arm_breakpoint_from_pc);
10375   set_gdbarch_remote_breakpoint_from_pc (gdbarch,
10376                                          arm_remote_breakpoint_from_pc);
10377
10378   /* Information about registers, etc.  */
10379   set_gdbarch_sp_regnum (gdbarch, ARM_SP_REGNUM);
10380   set_gdbarch_pc_regnum (gdbarch, ARM_PC_REGNUM);
10381   set_gdbarch_num_regs (gdbarch, ARM_NUM_REGS);
10382   set_gdbarch_register_type (gdbarch, arm_register_type);
10383   set_gdbarch_register_reggroup_p (gdbarch, arm_register_reggroup_p);
10384
10385   /* This "info float" is FPA-specific.  Use the generic version if we
10386      do not have FPA.  */
10387   if (gdbarch_tdep (gdbarch)->have_fpa_registers)
10388     set_gdbarch_print_float_info (gdbarch, arm_print_float_info);
10389
10390   /* Internal <-> external register number maps.  */
10391   set_gdbarch_dwarf2_reg_to_regnum (gdbarch, arm_dwarf_reg_to_regnum);
10392   set_gdbarch_register_sim_regno (gdbarch, arm_register_sim_regno);
10393
10394   set_gdbarch_register_name (gdbarch, arm_register_name);
10395
10396   /* Returning results.  */
10397   set_gdbarch_return_value (gdbarch, arm_return_value);
10398
10399   /* Disassembly.  */
10400   set_gdbarch_print_insn (gdbarch, gdb_print_insn_arm);
10401
10402   /* Minsymbol frobbing.  */
10403   set_gdbarch_elf_make_msymbol_special (gdbarch, arm_elf_make_msymbol_special);
10404   set_gdbarch_coff_make_msymbol_special (gdbarch,
10405                                          arm_coff_make_msymbol_special);
10406   set_gdbarch_record_special_symbol (gdbarch, arm_record_special_symbol);
10407
10408   /* Thumb-2 IT block support.  */
10409   set_gdbarch_adjust_breakpoint_address (gdbarch,
10410                                          arm_adjust_breakpoint_address);
10411
10412   /* Virtual tables.  */
10413   set_gdbarch_vbit_in_delta (gdbarch, 1);
10414
10415   /* Hook in the ABI-specific overrides, if they have been registered.  */
10416   gdbarch_init_osabi (info, gdbarch);
10417
10418   dwarf2_frame_set_init_reg (gdbarch, arm_dwarf2_frame_init_reg);
10419
10420   /* Add some default predicates.  */
10421   if (is_m)
10422     frame_unwind_append_unwinder (gdbarch, &arm_m_exception_unwind);
10423   frame_unwind_append_unwinder (gdbarch, &arm_stub_unwind);
10424   dwarf2_append_unwinders (gdbarch);
10425   frame_unwind_append_unwinder (gdbarch, &arm_exidx_unwind);
10426   frame_unwind_append_unwinder (gdbarch, &arm_prologue_unwind);
10427
10428   /* Now we have tuned the configuration, set a few final things,
10429      based on what the OS ABI has told us.  */
10430
10431   /* If the ABI is not otherwise marked, assume the old GNU APCS.  EABI
10432      binaries are always marked.  */
10433   if (tdep->arm_abi == ARM_ABI_AUTO)
10434     tdep->arm_abi = ARM_ABI_APCS;
10435
10436   /* Watchpoints are not steppable.  */
10437   set_gdbarch_have_nonsteppable_watchpoint (gdbarch, 1);
10438
10439   /* We used to default to FPA for generic ARM, but almost nobody
10440      uses that now, and we now provide a way for the user to force
10441      the model.  So default to the most useful variant.  */
10442   if (tdep->fp_model == ARM_FLOAT_AUTO)
10443     tdep->fp_model = ARM_FLOAT_SOFT_FPA;
10444
10445   if (tdep->jb_pc >= 0)
10446     set_gdbarch_get_longjmp_target (gdbarch, arm_get_longjmp_target);
10447
10448   /* Floating point sizes and format.  */
10449   set_gdbarch_float_format (gdbarch, floatformats_ieee_single);
10450   if (tdep->fp_model == ARM_FLOAT_SOFT_FPA || tdep->fp_model == ARM_FLOAT_FPA)
10451     {
10452       set_gdbarch_double_format
10453         (gdbarch, floatformats_ieee_double_littlebyte_bigword);
10454       set_gdbarch_long_double_format
10455         (gdbarch, floatformats_ieee_double_littlebyte_bigword);
10456     }
10457   else
10458     {
10459       set_gdbarch_double_format (gdbarch, floatformats_ieee_double);
10460       set_gdbarch_long_double_format (gdbarch, floatformats_ieee_double);
10461     }
10462
10463   if (have_vfp_pseudos)
10464     {
10465       /* NOTE: These are the only pseudo registers used by
10466          the ARM target at the moment.  If more are added, a
10467          little more care in numbering will be needed.  */
10468
10469       int num_pseudos = 32;
10470       if (have_neon_pseudos)
10471         num_pseudos += 16;
10472       set_gdbarch_num_pseudo_regs (gdbarch, num_pseudos);
10473       set_gdbarch_pseudo_register_read (gdbarch, arm_pseudo_read);
10474       set_gdbarch_pseudo_register_write (gdbarch, arm_pseudo_write);
10475     }
10476
10477   if (tdesc_data)
10478     {
10479       set_tdesc_pseudo_register_name (gdbarch, arm_register_name);
10480
10481       tdesc_use_registers (gdbarch, tdesc, tdesc_data);
10482
10483       /* Override tdesc_register_type to adjust the types of VFP
10484          registers for NEON.  */
10485       set_gdbarch_register_type (gdbarch, arm_register_type);
10486     }
10487
10488   /* Add standard register aliases.  We add aliases even for those
10489      nanes which are used by the current architecture - it's simpler,
10490      and does no harm, since nothing ever lists user registers.  */
10491   for (i = 0; i < ARRAY_SIZE (arm_register_aliases); i++)
10492     user_reg_add (gdbarch, arm_register_aliases[i].name,
10493                   value_of_arm_user_reg, &arm_register_aliases[i].regnum);
10494
10495   return gdbarch;
10496 }
10497
10498 static void
10499 arm_dump_tdep (struct gdbarch *gdbarch, struct ui_file *file)
10500 {
10501   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
10502
10503   if (tdep == NULL)
10504     return;
10505
10506   fprintf_unfiltered (file, _("arm_dump_tdep: Lowest pc = 0x%lx"),
10507                       (unsigned long) tdep->lowest_pc);
10508 }
10509
10510 extern initialize_file_ftype _initialize_arm_tdep; /* -Wmissing-prototypes */
10511
10512 void
10513 _initialize_arm_tdep (void)
10514 {
10515   struct ui_file *stb;
10516   long length;
10517   struct cmd_list_element *new_set, *new_show;
10518   const char *setname;
10519   const char *setdesc;
10520   const char *const *regnames;
10521   int numregs, i, j;
10522   static char *helptext;
10523   char regdesc[1024], *rdptr = regdesc;
10524   size_t rest = sizeof (regdesc);
10525
10526   gdbarch_register (bfd_arch_arm, arm_gdbarch_init, arm_dump_tdep);
10527
10528   arm_objfile_data_key
10529     = register_objfile_data_with_cleanup (NULL, arm_objfile_data_free);
10530
10531   /* Add ourselves to objfile event chain.  */
10532   observer_attach_new_objfile (arm_exidx_new_objfile);
10533   arm_exidx_data_key
10534     = register_objfile_data_with_cleanup (NULL, arm_exidx_data_free);
10535
10536   /* Register an ELF OS ABI sniffer for ARM binaries.  */
10537   gdbarch_register_osabi_sniffer (bfd_arch_arm,
10538                                   bfd_target_elf_flavour,
10539                                   arm_elf_osabi_sniffer);
10540
10541   /* Initialize the standard target descriptions.  */
10542   initialize_tdesc_arm_with_m ();
10543   initialize_tdesc_arm_with_m_fpa_layout ();
10544   initialize_tdesc_arm_with_m_vfp_d16 ();
10545   initialize_tdesc_arm_with_iwmmxt ();
10546   initialize_tdesc_arm_with_vfpv2 ();
10547   initialize_tdesc_arm_with_vfpv3 ();
10548   initialize_tdesc_arm_with_neon ();
10549
10550   /* Get the number of possible sets of register names defined in opcodes.  */
10551   num_disassembly_options = get_arm_regname_num_options ();
10552
10553   /* Add root prefix command for all "set arm"/"show arm" commands.  */
10554   add_prefix_cmd ("arm", no_class, set_arm_command,
10555                   _("Various ARM-specific commands."),
10556                   &setarmcmdlist, "set arm ", 0, &setlist);
10557
10558   add_prefix_cmd ("arm", no_class, show_arm_command,
10559                   _("Various ARM-specific commands."),
10560                   &showarmcmdlist, "show arm ", 0, &showlist);
10561
10562   /* Sync the opcode insn printer with our register viewer.  */
10563   parse_arm_disassembler_option ("reg-names-std");
10564
10565   /* Initialize the array that will be passed to
10566      add_setshow_enum_cmd().  */
10567   valid_disassembly_styles
10568     = xmalloc ((num_disassembly_options + 1) * sizeof (char *));
10569   for (i = 0; i < num_disassembly_options; i++)
10570     {
10571       numregs = get_arm_regnames (i, &setname, &setdesc, &regnames);
10572       valid_disassembly_styles[i] = setname;
10573       length = snprintf (rdptr, rest, "%s - %s\n", setname, setdesc);
10574       rdptr += length;
10575       rest -= length;
10576       /* When we find the default names, tell the disassembler to use
10577          them.  */
10578       if (!strcmp (setname, "std"))
10579         {
10580           disassembly_style = setname;
10581           set_arm_regname_option (i);
10582         }
10583     }
10584   /* Mark the end of valid options.  */
10585   valid_disassembly_styles[num_disassembly_options] = NULL;
10586
10587   /* Create the help text.  */
10588   stb = mem_fileopen ();
10589   fprintf_unfiltered (stb, "%s%s%s",
10590                       _("The valid values are:\n"),
10591                       regdesc,
10592                       _("The default is \"std\"."));
10593   helptext = ui_file_xstrdup (stb, NULL);
10594   ui_file_delete (stb);
10595
10596   add_setshow_enum_cmd("disassembler", no_class,
10597                        valid_disassembly_styles, &disassembly_style,
10598                        _("Set the disassembly style."),
10599                        _("Show the disassembly style."),
10600                        helptext,
10601                        set_disassembly_style_sfunc,
10602                        NULL, /* FIXME: i18n: The disassembly style is
10603                                 \"%s\".  */
10604                        &setarmcmdlist, &showarmcmdlist);
10605
10606   add_setshow_boolean_cmd ("apcs32", no_class, &arm_apcs_32,
10607                            _("Set usage of ARM 32-bit mode."),
10608                            _("Show usage of ARM 32-bit mode."),
10609                            _("When off, a 26-bit PC will be used."),
10610                            NULL,
10611                            NULL, /* FIXME: i18n: Usage of ARM 32-bit
10612                                     mode is %s.  */
10613                            &setarmcmdlist, &showarmcmdlist);
10614
10615   /* Add a command to allow the user to force the FPU model.  */
10616   add_setshow_enum_cmd ("fpu", no_class, fp_model_strings, &current_fp_model,
10617                         _("Set the floating point type."),
10618                         _("Show the floating point type."),
10619                         _("auto - Determine the FP typefrom the OS-ABI.\n\
10620 softfpa - Software FP, mixed-endian doubles on little-endian ARMs.\n\
10621 fpa - FPA co-processor (GCC compiled).\n\
10622 softvfp - Software FP with pure-endian doubles.\n\
10623 vfp - VFP co-processor."),
10624                         set_fp_model_sfunc, show_fp_model,
10625                         &setarmcmdlist, &showarmcmdlist);
10626
10627   /* Add a command to allow the user to force the ABI.  */
10628   add_setshow_enum_cmd ("abi", class_support, arm_abi_strings, &arm_abi_string,
10629                         _("Set the ABI."),
10630                         _("Show the ABI."),
10631                         NULL, arm_set_abi, arm_show_abi,
10632                         &setarmcmdlist, &showarmcmdlist);
10633
10634   /* Add two commands to allow the user to force the assumed
10635      execution mode.  */
10636   add_setshow_enum_cmd ("fallback-mode", class_support,
10637                         arm_mode_strings, &arm_fallback_mode_string,
10638                         _("Set the mode assumed when symbols are unavailable."),
10639                         _("Show the mode assumed when symbols are unavailable."),
10640                         NULL, NULL, arm_show_fallback_mode,
10641                         &setarmcmdlist, &showarmcmdlist);
10642   add_setshow_enum_cmd ("force-mode", class_support,
10643                         arm_mode_strings, &arm_force_mode_string,
10644                         _("Set the mode assumed even when symbols are available."),
10645                         _("Show the mode assumed even when symbols are available."),
10646                         NULL, NULL, arm_show_force_mode,
10647                         &setarmcmdlist, &showarmcmdlist);
10648
10649   /* Debugging flag.  */
10650   add_setshow_boolean_cmd ("arm", class_maintenance, &arm_debug,
10651                            _("Set ARM debugging."),
10652                            _("Show ARM debugging."),
10653                            _("When on, arm-specific debugging is enabled."),
10654                            NULL,
10655                            NULL, /* FIXME: i18n: "ARM debugging is %s.  */
10656                            &setdebuglist, &showdebuglist);
10657 }
10658
10659 /* ARM-reversible process record data structures.  */
10660
10661 #define ARM_INSN_SIZE_BYTES 4    
10662 #define THUMB_INSN_SIZE_BYTES 2
10663 #define THUMB2_INSN_SIZE_BYTES 4
10664
10665
10666 /* Position of the bit within a 32-bit ARM instruction
10667    that defines whether the instruction is a load or store.  */
10668 #define INSN_S_L_BIT_NUM 20
10669
10670 #define REG_ALLOC(REGS, LENGTH, RECORD_BUF) \
10671         do  \
10672           { \
10673             unsigned int reg_len = LENGTH; \
10674             if (reg_len) \
10675               { \
10676                 REGS = XNEWVEC (uint32_t, reg_len); \
10677                 memcpy(&REGS[0], &RECORD_BUF[0], sizeof(uint32_t)*LENGTH); \
10678               } \
10679           } \
10680         while (0)
10681
10682 #define MEM_ALLOC(MEMS, LENGTH, RECORD_BUF) \
10683         do  \
10684           { \
10685             unsigned int mem_len = LENGTH; \
10686             if (mem_len) \
10687             { \
10688               MEMS =  XNEWVEC (struct arm_mem_r, mem_len);  \
10689               memcpy(&MEMS->len, &RECORD_BUF[0], \
10690                      sizeof(struct arm_mem_r) * LENGTH); \
10691             } \
10692           } \
10693           while (0)
10694
10695 /* Checks whether insn is already recorded or yet to be decoded. (boolean expression).  */
10696 #define INSN_RECORDED(ARM_RECORD) \
10697         (0 != (ARM_RECORD)->reg_rec_count || 0 != (ARM_RECORD)->mem_rec_count)
10698
10699 /* ARM memory record structure.  */
10700 struct arm_mem_r
10701 {
10702   uint32_t len;    /* Record length.  */
10703   uint32_t addr;   /* Memory address.  */
10704 };
10705
10706 /* ARM instruction record contains opcode of current insn
10707    and execution state (before entry to decode_insn()),
10708    contains list of to-be-modified registers and
10709    memory blocks (on return from decode_insn()).  */
10710
10711 typedef struct insn_decode_record_t
10712 {
10713   struct gdbarch *gdbarch;
10714   struct regcache *regcache;
10715   CORE_ADDR this_addr;          /* Address of the insn being decoded.  */
10716   uint32_t arm_insn;            /* Should accommodate thumb.  */
10717   uint32_t cond;                /* Condition code.  */
10718   uint32_t opcode;              /* Insn opcode.  */
10719   uint32_t decode;              /* Insn decode bits.  */
10720   uint32_t mem_rec_count;       /* No of mem records.  */
10721   uint32_t reg_rec_count;       /* No of reg records.  */
10722   uint32_t *arm_regs;           /* Registers to be saved for this record.  */
10723   struct arm_mem_r *arm_mems;   /* Memory to be saved for this record.  */
10724 } insn_decode_record;
10725
10726
10727 /* Checks ARM SBZ and SBO mandatory fields.  */
10728
10729 static int
10730 sbo_sbz (uint32_t insn, uint32_t bit_num, uint32_t len, uint32_t sbo)
10731 {
10732   uint32_t ones = bits (insn, bit_num - 1, (bit_num -1) + (len - 1));
10733
10734   if (!len)
10735     return 1;
10736
10737   if (!sbo)
10738     ones = ~ones;
10739
10740   while (ones)
10741     {
10742       if (!(ones & sbo))
10743         {
10744           return 0;
10745         }
10746       ones = ones >> 1;
10747     }
10748   return 1;
10749 }
10750
10751 enum arm_record_result
10752 {
10753   ARM_RECORD_SUCCESS = 0,
10754   ARM_RECORD_FAILURE = 1
10755 };
10756
10757 typedef enum
10758 {
10759   ARM_RECORD_STRH=1,
10760   ARM_RECORD_STRD
10761 } arm_record_strx_t;
10762
10763 typedef enum
10764 {
10765   ARM_RECORD=1,
10766   THUMB_RECORD,
10767   THUMB2_RECORD
10768 } record_type_t;
10769
10770
10771 static int
10772 arm_record_strx (insn_decode_record *arm_insn_r, uint32_t *record_buf, 
10773                  uint32_t *record_buf_mem, arm_record_strx_t str_type)
10774 {
10775
10776   struct regcache *reg_cache = arm_insn_r->regcache;
10777   ULONGEST u_regval[2]= {0};
10778
10779   uint32_t reg_src1 = 0, reg_src2 = 0;
10780   uint32_t immed_high = 0, immed_low = 0,offset_8 = 0, tgt_mem_addr = 0;
10781   uint32_t opcode1 = 0;
10782
10783   arm_insn_r->opcode = bits (arm_insn_r->arm_insn, 21, 24);
10784   arm_insn_r->decode = bits (arm_insn_r->arm_insn, 4, 7);
10785   opcode1 = bits (arm_insn_r->arm_insn, 20, 24);
10786
10787
10788   if (14 == arm_insn_r->opcode || 10 == arm_insn_r->opcode)
10789     {
10790       /* 1) Handle misc store, immediate offset.  */
10791       immed_low = bits (arm_insn_r->arm_insn, 0, 3);
10792       immed_high = bits (arm_insn_r->arm_insn, 8, 11);
10793       reg_src1 = bits (arm_insn_r->arm_insn, 16, 19);
10794       regcache_raw_read_unsigned (reg_cache, reg_src1,
10795                                   &u_regval[0]);
10796       if (ARM_PC_REGNUM == reg_src1)
10797         {
10798           /* If R15 was used as Rn, hence current PC+8.  */
10799           u_regval[0] = u_regval[0] + 8;
10800         }
10801       offset_8 = (immed_high << 4) | immed_low;
10802       /* Calculate target store address.  */
10803       if (14 == arm_insn_r->opcode)
10804         {
10805           tgt_mem_addr = u_regval[0] + offset_8;
10806         }
10807       else
10808         {
10809           tgt_mem_addr = u_regval[0] - offset_8;
10810         }
10811       if (ARM_RECORD_STRH == str_type)
10812         {
10813           record_buf_mem[0] = 2;
10814           record_buf_mem[1] = tgt_mem_addr;
10815           arm_insn_r->mem_rec_count = 1;
10816         }
10817       else if (ARM_RECORD_STRD == str_type)
10818         {
10819           record_buf_mem[0] = 4;
10820           record_buf_mem[1] = tgt_mem_addr;
10821           record_buf_mem[2] = 4;
10822           record_buf_mem[3] = tgt_mem_addr + 4;
10823           arm_insn_r->mem_rec_count = 2;
10824         }
10825     }
10826   else if (12 == arm_insn_r->opcode || 8 == arm_insn_r->opcode)
10827     {
10828       /* 2) Store, register offset.  */
10829       /* Get Rm.  */
10830       reg_src1 = bits (arm_insn_r->arm_insn, 0, 3);
10831       /* Get Rn.  */
10832       reg_src2 = bits (arm_insn_r->arm_insn, 16, 19);
10833       regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval[0]);
10834       regcache_raw_read_unsigned (reg_cache, reg_src2, &u_regval[1]);
10835       if (15 == reg_src2)
10836         {
10837           /* If R15 was used as Rn, hence current PC+8.  */
10838           u_regval[0] = u_regval[0] + 8;
10839         }
10840       /* Calculate target store address, Rn +/- Rm, register offset.  */
10841       if (12 == arm_insn_r->opcode)
10842         {
10843           tgt_mem_addr = u_regval[0] + u_regval[1];
10844         }
10845       else
10846         {
10847           tgt_mem_addr = u_regval[1] - u_regval[0];
10848         }
10849       if (ARM_RECORD_STRH == str_type)
10850         {
10851           record_buf_mem[0] = 2;
10852           record_buf_mem[1] = tgt_mem_addr;
10853           arm_insn_r->mem_rec_count = 1;
10854         }
10855       else if (ARM_RECORD_STRD == str_type)
10856         {
10857           record_buf_mem[0] = 4;
10858           record_buf_mem[1] = tgt_mem_addr;
10859           record_buf_mem[2] = 4;
10860           record_buf_mem[3] = tgt_mem_addr + 4;
10861           arm_insn_r->mem_rec_count = 2;
10862         }
10863     }
10864   else if (11 == arm_insn_r->opcode || 15 == arm_insn_r->opcode
10865            || 2 == arm_insn_r->opcode  || 6 == arm_insn_r->opcode)
10866     {
10867       /* 3) Store, immediate pre-indexed.  */
10868       /* 5) Store, immediate post-indexed.  */
10869       immed_low = bits (arm_insn_r->arm_insn, 0, 3);
10870       immed_high = bits (arm_insn_r->arm_insn, 8, 11);
10871       offset_8 = (immed_high << 4) | immed_low;
10872       reg_src1 = bits (arm_insn_r->arm_insn, 16, 19);
10873       regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval[0]);
10874       /* Calculate target store address, Rn +/- Rm, register offset.  */
10875       if (15 == arm_insn_r->opcode || 6 == arm_insn_r->opcode)
10876         {
10877           tgt_mem_addr = u_regval[0] + offset_8;
10878         }
10879       else
10880         {
10881           tgt_mem_addr = u_regval[0] - offset_8;
10882         }
10883       if (ARM_RECORD_STRH == str_type)
10884         {
10885           record_buf_mem[0] = 2;
10886           record_buf_mem[1] = tgt_mem_addr;
10887           arm_insn_r->mem_rec_count = 1;
10888         }
10889       else if (ARM_RECORD_STRD == str_type)
10890         {
10891           record_buf_mem[0] = 4;
10892           record_buf_mem[1] = tgt_mem_addr;
10893           record_buf_mem[2] = 4;
10894           record_buf_mem[3] = tgt_mem_addr + 4;
10895           arm_insn_r->mem_rec_count = 2;
10896         }
10897       /* Record Rn also as it changes.  */
10898       *(record_buf) = bits (arm_insn_r->arm_insn, 16, 19);
10899       arm_insn_r->reg_rec_count = 1;
10900     }
10901   else if (9 == arm_insn_r->opcode || 13 == arm_insn_r->opcode
10902            || 0 == arm_insn_r->opcode || 4 == arm_insn_r->opcode)
10903     {
10904       /* 4) Store, register pre-indexed.  */
10905       /* 6) Store, register post -indexed.  */
10906       reg_src1 = bits (arm_insn_r->arm_insn, 0, 3);
10907       reg_src2 = bits (arm_insn_r->arm_insn, 16, 19);
10908       regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval[0]);
10909       regcache_raw_read_unsigned (reg_cache, reg_src2, &u_regval[1]);
10910       /* Calculate target store address, Rn +/- Rm, register offset.  */
10911       if (13 == arm_insn_r->opcode || 4 == arm_insn_r->opcode)
10912         {
10913           tgt_mem_addr = u_regval[0] + u_regval[1];
10914         }
10915       else
10916         {
10917           tgt_mem_addr = u_regval[1] - u_regval[0];
10918         }
10919       if (ARM_RECORD_STRH == str_type)
10920         {
10921           record_buf_mem[0] = 2;
10922           record_buf_mem[1] = tgt_mem_addr;
10923           arm_insn_r->mem_rec_count = 1;
10924         }
10925       else if (ARM_RECORD_STRD == str_type)
10926         {
10927           record_buf_mem[0] = 4;
10928           record_buf_mem[1] = tgt_mem_addr;
10929           record_buf_mem[2] = 4;
10930           record_buf_mem[3] = tgt_mem_addr + 4;
10931           arm_insn_r->mem_rec_count = 2;
10932         }
10933       /* Record Rn also as it changes.  */
10934       *(record_buf) = bits (arm_insn_r->arm_insn, 16, 19);
10935       arm_insn_r->reg_rec_count = 1;
10936     }
10937   return 0;
10938 }
10939
10940 /* Handling ARM extension space insns.  */
10941
10942 static int
10943 arm_record_extension_space (insn_decode_record *arm_insn_r)
10944 {
10945   uint32_t ret = 0;  /* Return value: -1:record failure ;  0:success  */
10946   uint32_t opcode1 = 0, opcode2 = 0, insn_op1 = 0;
10947   uint32_t record_buf[8], record_buf_mem[8];
10948   uint32_t reg_src1 = 0;
10949   uint32_t immed_high = 0, immed_low = 0,offset_8 = 0, tgt_mem_addr = 0;
10950   struct regcache *reg_cache = arm_insn_r->regcache;
10951   ULONGEST u_regval = 0;
10952
10953   gdb_assert (!INSN_RECORDED(arm_insn_r));
10954   /* Handle unconditional insn extension space.  */
10955
10956   opcode1 = bits (arm_insn_r->arm_insn, 20, 27);
10957   opcode2 = bits (arm_insn_r->arm_insn, 4, 7);
10958   if (arm_insn_r->cond)
10959     {
10960       /* PLD has no affect on architectural state, it just affects
10961          the caches.  */
10962       if (5 == ((opcode1 & 0xE0) >> 5))
10963         {
10964           /* BLX(1) */
10965           record_buf[0] = ARM_PS_REGNUM;
10966           record_buf[1] = ARM_LR_REGNUM;
10967           arm_insn_r->reg_rec_count = 2;
10968         }
10969       /* STC2, LDC2, MCR2, MRC2, CDP2: <TBD>, co-processor insn.  */
10970     }
10971
10972
10973   opcode1 = bits (arm_insn_r->arm_insn, 25, 27);
10974   if (3 == opcode1 && bit (arm_insn_r->arm_insn, 4))
10975     {
10976       ret = -1;
10977       /* Undefined instruction on ARM V5; need to handle if later 
10978          versions define it.  */
10979     }
10980
10981   opcode1 = bits (arm_insn_r->arm_insn, 24, 27);
10982   opcode2 = bits (arm_insn_r->arm_insn, 4, 7);
10983   insn_op1 = bits (arm_insn_r->arm_insn, 20, 23);
10984
10985   /* Handle arithmetic insn extension space.  */
10986   if (!opcode1 && 9 == opcode2 && 1 != arm_insn_r->cond
10987       && !INSN_RECORDED(arm_insn_r))
10988     {
10989       /* Handle MLA(S) and MUL(S).  */
10990       if (0 <= insn_op1 && 3 >= insn_op1)
10991       {
10992         record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
10993         record_buf[1] = ARM_PS_REGNUM;
10994         arm_insn_r->reg_rec_count = 2;
10995       }
10996       else if (4 <= insn_op1 && 15 >= insn_op1)
10997       {
10998         /* Handle SMLAL(S), SMULL(S), UMLAL(S), UMULL(S).  */
10999         record_buf[0] = bits (arm_insn_r->arm_insn, 16, 19);
11000         record_buf[1] = bits (arm_insn_r->arm_insn, 12, 15);
11001         record_buf[2] = ARM_PS_REGNUM;
11002         arm_insn_r->reg_rec_count = 3;
11003       }
11004     }
11005
11006   opcode1 = bits (arm_insn_r->arm_insn, 26, 27);
11007   opcode2 = bits (arm_insn_r->arm_insn, 23, 24);
11008   insn_op1 = bits (arm_insn_r->arm_insn, 21, 22);
11009
11010   /* Handle control insn extension space.  */
11011
11012   if (!opcode1 && 2 == opcode2 && !bit (arm_insn_r->arm_insn, 20)
11013       && 1 != arm_insn_r->cond && !INSN_RECORDED(arm_insn_r))
11014     {
11015       if (!bit (arm_insn_r->arm_insn,25))
11016         {
11017           if (!bits (arm_insn_r->arm_insn, 4, 7))
11018             {
11019               if ((0 == insn_op1) || (2 == insn_op1))
11020                 {
11021                   /* MRS.  */
11022                   record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11023                   arm_insn_r->reg_rec_count = 1;
11024                 }
11025               else if (1 == insn_op1)
11026                 {
11027                   /* CSPR is going to be changed.  */
11028                   record_buf[0] = ARM_PS_REGNUM;
11029                   arm_insn_r->reg_rec_count = 1;
11030                 }
11031               else if (3 == insn_op1)
11032                 {
11033                   /* SPSR is going to be changed.  */
11034                   /* We need to get SPSR value, which is yet to be done.  */
11035                   printf_unfiltered (_("Process record does not support "
11036                                      "instruction  0x%0x at address %s.\n"),
11037                                      arm_insn_r->arm_insn,
11038                                      paddress (arm_insn_r->gdbarch, 
11039                                      arm_insn_r->this_addr));
11040                   return -1;
11041                 }
11042             }
11043           else if (1 == bits (arm_insn_r->arm_insn, 4, 7))
11044             {
11045               if (1 == insn_op1)
11046                 {
11047                   /* BX.  */
11048                   record_buf[0] = ARM_PS_REGNUM;
11049                   arm_insn_r->reg_rec_count = 1;
11050                 }
11051               else if (3 == insn_op1)
11052                 {
11053                   /* CLZ.  */
11054                   record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11055                   arm_insn_r->reg_rec_count = 1;
11056                 }
11057             }
11058           else if (3 == bits (arm_insn_r->arm_insn, 4, 7))
11059             {
11060               /* BLX.  */
11061               record_buf[0] = ARM_PS_REGNUM;
11062               record_buf[1] = ARM_LR_REGNUM;
11063               arm_insn_r->reg_rec_count = 2;
11064             }
11065           else if (5 == bits (arm_insn_r->arm_insn, 4, 7))
11066             {
11067               /* QADD, QSUB, QDADD, QDSUB */
11068               record_buf[0] = ARM_PS_REGNUM;
11069               record_buf[1] = bits (arm_insn_r->arm_insn, 12, 15);
11070               arm_insn_r->reg_rec_count = 2;
11071             }
11072           else if (7 == bits (arm_insn_r->arm_insn, 4, 7))
11073             {
11074               /* BKPT.  */
11075               record_buf[0] = ARM_PS_REGNUM;
11076               record_buf[1] = ARM_LR_REGNUM;
11077               arm_insn_r->reg_rec_count = 2;
11078
11079               /* Save SPSR also;how?  */
11080               printf_unfiltered (_("Process record does not support "
11081                                   "instruction 0x%0x at address %s.\n"),
11082                                   arm_insn_r->arm_insn,
11083                   paddress (arm_insn_r->gdbarch, arm_insn_r->this_addr));
11084               return -1;
11085             }
11086           else if(8 == bits (arm_insn_r->arm_insn, 4, 7) 
11087                   || 10 == bits (arm_insn_r->arm_insn, 4, 7)
11088                   || 12 == bits (arm_insn_r->arm_insn, 4, 7)
11089                   || 14 == bits (arm_insn_r->arm_insn, 4, 7)
11090                  )
11091             {
11092               if (0 == insn_op1 || 1 == insn_op1)
11093                 {
11094                   /* SMLA<x><y>, SMLAW<y>, SMULW<y>.  */
11095                   /* We dont do optimization for SMULW<y> where we
11096                      need only Rd.  */
11097                   record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11098                   record_buf[1] = ARM_PS_REGNUM;
11099                   arm_insn_r->reg_rec_count = 2;
11100                 }
11101               else if (2 == insn_op1)
11102                 {
11103                   /* SMLAL<x><y>.  */
11104                   record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11105                   record_buf[1] = bits (arm_insn_r->arm_insn, 16, 19);
11106                   arm_insn_r->reg_rec_count = 2;
11107                 }
11108               else if (3 == insn_op1)
11109                 {
11110                   /* SMUL<x><y>.  */
11111                   record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11112                   arm_insn_r->reg_rec_count = 1;
11113                 }
11114             }
11115         }
11116       else
11117         {
11118           /* MSR : immediate form.  */
11119           if (1 == insn_op1)
11120             {
11121               /* CSPR is going to be changed.  */
11122               record_buf[0] = ARM_PS_REGNUM;
11123               arm_insn_r->reg_rec_count = 1;
11124             }
11125           else if (3 == insn_op1)
11126             {
11127               /* SPSR is going to be changed.  */
11128               /* we need to get SPSR value, which is yet to be done  */
11129               printf_unfiltered (_("Process record does not support "
11130                                    "instruction 0x%0x at address %s.\n"),
11131                                     arm_insn_r->arm_insn,
11132                                     paddress (arm_insn_r->gdbarch, 
11133                                     arm_insn_r->this_addr));
11134               return -1;
11135             }
11136         }
11137     }
11138
11139   opcode1 = bits (arm_insn_r->arm_insn, 25, 27);
11140   opcode2 = bits (arm_insn_r->arm_insn, 20, 24);
11141   insn_op1 = bits (arm_insn_r->arm_insn, 5, 6);
11142
11143   /* Handle load/store insn extension space.  */
11144
11145   if (!opcode1 && bit (arm_insn_r->arm_insn, 7) 
11146       && bit (arm_insn_r->arm_insn, 4) && 1 != arm_insn_r->cond
11147       && !INSN_RECORDED(arm_insn_r))
11148     {
11149       /* SWP/SWPB.  */
11150       if (0 == insn_op1)
11151         {
11152           /* These insn, changes register and memory as well.  */
11153           /* SWP or SWPB insn.  */
11154           /* Get memory address given by Rn.  */
11155           reg_src1 = bits (arm_insn_r->arm_insn, 16, 19);
11156           regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval);
11157           /* SWP insn ?, swaps word.  */
11158           if (8 == arm_insn_r->opcode)
11159             {
11160               record_buf_mem[0] = 4;
11161             }
11162           else
11163             {
11164               /* SWPB insn, swaps only byte.  */
11165               record_buf_mem[0] = 1;
11166             }
11167           record_buf_mem[1] = u_regval;
11168           arm_insn_r->mem_rec_count = 1;
11169           record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11170           arm_insn_r->reg_rec_count = 1;
11171         }
11172       else if (1 == insn_op1 && !bit (arm_insn_r->arm_insn, 20))
11173         {
11174           /* STRH.  */
11175           arm_record_strx(arm_insn_r, &record_buf[0], &record_buf_mem[0],
11176                           ARM_RECORD_STRH);
11177         }
11178       else if (2 == insn_op1 && !bit (arm_insn_r->arm_insn, 20))
11179         {
11180           /* LDRD.  */
11181           record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11182           record_buf[1] = record_buf[0] + 1;
11183           arm_insn_r->reg_rec_count = 2;
11184         }
11185       else if (3 == insn_op1 && !bit (arm_insn_r->arm_insn, 20))
11186         {
11187           /* STRD.  */
11188           arm_record_strx(arm_insn_r, &record_buf[0], &record_buf_mem[0],
11189                         ARM_RECORD_STRD);
11190         }
11191       else if (bit (arm_insn_r->arm_insn, 20) && insn_op1 <= 3)
11192         {
11193           /* LDRH, LDRSB, LDRSH.  */
11194           record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11195           arm_insn_r->reg_rec_count = 1;
11196         }
11197
11198     }
11199
11200   opcode1 = bits (arm_insn_r->arm_insn, 23, 27);
11201   if (24 == opcode1 && bit (arm_insn_r->arm_insn, 21)
11202       && !INSN_RECORDED(arm_insn_r))
11203     {
11204       ret = -1;
11205       /* Handle coprocessor insn extension space.  */
11206     }
11207
11208   /* To be done for ARMv5 and later; as of now we return -1.  */
11209   if (-1 == ret)
11210     printf_unfiltered (_("Process record does not support instruction x%0x "
11211                          "at address %s.\n"),arm_insn_r->arm_insn,
11212                          paddress (arm_insn_r->gdbarch, arm_insn_r->this_addr));
11213
11214
11215   REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
11216   MEM_ALLOC (arm_insn_r->arm_mems, arm_insn_r->mem_rec_count, record_buf_mem);
11217
11218   return ret;
11219 }
11220
11221 /* Handling opcode 000 insns.  */
11222
11223 static int
11224 arm_record_data_proc_misc_ld_str (insn_decode_record *arm_insn_r)
11225 {
11226   struct regcache *reg_cache = arm_insn_r->regcache;
11227   uint32_t record_buf[8], record_buf_mem[8];
11228   ULONGEST u_regval[2] = {0};
11229
11230   uint32_t reg_src1 = 0, reg_src2 = 0, reg_dest = 0;
11231   uint32_t immed_high = 0, immed_low = 0, offset_8 = 0, tgt_mem_addr = 0;
11232   uint32_t opcode1 = 0;
11233
11234   arm_insn_r->opcode = bits (arm_insn_r->arm_insn, 21, 24);
11235   arm_insn_r->decode = bits (arm_insn_r->arm_insn, 4, 7);
11236   opcode1 = bits (arm_insn_r->arm_insn, 20, 24);
11237
11238   /* Data processing insn /multiply insn.  */
11239   if (9 == arm_insn_r->decode
11240       && ((4 <= arm_insn_r->opcode && 7 >= arm_insn_r->opcode)
11241       ||  (0 == arm_insn_r->opcode || 1 == arm_insn_r->opcode)))
11242     {
11243       /* Handle multiply instructions.  */
11244       /* MLA, MUL, SMLAL, SMULL, UMLAL, UMULL.  */
11245         if (0 == arm_insn_r->opcode || 1 == arm_insn_r->opcode)
11246           {
11247             /* Handle MLA and MUL.  */
11248             record_buf[0] = bits (arm_insn_r->arm_insn, 16, 19);
11249             record_buf[1] = ARM_PS_REGNUM;
11250             arm_insn_r->reg_rec_count = 2;
11251           }
11252         else if (4 <= arm_insn_r->opcode && 7 >= arm_insn_r->opcode)
11253           {
11254             /* Handle SMLAL, SMULL, UMLAL, UMULL.  */
11255             record_buf[0] = bits (arm_insn_r->arm_insn, 16, 19);
11256             record_buf[1] = bits (arm_insn_r->arm_insn, 12, 15);
11257             record_buf[2] = ARM_PS_REGNUM;
11258             arm_insn_r->reg_rec_count = 3;
11259           }
11260     }
11261   else if (bit (arm_insn_r->arm_insn, INSN_S_L_BIT_NUM)
11262            && (11 == arm_insn_r->decode || 13 == arm_insn_r->decode))
11263     {
11264       /* Handle misc load insns, as 20th bit  (L = 1).  */
11265       /* LDR insn has a capability to do branching, if
11266          MOV LR, PC is precceded by LDR insn having Rn as R15
11267          in that case, it emulates branch and link insn, and hence we 
11268          need to save CSPR and PC as well. I am not sure this is right
11269          place; as opcode = 010 LDR insn make this happen, if R15 was
11270          used.  */
11271       reg_dest = bits (arm_insn_r->arm_insn, 12, 15);
11272       if (15 != reg_dest)
11273         {
11274           record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11275           arm_insn_r->reg_rec_count = 1;
11276         }
11277       else
11278         {
11279           record_buf[0] = reg_dest;
11280           record_buf[1] = ARM_PS_REGNUM;
11281           arm_insn_r->reg_rec_count = 2;
11282         }
11283     }
11284   else if ((9 == arm_insn_r->opcode || 11 == arm_insn_r->opcode)
11285            && sbo_sbz (arm_insn_r->arm_insn, 5, 12, 0)
11286            && sbo_sbz (arm_insn_r->arm_insn, 13, 4, 1)
11287            && 2 == bits (arm_insn_r->arm_insn, 20, 21))
11288     {
11289       /* Handle MSR insn.  */
11290       if (9 == arm_insn_r->opcode)
11291         {
11292           /* CSPR is going to be changed.  */
11293           record_buf[0] = ARM_PS_REGNUM;
11294           arm_insn_r->reg_rec_count = 1;
11295         }
11296       else
11297         {
11298           /* SPSR is going to be changed.  */
11299           /* How to read SPSR value?  */
11300           printf_unfiltered (_("Process record does not support instruction "
11301                             "0x%0x at address %s.\n"),
11302                             arm_insn_r->arm_insn,
11303                         paddress (arm_insn_r->gdbarch, arm_insn_r->this_addr));
11304           return -1;
11305         }
11306     }
11307   else if (9 == arm_insn_r->decode
11308            && (8 == arm_insn_r->opcode || 10 == arm_insn_r->opcode)
11309            && !bit (arm_insn_r->arm_insn, INSN_S_L_BIT_NUM))
11310     {
11311       /* Handling SWP, SWPB.  */
11312       /* These insn, changes register and memory as well.  */
11313       /* SWP or SWPB insn.  */
11314
11315       reg_src1 = bits (arm_insn_r->arm_insn, 16, 19);
11316       regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval[0]);
11317       /* SWP insn ?, swaps word.  */
11318       if (8 == arm_insn_r->opcode)
11319         {
11320           record_buf_mem[0] = 4;
11321         }
11322         else
11323         {
11324           /* SWPB insn, swaps only byte.  */
11325           record_buf_mem[0] = 1;
11326         }
11327       record_buf_mem[1] = u_regval[0];
11328       arm_insn_r->mem_rec_count = 1;
11329       record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11330       arm_insn_r->reg_rec_count = 1;
11331     }
11332   else if (3 == arm_insn_r->decode && 0x12 == opcode1
11333            && sbo_sbz (arm_insn_r->arm_insn, 9, 12, 1))
11334     {
11335       /* Handle BLX, branch and link/exchange.  */
11336       if (9 == arm_insn_r->opcode)
11337       {
11338         /* Branch is chosen by setting T bit of CSPR, bitp[0] of Rm,
11339            and R14 stores the return address.  */
11340         record_buf[0] = ARM_PS_REGNUM;
11341         record_buf[1] = ARM_LR_REGNUM;
11342         arm_insn_r->reg_rec_count = 2;
11343       }
11344     }
11345   else if (7 == arm_insn_r->decode && 0x12 == opcode1)
11346     {
11347       /* Handle enhanced software breakpoint insn, BKPT.  */
11348       /* CPSR is changed to be executed in ARM state,  disabling normal
11349          interrupts, entering abort mode.  */
11350       /* According to high vector configuration PC is set.  */
11351       /* user hit breakpoint and type reverse, in
11352          that case, we need to go back with previous CPSR and
11353          Program Counter.  */
11354       record_buf[0] = ARM_PS_REGNUM;
11355       record_buf[1] = ARM_LR_REGNUM;
11356       arm_insn_r->reg_rec_count = 2;
11357
11358       /* Save SPSR also; how?  */
11359       printf_unfiltered (_("Process record does not support instruction "
11360                            "0x%0x at address %s.\n"),arm_insn_r->arm_insn,
11361                            paddress (arm_insn_r->gdbarch, 
11362                            arm_insn_r->this_addr));
11363       return -1;
11364     }
11365   else if (11 == arm_insn_r->decode
11366            && !bit (arm_insn_r->arm_insn, INSN_S_L_BIT_NUM))
11367   {
11368     /* Handle enhanced store insns and DSP insns (e.g. LDRD).  */
11369
11370     /* Handle str(x) insn */
11371     arm_record_strx(arm_insn_r, &record_buf[0], &record_buf_mem[0],
11372                     ARM_RECORD_STRH);
11373   }
11374   else if (1 == arm_insn_r->decode && 0x12 == opcode1
11375            && sbo_sbz (arm_insn_r->arm_insn, 9, 12, 1))
11376     {
11377       /* Handle BX, branch and link/exchange.  */
11378       /* Branch is chosen by setting T bit of CSPR, bitp[0] of Rm.  */
11379       record_buf[0] = ARM_PS_REGNUM;
11380       arm_insn_r->reg_rec_count = 1;
11381     }
11382   else if (1 == arm_insn_r->decode && 0x16 == opcode1
11383            && sbo_sbz (arm_insn_r->arm_insn, 9, 4, 1)
11384            && sbo_sbz (arm_insn_r->arm_insn, 17, 4, 1))
11385     {
11386       /* Count leading zeros: CLZ.  */
11387       record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11388       arm_insn_r->reg_rec_count = 1;
11389     }
11390   else if (!bit (arm_insn_r->arm_insn, INSN_S_L_BIT_NUM)
11391            && (8 == arm_insn_r->opcode || 10 == arm_insn_r->opcode)
11392            && sbo_sbz (arm_insn_r->arm_insn, 17, 4, 1)
11393            && sbo_sbz (arm_insn_r->arm_insn, 1, 12, 0)
11394           )
11395     {
11396       /* Handle MRS insn.  */
11397       record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11398       arm_insn_r->reg_rec_count = 1;
11399     }
11400   else if (arm_insn_r->opcode <= 15)
11401     {
11402       /* Normal data processing insns.  */
11403       /* Out of 11 shifter operands mode, all the insn modifies destination
11404          register, which is specified by 13-16 decode.  */
11405       record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11406       record_buf[1] = ARM_PS_REGNUM;
11407       arm_insn_r->reg_rec_count = 2;
11408     }
11409   else
11410     {
11411       return -1;
11412     }
11413
11414   REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
11415   MEM_ALLOC (arm_insn_r->arm_mems, arm_insn_r->mem_rec_count, record_buf_mem);
11416   return 0;
11417 }
11418
11419 /* Handling opcode 001 insns.  */
11420
11421 static int
11422 arm_record_data_proc_imm (insn_decode_record *arm_insn_r)
11423 {
11424   uint32_t record_buf[8], record_buf_mem[8];
11425
11426   arm_insn_r->opcode = bits (arm_insn_r->arm_insn, 21, 24);
11427   arm_insn_r->decode = bits (arm_insn_r->arm_insn, 4, 7);
11428
11429   if ((9 == arm_insn_r->opcode || 11 == arm_insn_r->opcode)
11430       && 2 == bits (arm_insn_r->arm_insn, 20, 21)
11431       && sbo_sbz (arm_insn_r->arm_insn, 13, 4, 1)
11432      )
11433     {
11434       /* Handle MSR insn.  */
11435       if (9 == arm_insn_r->opcode)
11436         {
11437           /* CSPR is going to be changed.  */
11438           record_buf[0] = ARM_PS_REGNUM;
11439           arm_insn_r->reg_rec_count = 1;
11440         }
11441       else
11442         {
11443           /* SPSR is going to be changed.  */
11444         }
11445     }
11446   else if (arm_insn_r->opcode <= 15)
11447     {
11448       /* Normal data processing insns.  */
11449       /* Out of 11 shifter operands mode, all the insn modifies destination
11450          register, which is specified by 13-16 decode.  */
11451       record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11452       record_buf[1] = ARM_PS_REGNUM;
11453       arm_insn_r->reg_rec_count = 2;
11454     }
11455   else
11456     {
11457       return -1;
11458     }
11459
11460   REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
11461   MEM_ALLOC (arm_insn_r->arm_mems, arm_insn_r->mem_rec_count, record_buf_mem);
11462   return 0;
11463 }
11464
11465 /* Handle ARM mode instructions with opcode 010.  */
11466
11467 static int
11468 arm_record_ld_st_imm_offset (insn_decode_record *arm_insn_r)
11469 {
11470   struct regcache *reg_cache = arm_insn_r->regcache;
11471
11472   uint32_t reg_base , reg_dest;
11473   uint32_t offset_12, tgt_mem_addr;
11474   uint32_t record_buf[8], record_buf_mem[8];
11475   unsigned char wback;
11476   ULONGEST u_regval;
11477
11478   /* Calculate wback.  */
11479   wback = (bit (arm_insn_r->arm_insn, 24) == 0)
11480           || (bit (arm_insn_r->arm_insn, 21) == 1);
11481
11482   arm_insn_r->reg_rec_count = 0;
11483   reg_base = bits (arm_insn_r->arm_insn, 16, 19);
11484
11485   if (bit (arm_insn_r->arm_insn, INSN_S_L_BIT_NUM))
11486     {
11487       /* LDR (immediate), LDR (literal), LDRB (immediate), LDRB (literal), LDRBT
11488          and LDRT.  */
11489
11490       reg_dest = bits (arm_insn_r->arm_insn, 12, 15);
11491       record_buf[arm_insn_r->reg_rec_count++] = reg_dest;
11492
11493       /* The LDR instruction is capable of doing branching.  If MOV LR, PC
11494          preceeds a LDR instruction having R15 as reg_base, it
11495          emulates a branch and link instruction, and hence we need to save
11496          CPSR and PC as well.  */
11497       if (ARM_PC_REGNUM == reg_dest)
11498         record_buf[arm_insn_r->reg_rec_count++] = ARM_PS_REGNUM;
11499
11500       /* If wback is true, also save the base register, which is going to be
11501          written to.  */
11502       if (wback)
11503         record_buf[arm_insn_r->reg_rec_count++] = reg_base;
11504     }
11505   else
11506     {
11507       /* STR (immediate), STRB (immediate), STRBT and STRT.  */
11508
11509       offset_12 = bits (arm_insn_r->arm_insn, 0, 11);
11510       regcache_raw_read_unsigned (reg_cache, reg_base, &u_regval);
11511
11512       /* Handle bit U.  */
11513       if (bit (arm_insn_r->arm_insn, 23))
11514         {
11515           /* U == 1: Add the offset. */
11516           tgt_mem_addr = (uint32_t) u_regval + offset_12;
11517         }
11518       else
11519         {
11520           /* U == 0: subtract the offset. */
11521           tgt_mem_addr = (uint32_t) u_regval - offset_12;
11522         }
11523
11524       /* Bit 22 tells us whether the store instruction writes 1 byte or 4
11525          bytes.  */
11526       if (bit (arm_insn_r->arm_insn, 22))
11527         {
11528           /* STRB and STRBT: 1 byte.  */
11529           record_buf_mem[0] = 1;
11530         }
11531       else
11532         {
11533           /* STR and STRT: 4 bytes.  */
11534           record_buf_mem[0] = 4;
11535         }
11536
11537       /* Handle bit P.  */
11538       if (bit (arm_insn_r->arm_insn, 24))
11539         record_buf_mem[1] = tgt_mem_addr;
11540       else
11541         record_buf_mem[1] = (uint32_t) u_regval;
11542
11543       arm_insn_r->mem_rec_count = 1;
11544
11545       /* If wback is true, also save the base register, which is going to be
11546          written to.  */
11547       if (wback)
11548         record_buf[arm_insn_r->reg_rec_count++] = reg_base;
11549     }
11550
11551   REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
11552   MEM_ALLOC (arm_insn_r->arm_mems, arm_insn_r->mem_rec_count, record_buf_mem);
11553   return 0;
11554 }
11555
11556 /* Handling opcode 011 insns.  */
11557
11558 static int
11559 arm_record_ld_st_reg_offset (insn_decode_record *arm_insn_r)
11560 {
11561   struct regcache *reg_cache = arm_insn_r->regcache;
11562
11563   uint32_t shift_imm = 0;
11564   uint32_t reg_src1 = 0, reg_src2 = 0, reg_dest = 0;
11565   uint32_t offset_12 = 0, tgt_mem_addr = 0;
11566   uint32_t record_buf[8], record_buf_mem[8];
11567
11568   LONGEST s_word;
11569   ULONGEST u_regval[2];
11570
11571   arm_insn_r->opcode = bits (arm_insn_r->arm_insn, 21, 24);
11572   arm_insn_r->decode = bits (arm_insn_r->arm_insn, 4, 7);
11573
11574   /* Handle enhanced store insns and LDRD DSP insn,
11575      order begins according to addressing modes for store insns
11576      STRH insn.  */
11577
11578   /* LDR or STR?  */
11579   if (bit (arm_insn_r->arm_insn, INSN_S_L_BIT_NUM))
11580     {
11581       reg_dest = bits (arm_insn_r->arm_insn, 12, 15);
11582       /* LDR insn has a capability to do branching, if
11583          MOV LR, PC is precedded by LDR insn having Rn as R15
11584          in that case, it emulates branch and link insn, and hence we
11585          need to save CSPR and PC as well.  */
11586       if (15 != reg_dest)
11587         {
11588           record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11589           arm_insn_r->reg_rec_count = 1;
11590         }
11591       else
11592         {
11593           record_buf[0] = reg_dest;
11594           record_buf[1] = ARM_PS_REGNUM;
11595           arm_insn_r->reg_rec_count = 2;
11596         }
11597     }
11598   else
11599     {
11600       if (! bits (arm_insn_r->arm_insn, 4, 11))
11601         {
11602           /* Store insn, register offset and register pre-indexed,
11603              register post-indexed.  */
11604           /* Get Rm.  */
11605           reg_src1 = bits (arm_insn_r->arm_insn, 0, 3);
11606           /* Get Rn.  */
11607           reg_src2 = bits (arm_insn_r->arm_insn, 16, 19);
11608           regcache_raw_read_unsigned (reg_cache, reg_src1
11609                                       , &u_regval[0]);
11610           regcache_raw_read_unsigned (reg_cache, reg_src2
11611                                       , &u_regval[1]);
11612           if (15 == reg_src2)
11613             {
11614               /* If R15 was used as Rn, hence current PC+8.  */
11615               /* Pre-indexed mode doesnt reach here ; illegal insn.  */
11616                 u_regval[0] = u_regval[0] + 8;
11617             }
11618           /* Calculate target store address, Rn +/- Rm, register offset.  */
11619           /* U == 1.  */
11620           if (bit (arm_insn_r->arm_insn, 23))
11621             {
11622               tgt_mem_addr = u_regval[0] + u_regval[1];
11623             }
11624           else
11625             {
11626               tgt_mem_addr = u_regval[1] - u_regval[0];
11627             }
11628
11629           switch (arm_insn_r->opcode)
11630             {
11631               /* STR.  */
11632               case 8:
11633               case 12:
11634               /* STR.  */    
11635               case 9:
11636               case 13:
11637               /* STRT.  */
11638               case 1:
11639               case 5:
11640               /* STR.  */
11641               case 0:
11642               case 4:
11643                 record_buf_mem[0] = 4;
11644               break;
11645
11646               /* STRB.  */
11647               case 10:
11648               case 14:
11649               /* STRB.  */
11650               case 11:
11651               case 15:
11652               /* STRBT.  */    
11653               case 3:
11654               case 7:
11655               /* STRB.  */
11656               case 2:
11657               case 6:
11658                 record_buf_mem[0] = 1;
11659               break;
11660
11661               default:
11662                 gdb_assert_not_reached ("no decoding pattern found");
11663               break;
11664             }
11665           record_buf_mem[1] = tgt_mem_addr;
11666           arm_insn_r->mem_rec_count = 1;
11667
11668           if (9 == arm_insn_r->opcode || 11 == arm_insn_r->opcode
11669               || 13 == arm_insn_r->opcode || 15 == arm_insn_r->opcode
11670               || 0 == arm_insn_r->opcode || 2 == arm_insn_r->opcode
11671               || 4 == arm_insn_r->opcode || 6 == arm_insn_r->opcode
11672               || 1 == arm_insn_r->opcode || 3 == arm_insn_r->opcode
11673               || 5 == arm_insn_r->opcode || 7 == arm_insn_r->opcode
11674              )
11675             {
11676               /* Rn is going to be changed in pre-indexed mode and
11677                  post-indexed mode as well.  */
11678               record_buf[0] = reg_src2;
11679               arm_insn_r->reg_rec_count = 1;
11680             }
11681         }
11682       else
11683         {
11684           /* Store insn, scaled register offset; scaled pre-indexed.  */
11685           offset_12 = bits (arm_insn_r->arm_insn, 5, 6);
11686           /* Get Rm.  */
11687           reg_src1 = bits (arm_insn_r->arm_insn, 0, 3);
11688           /* Get Rn.  */
11689           reg_src2 = bits (arm_insn_r->arm_insn, 16, 19);
11690           /* Get shift_imm.  */
11691           shift_imm = bits (arm_insn_r->arm_insn, 7, 11);
11692           regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval[0]);
11693           regcache_raw_read_signed (reg_cache, reg_src1, &s_word);
11694           regcache_raw_read_unsigned (reg_cache, reg_src2, &u_regval[1]);
11695           /* Offset_12 used as shift.  */
11696           switch (offset_12)
11697             {
11698               case 0:
11699                 /* Offset_12 used as index.  */
11700                 offset_12 = u_regval[0] << shift_imm;
11701               break;
11702
11703               case 1:
11704                 offset_12 = (!shift_imm)?0:u_regval[0] >> shift_imm;
11705               break;
11706
11707               case 2:
11708                 if (!shift_imm)
11709                   {
11710                     if (bit (u_regval[0], 31))
11711                       {
11712                         offset_12 = 0xFFFFFFFF;
11713                       }
11714                     else
11715                       {
11716                         offset_12 = 0;
11717                       }
11718                   }
11719                 else
11720                   {
11721                     /* This is arithmetic shift.  */
11722                     offset_12 = s_word >> shift_imm;
11723                   }
11724                 break;
11725
11726               case 3:
11727                 if (!shift_imm)
11728                   {
11729                     regcache_raw_read_unsigned (reg_cache, ARM_PS_REGNUM,
11730                                                 &u_regval[1]);
11731                     /* Get C flag value and shift it by 31.  */
11732                     offset_12 = (((bit (u_regval[1], 29)) << 31) \
11733                                   | (u_regval[0]) >> 1);
11734                   }
11735                 else
11736                   {
11737                     offset_12 = (u_regval[0] >> shift_imm) \
11738                                 | (u_regval[0] <<
11739                                 (sizeof(uint32_t) - shift_imm));
11740                   }
11741               break;
11742
11743               default:
11744                 gdb_assert_not_reached ("no decoding pattern found");
11745               break;
11746             }
11747
11748           regcache_raw_read_unsigned (reg_cache, reg_src2, &u_regval[1]);
11749           /* bit U set.  */
11750           if (bit (arm_insn_r->arm_insn, 23))
11751             {
11752               tgt_mem_addr = u_regval[1] + offset_12;
11753             }
11754           else
11755             {
11756               tgt_mem_addr = u_regval[1] - offset_12;
11757             }
11758
11759           switch (arm_insn_r->opcode)
11760             {
11761               /* STR.  */
11762               case 8:
11763               case 12:
11764               /* STR.  */    
11765               case 9:
11766               case 13:
11767               /* STRT.  */
11768               case 1:
11769               case 5:
11770               /* STR.  */
11771               case 0:
11772               case 4:
11773                 record_buf_mem[0] = 4;
11774               break;
11775
11776               /* STRB.  */
11777               case 10:
11778               case 14:
11779               /* STRB.  */
11780               case 11:
11781               case 15:
11782               /* STRBT.  */    
11783               case 3:
11784               case 7:
11785               /* STRB.  */
11786               case 2:
11787               case 6:
11788                 record_buf_mem[0] = 1;
11789               break;
11790
11791               default:
11792                 gdb_assert_not_reached ("no decoding pattern found");
11793               break;
11794             }
11795           record_buf_mem[1] = tgt_mem_addr;
11796           arm_insn_r->mem_rec_count = 1;
11797
11798           if (9 == arm_insn_r->opcode || 11 == arm_insn_r->opcode
11799               || 13 == arm_insn_r->opcode || 15 == arm_insn_r->opcode
11800               || 0 == arm_insn_r->opcode || 2 == arm_insn_r->opcode
11801               || 4 == arm_insn_r->opcode || 6 == arm_insn_r->opcode
11802               || 1 == arm_insn_r->opcode || 3 == arm_insn_r->opcode
11803               || 5 == arm_insn_r->opcode || 7 == arm_insn_r->opcode
11804              )
11805             {
11806               /* Rn is going to be changed in register scaled pre-indexed
11807                  mode,and scaled post indexed mode.  */
11808               record_buf[0] = reg_src2;
11809               arm_insn_r->reg_rec_count = 1;
11810             }
11811         }
11812     }
11813
11814   REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
11815   MEM_ALLOC (arm_insn_r->arm_mems, arm_insn_r->mem_rec_count, record_buf_mem);
11816   return 0;
11817 }
11818
11819 /* Handle ARM mode instructions with opcode 100.  */
11820
11821 static int
11822 arm_record_ld_st_multiple (insn_decode_record *arm_insn_r)
11823 {
11824   struct regcache *reg_cache = arm_insn_r->regcache;
11825   uint32_t register_count = 0, register_bits;
11826   uint32_t reg_base, addr_mode;
11827   uint32_t record_buf[24], record_buf_mem[48];
11828   uint32_t wback;
11829   ULONGEST u_regval;
11830
11831   /* Fetch the list of registers.  */
11832   register_bits = bits (arm_insn_r->arm_insn, 0, 15);
11833   arm_insn_r->reg_rec_count = 0;
11834
11835   /* Fetch the base register that contains the address we are loading data
11836      to.  */
11837   reg_base = bits (arm_insn_r->arm_insn, 16, 19);
11838
11839   /* Calculate wback.  */
11840   wback = (bit (arm_insn_r->arm_insn, 21) == 1);
11841
11842   if (bit (arm_insn_r->arm_insn, INSN_S_L_BIT_NUM))
11843     {
11844       /* LDM/LDMIA/LDMFD, LDMDA/LDMFA, LDMDB and LDMIB.  */
11845
11846       /* Find out which registers are going to be loaded from memory.  */
11847       while (register_bits)
11848         {
11849           if (register_bits & 0x00000001)
11850             record_buf[arm_insn_r->reg_rec_count++] = register_count;
11851           register_bits = register_bits >> 1;
11852           register_count++;
11853         }
11854
11855   
11856       /* If wback is true, also save the base register, which is going to be
11857          written to.  */
11858       if (wback)
11859         record_buf[arm_insn_r->reg_rec_count++] = reg_base;
11860
11861       /* Save the CPSR register.  */
11862       record_buf[arm_insn_r->reg_rec_count++] = ARM_PS_REGNUM;
11863     }
11864   else
11865     {
11866       /* STM (STMIA, STMEA), STMDA (STMED), STMDB (STMFD) and STMIB (STMFA).  */
11867
11868       addr_mode = bits (arm_insn_r->arm_insn, 23, 24); 
11869
11870       regcache_raw_read_unsigned (reg_cache, reg_base, &u_regval);
11871
11872       /* Find out how many registers are going to be stored to memory.  */
11873       while (register_bits)
11874         {
11875           if (register_bits & 0x00000001)
11876             register_count++;
11877           register_bits = register_bits >> 1;
11878         }
11879
11880       switch (addr_mode)
11881         {
11882           /* STMDA (STMED): Decrement after.  */
11883           case 0:
11884           record_buf_mem[1] = (uint32_t) u_regval
11885                               - register_count * INT_REGISTER_SIZE + 4;
11886           break;
11887           /* STM (STMIA, STMEA): Increment after.  */
11888           case 1:
11889           record_buf_mem[1] = (uint32_t) u_regval;
11890           break;
11891           /* STMDB (STMFD): Decrement before.  */
11892           case 2:
11893           record_buf_mem[1] = (uint32_t) u_regval
11894                               - register_count * INT_REGISTER_SIZE;
11895           break;
11896           /* STMIB (STMFA): Increment before.  */
11897           case 3:
11898           record_buf_mem[1] = (uint32_t) u_regval + INT_REGISTER_SIZE;
11899           break;
11900           default:
11901             gdb_assert_not_reached ("no decoding pattern found");
11902           break;
11903         }
11904
11905       record_buf_mem[0] = register_count * INT_REGISTER_SIZE;
11906       arm_insn_r->mem_rec_count = 1;
11907
11908       /* If wback is true, also save the base register, which is going to be
11909          written to.  */
11910       if (wback)
11911         record_buf[arm_insn_r->reg_rec_count++] = reg_base;
11912     }
11913
11914   REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
11915   MEM_ALLOC (arm_insn_r->arm_mems, arm_insn_r->mem_rec_count, record_buf_mem);
11916   return 0;
11917 }
11918
11919 /* Handling opcode 101 insns.  */
11920
11921 static int
11922 arm_record_b_bl (insn_decode_record *arm_insn_r)
11923 {
11924   uint32_t record_buf[8];
11925
11926   /* Handle B, BL, BLX(1) insns.  */
11927   /* B simply branches so we do nothing here.  */
11928   /* Note: BLX(1) doesnt fall here but instead it falls into
11929      extension space.  */
11930   if (bit (arm_insn_r->arm_insn, 24))
11931   {
11932     record_buf[0] = ARM_LR_REGNUM;
11933     arm_insn_r->reg_rec_count = 1;
11934   }
11935
11936   REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
11937
11938   return 0;
11939 }
11940
11941 /* Handling opcode 110 insns.  */
11942
11943 static int
11944 arm_record_unsupported_insn (insn_decode_record *arm_insn_r)
11945 {
11946   printf_unfiltered (_("Process record does not support instruction "
11947                     "0x%0x at address %s.\n"),arm_insn_r->arm_insn,
11948                     paddress (arm_insn_r->gdbarch, arm_insn_r->this_addr));
11949
11950   return -1;
11951 }
11952
11953 /* Record handler for vector data transfer instructions.  */
11954
11955 static int
11956 arm_record_vdata_transfer_insn (insn_decode_record *arm_insn_r)
11957 {
11958   uint32_t bits_a, bit_c, bit_l, reg_t, reg_v;
11959   uint32_t record_buf[4];
11960
11961   const int num_regs = gdbarch_num_regs (arm_insn_r->gdbarch);
11962   reg_t = bits (arm_insn_r->arm_insn, 12, 15);
11963   reg_v = bits (arm_insn_r->arm_insn, 21, 23);
11964   bits_a = bits (arm_insn_r->arm_insn, 21, 23);
11965   bit_l = bit (arm_insn_r->arm_insn, 20);
11966   bit_c = bit (arm_insn_r->arm_insn, 8);
11967
11968   /* Handle VMOV instruction.  */
11969   if (bit_l && bit_c)
11970     {
11971       record_buf[0] = reg_t;
11972       arm_insn_r->reg_rec_count = 1;
11973     }
11974   else if (bit_l && !bit_c)
11975     {
11976       /* Handle VMOV instruction.  */
11977       if (bits_a == 0x00)
11978         {
11979           if (bit (arm_insn_r->arm_insn, 20))
11980             record_buf[0] = reg_t;
11981           else
11982             record_buf[0] = num_regs + (bit (arm_insn_r->arm_insn, 7) |
11983                             (reg_v << 1));
11984
11985           arm_insn_r->reg_rec_count = 1;
11986         }
11987       /* Handle VMRS instruction.  */
11988       else if (bits_a == 0x07)
11989         {
11990           if (reg_t == 15)
11991             reg_t = ARM_PS_REGNUM;
11992
11993           record_buf[0] = reg_t;
11994           arm_insn_r->reg_rec_count = 1;
11995         }
11996     }
11997   else if (!bit_l && !bit_c)
11998     {
11999       /* Handle VMOV instruction.  */
12000       if (bits_a == 0x00)
12001         {
12002           if (bit (arm_insn_r->arm_insn, 20))
12003             record_buf[0] = reg_t;
12004           else
12005             record_buf[0] = num_regs + (bit (arm_insn_r->arm_insn, 7) |
12006                             (reg_v << 1));
12007
12008           arm_insn_r->reg_rec_count = 1;
12009         }
12010       /* Handle VMSR instruction.  */
12011       else if (bits_a == 0x07)
12012         {
12013           record_buf[0] = ARM_FPSCR_REGNUM;
12014           arm_insn_r->reg_rec_count = 1;
12015         }
12016     }
12017   else if (!bit_l && bit_c)
12018     {
12019       /* Handle VMOV instruction.  */
12020       if (!(bits_a & 0x04))
12021         {
12022           record_buf[0] = (reg_v | (bit (arm_insn_r->arm_insn, 7) << 4))
12023                           + ARM_D0_REGNUM;
12024           arm_insn_r->reg_rec_count = 1;
12025         }
12026       /* Handle VDUP instruction.  */
12027       else
12028         {
12029           if (bit (arm_insn_r->arm_insn, 21))
12030             {
12031               reg_v = reg_v | (bit (arm_insn_r->arm_insn, 7) << 4);
12032               record_buf[0] = reg_v + ARM_D0_REGNUM;
12033               record_buf[1] = reg_v + ARM_D0_REGNUM + 1;
12034               arm_insn_r->reg_rec_count = 2;
12035             }
12036           else
12037             {
12038               reg_v = reg_v | (bit (arm_insn_r->arm_insn, 7) << 4);
12039               record_buf[0] = reg_v + ARM_D0_REGNUM;
12040               arm_insn_r->reg_rec_count = 1;
12041             }
12042         }
12043     }
12044
12045   REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
12046   return 0;
12047 }
12048
12049 /* Record handler for extension register load/store instructions.  */
12050
12051 static int
12052 arm_record_exreg_ld_st_insn (insn_decode_record *arm_insn_r)
12053 {
12054   uint32_t opcode, single_reg;
12055   uint8_t op_vldm_vstm;
12056   uint32_t record_buf[8], record_buf_mem[128];
12057   ULONGEST u_regval = 0;
12058
12059   struct regcache *reg_cache = arm_insn_r->regcache;
12060   const int num_regs = gdbarch_num_regs (arm_insn_r->gdbarch);
12061
12062   opcode = bits (arm_insn_r->arm_insn, 20, 24);
12063   single_reg = bit (arm_insn_r->arm_insn, 8);
12064   op_vldm_vstm = opcode & 0x1b;
12065
12066   /* Handle VMOV instructions.  */
12067   if ((opcode & 0x1e) == 0x04)
12068     {
12069       if (bit (arm_insn_r->arm_insn, 4))
12070         {
12071           record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
12072           record_buf[1] = bits (arm_insn_r->arm_insn, 16, 19);
12073           arm_insn_r->reg_rec_count = 2;
12074         }
12075       else
12076         {
12077           uint8_t reg_m = (bits (arm_insn_r->arm_insn, 0, 3) << 1)
12078                           | bit (arm_insn_r->arm_insn, 5);
12079
12080           if (!single_reg)
12081             {
12082               record_buf[0] = num_regs + reg_m;
12083               record_buf[1] = num_regs + reg_m + 1;
12084               arm_insn_r->reg_rec_count = 2;
12085             }
12086           else
12087             {
12088               record_buf[0] = reg_m + ARM_D0_REGNUM;
12089               arm_insn_r->reg_rec_count = 1;
12090             }
12091         }
12092     }
12093   /* Handle VSTM and VPUSH instructions.  */
12094   else if (op_vldm_vstm == 0x08 || op_vldm_vstm == 0x0a
12095           || op_vldm_vstm == 0x12)
12096     {
12097       uint32_t start_address, reg_rn, imm_off32, imm_off8, memory_count;
12098       uint32_t memory_index = 0;
12099
12100       reg_rn = bits (arm_insn_r->arm_insn, 16, 19);
12101       regcache_raw_read_unsigned (reg_cache, reg_rn, &u_regval);
12102       imm_off8 = bits (arm_insn_r->arm_insn, 0, 7);
12103       imm_off32 = imm_off8 << 24;
12104       memory_count = imm_off8;
12105
12106       if (bit (arm_insn_r->arm_insn, 23))
12107         start_address = u_regval;
12108       else
12109         start_address = u_regval - imm_off32;
12110
12111       if (bit (arm_insn_r->arm_insn, 21))
12112         {
12113           record_buf[0] = reg_rn;
12114           arm_insn_r->reg_rec_count = 1;
12115         }
12116
12117       while (memory_count > 0)
12118         {
12119           if (!single_reg)
12120             {
12121               record_buf_mem[memory_index] = start_address;
12122               record_buf_mem[memory_index + 1] = 4;
12123               start_address = start_address + 4;
12124               memory_index = memory_index + 2;
12125             }
12126           else
12127             {
12128               record_buf_mem[memory_index] = start_address;
12129               record_buf_mem[memory_index + 1] = 4;
12130               record_buf_mem[memory_index + 2] = start_address + 4;
12131               record_buf_mem[memory_index + 3] = 4;
12132               start_address = start_address + 8;
12133               memory_index = memory_index + 4;
12134             }
12135           memory_count--;
12136         }
12137       arm_insn_r->mem_rec_count = (memory_index >> 1);
12138     }
12139   /* Handle VLDM instructions.  */
12140   else if (op_vldm_vstm == 0x09 || op_vldm_vstm == 0x0b
12141           || op_vldm_vstm == 0x13)
12142     {
12143       uint32_t reg_count, reg_vd;
12144       uint32_t reg_index = 0;
12145
12146       reg_vd = bits (arm_insn_r->arm_insn, 12, 15);
12147       reg_count = bits (arm_insn_r->arm_insn, 0, 7);
12148
12149       if (single_reg)
12150         reg_vd = reg_vd | (bit (arm_insn_r->arm_insn, 22) << 4);
12151       else
12152         reg_vd = (reg_vd << 1) | bit (arm_insn_r->arm_insn, 22);
12153
12154       if (bit (arm_insn_r->arm_insn, 21))
12155         record_buf[reg_index++] = bits (arm_insn_r->arm_insn, 16, 19);
12156
12157       while (reg_count > 0)
12158         {
12159           if (single_reg)
12160               record_buf[reg_index++] = num_regs + reg_vd + reg_count - 1;
12161           else
12162               record_buf[reg_index++] = ARM_D0_REGNUM + reg_vd + reg_count - 1;
12163
12164           reg_count--;
12165         }
12166       arm_insn_r->reg_rec_count = reg_index;
12167     }
12168   /* VSTR Vector store register.  */
12169   else if ((opcode & 0x13) == 0x10)
12170     {
12171       uint32_t start_address, reg_rn, imm_off32, imm_off8, memory_count;
12172       uint32_t memory_index = 0;
12173
12174       reg_rn = bits (arm_insn_r->arm_insn, 16, 19);
12175       regcache_raw_read_unsigned (reg_cache, reg_rn, &u_regval);
12176       imm_off8 = bits (arm_insn_r->arm_insn, 0, 7);
12177       imm_off32 = imm_off8 << 24;
12178       memory_count = imm_off8;
12179
12180       if (bit (arm_insn_r->arm_insn, 23))
12181         start_address = u_regval + imm_off32;
12182       else
12183         start_address = u_regval - imm_off32;
12184
12185       if (single_reg)
12186         {
12187           record_buf_mem[memory_index] = start_address;
12188           record_buf_mem[memory_index + 1] = 4;
12189           arm_insn_r->mem_rec_count = 1;
12190         }
12191       else
12192         {
12193           record_buf_mem[memory_index] = start_address;
12194           record_buf_mem[memory_index + 1] = 4;
12195           record_buf_mem[memory_index + 2] = start_address + 4;
12196           record_buf_mem[memory_index + 3] = 4;
12197           arm_insn_r->mem_rec_count = 2;
12198         }
12199     }
12200   /* VLDR Vector load register.  */
12201   else if ((opcode & 0x13) == 0x11)
12202     {
12203       uint32_t reg_vd = bits (arm_insn_r->arm_insn, 12, 15);
12204
12205       if (!single_reg)
12206         {
12207           reg_vd = reg_vd | (bit (arm_insn_r->arm_insn, 22) << 4);
12208           record_buf[0] = ARM_D0_REGNUM + reg_vd;
12209         }
12210       else
12211         {
12212           reg_vd = (reg_vd << 1) | bit (arm_insn_r->arm_insn, 22);
12213           record_buf[0] = num_regs + reg_vd;
12214         }
12215       arm_insn_r->reg_rec_count = 1;
12216     }
12217
12218   REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
12219   MEM_ALLOC (arm_insn_r->arm_mems, arm_insn_r->mem_rec_count, record_buf_mem);
12220   return 0;
12221 }
12222
12223 /* Record handler for arm/thumb mode VFP data processing instructions.  */
12224
12225 static int
12226 arm_record_vfp_data_proc_insn (insn_decode_record *arm_insn_r)
12227 {
12228   uint32_t opc1, opc2, opc3, dp_op_sz, bit_d, reg_vd;
12229   uint32_t record_buf[4];
12230   enum insn_types {INSN_T0, INSN_T1, INSN_T2, INSN_T3, INSN_INV};
12231   enum insn_types curr_insn_type = INSN_INV;
12232
12233   reg_vd = bits (arm_insn_r->arm_insn, 12, 15);
12234   opc1 = bits (arm_insn_r->arm_insn, 20, 23);
12235   opc2 = bits (arm_insn_r->arm_insn, 16, 19);
12236   opc3 = bits (arm_insn_r->arm_insn, 6, 7);
12237   dp_op_sz = bit (arm_insn_r->arm_insn, 8);
12238   bit_d = bit (arm_insn_r->arm_insn, 22);
12239   opc1 = opc1 & 0x04;
12240
12241   /* Handle VMLA, VMLS.  */
12242   if (opc1 == 0x00)
12243     {
12244       if (bit (arm_insn_r->arm_insn, 10))
12245         {
12246           if (bit (arm_insn_r->arm_insn, 6))
12247             curr_insn_type = INSN_T0;
12248           else
12249             curr_insn_type = INSN_T1;
12250         }
12251       else
12252         {
12253           if (dp_op_sz)
12254             curr_insn_type = INSN_T1;
12255           else
12256             curr_insn_type = INSN_T2;
12257         }
12258     }
12259   /* Handle VNMLA, VNMLS, VNMUL.  */
12260   else if (opc1 == 0x01)
12261     {
12262       if (dp_op_sz)
12263         curr_insn_type = INSN_T1;
12264       else
12265         curr_insn_type = INSN_T2;
12266     }
12267   /* Handle VMUL.  */
12268   else if (opc1 == 0x02 && !(opc3 & 0x01))
12269     {
12270       if (bit (arm_insn_r->arm_insn, 10))
12271         {
12272           if (bit (arm_insn_r->arm_insn, 6))
12273             curr_insn_type = INSN_T0;
12274           else
12275             curr_insn_type = INSN_T1;
12276         }
12277       else
12278         {
12279           if (dp_op_sz)
12280             curr_insn_type = INSN_T1;
12281           else
12282             curr_insn_type = INSN_T2;
12283         }
12284     }
12285   /* Handle VADD, VSUB.  */
12286   else if (opc1 == 0x03)
12287     {
12288       if (!bit (arm_insn_r->arm_insn, 9))
12289         {
12290           if (bit (arm_insn_r->arm_insn, 6))
12291             curr_insn_type = INSN_T0;
12292           else
12293             curr_insn_type = INSN_T1;
12294         }
12295       else
12296         {
12297           if (dp_op_sz)
12298             curr_insn_type = INSN_T1;
12299           else
12300             curr_insn_type = INSN_T2;
12301         }
12302     }
12303   /* Handle VDIV.  */
12304   else if (opc1 == 0x0b)
12305     {
12306       if (dp_op_sz)
12307         curr_insn_type = INSN_T1;
12308       else
12309         curr_insn_type = INSN_T2;
12310     }
12311   /* Handle all other vfp data processing instructions.  */
12312   else if (opc1 == 0x0b)
12313     {
12314       /* Handle VMOV.  */
12315       if (!(opc3 & 0x01) || (opc2 == 0x00 && opc3 == 0x01))
12316         {
12317           if (bit (arm_insn_r->arm_insn, 4))
12318             {
12319               if (bit (arm_insn_r->arm_insn, 6))
12320                 curr_insn_type = INSN_T0;
12321               else
12322                 curr_insn_type = INSN_T1;
12323             }
12324           else
12325             {
12326               if (dp_op_sz)
12327                 curr_insn_type = INSN_T1;
12328               else
12329                 curr_insn_type = INSN_T2;
12330             }
12331         }
12332       /* Handle VNEG and VABS.  */
12333       else if ((opc2 == 0x01 && opc3 == 0x01)
12334               || (opc2 == 0x00 && opc3 == 0x03))
12335         {
12336           if (!bit (arm_insn_r->arm_insn, 11))
12337             {
12338               if (bit (arm_insn_r->arm_insn, 6))
12339                 curr_insn_type = INSN_T0;
12340               else
12341                 curr_insn_type = INSN_T1;
12342             }
12343           else
12344             {
12345               if (dp_op_sz)
12346                 curr_insn_type = INSN_T1;
12347               else
12348                 curr_insn_type = INSN_T2;
12349             }
12350         }
12351       /* Handle VSQRT.  */
12352       else if (opc2 == 0x01 && opc3 == 0x03)
12353         {
12354           if (dp_op_sz)
12355             curr_insn_type = INSN_T1;
12356           else
12357             curr_insn_type = INSN_T2;
12358         }
12359       /* Handle VCVT.  */
12360       else if (opc2 == 0x07 && opc3 == 0x03)
12361         {
12362           if (!dp_op_sz)
12363             curr_insn_type = INSN_T1;
12364           else
12365             curr_insn_type = INSN_T2;
12366         }
12367       else if (opc3 & 0x01)
12368         {
12369           /* Handle VCVT.  */
12370           if ((opc2 == 0x08) || (opc2 & 0x0e) == 0x0c)
12371             {
12372               if (!bit (arm_insn_r->arm_insn, 18))
12373                 curr_insn_type = INSN_T2;
12374               else
12375                 {
12376                   if (dp_op_sz)
12377                     curr_insn_type = INSN_T1;
12378                   else
12379                     curr_insn_type = INSN_T2;
12380                 }
12381             }
12382           /* Handle VCVT.  */
12383           else if ((opc2 & 0x0e) == 0x0a || (opc2 & 0x0e) == 0x0e)
12384             {
12385               if (dp_op_sz)
12386                 curr_insn_type = INSN_T1;
12387               else
12388                 curr_insn_type = INSN_T2;
12389             }
12390           /* Handle VCVTB, VCVTT.  */
12391           else if ((opc2 & 0x0e) == 0x02)
12392             curr_insn_type = INSN_T2;
12393           /* Handle VCMP, VCMPE.  */
12394           else if ((opc2 & 0x0e) == 0x04)
12395             curr_insn_type = INSN_T3;
12396         }
12397     }
12398
12399   switch (curr_insn_type)
12400     {
12401       case INSN_T0:
12402         reg_vd = reg_vd | (bit_d << 4);
12403         record_buf[0] = reg_vd + ARM_D0_REGNUM;
12404         record_buf[1] = reg_vd + ARM_D0_REGNUM + 1;
12405         arm_insn_r->reg_rec_count = 2;
12406         break;
12407
12408       case INSN_T1:
12409         reg_vd = reg_vd | (bit_d << 4);
12410         record_buf[0] = reg_vd + ARM_D0_REGNUM;
12411         arm_insn_r->reg_rec_count = 1;
12412         break;
12413
12414       case INSN_T2:
12415         reg_vd = (reg_vd << 1) | bit_d;
12416         record_buf[0] = reg_vd + ARM_D0_REGNUM;
12417         arm_insn_r->reg_rec_count = 1;
12418         break;
12419
12420       case INSN_T3:
12421         record_buf[0] = ARM_FPSCR_REGNUM;
12422         arm_insn_r->reg_rec_count = 1;
12423         break;
12424
12425       default:
12426         gdb_assert_not_reached ("no decoding pattern found");
12427         break;
12428     }
12429
12430   REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
12431   return 0;
12432 }
12433
12434 /* Handling opcode 110 insns.  */
12435
12436 static int
12437 arm_record_asimd_vfp_coproc (insn_decode_record *arm_insn_r)
12438 {
12439   uint32_t op, op1, op1_sbit, op1_ebit, coproc;
12440
12441   coproc = bits (arm_insn_r->arm_insn, 8, 11);
12442   op1 = bits (arm_insn_r->arm_insn, 20, 25);
12443   op1_ebit = bit (arm_insn_r->arm_insn, 20);
12444
12445   if ((coproc & 0x0e) == 0x0a)
12446     {
12447       /* Handle extension register ld/st instructions.  */
12448       if (!(op1 & 0x20))
12449         return arm_record_exreg_ld_st_insn (arm_insn_r);
12450
12451       /* 64-bit transfers between arm core and extension registers.  */
12452       if ((op1 & 0x3e) == 0x04)
12453         return arm_record_exreg_ld_st_insn (arm_insn_r);
12454     }
12455   else
12456     {
12457       /* Handle coprocessor ld/st instructions.  */
12458       if (!(op1 & 0x3a))
12459         {
12460           /* Store.  */
12461           if (!op1_ebit)
12462             return arm_record_unsupported_insn (arm_insn_r);
12463           else
12464             /* Load.  */
12465             return arm_record_unsupported_insn (arm_insn_r);
12466         }
12467
12468       /* Move to coprocessor from two arm core registers.  */
12469       if (op1 == 0x4)
12470         return arm_record_unsupported_insn (arm_insn_r);
12471
12472       /* Move to two arm core registers from coprocessor.  */
12473       if (op1 == 0x5)
12474         {
12475           uint32_t reg_t[2];
12476
12477           reg_t[0] = bits (arm_insn_r->arm_insn, 12, 15);
12478           reg_t[1] = bits (arm_insn_r->arm_insn, 16, 19);
12479           arm_insn_r->reg_rec_count = 2;
12480
12481           REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, reg_t);
12482           return 0;
12483        }
12484     }
12485   return arm_record_unsupported_insn (arm_insn_r);
12486 }
12487
12488 /* Handling opcode 111 insns.  */
12489
12490 static int
12491 arm_record_coproc_data_proc (insn_decode_record *arm_insn_r)
12492 {
12493   uint32_t op, op1_sbit, op1_ebit, coproc;
12494   struct gdbarch_tdep *tdep = gdbarch_tdep (arm_insn_r->gdbarch);
12495   struct regcache *reg_cache = arm_insn_r->regcache;
12496   ULONGEST u_regval = 0;
12497
12498   arm_insn_r->opcode = bits (arm_insn_r->arm_insn, 24, 27);
12499   coproc = bits (arm_insn_r->arm_insn, 8, 11);
12500   op1_sbit = bit (arm_insn_r->arm_insn, 24);
12501   op1_ebit = bit (arm_insn_r->arm_insn, 20);
12502   op = bit (arm_insn_r->arm_insn, 4);
12503
12504   /* Handle arm SWI/SVC system call instructions.  */
12505   if (op1_sbit)
12506     {
12507       if (tdep->arm_syscall_record != NULL)
12508         {
12509           ULONGEST svc_operand, svc_number;
12510
12511           svc_operand = (0x00ffffff & arm_insn_r->arm_insn);
12512
12513           if (svc_operand)  /* OABI.  */
12514             svc_number = svc_operand - 0x900000;
12515           else /* EABI.  */
12516             regcache_raw_read_unsigned (reg_cache, 7, &svc_number);
12517
12518           return tdep->arm_syscall_record (reg_cache, svc_number);
12519         }
12520       else
12521         {
12522           printf_unfiltered (_("no syscall record support\n"));
12523           return -1;
12524         }
12525     }
12526
12527   if ((coproc & 0x0e) == 0x0a)
12528     {
12529       /* VFP data-processing instructions.  */
12530       if (!op1_sbit && !op)
12531         return arm_record_vfp_data_proc_insn (arm_insn_r);
12532
12533       /* Advanced SIMD, VFP instructions.  */
12534       if (!op1_sbit && op)
12535         return arm_record_vdata_transfer_insn (arm_insn_r);
12536     }
12537   else
12538     {
12539       /* Coprocessor data operations.  */
12540       if (!op1_sbit && !op)
12541         return arm_record_unsupported_insn (arm_insn_r);
12542
12543       /* Move to Coprocessor from ARM core register.  */
12544       if (!op1_sbit && !op1_ebit && op)
12545         return arm_record_unsupported_insn (arm_insn_r);
12546
12547       /* Move to arm core register from coprocessor.  */
12548       if (!op1_sbit && op1_ebit && op)
12549         {
12550           uint32_t record_buf[1];
12551
12552           record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
12553           if (record_buf[0] == 15)
12554             record_buf[0] = ARM_PS_REGNUM;
12555
12556           arm_insn_r->reg_rec_count = 1;
12557           REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count,
12558                      record_buf);
12559           return 0;
12560         }
12561     }
12562
12563   return arm_record_unsupported_insn (arm_insn_r);
12564 }
12565
12566 /* Handling opcode 000 insns.  */
12567
12568 static int
12569 thumb_record_shift_add_sub (insn_decode_record *thumb_insn_r)
12570 {
12571   uint32_t record_buf[8];
12572   uint32_t reg_src1 = 0;
12573
12574   reg_src1 = bits (thumb_insn_r->arm_insn, 0, 2);
12575
12576   record_buf[0] = ARM_PS_REGNUM;
12577   record_buf[1] = reg_src1;
12578   thumb_insn_r->reg_rec_count = 2;
12579
12580   REG_ALLOC (thumb_insn_r->arm_regs, thumb_insn_r->reg_rec_count, record_buf);
12581
12582   return 0;
12583 }
12584
12585
12586 /* Handling opcode 001 insns.  */
12587
12588 static int
12589 thumb_record_add_sub_cmp_mov (insn_decode_record *thumb_insn_r)
12590 {
12591   uint32_t record_buf[8];
12592   uint32_t reg_src1 = 0;
12593
12594   reg_src1 = bits (thumb_insn_r->arm_insn, 8, 10);
12595
12596   record_buf[0] = ARM_PS_REGNUM;
12597   record_buf[1] = reg_src1;
12598   thumb_insn_r->reg_rec_count = 2;
12599
12600   REG_ALLOC (thumb_insn_r->arm_regs, thumb_insn_r->reg_rec_count, record_buf);
12601
12602   return 0;
12603 }
12604
12605 /* Handling opcode 010 insns.  */
12606
12607 static int
12608 thumb_record_ld_st_reg_offset (insn_decode_record *thumb_insn_r)
12609 {
12610   struct regcache *reg_cache =  thumb_insn_r->regcache;
12611   uint32_t record_buf[8], record_buf_mem[8];
12612
12613   uint32_t reg_src1 = 0, reg_src2 = 0;
12614   uint32_t opcode1 = 0, opcode2 = 0, opcode3 = 0;
12615
12616   ULONGEST u_regval[2] = {0};
12617
12618   opcode1 = bits (thumb_insn_r->arm_insn, 10, 12);
12619
12620   if (bit (thumb_insn_r->arm_insn, 12))
12621     {
12622       /* Handle load/store register offset.  */
12623       opcode2 = bits (thumb_insn_r->arm_insn, 9, 10);
12624       if (opcode2 >= 12 && opcode2 <= 15)
12625         {
12626           /* LDR(2), LDRB(2) , LDRH(2), LDRSB, LDRSH.  */
12627           reg_src1 = bits (thumb_insn_r->arm_insn,0, 2);
12628           record_buf[0] = reg_src1;
12629           thumb_insn_r->reg_rec_count = 1;
12630         }
12631       else if (opcode2 >= 8 && opcode2 <= 10)
12632         {
12633           /* STR(2), STRB(2), STRH(2) .  */
12634           reg_src1 = bits (thumb_insn_r->arm_insn, 3, 5);
12635           reg_src2 = bits (thumb_insn_r->arm_insn, 6, 8);
12636           regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval[0]);
12637           regcache_raw_read_unsigned (reg_cache, reg_src2, &u_regval[1]);
12638           if (8 == opcode2)
12639             record_buf_mem[0] = 4;    /* STR (2).  */
12640           else if (10 == opcode2)
12641             record_buf_mem[0] = 1;    /*  STRB (2).  */
12642           else if (9 == opcode2)
12643             record_buf_mem[0] = 2;    /* STRH (2).  */
12644           record_buf_mem[1] = u_regval[0] + u_regval[1];
12645           thumb_insn_r->mem_rec_count = 1;
12646         }
12647     }
12648   else if (bit (thumb_insn_r->arm_insn, 11))
12649     {
12650       /* Handle load from literal pool.  */
12651       /* LDR(3).  */
12652       reg_src1 = bits (thumb_insn_r->arm_insn, 8, 10);
12653       record_buf[0] = reg_src1;
12654       thumb_insn_r->reg_rec_count = 1;
12655     }
12656   else if (opcode1)
12657     {
12658       opcode2 = bits (thumb_insn_r->arm_insn, 8, 9);
12659       opcode3 = bits (thumb_insn_r->arm_insn, 0, 2);
12660       if ((3 == opcode2) && (!opcode3))
12661         {
12662           /* Branch with exchange.  */
12663           record_buf[0] = ARM_PS_REGNUM;
12664           thumb_insn_r->reg_rec_count = 1;
12665         }
12666       else
12667         {
12668           /* Format 8; special data processing insns.  */
12669           reg_src1 = bits (thumb_insn_r->arm_insn, 0, 2);
12670           record_buf[0] = ARM_PS_REGNUM;
12671           record_buf[1] = reg_src1;
12672           thumb_insn_r->reg_rec_count = 2;
12673         }
12674     }
12675   else
12676     {
12677       /* Format 5; data processing insns.  */
12678       reg_src1 = bits (thumb_insn_r->arm_insn, 0, 2);
12679       if (bit (thumb_insn_r->arm_insn, 7))
12680         {
12681           reg_src1 = reg_src1 + 8;
12682         }
12683       record_buf[0] = ARM_PS_REGNUM;
12684       record_buf[1] = reg_src1;
12685       thumb_insn_r->reg_rec_count = 2;
12686     }
12687
12688   REG_ALLOC (thumb_insn_r->arm_regs, thumb_insn_r->reg_rec_count, record_buf);
12689   MEM_ALLOC (thumb_insn_r->arm_mems, thumb_insn_r->mem_rec_count,
12690              record_buf_mem);
12691
12692   return 0;
12693 }
12694
12695 /* Handling opcode 001 insns.  */
12696
12697 static int
12698 thumb_record_ld_st_imm_offset (insn_decode_record *thumb_insn_r)
12699 {
12700   struct regcache *reg_cache = thumb_insn_r->regcache;
12701   uint32_t record_buf[8], record_buf_mem[8];
12702
12703   uint32_t reg_src1 = 0;
12704   uint32_t opcode = 0, immed_5 = 0;
12705
12706   ULONGEST u_regval = 0;
12707
12708   opcode = bits (thumb_insn_r->arm_insn, 11, 12);
12709
12710   if (opcode)
12711     {
12712       /* LDR(1).  */
12713       reg_src1 = bits (thumb_insn_r->arm_insn, 0, 2);
12714       record_buf[0] = reg_src1;
12715       thumb_insn_r->reg_rec_count = 1;
12716     }
12717   else
12718     {
12719       /* STR(1).  */
12720       reg_src1 = bits (thumb_insn_r->arm_insn, 3, 5);
12721       immed_5 = bits (thumb_insn_r->arm_insn, 6, 10);
12722       regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval);
12723       record_buf_mem[0] = 4;
12724       record_buf_mem[1] = u_regval + (immed_5 * 4);
12725       thumb_insn_r->mem_rec_count = 1;
12726     }
12727
12728   REG_ALLOC (thumb_insn_r->arm_regs, thumb_insn_r->reg_rec_count, record_buf);
12729   MEM_ALLOC (thumb_insn_r->arm_mems, thumb_insn_r->mem_rec_count, 
12730              record_buf_mem);
12731
12732   return 0;
12733 }
12734
12735 /* Handling opcode 100 insns.  */
12736
12737 static int
12738 thumb_record_ld_st_stack (insn_decode_record *thumb_insn_r)
12739 {
12740   struct regcache *reg_cache = thumb_insn_r->regcache;
12741   uint32_t record_buf[8], record_buf_mem[8];
12742
12743   uint32_t reg_src1 = 0;
12744   uint32_t opcode = 0, immed_8 = 0, immed_5 = 0;
12745
12746   ULONGEST u_regval = 0;
12747
12748   opcode = bits (thumb_insn_r->arm_insn, 11, 12);
12749
12750   if (3 == opcode)
12751     {
12752       /* LDR(4).  */
12753       reg_src1 = bits (thumb_insn_r->arm_insn, 8, 10);
12754       record_buf[0] = reg_src1;
12755       thumb_insn_r->reg_rec_count = 1;
12756     }
12757   else if (1 == opcode)
12758     {
12759       /* LDRH(1).  */
12760       reg_src1 = bits (thumb_insn_r->arm_insn, 0, 2);
12761       record_buf[0] = reg_src1;
12762       thumb_insn_r->reg_rec_count = 1;
12763     }
12764   else if (2 == opcode)
12765     {
12766       /* STR(3).  */
12767       immed_8 = bits (thumb_insn_r->arm_insn, 0, 7);
12768       regcache_raw_read_unsigned (reg_cache, ARM_SP_REGNUM, &u_regval);
12769       record_buf_mem[0] = 4;
12770       record_buf_mem[1] = u_regval + (immed_8 * 4);
12771       thumb_insn_r->mem_rec_count = 1;
12772     }
12773   else if (0 == opcode)
12774     {
12775       /* STRH(1).  */
12776       immed_5 = bits (thumb_insn_r->arm_insn, 6, 10);
12777       reg_src1 = bits (thumb_insn_r->arm_insn, 3, 5);
12778       regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval);
12779       record_buf_mem[0] = 2;
12780       record_buf_mem[1] = u_regval + (immed_5 * 2);
12781       thumb_insn_r->mem_rec_count = 1;
12782     }
12783
12784   REG_ALLOC (thumb_insn_r->arm_regs, thumb_insn_r->reg_rec_count, record_buf);
12785   MEM_ALLOC (thumb_insn_r->arm_mems, thumb_insn_r->mem_rec_count,
12786              record_buf_mem);
12787
12788   return 0;
12789 }
12790
12791 /* Handling opcode 101 insns.  */
12792
12793 static int
12794 thumb_record_misc (insn_decode_record *thumb_insn_r)
12795 {
12796   struct regcache *reg_cache = thumb_insn_r->regcache;
12797
12798   uint32_t opcode = 0, opcode1 = 0, opcode2 = 0;
12799   uint32_t register_bits = 0, register_count = 0;
12800   uint32_t register_list[8] = {0}, index = 0, start_address = 0;
12801   uint32_t record_buf[24], record_buf_mem[48];
12802   uint32_t reg_src1;
12803
12804   ULONGEST u_regval = 0;
12805
12806   opcode = bits (thumb_insn_r->arm_insn, 11, 12);
12807   opcode1 = bits (thumb_insn_r->arm_insn, 8, 12);
12808   opcode2 = bits (thumb_insn_r->arm_insn, 9, 12);
12809
12810   if (14 == opcode2)
12811     {
12812       /* POP.  */
12813       register_bits = bits (thumb_insn_r->arm_insn, 0, 7);
12814       while (register_bits)
12815       {
12816         if (register_bits & 0x00000001)
12817           record_buf[index++] = register_count;
12818         register_bits = register_bits >> 1;
12819         register_count++;
12820       }
12821       record_buf[index++] = ARM_PS_REGNUM;
12822       record_buf[index++] = ARM_SP_REGNUM;
12823       thumb_insn_r->reg_rec_count = index;
12824     }
12825   else if (10 == opcode2)
12826     {
12827       /* PUSH.  */
12828       register_bits = bits (thumb_insn_r->arm_insn, 0, 7);
12829       regcache_raw_read_unsigned (reg_cache, ARM_SP_REGNUM, &u_regval);
12830       while (register_bits)
12831         {
12832           if (register_bits & 0x00000001)
12833             register_count++;
12834           register_bits = register_bits >> 1;
12835         }
12836       start_address = u_regval -  \
12837                   (4 * (bit (thumb_insn_r->arm_insn, 8) + register_count));
12838       thumb_insn_r->mem_rec_count = register_count;
12839       while (register_count)
12840         {
12841           record_buf_mem[(register_count * 2) - 1] = start_address;
12842           record_buf_mem[(register_count * 2) - 2] = 4;
12843           start_address = start_address + 4;
12844           register_count--;
12845         }
12846       record_buf[0] = ARM_SP_REGNUM;
12847       thumb_insn_r->reg_rec_count = 1;
12848     }
12849   else if (0x1E == opcode1)
12850     {
12851       /* BKPT insn.  */
12852       /* Handle enhanced software breakpoint insn, BKPT.  */
12853       /* CPSR is changed to be executed in ARM state,  disabling normal
12854          interrupts, entering abort mode.  */
12855       /* According to high vector configuration PC is set.  */
12856       /* User hits breakpoint and type reverse, in that case, we need to go back with 
12857       previous CPSR and Program Counter.  */
12858       record_buf[0] = ARM_PS_REGNUM;
12859       record_buf[1] = ARM_LR_REGNUM;
12860       thumb_insn_r->reg_rec_count = 2;
12861       /* We need to save SPSR value, which is not yet done.  */
12862       printf_unfiltered (_("Process record does not support instruction "
12863                            "0x%0x at address %s.\n"),
12864                            thumb_insn_r->arm_insn,
12865                            paddress (thumb_insn_r->gdbarch,
12866                            thumb_insn_r->this_addr));
12867       return -1;
12868     }
12869   else if ((0 == opcode) || (1 == opcode))
12870     {
12871       /* ADD(5), ADD(6).  */
12872       reg_src1 = bits (thumb_insn_r->arm_insn, 8, 10);
12873       record_buf[0] = reg_src1;
12874       thumb_insn_r->reg_rec_count = 1;
12875     }
12876   else if (2 == opcode)
12877     {
12878       /* ADD(7), SUB(4).  */
12879       reg_src1 = bits (thumb_insn_r->arm_insn, 8, 10);
12880       record_buf[0] = ARM_SP_REGNUM;
12881       thumb_insn_r->reg_rec_count = 1;
12882     }
12883
12884   REG_ALLOC (thumb_insn_r->arm_regs, thumb_insn_r->reg_rec_count, record_buf);
12885   MEM_ALLOC (thumb_insn_r->arm_mems, thumb_insn_r->mem_rec_count,
12886              record_buf_mem);
12887
12888   return 0;
12889 }
12890
12891 /* Handling opcode 110 insns.  */
12892
12893 static int
12894 thumb_record_ldm_stm_swi (insn_decode_record *thumb_insn_r)                
12895 {
12896   struct gdbarch_tdep *tdep = gdbarch_tdep (thumb_insn_r->gdbarch);
12897   struct regcache *reg_cache = thumb_insn_r->regcache;
12898
12899   uint32_t ret = 0; /* function return value: -1:record failure ;  0:success  */
12900   uint32_t reg_src1 = 0;
12901   uint32_t opcode1 = 0, opcode2 = 0, register_bits = 0, register_count = 0;
12902   uint32_t register_list[8] = {0}, index = 0, start_address = 0;
12903   uint32_t record_buf[24], record_buf_mem[48];
12904
12905   ULONGEST u_regval = 0;
12906
12907   opcode1 = bits (thumb_insn_r->arm_insn, 8, 12);
12908   opcode2 = bits (thumb_insn_r->arm_insn, 11, 12);
12909
12910   if (1 == opcode2)
12911     {
12912
12913       /* LDMIA.  */
12914       register_bits = bits (thumb_insn_r->arm_insn, 0, 7);
12915       /* Get Rn.  */
12916       reg_src1 = bits (thumb_insn_r->arm_insn, 8, 10);
12917       while (register_bits)
12918         {
12919           if (register_bits & 0x00000001)
12920             record_buf[index++] = register_count;
12921           register_bits = register_bits >> 1;
12922           register_count++;
12923         }
12924       record_buf[index++] = reg_src1;
12925       thumb_insn_r->reg_rec_count = index;
12926     }
12927   else if (0 == opcode2)
12928     {
12929       /* It handles both STMIA.  */
12930       register_bits = bits (thumb_insn_r->arm_insn, 0, 7);
12931       /* Get Rn.  */
12932       reg_src1 = bits (thumb_insn_r->arm_insn, 8, 10);
12933       regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval);
12934       while (register_bits)
12935         {
12936           if (register_bits & 0x00000001)
12937             register_count++;
12938           register_bits = register_bits >> 1;
12939         }
12940       start_address = u_regval;
12941       thumb_insn_r->mem_rec_count = register_count;
12942       while (register_count)
12943         {
12944           record_buf_mem[(register_count * 2) - 1] = start_address;
12945           record_buf_mem[(register_count * 2) - 2] = 4;
12946           start_address = start_address + 4;
12947           register_count--;
12948         }
12949     }
12950   else if (0x1F == opcode1)
12951     {
12952         /* Handle arm syscall insn.  */
12953         if (tdep->arm_syscall_record != NULL)
12954           {
12955             regcache_raw_read_unsigned (reg_cache, 7, &u_regval);
12956             ret = tdep->arm_syscall_record (reg_cache, u_regval);
12957           }
12958         else
12959           {
12960             printf_unfiltered (_("no syscall record support\n"));
12961             return -1;
12962           }
12963     }
12964
12965   /* B (1), conditional branch is automatically taken care in process_record,
12966     as PC is saved there.  */
12967
12968   REG_ALLOC (thumb_insn_r->arm_regs, thumb_insn_r->reg_rec_count, record_buf);
12969   MEM_ALLOC (thumb_insn_r->arm_mems, thumb_insn_r->mem_rec_count,
12970              record_buf_mem);
12971
12972   return ret;
12973 }
12974
12975 /* Handling opcode 111 insns.  */
12976
12977 static int
12978 thumb_record_branch (insn_decode_record *thumb_insn_r)
12979 {
12980   uint32_t record_buf[8];
12981   uint32_t bits_h = 0;
12982
12983   bits_h = bits (thumb_insn_r->arm_insn, 11, 12);
12984
12985   if (2 == bits_h || 3 == bits_h)
12986     {
12987       /* BL */
12988       record_buf[0] = ARM_LR_REGNUM;
12989       thumb_insn_r->reg_rec_count = 1;
12990     }
12991   else if (1 == bits_h)
12992     {
12993       /* BLX(1). */
12994       record_buf[0] = ARM_PS_REGNUM;
12995       record_buf[1] = ARM_LR_REGNUM;
12996       thumb_insn_r->reg_rec_count = 2;
12997     }
12998
12999   /* B(2) is automatically taken care in process_record, as PC is 
13000      saved there.  */
13001
13002   REG_ALLOC (thumb_insn_r->arm_regs, thumb_insn_r->reg_rec_count, record_buf);
13003
13004   return 0;     
13005 }
13006
13007 /* Handler for thumb2 load/store multiple instructions.  */
13008
13009 static int
13010 thumb2_record_ld_st_multiple (insn_decode_record *thumb2_insn_r)
13011 {
13012   struct regcache *reg_cache = thumb2_insn_r->regcache;
13013
13014   uint32_t reg_rn, op;
13015   uint32_t register_bits = 0, register_count = 0;
13016   uint32_t index = 0, start_address = 0;
13017   uint32_t record_buf[24], record_buf_mem[48];
13018
13019   ULONGEST u_regval = 0;
13020
13021   reg_rn = bits (thumb2_insn_r->arm_insn, 16, 19);
13022   op = bits (thumb2_insn_r->arm_insn, 23, 24);
13023
13024   if (0 == op || 3 == op)
13025     {
13026       if (bit (thumb2_insn_r->arm_insn, INSN_S_L_BIT_NUM))
13027         {
13028           /* Handle RFE instruction.  */
13029           record_buf[0] = ARM_PS_REGNUM;
13030           thumb2_insn_r->reg_rec_count = 1;
13031         }
13032       else
13033         {
13034           /* Handle SRS instruction after reading banked SP.  */
13035           return arm_record_unsupported_insn (thumb2_insn_r);
13036         }
13037     }
13038   else if (1 == op || 2 == op)
13039     {
13040       if (bit (thumb2_insn_r->arm_insn, INSN_S_L_BIT_NUM))
13041         {
13042           /* Handle LDM/LDMIA/LDMFD and LDMDB/LDMEA instructions.  */
13043           register_bits = bits (thumb2_insn_r->arm_insn, 0, 15);
13044           while (register_bits)
13045             {
13046               if (register_bits & 0x00000001)
13047                 record_buf[index++] = register_count;
13048
13049               register_count++;
13050               register_bits = register_bits >> 1;
13051             }
13052           record_buf[index++] = reg_rn;
13053           record_buf[index++] = ARM_PS_REGNUM;
13054           thumb2_insn_r->reg_rec_count = index;
13055         }
13056       else
13057         {
13058           /* Handle STM/STMIA/STMEA and STMDB/STMFD.  */
13059           register_bits = bits (thumb2_insn_r->arm_insn, 0, 15);
13060           regcache_raw_read_unsigned (reg_cache, reg_rn, &u_regval);
13061           while (register_bits)
13062             {
13063               if (register_bits & 0x00000001)
13064                 register_count++;
13065
13066               register_bits = register_bits >> 1;
13067             }
13068
13069           if (1 == op)
13070             {
13071               /* Start address calculation for LDMDB/LDMEA.  */
13072               start_address = u_regval;
13073             }
13074           else if (2 == op)
13075             {
13076               /* Start address calculation for LDMDB/LDMEA.  */
13077               start_address = u_regval - register_count * 4;
13078             }
13079
13080           thumb2_insn_r->mem_rec_count = register_count;
13081           while (register_count)
13082             {
13083               record_buf_mem[register_count * 2 - 1] = start_address;
13084               record_buf_mem[register_count * 2 - 2] = 4;
13085               start_address = start_address + 4;
13086               register_count--;
13087             }
13088           record_buf[0] = reg_rn;
13089           record_buf[1] = ARM_PS_REGNUM;
13090           thumb2_insn_r->reg_rec_count = 2;
13091         }
13092     }
13093
13094   MEM_ALLOC (thumb2_insn_r->arm_mems, thumb2_insn_r->mem_rec_count,
13095             record_buf_mem);
13096   REG_ALLOC (thumb2_insn_r->arm_regs, thumb2_insn_r->reg_rec_count,
13097             record_buf);
13098   return ARM_RECORD_SUCCESS;
13099 }
13100
13101 /* Handler for thumb2 load/store (dual/exclusive) and table branch
13102    instructions.  */
13103
13104 static int
13105 thumb2_record_ld_st_dual_ex_tbb (insn_decode_record *thumb2_insn_r)
13106 {
13107   struct regcache *reg_cache = thumb2_insn_r->regcache;
13108
13109   uint32_t reg_rd, reg_rn, offset_imm;
13110   uint32_t reg_dest1, reg_dest2;
13111   uint32_t address, offset_addr;
13112   uint32_t record_buf[8], record_buf_mem[8];
13113   uint32_t op1, op2, op3;
13114   LONGEST s_word;
13115
13116   ULONGEST u_regval[2];
13117
13118   op1 = bits (thumb2_insn_r->arm_insn, 23, 24);
13119   op2 = bits (thumb2_insn_r->arm_insn, 20, 21);
13120   op3 = bits (thumb2_insn_r->arm_insn, 4, 7);
13121
13122   if (bit (thumb2_insn_r->arm_insn, INSN_S_L_BIT_NUM))
13123     {
13124       if(!(1 == op1 && 1 == op2 && (0 == op3 || 1 == op3)))
13125         {
13126           reg_dest1 = bits (thumb2_insn_r->arm_insn, 12, 15);
13127           record_buf[0] = reg_dest1;
13128           record_buf[1] = ARM_PS_REGNUM;
13129           thumb2_insn_r->reg_rec_count = 2;
13130         }
13131
13132       if (3 == op2 || (op1 & 2) || (1 == op1 && 1 == op2 && 7 == op3))
13133         {
13134           reg_dest2 = bits (thumb2_insn_r->arm_insn, 8, 11);
13135           record_buf[2] = reg_dest2;
13136           thumb2_insn_r->reg_rec_count = 3;
13137         }
13138     }
13139   else
13140     {
13141       reg_rn = bits (thumb2_insn_r->arm_insn, 16, 19);
13142       regcache_raw_read_unsigned (reg_cache, reg_rn, &u_regval[0]);
13143
13144       if (0 == op1 && 0 == op2)
13145         {
13146           /* Handle STREX.  */
13147           offset_imm = bits (thumb2_insn_r->arm_insn, 0, 7);
13148           address = u_regval[0] + (offset_imm * 4);
13149           record_buf_mem[0] = 4;
13150           record_buf_mem[1] = address;
13151           thumb2_insn_r->mem_rec_count = 1;
13152           reg_rd = bits (thumb2_insn_r->arm_insn, 0, 3);
13153           record_buf[0] = reg_rd;
13154           thumb2_insn_r->reg_rec_count = 1;
13155         }
13156       else if (1 == op1 && 0 == op2)
13157         {
13158           reg_rd = bits (thumb2_insn_r->arm_insn, 0, 3);
13159           record_buf[0] = reg_rd;
13160           thumb2_insn_r->reg_rec_count = 1;
13161           address = u_regval[0];
13162           record_buf_mem[1] = address;
13163
13164           if (4 == op3)
13165             {
13166               /* Handle STREXB.  */
13167               record_buf_mem[0] = 1;
13168               thumb2_insn_r->mem_rec_count = 1;
13169             }
13170           else if (5 == op3)
13171             {
13172               /* Handle STREXH.  */
13173               record_buf_mem[0] = 2 ;
13174               thumb2_insn_r->mem_rec_count = 1;
13175             }
13176           else if (7 == op3)
13177             {
13178               /* Handle STREXD.  */
13179               address = u_regval[0];
13180               record_buf_mem[0] = 4;
13181               record_buf_mem[2] = 4;
13182               record_buf_mem[3] = address + 4;
13183               thumb2_insn_r->mem_rec_count = 2;
13184             }
13185         }
13186       else
13187         {
13188           offset_imm = bits (thumb2_insn_r->arm_insn, 0, 7);
13189
13190           if (bit (thumb2_insn_r->arm_insn, 24))
13191             {
13192               if (bit (thumb2_insn_r->arm_insn, 23))
13193                 offset_addr = u_regval[0] + (offset_imm * 4);
13194               else
13195                 offset_addr = u_regval[0] - (offset_imm * 4);
13196
13197               address = offset_addr;
13198             }
13199           else
13200             address = u_regval[0];
13201
13202           record_buf_mem[0] = 4;
13203           record_buf_mem[1] = address;
13204           record_buf_mem[2] = 4;
13205           record_buf_mem[3] = address + 4;
13206           thumb2_insn_r->mem_rec_count = 2;
13207           record_buf[0] = reg_rn;
13208           thumb2_insn_r->reg_rec_count = 1;
13209         }
13210     }
13211
13212   REG_ALLOC (thumb2_insn_r->arm_regs, thumb2_insn_r->reg_rec_count,
13213             record_buf);
13214   MEM_ALLOC (thumb2_insn_r->arm_mems, thumb2_insn_r->mem_rec_count,
13215             record_buf_mem);
13216   return ARM_RECORD_SUCCESS;
13217 }
13218
13219 /* Handler for thumb2 data processing (shift register and modified immediate)
13220    instructions.  */
13221
13222 static int
13223 thumb2_record_data_proc_sreg_mimm (insn_decode_record *thumb2_insn_r)
13224 {
13225   uint32_t reg_rd, op;
13226   uint32_t record_buf[8];
13227
13228   op = bits (thumb2_insn_r->arm_insn, 21, 24);
13229   reg_rd = bits (thumb2_insn_r->arm_insn, 8, 11);
13230
13231   if ((0 == op || 4 == op || 8 == op || 13 == op) && 15 == reg_rd)
13232     {
13233       record_buf[0] = ARM_PS_REGNUM;
13234       thumb2_insn_r->reg_rec_count = 1;
13235     }
13236   else
13237     {
13238       record_buf[0] = reg_rd;
13239       record_buf[1] = ARM_PS_REGNUM;
13240       thumb2_insn_r->reg_rec_count = 2;
13241     }
13242
13243   REG_ALLOC (thumb2_insn_r->arm_regs, thumb2_insn_r->reg_rec_count,
13244             record_buf);
13245   return ARM_RECORD_SUCCESS;
13246 }
13247
13248 /* Generic handler for thumb2 instructions which effect destination and PS
13249    registers.  */
13250
13251 static int
13252 thumb2_record_ps_dest_generic (insn_decode_record *thumb2_insn_r)
13253 {
13254   uint32_t reg_rd;
13255   uint32_t record_buf[8];
13256
13257   reg_rd = bits (thumb2_insn_r->arm_insn, 8, 11);
13258
13259   record_buf[0] = reg_rd;
13260   record_buf[1] = ARM_PS_REGNUM;
13261   thumb2_insn_r->reg_rec_count = 2;
13262
13263   REG_ALLOC (thumb2_insn_r->arm_regs, thumb2_insn_r->reg_rec_count,
13264             record_buf);
13265   return ARM_RECORD_SUCCESS;
13266 }
13267
13268 /* Handler for thumb2 branch and miscellaneous control instructions.  */
13269
13270 static int
13271 thumb2_record_branch_misc_cntrl (insn_decode_record *thumb2_insn_r)
13272 {
13273   uint32_t op, op1, op2;
13274   uint32_t record_buf[8];
13275
13276   op = bits (thumb2_insn_r->arm_insn, 20, 26);
13277   op1 = bits (thumb2_insn_r->arm_insn, 12, 14);
13278   op2 = bits (thumb2_insn_r->arm_insn, 8, 11);
13279
13280   /* Handle MSR insn.  */
13281   if (!(op1 & 0x2) && 0x38 == op)
13282     {
13283       if (!(op2 & 0x3))
13284         {
13285           /* CPSR is going to be changed.  */
13286           record_buf[0] = ARM_PS_REGNUM;
13287           thumb2_insn_r->reg_rec_count = 1;
13288         }
13289       else
13290         {
13291           arm_record_unsupported_insn(thumb2_insn_r);
13292           return -1;
13293         }
13294     }
13295   else if (4 == (op1 & 0x5) || 5 == (op1 & 0x5))
13296     {
13297       /* BLX.  */
13298       record_buf[0] = ARM_PS_REGNUM;
13299       record_buf[1] = ARM_LR_REGNUM;
13300       thumb2_insn_r->reg_rec_count = 2;
13301     }
13302
13303   REG_ALLOC (thumb2_insn_r->arm_regs, thumb2_insn_r->reg_rec_count,
13304             record_buf);
13305   return ARM_RECORD_SUCCESS;
13306 }
13307
13308 /* Handler for thumb2 store single data item instructions.  */
13309
13310 static int
13311 thumb2_record_str_single_data (insn_decode_record *thumb2_insn_r)
13312 {
13313   struct regcache *reg_cache = thumb2_insn_r->regcache;
13314
13315   uint32_t reg_rn, reg_rm, offset_imm, shift_imm;
13316   uint32_t address, offset_addr;
13317   uint32_t record_buf[8], record_buf_mem[8];
13318   uint32_t op1, op2;
13319
13320   ULONGEST u_regval[2];
13321
13322   op1 = bits (thumb2_insn_r->arm_insn, 21, 23);
13323   op2 = bits (thumb2_insn_r->arm_insn, 6, 11);
13324   reg_rn = bits (thumb2_insn_r->arm_insn, 16, 19);
13325   regcache_raw_read_unsigned (reg_cache, reg_rn, &u_regval[0]);
13326
13327   if (bit (thumb2_insn_r->arm_insn, 23))
13328     {
13329       /* T2 encoding.  */
13330       offset_imm = bits (thumb2_insn_r->arm_insn, 0, 11);
13331       offset_addr = u_regval[0] + offset_imm;
13332       address = offset_addr;
13333     }
13334   else
13335     {
13336       /* T3 encoding.  */
13337       if ((0 == op1 || 1 == op1 || 2 == op1) && !(op2 & 0x20))
13338         {
13339           /* Handle STRB (register).  */
13340           reg_rm = bits (thumb2_insn_r->arm_insn, 0, 3);
13341           regcache_raw_read_unsigned (reg_cache, reg_rm, &u_regval[1]);
13342           shift_imm = bits (thumb2_insn_r->arm_insn, 4, 5);
13343           offset_addr = u_regval[1] << shift_imm;
13344           address = u_regval[0] + offset_addr;
13345         }
13346       else
13347         {
13348           offset_imm = bits (thumb2_insn_r->arm_insn, 0, 7);
13349           if (bit (thumb2_insn_r->arm_insn, 10))
13350             {
13351               if (bit (thumb2_insn_r->arm_insn, 9))
13352                 offset_addr = u_regval[0] + offset_imm;
13353               else
13354                 offset_addr = u_regval[0] - offset_imm;
13355
13356               address = offset_addr;
13357             }
13358           else
13359             address = u_regval[0];
13360         }
13361     }
13362
13363   switch (op1)
13364     {
13365       /* Store byte instructions.  */
13366       case 4:
13367       case 0:
13368         record_buf_mem[0] = 1;
13369         break;
13370       /* Store half word instructions.  */
13371       case 1:
13372       case 5:
13373         record_buf_mem[0] = 2;
13374         break;
13375       /* Store word instructions.  */
13376       case 2:
13377       case 6:
13378         record_buf_mem[0] = 4;
13379         break;
13380
13381       default:
13382         gdb_assert_not_reached ("no decoding pattern found");
13383         break;
13384     }
13385
13386   record_buf_mem[1] = address;
13387   thumb2_insn_r->mem_rec_count = 1;
13388   record_buf[0] = reg_rn;
13389   thumb2_insn_r->reg_rec_count = 1;
13390
13391   REG_ALLOC (thumb2_insn_r->arm_regs, thumb2_insn_r->reg_rec_count,
13392             record_buf);
13393   MEM_ALLOC (thumb2_insn_r->arm_mems, thumb2_insn_r->mem_rec_count,
13394             record_buf_mem);
13395   return ARM_RECORD_SUCCESS;
13396 }
13397
13398 /* Handler for thumb2 load memory hints instructions.  */
13399
13400 static int
13401 thumb2_record_ld_mem_hints (insn_decode_record *thumb2_insn_r)
13402 {
13403   uint32_t record_buf[8];
13404   uint32_t reg_rt, reg_rn;
13405
13406   reg_rt = bits (thumb2_insn_r->arm_insn, 12, 15);
13407   reg_rn = bits (thumb2_insn_r->arm_insn, 16, 19);
13408
13409   if (ARM_PC_REGNUM != reg_rt)
13410     {
13411       record_buf[0] = reg_rt;
13412       record_buf[1] = reg_rn;
13413       record_buf[2] = ARM_PS_REGNUM;
13414       thumb2_insn_r->reg_rec_count = 3;
13415
13416       REG_ALLOC (thumb2_insn_r->arm_regs, thumb2_insn_r->reg_rec_count,
13417                 record_buf);
13418       return ARM_RECORD_SUCCESS;
13419     }
13420
13421   return ARM_RECORD_FAILURE;
13422 }
13423
13424 /* Handler for thumb2 load word instructions.  */
13425
13426 static int
13427 thumb2_record_ld_word (insn_decode_record *thumb2_insn_r)
13428 {
13429   uint32_t opcode1 = 0, opcode2 = 0;
13430   uint32_t record_buf[8];
13431
13432   record_buf[0] = bits (thumb2_insn_r->arm_insn, 12, 15);
13433   record_buf[1] = ARM_PS_REGNUM;
13434   thumb2_insn_r->reg_rec_count = 2;
13435
13436   REG_ALLOC (thumb2_insn_r->arm_regs, thumb2_insn_r->reg_rec_count,
13437             record_buf);
13438   return ARM_RECORD_SUCCESS;
13439 }
13440
13441 /* Handler for thumb2 long multiply, long multiply accumulate, and
13442    divide instructions.  */
13443
13444 static int
13445 thumb2_record_lmul_lmla_div (insn_decode_record *thumb2_insn_r)
13446 {
13447   uint32_t opcode1 = 0, opcode2 = 0;
13448   uint32_t record_buf[8];
13449   uint32_t reg_src1 = 0;
13450
13451   opcode1 = bits (thumb2_insn_r->arm_insn, 20, 22);
13452   opcode2 = bits (thumb2_insn_r->arm_insn, 4, 7);
13453
13454   if (0 == opcode1 || 2 == opcode1 || (opcode1 >= 4 && opcode1 <= 6))
13455     {
13456       /* Handle SMULL, UMULL, SMULAL.  */
13457       /* Handle SMLAL(S), SMULL(S), UMLAL(S), UMULL(S).  */
13458       record_buf[0] = bits (thumb2_insn_r->arm_insn, 16, 19);
13459       record_buf[1] = bits (thumb2_insn_r->arm_insn, 12, 15);
13460       record_buf[2] = ARM_PS_REGNUM;
13461       thumb2_insn_r->reg_rec_count = 3;
13462     }
13463   else if (1 == opcode1 || 3 == opcode2)
13464     {
13465       /* Handle SDIV and UDIV.  */
13466       record_buf[0] = bits (thumb2_insn_r->arm_insn, 16, 19);
13467       record_buf[1] = bits (thumb2_insn_r->arm_insn, 12, 15);
13468       record_buf[2] = ARM_PS_REGNUM;
13469       thumb2_insn_r->reg_rec_count = 3;
13470     }
13471   else
13472     return ARM_RECORD_FAILURE;
13473
13474   REG_ALLOC (thumb2_insn_r->arm_regs, thumb2_insn_r->reg_rec_count,
13475             record_buf);
13476   return ARM_RECORD_SUCCESS;
13477 }
13478
13479 /* Record handler for thumb32 coprocessor instructions.  */
13480
13481 static int
13482 thumb2_record_coproc_insn (insn_decode_record *thumb2_insn_r)
13483 {
13484   if (bit (thumb2_insn_r->arm_insn, 25))
13485     return arm_record_coproc_data_proc (thumb2_insn_r);
13486   else
13487     return arm_record_asimd_vfp_coproc (thumb2_insn_r);
13488 }
13489
13490 /* Record handler for advance SIMD structure load/store instructions.  */
13491
13492 static int
13493 thumb2_record_asimd_struct_ld_st (insn_decode_record *thumb2_insn_r)
13494 {
13495   struct regcache *reg_cache = thumb2_insn_r->regcache;
13496   uint32_t l_bit, a_bit, b_bits;
13497   uint32_t record_buf[128], record_buf_mem[128];
13498   uint32_t reg_rn, reg_vd, address, f_esize, f_elem;
13499   uint32_t index_r = 0, index_e = 0, bf_regs = 0, index_m = 0, loop_t = 0;
13500   uint8_t f_ebytes;
13501
13502   l_bit = bit (thumb2_insn_r->arm_insn, 21);
13503   a_bit = bit (thumb2_insn_r->arm_insn, 23);
13504   b_bits = bits (thumb2_insn_r->arm_insn, 8, 11);
13505   reg_rn = bits (thumb2_insn_r->arm_insn, 16, 19);
13506   reg_vd = bits (thumb2_insn_r->arm_insn, 12, 15);
13507   reg_vd = (bit (thumb2_insn_r->arm_insn, 22) << 4) | reg_vd;
13508   f_ebytes = (1 << bits (thumb2_insn_r->arm_insn, 6, 7));
13509   f_esize = 8 * f_ebytes;
13510   f_elem = 8 / f_ebytes;
13511
13512   if (!l_bit)
13513     {
13514       ULONGEST u_regval = 0;
13515       regcache_raw_read_unsigned (reg_cache, reg_rn, &u_regval);
13516       address = u_regval;
13517
13518       if (!a_bit)
13519         {
13520           /* Handle VST1.  */
13521           if (b_bits == 0x02 || b_bits == 0x0a || (b_bits & 0x0e) == 0x06)
13522             {
13523               if (b_bits == 0x07)
13524                 bf_regs = 1;
13525               else if (b_bits == 0x0a)
13526                 bf_regs = 2;
13527               else if (b_bits == 0x06)
13528                 bf_regs = 3;
13529               else if (b_bits == 0x02)
13530                 bf_regs = 4;
13531               else
13532                 bf_regs = 0;
13533
13534               for (index_r = 0; index_r < bf_regs; index_r++)
13535                 {
13536                   for (index_e = 0; index_e < f_elem; index_e++)
13537                     {
13538                       record_buf_mem[index_m++] = f_ebytes;
13539                       record_buf_mem[index_m++] = address;
13540                       address = address + f_ebytes;
13541                       thumb2_insn_r->mem_rec_count += 1;
13542                     }
13543                 }
13544             }
13545           /* Handle VST2.  */
13546           else if (b_bits == 0x03 || (b_bits & 0x0e) == 0x08)
13547             {
13548               if (b_bits == 0x09 || b_bits == 0x08)
13549                 bf_regs = 1;
13550               else if (b_bits == 0x03)
13551                 bf_regs = 2;
13552               else
13553                 bf_regs = 0;
13554
13555               for (index_r = 0; index_r < bf_regs; index_r++)
13556                 for (index_e = 0; index_e < f_elem; index_e++)
13557                   {
13558                     for (loop_t = 0; loop_t < 2; loop_t++)
13559                       {
13560                         record_buf_mem[index_m++] = f_ebytes;
13561                         record_buf_mem[index_m++] = address + (loop_t * f_ebytes);
13562                         thumb2_insn_r->mem_rec_count += 1;
13563                       }
13564                     address = address + (2 * f_ebytes);
13565                   }
13566             }
13567           /* Handle VST3.  */
13568           else if ((b_bits & 0x0e) == 0x04)
13569             {
13570               for (index_e = 0; index_e < f_elem; index_e++)
13571                 {
13572                   for (loop_t = 0; loop_t < 3; loop_t++)
13573                     {
13574                       record_buf_mem[index_m++] = f_ebytes;
13575                       record_buf_mem[index_m++] = address + (loop_t * f_ebytes);
13576                       thumb2_insn_r->mem_rec_count += 1;
13577                     }
13578                   address = address + (3 * f_ebytes);
13579                 }
13580             }
13581           /* Handle VST4.  */
13582           else if (!(b_bits & 0x0e))
13583             {
13584               for (index_e = 0; index_e < f_elem; index_e++)
13585                 {
13586                   for (loop_t = 0; loop_t < 4; loop_t++)
13587                     {
13588                       record_buf_mem[index_m++] = f_ebytes;
13589                       record_buf_mem[index_m++] = address + (loop_t * f_ebytes);
13590                       thumb2_insn_r->mem_rec_count += 1;
13591                     }
13592                   address = address + (4 * f_ebytes);
13593                 }
13594             }
13595         }
13596       else
13597         {
13598           uint8_t bft_size = bits (thumb2_insn_r->arm_insn, 10, 11);
13599
13600           if (bft_size == 0x00)
13601             f_ebytes = 1;
13602           else if (bft_size == 0x01)
13603             f_ebytes = 2;
13604           else if (bft_size == 0x02)
13605             f_ebytes = 4;
13606           else
13607             f_ebytes = 0;
13608
13609           /* Handle VST1.  */
13610           if (!(b_bits & 0x0b) || b_bits == 0x08)
13611             thumb2_insn_r->mem_rec_count = 1;
13612           /* Handle VST2.  */
13613           else if ((b_bits & 0x0b) == 0x01 || b_bits == 0x09)
13614             thumb2_insn_r->mem_rec_count = 2;
13615           /* Handle VST3.  */
13616           else if ((b_bits & 0x0b) == 0x02 || b_bits == 0x0a)
13617             thumb2_insn_r->mem_rec_count = 3;
13618           /* Handle VST4.  */
13619           else if ((b_bits & 0x0b) == 0x03 || b_bits == 0x0b)
13620             thumb2_insn_r->mem_rec_count = 4;
13621
13622           for (index_m = 0; index_m < thumb2_insn_r->mem_rec_count; index_m++)
13623             {
13624               record_buf_mem[index_m] = f_ebytes;
13625               record_buf_mem[index_m] = address + (index_m * f_ebytes);
13626             }
13627         }
13628     }
13629   else
13630     {
13631       if (!a_bit)
13632         {
13633           /* Handle VLD1.  */
13634           if (b_bits == 0x02 || b_bits == 0x0a || (b_bits & 0x0e) == 0x06)
13635             thumb2_insn_r->reg_rec_count = 1;
13636           /* Handle VLD2.  */
13637           else if (b_bits == 0x03 || (b_bits & 0x0e) == 0x08)
13638             thumb2_insn_r->reg_rec_count = 2;
13639           /* Handle VLD3.  */
13640           else if ((b_bits & 0x0e) == 0x04)
13641             thumb2_insn_r->reg_rec_count = 3;
13642           /* Handle VLD4.  */
13643           else if (!(b_bits & 0x0e))
13644             thumb2_insn_r->reg_rec_count = 4;
13645         }
13646       else
13647         {
13648           /* Handle VLD1.  */
13649           if (!(b_bits & 0x0b) || b_bits == 0x08 || b_bits == 0x0c)
13650             thumb2_insn_r->reg_rec_count = 1;
13651           /* Handle VLD2.  */
13652           else if ((b_bits & 0x0b) == 0x01 || b_bits == 0x09 || b_bits == 0x0d)
13653             thumb2_insn_r->reg_rec_count = 2;
13654           /* Handle VLD3.  */
13655           else if ((b_bits & 0x0b) == 0x02 || b_bits == 0x0a || b_bits == 0x0e)
13656             thumb2_insn_r->reg_rec_count = 3;
13657           /* Handle VLD4.  */
13658           else if ((b_bits & 0x0b) == 0x03 || b_bits == 0x0b || b_bits == 0x0f)
13659             thumb2_insn_r->reg_rec_count = 4;
13660
13661           for (index_r = 0; index_r < thumb2_insn_r->reg_rec_count; index_r++)
13662             record_buf[index_r] = reg_vd + ARM_D0_REGNUM + index_r;
13663         }
13664     }
13665
13666   if (bits (thumb2_insn_r->arm_insn, 0, 3) != 15)
13667     {
13668       record_buf[index_r] = reg_rn;
13669       thumb2_insn_r->reg_rec_count += 1;
13670     }
13671
13672   REG_ALLOC (thumb2_insn_r->arm_regs, thumb2_insn_r->reg_rec_count,
13673             record_buf);
13674   MEM_ALLOC (thumb2_insn_r->arm_mems, thumb2_insn_r->mem_rec_count,
13675             record_buf_mem);
13676   return 0;
13677 }
13678
13679 /* Decodes thumb2 instruction type and invokes its record handler.  */
13680
13681 static unsigned int
13682 thumb2_record_decode_insn_handler (insn_decode_record *thumb2_insn_r)
13683 {
13684   uint32_t op, op1, op2;
13685
13686   op = bit (thumb2_insn_r->arm_insn, 15);
13687   op1 = bits (thumb2_insn_r->arm_insn, 27, 28);
13688   op2 = bits (thumb2_insn_r->arm_insn, 20, 26);
13689
13690   if (op1 == 0x01)
13691     {
13692       if (!(op2 & 0x64 ))
13693         {
13694           /* Load/store multiple instruction.  */
13695           return thumb2_record_ld_st_multiple (thumb2_insn_r);
13696         }
13697       else if (!((op2 & 0x64) ^ 0x04))
13698         {
13699           /* Load/store (dual/exclusive) and table branch instruction.  */
13700           return thumb2_record_ld_st_dual_ex_tbb (thumb2_insn_r);
13701         }
13702       else if (!((op2 & 0x20) ^ 0x20))
13703         {
13704           /* Data-processing (shifted register).  */
13705           return thumb2_record_data_proc_sreg_mimm (thumb2_insn_r);
13706         }
13707       else if (op2 & 0x40)
13708         {
13709           /* Co-processor instructions.  */
13710           return thumb2_record_coproc_insn (thumb2_insn_r);
13711         }
13712     }
13713   else if (op1 == 0x02)
13714     {
13715       if (op)
13716         {
13717           /* Branches and miscellaneous control instructions.  */
13718           return thumb2_record_branch_misc_cntrl (thumb2_insn_r);
13719         }
13720       else if (op2 & 0x20)
13721         {
13722           /* Data-processing (plain binary immediate) instruction.  */
13723           return thumb2_record_ps_dest_generic (thumb2_insn_r);
13724         }
13725       else
13726         {
13727           /* Data-processing (modified immediate).  */
13728           return thumb2_record_data_proc_sreg_mimm (thumb2_insn_r);
13729         }
13730     }
13731   else if (op1 == 0x03)
13732     {
13733       if (!(op2 & 0x71 ))
13734         {
13735           /* Store single data item.  */
13736           return thumb2_record_str_single_data (thumb2_insn_r);
13737         }
13738       else if (!((op2 & 0x71) ^ 0x10))
13739         {
13740           /* Advanced SIMD or structure load/store instructions.  */
13741           return thumb2_record_asimd_struct_ld_st (thumb2_insn_r);
13742         }
13743       else if (!((op2 & 0x67) ^ 0x01))
13744         {
13745           /* Load byte, memory hints instruction.  */
13746           return thumb2_record_ld_mem_hints (thumb2_insn_r);
13747         }
13748       else if (!((op2 & 0x67) ^ 0x03))
13749         {
13750           /* Load halfword, memory hints instruction.  */
13751           return thumb2_record_ld_mem_hints (thumb2_insn_r);
13752         }
13753       else if (!((op2 & 0x67) ^ 0x05))
13754         {
13755           /* Load word instruction.  */
13756           return thumb2_record_ld_word (thumb2_insn_r);
13757         }
13758       else if (!((op2 & 0x70) ^ 0x20))
13759         {
13760           /* Data-processing (register) instruction.  */
13761           return thumb2_record_ps_dest_generic (thumb2_insn_r);
13762         }
13763       else if (!((op2 & 0x78) ^ 0x30))
13764         {
13765           /* Multiply, multiply accumulate, abs diff instruction.  */
13766           return thumb2_record_ps_dest_generic (thumb2_insn_r);
13767         }
13768       else if (!((op2 & 0x78) ^ 0x38))
13769         {
13770           /* Long multiply, long multiply accumulate, and divide.  */
13771           return thumb2_record_lmul_lmla_div (thumb2_insn_r);
13772         }
13773       else if (op2 & 0x40)
13774         {
13775           /* Co-processor instructions.  */
13776           return thumb2_record_coproc_insn (thumb2_insn_r);
13777         }
13778    }
13779
13780   return -1;
13781 }
13782
13783 /* Extracts arm/thumb/thumb2 insn depending on the size, and returns 0 on success 
13784 and positive val on fauilure.  */
13785
13786 static int
13787 extract_arm_insn (insn_decode_record *insn_record, uint32_t insn_size)
13788 {
13789   gdb_byte buf[insn_size];
13790
13791   memset (&buf[0], 0, insn_size);
13792   
13793   if (target_read_memory (insn_record->this_addr, &buf[0], insn_size))
13794     return 1;
13795   insn_record->arm_insn = (uint32_t) extract_unsigned_integer (&buf[0],
13796                            insn_size, 
13797                            gdbarch_byte_order_for_code (insn_record->gdbarch));
13798   return 0;
13799 }
13800
13801 typedef int (*sti_arm_hdl_fp_t) (insn_decode_record*);
13802
13803 /* Decode arm/thumb insn depending on condition cods and opcodes; and
13804    dispatch it.  */
13805
13806 static int
13807 decode_insn (insn_decode_record *arm_record, record_type_t record_type,
13808                 uint32_t insn_size)
13809 {
13810
13811   /* (Starting from numerical 0); bits 25, 26, 27 decodes type of arm instruction.  */
13812   static const sti_arm_hdl_fp_t arm_handle_insn[8] =
13813   {
13814     arm_record_data_proc_misc_ld_str,   /* 000.  */
13815     arm_record_data_proc_imm,           /* 001.  */
13816     arm_record_ld_st_imm_offset,        /* 010.  */
13817     arm_record_ld_st_reg_offset,        /* 011.  */
13818     arm_record_ld_st_multiple,          /* 100.  */
13819     arm_record_b_bl,                    /* 101.  */
13820     arm_record_asimd_vfp_coproc,        /* 110.  */
13821     arm_record_coproc_data_proc         /* 111.  */
13822   };
13823
13824   /* (Starting from numerical 0); bits 13,14,15 decodes type of thumb instruction.  */
13825   static const sti_arm_hdl_fp_t thumb_handle_insn[8] =
13826   { \
13827     thumb_record_shift_add_sub,        /* 000.  */
13828     thumb_record_add_sub_cmp_mov,      /* 001.  */
13829     thumb_record_ld_st_reg_offset,     /* 010.  */
13830     thumb_record_ld_st_imm_offset,     /* 011.  */
13831     thumb_record_ld_st_stack,          /* 100.  */
13832     thumb_record_misc,                 /* 101.  */
13833     thumb_record_ldm_stm_swi,          /* 110.  */
13834     thumb_record_branch                /* 111.  */
13835   };
13836
13837   uint32_t ret = 0;    /* return value: negative:failure   0:success.  */
13838   uint32_t insn_id = 0;
13839
13840   if (extract_arm_insn (arm_record, insn_size))
13841     {
13842       if (record_debug)
13843         {
13844           printf_unfiltered (_("Process record: error reading memory at "
13845                               "addr %s len = %d.\n"),
13846           paddress (arm_record->gdbarch, arm_record->this_addr), insn_size);        
13847         }
13848       return -1;
13849     }
13850   else if (ARM_RECORD == record_type)
13851     {
13852       arm_record->cond = bits (arm_record->arm_insn, 28, 31);
13853       insn_id = bits (arm_record->arm_insn, 25, 27);
13854       ret = arm_record_extension_space (arm_record);
13855       /* If this insn has fallen into extension space 
13856          then we need not decode it anymore.  */
13857       if (ret != -1 && !INSN_RECORDED(arm_record))
13858         {
13859           ret = arm_handle_insn[insn_id] (arm_record);
13860         }
13861     }
13862   else if (THUMB_RECORD == record_type)
13863     {
13864       /* As thumb does not have condition codes, we set negative.  */
13865       arm_record->cond = -1;
13866       insn_id = bits (arm_record->arm_insn, 13, 15);
13867       ret = thumb_handle_insn[insn_id] (arm_record);
13868     }
13869   else if (THUMB2_RECORD == record_type)
13870     {
13871       /* As thumb does not have condition codes, we set negative.  */
13872       arm_record->cond = -1;
13873
13874       /* Swap first half of 32bit thumb instruction with second half.  */
13875       arm_record->arm_insn
13876         = (arm_record->arm_insn >> 16) | (arm_record->arm_insn << 16);
13877
13878       insn_id = thumb2_record_decode_insn_handler (arm_record);
13879
13880       if (insn_id != ARM_RECORD_SUCCESS)
13881         {
13882           arm_record_unsupported_insn (arm_record);
13883           ret = -1;
13884         }
13885     }
13886   else
13887     {
13888       /* Throw assertion.  */
13889       gdb_assert_not_reached ("not a valid instruction, could not decode");
13890     }
13891
13892   return ret;
13893 }
13894
13895
13896 /* Cleans up local record registers and memory allocations.  */
13897
13898 static void 
13899 deallocate_reg_mem (insn_decode_record *record)
13900 {
13901   xfree (record->arm_regs);
13902   xfree (record->arm_mems);    
13903 }
13904
13905
13906 /* Parse the current instruction and record the values of the registers and    
13907    memory that will be changed in current instruction to record_arch_list".
13908    Return -1 if something is wrong.  */
13909
13910 int
13911 arm_process_record (struct gdbarch *gdbarch, struct regcache *regcache, 
13912                         CORE_ADDR insn_addr)
13913 {
13914
13915   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
13916   uint32_t no_of_rec = 0;
13917   uint32_t ret = 0;  /* return value: -1:record failure ;  0:success  */
13918   ULONGEST t_bit = 0, insn_id = 0;
13919
13920   ULONGEST u_regval = 0;
13921
13922   insn_decode_record arm_record;
13923
13924   memset (&arm_record, 0, sizeof (insn_decode_record));
13925   arm_record.regcache = regcache;
13926   arm_record.this_addr = insn_addr;
13927   arm_record.gdbarch = gdbarch;
13928
13929
13930   if (record_debug > 1)
13931     {
13932       fprintf_unfiltered (gdb_stdlog, "Process record: arm_process_record "
13933                                       "addr = %s\n",
13934       paddress (gdbarch, arm_record.this_addr));
13935     }
13936
13937   if (extract_arm_insn (&arm_record, 2))
13938     {
13939       if (record_debug)
13940         {
13941           printf_unfiltered (_("Process record: error reading memory at "
13942                              "addr %s len = %d.\n"),
13943                              paddress (arm_record.gdbarch, 
13944                              arm_record.this_addr), 2);
13945         }
13946       return -1;
13947     }
13948
13949   /* Check the insn, whether it is thumb or arm one.  */
13950
13951   t_bit = arm_psr_thumb_bit (arm_record.gdbarch);
13952   regcache_raw_read_unsigned (arm_record.regcache, ARM_PS_REGNUM, &u_regval);
13953
13954
13955   if (!(u_regval & t_bit))
13956     {
13957       /* We are decoding arm insn.  */
13958       ret = decode_insn (&arm_record, ARM_RECORD, ARM_INSN_SIZE_BYTES);
13959     }
13960   else
13961     {
13962       insn_id = bits (arm_record.arm_insn, 11, 15);
13963       /* is it thumb2 insn?  */
13964       if ((0x1D == insn_id) || (0x1E == insn_id) || (0x1F == insn_id))
13965         {
13966           ret = decode_insn (&arm_record, THUMB2_RECORD, 
13967                              THUMB2_INSN_SIZE_BYTES);
13968         }
13969       else
13970         {
13971           /* We are decoding thumb insn.  */
13972           ret = decode_insn (&arm_record, THUMB_RECORD, THUMB_INSN_SIZE_BYTES);
13973         }
13974     }
13975
13976   if (0 == ret)
13977     {
13978       /* Record registers.  */
13979       record_full_arch_list_add_reg (arm_record.regcache, ARM_PC_REGNUM);
13980       if (arm_record.arm_regs)
13981         {
13982           for (no_of_rec = 0; no_of_rec < arm_record.reg_rec_count; no_of_rec++)
13983             {
13984               if (record_full_arch_list_add_reg
13985                   (arm_record.regcache , arm_record.arm_regs[no_of_rec]))
13986               ret = -1;
13987             }
13988         }
13989       /* Record memories.  */
13990       if (arm_record.arm_mems)
13991         {
13992           for (no_of_rec = 0; no_of_rec < arm_record.mem_rec_count; no_of_rec++)
13993             {
13994               if (record_full_arch_list_add_mem
13995                   ((CORE_ADDR)arm_record.arm_mems[no_of_rec].addr,
13996                    arm_record.arm_mems[no_of_rec].len))
13997                 ret = -1;
13998             }
13999         }
14000
14001       if (record_full_arch_list_add_end ())
14002         ret = -1;
14003     }
14004
14005
14006   deallocate_reg_mem (&arm_record);
14007
14008   return ret;
14009 }
14010