* gdbarch.sh (target_gdbarch): New global variable.
[external/binutils.git] / gdb / arm-tdep.c
1 /* Common target dependent code for GDB on ARM systems.
2
3    Copyright (C) 1988, 1989, 1991, 1992, 1993, 1995, 1996, 1998, 1999, 2000,
4    2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008
5    Free Software Foundation, Inc.
6
7    This file is part of GDB.
8
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 3 of the License, or
12    (at your option) any later version.
13
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18
19    You should have received a copy of the GNU General Public License
20    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
21
22 #include <ctype.h>              /* XXX for isupper () */
23
24 #include "defs.h"
25 #include "frame.h"
26 #include "inferior.h"
27 #include "gdbcmd.h"
28 #include "gdbcore.h"
29 #include "gdb_string.h"
30 #include "dis-asm.h"            /* For register styles. */
31 #include "regcache.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 "target-descriptions.h"
44 #include "user-regs.h"
45
46 #include "arm-tdep.h"
47 #include "gdb/sim-arm.h"
48
49 #include "elf-bfd.h"
50 #include "coff/internal.h"
51 #include "elf/arm.h"
52
53 #include "gdb_assert.h"
54 #include "vec.h"
55
56 static int arm_debug;
57
58 /* Macros for setting and testing a bit in a minimal symbol that marks
59    it as Thumb function.  The MSB of the minimal symbol's "info" field
60    is used for this purpose.
61
62    MSYMBOL_SET_SPECIAL  Actually sets the "special" bit.
63    MSYMBOL_IS_SPECIAL   Tests the "special" bit in a minimal symbol.  */
64
65 #define MSYMBOL_SET_SPECIAL(msym)                                       \
66         MSYMBOL_INFO (msym) = (char *) (((long) MSYMBOL_INFO (msym))    \
67                                         | 0x80000000)
68
69 #define MSYMBOL_IS_SPECIAL(msym)                                \
70         (((long) MSYMBOL_INFO (msym) & 0x80000000) != 0)
71
72 /* Macros for swapping shorts and ints. In the unlikely case that anybody else needs these,
73    move to a general header. (A better solution might be to define memory read routines that
74    know whether they are reading code or data.)  */
75
76 #define SWAP_SHORT(x) \
77   ((((x) & 0xff00) >> 8) | (((x) & 0x00ff) << 8));
78
79 #define SWAP_INT(x) \
80   (  ((x & 0xff000000) >> 24) \
81    | ((x & 0x00ff0000) >> 8)  \
82    | ((x & 0x0000ff00) << 8)  \
83    | ((x & 0x000000ff) << 24))
84
85 /* Per-objfile data used for mapping symbols.  */
86 static const struct objfile_data *arm_objfile_data_key;
87
88 struct arm_mapping_symbol
89 {
90   bfd_vma value;
91   char type;
92 };
93 typedef struct arm_mapping_symbol arm_mapping_symbol_s;
94 DEF_VEC_O(arm_mapping_symbol_s);
95
96 struct arm_per_objfile
97 {
98   VEC(arm_mapping_symbol_s) **section_maps;
99 };
100
101 /* The list of available "set arm ..." and "show arm ..." commands.  */
102 static struct cmd_list_element *setarmcmdlist = NULL;
103 static struct cmd_list_element *showarmcmdlist = NULL;
104
105 /* The type of floating-point to use.  Keep this in sync with enum
106    arm_float_model, and the help string in _initialize_arm_tdep.  */
107 static const char *fp_model_strings[] =
108 {
109   "auto",
110   "softfpa",
111   "fpa",
112   "softvfp",
113   "vfp",
114   NULL
115 };
116
117 /* A variable that can be configured by the user.  */
118 static enum arm_float_model arm_fp_model = ARM_FLOAT_AUTO;
119 static const char *current_fp_model = "auto";
120
121 /* The ABI to use.  Keep this in sync with arm_abi_kind.  */
122 static const char *arm_abi_strings[] =
123 {
124   "auto",
125   "APCS",
126   "AAPCS",
127   NULL
128 };
129
130 /* A variable that can be configured by the user.  */
131 static enum arm_abi_kind arm_abi_global = ARM_ABI_AUTO;
132 static const char *arm_abi_string = "auto";
133
134 /* The execution mode to assume.  */
135 static const char *arm_mode_strings[] =
136   {
137     "auto",
138     "arm",
139     "thumb"
140   };
141
142 static const char *arm_fallback_mode_string = "auto";
143 static const char *arm_force_mode_string = "auto";
144
145 /* Number of different reg name sets (options).  */
146 static int num_disassembly_options;
147
148 /* The standard register names, and all the valid aliases for them.  */
149 static const struct
150 {
151   const char *name;
152   int regnum;
153 } arm_register_aliases[] = {
154   /* Basic register numbers.  */
155   { "r0", 0 },
156   { "r1", 1 },
157   { "r2", 2 },
158   { "r3", 3 },
159   { "r4", 4 },
160   { "r5", 5 },
161   { "r6", 6 },
162   { "r7", 7 },
163   { "r8", 8 },
164   { "r9", 9 },
165   { "r10", 10 },
166   { "r11", 11 },
167   { "r12", 12 },
168   { "r13", 13 },
169   { "r14", 14 },
170   { "r15", 15 },
171   /* Synonyms (argument and variable registers).  */
172   { "a1", 0 },
173   { "a2", 1 },
174   { "a3", 2 },
175   { "a4", 3 },
176   { "v1", 4 },
177   { "v2", 5 },
178   { "v3", 6 },
179   { "v4", 7 },
180   { "v5", 8 },
181   { "v6", 9 },
182   { "v7", 10 },
183   { "v8", 11 },
184   /* Other platform-specific names for r9.  */
185   { "sb", 9 },
186   { "tr", 9 },
187   /* Special names.  */
188   { "ip", 12 },
189   { "sp", 13 },
190   { "lr", 14 },
191   { "pc", 15 },
192   /* Names used by GCC (not listed in the ARM EABI).  */
193   { "sl", 10 },
194   { "fp", 11 },
195   /* A special name from the older ATPCS.  */
196   { "wr", 7 },
197 };
198
199 static const char *const arm_register_names[] =
200 {"r0",  "r1",  "r2",  "r3",     /*  0  1  2  3 */
201  "r4",  "r5",  "r6",  "r7",     /*  4  5  6  7 */
202  "r8",  "r9",  "r10", "r11",    /*  8  9 10 11 */
203  "r12", "sp",  "lr",  "pc",     /* 12 13 14 15 */
204  "f0",  "f1",  "f2",  "f3",     /* 16 17 18 19 */
205  "f4",  "f5",  "f6",  "f7",     /* 20 21 22 23 */
206  "fps", "cpsr" };               /* 24 25       */
207
208 /* Valid register name styles.  */
209 static const char **valid_disassembly_styles;
210
211 /* Disassembly style to use. Default to "std" register names.  */
212 static const char *disassembly_style;
213
214 /* This is used to keep the bfd arch_info in sync with the disassembly
215    style.  */
216 static void set_disassembly_style_sfunc(char *, int,
217                                          struct cmd_list_element *);
218 static void set_disassembly_style (void);
219
220 static void convert_from_extended (const struct floatformat *, const void *,
221                                    void *, int);
222 static void convert_to_extended (const struct floatformat *, void *,
223                                  const void *, int);
224
225 struct arm_prologue_cache
226 {
227   /* The stack pointer at the time this frame was created; i.e. the
228      caller's stack pointer when this function was called.  It is used
229      to identify this frame.  */
230   CORE_ADDR prev_sp;
231
232   /* The frame base for this frame is just prev_sp - frame size.
233      FRAMESIZE is the distance from the frame pointer to the
234      initial stack pointer.  */
235
236   int framesize;
237
238   /* The register used to hold the frame pointer for this frame.  */
239   int framereg;
240
241   /* Saved register offsets.  */
242   struct trad_frame_saved_reg *saved_regs;
243 };
244
245 /* Addresses for calling Thumb functions have the bit 0 set.
246    Here are some macros to test, set, or clear bit 0 of addresses.  */
247 #define IS_THUMB_ADDR(addr)     ((addr) & 1)
248 #define MAKE_THUMB_ADDR(addr)   ((addr) | 1)
249 #define UNMAKE_THUMB_ADDR(addr) ((addr) & ~1)
250
251 /* Set to true if the 32-bit mode is in use.  */
252
253 int arm_apcs_32 = 1;
254
255 /* Determine if FRAME is executing in Thumb mode.  */
256
257 static int
258 arm_frame_is_thumb (struct frame_info *frame)
259 {
260   CORE_ADDR cpsr;
261
262   /* Every ARM frame unwinder can unwind the T bit of the CPSR, either
263      directly (from a signal frame or dummy frame) or by interpreting
264      the saved LR (from a prologue or DWARF frame).  So consult it and
265      trust the unwinders.  */
266   cpsr = get_frame_register_unsigned (frame, ARM_PS_REGNUM);
267
268   return (cpsr & CPSR_T) != 0;
269 }
270
271 /* Callback for VEC_lower_bound.  */
272
273 static inline int
274 arm_compare_mapping_symbols (const struct arm_mapping_symbol *lhs,
275                              const struct arm_mapping_symbol *rhs)
276 {
277   return lhs->value < rhs->value;
278 }
279
280 /* Determine if the program counter specified in MEMADDR is in a Thumb
281    function.  This function should be called for addresses unrelated to
282    any executing frame; otherwise, prefer arm_frame_is_thumb.  */
283
284 static int
285 arm_pc_is_thumb (CORE_ADDR memaddr)
286 {
287   struct obj_section *sec;
288   struct minimal_symbol *sym;
289
290   /* If bit 0 of the address is set, assume this is a Thumb address.  */
291   if (IS_THUMB_ADDR (memaddr))
292     return 1;
293
294   /* If the user wants to override the symbol table, let him.  */
295   if (strcmp (arm_force_mode_string, "arm") == 0)
296     return 0;
297   if (strcmp (arm_force_mode_string, "thumb") == 0)
298     return 1;
299
300   /* If there are mapping symbols, consult them.  */
301   sec = find_pc_section (memaddr);
302   if (sec != NULL)
303     {
304       struct arm_per_objfile *data;
305       VEC(arm_mapping_symbol_s) *map;
306       struct arm_mapping_symbol map_key = { memaddr - obj_section_addr (sec),
307                                             0 };
308       unsigned int idx;
309
310       data = objfile_data (sec->objfile, arm_objfile_data_key);
311       if (data != NULL)
312         {
313           map = data->section_maps[sec->the_bfd_section->index];
314           if (!VEC_empty (arm_mapping_symbol_s, map))
315             {
316               struct arm_mapping_symbol *map_sym;
317
318               idx = VEC_lower_bound (arm_mapping_symbol_s, map, &map_key,
319                                      arm_compare_mapping_symbols);
320
321               /* VEC_lower_bound finds the earliest ordered insertion
322                  point.  If the following symbol starts at this exact
323                  address, we use that; otherwise, the preceding
324                  mapping symbol covers this address.  */
325               if (idx < VEC_length (arm_mapping_symbol_s, map))
326                 {
327                   map_sym = VEC_index (arm_mapping_symbol_s, map, idx);
328                   if (map_sym->value == map_key.value)
329                     return map_sym->type == 't';
330                 }
331
332               if (idx > 0)
333                 {
334                   map_sym = VEC_index (arm_mapping_symbol_s, map, idx - 1);
335                   return map_sym->type == 't';
336                 }
337             }
338         }
339     }
340
341   /* Thumb functions have a "special" bit set in minimal symbols.  */
342   sym = lookup_minimal_symbol_by_pc (memaddr);
343   if (sym)
344     return (MSYMBOL_IS_SPECIAL (sym));
345
346   /* If the user wants to override the fallback mode, let them.  */
347   if (strcmp (arm_fallback_mode_string, "arm") == 0)
348     return 0;
349   if (strcmp (arm_fallback_mode_string, "thumb") == 0)
350     return 1;
351
352   /* If we couldn't find any symbol, but we're talking to a running
353      target, then trust the current value of $cpsr.  This lets
354      "display/i $pc" always show the correct mode (though if there is
355      a symbol table we will not reach here, so it still may not be
356      displayed in the mode it will be executed).  */
357   if (target_has_registers)
358     return arm_frame_is_thumb (get_current_frame ());
359
360   /* Otherwise we're out of luck; we assume ARM.  */
361   return 0;
362 }
363
364 /* Remove useless bits from addresses in a running program.  */
365 static CORE_ADDR
366 arm_addr_bits_remove (CORE_ADDR val)
367 {
368   if (arm_apcs_32)
369     return UNMAKE_THUMB_ADDR (val);
370   else
371     return (val & 0x03fffffc);
372 }
373
374 /* When reading symbols, we need to zap the low bit of the address,
375    which may be set to 1 for Thumb functions.  */
376 static CORE_ADDR
377 arm_smash_text_address (CORE_ADDR val)
378 {
379   return val & ~1;
380 }
381
382 /* Analyze a Thumb prologue, looking for a recognizable stack frame
383    and frame pointer.  Scan until we encounter a store that could
384    clobber the stack frame unexpectedly, or an unknown instruction.  */
385
386 static CORE_ADDR
387 thumb_analyze_prologue (struct gdbarch *gdbarch,
388                         CORE_ADDR start, CORE_ADDR limit,
389                         struct arm_prologue_cache *cache)
390 {
391   int i;
392   pv_t regs[16];
393   struct pv_area *stack;
394   struct cleanup *back_to;
395   CORE_ADDR offset;
396
397   for (i = 0; i < 16; i++)
398     regs[i] = pv_register (i, 0);
399   stack = make_pv_area (ARM_SP_REGNUM);
400   back_to = make_cleanup_free_pv_area (stack);
401
402   while (start < limit)
403     {
404       unsigned short insn;
405
406       insn = read_memory_unsigned_integer (start, 2);
407
408       if (gdbarch_byte_order_for_code (gdbarch) != gdbarch_byte_order (gdbarch))
409         insn = SWAP_SHORT (insn);
410
411       if ((insn & 0xfe00) == 0xb400)            /* push { rlist } */
412         {
413           int regno;
414           int mask;
415
416           if (pv_area_store_would_trash (stack, regs[ARM_SP_REGNUM]))
417             break;
418
419           /* Bits 0-7 contain a mask for registers R0-R7.  Bit 8 says
420              whether to save LR (R14).  */
421           mask = (insn & 0xff) | ((insn & 0x100) << 6);
422
423           /* Calculate offsets of saved R0-R7 and LR.  */
424           for (regno = ARM_LR_REGNUM; regno >= 0; regno--)
425             if (mask & (1 << regno))
426               {
427                 regs[ARM_SP_REGNUM] = pv_add_constant (regs[ARM_SP_REGNUM],
428                                                        -4);
429                 pv_area_store (stack, regs[ARM_SP_REGNUM], 4, regs[regno]);
430               }
431         }
432       else if ((insn & 0xff00) == 0xb000)       /* add sp, #simm  OR  
433                                                    sub sp, #simm */
434         {
435           offset = (insn & 0x7f) << 2;          /* get scaled offset */
436           if (insn & 0x80)                      /* Check for SUB.  */
437             regs[ARM_SP_REGNUM] = pv_add_constant (regs[ARM_SP_REGNUM],
438                                                    -offset);
439           else
440             regs[ARM_SP_REGNUM] = pv_add_constant (regs[ARM_SP_REGNUM],
441                                                    offset);
442         }
443       else if ((insn & 0xff00) == 0xaf00)       /* add r7, sp, #imm */
444         regs[THUMB_FP_REGNUM] = pv_add_constant (regs[ARM_SP_REGNUM],
445                                                  (insn & 0xff) << 2);
446       else if ((insn & 0xff00) == 0x4600)       /* mov hi, lo or mov lo, hi */
447         {
448           int dst_reg = (insn & 0x7) + ((insn & 0x80) >> 4);
449           int src_reg = (insn & 0x78) >> 3;
450           regs[dst_reg] = regs[src_reg];
451         }
452       else if ((insn & 0xf800) == 0x9000)       /* str rd, [sp, #off] */
453         {
454           /* Handle stores to the stack.  Normally pushes are used,
455              but with GCC -mtpcs-frame, there may be other stores
456              in the prologue to create the frame.  */
457           int regno = (insn >> 8) & 0x7;
458           pv_t addr;
459
460           offset = (insn & 0xff) << 2;
461           addr = pv_add_constant (regs[ARM_SP_REGNUM], offset);
462
463           if (pv_area_store_would_trash (stack, addr))
464             break;
465
466           pv_area_store (stack, addr, 4, regs[regno]);
467         }
468       else
469         {
470           /* We don't know what this instruction is.  We're finished
471              scanning.  NOTE: Recognizing more safe-to-ignore
472              instructions here will improve support for optimized
473              code.  */
474           break;
475         }
476
477       start += 2;
478     }
479
480   if (cache == NULL)
481     {
482       do_cleanups (back_to);
483       return start;
484     }
485
486   if (pv_is_register (regs[ARM_FP_REGNUM], ARM_SP_REGNUM))
487     {
488       /* Frame pointer is fp.  Frame size is constant.  */
489       cache->framereg = ARM_FP_REGNUM;
490       cache->framesize = -regs[ARM_FP_REGNUM].k;
491     }
492   else if (pv_is_register (regs[THUMB_FP_REGNUM], ARM_SP_REGNUM))
493     {
494       /* Frame pointer is r7.  Frame size is constant.  */
495       cache->framereg = THUMB_FP_REGNUM;
496       cache->framesize = -regs[THUMB_FP_REGNUM].k;
497     }
498   else if (pv_is_register (regs[ARM_SP_REGNUM], ARM_SP_REGNUM))
499     {
500       /* Try the stack pointer... this is a bit desperate.  */
501       cache->framereg = ARM_SP_REGNUM;
502       cache->framesize = -regs[ARM_SP_REGNUM].k;
503     }
504   else
505     {
506       /* We're just out of luck.  We don't know where the frame is.  */
507       cache->framereg = -1;
508       cache->framesize = 0;
509     }
510
511   for (i = 0; i < 16; i++)
512     if (pv_area_find_reg (stack, gdbarch, i, &offset))
513       cache->saved_regs[i].addr = offset;
514
515   do_cleanups (back_to);
516   return start;
517 }
518
519 /* Advance the PC across any function entry prologue instructions to
520    reach some "real" code.
521
522    The APCS (ARM Procedure Call Standard) defines the following
523    prologue:
524
525    mov          ip, sp
526    [stmfd       sp!, {a1,a2,a3,a4}]
527    stmfd        sp!, {...,fp,ip,lr,pc}
528    [stfe        f7, [sp, #-12]!]
529    [stfe        f6, [sp, #-12]!]
530    [stfe        f5, [sp, #-12]!]
531    [stfe        f4, [sp, #-12]!]
532    sub fp, ip, #nn @@ nn == 20 or 4 depending on second insn */
533
534 static CORE_ADDR
535 arm_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR pc)
536 {
537   unsigned long inst;
538   CORE_ADDR skip_pc;
539   CORE_ADDR func_addr, func_end = 0;
540   char *func_name;
541   struct symtab_and_line sal;
542
543   /* If we're in a dummy frame, don't even try to skip the prologue.  */
544   if (deprecated_pc_in_call_dummy (pc))
545     return pc;
546
547   /* See what the symbol table says.  */
548
549   if (find_pc_partial_function (pc, &func_name, &func_addr, &func_end))
550     {
551       struct symbol *sym;
552
553       /* Found a function.  */
554       sym = lookup_symbol (func_name, NULL, VAR_DOMAIN, NULL);
555       if (sym && SYMBOL_LANGUAGE (sym) != language_asm)
556         {
557           /* Don't use this trick for assembly source files.  */
558           sal = find_pc_line (func_addr, 0);
559           if ((sal.line != 0) && (sal.end < func_end))
560             return sal.end;
561         }
562     }
563
564   /* Can't find the prologue end in the symbol table, try it the hard way
565      by disassembling the instructions.  */
566
567   /* Like arm_scan_prologue, stop no later than pc + 64. */
568   if (func_end == 0 || func_end > pc + 64)
569     func_end = pc + 64;
570
571   /* Check if this is Thumb code.  */
572   if (arm_pc_is_thumb (pc))
573     return thumb_analyze_prologue (gdbarch, pc, func_end, NULL);
574
575   for (skip_pc = pc; skip_pc < func_end; skip_pc += 4)
576     {
577       inst = read_memory_unsigned_integer (skip_pc, 4);
578
579       if (gdbarch_byte_order_for_code (gdbarch) != gdbarch_byte_order (gdbarch))
580         inst = SWAP_INT (inst);
581
582       /* "mov ip, sp" is no longer a required part of the prologue.  */
583       if (inst == 0xe1a0c00d)                   /* mov ip, sp */
584         continue;
585
586       if ((inst & 0xfffff000) == 0xe28dc000)    /* add ip, sp #n */
587         continue;
588
589       if ((inst & 0xfffff000) == 0xe24dc000)    /* sub ip, sp #n */
590         continue;
591
592       /* Some prologues begin with "str lr, [sp, #-4]!".  */
593       if (inst == 0xe52de004)                   /* str lr, [sp, #-4]! */
594         continue;
595
596       if ((inst & 0xfffffff0) == 0xe92d0000)    /* stmfd sp!,{a1,a2,a3,a4} */
597         continue;
598
599       if ((inst & 0xfffff800) == 0xe92dd800)    /* stmfd sp!,{fp,ip,lr,pc} */
600         continue;
601
602       /* Any insns after this point may float into the code, if it makes
603          for better instruction scheduling, so we skip them only if we
604          find them, but still consider the function to be frame-ful.  */
605
606       /* We may have either one sfmfd instruction here, or several stfe
607          insns, depending on the version of floating point code we
608          support.  */
609       if ((inst & 0xffbf0fff) == 0xec2d0200)    /* sfmfd fn, <cnt>, [sp]! */
610         continue;
611
612       if ((inst & 0xffff8fff) == 0xed6d0103)    /* stfe fn, [sp, #-12]! */
613         continue;
614
615       if ((inst & 0xfffff000) == 0xe24cb000)    /* sub fp, ip, #nn */
616         continue;
617
618       if ((inst & 0xfffff000) == 0xe24dd000)    /* sub sp, sp, #nn */
619         continue;
620
621       if ((inst & 0xffffc000) == 0xe54b0000 ||  /* strb r(0123),[r11,#-nn] */
622           (inst & 0xffffc0f0) == 0xe14b00b0 ||  /* strh r(0123),[r11,#-nn] */
623           (inst & 0xffffc000) == 0xe50b0000)    /* str  r(0123),[r11,#-nn] */
624         continue;
625
626       if ((inst & 0xffffc000) == 0xe5cd0000 ||  /* strb r(0123),[sp,#nn] */
627           (inst & 0xffffc0f0) == 0xe1cd00b0 ||  /* strh r(0123),[sp,#nn] */
628           (inst & 0xffffc000) == 0xe58d0000)    /* str  r(0123),[sp,#nn] */
629         continue;
630
631       /* Un-recognized instruction; stop scanning.  */
632       break;
633     }
634
635   return skip_pc;               /* End of prologue */
636 }
637
638 /* *INDENT-OFF* */
639 /* Function: thumb_scan_prologue (helper function for arm_scan_prologue)
640    This function decodes a Thumb function prologue to determine:
641      1) the size of the stack frame
642      2) which registers are saved on it
643      3) the offsets of saved regs
644      4) the offset from the stack pointer to the frame pointer
645
646    A typical Thumb function prologue would create this stack frame
647    (offsets relative to FP)
648      old SP ->  24  stack parameters
649                 20  LR
650                 16  R7
651      R7 ->       0  local variables (16 bytes)
652      SP ->     -12  additional stack space (12 bytes)
653    The frame size would thus be 36 bytes, and the frame offset would be
654    12 bytes.  The frame register is R7. 
655    
656    The comments for thumb_skip_prolog() describe the algorithm we use
657    to detect the end of the prolog.  */
658 /* *INDENT-ON* */
659
660 static void
661 thumb_scan_prologue (struct gdbarch *gdbarch, CORE_ADDR prev_pc,
662                      CORE_ADDR block_addr, struct arm_prologue_cache *cache)
663 {
664   CORE_ADDR prologue_start;
665   CORE_ADDR prologue_end;
666   CORE_ADDR current_pc;
667
668   if (find_pc_partial_function (block_addr, NULL, &prologue_start,
669                                 &prologue_end))
670     {
671       struct symtab_and_line sal = find_pc_line (prologue_start, 0);
672
673       if (sal.line == 0)                /* no line info, use current PC  */
674         prologue_end = prev_pc;
675       else if (sal.end < prologue_end)  /* next line begins after fn end */
676         prologue_end = sal.end;         /* (probably means no prologue)  */
677     }
678   else
679     /* We're in the boondocks: we have no idea where the start of the
680        function is.  */
681     return;
682
683   prologue_end = min (prologue_end, prev_pc);
684
685   thumb_analyze_prologue (gdbarch, prologue_start, prologue_end, cache);
686 }
687
688 /* This function decodes an ARM function prologue to determine:
689    1) the size of the stack frame
690    2) which registers are saved on it
691    3) the offsets of saved regs
692    4) the offset from the stack pointer to the frame pointer
693    This information is stored in the "extra" fields of the frame_info.
694
695    There are two basic forms for the ARM prologue.  The fixed argument
696    function call will look like:
697
698    mov    ip, sp
699    stmfd  sp!, {fp, ip, lr, pc}
700    sub    fp, ip, #4
701    [sub sp, sp, #4]
702
703    Which would create this stack frame (offsets relative to FP):
704    IP ->   4    (caller's stack)
705    FP ->   0    PC (points to address of stmfd instruction + 8 in callee)
706    -4   LR (return address in caller)
707    -8   IP (copy of caller's SP)
708    -12  FP (caller's FP)
709    SP -> -28    Local variables
710
711    The frame size would thus be 32 bytes, and the frame offset would be
712    28 bytes.  The stmfd call can also save any of the vN registers it
713    plans to use, which increases the frame size accordingly.
714
715    Note: The stored PC is 8 off of the STMFD instruction that stored it
716    because the ARM Store instructions always store PC + 8 when you read
717    the PC register.
718
719    A variable argument function call will look like:
720
721    mov    ip, sp
722    stmfd  sp!, {a1, a2, a3, a4}
723    stmfd  sp!, {fp, ip, lr, pc}
724    sub    fp, ip, #20
725
726    Which would create this stack frame (offsets relative to FP):
727    IP ->  20    (caller's stack)
728    16  A4
729    12  A3
730    8  A2
731    4  A1
732    FP ->   0    PC (points to address of stmfd instruction + 8 in callee)
733    -4   LR (return address in caller)
734    -8   IP (copy of caller's SP)
735    -12  FP (caller's FP)
736    SP -> -28    Local variables
737
738    The frame size would thus be 48 bytes, and the frame offset would be
739    28 bytes.
740
741    There is another potential complication, which is that the optimizer
742    will try to separate the store of fp in the "stmfd" instruction from
743    the "sub fp, ip, #NN" instruction.  Almost anything can be there, so
744    we just key on the stmfd, and then scan for the "sub fp, ip, #NN"...
745
746    Also, note, the original version of the ARM toolchain claimed that there
747    should be an
748
749    instruction at the end of the prologue.  I have never seen GCC produce
750    this, and the ARM docs don't mention it.  We still test for it below in
751    case it happens...
752
753  */
754
755 static void
756 arm_scan_prologue (struct frame_info *this_frame,
757                    struct arm_prologue_cache *cache)
758 {
759   struct gdbarch *gdbarch = get_frame_arch (this_frame);
760   int regno;
761   CORE_ADDR prologue_start, prologue_end, current_pc;
762   CORE_ADDR prev_pc = get_frame_pc (this_frame);
763   CORE_ADDR block_addr = get_frame_address_in_block (this_frame);
764   pv_t regs[ARM_FPS_REGNUM];
765   struct pv_area *stack;
766   struct cleanup *back_to;
767   CORE_ADDR offset;
768
769   /* Assume there is no frame until proven otherwise.  */
770   cache->framereg = ARM_SP_REGNUM;
771   cache->framesize = 0;
772
773   /* Check for Thumb prologue.  */
774   if (arm_frame_is_thumb (this_frame))
775     {
776       thumb_scan_prologue (gdbarch, prev_pc, block_addr, cache);
777       return;
778     }
779
780   /* Find the function prologue.  If we can't find the function in
781      the symbol table, peek in the stack frame to find the PC.  */
782   if (find_pc_partial_function (block_addr, NULL, &prologue_start,
783                                 &prologue_end))
784     {
785       /* One way to find the end of the prologue (which works well
786          for unoptimized code) is to do the following:
787
788             struct symtab_and_line sal = find_pc_line (prologue_start, 0);
789
790             if (sal.line == 0)
791               prologue_end = prev_pc;
792             else if (sal.end < prologue_end)
793               prologue_end = sal.end;
794
795          This mechanism is very accurate so long as the optimizer
796          doesn't move any instructions from the function body into the
797          prologue.  If this happens, sal.end will be the last
798          instruction in the first hunk of prologue code just before
799          the first instruction that the scheduler has moved from
800          the body to the prologue.
801
802          In order to make sure that we scan all of the prologue
803          instructions, we use a slightly less accurate mechanism which
804          may scan more than necessary.  To help compensate for this
805          lack of accuracy, the prologue scanning loop below contains
806          several clauses which'll cause the loop to terminate early if
807          an implausible prologue instruction is encountered.  
808          
809          The expression
810          
811               prologue_start + 64
812             
813          is a suitable endpoint since it accounts for the largest
814          possible prologue plus up to five instructions inserted by
815          the scheduler.  */
816          
817       if (prologue_end > prologue_start + 64)
818         {
819           prologue_end = prologue_start + 64;   /* See above.  */
820         }
821     }
822   else
823     {
824       /* We have no symbol information.  Our only option is to assume this
825          function has a standard stack frame and the normal frame register.
826          Then, we can find the value of our frame pointer on entrance to
827          the callee (or at the present moment if this is the innermost frame).
828          The value stored there should be the address of the stmfd + 8.  */
829       CORE_ADDR frame_loc;
830       LONGEST return_value;
831
832       frame_loc = get_frame_register_unsigned (this_frame, ARM_FP_REGNUM);
833       if (!safe_read_memory_integer (frame_loc, 4, &return_value))
834         return;
835       else
836         {
837           prologue_start = gdbarch_addr_bits_remove 
838                              (gdbarch, return_value) - 8;
839           prologue_end = prologue_start + 64;   /* See above.  */
840         }
841     }
842
843   if (prev_pc < prologue_end)
844     prologue_end = prev_pc;
845
846   /* Now search the prologue looking for instructions that set up the
847      frame pointer, adjust the stack pointer, and save registers.
848
849      Be careful, however, and if it doesn't look like a prologue,
850      don't try to scan it.  If, for instance, a frameless function
851      begins with stmfd sp!, then we will tell ourselves there is
852      a frame, which will confuse stack traceback, as well as "finish" 
853      and other operations that rely on a knowledge of the stack
854      traceback.
855
856      In the APCS, the prologue should start with  "mov ip, sp" so
857      if we don't see this as the first insn, we will stop.  
858
859      [Note: This doesn't seem to be true any longer, so it's now an
860      optional part of the prologue.  - Kevin Buettner, 2001-11-20]
861
862      [Note further: The "mov ip,sp" only seems to be missing in
863      frameless functions at optimization level "-O2" or above,
864      in which case it is often (but not always) replaced by
865      "str lr, [sp, #-4]!".  - Michael Snyder, 2002-04-23]  */
866
867   for (regno = 0; regno < ARM_FPS_REGNUM; regno++)
868     regs[regno] = pv_register (regno, 0);
869   stack = make_pv_area (ARM_SP_REGNUM);
870   back_to = make_cleanup_free_pv_area (stack);
871
872   for (current_pc = prologue_start;
873        current_pc < prologue_end;
874        current_pc += 4)
875     {
876       unsigned int insn = read_memory_unsigned_integer (current_pc, 4);
877
878       if (gdbarch_byte_order_for_code (gdbarch) != gdbarch_byte_order (gdbarch))
879         insn = SWAP_INT (insn);
880
881       if (insn == 0xe1a0c00d)           /* mov ip, sp */
882         {
883           regs[ARM_IP_REGNUM] = regs[ARM_SP_REGNUM];
884           continue;
885         }
886       else if ((insn & 0xfffff000) == 0xe28dc000) /* add ip, sp #n */
887         {
888           unsigned imm = insn & 0xff;                   /* immediate value */
889           unsigned rot = (insn & 0xf00) >> 7;           /* rotate amount */
890           imm = (imm >> rot) | (imm << (32 - rot));
891           regs[ARM_IP_REGNUM] = pv_add_constant (regs[ARM_SP_REGNUM], imm);
892           continue;
893         }
894       else if ((insn & 0xfffff000) == 0xe24dc000) /* sub ip, sp #n */
895         {
896           unsigned imm = insn & 0xff;                   /* immediate value */
897           unsigned rot = (insn & 0xf00) >> 7;           /* rotate amount */
898           imm = (imm >> rot) | (imm << (32 - rot));
899           regs[ARM_IP_REGNUM] = pv_add_constant (regs[ARM_SP_REGNUM], -imm);
900           continue;
901         }
902       else if (insn == 0xe52de004)      /* str lr, [sp, #-4]! */
903         {
904           if (pv_area_store_would_trash (stack, regs[ARM_SP_REGNUM]))
905             break;
906           regs[ARM_SP_REGNUM] = pv_add_constant (regs[ARM_SP_REGNUM], -4);
907           pv_area_store (stack, regs[ARM_SP_REGNUM], 4, regs[ARM_LR_REGNUM]);
908           continue;
909         }
910       else if ((insn & 0xffff0000) == 0xe92d0000)
911         /* stmfd sp!, {..., fp, ip, lr, pc}
912            or
913            stmfd sp!, {a1, a2, a3, a4}  */
914         {
915           int mask = insn & 0xffff;
916
917           if (pv_area_store_would_trash (stack, regs[ARM_SP_REGNUM]))
918             break;
919
920           /* Calculate offsets of saved registers.  */
921           for (regno = ARM_PC_REGNUM; regno >= 0; regno--)
922             if (mask & (1 << regno))
923               {
924                 regs[ARM_SP_REGNUM] = pv_add_constant (regs[ARM_SP_REGNUM], -4);
925                 pv_area_store (stack, regs[ARM_SP_REGNUM], 4, regs[regno]);
926               }
927         }
928       else if ((insn & 0xffffc000) == 0xe54b0000 ||     /* strb rx,[r11,#-n] */
929                (insn & 0xffffc0f0) == 0xe14b00b0 ||     /* strh rx,[r11,#-n] */
930                (insn & 0xffffc000) == 0xe50b0000)       /* str  rx,[r11,#-n] */
931         {
932           /* No need to add this to saved_regs -- it's just an arg reg.  */
933           continue;
934         }
935       else if ((insn & 0xffffc000) == 0xe5cd0000 ||     /* strb rx,[sp,#n] */
936                (insn & 0xffffc0f0) == 0xe1cd00b0 ||     /* strh rx,[sp,#n] */
937                (insn & 0xffffc000) == 0xe58d0000)       /* str  rx,[sp,#n] */
938         {
939           /* No need to add this to saved_regs -- it's just an arg reg.  */
940           continue;
941         }
942       else if ((insn & 0xfffff000) == 0xe24cb000)       /* sub fp, ip #n */
943         {
944           unsigned imm = insn & 0xff;                   /* immediate value */
945           unsigned rot = (insn & 0xf00) >> 7;           /* rotate amount */
946           imm = (imm >> rot) | (imm << (32 - rot));
947           regs[ARM_FP_REGNUM] = pv_add_constant (regs[ARM_IP_REGNUM], -imm);
948         }
949       else if ((insn & 0xfffff000) == 0xe24dd000)       /* sub sp, sp #n */
950         {
951           unsigned imm = insn & 0xff;                   /* immediate value */
952           unsigned rot = (insn & 0xf00) >> 7;           /* rotate amount */
953           imm = (imm >> rot) | (imm << (32 - rot));
954           regs[ARM_SP_REGNUM] = pv_add_constant (regs[ARM_SP_REGNUM], -imm);
955         }
956       else if ((insn & 0xffff7fff) == 0xed6d0103        /* stfe f?, [sp, -#c]! */
957                && gdbarch_tdep (gdbarch)->have_fpa_registers)
958         {
959           if (pv_area_store_would_trash (stack, regs[ARM_SP_REGNUM]))
960             break;
961
962           regs[ARM_SP_REGNUM] = pv_add_constant (regs[ARM_SP_REGNUM], -12);
963           regno = ARM_F0_REGNUM + ((insn >> 12) & 0x07);
964           pv_area_store (stack, regs[ARM_SP_REGNUM], 12, regs[regno]);
965         }
966       else if ((insn & 0xffbf0fff) == 0xec2d0200        /* sfmfd f0, 4, [sp!] */
967                && gdbarch_tdep (gdbarch)->have_fpa_registers)
968         {
969           int n_saved_fp_regs;
970           unsigned int fp_start_reg, fp_bound_reg;
971
972           if (pv_area_store_would_trash (stack, regs[ARM_SP_REGNUM]))
973             break;
974
975           if ((insn & 0x800) == 0x800)          /* N0 is set */
976             {
977               if ((insn & 0x40000) == 0x40000)  /* N1 is set */
978                 n_saved_fp_regs = 3;
979               else
980                 n_saved_fp_regs = 1;
981             }
982           else
983             {
984               if ((insn & 0x40000) == 0x40000)  /* N1 is set */
985                 n_saved_fp_regs = 2;
986               else
987                 n_saved_fp_regs = 4;
988             }
989
990           fp_start_reg = ARM_F0_REGNUM + ((insn >> 12) & 0x7);
991           fp_bound_reg = fp_start_reg + n_saved_fp_regs;
992           for (; fp_start_reg < fp_bound_reg; fp_start_reg++)
993             {
994               regs[ARM_SP_REGNUM] = pv_add_constant (regs[ARM_SP_REGNUM], -12);
995               pv_area_store (stack, regs[ARM_SP_REGNUM], 12,
996                              regs[fp_start_reg++]);
997             }
998         }
999       else if ((insn & 0xf0000000) != 0xe0000000)
1000         break;                  /* Condition not true, exit early */
1001       else if ((insn & 0xfe200000) == 0xe8200000)       /* ldm? */
1002         break;                  /* Don't scan past a block load */
1003       else
1004         /* The optimizer might shove anything into the prologue,
1005            so we just skip what we don't recognize.  */
1006         continue;
1007     }
1008
1009   /* The frame size is just the distance from the frame register
1010      to the original stack pointer.  */
1011   if (pv_is_register (regs[ARM_FP_REGNUM], ARM_SP_REGNUM))
1012     {
1013       /* Frame pointer is fp.  */
1014       cache->framereg = ARM_FP_REGNUM;
1015       cache->framesize = -regs[ARM_FP_REGNUM].k;
1016     }
1017   else if (pv_is_register (regs[ARM_SP_REGNUM], ARM_SP_REGNUM))
1018     {
1019       /* Try the stack pointer... this is a bit desperate.  */
1020       cache->framereg = ARM_SP_REGNUM;
1021       cache->framesize = -regs[ARM_SP_REGNUM].k;
1022     }
1023   else
1024     {
1025       /* We're just out of luck.  We don't know where the frame is.  */
1026       cache->framereg = -1;
1027       cache->framesize = 0;
1028     }
1029
1030   for (regno = 0; regno < ARM_FPS_REGNUM; regno++)
1031     if (pv_area_find_reg (stack, gdbarch, regno, &offset))
1032       cache->saved_regs[regno].addr = offset;
1033
1034   do_cleanups (back_to);
1035 }
1036
1037 static struct arm_prologue_cache *
1038 arm_make_prologue_cache (struct frame_info *this_frame)
1039 {
1040   int reg;
1041   struct arm_prologue_cache *cache;
1042   CORE_ADDR unwound_fp;
1043
1044   cache = FRAME_OBSTACK_ZALLOC (struct arm_prologue_cache);
1045   cache->saved_regs = trad_frame_alloc_saved_regs (this_frame);
1046
1047   arm_scan_prologue (this_frame, cache);
1048
1049   unwound_fp = get_frame_register_unsigned (this_frame, cache->framereg);
1050   if (unwound_fp == 0)
1051     return cache;
1052
1053   cache->prev_sp = unwound_fp + cache->framesize;
1054
1055   /* Calculate actual addresses of saved registers using offsets
1056      determined by arm_scan_prologue.  */
1057   for (reg = 0; reg < gdbarch_num_regs (get_frame_arch (this_frame)); reg++)
1058     if (trad_frame_addr_p (cache->saved_regs, reg))
1059       cache->saved_regs[reg].addr += cache->prev_sp;
1060
1061   return cache;
1062 }
1063
1064 /* Our frame ID for a normal frame is the current function's starting PC
1065    and the caller's SP when we were called.  */
1066
1067 static void
1068 arm_prologue_this_id (struct frame_info *this_frame,
1069                       void **this_cache,
1070                       struct frame_id *this_id)
1071 {
1072   struct arm_prologue_cache *cache;
1073   struct frame_id id;
1074   CORE_ADDR pc, func;
1075
1076   if (*this_cache == NULL)
1077     *this_cache = arm_make_prologue_cache (this_frame);
1078   cache = *this_cache;
1079
1080   /* This is meant to halt the backtrace at "_start".  */
1081   pc = get_frame_pc (this_frame);
1082   if (pc <= gdbarch_tdep (get_frame_arch (this_frame))->lowest_pc)
1083     return;
1084
1085   /* If we've hit a wall, stop.  */
1086   if (cache->prev_sp == 0)
1087     return;
1088
1089   func = get_frame_func (this_frame);
1090   id = frame_id_build (cache->prev_sp, func);
1091   *this_id = id;
1092 }
1093
1094 static struct value *
1095 arm_prologue_prev_register (struct frame_info *this_frame,
1096                             void **this_cache,
1097                             int prev_regnum)
1098 {
1099   struct arm_prologue_cache *cache;
1100
1101   if (*this_cache == NULL)
1102     *this_cache = arm_make_prologue_cache (this_frame);
1103   cache = *this_cache;
1104
1105   /* If we are asked to unwind the PC, then we need to return the LR
1106      instead.  The prologue may save PC, but it will point into this
1107      frame's prologue, not the next frame's resume location.  Also
1108      strip the saved T bit.  A valid LR may have the low bit set, but
1109      a valid PC never does.  */
1110   if (prev_regnum == ARM_PC_REGNUM)
1111     {
1112       CORE_ADDR lr;
1113
1114       lr = frame_unwind_register_unsigned (this_frame, ARM_LR_REGNUM);
1115       return frame_unwind_got_constant (this_frame, prev_regnum,
1116                                         arm_addr_bits_remove (lr));
1117     }
1118
1119   /* SP is generally not saved to the stack, but this frame is
1120      identified by the next frame's stack pointer at the time of the call.
1121      The value was already reconstructed into PREV_SP.  */
1122   if (prev_regnum == ARM_SP_REGNUM)
1123     return frame_unwind_got_constant (this_frame, prev_regnum, cache->prev_sp);
1124
1125   /* The CPSR may have been changed by the call instruction and by the
1126      called function.  The only bit we can reconstruct is the T bit,
1127      by checking the low bit of LR as of the call.  This is a reliable
1128      indicator of Thumb-ness except for some ARM v4T pre-interworking
1129      Thumb code, which could get away with a clear low bit as long as
1130      the called function did not use bx.  Guess that all other
1131      bits are unchanged; the condition flags are presumably lost,
1132      but the processor status is likely valid.  */
1133   if (prev_regnum == ARM_PS_REGNUM)
1134     {
1135       CORE_ADDR lr, cpsr;
1136
1137       cpsr = get_frame_register_unsigned (this_frame, prev_regnum);
1138       lr = frame_unwind_register_unsigned (this_frame, ARM_LR_REGNUM);
1139       if (IS_THUMB_ADDR (lr))
1140         cpsr |= CPSR_T;
1141       else
1142         cpsr &= ~CPSR_T;
1143       return frame_unwind_got_constant (this_frame, prev_regnum, cpsr);
1144     }
1145
1146   return trad_frame_get_prev_register (this_frame, cache->saved_regs,
1147                                        prev_regnum);
1148 }
1149
1150 struct frame_unwind arm_prologue_unwind = {
1151   NORMAL_FRAME,
1152   arm_prologue_this_id,
1153   arm_prologue_prev_register,
1154   NULL,
1155   default_frame_sniffer
1156 };
1157
1158 static struct arm_prologue_cache *
1159 arm_make_stub_cache (struct frame_info *this_frame)
1160 {
1161   int reg;
1162   struct arm_prologue_cache *cache;
1163   CORE_ADDR unwound_fp;
1164
1165   cache = FRAME_OBSTACK_ZALLOC (struct arm_prologue_cache);
1166   cache->saved_regs = trad_frame_alloc_saved_regs (this_frame);
1167
1168   cache->prev_sp = get_frame_register_unsigned (this_frame, ARM_SP_REGNUM);
1169
1170   return cache;
1171 }
1172
1173 /* Our frame ID for a stub frame is the current SP and LR.  */
1174
1175 static void
1176 arm_stub_this_id (struct frame_info *this_frame,
1177                   void **this_cache,
1178                   struct frame_id *this_id)
1179 {
1180   struct arm_prologue_cache *cache;
1181
1182   if (*this_cache == NULL)
1183     *this_cache = arm_make_stub_cache (this_frame);
1184   cache = *this_cache;
1185
1186   *this_id = frame_id_build (cache->prev_sp, get_frame_pc (this_frame));
1187 }
1188
1189 static int
1190 arm_stub_unwind_sniffer (const struct frame_unwind *self,
1191                          struct frame_info *this_frame,
1192                          void **this_prologue_cache)
1193 {
1194   CORE_ADDR addr_in_block;
1195   char dummy[4];
1196
1197   addr_in_block = get_frame_address_in_block (this_frame);
1198   if (in_plt_section (addr_in_block, NULL)
1199       || target_read_memory (get_frame_pc (this_frame), dummy, 4) != 0)
1200     return 1;
1201
1202   return 0;
1203 }
1204
1205 struct frame_unwind arm_stub_unwind = {
1206   NORMAL_FRAME,
1207   arm_stub_this_id,
1208   arm_prologue_prev_register,
1209   NULL,
1210   arm_stub_unwind_sniffer
1211 };
1212
1213 static CORE_ADDR
1214 arm_normal_frame_base (struct frame_info *this_frame, void **this_cache)
1215 {
1216   struct arm_prologue_cache *cache;
1217
1218   if (*this_cache == NULL)
1219     *this_cache = arm_make_prologue_cache (this_frame);
1220   cache = *this_cache;
1221
1222   return cache->prev_sp - cache->framesize;
1223 }
1224
1225 struct frame_base arm_normal_base = {
1226   &arm_prologue_unwind,
1227   arm_normal_frame_base,
1228   arm_normal_frame_base,
1229   arm_normal_frame_base
1230 };
1231
1232 /* Assuming THIS_FRAME is a dummy, return the frame ID of that
1233    dummy frame.  The frame ID's base needs to match the TOS value
1234    saved by save_dummy_frame_tos() and returned from
1235    arm_push_dummy_call, and the PC needs to match the dummy frame's
1236    breakpoint.  */
1237
1238 static struct frame_id
1239 arm_dummy_id (struct gdbarch *gdbarch, struct frame_info *this_frame)
1240 {
1241   return frame_id_build (get_frame_register_unsigned (this_frame, ARM_SP_REGNUM),
1242                          get_frame_pc (this_frame));
1243 }
1244
1245 /* Given THIS_FRAME, find the previous frame's resume PC (which will
1246    be used to construct the previous frame's ID, after looking up the
1247    containing function).  */
1248
1249 static CORE_ADDR
1250 arm_unwind_pc (struct gdbarch *gdbarch, struct frame_info *this_frame)
1251 {
1252   CORE_ADDR pc;
1253   pc = frame_unwind_register_unsigned (this_frame, ARM_PC_REGNUM);
1254   return arm_addr_bits_remove (pc);
1255 }
1256
1257 static CORE_ADDR
1258 arm_unwind_sp (struct gdbarch *gdbarch, struct frame_info *this_frame)
1259 {
1260   return frame_unwind_register_unsigned (this_frame, ARM_SP_REGNUM);
1261 }
1262
1263 static struct value *
1264 arm_dwarf2_prev_register (struct frame_info *this_frame, void **this_cache,
1265                           int regnum)
1266 {
1267   CORE_ADDR lr, cpsr;
1268
1269   switch (regnum)
1270     {
1271     case ARM_PC_REGNUM:
1272       /* The PC is normally copied from the return column, which
1273          describes saves of LR.  However, that version may have an
1274          extra bit set to indicate Thumb state.  The bit is not
1275          part of the PC.  */
1276       lr = frame_unwind_register_unsigned (this_frame, ARM_LR_REGNUM);
1277       return frame_unwind_got_constant (this_frame, regnum,
1278                                         arm_addr_bits_remove (lr));
1279
1280     case ARM_PS_REGNUM:
1281       /* Reconstruct the T bit; see arm_prologue_prev_register for details.  */
1282       cpsr = get_frame_register_unsigned (this_frame, regnum);
1283       lr = frame_unwind_register_unsigned (this_frame, ARM_LR_REGNUM);
1284       if (IS_THUMB_ADDR (lr))
1285         cpsr |= CPSR_T;
1286       else
1287         cpsr &= ~CPSR_T;
1288       return frame_unwind_got_constant (this_frame, regnum, cpsr);
1289
1290     default:
1291       internal_error (__FILE__, __LINE__,
1292                       _("Unexpected register %d"), regnum);
1293     }
1294 }
1295
1296 static void
1297 arm_dwarf2_frame_init_reg (struct gdbarch *gdbarch, int regnum,
1298                            struct dwarf2_frame_state_reg *reg,
1299                            struct frame_info *this_frame)
1300 {
1301   switch (regnum)
1302     {
1303     case ARM_PC_REGNUM:
1304     case ARM_PS_REGNUM:
1305       reg->how = DWARF2_FRAME_REG_FN;
1306       reg->loc.fn = arm_dwarf2_prev_register;
1307       break;
1308     case ARM_SP_REGNUM:
1309       reg->how = DWARF2_FRAME_REG_CFA;
1310       break;
1311     }
1312 }
1313
1314 /* When arguments must be pushed onto the stack, they go on in reverse
1315    order.  The code below implements a FILO (stack) to do this.  */
1316
1317 struct stack_item
1318 {
1319   int len;
1320   struct stack_item *prev;
1321   void *data;
1322 };
1323
1324 static struct stack_item *
1325 push_stack_item (struct stack_item *prev, void *contents, int len)
1326 {
1327   struct stack_item *si;
1328   si = xmalloc (sizeof (struct stack_item));
1329   si->data = xmalloc (len);
1330   si->len = len;
1331   si->prev = prev;
1332   memcpy (si->data, contents, len);
1333   return si;
1334 }
1335
1336 static struct stack_item *
1337 pop_stack_item (struct stack_item *si)
1338 {
1339   struct stack_item *dead = si;
1340   si = si->prev;
1341   xfree (dead->data);
1342   xfree (dead);
1343   return si;
1344 }
1345
1346
1347 /* Return the alignment (in bytes) of the given type.  */
1348
1349 static int
1350 arm_type_align (struct type *t)
1351 {
1352   int n;
1353   int align;
1354   int falign;
1355
1356   t = check_typedef (t);
1357   switch (TYPE_CODE (t))
1358     {
1359     default:
1360       /* Should never happen.  */
1361       internal_error (__FILE__, __LINE__, _("unknown type alignment"));
1362       return 4;
1363
1364     case TYPE_CODE_PTR:
1365     case TYPE_CODE_ENUM:
1366     case TYPE_CODE_INT:
1367     case TYPE_CODE_FLT:
1368     case TYPE_CODE_SET:
1369     case TYPE_CODE_RANGE:
1370     case TYPE_CODE_BITSTRING:
1371     case TYPE_CODE_REF:
1372     case TYPE_CODE_CHAR:
1373     case TYPE_CODE_BOOL:
1374       return TYPE_LENGTH (t);
1375
1376     case TYPE_CODE_ARRAY:
1377     case TYPE_CODE_COMPLEX:
1378       /* TODO: What about vector types?  */
1379       return arm_type_align (TYPE_TARGET_TYPE (t));
1380
1381     case TYPE_CODE_STRUCT:
1382     case TYPE_CODE_UNION:
1383       align = 1;
1384       for (n = 0; n < TYPE_NFIELDS (t); n++)
1385         {
1386           falign = arm_type_align (TYPE_FIELD_TYPE (t, n));
1387           if (falign > align)
1388             align = falign;
1389         }
1390       return align;
1391     }
1392 }
1393
1394 /* We currently only support passing parameters in integer registers.  This
1395    conforms with GCC's default model.  Several other variants exist and
1396    we should probably support some of them based on the selected ABI.  */
1397
1398 static CORE_ADDR
1399 arm_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
1400                      struct regcache *regcache, CORE_ADDR bp_addr, int nargs,
1401                      struct value **args, CORE_ADDR sp, int struct_return,
1402                      CORE_ADDR struct_addr)
1403 {
1404   int argnum;
1405   int argreg;
1406   int nstack;
1407   struct stack_item *si = NULL;
1408
1409   /* Set the return address.  For the ARM, the return breakpoint is
1410      always at BP_ADDR.  */
1411   /* XXX Fix for Thumb.  */
1412   regcache_cooked_write_unsigned (regcache, ARM_LR_REGNUM, bp_addr);
1413
1414   /* Walk through the list of args and determine how large a temporary
1415      stack is required.  Need to take care here as structs may be
1416      passed on the stack, and we have to to push them.  */
1417   nstack = 0;
1418
1419   argreg = ARM_A1_REGNUM;
1420   nstack = 0;
1421
1422   /* The struct_return pointer occupies the first parameter
1423      passing register.  */
1424   if (struct_return)
1425     {
1426       if (arm_debug)
1427         fprintf_unfiltered (gdb_stdlog, "struct return in %s = 0x%s\n",
1428                             gdbarch_register_name (gdbarch, argreg),
1429                             paddr (struct_addr));
1430       regcache_cooked_write_unsigned (regcache, argreg, struct_addr);
1431       argreg++;
1432     }
1433
1434   for (argnum = 0; argnum < nargs; argnum++)
1435     {
1436       int len;
1437       struct type *arg_type;
1438       struct type *target_type;
1439       enum type_code typecode;
1440       bfd_byte *val;
1441       int align;
1442
1443       arg_type = check_typedef (value_type (args[argnum]));
1444       len = TYPE_LENGTH (arg_type);
1445       target_type = TYPE_TARGET_TYPE (arg_type);
1446       typecode = TYPE_CODE (arg_type);
1447       val = value_contents_writeable (args[argnum]);
1448
1449       align = arm_type_align (arg_type);
1450       /* Round alignment up to a whole number of words.  */
1451       align = (align + INT_REGISTER_SIZE - 1) & ~(INT_REGISTER_SIZE - 1);
1452       /* Different ABIs have different maximum alignments.  */
1453       if (gdbarch_tdep (gdbarch)->arm_abi == ARM_ABI_APCS)
1454         {
1455           /* The APCS ABI only requires word alignment.  */
1456           align = INT_REGISTER_SIZE;
1457         }
1458       else
1459         {
1460           /* The AAPCS requires at most doubleword alignment.  */
1461           if (align > INT_REGISTER_SIZE * 2)
1462             align = INT_REGISTER_SIZE * 2;
1463         }
1464
1465       /* Push stack padding for dowubleword alignment.  */
1466       if (nstack & (align - 1))
1467         {
1468           si = push_stack_item (si, val, INT_REGISTER_SIZE);
1469           nstack += INT_REGISTER_SIZE;
1470         }
1471       
1472       /* Doubleword aligned quantities must go in even register pairs.  */
1473       if (argreg <= ARM_LAST_ARG_REGNUM
1474           && align > INT_REGISTER_SIZE
1475           && argreg & 1)
1476         argreg++;
1477
1478       /* If the argument is a pointer to a function, and it is a
1479          Thumb function, create a LOCAL copy of the value and set
1480          the THUMB bit in it.  */
1481       if (TYPE_CODE_PTR == typecode
1482           && target_type != NULL
1483           && TYPE_CODE_FUNC == TYPE_CODE (target_type))
1484         {
1485           CORE_ADDR regval = extract_unsigned_integer (val, len);
1486           if (arm_pc_is_thumb (regval))
1487             {
1488               val = alloca (len);
1489               store_unsigned_integer (val, len, MAKE_THUMB_ADDR (regval));
1490             }
1491         }
1492
1493       /* Copy the argument to general registers or the stack in
1494          register-sized pieces.  Large arguments are split between
1495          registers and stack.  */
1496       while (len > 0)
1497         {
1498           int partial_len = len < INT_REGISTER_SIZE ? len : INT_REGISTER_SIZE;
1499
1500           if (argreg <= ARM_LAST_ARG_REGNUM)
1501             {
1502               /* The argument is being passed in a general purpose
1503                  register.  */
1504               CORE_ADDR regval = extract_unsigned_integer (val, partial_len);
1505               if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
1506                 regval <<= (INT_REGISTER_SIZE - partial_len) * 8;
1507               if (arm_debug)
1508                 fprintf_unfiltered (gdb_stdlog, "arg %d in %s = 0x%s\n",
1509                                     argnum,
1510                                     gdbarch_register_name
1511                                       (gdbarch, argreg),
1512                                     phex (regval, INT_REGISTER_SIZE));
1513               regcache_cooked_write_unsigned (regcache, argreg, regval);
1514               argreg++;
1515             }
1516           else
1517             {
1518               /* Push the arguments onto the stack.  */
1519               if (arm_debug)
1520                 fprintf_unfiltered (gdb_stdlog, "arg %d @ sp + %d\n",
1521                                     argnum, nstack);
1522               si = push_stack_item (si, val, INT_REGISTER_SIZE);
1523               nstack += INT_REGISTER_SIZE;
1524             }
1525               
1526           len -= partial_len;
1527           val += partial_len;
1528         }
1529     }
1530   /* If we have an odd number of words to push, then decrement the stack
1531      by one word now, so first stack argument will be dword aligned.  */
1532   if (nstack & 4)
1533     sp -= 4;
1534
1535   while (si)
1536     {
1537       sp -= si->len;
1538       write_memory (sp, si->data, si->len);
1539       si = pop_stack_item (si);
1540     }
1541
1542   /* Finally, update teh SP register.  */
1543   regcache_cooked_write_unsigned (regcache, ARM_SP_REGNUM, sp);
1544
1545   return sp;
1546 }
1547
1548
1549 /* Always align the frame to an 8-byte boundary.  This is required on
1550    some platforms and harmless on the rest.  */
1551
1552 static CORE_ADDR
1553 arm_frame_align (struct gdbarch *gdbarch, CORE_ADDR sp)
1554 {
1555   /* Align the stack to eight bytes.  */
1556   return sp & ~ (CORE_ADDR) 7;
1557 }
1558
1559 static void
1560 print_fpu_flags (int flags)
1561 {
1562   if (flags & (1 << 0))
1563     fputs ("IVO ", stdout);
1564   if (flags & (1 << 1))
1565     fputs ("DVZ ", stdout);
1566   if (flags & (1 << 2))
1567     fputs ("OFL ", stdout);
1568   if (flags & (1 << 3))
1569     fputs ("UFL ", stdout);
1570   if (flags & (1 << 4))
1571     fputs ("INX ", stdout);
1572   putchar ('\n');
1573 }
1574
1575 /* Print interesting information about the floating point processor
1576    (if present) or emulator.  */
1577 static void
1578 arm_print_float_info (struct gdbarch *gdbarch, struct ui_file *file,
1579                       struct frame_info *frame, const char *args)
1580 {
1581   unsigned long status = get_frame_register_unsigned (frame, ARM_FPS_REGNUM);
1582   int type;
1583
1584   type = (status >> 24) & 127;
1585   if (status & (1 << 31))
1586     printf (_("Hardware FPU type %d\n"), type);
1587   else
1588     printf (_("Software FPU type %d\n"), type);
1589   /* i18n: [floating point unit] mask */
1590   fputs (_("mask: "), stdout);
1591   print_fpu_flags (status >> 16);
1592   /* i18n: [floating point unit] flags */
1593   fputs (_("flags: "), stdout);
1594   print_fpu_flags (status);
1595 }
1596
1597 /* Return the GDB type object for the "standard" data type of data in
1598    register N.  */
1599
1600 static struct type *
1601 arm_register_type (struct gdbarch *gdbarch, int regnum)
1602 {
1603   if (regnum >= ARM_F0_REGNUM && regnum < ARM_F0_REGNUM + NUM_FREGS)
1604     return builtin_type_arm_ext;
1605   else if (regnum == ARM_SP_REGNUM)
1606     return builtin_type_void_data_ptr;
1607   else if (regnum == ARM_PC_REGNUM)
1608     return builtin_type_void_func_ptr;
1609   else if (regnum >= ARRAY_SIZE (arm_register_names))
1610     /* These registers are only supported on targets which supply
1611        an XML description.  */
1612     return builtin_type_int0;
1613   else
1614     return builtin_type_uint32;
1615 }
1616
1617 /* Map a DWARF register REGNUM onto the appropriate GDB register
1618    number.  */
1619
1620 static int
1621 arm_dwarf_reg_to_regnum (struct gdbarch *gdbarch, int reg)
1622 {
1623   /* Core integer regs.  */
1624   if (reg >= 0 && reg <= 15)
1625     return reg;
1626
1627   /* Legacy FPA encoding.  These were once used in a way which
1628      overlapped with VFP register numbering, so their use is
1629      discouraged, but GDB doesn't support the ARM toolchain
1630      which used them for VFP.  */
1631   if (reg >= 16 && reg <= 23)
1632     return ARM_F0_REGNUM + reg - 16;
1633
1634   /* New assignments for the FPA registers.  */
1635   if (reg >= 96 && reg <= 103)
1636     return ARM_F0_REGNUM + reg - 96;
1637
1638   /* WMMX register assignments.  */
1639   if (reg >= 104 && reg <= 111)
1640     return ARM_WCGR0_REGNUM + reg - 104;
1641
1642   if (reg >= 112 && reg <= 127)
1643     return ARM_WR0_REGNUM + reg - 112;
1644
1645   if (reg >= 192 && reg <= 199)
1646     return ARM_WC0_REGNUM + reg - 192;
1647
1648   return -1;
1649 }
1650
1651 /* Map GDB internal REGNUM onto the Arm simulator register numbers.  */
1652 static int
1653 arm_register_sim_regno (struct gdbarch *gdbarch, int regnum)
1654 {
1655   int reg = regnum;
1656   gdb_assert (reg >= 0 && reg < gdbarch_num_regs (gdbarch));
1657
1658   if (regnum >= ARM_WR0_REGNUM && regnum <= ARM_WR15_REGNUM)
1659     return regnum - ARM_WR0_REGNUM + SIM_ARM_IWMMXT_COP0R0_REGNUM;
1660
1661   if (regnum >= ARM_WC0_REGNUM && regnum <= ARM_WC7_REGNUM)
1662     return regnum - ARM_WC0_REGNUM + SIM_ARM_IWMMXT_COP1R0_REGNUM;
1663
1664   if (regnum >= ARM_WCGR0_REGNUM && regnum <= ARM_WCGR7_REGNUM)
1665     return regnum - ARM_WCGR0_REGNUM + SIM_ARM_IWMMXT_COP1R8_REGNUM;
1666
1667   if (reg < NUM_GREGS)
1668     return SIM_ARM_R0_REGNUM + reg;
1669   reg -= NUM_GREGS;
1670
1671   if (reg < NUM_FREGS)
1672     return SIM_ARM_FP0_REGNUM + reg;
1673   reg -= NUM_FREGS;
1674
1675   if (reg < NUM_SREGS)
1676     return SIM_ARM_FPS_REGNUM + reg;
1677   reg -= NUM_SREGS;
1678
1679   internal_error (__FILE__, __LINE__, _("Bad REGNUM %d"), regnum);
1680 }
1681
1682 /* NOTE: cagney/2001-08-20: Both convert_from_extended() and
1683    convert_to_extended() use floatformat_arm_ext_littlebyte_bigword.
1684    It is thought that this is is the floating-point register format on
1685    little-endian systems.  */
1686
1687 static void
1688 convert_from_extended (const struct floatformat *fmt, const void *ptr,
1689                        void *dbl, int endianess)
1690 {
1691   DOUBLEST d;
1692
1693   if (endianess == BFD_ENDIAN_BIG)
1694     floatformat_to_doublest (&floatformat_arm_ext_big, ptr, &d);
1695   else
1696     floatformat_to_doublest (&floatformat_arm_ext_littlebyte_bigword,
1697                              ptr, &d);
1698   floatformat_from_doublest (fmt, &d, dbl);
1699 }
1700
1701 static void
1702 convert_to_extended (const struct floatformat *fmt, void *dbl, const void *ptr,
1703                      int endianess)
1704 {
1705   DOUBLEST d;
1706
1707   floatformat_to_doublest (fmt, ptr, &d);
1708   if (endianess == BFD_ENDIAN_BIG)
1709     floatformat_from_doublest (&floatformat_arm_ext_big, &d, dbl);
1710   else
1711     floatformat_from_doublest (&floatformat_arm_ext_littlebyte_bigword,
1712                                &d, dbl);
1713 }
1714
1715 static int
1716 condition_true (unsigned long cond, unsigned long status_reg)
1717 {
1718   if (cond == INST_AL || cond == INST_NV)
1719     return 1;
1720
1721   switch (cond)
1722     {
1723     case INST_EQ:
1724       return ((status_reg & FLAG_Z) != 0);
1725     case INST_NE:
1726       return ((status_reg & FLAG_Z) == 0);
1727     case INST_CS:
1728       return ((status_reg & FLAG_C) != 0);
1729     case INST_CC:
1730       return ((status_reg & FLAG_C) == 0);
1731     case INST_MI:
1732       return ((status_reg & FLAG_N) != 0);
1733     case INST_PL:
1734       return ((status_reg & FLAG_N) == 0);
1735     case INST_VS:
1736       return ((status_reg & FLAG_V) != 0);
1737     case INST_VC:
1738       return ((status_reg & FLAG_V) == 0);
1739     case INST_HI:
1740       return ((status_reg & (FLAG_C | FLAG_Z)) == FLAG_C);
1741     case INST_LS:
1742       return ((status_reg & (FLAG_C | FLAG_Z)) != FLAG_C);
1743     case INST_GE:
1744       return (((status_reg & FLAG_N) == 0) == ((status_reg & FLAG_V) == 0));
1745     case INST_LT:
1746       return (((status_reg & FLAG_N) == 0) != ((status_reg & FLAG_V) == 0));
1747     case INST_GT:
1748       return (((status_reg & FLAG_Z) == 0) &&
1749               (((status_reg & FLAG_N) == 0) == ((status_reg & FLAG_V) == 0)));
1750     case INST_LE:
1751       return (((status_reg & FLAG_Z) != 0) ||
1752               (((status_reg & FLAG_N) == 0) != ((status_reg & FLAG_V) == 0)));
1753     }
1754   return 1;
1755 }
1756
1757 /* Support routines for single stepping.  Calculate the next PC value.  */
1758 #define submask(x) ((1L << ((x) + 1)) - 1)
1759 #define bit(obj,st) (((obj) >> (st)) & 1)
1760 #define bits(obj,st,fn) (((obj) >> (st)) & submask ((fn) - (st)))
1761 #define sbits(obj,st,fn) \
1762   ((long) (bits(obj,st,fn) | ((long) bit(obj,fn) * ~ submask (fn - st))))
1763 #define BranchDest(addr,instr) \
1764   ((CORE_ADDR) (((long) (addr)) + 8 + (sbits (instr, 0, 23) << 2)))
1765 #define ARM_PC_32 1
1766
1767 static unsigned long
1768 shifted_reg_val (struct frame_info *frame, unsigned long inst, int carry,
1769                  unsigned long pc_val, unsigned long status_reg)
1770 {
1771   unsigned long res, shift;
1772   int rm = bits (inst, 0, 3);
1773   unsigned long shifttype = bits (inst, 5, 6);
1774
1775   if (bit (inst, 4))
1776     {
1777       int rs = bits (inst, 8, 11);
1778       shift = (rs == 15 ? pc_val + 8
1779                         : get_frame_register_unsigned (frame, rs)) & 0xFF;
1780     }
1781   else
1782     shift = bits (inst, 7, 11);
1783
1784   res = (rm == 15
1785          ? ((pc_val | (ARM_PC_32 ? 0 : status_reg))
1786             + (bit (inst, 4) ? 12 : 8))
1787          : get_frame_register_unsigned (frame, rm));
1788
1789   switch (shifttype)
1790     {
1791     case 0:                     /* LSL */
1792       res = shift >= 32 ? 0 : res << shift;
1793       break;
1794
1795     case 1:                     /* LSR */
1796       res = shift >= 32 ? 0 : res >> shift;
1797       break;
1798
1799     case 2:                     /* ASR */
1800       if (shift >= 32)
1801         shift = 31;
1802       res = ((res & 0x80000000L)
1803              ? ~((~res) >> shift) : res >> shift);
1804       break;
1805
1806     case 3:                     /* ROR/RRX */
1807       shift &= 31;
1808       if (shift == 0)
1809         res = (res >> 1) | (carry ? 0x80000000L : 0);
1810       else
1811         res = (res >> shift) | (res << (32 - shift));
1812       break;
1813     }
1814
1815   return res & 0xffffffff;
1816 }
1817
1818 /* Return number of 1-bits in VAL.  */
1819
1820 static int
1821 bitcount (unsigned long val)
1822 {
1823   int nbits;
1824   for (nbits = 0; val != 0; nbits++)
1825     val &= val - 1;             /* delete rightmost 1-bit in val */
1826   return nbits;
1827 }
1828
1829 static CORE_ADDR
1830 thumb_get_next_pc (struct frame_info *frame, CORE_ADDR pc)
1831 {
1832   struct gdbarch *gdbarch = get_frame_arch (frame);
1833   unsigned long pc_val = ((unsigned long) pc) + 4;      /* PC after prefetch */
1834   unsigned short inst1 = read_memory_unsigned_integer (pc, 2);
1835   CORE_ADDR nextpc = pc + 2;            /* default is next instruction */
1836   unsigned long offset;
1837
1838   if (gdbarch_byte_order_for_code (gdbarch) != gdbarch_byte_order (gdbarch))
1839     inst1 = SWAP_SHORT (inst1);
1840
1841   if ((inst1 & 0xff00) == 0xbd00)       /* pop {rlist, pc} */
1842     {
1843       CORE_ADDR sp;
1844
1845       /* Fetch the saved PC from the stack.  It's stored above
1846          all of the other registers.  */
1847       offset = bitcount (bits (inst1, 0, 7)) * INT_REGISTER_SIZE;
1848       sp = get_frame_register_unsigned (frame, ARM_SP_REGNUM);
1849       nextpc = (CORE_ADDR) read_memory_unsigned_integer (sp + offset, 4);
1850       nextpc = gdbarch_addr_bits_remove (gdbarch, nextpc);
1851       if (nextpc == pc)
1852         error (_("Infinite loop detected"));
1853     }
1854   else if ((inst1 & 0xf000) == 0xd000)  /* conditional branch */
1855     {
1856       unsigned long status = get_frame_register_unsigned (frame, ARM_PS_REGNUM);
1857       unsigned long cond = bits (inst1, 8, 11);
1858       if (cond != 0x0f && condition_true (cond, status))    /* 0x0f = SWI */
1859         nextpc = pc_val + (sbits (inst1, 0, 7) << 1);
1860     }
1861   else if ((inst1 & 0xf800) == 0xe000)  /* unconditional branch */
1862     {
1863       nextpc = pc_val + (sbits (inst1, 0, 10) << 1);
1864     }
1865   else if ((inst1 & 0xf800) == 0xf000)  /* long branch with link, and blx */
1866     {
1867       unsigned short inst2 = read_memory_unsigned_integer (pc + 2, 2);
1868       if (gdbarch_byte_order_for_code (gdbarch) != gdbarch_byte_order (gdbarch))
1869         inst2 = SWAP_SHORT (inst2);
1870       offset = (sbits (inst1, 0, 10) << 12) + (bits (inst2, 0, 10) << 1);
1871       nextpc = pc_val + offset;
1872       /* For BLX make sure to clear the low bits.  */
1873       if (bits (inst2, 11, 12) == 1)
1874         nextpc = nextpc & 0xfffffffc;
1875     }
1876   else if ((inst1 & 0xff00) == 0x4700)  /* bx REG, blx REG */
1877     {
1878       if (bits (inst1, 3, 6) == 0x0f)
1879         nextpc = pc_val;
1880       else
1881         nextpc = get_frame_register_unsigned (frame, bits (inst1, 3, 6));
1882
1883       nextpc = gdbarch_addr_bits_remove (gdbarch, nextpc);
1884       if (nextpc == pc)
1885         error (_("Infinite loop detected"));
1886     }
1887
1888   return nextpc;
1889 }
1890
1891 CORE_ADDR
1892 arm_get_next_pc (struct frame_info *frame, CORE_ADDR pc)
1893 {
1894   struct gdbarch *gdbarch = get_frame_arch (frame);
1895   unsigned long pc_val;
1896   unsigned long this_instr;
1897   unsigned long status;
1898   CORE_ADDR nextpc;
1899
1900   if (arm_frame_is_thumb (frame))
1901     return thumb_get_next_pc (frame, pc);
1902
1903   pc_val = (unsigned long) pc;
1904   this_instr = read_memory_unsigned_integer (pc, 4);
1905
1906   if (gdbarch_byte_order_for_code (gdbarch) != gdbarch_byte_order (gdbarch))
1907     this_instr = SWAP_INT (this_instr);
1908
1909   status = get_frame_register_unsigned (frame, ARM_PS_REGNUM);
1910   nextpc = (CORE_ADDR) (pc_val + 4);    /* Default case */
1911
1912   if (bits (this_instr, 28, 31) == INST_NV)
1913     switch (bits (this_instr, 24, 27))
1914       {
1915       case 0xa:
1916       case 0xb:
1917         {
1918           /* Branch with Link and change to Thumb.  */
1919           nextpc = BranchDest (pc, this_instr);
1920           nextpc |= bit (this_instr, 24) << 1;
1921
1922           nextpc = gdbarch_addr_bits_remove (gdbarch, nextpc);
1923           if (nextpc == pc)
1924             error (_("Infinite loop detected"));
1925           break;
1926         }
1927       case 0xc:
1928       case 0xd:
1929       case 0xe:
1930         /* Coprocessor register transfer.  */
1931         if (bits (this_instr, 12, 15) == 15)
1932           error (_("Invalid update to pc in instruction"));
1933         break;
1934       }
1935   else if (condition_true (bits (this_instr, 28, 31), status))
1936     {
1937       switch (bits (this_instr, 24, 27))
1938         {
1939         case 0x0:
1940         case 0x1:                       /* data processing */
1941         case 0x2:
1942         case 0x3:
1943           {
1944             unsigned long operand1, operand2, result = 0;
1945             unsigned long rn;
1946             int c;
1947
1948             if (bits (this_instr, 12, 15) != 15)
1949               break;
1950
1951             if (bits (this_instr, 22, 25) == 0
1952                 && bits (this_instr, 4, 7) == 9)        /* multiply */
1953               error (_("Invalid update to pc in instruction"));
1954
1955             /* BX <reg>, BLX <reg> */
1956             if (bits (this_instr, 4, 27) == 0x12fff1
1957                 || bits (this_instr, 4, 27) == 0x12fff3)
1958               {
1959                 rn = bits (this_instr, 0, 3);
1960                 result = (rn == 15) ? pc_val + 8
1961                                     : get_frame_register_unsigned (frame, rn);
1962                 nextpc = (CORE_ADDR) gdbarch_addr_bits_remove
1963                                        (gdbarch, result);
1964
1965                 if (nextpc == pc)
1966                   error (_("Infinite loop detected"));
1967
1968                 return nextpc;
1969               }
1970
1971             /* Multiply into PC */
1972             c = (status & FLAG_C) ? 1 : 0;
1973             rn = bits (this_instr, 16, 19);
1974             operand1 = (rn == 15) ? pc_val + 8
1975                                   : get_frame_register_unsigned (frame, rn);
1976
1977             if (bit (this_instr, 25))
1978               {
1979                 unsigned long immval = bits (this_instr, 0, 7);
1980                 unsigned long rotate = 2 * bits (this_instr, 8, 11);
1981                 operand2 = ((immval >> rotate) | (immval << (32 - rotate)))
1982                   & 0xffffffff;
1983               }
1984             else                /* operand 2 is a shifted register */
1985               operand2 = shifted_reg_val (frame, this_instr, c, pc_val, status);
1986
1987             switch (bits (this_instr, 21, 24))
1988               {
1989               case 0x0: /*and */
1990                 result = operand1 & operand2;
1991                 break;
1992
1993               case 0x1: /*eor */
1994                 result = operand1 ^ operand2;
1995                 break;
1996
1997               case 0x2: /*sub */
1998                 result = operand1 - operand2;
1999                 break;
2000
2001               case 0x3: /*rsb */
2002                 result = operand2 - operand1;
2003                 break;
2004
2005               case 0x4: /*add */
2006                 result = operand1 + operand2;
2007                 break;
2008
2009               case 0x5: /*adc */
2010                 result = operand1 + operand2 + c;
2011                 break;
2012
2013               case 0x6: /*sbc */
2014                 result = operand1 - operand2 + c;
2015                 break;
2016
2017               case 0x7: /*rsc */
2018                 result = operand2 - operand1 + c;
2019                 break;
2020
2021               case 0x8:
2022               case 0x9:
2023               case 0xa:
2024               case 0xb: /* tst, teq, cmp, cmn */
2025                 result = (unsigned long) nextpc;
2026                 break;
2027
2028               case 0xc: /*orr */
2029                 result = operand1 | operand2;
2030                 break;
2031
2032               case 0xd: /*mov */
2033                 /* Always step into a function.  */
2034                 result = operand2;
2035                 break;
2036
2037               case 0xe: /*bic */
2038                 result = operand1 & ~operand2;
2039                 break;
2040
2041               case 0xf: /*mvn */
2042                 result = ~operand2;
2043                 break;
2044               }
2045             nextpc = (CORE_ADDR) gdbarch_addr_bits_remove
2046                                    (gdbarch, result);
2047
2048             if (nextpc == pc)
2049               error (_("Infinite loop detected"));
2050             break;
2051           }
2052
2053         case 0x4:
2054         case 0x5:               /* data transfer */
2055         case 0x6:
2056         case 0x7:
2057           if (bit (this_instr, 20))
2058             {
2059               /* load */
2060               if (bits (this_instr, 12, 15) == 15)
2061                 {
2062                   /* rd == pc */
2063                   unsigned long rn;
2064                   unsigned long base;
2065
2066                   if (bit (this_instr, 22))
2067                     error (_("Invalid update to pc in instruction"));
2068
2069                   /* byte write to PC */
2070                   rn = bits (this_instr, 16, 19);
2071                   base = (rn == 15) ? pc_val + 8
2072                                     : get_frame_register_unsigned (frame, rn);
2073                   if (bit (this_instr, 24))
2074                     {
2075                       /* pre-indexed */
2076                       int c = (status & FLAG_C) ? 1 : 0;
2077                       unsigned long offset =
2078                       (bit (this_instr, 25)
2079                        ? shifted_reg_val (frame, this_instr, c, pc_val, status)
2080                        : bits (this_instr, 0, 11));
2081
2082                       if (bit (this_instr, 23))
2083                         base += offset;
2084                       else
2085                         base -= offset;
2086                     }
2087                   nextpc = (CORE_ADDR) read_memory_integer ((CORE_ADDR) base,
2088                                                             4);
2089
2090                   nextpc = gdbarch_addr_bits_remove (gdbarch, nextpc);
2091
2092                   if (nextpc == pc)
2093                     error (_("Infinite loop detected"));
2094                 }
2095             }
2096           break;
2097
2098         case 0x8:
2099         case 0x9:               /* block transfer */
2100           if (bit (this_instr, 20))
2101             {
2102               /* LDM */
2103               if (bit (this_instr, 15))
2104                 {
2105                   /* loading pc */
2106                   int offset = 0;
2107
2108                   if (bit (this_instr, 23))
2109                     {
2110                       /* up */
2111                       unsigned long reglist = bits (this_instr, 0, 14);
2112                       offset = bitcount (reglist) * 4;
2113                       if (bit (this_instr, 24))         /* pre */
2114                         offset += 4;
2115                     }
2116                   else if (bit (this_instr, 24))
2117                     offset = -4;
2118
2119                   {
2120                     unsigned long rn_val =
2121                     get_frame_register_unsigned (frame,
2122                                                  bits (this_instr, 16, 19));
2123                     nextpc =
2124                       (CORE_ADDR) read_memory_integer ((CORE_ADDR) (rn_val
2125                                                                   + offset),
2126                                                        4);
2127                   }
2128                   nextpc = gdbarch_addr_bits_remove
2129                              (gdbarch, nextpc);
2130                   if (nextpc == pc)
2131                     error (_("Infinite loop detected"));
2132                 }
2133             }
2134           break;
2135
2136         case 0xb:               /* branch & link */
2137         case 0xa:               /* branch */
2138           {
2139             nextpc = BranchDest (pc, this_instr);
2140
2141             nextpc = gdbarch_addr_bits_remove (gdbarch, nextpc);
2142             if (nextpc == pc)
2143               error (_("Infinite loop detected"));
2144             break;
2145           }
2146
2147         case 0xc:
2148         case 0xd:
2149         case 0xe:               /* coproc ops */
2150         case 0xf:               /* SWI */
2151           break;
2152
2153         default:
2154           fprintf_filtered (gdb_stderr, _("Bad bit-field extraction\n"));
2155           return (pc);
2156         }
2157     }
2158
2159   return nextpc;
2160 }
2161
2162 /* single_step() is called just before we want to resume the inferior,
2163    if we want to single-step it but there is no hardware or kernel
2164    single-step support.  We find the target of the coming instruction
2165    and breakpoint it.  */
2166
2167 int
2168 arm_software_single_step (struct frame_info *frame)
2169 {
2170   /* NOTE: This may insert the wrong breakpoint instruction when
2171      single-stepping over a mode-changing instruction, if the
2172      CPSR heuristics are used.  */
2173
2174   CORE_ADDR next_pc = arm_get_next_pc (frame, get_frame_pc (frame));
2175   insert_single_step_breakpoint (next_pc);
2176
2177   return 1;
2178 }
2179
2180 #include "bfd-in2.h"
2181 #include "libcoff.h"
2182
2183 static int
2184 gdb_print_insn_arm (bfd_vma memaddr, disassemble_info *info)
2185 {
2186   if (arm_pc_is_thumb (memaddr))
2187     {
2188       static asymbol *asym;
2189       static combined_entry_type ce;
2190       static struct coff_symbol_struct csym;
2191       static struct bfd fake_bfd;
2192       static bfd_target fake_target;
2193
2194       if (csym.native == NULL)
2195         {
2196           /* Create a fake symbol vector containing a Thumb symbol.
2197              This is solely so that the code in print_insn_little_arm() 
2198              and print_insn_big_arm() in opcodes/arm-dis.c will detect
2199              the presence of a Thumb symbol and switch to decoding
2200              Thumb instructions.  */
2201
2202           fake_target.flavour = bfd_target_coff_flavour;
2203           fake_bfd.xvec = &fake_target;
2204           ce.u.syment.n_sclass = C_THUMBEXTFUNC;
2205           csym.native = &ce;
2206           csym.symbol.the_bfd = &fake_bfd;
2207           csym.symbol.name = "fake";
2208           asym = (asymbol *) & csym;
2209         }
2210
2211       memaddr = UNMAKE_THUMB_ADDR (memaddr);
2212       info->symbols = &asym;
2213     }
2214   else
2215     info->symbols = NULL;
2216
2217   if (info->endian == BFD_ENDIAN_BIG)
2218     return print_insn_big_arm (memaddr, info);
2219   else
2220     return print_insn_little_arm (memaddr, info);
2221 }
2222
2223 /* The following define instruction sequences that will cause ARM
2224    cpu's to take an undefined instruction trap.  These are used to
2225    signal a breakpoint to GDB.
2226    
2227    The newer ARMv4T cpu's are capable of operating in ARM or Thumb
2228    modes.  A different instruction is required for each mode.  The ARM
2229    cpu's can also be big or little endian.  Thus four different
2230    instructions are needed to support all cases.
2231    
2232    Note: ARMv4 defines several new instructions that will take the
2233    undefined instruction trap.  ARM7TDMI is nominally ARMv4T, but does
2234    not in fact add the new instructions.  The new undefined
2235    instructions in ARMv4 are all instructions that had no defined
2236    behaviour in earlier chips.  There is no guarantee that they will
2237    raise an exception, but may be treated as NOP's.  In practice, it
2238    may only safe to rely on instructions matching:
2239    
2240    3 3 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1 
2241    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
2242    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
2243    
2244    Even this may only true if the condition predicate is true. The
2245    following use a condition predicate of ALWAYS so it is always TRUE.
2246    
2247    There are other ways of forcing a breakpoint.  GNU/Linux, RISC iX,
2248    and NetBSD all use a software interrupt rather than an undefined
2249    instruction to force a trap.  This can be handled by by the
2250    abi-specific code during establishment of the gdbarch vector.  */
2251
2252 #define ARM_LE_BREAKPOINT {0xFE,0xDE,0xFF,0xE7}
2253 #define ARM_BE_BREAKPOINT {0xE7,0xFF,0xDE,0xFE}
2254 #define THUMB_LE_BREAKPOINT {0xbe,0xbe}
2255 #define THUMB_BE_BREAKPOINT {0xbe,0xbe}
2256
2257 static const char arm_default_arm_le_breakpoint[] = ARM_LE_BREAKPOINT;
2258 static const char arm_default_arm_be_breakpoint[] = ARM_BE_BREAKPOINT;
2259 static const char arm_default_thumb_le_breakpoint[] = THUMB_LE_BREAKPOINT;
2260 static const char arm_default_thumb_be_breakpoint[] = THUMB_BE_BREAKPOINT;
2261
2262 /* Determine the type and size of breakpoint to insert at PCPTR.  Uses
2263    the program counter value to determine whether a 16-bit or 32-bit
2264    breakpoint should be used.  It returns a pointer to a string of
2265    bytes that encode a breakpoint instruction, stores the length of
2266    the string to *lenptr, and adjusts the program counter (if
2267    necessary) to point to the actual memory location where the
2268    breakpoint should be inserted.  */
2269
2270 static const unsigned char *
2271 arm_breakpoint_from_pc (struct gdbarch *gdbarch, CORE_ADDR *pcptr, int *lenptr)
2272 {
2273   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
2274
2275   if (arm_pc_is_thumb (*pcptr))
2276     {
2277       *pcptr = UNMAKE_THUMB_ADDR (*pcptr);
2278       *lenptr = tdep->thumb_breakpoint_size;
2279       return tdep->thumb_breakpoint;
2280     }
2281   else
2282     {
2283       *lenptr = tdep->arm_breakpoint_size;
2284       return tdep->arm_breakpoint;
2285     }
2286 }
2287
2288 /* Extract from an array REGBUF containing the (raw) register state a
2289    function return value of type TYPE, and copy that, in virtual
2290    format, into VALBUF.  */
2291
2292 static void
2293 arm_extract_return_value (struct type *type, struct regcache *regs,
2294                           gdb_byte *valbuf)
2295 {
2296   struct gdbarch *gdbarch = get_regcache_arch (regs);
2297
2298   if (TYPE_CODE_FLT == TYPE_CODE (type))
2299     {
2300       switch (gdbarch_tdep (gdbarch)->fp_model)
2301         {
2302         case ARM_FLOAT_FPA:
2303           {
2304             /* The value is in register F0 in internal format.  We need to
2305                extract the raw value and then convert it to the desired
2306                internal type.  */
2307             bfd_byte tmpbuf[FP_REGISTER_SIZE];
2308
2309             regcache_cooked_read (regs, ARM_F0_REGNUM, tmpbuf);
2310             convert_from_extended (floatformat_from_type (type), tmpbuf,
2311                                    valbuf, gdbarch_byte_order (gdbarch));
2312           }
2313           break;
2314
2315         case ARM_FLOAT_SOFT_FPA:
2316         case ARM_FLOAT_SOFT_VFP:
2317           regcache_cooked_read (regs, ARM_A1_REGNUM, valbuf);
2318           if (TYPE_LENGTH (type) > 4)
2319             regcache_cooked_read (regs, ARM_A1_REGNUM + 1,
2320                                   valbuf + INT_REGISTER_SIZE);
2321           break;
2322
2323         default:
2324           internal_error
2325             (__FILE__, __LINE__,
2326              _("arm_extract_return_value: Floating point model not supported"));
2327           break;
2328         }
2329     }
2330   else if (TYPE_CODE (type) == TYPE_CODE_INT
2331            || TYPE_CODE (type) == TYPE_CODE_CHAR
2332            || TYPE_CODE (type) == TYPE_CODE_BOOL
2333            || TYPE_CODE (type) == TYPE_CODE_PTR
2334            || TYPE_CODE (type) == TYPE_CODE_REF
2335            || TYPE_CODE (type) == TYPE_CODE_ENUM)
2336     {
2337       /* If the the type is a plain integer, then the access is
2338          straight-forward.  Otherwise we have to play around a bit more.  */
2339       int len = TYPE_LENGTH (type);
2340       int regno = ARM_A1_REGNUM;
2341       ULONGEST tmp;
2342
2343       while (len > 0)
2344         {
2345           /* By using store_unsigned_integer we avoid having to do
2346              anything special for small big-endian values.  */
2347           regcache_cooked_read_unsigned (regs, regno++, &tmp);
2348           store_unsigned_integer (valbuf, 
2349                                   (len > INT_REGISTER_SIZE
2350                                    ? INT_REGISTER_SIZE : len),
2351                                   tmp);
2352           len -= INT_REGISTER_SIZE;
2353           valbuf += INT_REGISTER_SIZE;
2354         }
2355     }
2356   else
2357     {
2358       /* For a structure or union the behaviour is as if the value had
2359          been stored to word-aligned memory and then loaded into 
2360          registers with 32-bit load instruction(s).  */
2361       int len = TYPE_LENGTH (type);
2362       int regno = ARM_A1_REGNUM;
2363       bfd_byte tmpbuf[INT_REGISTER_SIZE];
2364
2365       while (len > 0)
2366         {
2367           regcache_cooked_read (regs, regno++, tmpbuf);
2368           memcpy (valbuf, tmpbuf,
2369                   len > INT_REGISTER_SIZE ? INT_REGISTER_SIZE : len);
2370           len -= INT_REGISTER_SIZE;
2371           valbuf += INT_REGISTER_SIZE;
2372         }
2373     }
2374 }
2375
2376
2377 /* Will a function return an aggregate type in memory or in a
2378    register?  Return 0 if an aggregate type can be returned in a
2379    register, 1 if it must be returned in memory.  */
2380
2381 static int
2382 arm_return_in_memory (struct gdbarch *gdbarch, struct type *type)
2383 {
2384   int nRc;
2385   enum type_code code;
2386
2387   CHECK_TYPEDEF (type);
2388
2389   /* In the ARM ABI, "integer" like aggregate types are returned in
2390      registers.  For an aggregate type to be integer like, its size
2391      must be less than or equal to INT_REGISTER_SIZE and the
2392      offset of each addressable subfield must be zero.  Note that bit
2393      fields are not addressable, and all addressable subfields of
2394      unions always start at offset zero.
2395
2396      This function is based on the behaviour of GCC 2.95.1.
2397      See: gcc/arm.c: arm_return_in_memory() for details.
2398
2399      Note: All versions of GCC before GCC 2.95.2 do not set up the
2400      parameters correctly for a function returning the following
2401      structure: struct { float f;}; This should be returned in memory,
2402      not a register.  Richard Earnshaw sent me a patch, but I do not
2403      know of any way to detect if a function like the above has been
2404      compiled with the correct calling convention.  */
2405
2406   /* All aggregate types that won't fit in a register must be returned
2407      in memory.  */
2408   if (TYPE_LENGTH (type) > INT_REGISTER_SIZE)
2409     {
2410       return 1;
2411     }
2412
2413   /* The AAPCS says all aggregates not larger than a word are returned
2414      in a register.  */
2415   if (gdbarch_tdep (gdbarch)->arm_abi != ARM_ABI_APCS)
2416     return 0;
2417
2418   /* The only aggregate types that can be returned in a register are
2419      structs and unions.  Arrays must be returned in memory.  */
2420   code = TYPE_CODE (type);
2421   if ((TYPE_CODE_STRUCT != code) && (TYPE_CODE_UNION != code))
2422     {
2423       return 1;
2424     }
2425
2426   /* Assume all other aggregate types can be returned in a register.
2427      Run a check for structures, unions and arrays.  */
2428   nRc = 0;
2429
2430   if ((TYPE_CODE_STRUCT == code) || (TYPE_CODE_UNION == code))
2431     {
2432       int i;
2433       /* Need to check if this struct/union is "integer" like.  For
2434          this to be true, its size must be less than or equal to
2435          INT_REGISTER_SIZE and the offset of each addressable
2436          subfield must be zero.  Note that bit fields are not
2437          addressable, and unions always start at offset zero.  If any
2438          of the subfields is a floating point type, the struct/union
2439          cannot be an integer type.  */
2440
2441       /* For each field in the object, check:
2442          1) Is it FP? --> yes, nRc = 1;
2443          2) Is it addressable (bitpos != 0) and
2444          not packed (bitsize == 0)?
2445          --> yes, nRc = 1  
2446        */
2447
2448       for (i = 0; i < TYPE_NFIELDS (type); i++)
2449         {
2450           enum type_code field_type_code;
2451           field_type_code = TYPE_CODE (check_typedef (TYPE_FIELD_TYPE (type, i)));
2452
2453           /* Is it a floating point type field?  */
2454           if (field_type_code == TYPE_CODE_FLT)
2455             {
2456               nRc = 1;
2457               break;
2458             }
2459
2460           /* If bitpos != 0, then we have to care about it.  */
2461           if (TYPE_FIELD_BITPOS (type, i) != 0)
2462             {
2463               /* Bitfields are not addressable.  If the field bitsize is 
2464                  zero, then the field is not packed.  Hence it cannot be
2465                  a bitfield or any other packed type.  */
2466               if (TYPE_FIELD_BITSIZE (type, i) == 0)
2467                 {
2468                   nRc = 1;
2469                   break;
2470                 }
2471             }
2472         }
2473     }
2474
2475   return nRc;
2476 }
2477
2478 /* Write into appropriate registers a function return value of type
2479    TYPE, given in virtual format.  */
2480
2481 static void
2482 arm_store_return_value (struct type *type, struct regcache *regs,
2483                         const gdb_byte *valbuf)
2484 {
2485   struct gdbarch *gdbarch = get_regcache_arch (regs);
2486
2487   if (TYPE_CODE (type) == TYPE_CODE_FLT)
2488     {
2489       char buf[MAX_REGISTER_SIZE];
2490
2491       switch (gdbarch_tdep (gdbarch)->fp_model)
2492         {
2493         case ARM_FLOAT_FPA:
2494
2495           convert_to_extended (floatformat_from_type (type), buf, valbuf,
2496                                gdbarch_byte_order (gdbarch));
2497           regcache_cooked_write (regs, ARM_F0_REGNUM, buf);
2498           break;
2499
2500         case ARM_FLOAT_SOFT_FPA:
2501         case ARM_FLOAT_SOFT_VFP:
2502           regcache_cooked_write (regs, ARM_A1_REGNUM, valbuf);
2503           if (TYPE_LENGTH (type) > 4)
2504             regcache_cooked_write (regs, ARM_A1_REGNUM + 1, 
2505                                    valbuf + INT_REGISTER_SIZE);
2506           break;
2507
2508         default:
2509           internal_error
2510             (__FILE__, __LINE__,
2511              _("arm_store_return_value: Floating point model not supported"));
2512           break;
2513         }
2514     }
2515   else if (TYPE_CODE (type) == TYPE_CODE_INT
2516            || TYPE_CODE (type) == TYPE_CODE_CHAR
2517            || TYPE_CODE (type) == TYPE_CODE_BOOL
2518            || TYPE_CODE (type) == TYPE_CODE_PTR
2519            || TYPE_CODE (type) == TYPE_CODE_REF
2520            || TYPE_CODE (type) == TYPE_CODE_ENUM)
2521     {
2522       if (TYPE_LENGTH (type) <= 4)
2523         {
2524           /* Values of one word or less are zero/sign-extended and
2525              returned in r0.  */
2526           bfd_byte tmpbuf[INT_REGISTER_SIZE];
2527           LONGEST val = unpack_long (type, valbuf);
2528
2529           store_signed_integer (tmpbuf, INT_REGISTER_SIZE, val);
2530           regcache_cooked_write (regs, ARM_A1_REGNUM, tmpbuf);
2531         }
2532       else
2533         {
2534           /* Integral values greater than one word are stored in consecutive
2535              registers starting with r0.  This will always be a multiple of
2536              the regiser size.  */
2537           int len = TYPE_LENGTH (type);
2538           int regno = ARM_A1_REGNUM;
2539
2540           while (len > 0)
2541             {
2542               regcache_cooked_write (regs, regno++, valbuf);
2543               len -= INT_REGISTER_SIZE;
2544               valbuf += INT_REGISTER_SIZE;
2545             }
2546         }
2547     }
2548   else
2549     {
2550       /* For a structure or union the behaviour is as if the value had
2551          been stored to word-aligned memory and then loaded into 
2552          registers with 32-bit load instruction(s).  */
2553       int len = TYPE_LENGTH (type);
2554       int regno = ARM_A1_REGNUM;
2555       bfd_byte tmpbuf[INT_REGISTER_SIZE];
2556
2557       while (len > 0)
2558         {
2559           memcpy (tmpbuf, valbuf,
2560                   len > INT_REGISTER_SIZE ? INT_REGISTER_SIZE : len);
2561           regcache_cooked_write (regs, regno++, tmpbuf);
2562           len -= INT_REGISTER_SIZE;
2563           valbuf += INT_REGISTER_SIZE;
2564         }
2565     }
2566 }
2567
2568
2569 /* Handle function return values.  */
2570
2571 static enum return_value_convention
2572 arm_return_value (struct gdbarch *gdbarch, struct type *func_type,
2573                   struct type *valtype, struct regcache *regcache,
2574                   gdb_byte *readbuf, const gdb_byte *writebuf)
2575 {
2576   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
2577
2578   if (TYPE_CODE (valtype) == TYPE_CODE_STRUCT
2579       || TYPE_CODE (valtype) == TYPE_CODE_UNION
2580       || TYPE_CODE (valtype) == TYPE_CODE_ARRAY)
2581     {
2582       if (tdep->struct_return == pcc_struct_return
2583           || arm_return_in_memory (gdbarch, valtype))
2584         return RETURN_VALUE_STRUCT_CONVENTION;
2585     }
2586
2587   if (writebuf)
2588     arm_store_return_value (valtype, regcache, writebuf);
2589
2590   if (readbuf)
2591     arm_extract_return_value (valtype, regcache, readbuf);
2592
2593   return RETURN_VALUE_REGISTER_CONVENTION;
2594 }
2595
2596
2597 static int
2598 arm_get_longjmp_target (struct frame_info *frame, CORE_ADDR *pc)
2599 {
2600   CORE_ADDR jb_addr;
2601   char buf[INT_REGISTER_SIZE];
2602   struct gdbarch_tdep *tdep = gdbarch_tdep (get_frame_arch (frame));
2603   
2604   jb_addr = get_frame_register_unsigned (frame, ARM_A1_REGNUM);
2605
2606   if (target_read_memory (jb_addr + tdep->jb_pc * tdep->jb_elt_size, buf,
2607                           INT_REGISTER_SIZE))
2608     return 0;
2609
2610   *pc = extract_unsigned_integer (buf, INT_REGISTER_SIZE);
2611   return 1;
2612 }
2613
2614 /* Recognize GCC and GNU ld's trampolines.  If we are in a trampoline,
2615    return the target PC.  Otherwise return 0.  */
2616
2617 CORE_ADDR
2618 arm_skip_stub (struct frame_info *frame, CORE_ADDR pc)
2619 {
2620   char *name;
2621   int namelen;
2622   CORE_ADDR start_addr;
2623
2624   /* Find the starting address and name of the function containing the PC.  */
2625   if (find_pc_partial_function (pc, &name, &start_addr, NULL) == 0)
2626     return 0;
2627
2628   /* If PC is in a Thumb call or return stub, return the address of the
2629      target PC, which is in a register.  The thunk functions are called
2630      _call_via_xx, where x is the register name.  The possible names
2631      are r0-r9, sl, fp, ip, sp, and lr.  */
2632   if (strncmp (name, "_call_via_", 10) == 0)
2633     {
2634       /* Use the name suffix to determine which register contains the
2635          target PC.  */
2636       static char *table[15] =
2637       {"r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
2638        "r8", "r9", "sl", "fp", "ip", "sp", "lr"
2639       };
2640       int regno;
2641       int offset = strlen (name) - 2;
2642
2643       for (regno = 0; regno <= 14; regno++)
2644         if (strcmp (&name[offset], table[regno]) == 0)
2645           return get_frame_register_unsigned (frame, regno);
2646     }
2647
2648   /* GNU ld generates __foo_from_arm or __foo_from_thumb for
2649      non-interworking calls to foo.  We could decode the stubs
2650      to find the target but it's easier to use the symbol table.  */
2651   namelen = strlen (name);
2652   if (name[0] == '_' && name[1] == '_'
2653       && ((namelen > 2 + strlen ("_from_thumb")
2654            && strncmp (name + namelen - strlen ("_from_thumb"), "_from_thumb",
2655                        strlen ("_from_thumb")) == 0)
2656           || (namelen > 2 + strlen ("_from_arm")
2657               && strncmp (name + namelen - strlen ("_from_arm"), "_from_arm",
2658                           strlen ("_from_arm")) == 0)))
2659     {
2660       char *target_name;
2661       int target_len = namelen - 2;
2662       struct minimal_symbol *minsym;
2663       struct objfile *objfile;
2664       struct obj_section *sec;
2665
2666       if (name[namelen - 1] == 'b')
2667         target_len -= strlen ("_from_thumb");
2668       else
2669         target_len -= strlen ("_from_arm");
2670
2671       target_name = alloca (target_len + 1);
2672       memcpy (target_name, name + 2, target_len);
2673       target_name[target_len] = '\0';
2674
2675       sec = find_pc_section (pc);
2676       objfile = (sec == NULL) ? NULL : sec->objfile;
2677       minsym = lookup_minimal_symbol (target_name, NULL, objfile);
2678       if (minsym != NULL)
2679         return SYMBOL_VALUE_ADDRESS (minsym);
2680       else
2681         return 0;
2682     }
2683
2684   return 0;                     /* not a stub */
2685 }
2686
2687 static void
2688 set_arm_command (char *args, int from_tty)
2689 {
2690   printf_unfiltered (_("\
2691 \"set arm\" must be followed by an apporpriate subcommand.\n"));
2692   help_list (setarmcmdlist, "set arm ", all_commands, gdb_stdout);
2693 }
2694
2695 static void
2696 show_arm_command (char *args, int from_tty)
2697 {
2698   cmd_show_list (showarmcmdlist, from_tty, "");
2699 }
2700
2701 static void
2702 arm_update_current_architecture (void)
2703 {
2704   struct gdbarch_info info;
2705
2706   /* If the current architecture is not ARM, we have nothing to do.  */
2707   if (gdbarch_bfd_arch_info (target_gdbarch)->arch != bfd_arch_arm)
2708     return;
2709
2710   /* Update the architecture.  */
2711   gdbarch_info_init (&info);
2712
2713   if (!gdbarch_update_p (info))
2714     internal_error (__FILE__, __LINE__, "could not update architecture");
2715 }
2716
2717 static void
2718 set_fp_model_sfunc (char *args, int from_tty,
2719                     struct cmd_list_element *c)
2720 {
2721   enum arm_float_model fp_model;
2722
2723   for (fp_model = ARM_FLOAT_AUTO; fp_model != ARM_FLOAT_LAST; fp_model++)
2724     if (strcmp (current_fp_model, fp_model_strings[fp_model]) == 0)
2725       {
2726         arm_fp_model = fp_model;
2727         break;
2728       }
2729
2730   if (fp_model == ARM_FLOAT_LAST)
2731     internal_error (__FILE__, __LINE__, _("Invalid fp model accepted: %s."),
2732                     current_fp_model);
2733
2734   arm_update_current_architecture ();
2735 }
2736
2737 static void
2738 show_fp_model (struct ui_file *file, int from_tty,
2739                struct cmd_list_element *c, const char *value)
2740 {
2741   struct gdbarch_tdep *tdep = gdbarch_tdep (target_gdbarch);
2742
2743   if (arm_fp_model == ARM_FLOAT_AUTO
2744       && gdbarch_bfd_arch_info (target_gdbarch)->arch == bfd_arch_arm)
2745     fprintf_filtered (file, _("\
2746 The current ARM floating point model is \"auto\" (currently \"%s\").\n"),
2747                       fp_model_strings[tdep->fp_model]);
2748   else
2749     fprintf_filtered (file, _("\
2750 The current ARM floating point model is \"%s\".\n"),
2751                       fp_model_strings[arm_fp_model]);
2752 }
2753
2754 static void
2755 arm_set_abi (char *args, int from_tty,
2756              struct cmd_list_element *c)
2757 {
2758   enum arm_abi_kind arm_abi;
2759
2760   for (arm_abi = ARM_ABI_AUTO; arm_abi != ARM_ABI_LAST; arm_abi++)
2761     if (strcmp (arm_abi_string, arm_abi_strings[arm_abi]) == 0)
2762       {
2763         arm_abi_global = arm_abi;
2764         break;
2765       }
2766
2767   if (arm_abi == ARM_ABI_LAST)
2768     internal_error (__FILE__, __LINE__, _("Invalid ABI accepted: %s."),
2769                     arm_abi_string);
2770
2771   arm_update_current_architecture ();
2772 }
2773
2774 static void
2775 arm_show_abi (struct ui_file *file, int from_tty,
2776              struct cmd_list_element *c, const char *value)
2777 {
2778   struct gdbarch_tdep *tdep = gdbarch_tdep (target_gdbarch);
2779
2780   if (arm_abi_global == ARM_ABI_AUTO
2781       && gdbarch_bfd_arch_info (target_gdbarch)->arch == bfd_arch_arm)
2782     fprintf_filtered (file, _("\
2783 The current ARM ABI is \"auto\" (currently \"%s\").\n"),
2784                       arm_abi_strings[tdep->arm_abi]);
2785   else
2786     fprintf_filtered (file, _("The current ARM ABI is \"%s\".\n"),
2787                       arm_abi_string);
2788 }
2789
2790 static void
2791 arm_show_fallback_mode (struct ui_file *file, int from_tty,
2792                         struct cmd_list_element *c, const char *value)
2793 {
2794   struct gdbarch_tdep *tdep = gdbarch_tdep (target_gdbarch);
2795
2796   fprintf_filtered (file, _("\
2797 The current execution mode assumed (when symbols are unavailable) is \"%s\".\n"),
2798                     arm_fallback_mode_string);
2799 }
2800
2801 static void
2802 arm_show_force_mode (struct ui_file *file, int from_tty,
2803                      struct cmd_list_element *c, const char *value)
2804 {
2805   struct gdbarch_tdep *tdep = gdbarch_tdep (target_gdbarch);
2806
2807   fprintf_filtered (file, _("\
2808 The current execution mode assumed (even when symbols are available) is \"%s\".\n"),
2809                     arm_force_mode_string);
2810 }
2811
2812 /* If the user changes the register disassembly style used for info
2813    register and other commands, we have to also switch the style used
2814    in opcodes for disassembly output.  This function is run in the "set
2815    arm disassembly" command, and does that.  */
2816
2817 static void
2818 set_disassembly_style_sfunc (char *args, int from_tty,
2819                               struct cmd_list_element *c)
2820 {
2821   set_disassembly_style ();
2822 }
2823 \f
2824 /* Return the ARM register name corresponding to register I.  */
2825 static const char *
2826 arm_register_name (struct gdbarch *gdbarch, int i)
2827 {
2828   if (i >= ARRAY_SIZE (arm_register_names))
2829     /* These registers are only supported on targets which supply
2830        an XML description.  */
2831     return "";
2832
2833   return arm_register_names[i];
2834 }
2835
2836 static void
2837 set_disassembly_style (void)
2838 {
2839   int current;
2840
2841   /* Find the style that the user wants.  */
2842   for (current = 0; current < num_disassembly_options; current++)
2843     if (disassembly_style == valid_disassembly_styles[current])
2844       break;
2845   gdb_assert (current < num_disassembly_options);
2846
2847   /* Synchronize the disassembler.  */
2848   set_arm_regname_option (current);
2849 }
2850
2851 /* Test whether the coff symbol specific value corresponds to a Thumb
2852    function.  */
2853
2854 static int
2855 coff_sym_is_thumb (int val)
2856 {
2857   return (val == C_THUMBEXT ||
2858           val == C_THUMBSTAT ||
2859           val == C_THUMBEXTFUNC ||
2860           val == C_THUMBSTATFUNC ||
2861           val == C_THUMBLABEL);
2862 }
2863
2864 /* arm_coff_make_msymbol_special()
2865    arm_elf_make_msymbol_special()
2866    
2867    These functions test whether the COFF or ELF symbol corresponds to
2868    an address in thumb code, and set a "special" bit in a minimal
2869    symbol to indicate that it does.  */
2870    
2871 static void
2872 arm_elf_make_msymbol_special(asymbol *sym, struct minimal_symbol *msym)
2873 {
2874   /* Thumb symbols are of type STT_LOPROC, (synonymous with
2875      STT_ARM_TFUNC).  */
2876   if (ELF_ST_TYPE (((elf_symbol_type *)sym)->internal_elf_sym.st_info)
2877       == STT_LOPROC)
2878     MSYMBOL_SET_SPECIAL (msym);
2879 }
2880
2881 static void
2882 arm_coff_make_msymbol_special(int val, struct minimal_symbol *msym)
2883 {
2884   if (coff_sym_is_thumb (val))
2885     MSYMBOL_SET_SPECIAL (msym);
2886 }
2887
2888 static void
2889 arm_objfile_data_cleanup (struct objfile *objfile, void *arg)
2890 {
2891   struct arm_per_objfile *data = arg;
2892   unsigned int i;
2893
2894   for (i = 0; i < objfile->obfd->section_count; i++)
2895     VEC_free (arm_mapping_symbol_s, data->section_maps[i]);
2896 }
2897
2898 static void
2899 arm_record_special_symbol (struct gdbarch *gdbarch, struct objfile *objfile,
2900                            asymbol *sym)
2901 {
2902   const char *name = bfd_asymbol_name (sym);
2903   struct arm_per_objfile *data;
2904   VEC(arm_mapping_symbol_s) **map_p;
2905   struct arm_mapping_symbol new_map_sym;
2906
2907   gdb_assert (name[0] == '$');
2908   if (name[1] != 'a' && name[1] != 't' && name[1] != 'd')
2909     return;
2910
2911   data = objfile_data (objfile, arm_objfile_data_key);
2912   if (data == NULL)
2913     {
2914       data = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2915                              struct arm_per_objfile);
2916       set_objfile_data (objfile, arm_objfile_data_key, data);
2917       data->section_maps = OBSTACK_CALLOC (&objfile->objfile_obstack,
2918                                            objfile->obfd->section_count,
2919                                            VEC(arm_mapping_symbol_s) *);
2920     }
2921   map_p = &data->section_maps[bfd_get_section (sym)->index];
2922
2923   new_map_sym.value = sym->value;
2924   new_map_sym.type = name[1];
2925
2926   /* Assume that most mapping symbols appear in order of increasing
2927      value.  If they were randomly distributed, it would be faster to
2928      always push here and then sort at first use.  */
2929   if (!VEC_empty (arm_mapping_symbol_s, *map_p))
2930     {
2931       struct arm_mapping_symbol *prev_map_sym;
2932
2933       prev_map_sym = VEC_last (arm_mapping_symbol_s, *map_p);
2934       if (prev_map_sym->value >= sym->value)
2935         {
2936           unsigned int idx;
2937           idx = VEC_lower_bound (arm_mapping_symbol_s, *map_p, &new_map_sym,
2938                                  arm_compare_mapping_symbols);
2939           VEC_safe_insert (arm_mapping_symbol_s, *map_p, idx, &new_map_sym);
2940           return;
2941         }
2942     }
2943
2944   VEC_safe_push (arm_mapping_symbol_s, *map_p, &new_map_sym);
2945 }
2946
2947 static void
2948 arm_write_pc (struct regcache *regcache, CORE_ADDR pc)
2949 {
2950   regcache_cooked_write_unsigned (regcache, ARM_PC_REGNUM, pc);
2951
2952   /* If necessary, set the T bit.  */
2953   if (arm_apcs_32)
2954     {
2955       ULONGEST val;
2956       regcache_cooked_read_unsigned (regcache, ARM_PS_REGNUM, &val);
2957       if (arm_pc_is_thumb (pc))
2958         regcache_cooked_write_unsigned (regcache, ARM_PS_REGNUM, val | CPSR_T);
2959       else
2960         regcache_cooked_write_unsigned (regcache, ARM_PS_REGNUM,
2961                                         val & ~(ULONGEST) CPSR_T);
2962     }
2963 }
2964
2965 static struct value *
2966 value_of_arm_user_reg (struct frame_info *frame, const void *baton)
2967 {
2968   const int *reg_p = baton;
2969   return value_of_register (*reg_p, frame);
2970 }
2971 \f
2972 static enum gdb_osabi
2973 arm_elf_osabi_sniffer (bfd *abfd)
2974 {
2975   unsigned int elfosabi;
2976   enum gdb_osabi osabi = GDB_OSABI_UNKNOWN;
2977
2978   elfosabi = elf_elfheader (abfd)->e_ident[EI_OSABI];
2979
2980   if (elfosabi == ELFOSABI_ARM)
2981     /* GNU tools use this value.  Check note sections in this case,
2982        as well.  */
2983     bfd_map_over_sections (abfd,
2984                            generic_elf_osabi_sniff_abi_tag_sections, 
2985                            &osabi);
2986
2987   /* Anything else will be handled by the generic ELF sniffer.  */
2988   return osabi;
2989 }
2990
2991 \f
2992 /* Initialize the current architecture based on INFO.  If possible,
2993    re-use an architecture from ARCHES, which is a list of
2994    architectures already created during this debugging session.
2995
2996    Called e.g. at program startup, when reading a core file, and when
2997    reading a binary file.  */
2998
2999 static struct gdbarch *
3000 arm_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
3001 {
3002   struct gdbarch_tdep *tdep;
3003   struct gdbarch *gdbarch;
3004   struct gdbarch_list *best_arch;
3005   enum arm_abi_kind arm_abi = arm_abi_global;
3006   enum arm_float_model fp_model = arm_fp_model;
3007   struct tdesc_arch_data *tdesc_data = NULL;
3008   int i;
3009   int have_fpa_registers = 1;
3010
3011   /* Check any target description for validity.  */
3012   if (tdesc_has_registers (info.target_desc))
3013     {
3014       /* For most registers we require GDB's default names; but also allow
3015          the numeric names for sp / lr / pc, as a convenience.  */
3016       static const char *const arm_sp_names[] = { "r13", "sp", NULL };
3017       static const char *const arm_lr_names[] = { "r14", "lr", NULL };
3018       static const char *const arm_pc_names[] = { "r15", "pc", NULL };
3019
3020       const struct tdesc_feature *feature;
3021       int i, valid_p;
3022
3023       feature = tdesc_find_feature (info.target_desc,
3024                                     "org.gnu.gdb.arm.core");
3025       if (feature == NULL)
3026         return NULL;
3027
3028       tdesc_data = tdesc_data_alloc ();
3029
3030       valid_p = 1;
3031       for (i = 0; i < ARM_SP_REGNUM; i++)
3032         valid_p &= tdesc_numbered_register (feature, tdesc_data, i,
3033                                             arm_register_names[i]);
3034       valid_p &= tdesc_numbered_register_choices (feature, tdesc_data,
3035                                                   ARM_SP_REGNUM,
3036                                                   arm_sp_names);
3037       valid_p &= tdesc_numbered_register_choices (feature, tdesc_data,
3038                                                   ARM_LR_REGNUM,
3039                                                   arm_lr_names);
3040       valid_p &= tdesc_numbered_register_choices (feature, tdesc_data,
3041                                                   ARM_PC_REGNUM,
3042                                                   arm_pc_names);
3043       valid_p &= tdesc_numbered_register (feature, tdesc_data,
3044                                           ARM_PS_REGNUM, "cpsr");
3045
3046       if (!valid_p)
3047         {
3048           tdesc_data_cleanup (tdesc_data);
3049           return NULL;
3050         }
3051
3052       feature = tdesc_find_feature (info.target_desc,
3053                                     "org.gnu.gdb.arm.fpa");
3054       if (feature != NULL)
3055         {
3056           valid_p = 1;
3057           for (i = ARM_F0_REGNUM; i <= ARM_FPS_REGNUM; i++)
3058             valid_p &= tdesc_numbered_register (feature, tdesc_data, i,
3059                                                 arm_register_names[i]);
3060           if (!valid_p)
3061             {
3062               tdesc_data_cleanup (tdesc_data);
3063               return NULL;
3064             }
3065         }
3066       else
3067         have_fpa_registers = 0;
3068
3069       feature = tdesc_find_feature (info.target_desc,
3070                                     "org.gnu.gdb.xscale.iwmmxt");
3071       if (feature != NULL)
3072         {
3073           static const char *const iwmmxt_names[] = {
3074             "wR0", "wR1", "wR2", "wR3", "wR4", "wR5", "wR6", "wR7",
3075             "wR8", "wR9", "wR10", "wR11", "wR12", "wR13", "wR14", "wR15",
3076             "wCID", "wCon", "wCSSF", "wCASF", "", "", "", "",
3077             "wCGR0", "wCGR1", "wCGR2", "wCGR3", "", "", "", "",
3078           };
3079
3080           valid_p = 1;
3081           for (i = ARM_WR0_REGNUM; i <= ARM_WR15_REGNUM; i++)
3082             valid_p
3083               &= tdesc_numbered_register (feature, tdesc_data, i,
3084                                           iwmmxt_names[i - ARM_WR0_REGNUM]);
3085
3086           /* Check for the control registers, but do not fail if they
3087              are missing.  */
3088           for (i = ARM_WC0_REGNUM; i <= ARM_WCASF_REGNUM; i++)
3089             tdesc_numbered_register (feature, tdesc_data, i,
3090                                      iwmmxt_names[i - ARM_WR0_REGNUM]);
3091
3092           for (i = ARM_WCGR0_REGNUM; i <= ARM_WCGR3_REGNUM; i++)
3093             valid_p
3094               &= tdesc_numbered_register (feature, tdesc_data, i,
3095                                           iwmmxt_names[i - ARM_WR0_REGNUM]);
3096
3097           if (!valid_p)
3098             {
3099               tdesc_data_cleanup (tdesc_data);
3100               return NULL;
3101             }
3102         }
3103     }
3104
3105   /* If we have an object to base this architecture on, try to determine
3106      its ABI.  */
3107
3108   if (arm_abi == ARM_ABI_AUTO && info.abfd != NULL)
3109     {
3110       int ei_osabi, e_flags;
3111
3112       switch (bfd_get_flavour (info.abfd))
3113         {
3114         case bfd_target_aout_flavour:
3115           /* Assume it's an old APCS-style ABI.  */
3116           arm_abi = ARM_ABI_APCS;
3117           break;
3118
3119         case bfd_target_coff_flavour:
3120           /* Assume it's an old APCS-style ABI.  */
3121           /* XXX WinCE?  */
3122           arm_abi = ARM_ABI_APCS;
3123           break;
3124
3125         case bfd_target_elf_flavour:
3126           ei_osabi = elf_elfheader (info.abfd)->e_ident[EI_OSABI];
3127           e_flags = elf_elfheader (info.abfd)->e_flags;
3128
3129           if (ei_osabi == ELFOSABI_ARM)
3130             {
3131               /* GNU tools used to use this value, but do not for EABI
3132                  objects.  There's nowhere to tag an EABI version
3133                  anyway, so assume APCS.  */
3134               arm_abi = ARM_ABI_APCS;
3135             }
3136           else if (ei_osabi == ELFOSABI_NONE)
3137             {
3138               int eabi_ver = EF_ARM_EABI_VERSION (e_flags);
3139
3140               switch (eabi_ver)
3141                 {
3142                 case EF_ARM_EABI_UNKNOWN:
3143                   /* Assume GNU tools.  */
3144                   arm_abi = ARM_ABI_APCS;
3145                   break;
3146
3147                 case EF_ARM_EABI_VER4:
3148                 case EF_ARM_EABI_VER5:
3149                   arm_abi = ARM_ABI_AAPCS;
3150                   /* EABI binaries default to VFP float ordering.  */
3151                   if (fp_model == ARM_FLOAT_AUTO)
3152                     fp_model = ARM_FLOAT_SOFT_VFP;
3153                   break;
3154
3155                 default:
3156                   /* Leave it as "auto".  */
3157                   warning (_("unknown ARM EABI version 0x%x"), eabi_ver);
3158                   break;
3159                 }
3160             }
3161
3162           if (fp_model == ARM_FLOAT_AUTO)
3163             {
3164               int e_flags = elf_elfheader (info.abfd)->e_flags;
3165
3166               switch (e_flags & (EF_ARM_SOFT_FLOAT | EF_ARM_VFP_FLOAT))
3167                 {
3168                 case 0:
3169                   /* Leave it as "auto".  Strictly speaking this case
3170                      means FPA, but almost nobody uses that now, and
3171                      many toolchains fail to set the appropriate bits
3172                      for the floating-point model they use.  */
3173                   break;
3174                 case EF_ARM_SOFT_FLOAT:
3175                   fp_model = ARM_FLOAT_SOFT_FPA;
3176                   break;
3177                 case EF_ARM_VFP_FLOAT:
3178                   fp_model = ARM_FLOAT_VFP;
3179                   break;
3180                 case EF_ARM_SOFT_FLOAT | EF_ARM_VFP_FLOAT:
3181                   fp_model = ARM_FLOAT_SOFT_VFP;
3182                   break;
3183                 }
3184             }
3185
3186           if (e_flags & EF_ARM_BE8)
3187             info.byte_order_for_code = BFD_ENDIAN_LITTLE;
3188
3189           break;
3190
3191         default:
3192           /* Leave it as "auto".  */
3193           break;
3194         }
3195     }
3196
3197   /* If there is already a candidate, use it.  */
3198   for (best_arch = gdbarch_list_lookup_by_info (arches, &info);
3199        best_arch != NULL;
3200        best_arch = gdbarch_list_lookup_by_info (best_arch->next, &info))
3201     {
3202       if (arm_abi != ARM_ABI_AUTO
3203           && arm_abi != gdbarch_tdep (best_arch->gdbarch)->arm_abi)
3204         continue;
3205
3206       if (fp_model != ARM_FLOAT_AUTO
3207           && fp_model != gdbarch_tdep (best_arch->gdbarch)->fp_model)
3208         continue;
3209
3210       /* Found a match.  */
3211       break;
3212     }
3213
3214   if (best_arch != NULL)
3215     {
3216       if (tdesc_data != NULL)
3217         tdesc_data_cleanup (tdesc_data);
3218       return best_arch->gdbarch;
3219     }
3220
3221   tdep = xcalloc (1, sizeof (struct gdbarch_tdep));
3222   gdbarch = gdbarch_alloc (&info, tdep);
3223
3224   /* Record additional information about the architecture we are defining.
3225      These are gdbarch discriminators, like the OSABI.  */
3226   tdep->arm_abi = arm_abi;
3227   tdep->fp_model = fp_model;
3228   tdep->have_fpa_registers = have_fpa_registers;
3229
3230   /* Breakpoints.  */
3231   switch (info.byte_order_for_code)
3232     {
3233     case BFD_ENDIAN_BIG:
3234       tdep->arm_breakpoint = arm_default_arm_be_breakpoint;
3235       tdep->arm_breakpoint_size = sizeof (arm_default_arm_be_breakpoint);
3236       tdep->thumb_breakpoint = arm_default_thumb_be_breakpoint;
3237       tdep->thumb_breakpoint_size = sizeof (arm_default_thumb_be_breakpoint);
3238
3239       break;
3240
3241     case BFD_ENDIAN_LITTLE:
3242       tdep->arm_breakpoint = arm_default_arm_le_breakpoint;
3243       tdep->arm_breakpoint_size = sizeof (arm_default_arm_le_breakpoint);
3244       tdep->thumb_breakpoint = arm_default_thumb_le_breakpoint;
3245       tdep->thumb_breakpoint_size = sizeof (arm_default_thumb_le_breakpoint);
3246
3247       break;
3248
3249     default:
3250       internal_error (__FILE__, __LINE__,
3251                       _("arm_gdbarch_init: bad byte order for float format"));
3252     }
3253
3254   /* On ARM targets char defaults to unsigned.  */
3255   set_gdbarch_char_signed (gdbarch, 0);
3256
3257   /* This should be low enough for everything.  */
3258   tdep->lowest_pc = 0x20;
3259   tdep->jb_pc = -1;     /* Longjump support not enabled by default.  */
3260
3261   /* The default, for both APCS and AAPCS, is to return small
3262      structures in registers.  */
3263   tdep->struct_return = reg_struct_return;
3264
3265   set_gdbarch_push_dummy_call (gdbarch, arm_push_dummy_call);
3266   set_gdbarch_frame_align (gdbarch, arm_frame_align);
3267
3268   set_gdbarch_write_pc (gdbarch, arm_write_pc);
3269
3270   /* Frame handling.  */
3271   set_gdbarch_dummy_id (gdbarch, arm_dummy_id);
3272   set_gdbarch_unwind_pc (gdbarch, arm_unwind_pc);
3273   set_gdbarch_unwind_sp (gdbarch, arm_unwind_sp);
3274
3275   frame_base_set_default (gdbarch, &arm_normal_base);
3276
3277   /* Address manipulation.  */
3278   set_gdbarch_smash_text_address (gdbarch, arm_smash_text_address);
3279   set_gdbarch_addr_bits_remove (gdbarch, arm_addr_bits_remove);
3280
3281   /* Advance PC across function entry code.  */
3282   set_gdbarch_skip_prologue (gdbarch, arm_skip_prologue);
3283
3284   /* Skip trampolines.  */
3285   set_gdbarch_skip_trampoline_code (gdbarch, arm_skip_stub);
3286
3287   /* The stack grows downward.  */
3288   set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
3289
3290   /* Breakpoint manipulation.  */
3291   set_gdbarch_breakpoint_from_pc (gdbarch, arm_breakpoint_from_pc);
3292
3293   /* Information about registers, etc.  */
3294   set_gdbarch_deprecated_fp_regnum (gdbarch, ARM_FP_REGNUM);    /* ??? */
3295   set_gdbarch_sp_regnum (gdbarch, ARM_SP_REGNUM);
3296   set_gdbarch_pc_regnum (gdbarch, ARM_PC_REGNUM);
3297   set_gdbarch_num_regs (gdbarch, ARM_NUM_REGS);
3298   set_gdbarch_register_type (gdbarch, arm_register_type);
3299
3300   /* This "info float" is FPA-specific.  Use the generic version if we
3301      do not have FPA.  */
3302   if (gdbarch_tdep (gdbarch)->have_fpa_registers)
3303     set_gdbarch_print_float_info (gdbarch, arm_print_float_info);
3304
3305   /* Internal <-> external register number maps.  */
3306   set_gdbarch_dwarf2_reg_to_regnum (gdbarch, arm_dwarf_reg_to_regnum);
3307   set_gdbarch_register_sim_regno (gdbarch, arm_register_sim_regno);
3308
3309   set_gdbarch_register_name (gdbarch, arm_register_name);
3310
3311   /* Returning results.  */
3312   set_gdbarch_return_value (gdbarch, arm_return_value);
3313
3314   /* Disassembly.  */
3315   set_gdbarch_print_insn (gdbarch, gdb_print_insn_arm);
3316
3317   /* Minsymbol frobbing.  */
3318   set_gdbarch_elf_make_msymbol_special (gdbarch, arm_elf_make_msymbol_special);
3319   set_gdbarch_coff_make_msymbol_special (gdbarch,
3320                                          arm_coff_make_msymbol_special);
3321   set_gdbarch_record_special_symbol (gdbarch, arm_record_special_symbol);
3322
3323   /* Virtual tables.  */
3324   set_gdbarch_vbit_in_delta (gdbarch, 1);
3325
3326   /* Hook in the ABI-specific overrides, if they have been registered.  */
3327   gdbarch_init_osabi (info, gdbarch);
3328
3329   dwarf2_frame_set_init_reg (gdbarch, arm_dwarf2_frame_init_reg);
3330
3331   /* Add some default predicates.  */
3332   frame_unwind_append_unwinder (gdbarch, &arm_stub_unwind);
3333   dwarf2_append_unwinders (gdbarch);
3334   frame_unwind_append_unwinder (gdbarch, &arm_prologue_unwind);
3335
3336   /* Now we have tuned the configuration, set a few final things,
3337      based on what the OS ABI has told us.  */
3338
3339   /* If the ABI is not otherwise marked, assume the old GNU APCS.  EABI
3340      binaries are always marked.  */
3341   if (tdep->arm_abi == ARM_ABI_AUTO)
3342     tdep->arm_abi = ARM_ABI_APCS;
3343
3344   /* We used to default to FPA for generic ARM, but almost nobody
3345      uses that now, and we now provide a way for the user to force
3346      the model.  So default to the most useful variant.  */
3347   if (tdep->fp_model == ARM_FLOAT_AUTO)
3348     tdep->fp_model = ARM_FLOAT_SOFT_FPA;
3349
3350   if (tdep->jb_pc >= 0)
3351     set_gdbarch_get_longjmp_target (gdbarch, arm_get_longjmp_target);
3352
3353   /* Floating point sizes and format.  */
3354   set_gdbarch_float_format (gdbarch, floatformats_ieee_single);
3355   if (tdep->fp_model == ARM_FLOAT_SOFT_FPA || tdep->fp_model == ARM_FLOAT_FPA)
3356     {
3357       set_gdbarch_double_format
3358         (gdbarch, floatformats_ieee_double_littlebyte_bigword);
3359       set_gdbarch_long_double_format
3360         (gdbarch, floatformats_ieee_double_littlebyte_bigword);
3361     }
3362   else
3363     {
3364       set_gdbarch_double_format (gdbarch, floatformats_ieee_double);
3365       set_gdbarch_long_double_format (gdbarch, floatformats_ieee_double);
3366     }
3367
3368   if (tdesc_data)
3369     tdesc_use_registers (gdbarch, info.target_desc, tdesc_data);
3370
3371   /* Add standard register aliases.  We add aliases even for those
3372      nanes which are used by the current architecture - it's simpler,
3373      and does no harm, since nothing ever lists user registers.  */
3374   for (i = 0; i < ARRAY_SIZE (arm_register_aliases); i++)
3375     user_reg_add (gdbarch, arm_register_aliases[i].name,
3376                   value_of_arm_user_reg, &arm_register_aliases[i].regnum);
3377
3378   return gdbarch;
3379 }
3380
3381 static void
3382 arm_dump_tdep (struct gdbarch *gdbarch, struct ui_file *file)
3383 {
3384   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
3385
3386   if (tdep == NULL)
3387     return;
3388
3389   fprintf_unfiltered (file, _("arm_dump_tdep: Lowest pc = 0x%lx"),
3390                       (unsigned long) tdep->lowest_pc);
3391 }
3392
3393 extern initialize_file_ftype _initialize_arm_tdep; /* -Wmissing-prototypes */
3394
3395 void
3396 _initialize_arm_tdep (void)
3397 {
3398   struct ui_file *stb;
3399   long length;
3400   struct cmd_list_element *new_set, *new_show;
3401   const char *setname;
3402   const char *setdesc;
3403   const char *const *regnames;
3404   int numregs, i, j;
3405   static char *helptext;
3406   char regdesc[1024], *rdptr = regdesc;
3407   size_t rest = sizeof (regdesc);
3408
3409   gdbarch_register (bfd_arch_arm, arm_gdbarch_init, arm_dump_tdep);
3410
3411   arm_objfile_data_key
3412     = register_objfile_data_with_cleanup (arm_objfile_data_cleanup);
3413
3414   /* Register an ELF OS ABI sniffer for ARM binaries.  */
3415   gdbarch_register_osabi_sniffer (bfd_arch_arm,
3416                                   bfd_target_elf_flavour,
3417                                   arm_elf_osabi_sniffer);
3418
3419   /* Get the number of possible sets of register names defined in opcodes.  */
3420   num_disassembly_options = get_arm_regname_num_options ();
3421
3422   /* Add root prefix command for all "set arm"/"show arm" commands.  */
3423   add_prefix_cmd ("arm", no_class, set_arm_command,
3424                   _("Various ARM-specific commands."),
3425                   &setarmcmdlist, "set arm ", 0, &setlist);
3426
3427   add_prefix_cmd ("arm", no_class, show_arm_command,
3428                   _("Various ARM-specific commands."),
3429                   &showarmcmdlist, "show arm ", 0, &showlist);
3430
3431   /* Sync the opcode insn printer with our register viewer.  */
3432   parse_arm_disassembler_option ("reg-names-std");
3433
3434   /* Initialize the array that will be passed to
3435      add_setshow_enum_cmd().  */
3436   valid_disassembly_styles
3437     = xmalloc ((num_disassembly_options + 1) * sizeof (char *));
3438   for (i = 0; i < num_disassembly_options; i++)
3439     {
3440       numregs = get_arm_regnames (i, &setname, &setdesc, &regnames);
3441       valid_disassembly_styles[i] = setname;
3442       length = snprintf (rdptr, rest, "%s - %s\n", setname, setdesc);
3443       rdptr += length;
3444       rest -= length;
3445       /* When we find the default names, tell the disassembler to use
3446          them.  */
3447       if (!strcmp (setname, "std"))
3448         {
3449           disassembly_style = setname;
3450           set_arm_regname_option (i);
3451         }
3452     }
3453   /* Mark the end of valid options.  */
3454   valid_disassembly_styles[num_disassembly_options] = NULL;
3455
3456   /* Create the help text.  */
3457   stb = mem_fileopen ();
3458   fprintf_unfiltered (stb, "%s%s%s",
3459                       _("The valid values are:\n"),
3460                       regdesc,
3461                       _("The default is \"std\"."));
3462   helptext = ui_file_xstrdup (stb, &length);
3463   ui_file_delete (stb);
3464
3465   add_setshow_enum_cmd("disassembler", no_class,
3466                        valid_disassembly_styles, &disassembly_style,
3467                        _("Set the disassembly style."),
3468                        _("Show the disassembly style."),
3469                        helptext,
3470                        set_disassembly_style_sfunc,
3471                        NULL, /* FIXME: i18n: The disassembly style is \"%s\".  */
3472                        &setarmcmdlist, &showarmcmdlist);
3473
3474   add_setshow_boolean_cmd ("apcs32", no_class, &arm_apcs_32,
3475                            _("Set usage of ARM 32-bit mode."),
3476                            _("Show usage of ARM 32-bit mode."),
3477                            _("When off, a 26-bit PC will be used."),
3478                            NULL,
3479                            NULL, /* FIXME: i18n: Usage of ARM 32-bit mode is %s.  */
3480                            &setarmcmdlist, &showarmcmdlist);
3481
3482   /* Add a command to allow the user to force the FPU model.  */
3483   add_setshow_enum_cmd ("fpu", no_class, fp_model_strings, &current_fp_model,
3484                         _("Set the floating point type."),
3485                         _("Show the floating point type."),
3486                         _("auto - Determine the FP typefrom the OS-ABI.\n\
3487 softfpa - Software FP, mixed-endian doubles on little-endian ARMs.\n\
3488 fpa - FPA co-processor (GCC compiled).\n\
3489 softvfp - Software FP with pure-endian doubles.\n\
3490 vfp - VFP co-processor."),
3491                         set_fp_model_sfunc, show_fp_model,
3492                         &setarmcmdlist, &showarmcmdlist);
3493
3494   /* Add a command to allow the user to force the ABI.  */
3495   add_setshow_enum_cmd ("abi", class_support, arm_abi_strings, &arm_abi_string,
3496                         _("Set the ABI."),
3497                         _("Show the ABI."),
3498                         NULL, arm_set_abi, arm_show_abi,
3499                         &setarmcmdlist, &showarmcmdlist);
3500
3501   /* Add two commands to allow the user to force the assumed
3502      execution mode.  */
3503   add_setshow_enum_cmd ("fallback-mode", class_support,
3504                         arm_mode_strings, &arm_fallback_mode_string,
3505                         _("Set the mode assumed when symbols are unavailable."),
3506                         _("Show the mode assumed when symbols are unavailable."),
3507                         NULL, NULL, arm_show_fallback_mode,
3508                         &setarmcmdlist, &showarmcmdlist);
3509   add_setshow_enum_cmd ("force-mode", class_support,
3510                         arm_mode_strings, &arm_force_mode_string,
3511                         _("Set the mode assumed even when symbols are available."),
3512                         _("Show the mode assumed even when symbols are available."),
3513                         NULL, NULL, arm_show_force_mode,
3514                         &setarmcmdlist, &showarmcmdlist);
3515
3516   /* Debugging flag.  */
3517   add_setshow_boolean_cmd ("arm", class_maintenance, &arm_debug,
3518                            _("Set ARM debugging."),
3519                            _("Show ARM debugging."),
3520                            _("When on, arm-specific debugging is enabled."),
3521                            NULL,
3522                            NULL, /* FIXME: i18n: "ARM debugging is %s.  */
3523                            &setdebuglist, &showdebuglist);
3524 }