1 /* Common target dependent code for GDB on ARM systems.
3 Copyright (C) 1988, 1989, 1991, 1992, 1993, 1995, 1996, 1998, 1999, 2000,
4 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
5 Free Software Foundation, Inc.
7 This file is part of GDB.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
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.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
22 #include <ctype.h> /* XXX for isupper () */
29 #include "gdb_string.h"
30 #include "dis-asm.h" /* For register styles. */
34 #include "arch-utils.h"
36 #include "frame-unwind.h"
37 #include "frame-base.h"
38 #include "trad-frame.h"
40 #include "dwarf2-frame.h"
42 #include "prologue-value.h"
43 #include "target-descriptions.h"
44 #include "user-regs.h"
47 #include "gdb/sim-arm.h"
50 #include "coff/internal.h"
53 #include "gdb_assert.h"
58 /* Macros for setting and testing a bit in a minimal symbol that marks
59 it as Thumb function. The MSB of the minimal symbol's "info" field
60 is used for this purpose.
62 MSYMBOL_SET_SPECIAL Actually sets the "special" bit.
63 MSYMBOL_IS_SPECIAL Tests the "special" bit in a minimal symbol. */
65 #define MSYMBOL_SET_SPECIAL(msym) \
66 MSYMBOL_TARGET_FLAG_1 (msym) = 1
68 #define MSYMBOL_IS_SPECIAL(msym) \
69 MSYMBOL_TARGET_FLAG_1 (msym)
71 /* Per-objfile data used for mapping symbols. */
72 static const struct objfile_data *arm_objfile_data_key;
74 struct arm_mapping_symbol
79 typedef struct arm_mapping_symbol arm_mapping_symbol_s;
80 DEF_VEC_O(arm_mapping_symbol_s);
82 struct arm_per_objfile
84 VEC(arm_mapping_symbol_s) **section_maps;
87 /* The list of available "set arm ..." and "show arm ..." commands. */
88 static struct cmd_list_element *setarmcmdlist = NULL;
89 static struct cmd_list_element *showarmcmdlist = NULL;
91 /* The type of floating-point to use. Keep this in sync with enum
92 arm_float_model, and the help string in _initialize_arm_tdep. */
93 static const char *fp_model_strings[] =
103 /* A variable that can be configured by the user. */
104 static enum arm_float_model arm_fp_model = ARM_FLOAT_AUTO;
105 static const char *current_fp_model = "auto";
107 /* The ABI to use. Keep this in sync with arm_abi_kind. */
108 static const char *arm_abi_strings[] =
116 /* A variable that can be configured by the user. */
117 static enum arm_abi_kind arm_abi_global = ARM_ABI_AUTO;
118 static const char *arm_abi_string = "auto";
120 /* The execution mode to assume. */
121 static const char *arm_mode_strings[] =
128 static const char *arm_fallback_mode_string = "auto";
129 static const char *arm_force_mode_string = "auto";
131 /* Number of different reg name sets (options). */
132 static int num_disassembly_options;
134 /* The standard register names, and all the valid aliases for them. */
139 } arm_register_aliases[] = {
140 /* Basic register numbers. */
157 /* Synonyms (argument and variable registers). */
170 /* Other platform-specific names for r9. */
178 /* Names used by GCC (not listed in the ARM EABI). */
181 /* A special name from the older ATPCS. */
185 static const char *const arm_register_names[] =
186 {"r0", "r1", "r2", "r3", /* 0 1 2 3 */
187 "r4", "r5", "r6", "r7", /* 4 5 6 7 */
188 "r8", "r9", "r10", "r11", /* 8 9 10 11 */
189 "r12", "sp", "lr", "pc", /* 12 13 14 15 */
190 "f0", "f1", "f2", "f3", /* 16 17 18 19 */
191 "f4", "f5", "f6", "f7", /* 20 21 22 23 */
192 "fps", "cpsr" }; /* 24 25 */
194 /* Valid register name styles. */
195 static const char **valid_disassembly_styles;
197 /* Disassembly style to use. Default to "std" register names. */
198 static const char *disassembly_style;
200 /* This is used to keep the bfd arch_info in sync with the disassembly
202 static void set_disassembly_style_sfunc(char *, int,
203 struct cmd_list_element *);
204 static void set_disassembly_style (void);
206 static void convert_from_extended (const struct floatformat *, const void *,
208 static void convert_to_extended (const struct floatformat *, void *,
211 static void arm_neon_quad_read (struct gdbarch *gdbarch,
212 struct regcache *regcache,
213 int regnum, gdb_byte *buf);
214 static void arm_neon_quad_write (struct gdbarch *gdbarch,
215 struct regcache *regcache,
216 int regnum, const gdb_byte *buf);
218 struct arm_prologue_cache
220 /* The stack pointer at the time this frame was created; i.e. the
221 caller's stack pointer when this function was called. It is used
222 to identify this frame. */
225 /* The frame base for this frame is just prev_sp - frame size.
226 FRAMESIZE is the distance from the frame pointer to the
227 initial stack pointer. */
231 /* The register used to hold the frame pointer for this frame. */
234 /* Saved register offsets. */
235 struct trad_frame_saved_reg *saved_regs;
238 static CORE_ADDR arm_analyze_prologue (struct gdbarch *gdbarch,
239 CORE_ADDR prologue_start,
240 CORE_ADDR prologue_end,
241 struct arm_prologue_cache *cache);
243 /* Architecture version for displaced stepping. This effects the behaviour of
244 certain instructions, and really should not be hard-wired. */
246 #define DISPLACED_STEPPING_ARCH_VERSION 5
248 /* Addresses for calling Thumb functions have the bit 0 set.
249 Here are some macros to test, set, or clear bit 0 of addresses. */
250 #define IS_THUMB_ADDR(addr) ((addr) & 1)
251 #define MAKE_THUMB_ADDR(addr) ((addr) | 1)
252 #define UNMAKE_THUMB_ADDR(addr) ((addr) & ~1)
254 /* Set to true if the 32-bit mode is in use. */
258 /* Determine if FRAME is executing in Thumb mode. */
261 arm_frame_is_thumb (struct frame_info *frame)
265 /* Every ARM frame unwinder can unwind the T bit of the CPSR, either
266 directly (from a signal frame or dummy frame) or by interpreting
267 the saved LR (from a prologue or DWARF frame). So consult it and
268 trust the unwinders. */
269 cpsr = get_frame_register_unsigned (frame, ARM_PS_REGNUM);
271 return (cpsr & CPSR_T) != 0;
274 /* Callback for VEC_lower_bound. */
277 arm_compare_mapping_symbols (const struct arm_mapping_symbol *lhs,
278 const struct arm_mapping_symbol *rhs)
280 return lhs->value < rhs->value;
283 /* Search for the mapping symbol covering MEMADDR. If one is found,
284 return its type. Otherwise, return 0. If START is non-NULL,
285 set *START to the location of the mapping symbol. */
288 arm_find_mapping_symbol (CORE_ADDR memaddr, CORE_ADDR *start)
290 struct obj_section *sec;
292 /* If there are mapping symbols, consult them. */
293 sec = find_pc_section (memaddr);
296 struct arm_per_objfile *data;
297 VEC(arm_mapping_symbol_s) *map;
298 struct arm_mapping_symbol map_key = { memaddr - obj_section_addr (sec),
302 data = objfile_data (sec->objfile, arm_objfile_data_key);
305 map = data->section_maps[sec->the_bfd_section->index];
306 if (!VEC_empty (arm_mapping_symbol_s, map))
308 struct arm_mapping_symbol *map_sym;
310 idx = VEC_lower_bound (arm_mapping_symbol_s, map, &map_key,
311 arm_compare_mapping_symbols);
313 /* VEC_lower_bound finds the earliest ordered insertion
314 point. If the following symbol starts at this exact
315 address, we use that; otherwise, the preceding
316 mapping symbol covers this address. */
317 if (idx < VEC_length (arm_mapping_symbol_s, map))
319 map_sym = VEC_index (arm_mapping_symbol_s, map, idx);
320 if (map_sym->value == map_key.value)
323 *start = map_sym->value + obj_section_addr (sec);
324 return map_sym->type;
330 map_sym = VEC_index (arm_mapping_symbol_s, map, idx - 1);
332 *start = map_sym->value + obj_section_addr (sec);
333 return map_sym->type;
342 static CORE_ADDR arm_get_next_pc_raw (struct frame_info *frame,
343 CORE_ADDR pc, int insert_bkpt);
345 /* Determine if the program counter specified in MEMADDR is in a Thumb
346 function. This function should be called for addresses unrelated to
347 any executing frame; otherwise, prefer arm_frame_is_thumb. */
350 arm_pc_is_thumb (CORE_ADDR memaddr)
352 struct obj_section *sec;
353 struct minimal_symbol *sym;
356 /* If bit 0 of the address is set, assume this is a Thumb address. */
357 if (IS_THUMB_ADDR (memaddr))
360 /* If the user wants to override the symbol table, let him. */
361 if (strcmp (arm_force_mode_string, "arm") == 0)
363 if (strcmp (arm_force_mode_string, "thumb") == 0)
366 /* If there are mapping symbols, consult them. */
367 type = arm_find_mapping_symbol (memaddr, NULL);
371 /* Thumb functions have a "special" bit set in minimal symbols. */
372 sym = lookup_minimal_symbol_by_pc (memaddr);
374 return (MSYMBOL_IS_SPECIAL (sym));
376 /* If the user wants to override the fallback mode, let them. */
377 if (strcmp (arm_fallback_mode_string, "arm") == 0)
379 if (strcmp (arm_fallback_mode_string, "thumb") == 0)
382 /* If we couldn't find any symbol, but we're talking to a running
383 target, then trust the current value of $cpsr. This lets
384 "display/i $pc" always show the correct mode (though if there is
385 a symbol table we will not reach here, so it still may not be
386 displayed in the mode it will be executed).
388 As a further heuristic if we detect that we are doing a single-step we
389 see what state executing the current instruction ends up with us being
391 if (target_has_registers)
393 struct frame_info *current_frame = get_current_frame ();
394 CORE_ADDR current_pc = get_frame_pc (current_frame);
395 int is_thumb = arm_frame_is_thumb (current_frame);
397 if (memaddr == current_pc)
401 struct gdbarch *gdbarch = get_frame_arch (current_frame);
402 next_pc = arm_get_next_pc_raw (current_frame, current_pc, FALSE);
403 if (memaddr == gdbarch_addr_bits_remove (gdbarch, next_pc))
404 return IS_THUMB_ADDR (next_pc);
410 /* Otherwise we're out of luck; we assume ARM. */
414 /* Remove useless bits from addresses in a running program. */
416 arm_addr_bits_remove (struct gdbarch *gdbarch, CORE_ADDR val)
419 return UNMAKE_THUMB_ADDR (val);
421 return (val & 0x03fffffc);
424 /* When reading symbols, we need to zap the low bit of the address,
425 which may be set to 1 for Thumb functions. */
427 arm_smash_text_address (struct gdbarch *gdbarch, CORE_ADDR val)
432 /* Return 1 if PC is the start of a compiler helper function which
433 can be safely ignored during prologue skipping. */
435 skip_prologue_function (CORE_ADDR pc)
437 struct minimal_symbol *msym;
440 msym = lookup_minimal_symbol_by_pc (pc);
441 if (msym == NULL || SYMBOL_VALUE_ADDRESS (msym) != pc)
444 name = SYMBOL_LINKAGE_NAME (msym);
448 /* The GNU linker's Thumb call stub to foo is named
450 if (strstr (name, "_from_thumb") != NULL)
453 /* On soft-float targets, __truncdfsf2 is called to convert promoted
454 arguments to their argument types in non-prototyped
456 if (strncmp (name, "__truncdfsf2", strlen ("__truncdfsf2")) == 0)
458 if (strncmp (name, "__aeabi_d2f", strlen ("__aeabi_d2f")) == 0)
464 /* Support routines for instruction parsing. */
465 #define submask(x) ((1L << ((x) + 1)) - 1)
466 #define bit(obj,st) (((obj) >> (st)) & 1)
467 #define bits(obj,st,fn) (((obj) >> (st)) & submask ((fn) - (st)))
468 #define sbits(obj,st,fn) \
469 ((long) (bits(obj,st,fn) | ((long) bit(obj,fn) * ~ submask (fn - st))))
470 #define BranchDest(addr,instr) \
471 ((CORE_ADDR) (((long) (addr)) + 8 + (sbits (instr, 0, 23) << 2)))
473 /* Analyze a Thumb prologue, looking for a recognizable stack frame
474 and frame pointer. Scan until we encounter a store that could
475 clobber the stack frame unexpectedly, or an unknown instruction.
476 Return the last address which is definitely safe to skip for an
477 initial breakpoint. */
480 thumb_analyze_prologue (struct gdbarch *gdbarch,
481 CORE_ADDR start, CORE_ADDR limit,
482 struct arm_prologue_cache *cache)
484 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
485 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
488 struct pv_area *stack;
489 struct cleanup *back_to;
492 for (i = 0; i < 16; i++)
493 regs[i] = pv_register (i, 0);
494 stack = make_pv_area (ARM_SP_REGNUM, gdbarch_addr_bit (gdbarch));
495 back_to = make_cleanup_free_pv_area (stack);
497 while (start < limit)
501 insn = read_memory_unsigned_integer (start, 2, byte_order_for_code);
503 if ((insn & 0xfe00) == 0xb400) /* push { rlist } */
508 if (pv_area_store_would_trash (stack, regs[ARM_SP_REGNUM]))
511 /* Bits 0-7 contain a mask for registers R0-R7. Bit 8 says
512 whether to save LR (R14). */
513 mask = (insn & 0xff) | ((insn & 0x100) << 6);
515 /* Calculate offsets of saved R0-R7 and LR. */
516 for (regno = ARM_LR_REGNUM; regno >= 0; regno--)
517 if (mask & (1 << regno))
519 regs[ARM_SP_REGNUM] = pv_add_constant (regs[ARM_SP_REGNUM],
521 pv_area_store (stack, regs[ARM_SP_REGNUM], 4, regs[regno]);
524 else if ((insn & 0xff00) == 0xb000) /* add sp, #simm OR
527 offset = (insn & 0x7f) << 2; /* get scaled offset */
528 if (insn & 0x80) /* Check for SUB. */
529 regs[ARM_SP_REGNUM] = pv_add_constant (regs[ARM_SP_REGNUM],
532 regs[ARM_SP_REGNUM] = pv_add_constant (regs[ARM_SP_REGNUM],
535 else if ((insn & 0xf800) == 0xa800) /* add Rd, sp, #imm */
536 regs[bits (insn, 8, 10)] = pv_add_constant (regs[ARM_SP_REGNUM],
538 else if ((insn & 0xfe00) == 0x1c00 /* add Rd, Rn, #imm */
539 && pv_is_register (regs[bits (insn, 3, 5)], ARM_SP_REGNUM))
540 regs[bits (insn, 0, 2)] = pv_add_constant (regs[bits (insn, 3, 5)],
542 else if ((insn & 0xf800) == 0x3000 /* add Rd, #imm */
543 && pv_is_register (regs[bits (insn, 8, 10)], ARM_SP_REGNUM))
544 regs[bits (insn, 8, 10)] = pv_add_constant (regs[bits (insn, 8, 10)],
546 else if ((insn & 0xfe00) == 0x1800 /* add Rd, Rn, Rm */
547 && pv_is_register (regs[bits (insn, 6, 8)], ARM_SP_REGNUM)
548 && pv_is_constant (regs[bits (insn, 3, 5)]))
549 regs[bits (insn, 0, 2)] = pv_add (regs[bits (insn, 3, 5)],
550 regs[bits (insn, 6, 8)]);
551 else if ((insn & 0xff00) == 0x4400 /* add Rd, Rm */
552 && pv_is_constant (regs[bits (insn, 3, 6)]))
554 int rd = (bit (insn, 7) << 3) + bits (insn, 0, 2);
555 int rm = bits (insn, 3, 6);
556 regs[rd] = pv_add (regs[rd], regs[rm]);
558 else if ((insn & 0xff00) == 0x4600) /* mov hi, lo or mov lo, hi */
560 int dst_reg = (insn & 0x7) + ((insn & 0x80) >> 4);
561 int src_reg = (insn & 0x78) >> 3;
562 regs[dst_reg] = regs[src_reg];
564 else if ((insn & 0xf800) == 0x9000) /* str rd, [sp, #off] */
566 /* Handle stores to the stack. Normally pushes are used,
567 but with GCC -mtpcs-frame, there may be other stores
568 in the prologue to create the frame. */
569 int regno = (insn >> 8) & 0x7;
572 offset = (insn & 0xff) << 2;
573 addr = pv_add_constant (regs[ARM_SP_REGNUM], offset);
575 if (pv_area_store_would_trash (stack, addr))
578 pv_area_store (stack, addr, 4, regs[regno]);
580 else if ((insn & 0xf800) == 0x6000) /* str rd, [rn, #off] */
582 int rd = bits (insn, 0, 2);
583 int rn = bits (insn, 3, 5);
586 offset = bits (insn, 6, 10) << 2;
587 addr = pv_add_constant (regs[rn], offset);
589 if (pv_area_store_would_trash (stack, addr))
592 pv_area_store (stack, addr, 4, regs[rd]);
594 else if (((insn & 0xf800) == 0x7000 /* strb Rd, [Rn, #off] */
595 || (insn & 0xf800) == 0x8000) /* strh Rd, [Rn, #off] */
596 && pv_is_register (regs[bits (insn, 3, 5)], ARM_SP_REGNUM))
597 /* Ignore stores of argument registers to the stack. */
599 else if ((insn & 0xf800) == 0xc800 /* ldmia Rn!, { registers } */
600 && pv_is_register (regs[bits (insn, 8, 10)], ARM_SP_REGNUM))
601 /* Ignore block loads from the stack, potentially copying
602 parameters from memory. */
604 else if ((insn & 0xf800) == 0x9800 /* ldr Rd, [Rn, #immed] */
605 || ((insn & 0xf800) == 0x6800 /* ldr Rd, [sp, #immed] */
606 && pv_is_register (regs[bits (insn, 3, 5)], ARM_SP_REGNUM)))
607 /* Similarly ignore single loads from the stack. */
609 else if ((insn & 0xffc0) == 0x0000 /* lsls Rd, Rm, #0 */
610 || (insn & 0xffc0) == 0x1c00) /* add Rd, Rn, #0 */
611 /* Skip register copies, i.e. saves to another register
612 instead of the stack. */
614 else if ((insn & 0xf800) == 0x2000) /* movs Rd, #imm */
615 /* Recognize constant loads; even with small stacks these are necessary
617 regs[bits (insn, 8, 10)] = pv_constant (bits (insn, 0, 7));
618 else if ((insn & 0xf800) == 0x4800) /* ldr Rd, [pc, #imm] */
620 /* Constant pool loads, for the same reason. */
621 unsigned int constant;
624 loc = start + 4 + bits (insn, 0, 7) * 4;
625 constant = read_memory_unsigned_integer (loc, 4, byte_order);
626 regs[bits (insn, 8, 10)] = pv_constant (constant);
628 else if ((insn & 0xe000) == 0xe000 && cache == NULL)
630 /* Only recognize 32-bit instructions for prologue skipping. */
631 unsigned short inst2;
633 inst2 = read_memory_unsigned_integer (start + 2, 2,
634 byte_order_for_code);
636 if ((insn & 0xf800) == 0xf000 && (inst2 & 0xe800) == 0xe800)
638 /* BL, BLX. Allow some special function calls when
639 skipping the prologue; GCC generates these before
640 storing arguments to the stack. */
642 int j1, j2, imm1, imm2;
644 imm1 = sbits (insn, 0, 10);
645 imm2 = bits (inst2, 0, 10);
646 j1 = bit (inst2, 13);
647 j2 = bit (inst2, 11);
649 offset = ((imm1 << 12) + (imm2 << 1));
650 offset ^= ((!j2) << 22) | ((!j1) << 23);
652 nextpc = start + 4 + offset;
653 /* For BLX make sure to clear the low bits. */
654 if (bit (inst2, 12) == 0)
655 nextpc = nextpc & 0xfffffffc;
657 if (!skip_prologue_function (nextpc))
660 else if ((insn & 0xfe50) == 0xe800 /* stm{db,ia} Rn[!], { registers } */
661 && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
663 else if ((insn & 0xfe50) == 0xe840 /* strd Rt, Rt2, [Rn, #imm] */
664 && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
666 else if ((insn & 0xffd0) == 0xe890 /* ldmia Rn[!], { registers } */
667 && (inst2 & 0x8000) == 0x0000
668 && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
670 else if ((insn & 0xfbf0) == 0xf100 /* add.w Rd, Rn, #imm */
671 && (inst2 & 0x8000) == 0x0000)
672 /* Since we only recognize this for prologue skipping, do not bother
673 to compute the constant. */
674 regs[bits (inst2, 8, 11)] = regs[bits (insn, 0, 3)];
675 else if ((insn & 0xfbf0) == 0xf1a0 /* sub.w Rd, Rn, #imm12 */
676 && (inst2 & 0x8000) == 0x0000)
677 /* Since we only recognize this for prologue skipping, do not bother
678 to compute the constant. */
679 regs[bits (inst2, 8, 11)] = regs[bits (insn, 0, 3)];
680 else if ((insn & 0xfbf0) == 0xf2a0 /* sub.w Rd, Rn, #imm8 */
681 && (inst2 & 0x8000) == 0x0000)
682 /* Since we only recognize this for prologue skipping, do not bother
683 to compute the constant. */
684 regs[bits (inst2, 8, 11)] = regs[bits (insn, 0, 3)];
685 else if ((insn & 0xff50) == 0xf850 /* ldr.w Rd, [Rn, #imm]{!} */
686 && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
688 else if ((insn & 0xff50) == 0xe950 /* ldrd Rt, Rt2, [Rn, #imm]{!} */
689 && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
691 else if ((insn & 0xff50) == 0xf800 /* strb.w or strh.w */
692 && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
696 /* We don't know what this instruction is. We're finished
697 scanning. NOTE: Recognizing more safe-to-ignore
698 instructions here will improve support for optimized
707 /* We don't know what this instruction is. We're finished
708 scanning. NOTE: Recognizing more safe-to-ignore
709 instructions here will improve support for optimized
718 fprintf_unfiltered (gdb_stdlog, "Prologue scan stopped at %s\n",
719 paddress (gdbarch, start));
723 do_cleanups (back_to);
727 if (pv_is_register (regs[ARM_FP_REGNUM], ARM_SP_REGNUM))
729 /* Frame pointer is fp. Frame size is constant. */
730 cache->framereg = ARM_FP_REGNUM;
731 cache->framesize = -regs[ARM_FP_REGNUM].k;
733 else if (pv_is_register (regs[THUMB_FP_REGNUM], ARM_SP_REGNUM))
735 /* Frame pointer is r7. Frame size is constant. */
736 cache->framereg = THUMB_FP_REGNUM;
737 cache->framesize = -regs[THUMB_FP_REGNUM].k;
739 else if (pv_is_register (regs[ARM_SP_REGNUM], ARM_SP_REGNUM))
741 /* Try the stack pointer... this is a bit desperate. */
742 cache->framereg = ARM_SP_REGNUM;
743 cache->framesize = -regs[ARM_SP_REGNUM].k;
747 /* We're just out of luck. We don't know where the frame is. */
748 cache->framereg = -1;
749 cache->framesize = 0;
752 for (i = 0; i < 16; i++)
753 if (pv_area_find_reg (stack, gdbarch, i, &offset))
754 cache->saved_regs[i].addr = offset;
756 do_cleanups (back_to);
760 /* Advance the PC across any function entry prologue instructions to
761 reach some "real" code.
763 The APCS (ARM Procedure Call Standard) defines the following
767 [stmfd sp!, {a1,a2,a3,a4}]
768 stmfd sp!, {...,fp,ip,lr,pc}
769 [stfe f7, [sp, #-12]!]
770 [stfe f6, [sp, #-12]!]
771 [stfe f5, [sp, #-12]!]
772 [stfe f4, [sp, #-12]!]
773 sub fp, ip, #nn @@ nn == 20 or 4 depending on second insn */
776 arm_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR pc)
778 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
781 CORE_ADDR func_addr, limit_pc;
782 struct symtab_and_line sal;
784 /* See if we can determine the end of the prologue via the symbol table.
785 If so, then return either PC, or the PC after the prologue, whichever
787 if (find_pc_partial_function (pc, NULL, &func_addr, NULL))
789 CORE_ADDR post_prologue_pc
790 = skip_prologue_using_sal (gdbarch, func_addr);
791 struct symtab *s = find_pc_symtab (func_addr);
793 /* GCC always emits a line note before the prologue and another
794 one after, even if the two are at the same address or on the
795 same line. Take advantage of this so that we do not need to
796 know every instruction that might appear in the prologue. We
797 will have producer information for most binaries; if it is
798 missing (e.g. for -gstabs), assuming the GNU tools. */
801 || s->producer == NULL
802 || strncmp (s->producer, "GNU ", sizeof ("GNU ") - 1) == 0))
803 return post_prologue_pc;
805 if (post_prologue_pc != 0)
807 CORE_ADDR analyzed_limit;
809 /* For non-GCC compilers, make sure the entire line is an
810 acceptable prologue; GDB will round this function's
811 return value up to the end of the following line so we
812 can not skip just part of a line (and we do not want to).
814 RealView does not treat the prologue specially, but does
815 associate prologue code with the opening brace; so this
816 lets us skip the first line if we think it is the opening
818 if (arm_pc_is_thumb (func_addr))
819 analyzed_limit = thumb_analyze_prologue (gdbarch, func_addr,
820 post_prologue_pc, NULL);
822 analyzed_limit = arm_analyze_prologue (gdbarch, func_addr,
823 post_prologue_pc, NULL);
825 if (analyzed_limit != post_prologue_pc)
828 return post_prologue_pc;
832 /* Can't determine prologue from the symbol table, need to examine
835 /* Find an upper limit on the function prologue using the debug
836 information. If the debug information could not be used to provide
837 that bound, then use an arbitrary large number as the upper bound. */
838 /* Like arm_scan_prologue, stop no later than pc + 64. */
839 limit_pc = skip_prologue_using_sal (gdbarch, pc);
841 limit_pc = pc + 64; /* Magic. */
844 /* Check if this is Thumb code. */
845 if (arm_pc_is_thumb (pc))
846 return thumb_analyze_prologue (gdbarch, pc, limit_pc, NULL);
848 for (skip_pc = pc; skip_pc < limit_pc; skip_pc += 4)
850 inst = read_memory_unsigned_integer (skip_pc, 4, byte_order_for_code);
852 /* "mov ip, sp" is no longer a required part of the prologue. */
853 if (inst == 0xe1a0c00d) /* mov ip, sp */
856 if ((inst & 0xfffff000) == 0xe28dc000) /* add ip, sp #n */
859 if ((inst & 0xfffff000) == 0xe24dc000) /* sub ip, sp #n */
862 /* Some prologues begin with "str lr, [sp, #-4]!". */
863 if (inst == 0xe52de004) /* str lr, [sp, #-4]! */
866 if ((inst & 0xfffffff0) == 0xe92d0000) /* stmfd sp!,{a1,a2,a3,a4} */
869 if ((inst & 0xfffff800) == 0xe92dd800) /* stmfd sp!,{fp,ip,lr,pc} */
872 /* Any insns after this point may float into the code, if it makes
873 for better instruction scheduling, so we skip them only if we
874 find them, but still consider the function to be frame-ful. */
876 /* We may have either one sfmfd instruction here, or several stfe
877 insns, depending on the version of floating point code we
879 if ((inst & 0xffbf0fff) == 0xec2d0200) /* sfmfd fn, <cnt>, [sp]! */
882 if ((inst & 0xffff8fff) == 0xed6d0103) /* stfe fn, [sp, #-12]! */
885 if ((inst & 0xfffff000) == 0xe24cb000) /* sub fp, ip, #nn */
888 if ((inst & 0xfffff000) == 0xe24dd000) /* sub sp, sp, #nn */
891 if ((inst & 0xffffc000) == 0xe54b0000 /* strb r(0123),[r11,#-nn] */
892 || (inst & 0xffffc0f0) == 0xe14b00b0 /* strh r(0123),[r11,#-nn] */
893 || (inst & 0xffffc000) == 0xe50b0000) /* str r(0123),[r11,#-nn] */
896 if ((inst & 0xffffc000) == 0xe5cd0000 /* strb r(0123),[sp,#nn] */
897 || (inst & 0xffffc0f0) == 0xe1cd00b0 /* strh r(0123),[sp,#nn] */
898 || (inst & 0xffffc000) == 0xe58d0000) /* str r(0123),[sp,#nn] */
901 /* Un-recognized instruction; stop scanning. */
905 return skip_pc; /* End of prologue */
909 /* Function: thumb_scan_prologue (helper function for arm_scan_prologue)
910 This function decodes a Thumb function prologue to determine:
911 1) the size of the stack frame
912 2) which registers are saved on it
913 3) the offsets of saved regs
914 4) the offset from the stack pointer to the frame pointer
916 A typical Thumb function prologue would create this stack frame
917 (offsets relative to FP)
918 old SP -> 24 stack parameters
921 R7 -> 0 local variables (16 bytes)
922 SP -> -12 additional stack space (12 bytes)
923 The frame size would thus be 36 bytes, and the frame offset would be
924 12 bytes. The frame register is R7.
926 The comments for thumb_skip_prolog() describe the algorithm we use
927 to detect the end of the prolog. */
931 thumb_scan_prologue (struct gdbarch *gdbarch, CORE_ADDR prev_pc,
932 CORE_ADDR block_addr, struct arm_prologue_cache *cache)
934 CORE_ADDR prologue_start;
935 CORE_ADDR prologue_end;
936 CORE_ADDR current_pc;
938 if (find_pc_partial_function (block_addr, NULL, &prologue_start,
941 struct symtab_and_line sal = find_pc_line (prologue_start, 0);
943 if (sal.line == 0) /* no line info, use current PC */
944 prologue_end = prev_pc;
945 else if (sal.end < prologue_end) /* next line begins after fn end */
946 prologue_end = sal.end; /* (probably means no prologue) */
949 /* We're in the boondocks: we have no idea where the start of the
953 prologue_end = min (prologue_end, prev_pc);
955 thumb_analyze_prologue (gdbarch, prologue_start, prologue_end, cache);
958 /* Return 1 if THIS_INSTR might change control flow, 0 otherwise. */
961 arm_instruction_changes_pc (uint32_t this_instr)
963 if (bits (this_instr, 28, 31) == INST_NV)
964 /* Unconditional instructions. */
965 switch (bits (this_instr, 24, 27))
969 /* Branch with Link and change to Thumb. */
974 /* Coprocessor register transfer. */
975 if (bits (this_instr, 12, 15) == 15)
976 error (_("Invalid update to pc in instruction"));
982 switch (bits (this_instr, 25, 27))
985 if (bits (this_instr, 23, 24) == 2 && bit (this_instr, 20) == 0)
987 /* Multiplies and extra load/stores. */
988 if (bit (this_instr, 4) == 1 && bit (this_instr, 7) == 1)
989 /* Neither multiplies nor extension load/stores are allowed
993 /* Otherwise, miscellaneous instructions. */
995 /* BX <reg>, BXJ <reg>, BLX <reg> */
996 if (bits (this_instr, 4, 27) == 0x12fff1
997 || bits (this_instr, 4, 27) == 0x12fff2
998 || bits (this_instr, 4, 27) == 0x12fff3)
1001 /* Other miscellaneous instructions are unpredictable if they
1005 /* Data processing instruction. Fall through. */
1008 if (bits (this_instr, 12, 15) == 15)
1015 /* Media instructions and architecturally undefined instructions. */
1016 if (bits (this_instr, 25, 27) == 3 && bit (this_instr, 4) == 1)
1020 if (bit (this_instr, 20) == 0)
1024 if (bits (this_instr, 12, 15) == ARM_PC_REGNUM)
1030 /* Load/store multiple. */
1031 if (bit (this_instr, 20) == 1 && bit (this_instr, 15) == 1)
1037 /* Branch and branch with link. */
1042 /* Coprocessor transfers or SWIs can not affect PC. */
1046 internal_error (__FILE__, __LINE__, "bad value in switch");
1050 /* Analyze an ARM mode prologue starting at PROLOGUE_START and
1051 continuing no further than PROLOGUE_END. If CACHE is non-NULL,
1052 fill it in. Return the first address not recognized as a prologue
1055 We recognize all the instructions typically found in ARM prologues,
1056 plus harmless instructions which can be skipped (either for analysis
1057 purposes, or a more restrictive set that can be skipped when finding
1058 the end of the prologue). */
1061 arm_analyze_prologue (struct gdbarch *gdbarch,
1062 CORE_ADDR prologue_start, CORE_ADDR prologue_end,
1063 struct arm_prologue_cache *cache)
1065 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1066 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
1068 CORE_ADDR offset, current_pc;
1069 pv_t regs[ARM_FPS_REGNUM];
1070 struct pv_area *stack;
1071 struct cleanup *back_to;
1072 int framereg, framesize;
1073 CORE_ADDR unrecognized_pc = 0;
1075 /* Search the prologue looking for instructions that set up the
1076 frame pointer, adjust the stack pointer, and save registers.
1078 Be careful, however, and if it doesn't look like a prologue,
1079 don't try to scan it. If, for instance, a frameless function
1080 begins with stmfd sp!, then we will tell ourselves there is
1081 a frame, which will confuse stack traceback, as well as "finish"
1082 and other operations that rely on a knowledge of the stack
1085 for (regno = 0; regno < ARM_FPS_REGNUM; regno++)
1086 regs[regno] = pv_register (regno, 0);
1087 stack = make_pv_area (ARM_SP_REGNUM, gdbarch_addr_bit (gdbarch));
1088 back_to = make_cleanup_free_pv_area (stack);
1090 for (current_pc = prologue_start;
1091 current_pc < prologue_end;
1095 = read_memory_unsigned_integer (current_pc, 4, byte_order_for_code);
1097 if (insn == 0xe1a0c00d) /* mov ip, sp */
1099 regs[ARM_IP_REGNUM] = regs[ARM_SP_REGNUM];
1102 else if ((insn & 0xfff00000) == 0xe2800000 /* add Rd, Rn, #n */
1103 && pv_is_register (regs[bits (insn, 16, 19)], ARM_SP_REGNUM))
1105 unsigned imm = insn & 0xff; /* immediate value */
1106 unsigned rot = (insn & 0xf00) >> 7; /* rotate amount */
1107 int rd = bits (insn, 12, 15);
1108 imm = (imm >> rot) | (imm << (32 - rot));
1109 regs[rd] = pv_add_constant (regs[bits (insn, 16, 19)], imm);
1112 else if ((insn & 0xfff00000) == 0xe2400000 /* sub Rd, Rn, #n */
1113 && pv_is_register (regs[bits (insn, 16, 19)], ARM_SP_REGNUM))
1115 unsigned imm = insn & 0xff; /* immediate value */
1116 unsigned rot = (insn & 0xf00) >> 7; /* rotate amount */
1117 int rd = bits (insn, 12, 15);
1118 imm = (imm >> rot) | (imm << (32 - rot));
1119 regs[rd] = pv_add_constant (regs[bits (insn, 16, 19)], -imm);
1122 else if ((insn & 0xffff0fff) == 0xe52d0004) /* str Rd, [sp, #-4]! */
1124 if (pv_area_store_would_trash (stack, regs[ARM_SP_REGNUM]))
1126 regs[ARM_SP_REGNUM] = pv_add_constant (regs[ARM_SP_REGNUM], -4);
1127 pv_area_store (stack, regs[ARM_SP_REGNUM], 4,
1128 regs[bits (insn, 12, 15)]);
1131 else if ((insn & 0xffff0000) == 0xe92d0000)
1132 /* stmfd sp!, {..., fp, ip, lr, pc}
1134 stmfd sp!, {a1, a2, a3, a4} */
1136 int mask = insn & 0xffff;
1138 if (pv_area_store_would_trash (stack, regs[ARM_SP_REGNUM]))
1141 /* Calculate offsets of saved registers. */
1142 for (regno = ARM_PC_REGNUM; regno >= 0; regno--)
1143 if (mask & (1 << regno))
1145 regs[ARM_SP_REGNUM] = pv_add_constant (regs[ARM_SP_REGNUM], -4);
1146 pv_area_store (stack, regs[ARM_SP_REGNUM], 4, regs[regno]);
1149 else if ((insn & 0xffff0000) == 0xe54b0000 /* strb rx,[r11,#-n] */
1150 || (insn & 0xffff00f0) == 0xe14b00b0 /* strh rx,[r11,#-n] */
1151 || (insn & 0xffffc000) == 0xe50b0000) /* str rx,[r11,#-n] */
1153 /* No need to add this to saved_regs -- it's just an arg reg. */
1156 else if ((insn & 0xffff0000) == 0xe5cd0000 /* strb rx,[sp,#n] */
1157 || (insn & 0xffff00f0) == 0xe1cd00b0 /* strh rx,[sp,#n] */
1158 || (insn & 0xffffc000) == 0xe58d0000) /* str rx,[sp,#n] */
1160 /* No need to add this to saved_regs -- it's just an arg reg. */
1163 else if ((insn & 0xfff00000) == 0xe8800000 /* stm Rn, { registers } */
1164 && pv_is_register (regs[bits (insn, 16, 19)], ARM_SP_REGNUM))
1166 /* No need to add this to saved_regs -- it's just arg regs. */
1169 else if ((insn & 0xfffff000) == 0xe24cb000) /* sub fp, ip #n */
1171 unsigned imm = insn & 0xff; /* immediate value */
1172 unsigned rot = (insn & 0xf00) >> 7; /* rotate amount */
1173 imm = (imm >> rot) | (imm << (32 - rot));
1174 regs[ARM_FP_REGNUM] = pv_add_constant (regs[ARM_IP_REGNUM], -imm);
1176 else if ((insn & 0xfffff000) == 0xe24dd000) /* sub sp, sp #n */
1178 unsigned imm = insn & 0xff; /* immediate value */
1179 unsigned rot = (insn & 0xf00) >> 7; /* rotate amount */
1180 imm = (imm >> rot) | (imm << (32 - rot));
1181 regs[ARM_SP_REGNUM] = pv_add_constant (regs[ARM_SP_REGNUM], -imm);
1183 else if ((insn & 0xffff7fff) == 0xed6d0103 /* stfe f?, [sp, -#c]! */
1184 && gdbarch_tdep (gdbarch)->have_fpa_registers)
1186 if (pv_area_store_would_trash (stack, regs[ARM_SP_REGNUM]))
1189 regs[ARM_SP_REGNUM] = pv_add_constant (regs[ARM_SP_REGNUM], -12);
1190 regno = ARM_F0_REGNUM + ((insn >> 12) & 0x07);
1191 pv_area_store (stack, regs[ARM_SP_REGNUM], 12, regs[regno]);
1193 else if ((insn & 0xffbf0fff) == 0xec2d0200 /* sfmfd f0, 4, [sp!] */
1194 && gdbarch_tdep (gdbarch)->have_fpa_registers)
1196 int n_saved_fp_regs;
1197 unsigned int fp_start_reg, fp_bound_reg;
1199 if (pv_area_store_would_trash (stack, regs[ARM_SP_REGNUM]))
1202 if ((insn & 0x800) == 0x800) /* N0 is set */
1204 if ((insn & 0x40000) == 0x40000) /* N1 is set */
1205 n_saved_fp_regs = 3;
1207 n_saved_fp_regs = 1;
1211 if ((insn & 0x40000) == 0x40000) /* N1 is set */
1212 n_saved_fp_regs = 2;
1214 n_saved_fp_regs = 4;
1217 fp_start_reg = ARM_F0_REGNUM + ((insn >> 12) & 0x7);
1218 fp_bound_reg = fp_start_reg + n_saved_fp_regs;
1219 for (; fp_start_reg < fp_bound_reg; fp_start_reg++)
1221 regs[ARM_SP_REGNUM] = pv_add_constant (regs[ARM_SP_REGNUM], -12);
1222 pv_area_store (stack, regs[ARM_SP_REGNUM], 12,
1223 regs[fp_start_reg++]);
1226 else if ((insn & 0xff000000) == 0xeb000000 && cache == NULL) /* bl */
1228 /* Allow some special function calls when skipping the
1229 prologue; GCC generates these before storing arguments to
1231 CORE_ADDR dest = BranchDest (current_pc, insn);
1233 if (skip_prologue_function (dest))
1238 else if ((insn & 0xf0000000) != 0xe0000000)
1239 break; /* Condition not true, exit early */
1240 else if (arm_instruction_changes_pc (insn))
1241 /* Don't scan past anything that might change control flow. */
1243 else if ((insn & 0xfe500000) == 0xe8100000) /* ldm */
1245 /* Ignore block loads from the stack, potentially copying
1246 parameters from memory. */
1247 if (pv_is_register (regs[bits (insn, 16, 19)], ARM_SP_REGNUM))
1252 else if ((insn & 0xfc500000) == 0xe4100000)
1254 /* Similarly ignore single loads from the stack. */
1255 if (pv_is_register (regs[bits (insn, 16, 19)], ARM_SP_REGNUM))
1260 else if ((insn & 0xffff0ff0) == 0xe1a00000)
1261 /* MOV Rd, Rm. Skip register copies, i.e. saves to another
1262 register instead of the stack. */
1266 /* The optimizer might shove anything into the prologue,
1267 so we just skip what we don't recognize. */
1268 unrecognized_pc = current_pc;
1273 if (unrecognized_pc == 0)
1274 unrecognized_pc = current_pc;
1276 /* The frame size is just the distance from the frame register
1277 to the original stack pointer. */
1278 if (pv_is_register (regs[ARM_FP_REGNUM], ARM_SP_REGNUM))
1280 /* Frame pointer is fp. */
1281 framereg = ARM_FP_REGNUM;
1282 framesize = -regs[ARM_FP_REGNUM].k;
1284 else if (pv_is_register (regs[ARM_SP_REGNUM], ARM_SP_REGNUM))
1286 /* Try the stack pointer... this is a bit desperate. */
1287 framereg = ARM_SP_REGNUM;
1288 framesize = -regs[ARM_SP_REGNUM].k;
1292 /* We're just out of luck. We don't know where the frame is. */
1299 cache->framereg = framereg;
1300 cache->framesize = framesize;
1302 for (regno = 0; regno < ARM_FPS_REGNUM; regno++)
1303 if (pv_area_find_reg (stack, gdbarch, regno, &offset))
1304 cache->saved_regs[regno].addr = offset;
1308 fprintf_unfiltered (gdb_stdlog, "Prologue scan stopped at %s\n",
1309 paddress (gdbarch, unrecognized_pc));
1311 do_cleanups (back_to);
1312 return unrecognized_pc;
1316 arm_scan_prologue (struct frame_info *this_frame,
1317 struct arm_prologue_cache *cache)
1319 struct gdbarch *gdbarch = get_frame_arch (this_frame);
1320 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1322 CORE_ADDR prologue_start, prologue_end, current_pc;
1323 CORE_ADDR prev_pc = get_frame_pc (this_frame);
1324 CORE_ADDR block_addr = get_frame_address_in_block (this_frame);
1325 pv_t regs[ARM_FPS_REGNUM];
1326 struct pv_area *stack;
1327 struct cleanup *back_to;
1330 /* Assume there is no frame until proven otherwise. */
1331 cache->framereg = ARM_SP_REGNUM;
1332 cache->framesize = 0;
1334 /* Check for Thumb prologue. */
1335 if (arm_frame_is_thumb (this_frame))
1337 thumb_scan_prologue (gdbarch, prev_pc, block_addr, cache);
1341 /* Find the function prologue. If we can't find the function in
1342 the symbol table, peek in the stack frame to find the PC. */
1343 if (find_pc_partial_function (block_addr, NULL, &prologue_start,
1346 /* One way to find the end of the prologue (which works well
1347 for unoptimized code) is to do the following:
1349 struct symtab_and_line sal = find_pc_line (prologue_start, 0);
1352 prologue_end = prev_pc;
1353 else if (sal.end < prologue_end)
1354 prologue_end = sal.end;
1356 This mechanism is very accurate so long as the optimizer
1357 doesn't move any instructions from the function body into the
1358 prologue. If this happens, sal.end will be the last
1359 instruction in the first hunk of prologue code just before
1360 the first instruction that the scheduler has moved from
1361 the body to the prologue.
1363 In order to make sure that we scan all of the prologue
1364 instructions, we use a slightly less accurate mechanism which
1365 may scan more than necessary. To help compensate for this
1366 lack of accuracy, the prologue scanning loop below contains
1367 several clauses which'll cause the loop to terminate early if
1368 an implausible prologue instruction is encountered.
1374 is a suitable endpoint since it accounts for the largest
1375 possible prologue plus up to five instructions inserted by
1378 if (prologue_end > prologue_start + 64)
1380 prologue_end = prologue_start + 64; /* See above. */
1385 /* We have no symbol information. Our only option is to assume this
1386 function has a standard stack frame and the normal frame register.
1387 Then, we can find the value of our frame pointer on entrance to
1388 the callee (or at the present moment if this is the innermost frame).
1389 The value stored there should be the address of the stmfd + 8. */
1390 CORE_ADDR frame_loc;
1391 LONGEST return_value;
1393 frame_loc = get_frame_register_unsigned (this_frame, ARM_FP_REGNUM);
1394 if (!safe_read_memory_integer (frame_loc, 4, byte_order, &return_value))
1398 prologue_start = gdbarch_addr_bits_remove
1399 (gdbarch, return_value) - 8;
1400 prologue_end = prologue_start + 64; /* See above. */
1404 if (prev_pc < prologue_end)
1405 prologue_end = prev_pc;
1407 arm_analyze_prologue (gdbarch, prologue_start, prologue_end, cache);
1410 static struct arm_prologue_cache *
1411 arm_make_prologue_cache (struct frame_info *this_frame)
1414 struct arm_prologue_cache *cache;
1415 CORE_ADDR unwound_fp;
1417 cache = FRAME_OBSTACK_ZALLOC (struct arm_prologue_cache);
1418 cache->saved_regs = trad_frame_alloc_saved_regs (this_frame);
1420 arm_scan_prologue (this_frame, cache);
1422 unwound_fp = get_frame_register_unsigned (this_frame, cache->framereg);
1423 if (unwound_fp == 0)
1426 cache->prev_sp = unwound_fp + cache->framesize;
1428 /* Calculate actual addresses of saved registers using offsets
1429 determined by arm_scan_prologue. */
1430 for (reg = 0; reg < gdbarch_num_regs (get_frame_arch (this_frame)); reg++)
1431 if (trad_frame_addr_p (cache->saved_regs, reg))
1432 cache->saved_regs[reg].addr += cache->prev_sp;
1437 /* Our frame ID for a normal frame is the current function's starting PC
1438 and the caller's SP when we were called. */
1441 arm_prologue_this_id (struct frame_info *this_frame,
1443 struct frame_id *this_id)
1445 struct arm_prologue_cache *cache;
1449 if (*this_cache == NULL)
1450 *this_cache = arm_make_prologue_cache (this_frame);
1451 cache = *this_cache;
1453 /* This is meant to halt the backtrace at "_start". */
1454 pc = get_frame_pc (this_frame);
1455 if (pc <= gdbarch_tdep (get_frame_arch (this_frame))->lowest_pc)
1458 /* If we've hit a wall, stop. */
1459 if (cache->prev_sp == 0)
1462 func = get_frame_func (this_frame);
1463 id = frame_id_build (cache->prev_sp, func);
1467 static struct value *
1468 arm_prologue_prev_register (struct frame_info *this_frame,
1472 struct gdbarch *gdbarch = get_frame_arch (this_frame);
1473 struct arm_prologue_cache *cache;
1475 if (*this_cache == NULL)
1476 *this_cache = arm_make_prologue_cache (this_frame);
1477 cache = *this_cache;
1479 /* If we are asked to unwind the PC, then we need to return the LR
1480 instead. The prologue may save PC, but it will point into this
1481 frame's prologue, not the next frame's resume location. Also
1482 strip the saved T bit. A valid LR may have the low bit set, but
1483 a valid PC never does. */
1484 if (prev_regnum == ARM_PC_REGNUM)
1488 lr = frame_unwind_register_unsigned (this_frame, ARM_LR_REGNUM);
1489 return frame_unwind_got_constant (this_frame, prev_regnum,
1490 arm_addr_bits_remove (gdbarch, lr));
1493 /* SP is generally not saved to the stack, but this frame is
1494 identified by the next frame's stack pointer at the time of the call.
1495 The value was already reconstructed into PREV_SP. */
1496 if (prev_regnum == ARM_SP_REGNUM)
1497 return frame_unwind_got_constant (this_frame, prev_regnum, cache->prev_sp);
1499 /* The CPSR may have been changed by the call instruction and by the
1500 called function. The only bit we can reconstruct is the T bit,
1501 by checking the low bit of LR as of the call. This is a reliable
1502 indicator of Thumb-ness except for some ARM v4T pre-interworking
1503 Thumb code, which could get away with a clear low bit as long as
1504 the called function did not use bx. Guess that all other
1505 bits are unchanged; the condition flags are presumably lost,
1506 but the processor status is likely valid. */
1507 if (prev_regnum == ARM_PS_REGNUM)
1511 cpsr = get_frame_register_unsigned (this_frame, prev_regnum);
1512 lr = frame_unwind_register_unsigned (this_frame, ARM_LR_REGNUM);
1513 if (IS_THUMB_ADDR (lr))
1517 return frame_unwind_got_constant (this_frame, prev_regnum, cpsr);
1520 return trad_frame_get_prev_register (this_frame, cache->saved_regs,
1524 struct frame_unwind arm_prologue_unwind = {
1526 arm_prologue_this_id,
1527 arm_prologue_prev_register,
1529 default_frame_sniffer
1532 static struct arm_prologue_cache *
1533 arm_make_stub_cache (struct frame_info *this_frame)
1535 struct arm_prologue_cache *cache;
1537 cache = FRAME_OBSTACK_ZALLOC (struct arm_prologue_cache);
1538 cache->saved_regs = trad_frame_alloc_saved_regs (this_frame);
1540 cache->prev_sp = get_frame_register_unsigned (this_frame, ARM_SP_REGNUM);
1545 /* Our frame ID for a stub frame is the current SP and LR. */
1548 arm_stub_this_id (struct frame_info *this_frame,
1550 struct frame_id *this_id)
1552 struct arm_prologue_cache *cache;
1554 if (*this_cache == NULL)
1555 *this_cache = arm_make_stub_cache (this_frame);
1556 cache = *this_cache;
1558 *this_id = frame_id_build (cache->prev_sp, get_frame_pc (this_frame));
1562 arm_stub_unwind_sniffer (const struct frame_unwind *self,
1563 struct frame_info *this_frame,
1564 void **this_prologue_cache)
1566 CORE_ADDR addr_in_block;
1569 addr_in_block = get_frame_address_in_block (this_frame);
1570 if (in_plt_section (addr_in_block, NULL)
1571 /* We also use the stub winder if the target memory is unreadable
1572 to avoid having the prologue unwinder trying to read it. */
1573 || target_read_memory (get_frame_pc (this_frame), dummy, 4) != 0)
1579 struct frame_unwind arm_stub_unwind = {
1582 arm_prologue_prev_register,
1584 arm_stub_unwind_sniffer
1588 arm_normal_frame_base (struct frame_info *this_frame, void **this_cache)
1590 struct arm_prologue_cache *cache;
1592 if (*this_cache == NULL)
1593 *this_cache = arm_make_prologue_cache (this_frame);
1594 cache = *this_cache;
1596 return cache->prev_sp - cache->framesize;
1599 struct frame_base arm_normal_base = {
1600 &arm_prologue_unwind,
1601 arm_normal_frame_base,
1602 arm_normal_frame_base,
1603 arm_normal_frame_base
1606 /* Assuming THIS_FRAME is a dummy, return the frame ID of that
1607 dummy frame. The frame ID's base needs to match the TOS value
1608 saved by save_dummy_frame_tos() and returned from
1609 arm_push_dummy_call, and the PC needs to match the dummy frame's
1612 static struct frame_id
1613 arm_dummy_id (struct gdbarch *gdbarch, struct frame_info *this_frame)
1615 return frame_id_build (get_frame_register_unsigned (this_frame, ARM_SP_REGNUM),
1616 get_frame_pc (this_frame));
1619 /* Given THIS_FRAME, find the previous frame's resume PC (which will
1620 be used to construct the previous frame's ID, after looking up the
1621 containing function). */
1624 arm_unwind_pc (struct gdbarch *gdbarch, struct frame_info *this_frame)
1627 pc = frame_unwind_register_unsigned (this_frame, ARM_PC_REGNUM);
1628 return arm_addr_bits_remove (gdbarch, pc);
1632 arm_unwind_sp (struct gdbarch *gdbarch, struct frame_info *this_frame)
1634 return frame_unwind_register_unsigned (this_frame, ARM_SP_REGNUM);
1637 static struct value *
1638 arm_dwarf2_prev_register (struct frame_info *this_frame, void **this_cache,
1641 struct gdbarch * gdbarch = get_frame_arch (this_frame);
1647 /* The PC is normally copied from the return column, which
1648 describes saves of LR. However, that version may have an
1649 extra bit set to indicate Thumb state. The bit is not
1651 lr = frame_unwind_register_unsigned (this_frame, ARM_LR_REGNUM);
1652 return frame_unwind_got_constant (this_frame, regnum,
1653 arm_addr_bits_remove (gdbarch, lr));
1656 /* Reconstruct the T bit; see arm_prologue_prev_register for details. */
1657 cpsr = get_frame_register_unsigned (this_frame, regnum);
1658 lr = frame_unwind_register_unsigned (this_frame, ARM_LR_REGNUM);
1659 if (IS_THUMB_ADDR (lr))
1663 return frame_unwind_got_constant (this_frame, regnum, cpsr);
1666 internal_error (__FILE__, __LINE__,
1667 _("Unexpected register %d"), regnum);
1672 arm_dwarf2_frame_init_reg (struct gdbarch *gdbarch, int regnum,
1673 struct dwarf2_frame_state_reg *reg,
1674 struct frame_info *this_frame)
1680 reg->how = DWARF2_FRAME_REG_FN;
1681 reg->loc.fn = arm_dwarf2_prev_register;
1684 reg->how = DWARF2_FRAME_REG_CFA;
1689 /* When arguments must be pushed onto the stack, they go on in reverse
1690 order. The code below implements a FILO (stack) to do this. */
1695 struct stack_item *prev;
1699 static struct stack_item *
1700 push_stack_item (struct stack_item *prev, const void *contents, int len)
1702 struct stack_item *si;
1703 si = xmalloc (sizeof (struct stack_item));
1704 si->data = xmalloc (len);
1707 memcpy (si->data, contents, len);
1711 static struct stack_item *
1712 pop_stack_item (struct stack_item *si)
1714 struct stack_item *dead = si;
1722 /* Return the alignment (in bytes) of the given type. */
1725 arm_type_align (struct type *t)
1731 t = check_typedef (t);
1732 switch (TYPE_CODE (t))
1735 /* Should never happen. */
1736 internal_error (__FILE__, __LINE__, _("unknown type alignment"));
1740 case TYPE_CODE_ENUM:
1744 case TYPE_CODE_RANGE:
1745 case TYPE_CODE_BITSTRING:
1747 case TYPE_CODE_CHAR:
1748 case TYPE_CODE_BOOL:
1749 return TYPE_LENGTH (t);
1751 case TYPE_CODE_ARRAY:
1752 case TYPE_CODE_COMPLEX:
1753 /* TODO: What about vector types? */
1754 return arm_type_align (TYPE_TARGET_TYPE (t));
1756 case TYPE_CODE_STRUCT:
1757 case TYPE_CODE_UNION:
1759 for (n = 0; n < TYPE_NFIELDS (t); n++)
1761 falign = arm_type_align (TYPE_FIELD_TYPE (t, n));
1769 /* Possible base types for a candidate for passing and returning in
1772 enum arm_vfp_cprc_base_type
1781 /* The length of one element of base type B. */
1784 arm_vfp_cprc_unit_length (enum arm_vfp_cprc_base_type b)
1788 case VFP_CPRC_SINGLE:
1790 case VFP_CPRC_DOUBLE:
1792 case VFP_CPRC_VEC64:
1794 case VFP_CPRC_VEC128:
1797 internal_error (__FILE__, __LINE__, _("Invalid VFP CPRC type: %d."),
1802 /* The character ('s', 'd' or 'q') for the type of VFP register used
1803 for passing base type B. */
1806 arm_vfp_cprc_reg_char (enum arm_vfp_cprc_base_type b)
1810 case VFP_CPRC_SINGLE:
1812 case VFP_CPRC_DOUBLE:
1814 case VFP_CPRC_VEC64:
1816 case VFP_CPRC_VEC128:
1819 internal_error (__FILE__, __LINE__, _("Invalid VFP CPRC type: %d."),
1824 /* Determine whether T may be part of a candidate for passing and
1825 returning in VFP registers, ignoring the limit on the total number
1826 of components. If *BASE_TYPE is VFP_CPRC_UNKNOWN, set it to the
1827 classification of the first valid component found; if it is not
1828 VFP_CPRC_UNKNOWN, all components must have the same classification
1829 as *BASE_TYPE. If it is found that T contains a type not permitted
1830 for passing and returning in VFP registers, a type differently
1831 classified from *BASE_TYPE, or two types differently classified
1832 from each other, return -1, otherwise return the total number of
1833 base-type elements found (possibly 0 in an empty structure or
1834 array). Vectors and complex types are not currently supported,
1835 matching the generic AAPCS support. */
1838 arm_vfp_cprc_sub_candidate (struct type *t,
1839 enum arm_vfp_cprc_base_type *base_type)
1841 t = check_typedef (t);
1842 switch (TYPE_CODE (t))
1845 switch (TYPE_LENGTH (t))
1848 if (*base_type == VFP_CPRC_UNKNOWN)
1849 *base_type = VFP_CPRC_SINGLE;
1850 else if (*base_type != VFP_CPRC_SINGLE)
1855 if (*base_type == VFP_CPRC_UNKNOWN)
1856 *base_type = VFP_CPRC_DOUBLE;
1857 else if (*base_type != VFP_CPRC_DOUBLE)
1866 case TYPE_CODE_ARRAY:
1870 count = arm_vfp_cprc_sub_candidate (TYPE_TARGET_TYPE (t), base_type);
1873 if (TYPE_LENGTH (t) == 0)
1875 gdb_assert (count == 0);
1878 else if (count == 0)
1880 unitlen = arm_vfp_cprc_unit_length (*base_type);
1881 gdb_assert ((TYPE_LENGTH (t) % unitlen) == 0);
1882 return TYPE_LENGTH (t) / unitlen;
1886 case TYPE_CODE_STRUCT:
1891 for (i = 0; i < TYPE_NFIELDS (t); i++)
1893 int sub_count = arm_vfp_cprc_sub_candidate (TYPE_FIELD_TYPE (t, i),
1895 if (sub_count == -1)
1899 if (TYPE_LENGTH (t) == 0)
1901 gdb_assert (count == 0);
1904 else if (count == 0)
1906 unitlen = arm_vfp_cprc_unit_length (*base_type);
1907 if (TYPE_LENGTH (t) != unitlen * count)
1912 case TYPE_CODE_UNION:
1917 for (i = 0; i < TYPE_NFIELDS (t); i++)
1919 int sub_count = arm_vfp_cprc_sub_candidate (TYPE_FIELD_TYPE (t, i),
1921 if (sub_count == -1)
1923 count = (count > sub_count ? count : sub_count);
1925 if (TYPE_LENGTH (t) == 0)
1927 gdb_assert (count == 0);
1930 else if (count == 0)
1932 unitlen = arm_vfp_cprc_unit_length (*base_type);
1933 if (TYPE_LENGTH (t) != unitlen * count)
1945 /* Determine whether T is a VFP co-processor register candidate (CPRC)
1946 if passed to or returned from a non-variadic function with the VFP
1947 ABI in effect. Return 1 if it is, 0 otherwise. If it is, set
1948 *BASE_TYPE to the base type for T and *COUNT to the number of
1949 elements of that base type before returning. */
1952 arm_vfp_call_candidate (struct type *t, enum arm_vfp_cprc_base_type *base_type,
1955 enum arm_vfp_cprc_base_type b = VFP_CPRC_UNKNOWN;
1956 int c = arm_vfp_cprc_sub_candidate (t, &b);
1957 if (c <= 0 || c > 4)
1964 /* Return 1 if the VFP ABI should be used for passing arguments to and
1965 returning values from a function of type FUNC_TYPE, 0
1969 arm_vfp_abi_for_function (struct gdbarch *gdbarch, struct type *func_type)
1971 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1972 /* Variadic functions always use the base ABI. Assume that functions
1973 without debug info are not variadic. */
1974 if (func_type && TYPE_VARARGS (check_typedef (func_type)))
1976 /* The VFP ABI is only supported as a variant of AAPCS. */
1977 if (tdep->arm_abi != ARM_ABI_AAPCS)
1979 return gdbarch_tdep (gdbarch)->fp_model == ARM_FLOAT_VFP;
1982 /* We currently only support passing parameters in integer registers, which
1983 conforms with GCC's default model, and VFP argument passing following
1984 the VFP variant of AAPCS. Several other variants exist and
1985 we should probably support some of them based on the selected ABI. */
1988 arm_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
1989 struct regcache *regcache, CORE_ADDR bp_addr, int nargs,
1990 struct value **args, CORE_ADDR sp, int struct_return,
1991 CORE_ADDR struct_addr)
1993 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1997 struct stack_item *si = NULL;
2000 unsigned vfp_regs_free = (1 << 16) - 1;
2002 /* Determine the type of this function and whether the VFP ABI
2004 ftype = check_typedef (value_type (function));
2005 if (TYPE_CODE (ftype) == TYPE_CODE_PTR)
2006 ftype = check_typedef (TYPE_TARGET_TYPE (ftype));
2007 use_vfp_abi = arm_vfp_abi_for_function (gdbarch, ftype);
2009 /* Set the return address. For the ARM, the return breakpoint is
2010 always at BP_ADDR. */
2011 if (arm_pc_is_thumb (bp_addr))
2013 regcache_cooked_write_unsigned (regcache, ARM_LR_REGNUM, bp_addr);
2015 /* Walk through the list of args and determine how large a temporary
2016 stack is required. Need to take care here as structs may be
2017 passed on the stack, and we have to to push them. */
2020 argreg = ARM_A1_REGNUM;
2023 /* The struct_return pointer occupies the first parameter
2024 passing register. */
2028 fprintf_unfiltered (gdb_stdlog, "struct return in %s = %s\n",
2029 gdbarch_register_name (gdbarch, argreg),
2030 paddress (gdbarch, struct_addr));
2031 regcache_cooked_write_unsigned (regcache, argreg, struct_addr);
2035 for (argnum = 0; argnum < nargs; argnum++)
2038 struct type *arg_type;
2039 struct type *target_type;
2040 enum type_code typecode;
2041 const bfd_byte *val;
2043 enum arm_vfp_cprc_base_type vfp_base_type;
2045 int may_use_core_reg = 1;
2047 arg_type = check_typedef (value_type (args[argnum]));
2048 len = TYPE_LENGTH (arg_type);
2049 target_type = TYPE_TARGET_TYPE (arg_type);
2050 typecode = TYPE_CODE (arg_type);
2051 val = value_contents (args[argnum]);
2053 align = arm_type_align (arg_type);
2054 /* Round alignment up to a whole number of words. */
2055 align = (align + INT_REGISTER_SIZE - 1) & ~(INT_REGISTER_SIZE - 1);
2056 /* Different ABIs have different maximum alignments. */
2057 if (gdbarch_tdep (gdbarch)->arm_abi == ARM_ABI_APCS)
2059 /* The APCS ABI only requires word alignment. */
2060 align = INT_REGISTER_SIZE;
2064 /* The AAPCS requires at most doubleword alignment. */
2065 if (align > INT_REGISTER_SIZE * 2)
2066 align = INT_REGISTER_SIZE * 2;
2070 && arm_vfp_call_candidate (arg_type, &vfp_base_type,
2078 /* Because this is a CPRC it cannot go in a core register or
2079 cause a core register to be skipped for alignment.
2080 Either it goes in VFP registers and the rest of this loop
2081 iteration is skipped for this argument, or it goes on the
2082 stack (and the stack alignment code is correct for this
2084 may_use_core_reg = 0;
2086 unit_length = arm_vfp_cprc_unit_length (vfp_base_type);
2087 shift = unit_length / 4;
2088 mask = (1 << (shift * vfp_base_count)) - 1;
2089 for (regno = 0; regno < 16; regno += shift)
2090 if (((vfp_regs_free >> regno) & mask) == mask)
2099 vfp_regs_free &= ~(mask << regno);
2100 reg_scaled = regno / shift;
2101 reg_char = arm_vfp_cprc_reg_char (vfp_base_type);
2102 for (i = 0; i < vfp_base_count; i++)
2106 if (reg_char == 'q')
2107 arm_neon_quad_write (gdbarch, regcache, reg_scaled + i,
2108 val + i * unit_length);
2111 sprintf (name_buf, "%c%d", reg_char, reg_scaled + i);
2112 regnum = user_reg_map_name_to_regnum (gdbarch, name_buf,
2114 regcache_cooked_write (regcache, regnum,
2115 val + i * unit_length);
2122 /* This CPRC could not go in VFP registers, so all VFP
2123 registers are now marked as used. */
2128 /* Push stack padding for dowubleword alignment. */
2129 if (nstack & (align - 1))
2131 si = push_stack_item (si, val, INT_REGISTER_SIZE);
2132 nstack += INT_REGISTER_SIZE;
2135 /* Doubleword aligned quantities must go in even register pairs. */
2136 if (may_use_core_reg
2137 && argreg <= ARM_LAST_ARG_REGNUM
2138 && align > INT_REGISTER_SIZE
2142 /* If the argument is a pointer to a function, and it is a
2143 Thumb function, create a LOCAL copy of the value and set
2144 the THUMB bit in it. */
2145 if (TYPE_CODE_PTR == typecode
2146 && target_type != NULL
2147 && TYPE_CODE_FUNC == TYPE_CODE (target_type))
2149 CORE_ADDR regval = extract_unsigned_integer (val, len, byte_order);
2150 if (arm_pc_is_thumb (regval))
2152 bfd_byte *copy = alloca (len);
2153 store_unsigned_integer (copy, len, byte_order,
2154 MAKE_THUMB_ADDR (regval));
2159 /* Copy the argument to general registers or the stack in
2160 register-sized pieces. Large arguments are split between
2161 registers and stack. */
2164 int partial_len = len < INT_REGISTER_SIZE ? len : INT_REGISTER_SIZE;
2166 if (may_use_core_reg && argreg <= ARM_LAST_ARG_REGNUM)
2168 /* The argument is being passed in a general purpose
2171 = extract_unsigned_integer (val, partial_len, byte_order);
2172 if (byte_order == BFD_ENDIAN_BIG)
2173 regval <<= (INT_REGISTER_SIZE - partial_len) * 8;
2175 fprintf_unfiltered (gdb_stdlog, "arg %d in %s = 0x%s\n",
2177 gdbarch_register_name
2179 phex (regval, INT_REGISTER_SIZE));
2180 regcache_cooked_write_unsigned (regcache, argreg, regval);
2185 /* Push the arguments onto the stack. */
2187 fprintf_unfiltered (gdb_stdlog, "arg %d @ sp + %d\n",
2189 si = push_stack_item (si, val, INT_REGISTER_SIZE);
2190 nstack += INT_REGISTER_SIZE;
2197 /* If we have an odd number of words to push, then decrement the stack
2198 by one word now, so first stack argument will be dword aligned. */
2205 write_memory (sp, si->data, si->len);
2206 si = pop_stack_item (si);
2209 /* Finally, update teh SP register. */
2210 regcache_cooked_write_unsigned (regcache, ARM_SP_REGNUM, sp);
2216 /* Always align the frame to an 8-byte boundary. This is required on
2217 some platforms and harmless on the rest. */
2220 arm_frame_align (struct gdbarch *gdbarch, CORE_ADDR sp)
2222 /* Align the stack to eight bytes. */
2223 return sp & ~ (CORE_ADDR) 7;
2227 print_fpu_flags (int flags)
2229 if (flags & (1 << 0))
2230 fputs ("IVO ", stdout);
2231 if (flags & (1 << 1))
2232 fputs ("DVZ ", stdout);
2233 if (flags & (1 << 2))
2234 fputs ("OFL ", stdout);
2235 if (flags & (1 << 3))
2236 fputs ("UFL ", stdout);
2237 if (flags & (1 << 4))
2238 fputs ("INX ", stdout);
2242 /* Print interesting information about the floating point processor
2243 (if present) or emulator. */
2245 arm_print_float_info (struct gdbarch *gdbarch, struct ui_file *file,
2246 struct frame_info *frame, const char *args)
2248 unsigned long status = get_frame_register_unsigned (frame, ARM_FPS_REGNUM);
2251 type = (status >> 24) & 127;
2252 if (status & (1 << 31))
2253 printf (_("Hardware FPU type %d\n"), type);
2255 printf (_("Software FPU type %d\n"), type);
2256 /* i18n: [floating point unit] mask */
2257 fputs (_("mask: "), stdout);
2258 print_fpu_flags (status >> 16);
2259 /* i18n: [floating point unit] flags */
2260 fputs (_("flags: "), stdout);
2261 print_fpu_flags (status);
2264 /* Construct the ARM extended floating point type. */
2265 static struct type *
2266 arm_ext_type (struct gdbarch *gdbarch)
2268 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
2270 if (!tdep->arm_ext_type)
2272 = arch_float_type (gdbarch, -1, "builtin_type_arm_ext",
2273 floatformats_arm_ext);
2275 return tdep->arm_ext_type;
2278 static struct type *
2279 arm_neon_double_type (struct gdbarch *gdbarch)
2281 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
2283 if (tdep->neon_double_type == NULL)
2285 struct type *t, *elem;
2287 t = arch_composite_type (gdbarch, "__gdb_builtin_type_neon_d",
2289 elem = builtin_type (gdbarch)->builtin_uint8;
2290 append_composite_type_field (t, "u8", init_vector_type (elem, 8));
2291 elem = builtin_type (gdbarch)->builtin_uint16;
2292 append_composite_type_field (t, "u16", init_vector_type (elem, 4));
2293 elem = builtin_type (gdbarch)->builtin_uint32;
2294 append_composite_type_field (t, "u32", init_vector_type (elem, 2));
2295 elem = builtin_type (gdbarch)->builtin_uint64;
2296 append_composite_type_field (t, "u64", elem);
2297 elem = builtin_type (gdbarch)->builtin_float;
2298 append_composite_type_field (t, "f32", init_vector_type (elem, 2));
2299 elem = builtin_type (gdbarch)->builtin_double;
2300 append_composite_type_field (t, "f64", elem);
2302 TYPE_VECTOR (t) = 1;
2303 TYPE_NAME (t) = "neon_d";
2304 tdep->neon_double_type = t;
2307 return tdep->neon_double_type;
2310 /* FIXME: The vector types are not correctly ordered on big-endian
2311 targets. Just as s0 is the low bits of d0, d0[0] is also the low
2312 bits of d0 - regardless of what unit size is being held in d0. So
2313 the offset of the first uint8 in d0 is 7, but the offset of the
2314 first float is 4. This code works as-is for little-endian
2317 static struct type *
2318 arm_neon_quad_type (struct gdbarch *gdbarch)
2320 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
2322 if (tdep->neon_quad_type == NULL)
2324 struct type *t, *elem;
2326 t = arch_composite_type (gdbarch, "__gdb_builtin_type_neon_q",
2328 elem = builtin_type (gdbarch)->builtin_uint8;
2329 append_composite_type_field (t, "u8", init_vector_type (elem, 16));
2330 elem = builtin_type (gdbarch)->builtin_uint16;
2331 append_composite_type_field (t, "u16", init_vector_type (elem, 8));
2332 elem = builtin_type (gdbarch)->builtin_uint32;
2333 append_composite_type_field (t, "u32", init_vector_type (elem, 4));
2334 elem = builtin_type (gdbarch)->builtin_uint64;
2335 append_composite_type_field (t, "u64", init_vector_type (elem, 2));
2336 elem = builtin_type (gdbarch)->builtin_float;
2337 append_composite_type_field (t, "f32", init_vector_type (elem, 4));
2338 elem = builtin_type (gdbarch)->builtin_double;
2339 append_composite_type_field (t, "f64", init_vector_type (elem, 2));
2341 TYPE_VECTOR (t) = 1;
2342 TYPE_NAME (t) = "neon_q";
2343 tdep->neon_quad_type = t;
2346 return tdep->neon_quad_type;
2349 /* Return the GDB type object for the "standard" data type of data in
2352 static struct type *
2353 arm_register_type (struct gdbarch *gdbarch, int regnum)
2355 int num_regs = gdbarch_num_regs (gdbarch);
2357 if (gdbarch_tdep (gdbarch)->have_vfp_pseudos
2358 && regnum >= num_regs && regnum < num_regs + 32)
2359 return builtin_type (gdbarch)->builtin_float;
2361 if (gdbarch_tdep (gdbarch)->have_neon_pseudos
2362 && regnum >= num_regs + 32 && regnum < num_regs + 32 + 16)
2363 return arm_neon_quad_type (gdbarch);
2365 /* If the target description has register information, we are only
2366 in this function so that we can override the types of
2367 double-precision registers for NEON. */
2368 if (tdesc_has_registers (gdbarch_target_desc (gdbarch)))
2370 struct type *t = tdesc_register_type (gdbarch, regnum);
2372 if (regnum >= ARM_D0_REGNUM && regnum < ARM_D0_REGNUM + 32
2373 && TYPE_CODE (t) == TYPE_CODE_FLT
2374 && gdbarch_tdep (gdbarch)->have_neon)
2375 return arm_neon_double_type (gdbarch);
2380 if (regnum >= ARM_F0_REGNUM && regnum < ARM_F0_REGNUM + NUM_FREGS)
2382 if (!gdbarch_tdep (gdbarch)->have_fpa_registers)
2383 return builtin_type (gdbarch)->builtin_void;
2385 return arm_ext_type (gdbarch);
2387 else if (regnum == ARM_SP_REGNUM)
2388 return builtin_type (gdbarch)->builtin_data_ptr;
2389 else if (regnum == ARM_PC_REGNUM)
2390 return builtin_type (gdbarch)->builtin_func_ptr;
2391 else if (regnum >= ARRAY_SIZE (arm_register_names))
2392 /* These registers are only supported on targets which supply
2393 an XML description. */
2394 return builtin_type (gdbarch)->builtin_int0;
2396 return builtin_type (gdbarch)->builtin_uint32;
2399 /* Map a DWARF register REGNUM onto the appropriate GDB register
2403 arm_dwarf_reg_to_regnum (struct gdbarch *gdbarch, int reg)
2405 /* Core integer regs. */
2406 if (reg >= 0 && reg <= 15)
2409 /* Legacy FPA encoding. These were once used in a way which
2410 overlapped with VFP register numbering, so their use is
2411 discouraged, but GDB doesn't support the ARM toolchain
2412 which used them for VFP. */
2413 if (reg >= 16 && reg <= 23)
2414 return ARM_F0_REGNUM + reg - 16;
2416 /* New assignments for the FPA registers. */
2417 if (reg >= 96 && reg <= 103)
2418 return ARM_F0_REGNUM + reg - 96;
2420 /* WMMX register assignments. */
2421 if (reg >= 104 && reg <= 111)
2422 return ARM_WCGR0_REGNUM + reg - 104;
2424 if (reg >= 112 && reg <= 127)
2425 return ARM_WR0_REGNUM + reg - 112;
2427 if (reg >= 192 && reg <= 199)
2428 return ARM_WC0_REGNUM + reg - 192;
2430 /* VFP v2 registers. A double precision value is actually
2431 in d1 rather than s2, but the ABI only defines numbering
2432 for the single precision registers. This will "just work"
2433 in GDB for little endian targets (we'll read eight bytes,
2434 starting in s0 and then progressing to s1), but will be
2435 reversed on big endian targets with VFP. This won't
2436 be a problem for the new Neon quad registers; you're supposed
2437 to use DW_OP_piece for those. */
2438 if (reg >= 64 && reg <= 95)
2442 sprintf (name_buf, "s%d", reg - 64);
2443 return user_reg_map_name_to_regnum (gdbarch, name_buf,
2447 /* VFP v3 / Neon registers. This range is also used for VFP v2
2448 registers, except that it now describes d0 instead of s0. */
2449 if (reg >= 256 && reg <= 287)
2453 sprintf (name_buf, "d%d", reg - 256);
2454 return user_reg_map_name_to_regnum (gdbarch, name_buf,
2461 /* Map GDB internal REGNUM onto the Arm simulator register numbers. */
2463 arm_register_sim_regno (struct gdbarch *gdbarch, int regnum)
2466 gdb_assert (reg >= 0 && reg < gdbarch_num_regs (gdbarch));
2468 if (regnum >= ARM_WR0_REGNUM && regnum <= ARM_WR15_REGNUM)
2469 return regnum - ARM_WR0_REGNUM + SIM_ARM_IWMMXT_COP0R0_REGNUM;
2471 if (regnum >= ARM_WC0_REGNUM && regnum <= ARM_WC7_REGNUM)
2472 return regnum - ARM_WC0_REGNUM + SIM_ARM_IWMMXT_COP1R0_REGNUM;
2474 if (regnum >= ARM_WCGR0_REGNUM && regnum <= ARM_WCGR7_REGNUM)
2475 return regnum - ARM_WCGR0_REGNUM + SIM_ARM_IWMMXT_COP1R8_REGNUM;
2477 if (reg < NUM_GREGS)
2478 return SIM_ARM_R0_REGNUM + reg;
2481 if (reg < NUM_FREGS)
2482 return SIM_ARM_FP0_REGNUM + reg;
2485 if (reg < NUM_SREGS)
2486 return SIM_ARM_FPS_REGNUM + reg;
2489 internal_error (__FILE__, __LINE__, _("Bad REGNUM %d"), regnum);
2492 /* NOTE: cagney/2001-08-20: Both convert_from_extended() and
2493 convert_to_extended() use floatformat_arm_ext_littlebyte_bigword.
2494 It is thought that this is is the floating-point register format on
2495 little-endian systems. */
2498 convert_from_extended (const struct floatformat *fmt, const void *ptr,
2499 void *dbl, int endianess)
2503 if (endianess == BFD_ENDIAN_BIG)
2504 floatformat_to_doublest (&floatformat_arm_ext_big, ptr, &d);
2506 floatformat_to_doublest (&floatformat_arm_ext_littlebyte_bigword,
2508 floatformat_from_doublest (fmt, &d, dbl);
2512 convert_to_extended (const struct floatformat *fmt, void *dbl, const void *ptr,
2517 floatformat_to_doublest (fmt, ptr, &d);
2518 if (endianess == BFD_ENDIAN_BIG)
2519 floatformat_from_doublest (&floatformat_arm_ext_big, &d, dbl);
2521 floatformat_from_doublest (&floatformat_arm_ext_littlebyte_bigword,
2526 condition_true (unsigned long cond, unsigned long status_reg)
2528 if (cond == INST_AL || cond == INST_NV)
2534 return ((status_reg & FLAG_Z) != 0);
2536 return ((status_reg & FLAG_Z) == 0);
2538 return ((status_reg & FLAG_C) != 0);
2540 return ((status_reg & FLAG_C) == 0);
2542 return ((status_reg & FLAG_N) != 0);
2544 return ((status_reg & FLAG_N) == 0);
2546 return ((status_reg & FLAG_V) != 0);
2548 return ((status_reg & FLAG_V) == 0);
2550 return ((status_reg & (FLAG_C | FLAG_Z)) == FLAG_C);
2552 return ((status_reg & (FLAG_C | FLAG_Z)) != FLAG_C);
2554 return (((status_reg & FLAG_N) == 0) == ((status_reg & FLAG_V) == 0));
2556 return (((status_reg & FLAG_N) == 0) != ((status_reg & FLAG_V) == 0));
2558 return (((status_reg & FLAG_Z) == 0)
2559 && (((status_reg & FLAG_N) == 0)
2560 == ((status_reg & FLAG_V) == 0)));
2562 return (((status_reg & FLAG_Z) != 0)
2563 || (((status_reg & FLAG_N) == 0)
2564 != ((status_reg & FLAG_V) == 0)));
2569 static unsigned long
2570 shifted_reg_val (struct frame_info *frame, unsigned long inst, int carry,
2571 unsigned long pc_val, unsigned long status_reg)
2573 unsigned long res, shift;
2574 int rm = bits (inst, 0, 3);
2575 unsigned long shifttype = bits (inst, 5, 6);
2579 int rs = bits (inst, 8, 11);
2580 shift = (rs == 15 ? pc_val + 8
2581 : get_frame_register_unsigned (frame, rs)) & 0xFF;
2584 shift = bits (inst, 7, 11);
2587 ? (pc_val + (bit (inst, 4) ? 12 : 8))
2588 : get_frame_register_unsigned (frame, rm));
2593 res = shift >= 32 ? 0 : res << shift;
2597 res = shift >= 32 ? 0 : res >> shift;
2603 res = ((res & 0x80000000L)
2604 ? ~((~res) >> shift) : res >> shift);
2607 case 3: /* ROR/RRX */
2610 res = (res >> 1) | (carry ? 0x80000000L : 0);
2612 res = (res >> shift) | (res << (32 - shift));
2616 return res & 0xffffffff;
2619 /* Return number of 1-bits in VAL. */
2622 bitcount (unsigned long val)
2625 for (nbits = 0; val != 0; nbits++)
2626 val &= val - 1; /* delete rightmost 1-bit in val */
2630 /* Return the size in bytes of the complete Thumb instruction whose
2631 first halfword is INST1. */
2634 thumb_insn_size (unsigned short inst1)
2636 if ((inst1 & 0xe000) == 0xe000 && (inst1 & 0x1800) != 0)
2643 thumb_advance_itstate (unsigned int itstate)
2645 /* Preserve IT[7:5], the first three bits of the condition. Shift
2646 the upcoming condition flags left by one bit. */
2647 itstate = (itstate & 0xe0) | ((itstate << 1) & 0x1f);
2649 /* If we have finished the IT block, clear the state. */
2650 if ((itstate & 0x0f) == 0)
2656 /* Find the next PC after the current instruction executes. In some
2657 cases we can not statically determine the answer (see the IT state
2658 handling in this function); in that case, a breakpoint may be
2659 inserted in addition to the returned PC, which will be used to set
2660 another breakpoint by our caller. */
2663 thumb_get_next_pc_raw (struct frame_info *frame, CORE_ADDR pc, int insert_bkpt)
2665 struct gdbarch *gdbarch = get_frame_arch (frame);
2666 struct address_space *aspace = get_frame_address_space (frame);
2667 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
2668 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
2669 unsigned long pc_val = ((unsigned long) pc) + 4; /* PC after prefetch */
2670 unsigned short inst1;
2671 CORE_ADDR nextpc = pc + 2; /* default is next instruction */
2672 unsigned long offset;
2673 ULONGEST status, itstate;
2675 nextpc = MAKE_THUMB_ADDR (nextpc);
2676 pc_val = MAKE_THUMB_ADDR (pc_val);
2678 inst1 = read_memory_unsigned_integer (pc, 2, byte_order_for_code);
2680 /* Thumb-2 conditional execution support. There are eight bits in
2681 the CPSR which describe conditional execution state. Once
2682 reconstructed (they're in a funny order), the low five bits
2683 describe the low bit of the condition for each instruction and
2684 how many instructions remain. The high three bits describe the
2685 base condition. One of the low four bits will be set if an IT
2686 block is active. These bits read as zero on earlier
2688 status = get_frame_register_unsigned (frame, ARM_PS_REGNUM);
2689 itstate = ((status >> 8) & 0xfc) | ((status >> 25) & 0x3);
2691 /* If-Then handling. On GNU/Linux, where this routine is used, we
2692 use an undefined instruction as a breakpoint. Unlike BKPT, IT
2693 can disable execution of the undefined instruction. So we might
2694 miss the breakpoint if we set it on a skipped conditional
2695 instruction. Because conditional instructions can change the
2696 flags, affecting the execution of further instructions, we may
2697 need to set two breakpoints. */
2699 if (gdbarch_tdep (gdbarch)->thumb2_breakpoint != NULL)
2701 if ((inst1 & 0xff00) == 0xbf00 && (inst1 & 0x000f) != 0)
2703 /* An IT instruction. Because this instruction does not
2704 modify the flags, we can accurately predict the next
2705 executed instruction. */
2706 itstate = inst1 & 0x00ff;
2707 pc += thumb_insn_size (inst1);
2709 while (itstate != 0 && ! condition_true (itstate >> 4, status))
2711 inst1 = read_memory_unsigned_integer (pc, 2, byte_order_for_code);
2712 pc += thumb_insn_size (inst1);
2713 itstate = thumb_advance_itstate (itstate);
2716 return MAKE_THUMB_ADDR (pc);
2718 else if (itstate != 0)
2720 /* We are in a conditional block. Check the condition. */
2721 if (! condition_true (itstate >> 4, status))
2723 /* Advance to the next executed instruction. */
2724 pc += thumb_insn_size (inst1);
2725 itstate = thumb_advance_itstate (itstate);
2727 while (itstate != 0 && ! condition_true (itstate >> 4, status))
2729 inst1 = read_memory_unsigned_integer (pc, 2, byte_order_for_code);
2730 pc += thumb_insn_size (inst1);
2731 itstate = thumb_advance_itstate (itstate);
2734 return MAKE_THUMB_ADDR (pc);
2736 else if ((itstate & 0x0f) == 0x08)
2738 /* This is the last instruction of the conditional
2739 block, and it is executed. We can handle it normally
2740 because the following instruction is not conditional,
2741 and we must handle it normally because it is
2742 permitted to branch. Fall through. */
2748 /* There are conditional instructions after this one.
2749 If this instruction modifies the flags, then we can
2750 not predict what the next executed instruction will
2751 be. Fortunately, this instruction is architecturally
2752 forbidden to branch; we know it will fall through.
2753 Start by skipping past it. */
2754 pc += thumb_insn_size (inst1);
2755 itstate = thumb_advance_itstate (itstate);
2757 /* Set a breakpoint on the following instruction. */
2758 gdb_assert ((itstate & 0x0f) != 0);
2760 insert_single_step_breakpoint (gdbarch, aspace, pc);
2761 cond_negated = (itstate >> 4) & 1;
2763 /* Skip all following instructions with the same
2764 condition. If there is a later instruction in the IT
2765 block with the opposite condition, set the other
2766 breakpoint there. If not, then set a breakpoint on
2767 the instruction after the IT block. */
2770 inst1 = read_memory_unsigned_integer (pc, 2, byte_order_for_code);
2771 pc += thumb_insn_size (inst1);
2772 itstate = thumb_advance_itstate (itstate);
2774 while (itstate != 0 && ((itstate >> 4) & 1) == cond_negated);
2776 return MAKE_THUMB_ADDR (pc);
2780 else if (itstate & 0x0f)
2782 /* We are in a conditional block. Check the condition. */
2783 int cond = itstate >> 4;
2785 if (! condition_true (cond, status))
2787 /* Advance to the next instruction. All the 32-bit
2788 instructions share a common prefix. */
2789 if ((inst1 & 0xe000) == 0xe000 && (inst1 & 0x1800) != 0)
2790 return MAKE_THUMB_ADDR (pc + 4);
2792 return MAKE_THUMB_ADDR (pc + 2);
2795 /* Otherwise, handle the instruction normally. */
2798 if ((inst1 & 0xff00) == 0xbd00) /* pop {rlist, pc} */
2802 /* Fetch the saved PC from the stack. It's stored above
2803 all of the other registers. */
2804 offset = bitcount (bits (inst1, 0, 7)) * INT_REGISTER_SIZE;
2805 sp = get_frame_register_unsigned (frame, ARM_SP_REGNUM);
2806 nextpc = read_memory_unsigned_integer (sp + offset, 4, byte_order);
2808 else if ((inst1 & 0xf000) == 0xd000) /* conditional branch */
2810 unsigned long cond = bits (inst1, 8, 11);
2811 if (cond != 0x0f && condition_true (cond, status)) /* 0x0f = SWI */
2812 nextpc = pc_val + (sbits (inst1, 0, 7) << 1);
2814 else if ((inst1 & 0xf800) == 0xe000) /* unconditional branch */
2816 nextpc = pc_val + (sbits (inst1, 0, 10) << 1);
2818 else if ((inst1 & 0xe000) == 0xe000) /* 32-bit instruction */
2820 unsigned short inst2;
2821 inst2 = read_memory_unsigned_integer (pc + 2, 2, byte_order_for_code);
2823 /* Default to the next instruction. */
2825 nextpc = MAKE_THUMB_ADDR (nextpc);
2827 if ((inst1 & 0xf800) == 0xf000 && (inst2 & 0x8000) == 0x8000)
2829 /* Branches and miscellaneous control instructions. */
2831 if ((inst2 & 0x1000) != 0 || (inst2 & 0xd001) == 0xc000)
2834 int j1, j2, imm1, imm2;
2836 imm1 = sbits (inst1, 0, 10);
2837 imm2 = bits (inst2, 0, 10);
2838 j1 = bit (inst2, 13);
2839 j2 = bit (inst2, 11);
2841 offset = ((imm1 << 12) + (imm2 << 1));
2842 offset ^= ((!j2) << 22) | ((!j1) << 23);
2844 nextpc = pc_val + offset;
2845 /* For BLX make sure to clear the low bits. */
2846 if (bit (inst2, 12) == 0)
2847 nextpc = nextpc & 0xfffffffc;
2849 else if (inst1 == 0xf3de && (inst2 & 0xff00) == 0x3f00)
2851 /* SUBS PC, LR, #imm8. */
2852 nextpc = get_frame_register_unsigned (frame, ARM_LR_REGNUM);
2853 nextpc -= inst2 & 0x00ff;
2855 else if ((inst2 & 0xd000) == 0x8000 && (inst1 & 0x0380) != 0x0380)
2857 /* Conditional branch. */
2858 if (condition_true (bits (inst1, 6, 9), status))
2860 int sign, j1, j2, imm1, imm2;
2862 sign = sbits (inst1, 10, 10);
2863 imm1 = bits (inst1, 0, 5);
2864 imm2 = bits (inst2, 0, 10);
2865 j1 = bit (inst2, 13);
2866 j2 = bit (inst2, 11);
2868 offset = (sign << 20) + (j2 << 19) + (j1 << 18);
2869 offset += (imm1 << 12) + (imm2 << 1);
2871 nextpc = pc_val + offset;
2875 else if ((inst1 & 0xfe50) == 0xe810)
2877 /* Load multiple or RFE. */
2878 int rn, offset, load_pc = 1;
2880 rn = bits (inst1, 0, 3);
2881 if (bit (inst1, 7) && !bit (inst1, 8))
2884 if (!bit (inst2, 15))
2886 offset = bitcount (inst2) * 4 - 4;
2888 else if (!bit (inst1, 7) && bit (inst1, 8))
2891 if (!bit (inst2, 15))
2895 else if (bit (inst1, 7) && bit (inst1, 8))
2900 else if (!bit (inst1, 7) && !bit (inst1, 8))
2910 CORE_ADDR addr = get_frame_register_unsigned (frame, rn);
2911 nextpc = get_frame_memory_unsigned (frame, addr + offset, 4);
2914 else if ((inst1 & 0xffef) == 0xea4f && (inst2 & 0xfff0) == 0x0f00)
2916 /* MOV PC or MOVS PC. */
2917 nextpc = get_frame_register_unsigned (frame, bits (inst2, 0, 3));
2918 nextpc = MAKE_THUMB_ADDR (nextpc);
2920 else if ((inst1 & 0xff70) == 0xf850 && (inst2 & 0xf000) == 0xf000)
2924 int rn, load_pc = 1;
2926 rn = bits (inst1, 0, 3);
2927 base = get_frame_register_unsigned (frame, rn);
2930 base = (base + 4) & ~(CORE_ADDR) 0x3;
2932 base += bits (inst2, 0, 11);
2934 base -= bits (inst2, 0, 11);
2936 else if (bit (inst1, 7))
2937 base += bits (inst2, 0, 11);
2938 else if (bit (inst2, 11))
2940 if (bit (inst2, 10))
2943 base += bits (inst2, 0, 7);
2945 base -= bits (inst2, 0, 7);
2948 else if ((inst2 & 0x0fc0) == 0x0000)
2950 int shift = bits (inst2, 4, 5), rm = bits (inst2, 0, 3);
2951 base += get_frame_register_unsigned (frame, rm) << shift;
2958 nextpc = get_frame_memory_unsigned (frame, base, 4);
2960 else if ((inst1 & 0xfff0) == 0xe8d0 && (inst2 & 0xfff0) == 0xf000)
2963 CORE_ADDR tbl_reg, table, offset, length;
2965 tbl_reg = bits (inst1, 0, 3);
2966 if (tbl_reg == 0x0f)
2967 table = pc + 4; /* Regcache copy of PC isn't right yet. */
2969 table = get_frame_register_unsigned (frame, tbl_reg);
2971 offset = get_frame_register_unsigned (frame, bits (inst2, 0, 3));
2972 length = 2 * get_frame_memory_unsigned (frame, table + offset, 1);
2973 nextpc = pc_val + length;
2975 else if ((inst1 & 0xfff0) == 0xe8d0 && (inst2 & 0xfff0) == 0xf010)
2978 CORE_ADDR tbl_reg, table, offset, length;
2980 tbl_reg = bits (inst1, 0, 3);
2981 if (tbl_reg == 0x0f)
2982 table = pc + 4; /* Regcache copy of PC isn't right yet. */
2984 table = get_frame_register_unsigned (frame, tbl_reg);
2986 offset = 2 * get_frame_register_unsigned (frame, bits (inst2, 0, 3));
2987 length = 2 * get_frame_memory_unsigned (frame, table + offset, 2);
2988 nextpc = pc_val + length;
2991 else if ((inst1 & 0xff00) == 0x4700) /* bx REG, blx REG */
2993 if (bits (inst1, 3, 6) == 0x0f)
2996 nextpc = get_frame_register_unsigned (frame, bits (inst1, 3, 6));
2998 else if ((inst1 & 0xf500) == 0xb100)
3001 int imm = (bit (inst1, 9) << 6) + (bits (inst1, 3, 7) << 1);
3002 ULONGEST reg = get_frame_register_unsigned (frame, bits (inst1, 0, 2));
3004 if (bit (inst1, 11) && reg != 0)
3005 nextpc = pc_val + imm;
3006 else if (!bit (inst1, 11) && reg == 0)
3007 nextpc = pc_val + imm;
3012 /* Get the raw next address. PC is the current program counter, in
3013 FRAME. INSERT_BKPT should be TRUE if we want a breakpoint set on
3014 the alternative next instruction if there are two options.
3016 The value returned has the execution state of the next instruction
3017 encoded in it. Use IS_THUMB_ADDR () to see whether the instruction is
3018 in Thumb-State, and gdbarch_addr_bits_remove () to get the plain memory
3022 arm_get_next_pc_raw (struct frame_info *frame, CORE_ADDR pc, int insert_bkpt)
3024 struct gdbarch *gdbarch = get_frame_arch (frame);
3025 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
3026 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
3027 unsigned long pc_val;
3028 unsigned long this_instr;
3029 unsigned long status;
3032 if (arm_frame_is_thumb (frame))
3033 return thumb_get_next_pc_raw (frame, pc, insert_bkpt);
3035 pc_val = (unsigned long) pc;
3036 this_instr = read_memory_unsigned_integer (pc, 4, byte_order_for_code);
3038 status = get_frame_register_unsigned (frame, ARM_PS_REGNUM);
3039 nextpc = (CORE_ADDR) (pc_val + 4); /* Default case */
3041 if (bits (this_instr, 28, 31) == INST_NV)
3042 switch (bits (this_instr, 24, 27))
3047 /* Branch with Link and change to Thumb. */
3048 nextpc = BranchDest (pc, this_instr);
3049 nextpc |= bit (this_instr, 24) << 1;
3050 nextpc = MAKE_THUMB_ADDR (nextpc);
3056 /* Coprocessor register transfer. */
3057 if (bits (this_instr, 12, 15) == 15)
3058 error (_("Invalid update to pc in instruction"));
3061 else if (condition_true (bits (this_instr, 28, 31), status))
3063 switch (bits (this_instr, 24, 27))
3066 case 0x1: /* data processing */
3070 unsigned long operand1, operand2, result = 0;
3074 if (bits (this_instr, 12, 15) != 15)
3077 if (bits (this_instr, 22, 25) == 0
3078 && bits (this_instr, 4, 7) == 9) /* multiply */
3079 error (_("Invalid update to pc in instruction"));
3081 /* BX <reg>, BLX <reg> */
3082 if (bits (this_instr, 4, 27) == 0x12fff1
3083 || bits (this_instr, 4, 27) == 0x12fff3)
3085 rn = bits (this_instr, 0, 3);
3086 nextpc = (rn == 15) ? pc_val + 8
3087 : get_frame_register_unsigned (frame, rn);
3091 /* Multiply into PC */
3092 c = (status & FLAG_C) ? 1 : 0;
3093 rn = bits (this_instr, 16, 19);
3094 operand1 = (rn == 15) ? pc_val + 8
3095 : get_frame_register_unsigned (frame, rn);
3097 if (bit (this_instr, 25))
3099 unsigned long immval = bits (this_instr, 0, 7);
3100 unsigned long rotate = 2 * bits (this_instr, 8, 11);
3101 operand2 = ((immval >> rotate) | (immval << (32 - rotate)))
3104 else /* operand 2 is a shifted register */
3105 operand2 = shifted_reg_val (frame, this_instr, c, pc_val, status);
3107 switch (bits (this_instr, 21, 24))
3110 result = operand1 & operand2;
3114 result = operand1 ^ operand2;
3118 result = operand1 - operand2;
3122 result = operand2 - operand1;
3126 result = operand1 + operand2;
3130 result = operand1 + operand2 + c;
3134 result = operand1 - operand2 + c;
3138 result = operand2 - operand1 + c;
3144 case 0xb: /* tst, teq, cmp, cmn */
3145 result = (unsigned long) nextpc;
3149 result = operand1 | operand2;
3153 /* Always step into a function. */
3158 result = operand1 & ~operand2;
3166 /* In 26-bit APCS the bottom two bits of the result are
3167 ignored, and we always end up in ARM state. */
3169 nextpc = arm_addr_bits_remove (gdbarch, result);
3177 case 0x5: /* data transfer */
3180 if (bit (this_instr, 20))
3183 if (bits (this_instr, 12, 15) == 15)
3189 if (bit (this_instr, 22))
3190 error (_("Invalid update to pc in instruction"));
3192 /* byte write to PC */
3193 rn = bits (this_instr, 16, 19);
3194 base = (rn == 15) ? pc_val + 8
3195 : get_frame_register_unsigned (frame, rn);
3196 if (bit (this_instr, 24))
3199 int c = (status & FLAG_C) ? 1 : 0;
3200 unsigned long offset =
3201 (bit (this_instr, 25)
3202 ? shifted_reg_val (frame, this_instr, c, pc_val, status)
3203 : bits (this_instr, 0, 11));
3205 if (bit (this_instr, 23))
3210 nextpc = (CORE_ADDR) read_memory_integer ((CORE_ADDR) base,
3217 case 0x9: /* block transfer */
3218 if (bit (this_instr, 20))
3221 if (bit (this_instr, 15))
3226 if (bit (this_instr, 23))
3229 unsigned long reglist = bits (this_instr, 0, 14);
3230 offset = bitcount (reglist) * 4;
3231 if (bit (this_instr, 24)) /* pre */
3234 else if (bit (this_instr, 24))
3238 unsigned long rn_val =
3239 get_frame_register_unsigned (frame,
3240 bits (this_instr, 16, 19));
3242 (CORE_ADDR) read_memory_integer ((CORE_ADDR) (rn_val
3250 case 0xb: /* branch & link */
3251 case 0xa: /* branch */
3253 nextpc = BranchDest (pc, this_instr);
3259 case 0xe: /* coproc ops */
3264 fprintf_filtered (gdb_stderr, _("Bad bit-field extraction\n"));
3273 arm_get_next_pc (struct frame_info *frame, CORE_ADDR pc)
3275 struct gdbarch *gdbarch = get_frame_arch (frame);
3277 gdbarch_addr_bits_remove (gdbarch,
3278 arm_get_next_pc_raw (frame, pc, TRUE));
3280 error (_("Infinite loop detected"));
3284 /* single_step() is called just before we want to resume the inferior,
3285 if we want to single-step it but there is no hardware or kernel
3286 single-step support. We find the target of the coming instruction
3287 and breakpoint it. */
3290 arm_software_single_step (struct frame_info *frame)
3292 struct gdbarch *gdbarch = get_frame_arch (frame);
3293 struct address_space *aspace = get_frame_address_space (frame);
3295 /* NOTE: This may insert the wrong breakpoint instruction when
3296 single-stepping over a mode-changing instruction, if the
3297 CPSR heuristics are used. */
3299 CORE_ADDR next_pc = arm_get_next_pc (frame, get_frame_pc (frame));
3300 insert_single_step_breakpoint (gdbarch, aspace, next_pc);
3305 /* Given BUF, which is OLD_LEN bytes ending at ENDADDR, expand
3306 the buffer to be NEW_LEN bytes ending at ENDADDR. Return
3307 NULL if an error occurs. BUF is freed. */
3310 extend_buffer_earlier (gdb_byte *buf, CORE_ADDR endaddr,
3311 int old_len, int new_len)
3313 gdb_byte *new_buf, *middle;
3314 int bytes_to_read = new_len - old_len;
3316 new_buf = xmalloc (new_len);
3317 memcpy (new_buf + bytes_to_read, buf, old_len);
3319 if (target_read_memory (endaddr - new_len, new_buf, bytes_to_read) != 0)
3327 /* An IT block is at most the 2-byte IT instruction followed by
3328 four 4-byte instructions. The furthest back we must search to
3329 find an IT block that affects the current instruction is thus
3330 2 + 3 * 4 == 14 bytes. */
3331 #define MAX_IT_BLOCK_PREFIX 14
3333 /* Use a quick scan if there are more than this many bytes of
3335 #define IT_SCAN_THRESHOLD 32
3337 /* Adjust a breakpoint's address to move breakpoints out of IT blocks.
3338 A breakpoint in an IT block may not be hit, depending on the
3341 arm_adjust_breakpoint_address (struct gdbarch *gdbarch, CORE_ADDR bpaddr)
3345 CORE_ADDR boundary, func_start;
3346 int buf_len, buf2_len;
3347 enum bfd_endian order = gdbarch_byte_order_for_code (gdbarch);
3348 int i, any, last_it, last_it_count;
3350 /* If we are using BKPT breakpoints, none of this is necessary. */
3351 if (gdbarch_tdep (gdbarch)->thumb2_breakpoint == NULL)
3354 /* ARM mode does not have this problem. */
3355 if (!arm_pc_is_thumb (bpaddr))
3358 /* We are setting a breakpoint in Thumb code that could potentially
3359 contain an IT block. The first step is to find how much Thumb
3360 code there is; we do not need to read outside of known Thumb
3362 map_type = arm_find_mapping_symbol (bpaddr, &boundary);
3364 /* Thumb-2 code must have mapping symbols to have a chance. */
3367 bpaddr = gdbarch_addr_bits_remove (gdbarch, bpaddr);
3369 if (find_pc_partial_function (bpaddr, NULL, &func_start, NULL)
3370 && func_start > boundary)
3371 boundary = func_start;
3373 /* Search for a candidate IT instruction. We have to do some fancy
3374 footwork to distinguish a real IT instruction from the second
3375 half of a 32-bit instruction, but there is no need for that if
3376 there's no candidate. */
3377 buf_len = min (bpaddr - boundary, MAX_IT_BLOCK_PREFIX);
3379 /* No room for an IT instruction. */
3382 buf = xmalloc (buf_len);
3383 if (target_read_memory (bpaddr - buf_len, buf, buf_len) != 0)
3386 for (i = 0; i < buf_len; i += 2)
3388 unsigned short inst1 = extract_unsigned_integer (&buf[i], 2, order);
3389 if ((inst1 & 0xff00) == 0xbf00 && (inst1 & 0x000f) != 0)
3401 /* OK, the code bytes before this instruction contain at least one
3402 halfword which resembles an IT instruction. We know that it's
3403 Thumb code, but there are still two possibilities. Either the
3404 halfword really is an IT instruction, or it is the second half of
3405 a 32-bit Thumb instruction. The only way we can tell is to
3406 scan forwards from a known instruction boundary. */
3407 if (bpaddr - boundary > IT_SCAN_THRESHOLD)
3411 /* There's a lot of code before this instruction. Start with an
3412 optimistic search; it's easy to recognize halfwords that can
3413 not be the start of a 32-bit instruction, and use that to
3414 lock on to the instruction boundaries. */
3415 buf = extend_buffer_earlier (buf, bpaddr, buf_len, IT_SCAN_THRESHOLD);
3418 buf_len = IT_SCAN_THRESHOLD;
3421 for (i = 0; i < buf_len - sizeof (buf) && ! definite; i += 2)
3423 unsigned short inst1 = extract_unsigned_integer (&buf[i], 2, order);
3424 if (thumb_insn_size (inst1) == 2)
3431 /* At this point, if DEFINITE, BUF[I] is the first place we
3432 are sure that we know the instruction boundaries, and it is far
3433 enough from BPADDR that we could not miss an IT instruction
3434 affecting BPADDR. If ! DEFINITE, give up - start from a
3438 buf = extend_buffer_earlier (buf, bpaddr, buf_len, bpaddr - boundary);
3441 buf_len = bpaddr - boundary;
3447 buf = extend_buffer_earlier (buf, bpaddr, buf_len, bpaddr - boundary);
3450 buf_len = bpaddr - boundary;
3454 /* Scan forwards. Find the last IT instruction before BPADDR. */
3459 unsigned short inst1 = extract_unsigned_integer (&buf[i], 2, order);
3461 if ((inst1 & 0xff00) == 0xbf00 && (inst1 & 0x000f) != 0)
3466 else if (inst1 & 0x0002)
3468 else if (inst1 & 0x0004)
3473 i += thumb_insn_size (inst1);
3479 /* There wasn't really an IT instruction after all. */
3482 if (last_it_count < 1)
3483 /* It was too far away. */
3486 /* This really is a trouble spot. Move the breakpoint to the IT
3488 return bpaddr - buf_len + last_it;
3491 /* ARM displaced stepping support.
3493 Generally ARM displaced stepping works as follows:
3495 1. When an instruction is to be single-stepped, it is first decoded by
3496 arm_process_displaced_insn (called from arm_displaced_step_copy_insn).
3497 Depending on the type of instruction, it is then copied to a scratch
3498 location, possibly in a modified form. The copy_* set of functions
3499 performs such modification, as necessary. A breakpoint is placed after
3500 the modified instruction in the scratch space to return control to GDB.
3501 Note in particular that instructions which modify the PC will no longer
3502 do so after modification.
3504 2. The instruction is single-stepped, by setting the PC to the scratch
3505 location address, and resuming. Control returns to GDB when the
3508 3. A cleanup function (cleanup_*) is called corresponding to the copy_*
3509 function used for the current instruction. This function's job is to
3510 put the CPU/memory state back to what it would have been if the
3511 instruction had been executed unmodified in its original location. */
3513 /* NOP instruction (mov r0, r0). */
3514 #define ARM_NOP 0xe1a00000
3516 /* Helper for register reads for displaced stepping. In particular, this
3517 returns the PC as it would be seen by the instruction at its original
3521 displaced_read_reg (struct regcache *regs, CORE_ADDR from, int regno)
3527 if (debug_displaced)
3528 fprintf_unfiltered (gdb_stdlog, "displaced: read pc value %.8lx\n",
3529 (unsigned long) from + 8);
3530 return (ULONGEST) from + 8; /* Pipeline offset. */
3534 regcache_cooked_read_unsigned (regs, regno, &ret);
3535 if (debug_displaced)
3536 fprintf_unfiltered (gdb_stdlog, "displaced: read r%d value %.8lx\n",
3537 regno, (unsigned long) ret);
3543 displaced_in_arm_mode (struct regcache *regs)
3547 regcache_cooked_read_unsigned (regs, ARM_PS_REGNUM, &ps);
3549 return (ps & CPSR_T) == 0;
3552 /* Write to the PC as from a branch instruction. */
3555 branch_write_pc (struct regcache *regs, ULONGEST val)
3557 if (displaced_in_arm_mode (regs))
3558 /* Note: If bits 0/1 are set, this branch would be unpredictable for
3559 architecture versions < 6. */
3560 regcache_cooked_write_unsigned (regs, ARM_PC_REGNUM, val & ~(ULONGEST) 0x3);
3562 regcache_cooked_write_unsigned (regs, ARM_PC_REGNUM, val & ~(ULONGEST) 0x1);
3565 /* Write to the PC as from a branch-exchange instruction. */
3568 bx_write_pc (struct regcache *regs, ULONGEST val)
3572 regcache_cooked_read_unsigned (regs, ARM_PS_REGNUM, &ps);
3576 regcache_cooked_write_unsigned (regs, ARM_PS_REGNUM, ps | CPSR_T);
3577 regcache_cooked_write_unsigned (regs, ARM_PC_REGNUM, val & 0xfffffffe);
3579 else if ((val & 2) == 0)
3581 regcache_cooked_write_unsigned (regs, ARM_PS_REGNUM,
3582 ps & ~(ULONGEST) CPSR_T);
3583 regcache_cooked_write_unsigned (regs, ARM_PC_REGNUM, val);
3587 /* Unpredictable behaviour. Try to do something sensible (switch to ARM
3588 mode, align dest to 4 bytes). */
3589 warning (_("Single-stepping BX to non-word-aligned ARM instruction."));
3590 regcache_cooked_write_unsigned (regs, ARM_PS_REGNUM,
3591 ps & ~(ULONGEST) CPSR_T);
3592 regcache_cooked_write_unsigned (regs, ARM_PC_REGNUM, val & 0xfffffffc);
3596 /* Write to the PC as if from a load instruction. */
3599 load_write_pc (struct regcache *regs, ULONGEST val)
3601 if (DISPLACED_STEPPING_ARCH_VERSION >= 5)
3602 bx_write_pc (regs, val);
3604 branch_write_pc (regs, val);
3607 /* Write to the PC as if from an ALU instruction. */
3610 alu_write_pc (struct regcache *regs, ULONGEST val)
3612 if (DISPLACED_STEPPING_ARCH_VERSION >= 7 && displaced_in_arm_mode (regs))
3613 bx_write_pc (regs, val);
3615 branch_write_pc (regs, val);
3618 /* Helper for writing to registers for displaced stepping. Writing to the PC
3619 has a varying effects depending on the instruction which does the write:
3620 this is controlled by the WRITE_PC argument. */
3623 displaced_write_reg (struct regcache *regs, struct displaced_step_closure *dsc,
3624 int regno, ULONGEST val, enum pc_write_style write_pc)
3628 if (debug_displaced)
3629 fprintf_unfiltered (gdb_stdlog, "displaced: writing pc %.8lx\n",
3630 (unsigned long) val);
3633 case BRANCH_WRITE_PC:
3634 branch_write_pc (regs, val);
3638 bx_write_pc (regs, val);
3642 load_write_pc (regs, val);
3646 alu_write_pc (regs, val);
3649 case CANNOT_WRITE_PC:
3650 warning (_("Instruction wrote to PC in an unexpected way when "
3651 "single-stepping"));
3655 internal_error (__FILE__, __LINE__,
3656 _("Invalid argument to displaced_write_reg"));
3659 dsc->wrote_to_pc = 1;
3663 if (debug_displaced)
3664 fprintf_unfiltered (gdb_stdlog, "displaced: writing r%d value %.8lx\n",
3665 regno, (unsigned long) val);
3666 regcache_cooked_write_unsigned (regs, regno, val);
3670 /* This function is used to concisely determine if an instruction INSN
3671 references PC. Register fields of interest in INSN should have the
3672 corresponding fields of BITMASK set to 0b1111. The function returns return 1
3673 if any of these fields in INSN reference the PC (also 0b1111, r15), else it
3677 insn_references_pc (uint32_t insn, uint32_t bitmask)
3679 uint32_t lowbit = 1;
3681 while (bitmask != 0)
3685 for (; lowbit && (bitmask & lowbit) == 0; lowbit <<= 1)
3691 mask = lowbit * 0xf;
3693 if ((insn & mask) == mask)
3702 /* The simplest copy function. Many instructions have the same effect no
3703 matter what address they are executed at: in those cases, use this. */
3706 copy_unmodified (struct gdbarch *gdbarch, uint32_t insn,
3707 const char *iname, struct displaced_step_closure *dsc)
3709 if (debug_displaced)
3710 fprintf_unfiltered (gdb_stdlog, "displaced: copying insn %.8lx, "
3711 "opcode/class '%s' unmodified\n", (unsigned long) insn,
3714 dsc->modinsn[0] = insn;
3719 /* Preload instructions with immediate offset. */
3722 cleanup_preload (struct gdbarch *gdbarch,
3723 struct regcache *regs, struct displaced_step_closure *dsc)
3725 displaced_write_reg (regs, dsc, 0, dsc->tmp[0], CANNOT_WRITE_PC);
3726 if (!dsc->u.preload.immed)
3727 displaced_write_reg (regs, dsc, 1, dsc->tmp[1], CANNOT_WRITE_PC);
3731 copy_preload (struct gdbarch *gdbarch, uint32_t insn, struct regcache *regs,
3732 struct displaced_step_closure *dsc)
3734 unsigned int rn = bits (insn, 16, 19);
3736 CORE_ADDR from = dsc->insn_addr;
3738 if (!insn_references_pc (insn, 0x000f0000ul))
3739 return copy_unmodified (gdbarch, insn, "preload", dsc);
3741 if (debug_displaced)
3742 fprintf_unfiltered (gdb_stdlog, "displaced: copying preload insn %.8lx\n",
3743 (unsigned long) insn);
3745 /* Preload instructions:
3747 {pli/pld} [rn, #+/-imm]
3749 {pli/pld} [r0, #+/-imm]. */
3751 dsc->tmp[0] = displaced_read_reg (regs, from, 0);
3752 rn_val = displaced_read_reg (regs, from, rn);
3753 displaced_write_reg (regs, dsc, 0, rn_val, CANNOT_WRITE_PC);
3755 dsc->u.preload.immed = 1;
3757 dsc->modinsn[0] = insn & 0xfff0ffff;
3759 dsc->cleanup = &cleanup_preload;
3764 /* Preload instructions with register offset. */
3767 copy_preload_reg (struct gdbarch *gdbarch, uint32_t insn, struct regcache *regs,
3768 struct displaced_step_closure *dsc)
3770 unsigned int rn = bits (insn, 16, 19);
3771 unsigned int rm = bits (insn, 0, 3);
3772 ULONGEST rn_val, rm_val;
3773 CORE_ADDR from = dsc->insn_addr;
3775 if (!insn_references_pc (insn, 0x000f000ful))
3776 return copy_unmodified (gdbarch, insn, "preload reg", dsc);
3778 if (debug_displaced)
3779 fprintf_unfiltered (gdb_stdlog, "displaced: copying preload insn %.8lx\n",
3780 (unsigned long) insn);
3782 /* Preload register-offset instructions:
3784 {pli/pld} [rn, rm {, shift}]
3786 {pli/pld} [r0, r1 {, shift}]. */
3788 dsc->tmp[0] = displaced_read_reg (regs, from, 0);
3789 dsc->tmp[1] = displaced_read_reg (regs, from, 1);
3790 rn_val = displaced_read_reg (regs, from, rn);
3791 rm_val = displaced_read_reg (regs, from, rm);
3792 displaced_write_reg (regs, dsc, 0, rn_val, CANNOT_WRITE_PC);
3793 displaced_write_reg (regs, dsc, 1, rm_val, CANNOT_WRITE_PC);
3795 dsc->u.preload.immed = 0;
3797 dsc->modinsn[0] = (insn & 0xfff0fff0) | 0x1;
3799 dsc->cleanup = &cleanup_preload;
3804 /* Copy/cleanup coprocessor load and store instructions. */
3807 cleanup_copro_load_store (struct gdbarch *gdbarch,
3808 struct regcache *regs,
3809 struct displaced_step_closure *dsc)
3811 ULONGEST rn_val = displaced_read_reg (regs, dsc->insn_addr, 0);
3813 displaced_write_reg (regs, dsc, 0, dsc->tmp[0], CANNOT_WRITE_PC);
3815 if (dsc->u.ldst.writeback)
3816 displaced_write_reg (regs, dsc, dsc->u.ldst.rn, rn_val, LOAD_WRITE_PC);
3820 copy_copro_load_store (struct gdbarch *gdbarch, uint32_t insn,
3821 struct regcache *regs,
3822 struct displaced_step_closure *dsc)
3824 unsigned int rn = bits (insn, 16, 19);
3826 CORE_ADDR from = dsc->insn_addr;
3828 if (!insn_references_pc (insn, 0x000f0000ul))
3829 return copy_unmodified (gdbarch, insn, "copro load/store", dsc);
3831 if (debug_displaced)
3832 fprintf_unfiltered (gdb_stdlog, "displaced: copying coprocessor "
3833 "load/store insn %.8lx\n", (unsigned long) insn);
3835 /* Coprocessor load/store instructions:
3837 {stc/stc2} [<Rn>, #+/-imm] (and other immediate addressing modes)
3839 {stc/stc2} [r0, #+/-imm].
3841 ldc/ldc2 are handled identically. */
3843 dsc->tmp[0] = displaced_read_reg (regs, from, 0);
3844 rn_val = displaced_read_reg (regs, from, rn);
3845 displaced_write_reg (regs, dsc, 0, rn_val, CANNOT_WRITE_PC);
3847 dsc->u.ldst.writeback = bit (insn, 25);
3848 dsc->u.ldst.rn = rn;
3850 dsc->modinsn[0] = insn & 0xfff0ffff;
3852 dsc->cleanup = &cleanup_copro_load_store;
3857 /* Clean up branch instructions (actually perform the branch, by setting
3861 cleanup_branch (struct gdbarch *gdbarch, struct regcache *regs,
3862 struct displaced_step_closure *dsc)
3864 ULONGEST from = dsc->insn_addr;
3865 uint32_t status = displaced_read_reg (regs, from, ARM_PS_REGNUM);
3866 int branch_taken = condition_true (dsc->u.branch.cond, status);
3867 enum pc_write_style write_pc = dsc->u.branch.exchange
3868 ? BX_WRITE_PC : BRANCH_WRITE_PC;
3873 if (dsc->u.branch.link)
3875 ULONGEST pc = displaced_read_reg (regs, from, 15);
3876 displaced_write_reg (regs, dsc, 14, pc - 4, CANNOT_WRITE_PC);
3879 displaced_write_reg (regs, dsc, 15, dsc->u.branch.dest, write_pc);
3882 /* Copy B/BL/BLX instructions with immediate destinations. */
3885 copy_b_bl_blx (struct gdbarch *gdbarch, uint32_t insn,
3886 struct regcache *regs, struct displaced_step_closure *dsc)
3888 unsigned int cond = bits (insn, 28, 31);
3889 int exchange = (cond == 0xf);
3890 int link = exchange || bit (insn, 24);
3891 CORE_ADDR from = dsc->insn_addr;
3894 if (debug_displaced)
3895 fprintf_unfiltered (gdb_stdlog, "displaced: copying %s immediate insn "
3896 "%.8lx\n", (exchange) ? "blx" : (link) ? "bl" : "b",
3897 (unsigned long) insn);
3899 /* Implement "BL<cond> <label>" as:
3901 Preparation: cond <- instruction condition
3902 Insn: mov r0, r0 (nop)
3903 Cleanup: if (condition true) { r14 <- pc; pc <- label }.
3905 B<cond> similar, but don't set r14 in cleanup. */
3908 /* For BLX, set bit 0 of the destination. The cleanup_branch function will
3909 then arrange the switch into Thumb mode. */
3910 offset = (bits (insn, 0, 23) << 2) | (bit (insn, 24) << 1) | 1;
3912 offset = bits (insn, 0, 23) << 2;
3914 if (bit (offset, 25))
3915 offset = offset | ~0x3ffffff;
3917 dsc->u.branch.cond = cond;
3918 dsc->u.branch.link = link;
3919 dsc->u.branch.exchange = exchange;
3920 dsc->u.branch.dest = from + 8 + offset;
3922 dsc->modinsn[0] = ARM_NOP;
3924 dsc->cleanup = &cleanup_branch;
3929 /* Copy BX/BLX with register-specified destinations. */
3932 copy_bx_blx_reg (struct gdbarch *gdbarch, uint32_t insn,
3933 struct regcache *regs, struct displaced_step_closure *dsc)
3935 unsigned int cond = bits (insn, 28, 31);
3938 int link = bit (insn, 5);
3939 unsigned int rm = bits (insn, 0, 3);
3940 CORE_ADDR from = dsc->insn_addr;
3942 if (debug_displaced)
3943 fprintf_unfiltered (gdb_stdlog, "displaced: copying %s register insn "
3944 "%.8lx\n", (link) ? "blx" : "bx", (unsigned long) insn);
3946 /* Implement {BX,BLX}<cond> <reg>" as:
3948 Preparation: cond <- instruction condition
3949 Insn: mov r0, r0 (nop)
3950 Cleanup: if (condition true) { r14 <- pc; pc <- dest; }.
3952 Don't set r14 in cleanup for BX. */
3954 dsc->u.branch.dest = displaced_read_reg (regs, from, rm);
3956 dsc->u.branch.cond = cond;
3957 dsc->u.branch.link = link;
3958 dsc->u.branch.exchange = 1;
3960 dsc->modinsn[0] = ARM_NOP;
3962 dsc->cleanup = &cleanup_branch;
3967 /* Copy/cleanup arithmetic/logic instruction with immediate RHS. */
3970 cleanup_alu_imm (struct gdbarch *gdbarch,
3971 struct regcache *regs, struct displaced_step_closure *dsc)
3973 ULONGEST rd_val = displaced_read_reg (regs, dsc->insn_addr, 0);
3974 displaced_write_reg (regs, dsc, 0, dsc->tmp[0], CANNOT_WRITE_PC);
3975 displaced_write_reg (regs, dsc, 1, dsc->tmp[1], CANNOT_WRITE_PC);
3976 displaced_write_reg (regs, dsc, dsc->rd, rd_val, ALU_WRITE_PC);
3980 copy_alu_imm (struct gdbarch *gdbarch, uint32_t insn, struct regcache *regs,
3981 struct displaced_step_closure *dsc)
3983 unsigned int rn = bits (insn, 16, 19);
3984 unsigned int rd = bits (insn, 12, 15);
3985 unsigned int op = bits (insn, 21, 24);
3986 int is_mov = (op == 0xd);
3987 ULONGEST rd_val, rn_val;
3988 CORE_ADDR from = dsc->insn_addr;
3990 if (!insn_references_pc (insn, 0x000ff000ul))
3991 return copy_unmodified (gdbarch, insn, "ALU immediate", dsc);
3993 if (debug_displaced)
3994 fprintf_unfiltered (gdb_stdlog, "displaced: copying immediate %s insn "
3995 "%.8lx\n", is_mov ? "move" : "ALU",
3996 (unsigned long) insn);
3998 /* Instruction is of form:
4000 <op><cond> rd, [rn,] #imm
4004 Preparation: tmp1, tmp2 <- r0, r1;
4006 Insn: <op><cond> r0, r1, #imm
4007 Cleanup: rd <- r0; r0 <- tmp1; r1 <- tmp2
4010 dsc->tmp[0] = displaced_read_reg (regs, from, 0);
4011 dsc->tmp[1] = displaced_read_reg (regs, from, 1);
4012 rn_val = displaced_read_reg (regs, from, rn);
4013 rd_val = displaced_read_reg (regs, from, rd);
4014 displaced_write_reg (regs, dsc, 0, rd_val, CANNOT_WRITE_PC);
4015 displaced_write_reg (regs, dsc, 1, rn_val, CANNOT_WRITE_PC);
4019 dsc->modinsn[0] = insn & 0xfff00fff;
4021 dsc->modinsn[0] = (insn & 0xfff00fff) | 0x10000;
4023 dsc->cleanup = &cleanup_alu_imm;
4028 /* Copy/cleanup arithmetic/logic insns with register RHS. */
4031 cleanup_alu_reg (struct gdbarch *gdbarch,
4032 struct regcache *regs, struct displaced_step_closure *dsc)
4037 rd_val = displaced_read_reg (regs, dsc->insn_addr, 0);
4039 for (i = 0; i < 3; i++)
4040 displaced_write_reg (regs, dsc, i, dsc->tmp[i], CANNOT_WRITE_PC);
4042 displaced_write_reg (regs, dsc, dsc->rd, rd_val, ALU_WRITE_PC);
4046 copy_alu_reg (struct gdbarch *gdbarch, uint32_t insn, struct regcache *regs,
4047 struct displaced_step_closure *dsc)
4049 unsigned int rn = bits (insn, 16, 19);
4050 unsigned int rm = bits (insn, 0, 3);
4051 unsigned int rd = bits (insn, 12, 15);
4052 unsigned int op = bits (insn, 21, 24);
4053 int is_mov = (op == 0xd);
4054 ULONGEST rd_val, rn_val, rm_val;
4055 CORE_ADDR from = dsc->insn_addr;
4057 if (!insn_references_pc (insn, 0x000ff00ful))
4058 return copy_unmodified (gdbarch, insn, "ALU reg", dsc);
4060 if (debug_displaced)
4061 fprintf_unfiltered (gdb_stdlog, "displaced: copying reg %s insn %.8lx\n",
4062 is_mov ? "move" : "ALU", (unsigned long) insn);
4064 /* Instruction is of form:
4066 <op><cond> rd, [rn,] rm [, <shift>]
4070 Preparation: tmp1, tmp2, tmp3 <- r0, r1, r2;
4071 r0, r1, r2 <- rd, rn, rm
4072 Insn: <op><cond> r0, r1, r2 [, <shift>]
4073 Cleanup: rd <- r0; r0, r1, r2 <- tmp1, tmp2, tmp3
4076 dsc->tmp[0] = displaced_read_reg (regs, from, 0);
4077 dsc->tmp[1] = displaced_read_reg (regs, from, 1);
4078 dsc->tmp[2] = displaced_read_reg (regs, from, 2);
4079 rd_val = displaced_read_reg (regs, from, rd);
4080 rn_val = displaced_read_reg (regs, from, rn);
4081 rm_val = displaced_read_reg (regs, from, rm);
4082 displaced_write_reg (regs, dsc, 0, rd_val, CANNOT_WRITE_PC);
4083 displaced_write_reg (regs, dsc, 1, rn_val, CANNOT_WRITE_PC);
4084 displaced_write_reg (regs, dsc, 2, rm_val, CANNOT_WRITE_PC);
4088 dsc->modinsn[0] = (insn & 0xfff00ff0) | 0x2;
4090 dsc->modinsn[0] = (insn & 0xfff00ff0) | 0x10002;
4092 dsc->cleanup = &cleanup_alu_reg;
4097 /* Cleanup/copy arithmetic/logic insns with shifted register RHS. */
4100 cleanup_alu_shifted_reg (struct gdbarch *gdbarch,
4101 struct regcache *regs,
4102 struct displaced_step_closure *dsc)
4104 ULONGEST rd_val = displaced_read_reg (regs, dsc->insn_addr, 0);
4107 for (i = 0; i < 4; i++)
4108 displaced_write_reg (regs, dsc, i, dsc->tmp[i], CANNOT_WRITE_PC);
4110 displaced_write_reg (regs, dsc, dsc->rd, rd_val, ALU_WRITE_PC);
4114 copy_alu_shifted_reg (struct gdbarch *gdbarch, uint32_t insn,
4115 struct regcache *regs, struct displaced_step_closure *dsc)
4117 unsigned int rn = bits (insn, 16, 19);
4118 unsigned int rm = bits (insn, 0, 3);
4119 unsigned int rd = bits (insn, 12, 15);
4120 unsigned int rs = bits (insn, 8, 11);
4121 unsigned int op = bits (insn, 21, 24);
4122 int is_mov = (op == 0xd), i;
4123 ULONGEST rd_val, rn_val, rm_val, rs_val;
4124 CORE_ADDR from = dsc->insn_addr;
4126 if (!insn_references_pc (insn, 0x000fff0ful))
4127 return copy_unmodified (gdbarch, insn, "ALU shifted reg", dsc);
4129 if (debug_displaced)
4130 fprintf_unfiltered (gdb_stdlog, "displaced: copying shifted reg %s insn "
4131 "%.8lx\n", is_mov ? "move" : "ALU",
4132 (unsigned long) insn);
4134 /* Instruction is of form:
4136 <op><cond> rd, [rn,] rm, <shift> rs
4140 Preparation: tmp1, tmp2, tmp3, tmp4 <- r0, r1, r2, r3
4141 r0, r1, r2, r3 <- rd, rn, rm, rs
4142 Insn: <op><cond> r0, r1, r2, <shift> r3
4144 r0, r1, r2, r3 <- tmp1, tmp2, tmp3, tmp4
4148 for (i = 0; i < 4; i++)
4149 dsc->tmp[i] = displaced_read_reg (regs, from, i);
4151 rd_val = displaced_read_reg (regs, from, rd);
4152 rn_val = displaced_read_reg (regs, from, rn);
4153 rm_val = displaced_read_reg (regs, from, rm);
4154 rs_val = displaced_read_reg (regs, from, rs);
4155 displaced_write_reg (regs, dsc, 0, rd_val, CANNOT_WRITE_PC);
4156 displaced_write_reg (regs, dsc, 1, rn_val, CANNOT_WRITE_PC);
4157 displaced_write_reg (regs, dsc, 2, rm_val, CANNOT_WRITE_PC);
4158 displaced_write_reg (regs, dsc, 3, rs_val, CANNOT_WRITE_PC);
4162 dsc->modinsn[0] = (insn & 0xfff000f0) | 0x302;
4164 dsc->modinsn[0] = (insn & 0xfff000f0) | 0x10302;
4166 dsc->cleanup = &cleanup_alu_shifted_reg;
4171 /* Clean up load instructions. */
4174 cleanup_load (struct gdbarch *gdbarch, struct regcache *regs,
4175 struct displaced_step_closure *dsc)
4177 ULONGEST rt_val, rt_val2 = 0, rn_val;
4178 CORE_ADDR from = dsc->insn_addr;
4180 rt_val = displaced_read_reg (regs, from, 0);
4181 if (dsc->u.ldst.xfersize == 8)
4182 rt_val2 = displaced_read_reg (regs, from, 1);
4183 rn_val = displaced_read_reg (regs, from, 2);
4185 displaced_write_reg (regs, dsc, 0, dsc->tmp[0], CANNOT_WRITE_PC);
4186 if (dsc->u.ldst.xfersize > 4)
4187 displaced_write_reg (regs, dsc, 1, dsc->tmp[1], CANNOT_WRITE_PC);
4188 displaced_write_reg (regs, dsc, 2, dsc->tmp[2], CANNOT_WRITE_PC);
4189 if (!dsc->u.ldst.immed)
4190 displaced_write_reg (regs, dsc, 3, dsc->tmp[3], CANNOT_WRITE_PC);
4192 /* Handle register writeback. */
4193 if (dsc->u.ldst.writeback)
4194 displaced_write_reg (regs, dsc, dsc->u.ldst.rn, rn_val, CANNOT_WRITE_PC);
4195 /* Put result in right place. */
4196 displaced_write_reg (regs, dsc, dsc->rd, rt_val, LOAD_WRITE_PC);
4197 if (dsc->u.ldst.xfersize == 8)
4198 displaced_write_reg (regs, dsc, dsc->rd + 1, rt_val2, LOAD_WRITE_PC);
4201 /* Clean up store instructions. */
4204 cleanup_store (struct gdbarch *gdbarch, struct regcache *regs,
4205 struct displaced_step_closure *dsc)
4207 CORE_ADDR from = dsc->insn_addr;
4208 ULONGEST rn_val = displaced_read_reg (regs, from, 2);
4210 displaced_write_reg (regs, dsc, 0, dsc->tmp[0], CANNOT_WRITE_PC);
4211 if (dsc->u.ldst.xfersize > 4)
4212 displaced_write_reg (regs, dsc, 1, dsc->tmp[1], CANNOT_WRITE_PC);
4213 displaced_write_reg (regs, dsc, 2, dsc->tmp[2], CANNOT_WRITE_PC);
4214 if (!dsc->u.ldst.immed)
4215 displaced_write_reg (regs, dsc, 3, dsc->tmp[3], CANNOT_WRITE_PC);
4216 if (!dsc->u.ldst.restore_r4)
4217 displaced_write_reg (regs, dsc, 4, dsc->tmp[4], CANNOT_WRITE_PC);
4220 if (dsc->u.ldst.writeback)
4221 displaced_write_reg (regs, dsc, dsc->u.ldst.rn, rn_val, CANNOT_WRITE_PC);
4224 /* Copy "extra" load/store instructions. These are halfword/doubleword
4225 transfers, which have a different encoding to byte/word transfers. */
4228 copy_extra_ld_st (struct gdbarch *gdbarch, uint32_t insn, int unpriveleged,
4229 struct regcache *regs, struct displaced_step_closure *dsc)
4231 unsigned int op1 = bits (insn, 20, 24);
4232 unsigned int op2 = bits (insn, 5, 6);
4233 unsigned int rt = bits (insn, 12, 15);
4234 unsigned int rn = bits (insn, 16, 19);
4235 unsigned int rm = bits (insn, 0, 3);
4236 char load[12] = {0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1};
4237 char bytesize[12] = {2, 2, 2, 2, 8, 1, 8, 1, 8, 2, 8, 2};
4238 int immed = (op1 & 0x4) != 0;
4240 ULONGEST rt_val, rt_val2 = 0, rn_val, rm_val = 0;
4241 CORE_ADDR from = dsc->insn_addr;
4243 if (!insn_references_pc (insn, 0x000ff00ful))
4244 return copy_unmodified (gdbarch, insn, "extra load/store", dsc);
4246 if (debug_displaced)
4247 fprintf_unfiltered (gdb_stdlog, "displaced: copying %sextra load/store "
4248 "insn %.8lx\n", unpriveleged ? "unpriveleged " : "",
4249 (unsigned long) insn);
4251 opcode = ((op2 << 2) | (op1 & 0x1) | ((op1 & 0x4) >> 1)) - 4;
4254 internal_error (__FILE__, __LINE__,
4255 _("copy_extra_ld_st: instruction decode error"));
4257 dsc->tmp[0] = displaced_read_reg (regs, from, 0);
4258 dsc->tmp[1] = displaced_read_reg (regs, from, 1);
4259 dsc->tmp[2] = displaced_read_reg (regs, from, 2);
4261 dsc->tmp[3] = displaced_read_reg (regs, from, 3);
4263 rt_val = displaced_read_reg (regs, from, rt);
4264 if (bytesize[opcode] == 8)
4265 rt_val2 = displaced_read_reg (regs, from, rt + 1);
4266 rn_val = displaced_read_reg (regs, from, rn);
4268 rm_val = displaced_read_reg (regs, from, rm);
4270 displaced_write_reg (regs, dsc, 0, rt_val, CANNOT_WRITE_PC);
4271 if (bytesize[opcode] == 8)
4272 displaced_write_reg (regs, dsc, 1, rt_val2, CANNOT_WRITE_PC);
4273 displaced_write_reg (regs, dsc, 2, rn_val, CANNOT_WRITE_PC);
4275 displaced_write_reg (regs, dsc, 3, rm_val, CANNOT_WRITE_PC);
4278 dsc->u.ldst.xfersize = bytesize[opcode];
4279 dsc->u.ldst.rn = rn;
4280 dsc->u.ldst.immed = immed;
4281 dsc->u.ldst.writeback = bit (insn, 24) == 0 || bit (insn, 21) != 0;
4282 dsc->u.ldst.restore_r4 = 0;
4285 /* {ldr,str}<width><cond> rt, [rt2,] [rn, #imm]
4287 {ldr,str}<width><cond> r0, [r1,] [r2, #imm]. */
4288 dsc->modinsn[0] = (insn & 0xfff00fff) | 0x20000;
4290 /* {ldr,str}<width><cond> rt, [rt2,] [rn, +/-rm]
4292 {ldr,str}<width><cond> r0, [r1,] [r2, +/-r3]. */
4293 dsc->modinsn[0] = (insn & 0xfff00ff0) | 0x20003;
4295 dsc->cleanup = load[opcode] ? &cleanup_load : &cleanup_store;
4300 /* Copy byte/word loads and stores. */
4303 copy_ldr_str_ldrb_strb (struct gdbarch *gdbarch, uint32_t insn,
4304 struct regcache *regs,
4305 struct displaced_step_closure *dsc, int load, int byte,
4308 int immed = !bit (insn, 25);
4309 unsigned int rt = bits (insn, 12, 15);
4310 unsigned int rn = bits (insn, 16, 19);
4311 unsigned int rm = bits (insn, 0, 3); /* Only valid if !immed. */
4312 ULONGEST rt_val, rn_val, rm_val = 0;
4313 CORE_ADDR from = dsc->insn_addr;
4315 if (!insn_references_pc (insn, 0x000ff00ful))
4316 return copy_unmodified (gdbarch, insn, "load/store", dsc);
4318 if (debug_displaced)
4319 fprintf_unfiltered (gdb_stdlog, "displaced: copying %s%s insn %.8lx\n",
4320 load ? (byte ? "ldrb" : "ldr")
4321 : (byte ? "strb" : "str"), usermode ? "t" : "",
4322 (unsigned long) insn);
4324 dsc->tmp[0] = displaced_read_reg (regs, from, 0);
4325 dsc->tmp[2] = displaced_read_reg (regs, from, 2);
4327 dsc->tmp[3] = displaced_read_reg (regs, from, 3);
4329 dsc->tmp[4] = displaced_read_reg (regs, from, 4);
4331 rt_val = displaced_read_reg (regs, from, rt);
4332 rn_val = displaced_read_reg (regs, from, rn);
4334 rm_val = displaced_read_reg (regs, from, rm);
4336 displaced_write_reg (regs, dsc, 0, rt_val, CANNOT_WRITE_PC);
4337 displaced_write_reg (regs, dsc, 2, rn_val, CANNOT_WRITE_PC);
4339 displaced_write_reg (regs, dsc, 3, rm_val, CANNOT_WRITE_PC);
4342 dsc->u.ldst.xfersize = byte ? 1 : 4;
4343 dsc->u.ldst.rn = rn;
4344 dsc->u.ldst.immed = immed;
4345 dsc->u.ldst.writeback = bit (insn, 24) == 0 || bit (insn, 21) != 0;
4347 /* To write PC we can do:
4349 scratch+0: str pc, temp (*temp = scratch + 8 + offset)
4350 scratch+4: ldr r4, temp
4351 scratch+8: sub r4, r4, pc (r4 = scratch + 8 + offset - scratch - 8 - 8)
4352 scratch+12: add r4, r4, #8 (r4 = offset)
4353 scratch+16: add r0, r0, r4
4354 scratch+20: str r0, [r2, #imm] (or str r0, [r2, r3])
4357 Otherwise we don't know what value to write for PC, since the offset is
4358 architecture-dependent (sometimes PC+8, sometimes PC+12). */
4360 if (load || rt != 15)
4362 dsc->u.ldst.restore_r4 = 0;
4365 /* {ldr,str}[b]<cond> rt, [rn, #imm], etc.
4367 {ldr,str}[b]<cond> r0, [r2, #imm]. */
4368 dsc->modinsn[0] = (insn & 0xfff00fff) | 0x20000;
4370 /* {ldr,str}[b]<cond> rt, [rn, rm], etc.
4372 {ldr,str}[b]<cond> r0, [r2, r3]. */
4373 dsc->modinsn[0] = (insn & 0xfff00ff0) | 0x20003;
4377 /* We need to use r4 as scratch. Make sure it's restored afterwards. */
4378 dsc->u.ldst.restore_r4 = 1;
4380 dsc->modinsn[0] = 0xe58ff014; /* str pc, [pc, #20]. */
4381 dsc->modinsn[1] = 0xe59f4010; /* ldr r4, [pc, #16]. */
4382 dsc->modinsn[2] = 0xe044400f; /* sub r4, r4, pc. */
4383 dsc->modinsn[3] = 0xe2844008; /* add r4, r4, #8. */
4384 dsc->modinsn[4] = 0xe0800004; /* add r0, r0, r4. */
4388 dsc->modinsn[5] = (insn & 0xfff00fff) | 0x20000;
4390 dsc->modinsn[5] = (insn & 0xfff00ff0) | 0x20003;
4392 dsc->modinsn[6] = 0x0; /* breakpoint location. */
4393 dsc->modinsn[7] = 0x0; /* scratch space. */
4398 dsc->cleanup = load ? &cleanup_load : &cleanup_store;
4403 /* Cleanup LDM instructions with fully-populated register list. This is an
4404 unfortunate corner case: it's impossible to implement correctly by modifying
4405 the instruction. The issue is as follows: we have an instruction,
4409 which we must rewrite to avoid loading PC. A possible solution would be to
4410 do the load in two halves, something like (with suitable cleanup
4414 ldm[id][ab] r8!, {r0-r7}
4416 ldm[id][ab] r8, {r7-r14}
4419 but at present there's no suitable place for <temp>, since the scratch space
4420 is overwritten before the cleanup routine is called. For now, we simply
4421 emulate the instruction. */
4424 cleanup_block_load_all (struct gdbarch *gdbarch, struct regcache *regs,
4425 struct displaced_step_closure *dsc)
4427 ULONGEST from = dsc->insn_addr;
4428 int inc = dsc->u.block.increment;
4429 int bump_before = dsc->u.block.before ? (inc ? 4 : -4) : 0;
4430 int bump_after = dsc->u.block.before ? 0 : (inc ? 4 : -4);
4431 uint32_t regmask = dsc->u.block.regmask;
4432 int regno = inc ? 0 : 15;
4433 CORE_ADDR xfer_addr = dsc->u.block.xfer_addr;
4434 int exception_return = dsc->u.block.load && dsc->u.block.user
4435 && (regmask & 0x8000) != 0;
4436 uint32_t status = displaced_read_reg (regs, from, ARM_PS_REGNUM);
4437 int do_transfer = condition_true (dsc->u.block.cond, status);
4438 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
4443 /* If the instruction is ldm rN, {...pc}^, I don't think there's anything
4444 sensible we can do here. Complain loudly. */
4445 if (exception_return)
4446 error (_("Cannot single-step exception return"));
4448 /* We don't handle any stores here for now. */
4449 gdb_assert (dsc->u.block.load != 0);
4451 if (debug_displaced)
4452 fprintf_unfiltered (gdb_stdlog, "displaced: emulating block transfer: "
4453 "%s %s %s\n", dsc->u.block.load ? "ldm" : "stm",
4454 dsc->u.block.increment ? "inc" : "dec",
4455 dsc->u.block.before ? "before" : "after");
4462 while (regno <= 15 && (regmask & (1 << regno)) == 0)
4465 while (regno >= 0 && (regmask & (1 << regno)) == 0)
4468 xfer_addr += bump_before;
4470 memword = read_memory_unsigned_integer (xfer_addr, 4, byte_order);
4471 displaced_write_reg (regs, dsc, regno, memword, LOAD_WRITE_PC);
4473 xfer_addr += bump_after;
4475 regmask &= ~(1 << regno);
4478 if (dsc->u.block.writeback)
4479 displaced_write_reg (regs, dsc, dsc->u.block.rn, xfer_addr,
4483 /* Clean up an STM which included the PC in the register list. */
4486 cleanup_block_store_pc (struct gdbarch *gdbarch, struct regcache *regs,
4487 struct displaced_step_closure *dsc)
4489 ULONGEST from = dsc->insn_addr;
4490 uint32_t status = displaced_read_reg (regs, from, ARM_PS_REGNUM);
4491 int store_executed = condition_true (dsc->u.block.cond, status);
4492 CORE_ADDR pc_stored_at, transferred_regs = bitcount (dsc->u.block.regmask);
4493 CORE_ADDR stm_insn_addr;
4496 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
4498 /* If condition code fails, there's nothing else to do. */
4499 if (!store_executed)
4502 if (dsc->u.block.increment)
4504 pc_stored_at = dsc->u.block.xfer_addr + 4 * transferred_regs;
4506 if (dsc->u.block.before)
4511 pc_stored_at = dsc->u.block.xfer_addr;
4513 if (dsc->u.block.before)
4517 pc_val = read_memory_unsigned_integer (pc_stored_at, 4, byte_order);
4518 stm_insn_addr = dsc->scratch_base;
4519 offset = pc_val - stm_insn_addr;
4521 if (debug_displaced)
4522 fprintf_unfiltered (gdb_stdlog, "displaced: detected PC offset %.8lx for "
4523 "STM instruction\n", offset);
4525 /* Rewrite the stored PC to the proper value for the non-displaced original
4527 write_memory_unsigned_integer (pc_stored_at, 4, byte_order,
4528 dsc->insn_addr + offset);
4531 /* Clean up an LDM which includes the PC in the register list. We clumped all
4532 the registers in the transferred list into a contiguous range r0...rX (to
4533 avoid loading PC directly and losing control of the debugged program), so we
4534 must undo that here. */
4537 cleanup_block_load_pc (struct gdbarch *gdbarch,
4538 struct regcache *regs,
4539 struct displaced_step_closure *dsc)
4541 ULONGEST from = dsc->insn_addr;
4542 uint32_t status = displaced_read_reg (regs, from, ARM_PS_REGNUM);
4543 int load_executed = condition_true (dsc->u.block.cond, status), i;
4544 unsigned int mask = dsc->u.block.regmask, write_reg = 15;
4545 unsigned int regs_loaded = bitcount (mask);
4546 unsigned int num_to_shuffle = regs_loaded, clobbered;
4548 /* The method employed here will fail if the register list is fully populated
4549 (we need to avoid loading PC directly). */
4550 gdb_assert (num_to_shuffle < 16);
4555 clobbered = (1 << num_to_shuffle) - 1;
4557 while (num_to_shuffle > 0)
4559 if ((mask & (1 << write_reg)) != 0)
4561 unsigned int read_reg = num_to_shuffle - 1;
4563 if (read_reg != write_reg)
4565 ULONGEST rval = displaced_read_reg (regs, from, read_reg);
4566 displaced_write_reg (regs, dsc, write_reg, rval, LOAD_WRITE_PC);
4567 if (debug_displaced)
4568 fprintf_unfiltered (gdb_stdlog, _("displaced: LDM: move "
4569 "loaded register r%d to r%d\n"), read_reg,
4572 else if (debug_displaced)
4573 fprintf_unfiltered (gdb_stdlog, _("displaced: LDM: register "
4574 "r%d already in the right place\n"),
4577 clobbered &= ~(1 << write_reg);
4585 /* Restore any registers we scribbled over. */
4586 for (write_reg = 0; clobbered != 0; write_reg++)
4588 if ((clobbered & (1 << write_reg)) != 0)
4590 displaced_write_reg (regs, dsc, write_reg, dsc->tmp[write_reg],
4592 if (debug_displaced)
4593 fprintf_unfiltered (gdb_stdlog, _("displaced: LDM: restored "
4594 "clobbered register r%d\n"), write_reg);
4595 clobbered &= ~(1 << write_reg);
4599 /* Perform register writeback manually. */
4600 if (dsc->u.block.writeback)
4602 ULONGEST new_rn_val = dsc->u.block.xfer_addr;
4604 if (dsc->u.block.increment)
4605 new_rn_val += regs_loaded * 4;
4607 new_rn_val -= regs_loaded * 4;
4609 displaced_write_reg (regs, dsc, dsc->u.block.rn, new_rn_val,
4614 /* Handle ldm/stm, apart from some tricky cases which are unlikely to occur
4615 in user-level code (in particular exception return, ldm rn, {...pc}^). */
4618 copy_block_xfer (struct gdbarch *gdbarch, uint32_t insn, struct regcache *regs,
4619 struct displaced_step_closure *dsc)
4621 int load = bit (insn, 20);
4622 int user = bit (insn, 22);
4623 int increment = bit (insn, 23);
4624 int before = bit (insn, 24);
4625 int writeback = bit (insn, 21);
4626 int rn = bits (insn, 16, 19);
4627 CORE_ADDR from = dsc->insn_addr;
4629 /* Block transfers which don't mention PC can be run directly out-of-line. */
4630 if (rn != 15 && (insn & 0x8000) == 0)
4631 return copy_unmodified (gdbarch, insn, "ldm/stm", dsc);
4635 warning (_("displaced: Unpredictable LDM or STM with base register r15"));
4636 return copy_unmodified (gdbarch, insn, "unpredictable ldm/stm", dsc);
4639 if (debug_displaced)
4640 fprintf_unfiltered (gdb_stdlog, "displaced: copying block transfer insn "
4641 "%.8lx\n", (unsigned long) insn);
4643 dsc->u.block.xfer_addr = displaced_read_reg (regs, from, rn);
4644 dsc->u.block.rn = rn;
4646 dsc->u.block.load = load;
4647 dsc->u.block.user = user;
4648 dsc->u.block.increment = increment;
4649 dsc->u.block.before = before;
4650 dsc->u.block.writeback = writeback;
4651 dsc->u.block.cond = bits (insn, 28, 31);
4653 dsc->u.block.regmask = insn & 0xffff;
4657 if ((insn & 0xffff) == 0xffff)
4659 /* LDM with a fully-populated register list. This case is
4660 particularly tricky. Implement for now by fully emulating the
4661 instruction (which might not behave perfectly in all cases, but
4662 these instructions should be rare enough for that not to matter
4664 dsc->modinsn[0] = ARM_NOP;
4666 dsc->cleanup = &cleanup_block_load_all;
4670 /* LDM of a list of registers which includes PC. Implement by
4671 rewriting the list of registers to be transferred into a
4672 contiguous chunk r0...rX before doing the transfer, then shuffling
4673 registers into the correct places in the cleanup routine. */
4674 unsigned int regmask = insn & 0xffff;
4675 unsigned int num_in_list = bitcount (regmask), new_regmask, bit = 1;
4676 unsigned int to = 0, from = 0, i, new_rn;
4678 for (i = 0; i < num_in_list; i++)
4679 dsc->tmp[i] = displaced_read_reg (regs, from, i);
4681 /* Writeback makes things complicated. We need to avoid clobbering
4682 the base register with one of the registers in our modified
4683 register list, but just using a different register can't work in
4686 ldm r14!, {r0-r13,pc}
4688 which would need to be rewritten as:
4692 but that can't work, because there's no free register for N.
4694 Solve this by turning off the writeback bit, and emulating
4695 writeback manually in the cleanup routine. */
4700 new_regmask = (1 << num_in_list) - 1;
4702 if (debug_displaced)
4703 fprintf_unfiltered (gdb_stdlog, _("displaced: LDM r%d%s, "
4704 "{..., pc}: original reg list %.4x, modified "
4705 "list %.4x\n"), rn, writeback ? "!" : "",
4706 (int) insn & 0xffff, new_regmask);
4708 dsc->modinsn[0] = (insn & ~0xffff) | (new_regmask & 0xffff);
4710 dsc->cleanup = &cleanup_block_load_pc;
4715 /* STM of a list of registers which includes PC. Run the instruction
4716 as-is, but out of line: this will store the wrong value for the PC,
4717 so we must manually fix up the memory in the cleanup routine.
4718 Doing things this way has the advantage that we can auto-detect
4719 the offset of the PC write (which is architecture-dependent) in
4720 the cleanup routine. */
4721 dsc->modinsn[0] = insn;
4723 dsc->cleanup = &cleanup_block_store_pc;
4729 /* Cleanup/copy SVC (SWI) instructions. These two functions are overridden
4730 for Linux, where some SVC instructions must be treated specially. */
4733 cleanup_svc (struct gdbarch *gdbarch, struct regcache *regs,
4734 struct displaced_step_closure *dsc)
4736 CORE_ADDR from = dsc->insn_addr;
4737 CORE_ADDR resume_addr = from + 4;
4739 if (debug_displaced)
4740 fprintf_unfiltered (gdb_stdlog, "displaced: cleanup for svc, resume at "
4741 "%.8lx\n", (unsigned long) resume_addr);
4743 displaced_write_reg (regs, dsc, ARM_PC_REGNUM, resume_addr, BRANCH_WRITE_PC);
4747 copy_svc (struct gdbarch *gdbarch, uint32_t insn, CORE_ADDR to,
4748 struct regcache *regs, struct displaced_step_closure *dsc)
4750 CORE_ADDR from = dsc->insn_addr;
4752 /* Allow OS-specific code to override SVC handling. */
4753 if (dsc->u.svc.copy_svc_os)
4754 return dsc->u.svc.copy_svc_os (gdbarch, insn, to, regs, dsc);
4756 if (debug_displaced)
4757 fprintf_unfiltered (gdb_stdlog, "displaced: copying svc insn %.8lx\n",
4758 (unsigned long) insn);
4760 /* Preparation: none.
4761 Insn: unmodified svc.
4762 Cleanup: pc <- insn_addr + 4. */
4764 dsc->modinsn[0] = insn;
4766 dsc->cleanup = &cleanup_svc;
4767 /* Pretend we wrote to the PC, so cleanup doesn't set PC to the next
4769 dsc->wrote_to_pc = 1;
4774 /* Copy undefined instructions. */
4777 copy_undef (struct gdbarch *gdbarch, uint32_t insn,
4778 struct displaced_step_closure *dsc)
4780 if (debug_displaced)
4781 fprintf_unfiltered (gdb_stdlog, "displaced: copying undefined insn %.8lx\n",
4782 (unsigned long) insn);
4784 dsc->modinsn[0] = insn;
4789 /* Copy unpredictable instructions. */
4792 copy_unpred (struct gdbarch *gdbarch, uint32_t insn,
4793 struct displaced_step_closure *dsc)
4795 if (debug_displaced)
4796 fprintf_unfiltered (gdb_stdlog, "displaced: copying unpredictable insn "
4797 "%.8lx\n", (unsigned long) insn);
4799 dsc->modinsn[0] = insn;
4804 /* The decode_* functions are instruction decoding helpers. They mostly follow
4805 the presentation in the ARM ARM. */
4808 decode_misc_memhint_neon (struct gdbarch *gdbarch, uint32_t insn,
4809 struct regcache *regs,
4810 struct displaced_step_closure *dsc)
4812 unsigned int op1 = bits (insn, 20, 26), op2 = bits (insn, 4, 7);
4813 unsigned int rn = bits (insn, 16, 19);
4815 if (op1 == 0x10 && (op2 & 0x2) == 0x0 && (rn & 0xe) == 0x0)
4816 return copy_unmodified (gdbarch, insn, "cps", dsc);
4817 else if (op1 == 0x10 && op2 == 0x0 && (rn & 0xe) == 0x1)
4818 return copy_unmodified (gdbarch, insn, "setend", dsc);
4819 else if ((op1 & 0x60) == 0x20)
4820 return copy_unmodified (gdbarch, insn, "neon dataproc", dsc);
4821 else if ((op1 & 0x71) == 0x40)
4822 return copy_unmodified (gdbarch, insn, "neon elt/struct load/store", dsc);
4823 else if ((op1 & 0x77) == 0x41)
4824 return copy_unmodified (gdbarch, insn, "unallocated mem hint", dsc);
4825 else if ((op1 & 0x77) == 0x45)
4826 return copy_preload (gdbarch, insn, regs, dsc); /* pli. */
4827 else if ((op1 & 0x77) == 0x51)
4830 return copy_preload (gdbarch, insn, regs, dsc); /* pld/pldw. */
4832 return copy_unpred (gdbarch, insn, dsc);
4834 else if ((op1 & 0x77) == 0x55)
4835 return copy_preload (gdbarch, insn, regs, dsc); /* pld/pldw. */
4836 else if (op1 == 0x57)
4839 case 0x1: return copy_unmodified (gdbarch, insn, "clrex", dsc);
4840 case 0x4: return copy_unmodified (gdbarch, insn, "dsb", dsc);
4841 case 0x5: return copy_unmodified (gdbarch, insn, "dmb", dsc);
4842 case 0x6: return copy_unmodified (gdbarch, insn, "isb", dsc);
4843 default: return copy_unpred (gdbarch, insn, dsc);
4845 else if ((op1 & 0x63) == 0x43)
4846 return copy_unpred (gdbarch, insn, dsc);
4847 else if ((op2 & 0x1) == 0x0)
4848 switch (op1 & ~0x80)
4851 return copy_unmodified (gdbarch, insn, "unallocated mem hint", dsc);
4853 return copy_preload_reg (gdbarch, insn, regs, dsc); /* pli reg. */
4854 case 0x71: case 0x75:
4856 return copy_preload_reg (gdbarch, insn, regs, dsc);
4857 case 0x63: case 0x67: case 0x73: case 0x77:
4858 return copy_unpred (gdbarch, insn, dsc);
4860 return copy_undef (gdbarch, insn, dsc);
4863 return copy_undef (gdbarch, insn, dsc); /* Probably unreachable. */
4867 decode_unconditional (struct gdbarch *gdbarch, uint32_t insn,
4868 struct regcache *regs, struct displaced_step_closure *dsc)
4870 if (bit (insn, 27) == 0)
4871 return decode_misc_memhint_neon (gdbarch, insn, regs, dsc);
4872 /* Switch on bits: 0bxxxxx321xxx0xxxxxxxxxxxxxxxxxxxx. */
4873 else switch (((insn & 0x7000000) >> 23) | ((insn & 0x100000) >> 20))
4876 return copy_unmodified (gdbarch, insn, "srs", dsc);
4879 return copy_unmodified (gdbarch, insn, "rfe", dsc);
4881 case 0x4: case 0x5: case 0x6: case 0x7:
4882 return copy_b_bl_blx (gdbarch, insn, regs, dsc);
4885 switch ((insn & 0xe00000) >> 21)
4887 case 0x1: case 0x3: case 0x4: case 0x5: case 0x6: case 0x7:
4889 return copy_copro_load_store (gdbarch, insn, regs, dsc);
4892 return copy_unmodified (gdbarch, insn, "mcrr/mcrr2", dsc);
4895 return copy_undef (gdbarch, insn, dsc);
4900 int rn_f = (bits (insn, 16, 19) == 0xf);
4901 switch ((insn & 0xe00000) >> 21)
4904 /* ldc/ldc2 imm (undefined for rn == pc). */
4905 return rn_f ? copy_undef (gdbarch, insn, dsc)
4906 : copy_copro_load_store (gdbarch, insn, regs, dsc);
4909 return copy_unmodified (gdbarch, insn, "mrrc/mrrc2", dsc);
4911 case 0x4: case 0x5: case 0x6: case 0x7:
4912 /* ldc/ldc2 lit (undefined for rn != pc). */
4913 return rn_f ? copy_copro_load_store (gdbarch, insn, regs, dsc)
4914 : copy_undef (gdbarch, insn, dsc);
4917 return copy_undef (gdbarch, insn, dsc);
4922 return copy_unmodified (gdbarch, insn, "stc/stc2", dsc);
4925 if (bits (insn, 16, 19) == 0xf)
4927 return copy_copro_load_store (gdbarch, insn, regs, dsc);
4929 return copy_undef (gdbarch, insn, dsc);
4933 return copy_unmodified (gdbarch, insn, "mcr/mcr2", dsc);
4935 return copy_unmodified (gdbarch, insn, "cdp/cdp2", dsc);
4939 return copy_unmodified (gdbarch, insn, "mrc/mrc2", dsc);
4941 return copy_unmodified (gdbarch, insn, "cdp/cdp2", dsc);
4944 return copy_undef (gdbarch, insn, dsc);
4948 /* Decode miscellaneous instructions in dp/misc encoding space. */
4951 decode_miscellaneous (struct gdbarch *gdbarch, uint32_t insn,
4952 struct regcache *regs, struct displaced_step_closure *dsc)
4954 unsigned int op2 = bits (insn, 4, 6);
4955 unsigned int op = bits (insn, 21, 22);
4956 unsigned int op1 = bits (insn, 16, 19);
4961 return copy_unmodified (gdbarch, insn, "mrs/msr", dsc);
4964 if (op == 0x1) /* bx. */
4965 return copy_bx_blx_reg (gdbarch, insn, regs, dsc);
4967 return copy_unmodified (gdbarch, insn, "clz", dsc);
4969 return copy_undef (gdbarch, insn, dsc);
4973 /* Not really supported. */
4974 return copy_unmodified (gdbarch, insn, "bxj", dsc);
4976 return copy_undef (gdbarch, insn, dsc);
4980 return copy_bx_blx_reg (gdbarch, insn, regs, dsc); /* blx register. */
4982 return copy_undef (gdbarch, insn, dsc);
4985 return copy_unmodified (gdbarch, insn, "saturating add/sub", dsc);
4989 return copy_unmodified (gdbarch, insn, "bkpt", dsc);
4991 /* Not really supported. */
4992 return copy_unmodified (gdbarch, insn, "smc", dsc);
4995 return copy_undef (gdbarch, insn, dsc);
5000 decode_dp_misc (struct gdbarch *gdbarch, uint32_t insn, struct regcache *regs,
5001 struct displaced_step_closure *dsc)
5004 switch (bits (insn, 20, 24))
5007 return copy_unmodified (gdbarch, insn, "movw", dsc);
5010 return copy_unmodified (gdbarch, insn, "movt", dsc);
5012 case 0x12: case 0x16:
5013 return copy_unmodified (gdbarch, insn, "msr imm", dsc);
5016 return copy_alu_imm (gdbarch, insn, regs, dsc);
5020 uint32_t op1 = bits (insn, 20, 24), op2 = bits (insn, 4, 7);
5022 if ((op1 & 0x19) != 0x10 && (op2 & 0x1) == 0x0)
5023 return copy_alu_reg (gdbarch, insn, regs, dsc);
5024 else if ((op1 & 0x19) != 0x10 && (op2 & 0x9) == 0x1)
5025 return copy_alu_shifted_reg (gdbarch, insn, regs, dsc);
5026 else if ((op1 & 0x19) == 0x10 && (op2 & 0x8) == 0x0)
5027 return decode_miscellaneous (gdbarch, insn, regs, dsc);
5028 else if ((op1 & 0x19) == 0x10 && (op2 & 0x9) == 0x8)
5029 return copy_unmodified (gdbarch, insn, "halfword mul/mla", dsc);
5030 else if ((op1 & 0x10) == 0x00 && op2 == 0x9)
5031 return copy_unmodified (gdbarch, insn, "mul/mla", dsc);
5032 else if ((op1 & 0x10) == 0x10 && op2 == 0x9)
5033 return copy_unmodified (gdbarch, insn, "synch", dsc);
5034 else if (op2 == 0xb || (op2 & 0xd) == 0xd)
5035 /* 2nd arg means "unpriveleged". */
5036 return copy_extra_ld_st (gdbarch, insn, (op1 & 0x12) == 0x02, regs,
5040 /* Should be unreachable. */
5045 decode_ld_st_word_ubyte (struct gdbarch *gdbarch, uint32_t insn,
5046 struct regcache *regs,
5047 struct displaced_step_closure *dsc)
5049 int a = bit (insn, 25), b = bit (insn, 4);
5050 uint32_t op1 = bits (insn, 20, 24);
5051 int rn_f = bits (insn, 16, 19) == 0xf;
5053 if ((!a && (op1 & 0x05) == 0x00 && (op1 & 0x17) != 0x02)
5054 || (a && (op1 & 0x05) == 0x00 && (op1 & 0x17) != 0x02 && !b))
5055 return copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 0, 0, 0);
5056 else if ((!a && (op1 & 0x17) == 0x02)
5057 || (a && (op1 & 0x17) == 0x02 && !b))
5058 return copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 0, 0, 1);
5059 else if ((!a && (op1 & 0x05) == 0x01 && (op1 & 0x17) != 0x03)
5060 || (a && (op1 & 0x05) == 0x01 && (op1 & 0x17) != 0x03 && !b))
5061 return copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 1, 0, 0);
5062 else if ((!a && (op1 & 0x17) == 0x03)
5063 || (a && (op1 & 0x17) == 0x03 && !b))
5064 return copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 1, 0, 1);
5065 else if ((!a && (op1 & 0x05) == 0x04 && (op1 & 0x17) != 0x06)
5066 || (a && (op1 & 0x05) == 0x04 && (op1 & 0x17) != 0x06 && !b))
5067 return copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 0, 1, 0);
5068 else if ((!a && (op1 & 0x17) == 0x06)
5069 || (a && (op1 & 0x17) == 0x06 && !b))
5070 return copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 0, 1, 1);
5071 else if ((!a && (op1 & 0x05) == 0x05 && (op1 & 0x17) != 0x07)
5072 || (a && (op1 & 0x05) == 0x05 && (op1 & 0x17) != 0x07 && !b))
5073 return copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 1, 1, 0);
5074 else if ((!a && (op1 & 0x17) == 0x07)
5075 || (a && (op1 & 0x17) == 0x07 && !b))
5076 return copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 1, 1, 1);
5078 /* Should be unreachable. */
5083 decode_media (struct gdbarch *gdbarch, uint32_t insn,
5084 struct displaced_step_closure *dsc)
5086 switch (bits (insn, 20, 24))
5088 case 0x00: case 0x01: case 0x02: case 0x03:
5089 return copy_unmodified (gdbarch, insn, "parallel add/sub signed", dsc);
5091 case 0x04: case 0x05: case 0x06: case 0x07:
5092 return copy_unmodified (gdbarch, insn, "parallel add/sub unsigned", dsc);
5094 case 0x08: case 0x09: case 0x0a: case 0x0b:
5095 case 0x0c: case 0x0d: case 0x0e: case 0x0f:
5096 return copy_unmodified (gdbarch, insn,
5097 "decode/pack/unpack/saturate/reverse", dsc);
5100 if (bits (insn, 5, 7) == 0) /* op2. */
5102 if (bits (insn, 12, 15) == 0xf)
5103 return copy_unmodified (gdbarch, insn, "usad8", dsc);
5105 return copy_unmodified (gdbarch, insn, "usada8", dsc);
5108 return copy_undef (gdbarch, insn, dsc);
5110 case 0x1a: case 0x1b:
5111 if (bits (insn, 5, 6) == 0x2) /* op2[1:0]. */
5112 return copy_unmodified (gdbarch, insn, "sbfx", dsc);
5114 return copy_undef (gdbarch, insn, dsc);
5116 case 0x1c: case 0x1d:
5117 if (bits (insn, 5, 6) == 0x0) /* op2[1:0]. */
5119 if (bits (insn, 0, 3) == 0xf)
5120 return copy_unmodified (gdbarch, insn, "bfc", dsc);
5122 return copy_unmodified (gdbarch, insn, "bfi", dsc);
5125 return copy_undef (gdbarch, insn, dsc);
5127 case 0x1e: case 0x1f:
5128 if (bits (insn, 5, 6) == 0x2) /* op2[1:0]. */
5129 return copy_unmodified (gdbarch, insn, "ubfx", dsc);
5131 return copy_undef (gdbarch, insn, dsc);
5134 /* Should be unreachable. */
5139 decode_b_bl_ldmstm (struct gdbarch *gdbarch, int32_t insn,
5140 struct regcache *regs, struct displaced_step_closure *dsc)
5143 return copy_b_bl_blx (gdbarch, insn, regs, dsc);
5145 return copy_block_xfer (gdbarch, insn, regs, dsc);
5149 decode_ext_reg_ld_st (struct gdbarch *gdbarch, uint32_t insn,
5150 struct regcache *regs, struct displaced_step_closure *dsc)
5152 unsigned int opcode = bits (insn, 20, 24);
5156 case 0x04: case 0x05: /* VFP/Neon mrrc/mcrr. */
5157 return copy_unmodified (gdbarch, insn, "vfp/neon mrrc/mcrr", dsc);
5159 case 0x08: case 0x0a: case 0x0c: case 0x0e:
5160 case 0x12: case 0x16:
5161 return copy_unmodified (gdbarch, insn, "vfp/neon vstm/vpush", dsc);
5163 case 0x09: case 0x0b: case 0x0d: case 0x0f:
5164 case 0x13: case 0x17:
5165 return copy_unmodified (gdbarch, insn, "vfp/neon vldm/vpop", dsc);
5167 case 0x10: case 0x14: case 0x18: case 0x1c: /* vstr. */
5168 case 0x11: case 0x15: case 0x19: case 0x1d: /* vldr. */
5169 /* Note: no writeback for these instructions. Bit 25 will always be
5170 zero though (via caller), so the following works OK. */
5171 return copy_copro_load_store (gdbarch, insn, regs, dsc);
5174 /* Should be unreachable. */
5179 decode_svc_copro (struct gdbarch *gdbarch, uint32_t insn, CORE_ADDR to,
5180 struct regcache *regs, struct displaced_step_closure *dsc)
5182 unsigned int op1 = bits (insn, 20, 25);
5183 int op = bit (insn, 4);
5184 unsigned int coproc = bits (insn, 8, 11);
5185 unsigned int rn = bits (insn, 16, 19);
5187 if ((op1 & 0x20) == 0x00 && (op1 & 0x3a) != 0x00 && (coproc & 0xe) == 0xa)
5188 return decode_ext_reg_ld_st (gdbarch, insn, regs, dsc);
5189 else if ((op1 & 0x21) == 0x00 && (op1 & 0x3a) != 0x00
5190 && (coproc & 0xe) != 0xa)
5192 return copy_copro_load_store (gdbarch, insn, regs, dsc);
5193 else if ((op1 & 0x21) == 0x01 && (op1 & 0x3a) != 0x00
5194 && (coproc & 0xe) != 0xa)
5195 /* ldc/ldc2 imm/lit. */
5196 return copy_copro_load_store (gdbarch, insn, regs, dsc);
5197 else if ((op1 & 0x3e) == 0x00)
5198 return copy_undef (gdbarch, insn, dsc);
5199 else if ((op1 & 0x3e) == 0x04 && (coproc & 0xe) == 0xa)
5200 return copy_unmodified (gdbarch, insn, "neon 64bit xfer", dsc);
5201 else if (op1 == 0x04 && (coproc & 0xe) != 0xa)
5202 return copy_unmodified (gdbarch, insn, "mcrr/mcrr2", dsc);
5203 else if (op1 == 0x05 && (coproc & 0xe) != 0xa)
5204 return copy_unmodified (gdbarch, insn, "mrrc/mrrc2", dsc);
5205 else if ((op1 & 0x30) == 0x20 && !op)
5207 if ((coproc & 0xe) == 0xa)
5208 return copy_unmodified (gdbarch, insn, "vfp dataproc", dsc);
5210 return copy_unmodified (gdbarch, insn, "cdp/cdp2", dsc);
5212 else if ((op1 & 0x30) == 0x20 && op)
5213 return copy_unmodified (gdbarch, insn, "neon 8/16/32 bit xfer", dsc);
5214 else if ((op1 & 0x31) == 0x20 && op && (coproc & 0xe) != 0xa)
5215 return copy_unmodified (gdbarch, insn, "mcr/mcr2", dsc);
5216 else if ((op1 & 0x31) == 0x21 && op && (coproc & 0xe) != 0xa)
5217 return copy_unmodified (gdbarch, insn, "mrc/mrc2", dsc);
5218 else if ((op1 & 0x30) == 0x30)
5219 return copy_svc (gdbarch, insn, to, regs, dsc);
5221 return copy_undef (gdbarch, insn, dsc); /* Possibly unreachable. */
5225 arm_process_displaced_insn (struct gdbarch *gdbarch, uint32_t insn,
5226 CORE_ADDR from, CORE_ADDR to, struct regcache *regs,
5227 struct displaced_step_closure *dsc)
5231 if (!displaced_in_arm_mode (regs))
5232 error (_("Displaced stepping is only supported in ARM mode"));
5234 /* Most displaced instructions use a 1-instruction scratch space, so set this
5235 here and override below if/when necessary. */
5237 dsc->insn_addr = from;
5238 dsc->scratch_base = to;
5239 dsc->cleanup = NULL;
5240 dsc->wrote_to_pc = 0;
5242 if ((insn & 0xf0000000) == 0xf0000000)
5243 err = decode_unconditional (gdbarch, insn, regs, dsc);
5244 else switch (((insn & 0x10) >> 4) | ((insn & 0xe000000) >> 24))
5246 case 0x0: case 0x1: case 0x2: case 0x3:
5247 err = decode_dp_misc (gdbarch, insn, regs, dsc);
5250 case 0x4: case 0x5: case 0x6:
5251 err = decode_ld_st_word_ubyte (gdbarch, insn, regs, dsc);
5255 err = decode_media (gdbarch, insn, dsc);
5258 case 0x8: case 0x9: case 0xa: case 0xb:
5259 err = decode_b_bl_ldmstm (gdbarch, insn, regs, dsc);
5262 case 0xc: case 0xd: case 0xe: case 0xf:
5263 err = decode_svc_copro (gdbarch, insn, to, regs, dsc);
5268 internal_error (__FILE__, __LINE__,
5269 _("arm_process_displaced_insn: Instruction decode error"));
5272 /* Actually set up the scratch space for a displaced instruction. */
5275 arm_displaced_init_closure (struct gdbarch *gdbarch, CORE_ADDR from,
5276 CORE_ADDR to, struct displaced_step_closure *dsc)
5278 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
5280 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
5282 /* Poke modified instruction(s). */
5283 for (i = 0; i < dsc->numinsns; i++)
5285 if (debug_displaced)
5286 fprintf_unfiltered (gdb_stdlog, "displaced: writing insn %.8lx at "
5287 "%.8lx\n", (unsigned long) dsc->modinsn[i],
5288 (unsigned long) to + i * 4);
5289 write_memory_unsigned_integer (to + i * 4, 4, byte_order_for_code,
5293 /* Put breakpoint afterwards. */
5294 write_memory (to + dsc->numinsns * 4, tdep->arm_breakpoint,
5295 tdep->arm_breakpoint_size);
5297 if (debug_displaced)
5298 fprintf_unfiltered (gdb_stdlog, "displaced: copy %s->%s: ",
5299 paddress (gdbarch, from), paddress (gdbarch, to));
5302 /* Entry point for copying an instruction into scratch space for displaced
5305 struct displaced_step_closure *
5306 arm_displaced_step_copy_insn (struct gdbarch *gdbarch,
5307 CORE_ADDR from, CORE_ADDR to,
5308 struct regcache *regs)
5310 struct displaced_step_closure *dsc
5311 = xmalloc (sizeof (struct displaced_step_closure));
5312 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
5313 uint32_t insn = read_memory_unsigned_integer (from, 4, byte_order_for_code);
5315 if (debug_displaced)
5316 fprintf_unfiltered (gdb_stdlog, "displaced: stepping insn %.8lx "
5317 "at %.8lx\n", (unsigned long) insn,
5318 (unsigned long) from);
5320 arm_process_displaced_insn (gdbarch, insn, from, to, regs, dsc);
5321 arm_displaced_init_closure (gdbarch, from, to, dsc);
5326 /* Entry point for cleaning things up after a displaced instruction has been
5330 arm_displaced_step_fixup (struct gdbarch *gdbarch,
5331 struct displaced_step_closure *dsc,
5332 CORE_ADDR from, CORE_ADDR to,
5333 struct regcache *regs)
5336 dsc->cleanup (gdbarch, regs, dsc);
5338 if (!dsc->wrote_to_pc)
5339 regcache_cooked_write_unsigned (regs, ARM_PC_REGNUM, dsc->insn_addr + 4);
5342 #include "bfd-in2.h"
5343 #include "libcoff.h"
5346 gdb_print_insn_arm (bfd_vma memaddr, disassemble_info *info)
5348 if (arm_pc_is_thumb (memaddr))
5350 static asymbol *asym;
5351 static combined_entry_type ce;
5352 static struct coff_symbol_struct csym;
5353 static struct bfd fake_bfd;
5354 static bfd_target fake_target;
5356 if (csym.native == NULL)
5358 /* Create a fake symbol vector containing a Thumb symbol.
5359 This is solely so that the code in print_insn_little_arm()
5360 and print_insn_big_arm() in opcodes/arm-dis.c will detect
5361 the presence of a Thumb symbol and switch to decoding
5362 Thumb instructions. */
5364 fake_target.flavour = bfd_target_coff_flavour;
5365 fake_bfd.xvec = &fake_target;
5366 ce.u.syment.n_sclass = C_THUMBEXTFUNC;
5368 csym.symbol.the_bfd = &fake_bfd;
5369 csym.symbol.name = "fake";
5370 asym = (asymbol *) & csym;
5373 memaddr = UNMAKE_THUMB_ADDR (memaddr);
5374 info->symbols = &asym;
5377 info->symbols = NULL;
5379 if (info->endian == BFD_ENDIAN_BIG)
5380 return print_insn_big_arm (memaddr, info);
5382 return print_insn_little_arm (memaddr, info);
5385 /* The following define instruction sequences that will cause ARM
5386 cpu's to take an undefined instruction trap. These are used to
5387 signal a breakpoint to GDB.
5389 The newer ARMv4T cpu's are capable of operating in ARM or Thumb
5390 modes. A different instruction is required for each mode. The ARM
5391 cpu's can also be big or little endian. Thus four different
5392 instructions are needed to support all cases.
5394 Note: ARMv4 defines several new instructions that will take the
5395 undefined instruction trap. ARM7TDMI is nominally ARMv4T, but does
5396 not in fact add the new instructions. The new undefined
5397 instructions in ARMv4 are all instructions that had no defined
5398 behaviour in earlier chips. There is no guarantee that they will
5399 raise an exception, but may be treated as NOP's. In practice, it
5400 may only safe to rely on instructions matching:
5402 3 3 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1
5403 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
5404 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
5406 Even this may only true if the condition predicate is true. The
5407 following use a condition predicate of ALWAYS so it is always TRUE.
5409 There are other ways of forcing a breakpoint. GNU/Linux, RISC iX,
5410 and NetBSD all use a software interrupt rather than an undefined
5411 instruction to force a trap. This can be handled by by the
5412 abi-specific code during establishment of the gdbarch vector. */
5414 #define ARM_LE_BREAKPOINT {0xFE,0xDE,0xFF,0xE7}
5415 #define ARM_BE_BREAKPOINT {0xE7,0xFF,0xDE,0xFE}
5416 #define THUMB_LE_BREAKPOINT {0xbe,0xbe}
5417 #define THUMB_BE_BREAKPOINT {0xbe,0xbe}
5419 static const char arm_default_arm_le_breakpoint[] = ARM_LE_BREAKPOINT;
5420 static const char arm_default_arm_be_breakpoint[] = ARM_BE_BREAKPOINT;
5421 static const char arm_default_thumb_le_breakpoint[] = THUMB_LE_BREAKPOINT;
5422 static const char arm_default_thumb_be_breakpoint[] = THUMB_BE_BREAKPOINT;
5424 /* Determine the type and size of breakpoint to insert at PCPTR. Uses
5425 the program counter value to determine whether a 16-bit or 32-bit
5426 breakpoint should be used. It returns a pointer to a string of
5427 bytes that encode a breakpoint instruction, stores the length of
5428 the string to *lenptr, and adjusts the program counter (if
5429 necessary) to point to the actual memory location where the
5430 breakpoint should be inserted. */
5432 static const unsigned char *
5433 arm_breakpoint_from_pc (struct gdbarch *gdbarch, CORE_ADDR *pcptr, int *lenptr)
5435 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
5436 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
5438 if (arm_pc_is_thumb (*pcptr))
5440 *pcptr = UNMAKE_THUMB_ADDR (*pcptr);
5442 /* If we have a separate 32-bit breakpoint instruction for Thumb-2,
5443 check whether we are replacing a 32-bit instruction. */
5444 if (tdep->thumb2_breakpoint != NULL)
5447 if (target_read_memory (*pcptr, buf, 2) == 0)
5449 unsigned short inst1;
5450 inst1 = extract_unsigned_integer (buf, 2, byte_order_for_code);
5451 if ((inst1 & 0xe000) == 0xe000 && (inst1 & 0x1800) != 0)
5453 *lenptr = tdep->thumb2_breakpoint_size;
5454 return tdep->thumb2_breakpoint;
5459 *lenptr = tdep->thumb_breakpoint_size;
5460 return tdep->thumb_breakpoint;
5464 *lenptr = tdep->arm_breakpoint_size;
5465 return tdep->arm_breakpoint;
5470 arm_remote_breakpoint_from_pc (struct gdbarch *gdbarch, CORE_ADDR *pcptr,
5473 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
5475 arm_breakpoint_from_pc (gdbarch, pcptr, kindptr);
5477 if (arm_pc_is_thumb (*pcptr) && *kindptr == 4)
5478 /* The documented magic value for a 32-bit Thumb-2 breakpoint, so
5479 that this is not confused with a 32-bit ARM breakpoint. */
5483 /* Extract from an array REGBUF containing the (raw) register state a
5484 function return value of type TYPE, and copy that, in virtual
5485 format, into VALBUF. */
5488 arm_extract_return_value (struct type *type, struct regcache *regs,
5491 struct gdbarch *gdbarch = get_regcache_arch (regs);
5492 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
5494 if (TYPE_CODE_FLT == TYPE_CODE (type))
5496 switch (gdbarch_tdep (gdbarch)->fp_model)
5500 /* The value is in register F0 in internal format. We need to
5501 extract the raw value and then convert it to the desired
5503 bfd_byte tmpbuf[FP_REGISTER_SIZE];
5505 regcache_cooked_read (regs, ARM_F0_REGNUM, tmpbuf);
5506 convert_from_extended (floatformat_from_type (type), tmpbuf,
5507 valbuf, gdbarch_byte_order (gdbarch));
5511 case ARM_FLOAT_SOFT_FPA:
5512 case ARM_FLOAT_SOFT_VFP:
5513 /* ARM_FLOAT_VFP can arise if this is a variadic function so
5514 not using the VFP ABI code. */
5516 regcache_cooked_read (regs, ARM_A1_REGNUM, valbuf);
5517 if (TYPE_LENGTH (type) > 4)
5518 regcache_cooked_read (regs, ARM_A1_REGNUM + 1,
5519 valbuf + INT_REGISTER_SIZE);
5524 (__FILE__, __LINE__,
5525 _("arm_extract_return_value: Floating point model not supported"));
5529 else if (TYPE_CODE (type) == TYPE_CODE_INT
5530 || TYPE_CODE (type) == TYPE_CODE_CHAR
5531 || TYPE_CODE (type) == TYPE_CODE_BOOL
5532 || TYPE_CODE (type) == TYPE_CODE_PTR
5533 || TYPE_CODE (type) == TYPE_CODE_REF
5534 || TYPE_CODE (type) == TYPE_CODE_ENUM)
5536 /* If the the type is a plain integer, then the access is
5537 straight-forward. Otherwise we have to play around a bit more. */
5538 int len = TYPE_LENGTH (type);
5539 int regno = ARM_A1_REGNUM;
5544 /* By using store_unsigned_integer we avoid having to do
5545 anything special for small big-endian values. */
5546 regcache_cooked_read_unsigned (regs, regno++, &tmp);
5547 store_unsigned_integer (valbuf,
5548 (len > INT_REGISTER_SIZE
5549 ? INT_REGISTER_SIZE : len),
5551 len -= INT_REGISTER_SIZE;
5552 valbuf += INT_REGISTER_SIZE;
5557 /* For a structure or union the behaviour is as if the value had
5558 been stored to word-aligned memory and then loaded into
5559 registers with 32-bit load instruction(s). */
5560 int len = TYPE_LENGTH (type);
5561 int regno = ARM_A1_REGNUM;
5562 bfd_byte tmpbuf[INT_REGISTER_SIZE];
5566 regcache_cooked_read (regs, regno++, tmpbuf);
5567 memcpy (valbuf, tmpbuf,
5568 len > INT_REGISTER_SIZE ? INT_REGISTER_SIZE : len);
5569 len -= INT_REGISTER_SIZE;
5570 valbuf += INT_REGISTER_SIZE;
5576 /* Will a function return an aggregate type in memory or in a
5577 register? Return 0 if an aggregate type can be returned in a
5578 register, 1 if it must be returned in memory. */
5581 arm_return_in_memory (struct gdbarch *gdbarch, struct type *type)
5584 enum type_code code;
5586 CHECK_TYPEDEF (type);
5588 /* In the ARM ABI, "integer" like aggregate types are returned in
5589 registers. For an aggregate type to be integer like, its size
5590 must be less than or equal to INT_REGISTER_SIZE and the
5591 offset of each addressable subfield must be zero. Note that bit
5592 fields are not addressable, and all addressable subfields of
5593 unions always start at offset zero.
5595 This function is based on the behaviour of GCC 2.95.1.
5596 See: gcc/arm.c: arm_return_in_memory() for details.
5598 Note: All versions of GCC before GCC 2.95.2 do not set up the
5599 parameters correctly for a function returning the following
5600 structure: struct { float f;}; This should be returned in memory,
5601 not a register. Richard Earnshaw sent me a patch, but I do not
5602 know of any way to detect if a function like the above has been
5603 compiled with the correct calling convention. */
5605 /* All aggregate types that won't fit in a register must be returned
5607 if (TYPE_LENGTH (type) > INT_REGISTER_SIZE)
5612 /* The AAPCS says all aggregates not larger than a word are returned
5614 if (gdbarch_tdep (gdbarch)->arm_abi != ARM_ABI_APCS)
5617 /* The only aggregate types that can be returned in a register are
5618 structs and unions. Arrays must be returned in memory. */
5619 code = TYPE_CODE (type);
5620 if ((TYPE_CODE_STRUCT != code) && (TYPE_CODE_UNION != code))
5625 /* Assume all other aggregate types can be returned in a register.
5626 Run a check for structures, unions and arrays. */
5629 if ((TYPE_CODE_STRUCT == code) || (TYPE_CODE_UNION == code))
5632 /* Need to check if this struct/union is "integer" like. For
5633 this to be true, its size must be less than or equal to
5634 INT_REGISTER_SIZE and the offset of each addressable
5635 subfield must be zero. Note that bit fields are not
5636 addressable, and unions always start at offset zero. If any
5637 of the subfields is a floating point type, the struct/union
5638 cannot be an integer type. */
5640 /* For each field in the object, check:
5641 1) Is it FP? --> yes, nRc = 1;
5642 2) Is it addressable (bitpos != 0) and
5643 not packed (bitsize == 0)?
5647 for (i = 0; i < TYPE_NFIELDS (type); i++)
5649 enum type_code field_type_code;
5650 field_type_code = TYPE_CODE (check_typedef (TYPE_FIELD_TYPE (type, i)));
5652 /* Is it a floating point type field? */
5653 if (field_type_code == TYPE_CODE_FLT)
5659 /* If bitpos != 0, then we have to care about it. */
5660 if (TYPE_FIELD_BITPOS (type, i) != 0)
5662 /* Bitfields are not addressable. If the field bitsize is
5663 zero, then the field is not packed. Hence it cannot be
5664 a bitfield or any other packed type. */
5665 if (TYPE_FIELD_BITSIZE (type, i) == 0)
5677 /* Write into appropriate registers a function return value of type
5678 TYPE, given in virtual format. */
5681 arm_store_return_value (struct type *type, struct regcache *regs,
5682 const gdb_byte *valbuf)
5684 struct gdbarch *gdbarch = get_regcache_arch (regs);
5685 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
5687 if (TYPE_CODE (type) == TYPE_CODE_FLT)
5689 char buf[MAX_REGISTER_SIZE];
5691 switch (gdbarch_tdep (gdbarch)->fp_model)
5695 convert_to_extended (floatformat_from_type (type), buf, valbuf,
5696 gdbarch_byte_order (gdbarch));
5697 regcache_cooked_write (regs, ARM_F0_REGNUM, buf);
5700 case ARM_FLOAT_SOFT_FPA:
5701 case ARM_FLOAT_SOFT_VFP:
5702 /* ARM_FLOAT_VFP can arise if this is a variadic function so
5703 not using the VFP ABI code. */
5705 regcache_cooked_write (regs, ARM_A1_REGNUM, valbuf);
5706 if (TYPE_LENGTH (type) > 4)
5707 regcache_cooked_write (regs, ARM_A1_REGNUM + 1,
5708 valbuf + INT_REGISTER_SIZE);
5713 (__FILE__, __LINE__,
5714 _("arm_store_return_value: Floating point model not supported"));
5718 else if (TYPE_CODE (type) == TYPE_CODE_INT
5719 || TYPE_CODE (type) == TYPE_CODE_CHAR
5720 || TYPE_CODE (type) == TYPE_CODE_BOOL
5721 || TYPE_CODE (type) == TYPE_CODE_PTR
5722 || TYPE_CODE (type) == TYPE_CODE_REF
5723 || TYPE_CODE (type) == TYPE_CODE_ENUM)
5725 if (TYPE_LENGTH (type) <= 4)
5727 /* Values of one word or less are zero/sign-extended and
5729 bfd_byte tmpbuf[INT_REGISTER_SIZE];
5730 LONGEST val = unpack_long (type, valbuf);
5732 store_signed_integer (tmpbuf, INT_REGISTER_SIZE, byte_order, val);
5733 regcache_cooked_write (regs, ARM_A1_REGNUM, tmpbuf);
5737 /* Integral values greater than one word are stored in consecutive
5738 registers starting with r0. This will always be a multiple of
5739 the regiser size. */
5740 int len = TYPE_LENGTH (type);
5741 int regno = ARM_A1_REGNUM;
5745 regcache_cooked_write (regs, regno++, valbuf);
5746 len -= INT_REGISTER_SIZE;
5747 valbuf += INT_REGISTER_SIZE;
5753 /* For a structure or union the behaviour is as if the value had
5754 been stored to word-aligned memory and then loaded into
5755 registers with 32-bit load instruction(s). */
5756 int len = TYPE_LENGTH (type);
5757 int regno = ARM_A1_REGNUM;
5758 bfd_byte tmpbuf[INT_REGISTER_SIZE];
5762 memcpy (tmpbuf, valbuf,
5763 len > INT_REGISTER_SIZE ? INT_REGISTER_SIZE : len);
5764 regcache_cooked_write (regs, regno++, tmpbuf);
5765 len -= INT_REGISTER_SIZE;
5766 valbuf += INT_REGISTER_SIZE;
5772 /* Handle function return values. */
5774 static enum return_value_convention
5775 arm_return_value (struct gdbarch *gdbarch, struct type *func_type,
5776 struct type *valtype, struct regcache *regcache,
5777 gdb_byte *readbuf, const gdb_byte *writebuf)
5779 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
5780 enum arm_vfp_cprc_base_type vfp_base_type;
5783 if (arm_vfp_abi_for_function (gdbarch, func_type)
5784 && arm_vfp_call_candidate (valtype, &vfp_base_type, &vfp_base_count))
5786 int reg_char = arm_vfp_cprc_reg_char (vfp_base_type);
5787 int unit_length = arm_vfp_cprc_unit_length (vfp_base_type);
5789 for (i = 0; i < vfp_base_count; i++)
5791 if (reg_char == 'q')
5794 arm_neon_quad_write (gdbarch, regcache, i,
5795 writebuf + i * unit_length);
5798 arm_neon_quad_read (gdbarch, regcache, i,
5799 readbuf + i * unit_length);
5806 sprintf (name_buf, "%c%d", reg_char, i);
5807 regnum = user_reg_map_name_to_regnum (gdbarch, name_buf,
5810 regcache_cooked_write (regcache, regnum,
5811 writebuf + i * unit_length);
5813 regcache_cooked_read (regcache, regnum,
5814 readbuf + i * unit_length);
5817 return RETURN_VALUE_REGISTER_CONVENTION;
5820 if (TYPE_CODE (valtype) == TYPE_CODE_STRUCT
5821 || TYPE_CODE (valtype) == TYPE_CODE_UNION
5822 || TYPE_CODE (valtype) == TYPE_CODE_ARRAY)
5824 if (tdep->struct_return == pcc_struct_return
5825 || arm_return_in_memory (gdbarch, valtype))
5826 return RETURN_VALUE_STRUCT_CONVENTION;
5830 arm_store_return_value (valtype, regcache, writebuf);
5833 arm_extract_return_value (valtype, regcache, readbuf);
5835 return RETURN_VALUE_REGISTER_CONVENTION;
5840 arm_get_longjmp_target (struct frame_info *frame, CORE_ADDR *pc)
5842 struct gdbarch *gdbarch = get_frame_arch (frame);
5843 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
5844 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
5846 char buf[INT_REGISTER_SIZE];
5848 jb_addr = get_frame_register_unsigned (frame, ARM_A1_REGNUM);
5850 if (target_read_memory (jb_addr + tdep->jb_pc * tdep->jb_elt_size, buf,
5854 *pc = extract_unsigned_integer (buf, INT_REGISTER_SIZE, byte_order);
5858 /* Recognize GCC and GNU ld's trampolines. If we are in a trampoline,
5859 return the target PC. Otherwise return 0. */
5862 arm_skip_stub (struct frame_info *frame, CORE_ADDR pc)
5866 CORE_ADDR start_addr;
5868 /* Find the starting address and name of the function containing the PC. */
5869 if (find_pc_partial_function (pc, &name, &start_addr, NULL) == 0)
5872 /* If PC is in a Thumb call or return stub, return the address of the
5873 target PC, which is in a register. The thunk functions are called
5874 _call_via_xx, where x is the register name. The possible names
5875 are r0-r9, sl, fp, ip, sp, and lr. ARM RealView has similar
5876 functions, named __ARM_call_via_r[0-7]. */
5877 if (strncmp (name, "_call_via_", 10) == 0
5878 || strncmp (name, "__ARM_call_via_", strlen ("__ARM_call_via_")) == 0)
5880 /* Use the name suffix to determine which register contains the
5882 static char *table[15] =
5883 {"r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
5884 "r8", "r9", "sl", "fp", "ip", "sp", "lr"
5887 int offset = strlen (name) - 2;
5889 for (regno = 0; regno <= 14; regno++)
5890 if (strcmp (&name[offset], table[regno]) == 0)
5891 return get_frame_register_unsigned (frame, regno);
5894 /* GNU ld generates __foo_from_arm or __foo_from_thumb for
5895 non-interworking calls to foo. We could decode the stubs
5896 to find the target but it's easier to use the symbol table. */
5897 namelen = strlen (name);
5898 if (name[0] == '_' && name[1] == '_'
5899 && ((namelen > 2 + strlen ("_from_thumb")
5900 && strncmp (name + namelen - strlen ("_from_thumb"), "_from_thumb",
5901 strlen ("_from_thumb")) == 0)
5902 || (namelen > 2 + strlen ("_from_arm")
5903 && strncmp (name + namelen - strlen ("_from_arm"), "_from_arm",
5904 strlen ("_from_arm")) == 0)))
5907 int target_len = namelen - 2;
5908 struct minimal_symbol *minsym;
5909 struct objfile *objfile;
5910 struct obj_section *sec;
5912 if (name[namelen - 1] == 'b')
5913 target_len -= strlen ("_from_thumb");
5915 target_len -= strlen ("_from_arm");
5917 target_name = alloca (target_len + 1);
5918 memcpy (target_name, name + 2, target_len);
5919 target_name[target_len] = '\0';
5921 sec = find_pc_section (pc);
5922 objfile = (sec == NULL) ? NULL : sec->objfile;
5923 minsym = lookup_minimal_symbol (target_name, NULL, objfile);
5925 return SYMBOL_VALUE_ADDRESS (minsym);
5930 return 0; /* not a stub */
5934 set_arm_command (char *args, int from_tty)
5936 printf_unfiltered (_("\
5937 \"set arm\" must be followed by an apporpriate subcommand.\n"));
5938 help_list (setarmcmdlist, "set arm ", all_commands, gdb_stdout);
5942 show_arm_command (char *args, int from_tty)
5944 cmd_show_list (showarmcmdlist, from_tty, "");
5948 arm_update_current_architecture (void)
5950 struct gdbarch_info info;
5952 /* If the current architecture is not ARM, we have nothing to do. */
5953 if (gdbarch_bfd_arch_info (target_gdbarch)->arch != bfd_arch_arm)
5956 /* Update the architecture. */
5957 gdbarch_info_init (&info);
5959 if (!gdbarch_update_p (info))
5960 internal_error (__FILE__, __LINE__, "could not update architecture");
5964 set_fp_model_sfunc (char *args, int from_tty,
5965 struct cmd_list_element *c)
5967 enum arm_float_model fp_model;
5969 for (fp_model = ARM_FLOAT_AUTO; fp_model != ARM_FLOAT_LAST; fp_model++)
5970 if (strcmp (current_fp_model, fp_model_strings[fp_model]) == 0)
5972 arm_fp_model = fp_model;
5976 if (fp_model == ARM_FLOAT_LAST)
5977 internal_error (__FILE__, __LINE__, _("Invalid fp model accepted: %s."),
5980 arm_update_current_architecture ();
5984 show_fp_model (struct ui_file *file, int from_tty,
5985 struct cmd_list_element *c, const char *value)
5987 struct gdbarch_tdep *tdep = gdbarch_tdep (target_gdbarch);
5989 if (arm_fp_model == ARM_FLOAT_AUTO
5990 && gdbarch_bfd_arch_info (target_gdbarch)->arch == bfd_arch_arm)
5991 fprintf_filtered (file, _("\
5992 The current ARM floating point model is \"auto\" (currently \"%s\").\n"),
5993 fp_model_strings[tdep->fp_model]);
5995 fprintf_filtered (file, _("\
5996 The current ARM floating point model is \"%s\".\n"),
5997 fp_model_strings[arm_fp_model]);
6001 arm_set_abi (char *args, int from_tty,
6002 struct cmd_list_element *c)
6004 enum arm_abi_kind arm_abi;
6006 for (arm_abi = ARM_ABI_AUTO; arm_abi != ARM_ABI_LAST; arm_abi++)
6007 if (strcmp (arm_abi_string, arm_abi_strings[arm_abi]) == 0)
6009 arm_abi_global = arm_abi;
6013 if (arm_abi == ARM_ABI_LAST)
6014 internal_error (__FILE__, __LINE__, _("Invalid ABI accepted: %s."),
6017 arm_update_current_architecture ();
6021 arm_show_abi (struct ui_file *file, int from_tty,
6022 struct cmd_list_element *c, const char *value)
6024 struct gdbarch_tdep *tdep = gdbarch_tdep (target_gdbarch);
6026 if (arm_abi_global == ARM_ABI_AUTO
6027 && gdbarch_bfd_arch_info (target_gdbarch)->arch == bfd_arch_arm)
6028 fprintf_filtered (file, _("\
6029 The current ARM ABI is \"auto\" (currently \"%s\").\n"),
6030 arm_abi_strings[tdep->arm_abi]);
6032 fprintf_filtered (file, _("The current ARM ABI is \"%s\".\n"),
6037 arm_show_fallback_mode (struct ui_file *file, int from_tty,
6038 struct cmd_list_element *c, const char *value)
6040 struct gdbarch_tdep *tdep = gdbarch_tdep (target_gdbarch);
6042 fprintf_filtered (file, _("\
6043 The current execution mode assumed (when symbols are unavailable) is \"%s\".\n"),
6044 arm_fallback_mode_string);
6048 arm_show_force_mode (struct ui_file *file, int from_tty,
6049 struct cmd_list_element *c, const char *value)
6051 struct gdbarch_tdep *tdep = gdbarch_tdep (target_gdbarch);
6053 fprintf_filtered (file, _("\
6054 The current execution mode assumed (even when symbols are available) is \"%s\".\n"),
6055 arm_force_mode_string);
6058 /* If the user changes the register disassembly style used for info
6059 register and other commands, we have to also switch the style used
6060 in opcodes for disassembly output. This function is run in the "set
6061 arm disassembly" command, and does that. */
6064 set_disassembly_style_sfunc (char *args, int from_tty,
6065 struct cmd_list_element *c)
6067 set_disassembly_style ();
6070 /* Return the ARM register name corresponding to register I. */
6072 arm_register_name (struct gdbarch *gdbarch, int i)
6074 const int num_regs = gdbarch_num_regs (gdbarch);
6076 if (gdbarch_tdep (gdbarch)->have_vfp_pseudos
6077 && i >= num_regs && i < num_regs + 32)
6079 static const char *const vfp_pseudo_names[] = {
6080 "s0", "s1", "s2", "s3", "s4", "s5", "s6", "s7",
6081 "s8", "s9", "s10", "s11", "s12", "s13", "s14", "s15",
6082 "s16", "s17", "s18", "s19", "s20", "s21", "s22", "s23",
6083 "s24", "s25", "s26", "s27", "s28", "s29", "s30", "s31",
6086 return vfp_pseudo_names[i - num_regs];
6089 if (gdbarch_tdep (gdbarch)->have_neon_pseudos
6090 && i >= num_regs + 32 && i < num_regs + 32 + 16)
6092 static const char *const neon_pseudo_names[] = {
6093 "q0", "q1", "q2", "q3", "q4", "q5", "q6", "q7",
6094 "q8", "q9", "q10", "q11", "q12", "q13", "q14", "q15",
6097 return neon_pseudo_names[i - num_regs - 32];
6100 if (i >= ARRAY_SIZE (arm_register_names))
6101 /* These registers are only supported on targets which supply
6102 an XML description. */
6105 return arm_register_names[i];
6109 set_disassembly_style (void)
6113 /* Find the style that the user wants. */
6114 for (current = 0; current < num_disassembly_options; current++)
6115 if (disassembly_style == valid_disassembly_styles[current])
6117 gdb_assert (current < num_disassembly_options);
6119 /* Synchronize the disassembler. */
6120 set_arm_regname_option (current);
6123 /* Test whether the coff symbol specific value corresponds to a Thumb
6127 coff_sym_is_thumb (int val)
6129 return (val == C_THUMBEXT
6130 || val == C_THUMBSTAT
6131 || val == C_THUMBEXTFUNC
6132 || val == C_THUMBSTATFUNC
6133 || val == C_THUMBLABEL);
6136 /* arm_coff_make_msymbol_special()
6137 arm_elf_make_msymbol_special()
6139 These functions test whether the COFF or ELF symbol corresponds to
6140 an address in thumb code, and set a "special" bit in a minimal
6141 symbol to indicate that it does. */
6144 arm_elf_make_msymbol_special(asymbol *sym, struct minimal_symbol *msym)
6146 /* Thumb symbols are of type STT_LOPROC, (synonymous with
6148 if (ELF_ST_TYPE (((elf_symbol_type *)sym)->internal_elf_sym.st_info)
6150 MSYMBOL_SET_SPECIAL (msym);
6154 arm_coff_make_msymbol_special(int val, struct minimal_symbol *msym)
6156 if (coff_sym_is_thumb (val))
6157 MSYMBOL_SET_SPECIAL (msym);
6161 arm_objfile_data_free (struct objfile *objfile, void *arg)
6163 struct arm_per_objfile *data = arg;
6166 for (i = 0; i < objfile->obfd->section_count; i++)
6167 VEC_free (arm_mapping_symbol_s, data->section_maps[i]);
6171 arm_record_special_symbol (struct gdbarch *gdbarch, struct objfile *objfile,
6174 const char *name = bfd_asymbol_name (sym);
6175 struct arm_per_objfile *data;
6176 VEC(arm_mapping_symbol_s) **map_p;
6177 struct arm_mapping_symbol new_map_sym;
6179 gdb_assert (name[0] == '$');
6180 if (name[1] != 'a' && name[1] != 't' && name[1] != 'd')
6183 data = objfile_data (objfile, arm_objfile_data_key);
6186 data = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6187 struct arm_per_objfile);
6188 set_objfile_data (objfile, arm_objfile_data_key, data);
6189 data->section_maps = OBSTACK_CALLOC (&objfile->objfile_obstack,
6190 objfile->obfd->section_count,
6191 VEC(arm_mapping_symbol_s) *);
6193 map_p = &data->section_maps[bfd_get_section (sym)->index];
6195 new_map_sym.value = sym->value;
6196 new_map_sym.type = name[1];
6198 /* Assume that most mapping symbols appear in order of increasing
6199 value. If they were randomly distributed, it would be faster to
6200 always push here and then sort at first use. */
6201 if (!VEC_empty (arm_mapping_symbol_s, *map_p))
6203 struct arm_mapping_symbol *prev_map_sym;
6205 prev_map_sym = VEC_last (arm_mapping_symbol_s, *map_p);
6206 if (prev_map_sym->value >= sym->value)
6209 idx = VEC_lower_bound (arm_mapping_symbol_s, *map_p, &new_map_sym,
6210 arm_compare_mapping_symbols);
6211 VEC_safe_insert (arm_mapping_symbol_s, *map_p, idx, &new_map_sym);
6216 VEC_safe_push (arm_mapping_symbol_s, *map_p, &new_map_sym);
6220 arm_write_pc (struct regcache *regcache, CORE_ADDR pc)
6222 regcache_cooked_write_unsigned (regcache, ARM_PC_REGNUM, pc);
6224 /* If necessary, set the T bit. */
6228 regcache_cooked_read_unsigned (regcache, ARM_PS_REGNUM, &val);
6229 if (arm_pc_is_thumb (pc))
6230 regcache_cooked_write_unsigned (regcache, ARM_PS_REGNUM, val | CPSR_T);
6232 regcache_cooked_write_unsigned (regcache, ARM_PS_REGNUM,
6233 val & ~(ULONGEST) CPSR_T);
6237 /* Read the contents of a NEON quad register, by reading from two
6238 double registers. This is used to implement the quad pseudo
6239 registers, and for argument passing in case the quad registers are
6240 missing; vectors are passed in quad registers when using the VFP
6241 ABI, even if a NEON unit is not present. REGNUM is the index of
6242 the quad register, in [0, 15]. */
6245 arm_neon_quad_read (struct gdbarch *gdbarch, struct regcache *regcache,
6246 int regnum, gdb_byte *buf)
6249 gdb_byte reg_buf[8];
6250 int offset, double_regnum;
6252 sprintf (name_buf, "d%d", regnum << 1);
6253 double_regnum = user_reg_map_name_to_regnum (gdbarch, name_buf,
6256 /* d0 is always the least significant half of q0. */
6257 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
6262 regcache_raw_read (regcache, double_regnum, reg_buf);
6263 memcpy (buf + offset, reg_buf, 8);
6265 offset = 8 - offset;
6266 regcache_raw_read (regcache, double_regnum + 1, reg_buf);
6267 memcpy (buf + offset, reg_buf, 8);
6271 arm_pseudo_read (struct gdbarch *gdbarch, struct regcache *regcache,
6272 int regnum, gdb_byte *buf)
6274 const int num_regs = gdbarch_num_regs (gdbarch);
6276 gdb_byte reg_buf[8];
6277 int offset, double_regnum;
6279 gdb_assert (regnum >= num_regs);
6282 if (gdbarch_tdep (gdbarch)->have_neon_pseudos && regnum >= 32 && regnum < 48)
6283 /* Quad-precision register. */
6284 arm_neon_quad_read (gdbarch, regcache, regnum - 32, buf);
6287 /* Single-precision register. */
6288 gdb_assert (regnum < 32);
6290 /* s0 is always the least significant half of d0. */
6291 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
6292 offset = (regnum & 1) ? 0 : 4;
6294 offset = (regnum & 1) ? 4 : 0;
6296 sprintf (name_buf, "d%d", regnum >> 1);
6297 double_regnum = user_reg_map_name_to_regnum (gdbarch, name_buf,
6300 regcache_raw_read (regcache, double_regnum, reg_buf);
6301 memcpy (buf, reg_buf + offset, 4);
6305 /* Store the contents of BUF to a NEON quad register, by writing to
6306 two double registers. This is used to implement the quad pseudo
6307 registers, and for argument passing in case the quad registers are
6308 missing; vectors are passed in quad registers when using the VFP
6309 ABI, even if a NEON unit is not present. REGNUM is the index
6310 of the quad register, in [0, 15]. */
6313 arm_neon_quad_write (struct gdbarch *gdbarch, struct regcache *regcache,
6314 int regnum, const gdb_byte *buf)
6317 gdb_byte reg_buf[8];
6318 int offset, double_regnum;
6320 sprintf (name_buf, "d%d", regnum << 1);
6321 double_regnum = user_reg_map_name_to_regnum (gdbarch, name_buf,
6324 /* d0 is always the least significant half of q0. */
6325 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
6330 regcache_raw_write (regcache, double_regnum, buf + offset);
6331 offset = 8 - offset;
6332 regcache_raw_write (regcache, double_regnum + 1, buf + offset);
6336 arm_pseudo_write (struct gdbarch *gdbarch, struct regcache *regcache,
6337 int regnum, const gdb_byte *buf)
6339 const int num_regs = gdbarch_num_regs (gdbarch);
6341 gdb_byte reg_buf[8];
6342 int offset, double_regnum;
6344 gdb_assert (regnum >= num_regs);
6347 if (gdbarch_tdep (gdbarch)->have_neon_pseudos && regnum >= 32 && regnum < 48)
6348 /* Quad-precision register. */
6349 arm_neon_quad_write (gdbarch, regcache, regnum - 32, buf);
6352 /* Single-precision register. */
6353 gdb_assert (regnum < 32);
6355 /* s0 is always the least significant half of d0. */
6356 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
6357 offset = (regnum & 1) ? 0 : 4;
6359 offset = (regnum & 1) ? 4 : 0;
6361 sprintf (name_buf, "d%d", regnum >> 1);
6362 double_regnum = user_reg_map_name_to_regnum (gdbarch, name_buf,
6365 regcache_raw_read (regcache, double_regnum, reg_buf);
6366 memcpy (reg_buf + offset, buf, 4);
6367 regcache_raw_write (regcache, double_regnum, reg_buf);
6371 static struct value *
6372 value_of_arm_user_reg (struct frame_info *frame, const void *baton)
6374 const int *reg_p = baton;
6375 return value_of_register (*reg_p, frame);
6378 static enum gdb_osabi
6379 arm_elf_osabi_sniffer (bfd *abfd)
6381 unsigned int elfosabi;
6382 enum gdb_osabi osabi = GDB_OSABI_UNKNOWN;
6384 elfosabi = elf_elfheader (abfd)->e_ident[EI_OSABI];
6386 if (elfosabi == ELFOSABI_ARM)
6387 /* GNU tools use this value. Check note sections in this case,
6389 bfd_map_over_sections (abfd,
6390 generic_elf_osabi_sniff_abi_tag_sections,
6393 /* Anything else will be handled by the generic ELF sniffer. */
6398 /* Initialize the current architecture based on INFO. If possible,
6399 re-use an architecture from ARCHES, which is a list of
6400 architectures already created during this debugging session.
6402 Called e.g. at program startup, when reading a core file, and when
6403 reading a binary file. */
6405 static struct gdbarch *
6406 arm_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
6408 struct gdbarch_tdep *tdep;
6409 struct gdbarch *gdbarch;
6410 struct gdbarch_list *best_arch;
6411 enum arm_abi_kind arm_abi = arm_abi_global;
6412 enum arm_float_model fp_model = arm_fp_model;
6413 struct tdesc_arch_data *tdesc_data = NULL;
6415 int have_vfp_registers = 0, have_vfp_pseudos = 0, have_neon_pseudos = 0;
6417 int have_fpa_registers = 1;
6419 /* Check any target description for validity. */
6420 if (tdesc_has_registers (info.target_desc))
6422 /* For most registers we require GDB's default names; but also allow
6423 the numeric names for sp / lr / pc, as a convenience. */
6424 static const char *const arm_sp_names[] = { "r13", "sp", NULL };
6425 static const char *const arm_lr_names[] = { "r14", "lr", NULL };
6426 static const char *const arm_pc_names[] = { "r15", "pc", NULL };
6428 const struct tdesc_feature *feature;
6431 feature = tdesc_find_feature (info.target_desc,
6432 "org.gnu.gdb.arm.core");
6433 if (feature == NULL)
6436 tdesc_data = tdesc_data_alloc ();
6439 for (i = 0; i < ARM_SP_REGNUM; i++)
6440 valid_p &= tdesc_numbered_register (feature, tdesc_data, i,
6441 arm_register_names[i]);
6442 valid_p &= tdesc_numbered_register_choices (feature, tdesc_data,
6445 valid_p &= tdesc_numbered_register_choices (feature, tdesc_data,
6448 valid_p &= tdesc_numbered_register_choices (feature, tdesc_data,
6451 valid_p &= tdesc_numbered_register (feature, tdesc_data,
6452 ARM_PS_REGNUM, "cpsr");
6456 tdesc_data_cleanup (tdesc_data);
6460 feature = tdesc_find_feature (info.target_desc,
6461 "org.gnu.gdb.arm.fpa");
6462 if (feature != NULL)
6465 for (i = ARM_F0_REGNUM; i <= ARM_FPS_REGNUM; i++)
6466 valid_p &= tdesc_numbered_register (feature, tdesc_data, i,
6467 arm_register_names[i]);
6470 tdesc_data_cleanup (tdesc_data);
6475 have_fpa_registers = 0;
6477 feature = tdesc_find_feature (info.target_desc,
6478 "org.gnu.gdb.xscale.iwmmxt");
6479 if (feature != NULL)
6481 static const char *const iwmmxt_names[] = {
6482 "wR0", "wR1", "wR2", "wR3", "wR4", "wR5", "wR6", "wR7",
6483 "wR8", "wR9", "wR10", "wR11", "wR12", "wR13", "wR14", "wR15",
6484 "wCID", "wCon", "wCSSF", "wCASF", "", "", "", "",
6485 "wCGR0", "wCGR1", "wCGR2", "wCGR3", "", "", "", "",
6489 for (i = ARM_WR0_REGNUM; i <= ARM_WR15_REGNUM; i++)
6491 &= tdesc_numbered_register (feature, tdesc_data, i,
6492 iwmmxt_names[i - ARM_WR0_REGNUM]);
6494 /* Check for the control registers, but do not fail if they
6496 for (i = ARM_WC0_REGNUM; i <= ARM_WCASF_REGNUM; i++)
6497 tdesc_numbered_register (feature, tdesc_data, i,
6498 iwmmxt_names[i - ARM_WR0_REGNUM]);
6500 for (i = ARM_WCGR0_REGNUM; i <= ARM_WCGR3_REGNUM; i++)
6502 &= tdesc_numbered_register (feature, tdesc_data, i,
6503 iwmmxt_names[i - ARM_WR0_REGNUM]);
6507 tdesc_data_cleanup (tdesc_data);
6512 /* If we have a VFP unit, check whether the single precision registers
6513 are present. If not, then we will synthesize them as pseudo
6515 feature = tdesc_find_feature (info.target_desc,
6516 "org.gnu.gdb.arm.vfp");
6517 if (feature != NULL)
6519 static const char *const vfp_double_names[] = {
6520 "d0", "d1", "d2", "d3", "d4", "d5", "d6", "d7",
6521 "d8", "d9", "d10", "d11", "d12", "d13", "d14", "d15",
6522 "d16", "d17", "d18", "d19", "d20", "d21", "d22", "d23",
6523 "d24", "d25", "d26", "d27", "d28", "d29", "d30", "d31",
6526 /* Require the double precision registers. There must be either
6529 for (i = 0; i < 32; i++)
6531 valid_p &= tdesc_numbered_register (feature, tdesc_data,
6533 vfp_double_names[i]);
6538 if (!valid_p && i != 16)
6540 tdesc_data_cleanup (tdesc_data);
6544 if (tdesc_unnumbered_register (feature, "s0") == 0)
6545 have_vfp_pseudos = 1;
6547 have_vfp_registers = 1;
6549 /* If we have VFP, also check for NEON. The architecture allows
6550 NEON without VFP (integer vector operations only), but GDB
6551 does not support that. */
6552 feature = tdesc_find_feature (info.target_desc,
6553 "org.gnu.gdb.arm.neon");
6554 if (feature != NULL)
6556 /* NEON requires 32 double-precision registers. */
6559 tdesc_data_cleanup (tdesc_data);
6563 /* If there are quad registers defined by the stub, use
6564 their type; otherwise (normally) provide them with
6565 the default type. */
6566 if (tdesc_unnumbered_register (feature, "q0") == 0)
6567 have_neon_pseudos = 1;
6574 /* If we have an object to base this architecture on, try to determine
6577 if (arm_abi == ARM_ABI_AUTO && info.abfd != NULL)
6579 int ei_osabi, e_flags;
6581 switch (bfd_get_flavour (info.abfd))
6583 case bfd_target_aout_flavour:
6584 /* Assume it's an old APCS-style ABI. */
6585 arm_abi = ARM_ABI_APCS;
6588 case bfd_target_coff_flavour:
6589 /* Assume it's an old APCS-style ABI. */
6591 arm_abi = ARM_ABI_APCS;
6594 case bfd_target_elf_flavour:
6595 ei_osabi = elf_elfheader (info.abfd)->e_ident[EI_OSABI];
6596 e_flags = elf_elfheader (info.abfd)->e_flags;
6598 if (ei_osabi == ELFOSABI_ARM)
6600 /* GNU tools used to use this value, but do not for EABI
6601 objects. There's nowhere to tag an EABI version
6602 anyway, so assume APCS. */
6603 arm_abi = ARM_ABI_APCS;
6605 else if (ei_osabi == ELFOSABI_NONE)
6607 int eabi_ver = EF_ARM_EABI_VERSION (e_flags);
6611 case EF_ARM_EABI_UNKNOWN:
6612 /* Assume GNU tools. */
6613 arm_abi = ARM_ABI_APCS;
6616 case EF_ARM_EABI_VER4:
6617 case EF_ARM_EABI_VER5:
6618 arm_abi = ARM_ABI_AAPCS;
6619 /* EABI binaries default to VFP float ordering.
6620 They may also contain build attributes that can
6621 be used to identify if the VFP argument-passing
6623 if (fp_model == ARM_FLOAT_AUTO)
6626 switch (bfd_elf_get_obj_attr_int (info.abfd,
6631 /* "The user intended FP parameter/result
6632 passing to conform to AAPCS, base
6634 fp_model = ARM_FLOAT_SOFT_VFP;
6637 /* "The user intended FP parameter/result
6638 passing to conform to AAPCS, VFP
6640 fp_model = ARM_FLOAT_VFP;
6643 /* "The user intended FP parameter/result
6644 passing to conform to tool chain-specific
6645 conventions" - we don't know any such
6646 conventions, so leave it as "auto". */
6649 /* Attribute value not mentioned in the
6650 October 2008 ABI, so leave it as
6655 fp_model = ARM_FLOAT_SOFT_VFP;
6661 /* Leave it as "auto". */
6662 warning (_("unknown ARM EABI version 0x%x"), eabi_ver);
6667 if (fp_model == ARM_FLOAT_AUTO)
6669 int e_flags = elf_elfheader (info.abfd)->e_flags;
6671 switch (e_flags & (EF_ARM_SOFT_FLOAT | EF_ARM_VFP_FLOAT))
6674 /* Leave it as "auto". Strictly speaking this case
6675 means FPA, but almost nobody uses that now, and
6676 many toolchains fail to set the appropriate bits
6677 for the floating-point model they use. */
6679 case EF_ARM_SOFT_FLOAT:
6680 fp_model = ARM_FLOAT_SOFT_FPA;
6682 case EF_ARM_VFP_FLOAT:
6683 fp_model = ARM_FLOAT_VFP;
6685 case EF_ARM_SOFT_FLOAT | EF_ARM_VFP_FLOAT:
6686 fp_model = ARM_FLOAT_SOFT_VFP;
6691 if (e_flags & EF_ARM_BE8)
6692 info.byte_order_for_code = BFD_ENDIAN_LITTLE;
6697 /* Leave it as "auto". */
6702 /* If there is already a candidate, use it. */
6703 for (best_arch = gdbarch_list_lookup_by_info (arches, &info);
6705 best_arch = gdbarch_list_lookup_by_info (best_arch->next, &info))
6707 if (arm_abi != ARM_ABI_AUTO
6708 && arm_abi != gdbarch_tdep (best_arch->gdbarch)->arm_abi)
6711 if (fp_model != ARM_FLOAT_AUTO
6712 && fp_model != gdbarch_tdep (best_arch->gdbarch)->fp_model)
6715 /* There are various other properties in tdep that we do not
6716 need to check here: those derived from a target description,
6717 since gdbarches with a different target description are
6718 automatically disqualified. */
6720 /* Found a match. */
6724 if (best_arch != NULL)
6726 if (tdesc_data != NULL)
6727 tdesc_data_cleanup (tdesc_data);
6728 return best_arch->gdbarch;
6731 tdep = xcalloc (1, sizeof (struct gdbarch_tdep));
6732 gdbarch = gdbarch_alloc (&info, tdep);
6734 /* Record additional information about the architecture we are defining.
6735 These are gdbarch discriminators, like the OSABI. */
6736 tdep->arm_abi = arm_abi;
6737 tdep->fp_model = fp_model;
6738 tdep->have_fpa_registers = have_fpa_registers;
6739 tdep->have_vfp_registers = have_vfp_registers;
6740 tdep->have_vfp_pseudos = have_vfp_pseudos;
6741 tdep->have_neon_pseudos = have_neon_pseudos;
6742 tdep->have_neon = have_neon;
6745 switch (info.byte_order_for_code)
6747 case BFD_ENDIAN_BIG:
6748 tdep->arm_breakpoint = arm_default_arm_be_breakpoint;
6749 tdep->arm_breakpoint_size = sizeof (arm_default_arm_be_breakpoint);
6750 tdep->thumb_breakpoint = arm_default_thumb_be_breakpoint;
6751 tdep->thumb_breakpoint_size = sizeof (arm_default_thumb_be_breakpoint);
6755 case BFD_ENDIAN_LITTLE:
6756 tdep->arm_breakpoint = arm_default_arm_le_breakpoint;
6757 tdep->arm_breakpoint_size = sizeof (arm_default_arm_le_breakpoint);
6758 tdep->thumb_breakpoint = arm_default_thumb_le_breakpoint;
6759 tdep->thumb_breakpoint_size = sizeof (arm_default_thumb_le_breakpoint);
6764 internal_error (__FILE__, __LINE__,
6765 _("arm_gdbarch_init: bad byte order for float format"));
6768 /* On ARM targets char defaults to unsigned. */
6769 set_gdbarch_char_signed (gdbarch, 0);
6771 /* Note: for displaced stepping, this includes the breakpoint, and one word
6772 of additional scratch space. This setting isn't used for anything beside
6773 displaced stepping at present. */
6774 set_gdbarch_max_insn_length (gdbarch, 4 * DISPLACED_MODIFIED_INSNS);
6776 /* This should be low enough for everything. */
6777 tdep->lowest_pc = 0x20;
6778 tdep->jb_pc = -1; /* Longjump support not enabled by default. */
6780 /* The default, for both APCS and AAPCS, is to return small
6781 structures in registers. */
6782 tdep->struct_return = reg_struct_return;
6784 set_gdbarch_push_dummy_call (gdbarch, arm_push_dummy_call);
6785 set_gdbarch_frame_align (gdbarch, arm_frame_align);
6787 set_gdbarch_write_pc (gdbarch, arm_write_pc);
6789 /* Frame handling. */
6790 set_gdbarch_dummy_id (gdbarch, arm_dummy_id);
6791 set_gdbarch_unwind_pc (gdbarch, arm_unwind_pc);
6792 set_gdbarch_unwind_sp (gdbarch, arm_unwind_sp);
6794 frame_base_set_default (gdbarch, &arm_normal_base);
6796 /* Address manipulation. */
6797 set_gdbarch_smash_text_address (gdbarch, arm_smash_text_address);
6798 set_gdbarch_addr_bits_remove (gdbarch, arm_addr_bits_remove);
6800 /* Advance PC across function entry code. */
6801 set_gdbarch_skip_prologue (gdbarch, arm_skip_prologue);
6803 /* Skip trampolines. */
6804 set_gdbarch_skip_trampoline_code (gdbarch, arm_skip_stub);
6806 /* The stack grows downward. */
6807 set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
6809 /* Breakpoint manipulation. */
6810 set_gdbarch_breakpoint_from_pc (gdbarch, arm_breakpoint_from_pc);
6811 set_gdbarch_remote_breakpoint_from_pc (gdbarch,
6812 arm_remote_breakpoint_from_pc);
6814 /* Information about registers, etc. */
6815 set_gdbarch_deprecated_fp_regnum (gdbarch, ARM_FP_REGNUM); /* ??? */
6816 set_gdbarch_sp_regnum (gdbarch, ARM_SP_REGNUM);
6817 set_gdbarch_pc_regnum (gdbarch, ARM_PC_REGNUM);
6818 set_gdbarch_num_regs (gdbarch, ARM_NUM_REGS);
6819 set_gdbarch_register_type (gdbarch, arm_register_type);
6821 /* This "info float" is FPA-specific. Use the generic version if we
6823 if (gdbarch_tdep (gdbarch)->have_fpa_registers)
6824 set_gdbarch_print_float_info (gdbarch, arm_print_float_info);
6826 /* Internal <-> external register number maps. */
6827 set_gdbarch_dwarf2_reg_to_regnum (gdbarch, arm_dwarf_reg_to_regnum);
6828 set_gdbarch_register_sim_regno (gdbarch, arm_register_sim_regno);
6830 set_gdbarch_register_name (gdbarch, arm_register_name);
6832 /* Returning results. */
6833 set_gdbarch_return_value (gdbarch, arm_return_value);
6836 set_gdbarch_print_insn (gdbarch, gdb_print_insn_arm);
6838 /* Minsymbol frobbing. */
6839 set_gdbarch_elf_make_msymbol_special (gdbarch, arm_elf_make_msymbol_special);
6840 set_gdbarch_coff_make_msymbol_special (gdbarch,
6841 arm_coff_make_msymbol_special);
6842 set_gdbarch_record_special_symbol (gdbarch, arm_record_special_symbol);
6844 /* Thumb-2 IT block support. */
6845 set_gdbarch_adjust_breakpoint_address (gdbarch,
6846 arm_adjust_breakpoint_address);
6848 /* Virtual tables. */
6849 set_gdbarch_vbit_in_delta (gdbarch, 1);
6851 /* Hook in the ABI-specific overrides, if they have been registered. */
6852 gdbarch_init_osabi (info, gdbarch);
6854 dwarf2_frame_set_init_reg (gdbarch, arm_dwarf2_frame_init_reg);
6856 /* Add some default predicates. */
6857 frame_unwind_append_unwinder (gdbarch, &arm_stub_unwind);
6858 dwarf2_append_unwinders (gdbarch);
6859 frame_unwind_append_unwinder (gdbarch, &arm_prologue_unwind);
6861 /* Now we have tuned the configuration, set a few final things,
6862 based on what the OS ABI has told us. */
6864 /* If the ABI is not otherwise marked, assume the old GNU APCS. EABI
6865 binaries are always marked. */
6866 if (tdep->arm_abi == ARM_ABI_AUTO)
6867 tdep->arm_abi = ARM_ABI_APCS;
6869 /* We used to default to FPA for generic ARM, but almost nobody
6870 uses that now, and we now provide a way for the user to force
6871 the model. So default to the most useful variant. */
6872 if (tdep->fp_model == ARM_FLOAT_AUTO)
6873 tdep->fp_model = ARM_FLOAT_SOFT_FPA;
6875 if (tdep->jb_pc >= 0)
6876 set_gdbarch_get_longjmp_target (gdbarch, arm_get_longjmp_target);
6878 /* Floating point sizes and format. */
6879 set_gdbarch_float_format (gdbarch, floatformats_ieee_single);
6880 if (tdep->fp_model == ARM_FLOAT_SOFT_FPA || tdep->fp_model == ARM_FLOAT_FPA)
6882 set_gdbarch_double_format
6883 (gdbarch, floatformats_ieee_double_littlebyte_bigword);
6884 set_gdbarch_long_double_format
6885 (gdbarch, floatformats_ieee_double_littlebyte_bigword);
6889 set_gdbarch_double_format (gdbarch, floatformats_ieee_double);
6890 set_gdbarch_long_double_format (gdbarch, floatformats_ieee_double);
6893 if (have_vfp_pseudos)
6895 /* NOTE: These are the only pseudo registers used by
6896 the ARM target at the moment. If more are added, a
6897 little more care in numbering will be needed. */
6899 int num_pseudos = 32;
6900 if (have_neon_pseudos)
6902 set_gdbarch_num_pseudo_regs (gdbarch, num_pseudos);
6903 set_gdbarch_pseudo_register_read (gdbarch, arm_pseudo_read);
6904 set_gdbarch_pseudo_register_write (gdbarch, arm_pseudo_write);
6909 set_tdesc_pseudo_register_name (gdbarch, arm_register_name);
6911 tdesc_use_registers (gdbarch, info.target_desc, tdesc_data);
6913 /* Override tdesc_register_type to adjust the types of VFP
6914 registers for NEON. */
6915 set_gdbarch_register_type (gdbarch, arm_register_type);
6918 /* Add standard register aliases. We add aliases even for those
6919 nanes which are used by the current architecture - it's simpler,
6920 and does no harm, since nothing ever lists user registers. */
6921 for (i = 0; i < ARRAY_SIZE (arm_register_aliases); i++)
6922 user_reg_add (gdbarch, arm_register_aliases[i].name,
6923 value_of_arm_user_reg, &arm_register_aliases[i].regnum);
6929 arm_dump_tdep (struct gdbarch *gdbarch, struct ui_file *file)
6931 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
6936 fprintf_unfiltered (file, _("arm_dump_tdep: Lowest pc = 0x%lx"),
6937 (unsigned long) tdep->lowest_pc);
6940 extern initialize_file_ftype _initialize_arm_tdep; /* -Wmissing-prototypes */
6943 _initialize_arm_tdep (void)
6945 struct ui_file *stb;
6947 struct cmd_list_element *new_set, *new_show;
6948 const char *setname;
6949 const char *setdesc;
6950 const char *const *regnames;
6952 static char *helptext;
6953 char regdesc[1024], *rdptr = regdesc;
6954 size_t rest = sizeof (regdesc);
6956 gdbarch_register (bfd_arch_arm, arm_gdbarch_init, arm_dump_tdep);
6958 arm_objfile_data_key
6959 = register_objfile_data_with_cleanup (NULL, arm_objfile_data_free);
6961 /* Register an ELF OS ABI sniffer for ARM binaries. */
6962 gdbarch_register_osabi_sniffer (bfd_arch_arm,
6963 bfd_target_elf_flavour,
6964 arm_elf_osabi_sniffer);
6966 /* Get the number of possible sets of register names defined in opcodes. */
6967 num_disassembly_options = get_arm_regname_num_options ();
6969 /* Add root prefix command for all "set arm"/"show arm" commands. */
6970 add_prefix_cmd ("arm", no_class, set_arm_command,
6971 _("Various ARM-specific commands."),
6972 &setarmcmdlist, "set arm ", 0, &setlist);
6974 add_prefix_cmd ("arm", no_class, show_arm_command,
6975 _("Various ARM-specific commands."),
6976 &showarmcmdlist, "show arm ", 0, &showlist);
6978 /* Sync the opcode insn printer with our register viewer. */
6979 parse_arm_disassembler_option ("reg-names-std");
6981 /* Initialize the array that will be passed to
6982 add_setshow_enum_cmd(). */
6983 valid_disassembly_styles
6984 = xmalloc ((num_disassembly_options + 1) * sizeof (char *));
6985 for (i = 0; i < num_disassembly_options; i++)
6987 numregs = get_arm_regnames (i, &setname, &setdesc, ®names);
6988 valid_disassembly_styles[i] = setname;
6989 length = snprintf (rdptr, rest, "%s - %s\n", setname, setdesc);
6992 /* When we find the default names, tell the disassembler to use
6994 if (!strcmp (setname, "std"))
6996 disassembly_style = setname;
6997 set_arm_regname_option (i);
7000 /* Mark the end of valid options. */
7001 valid_disassembly_styles[num_disassembly_options] = NULL;
7003 /* Create the help text. */
7004 stb = mem_fileopen ();
7005 fprintf_unfiltered (stb, "%s%s%s",
7006 _("The valid values are:\n"),
7008 _("The default is \"std\"."));
7009 helptext = ui_file_xstrdup (stb, NULL);
7010 ui_file_delete (stb);
7012 add_setshow_enum_cmd("disassembler", no_class,
7013 valid_disassembly_styles, &disassembly_style,
7014 _("Set the disassembly style."),
7015 _("Show the disassembly style."),
7017 set_disassembly_style_sfunc,
7018 NULL, /* FIXME: i18n: The disassembly style is \"%s\". */
7019 &setarmcmdlist, &showarmcmdlist);
7021 add_setshow_boolean_cmd ("apcs32", no_class, &arm_apcs_32,
7022 _("Set usage of ARM 32-bit mode."),
7023 _("Show usage of ARM 32-bit mode."),
7024 _("When off, a 26-bit PC will be used."),
7026 NULL, /* FIXME: i18n: Usage of ARM 32-bit mode is %s. */
7027 &setarmcmdlist, &showarmcmdlist);
7029 /* Add a command to allow the user to force the FPU model. */
7030 add_setshow_enum_cmd ("fpu", no_class, fp_model_strings, ¤t_fp_model,
7031 _("Set the floating point type."),
7032 _("Show the floating point type."),
7033 _("auto - Determine the FP typefrom the OS-ABI.\n\
7034 softfpa - Software FP, mixed-endian doubles on little-endian ARMs.\n\
7035 fpa - FPA co-processor (GCC compiled).\n\
7036 softvfp - Software FP with pure-endian doubles.\n\
7037 vfp - VFP co-processor."),
7038 set_fp_model_sfunc, show_fp_model,
7039 &setarmcmdlist, &showarmcmdlist);
7041 /* Add a command to allow the user to force the ABI. */
7042 add_setshow_enum_cmd ("abi", class_support, arm_abi_strings, &arm_abi_string,
7045 NULL, arm_set_abi, arm_show_abi,
7046 &setarmcmdlist, &showarmcmdlist);
7048 /* Add two commands to allow the user to force the assumed
7050 add_setshow_enum_cmd ("fallback-mode", class_support,
7051 arm_mode_strings, &arm_fallback_mode_string,
7052 _("Set the mode assumed when symbols are unavailable."),
7053 _("Show the mode assumed when symbols are unavailable."),
7054 NULL, NULL, arm_show_fallback_mode,
7055 &setarmcmdlist, &showarmcmdlist);
7056 add_setshow_enum_cmd ("force-mode", class_support,
7057 arm_mode_strings, &arm_force_mode_string,
7058 _("Set the mode assumed even when symbols are available."),
7059 _("Show the mode assumed even when symbols are available."),
7060 NULL, NULL, arm_show_force_mode,
7061 &setarmcmdlist, &showarmcmdlist);
7063 /* Debugging flag. */
7064 add_setshow_boolean_cmd ("arm", class_maintenance, &arm_debug,
7065 _("Set ARM debugging."),
7066 _("Show ARM debugging."),
7067 _("When on, arm-specific debugging is enabled."),
7069 NULL, /* FIXME: i18n: "ARM debugging is %s. */
7070 &setdebuglist, &showdebuglist);