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