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