Automatic date update in version.in
[binutils-gdb/blckswan.git] / gdb / arm-tdep.c
blob49664093f00da40e7ba6d0c0b867337fc48a2f26
1 /* Common target dependent code for GDB on ARM systems.
3 Copyright (C) 1988-2022 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20 #include "defs.h"
22 #include <ctype.h> /* XXX for isupper (). */
24 #include "frame.h"
25 #include "inferior.h"
26 #include "infrun.h"
27 #include "gdbcmd.h"
28 #include "gdbcore.h"
29 #include "dis-asm.h" /* For register styles. */
30 #include "disasm.h"
31 #include "regcache.h"
32 #include "reggroups.h"
33 #include "target-float.h"
34 #include "value.h"
35 #include "arch-utils.h"
36 #include "osabi.h"
37 #include "frame-unwind.h"
38 #include "frame-base.h"
39 #include "trad-frame.h"
40 #include "objfiles.h"
41 #include "dwarf2.h"
42 #include "dwarf2/frame.h"
43 #include "gdbtypes.h"
44 #include "prologue-value.h"
45 #include "remote.h"
46 #include "target-descriptions.h"
47 #include "user-regs.h"
48 #include "observable.h"
49 #include "count-one-bits.h"
51 #include "arch/arm.h"
52 #include "arch/arm-get-next-pcs.h"
53 #include "arm-tdep.h"
54 #include "gdb/sim-arm.h"
56 #include "elf-bfd.h"
57 #include "coff/internal.h"
58 #include "elf/arm.h"
60 #include "record.h"
61 #include "record-full.h"
62 #include <algorithm>
64 #include "producer.h"
66 #if GDB_SELF_TEST
67 #include "gdbsupport/selftest.h"
68 #endif
70 static bool arm_debug;
72 /* Print an "arm" debug statement. */
74 #define arm_debug_printf(fmt, ...) \
75 debug_prefixed_printf_cond (arm_debug, "arm", fmt, ##__VA_ARGS__)
77 /* Macros for setting and testing a bit in a minimal symbol that marks
78 it as Thumb function. The MSB of the minimal symbol's "info" field
79 is used for this purpose.
81 MSYMBOL_SET_SPECIAL Actually sets the "special" bit.
82 MSYMBOL_IS_SPECIAL Tests the "special" bit in a minimal symbol. */
84 #define MSYMBOL_SET_SPECIAL(msym) \
85 (msym)->set_target_flag_1 (true)
87 #define MSYMBOL_IS_SPECIAL(msym) \
88 (msym)->target_flag_1 ()
90 struct arm_mapping_symbol
92 CORE_ADDR value;
93 char type;
95 bool operator< (const arm_mapping_symbol &other) const
96 { return this->value < other.value; }
99 typedef std::vector<arm_mapping_symbol> arm_mapping_symbol_vec;
101 struct arm_per_bfd
103 explicit arm_per_bfd (size_t num_sections)
104 : section_maps (new arm_mapping_symbol_vec[num_sections]),
105 section_maps_sorted (new bool[num_sections] ())
108 DISABLE_COPY_AND_ASSIGN (arm_per_bfd);
110 /* Information about mapping symbols ($a, $d, $t) in the objfile.
112 The format is an array of vectors of arm_mapping_symbols, there is one
113 vector for each section of the objfile (the array is index by BFD section
114 index).
116 For each section, the vector of arm_mapping_symbol is sorted by
117 symbol value (address). */
118 std::unique_ptr<arm_mapping_symbol_vec[]> section_maps;
120 /* For each corresponding element of section_maps above, is this vector
121 sorted. */
122 std::unique_ptr<bool[]> section_maps_sorted;
125 /* Per-bfd data used for mapping symbols. */
126 static bfd_key<arm_per_bfd> arm_bfd_data_key;
128 /* The list of available "set arm ..." and "show arm ..." commands. */
129 static struct cmd_list_element *setarmcmdlist = NULL;
130 static struct cmd_list_element *showarmcmdlist = NULL;
132 /* The type of floating-point to use. Keep this in sync with enum
133 arm_float_model, and the help string in _initialize_arm_tdep. */
134 static const char *const fp_model_strings[] =
136 "auto",
137 "softfpa",
138 "fpa",
139 "softvfp",
140 "vfp",
141 NULL
144 /* A variable that can be configured by the user. */
145 static enum arm_float_model arm_fp_model = ARM_FLOAT_AUTO;
146 static const char *current_fp_model = "auto";
148 /* The ABI to use. Keep this in sync with arm_abi_kind. */
149 static const char *const arm_abi_strings[] =
151 "auto",
152 "APCS",
153 "AAPCS",
154 NULL
157 /* A variable that can be configured by the user. */
158 static enum arm_abi_kind arm_abi_global = ARM_ABI_AUTO;
159 static const char *arm_abi_string = "auto";
161 /* The execution mode to assume. */
162 static const char *const arm_mode_strings[] =
164 "auto",
165 "arm",
166 "thumb",
167 NULL
170 static const char *arm_fallback_mode_string = "auto";
171 static const char *arm_force_mode_string = "auto";
173 /* The standard register names, and all the valid aliases for them. Note
174 that `fp', `sp' and `pc' are not added in this alias list, because they
175 have been added as builtin user registers in
176 std-regs.c:_initialize_frame_reg. */
177 static const struct
179 const char *name;
180 int regnum;
181 } arm_register_aliases[] = {
182 /* Basic register numbers. */
183 { "r0", 0 },
184 { "r1", 1 },
185 { "r2", 2 },
186 { "r3", 3 },
187 { "r4", 4 },
188 { "r5", 5 },
189 { "r6", 6 },
190 { "r7", 7 },
191 { "r8", 8 },
192 { "r9", 9 },
193 { "r10", 10 },
194 { "r11", 11 },
195 { "r12", 12 },
196 { "r13", 13 },
197 { "r14", 14 },
198 { "r15", 15 },
199 /* Synonyms (argument and variable registers). */
200 { "a1", 0 },
201 { "a2", 1 },
202 { "a3", 2 },
203 { "a4", 3 },
204 { "v1", 4 },
205 { "v2", 5 },
206 { "v3", 6 },
207 { "v4", 7 },
208 { "v5", 8 },
209 { "v6", 9 },
210 { "v7", 10 },
211 { "v8", 11 },
212 /* Other platform-specific names for r9. */
213 { "sb", 9 },
214 { "tr", 9 },
215 /* Special names. */
216 { "ip", 12 },
217 { "lr", 14 },
218 /* Names used by GCC (not listed in the ARM EABI). */
219 { "sl", 10 },
220 /* A special name from the older ATPCS. */
221 { "wr", 7 },
224 static const char *const arm_register_names[] =
225 {"r0", "r1", "r2", "r3", /* 0 1 2 3 */
226 "r4", "r5", "r6", "r7", /* 4 5 6 7 */
227 "r8", "r9", "r10", "r11", /* 8 9 10 11 */
228 "r12", "sp", "lr", "pc", /* 12 13 14 15 */
229 "f0", "f1", "f2", "f3", /* 16 17 18 19 */
230 "f4", "f5", "f6", "f7", /* 20 21 22 23 */
231 "fps", "cpsr" }; /* 24 25 */
233 /* Holds the current set of options to be passed to the disassembler. */
234 static char *arm_disassembler_options;
236 /* Valid register name styles. */
237 static const char **valid_disassembly_styles;
239 /* Disassembly style to use. Default to "std" register names. */
240 static const char *disassembly_style;
242 /* All possible arm target descriptors. */
243 static struct target_desc *tdesc_arm_list[ARM_FP_TYPE_INVALID][2];
244 static struct target_desc *tdesc_arm_mprofile_list[ARM_M_TYPE_INVALID];
246 /* This is used to keep the bfd arch_info in sync with the disassembly
247 style. */
248 static void set_disassembly_style_sfunc (const char *, int,
249 struct cmd_list_element *);
250 static void show_disassembly_style_sfunc (struct ui_file *, int,
251 struct cmd_list_element *,
252 const char *);
254 static enum register_status arm_neon_quad_read (struct gdbarch *gdbarch,
255 readable_regcache *regcache,
256 int regnum, gdb_byte *buf);
257 static void arm_neon_quad_write (struct gdbarch *gdbarch,
258 struct regcache *regcache,
259 int regnum, const gdb_byte *buf);
261 static CORE_ADDR
262 arm_get_next_pcs_syscall_next_pc (struct arm_get_next_pcs *self);
265 /* get_next_pcs operations. */
266 static struct arm_get_next_pcs_ops arm_get_next_pcs_ops = {
267 arm_get_next_pcs_read_memory_unsigned_integer,
268 arm_get_next_pcs_syscall_next_pc,
269 arm_get_next_pcs_addr_bits_remove,
270 arm_get_next_pcs_is_thumb,
271 NULL,
274 struct arm_prologue_cache
276 /* The stack pointer at the time this frame was created; i.e. the
277 caller's stack pointer when this function was called. It is used
278 to identify this frame. */
279 CORE_ADDR sp;
281 /* Additional stack pointers used by M-profile with Security extension. */
282 /* Use msp_s / psp_s to hold the values of msp / psp when there is
283 no Security extension. */
284 CORE_ADDR msp_s;
285 CORE_ADDR msp_ns;
286 CORE_ADDR psp_s;
287 CORE_ADDR psp_ns;
289 /* Active stack pointer. */
290 int active_sp_regnum;
292 /* The frame base for this frame is just prev_sp - frame size.
293 FRAMESIZE is the distance from the frame pointer to the
294 initial stack pointer. */
296 int framesize;
298 /* The register used to hold the frame pointer for this frame. */
299 int framereg;
301 /* True if the return address is signed, false otherwise. */
302 gdb::optional<bool> ra_signed_state;
304 /* Saved register offsets. */
305 trad_frame_saved_reg *saved_regs;
307 arm_prologue_cache() = default;
310 /* Initialize stack pointers, and flag the active one. */
312 static inline void
313 arm_cache_init_sp (int regnum, CORE_ADDR* member,
314 struct arm_prologue_cache *cache,
315 struct frame_info *frame)
317 CORE_ADDR val = get_frame_register_unsigned (frame, regnum);
318 if (val == cache->sp)
319 cache->active_sp_regnum = regnum;
321 *member = val;
324 /* Initialize CACHE fields for which zero is not adequate (CACHE is
325 expected to have been ZALLOC'ed before calling this function). */
327 static void
328 arm_cache_init (struct arm_prologue_cache *cache, struct gdbarch *gdbarch)
330 cache->active_sp_regnum = ARM_SP_REGNUM;
332 cache->saved_regs = trad_frame_alloc_saved_regs (gdbarch);
335 /* Similar to the previous function, but extracts GDBARCH from FRAME. */
337 static void
338 arm_cache_init (struct arm_prologue_cache *cache, struct frame_info *frame)
340 struct gdbarch *gdbarch = get_frame_arch (frame);
341 arm_gdbarch_tdep *tdep = (arm_gdbarch_tdep *) gdbarch_tdep (gdbarch);
343 arm_cache_init (cache, gdbarch);
345 if (tdep->have_sec_ext)
347 arm_cache_init_sp (tdep->m_profile_msp_s_regnum, &cache->msp_s, cache, frame);
348 arm_cache_init_sp (tdep->m_profile_psp_s_regnum, &cache->psp_s, cache, frame);
349 arm_cache_init_sp (tdep->m_profile_msp_ns_regnum, &cache->msp_ns, cache, frame);
350 arm_cache_init_sp (tdep->m_profile_psp_ns_regnum, &cache->psp_ns, cache, frame);
352 /* Use MSP_S as default stack pointer. */
353 if (cache->active_sp_regnum == ARM_SP_REGNUM)
354 cache->active_sp_regnum = tdep->m_profile_msp_s_regnum;
356 else if (tdep->is_m)
358 arm_cache_init_sp (tdep->m_profile_msp_regnum, &cache->msp_s, cache, frame);
359 arm_cache_init_sp (tdep->m_profile_psp_regnum, &cache->psp_s, cache, frame);
361 else
362 arm_cache_init_sp (ARM_SP_REGNUM, &cache->msp_s, cache, frame);
365 /* Return the requested stack pointer value (in REGNUM), taking into
366 account whether we have a Security extension or an M-profile
367 CPU. */
369 static CORE_ADDR
370 arm_cache_get_sp_register (struct arm_prologue_cache *cache,
371 arm_gdbarch_tdep *tdep, int regnum)
373 if (regnum == ARM_SP_REGNUM)
374 return cache->sp;
376 if (tdep->have_sec_ext)
378 if (regnum == tdep->m_profile_msp_s_regnum)
379 return cache->msp_s;
380 if (regnum == tdep->m_profile_msp_ns_regnum)
381 return cache->msp_ns;
382 if (regnum == tdep->m_profile_psp_s_regnum)
383 return cache->psp_s;
384 if (regnum == tdep->m_profile_psp_ns_regnum)
385 return cache->psp_ns;
387 else if (tdep->is_m)
389 if (regnum == tdep->m_profile_msp_regnum)
390 return cache->msp_s;
391 if (regnum == tdep->m_profile_psp_regnum)
392 return cache->psp_s;
395 gdb_assert_not_reached ("Invalid SP selection");
398 /* Return the previous stack address, depending on which SP register
399 is active. */
401 static CORE_ADDR
402 arm_cache_get_prev_sp_value (struct arm_prologue_cache *cache, arm_gdbarch_tdep *tdep)
404 CORE_ADDR val = arm_cache_get_sp_register (cache, tdep, cache->active_sp_regnum);
405 return val;
408 /* Set the active stack pointer to VAL. */
410 static void
411 arm_cache_set_active_sp_value (struct arm_prologue_cache *cache,
412 arm_gdbarch_tdep *tdep, CORE_ADDR val)
414 if (cache->active_sp_regnum == ARM_SP_REGNUM)
416 cache->sp = val;
417 return;
420 if (tdep->have_sec_ext)
422 if (cache->active_sp_regnum == tdep->m_profile_msp_s_regnum)
423 cache->msp_s = val;
424 else if (cache->active_sp_regnum == tdep->m_profile_msp_ns_regnum)
425 cache->msp_ns = val;
426 else if (cache->active_sp_regnum == tdep->m_profile_psp_s_regnum)
427 cache->psp_s = val;
428 else if (cache->active_sp_regnum == tdep->m_profile_psp_ns_regnum)
429 cache->psp_ns = val;
431 return;
433 else if (tdep->is_m)
435 if (cache->active_sp_regnum == tdep->m_profile_msp_regnum)
436 cache->msp_s = val;
437 else if (cache->active_sp_regnum == tdep->m_profile_psp_regnum)
438 cache->psp_s = val;
440 return;
443 gdb_assert_not_reached ("Invalid SP selection");
446 /* Return true if REGNUM is one of the stack pointers. */
448 static bool
449 arm_cache_is_sp_register (struct arm_prologue_cache *cache,
450 arm_gdbarch_tdep *tdep, int regnum)
452 if ((regnum == ARM_SP_REGNUM)
453 || (regnum == tdep->m_profile_msp_regnum)
454 || (regnum == tdep->m_profile_msp_s_regnum)
455 || (regnum == tdep->m_profile_msp_ns_regnum)
456 || (regnum == tdep->m_profile_psp_regnum)
457 || (regnum == tdep->m_profile_psp_s_regnum)
458 || (regnum == tdep->m_profile_psp_ns_regnum))
459 return true;
460 else
461 return false;
464 /* Set the active stack pointer to SP_REGNUM. */
466 static void
467 arm_cache_switch_prev_sp (struct arm_prologue_cache *cache,
468 arm_gdbarch_tdep *tdep, int sp_regnum)
470 gdb_assert (sp_regnum != ARM_SP_REGNUM);
471 gdb_assert (arm_cache_is_sp_register (cache, tdep, sp_regnum));
473 if (tdep->have_sec_ext)
474 gdb_assert (sp_regnum != tdep->m_profile_msp_regnum
475 && sp_regnum != tdep->m_profile_psp_regnum);
477 cache->active_sp_regnum = sp_regnum;
480 namespace {
482 /* Abstract class to read ARM instructions from memory. */
484 class arm_instruction_reader
486 public:
487 /* Read a 4 bytes instruction from memory using the BYTE_ORDER endianness. */
488 virtual uint32_t read (CORE_ADDR memaddr, bfd_endian byte_order) const = 0;
491 /* Read instructions from target memory. */
493 class target_arm_instruction_reader : public arm_instruction_reader
495 public:
496 uint32_t read (CORE_ADDR memaddr, bfd_endian byte_order) const override
498 return read_code_unsigned_integer (memaddr, 4, byte_order);
502 } /* namespace */
504 static CORE_ADDR arm_analyze_prologue
505 (struct gdbarch *gdbarch, CORE_ADDR prologue_start, CORE_ADDR prologue_end,
506 struct arm_prologue_cache *cache, const arm_instruction_reader &insn_reader);
508 /* Architecture version for displaced stepping. This effects the behaviour of
509 certain instructions, and really should not be hard-wired. */
511 #define DISPLACED_STEPPING_ARCH_VERSION 5
513 /* See arm-tdep.h. */
515 bool arm_apcs_32 = true;
516 bool arm_unwind_secure_frames = true;
518 /* Return the bit mask in ARM_PS_REGNUM that indicates Thumb mode. */
521 arm_psr_thumb_bit (struct gdbarch *gdbarch)
523 arm_gdbarch_tdep *tdep = (arm_gdbarch_tdep *) gdbarch_tdep (gdbarch);
525 if (tdep->is_m)
526 return XPSR_T;
527 else
528 return CPSR_T;
531 /* Determine if the processor is currently executing in Thumb mode. */
534 arm_is_thumb (struct regcache *regcache)
536 ULONGEST cpsr;
537 ULONGEST t_bit = arm_psr_thumb_bit (regcache->arch ());
539 cpsr = regcache_raw_get_unsigned (regcache, ARM_PS_REGNUM);
541 return (cpsr & t_bit) != 0;
544 /* Determine if FRAME is executing in Thumb mode. */
547 arm_frame_is_thumb (struct frame_info *frame)
549 CORE_ADDR cpsr;
550 ULONGEST t_bit = arm_psr_thumb_bit (get_frame_arch (frame));
552 /* Every ARM frame unwinder can unwind the T bit of the CPSR, either
553 directly (from a signal frame or dummy frame) or by interpreting
554 the saved LR (from a prologue or DWARF frame). So consult it and
555 trust the unwinders. */
556 cpsr = get_frame_register_unsigned (frame, ARM_PS_REGNUM);
558 return (cpsr & t_bit) != 0;
561 /* Search for the mapping symbol covering MEMADDR. If one is found,
562 return its type. Otherwise, return 0. If START is non-NULL,
563 set *START to the location of the mapping symbol. */
565 static char
566 arm_find_mapping_symbol (CORE_ADDR memaddr, CORE_ADDR *start)
568 struct obj_section *sec;
570 /* If there are mapping symbols, consult them. */
571 sec = find_pc_section (memaddr);
572 if (sec != NULL)
574 arm_per_bfd *data = arm_bfd_data_key.get (sec->objfile->obfd);
575 if (data != NULL)
577 unsigned int section_idx = sec->the_bfd_section->index;
578 arm_mapping_symbol_vec &map
579 = data->section_maps[section_idx];
581 /* Sort the vector on first use. */
582 if (!data->section_maps_sorted[section_idx])
584 std::sort (map.begin (), map.end ());
585 data->section_maps_sorted[section_idx] = true;
588 arm_mapping_symbol map_key = { memaddr - sec->addr (), 0 };
589 arm_mapping_symbol_vec::const_iterator it
590 = std::lower_bound (map.begin (), map.end (), map_key);
592 /* std::lower_bound finds the earliest ordered insertion
593 point. If the symbol at this position starts at this exact
594 address, we use that; otherwise, the preceding
595 mapping symbol covers this address. */
596 if (it < map.end ())
598 if (it->value == map_key.value)
600 if (start)
601 *start = it->value + sec->addr ();
602 return it->type;
606 if (it > map.begin ())
608 arm_mapping_symbol_vec::const_iterator prev_it
609 = it - 1;
611 if (start)
612 *start = prev_it->value + sec->addr ();
613 return prev_it->type;
618 return 0;
621 /* Determine if the program counter specified in MEMADDR is in a Thumb
622 function. This function should be called for addresses unrelated to
623 any executing frame; otherwise, prefer arm_frame_is_thumb. */
626 arm_pc_is_thumb (struct gdbarch *gdbarch, CORE_ADDR memaddr)
628 struct bound_minimal_symbol sym;
629 char type;
630 arm_displaced_step_copy_insn_closure *dsc = nullptr;
631 arm_gdbarch_tdep *tdep = (arm_gdbarch_tdep *) gdbarch_tdep (gdbarch);
633 if (gdbarch_displaced_step_copy_insn_closure_by_addr_p (gdbarch))
634 dsc = ((arm_displaced_step_copy_insn_closure * )
635 gdbarch_displaced_step_copy_insn_closure_by_addr
636 (gdbarch, current_inferior (), memaddr));
638 /* If checking the mode of displaced instruction in copy area, the mode
639 should be determined by instruction on the original address. */
640 if (dsc)
642 displaced_debug_printf ("check mode of %.8lx instead of %.8lx",
643 (unsigned long) dsc->insn_addr,
644 (unsigned long) memaddr);
645 memaddr = dsc->insn_addr;
648 /* If bit 0 of the address is set, assume this is a Thumb address. */
649 if (IS_THUMB_ADDR (memaddr))
650 return 1;
652 /* If the user wants to override the symbol table, let him. */
653 if (strcmp (arm_force_mode_string, "arm") == 0)
654 return 0;
655 if (strcmp (arm_force_mode_string, "thumb") == 0)
656 return 1;
658 /* ARM v6-M and v7-M are always in Thumb mode. */
659 if (tdep->is_m)
660 return 1;
662 /* If there are mapping symbols, consult them. */
663 type = arm_find_mapping_symbol (memaddr, NULL);
664 if (type)
665 return type == 't';
667 /* Thumb functions have a "special" bit set in minimal symbols. */
668 sym = lookup_minimal_symbol_by_pc (memaddr);
669 if (sym.minsym)
670 return (MSYMBOL_IS_SPECIAL (sym.minsym));
672 /* If the user wants to override the fallback mode, let them. */
673 if (strcmp (arm_fallback_mode_string, "arm") == 0)
674 return 0;
675 if (strcmp (arm_fallback_mode_string, "thumb") == 0)
676 return 1;
678 /* If we couldn't find any symbol, but we're talking to a running
679 target, then trust the current value of $cpsr. This lets
680 "display/i $pc" always show the correct mode (though if there is
681 a symbol table we will not reach here, so it still may not be
682 displayed in the mode it will be executed). */
683 if (target_has_registers ())
684 return arm_frame_is_thumb (get_current_frame ());
686 /* Otherwise we're out of luck; we assume ARM. */
687 return 0;
690 /* Determine if the address specified equals any of these magic return
691 values, called EXC_RETURN, defined by the ARM v6-M, v7-M and v8-M
692 architectures.
694 From ARMv6-M Reference Manual B1.5.8
695 Table B1-5 Exception return behavior
697 EXC_RETURN Return To Return Stack
698 0xFFFFFFF1 Handler mode Main
699 0xFFFFFFF9 Thread mode Main
700 0xFFFFFFFD Thread mode Process
702 From ARMv7-M Reference Manual B1.5.8
703 Table B1-8 EXC_RETURN definition of exception return behavior, no FP
705 EXC_RETURN Return To Return Stack
706 0xFFFFFFF1 Handler mode Main
707 0xFFFFFFF9 Thread mode Main
708 0xFFFFFFFD Thread mode Process
710 Table B1-9 EXC_RETURN definition of exception return behavior, with
713 EXC_RETURN Return To Return Stack Frame Type
714 0xFFFFFFE1 Handler mode Main Extended
715 0xFFFFFFE9 Thread mode Main Extended
716 0xFFFFFFED Thread mode Process Extended
717 0xFFFFFFF1 Handler mode Main Basic
718 0xFFFFFFF9 Thread mode Main Basic
719 0xFFFFFFFD Thread mode Process Basic
721 For more details see "B1.5.8 Exception return behavior"
722 in both ARMv6-M and ARMv7-M Architecture Reference Manuals.
724 In the ARMv8-M Architecture Technical Reference also adds
725 for implementations without the Security Extension:
727 EXC_RETURN Condition
728 0xFFFFFFB0 Return to Handler mode.
729 0xFFFFFFB8 Return to Thread mode using the main stack.
730 0xFFFFFFBC Return to Thread mode using the process stack. */
732 static int
733 arm_m_addr_is_magic (struct gdbarch *gdbarch, CORE_ADDR addr)
735 arm_gdbarch_tdep *tdep = (arm_gdbarch_tdep *) gdbarch_tdep (gdbarch);
736 if (tdep->have_sec_ext)
738 switch ((addr & 0xff000000))
740 case 0xff000000: /* EXC_RETURN pattern. */
741 case 0xfe000000: /* FNC_RETURN pattern. */
742 return 1;
743 default:
744 return 0;
747 else
749 switch (addr)
751 /* Values from ARMv8-M Architecture Technical Reference. */
752 case 0xffffffb0:
753 case 0xffffffb8:
754 case 0xffffffbc:
755 /* Values from Tables in B1.5.8 the EXC_RETURN definitions of
756 the exception return behavior. */
757 case 0xffffffe1:
758 case 0xffffffe9:
759 case 0xffffffed:
760 case 0xfffffff1:
761 case 0xfffffff9:
762 case 0xfffffffd:
763 /* Address is magic. */
764 return 1;
766 default:
767 /* Address is not magic. */
768 return 0;
773 /* Remove useless bits from addresses in a running program. */
774 static CORE_ADDR
775 arm_addr_bits_remove (struct gdbarch *gdbarch, CORE_ADDR val)
777 arm_gdbarch_tdep *tdep = (arm_gdbarch_tdep *) gdbarch_tdep (gdbarch);
779 /* On M-profile devices, do not strip the low bit from EXC_RETURN
780 (the magic exception return address). */
781 if (tdep->is_m && arm_m_addr_is_magic (gdbarch, val))
782 return val;
784 if (arm_apcs_32)
785 return UNMAKE_THUMB_ADDR (val);
786 else
787 return (val & 0x03fffffc);
790 /* Return 1 if PC is the start of a compiler helper function which
791 can be safely ignored during prologue skipping. IS_THUMB is true
792 if the function is known to be a Thumb function due to the way it
793 is being called. */
794 static int
795 skip_prologue_function (struct gdbarch *gdbarch, CORE_ADDR pc, int is_thumb)
797 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
798 struct bound_minimal_symbol msym;
800 msym = lookup_minimal_symbol_by_pc (pc);
801 if (msym.minsym != NULL
802 && msym.value_address () == pc
803 && msym.minsym->linkage_name () != NULL)
805 const char *name = msym.minsym->linkage_name ();
807 /* The GNU linker's Thumb call stub to foo is named
808 __foo_from_thumb. */
809 if (strstr (name, "_from_thumb") != NULL)
810 name += 2;
812 /* On soft-float targets, __truncdfsf2 is called to convert promoted
813 arguments to their argument types in non-prototyped
814 functions. */
815 if (startswith (name, "__truncdfsf2"))
816 return 1;
817 if (startswith (name, "__aeabi_d2f"))
818 return 1;
820 /* Internal functions related to thread-local storage. */
821 if (startswith (name, "__tls_get_addr"))
822 return 1;
823 if (startswith (name, "__aeabi_read_tp"))
824 return 1;
826 else
828 /* If we run against a stripped glibc, we may be unable to identify
829 special functions by name. Check for one important case,
830 __aeabi_read_tp, by comparing the *code* against the default
831 implementation (this is hand-written ARM assembler in glibc). */
833 if (!is_thumb
834 && read_code_unsigned_integer (pc, 4, byte_order_for_code)
835 == 0xe3e00a0f /* mov r0, #0xffff0fff */
836 && read_code_unsigned_integer (pc + 4, 4, byte_order_for_code)
837 == 0xe240f01f) /* sub pc, r0, #31 */
838 return 1;
841 return 0;
844 /* Extract the immediate from instruction movw/movt of encoding T. INSN1 is
845 the first 16-bit of instruction, and INSN2 is the second 16-bit of
846 instruction. */
847 #define EXTRACT_MOVW_MOVT_IMM_T(insn1, insn2) \
848 ((bits ((insn1), 0, 3) << 12) \
849 | (bits ((insn1), 10, 10) << 11) \
850 | (bits ((insn2), 12, 14) << 8) \
851 | bits ((insn2), 0, 7))
853 /* Extract the immediate from instruction movw/movt of encoding A. INSN is
854 the 32-bit instruction. */
855 #define EXTRACT_MOVW_MOVT_IMM_A(insn) \
856 ((bits ((insn), 16, 19) << 12) \
857 | bits ((insn), 0, 11))
859 /* Decode immediate value; implements ThumbExpandImmediate pseudo-op. */
861 static unsigned int
862 thumb_expand_immediate (unsigned int imm)
864 unsigned int count = imm >> 7;
866 if (count < 8)
867 switch (count / 2)
869 case 0:
870 return imm & 0xff;
871 case 1:
872 return (imm & 0xff) | ((imm & 0xff) << 16);
873 case 2:
874 return ((imm & 0xff) << 8) | ((imm & 0xff) << 24);
875 case 3:
876 return (imm & 0xff) | ((imm & 0xff) << 8)
877 | ((imm & 0xff) << 16) | ((imm & 0xff) << 24);
880 return (0x80 | (imm & 0x7f)) << (32 - count);
883 /* Return 1 if the 16-bit Thumb instruction INSN restores SP in
884 epilogue, 0 otherwise. */
886 static int
887 thumb_instruction_restores_sp (unsigned short insn)
889 return (insn == 0x46bd /* mov sp, r7 */
890 || (insn & 0xff80) == 0xb000 /* add sp, imm */
891 || (insn & 0xfe00) == 0xbc00); /* pop <registers> */
894 /* Analyze a Thumb prologue, looking for a recognizable stack frame
895 and frame pointer. Scan until we encounter a store that could
896 clobber the stack frame unexpectedly, or an unknown instruction.
897 Return the last address which is definitely safe to skip for an
898 initial breakpoint. */
900 static CORE_ADDR
901 thumb_analyze_prologue (struct gdbarch *gdbarch,
902 CORE_ADDR start, CORE_ADDR limit,
903 struct arm_prologue_cache *cache)
905 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
906 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
907 int i;
908 pv_t regs[16];
909 CORE_ADDR offset;
910 CORE_ADDR unrecognized_pc = 0;
912 for (i = 0; i < 16; i++)
913 regs[i] = pv_register (i, 0);
914 pv_area stack (ARM_SP_REGNUM, gdbarch_addr_bit (gdbarch));
916 while (start < limit)
918 unsigned short insn;
919 gdb::optional<bool> ra_signed_state;
921 insn = read_code_unsigned_integer (start, 2, byte_order_for_code);
923 if ((insn & 0xfe00) == 0xb400) /* push { rlist } */
925 int regno;
926 int mask;
928 if (stack.store_would_trash (regs[ARM_SP_REGNUM]))
929 break;
931 /* Bits 0-7 contain a mask for registers R0-R7. Bit 8 says
932 whether to save LR (R14). */
933 mask = (insn & 0xff) | ((insn & 0x100) << 6);
935 /* Calculate offsets of saved R0-R7 and LR. */
936 for (regno = ARM_LR_REGNUM; regno >= 0; regno--)
937 if (mask & (1 << regno))
939 regs[ARM_SP_REGNUM] = pv_add_constant (regs[ARM_SP_REGNUM],
940 -4);
941 stack.store (regs[ARM_SP_REGNUM], 4, regs[regno]);
944 else if ((insn & 0xff80) == 0xb080) /* sub sp, #imm */
946 offset = (insn & 0x7f) << 2; /* get scaled offset */
947 regs[ARM_SP_REGNUM] = pv_add_constant (regs[ARM_SP_REGNUM],
948 -offset);
950 else if (thumb_instruction_restores_sp (insn))
952 /* Don't scan past the epilogue. */
953 break;
955 else if ((insn & 0xf800) == 0xa800) /* add Rd, sp, #imm */
956 regs[bits (insn, 8, 10)] = pv_add_constant (regs[ARM_SP_REGNUM],
957 (insn & 0xff) << 2);
958 else if ((insn & 0xfe00) == 0x1c00 /* add Rd, Rn, #imm */
959 && pv_is_register (regs[bits (insn, 3, 5)], ARM_SP_REGNUM))
960 regs[bits (insn, 0, 2)] = pv_add_constant (regs[bits (insn, 3, 5)],
961 bits (insn, 6, 8));
962 else if ((insn & 0xf800) == 0x3000 /* add Rd, #imm */
963 && pv_is_register (regs[bits (insn, 8, 10)], ARM_SP_REGNUM))
964 regs[bits (insn, 8, 10)] = pv_add_constant (regs[bits (insn, 8, 10)],
965 bits (insn, 0, 7));
966 else if ((insn & 0xfe00) == 0x1800 /* add Rd, Rn, Rm */
967 && pv_is_register (regs[bits (insn, 6, 8)], ARM_SP_REGNUM)
968 && pv_is_constant (regs[bits (insn, 3, 5)]))
969 regs[bits (insn, 0, 2)] = pv_add (regs[bits (insn, 3, 5)],
970 regs[bits (insn, 6, 8)]);
971 else if ((insn & 0xff00) == 0x4400 /* add Rd, Rm */
972 && pv_is_constant (regs[bits (insn, 3, 6)]))
974 int rd = (bit (insn, 7) << 3) + bits (insn, 0, 2);
975 int rm = bits (insn, 3, 6);
976 regs[rd] = pv_add (regs[rd], regs[rm]);
978 else if ((insn & 0xff00) == 0x4600) /* mov hi, lo or mov lo, hi */
980 int dst_reg = (insn & 0x7) + ((insn & 0x80) >> 4);
981 int src_reg = (insn & 0x78) >> 3;
982 regs[dst_reg] = regs[src_reg];
984 else if ((insn & 0xf800) == 0x9000) /* str rd, [sp, #off] */
986 /* Handle stores to the stack. Normally pushes are used,
987 but with GCC -mtpcs-frame, there may be other stores
988 in the prologue to create the frame. */
989 int regno = (insn >> 8) & 0x7;
990 pv_t addr;
992 offset = (insn & 0xff) << 2;
993 addr = pv_add_constant (regs[ARM_SP_REGNUM], offset);
995 if (stack.store_would_trash (addr))
996 break;
998 stack.store (addr, 4, regs[regno]);
1000 else if ((insn & 0xf800) == 0x6000) /* str rd, [rn, #off] */
1002 int rd = bits (insn, 0, 2);
1003 int rn = bits (insn, 3, 5);
1004 pv_t addr;
1006 offset = bits (insn, 6, 10) << 2;
1007 addr = pv_add_constant (regs[rn], offset);
1009 if (stack.store_would_trash (addr))
1010 break;
1012 stack.store (addr, 4, regs[rd]);
1014 else if (((insn & 0xf800) == 0x7000 /* strb Rd, [Rn, #off] */
1015 || (insn & 0xf800) == 0x8000) /* strh Rd, [Rn, #off] */
1016 && pv_is_register (regs[bits (insn, 3, 5)], ARM_SP_REGNUM))
1017 /* Ignore stores of argument registers to the stack. */
1019 else if ((insn & 0xf800) == 0xc800 /* ldmia Rn!, { registers } */
1020 && pv_is_register (regs[bits (insn, 8, 10)], ARM_SP_REGNUM))
1021 /* Ignore block loads from the stack, potentially copying
1022 parameters from memory. */
1024 else if ((insn & 0xf800) == 0x9800 /* ldr Rd, [Rn, #immed] */
1025 || ((insn & 0xf800) == 0x6800 /* ldr Rd, [sp, #immed] */
1026 && pv_is_register (regs[bits (insn, 3, 5)], ARM_SP_REGNUM)))
1027 /* Similarly ignore single loads from the stack. */
1029 else if ((insn & 0xffc0) == 0x0000 /* lsls Rd, Rm, #0 */
1030 || (insn & 0xffc0) == 0x1c00) /* add Rd, Rn, #0 */
1031 /* Skip register copies, i.e. saves to another register
1032 instead of the stack. */
1034 else if ((insn & 0xf800) == 0x2000) /* movs Rd, #imm */
1035 /* Recognize constant loads; even with small stacks these are necessary
1036 on Thumb. */
1037 regs[bits (insn, 8, 10)] = pv_constant (bits (insn, 0, 7));
1038 else if ((insn & 0xf800) == 0x4800) /* ldr Rd, [pc, #imm] */
1040 /* Constant pool loads, for the same reason. */
1041 unsigned int constant;
1042 CORE_ADDR loc;
1044 loc = start + 4 + bits (insn, 0, 7) * 4;
1045 constant = read_memory_unsigned_integer (loc, 4, byte_order);
1046 regs[bits (insn, 8, 10)] = pv_constant (constant);
1048 else if (thumb_insn_size (insn) == 4) /* 32-bit Thumb-2 instructions. */
1050 unsigned short inst2;
1052 inst2 = read_code_unsigned_integer (start + 2, 2,
1053 byte_order_for_code);
1054 uint32_t whole_insn = (insn << 16) | inst2;
1056 if ((insn & 0xf800) == 0xf000 && (inst2 & 0xe800) == 0xe800)
1058 /* BL, BLX. Allow some special function calls when
1059 skipping the prologue; GCC generates these before
1060 storing arguments to the stack. */
1061 CORE_ADDR nextpc;
1062 int j1, j2, imm1, imm2;
1064 imm1 = sbits (insn, 0, 10);
1065 imm2 = bits (inst2, 0, 10);
1066 j1 = bit (inst2, 13);
1067 j2 = bit (inst2, 11);
1069 offset = ((imm1 << 12) + (imm2 << 1));
1070 offset ^= ((!j2) << 22) | ((!j1) << 23);
1072 nextpc = start + 4 + offset;
1073 /* For BLX make sure to clear the low bits. */
1074 if (bit (inst2, 12) == 0)
1075 nextpc = nextpc & 0xfffffffc;
1077 if (!skip_prologue_function (gdbarch, nextpc,
1078 bit (inst2, 12) != 0))
1079 break;
1082 else if ((insn & 0xffd0) == 0xe900 /* stmdb Rn{!},
1083 { registers } */
1084 && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
1086 pv_t addr = regs[bits (insn, 0, 3)];
1087 int regno;
1089 if (stack.store_would_trash (addr))
1090 break;
1092 /* Calculate offsets of saved registers. */
1093 for (regno = ARM_LR_REGNUM; regno >= 0; regno--)
1094 if (inst2 & (1 << regno))
1096 addr = pv_add_constant (addr, -4);
1097 stack.store (addr, 4, regs[regno]);
1100 if (insn & 0x0020)
1101 regs[bits (insn, 0, 3)] = addr;
1104 /* vstmdb Rn{!}, { D-registers } (aka vpush). */
1105 else if ((insn & 0xff20) == 0xed20
1106 && (inst2 & 0x0f00) == 0x0b00
1107 && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
1109 /* Address SP points to. */
1110 pv_t addr = regs[bits (insn, 0, 3)];
1112 /* Number of registers saved. */
1113 unsigned int number = bits (inst2, 0, 7) >> 1;
1115 /* First register to save. */
1116 int vd = bits (inst2, 12, 15) | (bits (insn, 6, 6) << 4);
1118 if (stack.store_would_trash (addr))
1119 break;
1121 /* Calculate offsets of saved registers. */
1122 for (; number > 0; number--)
1124 addr = pv_add_constant (addr, -8);
1125 stack.store (addr, 8, pv_register (ARM_D0_REGNUM
1126 + vd + number, 0));
1129 /* Writeback SP to account for the saved registers. */
1130 regs[bits (insn, 0, 3)] = addr;
1133 else if ((insn & 0xff50) == 0xe940 /* strd Rt, Rt2,
1134 [Rn, #+/-imm]{!} */
1135 && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
1137 int regno1 = bits (inst2, 12, 15);
1138 int regno2 = bits (inst2, 8, 11);
1139 pv_t addr = regs[bits (insn, 0, 3)];
1141 offset = inst2 & 0xff;
1142 if (insn & 0x0080)
1143 addr = pv_add_constant (addr, offset);
1144 else
1145 addr = pv_add_constant (addr, -offset);
1147 if (stack.store_would_trash (addr))
1148 break;
1150 stack.store (addr, 4, regs[regno1]);
1151 stack.store (pv_add_constant (addr, 4),
1152 4, regs[regno2]);
1154 if (insn & 0x0020)
1155 regs[bits (insn, 0, 3)] = addr;
1158 else if ((insn & 0xfff0) == 0xf8c0 /* str Rt,[Rn,+/-#imm]{!} */
1159 && (inst2 & 0x0c00) == 0x0c00
1160 && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
1162 int regno = bits (inst2, 12, 15);
1163 pv_t addr = regs[bits (insn, 0, 3)];
1165 offset = inst2 & 0xff;
1166 if (inst2 & 0x0200)
1167 addr = pv_add_constant (addr, offset);
1168 else
1169 addr = pv_add_constant (addr, -offset);
1171 if (stack.store_would_trash (addr))
1172 break;
1174 stack.store (addr, 4, regs[regno]);
1176 if (inst2 & 0x0100)
1177 regs[bits (insn, 0, 3)] = addr;
1180 else if ((insn & 0xfff0) == 0xf8c0 /* str.w Rt,[Rn,#imm] */
1181 && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
1183 int regno = bits (inst2, 12, 15);
1184 pv_t addr;
1186 offset = inst2 & 0xfff;
1187 addr = pv_add_constant (regs[bits (insn, 0, 3)], offset);
1189 if (stack.store_would_trash (addr))
1190 break;
1192 stack.store (addr, 4, regs[regno]);
1195 else if ((insn & 0xffd0) == 0xf880 /* str{bh}.w Rt,[Rn,#imm] */
1196 && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
1197 /* Ignore stores of argument registers to the stack. */
1200 else if ((insn & 0xffd0) == 0xf800 /* str{bh} Rt,[Rn,#+/-imm] */
1201 && (inst2 & 0x0d00) == 0x0c00
1202 && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
1203 /* Ignore stores of argument registers to the stack. */
1206 else if ((insn & 0xffd0) == 0xe890 /* ldmia Rn[!],
1207 { registers } */
1208 && (inst2 & 0x8000) == 0x0000
1209 && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
1210 /* Ignore block loads from the stack, potentially copying
1211 parameters from memory. */
1214 else if ((insn & 0xff70) == 0xe950 /* ldrd Rt, Rt2,
1215 [Rn, #+/-imm] */
1216 && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
1217 /* Similarly ignore dual loads from the stack. */
1220 else if ((insn & 0xfff0) == 0xf850 /* ldr Rt,[Rn,#+/-imm] */
1221 && (inst2 & 0x0d00) == 0x0c00
1222 && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
1223 /* Similarly ignore single loads from the stack. */
1226 else if ((insn & 0xfff0) == 0xf8d0 /* ldr.w Rt,[Rn,#imm] */
1227 && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
1228 /* Similarly ignore single loads from the stack. */
1231 else if ((insn & 0xfbf0) == 0xf100 /* add.w Rd, Rn, #imm */
1232 && (inst2 & 0x8000) == 0x0000)
1234 unsigned int imm = ((bits (insn, 10, 10) << 11)
1235 | (bits (inst2, 12, 14) << 8)
1236 | bits (inst2, 0, 7));
1238 regs[bits (inst2, 8, 11)]
1239 = pv_add_constant (regs[bits (insn, 0, 3)],
1240 thumb_expand_immediate (imm));
1243 else if ((insn & 0xfbf0) == 0xf200 /* addw Rd, Rn, #imm */
1244 && (inst2 & 0x8000) == 0x0000)
1246 unsigned int imm = ((bits (insn, 10, 10) << 11)
1247 | (bits (inst2, 12, 14) << 8)
1248 | bits (inst2, 0, 7));
1250 regs[bits (inst2, 8, 11)]
1251 = pv_add_constant (regs[bits (insn, 0, 3)], imm);
1254 else if ((insn & 0xfbf0) == 0xf1a0 /* sub.w Rd, Rn, #imm */
1255 && (inst2 & 0x8000) == 0x0000)
1257 unsigned int imm = ((bits (insn, 10, 10) << 11)
1258 | (bits (inst2, 12, 14) << 8)
1259 | bits (inst2, 0, 7));
1261 regs[bits (inst2, 8, 11)]
1262 = pv_add_constant (regs[bits (insn, 0, 3)],
1263 - (CORE_ADDR) thumb_expand_immediate (imm));
1266 else if ((insn & 0xfbf0) == 0xf2a0 /* subw Rd, Rn, #imm */
1267 && (inst2 & 0x8000) == 0x0000)
1269 unsigned int imm = ((bits (insn, 10, 10) << 11)
1270 | (bits (inst2, 12, 14) << 8)
1271 | bits (inst2, 0, 7));
1273 regs[bits (inst2, 8, 11)]
1274 = pv_add_constant (regs[bits (insn, 0, 3)], - (CORE_ADDR) imm);
1277 else if ((insn & 0xfbff) == 0xf04f) /* mov.w Rd, #const */
1279 unsigned int imm = ((bits (insn, 10, 10) << 11)
1280 | (bits (inst2, 12, 14) << 8)
1281 | bits (inst2, 0, 7));
1283 regs[bits (inst2, 8, 11)]
1284 = pv_constant (thumb_expand_immediate (imm));
1287 else if ((insn & 0xfbf0) == 0xf240) /* movw Rd, #const */
1289 unsigned int imm
1290 = EXTRACT_MOVW_MOVT_IMM_T (insn, inst2);
1292 regs[bits (inst2, 8, 11)] = pv_constant (imm);
1295 else if (insn == 0xea5f /* mov.w Rd,Rm */
1296 && (inst2 & 0xf0f0) == 0)
1298 int dst_reg = (inst2 & 0x0f00) >> 8;
1299 int src_reg = inst2 & 0xf;
1300 regs[dst_reg] = regs[src_reg];
1303 else if ((insn & 0xff7f) == 0xf85f) /* ldr.w Rt,<label> */
1305 /* Constant pool loads. */
1306 unsigned int constant;
1307 CORE_ADDR loc;
1309 offset = bits (inst2, 0, 11);
1310 if (insn & 0x0080)
1311 loc = start + 4 + offset;
1312 else
1313 loc = start + 4 - offset;
1315 constant = read_memory_unsigned_integer (loc, 4, byte_order);
1316 regs[bits (inst2, 12, 15)] = pv_constant (constant);
1319 else if ((insn & 0xff7f) == 0xe95f) /* ldrd Rt,Rt2,<label> */
1321 /* Constant pool loads. */
1322 unsigned int constant;
1323 CORE_ADDR loc;
1325 offset = bits (inst2, 0, 7) << 2;
1326 if (insn & 0x0080)
1327 loc = start + 4 + offset;
1328 else
1329 loc = start + 4 - offset;
1331 constant = read_memory_unsigned_integer (loc, 4, byte_order);
1332 regs[bits (inst2, 12, 15)] = pv_constant (constant);
1334 constant = read_memory_unsigned_integer (loc + 4, 4, byte_order);
1335 regs[bits (inst2, 8, 11)] = pv_constant (constant);
1337 /* Start of ARMv8.1-m PACBTI extension instructions. */
1338 else if (IS_PAC (whole_insn))
1340 /* LR and SP are input registers. PAC is in R12. LR is
1341 signed from this point onwards. NOP space. */
1342 ra_signed_state = true;
1344 else if (IS_PACBTI (whole_insn))
1346 /* LR and SP are input registers. PAC is in R12 and PC is a
1347 valid BTI landing pad. LR is signed from this point onwards.
1348 NOP space. */
1349 ra_signed_state = true;
1351 else if (IS_BTI (whole_insn))
1353 /* Valid BTI landing pad. NOP space. */
1355 else if (IS_PACG (whole_insn))
1357 /* Sign Rn using Rm and store the PAC in Rd. Rd is signed from
1358 this point onwards. */
1359 ra_signed_state = true;
1361 else if (IS_AUT (whole_insn) || IS_AUTG (whole_insn))
1363 /* These instructions appear close to the epilogue, when signed
1364 pointers are getting authenticated. */
1365 ra_signed_state = false;
1367 /* End of ARMv8.1-m PACBTI extension instructions */
1368 else if (thumb2_instruction_changes_pc (insn, inst2))
1370 /* Don't scan past anything that might change control flow. */
1371 break;
1373 else
1375 /* The optimizer might shove anything into the prologue,
1376 so we just skip what we don't recognize. */
1377 unrecognized_pc = start;
1380 arm_gdbarch_tdep *tdep
1381 = (arm_gdbarch_tdep *) gdbarch_tdep (gdbarch);
1383 /* Make sure we are dealing with a target that supports ARMv8.1-m
1384 PACBTI. */
1385 if (cache != nullptr && tdep->have_pacbti
1386 && ra_signed_state.has_value ())
1388 arm_debug_printf ("Found pacbti instruction at %s",
1389 paddress (gdbarch, start));
1390 arm_debug_printf ("RA is %s",
1391 *ra_signed_state? "signed" : "not signed");
1392 cache->ra_signed_state = ra_signed_state;
1395 start += 2;
1397 else if (thumb_instruction_changes_pc (insn))
1399 /* Don't scan past anything that might change control flow. */
1400 break;
1402 else
1404 /* The optimizer might shove anything into the prologue,
1405 so we just skip what we don't recognize. */
1406 unrecognized_pc = start;
1409 start += 2;
1412 arm_debug_printf ("Prologue scan stopped at %s",
1413 paddress (gdbarch, start));
1415 if (unrecognized_pc == 0)
1416 unrecognized_pc = start;
1418 if (cache == NULL)
1419 return unrecognized_pc;
1421 if (pv_is_register (regs[ARM_FP_REGNUM], ARM_SP_REGNUM))
1423 /* Frame pointer is fp. Frame size is constant. */
1424 cache->framereg = ARM_FP_REGNUM;
1425 cache->framesize = -regs[ARM_FP_REGNUM].k;
1427 else if (pv_is_register (regs[THUMB_FP_REGNUM], ARM_SP_REGNUM))
1429 /* Frame pointer is r7. Frame size is constant. */
1430 cache->framereg = THUMB_FP_REGNUM;
1431 cache->framesize = -regs[THUMB_FP_REGNUM].k;
1433 else
1435 /* Try the stack pointer... this is a bit desperate. */
1436 cache->framereg = ARM_SP_REGNUM;
1437 cache->framesize = -regs[ARM_SP_REGNUM].k;
1440 for (i = 0; i < 16; i++)
1441 if (stack.find_reg (gdbarch, i, &offset))
1442 cache->saved_regs[i].set_addr (offset);
1444 return unrecognized_pc;
1448 /* Try to analyze the instructions starting from PC, which load symbol
1449 __stack_chk_guard. Return the address of instruction after loading this
1450 symbol, set the dest register number to *BASEREG, and set the size of
1451 instructions for loading symbol in OFFSET. Return 0 if instructions are
1452 not recognized. */
1454 static CORE_ADDR
1455 arm_analyze_load_stack_chk_guard(CORE_ADDR pc, struct gdbarch *gdbarch,
1456 unsigned int *destreg, int *offset)
1458 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
1459 int is_thumb = arm_pc_is_thumb (gdbarch, pc);
1460 unsigned int low, high, address;
1462 address = 0;
1463 if (is_thumb)
1465 unsigned short insn1
1466 = read_code_unsigned_integer (pc, 2, byte_order_for_code);
1468 if ((insn1 & 0xf800) == 0x4800) /* ldr Rd, #immed */
1470 *destreg = bits (insn1, 8, 10);
1471 *offset = 2;
1472 address = (pc & 0xfffffffc) + 4 + (bits (insn1, 0, 7) << 2);
1473 address = read_memory_unsigned_integer (address, 4,
1474 byte_order_for_code);
1476 else if ((insn1 & 0xfbf0) == 0xf240) /* movw Rd, #const */
1478 unsigned short insn2
1479 = read_code_unsigned_integer (pc + 2, 2, byte_order_for_code);
1481 low = EXTRACT_MOVW_MOVT_IMM_T (insn1, insn2);
1483 insn1
1484 = read_code_unsigned_integer (pc + 4, 2, byte_order_for_code);
1485 insn2
1486 = read_code_unsigned_integer (pc + 6, 2, byte_order_for_code);
1488 /* movt Rd, #const */
1489 if ((insn1 & 0xfbc0) == 0xf2c0)
1491 high = EXTRACT_MOVW_MOVT_IMM_T (insn1, insn2);
1492 *destreg = bits (insn2, 8, 11);
1493 *offset = 8;
1494 address = (high << 16 | low);
1498 else
1500 unsigned int insn
1501 = read_code_unsigned_integer (pc, 4, byte_order_for_code);
1503 if ((insn & 0x0e5f0000) == 0x041f0000) /* ldr Rd, [PC, #immed] */
1505 address = bits (insn, 0, 11) + pc + 8;
1506 address = read_memory_unsigned_integer (address, 4,
1507 byte_order_for_code);
1509 *destreg = bits (insn, 12, 15);
1510 *offset = 4;
1512 else if ((insn & 0x0ff00000) == 0x03000000) /* movw Rd, #const */
1514 low = EXTRACT_MOVW_MOVT_IMM_A (insn);
1516 insn
1517 = read_code_unsigned_integer (pc + 4, 4, byte_order_for_code);
1519 if ((insn & 0x0ff00000) == 0x03400000) /* movt Rd, #const */
1521 high = EXTRACT_MOVW_MOVT_IMM_A (insn);
1522 *destreg = bits (insn, 12, 15);
1523 *offset = 8;
1524 address = (high << 16 | low);
1529 return address;
1532 /* Try to skip a sequence of instructions used for stack protector. If PC
1533 points to the first instruction of this sequence, return the address of
1534 first instruction after this sequence, otherwise, return original PC.
1536 On arm, this sequence of instructions is composed of mainly three steps,
1537 Step 1: load symbol __stack_chk_guard,
1538 Step 2: load from address of __stack_chk_guard,
1539 Step 3: store it to somewhere else.
1541 Usually, instructions on step 2 and step 3 are the same on various ARM
1542 architectures. On step 2, it is one instruction 'ldr Rx, [Rn, #0]', and
1543 on step 3, it is also one instruction 'str Rx, [r7, #immd]'. However,
1544 instructions in step 1 vary from different ARM architectures. On ARMv7,
1545 they are,
1547 movw Rn, #:lower16:__stack_chk_guard
1548 movt Rn, #:upper16:__stack_chk_guard
1550 On ARMv5t, it is,
1552 ldr Rn, .Label
1553 ....
1554 .Lable:
1555 .word __stack_chk_guard
1557 Since ldr/str is a very popular instruction, we can't use them as
1558 'fingerprint' or 'signature' of stack protector sequence. Here we choose
1559 sequence {movw/movt, ldr}/ldr/str plus symbol __stack_chk_guard, if not
1560 stripped, as the 'fingerprint' of a stack protector cdoe sequence. */
1562 static CORE_ADDR
1563 arm_skip_stack_protector(CORE_ADDR pc, struct gdbarch *gdbarch)
1565 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
1566 unsigned int basereg;
1567 struct bound_minimal_symbol stack_chk_guard;
1568 int offset;
1569 int is_thumb = arm_pc_is_thumb (gdbarch, pc);
1570 CORE_ADDR addr;
1572 /* Try to parse the instructions in Step 1. */
1573 addr = arm_analyze_load_stack_chk_guard (pc, gdbarch,
1574 &basereg, &offset);
1575 if (!addr)
1576 return pc;
1578 stack_chk_guard = lookup_minimal_symbol_by_pc (addr);
1579 /* ADDR must correspond to a symbol whose name is __stack_chk_guard.
1580 Otherwise, this sequence cannot be for stack protector. */
1581 if (stack_chk_guard.minsym == NULL
1582 || !startswith (stack_chk_guard.minsym->linkage_name (), "__stack_chk_guard"))
1583 return pc;
1585 if (is_thumb)
1587 unsigned int destreg;
1588 unsigned short insn
1589 = read_code_unsigned_integer (pc + offset, 2, byte_order_for_code);
1591 /* Step 2: ldr Rd, [Rn, #immed], encoding T1. */
1592 if ((insn & 0xf800) != 0x6800)
1593 return pc;
1594 if (bits (insn, 3, 5) != basereg)
1595 return pc;
1596 destreg = bits (insn, 0, 2);
1598 insn = read_code_unsigned_integer (pc + offset + 2, 2,
1599 byte_order_for_code);
1600 /* Step 3: str Rd, [Rn, #immed], encoding T1. */
1601 if ((insn & 0xf800) != 0x6000)
1602 return pc;
1603 if (destreg != bits (insn, 0, 2))
1604 return pc;
1606 else
1608 unsigned int destreg;
1609 unsigned int insn
1610 = read_code_unsigned_integer (pc + offset, 4, byte_order_for_code);
1612 /* Step 2: ldr Rd, [Rn, #immed], encoding A1. */
1613 if ((insn & 0x0e500000) != 0x04100000)
1614 return pc;
1615 if (bits (insn, 16, 19) != basereg)
1616 return pc;
1617 destreg = bits (insn, 12, 15);
1618 /* Step 3: str Rd, [Rn, #immed], encoding A1. */
1619 insn = read_code_unsigned_integer (pc + offset + 4,
1620 4, byte_order_for_code);
1621 if ((insn & 0x0e500000) != 0x04000000)
1622 return pc;
1623 if (bits (insn, 12, 15) != destreg)
1624 return pc;
1626 /* The size of total two instructions ldr/str is 4 on Thumb-2, while 8
1627 on arm. */
1628 if (is_thumb)
1629 return pc + offset + 4;
1630 else
1631 return pc + offset + 8;
1634 /* Advance the PC across any function entry prologue instructions to
1635 reach some "real" code.
1637 The APCS (ARM Procedure Call Standard) defines the following
1638 prologue:
1640 mov ip, sp
1641 [stmfd sp!, {a1,a2,a3,a4}]
1642 stmfd sp!, {...,fp,ip,lr,pc}
1643 [stfe f7, [sp, #-12]!]
1644 [stfe f6, [sp, #-12]!]
1645 [stfe f5, [sp, #-12]!]
1646 [stfe f4, [sp, #-12]!]
1647 sub fp, ip, #nn @@ nn == 20 or 4 depending on second insn. */
1649 static CORE_ADDR
1650 arm_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR pc)
1652 CORE_ADDR func_addr, limit_pc;
1654 /* See if we can determine the end of the prologue via the symbol table.
1655 If so, then return either PC, or the PC after the prologue, whichever
1656 is greater. */
1657 if (find_pc_partial_function (pc, NULL, &func_addr, NULL))
1659 CORE_ADDR post_prologue_pc
1660 = skip_prologue_using_sal (gdbarch, func_addr);
1661 struct compunit_symtab *cust = find_pc_compunit_symtab (func_addr);
1663 if (post_prologue_pc)
1664 post_prologue_pc
1665 = arm_skip_stack_protector (post_prologue_pc, gdbarch);
1668 /* GCC always emits a line note before the prologue and another
1669 one after, even if the two are at the same address or on the
1670 same line. Take advantage of this so that we do not need to
1671 know every instruction that might appear in the prologue. We
1672 will have producer information for most binaries; if it is
1673 missing (e.g. for -gstabs), assuming the GNU tools. */
1674 if (post_prologue_pc
1675 && (cust == NULL
1676 || cust->producer () == NULL
1677 || startswith (cust->producer (), "GNU ")
1678 || producer_is_llvm (cust->producer ())))
1679 return post_prologue_pc;
1681 if (post_prologue_pc != 0)
1683 CORE_ADDR analyzed_limit;
1685 /* For non-GCC compilers, make sure the entire line is an
1686 acceptable prologue; GDB will round this function's
1687 return value up to the end of the following line so we
1688 can not skip just part of a line (and we do not want to).
1690 RealView does not treat the prologue specially, but does
1691 associate prologue code with the opening brace; so this
1692 lets us skip the first line if we think it is the opening
1693 brace. */
1694 if (arm_pc_is_thumb (gdbarch, func_addr))
1695 analyzed_limit = thumb_analyze_prologue (gdbarch, func_addr,
1696 post_prologue_pc, NULL);
1697 else
1698 analyzed_limit
1699 = arm_analyze_prologue (gdbarch, func_addr, post_prologue_pc,
1700 NULL, target_arm_instruction_reader ());
1702 if (analyzed_limit != post_prologue_pc)
1703 return func_addr;
1705 return post_prologue_pc;
1709 /* Can't determine prologue from the symbol table, need to examine
1710 instructions. */
1712 /* Find an upper limit on the function prologue using the debug
1713 information. If the debug information could not be used to provide
1714 that bound, then use an arbitrary large number as the upper bound. */
1715 /* Like arm_scan_prologue, stop no later than pc + 64. */
1716 limit_pc = skip_prologue_using_sal (gdbarch, pc);
1717 if (limit_pc == 0)
1718 limit_pc = pc + 64; /* Magic. */
1721 /* Check if this is Thumb code. */
1722 if (arm_pc_is_thumb (gdbarch, pc))
1723 return thumb_analyze_prologue (gdbarch, pc, limit_pc, NULL);
1724 else
1725 return arm_analyze_prologue (gdbarch, pc, limit_pc, NULL,
1726 target_arm_instruction_reader ());
1729 /* *INDENT-OFF* */
1730 /* Function: thumb_scan_prologue (helper function for arm_scan_prologue)
1731 This function decodes a Thumb function prologue to determine:
1732 1) the size of the stack frame
1733 2) which registers are saved on it
1734 3) the offsets of saved regs
1735 4) the offset from the stack pointer to the frame pointer
1737 A typical Thumb function prologue would create this stack frame
1738 (offsets relative to FP)
1739 old SP -> 24 stack parameters
1740 20 LR
1741 16 R7
1742 R7 -> 0 local variables (16 bytes)
1743 SP -> -12 additional stack space (12 bytes)
1744 The frame size would thus be 36 bytes, and the frame offset would be
1745 12 bytes. The frame register is R7.
1747 The comments for thumb_skip_prolog() describe the algorithm we use
1748 to detect the end of the prolog. */
1749 /* *INDENT-ON* */
1751 static void
1752 thumb_scan_prologue (struct gdbarch *gdbarch, CORE_ADDR prev_pc,
1753 CORE_ADDR block_addr, struct arm_prologue_cache *cache)
1755 CORE_ADDR prologue_start;
1756 CORE_ADDR prologue_end;
1758 if (find_pc_partial_function (block_addr, NULL, &prologue_start,
1759 &prologue_end))
1761 /* See comment in arm_scan_prologue for an explanation of
1762 this heuristics. */
1763 if (prologue_end > prologue_start + 64)
1765 prologue_end = prologue_start + 64;
1768 else
1769 /* We're in the boondocks: we have no idea where the start of the
1770 function is. */
1771 return;
1773 prologue_end = std::min (prologue_end, prev_pc);
1775 thumb_analyze_prologue (gdbarch, prologue_start, prologue_end, cache);
1778 /* Return 1 if the ARM instruction INSN restores SP in epilogue, 0
1779 otherwise. */
1781 static int
1782 arm_instruction_restores_sp (unsigned int insn)
1784 if (bits (insn, 28, 31) != INST_NV)
1786 if ((insn & 0x0df0f000) == 0x0080d000
1787 /* ADD SP (register or immediate). */
1788 || (insn & 0x0df0f000) == 0x0040d000
1789 /* SUB SP (register or immediate). */
1790 || (insn & 0x0ffffff0) == 0x01a0d000
1791 /* MOV SP. */
1792 || (insn & 0x0fff0000) == 0x08bd0000
1793 /* POP (LDMIA). */
1794 || (insn & 0x0fff0000) == 0x049d0000)
1795 /* POP of a single register. */
1796 return 1;
1799 return 0;
1802 /* Implement immediate value decoding, as described in section A5.2.4
1803 (Modified immediate constants in ARM instructions) of the ARM Architecture
1804 Reference Manual (ARMv7-A and ARMv7-R edition). */
1806 static uint32_t
1807 arm_expand_immediate (uint32_t imm)
1809 /* Immediate values are 12 bits long. */
1810 gdb_assert ((imm & 0xfffff000) == 0);
1812 uint32_t unrotated_value = imm & 0xff;
1813 uint32_t rotate_amount = (imm & 0xf00) >> 7;
1815 if (rotate_amount == 0)
1816 return unrotated_value;
1818 return ((unrotated_value >> rotate_amount)
1819 | (unrotated_value << (32 - rotate_amount)));
1822 /* Analyze an ARM mode prologue starting at PROLOGUE_START and
1823 continuing no further than PROLOGUE_END. If CACHE is non-NULL,
1824 fill it in. Return the first address not recognized as a prologue
1825 instruction.
1827 We recognize all the instructions typically found in ARM prologues,
1828 plus harmless instructions which can be skipped (either for analysis
1829 purposes, or a more restrictive set that can be skipped when finding
1830 the end of the prologue). */
1832 static CORE_ADDR
1833 arm_analyze_prologue (struct gdbarch *gdbarch,
1834 CORE_ADDR prologue_start, CORE_ADDR prologue_end,
1835 struct arm_prologue_cache *cache,
1836 const arm_instruction_reader &insn_reader)
1838 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
1839 int regno;
1840 CORE_ADDR offset, current_pc;
1841 pv_t regs[ARM_FPS_REGNUM];
1842 CORE_ADDR unrecognized_pc = 0;
1843 arm_gdbarch_tdep *tdep = (arm_gdbarch_tdep *) gdbarch_tdep (gdbarch);
1845 /* Search the prologue looking for instructions that set up the
1846 frame pointer, adjust the stack pointer, and save registers.
1848 Be careful, however, and if it doesn't look like a prologue,
1849 don't try to scan it. If, for instance, a frameless function
1850 begins with stmfd sp!, then we will tell ourselves there is
1851 a frame, which will confuse stack traceback, as well as "finish"
1852 and other operations that rely on a knowledge of the stack
1853 traceback. */
1855 for (regno = 0; regno < ARM_FPS_REGNUM; regno++)
1856 regs[regno] = pv_register (regno, 0);
1857 pv_area stack (ARM_SP_REGNUM, gdbarch_addr_bit (gdbarch));
1859 for (current_pc = prologue_start;
1860 current_pc < prologue_end;
1861 current_pc += 4)
1863 uint32_t insn = insn_reader.read (current_pc, byte_order_for_code);
1865 if (insn == 0xe1a0c00d) /* mov ip, sp */
1867 regs[ARM_IP_REGNUM] = regs[ARM_SP_REGNUM];
1868 continue;
1870 else if ((insn & 0xfff00000) == 0xe2800000 /* add Rd, Rn, #n */
1871 && pv_is_register (regs[bits (insn, 16, 19)], ARM_SP_REGNUM))
1873 uint32_t imm = arm_expand_immediate (insn & 0xfff);
1874 int rd = bits (insn, 12, 15);
1875 regs[rd] = pv_add_constant (regs[bits (insn, 16, 19)], imm);
1876 continue;
1878 else if ((insn & 0xfff00000) == 0xe2400000 /* sub Rd, Rn, #n */
1879 && pv_is_register (regs[bits (insn, 16, 19)], ARM_SP_REGNUM))
1881 uint32_t imm = arm_expand_immediate (insn & 0xfff);
1882 int rd = bits (insn, 12, 15);
1883 regs[rd] = pv_add_constant (regs[bits (insn, 16, 19)], -imm);
1884 continue;
1886 else if ((insn & 0xffff0fff) == 0xe52d0004) /* str Rd,
1887 [sp, #-4]! */
1889 if (stack.store_would_trash (regs[ARM_SP_REGNUM]))
1890 break;
1891 regs[ARM_SP_REGNUM] = pv_add_constant (regs[ARM_SP_REGNUM], -4);
1892 stack.store (regs[ARM_SP_REGNUM], 4,
1893 regs[bits (insn, 12, 15)]);
1894 continue;
1896 else if ((insn & 0xffff0000) == 0xe92d0000)
1897 /* stmfd sp!, {..., fp, ip, lr, pc}
1899 stmfd sp!, {a1, a2, a3, a4} */
1901 int mask = insn & 0xffff;
1903 if (stack.store_would_trash (regs[ARM_SP_REGNUM]))
1904 break;
1906 /* Calculate offsets of saved registers. */
1907 for (regno = ARM_PC_REGNUM; regno >= 0; regno--)
1908 if (mask & (1 << regno))
1910 regs[ARM_SP_REGNUM]
1911 = pv_add_constant (regs[ARM_SP_REGNUM], -4);
1912 stack.store (regs[ARM_SP_REGNUM], 4, regs[regno]);
1915 else if ((insn & 0xffff0000) == 0xe54b0000 /* strb rx,[r11,#-n] */
1916 || (insn & 0xffff00f0) == 0xe14b00b0 /* strh rx,[r11,#-n] */
1917 || (insn & 0xffffc000) == 0xe50b0000) /* str rx,[r11,#-n] */
1919 /* No need to add this to saved_regs -- it's just an arg reg. */
1920 continue;
1922 else if ((insn & 0xffff0000) == 0xe5cd0000 /* strb rx,[sp,#n] */
1923 || (insn & 0xffff00f0) == 0xe1cd00b0 /* strh rx,[sp,#n] */
1924 || (insn & 0xffffc000) == 0xe58d0000) /* str rx,[sp,#n] */
1926 /* No need to add this to saved_regs -- it's just an arg reg. */
1927 continue;
1929 else if ((insn & 0xfff00000) == 0xe8800000 /* stm Rn,
1930 { registers } */
1931 && pv_is_register (regs[bits (insn, 16, 19)], ARM_SP_REGNUM))
1933 /* No need to add this to saved_regs -- it's just arg regs. */
1934 continue;
1936 else if ((insn & 0xfffff000) == 0xe24cb000) /* sub fp, ip #n */
1938 uint32_t imm = arm_expand_immediate (insn & 0xfff);
1939 regs[ARM_FP_REGNUM] = pv_add_constant (regs[ARM_IP_REGNUM], -imm);
1941 else if ((insn & 0xfffff000) == 0xe24dd000) /* sub sp, sp #n */
1943 uint32_t imm = arm_expand_immediate(insn & 0xfff);
1944 regs[ARM_SP_REGNUM] = pv_add_constant (regs[ARM_SP_REGNUM], -imm);
1946 else if ((insn & 0xffff7fff) == 0xed6d0103 /* stfe f?,
1947 [sp, -#c]! */
1948 && tdep->have_fpa_registers)
1950 if (stack.store_would_trash (regs[ARM_SP_REGNUM]))
1951 break;
1953 regs[ARM_SP_REGNUM] = pv_add_constant (regs[ARM_SP_REGNUM], -12);
1954 regno = ARM_F0_REGNUM + ((insn >> 12) & 0x07);
1955 stack.store (regs[ARM_SP_REGNUM], 12, regs[regno]);
1957 else if ((insn & 0xffbf0fff) == 0xec2d0200 /* sfmfd f0, 4,
1958 [sp!] */
1959 && tdep->have_fpa_registers)
1961 int n_saved_fp_regs;
1962 unsigned int fp_start_reg, fp_bound_reg;
1964 if (stack.store_would_trash (regs[ARM_SP_REGNUM]))
1965 break;
1967 if ((insn & 0x800) == 0x800) /* N0 is set */
1969 if ((insn & 0x40000) == 0x40000) /* N1 is set */
1970 n_saved_fp_regs = 3;
1971 else
1972 n_saved_fp_regs = 1;
1974 else
1976 if ((insn & 0x40000) == 0x40000) /* N1 is set */
1977 n_saved_fp_regs = 2;
1978 else
1979 n_saved_fp_regs = 4;
1982 fp_start_reg = ARM_F0_REGNUM + ((insn >> 12) & 0x7);
1983 fp_bound_reg = fp_start_reg + n_saved_fp_regs;
1984 for (; fp_start_reg < fp_bound_reg; fp_start_reg++)
1986 regs[ARM_SP_REGNUM] = pv_add_constant (regs[ARM_SP_REGNUM], -12);
1987 stack.store (regs[ARM_SP_REGNUM], 12,
1988 regs[fp_start_reg++]);
1991 else if ((insn & 0xff000000) == 0xeb000000 && cache == NULL) /* bl */
1993 /* Allow some special function calls when skipping the
1994 prologue; GCC generates these before storing arguments to
1995 the stack. */
1996 CORE_ADDR dest = BranchDest (current_pc, insn);
1998 if (skip_prologue_function (gdbarch, dest, 0))
1999 continue;
2000 else
2001 break;
2003 else if ((insn & 0xf0000000) != 0xe0000000)
2004 break; /* Condition not true, exit early. */
2005 else if (arm_instruction_changes_pc (insn))
2006 /* Don't scan past anything that might change control flow. */
2007 break;
2008 else if (arm_instruction_restores_sp (insn))
2010 /* Don't scan past the epilogue. */
2011 break;
2013 else if ((insn & 0xfe500000) == 0xe8100000 /* ldm */
2014 && pv_is_register (regs[bits (insn, 16, 19)], ARM_SP_REGNUM))
2015 /* Ignore block loads from the stack, potentially copying
2016 parameters from memory. */
2017 continue;
2018 else if ((insn & 0xfc500000) == 0xe4100000
2019 && pv_is_register (regs[bits (insn, 16, 19)], ARM_SP_REGNUM))
2020 /* Similarly ignore single loads from the stack. */
2021 continue;
2022 else if ((insn & 0xffff0ff0) == 0xe1a00000)
2023 /* MOV Rd, Rm. Skip register copies, i.e. saves to another
2024 register instead of the stack. */
2025 continue;
2026 else
2028 /* The optimizer might shove anything into the prologue, if
2029 we build up cache (cache != NULL) from scanning prologue,
2030 we just skip what we don't recognize and scan further to
2031 make cache as complete as possible. However, if we skip
2032 prologue, we'll stop immediately on unrecognized
2033 instruction. */
2034 unrecognized_pc = current_pc;
2035 if (cache != NULL)
2036 continue;
2037 else
2038 break;
2042 if (unrecognized_pc == 0)
2043 unrecognized_pc = current_pc;
2045 if (cache)
2047 int framereg, framesize;
2049 /* The frame size is just the distance from the frame register
2050 to the original stack pointer. */
2051 if (pv_is_register (regs[ARM_FP_REGNUM], ARM_SP_REGNUM))
2053 /* Frame pointer is fp. */
2054 framereg = ARM_FP_REGNUM;
2055 framesize = -regs[ARM_FP_REGNUM].k;
2057 else
2059 /* Try the stack pointer... this is a bit desperate. */
2060 framereg = ARM_SP_REGNUM;
2061 framesize = -regs[ARM_SP_REGNUM].k;
2064 cache->framereg = framereg;
2065 cache->framesize = framesize;
2067 for (regno = 0; regno < ARM_FPS_REGNUM; regno++)
2068 if (stack.find_reg (gdbarch, regno, &offset))
2069 cache->saved_regs[regno].set_addr (offset);
2072 arm_debug_printf ("Prologue scan stopped at %s",
2073 paddress (gdbarch, unrecognized_pc));
2075 return unrecognized_pc;
2078 static void
2079 arm_scan_prologue (struct frame_info *this_frame,
2080 struct arm_prologue_cache *cache)
2082 struct gdbarch *gdbarch = get_frame_arch (this_frame);
2083 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
2084 CORE_ADDR prologue_start, prologue_end;
2085 CORE_ADDR prev_pc = get_frame_pc (this_frame);
2086 CORE_ADDR block_addr = get_frame_address_in_block (this_frame);
2087 arm_gdbarch_tdep *tdep = (arm_gdbarch_tdep *) gdbarch_tdep (gdbarch);
2089 /* Assume there is no frame until proven otherwise. */
2090 cache->framereg = ARM_SP_REGNUM;
2091 cache->framesize = 0;
2093 /* Check for Thumb prologue. */
2094 if (arm_frame_is_thumb (this_frame))
2096 thumb_scan_prologue (gdbarch, prev_pc, block_addr, cache);
2097 return;
2100 /* Find the function prologue. If we can't find the function in
2101 the symbol table, peek in the stack frame to find the PC. */
2102 if (find_pc_partial_function (block_addr, NULL, &prologue_start,
2103 &prologue_end))
2105 /* One way to find the end of the prologue (which works well
2106 for unoptimized code) is to do the following:
2108 struct symtab_and_line sal = find_pc_line (prologue_start, 0);
2110 if (sal.line == 0)
2111 prologue_end = prev_pc;
2112 else if (sal.end < prologue_end)
2113 prologue_end = sal.end;
2115 This mechanism is very accurate so long as the optimizer
2116 doesn't move any instructions from the function body into the
2117 prologue. If this happens, sal.end will be the last
2118 instruction in the first hunk of prologue code just before
2119 the first instruction that the scheduler has moved from
2120 the body to the prologue.
2122 In order to make sure that we scan all of the prologue
2123 instructions, we use a slightly less accurate mechanism which
2124 may scan more than necessary. To help compensate for this
2125 lack of accuracy, the prologue scanning loop below contains
2126 several clauses which'll cause the loop to terminate early if
2127 an implausible prologue instruction is encountered.
2129 The expression
2131 prologue_start + 64
2133 is a suitable endpoint since it accounts for the largest
2134 possible prologue plus up to five instructions inserted by
2135 the scheduler. */
2137 if (prologue_end > prologue_start + 64)
2139 prologue_end = prologue_start + 64; /* See above. */
2142 else
2144 /* We have no symbol information. Our only option is to assume this
2145 function has a standard stack frame and the normal frame register.
2146 Then, we can find the value of our frame pointer on entrance to
2147 the callee (or at the present moment if this is the innermost frame).
2148 The value stored there should be the address of the stmfd + 8. */
2149 CORE_ADDR frame_loc;
2150 ULONGEST return_value;
2152 /* AAPCS does not use a frame register, so we can abort here. */
2153 if (tdep->arm_abi == ARM_ABI_AAPCS)
2154 return;
2156 frame_loc = get_frame_register_unsigned (this_frame, ARM_FP_REGNUM);
2157 if (!safe_read_memory_unsigned_integer (frame_loc, 4, byte_order,
2158 &return_value))
2159 return;
2160 else
2162 prologue_start = gdbarch_addr_bits_remove
2163 (gdbarch, return_value) - 8;
2164 prologue_end = prologue_start + 64; /* See above. */
2168 if (prev_pc < prologue_end)
2169 prologue_end = prev_pc;
2171 arm_analyze_prologue (gdbarch, prologue_start, prologue_end, cache,
2172 target_arm_instruction_reader ());
2175 static struct arm_prologue_cache *
2176 arm_make_prologue_cache (struct frame_info *this_frame)
2178 int reg;
2179 struct arm_prologue_cache *cache;
2180 CORE_ADDR unwound_fp;
2182 cache = FRAME_OBSTACK_ZALLOC (struct arm_prologue_cache);
2183 arm_cache_init (cache, this_frame);
2185 arm_scan_prologue (this_frame, cache);
2187 unwound_fp = get_frame_register_unsigned (this_frame, cache->framereg);
2188 if (unwound_fp == 0)
2189 return cache;
2191 arm_gdbarch_tdep *tdep =
2192 (arm_gdbarch_tdep *) gdbarch_tdep (get_frame_arch (this_frame));
2194 arm_cache_set_active_sp_value (cache, tdep, unwound_fp + cache->framesize);
2196 /* Calculate actual addresses of saved registers using offsets
2197 determined by arm_scan_prologue. */
2198 for (reg = 0; reg < gdbarch_num_regs (get_frame_arch (this_frame)); reg++)
2199 if (cache->saved_regs[reg].is_addr ())
2200 cache->saved_regs[reg].set_addr (cache->saved_regs[reg].addr ()
2201 + arm_cache_get_prev_sp_value (cache, tdep));
2203 return cache;
2206 /* Implementation of the stop_reason hook for arm_prologue frames. */
2208 static enum unwind_stop_reason
2209 arm_prologue_unwind_stop_reason (struct frame_info *this_frame,
2210 void **this_cache)
2212 struct arm_prologue_cache *cache;
2213 CORE_ADDR pc;
2215 if (*this_cache == NULL)
2216 *this_cache = arm_make_prologue_cache (this_frame);
2217 cache = (struct arm_prologue_cache *) *this_cache;
2219 /* This is meant to halt the backtrace at "_start". */
2220 pc = get_frame_pc (this_frame);
2221 gdbarch *arch = get_frame_arch (this_frame);
2222 arm_gdbarch_tdep *tdep = (arm_gdbarch_tdep *) gdbarch_tdep (arch);
2223 if (pc <= tdep->lowest_pc)
2224 return UNWIND_OUTERMOST;
2226 /* If we've hit a wall, stop. */
2227 if (arm_cache_get_prev_sp_value (cache, tdep) == 0)
2228 return UNWIND_OUTERMOST;
2230 return UNWIND_NO_REASON;
2233 /* Our frame ID for a normal frame is the current function's starting PC
2234 and the caller's SP when we were called. */
2236 static void
2237 arm_prologue_this_id (struct frame_info *this_frame,
2238 void **this_cache,
2239 struct frame_id *this_id)
2241 struct arm_prologue_cache *cache;
2242 struct frame_id id;
2243 CORE_ADDR pc, func;
2245 if (*this_cache == NULL)
2246 *this_cache = arm_make_prologue_cache (this_frame);
2247 cache = (struct arm_prologue_cache *) *this_cache;
2249 arm_gdbarch_tdep *tdep
2250 = (arm_gdbarch_tdep *) gdbarch_tdep (get_frame_arch (this_frame));
2252 /* Use function start address as part of the frame ID. If we cannot
2253 identify the start address (due to missing symbol information),
2254 fall back to just using the current PC. */
2255 pc = get_frame_pc (this_frame);
2256 func = get_frame_func (this_frame);
2257 if (!func)
2258 func = pc;
2260 id = frame_id_build (arm_cache_get_prev_sp_value (cache, tdep), func);
2261 *this_id = id;
2264 static struct value *
2265 arm_prologue_prev_register (struct frame_info *this_frame,
2266 void **this_cache,
2267 int prev_regnum)
2269 struct gdbarch *gdbarch = get_frame_arch (this_frame);
2270 struct arm_prologue_cache *cache;
2271 CORE_ADDR sp_value;
2273 if (*this_cache == NULL)
2274 *this_cache = arm_make_prologue_cache (this_frame);
2275 cache = (struct arm_prologue_cache *) *this_cache;
2277 arm_gdbarch_tdep *tdep = (arm_gdbarch_tdep *) gdbarch_tdep (gdbarch);
2279 /* If this frame has signed the return address, mark it as so. */
2280 if (tdep->have_pacbti && cache->ra_signed_state.has_value ()
2281 && *cache->ra_signed_state)
2282 set_frame_previous_pc_masked (this_frame);
2284 /* If we are asked to unwind the PC, then we need to return the LR
2285 instead. The prologue may save PC, but it will point into this
2286 frame's prologue, not the next frame's resume location. Also
2287 strip the saved T bit. A valid LR may have the low bit set, but
2288 a valid PC never does. */
2289 if (prev_regnum == ARM_PC_REGNUM)
2291 CORE_ADDR lr;
2293 lr = frame_unwind_register_unsigned (this_frame, ARM_LR_REGNUM);
2294 return frame_unwind_got_constant (this_frame, prev_regnum,
2295 arm_addr_bits_remove (gdbarch, lr));
2298 /* SP is generally not saved to the stack, but this frame is
2299 identified by the next frame's stack pointer at the time of the call.
2300 The value was already reconstructed into PREV_SP. */
2301 if (prev_regnum == ARM_SP_REGNUM)
2302 return frame_unwind_got_constant (this_frame, prev_regnum,
2303 arm_cache_get_prev_sp_value (cache, tdep));
2305 /* The value might be one of the alternative SP, if so, use the
2306 value already constructed. */
2307 if (arm_cache_is_sp_register (cache, tdep, prev_regnum))
2309 sp_value = arm_cache_get_sp_register (cache, tdep, prev_regnum);
2310 return frame_unwind_got_constant (this_frame, prev_regnum, sp_value);
2313 /* The CPSR may have been changed by the call instruction and by the
2314 called function. The only bit we can reconstruct is the T bit,
2315 by checking the low bit of LR as of the call. This is a reliable
2316 indicator of Thumb-ness except for some ARM v4T pre-interworking
2317 Thumb code, which could get away with a clear low bit as long as
2318 the called function did not use bx. Guess that all other
2319 bits are unchanged; the condition flags are presumably lost,
2320 but the processor status is likely valid. */
2321 if (prev_regnum == ARM_PS_REGNUM)
2323 CORE_ADDR lr, cpsr;
2324 ULONGEST t_bit = arm_psr_thumb_bit (gdbarch);
2326 cpsr = get_frame_register_unsigned (this_frame, prev_regnum);
2327 lr = frame_unwind_register_unsigned (this_frame, ARM_LR_REGNUM);
2328 if (IS_THUMB_ADDR (lr))
2329 cpsr |= t_bit;
2330 else
2331 cpsr &= ~t_bit;
2332 return frame_unwind_got_constant (this_frame, prev_regnum, cpsr);
2335 return trad_frame_get_prev_register (this_frame, cache->saved_regs,
2336 prev_regnum);
2339 static frame_unwind arm_prologue_unwind = {
2340 "arm prologue",
2341 NORMAL_FRAME,
2342 arm_prologue_unwind_stop_reason,
2343 arm_prologue_this_id,
2344 arm_prologue_prev_register,
2345 NULL,
2346 default_frame_sniffer
2349 /* Maintain a list of ARM exception table entries per objfile, similar to the
2350 list of mapping symbols. We only cache entries for standard ARM-defined
2351 personality routines; the cache will contain only the frame unwinding
2352 instructions associated with the entry (not the descriptors). */
2354 struct arm_exidx_entry
2356 CORE_ADDR addr;
2357 gdb_byte *entry;
2359 bool operator< (const arm_exidx_entry &other) const
2361 return addr < other.addr;
2365 struct arm_exidx_data
2367 std::vector<std::vector<arm_exidx_entry>> section_maps;
2370 /* Per-BFD key to store exception handling information. */
2371 static const struct bfd_key<arm_exidx_data> arm_exidx_data_key;
2373 static struct obj_section *
2374 arm_obj_section_from_vma (struct objfile *objfile, bfd_vma vma)
2376 struct obj_section *osect;
2378 ALL_OBJFILE_OSECTIONS (objfile, osect)
2379 if (bfd_section_flags (osect->the_bfd_section) & SEC_ALLOC)
2381 bfd_vma start, size;
2382 start = bfd_section_vma (osect->the_bfd_section);
2383 size = bfd_section_size (osect->the_bfd_section);
2385 if (start <= vma && vma < start + size)
2386 return osect;
2389 return NULL;
2392 /* Parse contents of exception table and exception index sections
2393 of OBJFILE, and fill in the exception table entry cache.
2395 For each entry that refers to a standard ARM-defined personality
2396 routine, extract the frame unwinding instructions (from either
2397 the index or the table section). The unwinding instructions
2398 are normalized by:
2399 - extracting them from the rest of the table data
2400 - converting to host endianness
2401 - appending the implicit 0xb0 ("Finish") code
2403 The extracted and normalized instructions are stored for later
2404 retrieval by the arm_find_exidx_entry routine. */
2406 static void
2407 arm_exidx_new_objfile (struct objfile *objfile)
2409 struct arm_exidx_data *data;
2410 asection *exidx, *extab;
2411 bfd_vma exidx_vma = 0, extab_vma = 0;
2412 LONGEST i;
2414 /* If we've already touched this file, do nothing. */
2415 if (!objfile || arm_exidx_data_key.get (objfile->obfd) != NULL)
2416 return;
2418 /* Read contents of exception table and index. */
2419 exidx = bfd_get_section_by_name (objfile->obfd, ELF_STRING_ARM_unwind);
2420 gdb::byte_vector exidx_data;
2421 if (exidx)
2423 exidx_vma = bfd_section_vma (exidx);
2424 exidx_data.resize (bfd_section_size (exidx));
2426 if (!bfd_get_section_contents (objfile->obfd, exidx,
2427 exidx_data.data (), 0,
2428 exidx_data.size ()))
2429 return;
2432 extab = bfd_get_section_by_name (objfile->obfd, ".ARM.extab");
2433 gdb::byte_vector extab_data;
2434 if (extab)
2436 extab_vma = bfd_section_vma (extab);
2437 extab_data.resize (bfd_section_size (extab));
2439 if (!bfd_get_section_contents (objfile->obfd, extab,
2440 extab_data.data (), 0,
2441 extab_data.size ()))
2442 return;
2445 /* Allocate exception table data structure. */
2446 data = arm_exidx_data_key.emplace (objfile->obfd);
2447 data->section_maps.resize (objfile->obfd->section_count);
2449 /* Fill in exception table. */
2450 for (i = 0; i < exidx_data.size () / 8; i++)
2452 struct arm_exidx_entry new_exidx_entry;
2453 bfd_vma idx = bfd_h_get_32 (objfile->obfd, exidx_data.data () + i * 8);
2454 bfd_vma val = bfd_h_get_32 (objfile->obfd,
2455 exidx_data.data () + i * 8 + 4);
2456 bfd_vma addr = 0, word = 0;
2457 int n_bytes = 0, n_words = 0;
2458 struct obj_section *sec;
2459 gdb_byte *entry = NULL;
2461 /* Extract address of start of function. */
2462 idx = ((idx & 0x7fffffff) ^ 0x40000000) - 0x40000000;
2463 idx += exidx_vma + i * 8;
2465 /* Find section containing function and compute section offset. */
2466 sec = arm_obj_section_from_vma (objfile, idx);
2467 if (sec == NULL)
2468 continue;
2469 idx -= bfd_section_vma (sec->the_bfd_section);
2471 /* Determine address of exception table entry. */
2472 if (val == 1)
2474 /* EXIDX_CANTUNWIND -- no exception table entry present. */
2476 else if ((val & 0xff000000) == 0x80000000)
2478 /* Exception table entry embedded in .ARM.exidx
2479 -- must be short form. */
2480 word = val;
2481 n_bytes = 3;
2483 else if (!(val & 0x80000000))
2485 /* Exception table entry in .ARM.extab. */
2486 addr = ((val & 0x7fffffff) ^ 0x40000000) - 0x40000000;
2487 addr += exidx_vma + i * 8 + 4;
2489 if (addr >= extab_vma && addr + 4 <= extab_vma + extab_data.size ())
2491 word = bfd_h_get_32 (objfile->obfd,
2492 extab_data.data () + addr - extab_vma);
2493 addr += 4;
2495 if ((word & 0xff000000) == 0x80000000)
2497 /* Short form. */
2498 n_bytes = 3;
2500 else if ((word & 0xff000000) == 0x81000000
2501 || (word & 0xff000000) == 0x82000000)
2503 /* Long form. */
2504 n_bytes = 2;
2505 n_words = ((word >> 16) & 0xff);
2507 else if (!(word & 0x80000000))
2509 bfd_vma pers;
2510 struct obj_section *pers_sec;
2511 int gnu_personality = 0;
2513 /* Custom personality routine. */
2514 pers = ((word & 0x7fffffff) ^ 0x40000000) - 0x40000000;
2515 pers = UNMAKE_THUMB_ADDR (pers + addr - 4);
2517 /* Check whether we've got one of the variants of the
2518 GNU personality routines. */
2519 pers_sec = arm_obj_section_from_vma (objfile, pers);
2520 if (pers_sec)
2522 static const char *personality[] =
2524 "__gcc_personality_v0",
2525 "__gxx_personality_v0",
2526 "__gcj_personality_v0",
2527 "__gnu_objc_personality_v0",
2528 NULL
2531 CORE_ADDR pc = pers + pers_sec->offset ();
2532 int k;
2534 for (k = 0; personality[k]; k++)
2535 if (lookup_minimal_symbol_by_pc_name
2536 (pc, personality[k], objfile))
2538 gnu_personality = 1;
2539 break;
2543 /* If so, the next word contains a word count in the high
2544 byte, followed by the same unwind instructions as the
2545 pre-defined forms. */
2546 if (gnu_personality
2547 && addr + 4 <= extab_vma + extab_data.size ())
2549 word = bfd_h_get_32 (objfile->obfd,
2550 (extab_data.data ()
2551 + addr - extab_vma));
2552 addr += 4;
2553 n_bytes = 3;
2554 n_words = ((word >> 24) & 0xff);
2560 /* Sanity check address. */
2561 if (n_words)
2562 if (addr < extab_vma
2563 || addr + 4 * n_words > extab_vma + extab_data.size ())
2564 n_words = n_bytes = 0;
2566 /* The unwind instructions reside in WORD (only the N_BYTES least
2567 significant bytes are valid), followed by N_WORDS words in the
2568 extab section starting at ADDR. */
2569 if (n_bytes || n_words)
2571 gdb_byte *p = entry
2572 = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack,
2573 n_bytes + n_words * 4 + 1);
2575 while (n_bytes--)
2576 *p++ = (gdb_byte) ((word >> (8 * n_bytes)) & 0xff);
2578 while (n_words--)
2580 word = bfd_h_get_32 (objfile->obfd,
2581 extab_data.data () + addr - extab_vma);
2582 addr += 4;
2584 *p++ = (gdb_byte) ((word >> 24) & 0xff);
2585 *p++ = (gdb_byte) ((word >> 16) & 0xff);
2586 *p++ = (gdb_byte) ((word >> 8) & 0xff);
2587 *p++ = (gdb_byte) (word & 0xff);
2590 /* Implied "Finish" to terminate the list. */
2591 *p++ = 0xb0;
2594 /* Push entry onto vector. They are guaranteed to always
2595 appear in order of increasing addresses. */
2596 new_exidx_entry.addr = idx;
2597 new_exidx_entry.entry = entry;
2598 data->section_maps[sec->the_bfd_section->index].push_back
2599 (new_exidx_entry);
2603 /* Search for the exception table entry covering MEMADDR. If one is found,
2604 return a pointer to its data. Otherwise, return 0. If START is non-NULL,
2605 set *START to the start of the region covered by this entry. */
2607 static gdb_byte *
2608 arm_find_exidx_entry (CORE_ADDR memaddr, CORE_ADDR *start)
2610 struct obj_section *sec;
2612 sec = find_pc_section (memaddr);
2613 if (sec != NULL)
2615 struct arm_exidx_data *data;
2616 struct arm_exidx_entry map_key = { memaddr - sec->addr (), 0 };
2618 data = arm_exidx_data_key.get (sec->objfile->obfd);
2619 if (data != NULL)
2621 std::vector<arm_exidx_entry> &map
2622 = data->section_maps[sec->the_bfd_section->index];
2623 if (!map.empty ())
2625 auto idx = std::lower_bound (map.begin (), map.end (), map_key);
2627 /* std::lower_bound finds the earliest ordered insertion
2628 point. If the following symbol starts at this exact
2629 address, we use that; otherwise, the preceding
2630 exception table entry covers this address. */
2631 if (idx < map.end ())
2633 if (idx->addr == map_key.addr)
2635 if (start)
2636 *start = idx->addr + sec->addr ();
2637 return idx->entry;
2641 if (idx > map.begin ())
2643 idx = idx - 1;
2644 if (start)
2645 *start = idx->addr + sec->addr ();
2646 return idx->entry;
2652 return NULL;
2655 /* Given the current frame THIS_FRAME, and its associated frame unwinding
2656 instruction list from the ARM exception table entry ENTRY, allocate and
2657 return a prologue cache structure describing how to unwind this frame.
2659 Return NULL if the unwinding instruction list contains a "spare",
2660 "reserved" or "refuse to unwind" instruction as defined in section
2661 "9.3 Frame unwinding instructions" of the "Exception Handling ABI
2662 for the ARM Architecture" document. */
2664 static struct arm_prologue_cache *
2665 arm_exidx_fill_cache (struct frame_info *this_frame, gdb_byte *entry)
2667 CORE_ADDR vsp = 0;
2668 int vsp_valid = 0;
2670 struct arm_prologue_cache *cache;
2671 cache = FRAME_OBSTACK_ZALLOC (struct arm_prologue_cache);
2672 arm_cache_init (cache, this_frame);
2674 for (;;)
2676 gdb_byte insn;
2678 /* Whenever we reload SP, we actually have to retrieve its
2679 actual value in the current frame. */
2680 if (!vsp_valid)
2682 if (cache->saved_regs[ARM_SP_REGNUM].is_realreg ())
2684 int reg = cache->saved_regs[ARM_SP_REGNUM].realreg ();
2685 vsp = get_frame_register_unsigned (this_frame, reg);
2687 else
2689 CORE_ADDR addr = cache->saved_regs[ARM_SP_REGNUM].addr ();
2690 vsp = get_frame_memory_unsigned (this_frame, addr, 4);
2693 vsp_valid = 1;
2696 /* Decode next unwind instruction. */
2697 insn = *entry++;
2699 if ((insn & 0xc0) == 0)
2701 int offset = insn & 0x3f;
2702 vsp += (offset << 2) + 4;
2704 else if ((insn & 0xc0) == 0x40)
2706 int offset = insn & 0x3f;
2707 vsp -= (offset << 2) + 4;
2709 else if ((insn & 0xf0) == 0x80)
2711 int mask = ((insn & 0xf) << 8) | *entry++;
2712 int i;
2714 /* The special case of an all-zero mask identifies
2715 "Refuse to unwind". We return NULL to fall back
2716 to the prologue analyzer. */
2717 if (mask == 0)
2718 return NULL;
2720 /* Pop registers r4..r15 under mask. */
2721 for (i = 0; i < 12; i++)
2722 if (mask & (1 << i))
2724 cache->saved_regs[4 + i].set_addr (vsp);
2725 vsp += 4;
2728 /* Special-case popping SP -- we need to reload vsp. */
2729 if (mask & (1 << (ARM_SP_REGNUM - 4)))
2730 vsp_valid = 0;
2732 else if ((insn & 0xf0) == 0x90)
2734 int reg = insn & 0xf;
2736 /* Reserved cases. */
2737 if (reg == ARM_SP_REGNUM || reg == ARM_PC_REGNUM)
2738 return NULL;
2740 /* Set SP from another register and mark VSP for reload. */
2741 cache->saved_regs[ARM_SP_REGNUM] = cache->saved_regs[reg];
2742 vsp_valid = 0;
2744 else if ((insn & 0xf0) == 0xa0)
2746 int count = insn & 0x7;
2747 int pop_lr = (insn & 0x8) != 0;
2748 int i;
2750 /* Pop r4..r[4+count]. */
2751 for (i = 0; i <= count; i++)
2753 cache->saved_regs[4 + i].set_addr (vsp);
2754 vsp += 4;
2757 /* If indicated by flag, pop LR as well. */
2758 if (pop_lr)
2760 cache->saved_regs[ARM_LR_REGNUM].set_addr (vsp);
2761 vsp += 4;
2764 else if (insn == 0xb0)
2766 /* We could only have updated PC by popping into it; if so, it
2767 will show up as address. Otherwise, copy LR into PC. */
2768 if (!cache->saved_regs[ARM_PC_REGNUM].is_addr ())
2769 cache->saved_regs[ARM_PC_REGNUM]
2770 = cache->saved_regs[ARM_LR_REGNUM];
2772 /* We're done. */
2773 break;
2775 else if (insn == 0xb1)
2777 int mask = *entry++;
2778 int i;
2780 /* All-zero mask and mask >= 16 is "spare". */
2781 if (mask == 0 || mask >= 16)
2782 return NULL;
2784 /* Pop r0..r3 under mask. */
2785 for (i = 0; i < 4; i++)
2786 if (mask & (1 << i))
2788 cache->saved_regs[i].set_addr (vsp);
2789 vsp += 4;
2792 else if (insn == 0xb2)
2794 ULONGEST offset = 0;
2795 unsigned shift = 0;
2799 offset |= (*entry & 0x7f) << shift;
2800 shift += 7;
2802 while (*entry++ & 0x80);
2804 vsp += 0x204 + (offset << 2);
2806 else if (insn == 0xb3)
2808 int start = *entry >> 4;
2809 int count = (*entry++) & 0xf;
2810 int i;
2812 /* Only registers D0..D15 are valid here. */
2813 if (start + count >= 16)
2814 return NULL;
2816 /* Pop VFP double-precision registers D[start]..D[start+count]. */
2817 for (i = 0; i <= count; i++)
2819 cache->saved_regs[ARM_D0_REGNUM + start + i].set_addr (vsp);
2820 vsp += 8;
2823 /* Add an extra 4 bytes for FSTMFDX-style stack. */
2824 vsp += 4;
2826 else if ((insn & 0xf8) == 0xb8)
2828 int count = insn & 0x7;
2829 int i;
2831 /* Pop VFP double-precision registers D[8]..D[8+count]. */
2832 for (i = 0; i <= count; i++)
2834 cache->saved_regs[ARM_D0_REGNUM + 8 + i].set_addr (vsp);
2835 vsp += 8;
2838 /* Add an extra 4 bytes for FSTMFDX-style stack. */
2839 vsp += 4;
2841 else if (insn == 0xc6)
2843 int start = *entry >> 4;
2844 int count = (*entry++) & 0xf;
2845 int i;
2847 /* Only registers WR0..WR15 are valid. */
2848 if (start + count >= 16)
2849 return NULL;
2851 /* Pop iwmmx registers WR[start]..WR[start+count]. */
2852 for (i = 0; i <= count; i++)
2854 cache->saved_regs[ARM_WR0_REGNUM + start + i].set_addr (vsp);
2855 vsp += 8;
2858 else if (insn == 0xc7)
2860 int mask = *entry++;
2861 int i;
2863 /* All-zero mask and mask >= 16 is "spare". */
2864 if (mask == 0 || mask >= 16)
2865 return NULL;
2867 /* Pop iwmmx general-purpose registers WCGR0..WCGR3 under mask. */
2868 for (i = 0; i < 4; i++)
2869 if (mask & (1 << i))
2871 cache->saved_regs[ARM_WCGR0_REGNUM + i].set_addr (vsp);
2872 vsp += 4;
2875 else if ((insn & 0xf8) == 0xc0)
2877 int count = insn & 0x7;
2878 int i;
2880 /* Pop iwmmx registers WR[10]..WR[10+count]. */
2881 for (i = 0; i <= count; i++)
2883 cache->saved_regs[ARM_WR0_REGNUM + 10 + i].set_addr (vsp);
2884 vsp += 8;
2887 else if (insn == 0xc8)
2889 int start = *entry >> 4;
2890 int count = (*entry++) & 0xf;
2891 int i;
2893 /* Only registers D0..D31 are valid. */
2894 if (start + count >= 16)
2895 return NULL;
2897 /* Pop VFP double-precision registers
2898 D[16+start]..D[16+start+count]. */
2899 for (i = 0; i <= count; i++)
2901 cache->saved_regs[ARM_D0_REGNUM + 16 + start + i].set_addr (vsp);
2902 vsp += 8;
2905 else if (insn == 0xc9)
2907 int start = *entry >> 4;
2908 int count = (*entry++) & 0xf;
2909 int i;
2911 /* Pop VFP double-precision registers D[start]..D[start+count]. */
2912 for (i = 0; i <= count; i++)
2914 cache->saved_regs[ARM_D0_REGNUM + start + i].set_addr (vsp);
2915 vsp += 8;
2918 else if ((insn & 0xf8) == 0xd0)
2920 int count = insn & 0x7;
2921 int i;
2923 /* Pop VFP double-precision registers D[8]..D[8+count]. */
2924 for (i = 0; i <= count; i++)
2926 cache->saved_regs[ARM_D0_REGNUM + 8 + i].set_addr (vsp);
2927 vsp += 8;
2930 else
2932 /* Everything else is "spare". */
2933 return NULL;
2937 /* If we restore SP from a register, assume this was the frame register.
2938 Otherwise just fall back to SP as frame register. */
2939 if (cache->saved_regs[ARM_SP_REGNUM].is_realreg ())
2940 cache->framereg = cache->saved_regs[ARM_SP_REGNUM].realreg ();
2941 else
2942 cache->framereg = ARM_SP_REGNUM;
2944 /* Determine offset to previous frame. */
2945 cache->framesize
2946 = vsp - get_frame_register_unsigned (this_frame, cache->framereg);
2948 /* We already got the previous SP. */
2949 arm_gdbarch_tdep *tdep
2950 = (arm_gdbarch_tdep *) gdbarch_tdep (get_frame_arch (this_frame));
2951 arm_cache_set_active_sp_value (cache, tdep, vsp);
2953 return cache;
2956 /* Unwinding via ARM exception table entries. Note that the sniffer
2957 already computes a filled-in prologue cache, which is then used
2958 with the same arm_prologue_this_id and arm_prologue_prev_register
2959 routines also used for prologue-parsing based unwinding. */
2961 static int
2962 arm_exidx_unwind_sniffer (const struct frame_unwind *self,
2963 struct frame_info *this_frame,
2964 void **this_prologue_cache)
2966 struct gdbarch *gdbarch = get_frame_arch (this_frame);
2967 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
2968 CORE_ADDR addr_in_block, exidx_region, func_start;
2969 struct arm_prologue_cache *cache;
2970 gdb_byte *entry;
2972 /* See if we have an ARM exception table entry covering this address. */
2973 addr_in_block = get_frame_address_in_block (this_frame);
2974 entry = arm_find_exidx_entry (addr_in_block, &exidx_region);
2975 if (!entry)
2976 return 0;
2978 /* The ARM exception table does not describe unwind information
2979 for arbitrary PC values, but is guaranteed to be correct only
2980 at call sites. We have to decide here whether we want to use
2981 ARM exception table information for this frame, or fall back
2982 to using prologue parsing. (Note that if we have DWARF CFI,
2983 this sniffer isn't even called -- CFI is always preferred.)
2985 Before we make this decision, however, we check whether we
2986 actually have *symbol* information for the current frame.
2987 If not, prologue parsing would not work anyway, so we might
2988 as well use the exception table and hope for the best. */
2989 if (find_pc_partial_function (addr_in_block, NULL, &func_start, NULL))
2991 int exc_valid = 0;
2993 /* If the next frame is "normal", we are at a call site in this
2994 frame, so exception information is guaranteed to be valid. */
2995 if (get_next_frame (this_frame)
2996 && get_frame_type (get_next_frame (this_frame)) == NORMAL_FRAME)
2997 exc_valid = 1;
2999 /* We also assume exception information is valid if we're currently
3000 blocked in a system call. The system library is supposed to
3001 ensure this, so that e.g. pthread cancellation works. */
3002 if (arm_frame_is_thumb (this_frame))
3004 ULONGEST insn;
3006 if (safe_read_memory_unsigned_integer (get_frame_pc (this_frame) - 2,
3007 2, byte_order_for_code, &insn)
3008 && (insn & 0xff00) == 0xdf00 /* svc */)
3009 exc_valid = 1;
3011 else
3013 ULONGEST insn;
3015 if (safe_read_memory_unsigned_integer (get_frame_pc (this_frame) - 4,
3016 4, byte_order_for_code, &insn)
3017 && (insn & 0x0f000000) == 0x0f000000 /* svc */)
3018 exc_valid = 1;
3021 /* Bail out if we don't know that exception information is valid. */
3022 if (!exc_valid)
3023 return 0;
3025 /* The ARM exception index does not mark the *end* of the region
3026 covered by the entry, and some functions will not have any entry.
3027 To correctly recognize the end of the covered region, the linker
3028 should have inserted dummy records with a CANTUNWIND marker.
3030 Unfortunately, current versions of GNU ld do not reliably do
3031 this, and thus we may have found an incorrect entry above.
3032 As a (temporary) sanity check, we only use the entry if it
3033 lies *within* the bounds of the function. Note that this check
3034 might reject perfectly valid entries that just happen to cover
3035 multiple functions; therefore this check ought to be removed
3036 once the linker is fixed. */
3037 if (func_start > exidx_region)
3038 return 0;
3041 /* Decode the list of unwinding instructions into a prologue cache.
3042 Note that this may fail due to e.g. a "refuse to unwind" code. */
3043 cache = arm_exidx_fill_cache (this_frame, entry);
3044 if (!cache)
3045 return 0;
3047 *this_prologue_cache = cache;
3048 return 1;
3051 struct frame_unwind arm_exidx_unwind = {
3052 "arm exidx",
3053 NORMAL_FRAME,
3054 default_frame_unwind_stop_reason,
3055 arm_prologue_this_id,
3056 arm_prologue_prev_register,
3057 NULL,
3058 arm_exidx_unwind_sniffer
3061 static struct arm_prologue_cache *
3062 arm_make_epilogue_frame_cache (struct frame_info *this_frame)
3064 struct arm_prologue_cache *cache;
3065 int reg;
3067 cache = FRAME_OBSTACK_ZALLOC (struct arm_prologue_cache);
3068 arm_cache_init (cache, this_frame);
3070 /* Still rely on the offset calculated from prologue. */
3071 arm_scan_prologue (this_frame, cache);
3073 /* Since we are in epilogue, the SP has been restored. */
3074 arm_gdbarch_tdep *tdep
3075 = (arm_gdbarch_tdep *) gdbarch_tdep (get_frame_arch (this_frame));
3076 arm_cache_set_active_sp_value (cache, tdep,
3077 get_frame_register_unsigned (this_frame,
3078 ARM_SP_REGNUM));
3080 /* Calculate actual addresses of saved registers using offsets
3081 determined by arm_scan_prologue. */
3082 for (reg = 0; reg < gdbarch_num_regs (get_frame_arch (this_frame)); reg++)
3083 if (cache->saved_regs[reg].is_addr ())
3084 cache->saved_regs[reg].set_addr (cache->saved_regs[reg].addr ()
3085 + arm_cache_get_prev_sp_value (cache, tdep));
3087 return cache;
3090 /* Implementation of function hook 'this_id' in
3091 'struct frame_uwnind' for epilogue unwinder. */
3093 static void
3094 arm_epilogue_frame_this_id (struct frame_info *this_frame,
3095 void **this_cache,
3096 struct frame_id *this_id)
3098 struct arm_prologue_cache *cache;
3099 CORE_ADDR pc, func;
3101 if (*this_cache == NULL)
3102 *this_cache = arm_make_epilogue_frame_cache (this_frame);
3103 cache = (struct arm_prologue_cache *) *this_cache;
3105 /* Use function start address as part of the frame ID. If we cannot
3106 identify the start address (due to missing symbol information),
3107 fall back to just using the current PC. */
3108 pc = get_frame_pc (this_frame);
3109 func = get_frame_func (this_frame);
3110 if (func == 0)
3111 func = pc;
3113 arm_gdbarch_tdep *tdep
3114 = (arm_gdbarch_tdep *) gdbarch_tdep (get_frame_arch (this_frame));
3115 *this_id = frame_id_build (arm_cache_get_prev_sp_value (cache, tdep), pc);
3118 /* Implementation of function hook 'prev_register' in
3119 'struct frame_uwnind' for epilogue unwinder. */
3121 static struct value *
3122 arm_epilogue_frame_prev_register (struct frame_info *this_frame,
3123 void **this_cache, int regnum)
3125 if (*this_cache == NULL)
3126 *this_cache = arm_make_epilogue_frame_cache (this_frame);
3128 return arm_prologue_prev_register (this_frame, this_cache, regnum);
3131 static int arm_stack_frame_destroyed_p_1 (struct gdbarch *gdbarch,
3132 CORE_ADDR pc);
3133 static int thumb_stack_frame_destroyed_p (struct gdbarch *gdbarch,
3134 CORE_ADDR pc);
3136 /* Implementation of function hook 'sniffer' in
3137 'struct frame_uwnind' for epilogue unwinder. */
3139 static int
3140 arm_epilogue_frame_sniffer (const struct frame_unwind *self,
3141 struct frame_info *this_frame,
3142 void **this_prologue_cache)
3144 if (frame_relative_level (this_frame) == 0)
3146 struct gdbarch *gdbarch = get_frame_arch (this_frame);
3147 CORE_ADDR pc = get_frame_pc (this_frame);
3149 if (arm_frame_is_thumb (this_frame))
3150 return thumb_stack_frame_destroyed_p (gdbarch, pc);
3151 else
3152 return arm_stack_frame_destroyed_p_1 (gdbarch, pc);
3154 else
3155 return 0;
3158 /* Frame unwinder from epilogue. */
3160 static const struct frame_unwind arm_epilogue_frame_unwind =
3162 "arm epilogue",
3163 NORMAL_FRAME,
3164 default_frame_unwind_stop_reason,
3165 arm_epilogue_frame_this_id,
3166 arm_epilogue_frame_prev_register,
3167 NULL,
3168 arm_epilogue_frame_sniffer,
3171 /* Recognize GCC's trampoline for thumb call-indirect. If we are in a
3172 trampoline, return the target PC. Otherwise return 0.
3174 void call0a (char c, short s, int i, long l) {}
3176 int main (void)
3178 (*pointer_to_call0a) (c, s, i, l);
3181 Instead of calling a stub library function _call_via_xx (xx is
3182 the register name), GCC may inline the trampoline in the object
3183 file as below (register r2 has the address of call0a).
3185 .global main
3186 .type main, %function
3188 bl .L1
3190 .size main, .-main
3192 .L1:
3193 bx r2
3195 The trampoline 'bx r2' doesn't belong to main. */
3197 static CORE_ADDR
3198 arm_skip_bx_reg (struct frame_info *frame, CORE_ADDR pc)
3200 /* The heuristics of recognizing such trampoline is that FRAME is
3201 executing in Thumb mode and the instruction on PC is 'bx Rm'. */
3202 if (arm_frame_is_thumb (frame))
3204 gdb_byte buf[2];
3206 if (target_read_memory (pc, buf, 2) == 0)
3208 struct gdbarch *gdbarch = get_frame_arch (frame);
3209 enum bfd_endian byte_order_for_code
3210 = gdbarch_byte_order_for_code (gdbarch);
3211 uint16_t insn
3212 = extract_unsigned_integer (buf, 2, byte_order_for_code);
3214 if ((insn & 0xff80) == 0x4700) /* bx <Rm> */
3216 CORE_ADDR dest
3217 = get_frame_register_unsigned (frame, bits (insn, 3, 6));
3219 /* Clear the LSB so that gdb core sets step-resume
3220 breakpoint at the right address. */
3221 return UNMAKE_THUMB_ADDR (dest);
3226 return 0;
3229 static struct arm_prologue_cache *
3230 arm_make_stub_cache (struct frame_info *this_frame)
3232 struct arm_prologue_cache *cache;
3234 cache = FRAME_OBSTACK_ZALLOC (struct arm_prologue_cache);
3235 arm_cache_init (cache, this_frame);
3237 arm_gdbarch_tdep *tdep
3238 = (arm_gdbarch_tdep *) gdbarch_tdep (get_frame_arch (this_frame));
3239 arm_cache_set_active_sp_value (cache, tdep,
3240 get_frame_register_unsigned (this_frame,
3241 ARM_SP_REGNUM));
3243 return cache;
3246 /* Our frame ID for a stub frame is the current SP and LR. */
3248 static void
3249 arm_stub_this_id (struct frame_info *this_frame,
3250 void **this_cache,
3251 struct frame_id *this_id)
3253 struct arm_prologue_cache *cache;
3255 if (*this_cache == NULL)
3256 *this_cache = arm_make_stub_cache (this_frame);
3257 cache = (struct arm_prologue_cache *) *this_cache;
3259 arm_gdbarch_tdep *tdep
3260 = (arm_gdbarch_tdep *) gdbarch_tdep (get_frame_arch (this_frame));
3261 *this_id = frame_id_build (arm_cache_get_prev_sp_value (cache, tdep),
3262 get_frame_pc (this_frame));
3265 static int
3266 arm_stub_unwind_sniffer (const struct frame_unwind *self,
3267 struct frame_info *this_frame,
3268 void **this_prologue_cache)
3270 CORE_ADDR addr_in_block;
3271 gdb_byte dummy[4];
3272 CORE_ADDR pc, start_addr;
3273 const char *name;
3275 addr_in_block = get_frame_address_in_block (this_frame);
3276 pc = get_frame_pc (this_frame);
3277 if (in_plt_section (addr_in_block)
3278 /* We also use the stub winder if the target memory is unreadable
3279 to avoid having the prologue unwinder trying to read it. */
3280 || target_read_memory (pc, dummy, 4) != 0)
3281 return 1;
3283 if (find_pc_partial_function (pc, &name, &start_addr, NULL) == 0
3284 && arm_skip_bx_reg (this_frame, pc) != 0)
3285 return 1;
3287 return 0;
3290 struct frame_unwind arm_stub_unwind = {
3291 "arm stub",
3292 NORMAL_FRAME,
3293 default_frame_unwind_stop_reason,
3294 arm_stub_this_id,
3295 arm_prologue_prev_register,
3296 NULL,
3297 arm_stub_unwind_sniffer
3300 /* Put here the code to store, into CACHE->saved_regs, the addresses
3301 of the saved registers of frame described by THIS_FRAME. CACHE is
3302 returned. */
3304 static struct arm_prologue_cache *
3305 arm_m_exception_cache (struct frame_info *this_frame)
3307 struct gdbarch *gdbarch = get_frame_arch (this_frame);
3308 arm_gdbarch_tdep *tdep = (arm_gdbarch_tdep *) gdbarch_tdep (gdbarch);
3309 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
3310 struct arm_prologue_cache *cache;
3311 CORE_ADDR lr;
3312 CORE_ADDR sp;
3313 CORE_ADDR unwound_sp;
3314 uint32_t sp_r0_offset = 0;
3315 LONGEST xpsr;
3316 uint32_t exc_return;
3317 bool fnc_return;
3318 uint32_t extended_frame_used;
3319 bool secure_stack_used = false;
3320 bool default_callee_register_stacking = false;
3321 bool exception_domain_is_secure = false;
3323 cache = FRAME_OBSTACK_ZALLOC (struct arm_prologue_cache);
3324 arm_cache_init (cache, this_frame);
3326 /* ARMv7-M Architecture Reference "B1.5.6 Exception entry behavior"
3327 describes which bits in LR that define which stack was used prior
3328 to the exception and if FPU is used (causing extended stack frame). */
3330 lr = get_frame_register_unsigned (this_frame, ARM_LR_REGNUM);
3331 sp = get_frame_register_unsigned (this_frame, ARM_SP_REGNUM);
3333 fnc_return = ((lr & 0xfffffffe) == 0xfefffffe);
3334 if (tdep->have_sec_ext && fnc_return)
3336 int actual_sp;
3338 arm_cache_switch_prev_sp (cache, tdep, tdep->m_profile_msp_ns_regnum);
3339 arm_cache_set_active_sp_value (cache, tdep, sp);
3340 if (lr & 1)
3341 actual_sp = tdep->m_profile_msp_s_regnum;
3342 else
3343 actual_sp = tdep->m_profile_msp_ns_regnum;
3345 arm_cache_switch_prev_sp (cache, tdep, actual_sp);
3346 sp = get_frame_register_unsigned (this_frame, actual_sp);
3348 cache->saved_regs[ARM_LR_REGNUM].set_addr (sp);
3350 arm_cache_set_active_sp_value (cache, tdep, sp + 8);
3352 return cache;
3355 /* Check EXC_RETURN indicator bits (24-31). */
3356 exc_return = (((lr >> 24) & 0xff) == 0xff);
3357 if (exc_return)
3359 /* Check EXC_RETURN bit SPSEL if Main or Thread (process) stack used. */
3360 bool process_stack_used = ((lr & (1 << 2)) != 0);
3362 if (tdep->have_sec_ext)
3364 secure_stack_used = ((lr & (1 << 6)) != 0);
3365 default_callee_register_stacking = ((lr & (1 << 5)) != 0);
3366 exception_domain_is_secure = ((lr & (1 << 0)) == 0);
3368 /* Unwinding from non-secure to secure can trip security
3369 measures. In order to avoid the debugger being
3370 intrusive, rely on the user to configure the requested
3371 mode. */
3372 if (secure_stack_used && !exception_domain_is_secure
3373 && !arm_unwind_secure_frames)
3375 warning (_("Non-secure to secure stack unwinding disabled."));
3377 /* Terminate any further stack unwinding by referring to self. */
3378 arm_cache_set_active_sp_value (cache, tdep, sp);
3379 return cache;
3382 if (process_stack_used)
3384 if (secure_stack_used)
3385 /* Secure thread (process) stack used, use PSP_S as SP. */
3386 arm_cache_switch_prev_sp (cache, tdep, tdep->m_profile_psp_s_regnum);
3387 else
3388 /* Non-secure thread (process) stack used, use PSP_NS as SP. */
3389 arm_cache_switch_prev_sp (cache, tdep, tdep->m_profile_psp_ns_regnum);
3391 else
3393 if (secure_stack_used)
3394 /* Secure main stack used, use MSP_S as SP. */
3395 arm_cache_switch_prev_sp (cache, tdep, tdep->m_profile_msp_s_regnum);
3396 else
3397 /* Non-secure main stack used, use MSP_NS as SP. */
3398 arm_cache_switch_prev_sp (cache, tdep, tdep->m_profile_msp_ns_regnum);
3401 else
3403 if (process_stack_used)
3404 /* Thread (process) stack used, use PSP as SP. */
3405 arm_cache_switch_prev_sp (cache, tdep, tdep->m_profile_psp_regnum);
3406 else
3407 /* Main stack used, use MSP as SP. */
3408 arm_cache_switch_prev_sp (cache, tdep, tdep->m_profile_msp_regnum);
3411 else
3413 /* Main stack used, use MSP as SP. */
3414 arm_cache_switch_prev_sp (cache, tdep, tdep->m_profile_msp_regnum);
3417 /* Fetch the SP to use for this frame. */
3418 unwound_sp = arm_cache_get_prev_sp_value (cache, tdep);
3420 /* With the Security extension, the hardware saves R4..R11 too. */
3421 if (exc_return && tdep->have_sec_ext && secure_stack_used
3422 && (!default_callee_register_stacking || exception_domain_is_secure))
3424 /* Read R4..R11 from the integer callee registers. */
3425 cache->saved_regs[4].set_addr (unwound_sp + 0x08);
3426 cache->saved_regs[5].set_addr (unwound_sp + 0x0C);
3427 cache->saved_regs[6].set_addr (unwound_sp + 0x10);
3428 cache->saved_regs[7].set_addr (unwound_sp + 0x14);
3429 cache->saved_regs[8].set_addr (unwound_sp + 0x18);
3430 cache->saved_regs[9].set_addr (unwound_sp + 0x1C);
3431 cache->saved_regs[10].set_addr (unwound_sp + 0x20);
3432 cache->saved_regs[11].set_addr (unwound_sp + 0x24);
3433 sp_r0_offset = 0x28;
3436 /* The hardware saves eight 32-bit words, comprising xPSR,
3437 ReturnAddress, LR (R14), R12, R3, R2, R1, R0. See details in
3438 "B1.5.6 Exception entry behavior" in
3439 "ARMv7-M Architecture Reference Manual". */
3440 cache->saved_regs[0].set_addr (unwound_sp + sp_r0_offset);
3441 cache->saved_regs[1].set_addr (unwound_sp + sp_r0_offset + 4);
3442 cache->saved_regs[2].set_addr (unwound_sp + sp_r0_offset + 8);
3443 cache->saved_regs[3].set_addr (unwound_sp + sp_r0_offset + 12);
3444 cache->saved_regs[ARM_IP_REGNUM].set_addr (unwound_sp + sp_r0_offset + 16);
3445 cache->saved_regs[ARM_LR_REGNUM].set_addr (unwound_sp + sp_r0_offset + 20);
3446 cache->saved_regs[ARM_PC_REGNUM].set_addr (unwound_sp + sp_r0_offset + 24);
3447 cache->saved_regs[ARM_PS_REGNUM].set_addr (unwound_sp + sp_r0_offset + 28);
3449 /* Check EXC_RETURN bit FTYPE if extended stack frame (FPU regs stored)
3450 type used. */
3451 extended_frame_used = ((lr & (1 << 4)) == 0);
3452 if (exc_return && extended_frame_used)
3454 int i;
3455 int fpu_regs_stack_offset;
3457 /* This code does not take into account the lazy stacking, see "Lazy
3458 context save of FP state", in B1.5.7, also ARM AN298, supported
3459 by Cortex-M4F architecture.
3460 To fully handle this the FPCCR register (Floating-point Context
3461 Control Register) needs to be read out and the bits ASPEN and LSPEN
3462 could be checked to setup correct lazy stacked FP registers.
3463 This register is located at address 0xE000EF34. */
3465 /* Extended stack frame type used. */
3466 fpu_regs_stack_offset = unwound_sp + sp_r0_offset + 0x20;
3467 for (i = 0; i < 16; i++)
3469 cache->saved_regs[ARM_D0_REGNUM + i].set_addr (fpu_regs_stack_offset);
3470 fpu_regs_stack_offset += 4;
3472 cache->saved_regs[ARM_FPSCR_REGNUM].set_addr (unwound_sp + sp_r0_offset + 0x60);
3473 fpu_regs_stack_offset += 4;
3475 if (tdep->have_sec_ext && !default_callee_register_stacking)
3477 /* Handle floating-point callee saved registers. */
3478 fpu_regs_stack_offset = 0x90;
3479 for (i = 16; i < 32; i++)
3481 cache->saved_regs[ARM_D0_REGNUM + i].set_addr (fpu_regs_stack_offset);
3482 fpu_regs_stack_offset += 4;
3485 arm_cache_set_active_sp_value (cache, tdep, unwound_sp + sp_r0_offset + 0xD0);
3487 else
3489 /* Offset 0x64 is reserved. */
3490 arm_cache_set_active_sp_value (cache, tdep, unwound_sp + sp_r0_offset + 0x68);
3493 else
3495 /* Standard stack frame type used. */
3496 arm_cache_set_active_sp_value (cache, tdep, unwound_sp + sp_r0_offset + 0x20);
3499 /* If bit 9 of the saved xPSR is set, then there is a four-byte
3500 aligner between the top of the 32-byte stack frame and the
3501 previous context's stack pointer. */
3502 if (safe_read_memory_integer (unwound_sp + sp_r0_offset + 28, 4, byte_order, &xpsr)
3503 && (xpsr & (1 << 9)) != 0)
3504 arm_cache_set_active_sp_value (cache, tdep,
3505 arm_cache_get_prev_sp_value (cache, tdep) + 4);
3507 return cache;
3510 /* Implementation of function hook 'this_id' in
3511 'struct frame_uwnind'. */
3513 static void
3514 arm_m_exception_this_id (struct frame_info *this_frame,
3515 void **this_cache,
3516 struct frame_id *this_id)
3518 struct arm_prologue_cache *cache;
3520 if (*this_cache == NULL)
3521 *this_cache = arm_m_exception_cache (this_frame);
3522 cache = (struct arm_prologue_cache *) *this_cache;
3524 /* Our frame ID for a stub frame is the current SP and LR. */
3525 arm_gdbarch_tdep *tdep
3526 = (arm_gdbarch_tdep *) gdbarch_tdep (get_frame_arch (this_frame));
3527 *this_id = frame_id_build (arm_cache_get_prev_sp_value (cache, tdep),
3528 get_frame_pc (this_frame));
3531 /* Implementation of function hook 'prev_register' in
3532 'struct frame_uwnind'. */
3534 static struct value *
3535 arm_m_exception_prev_register (struct frame_info *this_frame,
3536 void **this_cache,
3537 int prev_regnum)
3539 struct arm_prologue_cache *cache;
3540 CORE_ADDR sp_value;
3542 if (*this_cache == NULL)
3543 *this_cache = arm_m_exception_cache (this_frame);
3544 cache = (struct arm_prologue_cache *) *this_cache;
3546 /* The value was already reconstructed into PREV_SP. */
3547 arm_gdbarch_tdep *tdep
3548 = (arm_gdbarch_tdep *) gdbarch_tdep (get_frame_arch (this_frame));
3549 if (prev_regnum == ARM_SP_REGNUM)
3550 return frame_unwind_got_constant (this_frame, prev_regnum,
3551 arm_cache_get_prev_sp_value (cache, tdep));
3553 /* The value might be one of the alternative SP, if so, use the
3554 value already constructed. */
3555 if (arm_cache_is_sp_register (cache, tdep, prev_regnum))
3557 sp_value = arm_cache_get_sp_register (cache, tdep, prev_regnum);
3558 return frame_unwind_got_constant (this_frame, prev_regnum, sp_value);
3561 if (prev_regnum == ARM_PC_REGNUM)
3563 CORE_ADDR lr = frame_unwind_register_unsigned (this_frame, ARM_LR_REGNUM);
3564 struct gdbarch *gdbarch = get_frame_arch (this_frame);
3566 return frame_unwind_got_constant (this_frame, prev_regnum,
3567 arm_addr_bits_remove (gdbarch, lr));
3570 return trad_frame_get_prev_register (this_frame, cache->saved_regs,
3571 prev_regnum);
3574 /* Implementation of function hook 'sniffer' in
3575 'struct frame_uwnind'. */
3577 static int
3578 arm_m_exception_unwind_sniffer (const struct frame_unwind *self,
3579 struct frame_info *this_frame,
3580 void **this_prologue_cache)
3582 struct gdbarch *gdbarch = get_frame_arch (this_frame);
3583 CORE_ADDR this_pc = get_frame_pc (this_frame);
3585 /* No need to check is_m; this sniffer is only registered for
3586 M-profile architectures. */
3588 /* Check if exception frame returns to a magic PC value. */
3589 return arm_m_addr_is_magic (gdbarch, this_pc);
3592 /* Frame unwinder for M-profile exceptions. */
3594 struct frame_unwind arm_m_exception_unwind =
3596 "arm m exception",
3597 SIGTRAMP_FRAME,
3598 default_frame_unwind_stop_reason,
3599 arm_m_exception_this_id,
3600 arm_m_exception_prev_register,
3601 NULL,
3602 arm_m_exception_unwind_sniffer
3605 static CORE_ADDR
3606 arm_normal_frame_base (struct frame_info *this_frame, void **this_cache)
3608 struct arm_prologue_cache *cache;
3610 if (*this_cache == NULL)
3611 *this_cache = arm_make_prologue_cache (this_frame);
3612 cache = (struct arm_prologue_cache *) *this_cache;
3614 arm_gdbarch_tdep *tdep
3615 = (arm_gdbarch_tdep *) gdbarch_tdep (get_frame_arch (this_frame));
3616 return arm_cache_get_prev_sp_value (cache, tdep) - cache->framesize;
3619 struct frame_base arm_normal_base = {
3620 &arm_prologue_unwind,
3621 arm_normal_frame_base,
3622 arm_normal_frame_base,
3623 arm_normal_frame_base
3626 static struct value *
3627 arm_dwarf2_prev_register (struct frame_info *this_frame, void **this_cache,
3628 int regnum)
3630 struct gdbarch * gdbarch = get_frame_arch (this_frame);
3631 arm_gdbarch_tdep *tdep = (arm_gdbarch_tdep *) gdbarch_tdep (gdbarch);
3632 CORE_ADDR lr, cpsr;
3633 ULONGEST t_bit = arm_psr_thumb_bit (gdbarch);
3635 switch (regnum)
3637 case ARM_PC_REGNUM:
3638 /* The PC is normally copied from the return column, which
3639 describes saves of LR. However, that version may have an
3640 extra bit set to indicate Thumb state. The bit is not
3641 part of the PC. */
3643 /* Record in the frame whether the return address was signed. */
3644 if (tdep->have_pacbti)
3646 CORE_ADDR ra_auth_code
3647 = frame_unwind_register_unsigned (this_frame,
3648 tdep->pacbti_pseudo_base);
3650 if (ra_auth_code != 0)
3651 set_frame_previous_pc_masked (this_frame);
3654 lr = frame_unwind_register_unsigned (this_frame, ARM_LR_REGNUM);
3655 return frame_unwind_got_constant (this_frame, regnum,
3656 arm_addr_bits_remove (gdbarch, lr));
3658 case ARM_PS_REGNUM:
3659 /* Reconstruct the T bit; see arm_prologue_prev_register for details. */
3660 cpsr = get_frame_register_unsigned (this_frame, regnum);
3661 lr = frame_unwind_register_unsigned (this_frame, ARM_LR_REGNUM);
3662 if (IS_THUMB_ADDR (lr))
3663 cpsr |= t_bit;
3664 else
3665 cpsr &= ~t_bit;
3666 return frame_unwind_got_constant (this_frame, regnum, cpsr);
3668 default:
3669 internal_error (__FILE__, __LINE__,
3670 _("Unexpected register %d"), regnum);
3674 /* Implement the stack_frame_destroyed_p gdbarch method. */
3676 static int
3677 thumb_stack_frame_destroyed_p (struct gdbarch *gdbarch, CORE_ADDR pc)
3679 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
3680 unsigned int insn, insn2;
3681 int found_return = 0, found_stack_adjust = 0;
3682 CORE_ADDR func_start, func_end;
3683 CORE_ADDR scan_pc;
3684 gdb_byte buf[4];
3686 if (!find_pc_partial_function (pc, NULL, &func_start, &func_end))
3687 return 0;
3689 /* The epilogue is a sequence of instructions along the following lines:
3691 - add stack frame size to SP or FP
3692 - [if frame pointer used] restore SP from FP
3693 - restore registers from SP [may include PC]
3694 - a return-type instruction [if PC wasn't already restored]
3696 In a first pass, we scan forward from the current PC and verify the
3697 instructions we find as compatible with this sequence, ending in a
3698 return instruction.
3700 However, this is not sufficient to distinguish indirect function calls
3701 within a function from indirect tail calls in the epilogue in some cases.
3702 Therefore, if we didn't already find any SP-changing instruction during
3703 forward scan, we add a backward scanning heuristic to ensure we actually
3704 are in the epilogue. */
3706 scan_pc = pc;
3707 while (scan_pc < func_end && !found_return)
3709 if (target_read_memory (scan_pc, buf, 2))
3710 break;
3712 scan_pc += 2;
3713 insn = extract_unsigned_integer (buf, 2, byte_order_for_code);
3715 if ((insn & 0xff80) == 0x4700) /* bx <Rm> */
3716 found_return = 1;
3717 else if (insn == 0x46f7) /* mov pc, lr */
3718 found_return = 1;
3719 else if (thumb_instruction_restores_sp (insn))
3721 if ((insn & 0xff00) == 0xbd00) /* pop <registers, PC> */
3722 found_return = 1;
3724 else if (thumb_insn_size (insn) == 4) /* 32-bit Thumb-2 instruction */
3726 if (target_read_memory (scan_pc, buf, 2))
3727 break;
3729 scan_pc += 2;
3730 insn2 = extract_unsigned_integer (buf, 2, byte_order_for_code);
3732 if (insn == 0xe8bd) /* ldm.w sp!, <registers> */
3734 if (insn2 & 0x8000) /* <registers> include PC. */
3735 found_return = 1;
3737 else if (insn == 0xf85d /* ldr.w <Rt>, [sp], #4 */
3738 && (insn2 & 0x0fff) == 0x0b04)
3740 if ((insn2 & 0xf000) == 0xf000) /* <Rt> is PC. */
3741 found_return = 1;
3743 else if ((insn & 0xffbf) == 0xecbd /* vldm sp!, <list> */
3744 && (insn2 & 0x0e00) == 0x0a00)
3746 else
3747 break;
3749 else
3750 break;
3753 if (!found_return)
3754 return 0;
3756 /* Since any instruction in the epilogue sequence, with the possible
3757 exception of return itself, updates the stack pointer, we need to
3758 scan backwards for at most one instruction. Try either a 16-bit or
3759 a 32-bit instruction. This is just a heuristic, so we do not worry
3760 too much about false positives. */
3762 if (pc - 4 < func_start)
3763 return 0;
3764 if (target_read_memory (pc - 4, buf, 4))
3765 return 0;
3767 insn = extract_unsigned_integer (buf, 2, byte_order_for_code);
3768 insn2 = extract_unsigned_integer (buf + 2, 2, byte_order_for_code);
3770 if (thumb_instruction_restores_sp (insn2))
3771 found_stack_adjust = 1;
3772 else if (insn == 0xe8bd) /* ldm.w sp!, <registers> */
3773 found_stack_adjust = 1;
3774 else if (insn == 0xf85d /* ldr.w <Rt>, [sp], #4 */
3775 && (insn2 & 0x0fff) == 0x0b04)
3776 found_stack_adjust = 1;
3777 else if ((insn & 0xffbf) == 0xecbd /* vldm sp!, <list> */
3778 && (insn2 & 0x0e00) == 0x0a00)
3779 found_stack_adjust = 1;
3781 return found_stack_adjust;
3784 static int
3785 arm_stack_frame_destroyed_p_1 (struct gdbarch *gdbarch, CORE_ADDR pc)
3787 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
3788 unsigned int insn;
3789 int found_return;
3790 CORE_ADDR func_start, func_end;
3792 if (!find_pc_partial_function (pc, NULL, &func_start, &func_end))
3793 return 0;
3795 /* We are in the epilogue if the previous instruction was a stack
3796 adjustment and the next instruction is a possible return (bx, mov
3797 pc, or pop). We could have to scan backwards to find the stack
3798 adjustment, or forwards to find the return, but this is a decent
3799 approximation. First scan forwards. */
3801 found_return = 0;
3802 insn = read_memory_unsigned_integer (pc, 4, byte_order_for_code);
3803 if (bits (insn, 28, 31) != INST_NV)
3805 if ((insn & 0x0ffffff0) == 0x012fff10)
3806 /* BX. */
3807 found_return = 1;
3808 else if ((insn & 0x0ffffff0) == 0x01a0f000)
3809 /* MOV PC. */
3810 found_return = 1;
3811 else if ((insn & 0x0fff0000) == 0x08bd0000
3812 && (insn & 0x0000c000) != 0)
3813 /* POP (LDMIA), including PC or LR. */
3814 found_return = 1;
3817 if (!found_return)
3818 return 0;
3820 /* Scan backwards. This is just a heuristic, so do not worry about
3821 false positives from mode changes. */
3823 if (pc < func_start + 4)
3824 return 0;
3826 insn = read_memory_unsigned_integer (pc - 4, 4, byte_order_for_code);
3827 if (arm_instruction_restores_sp (insn))
3828 return 1;
3830 return 0;
3833 /* Implement the stack_frame_destroyed_p gdbarch method. */
3835 static int
3836 arm_stack_frame_destroyed_p (struct gdbarch *gdbarch, CORE_ADDR pc)
3838 if (arm_pc_is_thumb (gdbarch, pc))
3839 return thumb_stack_frame_destroyed_p (gdbarch, pc);
3840 else
3841 return arm_stack_frame_destroyed_p_1 (gdbarch, pc);
3844 /* When arguments must be pushed onto the stack, they go on in reverse
3845 order. The code below implements a FILO (stack) to do this. */
3847 struct stack_item
3849 int len;
3850 struct stack_item *prev;
3851 gdb_byte *data;
3854 static struct stack_item *
3855 push_stack_item (struct stack_item *prev, const gdb_byte *contents, int len)
3857 struct stack_item *si;
3858 si = XNEW (struct stack_item);
3859 si->data = (gdb_byte *) xmalloc (len);
3860 si->len = len;
3861 si->prev = prev;
3862 memcpy (si->data, contents, len);
3863 return si;
3866 static struct stack_item *
3867 pop_stack_item (struct stack_item *si)
3869 struct stack_item *dead = si;
3870 si = si->prev;
3871 xfree (dead->data);
3872 xfree (dead);
3873 return si;
3876 /* Implement the gdbarch type alignment method, overrides the generic
3877 alignment algorithm for anything that is arm specific. */
3879 static ULONGEST
3880 arm_type_align (gdbarch *gdbarch, struct type *t)
3882 t = check_typedef (t);
3883 if (t->code () == TYPE_CODE_ARRAY && t->is_vector ())
3885 /* Use the natural alignment for vector types (the same for
3886 scalar type), but the maximum alignment is 64-bit. */
3887 if (TYPE_LENGTH (t) > 8)
3888 return 8;
3889 else
3890 return TYPE_LENGTH (t);
3893 /* Allow the common code to calculate the alignment. */
3894 return 0;
3897 /* Possible base types for a candidate for passing and returning in
3898 VFP registers. */
3900 enum arm_vfp_cprc_base_type
3902 VFP_CPRC_UNKNOWN,
3903 VFP_CPRC_SINGLE,
3904 VFP_CPRC_DOUBLE,
3905 VFP_CPRC_VEC64,
3906 VFP_CPRC_VEC128
3909 /* The length of one element of base type B. */
3911 static unsigned
3912 arm_vfp_cprc_unit_length (enum arm_vfp_cprc_base_type b)
3914 switch (b)
3916 case VFP_CPRC_SINGLE:
3917 return 4;
3918 case VFP_CPRC_DOUBLE:
3919 return 8;
3920 case VFP_CPRC_VEC64:
3921 return 8;
3922 case VFP_CPRC_VEC128:
3923 return 16;
3924 default:
3925 internal_error (__FILE__, __LINE__, _("Invalid VFP CPRC type: %d."),
3926 (int) b);
3930 /* The character ('s', 'd' or 'q') for the type of VFP register used
3931 for passing base type B. */
3933 static int
3934 arm_vfp_cprc_reg_char (enum arm_vfp_cprc_base_type b)
3936 switch (b)
3938 case VFP_CPRC_SINGLE:
3939 return 's';
3940 case VFP_CPRC_DOUBLE:
3941 return 'd';
3942 case VFP_CPRC_VEC64:
3943 return 'd';
3944 case VFP_CPRC_VEC128:
3945 return 'q';
3946 default:
3947 internal_error (__FILE__, __LINE__, _("Invalid VFP CPRC type: %d."),
3948 (int) b);
3952 /* Determine whether T may be part of a candidate for passing and
3953 returning in VFP registers, ignoring the limit on the total number
3954 of components. If *BASE_TYPE is VFP_CPRC_UNKNOWN, set it to the
3955 classification of the first valid component found; if it is not
3956 VFP_CPRC_UNKNOWN, all components must have the same classification
3957 as *BASE_TYPE. If it is found that T contains a type not permitted
3958 for passing and returning in VFP registers, a type differently
3959 classified from *BASE_TYPE, or two types differently classified
3960 from each other, return -1, otherwise return the total number of
3961 base-type elements found (possibly 0 in an empty structure or
3962 array). Vector types are not currently supported, matching the
3963 generic AAPCS support. */
3965 static int
3966 arm_vfp_cprc_sub_candidate (struct type *t,
3967 enum arm_vfp_cprc_base_type *base_type)
3969 t = check_typedef (t);
3970 switch (t->code ())
3972 case TYPE_CODE_FLT:
3973 switch (TYPE_LENGTH (t))
3975 case 4:
3976 if (*base_type == VFP_CPRC_UNKNOWN)
3977 *base_type = VFP_CPRC_SINGLE;
3978 else if (*base_type != VFP_CPRC_SINGLE)
3979 return -1;
3980 return 1;
3982 case 8:
3983 if (*base_type == VFP_CPRC_UNKNOWN)
3984 *base_type = VFP_CPRC_DOUBLE;
3985 else if (*base_type != VFP_CPRC_DOUBLE)
3986 return -1;
3987 return 1;
3989 default:
3990 return -1;
3992 break;
3994 case TYPE_CODE_COMPLEX:
3995 /* Arguments of complex T where T is one of the types float or
3996 double get treated as if they are implemented as:
3998 struct complexT
4000 T real;
4001 T imag;
4005 switch (TYPE_LENGTH (t))
4007 case 8:
4008 if (*base_type == VFP_CPRC_UNKNOWN)
4009 *base_type = VFP_CPRC_SINGLE;
4010 else if (*base_type != VFP_CPRC_SINGLE)
4011 return -1;
4012 return 2;
4014 case 16:
4015 if (*base_type == VFP_CPRC_UNKNOWN)
4016 *base_type = VFP_CPRC_DOUBLE;
4017 else if (*base_type != VFP_CPRC_DOUBLE)
4018 return -1;
4019 return 2;
4021 default:
4022 return -1;
4024 break;
4026 case TYPE_CODE_ARRAY:
4028 if (t->is_vector ())
4030 /* A 64-bit or 128-bit containerized vector type are VFP
4031 CPRCs. */
4032 switch (TYPE_LENGTH (t))
4034 case 8:
4035 if (*base_type == VFP_CPRC_UNKNOWN)
4036 *base_type = VFP_CPRC_VEC64;
4037 return 1;
4038 case 16:
4039 if (*base_type == VFP_CPRC_UNKNOWN)
4040 *base_type = VFP_CPRC_VEC128;
4041 return 1;
4042 default:
4043 return -1;
4046 else
4048 int count;
4049 unsigned unitlen;
4051 count = arm_vfp_cprc_sub_candidate (TYPE_TARGET_TYPE (t),
4052 base_type);
4053 if (count == -1)
4054 return -1;
4055 if (TYPE_LENGTH (t) == 0)
4057 gdb_assert (count == 0);
4058 return 0;
4060 else if (count == 0)
4061 return -1;
4062 unitlen = arm_vfp_cprc_unit_length (*base_type);
4063 gdb_assert ((TYPE_LENGTH (t) % unitlen) == 0);
4064 return TYPE_LENGTH (t) / unitlen;
4067 break;
4069 case TYPE_CODE_STRUCT:
4071 int count = 0;
4072 unsigned unitlen;
4073 int i;
4074 for (i = 0; i < t->num_fields (); i++)
4076 int sub_count = 0;
4078 if (!field_is_static (&t->field (i)))
4079 sub_count = arm_vfp_cprc_sub_candidate (t->field (i).type (),
4080 base_type);
4081 if (sub_count == -1)
4082 return -1;
4083 count += sub_count;
4085 if (TYPE_LENGTH (t) == 0)
4087 gdb_assert (count == 0);
4088 return 0;
4090 else if (count == 0)
4091 return -1;
4092 unitlen = arm_vfp_cprc_unit_length (*base_type);
4093 if (TYPE_LENGTH (t) != unitlen * count)
4094 return -1;
4095 return count;
4098 case TYPE_CODE_UNION:
4100 int count = 0;
4101 unsigned unitlen;
4102 int i;
4103 for (i = 0; i < t->num_fields (); i++)
4105 int sub_count = arm_vfp_cprc_sub_candidate (t->field (i).type (),
4106 base_type);
4107 if (sub_count == -1)
4108 return -1;
4109 count = (count > sub_count ? count : sub_count);
4111 if (TYPE_LENGTH (t) == 0)
4113 gdb_assert (count == 0);
4114 return 0;
4116 else if (count == 0)
4117 return -1;
4118 unitlen = arm_vfp_cprc_unit_length (*base_type);
4119 if (TYPE_LENGTH (t) != unitlen * count)
4120 return -1;
4121 return count;
4124 default:
4125 break;
4128 return -1;
4131 /* Determine whether T is a VFP co-processor register candidate (CPRC)
4132 if passed to or returned from a non-variadic function with the VFP
4133 ABI in effect. Return 1 if it is, 0 otherwise. If it is, set
4134 *BASE_TYPE to the base type for T and *COUNT to the number of
4135 elements of that base type before returning. */
4137 static int
4138 arm_vfp_call_candidate (struct type *t, enum arm_vfp_cprc_base_type *base_type,
4139 int *count)
4141 enum arm_vfp_cprc_base_type b = VFP_CPRC_UNKNOWN;
4142 int c = arm_vfp_cprc_sub_candidate (t, &b);
4143 if (c <= 0 || c > 4)
4144 return 0;
4145 *base_type = b;
4146 *count = c;
4147 return 1;
4150 /* Return 1 if the VFP ABI should be used for passing arguments to and
4151 returning values from a function of type FUNC_TYPE, 0
4152 otherwise. */
4154 static int
4155 arm_vfp_abi_for_function (struct gdbarch *gdbarch, struct type *func_type)
4157 arm_gdbarch_tdep *tdep = (arm_gdbarch_tdep *) gdbarch_tdep (gdbarch);
4159 /* Variadic functions always use the base ABI. Assume that functions
4160 without debug info are not variadic. */
4161 if (func_type && check_typedef (func_type)->has_varargs ())
4162 return 0;
4164 /* The VFP ABI is only supported as a variant of AAPCS. */
4165 if (tdep->arm_abi != ARM_ABI_AAPCS)
4166 return 0;
4168 return tdep->fp_model == ARM_FLOAT_VFP;
4171 /* We currently only support passing parameters in integer registers, which
4172 conforms with GCC's default model, and VFP argument passing following
4173 the VFP variant of AAPCS. Several other variants exist and
4174 we should probably support some of them based on the selected ABI. */
4176 static CORE_ADDR
4177 arm_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
4178 struct regcache *regcache, CORE_ADDR bp_addr, int nargs,
4179 struct value **args, CORE_ADDR sp,
4180 function_call_return_method return_method,
4181 CORE_ADDR struct_addr)
4183 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
4184 int argnum;
4185 int argreg;
4186 int nstack;
4187 struct stack_item *si = NULL;
4188 int use_vfp_abi;
4189 struct type *ftype;
4190 unsigned vfp_regs_free = (1 << 16) - 1;
4191 arm_gdbarch_tdep *tdep = (arm_gdbarch_tdep *) gdbarch_tdep (gdbarch);
4193 /* Determine the type of this function and whether the VFP ABI
4194 applies. */
4195 ftype = check_typedef (value_type (function));
4196 if (ftype->code () == TYPE_CODE_PTR)
4197 ftype = check_typedef (TYPE_TARGET_TYPE (ftype));
4198 use_vfp_abi = arm_vfp_abi_for_function (gdbarch, ftype);
4200 /* Set the return address. For the ARM, the return breakpoint is
4201 always at BP_ADDR. */
4202 if (arm_pc_is_thumb (gdbarch, bp_addr))
4203 bp_addr |= 1;
4204 regcache_cooked_write_unsigned (regcache, ARM_LR_REGNUM, bp_addr);
4206 /* Walk through the list of args and determine how large a temporary
4207 stack is required. Need to take care here as structs may be
4208 passed on the stack, and we have to push them. */
4209 nstack = 0;
4211 argreg = ARM_A1_REGNUM;
4212 nstack = 0;
4214 /* The struct_return pointer occupies the first parameter
4215 passing register. */
4216 if (return_method == return_method_struct)
4218 arm_debug_printf ("struct return in %s = %s",
4219 gdbarch_register_name (gdbarch, argreg),
4220 paddress (gdbarch, struct_addr));
4222 regcache_cooked_write_unsigned (regcache, argreg, struct_addr);
4223 argreg++;
4226 for (argnum = 0; argnum < nargs; argnum++)
4228 int len;
4229 struct type *arg_type;
4230 struct type *target_type;
4231 enum type_code typecode;
4232 const bfd_byte *val;
4233 int align;
4234 enum arm_vfp_cprc_base_type vfp_base_type;
4235 int vfp_base_count;
4236 int may_use_core_reg = 1;
4238 arg_type = check_typedef (value_type (args[argnum]));
4239 len = TYPE_LENGTH (arg_type);
4240 target_type = TYPE_TARGET_TYPE (arg_type);
4241 typecode = arg_type->code ();
4242 val = value_contents (args[argnum]).data ();
4244 align = type_align (arg_type);
4245 /* Round alignment up to a whole number of words. */
4246 align = (align + ARM_INT_REGISTER_SIZE - 1)
4247 & ~(ARM_INT_REGISTER_SIZE - 1);
4248 /* Different ABIs have different maximum alignments. */
4249 if (tdep->arm_abi == ARM_ABI_APCS)
4251 /* The APCS ABI only requires word alignment. */
4252 align = ARM_INT_REGISTER_SIZE;
4254 else
4256 /* The AAPCS requires at most doubleword alignment. */
4257 if (align > ARM_INT_REGISTER_SIZE * 2)
4258 align = ARM_INT_REGISTER_SIZE * 2;
4261 if (use_vfp_abi
4262 && arm_vfp_call_candidate (arg_type, &vfp_base_type,
4263 &vfp_base_count))
4265 int regno;
4266 int unit_length;
4267 int shift;
4268 unsigned mask;
4270 /* Because this is a CPRC it cannot go in a core register or
4271 cause a core register to be skipped for alignment.
4272 Either it goes in VFP registers and the rest of this loop
4273 iteration is skipped for this argument, or it goes on the
4274 stack (and the stack alignment code is correct for this
4275 case). */
4276 may_use_core_reg = 0;
4278 unit_length = arm_vfp_cprc_unit_length (vfp_base_type);
4279 shift = unit_length / 4;
4280 mask = (1 << (shift * vfp_base_count)) - 1;
4281 for (regno = 0; regno < 16; regno += shift)
4282 if (((vfp_regs_free >> regno) & mask) == mask)
4283 break;
4285 if (regno < 16)
4287 int reg_char;
4288 int reg_scaled;
4289 int i;
4291 vfp_regs_free &= ~(mask << regno);
4292 reg_scaled = regno / shift;
4293 reg_char = arm_vfp_cprc_reg_char (vfp_base_type);
4294 for (i = 0; i < vfp_base_count; i++)
4296 char name_buf[4];
4297 int regnum;
4298 if (reg_char == 'q')
4299 arm_neon_quad_write (gdbarch, regcache, reg_scaled + i,
4300 val + i * unit_length);
4301 else
4303 xsnprintf (name_buf, sizeof (name_buf), "%c%d",
4304 reg_char, reg_scaled + i);
4305 regnum = user_reg_map_name_to_regnum (gdbarch, name_buf,
4306 strlen (name_buf));
4307 regcache->cooked_write (regnum, val + i * unit_length);
4310 continue;
4312 else
4314 /* This CPRC could not go in VFP registers, so all VFP
4315 registers are now marked as used. */
4316 vfp_regs_free = 0;
4320 /* Push stack padding for doubleword alignment. */
4321 if (nstack & (align - 1))
4323 si = push_stack_item (si, val, ARM_INT_REGISTER_SIZE);
4324 nstack += ARM_INT_REGISTER_SIZE;
4327 /* Doubleword aligned quantities must go in even register pairs. */
4328 if (may_use_core_reg
4329 && argreg <= ARM_LAST_ARG_REGNUM
4330 && align > ARM_INT_REGISTER_SIZE
4331 && argreg & 1)
4332 argreg++;
4334 /* If the argument is a pointer to a function, and it is a
4335 Thumb function, create a LOCAL copy of the value and set
4336 the THUMB bit in it. */
4337 if (TYPE_CODE_PTR == typecode
4338 && target_type != NULL
4339 && TYPE_CODE_FUNC == check_typedef (target_type)->code ())
4341 CORE_ADDR regval = extract_unsigned_integer (val, len, byte_order);
4342 if (arm_pc_is_thumb (gdbarch, regval))
4344 bfd_byte *copy = (bfd_byte *) alloca (len);
4345 store_unsigned_integer (copy, len, byte_order,
4346 MAKE_THUMB_ADDR (regval));
4347 val = copy;
4351 /* Copy the argument to general registers or the stack in
4352 register-sized pieces. Large arguments are split between
4353 registers and stack. */
4354 while (len > 0)
4356 int partial_len = len < ARM_INT_REGISTER_SIZE
4357 ? len : ARM_INT_REGISTER_SIZE;
4358 CORE_ADDR regval
4359 = extract_unsigned_integer (val, partial_len, byte_order);
4361 if (may_use_core_reg && argreg <= ARM_LAST_ARG_REGNUM)
4363 /* The argument is being passed in a general purpose
4364 register. */
4365 if (byte_order == BFD_ENDIAN_BIG)
4366 regval <<= (ARM_INT_REGISTER_SIZE - partial_len) * 8;
4368 arm_debug_printf ("arg %d in %s = 0x%s", argnum,
4369 gdbarch_register_name (gdbarch, argreg),
4370 phex (regval, ARM_INT_REGISTER_SIZE));
4372 regcache_cooked_write_unsigned (regcache, argreg, regval);
4373 argreg++;
4375 else
4377 gdb_byte buf[ARM_INT_REGISTER_SIZE];
4379 memset (buf, 0, sizeof (buf));
4380 store_unsigned_integer (buf, partial_len, byte_order, regval);
4382 /* Push the arguments onto the stack. */
4383 arm_debug_printf ("arg %d @ sp + %d", argnum, nstack);
4384 si = push_stack_item (si, buf, ARM_INT_REGISTER_SIZE);
4385 nstack += ARM_INT_REGISTER_SIZE;
4388 len -= partial_len;
4389 val += partial_len;
4392 /* If we have an odd number of words to push, then decrement the stack
4393 by one word now, so first stack argument will be dword aligned. */
4394 if (nstack & 4)
4395 sp -= 4;
4397 while (si)
4399 sp -= si->len;
4400 write_memory (sp, si->data, si->len);
4401 si = pop_stack_item (si);
4404 /* Finally, update teh SP register. */
4405 regcache_cooked_write_unsigned (regcache, ARM_SP_REGNUM, sp);
4407 return sp;
4411 /* Always align the frame to an 8-byte boundary. This is required on
4412 some platforms and harmless on the rest. */
4414 static CORE_ADDR
4415 arm_frame_align (struct gdbarch *gdbarch, CORE_ADDR sp)
4417 /* Align the stack to eight bytes. */
4418 return sp & ~ (CORE_ADDR) 7;
4421 static void
4422 print_fpu_flags (struct ui_file *file, int flags)
4424 if (flags & (1 << 0))
4425 gdb_puts ("IVO ", file);
4426 if (flags & (1 << 1))
4427 gdb_puts ("DVZ ", file);
4428 if (flags & (1 << 2))
4429 gdb_puts ("OFL ", file);
4430 if (flags & (1 << 3))
4431 gdb_puts ("UFL ", file);
4432 if (flags & (1 << 4))
4433 gdb_puts ("INX ", file);
4434 gdb_putc ('\n', file);
4437 /* Print interesting information about the floating point processor
4438 (if present) or emulator. */
4439 static void
4440 arm_print_float_info (struct gdbarch *gdbarch, struct ui_file *file,
4441 struct frame_info *frame, const char *args)
4443 unsigned long status = get_frame_register_unsigned (frame, ARM_FPS_REGNUM);
4444 int type;
4446 type = (status >> 24) & 127;
4447 if (status & (1 << 31))
4448 gdb_printf (file, _("Hardware FPU type %d\n"), type);
4449 else
4450 gdb_printf (file, _("Software FPU type %d\n"), type);
4451 /* i18n: [floating point unit] mask */
4452 gdb_puts (_("mask: "), file);
4453 print_fpu_flags (file, status >> 16);
4454 /* i18n: [floating point unit] flags */
4455 gdb_puts (_("flags: "), file);
4456 print_fpu_flags (file, status);
4459 /* Construct the ARM extended floating point type. */
4460 static struct type *
4461 arm_ext_type (struct gdbarch *gdbarch)
4463 arm_gdbarch_tdep *tdep = (arm_gdbarch_tdep *) gdbarch_tdep (gdbarch);
4465 if (!tdep->arm_ext_type)
4466 tdep->arm_ext_type
4467 = arch_float_type (gdbarch, -1, "builtin_type_arm_ext",
4468 floatformats_arm_ext);
4470 return tdep->arm_ext_type;
4473 static struct type *
4474 arm_neon_double_type (struct gdbarch *gdbarch)
4476 arm_gdbarch_tdep *tdep = (arm_gdbarch_tdep *) gdbarch_tdep (gdbarch);
4478 if (tdep->neon_double_type == NULL)
4480 struct type *t, *elem;
4482 t = arch_composite_type (gdbarch, "__gdb_builtin_type_neon_d",
4483 TYPE_CODE_UNION);
4484 elem = builtin_type (gdbarch)->builtin_uint8;
4485 append_composite_type_field (t, "u8", init_vector_type (elem, 8));
4486 elem = builtin_type (gdbarch)->builtin_uint16;
4487 append_composite_type_field (t, "u16", init_vector_type (elem, 4));
4488 elem = builtin_type (gdbarch)->builtin_uint32;
4489 append_composite_type_field (t, "u32", init_vector_type (elem, 2));
4490 elem = builtin_type (gdbarch)->builtin_uint64;
4491 append_composite_type_field (t, "u64", elem);
4492 elem = builtin_type (gdbarch)->builtin_float;
4493 append_composite_type_field (t, "f32", init_vector_type (elem, 2));
4494 elem = builtin_type (gdbarch)->builtin_double;
4495 append_composite_type_field (t, "f64", elem);
4497 t->set_is_vector (true);
4498 t->set_name ("neon_d");
4499 tdep->neon_double_type = t;
4502 return tdep->neon_double_type;
4505 /* FIXME: The vector types are not correctly ordered on big-endian
4506 targets. Just as s0 is the low bits of d0, d0[0] is also the low
4507 bits of d0 - regardless of what unit size is being held in d0. So
4508 the offset of the first uint8 in d0 is 7, but the offset of the
4509 first float is 4. This code works as-is for little-endian
4510 targets. */
4512 static struct type *
4513 arm_neon_quad_type (struct gdbarch *gdbarch)
4515 arm_gdbarch_tdep *tdep = (arm_gdbarch_tdep *) gdbarch_tdep (gdbarch);
4517 if (tdep->neon_quad_type == NULL)
4519 struct type *t, *elem;
4521 t = arch_composite_type (gdbarch, "__gdb_builtin_type_neon_q",
4522 TYPE_CODE_UNION);
4523 elem = builtin_type (gdbarch)->builtin_uint8;
4524 append_composite_type_field (t, "u8", init_vector_type (elem, 16));
4525 elem = builtin_type (gdbarch)->builtin_uint16;
4526 append_composite_type_field (t, "u16", init_vector_type (elem, 8));
4527 elem = builtin_type (gdbarch)->builtin_uint32;
4528 append_composite_type_field (t, "u32", init_vector_type (elem, 4));
4529 elem = builtin_type (gdbarch)->builtin_uint64;
4530 append_composite_type_field (t, "u64", init_vector_type (elem, 2));
4531 elem = builtin_type (gdbarch)->builtin_float;
4532 append_composite_type_field (t, "f32", init_vector_type (elem, 4));
4533 elem = builtin_type (gdbarch)->builtin_double;
4534 append_composite_type_field (t, "f64", init_vector_type (elem, 2));
4536 t->set_is_vector (true);
4537 t->set_name ("neon_q");
4538 tdep->neon_quad_type = t;
4541 return tdep->neon_quad_type;
4544 /* Return true if REGNUM is a Q pseudo register. Return false
4545 otherwise.
4547 REGNUM is the raw register number and not a pseudo-relative register
4548 number. */
4550 static bool
4551 is_q_pseudo (struct gdbarch *gdbarch, int regnum)
4553 arm_gdbarch_tdep *tdep = (arm_gdbarch_tdep *) gdbarch_tdep (gdbarch);
4555 /* Q pseudo registers are available for both NEON (Q0~Q15) and
4556 MVE (Q0~Q7) features. */
4557 if (tdep->have_q_pseudos
4558 && regnum >= tdep->q_pseudo_base
4559 && regnum < (tdep->q_pseudo_base + tdep->q_pseudo_count))
4560 return true;
4562 return false;
4565 /* Return true if REGNUM is a VFP S pseudo register. Return false
4566 otherwise.
4568 REGNUM is the raw register number and not a pseudo-relative register
4569 number. */
4571 static bool
4572 is_s_pseudo (struct gdbarch *gdbarch, int regnum)
4574 arm_gdbarch_tdep *tdep = (arm_gdbarch_tdep *) gdbarch_tdep (gdbarch);
4576 if (tdep->have_s_pseudos
4577 && regnum >= tdep->s_pseudo_base
4578 && regnum < (tdep->s_pseudo_base + tdep->s_pseudo_count))
4579 return true;
4581 return false;
4584 /* Return true if REGNUM is a MVE pseudo register (P0). Return false
4585 otherwise.
4587 REGNUM is the raw register number and not a pseudo-relative register
4588 number. */
4590 static bool
4591 is_mve_pseudo (struct gdbarch *gdbarch, int regnum)
4593 arm_gdbarch_tdep *tdep = (arm_gdbarch_tdep *) gdbarch_tdep (gdbarch);
4595 if (tdep->have_mve
4596 && regnum >= tdep->mve_pseudo_base
4597 && regnum < tdep->mve_pseudo_base + tdep->mve_pseudo_count)
4598 return true;
4600 return false;
4603 /* Return true if REGNUM is a PACBTI pseudo register (ra_auth_code). Return
4604 false otherwise.
4606 REGNUM is the raw register number and not a pseudo-relative register
4607 number. */
4609 static bool
4610 is_pacbti_pseudo (struct gdbarch *gdbarch, int regnum)
4612 arm_gdbarch_tdep *tdep = (arm_gdbarch_tdep *) gdbarch_tdep (gdbarch);
4614 if (tdep->have_pacbti
4615 && regnum >= tdep->pacbti_pseudo_base
4616 && regnum < tdep->pacbti_pseudo_base + tdep->pacbti_pseudo_count)
4617 return true;
4619 return false;
4622 /* Return the GDB type object for the "standard" data type of data in
4623 register N. */
4625 static struct type *
4626 arm_register_type (struct gdbarch *gdbarch, int regnum)
4628 arm_gdbarch_tdep *tdep = (arm_gdbarch_tdep *) gdbarch_tdep (gdbarch);
4630 if (is_s_pseudo (gdbarch, regnum))
4631 return builtin_type (gdbarch)->builtin_float;
4633 if (is_q_pseudo (gdbarch, regnum))
4634 return arm_neon_quad_type (gdbarch);
4636 if (is_mve_pseudo (gdbarch, regnum))
4637 return builtin_type (gdbarch)->builtin_int16;
4639 if (is_pacbti_pseudo (gdbarch, regnum))
4640 return builtin_type (gdbarch)->builtin_uint32;
4642 /* If the target description has register information, we are only
4643 in this function so that we can override the types of
4644 double-precision registers for NEON. */
4645 if (tdesc_has_registers (gdbarch_target_desc (gdbarch)))
4647 struct type *t = tdesc_register_type (gdbarch, regnum);
4649 if (regnum >= ARM_D0_REGNUM && regnum < ARM_D0_REGNUM + 32
4650 && t->code () == TYPE_CODE_FLT
4651 && tdep->have_neon)
4652 return arm_neon_double_type (gdbarch);
4653 else
4654 return t;
4657 if (regnum >= ARM_F0_REGNUM && regnum < ARM_F0_REGNUM + NUM_FREGS)
4659 if (!tdep->have_fpa_registers)
4660 return builtin_type (gdbarch)->builtin_void;
4662 return arm_ext_type (gdbarch);
4664 else if (regnum == ARM_SP_REGNUM)
4665 return builtin_type (gdbarch)->builtin_data_ptr;
4666 else if (regnum == ARM_PC_REGNUM)
4667 return builtin_type (gdbarch)->builtin_func_ptr;
4668 else if (regnum >= ARRAY_SIZE (arm_register_names))
4669 /* These registers are only supported on targets which supply
4670 an XML description. */
4671 return builtin_type (gdbarch)->builtin_int0;
4672 else
4673 return builtin_type (gdbarch)->builtin_uint32;
4676 /* Map a DWARF register REGNUM onto the appropriate GDB register
4677 number. */
4679 static int
4680 arm_dwarf_reg_to_regnum (struct gdbarch *gdbarch, int reg)
4682 /* Core integer regs. */
4683 if (reg >= 0 && reg <= 15)
4684 return reg;
4686 /* Legacy FPA encoding. These were once used in a way which
4687 overlapped with VFP register numbering, so their use is
4688 discouraged, but GDB doesn't support the ARM toolchain
4689 which used them for VFP. */
4690 if (reg >= 16 && reg <= 23)
4691 return ARM_F0_REGNUM + reg - 16;
4693 /* New assignments for the FPA registers. */
4694 if (reg >= 96 && reg <= 103)
4695 return ARM_F0_REGNUM + reg - 96;
4697 /* WMMX register assignments. */
4698 if (reg >= 104 && reg <= 111)
4699 return ARM_WCGR0_REGNUM + reg - 104;
4701 if (reg >= 112 && reg <= 127)
4702 return ARM_WR0_REGNUM + reg - 112;
4704 /* PACBTI register containing the Pointer Authentication Code. */
4705 if (reg == ARM_DWARF_RA_AUTH_CODE)
4707 arm_gdbarch_tdep *tdep = (arm_gdbarch_tdep *) gdbarch_tdep (gdbarch);
4709 if (tdep->have_pacbti)
4710 return tdep->pacbti_pseudo_base;
4712 return -1;
4715 if (reg >= 192 && reg <= 199)
4716 return ARM_WC0_REGNUM + reg - 192;
4718 /* VFP v2 registers. A double precision value is actually
4719 in d1 rather than s2, but the ABI only defines numbering
4720 for the single precision registers. This will "just work"
4721 in GDB for little endian targets (we'll read eight bytes,
4722 starting in s0 and then progressing to s1), but will be
4723 reversed on big endian targets with VFP. This won't
4724 be a problem for the new Neon quad registers; you're supposed
4725 to use DW_OP_piece for those. */
4726 if (reg >= 64 && reg <= 95)
4728 char name_buf[4];
4730 xsnprintf (name_buf, sizeof (name_buf), "s%d", reg - 64);
4731 return user_reg_map_name_to_regnum (gdbarch, name_buf,
4732 strlen (name_buf));
4735 /* VFP v3 / Neon registers. This range is also used for VFP v2
4736 registers, except that it now describes d0 instead of s0. */
4737 if (reg >= 256 && reg <= 287)
4739 char name_buf[4];
4741 xsnprintf (name_buf, sizeof (name_buf), "d%d", reg - 256);
4742 return user_reg_map_name_to_regnum (gdbarch, name_buf,
4743 strlen (name_buf));
4746 return -1;
4749 /* Map GDB internal REGNUM onto the Arm simulator register numbers. */
4750 static int
4751 arm_register_sim_regno (struct gdbarch *gdbarch, int regnum)
4753 int reg = regnum;
4754 gdb_assert (reg >= 0 && reg < gdbarch_num_regs (gdbarch));
4756 if (regnum >= ARM_WR0_REGNUM && regnum <= ARM_WR15_REGNUM)
4757 return regnum - ARM_WR0_REGNUM + SIM_ARM_IWMMXT_COP0R0_REGNUM;
4759 if (regnum >= ARM_WC0_REGNUM && regnum <= ARM_WC7_REGNUM)
4760 return regnum - ARM_WC0_REGNUM + SIM_ARM_IWMMXT_COP1R0_REGNUM;
4762 if (regnum >= ARM_WCGR0_REGNUM && regnum <= ARM_WCGR7_REGNUM)
4763 return regnum - ARM_WCGR0_REGNUM + SIM_ARM_IWMMXT_COP1R8_REGNUM;
4765 if (reg < NUM_GREGS)
4766 return SIM_ARM_R0_REGNUM + reg;
4767 reg -= NUM_GREGS;
4769 if (reg < NUM_FREGS)
4770 return SIM_ARM_FP0_REGNUM + reg;
4771 reg -= NUM_FREGS;
4773 if (reg < NUM_SREGS)
4774 return SIM_ARM_FPS_REGNUM + reg;
4775 reg -= NUM_SREGS;
4777 internal_error (__FILE__, __LINE__, _("Bad REGNUM %d"), regnum);
4780 static const unsigned char op_lit0 = DW_OP_lit0;
4782 static void
4783 arm_dwarf2_frame_init_reg (struct gdbarch *gdbarch, int regnum,
4784 struct dwarf2_frame_state_reg *reg,
4785 struct frame_info *this_frame)
4787 if (is_pacbti_pseudo (gdbarch, regnum))
4789 /* Initialize RA_AUTH_CODE to zero. */
4790 reg->how = DWARF2_FRAME_REG_SAVED_VAL_EXP;
4791 reg->loc.exp.start = &op_lit0;
4792 reg->loc.exp.len = 1;
4793 return;
4796 switch (regnum)
4798 case ARM_PC_REGNUM:
4799 case ARM_PS_REGNUM:
4800 reg->how = DWARF2_FRAME_REG_FN;
4801 reg->loc.fn = arm_dwarf2_prev_register;
4802 break;
4803 case ARM_SP_REGNUM:
4804 reg->how = DWARF2_FRAME_REG_CFA;
4805 break;
4809 /* Given BUF, which is OLD_LEN bytes ending at ENDADDR, expand
4810 the buffer to be NEW_LEN bytes ending at ENDADDR. Return
4811 NULL if an error occurs. BUF is freed. */
4813 static gdb_byte *
4814 extend_buffer_earlier (gdb_byte *buf, CORE_ADDR endaddr,
4815 int old_len, int new_len)
4817 gdb_byte *new_buf;
4818 int bytes_to_read = new_len - old_len;
4820 new_buf = (gdb_byte *) xmalloc (new_len);
4821 memcpy (new_buf + bytes_to_read, buf, old_len);
4822 xfree (buf);
4823 if (target_read_code (endaddr - new_len, new_buf, bytes_to_read) != 0)
4825 xfree (new_buf);
4826 return NULL;
4828 return new_buf;
4831 /* An IT block is at most the 2-byte IT instruction followed by
4832 four 4-byte instructions. The furthest back we must search to
4833 find an IT block that affects the current instruction is thus
4834 2 + 3 * 4 == 14 bytes. */
4835 #define MAX_IT_BLOCK_PREFIX 14
4837 /* Use a quick scan if there are more than this many bytes of
4838 code. */
4839 #define IT_SCAN_THRESHOLD 32
4841 /* Adjust a breakpoint's address to move breakpoints out of IT blocks.
4842 A breakpoint in an IT block may not be hit, depending on the
4843 condition flags. */
4844 static CORE_ADDR
4845 arm_adjust_breakpoint_address (struct gdbarch *gdbarch, CORE_ADDR bpaddr)
4847 gdb_byte *buf;
4848 char map_type;
4849 CORE_ADDR boundary, func_start;
4850 int buf_len;
4851 enum bfd_endian order = gdbarch_byte_order_for_code (gdbarch);
4852 int i, any, last_it, last_it_count;
4853 arm_gdbarch_tdep *tdep = (arm_gdbarch_tdep *) gdbarch_tdep (gdbarch);
4855 /* If we are using BKPT breakpoints, none of this is necessary. */
4856 if (tdep->thumb2_breakpoint == NULL)
4857 return bpaddr;
4859 /* ARM mode does not have this problem. */
4860 if (!arm_pc_is_thumb (gdbarch, bpaddr))
4861 return bpaddr;
4863 /* We are setting a breakpoint in Thumb code that could potentially
4864 contain an IT block. The first step is to find how much Thumb
4865 code there is; we do not need to read outside of known Thumb
4866 sequences. */
4867 map_type = arm_find_mapping_symbol (bpaddr, &boundary);
4868 if (map_type == 0)
4869 /* Thumb-2 code must have mapping symbols to have a chance. */
4870 return bpaddr;
4872 bpaddr = gdbarch_addr_bits_remove (gdbarch, bpaddr);
4874 if (find_pc_partial_function (bpaddr, NULL, &func_start, NULL)
4875 && func_start > boundary)
4876 boundary = func_start;
4878 /* Search for a candidate IT instruction. We have to do some fancy
4879 footwork to distinguish a real IT instruction from the second
4880 half of a 32-bit instruction, but there is no need for that if
4881 there's no candidate. */
4882 buf_len = std::min (bpaddr - boundary, (CORE_ADDR) MAX_IT_BLOCK_PREFIX);
4883 if (buf_len == 0)
4884 /* No room for an IT instruction. */
4885 return bpaddr;
4887 buf = (gdb_byte *) xmalloc (buf_len);
4888 if (target_read_code (bpaddr - buf_len, buf, buf_len) != 0)
4889 return bpaddr;
4890 any = 0;
4891 for (i = 0; i < buf_len; i += 2)
4893 unsigned short inst1 = extract_unsigned_integer (&buf[i], 2, order);
4894 if ((inst1 & 0xff00) == 0xbf00 && (inst1 & 0x000f) != 0)
4896 any = 1;
4897 break;
4901 if (any == 0)
4903 xfree (buf);
4904 return bpaddr;
4907 /* OK, the code bytes before this instruction contain at least one
4908 halfword which resembles an IT instruction. We know that it's
4909 Thumb code, but there are still two possibilities. Either the
4910 halfword really is an IT instruction, or it is the second half of
4911 a 32-bit Thumb instruction. The only way we can tell is to
4912 scan forwards from a known instruction boundary. */
4913 if (bpaddr - boundary > IT_SCAN_THRESHOLD)
4915 int definite;
4917 /* There's a lot of code before this instruction. Start with an
4918 optimistic search; it's easy to recognize halfwords that can
4919 not be the start of a 32-bit instruction, and use that to
4920 lock on to the instruction boundaries. */
4921 buf = extend_buffer_earlier (buf, bpaddr, buf_len, IT_SCAN_THRESHOLD);
4922 if (buf == NULL)
4923 return bpaddr;
4924 buf_len = IT_SCAN_THRESHOLD;
4926 definite = 0;
4927 for (i = 0; i < buf_len - sizeof (buf) && ! definite; i += 2)
4929 unsigned short inst1 = extract_unsigned_integer (&buf[i], 2, order);
4930 if (thumb_insn_size (inst1) == 2)
4932 definite = 1;
4933 break;
4937 /* At this point, if DEFINITE, BUF[I] is the first place we
4938 are sure that we know the instruction boundaries, and it is far
4939 enough from BPADDR that we could not miss an IT instruction
4940 affecting BPADDR. If ! DEFINITE, give up - start from a
4941 known boundary. */
4942 if (! definite)
4944 buf = extend_buffer_earlier (buf, bpaddr, buf_len,
4945 bpaddr - boundary);
4946 if (buf == NULL)
4947 return bpaddr;
4948 buf_len = bpaddr - boundary;
4949 i = 0;
4952 else
4954 buf = extend_buffer_earlier (buf, bpaddr, buf_len, bpaddr - boundary);
4955 if (buf == NULL)
4956 return bpaddr;
4957 buf_len = bpaddr - boundary;
4958 i = 0;
4961 /* Scan forwards. Find the last IT instruction before BPADDR. */
4962 last_it = -1;
4963 last_it_count = 0;
4964 while (i < buf_len)
4966 unsigned short inst1 = extract_unsigned_integer (&buf[i], 2, order);
4967 last_it_count--;
4968 if ((inst1 & 0xff00) == 0xbf00 && (inst1 & 0x000f) != 0)
4970 last_it = i;
4971 if (inst1 & 0x0001)
4972 last_it_count = 4;
4973 else if (inst1 & 0x0002)
4974 last_it_count = 3;
4975 else if (inst1 & 0x0004)
4976 last_it_count = 2;
4977 else
4978 last_it_count = 1;
4980 i += thumb_insn_size (inst1);
4983 xfree (buf);
4985 if (last_it == -1)
4986 /* There wasn't really an IT instruction after all. */
4987 return bpaddr;
4989 if (last_it_count < 1)
4990 /* It was too far away. */
4991 return bpaddr;
4993 /* This really is a trouble spot. Move the breakpoint to the IT
4994 instruction. */
4995 return bpaddr - buf_len + last_it;
4998 /* ARM displaced stepping support.
5000 Generally ARM displaced stepping works as follows:
5002 1. When an instruction is to be single-stepped, it is first decoded by
5003 arm_process_displaced_insn. Depending on the type of instruction, it is
5004 then copied to a scratch location, possibly in a modified form. The
5005 copy_* set of functions performs such modification, as necessary. A
5006 breakpoint is placed after the modified instruction in the scratch space
5007 to return control to GDB. Note in particular that instructions which
5008 modify the PC will no longer do so after modification.
5010 2. The instruction is single-stepped, by setting the PC to the scratch
5011 location address, and resuming. Control returns to GDB when the
5012 breakpoint is hit.
5014 3. A cleanup function (cleanup_*) is called corresponding to the copy_*
5015 function used for the current instruction. This function's job is to
5016 put the CPU/memory state back to what it would have been if the
5017 instruction had been executed unmodified in its original location. */
5019 /* NOP instruction (mov r0, r0). */
5020 #define ARM_NOP 0xe1a00000
5021 #define THUMB_NOP 0x4600
5023 /* Helper for register reads for displaced stepping. In particular, this
5024 returns the PC as it would be seen by the instruction at its original
5025 location. */
5027 ULONGEST
5028 displaced_read_reg (regcache *regs, arm_displaced_step_copy_insn_closure *dsc,
5029 int regno)
5031 ULONGEST ret;
5032 CORE_ADDR from = dsc->insn_addr;
5034 if (regno == ARM_PC_REGNUM)
5036 /* Compute pipeline offset:
5037 - When executing an ARM instruction, PC reads as the address of the
5038 current instruction plus 8.
5039 - When executing a Thumb instruction, PC reads as the address of the
5040 current instruction plus 4. */
5042 if (!dsc->is_thumb)
5043 from += 8;
5044 else
5045 from += 4;
5047 displaced_debug_printf ("read pc value %.8lx",
5048 (unsigned long) from);
5049 return (ULONGEST) from;
5051 else
5053 regcache_cooked_read_unsigned (regs, regno, &ret);
5055 displaced_debug_printf ("read r%d value %.8lx",
5056 regno, (unsigned long) ret);
5058 return ret;
5062 static int
5063 displaced_in_arm_mode (struct regcache *regs)
5065 ULONGEST ps;
5066 ULONGEST t_bit = arm_psr_thumb_bit (regs->arch ());
5068 regcache_cooked_read_unsigned (regs, ARM_PS_REGNUM, &ps);
5070 return (ps & t_bit) == 0;
5073 /* Write to the PC as from a branch instruction. */
5075 static void
5076 branch_write_pc (regcache *regs, arm_displaced_step_copy_insn_closure *dsc,
5077 ULONGEST val)
5079 if (!dsc->is_thumb)
5080 /* Note: If bits 0/1 are set, this branch would be unpredictable for
5081 architecture versions < 6. */
5082 regcache_cooked_write_unsigned (regs, ARM_PC_REGNUM,
5083 val & ~(ULONGEST) 0x3);
5084 else
5085 regcache_cooked_write_unsigned (regs, ARM_PC_REGNUM,
5086 val & ~(ULONGEST) 0x1);
5089 /* Write to the PC as from a branch-exchange instruction. */
5091 static void
5092 bx_write_pc (struct regcache *regs, ULONGEST val)
5094 ULONGEST ps;
5095 ULONGEST t_bit = arm_psr_thumb_bit (regs->arch ());
5097 regcache_cooked_read_unsigned (regs, ARM_PS_REGNUM, &ps);
5099 if ((val & 1) == 1)
5101 regcache_cooked_write_unsigned (regs, ARM_PS_REGNUM, ps | t_bit);
5102 regcache_cooked_write_unsigned (regs, ARM_PC_REGNUM, val & 0xfffffffe);
5104 else if ((val & 2) == 0)
5106 regcache_cooked_write_unsigned (regs, ARM_PS_REGNUM, ps & ~t_bit);
5107 regcache_cooked_write_unsigned (regs, ARM_PC_REGNUM, val);
5109 else
5111 /* Unpredictable behaviour. Try to do something sensible (switch to ARM
5112 mode, align dest to 4 bytes). */
5113 warning (_("Single-stepping BX to non-word-aligned ARM instruction."));
5114 regcache_cooked_write_unsigned (regs, ARM_PS_REGNUM, ps & ~t_bit);
5115 regcache_cooked_write_unsigned (regs, ARM_PC_REGNUM, val & 0xfffffffc);
5119 /* Write to the PC as if from a load instruction. */
5121 static void
5122 load_write_pc (regcache *regs, arm_displaced_step_copy_insn_closure *dsc,
5123 ULONGEST val)
5125 if (DISPLACED_STEPPING_ARCH_VERSION >= 5)
5126 bx_write_pc (regs, val);
5127 else
5128 branch_write_pc (regs, dsc, val);
5131 /* Write to the PC as if from an ALU instruction. */
5133 static void
5134 alu_write_pc (regcache *regs, arm_displaced_step_copy_insn_closure *dsc,
5135 ULONGEST val)
5137 if (DISPLACED_STEPPING_ARCH_VERSION >= 7 && !dsc->is_thumb)
5138 bx_write_pc (regs, val);
5139 else
5140 branch_write_pc (regs, dsc, val);
5143 /* Helper for writing to registers for displaced stepping. Writing to the PC
5144 has a varying effects depending on the instruction which does the write:
5145 this is controlled by the WRITE_PC argument. */
5147 void
5148 displaced_write_reg (regcache *regs, arm_displaced_step_copy_insn_closure *dsc,
5149 int regno, ULONGEST val, enum pc_write_style write_pc)
5151 if (regno == ARM_PC_REGNUM)
5153 displaced_debug_printf ("writing pc %.8lx", (unsigned long) val);
5155 switch (write_pc)
5157 case BRANCH_WRITE_PC:
5158 branch_write_pc (regs, dsc, val);
5159 break;
5161 case BX_WRITE_PC:
5162 bx_write_pc (regs, val);
5163 break;
5165 case LOAD_WRITE_PC:
5166 load_write_pc (regs, dsc, val);
5167 break;
5169 case ALU_WRITE_PC:
5170 alu_write_pc (regs, dsc, val);
5171 break;
5173 case CANNOT_WRITE_PC:
5174 warning (_("Instruction wrote to PC in an unexpected way when "
5175 "single-stepping"));
5176 break;
5178 default:
5179 internal_error (__FILE__, __LINE__,
5180 _("Invalid argument to displaced_write_reg"));
5183 dsc->wrote_to_pc = 1;
5185 else
5187 displaced_debug_printf ("writing r%d value %.8lx",
5188 regno, (unsigned long) val);
5189 regcache_cooked_write_unsigned (regs, regno, val);
5193 /* This function is used to concisely determine if an instruction INSN
5194 references PC. Register fields of interest in INSN should have the
5195 corresponding fields of BITMASK set to 0b1111. The function
5196 returns return 1 if any of these fields in INSN reference the PC
5197 (also 0b1111, r15), else it returns 0. */
5199 static int
5200 insn_references_pc (uint32_t insn, uint32_t bitmask)
5202 uint32_t lowbit = 1;
5204 while (bitmask != 0)
5206 uint32_t mask;
5208 for (; lowbit && (bitmask & lowbit) == 0; lowbit <<= 1)
5211 if (!lowbit)
5212 break;
5214 mask = lowbit * 0xf;
5216 if ((insn & mask) == mask)
5217 return 1;
5219 bitmask &= ~mask;
5222 return 0;
5225 /* The simplest copy function. Many instructions have the same effect no
5226 matter what address they are executed at: in those cases, use this. */
5228 static int
5229 arm_copy_unmodified (struct gdbarch *gdbarch, uint32_t insn, const char *iname,
5230 arm_displaced_step_copy_insn_closure *dsc)
5232 displaced_debug_printf ("copying insn %.8lx, opcode/class '%s' unmodified",
5233 (unsigned long) insn, iname);
5235 dsc->modinsn[0] = insn;
5237 return 0;
5240 static int
5241 thumb_copy_unmodified_32bit (struct gdbarch *gdbarch, uint16_t insn1,
5242 uint16_t insn2, const char *iname,
5243 arm_displaced_step_copy_insn_closure *dsc)
5245 displaced_debug_printf ("copying insn %.4x %.4x, opcode/class '%s' "
5246 "unmodified", insn1, insn2, iname);
5248 dsc->modinsn[0] = insn1;
5249 dsc->modinsn[1] = insn2;
5250 dsc->numinsns = 2;
5252 return 0;
5255 /* Copy 16-bit Thumb(Thumb and 16-bit Thumb-2) instruction without any
5256 modification. */
5257 static int
5258 thumb_copy_unmodified_16bit (struct gdbarch *gdbarch, uint16_t insn,
5259 const char *iname,
5260 arm_displaced_step_copy_insn_closure *dsc)
5262 displaced_debug_printf ("copying insn %.4x, opcode/class '%s' unmodified",
5263 insn, iname);
5265 dsc->modinsn[0] = insn;
5267 return 0;
5270 /* Preload instructions with immediate offset. */
5272 static void
5273 cleanup_preload (struct gdbarch *gdbarch, regcache *regs,
5274 arm_displaced_step_copy_insn_closure *dsc)
5276 displaced_write_reg (regs, dsc, 0, dsc->tmp[0], CANNOT_WRITE_PC);
5277 if (!dsc->u.preload.immed)
5278 displaced_write_reg (regs, dsc, 1, dsc->tmp[1], CANNOT_WRITE_PC);
5281 static void
5282 install_preload (struct gdbarch *gdbarch, struct regcache *regs,
5283 arm_displaced_step_copy_insn_closure *dsc, unsigned int rn)
5285 ULONGEST rn_val;
5286 /* Preload instructions:
5288 {pli/pld} [rn, #+/-imm]
5290 {pli/pld} [r0, #+/-imm]. */
5292 dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
5293 rn_val = displaced_read_reg (regs, dsc, rn);
5294 displaced_write_reg (regs, dsc, 0, rn_val, CANNOT_WRITE_PC);
5295 dsc->u.preload.immed = 1;
5297 dsc->cleanup = &cleanup_preload;
5300 static int
5301 arm_copy_preload (struct gdbarch *gdbarch, uint32_t insn, struct regcache *regs,
5302 arm_displaced_step_copy_insn_closure *dsc)
5304 unsigned int rn = bits (insn, 16, 19);
5306 if (!insn_references_pc (insn, 0x000f0000ul))
5307 return arm_copy_unmodified (gdbarch, insn, "preload", dsc);
5309 displaced_debug_printf ("copying preload insn %.8lx", (unsigned long) insn);
5311 dsc->modinsn[0] = insn & 0xfff0ffff;
5313 install_preload (gdbarch, regs, dsc, rn);
5315 return 0;
5318 static int
5319 thumb2_copy_preload (struct gdbarch *gdbarch, uint16_t insn1, uint16_t insn2,
5320 regcache *regs, arm_displaced_step_copy_insn_closure *dsc)
5322 unsigned int rn = bits (insn1, 0, 3);
5323 unsigned int u_bit = bit (insn1, 7);
5324 int imm12 = bits (insn2, 0, 11);
5325 ULONGEST pc_val;
5327 if (rn != ARM_PC_REGNUM)
5328 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2, "preload", dsc);
5330 /* PC is only allowed to use in PLI (immediate,literal) Encoding T3, and
5331 PLD (literal) Encoding T1. */
5332 displaced_debug_printf ("copying pld/pli pc (0x%x) %c imm12 %.4x",
5333 (unsigned int) dsc->insn_addr, u_bit ? '+' : '-',
5334 imm12);
5336 if (!u_bit)
5337 imm12 = -1 * imm12;
5339 /* Rewrite instruction {pli/pld} PC imm12 into:
5340 Prepare: tmp[0] <- r0, tmp[1] <- r1, r0 <- pc, r1 <- imm12
5342 {pli/pld} [r0, r1]
5344 Cleanup: r0 <- tmp[0], r1 <- tmp[1]. */
5346 dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
5347 dsc->tmp[1] = displaced_read_reg (regs, dsc, 1);
5349 pc_val = displaced_read_reg (regs, dsc, ARM_PC_REGNUM);
5351 displaced_write_reg (regs, dsc, 0, pc_val, CANNOT_WRITE_PC);
5352 displaced_write_reg (regs, dsc, 1, imm12, CANNOT_WRITE_PC);
5353 dsc->u.preload.immed = 0;
5355 /* {pli/pld} [r0, r1] */
5356 dsc->modinsn[0] = insn1 & 0xfff0;
5357 dsc->modinsn[1] = 0xf001;
5358 dsc->numinsns = 2;
5360 dsc->cleanup = &cleanup_preload;
5361 return 0;
5364 /* Preload instructions with register offset. */
5366 static void
5367 install_preload_reg(struct gdbarch *gdbarch, struct regcache *regs,
5368 arm_displaced_step_copy_insn_closure *dsc, unsigned int rn,
5369 unsigned int rm)
5371 ULONGEST rn_val, rm_val;
5373 /* Preload register-offset instructions:
5375 {pli/pld} [rn, rm {, shift}]
5377 {pli/pld} [r0, r1 {, shift}]. */
5379 dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
5380 dsc->tmp[1] = displaced_read_reg (regs, dsc, 1);
5381 rn_val = displaced_read_reg (regs, dsc, rn);
5382 rm_val = displaced_read_reg (regs, dsc, rm);
5383 displaced_write_reg (regs, dsc, 0, rn_val, CANNOT_WRITE_PC);
5384 displaced_write_reg (regs, dsc, 1, rm_val, CANNOT_WRITE_PC);
5385 dsc->u.preload.immed = 0;
5387 dsc->cleanup = &cleanup_preload;
5390 static int
5391 arm_copy_preload_reg (struct gdbarch *gdbarch, uint32_t insn,
5392 struct regcache *regs,
5393 arm_displaced_step_copy_insn_closure *dsc)
5395 unsigned int rn = bits (insn, 16, 19);
5396 unsigned int rm = bits (insn, 0, 3);
5399 if (!insn_references_pc (insn, 0x000f000ful))
5400 return arm_copy_unmodified (gdbarch, insn, "preload reg", dsc);
5402 displaced_debug_printf ("copying preload insn %.8lx",
5403 (unsigned long) insn);
5405 dsc->modinsn[0] = (insn & 0xfff0fff0) | 0x1;
5407 install_preload_reg (gdbarch, regs, dsc, rn, rm);
5408 return 0;
5411 /* Copy/cleanup coprocessor load and store instructions. */
5413 static void
5414 cleanup_copro_load_store (struct gdbarch *gdbarch,
5415 struct regcache *regs,
5416 arm_displaced_step_copy_insn_closure *dsc)
5418 ULONGEST rn_val = displaced_read_reg (regs, dsc, 0);
5420 displaced_write_reg (regs, dsc, 0, dsc->tmp[0], CANNOT_WRITE_PC);
5422 if (dsc->u.ldst.writeback)
5423 displaced_write_reg (regs, dsc, dsc->u.ldst.rn, rn_val, LOAD_WRITE_PC);
5426 static void
5427 install_copro_load_store (struct gdbarch *gdbarch, struct regcache *regs,
5428 arm_displaced_step_copy_insn_closure *dsc,
5429 int writeback, unsigned int rn)
5431 ULONGEST rn_val;
5433 /* Coprocessor load/store instructions:
5435 {stc/stc2} [<Rn>, #+/-imm] (and other immediate addressing modes)
5437 {stc/stc2} [r0, #+/-imm].
5439 ldc/ldc2 are handled identically. */
5441 dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
5442 rn_val = displaced_read_reg (regs, dsc, rn);
5443 /* PC should be 4-byte aligned. */
5444 rn_val = rn_val & 0xfffffffc;
5445 displaced_write_reg (regs, dsc, 0, rn_val, CANNOT_WRITE_PC);
5447 dsc->u.ldst.writeback = writeback;
5448 dsc->u.ldst.rn = rn;
5450 dsc->cleanup = &cleanup_copro_load_store;
5453 static int
5454 arm_copy_copro_load_store (struct gdbarch *gdbarch, uint32_t insn,
5455 struct regcache *regs,
5456 arm_displaced_step_copy_insn_closure *dsc)
5458 unsigned int rn = bits (insn, 16, 19);
5460 if (!insn_references_pc (insn, 0x000f0000ul))
5461 return arm_copy_unmodified (gdbarch, insn, "copro load/store", dsc);
5463 displaced_debug_printf ("copying coprocessor load/store insn %.8lx",
5464 (unsigned long) insn);
5466 dsc->modinsn[0] = insn & 0xfff0ffff;
5468 install_copro_load_store (gdbarch, regs, dsc, bit (insn, 25), rn);
5470 return 0;
5473 static int
5474 thumb2_copy_copro_load_store (struct gdbarch *gdbarch, uint16_t insn1,
5475 uint16_t insn2, struct regcache *regs,
5476 arm_displaced_step_copy_insn_closure *dsc)
5478 unsigned int rn = bits (insn1, 0, 3);
5480 if (rn != ARM_PC_REGNUM)
5481 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
5482 "copro load/store", dsc);
5484 displaced_debug_printf ("copying coprocessor load/store insn %.4x%.4x",
5485 insn1, insn2);
5487 dsc->modinsn[0] = insn1 & 0xfff0;
5488 dsc->modinsn[1] = insn2;
5489 dsc->numinsns = 2;
5491 /* This function is called for copying instruction LDC/LDC2/VLDR, which
5492 doesn't support writeback, so pass 0. */
5493 install_copro_load_store (gdbarch, regs, dsc, 0, rn);
5495 return 0;
5498 /* Clean up branch instructions (actually perform the branch, by setting
5499 PC). */
5501 static void
5502 cleanup_branch (struct gdbarch *gdbarch, struct regcache *regs,
5503 arm_displaced_step_copy_insn_closure *dsc)
5505 uint32_t status = displaced_read_reg (regs, dsc, ARM_PS_REGNUM);
5506 int branch_taken = condition_true (dsc->u.branch.cond, status);
5507 enum pc_write_style write_pc = dsc->u.branch.exchange
5508 ? BX_WRITE_PC : BRANCH_WRITE_PC;
5510 if (!branch_taken)
5511 return;
5513 if (dsc->u.branch.link)
5515 /* The value of LR should be the next insn of current one. In order
5516 not to confuse logic handling later insn `bx lr', if current insn mode
5517 is Thumb, the bit 0 of LR value should be set to 1. */
5518 ULONGEST next_insn_addr = dsc->insn_addr + dsc->insn_size;
5520 if (dsc->is_thumb)
5521 next_insn_addr |= 0x1;
5523 displaced_write_reg (regs, dsc, ARM_LR_REGNUM, next_insn_addr,
5524 CANNOT_WRITE_PC);
5527 displaced_write_reg (regs, dsc, ARM_PC_REGNUM, dsc->u.branch.dest, write_pc);
5530 /* Copy B/BL/BLX instructions with immediate destinations. */
5532 static void
5533 install_b_bl_blx (struct gdbarch *gdbarch, struct regcache *regs,
5534 arm_displaced_step_copy_insn_closure *dsc,
5535 unsigned int cond, int exchange, int link, long offset)
5537 /* Implement "BL<cond> <label>" as:
5539 Preparation: cond <- instruction condition
5540 Insn: mov r0, r0 (nop)
5541 Cleanup: if (condition true) { r14 <- pc; pc <- label }.
5543 B<cond> similar, but don't set r14 in cleanup. */
5545 dsc->u.branch.cond = cond;
5546 dsc->u.branch.link = link;
5547 dsc->u.branch.exchange = exchange;
5549 dsc->u.branch.dest = dsc->insn_addr;
5550 if (link && exchange)
5551 /* For BLX, offset is computed from the Align (PC, 4). */
5552 dsc->u.branch.dest = dsc->u.branch.dest & 0xfffffffc;
5554 if (dsc->is_thumb)
5555 dsc->u.branch.dest += 4 + offset;
5556 else
5557 dsc->u.branch.dest += 8 + offset;
5559 dsc->cleanup = &cleanup_branch;
5561 static int
5562 arm_copy_b_bl_blx (struct gdbarch *gdbarch, uint32_t insn,
5563 regcache *regs, arm_displaced_step_copy_insn_closure *dsc)
5565 unsigned int cond = bits (insn, 28, 31);
5566 int exchange = (cond == 0xf);
5567 int link = exchange || bit (insn, 24);
5568 long offset;
5570 displaced_debug_printf ("copying %s immediate insn %.8lx",
5571 (exchange) ? "blx" : (link) ? "bl" : "b",
5572 (unsigned long) insn);
5573 if (exchange)
5574 /* For BLX, set bit 0 of the destination. The cleanup_branch function will
5575 then arrange the switch into Thumb mode. */
5576 offset = (bits (insn, 0, 23) << 2) | (bit (insn, 24) << 1) | 1;
5577 else
5578 offset = bits (insn, 0, 23) << 2;
5580 if (bit (offset, 25))
5581 offset = offset | ~0x3ffffff;
5583 dsc->modinsn[0] = ARM_NOP;
5585 install_b_bl_blx (gdbarch, regs, dsc, cond, exchange, link, offset);
5586 return 0;
5589 static int
5590 thumb2_copy_b_bl_blx (struct gdbarch *gdbarch, uint16_t insn1,
5591 uint16_t insn2, struct regcache *regs,
5592 arm_displaced_step_copy_insn_closure *dsc)
5594 int link = bit (insn2, 14);
5595 int exchange = link && !bit (insn2, 12);
5596 int cond = INST_AL;
5597 long offset = 0;
5598 int j1 = bit (insn2, 13);
5599 int j2 = bit (insn2, 11);
5600 int s = sbits (insn1, 10, 10);
5601 int i1 = !(j1 ^ bit (insn1, 10));
5602 int i2 = !(j2 ^ bit (insn1, 10));
5604 if (!link && !exchange) /* B */
5606 offset = (bits (insn2, 0, 10) << 1);
5607 if (bit (insn2, 12)) /* Encoding T4 */
5609 offset |= (bits (insn1, 0, 9) << 12)
5610 | (i2 << 22)
5611 | (i1 << 23)
5612 | (s << 24);
5613 cond = INST_AL;
5615 else /* Encoding T3 */
5617 offset |= (bits (insn1, 0, 5) << 12)
5618 | (j1 << 18)
5619 | (j2 << 19)
5620 | (s << 20);
5621 cond = bits (insn1, 6, 9);
5624 else
5626 offset = (bits (insn1, 0, 9) << 12);
5627 offset |= ((i2 << 22) | (i1 << 23) | (s << 24));
5628 offset |= exchange ?
5629 (bits (insn2, 1, 10) << 2) : (bits (insn2, 0, 10) << 1);
5632 displaced_debug_printf ("copying %s insn %.4x %.4x with offset %.8lx",
5633 link ? (exchange) ? "blx" : "bl" : "b",
5634 insn1, insn2, offset);
5636 dsc->modinsn[0] = THUMB_NOP;
5638 install_b_bl_blx (gdbarch, regs, dsc, cond, exchange, link, offset);
5639 return 0;
5642 /* Copy B Thumb instructions. */
5643 static int
5644 thumb_copy_b (struct gdbarch *gdbarch, uint16_t insn,
5645 arm_displaced_step_copy_insn_closure *dsc)
5647 unsigned int cond = 0;
5648 int offset = 0;
5649 unsigned short bit_12_15 = bits (insn, 12, 15);
5650 CORE_ADDR from = dsc->insn_addr;
5652 if (bit_12_15 == 0xd)
5654 /* offset = SignExtend (imm8:0, 32) */
5655 offset = sbits ((insn << 1), 0, 8);
5656 cond = bits (insn, 8, 11);
5658 else if (bit_12_15 == 0xe) /* Encoding T2 */
5660 offset = sbits ((insn << 1), 0, 11);
5661 cond = INST_AL;
5664 displaced_debug_printf ("copying b immediate insn %.4x with offset %d",
5665 insn, offset);
5667 dsc->u.branch.cond = cond;
5668 dsc->u.branch.link = 0;
5669 dsc->u.branch.exchange = 0;
5670 dsc->u.branch.dest = from + 4 + offset;
5672 dsc->modinsn[0] = THUMB_NOP;
5674 dsc->cleanup = &cleanup_branch;
5676 return 0;
5679 /* Copy BX/BLX with register-specified destinations. */
5681 static void
5682 install_bx_blx_reg (struct gdbarch *gdbarch, struct regcache *regs,
5683 arm_displaced_step_copy_insn_closure *dsc, int link,
5684 unsigned int cond, unsigned int rm)
5686 /* Implement {BX,BLX}<cond> <reg>" as:
5688 Preparation: cond <- instruction condition
5689 Insn: mov r0, r0 (nop)
5690 Cleanup: if (condition true) { r14 <- pc; pc <- dest; }.
5692 Don't set r14 in cleanup for BX. */
5694 dsc->u.branch.dest = displaced_read_reg (regs, dsc, rm);
5696 dsc->u.branch.cond = cond;
5697 dsc->u.branch.link = link;
5699 dsc->u.branch.exchange = 1;
5701 dsc->cleanup = &cleanup_branch;
5704 static int
5705 arm_copy_bx_blx_reg (struct gdbarch *gdbarch, uint32_t insn,
5706 regcache *regs, arm_displaced_step_copy_insn_closure *dsc)
5708 unsigned int cond = bits (insn, 28, 31);
5709 /* BX: x12xxx1x
5710 BLX: x12xxx3x. */
5711 int link = bit (insn, 5);
5712 unsigned int rm = bits (insn, 0, 3);
5714 displaced_debug_printf ("copying insn %.8lx", (unsigned long) insn);
5716 dsc->modinsn[0] = ARM_NOP;
5718 install_bx_blx_reg (gdbarch, regs, dsc, link, cond, rm);
5719 return 0;
5722 static int
5723 thumb_copy_bx_blx_reg (struct gdbarch *gdbarch, uint16_t insn,
5724 struct regcache *regs,
5725 arm_displaced_step_copy_insn_closure *dsc)
5727 int link = bit (insn, 7);
5728 unsigned int rm = bits (insn, 3, 6);
5730 displaced_debug_printf ("copying insn %.4x", (unsigned short) insn);
5732 dsc->modinsn[0] = THUMB_NOP;
5734 install_bx_blx_reg (gdbarch, regs, dsc, link, INST_AL, rm);
5736 return 0;
5740 /* Copy/cleanup arithmetic/logic instruction with immediate RHS. */
5742 static void
5743 cleanup_alu_imm (struct gdbarch *gdbarch,
5744 regcache *regs, arm_displaced_step_copy_insn_closure *dsc)
5746 ULONGEST rd_val = displaced_read_reg (regs, dsc, 0);
5747 displaced_write_reg (regs, dsc, 0, dsc->tmp[0], CANNOT_WRITE_PC);
5748 displaced_write_reg (regs, dsc, 1, dsc->tmp[1], CANNOT_WRITE_PC);
5749 displaced_write_reg (regs, dsc, dsc->rd, rd_val, ALU_WRITE_PC);
5752 static int
5753 arm_copy_alu_imm (struct gdbarch *gdbarch, uint32_t insn, struct regcache *regs,
5754 arm_displaced_step_copy_insn_closure *dsc)
5756 unsigned int rn = bits (insn, 16, 19);
5757 unsigned int rd = bits (insn, 12, 15);
5758 unsigned int op = bits (insn, 21, 24);
5759 int is_mov = (op == 0xd);
5760 ULONGEST rd_val, rn_val;
5762 if (!insn_references_pc (insn, 0x000ff000ul))
5763 return arm_copy_unmodified (gdbarch, insn, "ALU immediate", dsc);
5765 displaced_debug_printf ("copying immediate %s insn %.8lx",
5766 is_mov ? "move" : "ALU",
5767 (unsigned long) insn);
5769 /* Instruction is of form:
5771 <op><cond> rd, [rn,] #imm
5773 Rewrite as:
5775 Preparation: tmp1, tmp2 <- r0, r1;
5776 r0, r1 <- rd, rn
5777 Insn: <op><cond> r0, r1, #imm
5778 Cleanup: rd <- r0; r0 <- tmp1; r1 <- tmp2
5781 dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
5782 dsc->tmp[1] = displaced_read_reg (regs, dsc, 1);
5783 rn_val = displaced_read_reg (regs, dsc, rn);
5784 rd_val = displaced_read_reg (regs, dsc, rd);
5785 displaced_write_reg (regs, dsc, 0, rd_val, CANNOT_WRITE_PC);
5786 displaced_write_reg (regs, dsc, 1, rn_val, CANNOT_WRITE_PC);
5787 dsc->rd = rd;
5789 if (is_mov)
5790 dsc->modinsn[0] = insn & 0xfff00fff;
5791 else
5792 dsc->modinsn[0] = (insn & 0xfff00fff) | 0x10000;
5794 dsc->cleanup = &cleanup_alu_imm;
5796 return 0;
5799 static int
5800 thumb2_copy_alu_imm (struct gdbarch *gdbarch, uint16_t insn1,
5801 uint16_t insn2, struct regcache *regs,
5802 arm_displaced_step_copy_insn_closure *dsc)
5804 unsigned int op = bits (insn1, 5, 8);
5805 unsigned int rn, rm, rd;
5806 ULONGEST rd_val, rn_val;
5808 rn = bits (insn1, 0, 3); /* Rn */
5809 rm = bits (insn2, 0, 3); /* Rm */
5810 rd = bits (insn2, 8, 11); /* Rd */
5812 /* This routine is only called for instruction MOV. */
5813 gdb_assert (op == 0x2 && rn == 0xf);
5815 if (rm != ARM_PC_REGNUM && rd != ARM_PC_REGNUM)
5816 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2, "ALU imm", dsc);
5818 displaced_debug_printf ("copying reg %s insn %.4x%.4x", "ALU", insn1, insn2);
5820 /* Instruction is of form:
5822 <op><cond> rd, [rn,] #imm
5824 Rewrite as:
5826 Preparation: tmp1, tmp2 <- r0, r1;
5827 r0, r1 <- rd, rn
5828 Insn: <op><cond> r0, r1, #imm
5829 Cleanup: rd <- r0; r0 <- tmp1; r1 <- tmp2
5832 dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
5833 dsc->tmp[1] = displaced_read_reg (regs, dsc, 1);
5834 rn_val = displaced_read_reg (regs, dsc, rn);
5835 rd_val = displaced_read_reg (regs, dsc, rd);
5836 displaced_write_reg (regs, dsc, 0, rd_val, CANNOT_WRITE_PC);
5837 displaced_write_reg (regs, dsc, 1, rn_val, CANNOT_WRITE_PC);
5838 dsc->rd = rd;
5840 dsc->modinsn[0] = insn1;
5841 dsc->modinsn[1] = ((insn2 & 0xf0f0) | 0x1);
5842 dsc->numinsns = 2;
5844 dsc->cleanup = &cleanup_alu_imm;
5846 return 0;
5849 /* Copy/cleanup arithmetic/logic insns with register RHS. */
5851 static void
5852 cleanup_alu_reg (struct gdbarch *gdbarch,
5853 regcache *regs, arm_displaced_step_copy_insn_closure *dsc)
5855 ULONGEST rd_val;
5856 int i;
5858 rd_val = displaced_read_reg (regs, dsc, 0);
5860 for (i = 0; i < 3; i++)
5861 displaced_write_reg (regs, dsc, i, dsc->tmp[i], CANNOT_WRITE_PC);
5863 displaced_write_reg (regs, dsc, dsc->rd, rd_val, ALU_WRITE_PC);
5866 static void
5867 install_alu_reg (struct gdbarch *gdbarch, struct regcache *regs,
5868 arm_displaced_step_copy_insn_closure *dsc,
5869 unsigned int rd, unsigned int rn, unsigned int rm)
5871 ULONGEST rd_val, rn_val, rm_val;
5873 /* Instruction is of form:
5875 <op><cond> rd, [rn,] rm [, <shift>]
5877 Rewrite as:
5879 Preparation: tmp1, tmp2, tmp3 <- r0, r1, r2;
5880 r0, r1, r2 <- rd, rn, rm
5881 Insn: <op><cond> r0, [r1,] r2 [, <shift>]
5882 Cleanup: rd <- r0; r0, r1, r2 <- tmp1, tmp2, tmp3
5885 dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
5886 dsc->tmp[1] = displaced_read_reg (regs, dsc, 1);
5887 dsc->tmp[2] = displaced_read_reg (regs, dsc, 2);
5888 rd_val = displaced_read_reg (regs, dsc, rd);
5889 rn_val = displaced_read_reg (regs, dsc, rn);
5890 rm_val = displaced_read_reg (regs, dsc, rm);
5891 displaced_write_reg (regs, dsc, 0, rd_val, CANNOT_WRITE_PC);
5892 displaced_write_reg (regs, dsc, 1, rn_val, CANNOT_WRITE_PC);
5893 displaced_write_reg (regs, dsc, 2, rm_val, CANNOT_WRITE_PC);
5894 dsc->rd = rd;
5896 dsc->cleanup = &cleanup_alu_reg;
5899 static int
5900 arm_copy_alu_reg (struct gdbarch *gdbarch, uint32_t insn, struct regcache *regs,
5901 arm_displaced_step_copy_insn_closure *dsc)
5903 unsigned int op = bits (insn, 21, 24);
5904 int is_mov = (op == 0xd);
5906 if (!insn_references_pc (insn, 0x000ff00ful))
5907 return arm_copy_unmodified (gdbarch, insn, "ALU reg", dsc);
5909 displaced_debug_printf ("copying reg %s insn %.8lx",
5910 is_mov ? "move" : "ALU", (unsigned long) insn);
5912 if (is_mov)
5913 dsc->modinsn[0] = (insn & 0xfff00ff0) | 0x2;
5914 else
5915 dsc->modinsn[0] = (insn & 0xfff00ff0) | 0x10002;
5917 install_alu_reg (gdbarch, regs, dsc, bits (insn, 12, 15), bits (insn, 16, 19),
5918 bits (insn, 0, 3));
5919 return 0;
5922 static int
5923 thumb_copy_alu_reg (struct gdbarch *gdbarch, uint16_t insn,
5924 struct regcache *regs,
5925 arm_displaced_step_copy_insn_closure *dsc)
5927 unsigned rm, rd;
5929 rm = bits (insn, 3, 6);
5930 rd = (bit (insn, 7) << 3) | bits (insn, 0, 2);
5932 if (rd != ARM_PC_REGNUM && rm != ARM_PC_REGNUM)
5933 return thumb_copy_unmodified_16bit (gdbarch, insn, "ALU reg", dsc);
5935 displaced_debug_printf ("copying ALU reg insn %.4x", (unsigned short) insn);
5937 dsc->modinsn[0] = ((insn & 0xff00) | 0x10);
5939 install_alu_reg (gdbarch, regs, dsc, rd, rd, rm);
5941 return 0;
5944 /* Cleanup/copy arithmetic/logic insns with shifted register RHS. */
5946 static void
5947 cleanup_alu_shifted_reg (struct gdbarch *gdbarch,
5948 struct regcache *regs,
5949 arm_displaced_step_copy_insn_closure *dsc)
5951 ULONGEST rd_val = displaced_read_reg (regs, dsc, 0);
5952 int i;
5954 for (i = 0; i < 4; i++)
5955 displaced_write_reg (regs, dsc, i, dsc->tmp[i], CANNOT_WRITE_PC);
5957 displaced_write_reg (regs, dsc, dsc->rd, rd_val, ALU_WRITE_PC);
5960 static void
5961 install_alu_shifted_reg (struct gdbarch *gdbarch, struct regcache *regs,
5962 arm_displaced_step_copy_insn_closure *dsc,
5963 unsigned int rd, unsigned int rn, unsigned int rm,
5964 unsigned rs)
5966 int i;
5967 ULONGEST rd_val, rn_val, rm_val, rs_val;
5969 /* Instruction is of form:
5971 <op><cond> rd, [rn,] rm, <shift> rs
5973 Rewrite as:
5975 Preparation: tmp1, tmp2, tmp3, tmp4 <- r0, r1, r2, r3
5976 r0, r1, r2, r3 <- rd, rn, rm, rs
5977 Insn: <op><cond> r0, r1, r2, <shift> r3
5978 Cleanup: tmp5 <- r0
5979 r0, r1, r2, r3 <- tmp1, tmp2, tmp3, tmp4
5980 rd <- tmp5
5983 for (i = 0; i < 4; i++)
5984 dsc->tmp[i] = displaced_read_reg (regs, dsc, i);
5986 rd_val = displaced_read_reg (regs, dsc, rd);
5987 rn_val = displaced_read_reg (regs, dsc, rn);
5988 rm_val = displaced_read_reg (regs, dsc, rm);
5989 rs_val = displaced_read_reg (regs, dsc, rs);
5990 displaced_write_reg (regs, dsc, 0, rd_val, CANNOT_WRITE_PC);
5991 displaced_write_reg (regs, dsc, 1, rn_val, CANNOT_WRITE_PC);
5992 displaced_write_reg (regs, dsc, 2, rm_val, CANNOT_WRITE_PC);
5993 displaced_write_reg (regs, dsc, 3, rs_val, CANNOT_WRITE_PC);
5994 dsc->rd = rd;
5995 dsc->cleanup = &cleanup_alu_shifted_reg;
5998 static int
5999 arm_copy_alu_shifted_reg (struct gdbarch *gdbarch, uint32_t insn,
6000 struct regcache *regs,
6001 arm_displaced_step_copy_insn_closure *dsc)
6003 unsigned int op = bits (insn, 21, 24);
6004 int is_mov = (op == 0xd);
6005 unsigned int rd, rn, rm, rs;
6007 if (!insn_references_pc (insn, 0x000fff0ful))
6008 return arm_copy_unmodified (gdbarch, insn, "ALU shifted reg", dsc);
6010 displaced_debug_printf ("copying shifted reg %s insn %.8lx",
6011 is_mov ? "move" : "ALU",
6012 (unsigned long) insn);
6014 rn = bits (insn, 16, 19);
6015 rm = bits (insn, 0, 3);
6016 rs = bits (insn, 8, 11);
6017 rd = bits (insn, 12, 15);
6019 if (is_mov)
6020 dsc->modinsn[0] = (insn & 0xfff000f0) | 0x302;
6021 else
6022 dsc->modinsn[0] = (insn & 0xfff000f0) | 0x10302;
6024 install_alu_shifted_reg (gdbarch, regs, dsc, rd, rn, rm, rs);
6026 return 0;
6029 /* Clean up load instructions. */
6031 static void
6032 cleanup_load (struct gdbarch *gdbarch, struct regcache *regs,
6033 arm_displaced_step_copy_insn_closure *dsc)
6035 ULONGEST rt_val, rt_val2 = 0, rn_val;
6037 rt_val = displaced_read_reg (regs, dsc, 0);
6038 if (dsc->u.ldst.xfersize == 8)
6039 rt_val2 = displaced_read_reg (regs, dsc, 1);
6040 rn_val = displaced_read_reg (regs, dsc, 2);
6042 displaced_write_reg (regs, dsc, 0, dsc->tmp[0], CANNOT_WRITE_PC);
6043 if (dsc->u.ldst.xfersize > 4)
6044 displaced_write_reg (regs, dsc, 1, dsc->tmp[1], CANNOT_WRITE_PC);
6045 displaced_write_reg (regs, dsc, 2, dsc->tmp[2], CANNOT_WRITE_PC);
6046 if (!dsc->u.ldst.immed)
6047 displaced_write_reg (regs, dsc, 3, dsc->tmp[3], CANNOT_WRITE_PC);
6049 /* Handle register writeback. */
6050 if (dsc->u.ldst.writeback)
6051 displaced_write_reg (regs, dsc, dsc->u.ldst.rn, rn_val, CANNOT_WRITE_PC);
6052 /* Put result in right place. */
6053 displaced_write_reg (regs, dsc, dsc->rd, rt_val, LOAD_WRITE_PC);
6054 if (dsc->u.ldst.xfersize == 8)
6055 displaced_write_reg (regs, dsc, dsc->rd + 1, rt_val2, LOAD_WRITE_PC);
6058 /* Clean up store instructions. */
6060 static void
6061 cleanup_store (struct gdbarch *gdbarch, struct regcache *regs,
6062 arm_displaced_step_copy_insn_closure *dsc)
6064 ULONGEST rn_val = displaced_read_reg (regs, dsc, 2);
6066 displaced_write_reg (regs, dsc, 0, dsc->tmp[0], CANNOT_WRITE_PC);
6067 if (dsc->u.ldst.xfersize > 4)
6068 displaced_write_reg (regs, dsc, 1, dsc->tmp[1], CANNOT_WRITE_PC);
6069 displaced_write_reg (regs, dsc, 2, dsc->tmp[2], CANNOT_WRITE_PC);
6070 if (!dsc->u.ldst.immed)
6071 displaced_write_reg (regs, dsc, 3, dsc->tmp[3], CANNOT_WRITE_PC);
6072 if (!dsc->u.ldst.restore_r4)
6073 displaced_write_reg (regs, dsc, 4, dsc->tmp[4], CANNOT_WRITE_PC);
6075 /* Writeback. */
6076 if (dsc->u.ldst.writeback)
6077 displaced_write_reg (regs, dsc, dsc->u.ldst.rn, rn_val, CANNOT_WRITE_PC);
6080 /* Copy "extra" load/store instructions. These are halfword/doubleword
6081 transfers, which have a different encoding to byte/word transfers. */
6083 static int
6084 arm_copy_extra_ld_st (struct gdbarch *gdbarch, uint32_t insn, int unprivileged,
6085 regcache *regs, arm_displaced_step_copy_insn_closure *dsc)
6087 unsigned int op1 = bits (insn, 20, 24);
6088 unsigned int op2 = bits (insn, 5, 6);
6089 unsigned int rt = bits (insn, 12, 15);
6090 unsigned int rn = bits (insn, 16, 19);
6091 unsigned int rm = bits (insn, 0, 3);
6092 char load[12] = {0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1};
6093 char bytesize[12] = {2, 2, 2, 2, 8, 1, 8, 1, 8, 2, 8, 2};
6094 int immed = (op1 & 0x4) != 0;
6095 int opcode;
6096 ULONGEST rt_val, rt_val2 = 0, rn_val, rm_val = 0;
6098 if (!insn_references_pc (insn, 0x000ff00ful))
6099 return arm_copy_unmodified (gdbarch, insn, "extra load/store", dsc);
6101 displaced_debug_printf ("copying %sextra load/store insn %.8lx",
6102 unprivileged ? "unprivileged " : "",
6103 (unsigned long) insn);
6105 opcode = ((op2 << 2) | (op1 & 0x1) | ((op1 & 0x4) >> 1)) - 4;
6107 if (opcode < 0)
6108 internal_error (__FILE__, __LINE__,
6109 _("copy_extra_ld_st: instruction decode error"));
6111 dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
6112 dsc->tmp[1] = displaced_read_reg (regs, dsc, 1);
6113 dsc->tmp[2] = displaced_read_reg (regs, dsc, 2);
6114 if (!immed)
6115 dsc->tmp[3] = displaced_read_reg (regs, dsc, 3);
6117 rt_val = displaced_read_reg (regs, dsc, rt);
6118 if (bytesize[opcode] == 8)
6119 rt_val2 = displaced_read_reg (regs, dsc, rt + 1);
6120 rn_val = displaced_read_reg (regs, dsc, rn);
6121 if (!immed)
6122 rm_val = displaced_read_reg (regs, dsc, rm);
6124 displaced_write_reg (regs, dsc, 0, rt_val, CANNOT_WRITE_PC);
6125 if (bytesize[opcode] == 8)
6126 displaced_write_reg (regs, dsc, 1, rt_val2, CANNOT_WRITE_PC);
6127 displaced_write_reg (regs, dsc, 2, rn_val, CANNOT_WRITE_PC);
6128 if (!immed)
6129 displaced_write_reg (regs, dsc, 3, rm_val, CANNOT_WRITE_PC);
6131 dsc->rd = rt;
6132 dsc->u.ldst.xfersize = bytesize[opcode];
6133 dsc->u.ldst.rn = rn;
6134 dsc->u.ldst.immed = immed;
6135 dsc->u.ldst.writeback = bit (insn, 24) == 0 || bit (insn, 21) != 0;
6136 dsc->u.ldst.restore_r4 = 0;
6138 if (immed)
6139 /* {ldr,str}<width><cond> rt, [rt2,] [rn, #imm]
6141 {ldr,str}<width><cond> r0, [r1,] [r2, #imm]. */
6142 dsc->modinsn[0] = (insn & 0xfff00fff) | 0x20000;
6143 else
6144 /* {ldr,str}<width><cond> rt, [rt2,] [rn, +/-rm]
6146 {ldr,str}<width><cond> r0, [r1,] [r2, +/-r3]. */
6147 dsc->modinsn[0] = (insn & 0xfff00ff0) | 0x20003;
6149 dsc->cleanup = load[opcode] ? &cleanup_load : &cleanup_store;
6151 return 0;
6154 /* Copy byte/half word/word loads and stores. */
6156 static void
6157 install_load_store (struct gdbarch *gdbarch, struct regcache *regs,
6158 arm_displaced_step_copy_insn_closure *dsc, int load,
6159 int immed, int writeback, int size, int usermode,
6160 int rt, int rm, int rn)
6162 ULONGEST rt_val, rn_val, rm_val = 0;
6164 dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
6165 dsc->tmp[2] = displaced_read_reg (regs, dsc, 2);
6166 if (!immed)
6167 dsc->tmp[3] = displaced_read_reg (regs, dsc, 3);
6168 if (!load)
6169 dsc->tmp[4] = displaced_read_reg (regs, dsc, 4);
6171 rt_val = displaced_read_reg (regs, dsc, rt);
6172 rn_val = displaced_read_reg (regs, dsc, rn);
6173 if (!immed)
6174 rm_val = displaced_read_reg (regs, dsc, rm);
6176 displaced_write_reg (regs, dsc, 0, rt_val, CANNOT_WRITE_PC);
6177 displaced_write_reg (regs, dsc, 2, rn_val, CANNOT_WRITE_PC);
6178 if (!immed)
6179 displaced_write_reg (regs, dsc, 3, rm_val, CANNOT_WRITE_PC);
6180 dsc->rd = rt;
6181 dsc->u.ldst.xfersize = size;
6182 dsc->u.ldst.rn = rn;
6183 dsc->u.ldst.immed = immed;
6184 dsc->u.ldst.writeback = writeback;
6186 /* To write PC we can do:
6188 Before this sequence of instructions:
6189 r0 is the PC value got from displaced_read_reg, so r0 = from + 8;
6190 r2 is the Rn value got from displaced_read_reg.
6192 Insn1: push {pc} Write address of STR instruction + offset on stack
6193 Insn2: pop {r4} Read it back from stack, r4 = addr(Insn1) + offset
6194 Insn3: sub r4, r4, pc r4 = addr(Insn1) + offset - pc
6195 = addr(Insn1) + offset - addr(Insn3) - 8
6196 = offset - 16
6197 Insn4: add r4, r4, #8 r4 = offset - 8
6198 Insn5: add r0, r0, r4 r0 = from + 8 + offset - 8
6199 = from + offset
6200 Insn6: str r0, [r2, #imm] (or str r0, [r2, r3])
6202 Otherwise we don't know what value to write for PC, since the offset is
6203 architecture-dependent (sometimes PC+8, sometimes PC+12). More details
6204 of this can be found in Section "Saving from r15" in
6205 http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dui0204g/Cihbjifh.html */
6207 dsc->cleanup = load ? &cleanup_load : &cleanup_store;
6211 static int
6212 thumb2_copy_load_literal (struct gdbarch *gdbarch, uint16_t insn1,
6213 uint16_t insn2, struct regcache *regs,
6214 arm_displaced_step_copy_insn_closure *dsc, int size)
6216 unsigned int u_bit = bit (insn1, 7);
6217 unsigned int rt = bits (insn2, 12, 15);
6218 int imm12 = bits (insn2, 0, 11);
6219 ULONGEST pc_val;
6221 displaced_debug_printf ("copying ldr pc (0x%x) R%d %c imm12 %.4x",
6222 (unsigned int) dsc->insn_addr, rt, u_bit ? '+' : '-',
6223 imm12);
6225 if (!u_bit)
6226 imm12 = -1 * imm12;
6228 /* Rewrite instruction LDR Rt imm12 into:
6230 Prepare: tmp[0] <- r0, tmp[1] <- r2, tmp[2] <- r3, r2 <- pc, r3 <- imm12
6232 LDR R0, R2, R3,
6234 Cleanup: rt <- r0, r0 <- tmp[0], r2 <- tmp[1], r3 <- tmp[2]. */
6237 dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
6238 dsc->tmp[2] = displaced_read_reg (regs, dsc, 2);
6239 dsc->tmp[3] = displaced_read_reg (regs, dsc, 3);
6241 pc_val = displaced_read_reg (regs, dsc, ARM_PC_REGNUM);
6243 pc_val = pc_val & 0xfffffffc;
6245 displaced_write_reg (regs, dsc, 2, pc_val, CANNOT_WRITE_PC);
6246 displaced_write_reg (regs, dsc, 3, imm12, CANNOT_WRITE_PC);
6248 dsc->rd = rt;
6250 dsc->u.ldst.xfersize = size;
6251 dsc->u.ldst.immed = 0;
6252 dsc->u.ldst.writeback = 0;
6253 dsc->u.ldst.restore_r4 = 0;
6255 /* LDR R0, R2, R3 */
6256 dsc->modinsn[0] = 0xf852;
6257 dsc->modinsn[1] = 0x3;
6258 dsc->numinsns = 2;
6260 dsc->cleanup = &cleanup_load;
6262 return 0;
6265 static int
6266 thumb2_copy_load_reg_imm (struct gdbarch *gdbarch, uint16_t insn1,
6267 uint16_t insn2, struct regcache *regs,
6268 arm_displaced_step_copy_insn_closure *dsc,
6269 int writeback, int immed)
6271 unsigned int rt = bits (insn2, 12, 15);
6272 unsigned int rn = bits (insn1, 0, 3);
6273 unsigned int rm = bits (insn2, 0, 3); /* Only valid if !immed. */
6274 /* In LDR (register), there is also a register Rm, which is not allowed to
6275 be PC, so we don't have to check it. */
6277 if (rt != ARM_PC_REGNUM && rn != ARM_PC_REGNUM)
6278 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2, "load",
6279 dsc);
6281 displaced_debug_printf ("copying ldr r%d [r%d] insn %.4x%.4x",
6282 rt, rn, insn1, insn2);
6284 install_load_store (gdbarch, regs, dsc, 1, immed, writeback, 4,
6285 0, rt, rm, rn);
6287 dsc->u.ldst.restore_r4 = 0;
6289 if (immed)
6290 /* ldr[b]<cond> rt, [rn, #imm], etc.
6292 ldr[b]<cond> r0, [r2, #imm]. */
6294 dsc->modinsn[0] = (insn1 & 0xfff0) | 0x2;
6295 dsc->modinsn[1] = insn2 & 0x0fff;
6297 else
6298 /* ldr[b]<cond> rt, [rn, rm], etc.
6300 ldr[b]<cond> r0, [r2, r3]. */
6302 dsc->modinsn[0] = (insn1 & 0xfff0) | 0x2;
6303 dsc->modinsn[1] = (insn2 & 0x0ff0) | 0x3;
6306 dsc->numinsns = 2;
6308 return 0;
6312 static int
6313 arm_copy_ldr_str_ldrb_strb (struct gdbarch *gdbarch, uint32_t insn,
6314 struct regcache *regs,
6315 arm_displaced_step_copy_insn_closure *dsc,
6316 int load, int size, int usermode)
6318 int immed = !bit (insn, 25);
6319 int writeback = (bit (insn, 24) == 0 || bit (insn, 21) != 0);
6320 unsigned int rt = bits (insn, 12, 15);
6321 unsigned int rn = bits (insn, 16, 19);
6322 unsigned int rm = bits (insn, 0, 3); /* Only valid if !immed. */
6324 if (!insn_references_pc (insn, 0x000ff00ful))
6325 return arm_copy_unmodified (gdbarch, insn, "load/store", dsc);
6327 displaced_debug_printf ("copying %s%s r%d [r%d] insn %.8lx",
6328 load ? (size == 1 ? "ldrb" : "ldr")
6329 : (size == 1 ? "strb" : "str"),
6330 usermode ? "t" : "",
6331 rt, rn,
6332 (unsigned long) insn);
6334 install_load_store (gdbarch, regs, dsc, load, immed, writeback, size,
6335 usermode, rt, rm, rn);
6337 if (load || rt != ARM_PC_REGNUM)
6339 dsc->u.ldst.restore_r4 = 0;
6341 if (immed)
6342 /* {ldr,str}[b]<cond> rt, [rn, #imm], etc.
6344 {ldr,str}[b]<cond> r0, [r2, #imm]. */
6345 dsc->modinsn[0] = (insn & 0xfff00fff) | 0x20000;
6346 else
6347 /* {ldr,str}[b]<cond> rt, [rn, rm], etc.
6349 {ldr,str}[b]<cond> r0, [r2, r3]. */
6350 dsc->modinsn[0] = (insn & 0xfff00ff0) | 0x20003;
6352 else
6354 /* We need to use r4 as scratch. Make sure it's restored afterwards. */
6355 dsc->u.ldst.restore_r4 = 1;
6356 dsc->modinsn[0] = 0xe92d8000; /* push {pc} */
6357 dsc->modinsn[1] = 0xe8bd0010; /* pop {r4} */
6358 dsc->modinsn[2] = 0xe044400f; /* sub r4, r4, pc. */
6359 dsc->modinsn[3] = 0xe2844008; /* add r4, r4, #8. */
6360 dsc->modinsn[4] = 0xe0800004; /* add r0, r0, r4. */
6362 /* As above. */
6363 if (immed)
6364 dsc->modinsn[5] = (insn & 0xfff00fff) | 0x20000;
6365 else
6366 dsc->modinsn[5] = (insn & 0xfff00ff0) | 0x20003;
6368 dsc->numinsns = 6;
6371 dsc->cleanup = load ? &cleanup_load : &cleanup_store;
6373 return 0;
6376 /* Cleanup LDM instructions with fully-populated register list. This is an
6377 unfortunate corner case: it's impossible to implement correctly by modifying
6378 the instruction. The issue is as follows: we have an instruction,
6380 ldm rN, {r0-r15}
6382 which we must rewrite to avoid loading PC. A possible solution would be to
6383 do the load in two halves, something like (with suitable cleanup
6384 afterwards):
6386 mov r8, rN
6387 ldm[id][ab] r8!, {r0-r7}
6388 str r7, <temp>
6389 ldm[id][ab] r8, {r7-r14}
6390 <bkpt>
6392 but at present there's no suitable place for <temp>, since the scratch space
6393 is overwritten before the cleanup routine is called. For now, we simply
6394 emulate the instruction. */
6396 static void
6397 cleanup_block_load_all (struct gdbarch *gdbarch, struct regcache *regs,
6398 arm_displaced_step_copy_insn_closure *dsc)
6400 int inc = dsc->u.block.increment;
6401 int bump_before = dsc->u.block.before ? (inc ? 4 : -4) : 0;
6402 int bump_after = dsc->u.block.before ? 0 : (inc ? 4 : -4);
6403 uint32_t regmask = dsc->u.block.regmask;
6404 int regno = inc ? 0 : 15;
6405 CORE_ADDR xfer_addr = dsc->u.block.xfer_addr;
6406 int exception_return = dsc->u.block.load && dsc->u.block.user
6407 && (regmask & 0x8000) != 0;
6408 uint32_t status = displaced_read_reg (regs, dsc, ARM_PS_REGNUM);
6409 int do_transfer = condition_true (dsc->u.block.cond, status);
6410 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
6412 if (!do_transfer)
6413 return;
6415 /* If the instruction is ldm rN, {...pc}^, I don't think there's anything
6416 sensible we can do here. Complain loudly. */
6417 if (exception_return)
6418 error (_("Cannot single-step exception return"));
6420 /* We don't handle any stores here for now. */
6421 gdb_assert (dsc->u.block.load != 0);
6423 displaced_debug_printf ("emulating block transfer: %s %s %s",
6424 dsc->u.block.load ? "ldm" : "stm",
6425 dsc->u.block.increment ? "inc" : "dec",
6426 dsc->u.block.before ? "before" : "after");
6428 while (regmask)
6430 uint32_t memword;
6432 if (inc)
6433 while (regno <= ARM_PC_REGNUM && (regmask & (1 << regno)) == 0)
6434 regno++;
6435 else
6436 while (regno >= 0 && (regmask & (1 << regno)) == 0)
6437 regno--;
6439 xfer_addr += bump_before;
6441 memword = read_memory_unsigned_integer (xfer_addr, 4, byte_order);
6442 displaced_write_reg (regs, dsc, regno, memword, LOAD_WRITE_PC);
6444 xfer_addr += bump_after;
6446 regmask &= ~(1 << regno);
6449 if (dsc->u.block.writeback)
6450 displaced_write_reg (regs, dsc, dsc->u.block.rn, xfer_addr,
6451 CANNOT_WRITE_PC);
6454 /* Clean up an STM which included the PC in the register list. */
6456 static void
6457 cleanup_block_store_pc (struct gdbarch *gdbarch, struct regcache *regs,
6458 arm_displaced_step_copy_insn_closure *dsc)
6460 uint32_t status = displaced_read_reg (regs, dsc, ARM_PS_REGNUM);
6461 int store_executed = condition_true (dsc->u.block.cond, status);
6462 CORE_ADDR pc_stored_at, transferred_regs
6463 = count_one_bits (dsc->u.block.regmask);
6464 CORE_ADDR stm_insn_addr;
6465 uint32_t pc_val;
6466 long offset;
6467 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
6469 /* If condition code fails, there's nothing else to do. */
6470 if (!store_executed)
6471 return;
6473 if (dsc->u.block.increment)
6475 pc_stored_at = dsc->u.block.xfer_addr + 4 * transferred_regs;
6477 if (dsc->u.block.before)
6478 pc_stored_at += 4;
6480 else
6482 pc_stored_at = dsc->u.block.xfer_addr;
6484 if (dsc->u.block.before)
6485 pc_stored_at -= 4;
6488 pc_val = read_memory_unsigned_integer (pc_stored_at, 4, byte_order);
6489 stm_insn_addr = dsc->scratch_base;
6490 offset = pc_val - stm_insn_addr;
6492 displaced_debug_printf ("detected PC offset %.8lx for STM instruction",
6493 offset);
6495 /* Rewrite the stored PC to the proper value for the non-displaced original
6496 instruction. */
6497 write_memory_unsigned_integer (pc_stored_at, 4, byte_order,
6498 dsc->insn_addr + offset);
6501 /* Clean up an LDM which includes the PC in the register list. We clumped all
6502 the registers in the transferred list into a contiguous range r0...rX (to
6503 avoid loading PC directly and losing control of the debugged program), so we
6504 must undo that here. */
6506 static void
6507 cleanup_block_load_pc (struct gdbarch *gdbarch,
6508 struct regcache *regs,
6509 arm_displaced_step_copy_insn_closure *dsc)
6511 uint32_t status = displaced_read_reg (regs, dsc, ARM_PS_REGNUM);
6512 int load_executed = condition_true (dsc->u.block.cond, status);
6513 unsigned int mask = dsc->u.block.regmask, write_reg = ARM_PC_REGNUM;
6514 unsigned int regs_loaded = count_one_bits (mask);
6515 unsigned int num_to_shuffle = regs_loaded, clobbered;
6517 /* The method employed here will fail if the register list is fully populated
6518 (we need to avoid loading PC directly). */
6519 gdb_assert (num_to_shuffle < 16);
6521 if (!load_executed)
6522 return;
6524 clobbered = (1 << num_to_shuffle) - 1;
6526 while (num_to_shuffle > 0)
6528 if ((mask & (1 << write_reg)) != 0)
6530 unsigned int read_reg = num_to_shuffle - 1;
6532 if (read_reg != write_reg)
6534 ULONGEST rval = displaced_read_reg (regs, dsc, read_reg);
6535 displaced_write_reg (regs, dsc, write_reg, rval, LOAD_WRITE_PC);
6536 displaced_debug_printf ("LDM: move loaded register r%d to r%d",
6537 read_reg, write_reg);
6539 else
6540 displaced_debug_printf ("LDM: register r%d already in the right "
6541 "place", write_reg);
6543 clobbered &= ~(1 << write_reg);
6545 num_to_shuffle--;
6548 write_reg--;
6551 /* Restore any registers we scribbled over. */
6552 for (write_reg = 0; clobbered != 0; write_reg++)
6554 if ((clobbered & (1 << write_reg)) != 0)
6556 displaced_write_reg (regs, dsc, write_reg, dsc->tmp[write_reg],
6557 CANNOT_WRITE_PC);
6558 displaced_debug_printf ("LDM: restored clobbered register r%d",
6559 write_reg);
6560 clobbered &= ~(1 << write_reg);
6564 /* Perform register writeback manually. */
6565 if (dsc->u.block.writeback)
6567 ULONGEST new_rn_val = dsc->u.block.xfer_addr;
6569 if (dsc->u.block.increment)
6570 new_rn_val += regs_loaded * 4;
6571 else
6572 new_rn_val -= regs_loaded * 4;
6574 displaced_write_reg (regs, dsc, dsc->u.block.rn, new_rn_val,
6575 CANNOT_WRITE_PC);
6579 /* Handle ldm/stm, apart from some tricky cases which are unlikely to occur
6580 in user-level code (in particular exception return, ldm rn, {...pc}^). */
6582 static int
6583 arm_copy_block_xfer (struct gdbarch *gdbarch, uint32_t insn,
6584 struct regcache *regs,
6585 arm_displaced_step_copy_insn_closure *dsc)
6587 int load = bit (insn, 20);
6588 int user = bit (insn, 22);
6589 int increment = bit (insn, 23);
6590 int before = bit (insn, 24);
6591 int writeback = bit (insn, 21);
6592 int rn = bits (insn, 16, 19);
6594 /* Block transfers which don't mention PC can be run directly
6595 out-of-line. */
6596 if (rn != ARM_PC_REGNUM && (insn & 0x8000) == 0)
6597 return arm_copy_unmodified (gdbarch, insn, "ldm/stm", dsc);
6599 if (rn == ARM_PC_REGNUM)
6601 warning (_("displaced: Unpredictable LDM or STM with "
6602 "base register r15"));
6603 return arm_copy_unmodified (gdbarch, insn, "unpredictable ldm/stm", dsc);
6606 displaced_debug_printf ("copying block transfer insn %.8lx",
6607 (unsigned long) insn);
6609 dsc->u.block.xfer_addr = displaced_read_reg (regs, dsc, rn);
6610 dsc->u.block.rn = rn;
6612 dsc->u.block.load = load;
6613 dsc->u.block.user = user;
6614 dsc->u.block.increment = increment;
6615 dsc->u.block.before = before;
6616 dsc->u.block.writeback = writeback;
6617 dsc->u.block.cond = bits (insn, 28, 31);
6619 dsc->u.block.regmask = insn & 0xffff;
6621 if (load)
6623 if ((insn & 0xffff) == 0xffff)
6625 /* LDM with a fully-populated register list. This case is
6626 particularly tricky. Implement for now by fully emulating the
6627 instruction (which might not behave perfectly in all cases, but
6628 these instructions should be rare enough for that not to matter
6629 too much). */
6630 dsc->modinsn[0] = ARM_NOP;
6632 dsc->cleanup = &cleanup_block_load_all;
6634 else
6636 /* LDM of a list of registers which includes PC. Implement by
6637 rewriting the list of registers to be transferred into a
6638 contiguous chunk r0...rX before doing the transfer, then shuffling
6639 registers into the correct places in the cleanup routine. */
6640 unsigned int regmask = insn & 0xffff;
6641 unsigned int num_in_list = count_one_bits (regmask), new_regmask;
6642 unsigned int i;
6644 for (i = 0; i < num_in_list; i++)
6645 dsc->tmp[i] = displaced_read_reg (regs, dsc, i);
6647 /* Writeback makes things complicated. We need to avoid clobbering
6648 the base register with one of the registers in our modified
6649 register list, but just using a different register can't work in
6650 all cases, e.g.:
6652 ldm r14!, {r0-r13,pc}
6654 which would need to be rewritten as:
6656 ldm rN!, {r0-r14}
6658 but that can't work, because there's no free register for N.
6660 Solve this by turning off the writeback bit, and emulating
6661 writeback manually in the cleanup routine. */
6663 if (writeback)
6664 insn &= ~(1 << 21);
6666 new_regmask = (1 << num_in_list) - 1;
6668 displaced_debug_printf ("LDM r%d%s, {..., pc}: original reg list "
6669 "%.4x, modified list %.4x",
6670 rn, writeback ? "!" : "",
6671 (int) insn & 0xffff, new_regmask);
6673 dsc->modinsn[0] = (insn & ~0xffff) | (new_regmask & 0xffff);
6675 dsc->cleanup = &cleanup_block_load_pc;
6678 else
6680 /* STM of a list of registers which includes PC. Run the instruction
6681 as-is, but out of line: this will store the wrong value for the PC,
6682 so we must manually fix up the memory in the cleanup routine.
6683 Doing things this way has the advantage that we can auto-detect
6684 the offset of the PC write (which is architecture-dependent) in
6685 the cleanup routine. */
6686 dsc->modinsn[0] = insn;
6688 dsc->cleanup = &cleanup_block_store_pc;
6691 return 0;
6694 static int
6695 thumb2_copy_block_xfer (struct gdbarch *gdbarch, uint16_t insn1, uint16_t insn2,
6696 struct regcache *regs,
6697 arm_displaced_step_copy_insn_closure *dsc)
6699 int rn = bits (insn1, 0, 3);
6700 int load = bit (insn1, 4);
6701 int writeback = bit (insn1, 5);
6703 /* Block transfers which don't mention PC can be run directly
6704 out-of-line. */
6705 if (rn != ARM_PC_REGNUM && (insn2 & 0x8000) == 0)
6706 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2, "ldm/stm", dsc);
6708 if (rn == ARM_PC_REGNUM)
6710 warning (_("displaced: Unpredictable LDM or STM with "
6711 "base register r15"));
6712 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
6713 "unpredictable ldm/stm", dsc);
6716 displaced_debug_printf ("copying block transfer insn %.4x%.4x",
6717 insn1, insn2);
6719 /* Clear bit 13, since it should be always zero. */
6720 dsc->u.block.regmask = (insn2 & 0xdfff);
6721 dsc->u.block.rn = rn;
6723 dsc->u.block.load = load;
6724 dsc->u.block.user = 0;
6725 dsc->u.block.increment = bit (insn1, 7);
6726 dsc->u.block.before = bit (insn1, 8);
6727 dsc->u.block.writeback = writeback;
6728 dsc->u.block.cond = INST_AL;
6729 dsc->u.block.xfer_addr = displaced_read_reg (regs, dsc, rn);
6731 if (load)
6733 if (dsc->u.block.regmask == 0xffff)
6735 /* This branch is impossible to happen. */
6736 gdb_assert (0);
6738 else
6740 unsigned int regmask = dsc->u.block.regmask;
6741 unsigned int num_in_list = count_one_bits (regmask), new_regmask;
6742 unsigned int i;
6744 for (i = 0; i < num_in_list; i++)
6745 dsc->tmp[i] = displaced_read_reg (regs, dsc, i);
6747 if (writeback)
6748 insn1 &= ~(1 << 5);
6750 new_regmask = (1 << num_in_list) - 1;
6752 displaced_debug_printf ("LDM r%d%s, {..., pc}: original reg list "
6753 "%.4x, modified list %.4x",
6754 rn, writeback ? "!" : "",
6755 (int) dsc->u.block.regmask, new_regmask);
6757 dsc->modinsn[0] = insn1;
6758 dsc->modinsn[1] = (new_regmask & 0xffff);
6759 dsc->numinsns = 2;
6761 dsc->cleanup = &cleanup_block_load_pc;
6764 else
6766 dsc->modinsn[0] = insn1;
6767 dsc->modinsn[1] = insn2;
6768 dsc->numinsns = 2;
6769 dsc->cleanup = &cleanup_block_store_pc;
6771 return 0;
6774 /* Wrapper over read_memory_unsigned_integer for use in arm_get_next_pcs.
6775 This is used to avoid a dependency on BFD's bfd_endian enum. */
6777 ULONGEST
6778 arm_get_next_pcs_read_memory_unsigned_integer (CORE_ADDR memaddr, int len,
6779 int byte_order)
6781 return read_memory_unsigned_integer (memaddr, len,
6782 (enum bfd_endian) byte_order);
6785 /* Wrapper over gdbarch_addr_bits_remove for use in arm_get_next_pcs. */
6787 CORE_ADDR
6788 arm_get_next_pcs_addr_bits_remove (struct arm_get_next_pcs *self,
6789 CORE_ADDR val)
6791 return gdbarch_addr_bits_remove (self->regcache->arch (), val);
6794 /* Wrapper over syscall_next_pc for use in get_next_pcs. */
6796 static CORE_ADDR
6797 arm_get_next_pcs_syscall_next_pc (struct arm_get_next_pcs *self)
6799 return 0;
6802 /* Wrapper over arm_is_thumb for use in arm_get_next_pcs. */
6805 arm_get_next_pcs_is_thumb (struct arm_get_next_pcs *self)
6807 return arm_is_thumb (self->regcache);
6810 /* single_step() is called just before we want to resume the inferior,
6811 if we want to single-step it but there is no hardware or kernel
6812 single-step support. We find the target of the coming instructions
6813 and breakpoint them. */
6815 std::vector<CORE_ADDR>
6816 arm_software_single_step (struct regcache *regcache)
6818 struct gdbarch *gdbarch = regcache->arch ();
6819 struct arm_get_next_pcs next_pcs_ctx;
6821 arm_get_next_pcs_ctor (&next_pcs_ctx,
6822 &arm_get_next_pcs_ops,
6823 gdbarch_byte_order (gdbarch),
6824 gdbarch_byte_order_for_code (gdbarch),
6826 regcache);
6828 std::vector<CORE_ADDR> next_pcs = arm_get_next_pcs (&next_pcs_ctx);
6830 for (CORE_ADDR &pc_ref : next_pcs)
6831 pc_ref = gdbarch_addr_bits_remove (gdbarch, pc_ref);
6833 return next_pcs;
6836 /* Cleanup/copy SVC (SWI) instructions. These two functions are overridden
6837 for Linux, where some SVC instructions must be treated specially. */
6839 static void
6840 cleanup_svc (struct gdbarch *gdbarch, struct regcache *regs,
6841 arm_displaced_step_copy_insn_closure *dsc)
6843 CORE_ADDR resume_addr = dsc->insn_addr + dsc->insn_size;
6845 displaced_debug_printf ("cleanup for svc, resume at %.8lx",
6846 (unsigned long) resume_addr);
6848 displaced_write_reg (regs, dsc, ARM_PC_REGNUM, resume_addr, BRANCH_WRITE_PC);
6852 /* Common copy routine for svc instruction. */
6854 static int
6855 install_svc (struct gdbarch *gdbarch, struct regcache *regs,
6856 arm_displaced_step_copy_insn_closure *dsc)
6858 /* Preparation: none.
6859 Insn: unmodified svc.
6860 Cleanup: pc <- insn_addr + insn_size. */
6862 /* Pretend we wrote to the PC, so cleanup doesn't set PC to the next
6863 instruction. */
6864 dsc->wrote_to_pc = 1;
6866 /* Allow OS-specific code to override SVC handling. */
6867 if (dsc->u.svc.copy_svc_os)
6868 return dsc->u.svc.copy_svc_os (gdbarch, regs, dsc);
6869 else
6871 dsc->cleanup = &cleanup_svc;
6872 return 0;
6876 static int
6877 arm_copy_svc (struct gdbarch *gdbarch, uint32_t insn,
6878 regcache *regs, arm_displaced_step_copy_insn_closure *dsc)
6881 displaced_debug_printf ("copying svc insn %.8lx",
6882 (unsigned long) insn);
6884 dsc->modinsn[0] = insn;
6886 return install_svc (gdbarch, regs, dsc);
6889 static int
6890 thumb_copy_svc (struct gdbarch *gdbarch, uint16_t insn,
6891 regcache *regs, arm_displaced_step_copy_insn_closure *dsc)
6894 displaced_debug_printf ("copying svc insn %.4x", insn);
6896 dsc->modinsn[0] = insn;
6898 return install_svc (gdbarch, regs, dsc);
6901 /* Copy undefined instructions. */
6903 static int
6904 arm_copy_undef (struct gdbarch *gdbarch, uint32_t insn,
6905 arm_displaced_step_copy_insn_closure *dsc)
6907 displaced_debug_printf ("copying undefined insn %.8lx",
6908 (unsigned long) insn);
6910 dsc->modinsn[0] = insn;
6912 return 0;
6915 static int
6916 thumb_32bit_copy_undef (struct gdbarch *gdbarch, uint16_t insn1, uint16_t insn2,
6917 arm_displaced_step_copy_insn_closure *dsc)
6920 displaced_debug_printf ("copying undefined insn %.4x %.4x",
6921 (unsigned short) insn1, (unsigned short) insn2);
6923 dsc->modinsn[0] = insn1;
6924 dsc->modinsn[1] = insn2;
6925 dsc->numinsns = 2;
6927 return 0;
6930 /* Copy unpredictable instructions. */
6932 static int
6933 arm_copy_unpred (struct gdbarch *gdbarch, uint32_t insn,
6934 arm_displaced_step_copy_insn_closure *dsc)
6936 displaced_debug_printf ("copying unpredictable insn %.8lx",
6937 (unsigned long) insn);
6939 dsc->modinsn[0] = insn;
6941 return 0;
6944 /* The decode_* functions are instruction decoding helpers. They mostly follow
6945 the presentation in the ARM ARM. */
6947 static int
6948 arm_decode_misc_memhint_neon (struct gdbarch *gdbarch, uint32_t insn,
6949 struct regcache *regs,
6950 arm_displaced_step_copy_insn_closure *dsc)
6952 unsigned int op1 = bits (insn, 20, 26), op2 = bits (insn, 4, 7);
6953 unsigned int rn = bits (insn, 16, 19);
6955 if (op1 == 0x10 && (op2 & 0x2) == 0x0 && (rn & 0x1) == 0x0)
6956 return arm_copy_unmodified (gdbarch, insn, "cps", dsc);
6957 else if (op1 == 0x10 && op2 == 0x0 && (rn & 0x1) == 0x1)
6958 return arm_copy_unmodified (gdbarch, insn, "setend", dsc);
6959 else if ((op1 & 0x60) == 0x20)
6960 return arm_copy_unmodified (gdbarch, insn, "neon dataproc", dsc);
6961 else if ((op1 & 0x71) == 0x40)
6962 return arm_copy_unmodified (gdbarch, insn, "neon elt/struct load/store",
6963 dsc);
6964 else if ((op1 & 0x77) == 0x41)
6965 return arm_copy_unmodified (gdbarch, insn, "unallocated mem hint", dsc);
6966 else if ((op1 & 0x77) == 0x45)
6967 return arm_copy_preload (gdbarch, insn, regs, dsc); /* pli. */
6968 else if ((op1 & 0x77) == 0x51)
6970 if (rn != 0xf)
6971 return arm_copy_preload (gdbarch, insn, regs, dsc); /* pld/pldw. */
6972 else
6973 return arm_copy_unpred (gdbarch, insn, dsc);
6975 else if ((op1 & 0x77) == 0x55)
6976 return arm_copy_preload (gdbarch, insn, regs, dsc); /* pld/pldw. */
6977 else if (op1 == 0x57)
6978 switch (op2)
6980 case 0x1: return arm_copy_unmodified (gdbarch, insn, "clrex", dsc);
6981 case 0x4: return arm_copy_unmodified (gdbarch, insn, "dsb", dsc);
6982 case 0x5: return arm_copy_unmodified (gdbarch, insn, "dmb", dsc);
6983 case 0x6: return arm_copy_unmodified (gdbarch, insn, "isb", dsc);
6984 default: return arm_copy_unpred (gdbarch, insn, dsc);
6986 else if ((op1 & 0x63) == 0x43)
6987 return arm_copy_unpred (gdbarch, insn, dsc);
6988 else if ((op2 & 0x1) == 0x0)
6989 switch (op1 & ~0x80)
6991 case 0x61:
6992 return arm_copy_unmodified (gdbarch, insn, "unallocated mem hint", dsc);
6993 case 0x65:
6994 return arm_copy_preload_reg (gdbarch, insn, regs, dsc); /* pli reg. */
6995 case 0x71: case 0x75:
6996 /* pld/pldw reg. */
6997 return arm_copy_preload_reg (gdbarch, insn, regs, dsc);
6998 case 0x63: case 0x67: case 0x73: case 0x77:
6999 return arm_copy_unpred (gdbarch, insn, dsc);
7000 default:
7001 return arm_copy_undef (gdbarch, insn, dsc);
7003 else
7004 return arm_copy_undef (gdbarch, insn, dsc); /* Probably unreachable. */
7007 static int
7008 arm_decode_unconditional (struct gdbarch *gdbarch, uint32_t insn,
7009 struct regcache *regs,
7010 arm_displaced_step_copy_insn_closure *dsc)
7012 if (bit (insn, 27) == 0)
7013 return arm_decode_misc_memhint_neon (gdbarch, insn, regs, dsc);
7014 /* Switch on bits: 0bxxxxx321xxx0xxxxxxxxxxxxxxxxxxxx. */
7015 else switch (((insn & 0x7000000) >> 23) | ((insn & 0x100000) >> 20))
7017 case 0x0: case 0x2:
7018 return arm_copy_unmodified (gdbarch, insn, "srs", dsc);
7020 case 0x1: case 0x3:
7021 return arm_copy_unmodified (gdbarch, insn, "rfe", dsc);
7023 case 0x4: case 0x5: case 0x6: case 0x7:
7024 return arm_copy_b_bl_blx (gdbarch, insn, regs, dsc);
7026 case 0x8:
7027 switch ((insn & 0xe00000) >> 21)
7029 case 0x1: case 0x3: case 0x4: case 0x5: case 0x6: case 0x7:
7030 /* stc/stc2. */
7031 return arm_copy_copro_load_store (gdbarch, insn, regs, dsc);
7033 case 0x2:
7034 return arm_copy_unmodified (gdbarch, insn, "mcrr/mcrr2", dsc);
7036 default:
7037 return arm_copy_undef (gdbarch, insn, dsc);
7040 case 0x9:
7042 int rn_f = (bits (insn, 16, 19) == 0xf);
7043 switch ((insn & 0xe00000) >> 21)
7045 case 0x1: case 0x3:
7046 /* ldc/ldc2 imm (undefined for rn == pc). */
7047 return rn_f ? arm_copy_undef (gdbarch, insn, dsc)
7048 : arm_copy_copro_load_store (gdbarch, insn, regs, dsc);
7050 case 0x2:
7051 return arm_copy_unmodified (gdbarch, insn, "mrrc/mrrc2", dsc);
7053 case 0x4: case 0x5: case 0x6: case 0x7:
7054 /* ldc/ldc2 lit (undefined for rn != pc). */
7055 return rn_f ? arm_copy_copro_load_store (gdbarch, insn, regs, dsc)
7056 : arm_copy_undef (gdbarch, insn, dsc);
7058 default:
7059 return arm_copy_undef (gdbarch, insn, dsc);
7063 case 0xa:
7064 return arm_copy_unmodified (gdbarch, insn, "stc/stc2", dsc);
7066 case 0xb:
7067 if (bits (insn, 16, 19) == 0xf)
7068 /* ldc/ldc2 lit. */
7069 return arm_copy_copro_load_store (gdbarch, insn, regs, dsc);
7070 else
7071 return arm_copy_undef (gdbarch, insn, dsc);
7073 case 0xc:
7074 if (bit (insn, 4))
7075 return arm_copy_unmodified (gdbarch, insn, "mcr/mcr2", dsc);
7076 else
7077 return arm_copy_unmodified (gdbarch, insn, "cdp/cdp2", dsc);
7079 case 0xd:
7080 if (bit (insn, 4))
7081 return arm_copy_unmodified (gdbarch, insn, "mrc/mrc2", dsc);
7082 else
7083 return arm_copy_unmodified (gdbarch, insn, "cdp/cdp2", dsc);
7085 default:
7086 return arm_copy_undef (gdbarch, insn, dsc);
7090 /* Decode miscellaneous instructions in dp/misc encoding space. */
7092 static int
7093 arm_decode_miscellaneous (struct gdbarch *gdbarch, uint32_t insn,
7094 struct regcache *regs,
7095 arm_displaced_step_copy_insn_closure *dsc)
7097 unsigned int op2 = bits (insn, 4, 6);
7098 unsigned int op = bits (insn, 21, 22);
7100 switch (op2)
7102 case 0x0:
7103 return arm_copy_unmodified (gdbarch, insn, "mrs/msr", dsc);
7105 case 0x1:
7106 if (op == 0x1) /* bx. */
7107 return arm_copy_bx_blx_reg (gdbarch, insn, regs, dsc);
7108 else if (op == 0x3)
7109 return arm_copy_unmodified (gdbarch, insn, "clz", dsc);
7110 else
7111 return arm_copy_undef (gdbarch, insn, dsc);
7113 case 0x2:
7114 if (op == 0x1)
7115 /* Not really supported. */
7116 return arm_copy_unmodified (gdbarch, insn, "bxj", dsc);
7117 else
7118 return arm_copy_undef (gdbarch, insn, dsc);
7120 case 0x3:
7121 if (op == 0x1)
7122 return arm_copy_bx_blx_reg (gdbarch, insn,
7123 regs, dsc); /* blx register. */
7124 else
7125 return arm_copy_undef (gdbarch, insn, dsc);
7127 case 0x5:
7128 return arm_copy_unmodified (gdbarch, insn, "saturating add/sub", dsc);
7130 case 0x7:
7131 if (op == 0x1)
7132 return arm_copy_unmodified (gdbarch, insn, "bkpt", dsc);
7133 else if (op == 0x3)
7134 /* Not really supported. */
7135 return arm_copy_unmodified (gdbarch, insn, "smc", dsc);
7136 /* Fall through. */
7138 default:
7139 return arm_copy_undef (gdbarch, insn, dsc);
7143 static int
7144 arm_decode_dp_misc (struct gdbarch *gdbarch, uint32_t insn,
7145 struct regcache *regs,
7146 arm_displaced_step_copy_insn_closure *dsc)
7148 if (bit (insn, 25))
7149 switch (bits (insn, 20, 24))
7151 case 0x10:
7152 return arm_copy_unmodified (gdbarch, insn, "movw", dsc);
7154 case 0x14:
7155 return arm_copy_unmodified (gdbarch, insn, "movt", dsc);
7157 case 0x12: case 0x16:
7158 return arm_copy_unmodified (gdbarch, insn, "msr imm", dsc);
7160 default:
7161 return arm_copy_alu_imm (gdbarch, insn, regs, dsc);
7163 else
7165 uint32_t op1 = bits (insn, 20, 24), op2 = bits (insn, 4, 7);
7167 if ((op1 & 0x19) != 0x10 && (op2 & 0x1) == 0x0)
7168 return arm_copy_alu_reg (gdbarch, insn, regs, dsc);
7169 else if ((op1 & 0x19) != 0x10 && (op2 & 0x9) == 0x1)
7170 return arm_copy_alu_shifted_reg (gdbarch, insn, regs, dsc);
7171 else if ((op1 & 0x19) == 0x10 && (op2 & 0x8) == 0x0)
7172 return arm_decode_miscellaneous (gdbarch, insn, regs, dsc);
7173 else if ((op1 & 0x19) == 0x10 && (op2 & 0x9) == 0x8)
7174 return arm_copy_unmodified (gdbarch, insn, "halfword mul/mla", dsc);
7175 else if ((op1 & 0x10) == 0x00 && op2 == 0x9)
7176 return arm_copy_unmodified (gdbarch, insn, "mul/mla", dsc);
7177 else if ((op1 & 0x10) == 0x10 && op2 == 0x9)
7178 return arm_copy_unmodified (gdbarch, insn, "synch", dsc);
7179 else if (op2 == 0xb || (op2 & 0xd) == 0xd)
7180 /* 2nd arg means "unprivileged". */
7181 return arm_copy_extra_ld_st (gdbarch, insn, (op1 & 0x12) == 0x02, regs,
7182 dsc);
7185 /* Should be unreachable. */
7186 return 1;
7189 static int
7190 arm_decode_ld_st_word_ubyte (struct gdbarch *gdbarch, uint32_t insn,
7191 struct regcache *regs,
7192 arm_displaced_step_copy_insn_closure *dsc)
7194 int a = bit (insn, 25), b = bit (insn, 4);
7195 uint32_t op1 = bits (insn, 20, 24);
7197 if ((!a && (op1 & 0x05) == 0x00 && (op1 & 0x17) != 0x02)
7198 || (a && (op1 & 0x05) == 0x00 && (op1 & 0x17) != 0x02 && !b))
7199 return arm_copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 0, 4, 0);
7200 else if ((!a && (op1 & 0x17) == 0x02)
7201 || (a && (op1 & 0x17) == 0x02 && !b))
7202 return arm_copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 0, 4, 1);
7203 else if ((!a && (op1 & 0x05) == 0x01 && (op1 & 0x17) != 0x03)
7204 || (a && (op1 & 0x05) == 0x01 && (op1 & 0x17) != 0x03 && !b))
7205 return arm_copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 1, 4, 0);
7206 else if ((!a && (op1 & 0x17) == 0x03)
7207 || (a && (op1 & 0x17) == 0x03 && !b))
7208 return arm_copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 1, 4, 1);
7209 else if ((!a && (op1 & 0x05) == 0x04 && (op1 & 0x17) != 0x06)
7210 || (a && (op1 & 0x05) == 0x04 && (op1 & 0x17) != 0x06 && !b))
7211 return arm_copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 0, 1, 0);
7212 else if ((!a && (op1 & 0x17) == 0x06)
7213 || (a && (op1 & 0x17) == 0x06 && !b))
7214 return arm_copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 0, 1, 1);
7215 else if ((!a && (op1 & 0x05) == 0x05 && (op1 & 0x17) != 0x07)
7216 || (a && (op1 & 0x05) == 0x05 && (op1 & 0x17) != 0x07 && !b))
7217 return arm_copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 1, 1, 0);
7218 else if ((!a && (op1 & 0x17) == 0x07)
7219 || (a && (op1 & 0x17) == 0x07 && !b))
7220 return arm_copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 1, 1, 1);
7222 /* Should be unreachable. */
7223 return 1;
7226 static int
7227 arm_decode_media (struct gdbarch *gdbarch, uint32_t insn,
7228 arm_displaced_step_copy_insn_closure *dsc)
7230 switch (bits (insn, 20, 24))
7232 case 0x00: case 0x01: case 0x02: case 0x03:
7233 return arm_copy_unmodified (gdbarch, insn, "parallel add/sub signed", dsc);
7235 case 0x04: case 0x05: case 0x06: case 0x07:
7236 return arm_copy_unmodified (gdbarch, insn, "parallel add/sub unsigned", dsc);
7238 case 0x08: case 0x09: case 0x0a: case 0x0b:
7239 case 0x0c: case 0x0d: case 0x0e: case 0x0f:
7240 return arm_copy_unmodified (gdbarch, insn,
7241 "decode/pack/unpack/saturate/reverse", dsc);
7243 case 0x18:
7244 if (bits (insn, 5, 7) == 0) /* op2. */
7246 if (bits (insn, 12, 15) == 0xf)
7247 return arm_copy_unmodified (gdbarch, insn, "usad8", dsc);
7248 else
7249 return arm_copy_unmodified (gdbarch, insn, "usada8", dsc);
7251 else
7252 return arm_copy_undef (gdbarch, insn, dsc);
7254 case 0x1a: case 0x1b:
7255 if (bits (insn, 5, 6) == 0x2) /* op2[1:0]. */
7256 return arm_copy_unmodified (gdbarch, insn, "sbfx", dsc);
7257 else
7258 return arm_copy_undef (gdbarch, insn, dsc);
7260 case 0x1c: case 0x1d:
7261 if (bits (insn, 5, 6) == 0x0) /* op2[1:0]. */
7263 if (bits (insn, 0, 3) == 0xf)
7264 return arm_copy_unmodified (gdbarch, insn, "bfc", dsc);
7265 else
7266 return arm_copy_unmodified (gdbarch, insn, "bfi", dsc);
7268 else
7269 return arm_copy_undef (gdbarch, insn, dsc);
7271 case 0x1e: case 0x1f:
7272 if (bits (insn, 5, 6) == 0x2) /* op2[1:0]. */
7273 return arm_copy_unmodified (gdbarch, insn, "ubfx", dsc);
7274 else
7275 return arm_copy_undef (gdbarch, insn, dsc);
7278 /* Should be unreachable. */
7279 return 1;
7282 static int
7283 arm_decode_b_bl_ldmstm (struct gdbarch *gdbarch, uint32_t insn,
7284 struct regcache *regs,
7285 arm_displaced_step_copy_insn_closure *dsc)
7287 if (bit (insn, 25))
7288 return arm_copy_b_bl_blx (gdbarch, insn, regs, dsc);
7289 else
7290 return arm_copy_block_xfer (gdbarch, insn, regs, dsc);
7293 static int
7294 arm_decode_ext_reg_ld_st (struct gdbarch *gdbarch, uint32_t insn,
7295 struct regcache *regs,
7296 arm_displaced_step_copy_insn_closure *dsc)
7298 unsigned int opcode = bits (insn, 20, 24);
7300 switch (opcode)
7302 case 0x04: case 0x05: /* VFP/Neon mrrc/mcrr. */
7303 return arm_copy_unmodified (gdbarch, insn, "vfp/neon mrrc/mcrr", dsc);
7305 case 0x08: case 0x0a: case 0x0c: case 0x0e:
7306 case 0x12: case 0x16:
7307 return arm_copy_unmodified (gdbarch, insn, "vfp/neon vstm/vpush", dsc);
7309 case 0x09: case 0x0b: case 0x0d: case 0x0f:
7310 case 0x13: case 0x17:
7311 return arm_copy_unmodified (gdbarch, insn, "vfp/neon vldm/vpop", dsc);
7313 case 0x10: case 0x14: case 0x18: case 0x1c: /* vstr. */
7314 case 0x11: case 0x15: case 0x19: case 0x1d: /* vldr. */
7315 /* Note: no writeback for these instructions. Bit 25 will always be
7316 zero though (via caller), so the following works OK. */
7317 return arm_copy_copro_load_store (gdbarch, insn, regs, dsc);
7320 /* Should be unreachable. */
7321 return 1;
7324 /* Decode shifted register instructions. */
7326 static int
7327 thumb2_decode_dp_shift_reg (struct gdbarch *gdbarch, uint16_t insn1,
7328 uint16_t insn2, struct regcache *regs,
7329 arm_displaced_step_copy_insn_closure *dsc)
7331 /* PC is only allowed to be used in instruction MOV. */
7333 unsigned int op = bits (insn1, 5, 8);
7334 unsigned int rn = bits (insn1, 0, 3);
7336 if (op == 0x2 && rn == 0xf) /* MOV */
7337 return thumb2_copy_alu_imm (gdbarch, insn1, insn2, regs, dsc);
7338 else
7339 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
7340 "dp (shift reg)", dsc);
7344 /* Decode extension register load/store. Exactly the same as
7345 arm_decode_ext_reg_ld_st. */
7347 static int
7348 thumb2_decode_ext_reg_ld_st (struct gdbarch *gdbarch, uint16_t insn1,
7349 uint16_t insn2, struct regcache *regs,
7350 arm_displaced_step_copy_insn_closure *dsc)
7352 unsigned int opcode = bits (insn1, 4, 8);
7354 switch (opcode)
7356 case 0x04: case 0x05:
7357 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
7358 "vfp/neon vmov", dsc);
7360 case 0x08: case 0x0c: /* 01x00 */
7361 case 0x0a: case 0x0e: /* 01x10 */
7362 case 0x12: case 0x16: /* 10x10 */
7363 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
7364 "vfp/neon vstm/vpush", dsc);
7366 case 0x09: case 0x0d: /* 01x01 */
7367 case 0x0b: case 0x0f: /* 01x11 */
7368 case 0x13: case 0x17: /* 10x11 */
7369 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
7370 "vfp/neon vldm/vpop", dsc);
7372 case 0x10: case 0x14: case 0x18: case 0x1c: /* vstr. */
7373 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
7374 "vstr", dsc);
7375 case 0x11: case 0x15: case 0x19: case 0x1d: /* vldr. */
7376 return thumb2_copy_copro_load_store (gdbarch, insn1, insn2, regs, dsc);
7379 /* Should be unreachable. */
7380 return 1;
7383 static int
7384 arm_decode_svc_copro (struct gdbarch *gdbarch, uint32_t insn,
7385 regcache *regs, arm_displaced_step_copy_insn_closure *dsc)
7387 unsigned int op1 = bits (insn, 20, 25);
7388 int op = bit (insn, 4);
7389 unsigned int coproc = bits (insn, 8, 11);
7391 if ((op1 & 0x20) == 0x00 && (op1 & 0x3a) != 0x00 && (coproc & 0xe) == 0xa)
7392 return arm_decode_ext_reg_ld_st (gdbarch, insn, regs, dsc);
7393 else if ((op1 & 0x21) == 0x00 && (op1 & 0x3a) != 0x00
7394 && (coproc & 0xe) != 0xa)
7395 /* stc/stc2. */
7396 return arm_copy_copro_load_store (gdbarch, insn, regs, dsc);
7397 else if ((op1 & 0x21) == 0x01 && (op1 & 0x3a) != 0x00
7398 && (coproc & 0xe) != 0xa)
7399 /* ldc/ldc2 imm/lit. */
7400 return arm_copy_copro_load_store (gdbarch, insn, regs, dsc);
7401 else if ((op1 & 0x3e) == 0x00)
7402 return arm_copy_undef (gdbarch, insn, dsc);
7403 else if ((op1 & 0x3e) == 0x04 && (coproc & 0xe) == 0xa)
7404 return arm_copy_unmodified (gdbarch, insn, "neon 64bit xfer", dsc);
7405 else if (op1 == 0x04 && (coproc & 0xe) != 0xa)
7406 return arm_copy_unmodified (gdbarch, insn, "mcrr/mcrr2", dsc);
7407 else if (op1 == 0x05 && (coproc & 0xe) != 0xa)
7408 return arm_copy_unmodified (gdbarch, insn, "mrrc/mrrc2", dsc);
7409 else if ((op1 & 0x30) == 0x20 && !op)
7411 if ((coproc & 0xe) == 0xa)
7412 return arm_copy_unmodified (gdbarch, insn, "vfp dataproc", dsc);
7413 else
7414 return arm_copy_unmodified (gdbarch, insn, "cdp/cdp2", dsc);
7416 else if ((op1 & 0x30) == 0x20 && op)
7417 return arm_copy_unmodified (gdbarch, insn, "neon 8/16/32 bit xfer", dsc);
7418 else if ((op1 & 0x31) == 0x20 && op && (coproc & 0xe) != 0xa)
7419 return arm_copy_unmodified (gdbarch, insn, "mcr/mcr2", dsc);
7420 else if ((op1 & 0x31) == 0x21 && op && (coproc & 0xe) != 0xa)
7421 return arm_copy_unmodified (gdbarch, insn, "mrc/mrc2", dsc);
7422 else if ((op1 & 0x30) == 0x30)
7423 return arm_copy_svc (gdbarch, insn, regs, dsc);
7424 else
7425 return arm_copy_undef (gdbarch, insn, dsc); /* Possibly unreachable. */
7428 static int
7429 thumb2_decode_svc_copro (struct gdbarch *gdbarch, uint16_t insn1,
7430 uint16_t insn2, struct regcache *regs,
7431 arm_displaced_step_copy_insn_closure *dsc)
7433 unsigned int coproc = bits (insn2, 8, 11);
7434 unsigned int bit_5_8 = bits (insn1, 5, 8);
7435 unsigned int bit_9 = bit (insn1, 9);
7436 unsigned int bit_4 = bit (insn1, 4);
7438 if (bit_9 == 0)
7440 if (bit_5_8 == 2)
7441 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
7442 "neon 64bit xfer/mrrc/mrrc2/mcrr/mcrr2",
7443 dsc);
7444 else if (bit_5_8 == 0) /* UNDEFINED. */
7445 return thumb_32bit_copy_undef (gdbarch, insn1, insn2, dsc);
7446 else
7448 /*coproc is 101x. SIMD/VFP, ext registers load/store. */
7449 if ((coproc & 0xe) == 0xa)
7450 return thumb2_decode_ext_reg_ld_st (gdbarch, insn1, insn2, regs,
7451 dsc);
7452 else /* coproc is not 101x. */
7454 if (bit_4 == 0) /* STC/STC2. */
7455 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
7456 "stc/stc2", dsc);
7457 else /* LDC/LDC2 {literal, immediate}. */
7458 return thumb2_copy_copro_load_store (gdbarch, insn1, insn2,
7459 regs, dsc);
7463 else
7464 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2, "coproc", dsc);
7466 return 0;
7469 static void
7470 install_pc_relative (struct gdbarch *gdbarch, struct regcache *regs,
7471 arm_displaced_step_copy_insn_closure *dsc, int rd)
7473 /* ADR Rd, #imm
7475 Rewrite as:
7477 Preparation: Rd <- PC
7478 Insn: ADD Rd, #imm
7479 Cleanup: Null.
7482 /* Rd <- PC */
7483 int val = displaced_read_reg (regs, dsc, ARM_PC_REGNUM);
7484 displaced_write_reg (regs, dsc, rd, val, CANNOT_WRITE_PC);
7487 static int
7488 thumb_copy_pc_relative_16bit (struct gdbarch *gdbarch, struct regcache *regs,
7489 arm_displaced_step_copy_insn_closure *dsc,
7490 int rd, unsigned int imm)
7493 /* Encoding T2: ADDS Rd, #imm */
7494 dsc->modinsn[0] = (0x3000 | (rd << 8) | imm);
7496 install_pc_relative (gdbarch, regs, dsc, rd);
7498 return 0;
7501 static int
7502 thumb_decode_pc_relative_16bit (struct gdbarch *gdbarch, uint16_t insn,
7503 struct regcache *regs,
7504 arm_displaced_step_copy_insn_closure *dsc)
7506 unsigned int rd = bits (insn, 8, 10);
7507 unsigned int imm8 = bits (insn, 0, 7);
7509 displaced_debug_printf ("copying thumb adr r%d, #%d insn %.4x",
7510 rd, imm8, insn);
7512 return thumb_copy_pc_relative_16bit (gdbarch, regs, dsc, rd, imm8);
7515 static int
7516 thumb_copy_pc_relative_32bit (struct gdbarch *gdbarch, uint16_t insn1,
7517 uint16_t insn2, struct regcache *regs,
7518 arm_displaced_step_copy_insn_closure *dsc)
7520 unsigned int rd = bits (insn2, 8, 11);
7521 /* Since immediate has the same encoding in ADR ADD and SUB, so we simply
7522 extract raw immediate encoding rather than computing immediate. When
7523 generating ADD or SUB instruction, we can simply perform OR operation to
7524 set immediate into ADD. */
7525 unsigned int imm_3_8 = insn2 & 0x70ff;
7526 unsigned int imm_i = insn1 & 0x0400; /* Clear all bits except bit 10. */
7528 displaced_debug_printf ("copying thumb adr r%d, #%d:%d insn %.4x%.4x",
7529 rd, imm_i, imm_3_8, insn1, insn2);
7531 if (bit (insn1, 7)) /* Encoding T2 */
7533 /* Encoding T3: SUB Rd, Rd, #imm */
7534 dsc->modinsn[0] = (0xf1a0 | rd | imm_i);
7535 dsc->modinsn[1] = ((rd << 8) | imm_3_8);
7537 else /* Encoding T3 */
7539 /* Encoding T3: ADD Rd, Rd, #imm */
7540 dsc->modinsn[0] = (0xf100 | rd | imm_i);
7541 dsc->modinsn[1] = ((rd << 8) | imm_3_8);
7543 dsc->numinsns = 2;
7545 install_pc_relative (gdbarch, regs, dsc, rd);
7547 return 0;
7550 static int
7551 thumb_copy_16bit_ldr_literal (struct gdbarch *gdbarch, uint16_t insn1,
7552 struct regcache *regs,
7553 arm_displaced_step_copy_insn_closure *dsc)
7555 unsigned int rt = bits (insn1, 8, 10);
7556 unsigned int pc;
7557 int imm8 = (bits (insn1, 0, 7) << 2);
7559 /* LDR Rd, #imm8
7561 Rwrite as:
7563 Preparation: tmp0 <- R0, tmp2 <- R2, tmp3 <- R3, R2 <- PC, R3 <- #imm8;
7565 Insn: LDR R0, [R2, R3];
7566 Cleanup: R2 <- tmp2, R3 <- tmp3, Rd <- R0, R0 <- tmp0 */
7568 displaced_debug_printf ("copying thumb ldr r%d [pc #%d]", rt, imm8);
7570 dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
7571 dsc->tmp[2] = displaced_read_reg (regs, dsc, 2);
7572 dsc->tmp[3] = displaced_read_reg (regs, dsc, 3);
7573 pc = displaced_read_reg (regs, dsc, ARM_PC_REGNUM);
7574 /* The assembler calculates the required value of the offset from the
7575 Align(PC,4) value of this instruction to the label. */
7576 pc = pc & 0xfffffffc;
7578 displaced_write_reg (regs, dsc, 2, pc, CANNOT_WRITE_PC);
7579 displaced_write_reg (regs, dsc, 3, imm8, CANNOT_WRITE_PC);
7581 dsc->rd = rt;
7582 dsc->u.ldst.xfersize = 4;
7583 dsc->u.ldst.rn = 0;
7584 dsc->u.ldst.immed = 0;
7585 dsc->u.ldst.writeback = 0;
7586 dsc->u.ldst.restore_r4 = 0;
7588 dsc->modinsn[0] = 0x58d0; /* ldr r0, [r2, r3]*/
7590 dsc->cleanup = &cleanup_load;
7592 return 0;
7595 /* Copy Thumb cbnz/cbz instruction. */
7597 static int
7598 thumb_copy_cbnz_cbz (struct gdbarch *gdbarch, uint16_t insn1,
7599 struct regcache *regs,
7600 arm_displaced_step_copy_insn_closure *dsc)
7602 int non_zero = bit (insn1, 11);
7603 unsigned int imm5 = (bit (insn1, 9) << 6) | (bits (insn1, 3, 7) << 1);
7604 CORE_ADDR from = dsc->insn_addr;
7605 int rn = bits (insn1, 0, 2);
7606 int rn_val = displaced_read_reg (regs, dsc, rn);
7608 dsc->u.branch.cond = (rn_val && non_zero) || (!rn_val && !non_zero);
7609 /* CBNZ and CBZ do not affect the condition flags. If condition is true,
7610 set it INST_AL, so cleanup_branch will know branch is taken, otherwise,
7611 condition is false, let it be, cleanup_branch will do nothing. */
7612 if (dsc->u.branch.cond)
7614 dsc->u.branch.cond = INST_AL;
7615 dsc->u.branch.dest = from + 4 + imm5;
7617 else
7618 dsc->u.branch.dest = from + 2;
7620 dsc->u.branch.link = 0;
7621 dsc->u.branch.exchange = 0;
7623 displaced_debug_printf ("copying %s [r%d = 0x%x] insn %.4x to %.8lx",
7624 non_zero ? "cbnz" : "cbz",
7625 rn, rn_val, insn1, dsc->u.branch.dest);
7627 dsc->modinsn[0] = THUMB_NOP;
7629 dsc->cleanup = &cleanup_branch;
7630 return 0;
7633 /* Copy Table Branch Byte/Halfword */
7634 static int
7635 thumb2_copy_table_branch (struct gdbarch *gdbarch, uint16_t insn1,
7636 uint16_t insn2, struct regcache *regs,
7637 arm_displaced_step_copy_insn_closure *dsc)
7639 ULONGEST rn_val, rm_val;
7640 int is_tbh = bit (insn2, 4);
7641 CORE_ADDR halfwords = 0;
7642 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
7644 rn_val = displaced_read_reg (regs, dsc, bits (insn1, 0, 3));
7645 rm_val = displaced_read_reg (regs, dsc, bits (insn2, 0, 3));
7647 if (is_tbh)
7649 gdb_byte buf[2];
7651 target_read_memory (rn_val + 2 * rm_val, buf, 2);
7652 halfwords = extract_unsigned_integer (buf, 2, byte_order);
7654 else
7656 gdb_byte buf[1];
7658 target_read_memory (rn_val + rm_val, buf, 1);
7659 halfwords = extract_unsigned_integer (buf, 1, byte_order);
7662 displaced_debug_printf ("%s base 0x%x offset 0x%x offset 0x%x",
7663 is_tbh ? "tbh" : "tbb",
7664 (unsigned int) rn_val, (unsigned int) rm_val,
7665 (unsigned int) halfwords);
7667 dsc->u.branch.cond = INST_AL;
7668 dsc->u.branch.link = 0;
7669 dsc->u.branch.exchange = 0;
7670 dsc->u.branch.dest = dsc->insn_addr + 4 + 2 * halfwords;
7672 dsc->cleanup = &cleanup_branch;
7674 return 0;
7677 static void
7678 cleanup_pop_pc_16bit_all (struct gdbarch *gdbarch, struct regcache *regs,
7679 arm_displaced_step_copy_insn_closure *dsc)
7681 /* PC <- r7 */
7682 int val = displaced_read_reg (regs, dsc, 7);
7683 displaced_write_reg (regs, dsc, ARM_PC_REGNUM, val, BX_WRITE_PC);
7685 /* r7 <- r8 */
7686 val = displaced_read_reg (regs, dsc, 8);
7687 displaced_write_reg (regs, dsc, 7, val, CANNOT_WRITE_PC);
7689 /* r8 <- tmp[0] */
7690 displaced_write_reg (regs, dsc, 8, dsc->tmp[0], CANNOT_WRITE_PC);
7694 static int
7695 thumb_copy_pop_pc_16bit (struct gdbarch *gdbarch, uint16_t insn1,
7696 struct regcache *regs,
7697 arm_displaced_step_copy_insn_closure *dsc)
7699 dsc->u.block.regmask = insn1 & 0x00ff;
7701 /* Rewrite instruction: POP {rX, rY, ...,rZ, PC}
7702 to :
7704 (1) register list is full, that is, r0-r7 are used.
7705 Prepare: tmp[0] <- r8
7707 POP {r0, r1, ...., r6, r7}; remove PC from reglist
7708 MOV r8, r7; Move value of r7 to r8;
7709 POP {r7}; Store PC value into r7.
7711 Cleanup: PC <- r7, r7 <- r8, r8 <-tmp[0]
7713 (2) register list is not full, supposing there are N registers in
7714 register list (except PC, 0 <= N <= 7).
7715 Prepare: for each i, 0 - N, tmp[i] <- ri.
7717 POP {r0, r1, ...., rN};
7719 Cleanup: Set registers in original reglist from r0 - rN. Restore r0 - rN
7720 from tmp[] properly.
7722 displaced_debug_printf ("copying thumb pop {%.8x, pc} insn %.4x",
7723 dsc->u.block.regmask, insn1);
7725 if (dsc->u.block.regmask == 0xff)
7727 dsc->tmp[0] = displaced_read_reg (regs, dsc, 8);
7729 dsc->modinsn[0] = (insn1 & 0xfeff); /* POP {r0,r1,...,r6, r7} */
7730 dsc->modinsn[1] = 0x46b8; /* MOV r8, r7 */
7731 dsc->modinsn[2] = 0xbc80; /* POP {r7} */
7733 dsc->numinsns = 3;
7734 dsc->cleanup = &cleanup_pop_pc_16bit_all;
7736 else
7738 unsigned int num_in_list = count_one_bits (dsc->u.block.regmask);
7739 unsigned int i;
7740 unsigned int new_regmask;
7742 for (i = 0; i < num_in_list + 1; i++)
7743 dsc->tmp[i] = displaced_read_reg (regs, dsc, i);
7745 new_regmask = (1 << (num_in_list + 1)) - 1;
7747 displaced_debug_printf ("POP {..., pc}: original reg list %.4x, "
7748 "modified list %.4x",
7749 (int) dsc->u.block.regmask, new_regmask);
7751 dsc->u.block.regmask |= 0x8000;
7752 dsc->u.block.writeback = 0;
7753 dsc->u.block.cond = INST_AL;
7755 dsc->modinsn[0] = (insn1 & ~0x1ff) | (new_regmask & 0xff);
7757 dsc->cleanup = &cleanup_block_load_pc;
7760 return 0;
7763 static void
7764 thumb_process_displaced_16bit_insn (struct gdbarch *gdbarch, uint16_t insn1,
7765 struct regcache *regs,
7766 arm_displaced_step_copy_insn_closure *dsc)
7768 unsigned short op_bit_12_15 = bits (insn1, 12, 15);
7769 unsigned short op_bit_10_11 = bits (insn1, 10, 11);
7770 int err = 0;
7772 /* 16-bit thumb instructions. */
7773 switch (op_bit_12_15)
7775 /* Shift (imme), add, subtract, move and compare. */
7776 case 0: case 1: case 2: case 3:
7777 err = thumb_copy_unmodified_16bit (gdbarch, insn1,
7778 "shift/add/sub/mov/cmp",
7779 dsc);
7780 break;
7781 case 4:
7782 switch (op_bit_10_11)
7784 case 0: /* Data-processing */
7785 err = thumb_copy_unmodified_16bit (gdbarch, insn1,
7786 "data-processing",
7787 dsc);
7788 break;
7789 case 1: /* Special data instructions and branch and exchange. */
7791 unsigned short op = bits (insn1, 7, 9);
7792 if (op == 6 || op == 7) /* BX or BLX */
7793 err = thumb_copy_bx_blx_reg (gdbarch, insn1, regs, dsc);
7794 else if (bits (insn1, 6, 7) != 0) /* ADD/MOV/CMP high registers. */
7795 err = thumb_copy_alu_reg (gdbarch, insn1, regs, dsc);
7796 else
7797 err = thumb_copy_unmodified_16bit (gdbarch, insn1, "special data",
7798 dsc);
7800 break;
7801 default: /* LDR (literal) */
7802 err = thumb_copy_16bit_ldr_literal (gdbarch, insn1, regs, dsc);
7804 break;
7805 case 5: case 6: case 7: case 8: case 9: /* Load/Store single data item */
7806 err = thumb_copy_unmodified_16bit (gdbarch, insn1, "ldr/str", dsc);
7807 break;
7808 case 10:
7809 if (op_bit_10_11 < 2) /* Generate PC-relative address */
7810 err = thumb_decode_pc_relative_16bit (gdbarch, insn1, regs, dsc);
7811 else /* Generate SP-relative address */
7812 err = thumb_copy_unmodified_16bit (gdbarch, insn1, "sp-relative", dsc);
7813 break;
7814 case 11: /* Misc 16-bit instructions */
7816 switch (bits (insn1, 8, 11))
7818 case 1: case 3: case 9: case 11: /* CBNZ, CBZ */
7819 err = thumb_copy_cbnz_cbz (gdbarch, insn1, regs, dsc);
7820 break;
7821 case 12: case 13: /* POP */
7822 if (bit (insn1, 8)) /* PC is in register list. */
7823 err = thumb_copy_pop_pc_16bit (gdbarch, insn1, regs, dsc);
7824 else
7825 err = thumb_copy_unmodified_16bit (gdbarch, insn1, "pop", dsc);
7826 break;
7827 case 15: /* If-Then, and hints */
7828 if (bits (insn1, 0, 3))
7829 /* If-Then makes up to four following instructions conditional.
7830 IT instruction itself is not conditional, so handle it as a
7831 common unmodified instruction. */
7832 err = thumb_copy_unmodified_16bit (gdbarch, insn1, "If-Then",
7833 dsc);
7834 else
7835 err = thumb_copy_unmodified_16bit (gdbarch, insn1, "hints", dsc);
7836 break;
7837 default:
7838 err = thumb_copy_unmodified_16bit (gdbarch, insn1, "misc", dsc);
7841 break;
7842 case 12:
7843 if (op_bit_10_11 < 2) /* Store multiple registers */
7844 err = thumb_copy_unmodified_16bit (gdbarch, insn1, "stm", dsc);
7845 else /* Load multiple registers */
7846 err = thumb_copy_unmodified_16bit (gdbarch, insn1, "ldm", dsc);
7847 break;
7848 case 13: /* Conditional branch and supervisor call */
7849 if (bits (insn1, 9, 11) != 7) /* conditional branch */
7850 err = thumb_copy_b (gdbarch, insn1, dsc);
7851 else
7852 err = thumb_copy_svc (gdbarch, insn1, regs, dsc);
7853 break;
7854 case 14: /* Unconditional branch */
7855 err = thumb_copy_b (gdbarch, insn1, dsc);
7856 break;
7857 default:
7858 err = 1;
7861 if (err)
7862 internal_error (__FILE__, __LINE__,
7863 _("thumb_process_displaced_16bit_insn: Instruction decode error"));
7866 static int
7867 decode_thumb_32bit_ld_mem_hints (struct gdbarch *gdbarch,
7868 uint16_t insn1, uint16_t insn2,
7869 struct regcache *regs,
7870 arm_displaced_step_copy_insn_closure *dsc)
7872 int rt = bits (insn2, 12, 15);
7873 int rn = bits (insn1, 0, 3);
7874 int op1 = bits (insn1, 7, 8);
7876 switch (bits (insn1, 5, 6))
7878 case 0: /* Load byte and memory hints */
7879 if (rt == 0xf) /* PLD/PLI */
7881 if (rn == 0xf)
7882 /* PLD literal or Encoding T3 of PLI(immediate, literal). */
7883 return thumb2_copy_preload (gdbarch, insn1, insn2, regs, dsc);
7884 else
7885 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
7886 "pli/pld", dsc);
7888 else
7890 if (rn == 0xf) /* LDRB/LDRSB (literal) */
7891 return thumb2_copy_load_literal (gdbarch, insn1, insn2, regs, dsc,
7893 else
7894 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
7895 "ldrb{reg, immediate}/ldrbt",
7896 dsc);
7899 break;
7900 case 1: /* Load halfword and memory hints. */
7901 if (rt == 0xf) /* PLD{W} and Unalloc memory hint. */
7902 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
7903 "pld/unalloc memhint", dsc);
7904 else
7906 if (rn == 0xf)
7907 return thumb2_copy_load_literal (gdbarch, insn1, insn2, regs, dsc,
7909 else
7910 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
7911 "ldrh/ldrht", dsc);
7913 break;
7914 case 2: /* Load word */
7916 int insn2_bit_8_11 = bits (insn2, 8, 11);
7918 if (rn == 0xf)
7919 return thumb2_copy_load_literal (gdbarch, insn1, insn2, regs, dsc, 4);
7920 else if (op1 == 0x1) /* Encoding T3 */
7921 return thumb2_copy_load_reg_imm (gdbarch, insn1, insn2, regs, dsc,
7922 0, 1);
7923 else /* op1 == 0x0 */
7925 if (insn2_bit_8_11 == 0xc || (insn2_bit_8_11 & 0x9) == 0x9)
7926 /* LDR (immediate) */
7927 return thumb2_copy_load_reg_imm (gdbarch, insn1, insn2, regs,
7928 dsc, bit (insn2, 8), 1);
7929 else if (insn2_bit_8_11 == 0xe) /* LDRT */
7930 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
7931 "ldrt", dsc);
7932 else
7933 /* LDR (register) */
7934 return thumb2_copy_load_reg_imm (gdbarch, insn1, insn2, regs,
7935 dsc, 0, 0);
7937 break;
7939 default:
7940 return thumb_32bit_copy_undef (gdbarch, insn1, insn2, dsc);
7941 break;
7943 return 0;
7946 static void
7947 thumb_process_displaced_32bit_insn (struct gdbarch *gdbarch, uint16_t insn1,
7948 uint16_t insn2, struct regcache *regs,
7949 arm_displaced_step_copy_insn_closure *dsc)
7951 int err = 0;
7952 unsigned short op = bit (insn2, 15);
7953 unsigned int op1 = bits (insn1, 11, 12);
7955 switch (op1)
7957 case 1:
7959 switch (bits (insn1, 9, 10))
7961 case 0:
7962 if (bit (insn1, 6))
7964 /* Load/store {dual, exclusive}, table branch. */
7965 if (bits (insn1, 7, 8) == 1 && bits (insn1, 4, 5) == 1
7966 && bits (insn2, 5, 7) == 0)
7967 err = thumb2_copy_table_branch (gdbarch, insn1, insn2, regs,
7968 dsc);
7969 else
7970 /* PC is not allowed to use in load/store {dual, exclusive}
7971 instructions. */
7972 err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
7973 "load/store dual/ex", dsc);
7975 else /* load/store multiple */
7977 switch (bits (insn1, 7, 8))
7979 case 0: case 3: /* SRS, RFE */
7980 err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
7981 "srs/rfe", dsc);
7982 break;
7983 case 1: case 2: /* LDM/STM/PUSH/POP */
7984 err = thumb2_copy_block_xfer (gdbarch, insn1, insn2, regs, dsc);
7985 break;
7988 break;
7990 case 1:
7991 /* Data-processing (shift register). */
7992 err = thumb2_decode_dp_shift_reg (gdbarch, insn1, insn2, regs,
7993 dsc);
7994 break;
7995 default: /* Coprocessor instructions. */
7996 err = thumb2_decode_svc_copro (gdbarch, insn1, insn2, regs, dsc);
7997 break;
7999 break;
8001 case 2: /* op1 = 2 */
8002 if (op) /* Branch and misc control. */
8004 if (bit (insn2, 14) /* BLX/BL */
8005 || bit (insn2, 12) /* Unconditional branch */
8006 || (bits (insn1, 7, 9) != 0x7)) /* Conditional branch */
8007 err = thumb2_copy_b_bl_blx (gdbarch, insn1, insn2, regs, dsc);
8008 else
8009 err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8010 "misc ctrl", dsc);
8012 else
8014 if (bit (insn1, 9)) /* Data processing (plain binary imm). */
8016 int dp_op = bits (insn1, 4, 8);
8017 int rn = bits (insn1, 0, 3);
8018 if ((dp_op == 0 || dp_op == 0xa) && rn == 0xf)
8019 err = thumb_copy_pc_relative_32bit (gdbarch, insn1, insn2,
8020 regs, dsc);
8021 else
8022 err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8023 "dp/pb", dsc);
8025 else /* Data processing (modified immediate) */
8026 err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8027 "dp/mi", dsc);
8029 break;
8030 case 3: /* op1 = 3 */
8031 switch (bits (insn1, 9, 10))
8033 case 0:
8034 if (bit (insn1, 4))
8035 err = decode_thumb_32bit_ld_mem_hints (gdbarch, insn1, insn2,
8036 regs, dsc);
8037 else /* NEON Load/Store and Store single data item */
8038 err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8039 "neon elt/struct load/store",
8040 dsc);
8041 break;
8042 case 1: /* op1 = 3, bits (9, 10) == 1 */
8043 switch (bits (insn1, 7, 8))
8045 case 0: case 1: /* Data processing (register) */
8046 err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8047 "dp(reg)", dsc);
8048 break;
8049 case 2: /* Multiply and absolute difference */
8050 err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8051 "mul/mua/diff", dsc);
8052 break;
8053 case 3: /* Long multiply and divide */
8054 err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8055 "lmul/lmua", dsc);
8056 break;
8058 break;
8059 default: /* Coprocessor instructions */
8060 err = thumb2_decode_svc_copro (gdbarch, insn1, insn2, regs, dsc);
8061 break;
8063 break;
8064 default:
8065 err = 1;
8068 if (err)
8069 internal_error (__FILE__, __LINE__,
8070 _("thumb_process_displaced_32bit_insn: Instruction decode error"));
8074 static void
8075 thumb_process_displaced_insn (struct gdbarch *gdbarch, CORE_ADDR from,
8076 struct regcache *regs,
8077 arm_displaced_step_copy_insn_closure *dsc)
8079 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
8080 uint16_t insn1
8081 = read_memory_unsigned_integer (from, 2, byte_order_for_code);
8083 displaced_debug_printf ("process thumb insn %.4x at %.8lx",
8084 insn1, (unsigned long) from);
8086 dsc->is_thumb = 1;
8087 dsc->insn_size = thumb_insn_size (insn1);
8088 if (thumb_insn_size (insn1) == 4)
8090 uint16_t insn2
8091 = read_memory_unsigned_integer (from + 2, 2, byte_order_for_code);
8092 thumb_process_displaced_32bit_insn (gdbarch, insn1, insn2, regs, dsc);
8094 else
8095 thumb_process_displaced_16bit_insn (gdbarch, insn1, regs, dsc);
8098 void
8099 arm_process_displaced_insn (struct gdbarch *gdbarch, CORE_ADDR from,
8100 CORE_ADDR to, struct regcache *regs,
8101 arm_displaced_step_copy_insn_closure *dsc)
8103 int err = 0;
8104 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
8105 uint32_t insn;
8107 /* Most displaced instructions use a 1-instruction scratch space, so set this
8108 here and override below if/when necessary. */
8109 dsc->numinsns = 1;
8110 dsc->insn_addr = from;
8111 dsc->scratch_base = to;
8112 dsc->cleanup = NULL;
8113 dsc->wrote_to_pc = 0;
8115 if (!displaced_in_arm_mode (regs))
8116 return thumb_process_displaced_insn (gdbarch, from, regs, dsc);
8118 dsc->is_thumb = 0;
8119 dsc->insn_size = 4;
8120 insn = read_memory_unsigned_integer (from, 4, byte_order_for_code);
8121 displaced_debug_printf ("stepping insn %.8lx at %.8lx",
8122 (unsigned long) insn, (unsigned long) from);
8124 if ((insn & 0xf0000000) == 0xf0000000)
8125 err = arm_decode_unconditional (gdbarch, insn, regs, dsc);
8126 else switch (((insn & 0x10) >> 4) | ((insn & 0xe000000) >> 24))
8128 case 0x0: case 0x1: case 0x2: case 0x3:
8129 err = arm_decode_dp_misc (gdbarch, insn, regs, dsc);
8130 break;
8132 case 0x4: case 0x5: case 0x6:
8133 err = arm_decode_ld_st_word_ubyte (gdbarch, insn, regs, dsc);
8134 break;
8136 case 0x7:
8137 err = arm_decode_media (gdbarch, insn, dsc);
8138 break;
8140 case 0x8: case 0x9: case 0xa: case 0xb:
8141 err = arm_decode_b_bl_ldmstm (gdbarch, insn, regs, dsc);
8142 break;
8144 case 0xc: case 0xd: case 0xe: case 0xf:
8145 err = arm_decode_svc_copro (gdbarch, insn, regs, dsc);
8146 break;
8149 if (err)
8150 internal_error (__FILE__, __LINE__,
8151 _("arm_process_displaced_insn: Instruction decode error"));
8154 /* Actually set up the scratch space for a displaced instruction. */
8156 void
8157 arm_displaced_init_closure (struct gdbarch *gdbarch, CORE_ADDR from,
8158 CORE_ADDR to,
8159 arm_displaced_step_copy_insn_closure *dsc)
8161 arm_gdbarch_tdep *tdep = (arm_gdbarch_tdep *) gdbarch_tdep (gdbarch);
8162 unsigned int i, len, offset;
8163 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
8164 int size = dsc->is_thumb? 2 : 4;
8165 const gdb_byte *bkp_insn;
8167 offset = 0;
8168 /* Poke modified instruction(s). */
8169 for (i = 0; i < dsc->numinsns; i++)
8171 if (size == 4)
8172 displaced_debug_printf ("writing insn %.8lx at %.8lx",
8173 dsc->modinsn[i], (unsigned long) to + offset);
8174 else if (size == 2)
8175 displaced_debug_printf ("writing insn %.4x at %.8lx",
8176 (unsigned short) dsc->modinsn[i],
8177 (unsigned long) to + offset);
8179 write_memory_unsigned_integer (to + offset, size,
8180 byte_order_for_code,
8181 dsc->modinsn[i]);
8182 offset += size;
8185 /* Choose the correct breakpoint instruction. */
8186 if (dsc->is_thumb)
8188 bkp_insn = tdep->thumb_breakpoint;
8189 len = tdep->thumb_breakpoint_size;
8191 else
8193 bkp_insn = tdep->arm_breakpoint;
8194 len = tdep->arm_breakpoint_size;
8197 /* Put breakpoint afterwards. */
8198 write_memory (to + offset, bkp_insn, len);
8200 displaced_debug_printf ("copy %s->%s", paddress (gdbarch, from),
8201 paddress (gdbarch, to));
8204 /* Entry point for cleaning things up after a displaced instruction has been
8205 single-stepped. */
8207 void
8208 arm_displaced_step_fixup (struct gdbarch *gdbarch,
8209 struct displaced_step_copy_insn_closure *dsc_,
8210 CORE_ADDR from, CORE_ADDR to,
8211 struct regcache *regs)
8213 arm_displaced_step_copy_insn_closure *dsc
8214 = (arm_displaced_step_copy_insn_closure *) dsc_;
8216 if (dsc->cleanup)
8217 dsc->cleanup (gdbarch, regs, dsc);
8219 if (!dsc->wrote_to_pc)
8220 regcache_cooked_write_unsigned (regs, ARM_PC_REGNUM,
8221 dsc->insn_addr + dsc->insn_size);
8225 #include "bfd-in2.h"
8226 #include "libcoff.h"
8228 static int
8229 gdb_print_insn_arm (bfd_vma memaddr, disassemble_info *info)
8231 gdb_disassembler *di
8232 = static_cast<gdb_disassembler *>(info->application_data);
8233 struct gdbarch *gdbarch = di->arch ();
8235 if (arm_pc_is_thumb (gdbarch, memaddr))
8237 static asymbol *asym;
8238 static combined_entry_type ce;
8239 static struct coff_symbol_struct csym;
8240 static struct bfd fake_bfd;
8241 static bfd_target fake_target;
8243 if (csym.native == NULL)
8245 /* Create a fake symbol vector containing a Thumb symbol.
8246 This is solely so that the code in print_insn_little_arm()
8247 and print_insn_big_arm() in opcodes/arm-dis.c will detect
8248 the presence of a Thumb symbol and switch to decoding
8249 Thumb instructions. */
8251 fake_target.flavour = bfd_target_coff_flavour;
8252 fake_bfd.xvec = &fake_target;
8253 ce.u.syment.n_sclass = C_THUMBEXTFUNC;
8254 csym.native = &ce;
8255 csym.symbol.the_bfd = &fake_bfd;
8256 csym.symbol.name = "fake";
8257 asym = (asymbol *) & csym;
8260 memaddr = UNMAKE_THUMB_ADDR (memaddr);
8261 info->symbols = &asym;
8263 else
8264 info->symbols = NULL;
8266 /* GDB is able to get bfd_mach from the exe_bfd, info->mach is
8267 accurate, so mark USER_SPECIFIED_MACHINE_TYPE bit. Otherwise,
8268 opcodes/arm-dis.c:print_insn reset info->mach, and it will trigger
8269 the assert on the mismatch of info->mach and
8270 bfd_get_mach (current_program_space->exec_bfd ()) in
8271 default_print_insn. */
8272 if (current_program_space->exec_bfd () != NULL
8273 && (current_program_space->exec_bfd ()->arch_info
8274 == gdbarch_bfd_arch_info (gdbarch)))
8275 info->flags |= USER_SPECIFIED_MACHINE_TYPE;
8277 return default_print_insn (memaddr, info);
8280 /* The following define instruction sequences that will cause ARM
8281 cpu's to take an undefined instruction trap. These are used to
8282 signal a breakpoint to GDB.
8284 The newer ARMv4T cpu's are capable of operating in ARM or Thumb
8285 modes. A different instruction is required for each mode. The ARM
8286 cpu's can also be big or little endian. Thus four different
8287 instructions are needed to support all cases.
8289 Note: ARMv4 defines several new instructions that will take the
8290 undefined instruction trap. ARM7TDMI is nominally ARMv4T, but does
8291 not in fact add the new instructions. The new undefined
8292 instructions in ARMv4 are all instructions that had no defined
8293 behaviour in earlier chips. There is no guarantee that they will
8294 raise an exception, but may be treated as NOP's. In practice, it
8295 may only safe to rely on instructions matching:
8297 3 3 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1
8298 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
8299 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
8301 Even this may only true if the condition predicate is true. The
8302 following use a condition predicate of ALWAYS so it is always TRUE.
8304 There are other ways of forcing a breakpoint. GNU/Linux, RISC iX,
8305 and NetBSD all use a software interrupt rather than an undefined
8306 instruction to force a trap. This can be handled by by the
8307 abi-specific code during establishment of the gdbarch vector. */
8309 #define ARM_LE_BREAKPOINT {0xFE,0xDE,0xFF,0xE7}
8310 #define ARM_BE_BREAKPOINT {0xE7,0xFF,0xDE,0xFE}
8311 #define THUMB_LE_BREAKPOINT {0xbe,0xbe}
8312 #define THUMB_BE_BREAKPOINT {0xbe,0xbe}
8314 static const gdb_byte arm_default_arm_le_breakpoint[] = ARM_LE_BREAKPOINT;
8315 static const gdb_byte arm_default_arm_be_breakpoint[] = ARM_BE_BREAKPOINT;
8316 static const gdb_byte arm_default_thumb_le_breakpoint[] = THUMB_LE_BREAKPOINT;
8317 static const gdb_byte arm_default_thumb_be_breakpoint[] = THUMB_BE_BREAKPOINT;
8319 /* Implement the breakpoint_kind_from_pc gdbarch method. */
8321 static int
8322 arm_breakpoint_kind_from_pc (struct gdbarch *gdbarch, CORE_ADDR *pcptr)
8324 arm_gdbarch_tdep *tdep = (arm_gdbarch_tdep *) gdbarch_tdep (gdbarch);
8325 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
8327 if (arm_pc_is_thumb (gdbarch, *pcptr))
8329 *pcptr = UNMAKE_THUMB_ADDR (*pcptr);
8331 /* If we have a separate 32-bit breakpoint instruction for Thumb-2,
8332 check whether we are replacing a 32-bit instruction. */
8333 if (tdep->thumb2_breakpoint != NULL)
8335 gdb_byte buf[2];
8337 if (target_read_memory (*pcptr, buf, 2) == 0)
8339 unsigned short inst1;
8341 inst1 = extract_unsigned_integer (buf, 2, byte_order_for_code);
8342 if (thumb_insn_size (inst1) == 4)
8343 return ARM_BP_KIND_THUMB2;
8347 return ARM_BP_KIND_THUMB;
8349 else
8350 return ARM_BP_KIND_ARM;
8354 /* Implement the sw_breakpoint_from_kind gdbarch method. */
8356 static const gdb_byte *
8357 arm_sw_breakpoint_from_kind (struct gdbarch *gdbarch, int kind, int *size)
8359 arm_gdbarch_tdep *tdep = (arm_gdbarch_tdep *) gdbarch_tdep (gdbarch);
8361 switch (kind)
8363 case ARM_BP_KIND_ARM:
8364 *size = tdep->arm_breakpoint_size;
8365 return tdep->arm_breakpoint;
8366 case ARM_BP_KIND_THUMB:
8367 *size = tdep->thumb_breakpoint_size;
8368 return tdep->thumb_breakpoint;
8369 case ARM_BP_KIND_THUMB2:
8370 *size = tdep->thumb2_breakpoint_size;
8371 return tdep->thumb2_breakpoint;
8372 default:
8373 gdb_assert_not_reached ("unexpected arm breakpoint kind");
8377 /* Implement the breakpoint_kind_from_current_state gdbarch method. */
8379 static int
8380 arm_breakpoint_kind_from_current_state (struct gdbarch *gdbarch,
8381 struct regcache *regcache,
8382 CORE_ADDR *pcptr)
8384 gdb_byte buf[4];
8386 /* Check the memory pointed by PC is readable. */
8387 if (target_read_memory (regcache_read_pc (regcache), buf, 4) == 0)
8389 struct arm_get_next_pcs next_pcs_ctx;
8391 arm_get_next_pcs_ctor (&next_pcs_ctx,
8392 &arm_get_next_pcs_ops,
8393 gdbarch_byte_order (gdbarch),
8394 gdbarch_byte_order_for_code (gdbarch),
8396 regcache);
8398 std::vector<CORE_ADDR> next_pcs = arm_get_next_pcs (&next_pcs_ctx);
8400 /* If MEMADDR is the next instruction of current pc, do the
8401 software single step computation, and get the thumb mode by
8402 the destination address. */
8403 for (CORE_ADDR pc : next_pcs)
8405 if (UNMAKE_THUMB_ADDR (pc) == *pcptr)
8407 if (IS_THUMB_ADDR (pc))
8409 *pcptr = MAKE_THUMB_ADDR (*pcptr);
8410 return arm_breakpoint_kind_from_pc (gdbarch, pcptr);
8412 else
8413 return ARM_BP_KIND_ARM;
8418 return arm_breakpoint_kind_from_pc (gdbarch, pcptr);
8421 /* Extract from an array REGBUF containing the (raw) register state a
8422 function return value of type TYPE, and copy that, in virtual
8423 format, into VALBUF. */
8425 static void
8426 arm_extract_return_value (struct type *type, struct regcache *regs,
8427 gdb_byte *valbuf)
8429 struct gdbarch *gdbarch = regs->arch ();
8430 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
8431 arm_gdbarch_tdep *tdep = (arm_gdbarch_tdep *) gdbarch_tdep (gdbarch);
8433 if (TYPE_CODE_FLT == type->code ())
8435 switch (tdep->fp_model)
8437 case ARM_FLOAT_FPA:
8439 /* The value is in register F0 in internal format. We need to
8440 extract the raw value and then convert it to the desired
8441 internal type. */
8442 bfd_byte tmpbuf[ARM_FP_REGISTER_SIZE];
8444 regs->cooked_read (ARM_F0_REGNUM, tmpbuf);
8445 target_float_convert (tmpbuf, arm_ext_type (gdbarch),
8446 valbuf, type);
8448 break;
8450 case ARM_FLOAT_SOFT_FPA:
8451 case ARM_FLOAT_SOFT_VFP:
8452 /* ARM_FLOAT_VFP can arise if this is a variadic function so
8453 not using the VFP ABI code. */
8454 case ARM_FLOAT_VFP:
8455 regs->cooked_read (ARM_A1_REGNUM, valbuf);
8456 if (TYPE_LENGTH (type) > 4)
8457 regs->cooked_read (ARM_A1_REGNUM + 1,
8458 valbuf + ARM_INT_REGISTER_SIZE);
8459 break;
8461 default:
8462 internal_error (__FILE__, __LINE__,
8463 _("arm_extract_return_value: "
8464 "Floating point model not supported"));
8465 break;
8468 else if (type->code () == TYPE_CODE_INT
8469 || type->code () == TYPE_CODE_CHAR
8470 || type->code () == TYPE_CODE_BOOL
8471 || type->code () == TYPE_CODE_PTR
8472 || TYPE_IS_REFERENCE (type)
8473 || type->code () == TYPE_CODE_ENUM
8474 || is_fixed_point_type (type))
8476 /* If the type is a plain integer, then the access is
8477 straight-forward. Otherwise we have to play around a bit
8478 more. */
8479 int len = TYPE_LENGTH (type);
8480 int regno = ARM_A1_REGNUM;
8481 ULONGEST tmp;
8483 while (len > 0)
8485 /* By using store_unsigned_integer we avoid having to do
8486 anything special for small big-endian values. */
8487 regcache_cooked_read_unsigned (regs, regno++, &tmp);
8488 store_unsigned_integer (valbuf,
8489 (len > ARM_INT_REGISTER_SIZE
8490 ? ARM_INT_REGISTER_SIZE : len),
8491 byte_order, tmp);
8492 len -= ARM_INT_REGISTER_SIZE;
8493 valbuf += ARM_INT_REGISTER_SIZE;
8496 else
8498 /* For a structure or union the behaviour is as if the value had
8499 been stored to word-aligned memory and then loaded into
8500 registers with 32-bit load instruction(s). */
8501 int len = TYPE_LENGTH (type);
8502 int regno = ARM_A1_REGNUM;
8503 bfd_byte tmpbuf[ARM_INT_REGISTER_SIZE];
8505 while (len > 0)
8507 regs->cooked_read (regno++, tmpbuf);
8508 memcpy (valbuf, tmpbuf,
8509 len > ARM_INT_REGISTER_SIZE ? ARM_INT_REGISTER_SIZE : len);
8510 len -= ARM_INT_REGISTER_SIZE;
8511 valbuf += ARM_INT_REGISTER_SIZE;
8517 /* Will a function return an aggregate type in memory or in a
8518 register? Return 0 if an aggregate type can be returned in a
8519 register, 1 if it must be returned in memory. */
8521 static int
8522 arm_return_in_memory (struct gdbarch *gdbarch, struct type *type)
8524 enum type_code code;
8526 type = check_typedef (type);
8528 /* Simple, non-aggregate types (ie not including vectors and
8529 complex) are always returned in a register (or registers). */
8530 code = type->code ();
8531 if (TYPE_CODE_STRUCT != code && TYPE_CODE_UNION != code
8532 && TYPE_CODE_ARRAY != code && TYPE_CODE_COMPLEX != code)
8533 return 0;
8535 if (TYPE_CODE_ARRAY == code && type->is_vector ())
8537 /* Vector values should be returned using ARM registers if they
8538 are not over 16 bytes. */
8539 return (TYPE_LENGTH (type) > 16);
8542 arm_gdbarch_tdep *tdep = (arm_gdbarch_tdep *) gdbarch_tdep (gdbarch);
8543 if (tdep->arm_abi != ARM_ABI_APCS)
8545 /* The AAPCS says all aggregates not larger than a word are returned
8546 in a register. */
8547 if (TYPE_LENGTH (type) <= ARM_INT_REGISTER_SIZE
8548 && language_pass_by_reference (type).trivially_copyable)
8549 return 0;
8551 return 1;
8553 else
8555 int nRc;
8557 /* All aggregate types that won't fit in a register must be returned
8558 in memory. */
8559 if (TYPE_LENGTH (type) > ARM_INT_REGISTER_SIZE
8560 || !language_pass_by_reference (type).trivially_copyable)
8561 return 1;
8563 /* In the ARM ABI, "integer" like aggregate types are returned in
8564 registers. For an aggregate type to be integer like, its size
8565 must be less than or equal to ARM_INT_REGISTER_SIZE and the
8566 offset of each addressable subfield must be zero. Note that bit
8567 fields are not addressable, and all addressable subfields of
8568 unions always start at offset zero.
8570 This function is based on the behaviour of GCC 2.95.1.
8571 See: gcc/arm.c: arm_return_in_memory() for details.
8573 Note: All versions of GCC before GCC 2.95.2 do not set up the
8574 parameters correctly for a function returning the following
8575 structure: struct { float f;}; This should be returned in memory,
8576 not a register. Richard Earnshaw sent me a patch, but I do not
8577 know of any way to detect if a function like the above has been
8578 compiled with the correct calling convention. */
8580 /* Assume all other aggregate types can be returned in a register.
8581 Run a check for structures, unions and arrays. */
8582 nRc = 0;
8584 if ((TYPE_CODE_STRUCT == code) || (TYPE_CODE_UNION == code))
8586 int i;
8587 /* Need to check if this struct/union is "integer" like. For
8588 this to be true, its size must be less than or equal to
8589 ARM_INT_REGISTER_SIZE and the offset of each addressable
8590 subfield must be zero. Note that bit fields are not
8591 addressable, and unions always start at offset zero. If any
8592 of the subfields is a floating point type, the struct/union
8593 cannot be an integer type. */
8595 /* For each field in the object, check:
8596 1) Is it FP? --> yes, nRc = 1;
8597 2) Is it addressable (bitpos != 0) and
8598 not packed (bitsize == 0)?
8599 --> yes, nRc = 1
8602 for (i = 0; i < type->num_fields (); i++)
8604 enum type_code field_type_code;
8606 field_type_code
8607 = check_typedef (type->field (i).type ())->code ();
8609 /* Is it a floating point type field? */
8610 if (field_type_code == TYPE_CODE_FLT)
8612 nRc = 1;
8613 break;
8616 /* If bitpos != 0, then we have to care about it. */
8617 if (type->field (i).loc_bitpos () != 0)
8619 /* Bitfields are not addressable. If the field bitsize is
8620 zero, then the field is not packed. Hence it cannot be
8621 a bitfield or any other packed type. */
8622 if (TYPE_FIELD_BITSIZE (type, i) == 0)
8624 nRc = 1;
8625 break;
8631 return nRc;
8635 /* Write into appropriate registers a function return value of type
8636 TYPE, given in virtual format. */
8638 static void
8639 arm_store_return_value (struct type *type, struct regcache *regs,
8640 const gdb_byte *valbuf)
8642 struct gdbarch *gdbarch = regs->arch ();
8643 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
8645 if (type->code () == TYPE_CODE_FLT)
8647 gdb_byte buf[ARM_FP_REGISTER_SIZE];
8648 arm_gdbarch_tdep *tdep = (arm_gdbarch_tdep *) gdbarch_tdep (gdbarch);
8650 switch (tdep->fp_model)
8652 case ARM_FLOAT_FPA:
8654 target_float_convert (valbuf, type, buf, arm_ext_type (gdbarch));
8655 regs->cooked_write (ARM_F0_REGNUM, buf);
8656 break;
8658 case ARM_FLOAT_SOFT_FPA:
8659 case ARM_FLOAT_SOFT_VFP:
8660 /* ARM_FLOAT_VFP can arise if this is a variadic function so
8661 not using the VFP ABI code. */
8662 case ARM_FLOAT_VFP:
8663 regs->cooked_write (ARM_A1_REGNUM, valbuf);
8664 if (TYPE_LENGTH (type) > 4)
8665 regs->cooked_write (ARM_A1_REGNUM + 1,
8666 valbuf + ARM_INT_REGISTER_SIZE);
8667 break;
8669 default:
8670 internal_error (__FILE__, __LINE__,
8671 _("arm_store_return_value: Floating "
8672 "point model not supported"));
8673 break;
8676 else if (type->code () == TYPE_CODE_INT
8677 || type->code () == TYPE_CODE_CHAR
8678 || type->code () == TYPE_CODE_BOOL
8679 || type->code () == TYPE_CODE_PTR
8680 || TYPE_IS_REFERENCE (type)
8681 || type->code () == TYPE_CODE_ENUM)
8683 if (TYPE_LENGTH (type) <= 4)
8685 /* Values of one word or less are zero/sign-extended and
8686 returned in r0. */
8687 bfd_byte tmpbuf[ARM_INT_REGISTER_SIZE];
8688 LONGEST val = unpack_long (type, valbuf);
8690 store_signed_integer (tmpbuf, ARM_INT_REGISTER_SIZE, byte_order, val);
8691 regs->cooked_write (ARM_A1_REGNUM, tmpbuf);
8693 else
8695 /* Integral values greater than one word are stored in consecutive
8696 registers starting with r0. This will always be a multiple of
8697 the regiser size. */
8698 int len = TYPE_LENGTH (type);
8699 int regno = ARM_A1_REGNUM;
8701 while (len > 0)
8703 regs->cooked_write (regno++, valbuf);
8704 len -= ARM_INT_REGISTER_SIZE;
8705 valbuf += ARM_INT_REGISTER_SIZE;
8709 else
8711 /* For a structure or union the behaviour is as if the value had
8712 been stored to word-aligned memory and then loaded into
8713 registers with 32-bit load instruction(s). */
8714 int len = TYPE_LENGTH (type);
8715 int regno = ARM_A1_REGNUM;
8716 bfd_byte tmpbuf[ARM_INT_REGISTER_SIZE];
8718 while (len > 0)
8720 memcpy (tmpbuf, valbuf,
8721 len > ARM_INT_REGISTER_SIZE ? ARM_INT_REGISTER_SIZE : len);
8722 regs->cooked_write (regno++, tmpbuf);
8723 len -= ARM_INT_REGISTER_SIZE;
8724 valbuf += ARM_INT_REGISTER_SIZE;
8730 /* Handle function return values. */
8732 static enum return_value_convention
8733 arm_return_value (struct gdbarch *gdbarch, struct value *function,
8734 struct type *valtype, struct regcache *regcache,
8735 gdb_byte *readbuf, const gdb_byte *writebuf)
8737 arm_gdbarch_tdep *tdep = (arm_gdbarch_tdep *) gdbarch_tdep (gdbarch);
8738 struct type *func_type = function ? value_type (function) : NULL;
8739 enum arm_vfp_cprc_base_type vfp_base_type;
8740 int vfp_base_count;
8742 if (arm_vfp_abi_for_function (gdbarch, func_type)
8743 && arm_vfp_call_candidate (valtype, &vfp_base_type, &vfp_base_count))
8745 int reg_char = arm_vfp_cprc_reg_char (vfp_base_type);
8746 int unit_length = arm_vfp_cprc_unit_length (vfp_base_type);
8747 int i;
8748 for (i = 0; i < vfp_base_count; i++)
8750 if (reg_char == 'q')
8752 if (writebuf)
8753 arm_neon_quad_write (gdbarch, regcache, i,
8754 writebuf + i * unit_length);
8756 if (readbuf)
8757 arm_neon_quad_read (gdbarch, regcache, i,
8758 readbuf + i * unit_length);
8760 else
8762 char name_buf[4];
8763 int regnum;
8765 xsnprintf (name_buf, sizeof (name_buf), "%c%d", reg_char, i);
8766 regnum = user_reg_map_name_to_regnum (gdbarch, name_buf,
8767 strlen (name_buf));
8768 if (writebuf)
8769 regcache->cooked_write (regnum, writebuf + i * unit_length);
8770 if (readbuf)
8771 regcache->cooked_read (regnum, readbuf + i * unit_length);
8774 return RETURN_VALUE_REGISTER_CONVENTION;
8777 if (valtype->code () == TYPE_CODE_STRUCT
8778 || valtype->code () == TYPE_CODE_UNION
8779 || valtype->code () == TYPE_CODE_ARRAY)
8781 /* From the AAPCS document:
8783 Result return:
8785 A Composite Type larger than 4 bytes, or whose size cannot be
8786 determined statically by both caller and callee, is stored in memory
8787 at an address passed as an extra argument when the function was
8788 called (Parameter Passing, rule A.4). The memory to be used for the
8789 result may be modified at any point during the function call.
8791 Parameter Passing:
8793 A.4: If the subroutine is a function that returns a result in memory,
8794 then the address for the result is placed in r0 and the NCRN is set
8795 to r1. */
8796 if (tdep->struct_return == pcc_struct_return
8797 || arm_return_in_memory (gdbarch, valtype))
8799 if (readbuf)
8801 CORE_ADDR addr;
8803 regcache->cooked_read (ARM_A1_REGNUM, &addr);
8804 read_memory (addr, readbuf, TYPE_LENGTH (valtype));
8806 return RETURN_VALUE_ABI_RETURNS_ADDRESS;
8809 else if (valtype->code () == TYPE_CODE_COMPLEX)
8811 if (arm_return_in_memory (gdbarch, valtype))
8812 return RETURN_VALUE_STRUCT_CONVENTION;
8815 if (writebuf)
8816 arm_store_return_value (valtype, regcache, writebuf);
8818 if (readbuf)
8819 arm_extract_return_value (valtype, regcache, readbuf);
8821 return RETURN_VALUE_REGISTER_CONVENTION;
8825 static int
8826 arm_get_longjmp_target (struct frame_info *frame, CORE_ADDR *pc)
8828 struct gdbarch *gdbarch = get_frame_arch (frame);
8829 arm_gdbarch_tdep *tdep = (arm_gdbarch_tdep *) gdbarch_tdep (gdbarch);
8830 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
8831 CORE_ADDR jb_addr;
8832 gdb_byte buf[ARM_INT_REGISTER_SIZE];
8834 jb_addr = get_frame_register_unsigned (frame, ARM_A1_REGNUM);
8836 if (target_read_memory (jb_addr + tdep->jb_pc * tdep->jb_elt_size, buf,
8837 ARM_INT_REGISTER_SIZE))
8838 return 0;
8840 *pc = extract_unsigned_integer (buf, ARM_INT_REGISTER_SIZE, byte_order);
8841 return 1;
8843 /* A call to cmse secure entry function "foo" at "a" is modified by
8844 GNU ld as "b".
8845 a) bl xxxx <foo>
8847 <foo>
8848 xxxx:
8850 b) bl yyyy <__acle_se_foo>
8852 section .gnu.sgstubs:
8853 <foo>
8854 yyyy: sg // secure gateway
8855 b.w xxxx <__acle_se_foo> // original_branch_dest
8857 <__acle_se_foo>
8858 xxxx:
8860 When the control at "b", the pc contains "yyyy" (sg address) which is a
8861 trampoline and does not exist in source code. This function returns the
8862 target pc "xxxx". For more details please refer to section 5.4
8863 (Entry functions) and section 3.4.4 (C level development flow of secure code)
8864 of "armv8-m-security-extensions-requirements-on-development-tools-engineering-specification"
8865 document on www.developer.arm.com. */
8867 static CORE_ADDR
8868 arm_skip_cmse_entry (CORE_ADDR pc, const char *name, struct objfile *objfile)
8870 int target_len = strlen (name) + strlen ("__acle_se_") + 1;
8871 char *target_name = (char *) alloca (target_len);
8872 xsnprintf (target_name, target_len, "%s%s", "__acle_se_", name);
8874 struct bound_minimal_symbol minsym
8875 = lookup_minimal_symbol (target_name, NULL, objfile);
8877 if (minsym.minsym != nullptr)
8878 return minsym.value_address ();
8880 return 0;
8883 /* Return true when SEC points to ".gnu.sgstubs" section. */
8885 static bool
8886 arm_is_sgstubs_section (struct obj_section *sec)
8888 return (sec != nullptr
8889 && sec->the_bfd_section != nullptr
8890 && sec->the_bfd_section->name != nullptr
8891 && streq (sec->the_bfd_section->name, ".gnu.sgstubs"));
8894 /* Recognize GCC and GNU ld's trampolines. If we are in a trampoline,
8895 return the target PC. Otherwise return 0. */
8897 CORE_ADDR
8898 arm_skip_stub (struct frame_info *frame, CORE_ADDR pc)
8900 const char *name;
8901 int namelen;
8902 CORE_ADDR start_addr;
8904 /* Find the starting address and name of the function containing the PC. */
8905 if (find_pc_partial_function (pc, &name, &start_addr, NULL) == 0)
8907 /* Trampoline 'bx reg' doesn't belong to any functions. Do the
8908 check here. */
8909 start_addr = arm_skip_bx_reg (frame, pc);
8910 if (start_addr != 0)
8911 return start_addr;
8913 return 0;
8916 /* If PC is in a Thumb call or return stub, return the address of the
8917 target PC, which is in a register. The thunk functions are called
8918 _call_via_xx, where x is the register name. The possible names
8919 are r0-r9, sl, fp, ip, sp, and lr. ARM RealView has similar
8920 functions, named __ARM_call_via_r[0-7]. */
8921 if (startswith (name, "_call_via_")
8922 || startswith (name, "__ARM_call_via_"))
8924 /* Use the name suffix to determine which register contains the
8925 target PC. */
8926 static const char *table[15] =
8927 {"r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
8928 "r8", "r9", "sl", "fp", "ip", "sp", "lr"
8930 int regno;
8931 int offset = strlen (name) - 2;
8933 for (regno = 0; regno <= 14; regno++)
8934 if (strcmp (&name[offset], table[regno]) == 0)
8935 return get_frame_register_unsigned (frame, regno);
8938 /* GNU ld generates __foo_from_arm or __foo_from_thumb for
8939 non-interworking calls to foo. We could decode the stubs
8940 to find the target but it's easier to use the symbol table. */
8941 namelen = strlen (name);
8942 if (name[0] == '_' && name[1] == '_'
8943 && ((namelen > 2 + strlen ("_from_thumb")
8944 && startswith (name + namelen - strlen ("_from_thumb"), "_from_thumb"))
8945 || (namelen > 2 + strlen ("_from_arm")
8946 && startswith (name + namelen - strlen ("_from_arm"), "_from_arm"))))
8948 char *target_name;
8949 int target_len = namelen - 2;
8950 struct bound_minimal_symbol minsym;
8951 struct objfile *objfile;
8952 struct obj_section *sec;
8954 if (name[namelen - 1] == 'b')
8955 target_len -= strlen ("_from_thumb");
8956 else
8957 target_len -= strlen ("_from_arm");
8959 target_name = (char *) alloca (target_len + 1);
8960 memcpy (target_name, name + 2, target_len);
8961 target_name[target_len] = '\0';
8963 sec = find_pc_section (pc);
8964 objfile = (sec == NULL) ? NULL : sec->objfile;
8965 minsym = lookup_minimal_symbol (target_name, NULL, objfile);
8966 if (minsym.minsym != NULL)
8967 return minsym.value_address ();
8968 else
8969 return 0;
8972 struct obj_section *section = find_pc_section (pc);
8974 /* Check whether SECTION points to the ".gnu.sgstubs" section. */
8975 if (arm_is_sgstubs_section (section))
8976 return arm_skip_cmse_entry (pc, name, section->objfile);
8978 return 0; /* not a stub */
8981 static void
8982 arm_update_current_architecture (void)
8984 /* If the current architecture is not ARM, we have nothing to do. */
8985 if (gdbarch_bfd_arch_info (target_gdbarch ())->arch != bfd_arch_arm)
8986 return;
8988 /* Update the architecture. */
8989 gdbarch_info info;
8990 if (!gdbarch_update_p (info))
8991 internal_error (__FILE__, __LINE__, _("could not update architecture"));
8994 static void
8995 set_fp_model_sfunc (const char *args, int from_tty,
8996 struct cmd_list_element *c)
8998 int fp_model;
9000 for (fp_model = ARM_FLOAT_AUTO; fp_model != ARM_FLOAT_LAST; fp_model++)
9001 if (strcmp (current_fp_model, fp_model_strings[fp_model]) == 0)
9003 arm_fp_model = (enum arm_float_model) fp_model;
9004 break;
9007 if (fp_model == ARM_FLOAT_LAST)
9008 internal_error (__FILE__, __LINE__, _("Invalid fp model accepted: %s."),
9009 current_fp_model);
9011 arm_update_current_architecture ();
9014 static void
9015 show_fp_model (struct ui_file *file, int from_tty,
9016 struct cmd_list_element *c, const char *value)
9018 arm_gdbarch_tdep *tdep
9019 = (arm_gdbarch_tdep *) gdbarch_tdep (target_gdbarch ());
9021 if (arm_fp_model == ARM_FLOAT_AUTO
9022 && gdbarch_bfd_arch_info (target_gdbarch ())->arch == bfd_arch_arm)
9023 gdb_printf (file, _("\
9024 The current ARM floating point model is \"auto\" (currently \"%s\").\n"),
9025 fp_model_strings[tdep->fp_model]);
9026 else
9027 gdb_printf (file, _("\
9028 The current ARM floating point model is \"%s\".\n"),
9029 fp_model_strings[arm_fp_model]);
9032 static void
9033 arm_set_abi (const char *args, int from_tty,
9034 struct cmd_list_element *c)
9036 int arm_abi;
9038 for (arm_abi = ARM_ABI_AUTO; arm_abi != ARM_ABI_LAST; arm_abi++)
9039 if (strcmp (arm_abi_string, arm_abi_strings[arm_abi]) == 0)
9041 arm_abi_global = (enum arm_abi_kind) arm_abi;
9042 break;
9045 if (arm_abi == ARM_ABI_LAST)
9046 internal_error (__FILE__, __LINE__, _("Invalid ABI accepted: %s."),
9047 arm_abi_string);
9049 arm_update_current_architecture ();
9052 static void
9053 arm_show_abi (struct ui_file *file, int from_tty,
9054 struct cmd_list_element *c, const char *value)
9056 arm_gdbarch_tdep *tdep
9057 = (arm_gdbarch_tdep *) gdbarch_tdep (target_gdbarch ());
9059 if (arm_abi_global == ARM_ABI_AUTO
9060 && gdbarch_bfd_arch_info (target_gdbarch ())->arch == bfd_arch_arm)
9061 gdb_printf (file, _("\
9062 The current ARM ABI is \"auto\" (currently \"%s\").\n"),
9063 arm_abi_strings[tdep->arm_abi]);
9064 else
9065 gdb_printf (file, _("The current ARM ABI is \"%s\".\n"),
9066 arm_abi_string);
9069 static void
9070 arm_show_fallback_mode (struct ui_file *file, int from_tty,
9071 struct cmd_list_element *c, const char *value)
9073 gdb_printf (file,
9074 _("The current execution mode assumed "
9075 "(when symbols are unavailable) is \"%s\".\n"),
9076 arm_fallback_mode_string);
9079 static void
9080 arm_show_force_mode (struct ui_file *file, int from_tty,
9081 struct cmd_list_element *c, const char *value)
9083 gdb_printf (file,
9084 _("The current execution mode assumed "
9085 "(even when symbols are available) is \"%s\".\n"),
9086 arm_force_mode_string);
9089 static void
9090 arm_show_unwind_secure_frames (struct ui_file *file, int from_tty,
9091 struct cmd_list_element *c, const char *value)
9093 gdb_printf (file,
9094 _("Usage of non-secure to secure exception stack unwinding is %s.\n"),
9095 arm_unwind_secure_frames ? "on" : "off");
9098 /* If the user changes the register disassembly style used for info
9099 register and other commands, we have to also switch the style used
9100 in opcodes for disassembly output. This function is run in the "set
9101 arm disassembly" command, and does that. */
9103 static void
9104 set_disassembly_style_sfunc (const char *args, int from_tty,
9105 struct cmd_list_element *c)
9107 /* Convert the short style name into the long style name (eg, reg-names-*)
9108 before calling the generic set_disassembler_options() function. */
9109 std::string long_name = std::string ("reg-names-") + disassembly_style;
9110 set_disassembler_options (&long_name[0]);
9113 static void
9114 show_disassembly_style_sfunc (struct ui_file *file, int from_tty,
9115 struct cmd_list_element *c, const char *value)
9117 struct gdbarch *gdbarch = get_current_arch ();
9118 char *options = get_disassembler_options (gdbarch);
9119 const char *style = "";
9120 int len = 0;
9121 const char *opt;
9123 FOR_EACH_DISASSEMBLER_OPTION (opt, options)
9124 if (startswith (opt, "reg-names-"))
9126 style = &opt[strlen ("reg-names-")];
9127 len = strcspn (style, ",");
9130 gdb_printf (file, "The disassembly style is \"%.*s\".\n", len, style);
9133 /* Return the ARM register name corresponding to register I. */
9134 static const char *
9135 arm_register_name (struct gdbarch *gdbarch, int i)
9137 arm_gdbarch_tdep *tdep = (arm_gdbarch_tdep *) gdbarch_tdep (gdbarch);
9139 if (is_s_pseudo (gdbarch, i))
9141 static const char *const s_pseudo_names[] = {
9142 "s0", "s1", "s2", "s3", "s4", "s5", "s6", "s7",
9143 "s8", "s9", "s10", "s11", "s12", "s13", "s14", "s15",
9144 "s16", "s17", "s18", "s19", "s20", "s21", "s22", "s23",
9145 "s24", "s25", "s26", "s27", "s28", "s29", "s30", "s31",
9148 return s_pseudo_names[i - tdep->s_pseudo_base];
9151 if (is_q_pseudo (gdbarch, i))
9153 static const char *const q_pseudo_names[] = {
9154 "q0", "q1", "q2", "q3", "q4", "q5", "q6", "q7",
9155 "q8", "q9", "q10", "q11", "q12", "q13", "q14", "q15",
9158 return q_pseudo_names[i - tdep->q_pseudo_base];
9161 if (is_mve_pseudo (gdbarch, i))
9162 return "p0";
9164 /* RA_AUTH_CODE is used for unwinding only. Do not assign it a name. */
9165 if (is_pacbti_pseudo (gdbarch, i))
9166 return "";
9168 if (i >= ARRAY_SIZE (arm_register_names))
9169 /* These registers are only supported on targets which supply
9170 an XML description. */
9171 return "";
9173 /* Non-pseudo registers. */
9174 return arm_register_names[i];
9177 /* Test whether the coff symbol specific value corresponds to a Thumb
9178 function. */
9180 static int
9181 coff_sym_is_thumb (int val)
9183 return (val == C_THUMBEXT
9184 || val == C_THUMBSTAT
9185 || val == C_THUMBEXTFUNC
9186 || val == C_THUMBSTATFUNC
9187 || val == C_THUMBLABEL);
9190 /* arm_coff_make_msymbol_special()
9191 arm_elf_make_msymbol_special()
9193 These functions test whether the COFF or ELF symbol corresponds to
9194 an address in thumb code, and set a "special" bit in a minimal
9195 symbol to indicate that it does. */
9197 static void
9198 arm_elf_make_msymbol_special(asymbol *sym, struct minimal_symbol *msym)
9200 elf_symbol_type *elfsym = (elf_symbol_type *) sym;
9202 if (ARM_GET_SYM_BRANCH_TYPE (elfsym->internal_elf_sym.st_target_internal)
9203 == ST_BRANCH_TO_THUMB)
9204 MSYMBOL_SET_SPECIAL (msym);
9207 static void
9208 arm_coff_make_msymbol_special(int val, struct minimal_symbol *msym)
9210 if (coff_sym_is_thumb (val))
9211 MSYMBOL_SET_SPECIAL (msym);
9214 static void
9215 arm_record_special_symbol (struct gdbarch *gdbarch, struct objfile *objfile,
9216 asymbol *sym)
9218 const char *name = bfd_asymbol_name (sym);
9219 struct arm_per_bfd *data;
9220 struct arm_mapping_symbol new_map_sym;
9222 gdb_assert (name[0] == '$');
9223 if (name[1] != 'a' && name[1] != 't' && name[1] != 'd')
9224 return;
9226 data = arm_bfd_data_key.get (objfile->obfd);
9227 if (data == NULL)
9228 data = arm_bfd_data_key.emplace (objfile->obfd,
9229 objfile->obfd->section_count);
9230 arm_mapping_symbol_vec &map
9231 = data->section_maps[bfd_asymbol_section (sym)->index];
9233 new_map_sym.value = sym->value;
9234 new_map_sym.type = name[1];
9236 /* Insert at the end, the vector will be sorted on first use. */
9237 map.push_back (new_map_sym);
9240 static void
9241 arm_write_pc (struct regcache *regcache, CORE_ADDR pc)
9243 struct gdbarch *gdbarch = regcache->arch ();
9244 regcache_cooked_write_unsigned (regcache, ARM_PC_REGNUM, pc);
9246 /* If necessary, set the T bit. */
9247 if (arm_apcs_32)
9249 ULONGEST val, t_bit;
9250 regcache_cooked_read_unsigned (regcache, ARM_PS_REGNUM, &val);
9251 t_bit = arm_psr_thumb_bit (gdbarch);
9252 if (arm_pc_is_thumb (gdbarch, pc))
9253 regcache_cooked_write_unsigned (regcache, ARM_PS_REGNUM,
9254 val | t_bit);
9255 else
9256 regcache_cooked_write_unsigned (regcache, ARM_PS_REGNUM,
9257 val & ~t_bit);
9261 /* Read the contents of a NEON quad register, by reading from two
9262 double registers. This is used to implement the quad pseudo
9263 registers, and for argument passing in case the quad registers are
9264 missing; vectors are passed in quad registers when using the VFP
9265 ABI, even if a NEON unit is not present. REGNUM is the index of
9266 the quad register, in [0, 15]. */
9268 static enum register_status
9269 arm_neon_quad_read (struct gdbarch *gdbarch, readable_regcache *regcache,
9270 int regnum, gdb_byte *buf)
9272 char name_buf[4];
9273 gdb_byte reg_buf[8];
9274 int offset, double_regnum;
9275 enum register_status status;
9277 xsnprintf (name_buf, sizeof (name_buf), "d%d", regnum << 1);
9278 double_regnum = user_reg_map_name_to_regnum (gdbarch, name_buf,
9279 strlen (name_buf));
9281 /* d0 is always the least significant half of q0. */
9282 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
9283 offset = 8;
9284 else
9285 offset = 0;
9287 status = regcache->raw_read (double_regnum, reg_buf);
9288 if (status != REG_VALID)
9289 return status;
9290 memcpy (buf + offset, reg_buf, 8);
9292 offset = 8 - offset;
9293 status = regcache->raw_read (double_regnum + 1, reg_buf);
9294 if (status != REG_VALID)
9295 return status;
9296 memcpy (buf + offset, reg_buf, 8);
9298 return REG_VALID;
9301 /* Read the contents of the MVE pseudo register REGNUM and store it
9302 in BUF. */
9304 static enum register_status
9305 arm_mve_pseudo_read (struct gdbarch *gdbarch, readable_regcache *regcache,
9306 int regnum, gdb_byte *buf)
9308 arm_gdbarch_tdep *tdep = (arm_gdbarch_tdep *) gdbarch_tdep (gdbarch);
9310 /* P0 is the first 16 bits of VPR. */
9311 return regcache->raw_read_part (tdep->mve_vpr_regnum, 0, 2, buf);
9314 static enum register_status
9315 arm_pseudo_read (struct gdbarch *gdbarch, readable_regcache *regcache,
9316 int regnum, gdb_byte *buf)
9318 const int num_regs = gdbarch_num_regs (gdbarch);
9319 char name_buf[4];
9320 gdb_byte reg_buf[8];
9321 int offset, double_regnum;
9322 arm_gdbarch_tdep *tdep = (arm_gdbarch_tdep *) gdbarch_tdep (gdbarch);
9324 gdb_assert (regnum >= num_regs);
9326 if (is_q_pseudo (gdbarch, regnum))
9328 /* Quad-precision register. */
9329 return arm_neon_quad_read (gdbarch, regcache,
9330 regnum - tdep->q_pseudo_base, buf);
9332 else if (is_mve_pseudo (gdbarch, regnum))
9333 return arm_mve_pseudo_read (gdbarch, regcache, regnum, buf);
9334 else
9336 enum register_status status;
9338 regnum -= tdep->s_pseudo_base;
9339 /* Single-precision register. */
9340 gdb_assert (regnum < 32);
9342 /* s0 is always the least significant half of d0. */
9343 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
9344 offset = (regnum & 1) ? 0 : 4;
9345 else
9346 offset = (regnum & 1) ? 4 : 0;
9348 xsnprintf (name_buf, sizeof (name_buf), "d%d", regnum >> 1);
9349 double_regnum = user_reg_map_name_to_regnum (gdbarch, name_buf,
9350 strlen (name_buf));
9352 status = regcache->raw_read (double_regnum, reg_buf);
9353 if (status == REG_VALID)
9354 memcpy (buf, reg_buf + offset, 4);
9355 return status;
9359 /* Store the contents of BUF to a NEON quad register, by writing to
9360 two double registers. This is used to implement the quad pseudo
9361 registers, and for argument passing in case the quad registers are
9362 missing; vectors are passed in quad registers when using the VFP
9363 ABI, even if a NEON unit is not present. REGNUM is the index
9364 of the quad register, in [0, 15]. */
9366 static void
9367 arm_neon_quad_write (struct gdbarch *gdbarch, struct regcache *regcache,
9368 int regnum, const gdb_byte *buf)
9370 char name_buf[4];
9371 int offset, double_regnum;
9373 xsnprintf (name_buf, sizeof (name_buf), "d%d", regnum << 1);
9374 double_regnum = user_reg_map_name_to_regnum (gdbarch, name_buf,
9375 strlen (name_buf));
9377 /* d0 is always the least significant half of q0. */
9378 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
9379 offset = 8;
9380 else
9381 offset = 0;
9383 regcache->raw_write (double_regnum, buf + offset);
9384 offset = 8 - offset;
9385 regcache->raw_write (double_regnum + 1, buf + offset);
9388 /* Store the contents of BUF to the MVE pseudo register REGNUM. */
9390 static void
9391 arm_mve_pseudo_write (struct gdbarch *gdbarch, struct regcache *regcache,
9392 int regnum, const gdb_byte *buf)
9394 arm_gdbarch_tdep *tdep = (arm_gdbarch_tdep *) gdbarch_tdep (gdbarch);
9396 /* P0 is the first 16 bits of VPR. */
9397 regcache->raw_write_part (tdep->mve_vpr_regnum, 0, 2, buf);
9400 static void
9401 arm_pseudo_write (struct gdbarch *gdbarch, struct regcache *regcache,
9402 int regnum, const gdb_byte *buf)
9404 const int num_regs = gdbarch_num_regs (gdbarch);
9405 char name_buf[4];
9406 gdb_byte reg_buf[8];
9407 int offset, double_regnum;
9408 arm_gdbarch_tdep *tdep = (arm_gdbarch_tdep *) gdbarch_tdep (gdbarch);
9410 gdb_assert (regnum >= num_regs);
9412 if (is_q_pseudo (gdbarch, regnum))
9414 /* Quad-precision register. */
9415 arm_neon_quad_write (gdbarch, regcache,
9416 regnum - tdep->q_pseudo_base, buf);
9418 else if (is_mve_pseudo (gdbarch, regnum))
9419 arm_mve_pseudo_write (gdbarch, regcache, regnum, buf);
9420 else
9422 regnum -= tdep->s_pseudo_base;
9423 /* Single-precision register. */
9424 gdb_assert (regnum < 32);
9426 /* s0 is always the least significant half of d0. */
9427 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
9428 offset = (regnum & 1) ? 0 : 4;
9429 else
9430 offset = (regnum & 1) ? 4 : 0;
9432 xsnprintf (name_buf, sizeof (name_buf), "d%d", regnum >> 1);
9433 double_regnum = user_reg_map_name_to_regnum (gdbarch, name_buf,
9434 strlen (name_buf));
9436 regcache->raw_read (double_regnum, reg_buf);
9437 memcpy (reg_buf + offset, buf, 4);
9438 regcache->raw_write (double_regnum, reg_buf);
9442 static struct value *
9443 value_of_arm_user_reg (struct frame_info *frame, const void *baton)
9445 const int *reg_p = (const int *) baton;
9446 return value_of_register (*reg_p, frame);
9449 static enum gdb_osabi
9450 arm_elf_osabi_sniffer (bfd *abfd)
9452 unsigned int elfosabi;
9453 enum gdb_osabi osabi = GDB_OSABI_UNKNOWN;
9455 elfosabi = elf_elfheader (abfd)->e_ident[EI_OSABI];
9457 if (elfosabi == ELFOSABI_ARM)
9458 /* GNU tools use this value. Check note sections in this case,
9459 as well. */
9461 for (asection *sect : gdb_bfd_sections (abfd))
9462 generic_elf_osabi_sniff_abi_tag_sections (abfd, sect, &osabi);
9465 /* Anything else will be handled by the generic ELF sniffer. */
9466 return osabi;
9469 static int
9470 arm_register_reggroup_p (struct gdbarch *gdbarch, int regnum,
9471 const struct reggroup *group)
9473 /* FPS register's type is INT, but belongs to float_reggroup. Beside
9474 this, FPS register belongs to save_regroup, restore_reggroup, and
9475 all_reggroup, of course. */
9476 if (regnum == ARM_FPS_REGNUM)
9477 return (group == float_reggroup
9478 || group == save_reggroup
9479 || group == restore_reggroup
9480 || group == all_reggroup);
9481 else
9482 return default_register_reggroup_p (gdbarch, regnum, group);
9485 /* For backward-compatibility we allow two 'g' packet lengths with
9486 the remote protocol depending on whether FPA registers are
9487 supplied. M-profile targets do not have FPA registers, but some
9488 stubs already exist in the wild which use a 'g' packet which
9489 supplies them albeit with dummy values. The packet format which
9490 includes FPA registers should be considered deprecated for
9491 M-profile targets. */
9493 static void
9494 arm_register_g_packet_guesses (struct gdbarch *gdbarch)
9496 arm_gdbarch_tdep *tdep = (arm_gdbarch_tdep *) gdbarch_tdep (gdbarch);
9498 if (tdep->is_m)
9500 const target_desc *tdesc;
9502 /* If we know from the executable this is an M-profile target,
9503 cater for remote targets whose register set layout is the
9504 same as the FPA layout. */
9505 tdesc = arm_read_mprofile_description (ARM_M_TYPE_WITH_FPA);
9506 register_remote_g_packet_guess (gdbarch,
9507 ARM_CORE_REGS_SIZE + ARM_FP_REGS_SIZE,
9508 tdesc);
9510 /* The regular M-profile layout. */
9511 tdesc = arm_read_mprofile_description (ARM_M_TYPE_M_PROFILE);
9512 register_remote_g_packet_guess (gdbarch, ARM_CORE_REGS_SIZE,
9513 tdesc);
9515 /* M-profile plus M4F VFP. */
9516 tdesc = arm_read_mprofile_description (ARM_M_TYPE_VFP_D16);
9517 register_remote_g_packet_guess (gdbarch,
9518 ARM_CORE_REGS_SIZE + ARM_VFP2_REGS_SIZE,
9519 tdesc);
9520 /* M-profile plus MVE. */
9521 tdesc = arm_read_mprofile_description (ARM_M_TYPE_MVE);
9522 register_remote_g_packet_guess (gdbarch, ARM_CORE_REGS_SIZE
9523 + ARM_VFP2_REGS_SIZE
9524 + ARM_INT_REGISTER_SIZE, tdesc);
9526 /* M-profile system (stack pointers). */
9527 tdesc = arm_read_mprofile_description (ARM_M_TYPE_SYSTEM);
9528 register_remote_g_packet_guess (gdbarch, 2 * ARM_INT_REGISTER_SIZE, tdesc);
9531 /* Otherwise we don't have a useful guess. */
9534 /* Implement the code_of_frame_writable gdbarch method. */
9536 static int
9537 arm_code_of_frame_writable (struct gdbarch *gdbarch, struct frame_info *frame)
9539 arm_gdbarch_tdep *tdep = (arm_gdbarch_tdep *) gdbarch_tdep (gdbarch);
9541 if (tdep->is_m && get_frame_type (frame) == SIGTRAMP_FRAME)
9543 /* M-profile exception frames return to some magic PCs, where
9544 isn't writable at all. */
9545 return 0;
9547 else
9548 return 1;
9551 /* Implement gdbarch_gnu_triplet_regexp. If the arch name is arm then allow it
9552 to be postfixed by a version (eg armv7hl). */
9554 static const char *
9555 arm_gnu_triplet_regexp (struct gdbarch *gdbarch)
9557 if (strcmp (gdbarch_bfd_arch_info (gdbarch)->arch_name, "arm") == 0)
9558 return "arm(v[^- ]*)?";
9559 return gdbarch_bfd_arch_info (gdbarch)->arch_name;
9562 /* Implement the "get_pc_address_flags" gdbarch method. */
9564 static std::string
9565 arm_get_pc_address_flags (frame_info *frame, CORE_ADDR pc)
9567 if (get_frame_pc_masked (frame))
9568 return "PAC";
9570 return "";
9573 /* Initialize the current architecture based on INFO. If possible,
9574 re-use an architecture from ARCHES, which is a list of
9575 architectures already created during this debugging session.
9577 Called e.g. at program startup, when reading a core file, and when
9578 reading a binary file. */
9580 static struct gdbarch *
9581 arm_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
9583 struct gdbarch *gdbarch;
9584 struct gdbarch_list *best_arch;
9585 enum arm_abi_kind arm_abi = arm_abi_global;
9586 enum arm_float_model fp_model = arm_fp_model;
9587 tdesc_arch_data_up tdesc_data;
9588 int i;
9589 bool is_m = false;
9590 bool have_sec_ext = false;
9591 int vfp_register_count = 0;
9592 bool have_s_pseudos = false, have_q_pseudos = false;
9593 bool have_wmmx_registers = false;
9594 bool have_neon = false;
9595 bool have_fpa_registers = true;
9596 const struct target_desc *tdesc = info.target_desc;
9597 bool have_vfp = false;
9598 bool have_mve = false;
9599 bool have_pacbti = false;
9600 int mve_vpr_regnum = -1;
9601 int register_count = ARM_NUM_REGS;
9602 bool have_m_profile_msp = false;
9603 int m_profile_msp_regnum = -1;
9604 int m_profile_psp_regnum = -1;
9605 int m_profile_msp_ns_regnum = -1;
9606 int m_profile_psp_ns_regnum = -1;
9607 int m_profile_msp_s_regnum = -1;
9608 int m_profile_psp_s_regnum = -1;
9609 int tls_regnum = 0;
9611 /* If we have an object to base this architecture on, try to determine
9612 its ABI. */
9614 if (arm_abi == ARM_ABI_AUTO && info.abfd != NULL)
9616 int ei_osabi, e_flags;
9618 switch (bfd_get_flavour (info.abfd))
9620 case bfd_target_coff_flavour:
9621 /* Assume it's an old APCS-style ABI. */
9622 /* XXX WinCE? */
9623 arm_abi = ARM_ABI_APCS;
9624 break;
9626 case bfd_target_elf_flavour:
9627 ei_osabi = elf_elfheader (info.abfd)->e_ident[EI_OSABI];
9628 e_flags = elf_elfheader (info.abfd)->e_flags;
9630 if (ei_osabi == ELFOSABI_ARM)
9632 /* GNU tools used to use this value, but do not for EABI
9633 objects. There's nowhere to tag an EABI version
9634 anyway, so assume APCS. */
9635 arm_abi = ARM_ABI_APCS;
9637 else if (ei_osabi == ELFOSABI_NONE || ei_osabi == ELFOSABI_GNU)
9639 int eabi_ver = EF_ARM_EABI_VERSION (e_flags);
9641 switch (eabi_ver)
9643 case EF_ARM_EABI_UNKNOWN:
9644 /* Assume GNU tools. */
9645 arm_abi = ARM_ABI_APCS;
9646 break;
9648 case EF_ARM_EABI_VER4:
9649 case EF_ARM_EABI_VER5:
9650 arm_abi = ARM_ABI_AAPCS;
9651 /* EABI binaries default to VFP float ordering.
9652 They may also contain build attributes that can
9653 be used to identify if the VFP argument-passing
9654 ABI is in use. */
9655 if (fp_model == ARM_FLOAT_AUTO)
9657 #ifdef HAVE_ELF
9658 switch (bfd_elf_get_obj_attr_int (info.abfd,
9659 OBJ_ATTR_PROC,
9660 Tag_ABI_VFP_args))
9662 case AEABI_VFP_args_base:
9663 /* "The user intended FP parameter/result
9664 passing to conform to AAPCS, base
9665 variant". */
9666 fp_model = ARM_FLOAT_SOFT_VFP;
9667 break;
9668 case AEABI_VFP_args_vfp:
9669 /* "The user intended FP parameter/result
9670 passing to conform to AAPCS, VFP
9671 variant". */
9672 fp_model = ARM_FLOAT_VFP;
9673 break;
9674 case AEABI_VFP_args_toolchain:
9675 /* "The user intended FP parameter/result
9676 passing to conform to tool chain-specific
9677 conventions" - we don't know any such
9678 conventions, so leave it as "auto". */
9679 break;
9680 case AEABI_VFP_args_compatible:
9681 /* "Code is compatible with both the base
9682 and VFP variants; the user did not permit
9683 non-variadic functions to pass FP
9684 parameters/results" - leave it as
9685 "auto". */
9686 break;
9687 default:
9688 /* Attribute value not mentioned in the
9689 November 2012 ABI, so leave it as
9690 "auto". */
9691 break;
9693 #else
9694 fp_model = ARM_FLOAT_SOFT_VFP;
9695 #endif
9697 break;
9699 default:
9700 /* Leave it as "auto". */
9701 warning (_("unknown ARM EABI version 0x%x"), eabi_ver);
9702 break;
9705 #ifdef HAVE_ELF
9706 /* Detect M-profile programs. This only works if the
9707 executable file includes build attributes; GCC does
9708 copy them to the executable, but e.g. RealView does
9709 not. */
9710 int attr_arch
9711 = bfd_elf_get_obj_attr_int (info.abfd, OBJ_ATTR_PROC,
9712 Tag_CPU_arch);
9713 int attr_profile
9714 = bfd_elf_get_obj_attr_int (info.abfd, OBJ_ATTR_PROC,
9715 Tag_CPU_arch_profile);
9717 /* GCC specifies the profile for v6-M; RealView only
9718 specifies the profile for architectures starting with
9719 V7 (as opposed to architectures with a tag
9720 numerically greater than TAG_CPU_ARCH_V7). */
9721 if (!tdesc_has_registers (tdesc)
9722 && (attr_arch == TAG_CPU_ARCH_V6_M
9723 || attr_arch == TAG_CPU_ARCH_V6S_M
9724 || attr_arch == TAG_CPU_ARCH_V7E_M
9725 || attr_arch == TAG_CPU_ARCH_V8M_BASE
9726 || attr_arch == TAG_CPU_ARCH_V8M_MAIN
9727 || attr_arch == TAG_CPU_ARCH_V8_1M_MAIN
9728 || attr_profile == 'M'))
9729 is_m = true;
9731 /* Look for attributes that indicate support for ARMv8.1-m
9732 PACBTI. */
9733 if (!tdesc_has_registers (tdesc) && is_m)
9735 int attr_pac_extension
9736 = bfd_elf_get_obj_attr_int (info.abfd, OBJ_ATTR_PROC,
9737 Tag_PAC_extension);
9739 int attr_bti_extension
9740 = bfd_elf_get_obj_attr_int (info.abfd, OBJ_ATTR_PROC,
9741 Tag_BTI_extension);
9743 int attr_pacret_use
9744 = bfd_elf_get_obj_attr_int (info.abfd, OBJ_ATTR_PROC,
9745 Tag_PACRET_use);
9747 int attr_bti_use
9748 = bfd_elf_get_obj_attr_int (info.abfd, OBJ_ATTR_PROC,
9749 Tag_BTI_use);
9751 if (attr_pac_extension != 0 || attr_bti_extension != 0
9752 || attr_pacret_use != 0 || attr_bti_use != 0)
9753 have_pacbti = true;
9755 #endif
9758 if (fp_model == ARM_FLOAT_AUTO)
9760 switch (e_flags & (EF_ARM_SOFT_FLOAT | EF_ARM_VFP_FLOAT))
9762 case 0:
9763 /* Leave it as "auto". Strictly speaking this case
9764 means FPA, but almost nobody uses that now, and
9765 many toolchains fail to set the appropriate bits
9766 for the floating-point model they use. */
9767 break;
9768 case EF_ARM_SOFT_FLOAT:
9769 fp_model = ARM_FLOAT_SOFT_FPA;
9770 break;
9771 case EF_ARM_VFP_FLOAT:
9772 fp_model = ARM_FLOAT_VFP;
9773 break;
9774 case EF_ARM_SOFT_FLOAT | EF_ARM_VFP_FLOAT:
9775 fp_model = ARM_FLOAT_SOFT_VFP;
9776 break;
9780 if (e_flags & EF_ARM_BE8)
9781 info.byte_order_for_code = BFD_ENDIAN_LITTLE;
9783 break;
9785 default:
9786 /* Leave it as "auto". */
9787 break;
9791 /* Check any target description for validity. */
9792 if (tdesc_has_registers (tdesc))
9794 /* For most registers we require GDB's default names; but also allow
9795 the numeric names for sp / lr / pc, as a convenience. */
9796 static const char *const arm_sp_names[] = { "r13", "sp", NULL };
9797 static const char *const arm_lr_names[] = { "r14", "lr", NULL };
9798 static const char *const arm_pc_names[] = { "r15", "pc", NULL };
9800 const struct tdesc_feature *feature;
9801 int valid_p;
9803 feature = tdesc_find_feature (tdesc,
9804 "org.gnu.gdb.arm.core");
9805 if (feature == NULL)
9807 feature = tdesc_find_feature (tdesc,
9808 "org.gnu.gdb.arm.m-profile");
9809 if (feature == NULL)
9810 return NULL;
9811 else
9812 is_m = true;
9815 tdesc_data = tdesc_data_alloc ();
9817 valid_p = 1;
9818 for (i = 0; i < ARM_SP_REGNUM; i++)
9819 valid_p &= tdesc_numbered_register (feature, tdesc_data.get (), i,
9820 arm_register_names[i]);
9821 valid_p &= tdesc_numbered_register_choices (feature, tdesc_data.get (),
9822 ARM_SP_REGNUM,
9823 arm_sp_names);
9824 valid_p &= tdesc_numbered_register_choices (feature, tdesc_data.get (),
9825 ARM_LR_REGNUM,
9826 arm_lr_names);
9827 valid_p &= tdesc_numbered_register_choices (feature, tdesc_data.get (),
9828 ARM_PC_REGNUM,
9829 arm_pc_names);
9830 if (is_m)
9831 valid_p &= tdesc_numbered_register (feature, tdesc_data.get (),
9832 ARM_PS_REGNUM, "xpsr");
9833 else
9834 valid_p &= tdesc_numbered_register (feature, tdesc_data.get (),
9835 ARM_PS_REGNUM, "cpsr");
9837 if (!valid_p)
9838 return NULL;
9840 if (is_m)
9842 feature = tdesc_find_feature (tdesc,
9843 "org.gnu.gdb.arm.m-system");
9844 if (feature != nullptr)
9846 /* MSP */
9847 valid_p &= tdesc_numbered_register (feature, tdesc_data.get (),
9848 register_count, "msp");
9849 if (!valid_p)
9851 warning (_("M-profile m-system feature is missing required register msp."));
9852 return nullptr;
9854 have_m_profile_msp = true;
9855 m_profile_msp_regnum = register_count++;
9857 /* PSP */
9858 valid_p &= tdesc_numbered_register (feature, tdesc_data.get (),
9859 register_count, "psp");
9860 if (!valid_p)
9862 warning (_("M-profile m-system feature is missing required register psp."));
9863 return nullptr;
9865 m_profile_psp_regnum = register_count++;
9869 feature = tdesc_find_feature (tdesc,
9870 "org.gnu.gdb.arm.fpa");
9871 if (feature != NULL)
9873 valid_p = 1;
9874 for (i = ARM_F0_REGNUM; i <= ARM_FPS_REGNUM; i++)
9875 valid_p &= tdesc_numbered_register (feature, tdesc_data.get (), i,
9876 arm_register_names[i]);
9877 if (!valid_p)
9878 return NULL;
9880 else
9881 have_fpa_registers = false;
9883 feature = tdesc_find_feature (tdesc,
9884 "org.gnu.gdb.xscale.iwmmxt");
9885 if (feature != NULL)
9887 static const char *const iwmmxt_names[] = {
9888 "wR0", "wR1", "wR2", "wR3", "wR4", "wR5", "wR6", "wR7",
9889 "wR8", "wR9", "wR10", "wR11", "wR12", "wR13", "wR14", "wR15",
9890 "wCID", "wCon", "wCSSF", "wCASF", "", "", "", "",
9891 "wCGR0", "wCGR1", "wCGR2", "wCGR3", "", "", "", "",
9894 valid_p = 1;
9895 for (i = ARM_WR0_REGNUM; i <= ARM_WR15_REGNUM; i++)
9896 valid_p
9897 &= tdesc_numbered_register (feature, tdesc_data.get (), i,
9898 iwmmxt_names[i - ARM_WR0_REGNUM]);
9900 /* Check for the control registers, but do not fail if they
9901 are missing. */
9902 for (i = ARM_WC0_REGNUM; i <= ARM_WCASF_REGNUM; i++)
9903 tdesc_numbered_register (feature, tdesc_data.get (), i,
9904 iwmmxt_names[i - ARM_WR0_REGNUM]);
9906 for (i = ARM_WCGR0_REGNUM; i <= ARM_WCGR3_REGNUM; i++)
9907 valid_p
9908 &= tdesc_numbered_register (feature, tdesc_data.get (), i,
9909 iwmmxt_names[i - ARM_WR0_REGNUM]);
9911 if (!valid_p)
9912 return NULL;
9914 have_wmmx_registers = true;
9917 /* If we have a VFP unit, check whether the single precision registers
9918 are present. If not, then we will synthesize them as pseudo
9919 registers. */
9920 feature = tdesc_find_feature (tdesc,
9921 "org.gnu.gdb.arm.vfp");
9922 if (feature != NULL)
9924 static const char *const vfp_double_names[] = {
9925 "d0", "d1", "d2", "d3", "d4", "d5", "d6", "d7",
9926 "d8", "d9", "d10", "d11", "d12", "d13", "d14", "d15",
9927 "d16", "d17", "d18", "d19", "d20", "d21", "d22", "d23",
9928 "d24", "d25", "d26", "d27", "d28", "d29", "d30", "d31",
9931 /* Require the double precision registers. There must be either
9932 16 or 32. */
9933 valid_p = 1;
9934 for (i = 0; i < 32; i++)
9936 valid_p &= tdesc_numbered_register (feature, tdesc_data.get (),
9937 ARM_D0_REGNUM + i,
9938 vfp_double_names[i]);
9939 if (!valid_p)
9940 break;
9942 if (!valid_p && i == 16)
9943 valid_p = 1;
9945 /* Also require FPSCR. */
9946 valid_p &= tdesc_numbered_register (feature, tdesc_data.get (),
9947 ARM_FPSCR_REGNUM, "fpscr");
9948 if (!valid_p)
9949 return NULL;
9951 have_vfp = true;
9953 if (tdesc_unnumbered_register (feature, "s0") == 0)
9954 have_s_pseudos = true;
9956 vfp_register_count = i;
9958 /* If we have VFP, also check for NEON. The architecture allows
9959 NEON without VFP (integer vector operations only), but GDB
9960 does not support that. */
9961 feature = tdesc_find_feature (tdesc,
9962 "org.gnu.gdb.arm.neon");
9963 if (feature != NULL)
9965 /* NEON requires 32 double-precision registers. */
9966 if (i != 32)
9967 return NULL;
9969 /* If there are quad registers defined by the stub, use
9970 their type; otherwise (normally) provide them with
9971 the default type. */
9972 if (tdesc_unnumbered_register (feature, "q0") == 0)
9973 have_q_pseudos = true;
9977 /* Check for the TLS register feature. */
9978 feature = tdesc_find_feature (tdesc, "org.gnu.gdb.arm.tls");
9979 if (feature != nullptr)
9981 valid_p &= tdesc_numbered_register (feature, tdesc_data.get (),
9982 register_count, "tpidruro");
9983 if (!valid_p)
9984 return nullptr;
9986 tls_regnum = register_count;
9987 register_count++;
9990 /* Check for MVE after all the checks for GPR's, VFP and Neon.
9991 MVE (Helium) is an M-profile extension. */
9992 if (is_m)
9994 /* Do we have the MVE feature? */
9995 feature = tdesc_find_feature (tdesc,"org.gnu.gdb.arm.m-profile-mve");
9997 if (feature != nullptr)
9999 /* If we have MVE, we must always have the VPR register. */
10000 valid_p &= tdesc_numbered_register (feature, tdesc_data.get (),
10001 register_count, "vpr");
10002 if (!valid_p)
10004 warning (_("MVE feature is missing required register vpr."));
10005 return nullptr;
10008 have_mve = true;
10009 mve_vpr_regnum = register_count;
10010 register_count++;
10012 /* We can't have Q pseudo registers available here, as that
10013 would mean we have NEON features, and that is only available
10014 on A and R profiles. */
10015 gdb_assert (!have_q_pseudos);
10017 /* Given we have a M-profile target description, if MVE is
10018 enabled and there are VFP registers, we should have Q
10019 pseudo registers (Q0 ~ Q7). */
10020 if (have_vfp)
10021 have_q_pseudos = true;
10024 /* Do we have the ARMv8.1-m PACBTI feature? */
10025 feature = tdesc_find_feature (tdesc,
10026 "org.gnu.gdb.arm.m-profile-pacbti");
10027 if (feature != nullptr)
10029 /* By advertising this feature, the target acknowledges the
10030 presence of the ARMv8.1-m PACBTI extensions.
10032 We don't care for any particular registers in this group, so
10033 the target is free to include whatever it deems appropriate.
10035 The expectation is for this feature to include the PAC
10036 keys. */
10037 have_pacbti = true;
10040 /* Do we have the Security extension? */
10041 feature = tdesc_find_feature (tdesc,
10042 "org.gnu.gdb.arm.secext");
10043 if (feature != nullptr)
10045 /* Secure/Non-secure stack pointers. */
10046 /* MSP_NS */
10047 valid_p &= tdesc_numbered_register (feature, tdesc_data.get (),
10048 register_count, "msp_ns");
10049 if (!valid_p)
10051 warning (_("M-profile secext feature is missing required register msp_ns."));
10052 return nullptr;
10054 m_profile_msp_ns_regnum = register_count++;
10056 /* PSP_NS */
10057 valid_p &= tdesc_numbered_register (feature, tdesc_data.get (),
10058 register_count, "psp_ns");
10059 if (!valid_p)
10061 warning (_("M-profile secext feature is missing required register psp_ns."));
10062 return nullptr;
10064 m_profile_psp_ns_regnum = register_count++;
10066 /* MSP_S */
10067 valid_p &= tdesc_numbered_register (feature, tdesc_data.get (),
10068 register_count, "msp_s");
10069 if (!valid_p)
10071 warning (_("M-profile secext feature is missing required register msp_s."));
10072 return nullptr;
10074 m_profile_msp_s_regnum = register_count++;
10076 /* PSP_S */
10077 valid_p &= tdesc_numbered_register (feature, tdesc_data.get (),
10078 register_count, "psp_s");
10079 if (!valid_p)
10081 warning (_("M-profile secext feature is missing required register psp_s."));
10082 return nullptr;
10084 m_profile_psp_s_regnum = register_count++;
10086 have_sec_ext = true;
10092 /* If there is already a candidate, use it. */
10093 for (best_arch = gdbarch_list_lookup_by_info (arches, &info);
10094 best_arch != NULL;
10095 best_arch = gdbarch_list_lookup_by_info (best_arch->next, &info))
10097 arm_gdbarch_tdep *tdep
10098 = (arm_gdbarch_tdep *) gdbarch_tdep (best_arch->gdbarch);
10100 if (arm_abi != ARM_ABI_AUTO && arm_abi != tdep->arm_abi)
10101 continue;
10103 if (fp_model != ARM_FLOAT_AUTO && fp_model != tdep->fp_model)
10104 continue;
10106 /* There are various other properties in tdep that we do not
10107 need to check here: those derived from a target description,
10108 since gdbarches with a different target description are
10109 automatically disqualified. */
10111 /* Do check is_m, though, since it might come from the binary. */
10112 if (is_m != tdep->is_m)
10113 continue;
10115 /* Also check for ARMv8.1-m PACBTI support, since it might come from
10116 the binary. */
10117 if (have_pacbti != tdep->have_pacbti)
10118 continue;
10120 /* Found a match. */
10121 break;
10124 if (best_arch != NULL)
10125 return best_arch->gdbarch;
10127 arm_gdbarch_tdep *tdep = new arm_gdbarch_tdep;
10128 gdbarch = gdbarch_alloc (&info, tdep);
10130 /* Record additional information about the architecture we are defining.
10131 These are gdbarch discriminators, like the OSABI. */
10132 tdep->arm_abi = arm_abi;
10133 tdep->fp_model = fp_model;
10134 tdep->is_m = is_m;
10135 tdep->have_sec_ext = have_sec_ext;
10136 tdep->have_fpa_registers = have_fpa_registers;
10137 tdep->have_wmmx_registers = have_wmmx_registers;
10138 gdb_assert (vfp_register_count == 0
10139 || vfp_register_count == 16
10140 || vfp_register_count == 32);
10141 tdep->vfp_register_count = vfp_register_count;
10142 tdep->have_s_pseudos = have_s_pseudos;
10143 tdep->have_q_pseudos = have_q_pseudos;
10144 tdep->have_neon = have_neon;
10145 tdep->tls_regnum = tls_regnum;
10147 /* Adjust the MVE feature settings. */
10148 if (have_mve)
10150 tdep->have_mve = true;
10151 tdep->mve_vpr_regnum = mve_vpr_regnum;
10154 /* Adjust the PACBTI feature settings. */
10155 tdep->have_pacbti = have_pacbti;
10157 /* Adjust the M-profile stack pointers settings. */
10158 if (have_m_profile_msp)
10160 tdep->m_profile_msp_regnum = m_profile_msp_regnum;
10161 tdep->m_profile_psp_regnum = m_profile_psp_regnum;
10162 tdep->m_profile_msp_ns_regnum = m_profile_msp_ns_regnum;
10163 tdep->m_profile_psp_ns_regnum = m_profile_psp_ns_regnum;
10164 tdep->m_profile_msp_s_regnum = m_profile_msp_s_regnum;
10165 tdep->m_profile_psp_s_regnum = m_profile_psp_s_regnum;
10168 arm_register_g_packet_guesses (gdbarch);
10170 /* Breakpoints. */
10171 switch (info.byte_order_for_code)
10173 case BFD_ENDIAN_BIG:
10174 tdep->arm_breakpoint = arm_default_arm_be_breakpoint;
10175 tdep->arm_breakpoint_size = sizeof (arm_default_arm_be_breakpoint);
10176 tdep->thumb_breakpoint = arm_default_thumb_be_breakpoint;
10177 tdep->thumb_breakpoint_size = sizeof (arm_default_thumb_be_breakpoint);
10179 break;
10181 case BFD_ENDIAN_LITTLE:
10182 tdep->arm_breakpoint = arm_default_arm_le_breakpoint;
10183 tdep->arm_breakpoint_size = sizeof (arm_default_arm_le_breakpoint);
10184 tdep->thumb_breakpoint = arm_default_thumb_le_breakpoint;
10185 tdep->thumb_breakpoint_size = sizeof (arm_default_thumb_le_breakpoint);
10187 break;
10189 default:
10190 internal_error (__FILE__, __LINE__,
10191 _("arm_gdbarch_init: bad byte order for float format"));
10194 /* On ARM targets char defaults to unsigned. */
10195 set_gdbarch_char_signed (gdbarch, 0);
10197 /* wchar_t is unsigned under the AAPCS. */
10198 if (tdep->arm_abi == ARM_ABI_AAPCS)
10199 set_gdbarch_wchar_signed (gdbarch, 0);
10200 else
10201 set_gdbarch_wchar_signed (gdbarch, 1);
10203 /* Compute type alignment. */
10204 set_gdbarch_type_align (gdbarch, arm_type_align);
10206 /* Note: for displaced stepping, this includes the breakpoint, and one word
10207 of additional scratch space. This setting isn't used for anything beside
10208 displaced stepping at present. */
10209 set_gdbarch_max_insn_length (gdbarch, 4 * ARM_DISPLACED_MODIFIED_INSNS);
10211 /* This should be low enough for everything. */
10212 tdep->lowest_pc = 0x20;
10213 tdep->jb_pc = -1; /* Longjump support not enabled by default. */
10215 /* The default, for both APCS and AAPCS, is to return small
10216 structures in registers. */
10217 tdep->struct_return = reg_struct_return;
10219 set_gdbarch_push_dummy_call (gdbarch, arm_push_dummy_call);
10220 set_gdbarch_frame_align (gdbarch, arm_frame_align);
10222 if (is_m)
10223 set_gdbarch_code_of_frame_writable (gdbarch, arm_code_of_frame_writable);
10225 set_gdbarch_write_pc (gdbarch, arm_write_pc);
10227 frame_base_set_default (gdbarch, &arm_normal_base);
10229 /* Address manipulation. */
10230 set_gdbarch_addr_bits_remove (gdbarch, arm_addr_bits_remove);
10232 /* Advance PC across function entry code. */
10233 set_gdbarch_skip_prologue (gdbarch, arm_skip_prologue);
10235 /* Detect whether PC is at a point where the stack has been destroyed. */
10236 set_gdbarch_stack_frame_destroyed_p (gdbarch, arm_stack_frame_destroyed_p);
10238 /* Skip trampolines. */
10239 set_gdbarch_skip_trampoline_code (gdbarch, arm_skip_stub);
10241 /* The stack grows downward. */
10242 set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
10244 /* Breakpoint manipulation. */
10245 set_gdbarch_breakpoint_kind_from_pc (gdbarch, arm_breakpoint_kind_from_pc);
10246 set_gdbarch_sw_breakpoint_from_kind (gdbarch, arm_sw_breakpoint_from_kind);
10247 set_gdbarch_breakpoint_kind_from_current_state (gdbarch,
10248 arm_breakpoint_kind_from_current_state);
10250 /* Information about registers, etc. */
10251 set_gdbarch_sp_regnum (gdbarch, ARM_SP_REGNUM);
10252 set_gdbarch_pc_regnum (gdbarch, ARM_PC_REGNUM);
10253 set_gdbarch_num_regs (gdbarch, register_count);
10254 set_gdbarch_register_type (gdbarch, arm_register_type);
10255 set_gdbarch_register_reggroup_p (gdbarch, arm_register_reggroup_p);
10257 /* This "info float" is FPA-specific. Use the generic version if we
10258 do not have FPA. */
10259 if (tdep->have_fpa_registers)
10260 set_gdbarch_print_float_info (gdbarch, arm_print_float_info);
10262 /* Internal <-> external register number maps. */
10263 set_gdbarch_dwarf2_reg_to_regnum (gdbarch, arm_dwarf_reg_to_regnum);
10264 set_gdbarch_register_sim_regno (gdbarch, arm_register_sim_regno);
10266 set_gdbarch_register_name (gdbarch, arm_register_name);
10268 /* Returning results. */
10269 set_gdbarch_return_value (gdbarch, arm_return_value);
10271 /* Disassembly. */
10272 set_gdbarch_print_insn (gdbarch, gdb_print_insn_arm);
10274 /* Minsymbol frobbing. */
10275 set_gdbarch_elf_make_msymbol_special (gdbarch, arm_elf_make_msymbol_special);
10276 set_gdbarch_coff_make_msymbol_special (gdbarch,
10277 arm_coff_make_msymbol_special);
10278 set_gdbarch_record_special_symbol (gdbarch, arm_record_special_symbol);
10280 /* Thumb-2 IT block support. */
10281 set_gdbarch_adjust_breakpoint_address (gdbarch,
10282 arm_adjust_breakpoint_address);
10284 /* Virtual tables. */
10285 set_gdbarch_vbit_in_delta (gdbarch, 1);
10287 /* Hook in the ABI-specific overrides, if they have been registered. */
10288 gdbarch_init_osabi (info, gdbarch);
10290 dwarf2_frame_set_init_reg (gdbarch, arm_dwarf2_frame_init_reg);
10292 /* Add some default predicates. */
10293 if (is_m)
10294 frame_unwind_append_unwinder (gdbarch, &arm_m_exception_unwind);
10295 frame_unwind_append_unwinder (gdbarch, &arm_stub_unwind);
10296 dwarf2_append_unwinders (gdbarch);
10297 frame_unwind_append_unwinder (gdbarch, &arm_exidx_unwind);
10298 frame_unwind_append_unwinder (gdbarch, &arm_epilogue_frame_unwind);
10299 frame_unwind_append_unwinder (gdbarch, &arm_prologue_unwind);
10301 /* Now we have tuned the configuration, set a few final things,
10302 based on what the OS ABI has told us. */
10304 /* If the ABI is not otherwise marked, assume the old GNU APCS. EABI
10305 binaries are always marked. */
10306 if (tdep->arm_abi == ARM_ABI_AUTO)
10307 tdep->arm_abi = ARM_ABI_APCS;
10309 /* Watchpoints are not steppable. */
10310 set_gdbarch_have_nonsteppable_watchpoint (gdbarch, 1);
10312 /* We used to default to FPA for generic ARM, but almost nobody
10313 uses that now, and we now provide a way for the user to force
10314 the model. So default to the most useful variant. */
10315 if (tdep->fp_model == ARM_FLOAT_AUTO)
10316 tdep->fp_model = ARM_FLOAT_SOFT_FPA;
10318 if (tdep->jb_pc >= 0)
10319 set_gdbarch_get_longjmp_target (gdbarch, arm_get_longjmp_target);
10321 /* Floating point sizes and format. */
10322 set_gdbarch_float_format (gdbarch, floatformats_ieee_single);
10323 if (tdep->fp_model == ARM_FLOAT_SOFT_FPA || tdep->fp_model == ARM_FLOAT_FPA)
10325 set_gdbarch_double_format
10326 (gdbarch, floatformats_ieee_double_littlebyte_bigword);
10327 set_gdbarch_long_double_format
10328 (gdbarch, floatformats_ieee_double_littlebyte_bigword);
10330 else
10332 set_gdbarch_double_format (gdbarch, floatformats_ieee_double);
10333 set_gdbarch_long_double_format (gdbarch, floatformats_ieee_double);
10336 /* Hook used to decorate frames with signed return addresses, only available
10337 for ARMv8.1-m PACBTI. */
10338 if (is_m && have_pacbti)
10339 set_gdbarch_get_pc_address_flags (gdbarch, arm_get_pc_address_flags);
10341 if (tdesc_data != nullptr)
10343 set_tdesc_pseudo_register_name (gdbarch, arm_register_name);
10345 tdesc_use_registers (gdbarch, tdesc, std::move (tdesc_data));
10346 register_count = gdbarch_num_regs (gdbarch);
10348 /* Override tdesc_register_type to adjust the types of VFP
10349 registers for NEON. */
10350 set_gdbarch_register_type (gdbarch, arm_register_type);
10353 /* Initialize the pseudo register data. */
10354 int num_pseudos = 0;
10355 if (tdep->have_s_pseudos)
10357 /* VFP single precision pseudo registers (S0~S31). */
10358 tdep->s_pseudo_base = register_count;
10359 tdep->s_pseudo_count = 32;
10360 num_pseudos += tdep->s_pseudo_count;
10362 if (tdep->have_q_pseudos)
10364 /* NEON quad precision pseudo registers (Q0~Q15). */
10365 tdep->q_pseudo_base = register_count + num_pseudos;
10367 if (have_neon)
10368 tdep->q_pseudo_count = 16;
10369 else if (have_mve)
10370 tdep->q_pseudo_count = ARM_MVE_NUM_Q_REGS;
10372 num_pseudos += tdep->q_pseudo_count;
10376 /* Do we have any MVE pseudo registers? */
10377 if (have_mve)
10379 tdep->mve_pseudo_base = register_count + num_pseudos;
10380 tdep->mve_pseudo_count = 1;
10381 num_pseudos += tdep->mve_pseudo_count;
10384 /* Do we have any ARMv8.1-m PACBTI pseudo registers. */
10385 if (have_pacbti)
10387 tdep->pacbti_pseudo_base = register_count + num_pseudos;
10388 tdep->pacbti_pseudo_count = 1;
10389 num_pseudos += tdep->pacbti_pseudo_count;
10392 /* Set some pseudo register hooks, if we have pseudo registers. */
10393 if (tdep->have_s_pseudos || have_mve || have_pacbti)
10395 set_gdbarch_num_pseudo_regs (gdbarch, num_pseudos);
10396 set_gdbarch_pseudo_register_read (gdbarch, arm_pseudo_read);
10397 set_gdbarch_pseudo_register_write (gdbarch, arm_pseudo_write);
10400 /* Add standard register aliases. We add aliases even for those
10401 names which are used by the current architecture - it's simpler,
10402 and does no harm, since nothing ever lists user registers. */
10403 for (i = 0; i < ARRAY_SIZE (arm_register_aliases); i++)
10404 user_reg_add (gdbarch, arm_register_aliases[i].name,
10405 value_of_arm_user_reg, &arm_register_aliases[i].regnum);
10407 set_gdbarch_disassembler_options (gdbarch, &arm_disassembler_options);
10408 set_gdbarch_valid_disassembler_options (gdbarch, disassembler_options_arm ());
10410 set_gdbarch_gnu_triplet_regexp (gdbarch, arm_gnu_triplet_regexp);
10412 return gdbarch;
10415 static void
10416 arm_dump_tdep (struct gdbarch *gdbarch, struct ui_file *file)
10418 arm_gdbarch_tdep *tdep = (arm_gdbarch_tdep *) gdbarch_tdep (gdbarch);
10420 if (tdep == NULL)
10421 return;
10423 gdb_printf (file, _("arm_dump_tdep: fp_model = %i\n"),
10424 (int) tdep->fp_model);
10425 gdb_printf (file, _("arm_dump_tdep: have_fpa_registers = %i\n"),
10426 (int) tdep->have_fpa_registers);
10427 gdb_printf (file, _("arm_dump_tdep: have_wmmx_registers = %i\n"),
10428 (int) tdep->have_wmmx_registers);
10429 gdb_printf (file, _("arm_dump_tdep: vfp_register_count = %i\n"),
10430 (int) tdep->vfp_register_count);
10431 gdb_printf (file, _("arm_dump_tdep: have_s_pseudos = %s\n"),
10432 tdep->have_s_pseudos? "true" : "false");
10433 gdb_printf (file, _("arm_dump_tdep: s_pseudo_base = %i\n"),
10434 (int) tdep->s_pseudo_base);
10435 gdb_printf (file, _("arm_dump_tdep: s_pseudo_count = %i\n"),
10436 (int) tdep->s_pseudo_count);
10437 gdb_printf (file, _("arm_dump_tdep: have_q_pseudos = %s\n"),
10438 tdep->have_q_pseudos? "true" : "false");
10439 gdb_printf (file, _("arm_dump_tdep: q_pseudo_base = %i\n"),
10440 (int) tdep->q_pseudo_base);
10441 gdb_printf (file, _("arm_dump_tdep: q_pseudo_count = %i\n"),
10442 (int) tdep->q_pseudo_count);
10443 gdb_printf (file, _("arm_dump_tdep: have_neon = %i\n"),
10444 (int) tdep->have_neon);
10445 gdb_printf (file, _("arm_dump_tdep: have_mve = %s\n"),
10446 tdep->have_mve? "yes" : "no");
10447 gdb_printf (file, _("arm_dump_tdep: mve_vpr_regnum = %i\n"),
10448 tdep->mve_vpr_regnum);
10449 gdb_printf (file, _("arm_dump_tdep: mve_pseudo_base = %i\n"),
10450 tdep->mve_pseudo_base);
10451 gdb_printf (file, _("arm_dump_tdep: mve_pseudo_count = %i\n"),
10452 tdep->mve_pseudo_count);
10453 gdb_printf (file, _("arm_dump_tdep: m_profile_msp_regnum = %i\n"),
10454 tdep->m_profile_msp_regnum);
10455 gdb_printf (file, _("arm_dump_tdep: m_profile_psp_regnum = %i\n"),
10456 tdep->m_profile_psp_regnum);
10457 gdb_printf (file, _("arm_dump_tdep: m_profile_msp_ns_regnum = %i\n"),
10458 tdep->m_profile_msp_ns_regnum);
10459 gdb_printf (file, _("arm_dump_tdep: m_profile_psp_ns_regnum = %i\n"),
10460 tdep->m_profile_psp_ns_regnum);
10461 gdb_printf (file, _("arm_dump_tdep: m_profile_msp_s_regnum = %i\n"),
10462 tdep->m_profile_msp_s_regnum);
10463 gdb_printf (file, _("arm_dump_tdep: m_profile_psp_s_regnum = %i\n"),
10464 tdep->m_profile_psp_s_regnum);
10465 gdb_printf (file, _("arm_dump_tdep: Lowest pc = 0x%lx\n"),
10466 (unsigned long) tdep->lowest_pc);
10467 gdb_printf (file, _("arm_dump_tdep: have_pacbti = %s\n"),
10468 tdep->have_pacbti? "yes" : "no");
10469 gdb_printf (file, _("arm_dump_tdep: pacbti_pseudo_base = %i\n"),
10470 tdep->pacbti_pseudo_base);
10471 gdb_printf (file, _("arm_dump_tdep: pacbti_pseudo_count = %i\n"),
10472 tdep->pacbti_pseudo_count);
10473 gdb_printf (file, _("arm_dump_tdep: is_m = %s\n"),
10474 tdep->is_m? "yes" : "no");
10477 #if GDB_SELF_TEST
10478 namespace selftests
10480 static void arm_record_test (void);
10481 static void arm_analyze_prologue_test ();
10483 #endif
10485 void _initialize_arm_tdep ();
10486 void
10487 _initialize_arm_tdep ()
10489 long length;
10490 int i, j;
10491 char regdesc[1024], *rdptr = regdesc;
10492 size_t rest = sizeof (regdesc);
10494 gdbarch_register (bfd_arch_arm, arm_gdbarch_init, arm_dump_tdep);
10496 /* Add ourselves to objfile event chain. */
10497 gdb::observers::new_objfile.attach (arm_exidx_new_objfile, "arm-tdep");
10499 /* Register an ELF OS ABI sniffer for ARM binaries. */
10500 gdbarch_register_osabi_sniffer (bfd_arch_arm,
10501 bfd_target_elf_flavour,
10502 arm_elf_osabi_sniffer);
10504 /* Add root prefix command for all "set arm"/"show arm" commands. */
10505 add_setshow_prefix_cmd ("arm", no_class,
10506 _("Various ARM-specific commands."),
10507 _("Various ARM-specific commands."),
10508 &setarmcmdlist, &showarmcmdlist,
10509 &setlist, &showlist);
10511 arm_disassembler_options = xstrdup ("reg-names-std");
10512 const disasm_options_t *disasm_options
10513 = &disassembler_options_arm ()->options;
10514 int num_disassembly_styles = 0;
10515 for (i = 0; disasm_options->name[i] != NULL; i++)
10516 if (startswith (disasm_options->name[i], "reg-names-"))
10517 num_disassembly_styles++;
10519 /* Initialize the array that will be passed to add_setshow_enum_cmd(). */
10520 valid_disassembly_styles = XNEWVEC (const char *,
10521 num_disassembly_styles + 1);
10522 for (i = j = 0; disasm_options->name[i] != NULL; i++)
10523 if (startswith (disasm_options->name[i], "reg-names-"))
10525 size_t offset = strlen ("reg-names-");
10526 const char *style = disasm_options->name[i];
10527 valid_disassembly_styles[j++] = &style[offset];
10528 if (strcmp (&style[offset], "std") == 0)
10529 disassembly_style = &style[offset];
10530 length = snprintf (rdptr, rest, "%s - %s\n", &style[offset],
10531 disasm_options->description[i]);
10532 rdptr += length;
10533 rest -= length;
10535 /* Mark the end of valid options. */
10536 valid_disassembly_styles[num_disassembly_styles] = NULL;
10538 /* Create the help text. */
10539 std::string helptext = string_printf ("%s%s%s",
10540 _("The valid values are:\n"),
10541 regdesc,
10542 _("The default is \"std\"."));
10544 add_setshow_enum_cmd("disassembler", no_class,
10545 valid_disassembly_styles, &disassembly_style,
10546 _("Set the disassembly style."),
10547 _("Show the disassembly style."),
10548 helptext.c_str (),
10549 set_disassembly_style_sfunc,
10550 show_disassembly_style_sfunc,
10551 &setarmcmdlist, &showarmcmdlist);
10553 add_setshow_boolean_cmd ("apcs32", no_class, &arm_apcs_32,
10554 _("Set usage of ARM 32-bit mode."),
10555 _("Show usage of ARM 32-bit mode."),
10556 _("When off, a 26-bit PC will be used."),
10557 NULL,
10558 NULL, /* FIXME: i18n: Usage of ARM 32-bit
10559 mode is %s. */
10560 &setarmcmdlist, &showarmcmdlist);
10562 /* Add a command to allow the user to force the FPU model. */
10563 add_setshow_enum_cmd ("fpu", no_class, fp_model_strings, &current_fp_model,
10564 _("Set the floating point type."),
10565 _("Show the floating point type."),
10566 _("auto - Determine the FP typefrom the OS-ABI.\n\
10567 softfpa - Software FP, mixed-endian doubles on little-endian ARMs.\n\
10568 fpa - FPA co-processor (GCC compiled).\n\
10569 softvfp - Software FP with pure-endian doubles.\n\
10570 vfp - VFP co-processor."),
10571 set_fp_model_sfunc, show_fp_model,
10572 &setarmcmdlist, &showarmcmdlist);
10574 /* Add a command to allow the user to force the ABI. */
10575 add_setshow_enum_cmd ("abi", class_support, arm_abi_strings, &arm_abi_string,
10576 _("Set the ABI."),
10577 _("Show the ABI."),
10578 NULL, arm_set_abi, arm_show_abi,
10579 &setarmcmdlist, &showarmcmdlist);
10581 /* Add two commands to allow the user to force the assumed
10582 execution mode. */
10583 add_setshow_enum_cmd ("fallback-mode", class_support,
10584 arm_mode_strings, &arm_fallback_mode_string,
10585 _("Set the mode assumed when symbols are unavailable."),
10586 _("Show the mode assumed when symbols are unavailable."),
10587 NULL, NULL, arm_show_fallback_mode,
10588 &setarmcmdlist, &showarmcmdlist);
10589 add_setshow_enum_cmd ("force-mode", class_support,
10590 arm_mode_strings, &arm_force_mode_string,
10591 _("Set the mode assumed even when symbols are available."),
10592 _("Show the mode assumed even when symbols are available."),
10593 NULL, NULL, arm_show_force_mode,
10594 &setarmcmdlist, &showarmcmdlist);
10596 /* Add a command to stop triggering security exceptions when
10597 unwinding exception stacks. */
10598 add_setshow_boolean_cmd ("unwind-secure-frames", no_class, &arm_unwind_secure_frames,
10599 _("Set usage of non-secure to secure exception stack unwinding."),
10600 _("Show usage of non-secure to secure exception stack unwinding."),
10601 _("When on, the debugger can trigger memory access traps."),
10602 NULL, arm_show_unwind_secure_frames,
10603 &setarmcmdlist, &showarmcmdlist);
10605 /* Debugging flag. */
10606 add_setshow_boolean_cmd ("arm", class_maintenance, &arm_debug,
10607 _("Set ARM debugging."),
10608 _("Show ARM debugging."),
10609 _("When on, arm-specific debugging is enabled."),
10610 NULL,
10611 NULL, /* FIXME: i18n: "ARM debugging is %s. */
10612 &setdebuglist, &showdebuglist);
10614 #if GDB_SELF_TEST
10615 selftests::register_test ("arm-record", selftests::arm_record_test);
10616 selftests::register_test ("arm_analyze_prologue", selftests::arm_analyze_prologue_test);
10617 #endif
10621 /* ARM-reversible process record data structures. */
10623 #define ARM_INSN_SIZE_BYTES 4
10624 #define THUMB_INSN_SIZE_BYTES 2
10625 #define THUMB2_INSN_SIZE_BYTES 4
10628 /* Position of the bit within a 32-bit ARM instruction
10629 that defines whether the instruction is a load or store. */
10630 #define INSN_S_L_BIT_NUM 20
10632 #define REG_ALLOC(REGS, LENGTH, RECORD_BUF) \
10633 do \
10635 unsigned int reg_len = LENGTH; \
10636 if (reg_len) \
10638 REGS = XNEWVEC (uint32_t, reg_len); \
10639 memcpy(&REGS[0], &RECORD_BUF[0], sizeof(uint32_t)*LENGTH); \
10642 while (0)
10644 #define MEM_ALLOC(MEMS, LENGTH, RECORD_BUF) \
10645 do \
10647 unsigned int mem_len = LENGTH; \
10648 if (mem_len) \
10650 MEMS = XNEWVEC (struct arm_mem_r, mem_len); \
10651 memcpy(&MEMS->len, &RECORD_BUF[0], \
10652 sizeof(struct arm_mem_r) * LENGTH); \
10655 while (0)
10657 /* Checks whether insn is already recorded or yet to be decoded. (boolean expression). */
10658 #define INSN_RECORDED(ARM_RECORD) \
10659 (0 != (ARM_RECORD)->reg_rec_count || 0 != (ARM_RECORD)->mem_rec_count)
10661 /* ARM memory record structure. */
10662 struct arm_mem_r
10664 uint32_t len; /* Record length. */
10665 uint32_t addr; /* Memory address. */
10668 /* ARM instruction record contains opcode of current insn
10669 and execution state (before entry to decode_insn()),
10670 contains list of to-be-modified registers and
10671 memory blocks (on return from decode_insn()). */
10673 typedef struct insn_decode_record_t
10675 struct gdbarch *gdbarch;
10676 struct regcache *regcache;
10677 CORE_ADDR this_addr; /* Address of the insn being decoded. */
10678 uint32_t arm_insn; /* Should accommodate thumb. */
10679 uint32_t cond; /* Condition code. */
10680 uint32_t opcode; /* Insn opcode. */
10681 uint32_t decode; /* Insn decode bits. */
10682 uint32_t mem_rec_count; /* No of mem records. */
10683 uint32_t reg_rec_count; /* No of reg records. */
10684 uint32_t *arm_regs; /* Registers to be saved for this record. */
10685 struct arm_mem_r *arm_mems; /* Memory to be saved for this record. */
10686 } insn_decode_record;
10689 /* Checks ARM SBZ and SBO mandatory fields. */
10691 static int
10692 sbo_sbz (uint32_t insn, uint32_t bit_num, uint32_t len, uint32_t sbo)
10694 uint32_t ones = bits (insn, bit_num - 1, (bit_num -1) + (len - 1));
10696 if (!len)
10697 return 1;
10699 if (!sbo)
10700 ones = ~ones;
10702 while (ones)
10704 if (!(ones & sbo))
10706 return 0;
10708 ones = ones >> 1;
10710 return 1;
10713 enum arm_record_result
10715 ARM_RECORD_SUCCESS = 0,
10716 ARM_RECORD_FAILURE = 1
10719 enum arm_record_strx_t
10721 ARM_RECORD_STRH=1,
10722 ARM_RECORD_STRD
10725 enum record_type_t
10727 ARM_RECORD=1,
10728 THUMB_RECORD,
10729 THUMB2_RECORD
10733 static int
10734 arm_record_strx (insn_decode_record *arm_insn_r, uint32_t *record_buf,
10735 uint32_t *record_buf_mem, arm_record_strx_t str_type)
10738 struct regcache *reg_cache = arm_insn_r->regcache;
10739 ULONGEST u_regval[2]= {0};
10741 uint32_t reg_src1 = 0, reg_src2 = 0;
10742 uint32_t immed_high = 0, immed_low = 0,offset_8 = 0, tgt_mem_addr = 0;
10744 arm_insn_r->opcode = bits (arm_insn_r->arm_insn, 21, 24);
10745 arm_insn_r->decode = bits (arm_insn_r->arm_insn, 4, 7);
10747 if (14 == arm_insn_r->opcode || 10 == arm_insn_r->opcode)
10749 /* 1) Handle misc store, immediate offset. */
10750 immed_low = bits (arm_insn_r->arm_insn, 0, 3);
10751 immed_high = bits (arm_insn_r->arm_insn, 8, 11);
10752 reg_src1 = bits (arm_insn_r->arm_insn, 16, 19);
10753 regcache_raw_read_unsigned (reg_cache, reg_src1,
10754 &u_regval[0]);
10755 if (ARM_PC_REGNUM == reg_src1)
10757 /* If R15 was used as Rn, hence current PC+8. */
10758 u_regval[0] = u_regval[0] + 8;
10760 offset_8 = (immed_high << 4) | immed_low;
10761 /* Calculate target store address. */
10762 if (14 == arm_insn_r->opcode)
10764 tgt_mem_addr = u_regval[0] + offset_8;
10766 else
10768 tgt_mem_addr = u_regval[0] - offset_8;
10770 if (ARM_RECORD_STRH == str_type)
10772 record_buf_mem[0] = 2;
10773 record_buf_mem[1] = tgt_mem_addr;
10774 arm_insn_r->mem_rec_count = 1;
10776 else if (ARM_RECORD_STRD == str_type)
10778 record_buf_mem[0] = 4;
10779 record_buf_mem[1] = tgt_mem_addr;
10780 record_buf_mem[2] = 4;
10781 record_buf_mem[3] = tgt_mem_addr + 4;
10782 arm_insn_r->mem_rec_count = 2;
10785 else if (12 == arm_insn_r->opcode || 8 == arm_insn_r->opcode)
10787 /* 2) Store, register offset. */
10788 /* Get Rm. */
10789 reg_src1 = bits (arm_insn_r->arm_insn, 0, 3);
10790 /* Get Rn. */
10791 reg_src2 = bits (arm_insn_r->arm_insn, 16, 19);
10792 regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval[0]);
10793 regcache_raw_read_unsigned (reg_cache, reg_src2, &u_regval[1]);
10794 if (15 == reg_src2)
10796 /* If R15 was used as Rn, hence current PC+8. */
10797 u_regval[0] = u_regval[0] + 8;
10799 /* Calculate target store address, Rn +/- Rm, register offset. */
10800 if (12 == arm_insn_r->opcode)
10802 tgt_mem_addr = u_regval[0] + u_regval[1];
10804 else
10806 tgt_mem_addr = u_regval[1] - u_regval[0];
10808 if (ARM_RECORD_STRH == str_type)
10810 record_buf_mem[0] = 2;
10811 record_buf_mem[1] = tgt_mem_addr;
10812 arm_insn_r->mem_rec_count = 1;
10814 else if (ARM_RECORD_STRD == str_type)
10816 record_buf_mem[0] = 4;
10817 record_buf_mem[1] = tgt_mem_addr;
10818 record_buf_mem[2] = 4;
10819 record_buf_mem[3] = tgt_mem_addr + 4;
10820 arm_insn_r->mem_rec_count = 2;
10823 else if (11 == arm_insn_r->opcode || 15 == arm_insn_r->opcode
10824 || 2 == arm_insn_r->opcode || 6 == arm_insn_r->opcode)
10826 /* 3) Store, immediate pre-indexed. */
10827 /* 5) Store, immediate post-indexed. */
10828 immed_low = bits (arm_insn_r->arm_insn, 0, 3);
10829 immed_high = bits (arm_insn_r->arm_insn, 8, 11);
10830 offset_8 = (immed_high << 4) | immed_low;
10831 reg_src1 = bits (arm_insn_r->arm_insn, 16, 19);
10832 regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval[0]);
10833 /* Calculate target store address, Rn +/- Rm, register offset. */
10834 if (15 == arm_insn_r->opcode || 6 == arm_insn_r->opcode)
10836 tgt_mem_addr = u_regval[0] + offset_8;
10838 else
10840 tgt_mem_addr = u_regval[0] - offset_8;
10842 if (ARM_RECORD_STRH == str_type)
10844 record_buf_mem[0] = 2;
10845 record_buf_mem[1] = tgt_mem_addr;
10846 arm_insn_r->mem_rec_count = 1;
10848 else if (ARM_RECORD_STRD == str_type)
10850 record_buf_mem[0] = 4;
10851 record_buf_mem[1] = tgt_mem_addr;
10852 record_buf_mem[2] = 4;
10853 record_buf_mem[3] = tgt_mem_addr + 4;
10854 arm_insn_r->mem_rec_count = 2;
10856 /* Record Rn also as it changes. */
10857 *(record_buf) = bits (arm_insn_r->arm_insn, 16, 19);
10858 arm_insn_r->reg_rec_count = 1;
10860 else if (9 == arm_insn_r->opcode || 13 == arm_insn_r->opcode
10861 || 0 == arm_insn_r->opcode || 4 == arm_insn_r->opcode)
10863 /* 4) Store, register pre-indexed. */
10864 /* 6) Store, register post -indexed. */
10865 reg_src1 = bits (arm_insn_r->arm_insn, 0, 3);
10866 reg_src2 = bits (arm_insn_r->arm_insn, 16, 19);
10867 regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval[0]);
10868 regcache_raw_read_unsigned (reg_cache, reg_src2, &u_regval[1]);
10869 /* Calculate target store address, Rn +/- Rm, register offset. */
10870 if (13 == arm_insn_r->opcode || 4 == arm_insn_r->opcode)
10872 tgt_mem_addr = u_regval[0] + u_regval[1];
10874 else
10876 tgt_mem_addr = u_regval[1] - u_regval[0];
10878 if (ARM_RECORD_STRH == str_type)
10880 record_buf_mem[0] = 2;
10881 record_buf_mem[1] = tgt_mem_addr;
10882 arm_insn_r->mem_rec_count = 1;
10884 else if (ARM_RECORD_STRD == str_type)
10886 record_buf_mem[0] = 4;
10887 record_buf_mem[1] = tgt_mem_addr;
10888 record_buf_mem[2] = 4;
10889 record_buf_mem[3] = tgt_mem_addr + 4;
10890 arm_insn_r->mem_rec_count = 2;
10892 /* Record Rn also as it changes. */
10893 *(record_buf) = bits (arm_insn_r->arm_insn, 16, 19);
10894 arm_insn_r->reg_rec_count = 1;
10896 return 0;
10899 /* Handling ARM extension space insns. */
10901 static int
10902 arm_record_extension_space (insn_decode_record *arm_insn_r)
10904 int ret = 0; /* Return value: -1:record failure ; 0:success */
10905 uint32_t opcode1 = 0, opcode2 = 0, insn_op1 = 0;
10906 uint32_t record_buf[8], record_buf_mem[8];
10907 uint32_t reg_src1 = 0;
10908 struct regcache *reg_cache = arm_insn_r->regcache;
10909 ULONGEST u_regval = 0;
10911 gdb_assert (!INSN_RECORDED(arm_insn_r));
10912 /* Handle unconditional insn extension space. */
10914 opcode1 = bits (arm_insn_r->arm_insn, 20, 27);
10915 opcode2 = bits (arm_insn_r->arm_insn, 4, 7);
10916 if (arm_insn_r->cond)
10918 /* PLD has no affect on architectural state, it just affects
10919 the caches. */
10920 if (5 == ((opcode1 & 0xE0) >> 5))
10922 /* BLX(1) */
10923 record_buf[0] = ARM_PS_REGNUM;
10924 record_buf[1] = ARM_LR_REGNUM;
10925 arm_insn_r->reg_rec_count = 2;
10927 /* STC2, LDC2, MCR2, MRC2, CDP2: <TBD>, co-processor insn. */
10931 opcode1 = bits (arm_insn_r->arm_insn, 25, 27);
10932 if (3 == opcode1 && bit (arm_insn_r->arm_insn, 4))
10934 ret = -1;
10935 /* Undefined instruction on ARM V5; need to handle if later
10936 versions define it. */
10939 opcode1 = bits (arm_insn_r->arm_insn, 24, 27);
10940 opcode2 = bits (arm_insn_r->arm_insn, 4, 7);
10941 insn_op1 = bits (arm_insn_r->arm_insn, 20, 23);
10943 /* Handle arithmetic insn extension space. */
10944 if (!opcode1 && 9 == opcode2 && 1 != arm_insn_r->cond
10945 && !INSN_RECORDED(arm_insn_r))
10947 /* Handle MLA(S) and MUL(S). */
10948 if (in_inclusive_range (insn_op1, 0U, 3U))
10950 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
10951 record_buf[1] = ARM_PS_REGNUM;
10952 arm_insn_r->reg_rec_count = 2;
10954 else if (in_inclusive_range (insn_op1, 4U, 15U))
10956 /* Handle SMLAL(S), SMULL(S), UMLAL(S), UMULL(S). */
10957 record_buf[0] = bits (arm_insn_r->arm_insn, 16, 19);
10958 record_buf[1] = bits (arm_insn_r->arm_insn, 12, 15);
10959 record_buf[2] = ARM_PS_REGNUM;
10960 arm_insn_r->reg_rec_count = 3;
10964 opcode1 = bits (arm_insn_r->arm_insn, 26, 27);
10965 opcode2 = bits (arm_insn_r->arm_insn, 23, 24);
10966 insn_op1 = bits (arm_insn_r->arm_insn, 21, 22);
10968 /* Handle control insn extension space. */
10970 if (!opcode1 && 2 == opcode2 && !bit (arm_insn_r->arm_insn, 20)
10971 && 1 != arm_insn_r->cond && !INSN_RECORDED(arm_insn_r))
10973 if (!bit (arm_insn_r->arm_insn,25))
10975 if (!bits (arm_insn_r->arm_insn, 4, 7))
10977 if ((0 == insn_op1) || (2 == insn_op1))
10979 /* MRS. */
10980 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
10981 arm_insn_r->reg_rec_count = 1;
10983 else if (1 == insn_op1)
10985 /* CSPR is going to be changed. */
10986 record_buf[0] = ARM_PS_REGNUM;
10987 arm_insn_r->reg_rec_count = 1;
10989 else if (3 == insn_op1)
10991 /* SPSR is going to be changed. */
10992 /* We need to get SPSR value, which is yet to be done. */
10993 return -1;
10996 else if (1 == bits (arm_insn_r->arm_insn, 4, 7))
10998 if (1 == insn_op1)
11000 /* BX. */
11001 record_buf[0] = ARM_PS_REGNUM;
11002 arm_insn_r->reg_rec_count = 1;
11004 else if (3 == insn_op1)
11006 /* CLZ. */
11007 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11008 arm_insn_r->reg_rec_count = 1;
11011 else if (3 == bits (arm_insn_r->arm_insn, 4, 7))
11013 /* BLX. */
11014 record_buf[0] = ARM_PS_REGNUM;
11015 record_buf[1] = ARM_LR_REGNUM;
11016 arm_insn_r->reg_rec_count = 2;
11018 else if (5 == bits (arm_insn_r->arm_insn, 4, 7))
11020 /* QADD, QSUB, QDADD, QDSUB */
11021 record_buf[0] = ARM_PS_REGNUM;
11022 record_buf[1] = bits (arm_insn_r->arm_insn, 12, 15);
11023 arm_insn_r->reg_rec_count = 2;
11025 else if (7 == bits (arm_insn_r->arm_insn, 4, 7))
11027 /* BKPT. */
11028 record_buf[0] = ARM_PS_REGNUM;
11029 record_buf[1] = ARM_LR_REGNUM;
11030 arm_insn_r->reg_rec_count = 2;
11032 /* Save SPSR also;how? */
11033 return -1;
11035 else if(8 == bits (arm_insn_r->arm_insn, 4, 7)
11036 || 10 == bits (arm_insn_r->arm_insn, 4, 7)
11037 || 12 == bits (arm_insn_r->arm_insn, 4, 7)
11038 || 14 == bits (arm_insn_r->arm_insn, 4, 7)
11041 if (0 == insn_op1 || 1 == insn_op1)
11043 /* SMLA<x><y>, SMLAW<y>, SMULW<y>. */
11044 /* We dont do optimization for SMULW<y> where we
11045 need only Rd. */
11046 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11047 record_buf[1] = ARM_PS_REGNUM;
11048 arm_insn_r->reg_rec_count = 2;
11050 else if (2 == insn_op1)
11052 /* SMLAL<x><y>. */
11053 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11054 record_buf[1] = bits (arm_insn_r->arm_insn, 16, 19);
11055 arm_insn_r->reg_rec_count = 2;
11057 else if (3 == insn_op1)
11059 /* SMUL<x><y>. */
11060 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11061 arm_insn_r->reg_rec_count = 1;
11065 else
11067 /* MSR : immediate form. */
11068 if (1 == insn_op1)
11070 /* CSPR is going to be changed. */
11071 record_buf[0] = ARM_PS_REGNUM;
11072 arm_insn_r->reg_rec_count = 1;
11074 else if (3 == insn_op1)
11076 /* SPSR is going to be changed. */
11077 /* we need to get SPSR value, which is yet to be done */
11078 return -1;
11083 opcode1 = bits (arm_insn_r->arm_insn, 25, 27);
11084 opcode2 = bits (arm_insn_r->arm_insn, 20, 24);
11085 insn_op1 = bits (arm_insn_r->arm_insn, 5, 6);
11087 /* Handle load/store insn extension space. */
11089 if (!opcode1 && bit (arm_insn_r->arm_insn, 7)
11090 && bit (arm_insn_r->arm_insn, 4) && 1 != arm_insn_r->cond
11091 && !INSN_RECORDED(arm_insn_r))
11093 /* SWP/SWPB. */
11094 if (0 == insn_op1)
11096 /* These insn, changes register and memory as well. */
11097 /* SWP or SWPB insn. */
11098 /* Get memory address given by Rn. */
11099 reg_src1 = bits (arm_insn_r->arm_insn, 16, 19);
11100 regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval);
11101 /* SWP insn ?, swaps word. */
11102 if (8 == arm_insn_r->opcode)
11104 record_buf_mem[0] = 4;
11106 else
11108 /* SWPB insn, swaps only byte. */
11109 record_buf_mem[0] = 1;
11111 record_buf_mem[1] = u_regval;
11112 arm_insn_r->mem_rec_count = 1;
11113 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11114 arm_insn_r->reg_rec_count = 1;
11116 else if (1 == insn_op1 && !bit (arm_insn_r->arm_insn, 20))
11118 /* STRH. */
11119 arm_record_strx(arm_insn_r, &record_buf[0], &record_buf_mem[0],
11120 ARM_RECORD_STRH);
11122 else if (2 == insn_op1 && !bit (arm_insn_r->arm_insn, 20))
11124 /* LDRD. */
11125 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11126 record_buf[1] = record_buf[0] + 1;
11127 arm_insn_r->reg_rec_count = 2;
11129 else if (3 == insn_op1 && !bit (arm_insn_r->arm_insn, 20))
11131 /* STRD. */
11132 arm_record_strx(arm_insn_r, &record_buf[0], &record_buf_mem[0],
11133 ARM_RECORD_STRD);
11135 else if (bit (arm_insn_r->arm_insn, 20) && insn_op1 <= 3)
11137 /* LDRH, LDRSB, LDRSH. */
11138 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11139 arm_insn_r->reg_rec_count = 1;
11144 opcode1 = bits (arm_insn_r->arm_insn, 23, 27);
11145 if (24 == opcode1 && bit (arm_insn_r->arm_insn, 21)
11146 && !INSN_RECORDED(arm_insn_r))
11148 ret = -1;
11149 /* Handle coprocessor insn extension space. */
11152 /* To be done for ARMv5 and later; as of now we return -1. */
11153 if (-1 == ret)
11154 return ret;
11156 REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
11157 MEM_ALLOC (arm_insn_r->arm_mems, arm_insn_r->mem_rec_count, record_buf_mem);
11159 return ret;
11162 /* Handling opcode 000 insns. */
11164 static int
11165 arm_record_data_proc_misc_ld_str (insn_decode_record *arm_insn_r)
11167 struct regcache *reg_cache = arm_insn_r->regcache;
11168 uint32_t record_buf[8], record_buf_mem[8];
11169 ULONGEST u_regval[2] = {0};
11171 uint32_t reg_src1 = 0;
11172 uint32_t opcode1 = 0;
11174 arm_insn_r->opcode = bits (arm_insn_r->arm_insn, 21, 24);
11175 arm_insn_r->decode = bits (arm_insn_r->arm_insn, 4, 7);
11176 opcode1 = bits (arm_insn_r->arm_insn, 20, 24);
11178 if (!((opcode1 & 0x19) == 0x10))
11180 /* Data-processing (register) and Data-processing (register-shifted
11181 register */
11182 /* Out of 11 shifter operands mode, all the insn modifies destination
11183 register, which is specified by 13-16 decode. */
11184 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11185 record_buf[1] = ARM_PS_REGNUM;
11186 arm_insn_r->reg_rec_count = 2;
11188 else if ((arm_insn_r->decode < 8) && ((opcode1 & 0x19) == 0x10))
11190 /* Miscellaneous instructions */
11192 if (3 == arm_insn_r->decode && 0x12 == opcode1
11193 && sbo_sbz (arm_insn_r->arm_insn, 9, 12, 1))
11195 /* Handle BLX, branch and link/exchange. */
11196 if (9 == arm_insn_r->opcode)
11198 /* Branch is chosen by setting T bit of CSPR, bitp[0] of Rm,
11199 and R14 stores the return address. */
11200 record_buf[0] = ARM_PS_REGNUM;
11201 record_buf[1] = ARM_LR_REGNUM;
11202 arm_insn_r->reg_rec_count = 2;
11205 else if (7 == arm_insn_r->decode && 0x12 == opcode1)
11207 /* Handle enhanced software breakpoint insn, BKPT. */
11208 /* CPSR is changed to be executed in ARM state, disabling normal
11209 interrupts, entering abort mode. */
11210 /* According to high vector configuration PC is set. */
11211 /* user hit breakpoint and type reverse, in
11212 that case, we need to go back with previous CPSR and
11213 Program Counter. */
11214 record_buf[0] = ARM_PS_REGNUM;
11215 record_buf[1] = ARM_LR_REGNUM;
11216 arm_insn_r->reg_rec_count = 2;
11218 /* Save SPSR also; how? */
11219 return -1;
11221 else if (1 == arm_insn_r->decode && 0x12 == opcode1
11222 && sbo_sbz (arm_insn_r->arm_insn, 9, 12, 1))
11224 /* Handle BX, branch and link/exchange. */
11225 /* Branch is chosen by setting T bit of CSPR, bitp[0] of Rm. */
11226 record_buf[0] = ARM_PS_REGNUM;
11227 arm_insn_r->reg_rec_count = 1;
11229 else if (1 == arm_insn_r->decode && 0x16 == opcode1
11230 && sbo_sbz (arm_insn_r->arm_insn, 9, 4, 1)
11231 && sbo_sbz (arm_insn_r->arm_insn, 17, 4, 1))
11233 /* Count leading zeros: CLZ. */
11234 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11235 arm_insn_r->reg_rec_count = 1;
11237 else if (!bit (arm_insn_r->arm_insn, INSN_S_L_BIT_NUM)
11238 && (8 == arm_insn_r->opcode || 10 == arm_insn_r->opcode)
11239 && sbo_sbz (arm_insn_r->arm_insn, 17, 4, 1)
11240 && sbo_sbz (arm_insn_r->arm_insn, 1, 12, 0))
11242 /* Handle MRS insn. */
11243 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11244 arm_insn_r->reg_rec_count = 1;
11247 else if (9 == arm_insn_r->decode && opcode1 < 0x10)
11249 /* Multiply and multiply-accumulate */
11251 /* Handle multiply instructions. */
11252 /* MLA, MUL, SMLAL, SMULL, UMLAL, UMULL. */
11253 if (0 == arm_insn_r->opcode || 1 == arm_insn_r->opcode)
11255 /* Handle MLA and MUL. */
11256 record_buf[0] = bits (arm_insn_r->arm_insn, 16, 19);
11257 record_buf[1] = ARM_PS_REGNUM;
11258 arm_insn_r->reg_rec_count = 2;
11260 else if (4 <= arm_insn_r->opcode && 7 >= arm_insn_r->opcode)
11262 /* Handle SMLAL, SMULL, UMLAL, UMULL. */
11263 record_buf[0] = bits (arm_insn_r->arm_insn, 16, 19);
11264 record_buf[1] = bits (arm_insn_r->arm_insn, 12, 15);
11265 record_buf[2] = ARM_PS_REGNUM;
11266 arm_insn_r->reg_rec_count = 3;
11269 else if (9 == arm_insn_r->decode && opcode1 > 0x10)
11271 /* Synchronization primitives */
11273 /* Handling SWP, SWPB. */
11274 /* These insn, changes register and memory as well. */
11275 /* SWP or SWPB insn. */
11277 reg_src1 = bits (arm_insn_r->arm_insn, 16, 19);
11278 regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval[0]);
11279 /* SWP insn ?, swaps word. */
11280 if (8 == arm_insn_r->opcode)
11282 record_buf_mem[0] = 4;
11284 else
11286 /* SWPB insn, swaps only byte. */
11287 record_buf_mem[0] = 1;
11289 record_buf_mem[1] = u_regval[0];
11290 arm_insn_r->mem_rec_count = 1;
11291 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11292 arm_insn_r->reg_rec_count = 1;
11294 else if (11 == arm_insn_r->decode || 13 == arm_insn_r->decode
11295 || 15 == arm_insn_r->decode)
11297 if ((opcode1 & 0x12) == 2)
11299 /* Extra load/store (unprivileged) */
11300 return -1;
11302 else
11304 /* Extra load/store */
11305 switch (bits (arm_insn_r->arm_insn, 5, 6))
11307 case 1:
11308 if ((opcode1 & 0x05) == 0x0 || (opcode1 & 0x05) == 0x4)
11310 /* STRH (register), STRH (immediate) */
11311 arm_record_strx (arm_insn_r, &record_buf[0],
11312 &record_buf_mem[0], ARM_RECORD_STRH);
11314 else if ((opcode1 & 0x05) == 0x1)
11316 /* LDRH (register) */
11317 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11318 arm_insn_r->reg_rec_count = 1;
11320 if (bit (arm_insn_r->arm_insn, 21))
11322 /* Write back to Rn. */
11323 record_buf[arm_insn_r->reg_rec_count++]
11324 = bits (arm_insn_r->arm_insn, 16, 19);
11327 else if ((opcode1 & 0x05) == 0x5)
11329 /* LDRH (immediate), LDRH (literal) */
11330 int rn = bits (arm_insn_r->arm_insn, 16, 19);
11332 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11333 arm_insn_r->reg_rec_count = 1;
11335 if (rn != 15)
11337 /*LDRH (immediate) */
11338 if (bit (arm_insn_r->arm_insn, 21))
11340 /* Write back to Rn. */
11341 record_buf[arm_insn_r->reg_rec_count++] = rn;
11345 else
11346 return -1;
11347 break;
11348 case 2:
11349 if ((opcode1 & 0x05) == 0x0)
11351 /* LDRD (register) */
11352 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11353 record_buf[1] = record_buf[0] + 1;
11354 arm_insn_r->reg_rec_count = 2;
11356 if (bit (arm_insn_r->arm_insn, 21))
11358 /* Write back to Rn. */
11359 record_buf[arm_insn_r->reg_rec_count++]
11360 = bits (arm_insn_r->arm_insn, 16, 19);
11363 else if ((opcode1 & 0x05) == 0x1)
11365 /* LDRSB (register) */
11366 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11367 arm_insn_r->reg_rec_count = 1;
11369 if (bit (arm_insn_r->arm_insn, 21))
11371 /* Write back to Rn. */
11372 record_buf[arm_insn_r->reg_rec_count++]
11373 = bits (arm_insn_r->arm_insn, 16, 19);
11376 else if ((opcode1 & 0x05) == 0x4 || (opcode1 & 0x05) == 0x5)
11378 /* LDRD (immediate), LDRD (literal), LDRSB (immediate),
11379 LDRSB (literal) */
11380 int rn = bits (arm_insn_r->arm_insn, 16, 19);
11382 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11383 arm_insn_r->reg_rec_count = 1;
11385 if (rn != 15)
11387 /*LDRD (immediate), LDRSB (immediate) */
11388 if (bit (arm_insn_r->arm_insn, 21))
11390 /* Write back to Rn. */
11391 record_buf[arm_insn_r->reg_rec_count++] = rn;
11395 else
11396 return -1;
11397 break;
11398 case 3:
11399 if ((opcode1 & 0x05) == 0x0)
11401 /* STRD (register) */
11402 arm_record_strx (arm_insn_r, &record_buf[0],
11403 &record_buf_mem[0], ARM_RECORD_STRD);
11405 else if ((opcode1 & 0x05) == 0x1)
11407 /* LDRSH (register) */
11408 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11409 arm_insn_r->reg_rec_count = 1;
11411 if (bit (arm_insn_r->arm_insn, 21))
11413 /* Write back to Rn. */
11414 record_buf[arm_insn_r->reg_rec_count++]
11415 = bits (arm_insn_r->arm_insn, 16, 19);
11418 else if ((opcode1 & 0x05) == 0x4)
11420 /* STRD (immediate) */
11421 arm_record_strx (arm_insn_r, &record_buf[0],
11422 &record_buf_mem[0], ARM_RECORD_STRD);
11424 else if ((opcode1 & 0x05) == 0x5)
11426 /* LDRSH (immediate), LDRSH (literal) */
11427 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11428 arm_insn_r->reg_rec_count = 1;
11430 if (bit (arm_insn_r->arm_insn, 21))
11432 /* Write back to Rn. */
11433 record_buf[arm_insn_r->reg_rec_count++]
11434 = bits (arm_insn_r->arm_insn, 16, 19);
11437 else
11438 return -1;
11439 break;
11440 default:
11441 return -1;
11445 else
11447 return -1;
11450 REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
11451 MEM_ALLOC (arm_insn_r->arm_mems, arm_insn_r->mem_rec_count, record_buf_mem);
11452 return 0;
11455 /* Handling opcode 001 insns. */
11457 static int
11458 arm_record_data_proc_imm (insn_decode_record *arm_insn_r)
11460 uint32_t record_buf[8], record_buf_mem[8];
11462 arm_insn_r->opcode = bits (arm_insn_r->arm_insn, 21, 24);
11463 arm_insn_r->decode = bits (arm_insn_r->arm_insn, 4, 7);
11465 if ((9 == arm_insn_r->opcode || 11 == arm_insn_r->opcode)
11466 && 2 == bits (arm_insn_r->arm_insn, 20, 21)
11467 && sbo_sbz (arm_insn_r->arm_insn, 13, 4, 1)
11470 /* Handle MSR insn. */
11471 if (9 == arm_insn_r->opcode)
11473 /* CSPR is going to be changed. */
11474 record_buf[0] = ARM_PS_REGNUM;
11475 arm_insn_r->reg_rec_count = 1;
11477 else
11479 /* SPSR is going to be changed. */
11482 else if (arm_insn_r->opcode <= 15)
11484 /* Normal data processing insns. */
11485 /* Out of 11 shifter operands mode, all the insn modifies destination
11486 register, which is specified by 13-16 decode. */
11487 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11488 record_buf[1] = ARM_PS_REGNUM;
11489 arm_insn_r->reg_rec_count = 2;
11491 else
11493 return -1;
11496 REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
11497 MEM_ALLOC (arm_insn_r->arm_mems, arm_insn_r->mem_rec_count, record_buf_mem);
11498 return 0;
11501 static int
11502 arm_record_media (insn_decode_record *arm_insn_r)
11504 uint32_t record_buf[8];
11506 switch (bits (arm_insn_r->arm_insn, 22, 24))
11508 case 0:
11509 /* Parallel addition and subtraction, signed */
11510 case 1:
11511 /* Parallel addition and subtraction, unsigned */
11512 case 2:
11513 case 3:
11514 /* Packing, unpacking, saturation and reversal */
11516 int rd = bits (arm_insn_r->arm_insn, 12, 15);
11518 record_buf[arm_insn_r->reg_rec_count++] = rd;
11520 break;
11522 case 4:
11523 case 5:
11524 /* Signed multiplies */
11526 int rd = bits (arm_insn_r->arm_insn, 16, 19);
11527 unsigned int op1 = bits (arm_insn_r->arm_insn, 20, 22);
11529 record_buf[arm_insn_r->reg_rec_count++] = rd;
11530 if (op1 == 0x0)
11531 record_buf[arm_insn_r->reg_rec_count++] = ARM_PS_REGNUM;
11532 else if (op1 == 0x4)
11533 record_buf[arm_insn_r->reg_rec_count++]
11534 = bits (arm_insn_r->arm_insn, 12, 15);
11536 break;
11538 case 6:
11540 if (bit (arm_insn_r->arm_insn, 21)
11541 && bits (arm_insn_r->arm_insn, 5, 6) == 0x2)
11543 /* SBFX */
11544 record_buf[arm_insn_r->reg_rec_count++]
11545 = bits (arm_insn_r->arm_insn, 12, 15);
11547 else if (bits (arm_insn_r->arm_insn, 20, 21) == 0x0
11548 && bits (arm_insn_r->arm_insn, 5, 7) == 0x0)
11550 /* USAD8 and USADA8 */
11551 record_buf[arm_insn_r->reg_rec_count++]
11552 = bits (arm_insn_r->arm_insn, 16, 19);
11555 break;
11557 case 7:
11559 if (bits (arm_insn_r->arm_insn, 20, 21) == 0x3
11560 && bits (arm_insn_r->arm_insn, 5, 7) == 0x7)
11562 /* Permanently UNDEFINED */
11563 return -1;
11565 else
11567 /* BFC, BFI and UBFX */
11568 record_buf[arm_insn_r->reg_rec_count++]
11569 = bits (arm_insn_r->arm_insn, 12, 15);
11572 break;
11574 default:
11575 return -1;
11578 REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
11580 return 0;
11583 /* Handle ARM mode instructions with opcode 010. */
11585 static int
11586 arm_record_ld_st_imm_offset (insn_decode_record *arm_insn_r)
11588 struct regcache *reg_cache = arm_insn_r->regcache;
11590 uint32_t reg_base , reg_dest;
11591 uint32_t offset_12, tgt_mem_addr;
11592 uint32_t record_buf[8], record_buf_mem[8];
11593 unsigned char wback;
11594 ULONGEST u_regval;
11596 /* Calculate wback. */
11597 wback = (bit (arm_insn_r->arm_insn, 24) == 0)
11598 || (bit (arm_insn_r->arm_insn, 21) == 1);
11600 arm_insn_r->reg_rec_count = 0;
11601 reg_base = bits (arm_insn_r->arm_insn, 16, 19);
11603 if (bit (arm_insn_r->arm_insn, INSN_S_L_BIT_NUM))
11605 /* LDR (immediate), LDR (literal), LDRB (immediate), LDRB (literal), LDRBT
11606 and LDRT. */
11608 reg_dest = bits (arm_insn_r->arm_insn, 12, 15);
11609 record_buf[arm_insn_r->reg_rec_count++] = reg_dest;
11611 /* The LDR instruction is capable of doing branching. If MOV LR, PC
11612 preceeds a LDR instruction having R15 as reg_base, it
11613 emulates a branch and link instruction, and hence we need to save
11614 CPSR and PC as well. */
11615 if (ARM_PC_REGNUM == reg_dest)
11616 record_buf[arm_insn_r->reg_rec_count++] = ARM_PS_REGNUM;
11618 /* If wback is true, also save the base register, which is going to be
11619 written to. */
11620 if (wback)
11621 record_buf[arm_insn_r->reg_rec_count++] = reg_base;
11623 else
11625 /* STR (immediate), STRB (immediate), STRBT and STRT. */
11627 offset_12 = bits (arm_insn_r->arm_insn, 0, 11);
11628 regcache_raw_read_unsigned (reg_cache, reg_base, &u_regval);
11630 /* Handle bit U. */
11631 if (bit (arm_insn_r->arm_insn, 23))
11633 /* U == 1: Add the offset. */
11634 tgt_mem_addr = (uint32_t) u_regval + offset_12;
11636 else
11638 /* U == 0: subtract the offset. */
11639 tgt_mem_addr = (uint32_t) u_regval - offset_12;
11642 /* Bit 22 tells us whether the store instruction writes 1 byte or 4
11643 bytes. */
11644 if (bit (arm_insn_r->arm_insn, 22))
11646 /* STRB and STRBT: 1 byte. */
11647 record_buf_mem[0] = 1;
11649 else
11651 /* STR and STRT: 4 bytes. */
11652 record_buf_mem[0] = 4;
11655 /* Handle bit P. */
11656 if (bit (arm_insn_r->arm_insn, 24))
11657 record_buf_mem[1] = tgt_mem_addr;
11658 else
11659 record_buf_mem[1] = (uint32_t) u_regval;
11661 arm_insn_r->mem_rec_count = 1;
11663 /* If wback is true, also save the base register, which is going to be
11664 written to. */
11665 if (wback)
11666 record_buf[arm_insn_r->reg_rec_count++] = reg_base;
11669 REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
11670 MEM_ALLOC (arm_insn_r->arm_mems, arm_insn_r->mem_rec_count, record_buf_mem);
11671 return 0;
11674 /* Handling opcode 011 insns. */
11676 static int
11677 arm_record_ld_st_reg_offset (insn_decode_record *arm_insn_r)
11679 struct regcache *reg_cache = arm_insn_r->regcache;
11681 uint32_t shift_imm = 0;
11682 uint32_t reg_src1 = 0, reg_src2 = 0, reg_dest = 0;
11683 uint32_t offset_12 = 0, tgt_mem_addr = 0;
11684 uint32_t record_buf[8], record_buf_mem[8];
11686 LONGEST s_word;
11687 ULONGEST u_regval[2];
11689 if (bit (arm_insn_r->arm_insn, 4))
11690 return arm_record_media (arm_insn_r);
11692 arm_insn_r->opcode = bits (arm_insn_r->arm_insn, 21, 24);
11693 arm_insn_r->decode = bits (arm_insn_r->arm_insn, 4, 7);
11695 /* Handle enhanced store insns and LDRD DSP insn,
11696 order begins according to addressing modes for store insns
11697 STRH insn. */
11699 /* LDR or STR? */
11700 if (bit (arm_insn_r->arm_insn, INSN_S_L_BIT_NUM))
11702 reg_dest = bits (arm_insn_r->arm_insn, 12, 15);
11703 /* LDR insn has a capability to do branching, if
11704 MOV LR, PC is preceded by LDR insn having Rn as R15
11705 in that case, it emulates branch and link insn, and hence we
11706 need to save CSPR and PC as well. */
11707 if (15 != reg_dest)
11709 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11710 arm_insn_r->reg_rec_count = 1;
11712 else
11714 record_buf[0] = reg_dest;
11715 record_buf[1] = ARM_PS_REGNUM;
11716 arm_insn_r->reg_rec_count = 2;
11719 else
11721 if (! bits (arm_insn_r->arm_insn, 4, 11))
11723 /* Store insn, register offset and register pre-indexed,
11724 register post-indexed. */
11725 /* Get Rm. */
11726 reg_src1 = bits (arm_insn_r->arm_insn, 0, 3);
11727 /* Get Rn. */
11728 reg_src2 = bits (arm_insn_r->arm_insn, 16, 19);
11729 regcache_raw_read_unsigned (reg_cache, reg_src1
11730 , &u_regval[0]);
11731 regcache_raw_read_unsigned (reg_cache, reg_src2
11732 , &u_regval[1]);
11733 if (15 == reg_src2)
11735 /* If R15 was used as Rn, hence current PC+8. */
11736 /* Pre-indexed mode doesnt reach here ; illegal insn. */
11737 u_regval[0] = u_regval[0] + 8;
11739 /* Calculate target store address, Rn +/- Rm, register offset. */
11740 /* U == 1. */
11741 if (bit (arm_insn_r->arm_insn, 23))
11743 tgt_mem_addr = u_regval[0] + u_regval[1];
11745 else
11747 tgt_mem_addr = u_regval[1] - u_regval[0];
11750 switch (arm_insn_r->opcode)
11752 /* STR. */
11753 case 8:
11754 case 12:
11755 /* STR. */
11756 case 9:
11757 case 13:
11758 /* STRT. */
11759 case 1:
11760 case 5:
11761 /* STR. */
11762 case 0:
11763 case 4:
11764 record_buf_mem[0] = 4;
11765 break;
11767 /* STRB. */
11768 case 10:
11769 case 14:
11770 /* STRB. */
11771 case 11:
11772 case 15:
11773 /* STRBT. */
11774 case 3:
11775 case 7:
11776 /* STRB. */
11777 case 2:
11778 case 6:
11779 record_buf_mem[0] = 1;
11780 break;
11782 default:
11783 gdb_assert_not_reached ("no decoding pattern found");
11784 break;
11786 record_buf_mem[1] = tgt_mem_addr;
11787 arm_insn_r->mem_rec_count = 1;
11789 if (9 == arm_insn_r->opcode || 11 == arm_insn_r->opcode
11790 || 13 == arm_insn_r->opcode || 15 == arm_insn_r->opcode
11791 || 0 == arm_insn_r->opcode || 2 == arm_insn_r->opcode
11792 || 4 == arm_insn_r->opcode || 6 == arm_insn_r->opcode
11793 || 1 == arm_insn_r->opcode || 3 == arm_insn_r->opcode
11794 || 5 == arm_insn_r->opcode || 7 == arm_insn_r->opcode
11797 /* Rn is going to be changed in pre-indexed mode and
11798 post-indexed mode as well. */
11799 record_buf[0] = reg_src2;
11800 arm_insn_r->reg_rec_count = 1;
11803 else
11805 /* Store insn, scaled register offset; scaled pre-indexed. */
11806 offset_12 = bits (arm_insn_r->arm_insn, 5, 6);
11807 /* Get Rm. */
11808 reg_src1 = bits (arm_insn_r->arm_insn, 0, 3);
11809 /* Get Rn. */
11810 reg_src2 = bits (arm_insn_r->arm_insn, 16, 19);
11811 /* Get shift_imm. */
11812 shift_imm = bits (arm_insn_r->arm_insn, 7, 11);
11813 regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval[0]);
11814 regcache_raw_read_signed (reg_cache, reg_src1, &s_word);
11815 regcache_raw_read_unsigned (reg_cache, reg_src2, &u_regval[1]);
11816 /* Offset_12 used as shift. */
11817 switch (offset_12)
11819 case 0:
11820 /* Offset_12 used as index. */
11821 offset_12 = u_regval[0] << shift_imm;
11822 break;
11824 case 1:
11825 offset_12 = (!shift_imm)?0:u_regval[0] >> shift_imm;
11826 break;
11828 case 2:
11829 if (!shift_imm)
11831 if (bit (u_regval[0], 31))
11833 offset_12 = 0xFFFFFFFF;
11835 else
11837 offset_12 = 0;
11840 else
11842 /* This is arithmetic shift. */
11843 offset_12 = s_word >> shift_imm;
11845 break;
11847 case 3:
11848 if (!shift_imm)
11850 regcache_raw_read_unsigned (reg_cache, ARM_PS_REGNUM,
11851 &u_regval[1]);
11852 /* Get C flag value and shift it by 31. */
11853 offset_12 = (((bit (u_regval[1], 29)) << 31) \
11854 | (u_regval[0]) >> 1);
11856 else
11858 offset_12 = (u_regval[0] >> shift_imm) \
11859 | (u_regval[0] <<
11860 (sizeof(uint32_t) - shift_imm));
11862 break;
11864 default:
11865 gdb_assert_not_reached ("no decoding pattern found");
11866 break;
11869 regcache_raw_read_unsigned (reg_cache, reg_src2, &u_regval[1]);
11870 /* bit U set. */
11871 if (bit (arm_insn_r->arm_insn, 23))
11873 tgt_mem_addr = u_regval[1] + offset_12;
11875 else
11877 tgt_mem_addr = u_regval[1] - offset_12;
11880 switch (arm_insn_r->opcode)
11882 /* STR. */
11883 case 8:
11884 case 12:
11885 /* STR. */
11886 case 9:
11887 case 13:
11888 /* STRT. */
11889 case 1:
11890 case 5:
11891 /* STR. */
11892 case 0:
11893 case 4:
11894 record_buf_mem[0] = 4;
11895 break;
11897 /* STRB. */
11898 case 10:
11899 case 14:
11900 /* STRB. */
11901 case 11:
11902 case 15:
11903 /* STRBT. */
11904 case 3:
11905 case 7:
11906 /* STRB. */
11907 case 2:
11908 case 6:
11909 record_buf_mem[0] = 1;
11910 break;
11912 default:
11913 gdb_assert_not_reached ("no decoding pattern found");
11914 break;
11916 record_buf_mem[1] = tgt_mem_addr;
11917 arm_insn_r->mem_rec_count = 1;
11919 if (9 == arm_insn_r->opcode || 11 == arm_insn_r->opcode
11920 || 13 == arm_insn_r->opcode || 15 == arm_insn_r->opcode
11921 || 0 == arm_insn_r->opcode || 2 == arm_insn_r->opcode
11922 || 4 == arm_insn_r->opcode || 6 == arm_insn_r->opcode
11923 || 1 == arm_insn_r->opcode || 3 == arm_insn_r->opcode
11924 || 5 == arm_insn_r->opcode || 7 == arm_insn_r->opcode
11927 /* Rn is going to be changed in register scaled pre-indexed
11928 mode,and scaled post indexed mode. */
11929 record_buf[0] = reg_src2;
11930 arm_insn_r->reg_rec_count = 1;
11935 REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
11936 MEM_ALLOC (arm_insn_r->arm_mems, arm_insn_r->mem_rec_count, record_buf_mem);
11937 return 0;
11940 /* Handle ARM mode instructions with opcode 100. */
11942 static int
11943 arm_record_ld_st_multiple (insn_decode_record *arm_insn_r)
11945 struct regcache *reg_cache = arm_insn_r->regcache;
11946 uint32_t register_count = 0, register_bits;
11947 uint32_t reg_base, addr_mode;
11948 uint32_t record_buf[24], record_buf_mem[48];
11949 uint32_t wback;
11950 ULONGEST u_regval;
11952 /* Fetch the list of registers. */
11953 register_bits = bits (arm_insn_r->arm_insn, 0, 15);
11954 arm_insn_r->reg_rec_count = 0;
11956 /* Fetch the base register that contains the address we are loading data
11957 to. */
11958 reg_base = bits (arm_insn_r->arm_insn, 16, 19);
11960 /* Calculate wback. */
11961 wback = (bit (arm_insn_r->arm_insn, 21) == 1);
11963 if (bit (arm_insn_r->arm_insn, INSN_S_L_BIT_NUM))
11965 /* LDM/LDMIA/LDMFD, LDMDA/LDMFA, LDMDB and LDMIB. */
11967 /* Find out which registers are going to be loaded from memory. */
11968 while (register_bits)
11970 if (register_bits & 0x00000001)
11971 record_buf[arm_insn_r->reg_rec_count++] = register_count;
11972 register_bits = register_bits >> 1;
11973 register_count++;
11977 /* If wback is true, also save the base register, which is going to be
11978 written to. */
11979 if (wback)
11980 record_buf[arm_insn_r->reg_rec_count++] = reg_base;
11982 /* Save the CPSR register. */
11983 record_buf[arm_insn_r->reg_rec_count++] = ARM_PS_REGNUM;
11985 else
11987 /* STM (STMIA, STMEA), STMDA (STMED), STMDB (STMFD) and STMIB (STMFA). */
11989 addr_mode = bits (arm_insn_r->arm_insn, 23, 24);
11991 regcache_raw_read_unsigned (reg_cache, reg_base, &u_regval);
11993 /* Find out how many registers are going to be stored to memory. */
11994 while (register_bits)
11996 if (register_bits & 0x00000001)
11997 register_count++;
11998 register_bits = register_bits >> 1;
12001 switch (addr_mode)
12003 /* STMDA (STMED): Decrement after. */
12004 case 0:
12005 record_buf_mem[1] = (uint32_t) u_regval
12006 - register_count * ARM_INT_REGISTER_SIZE + 4;
12007 break;
12008 /* STM (STMIA, STMEA): Increment after. */
12009 case 1:
12010 record_buf_mem[1] = (uint32_t) u_regval;
12011 break;
12012 /* STMDB (STMFD): Decrement before. */
12013 case 2:
12014 record_buf_mem[1] = (uint32_t) u_regval
12015 - register_count * ARM_INT_REGISTER_SIZE;
12016 break;
12017 /* STMIB (STMFA): Increment before. */
12018 case 3:
12019 record_buf_mem[1] = (uint32_t) u_regval + ARM_INT_REGISTER_SIZE;
12020 break;
12021 default:
12022 gdb_assert_not_reached ("no decoding pattern found");
12023 break;
12026 record_buf_mem[0] = register_count * ARM_INT_REGISTER_SIZE;
12027 arm_insn_r->mem_rec_count = 1;
12029 /* If wback is true, also save the base register, which is going to be
12030 written to. */
12031 if (wback)
12032 record_buf[arm_insn_r->reg_rec_count++] = reg_base;
12035 REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
12036 MEM_ALLOC (arm_insn_r->arm_mems, arm_insn_r->mem_rec_count, record_buf_mem);
12037 return 0;
12040 /* Handling opcode 101 insns. */
12042 static int
12043 arm_record_b_bl (insn_decode_record *arm_insn_r)
12045 uint32_t record_buf[8];
12047 /* Handle B, BL, BLX(1) insns. */
12048 /* B simply branches so we do nothing here. */
12049 /* Note: BLX(1) doesnt fall here but instead it falls into
12050 extension space. */
12051 if (bit (arm_insn_r->arm_insn, 24))
12053 record_buf[0] = ARM_LR_REGNUM;
12054 arm_insn_r->reg_rec_count = 1;
12057 REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
12059 return 0;
12062 static int
12063 arm_record_unsupported_insn (insn_decode_record *arm_insn_r)
12065 gdb_printf (gdb_stderr,
12066 _("Process record does not support instruction "
12067 "0x%0x at address %s.\n"),arm_insn_r->arm_insn,
12068 paddress (arm_insn_r->gdbarch, arm_insn_r->this_addr));
12070 return -1;
12073 /* Record handler for vector data transfer instructions. */
12075 static int
12076 arm_record_vdata_transfer_insn (insn_decode_record *arm_insn_r)
12078 uint32_t bits_a, bit_c, bit_l, reg_t, reg_v;
12079 uint32_t record_buf[4];
12081 reg_t = bits (arm_insn_r->arm_insn, 12, 15);
12082 reg_v = bits (arm_insn_r->arm_insn, 21, 23);
12083 bits_a = bits (arm_insn_r->arm_insn, 21, 23);
12084 bit_l = bit (arm_insn_r->arm_insn, 20);
12085 bit_c = bit (arm_insn_r->arm_insn, 8);
12087 /* Handle VMOV instruction. */
12088 if (bit_l && bit_c)
12090 record_buf[0] = reg_t;
12091 arm_insn_r->reg_rec_count = 1;
12093 else if (bit_l && !bit_c)
12095 /* Handle VMOV instruction. */
12096 if (bits_a == 0x00)
12098 record_buf[0] = reg_t;
12099 arm_insn_r->reg_rec_count = 1;
12101 /* Handle VMRS instruction. */
12102 else if (bits_a == 0x07)
12104 if (reg_t == 15)
12105 reg_t = ARM_PS_REGNUM;
12107 record_buf[0] = reg_t;
12108 arm_insn_r->reg_rec_count = 1;
12111 else if (!bit_l && !bit_c)
12113 /* Handle VMOV instruction. */
12114 if (bits_a == 0x00)
12116 record_buf[0] = ARM_D0_REGNUM + reg_v;
12118 arm_insn_r->reg_rec_count = 1;
12120 /* Handle VMSR instruction. */
12121 else if (bits_a == 0x07)
12123 record_buf[0] = ARM_FPSCR_REGNUM;
12124 arm_insn_r->reg_rec_count = 1;
12127 else if (!bit_l && bit_c)
12129 /* Handle VMOV instruction. */
12130 if (!(bits_a & 0x04))
12132 record_buf[0] = (reg_v | (bit (arm_insn_r->arm_insn, 7) << 4))
12133 + ARM_D0_REGNUM;
12134 arm_insn_r->reg_rec_count = 1;
12136 /* Handle VDUP instruction. */
12137 else
12139 if (bit (arm_insn_r->arm_insn, 21))
12141 reg_v = reg_v | (bit (arm_insn_r->arm_insn, 7) << 4);
12142 record_buf[0] = reg_v + ARM_D0_REGNUM;
12143 record_buf[1] = reg_v + ARM_D0_REGNUM + 1;
12144 arm_insn_r->reg_rec_count = 2;
12146 else
12148 reg_v = reg_v | (bit (arm_insn_r->arm_insn, 7) << 4);
12149 record_buf[0] = reg_v + ARM_D0_REGNUM;
12150 arm_insn_r->reg_rec_count = 1;
12155 REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
12156 return 0;
12159 /* Record handler for extension register load/store instructions. */
12161 static int
12162 arm_record_exreg_ld_st_insn (insn_decode_record *arm_insn_r)
12164 uint32_t opcode, single_reg;
12165 uint8_t op_vldm_vstm;
12166 uint32_t record_buf[8], record_buf_mem[128];
12167 ULONGEST u_regval = 0;
12169 struct regcache *reg_cache = arm_insn_r->regcache;
12171 opcode = bits (arm_insn_r->arm_insn, 20, 24);
12172 single_reg = !bit (arm_insn_r->arm_insn, 8);
12173 op_vldm_vstm = opcode & 0x1b;
12175 /* Handle VMOV instructions. */
12176 if ((opcode & 0x1e) == 0x04)
12178 if (bit (arm_insn_r->arm_insn, 20)) /* to_arm_registers bit 20? */
12180 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
12181 record_buf[1] = bits (arm_insn_r->arm_insn, 16, 19);
12182 arm_insn_r->reg_rec_count = 2;
12184 else
12186 uint8_t reg_m = bits (arm_insn_r->arm_insn, 0, 3);
12187 uint8_t bit_m = bit (arm_insn_r->arm_insn, 5);
12189 if (single_reg)
12191 /* The first S register number m is REG_M:M (M is bit 5),
12192 the corresponding D register number is REG_M:M / 2, which
12193 is REG_M. */
12194 record_buf[arm_insn_r->reg_rec_count++] = ARM_D0_REGNUM + reg_m;
12195 /* The second S register number is REG_M:M + 1, the
12196 corresponding D register number is (REG_M:M + 1) / 2.
12197 IOW, if bit M is 1, the first and second S registers
12198 are mapped to different D registers, otherwise, they are
12199 in the same D register. */
12200 if (bit_m)
12202 record_buf[arm_insn_r->reg_rec_count++]
12203 = ARM_D0_REGNUM + reg_m + 1;
12206 else
12208 record_buf[0] = ((bit_m << 4) + reg_m + ARM_D0_REGNUM);
12209 arm_insn_r->reg_rec_count = 1;
12213 /* Handle VSTM and VPUSH instructions. */
12214 else if (op_vldm_vstm == 0x08 || op_vldm_vstm == 0x0a
12215 || op_vldm_vstm == 0x12)
12217 uint32_t start_address, reg_rn, imm_off32, imm_off8, memory_count;
12218 uint32_t memory_index = 0;
12220 reg_rn = bits (arm_insn_r->arm_insn, 16, 19);
12221 regcache_raw_read_unsigned (reg_cache, reg_rn, &u_regval);
12222 imm_off8 = bits (arm_insn_r->arm_insn, 0, 7);
12223 imm_off32 = imm_off8 << 2;
12224 memory_count = imm_off8;
12226 if (bit (arm_insn_r->arm_insn, 23))
12227 start_address = u_regval;
12228 else
12229 start_address = u_regval - imm_off32;
12231 if (bit (arm_insn_r->arm_insn, 21))
12233 record_buf[0] = reg_rn;
12234 arm_insn_r->reg_rec_count = 1;
12237 while (memory_count > 0)
12239 if (single_reg)
12241 record_buf_mem[memory_index] = 4;
12242 record_buf_mem[memory_index + 1] = start_address;
12243 start_address = start_address + 4;
12244 memory_index = memory_index + 2;
12246 else
12248 record_buf_mem[memory_index] = 4;
12249 record_buf_mem[memory_index + 1] = start_address;
12250 record_buf_mem[memory_index + 2] = 4;
12251 record_buf_mem[memory_index + 3] = start_address + 4;
12252 start_address = start_address + 8;
12253 memory_index = memory_index + 4;
12255 memory_count--;
12257 arm_insn_r->mem_rec_count = (memory_index >> 1);
12259 /* Handle VLDM instructions. */
12260 else if (op_vldm_vstm == 0x09 || op_vldm_vstm == 0x0b
12261 || op_vldm_vstm == 0x13)
12263 uint32_t reg_count, reg_vd;
12264 uint32_t reg_index = 0;
12265 uint32_t bit_d = bit (arm_insn_r->arm_insn, 22);
12267 reg_vd = bits (arm_insn_r->arm_insn, 12, 15);
12268 reg_count = bits (arm_insn_r->arm_insn, 0, 7);
12270 /* REG_VD is the first D register number. If the instruction
12271 loads memory to S registers (SINGLE_REG is TRUE), the register
12272 number is (REG_VD << 1 | bit D), so the corresponding D
12273 register number is (REG_VD << 1 | bit D) / 2 = REG_VD. */
12274 if (!single_reg)
12275 reg_vd = reg_vd | (bit_d << 4);
12277 if (bit (arm_insn_r->arm_insn, 21) /* write back */)
12278 record_buf[reg_index++] = bits (arm_insn_r->arm_insn, 16, 19);
12280 /* If the instruction loads memory to D register, REG_COUNT should
12281 be divided by 2, according to the ARM Architecture Reference
12282 Manual. If the instruction loads memory to S register, divide by
12283 2 as well because two S registers are mapped to D register. */
12284 reg_count = reg_count / 2;
12285 if (single_reg && bit_d)
12287 /* Increase the register count if S register list starts from
12288 an odd number (bit d is one). */
12289 reg_count++;
12292 while (reg_count > 0)
12294 record_buf[reg_index++] = ARM_D0_REGNUM + reg_vd + reg_count - 1;
12295 reg_count--;
12297 arm_insn_r->reg_rec_count = reg_index;
12299 /* VSTR Vector store register. */
12300 else if ((opcode & 0x13) == 0x10)
12302 uint32_t start_address, reg_rn, imm_off32, imm_off8;
12303 uint32_t memory_index = 0;
12305 reg_rn = bits (arm_insn_r->arm_insn, 16, 19);
12306 regcache_raw_read_unsigned (reg_cache, reg_rn, &u_regval);
12307 imm_off8 = bits (arm_insn_r->arm_insn, 0, 7);
12308 imm_off32 = imm_off8 << 2;
12310 if (bit (arm_insn_r->arm_insn, 23))
12311 start_address = u_regval + imm_off32;
12312 else
12313 start_address = u_regval - imm_off32;
12315 if (single_reg)
12317 record_buf_mem[memory_index] = 4;
12318 record_buf_mem[memory_index + 1] = start_address;
12319 arm_insn_r->mem_rec_count = 1;
12321 else
12323 record_buf_mem[memory_index] = 4;
12324 record_buf_mem[memory_index + 1] = start_address;
12325 record_buf_mem[memory_index + 2] = 4;
12326 record_buf_mem[memory_index + 3] = start_address + 4;
12327 arm_insn_r->mem_rec_count = 2;
12330 /* VLDR Vector load register. */
12331 else if ((opcode & 0x13) == 0x11)
12333 uint32_t reg_vd = bits (arm_insn_r->arm_insn, 12, 15);
12335 if (!single_reg)
12337 reg_vd = reg_vd | (bit (arm_insn_r->arm_insn, 22) << 4);
12338 record_buf[0] = ARM_D0_REGNUM + reg_vd;
12340 else
12342 reg_vd = (reg_vd << 1) | bit (arm_insn_r->arm_insn, 22);
12343 /* Record register D rather than pseudo register S. */
12344 record_buf[0] = ARM_D0_REGNUM + reg_vd / 2;
12346 arm_insn_r->reg_rec_count = 1;
12349 REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
12350 MEM_ALLOC (arm_insn_r->arm_mems, arm_insn_r->mem_rec_count, record_buf_mem);
12351 return 0;
12354 /* Record handler for arm/thumb mode VFP data processing instructions. */
12356 static int
12357 arm_record_vfp_data_proc_insn (insn_decode_record *arm_insn_r)
12359 uint32_t opc1, opc2, opc3, dp_op_sz, bit_d, reg_vd;
12360 uint32_t record_buf[4];
12361 enum insn_types {INSN_T0, INSN_T1, INSN_T2, INSN_T3, INSN_INV};
12362 enum insn_types curr_insn_type = INSN_INV;
12364 reg_vd = bits (arm_insn_r->arm_insn, 12, 15);
12365 opc1 = bits (arm_insn_r->arm_insn, 20, 23);
12366 opc2 = bits (arm_insn_r->arm_insn, 16, 19);
12367 opc3 = bits (arm_insn_r->arm_insn, 6, 7);
12368 dp_op_sz = bit (arm_insn_r->arm_insn, 8);
12369 bit_d = bit (arm_insn_r->arm_insn, 22);
12370 /* Mask off the "D" bit. */
12371 opc1 = opc1 & ~0x04;
12373 /* Handle VMLA, VMLS. */
12374 if (opc1 == 0x00)
12376 if (bit (arm_insn_r->arm_insn, 10))
12378 if (bit (arm_insn_r->arm_insn, 6))
12379 curr_insn_type = INSN_T0;
12380 else
12381 curr_insn_type = INSN_T1;
12383 else
12385 if (dp_op_sz)
12386 curr_insn_type = INSN_T1;
12387 else
12388 curr_insn_type = INSN_T2;
12391 /* Handle VNMLA, VNMLS, VNMUL. */
12392 else if (opc1 == 0x01)
12394 if (dp_op_sz)
12395 curr_insn_type = INSN_T1;
12396 else
12397 curr_insn_type = INSN_T2;
12399 /* Handle VMUL. */
12400 else if (opc1 == 0x02 && !(opc3 & 0x01))
12402 if (bit (arm_insn_r->arm_insn, 10))
12404 if (bit (arm_insn_r->arm_insn, 6))
12405 curr_insn_type = INSN_T0;
12406 else
12407 curr_insn_type = INSN_T1;
12409 else
12411 if (dp_op_sz)
12412 curr_insn_type = INSN_T1;
12413 else
12414 curr_insn_type = INSN_T2;
12417 /* Handle VADD, VSUB. */
12418 else if (opc1 == 0x03)
12420 if (!bit (arm_insn_r->arm_insn, 9))
12422 if (bit (arm_insn_r->arm_insn, 6))
12423 curr_insn_type = INSN_T0;
12424 else
12425 curr_insn_type = INSN_T1;
12427 else
12429 if (dp_op_sz)
12430 curr_insn_type = INSN_T1;
12431 else
12432 curr_insn_type = INSN_T2;
12435 /* Handle VDIV. */
12436 else if (opc1 == 0x08)
12438 if (dp_op_sz)
12439 curr_insn_type = INSN_T1;
12440 else
12441 curr_insn_type = INSN_T2;
12443 /* Handle all other vfp data processing instructions. */
12444 else if (opc1 == 0x0b)
12446 /* Handle VMOV. */
12447 if (!(opc3 & 0x01) || (opc2 == 0x00 && opc3 == 0x01))
12449 if (bit (arm_insn_r->arm_insn, 4))
12451 if (bit (arm_insn_r->arm_insn, 6))
12452 curr_insn_type = INSN_T0;
12453 else
12454 curr_insn_type = INSN_T1;
12456 else
12458 if (dp_op_sz)
12459 curr_insn_type = INSN_T1;
12460 else
12461 curr_insn_type = INSN_T2;
12464 /* Handle VNEG and VABS. */
12465 else if ((opc2 == 0x01 && opc3 == 0x01)
12466 || (opc2 == 0x00 && opc3 == 0x03))
12468 if (!bit (arm_insn_r->arm_insn, 11))
12470 if (bit (arm_insn_r->arm_insn, 6))
12471 curr_insn_type = INSN_T0;
12472 else
12473 curr_insn_type = INSN_T1;
12475 else
12477 if (dp_op_sz)
12478 curr_insn_type = INSN_T1;
12479 else
12480 curr_insn_type = INSN_T2;
12483 /* Handle VSQRT. */
12484 else if (opc2 == 0x01 && opc3 == 0x03)
12486 if (dp_op_sz)
12487 curr_insn_type = INSN_T1;
12488 else
12489 curr_insn_type = INSN_T2;
12491 /* Handle VCVT. */
12492 else if (opc2 == 0x07 && opc3 == 0x03)
12494 if (!dp_op_sz)
12495 curr_insn_type = INSN_T1;
12496 else
12497 curr_insn_type = INSN_T2;
12499 else if (opc3 & 0x01)
12501 /* Handle VCVT. */
12502 if ((opc2 == 0x08) || (opc2 & 0x0e) == 0x0c)
12504 if (!bit (arm_insn_r->arm_insn, 18))
12505 curr_insn_type = INSN_T2;
12506 else
12508 if (dp_op_sz)
12509 curr_insn_type = INSN_T1;
12510 else
12511 curr_insn_type = INSN_T2;
12514 /* Handle VCVT. */
12515 else if ((opc2 & 0x0e) == 0x0a || (opc2 & 0x0e) == 0x0e)
12517 if (dp_op_sz)
12518 curr_insn_type = INSN_T1;
12519 else
12520 curr_insn_type = INSN_T2;
12522 /* Handle VCVTB, VCVTT. */
12523 else if ((opc2 & 0x0e) == 0x02)
12524 curr_insn_type = INSN_T2;
12525 /* Handle VCMP, VCMPE. */
12526 else if ((opc2 & 0x0e) == 0x04)
12527 curr_insn_type = INSN_T3;
12531 switch (curr_insn_type)
12533 case INSN_T0:
12534 reg_vd = reg_vd | (bit_d << 4);
12535 record_buf[0] = reg_vd + ARM_D0_REGNUM;
12536 record_buf[1] = reg_vd + ARM_D0_REGNUM + 1;
12537 arm_insn_r->reg_rec_count = 2;
12538 break;
12540 case INSN_T1:
12541 reg_vd = reg_vd | (bit_d << 4);
12542 record_buf[0] = reg_vd + ARM_D0_REGNUM;
12543 arm_insn_r->reg_rec_count = 1;
12544 break;
12546 case INSN_T2:
12547 reg_vd = (reg_vd << 1) | bit_d;
12548 record_buf[0] = reg_vd + ARM_D0_REGNUM;
12549 arm_insn_r->reg_rec_count = 1;
12550 break;
12552 case INSN_T3:
12553 record_buf[0] = ARM_FPSCR_REGNUM;
12554 arm_insn_r->reg_rec_count = 1;
12555 break;
12557 default:
12558 gdb_assert_not_reached ("no decoding pattern found");
12559 break;
12562 REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
12563 return 0;
12566 /* Handling opcode 110 insns. */
12568 static int
12569 arm_record_asimd_vfp_coproc (insn_decode_record *arm_insn_r)
12571 uint32_t op1, op1_ebit, coproc;
12573 coproc = bits (arm_insn_r->arm_insn, 8, 11);
12574 op1 = bits (arm_insn_r->arm_insn, 20, 25);
12575 op1_ebit = bit (arm_insn_r->arm_insn, 20);
12577 if ((coproc & 0x0e) == 0x0a)
12579 /* Handle extension register ld/st instructions. */
12580 if (!(op1 & 0x20))
12581 return arm_record_exreg_ld_st_insn (arm_insn_r);
12583 /* 64-bit transfers between arm core and extension registers. */
12584 if ((op1 & 0x3e) == 0x04)
12585 return arm_record_exreg_ld_st_insn (arm_insn_r);
12587 else
12589 /* Handle coprocessor ld/st instructions. */
12590 if (!(op1 & 0x3a))
12592 /* Store. */
12593 if (!op1_ebit)
12594 return arm_record_unsupported_insn (arm_insn_r);
12595 else
12596 /* Load. */
12597 return arm_record_unsupported_insn (arm_insn_r);
12600 /* Move to coprocessor from two arm core registers. */
12601 if (op1 == 0x4)
12602 return arm_record_unsupported_insn (arm_insn_r);
12604 /* Move to two arm core registers from coprocessor. */
12605 if (op1 == 0x5)
12607 uint32_t reg_t[2];
12609 reg_t[0] = bits (arm_insn_r->arm_insn, 12, 15);
12610 reg_t[1] = bits (arm_insn_r->arm_insn, 16, 19);
12611 arm_insn_r->reg_rec_count = 2;
12613 REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, reg_t);
12614 return 0;
12617 return arm_record_unsupported_insn (arm_insn_r);
12620 /* Handling opcode 111 insns. */
12622 static int
12623 arm_record_coproc_data_proc (insn_decode_record *arm_insn_r)
12625 uint32_t op, op1_ebit, coproc, bits_24_25;
12626 arm_gdbarch_tdep *tdep
12627 = (arm_gdbarch_tdep *) gdbarch_tdep (arm_insn_r->gdbarch);
12628 struct regcache *reg_cache = arm_insn_r->regcache;
12630 arm_insn_r->opcode = bits (arm_insn_r->arm_insn, 24, 27);
12631 coproc = bits (arm_insn_r->arm_insn, 8, 11);
12632 op1_ebit = bit (arm_insn_r->arm_insn, 20);
12633 op = bit (arm_insn_r->arm_insn, 4);
12634 bits_24_25 = bits (arm_insn_r->arm_insn, 24, 25);
12636 /* Handle arm SWI/SVC system call instructions. */
12637 if (bits_24_25 == 0x3)
12639 if (tdep->arm_syscall_record != NULL)
12641 ULONGEST svc_operand, svc_number;
12643 svc_operand = (0x00ffffff & arm_insn_r->arm_insn);
12645 if (svc_operand) /* OABI. */
12646 svc_number = svc_operand - 0x900000;
12647 else /* EABI. */
12648 regcache_raw_read_unsigned (reg_cache, 7, &svc_number);
12650 return tdep->arm_syscall_record (reg_cache, svc_number);
12652 else
12654 gdb_printf (gdb_stderr, _("no syscall record support\n"));
12655 return -1;
12658 else if (bits_24_25 == 0x02)
12660 if (op)
12662 if ((coproc & 0x0e) == 0x0a)
12664 /* 8, 16, and 32-bit transfer */
12665 return arm_record_vdata_transfer_insn (arm_insn_r);
12667 else
12669 if (op1_ebit)
12671 /* MRC, MRC2 */
12672 uint32_t record_buf[1];
12674 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
12675 if (record_buf[0] == 15)
12676 record_buf[0] = ARM_PS_REGNUM;
12678 arm_insn_r->reg_rec_count = 1;
12679 REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count,
12680 record_buf);
12681 return 0;
12683 else
12685 /* MCR, MCR2 */
12686 return -1;
12690 else
12692 if ((coproc & 0x0e) == 0x0a)
12694 /* VFP data-processing instructions. */
12695 return arm_record_vfp_data_proc_insn (arm_insn_r);
12697 else
12699 /* CDP, CDP2 */
12700 return -1;
12704 else
12706 unsigned int op1 = bits (arm_insn_r->arm_insn, 20, 25);
12708 if (op1 == 5)
12710 if ((coproc & 0x0e) != 0x0a)
12712 /* MRRC, MRRC2 */
12713 return -1;
12716 else if (op1 == 4 || op1 == 5)
12718 if ((coproc & 0x0e) == 0x0a)
12720 /* 64-bit transfers between ARM core and extension */
12721 return -1;
12723 else if (op1 == 4)
12725 /* MCRR, MCRR2 */
12726 return -1;
12729 else if (op1 == 0 || op1 == 1)
12731 /* UNDEFINED */
12732 return -1;
12734 else
12736 if ((coproc & 0x0e) == 0x0a)
12738 /* Extension register load/store */
12740 else
12742 /* STC, STC2, LDC, LDC2 */
12744 return -1;
12748 return -1;
12751 /* Handling opcode 000 insns. */
12753 static int
12754 thumb_record_shift_add_sub (insn_decode_record *thumb_insn_r)
12756 uint32_t record_buf[8];
12757 uint32_t reg_src1 = 0;
12759 reg_src1 = bits (thumb_insn_r->arm_insn, 0, 2);
12761 record_buf[0] = ARM_PS_REGNUM;
12762 record_buf[1] = reg_src1;
12763 thumb_insn_r->reg_rec_count = 2;
12765 REG_ALLOC (thumb_insn_r->arm_regs, thumb_insn_r->reg_rec_count, record_buf);
12767 return 0;
12771 /* Handling opcode 001 insns. */
12773 static int
12774 thumb_record_add_sub_cmp_mov (insn_decode_record *thumb_insn_r)
12776 uint32_t record_buf[8];
12777 uint32_t reg_src1 = 0;
12779 reg_src1 = bits (thumb_insn_r->arm_insn, 8, 10);
12781 record_buf[0] = ARM_PS_REGNUM;
12782 record_buf[1] = reg_src1;
12783 thumb_insn_r->reg_rec_count = 2;
12785 REG_ALLOC (thumb_insn_r->arm_regs, thumb_insn_r->reg_rec_count, record_buf);
12787 return 0;
12790 /* Handling opcode 010 insns. */
12792 static int
12793 thumb_record_ld_st_reg_offset (insn_decode_record *thumb_insn_r)
12795 struct regcache *reg_cache = thumb_insn_r->regcache;
12796 uint32_t record_buf[8], record_buf_mem[8];
12798 uint32_t reg_src1 = 0, reg_src2 = 0;
12799 uint32_t opcode1 = 0, opcode2 = 0, opcode3 = 0;
12801 ULONGEST u_regval[2] = {0};
12803 opcode1 = bits (thumb_insn_r->arm_insn, 10, 12);
12805 if (bit (thumb_insn_r->arm_insn, 12))
12807 /* Handle load/store register offset. */
12808 uint32_t opB = bits (thumb_insn_r->arm_insn, 9, 11);
12810 if (in_inclusive_range (opB, 4U, 7U))
12812 /* LDR(2), LDRB(2) , LDRH(2), LDRSB, LDRSH. */
12813 reg_src1 = bits (thumb_insn_r->arm_insn,0, 2);
12814 record_buf[0] = reg_src1;
12815 thumb_insn_r->reg_rec_count = 1;
12817 else if (in_inclusive_range (opB, 0U, 2U))
12819 /* STR(2), STRB(2), STRH(2) . */
12820 reg_src1 = bits (thumb_insn_r->arm_insn, 3, 5);
12821 reg_src2 = bits (thumb_insn_r->arm_insn, 6, 8);
12822 regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval[0]);
12823 regcache_raw_read_unsigned (reg_cache, reg_src2, &u_regval[1]);
12824 if (0 == opB)
12825 record_buf_mem[0] = 4; /* STR (2). */
12826 else if (2 == opB)
12827 record_buf_mem[0] = 1; /* STRB (2). */
12828 else if (1 == opB)
12829 record_buf_mem[0] = 2; /* STRH (2). */
12830 record_buf_mem[1] = u_regval[0] + u_regval[1];
12831 thumb_insn_r->mem_rec_count = 1;
12834 else if (bit (thumb_insn_r->arm_insn, 11))
12836 /* Handle load from literal pool. */
12837 /* LDR(3). */
12838 reg_src1 = bits (thumb_insn_r->arm_insn, 8, 10);
12839 record_buf[0] = reg_src1;
12840 thumb_insn_r->reg_rec_count = 1;
12842 else if (opcode1)
12844 /* Special data instructions and branch and exchange */
12845 opcode2 = bits (thumb_insn_r->arm_insn, 8, 9);
12846 opcode3 = bits (thumb_insn_r->arm_insn, 0, 2);
12847 if ((3 == opcode2) && (!opcode3))
12849 /* Branch with exchange. */
12850 record_buf[0] = ARM_PS_REGNUM;
12851 thumb_insn_r->reg_rec_count = 1;
12853 else
12855 /* Format 8; special data processing insns. */
12856 record_buf[0] = ARM_PS_REGNUM;
12857 record_buf[1] = (bit (thumb_insn_r->arm_insn, 7) << 3
12858 | bits (thumb_insn_r->arm_insn, 0, 2));
12859 thumb_insn_r->reg_rec_count = 2;
12862 else
12864 /* Format 5; data processing insns. */
12865 reg_src1 = bits (thumb_insn_r->arm_insn, 0, 2);
12866 if (bit (thumb_insn_r->arm_insn, 7))
12868 reg_src1 = reg_src1 + 8;
12870 record_buf[0] = ARM_PS_REGNUM;
12871 record_buf[1] = reg_src1;
12872 thumb_insn_r->reg_rec_count = 2;
12875 REG_ALLOC (thumb_insn_r->arm_regs, thumb_insn_r->reg_rec_count, record_buf);
12876 MEM_ALLOC (thumb_insn_r->arm_mems, thumb_insn_r->mem_rec_count,
12877 record_buf_mem);
12879 return 0;
12882 /* Handling opcode 001 insns. */
12884 static int
12885 thumb_record_ld_st_imm_offset (insn_decode_record *thumb_insn_r)
12887 struct regcache *reg_cache = thumb_insn_r->regcache;
12888 uint32_t record_buf[8], record_buf_mem[8];
12890 uint32_t reg_src1 = 0;
12891 uint32_t opcode = 0, immed_5 = 0;
12893 ULONGEST u_regval = 0;
12895 opcode = bits (thumb_insn_r->arm_insn, 11, 12);
12897 if (opcode)
12899 /* LDR(1). */
12900 reg_src1 = bits (thumb_insn_r->arm_insn, 0, 2);
12901 record_buf[0] = reg_src1;
12902 thumb_insn_r->reg_rec_count = 1;
12904 else
12906 /* STR(1). */
12907 reg_src1 = bits (thumb_insn_r->arm_insn, 3, 5);
12908 immed_5 = bits (thumb_insn_r->arm_insn, 6, 10);
12909 regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval);
12910 record_buf_mem[0] = 4;
12911 record_buf_mem[1] = u_regval + (immed_5 * 4);
12912 thumb_insn_r->mem_rec_count = 1;
12915 REG_ALLOC (thumb_insn_r->arm_regs, thumb_insn_r->reg_rec_count, record_buf);
12916 MEM_ALLOC (thumb_insn_r->arm_mems, thumb_insn_r->mem_rec_count,
12917 record_buf_mem);
12919 return 0;
12922 /* Handling opcode 100 insns. */
12924 static int
12925 thumb_record_ld_st_stack (insn_decode_record *thumb_insn_r)
12927 struct regcache *reg_cache = thumb_insn_r->regcache;
12928 uint32_t record_buf[8], record_buf_mem[8];
12930 uint32_t reg_src1 = 0;
12931 uint32_t opcode = 0, immed_8 = 0, immed_5 = 0;
12933 ULONGEST u_regval = 0;
12935 opcode = bits (thumb_insn_r->arm_insn, 11, 12);
12937 if (3 == opcode)
12939 /* LDR(4). */
12940 reg_src1 = bits (thumb_insn_r->arm_insn, 8, 10);
12941 record_buf[0] = reg_src1;
12942 thumb_insn_r->reg_rec_count = 1;
12944 else if (1 == opcode)
12946 /* LDRH(1). */
12947 reg_src1 = bits (thumb_insn_r->arm_insn, 0, 2);
12948 record_buf[0] = reg_src1;
12949 thumb_insn_r->reg_rec_count = 1;
12951 else if (2 == opcode)
12953 /* STR(3). */
12954 immed_8 = bits (thumb_insn_r->arm_insn, 0, 7);
12955 regcache_raw_read_unsigned (reg_cache, ARM_SP_REGNUM, &u_regval);
12956 record_buf_mem[0] = 4;
12957 record_buf_mem[1] = u_regval + (immed_8 * 4);
12958 thumb_insn_r->mem_rec_count = 1;
12960 else if (0 == opcode)
12962 /* STRH(1). */
12963 immed_5 = bits (thumb_insn_r->arm_insn, 6, 10);
12964 reg_src1 = bits (thumb_insn_r->arm_insn, 3, 5);
12965 regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval);
12966 record_buf_mem[0] = 2;
12967 record_buf_mem[1] = u_regval + (immed_5 * 2);
12968 thumb_insn_r->mem_rec_count = 1;
12971 REG_ALLOC (thumb_insn_r->arm_regs, thumb_insn_r->reg_rec_count, record_buf);
12972 MEM_ALLOC (thumb_insn_r->arm_mems, thumb_insn_r->mem_rec_count,
12973 record_buf_mem);
12975 return 0;
12978 /* Handling opcode 101 insns. */
12980 static int
12981 thumb_record_misc (insn_decode_record *thumb_insn_r)
12983 struct regcache *reg_cache = thumb_insn_r->regcache;
12985 uint32_t opcode = 0;
12986 uint32_t register_bits = 0, register_count = 0;
12987 uint32_t index = 0, start_address = 0;
12988 uint32_t record_buf[24], record_buf_mem[48];
12989 uint32_t reg_src1;
12991 ULONGEST u_regval = 0;
12993 opcode = bits (thumb_insn_r->arm_insn, 11, 12);
12995 if (opcode == 0 || opcode == 1)
12997 /* ADR and ADD (SP plus immediate) */
12999 reg_src1 = bits (thumb_insn_r->arm_insn, 8, 10);
13000 record_buf[0] = reg_src1;
13001 thumb_insn_r->reg_rec_count = 1;
13003 else
13005 /* Miscellaneous 16-bit instructions */
13006 uint32_t opcode2 = bits (thumb_insn_r->arm_insn, 8, 11);
13008 switch (opcode2)
13010 case 6:
13011 /* SETEND and CPS */
13012 break;
13013 case 0:
13014 /* ADD/SUB (SP plus immediate) */
13015 reg_src1 = bits (thumb_insn_r->arm_insn, 8, 10);
13016 record_buf[0] = ARM_SP_REGNUM;
13017 thumb_insn_r->reg_rec_count = 1;
13018 break;
13019 case 1: /* fall through */
13020 case 3: /* fall through */
13021 case 9: /* fall through */
13022 case 11:
13023 /* CBNZ, CBZ */
13024 break;
13025 case 2:
13026 /* SXTH, SXTB, UXTH, UXTB */
13027 record_buf[0] = bits (thumb_insn_r->arm_insn, 0, 2);
13028 thumb_insn_r->reg_rec_count = 1;
13029 break;
13030 case 4: /* fall through */
13031 case 5:
13032 /* PUSH. */
13033 register_bits = bits (thumb_insn_r->arm_insn, 0, 7);
13034 regcache_raw_read_unsigned (reg_cache, ARM_SP_REGNUM, &u_regval);
13035 while (register_bits)
13037 if (register_bits & 0x00000001)
13038 register_count++;
13039 register_bits = register_bits >> 1;
13041 start_address = u_regval - \
13042 (4 * (bit (thumb_insn_r->arm_insn, 8) + register_count));
13043 thumb_insn_r->mem_rec_count = register_count;
13044 while (register_count)
13046 record_buf_mem[(register_count * 2) - 1] = start_address;
13047 record_buf_mem[(register_count * 2) - 2] = 4;
13048 start_address = start_address + 4;
13049 register_count--;
13051 record_buf[0] = ARM_SP_REGNUM;
13052 thumb_insn_r->reg_rec_count = 1;
13053 break;
13054 case 10:
13055 /* REV, REV16, REVSH */
13056 record_buf[0] = bits (thumb_insn_r->arm_insn, 0, 2);
13057 thumb_insn_r->reg_rec_count = 1;
13058 break;
13059 case 12: /* fall through */
13060 case 13:
13061 /* POP. */
13062 register_bits = bits (thumb_insn_r->arm_insn, 0, 7);
13063 while (register_bits)
13065 if (register_bits & 0x00000001)
13066 record_buf[index++] = register_count;
13067 register_bits = register_bits >> 1;
13068 register_count++;
13070 record_buf[index++] = ARM_PS_REGNUM;
13071 record_buf[index++] = ARM_SP_REGNUM;
13072 thumb_insn_r->reg_rec_count = index;
13073 break;
13074 case 0xe:
13075 /* BKPT insn. */
13076 /* Handle enhanced software breakpoint insn, BKPT. */
13077 /* CPSR is changed to be executed in ARM state, disabling normal
13078 interrupts, entering abort mode. */
13079 /* According to high vector configuration PC is set. */
13080 /* User hits breakpoint and type reverse, in that case, we need to go back with
13081 previous CPSR and Program Counter. */
13082 record_buf[0] = ARM_PS_REGNUM;
13083 record_buf[1] = ARM_LR_REGNUM;
13084 thumb_insn_r->reg_rec_count = 2;
13085 /* We need to save SPSR value, which is not yet done. */
13086 gdb_printf (gdb_stderr,
13087 _("Process record does not support instruction "
13088 "0x%0x at address %s.\n"),
13089 thumb_insn_r->arm_insn,
13090 paddress (thumb_insn_r->gdbarch,
13091 thumb_insn_r->this_addr));
13092 return -1;
13094 case 0xf:
13095 /* If-Then, and hints */
13096 break;
13097 default:
13098 return -1;
13102 REG_ALLOC (thumb_insn_r->arm_regs, thumb_insn_r->reg_rec_count, record_buf);
13103 MEM_ALLOC (thumb_insn_r->arm_mems, thumb_insn_r->mem_rec_count,
13104 record_buf_mem);
13106 return 0;
13109 /* Handling opcode 110 insns. */
13111 static int
13112 thumb_record_ldm_stm_swi (insn_decode_record *thumb_insn_r)
13114 arm_gdbarch_tdep *tdep
13115 = (arm_gdbarch_tdep *) gdbarch_tdep (thumb_insn_r->gdbarch);
13116 struct regcache *reg_cache = thumb_insn_r->regcache;
13118 uint32_t ret = 0; /* function return value: -1:record failure ; 0:success */
13119 uint32_t reg_src1 = 0;
13120 uint32_t opcode1 = 0, opcode2 = 0, register_bits = 0, register_count = 0;
13121 uint32_t index = 0, start_address = 0;
13122 uint32_t record_buf[24], record_buf_mem[48];
13124 ULONGEST u_regval = 0;
13126 opcode1 = bits (thumb_insn_r->arm_insn, 8, 12);
13127 opcode2 = bits (thumb_insn_r->arm_insn, 11, 12);
13129 if (1 == opcode2)
13132 /* LDMIA. */
13133 register_bits = bits (thumb_insn_r->arm_insn, 0, 7);
13134 /* Get Rn. */
13135 reg_src1 = bits (thumb_insn_r->arm_insn, 8, 10);
13136 while (register_bits)
13138 if (register_bits & 0x00000001)
13139 record_buf[index++] = register_count;
13140 register_bits = register_bits >> 1;
13141 register_count++;
13143 record_buf[index++] = reg_src1;
13144 thumb_insn_r->reg_rec_count = index;
13146 else if (0 == opcode2)
13148 /* It handles both STMIA. */
13149 register_bits = bits (thumb_insn_r->arm_insn, 0, 7);
13150 /* Get Rn. */
13151 reg_src1 = bits (thumb_insn_r->arm_insn, 8, 10);
13152 regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval);
13153 while (register_bits)
13155 if (register_bits & 0x00000001)
13156 register_count++;
13157 register_bits = register_bits >> 1;
13159 start_address = u_regval;
13160 thumb_insn_r->mem_rec_count = register_count;
13161 while (register_count)
13163 record_buf_mem[(register_count * 2) - 1] = start_address;
13164 record_buf_mem[(register_count * 2) - 2] = 4;
13165 start_address = start_address + 4;
13166 register_count--;
13169 else if (0x1F == opcode1)
13171 /* Handle arm syscall insn. */
13172 if (tdep->arm_syscall_record != NULL)
13174 regcache_raw_read_unsigned (reg_cache, 7, &u_regval);
13175 ret = tdep->arm_syscall_record (reg_cache, u_regval);
13177 else
13179 gdb_printf (gdb_stderr, _("no syscall record support\n"));
13180 return -1;
13184 /* B (1), conditional branch is automatically taken care in process_record,
13185 as PC is saved there. */
13187 REG_ALLOC (thumb_insn_r->arm_regs, thumb_insn_r->reg_rec_count, record_buf);
13188 MEM_ALLOC (thumb_insn_r->arm_mems, thumb_insn_r->mem_rec_count,
13189 record_buf_mem);
13191 return ret;
13194 /* Handling opcode 111 insns. */
13196 static int
13197 thumb_record_branch (insn_decode_record *thumb_insn_r)
13199 uint32_t record_buf[8];
13200 uint32_t bits_h = 0;
13202 bits_h = bits (thumb_insn_r->arm_insn, 11, 12);
13204 if (2 == bits_h || 3 == bits_h)
13206 /* BL */
13207 record_buf[0] = ARM_LR_REGNUM;
13208 thumb_insn_r->reg_rec_count = 1;
13210 else if (1 == bits_h)
13212 /* BLX(1). */
13213 record_buf[0] = ARM_PS_REGNUM;
13214 record_buf[1] = ARM_LR_REGNUM;
13215 thumb_insn_r->reg_rec_count = 2;
13218 /* B(2) is automatically taken care in process_record, as PC is
13219 saved there. */
13221 REG_ALLOC (thumb_insn_r->arm_regs, thumb_insn_r->reg_rec_count, record_buf);
13223 return 0;
13226 /* Handler for thumb2 load/store multiple instructions. */
13228 static int
13229 thumb2_record_ld_st_multiple (insn_decode_record *thumb2_insn_r)
13231 struct regcache *reg_cache = thumb2_insn_r->regcache;
13233 uint32_t reg_rn, op;
13234 uint32_t register_bits = 0, register_count = 0;
13235 uint32_t index = 0, start_address = 0;
13236 uint32_t record_buf[24], record_buf_mem[48];
13238 ULONGEST u_regval = 0;
13240 reg_rn = bits (thumb2_insn_r->arm_insn, 16, 19);
13241 op = bits (thumb2_insn_r->arm_insn, 23, 24);
13243 if (0 == op || 3 == op)
13245 if (bit (thumb2_insn_r->arm_insn, INSN_S_L_BIT_NUM))
13247 /* Handle RFE instruction. */
13248 record_buf[0] = ARM_PS_REGNUM;
13249 thumb2_insn_r->reg_rec_count = 1;
13251 else
13253 /* Handle SRS instruction after reading banked SP. */
13254 return arm_record_unsupported_insn (thumb2_insn_r);
13257 else if (1 == op || 2 == op)
13259 if (bit (thumb2_insn_r->arm_insn, INSN_S_L_BIT_NUM))
13261 /* Handle LDM/LDMIA/LDMFD and LDMDB/LDMEA instructions. */
13262 register_bits = bits (thumb2_insn_r->arm_insn, 0, 15);
13263 while (register_bits)
13265 if (register_bits & 0x00000001)
13266 record_buf[index++] = register_count;
13268 register_count++;
13269 register_bits = register_bits >> 1;
13271 record_buf[index++] = reg_rn;
13272 record_buf[index++] = ARM_PS_REGNUM;
13273 thumb2_insn_r->reg_rec_count = index;
13275 else
13277 /* Handle STM/STMIA/STMEA and STMDB/STMFD. */
13278 register_bits = bits (thumb2_insn_r->arm_insn, 0, 15);
13279 regcache_raw_read_unsigned (reg_cache, reg_rn, &u_regval);
13280 while (register_bits)
13282 if (register_bits & 0x00000001)
13283 register_count++;
13285 register_bits = register_bits >> 1;
13288 if (1 == op)
13290 /* Start address calculation for LDMDB/LDMEA. */
13291 start_address = u_regval;
13293 else if (2 == op)
13295 /* Start address calculation for LDMDB/LDMEA. */
13296 start_address = u_regval - register_count * 4;
13299 thumb2_insn_r->mem_rec_count = register_count;
13300 while (register_count)
13302 record_buf_mem[register_count * 2 - 1] = start_address;
13303 record_buf_mem[register_count * 2 - 2] = 4;
13304 start_address = start_address + 4;
13305 register_count--;
13307 record_buf[0] = reg_rn;
13308 record_buf[1] = ARM_PS_REGNUM;
13309 thumb2_insn_r->reg_rec_count = 2;
13313 MEM_ALLOC (thumb2_insn_r->arm_mems, thumb2_insn_r->mem_rec_count,
13314 record_buf_mem);
13315 REG_ALLOC (thumb2_insn_r->arm_regs, thumb2_insn_r->reg_rec_count,
13316 record_buf);
13317 return ARM_RECORD_SUCCESS;
13320 /* Handler for thumb2 load/store (dual/exclusive) and table branch
13321 instructions. */
13323 static int
13324 thumb2_record_ld_st_dual_ex_tbb (insn_decode_record *thumb2_insn_r)
13326 struct regcache *reg_cache = thumb2_insn_r->regcache;
13328 uint32_t reg_rd, reg_rn, offset_imm;
13329 uint32_t reg_dest1, reg_dest2;
13330 uint32_t address, offset_addr;
13331 uint32_t record_buf[8], record_buf_mem[8];
13332 uint32_t op1, op2, op3;
13334 ULONGEST u_regval[2];
13336 op1 = bits (thumb2_insn_r->arm_insn, 23, 24);
13337 op2 = bits (thumb2_insn_r->arm_insn, 20, 21);
13338 op3 = bits (thumb2_insn_r->arm_insn, 4, 7);
13340 if (bit (thumb2_insn_r->arm_insn, INSN_S_L_BIT_NUM))
13342 if(!(1 == op1 && 1 == op2 && (0 == op3 || 1 == op3)))
13344 reg_dest1 = bits (thumb2_insn_r->arm_insn, 12, 15);
13345 record_buf[0] = reg_dest1;
13346 record_buf[1] = ARM_PS_REGNUM;
13347 thumb2_insn_r->reg_rec_count = 2;
13350 if (3 == op2 || (op1 & 2) || (1 == op1 && 1 == op2 && 7 == op3))
13352 reg_dest2 = bits (thumb2_insn_r->arm_insn, 8, 11);
13353 record_buf[2] = reg_dest2;
13354 thumb2_insn_r->reg_rec_count = 3;
13357 else
13359 reg_rn = bits (thumb2_insn_r->arm_insn, 16, 19);
13360 regcache_raw_read_unsigned (reg_cache, reg_rn, &u_regval[0]);
13362 if (0 == op1 && 0 == op2)
13364 /* Handle STREX. */
13365 offset_imm = bits (thumb2_insn_r->arm_insn, 0, 7);
13366 address = u_regval[0] + (offset_imm * 4);
13367 record_buf_mem[0] = 4;
13368 record_buf_mem[1] = address;
13369 thumb2_insn_r->mem_rec_count = 1;
13370 reg_rd = bits (thumb2_insn_r->arm_insn, 0, 3);
13371 record_buf[0] = reg_rd;
13372 thumb2_insn_r->reg_rec_count = 1;
13374 else if (1 == op1 && 0 == op2)
13376 reg_rd = bits (thumb2_insn_r->arm_insn, 0, 3);
13377 record_buf[0] = reg_rd;
13378 thumb2_insn_r->reg_rec_count = 1;
13379 address = u_regval[0];
13380 record_buf_mem[1] = address;
13382 if (4 == op3)
13384 /* Handle STREXB. */
13385 record_buf_mem[0] = 1;
13386 thumb2_insn_r->mem_rec_count = 1;
13388 else if (5 == op3)
13390 /* Handle STREXH. */
13391 record_buf_mem[0] = 2 ;
13392 thumb2_insn_r->mem_rec_count = 1;
13394 else if (7 == op3)
13396 /* Handle STREXD. */
13397 address = u_regval[0];
13398 record_buf_mem[0] = 4;
13399 record_buf_mem[2] = 4;
13400 record_buf_mem[3] = address + 4;
13401 thumb2_insn_r->mem_rec_count = 2;
13404 else
13406 offset_imm = bits (thumb2_insn_r->arm_insn, 0, 7);
13408 if (bit (thumb2_insn_r->arm_insn, 24))
13410 if (bit (thumb2_insn_r->arm_insn, 23))
13411 offset_addr = u_regval[0] + (offset_imm * 4);
13412 else
13413 offset_addr = u_regval[0] - (offset_imm * 4);
13415 address = offset_addr;
13417 else
13418 address = u_regval[0];
13420 record_buf_mem[0] = 4;
13421 record_buf_mem[1] = address;
13422 record_buf_mem[2] = 4;
13423 record_buf_mem[3] = address + 4;
13424 thumb2_insn_r->mem_rec_count = 2;
13425 record_buf[0] = reg_rn;
13426 thumb2_insn_r->reg_rec_count = 1;
13430 REG_ALLOC (thumb2_insn_r->arm_regs, thumb2_insn_r->reg_rec_count,
13431 record_buf);
13432 MEM_ALLOC (thumb2_insn_r->arm_mems, thumb2_insn_r->mem_rec_count,
13433 record_buf_mem);
13434 return ARM_RECORD_SUCCESS;
13437 /* Handler for thumb2 data processing (shift register and modified immediate)
13438 instructions. */
13440 static int
13441 thumb2_record_data_proc_sreg_mimm (insn_decode_record *thumb2_insn_r)
13443 uint32_t reg_rd, op;
13444 uint32_t record_buf[8];
13446 op = bits (thumb2_insn_r->arm_insn, 21, 24);
13447 reg_rd = bits (thumb2_insn_r->arm_insn, 8, 11);
13449 if ((0 == op || 4 == op || 8 == op || 13 == op) && 15 == reg_rd)
13451 record_buf[0] = ARM_PS_REGNUM;
13452 thumb2_insn_r->reg_rec_count = 1;
13454 else
13456 record_buf[0] = reg_rd;
13457 record_buf[1] = ARM_PS_REGNUM;
13458 thumb2_insn_r->reg_rec_count = 2;
13461 REG_ALLOC (thumb2_insn_r->arm_regs, thumb2_insn_r->reg_rec_count,
13462 record_buf);
13463 return ARM_RECORD_SUCCESS;
13466 /* Generic handler for thumb2 instructions which effect destination and PS
13467 registers. */
13469 static int
13470 thumb2_record_ps_dest_generic (insn_decode_record *thumb2_insn_r)
13472 uint32_t reg_rd;
13473 uint32_t record_buf[8];
13475 reg_rd = bits (thumb2_insn_r->arm_insn, 8, 11);
13477 record_buf[0] = reg_rd;
13478 record_buf[1] = ARM_PS_REGNUM;
13479 thumb2_insn_r->reg_rec_count = 2;
13481 REG_ALLOC (thumb2_insn_r->arm_regs, thumb2_insn_r->reg_rec_count,
13482 record_buf);
13483 return ARM_RECORD_SUCCESS;
13486 /* Handler for thumb2 branch and miscellaneous control instructions. */
13488 static int
13489 thumb2_record_branch_misc_cntrl (insn_decode_record *thumb2_insn_r)
13491 uint32_t op, op1, op2;
13492 uint32_t record_buf[8];
13494 op = bits (thumb2_insn_r->arm_insn, 20, 26);
13495 op1 = bits (thumb2_insn_r->arm_insn, 12, 14);
13496 op2 = bits (thumb2_insn_r->arm_insn, 8, 11);
13498 /* Handle MSR insn. */
13499 if (!(op1 & 0x2) && 0x38 == op)
13501 if (!(op2 & 0x3))
13503 /* CPSR is going to be changed. */
13504 record_buf[0] = ARM_PS_REGNUM;
13505 thumb2_insn_r->reg_rec_count = 1;
13507 else
13509 arm_record_unsupported_insn(thumb2_insn_r);
13510 return -1;
13513 else if (4 == (op1 & 0x5) || 5 == (op1 & 0x5))
13515 /* BLX. */
13516 record_buf[0] = ARM_PS_REGNUM;
13517 record_buf[1] = ARM_LR_REGNUM;
13518 thumb2_insn_r->reg_rec_count = 2;
13521 REG_ALLOC (thumb2_insn_r->arm_regs, thumb2_insn_r->reg_rec_count,
13522 record_buf);
13523 return ARM_RECORD_SUCCESS;
13526 /* Handler for thumb2 store single data item instructions. */
13528 static int
13529 thumb2_record_str_single_data (insn_decode_record *thumb2_insn_r)
13531 struct regcache *reg_cache = thumb2_insn_r->regcache;
13533 uint32_t reg_rn, reg_rm, offset_imm, shift_imm;
13534 uint32_t address, offset_addr;
13535 uint32_t record_buf[8], record_buf_mem[8];
13536 uint32_t op1, op2;
13538 ULONGEST u_regval[2];
13540 op1 = bits (thumb2_insn_r->arm_insn, 21, 23);
13541 op2 = bits (thumb2_insn_r->arm_insn, 6, 11);
13542 reg_rn = bits (thumb2_insn_r->arm_insn, 16, 19);
13543 regcache_raw_read_unsigned (reg_cache, reg_rn, &u_regval[0]);
13545 if (bit (thumb2_insn_r->arm_insn, 23))
13547 /* T2 encoding. */
13548 offset_imm = bits (thumb2_insn_r->arm_insn, 0, 11);
13549 offset_addr = u_regval[0] + offset_imm;
13550 address = offset_addr;
13552 else
13554 /* T3 encoding. */
13555 if ((0 == op1 || 1 == op1 || 2 == op1) && !(op2 & 0x20))
13557 /* Handle STRB (register). */
13558 reg_rm = bits (thumb2_insn_r->arm_insn, 0, 3);
13559 regcache_raw_read_unsigned (reg_cache, reg_rm, &u_regval[1]);
13560 shift_imm = bits (thumb2_insn_r->arm_insn, 4, 5);
13561 offset_addr = u_regval[1] << shift_imm;
13562 address = u_regval[0] + offset_addr;
13564 else
13566 offset_imm = bits (thumb2_insn_r->arm_insn, 0, 7);
13567 if (bit (thumb2_insn_r->arm_insn, 10))
13569 if (bit (thumb2_insn_r->arm_insn, 9))
13570 offset_addr = u_regval[0] + offset_imm;
13571 else
13572 offset_addr = u_regval[0] - offset_imm;
13574 address = offset_addr;
13576 else
13577 address = u_regval[0];
13581 switch (op1)
13583 /* Store byte instructions. */
13584 case 4:
13585 case 0:
13586 record_buf_mem[0] = 1;
13587 break;
13588 /* Store half word instructions. */
13589 case 1:
13590 case 5:
13591 record_buf_mem[0] = 2;
13592 break;
13593 /* Store word instructions. */
13594 case 2:
13595 case 6:
13596 record_buf_mem[0] = 4;
13597 break;
13599 default:
13600 gdb_assert_not_reached ("no decoding pattern found");
13601 break;
13604 record_buf_mem[1] = address;
13605 thumb2_insn_r->mem_rec_count = 1;
13606 record_buf[0] = reg_rn;
13607 thumb2_insn_r->reg_rec_count = 1;
13609 REG_ALLOC (thumb2_insn_r->arm_regs, thumb2_insn_r->reg_rec_count,
13610 record_buf);
13611 MEM_ALLOC (thumb2_insn_r->arm_mems, thumb2_insn_r->mem_rec_count,
13612 record_buf_mem);
13613 return ARM_RECORD_SUCCESS;
13616 /* Handler for thumb2 load memory hints instructions. */
13618 static int
13619 thumb2_record_ld_mem_hints (insn_decode_record *thumb2_insn_r)
13621 uint32_t record_buf[8];
13622 uint32_t reg_rt, reg_rn;
13624 reg_rt = bits (thumb2_insn_r->arm_insn, 12, 15);
13625 reg_rn = bits (thumb2_insn_r->arm_insn, 16, 19);
13627 if (ARM_PC_REGNUM != reg_rt)
13629 record_buf[0] = reg_rt;
13630 record_buf[1] = reg_rn;
13631 record_buf[2] = ARM_PS_REGNUM;
13632 thumb2_insn_r->reg_rec_count = 3;
13634 REG_ALLOC (thumb2_insn_r->arm_regs, thumb2_insn_r->reg_rec_count,
13635 record_buf);
13636 return ARM_RECORD_SUCCESS;
13639 return ARM_RECORD_FAILURE;
13642 /* Handler for thumb2 load word instructions. */
13644 static int
13645 thumb2_record_ld_word (insn_decode_record *thumb2_insn_r)
13647 uint32_t record_buf[8];
13649 record_buf[0] = bits (thumb2_insn_r->arm_insn, 12, 15);
13650 record_buf[1] = ARM_PS_REGNUM;
13651 thumb2_insn_r->reg_rec_count = 2;
13653 REG_ALLOC (thumb2_insn_r->arm_regs, thumb2_insn_r->reg_rec_count,
13654 record_buf);
13655 return ARM_RECORD_SUCCESS;
13658 /* Handler for thumb2 long multiply, long multiply accumulate, and
13659 divide instructions. */
13661 static int
13662 thumb2_record_lmul_lmla_div (insn_decode_record *thumb2_insn_r)
13664 uint32_t opcode1 = 0, opcode2 = 0;
13665 uint32_t record_buf[8];
13667 opcode1 = bits (thumb2_insn_r->arm_insn, 20, 22);
13668 opcode2 = bits (thumb2_insn_r->arm_insn, 4, 7);
13670 if (0 == opcode1 || 2 == opcode1 || (opcode1 >= 4 && opcode1 <= 6))
13672 /* Handle SMULL, UMULL, SMULAL. */
13673 /* Handle SMLAL(S), SMULL(S), UMLAL(S), UMULL(S). */
13674 record_buf[0] = bits (thumb2_insn_r->arm_insn, 16, 19);
13675 record_buf[1] = bits (thumb2_insn_r->arm_insn, 12, 15);
13676 record_buf[2] = ARM_PS_REGNUM;
13677 thumb2_insn_r->reg_rec_count = 3;
13679 else if (1 == opcode1 || 3 == opcode2)
13681 /* Handle SDIV and UDIV. */
13682 record_buf[0] = bits (thumb2_insn_r->arm_insn, 16, 19);
13683 record_buf[1] = bits (thumb2_insn_r->arm_insn, 12, 15);
13684 record_buf[2] = ARM_PS_REGNUM;
13685 thumb2_insn_r->reg_rec_count = 3;
13687 else
13688 return ARM_RECORD_FAILURE;
13690 REG_ALLOC (thumb2_insn_r->arm_regs, thumb2_insn_r->reg_rec_count,
13691 record_buf);
13692 return ARM_RECORD_SUCCESS;
13695 /* Record handler for thumb32 coprocessor instructions. */
13697 static int
13698 thumb2_record_coproc_insn (insn_decode_record *thumb2_insn_r)
13700 if (bit (thumb2_insn_r->arm_insn, 25))
13701 return arm_record_coproc_data_proc (thumb2_insn_r);
13702 else
13703 return arm_record_asimd_vfp_coproc (thumb2_insn_r);
13706 /* Record handler for advance SIMD structure load/store instructions. */
13708 static int
13709 thumb2_record_asimd_struct_ld_st (insn_decode_record *thumb2_insn_r)
13711 struct regcache *reg_cache = thumb2_insn_r->regcache;
13712 uint32_t l_bit, a_bit, b_bits;
13713 uint32_t record_buf[128], record_buf_mem[128];
13714 uint32_t reg_rn, reg_vd, address, f_elem;
13715 uint32_t index_r = 0, index_e = 0, bf_regs = 0, index_m = 0, loop_t = 0;
13716 uint8_t f_ebytes;
13718 l_bit = bit (thumb2_insn_r->arm_insn, 21);
13719 a_bit = bit (thumb2_insn_r->arm_insn, 23);
13720 b_bits = bits (thumb2_insn_r->arm_insn, 8, 11);
13721 reg_rn = bits (thumb2_insn_r->arm_insn, 16, 19);
13722 reg_vd = bits (thumb2_insn_r->arm_insn, 12, 15);
13723 reg_vd = (bit (thumb2_insn_r->arm_insn, 22) << 4) | reg_vd;
13724 f_ebytes = (1 << bits (thumb2_insn_r->arm_insn, 6, 7));
13725 f_elem = 8 / f_ebytes;
13727 if (!l_bit)
13729 ULONGEST u_regval = 0;
13730 regcache_raw_read_unsigned (reg_cache, reg_rn, &u_regval);
13731 address = u_regval;
13733 if (!a_bit)
13735 /* Handle VST1. */
13736 if (b_bits == 0x02 || b_bits == 0x0a || (b_bits & 0x0e) == 0x06)
13738 if (b_bits == 0x07)
13739 bf_regs = 1;
13740 else if (b_bits == 0x0a)
13741 bf_regs = 2;
13742 else if (b_bits == 0x06)
13743 bf_regs = 3;
13744 else if (b_bits == 0x02)
13745 bf_regs = 4;
13746 else
13747 bf_regs = 0;
13749 for (index_r = 0; index_r < bf_regs; index_r++)
13751 for (index_e = 0; index_e < f_elem; index_e++)
13753 record_buf_mem[index_m++] = f_ebytes;
13754 record_buf_mem[index_m++] = address;
13755 address = address + f_ebytes;
13756 thumb2_insn_r->mem_rec_count += 1;
13760 /* Handle VST2. */
13761 else if (b_bits == 0x03 || (b_bits & 0x0e) == 0x08)
13763 if (b_bits == 0x09 || b_bits == 0x08)
13764 bf_regs = 1;
13765 else if (b_bits == 0x03)
13766 bf_regs = 2;
13767 else
13768 bf_regs = 0;
13770 for (index_r = 0; index_r < bf_regs; index_r++)
13771 for (index_e = 0; index_e < f_elem; index_e++)
13773 for (loop_t = 0; loop_t < 2; loop_t++)
13775 record_buf_mem[index_m++] = f_ebytes;
13776 record_buf_mem[index_m++] = address + (loop_t * f_ebytes);
13777 thumb2_insn_r->mem_rec_count += 1;
13779 address = address + (2 * f_ebytes);
13782 /* Handle VST3. */
13783 else if ((b_bits & 0x0e) == 0x04)
13785 for (index_e = 0; index_e < f_elem; index_e++)
13787 for (loop_t = 0; loop_t < 3; loop_t++)
13789 record_buf_mem[index_m++] = f_ebytes;
13790 record_buf_mem[index_m++] = address + (loop_t * f_ebytes);
13791 thumb2_insn_r->mem_rec_count += 1;
13793 address = address + (3 * f_ebytes);
13796 /* Handle VST4. */
13797 else if (!(b_bits & 0x0e))
13799 for (index_e = 0; index_e < f_elem; index_e++)
13801 for (loop_t = 0; loop_t < 4; loop_t++)
13803 record_buf_mem[index_m++] = f_ebytes;
13804 record_buf_mem[index_m++] = address + (loop_t * f_ebytes);
13805 thumb2_insn_r->mem_rec_count += 1;
13807 address = address + (4 * f_ebytes);
13811 else
13813 uint8_t bft_size = bits (thumb2_insn_r->arm_insn, 10, 11);
13815 if (bft_size == 0x00)
13816 f_ebytes = 1;
13817 else if (bft_size == 0x01)
13818 f_ebytes = 2;
13819 else if (bft_size == 0x02)
13820 f_ebytes = 4;
13821 else
13822 f_ebytes = 0;
13824 /* Handle VST1. */
13825 if (!(b_bits & 0x0b) || b_bits == 0x08)
13826 thumb2_insn_r->mem_rec_count = 1;
13827 /* Handle VST2. */
13828 else if ((b_bits & 0x0b) == 0x01 || b_bits == 0x09)
13829 thumb2_insn_r->mem_rec_count = 2;
13830 /* Handle VST3. */
13831 else if ((b_bits & 0x0b) == 0x02 || b_bits == 0x0a)
13832 thumb2_insn_r->mem_rec_count = 3;
13833 /* Handle VST4. */
13834 else if ((b_bits & 0x0b) == 0x03 || b_bits == 0x0b)
13835 thumb2_insn_r->mem_rec_count = 4;
13837 for (index_m = 0; index_m < thumb2_insn_r->mem_rec_count; index_m++)
13839 record_buf_mem[index_m] = f_ebytes;
13840 record_buf_mem[index_m] = address + (index_m * f_ebytes);
13844 else
13846 if (!a_bit)
13848 /* Handle VLD1. */
13849 if (b_bits == 0x02 || b_bits == 0x0a || (b_bits & 0x0e) == 0x06)
13850 thumb2_insn_r->reg_rec_count = 1;
13851 /* Handle VLD2. */
13852 else if (b_bits == 0x03 || (b_bits & 0x0e) == 0x08)
13853 thumb2_insn_r->reg_rec_count = 2;
13854 /* Handle VLD3. */
13855 else if ((b_bits & 0x0e) == 0x04)
13856 thumb2_insn_r->reg_rec_count = 3;
13857 /* Handle VLD4. */
13858 else if (!(b_bits & 0x0e))
13859 thumb2_insn_r->reg_rec_count = 4;
13861 else
13863 /* Handle VLD1. */
13864 if (!(b_bits & 0x0b) || b_bits == 0x08 || b_bits == 0x0c)
13865 thumb2_insn_r->reg_rec_count = 1;
13866 /* Handle VLD2. */
13867 else if ((b_bits & 0x0b) == 0x01 || b_bits == 0x09 || b_bits == 0x0d)
13868 thumb2_insn_r->reg_rec_count = 2;
13869 /* Handle VLD3. */
13870 else if ((b_bits & 0x0b) == 0x02 || b_bits == 0x0a || b_bits == 0x0e)
13871 thumb2_insn_r->reg_rec_count = 3;
13872 /* Handle VLD4. */
13873 else if ((b_bits & 0x0b) == 0x03 || b_bits == 0x0b || b_bits == 0x0f)
13874 thumb2_insn_r->reg_rec_count = 4;
13876 for (index_r = 0; index_r < thumb2_insn_r->reg_rec_count; index_r++)
13877 record_buf[index_r] = reg_vd + ARM_D0_REGNUM + index_r;
13881 if (bits (thumb2_insn_r->arm_insn, 0, 3) != 15)
13883 record_buf[index_r] = reg_rn;
13884 thumb2_insn_r->reg_rec_count += 1;
13887 REG_ALLOC (thumb2_insn_r->arm_regs, thumb2_insn_r->reg_rec_count,
13888 record_buf);
13889 MEM_ALLOC (thumb2_insn_r->arm_mems, thumb2_insn_r->mem_rec_count,
13890 record_buf_mem);
13891 return 0;
13894 /* Decodes thumb2 instruction type and invokes its record handler. */
13896 static unsigned int
13897 thumb2_record_decode_insn_handler (insn_decode_record *thumb2_insn_r)
13899 uint32_t op, op1, op2;
13901 op = bit (thumb2_insn_r->arm_insn, 15);
13902 op1 = bits (thumb2_insn_r->arm_insn, 27, 28);
13903 op2 = bits (thumb2_insn_r->arm_insn, 20, 26);
13905 if (op1 == 0x01)
13907 if (!(op2 & 0x64 ))
13909 /* Load/store multiple instruction. */
13910 return thumb2_record_ld_st_multiple (thumb2_insn_r);
13912 else if ((op2 & 0x64) == 0x4)
13914 /* Load/store (dual/exclusive) and table branch instruction. */
13915 return thumb2_record_ld_st_dual_ex_tbb (thumb2_insn_r);
13917 else if ((op2 & 0x60) == 0x20)
13919 /* Data-processing (shifted register). */
13920 return thumb2_record_data_proc_sreg_mimm (thumb2_insn_r);
13922 else if (op2 & 0x40)
13924 /* Co-processor instructions. */
13925 return thumb2_record_coproc_insn (thumb2_insn_r);
13928 else if (op1 == 0x02)
13930 if (op)
13932 /* Branches and miscellaneous control instructions. */
13933 return thumb2_record_branch_misc_cntrl (thumb2_insn_r);
13935 else if (op2 & 0x20)
13937 /* Data-processing (plain binary immediate) instruction. */
13938 return thumb2_record_ps_dest_generic (thumb2_insn_r);
13940 else
13942 /* Data-processing (modified immediate). */
13943 return thumb2_record_data_proc_sreg_mimm (thumb2_insn_r);
13946 else if (op1 == 0x03)
13948 if (!(op2 & 0x71 ))
13950 /* Store single data item. */
13951 return thumb2_record_str_single_data (thumb2_insn_r);
13953 else if (!((op2 & 0x71) ^ 0x10))
13955 /* Advanced SIMD or structure load/store instructions. */
13956 return thumb2_record_asimd_struct_ld_st (thumb2_insn_r);
13958 else if (!((op2 & 0x67) ^ 0x01))
13960 /* Load byte, memory hints instruction. */
13961 return thumb2_record_ld_mem_hints (thumb2_insn_r);
13963 else if (!((op2 & 0x67) ^ 0x03))
13965 /* Load halfword, memory hints instruction. */
13966 return thumb2_record_ld_mem_hints (thumb2_insn_r);
13968 else if (!((op2 & 0x67) ^ 0x05))
13970 /* Load word instruction. */
13971 return thumb2_record_ld_word (thumb2_insn_r);
13973 else if (!((op2 & 0x70) ^ 0x20))
13975 /* Data-processing (register) instruction. */
13976 return thumb2_record_ps_dest_generic (thumb2_insn_r);
13978 else if (!((op2 & 0x78) ^ 0x30))
13980 /* Multiply, multiply accumulate, abs diff instruction. */
13981 return thumb2_record_ps_dest_generic (thumb2_insn_r);
13983 else if (!((op2 & 0x78) ^ 0x38))
13985 /* Long multiply, long multiply accumulate, and divide. */
13986 return thumb2_record_lmul_lmla_div (thumb2_insn_r);
13988 else if (op2 & 0x40)
13990 /* Co-processor instructions. */
13991 return thumb2_record_coproc_insn (thumb2_insn_r);
13995 return -1;
13998 namespace {
13999 /* Abstract memory reader. */
14001 class abstract_memory_reader
14003 public:
14004 /* Read LEN bytes of target memory at address MEMADDR, placing the
14005 results in GDB's memory at BUF. Return true on success. */
14007 virtual bool read (CORE_ADDR memaddr, gdb_byte *buf, const size_t len) = 0;
14010 /* Instruction reader from real target. */
14012 class instruction_reader : public abstract_memory_reader
14014 public:
14015 bool read (CORE_ADDR memaddr, gdb_byte *buf, const size_t len) override
14017 if (target_read_memory (memaddr, buf, len))
14018 return false;
14019 else
14020 return true;
14024 } // namespace
14026 /* Extracts arm/thumb/thumb2 insn depending on the size, and returns 0 on success
14027 and positive val on failure. */
14029 static int
14030 extract_arm_insn (abstract_memory_reader& reader,
14031 insn_decode_record *insn_record, uint32_t insn_size)
14033 gdb_byte buf[insn_size];
14035 memset (&buf[0], 0, insn_size);
14037 if (!reader.read (insn_record->this_addr, buf, insn_size))
14038 return 1;
14039 insn_record->arm_insn = (uint32_t) extract_unsigned_integer (&buf[0],
14040 insn_size,
14041 gdbarch_byte_order_for_code (insn_record->gdbarch));
14042 return 0;
14045 typedef int (*sti_arm_hdl_fp_t) (insn_decode_record*);
14047 /* Decode arm/thumb insn depending on condition cods and opcodes; and
14048 dispatch it. */
14050 static int
14051 decode_insn (abstract_memory_reader &reader, insn_decode_record *arm_record,
14052 record_type_t record_type, uint32_t insn_size)
14055 /* (Starting from numerical 0); bits 25, 26, 27 decodes type of arm
14056 instruction. */
14057 static const sti_arm_hdl_fp_t arm_handle_insn[8] =
14059 arm_record_data_proc_misc_ld_str, /* 000. */
14060 arm_record_data_proc_imm, /* 001. */
14061 arm_record_ld_st_imm_offset, /* 010. */
14062 arm_record_ld_st_reg_offset, /* 011. */
14063 arm_record_ld_st_multiple, /* 100. */
14064 arm_record_b_bl, /* 101. */
14065 arm_record_asimd_vfp_coproc, /* 110. */
14066 arm_record_coproc_data_proc /* 111. */
14069 /* (Starting from numerical 0); bits 13,14,15 decodes type of thumb
14070 instruction. */
14071 static const sti_arm_hdl_fp_t thumb_handle_insn[8] =
14073 thumb_record_shift_add_sub, /* 000. */
14074 thumb_record_add_sub_cmp_mov, /* 001. */
14075 thumb_record_ld_st_reg_offset, /* 010. */
14076 thumb_record_ld_st_imm_offset, /* 011. */
14077 thumb_record_ld_st_stack, /* 100. */
14078 thumb_record_misc, /* 101. */
14079 thumb_record_ldm_stm_swi, /* 110. */
14080 thumb_record_branch /* 111. */
14083 uint32_t ret = 0; /* return value: negative:failure 0:success. */
14084 uint32_t insn_id = 0;
14086 if (extract_arm_insn (reader, arm_record, insn_size))
14088 if (record_debug)
14090 gdb_printf (gdb_stdlog,
14091 _("Process record: error reading memory at "
14092 "addr %s len = %d.\n"),
14093 paddress (arm_record->gdbarch,
14094 arm_record->this_addr), insn_size);
14096 return -1;
14098 else if (ARM_RECORD == record_type)
14100 arm_record->cond = bits (arm_record->arm_insn, 28, 31);
14101 insn_id = bits (arm_record->arm_insn, 25, 27);
14103 if (arm_record->cond == 0xf)
14104 ret = arm_record_extension_space (arm_record);
14105 else
14107 /* If this insn has fallen into extension space
14108 then we need not decode it anymore. */
14109 ret = arm_handle_insn[insn_id] (arm_record);
14111 if (ret != ARM_RECORD_SUCCESS)
14113 arm_record_unsupported_insn (arm_record);
14114 ret = -1;
14117 else if (THUMB_RECORD == record_type)
14119 /* As thumb does not have condition codes, we set negative. */
14120 arm_record->cond = -1;
14121 insn_id = bits (arm_record->arm_insn, 13, 15);
14122 ret = thumb_handle_insn[insn_id] (arm_record);
14123 if (ret != ARM_RECORD_SUCCESS)
14125 arm_record_unsupported_insn (arm_record);
14126 ret = -1;
14129 else if (THUMB2_RECORD == record_type)
14131 /* As thumb does not have condition codes, we set negative. */
14132 arm_record->cond = -1;
14134 /* Swap first half of 32bit thumb instruction with second half. */
14135 arm_record->arm_insn
14136 = (arm_record->arm_insn >> 16) | (arm_record->arm_insn << 16);
14138 ret = thumb2_record_decode_insn_handler (arm_record);
14140 if (ret != ARM_RECORD_SUCCESS)
14142 arm_record_unsupported_insn (arm_record);
14143 ret = -1;
14146 else
14148 /* Throw assertion. */
14149 gdb_assert_not_reached ("not a valid instruction, could not decode");
14152 return ret;
14155 #if GDB_SELF_TEST
14156 namespace selftests {
14158 /* Provide both 16-bit and 32-bit thumb instructions. */
14160 class instruction_reader_thumb : public abstract_memory_reader
14162 public:
14163 template<size_t SIZE>
14164 instruction_reader_thumb (enum bfd_endian endian,
14165 const uint16_t (&insns)[SIZE])
14166 : m_endian (endian), m_insns (insns), m_insns_size (SIZE)
14169 bool read (CORE_ADDR memaddr, gdb_byte *buf, const size_t len) override
14171 SELF_CHECK (len == 4 || len == 2);
14172 SELF_CHECK (memaddr % 2 == 0);
14173 SELF_CHECK ((memaddr / 2) < m_insns_size);
14175 store_unsigned_integer (buf, 2, m_endian, m_insns[memaddr / 2]);
14176 if (len == 4)
14178 store_unsigned_integer (&buf[2], 2, m_endian,
14179 m_insns[memaddr / 2 + 1]);
14181 return true;
14184 private:
14185 enum bfd_endian m_endian;
14186 const uint16_t *m_insns;
14187 size_t m_insns_size;
14190 static void
14191 arm_record_test (void)
14193 struct gdbarch_info info;
14194 info.bfd_arch_info = bfd_scan_arch ("arm");
14196 struct gdbarch *gdbarch = gdbarch_find_by_info (info);
14198 SELF_CHECK (gdbarch != NULL);
14200 /* 16-bit Thumb instructions. */
14202 insn_decode_record arm_record;
14204 memset (&arm_record, 0, sizeof (insn_decode_record));
14205 arm_record.gdbarch = gdbarch;
14207 static const uint16_t insns[] = {
14208 /* db b2 uxtb r3, r3 */
14209 0xb2db,
14210 /* cd 58 ldr r5, [r1, r3] */
14211 0x58cd,
14214 enum bfd_endian endian = gdbarch_byte_order_for_code (arm_record.gdbarch);
14215 instruction_reader_thumb reader (endian, insns);
14216 int ret = decode_insn (reader, &arm_record, THUMB_RECORD,
14217 THUMB_INSN_SIZE_BYTES);
14219 SELF_CHECK (ret == 0);
14220 SELF_CHECK (arm_record.mem_rec_count == 0);
14221 SELF_CHECK (arm_record.reg_rec_count == 1);
14222 SELF_CHECK (arm_record.arm_regs[0] == 3);
14224 arm_record.this_addr += 2;
14225 ret = decode_insn (reader, &arm_record, THUMB_RECORD,
14226 THUMB_INSN_SIZE_BYTES);
14228 SELF_CHECK (ret == 0);
14229 SELF_CHECK (arm_record.mem_rec_count == 0);
14230 SELF_CHECK (arm_record.reg_rec_count == 1);
14231 SELF_CHECK (arm_record.arm_regs[0] == 5);
14234 /* 32-bit Thumb-2 instructions. */
14236 insn_decode_record arm_record;
14238 memset (&arm_record, 0, sizeof (insn_decode_record));
14239 arm_record.gdbarch = gdbarch;
14241 static const uint16_t insns[] = {
14242 /* 1d ee 70 7f mrc 15, 0, r7, cr13, cr0, {3} */
14243 0xee1d, 0x7f70,
14246 enum bfd_endian endian = gdbarch_byte_order_for_code (arm_record.gdbarch);
14247 instruction_reader_thumb reader (endian, insns);
14248 int ret = decode_insn (reader, &arm_record, THUMB2_RECORD,
14249 THUMB2_INSN_SIZE_BYTES);
14251 SELF_CHECK (ret == 0);
14252 SELF_CHECK (arm_record.mem_rec_count == 0);
14253 SELF_CHECK (arm_record.reg_rec_count == 1);
14254 SELF_CHECK (arm_record.arm_regs[0] == 7);
14258 /* Instruction reader from manually cooked instruction sequences. */
14260 class test_arm_instruction_reader : public arm_instruction_reader
14262 public:
14263 explicit test_arm_instruction_reader (gdb::array_view<const uint32_t> insns)
14264 : m_insns (insns)
14267 uint32_t read (CORE_ADDR memaddr, enum bfd_endian byte_order) const override
14269 SELF_CHECK (memaddr % 4 == 0);
14270 SELF_CHECK (memaddr / 4 < m_insns.size ());
14272 return m_insns[memaddr / 4];
14275 private:
14276 const gdb::array_view<const uint32_t> m_insns;
14279 static void
14280 arm_analyze_prologue_test ()
14282 for (bfd_endian endianness : {BFD_ENDIAN_LITTLE, BFD_ENDIAN_BIG})
14284 struct gdbarch_info info;
14285 info.byte_order = endianness;
14286 info.byte_order_for_code = endianness;
14287 info.bfd_arch_info = bfd_scan_arch ("arm");
14289 struct gdbarch *gdbarch = gdbarch_find_by_info (info);
14291 SELF_CHECK (gdbarch != NULL);
14293 /* The "sub" instruction contains an immediate value rotate count of 0,
14294 which resulted in a 32-bit shift of a 32-bit value, caught by
14295 UBSan. */
14296 const uint32_t insns[] = {
14297 0xe92d4ff0, /* push {r4, r5, r6, r7, r8, r9, sl, fp, lr} */
14298 0xe1a05000, /* mov r5, r0 */
14299 0xe5903020, /* ldr r3, [r0, #32] */
14300 0xe24dd044, /* sub sp, sp, #68 ; 0x44 */
14303 test_arm_instruction_reader mem_reader (insns);
14304 arm_prologue_cache cache;
14305 arm_cache_init (&cache, gdbarch);
14307 arm_analyze_prologue (gdbarch, 0, sizeof (insns) - 1, &cache, mem_reader);
14311 } // namespace selftests
14312 #endif /* GDB_SELF_TEST */
14314 /* Cleans up local record registers and memory allocations. */
14316 static void
14317 deallocate_reg_mem (insn_decode_record *record)
14319 xfree (record->arm_regs);
14320 xfree (record->arm_mems);
14324 /* Parse the current instruction and record the values of the registers and
14325 memory that will be changed in current instruction to record_arch_list".
14326 Return -1 if something is wrong. */
14329 arm_process_record (struct gdbarch *gdbarch, struct regcache *regcache,
14330 CORE_ADDR insn_addr)
14333 uint32_t no_of_rec = 0;
14334 uint32_t ret = 0; /* return value: -1:record failure ; 0:success */
14335 ULONGEST t_bit = 0, insn_id = 0;
14337 ULONGEST u_regval = 0;
14339 insn_decode_record arm_record;
14341 memset (&arm_record, 0, sizeof (insn_decode_record));
14342 arm_record.regcache = regcache;
14343 arm_record.this_addr = insn_addr;
14344 arm_record.gdbarch = gdbarch;
14347 if (record_debug > 1)
14349 gdb_printf (gdb_stdlog, "Process record: arm_process_record "
14350 "addr = %s\n",
14351 paddress (gdbarch, arm_record.this_addr));
14354 instruction_reader reader;
14355 if (extract_arm_insn (reader, &arm_record, 2))
14357 if (record_debug)
14359 gdb_printf (gdb_stdlog,
14360 _("Process record: error reading memory at "
14361 "addr %s len = %d.\n"),
14362 paddress (arm_record.gdbarch,
14363 arm_record.this_addr), 2);
14365 return -1;
14368 /* Check the insn, whether it is thumb or arm one. */
14370 t_bit = arm_psr_thumb_bit (arm_record.gdbarch);
14371 regcache_raw_read_unsigned (arm_record.regcache, ARM_PS_REGNUM, &u_regval);
14374 if (!(u_regval & t_bit))
14376 /* We are decoding arm insn. */
14377 ret = decode_insn (reader, &arm_record, ARM_RECORD, ARM_INSN_SIZE_BYTES);
14379 else
14381 insn_id = bits (arm_record.arm_insn, 11, 15);
14382 /* is it thumb2 insn? */
14383 if ((0x1D == insn_id) || (0x1E == insn_id) || (0x1F == insn_id))
14385 ret = decode_insn (reader, &arm_record, THUMB2_RECORD,
14386 THUMB2_INSN_SIZE_BYTES);
14388 else
14390 /* We are decoding thumb insn. */
14391 ret = decode_insn (reader, &arm_record, THUMB_RECORD,
14392 THUMB_INSN_SIZE_BYTES);
14396 if (0 == ret)
14398 /* Record registers. */
14399 record_full_arch_list_add_reg (arm_record.regcache, ARM_PC_REGNUM);
14400 if (arm_record.arm_regs)
14402 for (no_of_rec = 0; no_of_rec < arm_record.reg_rec_count; no_of_rec++)
14404 if (record_full_arch_list_add_reg
14405 (arm_record.regcache , arm_record.arm_regs[no_of_rec]))
14406 ret = -1;
14409 /* Record memories. */
14410 if (arm_record.arm_mems)
14412 for (no_of_rec = 0; no_of_rec < arm_record.mem_rec_count; no_of_rec++)
14414 if (record_full_arch_list_add_mem
14415 ((CORE_ADDR)arm_record.arm_mems[no_of_rec].addr,
14416 arm_record.arm_mems[no_of_rec].len))
14417 ret = -1;
14421 if (record_full_arch_list_add_end ())
14422 ret = -1;
14426 deallocate_reg_mem (&arm_record);
14428 return ret;
14431 /* See arm-tdep.h. */
14433 const target_desc *
14434 arm_read_description (arm_fp_type fp_type, bool tls)
14436 struct target_desc *tdesc = tdesc_arm_list[fp_type][tls];
14438 if (tdesc == nullptr)
14440 tdesc = arm_create_target_description (fp_type, tls);
14441 tdesc_arm_list[fp_type][tls] = tdesc;
14444 return tdesc;
14447 /* See arm-tdep.h. */
14449 const target_desc *
14450 arm_read_mprofile_description (arm_m_profile_type m_type)
14452 struct target_desc *tdesc = tdesc_arm_mprofile_list[m_type];
14454 if (tdesc == nullptr)
14456 tdesc = arm_create_mprofile_target_description (m_type);
14457 tdesc_arm_mprofile_list[m_type] = tdesc;
14460 return tdesc;