RISC-V: Support SiFive extensions: xsfvqmaccdod, xsfvqmaccqoq and xsfvfnrclipxfqf
[binutils-gdb.git] / gdb / arm-tdep.c
blob34507615975ad0ff61c240ed68ec31a8ec827521
1 /* Common target dependent code for GDB on ARM systems.
3 Copyright (C) 1988-2024 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/>. */
21 #include <ctype.h>
23 #include "extract-store-integer.h"
24 #include "frame.h"
25 #include "language.h"
26 #include "inferior.h"
27 #include "infrun.h"
28 #include "cli/cli-cmds.h"
29 #include "gdbcore.h"
30 #include "dis-asm.h"
31 #include "disasm.h"
32 #include "regcache.h"
33 #include "reggroups.h"
34 #include "target-float.h"
35 #include "value.h"
36 #include "arch-utils.h"
37 #include "osabi.h"
38 #include "frame-unwind.h"
39 #include "frame-base.h"
40 #include "trad-frame.h"
41 #include "objfiles.h"
42 #include "dwarf2.h"
43 #include "dwarf2/frame.h"
44 #include "gdbtypes.h"
45 #include "prologue-value.h"
46 #include "remote.h"
47 #include "target-descriptions.h"
48 #include "user-regs.h"
49 #include "observable.h"
50 #include "count-one-bits.h"
52 #include "arch/arm.h"
53 #include "arch/arm-get-next-pcs.h"
54 #include "arm-tdep.h"
55 #include "sim/sim-arm.h"
57 #include "elf-bfd.h"
58 #include "coff/internal.h"
59 #include "elf/arm.h"
61 #include "record.h"
62 #include "record-full.h"
63 #include <algorithm>
65 #include "producer.h"
67 #if GDB_SELF_TEST
68 #include "gdbsupport/selftest.h"
69 #endif
71 static bool arm_debug;
73 /* Print an "arm" debug statement. */
75 #define arm_debug_printf(fmt, ...) \
76 debug_prefixed_printf_cond (arm_debug, "arm", fmt, ##__VA_ARGS__)
78 /* Macros for setting and testing a bit in a minimal symbol that marks
79 it as Thumb function. The MSB of the minimal symbol's "info" field
80 is used for this purpose.
82 MSYMBOL_SET_SPECIAL Actually sets the "special" bit.
83 MSYMBOL_IS_SPECIAL Tests the "special" bit in a minimal symbol. */
85 #define MSYMBOL_SET_SPECIAL(msym) \
86 (msym)->set_target_flag_1 (true)
88 #define MSYMBOL_IS_SPECIAL(msym) \
89 (msym)->target_flag_1 ()
91 struct arm_mapping_symbol
93 CORE_ADDR value;
94 char type;
96 bool operator< (const arm_mapping_symbol &other) const
97 { return this->value < other.value; }
100 typedef std::vector<arm_mapping_symbol> arm_mapping_symbol_vec;
102 struct arm_per_bfd
104 explicit arm_per_bfd (size_t num_sections)
105 : section_maps (new arm_mapping_symbol_vec[num_sections]),
106 section_maps_sorted (new bool[num_sections] ())
109 DISABLE_COPY_AND_ASSIGN (arm_per_bfd);
111 /* Information about mapping symbols ($a, $d, $t) in the objfile.
113 The format is an array of vectors of arm_mapping_symbols, there is one
114 vector for each section of the objfile (the array is index by BFD section
115 index).
117 For each section, the vector of arm_mapping_symbol is sorted by
118 symbol value (address). */
119 std::unique_ptr<arm_mapping_symbol_vec[]> section_maps;
121 /* For each corresponding element of section_maps above, is this vector
122 sorted. */
123 std::unique_ptr<bool[]> section_maps_sorted;
126 /* Per-bfd data used for mapping symbols. */
127 static const registry<bfd>::key<arm_per_bfd> arm_bfd_data_key;
129 /* The list of available "set arm ..." and "show arm ..." commands. */
130 static struct cmd_list_element *setarmcmdlist = NULL;
131 static struct cmd_list_element *showarmcmdlist = NULL;
133 /* The type of floating-point to use. Keep this in sync with enum
134 arm_float_model, and the help string in _initialize_arm_tdep. */
135 static const char *const fp_model_strings[] =
137 "auto",
138 "softfpa",
139 "fpa",
140 "softvfp",
141 "vfp",
142 NULL
145 /* A variable that can be configured by the user. */
146 static enum arm_float_model arm_fp_model = ARM_FLOAT_AUTO;
147 static const char *current_fp_model = "auto";
149 /* The ABI to use. Keep this in sync with arm_abi_kind. */
150 static const char *const arm_abi_strings[] =
152 "auto",
153 "APCS",
154 "AAPCS",
155 NULL
158 /* A variable that can be configured by the user. */
159 static enum arm_abi_kind arm_abi_global = ARM_ABI_AUTO;
160 static const char *arm_abi_string = "auto";
162 /* The execution mode to assume. */
163 static const char *const arm_mode_strings[] =
165 "auto",
166 "arm",
167 "thumb",
168 NULL
171 static const char *arm_fallback_mode_string = "auto";
172 static const char *arm_force_mode_string = "auto";
174 /* The standard register names, and all the valid aliases for them. Note
175 that `fp', `sp' and `pc' are not added in this alias list, because they
176 have been added as builtin user registers in
177 std-regs.c:_initialize_frame_reg. */
178 static const struct
180 const char *name;
181 int regnum;
182 } arm_register_aliases[] = {
183 /* Basic register numbers. */
184 { "r0", 0 },
185 { "r1", 1 },
186 { "r2", 2 },
187 { "r3", 3 },
188 { "r4", 4 },
189 { "r5", 5 },
190 { "r6", 6 },
191 { "r7", 7 },
192 { "r8", 8 },
193 { "r9", 9 },
194 { "r10", 10 },
195 { "r11", 11 },
196 { "r12", 12 },
197 { "r13", 13 },
198 { "r14", 14 },
199 { "r15", 15 },
200 /* Synonyms (argument and variable registers). */
201 { "a1", 0 },
202 { "a2", 1 },
203 { "a3", 2 },
204 { "a4", 3 },
205 { "v1", 4 },
206 { "v2", 5 },
207 { "v3", 6 },
208 { "v4", 7 },
209 { "v5", 8 },
210 { "v6", 9 },
211 { "v7", 10 },
212 { "v8", 11 },
213 /* Other platform-specific names for r9. */
214 { "sb", 9 },
215 { "tr", 9 },
216 /* Special names. */
217 { "ip", 12 },
218 { "lr", 14 },
219 /* Names used by GCC (not listed in the ARM EABI). */
220 { "sl", 10 },
221 /* A special name from the older ATPCS. */
222 { "wr", 7 },
225 static const char *const arm_register_names[] =
226 {"r0", "r1", "r2", "r3", /* 0 1 2 3 */
227 "r4", "r5", "r6", "r7", /* 4 5 6 7 */
228 "r8", "r9", "r10", "r11", /* 8 9 10 11 */
229 "r12", "sp", "lr", "pc", /* 12 13 14 15 */
230 "f0", "f1", "f2", "f3", /* 16 17 18 19 */
231 "f4", "f5", "f6", "f7", /* 20 21 22 23 */
232 "fps", "cpsr" }; /* 24 25 */
234 /* Holds the current set of options to be passed to the disassembler. */
235 static std::string arm_disassembler_options;
237 /* Valid register name styles. */
238 static const char **valid_disassembly_styles;
240 /* Disassembly style to use. Default to "std" register names. */
241 static const char *disassembly_style;
243 /* All possible arm target descriptors. */
244 static struct target_desc *tdesc_arm_list[ARM_FP_TYPE_INVALID][2];
245 static struct target_desc *tdesc_arm_mprofile_list[ARM_M_TYPE_INVALID];
247 /* This is used to keep the bfd arch_info in sync with the disassembly
248 style. */
249 static void set_disassembly_style_sfunc (const char *, int,
250 struct cmd_list_element *);
251 static void show_disassembly_style_sfunc (struct ui_file *, int,
252 struct cmd_list_element *,
253 const char *);
255 static enum register_status arm_neon_quad_read (struct gdbarch *gdbarch,
256 readable_regcache *regcache,
257 int regnum, gdb_byte *buf);
258 static void arm_neon_quad_write (struct gdbarch *gdbarch,
259 struct regcache *regcache,
260 int regnum, const gdb_byte *buf);
262 static CORE_ADDR
263 arm_get_next_pcs_syscall_next_pc (struct arm_get_next_pcs *self);
266 /* get_next_pcs operations. */
267 static struct arm_get_next_pcs_ops arm_get_next_pcs_ops = {
268 arm_get_next_pcs_read_memory_unsigned_integer,
269 arm_get_next_pcs_syscall_next_pc,
270 arm_get_next_pcs_addr_bits_remove,
271 arm_get_next_pcs_is_thumb,
272 NULL,
275 struct arm_prologue_cache
277 /* The stack pointer at the time this frame was created; i.e. the
278 caller's stack pointer when this function was called. It is used
279 to identify this frame. */
280 CORE_ADDR sp;
282 /* Additional stack pointers used by M-profile with Security extension. */
283 /* Use msp_s / psp_s to hold the values of msp / psp when there is
284 no Security extension. */
285 CORE_ADDR msp_s;
286 CORE_ADDR msp_ns;
287 CORE_ADDR psp_s;
288 CORE_ADDR psp_ns;
290 /* Active stack pointer. */
291 int active_sp_regnum;
292 int active_msp_regnum;
293 int active_psp_regnum;
295 /* The frame base for this frame is just prev_sp - frame size.
296 FRAMESIZE is the distance from the frame pointer to the
297 initial stack pointer. */
299 int framesize;
301 /* The register used to hold the frame pointer for this frame. */
302 int framereg;
304 /* True if the return address is signed, false otherwise. */
305 std::optional<bool> ra_signed_state;
307 /* Saved register offsets. */
308 trad_frame_saved_reg *saved_regs;
310 arm_prologue_cache() = default;
314 /* Reconstruct T bit in program status register from LR value. */
316 static inline ULONGEST
317 reconstruct_t_bit(struct gdbarch *gdbarch, CORE_ADDR lr, ULONGEST psr)
319 ULONGEST t_bit = arm_psr_thumb_bit (gdbarch);
320 if (IS_THUMB_ADDR (lr))
321 psr |= t_bit;
322 else
323 psr &= ~t_bit;
325 return psr;
328 /* Initialize CACHE fields for which zero is not adequate (CACHE is
329 expected to have been ZALLOC'ed before calling this function). */
331 static void
332 arm_cache_init (struct arm_prologue_cache *cache, struct gdbarch *gdbarch)
334 cache->active_sp_regnum = ARM_SP_REGNUM;
336 cache->saved_regs = trad_frame_alloc_saved_regs (gdbarch);
339 /* Similar to the previous function, but extracts GDBARCH from FRAME. */
341 static void
342 arm_cache_init (struct arm_prologue_cache *cache, const frame_info_ptr &frame)
344 struct gdbarch *gdbarch = get_frame_arch (frame);
345 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
347 arm_cache_init (cache, gdbarch);
348 cache->sp = get_frame_register_unsigned (frame, ARM_SP_REGNUM);
350 if (tdep->have_sec_ext)
352 const CORE_ADDR msp_val
353 = get_frame_register_unsigned (frame, tdep->m_profile_msp_regnum);
354 const CORE_ADDR psp_val
355 = get_frame_register_unsigned (frame, tdep->m_profile_psp_regnum);
357 cache->msp_s
358 = get_frame_register_unsigned (frame, tdep->m_profile_msp_s_regnum);
359 cache->msp_ns
360 = get_frame_register_unsigned (frame, tdep->m_profile_msp_ns_regnum);
361 cache->psp_s
362 = get_frame_register_unsigned (frame, tdep->m_profile_psp_s_regnum);
363 cache->psp_ns
364 = get_frame_register_unsigned (frame, tdep->m_profile_psp_ns_regnum);
366 /* Identify what msp is alias for (msp_s or msp_ns). */
367 if (msp_val == cache->msp_s)
368 cache->active_msp_regnum = tdep->m_profile_msp_s_regnum;
369 else if (msp_val == cache->msp_ns)
370 cache->active_msp_regnum = tdep->m_profile_msp_ns_regnum;
371 else
373 warning (_("Invalid state, unable to determine msp alias, assuming "
374 "msp_s."));
375 cache->active_msp_regnum = tdep->m_profile_msp_s_regnum;
378 /* Identify what psp is alias for (psp_s or psp_ns). */
379 if (psp_val == cache->psp_s)
380 cache->active_psp_regnum = tdep->m_profile_psp_s_regnum;
381 else if (psp_val == cache->psp_ns)
382 cache->active_psp_regnum = tdep->m_profile_psp_ns_regnum;
383 else
385 warning (_("Invalid state, unable to determine psp alias, assuming "
386 "psp_s."));
387 cache->active_psp_regnum = tdep->m_profile_psp_s_regnum;
390 /* Identify what sp is alias for (msp_s, msp_ns, psp_s or psp_ns). */
391 if (msp_val == cache->sp)
392 cache->active_sp_regnum = cache->active_msp_regnum;
393 else if (psp_val == cache->sp)
394 cache->active_sp_regnum = cache->active_psp_regnum;
395 else
397 warning (_("Invalid state, unable to determine sp alias, assuming "
398 "msp."));
399 cache->active_sp_regnum = cache->active_msp_regnum;
402 else if (tdep->is_m)
404 cache->msp_s
405 = get_frame_register_unsigned (frame, tdep->m_profile_msp_regnum);
406 cache->psp_s
407 = get_frame_register_unsigned (frame, tdep->m_profile_psp_regnum);
409 /* Identify what sp is alias for (msp or psp). */
410 if (cache->msp_s == cache->sp)
411 cache->active_sp_regnum = tdep->m_profile_msp_regnum;
412 else if (cache->psp_s == cache->sp)
413 cache->active_sp_regnum = tdep->m_profile_psp_regnum;
414 else
416 warning (_("Invalid state, unable to determine sp alias, assuming "
417 "msp."));
418 cache->active_sp_regnum = tdep->m_profile_msp_regnum;
421 else
423 cache->msp_s
424 = get_frame_register_unsigned (frame, ARM_SP_REGNUM);
426 cache->active_sp_regnum = ARM_SP_REGNUM;
430 /* Return the requested stack pointer value (in REGNUM), taking into
431 account whether we have a Security extension or an M-profile
432 CPU. */
434 static CORE_ADDR
435 arm_cache_get_sp_register (struct arm_prologue_cache *cache,
436 arm_gdbarch_tdep *tdep, int regnum)
438 if (tdep->have_sec_ext)
440 if (regnum == tdep->m_profile_msp_s_regnum)
441 return cache->msp_s;
442 if (regnum == tdep->m_profile_msp_ns_regnum)
443 return cache->msp_ns;
444 if (regnum == tdep->m_profile_psp_s_regnum)
445 return cache->psp_s;
446 if (regnum == tdep->m_profile_psp_ns_regnum)
447 return cache->psp_ns;
448 if (regnum == tdep->m_profile_msp_regnum)
449 return arm_cache_get_sp_register (cache, tdep, cache->active_msp_regnum);
450 if (regnum == tdep->m_profile_psp_regnum)
451 return arm_cache_get_sp_register (cache, tdep, cache->active_psp_regnum);
452 if (regnum == ARM_SP_REGNUM)
453 return arm_cache_get_sp_register (cache, tdep, cache->active_sp_regnum);
455 else if (tdep->is_m)
457 if (regnum == tdep->m_profile_msp_regnum)
458 return cache->msp_s;
459 if (regnum == tdep->m_profile_psp_regnum)
460 return cache->psp_s;
461 if (regnum == ARM_SP_REGNUM)
462 return arm_cache_get_sp_register (cache, tdep, cache->active_sp_regnum);
464 else if (regnum == ARM_SP_REGNUM)
465 return cache->sp;
467 gdb_assert_not_reached ("Invalid SP selection");
470 /* Return the previous stack address, depending on which SP register
471 is active. */
473 static CORE_ADDR
474 arm_cache_get_prev_sp_value (struct arm_prologue_cache *cache, arm_gdbarch_tdep *tdep)
476 CORE_ADDR val = arm_cache_get_sp_register (cache, tdep, cache->active_sp_regnum);
477 return val;
480 /* Set the active stack pointer to VAL. */
482 static void
483 arm_cache_set_active_sp_value (struct arm_prologue_cache *cache,
484 arm_gdbarch_tdep *tdep, CORE_ADDR val)
486 if (tdep->have_sec_ext)
488 if (cache->active_sp_regnum == tdep->m_profile_msp_s_regnum)
489 cache->msp_s = val;
490 else if (cache->active_sp_regnum == tdep->m_profile_msp_ns_regnum)
491 cache->msp_ns = val;
492 else if (cache->active_sp_regnum == tdep->m_profile_psp_s_regnum)
493 cache->psp_s = val;
494 else if (cache->active_sp_regnum == tdep->m_profile_psp_ns_regnum)
495 cache->psp_ns = val;
497 return;
499 else if (tdep->is_m)
501 if (cache->active_sp_regnum == tdep->m_profile_msp_regnum)
502 cache->msp_s = val;
503 else if (cache->active_sp_regnum == tdep->m_profile_psp_regnum)
504 cache->psp_s = val;
506 return;
508 else if (cache->active_sp_regnum == ARM_SP_REGNUM)
510 cache->sp = val;
511 return;
514 gdb_assert_not_reached ("Invalid SP selection");
517 /* Return true if REGNUM is one of the alternative stack pointers. */
519 static bool
520 arm_is_alternative_sp_register (arm_gdbarch_tdep *tdep, int regnum)
522 if ((regnum == tdep->m_profile_msp_regnum)
523 || (regnum == tdep->m_profile_msp_s_regnum)
524 || (regnum == tdep->m_profile_msp_ns_regnum)
525 || (regnum == tdep->m_profile_psp_regnum)
526 || (regnum == tdep->m_profile_psp_s_regnum)
527 || (regnum == tdep->m_profile_psp_ns_regnum))
528 return true;
529 else
530 return false;
533 /* Set the active stack pointer to SP_REGNUM. */
535 static void
536 arm_cache_switch_prev_sp (struct arm_prologue_cache *cache,
537 arm_gdbarch_tdep *tdep, int sp_regnum)
539 gdb_assert (arm_is_alternative_sp_register (tdep, sp_regnum));
541 if (tdep->have_sec_ext)
543 gdb_assert (sp_regnum != tdep->m_profile_msp_regnum
544 && sp_regnum != tdep->m_profile_psp_regnum);
546 if (sp_regnum == tdep->m_profile_msp_s_regnum
547 || sp_regnum == tdep->m_profile_psp_s_regnum)
549 cache->active_msp_regnum = tdep->m_profile_msp_s_regnum;
550 cache->active_psp_regnum = tdep->m_profile_psp_s_regnum;
552 else if (sp_regnum == tdep->m_profile_msp_ns_regnum
553 || sp_regnum == tdep->m_profile_psp_ns_regnum)
555 cache->active_msp_regnum = tdep->m_profile_msp_ns_regnum;
556 cache->active_psp_regnum = tdep->m_profile_psp_ns_regnum;
560 cache->active_sp_regnum = sp_regnum;
563 namespace {
565 /* Abstract class to read ARM instructions from memory. */
567 class arm_instruction_reader
569 public:
570 /* Read a 4 bytes instruction from memory using the BYTE_ORDER endianness. */
571 virtual uint32_t read (CORE_ADDR memaddr, bfd_endian byte_order) const = 0;
574 /* Read instructions from target memory. */
576 class target_arm_instruction_reader : public arm_instruction_reader
578 public:
579 uint32_t read (CORE_ADDR memaddr, bfd_endian byte_order) const override
581 return read_code_unsigned_integer (memaddr, 4, byte_order);
585 } /* namespace */
587 static CORE_ADDR arm_analyze_prologue
588 (struct gdbarch *gdbarch, CORE_ADDR prologue_start, CORE_ADDR prologue_end,
589 struct arm_prologue_cache *cache, const arm_instruction_reader &insn_reader);
591 /* Architecture version for displaced stepping. This effects the behaviour of
592 certain instructions, and really should not be hard-wired. */
594 #define DISPLACED_STEPPING_ARCH_VERSION 5
596 /* See arm-tdep.h. */
598 bool arm_apcs_32 = true;
599 bool arm_unwind_secure_frames = true;
601 /* Return the bit mask in ARM_PS_REGNUM that indicates Thumb mode. */
604 arm_psr_thumb_bit (struct gdbarch *gdbarch)
606 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
608 if (tdep->is_m)
609 return XPSR_T;
610 else
611 return CPSR_T;
614 /* Determine if the processor is currently executing in Thumb mode. */
617 arm_is_thumb (struct regcache *regcache)
619 ULONGEST cpsr;
620 ULONGEST t_bit = arm_psr_thumb_bit (regcache->arch ());
622 cpsr = regcache_raw_get_unsigned (regcache, ARM_PS_REGNUM);
624 return (cpsr & t_bit) != 0;
627 /* Determine if FRAME is executing in Thumb mode. FRAME must be an ARM
628 frame. */
631 arm_frame_is_thumb (const frame_info_ptr &frame)
633 /* Check the architecture of FRAME. */
634 struct gdbarch *gdbarch = get_frame_arch (frame);
635 gdb_assert (gdbarch_bfd_arch_info (gdbarch)->arch == bfd_arch_arm);
637 /* Every ARM frame unwinder can unwind the T bit of the CPSR, either
638 directly (from a signal frame or dummy frame) or by interpreting
639 the saved LR (from a prologue or DWARF frame). So consult it and
640 trust the unwinders. */
641 CORE_ADDR cpsr = get_frame_register_unsigned (frame, ARM_PS_REGNUM);
643 /* Find and extract the thumb bit. */
644 ULONGEST t_bit = arm_psr_thumb_bit (gdbarch);
645 return (cpsr & t_bit) != 0;
648 /* Search for the mapping symbol covering MEMADDR. If one is found,
649 return its type. Otherwise, return 0. If START is non-NULL,
650 set *START to the location of the mapping symbol. */
652 static char
653 arm_find_mapping_symbol (CORE_ADDR memaddr, CORE_ADDR *start)
655 struct obj_section *sec;
657 /* If there are mapping symbols, consult them. */
658 sec = find_pc_section (memaddr);
659 if (sec != NULL)
661 arm_per_bfd *data = arm_bfd_data_key.get (sec->objfile->obfd.get ());
662 if (data != NULL)
664 unsigned int section_idx = sec->the_bfd_section->index;
665 arm_mapping_symbol_vec &map
666 = data->section_maps[section_idx];
668 /* Sort the vector on first use. */
669 if (!data->section_maps_sorted[section_idx])
671 std::sort (map.begin (), map.end ());
672 data->section_maps_sorted[section_idx] = true;
675 arm_mapping_symbol map_key = { memaddr - sec->addr (), 0 };
676 arm_mapping_symbol_vec::const_iterator it
677 = std::lower_bound (map.begin (), map.end (), map_key);
679 /* std::lower_bound finds the earliest ordered insertion
680 point. If the symbol at this position starts at this exact
681 address, we use that; otherwise, the preceding
682 mapping symbol covers this address. */
683 if (it < map.end ())
685 if (it->value == map_key.value)
687 if (start)
688 *start = it->value + sec->addr ();
689 return it->type;
693 if (it > map.begin ())
695 arm_mapping_symbol_vec::const_iterator prev_it
696 = it - 1;
698 if (start)
699 *start = prev_it->value + sec->addr ();
700 return prev_it->type;
705 return 0;
708 /* Determine if the program counter specified in MEMADDR is in a Thumb
709 function. This function should be called for addresses unrelated to
710 any executing frame; otherwise, prefer arm_frame_is_thumb. */
713 arm_pc_is_thumb (struct gdbarch *gdbarch, CORE_ADDR memaddr)
715 char type;
716 arm_displaced_step_copy_insn_closure *dsc = nullptr;
717 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
719 if (gdbarch_displaced_step_copy_insn_closure_by_addr_p (gdbarch))
720 dsc = ((arm_displaced_step_copy_insn_closure * )
721 gdbarch_displaced_step_copy_insn_closure_by_addr
722 (gdbarch, current_inferior (), memaddr));
724 /* If checking the mode of displaced instruction in copy area, the mode
725 should be determined by instruction on the original address. */
726 if (dsc)
728 displaced_debug_printf ("check mode of %.8lx instead of %.8lx",
729 (unsigned long) dsc->insn_addr,
730 (unsigned long) memaddr);
731 memaddr = dsc->insn_addr;
734 /* If bit 0 of the address is set, assume this is a Thumb address. */
735 if (IS_THUMB_ADDR (memaddr))
736 return 1;
738 /* If the user wants to override the symbol table, let him. */
739 if (strcmp (arm_force_mode_string, "arm") == 0)
740 return 0;
741 if (strcmp (arm_force_mode_string, "thumb") == 0)
742 return 1;
744 /* ARM v6-M and v7-M are always in Thumb mode. */
745 if (tdep->is_m)
746 return 1;
748 /* If there are mapping symbols, consult them. */
749 type = arm_find_mapping_symbol (memaddr, NULL);
750 if (type)
751 return type == 't';
753 /* Thumb functions have a "special" bit set in minimal symbols. */
754 bound_minimal_symbol sym = lookup_minimal_symbol_by_pc (memaddr);
755 if (sym.minsym)
756 return (MSYMBOL_IS_SPECIAL (sym.minsym));
758 /* If the user wants to override the fallback mode, let them. */
759 if (strcmp (arm_fallback_mode_string, "arm") == 0)
760 return 0;
761 if (strcmp (arm_fallback_mode_string, "thumb") == 0)
762 return 1;
764 /* If we couldn't find any symbol, but we're talking to a running
765 target, then trust the current value of $cpsr. This lets
766 "display/i $pc" always show the correct mode (though if there is
767 a symbol table we will not reach here, so it still may not be
768 displayed in the mode it will be executed). */
769 if (target_has_registers ())
770 return arm_frame_is_thumb (get_current_frame ());
772 /* Otherwise we're out of luck; we assume ARM. */
773 return 0;
776 static inline bool
777 arm_m_addr_is_lockup (CORE_ADDR addr)
779 switch (addr)
781 /* Values for lockup state.
782 For more details see "B1.5.15 Unrecoverable exception cases" in
783 both ARMv6-M and ARMv7-M Architecture Reference Manuals, or
784 see "B4.32 Lockup" in ARMv8-M Architecture Reference Manual. */
785 case 0xeffffffe:
786 case 0xfffffffe:
787 case 0xffffffff:
788 return true;
790 default:
791 /* Address is not lockup. */
792 return false;
796 /* Determine if the address specified equals any of these magic return
797 values, called EXC_RETURN, defined by the ARM v6-M, v7-M and v8-M
798 architectures. Also include lockup magic PC value.
799 Check also for FNC_RETURN if we have the v8-M security extension.
801 From ARMv6-M Reference Manual B1.5.8
802 Table B1-5 Exception return behavior
804 EXC_RETURN Return To Return Stack
805 0xFFFFFFF1 Handler mode Main
806 0xFFFFFFF9 Thread mode Main
807 0xFFFFFFFD Thread mode Process
809 From ARMv7-M Reference Manual B1.5.8
810 Table B1-8 EXC_RETURN definition of exception return behavior, no FP
812 EXC_RETURN Return To Return Stack
813 0xFFFFFFF1 Handler mode Main
814 0xFFFFFFF9 Thread mode Main
815 0xFFFFFFFD Thread mode Process
817 Table B1-9 EXC_RETURN definition of exception return behavior, with
820 EXC_RETURN Return To Return Stack Frame Type
821 0xFFFFFFE1 Handler mode Main Extended
822 0xFFFFFFE9 Thread mode Main Extended
823 0xFFFFFFED Thread mode Process Extended
824 0xFFFFFFF1 Handler mode Main Basic
825 0xFFFFFFF9 Thread mode Main Basic
826 0xFFFFFFFD Thread mode Process Basic
828 For more details see "B1.5.8 Exception return behavior"
829 in both ARMv6-M and ARMv7-M Architecture Reference Manuals.
831 From ARMv8-M Architecture Technical Reference, D1.2.95
832 FType, Mode and SPSEL bits are to be considered when the Security
833 Extension is not implemented.
835 EXC_RETURN Return To Return Stack Frame Type
836 0xFFFFFFA0 Handler mode Main Extended
837 0xFFFFFFA8 Thread mode Main Extended
838 0xFFFFFFAC Thread mode Process Extended
839 0xFFFFFFB0 Handler mode Main Standard
840 0xFFFFFFB8 Thread mode Main Standard
841 0xFFFFFFBC Thread mode Process Standard */
843 static int
844 arm_m_addr_is_magic (struct gdbarch *gdbarch, CORE_ADDR addr)
846 if (arm_m_addr_is_lockup (addr))
847 return 1;
849 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
850 if (tdep->have_sec_ext)
852 switch ((addr & 0xff000000))
854 case 0xff000000: /* EXC_RETURN pattern. */
855 case 0xfe000000: /* FNC_RETURN pattern. */
856 return 1;
857 default:
858 return 0;
861 else
863 switch (addr)
865 /* Values from ARMv8-M Architecture Technical Reference. */
866 case 0xffffffa0:
867 case 0xffffffa8:
868 case 0xffffffac:
869 case 0xffffffb0:
870 case 0xffffffb8:
871 case 0xffffffbc:
872 /* Values from Tables in B1.5.8 the EXC_RETURN definitions of
873 the exception return behavior. */
874 case 0xffffffe1:
875 case 0xffffffe9:
876 case 0xffffffed:
877 case 0xfffffff1:
878 case 0xfffffff9:
879 case 0xfffffffd:
880 /* Address is magic. */
881 return 1;
883 default:
884 /* Address is not magic. */
885 return 0;
890 /* Remove useless bits from addresses in a running program. */
891 static CORE_ADDR
892 arm_addr_bits_remove (struct gdbarch *gdbarch, CORE_ADDR val)
894 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
896 /* On M-profile devices, do not strip the low bit from EXC_RETURN
897 (the magic exception return address). */
898 if (tdep->is_m && arm_m_addr_is_magic (gdbarch, val))
899 return val;
901 if (arm_apcs_32)
902 return UNMAKE_THUMB_ADDR (val);
903 else
904 return (val & 0x03fffffc);
907 /* Return 1 if PC is the start of a compiler helper function which
908 can be safely ignored during prologue skipping. IS_THUMB is true
909 if the function is known to be a Thumb function due to the way it
910 is being called. */
911 static int
912 skip_prologue_function (struct gdbarch *gdbarch, CORE_ADDR pc, int is_thumb)
914 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
916 bound_minimal_symbol msym = lookup_minimal_symbol_by_pc (pc);
917 if (msym.minsym != NULL
918 && msym.value_address () == pc
919 && msym.minsym->linkage_name () != NULL)
921 const char *name = msym.minsym->linkage_name ();
923 /* The GNU linker's Thumb call stub to foo is named
924 __foo_from_thumb. */
925 if (strstr (name, "_from_thumb") != NULL)
926 name += 2;
928 /* On soft-float targets, __truncdfsf2 is called to convert promoted
929 arguments to their argument types in non-prototyped
930 functions. */
931 if (startswith (name, "__truncdfsf2"))
932 return 1;
933 if (startswith (name, "__aeabi_d2f"))
934 return 1;
936 /* Internal functions related to thread-local storage. */
937 if (startswith (name, "__tls_get_addr"))
938 return 1;
939 if (startswith (name, "__aeabi_read_tp"))
940 return 1;
942 else
944 /* If we run against a stripped glibc, we may be unable to identify
945 special functions by name. Check for one important case,
946 __aeabi_read_tp, by comparing the *code* against the default
947 implementation (this is hand-written ARM assembler in glibc). */
949 if (!is_thumb
950 && read_code_unsigned_integer (pc, 4, byte_order_for_code)
951 == 0xe3e00a0f /* mov r0, #0xffff0fff */
952 && read_code_unsigned_integer (pc + 4, 4, byte_order_for_code)
953 == 0xe240f01f) /* sub pc, r0, #31 */
954 return 1;
957 return 0;
960 /* Extract the immediate from instruction movw/movt of encoding T. INSN1 is
961 the first 16-bit of instruction, and INSN2 is the second 16-bit of
962 instruction. */
963 #define EXTRACT_MOVW_MOVT_IMM_T(insn1, insn2) \
964 ((bits ((insn1), 0, 3) << 12) \
965 | (bits ((insn1), 10, 10) << 11) \
966 | (bits ((insn2), 12, 14) << 8) \
967 | bits ((insn2), 0, 7))
969 /* Extract the immediate from instruction movw/movt of encoding A. INSN is
970 the 32-bit instruction. */
971 #define EXTRACT_MOVW_MOVT_IMM_A(insn) \
972 ((bits ((insn), 16, 19) << 12) \
973 | bits ((insn), 0, 11))
975 /* Decode immediate value; implements ThumbExpandImmediate pseudo-op. */
977 static unsigned int
978 thumb_expand_immediate (unsigned int imm)
980 unsigned int count = imm >> 7;
982 if (count < 8)
983 switch (count / 2)
985 case 0:
986 return imm & 0xff;
987 case 1:
988 return (imm & 0xff) | ((imm & 0xff) << 16);
989 case 2:
990 return ((imm & 0xff) << 8) | ((imm & 0xff) << 24);
991 case 3:
992 return (imm & 0xff) | ((imm & 0xff) << 8)
993 | ((imm & 0xff) << 16) | ((imm & 0xff) << 24);
996 return (0x80 | (imm & 0x7f)) << (32 - count);
999 /* Return 1 if the 16-bit Thumb instruction INSN restores SP in
1000 epilogue, 0 otherwise. */
1002 static int
1003 thumb_instruction_restores_sp (unsigned short insn)
1005 return (insn == 0x46bd /* mov sp, r7 */
1006 || (insn & 0xff80) == 0xb000 /* add sp, imm */
1007 || (insn & 0xfe00) == 0xbc00); /* pop <registers> */
1010 /* Analyze a Thumb prologue, looking for a recognizable stack frame
1011 and frame pointer. Scan until we encounter a store that could
1012 clobber the stack frame unexpectedly, or an unknown instruction.
1013 Return the last address which is definitely safe to skip for an
1014 initial breakpoint. */
1016 static CORE_ADDR
1017 thumb_analyze_prologue (struct gdbarch *gdbarch,
1018 CORE_ADDR start, CORE_ADDR limit,
1019 struct arm_prologue_cache *cache)
1021 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
1022 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1023 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
1024 int i;
1025 pv_t regs[16];
1026 CORE_ADDR offset;
1027 CORE_ADDR unrecognized_pc = 0;
1029 for (i = 0; i < 16; i++)
1030 regs[i] = pv_register (i, 0);
1031 pv_area stack (ARM_SP_REGNUM, gdbarch_addr_bit (gdbarch));
1033 while (start < limit)
1035 unsigned short insn;
1036 std::optional<bool> ra_signed_state;
1038 insn = read_code_unsigned_integer (start, 2, byte_order_for_code);
1040 if ((insn & 0xfe00) == 0xb400) /* push { rlist } */
1042 int regno;
1043 int mask;
1045 if (stack.store_would_trash (regs[ARM_SP_REGNUM]))
1046 break;
1048 /* Bits 0-7 contain a mask for registers R0-R7. Bit 8 says
1049 whether to save LR (R14). */
1050 mask = (insn & 0xff) | ((insn & 0x100) << 6);
1052 /* Calculate offsets of saved R0-R7 and LR. */
1053 for (regno = ARM_LR_REGNUM; regno >= 0; regno--)
1054 if (mask & (1 << regno))
1056 regs[ARM_SP_REGNUM] = pv_add_constant (regs[ARM_SP_REGNUM],
1057 -4);
1058 stack.store (regs[ARM_SP_REGNUM], 4, regs[regno]);
1061 else if ((insn & 0xff80) == 0xb080) /* sub sp, #imm */
1063 offset = (insn & 0x7f) << 2; /* get scaled offset */
1064 regs[ARM_SP_REGNUM] = pv_add_constant (regs[ARM_SP_REGNUM],
1065 -offset);
1067 else if (thumb_instruction_restores_sp (insn))
1069 /* Don't scan past the epilogue. */
1070 break;
1072 else if ((insn & 0xf800) == 0xa800) /* add Rd, sp, #imm */
1073 regs[bits (insn, 8, 10)] = pv_add_constant (regs[ARM_SP_REGNUM],
1074 (insn & 0xff) << 2);
1075 else if ((insn & 0xfe00) == 0x1c00 /* add Rd, Rn, #imm */
1076 && pv_is_register (regs[bits (insn, 3, 5)], ARM_SP_REGNUM))
1077 regs[bits (insn, 0, 2)] = pv_add_constant (regs[bits (insn, 3, 5)],
1078 bits (insn, 6, 8));
1079 else if ((insn & 0xf800) == 0x3000 /* add Rd, #imm */
1080 && pv_is_register (regs[bits (insn, 8, 10)], ARM_SP_REGNUM))
1081 regs[bits (insn, 8, 10)] = pv_add_constant (regs[bits (insn, 8, 10)],
1082 bits (insn, 0, 7));
1083 else if ((insn & 0xfe00) == 0x1800 /* add Rd, Rn, Rm */
1084 && pv_is_register (regs[bits (insn, 6, 8)], ARM_SP_REGNUM)
1085 && pv_is_constant (regs[bits (insn, 3, 5)]))
1086 regs[bits (insn, 0, 2)] = pv_add (regs[bits (insn, 3, 5)],
1087 regs[bits (insn, 6, 8)]);
1088 else if ((insn & 0xff00) == 0x4400 /* add Rd, Rm */
1089 && pv_is_constant (regs[bits (insn, 3, 6)]))
1091 int rd = (bit (insn, 7) << 3) + bits (insn, 0, 2);
1092 int rm = bits (insn, 3, 6);
1093 regs[rd] = pv_add (regs[rd], regs[rm]);
1095 else if ((insn & 0xff00) == 0x4600) /* mov hi, lo or mov lo, hi */
1097 int dst_reg = (insn & 0x7) + ((insn & 0x80) >> 4);
1098 int src_reg = (insn & 0x78) >> 3;
1099 regs[dst_reg] = regs[src_reg];
1101 else if ((insn & 0xf800) == 0x9000) /* str rd, [sp, #off] */
1103 /* Handle stores to the stack. Normally pushes are used,
1104 but with GCC -mtpcs-frame, there may be other stores
1105 in the prologue to create the frame. */
1106 int regno = (insn >> 8) & 0x7;
1107 pv_t addr;
1109 offset = (insn & 0xff) << 2;
1110 addr = pv_add_constant (regs[ARM_SP_REGNUM], offset);
1112 if (stack.store_would_trash (addr))
1113 break;
1115 stack.store (addr, 4, regs[regno]);
1117 else if ((insn & 0xf800) == 0x6000) /* str rd, [rn, #off] */
1119 int rd = bits (insn, 0, 2);
1120 int rn = bits (insn, 3, 5);
1121 pv_t addr;
1123 offset = bits (insn, 6, 10) << 2;
1124 addr = pv_add_constant (regs[rn], offset);
1126 if (stack.store_would_trash (addr))
1127 break;
1129 stack.store (addr, 4, regs[rd]);
1131 else if (((insn & 0xf800) == 0x7000 /* strb Rd, [Rn, #off] */
1132 || (insn & 0xf800) == 0x8000) /* strh Rd, [Rn, #off] */
1133 && pv_is_register (regs[bits (insn, 3, 5)], ARM_SP_REGNUM))
1134 /* Ignore stores of argument registers to the stack. */
1136 else if ((insn & 0xf800) == 0xc800 /* ldmia Rn!, { registers } */
1137 && pv_is_register (regs[bits (insn, 8, 10)], ARM_SP_REGNUM))
1138 /* Ignore block loads from the stack, potentially copying
1139 parameters from memory. */
1141 else if ((insn & 0xf800) == 0x9800 /* ldr Rd, [Rn, #immed] */
1142 || ((insn & 0xf800) == 0x6800 /* ldr Rd, [sp, #immed] */
1143 && pv_is_register (regs[bits (insn, 3, 5)], ARM_SP_REGNUM)))
1144 /* Similarly ignore single loads from the stack. */
1146 else if ((insn & 0xffc0) == 0x0000 /* lsls Rd, Rm, #0 */
1147 || (insn & 0xffc0) == 0x1c00) /* add Rd, Rn, #0 */
1148 /* Skip register copies, i.e. saves to another register
1149 instead of the stack. */
1151 else if ((insn & 0xf800) == 0x2000) /* movs Rd, #imm */
1152 /* Recognize constant loads; even with small stacks these are necessary
1153 on Thumb. */
1154 regs[bits (insn, 8, 10)] = pv_constant (bits (insn, 0, 7));
1155 else if ((insn & 0xf800) == 0x4800) /* ldr Rd, [pc, #imm] */
1157 /* Constant pool loads, for the same reason. */
1158 unsigned int constant;
1159 CORE_ADDR loc;
1161 loc = start + 4 + bits (insn, 0, 7) * 4;
1162 constant = read_memory_unsigned_integer (loc, 4, byte_order);
1163 regs[bits (insn, 8, 10)] = pv_constant (constant);
1165 else if (thumb_insn_size (insn) == 4) /* 32-bit Thumb-2 instructions. */
1167 unsigned short inst2;
1169 inst2 = read_code_unsigned_integer (start + 2, 2,
1170 byte_order_for_code);
1171 uint32_t whole_insn = (insn << 16) | inst2;
1173 if ((insn & 0xf800) == 0xf000 && (inst2 & 0xe800) == 0xe800)
1175 /* BL, BLX. Allow some special function calls when
1176 skipping the prologue; GCC generates these before
1177 storing arguments to the stack. */
1178 CORE_ADDR nextpc;
1179 int j1, j2, imm1, imm2;
1181 imm1 = sbits (insn, 0, 10);
1182 imm2 = bits (inst2, 0, 10);
1183 j1 = bit (inst2, 13);
1184 j2 = bit (inst2, 11);
1186 offset = ((imm1 << 12) + (imm2 << 1));
1187 offset ^= ((!j2) << 22) | ((!j1) << 23);
1189 nextpc = start + 4 + offset;
1190 /* For BLX make sure to clear the low bits. */
1191 if (bit (inst2, 12) == 0)
1192 nextpc = nextpc & 0xfffffffc;
1194 if (!skip_prologue_function (gdbarch, nextpc,
1195 bit (inst2, 12) != 0))
1196 break;
1199 else if ((insn & 0xffd0) == 0xe900 /* stmdb Rn{!},
1200 { registers } */
1201 && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
1203 pv_t addr = regs[bits (insn, 0, 3)];
1204 int regno;
1206 if (stack.store_would_trash (addr))
1207 break;
1209 /* Calculate offsets of saved registers. */
1210 for (regno = ARM_LR_REGNUM; regno >= 0; regno--)
1211 if (inst2 & (1 << regno))
1213 addr = pv_add_constant (addr, -4);
1214 stack.store (addr, 4, regs[regno]);
1217 if (insn & 0x0020)
1218 regs[bits (insn, 0, 3)] = addr;
1221 /* vstmdb Rn{!}, { D-registers } (aka vpush). */
1222 else if ((insn & 0xff20) == 0xed20
1223 && (inst2 & 0x0f00) == 0x0b00
1224 && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
1226 /* Address SP points to. */
1227 pv_t addr = regs[bits (insn, 0, 3)];
1229 /* Number of registers saved. */
1230 unsigned int number = bits (inst2, 0, 7) >> 1;
1232 /* First register to save. */
1233 int vd = bits (inst2, 12, 15) | (bits (insn, 6, 6) << 4);
1235 if (stack.store_would_trash (addr))
1236 break;
1238 /* Calculate offsets of saved registers. */
1239 for (; number > 0; number--)
1241 addr = pv_add_constant (addr, -8);
1242 stack.store (addr, 8, pv_register (ARM_D0_REGNUM
1243 + vd + number, 0));
1246 /* Writeback SP to account for the saved registers. */
1247 regs[bits (insn, 0, 3)] = addr;
1250 else if ((insn & 0xff50) == 0xe940 /* strd Rt, Rt2,
1251 [Rn, #+/-imm]{!} */
1252 && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
1254 int regno1 = bits (inst2, 12, 15);
1255 int regno2 = bits (inst2, 8, 11);
1256 pv_t addr = regs[bits (insn, 0, 3)];
1258 offset = inst2 & 0xff;
1259 if (insn & 0x0080)
1260 addr = pv_add_constant (addr, offset);
1261 else
1262 addr = pv_add_constant (addr, -offset);
1264 if (stack.store_would_trash (addr))
1265 break;
1267 stack.store (addr, 4, regs[regno1]);
1268 stack.store (pv_add_constant (addr, 4),
1269 4, regs[regno2]);
1271 if (insn & 0x0020)
1272 regs[bits (insn, 0, 3)] = addr;
1275 else if ((insn & 0xfff0) == 0xf8c0 /* str Rt,[Rn,+/-#imm]{!} */
1276 && (inst2 & 0x0c00) == 0x0c00
1277 && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
1279 int regno = bits (inst2, 12, 15);
1280 pv_t addr = regs[bits (insn, 0, 3)];
1282 offset = inst2 & 0xff;
1283 if (inst2 & 0x0200)
1284 addr = pv_add_constant (addr, offset);
1285 else
1286 addr = pv_add_constant (addr, -offset);
1288 if (stack.store_would_trash (addr))
1289 break;
1291 stack.store (addr, 4, regs[regno]);
1293 if (inst2 & 0x0100)
1294 regs[bits (insn, 0, 3)] = addr;
1297 else if ((insn & 0xfff0) == 0xf8c0 /* str.w Rt,[Rn,#imm] */
1298 && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
1300 int regno = bits (inst2, 12, 15);
1301 pv_t addr;
1303 offset = inst2 & 0xfff;
1304 addr = pv_add_constant (regs[bits (insn, 0, 3)], offset);
1306 if (stack.store_would_trash (addr))
1307 break;
1309 stack.store (addr, 4, regs[regno]);
1312 else if ((insn & 0xffd0) == 0xf880 /* str{bh}.w Rt,[Rn,#imm] */
1313 && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
1314 /* Ignore stores of argument registers to the stack. */
1317 else if ((insn & 0xffd0) == 0xf800 /* str{bh} Rt,[Rn,#+/-imm] */
1318 && (inst2 & 0x0d00) == 0x0c00
1319 && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
1320 /* Ignore stores of argument registers to the stack. */
1323 else if ((insn & 0xffd0) == 0xe890 /* ldmia Rn[!],
1324 { registers } */
1325 && (inst2 & 0x8000) == 0x0000
1326 && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
1327 /* Ignore block loads from the stack, potentially copying
1328 parameters from memory. */
1331 else if ((insn & 0xff70) == 0xe950 /* ldrd Rt, Rt2,
1332 [Rn, #+/-imm] */
1333 && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
1334 /* Similarly ignore dual loads from the stack. */
1337 else if ((insn & 0xfff0) == 0xf850 /* ldr Rt,[Rn,#+/-imm] */
1338 && (inst2 & 0x0d00) == 0x0c00
1339 && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
1340 /* Similarly ignore single loads from the stack. */
1343 else if ((insn & 0xfff0) == 0xf8d0 /* ldr.w Rt,[Rn,#imm] */
1344 && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
1345 /* Similarly ignore single loads from the stack. */
1348 else if ((insn & 0xfbf0) == 0xf100 /* add.w Rd, Rn, #imm */
1349 && (inst2 & 0x8000) == 0x0000)
1351 unsigned int imm = ((bits (insn, 10, 10) << 11)
1352 | (bits (inst2, 12, 14) << 8)
1353 | bits (inst2, 0, 7));
1355 regs[bits (inst2, 8, 11)]
1356 = pv_add_constant (regs[bits (insn, 0, 3)],
1357 thumb_expand_immediate (imm));
1360 else if ((insn & 0xfbf0) == 0xf200 /* addw Rd, Rn, #imm */
1361 && (inst2 & 0x8000) == 0x0000)
1363 unsigned int imm = ((bits (insn, 10, 10) << 11)
1364 | (bits (inst2, 12, 14) << 8)
1365 | bits (inst2, 0, 7));
1367 regs[bits (inst2, 8, 11)]
1368 = pv_add_constant (regs[bits (insn, 0, 3)], imm);
1371 else if ((insn & 0xfbf0) == 0xf1a0 /* sub.w Rd, Rn, #imm */
1372 && (inst2 & 0x8000) == 0x0000)
1374 unsigned int imm = ((bits (insn, 10, 10) << 11)
1375 | (bits (inst2, 12, 14) << 8)
1376 | bits (inst2, 0, 7));
1378 regs[bits (inst2, 8, 11)]
1379 = pv_add_constant (regs[bits (insn, 0, 3)],
1380 - (CORE_ADDR) thumb_expand_immediate (imm));
1383 else if ((insn & 0xfbf0) == 0xf2a0 /* subw Rd, Rn, #imm */
1384 && (inst2 & 0x8000) == 0x0000)
1386 unsigned int imm = ((bits (insn, 10, 10) << 11)
1387 | (bits (inst2, 12, 14) << 8)
1388 | bits (inst2, 0, 7));
1390 regs[bits (inst2, 8, 11)]
1391 = pv_add_constant (regs[bits (insn, 0, 3)], - (CORE_ADDR) imm);
1394 else if ((insn & 0xfbff) == 0xf04f) /* mov.w Rd, #const */
1396 unsigned int imm = ((bits (insn, 10, 10) << 11)
1397 | (bits (inst2, 12, 14) << 8)
1398 | bits (inst2, 0, 7));
1400 regs[bits (inst2, 8, 11)]
1401 = pv_constant (thumb_expand_immediate (imm));
1404 else if ((insn & 0xfbf0) == 0xf240) /* movw Rd, #const */
1406 unsigned int imm
1407 = EXTRACT_MOVW_MOVT_IMM_T (insn, inst2);
1409 regs[bits (inst2, 8, 11)] = pv_constant (imm);
1412 else if (insn == 0xea5f /* mov.w Rd,Rm */
1413 && (inst2 & 0xf0f0) == 0)
1415 int dst_reg = (inst2 & 0x0f00) >> 8;
1416 int src_reg = inst2 & 0xf;
1417 regs[dst_reg] = regs[src_reg];
1420 else if ((insn & 0xff7f) == 0xf85f) /* ldr.w Rt,<label> */
1422 /* Constant pool loads. */
1423 unsigned int constant;
1424 CORE_ADDR loc;
1426 offset = bits (inst2, 0, 11);
1427 if (insn & 0x0080)
1428 loc = start + 4 + offset;
1429 else
1430 loc = start + 4 - offset;
1432 constant = read_memory_unsigned_integer (loc, 4, byte_order);
1433 regs[bits (inst2, 12, 15)] = pv_constant (constant);
1436 else if ((insn & 0xff7f) == 0xe95f) /* ldrd Rt,Rt2,<label> */
1438 /* Constant pool loads. */
1439 unsigned int constant;
1440 CORE_ADDR loc;
1442 offset = bits (inst2, 0, 7) << 2;
1443 if (insn & 0x0080)
1444 loc = start + 4 + offset;
1445 else
1446 loc = start + 4 - offset;
1448 constant = read_memory_unsigned_integer (loc, 4, byte_order);
1449 regs[bits (inst2, 12, 15)] = pv_constant (constant);
1451 constant = read_memory_unsigned_integer (loc + 4, 4, byte_order);
1452 regs[bits (inst2, 8, 11)] = pv_constant (constant);
1454 /* Start of ARMv8.1-m PACBTI extension instructions. */
1455 else if (IS_PAC (whole_insn))
1457 /* LR and SP are input registers. PAC is in R12. LR is
1458 signed from this point onwards. NOP space. */
1459 ra_signed_state = true;
1461 else if (IS_PACBTI (whole_insn))
1463 /* LR and SP are input registers. PAC is in R12 and PC is a
1464 valid BTI landing pad. LR is signed from this point onwards.
1465 NOP space. */
1466 ra_signed_state = true;
1468 else if (IS_BTI (whole_insn))
1470 /* Valid BTI landing pad. NOP space. */
1472 else if (IS_PACG (whole_insn))
1474 /* Sign Rn using Rm and store the PAC in Rd. Rd is signed from
1475 this point onwards. */
1476 ra_signed_state = true;
1478 else if (IS_AUT (whole_insn) || IS_AUTG (whole_insn))
1480 /* These instructions appear close to the epilogue, when signed
1481 pointers are getting authenticated. */
1482 ra_signed_state = false;
1484 /* End of ARMv8.1-m PACBTI extension instructions */
1485 else if (thumb2_instruction_changes_pc (insn, inst2))
1487 /* Don't scan past anything that might change control flow. */
1488 break;
1490 else
1492 /* The optimizer might shove anything into the prologue,
1493 so we just skip what we don't recognize. */
1494 unrecognized_pc = start;
1497 /* Make sure we are dealing with a target that supports ARMv8.1-m
1498 PACBTI. */
1499 if (cache != nullptr && tdep->have_pacbti
1500 && ra_signed_state.has_value ())
1502 arm_debug_printf ("Found pacbti instruction at %s",
1503 paddress (gdbarch, start));
1504 arm_debug_printf ("RA is %s",
1505 *ra_signed_state ? "signed" : "not signed");
1506 cache->ra_signed_state = ra_signed_state;
1509 start += 2;
1511 else if (thumb_instruction_changes_pc (insn))
1513 /* Don't scan past anything that might change control flow. */
1514 break;
1516 else
1518 /* The optimizer might shove anything into the prologue,
1519 so we just skip what we don't recognize. */
1520 unrecognized_pc = start;
1523 start += 2;
1526 arm_debug_printf ("Prologue scan stopped at %s",
1527 paddress (gdbarch, start));
1529 if (unrecognized_pc == 0)
1530 unrecognized_pc = start;
1532 if (cache == NULL)
1533 return unrecognized_pc;
1535 if (pv_is_register (regs[ARM_FP_REGNUM], ARM_SP_REGNUM))
1537 /* Frame pointer is fp. Frame size is constant. */
1538 cache->framereg = ARM_FP_REGNUM;
1539 cache->framesize = -regs[ARM_FP_REGNUM].k;
1541 else if (pv_is_register (regs[THUMB_FP_REGNUM], ARM_SP_REGNUM))
1543 /* Frame pointer is r7. Frame size is constant. */
1544 cache->framereg = THUMB_FP_REGNUM;
1545 cache->framesize = -regs[THUMB_FP_REGNUM].k;
1547 else
1549 /* Try the stack pointer... this is a bit desperate. */
1550 cache->framereg = ARM_SP_REGNUM;
1551 cache->framesize = -regs[ARM_SP_REGNUM].k;
1554 for (i = 0; i < gdbarch_num_regs (gdbarch); i++)
1555 if (stack.find_reg (gdbarch, i, &offset))
1557 cache->saved_regs[i].set_addr (offset);
1558 if (i == ARM_SP_REGNUM)
1559 arm_cache_set_active_sp_value(cache, tdep, offset);
1562 return unrecognized_pc;
1566 /* Try to analyze the instructions starting from PC, which load symbol
1567 __stack_chk_guard. Return the address of instruction after loading this
1568 symbol, set the dest register number to *BASEREG, and set the size of
1569 instructions for loading symbol in OFFSET. Return 0 if instructions are
1570 not recognized. */
1572 static CORE_ADDR
1573 arm_analyze_load_stack_chk_guard(CORE_ADDR pc, struct gdbarch *gdbarch,
1574 unsigned int *destreg, int *offset)
1576 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
1577 int is_thumb = arm_pc_is_thumb (gdbarch, pc);
1578 unsigned int low, high, address;
1580 address = 0;
1581 if (is_thumb)
1583 unsigned short insn1
1584 = read_code_unsigned_integer (pc, 2, byte_order_for_code);
1586 if ((insn1 & 0xf800) == 0x4800) /* ldr Rd, #immed */
1588 *destreg = bits (insn1, 8, 10);
1589 *offset = 2;
1590 address = (pc & 0xfffffffc) + 4 + (bits (insn1, 0, 7) << 2);
1591 address = read_memory_unsigned_integer (address, 4,
1592 byte_order_for_code);
1594 else if ((insn1 & 0xfbf0) == 0xf240) /* movw Rd, #const */
1596 unsigned short insn2
1597 = read_code_unsigned_integer (pc + 2, 2, byte_order_for_code);
1599 low = EXTRACT_MOVW_MOVT_IMM_T (insn1, insn2);
1601 insn1
1602 = read_code_unsigned_integer (pc + 4, 2, byte_order_for_code);
1603 insn2
1604 = read_code_unsigned_integer (pc + 6, 2, byte_order_for_code);
1606 /* movt Rd, #const */
1607 if ((insn1 & 0xfbc0) == 0xf2c0)
1609 high = EXTRACT_MOVW_MOVT_IMM_T (insn1, insn2);
1610 *destreg = bits (insn2, 8, 11);
1611 *offset = 8;
1612 address = (high << 16 | low);
1616 else
1618 unsigned int insn
1619 = read_code_unsigned_integer (pc, 4, byte_order_for_code);
1621 if ((insn & 0x0e5f0000) == 0x041f0000) /* ldr Rd, [PC, #immed] */
1623 address = bits (insn, 0, 11) + pc + 8;
1624 address = read_memory_unsigned_integer (address, 4,
1625 byte_order_for_code);
1627 *destreg = bits (insn, 12, 15);
1628 *offset = 4;
1630 else if ((insn & 0x0ff00000) == 0x03000000) /* movw Rd, #const */
1632 low = EXTRACT_MOVW_MOVT_IMM_A (insn);
1634 insn
1635 = read_code_unsigned_integer (pc + 4, 4, byte_order_for_code);
1637 if ((insn & 0x0ff00000) == 0x03400000) /* movt Rd, #const */
1639 high = EXTRACT_MOVW_MOVT_IMM_A (insn);
1640 *destreg = bits (insn, 12, 15);
1641 *offset = 8;
1642 address = (high << 16 | low);
1647 return address;
1650 /* Try to skip a sequence of instructions used for stack protector. If PC
1651 points to the first instruction of this sequence, return the address of
1652 first instruction after this sequence, otherwise, return original PC.
1654 On arm, this sequence of instructions is composed of mainly three steps,
1655 Step 1: load symbol __stack_chk_guard,
1656 Step 2: load from address of __stack_chk_guard,
1657 Step 3: store it to somewhere else.
1659 Usually, instructions on step 2 and step 3 are the same on various ARM
1660 architectures. On step 2, it is one instruction 'ldr Rx, [Rn, #0]', and
1661 on step 3, it is also one instruction 'str Rx, [r7, #immd]'. However,
1662 instructions in step 1 vary from different ARM architectures. On ARMv7,
1663 they are,
1665 movw Rn, #:lower16:__stack_chk_guard
1666 movt Rn, #:upper16:__stack_chk_guard
1668 On ARMv5t, it is,
1670 ldr Rn, .Label
1671 ....
1672 .Lable:
1673 .word __stack_chk_guard
1675 Since ldr/str is a very popular instruction, we can't use them as
1676 'fingerprint' or 'signature' of stack protector sequence. Here we choose
1677 sequence {movw/movt, ldr}/ldr/str plus symbol __stack_chk_guard, if not
1678 stripped, as the 'fingerprint' of a stack protector cdoe sequence. */
1680 static CORE_ADDR
1681 arm_skip_stack_protector(CORE_ADDR pc, struct gdbarch *gdbarch)
1683 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
1684 unsigned int basereg;
1685 int offset;
1686 int is_thumb = arm_pc_is_thumb (gdbarch, pc);
1687 CORE_ADDR addr;
1689 /* Try to parse the instructions in Step 1. */
1690 addr = arm_analyze_load_stack_chk_guard (pc, gdbarch,
1691 &basereg, &offset);
1692 if (!addr)
1693 return pc;
1695 bound_minimal_symbol stack_chk_guard = lookup_minimal_symbol_by_pc (addr);
1696 /* ADDR must correspond to a symbol whose name is __stack_chk_guard.
1697 Otherwise, this sequence cannot be for stack protector. */
1698 if (stack_chk_guard.minsym == NULL
1699 || !startswith (stack_chk_guard.minsym->linkage_name (), "__stack_chk_guard"))
1700 return pc;
1702 if (is_thumb)
1704 unsigned int destreg;
1705 unsigned short insn
1706 = read_code_unsigned_integer (pc + offset, 2, byte_order_for_code);
1708 /* Step 2: ldr Rd, [Rn, #immed], encoding T1. */
1709 if ((insn & 0xf800) != 0x6800)
1710 return pc;
1711 if (bits (insn, 3, 5) != basereg)
1712 return pc;
1713 destreg = bits (insn, 0, 2);
1715 insn = read_code_unsigned_integer (pc + offset + 2, 2,
1716 byte_order_for_code);
1717 /* Step 3: str Rd, [Rn, #immed], encoding T1. */
1718 if ((insn & 0xf800) != 0x6000)
1719 return pc;
1720 if (destreg != bits (insn, 0, 2))
1721 return pc;
1723 else
1725 unsigned int destreg;
1726 unsigned int insn
1727 = read_code_unsigned_integer (pc + offset, 4, byte_order_for_code);
1729 /* Step 2: ldr Rd, [Rn, #immed], encoding A1. */
1730 if ((insn & 0x0e500000) != 0x04100000)
1731 return pc;
1732 if (bits (insn, 16, 19) != basereg)
1733 return pc;
1734 destreg = bits (insn, 12, 15);
1735 /* Step 3: str Rd, [Rn, #immed], encoding A1. */
1736 insn = read_code_unsigned_integer (pc + offset + 4,
1737 4, byte_order_for_code);
1738 if ((insn & 0x0e500000) != 0x04000000)
1739 return pc;
1740 if (bits (insn, 12, 15) != destreg)
1741 return pc;
1743 /* The size of total two instructions ldr/str is 4 on Thumb-2, while 8
1744 on arm. */
1745 if (is_thumb)
1746 return pc + offset + 4;
1747 else
1748 return pc + offset + 8;
1751 /* Advance the PC across any function entry prologue instructions to
1752 reach some "real" code.
1754 The APCS (ARM Procedure Call Standard) defines the following
1755 prologue:
1757 mov ip, sp
1758 [stmfd sp!, {a1,a2,a3,a4}]
1759 stmfd sp!, {...,fp,ip,lr,pc}
1760 [stfe f7, [sp, #-12]!]
1761 [stfe f6, [sp, #-12]!]
1762 [stfe f5, [sp, #-12]!]
1763 [stfe f4, [sp, #-12]!]
1764 sub fp, ip, #nn @@ nn == 20 or 4 depending on second insn. */
1766 static CORE_ADDR
1767 arm_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR pc)
1769 CORE_ADDR func_addr, func_end_addr, limit_pc;
1771 /* See if we can determine the end of the prologue via the symbol table.
1772 If so, then return either PC, or the PC after the prologue, whichever
1773 is greater. */
1774 bool func_addr_found
1775 = find_pc_partial_function (pc, NULL, &func_addr, &func_end_addr);
1777 /* Whether the function is thumb mode or not. */
1778 bool func_is_thumb = false;
1780 if (func_addr_found)
1782 CORE_ADDR post_prologue_pc
1783 = skip_prologue_using_sal (gdbarch, func_addr);
1784 struct compunit_symtab *cust = find_pc_compunit_symtab (func_addr);
1786 if (post_prologue_pc)
1787 post_prologue_pc
1788 = arm_skip_stack_protector (post_prologue_pc, gdbarch);
1791 /* GCC always emits a line note before the prologue and another
1792 one after, even if the two are at the same address or on the
1793 same line. Take advantage of this so that we do not need to
1794 know every instruction that might appear in the prologue. We
1795 will have producer information for most binaries; if it is
1796 missing (e.g. for -gstabs), assuming the GNU tools. */
1797 if (post_prologue_pc
1798 && (cust == NULL
1799 || cust->producer () == NULL
1800 || startswith (cust->producer (), "GNU ")
1801 || producer_is_llvm (cust->producer ())))
1802 return post_prologue_pc;
1804 if (post_prologue_pc != 0)
1806 CORE_ADDR analyzed_limit;
1808 /* For non-GCC compilers, make sure the entire line is an
1809 acceptable prologue; GDB will round this function's
1810 return value up to the end of the following line so we
1811 can not skip just part of a line (and we do not want to).
1813 RealView does not treat the prologue specially, but does
1814 associate prologue code with the opening brace; so this
1815 lets us skip the first line if we think it is the opening
1816 brace. */
1817 func_is_thumb = arm_pc_is_thumb (gdbarch, func_addr);
1818 if (func_is_thumb)
1819 analyzed_limit = thumb_analyze_prologue (gdbarch, func_addr,
1820 post_prologue_pc, NULL);
1821 else
1822 analyzed_limit
1823 = arm_analyze_prologue (gdbarch, func_addr, post_prologue_pc,
1824 NULL, target_arm_instruction_reader ());
1826 if (analyzed_limit != post_prologue_pc)
1827 return func_addr;
1829 return post_prologue_pc;
1833 /* Can't determine prologue from the symbol table, need to examine
1834 instructions. */
1836 /* Find an upper limit on the function prologue using the debug
1837 information. If the debug information could not be used to provide
1838 that bound, then use an arbitrary large number as the upper bound. */
1839 /* Like arm_scan_prologue, stop no later than pc + 64. */
1840 limit_pc = skip_prologue_using_sal (gdbarch, pc);
1841 if (limit_pc == 0)
1842 limit_pc = pc + 64; /* Magic. */
1844 /* Set the correct adjustment based on whether the function is thumb mode or
1845 not. We use it to get the address of the last instruction in the
1846 function (as opposed to the first address of the next function). */
1847 CORE_ADDR adjustment = func_is_thumb ? 2 : 4;
1849 limit_pc
1850 = func_end_addr == 0 ? limit_pc : std::min (limit_pc,
1851 func_end_addr - adjustment);
1853 /* Check if this is Thumb code. */
1854 if (arm_pc_is_thumb (gdbarch, pc))
1855 return thumb_analyze_prologue (gdbarch, pc, limit_pc, NULL);
1856 else
1857 return arm_analyze_prologue (gdbarch, pc, limit_pc, NULL,
1858 target_arm_instruction_reader ());
1861 /* Function: thumb_scan_prologue (helper function for arm_scan_prologue)
1862 This function decodes a Thumb function prologue to determine:
1863 1) the size of the stack frame
1864 2) which registers are saved on it
1865 3) the offsets of saved regs
1866 4) the offset from the stack pointer to the frame pointer
1868 A typical Thumb function prologue would create this stack frame
1869 (offsets relative to FP)
1870 old SP -> 24 stack parameters
1871 20 LR
1872 16 R7
1873 R7 -> 0 local variables (16 bytes)
1874 SP -> -12 additional stack space (12 bytes)
1875 The frame size would thus be 36 bytes, and the frame offset would be
1876 12 bytes. The frame register is R7.
1878 The comments for thumb_skip_prolog() describe the algorithm we use
1879 to detect the end of the prolog. */
1881 static void
1882 thumb_scan_prologue (struct gdbarch *gdbarch, CORE_ADDR prev_pc,
1883 CORE_ADDR block_addr, struct arm_prologue_cache *cache)
1885 CORE_ADDR prologue_start;
1886 CORE_ADDR prologue_end;
1888 if (find_pc_partial_function (block_addr, NULL, &prologue_start,
1889 &prologue_end))
1891 /* See comment in arm_scan_prologue for an explanation of
1892 this heuristics. */
1893 if (prologue_end > prologue_start + 64)
1895 prologue_end = prologue_start + 64;
1898 else
1899 /* We're in the boondocks: we have no idea where the start of the
1900 function is. */
1901 return;
1903 prologue_end = std::min (prologue_end, prev_pc);
1905 thumb_analyze_prologue (gdbarch, prologue_start, prologue_end, cache);
1908 /* Return 1 if the ARM instruction INSN restores SP in epilogue, 0
1909 otherwise. */
1911 static int
1912 arm_instruction_restores_sp (unsigned int insn)
1914 if (bits (insn, 28, 31) != INST_NV)
1916 if ((insn & 0x0df0f000) == 0x0080d000
1917 /* ADD SP (register or immediate). */
1918 || (insn & 0x0df0f000) == 0x0040d000
1919 /* SUB SP (register or immediate). */
1920 || (insn & 0x0ffffff0) == 0x01a0d000
1921 /* MOV SP. */
1922 || (insn & 0x0fff0000) == 0x08bd0000
1923 /* POP (LDMIA). */
1924 || (insn & 0x0fff0000) == 0x049d0000)
1925 /* POP of a single register. */
1926 return 1;
1929 return 0;
1932 /* Implement immediate value decoding, as described in section A5.2.4
1933 (Modified immediate constants in ARM instructions) of the ARM Architecture
1934 Reference Manual (ARMv7-A and ARMv7-R edition). */
1936 static uint32_t
1937 arm_expand_immediate (uint32_t imm)
1939 /* Immediate values are 12 bits long. */
1940 gdb_assert ((imm & 0xfffff000) == 0);
1942 uint32_t unrotated_value = imm & 0xff;
1943 uint32_t rotate_amount = (imm & 0xf00) >> 7;
1945 if (rotate_amount == 0)
1946 return unrotated_value;
1948 return ((unrotated_value >> rotate_amount)
1949 | (unrotated_value << (32 - rotate_amount)));
1952 /* Analyze an ARM mode prologue starting at PROLOGUE_START and
1953 continuing no further than PROLOGUE_END. If CACHE is non-NULL,
1954 fill it in. Return the first address not recognized as a prologue
1955 instruction.
1957 We recognize all the instructions typically found in ARM prologues,
1958 plus harmless instructions which can be skipped (either for analysis
1959 purposes, or a more restrictive set that can be skipped when finding
1960 the end of the prologue). */
1962 static CORE_ADDR
1963 arm_analyze_prologue (struct gdbarch *gdbarch,
1964 CORE_ADDR prologue_start, CORE_ADDR prologue_end,
1965 struct arm_prologue_cache *cache,
1966 const arm_instruction_reader &insn_reader)
1968 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
1969 int regno;
1970 CORE_ADDR offset, current_pc;
1971 pv_t regs[ARM_FPS_REGNUM];
1972 CORE_ADDR unrecognized_pc = 0;
1973 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
1975 /* Search the prologue looking for instructions that set up the
1976 frame pointer, adjust the stack pointer, and save registers.
1978 Be careful, however, and if it doesn't look like a prologue,
1979 don't try to scan it. If, for instance, a frameless function
1980 begins with stmfd sp!, then we will tell ourselves there is
1981 a frame, which will confuse stack traceback, as well as "finish"
1982 and other operations that rely on a knowledge of the stack
1983 traceback. */
1985 for (regno = 0; regno < ARM_FPS_REGNUM; regno++)
1986 regs[regno] = pv_register (regno, 0);
1987 pv_area stack (ARM_SP_REGNUM, gdbarch_addr_bit (gdbarch));
1989 for (current_pc = prologue_start;
1990 current_pc < prologue_end;
1991 current_pc += 4)
1993 uint32_t insn = insn_reader.read (current_pc, byte_order_for_code);
1995 if (insn == 0xe1a0c00d) /* mov ip, sp */
1997 regs[ARM_IP_REGNUM] = regs[ARM_SP_REGNUM];
1998 continue;
2000 else if ((insn & 0xfff00000) == 0xe2800000 /* add Rd, Rn, #n */
2001 && pv_is_register (regs[bits (insn, 16, 19)], ARM_SP_REGNUM))
2003 uint32_t imm = arm_expand_immediate (insn & 0xfff);
2004 int rd = bits (insn, 12, 15);
2005 regs[rd] = pv_add_constant (regs[bits (insn, 16, 19)], imm);
2006 continue;
2008 else if ((insn & 0xfff00000) == 0xe2400000 /* sub Rd, Rn, #n */
2009 && pv_is_register (regs[bits (insn, 16, 19)], ARM_SP_REGNUM))
2011 uint32_t imm = arm_expand_immediate (insn & 0xfff);
2012 int rd = bits (insn, 12, 15);
2013 regs[rd] = pv_add_constant (regs[bits (insn, 16, 19)], -imm);
2014 continue;
2016 else if ((insn & 0xffff0fff) == 0xe52d0004) /* str Rd,
2017 [sp, #-4]! */
2019 if (stack.store_would_trash (regs[ARM_SP_REGNUM]))
2020 break;
2021 regs[ARM_SP_REGNUM] = pv_add_constant (regs[ARM_SP_REGNUM], -4);
2022 stack.store (regs[ARM_SP_REGNUM], 4,
2023 regs[bits (insn, 12, 15)]);
2024 continue;
2026 else if ((insn & 0xffff0000) == 0xe92d0000)
2027 /* stmfd sp!, {..., fp, ip, lr, pc}
2029 stmfd sp!, {a1, a2, a3, a4} */
2031 int mask = insn & 0xffff;
2033 if (stack.store_would_trash (regs[ARM_SP_REGNUM]))
2034 break;
2036 /* Calculate offsets of saved registers. */
2037 for (regno = ARM_PC_REGNUM; regno >= 0; regno--)
2038 if (mask & (1 << regno))
2040 regs[ARM_SP_REGNUM]
2041 = pv_add_constant (regs[ARM_SP_REGNUM], -4);
2042 stack.store (regs[ARM_SP_REGNUM], 4, regs[regno]);
2045 else if ((insn & 0xffff0000) == 0xe54b0000 /* strb rx,[r11,#-n] */
2046 || (insn & 0xffff00f0) == 0xe14b00b0 /* strh rx,[r11,#-n] */
2047 || (insn & 0xffffc000) == 0xe50b0000) /* str rx,[r11,#-n] */
2049 /* No need to add this to saved_regs -- it's just an arg reg. */
2050 continue;
2052 else if ((insn & 0xffff0000) == 0xe5cd0000 /* strb rx,[sp,#n] */
2053 || (insn & 0xffff00f0) == 0xe1cd00b0 /* strh rx,[sp,#n] */
2054 || (insn & 0xffffc000) == 0xe58d0000) /* str rx,[sp,#n] */
2056 /* No need to add this to saved_regs -- it's just an arg reg. */
2057 continue;
2059 else if ((insn & 0xfff00000) == 0xe8800000 /* stm Rn,
2060 { registers } */
2061 && pv_is_register (regs[bits (insn, 16, 19)], ARM_SP_REGNUM))
2063 /* No need to add this to saved_regs -- it's just arg regs. */
2064 continue;
2066 else if ((insn & 0xfffff000) == 0xe24cb000) /* sub fp, ip #n */
2068 uint32_t imm = arm_expand_immediate (insn & 0xfff);
2069 regs[ARM_FP_REGNUM] = pv_add_constant (regs[ARM_IP_REGNUM], -imm);
2071 else if ((insn & 0xfffff000) == 0xe24dd000) /* sub sp, sp #n */
2073 uint32_t imm = arm_expand_immediate(insn & 0xfff);
2074 regs[ARM_SP_REGNUM] = pv_add_constant (regs[ARM_SP_REGNUM], -imm);
2076 else if ((insn & 0xffff7fff) == 0xed6d0103 /* stfe f?,
2077 [sp, -#c]! */
2078 && tdep->have_fpa_registers)
2080 if (stack.store_would_trash (regs[ARM_SP_REGNUM]))
2081 break;
2083 regs[ARM_SP_REGNUM] = pv_add_constant (regs[ARM_SP_REGNUM], -12);
2084 regno = ARM_F0_REGNUM + ((insn >> 12) & 0x07);
2085 stack.store (regs[ARM_SP_REGNUM], 12, regs[regno]);
2087 else if ((insn & 0xffbf0fff) == 0xec2d0200 /* sfmfd f0, 4,
2088 [sp!] */
2089 && tdep->have_fpa_registers)
2091 int n_saved_fp_regs;
2092 unsigned int fp_start_reg, fp_bound_reg;
2094 if (stack.store_would_trash (regs[ARM_SP_REGNUM]))
2095 break;
2097 if ((insn & 0x800) == 0x800) /* N0 is set */
2099 if ((insn & 0x40000) == 0x40000) /* N1 is set */
2100 n_saved_fp_regs = 3;
2101 else
2102 n_saved_fp_regs = 1;
2104 else
2106 if ((insn & 0x40000) == 0x40000) /* N1 is set */
2107 n_saved_fp_regs = 2;
2108 else
2109 n_saved_fp_regs = 4;
2112 fp_start_reg = ARM_F0_REGNUM + ((insn >> 12) & 0x7);
2113 fp_bound_reg = fp_start_reg + n_saved_fp_regs;
2114 for (; fp_start_reg < fp_bound_reg; fp_start_reg++)
2116 regs[ARM_SP_REGNUM] = pv_add_constant (regs[ARM_SP_REGNUM], -12);
2117 stack.store (regs[ARM_SP_REGNUM], 12,
2118 regs[fp_start_reg++]);
2121 else if ((insn & 0xff000000) == 0xeb000000 && cache == NULL) /* bl */
2123 /* Allow some special function calls when skipping the
2124 prologue; GCC generates these before storing arguments to
2125 the stack. */
2126 CORE_ADDR dest = BranchDest (current_pc, insn);
2128 if (skip_prologue_function (gdbarch, dest, 0))
2129 continue;
2130 else
2131 break;
2133 else if ((insn & 0xf0000000) != 0xe0000000)
2134 break; /* Condition not true, exit early. */
2135 else if (arm_instruction_changes_pc (insn))
2136 /* Don't scan past anything that might change control flow. */
2137 break;
2138 else if (arm_instruction_restores_sp (insn))
2140 /* Don't scan past the epilogue. */
2141 break;
2143 else if ((insn & 0xfe500000) == 0xe8100000 /* ldm */
2144 && pv_is_register (regs[bits (insn, 16, 19)], ARM_SP_REGNUM))
2145 /* Ignore block loads from the stack, potentially copying
2146 parameters from memory. */
2147 continue;
2148 else if ((insn & 0xfc500000) == 0xe4100000
2149 && pv_is_register (regs[bits (insn, 16, 19)], ARM_SP_REGNUM))
2150 /* Similarly ignore single loads from the stack. */
2151 continue;
2152 else if ((insn & 0xffff0ff0) == 0xe1a00000)
2153 /* MOV Rd, Rm. Skip register copies, i.e. saves to another
2154 register instead of the stack. */
2155 continue;
2156 else
2158 /* The optimizer might shove anything into the prologue, if
2159 we build up cache (cache != NULL) from scanning prologue,
2160 we just skip what we don't recognize and scan further to
2161 make cache as complete as possible. However, if we skip
2162 prologue, we'll stop immediately on unrecognized
2163 instruction. */
2164 unrecognized_pc = current_pc;
2165 if (cache != NULL)
2166 continue;
2167 else
2168 break;
2172 if (unrecognized_pc == 0)
2173 unrecognized_pc = current_pc;
2175 if (cache)
2177 int framereg, framesize;
2179 /* The frame size is just the distance from the frame register
2180 to the original stack pointer. */
2181 if (pv_is_register (regs[ARM_FP_REGNUM], ARM_SP_REGNUM))
2183 /* Frame pointer is fp. */
2184 framereg = ARM_FP_REGNUM;
2185 framesize = -regs[ARM_FP_REGNUM].k;
2187 else
2189 /* Try the stack pointer... this is a bit desperate. */
2190 framereg = ARM_SP_REGNUM;
2191 framesize = -regs[ARM_SP_REGNUM].k;
2194 cache->framereg = framereg;
2195 cache->framesize = framesize;
2197 for (regno = 0; regno < ARM_FPS_REGNUM; regno++)
2198 if (stack.find_reg (gdbarch, regno, &offset))
2200 cache->saved_regs[regno].set_addr (offset);
2201 if (regno == ARM_SP_REGNUM)
2202 arm_cache_set_active_sp_value(cache, tdep, offset);
2206 arm_debug_printf ("Prologue scan stopped at %s",
2207 paddress (gdbarch, unrecognized_pc));
2209 return unrecognized_pc;
2212 static void
2213 arm_scan_prologue (const frame_info_ptr &this_frame,
2214 struct arm_prologue_cache *cache)
2216 struct gdbarch *gdbarch = get_frame_arch (this_frame);
2217 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
2218 CORE_ADDR prologue_start, prologue_end;
2219 CORE_ADDR prev_pc = get_frame_pc (this_frame);
2220 CORE_ADDR block_addr = get_frame_address_in_block (this_frame);
2221 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
2223 /* Assume there is no frame until proven otherwise. */
2224 cache->framereg = ARM_SP_REGNUM;
2225 cache->framesize = 0;
2227 /* Check for Thumb prologue. */
2228 if (arm_frame_is_thumb (this_frame))
2230 thumb_scan_prologue (gdbarch, prev_pc, block_addr, cache);
2231 return;
2234 /* Find the function prologue. If we can't find the function in
2235 the symbol table, peek in the stack frame to find the PC. */
2236 if (find_pc_partial_function (block_addr, NULL, &prologue_start,
2237 &prologue_end))
2239 /* One way to find the end of the prologue (which works well
2240 for unoptimized code) is to do the following:
2242 struct symtab_and_line sal = find_pc_line (prologue_start, 0);
2244 if (sal.line == 0)
2245 prologue_end = prev_pc;
2246 else if (sal.end < prologue_end)
2247 prologue_end = sal.end;
2249 This mechanism is very accurate so long as the optimizer
2250 doesn't move any instructions from the function body into the
2251 prologue. If this happens, sal.end will be the last
2252 instruction in the first hunk of prologue code just before
2253 the first instruction that the scheduler has moved from
2254 the body to the prologue.
2256 In order to make sure that we scan all of the prologue
2257 instructions, we use a slightly less accurate mechanism which
2258 may scan more than necessary. To help compensate for this
2259 lack of accuracy, the prologue scanning loop below contains
2260 several clauses which'll cause the loop to terminate early if
2261 an implausible prologue instruction is encountered.
2263 The expression
2265 prologue_start + 64
2267 is a suitable endpoint since it accounts for the largest
2268 possible prologue plus up to five instructions inserted by
2269 the scheduler. */
2271 if (prologue_end > prologue_start + 64)
2273 prologue_end = prologue_start + 64; /* See above. */
2276 else
2278 /* We have no symbol information. Our only option is to assume this
2279 function has a standard stack frame and the normal frame register.
2280 Then, we can find the value of our frame pointer on entrance to
2281 the callee (or at the present moment if this is the innermost frame).
2282 The value stored there should be the address of the stmfd + 8. */
2283 CORE_ADDR frame_loc;
2284 ULONGEST return_value;
2286 /* AAPCS does not use a frame register, so we can abort here. */
2287 if (tdep->arm_abi == ARM_ABI_AAPCS)
2288 return;
2290 frame_loc = get_frame_register_unsigned (this_frame, ARM_FP_REGNUM);
2291 if (!safe_read_memory_unsigned_integer (frame_loc, 4, byte_order,
2292 &return_value))
2293 return;
2294 else
2296 prologue_start = gdbarch_addr_bits_remove
2297 (gdbarch, return_value) - 8;
2298 prologue_end = prologue_start + 64; /* See above. */
2302 if (prev_pc < prologue_end)
2303 prologue_end = prev_pc;
2305 arm_analyze_prologue (gdbarch, prologue_start, prologue_end, cache,
2306 target_arm_instruction_reader ());
2309 static struct arm_prologue_cache *
2310 arm_make_prologue_cache (const frame_info_ptr &this_frame)
2312 int reg;
2313 struct arm_prologue_cache *cache;
2314 CORE_ADDR unwound_fp, prev_sp;
2316 cache = FRAME_OBSTACK_ZALLOC (struct arm_prologue_cache);
2317 arm_cache_init (cache, this_frame);
2319 arm_scan_prologue (this_frame, cache);
2321 unwound_fp = get_frame_register_unsigned (this_frame, cache->framereg);
2322 if (unwound_fp == 0)
2323 return cache;
2325 arm_gdbarch_tdep *tdep =
2326 gdbarch_tdep<arm_gdbarch_tdep> (get_frame_arch (this_frame));
2328 prev_sp = unwound_fp + cache->framesize;
2329 arm_cache_set_active_sp_value (cache, tdep, prev_sp);
2331 /* Calculate actual addresses of saved registers using offsets
2332 determined by arm_scan_prologue. */
2333 for (reg = 0; reg < gdbarch_num_regs (get_frame_arch (this_frame)); reg++)
2334 if (cache->saved_regs[reg].is_addr ())
2335 cache->saved_regs[reg].set_addr (cache->saved_regs[reg].addr () +
2336 prev_sp);
2338 return cache;
2341 /* Implementation of the stop_reason hook for arm_prologue frames. */
2343 static enum unwind_stop_reason
2344 arm_prologue_unwind_stop_reason (const frame_info_ptr &this_frame,
2345 void **this_cache)
2347 struct arm_prologue_cache *cache;
2348 CORE_ADDR pc;
2350 if (*this_cache == NULL)
2351 *this_cache = arm_make_prologue_cache (this_frame);
2352 cache = (struct arm_prologue_cache *) *this_cache;
2354 /* This is meant to halt the backtrace at "_start". */
2355 pc = get_frame_pc (this_frame);
2356 gdbarch *arch = get_frame_arch (this_frame);
2357 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (arch);
2358 if (pc <= tdep->lowest_pc)
2359 return UNWIND_OUTERMOST;
2361 /* If we've hit a wall, stop. */
2362 if (arm_cache_get_prev_sp_value (cache, tdep) == 0)
2363 return UNWIND_OUTERMOST;
2365 return UNWIND_NO_REASON;
2368 /* Our frame ID for a normal frame is the current function's starting PC
2369 and the caller's SP when we were called. */
2371 static void
2372 arm_prologue_this_id (const frame_info_ptr &this_frame,
2373 void **this_cache,
2374 struct frame_id *this_id)
2376 struct arm_prologue_cache *cache;
2377 struct frame_id id;
2378 CORE_ADDR pc, func;
2380 if (*this_cache == NULL)
2381 *this_cache = arm_make_prologue_cache (this_frame);
2382 cache = (struct arm_prologue_cache *) *this_cache;
2384 arm_gdbarch_tdep *tdep
2385 = gdbarch_tdep<arm_gdbarch_tdep> (get_frame_arch (this_frame));
2387 /* Use function start address as part of the frame ID. If we cannot
2388 identify the start address (due to missing symbol information),
2389 fall back to just using the current PC. */
2390 pc = get_frame_pc (this_frame);
2391 func = get_frame_func (this_frame);
2392 if (!func)
2393 func = pc;
2395 id = frame_id_build (arm_cache_get_prev_sp_value (cache, tdep), func);
2396 *this_id = id;
2399 static struct value *
2400 arm_prologue_prev_register (const frame_info_ptr &this_frame,
2401 void **this_cache,
2402 int prev_regnum)
2404 struct gdbarch *gdbarch = get_frame_arch (this_frame);
2405 struct arm_prologue_cache *cache;
2406 CORE_ADDR sp_value;
2408 if (*this_cache == NULL)
2409 *this_cache = arm_make_prologue_cache (this_frame);
2410 cache = (struct arm_prologue_cache *) *this_cache;
2412 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
2414 /* If this frame has signed the return address, mark it as so. */
2415 if (tdep->have_pacbti && cache->ra_signed_state.has_value ()
2416 && *cache->ra_signed_state)
2417 set_frame_previous_pc_masked (this_frame);
2419 /* If we are asked to unwind the PC, then we need to return the LR
2420 instead. The prologue may save PC, but it will point into this
2421 frame's prologue, not the next frame's resume location. Also
2422 strip the saved T bit. A valid LR may have the low bit set, but
2423 a valid PC never does. */
2424 if (prev_regnum == ARM_PC_REGNUM)
2426 CORE_ADDR lr;
2428 lr = frame_unwind_register_unsigned (this_frame, ARM_LR_REGNUM);
2429 return frame_unwind_got_constant (this_frame, prev_regnum,
2430 arm_addr_bits_remove (gdbarch, lr));
2433 /* SP is generally not saved to the stack, but this frame is
2434 identified by the next frame's stack pointer at the time of the call.
2435 The value was already reconstructed into PREV_SP. */
2436 if (prev_regnum == ARM_SP_REGNUM)
2437 return frame_unwind_got_constant (this_frame, prev_regnum,
2438 arm_cache_get_prev_sp_value (cache, tdep));
2440 /* The value might be one of the alternative SP, if so, use the
2441 value already constructed. */
2442 if (arm_is_alternative_sp_register (tdep, prev_regnum))
2444 sp_value = arm_cache_get_sp_register (cache, tdep, prev_regnum);
2445 return frame_unwind_got_constant (this_frame, prev_regnum, sp_value);
2448 /* The CPSR may have been changed by the call instruction and by the
2449 called function. The only bit we can reconstruct is the T bit,
2450 by checking the low bit of LR as of the call. This is a reliable
2451 indicator of Thumb-ness except for some ARM v4T pre-interworking
2452 Thumb code, which could get away with a clear low bit as long as
2453 the called function did not use bx. Guess that all other
2454 bits are unchanged; the condition flags are presumably lost,
2455 but the processor status is likely valid. */
2456 if (prev_regnum == ARM_PS_REGNUM)
2458 ULONGEST cpsr = get_frame_register_unsigned (this_frame, prev_regnum);
2459 CORE_ADDR lr = frame_unwind_register_unsigned (this_frame, ARM_LR_REGNUM);
2461 cpsr = reconstruct_t_bit (gdbarch, lr, cpsr);
2462 return frame_unwind_got_constant (this_frame, prev_regnum, cpsr);
2465 return trad_frame_get_prev_register (this_frame, cache->saved_regs,
2466 prev_regnum);
2469 static frame_unwind arm_prologue_unwind = {
2470 "arm prologue",
2471 NORMAL_FRAME,
2472 arm_prologue_unwind_stop_reason,
2473 arm_prologue_this_id,
2474 arm_prologue_prev_register,
2475 NULL,
2476 default_frame_sniffer
2479 /* Maintain a list of ARM exception table entries per objfile, similar to the
2480 list of mapping symbols. We only cache entries for standard ARM-defined
2481 personality routines; the cache will contain only the frame unwinding
2482 instructions associated with the entry (not the descriptors). */
2484 struct arm_exidx_entry
2486 CORE_ADDR addr;
2487 gdb_byte *entry;
2489 bool operator< (const arm_exidx_entry &other) const
2491 return addr < other.addr;
2495 struct arm_exidx_data
2497 std::vector<std::vector<arm_exidx_entry>> section_maps;
2500 /* Per-BFD key to store exception handling information. */
2501 static const registry<bfd>::key<arm_exidx_data> arm_exidx_data_key;
2503 static struct obj_section *
2504 arm_obj_section_from_vma (struct objfile *objfile, bfd_vma vma)
2506 for (obj_section *osect : objfile->sections ())
2507 if (bfd_section_flags (osect->the_bfd_section) & SEC_ALLOC)
2509 bfd_vma start, size;
2510 start = bfd_section_vma (osect->the_bfd_section);
2511 size = bfd_section_size (osect->the_bfd_section);
2513 if (start <= vma && vma < start + size)
2514 return osect;
2517 return NULL;
2520 /* Parse contents of exception table and exception index sections
2521 of OBJFILE, and fill in the exception table entry cache.
2523 For each entry that refers to a standard ARM-defined personality
2524 routine, extract the frame unwinding instructions (from either
2525 the index or the table section). The unwinding instructions
2526 are normalized by:
2527 - extracting them from the rest of the table data
2528 - converting to host endianness
2529 - appending the implicit 0xb0 ("Finish") code
2531 The extracted and normalized instructions are stored for later
2532 retrieval by the arm_find_exidx_entry routine. */
2534 static void
2535 arm_exidx_new_objfile (struct objfile *objfile)
2537 struct arm_exidx_data *data;
2538 asection *exidx, *extab;
2539 bfd_vma exidx_vma = 0, extab_vma = 0;
2540 LONGEST i;
2542 /* If we've already touched this file, do nothing. */
2543 if (arm_exidx_data_key.get (objfile->obfd.get ()) != nullptr)
2544 return;
2546 /* Read contents of exception table and index. */
2547 exidx = bfd_get_section_by_name (objfile->obfd.get (),
2548 ELF_STRING_ARM_unwind);
2549 gdb::byte_vector exidx_data;
2550 if (exidx)
2552 exidx_vma = bfd_section_vma (exidx);
2553 exidx_data.resize (bfd_section_size (exidx));
2555 if (!bfd_get_section_contents (objfile->obfd.get (), exidx,
2556 exidx_data.data (), 0,
2557 exidx_data.size ()))
2558 return;
2561 extab = bfd_get_section_by_name (objfile->obfd.get (), ".ARM.extab");
2562 gdb::byte_vector extab_data;
2563 if (extab)
2565 extab_vma = bfd_section_vma (extab);
2566 extab_data.resize (bfd_section_size (extab));
2568 if (!bfd_get_section_contents (objfile->obfd.get (), extab,
2569 extab_data.data (), 0,
2570 extab_data.size ()))
2571 return;
2574 /* Allocate exception table data structure. */
2575 data = arm_exidx_data_key.emplace (objfile->obfd.get ());
2576 data->section_maps.resize (objfile->obfd->section_count);
2578 /* Fill in exception table. */
2579 for (i = 0; i < exidx_data.size () / 8; i++)
2581 struct arm_exidx_entry new_exidx_entry;
2582 bfd_vma idx = bfd_h_get_32 (objfile->obfd, exidx_data.data () + i * 8);
2583 bfd_vma val = bfd_h_get_32 (objfile->obfd,
2584 exidx_data.data () + i * 8 + 4);
2585 bfd_vma addr = 0, word = 0;
2586 int n_bytes = 0, n_words = 0;
2587 struct obj_section *sec;
2588 gdb_byte *entry = NULL;
2590 /* Extract address of start of function. */
2591 idx = ((idx & 0x7fffffff) ^ 0x40000000) - 0x40000000;
2592 idx += exidx_vma + i * 8;
2594 /* Find section containing function and compute section offset. */
2595 sec = arm_obj_section_from_vma (objfile, idx);
2596 if (sec == NULL)
2597 continue;
2598 idx -= bfd_section_vma (sec->the_bfd_section);
2600 /* Determine address of exception table entry. */
2601 if (val == 1)
2603 /* EXIDX_CANTUNWIND -- no exception table entry present. */
2605 else if ((val & 0xff000000) == 0x80000000)
2607 /* Exception table entry embedded in .ARM.exidx
2608 -- must be short form. */
2609 word = val;
2610 n_bytes = 3;
2612 else if (!(val & 0x80000000))
2614 /* Exception table entry in .ARM.extab. */
2615 addr = ((val & 0x7fffffff) ^ 0x40000000) - 0x40000000;
2616 addr += exidx_vma + i * 8 + 4;
2618 if (addr >= extab_vma && addr + 4 <= extab_vma + extab_data.size ())
2620 word = bfd_h_get_32 (objfile->obfd,
2621 extab_data.data () + addr - extab_vma);
2622 addr += 4;
2624 if ((word & 0xff000000) == 0x80000000)
2626 /* Short form. */
2627 n_bytes = 3;
2629 else if ((word & 0xff000000) == 0x81000000
2630 || (word & 0xff000000) == 0x82000000)
2632 /* Long form. */
2633 n_bytes = 2;
2634 n_words = ((word >> 16) & 0xff);
2636 else if (!(word & 0x80000000))
2638 bfd_vma pers;
2639 struct obj_section *pers_sec;
2640 int gnu_personality = 0;
2642 /* Custom personality routine. */
2643 pers = ((word & 0x7fffffff) ^ 0x40000000) - 0x40000000;
2644 pers = UNMAKE_THUMB_ADDR (pers + addr - 4);
2646 /* Check whether we've got one of the variants of the
2647 GNU personality routines. */
2648 pers_sec = arm_obj_section_from_vma (objfile, pers);
2649 if (pers_sec)
2651 static const char *personality[] =
2653 "__gcc_personality_v0",
2654 "__gxx_personality_v0",
2655 "__gcj_personality_v0",
2656 "__gnu_objc_personality_v0",
2657 NULL
2660 CORE_ADDR pc = pers + pers_sec->offset ();
2661 int k;
2663 for (k = 0; personality[k]; k++)
2664 if (lookup_minimal_symbol_by_pc_name
2665 (pc, personality[k], objfile))
2667 gnu_personality = 1;
2668 break;
2672 /* If so, the next word contains a word count in the high
2673 byte, followed by the same unwind instructions as the
2674 pre-defined forms. */
2675 if (gnu_personality
2676 && addr + 4 <= extab_vma + extab_data.size ())
2678 word = bfd_h_get_32 (objfile->obfd,
2679 (extab_data.data ()
2680 + addr - extab_vma));
2681 addr += 4;
2682 n_bytes = 3;
2683 n_words = ((word >> 24) & 0xff);
2689 /* Sanity check address. */
2690 if (n_words)
2691 if (addr < extab_vma
2692 || addr + 4 * n_words > extab_vma + extab_data.size ())
2693 n_words = n_bytes = 0;
2695 /* The unwind instructions reside in WORD (only the N_BYTES least
2696 significant bytes are valid), followed by N_WORDS words in the
2697 extab section starting at ADDR. */
2698 if (n_bytes || n_words)
2700 gdb_byte *p = entry
2701 = (gdb_byte *) obstack_alloc (&objfile->per_bfd->storage_obstack,
2702 n_bytes + n_words * 4 + 1);
2704 while (n_bytes--)
2705 *p++ = (gdb_byte) ((word >> (8 * n_bytes)) & 0xff);
2707 while (n_words--)
2709 word = bfd_h_get_32 (objfile->obfd,
2710 extab_data.data () + addr - extab_vma);
2711 addr += 4;
2713 *p++ = (gdb_byte) ((word >> 24) & 0xff);
2714 *p++ = (gdb_byte) ((word >> 16) & 0xff);
2715 *p++ = (gdb_byte) ((word >> 8) & 0xff);
2716 *p++ = (gdb_byte) (word & 0xff);
2719 /* Implied "Finish" to terminate the list. */
2720 *p++ = 0xb0;
2723 /* Push entry onto vector. They are guaranteed to always
2724 appear in order of increasing addresses. */
2725 new_exidx_entry.addr = idx;
2726 new_exidx_entry.entry = entry;
2727 data->section_maps[sec->the_bfd_section->index].push_back
2728 (new_exidx_entry);
2732 /* Search for the exception table entry covering MEMADDR. If one is found,
2733 return a pointer to its data. Otherwise, return 0. If START is non-NULL,
2734 set *START to the start of the region covered by this entry. */
2736 static gdb_byte *
2737 arm_find_exidx_entry (CORE_ADDR memaddr, CORE_ADDR *start)
2739 struct obj_section *sec;
2741 sec = find_pc_section (memaddr);
2742 if (sec != NULL)
2744 struct arm_exidx_data *data;
2745 struct arm_exidx_entry map_key = { memaddr - sec->addr (), 0 };
2747 data = arm_exidx_data_key.get (sec->objfile->obfd.get ());
2748 if (data != NULL)
2750 std::vector<arm_exidx_entry> &map
2751 = data->section_maps[sec->the_bfd_section->index];
2752 if (!map.empty ())
2754 auto idx = std::lower_bound (map.begin (), map.end (), map_key);
2756 /* std::lower_bound finds the earliest ordered insertion
2757 point. If the following symbol starts at this exact
2758 address, we use that; otherwise, the preceding
2759 exception table entry covers this address. */
2760 if (idx < map.end ())
2762 if (idx->addr == map_key.addr)
2764 if (start)
2765 *start = idx->addr + sec->addr ();
2766 return idx->entry;
2770 if (idx > map.begin ())
2772 idx = idx - 1;
2773 if (start)
2774 *start = idx->addr + sec->addr ();
2775 return idx->entry;
2781 return NULL;
2784 /* Given the current frame THIS_FRAME, and its associated frame unwinding
2785 instruction list from the ARM exception table entry ENTRY, allocate and
2786 return a prologue cache structure describing how to unwind this frame.
2788 Return NULL if the unwinding instruction list contains a "spare",
2789 "reserved" or "refuse to unwind" instruction as defined in section
2790 "9.3 Frame unwinding instructions" of the "Exception Handling ABI
2791 for the ARM Architecture" document. */
2793 static struct arm_prologue_cache *
2794 arm_exidx_fill_cache (const frame_info_ptr &this_frame, gdb_byte *entry)
2796 CORE_ADDR vsp = 0;
2797 int vsp_valid = 0;
2799 struct arm_prologue_cache *cache;
2800 cache = FRAME_OBSTACK_ZALLOC (struct arm_prologue_cache);
2801 arm_cache_init (cache, this_frame);
2803 for (;;)
2805 gdb_byte insn;
2807 /* Whenever we reload SP, we actually have to retrieve its
2808 actual value in the current frame. */
2809 if (!vsp_valid)
2811 if (cache->saved_regs[ARM_SP_REGNUM].is_realreg ())
2813 int reg = cache->saved_regs[ARM_SP_REGNUM].realreg ();
2814 vsp = get_frame_register_unsigned (this_frame, reg);
2816 else
2818 CORE_ADDR addr = cache->saved_regs[ARM_SP_REGNUM].addr ();
2819 vsp = get_frame_memory_unsigned (this_frame, addr, 4);
2822 vsp_valid = 1;
2825 /* Decode next unwind instruction. */
2826 insn = *entry++;
2828 if ((insn & 0xc0) == 0)
2830 int offset = insn & 0x3f;
2831 vsp += (offset << 2) + 4;
2833 else if ((insn & 0xc0) == 0x40)
2835 int offset = insn & 0x3f;
2836 vsp -= (offset << 2) + 4;
2838 else if ((insn & 0xf0) == 0x80)
2840 int mask = ((insn & 0xf) << 8) | *entry++;
2841 int i;
2843 /* The special case of an all-zero mask identifies
2844 "Refuse to unwind". We return NULL to fall back
2845 to the prologue analyzer. */
2846 if (mask == 0)
2847 return NULL;
2849 /* Pop registers r4..r15 under mask. */
2850 for (i = 0; i < 12; i++)
2851 if (mask & (1 << i))
2853 cache->saved_regs[4 + i].set_addr (vsp);
2854 vsp += 4;
2857 /* Special-case popping SP -- we need to reload vsp. */
2858 if (mask & (1 << (ARM_SP_REGNUM - 4)))
2859 vsp_valid = 0;
2861 else if ((insn & 0xf0) == 0x90)
2863 int reg = insn & 0xf;
2865 /* Reserved cases. */
2866 if (reg == ARM_SP_REGNUM || reg == ARM_PC_REGNUM)
2867 return NULL;
2869 /* Set SP from another register and mark VSP for reload. */
2870 cache->saved_regs[ARM_SP_REGNUM] = cache->saved_regs[reg];
2871 vsp_valid = 0;
2873 else if ((insn & 0xf0) == 0xa0)
2875 int count = insn & 0x7;
2876 int pop_lr = (insn & 0x8) != 0;
2877 int i;
2879 /* Pop r4..r[4+count]. */
2880 for (i = 0; i <= count; i++)
2882 cache->saved_regs[4 + i].set_addr (vsp);
2883 vsp += 4;
2886 /* If indicated by flag, pop LR as well. */
2887 if (pop_lr)
2889 cache->saved_regs[ARM_LR_REGNUM].set_addr (vsp);
2890 vsp += 4;
2893 else if (insn == 0xb0)
2895 /* We could only have updated PC by popping into it; if so, it
2896 will show up as address. Otherwise, copy LR into PC. */
2897 if (!cache->saved_regs[ARM_PC_REGNUM].is_addr ())
2898 cache->saved_regs[ARM_PC_REGNUM]
2899 = cache->saved_regs[ARM_LR_REGNUM];
2901 /* We're done. */
2902 break;
2904 else if (insn == 0xb1)
2906 int mask = *entry++;
2907 int i;
2909 /* All-zero mask and mask >= 16 is "spare". */
2910 if (mask == 0 || mask >= 16)
2911 return NULL;
2913 /* Pop r0..r3 under mask. */
2914 for (i = 0; i < 4; i++)
2915 if (mask & (1 << i))
2917 cache->saved_regs[i].set_addr (vsp);
2918 vsp += 4;
2921 else if (insn == 0xb2)
2923 ULONGEST offset = 0;
2924 unsigned shift = 0;
2928 offset |= (*entry & 0x7f) << shift;
2929 shift += 7;
2931 while (*entry++ & 0x80);
2933 vsp += 0x204 + (offset << 2);
2935 else if (insn == 0xb3)
2937 int start = *entry >> 4;
2938 int count = (*entry++) & 0xf;
2939 int i;
2941 /* Only registers D0..D15 are valid here. */
2942 if (start + count >= 16)
2943 return NULL;
2945 /* Pop VFP double-precision registers D[start]..D[start+count]. */
2946 for (i = 0; i <= count; i++)
2948 cache->saved_regs[ARM_D0_REGNUM + start + i].set_addr (vsp);
2949 vsp += 8;
2952 /* Add an extra 4 bytes for FSTMFDX-style stack. */
2953 vsp += 4;
2955 else if ((insn & 0xf8) == 0xb8)
2957 int count = insn & 0x7;
2958 int i;
2960 /* Pop VFP double-precision registers D[8]..D[8+count]. */
2961 for (i = 0; i <= count; i++)
2963 cache->saved_regs[ARM_D0_REGNUM + 8 + i].set_addr (vsp);
2964 vsp += 8;
2967 /* Add an extra 4 bytes for FSTMFDX-style stack. */
2968 vsp += 4;
2970 else if (insn == 0xc6)
2972 int start = *entry >> 4;
2973 int count = (*entry++) & 0xf;
2974 int i;
2976 /* Only registers WR0..WR15 are valid. */
2977 if (start + count >= 16)
2978 return NULL;
2980 /* Pop iwmmx registers WR[start]..WR[start+count]. */
2981 for (i = 0; i <= count; i++)
2983 cache->saved_regs[ARM_WR0_REGNUM + start + i].set_addr (vsp);
2984 vsp += 8;
2987 else if (insn == 0xc7)
2989 int mask = *entry++;
2990 int i;
2992 /* All-zero mask and mask >= 16 is "spare". */
2993 if (mask == 0 || mask >= 16)
2994 return NULL;
2996 /* Pop iwmmx general-purpose registers WCGR0..WCGR3 under mask. */
2997 for (i = 0; i < 4; i++)
2998 if (mask & (1 << i))
3000 cache->saved_regs[ARM_WCGR0_REGNUM + i].set_addr (vsp);
3001 vsp += 4;
3004 else if ((insn & 0xf8) == 0xc0)
3006 int count = insn & 0x7;
3007 int i;
3009 /* Pop iwmmx registers WR[10]..WR[10+count]. */
3010 for (i = 0; i <= count; i++)
3012 cache->saved_regs[ARM_WR0_REGNUM + 10 + i].set_addr (vsp);
3013 vsp += 8;
3016 else if (insn == 0xc8)
3018 int start = *entry >> 4;
3019 int count = (*entry++) & 0xf;
3020 int i;
3022 /* Only registers D0..D31 are valid. */
3023 if (start + count >= 16)
3024 return NULL;
3026 /* Pop VFP double-precision registers
3027 D[16+start]..D[16+start+count]. */
3028 for (i = 0; i <= count; i++)
3030 cache->saved_regs[ARM_D0_REGNUM + 16 + start + i].set_addr (vsp);
3031 vsp += 8;
3034 else if (insn == 0xc9)
3036 int start = *entry >> 4;
3037 int count = (*entry++) & 0xf;
3038 int i;
3040 /* Pop VFP double-precision registers D[start]..D[start+count]. */
3041 for (i = 0; i <= count; i++)
3043 cache->saved_regs[ARM_D0_REGNUM + start + i].set_addr (vsp);
3044 vsp += 8;
3047 else if ((insn & 0xf8) == 0xd0)
3049 int count = insn & 0x7;
3050 int i;
3052 /* Pop VFP double-precision registers D[8]..D[8+count]. */
3053 for (i = 0; i <= count; i++)
3055 cache->saved_regs[ARM_D0_REGNUM + 8 + i].set_addr (vsp);
3056 vsp += 8;
3059 else
3061 /* Everything else is "spare". */
3062 return NULL;
3066 /* If we restore SP from a register, assume this was the frame register.
3067 Otherwise just fall back to SP as frame register. */
3068 if (cache->saved_regs[ARM_SP_REGNUM].is_realreg ())
3069 cache->framereg = cache->saved_regs[ARM_SP_REGNUM].realreg ();
3070 else
3071 cache->framereg = ARM_SP_REGNUM;
3073 /* Determine offset to previous frame. */
3074 cache->framesize
3075 = vsp - get_frame_register_unsigned (this_frame, cache->framereg);
3077 /* We already got the previous SP. */
3078 arm_gdbarch_tdep *tdep
3079 = gdbarch_tdep<arm_gdbarch_tdep> (get_frame_arch (this_frame));
3080 arm_cache_set_active_sp_value (cache, tdep, vsp);
3082 return cache;
3085 /* Unwinding via ARM exception table entries. Note that the sniffer
3086 already computes a filled-in prologue cache, which is then used
3087 with the same arm_prologue_this_id and arm_prologue_prev_register
3088 routines also used for prologue-parsing based unwinding. */
3090 static int
3091 arm_exidx_unwind_sniffer (const struct frame_unwind *self,
3092 const frame_info_ptr &this_frame,
3093 void **this_prologue_cache)
3095 struct gdbarch *gdbarch = get_frame_arch (this_frame);
3096 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
3097 CORE_ADDR addr_in_block, exidx_region, func_start;
3098 struct arm_prologue_cache *cache;
3099 gdb_byte *entry;
3101 /* See if we have an ARM exception table entry covering this address. */
3102 addr_in_block = get_frame_address_in_block (this_frame);
3103 entry = arm_find_exidx_entry (addr_in_block, &exidx_region);
3104 if (!entry)
3105 return 0;
3107 /* The ARM exception table does not describe unwind information
3108 for arbitrary PC values, but is guaranteed to be correct only
3109 at call sites. We have to decide here whether we want to use
3110 ARM exception table information for this frame, or fall back
3111 to using prologue parsing. (Note that if we have DWARF CFI,
3112 this sniffer isn't even called -- CFI is always preferred.)
3114 Before we make this decision, however, we check whether we
3115 actually have *symbol* information for the current frame.
3116 If not, prologue parsing would not work anyway, so we might
3117 as well use the exception table and hope for the best. */
3118 if (find_pc_partial_function (addr_in_block, NULL, &func_start, NULL))
3120 int exc_valid = 0;
3122 /* If the next frame is "normal", we are at a call site in this
3123 frame, so exception information is guaranteed to be valid. */
3124 if (get_next_frame (this_frame)
3125 && get_frame_type (get_next_frame (this_frame)) == NORMAL_FRAME)
3126 exc_valid = 1;
3128 /* Some syscalls keep PC pointing to the SVC instruction itself. */
3129 for (int shift = 0; shift <= 1 && !exc_valid; ++shift)
3131 /* We also assume exception information is valid if we're currently
3132 blocked in a system call. The system library is supposed to
3133 ensure this, so that e.g. pthread cancellation works. */
3134 if (arm_frame_is_thumb (this_frame))
3136 ULONGEST insn;
3138 if (safe_read_memory_unsigned_integer ((get_frame_pc (this_frame)
3139 - (shift ? 2 : 0)),
3140 2, byte_order_for_code,
3141 &insn)
3142 && (insn & 0xff00) == 0xdf00 /* svc */)
3143 exc_valid = 1;
3145 else
3147 ULONGEST insn;
3149 if (safe_read_memory_unsigned_integer ((get_frame_pc (this_frame)
3150 - (shift ? 4 : 0)),
3151 4, byte_order_for_code,
3152 &insn)
3153 && (insn & 0x0f000000) == 0x0f000000 /* svc */)
3154 exc_valid = 1;
3158 /* Bail out if we don't know that exception information is valid. */
3159 if (!exc_valid)
3160 return 0;
3162 /* The ARM exception index does not mark the *end* of the region
3163 covered by the entry, and some functions will not have any entry.
3164 To correctly recognize the end of the covered region, the linker
3165 should have inserted dummy records with a CANTUNWIND marker.
3167 Unfortunately, current versions of GNU ld do not reliably do
3168 this, and thus we may have found an incorrect entry above.
3169 As a (temporary) sanity check, we only use the entry if it
3170 lies *within* the bounds of the function. Note that this check
3171 might reject perfectly valid entries that just happen to cover
3172 multiple functions; therefore this check ought to be removed
3173 once the linker is fixed. */
3174 if (func_start > exidx_region)
3175 return 0;
3178 /* Decode the list of unwinding instructions into a prologue cache.
3179 Note that this may fail due to e.g. a "refuse to unwind" code. */
3180 cache = arm_exidx_fill_cache (this_frame, entry);
3181 if (!cache)
3182 return 0;
3184 *this_prologue_cache = cache;
3185 return 1;
3188 struct frame_unwind arm_exidx_unwind = {
3189 "arm exidx",
3190 NORMAL_FRAME,
3191 default_frame_unwind_stop_reason,
3192 arm_prologue_this_id,
3193 arm_prologue_prev_register,
3194 NULL,
3195 arm_exidx_unwind_sniffer
3198 static struct arm_prologue_cache *
3199 arm_make_epilogue_frame_cache (const frame_info_ptr &this_frame)
3201 struct arm_prologue_cache *cache;
3202 int reg;
3204 cache = FRAME_OBSTACK_ZALLOC (struct arm_prologue_cache);
3205 arm_cache_init (cache, this_frame);
3207 /* Still rely on the offset calculated from prologue. */
3208 arm_scan_prologue (this_frame, cache);
3210 /* Since we are in epilogue, the SP has been restored. */
3211 arm_gdbarch_tdep *tdep
3212 = gdbarch_tdep<arm_gdbarch_tdep> (get_frame_arch (this_frame));
3213 arm_cache_set_active_sp_value (cache, tdep,
3214 get_frame_register_unsigned (this_frame,
3215 ARM_SP_REGNUM));
3217 /* Calculate actual addresses of saved registers using offsets
3218 determined by arm_scan_prologue. */
3219 for (reg = 0; reg < gdbarch_num_regs (get_frame_arch (this_frame)); reg++)
3220 if (cache->saved_regs[reg].is_addr ())
3221 cache->saved_regs[reg].set_addr (cache->saved_regs[reg].addr ()
3222 + arm_cache_get_prev_sp_value (cache, tdep));
3224 return cache;
3227 /* Implementation of function hook 'this_id' in
3228 'struct frame_uwnind' for epilogue unwinder. */
3230 static void
3231 arm_epilogue_frame_this_id (const frame_info_ptr &this_frame,
3232 void **this_cache,
3233 struct frame_id *this_id)
3235 struct arm_prologue_cache *cache;
3236 CORE_ADDR pc, func;
3238 if (*this_cache == NULL)
3239 *this_cache = arm_make_epilogue_frame_cache (this_frame);
3240 cache = (struct arm_prologue_cache *) *this_cache;
3242 /* Use function start address as part of the frame ID. If we cannot
3243 identify the start address (due to missing symbol information),
3244 fall back to just using the current PC. */
3245 pc = get_frame_pc (this_frame);
3246 func = get_frame_func (this_frame);
3247 if (func == 0)
3248 func = pc;
3250 arm_gdbarch_tdep *tdep
3251 = gdbarch_tdep<arm_gdbarch_tdep> (get_frame_arch (this_frame));
3252 *this_id = frame_id_build (arm_cache_get_prev_sp_value (cache, tdep), func);
3255 /* Implementation of function hook 'prev_register' in
3256 'struct frame_uwnind' for epilogue unwinder. */
3258 static struct value *
3259 arm_epilogue_frame_prev_register (const frame_info_ptr &this_frame,
3260 void **this_cache, int regnum)
3262 if (*this_cache == NULL)
3263 *this_cache = arm_make_epilogue_frame_cache (this_frame);
3265 return arm_prologue_prev_register (this_frame, this_cache, regnum);
3268 static int arm_stack_frame_destroyed_p_1 (struct gdbarch *gdbarch,
3269 CORE_ADDR pc);
3270 static int thumb_stack_frame_destroyed_p (struct gdbarch *gdbarch,
3271 CORE_ADDR pc);
3273 /* Implementation of function hook 'sniffer' in
3274 'struct frame_uwnind' for epilogue unwinder. */
3276 static int
3277 arm_epilogue_frame_sniffer (const struct frame_unwind *self,
3278 const frame_info_ptr &this_frame,
3279 void **this_prologue_cache)
3281 if (frame_relative_level (this_frame) == 0)
3283 struct gdbarch *gdbarch = get_frame_arch (this_frame);
3284 CORE_ADDR pc = get_frame_pc (this_frame);
3286 if (arm_frame_is_thumb (this_frame))
3287 return thumb_stack_frame_destroyed_p (gdbarch, pc);
3288 else
3289 return arm_stack_frame_destroyed_p_1 (gdbarch, pc);
3291 else
3292 return 0;
3295 /* Frame unwinder from epilogue. */
3297 static const struct frame_unwind arm_epilogue_frame_unwind =
3299 "arm epilogue",
3300 NORMAL_FRAME,
3301 default_frame_unwind_stop_reason,
3302 arm_epilogue_frame_this_id,
3303 arm_epilogue_frame_prev_register,
3304 NULL,
3305 arm_epilogue_frame_sniffer,
3308 /* Recognize GCC's trampoline for thumb call-indirect. If we are in a
3309 trampoline, return the target PC. Otherwise return 0.
3311 void call0a (char c, short s, int i, long l) {}
3313 int main (void)
3315 (*pointer_to_call0a) (c, s, i, l);
3318 Instead of calling a stub library function _call_via_xx (xx is
3319 the register name), GCC may inline the trampoline in the object
3320 file as below (register r2 has the address of call0a).
3322 .global main
3323 .type main, %function
3325 bl .L1
3327 .size main, .-main
3329 .L1:
3330 bx r2
3332 The trampoline 'bx r2' doesn't belong to main. */
3334 static CORE_ADDR
3335 arm_skip_bx_reg (const frame_info_ptr &frame, CORE_ADDR pc)
3337 /* The heuristics of recognizing such trampoline is that FRAME is
3338 executing in Thumb mode and the instruction on PC is 'bx Rm'. */
3339 if (arm_frame_is_thumb (frame))
3341 gdb_byte buf[2];
3343 if (target_read_memory (pc, buf, 2) == 0)
3345 struct gdbarch *gdbarch = get_frame_arch (frame);
3346 enum bfd_endian byte_order_for_code
3347 = gdbarch_byte_order_for_code (gdbarch);
3348 uint16_t insn
3349 = extract_unsigned_integer (buf, 2, byte_order_for_code);
3351 if ((insn & 0xff80) == 0x4700) /* bx <Rm> */
3353 CORE_ADDR dest
3354 = get_frame_register_unsigned (frame, bits (insn, 3, 6));
3356 /* Clear the LSB so that gdb core sets step-resume
3357 breakpoint at the right address. */
3358 return UNMAKE_THUMB_ADDR (dest);
3363 return 0;
3366 static struct arm_prologue_cache *
3367 arm_make_stub_cache (const frame_info_ptr &this_frame)
3369 struct arm_prologue_cache *cache;
3371 cache = FRAME_OBSTACK_ZALLOC (struct arm_prologue_cache);
3372 arm_cache_init (cache, this_frame);
3374 arm_gdbarch_tdep *tdep
3375 = gdbarch_tdep<arm_gdbarch_tdep> (get_frame_arch (this_frame));
3376 arm_cache_set_active_sp_value (cache, tdep,
3377 get_frame_register_unsigned (this_frame,
3378 ARM_SP_REGNUM));
3380 return cache;
3383 /* Our frame ID for a stub frame is the current SP and LR. */
3385 static void
3386 arm_stub_this_id (const frame_info_ptr &this_frame,
3387 void **this_cache,
3388 struct frame_id *this_id)
3390 struct arm_prologue_cache *cache;
3392 if (*this_cache == NULL)
3393 *this_cache = arm_make_stub_cache (this_frame);
3394 cache = (struct arm_prologue_cache *) *this_cache;
3396 arm_gdbarch_tdep *tdep
3397 = gdbarch_tdep<arm_gdbarch_tdep> (get_frame_arch (this_frame));
3398 *this_id = frame_id_build (arm_cache_get_prev_sp_value (cache, tdep),
3399 get_frame_pc (this_frame));
3402 static int
3403 arm_stub_unwind_sniffer (const struct frame_unwind *self,
3404 const frame_info_ptr &this_frame,
3405 void **this_prologue_cache)
3407 CORE_ADDR addr_in_block;
3408 gdb_byte dummy[4];
3409 CORE_ADDR pc, start_addr;
3410 const char *name;
3412 addr_in_block = get_frame_address_in_block (this_frame);
3413 pc = get_frame_pc (this_frame);
3414 if (in_plt_section (addr_in_block)
3415 /* We also use the stub winder if the target memory is unreadable
3416 to avoid having the prologue unwinder trying to read it. */
3417 || target_read_memory (pc, dummy, 4) != 0)
3418 return 1;
3420 if (find_pc_partial_function (pc, &name, &start_addr, NULL) == 0
3421 && arm_skip_bx_reg (this_frame, pc) != 0)
3422 return 1;
3424 return 0;
3427 struct frame_unwind arm_stub_unwind = {
3428 "arm stub",
3429 NORMAL_FRAME,
3430 default_frame_unwind_stop_reason,
3431 arm_stub_this_id,
3432 arm_prologue_prev_register,
3433 NULL,
3434 arm_stub_unwind_sniffer
3437 /* Put here the code to store, into CACHE->saved_regs, the addresses
3438 of the saved registers of frame described by THIS_FRAME. CACHE is
3439 returned. */
3441 static struct arm_prologue_cache *
3442 arm_m_exception_cache (const frame_info_ptr &this_frame)
3444 struct gdbarch *gdbarch = get_frame_arch (this_frame);
3445 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
3446 struct arm_prologue_cache *cache;
3448 cache = FRAME_OBSTACK_ZALLOC (struct arm_prologue_cache);
3449 arm_cache_init (cache, this_frame);
3451 /* ARMv7-M Architecture Reference "B1.5.6 Exception entry behavior"
3452 describes which bits in LR that define which stack was used prior
3453 to the exception and if FPU is used (causing extended stack frame). */
3455 /* In the lockup state PC contains a lockup magic value.
3456 The PC value of the the next outer frame is irreversibly
3457 lost. The other registers are intact so LR likely contains
3458 PC of some frame next to the outer one, but we cannot analyze
3459 the next outer frame without knowing its PC
3460 therefore we do not know SP fixup for this frame.
3461 Some heuristics to resynchronize SP might be possible.
3462 For simplicity, just terminate the unwinding to prevent it going
3463 astray and attempting to read data/addresses it shouldn't,
3464 which may cause further issues due to side-effects. */
3465 CORE_ADDR pc = get_frame_pc (this_frame);
3466 if (arm_m_addr_is_lockup (pc))
3468 /* The lockup can be real just in the innermost frame
3469 as the CPU is stopped and cannot create more frames.
3470 If we hit lockup magic PC in the other frame, it is
3471 just a sentinel at the top of stack: do not warn then. */
3472 if (frame_relative_level (this_frame) == 0)
3473 warning (_("ARM M in lockup state, stack unwinding terminated."));
3475 /* Terminate any further stack unwinding. */
3476 arm_cache_set_active_sp_value (cache, tdep, 0);
3477 return cache;
3480 CORE_ADDR lr = get_frame_register_unsigned (this_frame, ARM_LR_REGNUM);
3482 /* ARMv7-M Architecture Reference "A2.3.1 Arm core registers"
3483 states that LR is set to 0xffffffff on reset. ARMv8-M Architecture
3484 Reference "B3.3 Registers" states that LR is set to 0xffffffff on warm
3485 reset if Main Extension is implemented, otherwise the value is unknown. */
3486 if (lr == 0xffffffff)
3488 /* Terminate any further stack unwinding. */
3489 arm_cache_set_active_sp_value (cache, tdep, 0);
3490 return cache;
3493 /* Check FNC_RETURN indicator bits (24-31). */
3494 bool fnc_return = (((lr >> 24) & 0xff) == 0xfe);
3495 if (fnc_return)
3497 /* FNC_RETURN is only valid for targets with Security Extension. */
3498 if (!tdep->have_sec_ext)
3500 error (_("While unwinding an exception frame, found unexpected Link "
3501 "Register value %s that requires the security extension, "
3502 "but the extension was not found or is disabled. This "
3503 "should not happen and may be caused by corrupt data or a "
3504 "bug in GDB."), phex (lr, ARM_INT_REGISTER_SIZE));
3507 if (!arm_unwind_secure_frames)
3509 warning (_("Non-secure to secure stack unwinding disabled."));
3511 /* Terminate any further stack unwinding. */
3512 arm_cache_set_active_sp_value (cache, tdep, 0);
3513 return cache;
3516 ULONGEST xpsr = get_frame_register_unsigned (this_frame, ARM_PS_REGNUM);
3517 if ((xpsr & 0x1ff) != 0)
3518 /* Handler mode: This is the mode that exceptions are handled in. */
3519 arm_cache_switch_prev_sp (cache, tdep, tdep->m_profile_msp_s_regnum);
3520 else
3521 /* Thread mode: This is the normal mode that programs run in. */
3522 arm_cache_switch_prev_sp (cache, tdep, tdep->m_profile_psp_s_regnum);
3524 CORE_ADDR unwound_sp = arm_cache_get_prev_sp_value (cache, tdep);
3526 /* Stack layout for a function call from Secure to Non-Secure state
3527 (ARMv8-M section B3.16):
3529 SP Offset
3531 +-------------------+
3532 0x08 | |
3533 +-------------------+ <-- Original SP
3534 0x04 | Partial xPSR |
3535 +-------------------+
3536 0x00 | Return Address |
3537 +===================+ <-- New SP */
3539 cache->saved_regs[ARM_PC_REGNUM].set_addr (unwound_sp + 0x00);
3540 cache->saved_regs[ARM_LR_REGNUM].set_addr (unwound_sp + 0x00);
3541 cache->saved_regs[ARM_PS_REGNUM].set_addr (unwound_sp + 0x04);
3543 arm_cache_set_active_sp_value (cache, tdep, unwound_sp + 0x08);
3545 return cache;
3548 /* Check EXC_RETURN indicator bits (24-31). */
3549 bool exc_return = (((lr >> 24) & 0xff) == 0xff);
3550 if (exc_return)
3552 int sp_regnum;
3553 bool secure_stack_used = false;
3554 bool default_callee_register_stacking = false;
3555 bool exception_domain_is_secure = false;
3556 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
3558 /* Check EXC_RETURN bit SPSEL if Main or Thread (process) stack used. */
3559 bool process_stack_used = (bit (lr, 2) != 0);
3561 if (tdep->have_sec_ext)
3563 secure_stack_used = (bit (lr, 6) != 0);
3564 default_callee_register_stacking = (bit (lr, 5) != 0);
3565 exception_domain_is_secure = (bit (lr, 0) != 0);
3567 /* Unwinding from non-secure to secure can trip security
3568 measures. In order to avoid the debugger being
3569 intrusive, rely on the user to configure the requested
3570 mode. */
3571 if (secure_stack_used && !exception_domain_is_secure
3572 && !arm_unwind_secure_frames)
3574 warning (_("Non-secure to secure stack unwinding disabled."));
3576 /* Terminate any further stack unwinding. */
3577 arm_cache_set_active_sp_value (cache, tdep, 0);
3578 return cache;
3581 if (process_stack_used)
3583 if (secure_stack_used)
3584 /* Secure thread (process) stack used, use PSP_S as SP. */
3585 sp_regnum = tdep->m_profile_psp_s_regnum;
3586 else
3587 /* Non-secure thread (process) stack used, use PSP_NS as SP. */
3588 sp_regnum = tdep->m_profile_psp_ns_regnum;
3590 else
3592 if (secure_stack_used)
3593 /* Secure main stack used, use MSP_S as SP. */
3594 sp_regnum = tdep->m_profile_msp_s_regnum;
3595 else
3596 /* Non-secure main stack used, use MSP_NS as SP. */
3597 sp_regnum = tdep->m_profile_msp_ns_regnum;
3600 else
3602 if (process_stack_used)
3603 /* Thread (process) stack used, use PSP as SP. */
3604 sp_regnum = tdep->m_profile_psp_regnum;
3605 else
3606 /* Main stack used, use MSP as SP. */
3607 sp_regnum = tdep->m_profile_msp_regnum;
3610 /* Set the active SP regnum. */
3611 arm_cache_switch_prev_sp (cache, tdep, sp_regnum);
3613 /* Fetch the SP to use for this frame. */
3614 CORE_ADDR unwound_sp = arm_cache_get_prev_sp_value (cache, tdep);
3616 /* Exception entry context stacking are described in ARMv8-M (section
3617 B3.19) and ARMv7-M (sections B1.5.6 and B1.5.7) Architecture Reference
3618 Manuals.
3620 The following figure shows the structure of the stack frame when
3621 Security and Floating-point extensions are present.
3623 SP Offsets
3624 Without With
3625 Callee Regs Callee Regs
3626 (Secure -> Non-Secure)
3627 +-------------------+
3628 0xA8 | | 0xD0
3629 +===================+ --+ <-- Original SP
3630 0xA4 | S31 | 0xCC |
3631 +-------------------+ |
3632 ... | Additional FP context
3633 +-------------------+ |
3634 0x68 | S16 | 0x90 |
3635 +===================+ --+
3636 0x64 | Reserved | 0x8C |
3637 +-------------------+ |
3638 0x60 | FPSCR | 0x88 |
3639 +-------------------+ |
3640 0x5C | S15 | 0x84 | FP context
3641 +-------------------+ |
3642 ... |
3643 +-------------------+ |
3644 0x20 | S0 | 0x48 |
3645 +===================+ --+
3646 0x1C | xPSR | 0x44 |
3647 +-------------------+ |
3648 0x18 | Return address | 0x40 |
3649 +-------------------+ |
3650 0x14 | LR(R14) | 0x3C |
3651 +-------------------+ |
3652 0x10 | R12 | 0x38 | State context
3653 +-------------------+ |
3654 0x0C | R3 | 0x34 |
3655 +-------------------+ |
3656 ... |
3657 +-------------------+ |
3658 0x00 | R0 | 0x28 |
3659 +===================+ --+
3660 | R11 | 0x24 |
3661 +-------------------+ |
3662 ... |
3663 +-------------------+ | Additional state
3664 | R4 | 0x08 | context when
3665 +-------------------+ | transitioning from
3666 | Reserved | 0x04 | Secure to Non-Secure
3667 +-------------------+ |
3668 | Magic signature | 0x00 |
3669 +===================+ --+ <-- New SP */
3671 uint32_t sp_r0_offset = 0;
3673 /* With the Security extension, the hardware saves R4..R11 too. */
3674 if (tdep->have_sec_ext && secure_stack_used
3675 && (!default_callee_register_stacking || !exception_domain_is_secure))
3677 /* Read R4..R11 from the integer callee registers. */
3678 cache->saved_regs[4].set_addr (unwound_sp + 0x08);
3679 cache->saved_regs[5].set_addr (unwound_sp + 0x0C);
3680 cache->saved_regs[6].set_addr (unwound_sp + 0x10);
3681 cache->saved_regs[7].set_addr (unwound_sp + 0x14);
3682 cache->saved_regs[8].set_addr (unwound_sp + 0x18);
3683 cache->saved_regs[9].set_addr (unwound_sp + 0x1C);
3684 cache->saved_regs[10].set_addr (unwound_sp + 0x20);
3685 cache->saved_regs[11].set_addr (unwound_sp + 0x24);
3686 sp_r0_offset = 0x28;
3689 /* The hardware saves eight 32-bit words, comprising xPSR,
3690 ReturnAddress, LR (R14), R12, R3, R2, R1, R0. See details in
3691 "B1.5.6 Exception entry behavior" in
3692 "ARMv7-M Architecture Reference Manual". */
3693 cache->saved_regs[0].set_addr (unwound_sp + sp_r0_offset);
3694 cache->saved_regs[1].set_addr (unwound_sp + sp_r0_offset + 0x04);
3695 cache->saved_regs[2].set_addr (unwound_sp + sp_r0_offset + 0x08);
3696 cache->saved_regs[3].set_addr (unwound_sp + sp_r0_offset + 0x0C);
3697 cache->saved_regs[ARM_IP_REGNUM].set_addr (unwound_sp + sp_r0_offset
3698 + 0x10);
3699 cache->saved_regs[ARM_LR_REGNUM].set_addr (unwound_sp + sp_r0_offset
3700 + 0x14);
3701 cache->saved_regs[ARM_PC_REGNUM].set_addr (unwound_sp + sp_r0_offset
3702 + 0x18);
3703 cache->saved_regs[ARM_PS_REGNUM].set_addr (unwound_sp + sp_r0_offset
3704 + 0x1C);
3706 /* Check EXC_RETURN bit FTYPE if extended stack frame (FPU regs stored)
3707 type used. */
3708 bool extended_frame_used = (bit (lr, 4) == 0);
3709 if (extended_frame_used)
3711 ULONGEST fpccr;
3712 ULONGEST fpcar;
3714 /* Read FPCCR register. */
3715 if (!safe_read_memory_unsigned_integer (FPCCR, ARM_INT_REGISTER_SIZE,
3716 byte_order, &fpccr))
3718 warning (_("Could not fetch required FPCCR content. Further "
3719 "unwinding is impossible."));
3720 arm_cache_set_active_sp_value (cache, tdep, 0);
3721 return cache;
3724 /* Read FPCAR register. */
3725 if (!safe_read_memory_unsigned_integer (FPCAR, ARM_INT_REGISTER_SIZE,
3726 byte_order, &fpcar))
3728 warning (_("Could not fetch FPCAR content. Further unwinding of "
3729 "FP register values will be unreliable."));
3730 fpcar = 0;
3733 bool fpccr_aspen = bit (fpccr, 31);
3734 bool fpccr_lspen = bit (fpccr, 30);
3735 bool fpccr_ts = bit (fpccr, 26);
3736 bool fpccr_lspact = bit (fpccr, 0);
3738 /* The LSPEN and ASPEN bits indicate if the lazy state preservation
3739 for FP registers is enabled or disabled. The LSPACT bit indicate,
3740 together with FPCAR, if the lazy state preservation feature is
3741 active for the current frame or for another frame.
3742 See "Lazy context save of FP state", in B1.5.7, also ARM AN298,
3743 supported by Cortex-M4F architecture for details. */
3744 bool fpcar_points_to_this_frame = ((unwound_sp + sp_r0_offset + 0x20)
3745 == (fpcar & ~0x7));
3746 bool read_fp_regs_from_stack = (!(fpccr_aspen && fpccr_lspen
3747 && fpccr_lspact
3748 && fpcar_points_to_this_frame));
3750 /* Extended stack frame type used. */
3751 if (read_fp_regs_from_stack)
3753 CORE_ADDR addr = unwound_sp + sp_r0_offset + 0x20;
3754 for (int i = 0; i < 8; i++)
3756 cache->saved_regs[ARM_D0_REGNUM + i].set_addr (addr);
3757 addr += 8;
3760 cache->saved_regs[ARM_FPSCR_REGNUM].set_addr (unwound_sp
3761 + sp_r0_offset + 0x60);
3763 if (tdep->have_sec_ext && !default_callee_register_stacking
3764 && fpccr_ts)
3766 /* Handle floating-point callee saved registers. */
3767 if (read_fp_regs_from_stack)
3769 CORE_ADDR addr = unwound_sp + sp_r0_offset + 0x68;
3770 for (int i = 8; i < 16; i++)
3772 cache->saved_regs[ARM_D0_REGNUM + i].set_addr (addr);
3773 addr += 8;
3777 arm_cache_set_active_sp_value (cache, tdep,
3778 unwound_sp + sp_r0_offset + 0xA8);
3780 else
3782 /* Offset 0x64 is reserved. */
3783 arm_cache_set_active_sp_value (cache, tdep,
3784 unwound_sp + sp_r0_offset + 0x68);
3787 else
3789 /* Standard stack frame type used. */
3790 arm_cache_set_active_sp_value (cache, tdep,
3791 unwound_sp + sp_r0_offset + 0x20);
3794 /* If bit 9 of the saved xPSR is set, then there is a four-byte
3795 aligner between the top of the 32-byte stack frame and the
3796 previous context's stack pointer. */
3797 ULONGEST xpsr;
3798 if (!safe_read_memory_unsigned_integer (cache->saved_regs[ARM_PS_REGNUM]
3799 .addr (), ARM_INT_REGISTER_SIZE,
3800 byte_order, &xpsr))
3802 warning (_("Could not fetch required XPSR content. Further "
3803 "unwinding is impossible."));
3804 arm_cache_set_active_sp_value (cache, tdep, 0);
3805 return cache;
3808 if (bit (xpsr, 9) != 0)
3810 CORE_ADDR new_sp = arm_cache_get_prev_sp_value (cache, tdep) + 4;
3811 arm_cache_set_active_sp_value (cache, tdep, new_sp);
3814 return cache;
3817 internal_error (_("While unwinding an exception frame, "
3818 "found unexpected Link Register value "
3819 "%s. This should not happen and may "
3820 "be caused by corrupt data or a bug in"
3821 " GDB."),
3822 phex (lr, ARM_INT_REGISTER_SIZE));
3825 /* Implementation of the stop_reason hook for arm_m_exception frames. */
3827 static enum unwind_stop_reason
3828 arm_m_exception_frame_unwind_stop_reason (const frame_info_ptr &this_frame,
3829 void **this_cache)
3831 struct arm_prologue_cache *cache;
3832 arm_gdbarch_tdep *tdep
3833 = gdbarch_tdep<arm_gdbarch_tdep> (get_frame_arch (this_frame));
3835 if (*this_cache == NULL)
3836 *this_cache = arm_m_exception_cache (this_frame);
3837 cache = (struct arm_prologue_cache *) *this_cache;
3839 /* If we've hit a wall, stop. */
3840 if (arm_cache_get_prev_sp_value (cache, tdep) == 0)
3841 return UNWIND_OUTERMOST;
3843 return UNWIND_NO_REASON;
3846 /* Implementation of function hook 'this_id' in
3847 'struct frame_uwnind'. */
3849 static void
3850 arm_m_exception_this_id (const frame_info_ptr &this_frame,
3851 void **this_cache,
3852 struct frame_id *this_id)
3854 struct arm_prologue_cache *cache;
3856 if (*this_cache == NULL)
3857 *this_cache = arm_m_exception_cache (this_frame);
3858 cache = (struct arm_prologue_cache *) *this_cache;
3860 /* Our frame ID for a stub frame is the current SP and LR. */
3861 arm_gdbarch_tdep *tdep
3862 = gdbarch_tdep<arm_gdbarch_tdep> (get_frame_arch (this_frame));
3863 *this_id = frame_id_build (arm_cache_get_prev_sp_value (cache, tdep),
3864 get_frame_pc (this_frame));
3867 /* Implementation of function hook 'prev_register' in
3868 'struct frame_uwnind'. */
3870 static struct value *
3871 arm_m_exception_prev_register (const frame_info_ptr &this_frame,
3872 void **this_cache,
3873 int prev_regnum)
3875 struct arm_prologue_cache *cache;
3876 CORE_ADDR sp_value;
3878 if (*this_cache == NULL)
3879 *this_cache = arm_m_exception_cache (this_frame);
3880 cache = (struct arm_prologue_cache *) *this_cache;
3882 /* The value was already reconstructed into PREV_SP. */
3883 arm_gdbarch_tdep *tdep
3884 = gdbarch_tdep<arm_gdbarch_tdep> (get_frame_arch (this_frame));
3885 if (prev_regnum == ARM_SP_REGNUM)
3886 return frame_unwind_got_constant (this_frame, prev_regnum,
3887 arm_cache_get_prev_sp_value (cache, tdep));
3889 /* If we are asked to unwind the PC, strip the saved T bit. */
3890 if (prev_regnum == ARM_PC_REGNUM)
3892 struct value *value = trad_frame_get_prev_register (this_frame,
3893 cache->saved_regs,
3894 prev_regnum);
3895 CORE_ADDR pc = value_as_address (value);
3896 return frame_unwind_got_constant (this_frame, prev_regnum,
3897 UNMAKE_THUMB_ADDR (pc));
3900 /* The value might be one of the alternative SP, if so, use the
3901 value already constructed. */
3902 if (arm_is_alternative_sp_register (tdep, prev_regnum))
3904 sp_value = arm_cache_get_sp_register (cache, tdep, prev_regnum);
3905 return frame_unwind_got_constant (this_frame, prev_regnum, sp_value);
3908 /* If we are asked to unwind the xPSR, set T bit if PC is in thumb mode.
3909 LR register is unreliable as it contains FNC_RETURN or EXC_RETURN
3910 pattern. */
3911 if (prev_regnum == ARM_PS_REGNUM)
3913 struct gdbarch *gdbarch = get_frame_arch (this_frame);
3914 struct value *value = trad_frame_get_prev_register (this_frame,
3915 cache->saved_regs,
3916 ARM_PC_REGNUM);
3917 CORE_ADDR pc = value_as_address (value);
3918 value = trad_frame_get_prev_register (this_frame, cache->saved_regs,
3919 ARM_PS_REGNUM);
3920 ULONGEST xpsr = value_as_long (value);
3922 /* Reconstruct the T bit; see arm_prologue_prev_register for details. */
3923 xpsr = reconstruct_t_bit (gdbarch, pc, xpsr);
3924 return frame_unwind_got_constant (this_frame, ARM_PS_REGNUM, xpsr);
3927 return trad_frame_get_prev_register (this_frame, cache->saved_regs,
3928 prev_regnum);
3931 /* Implementation of function hook 'sniffer' in
3932 'struct frame_uwnind'. */
3934 static int
3935 arm_m_exception_unwind_sniffer (const struct frame_unwind *self,
3936 const frame_info_ptr &this_frame,
3937 void **this_prologue_cache)
3939 struct gdbarch *gdbarch = get_frame_arch (this_frame);
3940 CORE_ADDR this_pc = get_frame_pc (this_frame);
3942 /* No need to check is_m; this sniffer is only registered for
3943 M-profile architectures. */
3945 /* Check if exception frame returns to a magic PC value. */
3946 return arm_m_addr_is_magic (gdbarch, this_pc);
3949 /* Frame unwinder for M-profile exceptions (EXC_RETURN on stack),
3950 lockup and secure/nonsecure interstate function calls (FNC_RETURN). */
3952 struct frame_unwind arm_m_exception_unwind =
3954 "arm m exception lockup sec_fnc",
3955 SIGTRAMP_FRAME,
3956 arm_m_exception_frame_unwind_stop_reason,
3957 arm_m_exception_this_id,
3958 arm_m_exception_prev_register,
3959 NULL,
3960 arm_m_exception_unwind_sniffer
3963 static CORE_ADDR
3964 arm_normal_frame_base (const frame_info_ptr &this_frame, void **this_cache)
3966 struct arm_prologue_cache *cache;
3968 if (*this_cache == NULL)
3969 *this_cache = arm_make_prologue_cache (this_frame);
3970 cache = (struct arm_prologue_cache *) *this_cache;
3972 arm_gdbarch_tdep *tdep
3973 = gdbarch_tdep<arm_gdbarch_tdep> (get_frame_arch (this_frame));
3974 return arm_cache_get_prev_sp_value (cache, tdep) - cache->framesize;
3977 struct frame_base arm_normal_base = {
3978 &arm_prologue_unwind,
3979 arm_normal_frame_base,
3980 arm_normal_frame_base,
3981 arm_normal_frame_base
3984 struct arm_dwarf2_prev_register_cache
3986 /* Cached value of the corresponding stack pointer for the inner frame. */
3987 CORE_ADDR sp;
3988 CORE_ADDR msp;
3989 CORE_ADDR msp_s;
3990 CORE_ADDR msp_ns;
3991 CORE_ADDR psp;
3992 CORE_ADDR psp_s;
3993 CORE_ADDR psp_ns;
3996 static struct value *
3997 arm_dwarf2_prev_register (const frame_info_ptr &this_frame, void **this_cache,
3998 int regnum)
4000 struct gdbarch * gdbarch = get_frame_arch (this_frame);
4001 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
4002 CORE_ADDR lr;
4003 ULONGEST cpsr;
4004 arm_dwarf2_prev_register_cache *cache
4005 = ((arm_dwarf2_prev_register_cache *)
4006 dwarf2_frame_get_fn_data (this_frame, this_cache,
4007 arm_dwarf2_prev_register));
4009 if (!cache)
4011 const unsigned int size = sizeof (struct arm_dwarf2_prev_register_cache);
4012 cache = ((arm_dwarf2_prev_register_cache *)
4013 dwarf2_frame_allocate_fn_data (this_frame, this_cache,
4014 arm_dwarf2_prev_register, size));
4016 if (tdep->have_sec_ext)
4018 cache->sp
4019 = get_frame_register_unsigned (this_frame, ARM_SP_REGNUM);
4021 cache->msp_s
4022 = get_frame_register_unsigned (this_frame,
4023 tdep->m_profile_msp_s_regnum);
4024 cache->msp_ns
4025 = get_frame_register_unsigned (this_frame,
4026 tdep->m_profile_msp_ns_regnum);
4027 cache->psp_s
4028 = get_frame_register_unsigned (this_frame,
4029 tdep->m_profile_psp_s_regnum);
4030 cache->psp_ns
4031 = get_frame_register_unsigned (this_frame,
4032 tdep->m_profile_psp_ns_regnum);
4034 else if (tdep->is_m)
4036 cache->sp
4037 = get_frame_register_unsigned (this_frame, ARM_SP_REGNUM);
4039 cache->msp
4040 = get_frame_register_unsigned (this_frame,
4041 tdep->m_profile_msp_regnum);
4042 cache->psp
4043 = get_frame_register_unsigned (this_frame,
4044 tdep->m_profile_psp_regnum);
4048 if (regnum == ARM_PC_REGNUM)
4050 /* The PC is normally copied from the return column, which
4051 describes saves of LR. However, that version may have an
4052 extra bit set to indicate Thumb state. The bit is not
4053 part of the PC. */
4055 /* Record in the frame whether the return address was signed. */
4056 if (tdep->have_pacbti)
4058 CORE_ADDR ra_auth_code
4059 = frame_unwind_register_unsigned (this_frame,
4060 tdep->pacbti_pseudo_base);
4062 if (ra_auth_code != 0)
4063 set_frame_previous_pc_masked (this_frame);
4066 lr = frame_unwind_register_unsigned (this_frame, ARM_LR_REGNUM);
4067 return frame_unwind_got_constant (this_frame, regnum,
4068 arm_addr_bits_remove (gdbarch, lr));
4070 else if (regnum == ARM_PS_REGNUM)
4072 /* Reconstruct the T bit; see arm_prologue_prev_register for details. */
4073 cpsr = get_frame_register_unsigned (this_frame, regnum);
4074 lr = frame_unwind_register_unsigned (this_frame, ARM_LR_REGNUM);
4075 cpsr = reconstruct_t_bit (gdbarch, lr, cpsr);
4076 return frame_unwind_got_constant (this_frame, regnum, cpsr);
4078 else if (arm_is_alternative_sp_register (tdep, regnum))
4080 /* Handle the alternative SP registers on Cortex-M. */
4081 bool override_with_sp_value = false;
4082 CORE_ADDR val;
4084 if (tdep->have_sec_ext)
4086 bool is_msp = (regnum == tdep->m_profile_msp_regnum)
4087 && (cache->msp_s == cache->sp || cache->msp_ns == cache->sp);
4088 bool is_msp_s = (regnum == tdep->m_profile_msp_s_regnum)
4089 && (cache->msp_s == cache->sp);
4090 bool is_msp_ns = (regnum == tdep->m_profile_msp_ns_regnum)
4091 && (cache->msp_ns == cache->sp);
4092 bool is_psp = (regnum == tdep->m_profile_psp_regnum)
4093 && (cache->psp_s == cache->sp || cache->psp_ns == cache->sp);
4094 bool is_psp_s = (regnum == tdep->m_profile_psp_s_regnum)
4095 && (cache->psp_s == cache->sp);
4096 bool is_psp_ns = (regnum == tdep->m_profile_psp_ns_regnum)
4097 && (cache->psp_ns == cache->sp);
4099 override_with_sp_value = is_msp || is_msp_s || is_msp_ns
4100 || is_psp || is_psp_s || is_psp_ns;
4103 else if (tdep->is_m)
4105 bool is_msp = (regnum == tdep->m_profile_msp_regnum)
4106 && (cache->sp == cache->msp);
4107 bool is_psp = (regnum == tdep->m_profile_psp_regnum)
4108 && (cache->sp == cache->psp);
4110 override_with_sp_value = is_msp || is_psp;
4113 if (override_with_sp_value)
4115 /* Use value of SP from previous frame. */
4116 frame_info_ptr prev_frame = get_prev_frame (this_frame);
4117 if (prev_frame)
4118 val = get_frame_register_unsigned (prev_frame, ARM_SP_REGNUM);
4119 else
4120 val = get_frame_base (this_frame);
4122 else
4123 /* Use value for the register from previous frame. */
4124 val = get_frame_register_unsigned (this_frame, regnum);
4126 return frame_unwind_got_constant (this_frame, regnum, val);
4129 internal_error (_("Unexpected register %d"), regnum);
4132 /* Implement the stack_frame_destroyed_p gdbarch method. */
4134 static int
4135 thumb_stack_frame_destroyed_p (struct gdbarch *gdbarch, CORE_ADDR pc)
4137 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
4138 unsigned int insn, insn2;
4139 int found_return = 0, found_stack_adjust = 0;
4140 CORE_ADDR func_start, func_end;
4141 CORE_ADDR scan_pc;
4142 gdb_byte buf[4];
4144 if (!find_pc_partial_function (pc, NULL, &func_start, &func_end))
4145 return 0;
4147 /* The epilogue is a sequence of instructions along the following lines:
4149 - add stack frame size to SP or FP
4150 - [if frame pointer used] restore SP from FP
4151 - restore registers from SP [may include PC]
4152 - a return-type instruction [if PC wasn't already restored]
4154 In a first pass, we scan forward from the current PC and verify the
4155 instructions we find as compatible with this sequence, ending in a
4156 return instruction.
4158 However, this is not sufficient to distinguish indirect function calls
4159 within a function from indirect tail calls in the epilogue in some cases.
4160 Therefore, if we didn't already find any SP-changing instruction during
4161 forward scan, we add a backward scanning heuristic to ensure we actually
4162 are in the epilogue. */
4164 scan_pc = pc;
4165 while (scan_pc < func_end && !found_return)
4167 if (target_read_memory (scan_pc, buf, 2))
4168 break;
4170 scan_pc += 2;
4171 insn = extract_unsigned_integer (buf, 2, byte_order_for_code);
4173 if ((insn & 0xff80) == 0x4700) /* bx <Rm> */
4174 found_return = 1;
4175 else if (insn == 0x46f7) /* mov pc, lr */
4176 found_return = 1;
4177 else if (thumb_instruction_restores_sp (insn))
4179 if ((insn & 0xff00) == 0xbd00) /* pop <registers, PC> */
4180 found_return = 1;
4182 else if (thumb_insn_size (insn) == 4) /* 32-bit Thumb-2 instruction */
4184 if (target_read_memory (scan_pc, buf, 2))
4185 break;
4187 scan_pc += 2;
4188 insn2 = extract_unsigned_integer (buf, 2, byte_order_for_code);
4190 if (insn == 0xe8bd) /* ldm.w sp!, <registers> */
4192 if (insn2 & 0x8000) /* <registers> include PC. */
4193 found_return = 1;
4195 else if (insn == 0xf85d /* ldr.w <Rt>, [sp], #4 */
4196 && (insn2 & 0x0fff) == 0x0b04)
4198 if ((insn2 & 0xf000) == 0xf000) /* <Rt> is PC. */
4199 found_return = 1;
4201 else if ((insn & 0xffbf) == 0xecbd /* vldm sp!, <list> */
4202 && (insn2 & 0x0e00) == 0x0a00)
4204 else
4205 break;
4207 else
4208 break;
4211 if (!found_return)
4212 return 0;
4214 /* Since any instruction in the epilogue sequence, with the possible
4215 exception of return itself, updates the stack pointer, we need to
4216 scan backwards for at most one instruction. Try either a 16-bit or
4217 a 32-bit instruction. This is just a heuristic, so we do not worry
4218 too much about false positives. */
4220 if (pc - 4 < func_start)
4221 return 0;
4222 if (target_read_memory (pc - 4, buf, 4))
4223 return 0;
4225 insn = extract_unsigned_integer (buf, 2, byte_order_for_code);
4226 insn2 = extract_unsigned_integer (buf + 2, 2, byte_order_for_code);
4228 if (thumb_instruction_restores_sp (insn2))
4229 found_stack_adjust = 1;
4230 else if (insn == 0xe8bd) /* ldm.w sp!, <registers> */
4231 found_stack_adjust = 1;
4232 else if (insn == 0xf85d /* ldr.w <Rt>, [sp], #4 */
4233 && (insn2 & 0x0fff) == 0x0b04)
4234 found_stack_adjust = 1;
4235 else if ((insn & 0xffbf) == 0xecbd /* vldm sp!, <list> */
4236 && (insn2 & 0x0e00) == 0x0a00)
4237 found_stack_adjust = 1;
4239 return found_stack_adjust;
4242 static int
4243 arm_stack_frame_destroyed_p_1 (struct gdbarch *gdbarch, CORE_ADDR pc)
4245 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
4246 unsigned int insn;
4247 int found_return;
4248 CORE_ADDR func_start, func_end;
4250 if (!find_pc_partial_function (pc, NULL, &func_start, &func_end))
4251 return 0;
4253 /* We are in the epilogue if the previous instruction was a stack
4254 adjustment and the next instruction is a possible return (bx, mov
4255 pc, or pop). We could have to scan backwards to find the stack
4256 adjustment, or forwards to find the return, but this is a decent
4257 approximation. First scan forwards. */
4259 found_return = 0;
4260 insn = read_memory_unsigned_integer (pc, 4, byte_order_for_code);
4261 if (bits (insn, 28, 31) != INST_NV)
4263 if ((insn & 0x0ffffff0) == 0x012fff10)
4264 /* BX. */
4265 found_return = 1;
4266 else if ((insn & 0x0ffffff0) == 0x01a0f000)
4267 /* MOV PC. */
4268 found_return = 1;
4269 else if ((insn & 0x0fff0000) == 0x08bd0000
4270 && (insn & 0x0000c000) != 0)
4271 /* POP (LDMIA), including PC or LR. */
4272 found_return = 1;
4275 if (!found_return)
4276 return 0;
4278 /* Scan backwards. This is just a heuristic, so do not worry about
4279 false positives from mode changes. */
4281 if (pc < func_start + 4)
4282 return 0;
4284 insn = read_memory_unsigned_integer (pc - 4, 4, byte_order_for_code);
4285 if (arm_instruction_restores_sp (insn))
4286 return 1;
4288 return 0;
4291 /* Implement the stack_frame_destroyed_p gdbarch method. */
4293 static int
4294 arm_stack_frame_destroyed_p (struct gdbarch *gdbarch, CORE_ADDR pc)
4296 if (arm_pc_is_thumb (gdbarch, pc))
4297 return thumb_stack_frame_destroyed_p (gdbarch, pc);
4298 else
4299 return arm_stack_frame_destroyed_p_1 (gdbarch, pc);
4302 /* When arguments must be pushed onto the stack, they go on in reverse
4303 order. The code below implements a FILO (stack) to do this. */
4305 struct arm_stack_item
4307 int len;
4308 struct arm_stack_item *prev;
4309 gdb_byte *data;
4312 static struct arm_stack_item *
4313 push_stack_item (struct arm_stack_item *prev, const gdb_byte *contents,
4314 int len)
4316 struct arm_stack_item *si;
4317 si = XNEW (struct arm_stack_item);
4318 si->data = (gdb_byte *) xmalloc (len);
4319 si->len = len;
4320 si->prev = prev;
4321 memcpy (si->data, contents, len);
4322 return si;
4325 static struct arm_stack_item *
4326 pop_stack_item (struct arm_stack_item *si)
4328 struct arm_stack_item *dead = si;
4329 si = si->prev;
4330 xfree (dead->data);
4331 xfree (dead);
4332 return si;
4335 /* Implement the gdbarch type alignment method, overrides the generic
4336 alignment algorithm for anything that is arm specific. */
4338 static ULONGEST
4339 arm_type_align (gdbarch *gdbarch, struct type *t)
4341 t = check_typedef (t);
4342 if (t->code () == TYPE_CODE_ARRAY && t->is_vector ())
4344 /* Use the natural alignment for vector types (the same for
4345 scalar type), but the maximum alignment is 64-bit. */
4346 if (t->length () > 8)
4347 return 8;
4348 else
4349 return t->length ();
4352 /* Allow the common code to calculate the alignment. */
4353 return 0;
4356 /* Possible base types for a candidate for passing and returning in
4357 VFP registers. */
4359 enum arm_vfp_cprc_base_type
4361 VFP_CPRC_UNKNOWN,
4362 VFP_CPRC_SINGLE,
4363 VFP_CPRC_DOUBLE,
4364 VFP_CPRC_VEC64,
4365 VFP_CPRC_VEC128
4368 /* The length of one element of base type B. */
4370 static unsigned
4371 arm_vfp_cprc_unit_length (enum arm_vfp_cprc_base_type b)
4373 switch (b)
4375 case VFP_CPRC_SINGLE:
4376 return 4;
4377 case VFP_CPRC_DOUBLE:
4378 return 8;
4379 case VFP_CPRC_VEC64:
4380 return 8;
4381 case VFP_CPRC_VEC128:
4382 return 16;
4383 default:
4384 internal_error (_("Invalid VFP CPRC type: %d."),
4385 (int) b);
4389 /* The character ('s', 'd' or 'q') for the type of VFP register used
4390 for passing base type B. */
4392 static int
4393 arm_vfp_cprc_reg_char (enum arm_vfp_cprc_base_type b)
4395 switch (b)
4397 case VFP_CPRC_SINGLE:
4398 return 's';
4399 case VFP_CPRC_DOUBLE:
4400 return 'd';
4401 case VFP_CPRC_VEC64:
4402 return 'd';
4403 case VFP_CPRC_VEC128:
4404 return 'q';
4405 default:
4406 internal_error (_("Invalid VFP CPRC type: %d."),
4407 (int) b);
4411 /* Determine whether T may be part of a candidate for passing and
4412 returning in VFP registers, ignoring the limit on the total number
4413 of components. If *BASE_TYPE is VFP_CPRC_UNKNOWN, set it to the
4414 classification of the first valid component found; if it is not
4415 VFP_CPRC_UNKNOWN, all components must have the same classification
4416 as *BASE_TYPE. If it is found that T contains a type not permitted
4417 for passing and returning in VFP registers, a type differently
4418 classified from *BASE_TYPE, or two types differently classified
4419 from each other, return -1, otherwise return the total number of
4420 base-type elements found (possibly 0 in an empty structure or
4421 array). Vector types are not currently supported, matching the
4422 generic AAPCS support. */
4424 static int
4425 arm_vfp_cprc_sub_candidate (struct type *t,
4426 enum arm_vfp_cprc_base_type *base_type)
4428 t = check_typedef (t);
4429 switch (t->code ())
4431 case TYPE_CODE_FLT:
4432 switch (t->length ())
4434 case 4:
4435 if (*base_type == VFP_CPRC_UNKNOWN)
4436 *base_type = VFP_CPRC_SINGLE;
4437 else if (*base_type != VFP_CPRC_SINGLE)
4438 return -1;
4439 return 1;
4441 case 8:
4442 if (*base_type == VFP_CPRC_UNKNOWN)
4443 *base_type = VFP_CPRC_DOUBLE;
4444 else if (*base_type != VFP_CPRC_DOUBLE)
4445 return -1;
4446 return 1;
4448 default:
4449 return -1;
4451 break;
4453 case TYPE_CODE_COMPLEX:
4454 /* Arguments of complex T where T is one of the types float or
4455 double get treated as if they are implemented as:
4457 struct complexT
4459 T real;
4460 T imag;
4464 switch (t->length ())
4466 case 8:
4467 if (*base_type == VFP_CPRC_UNKNOWN)
4468 *base_type = VFP_CPRC_SINGLE;
4469 else if (*base_type != VFP_CPRC_SINGLE)
4470 return -1;
4471 return 2;
4473 case 16:
4474 if (*base_type == VFP_CPRC_UNKNOWN)
4475 *base_type = VFP_CPRC_DOUBLE;
4476 else if (*base_type != VFP_CPRC_DOUBLE)
4477 return -1;
4478 return 2;
4480 default:
4481 return -1;
4483 break;
4485 case TYPE_CODE_ARRAY:
4487 if (t->is_vector ())
4489 /* A 64-bit or 128-bit containerized vector type are VFP
4490 CPRCs. */
4491 switch (t->length ())
4493 case 8:
4494 if (*base_type == VFP_CPRC_UNKNOWN)
4495 *base_type = VFP_CPRC_VEC64;
4496 return 1;
4497 case 16:
4498 if (*base_type == VFP_CPRC_UNKNOWN)
4499 *base_type = VFP_CPRC_VEC128;
4500 return 1;
4501 default:
4502 return -1;
4505 else
4507 int count;
4508 unsigned unitlen;
4510 count = arm_vfp_cprc_sub_candidate (t->target_type (),
4511 base_type);
4512 if (count == -1)
4513 return -1;
4514 if (t->length () == 0)
4516 gdb_assert (count == 0);
4517 return 0;
4519 else if (count == 0)
4520 return -1;
4521 unitlen = arm_vfp_cprc_unit_length (*base_type);
4522 gdb_assert ((t->length () % unitlen) == 0);
4523 return t->length () / unitlen;
4526 break;
4528 case TYPE_CODE_STRUCT:
4530 int count = 0;
4531 unsigned unitlen;
4532 int i;
4533 for (i = 0; i < t->num_fields (); i++)
4535 int sub_count = 0;
4537 if (!t->field (i).is_static ())
4538 sub_count = arm_vfp_cprc_sub_candidate (t->field (i).type (),
4539 base_type);
4540 if (sub_count == -1)
4541 return -1;
4542 count += sub_count;
4544 if (t->length () == 0)
4546 gdb_assert (count == 0);
4547 return 0;
4549 else if (count == 0)
4550 return -1;
4551 unitlen = arm_vfp_cprc_unit_length (*base_type);
4552 if (t->length () != unitlen * count)
4553 return -1;
4554 return count;
4557 case TYPE_CODE_UNION:
4559 int count = 0;
4560 unsigned unitlen;
4561 int i;
4562 for (i = 0; i < t->num_fields (); i++)
4564 int sub_count = arm_vfp_cprc_sub_candidate (t->field (i).type (),
4565 base_type);
4566 if (sub_count == -1)
4567 return -1;
4568 count = (count > sub_count ? count : sub_count);
4570 if (t->length () == 0)
4572 gdb_assert (count == 0);
4573 return 0;
4575 else if (count == 0)
4576 return -1;
4577 unitlen = arm_vfp_cprc_unit_length (*base_type);
4578 if (t->length () != unitlen * count)
4579 return -1;
4580 return count;
4583 default:
4584 break;
4587 return -1;
4590 /* Determine whether T is a VFP co-processor register candidate (CPRC)
4591 if passed to or returned from a non-variadic function with the VFP
4592 ABI in effect. Return 1 if it is, 0 otherwise. If it is, set
4593 *BASE_TYPE to the base type for T and *COUNT to the number of
4594 elements of that base type before returning. */
4596 static int
4597 arm_vfp_call_candidate (struct type *t, enum arm_vfp_cprc_base_type *base_type,
4598 int *count)
4600 enum arm_vfp_cprc_base_type b = VFP_CPRC_UNKNOWN;
4601 int c = arm_vfp_cprc_sub_candidate (t, &b);
4602 if (c <= 0 || c > 4)
4603 return 0;
4604 *base_type = b;
4605 *count = c;
4606 return 1;
4609 /* Return 1 if the VFP ABI should be used for passing arguments to and
4610 returning values from a function of type FUNC_TYPE, 0
4611 otherwise. */
4613 static int
4614 arm_vfp_abi_for_function (struct gdbarch *gdbarch, struct type *func_type)
4616 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
4618 /* Variadic functions always use the base ABI. Assume that functions
4619 without debug info are not variadic. */
4620 if (func_type && check_typedef (func_type)->has_varargs ())
4621 return 0;
4623 /* The VFP ABI is only supported as a variant of AAPCS. */
4624 if (tdep->arm_abi != ARM_ABI_AAPCS)
4625 return 0;
4627 return tdep->fp_model == ARM_FLOAT_VFP;
4630 /* We currently only support passing parameters in integer registers, which
4631 conforms with GCC's default model, and VFP argument passing following
4632 the VFP variant of AAPCS. Several other variants exist and
4633 we should probably support some of them based on the selected ABI. */
4635 static CORE_ADDR
4636 arm_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
4637 struct regcache *regcache, CORE_ADDR bp_addr, int nargs,
4638 struct value **args, CORE_ADDR sp,
4639 function_call_return_method return_method,
4640 CORE_ADDR struct_addr)
4642 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
4643 int argnum;
4644 int argreg;
4645 int nstack;
4646 struct arm_stack_item *si = NULL;
4647 int use_vfp_abi;
4648 struct type *ftype;
4649 unsigned vfp_regs_free = (1 << 16) - 1;
4650 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
4652 /* Determine the type of this function and whether the VFP ABI
4653 applies. */
4654 ftype = check_typedef (function->type ());
4655 if (ftype->code () == TYPE_CODE_PTR)
4656 ftype = check_typedef (ftype->target_type ());
4657 use_vfp_abi = arm_vfp_abi_for_function (gdbarch, ftype);
4659 /* Set the return address. For the ARM, the return breakpoint is
4660 always at BP_ADDR. */
4661 if (arm_pc_is_thumb (gdbarch, bp_addr))
4662 bp_addr |= 1;
4663 regcache_cooked_write_unsigned (regcache, ARM_LR_REGNUM, bp_addr);
4665 /* Walk through the list of args and determine how large a temporary
4666 stack is required. Need to take care here as structs may be
4667 passed on the stack, and we have to push them. */
4668 nstack = 0;
4670 argreg = ARM_A1_REGNUM;
4671 nstack = 0;
4673 /* The struct_return pointer occupies the first parameter
4674 passing register. */
4675 if (return_method == return_method_struct)
4677 arm_debug_printf ("struct return in %s = %s",
4678 gdbarch_register_name (gdbarch, argreg),
4679 paddress (gdbarch, struct_addr));
4681 regcache_cooked_write_unsigned (regcache, argreg, struct_addr);
4682 argreg++;
4685 for (argnum = 0; argnum < nargs; argnum++)
4687 int len;
4688 struct type *arg_type;
4689 struct type *target_type;
4690 enum type_code typecode;
4691 const bfd_byte *val;
4692 int align;
4693 enum arm_vfp_cprc_base_type vfp_base_type;
4694 int vfp_base_count;
4695 int may_use_core_reg = 1;
4697 arg_type = check_typedef (args[argnum]->type ());
4698 len = arg_type->length ();
4699 target_type = arg_type->target_type ();
4700 typecode = arg_type->code ();
4701 val = args[argnum]->contents ().data ();
4703 align = type_align (arg_type);
4704 /* Round alignment up to a whole number of words. */
4705 align = (align + ARM_INT_REGISTER_SIZE - 1)
4706 & ~(ARM_INT_REGISTER_SIZE - 1);
4707 /* Different ABIs have different maximum alignments. */
4708 if (tdep->arm_abi == ARM_ABI_APCS)
4710 /* The APCS ABI only requires word alignment. */
4711 align = ARM_INT_REGISTER_SIZE;
4713 else
4715 /* The AAPCS requires at most doubleword alignment. */
4716 if (align > ARM_INT_REGISTER_SIZE * 2)
4717 align = ARM_INT_REGISTER_SIZE * 2;
4720 if (use_vfp_abi
4721 && arm_vfp_call_candidate (arg_type, &vfp_base_type,
4722 &vfp_base_count))
4724 int regno;
4725 int unit_length;
4726 int shift;
4727 unsigned mask;
4729 /* Because this is a CPRC it cannot go in a core register or
4730 cause a core register to be skipped for alignment.
4731 Either it goes in VFP registers and the rest of this loop
4732 iteration is skipped for this argument, or it goes on the
4733 stack (and the stack alignment code is correct for this
4734 case). */
4735 may_use_core_reg = 0;
4737 unit_length = arm_vfp_cprc_unit_length (vfp_base_type);
4738 shift = unit_length / 4;
4739 mask = (1 << (shift * vfp_base_count)) - 1;
4740 for (regno = 0; regno < 16; regno += shift)
4741 if (((vfp_regs_free >> regno) & mask) == mask)
4742 break;
4744 if (regno < 16)
4746 int reg_char;
4747 int reg_scaled;
4748 int i;
4750 vfp_regs_free &= ~(mask << regno);
4751 reg_scaled = regno / shift;
4752 reg_char = arm_vfp_cprc_reg_char (vfp_base_type);
4753 for (i = 0; i < vfp_base_count; i++)
4755 char name_buf[4];
4756 int regnum;
4757 if (reg_char == 'q')
4758 arm_neon_quad_write (gdbarch, regcache, reg_scaled + i,
4759 val + i * unit_length);
4760 else
4762 xsnprintf (name_buf, sizeof (name_buf), "%c%d",
4763 reg_char, reg_scaled + i);
4764 regnum = user_reg_map_name_to_regnum (gdbarch, name_buf,
4765 strlen (name_buf));
4766 regcache->cooked_write (regnum, val + i * unit_length);
4769 continue;
4771 else
4773 /* This CPRC could not go in VFP registers, so all VFP
4774 registers are now marked as used. */
4775 vfp_regs_free = 0;
4779 /* Push stack padding for doubleword alignment. */
4780 if (nstack & (align - 1))
4782 si = push_stack_item (si, val, ARM_INT_REGISTER_SIZE);
4783 nstack += ARM_INT_REGISTER_SIZE;
4786 /* Doubleword aligned quantities must go in even register pairs. */
4787 if (may_use_core_reg
4788 && argreg <= ARM_LAST_ARG_REGNUM
4789 && align > ARM_INT_REGISTER_SIZE
4790 && argreg & 1)
4791 argreg++;
4793 /* If the argument is a pointer to a function, and it is a
4794 Thumb function, create a LOCAL copy of the value and set
4795 the THUMB bit in it. */
4796 if (TYPE_CODE_PTR == typecode
4797 && target_type != NULL
4798 && TYPE_CODE_FUNC == check_typedef (target_type)->code ())
4800 CORE_ADDR regval = extract_unsigned_integer (val, len, byte_order);
4801 if (arm_pc_is_thumb (gdbarch, regval))
4803 bfd_byte *copy = (bfd_byte *) alloca (len);
4804 store_unsigned_integer (copy, len, byte_order,
4805 MAKE_THUMB_ADDR (regval));
4806 val = copy;
4810 /* Copy the argument to general registers or the stack in
4811 register-sized pieces. Large arguments are split between
4812 registers and stack. */
4813 while (len > 0)
4815 int partial_len = len < ARM_INT_REGISTER_SIZE
4816 ? len : ARM_INT_REGISTER_SIZE;
4817 CORE_ADDR regval
4818 = extract_unsigned_integer (val, partial_len, byte_order);
4820 if (may_use_core_reg && argreg <= ARM_LAST_ARG_REGNUM)
4822 /* The argument is being passed in a general purpose
4823 register. */
4824 arm_debug_printf ("arg %d in %s = 0x%s", argnum,
4825 gdbarch_register_name (gdbarch, argreg),
4826 phex (regval, ARM_INT_REGISTER_SIZE));
4828 regcache_cooked_write_unsigned (regcache, argreg, regval);
4829 argreg++;
4831 else
4833 gdb_byte buf[ARM_INT_REGISTER_SIZE];
4835 memset (buf, 0, sizeof (buf));
4836 store_unsigned_integer (buf, partial_len, byte_order, regval);
4838 /* Push the arguments onto the stack. */
4839 arm_debug_printf ("arg %d @ sp + %d", argnum, nstack);
4840 si = push_stack_item (si, buf, ARM_INT_REGISTER_SIZE);
4841 nstack += ARM_INT_REGISTER_SIZE;
4844 len -= partial_len;
4845 val += partial_len;
4848 /* If we have an odd number of words to push, then decrement the stack
4849 by one word now, so first stack argument will be dword aligned. */
4850 if (nstack & 4)
4851 sp -= 4;
4853 while (si)
4855 sp -= si->len;
4856 write_memory (sp, si->data, si->len);
4857 si = pop_stack_item (si);
4860 /* Finally, update the SP register. */
4861 regcache_cooked_write_unsigned (regcache, ARM_SP_REGNUM, sp);
4863 return sp;
4867 /* Always align the frame to an 8-byte boundary. This is required on
4868 some platforms and harmless on the rest. */
4870 static CORE_ADDR
4871 arm_frame_align (struct gdbarch *gdbarch, CORE_ADDR sp)
4873 /* Align the stack to eight bytes. */
4874 return sp & ~ (CORE_ADDR) 7;
4877 static void
4878 print_fpu_flags (struct ui_file *file, int flags)
4880 if (flags & (1 << 0))
4881 gdb_puts ("IVO ", file);
4882 if (flags & (1 << 1))
4883 gdb_puts ("DVZ ", file);
4884 if (flags & (1 << 2))
4885 gdb_puts ("OFL ", file);
4886 if (flags & (1 << 3))
4887 gdb_puts ("UFL ", file);
4888 if (flags & (1 << 4))
4889 gdb_puts ("INX ", file);
4890 gdb_putc ('\n', file);
4893 /* Print interesting information about the floating point processor
4894 (if present) or emulator. */
4895 static void
4896 arm_print_float_info (struct gdbarch *gdbarch, struct ui_file *file,
4897 const frame_info_ptr &frame, const char *args)
4899 unsigned long status = get_frame_register_unsigned (frame, ARM_FPS_REGNUM);
4900 int type;
4902 type = (status >> 24) & 127;
4903 if (status & (1 << 31))
4904 gdb_printf (file, _("Hardware FPU type %d\n"), type);
4905 else
4906 gdb_printf (file, _("Software FPU type %d\n"), type);
4907 /* i18n: [floating point unit] mask */
4908 gdb_puts (_("mask: "), file);
4909 print_fpu_flags (file, status >> 16);
4910 /* i18n: [floating point unit] flags */
4911 gdb_puts (_("flags: "), file);
4912 print_fpu_flags (file, status);
4915 /* Construct the ARM extended floating point type. */
4916 static struct type *
4917 arm_ext_type (struct gdbarch *gdbarch)
4919 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
4921 if (!tdep->arm_ext_type)
4923 type_allocator alloc (gdbarch);
4924 tdep->arm_ext_type
4925 = init_float_type (alloc, -1, "builtin_type_arm_ext",
4926 floatformats_arm_ext);
4929 return tdep->arm_ext_type;
4932 static struct type *
4933 arm_neon_double_type (struct gdbarch *gdbarch)
4935 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
4937 if (tdep->neon_double_type == NULL)
4939 struct type *t, *elem;
4941 t = arch_composite_type (gdbarch, "__gdb_builtin_type_neon_d",
4942 TYPE_CODE_UNION);
4943 elem = builtin_type (gdbarch)->builtin_uint8;
4944 append_composite_type_field (t, "u8", init_vector_type (elem, 8));
4945 elem = builtin_type (gdbarch)->builtin_uint16;
4946 append_composite_type_field (t, "u16", init_vector_type (elem, 4));
4947 elem = builtin_type (gdbarch)->builtin_uint32;
4948 append_composite_type_field (t, "u32", init_vector_type (elem, 2));
4949 elem = builtin_type (gdbarch)->builtin_uint64;
4950 append_composite_type_field (t, "u64", elem);
4951 elem = builtin_type (gdbarch)->builtin_float;
4952 append_composite_type_field (t, "f32", init_vector_type (elem, 2));
4953 elem = builtin_type (gdbarch)->builtin_double;
4954 append_composite_type_field (t, "f64", elem);
4956 t->set_is_vector (true);
4957 t->set_name ("neon_d");
4958 tdep->neon_double_type = t;
4961 return tdep->neon_double_type;
4964 /* FIXME: The vector types are not correctly ordered on big-endian
4965 targets. Just as s0 is the low bits of d0, d0[0] is also the low
4966 bits of d0 - regardless of what unit size is being held in d0. So
4967 the offset of the first uint8 in d0 is 7, but the offset of the
4968 first float is 4. This code works as-is for little-endian
4969 targets. */
4971 static struct type *
4972 arm_neon_quad_type (struct gdbarch *gdbarch)
4974 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
4976 if (tdep->neon_quad_type == NULL)
4978 struct type *t, *elem;
4980 t = arch_composite_type (gdbarch, "__gdb_builtin_type_neon_q",
4981 TYPE_CODE_UNION);
4982 elem = builtin_type (gdbarch)->builtin_uint8;
4983 append_composite_type_field (t, "u8", init_vector_type (elem, 16));
4984 elem = builtin_type (gdbarch)->builtin_uint16;
4985 append_composite_type_field (t, "u16", init_vector_type (elem, 8));
4986 elem = builtin_type (gdbarch)->builtin_uint32;
4987 append_composite_type_field (t, "u32", init_vector_type (elem, 4));
4988 elem = builtin_type (gdbarch)->builtin_uint64;
4989 append_composite_type_field (t, "u64", init_vector_type (elem, 2));
4990 elem = builtin_type (gdbarch)->builtin_float;
4991 append_composite_type_field (t, "f32", init_vector_type (elem, 4));
4992 elem = builtin_type (gdbarch)->builtin_double;
4993 append_composite_type_field (t, "f64", init_vector_type (elem, 2));
4995 t->set_is_vector (true);
4996 t->set_name ("neon_q");
4997 tdep->neon_quad_type = t;
5000 return tdep->neon_quad_type;
5003 /* Return true if REGNUM is a Q pseudo register. Return false
5004 otherwise.
5006 REGNUM is the raw register number and not a pseudo-relative register
5007 number. */
5009 static bool
5010 is_q_pseudo (struct gdbarch *gdbarch, int regnum)
5012 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
5014 /* Q pseudo registers are available for both NEON (Q0~Q15) and
5015 MVE (Q0~Q7) features. */
5016 if (tdep->have_q_pseudos
5017 && regnum >= tdep->q_pseudo_base
5018 && regnum < (tdep->q_pseudo_base + tdep->q_pseudo_count))
5019 return true;
5021 return false;
5024 /* Return true if REGNUM is a VFP S pseudo register. Return false
5025 otherwise.
5027 REGNUM is the raw register number and not a pseudo-relative register
5028 number. */
5030 static bool
5031 is_s_pseudo (struct gdbarch *gdbarch, int regnum)
5033 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
5035 if (tdep->have_s_pseudos
5036 && regnum >= tdep->s_pseudo_base
5037 && regnum < (tdep->s_pseudo_base + tdep->s_pseudo_count))
5038 return true;
5040 return false;
5043 /* Return true if REGNUM is a MVE pseudo register (P0). Return false
5044 otherwise.
5046 REGNUM is the raw register number and not a pseudo-relative register
5047 number. */
5049 static bool
5050 is_mve_pseudo (struct gdbarch *gdbarch, int regnum)
5052 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
5054 if (tdep->have_mve
5055 && regnum >= tdep->mve_pseudo_base
5056 && regnum < tdep->mve_pseudo_base + tdep->mve_pseudo_count)
5057 return true;
5059 return false;
5062 /* Return true if REGNUM is a PACBTI pseudo register (ra_auth_code). Return
5063 false otherwise.
5065 REGNUM is the raw register number and not a pseudo-relative register
5066 number. */
5068 static bool
5069 is_pacbti_pseudo (struct gdbarch *gdbarch, int regnum)
5071 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
5073 if (tdep->have_pacbti
5074 && regnum >= tdep->pacbti_pseudo_base
5075 && regnum < tdep->pacbti_pseudo_base + tdep->pacbti_pseudo_count)
5076 return true;
5078 return false;
5081 /* Return the GDB type object for the "standard" data type of data in
5082 register N. */
5084 static struct type *
5085 arm_register_type (struct gdbarch *gdbarch, int regnum)
5087 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
5089 if (is_s_pseudo (gdbarch, regnum))
5090 return builtin_type (gdbarch)->builtin_float;
5092 if (is_q_pseudo (gdbarch, regnum))
5093 return arm_neon_quad_type (gdbarch);
5095 if (is_mve_pseudo (gdbarch, regnum))
5096 return builtin_type (gdbarch)->builtin_int16;
5098 if (is_pacbti_pseudo (gdbarch, regnum))
5099 return builtin_type (gdbarch)->builtin_uint32;
5101 /* If the target description has register information, we are only
5102 in this function so that we can override the types of
5103 double-precision registers for NEON. */
5104 if (tdesc_has_registers (gdbarch_target_desc (gdbarch)))
5106 struct type *t = tdesc_register_type (gdbarch, regnum);
5108 if (regnum >= ARM_D0_REGNUM && regnum < ARM_D0_REGNUM + 32
5109 && t->code () == TYPE_CODE_FLT
5110 && tdep->have_neon)
5111 return arm_neon_double_type (gdbarch);
5112 else
5113 return t;
5116 if (regnum >= ARM_F0_REGNUM && regnum < ARM_F0_REGNUM + NUM_FREGS)
5118 if (!tdep->have_fpa_registers)
5119 return builtin_type (gdbarch)->builtin_void;
5121 return arm_ext_type (gdbarch);
5123 else if (regnum == ARM_SP_REGNUM)
5124 return builtin_type (gdbarch)->builtin_data_ptr;
5125 else if (regnum == ARM_PC_REGNUM)
5126 return builtin_type (gdbarch)->builtin_func_ptr;
5127 else if (regnum >= ARRAY_SIZE (arm_register_names))
5128 /* These registers are only supported on targets which supply
5129 an XML description. */
5130 return builtin_type (gdbarch)->builtin_int0;
5131 else
5132 return builtin_type (gdbarch)->builtin_uint32;
5135 /* Map a DWARF register REGNUM onto the appropriate GDB register
5136 number. */
5138 static int
5139 arm_dwarf_reg_to_regnum (struct gdbarch *gdbarch, int reg)
5141 /* Core integer regs. */
5142 if (reg >= 0 && reg <= 15)
5143 return reg;
5145 /* Legacy FPA encoding. These were once used in a way which
5146 overlapped with VFP register numbering, so their use is
5147 discouraged, but GDB doesn't support the ARM toolchain
5148 which used them for VFP. */
5149 if (reg >= 16 && reg <= 23)
5150 return ARM_F0_REGNUM + reg - 16;
5152 /* New assignments for the FPA registers. */
5153 if (reg >= 96 && reg <= 103)
5154 return ARM_F0_REGNUM + reg - 96;
5156 /* WMMX register assignments. */
5157 if (reg >= 104 && reg <= 111)
5158 return ARM_WCGR0_REGNUM + reg - 104;
5160 if (reg >= 112 && reg <= 127)
5161 return ARM_WR0_REGNUM + reg - 112;
5163 /* PACBTI register containing the Pointer Authentication Code. */
5164 if (reg == ARM_DWARF_RA_AUTH_CODE)
5166 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
5168 if (tdep->have_pacbti)
5169 return tdep->pacbti_pseudo_base;
5171 return -1;
5174 if (reg >= 192 && reg <= 199)
5175 return ARM_WC0_REGNUM + reg - 192;
5177 /* VFP v2 registers. A double precision value is actually
5178 in d1 rather than s2, but the ABI only defines numbering
5179 for the single precision registers. This will "just work"
5180 in GDB for little endian targets (we'll read eight bytes,
5181 starting in s0 and then progressing to s1), but will be
5182 reversed on big endian targets with VFP. This won't
5183 be a problem for the new Neon quad registers; you're supposed
5184 to use DW_OP_piece for those. */
5185 if (reg >= 64 && reg <= 95)
5187 char name_buf[4];
5189 xsnprintf (name_buf, sizeof (name_buf), "s%d", reg - 64);
5190 return user_reg_map_name_to_regnum (gdbarch, name_buf,
5191 strlen (name_buf));
5194 /* VFP v3 / Neon registers. This range is also used for VFP v2
5195 registers, except that it now describes d0 instead of s0. */
5196 if (reg >= 256 && reg <= 287)
5198 char name_buf[4];
5200 xsnprintf (name_buf, sizeof (name_buf), "d%d", reg - 256);
5201 return user_reg_map_name_to_regnum (gdbarch, name_buf,
5202 strlen (name_buf));
5205 return -1;
5208 /* Map GDB internal REGNUM onto the Arm simulator register numbers. */
5209 static int
5210 arm_register_sim_regno (struct gdbarch *gdbarch, int regnum)
5212 int reg = regnum;
5213 gdb_assert (reg >= 0 && reg < gdbarch_num_regs (gdbarch));
5215 if (regnum >= ARM_WR0_REGNUM && regnum <= ARM_WR15_REGNUM)
5216 return regnum - ARM_WR0_REGNUM + SIM_ARM_IWMMXT_COP0R0_REGNUM;
5218 if (regnum >= ARM_WC0_REGNUM && regnum <= ARM_WC7_REGNUM)
5219 return regnum - ARM_WC0_REGNUM + SIM_ARM_IWMMXT_COP1R0_REGNUM;
5221 if (regnum >= ARM_WCGR0_REGNUM && regnum <= ARM_WCGR7_REGNUM)
5222 return regnum - ARM_WCGR0_REGNUM + SIM_ARM_IWMMXT_COP1R8_REGNUM;
5224 if (reg < NUM_GREGS)
5225 return SIM_ARM_R0_REGNUM + reg;
5226 reg -= NUM_GREGS;
5228 if (reg < NUM_FREGS)
5229 return SIM_ARM_FP0_REGNUM + reg;
5230 reg -= NUM_FREGS;
5232 if (reg < NUM_SREGS)
5233 return SIM_ARM_FPS_REGNUM + reg;
5234 reg -= NUM_SREGS;
5236 internal_error (_("Bad REGNUM %d"), regnum);
5239 static const unsigned char op_lit0 = DW_OP_lit0;
5241 static void
5242 arm_dwarf2_frame_init_reg (struct gdbarch *gdbarch, int regnum,
5243 struct dwarf2_frame_state_reg *reg,
5244 const frame_info_ptr &this_frame)
5246 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
5248 if (is_pacbti_pseudo (gdbarch, regnum))
5250 /* Initialize RA_AUTH_CODE to zero. */
5251 reg->how = DWARF2_FRAME_REG_SAVED_VAL_EXP;
5252 reg->loc.exp.start = &op_lit0;
5253 reg->loc.exp.len = 1;
5254 return;
5257 if (regnum == ARM_PC_REGNUM || regnum == ARM_PS_REGNUM)
5259 reg->how = DWARF2_FRAME_REG_FN;
5260 reg->loc.fn = arm_dwarf2_prev_register;
5262 else if (regnum == ARM_SP_REGNUM)
5263 reg->how = DWARF2_FRAME_REG_CFA;
5264 else if (arm_is_alternative_sp_register (tdep, regnum))
5266 /* Handle the alternative SP registers on Cortex-M. */
5267 reg->how = DWARF2_FRAME_REG_FN;
5268 reg->loc.fn = arm_dwarf2_prev_register;
5272 /* Given BUF, which is OLD_LEN bytes ending at ENDADDR, expand
5273 the buffer to be NEW_LEN bytes ending at ENDADDR. Return
5274 NULL if an error occurs. BUF is freed. */
5276 static gdb_byte *
5277 extend_buffer_earlier (gdb_byte *buf, CORE_ADDR endaddr,
5278 int old_len, int new_len)
5280 gdb_byte *new_buf;
5281 int bytes_to_read = new_len - old_len;
5283 new_buf = (gdb_byte *) xmalloc (new_len);
5284 memcpy (new_buf + bytes_to_read, buf, old_len);
5285 xfree (buf);
5286 if (target_read_code (endaddr - new_len, new_buf, bytes_to_read) != 0)
5288 xfree (new_buf);
5289 return NULL;
5291 return new_buf;
5294 /* An IT block is at most the 2-byte IT instruction followed by
5295 four 4-byte instructions. The furthest back we must search to
5296 find an IT block that affects the current instruction is thus
5297 2 + 3 * 4 == 14 bytes. */
5298 #define MAX_IT_BLOCK_PREFIX 14
5300 /* Use a quick scan if there are more than this many bytes of
5301 code. */
5302 #define IT_SCAN_THRESHOLD 32
5304 /* Adjust a breakpoint's address to move breakpoints out of IT blocks.
5305 A breakpoint in an IT block may not be hit, depending on the
5306 condition flags. */
5307 static CORE_ADDR
5308 arm_adjust_breakpoint_address (struct gdbarch *gdbarch, CORE_ADDR bpaddr)
5310 gdb_byte *buf;
5311 char map_type;
5312 CORE_ADDR boundary, func_start;
5313 int buf_len;
5314 enum bfd_endian order = gdbarch_byte_order_for_code (gdbarch);
5315 int i, any, last_it, last_it_count;
5316 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
5318 /* If we are using BKPT breakpoints, none of this is necessary. */
5319 if (tdep->thumb2_breakpoint == NULL)
5320 return bpaddr;
5322 /* ARM mode does not have this problem. */
5323 if (!arm_pc_is_thumb (gdbarch, bpaddr))
5324 return bpaddr;
5326 /* We are setting a breakpoint in Thumb code that could potentially
5327 contain an IT block. The first step is to find how much Thumb
5328 code there is; we do not need to read outside of known Thumb
5329 sequences. */
5330 map_type = arm_find_mapping_symbol (bpaddr, &boundary);
5331 if (map_type == 0)
5332 /* Thumb-2 code must have mapping symbols to have a chance. */
5333 return bpaddr;
5335 bpaddr = gdbarch_addr_bits_remove (gdbarch, bpaddr);
5337 if (find_pc_partial_function (bpaddr, NULL, &func_start, NULL))
5339 func_start = gdbarch_addr_bits_remove (gdbarch, func_start);
5340 if (func_start > boundary)
5341 boundary = func_start;
5344 /* Search for a candidate IT instruction. We have to do some fancy
5345 footwork to distinguish a real IT instruction from the second
5346 half of a 32-bit instruction, but there is no need for that if
5347 there's no candidate. */
5348 buf_len = std::min (bpaddr - boundary, (CORE_ADDR) MAX_IT_BLOCK_PREFIX);
5349 if (buf_len == 0)
5350 /* No room for an IT instruction. */
5351 return bpaddr;
5353 buf = (gdb_byte *) xmalloc (buf_len);
5354 if (target_read_code (bpaddr - buf_len, buf, buf_len) != 0)
5355 return bpaddr;
5356 any = 0;
5357 for (i = 0; i < buf_len; i += 2)
5359 unsigned short inst1 = extract_unsigned_integer (&buf[i], 2, order);
5360 if ((inst1 & 0xff00) == 0xbf00 && (inst1 & 0x000f) != 0)
5362 any = 1;
5363 break;
5367 if (any == 0)
5369 xfree (buf);
5370 return bpaddr;
5373 /* OK, the code bytes before this instruction contain at least one
5374 halfword which resembles an IT instruction. We know that it's
5375 Thumb code, but there are still two possibilities. Either the
5376 halfword really is an IT instruction, or it is the second half of
5377 a 32-bit Thumb instruction. The only way we can tell is to
5378 scan forwards from a known instruction boundary. */
5379 if (bpaddr - boundary > IT_SCAN_THRESHOLD)
5381 int definite;
5383 /* There's a lot of code before this instruction. Start with an
5384 optimistic search; it's easy to recognize halfwords that can
5385 not be the start of a 32-bit instruction, and use that to
5386 lock on to the instruction boundaries. */
5387 buf = extend_buffer_earlier (buf, bpaddr, buf_len, IT_SCAN_THRESHOLD);
5388 if (buf == NULL)
5389 return bpaddr;
5390 buf_len = IT_SCAN_THRESHOLD;
5392 definite = 0;
5393 for (i = 0; i < buf_len - sizeof (buf) && ! definite; i += 2)
5395 unsigned short inst1 = extract_unsigned_integer (&buf[i], 2, order);
5396 if (thumb_insn_size (inst1) == 2)
5398 definite = 1;
5399 break;
5403 /* At this point, if DEFINITE, BUF[I] is the first place we
5404 are sure that we know the instruction boundaries, and it is far
5405 enough from BPADDR that we could not miss an IT instruction
5406 affecting BPADDR. If ! DEFINITE, give up - start from a
5407 known boundary. */
5408 if (! definite)
5410 buf = extend_buffer_earlier (buf, bpaddr, buf_len,
5411 bpaddr - boundary);
5412 if (buf == NULL)
5413 return bpaddr;
5414 buf_len = bpaddr - boundary;
5415 i = 0;
5418 else
5420 buf = extend_buffer_earlier (buf, bpaddr, buf_len, bpaddr - boundary);
5421 if (buf == NULL)
5422 return bpaddr;
5423 buf_len = bpaddr - boundary;
5424 i = 0;
5427 /* Scan forwards. Find the last IT instruction before BPADDR. */
5428 last_it = -1;
5429 last_it_count = 0;
5430 while (i < buf_len)
5432 unsigned short inst1 = extract_unsigned_integer (&buf[i], 2, order);
5433 last_it_count--;
5434 if ((inst1 & 0xff00) == 0xbf00 && (inst1 & 0x000f) != 0)
5436 last_it = i;
5437 if (inst1 & 0x0001)
5438 last_it_count = 4;
5439 else if (inst1 & 0x0002)
5440 last_it_count = 3;
5441 else if (inst1 & 0x0004)
5442 last_it_count = 2;
5443 else
5444 last_it_count = 1;
5446 i += thumb_insn_size (inst1);
5449 xfree (buf);
5451 if (last_it == -1)
5452 /* There wasn't really an IT instruction after all. */
5453 return bpaddr;
5455 if (last_it_count < 1)
5456 /* It was too far away. */
5457 return bpaddr;
5459 /* This really is a trouble spot. Move the breakpoint to the IT
5460 instruction. */
5461 return bpaddr - buf_len + last_it;
5464 /* ARM displaced stepping support.
5466 Generally ARM displaced stepping works as follows:
5468 1. When an instruction is to be single-stepped, it is first decoded by
5469 arm_process_displaced_insn. Depending on the type of instruction, it is
5470 then copied to a scratch location, possibly in a modified form. The
5471 copy_* set of functions performs such modification, as necessary. A
5472 breakpoint is placed after the modified instruction in the scratch space
5473 to return control to GDB. Note in particular that instructions which
5474 modify the PC will no longer do so after modification.
5476 2. The instruction is single-stepped, by setting the PC to the scratch
5477 location address, and resuming. Control returns to GDB when the
5478 breakpoint is hit.
5480 3. A cleanup function (cleanup_*) is called corresponding to the copy_*
5481 function used for the current instruction. This function's job is to
5482 put the CPU/memory state back to what it would have been if the
5483 instruction had been executed unmodified in its original location. */
5485 /* NOP instruction (mov r0, r0). */
5486 #define ARM_NOP 0xe1a00000
5487 #define THUMB_NOP 0x4600
5489 /* Helper for register reads for displaced stepping. In particular, this
5490 returns the PC as it would be seen by the instruction at its original
5491 location. */
5493 ULONGEST
5494 displaced_read_reg (regcache *regs, arm_displaced_step_copy_insn_closure *dsc,
5495 int regno)
5497 ULONGEST ret;
5498 CORE_ADDR from = dsc->insn_addr;
5500 if (regno == ARM_PC_REGNUM)
5502 /* Compute pipeline offset:
5503 - When executing an ARM instruction, PC reads as the address of the
5504 current instruction plus 8.
5505 - When executing a Thumb instruction, PC reads as the address of the
5506 current instruction plus 4. */
5508 if (!dsc->is_thumb)
5509 from += 8;
5510 else
5511 from += 4;
5513 displaced_debug_printf ("read pc value %.8lx",
5514 (unsigned long) from);
5515 return (ULONGEST) from;
5517 else
5519 regcache_cooked_read_unsigned (regs, regno, &ret);
5521 displaced_debug_printf ("read r%d value %.8lx",
5522 regno, (unsigned long) ret);
5524 return ret;
5528 static int
5529 displaced_in_arm_mode (struct regcache *regs)
5531 ULONGEST ps;
5532 ULONGEST t_bit = arm_psr_thumb_bit (regs->arch ());
5534 regcache_cooked_read_unsigned (regs, ARM_PS_REGNUM, &ps);
5536 return (ps & t_bit) == 0;
5539 /* Write to the PC as from a branch instruction. */
5541 static void
5542 branch_write_pc (regcache *regs, arm_displaced_step_copy_insn_closure *dsc,
5543 ULONGEST val)
5545 if (!dsc->is_thumb)
5546 /* Note: If bits 0/1 are set, this branch would be unpredictable for
5547 architecture versions < 6. */
5548 regcache_cooked_write_unsigned (regs, ARM_PC_REGNUM,
5549 val & ~(ULONGEST) 0x3);
5550 else
5551 regcache_cooked_write_unsigned (regs, ARM_PC_REGNUM,
5552 val & ~(ULONGEST) 0x1);
5555 /* Write to the PC as from a branch-exchange instruction. */
5557 static void
5558 bx_write_pc (struct regcache *regs, ULONGEST val)
5560 ULONGEST ps;
5561 ULONGEST t_bit = arm_psr_thumb_bit (regs->arch ());
5563 regcache_cooked_read_unsigned (regs, ARM_PS_REGNUM, &ps);
5565 if ((val & 1) == 1)
5567 regcache_cooked_write_unsigned (regs, ARM_PS_REGNUM, ps | t_bit);
5568 regcache_cooked_write_unsigned (regs, ARM_PC_REGNUM, val & 0xfffffffe);
5570 else if ((val & 2) == 0)
5572 regcache_cooked_write_unsigned (regs, ARM_PS_REGNUM, ps & ~t_bit);
5573 regcache_cooked_write_unsigned (regs, ARM_PC_REGNUM, val);
5575 else
5577 /* Unpredictable behaviour. Try to do something sensible (switch to ARM
5578 mode, align dest to 4 bytes). */
5579 warning (_("Single-stepping BX to non-word-aligned ARM instruction."));
5580 regcache_cooked_write_unsigned (regs, ARM_PS_REGNUM, ps & ~t_bit);
5581 regcache_cooked_write_unsigned (regs, ARM_PC_REGNUM, val & 0xfffffffc);
5585 /* Write to the PC as if from a load instruction. */
5587 static void
5588 load_write_pc (regcache *regs, arm_displaced_step_copy_insn_closure *dsc,
5589 ULONGEST val)
5591 if (DISPLACED_STEPPING_ARCH_VERSION >= 5)
5592 bx_write_pc (regs, val);
5593 else
5594 branch_write_pc (regs, dsc, val);
5597 /* Write to the PC as if from an ALU instruction. */
5599 static void
5600 alu_write_pc (regcache *regs, arm_displaced_step_copy_insn_closure *dsc,
5601 ULONGEST val)
5603 if (DISPLACED_STEPPING_ARCH_VERSION >= 7 && !dsc->is_thumb)
5604 bx_write_pc (regs, val);
5605 else
5606 branch_write_pc (regs, dsc, val);
5609 /* Helper for writing to registers for displaced stepping. Writing to the PC
5610 has a varying effects depending on the instruction which does the write:
5611 this is controlled by the WRITE_PC argument. */
5613 void
5614 displaced_write_reg (regcache *regs, arm_displaced_step_copy_insn_closure *dsc,
5615 int regno, ULONGEST val, enum pc_write_style write_pc)
5617 if (regno == ARM_PC_REGNUM)
5619 displaced_debug_printf ("writing pc %.8lx", (unsigned long) val);
5621 switch (write_pc)
5623 case BRANCH_WRITE_PC:
5624 branch_write_pc (regs, dsc, val);
5625 break;
5627 case BX_WRITE_PC:
5628 bx_write_pc (regs, val);
5629 break;
5631 case LOAD_WRITE_PC:
5632 load_write_pc (regs, dsc, val);
5633 break;
5635 case ALU_WRITE_PC:
5636 alu_write_pc (regs, dsc, val);
5637 break;
5639 case CANNOT_WRITE_PC:
5640 warning (_("Instruction wrote to PC in an unexpected way when "
5641 "single-stepping"));
5642 break;
5644 default:
5645 internal_error (_("Invalid argument to displaced_write_reg"));
5648 dsc->wrote_to_pc = 1;
5650 else
5652 displaced_debug_printf ("writing r%d value %.8lx",
5653 regno, (unsigned long) val);
5654 regcache_cooked_write_unsigned (regs, regno, val);
5658 /* This function is used to concisely determine if an instruction INSN
5659 references PC. Register fields of interest in INSN should have the
5660 corresponding fields of BITMASK set to 0b1111. The function
5661 returns return 1 if any of these fields in INSN reference the PC
5662 (also 0b1111, r15), else it returns 0. */
5664 static int
5665 insn_references_pc (uint32_t insn, uint32_t bitmask)
5667 uint32_t lowbit = 1;
5669 while (bitmask != 0)
5671 uint32_t mask;
5673 for (; lowbit && (bitmask & lowbit) == 0; lowbit <<= 1)
5676 if (!lowbit)
5677 break;
5679 mask = lowbit * 0xf;
5681 if ((insn & mask) == mask)
5682 return 1;
5684 bitmask &= ~mask;
5687 return 0;
5690 /* The simplest copy function. Many instructions have the same effect no
5691 matter what address they are executed at: in those cases, use this. */
5693 static int
5694 arm_copy_unmodified (struct gdbarch *gdbarch, uint32_t insn, const char *iname,
5695 arm_displaced_step_copy_insn_closure *dsc)
5697 displaced_debug_printf ("copying insn %.8lx, opcode/class '%s' unmodified",
5698 (unsigned long) insn, iname);
5700 dsc->modinsn[0] = insn;
5702 return 0;
5705 static int
5706 thumb_copy_unmodified_32bit (struct gdbarch *gdbarch, uint16_t insn1,
5707 uint16_t insn2, const char *iname,
5708 arm_displaced_step_copy_insn_closure *dsc)
5710 displaced_debug_printf ("copying insn %.4x %.4x, opcode/class '%s' "
5711 "unmodified", insn1, insn2, iname);
5713 dsc->modinsn[0] = insn1;
5714 dsc->modinsn[1] = insn2;
5715 dsc->numinsns = 2;
5717 return 0;
5720 /* Copy 16-bit Thumb(Thumb and 16-bit Thumb-2) instruction without any
5721 modification. */
5722 static int
5723 thumb_copy_unmodified_16bit (struct gdbarch *gdbarch, uint16_t insn,
5724 const char *iname,
5725 arm_displaced_step_copy_insn_closure *dsc)
5727 displaced_debug_printf ("copying insn %.4x, opcode/class '%s' unmodified",
5728 insn, iname);
5730 dsc->modinsn[0] = insn;
5732 return 0;
5735 /* Preload instructions with immediate offset. */
5737 static void
5738 cleanup_preload (struct gdbarch *gdbarch, regcache *regs,
5739 arm_displaced_step_copy_insn_closure *dsc)
5741 displaced_write_reg (regs, dsc, 0, dsc->tmp[0], CANNOT_WRITE_PC);
5742 if (!dsc->u.preload.immed)
5743 displaced_write_reg (regs, dsc, 1, dsc->tmp[1], CANNOT_WRITE_PC);
5746 static void
5747 install_preload (struct gdbarch *gdbarch, struct regcache *regs,
5748 arm_displaced_step_copy_insn_closure *dsc, unsigned int rn)
5750 ULONGEST rn_val;
5751 /* Preload instructions:
5753 {pli/pld} [rn, #+/-imm]
5755 {pli/pld} [r0, #+/-imm]. */
5757 dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
5758 rn_val = displaced_read_reg (regs, dsc, rn);
5759 displaced_write_reg (regs, dsc, 0, rn_val, CANNOT_WRITE_PC);
5760 dsc->u.preload.immed = 1;
5762 dsc->cleanup = &cleanup_preload;
5765 static int
5766 arm_copy_preload (struct gdbarch *gdbarch, uint32_t insn, struct regcache *regs,
5767 arm_displaced_step_copy_insn_closure *dsc)
5769 unsigned int rn = bits (insn, 16, 19);
5771 if (!insn_references_pc (insn, 0x000f0000ul))
5772 return arm_copy_unmodified (gdbarch, insn, "preload", dsc);
5774 displaced_debug_printf ("copying preload insn %.8lx", (unsigned long) insn);
5776 dsc->modinsn[0] = insn & 0xfff0ffff;
5778 install_preload (gdbarch, regs, dsc, rn);
5780 return 0;
5783 static int
5784 thumb2_copy_preload (struct gdbarch *gdbarch, uint16_t insn1, uint16_t insn2,
5785 regcache *regs, arm_displaced_step_copy_insn_closure *dsc)
5787 unsigned int rn = bits (insn1, 0, 3);
5788 unsigned int u_bit = bit (insn1, 7);
5789 int imm12 = bits (insn2, 0, 11);
5790 ULONGEST pc_val;
5792 if (rn != ARM_PC_REGNUM)
5793 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2, "preload", dsc);
5795 /* PC is only allowed to use in PLI (immediate,literal) Encoding T3, and
5796 PLD (literal) Encoding T1. */
5797 displaced_debug_printf ("copying pld/pli pc (0x%x) %c imm12 %.4x",
5798 (unsigned int) dsc->insn_addr, u_bit ? '+' : '-',
5799 imm12);
5801 if (!u_bit)
5802 imm12 = -1 * imm12;
5804 /* Rewrite instruction {pli/pld} PC imm12 into:
5805 Prepare: tmp[0] <- r0, tmp[1] <- r1, r0 <- pc, r1 <- imm12
5807 {pli/pld} [r0, r1]
5809 Cleanup: r0 <- tmp[0], r1 <- tmp[1]. */
5811 dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
5812 dsc->tmp[1] = displaced_read_reg (regs, dsc, 1);
5814 pc_val = displaced_read_reg (regs, dsc, ARM_PC_REGNUM);
5816 displaced_write_reg (regs, dsc, 0, pc_val, CANNOT_WRITE_PC);
5817 displaced_write_reg (regs, dsc, 1, imm12, CANNOT_WRITE_PC);
5818 dsc->u.preload.immed = 0;
5820 /* {pli/pld} [r0, r1] */
5821 dsc->modinsn[0] = insn1 & 0xfff0;
5822 dsc->modinsn[1] = 0xf001;
5823 dsc->numinsns = 2;
5825 dsc->cleanup = &cleanup_preload;
5826 return 0;
5829 /* Preload instructions with register offset. */
5831 static void
5832 install_preload_reg(struct gdbarch *gdbarch, struct regcache *regs,
5833 arm_displaced_step_copy_insn_closure *dsc, unsigned int rn,
5834 unsigned int rm)
5836 ULONGEST rn_val, rm_val;
5838 /* Preload register-offset instructions:
5840 {pli/pld} [rn, rm {, shift}]
5842 {pli/pld} [r0, r1 {, shift}]. */
5844 dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
5845 dsc->tmp[1] = displaced_read_reg (regs, dsc, 1);
5846 rn_val = displaced_read_reg (regs, dsc, rn);
5847 rm_val = displaced_read_reg (regs, dsc, rm);
5848 displaced_write_reg (regs, dsc, 0, rn_val, CANNOT_WRITE_PC);
5849 displaced_write_reg (regs, dsc, 1, rm_val, CANNOT_WRITE_PC);
5850 dsc->u.preload.immed = 0;
5852 dsc->cleanup = &cleanup_preload;
5855 static int
5856 arm_copy_preload_reg (struct gdbarch *gdbarch, uint32_t insn,
5857 struct regcache *regs,
5858 arm_displaced_step_copy_insn_closure *dsc)
5860 unsigned int rn = bits (insn, 16, 19);
5861 unsigned int rm = bits (insn, 0, 3);
5864 if (!insn_references_pc (insn, 0x000f000ful))
5865 return arm_copy_unmodified (gdbarch, insn, "preload reg", dsc);
5867 displaced_debug_printf ("copying preload insn %.8lx",
5868 (unsigned long) insn);
5870 dsc->modinsn[0] = (insn & 0xfff0fff0) | 0x1;
5872 install_preload_reg (gdbarch, regs, dsc, rn, rm);
5873 return 0;
5876 /* Copy/cleanup coprocessor load and store instructions. */
5878 static void
5879 cleanup_copro_load_store (struct gdbarch *gdbarch,
5880 struct regcache *regs,
5881 arm_displaced_step_copy_insn_closure *dsc)
5883 ULONGEST rn_val = displaced_read_reg (regs, dsc, 0);
5885 displaced_write_reg (regs, dsc, 0, dsc->tmp[0], CANNOT_WRITE_PC);
5887 if (dsc->u.ldst.writeback)
5888 displaced_write_reg (regs, dsc, dsc->u.ldst.rn, rn_val, LOAD_WRITE_PC);
5891 static void
5892 install_copro_load_store (struct gdbarch *gdbarch, struct regcache *regs,
5893 arm_displaced_step_copy_insn_closure *dsc,
5894 int writeback, unsigned int rn)
5896 ULONGEST rn_val;
5898 /* Coprocessor load/store instructions:
5900 {stc/stc2} [<Rn>, #+/-imm] (and other immediate addressing modes)
5902 {stc/stc2} [r0, #+/-imm].
5904 ldc/ldc2 are handled identically. */
5906 dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
5907 rn_val = displaced_read_reg (regs, dsc, rn);
5908 /* PC should be 4-byte aligned. */
5909 rn_val = rn_val & 0xfffffffc;
5910 displaced_write_reg (regs, dsc, 0, rn_val, CANNOT_WRITE_PC);
5912 dsc->u.ldst.writeback = writeback;
5913 dsc->u.ldst.rn = rn;
5915 dsc->cleanup = &cleanup_copro_load_store;
5918 static int
5919 arm_copy_copro_load_store (struct gdbarch *gdbarch, uint32_t insn,
5920 struct regcache *regs,
5921 arm_displaced_step_copy_insn_closure *dsc)
5923 unsigned int rn = bits (insn, 16, 19);
5925 if (!insn_references_pc (insn, 0x000f0000ul))
5926 return arm_copy_unmodified (gdbarch, insn, "copro load/store", dsc);
5928 displaced_debug_printf ("copying coprocessor load/store insn %.8lx",
5929 (unsigned long) insn);
5931 dsc->modinsn[0] = insn & 0xfff0ffff;
5933 install_copro_load_store (gdbarch, regs, dsc, bit (insn, 25), rn);
5935 return 0;
5938 static int
5939 thumb2_copy_copro_load_store (struct gdbarch *gdbarch, uint16_t insn1,
5940 uint16_t insn2, struct regcache *regs,
5941 arm_displaced_step_copy_insn_closure *dsc)
5943 unsigned int rn = bits (insn1, 0, 3);
5945 if (rn != ARM_PC_REGNUM)
5946 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
5947 "copro load/store", dsc);
5949 displaced_debug_printf ("copying coprocessor load/store insn %.4x%.4x",
5950 insn1, insn2);
5952 dsc->modinsn[0] = insn1 & 0xfff0;
5953 dsc->modinsn[1] = insn2;
5954 dsc->numinsns = 2;
5956 /* This function is called for copying instruction LDC/LDC2/VLDR, which
5957 doesn't support writeback, so pass 0. */
5958 install_copro_load_store (gdbarch, regs, dsc, 0, rn);
5960 return 0;
5963 /* Clean up branch instructions (actually perform the branch, by setting
5964 PC). */
5966 static void
5967 cleanup_branch (struct gdbarch *gdbarch, struct regcache *regs,
5968 arm_displaced_step_copy_insn_closure *dsc)
5970 uint32_t status = displaced_read_reg (regs, dsc, ARM_PS_REGNUM);
5971 int branch_taken = condition_true (dsc->u.branch.cond, status);
5972 enum pc_write_style write_pc = dsc->u.branch.exchange
5973 ? BX_WRITE_PC : BRANCH_WRITE_PC;
5975 if (!branch_taken)
5976 return;
5978 if (dsc->u.branch.link)
5980 /* The value of LR should be the next insn of current one. In order
5981 not to confuse logic handling later insn `bx lr', if current insn mode
5982 is Thumb, the bit 0 of LR value should be set to 1. */
5983 ULONGEST next_insn_addr = dsc->insn_addr + dsc->insn_size;
5985 if (dsc->is_thumb)
5986 next_insn_addr |= 0x1;
5988 displaced_write_reg (regs, dsc, ARM_LR_REGNUM, next_insn_addr,
5989 CANNOT_WRITE_PC);
5992 displaced_write_reg (regs, dsc, ARM_PC_REGNUM, dsc->u.branch.dest, write_pc);
5995 /* Copy B/BL/BLX instructions with immediate destinations. */
5997 static void
5998 install_b_bl_blx (struct gdbarch *gdbarch, struct regcache *regs,
5999 arm_displaced_step_copy_insn_closure *dsc,
6000 unsigned int cond, int exchange, int link, long offset)
6002 /* Implement "BL<cond> <label>" as:
6004 Preparation: cond <- instruction condition
6005 Insn: mov r0, r0 (nop)
6006 Cleanup: if (condition true) { r14 <- pc; pc <- label }.
6008 B<cond> similar, but don't set r14 in cleanup. */
6010 dsc->u.branch.cond = cond;
6011 dsc->u.branch.link = link;
6012 dsc->u.branch.exchange = exchange;
6014 dsc->u.branch.dest = dsc->insn_addr;
6015 if (link && exchange)
6016 /* For BLX, offset is computed from the Align (PC, 4). */
6017 dsc->u.branch.dest = dsc->u.branch.dest & 0xfffffffc;
6019 if (dsc->is_thumb)
6020 dsc->u.branch.dest += 4 + offset;
6021 else
6022 dsc->u.branch.dest += 8 + offset;
6024 dsc->cleanup = &cleanup_branch;
6026 static int
6027 arm_copy_b_bl_blx (struct gdbarch *gdbarch, uint32_t insn,
6028 regcache *regs, arm_displaced_step_copy_insn_closure *dsc)
6030 unsigned int cond = bits (insn, 28, 31);
6031 int exchange = (cond == 0xf);
6032 int link = exchange || bit (insn, 24);
6033 long offset;
6035 displaced_debug_printf ("copying %s immediate insn %.8lx",
6036 (exchange) ? "blx" : (link) ? "bl" : "b",
6037 (unsigned long) insn);
6038 if (exchange)
6039 /* For BLX, set bit 0 of the destination. The cleanup_branch function will
6040 then arrange the switch into Thumb mode. */
6041 offset = (bits (insn, 0, 23) << 2) | (bit (insn, 24) << 1) | 1;
6042 else
6043 offset = bits (insn, 0, 23) << 2;
6045 if (bit (offset, 25))
6046 offset = offset | ~0x3ffffff;
6048 dsc->modinsn[0] = ARM_NOP;
6050 install_b_bl_blx (gdbarch, regs, dsc, cond, exchange, link, offset);
6051 return 0;
6054 static int
6055 thumb2_copy_b_bl_blx (struct gdbarch *gdbarch, uint16_t insn1,
6056 uint16_t insn2, struct regcache *regs,
6057 arm_displaced_step_copy_insn_closure *dsc)
6059 int link = bit (insn2, 14);
6060 int exchange = link && !bit (insn2, 12);
6061 int cond = INST_AL;
6062 long offset = 0;
6063 int j1 = bit (insn2, 13);
6064 int j2 = bit (insn2, 11);
6065 int s = sbits (insn1, 10, 10);
6066 int i1 = !(j1 ^ bit (insn1, 10));
6067 int i2 = !(j2 ^ bit (insn1, 10));
6069 if (!link && !exchange) /* B */
6071 offset = (bits (insn2, 0, 10) << 1);
6072 if (bit (insn2, 12)) /* Encoding T4 */
6074 offset |= (bits (insn1, 0, 9) << 12)
6075 | (i2 << 22)
6076 | (i1 << 23)
6077 | (s << 24);
6078 cond = INST_AL;
6080 else /* Encoding T3 */
6082 offset |= (bits (insn1, 0, 5) << 12)
6083 | (j1 << 18)
6084 | (j2 << 19)
6085 | (s << 20);
6086 cond = bits (insn1, 6, 9);
6089 else
6091 offset = (bits (insn1, 0, 9) << 12);
6092 offset |= ((i2 << 22) | (i1 << 23) | (s << 24));
6093 offset |= exchange ?
6094 (bits (insn2, 1, 10) << 2) : (bits (insn2, 0, 10) << 1);
6097 displaced_debug_printf ("copying %s insn %.4x %.4x with offset %.8lx",
6098 link ? (exchange) ? "blx" : "bl" : "b",
6099 insn1, insn2, offset);
6101 dsc->modinsn[0] = THUMB_NOP;
6103 install_b_bl_blx (gdbarch, regs, dsc, cond, exchange, link, offset);
6104 return 0;
6107 /* Copy B Thumb instructions. */
6108 static int
6109 thumb_copy_b (struct gdbarch *gdbarch, uint16_t insn,
6110 arm_displaced_step_copy_insn_closure *dsc)
6112 unsigned int cond = 0;
6113 int offset = 0;
6114 unsigned short bit_12_15 = bits (insn, 12, 15);
6115 CORE_ADDR from = dsc->insn_addr;
6117 if (bit_12_15 == 0xd)
6119 /* offset = SignExtend (imm8:0, 32) */
6120 offset = sbits ((insn << 1), 0, 8);
6121 cond = bits (insn, 8, 11);
6123 else if (bit_12_15 == 0xe) /* Encoding T2 */
6125 offset = sbits ((insn << 1), 0, 11);
6126 cond = INST_AL;
6129 displaced_debug_printf ("copying b immediate insn %.4x with offset %d",
6130 insn, offset);
6132 dsc->u.branch.cond = cond;
6133 dsc->u.branch.link = 0;
6134 dsc->u.branch.exchange = 0;
6135 dsc->u.branch.dest = from + 4 + offset;
6137 dsc->modinsn[0] = THUMB_NOP;
6139 dsc->cleanup = &cleanup_branch;
6141 return 0;
6144 /* Copy BX/BLX with register-specified destinations. */
6146 static void
6147 install_bx_blx_reg (struct gdbarch *gdbarch, struct regcache *regs,
6148 arm_displaced_step_copy_insn_closure *dsc, int link,
6149 unsigned int cond, unsigned int rm)
6151 /* Implement {BX,BLX}<cond> <reg>" as:
6153 Preparation: cond <- instruction condition
6154 Insn: mov r0, r0 (nop)
6155 Cleanup: if (condition true) { r14 <- pc; pc <- dest; }.
6157 Don't set r14 in cleanup for BX. */
6159 dsc->u.branch.dest = displaced_read_reg (regs, dsc, rm);
6161 dsc->u.branch.cond = cond;
6162 dsc->u.branch.link = link;
6164 dsc->u.branch.exchange = 1;
6166 dsc->cleanup = &cleanup_branch;
6169 static int
6170 arm_copy_bx_blx_reg (struct gdbarch *gdbarch, uint32_t insn,
6171 regcache *regs, arm_displaced_step_copy_insn_closure *dsc)
6173 unsigned int cond = bits (insn, 28, 31);
6174 /* BX: x12xxx1x
6175 BLX: x12xxx3x. */
6176 int link = bit (insn, 5);
6177 unsigned int rm = bits (insn, 0, 3);
6179 displaced_debug_printf ("copying insn %.8lx", (unsigned long) insn);
6181 dsc->modinsn[0] = ARM_NOP;
6183 install_bx_blx_reg (gdbarch, regs, dsc, link, cond, rm);
6184 return 0;
6187 static int
6188 thumb_copy_bx_blx_reg (struct gdbarch *gdbarch, uint16_t insn,
6189 struct regcache *regs,
6190 arm_displaced_step_copy_insn_closure *dsc)
6192 int link = bit (insn, 7);
6193 unsigned int rm = bits (insn, 3, 6);
6195 displaced_debug_printf ("copying insn %.4x", (unsigned short) insn);
6197 dsc->modinsn[0] = THUMB_NOP;
6199 install_bx_blx_reg (gdbarch, regs, dsc, link, INST_AL, rm);
6201 return 0;
6205 /* Copy/cleanup arithmetic/logic instruction with immediate RHS. */
6207 static void
6208 cleanup_alu_imm (struct gdbarch *gdbarch,
6209 regcache *regs, arm_displaced_step_copy_insn_closure *dsc)
6211 ULONGEST rd_val = displaced_read_reg (regs, dsc, 0);
6212 displaced_write_reg (regs, dsc, 0, dsc->tmp[0], CANNOT_WRITE_PC);
6213 displaced_write_reg (regs, dsc, 1, dsc->tmp[1], CANNOT_WRITE_PC);
6214 displaced_write_reg (regs, dsc, dsc->rd, rd_val, ALU_WRITE_PC);
6217 static int
6218 arm_copy_alu_imm (struct gdbarch *gdbarch, uint32_t insn, struct regcache *regs,
6219 arm_displaced_step_copy_insn_closure *dsc)
6221 unsigned int rn = bits (insn, 16, 19);
6222 unsigned int rd = bits (insn, 12, 15);
6223 unsigned int op = bits (insn, 21, 24);
6224 int is_mov = (op == 0xd);
6225 ULONGEST rd_val, rn_val;
6227 if (!insn_references_pc (insn, 0x000ff000ul))
6228 return arm_copy_unmodified (gdbarch, insn, "ALU immediate", dsc);
6230 displaced_debug_printf ("copying immediate %s insn %.8lx",
6231 is_mov ? "move" : "ALU",
6232 (unsigned long) insn);
6234 /* Instruction is of form:
6236 <op><cond> rd, [rn,] #imm
6238 Rewrite as:
6240 Preparation: tmp1, tmp2 <- r0, r1;
6241 r0, r1 <- rd, rn
6242 Insn: <op><cond> r0, r1, #imm
6243 Cleanup: rd <- r0; r0 <- tmp1; r1 <- tmp2
6246 dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
6247 dsc->tmp[1] = displaced_read_reg (regs, dsc, 1);
6248 rn_val = displaced_read_reg (regs, dsc, rn);
6249 rd_val = displaced_read_reg (regs, dsc, rd);
6250 displaced_write_reg (regs, dsc, 0, rd_val, CANNOT_WRITE_PC);
6251 displaced_write_reg (regs, dsc, 1, rn_val, CANNOT_WRITE_PC);
6252 dsc->rd = rd;
6254 if (is_mov)
6255 dsc->modinsn[0] = insn & 0xfff00fff;
6256 else
6257 dsc->modinsn[0] = (insn & 0xfff00fff) | 0x10000;
6259 dsc->cleanup = &cleanup_alu_imm;
6261 return 0;
6264 static int
6265 thumb2_copy_alu_imm (struct gdbarch *gdbarch, uint16_t insn1,
6266 uint16_t insn2, struct regcache *regs,
6267 arm_displaced_step_copy_insn_closure *dsc)
6269 unsigned int op = bits (insn1, 5, 8);
6270 unsigned int rn, rm, rd;
6271 ULONGEST rd_val, rn_val;
6273 rn = bits (insn1, 0, 3); /* Rn */
6274 rm = bits (insn2, 0, 3); /* Rm */
6275 rd = bits (insn2, 8, 11); /* Rd */
6277 /* This routine is only called for instruction MOV. */
6278 gdb_assert (op == 0x2 && rn == 0xf);
6280 if (rm != ARM_PC_REGNUM && rd != ARM_PC_REGNUM)
6281 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2, "ALU imm", dsc);
6283 displaced_debug_printf ("copying reg %s insn %.4x%.4x", "ALU", insn1, insn2);
6285 /* Instruction is of form:
6287 <op><cond> rd, [rn,] #imm
6289 Rewrite as:
6291 Preparation: tmp1, tmp2 <- r0, r1;
6292 r0, r1 <- rd, rn
6293 Insn: <op><cond> r0, r1, #imm
6294 Cleanup: rd <- r0; r0 <- tmp1; r1 <- tmp2
6297 dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
6298 dsc->tmp[1] = displaced_read_reg (regs, dsc, 1);
6299 rn_val = displaced_read_reg (regs, dsc, rn);
6300 rd_val = displaced_read_reg (regs, dsc, rd);
6301 displaced_write_reg (regs, dsc, 0, rd_val, CANNOT_WRITE_PC);
6302 displaced_write_reg (regs, dsc, 1, rn_val, CANNOT_WRITE_PC);
6303 dsc->rd = rd;
6305 dsc->modinsn[0] = insn1;
6306 dsc->modinsn[1] = ((insn2 & 0xf0f0) | 0x1);
6307 dsc->numinsns = 2;
6309 dsc->cleanup = &cleanup_alu_imm;
6311 return 0;
6314 /* Copy/cleanup arithmetic/logic insns with register RHS. */
6316 static void
6317 cleanup_alu_reg (struct gdbarch *gdbarch,
6318 regcache *regs, arm_displaced_step_copy_insn_closure *dsc)
6320 ULONGEST rd_val;
6321 int i;
6323 rd_val = displaced_read_reg (regs, dsc, 0);
6325 for (i = 0; i < 3; i++)
6326 displaced_write_reg (regs, dsc, i, dsc->tmp[i], CANNOT_WRITE_PC);
6328 displaced_write_reg (regs, dsc, dsc->rd, rd_val, ALU_WRITE_PC);
6331 static void
6332 install_alu_reg (struct gdbarch *gdbarch, struct regcache *regs,
6333 arm_displaced_step_copy_insn_closure *dsc,
6334 unsigned int rd, unsigned int rn, unsigned int rm)
6336 ULONGEST rd_val, rn_val, rm_val;
6338 /* Instruction is of form:
6340 <op><cond> rd, [rn,] rm [, <shift>]
6342 Rewrite as:
6344 Preparation: tmp1, tmp2, tmp3 <- r0, r1, r2;
6345 r0, r1, r2 <- rd, rn, rm
6346 Insn: <op><cond> r0, [r1,] r2 [, <shift>]
6347 Cleanup: rd <- r0; r0, r1, r2 <- tmp1, tmp2, tmp3
6350 dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
6351 dsc->tmp[1] = displaced_read_reg (regs, dsc, 1);
6352 dsc->tmp[2] = displaced_read_reg (regs, dsc, 2);
6353 rd_val = displaced_read_reg (regs, dsc, rd);
6354 rn_val = displaced_read_reg (regs, dsc, rn);
6355 rm_val = displaced_read_reg (regs, dsc, rm);
6356 displaced_write_reg (regs, dsc, 0, rd_val, CANNOT_WRITE_PC);
6357 displaced_write_reg (regs, dsc, 1, rn_val, CANNOT_WRITE_PC);
6358 displaced_write_reg (regs, dsc, 2, rm_val, CANNOT_WRITE_PC);
6359 dsc->rd = rd;
6361 dsc->cleanup = &cleanup_alu_reg;
6364 static int
6365 arm_copy_alu_reg (struct gdbarch *gdbarch, uint32_t insn, struct regcache *regs,
6366 arm_displaced_step_copy_insn_closure *dsc)
6368 unsigned int op = bits (insn, 21, 24);
6369 int is_mov = (op == 0xd);
6371 if (!insn_references_pc (insn, 0x000ff00ful))
6372 return arm_copy_unmodified (gdbarch, insn, "ALU reg", dsc);
6374 displaced_debug_printf ("copying reg %s insn %.8lx",
6375 is_mov ? "move" : "ALU", (unsigned long) insn);
6377 if (is_mov)
6378 dsc->modinsn[0] = (insn & 0xfff00ff0) | 0x2;
6379 else
6380 dsc->modinsn[0] = (insn & 0xfff00ff0) | 0x10002;
6382 install_alu_reg (gdbarch, regs, dsc, bits (insn, 12, 15), bits (insn, 16, 19),
6383 bits (insn, 0, 3));
6384 return 0;
6387 static int
6388 thumb_copy_alu_reg (struct gdbarch *gdbarch, uint16_t insn,
6389 struct regcache *regs,
6390 arm_displaced_step_copy_insn_closure *dsc)
6392 unsigned rm, rd;
6394 rm = bits (insn, 3, 6);
6395 rd = (bit (insn, 7) << 3) | bits (insn, 0, 2);
6397 if (rd != ARM_PC_REGNUM && rm != ARM_PC_REGNUM)
6398 return thumb_copy_unmodified_16bit (gdbarch, insn, "ALU reg", dsc);
6400 displaced_debug_printf ("copying ALU reg insn %.4x", (unsigned short) insn);
6402 dsc->modinsn[0] = ((insn & 0xff00) | 0x10);
6404 install_alu_reg (gdbarch, regs, dsc, rd, rd, rm);
6406 return 0;
6409 /* Cleanup/copy arithmetic/logic insns with shifted register RHS. */
6411 static void
6412 cleanup_alu_shifted_reg (struct gdbarch *gdbarch,
6413 struct regcache *regs,
6414 arm_displaced_step_copy_insn_closure *dsc)
6416 ULONGEST rd_val = displaced_read_reg (regs, dsc, 0);
6417 int i;
6419 for (i = 0; i < 4; i++)
6420 displaced_write_reg (regs, dsc, i, dsc->tmp[i], CANNOT_WRITE_PC);
6422 displaced_write_reg (regs, dsc, dsc->rd, rd_val, ALU_WRITE_PC);
6425 static void
6426 install_alu_shifted_reg (struct gdbarch *gdbarch, struct regcache *regs,
6427 arm_displaced_step_copy_insn_closure *dsc,
6428 unsigned int rd, unsigned int rn, unsigned int rm,
6429 unsigned rs)
6431 int i;
6432 ULONGEST rd_val, rn_val, rm_val, rs_val;
6434 /* Instruction is of form:
6436 <op><cond> rd, [rn,] rm, <shift> rs
6438 Rewrite as:
6440 Preparation: tmp1, tmp2, tmp3, tmp4 <- r0, r1, r2, r3
6441 r0, r1, r2, r3 <- rd, rn, rm, rs
6442 Insn: <op><cond> r0, r1, r2, <shift> r3
6443 Cleanup: tmp5 <- r0
6444 r0, r1, r2, r3 <- tmp1, tmp2, tmp3, tmp4
6445 rd <- tmp5
6448 for (i = 0; i < 4; i++)
6449 dsc->tmp[i] = displaced_read_reg (regs, dsc, i);
6451 rd_val = displaced_read_reg (regs, dsc, rd);
6452 rn_val = displaced_read_reg (regs, dsc, rn);
6453 rm_val = displaced_read_reg (regs, dsc, rm);
6454 rs_val = displaced_read_reg (regs, dsc, rs);
6455 displaced_write_reg (regs, dsc, 0, rd_val, CANNOT_WRITE_PC);
6456 displaced_write_reg (regs, dsc, 1, rn_val, CANNOT_WRITE_PC);
6457 displaced_write_reg (regs, dsc, 2, rm_val, CANNOT_WRITE_PC);
6458 displaced_write_reg (regs, dsc, 3, rs_val, CANNOT_WRITE_PC);
6459 dsc->rd = rd;
6460 dsc->cleanup = &cleanup_alu_shifted_reg;
6463 static int
6464 arm_copy_alu_shifted_reg (struct gdbarch *gdbarch, uint32_t insn,
6465 struct regcache *regs,
6466 arm_displaced_step_copy_insn_closure *dsc)
6468 unsigned int op = bits (insn, 21, 24);
6469 int is_mov = (op == 0xd);
6470 unsigned int rd, rn, rm, rs;
6472 if (!insn_references_pc (insn, 0x000fff0ful))
6473 return arm_copy_unmodified (gdbarch, insn, "ALU shifted reg", dsc);
6475 displaced_debug_printf ("copying shifted reg %s insn %.8lx",
6476 is_mov ? "move" : "ALU",
6477 (unsigned long) insn);
6479 rn = bits (insn, 16, 19);
6480 rm = bits (insn, 0, 3);
6481 rs = bits (insn, 8, 11);
6482 rd = bits (insn, 12, 15);
6484 if (is_mov)
6485 dsc->modinsn[0] = (insn & 0xfff000f0) | 0x302;
6486 else
6487 dsc->modinsn[0] = (insn & 0xfff000f0) | 0x10302;
6489 install_alu_shifted_reg (gdbarch, regs, dsc, rd, rn, rm, rs);
6491 return 0;
6494 /* Clean up load instructions. */
6496 static void
6497 cleanup_load (struct gdbarch *gdbarch, struct regcache *regs,
6498 arm_displaced_step_copy_insn_closure *dsc)
6500 ULONGEST rt_val, rt_val2 = 0, rn_val;
6502 rt_val = displaced_read_reg (regs, dsc, 0);
6503 if (dsc->u.ldst.xfersize == 8)
6504 rt_val2 = displaced_read_reg (regs, dsc, 1);
6505 rn_val = displaced_read_reg (regs, dsc, 2);
6507 displaced_write_reg (regs, dsc, 0, dsc->tmp[0], CANNOT_WRITE_PC);
6508 if (dsc->u.ldst.xfersize > 4)
6509 displaced_write_reg (regs, dsc, 1, dsc->tmp[1], CANNOT_WRITE_PC);
6510 displaced_write_reg (regs, dsc, 2, dsc->tmp[2], CANNOT_WRITE_PC);
6511 if (!dsc->u.ldst.immed)
6512 displaced_write_reg (regs, dsc, 3, dsc->tmp[3], CANNOT_WRITE_PC);
6514 /* Handle register writeback. */
6515 if (dsc->u.ldst.writeback)
6516 displaced_write_reg (regs, dsc, dsc->u.ldst.rn, rn_val, CANNOT_WRITE_PC);
6517 /* Put result in right place. */
6518 displaced_write_reg (regs, dsc, dsc->rd, rt_val, LOAD_WRITE_PC);
6519 if (dsc->u.ldst.xfersize == 8)
6520 displaced_write_reg (regs, dsc, dsc->rd + 1, rt_val2, LOAD_WRITE_PC);
6523 /* Clean up store instructions. */
6525 static void
6526 cleanup_store (struct gdbarch *gdbarch, struct regcache *regs,
6527 arm_displaced_step_copy_insn_closure *dsc)
6529 ULONGEST rn_val = displaced_read_reg (regs, dsc, 2);
6531 displaced_write_reg (regs, dsc, 0, dsc->tmp[0], CANNOT_WRITE_PC);
6532 if (dsc->u.ldst.xfersize > 4)
6533 displaced_write_reg (regs, dsc, 1, dsc->tmp[1], CANNOT_WRITE_PC);
6534 displaced_write_reg (regs, dsc, 2, dsc->tmp[2], CANNOT_WRITE_PC);
6535 if (!dsc->u.ldst.immed)
6536 displaced_write_reg (regs, dsc, 3, dsc->tmp[3], CANNOT_WRITE_PC);
6537 if (!dsc->u.ldst.restore_r4)
6538 displaced_write_reg (regs, dsc, 4, dsc->tmp[4], CANNOT_WRITE_PC);
6540 /* Writeback. */
6541 if (dsc->u.ldst.writeback)
6542 displaced_write_reg (regs, dsc, dsc->u.ldst.rn, rn_val, CANNOT_WRITE_PC);
6545 /* Copy "extra" load/store instructions. These are halfword/doubleword
6546 transfers, which have a different encoding to byte/word transfers. */
6548 static int
6549 arm_copy_extra_ld_st (struct gdbarch *gdbarch, uint32_t insn, int unprivileged,
6550 regcache *regs, arm_displaced_step_copy_insn_closure *dsc)
6552 unsigned int op1 = bits (insn, 20, 24);
6553 unsigned int op2 = bits (insn, 5, 6);
6554 unsigned int rt = bits (insn, 12, 15);
6555 unsigned int rn = bits (insn, 16, 19);
6556 unsigned int rm = bits (insn, 0, 3);
6557 char load[12] = {0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1};
6558 char bytesize[12] = {2, 2, 2, 2, 8, 1, 8, 1, 8, 2, 8, 2};
6559 int immed = (op1 & 0x4) != 0;
6560 int opcode;
6561 ULONGEST rt_val, rt_val2 = 0, rn_val, rm_val = 0;
6563 if (!insn_references_pc (insn, 0x000ff00ful))
6564 return arm_copy_unmodified (gdbarch, insn, "extra load/store", dsc);
6566 displaced_debug_printf ("copying %sextra load/store insn %.8lx",
6567 unprivileged ? "unprivileged " : "",
6568 (unsigned long) insn);
6570 opcode = ((op2 << 2) | (op1 & 0x1) | ((op1 & 0x4) >> 1)) - 4;
6572 if (opcode < 0)
6573 internal_error (_("copy_extra_ld_st: instruction decode error"));
6575 dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
6576 dsc->tmp[1] = displaced_read_reg (regs, dsc, 1);
6577 dsc->tmp[2] = displaced_read_reg (regs, dsc, 2);
6578 if (!immed)
6579 dsc->tmp[3] = displaced_read_reg (regs, dsc, 3);
6581 rt_val = displaced_read_reg (regs, dsc, rt);
6582 if (bytesize[opcode] == 8)
6583 rt_val2 = displaced_read_reg (regs, dsc, rt + 1);
6584 rn_val = displaced_read_reg (regs, dsc, rn);
6585 if (!immed)
6586 rm_val = displaced_read_reg (regs, dsc, rm);
6588 displaced_write_reg (regs, dsc, 0, rt_val, CANNOT_WRITE_PC);
6589 if (bytesize[opcode] == 8)
6590 displaced_write_reg (regs, dsc, 1, rt_val2, CANNOT_WRITE_PC);
6591 displaced_write_reg (regs, dsc, 2, rn_val, CANNOT_WRITE_PC);
6592 if (!immed)
6593 displaced_write_reg (regs, dsc, 3, rm_val, CANNOT_WRITE_PC);
6595 dsc->rd = rt;
6596 dsc->u.ldst.xfersize = bytesize[opcode];
6597 dsc->u.ldst.rn = rn;
6598 dsc->u.ldst.immed = immed;
6599 dsc->u.ldst.writeback = bit (insn, 24) == 0 || bit (insn, 21) != 0;
6600 dsc->u.ldst.restore_r4 = 0;
6602 if (immed)
6603 /* {ldr,str}<width><cond> rt, [rt2,] [rn, #imm]
6605 {ldr,str}<width><cond> r0, [r1,] [r2, #imm]. */
6606 dsc->modinsn[0] = (insn & 0xfff00fff) | 0x20000;
6607 else
6608 /* {ldr,str}<width><cond> rt, [rt2,] [rn, +/-rm]
6610 {ldr,str}<width><cond> r0, [r1,] [r2, +/-r3]. */
6611 dsc->modinsn[0] = (insn & 0xfff00ff0) | 0x20003;
6613 dsc->cleanup = load[opcode] ? &cleanup_load : &cleanup_store;
6615 return 0;
6618 /* Copy byte/half word/word loads and stores. */
6620 static void
6621 install_load_store (struct gdbarch *gdbarch, struct regcache *regs,
6622 arm_displaced_step_copy_insn_closure *dsc, int load,
6623 int immed, int writeback, int size, int usermode,
6624 int rt, int rm, int rn)
6626 ULONGEST rt_val, rn_val, rm_val = 0;
6628 dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
6629 dsc->tmp[2] = displaced_read_reg (regs, dsc, 2);
6630 if (!immed)
6631 dsc->tmp[3] = displaced_read_reg (regs, dsc, 3);
6632 if (!load)
6633 dsc->tmp[4] = displaced_read_reg (regs, dsc, 4);
6635 rt_val = displaced_read_reg (regs, dsc, rt);
6636 rn_val = displaced_read_reg (regs, dsc, rn);
6637 if (!immed)
6638 rm_val = displaced_read_reg (regs, dsc, rm);
6640 displaced_write_reg (regs, dsc, 0, rt_val, CANNOT_WRITE_PC);
6641 displaced_write_reg (regs, dsc, 2, rn_val, CANNOT_WRITE_PC);
6642 if (!immed)
6643 displaced_write_reg (regs, dsc, 3, rm_val, CANNOT_WRITE_PC);
6644 dsc->rd = rt;
6645 dsc->u.ldst.xfersize = size;
6646 dsc->u.ldst.rn = rn;
6647 dsc->u.ldst.immed = immed;
6648 dsc->u.ldst.writeback = writeback;
6650 /* To write PC we can do:
6652 Before this sequence of instructions:
6653 r0 is the PC value got from displaced_read_reg, so r0 = from + 8;
6654 r2 is the Rn value got from displaced_read_reg.
6656 Insn1: push {pc} Write address of STR instruction + offset on stack
6657 Insn2: pop {r4} Read it back from stack, r4 = addr(Insn1) + offset
6658 Insn3: sub r4, r4, pc r4 = addr(Insn1) + offset - pc
6659 = addr(Insn1) + offset - addr(Insn3) - 8
6660 = offset - 16
6661 Insn4: add r4, r4, #8 r4 = offset - 8
6662 Insn5: add r0, r0, r4 r0 = from + 8 + offset - 8
6663 = from + offset
6664 Insn6: str r0, [r2, #imm] (or str r0, [r2, r3])
6666 Otherwise we don't know what value to write for PC, since the offset is
6667 architecture-dependent (sometimes PC+8, sometimes PC+12). More details
6668 of this can be found in Section "Saving from r15" in
6669 http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dui0204g/Cihbjifh.html */
6671 dsc->cleanup = load ? &cleanup_load : &cleanup_store;
6675 static int
6676 thumb2_copy_load_literal (struct gdbarch *gdbarch, uint16_t insn1,
6677 uint16_t insn2, struct regcache *regs,
6678 arm_displaced_step_copy_insn_closure *dsc, int size)
6680 unsigned int u_bit = bit (insn1, 7);
6681 unsigned int rt = bits (insn2, 12, 15);
6682 int imm12 = bits (insn2, 0, 11);
6683 ULONGEST pc_val;
6685 displaced_debug_printf ("copying ldr pc (0x%x) R%d %c imm12 %.4x",
6686 (unsigned int) dsc->insn_addr, rt, u_bit ? '+' : '-',
6687 imm12);
6689 if (!u_bit)
6690 imm12 = -1 * imm12;
6692 /* Rewrite instruction LDR Rt imm12 into:
6694 Prepare: tmp[0] <- r0, tmp[1] <- r2, tmp[2] <- r3, r2 <- pc, r3 <- imm12
6696 LDR R0, R2, R3,
6698 Cleanup: rt <- r0, r0 <- tmp[0], r2 <- tmp[1], r3 <- tmp[2]. */
6701 dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
6702 dsc->tmp[2] = displaced_read_reg (regs, dsc, 2);
6703 dsc->tmp[3] = displaced_read_reg (regs, dsc, 3);
6705 pc_val = displaced_read_reg (regs, dsc, ARM_PC_REGNUM);
6707 pc_val = pc_val & 0xfffffffc;
6709 displaced_write_reg (regs, dsc, 2, pc_val, CANNOT_WRITE_PC);
6710 displaced_write_reg (regs, dsc, 3, imm12, CANNOT_WRITE_PC);
6712 dsc->rd = rt;
6714 dsc->u.ldst.xfersize = size;
6715 dsc->u.ldst.immed = 0;
6716 dsc->u.ldst.writeback = 0;
6717 dsc->u.ldst.restore_r4 = 0;
6719 /* LDR R0, R2, R3 */
6720 dsc->modinsn[0] = 0xf852;
6721 dsc->modinsn[1] = 0x3;
6722 dsc->numinsns = 2;
6724 dsc->cleanup = &cleanup_load;
6726 return 0;
6729 static int
6730 thumb2_copy_load_reg_imm (struct gdbarch *gdbarch, uint16_t insn1,
6731 uint16_t insn2, struct regcache *regs,
6732 arm_displaced_step_copy_insn_closure *dsc,
6733 int writeback, int immed)
6735 unsigned int rt = bits (insn2, 12, 15);
6736 unsigned int rn = bits (insn1, 0, 3);
6737 unsigned int rm = bits (insn2, 0, 3); /* Only valid if !immed. */
6738 /* In LDR (register), there is also a register Rm, which is not allowed to
6739 be PC, so we don't have to check it. */
6741 if (rt != ARM_PC_REGNUM && rn != ARM_PC_REGNUM)
6742 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2, "load",
6743 dsc);
6745 displaced_debug_printf ("copying ldr r%d [r%d] insn %.4x%.4x",
6746 rt, rn, insn1, insn2);
6748 install_load_store (gdbarch, regs, dsc, 1, immed, writeback, 4,
6749 0, rt, rm, rn);
6751 dsc->u.ldst.restore_r4 = 0;
6753 if (immed)
6754 /* ldr[b]<cond> rt, [rn, #imm], etc.
6756 ldr[b]<cond> r0, [r2, #imm]. */
6758 dsc->modinsn[0] = (insn1 & 0xfff0) | 0x2;
6759 dsc->modinsn[1] = insn2 & 0x0fff;
6761 else
6762 /* ldr[b]<cond> rt, [rn, rm], etc.
6764 ldr[b]<cond> r0, [r2, r3]. */
6766 dsc->modinsn[0] = (insn1 & 0xfff0) | 0x2;
6767 dsc->modinsn[1] = (insn2 & 0x0ff0) | 0x3;
6770 dsc->numinsns = 2;
6772 return 0;
6776 static int
6777 arm_copy_ldr_str_ldrb_strb (struct gdbarch *gdbarch, uint32_t insn,
6778 struct regcache *regs,
6779 arm_displaced_step_copy_insn_closure *dsc,
6780 int load, int size, int usermode)
6782 int immed = !bit (insn, 25);
6783 int writeback = (bit (insn, 24) == 0 || bit (insn, 21) != 0);
6784 unsigned int rt = bits (insn, 12, 15);
6785 unsigned int rn = bits (insn, 16, 19);
6786 unsigned int rm = bits (insn, 0, 3); /* Only valid if !immed. */
6788 if (!insn_references_pc (insn, 0x000ff00ful))
6789 return arm_copy_unmodified (gdbarch, insn, "load/store", dsc);
6791 displaced_debug_printf ("copying %s%s r%d [r%d] insn %.8lx",
6792 load ? (size == 1 ? "ldrb" : "ldr")
6793 : (size == 1 ? "strb" : "str"),
6794 usermode ? "t" : "",
6795 rt, rn,
6796 (unsigned long) insn);
6798 install_load_store (gdbarch, regs, dsc, load, immed, writeback, size,
6799 usermode, rt, rm, rn);
6801 if (load || rt != ARM_PC_REGNUM)
6803 dsc->u.ldst.restore_r4 = 0;
6805 if (immed)
6806 /* {ldr,str}[b]<cond> rt, [rn, #imm], etc.
6808 {ldr,str}[b]<cond> r0, [r2, #imm]. */
6809 dsc->modinsn[0] = (insn & 0xfff00fff) | 0x20000;
6810 else
6811 /* {ldr,str}[b]<cond> rt, [rn, rm], etc.
6813 {ldr,str}[b]<cond> r0, [r2, r3]. */
6814 dsc->modinsn[0] = (insn & 0xfff00ff0) | 0x20003;
6816 else
6818 /* We need to use r4 as scratch. Make sure it's restored afterwards. */
6819 dsc->u.ldst.restore_r4 = 1;
6820 dsc->modinsn[0] = 0xe92d8000; /* push {pc} */
6821 dsc->modinsn[1] = 0xe8bd0010; /* pop {r4} */
6822 dsc->modinsn[2] = 0xe044400f; /* sub r4, r4, pc. */
6823 dsc->modinsn[3] = 0xe2844008; /* add r4, r4, #8. */
6824 dsc->modinsn[4] = 0xe0800004; /* add r0, r0, r4. */
6826 /* As above. */
6827 if (immed)
6828 dsc->modinsn[5] = (insn & 0xfff00fff) | 0x20000;
6829 else
6830 dsc->modinsn[5] = (insn & 0xfff00ff0) | 0x20003;
6832 dsc->numinsns = 6;
6835 dsc->cleanup = load ? &cleanup_load : &cleanup_store;
6837 return 0;
6840 /* Cleanup LDM instructions with fully-populated register list. This is an
6841 unfortunate corner case: it's impossible to implement correctly by modifying
6842 the instruction. The issue is as follows: we have an instruction,
6844 ldm rN, {r0-r15}
6846 which we must rewrite to avoid loading PC. A possible solution would be to
6847 do the load in two halves, something like (with suitable cleanup
6848 afterwards):
6850 mov r8, rN
6851 ldm[id][ab] r8!, {r0-r7}
6852 str r7, <temp>
6853 ldm[id][ab] r8, {r7-r14}
6854 <bkpt>
6856 but at present there's no suitable place for <temp>, since the scratch space
6857 is overwritten before the cleanup routine is called. For now, we simply
6858 emulate the instruction. */
6860 static void
6861 cleanup_block_load_all (struct gdbarch *gdbarch, struct regcache *regs,
6862 arm_displaced_step_copy_insn_closure *dsc)
6864 int inc = dsc->u.block.increment;
6865 int bump_before = dsc->u.block.before ? (inc ? 4 : -4) : 0;
6866 int bump_after = dsc->u.block.before ? 0 : (inc ? 4 : -4);
6867 uint32_t regmask = dsc->u.block.regmask;
6868 int regno = inc ? 0 : 15;
6869 CORE_ADDR xfer_addr = dsc->u.block.xfer_addr;
6870 int exception_return = dsc->u.block.load && dsc->u.block.user
6871 && (regmask & 0x8000) != 0;
6872 uint32_t status = displaced_read_reg (regs, dsc, ARM_PS_REGNUM);
6873 int do_transfer = condition_true (dsc->u.block.cond, status);
6874 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
6876 if (!do_transfer)
6877 return;
6879 /* If the instruction is ldm rN, {...pc}^, I don't think there's anything
6880 sensible we can do here. Complain loudly. */
6881 if (exception_return)
6882 error (_("Cannot single-step exception return"));
6884 /* We don't handle any stores here for now. */
6885 gdb_assert (dsc->u.block.load != 0);
6887 displaced_debug_printf ("emulating block transfer: %s %s %s",
6888 dsc->u.block.load ? "ldm" : "stm",
6889 dsc->u.block.increment ? "inc" : "dec",
6890 dsc->u.block.before ? "before" : "after");
6892 while (regmask)
6894 uint32_t memword;
6896 if (inc)
6897 while (regno <= ARM_PC_REGNUM && (regmask & (1 << regno)) == 0)
6898 regno++;
6899 else
6900 while (regno >= 0 && (regmask & (1 << regno)) == 0)
6901 regno--;
6903 xfer_addr += bump_before;
6905 memword = read_memory_unsigned_integer (xfer_addr, 4, byte_order);
6906 displaced_write_reg (regs, dsc, regno, memword, LOAD_WRITE_PC);
6908 xfer_addr += bump_after;
6910 regmask &= ~(1 << regno);
6913 if (dsc->u.block.writeback)
6914 displaced_write_reg (regs, dsc, dsc->u.block.rn, xfer_addr,
6915 CANNOT_WRITE_PC);
6918 /* Clean up an STM which included the PC in the register list. */
6920 static void
6921 cleanup_block_store_pc (struct gdbarch *gdbarch, struct regcache *regs,
6922 arm_displaced_step_copy_insn_closure *dsc)
6924 uint32_t status = displaced_read_reg (regs, dsc, ARM_PS_REGNUM);
6925 int store_executed = condition_true (dsc->u.block.cond, status);
6926 CORE_ADDR pc_stored_at, transferred_regs
6927 = count_one_bits (dsc->u.block.regmask);
6928 CORE_ADDR stm_insn_addr;
6929 uint32_t pc_val;
6930 long offset;
6931 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
6933 /* If condition code fails, there's nothing else to do. */
6934 if (!store_executed)
6935 return;
6937 if (dsc->u.block.increment)
6939 pc_stored_at = dsc->u.block.xfer_addr + 4 * transferred_regs;
6941 if (dsc->u.block.before)
6942 pc_stored_at += 4;
6944 else
6946 pc_stored_at = dsc->u.block.xfer_addr;
6948 if (dsc->u.block.before)
6949 pc_stored_at -= 4;
6952 pc_val = read_memory_unsigned_integer (pc_stored_at, 4, byte_order);
6953 stm_insn_addr = dsc->scratch_base;
6954 offset = pc_val - stm_insn_addr;
6956 displaced_debug_printf ("detected PC offset %.8lx for STM instruction",
6957 offset);
6959 /* Rewrite the stored PC to the proper value for the non-displaced original
6960 instruction. */
6961 write_memory_unsigned_integer (pc_stored_at, 4, byte_order,
6962 dsc->insn_addr + offset);
6965 /* Clean up an LDM which includes the PC in the register list. We clumped all
6966 the registers in the transferred list into a contiguous range r0...rX (to
6967 avoid loading PC directly and losing control of the debugged program), so we
6968 must undo that here. */
6970 static void
6971 cleanup_block_load_pc (struct gdbarch *gdbarch,
6972 struct regcache *regs,
6973 arm_displaced_step_copy_insn_closure *dsc)
6975 uint32_t status = displaced_read_reg (regs, dsc, ARM_PS_REGNUM);
6976 int load_executed = condition_true (dsc->u.block.cond, status);
6977 unsigned int mask = dsc->u.block.regmask, write_reg = ARM_PC_REGNUM;
6978 unsigned int regs_loaded = count_one_bits (mask);
6979 unsigned int num_to_shuffle = regs_loaded, clobbered;
6981 /* The method employed here will fail if the register list is fully populated
6982 (we need to avoid loading PC directly). */
6983 gdb_assert (num_to_shuffle < 16);
6985 if (!load_executed)
6986 return;
6988 clobbered = (1 << num_to_shuffle) - 1;
6990 while (num_to_shuffle > 0)
6992 if ((mask & (1 << write_reg)) != 0)
6994 unsigned int read_reg = num_to_shuffle - 1;
6996 if (read_reg != write_reg)
6998 ULONGEST rval = displaced_read_reg (regs, dsc, read_reg);
6999 displaced_write_reg (regs, dsc, write_reg, rval, LOAD_WRITE_PC);
7000 displaced_debug_printf ("LDM: move loaded register r%d to r%d",
7001 read_reg, write_reg);
7003 else
7004 displaced_debug_printf ("LDM: register r%d already in the right "
7005 "place", write_reg);
7007 clobbered &= ~(1 << write_reg);
7009 num_to_shuffle--;
7012 write_reg--;
7015 /* Restore any registers we scribbled over. */
7016 for (write_reg = 0; clobbered != 0; write_reg++)
7018 if ((clobbered & (1 << write_reg)) != 0)
7020 displaced_write_reg (regs, dsc, write_reg, dsc->tmp[write_reg],
7021 CANNOT_WRITE_PC);
7022 displaced_debug_printf ("LDM: restored clobbered register r%d",
7023 write_reg);
7024 clobbered &= ~(1 << write_reg);
7028 /* Perform register writeback manually. */
7029 if (dsc->u.block.writeback)
7031 ULONGEST new_rn_val = dsc->u.block.xfer_addr;
7033 if (dsc->u.block.increment)
7034 new_rn_val += regs_loaded * 4;
7035 else
7036 new_rn_val -= regs_loaded * 4;
7038 displaced_write_reg (regs, dsc, dsc->u.block.rn, new_rn_val,
7039 CANNOT_WRITE_PC);
7043 /* Handle ldm/stm, apart from some tricky cases which are unlikely to occur
7044 in user-level code (in particular exception return, ldm rn, {...pc}^). */
7046 static int
7047 arm_copy_block_xfer (struct gdbarch *gdbarch, uint32_t insn,
7048 struct regcache *regs,
7049 arm_displaced_step_copy_insn_closure *dsc)
7051 int load = bit (insn, 20);
7052 int user = bit (insn, 22);
7053 int increment = bit (insn, 23);
7054 int before = bit (insn, 24);
7055 int writeback = bit (insn, 21);
7056 int rn = bits (insn, 16, 19);
7058 /* Block transfers which don't mention PC can be run directly
7059 out-of-line. */
7060 if (rn != ARM_PC_REGNUM && (insn & 0x8000) == 0)
7061 return arm_copy_unmodified (gdbarch, insn, "ldm/stm", dsc);
7063 if (rn == ARM_PC_REGNUM)
7065 warning (_("displaced: Unpredictable LDM or STM with "
7066 "base register r15"));
7067 return arm_copy_unmodified (gdbarch, insn, "unpredictable ldm/stm", dsc);
7070 displaced_debug_printf ("copying block transfer insn %.8lx",
7071 (unsigned long) insn);
7073 dsc->u.block.xfer_addr = displaced_read_reg (regs, dsc, rn);
7074 dsc->u.block.rn = rn;
7076 dsc->u.block.load = load;
7077 dsc->u.block.user = user;
7078 dsc->u.block.increment = increment;
7079 dsc->u.block.before = before;
7080 dsc->u.block.writeback = writeback;
7081 dsc->u.block.cond = bits (insn, 28, 31);
7083 dsc->u.block.regmask = insn & 0xffff;
7085 if (load)
7087 if ((insn & 0xffff) == 0xffff)
7089 /* LDM with a fully-populated register list. This case is
7090 particularly tricky. Implement for now by fully emulating the
7091 instruction (which might not behave perfectly in all cases, but
7092 these instructions should be rare enough for that not to matter
7093 too much). */
7094 dsc->modinsn[0] = ARM_NOP;
7096 dsc->cleanup = &cleanup_block_load_all;
7098 else
7100 /* LDM of a list of registers which includes PC. Implement by
7101 rewriting the list of registers to be transferred into a
7102 contiguous chunk r0...rX before doing the transfer, then shuffling
7103 registers into the correct places in the cleanup routine. */
7104 unsigned int regmask = insn & 0xffff;
7105 unsigned int num_in_list = count_one_bits (regmask), new_regmask;
7106 unsigned int i;
7108 for (i = 0; i < num_in_list; i++)
7109 dsc->tmp[i] = displaced_read_reg (regs, dsc, i);
7111 /* Writeback makes things complicated. We need to avoid clobbering
7112 the base register with one of the registers in our modified
7113 register list, but just using a different register can't work in
7114 all cases, e.g.:
7116 ldm r14!, {r0-r13,pc}
7118 which would need to be rewritten as:
7120 ldm rN!, {r0-r14}
7122 but that can't work, because there's no free register for N.
7124 Solve this by turning off the writeback bit, and emulating
7125 writeback manually in the cleanup routine. */
7127 if (writeback)
7128 insn &= ~(1 << 21);
7130 new_regmask = (1 << num_in_list) - 1;
7132 displaced_debug_printf ("LDM r%d%s, {..., pc}: original reg list "
7133 "%.4x, modified list %.4x",
7134 rn, writeback ? "!" : "",
7135 (int) insn & 0xffff, new_regmask);
7137 dsc->modinsn[0] = (insn & ~0xffff) | (new_regmask & 0xffff);
7139 dsc->cleanup = &cleanup_block_load_pc;
7142 else
7144 /* STM of a list of registers which includes PC. Run the instruction
7145 as-is, but out of line: this will store the wrong value for the PC,
7146 so we must manually fix up the memory in the cleanup routine.
7147 Doing things this way has the advantage that we can auto-detect
7148 the offset of the PC write (which is architecture-dependent) in
7149 the cleanup routine. */
7150 dsc->modinsn[0] = insn;
7152 dsc->cleanup = &cleanup_block_store_pc;
7155 return 0;
7158 static int
7159 thumb2_copy_block_xfer (struct gdbarch *gdbarch, uint16_t insn1, uint16_t insn2,
7160 struct regcache *regs,
7161 arm_displaced_step_copy_insn_closure *dsc)
7163 int rn = bits (insn1, 0, 3);
7164 int load = bit (insn1, 4);
7165 int writeback = bit (insn1, 5);
7167 /* Block transfers which don't mention PC can be run directly
7168 out-of-line. */
7169 if (rn != ARM_PC_REGNUM && (insn2 & 0x8000) == 0)
7170 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2, "ldm/stm", dsc);
7172 if (rn == ARM_PC_REGNUM)
7174 warning (_("displaced: Unpredictable LDM or STM with "
7175 "base register r15"));
7176 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
7177 "unpredictable ldm/stm", dsc);
7180 displaced_debug_printf ("copying block transfer insn %.4x%.4x",
7181 insn1, insn2);
7183 /* Clear bit 13, since it should be always zero. */
7184 dsc->u.block.regmask = (insn2 & 0xdfff);
7185 dsc->u.block.rn = rn;
7187 dsc->u.block.load = load;
7188 dsc->u.block.user = 0;
7189 dsc->u.block.increment = bit (insn1, 7);
7190 dsc->u.block.before = bit (insn1, 8);
7191 dsc->u.block.writeback = writeback;
7192 dsc->u.block.cond = INST_AL;
7193 dsc->u.block.xfer_addr = displaced_read_reg (regs, dsc, rn);
7195 if (load)
7197 if (dsc->u.block.regmask == 0xffff)
7199 /* This branch is impossible to happen. */
7200 gdb_assert (0);
7202 else
7204 unsigned int regmask = dsc->u.block.regmask;
7205 unsigned int num_in_list = count_one_bits (regmask), new_regmask;
7206 unsigned int i;
7208 for (i = 0; i < num_in_list; i++)
7209 dsc->tmp[i] = displaced_read_reg (regs, dsc, i);
7211 if (writeback)
7212 insn1 &= ~(1 << 5);
7214 new_regmask = (1 << num_in_list) - 1;
7216 displaced_debug_printf ("LDM r%d%s, {..., pc}: original reg list "
7217 "%.4x, modified list %.4x",
7218 rn, writeback ? "!" : "",
7219 (int) dsc->u.block.regmask, new_regmask);
7221 dsc->modinsn[0] = insn1;
7222 dsc->modinsn[1] = (new_regmask & 0xffff);
7223 dsc->numinsns = 2;
7225 dsc->cleanup = &cleanup_block_load_pc;
7228 else
7230 dsc->modinsn[0] = insn1;
7231 dsc->modinsn[1] = insn2;
7232 dsc->numinsns = 2;
7233 dsc->cleanup = &cleanup_block_store_pc;
7235 return 0;
7238 /* Wrapper over read_memory_unsigned_integer for use in arm_get_next_pcs.
7239 This is used to avoid a dependency on BFD's bfd_endian enum. */
7241 ULONGEST
7242 arm_get_next_pcs_read_memory_unsigned_integer (CORE_ADDR memaddr, int len,
7243 int byte_order)
7245 return read_memory_unsigned_integer (memaddr, len,
7246 (enum bfd_endian) byte_order);
7249 /* Wrapper over gdbarch_addr_bits_remove for use in arm_get_next_pcs. */
7251 CORE_ADDR
7252 arm_get_next_pcs_addr_bits_remove (struct arm_get_next_pcs *self,
7253 CORE_ADDR val)
7255 return gdbarch_addr_bits_remove
7256 (gdb::checked_static_cast<regcache *> (self->regcache)->arch (), val);
7259 /* Wrapper over syscall_next_pc for use in get_next_pcs. */
7261 static CORE_ADDR
7262 arm_get_next_pcs_syscall_next_pc (struct arm_get_next_pcs *self)
7264 return 0;
7267 /* Wrapper over arm_is_thumb for use in arm_get_next_pcs. */
7270 arm_get_next_pcs_is_thumb (struct arm_get_next_pcs *self)
7272 return arm_is_thumb (gdb::checked_static_cast<regcache *> (self->regcache));
7275 /* single_step() is called just before we want to resume the inferior,
7276 if we want to single-step it but there is no hardware or kernel
7277 single-step support. We find the target of the coming instructions
7278 and breakpoint them. */
7280 std::vector<CORE_ADDR>
7281 arm_software_single_step (struct regcache *regcache)
7283 struct gdbarch *gdbarch = regcache->arch ();
7284 struct arm_get_next_pcs next_pcs_ctx;
7286 arm_get_next_pcs_ctor (&next_pcs_ctx,
7287 &arm_get_next_pcs_ops,
7288 gdbarch_byte_order (gdbarch),
7289 gdbarch_byte_order_for_code (gdbarch),
7291 regcache);
7293 std::vector<CORE_ADDR> next_pcs = arm_get_next_pcs (&next_pcs_ctx);
7295 for (CORE_ADDR &pc_ref : next_pcs)
7296 pc_ref = gdbarch_addr_bits_remove (gdbarch, pc_ref);
7298 return next_pcs;
7301 /* Cleanup/copy SVC (SWI) instructions. These two functions are overridden
7302 for Linux, where some SVC instructions must be treated specially. */
7304 static void
7305 cleanup_svc (struct gdbarch *gdbarch, struct regcache *regs,
7306 arm_displaced_step_copy_insn_closure *dsc)
7308 CORE_ADDR resume_addr = dsc->insn_addr + dsc->insn_size;
7310 displaced_debug_printf ("cleanup for svc, resume at %.8lx",
7311 (unsigned long) resume_addr);
7313 displaced_write_reg (regs, dsc, ARM_PC_REGNUM, resume_addr, BRANCH_WRITE_PC);
7317 /* Common copy routine for svc instruction. */
7319 static int
7320 install_svc (struct gdbarch *gdbarch, struct regcache *regs,
7321 arm_displaced_step_copy_insn_closure *dsc)
7323 /* Preparation: none.
7324 Insn: unmodified svc.
7325 Cleanup: pc <- insn_addr + insn_size. */
7327 /* Pretend we wrote to the PC, so cleanup doesn't set PC to the next
7328 instruction. */
7329 dsc->wrote_to_pc = 1;
7331 /* Allow OS-specific code to override SVC handling. */
7332 if (dsc->u.svc.copy_svc_os)
7333 return dsc->u.svc.copy_svc_os (gdbarch, regs, dsc);
7334 else
7336 dsc->cleanup = &cleanup_svc;
7337 return 0;
7341 static int
7342 arm_copy_svc (struct gdbarch *gdbarch, uint32_t insn,
7343 regcache *regs, arm_displaced_step_copy_insn_closure *dsc)
7346 displaced_debug_printf ("copying svc insn %.8lx",
7347 (unsigned long) insn);
7349 dsc->modinsn[0] = insn;
7351 return install_svc (gdbarch, regs, dsc);
7354 static int
7355 thumb_copy_svc (struct gdbarch *gdbarch, uint16_t insn,
7356 regcache *regs, arm_displaced_step_copy_insn_closure *dsc)
7359 displaced_debug_printf ("copying svc insn %.4x", insn);
7361 dsc->modinsn[0] = insn;
7363 return install_svc (gdbarch, regs, dsc);
7366 /* Copy undefined instructions. */
7368 static int
7369 arm_copy_undef (struct gdbarch *gdbarch, uint32_t insn,
7370 arm_displaced_step_copy_insn_closure *dsc)
7372 displaced_debug_printf ("copying undefined insn %.8lx",
7373 (unsigned long) insn);
7375 dsc->modinsn[0] = insn;
7377 return 0;
7380 static int
7381 thumb_32bit_copy_undef (struct gdbarch *gdbarch, uint16_t insn1, uint16_t insn2,
7382 arm_displaced_step_copy_insn_closure *dsc)
7385 displaced_debug_printf ("copying undefined insn %.4x %.4x",
7386 (unsigned short) insn1, (unsigned short) insn2);
7388 dsc->modinsn[0] = insn1;
7389 dsc->modinsn[1] = insn2;
7390 dsc->numinsns = 2;
7392 return 0;
7395 /* Copy unpredictable instructions. */
7397 static int
7398 arm_copy_unpred (struct gdbarch *gdbarch, uint32_t insn,
7399 arm_displaced_step_copy_insn_closure *dsc)
7401 displaced_debug_printf ("copying unpredictable insn %.8lx",
7402 (unsigned long) insn);
7404 dsc->modinsn[0] = insn;
7406 return 0;
7409 /* The decode_* functions are instruction decoding helpers. They mostly follow
7410 the presentation in the ARM ARM. */
7412 static int
7413 arm_decode_misc_memhint_neon (struct gdbarch *gdbarch, uint32_t insn,
7414 struct regcache *regs,
7415 arm_displaced_step_copy_insn_closure *dsc)
7417 unsigned int op1 = bits (insn, 20, 26), op2 = bits (insn, 4, 7);
7418 unsigned int rn = bits (insn, 16, 19);
7420 if (op1 == 0x10 && (op2 & 0x2) == 0x0 && (rn & 0x1) == 0x0)
7421 return arm_copy_unmodified (gdbarch, insn, "cps", dsc);
7422 else if (op1 == 0x10 && op2 == 0x0 && (rn & 0x1) == 0x1)
7423 return arm_copy_unmodified (gdbarch, insn, "setend", dsc);
7424 else if ((op1 & 0x60) == 0x20)
7425 return arm_copy_unmodified (gdbarch, insn, "neon dataproc", dsc);
7426 else if ((op1 & 0x71) == 0x40)
7427 return arm_copy_unmodified (gdbarch, insn, "neon elt/struct load/store",
7428 dsc);
7429 else if ((op1 & 0x77) == 0x41)
7430 return arm_copy_unmodified (gdbarch, insn, "unallocated mem hint", dsc);
7431 else if ((op1 & 0x77) == 0x45)
7432 return arm_copy_preload (gdbarch, insn, regs, dsc); /* pli. */
7433 else if ((op1 & 0x77) == 0x51)
7435 if (rn != 0xf)
7436 return arm_copy_preload (gdbarch, insn, regs, dsc); /* pld/pldw. */
7437 else
7438 return arm_copy_unpred (gdbarch, insn, dsc);
7440 else if ((op1 & 0x77) == 0x55)
7441 return arm_copy_preload (gdbarch, insn, regs, dsc); /* pld/pldw. */
7442 else if (op1 == 0x57)
7443 switch (op2)
7445 case 0x1: return arm_copy_unmodified (gdbarch, insn, "clrex", dsc);
7446 case 0x4: return arm_copy_unmodified (gdbarch, insn, "dsb", dsc);
7447 case 0x5: return arm_copy_unmodified (gdbarch, insn, "dmb", dsc);
7448 case 0x6: return arm_copy_unmodified (gdbarch, insn, "isb", dsc);
7449 default: return arm_copy_unpred (gdbarch, insn, dsc);
7451 else if ((op1 & 0x63) == 0x43)
7452 return arm_copy_unpred (gdbarch, insn, dsc);
7453 else if ((op2 & 0x1) == 0x0)
7454 switch (op1 & ~0x80)
7456 case 0x61:
7457 return arm_copy_unmodified (gdbarch, insn, "unallocated mem hint", dsc);
7458 case 0x65:
7459 return arm_copy_preload_reg (gdbarch, insn, regs, dsc); /* pli reg. */
7460 case 0x71: case 0x75:
7461 /* pld/pldw reg. */
7462 return arm_copy_preload_reg (gdbarch, insn, regs, dsc);
7463 case 0x63: case 0x67: case 0x73: case 0x77:
7464 return arm_copy_unpred (gdbarch, insn, dsc);
7465 default:
7466 return arm_copy_undef (gdbarch, insn, dsc);
7468 else
7469 return arm_copy_undef (gdbarch, insn, dsc); /* Probably unreachable. */
7472 static int
7473 arm_decode_unconditional (struct gdbarch *gdbarch, uint32_t insn,
7474 struct regcache *regs,
7475 arm_displaced_step_copy_insn_closure *dsc)
7477 if (bit (insn, 27) == 0)
7478 return arm_decode_misc_memhint_neon (gdbarch, insn, regs, dsc);
7479 /* Switch on bits: 0bxxxxx321xxx0xxxxxxxxxxxxxxxxxxxx. */
7480 else switch (((insn & 0x7000000) >> 23) | ((insn & 0x100000) >> 20))
7482 case 0x0: case 0x2:
7483 return arm_copy_unmodified (gdbarch, insn, "srs", dsc);
7485 case 0x1: case 0x3:
7486 return arm_copy_unmodified (gdbarch, insn, "rfe", dsc);
7488 case 0x4: case 0x5: case 0x6: case 0x7:
7489 return arm_copy_b_bl_blx (gdbarch, insn, regs, dsc);
7491 case 0x8:
7492 switch ((insn & 0xe00000) >> 21)
7494 case 0x1: case 0x3: case 0x4: case 0x5: case 0x6: case 0x7:
7495 /* stc/stc2. */
7496 return arm_copy_copro_load_store (gdbarch, insn, regs, dsc);
7498 case 0x2:
7499 return arm_copy_unmodified (gdbarch, insn, "mcrr/mcrr2", dsc);
7501 default:
7502 return arm_copy_undef (gdbarch, insn, dsc);
7505 case 0x9:
7507 int rn_f = (bits (insn, 16, 19) == 0xf);
7508 switch ((insn & 0xe00000) >> 21)
7510 case 0x1: case 0x3:
7511 /* ldc/ldc2 imm (undefined for rn == pc). */
7512 return rn_f ? arm_copy_undef (gdbarch, insn, dsc)
7513 : arm_copy_copro_load_store (gdbarch, insn, regs, dsc);
7515 case 0x2:
7516 return arm_copy_unmodified (gdbarch, insn, "mrrc/mrrc2", dsc);
7518 case 0x4: case 0x5: case 0x6: case 0x7:
7519 /* ldc/ldc2 lit (undefined for rn != pc). */
7520 return rn_f ? arm_copy_copro_load_store (gdbarch, insn, regs, dsc)
7521 : arm_copy_undef (gdbarch, insn, dsc);
7523 default:
7524 return arm_copy_undef (gdbarch, insn, dsc);
7528 case 0xa:
7529 return arm_copy_unmodified (gdbarch, insn, "stc/stc2", dsc);
7531 case 0xb:
7532 if (bits (insn, 16, 19) == 0xf)
7533 /* ldc/ldc2 lit. */
7534 return arm_copy_copro_load_store (gdbarch, insn, regs, dsc);
7535 else
7536 return arm_copy_undef (gdbarch, insn, dsc);
7538 case 0xc:
7539 if (bit (insn, 4))
7540 return arm_copy_unmodified (gdbarch, insn, "mcr/mcr2", dsc);
7541 else
7542 return arm_copy_unmodified (gdbarch, insn, "cdp/cdp2", dsc);
7544 case 0xd:
7545 if (bit (insn, 4))
7546 return arm_copy_unmodified (gdbarch, insn, "mrc/mrc2", dsc);
7547 else
7548 return arm_copy_unmodified (gdbarch, insn, "cdp/cdp2", dsc);
7550 default:
7551 return arm_copy_undef (gdbarch, insn, dsc);
7555 /* Decode miscellaneous instructions in dp/misc encoding space. */
7557 static int
7558 arm_decode_miscellaneous (struct gdbarch *gdbarch, uint32_t insn,
7559 struct regcache *regs,
7560 arm_displaced_step_copy_insn_closure *dsc)
7562 unsigned int op2 = bits (insn, 4, 6);
7563 unsigned int op = bits (insn, 21, 22);
7565 switch (op2)
7567 case 0x0:
7568 return arm_copy_unmodified (gdbarch, insn, "mrs/msr", dsc);
7570 case 0x1:
7571 if (op == 0x1) /* bx. */
7572 return arm_copy_bx_blx_reg (gdbarch, insn, regs, dsc);
7573 else if (op == 0x3)
7574 return arm_copy_unmodified (gdbarch, insn, "clz", dsc);
7575 else
7576 return arm_copy_undef (gdbarch, insn, dsc);
7578 case 0x2:
7579 if (op == 0x1)
7580 /* Not really supported. */
7581 return arm_copy_unmodified (gdbarch, insn, "bxj", dsc);
7582 else
7583 return arm_copy_undef (gdbarch, insn, dsc);
7585 case 0x3:
7586 if (op == 0x1)
7587 return arm_copy_bx_blx_reg (gdbarch, insn,
7588 regs, dsc); /* blx register. */
7589 else
7590 return arm_copy_undef (gdbarch, insn, dsc);
7592 case 0x5:
7593 return arm_copy_unmodified (gdbarch, insn, "saturating add/sub", dsc);
7595 case 0x7:
7596 if (op == 0x1)
7597 return arm_copy_unmodified (gdbarch, insn, "bkpt", dsc);
7598 else if (op == 0x3)
7599 /* Not really supported. */
7600 return arm_copy_unmodified (gdbarch, insn, "smc", dsc);
7601 [[fallthrough]];
7603 default:
7604 return arm_copy_undef (gdbarch, insn, dsc);
7608 static int
7609 arm_decode_dp_misc (struct gdbarch *gdbarch, uint32_t insn,
7610 struct regcache *regs,
7611 arm_displaced_step_copy_insn_closure *dsc)
7613 if (bit (insn, 25))
7614 switch (bits (insn, 20, 24))
7616 case 0x10:
7617 return arm_copy_unmodified (gdbarch, insn, "movw", dsc);
7619 case 0x14:
7620 return arm_copy_unmodified (gdbarch, insn, "movt", dsc);
7622 case 0x12: case 0x16:
7623 return arm_copy_unmodified (gdbarch, insn, "msr imm", dsc);
7625 default:
7626 return arm_copy_alu_imm (gdbarch, insn, regs, dsc);
7628 else
7630 uint32_t op1 = bits (insn, 20, 24), op2 = bits (insn, 4, 7);
7632 if ((op1 & 0x19) != 0x10 && (op2 & 0x1) == 0x0)
7633 return arm_copy_alu_reg (gdbarch, insn, regs, dsc);
7634 else if ((op1 & 0x19) != 0x10 && (op2 & 0x9) == 0x1)
7635 return arm_copy_alu_shifted_reg (gdbarch, insn, regs, dsc);
7636 else if ((op1 & 0x19) == 0x10 && (op2 & 0x8) == 0x0)
7637 return arm_decode_miscellaneous (gdbarch, insn, regs, dsc);
7638 else if ((op1 & 0x19) == 0x10 && (op2 & 0x9) == 0x8)
7639 return arm_copy_unmodified (gdbarch, insn, "halfword mul/mla", dsc);
7640 else if ((op1 & 0x10) == 0x00 && op2 == 0x9)
7641 return arm_copy_unmodified (gdbarch, insn, "mul/mla", dsc);
7642 else if ((op1 & 0x10) == 0x10 && op2 == 0x9)
7643 return arm_copy_unmodified (gdbarch, insn, "synch", dsc);
7644 else if (op2 == 0xb || (op2 & 0xd) == 0xd)
7645 /* 2nd arg means "unprivileged". */
7646 return arm_copy_extra_ld_st (gdbarch, insn, (op1 & 0x12) == 0x02, regs,
7647 dsc);
7650 /* Should be unreachable. */
7651 return 1;
7654 static int
7655 arm_decode_ld_st_word_ubyte (struct gdbarch *gdbarch, uint32_t insn,
7656 struct regcache *regs,
7657 arm_displaced_step_copy_insn_closure *dsc)
7659 int a = bit (insn, 25), b = bit (insn, 4);
7660 uint32_t op1 = bits (insn, 20, 24);
7662 if ((!a && (op1 & 0x05) == 0x00 && (op1 & 0x17) != 0x02)
7663 || (a && (op1 & 0x05) == 0x00 && (op1 & 0x17) != 0x02 && !b))
7664 return arm_copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 0, 4, 0);
7665 else if ((!a && (op1 & 0x17) == 0x02)
7666 || (a && (op1 & 0x17) == 0x02 && !b))
7667 return arm_copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 0, 4, 1);
7668 else if ((!a && (op1 & 0x05) == 0x01 && (op1 & 0x17) != 0x03)
7669 || (a && (op1 & 0x05) == 0x01 && (op1 & 0x17) != 0x03 && !b))
7670 return arm_copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 1, 4, 0);
7671 else if ((!a && (op1 & 0x17) == 0x03)
7672 || (a && (op1 & 0x17) == 0x03 && !b))
7673 return arm_copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 1, 4, 1);
7674 else if ((!a && (op1 & 0x05) == 0x04 && (op1 & 0x17) != 0x06)
7675 || (a && (op1 & 0x05) == 0x04 && (op1 & 0x17) != 0x06 && !b))
7676 return arm_copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 0, 1, 0);
7677 else if ((!a && (op1 & 0x17) == 0x06)
7678 || (a && (op1 & 0x17) == 0x06 && !b))
7679 return arm_copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 0, 1, 1);
7680 else if ((!a && (op1 & 0x05) == 0x05 && (op1 & 0x17) != 0x07)
7681 || (a && (op1 & 0x05) == 0x05 && (op1 & 0x17) != 0x07 && !b))
7682 return arm_copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 1, 1, 0);
7683 else if ((!a && (op1 & 0x17) == 0x07)
7684 || (a && (op1 & 0x17) == 0x07 && !b))
7685 return arm_copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 1, 1, 1);
7687 /* Should be unreachable. */
7688 return 1;
7691 static int
7692 arm_decode_media (struct gdbarch *gdbarch, uint32_t insn,
7693 arm_displaced_step_copy_insn_closure *dsc)
7695 switch (bits (insn, 20, 24))
7697 case 0x00: case 0x01: case 0x02: case 0x03:
7698 return arm_copy_unmodified (gdbarch, insn, "parallel add/sub signed", dsc);
7700 case 0x04: case 0x05: case 0x06: case 0x07:
7701 return arm_copy_unmodified (gdbarch, insn, "parallel add/sub unsigned", dsc);
7703 case 0x08: case 0x09: case 0x0a: case 0x0b:
7704 case 0x0c: case 0x0d: case 0x0e: case 0x0f:
7705 return arm_copy_unmodified (gdbarch, insn,
7706 "decode/pack/unpack/saturate/reverse", dsc);
7708 case 0x18:
7709 if (bits (insn, 5, 7) == 0) /* op2. */
7711 if (bits (insn, 12, 15) == 0xf)
7712 return arm_copy_unmodified (gdbarch, insn, "usad8", dsc);
7713 else
7714 return arm_copy_unmodified (gdbarch, insn, "usada8", dsc);
7716 else
7717 return arm_copy_undef (gdbarch, insn, dsc);
7719 case 0x1a: case 0x1b:
7720 if (bits (insn, 5, 6) == 0x2) /* op2[1:0]. */
7721 return arm_copy_unmodified (gdbarch, insn, "sbfx", dsc);
7722 else
7723 return arm_copy_undef (gdbarch, insn, dsc);
7725 case 0x1c: case 0x1d:
7726 if (bits (insn, 5, 6) == 0x0) /* op2[1:0]. */
7728 if (bits (insn, 0, 3) == 0xf)
7729 return arm_copy_unmodified (gdbarch, insn, "bfc", dsc);
7730 else
7731 return arm_copy_unmodified (gdbarch, insn, "bfi", dsc);
7733 else
7734 return arm_copy_undef (gdbarch, insn, dsc);
7736 case 0x1e: case 0x1f:
7737 if (bits (insn, 5, 6) == 0x2) /* op2[1:0]. */
7738 return arm_copy_unmodified (gdbarch, insn, "ubfx", dsc);
7739 else
7740 return arm_copy_undef (gdbarch, insn, dsc);
7743 /* Should be unreachable. */
7744 return 1;
7747 static int
7748 arm_decode_b_bl_ldmstm (struct gdbarch *gdbarch, uint32_t insn,
7749 struct regcache *regs,
7750 arm_displaced_step_copy_insn_closure *dsc)
7752 if (bit (insn, 25))
7753 return arm_copy_b_bl_blx (gdbarch, insn, regs, dsc);
7754 else
7755 return arm_copy_block_xfer (gdbarch, insn, regs, dsc);
7758 static int
7759 arm_decode_ext_reg_ld_st (struct gdbarch *gdbarch, uint32_t insn,
7760 struct regcache *regs,
7761 arm_displaced_step_copy_insn_closure *dsc)
7763 unsigned int opcode = bits (insn, 20, 24);
7765 switch (opcode)
7767 case 0x04: case 0x05: /* VFP/Neon mrrc/mcrr. */
7768 return arm_copy_unmodified (gdbarch, insn, "vfp/neon mrrc/mcrr", dsc);
7770 case 0x08: case 0x0a: case 0x0c: case 0x0e:
7771 case 0x12: case 0x16:
7772 return arm_copy_unmodified (gdbarch, insn, "vfp/neon vstm/vpush", dsc);
7774 case 0x09: case 0x0b: case 0x0d: case 0x0f:
7775 case 0x13: case 0x17:
7776 return arm_copy_unmodified (gdbarch, insn, "vfp/neon vldm/vpop", dsc);
7778 case 0x10: case 0x14: case 0x18: case 0x1c: /* vstr. */
7779 case 0x11: case 0x15: case 0x19: case 0x1d: /* vldr. */
7780 /* Note: no writeback for these instructions. Bit 25 will always be
7781 zero though (via caller), so the following works OK. */
7782 return arm_copy_copro_load_store (gdbarch, insn, regs, dsc);
7785 /* Should be unreachable. */
7786 return 1;
7789 /* Decode shifted register instructions. */
7791 static int
7792 thumb2_decode_dp_shift_reg (struct gdbarch *gdbarch, uint16_t insn1,
7793 uint16_t insn2, struct regcache *regs,
7794 arm_displaced_step_copy_insn_closure *dsc)
7796 /* PC is only allowed to be used in instruction MOV. */
7798 unsigned int op = bits (insn1, 5, 8);
7799 unsigned int rn = bits (insn1, 0, 3);
7801 if (op == 0x2 && rn == 0xf) /* MOV */
7802 return thumb2_copy_alu_imm (gdbarch, insn1, insn2, regs, dsc);
7803 else
7804 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
7805 "dp (shift reg)", dsc);
7809 /* Decode extension register load/store. Exactly the same as
7810 arm_decode_ext_reg_ld_st. */
7812 static int
7813 thumb2_decode_ext_reg_ld_st (struct gdbarch *gdbarch, uint16_t insn1,
7814 uint16_t insn2, struct regcache *regs,
7815 arm_displaced_step_copy_insn_closure *dsc)
7817 unsigned int opcode = bits (insn1, 4, 8);
7819 switch (opcode)
7821 case 0x04: case 0x05:
7822 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
7823 "vfp/neon vmov", dsc);
7825 case 0x08: case 0x0c: /* 01x00 */
7826 case 0x0a: case 0x0e: /* 01x10 */
7827 case 0x12: case 0x16: /* 10x10 */
7828 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
7829 "vfp/neon vstm/vpush", dsc);
7831 case 0x09: case 0x0d: /* 01x01 */
7832 case 0x0b: case 0x0f: /* 01x11 */
7833 case 0x13: case 0x17: /* 10x11 */
7834 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
7835 "vfp/neon vldm/vpop", dsc);
7837 case 0x10: case 0x14: case 0x18: case 0x1c: /* vstr. */
7838 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
7839 "vstr", dsc);
7840 case 0x11: case 0x15: case 0x19: case 0x1d: /* vldr. */
7841 return thumb2_copy_copro_load_store (gdbarch, insn1, insn2, regs, dsc);
7844 /* Should be unreachable. */
7845 return 1;
7848 static int
7849 arm_decode_svc_copro (struct gdbarch *gdbarch, uint32_t insn,
7850 regcache *regs, arm_displaced_step_copy_insn_closure *dsc)
7852 unsigned int op1 = bits (insn, 20, 25);
7853 int op = bit (insn, 4);
7854 unsigned int coproc = bits (insn, 8, 11);
7856 if ((op1 & 0x20) == 0x00 && (op1 & 0x3a) != 0x00 && (coproc & 0xe) == 0xa)
7857 return arm_decode_ext_reg_ld_st (gdbarch, insn, regs, dsc);
7858 else if ((op1 & 0x21) == 0x00 && (op1 & 0x3a) != 0x00
7859 && (coproc & 0xe) != 0xa)
7860 /* stc/stc2. */
7861 return arm_copy_copro_load_store (gdbarch, insn, regs, dsc);
7862 else if ((op1 & 0x21) == 0x01 && (op1 & 0x3a) != 0x00
7863 && (coproc & 0xe) != 0xa)
7864 /* ldc/ldc2 imm/lit. */
7865 return arm_copy_copro_load_store (gdbarch, insn, regs, dsc);
7866 else if ((op1 & 0x3e) == 0x00)
7867 return arm_copy_undef (gdbarch, insn, dsc);
7868 else if ((op1 & 0x3e) == 0x04 && (coproc & 0xe) == 0xa)
7869 return arm_copy_unmodified (gdbarch, insn, "neon 64bit xfer", dsc);
7870 else if (op1 == 0x04 && (coproc & 0xe) != 0xa)
7871 return arm_copy_unmodified (gdbarch, insn, "mcrr/mcrr2", dsc);
7872 else if (op1 == 0x05 && (coproc & 0xe) != 0xa)
7873 return arm_copy_unmodified (gdbarch, insn, "mrrc/mrrc2", dsc);
7874 else if ((op1 & 0x30) == 0x20 && !op)
7876 if ((coproc & 0xe) == 0xa)
7877 return arm_copy_unmodified (gdbarch, insn, "vfp dataproc", dsc);
7878 else
7879 return arm_copy_unmodified (gdbarch, insn, "cdp/cdp2", dsc);
7881 else if ((op1 & 0x30) == 0x20 && op)
7882 return arm_copy_unmodified (gdbarch, insn, "neon 8/16/32 bit xfer", dsc);
7883 else if ((op1 & 0x31) == 0x20 && op && (coproc & 0xe) != 0xa)
7884 return arm_copy_unmodified (gdbarch, insn, "mcr/mcr2", dsc);
7885 else if ((op1 & 0x31) == 0x21 && op && (coproc & 0xe) != 0xa)
7886 return arm_copy_unmodified (gdbarch, insn, "mrc/mrc2", dsc);
7887 else if ((op1 & 0x30) == 0x30)
7888 return arm_copy_svc (gdbarch, insn, regs, dsc);
7889 else
7890 return arm_copy_undef (gdbarch, insn, dsc); /* Possibly unreachable. */
7893 static int
7894 thumb2_decode_svc_copro (struct gdbarch *gdbarch, uint16_t insn1,
7895 uint16_t insn2, struct regcache *regs,
7896 arm_displaced_step_copy_insn_closure *dsc)
7898 unsigned int coproc = bits (insn2, 8, 11);
7899 unsigned int bit_5_8 = bits (insn1, 5, 8);
7900 unsigned int bit_9 = bit (insn1, 9);
7901 unsigned int bit_4 = bit (insn1, 4);
7903 if (bit_9 == 0)
7905 if (bit_5_8 == 2)
7906 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
7907 "neon 64bit xfer/mrrc/mrrc2/mcrr/mcrr2",
7908 dsc);
7909 else if (bit_5_8 == 0) /* UNDEFINED. */
7910 return thumb_32bit_copy_undef (gdbarch, insn1, insn2, dsc);
7911 else
7913 /*coproc is 101x. SIMD/VFP, ext registers load/store. */
7914 if ((coproc & 0xe) == 0xa)
7915 return thumb2_decode_ext_reg_ld_st (gdbarch, insn1, insn2, regs,
7916 dsc);
7917 else /* coproc is not 101x. */
7919 if (bit_4 == 0) /* STC/STC2. */
7920 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
7921 "stc/stc2", dsc);
7922 else /* LDC/LDC2 {literal, immediate}. */
7923 return thumb2_copy_copro_load_store (gdbarch, insn1, insn2,
7924 regs, dsc);
7928 else
7929 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2, "coproc", dsc);
7931 return 0;
7934 static void
7935 install_pc_relative (struct gdbarch *gdbarch, struct regcache *regs,
7936 arm_displaced_step_copy_insn_closure *dsc, int rd)
7938 /* ADR Rd, #imm
7940 Rewrite as:
7942 Preparation: Rd <- PC
7943 Insn: ADD Rd, #imm
7944 Cleanup: Null.
7947 /* Rd <- PC */
7948 int val = displaced_read_reg (regs, dsc, ARM_PC_REGNUM);
7949 displaced_write_reg (regs, dsc, rd, val, CANNOT_WRITE_PC);
7952 static int
7953 thumb_copy_pc_relative_16bit (struct gdbarch *gdbarch, struct regcache *regs,
7954 arm_displaced_step_copy_insn_closure *dsc,
7955 int rd, unsigned int imm)
7958 /* Encoding T2: ADDS Rd, #imm */
7959 dsc->modinsn[0] = (0x3000 | (rd << 8) | imm);
7961 install_pc_relative (gdbarch, regs, dsc, rd);
7963 return 0;
7966 static int
7967 thumb_decode_pc_relative_16bit (struct gdbarch *gdbarch, uint16_t insn,
7968 struct regcache *regs,
7969 arm_displaced_step_copy_insn_closure *dsc)
7971 unsigned int rd = bits (insn, 8, 10);
7972 unsigned int imm8 = bits (insn, 0, 7);
7974 displaced_debug_printf ("copying thumb adr r%d, #%d insn %.4x",
7975 rd, imm8, insn);
7977 return thumb_copy_pc_relative_16bit (gdbarch, regs, dsc, rd, imm8);
7980 static int
7981 thumb_copy_pc_relative_32bit (struct gdbarch *gdbarch, uint16_t insn1,
7982 uint16_t insn2, struct regcache *regs,
7983 arm_displaced_step_copy_insn_closure *dsc)
7985 unsigned int rd = bits (insn2, 8, 11);
7986 /* Since immediate has the same encoding in ADR ADD and SUB, so we simply
7987 extract raw immediate encoding rather than computing immediate. When
7988 generating ADD or SUB instruction, we can simply perform OR operation to
7989 set immediate into ADD. */
7990 unsigned int imm_3_8 = insn2 & 0x70ff;
7991 unsigned int imm_i = insn1 & 0x0400; /* Clear all bits except bit 10. */
7993 displaced_debug_printf ("copying thumb adr r%d, #%d:%d insn %.4x%.4x",
7994 rd, imm_i, imm_3_8, insn1, insn2);
7996 if (bit (insn1, 7)) /* Encoding T2 */
7998 /* Encoding T3: SUB Rd, Rd, #imm */
7999 dsc->modinsn[0] = (0xf1a0 | rd | imm_i);
8000 dsc->modinsn[1] = ((rd << 8) | imm_3_8);
8002 else /* Encoding T3 */
8004 /* Encoding T3: ADD Rd, Rd, #imm */
8005 dsc->modinsn[0] = (0xf100 | rd | imm_i);
8006 dsc->modinsn[1] = ((rd << 8) | imm_3_8);
8008 dsc->numinsns = 2;
8010 install_pc_relative (gdbarch, regs, dsc, rd);
8012 return 0;
8015 static int
8016 thumb_copy_16bit_ldr_literal (struct gdbarch *gdbarch, uint16_t insn1,
8017 struct regcache *regs,
8018 arm_displaced_step_copy_insn_closure *dsc)
8020 unsigned int rt = bits (insn1, 8, 10);
8021 unsigned int pc;
8022 int imm8 = (bits (insn1, 0, 7) << 2);
8024 /* LDR Rd, #imm8
8026 Rwrite as:
8028 Preparation: tmp0 <- R0, tmp2 <- R2, tmp3 <- R3, R2 <- PC, R3 <- #imm8;
8030 Insn: LDR R0, [R2, R3];
8031 Cleanup: R2 <- tmp2, R3 <- tmp3, Rd <- R0, R0 <- tmp0 */
8033 displaced_debug_printf ("copying thumb ldr r%d [pc #%d]", rt, imm8);
8035 dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
8036 dsc->tmp[2] = displaced_read_reg (regs, dsc, 2);
8037 dsc->tmp[3] = displaced_read_reg (regs, dsc, 3);
8038 pc = displaced_read_reg (regs, dsc, ARM_PC_REGNUM);
8039 /* The assembler calculates the required value of the offset from the
8040 Align(PC,4) value of this instruction to the label. */
8041 pc = pc & 0xfffffffc;
8043 displaced_write_reg (regs, dsc, 2, pc, CANNOT_WRITE_PC);
8044 displaced_write_reg (regs, dsc, 3, imm8, CANNOT_WRITE_PC);
8046 dsc->rd = rt;
8047 dsc->u.ldst.xfersize = 4;
8048 dsc->u.ldst.rn = 0;
8049 dsc->u.ldst.immed = 0;
8050 dsc->u.ldst.writeback = 0;
8051 dsc->u.ldst.restore_r4 = 0;
8053 dsc->modinsn[0] = 0x58d0; /* ldr r0, [r2, r3]*/
8055 dsc->cleanup = &cleanup_load;
8057 return 0;
8060 /* Copy Thumb cbnz/cbz instruction. */
8062 static int
8063 thumb_copy_cbnz_cbz (struct gdbarch *gdbarch, uint16_t insn1,
8064 struct regcache *regs,
8065 arm_displaced_step_copy_insn_closure *dsc)
8067 int non_zero = bit (insn1, 11);
8068 unsigned int imm5 = (bit (insn1, 9) << 6) | (bits (insn1, 3, 7) << 1);
8069 CORE_ADDR from = dsc->insn_addr;
8070 int rn = bits (insn1, 0, 2);
8071 int rn_val = displaced_read_reg (regs, dsc, rn);
8073 dsc->u.branch.cond = (rn_val && non_zero) || (!rn_val && !non_zero);
8074 /* CBNZ and CBZ do not affect the condition flags. If condition is true,
8075 set it INST_AL, so cleanup_branch will know branch is taken, otherwise,
8076 condition is false, let it be, cleanup_branch will do nothing. */
8077 if (dsc->u.branch.cond)
8079 dsc->u.branch.cond = INST_AL;
8080 dsc->u.branch.dest = from + 4 + imm5;
8082 else
8083 dsc->u.branch.dest = from + 2;
8085 dsc->u.branch.link = 0;
8086 dsc->u.branch.exchange = 0;
8088 displaced_debug_printf ("copying %s [r%d = 0x%x] insn %.4x to %.8lx",
8089 non_zero ? "cbnz" : "cbz",
8090 rn, rn_val, insn1, dsc->u.branch.dest);
8092 dsc->modinsn[0] = THUMB_NOP;
8094 dsc->cleanup = &cleanup_branch;
8095 return 0;
8098 /* Copy Table Branch Byte/Halfword */
8099 static int
8100 thumb2_copy_table_branch (struct gdbarch *gdbarch, uint16_t insn1,
8101 uint16_t insn2, struct regcache *regs,
8102 arm_displaced_step_copy_insn_closure *dsc)
8104 ULONGEST rn_val, rm_val;
8105 int is_tbh = bit (insn2, 4);
8106 CORE_ADDR halfwords = 0;
8107 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
8109 rn_val = displaced_read_reg (regs, dsc, bits (insn1, 0, 3));
8110 rm_val = displaced_read_reg (regs, dsc, bits (insn2, 0, 3));
8112 if (is_tbh)
8114 gdb_byte buf[2];
8116 target_read_memory (rn_val + 2 * rm_val, buf, 2);
8117 halfwords = extract_unsigned_integer (buf, 2, byte_order);
8119 else
8121 gdb_byte buf[1];
8123 target_read_memory (rn_val + rm_val, buf, 1);
8124 halfwords = extract_unsigned_integer (buf, 1, byte_order);
8127 displaced_debug_printf ("%s base 0x%x offset 0x%x offset 0x%x",
8128 is_tbh ? "tbh" : "tbb",
8129 (unsigned int) rn_val, (unsigned int) rm_val,
8130 (unsigned int) halfwords);
8132 dsc->u.branch.cond = INST_AL;
8133 dsc->u.branch.link = 0;
8134 dsc->u.branch.exchange = 0;
8135 dsc->u.branch.dest = dsc->insn_addr + 4 + 2 * halfwords;
8137 dsc->cleanup = &cleanup_branch;
8139 return 0;
8142 static void
8143 cleanup_pop_pc_16bit_all (struct gdbarch *gdbarch, struct regcache *regs,
8144 arm_displaced_step_copy_insn_closure *dsc)
8146 /* PC <- r7 */
8147 int val = displaced_read_reg (regs, dsc, 7);
8148 displaced_write_reg (regs, dsc, ARM_PC_REGNUM, val, BX_WRITE_PC);
8150 /* r7 <- r8 */
8151 val = displaced_read_reg (regs, dsc, 8);
8152 displaced_write_reg (regs, dsc, 7, val, CANNOT_WRITE_PC);
8154 /* r8 <- tmp[0] */
8155 displaced_write_reg (regs, dsc, 8, dsc->tmp[0], CANNOT_WRITE_PC);
8159 static int
8160 thumb_copy_pop_pc_16bit (struct gdbarch *gdbarch, uint16_t insn1,
8161 struct regcache *regs,
8162 arm_displaced_step_copy_insn_closure *dsc)
8164 dsc->u.block.regmask = insn1 & 0x00ff;
8166 /* Rewrite instruction: POP {rX, rY, ...,rZ, PC}
8167 to :
8169 (1) register list is full, that is, r0-r7 are used.
8170 Prepare: tmp[0] <- r8
8172 POP {r0, r1, ...., r6, r7}; remove PC from reglist
8173 MOV r8, r7; Move value of r7 to r8;
8174 POP {r7}; Store PC value into r7.
8176 Cleanup: PC <- r7, r7 <- r8, r8 <-tmp[0]
8178 (2) register list is not full, supposing there are N registers in
8179 register list (except PC, 0 <= N <= 7).
8180 Prepare: for each i, 0 - N, tmp[i] <- ri.
8182 POP {r0, r1, ...., rN};
8184 Cleanup: Set registers in original reglist from r0 - rN. Restore r0 - rN
8185 from tmp[] properly.
8187 displaced_debug_printf ("copying thumb pop {%.8x, pc} insn %.4x",
8188 dsc->u.block.regmask, insn1);
8190 if (dsc->u.block.regmask == 0xff)
8192 dsc->tmp[0] = displaced_read_reg (regs, dsc, 8);
8194 dsc->modinsn[0] = (insn1 & 0xfeff); /* POP {r0,r1,...,r6, r7} */
8195 dsc->modinsn[1] = 0x46b8; /* MOV r8, r7 */
8196 dsc->modinsn[2] = 0xbc80; /* POP {r7} */
8198 dsc->numinsns = 3;
8199 dsc->cleanup = &cleanup_pop_pc_16bit_all;
8201 else
8203 unsigned int num_in_list = count_one_bits (dsc->u.block.regmask);
8204 unsigned int i;
8205 unsigned int new_regmask;
8207 for (i = 0; i < num_in_list + 1; i++)
8208 dsc->tmp[i] = displaced_read_reg (regs, dsc, i);
8210 new_regmask = (1 << (num_in_list + 1)) - 1;
8212 displaced_debug_printf ("POP {..., pc}: original reg list %.4x, "
8213 "modified list %.4x",
8214 (int) dsc->u.block.regmask, new_regmask);
8216 dsc->u.block.regmask |= 0x8000;
8217 dsc->u.block.writeback = 0;
8218 dsc->u.block.cond = INST_AL;
8220 dsc->modinsn[0] = (insn1 & ~0x1ff) | (new_regmask & 0xff);
8222 dsc->cleanup = &cleanup_block_load_pc;
8225 return 0;
8228 static void
8229 thumb_process_displaced_16bit_insn (struct gdbarch *gdbarch, uint16_t insn1,
8230 struct regcache *regs,
8231 arm_displaced_step_copy_insn_closure *dsc)
8233 unsigned short op_bit_12_15 = bits (insn1, 12, 15);
8234 unsigned short op_bit_10_11 = bits (insn1, 10, 11);
8235 int err = 0;
8237 /* 16-bit thumb instructions. */
8238 switch (op_bit_12_15)
8240 /* Shift (imme), add, subtract, move and compare. */
8241 case 0: case 1: case 2: case 3:
8242 err = thumb_copy_unmodified_16bit (gdbarch, insn1,
8243 "shift/add/sub/mov/cmp",
8244 dsc);
8245 break;
8246 case 4:
8247 switch (op_bit_10_11)
8249 case 0: /* Data-processing */
8250 err = thumb_copy_unmodified_16bit (gdbarch, insn1,
8251 "data-processing",
8252 dsc);
8253 break;
8254 case 1: /* Special data instructions and branch and exchange. */
8256 unsigned short op = bits (insn1, 7, 9);
8257 if (op == 6 || op == 7) /* BX or BLX */
8258 err = thumb_copy_bx_blx_reg (gdbarch, insn1, regs, dsc);
8259 else if (bits (insn1, 6, 7) != 0) /* ADD/MOV/CMP high registers. */
8260 err = thumb_copy_alu_reg (gdbarch, insn1, regs, dsc);
8261 else
8262 err = thumb_copy_unmodified_16bit (gdbarch, insn1, "special data",
8263 dsc);
8265 break;
8266 default: /* LDR (literal) */
8267 err = thumb_copy_16bit_ldr_literal (gdbarch, insn1, regs, dsc);
8269 break;
8270 case 5: case 6: case 7: case 8: case 9: /* Load/Store single data item */
8271 err = thumb_copy_unmodified_16bit (gdbarch, insn1, "ldr/str", dsc);
8272 break;
8273 case 10:
8274 if (op_bit_10_11 < 2) /* Generate PC-relative address */
8275 err = thumb_decode_pc_relative_16bit (gdbarch, insn1, regs, dsc);
8276 else /* Generate SP-relative address */
8277 err = thumb_copy_unmodified_16bit (gdbarch, insn1, "sp-relative", dsc);
8278 break;
8279 case 11: /* Misc 16-bit instructions */
8281 switch (bits (insn1, 8, 11))
8283 case 1: case 3: case 9: case 11: /* CBNZ, CBZ */
8284 err = thumb_copy_cbnz_cbz (gdbarch, insn1, regs, dsc);
8285 break;
8286 case 12: case 13: /* POP */
8287 if (bit (insn1, 8)) /* PC is in register list. */
8288 err = thumb_copy_pop_pc_16bit (gdbarch, insn1, regs, dsc);
8289 else
8290 err = thumb_copy_unmodified_16bit (gdbarch, insn1, "pop", dsc);
8291 break;
8292 case 15: /* If-Then, and hints */
8293 if (bits (insn1, 0, 3))
8294 /* If-Then makes up to four following instructions conditional.
8295 IT instruction itself is not conditional, so handle it as a
8296 common unmodified instruction. */
8297 err = thumb_copy_unmodified_16bit (gdbarch, insn1, "If-Then",
8298 dsc);
8299 else
8300 err = thumb_copy_unmodified_16bit (gdbarch, insn1, "hints", dsc);
8301 break;
8302 default:
8303 err = thumb_copy_unmodified_16bit (gdbarch, insn1, "misc", dsc);
8306 break;
8307 case 12:
8308 if (op_bit_10_11 < 2) /* Store multiple registers */
8309 err = thumb_copy_unmodified_16bit (gdbarch, insn1, "stm", dsc);
8310 else /* Load multiple registers */
8311 err = thumb_copy_unmodified_16bit (gdbarch, insn1, "ldm", dsc);
8312 break;
8313 case 13: /* Conditional branch and supervisor call */
8314 if (bits (insn1, 9, 11) != 7) /* conditional branch */
8315 err = thumb_copy_b (gdbarch, insn1, dsc);
8316 else
8317 err = thumb_copy_svc (gdbarch, insn1, regs, dsc);
8318 break;
8319 case 14: /* Unconditional branch */
8320 err = thumb_copy_b (gdbarch, insn1, dsc);
8321 break;
8322 default:
8323 err = 1;
8326 if (err)
8327 internal_error (_("thumb_process_displaced_16bit_insn: Instruction decode error"));
8330 static int
8331 decode_thumb_32bit_ld_mem_hints (struct gdbarch *gdbarch,
8332 uint16_t insn1, uint16_t insn2,
8333 struct regcache *regs,
8334 arm_displaced_step_copy_insn_closure *dsc)
8336 int rt = bits (insn2, 12, 15);
8337 int rn = bits (insn1, 0, 3);
8338 int op1 = bits (insn1, 7, 8);
8340 switch (bits (insn1, 5, 6))
8342 case 0: /* Load byte and memory hints */
8343 if (rt == 0xf) /* PLD/PLI */
8345 if (rn == 0xf)
8346 /* PLD literal or Encoding T3 of PLI(immediate, literal). */
8347 return thumb2_copy_preload (gdbarch, insn1, insn2, regs, dsc);
8348 else
8349 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8350 "pli/pld", dsc);
8352 else
8354 if (rn == 0xf) /* LDRB/LDRSB (literal) */
8355 return thumb2_copy_load_literal (gdbarch, insn1, insn2, regs, dsc,
8357 else
8358 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8359 "ldrb{reg, immediate}/ldrbt",
8360 dsc);
8363 break;
8364 case 1: /* Load halfword and memory hints. */
8365 if (rt == 0xf) /* PLD{W} and Unalloc memory hint. */
8366 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8367 "pld/unalloc memhint", dsc);
8368 else
8370 if (rn == 0xf)
8371 return thumb2_copy_load_literal (gdbarch, insn1, insn2, regs, dsc,
8373 else
8374 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8375 "ldrh/ldrht", dsc);
8377 break;
8378 case 2: /* Load word */
8380 int insn2_bit_8_11 = bits (insn2, 8, 11);
8382 if (rn == 0xf)
8383 return thumb2_copy_load_literal (gdbarch, insn1, insn2, regs, dsc, 4);
8384 else if (op1 == 0x1) /* Encoding T3 */
8385 return thumb2_copy_load_reg_imm (gdbarch, insn1, insn2, regs, dsc,
8386 0, 1);
8387 else /* op1 == 0x0 */
8389 if (insn2_bit_8_11 == 0xc || (insn2_bit_8_11 & 0x9) == 0x9)
8390 /* LDR (immediate) */
8391 return thumb2_copy_load_reg_imm (gdbarch, insn1, insn2, regs,
8392 dsc, bit (insn2, 8), 1);
8393 else if (insn2_bit_8_11 == 0xe) /* LDRT */
8394 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8395 "ldrt", dsc);
8396 else
8397 /* LDR (register) */
8398 return thumb2_copy_load_reg_imm (gdbarch, insn1, insn2, regs,
8399 dsc, 0, 0);
8401 break;
8403 default:
8404 return thumb_32bit_copy_undef (gdbarch, insn1, insn2, dsc);
8405 break;
8407 return 0;
8410 static void
8411 thumb_process_displaced_32bit_insn (struct gdbarch *gdbarch, uint16_t insn1,
8412 uint16_t insn2, struct regcache *regs,
8413 arm_displaced_step_copy_insn_closure *dsc)
8415 int err = 0;
8416 unsigned short op = bit (insn2, 15);
8417 unsigned int op1 = bits (insn1, 11, 12);
8419 switch (op1)
8421 case 1:
8423 switch (bits (insn1, 9, 10))
8425 case 0:
8426 if (bit (insn1, 6))
8428 /* Load/store {dual, exclusive}, table branch. */
8429 if (bits (insn1, 7, 8) == 1 && bits (insn1, 4, 5) == 1
8430 && bits (insn2, 5, 7) == 0)
8431 err = thumb2_copy_table_branch (gdbarch, insn1, insn2, regs,
8432 dsc);
8433 else
8434 /* PC is not allowed to use in load/store {dual, exclusive}
8435 instructions. */
8436 err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8437 "load/store dual/ex", dsc);
8439 else /* load/store multiple */
8441 switch (bits (insn1, 7, 8))
8443 case 0: case 3: /* SRS, RFE */
8444 err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8445 "srs/rfe", dsc);
8446 break;
8447 case 1: case 2: /* LDM/STM/PUSH/POP */
8448 err = thumb2_copy_block_xfer (gdbarch, insn1, insn2, regs, dsc);
8449 break;
8452 break;
8454 case 1:
8455 /* Data-processing (shift register). */
8456 err = thumb2_decode_dp_shift_reg (gdbarch, insn1, insn2, regs,
8457 dsc);
8458 break;
8459 default: /* Coprocessor instructions. */
8460 err = thumb2_decode_svc_copro (gdbarch, insn1, insn2, regs, dsc);
8461 break;
8463 break;
8465 case 2: /* op1 = 2 */
8466 if (op) /* Branch and misc control. */
8468 if (bit (insn2, 14) /* BLX/BL */
8469 || bit (insn2, 12) /* Unconditional branch */
8470 || (bits (insn1, 7, 9) != 0x7)) /* Conditional branch */
8471 err = thumb2_copy_b_bl_blx (gdbarch, insn1, insn2, regs, dsc);
8472 else
8473 err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8474 "misc ctrl", dsc);
8476 else
8478 if (bit (insn1, 9)) /* Data processing (plain binary imm). */
8480 int dp_op = bits (insn1, 4, 8);
8481 int rn = bits (insn1, 0, 3);
8482 if ((dp_op == 0 || dp_op == 0xa) && rn == 0xf)
8483 err = thumb_copy_pc_relative_32bit (gdbarch, insn1, insn2,
8484 regs, dsc);
8485 else
8486 err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8487 "dp/pb", dsc);
8489 else /* Data processing (modified immediate) */
8490 err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8491 "dp/mi", dsc);
8493 break;
8494 case 3: /* op1 = 3 */
8495 switch (bits (insn1, 9, 10))
8497 case 0:
8498 if (bit (insn1, 4))
8499 err = decode_thumb_32bit_ld_mem_hints (gdbarch, insn1, insn2,
8500 regs, dsc);
8501 else /* NEON Load/Store and Store single data item */
8502 err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8503 "neon elt/struct load/store",
8504 dsc);
8505 break;
8506 case 1: /* op1 = 3, bits (9, 10) == 1 */
8507 switch (bits (insn1, 7, 8))
8509 case 0: case 1: /* Data processing (register) */
8510 err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8511 "dp(reg)", dsc);
8512 break;
8513 case 2: /* Multiply and absolute difference */
8514 err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8515 "mul/mua/diff", dsc);
8516 break;
8517 case 3: /* Long multiply and divide */
8518 err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8519 "lmul/lmua", dsc);
8520 break;
8522 break;
8523 default: /* Coprocessor instructions */
8524 err = thumb2_decode_svc_copro (gdbarch, insn1, insn2, regs, dsc);
8525 break;
8527 break;
8528 default:
8529 err = 1;
8532 if (err)
8533 internal_error (_("thumb_process_displaced_32bit_insn: Instruction decode error"));
8537 static void
8538 thumb_process_displaced_insn (struct gdbarch *gdbarch, CORE_ADDR from,
8539 struct regcache *regs,
8540 arm_displaced_step_copy_insn_closure *dsc)
8542 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
8543 uint16_t insn1
8544 = read_memory_unsigned_integer (from, 2, byte_order_for_code);
8546 displaced_debug_printf ("process thumb insn %.4x at %.8lx",
8547 insn1, (unsigned long) from);
8549 dsc->is_thumb = 1;
8550 dsc->insn_size = thumb_insn_size (insn1);
8551 if (thumb_insn_size (insn1) == 4)
8553 uint16_t insn2
8554 = read_memory_unsigned_integer (from + 2, 2, byte_order_for_code);
8555 thumb_process_displaced_32bit_insn (gdbarch, insn1, insn2, regs, dsc);
8557 else
8558 thumb_process_displaced_16bit_insn (gdbarch, insn1, regs, dsc);
8561 void
8562 arm_process_displaced_insn (struct gdbarch *gdbarch, CORE_ADDR from,
8563 CORE_ADDR to, struct regcache *regs,
8564 arm_displaced_step_copy_insn_closure *dsc)
8566 int err = 0;
8567 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
8568 uint32_t insn;
8570 /* Most displaced instructions use a 1-instruction scratch space, so set this
8571 here and override below if/when necessary. */
8572 dsc->numinsns = 1;
8573 dsc->insn_addr = from;
8574 dsc->scratch_base = to;
8575 dsc->cleanup = NULL;
8576 dsc->wrote_to_pc = 0;
8578 if (!displaced_in_arm_mode (regs))
8579 return thumb_process_displaced_insn (gdbarch, from, regs, dsc);
8581 dsc->is_thumb = 0;
8582 dsc->insn_size = 4;
8583 insn = read_memory_unsigned_integer (from, 4, byte_order_for_code);
8584 displaced_debug_printf ("stepping insn %.8lx at %.8lx",
8585 (unsigned long) insn, (unsigned long) from);
8587 if ((insn & 0xf0000000) == 0xf0000000)
8588 err = arm_decode_unconditional (gdbarch, insn, regs, dsc);
8589 else switch (((insn & 0x10) >> 4) | ((insn & 0xe000000) >> 24))
8591 case 0x0: case 0x1: case 0x2: case 0x3:
8592 err = arm_decode_dp_misc (gdbarch, insn, regs, dsc);
8593 break;
8595 case 0x4: case 0x5: case 0x6:
8596 err = arm_decode_ld_st_word_ubyte (gdbarch, insn, regs, dsc);
8597 break;
8599 case 0x7:
8600 err = arm_decode_media (gdbarch, insn, dsc);
8601 break;
8603 case 0x8: case 0x9: case 0xa: case 0xb:
8604 err = arm_decode_b_bl_ldmstm (gdbarch, insn, regs, dsc);
8605 break;
8607 case 0xc: case 0xd: case 0xe: case 0xf:
8608 err = arm_decode_svc_copro (gdbarch, insn, regs, dsc);
8609 break;
8612 if (err)
8613 internal_error (_("arm_process_displaced_insn: Instruction decode error"));
8616 /* Actually set up the scratch space for a displaced instruction. */
8618 void
8619 arm_displaced_init_closure (struct gdbarch *gdbarch, CORE_ADDR from,
8620 CORE_ADDR to,
8621 arm_displaced_step_copy_insn_closure *dsc)
8623 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
8624 unsigned int i, len, offset;
8625 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
8626 int size = dsc->is_thumb ? 2 : 4;
8627 const gdb_byte *bkp_insn;
8629 offset = 0;
8630 /* Poke modified instruction(s). */
8631 for (i = 0; i < dsc->numinsns; i++)
8633 if (size == 4)
8634 displaced_debug_printf ("writing insn %.8lx at %.8lx",
8635 dsc->modinsn[i], (unsigned long) to + offset);
8636 else if (size == 2)
8637 displaced_debug_printf ("writing insn %.4x at %.8lx",
8638 (unsigned short) dsc->modinsn[i],
8639 (unsigned long) to + offset);
8641 write_memory_unsigned_integer (to + offset, size,
8642 byte_order_for_code,
8643 dsc->modinsn[i]);
8644 offset += size;
8647 /* Choose the correct breakpoint instruction. */
8648 if (dsc->is_thumb)
8650 bkp_insn = tdep->thumb_breakpoint;
8651 len = tdep->thumb_breakpoint_size;
8653 else
8655 bkp_insn = tdep->arm_breakpoint;
8656 len = tdep->arm_breakpoint_size;
8659 /* Put breakpoint afterwards. */
8660 write_memory (to + offset, bkp_insn, len);
8662 displaced_debug_printf ("copy %s->%s", paddress (gdbarch, from),
8663 paddress (gdbarch, to));
8666 /* Entry point for cleaning things up after a displaced instruction has been
8667 single-stepped. */
8669 void
8670 arm_displaced_step_fixup (struct gdbarch *gdbarch,
8671 struct displaced_step_copy_insn_closure *dsc_,
8672 CORE_ADDR from, CORE_ADDR to,
8673 struct regcache *regs, bool completed_p)
8675 /* The following block exists as a temporary measure while displaced
8676 stepping is fixed architecture at a time within GDB.
8678 In an earlier implementation of displaced stepping, if GDB thought the
8679 displaced instruction had not been executed then this fix up function
8680 was never called. As a consequence, things that should be fixed by
8681 this function were left in an unfixed state.
8683 However, it's not as simple as always calling this function; this
8684 function needs to be updated to decide what should be fixed up based
8685 on whether the displaced step executed or not, which requires each
8686 architecture to be considered individually.
8688 Until this architecture is updated, this block replicates the old
8689 behaviour; we just restore the program counter register, and leave
8690 everything else unfixed. */
8691 if (!completed_p)
8693 CORE_ADDR pc = regcache_read_pc (regs);
8694 pc = from + (pc - to);
8695 regcache_write_pc (regs, pc);
8696 return;
8699 arm_displaced_step_copy_insn_closure *dsc
8700 = (arm_displaced_step_copy_insn_closure *) dsc_;
8702 if (dsc->cleanup)
8703 dsc->cleanup (gdbarch, regs, dsc);
8705 if (!dsc->wrote_to_pc)
8706 regcache_cooked_write_unsigned (regs, ARM_PC_REGNUM,
8707 dsc->insn_addr + dsc->insn_size);
8711 #include "bfd-in2.h"
8712 #include "libcoff.h"
8714 static int
8715 gdb_print_insn_arm (bfd_vma memaddr, disassemble_info *info)
8717 gdb_disassemble_info *di
8718 = static_cast<gdb_disassemble_info *> (info->application_data);
8719 struct gdbarch *gdbarch = di->arch ();
8721 if (arm_pc_is_thumb (gdbarch, memaddr))
8723 static asymbol *asym;
8724 static combined_entry_type ce;
8725 static struct coff_symbol_struct csym;
8726 static struct bfd fake_bfd;
8727 static bfd_target fake_target;
8729 if (csym.native == NULL)
8731 /* Create a fake symbol vector containing a Thumb symbol.
8732 This is solely so that the code in print_insn_little_arm()
8733 and print_insn_big_arm() in opcodes/arm-dis.c will detect
8734 the presence of a Thumb symbol and switch to decoding
8735 Thumb instructions. */
8737 fake_target.flavour = bfd_target_coff_flavour;
8738 fake_bfd.xvec = &fake_target;
8739 ce.u.syment.n_sclass = C_THUMBEXTFUNC;
8740 csym.native = &ce;
8741 csym.symbol.the_bfd = &fake_bfd;
8742 csym.symbol.name = "fake";
8743 asym = (asymbol *) & csym;
8746 memaddr = UNMAKE_THUMB_ADDR (memaddr);
8747 info->symbols = &asym;
8749 else
8750 info->symbols = NULL;
8752 /* GDB is able to get bfd_mach from the exe_bfd, info->mach is
8753 accurate, so mark USER_SPECIFIED_MACHINE_TYPE bit. Otherwise,
8754 opcodes/arm-dis.c:print_insn reset info->mach, and it will trigger
8755 the assert on the mismatch of info->mach and
8756 bfd_get_mach (current_program_space->exec_bfd ()) in
8757 default_print_insn. */
8758 if (current_program_space->exec_bfd () != NULL
8759 && (current_program_space->exec_bfd ()->arch_info
8760 == gdbarch_bfd_arch_info (gdbarch)))
8761 info->flags |= USER_SPECIFIED_MACHINE_TYPE;
8763 return default_print_insn (memaddr, info);
8766 /* The following define instruction sequences that will cause ARM
8767 cpu's to take an undefined instruction trap. These are used to
8768 signal a breakpoint to GDB.
8770 The newer ARMv4T cpu's are capable of operating in ARM or Thumb
8771 modes. A different instruction is required for each mode. The ARM
8772 cpu's can also be big or little endian. Thus four different
8773 instructions are needed to support all cases.
8775 Note: ARMv4 defines several new instructions that will take the
8776 undefined instruction trap. ARM7TDMI is nominally ARMv4T, but does
8777 not in fact add the new instructions. The new undefined
8778 instructions in ARMv4 are all instructions that had no defined
8779 behaviour in earlier chips. There is no guarantee that they will
8780 raise an exception, but may be treated as NOP's. In practice, it
8781 may only safe to rely on instructions matching:
8783 3 3 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1
8784 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
8785 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
8787 Even this may only true if the condition predicate is true. The
8788 following use a condition predicate of ALWAYS so it is always TRUE.
8790 There are other ways of forcing a breakpoint. GNU/Linux, RISC iX,
8791 and NetBSD all use a software interrupt rather than an undefined
8792 instruction to force a trap. This can be handled by by the
8793 abi-specific code during establishment of the gdbarch vector. */
8795 #define ARM_LE_BREAKPOINT {0xFE,0xDE,0xFF,0xE7}
8796 #define ARM_BE_BREAKPOINT {0xE7,0xFF,0xDE,0xFE}
8797 #define THUMB_LE_BREAKPOINT {0xbe,0xbe}
8798 #define THUMB_BE_BREAKPOINT {0xbe,0xbe}
8800 static const gdb_byte arm_default_arm_le_breakpoint[] = ARM_LE_BREAKPOINT;
8801 static const gdb_byte arm_default_arm_be_breakpoint[] = ARM_BE_BREAKPOINT;
8802 static const gdb_byte arm_default_thumb_le_breakpoint[] = THUMB_LE_BREAKPOINT;
8803 static const gdb_byte arm_default_thumb_be_breakpoint[] = THUMB_BE_BREAKPOINT;
8805 /* Implement the breakpoint_kind_from_pc gdbarch method. */
8807 static int
8808 arm_breakpoint_kind_from_pc (struct gdbarch *gdbarch, CORE_ADDR *pcptr)
8810 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
8811 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
8813 if (arm_pc_is_thumb (gdbarch, *pcptr))
8815 *pcptr = UNMAKE_THUMB_ADDR (*pcptr);
8817 /* If we have a separate 32-bit breakpoint instruction for Thumb-2,
8818 check whether we are replacing a 32-bit instruction. */
8819 if (tdep->thumb2_breakpoint != NULL)
8821 gdb_byte buf[2];
8823 if (target_read_memory (*pcptr, buf, 2) == 0)
8825 unsigned short inst1;
8827 inst1 = extract_unsigned_integer (buf, 2, byte_order_for_code);
8828 if (thumb_insn_size (inst1) == 4)
8829 return ARM_BP_KIND_THUMB2;
8833 return ARM_BP_KIND_THUMB;
8835 else
8836 return ARM_BP_KIND_ARM;
8840 /* Implement the sw_breakpoint_from_kind gdbarch method. */
8842 static const gdb_byte *
8843 arm_sw_breakpoint_from_kind (struct gdbarch *gdbarch, int kind, int *size)
8845 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
8847 switch (kind)
8849 case ARM_BP_KIND_ARM:
8850 *size = tdep->arm_breakpoint_size;
8851 return tdep->arm_breakpoint;
8852 case ARM_BP_KIND_THUMB:
8853 *size = tdep->thumb_breakpoint_size;
8854 return tdep->thumb_breakpoint;
8855 case ARM_BP_KIND_THUMB2:
8856 *size = tdep->thumb2_breakpoint_size;
8857 return tdep->thumb2_breakpoint;
8858 default:
8859 gdb_assert_not_reached ("unexpected arm breakpoint kind");
8863 /* Implement the breakpoint_kind_from_current_state gdbarch method. */
8865 static int
8866 arm_breakpoint_kind_from_current_state (struct gdbarch *gdbarch,
8867 struct regcache *regcache,
8868 CORE_ADDR *pcptr)
8870 gdb_byte buf[4];
8872 /* Check the memory pointed by PC is readable. */
8873 if (target_read_memory (regcache_read_pc (regcache), buf, 4) == 0)
8875 struct arm_get_next_pcs next_pcs_ctx;
8877 arm_get_next_pcs_ctor (&next_pcs_ctx,
8878 &arm_get_next_pcs_ops,
8879 gdbarch_byte_order (gdbarch),
8880 gdbarch_byte_order_for_code (gdbarch),
8882 regcache);
8884 std::vector<CORE_ADDR> next_pcs = arm_get_next_pcs (&next_pcs_ctx);
8886 /* If MEMADDR is the next instruction of current pc, do the
8887 software single step computation, and get the thumb mode by
8888 the destination address. */
8889 for (CORE_ADDR pc : next_pcs)
8891 if (UNMAKE_THUMB_ADDR (pc) == *pcptr)
8893 if (IS_THUMB_ADDR (pc))
8895 *pcptr = MAKE_THUMB_ADDR (*pcptr);
8896 return arm_breakpoint_kind_from_pc (gdbarch, pcptr);
8898 else
8899 return ARM_BP_KIND_ARM;
8904 return arm_breakpoint_kind_from_pc (gdbarch, pcptr);
8907 /* Extract from an array REGBUF containing the (raw) register state a
8908 function return value of type TYPE, and copy that, in virtual
8909 format, into VALBUF. */
8911 static void
8912 arm_extract_return_value (struct type *type, struct regcache *regs,
8913 gdb_byte *valbuf)
8915 struct gdbarch *gdbarch = regs->arch ();
8916 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
8917 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
8919 while (type->code () == TYPE_CODE_RANGE)
8920 type = check_typedef (type->target_type ());
8922 if (TYPE_CODE_FLT == type->code ())
8924 switch (tdep->fp_model)
8926 case ARM_FLOAT_FPA:
8928 /* The value is in register F0 in internal format. We need to
8929 extract the raw value and then convert it to the desired
8930 internal type. */
8931 bfd_byte tmpbuf[ARM_FP_REGISTER_SIZE];
8933 regs->cooked_read (ARM_F0_REGNUM, tmpbuf);
8934 target_float_convert (tmpbuf, arm_ext_type (gdbarch),
8935 valbuf, type);
8937 break;
8939 case ARM_FLOAT_SOFT_FPA:
8940 case ARM_FLOAT_SOFT_VFP:
8941 /* ARM_FLOAT_VFP can arise if this is a variadic function so
8942 not using the VFP ABI code. */
8943 case ARM_FLOAT_VFP:
8944 regs->cooked_read (ARM_A1_REGNUM, valbuf);
8945 if (type->length () > 4)
8946 regs->cooked_read (ARM_A1_REGNUM + 1,
8947 valbuf + ARM_INT_REGISTER_SIZE);
8948 break;
8950 default:
8951 internal_error (_("arm_extract_return_value: "
8952 "Floating point model not supported"));
8953 break;
8956 else if (type->code () == TYPE_CODE_INT
8957 || type->code () == TYPE_CODE_CHAR
8958 || type->code () == TYPE_CODE_BOOL
8959 || type->code () == TYPE_CODE_PTR
8960 || TYPE_IS_REFERENCE (type)
8961 || type->code () == TYPE_CODE_ENUM
8962 || is_fixed_point_type (type))
8964 /* If the type is a plain integer, then the access is
8965 straight-forward. Otherwise we have to play around a bit
8966 more. */
8967 int len = type->length ();
8968 int regno = ARM_A1_REGNUM;
8969 ULONGEST tmp;
8971 while (len > 0)
8973 /* By using store_unsigned_integer we avoid having to do
8974 anything special for small big-endian values. */
8975 regcache_cooked_read_unsigned (regs, regno++, &tmp);
8976 store_unsigned_integer (valbuf,
8977 (len > ARM_INT_REGISTER_SIZE
8978 ? ARM_INT_REGISTER_SIZE : len),
8979 byte_order, tmp);
8980 len -= ARM_INT_REGISTER_SIZE;
8981 valbuf += ARM_INT_REGISTER_SIZE;
8984 else
8986 /* For a structure or union the behaviour is as if the value had
8987 been stored to word-aligned memory and then loaded into
8988 registers with 32-bit load instruction(s). */
8989 int len = type->length ();
8990 int regno = ARM_A1_REGNUM;
8991 bfd_byte tmpbuf[ARM_INT_REGISTER_SIZE];
8993 while (len > 0)
8995 regs->cooked_read (regno++, tmpbuf);
8996 memcpy (valbuf, tmpbuf,
8997 len > ARM_INT_REGISTER_SIZE ? ARM_INT_REGISTER_SIZE : len);
8998 len -= ARM_INT_REGISTER_SIZE;
8999 valbuf += ARM_INT_REGISTER_SIZE;
9005 /* Will a function return an aggregate type in memory or in a
9006 register? Return 0 if an aggregate type can be returned in a
9007 register, 1 if it must be returned in memory. */
9009 static int
9010 arm_return_in_memory (struct gdbarch *gdbarch, struct type *type)
9012 enum type_code code;
9014 type = check_typedef (type);
9016 /* Simple, non-aggregate types (ie not including vectors and
9017 complex) are always returned in a register (or registers). */
9018 code = type->code ();
9019 if (TYPE_CODE_STRUCT != code && TYPE_CODE_UNION != code
9020 && TYPE_CODE_ARRAY != code && TYPE_CODE_COMPLEX != code)
9021 return 0;
9023 if (TYPE_HAS_DYNAMIC_LENGTH (type))
9024 return 1;
9026 if (TYPE_CODE_ARRAY == code && type->is_vector ())
9028 /* Vector values should be returned using ARM registers if they
9029 are not over 16 bytes. */
9030 return (type->length () > 16);
9033 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
9034 if (tdep->arm_abi != ARM_ABI_APCS)
9036 /* The AAPCS says all aggregates not larger than a word are returned
9037 in a register. */
9038 if (type->length () <= ARM_INT_REGISTER_SIZE
9039 && language_pass_by_reference (type).trivially_copyable)
9040 return 0;
9042 return 1;
9044 else
9046 int nRc;
9048 /* All aggregate types that won't fit in a register must be returned
9049 in memory. */
9050 if (type->length () > ARM_INT_REGISTER_SIZE
9051 || !language_pass_by_reference (type).trivially_copyable)
9052 return 1;
9054 /* In the ARM ABI, "integer" like aggregate types are returned in
9055 registers. For an aggregate type to be integer like, its size
9056 must be less than or equal to ARM_INT_REGISTER_SIZE and the
9057 offset of each addressable subfield must be zero. Note that bit
9058 fields are not addressable, and all addressable subfields of
9059 unions always start at offset zero.
9061 This function is based on the behaviour of GCC 2.95.1.
9062 See: gcc/arm.c: arm_return_in_memory() for details.
9064 Note: All versions of GCC before GCC 2.95.2 do not set up the
9065 parameters correctly for a function returning the following
9066 structure: struct { float f;}; This should be returned in memory,
9067 not a register. Richard Earnshaw sent me a patch, but I do not
9068 know of any way to detect if a function like the above has been
9069 compiled with the correct calling convention. */
9071 /* Assume all other aggregate types can be returned in a register.
9072 Run a check for structures, unions and arrays. */
9073 nRc = 0;
9075 if ((TYPE_CODE_STRUCT == code) || (TYPE_CODE_UNION == code))
9077 int i;
9078 /* Need to check if this struct/union is "integer" like. For
9079 this to be true, its size must be less than or equal to
9080 ARM_INT_REGISTER_SIZE and the offset of each addressable
9081 subfield must be zero. Note that bit fields are not
9082 addressable, and unions always start at offset zero. If any
9083 of the subfields is a floating point type, the struct/union
9084 cannot be an integer type. */
9086 /* For each field in the object, check:
9087 1) Is it FP? --> yes, nRc = 1;
9088 2) Is it addressable (bitpos != 0) and
9089 not packed (bitsize == 0)?
9090 --> yes, nRc = 1
9093 for (i = 0; i < type->num_fields (); i++)
9095 enum type_code field_type_code;
9097 field_type_code
9098 = check_typedef (type->field (i).type ())->code ();
9100 /* Is it a floating point type field? */
9101 if (field_type_code == TYPE_CODE_FLT)
9103 nRc = 1;
9104 break;
9107 /* If bitpos != 0, then we have to care about it. */
9108 if (type->field (i).loc_bitpos () != 0)
9110 /* Bitfields are not addressable. If the field bitsize is
9111 zero, then the field is not packed. Hence it cannot be
9112 a bitfield or any other packed type. */
9113 if (type->field (i).bitsize () == 0)
9115 nRc = 1;
9116 break;
9122 return nRc;
9126 /* Write into appropriate registers a function return value of type
9127 TYPE, given in virtual format. */
9129 static void
9130 arm_store_return_value (struct type *type, struct regcache *regs,
9131 const gdb_byte *valbuf)
9133 struct gdbarch *gdbarch = regs->arch ();
9134 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
9136 while (type->code () == TYPE_CODE_RANGE)
9137 type = check_typedef (type->target_type ());
9139 if (type->code () == TYPE_CODE_FLT)
9141 gdb_byte buf[ARM_FP_REGISTER_SIZE];
9142 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
9144 switch (tdep->fp_model)
9146 case ARM_FLOAT_FPA:
9148 target_float_convert (valbuf, type, buf, arm_ext_type (gdbarch));
9149 regs->cooked_write (ARM_F0_REGNUM, buf);
9150 break;
9152 case ARM_FLOAT_SOFT_FPA:
9153 case ARM_FLOAT_SOFT_VFP:
9154 /* ARM_FLOAT_VFP can arise if this is a variadic function so
9155 not using the VFP ABI code. */
9156 case ARM_FLOAT_VFP:
9157 regs->cooked_write (ARM_A1_REGNUM, valbuf);
9158 if (type->length () > 4)
9159 regs->cooked_write (ARM_A1_REGNUM + 1,
9160 valbuf + ARM_INT_REGISTER_SIZE);
9161 break;
9163 default:
9164 internal_error (_("arm_store_return_value: Floating "
9165 "point model not supported"));
9166 break;
9169 else if (type->code () == TYPE_CODE_INT
9170 || type->code () == TYPE_CODE_CHAR
9171 || type->code () == TYPE_CODE_BOOL
9172 || type->code () == TYPE_CODE_PTR
9173 || TYPE_IS_REFERENCE (type)
9174 || type->code () == TYPE_CODE_ENUM
9175 || is_fixed_point_type (type))
9177 if (type->length () <= 4)
9179 /* Values of one word or less are zero/sign-extended and
9180 returned in r0. */
9181 bfd_byte tmpbuf[ARM_INT_REGISTER_SIZE];
9183 if (is_fixed_point_type (type))
9185 gdb_mpz unscaled;
9186 unscaled.read (gdb::make_array_view (valbuf, type->length ()),
9187 byte_order, type->is_unsigned ());
9188 unscaled.write (gdb::make_array_view (tmpbuf, sizeof (tmpbuf)),
9189 byte_order, type->is_unsigned ());
9191 else
9193 LONGEST val = unpack_long (type, valbuf);
9194 store_signed_integer (tmpbuf, ARM_INT_REGISTER_SIZE, byte_order, val);
9196 regs->cooked_write (ARM_A1_REGNUM, tmpbuf);
9198 else
9200 /* Integral values greater than one word are stored in consecutive
9201 registers starting with r0. This will always be a multiple of
9202 the regiser size. */
9203 int len = type->length ();
9204 int regno = ARM_A1_REGNUM;
9206 while (len > 0)
9208 regs->cooked_write (regno++, valbuf);
9209 len -= ARM_INT_REGISTER_SIZE;
9210 valbuf += ARM_INT_REGISTER_SIZE;
9214 else
9216 /* For a structure or union the behaviour is as if the value had
9217 been stored to word-aligned memory and then loaded into
9218 registers with 32-bit load instruction(s). */
9219 int len = type->length ();
9220 int regno = ARM_A1_REGNUM;
9221 bfd_byte tmpbuf[ARM_INT_REGISTER_SIZE];
9223 while (len > 0)
9225 memcpy (tmpbuf, valbuf,
9226 len > ARM_INT_REGISTER_SIZE ? ARM_INT_REGISTER_SIZE : len);
9227 regs->cooked_write (regno++, tmpbuf);
9228 len -= ARM_INT_REGISTER_SIZE;
9229 valbuf += ARM_INT_REGISTER_SIZE;
9235 /* Handle function return values. */
9237 static enum return_value_convention
9238 arm_return_value (struct gdbarch *gdbarch, struct value *function,
9239 struct type *valtype, struct regcache *regcache,
9240 struct value **read_value, const gdb_byte *writebuf)
9242 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
9243 struct type *func_type = function ? function->type () : NULL;
9244 enum arm_vfp_cprc_base_type vfp_base_type;
9245 int vfp_base_count;
9247 if (arm_vfp_abi_for_function (gdbarch, func_type)
9248 && arm_vfp_call_candidate (valtype, &vfp_base_type, &vfp_base_count))
9250 int reg_char = arm_vfp_cprc_reg_char (vfp_base_type);
9251 int unit_length = arm_vfp_cprc_unit_length (vfp_base_type);
9252 int i;
9254 gdb_byte *readbuf = nullptr;
9255 if (read_value != nullptr)
9257 *read_value = value::allocate (valtype);
9258 readbuf = (*read_value)->contents_raw ().data ();
9261 for (i = 0; i < vfp_base_count; i++)
9263 if (reg_char == 'q')
9265 if (writebuf)
9266 arm_neon_quad_write (gdbarch, regcache, i,
9267 writebuf + i * unit_length);
9269 if (readbuf)
9270 arm_neon_quad_read (gdbarch, regcache, i,
9271 readbuf + i * unit_length);
9273 else
9275 char name_buf[4];
9276 int regnum;
9278 xsnprintf (name_buf, sizeof (name_buf), "%c%d", reg_char, i);
9279 regnum = user_reg_map_name_to_regnum (gdbarch, name_buf,
9280 strlen (name_buf));
9281 if (writebuf)
9282 regcache->cooked_write (regnum, writebuf + i * unit_length);
9283 if (readbuf)
9284 regcache->cooked_read (regnum, readbuf + i * unit_length);
9287 return RETURN_VALUE_REGISTER_CONVENTION;
9290 if (valtype->code () == TYPE_CODE_STRUCT
9291 || valtype->code () == TYPE_CODE_UNION
9292 || valtype->code () == TYPE_CODE_ARRAY)
9294 /* From the AAPCS document:
9296 Result return:
9298 A Composite Type larger than 4 bytes, or whose size cannot be
9299 determined statically by both caller and callee, is stored in memory
9300 at an address passed as an extra argument when the function was
9301 called (Parameter Passing, rule A.4). The memory to be used for the
9302 result may be modified at any point during the function call.
9304 Parameter Passing:
9306 A.4: If the subroutine is a function that returns a result in memory,
9307 then the address for the result is placed in r0 and the NCRN is set
9308 to r1. */
9309 if (tdep->struct_return == pcc_struct_return
9310 || arm_return_in_memory (gdbarch, valtype))
9312 if (read_value != nullptr)
9314 CORE_ADDR addr;
9316 regcache->cooked_read (ARM_A1_REGNUM, &addr);
9317 *read_value = value_at_non_lval (valtype, addr);
9319 return RETURN_VALUE_ABI_RETURNS_ADDRESS;
9322 else if (valtype->code () == TYPE_CODE_COMPLEX)
9324 if (arm_return_in_memory (gdbarch, valtype))
9325 return RETURN_VALUE_STRUCT_CONVENTION;
9328 if (writebuf)
9329 arm_store_return_value (valtype, regcache, writebuf);
9331 if (read_value != nullptr)
9333 *read_value = value::allocate (valtype);
9334 gdb_byte *readbuf = (*read_value)->contents_raw ().data ();
9335 arm_extract_return_value (valtype, regcache, readbuf);
9338 return RETURN_VALUE_REGISTER_CONVENTION;
9342 static int
9343 arm_get_longjmp_target (const frame_info_ptr &frame, CORE_ADDR *pc)
9345 struct gdbarch *gdbarch = get_frame_arch (frame);
9346 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
9347 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
9348 CORE_ADDR jb_addr;
9349 gdb_byte buf[ARM_INT_REGISTER_SIZE];
9351 jb_addr = get_frame_register_unsigned (frame, ARM_A1_REGNUM);
9353 if (target_read_memory (jb_addr + tdep->jb_pc * tdep->jb_elt_size, buf,
9354 ARM_INT_REGISTER_SIZE))
9355 return 0;
9357 *pc = extract_unsigned_integer (buf, ARM_INT_REGISTER_SIZE, byte_order);
9358 return 1;
9360 /* A call to cmse secure entry function "foo" at "a" is modified by
9361 GNU ld as "b".
9362 a) bl xxxx <foo>
9364 <foo>
9365 xxxx:
9367 b) bl yyyy <__acle_se_foo>
9369 section .gnu.sgstubs:
9370 <foo>
9371 yyyy: sg // secure gateway
9372 b.w xxxx <__acle_se_foo> // original_branch_dest
9374 <__acle_se_foo>
9375 xxxx:
9377 When the control at "b", the pc contains "yyyy" (sg address) which is a
9378 trampoline and does not exist in source code. This function returns the
9379 target pc "xxxx". For more details please refer to section 5.4
9380 (Entry functions) and section 3.4.4 (C level development flow of secure code)
9381 of "armv8-m-security-extensions-requirements-on-development-tools-engineering-specification"
9382 document on www.developer.arm.com. */
9384 static CORE_ADDR
9385 arm_skip_cmse_entry (CORE_ADDR pc, const char *name, struct objfile *objfile)
9387 int target_len = strlen (name) + strlen ("__acle_se_") + 1;
9388 char *target_name = (char *) alloca (target_len);
9389 xsnprintf (target_name, target_len, "%s%s", "__acle_se_", name);
9391 bound_minimal_symbol minsym
9392 = lookup_minimal_symbol (current_program_space, target_name, objfile);
9393 if (minsym.minsym != nullptr)
9394 return minsym.value_address ();
9396 return 0;
9399 /* Return true when SEC points to ".gnu.sgstubs" section. */
9401 static bool
9402 arm_is_sgstubs_section (struct obj_section *sec)
9404 return (sec != nullptr
9405 && sec->the_bfd_section != nullptr
9406 && sec->the_bfd_section->name != nullptr
9407 && streq (sec->the_bfd_section->name, ".gnu.sgstubs"));
9410 /* Recognize GCC and GNU ld's trampolines. If we are in a trampoline,
9411 return the target PC. Otherwise return 0. */
9413 CORE_ADDR
9414 arm_skip_stub (const frame_info_ptr &frame, CORE_ADDR pc)
9416 const char *name;
9417 int namelen;
9418 CORE_ADDR start_addr;
9420 /* Find the starting address and name of the function containing the PC. */
9421 if (find_pc_partial_function (pc, &name, &start_addr, NULL) == 0)
9423 /* Trampoline 'bx reg' doesn't belong to any functions. Do the
9424 check here. */
9425 start_addr = arm_skip_bx_reg (frame, pc);
9426 if (start_addr != 0)
9427 return start_addr;
9429 return 0;
9432 /* If PC is in a Thumb call or return stub, return the address of the
9433 target PC, which is in a register. The thunk functions are called
9434 _call_via_xx, where x is the register name. The possible names
9435 are r0-r9, sl, fp, ip, sp, and lr. ARM RealView has similar
9436 functions, named __ARM_call_via_r[0-7]. */
9437 if (startswith (name, "_call_via_")
9438 || startswith (name, "__ARM_call_via_"))
9440 /* Use the name suffix to determine which register contains the
9441 target PC. */
9442 static const char *table[15] =
9443 {"r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
9444 "r8", "r9", "sl", "fp", "ip", "sp", "lr"
9446 int regno;
9447 int offset = strlen (name) - 2;
9449 for (regno = 0; regno <= 14; regno++)
9450 if (strcmp (&name[offset], table[regno]) == 0)
9451 return get_frame_register_unsigned (frame, regno);
9454 /* GNU ld generates __foo_from_arm or __foo_from_thumb for
9455 non-interworking calls to foo. We could decode the stubs
9456 to find the target but it's easier to use the symbol table. */
9457 namelen = strlen (name);
9458 if (name[0] == '_' && name[1] == '_'
9459 && ((namelen > 2 + strlen ("_from_thumb")
9460 && startswith (name + namelen - strlen ("_from_thumb"), "_from_thumb"))
9461 || (namelen > 2 + strlen ("_from_arm")
9462 && startswith (name + namelen - strlen ("_from_arm"), "_from_arm"))))
9464 char *target_name;
9465 int target_len = namelen - 2;
9466 struct objfile *objfile;
9467 struct obj_section *sec;
9469 if (name[namelen - 1] == 'b')
9470 target_len -= strlen ("_from_thumb");
9471 else
9472 target_len -= strlen ("_from_arm");
9474 target_name = (char *) alloca (target_len + 1);
9475 memcpy (target_name, name + 2, target_len);
9476 target_name[target_len] = '\0';
9478 sec = find_pc_section (pc);
9479 objfile = (sec == NULL) ? NULL : sec->objfile;
9480 bound_minimal_symbol minsym
9481 = lookup_minimal_symbol (current_program_space, target_name, objfile);
9482 if (minsym.minsym != NULL)
9483 return minsym.value_address ();
9484 else
9485 return 0;
9488 struct obj_section *section = find_pc_section (pc);
9490 /* Check whether SECTION points to the ".gnu.sgstubs" section. */
9491 if (arm_is_sgstubs_section (section))
9492 return arm_skip_cmse_entry (pc, name, section->objfile);
9494 return 0; /* not a stub */
9497 static void
9498 arm_update_current_architecture (void)
9500 /* If the current architecture is not ARM, we have nothing to do. */
9501 gdbarch *arch = current_inferior ()->arch ();
9502 if (gdbarch_bfd_arch_info (arch)->arch != bfd_arch_arm)
9503 return;
9505 /* Update the architecture. */
9506 gdbarch_info info;
9507 if (!gdbarch_update_p (current_inferior (), info))
9508 internal_error (_("could not update architecture"));
9511 static void
9512 set_fp_model_sfunc (const char *args, int from_tty,
9513 struct cmd_list_element *c)
9515 int fp_model;
9517 for (fp_model = ARM_FLOAT_AUTO; fp_model != ARM_FLOAT_LAST; fp_model++)
9518 if (strcmp (current_fp_model, fp_model_strings[fp_model]) == 0)
9520 arm_fp_model = (enum arm_float_model) fp_model;
9521 break;
9524 if (fp_model == ARM_FLOAT_LAST)
9525 internal_error (_("Invalid fp model accepted: %s."),
9526 current_fp_model);
9528 arm_update_current_architecture ();
9531 static void
9532 show_fp_model (struct ui_file *file, int from_tty,
9533 struct cmd_list_element *c, const char *value)
9535 gdbarch *arch = current_inferior ()->arch ();
9536 if (arm_fp_model == ARM_FLOAT_AUTO
9537 && gdbarch_bfd_arch_info (arch)->arch == bfd_arch_arm)
9539 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (arch);
9541 gdb_printf (file, _("\
9542 The current ARM floating point model is \"auto\" (currently \"%s\").\n"),
9543 fp_model_strings[tdep->fp_model]);
9545 else
9546 gdb_printf (file, _("\
9547 The current ARM floating point model is \"%s\".\n"),
9548 fp_model_strings[arm_fp_model]);
9551 static void
9552 arm_set_abi (const char *args, int from_tty,
9553 struct cmd_list_element *c)
9555 int arm_abi;
9557 for (arm_abi = ARM_ABI_AUTO; arm_abi != ARM_ABI_LAST; arm_abi++)
9558 if (strcmp (arm_abi_string, arm_abi_strings[arm_abi]) == 0)
9560 arm_abi_global = (enum arm_abi_kind) arm_abi;
9561 break;
9564 if (arm_abi == ARM_ABI_LAST)
9565 internal_error (_("Invalid ABI accepted: %s."),
9566 arm_abi_string);
9568 arm_update_current_architecture ();
9571 static void
9572 arm_show_abi (struct ui_file *file, int from_tty,
9573 struct cmd_list_element *c, const char *value)
9575 gdbarch *arch = current_inferior ()->arch ();
9576 if (arm_abi_global == ARM_ABI_AUTO
9577 && gdbarch_bfd_arch_info (arch)->arch == bfd_arch_arm)
9579 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (arch);
9581 gdb_printf (file, _("\
9582 The current ARM ABI is \"auto\" (currently \"%s\").\n"),
9583 arm_abi_strings[tdep->arm_abi]);
9585 else
9586 gdb_printf (file, _("The current ARM ABI is \"%s\".\n"),
9587 arm_abi_string);
9590 static void
9591 arm_show_fallback_mode (struct ui_file *file, int from_tty,
9592 struct cmd_list_element *c, const char *value)
9594 gdb_printf (file,
9595 _("The current execution mode assumed "
9596 "(when symbols are unavailable) is \"%s\".\n"),
9597 arm_fallback_mode_string);
9600 static void
9601 arm_show_force_mode (struct ui_file *file, int from_tty,
9602 struct cmd_list_element *c, const char *value)
9604 gdb_printf (file,
9605 _("The current execution mode assumed "
9606 "(even when symbols are available) is \"%s\".\n"),
9607 arm_force_mode_string);
9610 static void
9611 arm_show_unwind_secure_frames (struct ui_file *file, int from_tty,
9612 struct cmd_list_element *c, const char *value)
9614 gdb_printf (file,
9615 _("Usage of non-secure to secure exception stack unwinding is %s.\n"),
9616 arm_unwind_secure_frames ? "on" : "off");
9619 /* If the user changes the register disassembly style used for info
9620 register and other commands, we have to also switch the style used
9621 in opcodes for disassembly output. This function is run in the "set
9622 arm disassembly" command, and does that. */
9624 static void
9625 set_disassembly_style_sfunc (const char *args, int from_tty,
9626 struct cmd_list_element *c)
9628 /* Convert the short style name into the long style name (eg, reg-names-*)
9629 before calling the generic set_disassembler_options() function. */
9630 std::string long_name = std::string ("reg-names-") + disassembly_style;
9631 set_disassembler_options (&long_name[0]);
9634 static void
9635 show_disassembly_style_sfunc (struct ui_file *file, int from_tty,
9636 struct cmd_list_element *c, const char *value)
9638 struct gdbarch *gdbarch = get_current_arch ();
9639 const char *options = get_disassembler_options (gdbarch);
9640 const char *style = "";
9641 int len = 0;
9642 const char *opt;
9644 FOR_EACH_DISASSEMBLER_OPTION (opt, options)
9645 if (startswith (opt, "reg-names-"))
9647 style = &opt[strlen ("reg-names-")];
9648 len = strcspn (style, ",");
9651 gdb_printf (file, "The disassembly style is \"%.*s\".\n", len, style);
9654 /* Return the ARM register name corresponding to register I. */
9655 static const char *
9656 arm_register_name (struct gdbarch *gdbarch, int i)
9658 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
9660 if (is_s_pseudo (gdbarch, i))
9662 static const char *const s_pseudo_names[] = {
9663 "s0", "s1", "s2", "s3", "s4", "s5", "s6", "s7",
9664 "s8", "s9", "s10", "s11", "s12", "s13", "s14", "s15",
9665 "s16", "s17", "s18", "s19", "s20", "s21", "s22", "s23",
9666 "s24", "s25", "s26", "s27", "s28", "s29", "s30", "s31",
9669 return s_pseudo_names[i - tdep->s_pseudo_base];
9672 if (is_q_pseudo (gdbarch, i))
9674 static const char *const q_pseudo_names[] = {
9675 "q0", "q1", "q2", "q3", "q4", "q5", "q6", "q7",
9676 "q8", "q9", "q10", "q11", "q12", "q13", "q14", "q15",
9679 return q_pseudo_names[i - tdep->q_pseudo_base];
9682 if (is_mve_pseudo (gdbarch, i))
9683 return "p0";
9685 /* RA_AUTH_CODE is used for unwinding only. Do not assign it a name. */
9686 if (is_pacbti_pseudo (gdbarch, i))
9687 return "";
9689 if (i >= ARRAY_SIZE (arm_register_names))
9690 /* These registers are only supported on targets which supply
9691 an XML description. */
9692 return "";
9694 /* Non-pseudo registers. */
9695 return arm_register_names[i];
9698 /* Test whether the coff symbol specific value corresponds to a Thumb
9699 function. */
9701 static int
9702 coff_sym_is_thumb (int val)
9704 return (val == C_THUMBEXT
9705 || val == C_THUMBSTAT
9706 || val == C_THUMBEXTFUNC
9707 || val == C_THUMBSTATFUNC
9708 || val == C_THUMBLABEL);
9711 /* arm_coff_make_msymbol_special()
9712 arm_elf_make_msymbol_special()
9714 These functions test whether the COFF or ELF symbol corresponds to
9715 an address in thumb code, and set a "special" bit in a minimal
9716 symbol to indicate that it does. */
9718 static void
9719 arm_elf_make_msymbol_special(asymbol *sym, struct minimal_symbol *msym)
9721 elf_symbol_type *elfsym = (elf_symbol_type *) sym;
9723 if (ARM_GET_SYM_BRANCH_TYPE (elfsym->internal_elf_sym.st_target_internal)
9724 == ST_BRANCH_TO_THUMB)
9725 MSYMBOL_SET_SPECIAL (msym);
9728 static void
9729 arm_coff_make_msymbol_special(int val, struct minimal_symbol *msym)
9731 if (coff_sym_is_thumb (val))
9732 MSYMBOL_SET_SPECIAL (msym);
9735 static void
9736 arm_record_special_symbol (struct gdbarch *gdbarch, struct objfile *objfile,
9737 asymbol *sym)
9739 const char *name = bfd_asymbol_name (sym);
9740 struct arm_per_bfd *data;
9741 struct arm_mapping_symbol new_map_sym;
9743 gdb_assert (name[0] == '$');
9744 if (name[1] != 'a' && name[1] != 't' && name[1] != 'd')
9745 return;
9747 data = arm_bfd_data_key.get (objfile->obfd.get ());
9748 if (data == NULL)
9749 data = arm_bfd_data_key.emplace (objfile->obfd.get (),
9750 objfile->obfd->section_count);
9751 arm_mapping_symbol_vec &map
9752 = data->section_maps[bfd_asymbol_section (sym)->index];
9754 new_map_sym.value = sym->value;
9755 new_map_sym.type = name[1];
9757 /* Insert at the end, the vector will be sorted on first use. */
9758 map.push_back (new_map_sym);
9761 static void
9762 arm_write_pc (struct regcache *regcache, CORE_ADDR pc)
9764 struct gdbarch *gdbarch = regcache->arch ();
9765 regcache_cooked_write_unsigned (regcache, ARM_PC_REGNUM, pc);
9767 /* If necessary, set the T bit. */
9768 if (arm_apcs_32)
9770 ULONGEST val, t_bit;
9771 regcache_cooked_read_unsigned (regcache, ARM_PS_REGNUM, &val);
9772 t_bit = arm_psr_thumb_bit (gdbarch);
9773 if (arm_pc_is_thumb (gdbarch, pc))
9774 regcache_cooked_write_unsigned (regcache, ARM_PS_REGNUM,
9775 val | t_bit);
9776 else
9777 regcache_cooked_write_unsigned (regcache, ARM_PS_REGNUM,
9778 val & ~t_bit);
9782 /* Read the contents of a NEON quad register, by reading from two
9783 double registers. This is used to implement the quad pseudo
9784 registers, and for argument passing in case the quad registers are
9785 missing; vectors are passed in quad registers when using the VFP
9786 ABI, even if a NEON unit is not present. REGNUM is the index of
9787 the quad register, in [0, 15]. */
9789 static enum register_status
9790 arm_neon_quad_read (struct gdbarch *gdbarch, readable_regcache *regcache,
9791 int regnum, gdb_byte *buf)
9793 char name_buf[4];
9794 gdb_byte reg_buf[8];
9795 int double_regnum;
9796 enum register_status status;
9798 xsnprintf (name_buf, sizeof (name_buf), "d%d", regnum << 1);
9799 double_regnum = user_reg_map_name_to_regnum (gdbarch, name_buf,
9800 strlen (name_buf));
9802 status = regcache->raw_read (double_regnum, reg_buf);
9803 if (status != REG_VALID)
9804 return status;
9805 memcpy (buf, reg_buf, 8);
9807 status = regcache->raw_read (double_regnum + 1, reg_buf);
9808 if (status != REG_VALID)
9809 return status;
9810 memcpy (buf + 8, reg_buf, 8);
9812 return REG_VALID;
9815 /* Read the contents of a NEON quad register, by reading from two double
9816 registers, and return it as a value. QUAD_REG_INDEX is the index of the quad
9817 register, in [0, 15]. */
9819 static value *
9820 arm_neon_quad_read_value (gdbarch *gdbarch, const frame_info_ptr &next_frame,
9821 int pseudo_reg_num, int quad_reg_index)
9823 std::string raw_reg_name = string_printf ("d%d", quad_reg_index << 1);
9824 int double_regnum
9825 = user_reg_map_name_to_regnum (gdbarch, raw_reg_name.c_str (),
9826 raw_reg_name.length ());
9828 return pseudo_from_concat_raw (next_frame, pseudo_reg_num, double_regnum,
9829 double_regnum + 1);
9832 /* Read the contents of the MVE pseudo register REGNUM and return it as a
9833 value. */
9834 static value *
9835 arm_mve_pseudo_read_value (gdbarch *gdbarch, const frame_info_ptr &next_frame,
9836 int pseudo_reg_num)
9838 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
9840 /* P0 is the first 16 bits of VPR. */
9841 return pseudo_from_raw_part (next_frame, pseudo_reg_num,
9842 tdep->mve_vpr_regnum, 0);
9845 static value *
9846 arm_pseudo_read_value (gdbarch *gdbarch, const frame_info_ptr &next_frame,
9847 const int pseudo_reg_num)
9849 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
9851 gdb_assert (pseudo_reg_num >= gdbarch_num_regs (gdbarch));
9853 if (is_q_pseudo (gdbarch, pseudo_reg_num))
9855 /* Quad-precision register. */
9856 return arm_neon_quad_read_value (gdbarch, next_frame, pseudo_reg_num,
9857 pseudo_reg_num - tdep->q_pseudo_base);
9859 else if (is_mve_pseudo (gdbarch, pseudo_reg_num))
9860 return arm_mve_pseudo_read_value (gdbarch, next_frame, pseudo_reg_num);
9861 else
9863 int s_reg_index = pseudo_reg_num - tdep->s_pseudo_base;
9865 /* Single-precision register. */
9866 gdb_assert (s_reg_index < 32);
9868 /* s0 is always the least significant half of d0. */
9869 int offset;
9870 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
9871 offset = (s_reg_index & 1) ? 0 : 4;
9872 else
9873 offset = (s_reg_index & 1) ? 4 : 0;
9875 std::string raw_reg_name = string_printf ("d%d", s_reg_index >> 1);
9876 int double_regnum
9877 = user_reg_map_name_to_regnum (gdbarch, raw_reg_name.c_str (),
9878 raw_reg_name.length ());
9880 return pseudo_from_raw_part (next_frame, pseudo_reg_num, double_regnum,
9881 offset);
9885 /* Store the contents of BUF to a NEON quad register, by writing to
9886 two double registers. This is used to implement the quad pseudo
9887 registers, and for argument passing in case the quad registers are
9888 missing; vectors are passed in quad registers when using the VFP
9889 ABI, even if a NEON unit is not present. REGNUM is the index
9890 of the quad register, in [0, 15]. */
9892 static void
9893 arm_neon_quad_write (struct gdbarch *gdbarch, struct regcache *regcache,
9894 int regnum, const gdb_byte *buf)
9896 char name_buf[4];
9897 int double_regnum;
9899 xsnprintf (name_buf, sizeof (name_buf), "d%d", regnum << 1);
9900 double_regnum = user_reg_map_name_to_regnum (gdbarch, name_buf,
9901 strlen (name_buf));
9903 regcache->raw_write (double_regnum, buf);
9904 regcache->raw_write (double_regnum + 1, buf + 8);
9907 static void
9908 arm_neon_quad_write (gdbarch *gdbarch, const frame_info_ptr &next_frame,
9909 int quad_reg_index, gdb::array_view<const gdb_byte> buf)
9911 std::string raw_reg_name = string_printf ("d%d", quad_reg_index << 1);
9912 int double_regnum
9913 = user_reg_map_name_to_regnum (gdbarch, raw_reg_name.data (),
9914 raw_reg_name.length ());
9916 pseudo_to_concat_raw (next_frame, buf, double_regnum, double_regnum + 1);
9919 /* Store the contents of BUF to the MVE pseudo register REGNUM. */
9921 static void
9922 arm_mve_pseudo_write (gdbarch *gdbarch, const frame_info_ptr &next_frame,
9923 int pseudo_reg_num, gdb::array_view<const gdb_byte> buf)
9925 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
9927 /* P0 is the first 16 bits of VPR. */
9928 pseudo_to_raw_part(next_frame, buf, tdep->mve_vpr_regnum, 0);
9931 static void
9932 arm_pseudo_write (gdbarch *gdbarch, const frame_info_ptr &next_frame,
9933 const int pseudo_reg_num,
9934 gdb::array_view<const gdb_byte> buf)
9936 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
9938 gdb_assert (pseudo_reg_num >= gdbarch_num_regs (gdbarch));
9940 if (is_q_pseudo (gdbarch, pseudo_reg_num))
9942 /* Quad-precision register. */
9943 arm_neon_quad_write (gdbarch, next_frame,
9944 pseudo_reg_num - tdep->q_pseudo_base, buf);
9946 else if (is_mve_pseudo (gdbarch, pseudo_reg_num))
9947 arm_mve_pseudo_write (gdbarch, next_frame, pseudo_reg_num, buf);
9948 else
9950 int s_reg_index = pseudo_reg_num - tdep->s_pseudo_base;
9952 /* Single-precision register. */
9953 gdb_assert (s_reg_index < 32);
9955 /* s0 is always the least significant half of d0. */
9956 int offset;
9957 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
9958 offset = (s_reg_index & 1) ? 0 : 4;
9959 else
9960 offset = (s_reg_index & 1) ? 4 : 0;
9962 std::string raw_reg_name = string_printf ("d%d", s_reg_index >> 1);
9963 int double_regnum
9964 = user_reg_map_name_to_regnum (gdbarch, raw_reg_name.c_str (),
9965 raw_reg_name.length ());
9967 pseudo_to_raw_part (next_frame, buf, double_regnum, offset);
9971 static struct value *
9972 value_of_arm_user_reg (const frame_info_ptr &frame, const void *baton)
9974 const int *reg_p = (const int *) baton;
9975 return value_of_register (*reg_p, get_next_frame_sentinel_okay (frame));
9978 static enum gdb_osabi
9979 arm_elf_osabi_sniffer (bfd *abfd)
9981 unsigned int elfosabi;
9982 enum gdb_osabi osabi = GDB_OSABI_UNKNOWN;
9984 elfosabi = elf_elfheader (abfd)->e_ident[EI_OSABI];
9986 if (elfosabi == ELFOSABI_ARM)
9987 /* GNU tools use this value. Check note sections in this case,
9988 as well. */
9990 for (asection *sect : gdb_bfd_sections (abfd))
9991 generic_elf_osabi_sniff_abi_tag_sections (abfd, sect, &osabi);
9994 /* Anything else will be handled by the generic ELF sniffer. */
9995 return osabi;
9998 static int
9999 arm_register_reggroup_p (struct gdbarch *gdbarch, int regnum,
10000 const struct reggroup *group)
10002 /* FPS register's type is INT, but belongs to float_reggroup. Beside
10003 this, FPS register belongs to save_regroup, restore_reggroup, and
10004 all_reggroup, of course. */
10005 if (regnum == ARM_FPS_REGNUM)
10006 return (group == float_reggroup
10007 || group == save_reggroup
10008 || group == restore_reggroup
10009 || group == all_reggroup);
10010 else
10011 return default_register_reggroup_p (gdbarch, regnum, group);
10014 /* For backward-compatibility we allow two 'g' packet lengths with
10015 the remote protocol depending on whether FPA registers are
10016 supplied. M-profile targets do not have FPA registers, but some
10017 stubs already exist in the wild which use a 'g' packet which
10018 supplies them albeit with dummy values. The packet format which
10019 includes FPA registers should be considered deprecated for
10020 M-profile targets. */
10022 static void
10023 arm_register_g_packet_guesses (struct gdbarch *gdbarch)
10025 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
10027 if (tdep->is_m)
10029 const target_desc *tdesc;
10031 /* If we know from the executable this is an M-profile target,
10032 cater for remote targets whose register set layout is the
10033 same as the FPA layout. */
10034 tdesc = arm_read_mprofile_description (ARM_M_TYPE_WITH_FPA);
10035 register_remote_g_packet_guess (gdbarch,
10036 ARM_CORE_REGS_SIZE + ARM_FP_REGS_SIZE,
10037 tdesc);
10039 /* The regular M-profile layout. */
10040 tdesc = arm_read_mprofile_description (ARM_M_TYPE_M_PROFILE);
10041 register_remote_g_packet_guess (gdbarch, ARM_CORE_REGS_SIZE,
10042 tdesc);
10044 /* M-profile plus M4F VFP. */
10045 tdesc = arm_read_mprofile_description (ARM_M_TYPE_VFP_D16);
10046 register_remote_g_packet_guess (gdbarch,
10047 ARM_CORE_REGS_SIZE + ARM_VFP2_REGS_SIZE,
10048 tdesc);
10049 /* M-profile plus MVE. */
10050 tdesc = arm_read_mprofile_description (ARM_M_TYPE_MVE);
10051 register_remote_g_packet_guess (gdbarch, ARM_CORE_REGS_SIZE
10052 + ARM_VFP2_REGS_SIZE
10053 + ARM_INT_REGISTER_SIZE, tdesc);
10055 /* M-profile system (stack pointers). */
10056 tdesc = arm_read_mprofile_description (ARM_M_TYPE_SYSTEM);
10057 register_remote_g_packet_guess (gdbarch, 2 * ARM_INT_REGISTER_SIZE, tdesc);
10060 /* Otherwise we don't have a useful guess. */
10063 /* Implement the code_of_frame_writable gdbarch method. */
10065 static int
10066 arm_code_of_frame_writable (struct gdbarch *gdbarch, const frame_info_ptr &frame)
10068 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
10070 if (tdep->is_m && get_frame_type (frame) == SIGTRAMP_FRAME)
10072 /* M-profile exception frames return to some magic PCs, where
10073 isn't writable at all. */
10074 return 0;
10076 else
10077 return 1;
10080 /* Implement gdbarch_gnu_triplet_regexp. If the arch name is arm then allow it
10081 to be postfixed by a version (eg armv7hl). */
10083 static const char *
10084 arm_gnu_triplet_regexp (struct gdbarch *gdbarch)
10086 if (strcmp (gdbarch_bfd_arch_info (gdbarch)->arch_name, "arm") == 0)
10087 return "arm(v[^- ]*)?";
10088 return gdbarch_bfd_arch_info (gdbarch)->arch_name;
10091 /* Implement the "get_pc_address_flags" gdbarch method. */
10093 static std::string
10094 arm_get_pc_address_flags (const frame_info_ptr &frame, CORE_ADDR pc)
10096 if (get_frame_pc_masked (frame))
10097 return "PAC";
10099 return "";
10102 /* Initialize the current architecture based on INFO. If possible,
10103 re-use an architecture from ARCHES, which is a list of
10104 architectures already created during this debugging session.
10106 Called e.g. at program startup, when reading a core file, and when
10107 reading a binary file. */
10109 static struct gdbarch *
10110 arm_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
10112 struct gdbarch_list *best_arch;
10113 enum arm_abi_kind arm_abi = arm_abi_global;
10114 enum arm_float_model fp_model = arm_fp_model;
10115 tdesc_arch_data_up tdesc_data;
10116 int i;
10117 bool is_m = false;
10118 bool have_sec_ext = false;
10119 int vfp_register_count = 0;
10120 bool have_s_pseudos = false, have_q_pseudos = false;
10121 bool have_wmmx_registers = false;
10122 bool have_neon = false;
10123 bool have_fpa_registers = true;
10124 const struct target_desc *tdesc = info.target_desc;
10125 bool have_vfp = false;
10126 bool have_mve = false;
10127 bool have_pacbti = false;
10128 int mve_vpr_regnum = -1;
10129 int register_count = ARM_NUM_REGS;
10130 bool have_m_profile_msp = false;
10131 int m_profile_msp_regnum = -1;
10132 int m_profile_psp_regnum = -1;
10133 int m_profile_msp_ns_regnum = -1;
10134 int m_profile_psp_ns_regnum = -1;
10135 int m_profile_msp_s_regnum = -1;
10136 int m_profile_psp_s_regnum = -1;
10137 int tls_regnum = 0;
10139 /* If we have an object to base this architecture on, try to determine
10140 its ABI. */
10142 if (arm_abi == ARM_ABI_AUTO && info.abfd != NULL)
10144 int ei_osabi, e_flags;
10146 switch (bfd_get_flavour (info.abfd))
10148 case bfd_target_coff_flavour:
10149 /* Assume it's an old APCS-style ABI. */
10150 /* XXX WinCE? */
10151 arm_abi = ARM_ABI_APCS;
10152 break;
10154 case bfd_target_elf_flavour:
10155 ei_osabi = elf_elfheader (info.abfd)->e_ident[EI_OSABI];
10156 e_flags = elf_elfheader (info.abfd)->e_flags;
10158 if (ei_osabi == ELFOSABI_ARM)
10160 /* GNU tools used to use this value, but do not for EABI
10161 objects. There's nowhere to tag an EABI version
10162 anyway, so assume APCS. */
10163 arm_abi = ARM_ABI_APCS;
10165 else if (ei_osabi == ELFOSABI_NONE || ei_osabi == ELFOSABI_GNU)
10167 int eabi_ver = EF_ARM_EABI_VERSION (e_flags);
10169 switch (eabi_ver)
10171 case EF_ARM_EABI_UNKNOWN:
10172 /* Assume GNU tools. */
10173 arm_abi = ARM_ABI_APCS;
10174 break;
10176 case EF_ARM_EABI_VER4:
10177 case EF_ARM_EABI_VER5:
10178 arm_abi = ARM_ABI_AAPCS;
10179 /* EABI binaries default to VFP float ordering.
10180 They may also contain build attributes that can
10181 be used to identify if the VFP argument-passing
10182 ABI is in use. */
10183 if (fp_model == ARM_FLOAT_AUTO)
10185 #ifdef HAVE_ELF
10186 switch (bfd_elf_get_obj_attr_int (info.abfd,
10187 OBJ_ATTR_PROC,
10188 Tag_ABI_VFP_args))
10190 case AEABI_VFP_args_base:
10191 /* "The user intended FP parameter/result
10192 passing to conform to AAPCS, base
10193 variant". */
10194 fp_model = ARM_FLOAT_SOFT_VFP;
10195 break;
10196 case AEABI_VFP_args_vfp:
10197 /* "The user intended FP parameter/result
10198 passing to conform to AAPCS, VFP
10199 variant". */
10200 fp_model = ARM_FLOAT_VFP;
10201 break;
10202 case AEABI_VFP_args_toolchain:
10203 /* "The user intended FP parameter/result
10204 passing to conform to tool chain-specific
10205 conventions" - we don't know any such
10206 conventions, so leave it as "auto". */
10207 break;
10208 case AEABI_VFP_args_compatible:
10209 /* "Code is compatible with both the base
10210 and VFP variants; the user did not permit
10211 non-variadic functions to pass FP
10212 parameters/results" - leave it as
10213 "auto". */
10214 break;
10215 default:
10216 /* Attribute value not mentioned in the
10217 November 2012 ABI, so leave it as
10218 "auto". */
10219 break;
10221 #else
10222 fp_model = ARM_FLOAT_SOFT_VFP;
10223 #endif
10225 break;
10227 default:
10228 /* Leave it as "auto". */
10229 warning (_("unknown ARM EABI version 0x%x"), eabi_ver);
10230 break;
10233 #ifdef HAVE_ELF
10234 /* Detect M-profile programs. This only works if the
10235 executable file includes build attributes; GCC does
10236 copy them to the executable, but e.g. RealView does
10237 not. */
10238 int attr_arch
10239 = bfd_elf_get_obj_attr_int (info.abfd, OBJ_ATTR_PROC,
10240 Tag_CPU_arch);
10241 int attr_profile
10242 = bfd_elf_get_obj_attr_int (info.abfd, OBJ_ATTR_PROC,
10243 Tag_CPU_arch_profile);
10245 /* GCC specifies the profile for v6-M; RealView only
10246 specifies the profile for architectures starting with
10247 V7 (as opposed to architectures with a tag
10248 numerically greater than TAG_CPU_ARCH_V7). */
10249 if (!tdesc_has_registers (tdesc)
10250 && (attr_arch == TAG_CPU_ARCH_V6_M
10251 || attr_arch == TAG_CPU_ARCH_V6S_M
10252 || attr_arch == TAG_CPU_ARCH_V7E_M
10253 || attr_arch == TAG_CPU_ARCH_V8M_BASE
10254 || attr_arch == TAG_CPU_ARCH_V8M_MAIN
10255 || attr_arch == TAG_CPU_ARCH_V8_1M_MAIN
10256 || attr_profile == 'M'))
10257 is_m = true;
10259 /* Look for attributes that indicate support for ARMv8.1-m
10260 PACBTI. */
10261 if (!tdesc_has_registers (tdesc) && is_m)
10263 int attr_pac_extension
10264 = bfd_elf_get_obj_attr_int (info.abfd, OBJ_ATTR_PROC,
10265 Tag_PAC_extension);
10267 int attr_bti_extension
10268 = bfd_elf_get_obj_attr_int (info.abfd, OBJ_ATTR_PROC,
10269 Tag_BTI_extension);
10271 int attr_pacret_use
10272 = bfd_elf_get_obj_attr_int (info.abfd, OBJ_ATTR_PROC,
10273 Tag_PACRET_use);
10275 int attr_bti_use
10276 = bfd_elf_get_obj_attr_int (info.abfd, OBJ_ATTR_PROC,
10277 Tag_BTI_use);
10279 if (attr_pac_extension != 0 || attr_bti_extension != 0
10280 || attr_pacret_use != 0 || attr_bti_use != 0)
10281 have_pacbti = true;
10283 #endif
10286 if (fp_model == ARM_FLOAT_AUTO)
10288 switch (e_flags & (EF_ARM_SOFT_FLOAT | EF_ARM_VFP_FLOAT))
10290 case 0:
10291 /* Leave it as "auto". Strictly speaking this case
10292 means FPA, but almost nobody uses that now, and
10293 many toolchains fail to set the appropriate bits
10294 for the floating-point model they use. */
10295 break;
10296 case EF_ARM_SOFT_FLOAT:
10297 fp_model = ARM_FLOAT_SOFT_FPA;
10298 break;
10299 case EF_ARM_VFP_FLOAT:
10300 fp_model = ARM_FLOAT_VFP;
10301 break;
10302 case EF_ARM_SOFT_FLOAT | EF_ARM_VFP_FLOAT:
10303 fp_model = ARM_FLOAT_SOFT_VFP;
10304 break;
10308 if (e_flags & EF_ARM_BE8)
10309 info.byte_order_for_code = BFD_ENDIAN_LITTLE;
10311 break;
10313 default:
10314 /* Leave it as "auto". */
10315 break;
10319 /* Check any target description for validity. */
10320 if (tdesc_has_registers (tdesc))
10322 /* For most registers we require GDB's default names; but also allow
10323 the numeric names for sp / lr / pc, as a convenience. */
10324 static const char *const arm_sp_names[] = { "r13", "sp", NULL };
10325 static const char *const arm_lr_names[] = { "r14", "lr", NULL };
10326 static const char *const arm_pc_names[] = { "r15", "pc", NULL };
10328 const struct tdesc_feature *feature;
10329 int valid_p;
10331 feature = tdesc_find_feature (tdesc,
10332 "org.gnu.gdb.arm.core");
10333 if (feature == NULL)
10335 feature = tdesc_find_feature (tdesc,
10336 "org.gnu.gdb.arm.m-profile");
10337 if (feature == NULL)
10338 return NULL;
10339 else
10340 is_m = true;
10343 tdesc_data = tdesc_data_alloc ();
10345 valid_p = 1;
10346 for (i = 0; i < ARM_SP_REGNUM; i++)
10347 valid_p &= tdesc_numbered_register (feature, tdesc_data.get (), i,
10348 arm_register_names[i]);
10349 valid_p &= tdesc_numbered_register_choices (feature, tdesc_data.get (),
10350 ARM_SP_REGNUM,
10351 arm_sp_names);
10352 valid_p &= tdesc_numbered_register_choices (feature, tdesc_data.get (),
10353 ARM_LR_REGNUM,
10354 arm_lr_names);
10355 valid_p &= tdesc_numbered_register_choices (feature, tdesc_data.get (),
10356 ARM_PC_REGNUM,
10357 arm_pc_names);
10358 if (is_m)
10359 valid_p &= tdesc_numbered_register (feature, tdesc_data.get (),
10360 ARM_PS_REGNUM, "xpsr");
10361 else
10362 valid_p &= tdesc_numbered_register (feature, tdesc_data.get (),
10363 ARM_PS_REGNUM, "cpsr");
10365 if (!valid_p)
10366 return NULL;
10368 if (is_m)
10370 feature = tdesc_find_feature (tdesc,
10371 "org.gnu.gdb.arm.m-system");
10372 if (feature != nullptr)
10374 /* MSP */
10375 valid_p &= tdesc_numbered_register (feature, tdesc_data.get (),
10376 register_count, "msp");
10377 if (!valid_p)
10379 warning (_("M-profile m-system feature is missing required register msp."));
10380 return nullptr;
10382 have_m_profile_msp = true;
10383 m_profile_msp_regnum = register_count++;
10385 /* PSP */
10386 valid_p &= tdesc_numbered_register (feature, tdesc_data.get (),
10387 register_count, "psp");
10388 if (!valid_p)
10390 warning (_("M-profile m-system feature is missing required register psp."));
10391 return nullptr;
10393 m_profile_psp_regnum = register_count++;
10397 feature = tdesc_find_feature (tdesc,
10398 "org.gnu.gdb.arm.fpa");
10399 if (feature != NULL)
10401 valid_p = 1;
10402 for (i = ARM_F0_REGNUM; i <= ARM_FPS_REGNUM; i++)
10403 valid_p &= tdesc_numbered_register (feature, tdesc_data.get (), i,
10404 arm_register_names[i]);
10405 if (!valid_p)
10406 return NULL;
10408 else
10409 have_fpa_registers = false;
10411 feature = tdesc_find_feature (tdesc,
10412 "org.gnu.gdb.xscale.iwmmxt");
10413 if (feature != NULL)
10415 static const char *const iwmmxt_names[] = {
10416 "wR0", "wR1", "wR2", "wR3", "wR4", "wR5", "wR6", "wR7",
10417 "wR8", "wR9", "wR10", "wR11", "wR12", "wR13", "wR14", "wR15",
10418 "wCID", "wCon", "wCSSF", "wCASF", "", "", "", "",
10419 "wCGR0", "wCGR1", "wCGR2", "wCGR3", "", "", "", "",
10422 valid_p = 1;
10423 for (i = ARM_WR0_REGNUM; i <= ARM_WR15_REGNUM; i++)
10424 valid_p
10425 &= tdesc_numbered_register (feature, tdesc_data.get (), i,
10426 iwmmxt_names[i - ARM_WR0_REGNUM]);
10428 /* Check for the control registers, but do not fail if they
10429 are missing. */
10430 for (i = ARM_WC0_REGNUM; i <= ARM_WCASF_REGNUM; i++)
10431 tdesc_numbered_register (feature, tdesc_data.get (), i,
10432 iwmmxt_names[i - ARM_WR0_REGNUM]);
10434 for (i = ARM_WCGR0_REGNUM; i <= ARM_WCGR3_REGNUM; i++)
10435 valid_p
10436 &= tdesc_numbered_register (feature, tdesc_data.get (), i,
10437 iwmmxt_names[i - ARM_WR0_REGNUM]);
10439 if (!valid_p)
10440 return NULL;
10442 have_wmmx_registers = true;
10445 /* If we have a VFP unit, check whether the single precision registers
10446 are present. If not, then we will synthesize them as pseudo
10447 registers. */
10448 feature = tdesc_find_feature (tdesc,
10449 "org.gnu.gdb.arm.vfp");
10450 if (feature != NULL)
10452 static const char *const vfp_double_names[] = {
10453 "d0", "d1", "d2", "d3", "d4", "d5", "d6", "d7",
10454 "d8", "d9", "d10", "d11", "d12", "d13", "d14", "d15",
10455 "d16", "d17", "d18", "d19", "d20", "d21", "d22", "d23",
10456 "d24", "d25", "d26", "d27", "d28", "d29", "d30", "d31",
10459 /* Require the double precision registers. There must be either
10460 16 or 32. */
10461 valid_p = 1;
10462 for (i = 0; i < 32; i++)
10464 valid_p &= tdesc_numbered_register (feature, tdesc_data.get (),
10465 ARM_D0_REGNUM + i,
10466 vfp_double_names[i]);
10467 if (!valid_p)
10468 break;
10470 if (!valid_p && i == 16)
10471 valid_p = 1;
10473 /* Also require FPSCR. */
10474 valid_p &= tdesc_numbered_register (feature, tdesc_data.get (),
10475 ARM_FPSCR_REGNUM, "fpscr");
10476 if (!valid_p)
10477 return NULL;
10479 have_vfp = true;
10481 if (tdesc_unnumbered_register (feature, "s0") == 0)
10482 have_s_pseudos = true;
10484 vfp_register_count = i;
10486 /* If we have VFP, also check for NEON. The architecture allows
10487 NEON without VFP (integer vector operations only), but GDB
10488 does not support that. */
10489 feature = tdesc_find_feature (tdesc,
10490 "org.gnu.gdb.arm.neon");
10491 if (feature != NULL)
10493 /* NEON requires 32 double-precision registers. */
10494 if (i != 32)
10495 return NULL;
10497 /* If there are quad registers defined by the stub, use
10498 their type; otherwise (normally) provide them with
10499 the default type. */
10500 if (tdesc_unnumbered_register (feature, "q0") == 0)
10501 have_q_pseudos = true;
10505 /* Check for the TLS register feature. */
10506 feature = tdesc_find_feature (tdesc, "org.gnu.gdb.arm.tls");
10507 if (feature != nullptr)
10509 valid_p &= tdesc_numbered_register (feature, tdesc_data.get (),
10510 register_count, "tpidruro");
10511 if (!valid_p)
10512 return nullptr;
10514 tls_regnum = register_count;
10515 register_count++;
10518 /* Check for MVE after all the checks for GPR's, VFP and Neon.
10519 MVE (Helium) is an M-profile extension. */
10520 if (is_m)
10522 /* Do we have the MVE feature? */
10523 feature = tdesc_find_feature (tdesc,"org.gnu.gdb.arm.m-profile-mve");
10525 if (feature != nullptr)
10527 /* If we have MVE, we must always have the VPR register. */
10528 valid_p &= tdesc_numbered_register (feature, tdesc_data.get (),
10529 register_count, "vpr");
10530 if (!valid_p)
10532 warning (_("MVE feature is missing required register vpr."));
10533 return nullptr;
10536 have_mve = true;
10537 mve_vpr_regnum = register_count;
10538 register_count++;
10540 /* We can't have Q pseudo registers available here, as that
10541 would mean we have NEON features, and that is only available
10542 on A and R profiles. */
10543 gdb_assert (!have_q_pseudos);
10545 /* Given we have a M-profile target description, if MVE is
10546 enabled and there are VFP registers, we should have Q
10547 pseudo registers (Q0 ~ Q7). */
10548 if (have_vfp)
10549 have_q_pseudos = true;
10552 /* Do we have the ARMv8.1-m PACBTI feature? */
10553 feature = tdesc_find_feature (tdesc,
10554 "org.gnu.gdb.arm.m-profile-pacbti");
10555 if (feature != nullptr)
10557 /* By advertising this feature, the target acknowledges the
10558 presence of the ARMv8.1-m PACBTI extensions.
10560 We don't care for any particular registers in this group, so
10561 the target is free to include whatever it deems appropriate.
10563 The expectation is for this feature to include the PAC
10564 keys. */
10565 have_pacbti = true;
10568 /* Do we have the Security extension? */
10569 feature = tdesc_find_feature (tdesc,
10570 "org.gnu.gdb.arm.secext");
10571 if (feature != nullptr)
10573 /* Secure/Non-secure stack pointers. */
10574 /* MSP_NS */
10575 valid_p &= tdesc_numbered_register (feature, tdesc_data.get (),
10576 register_count, "msp_ns");
10577 if (!valid_p)
10579 warning (_("M-profile secext feature is missing required register msp_ns."));
10580 return nullptr;
10582 m_profile_msp_ns_regnum = register_count++;
10584 /* PSP_NS */
10585 valid_p &= tdesc_numbered_register (feature, tdesc_data.get (),
10586 register_count, "psp_ns");
10587 if (!valid_p)
10589 warning (_("M-profile secext feature is missing required register psp_ns."));
10590 return nullptr;
10592 m_profile_psp_ns_regnum = register_count++;
10594 /* MSP_S */
10595 valid_p &= tdesc_numbered_register (feature, tdesc_data.get (),
10596 register_count, "msp_s");
10597 if (!valid_p)
10599 warning (_("M-profile secext feature is missing required register msp_s."));
10600 return nullptr;
10602 m_profile_msp_s_regnum = register_count++;
10604 /* PSP_S */
10605 valid_p &= tdesc_numbered_register (feature, tdesc_data.get (),
10606 register_count, "psp_s");
10607 if (!valid_p)
10609 warning (_("M-profile secext feature is missing required register psp_s."));
10610 return nullptr;
10612 m_profile_psp_s_regnum = register_count++;
10614 have_sec_ext = true;
10620 /* If there is already a candidate, use it. */
10621 for (best_arch = gdbarch_list_lookup_by_info (arches, &info);
10622 best_arch != NULL;
10623 best_arch = gdbarch_list_lookup_by_info (best_arch->next, &info))
10625 arm_gdbarch_tdep *tdep
10626 = gdbarch_tdep<arm_gdbarch_tdep> (best_arch->gdbarch);
10628 if (arm_abi != ARM_ABI_AUTO && arm_abi != tdep->arm_abi)
10629 continue;
10631 if (fp_model != ARM_FLOAT_AUTO && fp_model != tdep->fp_model)
10632 continue;
10634 /* There are various other properties in tdep that we do not
10635 need to check here: those derived from a target description,
10636 since gdbarches with a different target description are
10637 automatically disqualified. */
10639 /* Do check is_m, though, since it might come from the binary. */
10640 if (is_m != tdep->is_m)
10641 continue;
10643 /* Also check for ARMv8.1-m PACBTI support, since it might come from
10644 the binary. */
10645 if (have_pacbti != tdep->have_pacbti)
10646 continue;
10648 /* Found a match. */
10649 break;
10652 if (best_arch != NULL)
10653 return best_arch->gdbarch;
10655 gdbarch *gdbarch
10656 = gdbarch_alloc (&info, gdbarch_tdep_up (new arm_gdbarch_tdep));
10657 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
10659 /* Record additional information about the architecture we are defining.
10660 These are gdbarch discriminators, like the OSABI. */
10661 tdep->arm_abi = arm_abi;
10662 tdep->fp_model = fp_model;
10663 tdep->is_m = is_m;
10664 tdep->have_sec_ext = have_sec_ext;
10665 tdep->have_fpa_registers = have_fpa_registers;
10666 tdep->have_wmmx_registers = have_wmmx_registers;
10667 gdb_assert (vfp_register_count == 0
10668 || vfp_register_count == 16
10669 || vfp_register_count == 32);
10670 tdep->vfp_register_count = vfp_register_count;
10671 tdep->have_s_pseudos = have_s_pseudos;
10672 tdep->have_q_pseudos = have_q_pseudos;
10673 tdep->have_neon = have_neon;
10674 tdep->tls_regnum = tls_regnum;
10676 /* Adjust the MVE feature settings. */
10677 if (have_mve)
10679 tdep->have_mve = true;
10680 tdep->mve_vpr_regnum = mve_vpr_regnum;
10683 /* Adjust the PACBTI feature settings. */
10684 tdep->have_pacbti = have_pacbti;
10686 /* Adjust the M-profile stack pointers settings. */
10687 if (have_m_profile_msp)
10689 tdep->m_profile_msp_regnum = m_profile_msp_regnum;
10690 tdep->m_profile_psp_regnum = m_profile_psp_regnum;
10691 tdep->m_profile_msp_ns_regnum = m_profile_msp_ns_regnum;
10692 tdep->m_profile_psp_ns_regnum = m_profile_psp_ns_regnum;
10693 tdep->m_profile_msp_s_regnum = m_profile_msp_s_regnum;
10694 tdep->m_profile_psp_s_regnum = m_profile_psp_s_regnum;
10697 arm_register_g_packet_guesses (gdbarch);
10699 /* Breakpoints. */
10700 switch (info.byte_order_for_code)
10702 case BFD_ENDIAN_BIG:
10703 tdep->arm_breakpoint = arm_default_arm_be_breakpoint;
10704 tdep->arm_breakpoint_size = sizeof (arm_default_arm_be_breakpoint);
10705 tdep->thumb_breakpoint = arm_default_thumb_be_breakpoint;
10706 tdep->thumb_breakpoint_size = sizeof (arm_default_thumb_be_breakpoint);
10708 break;
10710 case BFD_ENDIAN_LITTLE:
10711 tdep->arm_breakpoint = arm_default_arm_le_breakpoint;
10712 tdep->arm_breakpoint_size = sizeof (arm_default_arm_le_breakpoint);
10713 tdep->thumb_breakpoint = arm_default_thumb_le_breakpoint;
10714 tdep->thumb_breakpoint_size = sizeof (arm_default_thumb_le_breakpoint);
10716 break;
10718 default:
10719 internal_error (_("arm_gdbarch_init: bad byte order for float format"));
10722 /* On ARM targets char defaults to unsigned. */
10723 set_gdbarch_char_signed (gdbarch, 0);
10725 /* wchar_t is unsigned under the AAPCS. */
10726 if (tdep->arm_abi == ARM_ABI_AAPCS)
10727 set_gdbarch_wchar_signed (gdbarch, 0);
10728 else
10729 set_gdbarch_wchar_signed (gdbarch, 1);
10731 /* Compute type alignment. */
10732 set_gdbarch_type_align (gdbarch, arm_type_align);
10734 /* Note: for displaced stepping, this includes the breakpoint, and one word
10735 of additional scratch space. This setting isn't used for anything beside
10736 displaced stepping at present. */
10737 set_gdbarch_displaced_step_buffer_length
10738 (gdbarch, 4 * ARM_DISPLACED_MODIFIED_INSNS);
10739 set_gdbarch_max_insn_length (gdbarch, 4);
10741 /* This should be low enough for everything. */
10742 tdep->lowest_pc = 0x20;
10743 tdep->jb_pc = -1; /* Longjump support not enabled by default. */
10745 /* The default, for both APCS and AAPCS, is to return small
10746 structures in registers. */
10747 tdep->struct_return = reg_struct_return;
10749 set_gdbarch_push_dummy_call (gdbarch, arm_push_dummy_call);
10750 set_gdbarch_frame_align (gdbarch, arm_frame_align);
10752 if (is_m)
10753 set_gdbarch_code_of_frame_writable (gdbarch, arm_code_of_frame_writable);
10755 set_gdbarch_write_pc (gdbarch, arm_write_pc);
10757 frame_base_set_default (gdbarch, &arm_normal_base);
10759 /* Address manipulation. */
10760 set_gdbarch_addr_bits_remove (gdbarch, arm_addr_bits_remove);
10762 /* Advance PC across function entry code. */
10763 set_gdbarch_skip_prologue (gdbarch, arm_skip_prologue);
10765 /* Detect whether PC is at a point where the stack has been destroyed. */
10766 set_gdbarch_stack_frame_destroyed_p (gdbarch, arm_stack_frame_destroyed_p);
10768 /* Skip trampolines. */
10769 set_gdbarch_skip_trampoline_code (gdbarch, arm_skip_stub);
10771 /* The stack grows downward. */
10772 set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
10774 /* Breakpoint manipulation. */
10775 set_gdbarch_breakpoint_kind_from_pc (gdbarch, arm_breakpoint_kind_from_pc);
10776 set_gdbarch_sw_breakpoint_from_kind (gdbarch, arm_sw_breakpoint_from_kind);
10777 set_gdbarch_breakpoint_kind_from_current_state (gdbarch,
10778 arm_breakpoint_kind_from_current_state);
10780 /* Information about registers, etc. */
10781 set_gdbarch_sp_regnum (gdbarch, ARM_SP_REGNUM);
10782 set_gdbarch_pc_regnum (gdbarch, ARM_PC_REGNUM);
10783 set_gdbarch_num_regs (gdbarch, register_count);
10784 set_gdbarch_register_type (gdbarch, arm_register_type);
10785 set_gdbarch_register_reggroup_p (gdbarch, arm_register_reggroup_p);
10787 /* This "info float" is FPA-specific. Use the generic version if we
10788 do not have FPA. */
10789 if (tdep->have_fpa_registers)
10790 set_gdbarch_print_float_info (gdbarch, arm_print_float_info);
10792 /* Internal <-> external register number maps. */
10793 set_gdbarch_dwarf2_reg_to_regnum (gdbarch, arm_dwarf_reg_to_regnum);
10794 set_gdbarch_register_sim_regno (gdbarch, arm_register_sim_regno);
10796 set_gdbarch_register_name (gdbarch, arm_register_name);
10798 /* Returning results. */
10799 set_gdbarch_return_value_as_value (gdbarch, arm_return_value);
10801 /* Disassembly. */
10802 set_gdbarch_print_insn (gdbarch, gdb_print_insn_arm);
10804 /* Minsymbol frobbing. */
10805 set_gdbarch_elf_make_msymbol_special (gdbarch, arm_elf_make_msymbol_special);
10806 set_gdbarch_coff_make_msymbol_special (gdbarch,
10807 arm_coff_make_msymbol_special);
10808 set_gdbarch_record_special_symbol (gdbarch, arm_record_special_symbol);
10810 /* Thumb-2 IT block support. */
10811 set_gdbarch_adjust_breakpoint_address (gdbarch,
10812 arm_adjust_breakpoint_address);
10814 /* Virtual tables. */
10815 set_gdbarch_vbit_in_delta (gdbarch, 1);
10817 /* Hook in the ABI-specific overrides, if they have been registered. */
10818 gdbarch_init_osabi (info, gdbarch);
10820 dwarf2_frame_set_init_reg (gdbarch, arm_dwarf2_frame_init_reg);
10822 /* Add some default predicates. */
10823 if (is_m)
10824 frame_unwind_append_unwinder (gdbarch, &arm_m_exception_unwind);
10825 frame_unwind_append_unwinder (gdbarch, &arm_stub_unwind);
10826 dwarf2_append_unwinders (gdbarch);
10827 frame_unwind_append_unwinder (gdbarch, &arm_exidx_unwind);
10828 frame_unwind_append_unwinder (gdbarch, &arm_epilogue_frame_unwind);
10829 frame_unwind_append_unwinder (gdbarch, &arm_prologue_unwind);
10831 /* Now we have tuned the configuration, set a few final things,
10832 based on what the OS ABI has told us. */
10834 /* If the ABI is not otherwise marked, assume the old GNU APCS. EABI
10835 binaries are always marked. */
10836 if (tdep->arm_abi == ARM_ABI_AUTO)
10837 tdep->arm_abi = ARM_ABI_APCS;
10839 /* Watchpoints are not steppable. */
10840 set_gdbarch_have_nonsteppable_watchpoint (gdbarch, 1);
10842 /* We used to default to FPA for generic ARM, but almost nobody
10843 uses that now, and we now provide a way for the user to force
10844 the model. So default to the most useful variant. */
10845 if (tdep->fp_model == ARM_FLOAT_AUTO)
10846 tdep->fp_model = ARM_FLOAT_SOFT_FPA;
10848 if (tdep->jb_pc >= 0)
10849 set_gdbarch_get_longjmp_target (gdbarch, arm_get_longjmp_target);
10851 /* Floating point sizes and format. */
10852 set_gdbarch_float_format (gdbarch, floatformats_ieee_single);
10853 if (tdep->fp_model == ARM_FLOAT_SOFT_FPA || tdep->fp_model == ARM_FLOAT_FPA)
10855 set_gdbarch_double_format
10856 (gdbarch, floatformats_ieee_double_littlebyte_bigword);
10857 set_gdbarch_long_double_format
10858 (gdbarch, floatformats_ieee_double_littlebyte_bigword);
10860 else
10862 set_gdbarch_double_format (gdbarch, floatformats_ieee_double);
10863 set_gdbarch_long_double_format (gdbarch, floatformats_ieee_double);
10866 /* Hook used to decorate frames with signed return addresses, only available
10867 for ARMv8.1-m PACBTI. */
10868 if (is_m && have_pacbti)
10869 set_gdbarch_get_pc_address_flags (gdbarch, arm_get_pc_address_flags);
10871 if (tdesc_data != nullptr)
10873 set_tdesc_pseudo_register_name (gdbarch, arm_register_name);
10875 tdesc_use_registers (gdbarch, tdesc, std::move (tdesc_data));
10876 register_count = gdbarch_num_regs (gdbarch);
10878 /* Override tdesc_register_type to adjust the types of VFP
10879 registers for NEON. */
10880 set_gdbarch_register_type (gdbarch, arm_register_type);
10883 /* Initialize the pseudo register data. */
10884 int num_pseudos = 0;
10885 if (tdep->have_s_pseudos)
10887 /* VFP single precision pseudo registers (S0~S31). */
10888 tdep->s_pseudo_base = register_count;
10889 tdep->s_pseudo_count = 32;
10890 num_pseudos += tdep->s_pseudo_count;
10892 if (tdep->have_q_pseudos)
10894 /* NEON quad precision pseudo registers (Q0~Q15). */
10895 tdep->q_pseudo_base = register_count + num_pseudos;
10897 if (have_neon)
10898 tdep->q_pseudo_count = 16;
10899 else if (have_mve)
10900 tdep->q_pseudo_count = ARM_MVE_NUM_Q_REGS;
10902 num_pseudos += tdep->q_pseudo_count;
10906 /* Do we have any MVE pseudo registers? */
10907 if (have_mve)
10909 tdep->mve_pseudo_base = register_count + num_pseudos;
10910 tdep->mve_pseudo_count = 1;
10911 num_pseudos += tdep->mve_pseudo_count;
10914 /* Do we have any ARMv8.1-m PACBTI pseudo registers. */
10915 if (have_pacbti)
10917 tdep->pacbti_pseudo_base = register_count + num_pseudos;
10918 tdep->pacbti_pseudo_count = 1;
10919 num_pseudos += tdep->pacbti_pseudo_count;
10922 /* Set some pseudo register hooks, if we have pseudo registers. */
10923 if (tdep->have_s_pseudos || have_mve || have_pacbti)
10925 set_gdbarch_num_pseudo_regs (gdbarch, num_pseudos);
10926 set_gdbarch_pseudo_register_read_value (gdbarch, arm_pseudo_read_value);
10927 set_gdbarch_pseudo_register_write (gdbarch, arm_pseudo_write);
10930 /* Add standard register aliases. We add aliases even for those
10931 names which are used by the current architecture - it's simpler,
10932 and does no harm, since nothing ever lists user registers. */
10933 for (i = 0; i < ARRAY_SIZE (arm_register_aliases); i++)
10934 user_reg_add (gdbarch, arm_register_aliases[i].name,
10935 value_of_arm_user_reg, &arm_register_aliases[i].regnum);
10937 set_gdbarch_disassembler_options (gdbarch, &arm_disassembler_options);
10938 set_gdbarch_valid_disassembler_options (gdbarch, disassembler_options_arm ());
10940 set_gdbarch_gnu_triplet_regexp (gdbarch, arm_gnu_triplet_regexp);
10942 return gdbarch;
10945 static void
10946 arm_dump_tdep (struct gdbarch *gdbarch, struct ui_file *file)
10948 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
10950 if (tdep == NULL)
10951 return;
10953 gdb_printf (file, _("arm_dump_tdep: fp_model = %i\n"),
10954 (int) tdep->fp_model);
10955 gdb_printf (file, _("arm_dump_tdep: have_fpa_registers = %i\n"),
10956 (int) tdep->have_fpa_registers);
10957 gdb_printf (file, _("arm_dump_tdep: have_wmmx_registers = %i\n"),
10958 (int) tdep->have_wmmx_registers);
10959 gdb_printf (file, _("arm_dump_tdep: vfp_register_count = %i\n"),
10960 (int) tdep->vfp_register_count);
10961 gdb_printf (file, _("arm_dump_tdep: have_s_pseudos = %s\n"),
10962 tdep->have_s_pseudos ? "true" : "false");
10963 gdb_printf (file, _("arm_dump_tdep: s_pseudo_base = %i\n"),
10964 (int) tdep->s_pseudo_base);
10965 gdb_printf (file, _("arm_dump_tdep: s_pseudo_count = %i\n"),
10966 (int) tdep->s_pseudo_count);
10967 gdb_printf (file, _("arm_dump_tdep: have_q_pseudos = %s\n"),
10968 tdep->have_q_pseudos ? "true" : "false");
10969 gdb_printf (file, _("arm_dump_tdep: q_pseudo_base = %i\n"),
10970 (int) tdep->q_pseudo_base);
10971 gdb_printf (file, _("arm_dump_tdep: q_pseudo_count = %i\n"),
10972 (int) tdep->q_pseudo_count);
10973 gdb_printf (file, _("arm_dump_tdep: have_neon = %i\n"),
10974 (int) tdep->have_neon);
10975 gdb_printf (file, _("arm_dump_tdep: have_mve = %s\n"),
10976 tdep->have_mve ? "yes" : "no");
10977 gdb_printf (file, _("arm_dump_tdep: mve_vpr_regnum = %i\n"),
10978 tdep->mve_vpr_regnum);
10979 gdb_printf (file, _("arm_dump_tdep: mve_pseudo_base = %i\n"),
10980 tdep->mve_pseudo_base);
10981 gdb_printf (file, _("arm_dump_tdep: mve_pseudo_count = %i\n"),
10982 tdep->mve_pseudo_count);
10983 gdb_printf (file, _("arm_dump_tdep: m_profile_msp_regnum = %i\n"),
10984 tdep->m_profile_msp_regnum);
10985 gdb_printf (file, _("arm_dump_tdep: m_profile_psp_regnum = %i\n"),
10986 tdep->m_profile_psp_regnum);
10987 gdb_printf (file, _("arm_dump_tdep: m_profile_msp_ns_regnum = %i\n"),
10988 tdep->m_profile_msp_ns_regnum);
10989 gdb_printf (file, _("arm_dump_tdep: m_profile_psp_ns_regnum = %i\n"),
10990 tdep->m_profile_psp_ns_regnum);
10991 gdb_printf (file, _("arm_dump_tdep: m_profile_msp_s_regnum = %i\n"),
10992 tdep->m_profile_msp_s_regnum);
10993 gdb_printf (file, _("arm_dump_tdep: m_profile_psp_s_regnum = %i\n"),
10994 tdep->m_profile_psp_s_regnum);
10995 gdb_printf (file, _("arm_dump_tdep: Lowest pc = 0x%lx\n"),
10996 (unsigned long) tdep->lowest_pc);
10997 gdb_printf (file, _("arm_dump_tdep: have_pacbti = %s\n"),
10998 tdep->have_pacbti ? "yes" : "no");
10999 gdb_printf (file, _("arm_dump_tdep: pacbti_pseudo_base = %i\n"),
11000 tdep->pacbti_pseudo_base);
11001 gdb_printf (file, _("arm_dump_tdep: pacbti_pseudo_count = %i\n"),
11002 tdep->pacbti_pseudo_count);
11003 gdb_printf (file, _("arm_dump_tdep: is_m = %s\n"),
11004 tdep->is_m ? "yes" : "no");
11007 #if GDB_SELF_TEST
11008 namespace selftests
11010 static void arm_record_test (void);
11011 static void arm_analyze_prologue_test ();
11013 #endif
11015 void _initialize_arm_tdep ();
11016 void
11017 _initialize_arm_tdep ()
11019 long length;
11020 int i, j;
11021 char regdesc[1024], *rdptr = regdesc;
11022 size_t rest = sizeof (regdesc);
11024 gdbarch_register (bfd_arch_arm, arm_gdbarch_init, arm_dump_tdep);
11026 /* Add ourselves to objfile event chain. */
11027 gdb::observers::new_objfile.attach (arm_exidx_new_objfile, "arm-tdep");
11029 /* Register an ELF OS ABI sniffer for ARM binaries. */
11030 gdbarch_register_osabi_sniffer (bfd_arch_arm,
11031 bfd_target_elf_flavour,
11032 arm_elf_osabi_sniffer);
11034 /* Add root prefix command for all "set arm"/"show arm" commands. */
11035 add_setshow_prefix_cmd ("arm", no_class,
11036 _("Various ARM-specific commands."),
11037 _("Various ARM-specific commands."),
11038 &setarmcmdlist, &showarmcmdlist,
11039 &setlist, &showlist);
11041 arm_disassembler_options = "reg-names-std";
11042 const disasm_options_t *disasm_options
11043 = &disassembler_options_arm ()->options;
11044 int num_disassembly_styles = 0;
11045 for (i = 0; disasm_options->name[i] != NULL; i++)
11046 if (startswith (disasm_options->name[i], "reg-names-"))
11047 num_disassembly_styles++;
11049 /* Initialize the array that will be passed to add_setshow_enum_cmd(). */
11050 valid_disassembly_styles = XNEWVEC (const char *,
11051 num_disassembly_styles + 1);
11052 for (i = j = 0; disasm_options->name[i] != NULL; i++)
11053 if (startswith (disasm_options->name[i], "reg-names-"))
11055 size_t offset = strlen ("reg-names-");
11056 const char *style = disasm_options->name[i];
11057 valid_disassembly_styles[j++] = &style[offset];
11058 if (strcmp (&style[offset], "std") == 0)
11059 disassembly_style = &style[offset];
11060 length = snprintf (rdptr, rest, "%s - %s\n", &style[offset],
11061 disasm_options->description[i]);
11062 rdptr += length;
11063 rest -= length;
11065 /* Mark the end of valid options. */
11066 valid_disassembly_styles[num_disassembly_styles] = NULL;
11068 /* Create the help text. */
11069 std::string helptext = string_printf ("%s%s%s",
11070 _("The valid values are:\n"),
11071 regdesc,
11072 _("The default is \"std\"."));
11074 add_setshow_enum_cmd("disassembler", no_class,
11075 valid_disassembly_styles, &disassembly_style,
11076 _("Set the disassembly style."),
11077 _("Show the disassembly style."),
11078 helptext.c_str (),
11079 set_disassembly_style_sfunc,
11080 show_disassembly_style_sfunc,
11081 &setarmcmdlist, &showarmcmdlist);
11083 add_setshow_boolean_cmd ("apcs32", no_class, &arm_apcs_32,
11084 _("Set usage of ARM 32-bit mode."),
11085 _("Show usage of ARM 32-bit mode."),
11086 _("When off, a 26-bit PC will be used."),
11087 NULL,
11088 NULL, /* FIXME: i18n: Usage of ARM 32-bit
11089 mode is %s. */
11090 &setarmcmdlist, &showarmcmdlist);
11092 /* Add a command to allow the user to force the FPU model. */
11093 add_setshow_enum_cmd ("fpu", no_class, fp_model_strings, &current_fp_model,
11094 _("Set the floating point type."),
11095 _("Show the floating point type."),
11096 _("auto - Determine the FP typefrom the OS-ABI.\n\
11097 softfpa - Software FP, mixed-endian doubles on little-endian ARMs.\n\
11098 fpa - FPA co-processor (GCC compiled).\n\
11099 softvfp - Software FP with pure-endian doubles.\n\
11100 vfp - VFP co-processor."),
11101 set_fp_model_sfunc, show_fp_model,
11102 &setarmcmdlist, &showarmcmdlist);
11104 /* Add a command to allow the user to force the ABI. */
11105 add_setshow_enum_cmd ("abi", class_support, arm_abi_strings, &arm_abi_string,
11106 _("Set the ABI."),
11107 _("Show the ABI."),
11108 NULL, arm_set_abi, arm_show_abi,
11109 &setarmcmdlist, &showarmcmdlist);
11111 /* Add two commands to allow the user to force the assumed
11112 execution mode. */
11113 add_setshow_enum_cmd ("fallback-mode", class_support,
11114 arm_mode_strings, &arm_fallback_mode_string,
11115 _("Set the mode assumed when symbols are unavailable."),
11116 _("Show the mode assumed when symbols are unavailable."),
11117 NULL, NULL, arm_show_fallback_mode,
11118 &setarmcmdlist, &showarmcmdlist);
11119 add_setshow_enum_cmd ("force-mode", class_support,
11120 arm_mode_strings, &arm_force_mode_string,
11121 _("Set the mode assumed even when symbols are available."),
11122 _("Show the mode assumed even when symbols are available."),
11123 NULL, NULL, arm_show_force_mode,
11124 &setarmcmdlist, &showarmcmdlist);
11126 /* Add a command to stop triggering security exceptions when
11127 unwinding exception stacks. */
11128 add_setshow_boolean_cmd ("unwind-secure-frames", no_class, &arm_unwind_secure_frames,
11129 _("Set usage of non-secure to secure exception stack unwinding."),
11130 _("Show usage of non-secure to secure exception stack unwinding."),
11131 _("When on, the debugger can trigger memory access traps."),
11132 NULL, arm_show_unwind_secure_frames,
11133 &setarmcmdlist, &showarmcmdlist);
11135 /* Debugging flag. */
11136 add_setshow_boolean_cmd ("arm", class_maintenance, &arm_debug,
11137 _("Set ARM debugging."),
11138 _("Show ARM debugging."),
11139 _("When on, arm-specific debugging is enabled."),
11140 NULL,
11141 NULL, /* FIXME: i18n: "ARM debugging is %s. */
11142 &setdebuglist, &showdebuglist);
11144 #if GDB_SELF_TEST
11145 selftests::register_test ("arm-record", selftests::arm_record_test);
11146 selftests::register_test ("arm_analyze_prologue", selftests::arm_analyze_prologue_test);
11147 #endif
11151 /* ARM-reversible process record data structures. */
11153 #define ARM_INSN_SIZE_BYTES 4
11154 #define THUMB_INSN_SIZE_BYTES 2
11155 #define THUMB2_INSN_SIZE_BYTES 4
11158 /* Position of the bit within a 32-bit ARM instruction
11159 that defines whether the instruction is a load or store. */
11160 #define INSN_S_L_BIT_NUM 20
11162 #define REG_ALLOC(REGS, LENGTH, RECORD_BUF) \
11163 do \
11165 unsigned int reg_len = LENGTH; \
11166 if (reg_len) \
11168 REGS = XNEWVEC (uint32_t, reg_len); \
11169 memcpy(&REGS[0], &RECORD_BUF[0], sizeof(uint32_t)*LENGTH); \
11172 while (0)
11174 #define MEM_ALLOC(MEMS, LENGTH, RECORD_BUF) \
11175 do \
11177 unsigned int mem_len = LENGTH; \
11178 if (mem_len) \
11180 MEMS = XNEWVEC (struct arm_mem_r, mem_len); \
11181 memcpy(&MEMS->len, &RECORD_BUF[0], \
11182 sizeof(struct arm_mem_r) * LENGTH); \
11185 while (0)
11187 /* Checks whether insn is already recorded or yet to be decoded. (boolean expression). */
11188 #define INSN_RECORDED(ARM_RECORD) \
11189 (0 != (ARM_RECORD)->reg_rec_count || 0 != (ARM_RECORD)->mem_rec_count)
11191 /* ARM memory record structure. */
11192 struct arm_mem_r
11194 uint32_t len; /* Record length. */
11195 uint32_t addr; /* Memory address. */
11198 /* ARM instruction record contains opcode of current insn
11199 and execution state (before entry to decode_insn()),
11200 contains list of to-be-modified registers and
11201 memory blocks (on return from decode_insn()). */
11203 struct arm_insn_decode_record
11205 struct gdbarch *gdbarch;
11206 struct regcache *regcache;
11207 CORE_ADDR this_addr; /* Address of the insn being decoded. */
11208 uint32_t arm_insn; /* Should accommodate thumb. */
11209 uint32_t cond; /* Condition code. */
11210 uint32_t opcode; /* Insn opcode. */
11211 uint32_t decode; /* Insn decode bits. */
11212 uint32_t mem_rec_count; /* No of mem records. */
11213 uint32_t reg_rec_count; /* No of reg records. */
11214 uint32_t *arm_regs; /* Registers to be saved for this record. */
11215 struct arm_mem_r *arm_mems; /* Memory to be saved for this record. */
11219 /* Checks ARM SBZ and SBO mandatory fields. */
11221 static int
11222 sbo_sbz (uint32_t insn, uint32_t bit_num, uint32_t len, uint32_t sbo)
11224 uint32_t ones = bits (insn, bit_num - 1, (bit_num -1) + (len - 1));
11226 if (!len)
11227 return 1;
11229 if (!sbo)
11230 ones = ~ones;
11232 while (ones)
11234 if (!(ones & sbo))
11236 return 0;
11238 ones = ones >> 1;
11240 return 1;
11243 enum arm_record_result
11245 ARM_RECORD_SUCCESS = 0,
11246 ARM_RECORD_FAILURE = 1
11249 enum arm_record_strx_t
11251 ARM_RECORD_STRH=1,
11252 ARM_RECORD_STRD
11255 enum record_type_t
11257 ARM_RECORD=1,
11258 THUMB_RECORD,
11259 THUMB2_RECORD
11263 static int
11264 arm_record_strx (arm_insn_decode_record *arm_insn_r, uint32_t *record_buf,
11265 uint32_t *record_buf_mem, arm_record_strx_t str_type)
11268 struct regcache *reg_cache = arm_insn_r->regcache;
11269 ULONGEST u_regval[2]= {0};
11271 uint32_t reg_src1 = 0, reg_src2 = 0;
11272 uint32_t immed_high = 0, immed_low = 0,offset_8 = 0, tgt_mem_addr = 0;
11274 arm_insn_r->opcode = bits (arm_insn_r->arm_insn, 21, 24);
11275 arm_insn_r->decode = bits (arm_insn_r->arm_insn, 4, 7);
11277 if (14 == arm_insn_r->opcode || 10 == arm_insn_r->opcode)
11279 /* 1) Handle misc store, immediate offset. */
11280 immed_low = bits (arm_insn_r->arm_insn, 0, 3);
11281 immed_high = bits (arm_insn_r->arm_insn, 8, 11);
11282 reg_src1 = bits (arm_insn_r->arm_insn, 16, 19);
11283 regcache_raw_read_unsigned (reg_cache, reg_src1,
11284 &u_regval[0]);
11285 if (ARM_PC_REGNUM == reg_src1)
11287 /* If R15 was used as Rn, hence current PC+8. */
11288 u_regval[0] = u_regval[0] + 8;
11290 offset_8 = (immed_high << 4) | immed_low;
11291 /* Calculate target store address. */
11292 if (14 == arm_insn_r->opcode)
11294 tgt_mem_addr = u_regval[0] + offset_8;
11296 else
11298 tgt_mem_addr = u_regval[0] - offset_8;
11300 if (ARM_RECORD_STRH == str_type)
11302 record_buf_mem[0] = 2;
11303 record_buf_mem[1] = tgt_mem_addr;
11304 arm_insn_r->mem_rec_count = 1;
11306 else if (ARM_RECORD_STRD == str_type)
11308 record_buf_mem[0] = 4;
11309 record_buf_mem[1] = tgt_mem_addr;
11310 record_buf_mem[2] = 4;
11311 record_buf_mem[3] = tgt_mem_addr + 4;
11312 arm_insn_r->mem_rec_count = 2;
11315 else if (12 == arm_insn_r->opcode || 8 == arm_insn_r->opcode)
11317 /* 2) Store, register offset. */
11318 /* Get Rm. */
11319 reg_src1 = bits (arm_insn_r->arm_insn, 0, 3);
11320 /* Get Rn. */
11321 reg_src2 = bits (arm_insn_r->arm_insn, 16, 19);
11322 regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval[0]);
11323 regcache_raw_read_unsigned (reg_cache, reg_src2, &u_regval[1]);
11324 if (15 == reg_src2)
11326 /* If R15 was used as Rn, hence current PC+8. */
11327 u_regval[0] = u_regval[0] + 8;
11329 /* Calculate target store address, Rn +/- Rm, register offset. */
11330 if (12 == arm_insn_r->opcode)
11332 tgt_mem_addr = u_regval[0] + u_regval[1];
11334 else
11336 tgt_mem_addr = u_regval[1] - u_regval[0];
11338 if (ARM_RECORD_STRH == str_type)
11340 record_buf_mem[0] = 2;
11341 record_buf_mem[1] = tgt_mem_addr;
11342 arm_insn_r->mem_rec_count = 1;
11344 else if (ARM_RECORD_STRD == str_type)
11346 record_buf_mem[0] = 4;
11347 record_buf_mem[1] = tgt_mem_addr;
11348 record_buf_mem[2] = 4;
11349 record_buf_mem[3] = tgt_mem_addr + 4;
11350 arm_insn_r->mem_rec_count = 2;
11353 else if (11 == arm_insn_r->opcode || 15 == arm_insn_r->opcode
11354 || 2 == arm_insn_r->opcode || 6 == arm_insn_r->opcode)
11356 /* 3) Store, immediate pre-indexed. */
11357 /* 5) Store, immediate post-indexed. */
11358 immed_low = bits (arm_insn_r->arm_insn, 0, 3);
11359 immed_high = bits (arm_insn_r->arm_insn, 8, 11);
11360 offset_8 = (immed_high << 4) | immed_low;
11361 reg_src1 = bits (arm_insn_r->arm_insn, 16, 19);
11362 regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval[0]);
11363 /* Calculate target store address, Rn +/- Rm, register offset. */
11364 if (15 == arm_insn_r->opcode || 6 == arm_insn_r->opcode)
11366 tgt_mem_addr = u_regval[0] + offset_8;
11368 else
11370 tgt_mem_addr = u_regval[0] - offset_8;
11372 if (ARM_RECORD_STRH == str_type)
11374 record_buf_mem[0] = 2;
11375 record_buf_mem[1] = tgt_mem_addr;
11376 arm_insn_r->mem_rec_count = 1;
11378 else if (ARM_RECORD_STRD == str_type)
11380 record_buf_mem[0] = 4;
11381 record_buf_mem[1] = tgt_mem_addr;
11382 record_buf_mem[2] = 4;
11383 record_buf_mem[3] = tgt_mem_addr + 4;
11384 arm_insn_r->mem_rec_count = 2;
11386 /* Record Rn also as it changes. */
11387 *(record_buf) = bits (arm_insn_r->arm_insn, 16, 19);
11388 arm_insn_r->reg_rec_count = 1;
11390 else if (9 == arm_insn_r->opcode || 13 == arm_insn_r->opcode
11391 || 0 == arm_insn_r->opcode || 4 == arm_insn_r->opcode)
11393 /* 4) Store, register pre-indexed. */
11394 /* 6) Store, register post -indexed. */
11395 reg_src1 = bits (arm_insn_r->arm_insn, 0, 3);
11396 reg_src2 = bits (arm_insn_r->arm_insn, 16, 19);
11397 regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval[0]);
11398 regcache_raw_read_unsigned (reg_cache, reg_src2, &u_regval[1]);
11399 /* Calculate target store address, Rn +/- Rm, register offset. */
11400 if (13 == arm_insn_r->opcode || 4 == arm_insn_r->opcode)
11402 tgt_mem_addr = u_regval[0] + u_regval[1];
11404 else
11406 tgt_mem_addr = u_regval[1] - u_regval[0];
11408 if (ARM_RECORD_STRH == str_type)
11410 record_buf_mem[0] = 2;
11411 record_buf_mem[1] = tgt_mem_addr;
11412 arm_insn_r->mem_rec_count = 1;
11414 else if (ARM_RECORD_STRD == str_type)
11416 record_buf_mem[0] = 4;
11417 record_buf_mem[1] = tgt_mem_addr;
11418 record_buf_mem[2] = 4;
11419 record_buf_mem[3] = tgt_mem_addr + 4;
11420 arm_insn_r->mem_rec_count = 2;
11422 /* Record Rn also as it changes. */
11423 *(record_buf) = bits (arm_insn_r->arm_insn, 16, 19);
11424 arm_insn_r->reg_rec_count = 1;
11426 return 0;
11429 /* Handling ARM extension space insns. */
11431 static int
11432 arm_record_extension_space (arm_insn_decode_record *arm_insn_r)
11434 int ret = 0; /* Return value: -1:record failure ; 0:success */
11435 uint32_t opcode1 = 0, opcode2 = 0, insn_op1 = 0;
11436 uint32_t record_buf[8], record_buf_mem[8];
11437 uint32_t reg_src1 = 0;
11438 struct regcache *reg_cache = arm_insn_r->regcache;
11439 ULONGEST u_regval = 0;
11441 gdb_assert (!INSN_RECORDED(arm_insn_r));
11442 /* Handle unconditional insn extension space. */
11444 opcode1 = bits (arm_insn_r->arm_insn, 20, 27);
11445 opcode2 = bits (arm_insn_r->arm_insn, 4, 7);
11446 if (arm_insn_r->cond)
11448 /* PLD has no affect on architectural state, it just affects
11449 the caches. */
11450 if (5 == ((opcode1 & 0xE0) >> 5))
11452 /* BLX(1) */
11453 record_buf[0] = ARM_PS_REGNUM;
11454 record_buf[1] = ARM_LR_REGNUM;
11455 arm_insn_r->reg_rec_count = 2;
11457 /* STC2, LDC2, MCR2, MRC2, CDP2: <TBD>, co-processor insn. */
11461 opcode1 = bits (arm_insn_r->arm_insn, 25, 27);
11462 if (3 == opcode1 && bit (arm_insn_r->arm_insn, 4))
11464 ret = -1;
11465 /* Undefined instruction on ARM V5; need to handle if later
11466 versions define it. */
11469 opcode1 = bits (arm_insn_r->arm_insn, 24, 27);
11470 opcode2 = bits (arm_insn_r->arm_insn, 4, 7);
11471 insn_op1 = bits (arm_insn_r->arm_insn, 20, 23);
11473 /* Handle arithmetic insn extension space. */
11474 if (!opcode1 && 9 == opcode2 && 1 != arm_insn_r->cond
11475 && !INSN_RECORDED(arm_insn_r))
11477 /* Handle MLA(S) and MUL(S). */
11478 if (in_inclusive_range (insn_op1, 0U, 3U))
11480 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11481 record_buf[1] = ARM_PS_REGNUM;
11482 arm_insn_r->reg_rec_count = 2;
11484 else if (in_inclusive_range (insn_op1, 4U, 15U))
11486 /* Handle SMLAL(S), SMULL(S), UMLAL(S), UMULL(S). */
11487 record_buf[0] = bits (arm_insn_r->arm_insn, 16, 19);
11488 record_buf[1] = bits (arm_insn_r->arm_insn, 12, 15);
11489 record_buf[2] = ARM_PS_REGNUM;
11490 arm_insn_r->reg_rec_count = 3;
11494 opcode1 = bits (arm_insn_r->arm_insn, 26, 27);
11495 opcode2 = bits (arm_insn_r->arm_insn, 23, 24);
11496 insn_op1 = bits (arm_insn_r->arm_insn, 21, 22);
11498 /* Handle control insn extension space. */
11500 if (!opcode1 && 2 == opcode2 && !bit (arm_insn_r->arm_insn, 20)
11501 && 1 != arm_insn_r->cond && !INSN_RECORDED(arm_insn_r))
11503 if (!bit (arm_insn_r->arm_insn,25))
11505 if (!bits (arm_insn_r->arm_insn, 4, 7))
11507 if ((0 == insn_op1) || (2 == insn_op1))
11509 /* MRS. */
11510 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11511 arm_insn_r->reg_rec_count = 1;
11513 else if (1 == insn_op1)
11515 /* CSPR is going to be changed. */
11516 record_buf[0] = ARM_PS_REGNUM;
11517 arm_insn_r->reg_rec_count = 1;
11519 else if (3 == insn_op1)
11521 /* SPSR is going to be changed. */
11522 /* We need to get SPSR value, which is yet to be done. */
11523 return -1;
11526 else if (1 == bits (arm_insn_r->arm_insn, 4, 7))
11528 if (1 == insn_op1)
11530 /* BX. */
11531 record_buf[0] = ARM_PS_REGNUM;
11532 arm_insn_r->reg_rec_count = 1;
11534 else if (3 == insn_op1)
11536 /* CLZ. */
11537 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11538 arm_insn_r->reg_rec_count = 1;
11541 else if (3 == bits (arm_insn_r->arm_insn, 4, 7))
11543 /* BLX. */
11544 record_buf[0] = ARM_PS_REGNUM;
11545 record_buf[1] = ARM_LR_REGNUM;
11546 arm_insn_r->reg_rec_count = 2;
11548 else if (5 == bits (arm_insn_r->arm_insn, 4, 7))
11550 /* QADD, QSUB, QDADD, QDSUB */
11551 record_buf[0] = ARM_PS_REGNUM;
11552 record_buf[1] = bits (arm_insn_r->arm_insn, 12, 15);
11553 arm_insn_r->reg_rec_count = 2;
11555 else if (7 == bits (arm_insn_r->arm_insn, 4, 7))
11557 /* BKPT. */
11558 record_buf[0] = ARM_PS_REGNUM;
11559 record_buf[1] = ARM_LR_REGNUM;
11560 arm_insn_r->reg_rec_count = 2;
11562 /* Save SPSR also;how? */
11563 return -1;
11565 else if(8 == bits (arm_insn_r->arm_insn, 4, 7)
11566 || 10 == bits (arm_insn_r->arm_insn, 4, 7)
11567 || 12 == bits (arm_insn_r->arm_insn, 4, 7)
11568 || 14 == bits (arm_insn_r->arm_insn, 4, 7)
11571 if (0 == insn_op1 || 1 == insn_op1)
11573 /* SMLA<x><y>, SMLAW<y>, SMULW<y>. */
11574 /* We dont do optimization for SMULW<y> where we
11575 need only Rd. */
11576 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11577 record_buf[1] = ARM_PS_REGNUM;
11578 arm_insn_r->reg_rec_count = 2;
11580 else if (2 == insn_op1)
11582 /* SMLAL<x><y>. */
11583 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11584 record_buf[1] = bits (arm_insn_r->arm_insn, 16, 19);
11585 arm_insn_r->reg_rec_count = 2;
11587 else if (3 == insn_op1)
11589 /* SMUL<x><y>. */
11590 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11591 arm_insn_r->reg_rec_count = 1;
11595 else
11597 /* MSR : immediate form. */
11598 if (1 == insn_op1)
11600 /* CSPR is going to be changed. */
11601 record_buf[0] = ARM_PS_REGNUM;
11602 arm_insn_r->reg_rec_count = 1;
11604 else if (3 == insn_op1)
11606 /* SPSR is going to be changed. */
11607 /* we need to get SPSR value, which is yet to be done */
11608 return -1;
11613 opcode1 = bits (arm_insn_r->arm_insn, 25, 27);
11614 opcode2 = bits (arm_insn_r->arm_insn, 20, 24);
11615 insn_op1 = bits (arm_insn_r->arm_insn, 5, 6);
11617 /* Handle load/store insn extension space. */
11619 if (!opcode1 && bit (arm_insn_r->arm_insn, 7)
11620 && bit (arm_insn_r->arm_insn, 4) && 1 != arm_insn_r->cond
11621 && !INSN_RECORDED(arm_insn_r))
11623 /* SWP/SWPB. */
11624 if (0 == insn_op1)
11626 /* These insn, changes register and memory as well. */
11627 /* SWP or SWPB insn. */
11628 /* Get memory address given by Rn. */
11629 reg_src1 = bits (arm_insn_r->arm_insn, 16, 19);
11630 regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval);
11631 /* SWP insn ?, swaps word. */
11632 if (8 == arm_insn_r->opcode)
11634 record_buf_mem[0] = 4;
11636 else
11638 /* SWPB insn, swaps only byte. */
11639 record_buf_mem[0] = 1;
11641 record_buf_mem[1] = u_regval;
11642 arm_insn_r->mem_rec_count = 1;
11643 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11644 arm_insn_r->reg_rec_count = 1;
11646 else if (1 == insn_op1 && !bit (arm_insn_r->arm_insn, 20))
11648 /* STRH. */
11649 arm_record_strx(arm_insn_r, &record_buf[0], &record_buf_mem[0],
11650 ARM_RECORD_STRH);
11652 else if (2 == insn_op1 && !bit (arm_insn_r->arm_insn, 20))
11654 /* LDRD. */
11655 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11656 record_buf[1] = record_buf[0] + 1;
11657 arm_insn_r->reg_rec_count = 2;
11659 else if (3 == insn_op1 && !bit (arm_insn_r->arm_insn, 20))
11661 /* STRD. */
11662 arm_record_strx(arm_insn_r, &record_buf[0], &record_buf_mem[0],
11663 ARM_RECORD_STRD);
11665 else if (bit (arm_insn_r->arm_insn, 20) && insn_op1 <= 3)
11667 /* LDRH, LDRSB, LDRSH. */
11668 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11669 arm_insn_r->reg_rec_count = 1;
11674 opcode1 = bits (arm_insn_r->arm_insn, 23, 27);
11675 if (24 == opcode1 && bit (arm_insn_r->arm_insn, 21)
11676 && !INSN_RECORDED(arm_insn_r))
11678 ret = -1;
11679 /* Handle coprocessor insn extension space. */
11682 /* To be done for ARMv5 and later; as of now we return -1. */
11683 if (-1 == ret)
11684 return ret;
11686 REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
11687 MEM_ALLOC (arm_insn_r->arm_mems, arm_insn_r->mem_rec_count, record_buf_mem);
11689 return ret;
11692 /* Handling opcode 000 insns. */
11694 static int
11695 arm_record_data_proc_misc_ld_str (arm_insn_decode_record *arm_insn_r)
11697 struct regcache *reg_cache = arm_insn_r->regcache;
11698 uint32_t record_buf[8], record_buf_mem[8];
11699 ULONGEST u_regval[2] = {0};
11701 uint32_t reg_src1 = 0;
11702 uint32_t opcode1 = 0;
11704 arm_insn_r->opcode = bits (arm_insn_r->arm_insn, 21, 24);
11705 arm_insn_r->decode = bits (arm_insn_r->arm_insn, 4, 7);
11706 opcode1 = bits (arm_insn_r->arm_insn, 20, 24);
11708 if (!((opcode1 & 0x19) == 0x10))
11710 /* Data-processing (register) and Data-processing (register-shifted
11711 register */
11712 /* Out of 11 shifter operands mode, all the insn modifies destination
11713 register, which is specified by 13-16 decode. */
11714 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11715 record_buf[1] = ARM_PS_REGNUM;
11716 arm_insn_r->reg_rec_count = 2;
11718 else if ((arm_insn_r->decode < 8) && ((opcode1 & 0x19) == 0x10))
11720 /* Miscellaneous instructions */
11722 if (3 == arm_insn_r->decode && 0x12 == opcode1
11723 && sbo_sbz (arm_insn_r->arm_insn, 9, 12, 1))
11725 /* Handle BLX, branch and link/exchange. */
11726 if (9 == arm_insn_r->opcode)
11728 /* Branch is chosen by setting T bit of CSPR, bitp[0] of Rm,
11729 and R14 stores the return address. */
11730 record_buf[0] = ARM_PS_REGNUM;
11731 record_buf[1] = ARM_LR_REGNUM;
11732 arm_insn_r->reg_rec_count = 2;
11735 else if (7 == arm_insn_r->decode && 0x12 == opcode1)
11737 /* Handle enhanced software breakpoint insn, BKPT. */
11738 /* CPSR is changed to be executed in ARM state, disabling normal
11739 interrupts, entering abort mode. */
11740 /* According to high vector configuration PC is set. */
11741 /* user hit breakpoint and type reverse, in
11742 that case, we need to go back with previous CPSR and
11743 Program Counter. */
11744 record_buf[0] = ARM_PS_REGNUM;
11745 record_buf[1] = ARM_LR_REGNUM;
11746 arm_insn_r->reg_rec_count = 2;
11748 /* Save SPSR also; how? */
11749 return -1;
11751 else if (1 == arm_insn_r->decode && 0x12 == opcode1
11752 && sbo_sbz (arm_insn_r->arm_insn, 9, 12, 1))
11754 /* Handle BX, branch and link/exchange. */
11755 /* Branch is chosen by setting T bit of CSPR, bitp[0] of Rm. */
11756 record_buf[0] = ARM_PS_REGNUM;
11757 arm_insn_r->reg_rec_count = 1;
11759 else if (1 == arm_insn_r->decode && 0x16 == opcode1
11760 && sbo_sbz (arm_insn_r->arm_insn, 9, 4, 1)
11761 && sbo_sbz (arm_insn_r->arm_insn, 17, 4, 1))
11763 /* Count leading zeros: CLZ. */
11764 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11765 arm_insn_r->reg_rec_count = 1;
11767 else if (!bit (arm_insn_r->arm_insn, INSN_S_L_BIT_NUM)
11768 && (8 == arm_insn_r->opcode || 10 == arm_insn_r->opcode)
11769 && sbo_sbz (arm_insn_r->arm_insn, 17, 4, 1)
11770 && sbo_sbz (arm_insn_r->arm_insn, 1, 12, 0))
11772 /* Handle MRS insn. */
11773 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11774 arm_insn_r->reg_rec_count = 1;
11777 else if (9 == arm_insn_r->decode && opcode1 < 0x10)
11779 /* Multiply and multiply-accumulate */
11781 /* Handle multiply instructions. */
11782 /* MLA, MUL, SMLAL, SMULL, UMLAL, UMULL. */
11783 if (0 == arm_insn_r->opcode || 1 == arm_insn_r->opcode)
11785 /* Handle MLA and MUL. */
11786 record_buf[0] = bits (arm_insn_r->arm_insn, 16, 19);
11787 record_buf[1] = ARM_PS_REGNUM;
11788 arm_insn_r->reg_rec_count = 2;
11790 else if (4 <= arm_insn_r->opcode && 7 >= arm_insn_r->opcode)
11792 /* Handle SMLAL, SMULL, UMLAL, UMULL. */
11793 record_buf[0] = bits (arm_insn_r->arm_insn, 16, 19);
11794 record_buf[1] = bits (arm_insn_r->arm_insn, 12, 15);
11795 record_buf[2] = ARM_PS_REGNUM;
11796 arm_insn_r->reg_rec_count = 3;
11799 else if (9 == arm_insn_r->decode && opcode1 > 0x10)
11801 /* Synchronization primitives */
11803 /* Handling SWP, SWPB. */
11804 /* These insn, changes register and memory as well. */
11805 /* SWP or SWPB insn. */
11807 reg_src1 = bits (arm_insn_r->arm_insn, 16, 19);
11808 regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval[0]);
11809 /* SWP insn ?, swaps word. */
11810 if (8 == arm_insn_r->opcode)
11812 record_buf_mem[0] = 4;
11814 else
11816 /* SWPB insn, swaps only byte. */
11817 record_buf_mem[0] = 1;
11819 record_buf_mem[1] = u_regval[0];
11820 arm_insn_r->mem_rec_count = 1;
11821 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11822 arm_insn_r->reg_rec_count = 1;
11824 else if (11 == arm_insn_r->decode || 13 == arm_insn_r->decode
11825 || 15 == arm_insn_r->decode)
11827 if ((opcode1 & 0x12) == 2)
11829 /* Extra load/store (unprivileged) */
11830 return -1;
11832 else
11834 /* Extra load/store */
11835 switch (bits (arm_insn_r->arm_insn, 5, 6))
11837 case 1:
11838 if ((opcode1 & 0x05) == 0x0 || (opcode1 & 0x05) == 0x4)
11840 /* STRH (register), STRH (immediate) */
11841 arm_record_strx (arm_insn_r, &record_buf[0],
11842 &record_buf_mem[0], ARM_RECORD_STRH);
11844 else if ((opcode1 & 0x05) == 0x1)
11846 /* LDRH (register) */
11847 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11848 arm_insn_r->reg_rec_count = 1;
11850 if (bit (arm_insn_r->arm_insn, 21))
11852 /* Write back to Rn. */
11853 record_buf[arm_insn_r->reg_rec_count++]
11854 = bits (arm_insn_r->arm_insn, 16, 19);
11857 else if ((opcode1 & 0x05) == 0x5)
11859 /* LDRH (immediate), LDRH (literal) */
11860 int rn = bits (arm_insn_r->arm_insn, 16, 19);
11862 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11863 arm_insn_r->reg_rec_count = 1;
11865 if (rn != 15)
11867 /*LDRH (immediate) */
11868 if (bit (arm_insn_r->arm_insn, 21))
11870 /* Write back to Rn. */
11871 record_buf[arm_insn_r->reg_rec_count++] = rn;
11875 else
11876 return -1;
11877 break;
11878 case 2:
11879 if ((opcode1 & 0x05) == 0x0)
11881 /* LDRD (register) */
11882 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11883 record_buf[1] = record_buf[0] + 1;
11884 arm_insn_r->reg_rec_count = 2;
11886 if (bit (arm_insn_r->arm_insn, 21))
11888 /* Write back to Rn. */
11889 record_buf[arm_insn_r->reg_rec_count++]
11890 = bits (arm_insn_r->arm_insn, 16, 19);
11893 else if ((opcode1 & 0x05) == 0x1)
11895 /* LDRSB (register) */
11896 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11897 arm_insn_r->reg_rec_count = 1;
11899 if (bit (arm_insn_r->arm_insn, 21))
11901 /* Write back to Rn. */
11902 record_buf[arm_insn_r->reg_rec_count++]
11903 = bits (arm_insn_r->arm_insn, 16, 19);
11906 else if ((opcode1 & 0x05) == 0x4 || (opcode1 & 0x05) == 0x5)
11908 /* LDRD (immediate), LDRD (literal), LDRSB (immediate),
11909 LDRSB (literal) */
11910 int rn = bits (arm_insn_r->arm_insn, 16, 19);
11912 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11913 arm_insn_r->reg_rec_count = 1;
11915 if (rn != 15)
11917 /*LDRD (immediate), LDRSB (immediate) */
11918 if (bit (arm_insn_r->arm_insn, 21))
11920 /* Write back to Rn. */
11921 record_buf[arm_insn_r->reg_rec_count++] = rn;
11925 else
11926 return -1;
11927 break;
11928 case 3:
11929 if ((opcode1 & 0x05) == 0x0)
11931 /* STRD (register) */
11932 arm_record_strx (arm_insn_r, &record_buf[0],
11933 &record_buf_mem[0], ARM_RECORD_STRD);
11935 else if ((opcode1 & 0x05) == 0x1)
11937 /* LDRSH (register) */
11938 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11939 arm_insn_r->reg_rec_count = 1;
11941 if (bit (arm_insn_r->arm_insn, 21))
11943 /* Write back to Rn. */
11944 record_buf[arm_insn_r->reg_rec_count++]
11945 = bits (arm_insn_r->arm_insn, 16, 19);
11948 else if ((opcode1 & 0x05) == 0x4)
11950 /* STRD (immediate) */
11951 arm_record_strx (arm_insn_r, &record_buf[0],
11952 &record_buf_mem[0], ARM_RECORD_STRD);
11954 else if ((opcode1 & 0x05) == 0x5)
11956 /* LDRSH (immediate), LDRSH (literal) */
11957 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11958 arm_insn_r->reg_rec_count = 1;
11960 if (bit (arm_insn_r->arm_insn, 21))
11962 /* Write back to Rn. */
11963 record_buf[arm_insn_r->reg_rec_count++]
11964 = bits (arm_insn_r->arm_insn, 16, 19);
11967 else
11968 return -1;
11969 break;
11970 default:
11971 return -1;
11975 else
11977 return -1;
11980 REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
11981 MEM_ALLOC (arm_insn_r->arm_mems, arm_insn_r->mem_rec_count, record_buf_mem);
11982 return 0;
11985 /* Handling opcode 001 insns. */
11987 static int
11988 arm_record_data_proc_imm (arm_insn_decode_record *arm_insn_r)
11990 uint32_t record_buf[8], record_buf_mem[8];
11992 arm_insn_r->opcode = bits (arm_insn_r->arm_insn, 21, 24);
11993 arm_insn_r->decode = bits (arm_insn_r->arm_insn, 4, 7);
11995 if ((9 == arm_insn_r->opcode || 11 == arm_insn_r->opcode)
11996 && 2 == bits (arm_insn_r->arm_insn, 20, 21)
11997 && sbo_sbz (arm_insn_r->arm_insn, 13, 4, 1)
12000 /* Handle MSR insn. */
12001 if (9 == arm_insn_r->opcode)
12003 /* CSPR is going to be changed. */
12004 record_buf[0] = ARM_PS_REGNUM;
12005 arm_insn_r->reg_rec_count = 1;
12007 else
12009 /* SPSR is going to be changed. */
12012 else if (arm_insn_r->opcode <= 15)
12014 /* Normal data processing insns. */
12015 /* Out of 11 shifter operands mode, all the insn modifies destination
12016 register, which is specified by 13-16 decode. */
12017 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
12018 record_buf[1] = ARM_PS_REGNUM;
12019 arm_insn_r->reg_rec_count = 2;
12021 else
12023 return -1;
12026 REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
12027 MEM_ALLOC (arm_insn_r->arm_mems, arm_insn_r->mem_rec_count, record_buf_mem);
12028 return 0;
12031 static int
12032 arm_record_media (arm_insn_decode_record *arm_insn_r)
12034 uint32_t record_buf[8];
12036 switch (bits (arm_insn_r->arm_insn, 22, 24))
12038 case 0:
12039 /* Parallel addition and subtraction, signed */
12040 case 1:
12041 /* Parallel addition and subtraction, unsigned */
12042 case 2:
12043 case 3:
12044 /* Packing, unpacking, saturation and reversal */
12046 int rd = bits (arm_insn_r->arm_insn, 12, 15);
12048 record_buf[arm_insn_r->reg_rec_count++] = rd;
12050 break;
12052 case 4:
12053 case 5:
12054 /* Signed multiplies */
12056 int rd = bits (arm_insn_r->arm_insn, 16, 19);
12057 unsigned int op1 = bits (arm_insn_r->arm_insn, 20, 22);
12059 record_buf[arm_insn_r->reg_rec_count++] = rd;
12060 if (op1 == 0x0)
12061 record_buf[arm_insn_r->reg_rec_count++] = ARM_PS_REGNUM;
12062 else if (op1 == 0x4)
12063 record_buf[arm_insn_r->reg_rec_count++]
12064 = bits (arm_insn_r->arm_insn, 12, 15);
12066 break;
12068 case 6:
12070 if (bit (arm_insn_r->arm_insn, 21)
12071 && bits (arm_insn_r->arm_insn, 5, 6) == 0x2)
12073 /* SBFX */
12074 record_buf[arm_insn_r->reg_rec_count++]
12075 = bits (arm_insn_r->arm_insn, 12, 15);
12077 else if (bits (arm_insn_r->arm_insn, 20, 21) == 0x0
12078 && bits (arm_insn_r->arm_insn, 5, 7) == 0x0)
12080 /* USAD8 and USADA8 */
12081 record_buf[arm_insn_r->reg_rec_count++]
12082 = bits (arm_insn_r->arm_insn, 16, 19);
12085 break;
12087 case 7:
12089 if (bits (arm_insn_r->arm_insn, 20, 21) == 0x3
12090 && bits (arm_insn_r->arm_insn, 5, 7) == 0x7)
12092 /* Permanently UNDEFINED */
12093 return -1;
12095 else
12097 /* BFC, BFI and UBFX */
12098 record_buf[arm_insn_r->reg_rec_count++]
12099 = bits (arm_insn_r->arm_insn, 12, 15);
12102 break;
12104 default:
12105 return -1;
12108 REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
12110 return 0;
12113 /* Handle ARM mode instructions with opcode 010. */
12115 static int
12116 arm_record_ld_st_imm_offset (arm_insn_decode_record *arm_insn_r)
12118 struct regcache *reg_cache = arm_insn_r->regcache;
12120 uint32_t reg_base , reg_dest;
12121 uint32_t offset_12, tgt_mem_addr;
12122 uint32_t record_buf[8], record_buf_mem[8];
12123 unsigned char wback;
12124 ULONGEST u_regval;
12126 /* Calculate wback. */
12127 wback = (bit (arm_insn_r->arm_insn, 24) == 0)
12128 || (bit (arm_insn_r->arm_insn, 21) == 1);
12130 arm_insn_r->reg_rec_count = 0;
12131 reg_base = bits (arm_insn_r->arm_insn, 16, 19);
12133 if (bit (arm_insn_r->arm_insn, INSN_S_L_BIT_NUM))
12135 /* LDR (immediate), LDR (literal), LDRB (immediate), LDRB (literal), LDRBT
12136 and LDRT. */
12138 reg_dest = bits (arm_insn_r->arm_insn, 12, 15);
12139 record_buf[arm_insn_r->reg_rec_count++] = reg_dest;
12141 /* The LDR instruction is capable of doing branching. If MOV LR, PC
12142 precedes a LDR instruction having R15 as reg_base, it
12143 emulates a branch and link instruction, and hence we need to save
12144 CPSR and PC as well. */
12145 if (ARM_PC_REGNUM == reg_dest)
12146 record_buf[arm_insn_r->reg_rec_count++] = ARM_PS_REGNUM;
12148 /* If wback is true, also save the base register, which is going to be
12149 written to. */
12150 if (wback)
12151 record_buf[arm_insn_r->reg_rec_count++] = reg_base;
12153 else
12155 /* STR (immediate), STRB (immediate), STRBT and STRT. */
12157 offset_12 = bits (arm_insn_r->arm_insn, 0, 11);
12158 regcache_raw_read_unsigned (reg_cache, reg_base, &u_regval);
12160 /* Handle bit U. */
12161 if (bit (arm_insn_r->arm_insn, 23))
12163 /* U == 1: Add the offset. */
12164 tgt_mem_addr = (uint32_t) u_regval + offset_12;
12166 else
12168 /* U == 0: subtract the offset. */
12169 tgt_mem_addr = (uint32_t) u_regval - offset_12;
12172 /* Bit 22 tells us whether the store instruction writes 1 byte or 4
12173 bytes. */
12174 if (bit (arm_insn_r->arm_insn, 22))
12176 /* STRB and STRBT: 1 byte. */
12177 record_buf_mem[0] = 1;
12179 else
12181 /* STR and STRT: 4 bytes. */
12182 record_buf_mem[0] = 4;
12185 /* Handle bit P. */
12186 if (bit (arm_insn_r->arm_insn, 24))
12187 record_buf_mem[1] = tgt_mem_addr;
12188 else
12189 record_buf_mem[1] = (uint32_t) u_regval;
12191 arm_insn_r->mem_rec_count = 1;
12193 /* If wback is true, also save the base register, which is going to be
12194 written to. */
12195 if (wback)
12196 record_buf[arm_insn_r->reg_rec_count++] = reg_base;
12199 REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
12200 MEM_ALLOC (arm_insn_r->arm_mems, arm_insn_r->mem_rec_count, record_buf_mem);
12201 return 0;
12204 /* Handling opcode 011 insns. */
12206 static int
12207 arm_record_ld_st_reg_offset (arm_insn_decode_record *arm_insn_r)
12209 struct regcache *reg_cache = arm_insn_r->regcache;
12211 uint32_t shift_imm = 0;
12212 uint32_t reg_src1 = 0, reg_src2 = 0, reg_dest = 0;
12213 uint32_t offset_12 = 0, tgt_mem_addr = 0;
12214 uint32_t record_buf[8], record_buf_mem[8];
12216 LONGEST s_word;
12217 ULONGEST u_regval[2];
12219 if (bit (arm_insn_r->arm_insn, 4))
12220 return arm_record_media (arm_insn_r);
12222 arm_insn_r->opcode = bits (arm_insn_r->arm_insn, 21, 24);
12223 arm_insn_r->decode = bits (arm_insn_r->arm_insn, 4, 7);
12225 /* Handle enhanced store insns and LDRD DSP insn,
12226 order begins according to addressing modes for store insns
12227 STRH insn. */
12229 /* LDR or STR? */
12230 if (bit (arm_insn_r->arm_insn, INSN_S_L_BIT_NUM))
12232 reg_dest = bits (arm_insn_r->arm_insn, 12, 15);
12233 /* LDR insn has a capability to do branching, if
12234 MOV LR, PC is preceded by LDR insn having Rn as R15
12235 in that case, it emulates branch and link insn, and hence we
12236 need to save CSPR and PC as well. */
12237 if (15 != reg_dest)
12239 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
12240 arm_insn_r->reg_rec_count = 1;
12242 else
12244 record_buf[0] = reg_dest;
12245 record_buf[1] = ARM_PS_REGNUM;
12246 arm_insn_r->reg_rec_count = 2;
12249 else
12251 if (! bits (arm_insn_r->arm_insn, 4, 11))
12253 /* Store insn, register offset and register pre-indexed,
12254 register post-indexed. */
12255 /* Get Rm. */
12256 reg_src1 = bits (arm_insn_r->arm_insn, 0, 3);
12257 /* Get Rn. */
12258 reg_src2 = bits (arm_insn_r->arm_insn, 16, 19);
12259 regcache_raw_read_unsigned (reg_cache, reg_src1
12260 , &u_regval[0]);
12261 regcache_raw_read_unsigned (reg_cache, reg_src2
12262 , &u_regval[1]);
12263 if (15 == reg_src2)
12265 /* If R15 was used as Rn, hence current PC+8. */
12266 /* Pre-indexed mode doesn't reach here ; illegal insn. */
12267 u_regval[0] = u_regval[0] + 8;
12269 /* Calculate target store address, Rn +/- Rm, register offset. */
12270 /* U == 1. */
12271 if (bit (arm_insn_r->arm_insn, 23))
12273 tgt_mem_addr = u_regval[0] + u_regval[1];
12275 else
12277 tgt_mem_addr = u_regval[1] - u_regval[0];
12280 switch (arm_insn_r->opcode)
12282 /* STR. */
12283 case 8:
12284 case 12:
12285 /* STR. */
12286 case 9:
12287 case 13:
12288 /* STRT. */
12289 case 1:
12290 case 5:
12291 /* STR. */
12292 case 0:
12293 case 4:
12294 record_buf_mem[0] = 4;
12295 break;
12297 /* STRB. */
12298 case 10:
12299 case 14:
12300 /* STRB. */
12301 case 11:
12302 case 15:
12303 /* STRBT. */
12304 case 3:
12305 case 7:
12306 /* STRB. */
12307 case 2:
12308 case 6:
12309 record_buf_mem[0] = 1;
12310 break;
12312 default:
12313 gdb_assert_not_reached ("no decoding pattern found");
12314 break;
12316 record_buf_mem[1] = tgt_mem_addr;
12317 arm_insn_r->mem_rec_count = 1;
12319 if (9 == arm_insn_r->opcode || 11 == arm_insn_r->opcode
12320 || 13 == arm_insn_r->opcode || 15 == arm_insn_r->opcode
12321 || 0 == arm_insn_r->opcode || 2 == arm_insn_r->opcode
12322 || 4 == arm_insn_r->opcode || 6 == arm_insn_r->opcode
12323 || 1 == arm_insn_r->opcode || 3 == arm_insn_r->opcode
12324 || 5 == arm_insn_r->opcode || 7 == arm_insn_r->opcode
12327 /* Rn is going to be changed in pre-indexed mode and
12328 post-indexed mode as well. */
12329 record_buf[0] = reg_src2;
12330 arm_insn_r->reg_rec_count = 1;
12333 else
12335 /* Store insn, scaled register offset; scaled pre-indexed. */
12336 offset_12 = bits (arm_insn_r->arm_insn, 5, 6);
12337 /* Get Rm. */
12338 reg_src1 = bits (arm_insn_r->arm_insn, 0, 3);
12339 /* Get Rn. */
12340 reg_src2 = bits (arm_insn_r->arm_insn, 16, 19);
12341 /* Get shift_imm. */
12342 shift_imm = bits (arm_insn_r->arm_insn, 7, 11);
12343 regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval[0]);
12344 regcache_raw_read_signed (reg_cache, reg_src1, &s_word);
12345 regcache_raw_read_unsigned (reg_cache, reg_src2, &u_regval[1]);
12346 /* Offset_12 used as shift. */
12347 switch (offset_12)
12349 case 0:
12350 /* Offset_12 used as index. */
12351 offset_12 = u_regval[0] << shift_imm;
12352 break;
12354 case 1:
12355 offset_12 = (!shift_imm) ? 0 : u_regval[0] >> shift_imm;
12356 break;
12358 case 2:
12359 if (!shift_imm)
12361 if (bit (u_regval[0], 31))
12363 offset_12 = 0xFFFFFFFF;
12365 else
12367 offset_12 = 0;
12370 else
12372 /* This is arithmetic shift. */
12373 offset_12 = s_word >> shift_imm;
12375 break;
12377 case 3:
12378 if (!shift_imm)
12380 regcache_raw_read_unsigned (reg_cache, ARM_PS_REGNUM,
12381 &u_regval[1]);
12382 /* Get C flag value and shift it by 31. */
12383 offset_12 = (((bit (u_regval[1], 29)) << 31) \
12384 | (u_regval[0]) >> 1);
12386 else
12388 offset_12 = (u_regval[0] >> shift_imm) \
12389 | (u_regval[0] <<
12390 (sizeof(uint32_t) - shift_imm));
12392 break;
12394 default:
12395 gdb_assert_not_reached ("no decoding pattern found");
12396 break;
12399 regcache_raw_read_unsigned (reg_cache, reg_src2, &u_regval[1]);
12400 /* bit U set. */
12401 if (bit (arm_insn_r->arm_insn, 23))
12403 tgt_mem_addr = u_regval[1] + offset_12;
12405 else
12407 tgt_mem_addr = u_regval[1] - offset_12;
12410 switch (arm_insn_r->opcode)
12412 /* STR. */
12413 case 8:
12414 case 12:
12415 /* STR. */
12416 case 9:
12417 case 13:
12418 /* STRT. */
12419 case 1:
12420 case 5:
12421 /* STR. */
12422 case 0:
12423 case 4:
12424 record_buf_mem[0] = 4;
12425 break;
12427 /* STRB. */
12428 case 10:
12429 case 14:
12430 /* STRB. */
12431 case 11:
12432 case 15:
12433 /* STRBT. */
12434 case 3:
12435 case 7:
12436 /* STRB. */
12437 case 2:
12438 case 6:
12439 record_buf_mem[0] = 1;
12440 break;
12442 default:
12443 gdb_assert_not_reached ("no decoding pattern found");
12444 break;
12446 record_buf_mem[1] = tgt_mem_addr;
12447 arm_insn_r->mem_rec_count = 1;
12449 if (9 == arm_insn_r->opcode || 11 == arm_insn_r->opcode
12450 || 13 == arm_insn_r->opcode || 15 == arm_insn_r->opcode
12451 || 0 == arm_insn_r->opcode || 2 == arm_insn_r->opcode
12452 || 4 == arm_insn_r->opcode || 6 == arm_insn_r->opcode
12453 || 1 == arm_insn_r->opcode || 3 == arm_insn_r->opcode
12454 || 5 == arm_insn_r->opcode || 7 == arm_insn_r->opcode
12457 /* Rn is going to be changed in register scaled pre-indexed
12458 mode,and scaled post indexed mode. */
12459 record_buf[0] = reg_src2;
12460 arm_insn_r->reg_rec_count = 1;
12465 REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
12466 MEM_ALLOC (arm_insn_r->arm_mems, arm_insn_r->mem_rec_count, record_buf_mem);
12467 return 0;
12470 /* Handle ARM mode instructions with opcode 100. */
12472 static int
12473 arm_record_ld_st_multiple (arm_insn_decode_record *arm_insn_r)
12475 struct regcache *reg_cache = arm_insn_r->regcache;
12476 uint32_t register_count = 0, register_bits;
12477 uint32_t reg_base, addr_mode;
12478 uint32_t record_buf[24], record_buf_mem[48];
12479 uint32_t wback;
12480 ULONGEST u_regval;
12482 /* Fetch the list of registers. */
12483 register_bits = bits (arm_insn_r->arm_insn, 0, 15);
12484 arm_insn_r->reg_rec_count = 0;
12486 /* Fetch the base register that contains the address we are loading data
12487 to. */
12488 reg_base = bits (arm_insn_r->arm_insn, 16, 19);
12490 /* Calculate wback. */
12491 wback = (bit (arm_insn_r->arm_insn, 21) == 1);
12493 if (bit (arm_insn_r->arm_insn, INSN_S_L_BIT_NUM))
12495 /* LDM/LDMIA/LDMFD, LDMDA/LDMFA, LDMDB and LDMIB. */
12497 /* Find out which registers are going to be loaded from memory. */
12498 while (register_bits)
12500 if (register_bits & 0x00000001)
12501 record_buf[arm_insn_r->reg_rec_count++] = register_count;
12502 register_bits = register_bits >> 1;
12503 register_count++;
12507 /* If wback is true, also save the base register, which is going to be
12508 written to. */
12509 if (wback)
12510 record_buf[arm_insn_r->reg_rec_count++] = reg_base;
12512 /* Save the CPSR register. */
12513 record_buf[arm_insn_r->reg_rec_count++] = ARM_PS_REGNUM;
12515 else
12517 /* STM (STMIA, STMEA), STMDA (STMED), STMDB (STMFD) and STMIB (STMFA). */
12519 addr_mode = bits (arm_insn_r->arm_insn, 23, 24);
12521 regcache_raw_read_unsigned (reg_cache, reg_base, &u_regval);
12523 /* Find out how many registers are going to be stored to memory. */
12524 while (register_bits)
12526 if (register_bits & 0x00000001)
12527 register_count++;
12528 register_bits = register_bits >> 1;
12531 switch (addr_mode)
12533 /* STMDA (STMED): Decrement after. */
12534 case 0:
12535 record_buf_mem[1] = (uint32_t) u_regval
12536 - register_count * ARM_INT_REGISTER_SIZE + 4;
12537 break;
12538 /* STM (STMIA, STMEA): Increment after. */
12539 case 1:
12540 record_buf_mem[1] = (uint32_t) u_regval;
12541 break;
12542 /* STMDB (STMFD): Decrement before. */
12543 case 2:
12544 record_buf_mem[1] = (uint32_t) u_regval
12545 - register_count * ARM_INT_REGISTER_SIZE;
12546 break;
12547 /* STMIB (STMFA): Increment before. */
12548 case 3:
12549 record_buf_mem[1] = (uint32_t) u_regval + ARM_INT_REGISTER_SIZE;
12550 break;
12551 default:
12552 gdb_assert_not_reached ("no decoding pattern found");
12553 break;
12556 record_buf_mem[0] = register_count * ARM_INT_REGISTER_SIZE;
12557 arm_insn_r->mem_rec_count = 1;
12559 /* If wback is true, also save the base register, which is going to be
12560 written to. */
12561 if (wback)
12562 record_buf[arm_insn_r->reg_rec_count++] = reg_base;
12565 REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
12566 MEM_ALLOC (arm_insn_r->arm_mems, arm_insn_r->mem_rec_count, record_buf_mem);
12567 return 0;
12570 /* Handling opcode 101 insns. */
12572 static int
12573 arm_record_b_bl (arm_insn_decode_record *arm_insn_r)
12575 uint32_t record_buf[8];
12577 /* Handle B, BL, BLX(1) insns. */
12578 /* B simply branches so we do nothing here. */
12579 /* Note: BLX(1) doesn't fall here but instead it falls into
12580 extension space. */
12581 if (bit (arm_insn_r->arm_insn, 24))
12583 record_buf[0] = ARM_LR_REGNUM;
12584 arm_insn_r->reg_rec_count = 1;
12587 REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
12589 return 0;
12592 static int
12593 arm_record_unsupported_insn (arm_insn_decode_record *arm_insn_r)
12595 gdb_printf (gdb_stderr,
12596 _("Process record does not support instruction "
12597 "0x%0x at address %s.\n"),arm_insn_r->arm_insn,
12598 paddress (arm_insn_r->gdbarch, arm_insn_r->this_addr));
12600 return -1;
12603 /* Record handler for vector data transfer instructions. */
12605 static int
12606 arm_record_vdata_transfer_insn (arm_insn_decode_record *arm_insn_r)
12608 uint32_t bits_a, bit_c, bit_l, reg_t, reg_v;
12609 uint32_t record_buf[4];
12611 reg_t = bits (arm_insn_r->arm_insn, 12, 15);
12612 reg_v = bits (arm_insn_r->arm_insn, 21, 23);
12613 bits_a = bits (arm_insn_r->arm_insn, 21, 23);
12614 bit_l = bit (arm_insn_r->arm_insn, 20);
12615 bit_c = bit (arm_insn_r->arm_insn, 8);
12617 /* Handle VMOV instruction. */
12618 if (bit_l && bit_c)
12620 record_buf[0] = reg_t;
12621 arm_insn_r->reg_rec_count = 1;
12623 else if (bit_l && !bit_c)
12625 /* Handle VMOV instruction. */
12626 if (bits_a == 0x00)
12628 record_buf[0] = reg_t;
12629 arm_insn_r->reg_rec_count = 1;
12631 /* Handle VMRS instruction. */
12632 else if (bits_a == 0x07)
12634 if (reg_t == 15)
12635 reg_t = ARM_PS_REGNUM;
12637 record_buf[0] = reg_t;
12638 arm_insn_r->reg_rec_count = 1;
12641 else if (!bit_l && !bit_c)
12643 /* Handle VMOV instruction. */
12644 if (bits_a == 0x00)
12646 record_buf[0] = ARM_D0_REGNUM + reg_v;
12648 arm_insn_r->reg_rec_count = 1;
12650 /* Handle VMSR instruction. */
12651 else if (bits_a == 0x07)
12653 record_buf[0] = ARM_FPSCR_REGNUM;
12654 arm_insn_r->reg_rec_count = 1;
12657 else if (!bit_l && bit_c)
12659 /* Handle VMOV instruction. */
12660 if (!(bits_a & 0x04))
12662 record_buf[0] = (reg_v | (bit (arm_insn_r->arm_insn, 7) << 4))
12663 + ARM_D0_REGNUM;
12664 arm_insn_r->reg_rec_count = 1;
12666 /* Handle VDUP instruction. */
12667 else
12669 if (bit (arm_insn_r->arm_insn, 21))
12671 reg_v = reg_v | (bit (arm_insn_r->arm_insn, 7) << 4);
12672 record_buf[0] = reg_v + ARM_D0_REGNUM;
12673 record_buf[1] = reg_v + ARM_D0_REGNUM + 1;
12674 arm_insn_r->reg_rec_count = 2;
12676 else
12678 reg_v = reg_v | (bit (arm_insn_r->arm_insn, 7) << 4);
12679 record_buf[0] = reg_v + ARM_D0_REGNUM;
12680 arm_insn_r->reg_rec_count = 1;
12685 REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
12686 return 0;
12689 /* Record handler for extension register load/store instructions. */
12691 static int
12692 arm_record_exreg_ld_st_insn (arm_insn_decode_record *arm_insn_r)
12694 uint32_t opcode, single_reg;
12695 uint8_t op_vldm_vstm;
12696 uint32_t record_buf[8], record_buf_mem[128];
12697 ULONGEST u_regval = 0;
12699 struct regcache *reg_cache = arm_insn_r->regcache;
12701 opcode = bits (arm_insn_r->arm_insn, 20, 24);
12702 single_reg = !bit (arm_insn_r->arm_insn, 8);
12703 op_vldm_vstm = opcode & 0x1b;
12705 /* Handle VMOV instructions. */
12706 if ((opcode & 0x1e) == 0x04)
12708 if (bit (arm_insn_r->arm_insn, 20)) /* to_arm_registers bit 20? */
12710 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
12711 record_buf[1] = bits (arm_insn_r->arm_insn, 16, 19);
12712 arm_insn_r->reg_rec_count = 2;
12714 else
12716 uint8_t reg_m = bits (arm_insn_r->arm_insn, 0, 3);
12717 uint8_t bit_m = bit (arm_insn_r->arm_insn, 5);
12719 if (single_reg)
12721 /* The first S register number m is REG_M:M (M is bit 5),
12722 the corresponding D register number is REG_M:M / 2, which
12723 is REG_M. */
12724 record_buf[arm_insn_r->reg_rec_count++] = ARM_D0_REGNUM + reg_m;
12725 /* The second S register number is REG_M:M + 1, the
12726 corresponding D register number is (REG_M:M + 1) / 2.
12727 IOW, if bit M is 1, the first and second S registers
12728 are mapped to different D registers, otherwise, they are
12729 in the same D register. */
12730 if (bit_m)
12732 record_buf[arm_insn_r->reg_rec_count++]
12733 = ARM_D0_REGNUM + reg_m + 1;
12736 else
12738 record_buf[0] = ((bit_m << 4) + reg_m + ARM_D0_REGNUM);
12739 arm_insn_r->reg_rec_count = 1;
12743 /* Handle VSTM and VPUSH instructions. */
12744 else if (op_vldm_vstm == 0x08 || op_vldm_vstm == 0x0a
12745 || op_vldm_vstm == 0x12)
12747 uint32_t start_address, reg_rn, imm_off32, imm_off8, memory_count;
12748 uint32_t memory_index = 0;
12750 reg_rn = bits (arm_insn_r->arm_insn, 16, 19);
12751 regcache_raw_read_unsigned (reg_cache, reg_rn, &u_regval);
12752 imm_off8 = bits (arm_insn_r->arm_insn, 0, 7);
12753 imm_off32 = imm_off8 << 2;
12754 memory_count = imm_off8;
12756 if (bit (arm_insn_r->arm_insn, 23))
12757 start_address = u_regval;
12758 else
12759 start_address = u_regval - imm_off32;
12761 if (bit (arm_insn_r->arm_insn, 21))
12763 record_buf[0] = reg_rn;
12764 arm_insn_r->reg_rec_count = 1;
12767 while (memory_count > 0)
12769 if (single_reg)
12771 record_buf_mem[memory_index] = 4;
12772 record_buf_mem[memory_index + 1] = start_address;
12773 start_address = start_address + 4;
12774 memory_index = memory_index + 2;
12776 else
12778 record_buf_mem[memory_index] = 4;
12779 record_buf_mem[memory_index + 1] = start_address;
12780 record_buf_mem[memory_index + 2] = 4;
12781 record_buf_mem[memory_index + 3] = start_address + 4;
12782 start_address = start_address + 8;
12783 memory_index = memory_index + 4;
12785 memory_count--;
12787 arm_insn_r->mem_rec_count = (memory_index >> 1);
12789 /* Handle VLDM instructions. */
12790 else if (op_vldm_vstm == 0x09 || op_vldm_vstm == 0x0b
12791 || op_vldm_vstm == 0x13)
12793 uint32_t reg_count, reg_vd;
12794 uint32_t reg_index = 0;
12795 uint32_t bit_d = bit (arm_insn_r->arm_insn, 22);
12797 reg_vd = bits (arm_insn_r->arm_insn, 12, 15);
12798 reg_count = bits (arm_insn_r->arm_insn, 0, 7);
12800 /* REG_VD is the first D register number. If the instruction
12801 loads memory to S registers (SINGLE_REG is TRUE), the register
12802 number is (REG_VD << 1 | bit D), so the corresponding D
12803 register number is (REG_VD << 1 | bit D) / 2 = REG_VD. */
12804 if (!single_reg)
12805 reg_vd = reg_vd | (bit_d << 4);
12807 if (bit (arm_insn_r->arm_insn, 21) /* write back */)
12808 record_buf[reg_index++] = bits (arm_insn_r->arm_insn, 16, 19);
12810 /* If the instruction loads memory to D register, REG_COUNT should
12811 be divided by 2, according to the ARM Architecture Reference
12812 Manual. If the instruction loads memory to S register, divide by
12813 2 as well because two S registers are mapped to D register. */
12814 reg_count = reg_count / 2;
12815 if (single_reg && bit_d)
12817 /* Increase the register count if S register list starts from
12818 an odd number (bit d is one). */
12819 reg_count++;
12822 while (reg_count > 0)
12824 record_buf[reg_index++] = ARM_D0_REGNUM + reg_vd + reg_count - 1;
12825 reg_count--;
12827 arm_insn_r->reg_rec_count = reg_index;
12829 /* VSTR Vector store register. */
12830 else if ((opcode & 0x13) == 0x10)
12832 uint32_t start_address, reg_rn, imm_off32, imm_off8;
12833 uint32_t memory_index = 0;
12835 reg_rn = bits (arm_insn_r->arm_insn, 16, 19);
12836 regcache_raw_read_unsigned (reg_cache, reg_rn, &u_regval);
12837 imm_off8 = bits (arm_insn_r->arm_insn, 0, 7);
12838 imm_off32 = imm_off8 << 2;
12840 if (bit (arm_insn_r->arm_insn, 23))
12841 start_address = u_regval + imm_off32;
12842 else
12843 start_address = u_regval - imm_off32;
12845 if (single_reg)
12847 record_buf_mem[memory_index] = 4;
12848 record_buf_mem[memory_index + 1] = start_address;
12849 arm_insn_r->mem_rec_count = 1;
12851 else
12853 record_buf_mem[memory_index] = 4;
12854 record_buf_mem[memory_index + 1] = start_address;
12855 record_buf_mem[memory_index + 2] = 4;
12856 record_buf_mem[memory_index + 3] = start_address + 4;
12857 arm_insn_r->mem_rec_count = 2;
12860 /* VLDR Vector load register. */
12861 else if ((opcode & 0x13) == 0x11)
12863 uint32_t reg_vd = bits (arm_insn_r->arm_insn, 12, 15);
12865 if (!single_reg)
12867 reg_vd = reg_vd | (bit (arm_insn_r->arm_insn, 22) << 4);
12868 record_buf[0] = ARM_D0_REGNUM + reg_vd;
12870 else
12872 reg_vd = (reg_vd << 1) | bit (arm_insn_r->arm_insn, 22);
12873 /* Record register D rather than pseudo register S. */
12874 record_buf[0] = ARM_D0_REGNUM + reg_vd / 2;
12876 arm_insn_r->reg_rec_count = 1;
12879 REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
12880 MEM_ALLOC (arm_insn_r->arm_mems, arm_insn_r->mem_rec_count, record_buf_mem);
12881 return 0;
12884 /* Record handler for arm/thumb mode VFP data processing instructions. */
12886 static int
12887 arm_record_vfp_data_proc_insn (arm_insn_decode_record *arm_insn_r)
12889 uint32_t opc1, opc2, opc3, dp_op_sz, bit_d, reg_vd;
12890 uint32_t record_buf[4];
12891 enum insn_types {INSN_T0, INSN_T1, INSN_T2, INSN_T3, INSN_INV};
12892 enum insn_types curr_insn_type = INSN_INV;
12894 reg_vd = bits (arm_insn_r->arm_insn, 12, 15);
12895 opc1 = bits (arm_insn_r->arm_insn, 20, 23);
12896 opc2 = bits (arm_insn_r->arm_insn, 16, 19);
12897 opc3 = bits (arm_insn_r->arm_insn, 6, 7);
12898 dp_op_sz = bit (arm_insn_r->arm_insn, 8);
12899 bit_d = bit (arm_insn_r->arm_insn, 22);
12900 /* Mask off the "D" bit. */
12901 opc1 = opc1 & ~0x04;
12903 /* Handle VMLA, VMLS. */
12904 if (opc1 == 0x00)
12906 if (bit (arm_insn_r->arm_insn, 10))
12908 if (bit (arm_insn_r->arm_insn, 6))
12909 curr_insn_type = INSN_T0;
12910 else
12911 curr_insn_type = INSN_T1;
12913 else
12915 if (dp_op_sz)
12916 curr_insn_type = INSN_T1;
12917 else
12918 curr_insn_type = INSN_T2;
12921 /* Handle VNMLA, VNMLS, VNMUL. */
12922 else if (opc1 == 0x01)
12924 if (dp_op_sz)
12925 curr_insn_type = INSN_T1;
12926 else
12927 curr_insn_type = INSN_T2;
12929 /* Handle VMUL. */
12930 else if (opc1 == 0x02 && !(opc3 & 0x01))
12932 if (bit (arm_insn_r->arm_insn, 10))
12934 if (bit (arm_insn_r->arm_insn, 6))
12935 curr_insn_type = INSN_T0;
12936 else
12937 curr_insn_type = INSN_T1;
12939 else
12941 if (dp_op_sz)
12942 curr_insn_type = INSN_T1;
12943 else
12944 curr_insn_type = INSN_T2;
12947 /* Handle VADD, VSUB. */
12948 else if (opc1 == 0x03)
12950 if (!bit (arm_insn_r->arm_insn, 9))
12952 if (bit (arm_insn_r->arm_insn, 6))
12953 curr_insn_type = INSN_T0;
12954 else
12955 curr_insn_type = INSN_T1;
12957 else
12959 if (dp_op_sz)
12960 curr_insn_type = INSN_T1;
12961 else
12962 curr_insn_type = INSN_T2;
12965 /* Handle VDIV. */
12966 else if (opc1 == 0x08)
12968 if (dp_op_sz)
12969 curr_insn_type = INSN_T1;
12970 else
12971 curr_insn_type = INSN_T2;
12973 /* Handle all other vfp data processing instructions. */
12974 else if (opc1 == 0x0b)
12976 /* Handle VMOV. */
12977 if (!(opc3 & 0x01) || (opc2 == 0x00 && opc3 == 0x01))
12979 if (bit (arm_insn_r->arm_insn, 4))
12981 if (bit (arm_insn_r->arm_insn, 6))
12982 curr_insn_type = INSN_T0;
12983 else
12984 curr_insn_type = INSN_T1;
12986 else
12988 if (dp_op_sz)
12989 curr_insn_type = INSN_T1;
12990 else
12991 curr_insn_type = INSN_T2;
12994 /* Handle VNEG and VABS. */
12995 else if ((opc2 == 0x01 && opc3 == 0x01)
12996 || (opc2 == 0x00 && opc3 == 0x03))
12998 if (!bit (arm_insn_r->arm_insn, 11))
13000 if (bit (arm_insn_r->arm_insn, 6))
13001 curr_insn_type = INSN_T0;
13002 else
13003 curr_insn_type = INSN_T1;
13005 else
13007 if (dp_op_sz)
13008 curr_insn_type = INSN_T1;
13009 else
13010 curr_insn_type = INSN_T2;
13013 /* Handle VSQRT. */
13014 else if (opc2 == 0x01 && opc3 == 0x03)
13016 if (dp_op_sz)
13017 curr_insn_type = INSN_T1;
13018 else
13019 curr_insn_type = INSN_T2;
13021 /* Handle VCVT. */
13022 else if (opc2 == 0x07 && opc3 == 0x03)
13024 if (!dp_op_sz)
13025 curr_insn_type = INSN_T1;
13026 else
13027 curr_insn_type = INSN_T2;
13029 else if (opc3 & 0x01)
13031 /* Handle VCVT. */
13032 if ((opc2 == 0x08) || (opc2 & 0x0e) == 0x0c)
13034 if (!bit (arm_insn_r->arm_insn, 18))
13035 curr_insn_type = INSN_T2;
13036 else
13038 if (dp_op_sz)
13039 curr_insn_type = INSN_T1;
13040 else
13041 curr_insn_type = INSN_T2;
13044 /* Handle VCVT. */
13045 else if ((opc2 & 0x0e) == 0x0a || (opc2 & 0x0e) == 0x0e)
13047 if (dp_op_sz)
13048 curr_insn_type = INSN_T1;
13049 else
13050 curr_insn_type = INSN_T2;
13052 /* Handle VCVTB, VCVTT. */
13053 else if ((opc2 & 0x0e) == 0x02)
13054 curr_insn_type = INSN_T2;
13055 /* Handle VCMP, VCMPE. */
13056 else if ((opc2 & 0x0e) == 0x04)
13057 curr_insn_type = INSN_T3;
13061 switch (curr_insn_type)
13063 case INSN_T0:
13064 reg_vd = reg_vd | (bit_d << 4);
13065 record_buf[0] = reg_vd + ARM_D0_REGNUM;
13066 record_buf[1] = reg_vd + ARM_D0_REGNUM + 1;
13067 arm_insn_r->reg_rec_count = 2;
13068 break;
13070 case INSN_T1:
13071 reg_vd = reg_vd | (bit_d << 4);
13072 record_buf[0] = reg_vd + ARM_D0_REGNUM;
13073 arm_insn_r->reg_rec_count = 1;
13074 break;
13076 case INSN_T2:
13077 reg_vd = (reg_vd << 1) | bit_d;
13078 record_buf[0] = reg_vd + ARM_D0_REGNUM;
13079 arm_insn_r->reg_rec_count = 1;
13080 break;
13082 case INSN_T3:
13083 record_buf[0] = ARM_FPSCR_REGNUM;
13084 arm_insn_r->reg_rec_count = 1;
13085 break;
13087 default:
13088 gdb_assert_not_reached ("no decoding pattern found");
13089 break;
13092 REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
13093 return 0;
13096 /* Handling opcode 110 insns. */
13098 static int
13099 arm_record_asimd_vfp_coproc (arm_insn_decode_record *arm_insn_r)
13101 uint32_t op1, op1_ebit, coproc;
13103 coproc = bits (arm_insn_r->arm_insn, 8, 11);
13104 op1 = bits (arm_insn_r->arm_insn, 20, 25);
13105 op1_ebit = bit (arm_insn_r->arm_insn, 20);
13107 if ((coproc & 0x0e) == 0x0a)
13109 /* Handle extension register ld/st instructions. */
13110 if (!(op1 & 0x20))
13111 return arm_record_exreg_ld_st_insn (arm_insn_r);
13113 /* 64-bit transfers between arm core and extension registers. */
13114 if ((op1 & 0x3e) == 0x04)
13115 return arm_record_exreg_ld_st_insn (arm_insn_r);
13117 else
13119 /* Handle coprocessor ld/st instructions. */
13120 if (!(op1 & 0x3a))
13122 /* Store. */
13123 if (!op1_ebit)
13124 return arm_record_unsupported_insn (arm_insn_r);
13125 else
13126 /* Load. */
13127 return arm_record_unsupported_insn (arm_insn_r);
13130 /* Move to coprocessor from two arm core registers. */
13131 if (op1 == 0x4)
13132 return arm_record_unsupported_insn (arm_insn_r);
13134 /* Move to two arm core registers from coprocessor. */
13135 if (op1 == 0x5)
13137 uint32_t reg_t[2];
13139 reg_t[0] = bits (arm_insn_r->arm_insn, 12, 15);
13140 reg_t[1] = bits (arm_insn_r->arm_insn, 16, 19);
13141 arm_insn_r->reg_rec_count = 2;
13143 REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, reg_t);
13144 return 0;
13147 return arm_record_unsupported_insn (arm_insn_r);
13150 /* Handling opcode 111 insns. */
13152 static int
13153 arm_record_coproc_data_proc (arm_insn_decode_record *arm_insn_r)
13155 uint32_t op, op1_ebit, coproc, bits_24_25;
13156 arm_gdbarch_tdep *tdep
13157 = gdbarch_tdep<arm_gdbarch_tdep> (arm_insn_r->gdbarch);
13158 struct regcache *reg_cache = arm_insn_r->regcache;
13160 arm_insn_r->opcode = bits (arm_insn_r->arm_insn, 24, 27);
13161 coproc = bits (arm_insn_r->arm_insn, 8, 11);
13162 op1_ebit = bit (arm_insn_r->arm_insn, 20);
13163 op = bit (arm_insn_r->arm_insn, 4);
13164 bits_24_25 = bits (arm_insn_r->arm_insn, 24, 25);
13166 /* Handle arm SWI/SVC system call instructions. */
13167 if (bits_24_25 == 0x3)
13169 if (tdep->arm_syscall_record != NULL)
13171 ULONGEST svc_operand, svc_number;
13173 svc_operand = (0x00ffffff & arm_insn_r->arm_insn);
13175 if (svc_operand) /* OABI. */
13176 svc_number = svc_operand - 0x900000;
13177 else /* EABI. */
13178 regcache_raw_read_unsigned (reg_cache, 7, &svc_number);
13180 return tdep->arm_syscall_record (reg_cache, svc_number);
13182 else
13184 gdb_printf (gdb_stderr, _("no syscall record support\n"));
13185 return -1;
13188 else if (bits_24_25 == 0x02)
13190 if (op)
13192 if ((coproc & 0x0e) == 0x0a)
13194 /* 8, 16, and 32-bit transfer */
13195 return arm_record_vdata_transfer_insn (arm_insn_r);
13197 else
13199 if (op1_ebit)
13201 /* MRC, MRC2 */
13202 uint32_t record_buf[1];
13204 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
13205 if (record_buf[0] == 15)
13206 record_buf[0] = ARM_PS_REGNUM;
13208 arm_insn_r->reg_rec_count = 1;
13209 REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count,
13210 record_buf);
13211 return 0;
13213 else
13215 /* MCR, MCR2 */
13216 return -1;
13220 else
13222 if ((coproc & 0x0e) == 0x0a)
13224 /* VFP data-processing instructions. */
13225 return arm_record_vfp_data_proc_insn (arm_insn_r);
13227 else
13229 /* CDP, CDP2 */
13230 return -1;
13234 else
13236 unsigned int op1 = bits (arm_insn_r->arm_insn, 20, 25);
13238 if (op1 == 5)
13240 if ((coproc & 0x0e) != 0x0a)
13242 /* MRRC, MRRC2 */
13243 return -1;
13246 else if (op1 == 4 || op1 == 5)
13248 if ((coproc & 0x0e) == 0x0a)
13250 /* 64-bit transfers between ARM core and extension */
13251 return -1;
13253 else if (op1 == 4)
13255 /* MCRR, MCRR2 */
13256 return -1;
13259 else if (op1 == 0 || op1 == 1)
13261 /* UNDEFINED */
13262 return -1;
13264 else
13266 if ((coproc & 0x0e) == 0x0a)
13268 /* Extension register load/store */
13270 else
13272 /* STC, STC2, LDC, LDC2 */
13274 return -1;
13278 return -1;
13281 /* Handling opcode 000 insns. */
13283 static int
13284 thumb_record_shift_add_sub (arm_insn_decode_record *thumb_insn_r)
13286 uint32_t record_buf[8];
13287 uint32_t reg_src1 = 0;
13289 reg_src1 = bits (thumb_insn_r->arm_insn, 0, 2);
13291 record_buf[0] = ARM_PS_REGNUM;
13292 record_buf[1] = reg_src1;
13293 thumb_insn_r->reg_rec_count = 2;
13295 REG_ALLOC (thumb_insn_r->arm_regs, thumb_insn_r->reg_rec_count, record_buf);
13297 return 0;
13301 /* Handling opcode 001 insns. */
13303 static int
13304 thumb_record_add_sub_cmp_mov (arm_insn_decode_record *thumb_insn_r)
13306 uint32_t record_buf[8];
13307 uint32_t reg_src1 = 0;
13309 reg_src1 = bits (thumb_insn_r->arm_insn, 8, 10);
13311 record_buf[0] = ARM_PS_REGNUM;
13312 record_buf[1] = reg_src1;
13313 thumb_insn_r->reg_rec_count = 2;
13315 REG_ALLOC (thumb_insn_r->arm_regs, thumb_insn_r->reg_rec_count, record_buf);
13317 return 0;
13320 /* Handling opcode 010 insns. */
13322 static int
13323 thumb_record_ld_st_reg_offset (arm_insn_decode_record *thumb_insn_r)
13325 struct regcache *reg_cache = thumb_insn_r->regcache;
13326 uint32_t record_buf[8], record_buf_mem[8];
13328 uint32_t reg_src1 = 0, reg_src2 = 0;
13329 uint32_t opcode1 = 0, opcode2 = 0, opcode3 = 0;
13331 ULONGEST u_regval[2] = {0};
13333 opcode1 = bits (thumb_insn_r->arm_insn, 10, 12);
13335 if (bit (thumb_insn_r->arm_insn, 12))
13337 /* Handle load/store register offset. */
13338 uint32_t opB = bits (thumb_insn_r->arm_insn, 9, 11);
13340 if (in_inclusive_range (opB, 4U, 7U))
13342 /* LDR(2), LDRB(2) , LDRH(2), LDRSB, LDRSH. */
13343 reg_src1 = bits (thumb_insn_r->arm_insn,0, 2);
13344 record_buf[0] = reg_src1;
13345 thumb_insn_r->reg_rec_count = 1;
13347 else if (in_inclusive_range (opB, 0U, 2U))
13349 /* STR(2), STRB(2), STRH(2) . */
13350 reg_src1 = bits (thumb_insn_r->arm_insn, 3, 5);
13351 reg_src2 = bits (thumb_insn_r->arm_insn, 6, 8);
13352 regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval[0]);
13353 regcache_raw_read_unsigned (reg_cache, reg_src2, &u_regval[1]);
13354 if (0 == opB)
13355 record_buf_mem[0] = 4; /* STR (2). */
13356 else if (2 == opB)
13357 record_buf_mem[0] = 1; /* STRB (2). */
13358 else if (1 == opB)
13359 record_buf_mem[0] = 2; /* STRH (2). */
13360 record_buf_mem[1] = u_regval[0] + u_regval[1];
13361 thumb_insn_r->mem_rec_count = 1;
13364 else if (bit (thumb_insn_r->arm_insn, 11))
13366 /* Handle load from literal pool. */
13367 /* LDR(3). */
13368 reg_src1 = bits (thumb_insn_r->arm_insn, 8, 10);
13369 record_buf[0] = reg_src1;
13370 thumb_insn_r->reg_rec_count = 1;
13372 else if (opcode1)
13374 /* Special data instructions and branch and exchange */
13375 opcode2 = bits (thumb_insn_r->arm_insn, 8, 9);
13376 opcode3 = bits (thumb_insn_r->arm_insn, 0, 2);
13377 if ((3 == opcode2) && (!opcode3))
13379 /* Branch with exchange. */
13380 record_buf[0] = ARM_PS_REGNUM;
13381 thumb_insn_r->reg_rec_count = 1;
13383 else
13385 /* Format 8; special data processing insns. */
13386 record_buf[0] = ARM_PS_REGNUM;
13387 record_buf[1] = (bit (thumb_insn_r->arm_insn, 7) << 3
13388 | bits (thumb_insn_r->arm_insn, 0, 2));
13389 thumb_insn_r->reg_rec_count = 2;
13392 else
13394 /* Format 5; data processing insns. */
13395 reg_src1 = bits (thumb_insn_r->arm_insn, 0, 2);
13396 if (bit (thumb_insn_r->arm_insn, 7))
13398 reg_src1 = reg_src1 + 8;
13400 record_buf[0] = ARM_PS_REGNUM;
13401 record_buf[1] = reg_src1;
13402 thumb_insn_r->reg_rec_count = 2;
13405 REG_ALLOC (thumb_insn_r->arm_regs, thumb_insn_r->reg_rec_count, record_buf);
13406 MEM_ALLOC (thumb_insn_r->arm_mems, thumb_insn_r->mem_rec_count,
13407 record_buf_mem);
13409 return 0;
13412 /* Handling opcode 001 insns. */
13414 static int
13415 thumb_record_ld_st_imm_offset (arm_insn_decode_record *thumb_insn_r)
13417 struct regcache *reg_cache = thumb_insn_r->regcache;
13418 uint32_t record_buf[8], record_buf_mem[8];
13420 uint32_t reg_src1 = 0;
13421 uint32_t opcode = 0, immed_5 = 0;
13423 ULONGEST u_regval = 0;
13425 opcode = bits (thumb_insn_r->arm_insn, 11, 12);
13427 if (opcode)
13429 /* LDR(1). */
13430 reg_src1 = bits (thumb_insn_r->arm_insn, 0, 2);
13431 record_buf[0] = reg_src1;
13432 thumb_insn_r->reg_rec_count = 1;
13434 else
13436 /* STR(1). */
13437 reg_src1 = bits (thumb_insn_r->arm_insn, 3, 5);
13438 immed_5 = bits (thumb_insn_r->arm_insn, 6, 10);
13439 regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval);
13440 record_buf_mem[0] = 4;
13441 record_buf_mem[1] = u_regval + (immed_5 * 4);
13442 thumb_insn_r->mem_rec_count = 1;
13445 REG_ALLOC (thumb_insn_r->arm_regs, thumb_insn_r->reg_rec_count, record_buf);
13446 MEM_ALLOC (thumb_insn_r->arm_mems, thumb_insn_r->mem_rec_count,
13447 record_buf_mem);
13449 return 0;
13452 /* Handling opcode 100 insns. */
13454 static int
13455 thumb_record_ld_st_stack (arm_insn_decode_record *thumb_insn_r)
13457 struct regcache *reg_cache = thumb_insn_r->regcache;
13458 uint32_t record_buf[8], record_buf_mem[8];
13460 uint32_t reg_src1 = 0;
13461 uint32_t opcode = 0, immed_8 = 0, immed_5 = 0;
13463 ULONGEST u_regval = 0;
13465 opcode = bits (thumb_insn_r->arm_insn, 11, 12);
13467 if (3 == opcode)
13469 /* LDR(4). */
13470 reg_src1 = bits (thumb_insn_r->arm_insn, 8, 10);
13471 record_buf[0] = reg_src1;
13472 thumb_insn_r->reg_rec_count = 1;
13474 else if (1 == opcode)
13476 /* LDRH(1). */
13477 reg_src1 = bits (thumb_insn_r->arm_insn, 0, 2);
13478 record_buf[0] = reg_src1;
13479 thumb_insn_r->reg_rec_count = 1;
13481 else if (2 == opcode)
13483 /* STR(3). */
13484 immed_8 = bits (thumb_insn_r->arm_insn, 0, 7);
13485 regcache_raw_read_unsigned (reg_cache, ARM_SP_REGNUM, &u_regval);
13486 record_buf_mem[0] = 4;
13487 record_buf_mem[1] = u_regval + (immed_8 * 4);
13488 thumb_insn_r->mem_rec_count = 1;
13490 else if (0 == opcode)
13492 /* STRH(1). */
13493 immed_5 = bits (thumb_insn_r->arm_insn, 6, 10);
13494 reg_src1 = bits (thumb_insn_r->arm_insn, 3, 5);
13495 regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval);
13496 record_buf_mem[0] = 2;
13497 record_buf_mem[1] = u_regval + (immed_5 * 2);
13498 thumb_insn_r->mem_rec_count = 1;
13501 REG_ALLOC (thumb_insn_r->arm_regs, thumb_insn_r->reg_rec_count, record_buf);
13502 MEM_ALLOC (thumb_insn_r->arm_mems, thumb_insn_r->mem_rec_count,
13503 record_buf_mem);
13505 return 0;
13508 /* Handling opcode 101 insns. */
13510 static int
13511 thumb_record_misc (arm_insn_decode_record *thumb_insn_r)
13513 struct regcache *reg_cache = thumb_insn_r->regcache;
13515 uint32_t opcode = 0;
13516 uint32_t register_bits = 0, register_count = 0;
13517 uint32_t index = 0, start_address = 0;
13518 uint32_t record_buf[24], record_buf_mem[48];
13519 uint32_t reg_src1;
13521 ULONGEST u_regval = 0;
13523 opcode = bits (thumb_insn_r->arm_insn, 11, 12);
13525 if (opcode == 0 || opcode == 1)
13527 /* ADR and ADD (SP plus immediate) */
13529 reg_src1 = bits (thumb_insn_r->arm_insn, 8, 10);
13530 record_buf[0] = reg_src1;
13531 thumb_insn_r->reg_rec_count = 1;
13533 else
13535 /* Miscellaneous 16-bit instructions */
13536 uint32_t opcode2 = bits (thumb_insn_r->arm_insn, 8, 11);
13538 switch (opcode2)
13540 case 6:
13541 /* SETEND and CPS */
13542 break;
13543 case 0:
13544 /* ADD/SUB (SP plus immediate) */
13545 reg_src1 = bits (thumb_insn_r->arm_insn, 8, 10);
13546 record_buf[0] = ARM_SP_REGNUM;
13547 thumb_insn_r->reg_rec_count = 1;
13548 break;
13549 case 1: /* fall through */
13550 case 3: /* fall through */
13551 case 9: /* fall through */
13552 case 11:
13553 /* CBNZ, CBZ */
13554 break;
13555 case 2:
13556 /* SXTH, SXTB, UXTH, UXTB */
13557 record_buf[0] = bits (thumb_insn_r->arm_insn, 0, 2);
13558 thumb_insn_r->reg_rec_count = 1;
13559 break;
13560 case 5:
13561 /* PUSH with lr. */
13562 register_count++;
13563 [[fallthrough]];
13564 case 4:
13565 /* PUSH without lr. */
13566 register_bits = bits (thumb_insn_r->arm_insn, 0, 7);
13567 regcache_raw_read_unsigned (reg_cache, ARM_SP_REGNUM, &u_regval);
13568 while (register_bits)
13570 if (register_bits & 0x00000001)
13571 register_count++;
13572 register_bits = register_bits >> 1;
13574 start_address = u_regval - (4 * register_count);
13575 thumb_insn_r->mem_rec_count = register_count;
13576 while (register_count)
13578 record_buf_mem[(register_count * 2) - 1] = start_address;
13579 record_buf_mem[(register_count * 2) - 2] = 4;
13580 start_address = start_address + 4;
13581 register_count--;
13583 record_buf[0] = ARM_SP_REGNUM;
13584 thumb_insn_r->reg_rec_count = 1;
13585 break;
13586 case 10:
13587 /* REV, REV16, REVSH */
13588 record_buf[0] = bits (thumb_insn_r->arm_insn, 0, 2);
13589 thumb_insn_r->reg_rec_count = 1;
13590 break;
13591 case 12: /* fall through */
13592 case 13:
13593 /* POP. */
13594 register_bits = bits (thumb_insn_r->arm_insn, 0, 7);
13595 while (register_bits)
13597 if (register_bits & 0x00000001)
13598 record_buf[index++] = register_count;
13599 register_bits = register_bits >> 1;
13600 register_count++;
13602 record_buf[index++] = ARM_PS_REGNUM;
13603 record_buf[index++] = ARM_SP_REGNUM;
13604 thumb_insn_r->reg_rec_count = index;
13605 break;
13606 case 0xe:
13607 /* BKPT insn. */
13608 /* Handle enhanced software breakpoint insn, BKPT. */
13609 /* CPSR is changed to be executed in ARM state, disabling normal
13610 interrupts, entering abort mode. */
13611 /* According to high vector configuration PC is set. */
13612 /* User hits breakpoint and type reverse, in that case, we need to go back with
13613 previous CPSR and Program Counter. */
13614 record_buf[0] = ARM_PS_REGNUM;
13615 record_buf[1] = ARM_LR_REGNUM;
13616 thumb_insn_r->reg_rec_count = 2;
13617 /* We need to save SPSR value, which is not yet done. */
13618 gdb_printf (gdb_stderr,
13619 _("Process record does not support instruction "
13620 "0x%0x at address %s.\n"),
13621 thumb_insn_r->arm_insn,
13622 paddress (thumb_insn_r->gdbarch,
13623 thumb_insn_r->this_addr));
13624 return -1;
13626 case 0xf:
13627 /* If-Then, and hints */
13628 break;
13629 default:
13630 return -1;
13634 REG_ALLOC (thumb_insn_r->arm_regs, thumb_insn_r->reg_rec_count, record_buf);
13635 MEM_ALLOC (thumb_insn_r->arm_mems, thumb_insn_r->mem_rec_count,
13636 record_buf_mem);
13638 return 0;
13641 /* Handling opcode 110 insns. */
13643 static int
13644 thumb_record_ldm_stm_swi (arm_insn_decode_record *thumb_insn_r)
13646 arm_gdbarch_tdep *tdep
13647 = gdbarch_tdep<arm_gdbarch_tdep> (thumb_insn_r->gdbarch);
13648 struct regcache *reg_cache = thumb_insn_r->regcache;
13650 uint32_t ret = 0; /* function return value: -1:record failure ; 0:success */
13651 uint32_t reg_src1 = 0;
13652 uint32_t opcode1 = 0, opcode2 = 0, register_bits = 0, register_count = 0;
13653 uint32_t index = 0, start_address = 0;
13654 uint32_t record_buf[24], record_buf_mem[48];
13656 ULONGEST u_regval = 0;
13658 opcode1 = bits (thumb_insn_r->arm_insn, 8, 12);
13659 opcode2 = bits (thumb_insn_r->arm_insn, 11, 12);
13661 if (1 == opcode2)
13664 /* LDMIA. */
13665 register_bits = bits (thumb_insn_r->arm_insn, 0, 7);
13666 /* Get Rn. */
13667 reg_src1 = bits (thumb_insn_r->arm_insn, 8, 10);
13668 while (register_bits)
13670 if (register_bits & 0x00000001)
13671 record_buf[index++] = register_count;
13672 register_bits = register_bits >> 1;
13673 register_count++;
13675 record_buf[index++] = reg_src1;
13676 thumb_insn_r->reg_rec_count = index;
13678 else if (0 == opcode2)
13680 /* It handles both STMIA. */
13681 register_bits = bits (thumb_insn_r->arm_insn, 0, 7);
13682 /* Get Rn. */
13683 reg_src1 = bits (thumb_insn_r->arm_insn, 8, 10);
13684 regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval);
13685 while (register_bits)
13687 if (register_bits & 0x00000001)
13688 register_count++;
13689 register_bits = register_bits >> 1;
13691 start_address = u_regval;
13692 thumb_insn_r->mem_rec_count = register_count;
13693 while (register_count)
13695 record_buf_mem[(register_count * 2) - 1] = start_address;
13696 record_buf_mem[(register_count * 2) - 2] = 4;
13697 start_address = start_address + 4;
13698 register_count--;
13701 else if (0x1F == opcode1)
13703 /* Handle arm syscall insn. */
13704 if (tdep->arm_syscall_record != NULL)
13706 regcache_raw_read_unsigned (reg_cache, 7, &u_regval);
13707 ret = tdep->arm_syscall_record (reg_cache, u_regval);
13709 else
13711 gdb_printf (gdb_stderr, _("no syscall record support\n"));
13712 return -1;
13716 /* B (1), conditional branch is automatically taken care in process_record,
13717 as PC is saved there. */
13719 REG_ALLOC (thumb_insn_r->arm_regs, thumb_insn_r->reg_rec_count, record_buf);
13720 MEM_ALLOC (thumb_insn_r->arm_mems, thumb_insn_r->mem_rec_count,
13721 record_buf_mem);
13723 return ret;
13726 /* Handling opcode 111 insns. */
13728 static int
13729 thumb_record_branch (arm_insn_decode_record *thumb_insn_r)
13731 uint32_t record_buf[8];
13732 uint32_t bits_h = 0;
13734 bits_h = bits (thumb_insn_r->arm_insn, 11, 12);
13736 if (2 == bits_h || 3 == bits_h)
13738 /* BL */
13739 record_buf[0] = ARM_LR_REGNUM;
13740 thumb_insn_r->reg_rec_count = 1;
13742 else if (1 == bits_h)
13744 /* BLX(1). */
13745 record_buf[0] = ARM_PS_REGNUM;
13746 record_buf[1] = ARM_LR_REGNUM;
13747 thumb_insn_r->reg_rec_count = 2;
13750 /* B(2) is automatically taken care in process_record, as PC is
13751 saved there. */
13753 REG_ALLOC (thumb_insn_r->arm_regs, thumb_insn_r->reg_rec_count, record_buf);
13755 return 0;
13758 /* Handler for thumb2 load/store multiple instructions. */
13760 static int
13761 thumb2_record_ld_st_multiple (arm_insn_decode_record *thumb2_insn_r)
13763 struct regcache *reg_cache = thumb2_insn_r->regcache;
13765 uint32_t reg_rn, op;
13766 uint32_t register_bits = 0, register_count = 0;
13767 uint32_t index = 0, start_address = 0;
13768 uint32_t record_buf[24], record_buf_mem[48];
13770 ULONGEST u_regval = 0;
13772 reg_rn = bits (thumb2_insn_r->arm_insn, 16, 19);
13773 op = bits (thumb2_insn_r->arm_insn, 23, 24);
13775 if (0 == op || 3 == op)
13777 if (bit (thumb2_insn_r->arm_insn, INSN_S_L_BIT_NUM))
13779 /* Handle RFE instruction. */
13780 record_buf[0] = ARM_PS_REGNUM;
13781 thumb2_insn_r->reg_rec_count = 1;
13783 else
13785 /* Handle SRS instruction after reading banked SP. */
13786 return arm_record_unsupported_insn (thumb2_insn_r);
13789 else if (1 == op || 2 == op)
13791 if (bit (thumb2_insn_r->arm_insn, INSN_S_L_BIT_NUM))
13793 /* Handle LDM/LDMIA/LDMFD and LDMDB/LDMEA instructions. */
13794 register_bits = bits (thumb2_insn_r->arm_insn, 0, 15);
13795 while (register_bits)
13797 if (register_bits & 0x00000001)
13798 record_buf[index++] = register_count;
13800 register_count++;
13801 register_bits = register_bits >> 1;
13803 record_buf[index++] = reg_rn;
13804 record_buf[index++] = ARM_PS_REGNUM;
13805 thumb2_insn_r->reg_rec_count = index;
13807 else
13809 /* Handle STM/STMIA/STMEA and STMDB/STMFD. */
13810 register_bits = bits (thumb2_insn_r->arm_insn, 0, 15);
13811 regcache_raw_read_unsigned (reg_cache, reg_rn, &u_regval);
13812 while (register_bits)
13814 if (register_bits & 0x00000001)
13815 register_count++;
13817 register_bits = register_bits >> 1;
13820 if (1 == op)
13822 /* Start address calculation for LDMDB/LDMEA. */
13823 start_address = u_regval;
13825 else if (2 == op)
13827 /* Start address calculation for LDMDB/LDMEA. */
13828 start_address = u_regval - register_count * 4;
13831 thumb2_insn_r->mem_rec_count = register_count;
13832 while (register_count)
13834 record_buf_mem[register_count * 2 - 1] = start_address;
13835 record_buf_mem[register_count * 2 - 2] = 4;
13836 start_address = start_address + 4;
13837 register_count--;
13839 record_buf[0] = reg_rn;
13840 record_buf[1] = ARM_PS_REGNUM;
13841 thumb2_insn_r->reg_rec_count = 2;
13845 MEM_ALLOC (thumb2_insn_r->arm_mems, thumb2_insn_r->mem_rec_count,
13846 record_buf_mem);
13847 REG_ALLOC (thumb2_insn_r->arm_regs, thumb2_insn_r->reg_rec_count,
13848 record_buf);
13849 return ARM_RECORD_SUCCESS;
13852 /* Handler for thumb2 load/store (dual/exclusive) and table branch
13853 instructions. */
13855 static int
13856 thumb2_record_ld_st_dual_ex_tbb (arm_insn_decode_record *thumb2_insn_r)
13858 struct regcache *reg_cache = thumb2_insn_r->regcache;
13860 uint32_t reg_rd, reg_rn, offset_imm;
13861 uint32_t reg_dest1, reg_dest2;
13862 uint32_t address, offset_addr;
13863 uint32_t record_buf[8], record_buf_mem[8];
13864 uint32_t op1, op2, op3;
13866 ULONGEST u_regval[2];
13868 op1 = bits (thumb2_insn_r->arm_insn, 23, 24);
13869 op2 = bits (thumb2_insn_r->arm_insn, 20, 21);
13870 op3 = bits (thumb2_insn_r->arm_insn, 4, 7);
13872 if (bit (thumb2_insn_r->arm_insn, INSN_S_L_BIT_NUM))
13874 if(!(1 == op1 && 1 == op2 && (0 == op3 || 1 == op3)))
13876 reg_dest1 = bits (thumb2_insn_r->arm_insn, 12, 15);
13877 record_buf[0] = reg_dest1;
13878 record_buf[1] = ARM_PS_REGNUM;
13879 thumb2_insn_r->reg_rec_count = 2;
13882 if (3 == op2 || (op1 & 2) || (1 == op1 && 1 == op2 && 7 == op3))
13884 reg_dest2 = bits (thumb2_insn_r->arm_insn, 8, 11);
13885 record_buf[2] = reg_dest2;
13886 thumb2_insn_r->reg_rec_count = 3;
13889 else
13891 reg_rn = bits (thumb2_insn_r->arm_insn, 16, 19);
13892 regcache_raw_read_unsigned (reg_cache, reg_rn, &u_regval[0]);
13894 if (0 == op1 && 0 == op2)
13896 /* Handle STREX. */
13897 offset_imm = bits (thumb2_insn_r->arm_insn, 0, 7);
13898 address = u_regval[0] + (offset_imm * 4);
13899 record_buf_mem[0] = 4;
13900 record_buf_mem[1] = address;
13901 thumb2_insn_r->mem_rec_count = 1;
13902 reg_rd = bits (thumb2_insn_r->arm_insn, 0, 3);
13903 record_buf[0] = reg_rd;
13904 thumb2_insn_r->reg_rec_count = 1;
13906 else if (1 == op1 && 0 == op2)
13908 reg_rd = bits (thumb2_insn_r->arm_insn, 0, 3);
13909 record_buf[0] = reg_rd;
13910 thumb2_insn_r->reg_rec_count = 1;
13911 address = u_regval[0];
13912 record_buf_mem[1] = address;
13914 if (4 == op3)
13916 /* Handle STREXB. */
13917 record_buf_mem[0] = 1;
13918 thumb2_insn_r->mem_rec_count = 1;
13920 else if (5 == op3)
13922 /* Handle STREXH. */
13923 record_buf_mem[0] = 2 ;
13924 thumb2_insn_r->mem_rec_count = 1;
13926 else if (7 == op3)
13928 /* Handle STREXD. */
13929 address = u_regval[0];
13930 record_buf_mem[0] = 4;
13931 record_buf_mem[2] = 4;
13932 record_buf_mem[3] = address + 4;
13933 thumb2_insn_r->mem_rec_count = 2;
13936 else
13938 offset_imm = bits (thumb2_insn_r->arm_insn, 0, 7);
13940 if (bit (thumb2_insn_r->arm_insn, 24))
13942 if (bit (thumb2_insn_r->arm_insn, 23))
13943 offset_addr = u_regval[0] + (offset_imm * 4);
13944 else
13945 offset_addr = u_regval[0] - (offset_imm * 4);
13947 address = offset_addr;
13949 else
13950 address = u_regval[0];
13952 record_buf_mem[0] = 4;
13953 record_buf_mem[1] = address;
13954 record_buf_mem[2] = 4;
13955 record_buf_mem[3] = address + 4;
13956 thumb2_insn_r->mem_rec_count = 2;
13957 record_buf[0] = reg_rn;
13958 thumb2_insn_r->reg_rec_count = 1;
13962 REG_ALLOC (thumb2_insn_r->arm_regs, thumb2_insn_r->reg_rec_count,
13963 record_buf);
13964 MEM_ALLOC (thumb2_insn_r->arm_mems, thumb2_insn_r->mem_rec_count,
13965 record_buf_mem);
13966 return ARM_RECORD_SUCCESS;
13969 /* Handler for thumb2 data processing (shift register and modified immediate)
13970 instructions. */
13972 static int
13973 thumb2_record_data_proc_sreg_mimm (arm_insn_decode_record *thumb2_insn_r)
13975 uint32_t reg_rd, op;
13976 uint32_t record_buf[8];
13978 op = bits (thumb2_insn_r->arm_insn, 21, 24);
13979 reg_rd = bits (thumb2_insn_r->arm_insn, 8, 11);
13981 if ((0 == op || 4 == op || 8 == op || 13 == op) && 15 == reg_rd)
13983 record_buf[0] = ARM_PS_REGNUM;
13984 thumb2_insn_r->reg_rec_count = 1;
13986 else
13988 record_buf[0] = reg_rd;
13989 record_buf[1] = ARM_PS_REGNUM;
13990 thumb2_insn_r->reg_rec_count = 2;
13993 REG_ALLOC (thumb2_insn_r->arm_regs, thumb2_insn_r->reg_rec_count,
13994 record_buf);
13995 return ARM_RECORD_SUCCESS;
13998 /* Generic handler for thumb2 instructions which effect destination and PS
13999 registers. */
14001 static int
14002 thumb2_record_ps_dest_generic (arm_insn_decode_record *thumb2_insn_r)
14004 uint32_t reg_rd;
14005 uint32_t record_buf[8];
14007 reg_rd = bits (thumb2_insn_r->arm_insn, 8, 11);
14009 record_buf[0] = reg_rd;
14010 record_buf[1] = ARM_PS_REGNUM;
14011 thumb2_insn_r->reg_rec_count = 2;
14013 REG_ALLOC (thumb2_insn_r->arm_regs, thumb2_insn_r->reg_rec_count,
14014 record_buf);
14015 return ARM_RECORD_SUCCESS;
14018 /* Handler for thumb2 branch and miscellaneous control instructions. */
14020 static int
14021 thumb2_record_branch_misc_cntrl (arm_insn_decode_record *thumb2_insn_r)
14023 uint32_t op, op1, op2;
14024 uint32_t record_buf[8];
14026 op = bits (thumb2_insn_r->arm_insn, 20, 26);
14027 op1 = bits (thumb2_insn_r->arm_insn, 12, 14);
14028 op2 = bits (thumb2_insn_r->arm_insn, 8, 11);
14030 /* Handle MSR insn. */
14031 if (!(op1 & 0x2) && 0x38 == op)
14033 if (!(op2 & 0x3))
14035 /* CPSR is going to be changed. */
14036 record_buf[0] = ARM_PS_REGNUM;
14037 thumb2_insn_r->reg_rec_count = 1;
14039 else
14041 arm_record_unsupported_insn(thumb2_insn_r);
14042 return -1;
14045 else if (4 == (op1 & 0x5) || 5 == (op1 & 0x5))
14047 /* BLX. */
14048 record_buf[0] = ARM_PS_REGNUM;
14049 record_buf[1] = ARM_LR_REGNUM;
14050 thumb2_insn_r->reg_rec_count = 2;
14053 REG_ALLOC (thumb2_insn_r->arm_regs, thumb2_insn_r->reg_rec_count,
14054 record_buf);
14055 return ARM_RECORD_SUCCESS;
14058 /* Handler for thumb2 store single data item instructions. */
14060 static int
14061 thumb2_record_str_single_data (arm_insn_decode_record *thumb2_insn_r)
14063 struct regcache *reg_cache = thumb2_insn_r->regcache;
14065 uint32_t reg_rn, reg_rm, offset_imm, shift_imm;
14066 uint32_t address, offset_addr;
14067 uint32_t record_buf[8], record_buf_mem[8];
14068 uint32_t op1, op2;
14070 ULONGEST u_regval[2];
14072 op1 = bits (thumb2_insn_r->arm_insn, 21, 23);
14073 op2 = bits (thumb2_insn_r->arm_insn, 6, 11);
14074 reg_rn = bits (thumb2_insn_r->arm_insn, 16, 19);
14075 regcache_raw_read_unsigned (reg_cache, reg_rn, &u_regval[0]);
14077 if (bit (thumb2_insn_r->arm_insn, 23))
14079 /* T2 encoding. */
14080 offset_imm = bits (thumb2_insn_r->arm_insn, 0, 11);
14081 offset_addr = u_regval[0] + offset_imm;
14082 address = offset_addr;
14084 else
14086 /* T3 encoding. */
14087 if ((0 == op1 || 1 == op1 || 2 == op1) && !(op2 & 0x20))
14089 /* Handle STRB (register). */
14090 reg_rm = bits (thumb2_insn_r->arm_insn, 0, 3);
14091 regcache_raw_read_unsigned (reg_cache, reg_rm, &u_regval[1]);
14092 shift_imm = bits (thumb2_insn_r->arm_insn, 4, 5);
14093 offset_addr = u_regval[1] << shift_imm;
14094 address = u_regval[0] + offset_addr;
14096 else
14098 offset_imm = bits (thumb2_insn_r->arm_insn, 0, 7);
14099 if (bit (thumb2_insn_r->arm_insn, 10))
14101 if (bit (thumb2_insn_r->arm_insn, 9))
14102 offset_addr = u_regval[0] + offset_imm;
14103 else
14104 offset_addr = u_regval[0] - offset_imm;
14106 address = offset_addr;
14108 else
14109 address = u_regval[0];
14113 switch (op1)
14115 /* Store byte instructions. */
14116 case 4:
14117 case 0:
14118 record_buf_mem[0] = 1;
14119 break;
14120 /* Store half word instructions. */
14121 case 1:
14122 case 5:
14123 record_buf_mem[0] = 2;
14124 break;
14125 /* Store word instructions. */
14126 case 2:
14127 case 6:
14128 record_buf_mem[0] = 4;
14129 break;
14131 default:
14132 gdb_assert_not_reached ("no decoding pattern found");
14133 break;
14136 record_buf_mem[1] = address;
14137 thumb2_insn_r->mem_rec_count = 1;
14138 record_buf[0] = reg_rn;
14139 thumb2_insn_r->reg_rec_count = 1;
14141 REG_ALLOC (thumb2_insn_r->arm_regs, thumb2_insn_r->reg_rec_count,
14142 record_buf);
14143 MEM_ALLOC (thumb2_insn_r->arm_mems, thumb2_insn_r->mem_rec_count,
14144 record_buf_mem);
14145 return ARM_RECORD_SUCCESS;
14148 /* Handler for thumb2 load memory hints instructions. */
14150 static int
14151 thumb2_record_ld_mem_hints (arm_insn_decode_record *thumb2_insn_r)
14153 uint32_t record_buf[8];
14154 uint32_t reg_rt, reg_rn;
14156 reg_rt = bits (thumb2_insn_r->arm_insn, 12, 15);
14157 reg_rn = bits (thumb2_insn_r->arm_insn, 16, 19);
14159 if (ARM_PC_REGNUM != reg_rt)
14161 record_buf[0] = reg_rt;
14162 record_buf[1] = reg_rn;
14163 record_buf[2] = ARM_PS_REGNUM;
14164 thumb2_insn_r->reg_rec_count = 3;
14166 REG_ALLOC (thumb2_insn_r->arm_regs, thumb2_insn_r->reg_rec_count,
14167 record_buf);
14168 return ARM_RECORD_SUCCESS;
14171 return ARM_RECORD_FAILURE;
14174 /* Handler for thumb2 load word instructions. */
14176 static int
14177 thumb2_record_ld_word (arm_insn_decode_record *thumb2_insn_r)
14179 uint32_t record_buf[8];
14181 record_buf[0] = bits (thumb2_insn_r->arm_insn, 12, 15);
14182 record_buf[1] = ARM_PS_REGNUM;
14183 thumb2_insn_r->reg_rec_count = 2;
14185 if ((thumb2_insn_r->arm_insn & 0xfff00900) == 0xf8500900)
14187 /* Detected LDR(immediate), T4, with write-back bit set. Record Rn
14188 update. */
14189 record_buf[2] = bits (thumb2_insn_r->arm_insn, 16, 19);
14190 thumb2_insn_r->reg_rec_count++;
14193 REG_ALLOC (thumb2_insn_r->arm_regs, thumb2_insn_r->reg_rec_count,
14194 record_buf);
14195 return ARM_RECORD_SUCCESS;
14198 /* Handler for thumb2 long multiply, long multiply accumulate, and
14199 divide instructions. */
14201 static int
14202 thumb2_record_lmul_lmla_div (arm_insn_decode_record *thumb2_insn_r)
14204 uint32_t opcode1 = 0, opcode2 = 0;
14205 uint32_t record_buf[8];
14207 opcode1 = bits (thumb2_insn_r->arm_insn, 20, 22);
14208 opcode2 = bits (thumb2_insn_r->arm_insn, 4, 7);
14210 if (0 == opcode1 || 2 == opcode1 || (opcode1 >= 4 && opcode1 <= 6))
14212 /* Handle SMULL, UMULL, SMULAL. */
14213 /* Handle SMLAL(S), SMULL(S), UMLAL(S), UMULL(S). */
14214 record_buf[0] = bits (thumb2_insn_r->arm_insn, 16, 19);
14215 record_buf[1] = bits (thumb2_insn_r->arm_insn, 12, 15);
14216 record_buf[2] = ARM_PS_REGNUM;
14217 thumb2_insn_r->reg_rec_count = 3;
14219 else if (1 == opcode1 || 3 == opcode2)
14221 /* Handle SDIV and UDIV. */
14222 record_buf[0] = bits (thumb2_insn_r->arm_insn, 16, 19);
14223 record_buf[1] = bits (thumb2_insn_r->arm_insn, 12, 15);
14224 record_buf[2] = ARM_PS_REGNUM;
14225 thumb2_insn_r->reg_rec_count = 3;
14227 else
14228 return ARM_RECORD_FAILURE;
14230 REG_ALLOC (thumb2_insn_r->arm_regs, thumb2_insn_r->reg_rec_count,
14231 record_buf);
14232 return ARM_RECORD_SUCCESS;
14235 /* Record handler for thumb32 coprocessor instructions. */
14237 static int
14238 thumb2_record_coproc_insn (arm_insn_decode_record *thumb2_insn_r)
14240 if (bit (thumb2_insn_r->arm_insn, 25))
14241 return arm_record_coproc_data_proc (thumb2_insn_r);
14242 else
14243 return arm_record_asimd_vfp_coproc (thumb2_insn_r);
14246 /* Record handler for advance SIMD structure load/store instructions. */
14248 static int
14249 thumb2_record_asimd_struct_ld_st (arm_insn_decode_record *thumb2_insn_r)
14251 struct regcache *reg_cache = thumb2_insn_r->regcache;
14252 uint32_t l_bit, a_bit, b_bits;
14253 uint32_t record_buf[128], record_buf_mem[128];
14254 uint32_t reg_rn, reg_vd, address, f_elem;
14255 uint32_t index_r = 0, index_e = 0, bf_regs = 0, index_m = 0, loop_t = 0;
14256 uint8_t f_ebytes;
14258 l_bit = bit (thumb2_insn_r->arm_insn, 21);
14259 a_bit = bit (thumb2_insn_r->arm_insn, 23);
14260 b_bits = bits (thumb2_insn_r->arm_insn, 8, 11);
14261 reg_rn = bits (thumb2_insn_r->arm_insn, 16, 19);
14262 reg_vd = bits (thumb2_insn_r->arm_insn, 12, 15);
14263 reg_vd = (bit (thumb2_insn_r->arm_insn, 22) << 4) | reg_vd;
14264 f_ebytes = (1 << bits (thumb2_insn_r->arm_insn, 6, 7));
14265 f_elem = 8 / f_ebytes;
14267 if (!l_bit)
14269 ULONGEST u_regval = 0;
14270 regcache_raw_read_unsigned (reg_cache, reg_rn, &u_regval);
14271 address = u_regval;
14273 if (!a_bit)
14275 /* Handle VST1. */
14276 if (b_bits == 0x02 || b_bits == 0x0a || (b_bits & 0x0e) == 0x06)
14278 if (b_bits == 0x07)
14279 bf_regs = 1;
14280 else if (b_bits == 0x0a)
14281 bf_regs = 2;
14282 else if (b_bits == 0x06)
14283 bf_regs = 3;
14284 else if (b_bits == 0x02)
14285 bf_regs = 4;
14286 else
14287 bf_regs = 0;
14289 for (index_r = 0; index_r < bf_regs; index_r++)
14291 for (index_e = 0; index_e < f_elem; index_e++)
14293 record_buf_mem[index_m++] = f_ebytes;
14294 record_buf_mem[index_m++] = address;
14295 address = address + f_ebytes;
14296 thumb2_insn_r->mem_rec_count += 1;
14300 /* Handle VST2. */
14301 else if (b_bits == 0x03 || (b_bits & 0x0e) == 0x08)
14303 if (b_bits == 0x09 || b_bits == 0x08)
14304 bf_regs = 1;
14305 else if (b_bits == 0x03)
14306 bf_regs = 2;
14307 else
14308 bf_regs = 0;
14310 for (index_r = 0; index_r < bf_regs; index_r++)
14311 for (index_e = 0; index_e < f_elem; index_e++)
14313 for (loop_t = 0; loop_t < 2; loop_t++)
14315 record_buf_mem[index_m++] = f_ebytes;
14316 record_buf_mem[index_m++] = address + (loop_t * f_ebytes);
14317 thumb2_insn_r->mem_rec_count += 1;
14319 address = address + (2 * f_ebytes);
14322 /* Handle VST3. */
14323 else if ((b_bits & 0x0e) == 0x04)
14325 for (index_e = 0; index_e < f_elem; index_e++)
14327 for (loop_t = 0; loop_t < 3; loop_t++)
14329 record_buf_mem[index_m++] = f_ebytes;
14330 record_buf_mem[index_m++] = address + (loop_t * f_ebytes);
14331 thumb2_insn_r->mem_rec_count += 1;
14333 address = address + (3 * f_ebytes);
14336 /* Handle VST4. */
14337 else if (!(b_bits & 0x0e))
14339 for (index_e = 0; index_e < f_elem; index_e++)
14341 for (loop_t = 0; loop_t < 4; loop_t++)
14343 record_buf_mem[index_m++] = f_ebytes;
14344 record_buf_mem[index_m++] = address + (loop_t * f_ebytes);
14345 thumb2_insn_r->mem_rec_count += 1;
14347 address = address + (4 * f_ebytes);
14351 else
14353 uint8_t bft_size = bits (thumb2_insn_r->arm_insn, 10, 11);
14355 if (bft_size == 0x00)
14356 f_ebytes = 1;
14357 else if (bft_size == 0x01)
14358 f_ebytes = 2;
14359 else if (bft_size == 0x02)
14360 f_ebytes = 4;
14361 else
14362 f_ebytes = 0;
14364 /* Handle VST1. */
14365 if (!(b_bits & 0x0b) || b_bits == 0x08)
14366 thumb2_insn_r->mem_rec_count = 1;
14367 /* Handle VST2. */
14368 else if ((b_bits & 0x0b) == 0x01 || b_bits == 0x09)
14369 thumb2_insn_r->mem_rec_count = 2;
14370 /* Handle VST3. */
14371 else if ((b_bits & 0x0b) == 0x02 || b_bits == 0x0a)
14372 thumb2_insn_r->mem_rec_count = 3;
14373 /* Handle VST4. */
14374 else if ((b_bits & 0x0b) == 0x03 || b_bits == 0x0b)
14375 thumb2_insn_r->mem_rec_count = 4;
14377 for (index_m = 0; index_m < thumb2_insn_r->mem_rec_count; index_m++)
14379 record_buf_mem[index_m] = f_ebytes;
14380 record_buf_mem[index_m] = address + (index_m * f_ebytes);
14384 else
14386 if (!a_bit)
14388 /* Handle VLD1. */
14389 if (b_bits == 0x02 || b_bits == 0x0a || (b_bits & 0x0e) == 0x06)
14390 thumb2_insn_r->reg_rec_count = 1;
14391 /* Handle VLD2. */
14392 else if (b_bits == 0x03 || (b_bits & 0x0e) == 0x08)
14393 thumb2_insn_r->reg_rec_count = 2;
14394 /* Handle VLD3. */
14395 else if ((b_bits & 0x0e) == 0x04)
14396 thumb2_insn_r->reg_rec_count = 3;
14397 /* Handle VLD4. */
14398 else if (!(b_bits & 0x0e))
14399 thumb2_insn_r->reg_rec_count = 4;
14401 else
14403 /* Handle VLD1. */
14404 if (!(b_bits & 0x0b) || b_bits == 0x08 || b_bits == 0x0c)
14405 thumb2_insn_r->reg_rec_count = 1;
14406 /* Handle VLD2. */
14407 else if ((b_bits & 0x0b) == 0x01 || b_bits == 0x09 || b_bits == 0x0d)
14408 thumb2_insn_r->reg_rec_count = 2;
14409 /* Handle VLD3. */
14410 else if ((b_bits & 0x0b) == 0x02 || b_bits == 0x0a || b_bits == 0x0e)
14411 thumb2_insn_r->reg_rec_count = 3;
14412 /* Handle VLD4. */
14413 else if ((b_bits & 0x0b) == 0x03 || b_bits == 0x0b || b_bits == 0x0f)
14414 thumb2_insn_r->reg_rec_count = 4;
14416 for (index_r = 0; index_r < thumb2_insn_r->reg_rec_count; index_r++)
14417 record_buf[index_r] = reg_vd + ARM_D0_REGNUM + index_r;
14421 if (bits (thumb2_insn_r->arm_insn, 0, 3) != 15)
14423 record_buf[index_r] = reg_rn;
14424 thumb2_insn_r->reg_rec_count += 1;
14427 REG_ALLOC (thumb2_insn_r->arm_regs, thumb2_insn_r->reg_rec_count,
14428 record_buf);
14429 MEM_ALLOC (thumb2_insn_r->arm_mems, thumb2_insn_r->mem_rec_count,
14430 record_buf_mem);
14431 return 0;
14434 /* Decodes thumb2 instruction type and invokes its record handler. */
14436 static unsigned int
14437 thumb2_record_decode_insn_handler (arm_insn_decode_record *thumb2_insn_r)
14439 uint32_t op, op1, op2;
14441 op = bit (thumb2_insn_r->arm_insn, 15);
14442 op1 = bits (thumb2_insn_r->arm_insn, 27, 28);
14443 op2 = bits (thumb2_insn_r->arm_insn, 20, 26);
14445 if (op1 == 0x01)
14447 if (!(op2 & 0x64 ))
14449 /* Load/store multiple instruction. */
14450 return thumb2_record_ld_st_multiple (thumb2_insn_r);
14452 else if ((op2 & 0x64) == 0x4)
14454 /* Load/store (dual/exclusive) and table branch instruction. */
14455 return thumb2_record_ld_st_dual_ex_tbb (thumb2_insn_r);
14457 else if ((op2 & 0x60) == 0x20)
14459 /* Data-processing (shifted register). */
14460 return thumb2_record_data_proc_sreg_mimm (thumb2_insn_r);
14462 else if (op2 & 0x40)
14464 /* Co-processor instructions. */
14465 return thumb2_record_coproc_insn (thumb2_insn_r);
14468 else if (op1 == 0x02)
14470 if (op)
14472 /* Branches and miscellaneous control instructions. */
14473 return thumb2_record_branch_misc_cntrl (thumb2_insn_r);
14475 else if (op2 & 0x20)
14477 /* Data-processing (plain binary immediate) instruction. */
14478 return thumb2_record_ps_dest_generic (thumb2_insn_r);
14480 else
14482 /* Data-processing (modified immediate). */
14483 return thumb2_record_data_proc_sreg_mimm (thumb2_insn_r);
14486 else if (op1 == 0x03)
14488 if (!(op2 & 0x71 ))
14490 /* Store single data item. */
14491 return thumb2_record_str_single_data (thumb2_insn_r);
14493 else if (!((op2 & 0x71) ^ 0x10))
14495 /* Advanced SIMD or structure load/store instructions. */
14496 return thumb2_record_asimd_struct_ld_st (thumb2_insn_r);
14498 else if (!((op2 & 0x67) ^ 0x01))
14500 /* Load byte, memory hints instruction. */
14501 return thumb2_record_ld_mem_hints (thumb2_insn_r);
14503 else if (!((op2 & 0x67) ^ 0x03))
14505 /* Load halfword, memory hints instruction. */
14506 return thumb2_record_ld_mem_hints (thumb2_insn_r);
14508 else if (!((op2 & 0x67) ^ 0x05))
14510 /* Load word instruction. */
14511 return thumb2_record_ld_word (thumb2_insn_r);
14513 else if (!((op2 & 0x70) ^ 0x20))
14515 /* Data-processing (register) instruction. */
14516 return thumb2_record_ps_dest_generic (thumb2_insn_r);
14518 else if (!((op2 & 0x78) ^ 0x30))
14520 /* Multiply, multiply accumulate, abs diff instruction. */
14521 return thumb2_record_ps_dest_generic (thumb2_insn_r);
14523 else if (!((op2 & 0x78) ^ 0x38))
14525 /* Long multiply, long multiply accumulate, and divide. */
14526 return thumb2_record_lmul_lmla_div (thumb2_insn_r);
14528 else if (op2 & 0x40)
14530 /* Co-processor instructions. */
14531 return thumb2_record_coproc_insn (thumb2_insn_r);
14535 return -1;
14538 namespace {
14539 /* Abstract instruction reader. */
14541 class abstract_instruction_reader
14543 public:
14544 /* Read one instruction of size LEN from address MEMADDR and using
14545 BYTE_ORDER endianness. */
14547 virtual ULONGEST read (CORE_ADDR memaddr, const size_t len,
14548 enum bfd_endian byte_order) = 0;
14551 /* Instruction reader from real target. */
14553 class instruction_reader : public abstract_instruction_reader
14555 public:
14556 ULONGEST read (CORE_ADDR memaddr, const size_t len,
14557 enum bfd_endian byte_order) override
14559 return read_code_unsigned_integer (memaddr, len, byte_order);
14563 } // namespace
14565 typedef int (*sti_arm_hdl_fp_t) (arm_insn_decode_record*);
14567 /* Decode arm/thumb insn depending on condition cods and opcodes; and
14568 dispatch it. */
14570 static int
14571 decode_insn (abstract_instruction_reader &reader,
14572 arm_insn_decode_record *arm_record,
14573 record_type_t record_type, uint32_t insn_size)
14576 /* (Starting from numerical 0); bits 25, 26, 27 decodes type of arm
14577 instruction. */
14578 static const sti_arm_hdl_fp_t arm_handle_insn[8] =
14580 arm_record_data_proc_misc_ld_str, /* 000. */
14581 arm_record_data_proc_imm, /* 001. */
14582 arm_record_ld_st_imm_offset, /* 010. */
14583 arm_record_ld_st_reg_offset, /* 011. */
14584 arm_record_ld_st_multiple, /* 100. */
14585 arm_record_b_bl, /* 101. */
14586 arm_record_asimd_vfp_coproc, /* 110. */
14587 arm_record_coproc_data_proc /* 111. */
14590 /* (Starting from numerical 0); bits 13,14,15 decodes type of thumb
14591 instruction. */
14592 static const sti_arm_hdl_fp_t thumb_handle_insn[8] =
14594 thumb_record_shift_add_sub, /* 000. */
14595 thumb_record_add_sub_cmp_mov, /* 001. */
14596 thumb_record_ld_st_reg_offset, /* 010. */
14597 thumb_record_ld_st_imm_offset, /* 011. */
14598 thumb_record_ld_st_stack, /* 100. */
14599 thumb_record_misc, /* 101. */
14600 thumb_record_ldm_stm_swi, /* 110. */
14601 thumb_record_branch /* 111. */
14604 uint32_t ret = 0; /* return value: negative:failure 0:success. */
14605 uint32_t insn_id = 0;
14606 enum bfd_endian code_endian
14607 = gdbarch_byte_order_for_code (arm_record->gdbarch);
14608 arm_record->arm_insn
14609 = reader.read (arm_record->this_addr, insn_size, code_endian);
14611 if (ARM_RECORD == record_type)
14613 arm_record->cond = bits (arm_record->arm_insn, 28, 31);
14614 insn_id = bits (arm_record->arm_insn, 25, 27);
14616 if (arm_record->cond == 0xf)
14617 ret = arm_record_extension_space (arm_record);
14618 else
14620 /* If this insn has fallen into extension space
14621 then we need not decode it anymore. */
14622 ret = arm_handle_insn[insn_id] (arm_record);
14624 if (ret != ARM_RECORD_SUCCESS)
14626 arm_record_unsupported_insn (arm_record);
14627 ret = -1;
14630 else if (THUMB_RECORD == record_type)
14632 /* As thumb does not have condition codes, we set negative. */
14633 arm_record->cond = -1;
14634 insn_id = bits (arm_record->arm_insn, 13, 15);
14635 ret = thumb_handle_insn[insn_id] (arm_record);
14636 if (ret != ARM_RECORD_SUCCESS)
14638 arm_record_unsupported_insn (arm_record);
14639 ret = -1;
14642 else if (THUMB2_RECORD == record_type)
14644 /* As thumb does not have condition codes, we set negative. */
14645 arm_record->cond = -1;
14647 /* Swap first half of 32bit thumb instruction with second half. */
14648 arm_record->arm_insn
14649 = (arm_record->arm_insn >> 16) | (arm_record->arm_insn << 16);
14651 ret = thumb2_record_decode_insn_handler (arm_record);
14653 if (ret != ARM_RECORD_SUCCESS)
14655 arm_record_unsupported_insn (arm_record);
14656 ret = -1;
14659 else
14661 /* Throw assertion. */
14662 gdb_assert_not_reached ("not a valid instruction, could not decode");
14665 return ret;
14668 #if GDB_SELF_TEST
14669 namespace selftests {
14671 /* Instruction reader class for selftests.
14673 For 16-bit Thumb instructions, an array of uint16_t should be used.
14675 For 32-bit Thumb instructions and regular 32-bit Arm instructions, an array
14676 of uint32_t should be used. */
14678 template<typename T>
14679 class instruction_reader_selftest : public abstract_instruction_reader
14681 public:
14682 template<size_t SIZE>
14683 instruction_reader_selftest (const T (&insns)[SIZE])
14684 : m_insns (insns), m_insns_size (SIZE)
14687 ULONGEST read (CORE_ADDR memaddr, const size_t length,
14688 enum bfd_endian byte_order) override
14690 SELF_CHECK (length == sizeof (T));
14691 SELF_CHECK (memaddr % sizeof (T) == 0);
14692 SELF_CHECK ((memaddr / sizeof (T)) < m_insns_size);
14694 return m_insns[memaddr / sizeof (T)];
14697 private:
14698 const T *m_insns;
14699 const size_t m_insns_size;
14702 static void
14703 arm_record_test (void)
14705 struct gdbarch_info info;
14706 info.bfd_arch_info = bfd_scan_arch ("arm");
14708 struct gdbarch *gdbarch = gdbarch_find_by_info (info);
14710 SELF_CHECK (gdbarch != NULL);
14712 /* 16-bit Thumb instructions. */
14714 arm_insn_decode_record arm_record;
14716 memset (&arm_record, 0, sizeof (arm_insn_decode_record));
14717 arm_record.gdbarch = gdbarch;
14719 /* Use the endian-free representation of the instructions here. The test
14720 will handle endianness conversions. */
14721 static const uint16_t insns[] = {
14722 /* db b2 uxtb r3, r3 */
14723 0xb2db,
14724 /* cd 58 ldr r5, [r1, r3] */
14725 0x58cd,
14728 instruction_reader_selftest<uint16_t> reader (insns);
14729 int ret = decode_insn (reader, &arm_record, THUMB_RECORD,
14730 THUMB_INSN_SIZE_BYTES);
14732 SELF_CHECK (ret == 0);
14733 SELF_CHECK (arm_record.mem_rec_count == 0);
14734 SELF_CHECK (arm_record.reg_rec_count == 1);
14735 SELF_CHECK (arm_record.arm_regs[0] == 3);
14737 arm_record.this_addr += 2;
14738 ret = decode_insn (reader, &arm_record, THUMB_RECORD,
14739 THUMB_INSN_SIZE_BYTES);
14741 SELF_CHECK (ret == 0);
14742 SELF_CHECK (arm_record.mem_rec_count == 0);
14743 SELF_CHECK (arm_record.reg_rec_count == 1);
14744 SELF_CHECK (arm_record.arm_regs[0] == 5);
14747 /* 32-bit Thumb-2 instructions. */
14749 arm_insn_decode_record arm_record;
14751 memset (&arm_record, 0, sizeof (arm_insn_decode_record));
14752 arm_record.gdbarch = gdbarch;
14754 /* Use the endian-free representation of the instruction here. The test
14755 will handle endianness conversions. */
14756 static const uint32_t insns[] = {
14757 /* mrc 15, 0, r7, cr13, cr0, {3} */
14758 0x7f70ee1d,
14761 instruction_reader_selftest<uint32_t> reader (insns);
14762 int ret = decode_insn (reader, &arm_record, THUMB2_RECORD,
14763 THUMB2_INSN_SIZE_BYTES);
14765 SELF_CHECK (ret == 0);
14766 SELF_CHECK (arm_record.mem_rec_count == 0);
14767 SELF_CHECK (arm_record.reg_rec_count == 1);
14768 SELF_CHECK (arm_record.arm_regs[0] == 7);
14771 /* 32-bit instructions. */
14773 arm_insn_decode_record arm_record;
14775 memset (&arm_record, 0, sizeof (arm_insn_decode_record));
14776 arm_record.gdbarch = gdbarch;
14778 /* Use the endian-free representation of the instruction here. The test
14779 will handle endianness conversions. */
14780 static const uint32_t insns[] = {
14781 /* mov r5, r0 */
14782 0xe1a05000,
14785 instruction_reader_selftest<uint32_t> reader (insns);
14786 int ret = decode_insn (reader, &arm_record, ARM_RECORD,
14787 ARM_INSN_SIZE_BYTES);
14789 SELF_CHECK (ret == 0);
14793 /* Instruction reader from manually cooked instruction sequences. */
14795 class test_arm_instruction_reader : public arm_instruction_reader
14797 public:
14798 explicit test_arm_instruction_reader (gdb::array_view<const uint32_t> insns)
14799 : m_insns (insns)
14802 uint32_t read (CORE_ADDR memaddr, enum bfd_endian byte_order) const override
14804 SELF_CHECK (memaddr % 4 == 0);
14805 SELF_CHECK (memaddr / 4 < m_insns.size ());
14807 return m_insns[memaddr / 4];
14810 private:
14811 const gdb::array_view<const uint32_t> m_insns;
14814 static void
14815 arm_analyze_prologue_test ()
14817 for (bfd_endian endianness : {BFD_ENDIAN_LITTLE, BFD_ENDIAN_BIG})
14819 struct gdbarch_info info;
14820 info.byte_order = endianness;
14821 info.byte_order_for_code = endianness;
14822 info.bfd_arch_info = bfd_scan_arch ("arm");
14824 struct gdbarch *gdbarch = gdbarch_find_by_info (info);
14826 SELF_CHECK (gdbarch != NULL);
14828 /* The "sub" instruction contains an immediate value rotate count of 0,
14829 which resulted in a 32-bit shift of a 32-bit value, caught by
14830 UBSan. */
14831 const uint32_t insns[] = {
14832 0xe92d4ff0, /* push {r4, r5, r6, r7, r8, r9, sl, fp, lr} */
14833 0xe1a05000, /* mov r5, r0 */
14834 0xe5903020, /* ldr r3, [r0, #32] */
14835 0xe24dd044, /* sub sp, sp, #68 ; 0x44 */
14838 test_arm_instruction_reader mem_reader (insns);
14839 arm_prologue_cache cache;
14840 arm_cache_init (&cache, gdbarch);
14842 arm_analyze_prologue (gdbarch, 0, sizeof (insns) - 1, &cache, mem_reader);
14846 } // namespace selftests
14847 #endif /* GDB_SELF_TEST */
14849 /* Cleans up local record registers and memory allocations. */
14851 static void
14852 deallocate_reg_mem (arm_insn_decode_record *record)
14854 xfree (record->arm_regs);
14855 xfree (record->arm_mems);
14859 /* Parse the current instruction and record the values of the registers and
14860 memory that will be changed in current instruction to record_arch_list".
14861 Return -1 if something is wrong. */
14864 arm_process_record (struct gdbarch *gdbarch, struct regcache *regcache,
14865 CORE_ADDR insn_addr)
14868 uint32_t no_of_rec = 0;
14869 uint32_t ret = 0; /* return value: -1:record failure ; 0:success */
14870 ULONGEST t_bit = 0, insn_id = 0;
14872 ULONGEST u_regval = 0;
14874 arm_insn_decode_record arm_record;
14876 memset (&arm_record, 0, sizeof (arm_insn_decode_record));
14877 arm_record.regcache = regcache;
14878 arm_record.this_addr = insn_addr;
14879 arm_record.gdbarch = gdbarch;
14882 if (record_debug > 1)
14884 gdb_printf (gdb_stdlog, "Process record: arm_process_record "
14885 "addr = %s\n",
14886 paddress (gdbarch, arm_record.this_addr));
14889 instruction_reader reader;
14890 enum bfd_endian code_endian
14891 = gdbarch_byte_order_for_code (arm_record.gdbarch);
14892 arm_record.arm_insn
14893 = reader.read (arm_record.this_addr, 2, code_endian);
14895 /* Check the insn, whether it is thumb or arm one. */
14897 t_bit = arm_psr_thumb_bit (arm_record.gdbarch);
14898 regcache_raw_read_unsigned (arm_record.regcache, ARM_PS_REGNUM, &u_regval);
14901 if (!(u_regval & t_bit))
14903 /* We are decoding arm insn. */
14904 ret = decode_insn (reader, &arm_record, ARM_RECORD, ARM_INSN_SIZE_BYTES);
14906 else
14908 insn_id = bits (arm_record.arm_insn, 11, 15);
14909 /* is it thumb2 insn? */
14910 if ((0x1D == insn_id) || (0x1E == insn_id) || (0x1F == insn_id))
14912 ret = decode_insn (reader, &arm_record, THUMB2_RECORD,
14913 THUMB2_INSN_SIZE_BYTES);
14915 else
14917 /* We are decoding thumb insn. */
14918 ret = decode_insn (reader, &arm_record, THUMB_RECORD,
14919 THUMB_INSN_SIZE_BYTES);
14923 if (0 == ret)
14925 /* Record registers. */
14926 record_full_arch_list_add_reg (arm_record.regcache, ARM_PC_REGNUM);
14927 if (arm_record.arm_regs)
14929 for (no_of_rec = 0; no_of_rec < arm_record.reg_rec_count; no_of_rec++)
14931 if (record_full_arch_list_add_reg
14932 (arm_record.regcache , arm_record.arm_regs[no_of_rec]))
14933 ret = -1;
14936 /* Record memories. */
14937 if (arm_record.arm_mems)
14939 for (no_of_rec = 0; no_of_rec < arm_record.mem_rec_count; no_of_rec++)
14941 if (record_full_arch_list_add_mem
14942 ((CORE_ADDR)arm_record.arm_mems[no_of_rec].addr,
14943 arm_record.arm_mems[no_of_rec].len))
14944 ret = -1;
14948 if (record_full_arch_list_add_end ())
14949 ret = -1;
14953 deallocate_reg_mem (&arm_record);
14955 return ret;
14958 /* See arm-tdep.h. */
14960 const target_desc *
14961 arm_read_description (arm_fp_type fp_type, bool tls)
14963 struct target_desc *tdesc = tdesc_arm_list[fp_type][tls];
14965 if (tdesc == nullptr)
14967 tdesc = arm_create_target_description (fp_type, tls);
14968 tdesc_arm_list[fp_type][tls] = tdesc;
14971 return tdesc;
14974 /* See arm-tdep.h. */
14976 const target_desc *
14977 arm_read_mprofile_description (arm_m_profile_type m_type)
14979 struct target_desc *tdesc = tdesc_arm_mprofile_list[m_type];
14981 if (tdesc == nullptr)
14983 tdesc = arm_create_mprofile_target_description (m_type);
14984 tdesc_arm_mprofile_list[m_type] = tdesc;
14987 return tdesc;