Include string.h in common-defs.h
[external/binutils.git] / gdb / arm-tdep.c
1 /* Common target dependent code for GDB on ARM systems.
2
3    Copyright (C) 1988-2014 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 (strncmp (name, "__truncdfsf2", strlen ("__truncdfsf2")) == 0)
488         return 1;
489       if (strncmp (name, "__aeabi_d2f", strlen ("__aeabi_d2f")) == 0)
490         return 1;
491
492       /* Internal functions related to thread-local storage.  */
493       if (strncmp (name, "__tls_get_addr", strlen ("__tls_get_addr")) == 0)
494         return 1;
495       if (strncmp (name, "__aeabi_read_tp", strlen ("__aeabi_read_tp")) == 0)
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 = bits (insn1, 0, 7);
1208         }
1209       else if ((insn1 & 0xfbf0) == 0xf240) /* movw Rd, #const */
1210         {
1211           unsigned short insn2
1212             = read_memory_unsigned_integer (pc + 2, 2, byte_order_for_code);
1213
1214           low = EXTRACT_MOVW_MOVT_IMM_T (insn1, insn2);
1215
1216           insn1
1217             = read_memory_unsigned_integer (pc + 4, 2, byte_order_for_code);
1218           insn2
1219             = read_memory_unsigned_integer (pc + 6, 2, byte_order_for_code);
1220
1221           /* movt Rd, #const */
1222           if ((insn1 & 0xfbc0) == 0xf2c0)
1223             {
1224               high = EXTRACT_MOVW_MOVT_IMM_T (insn1, insn2);
1225               *destreg = bits (insn2, 8, 11);
1226               *offset = 8;
1227               address = (high << 16 | low);
1228             }
1229         }
1230     }
1231   else
1232     {
1233       unsigned int insn
1234         = read_memory_unsigned_integer (pc, 4, byte_order_for_code);
1235
1236       if ((insn & 0x0e5f0000) == 0x041f0000) /* ldr Rd, #immed */
1237         {
1238           address = bits (insn, 0, 11);
1239           *destreg = bits (insn, 12, 15);
1240           *offset = 4;
1241         }
1242       else if ((insn & 0x0ff00000) == 0x03000000) /* movw Rd, #const */
1243         {
1244           low = EXTRACT_MOVW_MOVT_IMM_A (insn);
1245
1246           insn
1247             = read_memory_unsigned_integer (pc + 4, 4, byte_order_for_code);
1248
1249           if ((insn & 0x0ff00000) == 0x03400000) /* movt Rd, #const */
1250             {
1251               high = EXTRACT_MOVW_MOVT_IMM_A (insn);
1252               *destreg = bits (insn, 12, 15);
1253               *offset = 8;
1254               address = (high << 16 | low);
1255             }
1256         }
1257     }
1258
1259   return address;
1260 }
1261
1262 /* Try to skip a sequence of instructions used for stack protector.  If PC
1263    points to the first instruction of this sequence, return the address of
1264    first instruction after this sequence, otherwise, return original PC.
1265
1266    On arm, this sequence of instructions is composed of mainly three steps,
1267      Step 1: load symbol __stack_chk_guard,
1268      Step 2: load from address of __stack_chk_guard,
1269      Step 3: store it to somewhere else.
1270
1271    Usually, instructions on step 2 and step 3 are the same on various ARM
1272    architectures.  On step 2, it is one instruction 'ldr Rx, [Rn, #0]', and
1273    on step 3, it is also one instruction 'str Rx, [r7, #immd]'.  However,
1274    instructions in step 1 vary from different ARM architectures.  On ARMv7,
1275    they are,
1276
1277         movw    Rn, #:lower16:__stack_chk_guard
1278         movt    Rn, #:upper16:__stack_chk_guard
1279
1280    On ARMv5t, it is,
1281
1282         ldr     Rn, .Label
1283         ....
1284         .Lable:
1285         .word   __stack_chk_guard
1286
1287    Since ldr/str is a very popular instruction, we can't use them as
1288    'fingerprint' or 'signature' of stack protector sequence.  Here we choose
1289    sequence {movw/movt, ldr}/ldr/str plus symbol __stack_chk_guard, if not
1290    stripped, as the 'fingerprint' of a stack protector cdoe sequence.  */
1291
1292 static CORE_ADDR
1293 arm_skip_stack_protector(CORE_ADDR pc, struct gdbarch *gdbarch)
1294 {
1295   enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
1296   unsigned int basereg;
1297   struct bound_minimal_symbol stack_chk_guard;
1298   int offset;
1299   int is_thumb = arm_pc_is_thumb (gdbarch, pc);
1300   CORE_ADDR addr;
1301
1302   /* Try to parse the instructions in Step 1.  */
1303   addr = arm_analyze_load_stack_chk_guard (pc, gdbarch,
1304                                            &basereg, &offset);
1305   if (!addr)
1306     return pc;
1307
1308   stack_chk_guard = lookup_minimal_symbol_by_pc (addr);
1309   /* If name of symbol doesn't start with '__stack_chk_guard', this
1310      instruction sequence is not for stack protector.  If symbol is
1311      removed, we conservatively think this sequence is for stack protector.  */
1312   if (stack_chk_guard.minsym
1313       && strncmp (MSYMBOL_LINKAGE_NAME (stack_chk_guard.minsym),
1314                   "__stack_chk_guard",
1315                   strlen ("__stack_chk_guard")) != 0)
1316    return pc;
1317
1318   if (is_thumb)
1319     {
1320       unsigned int destreg;
1321       unsigned short insn
1322         = read_memory_unsigned_integer (pc + offset, 2, byte_order_for_code);
1323
1324       /* Step 2: ldr Rd, [Rn, #immed], encoding T1.  */
1325       if ((insn & 0xf800) != 0x6800)
1326         return pc;
1327       if (bits (insn, 3, 5) != basereg)
1328         return pc;
1329       destreg = bits (insn, 0, 2);
1330
1331       insn = read_memory_unsigned_integer (pc + offset + 2, 2,
1332                                            byte_order_for_code);
1333       /* Step 3: str Rd, [Rn, #immed], encoding T1.  */
1334       if ((insn & 0xf800) != 0x6000)
1335         return pc;
1336       if (destreg != bits (insn, 0, 2))
1337         return pc;
1338     }
1339   else
1340     {
1341       unsigned int destreg;
1342       unsigned int insn
1343         = read_memory_unsigned_integer (pc + offset, 4, byte_order_for_code);
1344
1345       /* Step 2: ldr Rd, [Rn, #immed], encoding A1.  */
1346       if ((insn & 0x0e500000) != 0x04100000)
1347         return pc;
1348       if (bits (insn, 16, 19) != basereg)
1349         return pc;
1350       destreg = bits (insn, 12, 15);
1351       /* Step 3: str Rd, [Rn, #immed], encoding A1.  */
1352       insn = read_memory_unsigned_integer (pc + offset + 4,
1353                                            4, byte_order_for_code);
1354       if ((insn & 0x0e500000) != 0x04000000)
1355         return pc;
1356       if (bits (insn, 12, 15) != destreg)
1357         return pc;
1358     }
1359   /* The size of total two instructions ldr/str is 4 on Thumb-2, while 8
1360      on arm.  */
1361   if (is_thumb)
1362     return pc + offset + 4;
1363   else
1364     return pc + offset + 8;
1365 }
1366
1367 /* Advance the PC across any function entry prologue instructions to
1368    reach some "real" code.
1369
1370    The APCS (ARM Procedure Call Standard) defines the following
1371    prologue:
1372
1373    mov          ip, sp
1374    [stmfd       sp!, {a1,a2,a3,a4}]
1375    stmfd        sp!, {...,fp,ip,lr,pc}
1376    [stfe        f7, [sp, #-12]!]
1377    [stfe        f6, [sp, #-12]!]
1378    [stfe        f5, [sp, #-12]!]
1379    [stfe        f4, [sp, #-12]!]
1380    sub fp, ip, #nn @@ nn == 20 or 4 depending on second insn.  */
1381
1382 static CORE_ADDR
1383 arm_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR pc)
1384 {
1385   enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
1386   unsigned long inst;
1387   CORE_ADDR skip_pc;
1388   CORE_ADDR func_addr, limit_pc;
1389
1390   /* See if we can determine the end of the prologue via the symbol table.
1391      If so, then return either PC, or the PC after the prologue, whichever
1392      is greater.  */
1393   if (find_pc_partial_function (pc, NULL, &func_addr, NULL))
1394     {
1395       CORE_ADDR post_prologue_pc
1396         = skip_prologue_using_sal (gdbarch, func_addr);
1397       struct symtab *s = find_pc_symtab (func_addr);
1398
1399       if (post_prologue_pc)
1400         post_prologue_pc
1401           = arm_skip_stack_protector (post_prologue_pc, gdbarch);
1402
1403
1404       /* GCC always emits a line note before the prologue and another
1405          one after, even if the two are at the same address or on the
1406          same line.  Take advantage of this so that we do not need to
1407          know every instruction that might appear in the prologue.  We
1408          will have producer information for most binaries; if it is
1409          missing (e.g. for -gstabs), assuming the GNU tools.  */
1410       if (post_prologue_pc
1411           && (s == NULL
1412               || s->producer == NULL
1413               || strncmp (s->producer, "GNU ", sizeof ("GNU ") - 1) == 0 
1414               || strncmp (s->producer, "clang ", sizeof ("clang ") - 1) == 0))
1415         return post_prologue_pc;
1416
1417       if (post_prologue_pc != 0)
1418         {
1419           CORE_ADDR analyzed_limit;
1420
1421           /* For non-GCC compilers, make sure the entire line is an
1422              acceptable prologue; GDB will round this function's
1423              return value up to the end of the following line so we
1424              can not skip just part of a line (and we do not want to).
1425
1426              RealView does not treat the prologue specially, but does
1427              associate prologue code with the opening brace; so this
1428              lets us skip the first line if we think it is the opening
1429              brace.  */
1430           if (arm_pc_is_thumb (gdbarch, func_addr))
1431             analyzed_limit = thumb_analyze_prologue (gdbarch, func_addr,
1432                                                      post_prologue_pc, NULL);
1433           else
1434             analyzed_limit = arm_analyze_prologue (gdbarch, func_addr,
1435                                                    post_prologue_pc, NULL);
1436
1437           if (analyzed_limit != post_prologue_pc)
1438             return func_addr;
1439
1440           return post_prologue_pc;
1441         }
1442     }
1443
1444   /* Can't determine prologue from the symbol table, need to examine
1445      instructions.  */
1446
1447   /* Find an upper limit on the function prologue using the debug
1448      information.  If the debug information could not be used to provide
1449      that bound, then use an arbitrary large number as the upper bound.  */
1450   /* Like arm_scan_prologue, stop no later than pc + 64.  */
1451   limit_pc = skip_prologue_using_sal (gdbarch, pc);
1452   if (limit_pc == 0)
1453     limit_pc = pc + 64;          /* Magic.  */
1454
1455
1456   /* Check if this is Thumb code.  */
1457   if (arm_pc_is_thumb (gdbarch, pc))
1458     return thumb_analyze_prologue (gdbarch, pc, limit_pc, NULL);
1459
1460   for (skip_pc = pc; skip_pc < limit_pc; skip_pc += 4)
1461     {
1462       inst = read_memory_unsigned_integer (skip_pc, 4, byte_order_for_code);
1463
1464       /* "mov ip, sp" is no longer a required part of the prologue.  */
1465       if (inst == 0xe1a0c00d)                   /* mov ip, sp */
1466         continue;
1467
1468       if ((inst & 0xfffff000) == 0xe28dc000)    /* add ip, sp #n */
1469         continue;
1470
1471       if ((inst & 0xfffff000) == 0xe24dc000)    /* sub ip, sp #n */
1472         continue;
1473
1474       /* Some prologues begin with "str lr, [sp, #-4]!".  */
1475       if (inst == 0xe52de004)                   /* str lr, [sp, #-4]! */
1476         continue;
1477
1478       if ((inst & 0xfffffff0) == 0xe92d0000)    /* stmfd sp!,{a1,a2,a3,a4} */
1479         continue;
1480
1481       if ((inst & 0xfffff800) == 0xe92dd800)    /* stmfd sp!,{fp,ip,lr,pc} */
1482         continue;
1483
1484       /* Any insns after this point may float into the code, if it makes
1485          for better instruction scheduling, so we skip them only if we
1486          find them, but still consider the function to be frame-ful.  */
1487
1488       /* We may have either one sfmfd instruction here, or several stfe
1489          insns, depending on the version of floating point code we
1490          support.  */
1491       if ((inst & 0xffbf0fff) == 0xec2d0200)    /* sfmfd fn, <cnt>, [sp]! */
1492         continue;
1493
1494       if ((inst & 0xffff8fff) == 0xed6d0103)    /* stfe fn, [sp, #-12]! */
1495         continue;
1496
1497       if ((inst & 0xfffff000) == 0xe24cb000)    /* sub fp, ip, #nn */
1498         continue;
1499
1500       if ((inst & 0xfffff000) == 0xe24dd000)    /* sub sp, sp, #nn */
1501         continue;
1502
1503       if ((inst & 0xffffc000) == 0xe54b0000     /* strb r(0123),[r11,#-nn] */
1504           || (inst & 0xffffc0f0) == 0xe14b00b0  /* strh r(0123),[r11,#-nn] */
1505           || (inst & 0xffffc000) == 0xe50b0000) /* str  r(0123),[r11,#-nn] */
1506         continue;
1507
1508       if ((inst & 0xffffc000) == 0xe5cd0000     /* strb r(0123),[sp,#nn] */
1509           || (inst & 0xffffc0f0) == 0xe1cd00b0  /* strh r(0123),[sp,#nn] */
1510           || (inst & 0xffffc000) == 0xe58d0000) /* str  r(0123),[sp,#nn] */
1511         continue;
1512
1513       /* Un-recognized instruction; stop scanning.  */
1514       break;
1515     }
1516
1517   return skip_pc;               /* End of prologue.  */
1518 }
1519
1520 /* *INDENT-OFF* */
1521 /* Function: thumb_scan_prologue (helper function for arm_scan_prologue)
1522    This function decodes a Thumb function prologue to determine:
1523      1) the size of the stack frame
1524      2) which registers are saved on it
1525      3) the offsets of saved regs
1526      4) the offset from the stack pointer to the frame pointer
1527
1528    A typical Thumb function prologue would create this stack frame
1529    (offsets relative to FP)
1530      old SP ->  24  stack parameters
1531                 20  LR
1532                 16  R7
1533      R7 ->       0  local variables (16 bytes)
1534      SP ->     -12  additional stack space (12 bytes)
1535    The frame size would thus be 36 bytes, and the frame offset would be
1536    12 bytes.  The frame register is R7.
1537    
1538    The comments for thumb_skip_prolog() describe the algorithm we use
1539    to detect the end of the prolog.  */
1540 /* *INDENT-ON* */
1541
1542 static void
1543 thumb_scan_prologue (struct gdbarch *gdbarch, CORE_ADDR prev_pc,
1544                      CORE_ADDR block_addr, struct arm_prologue_cache *cache)
1545 {
1546   CORE_ADDR prologue_start;
1547   CORE_ADDR prologue_end;
1548
1549   if (find_pc_partial_function (block_addr, NULL, &prologue_start,
1550                                 &prologue_end))
1551     {
1552       /* See comment in arm_scan_prologue for an explanation of
1553          this heuristics.  */
1554       if (prologue_end > prologue_start + 64)
1555         {
1556           prologue_end = prologue_start + 64;
1557         }
1558     }
1559   else
1560     /* We're in the boondocks: we have no idea where the start of the
1561        function is.  */
1562     return;
1563
1564   prologue_end = min (prologue_end, prev_pc);
1565
1566   thumb_analyze_prologue (gdbarch, prologue_start, prologue_end, cache);
1567 }
1568
1569 /* Return 1 if THIS_INSTR might change control flow, 0 otherwise.  */
1570
1571 static int
1572 arm_instruction_changes_pc (uint32_t this_instr)
1573 {
1574   if (bits (this_instr, 28, 31) == INST_NV)
1575     /* Unconditional instructions.  */
1576     switch (bits (this_instr, 24, 27))
1577       {
1578       case 0xa:
1579       case 0xb:
1580         /* Branch with Link and change to Thumb.  */
1581         return 1;
1582       case 0xc:
1583       case 0xd:
1584       case 0xe:
1585         /* Coprocessor register transfer.  */
1586         if (bits (this_instr, 12, 15) == 15)
1587           error (_("Invalid update to pc in instruction"));
1588         return 0;
1589       default:
1590         return 0;
1591       }
1592   else
1593     switch (bits (this_instr, 25, 27))
1594       {
1595       case 0x0:
1596         if (bits (this_instr, 23, 24) == 2 && bit (this_instr, 20) == 0)
1597           {
1598             /* Multiplies and extra load/stores.  */
1599             if (bit (this_instr, 4) == 1 && bit (this_instr, 7) == 1)
1600               /* Neither multiplies nor extension load/stores are allowed
1601                  to modify PC.  */
1602               return 0;
1603
1604             /* Otherwise, miscellaneous instructions.  */
1605
1606             /* BX <reg>, BXJ <reg>, BLX <reg> */
1607             if (bits (this_instr, 4, 27) == 0x12fff1
1608                 || bits (this_instr, 4, 27) == 0x12fff2
1609                 || bits (this_instr, 4, 27) == 0x12fff3)
1610               return 1;
1611
1612             /* Other miscellaneous instructions are unpredictable if they
1613                modify PC.  */
1614             return 0;
1615           }
1616         /* Data processing instruction.  Fall through.  */
1617
1618       case 0x1:
1619         if (bits (this_instr, 12, 15) == 15)
1620           return 1;
1621         else
1622           return 0;
1623
1624       case 0x2:
1625       case 0x3:
1626         /* Media instructions and architecturally undefined instructions.  */
1627         if (bits (this_instr, 25, 27) == 3 && bit (this_instr, 4) == 1)
1628           return 0;
1629
1630         /* Stores.  */
1631         if (bit (this_instr, 20) == 0)
1632           return 0;
1633
1634         /* Loads.  */
1635         if (bits (this_instr, 12, 15) == ARM_PC_REGNUM)
1636           return 1;
1637         else
1638           return 0;
1639
1640       case 0x4:
1641         /* Load/store multiple.  */
1642         if (bit (this_instr, 20) == 1 && bit (this_instr, 15) == 1)
1643           return 1;
1644         else
1645           return 0;
1646
1647       case 0x5:
1648         /* Branch and branch with link.  */
1649         return 1;
1650
1651       case 0x6:
1652       case 0x7:
1653         /* Coprocessor transfers or SWIs can not affect PC.  */
1654         return 0;
1655
1656       default:
1657         internal_error (__FILE__, __LINE__, _("bad value in switch"));
1658       }
1659 }
1660
1661 /* Analyze an ARM mode prologue starting at PROLOGUE_START and
1662    continuing no further than PROLOGUE_END.  If CACHE is non-NULL,
1663    fill it in.  Return the first address not recognized as a prologue
1664    instruction.
1665
1666    We recognize all the instructions typically found in ARM prologues,
1667    plus harmless instructions which can be skipped (either for analysis
1668    purposes, or a more restrictive set that can be skipped when finding
1669    the end of the prologue).  */
1670
1671 static CORE_ADDR
1672 arm_analyze_prologue (struct gdbarch *gdbarch,
1673                       CORE_ADDR prologue_start, CORE_ADDR prologue_end,
1674                       struct arm_prologue_cache *cache)
1675 {
1676   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1677   enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
1678   int regno;
1679   CORE_ADDR offset, current_pc;
1680   pv_t regs[ARM_FPS_REGNUM];
1681   struct pv_area *stack;
1682   struct cleanup *back_to;
1683   int framereg, framesize;
1684   CORE_ADDR unrecognized_pc = 0;
1685
1686   /* Search the prologue looking for instructions that set up the
1687      frame pointer, adjust the stack pointer, and save registers.
1688
1689      Be careful, however, and if it doesn't look like a prologue,
1690      don't try to scan it.  If, for instance, a frameless function
1691      begins with stmfd sp!, then we will tell ourselves there is
1692      a frame, which will confuse stack traceback, as well as "finish" 
1693      and other operations that rely on a knowledge of the stack
1694      traceback.  */
1695
1696   for (regno = 0; regno < ARM_FPS_REGNUM; regno++)
1697     regs[regno] = pv_register (regno, 0);
1698   stack = make_pv_area (ARM_SP_REGNUM, gdbarch_addr_bit (gdbarch));
1699   back_to = make_cleanup_free_pv_area (stack);
1700
1701   for (current_pc = prologue_start;
1702        current_pc < prologue_end;
1703        current_pc += 4)
1704     {
1705       unsigned int insn
1706         = read_memory_unsigned_integer (current_pc, 4, byte_order_for_code);
1707
1708       if (insn == 0xe1a0c00d)           /* mov ip, sp */
1709         {
1710           regs[ARM_IP_REGNUM] = regs[ARM_SP_REGNUM];
1711           continue;
1712         }
1713       else if ((insn & 0xfff00000) == 0xe2800000        /* add Rd, Rn, #n */
1714                && pv_is_register (regs[bits (insn, 16, 19)], ARM_SP_REGNUM))
1715         {
1716           unsigned imm = insn & 0xff;                   /* immediate value */
1717           unsigned rot = (insn & 0xf00) >> 7;           /* rotate amount */
1718           int rd = bits (insn, 12, 15);
1719           imm = (imm >> rot) | (imm << (32 - rot));
1720           regs[rd] = pv_add_constant (regs[bits (insn, 16, 19)], imm);
1721           continue;
1722         }
1723       else if ((insn & 0xfff00000) == 0xe2400000        /* sub Rd, Rn, #n */
1724                && pv_is_register (regs[bits (insn, 16, 19)], ARM_SP_REGNUM))
1725         {
1726           unsigned imm = insn & 0xff;                   /* immediate value */
1727           unsigned rot = (insn & 0xf00) >> 7;           /* rotate amount */
1728           int rd = bits (insn, 12, 15);
1729           imm = (imm >> rot) | (imm << (32 - rot));
1730           regs[rd] = pv_add_constant (regs[bits (insn, 16, 19)], -imm);
1731           continue;
1732         }
1733       else if ((insn & 0xffff0fff) == 0xe52d0004)       /* str Rd,
1734                                                            [sp, #-4]! */
1735         {
1736           if (pv_area_store_would_trash (stack, regs[ARM_SP_REGNUM]))
1737             break;
1738           regs[ARM_SP_REGNUM] = pv_add_constant (regs[ARM_SP_REGNUM], -4);
1739           pv_area_store (stack, regs[ARM_SP_REGNUM], 4,
1740                          regs[bits (insn, 12, 15)]);
1741           continue;
1742         }
1743       else if ((insn & 0xffff0000) == 0xe92d0000)
1744         /* stmfd sp!, {..., fp, ip, lr, pc}
1745            or
1746            stmfd sp!, {a1, a2, a3, a4}  */
1747         {
1748           int mask = insn & 0xffff;
1749
1750           if (pv_area_store_would_trash (stack, regs[ARM_SP_REGNUM]))
1751             break;
1752
1753           /* Calculate offsets of saved registers.  */
1754           for (regno = ARM_PC_REGNUM; regno >= 0; regno--)
1755             if (mask & (1 << regno))
1756               {
1757                 regs[ARM_SP_REGNUM]
1758                   = pv_add_constant (regs[ARM_SP_REGNUM], -4);
1759                 pv_area_store (stack, regs[ARM_SP_REGNUM], 4, regs[regno]);
1760               }
1761         }
1762       else if ((insn & 0xffff0000) == 0xe54b0000        /* strb rx,[r11,#-n] */
1763                || (insn & 0xffff00f0) == 0xe14b00b0     /* strh rx,[r11,#-n] */
1764                || (insn & 0xffffc000) == 0xe50b0000)    /* str  rx,[r11,#-n] */
1765         {
1766           /* No need to add this to saved_regs -- it's just an arg reg.  */
1767           continue;
1768         }
1769       else if ((insn & 0xffff0000) == 0xe5cd0000        /* strb rx,[sp,#n] */
1770                || (insn & 0xffff00f0) == 0xe1cd00b0     /* strh rx,[sp,#n] */
1771                || (insn & 0xffffc000) == 0xe58d0000)    /* str  rx,[sp,#n] */
1772         {
1773           /* No need to add this to saved_regs -- it's just an arg reg.  */
1774           continue;
1775         }
1776       else if ((insn & 0xfff00000) == 0xe8800000        /* stm Rn,
1777                                                            { registers } */
1778                && pv_is_register (regs[bits (insn, 16, 19)], ARM_SP_REGNUM))
1779         {
1780           /* No need to add this to saved_regs -- it's just arg regs.  */
1781           continue;
1782         }
1783       else if ((insn & 0xfffff000) == 0xe24cb000)       /* sub fp, ip #n */
1784         {
1785           unsigned imm = insn & 0xff;                   /* immediate value */
1786           unsigned rot = (insn & 0xf00) >> 7;           /* rotate amount */
1787           imm = (imm >> rot) | (imm << (32 - rot));
1788           regs[ARM_FP_REGNUM] = pv_add_constant (regs[ARM_IP_REGNUM], -imm);
1789         }
1790       else if ((insn & 0xfffff000) == 0xe24dd000)       /* sub sp, sp #n */
1791         {
1792           unsigned imm = insn & 0xff;                   /* immediate value */
1793           unsigned rot = (insn & 0xf00) >> 7;           /* rotate amount */
1794           imm = (imm >> rot) | (imm << (32 - rot));
1795           regs[ARM_SP_REGNUM] = pv_add_constant (regs[ARM_SP_REGNUM], -imm);
1796         }
1797       else if ((insn & 0xffff7fff) == 0xed6d0103        /* stfe f?,
1798                                                            [sp, -#c]! */
1799                && gdbarch_tdep (gdbarch)->have_fpa_registers)
1800         {
1801           if (pv_area_store_would_trash (stack, regs[ARM_SP_REGNUM]))
1802             break;
1803
1804           regs[ARM_SP_REGNUM] = pv_add_constant (regs[ARM_SP_REGNUM], -12);
1805           regno = ARM_F0_REGNUM + ((insn >> 12) & 0x07);
1806           pv_area_store (stack, regs[ARM_SP_REGNUM], 12, regs[regno]);
1807         }
1808       else if ((insn & 0xffbf0fff) == 0xec2d0200        /* sfmfd f0, 4,
1809                                                            [sp!] */
1810                && gdbarch_tdep (gdbarch)->have_fpa_registers)
1811         {
1812           int n_saved_fp_regs;
1813           unsigned int fp_start_reg, fp_bound_reg;
1814
1815           if (pv_area_store_would_trash (stack, regs[ARM_SP_REGNUM]))
1816             break;
1817
1818           if ((insn & 0x800) == 0x800)          /* N0 is set */
1819             {
1820               if ((insn & 0x40000) == 0x40000)  /* N1 is set */
1821                 n_saved_fp_regs = 3;
1822               else
1823                 n_saved_fp_regs = 1;
1824             }
1825           else
1826             {
1827               if ((insn & 0x40000) == 0x40000)  /* N1 is set */
1828                 n_saved_fp_regs = 2;
1829               else
1830                 n_saved_fp_regs = 4;
1831             }
1832
1833           fp_start_reg = ARM_F0_REGNUM + ((insn >> 12) & 0x7);
1834           fp_bound_reg = fp_start_reg + n_saved_fp_regs;
1835           for (; fp_start_reg < fp_bound_reg; fp_start_reg++)
1836             {
1837               regs[ARM_SP_REGNUM] = pv_add_constant (regs[ARM_SP_REGNUM], -12);
1838               pv_area_store (stack, regs[ARM_SP_REGNUM], 12,
1839                              regs[fp_start_reg++]);
1840             }
1841         }
1842       else if ((insn & 0xff000000) == 0xeb000000 && cache == NULL) /* bl */
1843         {
1844           /* Allow some special function calls when skipping the
1845              prologue; GCC generates these before storing arguments to
1846              the stack.  */
1847           CORE_ADDR dest = BranchDest (current_pc, insn);
1848
1849           if (skip_prologue_function (gdbarch, dest, 0))
1850             continue;
1851           else
1852             break;
1853         }
1854       else if ((insn & 0xf0000000) != 0xe0000000)
1855         break;                  /* Condition not true, exit early.  */
1856       else if (arm_instruction_changes_pc (insn))
1857         /* Don't scan past anything that might change control flow.  */
1858         break;
1859       else if ((insn & 0xfe500000) == 0xe8100000        /* ldm */
1860                && pv_is_register (regs[bits (insn, 16, 19)], ARM_SP_REGNUM))
1861         /* Ignore block loads from the stack, potentially copying
1862            parameters from memory.  */
1863         continue;
1864       else if ((insn & 0xfc500000) == 0xe4100000
1865                && pv_is_register (regs[bits (insn, 16, 19)], ARM_SP_REGNUM))
1866         /* Similarly ignore single loads from the stack.  */
1867         continue;
1868       else if ((insn & 0xffff0ff0) == 0xe1a00000)
1869         /* MOV Rd, Rm.  Skip register copies, i.e. saves to another
1870            register instead of the stack.  */
1871         continue;
1872       else
1873         {
1874           /* The optimizer might shove anything into the prologue,
1875              so we just skip what we don't recognize.  */
1876           unrecognized_pc = current_pc;
1877           continue;
1878         }
1879     }
1880
1881   if (unrecognized_pc == 0)
1882     unrecognized_pc = current_pc;
1883
1884   /* The frame size is just the distance from the frame register
1885      to the original stack pointer.  */
1886   if (pv_is_register (regs[ARM_FP_REGNUM], ARM_SP_REGNUM))
1887     {
1888       /* Frame pointer is fp.  */
1889       framereg = ARM_FP_REGNUM;
1890       framesize = -regs[ARM_FP_REGNUM].k;
1891     }
1892   else
1893     {
1894       /* Try the stack pointer... this is a bit desperate.  */
1895       framereg = ARM_SP_REGNUM;
1896       framesize = -regs[ARM_SP_REGNUM].k;
1897     }
1898
1899   if (cache)
1900     {
1901       cache->framereg = framereg;
1902       cache->framesize = framesize;
1903
1904       for (regno = 0; regno < ARM_FPS_REGNUM; regno++)
1905         if (pv_area_find_reg (stack, gdbarch, regno, &offset))
1906           cache->saved_regs[regno].addr = offset;
1907     }
1908
1909   if (arm_debug)
1910     fprintf_unfiltered (gdb_stdlog, "Prologue scan stopped at %s\n",
1911                         paddress (gdbarch, unrecognized_pc));
1912
1913   do_cleanups (back_to);
1914   return unrecognized_pc;
1915 }
1916
1917 static void
1918 arm_scan_prologue (struct frame_info *this_frame,
1919                    struct arm_prologue_cache *cache)
1920 {
1921   struct gdbarch *gdbarch = get_frame_arch (this_frame);
1922   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1923   int regno;
1924   CORE_ADDR prologue_start, prologue_end, current_pc;
1925   CORE_ADDR prev_pc = get_frame_pc (this_frame);
1926   CORE_ADDR block_addr = get_frame_address_in_block (this_frame);
1927   pv_t regs[ARM_FPS_REGNUM];
1928   struct pv_area *stack;
1929   struct cleanup *back_to;
1930   CORE_ADDR offset;
1931
1932   /* Assume there is no frame until proven otherwise.  */
1933   cache->framereg = ARM_SP_REGNUM;
1934   cache->framesize = 0;
1935
1936   /* Check for Thumb prologue.  */
1937   if (arm_frame_is_thumb (this_frame))
1938     {
1939       thumb_scan_prologue (gdbarch, prev_pc, block_addr, cache);
1940       return;
1941     }
1942
1943   /* Find the function prologue.  If we can't find the function in
1944      the symbol table, peek in the stack frame to find the PC.  */
1945   if (find_pc_partial_function (block_addr, NULL, &prologue_start,
1946                                 &prologue_end))
1947     {
1948       /* One way to find the end of the prologue (which works well
1949          for unoptimized code) is to do the following:
1950
1951             struct symtab_and_line sal = find_pc_line (prologue_start, 0);
1952
1953             if (sal.line == 0)
1954               prologue_end = prev_pc;
1955             else if (sal.end < prologue_end)
1956               prologue_end = sal.end;
1957
1958          This mechanism is very accurate so long as the optimizer
1959          doesn't move any instructions from the function body into the
1960          prologue.  If this happens, sal.end will be the last
1961          instruction in the first hunk of prologue code just before
1962          the first instruction that the scheduler has moved from
1963          the body to the prologue.
1964
1965          In order to make sure that we scan all of the prologue
1966          instructions, we use a slightly less accurate mechanism which
1967          may scan more than necessary.  To help compensate for this
1968          lack of accuracy, the prologue scanning loop below contains
1969          several clauses which'll cause the loop to terminate early if
1970          an implausible prologue instruction is encountered.
1971
1972          The expression
1973
1974               prologue_start + 64
1975
1976          is a suitable endpoint since it accounts for the largest
1977          possible prologue plus up to five instructions inserted by
1978          the scheduler.  */
1979
1980       if (prologue_end > prologue_start + 64)
1981         {
1982           prologue_end = prologue_start + 64;   /* See above.  */
1983         }
1984     }
1985   else
1986     {
1987       /* We have no symbol information.  Our only option is to assume this
1988          function has a standard stack frame and the normal frame register.
1989          Then, we can find the value of our frame pointer on entrance to
1990          the callee (or at the present moment if this is the innermost frame).
1991          The value stored there should be the address of the stmfd + 8.  */
1992       CORE_ADDR frame_loc;
1993       LONGEST return_value;
1994
1995       frame_loc = get_frame_register_unsigned (this_frame, ARM_FP_REGNUM);
1996       if (!safe_read_memory_integer (frame_loc, 4, byte_order, &return_value))
1997         return;
1998       else
1999         {
2000           prologue_start = gdbarch_addr_bits_remove
2001                              (gdbarch, return_value) - 8;
2002           prologue_end = prologue_start + 64;   /* See above.  */
2003         }
2004     }
2005
2006   if (prev_pc < prologue_end)
2007     prologue_end = prev_pc;
2008
2009   arm_analyze_prologue (gdbarch, prologue_start, prologue_end, cache);
2010 }
2011
2012 static struct arm_prologue_cache *
2013 arm_make_prologue_cache (struct frame_info *this_frame)
2014 {
2015   int reg;
2016   struct arm_prologue_cache *cache;
2017   CORE_ADDR unwound_fp;
2018
2019   cache = FRAME_OBSTACK_ZALLOC (struct arm_prologue_cache);
2020   cache->saved_regs = trad_frame_alloc_saved_regs (this_frame);
2021
2022   arm_scan_prologue (this_frame, cache);
2023
2024   unwound_fp = get_frame_register_unsigned (this_frame, cache->framereg);
2025   if (unwound_fp == 0)
2026     return cache;
2027
2028   cache->prev_sp = unwound_fp + cache->framesize;
2029
2030   /* Calculate actual addresses of saved registers using offsets
2031      determined by arm_scan_prologue.  */
2032   for (reg = 0; reg < gdbarch_num_regs (get_frame_arch (this_frame)); reg++)
2033     if (trad_frame_addr_p (cache->saved_regs, reg))
2034       cache->saved_regs[reg].addr += cache->prev_sp;
2035
2036   return cache;
2037 }
2038
2039 /* Our frame ID for a normal frame is the current function's starting PC
2040    and the caller's SP when we were called.  */
2041
2042 static void
2043 arm_prologue_this_id (struct frame_info *this_frame,
2044                       void **this_cache,
2045                       struct frame_id *this_id)
2046 {
2047   struct arm_prologue_cache *cache;
2048   struct frame_id id;
2049   CORE_ADDR pc, func;
2050
2051   if (*this_cache == NULL)
2052     *this_cache = arm_make_prologue_cache (this_frame);
2053   cache = *this_cache;
2054
2055   /* This is meant to halt the backtrace at "_start".  */
2056   pc = get_frame_pc (this_frame);
2057   if (pc <= gdbarch_tdep (get_frame_arch (this_frame))->lowest_pc)
2058     return;
2059
2060   /* If we've hit a wall, stop.  */
2061   if (cache->prev_sp == 0)
2062     return;
2063
2064   /* Use function start address as part of the frame ID.  If we cannot
2065      identify the start address (due to missing symbol information),
2066      fall back to just using the current PC.  */
2067   func = get_frame_func (this_frame);
2068   if (!func)
2069     func = pc;
2070
2071   id = frame_id_build (cache->prev_sp, func);
2072   *this_id = id;
2073 }
2074
2075 static struct value *
2076 arm_prologue_prev_register (struct frame_info *this_frame,
2077                             void **this_cache,
2078                             int prev_regnum)
2079 {
2080   struct gdbarch *gdbarch = get_frame_arch (this_frame);
2081   struct arm_prologue_cache *cache;
2082
2083   if (*this_cache == NULL)
2084     *this_cache = arm_make_prologue_cache (this_frame);
2085   cache = *this_cache;
2086
2087   /* If we are asked to unwind the PC, then we need to return the LR
2088      instead.  The prologue may save PC, but it will point into this
2089      frame's prologue, not the next frame's resume location.  Also
2090      strip the saved T bit.  A valid LR may have the low bit set, but
2091      a valid PC never does.  */
2092   if (prev_regnum == ARM_PC_REGNUM)
2093     {
2094       CORE_ADDR lr;
2095
2096       lr = frame_unwind_register_unsigned (this_frame, ARM_LR_REGNUM);
2097       return frame_unwind_got_constant (this_frame, prev_regnum,
2098                                         arm_addr_bits_remove (gdbarch, lr));
2099     }
2100
2101   /* SP is generally not saved to the stack, but this frame is
2102      identified by the next frame's stack pointer at the time of the call.
2103      The value was already reconstructed into PREV_SP.  */
2104   if (prev_regnum == ARM_SP_REGNUM)
2105     return frame_unwind_got_constant (this_frame, prev_regnum, cache->prev_sp);
2106
2107   /* The CPSR may have been changed by the call instruction and by the
2108      called function.  The only bit we can reconstruct is the T bit,
2109      by checking the low bit of LR as of the call.  This is a reliable
2110      indicator of Thumb-ness except for some ARM v4T pre-interworking
2111      Thumb code, which could get away with a clear low bit as long as
2112      the called function did not use bx.  Guess that all other
2113      bits are unchanged; the condition flags are presumably lost,
2114      but the processor status is likely valid.  */
2115   if (prev_regnum == ARM_PS_REGNUM)
2116     {
2117       CORE_ADDR lr, cpsr;
2118       ULONGEST t_bit = arm_psr_thumb_bit (gdbarch);
2119
2120       cpsr = get_frame_register_unsigned (this_frame, prev_regnum);
2121       lr = frame_unwind_register_unsigned (this_frame, ARM_LR_REGNUM);
2122       if (IS_THUMB_ADDR (lr))
2123         cpsr |= t_bit;
2124       else
2125         cpsr &= ~t_bit;
2126       return frame_unwind_got_constant (this_frame, prev_regnum, cpsr);
2127     }
2128
2129   return trad_frame_get_prev_register (this_frame, cache->saved_regs,
2130                                        prev_regnum);
2131 }
2132
2133 struct frame_unwind arm_prologue_unwind = {
2134   NORMAL_FRAME,
2135   default_frame_unwind_stop_reason,
2136   arm_prologue_this_id,
2137   arm_prologue_prev_register,
2138   NULL,
2139   default_frame_sniffer
2140 };
2141
2142 /* Maintain a list of ARM exception table entries per objfile, similar to the
2143    list of mapping symbols.  We only cache entries for standard ARM-defined
2144    personality routines; the cache will contain only the frame unwinding
2145    instructions associated with the entry (not the descriptors).  */
2146
2147 static const struct objfile_data *arm_exidx_data_key;
2148
2149 struct arm_exidx_entry
2150 {
2151   bfd_vma addr;
2152   gdb_byte *entry;
2153 };
2154 typedef struct arm_exidx_entry arm_exidx_entry_s;
2155 DEF_VEC_O(arm_exidx_entry_s);
2156
2157 struct arm_exidx_data
2158 {
2159   VEC(arm_exidx_entry_s) **section_maps;
2160 };
2161
2162 static void
2163 arm_exidx_data_free (struct objfile *objfile, void *arg)
2164 {
2165   struct arm_exidx_data *data = arg;
2166   unsigned int i;
2167
2168   for (i = 0; i < objfile->obfd->section_count; i++)
2169     VEC_free (arm_exidx_entry_s, data->section_maps[i]);
2170 }
2171
2172 static inline int
2173 arm_compare_exidx_entries (const struct arm_exidx_entry *lhs,
2174                            const struct arm_exidx_entry *rhs)
2175 {
2176   return lhs->addr < rhs->addr;
2177 }
2178
2179 static struct obj_section *
2180 arm_obj_section_from_vma (struct objfile *objfile, bfd_vma vma)
2181 {
2182   struct obj_section *osect;
2183
2184   ALL_OBJFILE_OSECTIONS (objfile, osect)
2185     if (bfd_get_section_flags (objfile->obfd,
2186                                osect->the_bfd_section) & SEC_ALLOC)
2187       {
2188         bfd_vma start, size;
2189         start = bfd_get_section_vma (objfile->obfd, osect->the_bfd_section);
2190         size = bfd_get_section_size (osect->the_bfd_section);
2191
2192         if (start <= vma && vma < start + size)
2193           return osect;
2194       }
2195
2196   return NULL;
2197 }
2198
2199 /* Parse contents of exception table and exception index sections
2200    of OBJFILE, and fill in the exception table entry cache.
2201
2202    For each entry that refers to a standard ARM-defined personality
2203    routine, extract the frame unwinding instructions (from either
2204    the index or the table section).  The unwinding instructions
2205    are normalized by:
2206     - extracting them from the rest of the table data
2207     - converting to host endianness
2208     - appending the implicit 0xb0 ("Finish") code
2209
2210    The extracted and normalized instructions are stored for later
2211    retrieval by the arm_find_exidx_entry routine.  */
2212  
2213 static void
2214 arm_exidx_new_objfile (struct objfile *objfile)
2215 {
2216   struct cleanup *cleanups;
2217   struct arm_exidx_data *data;
2218   asection *exidx, *extab;
2219   bfd_vma exidx_vma = 0, extab_vma = 0;
2220   bfd_size_type exidx_size = 0, extab_size = 0;
2221   gdb_byte *exidx_data = NULL, *extab_data = NULL;
2222   LONGEST i;
2223
2224   /* If we've already touched this file, do nothing.  */
2225   if (!objfile || objfile_data (objfile, arm_exidx_data_key) != NULL)
2226     return;
2227   cleanups = make_cleanup (null_cleanup, NULL);
2228
2229   /* Read contents of exception table and index.  */
2230   exidx = bfd_get_section_by_name (objfile->obfd, ".ARM.exidx");
2231   if (exidx)
2232     {
2233       exidx_vma = bfd_section_vma (objfile->obfd, exidx);
2234       exidx_size = bfd_get_section_size (exidx);
2235       exidx_data = xmalloc (exidx_size);
2236       make_cleanup (xfree, exidx_data);
2237
2238       if (!bfd_get_section_contents (objfile->obfd, exidx,
2239                                      exidx_data, 0, exidx_size))
2240         {
2241           do_cleanups (cleanups);
2242           return;
2243         }
2244     }
2245
2246   extab = bfd_get_section_by_name (objfile->obfd, ".ARM.extab");
2247   if (extab)
2248     {
2249       extab_vma = bfd_section_vma (objfile->obfd, extab);
2250       extab_size = bfd_get_section_size (extab);
2251       extab_data = xmalloc (extab_size);
2252       make_cleanup (xfree, extab_data);
2253
2254       if (!bfd_get_section_contents (objfile->obfd, extab,
2255                                      extab_data, 0, extab_size))
2256         {
2257           do_cleanups (cleanups);
2258           return;
2259         }
2260     }
2261
2262   /* Allocate exception table data structure.  */
2263   data = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct arm_exidx_data);
2264   set_objfile_data (objfile, arm_exidx_data_key, data);
2265   data->section_maps = OBSTACK_CALLOC (&objfile->objfile_obstack,
2266                                        objfile->obfd->section_count,
2267                                        VEC(arm_exidx_entry_s) *);
2268
2269   /* Fill in exception table.  */
2270   for (i = 0; i < exidx_size / 8; i++)
2271     {
2272       struct arm_exidx_entry new_exidx_entry;
2273       bfd_vma idx = bfd_h_get_32 (objfile->obfd, exidx_data + i * 8);
2274       bfd_vma val = bfd_h_get_32 (objfile->obfd, exidx_data + i * 8 + 4);
2275       bfd_vma addr = 0, word = 0;
2276       int n_bytes = 0, n_words = 0;
2277       struct obj_section *sec;
2278       gdb_byte *entry = NULL;
2279
2280       /* Extract address of start of function.  */
2281       idx = ((idx & 0x7fffffff) ^ 0x40000000) - 0x40000000;
2282       idx += exidx_vma + i * 8;
2283
2284       /* Find section containing function and compute section offset.  */
2285       sec = arm_obj_section_from_vma (objfile, idx);
2286       if (sec == NULL)
2287         continue;
2288       idx -= bfd_get_section_vma (objfile->obfd, sec->the_bfd_section);
2289
2290       /* Determine address of exception table entry.  */
2291       if (val == 1)
2292         {
2293           /* EXIDX_CANTUNWIND -- no exception table entry present.  */
2294         }
2295       else if ((val & 0xff000000) == 0x80000000)
2296         {
2297           /* Exception table entry embedded in .ARM.exidx
2298              -- must be short form.  */
2299           word = val;
2300           n_bytes = 3;
2301         }
2302       else if (!(val & 0x80000000))
2303         {
2304           /* Exception table entry in .ARM.extab.  */
2305           addr = ((val & 0x7fffffff) ^ 0x40000000) - 0x40000000;
2306           addr += exidx_vma + i * 8 + 4;
2307
2308           if (addr >= extab_vma && addr + 4 <= extab_vma + extab_size)
2309             {
2310               word = bfd_h_get_32 (objfile->obfd,
2311                                    extab_data + addr - extab_vma);
2312               addr += 4;
2313
2314               if ((word & 0xff000000) == 0x80000000)
2315                 {
2316                   /* Short form.  */
2317                   n_bytes = 3;
2318                 }
2319               else if ((word & 0xff000000) == 0x81000000
2320                        || (word & 0xff000000) == 0x82000000)
2321                 {
2322                   /* Long form.  */
2323                   n_bytes = 2;
2324                   n_words = ((word >> 16) & 0xff);
2325                 }
2326               else if (!(word & 0x80000000))
2327                 {
2328                   bfd_vma pers;
2329                   struct obj_section *pers_sec;
2330                   int gnu_personality = 0;
2331
2332                   /* Custom personality routine.  */
2333                   pers = ((word & 0x7fffffff) ^ 0x40000000) - 0x40000000;
2334                   pers = UNMAKE_THUMB_ADDR (pers + addr - 4);
2335
2336                   /* Check whether we've got one of the variants of the
2337                      GNU personality routines.  */
2338                   pers_sec = arm_obj_section_from_vma (objfile, pers);
2339                   if (pers_sec)
2340                     {
2341                       static const char *personality[] = 
2342                         {
2343                           "__gcc_personality_v0",
2344                           "__gxx_personality_v0",
2345                           "__gcj_personality_v0",
2346                           "__gnu_objc_personality_v0",
2347                           NULL
2348                         };
2349
2350                       CORE_ADDR pc = pers + obj_section_offset (pers_sec);
2351                       int k;
2352
2353                       for (k = 0; personality[k]; k++)
2354                         if (lookup_minimal_symbol_by_pc_name
2355                               (pc, personality[k], objfile))
2356                           {
2357                             gnu_personality = 1;
2358                             break;
2359                           }
2360                     }
2361
2362                   /* If so, the next word contains a word count in the high
2363                      byte, followed by the same unwind instructions as the
2364                      pre-defined forms.  */
2365                   if (gnu_personality
2366                       && addr + 4 <= extab_vma + extab_size)
2367                     {
2368                       word = bfd_h_get_32 (objfile->obfd,
2369                                            extab_data + addr - extab_vma);
2370                       addr += 4;
2371                       n_bytes = 3;
2372                       n_words = ((word >> 24) & 0xff);
2373                     }
2374                 }
2375             }
2376         }
2377
2378       /* Sanity check address.  */
2379       if (n_words)
2380         if (addr < extab_vma || addr + 4 * n_words > extab_vma + extab_size)
2381           n_words = n_bytes = 0;
2382
2383       /* The unwind instructions reside in WORD (only the N_BYTES least
2384          significant bytes are valid), followed by N_WORDS words in the
2385          extab section starting at ADDR.  */
2386       if (n_bytes || n_words)
2387         {
2388           gdb_byte *p = entry = obstack_alloc (&objfile->objfile_obstack,
2389                                                n_bytes + n_words * 4 + 1);
2390
2391           while (n_bytes--)
2392             *p++ = (gdb_byte) ((word >> (8 * n_bytes)) & 0xff);
2393
2394           while (n_words--)
2395             {
2396               word = bfd_h_get_32 (objfile->obfd,
2397                                    extab_data + addr - extab_vma);
2398               addr += 4;
2399
2400               *p++ = (gdb_byte) ((word >> 24) & 0xff);
2401               *p++ = (gdb_byte) ((word >> 16) & 0xff);
2402               *p++ = (gdb_byte) ((word >> 8) & 0xff);
2403               *p++ = (gdb_byte) (word & 0xff);
2404             }
2405
2406           /* Implied "Finish" to terminate the list.  */
2407           *p++ = 0xb0;
2408         }
2409
2410       /* Push entry onto vector.  They are guaranteed to always
2411          appear in order of increasing addresses.  */
2412       new_exidx_entry.addr = idx;
2413       new_exidx_entry.entry = entry;
2414       VEC_safe_push (arm_exidx_entry_s,
2415                      data->section_maps[sec->the_bfd_section->index],
2416                      &new_exidx_entry);
2417     }
2418
2419   do_cleanups (cleanups);
2420 }
2421
2422 /* Search for the exception table entry covering MEMADDR.  If one is found,
2423    return a pointer to its data.  Otherwise, return 0.  If START is non-NULL,
2424    set *START to the start of the region covered by this entry.  */
2425
2426 static gdb_byte *
2427 arm_find_exidx_entry (CORE_ADDR memaddr, CORE_ADDR *start)
2428 {
2429   struct obj_section *sec;
2430
2431   sec = find_pc_section (memaddr);
2432   if (sec != NULL)
2433     {
2434       struct arm_exidx_data *data;
2435       VEC(arm_exidx_entry_s) *map;
2436       struct arm_exidx_entry map_key = { memaddr - obj_section_addr (sec), 0 };
2437       unsigned int idx;
2438
2439       data = objfile_data (sec->objfile, arm_exidx_data_key);
2440       if (data != NULL)
2441         {
2442           map = data->section_maps[sec->the_bfd_section->index];
2443           if (!VEC_empty (arm_exidx_entry_s, map))
2444             {
2445               struct arm_exidx_entry *map_sym;
2446
2447               idx = VEC_lower_bound (arm_exidx_entry_s, map, &map_key,
2448                                      arm_compare_exidx_entries);
2449
2450               /* VEC_lower_bound finds the earliest ordered insertion
2451                  point.  If the following symbol starts at this exact
2452                  address, we use that; otherwise, the preceding
2453                  exception table entry covers this address.  */
2454               if (idx < VEC_length (arm_exidx_entry_s, map))
2455                 {
2456                   map_sym = VEC_index (arm_exidx_entry_s, map, idx);
2457                   if (map_sym->addr == map_key.addr)
2458                     {
2459                       if (start)
2460                         *start = map_sym->addr + obj_section_addr (sec);
2461                       return map_sym->entry;
2462                     }
2463                 }
2464
2465               if (idx > 0)
2466                 {
2467                   map_sym = VEC_index (arm_exidx_entry_s, map, idx - 1);
2468                   if (start)
2469                     *start = map_sym->addr + obj_section_addr (sec);
2470                   return map_sym->entry;
2471                 }
2472             }
2473         }
2474     }
2475
2476   return NULL;
2477 }
2478
2479 /* Given the current frame THIS_FRAME, and its associated frame unwinding
2480    instruction list from the ARM exception table entry ENTRY, allocate and
2481    return a prologue cache structure describing how to unwind this frame.
2482
2483    Return NULL if the unwinding instruction list contains a "spare",
2484    "reserved" or "refuse to unwind" instruction as defined in section
2485    "9.3 Frame unwinding instructions" of the "Exception Handling ABI
2486    for the ARM Architecture" document.  */
2487
2488 static struct arm_prologue_cache *
2489 arm_exidx_fill_cache (struct frame_info *this_frame, gdb_byte *entry)
2490 {
2491   CORE_ADDR vsp = 0;
2492   int vsp_valid = 0;
2493
2494   struct arm_prologue_cache *cache;
2495   cache = FRAME_OBSTACK_ZALLOC (struct arm_prologue_cache);
2496   cache->saved_regs = trad_frame_alloc_saved_regs (this_frame);
2497
2498   for (;;)
2499     {
2500       gdb_byte insn;
2501
2502       /* Whenever we reload SP, we actually have to retrieve its
2503          actual value in the current frame.  */
2504       if (!vsp_valid)
2505         {
2506           if (trad_frame_realreg_p (cache->saved_regs, ARM_SP_REGNUM))
2507             {
2508               int reg = cache->saved_regs[ARM_SP_REGNUM].realreg;
2509               vsp = get_frame_register_unsigned (this_frame, reg);
2510             }
2511           else
2512             {
2513               CORE_ADDR addr = cache->saved_regs[ARM_SP_REGNUM].addr;
2514               vsp = get_frame_memory_unsigned (this_frame, addr, 4);
2515             }
2516
2517           vsp_valid = 1;
2518         }
2519
2520       /* Decode next unwind instruction.  */
2521       insn = *entry++;
2522
2523       if ((insn & 0xc0) == 0)
2524         {
2525           int offset = insn & 0x3f;
2526           vsp += (offset << 2) + 4;
2527         }
2528       else if ((insn & 0xc0) == 0x40)
2529         {
2530           int offset = insn & 0x3f;
2531           vsp -= (offset << 2) + 4;
2532         }
2533       else if ((insn & 0xf0) == 0x80)
2534         {
2535           int mask = ((insn & 0xf) << 8) | *entry++;
2536           int i;
2537
2538           /* The special case of an all-zero mask identifies
2539              "Refuse to unwind".  We return NULL to fall back
2540              to the prologue analyzer.  */
2541           if (mask == 0)
2542             return NULL;
2543
2544           /* Pop registers r4..r15 under mask.  */
2545           for (i = 0; i < 12; i++)
2546             if (mask & (1 << i))
2547               {
2548                 cache->saved_regs[4 + i].addr = vsp;
2549                 vsp += 4;
2550               }
2551
2552           /* Special-case popping SP -- we need to reload vsp.  */
2553           if (mask & (1 << (ARM_SP_REGNUM - 4)))
2554             vsp_valid = 0;
2555         }
2556       else if ((insn & 0xf0) == 0x90)
2557         {
2558           int reg = insn & 0xf;
2559
2560           /* Reserved cases.  */
2561           if (reg == ARM_SP_REGNUM || reg == ARM_PC_REGNUM)
2562             return NULL;
2563
2564           /* Set SP from another register and mark VSP for reload.  */
2565           cache->saved_regs[ARM_SP_REGNUM] = cache->saved_regs[reg];
2566           vsp_valid = 0;
2567         }
2568       else if ((insn & 0xf0) == 0xa0)
2569         {
2570           int count = insn & 0x7;
2571           int pop_lr = (insn & 0x8) != 0;
2572           int i;
2573
2574           /* Pop r4..r[4+count].  */
2575           for (i = 0; i <= count; i++)
2576             {
2577               cache->saved_regs[4 + i].addr = vsp;
2578               vsp += 4;
2579             }
2580
2581           /* If indicated by flag, pop LR as well.  */
2582           if (pop_lr)
2583             {
2584               cache->saved_regs[ARM_LR_REGNUM].addr = vsp;
2585               vsp += 4;
2586             }
2587         }
2588       else if (insn == 0xb0)
2589         {
2590           /* We could only have updated PC by popping into it; if so, it
2591              will show up as address.  Otherwise, copy LR into PC.  */
2592           if (!trad_frame_addr_p (cache->saved_regs, ARM_PC_REGNUM))
2593             cache->saved_regs[ARM_PC_REGNUM]
2594               = cache->saved_regs[ARM_LR_REGNUM];
2595
2596           /* We're done.  */
2597           break;
2598         }
2599       else if (insn == 0xb1)
2600         {
2601           int mask = *entry++;
2602           int i;
2603
2604           /* All-zero mask and mask >= 16 is "spare".  */
2605           if (mask == 0 || mask >= 16)
2606             return NULL;
2607
2608           /* Pop r0..r3 under mask.  */
2609           for (i = 0; i < 4; i++)
2610             if (mask & (1 << i))
2611               {
2612                 cache->saved_regs[i].addr = vsp;
2613                 vsp += 4;
2614               }
2615         }
2616       else if (insn == 0xb2)
2617         {
2618           ULONGEST offset = 0;
2619           unsigned shift = 0;
2620
2621           do
2622             {
2623               offset |= (*entry & 0x7f) << shift;
2624               shift += 7;
2625             }
2626           while (*entry++ & 0x80);
2627
2628           vsp += 0x204 + (offset << 2);
2629         }
2630       else if (insn == 0xb3)
2631         {
2632           int start = *entry >> 4;
2633           int count = (*entry++) & 0xf;
2634           int i;
2635
2636           /* Only registers D0..D15 are valid here.  */
2637           if (start + count >= 16)
2638             return NULL;
2639
2640           /* Pop VFP double-precision registers D[start]..D[start+count].  */
2641           for (i = 0; i <= count; i++)
2642             {
2643               cache->saved_regs[ARM_D0_REGNUM + start + i].addr = vsp;
2644               vsp += 8;
2645             }
2646
2647           /* Add an extra 4 bytes for FSTMFDX-style stack.  */
2648           vsp += 4;
2649         }
2650       else if ((insn & 0xf8) == 0xb8)
2651         {
2652           int count = insn & 0x7;
2653           int i;
2654
2655           /* Pop VFP double-precision registers D[8]..D[8+count].  */
2656           for (i = 0; i <= count; i++)
2657             {
2658               cache->saved_regs[ARM_D0_REGNUM + 8 + i].addr = vsp;
2659               vsp += 8;
2660             }
2661
2662           /* Add an extra 4 bytes for FSTMFDX-style stack.  */
2663           vsp += 4;
2664         }
2665       else if (insn == 0xc6)
2666         {
2667           int start = *entry >> 4;
2668           int count = (*entry++) & 0xf;
2669           int i;
2670
2671           /* Only registers WR0..WR15 are valid.  */
2672           if (start + count >= 16)
2673             return NULL;
2674
2675           /* Pop iwmmx registers WR[start]..WR[start+count].  */
2676           for (i = 0; i <= count; i++)
2677             {
2678               cache->saved_regs[ARM_WR0_REGNUM + start + i].addr = vsp;
2679               vsp += 8;
2680             }
2681         }
2682       else if (insn == 0xc7)
2683         {
2684           int mask = *entry++;
2685           int i;
2686
2687           /* All-zero mask and mask >= 16 is "spare".  */
2688           if (mask == 0 || mask >= 16)
2689             return NULL;
2690
2691           /* Pop iwmmx general-purpose registers WCGR0..WCGR3 under mask.  */
2692           for (i = 0; i < 4; i++)
2693             if (mask & (1 << i))
2694               {
2695                 cache->saved_regs[ARM_WCGR0_REGNUM + i].addr = vsp;
2696                 vsp += 4;
2697               }
2698         }
2699       else if ((insn & 0xf8) == 0xc0)
2700         {
2701           int count = insn & 0x7;
2702           int i;
2703
2704           /* Pop iwmmx registers WR[10]..WR[10+count].  */
2705           for (i = 0; i <= count; i++)
2706             {
2707               cache->saved_regs[ARM_WR0_REGNUM + 10 + i].addr = vsp;
2708               vsp += 8;
2709             }
2710         }
2711       else if (insn == 0xc8)
2712         {
2713           int start = *entry >> 4;
2714           int count = (*entry++) & 0xf;
2715           int i;
2716
2717           /* Only registers D0..D31 are valid.  */
2718           if (start + count >= 16)
2719             return NULL;
2720
2721           /* Pop VFP double-precision registers
2722              D[16+start]..D[16+start+count].  */
2723           for (i = 0; i <= count; i++)
2724             {
2725               cache->saved_regs[ARM_D0_REGNUM + 16 + start + i].addr = vsp;
2726               vsp += 8;
2727             }
2728         }
2729       else if (insn == 0xc9)
2730         {
2731           int start = *entry >> 4;
2732           int count = (*entry++) & 0xf;
2733           int i;
2734
2735           /* Pop VFP double-precision registers D[start]..D[start+count].  */
2736           for (i = 0; i <= count; i++)
2737             {
2738               cache->saved_regs[ARM_D0_REGNUM + start + i].addr = vsp;
2739               vsp += 8;
2740             }
2741         }
2742       else if ((insn & 0xf8) == 0xd0)
2743         {
2744           int count = insn & 0x7;
2745           int i;
2746
2747           /* Pop VFP double-precision registers D[8]..D[8+count].  */
2748           for (i = 0; i <= count; i++)
2749             {
2750               cache->saved_regs[ARM_D0_REGNUM + 8 + i].addr = vsp;
2751               vsp += 8;
2752             }
2753         }
2754       else
2755         {
2756           /* Everything else is "spare".  */
2757           return NULL;
2758         }
2759     }
2760
2761   /* If we restore SP from a register, assume this was the frame register.
2762      Otherwise just fall back to SP as frame register.  */
2763   if (trad_frame_realreg_p (cache->saved_regs, ARM_SP_REGNUM))
2764     cache->framereg = cache->saved_regs[ARM_SP_REGNUM].realreg;
2765   else
2766     cache->framereg = ARM_SP_REGNUM;
2767
2768   /* Determine offset to previous frame.  */
2769   cache->framesize
2770     = vsp - get_frame_register_unsigned (this_frame, cache->framereg);
2771
2772   /* We already got the previous SP.  */
2773   cache->prev_sp = vsp;
2774
2775   return cache;
2776 }
2777
2778 /* Unwinding via ARM exception table entries.  Note that the sniffer
2779    already computes a filled-in prologue cache, which is then used
2780    with the same arm_prologue_this_id and arm_prologue_prev_register
2781    routines also used for prologue-parsing based unwinding.  */
2782
2783 static int
2784 arm_exidx_unwind_sniffer (const struct frame_unwind *self,
2785                           struct frame_info *this_frame,
2786                           void **this_prologue_cache)
2787 {
2788   struct gdbarch *gdbarch = get_frame_arch (this_frame);
2789   enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
2790   CORE_ADDR addr_in_block, exidx_region, func_start;
2791   struct arm_prologue_cache *cache;
2792   gdb_byte *entry;
2793
2794   /* See if we have an ARM exception table entry covering this address.  */
2795   addr_in_block = get_frame_address_in_block (this_frame);
2796   entry = arm_find_exidx_entry (addr_in_block, &exidx_region);
2797   if (!entry)
2798     return 0;
2799
2800   /* The ARM exception table does not describe unwind information
2801      for arbitrary PC values, but is guaranteed to be correct only
2802      at call sites.  We have to decide here whether we want to use
2803      ARM exception table information for this frame, or fall back
2804      to using prologue parsing.  (Note that if we have DWARF CFI,
2805      this sniffer isn't even called -- CFI is always preferred.)
2806
2807      Before we make this decision, however, we check whether we
2808      actually have *symbol* information for the current frame.
2809      If not, prologue parsing would not work anyway, so we might
2810      as well use the exception table and hope for the best.  */
2811   if (find_pc_partial_function (addr_in_block, NULL, &func_start, NULL))
2812     {
2813       int exc_valid = 0;
2814
2815       /* If the next frame is "normal", we are at a call site in this
2816          frame, so exception information is guaranteed to be valid.  */
2817       if (get_next_frame (this_frame)
2818           && get_frame_type (get_next_frame (this_frame)) == NORMAL_FRAME)
2819         exc_valid = 1;
2820
2821       /* We also assume exception information is valid if we're currently
2822          blocked in a system call.  The system library is supposed to
2823          ensure this, so that e.g. pthread cancellation works.  */
2824       if (arm_frame_is_thumb (this_frame))
2825         {
2826           LONGEST insn;
2827
2828           if (safe_read_memory_integer (get_frame_pc (this_frame) - 2, 2,
2829                                         byte_order_for_code, &insn)
2830               && (insn & 0xff00) == 0xdf00 /* svc */)
2831             exc_valid = 1;
2832         }
2833       else
2834         {
2835           LONGEST insn;
2836
2837           if (safe_read_memory_integer (get_frame_pc (this_frame) - 4, 4,
2838                                         byte_order_for_code, &insn)
2839               && (insn & 0x0f000000) == 0x0f000000 /* svc */)
2840             exc_valid = 1;
2841         }
2842         
2843       /* Bail out if we don't know that exception information is valid.  */
2844       if (!exc_valid)
2845         return 0;
2846
2847      /* The ARM exception index does not mark the *end* of the region
2848         covered by the entry, and some functions will not have any entry.
2849         To correctly recognize the end of the covered region, the linker
2850         should have inserted dummy records with a CANTUNWIND marker.
2851
2852         Unfortunately, current versions of GNU ld do not reliably do
2853         this, and thus we may have found an incorrect entry above.
2854         As a (temporary) sanity check, we only use the entry if it
2855         lies *within* the bounds of the function.  Note that this check
2856         might reject perfectly valid entries that just happen to cover
2857         multiple functions; therefore this check ought to be removed
2858         once the linker is fixed.  */
2859       if (func_start > exidx_region)
2860         return 0;
2861     }
2862
2863   /* Decode the list of unwinding instructions into a prologue cache.
2864      Note that this may fail due to e.g. a "refuse to unwind" code.  */
2865   cache = arm_exidx_fill_cache (this_frame, entry);
2866   if (!cache)
2867     return 0;
2868
2869   *this_prologue_cache = cache;
2870   return 1;
2871 }
2872
2873 struct frame_unwind arm_exidx_unwind = {
2874   NORMAL_FRAME,
2875   default_frame_unwind_stop_reason,
2876   arm_prologue_this_id,
2877   arm_prologue_prev_register,
2878   NULL,
2879   arm_exidx_unwind_sniffer
2880 };
2881
2882 /* Recognize GCC's trampoline for thumb call-indirect.  If we are in a
2883    trampoline, return the target PC.  Otherwise return 0.
2884
2885    void call0a (char c, short s, int i, long l) {}
2886
2887    int main (void)
2888    {
2889      (*pointer_to_call0a) (c, s, i, l);
2890    }
2891
2892    Instead of calling a stub library function  _call_via_xx (xx is
2893    the register name), GCC may inline the trampoline in the object
2894    file as below (register r2 has the address of call0a).
2895
2896    .global main
2897    .type main, %function
2898    ...
2899    bl .L1
2900    ...
2901    .size main, .-main
2902
2903    .L1:
2904    bx r2
2905
2906    The trampoline 'bx r2' doesn't belong to main.  */
2907
2908 static CORE_ADDR
2909 arm_skip_bx_reg (struct frame_info *frame, CORE_ADDR pc)
2910 {
2911   /* The heuristics of recognizing such trampoline is that FRAME is
2912      executing in Thumb mode and the instruction on PC is 'bx Rm'.  */
2913   if (arm_frame_is_thumb (frame))
2914     {
2915       gdb_byte buf[2];
2916
2917       if (target_read_memory (pc, buf, 2) == 0)
2918         {
2919           struct gdbarch *gdbarch = get_frame_arch (frame);
2920           enum bfd_endian byte_order_for_code
2921             = gdbarch_byte_order_for_code (gdbarch);
2922           uint16_t insn
2923             = extract_unsigned_integer (buf, 2, byte_order_for_code);
2924
2925           if ((insn & 0xff80) == 0x4700)  /* bx <Rm> */
2926             {
2927               CORE_ADDR dest
2928                 = get_frame_register_unsigned (frame, bits (insn, 3, 6));
2929
2930               /* Clear the LSB so that gdb core sets step-resume
2931                  breakpoint at the right address.  */
2932               return UNMAKE_THUMB_ADDR (dest);
2933             }
2934         }
2935     }
2936
2937   return 0;
2938 }
2939
2940 static struct arm_prologue_cache *
2941 arm_make_stub_cache (struct frame_info *this_frame)
2942 {
2943   struct arm_prologue_cache *cache;
2944
2945   cache = FRAME_OBSTACK_ZALLOC (struct arm_prologue_cache);
2946   cache->saved_regs = trad_frame_alloc_saved_regs (this_frame);
2947
2948   cache->prev_sp = get_frame_register_unsigned (this_frame, ARM_SP_REGNUM);
2949
2950   return cache;
2951 }
2952
2953 /* Our frame ID for a stub frame is the current SP and LR.  */
2954
2955 static void
2956 arm_stub_this_id (struct frame_info *this_frame,
2957                   void **this_cache,
2958                   struct frame_id *this_id)
2959 {
2960   struct arm_prologue_cache *cache;
2961
2962   if (*this_cache == NULL)
2963     *this_cache = arm_make_stub_cache (this_frame);
2964   cache = *this_cache;
2965
2966   *this_id = frame_id_build (cache->prev_sp, get_frame_pc (this_frame));
2967 }
2968
2969 static int
2970 arm_stub_unwind_sniffer (const struct frame_unwind *self,
2971                          struct frame_info *this_frame,
2972                          void **this_prologue_cache)
2973 {
2974   CORE_ADDR addr_in_block;
2975   gdb_byte dummy[4];
2976   CORE_ADDR pc, start_addr;
2977   const char *name;
2978
2979   addr_in_block = get_frame_address_in_block (this_frame);
2980   pc = get_frame_pc (this_frame);
2981   if (in_plt_section (addr_in_block)
2982       /* We also use the stub winder if the target memory is unreadable
2983          to avoid having the prologue unwinder trying to read it.  */
2984       || target_read_memory (pc, dummy, 4) != 0)
2985     return 1;
2986
2987   if (find_pc_partial_function (pc, &name, &start_addr, NULL) == 0
2988       && arm_skip_bx_reg (this_frame, pc) != 0)
2989     return 1;
2990
2991   return 0;
2992 }
2993
2994 struct frame_unwind arm_stub_unwind = {
2995   NORMAL_FRAME,
2996   default_frame_unwind_stop_reason,
2997   arm_stub_this_id,
2998   arm_prologue_prev_register,
2999   NULL,
3000   arm_stub_unwind_sniffer
3001 };
3002
3003 /* Put here the code to store, into CACHE->saved_regs, the addresses
3004    of the saved registers of frame described by THIS_FRAME.  CACHE is
3005    returned.  */
3006
3007 static struct arm_prologue_cache *
3008 arm_m_exception_cache (struct frame_info *this_frame)
3009 {
3010   struct gdbarch *gdbarch = get_frame_arch (this_frame);
3011   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
3012   struct arm_prologue_cache *cache;
3013   CORE_ADDR unwound_sp;
3014   LONGEST xpsr;
3015
3016   cache = FRAME_OBSTACK_ZALLOC (struct arm_prologue_cache);
3017   cache->saved_regs = trad_frame_alloc_saved_regs (this_frame);
3018
3019   unwound_sp = get_frame_register_unsigned (this_frame,
3020                                             ARM_SP_REGNUM);
3021
3022   /* The hardware saves eight 32-bit words, comprising xPSR,
3023      ReturnAddress, LR (R14), R12, R3, R2, R1, R0.  See details in
3024      "B1.5.6 Exception entry behavior" in
3025      "ARMv7-M Architecture Reference Manual".  */
3026   cache->saved_regs[0].addr = unwound_sp;
3027   cache->saved_regs[1].addr = unwound_sp + 4;
3028   cache->saved_regs[2].addr = unwound_sp + 8;
3029   cache->saved_regs[3].addr = unwound_sp + 12;
3030   cache->saved_regs[12].addr = unwound_sp + 16;
3031   cache->saved_regs[14].addr = unwound_sp + 20;
3032   cache->saved_regs[15].addr = unwound_sp + 24;
3033   cache->saved_regs[ARM_PS_REGNUM].addr = unwound_sp + 28;
3034
3035   /* If bit 9 of the saved xPSR is set, then there is a four-byte
3036      aligner between the top of the 32-byte stack frame and the
3037      previous context's stack pointer.  */
3038   cache->prev_sp = unwound_sp + 32;
3039   if (safe_read_memory_integer (unwound_sp + 28, 4, byte_order, &xpsr)
3040       && (xpsr & (1 << 9)) != 0)
3041     cache->prev_sp += 4;
3042
3043   return cache;
3044 }
3045
3046 /* Implementation of function hook 'this_id' in
3047    'struct frame_uwnind'.  */
3048
3049 static void
3050 arm_m_exception_this_id (struct frame_info *this_frame,
3051                          void **this_cache,
3052                          struct frame_id *this_id)
3053 {
3054   struct arm_prologue_cache *cache;
3055
3056   if (*this_cache == NULL)
3057     *this_cache = arm_m_exception_cache (this_frame);
3058   cache = *this_cache;
3059
3060   /* Our frame ID for a stub frame is the current SP and LR.  */
3061   *this_id = frame_id_build (cache->prev_sp,
3062                              get_frame_pc (this_frame));
3063 }
3064
3065 /* Implementation of function hook 'prev_register' in
3066    'struct frame_uwnind'.  */
3067
3068 static struct value *
3069 arm_m_exception_prev_register (struct frame_info *this_frame,
3070                                void **this_cache,
3071                                int prev_regnum)
3072 {
3073   struct gdbarch *gdbarch = get_frame_arch (this_frame);
3074   struct arm_prologue_cache *cache;
3075
3076   if (*this_cache == NULL)
3077     *this_cache = arm_m_exception_cache (this_frame);
3078   cache = *this_cache;
3079
3080   /* The value was already reconstructed into PREV_SP.  */
3081   if (prev_regnum == ARM_SP_REGNUM)
3082     return frame_unwind_got_constant (this_frame, prev_regnum,
3083                                       cache->prev_sp);
3084
3085   return trad_frame_get_prev_register (this_frame, cache->saved_regs,
3086                                        prev_regnum);
3087 }
3088
3089 /* Implementation of function hook 'sniffer' in
3090    'struct frame_uwnind'.  */
3091
3092 static int
3093 arm_m_exception_unwind_sniffer (const struct frame_unwind *self,
3094                                 struct frame_info *this_frame,
3095                                 void **this_prologue_cache)
3096 {
3097   CORE_ADDR this_pc = get_frame_pc (this_frame);
3098
3099   /* No need to check is_m; this sniffer is only registered for
3100      M-profile architectures.  */
3101
3102   /* Exception frames return to one of these magic PCs.  Other values
3103      are not defined as of v7-M.  See details in "B1.5.8 Exception
3104      return behavior" in "ARMv7-M Architecture Reference Manual".  */
3105   if (this_pc == 0xfffffff1 || this_pc == 0xfffffff9
3106       || this_pc == 0xfffffffd)
3107     return 1;
3108
3109   return 0;
3110 }
3111
3112 /* Frame unwinder for M-profile exceptions.  */
3113
3114 struct frame_unwind arm_m_exception_unwind =
3115 {
3116   SIGTRAMP_FRAME,
3117   default_frame_unwind_stop_reason,
3118   arm_m_exception_this_id,
3119   arm_m_exception_prev_register,
3120   NULL,
3121   arm_m_exception_unwind_sniffer
3122 };
3123
3124 static CORE_ADDR
3125 arm_normal_frame_base (struct frame_info *this_frame, void **this_cache)
3126 {
3127   struct arm_prologue_cache *cache;
3128
3129   if (*this_cache == NULL)
3130     *this_cache = arm_make_prologue_cache (this_frame);
3131   cache = *this_cache;
3132
3133   return cache->prev_sp - cache->framesize;
3134 }
3135
3136 struct frame_base arm_normal_base = {
3137   &arm_prologue_unwind,
3138   arm_normal_frame_base,
3139   arm_normal_frame_base,
3140   arm_normal_frame_base
3141 };
3142
3143 /* Assuming THIS_FRAME is a dummy, return the frame ID of that
3144    dummy frame.  The frame ID's base needs to match the TOS value
3145    saved by save_dummy_frame_tos() and returned from
3146    arm_push_dummy_call, and the PC needs to match the dummy frame's
3147    breakpoint.  */
3148
3149 static struct frame_id
3150 arm_dummy_id (struct gdbarch *gdbarch, struct frame_info *this_frame)
3151 {
3152   return frame_id_build (get_frame_register_unsigned (this_frame,
3153                                                       ARM_SP_REGNUM),
3154                          get_frame_pc (this_frame));
3155 }
3156
3157 /* Given THIS_FRAME, find the previous frame's resume PC (which will
3158    be used to construct the previous frame's ID, after looking up the
3159    containing function).  */
3160
3161 static CORE_ADDR
3162 arm_unwind_pc (struct gdbarch *gdbarch, struct frame_info *this_frame)
3163 {
3164   CORE_ADDR pc;
3165   pc = frame_unwind_register_unsigned (this_frame, ARM_PC_REGNUM);
3166   return arm_addr_bits_remove (gdbarch, pc);
3167 }
3168
3169 static CORE_ADDR
3170 arm_unwind_sp (struct gdbarch *gdbarch, struct frame_info *this_frame)
3171 {
3172   return frame_unwind_register_unsigned (this_frame, ARM_SP_REGNUM);
3173 }
3174
3175 static struct value *
3176 arm_dwarf2_prev_register (struct frame_info *this_frame, void **this_cache,
3177                           int regnum)
3178 {
3179   struct gdbarch * gdbarch = get_frame_arch (this_frame);
3180   CORE_ADDR lr, cpsr;
3181   ULONGEST t_bit = arm_psr_thumb_bit (gdbarch);
3182
3183   switch (regnum)
3184     {
3185     case ARM_PC_REGNUM:
3186       /* The PC is normally copied from the return column, which
3187          describes saves of LR.  However, that version may have an
3188          extra bit set to indicate Thumb state.  The bit is not
3189          part of the PC.  */
3190       lr = frame_unwind_register_unsigned (this_frame, ARM_LR_REGNUM);
3191       return frame_unwind_got_constant (this_frame, regnum,
3192                                         arm_addr_bits_remove (gdbarch, lr));
3193
3194     case ARM_PS_REGNUM:
3195       /* Reconstruct the T bit; see arm_prologue_prev_register for details.  */
3196       cpsr = get_frame_register_unsigned (this_frame, regnum);
3197       lr = frame_unwind_register_unsigned (this_frame, ARM_LR_REGNUM);
3198       if (IS_THUMB_ADDR (lr))
3199         cpsr |= t_bit;
3200       else
3201         cpsr &= ~t_bit;
3202       return frame_unwind_got_constant (this_frame, regnum, cpsr);
3203
3204     default:
3205       internal_error (__FILE__, __LINE__,
3206                       _("Unexpected register %d"), regnum);
3207     }
3208 }
3209
3210 static void
3211 arm_dwarf2_frame_init_reg (struct gdbarch *gdbarch, int regnum,
3212                            struct dwarf2_frame_state_reg *reg,
3213                            struct frame_info *this_frame)
3214 {
3215   switch (regnum)
3216     {
3217     case ARM_PC_REGNUM:
3218     case ARM_PS_REGNUM:
3219       reg->how = DWARF2_FRAME_REG_FN;
3220       reg->loc.fn = arm_dwarf2_prev_register;
3221       break;
3222     case ARM_SP_REGNUM:
3223       reg->how = DWARF2_FRAME_REG_CFA;
3224       break;
3225     }
3226 }
3227
3228 /* Return true if we are in the function's epilogue, i.e. after the
3229    instruction that destroyed the function's stack frame.  */
3230
3231 static int
3232 thumb_in_function_epilogue_p (struct gdbarch *gdbarch, CORE_ADDR pc)
3233 {
3234   enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
3235   unsigned int insn, insn2;
3236   int found_return = 0, found_stack_adjust = 0;
3237   CORE_ADDR func_start, func_end;
3238   CORE_ADDR scan_pc;
3239   gdb_byte buf[4];
3240
3241   if (!find_pc_partial_function (pc, NULL, &func_start, &func_end))
3242     return 0;
3243
3244   /* The epilogue is a sequence of instructions along the following lines:
3245
3246     - add stack frame size to SP or FP
3247     - [if frame pointer used] restore SP from FP
3248     - restore registers from SP [may include PC]
3249     - a return-type instruction [if PC wasn't already restored]
3250
3251     In a first pass, we scan forward from the current PC and verify the
3252     instructions we find as compatible with this sequence, ending in a
3253     return instruction.
3254
3255     However, this is not sufficient to distinguish indirect function calls
3256     within a function from indirect tail calls in the epilogue in some cases.
3257     Therefore, if we didn't already find any SP-changing instruction during
3258     forward scan, we add a backward scanning heuristic to ensure we actually
3259     are in the epilogue.  */
3260
3261   scan_pc = pc;
3262   while (scan_pc < func_end && !found_return)
3263     {
3264       if (target_read_memory (scan_pc, buf, 2))
3265         break;
3266
3267       scan_pc += 2;
3268       insn = extract_unsigned_integer (buf, 2, byte_order_for_code);
3269
3270       if ((insn & 0xff80) == 0x4700)  /* bx <Rm> */
3271         found_return = 1;
3272       else if (insn == 0x46f7)  /* mov pc, lr */
3273         found_return = 1;
3274       else if (thumb_instruction_restores_sp (insn))
3275         {
3276           found_stack_adjust = 1;
3277           if ((insn & 0xfe00) == 0xbd00)  /* pop <registers, PC> */
3278             found_return = 1;
3279         }
3280       else if (thumb_insn_size (insn) == 4)  /* 32-bit Thumb-2 instruction */
3281         {
3282           if (target_read_memory (scan_pc, buf, 2))
3283             break;
3284
3285           scan_pc += 2;
3286           insn2 = extract_unsigned_integer (buf, 2, byte_order_for_code);
3287
3288           if (insn == 0xe8bd)  /* ldm.w sp!, <registers> */
3289             {
3290               found_stack_adjust = 1;
3291               if (insn2 & 0x8000)  /* <registers> include PC.  */
3292                 found_return = 1;
3293             }
3294           else if (insn == 0xf85d  /* ldr.w <Rt>, [sp], #4 */
3295                    && (insn2 & 0x0fff) == 0x0b04)
3296             {
3297               found_stack_adjust = 1;
3298               if ((insn2 & 0xf000) == 0xf000) /* <Rt> is PC.  */
3299                 found_return = 1;
3300             }
3301           else if ((insn & 0xffbf) == 0xecbd  /* vldm sp!, <list> */
3302                    && (insn2 & 0x0e00) == 0x0a00)
3303             found_stack_adjust = 1;
3304           else
3305             break;
3306         }
3307       else
3308         break;
3309     }
3310
3311   if (!found_return)
3312     return 0;
3313
3314   /* Since any instruction in the epilogue sequence, with the possible
3315      exception of return itself, updates the stack pointer, we need to
3316      scan backwards for at most one instruction.  Try either a 16-bit or
3317      a 32-bit instruction.  This is just a heuristic, so we do not worry
3318      too much about false positives.  */
3319
3320   if (!found_stack_adjust)
3321     {
3322       if (pc - 4 < func_start)
3323         return 0;
3324       if (target_read_memory (pc - 4, buf, 4))
3325         return 0;
3326
3327       insn = extract_unsigned_integer (buf, 2, byte_order_for_code);
3328       insn2 = extract_unsigned_integer (buf + 2, 2, byte_order_for_code);
3329
3330       if (thumb_instruction_restores_sp (insn2))
3331         found_stack_adjust = 1;
3332       else if (insn == 0xe8bd)  /* ldm.w sp!, <registers> */
3333         found_stack_adjust = 1;
3334       else if (insn == 0xf85d  /* ldr.w <Rt>, [sp], #4 */
3335                && (insn2 & 0x0fff) == 0x0b04)
3336         found_stack_adjust = 1;
3337       else if ((insn & 0xffbf) == 0xecbd  /* vldm sp!, <list> */
3338                && (insn2 & 0x0e00) == 0x0a00)
3339         found_stack_adjust = 1;
3340     }
3341
3342   return found_stack_adjust;
3343 }
3344
3345 /* Return true if we are in the function's epilogue, i.e. after the
3346    instruction that destroyed the function's stack frame.  */
3347
3348 static int
3349 arm_in_function_epilogue_p (struct gdbarch *gdbarch, CORE_ADDR pc)
3350 {
3351   enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
3352   unsigned int insn;
3353   int found_return, found_stack_adjust;
3354   CORE_ADDR func_start, func_end;
3355
3356   if (arm_pc_is_thumb (gdbarch, pc))
3357     return thumb_in_function_epilogue_p (gdbarch, pc);
3358
3359   if (!find_pc_partial_function (pc, NULL, &func_start, &func_end))
3360     return 0;
3361
3362   /* We are in the epilogue if the previous instruction was a stack
3363      adjustment and the next instruction is a possible return (bx, mov
3364      pc, or pop).  We could have to scan backwards to find the stack
3365      adjustment, or forwards to find the return, but this is a decent
3366      approximation.  First scan forwards.  */
3367
3368   found_return = 0;
3369   insn = read_memory_unsigned_integer (pc, 4, byte_order_for_code);
3370   if (bits (insn, 28, 31) != INST_NV)
3371     {
3372       if ((insn & 0x0ffffff0) == 0x012fff10)
3373         /* BX.  */
3374         found_return = 1;
3375       else if ((insn & 0x0ffffff0) == 0x01a0f000)
3376         /* MOV PC.  */
3377         found_return = 1;
3378       else if ((insn & 0x0fff0000) == 0x08bd0000
3379           && (insn & 0x0000c000) != 0)
3380         /* POP (LDMIA), including PC or LR.  */
3381         found_return = 1;
3382     }
3383
3384   if (!found_return)
3385     return 0;
3386
3387   /* Scan backwards.  This is just a heuristic, so do not worry about
3388      false positives from mode changes.  */
3389
3390   if (pc < func_start + 4)
3391     return 0;
3392
3393   found_stack_adjust = 0;
3394   insn = read_memory_unsigned_integer (pc - 4, 4, byte_order_for_code);
3395   if (bits (insn, 28, 31) != INST_NV)
3396     {
3397       if ((insn & 0x0df0f000) == 0x0080d000)
3398         /* ADD SP (register or immediate).  */
3399         found_stack_adjust = 1;
3400       else if ((insn & 0x0df0f000) == 0x0040d000)
3401         /* SUB SP (register or immediate).  */
3402         found_stack_adjust = 1;
3403       else if ((insn & 0x0ffffff0) == 0x01a0d000)
3404         /* MOV SP.  */
3405         found_stack_adjust = 1;
3406       else if ((insn & 0x0fff0000) == 0x08bd0000)
3407         /* POP (LDMIA).  */
3408         found_stack_adjust = 1;
3409       else if ((insn & 0x0fff0000) == 0x049d0000)
3410         /* POP of a single register.  */
3411         found_stack_adjust = 1;
3412     }
3413
3414   if (found_stack_adjust)
3415     return 1;
3416
3417   return 0;
3418 }
3419
3420
3421 /* When arguments must be pushed onto the stack, they go on in reverse
3422    order.  The code below implements a FILO (stack) to do this.  */
3423
3424 struct stack_item
3425 {
3426   int len;
3427   struct stack_item *prev;
3428   void *data;
3429 };
3430
3431 static struct stack_item *
3432 push_stack_item (struct stack_item *prev, const void *contents, int len)
3433 {
3434   struct stack_item *si;
3435   si = xmalloc (sizeof (struct stack_item));
3436   si->data = xmalloc (len);
3437   si->len = len;
3438   si->prev = prev;
3439   memcpy (si->data, contents, len);
3440   return si;
3441 }
3442
3443 static struct stack_item *
3444 pop_stack_item (struct stack_item *si)
3445 {
3446   struct stack_item *dead = si;
3447   si = si->prev;
3448   xfree (dead->data);
3449   xfree (dead);
3450   return si;
3451 }
3452
3453
3454 /* Return the alignment (in bytes) of the given type.  */
3455
3456 static int
3457 arm_type_align (struct type *t)
3458 {
3459   int n;
3460   int align;
3461   int falign;
3462
3463   t = check_typedef (t);
3464   switch (TYPE_CODE (t))
3465     {
3466     default:
3467       /* Should never happen.  */
3468       internal_error (__FILE__, __LINE__, _("unknown type alignment"));
3469       return 4;
3470
3471     case TYPE_CODE_PTR:
3472     case TYPE_CODE_ENUM:
3473     case TYPE_CODE_INT:
3474     case TYPE_CODE_FLT:
3475     case TYPE_CODE_SET:
3476     case TYPE_CODE_RANGE:
3477     case TYPE_CODE_REF:
3478     case TYPE_CODE_CHAR:
3479     case TYPE_CODE_BOOL:
3480       return TYPE_LENGTH (t);
3481
3482     case TYPE_CODE_ARRAY:
3483     case TYPE_CODE_COMPLEX:
3484       /* TODO: What about vector types?  */
3485       return arm_type_align (TYPE_TARGET_TYPE (t));
3486
3487     case TYPE_CODE_STRUCT:
3488     case TYPE_CODE_UNION:
3489       align = 1;
3490       for (n = 0; n < TYPE_NFIELDS (t); n++)
3491         {
3492           falign = arm_type_align (TYPE_FIELD_TYPE (t, n));
3493           if (falign > align)
3494             align = falign;
3495         }
3496       return align;
3497     }
3498 }
3499
3500 /* Possible base types for a candidate for passing and returning in
3501    VFP registers.  */
3502
3503 enum arm_vfp_cprc_base_type
3504 {
3505   VFP_CPRC_UNKNOWN,
3506   VFP_CPRC_SINGLE,
3507   VFP_CPRC_DOUBLE,
3508   VFP_CPRC_VEC64,
3509   VFP_CPRC_VEC128
3510 };
3511
3512 /* The length of one element of base type B.  */
3513
3514 static unsigned
3515 arm_vfp_cprc_unit_length (enum arm_vfp_cprc_base_type b)
3516 {
3517   switch (b)
3518     {
3519     case VFP_CPRC_SINGLE:
3520       return 4;
3521     case VFP_CPRC_DOUBLE:
3522       return 8;
3523     case VFP_CPRC_VEC64:
3524       return 8;
3525     case VFP_CPRC_VEC128:
3526       return 16;
3527     default:
3528       internal_error (__FILE__, __LINE__, _("Invalid VFP CPRC type: %d."),
3529                       (int) b);
3530     }
3531 }
3532
3533 /* The character ('s', 'd' or 'q') for the type of VFP register used
3534    for passing base type B.  */
3535
3536 static int
3537 arm_vfp_cprc_reg_char (enum arm_vfp_cprc_base_type b)
3538 {
3539   switch (b)
3540     {
3541     case VFP_CPRC_SINGLE:
3542       return 's';
3543     case VFP_CPRC_DOUBLE:
3544       return 'd';
3545     case VFP_CPRC_VEC64:
3546       return 'd';
3547     case VFP_CPRC_VEC128:
3548       return 'q';
3549     default:
3550       internal_error (__FILE__, __LINE__, _("Invalid VFP CPRC type: %d."),
3551                       (int) b);
3552     }
3553 }
3554
3555 /* Determine whether T may be part of a candidate for passing and
3556    returning in VFP registers, ignoring the limit on the total number
3557    of components.  If *BASE_TYPE is VFP_CPRC_UNKNOWN, set it to the
3558    classification of the first valid component found; if it is not
3559    VFP_CPRC_UNKNOWN, all components must have the same classification
3560    as *BASE_TYPE.  If it is found that T contains a type not permitted
3561    for passing and returning in VFP registers, a type differently
3562    classified from *BASE_TYPE, or two types differently classified
3563    from each other, return -1, otherwise return the total number of
3564    base-type elements found (possibly 0 in an empty structure or
3565    array).  Vectors and complex types are not currently supported,
3566    matching the generic AAPCS support.  */
3567
3568 static int
3569 arm_vfp_cprc_sub_candidate (struct type *t,
3570                             enum arm_vfp_cprc_base_type *base_type)
3571 {
3572   t = check_typedef (t);
3573   switch (TYPE_CODE (t))
3574     {
3575     case TYPE_CODE_FLT:
3576       switch (TYPE_LENGTH (t))
3577         {
3578         case 4:
3579           if (*base_type == VFP_CPRC_UNKNOWN)
3580             *base_type = VFP_CPRC_SINGLE;
3581           else if (*base_type != VFP_CPRC_SINGLE)
3582             return -1;
3583           return 1;
3584
3585         case 8:
3586           if (*base_type == VFP_CPRC_UNKNOWN)
3587             *base_type = VFP_CPRC_DOUBLE;
3588           else if (*base_type != VFP_CPRC_DOUBLE)
3589             return -1;
3590           return 1;
3591
3592         default:
3593           return -1;
3594         }
3595       break;
3596
3597     case TYPE_CODE_ARRAY:
3598       {
3599         int count;
3600         unsigned unitlen;
3601         count = arm_vfp_cprc_sub_candidate (TYPE_TARGET_TYPE (t), base_type);
3602         if (count == -1)
3603           return -1;
3604         if (TYPE_LENGTH (t) == 0)
3605           {
3606             gdb_assert (count == 0);
3607             return 0;
3608           }
3609         else if (count == 0)
3610           return -1;
3611         unitlen = arm_vfp_cprc_unit_length (*base_type);
3612         gdb_assert ((TYPE_LENGTH (t) % unitlen) == 0);
3613         return TYPE_LENGTH (t) / unitlen;
3614       }
3615       break;
3616
3617     case TYPE_CODE_STRUCT:
3618       {
3619         int count = 0;
3620         unsigned unitlen;
3621         int i;
3622         for (i = 0; i < TYPE_NFIELDS (t); i++)
3623           {
3624             int sub_count = arm_vfp_cprc_sub_candidate (TYPE_FIELD_TYPE (t, i),
3625                                                         base_type);
3626             if (sub_count == -1)
3627               return -1;
3628             count += sub_count;
3629           }
3630         if (TYPE_LENGTH (t) == 0)
3631           {
3632             gdb_assert (count == 0);
3633             return 0;
3634           }
3635         else if (count == 0)
3636           return -1;
3637         unitlen = arm_vfp_cprc_unit_length (*base_type);
3638         if (TYPE_LENGTH (t) != unitlen * count)
3639           return -1;
3640         return count;
3641       }
3642
3643     case TYPE_CODE_UNION:
3644       {
3645         int count = 0;
3646         unsigned unitlen;
3647         int i;
3648         for (i = 0; i < TYPE_NFIELDS (t); i++)
3649           {
3650             int sub_count = arm_vfp_cprc_sub_candidate (TYPE_FIELD_TYPE (t, i),
3651                                                         base_type);
3652             if (sub_count == -1)
3653               return -1;
3654             count = (count > sub_count ? count : sub_count);
3655           }
3656         if (TYPE_LENGTH (t) == 0)
3657           {
3658             gdb_assert (count == 0);
3659             return 0;
3660           }
3661         else if (count == 0)
3662           return -1;
3663         unitlen = arm_vfp_cprc_unit_length (*base_type);
3664         if (TYPE_LENGTH (t) != unitlen * count)
3665           return -1;
3666         return count;
3667       }
3668
3669     default:
3670       break;
3671     }
3672
3673   return -1;
3674 }
3675
3676 /* Determine whether T is a VFP co-processor register candidate (CPRC)
3677    if passed to or returned from a non-variadic function with the VFP
3678    ABI in effect.  Return 1 if it is, 0 otherwise.  If it is, set
3679    *BASE_TYPE to the base type for T and *COUNT to the number of
3680    elements of that base type before returning.  */
3681
3682 static int
3683 arm_vfp_call_candidate (struct type *t, enum arm_vfp_cprc_base_type *base_type,
3684                         int *count)
3685 {
3686   enum arm_vfp_cprc_base_type b = VFP_CPRC_UNKNOWN;
3687   int c = arm_vfp_cprc_sub_candidate (t, &b);
3688   if (c <= 0 || c > 4)
3689     return 0;
3690   *base_type = b;
3691   *count = c;
3692   return 1;
3693 }
3694
3695 /* Return 1 if the VFP ABI should be used for passing arguments to and
3696    returning values from a function of type FUNC_TYPE, 0
3697    otherwise.  */
3698
3699 static int
3700 arm_vfp_abi_for_function (struct gdbarch *gdbarch, struct type *func_type)
3701 {
3702   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
3703   /* Variadic functions always use the base ABI.  Assume that functions
3704      without debug info are not variadic.  */
3705   if (func_type && TYPE_VARARGS (check_typedef (func_type)))
3706     return 0;
3707   /* The VFP ABI is only supported as a variant of AAPCS.  */
3708   if (tdep->arm_abi != ARM_ABI_AAPCS)
3709     return 0;
3710   return gdbarch_tdep (gdbarch)->fp_model == ARM_FLOAT_VFP;
3711 }
3712
3713 /* We currently only support passing parameters in integer registers, which
3714    conforms with GCC's default model, and VFP argument passing following
3715    the VFP variant of AAPCS.  Several other variants exist and
3716    we should probably support some of them based on the selected ABI.  */
3717
3718 static CORE_ADDR
3719 arm_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
3720                      struct regcache *regcache, CORE_ADDR bp_addr, int nargs,
3721                      struct value **args, CORE_ADDR sp, int struct_return,
3722                      CORE_ADDR struct_addr)
3723 {
3724   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
3725   int argnum;
3726   int argreg;
3727   int nstack;
3728   struct stack_item *si = NULL;
3729   int use_vfp_abi;
3730   struct type *ftype;
3731   unsigned vfp_regs_free = (1 << 16) - 1;
3732
3733   /* Determine the type of this function and whether the VFP ABI
3734      applies.  */
3735   ftype = check_typedef (value_type (function));
3736   if (TYPE_CODE (ftype) == TYPE_CODE_PTR)
3737     ftype = check_typedef (TYPE_TARGET_TYPE (ftype));
3738   use_vfp_abi = arm_vfp_abi_for_function (gdbarch, ftype);
3739
3740   /* Set the return address.  For the ARM, the return breakpoint is
3741      always at BP_ADDR.  */
3742   if (arm_pc_is_thumb (gdbarch, bp_addr))
3743     bp_addr |= 1;
3744   regcache_cooked_write_unsigned (regcache, ARM_LR_REGNUM, bp_addr);
3745
3746   /* Walk through the list of args and determine how large a temporary
3747      stack is required.  Need to take care here as structs may be
3748      passed on the stack, and we have to push them.  */
3749   nstack = 0;
3750
3751   argreg = ARM_A1_REGNUM;
3752   nstack = 0;
3753
3754   /* The struct_return pointer occupies the first parameter
3755      passing register.  */
3756   if (struct_return)
3757     {
3758       if (arm_debug)
3759         fprintf_unfiltered (gdb_stdlog, "struct return in %s = %s\n",
3760                             gdbarch_register_name (gdbarch, argreg),
3761                             paddress (gdbarch, struct_addr));
3762       regcache_cooked_write_unsigned (regcache, argreg, struct_addr);
3763       argreg++;
3764     }
3765
3766   for (argnum = 0; argnum < nargs; argnum++)
3767     {
3768       int len;
3769       struct type *arg_type;
3770       struct type *target_type;
3771       enum type_code typecode;
3772       const bfd_byte *val;
3773       int align;
3774       enum arm_vfp_cprc_base_type vfp_base_type;
3775       int vfp_base_count;
3776       int may_use_core_reg = 1;
3777
3778       arg_type = check_typedef (value_type (args[argnum]));
3779       len = TYPE_LENGTH (arg_type);
3780       target_type = TYPE_TARGET_TYPE (arg_type);
3781       typecode = TYPE_CODE (arg_type);
3782       val = value_contents (args[argnum]);
3783
3784       align = arm_type_align (arg_type);
3785       /* Round alignment up to a whole number of words.  */
3786       align = (align + INT_REGISTER_SIZE - 1) & ~(INT_REGISTER_SIZE - 1);
3787       /* Different ABIs have different maximum alignments.  */
3788       if (gdbarch_tdep (gdbarch)->arm_abi == ARM_ABI_APCS)
3789         {
3790           /* The APCS ABI only requires word alignment.  */
3791           align = INT_REGISTER_SIZE;
3792         }
3793       else
3794         {
3795           /* The AAPCS requires at most doubleword alignment.  */
3796           if (align > INT_REGISTER_SIZE * 2)
3797             align = INT_REGISTER_SIZE * 2;
3798         }
3799
3800       if (use_vfp_abi
3801           && arm_vfp_call_candidate (arg_type, &vfp_base_type,
3802                                      &vfp_base_count))
3803         {
3804           int regno;
3805           int unit_length;
3806           int shift;
3807           unsigned mask;
3808
3809           /* Because this is a CPRC it cannot go in a core register or
3810              cause a core register to be skipped for alignment.
3811              Either it goes in VFP registers and the rest of this loop
3812              iteration is skipped for this argument, or it goes on the
3813              stack (and the stack alignment code is correct for this
3814              case).  */
3815           may_use_core_reg = 0;
3816
3817           unit_length = arm_vfp_cprc_unit_length (vfp_base_type);
3818           shift = unit_length / 4;
3819           mask = (1 << (shift * vfp_base_count)) - 1;
3820           for (regno = 0; regno < 16; regno += shift)
3821             if (((vfp_regs_free >> regno) & mask) == mask)
3822               break;
3823
3824           if (regno < 16)
3825             {
3826               int reg_char;
3827               int reg_scaled;
3828               int i;
3829
3830               vfp_regs_free &= ~(mask << regno);
3831               reg_scaled = regno / shift;
3832               reg_char = arm_vfp_cprc_reg_char (vfp_base_type);
3833               for (i = 0; i < vfp_base_count; i++)
3834                 {
3835                   char name_buf[4];
3836                   int regnum;
3837                   if (reg_char == 'q')
3838                     arm_neon_quad_write (gdbarch, regcache, reg_scaled + i,
3839                                          val + i * unit_length);
3840                   else
3841                     {
3842                       xsnprintf (name_buf, sizeof (name_buf), "%c%d",
3843                                  reg_char, reg_scaled + i);
3844                       regnum = user_reg_map_name_to_regnum (gdbarch, name_buf,
3845                                                             strlen (name_buf));
3846                       regcache_cooked_write (regcache, regnum,
3847                                              val + i * unit_length);
3848                     }
3849                 }
3850               continue;
3851             }
3852           else
3853             {
3854               /* This CPRC could not go in VFP registers, so all VFP
3855                  registers are now marked as used.  */
3856               vfp_regs_free = 0;
3857             }
3858         }
3859
3860       /* Push stack padding for dowubleword alignment.  */
3861       if (nstack & (align - 1))
3862         {
3863           si = push_stack_item (si, val, INT_REGISTER_SIZE);
3864           nstack += INT_REGISTER_SIZE;
3865         }
3866       
3867       /* Doubleword aligned quantities must go in even register pairs.  */
3868       if (may_use_core_reg
3869           && argreg <= ARM_LAST_ARG_REGNUM
3870           && align > INT_REGISTER_SIZE
3871           && argreg & 1)
3872         argreg++;
3873
3874       /* If the argument is a pointer to a function, and it is a
3875          Thumb function, create a LOCAL copy of the value and set
3876          the THUMB bit in it.  */
3877       if (TYPE_CODE_PTR == typecode
3878           && target_type != NULL
3879           && TYPE_CODE_FUNC == TYPE_CODE (check_typedef (target_type)))
3880         {
3881           CORE_ADDR regval = extract_unsigned_integer (val, len, byte_order);
3882           if (arm_pc_is_thumb (gdbarch, regval))
3883             {
3884               bfd_byte *copy = alloca (len);
3885               store_unsigned_integer (copy, len, byte_order,
3886                                       MAKE_THUMB_ADDR (regval));
3887               val = copy;
3888             }
3889         }
3890
3891       /* Copy the argument to general registers or the stack in
3892          register-sized pieces.  Large arguments are split between
3893          registers and stack.  */
3894       while (len > 0)
3895         {
3896           int partial_len = len < INT_REGISTER_SIZE ? len : INT_REGISTER_SIZE;
3897
3898           if (may_use_core_reg && argreg <= ARM_LAST_ARG_REGNUM)
3899             {
3900               /* The argument is being passed in a general purpose
3901                  register.  */
3902               CORE_ADDR regval
3903                 = extract_unsigned_integer (val, partial_len, byte_order);
3904               if (byte_order == BFD_ENDIAN_BIG)
3905                 regval <<= (INT_REGISTER_SIZE - partial_len) * 8;
3906               if (arm_debug)
3907                 fprintf_unfiltered (gdb_stdlog, "arg %d in %s = 0x%s\n",
3908                                     argnum,
3909                                     gdbarch_register_name
3910                                       (gdbarch, argreg),
3911                                     phex (regval, INT_REGISTER_SIZE));
3912               regcache_cooked_write_unsigned (regcache, argreg, regval);
3913               argreg++;
3914             }
3915           else
3916             {
3917               /* Push the arguments onto the stack.  */
3918               if (arm_debug)
3919                 fprintf_unfiltered (gdb_stdlog, "arg %d @ sp + %d\n",
3920                                     argnum, nstack);
3921               si = push_stack_item (si, val, INT_REGISTER_SIZE);
3922               nstack += INT_REGISTER_SIZE;
3923             }
3924               
3925           len -= partial_len;
3926           val += partial_len;
3927         }
3928     }
3929   /* If we have an odd number of words to push, then decrement the stack
3930      by one word now, so first stack argument will be dword aligned.  */
3931   if (nstack & 4)
3932     sp -= 4;
3933
3934   while (si)
3935     {
3936       sp -= si->len;
3937       write_memory (sp, si->data, si->len);
3938       si = pop_stack_item (si);
3939     }
3940
3941   /* Finally, update teh SP register.  */
3942   regcache_cooked_write_unsigned (regcache, ARM_SP_REGNUM, sp);
3943
3944   return sp;
3945 }
3946
3947
3948 /* Always align the frame to an 8-byte boundary.  This is required on
3949    some platforms and harmless on the rest.  */
3950
3951 static CORE_ADDR
3952 arm_frame_align (struct gdbarch *gdbarch, CORE_ADDR sp)
3953 {
3954   /* Align the stack to eight bytes.  */
3955   return sp & ~ (CORE_ADDR) 7;
3956 }
3957
3958 static void
3959 print_fpu_flags (struct ui_file *file, int flags)
3960 {
3961   if (flags & (1 << 0))
3962     fputs_filtered ("IVO ", file);
3963   if (flags & (1 << 1))
3964     fputs_filtered ("DVZ ", file);
3965   if (flags & (1 << 2))
3966     fputs_filtered ("OFL ", file);
3967   if (flags & (1 << 3))
3968     fputs_filtered ("UFL ", file);
3969   if (flags & (1 << 4))
3970     fputs_filtered ("INX ", file);
3971   fputc_filtered ('\n', file);
3972 }
3973
3974 /* Print interesting information about the floating point processor
3975    (if present) or emulator.  */
3976 static void
3977 arm_print_float_info (struct gdbarch *gdbarch, struct ui_file *file,
3978                       struct frame_info *frame, const char *args)
3979 {
3980   unsigned long status = get_frame_register_unsigned (frame, ARM_FPS_REGNUM);
3981   int type;
3982
3983   type = (status >> 24) & 127;
3984   if (status & (1 << 31))
3985     fprintf_filtered (file, _("Hardware FPU type %d\n"), type);
3986   else
3987     fprintf_filtered (file, _("Software FPU type %d\n"), type);
3988   /* i18n: [floating point unit] mask */
3989   fputs_filtered (_("mask: "), file);
3990   print_fpu_flags (file, status >> 16);
3991   /* i18n: [floating point unit] flags */
3992   fputs_filtered (_("flags: "), file);
3993   print_fpu_flags (file, status);
3994 }
3995
3996 /* Construct the ARM extended floating point type.  */
3997 static struct type *
3998 arm_ext_type (struct gdbarch *gdbarch)
3999 {
4000   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
4001
4002   if (!tdep->arm_ext_type)
4003     tdep->arm_ext_type
4004       = arch_float_type (gdbarch, -1, "builtin_type_arm_ext",
4005                          floatformats_arm_ext);
4006
4007   return tdep->arm_ext_type;
4008 }
4009
4010 static struct type *
4011 arm_neon_double_type (struct gdbarch *gdbarch)
4012 {
4013   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
4014
4015   if (tdep->neon_double_type == NULL)
4016     {
4017       struct type *t, *elem;
4018
4019       t = arch_composite_type (gdbarch, "__gdb_builtin_type_neon_d",
4020                                TYPE_CODE_UNION);
4021       elem = builtin_type (gdbarch)->builtin_uint8;
4022       append_composite_type_field (t, "u8", init_vector_type (elem, 8));
4023       elem = builtin_type (gdbarch)->builtin_uint16;
4024       append_composite_type_field (t, "u16", init_vector_type (elem, 4));
4025       elem = builtin_type (gdbarch)->builtin_uint32;
4026       append_composite_type_field (t, "u32", init_vector_type (elem, 2));
4027       elem = builtin_type (gdbarch)->builtin_uint64;
4028       append_composite_type_field (t, "u64", elem);
4029       elem = builtin_type (gdbarch)->builtin_float;
4030       append_composite_type_field (t, "f32", init_vector_type (elem, 2));
4031       elem = builtin_type (gdbarch)->builtin_double;
4032       append_composite_type_field (t, "f64", elem);
4033
4034       TYPE_VECTOR (t) = 1;
4035       TYPE_NAME (t) = "neon_d";
4036       tdep->neon_double_type = t;
4037     }
4038
4039   return tdep->neon_double_type;
4040 }
4041
4042 /* FIXME: The vector types are not correctly ordered on big-endian
4043    targets.  Just as s0 is the low bits of d0, d0[0] is also the low
4044    bits of d0 - regardless of what unit size is being held in d0.  So
4045    the offset of the first uint8 in d0 is 7, but the offset of the
4046    first float is 4.  This code works as-is for little-endian
4047    targets.  */
4048
4049 static struct type *
4050 arm_neon_quad_type (struct gdbarch *gdbarch)
4051 {
4052   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
4053
4054   if (tdep->neon_quad_type == NULL)
4055     {
4056       struct type *t, *elem;
4057
4058       t = arch_composite_type (gdbarch, "__gdb_builtin_type_neon_q",
4059                                TYPE_CODE_UNION);
4060       elem = builtin_type (gdbarch)->builtin_uint8;
4061       append_composite_type_field (t, "u8", init_vector_type (elem, 16));
4062       elem = builtin_type (gdbarch)->builtin_uint16;
4063       append_composite_type_field (t, "u16", init_vector_type (elem, 8));
4064       elem = builtin_type (gdbarch)->builtin_uint32;
4065       append_composite_type_field (t, "u32", init_vector_type (elem, 4));
4066       elem = builtin_type (gdbarch)->builtin_uint64;
4067       append_composite_type_field (t, "u64", init_vector_type (elem, 2));
4068       elem = builtin_type (gdbarch)->builtin_float;
4069       append_composite_type_field (t, "f32", init_vector_type (elem, 4));
4070       elem = builtin_type (gdbarch)->builtin_double;
4071       append_composite_type_field (t, "f64", init_vector_type (elem, 2));
4072
4073       TYPE_VECTOR (t) = 1;
4074       TYPE_NAME (t) = "neon_q";
4075       tdep->neon_quad_type = t;
4076     }
4077
4078   return tdep->neon_quad_type;
4079 }
4080
4081 /* Return the GDB type object for the "standard" data type of data in
4082    register N.  */
4083
4084 static struct type *
4085 arm_register_type (struct gdbarch *gdbarch, int regnum)
4086 {
4087   int num_regs = gdbarch_num_regs (gdbarch);
4088
4089   if (gdbarch_tdep (gdbarch)->have_vfp_pseudos
4090       && regnum >= num_regs && regnum < num_regs + 32)
4091     return builtin_type (gdbarch)->builtin_float;
4092
4093   if (gdbarch_tdep (gdbarch)->have_neon_pseudos
4094       && regnum >= num_regs + 32 && regnum < num_regs + 32 + 16)
4095     return arm_neon_quad_type (gdbarch);
4096
4097   /* If the target description has register information, we are only
4098      in this function so that we can override the types of
4099      double-precision registers for NEON.  */
4100   if (tdesc_has_registers (gdbarch_target_desc (gdbarch)))
4101     {
4102       struct type *t = tdesc_register_type (gdbarch, regnum);
4103
4104       if (regnum >= ARM_D0_REGNUM && regnum < ARM_D0_REGNUM + 32
4105           && TYPE_CODE (t) == TYPE_CODE_FLT
4106           && gdbarch_tdep (gdbarch)->have_neon)
4107         return arm_neon_double_type (gdbarch);
4108       else
4109         return t;
4110     }
4111
4112   if (regnum >= ARM_F0_REGNUM && regnum < ARM_F0_REGNUM + NUM_FREGS)
4113     {
4114       if (!gdbarch_tdep (gdbarch)->have_fpa_registers)
4115         return builtin_type (gdbarch)->builtin_void;
4116
4117       return arm_ext_type (gdbarch);
4118     }
4119   else if (regnum == ARM_SP_REGNUM)
4120     return builtin_type (gdbarch)->builtin_data_ptr;
4121   else if (regnum == ARM_PC_REGNUM)
4122     return builtin_type (gdbarch)->builtin_func_ptr;
4123   else if (regnum >= ARRAY_SIZE (arm_register_names))
4124     /* These registers are only supported on targets which supply
4125        an XML description.  */
4126     return builtin_type (gdbarch)->builtin_int0;
4127   else
4128     return builtin_type (gdbarch)->builtin_uint32;
4129 }
4130
4131 /* Map a DWARF register REGNUM onto the appropriate GDB register
4132    number.  */
4133
4134 static int
4135 arm_dwarf_reg_to_regnum (struct gdbarch *gdbarch, int reg)
4136 {
4137   /* Core integer regs.  */
4138   if (reg >= 0 && reg <= 15)
4139     return reg;
4140
4141   /* Legacy FPA encoding.  These were once used in a way which
4142      overlapped with VFP register numbering, so their use is
4143      discouraged, but GDB doesn't support the ARM toolchain
4144      which used them for VFP.  */
4145   if (reg >= 16 && reg <= 23)
4146     return ARM_F0_REGNUM + reg - 16;
4147
4148   /* New assignments for the FPA registers.  */
4149   if (reg >= 96 && reg <= 103)
4150     return ARM_F0_REGNUM + reg - 96;
4151
4152   /* WMMX register assignments.  */
4153   if (reg >= 104 && reg <= 111)
4154     return ARM_WCGR0_REGNUM + reg - 104;
4155
4156   if (reg >= 112 && reg <= 127)
4157     return ARM_WR0_REGNUM + reg - 112;
4158
4159   if (reg >= 192 && reg <= 199)
4160     return ARM_WC0_REGNUM + reg - 192;
4161
4162   /* VFP v2 registers.  A double precision value is actually
4163      in d1 rather than s2, but the ABI only defines numbering
4164      for the single precision registers.  This will "just work"
4165      in GDB for little endian targets (we'll read eight bytes,
4166      starting in s0 and then progressing to s1), but will be
4167      reversed on big endian targets with VFP.  This won't
4168      be a problem for the new Neon quad registers; you're supposed
4169      to use DW_OP_piece for those.  */
4170   if (reg >= 64 && reg <= 95)
4171     {
4172       char name_buf[4];
4173
4174       xsnprintf (name_buf, sizeof (name_buf), "s%d", reg - 64);
4175       return user_reg_map_name_to_regnum (gdbarch, name_buf,
4176                                           strlen (name_buf));
4177     }
4178
4179   /* VFP v3 / Neon registers.  This range is also used for VFP v2
4180      registers, except that it now describes d0 instead of s0.  */
4181   if (reg >= 256 && reg <= 287)
4182     {
4183       char name_buf[4];
4184
4185       xsnprintf (name_buf, sizeof (name_buf), "d%d", reg - 256);
4186       return user_reg_map_name_to_regnum (gdbarch, name_buf,
4187                                           strlen (name_buf));
4188     }
4189
4190   return -1;
4191 }
4192
4193 /* Map GDB internal REGNUM onto the Arm simulator register numbers.  */
4194 static int
4195 arm_register_sim_regno (struct gdbarch *gdbarch, int regnum)
4196 {
4197   int reg = regnum;
4198   gdb_assert (reg >= 0 && reg < gdbarch_num_regs (gdbarch));
4199
4200   if (regnum >= ARM_WR0_REGNUM && regnum <= ARM_WR15_REGNUM)
4201     return regnum - ARM_WR0_REGNUM + SIM_ARM_IWMMXT_COP0R0_REGNUM;
4202
4203   if (regnum >= ARM_WC0_REGNUM && regnum <= ARM_WC7_REGNUM)
4204     return regnum - ARM_WC0_REGNUM + SIM_ARM_IWMMXT_COP1R0_REGNUM;
4205
4206   if (regnum >= ARM_WCGR0_REGNUM && regnum <= ARM_WCGR7_REGNUM)
4207     return regnum - ARM_WCGR0_REGNUM + SIM_ARM_IWMMXT_COP1R8_REGNUM;
4208
4209   if (reg < NUM_GREGS)
4210     return SIM_ARM_R0_REGNUM + reg;
4211   reg -= NUM_GREGS;
4212
4213   if (reg < NUM_FREGS)
4214     return SIM_ARM_FP0_REGNUM + reg;
4215   reg -= NUM_FREGS;
4216
4217   if (reg < NUM_SREGS)
4218     return SIM_ARM_FPS_REGNUM + reg;
4219   reg -= NUM_SREGS;
4220
4221   internal_error (__FILE__, __LINE__, _("Bad REGNUM %d"), regnum);
4222 }
4223
4224 /* NOTE: cagney/2001-08-20: Both convert_from_extended() and
4225    convert_to_extended() use floatformat_arm_ext_littlebyte_bigword.
4226    It is thought that this is is the floating-point register format on
4227    little-endian systems.  */
4228
4229 static void
4230 convert_from_extended (const struct floatformat *fmt, const void *ptr,
4231                        void *dbl, int endianess)
4232 {
4233   DOUBLEST d;
4234
4235   if (endianess == BFD_ENDIAN_BIG)
4236     floatformat_to_doublest (&floatformat_arm_ext_big, ptr, &d);
4237   else
4238     floatformat_to_doublest (&floatformat_arm_ext_littlebyte_bigword,
4239                              ptr, &d);
4240   floatformat_from_doublest (fmt, &d, dbl);
4241 }
4242
4243 static void
4244 convert_to_extended (const struct floatformat *fmt, void *dbl, const void *ptr,
4245                      int endianess)
4246 {
4247   DOUBLEST d;
4248
4249   floatformat_to_doublest (fmt, ptr, &d);
4250   if (endianess == BFD_ENDIAN_BIG)
4251     floatformat_from_doublest (&floatformat_arm_ext_big, &d, dbl);
4252   else
4253     floatformat_from_doublest (&floatformat_arm_ext_littlebyte_bigword,
4254                                &d, dbl);
4255 }
4256
4257 static int
4258 condition_true (unsigned long cond, unsigned long status_reg)
4259 {
4260   if (cond == INST_AL || cond == INST_NV)
4261     return 1;
4262
4263   switch (cond)
4264     {
4265     case INST_EQ:
4266       return ((status_reg & FLAG_Z) != 0);
4267     case INST_NE:
4268       return ((status_reg & FLAG_Z) == 0);
4269     case INST_CS:
4270       return ((status_reg & FLAG_C) != 0);
4271     case INST_CC:
4272       return ((status_reg & FLAG_C) == 0);
4273     case INST_MI:
4274       return ((status_reg & FLAG_N) != 0);
4275     case INST_PL:
4276       return ((status_reg & FLAG_N) == 0);
4277     case INST_VS:
4278       return ((status_reg & FLAG_V) != 0);
4279     case INST_VC:
4280       return ((status_reg & FLAG_V) == 0);
4281     case INST_HI:
4282       return ((status_reg & (FLAG_C | FLAG_Z)) == FLAG_C);
4283     case INST_LS:
4284       return ((status_reg & (FLAG_C | FLAG_Z)) != FLAG_C);
4285     case INST_GE:
4286       return (((status_reg & FLAG_N) == 0) == ((status_reg & FLAG_V) == 0));
4287     case INST_LT:
4288       return (((status_reg & FLAG_N) == 0) != ((status_reg & FLAG_V) == 0));
4289     case INST_GT:
4290       return (((status_reg & FLAG_Z) == 0)
4291               && (((status_reg & FLAG_N) == 0)
4292                   == ((status_reg & FLAG_V) == 0)));
4293     case INST_LE:
4294       return (((status_reg & FLAG_Z) != 0)
4295               || (((status_reg & FLAG_N) == 0)
4296                   != ((status_reg & FLAG_V) == 0)));
4297     }
4298   return 1;
4299 }
4300
4301 static unsigned long
4302 shifted_reg_val (struct frame_info *frame, unsigned long inst, int carry,
4303                  unsigned long pc_val, unsigned long status_reg)
4304 {
4305   unsigned long res, shift;
4306   int rm = bits (inst, 0, 3);
4307   unsigned long shifttype = bits (inst, 5, 6);
4308
4309   if (bit (inst, 4))
4310     {
4311       int rs = bits (inst, 8, 11);
4312       shift = (rs == 15 ? pc_val + 8
4313                         : get_frame_register_unsigned (frame, rs)) & 0xFF;
4314     }
4315   else
4316     shift = bits (inst, 7, 11);
4317
4318   res = (rm == ARM_PC_REGNUM
4319          ? (pc_val + (bit (inst, 4) ? 12 : 8))
4320          : get_frame_register_unsigned (frame, rm));
4321
4322   switch (shifttype)
4323     {
4324     case 0:                     /* LSL */
4325       res = shift >= 32 ? 0 : res << shift;
4326       break;
4327
4328     case 1:                     /* LSR */
4329       res = shift >= 32 ? 0 : res >> shift;
4330       break;
4331
4332     case 2:                     /* ASR */
4333       if (shift >= 32)
4334         shift = 31;
4335       res = ((res & 0x80000000L)
4336              ? ~((~res) >> shift) : res >> shift);
4337       break;
4338
4339     case 3:                     /* ROR/RRX */
4340       shift &= 31;
4341       if (shift == 0)
4342         res = (res >> 1) | (carry ? 0x80000000L : 0);
4343       else
4344         res = (res >> shift) | (res << (32 - shift));
4345       break;
4346     }
4347
4348   return res & 0xffffffff;
4349 }
4350
4351 /* Return number of 1-bits in VAL.  */
4352
4353 static int
4354 bitcount (unsigned long val)
4355 {
4356   int nbits;
4357   for (nbits = 0; val != 0; nbits++)
4358     val &= val - 1;             /* Delete rightmost 1-bit in val.  */
4359   return nbits;
4360 }
4361
4362 /* Return the size in bytes of the complete Thumb instruction whose
4363    first halfword is INST1.  */
4364
4365 static int
4366 thumb_insn_size (unsigned short inst1)
4367 {
4368   if ((inst1 & 0xe000) == 0xe000 && (inst1 & 0x1800) != 0)
4369     return 4;
4370   else
4371     return 2;
4372 }
4373
4374 static int
4375 thumb_advance_itstate (unsigned int itstate)
4376 {
4377   /* Preserve IT[7:5], the first three bits of the condition.  Shift
4378      the upcoming condition flags left by one bit.  */
4379   itstate = (itstate & 0xe0) | ((itstate << 1) & 0x1f);
4380
4381   /* If we have finished the IT block, clear the state.  */
4382   if ((itstate & 0x0f) == 0)
4383     itstate = 0;
4384
4385   return itstate;
4386 }
4387
4388 /* Find the next PC after the current instruction executes.  In some
4389    cases we can not statically determine the answer (see the IT state
4390    handling in this function); in that case, a breakpoint may be
4391    inserted in addition to the returned PC, which will be used to set
4392    another breakpoint by our caller.  */
4393
4394 static CORE_ADDR
4395 thumb_get_next_pc_raw (struct frame_info *frame, CORE_ADDR pc)
4396 {
4397   struct gdbarch *gdbarch = get_frame_arch (frame);
4398   struct address_space *aspace = get_frame_address_space (frame);
4399   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
4400   enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
4401   unsigned long pc_val = ((unsigned long) pc) + 4;      /* PC after prefetch */
4402   unsigned short inst1;
4403   CORE_ADDR nextpc = pc + 2;            /* Default is next instruction.  */
4404   unsigned long offset;
4405   ULONGEST status, itstate;
4406
4407   nextpc = MAKE_THUMB_ADDR (nextpc);
4408   pc_val = MAKE_THUMB_ADDR (pc_val);
4409
4410   inst1 = read_memory_unsigned_integer (pc, 2, byte_order_for_code);
4411
4412   /* Thumb-2 conditional execution support.  There are eight bits in
4413      the CPSR which describe conditional execution state.  Once
4414      reconstructed (they're in a funny order), the low five bits
4415      describe the low bit of the condition for each instruction and
4416      how many instructions remain.  The high three bits describe the
4417      base condition.  One of the low four bits will be set if an IT
4418      block is active.  These bits read as zero on earlier
4419      processors.  */
4420   status = get_frame_register_unsigned (frame, ARM_PS_REGNUM);
4421   itstate = ((status >> 8) & 0xfc) | ((status >> 25) & 0x3);
4422
4423   /* If-Then handling.  On GNU/Linux, where this routine is used, we
4424      use an undefined instruction as a breakpoint.  Unlike BKPT, IT
4425      can disable execution of the undefined instruction.  So we might
4426      miss the breakpoint if we set it on a skipped conditional
4427      instruction.  Because conditional instructions can change the
4428      flags, affecting the execution of further instructions, we may
4429      need to set two breakpoints.  */
4430
4431   if (gdbarch_tdep (gdbarch)->thumb2_breakpoint != NULL)
4432     {
4433       if ((inst1 & 0xff00) == 0xbf00 && (inst1 & 0x000f) != 0)
4434         {
4435           /* An IT instruction.  Because this instruction does not
4436              modify the flags, we can accurately predict the next
4437              executed instruction.  */
4438           itstate = inst1 & 0x00ff;
4439           pc += thumb_insn_size (inst1);
4440
4441           while (itstate != 0 && ! condition_true (itstate >> 4, status))
4442             {
4443               inst1 = read_memory_unsigned_integer (pc, 2,
4444                                                     byte_order_for_code);
4445               pc += thumb_insn_size (inst1);
4446               itstate = thumb_advance_itstate (itstate);
4447             }
4448
4449           return MAKE_THUMB_ADDR (pc);
4450         }
4451       else if (itstate != 0)
4452         {
4453           /* We are in a conditional block.  Check the condition.  */
4454           if (! condition_true (itstate >> 4, status))
4455             {
4456               /* Advance to the next executed instruction.  */
4457               pc += thumb_insn_size (inst1);
4458               itstate = thumb_advance_itstate (itstate);
4459
4460               while (itstate != 0 && ! condition_true (itstate >> 4, status))
4461                 {
4462                   inst1 = read_memory_unsigned_integer (pc, 2, 
4463                                                         byte_order_for_code);
4464                   pc += thumb_insn_size (inst1);
4465                   itstate = thumb_advance_itstate (itstate);
4466                 }
4467
4468               return MAKE_THUMB_ADDR (pc);
4469             }
4470           else if ((itstate & 0x0f) == 0x08)
4471             {
4472               /* This is the last instruction of the conditional
4473                  block, and it is executed.  We can handle it normally
4474                  because the following instruction is not conditional,
4475                  and we must handle it normally because it is
4476                  permitted to branch.  Fall through.  */
4477             }
4478           else
4479             {
4480               int cond_negated;
4481
4482               /* There are conditional instructions after this one.
4483                  If this instruction modifies the flags, then we can
4484                  not predict what the next executed instruction will
4485                  be.  Fortunately, this instruction is architecturally
4486                  forbidden to branch; we know it will fall through.
4487                  Start by skipping past it.  */
4488               pc += thumb_insn_size (inst1);
4489               itstate = thumb_advance_itstate (itstate);
4490
4491               /* Set a breakpoint on the following instruction.  */
4492               gdb_assert ((itstate & 0x0f) != 0);
4493               arm_insert_single_step_breakpoint (gdbarch, aspace,
4494                                                  MAKE_THUMB_ADDR (pc));
4495               cond_negated = (itstate >> 4) & 1;
4496
4497               /* Skip all following instructions with the same
4498                  condition.  If there is a later instruction in the IT
4499                  block with the opposite condition, set the other
4500                  breakpoint there.  If not, then set a breakpoint on
4501                  the instruction after the IT block.  */
4502               do
4503                 {
4504                   inst1 = read_memory_unsigned_integer (pc, 2,
4505                                                         byte_order_for_code);
4506                   pc += thumb_insn_size (inst1);
4507                   itstate = thumb_advance_itstate (itstate);
4508                 }
4509               while (itstate != 0 && ((itstate >> 4) & 1) == cond_negated);
4510
4511               return MAKE_THUMB_ADDR (pc);
4512             }
4513         }
4514     }
4515   else if (itstate & 0x0f)
4516     {
4517       /* We are in a conditional block.  Check the condition.  */
4518       int cond = itstate >> 4;
4519
4520       if (! condition_true (cond, status))
4521         /* Advance to the next instruction.  All the 32-bit
4522            instructions share a common prefix.  */
4523         return MAKE_THUMB_ADDR (pc + thumb_insn_size (inst1));
4524
4525       /* Otherwise, handle the instruction normally.  */
4526     }
4527
4528   if ((inst1 & 0xff00) == 0xbd00)       /* pop {rlist, pc} */
4529     {
4530       CORE_ADDR sp;
4531
4532       /* Fetch the saved PC from the stack.  It's stored above
4533          all of the other registers.  */
4534       offset = bitcount (bits (inst1, 0, 7)) * INT_REGISTER_SIZE;
4535       sp = get_frame_register_unsigned (frame, ARM_SP_REGNUM);
4536       nextpc = read_memory_unsigned_integer (sp + offset, 4, byte_order);
4537     }
4538   else if ((inst1 & 0xf000) == 0xd000)  /* conditional branch */
4539     {
4540       unsigned long cond = bits (inst1, 8, 11);
4541       if (cond == 0x0f)  /* 0x0f = SWI */
4542         {
4543           struct gdbarch_tdep *tdep;
4544           tdep = gdbarch_tdep (gdbarch);
4545
4546           if (tdep->syscall_next_pc != NULL)
4547             nextpc = tdep->syscall_next_pc (frame);
4548
4549         }
4550       else if (cond != 0x0f && condition_true (cond, status))
4551         nextpc = pc_val + (sbits (inst1, 0, 7) << 1);
4552     }
4553   else if ((inst1 & 0xf800) == 0xe000)  /* unconditional branch */
4554     {
4555       nextpc = pc_val + (sbits (inst1, 0, 10) << 1);
4556     }
4557   else if (thumb_insn_size (inst1) == 4) /* 32-bit instruction */
4558     {
4559       unsigned short inst2;
4560       inst2 = read_memory_unsigned_integer (pc + 2, 2, byte_order_for_code);
4561
4562       /* Default to the next instruction.  */
4563       nextpc = pc + 4;
4564       nextpc = MAKE_THUMB_ADDR (nextpc);
4565
4566       if ((inst1 & 0xf800) == 0xf000 && (inst2 & 0x8000) == 0x8000)
4567         {
4568           /* Branches and miscellaneous control instructions.  */
4569
4570           if ((inst2 & 0x1000) != 0 || (inst2 & 0xd001) == 0xc000)
4571             {
4572               /* B, BL, BLX.  */
4573               int j1, j2, imm1, imm2;
4574
4575               imm1 = sbits (inst1, 0, 10);
4576               imm2 = bits (inst2, 0, 10);
4577               j1 = bit (inst2, 13);
4578               j2 = bit (inst2, 11);
4579
4580               offset = ((imm1 << 12) + (imm2 << 1));
4581               offset ^= ((!j2) << 22) | ((!j1) << 23);
4582
4583               nextpc = pc_val + offset;
4584               /* For BLX make sure to clear the low bits.  */
4585               if (bit (inst2, 12) == 0)
4586                 nextpc = nextpc & 0xfffffffc;
4587             }
4588           else if (inst1 == 0xf3de && (inst2 & 0xff00) == 0x3f00)
4589             {
4590               /* SUBS PC, LR, #imm8.  */
4591               nextpc = get_frame_register_unsigned (frame, ARM_LR_REGNUM);
4592               nextpc -= inst2 & 0x00ff;
4593             }
4594           else if ((inst2 & 0xd000) == 0x8000 && (inst1 & 0x0380) != 0x0380)
4595             {
4596               /* Conditional branch.  */
4597               if (condition_true (bits (inst1, 6, 9), status))
4598                 {
4599                   int sign, j1, j2, imm1, imm2;
4600
4601                   sign = sbits (inst1, 10, 10);
4602                   imm1 = bits (inst1, 0, 5);
4603                   imm2 = bits (inst2, 0, 10);
4604                   j1 = bit (inst2, 13);
4605                   j2 = bit (inst2, 11);
4606
4607                   offset = (sign << 20) + (j2 << 19) + (j1 << 18);
4608                   offset += (imm1 << 12) + (imm2 << 1);
4609
4610                   nextpc = pc_val + offset;
4611                 }
4612             }
4613         }
4614       else if ((inst1 & 0xfe50) == 0xe810)
4615         {
4616           /* Load multiple or RFE.  */
4617           int rn, offset, load_pc = 1;
4618
4619           rn = bits (inst1, 0, 3);
4620           if (bit (inst1, 7) && !bit (inst1, 8))
4621             {
4622               /* LDMIA or POP */
4623               if (!bit (inst2, 15))
4624                 load_pc = 0;
4625               offset = bitcount (inst2) * 4 - 4;
4626             }
4627           else if (!bit (inst1, 7) && bit (inst1, 8))
4628             {
4629               /* LDMDB */
4630               if (!bit (inst2, 15))
4631                 load_pc = 0;
4632               offset = -4;
4633             }
4634           else if (bit (inst1, 7) && bit (inst1, 8))
4635             {
4636               /* RFEIA */
4637               offset = 0;
4638             }
4639           else if (!bit (inst1, 7) && !bit (inst1, 8))
4640             {
4641               /* RFEDB */
4642               offset = -8;
4643             }
4644           else
4645             load_pc = 0;
4646
4647           if (load_pc)
4648             {
4649               CORE_ADDR addr = get_frame_register_unsigned (frame, rn);
4650               nextpc = get_frame_memory_unsigned (frame, addr + offset, 4);
4651             }
4652         }
4653       else if ((inst1 & 0xffef) == 0xea4f && (inst2 & 0xfff0) == 0x0f00)
4654         {
4655           /* MOV PC or MOVS PC.  */
4656           nextpc = get_frame_register_unsigned (frame, bits (inst2, 0, 3));
4657           nextpc = MAKE_THUMB_ADDR (nextpc);
4658         }
4659       else if ((inst1 & 0xff70) == 0xf850 && (inst2 & 0xf000) == 0xf000)
4660         {
4661           /* LDR PC.  */
4662           CORE_ADDR base;
4663           int rn, load_pc = 1;
4664
4665           rn = bits (inst1, 0, 3);
4666           base = get_frame_register_unsigned (frame, rn);
4667           if (rn == ARM_PC_REGNUM)
4668             {
4669               base = (base + 4) & ~(CORE_ADDR) 0x3;
4670               if (bit (inst1, 7))
4671                 base += bits (inst2, 0, 11);
4672               else
4673                 base -= bits (inst2, 0, 11);
4674             }
4675           else if (bit (inst1, 7))
4676             base += bits (inst2, 0, 11);
4677           else if (bit (inst2, 11))
4678             {
4679               if (bit (inst2, 10))
4680                 {
4681                   if (bit (inst2, 9))
4682                     base += bits (inst2, 0, 7);
4683                   else
4684                     base -= bits (inst2, 0, 7);
4685                 }
4686             }
4687           else if ((inst2 & 0x0fc0) == 0x0000)
4688             {
4689               int shift = bits (inst2, 4, 5), rm = bits (inst2, 0, 3);
4690               base += get_frame_register_unsigned (frame, rm) << shift;
4691             }
4692           else
4693             /* Reserved.  */
4694             load_pc = 0;
4695
4696           if (load_pc)
4697             nextpc = get_frame_memory_unsigned (frame, base, 4);
4698         }
4699       else if ((inst1 & 0xfff0) == 0xe8d0 && (inst2 & 0xfff0) == 0xf000)
4700         {
4701           /* TBB.  */
4702           CORE_ADDR tbl_reg, table, offset, length;
4703
4704           tbl_reg = bits (inst1, 0, 3);
4705           if (tbl_reg == 0x0f)
4706             table = pc + 4;  /* Regcache copy of PC isn't right yet.  */
4707           else
4708             table = get_frame_register_unsigned (frame, tbl_reg);
4709
4710           offset = get_frame_register_unsigned (frame, bits (inst2, 0, 3));
4711           length = 2 * get_frame_memory_unsigned (frame, table + offset, 1);
4712           nextpc = pc_val + length;
4713         }
4714       else if ((inst1 & 0xfff0) == 0xe8d0 && (inst2 & 0xfff0) == 0xf010)
4715         {
4716           /* TBH.  */
4717           CORE_ADDR tbl_reg, table, offset, length;
4718
4719           tbl_reg = bits (inst1, 0, 3);
4720           if (tbl_reg == 0x0f)
4721             table = pc + 4;  /* Regcache copy of PC isn't right yet.  */
4722           else
4723             table = get_frame_register_unsigned (frame, tbl_reg);
4724
4725           offset = 2 * get_frame_register_unsigned (frame, bits (inst2, 0, 3));
4726           length = 2 * get_frame_memory_unsigned (frame, table + offset, 2);
4727           nextpc = pc_val + length;
4728         }
4729     }
4730   else if ((inst1 & 0xff00) == 0x4700)  /* bx REG, blx REG */
4731     {
4732       if (bits (inst1, 3, 6) == 0x0f)
4733         nextpc = UNMAKE_THUMB_ADDR (pc_val);
4734       else
4735         nextpc = get_frame_register_unsigned (frame, bits (inst1, 3, 6));
4736     }
4737   else if ((inst1 & 0xff87) == 0x4687)  /* mov pc, REG */
4738     {
4739       if (bits (inst1, 3, 6) == 0x0f)
4740         nextpc = pc_val;
4741       else
4742         nextpc = get_frame_register_unsigned (frame, bits (inst1, 3, 6));
4743
4744       nextpc = MAKE_THUMB_ADDR (nextpc);
4745     }
4746   else if ((inst1 & 0xf500) == 0xb100)
4747     {
4748       /* CBNZ or CBZ.  */
4749       int imm = (bit (inst1, 9) << 6) + (bits (inst1, 3, 7) << 1);
4750       ULONGEST reg = get_frame_register_unsigned (frame, bits (inst1, 0, 2));
4751
4752       if (bit (inst1, 11) && reg != 0)
4753         nextpc = pc_val + imm;
4754       else if (!bit (inst1, 11) && reg == 0)
4755         nextpc = pc_val + imm;
4756     }
4757   return nextpc;
4758 }
4759
4760 /* Get the raw next address.  PC is the current program counter, in 
4761    FRAME, which is assumed to be executing in ARM mode.
4762
4763    The value returned has the execution state of the next instruction 
4764    encoded in it.  Use IS_THUMB_ADDR () to see whether the instruction is
4765    in Thumb-State, and gdbarch_addr_bits_remove () to get the plain memory
4766    address.  */
4767
4768 static CORE_ADDR
4769 arm_get_next_pc_raw (struct frame_info *frame, CORE_ADDR pc)
4770 {
4771   struct gdbarch *gdbarch = get_frame_arch (frame);
4772   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
4773   enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
4774   unsigned long pc_val;
4775   unsigned long this_instr;
4776   unsigned long status;
4777   CORE_ADDR nextpc;
4778
4779   pc_val = (unsigned long) pc;
4780   this_instr = read_memory_unsigned_integer (pc, 4, byte_order_for_code);
4781
4782   status = get_frame_register_unsigned (frame, ARM_PS_REGNUM);
4783   nextpc = (CORE_ADDR) (pc_val + 4);    /* Default case */
4784
4785   if (bits (this_instr, 28, 31) == INST_NV)
4786     switch (bits (this_instr, 24, 27))
4787       {
4788       case 0xa:
4789       case 0xb:
4790         {
4791           /* Branch with Link and change to Thumb.  */
4792           nextpc = BranchDest (pc, this_instr);
4793           nextpc |= bit (this_instr, 24) << 1;
4794           nextpc = MAKE_THUMB_ADDR (nextpc);
4795           break;
4796         }
4797       case 0xc:
4798       case 0xd:
4799       case 0xe:
4800         /* Coprocessor register transfer.  */
4801         if (bits (this_instr, 12, 15) == 15)
4802           error (_("Invalid update to pc in instruction"));
4803         break;
4804       }
4805   else if (condition_true (bits (this_instr, 28, 31), status))
4806     {
4807       switch (bits (this_instr, 24, 27))
4808         {
4809         case 0x0:
4810         case 0x1:                       /* data processing */
4811         case 0x2:
4812         case 0x3:
4813           {
4814             unsigned long operand1, operand2, result = 0;
4815             unsigned long rn;
4816             int c;
4817
4818             if (bits (this_instr, 12, 15) != 15)
4819               break;
4820
4821             if (bits (this_instr, 22, 25) == 0
4822                 && bits (this_instr, 4, 7) == 9)        /* multiply */
4823               error (_("Invalid update to pc in instruction"));
4824
4825             /* BX <reg>, BLX <reg> */
4826             if (bits (this_instr, 4, 27) == 0x12fff1
4827                 || bits (this_instr, 4, 27) == 0x12fff3)
4828               {
4829                 rn = bits (this_instr, 0, 3);
4830                 nextpc = ((rn == ARM_PC_REGNUM)
4831                           ? (pc_val + 8)
4832                           : get_frame_register_unsigned (frame, rn));
4833
4834                 return nextpc;
4835               }
4836
4837             /* Multiply into PC.  */
4838             c = (status & FLAG_C) ? 1 : 0;
4839             rn = bits (this_instr, 16, 19);
4840             operand1 = ((rn == ARM_PC_REGNUM)
4841                         ? (pc_val + 8)
4842                         : get_frame_register_unsigned (frame, rn));
4843
4844             if (bit (this_instr, 25))
4845               {
4846                 unsigned long immval = bits (this_instr, 0, 7);
4847                 unsigned long rotate = 2 * bits (this_instr, 8, 11);
4848                 operand2 = ((immval >> rotate) | (immval << (32 - rotate)))
4849                   & 0xffffffff;
4850               }
4851             else                /* operand 2 is a shifted register.  */
4852               operand2 = shifted_reg_val (frame, this_instr, c,
4853                                           pc_val, status);
4854
4855             switch (bits (this_instr, 21, 24))
4856               {
4857               case 0x0: /*and */
4858                 result = operand1 & operand2;
4859                 break;
4860
4861               case 0x1: /*eor */
4862                 result = operand1 ^ operand2;
4863                 break;
4864
4865               case 0x2: /*sub */
4866                 result = operand1 - operand2;
4867                 break;
4868
4869               case 0x3: /*rsb */
4870                 result = operand2 - operand1;
4871                 break;
4872
4873               case 0x4: /*add */
4874                 result = operand1 + operand2;
4875                 break;
4876
4877               case 0x5: /*adc */
4878                 result = operand1 + operand2 + c;
4879                 break;
4880
4881               case 0x6: /*sbc */
4882                 result = operand1 - operand2 + c;
4883                 break;
4884
4885               case 0x7: /*rsc */
4886                 result = operand2 - operand1 + c;
4887                 break;
4888
4889               case 0x8:
4890               case 0x9:
4891               case 0xa:
4892               case 0xb: /* tst, teq, cmp, cmn */
4893                 result = (unsigned long) nextpc;
4894                 break;
4895
4896               case 0xc: /*orr */
4897                 result = operand1 | operand2;
4898                 break;
4899
4900               case 0xd: /*mov */
4901                 /* Always step into a function.  */
4902                 result = operand2;
4903                 break;
4904
4905               case 0xe: /*bic */
4906                 result = operand1 & ~operand2;
4907                 break;
4908
4909               case 0xf: /*mvn */
4910                 result = ~operand2;
4911                 break;
4912               }
4913
4914             /* In 26-bit APCS the bottom two bits of the result are 
4915                ignored, and we always end up in ARM state.  */
4916             if (!arm_apcs_32)
4917               nextpc = arm_addr_bits_remove (gdbarch, result);
4918             else
4919               nextpc = result;
4920
4921             break;
4922           }
4923
4924         case 0x4:
4925         case 0x5:               /* data transfer */
4926         case 0x6:
4927         case 0x7:
4928           if (bit (this_instr, 20))
4929             {
4930               /* load */
4931               if (bits (this_instr, 12, 15) == 15)
4932                 {
4933                   /* rd == pc */
4934                   unsigned long rn;
4935                   unsigned long base;
4936
4937                   if (bit (this_instr, 22))
4938                     error (_("Invalid update to pc in instruction"));
4939
4940                   /* byte write to PC */
4941                   rn = bits (this_instr, 16, 19);
4942                   base = ((rn == ARM_PC_REGNUM)
4943                           ? (pc_val + 8)
4944                           : get_frame_register_unsigned (frame, rn));
4945
4946                   if (bit (this_instr, 24))
4947                     {
4948                       /* pre-indexed */
4949                       int c = (status & FLAG_C) ? 1 : 0;
4950                       unsigned long offset =
4951                       (bit (this_instr, 25)
4952                        ? shifted_reg_val (frame, this_instr, c, pc_val, status)
4953                        : bits (this_instr, 0, 11));
4954
4955                       if (bit (this_instr, 23))
4956                         base += offset;
4957                       else
4958                         base -= offset;
4959                     }
4960                   nextpc =
4961                     (CORE_ADDR) read_memory_unsigned_integer ((CORE_ADDR) base,
4962                                                               4, byte_order);
4963                 }
4964             }
4965           break;
4966
4967         case 0x8:
4968         case 0x9:               /* block transfer */
4969           if (bit (this_instr, 20))
4970             {
4971               /* LDM */
4972               if (bit (this_instr, 15))
4973                 {
4974                   /* loading pc */
4975                   int offset = 0;
4976                   unsigned long rn_val
4977                     = get_frame_register_unsigned (frame,
4978                                                    bits (this_instr, 16, 19));
4979
4980                   if (bit (this_instr, 23))
4981                     {
4982                       /* up */
4983                       unsigned long reglist = bits (this_instr, 0, 14);
4984                       offset = bitcount (reglist) * 4;
4985                       if (bit (this_instr, 24))         /* pre */
4986                         offset += 4;
4987                     }
4988                   else if (bit (this_instr, 24))
4989                     offset = -4;
4990
4991                   nextpc =
4992                     (CORE_ADDR) read_memory_unsigned_integer ((CORE_ADDR)
4993                                                               (rn_val + offset),
4994                                                               4, byte_order);
4995                 }
4996             }
4997           break;
4998
4999         case 0xb:               /* branch & link */
5000         case 0xa:               /* branch */
5001           {
5002             nextpc = BranchDest (pc, this_instr);
5003             break;
5004           }
5005
5006         case 0xc:
5007         case 0xd:
5008         case 0xe:               /* coproc ops */
5009           break;
5010         case 0xf:               /* SWI */
5011           {
5012             struct gdbarch_tdep *tdep;
5013             tdep = gdbarch_tdep (gdbarch);
5014
5015             if (tdep->syscall_next_pc != NULL)
5016               nextpc = tdep->syscall_next_pc (frame);
5017
5018           }
5019           break;
5020
5021         default:
5022           fprintf_filtered (gdb_stderr, _("Bad bit-field extraction\n"));
5023           return (pc);
5024         }
5025     }
5026
5027   return nextpc;
5028 }
5029
5030 /* Determine next PC after current instruction executes.  Will call either
5031    arm_get_next_pc_raw or thumb_get_next_pc_raw.  Error out if infinite
5032    loop is detected.  */
5033
5034 CORE_ADDR
5035 arm_get_next_pc (struct frame_info *frame, CORE_ADDR pc)
5036 {
5037   CORE_ADDR nextpc;
5038
5039   if (arm_frame_is_thumb (frame))
5040     nextpc = thumb_get_next_pc_raw (frame, pc);
5041   else
5042     nextpc = arm_get_next_pc_raw (frame, pc);
5043
5044   return nextpc;
5045 }
5046
5047 /* Like insert_single_step_breakpoint, but make sure we use a breakpoint
5048    of the appropriate mode (as encoded in the PC value), even if this
5049    differs from what would be expected according to the symbol tables.  */
5050
5051 void
5052 arm_insert_single_step_breakpoint (struct gdbarch *gdbarch,
5053                                    struct address_space *aspace,
5054                                    CORE_ADDR pc)
5055 {
5056   struct cleanup *old_chain
5057     = make_cleanup_restore_integer (&arm_override_mode);
5058
5059   arm_override_mode = IS_THUMB_ADDR (pc);
5060   pc = gdbarch_addr_bits_remove (gdbarch, pc);
5061
5062   insert_single_step_breakpoint (gdbarch, aspace, pc);
5063
5064   do_cleanups (old_chain);
5065 }
5066
5067 /* Checks for an atomic sequence of instructions beginning with a LDREX{,B,H,D}
5068    instruction and ending with a STREX{,B,H,D} instruction.  If such a sequence
5069    is found, attempt to step through it.  A breakpoint is placed at the end of
5070    the sequence.  */
5071
5072 static int
5073 thumb_deal_with_atomic_sequence_raw (struct frame_info *frame)
5074 {
5075   struct gdbarch *gdbarch = get_frame_arch (frame);
5076   struct address_space *aspace = get_frame_address_space (frame);
5077   enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
5078   CORE_ADDR pc = get_frame_pc (frame);
5079   CORE_ADDR breaks[2] = {-1, -1};
5080   CORE_ADDR loc = pc;
5081   unsigned short insn1, insn2;
5082   int insn_count;
5083   int index;
5084   int last_breakpoint = 0; /* Defaults to 0 (no breakpoints placed).  */
5085   const int atomic_sequence_length = 16; /* Instruction sequence length.  */
5086   ULONGEST status, itstate;
5087
5088   /* We currently do not support atomic sequences within an IT block.  */
5089   status = get_frame_register_unsigned (frame, ARM_PS_REGNUM);
5090   itstate = ((status >> 8) & 0xfc) | ((status >> 25) & 0x3);
5091   if (itstate & 0x0f)
5092     return 0;
5093
5094   /* Assume all atomic sequences start with a ldrex{,b,h,d} instruction.  */
5095   insn1 = read_memory_unsigned_integer (loc, 2, byte_order_for_code);
5096   loc += 2;
5097   if (thumb_insn_size (insn1) != 4)
5098     return 0;
5099
5100   insn2 = read_memory_unsigned_integer (loc, 2, byte_order_for_code);
5101   loc += 2;
5102   if (!((insn1 & 0xfff0) == 0xe850
5103         || ((insn1 & 0xfff0) == 0xe8d0 && (insn2 & 0x00c0) == 0x0040)))
5104     return 0;
5105
5106   /* Assume that no atomic sequence is longer than "atomic_sequence_length"
5107      instructions.  */
5108   for (insn_count = 0; insn_count < atomic_sequence_length; ++insn_count)
5109     {
5110       insn1 = read_memory_unsigned_integer (loc, 2, byte_order_for_code);
5111       loc += 2;
5112
5113       if (thumb_insn_size (insn1) != 4)
5114         {
5115           /* Assume that there is at most one conditional branch in the
5116              atomic sequence.  If a conditional branch is found, put a
5117              breakpoint in its destination address.  */
5118           if ((insn1 & 0xf000) == 0xd000 && bits (insn1, 8, 11) != 0x0f)
5119             {
5120               if (last_breakpoint > 0)
5121                 return 0; /* More than one conditional branch found,
5122                              fallback to the standard code.  */
5123
5124               breaks[1] = loc + 2 + (sbits (insn1, 0, 7) << 1);
5125               last_breakpoint++;
5126             }
5127
5128           /* We do not support atomic sequences that use any *other*
5129              instructions but conditional branches to change the PC.
5130              Fall back to standard code to avoid losing control of
5131              execution.  */
5132           else if (thumb_instruction_changes_pc (insn1))
5133             return 0;
5134         }
5135       else
5136         {
5137           insn2 = read_memory_unsigned_integer (loc, 2, byte_order_for_code);
5138           loc += 2;
5139
5140           /* Assume that there is at most one conditional branch in the
5141              atomic sequence.  If a conditional branch is found, put a
5142              breakpoint in its destination address.  */
5143           if ((insn1 & 0xf800) == 0xf000
5144               && (insn2 & 0xd000) == 0x8000
5145               && (insn1 & 0x0380) != 0x0380)
5146             {
5147               int sign, j1, j2, imm1, imm2;
5148               unsigned int offset;
5149
5150               sign = sbits (insn1, 10, 10);
5151               imm1 = bits (insn1, 0, 5);
5152               imm2 = bits (insn2, 0, 10);
5153               j1 = bit (insn2, 13);
5154               j2 = bit (insn2, 11);
5155
5156               offset = (sign << 20) + (j2 << 19) + (j1 << 18);
5157               offset += (imm1 << 12) + (imm2 << 1);
5158
5159               if (last_breakpoint > 0)
5160                 return 0; /* More than one conditional branch found,
5161                              fallback to the standard code.  */
5162
5163               breaks[1] = loc + offset;
5164               last_breakpoint++;
5165             }
5166
5167           /* We do not support atomic sequences that use any *other*
5168              instructions but conditional branches to change the PC.
5169              Fall back to standard code to avoid losing control of
5170              execution.  */
5171           else if (thumb2_instruction_changes_pc (insn1, insn2))
5172             return 0;
5173
5174           /* If we find a strex{,b,h,d}, we're done.  */
5175           if ((insn1 & 0xfff0) == 0xe840
5176               || ((insn1 & 0xfff0) == 0xe8c0 && (insn2 & 0x00c0) == 0x0040))
5177             break;
5178         }
5179     }
5180
5181   /* If we didn't find the strex{,b,h,d}, we cannot handle the sequence.  */
5182   if (insn_count == atomic_sequence_length)
5183     return 0;
5184
5185   /* Insert a breakpoint right after the end of the atomic sequence.  */
5186   breaks[0] = loc;
5187
5188   /* Check for duplicated breakpoints.  Check also for a breakpoint
5189      placed (branch instruction's destination) anywhere in sequence.  */
5190   if (last_breakpoint
5191       && (breaks[1] == breaks[0]
5192           || (breaks[1] >= pc && breaks[1] < loc)))
5193     last_breakpoint = 0;
5194
5195   /* Effectively inserts the breakpoints.  */
5196   for (index = 0; index <= last_breakpoint; index++)
5197     arm_insert_single_step_breakpoint (gdbarch, aspace,
5198                                        MAKE_THUMB_ADDR (breaks[index]));
5199
5200   return 1;
5201 }
5202
5203 static int
5204 arm_deal_with_atomic_sequence_raw (struct frame_info *frame)
5205 {
5206   struct gdbarch *gdbarch = get_frame_arch (frame);
5207   struct address_space *aspace = get_frame_address_space (frame);
5208   enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
5209   CORE_ADDR pc = get_frame_pc (frame);
5210   CORE_ADDR breaks[2] = {-1, -1};
5211   CORE_ADDR loc = pc;
5212   unsigned int insn;
5213   int insn_count;
5214   int index;
5215   int last_breakpoint = 0; /* Defaults to 0 (no breakpoints placed).  */
5216   const int atomic_sequence_length = 16; /* Instruction sequence length.  */
5217
5218   /* Assume all atomic sequences start with a ldrex{,b,h,d} instruction.
5219      Note that we do not currently support conditionally executed atomic
5220      instructions.  */
5221   insn = read_memory_unsigned_integer (loc, 4, byte_order_for_code);
5222   loc += 4;
5223   if ((insn & 0xff9000f0) != 0xe1900090)
5224     return 0;
5225
5226   /* Assume that no atomic sequence is longer than "atomic_sequence_length"
5227      instructions.  */
5228   for (insn_count = 0; insn_count < atomic_sequence_length; ++insn_count)
5229     {
5230       insn = read_memory_unsigned_integer (loc, 4, byte_order_for_code);
5231       loc += 4;
5232
5233       /* Assume that there is at most one conditional branch in the atomic
5234          sequence.  If a conditional branch is found, put a breakpoint in
5235          its destination address.  */
5236       if (bits (insn, 24, 27) == 0xa)
5237         {
5238           if (last_breakpoint > 0)
5239             return 0; /* More than one conditional branch found, fallback
5240                          to the standard single-step code.  */
5241
5242           breaks[1] = BranchDest (loc - 4, insn);
5243           last_breakpoint++;
5244         }
5245
5246       /* We do not support atomic sequences that use any *other* instructions
5247          but conditional branches to change the PC.  Fall back to standard
5248          code to avoid losing control of execution.  */
5249       else if (arm_instruction_changes_pc (insn))
5250         return 0;
5251
5252       /* If we find a strex{,b,h,d}, we're done.  */
5253       if ((insn & 0xff9000f0) == 0xe1800090)
5254         break;
5255     }
5256
5257   /* If we didn't find the strex{,b,h,d}, we cannot handle the sequence.  */
5258   if (insn_count == atomic_sequence_length)
5259     return 0;
5260
5261   /* Insert a breakpoint right after the end of the atomic sequence.  */
5262   breaks[0] = loc;
5263
5264   /* Check for duplicated breakpoints.  Check also for a breakpoint
5265      placed (branch instruction's destination) anywhere in sequence.  */
5266   if (last_breakpoint
5267       && (breaks[1] == breaks[0]
5268           || (breaks[1] >= pc && breaks[1] < loc)))
5269     last_breakpoint = 0;
5270
5271   /* Effectively inserts the breakpoints.  */
5272   for (index = 0; index <= last_breakpoint; index++)
5273     arm_insert_single_step_breakpoint (gdbarch, aspace, breaks[index]);
5274
5275   return 1;
5276 }
5277
5278 int
5279 arm_deal_with_atomic_sequence (struct frame_info *frame)
5280 {
5281   if (arm_frame_is_thumb (frame))
5282     return thumb_deal_with_atomic_sequence_raw (frame);
5283   else
5284     return arm_deal_with_atomic_sequence_raw (frame);
5285 }
5286
5287 /* single_step() is called just before we want to resume the inferior,
5288    if we want to single-step it but there is no hardware or kernel
5289    single-step support.  We find the target of the coming instruction
5290    and breakpoint it.  */
5291
5292 int
5293 arm_software_single_step (struct frame_info *frame)
5294 {
5295   struct gdbarch *gdbarch = get_frame_arch (frame);
5296   struct address_space *aspace = get_frame_address_space (frame);
5297   CORE_ADDR next_pc;
5298
5299   if (arm_deal_with_atomic_sequence (frame))
5300     return 1;
5301
5302   next_pc = arm_get_next_pc (frame, get_frame_pc (frame));
5303   arm_insert_single_step_breakpoint (gdbarch, aspace, next_pc);
5304
5305   return 1;
5306 }
5307
5308 /* Given BUF, which is OLD_LEN bytes ending at ENDADDR, expand
5309    the buffer to be NEW_LEN bytes ending at ENDADDR.  Return
5310    NULL if an error occurs.  BUF is freed.  */
5311
5312 static gdb_byte *
5313 extend_buffer_earlier (gdb_byte *buf, CORE_ADDR endaddr,
5314                        int old_len, int new_len)
5315 {
5316   gdb_byte *new_buf;
5317   int bytes_to_read = new_len - old_len;
5318
5319   new_buf = xmalloc (new_len);
5320   memcpy (new_buf + bytes_to_read, buf, old_len);
5321   xfree (buf);
5322   if (target_read_memory (endaddr - new_len, new_buf, bytes_to_read) != 0)
5323     {
5324       xfree (new_buf);
5325       return NULL;
5326     }
5327   return new_buf;
5328 }
5329
5330 /* An IT block is at most the 2-byte IT instruction followed by
5331    four 4-byte instructions.  The furthest back we must search to
5332    find an IT block that affects the current instruction is thus
5333    2 + 3 * 4 == 14 bytes.  */
5334 #define MAX_IT_BLOCK_PREFIX 14
5335
5336 /* Use a quick scan if there are more than this many bytes of
5337    code.  */
5338 #define IT_SCAN_THRESHOLD 32
5339
5340 /* Adjust a breakpoint's address to move breakpoints out of IT blocks.
5341    A breakpoint in an IT block may not be hit, depending on the
5342    condition flags.  */
5343 static CORE_ADDR
5344 arm_adjust_breakpoint_address (struct gdbarch *gdbarch, CORE_ADDR bpaddr)
5345 {
5346   gdb_byte *buf;
5347   char map_type;
5348   CORE_ADDR boundary, func_start;
5349   int buf_len;
5350   enum bfd_endian order = gdbarch_byte_order_for_code (gdbarch);
5351   int i, any, last_it, last_it_count;
5352
5353   /* If we are using BKPT breakpoints, none of this is necessary.  */
5354   if (gdbarch_tdep (gdbarch)->thumb2_breakpoint == NULL)
5355     return bpaddr;
5356
5357   /* ARM mode does not have this problem.  */
5358   if (!arm_pc_is_thumb (gdbarch, bpaddr))
5359     return bpaddr;
5360
5361   /* We are setting a breakpoint in Thumb code that could potentially
5362      contain an IT block.  The first step is to find how much Thumb
5363      code there is; we do not need to read outside of known Thumb
5364      sequences.  */
5365   map_type = arm_find_mapping_symbol (bpaddr, &boundary);
5366   if (map_type == 0)
5367     /* Thumb-2 code must have mapping symbols to have a chance.  */
5368     return bpaddr;
5369
5370   bpaddr = gdbarch_addr_bits_remove (gdbarch, bpaddr);
5371
5372   if (find_pc_partial_function (bpaddr, NULL, &func_start, NULL)
5373       && func_start > boundary)
5374     boundary = func_start;
5375
5376   /* Search for a candidate IT instruction.  We have to do some fancy
5377      footwork to distinguish a real IT instruction from the second
5378      half of a 32-bit instruction, but there is no need for that if
5379      there's no candidate.  */
5380   buf_len = min (bpaddr - boundary, MAX_IT_BLOCK_PREFIX);
5381   if (buf_len == 0)
5382     /* No room for an IT instruction.  */
5383     return bpaddr;
5384
5385   buf = xmalloc (buf_len);
5386   if (target_read_memory (bpaddr - buf_len, buf, buf_len) != 0)
5387     return bpaddr;
5388   any = 0;
5389   for (i = 0; i < buf_len; i += 2)
5390     {
5391       unsigned short inst1 = extract_unsigned_integer (&buf[i], 2, order);
5392       if ((inst1 & 0xff00) == 0xbf00 && (inst1 & 0x000f) != 0)
5393         {
5394           any = 1;
5395           break;
5396         }
5397     }
5398   if (any == 0)
5399     {
5400       xfree (buf);
5401       return bpaddr;
5402     }
5403
5404   /* OK, the code bytes before this instruction contain at least one
5405      halfword which resembles an IT instruction.  We know that it's
5406      Thumb code, but there are still two possibilities.  Either the
5407      halfword really is an IT instruction, or it is the second half of
5408      a 32-bit Thumb instruction.  The only way we can tell is to
5409      scan forwards from a known instruction boundary.  */
5410   if (bpaddr - boundary > IT_SCAN_THRESHOLD)
5411     {
5412       int definite;
5413
5414       /* There's a lot of code before this instruction.  Start with an
5415          optimistic search; it's easy to recognize halfwords that can
5416          not be the start of a 32-bit instruction, and use that to
5417          lock on to the instruction boundaries.  */
5418       buf = extend_buffer_earlier (buf, bpaddr, buf_len, IT_SCAN_THRESHOLD);
5419       if (buf == NULL)
5420         return bpaddr;
5421       buf_len = IT_SCAN_THRESHOLD;
5422
5423       definite = 0;
5424       for (i = 0; i < buf_len - sizeof (buf) && ! definite; i += 2)
5425         {
5426           unsigned short inst1 = extract_unsigned_integer (&buf[i], 2, order);
5427           if (thumb_insn_size (inst1) == 2)
5428             {
5429               definite = 1;
5430               break;
5431             }
5432         }
5433
5434       /* At this point, if DEFINITE, BUF[I] is the first place we
5435          are sure that we know the instruction boundaries, and it is far
5436          enough from BPADDR that we could not miss an IT instruction
5437          affecting BPADDR.  If ! DEFINITE, give up - start from a
5438          known boundary.  */
5439       if (! definite)
5440         {
5441           buf = extend_buffer_earlier (buf, bpaddr, buf_len,
5442                                        bpaddr - boundary);
5443           if (buf == NULL)
5444             return bpaddr;
5445           buf_len = bpaddr - boundary;
5446           i = 0;
5447         }
5448     }
5449   else
5450     {
5451       buf = extend_buffer_earlier (buf, bpaddr, buf_len, bpaddr - boundary);
5452       if (buf == NULL)
5453         return bpaddr;
5454       buf_len = bpaddr - boundary;
5455       i = 0;
5456     }
5457
5458   /* Scan forwards.  Find the last IT instruction before BPADDR.  */
5459   last_it = -1;
5460   last_it_count = 0;
5461   while (i < buf_len)
5462     {
5463       unsigned short inst1 = extract_unsigned_integer (&buf[i], 2, order);
5464       last_it_count--;
5465       if ((inst1 & 0xff00) == 0xbf00 && (inst1 & 0x000f) != 0)
5466         {
5467           last_it = i;
5468           if (inst1 & 0x0001)
5469             last_it_count = 4;
5470           else if (inst1 & 0x0002)
5471             last_it_count = 3;
5472           else if (inst1 & 0x0004)
5473             last_it_count = 2;
5474           else
5475             last_it_count = 1;
5476         }
5477       i += thumb_insn_size (inst1);
5478     }
5479
5480   xfree (buf);
5481
5482   if (last_it == -1)
5483     /* There wasn't really an IT instruction after all.  */
5484     return bpaddr;
5485
5486   if (last_it_count < 1)
5487     /* It was too far away.  */
5488     return bpaddr;
5489
5490   /* This really is a trouble spot.  Move the breakpoint to the IT
5491      instruction.  */
5492   return bpaddr - buf_len + last_it;
5493 }
5494
5495 /* ARM displaced stepping support.
5496
5497    Generally ARM displaced stepping works as follows:
5498
5499    1. When an instruction is to be single-stepped, it is first decoded by
5500       arm_process_displaced_insn (called from arm_displaced_step_copy_insn).
5501       Depending on the type of instruction, it is then copied to a scratch
5502       location, possibly in a modified form.  The copy_* set of functions
5503       performs such modification, as necessary.  A breakpoint is placed after
5504       the modified instruction in the scratch space to return control to GDB.
5505       Note in particular that instructions which modify the PC will no longer
5506       do so after modification.
5507
5508    2. The instruction is single-stepped, by setting the PC to the scratch
5509       location address, and resuming.  Control returns to GDB when the
5510       breakpoint is hit.
5511
5512    3. A cleanup function (cleanup_*) is called corresponding to the copy_*
5513       function used for the current instruction.  This function's job is to
5514       put the CPU/memory state back to what it would have been if the
5515       instruction had been executed unmodified in its original location.  */
5516
5517 /* NOP instruction (mov r0, r0).  */
5518 #define ARM_NOP                         0xe1a00000
5519 #define THUMB_NOP 0x4600
5520
5521 /* Helper for register reads for displaced stepping.  In particular, this
5522    returns the PC as it would be seen by the instruction at its original
5523    location.  */
5524
5525 ULONGEST
5526 displaced_read_reg (struct regcache *regs, struct displaced_step_closure *dsc,
5527                     int regno)
5528 {
5529   ULONGEST ret;
5530   CORE_ADDR from = dsc->insn_addr;
5531
5532   if (regno == ARM_PC_REGNUM)
5533     {
5534       /* Compute pipeline offset:
5535          - When executing an ARM instruction, PC reads as the address of the
5536          current instruction plus 8.
5537          - When executing a Thumb instruction, PC reads as the address of the
5538          current instruction plus 4.  */
5539
5540       if (!dsc->is_thumb)
5541         from += 8;
5542       else
5543         from += 4;
5544
5545       if (debug_displaced)
5546         fprintf_unfiltered (gdb_stdlog, "displaced: read pc value %.8lx\n",
5547                             (unsigned long) from);
5548       return (ULONGEST) from;
5549     }
5550   else
5551     {
5552       regcache_cooked_read_unsigned (regs, regno, &ret);
5553       if (debug_displaced)
5554         fprintf_unfiltered (gdb_stdlog, "displaced: read r%d value %.8lx\n",
5555                             regno, (unsigned long) ret);
5556       return ret;
5557     }
5558 }
5559
5560 static int
5561 displaced_in_arm_mode (struct regcache *regs)
5562 {
5563   ULONGEST ps;
5564   ULONGEST t_bit = arm_psr_thumb_bit (get_regcache_arch (regs));
5565
5566   regcache_cooked_read_unsigned (regs, ARM_PS_REGNUM, &ps);
5567
5568   return (ps & t_bit) == 0;
5569 }
5570
5571 /* Write to the PC as from a branch instruction.  */
5572
5573 static void
5574 branch_write_pc (struct regcache *regs, struct displaced_step_closure *dsc,
5575                  ULONGEST val)
5576 {
5577   if (!dsc->is_thumb)
5578     /* Note: If bits 0/1 are set, this branch would be unpredictable for
5579        architecture versions < 6.  */
5580     regcache_cooked_write_unsigned (regs, ARM_PC_REGNUM,
5581                                     val & ~(ULONGEST) 0x3);
5582   else
5583     regcache_cooked_write_unsigned (regs, ARM_PC_REGNUM,
5584                                     val & ~(ULONGEST) 0x1);
5585 }
5586
5587 /* Write to the PC as from a branch-exchange instruction.  */
5588
5589 static void
5590 bx_write_pc (struct regcache *regs, ULONGEST val)
5591 {
5592   ULONGEST ps;
5593   ULONGEST t_bit = arm_psr_thumb_bit (get_regcache_arch (regs));
5594
5595   regcache_cooked_read_unsigned (regs, ARM_PS_REGNUM, &ps);
5596
5597   if ((val & 1) == 1)
5598     {
5599       regcache_cooked_write_unsigned (regs, ARM_PS_REGNUM, ps | t_bit);
5600       regcache_cooked_write_unsigned (regs, ARM_PC_REGNUM, val & 0xfffffffe);
5601     }
5602   else if ((val & 2) == 0)
5603     {
5604       regcache_cooked_write_unsigned (regs, ARM_PS_REGNUM, ps & ~t_bit);
5605       regcache_cooked_write_unsigned (regs, ARM_PC_REGNUM, val);
5606     }
5607   else
5608     {
5609       /* Unpredictable behaviour.  Try to do something sensible (switch to ARM
5610           mode, align dest to 4 bytes).  */
5611       warning (_("Single-stepping BX to non-word-aligned ARM instruction."));
5612       regcache_cooked_write_unsigned (regs, ARM_PS_REGNUM, ps & ~t_bit);
5613       regcache_cooked_write_unsigned (regs, ARM_PC_REGNUM, val & 0xfffffffc);
5614     }
5615 }
5616
5617 /* Write to the PC as if from a load instruction.  */
5618
5619 static void
5620 load_write_pc (struct regcache *regs, struct displaced_step_closure *dsc,
5621                ULONGEST val)
5622 {
5623   if (DISPLACED_STEPPING_ARCH_VERSION >= 5)
5624     bx_write_pc (regs, val);
5625   else
5626     branch_write_pc (regs, dsc, val);
5627 }
5628
5629 /* Write to the PC as if from an ALU instruction.  */
5630
5631 static void
5632 alu_write_pc (struct regcache *regs, struct displaced_step_closure *dsc,
5633               ULONGEST val)
5634 {
5635   if (DISPLACED_STEPPING_ARCH_VERSION >= 7 && !dsc->is_thumb)
5636     bx_write_pc (regs, val);
5637   else
5638     branch_write_pc (regs, dsc, val);
5639 }
5640
5641 /* Helper for writing to registers for displaced stepping.  Writing to the PC
5642    has a varying effects depending on the instruction which does the write:
5643    this is controlled by the WRITE_PC argument.  */
5644
5645 void
5646 displaced_write_reg (struct regcache *regs, struct displaced_step_closure *dsc,
5647                      int regno, ULONGEST val, enum pc_write_style write_pc)
5648 {
5649   if (regno == ARM_PC_REGNUM)
5650     {
5651       if (debug_displaced)
5652         fprintf_unfiltered (gdb_stdlog, "displaced: writing pc %.8lx\n",
5653                             (unsigned long) val);
5654       switch (write_pc)
5655         {
5656         case BRANCH_WRITE_PC:
5657           branch_write_pc (regs, dsc, val);
5658           break;
5659
5660         case BX_WRITE_PC:
5661           bx_write_pc (regs, val);
5662           break;
5663
5664         case LOAD_WRITE_PC:
5665           load_write_pc (regs, dsc, val);
5666           break;
5667
5668         case ALU_WRITE_PC:
5669           alu_write_pc (regs, dsc, val);
5670           break;
5671
5672         case CANNOT_WRITE_PC:
5673           warning (_("Instruction wrote to PC in an unexpected way when "
5674                      "single-stepping"));
5675           break;
5676
5677         default:
5678           internal_error (__FILE__, __LINE__,
5679                           _("Invalid argument to displaced_write_reg"));
5680         }
5681
5682       dsc->wrote_to_pc = 1;
5683     }
5684   else
5685     {
5686       if (debug_displaced)
5687         fprintf_unfiltered (gdb_stdlog, "displaced: writing r%d value %.8lx\n",
5688                             regno, (unsigned long) val);
5689       regcache_cooked_write_unsigned (regs, regno, val);
5690     }
5691 }
5692
5693 /* This function is used to concisely determine if an instruction INSN
5694    references PC.  Register fields of interest in INSN should have the
5695    corresponding fields of BITMASK set to 0b1111.  The function
5696    returns return 1 if any of these fields in INSN reference the PC
5697    (also 0b1111, r15), else it returns 0.  */
5698
5699 static int
5700 insn_references_pc (uint32_t insn, uint32_t bitmask)
5701 {
5702   uint32_t lowbit = 1;
5703
5704   while (bitmask != 0)
5705     {
5706       uint32_t mask;
5707
5708       for (; lowbit && (bitmask & lowbit) == 0; lowbit <<= 1)
5709         ;
5710
5711       if (!lowbit)
5712         break;
5713
5714       mask = lowbit * 0xf;
5715
5716       if ((insn & mask) == mask)
5717         return 1;
5718
5719       bitmask &= ~mask;
5720     }
5721
5722   return 0;
5723 }
5724
5725 /* The simplest copy function.  Many instructions have the same effect no
5726    matter what address they are executed at: in those cases, use this.  */
5727
5728 static int
5729 arm_copy_unmodified (struct gdbarch *gdbarch, uint32_t insn,
5730                      const char *iname, struct displaced_step_closure *dsc)
5731 {
5732   if (debug_displaced)
5733     fprintf_unfiltered (gdb_stdlog, "displaced: copying insn %.8lx, "
5734                         "opcode/class '%s' unmodified\n", (unsigned long) insn,
5735                         iname);
5736
5737   dsc->modinsn[0] = insn;
5738
5739   return 0;
5740 }
5741
5742 static int
5743 thumb_copy_unmodified_32bit (struct gdbarch *gdbarch, uint16_t insn1,
5744                              uint16_t insn2, const char *iname,
5745                              struct displaced_step_closure *dsc)
5746 {
5747   if (debug_displaced)
5748     fprintf_unfiltered (gdb_stdlog, "displaced: copying insn %.4x %.4x, "
5749                         "opcode/class '%s' unmodified\n", insn1, insn2,
5750                         iname);
5751
5752   dsc->modinsn[0] = insn1;
5753   dsc->modinsn[1] = insn2;
5754   dsc->numinsns = 2;
5755
5756   return 0;
5757 }
5758
5759 /* Copy 16-bit Thumb(Thumb and 16-bit Thumb-2) instruction without any
5760    modification.  */
5761 static int
5762 thumb_copy_unmodified_16bit (struct gdbarch *gdbarch, unsigned int insn,
5763                              const char *iname,
5764                              struct displaced_step_closure *dsc)
5765 {
5766   if (debug_displaced)
5767     fprintf_unfiltered (gdb_stdlog, "displaced: copying insn %.4x, "
5768                         "opcode/class '%s' unmodified\n", insn,
5769                         iname);
5770
5771   dsc->modinsn[0] = insn;
5772
5773   return 0;
5774 }
5775
5776 /* Preload instructions with immediate offset.  */
5777
5778 static void
5779 cleanup_preload (struct gdbarch *gdbarch,
5780                  struct regcache *regs, struct displaced_step_closure *dsc)
5781 {
5782   displaced_write_reg (regs, dsc, 0, dsc->tmp[0], CANNOT_WRITE_PC);
5783   if (!dsc->u.preload.immed)
5784     displaced_write_reg (regs, dsc, 1, dsc->tmp[1], CANNOT_WRITE_PC);
5785 }
5786
5787 static void
5788 install_preload (struct gdbarch *gdbarch, struct regcache *regs,
5789                  struct displaced_step_closure *dsc, unsigned int rn)
5790 {
5791   ULONGEST rn_val;
5792   /* Preload instructions:
5793
5794      {pli/pld} [rn, #+/-imm]
5795      ->
5796      {pli/pld} [r0, #+/-imm].  */
5797
5798   dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
5799   rn_val = displaced_read_reg (regs, dsc, rn);
5800   displaced_write_reg (regs, dsc, 0, rn_val, CANNOT_WRITE_PC);
5801   dsc->u.preload.immed = 1;
5802
5803   dsc->cleanup = &cleanup_preload;
5804 }
5805
5806 static int
5807 arm_copy_preload (struct gdbarch *gdbarch, uint32_t insn, struct regcache *regs,
5808                   struct displaced_step_closure *dsc)
5809 {
5810   unsigned int rn = bits (insn, 16, 19);
5811
5812   if (!insn_references_pc (insn, 0x000f0000ul))
5813     return arm_copy_unmodified (gdbarch, insn, "preload", dsc);
5814
5815   if (debug_displaced)
5816     fprintf_unfiltered (gdb_stdlog, "displaced: copying preload insn %.8lx\n",
5817                         (unsigned long) insn);
5818
5819   dsc->modinsn[0] = insn & 0xfff0ffff;
5820
5821   install_preload (gdbarch, regs, dsc, rn);
5822
5823   return 0;
5824 }
5825
5826 static int
5827 thumb2_copy_preload (struct gdbarch *gdbarch, uint16_t insn1, uint16_t insn2,
5828                      struct regcache *regs, struct displaced_step_closure *dsc)
5829 {
5830   unsigned int rn = bits (insn1, 0, 3);
5831   unsigned int u_bit = bit (insn1, 7);
5832   int imm12 = bits (insn2, 0, 11);
5833   ULONGEST pc_val;
5834
5835   if (rn != ARM_PC_REGNUM)
5836     return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2, "preload", dsc);
5837
5838   /* PC is only allowed to use in PLI (immediate,literal) Encoding T3, and
5839      PLD (literal) Encoding T1.  */
5840   if (debug_displaced)
5841     fprintf_unfiltered (gdb_stdlog,
5842                         "displaced: copying pld/pli pc (0x%x) %c imm12 %.4x\n",
5843                         (unsigned int) dsc->insn_addr, u_bit ? '+' : '-',
5844                         imm12);
5845
5846   if (!u_bit)
5847     imm12 = -1 * imm12;
5848
5849   /* Rewrite instruction {pli/pld} PC imm12 into:
5850      Prepare: tmp[0] <- r0, tmp[1] <- r1, r0 <- pc, r1 <- imm12
5851
5852      {pli/pld} [r0, r1]
5853
5854      Cleanup: r0 <- tmp[0], r1 <- tmp[1].  */
5855
5856   dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
5857   dsc->tmp[1] = displaced_read_reg (regs, dsc, 1);
5858
5859   pc_val = displaced_read_reg (regs, dsc, ARM_PC_REGNUM);
5860
5861   displaced_write_reg (regs, dsc, 0, pc_val, CANNOT_WRITE_PC);
5862   displaced_write_reg (regs, dsc, 1, imm12, CANNOT_WRITE_PC);
5863   dsc->u.preload.immed = 0;
5864
5865   /* {pli/pld} [r0, r1] */
5866   dsc->modinsn[0] = insn1 & 0xfff0;
5867   dsc->modinsn[1] = 0xf001;
5868   dsc->numinsns = 2;
5869
5870   dsc->cleanup = &cleanup_preload;
5871   return 0;
5872 }
5873
5874 /* Preload instructions with register offset.  */
5875
5876 static void
5877 install_preload_reg(struct gdbarch *gdbarch, struct regcache *regs,
5878                     struct displaced_step_closure *dsc, unsigned int rn,
5879                     unsigned int rm)
5880 {
5881   ULONGEST rn_val, rm_val;
5882
5883   /* Preload register-offset instructions:
5884
5885      {pli/pld} [rn, rm {, shift}]
5886      ->
5887      {pli/pld} [r0, r1 {, shift}].  */
5888
5889   dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
5890   dsc->tmp[1] = displaced_read_reg (regs, dsc, 1);
5891   rn_val = displaced_read_reg (regs, dsc, rn);
5892   rm_val = displaced_read_reg (regs, dsc, rm);
5893   displaced_write_reg (regs, dsc, 0, rn_val, CANNOT_WRITE_PC);
5894   displaced_write_reg (regs, dsc, 1, rm_val, CANNOT_WRITE_PC);
5895   dsc->u.preload.immed = 0;
5896
5897   dsc->cleanup = &cleanup_preload;
5898 }
5899
5900 static int
5901 arm_copy_preload_reg (struct gdbarch *gdbarch, uint32_t insn,
5902                       struct regcache *regs,
5903                       struct displaced_step_closure *dsc)
5904 {
5905   unsigned int rn = bits (insn, 16, 19);
5906   unsigned int rm = bits (insn, 0, 3);
5907
5908
5909   if (!insn_references_pc (insn, 0x000f000ful))
5910     return arm_copy_unmodified (gdbarch, insn, "preload reg", dsc);
5911
5912   if (debug_displaced)
5913     fprintf_unfiltered (gdb_stdlog, "displaced: copying preload insn %.8lx\n",
5914                         (unsigned long) insn);
5915
5916   dsc->modinsn[0] = (insn & 0xfff0fff0) | 0x1;
5917
5918   install_preload_reg (gdbarch, regs, dsc, rn, rm);
5919   return 0;
5920 }
5921
5922 /* Copy/cleanup coprocessor load and store instructions.  */
5923
5924 static void
5925 cleanup_copro_load_store (struct gdbarch *gdbarch,
5926                           struct regcache *regs,
5927                           struct displaced_step_closure *dsc)
5928 {
5929   ULONGEST rn_val = displaced_read_reg (regs, dsc, 0);
5930
5931   displaced_write_reg (regs, dsc, 0, dsc->tmp[0], CANNOT_WRITE_PC);
5932
5933   if (dsc->u.ldst.writeback)
5934     displaced_write_reg (regs, dsc, dsc->u.ldst.rn, rn_val, LOAD_WRITE_PC);
5935 }
5936
5937 static void
5938 install_copro_load_store (struct gdbarch *gdbarch, struct regcache *regs,
5939                           struct displaced_step_closure *dsc,
5940                           int writeback, unsigned int rn)
5941 {
5942   ULONGEST rn_val;
5943
5944   /* Coprocessor load/store instructions:
5945
5946      {stc/stc2} [<Rn>, #+/-imm]  (and other immediate addressing modes)
5947      ->
5948      {stc/stc2} [r0, #+/-imm].
5949
5950      ldc/ldc2 are handled identically.  */
5951
5952   dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
5953   rn_val = displaced_read_reg (regs, dsc, rn);
5954   /* PC should be 4-byte aligned.  */
5955   rn_val = rn_val & 0xfffffffc;
5956   displaced_write_reg (regs, dsc, 0, rn_val, CANNOT_WRITE_PC);
5957
5958   dsc->u.ldst.writeback = writeback;
5959   dsc->u.ldst.rn = rn;
5960
5961   dsc->cleanup = &cleanup_copro_load_store;
5962 }
5963
5964 static int
5965 arm_copy_copro_load_store (struct gdbarch *gdbarch, uint32_t insn,
5966                            struct regcache *regs,
5967                            struct displaced_step_closure *dsc)
5968 {
5969   unsigned int rn = bits (insn, 16, 19);
5970
5971   if (!insn_references_pc (insn, 0x000f0000ul))
5972     return arm_copy_unmodified (gdbarch, insn, "copro load/store", dsc);
5973
5974   if (debug_displaced)
5975     fprintf_unfiltered (gdb_stdlog, "displaced: copying coprocessor "
5976                         "load/store insn %.8lx\n", (unsigned long) insn);
5977
5978   dsc->modinsn[0] = insn & 0xfff0ffff;
5979
5980   install_copro_load_store (gdbarch, regs, dsc, bit (insn, 25), rn);
5981
5982   return 0;
5983 }
5984
5985 static int
5986 thumb2_copy_copro_load_store (struct gdbarch *gdbarch, uint16_t insn1,
5987                               uint16_t insn2, struct regcache *regs,
5988                               struct displaced_step_closure *dsc)
5989 {
5990   unsigned int rn = bits (insn1, 0, 3);
5991
5992   if (rn != ARM_PC_REGNUM)
5993     return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
5994                                         "copro load/store", dsc);
5995
5996   if (debug_displaced)
5997     fprintf_unfiltered (gdb_stdlog, "displaced: copying coprocessor "
5998                         "load/store insn %.4x%.4x\n", insn1, insn2);
5999
6000   dsc->modinsn[0] = insn1 & 0xfff0;
6001   dsc->modinsn[1] = insn2;
6002   dsc->numinsns = 2;
6003
6004   /* This function is called for copying instruction LDC/LDC2/VLDR, which
6005      doesn't support writeback, so pass 0.  */
6006   install_copro_load_store (gdbarch, regs, dsc, 0, rn);
6007
6008   return 0;
6009 }
6010
6011 /* Clean up branch instructions (actually perform the branch, by setting
6012    PC).  */
6013
6014 static void
6015 cleanup_branch (struct gdbarch *gdbarch, struct regcache *regs,
6016                 struct displaced_step_closure *dsc)
6017 {
6018   uint32_t status = displaced_read_reg (regs, dsc, ARM_PS_REGNUM);
6019   int branch_taken = condition_true (dsc->u.branch.cond, status);
6020   enum pc_write_style write_pc = dsc->u.branch.exchange
6021                                  ? BX_WRITE_PC : BRANCH_WRITE_PC;
6022
6023   if (!branch_taken)
6024     return;
6025
6026   if (dsc->u.branch.link)
6027     {
6028       /* The value of LR should be the next insn of current one.  In order
6029        not to confuse logic hanlding later insn `bx lr', if current insn mode
6030        is Thumb, the bit 0 of LR value should be set to 1.  */
6031       ULONGEST next_insn_addr = dsc->insn_addr + dsc->insn_size;
6032
6033       if (dsc->is_thumb)
6034         next_insn_addr |= 0x1;
6035
6036       displaced_write_reg (regs, dsc, ARM_LR_REGNUM, next_insn_addr,
6037                            CANNOT_WRITE_PC);
6038     }
6039
6040   displaced_write_reg (regs, dsc, ARM_PC_REGNUM, dsc->u.branch.dest, write_pc);
6041 }
6042
6043 /* Copy B/BL/BLX instructions with immediate destinations.  */
6044
6045 static void
6046 install_b_bl_blx (struct gdbarch *gdbarch, struct regcache *regs,
6047                   struct displaced_step_closure *dsc,
6048                   unsigned int cond, int exchange, int link, long offset)
6049 {
6050   /* Implement "BL<cond> <label>" as:
6051
6052      Preparation: cond <- instruction condition
6053      Insn: mov r0, r0  (nop)
6054      Cleanup: if (condition true) { r14 <- pc; pc <- label }.
6055
6056      B<cond> similar, but don't set r14 in cleanup.  */
6057
6058   dsc->u.branch.cond = cond;
6059   dsc->u.branch.link = link;
6060   dsc->u.branch.exchange = exchange;
6061
6062   dsc->u.branch.dest = dsc->insn_addr;
6063   if (link && exchange)
6064     /* For BLX, offset is computed from the Align (PC, 4).  */
6065     dsc->u.branch.dest = dsc->u.branch.dest & 0xfffffffc;
6066
6067   if (dsc->is_thumb)
6068     dsc->u.branch.dest += 4 + offset;
6069   else
6070     dsc->u.branch.dest += 8 + offset;
6071
6072   dsc->cleanup = &cleanup_branch;
6073 }
6074 static int
6075 arm_copy_b_bl_blx (struct gdbarch *gdbarch, uint32_t insn,
6076                    struct regcache *regs, struct displaced_step_closure *dsc)
6077 {
6078   unsigned int cond = bits (insn, 28, 31);
6079   int exchange = (cond == 0xf);
6080   int link = exchange || bit (insn, 24);
6081   long offset;
6082
6083   if (debug_displaced)
6084     fprintf_unfiltered (gdb_stdlog, "displaced: copying %s immediate insn "
6085                         "%.8lx\n", (exchange) ? "blx" : (link) ? "bl" : "b",
6086                         (unsigned long) insn);
6087   if (exchange)
6088     /* For BLX, set bit 0 of the destination.  The cleanup_branch function will
6089        then arrange the switch into Thumb mode.  */
6090     offset = (bits (insn, 0, 23) << 2) | (bit (insn, 24) << 1) | 1;
6091   else
6092     offset = bits (insn, 0, 23) << 2;
6093
6094   if (bit (offset, 25))
6095     offset = offset | ~0x3ffffff;
6096
6097   dsc->modinsn[0] = ARM_NOP;
6098
6099   install_b_bl_blx (gdbarch, regs, dsc, cond, exchange, link, offset);
6100   return 0;
6101 }
6102
6103 static int
6104 thumb2_copy_b_bl_blx (struct gdbarch *gdbarch, uint16_t insn1,
6105                       uint16_t insn2, struct regcache *regs,
6106                       struct displaced_step_closure *dsc)
6107 {
6108   int link = bit (insn2, 14);
6109   int exchange = link && !bit (insn2, 12);
6110   int cond = INST_AL;
6111   long offset = 0;
6112   int j1 = bit (insn2, 13);
6113   int j2 = bit (insn2, 11);
6114   int s = sbits (insn1, 10, 10);
6115   int i1 = !(j1 ^ bit (insn1, 10));
6116   int i2 = !(j2 ^ bit (insn1, 10));
6117
6118   if (!link && !exchange) /* B */
6119     {
6120       offset = (bits (insn2, 0, 10) << 1);
6121       if (bit (insn2, 12)) /* Encoding T4 */
6122         {
6123           offset |= (bits (insn1, 0, 9) << 12)
6124             | (i2 << 22)
6125             | (i1 << 23)
6126             | (s << 24);
6127           cond = INST_AL;
6128         }
6129       else /* Encoding T3 */
6130         {
6131           offset |= (bits (insn1, 0, 5) << 12)
6132             | (j1 << 18)
6133             | (j2 << 19)
6134             | (s << 20);
6135           cond = bits (insn1, 6, 9);
6136         }
6137     }
6138   else
6139     {
6140       offset = (bits (insn1, 0, 9) << 12);
6141       offset |= ((i2 << 22) | (i1 << 23) | (s << 24));
6142       offset |= exchange ?
6143         (bits (insn2, 1, 10) << 2) : (bits (insn2, 0, 10) << 1);
6144     }
6145
6146   if (debug_displaced)
6147     fprintf_unfiltered (gdb_stdlog, "displaced: copying %s insn "
6148                         "%.4x %.4x with offset %.8lx\n",
6149                         link ? (exchange) ? "blx" : "bl" : "b",
6150                         insn1, insn2, offset);
6151
6152   dsc->modinsn[0] = THUMB_NOP;
6153
6154   install_b_bl_blx (gdbarch, regs, dsc, cond, exchange, link, offset);
6155   return 0;
6156 }
6157
6158 /* Copy B Thumb instructions.  */
6159 static int
6160 thumb_copy_b (struct gdbarch *gdbarch, unsigned short insn,
6161               struct displaced_step_closure *dsc)
6162 {
6163   unsigned int cond = 0;
6164   int offset = 0;
6165   unsigned short bit_12_15 = bits (insn, 12, 15);
6166   CORE_ADDR from = dsc->insn_addr;
6167
6168   if (bit_12_15 == 0xd)
6169     {
6170       /* offset = SignExtend (imm8:0, 32) */
6171       offset = sbits ((insn << 1), 0, 8);
6172       cond = bits (insn, 8, 11);
6173     }
6174   else if (bit_12_15 == 0xe) /* Encoding T2 */
6175     {
6176       offset = sbits ((insn << 1), 0, 11);
6177       cond = INST_AL;
6178     }
6179
6180   if (debug_displaced)
6181     fprintf_unfiltered (gdb_stdlog,
6182                         "displaced: copying b immediate insn %.4x "
6183                         "with offset %d\n", insn, offset);
6184
6185   dsc->u.branch.cond = cond;
6186   dsc->u.branch.link = 0;
6187   dsc->u.branch.exchange = 0;
6188   dsc->u.branch.dest = from + 4 + offset;
6189
6190   dsc->modinsn[0] = THUMB_NOP;
6191
6192   dsc->cleanup = &cleanup_branch;
6193
6194   return 0;
6195 }
6196
6197 /* Copy BX/BLX with register-specified destinations.  */
6198
6199 static void
6200 install_bx_blx_reg (struct gdbarch *gdbarch, struct regcache *regs,
6201                     struct displaced_step_closure *dsc, int link,
6202                     unsigned int cond, unsigned int rm)
6203 {
6204   /* Implement {BX,BLX}<cond> <reg>" as:
6205
6206      Preparation: cond <- instruction condition
6207      Insn: mov r0, r0 (nop)
6208      Cleanup: if (condition true) { r14 <- pc; pc <- dest; }.
6209
6210      Don't set r14 in cleanup for BX.  */
6211
6212   dsc->u.branch.dest = displaced_read_reg (regs, dsc, rm);
6213
6214   dsc->u.branch.cond = cond;
6215   dsc->u.branch.link = link;
6216
6217   dsc->u.branch.exchange = 1;
6218
6219   dsc->cleanup = &cleanup_branch;
6220 }
6221
6222 static int
6223 arm_copy_bx_blx_reg (struct gdbarch *gdbarch, uint32_t insn,
6224                      struct regcache *regs, struct displaced_step_closure *dsc)
6225 {
6226   unsigned int cond = bits (insn, 28, 31);
6227   /* BX:  x12xxx1x
6228      BLX: x12xxx3x.  */
6229   int link = bit (insn, 5);
6230   unsigned int rm = bits (insn, 0, 3);
6231
6232   if (debug_displaced)
6233     fprintf_unfiltered (gdb_stdlog, "displaced: copying insn %.8lx",
6234                         (unsigned long) insn);
6235
6236   dsc->modinsn[0] = ARM_NOP;
6237
6238   install_bx_blx_reg (gdbarch, regs, dsc, link, cond, rm);
6239   return 0;
6240 }
6241
6242 static int
6243 thumb_copy_bx_blx_reg (struct gdbarch *gdbarch, uint16_t insn,
6244                        struct regcache *regs,
6245                        struct displaced_step_closure *dsc)
6246 {
6247   int link = bit (insn, 7);
6248   unsigned int rm = bits (insn, 3, 6);
6249
6250   if (debug_displaced)
6251     fprintf_unfiltered (gdb_stdlog, "displaced: copying insn %.4x",
6252                         (unsigned short) insn);
6253
6254   dsc->modinsn[0] = THUMB_NOP;
6255
6256   install_bx_blx_reg (gdbarch, regs, dsc, link, INST_AL, rm);
6257
6258   return 0;
6259 }
6260
6261
6262 /* Copy/cleanup arithmetic/logic instruction with immediate RHS.  */
6263
6264 static void
6265 cleanup_alu_imm (struct gdbarch *gdbarch,
6266                  struct regcache *regs, struct displaced_step_closure *dsc)
6267 {
6268   ULONGEST rd_val = displaced_read_reg (regs, dsc, 0);
6269   displaced_write_reg (regs, dsc, 0, dsc->tmp[0], CANNOT_WRITE_PC);
6270   displaced_write_reg (regs, dsc, 1, dsc->tmp[1], CANNOT_WRITE_PC);
6271   displaced_write_reg (regs, dsc, dsc->rd, rd_val, ALU_WRITE_PC);
6272 }
6273
6274 static int
6275 arm_copy_alu_imm (struct gdbarch *gdbarch, uint32_t insn, struct regcache *regs,
6276                   struct displaced_step_closure *dsc)
6277 {
6278   unsigned int rn = bits (insn, 16, 19);
6279   unsigned int rd = bits (insn, 12, 15);
6280   unsigned int op = bits (insn, 21, 24);
6281   int is_mov = (op == 0xd);
6282   ULONGEST rd_val, rn_val;
6283
6284   if (!insn_references_pc (insn, 0x000ff000ul))
6285     return arm_copy_unmodified (gdbarch, insn, "ALU immediate", dsc);
6286
6287   if (debug_displaced)
6288     fprintf_unfiltered (gdb_stdlog, "displaced: copying immediate %s insn "
6289                         "%.8lx\n", is_mov ? "move" : "ALU",
6290                         (unsigned long) insn);
6291
6292   /* Instruction is of form:
6293
6294      <op><cond> rd, [rn,] #imm
6295
6296      Rewrite as:
6297
6298      Preparation: tmp1, tmp2 <- r0, r1;
6299                   r0, r1 <- rd, rn
6300      Insn: <op><cond> r0, r1, #imm
6301      Cleanup: rd <- r0; r0 <- tmp1; r1 <- tmp2
6302   */
6303
6304   dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
6305   dsc->tmp[1] = displaced_read_reg (regs, dsc, 1);
6306   rn_val = displaced_read_reg (regs, dsc, rn);
6307   rd_val = displaced_read_reg (regs, dsc, rd);
6308   displaced_write_reg (regs, dsc, 0, rd_val, CANNOT_WRITE_PC);
6309   displaced_write_reg (regs, dsc, 1, rn_val, CANNOT_WRITE_PC);
6310   dsc->rd = rd;
6311
6312   if (is_mov)
6313     dsc->modinsn[0] = insn & 0xfff00fff;
6314   else
6315     dsc->modinsn[0] = (insn & 0xfff00fff) | 0x10000;
6316
6317   dsc->cleanup = &cleanup_alu_imm;
6318
6319   return 0;
6320 }
6321
6322 static int
6323 thumb2_copy_alu_imm (struct gdbarch *gdbarch, uint16_t insn1,
6324                      uint16_t insn2, struct regcache *regs,
6325                      struct displaced_step_closure *dsc)
6326 {
6327   unsigned int op = bits (insn1, 5, 8);
6328   unsigned int rn, rm, rd;
6329   ULONGEST rd_val, rn_val;
6330
6331   rn = bits (insn1, 0, 3); /* Rn */
6332   rm = bits (insn2, 0, 3); /* Rm */
6333   rd = bits (insn2, 8, 11); /* Rd */
6334
6335   /* This routine is only called for instruction MOV.  */
6336   gdb_assert (op == 0x2 && rn == 0xf);
6337
6338   if (rm != ARM_PC_REGNUM && rd != ARM_PC_REGNUM)
6339     return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2, "ALU imm", dsc);
6340
6341   if (debug_displaced)
6342     fprintf_unfiltered (gdb_stdlog, "displaced: copying reg %s insn %.4x%.4x\n",
6343                         "ALU", insn1, insn2);
6344
6345   /* Instruction is of form:
6346
6347      <op><cond> rd, [rn,] #imm
6348
6349      Rewrite as:
6350
6351      Preparation: tmp1, tmp2 <- r0, r1;
6352                   r0, r1 <- rd, rn
6353      Insn: <op><cond> r0, r1, #imm
6354      Cleanup: rd <- r0; r0 <- tmp1; r1 <- tmp2
6355   */
6356
6357   dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
6358   dsc->tmp[1] = displaced_read_reg (regs, dsc, 1);
6359   rn_val = displaced_read_reg (regs, dsc, rn);
6360   rd_val = displaced_read_reg (regs, dsc, rd);
6361   displaced_write_reg (regs, dsc, 0, rd_val, CANNOT_WRITE_PC);
6362   displaced_write_reg (regs, dsc, 1, rn_val, CANNOT_WRITE_PC);
6363   dsc->rd = rd;
6364
6365   dsc->modinsn[0] = insn1;
6366   dsc->modinsn[1] = ((insn2 & 0xf0f0) | 0x1);
6367   dsc->numinsns = 2;
6368
6369   dsc->cleanup = &cleanup_alu_imm;
6370
6371   return 0;
6372 }
6373
6374 /* Copy/cleanup arithmetic/logic insns with register RHS.  */
6375
6376 static void
6377 cleanup_alu_reg (struct gdbarch *gdbarch,
6378                  struct regcache *regs, struct displaced_step_closure *dsc)
6379 {
6380   ULONGEST rd_val;
6381   int i;
6382
6383   rd_val = displaced_read_reg (regs, dsc, 0);
6384
6385   for (i = 0; i < 3; i++)
6386     displaced_write_reg (regs, dsc, i, dsc->tmp[i], CANNOT_WRITE_PC);
6387
6388   displaced_write_reg (regs, dsc, dsc->rd, rd_val, ALU_WRITE_PC);
6389 }
6390
6391 static void
6392 install_alu_reg (struct gdbarch *gdbarch, struct regcache *regs,
6393                  struct displaced_step_closure *dsc,
6394                  unsigned int rd, unsigned int rn, unsigned int rm)
6395 {
6396   ULONGEST rd_val, rn_val, rm_val;
6397
6398   /* Instruction is of form:
6399
6400      <op><cond> rd, [rn,] rm [, <shift>]
6401
6402      Rewrite as:
6403
6404      Preparation: tmp1, tmp2, tmp3 <- r0, r1, r2;
6405                   r0, r1, r2 <- rd, rn, rm
6406      Insn: <op><cond> r0, r1, r2 [, <shift>]
6407      Cleanup: rd <- r0; r0, r1, r2 <- tmp1, tmp2, tmp3
6408   */
6409
6410   dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
6411   dsc->tmp[1] = displaced_read_reg (regs, dsc, 1);
6412   dsc->tmp[2] = displaced_read_reg (regs, dsc, 2);
6413   rd_val = displaced_read_reg (regs, dsc, rd);
6414   rn_val = displaced_read_reg (regs, dsc, rn);
6415   rm_val = displaced_read_reg (regs, dsc, rm);
6416   displaced_write_reg (regs, dsc, 0, rd_val, CANNOT_WRITE_PC);
6417   displaced_write_reg (regs, dsc, 1, rn_val, CANNOT_WRITE_PC);
6418   displaced_write_reg (regs, dsc, 2, rm_val, CANNOT_WRITE_PC);
6419   dsc->rd = rd;
6420
6421   dsc->cleanup = &cleanup_alu_reg;
6422 }
6423
6424 static int
6425 arm_copy_alu_reg (struct gdbarch *gdbarch, uint32_t insn, struct regcache *regs,
6426                   struct displaced_step_closure *dsc)
6427 {
6428   unsigned int op = bits (insn, 21, 24);
6429   int is_mov = (op == 0xd);
6430
6431   if (!insn_references_pc (insn, 0x000ff00ful))
6432     return arm_copy_unmodified (gdbarch, insn, "ALU reg", dsc);
6433
6434   if (debug_displaced)
6435     fprintf_unfiltered (gdb_stdlog, "displaced: copying reg %s insn %.8lx\n",
6436                         is_mov ? "move" : "ALU", (unsigned long) insn);
6437
6438   if (is_mov)
6439     dsc->modinsn[0] = (insn & 0xfff00ff0) | 0x2;
6440   else
6441     dsc->modinsn[0] = (insn & 0xfff00ff0) | 0x10002;
6442
6443   install_alu_reg (gdbarch, regs, dsc, bits (insn, 12, 15), bits (insn, 16, 19),
6444                    bits (insn, 0, 3));
6445   return 0;
6446 }
6447
6448 static int
6449 thumb_copy_alu_reg (struct gdbarch *gdbarch, uint16_t insn,
6450                     struct regcache *regs,
6451                     struct displaced_step_closure *dsc)
6452 {
6453   unsigned rn, rm, rd;
6454
6455   rd = bits (insn, 3, 6);
6456   rn = (bit (insn, 7) << 3) | bits (insn, 0, 2);
6457   rm = 2;
6458
6459   if (rd != ARM_PC_REGNUM && rn != ARM_PC_REGNUM)
6460     return thumb_copy_unmodified_16bit (gdbarch, insn, "ALU reg", dsc);
6461
6462   if (debug_displaced)
6463     fprintf_unfiltered (gdb_stdlog, "displaced: copying reg %s insn %.4x\n",
6464                         "ALU", (unsigned short) insn);
6465
6466   dsc->modinsn[0] = ((insn & 0xff00) | 0x08);
6467
6468   install_alu_reg (gdbarch, regs, dsc, rd, rn, rm);
6469
6470   return 0;
6471 }
6472
6473 /* Cleanup/copy arithmetic/logic insns with shifted register RHS.  */
6474
6475 static void
6476 cleanup_alu_shifted_reg (struct gdbarch *gdbarch,
6477                          struct regcache *regs,
6478                          struct displaced_step_closure *dsc)
6479 {
6480   ULONGEST rd_val = displaced_read_reg (regs, dsc, 0);
6481   int i;
6482
6483   for (i = 0; i < 4; i++)
6484     displaced_write_reg (regs, dsc, i, dsc->tmp[i], CANNOT_WRITE_PC);
6485
6486   displaced_write_reg (regs, dsc, dsc->rd, rd_val, ALU_WRITE_PC);
6487 }
6488
6489 static void
6490 install_alu_shifted_reg (struct gdbarch *gdbarch, struct regcache *regs,
6491                          struct displaced_step_closure *dsc,
6492                          unsigned int rd, unsigned int rn, unsigned int rm,
6493                          unsigned rs)
6494 {
6495   int i;
6496   ULONGEST rd_val, rn_val, rm_val, rs_val;
6497
6498   /* Instruction is of form:
6499
6500      <op><cond> rd, [rn,] rm, <shift> rs
6501
6502      Rewrite as:
6503
6504      Preparation: tmp1, tmp2, tmp3, tmp4 <- r0, r1, r2, r3
6505                   r0, r1, r2, r3 <- rd, rn, rm, rs
6506      Insn: <op><cond> r0, r1, r2, <shift> r3
6507      Cleanup: tmp5 <- r0
6508               r0, r1, r2, r3 <- tmp1, tmp2, tmp3, tmp4
6509               rd <- tmp5
6510   */
6511
6512   for (i = 0; i < 4; i++)
6513     dsc->tmp[i] = displaced_read_reg (regs, dsc, i);
6514
6515   rd_val = displaced_read_reg (regs, dsc, rd);
6516   rn_val = displaced_read_reg (regs, dsc, rn);
6517   rm_val = displaced_read_reg (regs, dsc, rm);
6518   rs_val = displaced_read_reg (regs, dsc, rs);
6519   displaced_write_reg (regs, dsc, 0, rd_val, CANNOT_WRITE_PC);
6520   displaced_write_reg (regs, dsc, 1, rn_val, CANNOT_WRITE_PC);
6521   displaced_write_reg (regs, dsc, 2, rm_val, CANNOT_WRITE_PC);
6522   displaced_write_reg (regs, dsc, 3, rs_val, CANNOT_WRITE_PC);
6523   dsc->rd = rd;
6524   dsc->cleanup = &cleanup_alu_shifted_reg;
6525 }
6526
6527 static int
6528 arm_copy_alu_shifted_reg (struct gdbarch *gdbarch, uint32_t insn,
6529                           struct regcache *regs,
6530                           struct displaced_step_closure *dsc)
6531 {
6532   unsigned int op = bits (insn, 21, 24);
6533   int is_mov = (op == 0xd);
6534   unsigned int rd, rn, rm, rs;
6535
6536   if (!insn_references_pc (insn, 0x000fff0ful))
6537     return arm_copy_unmodified (gdbarch, insn, "ALU shifted reg", dsc);
6538
6539   if (debug_displaced)
6540     fprintf_unfiltered (gdb_stdlog, "displaced: copying shifted reg %s insn "
6541                         "%.8lx\n", is_mov ? "move" : "ALU",
6542                         (unsigned long) insn);
6543
6544   rn = bits (insn, 16, 19);
6545   rm = bits (insn, 0, 3);
6546   rs = bits (insn, 8, 11);
6547   rd = bits (insn, 12, 15);
6548
6549   if (is_mov)
6550     dsc->modinsn[0] = (insn & 0xfff000f0) | 0x302;
6551   else
6552     dsc->modinsn[0] = (insn & 0xfff000f0) | 0x10302;
6553
6554   install_alu_shifted_reg (gdbarch, regs, dsc, rd, rn, rm, rs);
6555
6556   return 0;
6557 }
6558
6559 /* Clean up load instructions.  */
6560
6561 static void
6562 cleanup_load (struct gdbarch *gdbarch, struct regcache *regs,
6563               struct displaced_step_closure *dsc)
6564 {
6565   ULONGEST rt_val, rt_val2 = 0, rn_val;
6566
6567   rt_val = displaced_read_reg (regs, dsc, 0);
6568   if (dsc->u.ldst.xfersize == 8)
6569     rt_val2 = displaced_read_reg (regs, dsc, 1);
6570   rn_val = displaced_read_reg (regs, dsc, 2);
6571
6572   displaced_write_reg (regs, dsc, 0, dsc->tmp[0], CANNOT_WRITE_PC);
6573   if (dsc->u.ldst.xfersize > 4)
6574     displaced_write_reg (regs, dsc, 1, dsc->tmp[1], CANNOT_WRITE_PC);
6575   displaced_write_reg (regs, dsc, 2, dsc->tmp[2], CANNOT_WRITE_PC);
6576   if (!dsc->u.ldst.immed)
6577     displaced_write_reg (regs, dsc, 3, dsc->tmp[3], CANNOT_WRITE_PC);
6578
6579   /* Handle register writeback.  */
6580   if (dsc->u.ldst.writeback)
6581     displaced_write_reg (regs, dsc, dsc->u.ldst.rn, rn_val, CANNOT_WRITE_PC);
6582   /* Put result in right place.  */
6583   displaced_write_reg (regs, dsc, dsc->rd, rt_val, LOAD_WRITE_PC);
6584   if (dsc->u.ldst.xfersize == 8)
6585     displaced_write_reg (regs, dsc, dsc->rd + 1, rt_val2, LOAD_WRITE_PC);
6586 }
6587
6588 /* Clean up store instructions.  */
6589
6590 static void
6591 cleanup_store (struct gdbarch *gdbarch, struct regcache *regs,
6592                struct displaced_step_closure *dsc)
6593 {
6594   ULONGEST rn_val = displaced_read_reg (regs, dsc, 2);
6595
6596   displaced_write_reg (regs, dsc, 0, dsc->tmp[0], CANNOT_WRITE_PC);
6597   if (dsc->u.ldst.xfersize > 4)
6598     displaced_write_reg (regs, dsc, 1, dsc->tmp[1], CANNOT_WRITE_PC);
6599   displaced_write_reg (regs, dsc, 2, dsc->tmp[2], CANNOT_WRITE_PC);
6600   if (!dsc->u.ldst.immed)
6601     displaced_write_reg (regs, dsc, 3, dsc->tmp[3], CANNOT_WRITE_PC);
6602   if (!dsc->u.ldst.restore_r4)
6603     displaced_write_reg (regs, dsc, 4, dsc->tmp[4], CANNOT_WRITE_PC);
6604
6605   /* Writeback.  */
6606   if (dsc->u.ldst.writeback)
6607     displaced_write_reg (regs, dsc, dsc->u.ldst.rn, rn_val, CANNOT_WRITE_PC);
6608 }
6609
6610 /* Copy "extra" load/store instructions.  These are halfword/doubleword
6611    transfers, which have a different encoding to byte/word transfers.  */
6612
6613 static int
6614 arm_copy_extra_ld_st (struct gdbarch *gdbarch, uint32_t insn, int unpriveleged,
6615                       struct regcache *regs, struct displaced_step_closure *dsc)
6616 {
6617   unsigned int op1 = bits (insn, 20, 24);
6618   unsigned int op2 = bits (insn, 5, 6);
6619   unsigned int rt = bits (insn, 12, 15);
6620   unsigned int rn = bits (insn, 16, 19);
6621   unsigned int rm = bits (insn, 0, 3);
6622   char load[12]     = {0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1};
6623   char bytesize[12] = {2, 2, 2, 2, 8, 1, 8, 1, 8, 2, 8, 2};
6624   int immed = (op1 & 0x4) != 0;
6625   int opcode;
6626   ULONGEST rt_val, rt_val2 = 0, rn_val, rm_val = 0;
6627
6628   if (!insn_references_pc (insn, 0x000ff00ful))
6629     return arm_copy_unmodified (gdbarch, insn, "extra load/store", dsc);
6630
6631   if (debug_displaced)
6632     fprintf_unfiltered (gdb_stdlog, "displaced: copying %sextra load/store "
6633                         "insn %.8lx\n", unpriveleged ? "unpriveleged " : "",
6634                         (unsigned long) insn);
6635
6636   opcode = ((op2 << 2) | (op1 & 0x1) | ((op1 & 0x4) >> 1)) - 4;
6637
6638   if (opcode < 0)
6639     internal_error (__FILE__, __LINE__,
6640                     _("copy_extra_ld_st: instruction decode error"));
6641
6642   dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
6643   dsc->tmp[1] = displaced_read_reg (regs, dsc, 1);
6644   dsc->tmp[2] = displaced_read_reg (regs, dsc, 2);
6645   if (!immed)
6646     dsc->tmp[3] = displaced_read_reg (regs, dsc, 3);
6647
6648   rt_val = displaced_read_reg (regs, dsc, rt);
6649   if (bytesize[opcode] == 8)
6650     rt_val2 = displaced_read_reg (regs, dsc, rt + 1);
6651   rn_val = displaced_read_reg (regs, dsc, rn);
6652   if (!immed)
6653     rm_val = displaced_read_reg (regs, dsc, rm);
6654
6655   displaced_write_reg (regs, dsc, 0, rt_val, CANNOT_WRITE_PC);
6656   if (bytesize[opcode] == 8)
6657     displaced_write_reg (regs, dsc, 1, rt_val2, CANNOT_WRITE_PC);
6658   displaced_write_reg (regs, dsc, 2, rn_val, CANNOT_WRITE_PC);
6659   if (!immed)
6660     displaced_write_reg (regs, dsc, 3, rm_val, CANNOT_WRITE_PC);
6661
6662   dsc->rd = rt;
6663   dsc->u.ldst.xfersize = bytesize[opcode];
6664   dsc->u.ldst.rn = rn;
6665   dsc->u.ldst.immed = immed;
6666   dsc->u.ldst.writeback = bit (insn, 24) == 0 || bit (insn, 21) != 0;
6667   dsc->u.ldst.restore_r4 = 0;
6668
6669   if (immed)
6670     /* {ldr,str}<width><cond> rt, [rt2,] [rn, #imm]
6671         ->
6672        {ldr,str}<width><cond> r0, [r1,] [r2, #imm].  */
6673     dsc->modinsn[0] = (insn & 0xfff00fff) | 0x20000;
6674   else
6675     /* {ldr,str}<width><cond> rt, [rt2,] [rn, +/-rm]
6676         ->
6677        {ldr,str}<width><cond> r0, [r1,] [r2, +/-r3].  */
6678     dsc->modinsn[0] = (insn & 0xfff00ff0) | 0x20003;
6679
6680   dsc->cleanup = load[opcode] ? &cleanup_load : &cleanup_store;
6681
6682   return 0;
6683 }
6684
6685 /* Copy byte/half word/word loads and stores.  */
6686
6687 static void
6688 install_load_store (struct gdbarch *gdbarch, struct regcache *regs,
6689                     struct displaced_step_closure *dsc, int load,
6690                     int immed, int writeback, int size, int usermode,
6691                     int rt, int rm, int rn)
6692 {
6693   ULONGEST rt_val, rn_val, rm_val = 0;
6694
6695   dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
6696   dsc->tmp[2] = displaced_read_reg (regs, dsc, 2);
6697   if (!immed)
6698     dsc->tmp[3] = displaced_read_reg (regs, dsc, 3);
6699   if (!load)
6700     dsc->tmp[4] = displaced_read_reg (regs, dsc, 4);
6701
6702   rt_val = displaced_read_reg (regs, dsc, rt);
6703   rn_val = displaced_read_reg (regs, dsc, rn);
6704   if (!immed)
6705     rm_val = displaced_read_reg (regs, dsc, rm);
6706
6707   displaced_write_reg (regs, dsc, 0, rt_val, CANNOT_WRITE_PC);
6708   displaced_write_reg (regs, dsc, 2, rn_val, CANNOT_WRITE_PC);
6709   if (!immed)
6710     displaced_write_reg (regs, dsc, 3, rm_val, CANNOT_WRITE_PC);
6711   dsc->rd = rt;
6712   dsc->u.ldst.xfersize = size;
6713   dsc->u.ldst.rn = rn;
6714   dsc->u.ldst.immed = immed;
6715   dsc->u.ldst.writeback = writeback;
6716
6717   /* To write PC we can do:
6718
6719      Before this sequence of instructions:
6720      r0 is the PC value got from displaced_read_reg, so r0 = from + 8;
6721      r2 is the Rn value got from dispalced_read_reg.
6722
6723      Insn1: push {pc} Write address of STR instruction + offset on stack
6724      Insn2: pop  {r4} Read it back from stack, r4 = addr(Insn1) + offset
6725      Insn3: sub r4, r4, pc   r4 = addr(Insn1) + offset - pc
6726                                 = addr(Insn1) + offset - addr(Insn3) - 8
6727                                 = offset - 16
6728      Insn4: add r4, r4, #8   r4 = offset - 8
6729      Insn5: add r0, r0, r4   r0 = from + 8 + offset - 8
6730                                 = from + offset
6731      Insn6: str r0, [r2, #imm] (or str r0, [r2, r3])
6732
6733      Otherwise we don't know what value to write for PC, since the offset is
6734      architecture-dependent (sometimes PC+8, sometimes PC+12).  More details
6735      of this can be found in Section "Saving from r15" in
6736      http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dui0204g/Cihbjifh.html */
6737
6738   dsc->cleanup = load ? &cleanup_load : &cleanup_store;
6739 }
6740
6741
6742 static int
6743 thumb2_copy_load_literal (struct gdbarch *gdbarch, uint16_t insn1,
6744                           uint16_t insn2, struct regcache *regs,
6745                           struct displaced_step_closure *dsc, int size)
6746 {
6747   unsigned int u_bit = bit (insn1, 7);
6748   unsigned int rt = bits (insn2, 12, 15);
6749   int imm12 = bits (insn2, 0, 11);
6750   ULONGEST pc_val;
6751
6752   if (debug_displaced)
6753     fprintf_unfiltered (gdb_stdlog,
6754                         "displaced: copying ldr pc (0x%x) R%d %c imm12 %.4x\n",
6755                         (unsigned int) dsc->insn_addr, rt, u_bit ? '+' : '-',
6756                         imm12);
6757
6758   if (!u_bit)
6759     imm12 = -1 * imm12;
6760
6761   /* Rewrite instruction LDR Rt imm12 into:
6762
6763      Prepare: tmp[0] <- r0, tmp[1] <- r2, tmp[2] <- r3, r2 <- pc, r3 <- imm12
6764
6765      LDR R0, R2, R3,
6766
6767      Cleanup: rt <- r0, r0 <- tmp[0], r2 <- tmp[1], r3 <- tmp[2].  */
6768
6769
6770   dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
6771   dsc->tmp[2] = displaced_read_reg (regs, dsc, 2);
6772   dsc->tmp[3] = displaced_read_reg (regs, dsc, 3);
6773
6774   pc_val = displaced_read_reg (regs, dsc, ARM_PC_REGNUM);
6775
6776   pc_val = pc_val & 0xfffffffc;
6777
6778   displaced_write_reg (regs, dsc, 2, pc_val, CANNOT_WRITE_PC);
6779   displaced_write_reg (regs, dsc, 3, imm12, CANNOT_WRITE_PC);
6780
6781   dsc->rd = rt;
6782
6783   dsc->u.ldst.xfersize = size;
6784   dsc->u.ldst.immed = 0;
6785   dsc->u.ldst.writeback = 0;
6786   dsc->u.ldst.restore_r4 = 0;
6787
6788   /* LDR R0, R2, R3 */
6789   dsc->modinsn[0] = 0xf852;
6790   dsc->modinsn[1] = 0x3;
6791   dsc->numinsns = 2;
6792
6793   dsc->cleanup = &cleanup_load;
6794
6795   return 0;
6796 }
6797
6798 static int
6799 thumb2_copy_load_reg_imm (struct gdbarch *gdbarch, uint16_t insn1,
6800                           uint16_t insn2, struct regcache *regs,
6801                           struct displaced_step_closure *dsc,
6802                           int writeback, int immed)
6803 {
6804   unsigned int rt = bits (insn2, 12, 15);
6805   unsigned int rn = bits (insn1, 0, 3);
6806   unsigned int rm = bits (insn2, 0, 3);  /* Only valid if !immed.  */
6807   /* In LDR (register), there is also a register Rm, which is not allowed to
6808      be PC, so we don't have to check it.  */
6809
6810   if (rt != ARM_PC_REGNUM && rn != ARM_PC_REGNUM)
6811     return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2, "load",
6812                                         dsc);
6813
6814   if (debug_displaced)
6815     fprintf_unfiltered (gdb_stdlog,
6816                         "displaced: copying ldr r%d [r%d] insn %.4x%.4x\n",
6817                          rt, rn, insn1, insn2);
6818
6819   install_load_store (gdbarch, regs, dsc, 1, immed, writeback, 4,
6820                       0, rt, rm, rn);
6821
6822   dsc->u.ldst.restore_r4 = 0;
6823
6824   if (immed)
6825     /* ldr[b]<cond> rt, [rn, #imm], etc.
6826        ->
6827        ldr[b]<cond> r0, [r2, #imm].  */
6828     {
6829       dsc->modinsn[0] = (insn1 & 0xfff0) | 0x2;
6830       dsc->modinsn[1] = insn2 & 0x0fff;
6831     }
6832   else
6833     /* ldr[b]<cond> rt, [rn, rm], etc.
6834        ->
6835        ldr[b]<cond> r0, [r2, r3].  */
6836     {
6837       dsc->modinsn[0] = (insn1 & 0xfff0) | 0x2;
6838       dsc->modinsn[1] = (insn2 & 0x0ff0) | 0x3;
6839     }
6840
6841   dsc->numinsns = 2;
6842
6843   return 0;
6844 }
6845
6846
6847 static int
6848 arm_copy_ldr_str_ldrb_strb (struct gdbarch *gdbarch, uint32_t insn,
6849                             struct regcache *regs,
6850                             struct displaced_step_closure *dsc,
6851                             int load, int size, int usermode)
6852 {
6853   int immed = !bit (insn, 25);
6854   int writeback = (bit (insn, 24) == 0 || bit (insn, 21) != 0);
6855   unsigned int rt = bits (insn, 12, 15);
6856   unsigned int rn = bits (insn, 16, 19);
6857   unsigned int rm = bits (insn, 0, 3);  /* Only valid if !immed.  */
6858
6859   if (!insn_references_pc (insn, 0x000ff00ful))
6860     return arm_copy_unmodified (gdbarch, insn, "load/store", dsc);
6861
6862   if (debug_displaced)
6863     fprintf_unfiltered (gdb_stdlog,
6864                         "displaced: copying %s%s r%d [r%d] insn %.8lx\n",
6865                         load ? (size == 1 ? "ldrb" : "ldr")
6866                              : (size == 1 ? "strb" : "str"), usermode ? "t" : "",
6867                         rt, rn,
6868                         (unsigned long) insn);
6869
6870   install_load_store (gdbarch, regs, dsc, load, immed, writeback, size,
6871                       usermode, rt, rm, rn);
6872
6873   if (load || rt != ARM_PC_REGNUM)
6874     {
6875       dsc->u.ldst.restore_r4 = 0;
6876
6877       if (immed)
6878         /* {ldr,str}[b]<cond> rt, [rn, #imm], etc.
6879            ->
6880            {ldr,str}[b]<cond> r0, [r2, #imm].  */
6881         dsc->modinsn[0] = (insn & 0xfff00fff) | 0x20000;
6882       else
6883         /* {ldr,str}[b]<cond> rt, [rn, rm], etc.
6884            ->
6885            {ldr,str}[b]<cond> r0, [r2, r3].  */
6886         dsc->modinsn[0] = (insn & 0xfff00ff0) | 0x20003;
6887     }
6888   else
6889     {
6890       /* We need to use r4 as scratch.  Make sure it's restored afterwards.  */
6891       dsc->u.ldst.restore_r4 = 1;
6892       dsc->modinsn[0] = 0xe92d8000;  /* push {pc} */
6893       dsc->modinsn[1] = 0xe8bd0010;  /* pop  {r4} */
6894       dsc->modinsn[2] = 0xe044400f;  /* sub r4, r4, pc.  */
6895       dsc->modinsn[3] = 0xe2844008;  /* add r4, r4, #8.  */
6896       dsc->modinsn[4] = 0xe0800004;  /* add r0, r0, r4.  */
6897
6898       /* As above.  */
6899       if (immed)
6900         dsc->modinsn[5] = (insn & 0xfff00fff) | 0x20000;
6901       else
6902         dsc->modinsn[5] = (insn & 0xfff00ff0) | 0x20003;
6903
6904       dsc->numinsns = 6;
6905     }
6906
6907   dsc->cleanup = load ? &cleanup_load : &cleanup_store;
6908
6909   return 0;
6910 }
6911
6912 /* Cleanup LDM instructions with fully-populated register list.  This is an
6913    unfortunate corner case: it's impossible to implement correctly by modifying
6914    the instruction.  The issue is as follows: we have an instruction,
6915
6916    ldm rN, {r0-r15}
6917
6918    which we must rewrite to avoid loading PC.  A possible solution would be to
6919    do the load in two halves, something like (with suitable cleanup
6920    afterwards):
6921
6922    mov r8, rN
6923    ldm[id][ab] r8!, {r0-r7}
6924    str r7, <temp>
6925    ldm[id][ab] r8, {r7-r14}
6926    <bkpt>
6927
6928    but at present there's no suitable place for <temp>, since the scratch space
6929    is overwritten before the cleanup routine is called.  For now, we simply
6930    emulate the instruction.  */
6931
6932 static void
6933 cleanup_block_load_all (struct gdbarch *gdbarch, struct regcache *regs,
6934                         struct displaced_step_closure *dsc)
6935 {
6936   int inc = dsc->u.block.increment;
6937   int bump_before = dsc->u.block.before ? (inc ? 4 : -4) : 0;
6938   int bump_after = dsc->u.block.before ? 0 : (inc ? 4 : -4);
6939   uint32_t regmask = dsc->u.block.regmask;
6940   int regno = inc ? 0 : 15;
6941   CORE_ADDR xfer_addr = dsc->u.block.xfer_addr;
6942   int exception_return = dsc->u.block.load && dsc->u.block.user
6943                          && (regmask & 0x8000) != 0;
6944   uint32_t status = displaced_read_reg (regs, dsc, ARM_PS_REGNUM);
6945   int do_transfer = condition_true (dsc->u.block.cond, status);
6946   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
6947
6948   if (!do_transfer)
6949     return;
6950
6951   /* If the instruction is ldm rN, {...pc}^, I don't think there's anything
6952      sensible we can do here.  Complain loudly.  */
6953   if (exception_return)
6954     error (_("Cannot single-step exception return"));
6955
6956   /* We don't handle any stores here for now.  */
6957   gdb_assert (dsc->u.block.load != 0);
6958
6959   if (debug_displaced)
6960     fprintf_unfiltered (gdb_stdlog, "displaced: emulating block transfer: "
6961                         "%s %s %s\n", dsc->u.block.load ? "ldm" : "stm",
6962                         dsc->u.block.increment ? "inc" : "dec",
6963                         dsc->u.block.before ? "before" : "after");
6964
6965   while (regmask)
6966     {
6967       uint32_t memword;
6968
6969       if (inc)
6970         while (regno <= ARM_PC_REGNUM && (regmask & (1 << regno)) == 0)
6971           regno++;
6972       else
6973         while (regno >= 0 && (regmask & (1 << regno)) == 0)
6974           regno--;
6975
6976       xfer_addr += bump_before;
6977
6978       memword = read_memory_unsigned_integer (xfer_addr, 4, byte_order);
6979       displaced_write_reg (regs, dsc, regno, memword, LOAD_WRITE_PC);
6980
6981       xfer_addr += bump_after;
6982
6983       regmask &= ~(1 << regno);
6984     }
6985
6986   if (dsc->u.block.writeback)
6987     displaced_write_reg (regs, dsc, dsc->u.block.rn, xfer_addr,
6988                          CANNOT_WRITE_PC);
6989 }
6990
6991 /* Clean up an STM which included the PC in the register list.  */
6992
6993 static void
6994 cleanup_block_store_pc (struct gdbarch *gdbarch, struct regcache *regs,
6995                         struct displaced_step_closure *dsc)
6996 {
6997   uint32_t status = displaced_read_reg (regs, dsc, ARM_PS_REGNUM);
6998   int store_executed = condition_true (dsc->u.block.cond, status);
6999   CORE_ADDR pc_stored_at, transferred_regs = bitcount (dsc->u.block.regmask);
7000   CORE_ADDR stm_insn_addr;
7001   uint32_t pc_val;
7002   long offset;
7003   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
7004
7005   /* If condition code fails, there's nothing else to do.  */
7006   if (!store_executed)
7007     return;
7008
7009   if (dsc->u.block.increment)
7010     {
7011       pc_stored_at = dsc->u.block.xfer_addr + 4 * transferred_regs;
7012
7013       if (dsc->u.block.before)
7014          pc_stored_at += 4;
7015     }
7016   else
7017     {
7018       pc_stored_at = dsc->u.block.xfer_addr;
7019
7020       if (dsc->u.block.before)
7021          pc_stored_at -= 4;
7022     }
7023
7024   pc_val = read_memory_unsigned_integer (pc_stored_at, 4, byte_order);
7025   stm_insn_addr = dsc->scratch_base;
7026   offset = pc_val - stm_insn_addr;
7027
7028   if (debug_displaced)
7029     fprintf_unfiltered (gdb_stdlog, "displaced: detected PC offset %.8lx for "
7030                         "STM instruction\n", offset);
7031
7032   /* Rewrite the stored PC to the proper value for the non-displaced original
7033      instruction.  */
7034   write_memory_unsigned_integer (pc_stored_at, 4, byte_order,
7035                                  dsc->insn_addr + offset);
7036 }
7037
7038 /* Clean up an LDM which includes the PC in the register list.  We clumped all
7039    the registers in the transferred list into a contiguous range r0...rX (to
7040    avoid loading PC directly and losing control of the debugged program), so we
7041    must undo that here.  */
7042
7043 static void
7044 cleanup_block_load_pc (struct gdbarch *gdbarch,
7045                        struct regcache *regs,
7046                        struct displaced_step_closure *dsc)
7047 {
7048   uint32_t status = displaced_read_reg (regs, dsc, ARM_PS_REGNUM);
7049   int load_executed = condition_true (dsc->u.block.cond, status);
7050   unsigned int mask = dsc->u.block.regmask, write_reg = ARM_PC_REGNUM;
7051   unsigned int regs_loaded = bitcount (mask);
7052   unsigned int num_to_shuffle = regs_loaded, clobbered;
7053
7054   /* The method employed here will fail if the register list is fully populated
7055      (we need to avoid loading PC directly).  */
7056   gdb_assert (num_to_shuffle < 16);
7057
7058   if (!load_executed)
7059     return;
7060
7061   clobbered = (1 << num_to_shuffle) - 1;
7062
7063   while (num_to_shuffle > 0)
7064     {
7065       if ((mask & (1 << write_reg)) != 0)
7066         {
7067           unsigned int read_reg = num_to_shuffle - 1;
7068
7069           if (read_reg != write_reg)
7070             {
7071               ULONGEST rval = displaced_read_reg (regs, dsc, read_reg);
7072               displaced_write_reg (regs, dsc, write_reg, rval, LOAD_WRITE_PC);
7073               if (debug_displaced)
7074                 fprintf_unfiltered (gdb_stdlog, _("displaced: LDM: move "
7075                                     "loaded register r%d to r%d\n"), read_reg,
7076                                     write_reg);
7077             }
7078           else if (debug_displaced)
7079             fprintf_unfiltered (gdb_stdlog, _("displaced: LDM: register "
7080                                 "r%d already in the right place\n"),
7081                                 write_reg);
7082
7083           clobbered &= ~(1 << write_reg);
7084
7085           num_to_shuffle--;
7086         }
7087
7088       write_reg--;
7089     }
7090
7091   /* Restore any registers we scribbled over.  */
7092   for (write_reg = 0; clobbered != 0; write_reg++)
7093     {
7094       if ((clobbered & (1 << write_reg)) != 0)
7095         {
7096           displaced_write_reg (regs, dsc, write_reg, dsc->tmp[write_reg],
7097                                CANNOT_WRITE_PC);
7098           if (debug_displaced)
7099             fprintf_unfiltered (gdb_stdlog, _("displaced: LDM: restored "
7100                                 "clobbered register r%d\n"), write_reg);
7101           clobbered &= ~(1 << write_reg);
7102         }
7103     }
7104
7105   /* Perform register writeback manually.  */
7106   if (dsc->u.block.writeback)
7107     {
7108       ULONGEST new_rn_val = dsc->u.block.xfer_addr;
7109
7110       if (dsc->u.block.increment)
7111         new_rn_val += regs_loaded * 4;
7112       else
7113         new_rn_val -= regs_loaded * 4;
7114
7115       displaced_write_reg (regs, dsc, dsc->u.block.rn, new_rn_val,
7116                            CANNOT_WRITE_PC);
7117     }
7118 }
7119
7120 /* Handle ldm/stm, apart from some tricky cases which are unlikely to occur
7121    in user-level code (in particular exception return, ldm rn, {...pc}^).  */
7122
7123 static int
7124 arm_copy_block_xfer (struct gdbarch *gdbarch, uint32_t insn,
7125                      struct regcache *regs,
7126                      struct displaced_step_closure *dsc)
7127 {
7128   int load = bit (insn, 20);
7129   int user = bit (insn, 22);
7130   int increment = bit (insn, 23);
7131   int before = bit (insn, 24);
7132   int writeback = bit (insn, 21);
7133   int rn = bits (insn, 16, 19);
7134
7135   /* Block transfers which don't mention PC can be run directly
7136      out-of-line.  */
7137   if (rn != ARM_PC_REGNUM && (insn & 0x8000) == 0)
7138     return arm_copy_unmodified (gdbarch, insn, "ldm/stm", dsc);
7139
7140   if (rn == ARM_PC_REGNUM)
7141     {
7142       warning (_("displaced: Unpredictable LDM or STM with "
7143                  "base register r15"));
7144       return arm_copy_unmodified (gdbarch, insn, "unpredictable ldm/stm", dsc);
7145     }
7146
7147   if (debug_displaced)
7148     fprintf_unfiltered (gdb_stdlog, "displaced: copying block transfer insn "
7149                         "%.8lx\n", (unsigned long) insn);
7150
7151   dsc->u.block.xfer_addr = displaced_read_reg (regs, dsc, rn);
7152   dsc->u.block.rn = rn;
7153
7154   dsc->u.block.load = load;
7155   dsc->u.block.user = user;
7156   dsc->u.block.increment = increment;
7157   dsc->u.block.before = before;
7158   dsc->u.block.writeback = writeback;
7159   dsc->u.block.cond = bits (insn, 28, 31);
7160
7161   dsc->u.block.regmask = insn & 0xffff;
7162
7163   if (load)
7164     {
7165       if ((insn & 0xffff) == 0xffff)
7166         {
7167           /* LDM with a fully-populated register list.  This case is
7168              particularly tricky.  Implement for now by fully emulating the
7169              instruction (which might not behave perfectly in all cases, but
7170              these instructions should be rare enough for that not to matter
7171              too much).  */
7172           dsc->modinsn[0] = ARM_NOP;
7173
7174           dsc->cleanup = &cleanup_block_load_all;
7175         }
7176       else
7177         {
7178           /* LDM of a list of registers which includes PC.  Implement by
7179              rewriting the list of registers to be transferred into a
7180              contiguous chunk r0...rX before doing the transfer, then shuffling
7181              registers into the correct places in the cleanup routine.  */
7182           unsigned int regmask = insn & 0xffff;
7183           unsigned int num_in_list = bitcount (regmask), new_regmask, bit = 1;
7184           unsigned int to = 0, from = 0, i, new_rn;
7185
7186           for (i = 0; i < num_in_list; i++)
7187             dsc->tmp[i] = displaced_read_reg (regs, dsc, i);
7188
7189           /* Writeback makes things complicated.  We need to avoid clobbering
7190              the base register with one of the registers in our modified
7191              register list, but just using a different register can't work in
7192              all cases, e.g.:
7193
7194                ldm r14!, {r0-r13,pc}
7195
7196              which would need to be rewritten as:
7197
7198                ldm rN!, {r0-r14}
7199
7200              but that can't work, because there's no free register for N.
7201
7202              Solve this by turning off the writeback bit, and emulating
7203              writeback manually in the cleanup routine.  */
7204
7205           if (writeback)
7206             insn &= ~(1 << 21);
7207
7208           new_regmask = (1 << num_in_list) - 1;
7209
7210           if (debug_displaced)
7211             fprintf_unfiltered (gdb_stdlog, _("displaced: LDM r%d%s, "
7212                                 "{..., pc}: original reg list %.4x, modified "
7213                                 "list %.4x\n"), rn, writeback ? "!" : "",
7214                                 (int) insn & 0xffff, new_regmask);
7215
7216           dsc->modinsn[0] = (insn & ~0xffff) | (new_regmask & 0xffff);
7217
7218           dsc->cleanup = &cleanup_block_load_pc;
7219         }
7220     }
7221   else
7222     {
7223       /* STM of a list of registers which includes PC.  Run the instruction
7224          as-is, but out of line: this will store the wrong value for the PC,
7225          so we must manually fix up the memory in the cleanup routine.
7226          Doing things this way has the advantage that we can auto-detect
7227          the offset of the PC write (which is architecture-dependent) in
7228          the cleanup routine.  */
7229       dsc->modinsn[0] = insn;
7230
7231       dsc->cleanup = &cleanup_block_store_pc;
7232     }
7233
7234   return 0;
7235 }
7236
7237 static int
7238 thumb2_copy_block_xfer (struct gdbarch *gdbarch, uint16_t insn1, uint16_t insn2,
7239                         struct regcache *regs,
7240                         struct displaced_step_closure *dsc)
7241 {
7242   int rn = bits (insn1, 0, 3);
7243   int load = bit (insn1, 4);
7244   int writeback = bit (insn1, 5);
7245
7246   /* Block transfers which don't mention PC can be run directly
7247      out-of-line.  */
7248   if (rn != ARM_PC_REGNUM && (insn2 & 0x8000) == 0)
7249     return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2, "ldm/stm", dsc);
7250
7251   if (rn == ARM_PC_REGNUM)
7252     {
7253       warning (_("displaced: Unpredictable LDM or STM with "
7254                  "base register r15"));
7255       return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
7256                                           "unpredictable ldm/stm", dsc);
7257     }
7258
7259   if (debug_displaced)
7260     fprintf_unfiltered (gdb_stdlog, "displaced: copying block transfer insn "
7261                         "%.4x%.4x\n", insn1, insn2);
7262
7263   /* Clear bit 13, since it should be always zero.  */
7264   dsc->u.block.regmask = (insn2 & 0xdfff);
7265   dsc->u.block.rn = rn;
7266
7267   dsc->u.block.load = load;
7268   dsc->u.block.user = 0;
7269   dsc->u.block.increment = bit (insn1, 7);
7270   dsc->u.block.before = bit (insn1, 8);
7271   dsc->u.block.writeback = writeback;
7272   dsc->u.block.cond = INST_AL;
7273   dsc->u.block.xfer_addr = displaced_read_reg (regs, dsc, rn);
7274
7275   if (load)
7276     {
7277       if (dsc->u.block.regmask == 0xffff)
7278         {
7279           /* This branch is impossible to happen.  */
7280           gdb_assert (0);
7281         }
7282       else
7283         {
7284           unsigned int regmask = dsc->u.block.regmask;
7285           unsigned int num_in_list = bitcount (regmask), new_regmask, bit = 1;
7286           unsigned int to = 0, from = 0, i, new_rn;
7287
7288           for (i = 0; i < num_in_list; i++)
7289             dsc->tmp[i] = displaced_read_reg (regs, dsc, i);
7290
7291           if (writeback)
7292             insn1 &= ~(1 << 5);
7293
7294           new_regmask = (1 << num_in_list) - 1;
7295
7296           if (debug_displaced)
7297             fprintf_unfiltered (gdb_stdlog, _("displaced: LDM r%d%s, "
7298                                 "{..., pc}: original reg list %.4x, modified "
7299                                 "list %.4x\n"), rn, writeback ? "!" : "",
7300                                 (int) dsc->u.block.regmask, new_regmask);
7301
7302           dsc->modinsn[0] = insn1;
7303           dsc->modinsn[1] = (new_regmask & 0xffff);
7304           dsc->numinsns = 2;
7305
7306           dsc->cleanup = &cleanup_block_load_pc;
7307         }
7308     }
7309   else
7310     {
7311       dsc->modinsn[0] = insn1;
7312       dsc->modinsn[1] = insn2;
7313       dsc->numinsns = 2;
7314       dsc->cleanup = &cleanup_block_store_pc;
7315     }
7316   return 0;
7317 }
7318
7319 /* Cleanup/copy SVC (SWI) instructions.  These two functions are overridden
7320    for Linux, where some SVC instructions must be treated specially.  */
7321
7322 static void
7323 cleanup_svc (struct gdbarch *gdbarch, struct regcache *regs,
7324              struct displaced_step_closure *dsc)
7325 {
7326   CORE_ADDR resume_addr = dsc->insn_addr + dsc->insn_size;
7327
7328   if (debug_displaced)
7329     fprintf_unfiltered (gdb_stdlog, "displaced: cleanup for svc, resume at "
7330                         "%.8lx\n", (unsigned long) resume_addr);
7331
7332   displaced_write_reg (regs, dsc, ARM_PC_REGNUM, resume_addr, BRANCH_WRITE_PC);
7333 }
7334
7335
7336 /* Common copy routine for svc instruciton.  */
7337
7338 static int
7339 install_svc (struct gdbarch *gdbarch, struct regcache *regs,
7340              struct displaced_step_closure *dsc)
7341 {
7342   /* Preparation: none.
7343      Insn: unmodified svc.
7344      Cleanup: pc <- insn_addr + insn_size.  */
7345
7346   /* Pretend we wrote to the PC, so cleanup doesn't set PC to the next
7347      instruction.  */
7348   dsc->wrote_to_pc = 1;
7349
7350   /* Allow OS-specific code to override SVC handling.  */
7351   if (dsc->u.svc.copy_svc_os)
7352     return dsc->u.svc.copy_svc_os (gdbarch, regs, dsc);
7353   else
7354     {
7355       dsc->cleanup = &cleanup_svc;
7356       return 0;
7357     }
7358 }
7359
7360 static int
7361 arm_copy_svc (struct gdbarch *gdbarch, uint32_t insn,
7362               struct regcache *regs, struct displaced_step_closure *dsc)
7363 {
7364
7365   if (debug_displaced)
7366     fprintf_unfiltered (gdb_stdlog, "displaced: copying svc insn %.8lx\n",
7367                         (unsigned long) insn);
7368
7369   dsc->modinsn[0] = insn;
7370
7371   return install_svc (gdbarch, regs, dsc);
7372 }
7373
7374 static int
7375 thumb_copy_svc (struct gdbarch *gdbarch, uint16_t insn,
7376                 struct regcache *regs, struct displaced_step_closure *dsc)
7377 {
7378
7379   if (debug_displaced)
7380     fprintf_unfiltered (gdb_stdlog, "displaced: copying svc insn %.4x\n",
7381                         insn);
7382
7383   dsc->modinsn[0] = insn;
7384
7385   return install_svc (gdbarch, regs, dsc);
7386 }
7387
7388 /* Copy undefined instructions.  */
7389
7390 static int
7391 arm_copy_undef (struct gdbarch *gdbarch, uint32_t insn,
7392                 struct displaced_step_closure *dsc)
7393 {
7394   if (debug_displaced)
7395     fprintf_unfiltered (gdb_stdlog,
7396                         "displaced: copying undefined insn %.8lx\n",
7397                         (unsigned long) insn);
7398
7399   dsc->modinsn[0] = insn;
7400
7401   return 0;
7402 }
7403
7404 static int
7405 thumb_32bit_copy_undef (struct gdbarch *gdbarch, uint16_t insn1, uint16_t insn2,
7406                        struct displaced_step_closure *dsc)
7407 {
7408
7409   if (debug_displaced)
7410     fprintf_unfiltered (gdb_stdlog, "displaced: copying undefined insn "
7411                        "%.4x %.4x\n", (unsigned short) insn1,
7412                        (unsigned short) insn2);
7413
7414   dsc->modinsn[0] = insn1;
7415   dsc->modinsn[1] = insn2;
7416   dsc->numinsns = 2;
7417
7418   return 0;
7419 }
7420
7421 /* Copy unpredictable instructions.  */
7422
7423 static int
7424 arm_copy_unpred (struct gdbarch *gdbarch, uint32_t insn,
7425                  struct displaced_step_closure *dsc)
7426 {
7427   if (debug_displaced)
7428     fprintf_unfiltered (gdb_stdlog, "displaced: copying unpredictable insn "
7429                         "%.8lx\n", (unsigned long) insn);
7430
7431   dsc->modinsn[0] = insn;
7432
7433   return 0;
7434 }
7435
7436 /* The decode_* functions are instruction decoding helpers.  They mostly follow
7437    the presentation in the ARM ARM.  */
7438
7439 static int
7440 arm_decode_misc_memhint_neon (struct gdbarch *gdbarch, uint32_t insn,
7441                               struct regcache *regs,
7442                               struct displaced_step_closure *dsc)
7443 {
7444   unsigned int op1 = bits (insn, 20, 26), op2 = bits (insn, 4, 7);
7445   unsigned int rn = bits (insn, 16, 19);
7446
7447   if (op1 == 0x10 && (op2 & 0x2) == 0x0 && (rn & 0xe) == 0x0)
7448     return arm_copy_unmodified (gdbarch, insn, "cps", dsc);
7449   else if (op1 == 0x10 && op2 == 0x0 && (rn & 0xe) == 0x1)
7450     return arm_copy_unmodified (gdbarch, insn, "setend", dsc);
7451   else if ((op1 & 0x60) == 0x20)
7452     return arm_copy_unmodified (gdbarch, insn, "neon dataproc", dsc);
7453   else if ((op1 & 0x71) == 0x40)
7454     return arm_copy_unmodified (gdbarch, insn, "neon elt/struct load/store",
7455                                 dsc);
7456   else if ((op1 & 0x77) == 0x41)
7457     return arm_copy_unmodified (gdbarch, insn, "unallocated mem hint", dsc);
7458   else if ((op1 & 0x77) == 0x45)
7459     return arm_copy_preload (gdbarch, insn, regs, dsc);  /* pli.  */
7460   else if ((op1 & 0x77) == 0x51)
7461     {
7462       if (rn != 0xf)
7463         return arm_copy_preload (gdbarch, insn, regs, dsc);  /* pld/pldw.  */
7464       else
7465         return arm_copy_unpred (gdbarch, insn, dsc);
7466     }
7467   else if ((op1 & 0x77) == 0x55)
7468     return arm_copy_preload (gdbarch, insn, regs, dsc);  /* pld/pldw.  */
7469   else if (op1 == 0x57)
7470     switch (op2)
7471       {
7472       case 0x1: return arm_copy_unmodified (gdbarch, insn, "clrex", dsc);
7473       case 0x4: return arm_copy_unmodified (gdbarch, insn, "dsb", dsc);
7474       case 0x5: return arm_copy_unmodified (gdbarch, insn, "dmb", dsc);
7475       case 0x6: return arm_copy_unmodified (gdbarch, insn, "isb", dsc);
7476       default: return arm_copy_unpred (gdbarch, insn, dsc);
7477       }
7478   else if ((op1 & 0x63) == 0x43)
7479     return arm_copy_unpred (gdbarch, insn, dsc);
7480   else if ((op2 & 0x1) == 0x0)
7481     switch (op1 & ~0x80)
7482       {
7483       case 0x61:
7484         return arm_copy_unmodified (gdbarch, insn, "unallocated mem hint", dsc);
7485       case 0x65:
7486         return arm_copy_preload_reg (gdbarch, insn, regs, dsc);  /* pli reg.  */
7487       case 0x71: case 0x75:
7488         /* pld/pldw reg.  */
7489         return arm_copy_preload_reg (gdbarch, insn, regs, dsc);
7490       case 0x63: case 0x67: case 0x73: case 0x77:
7491         return arm_copy_unpred (gdbarch, insn, dsc);
7492       default:
7493         return arm_copy_undef (gdbarch, insn, dsc);
7494       }
7495   else
7496     return arm_copy_undef (gdbarch, insn, dsc);  /* Probably unreachable.  */
7497 }
7498
7499 static int
7500 arm_decode_unconditional (struct gdbarch *gdbarch, uint32_t insn,
7501                           struct regcache *regs,
7502                           struct displaced_step_closure *dsc)
7503 {
7504   if (bit (insn, 27) == 0)
7505     return arm_decode_misc_memhint_neon (gdbarch, insn, regs, dsc);
7506   /* Switch on bits: 0bxxxxx321xxx0xxxxxxxxxxxxxxxxxxxx.  */
7507   else switch (((insn & 0x7000000) >> 23) | ((insn & 0x100000) >> 20))
7508     {
7509     case 0x0: case 0x2:
7510       return arm_copy_unmodified (gdbarch, insn, "srs", dsc);
7511
7512     case 0x1: case 0x3:
7513       return arm_copy_unmodified (gdbarch, insn, "rfe", dsc);
7514
7515     case 0x4: case 0x5: case 0x6: case 0x7:
7516       return arm_copy_b_bl_blx (gdbarch, insn, regs, dsc);
7517
7518     case 0x8:
7519       switch ((insn & 0xe00000) >> 21)
7520         {
7521         case 0x1: case 0x3: case 0x4: case 0x5: case 0x6: case 0x7:
7522           /* stc/stc2.  */
7523           return arm_copy_copro_load_store (gdbarch, insn, regs, dsc);
7524
7525         case 0x2:
7526           return arm_copy_unmodified (gdbarch, insn, "mcrr/mcrr2", dsc);
7527
7528         default:
7529           return arm_copy_undef (gdbarch, insn, dsc);
7530         }
7531
7532     case 0x9:
7533       {
7534          int rn_f = (bits (insn, 16, 19) == 0xf);
7535         switch ((insn & 0xe00000) >> 21)
7536           {
7537           case 0x1: case 0x3:
7538             /* ldc/ldc2 imm (undefined for rn == pc).  */
7539             return rn_f ? arm_copy_undef (gdbarch, insn, dsc)
7540                         : arm_copy_copro_load_store (gdbarch, insn, regs, dsc);
7541
7542           case 0x2:
7543             return arm_copy_unmodified (gdbarch, insn, "mrrc/mrrc2", dsc);
7544
7545           case 0x4: case 0x5: case 0x6: case 0x7:
7546             /* ldc/ldc2 lit (undefined for rn != pc).  */
7547             return rn_f ? arm_copy_copro_load_store (gdbarch, insn, regs, dsc)
7548                         : arm_copy_undef (gdbarch, insn, dsc);
7549
7550           default:
7551             return arm_copy_undef (gdbarch, insn, dsc);
7552           }
7553       }
7554
7555     case 0xa:
7556       return arm_copy_unmodified (gdbarch, insn, "stc/stc2", dsc);
7557
7558     case 0xb:
7559       if (bits (insn, 16, 19) == 0xf)
7560         /* ldc/ldc2 lit.  */
7561         return arm_copy_copro_load_store (gdbarch, insn, regs, dsc);
7562       else
7563         return arm_copy_undef (gdbarch, insn, dsc);
7564
7565     case 0xc:
7566       if (bit (insn, 4))
7567         return arm_copy_unmodified (gdbarch, insn, "mcr/mcr2", dsc);
7568       else
7569         return arm_copy_unmodified (gdbarch, insn, "cdp/cdp2", dsc);
7570
7571     case 0xd:
7572       if (bit (insn, 4))
7573         return arm_copy_unmodified (gdbarch, insn, "mrc/mrc2", dsc);
7574       else
7575         return arm_copy_unmodified (gdbarch, insn, "cdp/cdp2", dsc);
7576
7577     default:
7578       return arm_copy_undef (gdbarch, insn, dsc);
7579     }
7580 }
7581
7582 /* Decode miscellaneous instructions in dp/misc encoding space.  */
7583
7584 static int
7585 arm_decode_miscellaneous (struct gdbarch *gdbarch, uint32_t insn,
7586                           struct regcache *regs,
7587                           struct displaced_step_closure *dsc)
7588 {
7589   unsigned int op2 = bits (insn, 4, 6);
7590   unsigned int op = bits (insn, 21, 22);
7591   unsigned int op1 = bits (insn, 16, 19);
7592
7593   switch (op2)
7594     {
7595     case 0x0:
7596       return arm_copy_unmodified (gdbarch, insn, "mrs/msr", dsc);
7597
7598     case 0x1:
7599       if (op == 0x1)  /* bx.  */
7600         return arm_copy_bx_blx_reg (gdbarch, insn, regs, dsc);
7601       else if (op == 0x3)
7602         return arm_copy_unmodified (gdbarch, insn, "clz", dsc);
7603       else
7604         return arm_copy_undef (gdbarch, insn, dsc);
7605
7606     case 0x2:
7607       if (op == 0x1)
7608         /* Not really supported.  */
7609         return arm_copy_unmodified (gdbarch, insn, "bxj", dsc);
7610       else
7611         return arm_copy_undef (gdbarch, insn, dsc);
7612
7613     case 0x3:
7614       if (op == 0x1)
7615         return arm_copy_bx_blx_reg (gdbarch, insn,
7616                                 regs, dsc);  /* blx register.  */
7617       else
7618         return arm_copy_undef (gdbarch, insn, dsc);
7619
7620     case 0x5:
7621       return arm_copy_unmodified (gdbarch, insn, "saturating add/sub", dsc);
7622
7623     case 0x7:
7624       if (op == 0x1)
7625         return arm_copy_unmodified (gdbarch, insn, "bkpt", dsc);
7626       else if (op == 0x3)
7627         /* Not really supported.  */
7628         return arm_copy_unmodified (gdbarch, insn, "smc", dsc);
7629
7630     default:
7631       return arm_copy_undef (gdbarch, insn, dsc);
7632     }
7633 }
7634
7635 static int
7636 arm_decode_dp_misc (struct gdbarch *gdbarch, uint32_t insn,
7637                     struct regcache *regs,
7638                     struct displaced_step_closure *dsc)
7639 {
7640   if (bit (insn, 25))
7641     switch (bits (insn, 20, 24))
7642       {
7643       case 0x10:
7644         return arm_copy_unmodified (gdbarch, insn, "movw", dsc);
7645
7646       case 0x14:
7647         return arm_copy_unmodified (gdbarch, insn, "movt", dsc);
7648
7649       case 0x12: case 0x16:
7650         return arm_copy_unmodified (gdbarch, insn, "msr imm", dsc);
7651
7652       default:
7653         return arm_copy_alu_imm (gdbarch, insn, regs, dsc);
7654       }
7655   else
7656     {
7657       uint32_t op1 = bits (insn, 20, 24), op2 = bits (insn, 4, 7);
7658
7659       if ((op1 & 0x19) != 0x10 && (op2 & 0x1) == 0x0)
7660         return arm_copy_alu_reg (gdbarch, insn, regs, dsc);
7661       else if ((op1 & 0x19) != 0x10 && (op2 & 0x9) == 0x1)
7662         return arm_copy_alu_shifted_reg (gdbarch, insn, regs, dsc);
7663       else if ((op1 & 0x19) == 0x10 && (op2 & 0x8) == 0x0)
7664         return arm_decode_miscellaneous (gdbarch, insn, regs, dsc);
7665       else if ((op1 & 0x19) == 0x10 && (op2 & 0x9) == 0x8)
7666         return arm_copy_unmodified (gdbarch, insn, "halfword mul/mla", dsc);
7667       else if ((op1 & 0x10) == 0x00 && op2 == 0x9)
7668         return arm_copy_unmodified (gdbarch, insn, "mul/mla", dsc);
7669       else if ((op1 & 0x10) == 0x10 && op2 == 0x9)
7670         return arm_copy_unmodified (gdbarch, insn, "synch", dsc);
7671       else if (op2 == 0xb || (op2 & 0xd) == 0xd)
7672         /* 2nd arg means "unpriveleged".  */
7673         return arm_copy_extra_ld_st (gdbarch, insn, (op1 & 0x12) == 0x02, regs,
7674                                      dsc);
7675     }
7676
7677   /* Should be unreachable.  */
7678   return 1;
7679 }
7680
7681 static int
7682 arm_decode_ld_st_word_ubyte (struct gdbarch *gdbarch, uint32_t insn,
7683                              struct regcache *regs,
7684                              struct displaced_step_closure *dsc)
7685 {
7686   int a = bit (insn, 25), b = bit (insn, 4);
7687   uint32_t op1 = bits (insn, 20, 24);
7688   int rn_f = bits (insn, 16, 19) == 0xf;
7689
7690   if ((!a && (op1 & 0x05) == 0x00 && (op1 & 0x17) != 0x02)
7691       || (a && (op1 & 0x05) == 0x00 && (op1 & 0x17) != 0x02 && !b))
7692     return arm_copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 0, 4, 0);
7693   else if ((!a && (op1 & 0x17) == 0x02)
7694             || (a && (op1 & 0x17) == 0x02 && !b))
7695     return arm_copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 0, 4, 1);
7696   else if ((!a && (op1 & 0x05) == 0x01 && (op1 & 0x17) != 0x03)
7697             || (a && (op1 & 0x05) == 0x01 && (op1 & 0x17) != 0x03 && !b))
7698     return arm_copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 1, 4, 0);
7699   else if ((!a && (op1 & 0x17) == 0x03)
7700            || (a && (op1 & 0x17) == 0x03 && !b))
7701     return arm_copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 1, 4, 1);
7702   else if ((!a && (op1 & 0x05) == 0x04 && (op1 & 0x17) != 0x06)
7703             || (a && (op1 & 0x05) == 0x04 && (op1 & 0x17) != 0x06 && !b))
7704     return arm_copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 0, 1, 0);
7705   else if ((!a && (op1 & 0x17) == 0x06)
7706            || (a && (op1 & 0x17) == 0x06 && !b))
7707     return arm_copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 0, 1, 1);
7708   else if ((!a && (op1 & 0x05) == 0x05 && (op1 & 0x17) != 0x07)
7709            || (a && (op1 & 0x05) == 0x05 && (op1 & 0x17) != 0x07 && !b))
7710     return arm_copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 1, 1, 0);
7711   else if ((!a && (op1 & 0x17) == 0x07)
7712            || (a && (op1 & 0x17) == 0x07 && !b))
7713     return arm_copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 1, 1, 1);
7714
7715   /* Should be unreachable.  */
7716   return 1;
7717 }
7718
7719 static int
7720 arm_decode_media (struct gdbarch *gdbarch, uint32_t insn,
7721                   struct displaced_step_closure *dsc)
7722 {
7723   switch (bits (insn, 20, 24))
7724     {
7725     case 0x00: case 0x01: case 0x02: case 0x03:
7726       return arm_copy_unmodified (gdbarch, insn, "parallel add/sub signed", dsc);
7727
7728     case 0x04: case 0x05: case 0x06: case 0x07:
7729       return arm_copy_unmodified (gdbarch, insn, "parallel add/sub unsigned", dsc);
7730
7731     case 0x08: case 0x09: case 0x0a: case 0x0b:
7732     case 0x0c: case 0x0d: case 0x0e: case 0x0f:
7733       return arm_copy_unmodified (gdbarch, insn,
7734                               "decode/pack/unpack/saturate/reverse", dsc);
7735
7736     case 0x18:
7737       if (bits (insn, 5, 7) == 0)  /* op2.  */
7738          {
7739           if (bits (insn, 12, 15) == 0xf)
7740             return arm_copy_unmodified (gdbarch, insn, "usad8", dsc);
7741           else
7742             return arm_copy_unmodified (gdbarch, insn, "usada8", dsc);
7743         }
7744       else
7745          return arm_copy_undef (gdbarch, insn, dsc);
7746
7747     case 0x1a: case 0x1b:
7748       if (bits (insn, 5, 6) == 0x2)  /* op2[1:0].  */
7749         return arm_copy_unmodified (gdbarch, insn, "sbfx", dsc);
7750       else
7751         return arm_copy_undef (gdbarch, insn, dsc);
7752
7753     case 0x1c: case 0x1d:
7754       if (bits (insn, 5, 6) == 0x0)  /* op2[1:0].  */
7755          {
7756           if (bits (insn, 0, 3) == 0xf)
7757             return arm_copy_unmodified (gdbarch, insn, "bfc", dsc);
7758           else
7759             return arm_copy_unmodified (gdbarch, insn, "bfi", dsc);
7760         }
7761       else
7762         return arm_copy_undef (gdbarch, insn, dsc);
7763
7764     case 0x1e: case 0x1f:
7765       if (bits (insn, 5, 6) == 0x2)  /* op2[1:0].  */
7766         return arm_copy_unmodified (gdbarch, insn, "ubfx", dsc);
7767       else
7768         return arm_copy_undef (gdbarch, insn, dsc);
7769     }
7770
7771   /* Should be unreachable.  */
7772   return 1;
7773 }
7774
7775 static int
7776 arm_decode_b_bl_ldmstm (struct gdbarch *gdbarch, int32_t insn,
7777                         struct regcache *regs,
7778                         struct displaced_step_closure *dsc)
7779 {
7780   if (bit (insn, 25))
7781     return arm_copy_b_bl_blx (gdbarch, insn, regs, dsc);
7782   else
7783     return arm_copy_block_xfer (gdbarch, insn, regs, dsc);
7784 }
7785
7786 static int
7787 arm_decode_ext_reg_ld_st (struct gdbarch *gdbarch, uint32_t insn,
7788                           struct regcache *regs,
7789                           struct displaced_step_closure *dsc)
7790 {
7791   unsigned int opcode = bits (insn, 20, 24);
7792
7793   switch (opcode)
7794     {
7795     case 0x04: case 0x05:  /* VFP/Neon mrrc/mcrr.  */
7796       return arm_copy_unmodified (gdbarch, insn, "vfp/neon mrrc/mcrr", dsc);
7797
7798     case 0x08: case 0x0a: case 0x0c: case 0x0e:
7799     case 0x12: case 0x16:
7800       return arm_copy_unmodified (gdbarch, insn, "vfp/neon vstm/vpush", dsc);
7801
7802     case 0x09: case 0x0b: case 0x0d: case 0x0f:
7803     case 0x13: case 0x17:
7804       return arm_copy_unmodified (gdbarch, insn, "vfp/neon vldm/vpop", dsc);
7805
7806     case 0x10: case 0x14: case 0x18: case 0x1c:  /* vstr.  */
7807     case 0x11: case 0x15: case 0x19: case 0x1d:  /* vldr.  */
7808       /* Note: no writeback for these instructions.  Bit 25 will always be
7809          zero though (via caller), so the following works OK.  */
7810       return arm_copy_copro_load_store (gdbarch, insn, regs, dsc);
7811     }
7812
7813   /* Should be unreachable.  */
7814   return 1;
7815 }
7816
7817 /* Decode shifted register instructions.  */
7818
7819 static int
7820 thumb2_decode_dp_shift_reg (struct gdbarch *gdbarch, uint16_t insn1,
7821                             uint16_t insn2,  struct regcache *regs,
7822                             struct displaced_step_closure *dsc)
7823 {
7824   /* PC is only allowed to be used in instruction MOV.  */
7825
7826   unsigned int op = bits (insn1, 5, 8);
7827   unsigned int rn = bits (insn1, 0, 3);
7828
7829   if (op == 0x2 && rn == 0xf) /* MOV */
7830     return thumb2_copy_alu_imm (gdbarch, insn1, insn2, regs, dsc);
7831   else
7832     return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
7833                                         "dp (shift reg)", dsc);
7834 }
7835
7836
7837 /* Decode extension register load/store.  Exactly the same as
7838    arm_decode_ext_reg_ld_st.  */
7839
7840 static int
7841 thumb2_decode_ext_reg_ld_st (struct gdbarch *gdbarch, uint16_t insn1,
7842                              uint16_t insn2,  struct regcache *regs,
7843                              struct displaced_step_closure *dsc)
7844 {
7845   unsigned int opcode = bits (insn1, 4, 8);
7846
7847   switch (opcode)
7848     {
7849     case 0x04: case 0x05:
7850       return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
7851                                           "vfp/neon vmov", dsc);
7852
7853     case 0x08: case 0x0c: /* 01x00 */
7854     case 0x0a: case 0x0e: /* 01x10 */
7855     case 0x12: case 0x16: /* 10x10 */
7856       return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
7857                                           "vfp/neon vstm/vpush", dsc);
7858
7859     case 0x09: case 0x0d: /* 01x01 */
7860     case 0x0b: case 0x0f: /* 01x11 */
7861     case 0x13: case 0x17: /* 10x11 */
7862       return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
7863                                           "vfp/neon vldm/vpop", dsc);
7864
7865     case 0x10: case 0x14: case 0x18: case 0x1c:  /* vstr.  */
7866       return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
7867                                           "vstr", dsc);
7868     case 0x11: case 0x15: case 0x19: case 0x1d:  /* vldr.  */
7869       return thumb2_copy_copro_load_store (gdbarch, insn1, insn2, regs, dsc);
7870     }
7871
7872   /* Should be unreachable.  */
7873   return 1;
7874 }
7875
7876 static int
7877 arm_decode_svc_copro (struct gdbarch *gdbarch, uint32_t insn, CORE_ADDR to,
7878                       struct regcache *regs, struct displaced_step_closure *dsc)
7879 {
7880   unsigned int op1 = bits (insn, 20, 25);
7881   int op = bit (insn, 4);
7882   unsigned int coproc = bits (insn, 8, 11);
7883   unsigned int rn = bits (insn, 16, 19);
7884
7885   if ((op1 & 0x20) == 0x00 && (op1 & 0x3a) != 0x00 && (coproc & 0xe) == 0xa)
7886     return arm_decode_ext_reg_ld_st (gdbarch, insn, regs, dsc);
7887   else if ((op1 & 0x21) == 0x00 && (op1 & 0x3a) != 0x00
7888            && (coproc & 0xe) != 0xa)
7889     /* stc/stc2.  */
7890     return arm_copy_copro_load_store (gdbarch, insn, regs, dsc);
7891   else if ((op1 & 0x21) == 0x01 && (op1 & 0x3a) != 0x00
7892            && (coproc & 0xe) != 0xa)
7893     /* ldc/ldc2 imm/lit.  */
7894     return arm_copy_copro_load_store (gdbarch, insn, regs, dsc);
7895   else if ((op1 & 0x3e) == 0x00)
7896     return arm_copy_undef (gdbarch, insn, dsc);
7897   else if ((op1 & 0x3e) == 0x04 && (coproc & 0xe) == 0xa)
7898     return arm_copy_unmodified (gdbarch, insn, "neon 64bit xfer", dsc);
7899   else if (op1 == 0x04 && (coproc & 0xe) != 0xa)
7900     return arm_copy_unmodified (gdbarch, insn, "mcrr/mcrr2", dsc);
7901   else if (op1 == 0x05 && (coproc & 0xe) != 0xa)
7902     return arm_copy_unmodified (gdbarch, insn, "mrrc/mrrc2", dsc);
7903   else if ((op1 & 0x30) == 0x20 && !op)
7904     {
7905       if ((coproc & 0xe) == 0xa)
7906         return arm_copy_unmodified (gdbarch, insn, "vfp dataproc", dsc);
7907       else
7908         return arm_copy_unmodified (gdbarch, insn, "cdp/cdp2", dsc);
7909     }
7910   else if ((op1 & 0x30) == 0x20 && op)
7911     return arm_copy_unmodified (gdbarch, insn, "neon 8/16/32 bit xfer", dsc);
7912   else if ((op1 & 0x31) == 0x20 && op && (coproc & 0xe) != 0xa)
7913     return arm_copy_unmodified (gdbarch, insn, "mcr/mcr2", dsc);
7914   else if ((op1 & 0x31) == 0x21 && op && (coproc & 0xe) != 0xa)
7915     return arm_copy_unmodified (gdbarch, insn, "mrc/mrc2", dsc);
7916   else if ((op1 & 0x30) == 0x30)
7917     return arm_copy_svc (gdbarch, insn, regs, dsc);
7918   else
7919     return arm_copy_undef (gdbarch, insn, dsc);  /* Possibly unreachable.  */
7920 }
7921
7922 static int
7923 thumb2_decode_svc_copro (struct gdbarch *gdbarch, uint16_t insn1,
7924                          uint16_t insn2, struct regcache *regs,
7925                          struct displaced_step_closure *dsc)
7926 {
7927   unsigned int coproc = bits (insn2, 8, 11);
7928   unsigned int op1 = bits (insn1, 4, 9);
7929   unsigned int bit_5_8 = bits (insn1, 5, 8);
7930   unsigned int bit_9 = bit (insn1, 9);
7931   unsigned int bit_4 = bit (insn1, 4);
7932   unsigned int rn = bits (insn1, 0, 3);
7933
7934   if (bit_9 == 0)
7935     {
7936       if (bit_5_8 == 2)
7937         return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
7938                                             "neon 64bit xfer/mrrc/mrrc2/mcrr/mcrr2",
7939                                             dsc);
7940       else if (bit_5_8 == 0) /* UNDEFINED.  */
7941         return thumb_32bit_copy_undef (gdbarch, insn1, insn2, dsc);
7942       else
7943         {
7944            /*coproc is 101x.  SIMD/VFP, ext registers load/store.  */
7945           if ((coproc & 0xe) == 0xa)
7946             return thumb2_decode_ext_reg_ld_st (gdbarch, insn1, insn2, regs,
7947                                                 dsc);
7948           else /* coproc is not 101x.  */
7949             {
7950               if (bit_4 == 0) /* STC/STC2.  */
7951                 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
7952                                                     "stc/stc2", dsc);
7953               else /* LDC/LDC2 {literal, immeidate}.  */
7954                 return thumb2_copy_copro_load_store (gdbarch, insn1, insn2,
7955                                                      regs, dsc);
7956             }
7957         }
7958     }
7959   else
7960     return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2, "coproc", dsc);
7961
7962   return 0;
7963 }
7964
7965 static void
7966 install_pc_relative (struct gdbarch *gdbarch, struct regcache *regs,
7967                      struct displaced_step_closure *dsc, int rd)
7968 {
7969   /* ADR Rd, #imm
7970
7971      Rewrite as:
7972
7973      Preparation: Rd <- PC
7974      Insn: ADD Rd, #imm
7975      Cleanup: Null.
7976   */
7977
7978   /* Rd <- PC */
7979   int val = displaced_read_reg (regs, dsc, ARM_PC_REGNUM);
7980   displaced_write_reg (regs, dsc, rd, val, CANNOT_WRITE_PC);
7981 }
7982
7983 static int
7984 thumb_copy_pc_relative_16bit (struct gdbarch *gdbarch, struct regcache *regs,
7985                               struct displaced_step_closure *dsc,
7986                               int rd, unsigned int imm)
7987 {
7988
7989   /* Encoding T2: ADDS Rd, #imm */
7990   dsc->modinsn[0] = (0x3000 | (rd << 8) | imm);
7991
7992   install_pc_relative (gdbarch, regs, dsc, rd);
7993
7994   return 0;
7995 }
7996
7997 static int
7998 thumb_decode_pc_relative_16bit (struct gdbarch *gdbarch, uint16_t insn,
7999                                 struct regcache *regs,
8000                                 struct displaced_step_closure *dsc)
8001 {
8002   unsigned int rd = bits (insn, 8, 10);
8003   unsigned int imm8 = bits (insn, 0, 7);
8004
8005   if (debug_displaced)
8006     fprintf_unfiltered (gdb_stdlog,
8007                         "displaced: copying thumb adr r%d, #%d insn %.4x\n",
8008                         rd, imm8, insn);
8009
8010   return thumb_copy_pc_relative_16bit (gdbarch, regs, dsc, rd, imm8);
8011 }
8012
8013 static int
8014 thumb_copy_pc_relative_32bit (struct gdbarch *gdbarch, uint16_t insn1,
8015                               uint16_t insn2, struct regcache *regs,
8016                               struct displaced_step_closure *dsc)
8017 {
8018   unsigned int rd = bits (insn2, 8, 11);
8019   /* Since immediate has the same encoding in ADR ADD and SUB, so we simply
8020      extract raw immediate encoding rather than computing immediate.  When
8021      generating ADD or SUB instruction, we can simply perform OR operation to
8022      set immediate into ADD.  */
8023   unsigned int imm_3_8 = insn2 & 0x70ff;
8024   unsigned int imm_i = insn1 & 0x0400; /* Clear all bits except bit 10.  */
8025
8026   if (debug_displaced)
8027     fprintf_unfiltered (gdb_stdlog,
8028                         "displaced: copying thumb adr r%d, #%d:%d insn %.4x%.4x\n",
8029                         rd, imm_i, imm_3_8, insn1, insn2);
8030
8031   if (bit (insn1, 7)) /* Encoding T2 */
8032     {
8033       /* Encoding T3: SUB Rd, Rd, #imm */
8034       dsc->modinsn[0] = (0xf1a0 | rd | imm_i);
8035       dsc->modinsn[1] = ((rd << 8) | imm_3_8);
8036     }
8037   else /* Encoding T3 */
8038     {
8039       /* Encoding T3: ADD Rd, Rd, #imm */
8040       dsc->modinsn[0] = (0xf100 | rd | imm_i);
8041       dsc->modinsn[1] = ((rd << 8) | imm_3_8);
8042     }
8043   dsc->numinsns = 2;
8044
8045   install_pc_relative (gdbarch, regs, dsc, rd);
8046
8047   return 0;
8048 }
8049
8050 static int
8051 thumb_copy_16bit_ldr_literal (struct gdbarch *gdbarch, unsigned short insn1,
8052                               struct regcache *regs,
8053                               struct displaced_step_closure *dsc)
8054 {
8055   unsigned int rt = bits (insn1, 8, 10);
8056   unsigned int pc;
8057   int imm8 = (bits (insn1, 0, 7) << 2);
8058   CORE_ADDR from = dsc->insn_addr;
8059
8060   /* LDR Rd, #imm8
8061
8062      Rwrite as:
8063
8064      Preparation: tmp0 <- R0, tmp2 <- R2, tmp3 <- R3, R2 <- PC, R3 <- #imm8;
8065
8066      Insn: LDR R0, [R2, R3];
8067      Cleanup: R2 <- tmp2, R3 <- tmp3, Rd <- R0, R0 <- tmp0 */
8068
8069   if (debug_displaced)
8070     fprintf_unfiltered (gdb_stdlog,
8071                         "displaced: copying thumb ldr r%d [pc #%d]\n"
8072                         , rt, imm8);
8073
8074   dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
8075   dsc->tmp[2] = displaced_read_reg (regs, dsc, 2);
8076   dsc->tmp[3] = displaced_read_reg (regs, dsc, 3);
8077   pc = displaced_read_reg (regs, dsc, ARM_PC_REGNUM);
8078   /* The assembler calculates the required value of the offset from the
8079      Align(PC,4) value of this instruction to the label.  */
8080   pc = pc & 0xfffffffc;
8081
8082   displaced_write_reg (regs, dsc, 2, pc, CANNOT_WRITE_PC);
8083   displaced_write_reg (regs, dsc, 3, imm8, CANNOT_WRITE_PC);
8084
8085   dsc->rd = rt;
8086   dsc->u.ldst.xfersize = 4;
8087   dsc->u.ldst.rn = 0;
8088   dsc->u.ldst.immed = 0;
8089   dsc->u.ldst.writeback = 0;
8090   dsc->u.ldst.restore_r4 = 0;
8091
8092   dsc->modinsn[0] = 0x58d0; /* ldr r0, [r2, r3]*/
8093
8094   dsc->cleanup = &cleanup_load;
8095
8096   return 0;
8097 }
8098
8099 /* Copy Thumb cbnz/cbz insruction.  */
8100
8101 static int
8102 thumb_copy_cbnz_cbz (struct gdbarch *gdbarch, uint16_t insn1,
8103                      struct regcache *regs,
8104                      struct displaced_step_closure *dsc)
8105 {
8106   int non_zero = bit (insn1, 11);
8107   unsigned int imm5 = (bit (insn1, 9) << 6) | (bits (insn1, 3, 7) << 1);
8108   CORE_ADDR from = dsc->insn_addr;
8109   int rn = bits (insn1, 0, 2);
8110   int rn_val = displaced_read_reg (regs, dsc, rn);
8111
8112   dsc->u.branch.cond = (rn_val && non_zero) || (!rn_val && !non_zero);
8113   /* CBNZ and CBZ do not affect the condition flags.  If condition is true,
8114      set it INST_AL, so cleanup_branch will know branch is taken, otherwise,
8115      condition is false, let it be, cleanup_branch will do nothing.  */
8116   if (dsc->u.branch.cond)
8117     {
8118       dsc->u.branch.cond = INST_AL;
8119       dsc->u.branch.dest = from + 4 + imm5;
8120     }
8121   else
8122       dsc->u.branch.dest = from + 2;
8123
8124   dsc->u.branch.link = 0;
8125   dsc->u.branch.exchange = 0;
8126
8127   if (debug_displaced)
8128     fprintf_unfiltered (gdb_stdlog, "displaced: copying %s [r%d = 0x%x]"
8129                         " insn %.4x to %.8lx\n", non_zero ? "cbnz" : "cbz",
8130                         rn, rn_val, insn1, dsc->u.branch.dest);
8131
8132   dsc->modinsn[0] = THUMB_NOP;
8133
8134   dsc->cleanup = &cleanup_branch;
8135   return 0;
8136 }
8137
8138 /* Copy Table Branch Byte/Halfword */
8139 static int
8140 thumb2_copy_table_branch (struct gdbarch *gdbarch, uint16_t insn1,
8141                           uint16_t insn2, struct regcache *regs,
8142                           struct displaced_step_closure *dsc)
8143 {
8144   ULONGEST rn_val, rm_val;
8145   int is_tbh = bit (insn2, 4);
8146   CORE_ADDR halfwords = 0;
8147   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
8148
8149   rn_val = displaced_read_reg (regs, dsc, bits (insn1, 0, 3));
8150   rm_val = displaced_read_reg (regs, dsc, bits (insn2, 0, 3));
8151
8152   if (is_tbh)
8153     {
8154       gdb_byte buf[2];
8155
8156       target_read_memory (rn_val + 2 * rm_val, buf, 2);
8157       halfwords = extract_unsigned_integer (buf, 2, byte_order);
8158     }
8159   else
8160     {
8161       gdb_byte buf[1];
8162
8163       target_read_memory (rn_val + rm_val, buf, 1);
8164       halfwords = extract_unsigned_integer (buf, 1, byte_order);
8165     }
8166
8167   if (debug_displaced)
8168     fprintf_unfiltered (gdb_stdlog, "displaced: %s base 0x%x offset 0x%x"
8169                         " offset 0x%x\n", is_tbh ? "tbh" : "tbb",
8170                         (unsigned int) rn_val, (unsigned int) rm_val,
8171                         (unsigned int) halfwords);
8172
8173   dsc->u.branch.cond = INST_AL;
8174   dsc->u.branch.link = 0;
8175   dsc->u.branch.exchange = 0;
8176   dsc->u.branch.dest = dsc->insn_addr + 4 + 2 * halfwords;
8177
8178   dsc->cleanup = &cleanup_branch;
8179
8180   return 0;
8181 }
8182
8183 static void
8184 cleanup_pop_pc_16bit_all (struct gdbarch *gdbarch, struct regcache *regs,
8185                           struct displaced_step_closure *dsc)
8186 {
8187   /* PC <- r7 */
8188   int val = displaced_read_reg (regs, dsc, 7);
8189   displaced_write_reg (regs, dsc, ARM_PC_REGNUM, val, BX_WRITE_PC);
8190
8191   /* r7 <- r8 */
8192   val = displaced_read_reg (regs, dsc, 8);
8193   displaced_write_reg (regs, dsc, 7, val, CANNOT_WRITE_PC);
8194
8195   /* r8 <- tmp[0] */
8196   displaced_write_reg (regs, dsc, 8, dsc->tmp[0], CANNOT_WRITE_PC);
8197
8198 }
8199
8200 static int
8201 thumb_copy_pop_pc_16bit (struct gdbarch *gdbarch, unsigned short insn1,
8202                          struct regcache *regs,
8203                          struct displaced_step_closure *dsc)
8204 {
8205   dsc->u.block.regmask = insn1 & 0x00ff;
8206
8207   /* Rewrite instruction: POP {rX, rY, ...,rZ, PC}
8208      to :
8209
8210      (1) register list is full, that is, r0-r7 are used.
8211      Prepare: tmp[0] <- r8
8212
8213      POP {r0, r1, ...., r6, r7}; remove PC from reglist
8214      MOV r8, r7; Move value of r7 to r8;
8215      POP {r7}; Store PC value into r7.
8216
8217      Cleanup: PC <- r7, r7 <- r8, r8 <-tmp[0]
8218
8219      (2) register list is not full, supposing there are N registers in
8220      register list (except PC, 0 <= N <= 7).
8221      Prepare: for each i, 0 - N, tmp[i] <- ri.
8222
8223      POP {r0, r1, ...., rN};
8224
8225      Cleanup: Set registers in original reglist from r0 - rN.  Restore r0 - rN
8226      from tmp[] properly.
8227   */
8228   if (debug_displaced)
8229     fprintf_unfiltered (gdb_stdlog,
8230                         "displaced: copying thumb pop {%.8x, pc} insn %.4x\n",
8231                         dsc->u.block.regmask, insn1);
8232
8233   if (dsc->u.block.regmask == 0xff)
8234     {
8235       dsc->tmp[0] = displaced_read_reg (regs, dsc, 8);
8236
8237       dsc->modinsn[0] = (insn1 & 0xfeff); /* POP {r0,r1,...,r6, r7} */
8238       dsc->modinsn[1] = 0x46b8; /* MOV r8, r7 */
8239       dsc->modinsn[2] = 0xbc80; /* POP {r7} */
8240
8241       dsc->numinsns = 3;
8242       dsc->cleanup = &cleanup_pop_pc_16bit_all;
8243     }
8244   else
8245     {
8246       unsigned int num_in_list = bitcount (dsc->u.block.regmask);
8247       unsigned int new_regmask, bit = 1;
8248       unsigned int to = 0, from = 0, i, new_rn;
8249
8250       for (i = 0; i < num_in_list + 1; i++)
8251         dsc->tmp[i] = displaced_read_reg (regs, dsc, i);
8252
8253       new_regmask = (1 << (num_in_list + 1)) - 1;
8254
8255       if (debug_displaced)
8256         fprintf_unfiltered (gdb_stdlog, _("displaced: POP "
8257                                           "{..., pc}: original reg list %.4x,"
8258                                           " modified list %.4x\n"),
8259                             (int) dsc->u.block.regmask, new_regmask);
8260
8261       dsc->u.block.regmask |= 0x8000;
8262       dsc->u.block.writeback = 0;
8263       dsc->u.block.cond = INST_AL;
8264
8265       dsc->modinsn[0] = (insn1 & ~0x1ff) | (new_regmask & 0xff);
8266
8267       dsc->cleanup = &cleanup_block_load_pc;
8268     }
8269
8270   return 0;
8271 }
8272
8273 static void
8274 thumb_process_displaced_16bit_insn (struct gdbarch *gdbarch, uint16_t insn1,
8275                                     struct regcache *regs,
8276                                     struct displaced_step_closure *dsc)
8277 {
8278   unsigned short op_bit_12_15 = bits (insn1, 12, 15);
8279   unsigned short op_bit_10_11 = bits (insn1, 10, 11);
8280   int err = 0;
8281
8282   /* 16-bit thumb instructions.  */
8283   switch (op_bit_12_15)
8284     {
8285       /* Shift (imme), add, subtract, move and compare.  */
8286     case 0: case 1: case 2: case 3:
8287       err = thumb_copy_unmodified_16bit (gdbarch, insn1,
8288                                          "shift/add/sub/mov/cmp",
8289                                          dsc);
8290       break;
8291     case 4:
8292       switch (op_bit_10_11)
8293         {
8294         case 0: /* Data-processing */
8295           err = thumb_copy_unmodified_16bit (gdbarch, insn1,
8296                                              "data-processing",
8297                                              dsc);
8298           break;
8299         case 1: /* Special data instructions and branch and exchange.  */
8300           {
8301             unsigned short op = bits (insn1, 7, 9);
8302             if (op == 6 || op == 7) /* BX or BLX */
8303               err = thumb_copy_bx_blx_reg (gdbarch, insn1, regs, dsc);
8304             else if (bits (insn1, 6, 7) != 0) /* ADD/MOV/CMP high registers.  */
8305               err = thumb_copy_alu_reg (gdbarch, insn1, regs, dsc);
8306             else
8307               err = thumb_copy_unmodified_16bit (gdbarch, insn1, "special data",
8308                                                  dsc);
8309           }
8310           break;
8311         default: /* LDR (literal) */
8312           err = thumb_copy_16bit_ldr_literal (gdbarch, insn1, regs, dsc);
8313         }
8314       break;
8315     case 5: case 6: case 7: case 8: case 9: /* Load/Store single data item */
8316       err = thumb_copy_unmodified_16bit (gdbarch, insn1, "ldr/str", dsc);
8317       break;
8318     case 10:
8319       if (op_bit_10_11 < 2) /* Generate PC-relative address */
8320         err = thumb_decode_pc_relative_16bit (gdbarch, insn1, regs, dsc);
8321       else /* Generate SP-relative address */
8322         err = thumb_copy_unmodified_16bit (gdbarch, insn1, "sp-relative", dsc);
8323       break;
8324     case 11: /* Misc 16-bit instructions */
8325       {
8326         switch (bits (insn1, 8, 11))
8327           {
8328           case 1: case 3:  case 9: case 11: /* CBNZ, CBZ */
8329             err = thumb_copy_cbnz_cbz (gdbarch, insn1, regs, dsc);
8330             break;
8331           case 12: case 13: /* POP */
8332             if (bit (insn1, 8)) /* PC is in register list.  */
8333               err = thumb_copy_pop_pc_16bit (gdbarch, insn1, regs, dsc);
8334             else
8335               err = thumb_copy_unmodified_16bit (gdbarch, insn1, "pop", dsc);
8336             break;
8337           case 15: /* If-Then, and hints */
8338             if (bits (insn1, 0, 3))
8339               /* If-Then makes up to four following instructions conditional.
8340                  IT instruction itself is not conditional, so handle it as a
8341                  common unmodified instruction.  */
8342               err = thumb_copy_unmodified_16bit (gdbarch, insn1, "If-Then",
8343                                                  dsc);
8344             else
8345               err = thumb_copy_unmodified_16bit (gdbarch, insn1, "hints", dsc);
8346             break;
8347           default:
8348             err = thumb_copy_unmodified_16bit (gdbarch, insn1, "misc", dsc);
8349           }
8350       }
8351       break;
8352     case 12:
8353       if (op_bit_10_11 < 2) /* Store multiple registers */
8354         err = thumb_copy_unmodified_16bit (gdbarch, insn1, "stm", dsc);
8355       else /* Load multiple registers */
8356         err = thumb_copy_unmodified_16bit (gdbarch, insn1, "ldm", dsc);
8357       break;
8358     case 13: /* Conditional branch and supervisor call */
8359       if (bits (insn1, 9, 11) != 7) /* conditional branch */
8360         err = thumb_copy_b (gdbarch, insn1, dsc);
8361       else
8362         err = thumb_copy_svc (gdbarch, insn1, regs, dsc);
8363       break;
8364     case 14: /* Unconditional branch */
8365       err = thumb_copy_b (gdbarch, insn1, dsc);
8366       break;
8367     default:
8368       err = 1;
8369     }
8370
8371   if (err)
8372     internal_error (__FILE__, __LINE__,
8373                     _("thumb_process_displaced_16bit_insn: Instruction decode error"));
8374 }
8375
8376 static int
8377 decode_thumb_32bit_ld_mem_hints (struct gdbarch *gdbarch,
8378                                  uint16_t insn1, uint16_t insn2,
8379                                  struct regcache *regs,
8380                                  struct displaced_step_closure *dsc)
8381 {
8382   int rt = bits (insn2, 12, 15);
8383   int rn = bits (insn1, 0, 3);
8384   int op1 = bits (insn1, 7, 8);
8385   int err = 0;
8386
8387   switch (bits (insn1, 5, 6))
8388     {
8389     case 0: /* Load byte and memory hints */
8390       if (rt == 0xf) /* PLD/PLI */
8391         {
8392           if (rn == 0xf)
8393             /* PLD literal or Encoding T3 of PLI(immediate, literal).  */
8394             return thumb2_copy_preload (gdbarch, insn1, insn2, regs, dsc);
8395           else
8396             return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8397                                                 "pli/pld", dsc);
8398         }
8399       else
8400         {
8401           if (rn == 0xf) /* LDRB/LDRSB (literal) */
8402             return thumb2_copy_load_literal (gdbarch, insn1, insn2, regs, dsc,
8403                                              1);
8404           else
8405             return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8406                                                 "ldrb{reg, immediate}/ldrbt",
8407                                                 dsc);
8408         }
8409
8410       break;
8411     case 1: /* Load halfword and memory hints.  */
8412       if (rt == 0xf) /* PLD{W} and Unalloc memory hint.  */
8413         return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8414                                             "pld/unalloc memhint", dsc);
8415       else
8416         {
8417           if (rn == 0xf)
8418             return thumb2_copy_load_literal (gdbarch, insn1, insn2, regs, dsc,
8419                                              2);
8420           else
8421             return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8422                                                 "ldrh/ldrht", dsc);
8423         }
8424       break;
8425     case 2: /* Load word */
8426       {
8427         int insn2_bit_8_11 = bits (insn2, 8, 11);
8428
8429         if (rn == 0xf)
8430           return thumb2_copy_load_literal (gdbarch, insn1, insn2, regs, dsc, 4);
8431         else if (op1 == 0x1) /* Encoding T3 */
8432           return thumb2_copy_load_reg_imm (gdbarch, insn1, insn2, regs, dsc,
8433                                            0, 1);
8434         else /* op1 == 0x0 */
8435           {
8436             if (insn2_bit_8_11 == 0xc || (insn2_bit_8_11 & 0x9) == 0x9)
8437               /* LDR (immediate) */
8438               return thumb2_copy_load_reg_imm (gdbarch, insn1, insn2, regs,
8439                                                dsc, bit (insn2, 8), 1);
8440             else if (insn2_bit_8_11 == 0xe) /* LDRT */
8441               return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8442                                                   "ldrt", dsc);
8443             else
8444               /* LDR (register) */
8445               return thumb2_copy_load_reg_imm (gdbarch, insn1, insn2, regs,
8446                                                dsc, 0, 0);
8447           }
8448         break;
8449       }
8450     default:
8451       return thumb_32bit_copy_undef (gdbarch, insn1, insn2, dsc);
8452       break;
8453     }
8454   return 0;
8455 }
8456
8457 static void
8458 thumb_process_displaced_32bit_insn (struct gdbarch *gdbarch, uint16_t insn1,
8459                                     uint16_t insn2, struct regcache *regs,
8460                                     struct displaced_step_closure *dsc)
8461 {
8462   int err = 0;
8463   unsigned short op = bit (insn2, 15);
8464   unsigned int op1 = bits (insn1, 11, 12);
8465
8466   switch (op1)
8467     {
8468     case 1:
8469       {
8470         switch (bits (insn1, 9, 10))
8471           {
8472           case 0:
8473             if (bit (insn1, 6))
8474               {
8475                 /* Load/store {dual, execlusive}, table branch.  */
8476                 if (bits (insn1, 7, 8) == 1 && bits (insn1, 4, 5) == 1
8477                     && bits (insn2, 5, 7) == 0)
8478                   err = thumb2_copy_table_branch (gdbarch, insn1, insn2, regs,
8479                                                   dsc);
8480                 else
8481                   /* PC is not allowed to use in load/store {dual, exclusive}
8482                      instructions.  */
8483                   err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8484                                                      "load/store dual/ex", dsc);
8485               }
8486             else /* load/store multiple */
8487               {
8488                 switch (bits (insn1, 7, 8))
8489                   {
8490                   case 0: case 3: /* SRS, RFE */
8491                     err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8492                                                        "srs/rfe", dsc);
8493                     break;
8494                   case 1: case 2: /* LDM/STM/PUSH/POP */
8495                     err = thumb2_copy_block_xfer (gdbarch, insn1, insn2, regs, dsc);
8496                     break;
8497                   }
8498               }
8499             break;
8500
8501           case 1:
8502             /* Data-processing (shift register).  */
8503             err = thumb2_decode_dp_shift_reg (gdbarch, insn1, insn2, regs,
8504                                               dsc);
8505             break;
8506           default: /* Coprocessor instructions.  */
8507             err = thumb2_decode_svc_copro (gdbarch, insn1, insn2, regs, dsc);
8508             break;
8509           }
8510       break;
8511       }
8512     case 2: /* op1 = 2 */
8513       if (op) /* Branch and misc control.  */
8514         {
8515           if (bit (insn2, 14)  /* BLX/BL */
8516               || bit (insn2, 12) /* Unconditional branch */
8517               || (bits (insn1, 7, 9) != 0x7)) /* Conditional branch */
8518             err = thumb2_copy_b_bl_blx (gdbarch, insn1, insn2, regs, dsc);
8519           else
8520             err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8521                                                "misc ctrl", dsc);
8522         }
8523       else
8524         {
8525           if (bit (insn1, 9)) /* Data processing (plain binary imm).  */
8526             {
8527               int op = bits (insn1, 4, 8);
8528               int rn = bits (insn1, 0, 3);
8529               if ((op == 0 || op == 0xa) && rn == 0xf)
8530                 err = thumb_copy_pc_relative_32bit (gdbarch, insn1, insn2,
8531                                                     regs, dsc);
8532               else
8533                 err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8534                                                    "dp/pb", dsc);
8535             }
8536           else /* Data processing (modified immeidate) */
8537             err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8538                                                "dp/mi", dsc);
8539         }
8540       break;
8541     case 3: /* op1 = 3 */
8542       switch (bits (insn1, 9, 10))
8543         {
8544         case 0:
8545           if (bit (insn1, 4))
8546             err = decode_thumb_32bit_ld_mem_hints (gdbarch, insn1, insn2,
8547                                                    regs, dsc);
8548           else /* NEON Load/Store and Store single data item */
8549             err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8550                                                "neon elt/struct load/store",
8551                                                dsc);
8552           break;
8553         case 1: /* op1 = 3, bits (9, 10) == 1 */
8554           switch (bits (insn1, 7, 8))
8555             {
8556             case 0: case 1: /* Data processing (register) */
8557               err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8558                                                  "dp(reg)", dsc);
8559               break;
8560             case 2: /* Multiply and absolute difference */
8561               err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8562                                                  "mul/mua/diff", dsc);
8563               break;
8564             case 3: /* Long multiply and divide */
8565               err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8566                                                  "lmul/lmua", dsc);
8567               break;
8568             }
8569           break;
8570         default: /* Coprocessor instructions */
8571           err = thumb2_decode_svc_copro (gdbarch, insn1, insn2, regs, dsc);
8572           break;
8573         }
8574       break;
8575     default:
8576       err = 1;
8577     }
8578
8579   if (err)
8580     internal_error (__FILE__, __LINE__,
8581                     _("thumb_process_displaced_32bit_insn: Instruction decode error"));
8582
8583 }
8584
8585 static void
8586 thumb_process_displaced_insn (struct gdbarch *gdbarch, CORE_ADDR from,
8587                               CORE_ADDR to, struct regcache *regs,
8588                               struct displaced_step_closure *dsc)
8589 {
8590   enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
8591   uint16_t insn1
8592     = read_memory_unsigned_integer (from, 2, byte_order_for_code);
8593
8594   if (debug_displaced)
8595     fprintf_unfiltered (gdb_stdlog, "displaced: process thumb insn %.4x "
8596                         "at %.8lx\n", insn1, (unsigned long) from);
8597
8598   dsc->is_thumb = 1;
8599   dsc->insn_size = thumb_insn_size (insn1);
8600   if (thumb_insn_size (insn1) == 4)
8601     {
8602       uint16_t insn2
8603         = read_memory_unsigned_integer (from + 2, 2, byte_order_for_code);
8604       thumb_process_displaced_32bit_insn (gdbarch, insn1, insn2, regs, dsc);
8605     }
8606   else
8607     thumb_process_displaced_16bit_insn (gdbarch, insn1, regs, dsc);
8608 }
8609
8610 void
8611 arm_process_displaced_insn (struct gdbarch *gdbarch, CORE_ADDR from,
8612                             CORE_ADDR to, struct regcache *regs,
8613                             struct displaced_step_closure *dsc)
8614 {
8615   int err = 0;
8616   enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
8617   uint32_t insn;
8618
8619   /* Most displaced instructions use a 1-instruction scratch space, so set this
8620      here and override below if/when necessary.  */
8621   dsc->numinsns = 1;
8622   dsc->insn_addr = from;
8623   dsc->scratch_base = to;
8624   dsc->cleanup = NULL;
8625   dsc->wrote_to_pc = 0;
8626
8627   if (!displaced_in_arm_mode (regs))
8628     return thumb_process_displaced_insn (gdbarch, from, to, regs, dsc);
8629
8630   dsc->is_thumb = 0;
8631   dsc->insn_size = 4;
8632   insn = read_memory_unsigned_integer (from, 4, byte_order_for_code);
8633   if (debug_displaced)
8634     fprintf_unfiltered (gdb_stdlog, "displaced: stepping insn %.8lx "
8635                         "at %.8lx\n", (unsigned long) insn,
8636                         (unsigned long) from);
8637
8638   if ((insn & 0xf0000000) == 0xf0000000)
8639     err = arm_decode_unconditional (gdbarch, insn, regs, dsc);
8640   else switch (((insn & 0x10) >> 4) | ((insn & 0xe000000) >> 24))
8641     {
8642     case 0x0: case 0x1: case 0x2: case 0x3:
8643       err = arm_decode_dp_misc (gdbarch, insn, regs, dsc);
8644       break;
8645
8646     case 0x4: case 0x5: case 0x6:
8647       err = arm_decode_ld_st_word_ubyte (gdbarch, insn, regs, dsc);
8648       break;
8649
8650     case 0x7:
8651       err = arm_decode_media (gdbarch, insn, dsc);
8652       break;
8653
8654     case 0x8: case 0x9: case 0xa: case 0xb:
8655       err = arm_decode_b_bl_ldmstm (gdbarch, insn, regs, dsc);
8656       break;
8657
8658     case 0xc: case 0xd: case 0xe: case 0xf:
8659       err = arm_decode_svc_copro (gdbarch, insn, to, regs, dsc);
8660       break;
8661     }
8662
8663   if (err)
8664     internal_error (__FILE__, __LINE__,
8665                     _("arm_process_displaced_insn: Instruction decode error"));
8666 }
8667
8668 /* Actually set up the scratch space for a displaced instruction.  */
8669
8670 void
8671 arm_displaced_init_closure (struct gdbarch *gdbarch, CORE_ADDR from,
8672                             CORE_ADDR to, struct displaced_step_closure *dsc)
8673 {
8674   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
8675   unsigned int i, len, offset;
8676   enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
8677   int size = dsc->is_thumb? 2 : 4;
8678   const gdb_byte *bkp_insn;
8679
8680   offset = 0;
8681   /* Poke modified instruction(s).  */
8682   for (i = 0; i < dsc->numinsns; i++)
8683     {
8684       if (debug_displaced)
8685         {
8686           fprintf_unfiltered (gdb_stdlog, "displaced: writing insn ");
8687           if (size == 4)
8688             fprintf_unfiltered (gdb_stdlog, "%.8lx",
8689                                 dsc->modinsn[i]);
8690           else if (size == 2)
8691             fprintf_unfiltered (gdb_stdlog, "%.4x",
8692                                 (unsigned short)dsc->modinsn[i]);
8693
8694           fprintf_unfiltered (gdb_stdlog, " at %.8lx\n",
8695                               (unsigned long) to + offset);
8696
8697         }
8698       write_memory_unsigned_integer (to + offset, size,
8699                                      byte_order_for_code,
8700                                      dsc->modinsn[i]);
8701       offset += size;
8702     }
8703
8704   /* Choose the correct breakpoint instruction.  */
8705   if (dsc->is_thumb)
8706     {
8707       bkp_insn = tdep->thumb_breakpoint;
8708       len = tdep->thumb_breakpoint_size;
8709     }
8710   else
8711     {
8712       bkp_insn = tdep->arm_breakpoint;
8713       len = tdep->arm_breakpoint_size;
8714     }
8715
8716   /* Put breakpoint afterwards.  */
8717   write_memory (to + offset, bkp_insn, len);
8718
8719   if (debug_displaced)
8720     fprintf_unfiltered (gdb_stdlog, "displaced: copy %s->%s: ",
8721                         paddress (gdbarch, from), paddress (gdbarch, to));
8722 }
8723
8724 /* Entry point for copying an instruction into scratch space for displaced
8725    stepping.  */
8726
8727 struct displaced_step_closure *
8728 arm_displaced_step_copy_insn (struct gdbarch *gdbarch,
8729                               CORE_ADDR from, CORE_ADDR to,
8730                               struct regcache *regs)
8731 {
8732   struct displaced_step_closure *dsc
8733     = xmalloc (sizeof (struct displaced_step_closure));
8734   arm_process_displaced_insn (gdbarch, from, to, regs, dsc);
8735   arm_displaced_init_closure (gdbarch, from, to, dsc);
8736
8737   return dsc;
8738 }
8739
8740 /* Entry point for cleaning things up after a displaced instruction has been
8741    single-stepped.  */
8742
8743 void
8744 arm_displaced_step_fixup (struct gdbarch *gdbarch,
8745                           struct displaced_step_closure *dsc,
8746                           CORE_ADDR from, CORE_ADDR to,
8747                           struct regcache *regs)
8748 {
8749   if (dsc->cleanup)
8750     dsc->cleanup (gdbarch, regs, dsc);
8751
8752   if (!dsc->wrote_to_pc)
8753     regcache_cooked_write_unsigned (regs, ARM_PC_REGNUM,
8754                                     dsc->insn_addr + dsc->insn_size);
8755
8756 }
8757
8758 #include "bfd-in2.h"
8759 #include "libcoff.h"
8760
8761 static int
8762 gdb_print_insn_arm (bfd_vma memaddr, disassemble_info *info)
8763 {
8764   struct gdbarch *gdbarch = info->application_data;
8765
8766   if (arm_pc_is_thumb (gdbarch, memaddr))
8767     {
8768       static asymbol *asym;
8769       static combined_entry_type ce;
8770       static struct coff_symbol_struct csym;
8771       static struct bfd fake_bfd;
8772       static bfd_target fake_target;
8773
8774       if (csym.native == NULL)
8775         {
8776           /* Create a fake symbol vector containing a Thumb symbol.
8777              This is solely so that the code in print_insn_little_arm() 
8778              and print_insn_big_arm() in opcodes/arm-dis.c will detect
8779              the presence of a Thumb symbol and switch to decoding
8780              Thumb instructions.  */
8781
8782           fake_target.flavour = bfd_target_coff_flavour;
8783           fake_bfd.xvec = &fake_target;
8784           ce.u.syment.n_sclass = C_THUMBEXTFUNC;
8785           csym.native = &ce;
8786           csym.symbol.the_bfd = &fake_bfd;
8787           csym.symbol.name = "fake";
8788           asym = (asymbol *) & csym;
8789         }
8790
8791       memaddr = UNMAKE_THUMB_ADDR (memaddr);
8792       info->symbols = &asym;
8793     }
8794   else
8795     info->symbols = NULL;
8796
8797   if (info->endian == BFD_ENDIAN_BIG)
8798     return print_insn_big_arm (memaddr, info);
8799   else
8800     return print_insn_little_arm (memaddr, info);
8801 }
8802
8803 /* The following define instruction sequences that will cause ARM
8804    cpu's to take an undefined instruction trap.  These are used to
8805    signal a breakpoint to GDB.
8806    
8807    The newer ARMv4T cpu's are capable of operating in ARM or Thumb
8808    modes.  A different instruction is required for each mode.  The ARM
8809    cpu's can also be big or little endian.  Thus four different
8810    instructions are needed to support all cases.
8811    
8812    Note: ARMv4 defines several new instructions that will take the
8813    undefined instruction trap.  ARM7TDMI is nominally ARMv4T, but does
8814    not in fact add the new instructions.  The new undefined
8815    instructions in ARMv4 are all instructions that had no defined
8816    behaviour in earlier chips.  There is no guarantee that they will
8817    raise an exception, but may be treated as NOP's.  In practice, it
8818    may only safe to rely on instructions matching:
8819    
8820    3 3 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1 
8821    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
8822    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
8823    
8824    Even this may only true if the condition predicate is true.  The
8825    following use a condition predicate of ALWAYS so it is always TRUE.
8826    
8827    There are other ways of forcing a breakpoint.  GNU/Linux, RISC iX,
8828    and NetBSD all use a software interrupt rather than an undefined
8829    instruction to force a trap.  This can be handled by by the
8830    abi-specific code during establishment of the gdbarch vector.  */
8831
8832 #define ARM_LE_BREAKPOINT {0xFE,0xDE,0xFF,0xE7}
8833 #define ARM_BE_BREAKPOINT {0xE7,0xFF,0xDE,0xFE}
8834 #define THUMB_LE_BREAKPOINT {0xbe,0xbe}
8835 #define THUMB_BE_BREAKPOINT {0xbe,0xbe}
8836
8837 static const gdb_byte arm_default_arm_le_breakpoint[] = ARM_LE_BREAKPOINT;
8838 static const gdb_byte arm_default_arm_be_breakpoint[] = ARM_BE_BREAKPOINT;
8839 static const gdb_byte arm_default_thumb_le_breakpoint[] = THUMB_LE_BREAKPOINT;
8840 static const gdb_byte arm_default_thumb_be_breakpoint[] = THUMB_BE_BREAKPOINT;
8841
8842 /* Determine the type and size of breakpoint to insert at PCPTR.  Uses
8843    the program counter value to determine whether a 16-bit or 32-bit
8844    breakpoint should be used.  It returns a pointer to a string of
8845    bytes that encode a breakpoint instruction, stores the length of
8846    the string to *lenptr, and adjusts the program counter (if
8847    necessary) to point to the actual memory location where the
8848    breakpoint should be inserted.  */
8849
8850 static const unsigned char *
8851 arm_breakpoint_from_pc (struct gdbarch *gdbarch, CORE_ADDR *pcptr, int *lenptr)
8852 {
8853   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
8854   enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
8855
8856   if (arm_pc_is_thumb (gdbarch, *pcptr))
8857     {
8858       *pcptr = UNMAKE_THUMB_ADDR (*pcptr);
8859
8860       /* If we have a separate 32-bit breakpoint instruction for Thumb-2,
8861          check whether we are replacing a 32-bit instruction.  */
8862       if (tdep->thumb2_breakpoint != NULL)
8863         {
8864           gdb_byte buf[2];
8865           if (target_read_memory (*pcptr, buf, 2) == 0)
8866             {
8867               unsigned short inst1;
8868               inst1 = extract_unsigned_integer (buf, 2, byte_order_for_code);
8869               if (thumb_insn_size (inst1) == 4)
8870                 {
8871                   *lenptr = tdep->thumb2_breakpoint_size;
8872                   return tdep->thumb2_breakpoint;
8873                 }
8874             }
8875         }
8876
8877       *lenptr = tdep->thumb_breakpoint_size;
8878       return tdep->thumb_breakpoint;
8879     }
8880   else
8881     {
8882       *lenptr = tdep->arm_breakpoint_size;
8883       return tdep->arm_breakpoint;
8884     }
8885 }
8886
8887 static void
8888 arm_remote_breakpoint_from_pc (struct gdbarch *gdbarch, CORE_ADDR *pcptr,
8889                                int *kindptr)
8890 {
8891   arm_breakpoint_from_pc (gdbarch, pcptr, kindptr);
8892
8893   if (arm_pc_is_thumb (gdbarch, *pcptr) && *kindptr == 4)
8894     /* The documented magic value for a 32-bit Thumb-2 breakpoint, so
8895        that this is not confused with a 32-bit ARM breakpoint.  */
8896     *kindptr = 3;
8897 }
8898
8899 /* Extract from an array REGBUF containing the (raw) register state a
8900    function return value of type TYPE, and copy that, in virtual
8901    format, into VALBUF.  */
8902
8903 static void
8904 arm_extract_return_value (struct type *type, struct regcache *regs,
8905                           gdb_byte *valbuf)
8906 {
8907   struct gdbarch *gdbarch = get_regcache_arch (regs);
8908   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
8909
8910   if (TYPE_CODE_FLT == TYPE_CODE (type))
8911     {
8912       switch (gdbarch_tdep (gdbarch)->fp_model)
8913         {
8914         case ARM_FLOAT_FPA:
8915           {
8916             /* The value is in register F0 in internal format.  We need to
8917                extract the raw value and then convert it to the desired
8918                internal type.  */
8919             bfd_byte tmpbuf[FP_REGISTER_SIZE];
8920
8921             regcache_cooked_read (regs, ARM_F0_REGNUM, tmpbuf);
8922             convert_from_extended (floatformat_from_type (type), tmpbuf,
8923                                    valbuf, gdbarch_byte_order (gdbarch));
8924           }
8925           break;
8926
8927         case ARM_FLOAT_SOFT_FPA:
8928         case ARM_FLOAT_SOFT_VFP:
8929           /* ARM_FLOAT_VFP can arise if this is a variadic function so
8930              not using the VFP ABI code.  */
8931         case ARM_FLOAT_VFP:
8932           regcache_cooked_read (regs, ARM_A1_REGNUM, valbuf);
8933           if (TYPE_LENGTH (type) > 4)
8934             regcache_cooked_read (regs, ARM_A1_REGNUM + 1,
8935                                   valbuf + INT_REGISTER_SIZE);
8936           break;
8937
8938         default:
8939           internal_error (__FILE__, __LINE__,
8940                           _("arm_extract_return_value: "
8941                             "Floating point model not supported"));
8942           break;
8943         }
8944     }
8945   else if (TYPE_CODE (type) == TYPE_CODE_INT
8946            || TYPE_CODE (type) == TYPE_CODE_CHAR
8947            || TYPE_CODE (type) == TYPE_CODE_BOOL
8948            || TYPE_CODE (type) == TYPE_CODE_PTR
8949            || TYPE_CODE (type) == TYPE_CODE_REF
8950            || TYPE_CODE (type) == TYPE_CODE_ENUM)
8951     {
8952       /* If the type is a plain integer, then the access is
8953          straight-forward.  Otherwise we have to play around a bit
8954          more.  */
8955       int len = TYPE_LENGTH (type);
8956       int regno = ARM_A1_REGNUM;
8957       ULONGEST tmp;
8958
8959       while (len > 0)
8960         {
8961           /* By using store_unsigned_integer we avoid having to do
8962              anything special for small big-endian values.  */
8963           regcache_cooked_read_unsigned (regs, regno++, &tmp);
8964           store_unsigned_integer (valbuf, 
8965                                   (len > INT_REGISTER_SIZE
8966                                    ? INT_REGISTER_SIZE : len),
8967                                   byte_order, tmp);
8968           len -= INT_REGISTER_SIZE;
8969           valbuf += INT_REGISTER_SIZE;
8970         }
8971     }
8972   else
8973     {
8974       /* For a structure or union the behaviour is as if the value had
8975          been stored to word-aligned memory and then loaded into 
8976          registers with 32-bit load instruction(s).  */
8977       int len = TYPE_LENGTH (type);
8978       int regno = ARM_A1_REGNUM;
8979       bfd_byte tmpbuf[INT_REGISTER_SIZE];
8980
8981       while (len > 0)
8982         {
8983           regcache_cooked_read (regs, regno++, tmpbuf);
8984           memcpy (valbuf, tmpbuf,
8985                   len > INT_REGISTER_SIZE ? INT_REGISTER_SIZE : len);
8986           len -= INT_REGISTER_SIZE;
8987           valbuf += INT_REGISTER_SIZE;
8988         }
8989     }
8990 }
8991
8992
8993 /* Will a function return an aggregate type in memory or in a
8994    register?  Return 0 if an aggregate type can be returned in a
8995    register, 1 if it must be returned in memory.  */
8996
8997 static int
8998 arm_return_in_memory (struct gdbarch *gdbarch, struct type *type)
8999 {
9000   int nRc;
9001   enum type_code code;
9002
9003   CHECK_TYPEDEF (type);
9004
9005   /* In the ARM ABI, "integer" like aggregate types are returned in
9006      registers.  For an aggregate type to be integer like, its size
9007      must be less than or equal to INT_REGISTER_SIZE and the
9008      offset of each addressable subfield must be zero.  Note that bit
9009      fields are not addressable, and all addressable subfields of
9010      unions always start at offset zero.
9011
9012      This function is based on the behaviour of GCC 2.95.1.
9013      See: gcc/arm.c: arm_return_in_memory() for details.
9014
9015      Note: All versions of GCC before GCC 2.95.2 do not set up the
9016      parameters correctly for a function returning the following
9017      structure: struct { float f;}; This should be returned in memory,
9018      not a register.  Richard Earnshaw sent me a patch, but I do not
9019      know of any way to detect if a function like the above has been
9020      compiled with the correct calling convention.  */
9021
9022   /* All aggregate types that won't fit in a register must be returned
9023      in memory.  */
9024   if (TYPE_LENGTH (type) > INT_REGISTER_SIZE)
9025     {
9026       return 1;
9027     }
9028
9029   /* The AAPCS says all aggregates not larger than a word are returned
9030      in a register.  */
9031   if (gdbarch_tdep (gdbarch)->arm_abi != ARM_ABI_APCS)
9032     return 0;
9033
9034   /* The only aggregate types that can be returned in a register are
9035      structs and unions.  Arrays must be returned in memory.  */
9036   code = TYPE_CODE (type);
9037   if ((TYPE_CODE_STRUCT != code) && (TYPE_CODE_UNION != code))
9038     {
9039       return 1;
9040     }
9041
9042   /* Assume all other aggregate types can be returned in a register.
9043      Run a check for structures, unions and arrays.  */
9044   nRc = 0;
9045
9046   if ((TYPE_CODE_STRUCT == code) || (TYPE_CODE_UNION == code))
9047     {
9048       int i;
9049       /* Need to check if this struct/union is "integer" like.  For
9050          this to be true, its size must be less than or equal to
9051          INT_REGISTER_SIZE and the offset of each addressable
9052          subfield must be zero.  Note that bit fields are not
9053          addressable, and unions always start at offset zero.  If any
9054          of the subfields is a floating point type, the struct/union
9055          cannot be an integer type.  */
9056
9057       /* For each field in the object, check:
9058          1) Is it FP? --> yes, nRc = 1;
9059          2) Is it addressable (bitpos != 0) and
9060          not packed (bitsize == 0)?
9061          --> yes, nRc = 1  
9062        */
9063
9064       for (i = 0; i < TYPE_NFIELDS (type); i++)
9065         {
9066           enum type_code field_type_code;
9067           field_type_code = TYPE_CODE (check_typedef (TYPE_FIELD_TYPE (type,
9068                                                                        i)));
9069
9070           /* Is it a floating point type field?  */
9071           if (field_type_code == TYPE_CODE_FLT)
9072             {
9073               nRc = 1;
9074               break;
9075             }
9076
9077           /* If bitpos != 0, then we have to care about it.  */
9078           if (TYPE_FIELD_BITPOS (type, i) != 0)
9079             {
9080               /* Bitfields are not addressable.  If the field bitsize is 
9081                  zero, then the field is not packed.  Hence it cannot be
9082                  a bitfield or any other packed type.  */
9083               if (TYPE_FIELD_BITSIZE (type, i) == 0)
9084                 {
9085                   nRc = 1;
9086                   break;
9087                 }
9088             }
9089         }
9090     }
9091
9092   return nRc;
9093 }
9094
9095 /* Write into appropriate registers a function return value of type
9096    TYPE, given in virtual format.  */
9097
9098 static void
9099 arm_store_return_value (struct type *type, struct regcache *regs,
9100                         const gdb_byte *valbuf)
9101 {
9102   struct gdbarch *gdbarch = get_regcache_arch (regs);
9103   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
9104
9105   if (TYPE_CODE (type) == TYPE_CODE_FLT)
9106     {
9107       gdb_byte buf[MAX_REGISTER_SIZE];
9108
9109       switch (gdbarch_tdep (gdbarch)->fp_model)
9110         {
9111         case ARM_FLOAT_FPA:
9112
9113           convert_to_extended (floatformat_from_type (type), buf, valbuf,
9114                                gdbarch_byte_order (gdbarch));
9115           regcache_cooked_write (regs, ARM_F0_REGNUM, buf);
9116           break;
9117
9118         case ARM_FLOAT_SOFT_FPA:
9119         case ARM_FLOAT_SOFT_VFP:
9120           /* ARM_FLOAT_VFP can arise if this is a variadic function so
9121              not using the VFP ABI code.  */
9122         case ARM_FLOAT_VFP:
9123           regcache_cooked_write (regs, ARM_A1_REGNUM, valbuf);
9124           if (TYPE_LENGTH (type) > 4)
9125             regcache_cooked_write (regs, ARM_A1_REGNUM + 1, 
9126                                    valbuf + INT_REGISTER_SIZE);
9127           break;
9128
9129         default:
9130           internal_error (__FILE__, __LINE__,
9131                           _("arm_store_return_value: Floating "
9132                             "point model not supported"));
9133           break;
9134         }
9135     }
9136   else if (TYPE_CODE (type) == TYPE_CODE_INT
9137            || TYPE_CODE (type) == TYPE_CODE_CHAR
9138            || TYPE_CODE (type) == TYPE_CODE_BOOL
9139            || TYPE_CODE (type) == TYPE_CODE_PTR
9140            || TYPE_CODE (type) == TYPE_CODE_REF
9141            || TYPE_CODE (type) == TYPE_CODE_ENUM)
9142     {
9143       if (TYPE_LENGTH (type) <= 4)
9144         {
9145           /* Values of one word or less are zero/sign-extended and
9146              returned in r0.  */
9147           bfd_byte tmpbuf[INT_REGISTER_SIZE];
9148           LONGEST val = unpack_long (type, valbuf);
9149
9150           store_signed_integer (tmpbuf, INT_REGISTER_SIZE, byte_order, val);
9151           regcache_cooked_write (regs, ARM_A1_REGNUM, tmpbuf);
9152         }
9153       else
9154         {
9155           /* Integral values greater than one word are stored in consecutive
9156              registers starting with r0.  This will always be a multiple of
9157              the regiser size.  */
9158           int len = TYPE_LENGTH (type);
9159           int regno = ARM_A1_REGNUM;
9160
9161           while (len > 0)
9162             {
9163               regcache_cooked_write (regs, regno++, valbuf);
9164               len -= INT_REGISTER_SIZE;
9165               valbuf += INT_REGISTER_SIZE;
9166             }
9167         }
9168     }
9169   else
9170     {
9171       /* For a structure or union the behaviour is as if the value had
9172          been stored to word-aligned memory and then loaded into 
9173          registers with 32-bit load instruction(s).  */
9174       int len = TYPE_LENGTH (type);
9175       int regno = ARM_A1_REGNUM;
9176       bfd_byte tmpbuf[INT_REGISTER_SIZE];
9177
9178       while (len > 0)
9179         {
9180           memcpy (tmpbuf, valbuf,
9181                   len > INT_REGISTER_SIZE ? INT_REGISTER_SIZE : len);
9182           regcache_cooked_write (regs, regno++, tmpbuf);
9183           len -= INT_REGISTER_SIZE;
9184           valbuf += INT_REGISTER_SIZE;
9185         }
9186     }
9187 }
9188
9189
9190 /* Handle function return values.  */
9191
9192 static enum return_value_convention
9193 arm_return_value (struct gdbarch *gdbarch, struct value *function,
9194                   struct type *valtype, struct regcache *regcache,
9195                   gdb_byte *readbuf, const gdb_byte *writebuf)
9196 {
9197   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
9198   struct type *func_type = function ? value_type (function) : NULL;
9199   enum arm_vfp_cprc_base_type vfp_base_type;
9200   int vfp_base_count;
9201
9202   if (arm_vfp_abi_for_function (gdbarch, func_type)
9203       && arm_vfp_call_candidate (valtype, &vfp_base_type, &vfp_base_count))
9204     {
9205       int reg_char = arm_vfp_cprc_reg_char (vfp_base_type);
9206       int unit_length = arm_vfp_cprc_unit_length (vfp_base_type);
9207       int i;
9208       for (i = 0; i < vfp_base_count; i++)
9209         {
9210           if (reg_char == 'q')
9211             {
9212               if (writebuf)
9213                 arm_neon_quad_write (gdbarch, regcache, i,
9214                                      writebuf + i * unit_length);
9215
9216               if (readbuf)
9217                 arm_neon_quad_read (gdbarch, regcache, i,
9218                                     readbuf + i * unit_length);
9219             }
9220           else
9221             {
9222               char name_buf[4];
9223               int regnum;
9224
9225               xsnprintf (name_buf, sizeof (name_buf), "%c%d", reg_char, i);
9226               regnum = user_reg_map_name_to_regnum (gdbarch, name_buf,
9227                                                     strlen (name_buf));
9228               if (writebuf)
9229                 regcache_cooked_write (regcache, regnum,
9230                                        writebuf + i * unit_length);
9231               if (readbuf)
9232                 regcache_cooked_read (regcache, regnum,
9233                                       readbuf + i * unit_length);
9234             }
9235         }
9236       return RETURN_VALUE_REGISTER_CONVENTION;
9237     }
9238
9239   if (TYPE_CODE (valtype) == TYPE_CODE_STRUCT
9240       || TYPE_CODE (valtype) == TYPE_CODE_UNION
9241       || TYPE_CODE (valtype) == TYPE_CODE_ARRAY)
9242     {
9243       if (tdep->struct_return == pcc_struct_return
9244           || arm_return_in_memory (gdbarch, valtype))
9245         return RETURN_VALUE_STRUCT_CONVENTION;
9246     }
9247
9248   /* AAPCS returns complex types longer than a register in memory.  */
9249   if (tdep->arm_abi != ARM_ABI_APCS
9250       && TYPE_CODE (valtype) == TYPE_CODE_COMPLEX
9251       && TYPE_LENGTH (valtype) > INT_REGISTER_SIZE)
9252     return RETURN_VALUE_STRUCT_CONVENTION;
9253
9254   if (writebuf)
9255     arm_store_return_value (valtype, regcache, writebuf);
9256
9257   if (readbuf)
9258     arm_extract_return_value (valtype, regcache, readbuf);
9259
9260   return RETURN_VALUE_REGISTER_CONVENTION;
9261 }
9262
9263
9264 static int
9265 arm_get_longjmp_target (struct frame_info *frame, CORE_ADDR *pc)
9266 {
9267   struct gdbarch *gdbarch = get_frame_arch (frame);
9268   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
9269   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
9270   CORE_ADDR jb_addr;
9271   gdb_byte buf[INT_REGISTER_SIZE];
9272   
9273   jb_addr = get_frame_register_unsigned (frame, ARM_A1_REGNUM);
9274
9275   if (target_read_memory (jb_addr + tdep->jb_pc * tdep->jb_elt_size, buf,
9276                           INT_REGISTER_SIZE))
9277     return 0;
9278
9279   *pc = extract_unsigned_integer (buf, INT_REGISTER_SIZE, byte_order);
9280   return 1;
9281 }
9282
9283 /* Recognize GCC and GNU ld's trampolines.  If we are in a trampoline,
9284    return the target PC.  Otherwise return 0.  */
9285
9286 CORE_ADDR
9287 arm_skip_stub (struct frame_info *frame, CORE_ADDR pc)
9288 {
9289   const char *name;
9290   int namelen;
9291   CORE_ADDR start_addr;
9292
9293   /* Find the starting address and name of the function containing the PC.  */
9294   if (find_pc_partial_function (pc, &name, &start_addr, NULL) == 0)
9295     {
9296       /* Trampoline 'bx reg' doesn't belong to any functions.  Do the
9297          check here.  */
9298       start_addr = arm_skip_bx_reg (frame, pc);
9299       if (start_addr != 0)
9300         return start_addr;
9301
9302       return 0;
9303     }
9304
9305   /* If PC is in a Thumb call or return stub, return the address of the
9306      target PC, which is in a register.  The thunk functions are called
9307      _call_via_xx, where x is the register name.  The possible names
9308      are r0-r9, sl, fp, ip, sp, and lr.  ARM RealView has similar
9309      functions, named __ARM_call_via_r[0-7].  */
9310   if (strncmp (name, "_call_via_", 10) == 0
9311       || strncmp (name, "__ARM_call_via_", strlen ("__ARM_call_via_")) == 0)
9312     {
9313       /* Use the name suffix to determine which register contains the
9314          target PC.  */
9315       static char *table[15] =
9316       {"r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
9317        "r8", "r9", "sl", "fp", "ip", "sp", "lr"
9318       };
9319       int regno;
9320       int offset = strlen (name) - 2;
9321
9322       for (regno = 0; regno <= 14; regno++)
9323         if (strcmp (&name[offset], table[regno]) == 0)
9324           return get_frame_register_unsigned (frame, regno);
9325     }
9326
9327   /* GNU ld generates __foo_from_arm or __foo_from_thumb for
9328      non-interworking calls to foo.  We could decode the stubs
9329      to find the target but it's easier to use the symbol table.  */
9330   namelen = strlen (name);
9331   if (name[0] == '_' && name[1] == '_'
9332       && ((namelen > 2 + strlen ("_from_thumb")
9333            && strncmp (name + namelen - strlen ("_from_thumb"), "_from_thumb",
9334                        strlen ("_from_thumb")) == 0)
9335           || (namelen > 2 + strlen ("_from_arm")
9336               && strncmp (name + namelen - strlen ("_from_arm"), "_from_arm",
9337                           strlen ("_from_arm")) == 0)))
9338     {
9339       char *target_name;
9340       int target_len = namelen - 2;
9341       struct bound_minimal_symbol minsym;
9342       struct objfile *objfile;
9343       struct obj_section *sec;
9344
9345       if (name[namelen - 1] == 'b')
9346         target_len -= strlen ("_from_thumb");
9347       else
9348         target_len -= strlen ("_from_arm");
9349
9350       target_name = alloca (target_len + 1);
9351       memcpy (target_name, name + 2, target_len);
9352       target_name[target_len] = '\0';
9353
9354       sec = find_pc_section (pc);
9355       objfile = (sec == NULL) ? NULL : sec->objfile;
9356       minsym = lookup_minimal_symbol (target_name, NULL, objfile);
9357       if (minsym.minsym != NULL)
9358         return BMSYMBOL_VALUE_ADDRESS (minsym);
9359       else
9360         return 0;
9361     }
9362
9363   return 0;                     /* not a stub */
9364 }
9365
9366 static void
9367 set_arm_command (char *args, int from_tty)
9368 {
9369   printf_unfiltered (_("\
9370 \"set arm\" must be followed by an apporpriate subcommand.\n"));
9371   help_list (setarmcmdlist, "set arm ", all_commands, gdb_stdout);
9372 }
9373
9374 static void
9375 show_arm_command (char *args, int from_tty)
9376 {
9377   cmd_show_list (showarmcmdlist, from_tty, "");
9378 }
9379
9380 static void
9381 arm_update_current_architecture (void)
9382 {
9383   struct gdbarch_info info;
9384
9385   /* If the current architecture is not ARM, we have nothing to do.  */
9386   if (gdbarch_bfd_arch_info (target_gdbarch ())->arch != bfd_arch_arm)
9387     return;
9388
9389   /* Update the architecture.  */
9390   gdbarch_info_init (&info);
9391
9392   if (!gdbarch_update_p (info))
9393     internal_error (__FILE__, __LINE__, _("could not update architecture"));
9394 }
9395
9396 static void
9397 set_fp_model_sfunc (char *args, int from_tty,
9398                     struct cmd_list_element *c)
9399 {
9400   enum arm_float_model fp_model;
9401
9402   for (fp_model = ARM_FLOAT_AUTO; fp_model != ARM_FLOAT_LAST; fp_model++)
9403     if (strcmp (current_fp_model, fp_model_strings[fp_model]) == 0)
9404       {
9405         arm_fp_model = fp_model;
9406         break;
9407       }
9408
9409   if (fp_model == ARM_FLOAT_LAST)
9410     internal_error (__FILE__, __LINE__, _("Invalid fp model accepted: %s."),
9411                     current_fp_model);
9412
9413   arm_update_current_architecture ();
9414 }
9415
9416 static void
9417 show_fp_model (struct ui_file *file, int from_tty,
9418                struct cmd_list_element *c, const char *value)
9419 {
9420   struct gdbarch_tdep *tdep = gdbarch_tdep (target_gdbarch ());
9421
9422   if (arm_fp_model == ARM_FLOAT_AUTO
9423       && gdbarch_bfd_arch_info (target_gdbarch ())->arch == bfd_arch_arm)
9424     fprintf_filtered (file, _("\
9425 The current ARM floating point model is \"auto\" (currently \"%s\").\n"),
9426                       fp_model_strings[tdep->fp_model]);
9427   else
9428     fprintf_filtered (file, _("\
9429 The current ARM floating point model is \"%s\".\n"),
9430                       fp_model_strings[arm_fp_model]);
9431 }
9432
9433 static void
9434 arm_set_abi (char *args, int from_tty,
9435              struct cmd_list_element *c)
9436 {
9437   enum arm_abi_kind arm_abi;
9438
9439   for (arm_abi = ARM_ABI_AUTO; arm_abi != ARM_ABI_LAST; arm_abi++)
9440     if (strcmp (arm_abi_string, arm_abi_strings[arm_abi]) == 0)
9441       {
9442         arm_abi_global = arm_abi;
9443         break;
9444       }
9445
9446   if (arm_abi == ARM_ABI_LAST)
9447     internal_error (__FILE__, __LINE__, _("Invalid ABI accepted: %s."),
9448                     arm_abi_string);
9449
9450   arm_update_current_architecture ();
9451 }
9452
9453 static void
9454 arm_show_abi (struct ui_file *file, int from_tty,
9455              struct cmd_list_element *c, const char *value)
9456 {
9457   struct gdbarch_tdep *tdep = gdbarch_tdep (target_gdbarch ());
9458
9459   if (arm_abi_global == ARM_ABI_AUTO
9460       && gdbarch_bfd_arch_info (target_gdbarch ())->arch == bfd_arch_arm)
9461     fprintf_filtered (file, _("\
9462 The current ARM ABI is \"auto\" (currently \"%s\").\n"),
9463                       arm_abi_strings[tdep->arm_abi]);
9464   else
9465     fprintf_filtered (file, _("The current ARM ABI is \"%s\".\n"),
9466                       arm_abi_string);
9467 }
9468
9469 static void
9470 arm_show_fallback_mode (struct ui_file *file, int from_tty,
9471                         struct cmd_list_element *c, const char *value)
9472 {
9473   fprintf_filtered (file,
9474                     _("The current execution mode assumed "
9475                       "(when symbols are unavailable) is \"%s\".\n"),
9476                     arm_fallback_mode_string);
9477 }
9478
9479 static void
9480 arm_show_force_mode (struct ui_file *file, int from_tty,
9481                      struct cmd_list_element *c, const char *value)
9482 {
9483   struct gdbarch_tdep *tdep = gdbarch_tdep (target_gdbarch ());
9484
9485   fprintf_filtered (file,
9486                     _("The current execution mode assumed "
9487                       "(even when symbols are available) is \"%s\".\n"),
9488                     arm_force_mode_string);
9489 }
9490
9491 /* If the user changes the register disassembly style used for info
9492    register and other commands, we have to also switch the style used
9493    in opcodes for disassembly output.  This function is run in the "set
9494    arm disassembly" command, and does that.  */
9495
9496 static void
9497 set_disassembly_style_sfunc (char *args, int from_tty,
9498                               struct cmd_list_element *c)
9499 {
9500   set_disassembly_style ();
9501 }
9502 \f
9503 /* Return the ARM register name corresponding to register I.  */
9504 static const char *
9505 arm_register_name (struct gdbarch *gdbarch, int i)
9506 {
9507   const int num_regs = gdbarch_num_regs (gdbarch);
9508
9509   if (gdbarch_tdep (gdbarch)->have_vfp_pseudos
9510       && i >= num_regs && i < num_regs + 32)
9511     {
9512       static const char *const vfp_pseudo_names[] = {
9513         "s0", "s1", "s2", "s3", "s4", "s5", "s6", "s7",
9514         "s8", "s9", "s10", "s11", "s12", "s13", "s14", "s15",
9515         "s16", "s17", "s18", "s19", "s20", "s21", "s22", "s23",
9516         "s24", "s25", "s26", "s27", "s28", "s29", "s30", "s31",
9517       };
9518
9519       return vfp_pseudo_names[i - num_regs];
9520     }
9521
9522   if (gdbarch_tdep (gdbarch)->have_neon_pseudos
9523       && i >= num_regs + 32 && i < num_regs + 32 + 16)
9524     {
9525       static const char *const neon_pseudo_names[] = {
9526         "q0", "q1", "q2", "q3", "q4", "q5", "q6", "q7",
9527         "q8", "q9", "q10", "q11", "q12", "q13", "q14", "q15",
9528       };
9529
9530       return neon_pseudo_names[i - num_regs - 32];
9531     }
9532
9533   if (i >= ARRAY_SIZE (arm_register_names))
9534     /* These registers are only supported on targets which supply
9535        an XML description.  */
9536     return "";
9537
9538   return arm_register_names[i];
9539 }
9540
9541 static void
9542 set_disassembly_style (void)
9543 {
9544   int current;
9545
9546   /* Find the style that the user wants.  */
9547   for (current = 0; current < num_disassembly_options; current++)
9548     if (disassembly_style == valid_disassembly_styles[current])
9549       break;
9550   gdb_assert (current < num_disassembly_options);
9551
9552   /* Synchronize the disassembler.  */
9553   set_arm_regname_option (current);
9554 }
9555
9556 /* Test whether the coff symbol specific value corresponds to a Thumb
9557    function.  */
9558
9559 static int
9560 coff_sym_is_thumb (int val)
9561 {
9562   return (val == C_THUMBEXT
9563           || val == C_THUMBSTAT
9564           || val == C_THUMBEXTFUNC
9565           || val == C_THUMBSTATFUNC
9566           || val == C_THUMBLABEL);
9567 }
9568
9569 /* arm_coff_make_msymbol_special()
9570    arm_elf_make_msymbol_special()
9571    
9572    These functions test whether the COFF or ELF symbol corresponds to
9573    an address in thumb code, and set a "special" bit in a minimal
9574    symbol to indicate that it does.  */
9575    
9576 static void
9577 arm_elf_make_msymbol_special(asymbol *sym, struct minimal_symbol *msym)
9578 {
9579   if (ARM_SYM_BRANCH_TYPE (&((elf_symbol_type *)sym)->internal_elf_sym)
9580       == ST_BRANCH_TO_THUMB)
9581     MSYMBOL_SET_SPECIAL (msym);
9582 }
9583
9584 static void
9585 arm_coff_make_msymbol_special(int val, struct minimal_symbol *msym)
9586 {
9587   if (coff_sym_is_thumb (val))
9588     MSYMBOL_SET_SPECIAL (msym);
9589 }
9590
9591 static void
9592 arm_objfile_data_free (struct objfile *objfile, void *arg)
9593 {
9594   struct arm_per_objfile *data = arg;
9595   unsigned int i;
9596
9597   for (i = 0; i < objfile->obfd->section_count; i++)
9598     VEC_free (arm_mapping_symbol_s, data->section_maps[i]);
9599 }
9600
9601 static void
9602 arm_record_special_symbol (struct gdbarch *gdbarch, struct objfile *objfile,
9603                            asymbol *sym)
9604 {
9605   const char *name = bfd_asymbol_name (sym);
9606   struct arm_per_objfile *data;
9607   VEC(arm_mapping_symbol_s) **map_p;
9608   struct arm_mapping_symbol new_map_sym;
9609
9610   gdb_assert (name[0] == '$');
9611   if (name[1] != 'a' && name[1] != 't' && name[1] != 'd')
9612     return;
9613
9614   data = objfile_data (objfile, arm_objfile_data_key);
9615   if (data == NULL)
9616     {
9617       data = OBSTACK_ZALLOC (&objfile->objfile_obstack,
9618                              struct arm_per_objfile);
9619       set_objfile_data (objfile, arm_objfile_data_key, data);
9620       data->section_maps = OBSTACK_CALLOC (&objfile->objfile_obstack,
9621                                            objfile->obfd->section_count,
9622                                            VEC(arm_mapping_symbol_s) *);
9623     }
9624   map_p = &data->section_maps[bfd_get_section (sym)->index];
9625
9626   new_map_sym.value = sym->value;
9627   new_map_sym.type = name[1];
9628
9629   /* Assume that most mapping symbols appear in order of increasing
9630      value.  If they were randomly distributed, it would be faster to
9631      always push here and then sort at first use.  */
9632   if (!VEC_empty (arm_mapping_symbol_s, *map_p))
9633     {
9634       struct arm_mapping_symbol *prev_map_sym;
9635
9636       prev_map_sym = VEC_last (arm_mapping_symbol_s, *map_p);
9637       if (prev_map_sym->value >= sym->value)
9638         {
9639           unsigned int idx;
9640           idx = VEC_lower_bound (arm_mapping_symbol_s, *map_p, &new_map_sym,
9641                                  arm_compare_mapping_symbols);
9642           VEC_safe_insert (arm_mapping_symbol_s, *map_p, idx, &new_map_sym);
9643           return;
9644         }
9645     }
9646
9647   VEC_safe_push (arm_mapping_symbol_s, *map_p, &new_map_sym);
9648 }
9649
9650 static void
9651 arm_write_pc (struct regcache *regcache, CORE_ADDR pc)
9652 {
9653   struct gdbarch *gdbarch = get_regcache_arch (regcache);
9654   regcache_cooked_write_unsigned (regcache, ARM_PC_REGNUM, pc);
9655
9656   /* If necessary, set the T bit.  */
9657   if (arm_apcs_32)
9658     {
9659       ULONGEST val, t_bit;
9660       regcache_cooked_read_unsigned (regcache, ARM_PS_REGNUM, &val);
9661       t_bit = arm_psr_thumb_bit (gdbarch);
9662       if (arm_pc_is_thumb (gdbarch, pc))
9663         regcache_cooked_write_unsigned (regcache, ARM_PS_REGNUM,
9664                                         val | t_bit);
9665       else
9666         regcache_cooked_write_unsigned (regcache, ARM_PS_REGNUM,
9667                                         val & ~t_bit);
9668     }
9669 }
9670
9671 /* Read the contents of a NEON quad register, by reading from two
9672    double registers.  This is used to implement the quad pseudo
9673    registers, and for argument passing in case the quad registers are
9674    missing; vectors are passed in quad registers when using the VFP
9675    ABI, even if a NEON unit is not present.  REGNUM is the index of
9676    the quad register, in [0, 15].  */
9677
9678 static enum register_status
9679 arm_neon_quad_read (struct gdbarch *gdbarch, struct regcache *regcache,
9680                     int regnum, gdb_byte *buf)
9681 {
9682   char name_buf[4];
9683   gdb_byte reg_buf[8];
9684   int offset, double_regnum;
9685   enum register_status status;
9686
9687   xsnprintf (name_buf, sizeof (name_buf), "d%d", regnum << 1);
9688   double_regnum = user_reg_map_name_to_regnum (gdbarch, name_buf,
9689                                                strlen (name_buf));
9690
9691   /* d0 is always the least significant half of q0.  */
9692   if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
9693     offset = 8;
9694   else
9695     offset = 0;
9696
9697   status = regcache_raw_read (regcache, double_regnum, reg_buf);
9698   if (status != REG_VALID)
9699     return status;
9700   memcpy (buf + offset, reg_buf, 8);
9701
9702   offset = 8 - offset;
9703   status = regcache_raw_read (regcache, double_regnum + 1, reg_buf);
9704   if (status != REG_VALID)
9705     return status;
9706   memcpy (buf + offset, reg_buf, 8);
9707
9708   return REG_VALID;
9709 }
9710
9711 static enum register_status
9712 arm_pseudo_read (struct gdbarch *gdbarch, struct regcache *regcache,
9713                  int regnum, gdb_byte *buf)
9714 {
9715   const int num_regs = gdbarch_num_regs (gdbarch);
9716   char name_buf[4];
9717   gdb_byte reg_buf[8];
9718   int offset, double_regnum;
9719
9720   gdb_assert (regnum >= num_regs);
9721   regnum -= num_regs;
9722
9723   if (gdbarch_tdep (gdbarch)->have_neon_pseudos && regnum >= 32 && regnum < 48)
9724     /* Quad-precision register.  */
9725     return arm_neon_quad_read (gdbarch, regcache, regnum - 32, buf);
9726   else
9727     {
9728       enum register_status status;
9729
9730       /* Single-precision register.  */
9731       gdb_assert (regnum < 32);
9732
9733       /* s0 is always the least significant half of d0.  */
9734       if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
9735         offset = (regnum & 1) ? 0 : 4;
9736       else
9737         offset = (regnum & 1) ? 4 : 0;
9738
9739       xsnprintf (name_buf, sizeof (name_buf), "d%d", regnum >> 1);
9740       double_regnum = user_reg_map_name_to_regnum (gdbarch, name_buf,
9741                                                    strlen (name_buf));
9742
9743       status = regcache_raw_read (regcache, double_regnum, reg_buf);
9744       if (status == REG_VALID)
9745         memcpy (buf, reg_buf + offset, 4);
9746       return status;
9747     }
9748 }
9749
9750 /* Store the contents of BUF to a NEON quad register, by writing to
9751    two double registers.  This is used to implement the quad pseudo
9752    registers, and for argument passing in case the quad registers are
9753    missing; vectors are passed in quad registers when using the VFP
9754    ABI, even if a NEON unit is not present.  REGNUM is the index
9755    of the quad register, in [0, 15].  */
9756
9757 static void
9758 arm_neon_quad_write (struct gdbarch *gdbarch, struct regcache *regcache,
9759                      int regnum, const gdb_byte *buf)
9760 {
9761   char name_buf[4];
9762   int offset, double_regnum;
9763
9764   xsnprintf (name_buf, sizeof (name_buf), "d%d", regnum << 1);
9765   double_regnum = user_reg_map_name_to_regnum (gdbarch, name_buf,
9766                                                strlen (name_buf));
9767
9768   /* d0 is always the least significant half of q0.  */
9769   if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
9770     offset = 8;
9771   else
9772     offset = 0;
9773
9774   regcache_raw_write (regcache, double_regnum, buf + offset);
9775   offset = 8 - offset;
9776   regcache_raw_write (regcache, double_regnum + 1, buf + offset);
9777 }
9778
9779 static void
9780 arm_pseudo_write (struct gdbarch *gdbarch, struct regcache *regcache,
9781                   int regnum, const gdb_byte *buf)
9782 {
9783   const int num_regs = gdbarch_num_regs (gdbarch);
9784   char name_buf[4];
9785   gdb_byte reg_buf[8];
9786   int offset, double_regnum;
9787
9788   gdb_assert (regnum >= num_regs);
9789   regnum -= num_regs;
9790
9791   if (gdbarch_tdep (gdbarch)->have_neon_pseudos && regnum >= 32 && regnum < 48)
9792     /* Quad-precision register.  */
9793     arm_neon_quad_write (gdbarch, regcache, regnum - 32, buf);
9794   else
9795     {
9796       /* Single-precision register.  */
9797       gdb_assert (regnum < 32);
9798
9799       /* s0 is always the least significant half of d0.  */
9800       if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
9801         offset = (regnum & 1) ? 0 : 4;
9802       else
9803         offset = (regnum & 1) ? 4 : 0;
9804
9805       xsnprintf (name_buf, sizeof (name_buf), "d%d", regnum >> 1);
9806       double_regnum = user_reg_map_name_to_regnum (gdbarch, name_buf,
9807                                                    strlen (name_buf));
9808
9809       regcache_raw_read (regcache, double_regnum, reg_buf);
9810       memcpy (reg_buf + offset, buf, 4);
9811       regcache_raw_write (regcache, double_regnum, reg_buf);
9812     }
9813 }
9814
9815 static struct value *
9816 value_of_arm_user_reg (struct frame_info *frame, const void *baton)
9817 {
9818   const int *reg_p = baton;
9819   return value_of_register (*reg_p, frame);
9820 }
9821 \f
9822 static enum gdb_osabi
9823 arm_elf_osabi_sniffer (bfd *abfd)
9824 {
9825   unsigned int elfosabi;
9826   enum gdb_osabi osabi = GDB_OSABI_UNKNOWN;
9827
9828   elfosabi = elf_elfheader (abfd)->e_ident[EI_OSABI];
9829
9830   if (elfosabi == ELFOSABI_ARM)
9831     /* GNU tools use this value.  Check note sections in this case,
9832        as well.  */
9833     bfd_map_over_sections (abfd,
9834                            generic_elf_osabi_sniff_abi_tag_sections, 
9835                            &osabi);
9836
9837   /* Anything else will be handled by the generic ELF sniffer.  */
9838   return osabi;
9839 }
9840
9841 static int
9842 arm_register_reggroup_p (struct gdbarch *gdbarch, int regnum,
9843                           struct reggroup *group)
9844 {
9845   /* FPS register's type is INT, but belongs to float_reggroup.  Beside
9846      this, FPS register belongs to save_regroup, restore_reggroup, and
9847      all_reggroup, of course.  */
9848   if (regnum == ARM_FPS_REGNUM)
9849     return (group == float_reggroup
9850             || group == save_reggroup
9851             || group == restore_reggroup
9852             || group == all_reggroup);
9853   else
9854     return default_register_reggroup_p (gdbarch, regnum, group);
9855 }
9856
9857 \f
9858 /* For backward-compatibility we allow two 'g' packet lengths with
9859    the remote protocol depending on whether FPA registers are
9860    supplied.  M-profile targets do not have FPA registers, but some
9861    stubs already exist in the wild which use a 'g' packet which
9862    supplies them albeit with dummy values.  The packet format which
9863    includes FPA registers should be considered deprecated for
9864    M-profile targets.  */
9865
9866 static void
9867 arm_register_g_packet_guesses (struct gdbarch *gdbarch)
9868 {
9869   if (gdbarch_tdep (gdbarch)->is_m)
9870     {
9871       /* If we know from the executable this is an M-profile target,
9872          cater for remote targets whose register set layout is the
9873          same as the FPA layout.  */
9874       register_remote_g_packet_guess (gdbarch,
9875                                       /* r0-r12,sp,lr,pc; f0-f7; fps,xpsr */
9876                                       (16 * INT_REGISTER_SIZE)
9877                                       + (8 * FP_REGISTER_SIZE)
9878                                       + (2 * INT_REGISTER_SIZE),
9879                                       tdesc_arm_with_m_fpa_layout);
9880
9881       /* The regular M-profile layout.  */
9882       register_remote_g_packet_guess (gdbarch,
9883                                       /* r0-r12,sp,lr,pc; xpsr */
9884                                       (16 * INT_REGISTER_SIZE)
9885                                       + INT_REGISTER_SIZE,
9886                                       tdesc_arm_with_m);
9887
9888       /* M-profile plus M4F VFP.  */
9889       register_remote_g_packet_guess (gdbarch,
9890                                       /* r0-r12,sp,lr,pc; d0-d15; fpscr,xpsr */
9891                                       (16 * INT_REGISTER_SIZE)
9892                                       + (16 * VFP_REGISTER_SIZE)
9893                                       + (2 * INT_REGISTER_SIZE),
9894                                       tdesc_arm_with_m_vfp_d16);
9895     }
9896
9897   /* Otherwise we don't have a useful guess.  */
9898 }
9899
9900 \f
9901 /* Initialize the current architecture based on INFO.  If possible,
9902    re-use an architecture from ARCHES, which is a list of
9903    architectures already created during this debugging session.
9904
9905    Called e.g. at program startup, when reading a core file, and when
9906    reading a binary file.  */
9907
9908 static struct gdbarch *
9909 arm_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
9910 {
9911   struct gdbarch_tdep *tdep;
9912   struct gdbarch *gdbarch;
9913   struct gdbarch_list *best_arch;
9914   enum arm_abi_kind arm_abi = arm_abi_global;
9915   enum arm_float_model fp_model = arm_fp_model;
9916   struct tdesc_arch_data *tdesc_data = NULL;
9917   int i, is_m = 0;
9918   int have_vfp_registers = 0, have_vfp_pseudos = 0, have_neon_pseudos = 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)
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 0:
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 1:
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 2:
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                         default:
9999                           /* Attribute value not mentioned in the
10000                              October 2008 ABI, so leave it as
10001                              "auto".  */
10002                           break;
10003                         }
10004 #else
10005                       fp_model = ARM_FLOAT_SOFT_VFP;
10006 #endif
10007                     }
10008                   break;
10009
10010                 default:
10011                   /* Leave it as "auto".  */
10012                   warning (_("unknown ARM EABI version 0x%x"), eabi_ver);
10013                   break;
10014                 }
10015
10016 #ifdef HAVE_ELF
10017               /* Detect M-profile programs.  This only works if the
10018                  executable file includes build attributes; GCC does
10019                  copy them to the executable, but e.g. RealView does
10020                  not.  */
10021               attr_arch = bfd_elf_get_obj_attr_int (info.abfd, OBJ_ATTR_PROC,
10022                                                     Tag_CPU_arch);
10023               attr_profile = bfd_elf_get_obj_attr_int (info.abfd,
10024                                                        OBJ_ATTR_PROC,
10025                                                        Tag_CPU_arch_profile);
10026               /* GCC specifies the profile for v6-M; RealView only
10027                  specifies the profile for architectures starting with
10028                  V7 (as opposed to architectures with a tag
10029                  numerically greater than TAG_CPU_ARCH_V7).  */
10030               if (!tdesc_has_registers (tdesc)
10031                   && (attr_arch == TAG_CPU_ARCH_V6_M
10032                       || attr_arch == TAG_CPU_ARCH_V6S_M
10033                       || attr_profile == 'M'))
10034                 is_m = 1;
10035 #endif
10036             }
10037
10038           if (fp_model == ARM_FLOAT_AUTO)
10039             {
10040               int e_flags = elf_elfheader (info.abfd)->e_flags;
10041
10042               switch (e_flags & (EF_ARM_SOFT_FLOAT | EF_ARM_VFP_FLOAT))
10043                 {
10044                 case 0:
10045                   /* Leave it as "auto".  Strictly speaking this case
10046                      means FPA, but almost nobody uses that now, and
10047                      many toolchains fail to set the appropriate bits
10048                      for the floating-point model they use.  */
10049                   break;
10050                 case EF_ARM_SOFT_FLOAT:
10051                   fp_model = ARM_FLOAT_SOFT_FPA;
10052                   break;
10053                 case EF_ARM_VFP_FLOAT:
10054                   fp_model = ARM_FLOAT_VFP;
10055                   break;
10056                 case EF_ARM_SOFT_FLOAT | EF_ARM_VFP_FLOAT:
10057                   fp_model = ARM_FLOAT_SOFT_VFP;
10058                   break;
10059                 }
10060             }
10061
10062           if (e_flags & EF_ARM_BE8)
10063             info.byte_order_for_code = BFD_ENDIAN_LITTLE;
10064
10065           break;
10066
10067         default:
10068           /* Leave it as "auto".  */
10069           break;
10070         }
10071     }
10072
10073   /* Check any target description for validity.  */
10074   if (tdesc_has_registers (tdesc))
10075     {
10076       /* For most registers we require GDB's default names; but also allow
10077          the numeric names for sp / lr / pc, as a convenience.  */
10078       static const char *const arm_sp_names[] = { "r13", "sp", NULL };
10079       static const char *const arm_lr_names[] = { "r14", "lr", NULL };
10080       static const char *const arm_pc_names[] = { "r15", "pc", NULL };
10081
10082       const struct tdesc_feature *feature;
10083       int valid_p;
10084
10085       feature = tdesc_find_feature (tdesc,
10086                                     "org.gnu.gdb.arm.core");
10087       if (feature == NULL)
10088         {
10089           feature = tdesc_find_feature (tdesc,
10090                                         "org.gnu.gdb.arm.m-profile");
10091           if (feature == NULL)
10092             return NULL;
10093           else
10094             is_m = 1;
10095         }
10096
10097       tdesc_data = tdesc_data_alloc ();
10098
10099       valid_p = 1;
10100       for (i = 0; i < ARM_SP_REGNUM; i++)
10101         valid_p &= tdesc_numbered_register (feature, tdesc_data, i,
10102                                             arm_register_names[i]);
10103       valid_p &= tdesc_numbered_register_choices (feature, tdesc_data,
10104                                                   ARM_SP_REGNUM,
10105                                                   arm_sp_names);
10106       valid_p &= tdesc_numbered_register_choices (feature, tdesc_data,
10107                                                   ARM_LR_REGNUM,
10108                                                   arm_lr_names);
10109       valid_p &= tdesc_numbered_register_choices (feature, tdesc_data,
10110                                                   ARM_PC_REGNUM,
10111                                                   arm_pc_names);
10112       if (is_m)
10113         valid_p &= tdesc_numbered_register (feature, tdesc_data,
10114                                             ARM_PS_REGNUM, "xpsr");
10115       else
10116         valid_p &= tdesc_numbered_register (feature, tdesc_data,
10117                                             ARM_PS_REGNUM, "cpsr");
10118
10119       if (!valid_p)
10120         {
10121           tdesc_data_cleanup (tdesc_data);
10122           return NULL;
10123         }
10124
10125       feature = tdesc_find_feature (tdesc,
10126                                     "org.gnu.gdb.arm.fpa");
10127       if (feature != NULL)
10128         {
10129           valid_p = 1;
10130           for (i = ARM_F0_REGNUM; i <= ARM_FPS_REGNUM; i++)
10131             valid_p &= tdesc_numbered_register (feature, tdesc_data, i,
10132                                                 arm_register_names[i]);
10133           if (!valid_p)
10134             {
10135               tdesc_data_cleanup (tdesc_data);
10136               return NULL;
10137             }
10138         }
10139       else
10140         have_fpa_registers = 0;
10141
10142       feature = tdesc_find_feature (tdesc,
10143                                     "org.gnu.gdb.xscale.iwmmxt");
10144       if (feature != NULL)
10145         {
10146           static const char *const iwmmxt_names[] = {
10147             "wR0", "wR1", "wR2", "wR3", "wR4", "wR5", "wR6", "wR7",
10148             "wR8", "wR9", "wR10", "wR11", "wR12", "wR13", "wR14", "wR15",
10149             "wCID", "wCon", "wCSSF", "wCASF", "", "", "", "",
10150             "wCGR0", "wCGR1", "wCGR2", "wCGR3", "", "", "", "",
10151           };
10152
10153           valid_p = 1;
10154           for (i = ARM_WR0_REGNUM; i <= ARM_WR15_REGNUM; i++)
10155             valid_p
10156               &= tdesc_numbered_register (feature, tdesc_data, i,
10157                                           iwmmxt_names[i - ARM_WR0_REGNUM]);
10158
10159           /* Check for the control registers, but do not fail if they
10160              are missing.  */
10161           for (i = ARM_WC0_REGNUM; i <= ARM_WCASF_REGNUM; i++)
10162             tdesc_numbered_register (feature, tdesc_data, i,
10163                                      iwmmxt_names[i - ARM_WR0_REGNUM]);
10164
10165           for (i = ARM_WCGR0_REGNUM; i <= ARM_WCGR3_REGNUM; i++)
10166             valid_p
10167               &= tdesc_numbered_register (feature, tdesc_data, i,
10168                                           iwmmxt_names[i - ARM_WR0_REGNUM]);
10169
10170           if (!valid_p)
10171             {
10172               tdesc_data_cleanup (tdesc_data);
10173               return NULL;
10174             }
10175         }
10176
10177       /* If we have a VFP unit, check whether the single precision registers
10178          are present.  If not, then we will synthesize them as pseudo
10179          registers.  */
10180       feature = tdesc_find_feature (tdesc,
10181                                     "org.gnu.gdb.arm.vfp");
10182       if (feature != NULL)
10183         {
10184           static const char *const vfp_double_names[] = {
10185             "d0", "d1", "d2", "d3", "d4", "d5", "d6", "d7",
10186             "d8", "d9", "d10", "d11", "d12", "d13", "d14", "d15",
10187             "d16", "d17", "d18", "d19", "d20", "d21", "d22", "d23",
10188             "d24", "d25", "d26", "d27", "d28", "d29", "d30", "d31",
10189           };
10190
10191           /* Require the double precision registers.  There must be either
10192              16 or 32.  */
10193           valid_p = 1;
10194           for (i = 0; i < 32; i++)
10195             {
10196               valid_p &= tdesc_numbered_register (feature, tdesc_data,
10197                                                   ARM_D0_REGNUM + i,
10198                                                   vfp_double_names[i]);
10199               if (!valid_p)
10200                 break;
10201             }
10202           if (!valid_p && i == 16)
10203             valid_p = 1;
10204
10205           /* Also require FPSCR.  */
10206           valid_p &= tdesc_numbered_register (feature, tdesc_data,
10207                                               ARM_FPSCR_REGNUM, "fpscr");
10208           if (!valid_p)
10209             {
10210               tdesc_data_cleanup (tdesc_data);
10211               return NULL;
10212             }
10213
10214           if (tdesc_unnumbered_register (feature, "s0") == 0)
10215             have_vfp_pseudos = 1;
10216
10217           have_vfp_registers = 1;
10218
10219           /* If we have VFP, also check for NEON.  The architecture allows
10220              NEON without VFP (integer vector operations only), but GDB
10221              does not support that.  */
10222           feature = tdesc_find_feature (tdesc,
10223                                         "org.gnu.gdb.arm.neon");
10224           if (feature != NULL)
10225             {
10226               /* NEON requires 32 double-precision registers.  */
10227               if (i != 32)
10228                 {
10229                   tdesc_data_cleanup (tdesc_data);
10230                   return NULL;
10231                 }
10232
10233               /* If there are quad registers defined by the stub, use
10234                  their type; otherwise (normally) provide them with
10235                  the default type.  */
10236               if (tdesc_unnumbered_register (feature, "q0") == 0)
10237                 have_neon_pseudos = 1;
10238
10239               have_neon = 1;
10240             }
10241         }
10242     }
10243
10244   /* If there is already a candidate, use it.  */
10245   for (best_arch = gdbarch_list_lookup_by_info (arches, &info);
10246        best_arch != NULL;
10247        best_arch = gdbarch_list_lookup_by_info (best_arch->next, &info))
10248     {
10249       if (arm_abi != ARM_ABI_AUTO
10250           && arm_abi != gdbarch_tdep (best_arch->gdbarch)->arm_abi)
10251         continue;
10252
10253       if (fp_model != ARM_FLOAT_AUTO
10254           && fp_model != gdbarch_tdep (best_arch->gdbarch)->fp_model)
10255         continue;
10256
10257       /* There are various other properties in tdep that we do not
10258          need to check here: those derived from a target description,
10259          since gdbarches with a different target description are
10260          automatically disqualified.  */
10261
10262       /* Do check is_m, though, since it might come from the binary.  */
10263       if (is_m != gdbarch_tdep (best_arch->gdbarch)->is_m)
10264         continue;
10265
10266       /* Found a match.  */
10267       break;
10268     }
10269
10270   if (best_arch != NULL)
10271     {
10272       if (tdesc_data != NULL)
10273         tdesc_data_cleanup (tdesc_data);
10274       return best_arch->gdbarch;
10275     }
10276
10277   tdep = xcalloc (1, sizeof (struct gdbarch_tdep));
10278   gdbarch = gdbarch_alloc (&info, tdep);
10279
10280   /* Record additional information about the architecture we are defining.
10281      These are gdbarch discriminators, like the OSABI.  */
10282   tdep->arm_abi = arm_abi;
10283   tdep->fp_model = fp_model;
10284   tdep->is_m = is_m;
10285   tdep->have_fpa_registers = have_fpa_registers;
10286   tdep->have_vfp_registers = have_vfp_registers;
10287   tdep->have_vfp_pseudos = have_vfp_pseudos;
10288   tdep->have_neon_pseudos = have_neon_pseudos;
10289   tdep->have_neon = have_neon;
10290
10291   arm_register_g_packet_guesses (gdbarch);
10292
10293   /* Breakpoints.  */
10294   switch (info.byte_order_for_code)
10295     {
10296     case BFD_ENDIAN_BIG:
10297       tdep->arm_breakpoint = arm_default_arm_be_breakpoint;
10298       tdep->arm_breakpoint_size = sizeof (arm_default_arm_be_breakpoint);
10299       tdep->thumb_breakpoint = arm_default_thumb_be_breakpoint;
10300       tdep->thumb_breakpoint_size = sizeof (arm_default_thumb_be_breakpoint);
10301
10302       break;
10303
10304     case BFD_ENDIAN_LITTLE:
10305       tdep->arm_breakpoint = arm_default_arm_le_breakpoint;
10306       tdep->arm_breakpoint_size = sizeof (arm_default_arm_le_breakpoint);
10307       tdep->thumb_breakpoint = arm_default_thumb_le_breakpoint;
10308       tdep->thumb_breakpoint_size = sizeof (arm_default_thumb_le_breakpoint);
10309
10310       break;
10311
10312     default:
10313       internal_error (__FILE__, __LINE__,
10314                       _("arm_gdbarch_init: bad byte order for float format"));
10315     }
10316
10317   /* On ARM targets char defaults to unsigned.  */
10318   set_gdbarch_char_signed (gdbarch, 0);
10319
10320   /* Note: for displaced stepping, this includes the breakpoint, and one word
10321      of additional scratch space.  This setting isn't used for anything beside
10322      displaced stepping at present.  */
10323   set_gdbarch_max_insn_length (gdbarch, 4 * DISPLACED_MODIFIED_INSNS);
10324
10325   /* This should be low enough for everything.  */
10326   tdep->lowest_pc = 0x20;
10327   tdep->jb_pc = -1;     /* Longjump support not enabled by default.  */
10328
10329   /* The default, for both APCS and AAPCS, is to return small
10330      structures in registers.  */
10331   tdep->struct_return = reg_struct_return;
10332
10333   set_gdbarch_push_dummy_call (gdbarch, arm_push_dummy_call);
10334   set_gdbarch_frame_align (gdbarch, arm_frame_align);
10335
10336   set_gdbarch_write_pc (gdbarch, arm_write_pc);
10337
10338   /* Frame handling.  */
10339   set_gdbarch_dummy_id (gdbarch, arm_dummy_id);
10340   set_gdbarch_unwind_pc (gdbarch, arm_unwind_pc);
10341   set_gdbarch_unwind_sp (gdbarch, arm_unwind_sp);
10342
10343   frame_base_set_default (gdbarch, &arm_normal_base);
10344
10345   /* Address manipulation.  */
10346   set_gdbarch_addr_bits_remove (gdbarch, arm_addr_bits_remove);
10347
10348   /* Advance PC across function entry code.  */
10349   set_gdbarch_skip_prologue (gdbarch, arm_skip_prologue);
10350
10351   /* Detect whether PC is in function epilogue.  */
10352   set_gdbarch_in_function_epilogue_p (gdbarch, arm_in_function_epilogue_p);
10353
10354   /* Skip trampolines.  */
10355   set_gdbarch_skip_trampoline_code (gdbarch, arm_skip_stub);
10356
10357   /* The stack grows downward.  */
10358   set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
10359
10360   /* Breakpoint manipulation.  */
10361   set_gdbarch_breakpoint_from_pc (gdbarch, arm_breakpoint_from_pc);
10362   set_gdbarch_remote_breakpoint_from_pc (gdbarch,
10363                                          arm_remote_breakpoint_from_pc);
10364
10365   /* Information about registers, etc.  */
10366   set_gdbarch_sp_regnum (gdbarch, ARM_SP_REGNUM);
10367   set_gdbarch_pc_regnum (gdbarch, ARM_PC_REGNUM);
10368   set_gdbarch_num_regs (gdbarch, ARM_NUM_REGS);
10369   set_gdbarch_register_type (gdbarch, arm_register_type);
10370   set_gdbarch_register_reggroup_p (gdbarch, arm_register_reggroup_p);
10371
10372   /* This "info float" is FPA-specific.  Use the generic version if we
10373      do not have FPA.  */
10374   if (gdbarch_tdep (gdbarch)->have_fpa_registers)
10375     set_gdbarch_print_float_info (gdbarch, arm_print_float_info);
10376
10377   /* Internal <-> external register number maps.  */
10378   set_gdbarch_dwarf2_reg_to_regnum (gdbarch, arm_dwarf_reg_to_regnum);
10379   set_gdbarch_register_sim_regno (gdbarch, arm_register_sim_regno);
10380
10381   set_gdbarch_register_name (gdbarch, arm_register_name);
10382
10383   /* Returning results.  */
10384   set_gdbarch_return_value (gdbarch, arm_return_value);
10385
10386   /* Disassembly.  */
10387   set_gdbarch_print_insn (gdbarch, gdb_print_insn_arm);
10388
10389   /* Minsymbol frobbing.  */
10390   set_gdbarch_elf_make_msymbol_special (gdbarch, arm_elf_make_msymbol_special);
10391   set_gdbarch_coff_make_msymbol_special (gdbarch,
10392                                          arm_coff_make_msymbol_special);
10393   set_gdbarch_record_special_symbol (gdbarch, arm_record_special_symbol);
10394
10395   /* Thumb-2 IT block support.  */
10396   set_gdbarch_adjust_breakpoint_address (gdbarch,
10397                                          arm_adjust_breakpoint_address);
10398
10399   /* Virtual tables.  */
10400   set_gdbarch_vbit_in_delta (gdbarch, 1);
10401
10402   /* Hook in the ABI-specific overrides, if they have been registered.  */
10403   gdbarch_init_osabi (info, gdbarch);
10404
10405   dwarf2_frame_set_init_reg (gdbarch, arm_dwarf2_frame_init_reg);
10406
10407   /* Add some default predicates.  */
10408   if (is_m)
10409     frame_unwind_append_unwinder (gdbarch, &arm_m_exception_unwind);
10410   frame_unwind_append_unwinder (gdbarch, &arm_stub_unwind);
10411   dwarf2_append_unwinders (gdbarch);
10412   frame_unwind_append_unwinder (gdbarch, &arm_exidx_unwind);
10413   frame_unwind_append_unwinder (gdbarch, &arm_prologue_unwind);
10414
10415   /* Now we have tuned the configuration, set a few final things,
10416      based on what the OS ABI has told us.  */
10417
10418   /* If the ABI is not otherwise marked, assume the old GNU APCS.  EABI
10419      binaries are always marked.  */
10420   if (tdep->arm_abi == ARM_ABI_AUTO)
10421     tdep->arm_abi = ARM_ABI_APCS;
10422
10423   /* Watchpoints are not steppable.  */
10424   set_gdbarch_have_nonsteppable_watchpoint (gdbarch, 1);
10425
10426   /* We used to default to FPA for generic ARM, but almost nobody
10427      uses that now, and we now provide a way for the user to force
10428      the model.  So default to the most useful variant.  */
10429   if (tdep->fp_model == ARM_FLOAT_AUTO)
10430     tdep->fp_model = ARM_FLOAT_SOFT_FPA;
10431
10432   if (tdep->jb_pc >= 0)
10433     set_gdbarch_get_longjmp_target (gdbarch, arm_get_longjmp_target);
10434
10435   /* Floating point sizes and format.  */
10436   set_gdbarch_float_format (gdbarch, floatformats_ieee_single);
10437   if (tdep->fp_model == ARM_FLOAT_SOFT_FPA || tdep->fp_model == ARM_FLOAT_FPA)
10438     {
10439       set_gdbarch_double_format
10440         (gdbarch, floatformats_ieee_double_littlebyte_bigword);
10441       set_gdbarch_long_double_format
10442         (gdbarch, floatformats_ieee_double_littlebyte_bigword);
10443     }
10444   else
10445     {
10446       set_gdbarch_double_format (gdbarch, floatformats_ieee_double);
10447       set_gdbarch_long_double_format (gdbarch, floatformats_ieee_double);
10448     }
10449
10450   if (have_vfp_pseudos)
10451     {
10452       /* NOTE: These are the only pseudo registers used by
10453          the ARM target at the moment.  If more are added, a
10454          little more care in numbering will be needed.  */
10455
10456       int num_pseudos = 32;
10457       if (have_neon_pseudos)
10458         num_pseudos += 16;
10459       set_gdbarch_num_pseudo_regs (gdbarch, num_pseudos);
10460       set_gdbarch_pseudo_register_read (gdbarch, arm_pseudo_read);
10461       set_gdbarch_pseudo_register_write (gdbarch, arm_pseudo_write);
10462     }
10463
10464   if (tdesc_data)
10465     {
10466       set_tdesc_pseudo_register_name (gdbarch, arm_register_name);
10467
10468       tdesc_use_registers (gdbarch, tdesc, tdesc_data);
10469
10470       /* Override tdesc_register_type to adjust the types of VFP
10471          registers for NEON.  */
10472       set_gdbarch_register_type (gdbarch, arm_register_type);
10473     }
10474
10475   /* Add standard register aliases.  We add aliases even for those
10476      nanes which are used by the current architecture - it's simpler,
10477      and does no harm, since nothing ever lists user registers.  */
10478   for (i = 0; i < ARRAY_SIZE (arm_register_aliases); i++)
10479     user_reg_add (gdbarch, arm_register_aliases[i].name,
10480                   value_of_arm_user_reg, &arm_register_aliases[i].regnum);
10481
10482   return gdbarch;
10483 }
10484
10485 static void
10486 arm_dump_tdep (struct gdbarch *gdbarch, struct ui_file *file)
10487 {
10488   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
10489
10490   if (tdep == NULL)
10491     return;
10492
10493   fprintf_unfiltered (file, _("arm_dump_tdep: Lowest pc = 0x%lx"),
10494                       (unsigned long) tdep->lowest_pc);
10495 }
10496
10497 extern initialize_file_ftype _initialize_arm_tdep; /* -Wmissing-prototypes */
10498
10499 void
10500 _initialize_arm_tdep (void)
10501 {
10502   struct ui_file *stb;
10503   long length;
10504   struct cmd_list_element *new_set, *new_show;
10505   const char *setname;
10506   const char *setdesc;
10507   const char *const *regnames;
10508   int numregs, i, j;
10509   static char *helptext;
10510   char regdesc[1024], *rdptr = regdesc;
10511   size_t rest = sizeof (regdesc);
10512
10513   gdbarch_register (bfd_arch_arm, arm_gdbarch_init, arm_dump_tdep);
10514
10515   arm_objfile_data_key
10516     = register_objfile_data_with_cleanup (NULL, arm_objfile_data_free);
10517
10518   /* Add ourselves to objfile event chain.  */
10519   observer_attach_new_objfile (arm_exidx_new_objfile);
10520   arm_exidx_data_key
10521     = register_objfile_data_with_cleanup (NULL, arm_exidx_data_free);
10522
10523   /* Register an ELF OS ABI sniffer for ARM binaries.  */
10524   gdbarch_register_osabi_sniffer (bfd_arch_arm,
10525                                   bfd_target_elf_flavour,
10526                                   arm_elf_osabi_sniffer);
10527
10528   /* Initialize the standard target descriptions.  */
10529   initialize_tdesc_arm_with_m ();
10530   initialize_tdesc_arm_with_m_fpa_layout ();
10531   initialize_tdesc_arm_with_m_vfp_d16 ();
10532   initialize_tdesc_arm_with_iwmmxt ();
10533   initialize_tdesc_arm_with_vfpv2 ();
10534   initialize_tdesc_arm_with_vfpv3 ();
10535   initialize_tdesc_arm_with_neon ();
10536
10537   /* Get the number of possible sets of register names defined in opcodes.  */
10538   num_disassembly_options = get_arm_regname_num_options ();
10539
10540   /* Add root prefix command for all "set arm"/"show arm" commands.  */
10541   add_prefix_cmd ("arm", no_class, set_arm_command,
10542                   _("Various ARM-specific commands."),
10543                   &setarmcmdlist, "set arm ", 0, &setlist);
10544
10545   add_prefix_cmd ("arm", no_class, show_arm_command,
10546                   _("Various ARM-specific commands."),
10547                   &showarmcmdlist, "show arm ", 0, &showlist);
10548
10549   /* Sync the opcode insn printer with our register viewer.  */
10550   parse_arm_disassembler_option ("reg-names-std");
10551
10552   /* Initialize the array that will be passed to
10553      add_setshow_enum_cmd().  */
10554   valid_disassembly_styles
10555     = xmalloc ((num_disassembly_options + 1) * sizeof (char *));
10556   for (i = 0; i < num_disassembly_options; i++)
10557     {
10558       numregs = get_arm_regnames (i, &setname, &setdesc, &regnames);
10559       valid_disassembly_styles[i] = setname;
10560       length = snprintf (rdptr, rest, "%s - %s\n", setname, setdesc);
10561       rdptr += length;
10562       rest -= length;
10563       /* When we find the default names, tell the disassembler to use
10564          them.  */
10565       if (!strcmp (setname, "std"))
10566         {
10567           disassembly_style = setname;
10568           set_arm_regname_option (i);
10569         }
10570     }
10571   /* Mark the end of valid options.  */
10572   valid_disassembly_styles[num_disassembly_options] = NULL;
10573
10574   /* Create the help text.  */
10575   stb = mem_fileopen ();
10576   fprintf_unfiltered (stb, "%s%s%s",
10577                       _("The valid values are:\n"),
10578                       regdesc,
10579                       _("The default is \"std\"."));
10580   helptext = ui_file_xstrdup (stb, NULL);
10581   ui_file_delete (stb);
10582
10583   add_setshow_enum_cmd("disassembler", no_class,
10584                        valid_disassembly_styles, &disassembly_style,
10585                        _("Set the disassembly style."),
10586                        _("Show the disassembly style."),
10587                        helptext,
10588                        set_disassembly_style_sfunc,
10589                        NULL, /* FIXME: i18n: The disassembly style is
10590                                 \"%s\".  */
10591                        &setarmcmdlist, &showarmcmdlist);
10592
10593   add_setshow_boolean_cmd ("apcs32", no_class, &arm_apcs_32,
10594                            _("Set usage of ARM 32-bit mode."),
10595                            _("Show usage of ARM 32-bit mode."),
10596                            _("When off, a 26-bit PC will be used."),
10597                            NULL,
10598                            NULL, /* FIXME: i18n: Usage of ARM 32-bit
10599                                     mode is %s.  */
10600                            &setarmcmdlist, &showarmcmdlist);
10601
10602   /* Add a command to allow the user to force the FPU model.  */
10603   add_setshow_enum_cmd ("fpu", no_class, fp_model_strings, &current_fp_model,
10604                         _("Set the floating point type."),
10605                         _("Show the floating point type."),
10606                         _("auto - Determine the FP typefrom the OS-ABI.\n\
10607 softfpa - Software FP, mixed-endian doubles on little-endian ARMs.\n\
10608 fpa - FPA co-processor (GCC compiled).\n\
10609 softvfp - Software FP with pure-endian doubles.\n\
10610 vfp - VFP co-processor."),
10611                         set_fp_model_sfunc, show_fp_model,
10612                         &setarmcmdlist, &showarmcmdlist);
10613
10614   /* Add a command to allow the user to force the ABI.  */
10615   add_setshow_enum_cmd ("abi", class_support, arm_abi_strings, &arm_abi_string,
10616                         _("Set the ABI."),
10617                         _("Show the ABI."),
10618                         NULL, arm_set_abi, arm_show_abi,
10619                         &setarmcmdlist, &showarmcmdlist);
10620
10621   /* Add two commands to allow the user to force the assumed
10622      execution mode.  */
10623   add_setshow_enum_cmd ("fallback-mode", class_support,
10624                         arm_mode_strings, &arm_fallback_mode_string,
10625                         _("Set the mode assumed when symbols are unavailable."),
10626                         _("Show the mode assumed when symbols are unavailable."),
10627                         NULL, NULL, arm_show_fallback_mode,
10628                         &setarmcmdlist, &showarmcmdlist);
10629   add_setshow_enum_cmd ("force-mode", class_support,
10630                         arm_mode_strings, &arm_force_mode_string,
10631                         _("Set the mode assumed even when symbols are available."),
10632                         _("Show the mode assumed even when symbols are available."),
10633                         NULL, NULL, arm_show_force_mode,
10634                         &setarmcmdlist, &showarmcmdlist);
10635
10636   /* Debugging flag.  */
10637   add_setshow_boolean_cmd ("arm", class_maintenance, &arm_debug,
10638                            _("Set ARM debugging."),
10639                            _("Show ARM debugging."),
10640                            _("When on, arm-specific debugging is enabled."),
10641                            NULL,
10642                            NULL, /* FIXME: i18n: "ARM debugging is %s.  */
10643                            &setdebuglist, &showdebuglist);
10644 }
10645
10646 /* ARM-reversible process record data structures.  */
10647
10648 #define ARM_INSN_SIZE_BYTES 4    
10649 #define THUMB_INSN_SIZE_BYTES 2
10650 #define THUMB2_INSN_SIZE_BYTES 4
10651
10652
10653 #define INSN_S_L_BIT_NUM 20
10654
10655 #define REG_ALLOC(REGS, LENGTH, RECORD_BUF) \
10656         do  \
10657           { \
10658             unsigned int reg_len = LENGTH; \
10659             if (reg_len) \
10660               { \
10661                 REGS = XNEWVEC (uint32_t, reg_len); \
10662                 memcpy(&REGS[0], &RECORD_BUF[0], sizeof(uint32_t)*LENGTH); \
10663               } \
10664           } \
10665         while (0)
10666
10667 #define MEM_ALLOC(MEMS, LENGTH, RECORD_BUF) \
10668         do  \
10669           { \
10670             unsigned int mem_len = LENGTH; \
10671             if (mem_len) \
10672             { \
10673               MEMS =  XNEWVEC (struct arm_mem_r, mem_len);  \
10674               memcpy(&MEMS->len, &RECORD_BUF[0], \
10675                      sizeof(struct arm_mem_r) * LENGTH); \
10676             } \
10677           } \
10678           while (0)
10679
10680 /* Checks whether insn is already recorded or yet to be decoded. (boolean expression).  */
10681 #define INSN_RECORDED(ARM_RECORD) \
10682         (0 != (ARM_RECORD)->reg_rec_count || 0 != (ARM_RECORD)->mem_rec_count)
10683
10684 /* ARM memory record structure.  */
10685 struct arm_mem_r
10686 {
10687   uint32_t len;    /* Record length.  */
10688   uint32_t addr;   /* Memory address.  */
10689 };
10690
10691 /* ARM instruction record contains opcode of current insn
10692    and execution state (before entry to decode_insn()),
10693    contains list of to-be-modified registers and
10694    memory blocks (on return from decode_insn()).  */
10695
10696 typedef struct insn_decode_record_t
10697 {
10698   struct gdbarch *gdbarch;
10699   struct regcache *regcache;
10700   CORE_ADDR this_addr;          /* Address of the insn being decoded.  */
10701   uint32_t arm_insn;            /* Should accommodate thumb.  */
10702   uint32_t cond;                /* Condition code.  */
10703   uint32_t opcode;              /* Insn opcode.  */
10704   uint32_t decode;              /* Insn decode bits.  */
10705   uint32_t mem_rec_count;       /* No of mem records.  */
10706   uint32_t reg_rec_count;       /* No of reg records.  */
10707   uint32_t *arm_regs;           /* Registers to be saved for this record.  */
10708   struct arm_mem_r *arm_mems;   /* Memory to be saved for this record.  */
10709 } insn_decode_record;
10710
10711
10712 /* Checks ARM SBZ and SBO mandatory fields.  */
10713
10714 static int
10715 sbo_sbz (uint32_t insn, uint32_t bit_num, uint32_t len, uint32_t sbo)
10716 {
10717   uint32_t ones = bits (insn, bit_num - 1, (bit_num -1) + (len - 1));
10718
10719   if (!len)
10720     return 1;
10721
10722   if (!sbo)
10723     ones = ~ones;
10724
10725   while (ones)
10726     {
10727       if (!(ones & sbo))
10728         {
10729           return 0;
10730         }
10731       ones = ones >> 1;
10732     }
10733   return 1;
10734 }
10735
10736 enum arm_record_result
10737 {
10738   ARM_RECORD_SUCCESS = 0,
10739   ARM_RECORD_FAILURE = 1
10740 };
10741
10742 typedef enum
10743 {
10744   ARM_RECORD_STRH=1,
10745   ARM_RECORD_STRD
10746 } arm_record_strx_t;
10747
10748 typedef enum
10749 {
10750   ARM_RECORD=1,
10751   THUMB_RECORD,
10752   THUMB2_RECORD
10753 } record_type_t;
10754
10755
10756 static int
10757 arm_record_strx (insn_decode_record *arm_insn_r, uint32_t *record_buf, 
10758                  uint32_t *record_buf_mem, arm_record_strx_t str_type)
10759 {
10760
10761   struct regcache *reg_cache = arm_insn_r->regcache;
10762   ULONGEST u_regval[2]= {0};
10763
10764   uint32_t reg_src1 = 0, reg_src2 = 0;
10765   uint32_t immed_high = 0, immed_low = 0,offset_8 = 0, tgt_mem_addr = 0;
10766   uint32_t opcode1 = 0;
10767
10768   arm_insn_r->opcode = bits (arm_insn_r->arm_insn, 21, 24);
10769   arm_insn_r->decode = bits (arm_insn_r->arm_insn, 4, 7);
10770   opcode1 = bits (arm_insn_r->arm_insn, 20, 24);
10771
10772
10773   if (14 == arm_insn_r->opcode || 10 == arm_insn_r->opcode)
10774     {
10775       /* 1) Handle misc store, immediate offset.  */
10776       immed_low = bits (arm_insn_r->arm_insn, 0, 3);
10777       immed_high = bits (arm_insn_r->arm_insn, 8, 11);
10778       reg_src1 = bits (arm_insn_r->arm_insn, 16, 19);
10779       regcache_raw_read_unsigned (reg_cache, reg_src1,
10780                                   &u_regval[0]);
10781       if (ARM_PC_REGNUM == reg_src1)
10782         {
10783           /* If R15 was used as Rn, hence current PC+8.  */
10784           u_regval[0] = u_regval[0] + 8;
10785         }
10786       offset_8 = (immed_high << 4) | immed_low;
10787       /* Calculate target store address.  */
10788       if (14 == arm_insn_r->opcode)
10789         {
10790           tgt_mem_addr = u_regval[0] + offset_8;
10791         }
10792       else
10793         {
10794           tgt_mem_addr = u_regval[0] - offset_8;
10795         }
10796       if (ARM_RECORD_STRH == str_type)
10797         {
10798           record_buf_mem[0] = 2;
10799           record_buf_mem[1] = tgt_mem_addr;
10800           arm_insn_r->mem_rec_count = 1;
10801         }
10802       else if (ARM_RECORD_STRD == str_type)
10803         {
10804           record_buf_mem[0] = 4;
10805           record_buf_mem[1] = tgt_mem_addr;
10806           record_buf_mem[2] = 4;
10807           record_buf_mem[3] = tgt_mem_addr + 4;
10808           arm_insn_r->mem_rec_count = 2;
10809         }
10810     }
10811   else if (12 == arm_insn_r->opcode || 8 == arm_insn_r->opcode)
10812     {
10813       /* 2) Store, register offset.  */
10814       /* Get Rm.  */
10815       reg_src1 = bits (arm_insn_r->arm_insn, 0, 3);
10816       /* Get Rn.  */
10817       reg_src2 = bits (arm_insn_r->arm_insn, 16, 19);
10818       regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval[0]);
10819       regcache_raw_read_unsigned (reg_cache, reg_src2, &u_regval[1]);
10820       if (15 == reg_src2)
10821         {
10822           /* If R15 was used as Rn, hence current PC+8.  */
10823           u_regval[0] = u_regval[0] + 8;
10824         }
10825       /* Calculate target store address, Rn +/- Rm, register offset.  */
10826       if (12 == arm_insn_r->opcode)
10827         {
10828           tgt_mem_addr = u_regval[0] + u_regval[1];
10829         }
10830       else
10831         {
10832           tgt_mem_addr = u_regval[1] - u_regval[0];
10833         }
10834       if (ARM_RECORD_STRH == str_type)
10835         {
10836           record_buf_mem[0] = 2;
10837           record_buf_mem[1] = tgt_mem_addr;
10838           arm_insn_r->mem_rec_count = 1;
10839         }
10840       else if (ARM_RECORD_STRD == str_type)
10841         {
10842           record_buf_mem[0] = 4;
10843           record_buf_mem[1] = tgt_mem_addr;
10844           record_buf_mem[2] = 4;
10845           record_buf_mem[3] = tgt_mem_addr + 4;
10846           arm_insn_r->mem_rec_count = 2;
10847         }
10848     }
10849   else if (11 == arm_insn_r->opcode || 15 == arm_insn_r->opcode
10850            || 2 == arm_insn_r->opcode  || 6 == arm_insn_r->opcode)
10851     {
10852       /* 3) Store, immediate pre-indexed.  */
10853       /* 5) Store, immediate post-indexed.  */
10854       immed_low = bits (arm_insn_r->arm_insn, 0, 3);
10855       immed_high = bits (arm_insn_r->arm_insn, 8, 11);
10856       offset_8 = (immed_high << 4) | immed_low;
10857       reg_src1 = bits (arm_insn_r->arm_insn, 16, 19);
10858       regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval[0]);
10859       /* Calculate target store address, Rn +/- Rm, register offset.  */
10860       if (15 == arm_insn_r->opcode || 6 == arm_insn_r->opcode)
10861         {
10862           tgt_mem_addr = u_regval[0] + offset_8;
10863         }
10864       else
10865         {
10866           tgt_mem_addr = u_regval[0] - offset_8;
10867         }
10868       if (ARM_RECORD_STRH == str_type)
10869         {
10870           record_buf_mem[0] = 2;
10871           record_buf_mem[1] = tgt_mem_addr;
10872           arm_insn_r->mem_rec_count = 1;
10873         }
10874       else if (ARM_RECORD_STRD == str_type)
10875         {
10876           record_buf_mem[0] = 4;
10877           record_buf_mem[1] = tgt_mem_addr;
10878           record_buf_mem[2] = 4;
10879           record_buf_mem[3] = tgt_mem_addr + 4;
10880           arm_insn_r->mem_rec_count = 2;
10881         }
10882       /* Record Rn also as it changes.  */
10883       *(record_buf) = bits (arm_insn_r->arm_insn, 16, 19);
10884       arm_insn_r->reg_rec_count = 1;
10885     }
10886   else if (9 == arm_insn_r->opcode || 13 == arm_insn_r->opcode
10887            || 0 == arm_insn_r->opcode || 4 == arm_insn_r->opcode)
10888     {
10889       /* 4) Store, register pre-indexed.  */
10890       /* 6) Store, register post -indexed.  */
10891       reg_src1 = bits (arm_insn_r->arm_insn, 0, 3);
10892       reg_src2 = bits (arm_insn_r->arm_insn, 16, 19);
10893       regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval[0]);
10894       regcache_raw_read_unsigned (reg_cache, reg_src2, &u_regval[1]);
10895       /* Calculate target store address, Rn +/- Rm, register offset.  */
10896       if (13 == arm_insn_r->opcode || 4 == arm_insn_r->opcode)
10897         {
10898           tgt_mem_addr = u_regval[0] + u_regval[1];
10899         }
10900       else
10901         {
10902           tgt_mem_addr = u_regval[1] - u_regval[0];
10903         }
10904       if (ARM_RECORD_STRH == str_type)
10905         {
10906           record_buf_mem[0] = 2;
10907           record_buf_mem[1] = tgt_mem_addr;
10908           arm_insn_r->mem_rec_count = 1;
10909         }
10910       else if (ARM_RECORD_STRD == str_type)
10911         {
10912           record_buf_mem[0] = 4;
10913           record_buf_mem[1] = tgt_mem_addr;
10914           record_buf_mem[2] = 4;
10915           record_buf_mem[3] = tgt_mem_addr + 4;
10916           arm_insn_r->mem_rec_count = 2;
10917         }
10918       /* Record Rn also as it changes.  */
10919       *(record_buf) = bits (arm_insn_r->arm_insn, 16, 19);
10920       arm_insn_r->reg_rec_count = 1;
10921     }
10922   return 0;
10923 }
10924
10925 /* Handling ARM extension space insns.  */
10926
10927 static int
10928 arm_record_extension_space (insn_decode_record *arm_insn_r)
10929 {
10930   uint32_t ret = 0;  /* Return value: -1:record failure ;  0:success  */
10931   uint32_t opcode1 = 0, opcode2 = 0, insn_op1 = 0;
10932   uint32_t record_buf[8], record_buf_mem[8];
10933   uint32_t reg_src1 = 0;
10934   uint32_t immed_high = 0, immed_low = 0,offset_8 = 0, tgt_mem_addr = 0;
10935   struct regcache *reg_cache = arm_insn_r->regcache;
10936   ULONGEST u_regval = 0;
10937
10938   gdb_assert (!INSN_RECORDED(arm_insn_r));
10939   /* Handle unconditional insn extension space.  */
10940
10941   opcode1 = bits (arm_insn_r->arm_insn, 20, 27);
10942   opcode2 = bits (arm_insn_r->arm_insn, 4, 7);
10943   if (arm_insn_r->cond)
10944     {
10945       /* PLD has no affect on architectural state, it just affects
10946          the caches.  */
10947       if (5 == ((opcode1 & 0xE0) >> 5))
10948         {
10949           /* BLX(1) */
10950           record_buf[0] = ARM_PS_REGNUM;
10951           record_buf[1] = ARM_LR_REGNUM;
10952           arm_insn_r->reg_rec_count = 2;
10953         }
10954       /* STC2, LDC2, MCR2, MRC2, CDP2: <TBD>, co-processor insn.  */
10955     }
10956
10957
10958   opcode1 = bits (arm_insn_r->arm_insn, 25, 27);
10959   if (3 == opcode1 && bit (arm_insn_r->arm_insn, 4))
10960     {
10961       ret = -1;
10962       /* Undefined instruction on ARM V5; need to handle if later 
10963          versions define it.  */
10964     }
10965
10966   opcode1 = bits (arm_insn_r->arm_insn, 24, 27);
10967   opcode2 = bits (arm_insn_r->arm_insn, 4, 7);
10968   insn_op1 = bits (arm_insn_r->arm_insn, 20, 23);
10969
10970   /* Handle arithmetic insn extension space.  */
10971   if (!opcode1 && 9 == opcode2 && 1 != arm_insn_r->cond
10972       && !INSN_RECORDED(arm_insn_r))
10973     {
10974       /* Handle MLA(S) and MUL(S).  */
10975       if (0 <= insn_op1 && 3 >= insn_op1)
10976       {
10977         record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
10978         record_buf[1] = ARM_PS_REGNUM;
10979         arm_insn_r->reg_rec_count = 2;
10980       }
10981       else if (4 <= insn_op1 && 15 >= insn_op1)
10982       {
10983         /* Handle SMLAL(S), SMULL(S), UMLAL(S), UMULL(S).  */
10984         record_buf[0] = bits (arm_insn_r->arm_insn, 16, 19);
10985         record_buf[1] = bits (arm_insn_r->arm_insn, 12, 15);
10986         record_buf[2] = ARM_PS_REGNUM;
10987         arm_insn_r->reg_rec_count = 3;
10988       }
10989     }
10990
10991   opcode1 = bits (arm_insn_r->arm_insn, 26, 27);
10992   opcode2 = bits (arm_insn_r->arm_insn, 23, 24);
10993   insn_op1 = bits (arm_insn_r->arm_insn, 21, 22);
10994
10995   /* Handle control insn extension space.  */
10996
10997   if (!opcode1 && 2 == opcode2 && !bit (arm_insn_r->arm_insn, 20)
10998       && 1 != arm_insn_r->cond && !INSN_RECORDED(arm_insn_r))
10999     {
11000       if (!bit (arm_insn_r->arm_insn,25))
11001         {
11002           if (!bits (arm_insn_r->arm_insn, 4, 7))
11003             {
11004               if ((0 == insn_op1) || (2 == insn_op1))
11005                 {
11006                   /* MRS.  */
11007                   record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11008                   arm_insn_r->reg_rec_count = 1;
11009                 }
11010               else if (1 == insn_op1)
11011                 {
11012                   /* CSPR is going to be changed.  */
11013                   record_buf[0] = ARM_PS_REGNUM;
11014                   arm_insn_r->reg_rec_count = 1;
11015                 }
11016               else if (3 == insn_op1)
11017                 {
11018                   /* SPSR is going to be changed.  */
11019                   /* We need to get SPSR value, which is yet to be done.  */
11020                   printf_unfiltered (_("Process record does not support "
11021                                      "instruction  0x%0x at address %s.\n"),
11022                                      arm_insn_r->arm_insn,
11023                                      paddress (arm_insn_r->gdbarch, 
11024                                      arm_insn_r->this_addr));
11025                   return -1;
11026                 }
11027             }
11028           else if (1 == bits (arm_insn_r->arm_insn, 4, 7))
11029             {
11030               if (1 == insn_op1)
11031                 {
11032                   /* BX.  */
11033                   record_buf[0] = ARM_PS_REGNUM;
11034                   arm_insn_r->reg_rec_count = 1;
11035                 }
11036               else if (3 == insn_op1)
11037                 {
11038                   /* CLZ.  */
11039                   record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11040                   arm_insn_r->reg_rec_count = 1;
11041                 }
11042             }
11043           else if (3 == bits (arm_insn_r->arm_insn, 4, 7))
11044             {
11045               /* BLX.  */
11046               record_buf[0] = ARM_PS_REGNUM;
11047               record_buf[1] = ARM_LR_REGNUM;
11048               arm_insn_r->reg_rec_count = 2;
11049             }
11050           else if (5 == bits (arm_insn_r->arm_insn, 4, 7))
11051             {
11052               /* QADD, QSUB, QDADD, QDSUB */
11053               record_buf[0] = ARM_PS_REGNUM;
11054               record_buf[1] = bits (arm_insn_r->arm_insn, 12, 15);
11055               arm_insn_r->reg_rec_count = 2;
11056             }
11057           else if (7 == bits (arm_insn_r->arm_insn, 4, 7))
11058             {
11059               /* BKPT.  */
11060               record_buf[0] = ARM_PS_REGNUM;
11061               record_buf[1] = ARM_LR_REGNUM;
11062               arm_insn_r->reg_rec_count = 2;
11063
11064               /* Save SPSR also;how?  */
11065               printf_unfiltered (_("Process record does not support "
11066                                   "instruction 0x%0x at address %s.\n"),
11067                                   arm_insn_r->arm_insn,
11068                   paddress (arm_insn_r->gdbarch, arm_insn_r->this_addr));
11069               return -1;
11070             }
11071           else if(8 == bits (arm_insn_r->arm_insn, 4, 7) 
11072                   || 10 == bits (arm_insn_r->arm_insn, 4, 7)
11073                   || 12 == bits (arm_insn_r->arm_insn, 4, 7)
11074                   || 14 == bits (arm_insn_r->arm_insn, 4, 7)
11075                  )
11076             {
11077               if (0 == insn_op1 || 1 == insn_op1)
11078                 {
11079                   /* SMLA<x><y>, SMLAW<y>, SMULW<y>.  */
11080                   /* We dont do optimization for SMULW<y> where we
11081                      need only Rd.  */
11082                   record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11083                   record_buf[1] = ARM_PS_REGNUM;
11084                   arm_insn_r->reg_rec_count = 2;
11085                 }
11086               else if (2 == insn_op1)
11087                 {
11088                   /* SMLAL<x><y>.  */
11089                   record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11090                   record_buf[1] = bits (arm_insn_r->arm_insn, 16, 19);
11091                   arm_insn_r->reg_rec_count = 2;
11092                 }
11093               else if (3 == insn_op1)
11094                 {
11095                   /* SMUL<x><y>.  */
11096                   record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11097                   arm_insn_r->reg_rec_count = 1;
11098                 }
11099             }
11100         }
11101       else
11102         {
11103           /* MSR : immediate form.  */
11104           if (1 == insn_op1)
11105             {
11106               /* CSPR is going to be changed.  */
11107               record_buf[0] = ARM_PS_REGNUM;
11108               arm_insn_r->reg_rec_count = 1;
11109             }
11110           else if (3 == insn_op1)
11111             {
11112               /* SPSR is going to be changed.  */
11113               /* we need to get SPSR value, which is yet to be done  */
11114               printf_unfiltered (_("Process record does not support "
11115                                    "instruction 0x%0x at address %s.\n"),
11116                                     arm_insn_r->arm_insn,
11117                                     paddress (arm_insn_r->gdbarch, 
11118                                     arm_insn_r->this_addr));
11119               return -1;
11120             }
11121         }
11122     }
11123
11124   opcode1 = bits (arm_insn_r->arm_insn, 25, 27);
11125   opcode2 = bits (arm_insn_r->arm_insn, 20, 24);
11126   insn_op1 = bits (arm_insn_r->arm_insn, 5, 6);
11127
11128   /* Handle load/store insn extension space.  */
11129
11130   if (!opcode1 && bit (arm_insn_r->arm_insn, 7) 
11131       && bit (arm_insn_r->arm_insn, 4) && 1 != arm_insn_r->cond
11132       && !INSN_RECORDED(arm_insn_r))
11133     {
11134       /* SWP/SWPB.  */
11135       if (0 == insn_op1)
11136         {
11137           /* These insn, changes register and memory as well.  */
11138           /* SWP or SWPB insn.  */
11139           /* Get memory address given by Rn.  */
11140           reg_src1 = bits (arm_insn_r->arm_insn, 16, 19);
11141           regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval);
11142           /* SWP insn ?, swaps word.  */
11143           if (8 == arm_insn_r->opcode)
11144             {
11145               record_buf_mem[0] = 4;
11146             }
11147           else
11148             {
11149               /* SWPB insn, swaps only byte.  */
11150               record_buf_mem[0] = 1;
11151             }
11152           record_buf_mem[1] = u_regval;
11153           arm_insn_r->mem_rec_count = 1;
11154           record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11155           arm_insn_r->reg_rec_count = 1;
11156         }
11157       else if (1 == insn_op1 && !bit (arm_insn_r->arm_insn, 20))
11158         {
11159           /* STRH.  */
11160           arm_record_strx(arm_insn_r, &record_buf[0], &record_buf_mem[0],
11161                           ARM_RECORD_STRH);
11162         }
11163       else if (2 == insn_op1 && !bit (arm_insn_r->arm_insn, 20))
11164         {
11165           /* LDRD.  */
11166           record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11167           record_buf[1] = record_buf[0] + 1;
11168           arm_insn_r->reg_rec_count = 2;
11169         }
11170       else if (3 == insn_op1 && !bit (arm_insn_r->arm_insn, 20))
11171         {
11172           /* STRD.  */
11173           arm_record_strx(arm_insn_r, &record_buf[0], &record_buf_mem[0],
11174                         ARM_RECORD_STRD);
11175         }
11176       else if (bit (arm_insn_r->arm_insn, 20) && insn_op1 <= 3)
11177         {
11178           /* LDRH, LDRSB, LDRSH.  */
11179           record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11180           arm_insn_r->reg_rec_count = 1;
11181         }
11182
11183     }
11184
11185   opcode1 = bits (arm_insn_r->arm_insn, 23, 27);
11186   if (24 == opcode1 && bit (arm_insn_r->arm_insn, 21)
11187       && !INSN_RECORDED(arm_insn_r))
11188     {
11189       ret = -1;
11190       /* Handle coprocessor insn extension space.  */
11191     }
11192
11193   /* To be done for ARMv5 and later; as of now we return -1.  */
11194   if (-1 == ret)
11195     printf_unfiltered (_("Process record does not support instruction x%0x "
11196                          "at address %s.\n"),arm_insn_r->arm_insn,
11197                          paddress (arm_insn_r->gdbarch, arm_insn_r->this_addr));
11198
11199
11200   REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
11201   MEM_ALLOC (arm_insn_r->arm_mems, arm_insn_r->mem_rec_count, record_buf_mem);
11202
11203   return ret;
11204 }
11205
11206 /* Handling opcode 000 insns.  */
11207
11208 static int
11209 arm_record_data_proc_misc_ld_str (insn_decode_record *arm_insn_r)
11210 {
11211   struct regcache *reg_cache = arm_insn_r->regcache;
11212   uint32_t record_buf[8], record_buf_mem[8];
11213   ULONGEST u_regval[2] = {0};
11214
11215   uint32_t reg_src1 = 0, reg_src2 = 0, reg_dest = 0;
11216   uint32_t immed_high = 0, immed_low = 0, offset_8 = 0, tgt_mem_addr = 0;
11217   uint32_t opcode1 = 0;
11218
11219   arm_insn_r->opcode = bits (arm_insn_r->arm_insn, 21, 24);
11220   arm_insn_r->decode = bits (arm_insn_r->arm_insn, 4, 7);
11221   opcode1 = bits (arm_insn_r->arm_insn, 20, 24);
11222
11223   /* Data processing insn /multiply insn.  */
11224   if (9 == arm_insn_r->decode
11225       && ((4 <= arm_insn_r->opcode && 7 >= arm_insn_r->opcode)
11226       ||  (0 == arm_insn_r->opcode || 1 == arm_insn_r->opcode)))
11227     {
11228       /* Handle multiply instructions.  */
11229       /* MLA, MUL, SMLAL, SMULL, UMLAL, UMULL.  */
11230         if (0 == arm_insn_r->opcode || 1 == arm_insn_r->opcode)
11231           {
11232             /* Handle MLA and MUL.  */
11233             record_buf[0] = bits (arm_insn_r->arm_insn, 16, 19);
11234             record_buf[1] = ARM_PS_REGNUM;
11235             arm_insn_r->reg_rec_count = 2;
11236           }
11237         else if (4 <= arm_insn_r->opcode && 7 >= arm_insn_r->opcode)
11238           {
11239             /* Handle SMLAL, SMULL, UMLAL, UMULL.  */
11240             record_buf[0] = bits (arm_insn_r->arm_insn, 16, 19);
11241             record_buf[1] = bits (arm_insn_r->arm_insn, 12, 15);
11242             record_buf[2] = ARM_PS_REGNUM;
11243             arm_insn_r->reg_rec_count = 3;
11244           }
11245     }
11246   else if (bit (arm_insn_r->arm_insn, INSN_S_L_BIT_NUM)
11247            && (11 == arm_insn_r->decode || 13 == arm_insn_r->decode))
11248     {
11249       /* Handle misc load insns, as 20th bit  (L = 1).  */
11250       /* LDR insn has a capability to do branching, if
11251          MOV LR, PC is precceded by LDR insn having Rn as R15
11252          in that case, it emulates branch and link insn, and hence we 
11253          need to save CSPR and PC as well. I am not sure this is right
11254          place; as opcode = 010 LDR insn make this happen, if R15 was
11255          used.  */
11256       reg_dest = bits (arm_insn_r->arm_insn, 12, 15);
11257       if (15 != reg_dest)
11258         {
11259           record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11260           arm_insn_r->reg_rec_count = 1;
11261         }
11262       else
11263         {
11264           record_buf[0] = reg_dest;
11265           record_buf[1] = ARM_PS_REGNUM;
11266           arm_insn_r->reg_rec_count = 2;
11267         }
11268     }
11269   else if ((9 == arm_insn_r->opcode || 11 == arm_insn_r->opcode)
11270            && sbo_sbz (arm_insn_r->arm_insn, 5, 12, 0)
11271            && sbo_sbz (arm_insn_r->arm_insn, 13, 4, 1)
11272            && 2 == bits (arm_insn_r->arm_insn, 20, 21))
11273     {
11274       /* Handle MSR insn.  */
11275       if (9 == arm_insn_r->opcode)
11276         {
11277           /* CSPR is going to be changed.  */
11278           record_buf[0] = ARM_PS_REGNUM;
11279           arm_insn_r->reg_rec_count = 1;
11280         }
11281       else
11282         {
11283           /* SPSR is going to be changed.  */
11284           /* How to read SPSR value?  */
11285           printf_unfiltered (_("Process record does not support instruction "
11286                             "0x%0x at address %s.\n"),
11287                             arm_insn_r->arm_insn,
11288                         paddress (arm_insn_r->gdbarch, arm_insn_r->this_addr));
11289           return -1;
11290         }
11291     }
11292   else if (9 == arm_insn_r->decode
11293            && (8 == arm_insn_r->opcode || 10 == arm_insn_r->opcode)
11294            && !bit (arm_insn_r->arm_insn, INSN_S_L_BIT_NUM))
11295     {
11296       /* Handling SWP, SWPB.  */
11297       /* These insn, changes register and memory as well.  */
11298       /* SWP or SWPB insn.  */
11299
11300       reg_src1 = bits (arm_insn_r->arm_insn, 16, 19);
11301       regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval[0]);
11302       /* SWP insn ?, swaps word.  */
11303       if (8 == arm_insn_r->opcode)
11304         {
11305           record_buf_mem[0] = 4;
11306         }
11307         else
11308         {
11309           /* SWPB insn, swaps only byte.  */
11310           record_buf_mem[0] = 1;
11311         }
11312       record_buf_mem[1] = u_regval[0];
11313       arm_insn_r->mem_rec_count = 1;
11314       record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11315       arm_insn_r->reg_rec_count = 1;
11316     }
11317   else if (3 == arm_insn_r->decode && 0x12 == opcode1
11318            && sbo_sbz (arm_insn_r->arm_insn, 9, 12, 1))
11319     {
11320       /* Handle BLX, branch and link/exchange.  */
11321       if (9 == arm_insn_r->opcode)
11322       {
11323         /* Branch is chosen by setting T bit of CSPR, bitp[0] of Rm,
11324            and R14 stores the return address.  */
11325         record_buf[0] = ARM_PS_REGNUM;
11326         record_buf[1] = ARM_LR_REGNUM;
11327         arm_insn_r->reg_rec_count = 2;
11328       }
11329     }
11330   else if (7 == arm_insn_r->decode && 0x12 == opcode1)
11331     {
11332       /* Handle enhanced software breakpoint insn, BKPT.  */
11333       /* CPSR is changed to be executed in ARM state,  disabling normal
11334          interrupts, entering abort mode.  */
11335       /* According to high vector configuration PC is set.  */
11336       /* user hit breakpoint and type reverse, in
11337          that case, we need to go back with previous CPSR and
11338          Program Counter.  */
11339       record_buf[0] = ARM_PS_REGNUM;
11340       record_buf[1] = ARM_LR_REGNUM;
11341       arm_insn_r->reg_rec_count = 2;
11342
11343       /* Save SPSR also; how?  */
11344       printf_unfiltered (_("Process record does not support instruction "
11345                            "0x%0x at address %s.\n"),arm_insn_r->arm_insn,
11346                            paddress (arm_insn_r->gdbarch, 
11347                            arm_insn_r->this_addr));
11348       return -1;
11349     }
11350   else if (11 == arm_insn_r->decode
11351            && !bit (arm_insn_r->arm_insn, INSN_S_L_BIT_NUM))
11352   {
11353     /* Handle enhanced store insns and DSP insns (e.g. LDRD).  */
11354
11355     /* Handle str(x) insn */
11356     arm_record_strx(arm_insn_r, &record_buf[0], &record_buf_mem[0],
11357                     ARM_RECORD_STRH);
11358   }
11359   else if (1 == arm_insn_r->decode && 0x12 == opcode1
11360            && sbo_sbz (arm_insn_r->arm_insn, 9, 12, 1))
11361     {
11362       /* Handle BX, branch and link/exchange.  */
11363       /* Branch is chosen by setting T bit of CSPR, bitp[0] of Rm.  */
11364       record_buf[0] = ARM_PS_REGNUM;
11365       arm_insn_r->reg_rec_count = 1;
11366     }
11367   else if (1 == arm_insn_r->decode && 0x16 == opcode1
11368            && sbo_sbz (arm_insn_r->arm_insn, 9, 4, 1)
11369            && sbo_sbz (arm_insn_r->arm_insn, 17, 4, 1))
11370     {
11371       /* Count leading zeros: CLZ.  */
11372       record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11373       arm_insn_r->reg_rec_count = 1;
11374     }
11375   else if (!bit (arm_insn_r->arm_insn, INSN_S_L_BIT_NUM)
11376            && (8 == arm_insn_r->opcode || 10 == arm_insn_r->opcode)
11377            && sbo_sbz (arm_insn_r->arm_insn, 17, 4, 1)
11378            && sbo_sbz (arm_insn_r->arm_insn, 1, 12, 0)
11379           )
11380     {
11381       /* Handle MRS insn.  */
11382       record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11383       arm_insn_r->reg_rec_count = 1;
11384     }
11385   else if (arm_insn_r->opcode <= 15)
11386     {
11387       /* Normal data processing insns.  */
11388       /* Out of 11 shifter operands mode, all the insn modifies destination
11389          register, which is specified by 13-16 decode.  */
11390       record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11391       record_buf[1] = ARM_PS_REGNUM;
11392       arm_insn_r->reg_rec_count = 2;
11393     }
11394   else
11395     {
11396       return -1;
11397     }
11398
11399   REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
11400   MEM_ALLOC (arm_insn_r->arm_mems, arm_insn_r->mem_rec_count, record_buf_mem);
11401   return 0;
11402 }
11403
11404 /* Handling opcode 001 insns.  */
11405
11406 static int
11407 arm_record_data_proc_imm (insn_decode_record *arm_insn_r)
11408 {
11409   uint32_t record_buf[8], record_buf_mem[8];
11410
11411   arm_insn_r->opcode = bits (arm_insn_r->arm_insn, 21, 24);
11412   arm_insn_r->decode = bits (arm_insn_r->arm_insn, 4, 7);
11413
11414   if ((9 == arm_insn_r->opcode || 11 == arm_insn_r->opcode)
11415       && 2 == bits (arm_insn_r->arm_insn, 20, 21)
11416       && sbo_sbz (arm_insn_r->arm_insn, 13, 4, 1)
11417      )
11418     {
11419       /* Handle MSR insn.  */
11420       if (9 == arm_insn_r->opcode)
11421         {
11422           /* CSPR is going to be changed.  */
11423           record_buf[0] = ARM_PS_REGNUM;
11424           arm_insn_r->reg_rec_count = 1;
11425         }
11426       else
11427         {
11428           /* SPSR is going to be changed.  */
11429         }
11430     }
11431   else if (arm_insn_r->opcode <= 15)
11432     {
11433       /* Normal data processing insns.  */
11434       /* Out of 11 shifter operands mode, all the insn modifies destination
11435          register, which is specified by 13-16 decode.  */
11436       record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11437       record_buf[1] = ARM_PS_REGNUM;
11438       arm_insn_r->reg_rec_count = 2;
11439     }
11440   else
11441     {
11442       return -1;
11443     }
11444
11445   REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
11446   MEM_ALLOC (arm_insn_r->arm_mems, arm_insn_r->mem_rec_count, record_buf_mem);
11447   return 0;
11448 }
11449
11450 /* Handling opcode 010 insns.  */
11451
11452 static int
11453 arm_record_ld_st_imm_offset (insn_decode_record *arm_insn_r)
11454 {
11455   struct regcache *reg_cache = arm_insn_r->regcache;
11456
11457   uint32_t reg_src1 = 0 , reg_dest = 0;
11458   uint32_t offset_12 = 0, tgt_mem_addr = 0;
11459   uint32_t record_buf[8], record_buf_mem[8];
11460
11461   ULONGEST u_regval = 0;
11462
11463   arm_insn_r->opcode = bits (arm_insn_r->arm_insn, 21, 24);
11464   arm_insn_r->decode = bits (arm_insn_r->arm_insn, 4, 7);
11465
11466   if (bit (arm_insn_r->arm_insn, INSN_S_L_BIT_NUM))
11467     {
11468       reg_dest = bits (arm_insn_r->arm_insn, 12, 15);
11469       /* LDR insn has a capability to do branching, if
11470          MOV LR, PC is precedded by LDR insn having Rn as R15
11471          in that case, it emulates branch and link insn, and hence we
11472          need to save CSPR and PC as well.  */
11473       if (ARM_PC_REGNUM != reg_dest)
11474         {
11475           record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11476           arm_insn_r->reg_rec_count = 1;
11477         }
11478       else
11479         {
11480           record_buf[0] = reg_dest;
11481           record_buf[1] = ARM_PS_REGNUM;
11482           arm_insn_r->reg_rec_count = 2;
11483         }
11484     }
11485   else
11486     {
11487       /* Store, immediate offset, immediate pre-indexed,
11488          immediate post-indexed.  */
11489       reg_src1 = bits (arm_insn_r->arm_insn, 16, 19);
11490       offset_12 = bits (arm_insn_r->arm_insn, 0, 11);
11491       regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval);
11492       /* U == 1 */
11493       if (bit (arm_insn_r->arm_insn, 23))
11494         {
11495           tgt_mem_addr = u_regval + offset_12;
11496         }
11497       else
11498         {
11499           tgt_mem_addr = u_regval - offset_12;
11500         }
11501
11502       switch (arm_insn_r->opcode)
11503         {
11504           /* STR.  */
11505           case 8:
11506           case 12:
11507           /* STR.  */
11508           case 9:
11509           case 13:
11510           /* STRT.  */    
11511           case 1:
11512           case 5:
11513           /* STR.  */    
11514           case 4:
11515           case 0:
11516             record_buf_mem[0] = 4;
11517           break;
11518
11519           /* STRB.  */
11520           case 10:
11521           case 14:
11522           /* STRB.  */    
11523           case 11:
11524           case 15:
11525           /* STRBT.  */    
11526           case 3:
11527           case 7:
11528           /* STRB.  */    
11529           case 2:
11530           case 6:
11531             record_buf_mem[0] = 1;
11532           break;
11533
11534           default:
11535             gdb_assert_not_reached ("no decoding pattern found");
11536           break;
11537         }
11538       record_buf_mem[1] = tgt_mem_addr;
11539       arm_insn_r->mem_rec_count = 1;
11540
11541       if (9 == arm_insn_r->opcode || 11 == arm_insn_r->opcode
11542           || 13 == arm_insn_r->opcode || 15 == arm_insn_r->opcode
11543           || 0 == arm_insn_r->opcode || 2 == arm_insn_r->opcode
11544           || 4 == arm_insn_r->opcode || 6 == arm_insn_r->opcode
11545           || 1 == arm_insn_r->opcode || 3 == arm_insn_r->opcode
11546           || 5 == arm_insn_r->opcode || 7 == arm_insn_r->opcode
11547          )
11548         {
11549           /* We are handling pre-indexed mode; post-indexed mode;
11550              where Rn is going to be changed.  */
11551           record_buf[0] = reg_src1;
11552           arm_insn_r->reg_rec_count = 1;
11553         }
11554     }
11555
11556   REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
11557   MEM_ALLOC (arm_insn_r->arm_mems, arm_insn_r->mem_rec_count, record_buf_mem);
11558   return 0;
11559 }
11560
11561 /* Handling opcode 011 insns.  */
11562
11563 static int
11564 arm_record_ld_st_reg_offset (insn_decode_record *arm_insn_r)
11565 {
11566   struct regcache *reg_cache = arm_insn_r->regcache;
11567
11568   uint32_t shift_imm = 0;
11569   uint32_t reg_src1 = 0, reg_src2 = 0, reg_dest = 0;
11570   uint32_t offset_12 = 0, tgt_mem_addr = 0;
11571   uint32_t record_buf[8], record_buf_mem[8];
11572
11573   LONGEST s_word;
11574   ULONGEST u_regval[2];
11575
11576   arm_insn_r->opcode = bits (arm_insn_r->arm_insn, 21, 24);
11577   arm_insn_r->decode = bits (arm_insn_r->arm_insn, 4, 7);
11578
11579   /* Handle enhanced store insns and LDRD DSP insn,
11580      order begins according to addressing modes for store insns
11581      STRH insn.  */
11582
11583   /* LDR or STR?  */
11584   if (bit (arm_insn_r->arm_insn, INSN_S_L_BIT_NUM))
11585     {
11586       reg_dest = bits (arm_insn_r->arm_insn, 12, 15);
11587       /* LDR insn has a capability to do branching, if
11588          MOV LR, PC is precedded by LDR insn having Rn as R15
11589          in that case, it emulates branch and link insn, and hence we
11590          need to save CSPR and PC as well.  */
11591       if (15 != reg_dest)
11592         {
11593           record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11594           arm_insn_r->reg_rec_count = 1;
11595         }
11596       else
11597         {
11598           record_buf[0] = reg_dest;
11599           record_buf[1] = ARM_PS_REGNUM;
11600           arm_insn_r->reg_rec_count = 2;
11601         }
11602     }
11603   else
11604     {
11605       if (! bits (arm_insn_r->arm_insn, 4, 11))
11606         {
11607           /* Store insn, register offset and register pre-indexed,
11608              register post-indexed.  */
11609           /* Get Rm.  */
11610           reg_src1 = bits (arm_insn_r->arm_insn, 0, 3);
11611           /* Get Rn.  */
11612           reg_src2 = bits (arm_insn_r->arm_insn, 16, 19);
11613           regcache_raw_read_unsigned (reg_cache, reg_src1
11614                                       , &u_regval[0]);
11615           regcache_raw_read_unsigned (reg_cache, reg_src2
11616                                       , &u_regval[1]);
11617           if (15 == reg_src2)
11618             {
11619               /* If R15 was used as Rn, hence current PC+8.  */
11620               /* Pre-indexed mode doesnt reach here ; illegal insn.  */
11621                 u_regval[0] = u_regval[0] + 8;
11622             }
11623           /* Calculate target store address, Rn +/- Rm, register offset.  */
11624           /* U == 1.  */
11625           if (bit (arm_insn_r->arm_insn, 23))
11626             {
11627               tgt_mem_addr = u_regval[0] + u_regval[1];
11628             }
11629           else
11630             {
11631               tgt_mem_addr = u_regval[1] - u_regval[0];
11632             }
11633
11634           switch (arm_insn_r->opcode)
11635             {
11636               /* STR.  */
11637               case 8:
11638               case 12:
11639               /* STR.  */    
11640               case 9:
11641               case 13:
11642               /* STRT.  */
11643               case 1:
11644               case 5:
11645               /* STR.  */
11646               case 0:
11647               case 4:
11648                 record_buf_mem[0] = 4;
11649               break;
11650
11651               /* STRB.  */
11652               case 10:
11653               case 14:
11654               /* STRB.  */
11655               case 11:
11656               case 15:
11657               /* STRBT.  */    
11658               case 3:
11659               case 7:
11660               /* STRB.  */
11661               case 2:
11662               case 6:
11663                 record_buf_mem[0] = 1;
11664               break;
11665
11666               default:
11667                 gdb_assert_not_reached ("no decoding pattern found");
11668               break;
11669             }
11670           record_buf_mem[1] = tgt_mem_addr;
11671           arm_insn_r->mem_rec_count = 1;
11672
11673           if (9 == arm_insn_r->opcode || 11 == arm_insn_r->opcode
11674               || 13 == arm_insn_r->opcode || 15 == arm_insn_r->opcode
11675               || 0 == arm_insn_r->opcode || 2 == arm_insn_r->opcode
11676               || 4 == arm_insn_r->opcode || 6 == arm_insn_r->opcode
11677               || 1 == arm_insn_r->opcode || 3 == arm_insn_r->opcode
11678               || 5 == arm_insn_r->opcode || 7 == arm_insn_r->opcode
11679              )
11680             {
11681               /* Rn is going to be changed in pre-indexed mode and
11682                  post-indexed mode as well.  */
11683               record_buf[0] = reg_src2;
11684               arm_insn_r->reg_rec_count = 1;
11685             }
11686         }
11687       else
11688         {
11689           /* Store insn, scaled register offset; scaled pre-indexed.  */
11690           offset_12 = bits (arm_insn_r->arm_insn, 5, 6);
11691           /* Get Rm.  */
11692           reg_src1 = bits (arm_insn_r->arm_insn, 0, 3);
11693           /* Get Rn.  */
11694           reg_src2 = bits (arm_insn_r->arm_insn, 16, 19);
11695           /* Get shift_imm.  */
11696           shift_imm = bits (arm_insn_r->arm_insn, 7, 11);
11697           regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval[0]);
11698           regcache_raw_read_signed (reg_cache, reg_src1, &s_word);
11699           regcache_raw_read_unsigned (reg_cache, reg_src2, &u_regval[1]);
11700           /* Offset_12 used as shift.  */
11701           switch (offset_12)
11702             {
11703               case 0:
11704                 /* Offset_12 used as index.  */
11705                 offset_12 = u_regval[0] << shift_imm;
11706               break;
11707
11708               case 1:
11709                 offset_12 = (!shift_imm)?0:u_regval[0] >> shift_imm;
11710               break;
11711
11712               case 2:
11713                 if (!shift_imm)
11714                   {
11715                     if (bit (u_regval[0], 31))
11716                       {
11717                         offset_12 = 0xFFFFFFFF;
11718                       }
11719                     else
11720                       {
11721                         offset_12 = 0;
11722                       }
11723                   }
11724                 else
11725                   {
11726                     /* This is arithmetic shift.  */
11727                     offset_12 = s_word >> shift_imm;
11728                   }
11729                 break;
11730
11731               case 3:
11732                 if (!shift_imm)
11733                   {
11734                     regcache_raw_read_unsigned (reg_cache, ARM_PS_REGNUM,
11735                                                 &u_regval[1]);
11736                     /* Get C flag value and shift it by 31.  */
11737                     offset_12 = (((bit (u_regval[1], 29)) << 31) \
11738                                   | (u_regval[0]) >> 1);
11739                   }
11740                 else
11741                   {
11742                     offset_12 = (u_regval[0] >> shift_imm) \
11743                                 | (u_regval[0] <<
11744                                 (sizeof(uint32_t) - shift_imm));
11745                   }
11746               break;
11747
11748               default:
11749                 gdb_assert_not_reached ("no decoding pattern found");
11750               break;
11751             }
11752
11753           regcache_raw_read_unsigned (reg_cache, reg_src2, &u_regval[1]);
11754           /* bit U set.  */
11755           if (bit (arm_insn_r->arm_insn, 23))
11756             {
11757               tgt_mem_addr = u_regval[1] + offset_12;
11758             }
11759           else
11760             {
11761               tgt_mem_addr = u_regval[1] - offset_12;
11762             }
11763
11764           switch (arm_insn_r->opcode)
11765             {
11766               /* STR.  */
11767               case 8:
11768               case 12:
11769               /* STR.  */    
11770               case 9:
11771               case 13:
11772               /* STRT.  */
11773               case 1:
11774               case 5:
11775               /* STR.  */
11776               case 0:
11777               case 4:
11778                 record_buf_mem[0] = 4;
11779               break;
11780
11781               /* STRB.  */
11782               case 10:
11783               case 14:
11784               /* STRB.  */
11785               case 11:
11786               case 15:
11787               /* STRBT.  */    
11788               case 3:
11789               case 7:
11790               /* STRB.  */
11791               case 2:
11792               case 6:
11793                 record_buf_mem[0] = 1;
11794               break;
11795
11796               default:
11797                 gdb_assert_not_reached ("no decoding pattern found");
11798               break;
11799             }
11800           record_buf_mem[1] = tgt_mem_addr;
11801           arm_insn_r->mem_rec_count = 1;
11802
11803           if (9 == arm_insn_r->opcode || 11 == arm_insn_r->opcode
11804               || 13 == arm_insn_r->opcode || 15 == arm_insn_r->opcode
11805               || 0 == arm_insn_r->opcode || 2 == arm_insn_r->opcode
11806               || 4 == arm_insn_r->opcode || 6 == arm_insn_r->opcode
11807               || 1 == arm_insn_r->opcode || 3 == arm_insn_r->opcode
11808               || 5 == arm_insn_r->opcode || 7 == arm_insn_r->opcode
11809              )
11810             {
11811               /* Rn is going to be changed in register scaled pre-indexed
11812                  mode,and scaled post indexed mode.  */
11813               record_buf[0] = reg_src2;
11814               arm_insn_r->reg_rec_count = 1;
11815             }
11816         }
11817     }
11818
11819   REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
11820   MEM_ALLOC (arm_insn_r->arm_mems, arm_insn_r->mem_rec_count, record_buf_mem);
11821   return 0;
11822 }
11823
11824 /* Handling opcode 100 insns.  */
11825
11826 static int
11827 arm_record_ld_st_multiple (insn_decode_record *arm_insn_r)
11828 {
11829   struct regcache *reg_cache = arm_insn_r->regcache;
11830
11831   uint32_t register_list[16] = {0}, register_count = 0, register_bits = 0;
11832   uint32_t reg_src1 = 0, addr_mode = 0, no_of_regs = 0;
11833   uint32_t start_address = 0, index = 0;
11834   uint32_t record_buf[24], record_buf_mem[48];
11835
11836   ULONGEST u_regval[2] = {0};
11837
11838   /* This mode is exclusively for load and store multiple.  */
11839   /* Handle incremenrt after/before and decrment after.before mode;
11840      Rn is changing depending on W bit, but as of now we store Rn too
11841      without optimization.  */
11842
11843   if (bit (arm_insn_r->arm_insn, INSN_S_L_BIT_NUM))
11844     {
11845       /* LDM  (1,2,3) where LDM  (3) changes CPSR too.  */
11846
11847       if (bit (arm_insn_r->arm_insn, 20) && !bit (arm_insn_r->arm_insn, 22))
11848         {
11849           register_bits = bits (arm_insn_r->arm_insn, 0, 15);
11850           no_of_regs = 15;
11851         }
11852       else
11853         {
11854           register_bits = bits (arm_insn_r->arm_insn, 0, 14);
11855           no_of_regs = 14;
11856         }
11857       /* Get Rn.  */
11858       reg_src1 = bits (arm_insn_r->arm_insn, 16, 19);
11859       while (register_bits)
11860       {
11861         if (register_bits & 0x00000001)
11862           record_buf[index++] = register_count;
11863         register_bits = register_bits >> 1;
11864         register_count++;
11865       }
11866
11867         /* Extra space for Base Register and CPSR; wihtout optimization.  */
11868         record_buf[index++] = reg_src1;
11869         record_buf[index++] = ARM_PS_REGNUM;
11870         arm_insn_r->reg_rec_count = index;
11871     }
11872   else
11873     {
11874       /* It handles both STM(1) and STM(2).  */
11875       addr_mode = bits (arm_insn_r->arm_insn, 23, 24);    
11876
11877       register_bits = bits (arm_insn_r->arm_insn, 0, 15);
11878       /* Get Rn.  */
11879       reg_src1 = bits (arm_insn_r->arm_insn, 16, 19);
11880       regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval[0]);
11881       while (register_bits)
11882         {
11883           if (register_bits & 0x00000001)
11884             register_count++;
11885           register_bits = register_bits >> 1;
11886         }
11887
11888       switch (addr_mode)
11889         {
11890           /* Decrement after.  */
11891           case 0:                          
11892             start_address = (u_regval[0]) - (register_count * 4) + 4;
11893             arm_insn_r->mem_rec_count = register_count;
11894             while (register_count)
11895               {
11896                 record_buf_mem[(register_count * 2) - 1] = start_address;
11897                 record_buf_mem[(register_count * 2) - 2] = 4;
11898                 start_address = start_address + 4;
11899                 register_count--;
11900               }
11901           break;    
11902
11903           /* Increment after.  */
11904           case 1:
11905             start_address = u_regval[0];
11906             arm_insn_r->mem_rec_count = register_count;
11907             while (register_count)
11908               {
11909                 record_buf_mem[(register_count * 2) - 1] = start_address;
11910                 record_buf_mem[(register_count * 2) - 2] = 4;
11911                 start_address = start_address + 4;
11912                 register_count--;
11913               }
11914           break;    
11915
11916           /* Decrement before.  */
11917           case 2:
11918
11919             start_address = (u_regval[0]) - (register_count * 4);
11920             arm_insn_r->mem_rec_count = register_count;
11921             while (register_count)
11922               {
11923                 record_buf_mem[(register_count * 2) - 1] = start_address;
11924                 record_buf_mem[(register_count * 2) - 2] = 4;
11925                 start_address = start_address + 4;
11926                 register_count--;
11927               }
11928           break;    
11929
11930           /* Increment before.  */
11931           case 3:
11932             start_address = u_regval[0] + 4;
11933             arm_insn_r->mem_rec_count = register_count;
11934             while (register_count)
11935               {
11936                 record_buf_mem[(register_count * 2) - 1] = start_address;
11937                 record_buf_mem[(register_count * 2) - 2] = 4;
11938                 start_address = start_address + 4;
11939                 register_count--;
11940               }
11941           break;    
11942
11943           default:
11944             gdb_assert_not_reached ("no decoding pattern found");
11945           break;    
11946         }
11947
11948       /* Base register also changes; based on condition and W bit.  */
11949       /* We save it anyway without optimization.  */
11950       record_buf[0] = reg_src1;
11951       arm_insn_r->reg_rec_count = 1;
11952     }
11953
11954   REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
11955   MEM_ALLOC (arm_insn_r->arm_mems, arm_insn_r->mem_rec_count, record_buf_mem);
11956   return 0;
11957 }
11958
11959 /* Handling opcode 101 insns.  */
11960
11961 static int
11962 arm_record_b_bl (insn_decode_record *arm_insn_r)
11963 {
11964   uint32_t record_buf[8];
11965
11966   /* Handle B, BL, BLX(1) insns.  */
11967   /* B simply branches so we do nothing here.  */
11968   /* Note: BLX(1) doesnt fall here but instead it falls into
11969      extension space.  */
11970   if (bit (arm_insn_r->arm_insn, 24))
11971   {
11972     record_buf[0] = ARM_LR_REGNUM;
11973     arm_insn_r->reg_rec_count = 1;
11974   }
11975
11976   REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
11977
11978   return 0;
11979 }
11980
11981 /* Handling opcode 110 insns.  */
11982
11983 static int
11984 arm_record_unsupported_insn (insn_decode_record *arm_insn_r)
11985 {
11986   printf_unfiltered (_("Process record does not support instruction "
11987                     "0x%0x at address %s.\n"),arm_insn_r->arm_insn,
11988                     paddress (arm_insn_r->gdbarch, arm_insn_r->this_addr));
11989
11990   return -1;
11991 }
11992
11993 /* Handling opcode 111 insns.  */
11994
11995 static int
11996 arm_record_coproc_data_proc (insn_decode_record *arm_insn_r)
11997 {
11998   struct gdbarch_tdep *tdep = gdbarch_tdep (arm_insn_r->gdbarch);
11999   struct regcache *reg_cache = arm_insn_r->regcache;
12000   uint32_t ret = 0; /* function return value: -1:record failure ;  0:success  */
12001   ULONGEST u_regval = 0;
12002
12003   arm_insn_r->opcode = bits (arm_insn_r->arm_insn, 24, 27);
12004
12005   /* Handle arm SWI/SVC system call instructions.  */
12006   if (15 == arm_insn_r->opcode)
12007     {
12008       if (tdep->arm_syscall_record != NULL)
12009         {
12010           ULONGEST svc_operand, svc_number;
12011
12012           svc_operand = (0x00ffffff & arm_insn_r->arm_insn);
12013
12014           if (svc_operand)  /* OABI.  */
12015             svc_number = svc_operand - 0x900000;
12016           else /* EABI.  */
12017             regcache_raw_read_unsigned (reg_cache, 7, &svc_number);
12018
12019           ret = tdep->arm_syscall_record (reg_cache, svc_number);
12020         }
12021       else
12022         {
12023           printf_unfiltered (_("no syscall record support\n"));
12024           ret = -1;
12025         }
12026     }
12027   else
12028     {
12029       arm_record_unsupported_insn (arm_insn_r);
12030       ret = -1;
12031     }
12032
12033   return ret;
12034 }
12035
12036 /* Handling opcode 000 insns.  */
12037
12038 static int
12039 thumb_record_shift_add_sub (insn_decode_record *thumb_insn_r)
12040 {
12041   uint32_t record_buf[8];
12042   uint32_t reg_src1 = 0;
12043
12044   reg_src1 = bits (thumb_insn_r->arm_insn, 0, 2);
12045
12046   record_buf[0] = ARM_PS_REGNUM;
12047   record_buf[1] = reg_src1;
12048   thumb_insn_r->reg_rec_count = 2;
12049
12050   REG_ALLOC (thumb_insn_r->arm_regs, thumb_insn_r->reg_rec_count, record_buf);
12051
12052   return 0;
12053 }
12054
12055
12056 /* Handling opcode 001 insns.  */
12057
12058 static int
12059 thumb_record_add_sub_cmp_mov (insn_decode_record *thumb_insn_r)
12060 {
12061   uint32_t record_buf[8];
12062   uint32_t reg_src1 = 0;
12063
12064   reg_src1 = bits (thumb_insn_r->arm_insn, 8, 10);
12065
12066   record_buf[0] = ARM_PS_REGNUM;
12067   record_buf[1] = reg_src1;
12068   thumb_insn_r->reg_rec_count = 2;
12069
12070   REG_ALLOC (thumb_insn_r->arm_regs, thumb_insn_r->reg_rec_count, record_buf);
12071
12072   return 0;
12073 }
12074
12075 /* Handling opcode 010 insns.  */
12076
12077 static int
12078 thumb_record_ld_st_reg_offset (insn_decode_record *thumb_insn_r)
12079 {
12080   struct regcache *reg_cache =  thumb_insn_r->regcache;
12081   uint32_t record_buf[8], record_buf_mem[8];
12082
12083   uint32_t reg_src1 = 0, reg_src2 = 0;
12084   uint32_t opcode1 = 0, opcode2 = 0, opcode3 = 0;
12085
12086   ULONGEST u_regval[2] = {0};
12087
12088   opcode1 = bits (thumb_insn_r->arm_insn, 10, 12);
12089
12090   if (bit (thumb_insn_r->arm_insn, 12))
12091     {
12092       /* Handle load/store register offset.  */
12093       opcode2 = bits (thumb_insn_r->arm_insn, 9, 10);
12094       if (opcode2 >= 12 && opcode2 <= 15)
12095         {
12096           /* LDR(2), LDRB(2) , LDRH(2), LDRSB, LDRSH.  */
12097           reg_src1 = bits (thumb_insn_r->arm_insn,0, 2);
12098           record_buf[0] = reg_src1;
12099           thumb_insn_r->reg_rec_count = 1;
12100         }
12101       else if (opcode2 >= 8 && opcode2 <= 10)
12102         {
12103           /* STR(2), STRB(2), STRH(2) .  */
12104           reg_src1 = bits (thumb_insn_r->arm_insn, 3, 5);
12105           reg_src2 = bits (thumb_insn_r->arm_insn, 6, 8);
12106           regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval[0]);
12107           regcache_raw_read_unsigned (reg_cache, reg_src2, &u_regval[1]);
12108           if (8 == opcode2)
12109             record_buf_mem[0] = 4;    /* STR (2).  */
12110           else if (10 == opcode2)
12111             record_buf_mem[0] = 1;    /*  STRB (2).  */
12112           else if (9 == opcode2)
12113             record_buf_mem[0] = 2;    /* STRH (2).  */
12114           record_buf_mem[1] = u_regval[0] + u_regval[1];
12115           thumb_insn_r->mem_rec_count = 1;
12116         }
12117     }
12118   else if (bit (thumb_insn_r->arm_insn, 11))
12119     {
12120       /* Handle load from literal pool.  */
12121       /* LDR(3).  */
12122       reg_src1 = bits (thumb_insn_r->arm_insn, 8, 10);
12123       record_buf[0] = reg_src1;
12124       thumb_insn_r->reg_rec_count = 1;
12125     }
12126   else if (opcode1)
12127     {
12128       opcode2 = bits (thumb_insn_r->arm_insn, 8, 9);
12129       opcode3 = bits (thumb_insn_r->arm_insn, 0, 2);
12130       if ((3 == opcode2) && (!opcode3))
12131         {
12132           /* Branch with exchange.  */
12133           record_buf[0] = ARM_PS_REGNUM;
12134           thumb_insn_r->reg_rec_count = 1;
12135         }
12136       else
12137         {
12138           /* Format 8; special data processing insns.  */
12139           reg_src1 = bits (thumb_insn_r->arm_insn, 0, 2);
12140           record_buf[0] = ARM_PS_REGNUM;
12141           record_buf[1] = reg_src1;
12142           thumb_insn_r->reg_rec_count = 2;
12143         }
12144     }
12145   else
12146     {
12147       /* Format 5; data processing insns.  */
12148       reg_src1 = bits (thumb_insn_r->arm_insn, 0, 2);
12149       if (bit (thumb_insn_r->arm_insn, 7))
12150         {
12151           reg_src1 = reg_src1 + 8;
12152         }
12153       record_buf[0] = ARM_PS_REGNUM;
12154       record_buf[1] = reg_src1;
12155       thumb_insn_r->reg_rec_count = 2;
12156     }
12157
12158   REG_ALLOC (thumb_insn_r->arm_regs, thumb_insn_r->reg_rec_count, record_buf);
12159   MEM_ALLOC (thumb_insn_r->arm_mems, thumb_insn_r->mem_rec_count,
12160              record_buf_mem);
12161
12162   return 0;
12163 }
12164
12165 /* Handling opcode 001 insns.  */
12166
12167 static int
12168 thumb_record_ld_st_imm_offset (insn_decode_record *thumb_insn_r)
12169 {
12170   struct regcache *reg_cache = thumb_insn_r->regcache;
12171   uint32_t record_buf[8], record_buf_mem[8];
12172
12173   uint32_t reg_src1 = 0;
12174   uint32_t opcode = 0, immed_5 = 0;
12175
12176   ULONGEST u_regval = 0;
12177
12178   opcode = bits (thumb_insn_r->arm_insn, 11, 12);
12179
12180   if (opcode)
12181     {
12182       /* LDR(1).  */
12183       reg_src1 = bits (thumb_insn_r->arm_insn, 0, 2);
12184       record_buf[0] = reg_src1;
12185       thumb_insn_r->reg_rec_count = 1;
12186     }
12187   else
12188     {
12189       /* STR(1).  */
12190       reg_src1 = bits (thumb_insn_r->arm_insn, 3, 5);
12191       immed_5 = bits (thumb_insn_r->arm_insn, 6, 10);
12192       regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval);
12193       record_buf_mem[0] = 4;
12194       record_buf_mem[1] = u_regval + (immed_5 * 4);
12195       thumb_insn_r->mem_rec_count = 1;
12196     }
12197
12198   REG_ALLOC (thumb_insn_r->arm_regs, thumb_insn_r->reg_rec_count, record_buf);
12199   MEM_ALLOC (thumb_insn_r->arm_mems, thumb_insn_r->mem_rec_count, 
12200              record_buf_mem);
12201
12202   return 0;
12203 }
12204
12205 /* Handling opcode 100 insns.  */
12206
12207 static int
12208 thumb_record_ld_st_stack (insn_decode_record *thumb_insn_r)
12209 {
12210   struct regcache *reg_cache = thumb_insn_r->regcache;
12211   uint32_t record_buf[8], record_buf_mem[8];
12212
12213   uint32_t reg_src1 = 0;
12214   uint32_t opcode = 0, immed_8 = 0, immed_5 = 0;
12215
12216   ULONGEST u_regval = 0;
12217
12218   opcode = bits (thumb_insn_r->arm_insn, 11, 12);
12219
12220   if (3 == opcode)
12221     {
12222       /* LDR(4).  */
12223       reg_src1 = bits (thumb_insn_r->arm_insn, 8, 10);
12224       record_buf[0] = reg_src1;
12225       thumb_insn_r->reg_rec_count = 1;
12226     }
12227   else if (1 == opcode)
12228     {
12229       /* LDRH(1).  */
12230       reg_src1 = bits (thumb_insn_r->arm_insn, 0, 2);
12231       record_buf[0] = reg_src1;
12232       thumb_insn_r->reg_rec_count = 1;
12233     }
12234   else if (2 == opcode)
12235     {
12236       /* STR(3).  */
12237       immed_8 = bits (thumb_insn_r->arm_insn, 0, 7);
12238       regcache_raw_read_unsigned (reg_cache, ARM_SP_REGNUM, &u_regval);
12239       record_buf_mem[0] = 4;
12240       record_buf_mem[1] = u_regval + (immed_8 * 4);
12241       thumb_insn_r->mem_rec_count = 1;
12242     }
12243   else if (0 == opcode)
12244     {
12245       /* STRH(1).  */
12246       immed_5 = bits (thumb_insn_r->arm_insn, 6, 10);
12247       reg_src1 = bits (thumb_insn_r->arm_insn, 3, 5);
12248       regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval);
12249       record_buf_mem[0] = 2;
12250       record_buf_mem[1] = u_regval + (immed_5 * 2);
12251       thumb_insn_r->mem_rec_count = 1;
12252     }
12253
12254   REG_ALLOC (thumb_insn_r->arm_regs, thumb_insn_r->reg_rec_count, record_buf);
12255   MEM_ALLOC (thumb_insn_r->arm_mems, thumb_insn_r->mem_rec_count,
12256              record_buf_mem);
12257
12258   return 0;
12259 }
12260
12261 /* Handling opcode 101 insns.  */
12262
12263 static int
12264 thumb_record_misc (insn_decode_record *thumb_insn_r)
12265 {
12266   struct regcache *reg_cache = thumb_insn_r->regcache;
12267
12268   uint32_t opcode = 0, opcode1 = 0, opcode2 = 0;
12269   uint32_t register_bits = 0, register_count = 0;
12270   uint32_t register_list[8] = {0}, index = 0, start_address = 0;
12271   uint32_t record_buf[24], record_buf_mem[48];
12272   uint32_t reg_src1;
12273
12274   ULONGEST u_regval = 0;
12275
12276   opcode = bits (thumb_insn_r->arm_insn, 11, 12);
12277   opcode1 = bits (thumb_insn_r->arm_insn, 8, 12);
12278   opcode2 = bits (thumb_insn_r->arm_insn, 9, 12);
12279
12280   if (14 == opcode2)
12281     {
12282       /* POP.  */
12283       register_bits = bits (thumb_insn_r->arm_insn, 0, 7);
12284       while (register_bits)
12285       {
12286         if (register_bits & 0x00000001)
12287           record_buf[index++] = register_count;
12288         register_bits = register_bits >> 1;
12289         register_count++;
12290       }
12291       record_buf[index++] = ARM_PS_REGNUM;
12292       record_buf[index++] = ARM_SP_REGNUM;
12293       thumb_insn_r->reg_rec_count = index;
12294     }
12295   else if (10 == opcode2)
12296     {
12297       /* PUSH.  */
12298       register_bits = bits (thumb_insn_r->arm_insn, 0, 7);
12299       regcache_raw_read_unsigned (reg_cache, ARM_SP_REGNUM, &u_regval);
12300       while (register_bits)
12301         {
12302           if (register_bits & 0x00000001)
12303             register_count++;
12304           register_bits = register_bits >> 1;
12305         }
12306       start_address = u_regval -  \
12307                   (4 * (bit (thumb_insn_r->arm_insn, 8) + register_count));
12308       thumb_insn_r->mem_rec_count = register_count;
12309       while (register_count)
12310         {
12311           record_buf_mem[(register_count * 2) - 1] = start_address;
12312           record_buf_mem[(register_count * 2) - 2] = 4;
12313           start_address = start_address + 4;
12314           register_count--;
12315         }
12316       record_buf[0] = ARM_SP_REGNUM;
12317       thumb_insn_r->reg_rec_count = 1;
12318     }
12319   else if (0x1E == opcode1)
12320     {
12321       /* BKPT insn.  */
12322       /* Handle enhanced software breakpoint insn, BKPT.  */
12323       /* CPSR is changed to be executed in ARM state,  disabling normal
12324          interrupts, entering abort mode.  */
12325       /* According to high vector configuration PC is set.  */
12326       /* User hits breakpoint and type reverse, in that case, we need to go back with 
12327       previous CPSR and Program Counter.  */
12328       record_buf[0] = ARM_PS_REGNUM;
12329       record_buf[1] = ARM_LR_REGNUM;
12330       thumb_insn_r->reg_rec_count = 2;
12331       /* We need to save SPSR value, which is not yet done.  */
12332       printf_unfiltered (_("Process record does not support instruction "
12333                            "0x%0x at address %s.\n"),
12334                            thumb_insn_r->arm_insn,
12335                            paddress (thumb_insn_r->gdbarch,
12336                            thumb_insn_r->this_addr));
12337       return -1;
12338     }
12339   else if ((0 == opcode) || (1 == opcode))
12340     {
12341       /* ADD(5), ADD(6).  */
12342       reg_src1 = bits (thumb_insn_r->arm_insn, 8, 10);
12343       record_buf[0] = reg_src1;
12344       thumb_insn_r->reg_rec_count = 1;
12345     }
12346   else if (2 == opcode)
12347     {
12348       /* ADD(7), SUB(4).  */
12349       reg_src1 = bits (thumb_insn_r->arm_insn, 8, 10);
12350       record_buf[0] = ARM_SP_REGNUM;
12351       thumb_insn_r->reg_rec_count = 1;
12352     }
12353
12354   REG_ALLOC (thumb_insn_r->arm_regs, thumb_insn_r->reg_rec_count, record_buf);
12355   MEM_ALLOC (thumb_insn_r->arm_mems, thumb_insn_r->mem_rec_count,
12356              record_buf_mem);
12357
12358   return 0;
12359 }
12360
12361 /* Handling opcode 110 insns.  */
12362
12363 static int
12364 thumb_record_ldm_stm_swi (insn_decode_record *thumb_insn_r)                
12365 {
12366   struct gdbarch_tdep *tdep = gdbarch_tdep (thumb_insn_r->gdbarch);
12367   struct regcache *reg_cache = thumb_insn_r->regcache;
12368
12369   uint32_t ret = 0; /* function return value: -1:record failure ;  0:success  */
12370   uint32_t reg_src1 = 0;
12371   uint32_t opcode1 = 0, opcode2 = 0, register_bits = 0, register_count = 0;
12372   uint32_t register_list[8] = {0}, index = 0, start_address = 0;
12373   uint32_t record_buf[24], record_buf_mem[48];
12374
12375   ULONGEST u_regval = 0;
12376
12377   opcode1 = bits (thumb_insn_r->arm_insn, 8, 12);
12378   opcode2 = bits (thumb_insn_r->arm_insn, 11, 12);
12379
12380   if (1 == opcode2)
12381     {
12382
12383       /* LDMIA.  */
12384       register_bits = bits (thumb_insn_r->arm_insn, 0, 7);
12385       /* Get Rn.  */
12386       reg_src1 = bits (thumb_insn_r->arm_insn, 8, 10);
12387       while (register_bits)
12388         {
12389           if (register_bits & 0x00000001)
12390             record_buf[index++] = register_count;
12391           register_bits = register_bits >> 1;
12392           register_count++;
12393         }
12394       record_buf[index++] = reg_src1;
12395       thumb_insn_r->reg_rec_count = index;
12396     }
12397   else if (0 == opcode2)
12398     {
12399       /* It handles both STMIA.  */
12400       register_bits = bits (thumb_insn_r->arm_insn, 0, 7);
12401       /* Get Rn.  */
12402       reg_src1 = bits (thumb_insn_r->arm_insn, 8, 10);
12403       regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval);
12404       while (register_bits)
12405         {
12406           if (register_bits & 0x00000001)
12407             register_count++;
12408           register_bits = register_bits >> 1;
12409         }
12410       start_address = u_regval;
12411       thumb_insn_r->mem_rec_count = register_count;
12412       while (register_count)
12413         {
12414           record_buf_mem[(register_count * 2) - 1] = start_address;
12415           record_buf_mem[(register_count * 2) - 2] = 4;
12416           start_address = start_address + 4;
12417           register_count--;
12418         }
12419     }
12420   else if (0x1F == opcode1)
12421     {
12422         /* Handle arm syscall insn.  */
12423         if (tdep->arm_syscall_record != NULL)
12424           {
12425             regcache_raw_read_unsigned (reg_cache, 7, &u_regval);
12426             ret = tdep->arm_syscall_record (reg_cache, u_regval);
12427           }
12428         else
12429           {
12430             printf_unfiltered (_("no syscall record support\n"));
12431             return -1;
12432           }
12433     }
12434
12435   /* B (1), conditional branch is automatically taken care in process_record,
12436     as PC is saved there.  */
12437
12438   REG_ALLOC (thumb_insn_r->arm_regs, thumb_insn_r->reg_rec_count, record_buf);
12439   MEM_ALLOC (thumb_insn_r->arm_mems, thumb_insn_r->mem_rec_count,
12440              record_buf_mem);
12441
12442   return ret;
12443 }
12444
12445 /* Handling opcode 111 insns.  */
12446
12447 static int
12448 thumb_record_branch (insn_decode_record *thumb_insn_r)
12449 {
12450   uint32_t record_buf[8];
12451   uint32_t bits_h = 0;
12452
12453   bits_h = bits (thumb_insn_r->arm_insn, 11, 12);
12454
12455   if (2 == bits_h || 3 == bits_h)
12456     {
12457       /* BL */
12458       record_buf[0] = ARM_LR_REGNUM;
12459       thumb_insn_r->reg_rec_count = 1;
12460     }
12461   else if (1 == bits_h)
12462     {
12463       /* BLX(1). */
12464       record_buf[0] = ARM_PS_REGNUM;
12465       record_buf[1] = ARM_LR_REGNUM;
12466       thumb_insn_r->reg_rec_count = 2;
12467     }
12468
12469   /* B(2) is automatically taken care in process_record, as PC is 
12470      saved there.  */
12471
12472   REG_ALLOC (thumb_insn_r->arm_regs, thumb_insn_r->reg_rec_count, record_buf);
12473
12474   return 0;     
12475 }
12476
12477 /* Handler for thumb2 load/store multiple instructions.  */
12478
12479 static int
12480 thumb2_record_ld_st_multiple (insn_decode_record *thumb2_insn_r)
12481 {
12482   struct regcache *reg_cache = thumb2_insn_r->regcache;
12483
12484   uint32_t reg_rn, op;
12485   uint32_t register_bits = 0, register_count = 0;
12486   uint32_t index = 0, start_address = 0;
12487   uint32_t record_buf[24], record_buf_mem[48];
12488
12489   ULONGEST u_regval = 0;
12490
12491   reg_rn = bits (thumb2_insn_r->arm_insn, 16, 19);
12492   op = bits (thumb2_insn_r->arm_insn, 23, 24);
12493
12494   if (0 == op || 3 == op)
12495     {
12496       if (bit (thumb2_insn_r->arm_insn, INSN_S_L_BIT_NUM))
12497         {
12498           /* Handle RFE instruction.  */
12499           record_buf[0] = ARM_PS_REGNUM;
12500           thumb2_insn_r->reg_rec_count = 1;
12501         }
12502       else
12503         {
12504           /* Handle SRS instruction after reading banked SP.  */
12505           return arm_record_unsupported_insn (thumb2_insn_r);
12506         }
12507     }
12508   else if (1 == op || 2 == op)
12509     {
12510       if (bit (thumb2_insn_r->arm_insn, INSN_S_L_BIT_NUM))
12511         {
12512           /* Handle LDM/LDMIA/LDMFD and LDMDB/LDMEA instructions.  */
12513           register_bits = bits (thumb2_insn_r->arm_insn, 0, 15);
12514           while (register_bits)
12515             {
12516               if (register_bits & 0x00000001)
12517                 record_buf[index++] = register_count;
12518
12519               register_count++;
12520               register_bits = register_bits >> 1;
12521             }
12522           record_buf[index++] = reg_rn;
12523           record_buf[index++] = ARM_PS_REGNUM;
12524           thumb2_insn_r->reg_rec_count = index;
12525         }
12526       else
12527         {
12528           /* Handle STM/STMIA/STMEA and STMDB/STMFD.  */
12529           register_bits = bits (thumb2_insn_r->arm_insn, 0, 15);
12530           regcache_raw_read_unsigned (reg_cache, reg_rn, &u_regval);
12531           while (register_bits)
12532             {
12533               if (register_bits & 0x00000001)
12534                 register_count++;
12535
12536               register_bits = register_bits >> 1;
12537             }
12538
12539           if (1 == op)
12540             {
12541               /* Start address calculation for LDMDB/LDMEA.  */
12542               start_address = u_regval;
12543             }
12544           else if (2 == op)
12545             {
12546               /* Start address calculation for LDMDB/LDMEA.  */
12547               start_address = u_regval - register_count * 4;
12548             }
12549
12550           thumb2_insn_r->mem_rec_count = register_count;
12551           while (register_count)
12552             {
12553               record_buf_mem[register_count * 2 - 1] = start_address;
12554               record_buf_mem[register_count * 2 - 2] = 4;
12555               start_address = start_address + 4;
12556               register_count--;
12557             }
12558           record_buf[0] = reg_rn;
12559           record_buf[1] = ARM_PS_REGNUM;
12560           thumb2_insn_r->reg_rec_count = 2;
12561         }
12562     }
12563
12564   MEM_ALLOC (thumb2_insn_r->arm_mems, thumb2_insn_r->mem_rec_count,
12565             record_buf_mem);
12566   REG_ALLOC (thumb2_insn_r->arm_regs, thumb2_insn_r->reg_rec_count,
12567             record_buf);
12568   return ARM_RECORD_SUCCESS;
12569 }
12570
12571 /* Handler for thumb2 load/store (dual/exclusive) and table branch
12572    instructions.  */
12573
12574 static int
12575 thumb2_record_ld_st_dual_ex_tbb (insn_decode_record *thumb2_insn_r)
12576 {
12577   struct regcache *reg_cache = thumb2_insn_r->regcache;
12578
12579   uint32_t reg_rd, reg_rn, offset_imm;
12580   uint32_t reg_dest1, reg_dest2;
12581   uint32_t address, offset_addr;
12582   uint32_t record_buf[8], record_buf_mem[8];
12583   uint32_t op1, op2, op3;
12584   LONGEST s_word;
12585
12586   ULONGEST u_regval[2];
12587
12588   op1 = bits (thumb2_insn_r->arm_insn, 23, 24);
12589   op2 = bits (thumb2_insn_r->arm_insn, 20, 21);
12590   op3 = bits (thumb2_insn_r->arm_insn, 4, 7);
12591
12592   if (bit (thumb2_insn_r->arm_insn, INSN_S_L_BIT_NUM))
12593     {
12594       if(!(1 == op1 && 1 == op2 && (0 == op3 || 1 == op3)))
12595         {
12596           reg_dest1 = bits (thumb2_insn_r->arm_insn, 12, 15);
12597           record_buf[0] = reg_dest1;
12598           record_buf[1] = ARM_PS_REGNUM;
12599           thumb2_insn_r->reg_rec_count = 2;
12600         }
12601
12602       if (3 == op2 || (op1 & 2) || (1 == op1 && 1 == op2 && 7 == op3))
12603         {
12604           reg_dest2 = bits (thumb2_insn_r->arm_insn, 8, 11);
12605           record_buf[2] = reg_dest2;
12606           thumb2_insn_r->reg_rec_count = 3;
12607         }
12608     }
12609   else
12610     {
12611       reg_rn = bits (thumb2_insn_r->arm_insn, 16, 19);
12612       regcache_raw_read_unsigned (reg_cache, reg_rn, &u_regval[0]);
12613
12614       if (0 == op1 && 0 == op2)
12615         {
12616           /* Handle STREX.  */
12617           offset_imm = bits (thumb2_insn_r->arm_insn, 0, 7);
12618           address = u_regval[0] + (offset_imm * 4);
12619           record_buf_mem[0] = 4;
12620           record_buf_mem[1] = address;
12621           thumb2_insn_r->mem_rec_count = 1;
12622           reg_rd = bits (thumb2_insn_r->arm_insn, 0, 3);
12623           record_buf[0] = reg_rd;
12624           thumb2_insn_r->reg_rec_count = 1;
12625         }
12626       else if (1 == op1 && 0 == op2)
12627         {
12628           reg_rd = bits (thumb2_insn_r->arm_insn, 0, 3);
12629           record_buf[0] = reg_rd;
12630           thumb2_insn_r->reg_rec_count = 1;
12631           address = u_regval[0];
12632           record_buf_mem[1] = address;
12633
12634           if (4 == op3)
12635             {
12636               /* Handle STREXB.  */
12637               record_buf_mem[0] = 1;
12638               thumb2_insn_r->mem_rec_count = 1;
12639             }
12640           else if (5 == op3)
12641             {
12642               /* Handle STREXH.  */
12643               record_buf_mem[0] = 2 ;
12644               thumb2_insn_r->mem_rec_count = 1;
12645             }
12646           else if (7 == op3)
12647             {
12648               /* Handle STREXD.  */
12649               address = u_regval[0];
12650               record_buf_mem[0] = 4;
12651               record_buf_mem[2] = 4;
12652               record_buf_mem[3] = address + 4;
12653               thumb2_insn_r->mem_rec_count = 2;
12654             }
12655         }
12656       else
12657         {
12658           offset_imm = bits (thumb2_insn_r->arm_insn, 0, 7);
12659
12660           if (bit (thumb2_insn_r->arm_insn, 24))
12661             {
12662               if (bit (thumb2_insn_r->arm_insn, 23))
12663                 offset_addr = u_regval[0] + (offset_imm * 4);
12664               else
12665                 offset_addr = u_regval[0] - (offset_imm * 4);
12666
12667               address = offset_addr;
12668             }
12669           else
12670             address = u_regval[0];
12671
12672           record_buf_mem[0] = 4;
12673           record_buf_mem[1] = address;
12674           record_buf_mem[2] = 4;
12675           record_buf_mem[3] = address + 4;
12676           thumb2_insn_r->mem_rec_count = 2;
12677           record_buf[0] = reg_rn;
12678           thumb2_insn_r->reg_rec_count = 1;
12679         }
12680     }
12681
12682   REG_ALLOC (thumb2_insn_r->arm_regs, thumb2_insn_r->reg_rec_count,
12683             record_buf);
12684   MEM_ALLOC (thumb2_insn_r->arm_mems, thumb2_insn_r->mem_rec_count,
12685             record_buf_mem);
12686   return ARM_RECORD_SUCCESS;
12687 }
12688
12689 /* Handler for thumb2 data processing (shift register and modified immediate)
12690    instructions.  */
12691
12692 static int
12693 thumb2_record_data_proc_sreg_mimm (insn_decode_record *thumb2_insn_r)
12694 {
12695   uint32_t reg_rd, op;
12696   uint32_t record_buf[8];
12697
12698   op = bits (thumb2_insn_r->arm_insn, 21, 24);
12699   reg_rd = bits (thumb2_insn_r->arm_insn, 8, 11);
12700
12701   if ((0 == op || 4 == op || 8 == op || 13 == op) && 15 == reg_rd)
12702     {
12703       record_buf[0] = ARM_PS_REGNUM;
12704       thumb2_insn_r->reg_rec_count = 1;
12705     }
12706   else
12707     {
12708       record_buf[0] = reg_rd;
12709       record_buf[1] = ARM_PS_REGNUM;
12710       thumb2_insn_r->reg_rec_count = 2;
12711     }
12712
12713   REG_ALLOC (thumb2_insn_r->arm_regs, thumb2_insn_r->reg_rec_count,
12714             record_buf);
12715   return ARM_RECORD_SUCCESS;
12716 }
12717
12718 /* Generic handler for thumb2 instructions which effect destination and PS
12719    registers.  */
12720
12721 static int
12722 thumb2_record_ps_dest_generic (insn_decode_record *thumb2_insn_r)
12723 {
12724   uint32_t reg_rd;
12725   uint32_t record_buf[8];
12726
12727   reg_rd = bits (thumb2_insn_r->arm_insn, 8, 11);
12728
12729   record_buf[0] = reg_rd;
12730   record_buf[1] = ARM_PS_REGNUM;
12731   thumb2_insn_r->reg_rec_count = 2;
12732
12733   REG_ALLOC (thumb2_insn_r->arm_regs, thumb2_insn_r->reg_rec_count,
12734             record_buf);
12735   return ARM_RECORD_SUCCESS;
12736 }
12737
12738 /* Handler for thumb2 branch and miscellaneous control instructions.  */
12739
12740 static int
12741 thumb2_record_branch_misc_cntrl (insn_decode_record *thumb2_insn_r)
12742 {
12743   uint32_t op, op1, op2;
12744   uint32_t record_buf[8];
12745
12746   op = bits (thumb2_insn_r->arm_insn, 20, 26);
12747   op1 = bits (thumb2_insn_r->arm_insn, 12, 14);
12748   op2 = bits (thumb2_insn_r->arm_insn, 8, 11);
12749
12750   /* Handle MSR insn.  */
12751   if (!(op1 & 0x2) && 0x38 == op)
12752     {
12753       if (!(op2 & 0x3))
12754         {
12755           /* CPSR is going to be changed.  */
12756           record_buf[0] = ARM_PS_REGNUM;
12757           thumb2_insn_r->reg_rec_count = 1;
12758         }
12759       else
12760         {
12761           arm_record_unsupported_insn(thumb2_insn_r);
12762           return -1;
12763         }
12764     }
12765   else if (4 == (op1 & 0x5) || 5 == (op1 & 0x5))
12766     {
12767       /* BLX.  */
12768       record_buf[0] = ARM_PS_REGNUM;
12769       record_buf[1] = ARM_LR_REGNUM;
12770       thumb2_insn_r->reg_rec_count = 2;
12771     }
12772
12773   REG_ALLOC (thumb2_insn_r->arm_regs, thumb2_insn_r->reg_rec_count,
12774             record_buf);
12775   return ARM_RECORD_SUCCESS;
12776 }
12777
12778 /* Handler for thumb2 store single data item instructions.  */
12779
12780 static int
12781 thumb2_record_str_single_data (insn_decode_record *thumb2_insn_r)
12782 {
12783   struct regcache *reg_cache = thumb2_insn_r->regcache;
12784
12785   uint32_t reg_rn, reg_rm, offset_imm, shift_imm;
12786   uint32_t address, offset_addr;
12787   uint32_t record_buf[8], record_buf_mem[8];
12788   uint32_t op1, op2;
12789
12790   ULONGEST u_regval[2];
12791
12792   op1 = bits (thumb2_insn_r->arm_insn, 21, 23);
12793   op2 = bits (thumb2_insn_r->arm_insn, 6, 11);
12794   reg_rn = bits (thumb2_insn_r->arm_insn, 16, 19);
12795   regcache_raw_read_unsigned (reg_cache, reg_rn, &u_regval[0]);
12796
12797   if (bit (thumb2_insn_r->arm_insn, 23))
12798     {
12799       /* T2 encoding.  */
12800       offset_imm = bits (thumb2_insn_r->arm_insn, 0, 11);
12801       offset_addr = u_regval[0] + offset_imm;
12802       address = offset_addr;
12803     }
12804   else
12805     {
12806       /* T3 encoding.  */
12807       if ((0 == op1 || 1 == op1 || 2 == op1) && !(op2 & 0x20))
12808         {
12809           /* Handle STRB (register).  */
12810           reg_rm = bits (thumb2_insn_r->arm_insn, 0, 3);
12811           regcache_raw_read_unsigned (reg_cache, reg_rm, &u_regval[1]);
12812           shift_imm = bits (thumb2_insn_r->arm_insn, 4, 5);
12813           offset_addr = u_regval[1] << shift_imm;
12814           address = u_regval[0] + offset_addr;
12815         }
12816       else
12817         {
12818           offset_imm = bits (thumb2_insn_r->arm_insn, 0, 7);
12819           if (bit (thumb2_insn_r->arm_insn, 10))
12820             {
12821               if (bit (thumb2_insn_r->arm_insn, 9))
12822                 offset_addr = u_regval[0] + offset_imm;
12823               else
12824                 offset_addr = u_regval[0] - offset_imm;
12825
12826               address = offset_addr;
12827             }
12828           else
12829             address = u_regval[0];
12830         }
12831     }
12832
12833   switch (op1)
12834     {
12835       /* Store byte instructions.  */
12836       case 4:
12837       case 0:
12838         record_buf_mem[0] = 1;
12839         break;
12840       /* Store half word instructions.  */
12841       case 1:
12842       case 5:
12843         record_buf_mem[0] = 2;
12844         break;
12845       /* Store word instructions.  */
12846       case 2:
12847       case 6:
12848         record_buf_mem[0] = 4;
12849         break;
12850
12851       default:
12852         gdb_assert_not_reached ("no decoding pattern found");
12853         break;
12854     }
12855
12856   record_buf_mem[1] = address;
12857   thumb2_insn_r->mem_rec_count = 1;
12858   record_buf[0] = reg_rn;
12859   thumb2_insn_r->reg_rec_count = 1;
12860
12861   REG_ALLOC (thumb2_insn_r->arm_regs, thumb2_insn_r->reg_rec_count,
12862             record_buf);
12863   MEM_ALLOC (thumb2_insn_r->arm_mems, thumb2_insn_r->mem_rec_count,
12864             record_buf_mem);
12865   return ARM_RECORD_SUCCESS;
12866 }
12867
12868 /* Handler for thumb2 load memory hints instructions.  */
12869
12870 static int
12871 thumb2_record_ld_mem_hints (insn_decode_record *thumb2_insn_r)
12872 {
12873   uint32_t record_buf[8];
12874   uint32_t reg_rt, reg_rn;
12875
12876   reg_rt = bits (thumb2_insn_r->arm_insn, 12, 15);
12877   reg_rn = bits (thumb2_insn_r->arm_insn, 16, 19);
12878
12879   if (ARM_PC_REGNUM != reg_rt)
12880     {
12881       record_buf[0] = reg_rt;
12882       record_buf[1] = reg_rn;
12883       record_buf[2] = ARM_PS_REGNUM;
12884       thumb2_insn_r->reg_rec_count = 3;
12885
12886       REG_ALLOC (thumb2_insn_r->arm_regs, thumb2_insn_r->reg_rec_count,
12887                 record_buf);
12888       return ARM_RECORD_SUCCESS;
12889     }
12890
12891   return ARM_RECORD_FAILURE;
12892 }
12893
12894 /* Handler for thumb2 load word instructions.  */
12895
12896 static int
12897 thumb2_record_ld_word (insn_decode_record *thumb2_insn_r)
12898 {
12899   uint32_t opcode1 = 0, opcode2 = 0;
12900   uint32_t record_buf[8];
12901
12902   record_buf[0] = bits (thumb2_insn_r->arm_insn, 12, 15);
12903   record_buf[1] = ARM_PS_REGNUM;
12904   thumb2_insn_r->reg_rec_count = 2;
12905
12906   REG_ALLOC (thumb2_insn_r->arm_regs, thumb2_insn_r->reg_rec_count,
12907             record_buf);
12908   return ARM_RECORD_SUCCESS;
12909 }
12910
12911 /* Handler for thumb2 long multiply, long multiply accumulate, and
12912    divide instructions.  */
12913
12914 static int
12915 thumb2_record_lmul_lmla_div (insn_decode_record *thumb2_insn_r)
12916 {
12917   uint32_t opcode1 = 0, opcode2 = 0;
12918   uint32_t record_buf[8];
12919   uint32_t reg_src1 = 0;
12920
12921   opcode1 = bits (thumb2_insn_r->arm_insn, 20, 22);
12922   opcode2 = bits (thumb2_insn_r->arm_insn, 4, 7);
12923
12924   if (0 == opcode1 || 2 == opcode1 || (opcode1 >= 4 && opcode1 <= 6))
12925     {
12926       /* Handle SMULL, UMULL, SMULAL.  */
12927       /* Handle SMLAL(S), SMULL(S), UMLAL(S), UMULL(S).  */
12928       record_buf[0] = bits (thumb2_insn_r->arm_insn, 16, 19);
12929       record_buf[1] = bits (thumb2_insn_r->arm_insn, 12, 15);
12930       record_buf[2] = ARM_PS_REGNUM;
12931       thumb2_insn_r->reg_rec_count = 3;
12932     }
12933   else if (1 == opcode1 || 3 == opcode2)
12934     {
12935       /* Handle SDIV and UDIV.  */
12936       record_buf[0] = bits (thumb2_insn_r->arm_insn, 16, 19);
12937       record_buf[1] = bits (thumb2_insn_r->arm_insn, 12, 15);
12938       record_buf[2] = ARM_PS_REGNUM;
12939       thumb2_insn_r->reg_rec_count = 3;
12940     }
12941   else
12942     return ARM_RECORD_FAILURE;
12943
12944   REG_ALLOC (thumb2_insn_r->arm_regs, thumb2_insn_r->reg_rec_count,
12945             record_buf);
12946   return ARM_RECORD_SUCCESS;
12947 }
12948
12949 /* Decodes thumb2 instruction type and invokes its record handler.  */
12950
12951 static unsigned int
12952 thumb2_record_decode_insn_handler (insn_decode_record *thumb2_insn_r)
12953 {
12954   uint32_t op, op1, op2;
12955
12956   op = bit (thumb2_insn_r->arm_insn, 15);
12957   op1 = bits (thumb2_insn_r->arm_insn, 27, 28);
12958   op2 = bits (thumb2_insn_r->arm_insn, 20, 26);
12959
12960   if (op1 == 0x01)
12961     {
12962       if (!(op2 & 0x64 ))
12963         {
12964           /* Load/store multiple instruction.  */
12965           return thumb2_record_ld_st_multiple (thumb2_insn_r);
12966         }
12967       else if (!((op2 & 0x64) ^ 0x04))
12968         {
12969           /* Load/store (dual/exclusive) and table branch instruction.  */
12970           return thumb2_record_ld_st_dual_ex_tbb (thumb2_insn_r);
12971         }
12972       else if (!((op2 & 0x20) ^ 0x20))
12973         {
12974           /* Data-processing (shifted register).  */
12975           return thumb2_record_data_proc_sreg_mimm (thumb2_insn_r);
12976         }
12977       else if (op2 & 0x40)
12978         {
12979           /* Co-processor instructions.  */
12980           arm_record_unsupported_insn (thumb2_insn_r);
12981         }
12982     }
12983   else if (op1 == 0x02)
12984     {
12985       if (op)
12986         {
12987           /* Branches and miscellaneous control instructions.  */
12988           return thumb2_record_branch_misc_cntrl (thumb2_insn_r);
12989         }
12990       else if (op2 & 0x20)
12991         {
12992           /* Data-processing (plain binary immediate) instruction.  */
12993           return thumb2_record_ps_dest_generic (thumb2_insn_r);
12994         }
12995       else
12996         {
12997           /* Data-processing (modified immediate).  */
12998           return thumb2_record_data_proc_sreg_mimm (thumb2_insn_r);
12999         }
13000     }
13001   else if (op1 == 0x03)
13002     {
13003       if (!(op2 & 0x71 ))
13004         {
13005           /* Store single data item.  */
13006           return thumb2_record_str_single_data (thumb2_insn_r);
13007         }
13008       else if (!((op2 & 0x71) ^ 0x10))
13009         {
13010           /* Advanced SIMD or structure load/store instructions.  */
13011           return arm_record_unsupported_insn (thumb2_insn_r);
13012         }
13013       else if (!((op2 & 0x67) ^ 0x01))
13014         {
13015           /* Load byte, memory hints instruction.  */
13016           return thumb2_record_ld_mem_hints (thumb2_insn_r);
13017         }
13018       else if (!((op2 & 0x67) ^ 0x03))
13019         {
13020           /* Load halfword, memory hints instruction.  */
13021           return thumb2_record_ld_mem_hints (thumb2_insn_r);
13022         }
13023       else if (!((op2 & 0x67) ^ 0x05))
13024         {
13025           /* Load word instruction.  */
13026           return thumb2_record_ld_word (thumb2_insn_r);
13027         }
13028       else if (!((op2 & 0x70) ^ 0x20))
13029         {
13030           /* Data-processing (register) instruction.  */
13031           return thumb2_record_ps_dest_generic (thumb2_insn_r);
13032         }
13033       else if (!((op2 & 0x78) ^ 0x30))
13034         {
13035           /* Multiply, multiply accumulate, abs diff instruction.  */
13036           return thumb2_record_ps_dest_generic (thumb2_insn_r);
13037         }
13038       else if (!((op2 & 0x78) ^ 0x38))
13039         {
13040           /* Long multiply, long multiply accumulate, and divide.  */
13041           return thumb2_record_lmul_lmla_div (thumb2_insn_r);
13042         }
13043       else if (op2 & 0x40)
13044         {
13045           /* Co-processor instructions.  */
13046           return arm_record_unsupported_insn (thumb2_insn_r);
13047         }
13048    }
13049
13050   return -1;
13051 }
13052
13053 /* Extracts arm/thumb/thumb2 insn depending on the size, and returns 0 on success 
13054 and positive val on fauilure.  */
13055
13056 static int
13057 extract_arm_insn (insn_decode_record *insn_record, uint32_t insn_size)
13058 {
13059   gdb_byte buf[insn_size];
13060
13061   memset (&buf[0], 0, insn_size);
13062   
13063   if (target_read_memory (insn_record->this_addr, &buf[0], insn_size))
13064     return 1;
13065   insn_record->arm_insn = (uint32_t) extract_unsigned_integer (&buf[0],
13066                            insn_size, 
13067                            gdbarch_byte_order (insn_record->gdbarch));
13068   return 0;
13069 }
13070
13071 typedef int (*sti_arm_hdl_fp_t) (insn_decode_record*);
13072
13073 /* Decode arm/thumb insn depending on condition cods and opcodes; and
13074    dispatch it.  */
13075
13076 static int
13077 decode_insn (insn_decode_record *arm_record, record_type_t record_type,
13078                 uint32_t insn_size)
13079 {
13080
13081   /* (Starting from numerical 0); bits 25, 26, 27 decodes type of arm instruction.  */
13082   static const sti_arm_hdl_fp_t const arm_handle_insn[8] =                    
13083   {
13084     arm_record_data_proc_misc_ld_str,   /* 000.  */
13085     arm_record_data_proc_imm,           /* 001.  */
13086     arm_record_ld_st_imm_offset,        /* 010.  */
13087     arm_record_ld_st_reg_offset,        /* 011.  */
13088     arm_record_ld_st_multiple,          /* 100.  */
13089     arm_record_b_bl,                    /* 101.  */
13090     arm_record_unsupported_insn,        /* 110.  */
13091     arm_record_coproc_data_proc         /* 111.  */
13092   };
13093
13094   /* (Starting from numerical 0); bits 13,14,15 decodes type of thumb instruction.  */
13095   static const sti_arm_hdl_fp_t const thumb_handle_insn[8] =
13096   { \
13097     thumb_record_shift_add_sub,        /* 000.  */
13098     thumb_record_add_sub_cmp_mov,      /* 001.  */
13099     thumb_record_ld_st_reg_offset,     /* 010.  */
13100     thumb_record_ld_st_imm_offset,     /* 011.  */
13101     thumb_record_ld_st_stack,          /* 100.  */
13102     thumb_record_misc,                 /* 101.  */
13103     thumb_record_ldm_stm_swi,          /* 110.  */
13104     thumb_record_branch                /* 111.  */
13105   };
13106
13107   uint32_t ret = 0;    /* return value: negative:failure   0:success.  */
13108   uint32_t insn_id = 0;
13109
13110   if (extract_arm_insn (arm_record, insn_size))
13111     {
13112       if (record_debug)
13113         {
13114           printf_unfiltered (_("Process record: error reading memory at "
13115                               "addr %s len = %d.\n"),
13116           paddress (arm_record->gdbarch, arm_record->this_addr), insn_size);        
13117         }
13118       return -1;
13119     }
13120   else if (ARM_RECORD == record_type)
13121     {
13122       arm_record->cond = bits (arm_record->arm_insn, 28, 31);
13123       insn_id = bits (arm_record->arm_insn, 25, 27);
13124       ret = arm_record_extension_space (arm_record);
13125       /* If this insn has fallen into extension space 
13126          then we need not decode it anymore.  */
13127       if (ret != -1 && !INSN_RECORDED(arm_record))
13128         {
13129           ret = arm_handle_insn[insn_id] (arm_record);
13130         }
13131     }
13132   else if (THUMB_RECORD == record_type)
13133     {
13134       /* As thumb does not have condition codes, we set negative.  */
13135       arm_record->cond = -1;
13136       insn_id = bits (arm_record->arm_insn, 13, 15);
13137       ret = thumb_handle_insn[insn_id] (arm_record);
13138     }
13139   else if (THUMB2_RECORD == record_type)
13140     {
13141       /* As thumb does not have condition codes, we set negative.  */
13142       arm_record->cond = -1;
13143
13144       /* Swap first half of 32bit thumb instruction with second half.  */
13145       arm_record->arm_insn
13146         = (arm_record->arm_insn >> 16) | (arm_record->arm_insn << 16);
13147
13148       insn_id = thumb2_record_decode_insn_handler (arm_record);
13149
13150       if (insn_id != ARM_RECORD_SUCCESS)
13151         {
13152           arm_record_unsupported_insn (arm_record);
13153           ret = -1;
13154         }
13155     }
13156   else
13157     {
13158       /* Throw assertion.  */
13159       gdb_assert_not_reached ("not a valid instruction, could not decode");
13160     }
13161
13162   return ret;
13163 }
13164
13165
13166 /* Cleans up local record registers and memory allocations.  */
13167
13168 static void 
13169 deallocate_reg_mem (insn_decode_record *record)
13170 {
13171   xfree (record->arm_regs);
13172   xfree (record->arm_mems);    
13173 }
13174
13175
13176 /* Parse the current instruction and record the values of the registers and    
13177    memory that will be changed in current instruction to record_arch_list".
13178    Return -1 if something is wrong.  */
13179
13180 int
13181 arm_process_record (struct gdbarch *gdbarch, struct regcache *regcache, 
13182                         CORE_ADDR insn_addr)
13183 {
13184
13185   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
13186   uint32_t no_of_rec = 0;
13187   uint32_t ret = 0;  /* return value: -1:record failure ;  0:success  */
13188   ULONGEST t_bit = 0, insn_id = 0;
13189
13190   ULONGEST u_regval = 0;
13191
13192   insn_decode_record arm_record;
13193
13194   memset (&arm_record, 0, sizeof (insn_decode_record));
13195   arm_record.regcache = regcache;
13196   arm_record.this_addr = insn_addr;
13197   arm_record.gdbarch = gdbarch;
13198
13199
13200   if (record_debug > 1)
13201     {
13202       fprintf_unfiltered (gdb_stdlog, "Process record: arm_process_record "
13203                                       "addr = %s\n",
13204       paddress (gdbarch, arm_record.this_addr));
13205     }
13206
13207   if (extract_arm_insn (&arm_record, 2))
13208     {
13209       if (record_debug)
13210         {
13211           printf_unfiltered (_("Process record: error reading memory at "
13212                              "addr %s len = %d.\n"),
13213                              paddress (arm_record.gdbarch, 
13214                              arm_record.this_addr), 2);
13215         }
13216       return -1;
13217     }
13218
13219   /* Check the insn, whether it is thumb or arm one.  */
13220
13221   t_bit = arm_psr_thumb_bit (arm_record.gdbarch);
13222   regcache_raw_read_unsigned (arm_record.regcache, ARM_PS_REGNUM, &u_regval);
13223
13224
13225   if (!(u_regval & t_bit))
13226     {
13227       /* We are decoding arm insn.  */
13228       ret = decode_insn (&arm_record, ARM_RECORD, ARM_INSN_SIZE_BYTES);
13229     }
13230   else
13231     {
13232       insn_id = bits (arm_record.arm_insn, 11, 15);
13233       /* is it thumb2 insn?  */
13234       if ((0x1D == insn_id) || (0x1E == insn_id) || (0x1F == insn_id))
13235         {
13236           ret = decode_insn (&arm_record, THUMB2_RECORD, 
13237                              THUMB2_INSN_SIZE_BYTES);
13238         }
13239       else
13240         {
13241           /* We are decoding thumb insn.  */
13242           ret = decode_insn (&arm_record, THUMB_RECORD, THUMB_INSN_SIZE_BYTES);
13243         }
13244     }
13245
13246   if (0 == ret)
13247     {
13248       /* Record registers.  */
13249       record_full_arch_list_add_reg (arm_record.regcache, ARM_PC_REGNUM);
13250       if (arm_record.arm_regs)
13251         {
13252           for (no_of_rec = 0; no_of_rec < arm_record.reg_rec_count; no_of_rec++)
13253             {
13254               if (record_full_arch_list_add_reg
13255                   (arm_record.regcache , arm_record.arm_regs[no_of_rec]))
13256               ret = -1;
13257             }
13258         }
13259       /* Record memories.  */
13260       if (arm_record.arm_mems)
13261         {
13262           for (no_of_rec = 0; no_of_rec < arm_record.mem_rec_count; no_of_rec++)
13263             {
13264               if (record_full_arch_list_add_mem
13265                   ((CORE_ADDR)arm_record.arm_mems[no_of_rec].addr,
13266                    arm_record.arm_mems[no_of_rec].len))
13267                 ret = -1;
13268             }
13269         }
13270
13271       if (record_full_arch_list_add_end ())
13272         ret = -1;
13273     }
13274
13275
13276   deallocate_reg_mem (&arm_record);
13277
13278   return ret;
13279 }
13280