Automatic date update in version.in
[binutils-gdb.git] / gdb / arm-tdep.c
blobcf8b610a381f13d115638e085d549207852078f4
1 /* Common target dependent code for GDB on ARM systems.
3 Copyright (C) 1988-2022 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20 #include "defs.h"
22 #include <ctype.h> /* XXX for isupper (). */
24 #include "frame.h"
25 #include "inferior.h"
26 #include "infrun.h"
27 #include "gdbcmd.h"
28 #include "gdbcore.h"
29 #include "dis-asm.h" /* For register styles. */
30 #include "disasm.h"
31 #include "regcache.h"
32 #include "reggroups.h"
33 #include "target-float.h"
34 #include "value.h"
35 #include "arch-utils.h"
36 #include "osabi.h"
37 #include "frame-unwind.h"
38 #include "frame-base.h"
39 #include "trad-frame.h"
40 #include "objfiles.h"
41 #include "dwarf2.h"
42 #include "dwarf2/frame.h"
43 #include "gdbtypes.h"
44 #include "prologue-value.h"
45 #include "remote.h"
46 #include "target-descriptions.h"
47 #include "user-regs.h"
48 #include "observable.h"
49 #include "count-one-bits.h"
51 #include "arch/arm.h"
52 #include "arch/arm-get-next-pcs.h"
53 #include "arm-tdep.h"
54 #include "gdb/sim-arm.h"
56 #include "elf-bfd.h"
57 #include "coff/internal.h"
58 #include "elf/arm.h"
60 #include "record.h"
61 #include "record-full.h"
62 #include <algorithm>
64 #include "producer.h"
66 #if GDB_SELF_TEST
67 #include "gdbsupport/selftest.h"
68 #endif
70 static bool arm_debug;
72 /* Print an "arm" debug statement. */
74 #define arm_debug_printf(fmt, ...) \
75 debug_prefixed_printf_cond (arm_debug, "arm", fmt, ##__VA_ARGS__)
77 /* Macros for setting and testing a bit in a minimal symbol that marks
78 it as Thumb function. The MSB of the minimal symbol's "info" field
79 is used for this purpose.
81 MSYMBOL_SET_SPECIAL Actually sets the "special" bit.
82 MSYMBOL_IS_SPECIAL Tests the "special" bit in a minimal symbol. */
84 #define MSYMBOL_SET_SPECIAL(msym) \
85 (msym)->set_target_flag_1 (true)
87 #define MSYMBOL_IS_SPECIAL(msym) \
88 (msym)->target_flag_1 ()
90 struct arm_mapping_symbol
92 CORE_ADDR value;
93 char type;
95 bool operator< (const arm_mapping_symbol &other) const
96 { return this->value < other.value; }
99 typedef std::vector<arm_mapping_symbol> arm_mapping_symbol_vec;
101 struct arm_per_bfd
103 explicit arm_per_bfd (size_t num_sections)
104 : section_maps (new arm_mapping_symbol_vec[num_sections]),
105 section_maps_sorted (new bool[num_sections] ())
108 DISABLE_COPY_AND_ASSIGN (arm_per_bfd);
110 /* Information about mapping symbols ($a, $d, $t) in the objfile.
112 The format is an array of vectors of arm_mapping_symbols, there is one
113 vector for each section of the objfile (the array is index by BFD section
114 index).
116 For each section, the vector of arm_mapping_symbol is sorted by
117 symbol value (address). */
118 std::unique_ptr<arm_mapping_symbol_vec[]> section_maps;
120 /* For each corresponding element of section_maps above, is this vector
121 sorted. */
122 std::unique_ptr<bool[]> section_maps_sorted;
125 /* Per-bfd data used for mapping symbols. */
126 static const registry<bfd>::key<arm_per_bfd> arm_bfd_data_key;
128 /* The list of available "set arm ..." and "show arm ..." commands. */
129 static struct cmd_list_element *setarmcmdlist = NULL;
130 static struct cmd_list_element *showarmcmdlist = NULL;
132 /* The type of floating-point to use. Keep this in sync with enum
133 arm_float_model, and the help string in _initialize_arm_tdep. */
134 static const char *const fp_model_strings[] =
136 "auto",
137 "softfpa",
138 "fpa",
139 "softvfp",
140 "vfp",
141 NULL
144 /* A variable that can be configured by the user. */
145 static enum arm_float_model arm_fp_model = ARM_FLOAT_AUTO;
146 static const char *current_fp_model = "auto";
148 /* The ABI to use. Keep this in sync with arm_abi_kind. */
149 static const char *const arm_abi_strings[] =
151 "auto",
152 "APCS",
153 "AAPCS",
154 NULL
157 /* A variable that can be configured by the user. */
158 static enum arm_abi_kind arm_abi_global = ARM_ABI_AUTO;
159 static const char *arm_abi_string = "auto";
161 /* The execution mode to assume. */
162 static const char *const arm_mode_strings[] =
164 "auto",
165 "arm",
166 "thumb",
167 NULL
170 static const char *arm_fallback_mode_string = "auto";
171 static const char *arm_force_mode_string = "auto";
173 /* The standard register names, and all the valid aliases for them. Note
174 that `fp', `sp' and `pc' are not added in this alias list, because they
175 have been added as builtin user registers in
176 std-regs.c:_initialize_frame_reg. */
177 static const struct
179 const char *name;
180 int regnum;
181 } arm_register_aliases[] = {
182 /* Basic register numbers. */
183 { "r0", 0 },
184 { "r1", 1 },
185 { "r2", 2 },
186 { "r3", 3 },
187 { "r4", 4 },
188 { "r5", 5 },
189 { "r6", 6 },
190 { "r7", 7 },
191 { "r8", 8 },
192 { "r9", 9 },
193 { "r10", 10 },
194 { "r11", 11 },
195 { "r12", 12 },
196 { "r13", 13 },
197 { "r14", 14 },
198 { "r15", 15 },
199 /* Synonyms (argument and variable registers). */
200 { "a1", 0 },
201 { "a2", 1 },
202 { "a3", 2 },
203 { "a4", 3 },
204 { "v1", 4 },
205 { "v2", 5 },
206 { "v3", 6 },
207 { "v4", 7 },
208 { "v5", 8 },
209 { "v6", 9 },
210 { "v7", 10 },
211 { "v8", 11 },
212 /* Other platform-specific names for r9. */
213 { "sb", 9 },
214 { "tr", 9 },
215 /* Special names. */
216 { "ip", 12 },
217 { "lr", 14 },
218 /* Names used by GCC (not listed in the ARM EABI). */
219 { "sl", 10 },
220 /* A special name from the older ATPCS. */
221 { "wr", 7 },
224 static const char *const arm_register_names[] =
225 {"r0", "r1", "r2", "r3", /* 0 1 2 3 */
226 "r4", "r5", "r6", "r7", /* 4 5 6 7 */
227 "r8", "r9", "r10", "r11", /* 8 9 10 11 */
228 "r12", "sp", "lr", "pc", /* 12 13 14 15 */
229 "f0", "f1", "f2", "f3", /* 16 17 18 19 */
230 "f4", "f5", "f6", "f7", /* 20 21 22 23 */
231 "fps", "cpsr" }; /* 24 25 */
233 /* Holds the current set of options to be passed to the disassembler. */
234 static char *arm_disassembler_options;
236 /* Valid register name styles. */
237 static const char **valid_disassembly_styles;
239 /* Disassembly style to use. Default to "std" register names. */
240 static const char *disassembly_style;
242 /* All possible arm target descriptors. */
243 static struct target_desc *tdesc_arm_list[ARM_FP_TYPE_INVALID][2];
244 static struct target_desc *tdesc_arm_mprofile_list[ARM_M_TYPE_INVALID];
246 /* This is used to keep the bfd arch_info in sync with the disassembly
247 style. */
248 static void set_disassembly_style_sfunc (const char *, int,
249 struct cmd_list_element *);
250 static void show_disassembly_style_sfunc (struct ui_file *, int,
251 struct cmd_list_element *,
252 const char *);
254 static enum register_status arm_neon_quad_read (struct gdbarch *gdbarch,
255 readable_regcache *regcache,
256 int regnum, gdb_byte *buf);
257 static void arm_neon_quad_write (struct gdbarch *gdbarch,
258 struct regcache *regcache,
259 int regnum, const gdb_byte *buf);
261 static CORE_ADDR
262 arm_get_next_pcs_syscall_next_pc (struct arm_get_next_pcs *self);
265 /* get_next_pcs operations. */
266 static struct arm_get_next_pcs_ops arm_get_next_pcs_ops = {
267 arm_get_next_pcs_read_memory_unsigned_integer,
268 arm_get_next_pcs_syscall_next_pc,
269 arm_get_next_pcs_addr_bits_remove,
270 arm_get_next_pcs_is_thumb,
271 NULL,
274 struct arm_prologue_cache
276 /* The stack pointer at the time this frame was created; i.e. the
277 caller's stack pointer when this function was called. It is used
278 to identify this frame. */
279 CORE_ADDR sp;
281 /* Additional stack pointers used by M-profile with Security extension. */
282 /* Use msp_s / psp_s to hold the values of msp / psp when there is
283 no Security extension. */
284 CORE_ADDR msp_s;
285 CORE_ADDR msp_ns;
286 CORE_ADDR psp_s;
287 CORE_ADDR psp_ns;
289 /* Active stack pointer. */
290 int active_sp_regnum;
291 int active_msp_regnum;
292 int active_psp_regnum;
294 /* The frame base for this frame is just prev_sp - frame size.
295 FRAMESIZE is the distance from the frame pointer to the
296 initial stack pointer. */
298 int framesize;
300 /* The register used to hold the frame pointer for this frame. */
301 int framereg;
303 /* True if the return address is signed, false otherwise. */
304 gdb::optional<bool> ra_signed_state;
306 /* Saved register offsets. */
307 trad_frame_saved_reg *saved_regs;
309 arm_prologue_cache() = default;
313 /* Reconstruct T bit in program status register from LR value. */
315 static inline ULONGEST
316 reconstruct_t_bit(struct gdbarch *gdbarch, CORE_ADDR lr, ULONGEST psr)
318 ULONGEST t_bit = arm_psr_thumb_bit (gdbarch);
319 if (IS_THUMB_ADDR (lr))
320 psr |= t_bit;
321 else
322 psr &= ~t_bit;
324 return psr;
327 /* Initialize stack pointers, and flag the active one. */
329 static inline void
330 arm_cache_init_sp (int regnum, CORE_ADDR* member,
331 struct arm_prologue_cache *cache,
332 struct frame_info *frame)
334 CORE_ADDR val = get_frame_register_unsigned (frame, regnum);
335 if (val == cache->sp)
336 cache->active_sp_regnum = regnum;
338 *member = val;
341 /* Initialize CACHE fields for which zero is not adequate (CACHE is
342 expected to have been ZALLOC'ed before calling this function). */
344 static void
345 arm_cache_init (struct arm_prologue_cache *cache, struct gdbarch *gdbarch)
347 cache->active_sp_regnum = ARM_SP_REGNUM;
349 cache->saved_regs = trad_frame_alloc_saved_regs (gdbarch);
352 /* Similar to the previous function, but extracts GDBARCH from FRAME. */
354 static void
355 arm_cache_init (struct arm_prologue_cache *cache, struct frame_info *frame)
357 struct gdbarch *gdbarch = get_frame_arch (frame);
358 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
360 arm_cache_init (cache, gdbarch);
361 cache->sp = get_frame_register_unsigned (frame, ARM_SP_REGNUM);
363 if (tdep->have_sec_ext)
365 CORE_ADDR msp_val = get_frame_register_unsigned (frame, tdep->m_profile_msp_regnum);
366 CORE_ADDR psp_val = get_frame_register_unsigned (frame, tdep->m_profile_psp_regnum);
368 arm_cache_init_sp (tdep->m_profile_msp_s_regnum, &cache->msp_s, cache, frame);
369 arm_cache_init_sp (tdep->m_profile_psp_s_regnum, &cache->psp_s, cache, frame);
370 arm_cache_init_sp (tdep->m_profile_msp_ns_regnum, &cache->msp_ns, cache, frame);
371 arm_cache_init_sp (tdep->m_profile_psp_ns_regnum, &cache->psp_ns, cache, frame);
373 if (msp_val == cache->msp_s)
374 cache->active_msp_regnum = tdep->m_profile_msp_s_regnum;
375 else if (msp_val == cache->msp_ns)
376 cache->active_msp_regnum = tdep->m_profile_msp_ns_regnum;
377 if (psp_val == cache->psp_s)
378 cache->active_psp_regnum = tdep->m_profile_psp_s_regnum;
379 else if (psp_val == cache->psp_ns)
380 cache->active_psp_regnum = tdep->m_profile_psp_ns_regnum;
382 /* Use MSP_S as default stack pointer. */
383 if (cache->active_sp_regnum == ARM_SP_REGNUM)
384 cache->active_sp_regnum = tdep->m_profile_msp_s_regnum;
386 else if (tdep->is_m)
388 arm_cache_init_sp (tdep->m_profile_msp_regnum, &cache->msp_s, cache, frame);
389 arm_cache_init_sp (tdep->m_profile_psp_regnum, &cache->psp_s, cache, frame);
391 else
392 arm_cache_init_sp (ARM_SP_REGNUM, &cache->msp_s, cache, frame);
395 /* Return the requested stack pointer value (in REGNUM), taking into
396 account whether we have a Security extension or an M-profile
397 CPU. */
399 static CORE_ADDR
400 arm_cache_get_sp_register (struct arm_prologue_cache *cache,
401 arm_gdbarch_tdep *tdep, int regnum)
403 if (tdep->have_sec_ext)
405 if (regnum == tdep->m_profile_msp_s_regnum)
406 return cache->msp_s;
407 if (regnum == tdep->m_profile_msp_ns_regnum)
408 return cache->msp_ns;
409 if (regnum == tdep->m_profile_psp_s_regnum)
410 return cache->psp_s;
411 if (regnum == tdep->m_profile_psp_ns_regnum)
412 return cache->psp_ns;
413 if (regnum == tdep->m_profile_msp_regnum)
414 return arm_cache_get_sp_register (cache, tdep, cache->active_msp_regnum);
415 if (regnum == tdep->m_profile_psp_regnum)
416 return arm_cache_get_sp_register (cache, tdep, cache->active_psp_regnum);
417 if (regnum == ARM_SP_REGNUM)
418 return arm_cache_get_sp_register (cache, tdep, cache->active_sp_regnum);
420 else if (tdep->is_m)
422 if (regnum == tdep->m_profile_msp_regnum)
423 return cache->msp_s;
424 if (regnum == tdep->m_profile_psp_regnum)
425 return cache->psp_s;
426 if (regnum == ARM_SP_REGNUM)
427 return arm_cache_get_sp_register (cache, tdep, cache->active_sp_regnum);
429 else if (regnum == ARM_SP_REGNUM)
430 return cache->sp;
432 gdb_assert_not_reached ("Invalid SP selection");
435 /* Return the previous stack address, depending on which SP register
436 is active. */
438 static CORE_ADDR
439 arm_cache_get_prev_sp_value (struct arm_prologue_cache *cache, arm_gdbarch_tdep *tdep)
441 CORE_ADDR val = arm_cache_get_sp_register (cache, tdep, cache->active_sp_regnum);
442 return val;
445 /* Set the active stack pointer to VAL. */
447 static void
448 arm_cache_set_active_sp_value (struct arm_prologue_cache *cache,
449 arm_gdbarch_tdep *tdep, CORE_ADDR val)
451 if (tdep->have_sec_ext)
453 if (cache->active_sp_regnum == tdep->m_profile_msp_s_regnum)
454 cache->msp_s = val;
455 else if (cache->active_sp_regnum == tdep->m_profile_msp_ns_regnum)
456 cache->msp_ns = val;
457 else if (cache->active_sp_regnum == tdep->m_profile_psp_s_regnum)
458 cache->psp_s = val;
459 else if (cache->active_sp_regnum == tdep->m_profile_psp_ns_regnum)
460 cache->psp_ns = val;
462 return;
464 else if (tdep->is_m)
466 if (cache->active_sp_regnum == tdep->m_profile_msp_regnum)
467 cache->msp_s = val;
468 else if (cache->active_sp_regnum == tdep->m_profile_psp_regnum)
469 cache->psp_s = val;
471 return;
473 else if (cache->active_sp_regnum == ARM_SP_REGNUM)
475 cache->sp = val;
476 return;
479 gdb_assert_not_reached ("Invalid SP selection");
482 /* Return true if REGNUM is one of the alternative stack pointers. */
484 static bool
485 arm_is_alternative_sp_register (arm_gdbarch_tdep *tdep, int regnum)
487 if ((regnum == tdep->m_profile_msp_regnum)
488 || (regnum == tdep->m_profile_msp_s_regnum)
489 || (regnum == tdep->m_profile_msp_ns_regnum)
490 || (regnum == tdep->m_profile_psp_regnum)
491 || (regnum == tdep->m_profile_psp_s_regnum)
492 || (regnum == tdep->m_profile_psp_ns_regnum))
493 return true;
494 else
495 return false;
498 /* Set the active stack pointer to SP_REGNUM. */
500 static void
501 arm_cache_switch_prev_sp (struct arm_prologue_cache *cache,
502 arm_gdbarch_tdep *tdep, int sp_regnum)
504 gdb_assert (arm_is_alternative_sp_register (tdep, sp_regnum));
506 if (tdep->have_sec_ext)
507 gdb_assert (sp_regnum != tdep->m_profile_msp_regnum
508 && sp_regnum != tdep->m_profile_psp_regnum);
510 cache->active_sp_regnum = sp_regnum;
513 namespace {
515 /* Abstract class to read ARM instructions from memory. */
517 class arm_instruction_reader
519 public:
520 /* Read a 4 bytes instruction from memory using the BYTE_ORDER endianness. */
521 virtual uint32_t read (CORE_ADDR memaddr, bfd_endian byte_order) const = 0;
524 /* Read instructions from target memory. */
526 class target_arm_instruction_reader : public arm_instruction_reader
528 public:
529 uint32_t read (CORE_ADDR memaddr, bfd_endian byte_order) const override
531 return read_code_unsigned_integer (memaddr, 4, byte_order);
535 } /* namespace */
537 static CORE_ADDR arm_analyze_prologue
538 (struct gdbarch *gdbarch, CORE_ADDR prologue_start, CORE_ADDR prologue_end,
539 struct arm_prologue_cache *cache, const arm_instruction_reader &insn_reader);
541 /* Architecture version for displaced stepping. This effects the behaviour of
542 certain instructions, and really should not be hard-wired. */
544 #define DISPLACED_STEPPING_ARCH_VERSION 5
546 /* See arm-tdep.h. */
548 bool arm_apcs_32 = true;
549 bool arm_unwind_secure_frames = true;
551 /* Return the bit mask in ARM_PS_REGNUM that indicates Thumb mode. */
554 arm_psr_thumb_bit (struct gdbarch *gdbarch)
556 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
558 if (tdep->is_m)
559 return XPSR_T;
560 else
561 return CPSR_T;
564 /* Determine if the processor is currently executing in Thumb mode. */
567 arm_is_thumb (struct regcache *regcache)
569 ULONGEST cpsr;
570 ULONGEST t_bit = arm_psr_thumb_bit (regcache->arch ());
572 cpsr = regcache_raw_get_unsigned (regcache, ARM_PS_REGNUM);
574 return (cpsr & t_bit) != 0;
577 /* Determine if FRAME is executing in Thumb mode. FRAME must be an ARM
578 frame. */
581 arm_frame_is_thumb (struct frame_info *frame)
583 /* Check the architecture of FRAME. */
584 struct gdbarch *gdbarch = get_frame_arch (frame);
585 gdb_assert (gdbarch_bfd_arch_info (gdbarch)->arch == bfd_arch_arm);
587 /* Every ARM frame unwinder can unwind the T bit of the CPSR, either
588 directly (from a signal frame or dummy frame) or by interpreting
589 the saved LR (from a prologue or DWARF frame). So consult it and
590 trust the unwinders. */
591 CORE_ADDR cpsr = get_frame_register_unsigned (frame, ARM_PS_REGNUM);
593 /* Find and extract the thumb bit. */
594 ULONGEST t_bit = arm_psr_thumb_bit (gdbarch);
595 return (cpsr & t_bit) != 0;
598 /* Search for the mapping symbol covering MEMADDR. If one is found,
599 return its type. Otherwise, return 0. If START is non-NULL,
600 set *START to the location of the mapping symbol. */
602 static char
603 arm_find_mapping_symbol (CORE_ADDR memaddr, CORE_ADDR *start)
605 struct obj_section *sec;
607 /* If there are mapping symbols, consult them. */
608 sec = find_pc_section (memaddr);
609 if (sec != NULL)
611 arm_per_bfd *data = arm_bfd_data_key.get (sec->objfile->obfd.get ());
612 if (data != NULL)
614 unsigned int section_idx = sec->the_bfd_section->index;
615 arm_mapping_symbol_vec &map
616 = data->section_maps[section_idx];
618 /* Sort the vector on first use. */
619 if (!data->section_maps_sorted[section_idx])
621 std::sort (map.begin (), map.end ());
622 data->section_maps_sorted[section_idx] = true;
625 arm_mapping_symbol map_key = { memaddr - sec->addr (), 0 };
626 arm_mapping_symbol_vec::const_iterator it
627 = std::lower_bound (map.begin (), map.end (), map_key);
629 /* std::lower_bound finds the earliest ordered insertion
630 point. If the symbol at this position starts at this exact
631 address, we use that; otherwise, the preceding
632 mapping symbol covers this address. */
633 if (it < map.end ())
635 if (it->value == map_key.value)
637 if (start)
638 *start = it->value + sec->addr ();
639 return it->type;
643 if (it > map.begin ())
645 arm_mapping_symbol_vec::const_iterator prev_it
646 = it - 1;
648 if (start)
649 *start = prev_it->value + sec->addr ();
650 return prev_it->type;
655 return 0;
658 /* Determine if the program counter specified in MEMADDR is in a Thumb
659 function. This function should be called for addresses unrelated to
660 any executing frame; otherwise, prefer arm_frame_is_thumb. */
663 arm_pc_is_thumb (struct gdbarch *gdbarch, CORE_ADDR memaddr)
665 struct bound_minimal_symbol sym;
666 char type;
667 arm_displaced_step_copy_insn_closure *dsc = nullptr;
668 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
670 if (gdbarch_displaced_step_copy_insn_closure_by_addr_p (gdbarch))
671 dsc = ((arm_displaced_step_copy_insn_closure * )
672 gdbarch_displaced_step_copy_insn_closure_by_addr
673 (gdbarch, current_inferior (), memaddr));
675 /* If checking the mode of displaced instruction in copy area, the mode
676 should be determined by instruction on the original address. */
677 if (dsc)
679 displaced_debug_printf ("check mode of %.8lx instead of %.8lx",
680 (unsigned long) dsc->insn_addr,
681 (unsigned long) memaddr);
682 memaddr = dsc->insn_addr;
685 /* If bit 0 of the address is set, assume this is a Thumb address. */
686 if (IS_THUMB_ADDR (memaddr))
687 return 1;
689 /* If the user wants to override the symbol table, let him. */
690 if (strcmp (arm_force_mode_string, "arm") == 0)
691 return 0;
692 if (strcmp (arm_force_mode_string, "thumb") == 0)
693 return 1;
695 /* ARM v6-M and v7-M are always in Thumb mode. */
696 if (tdep->is_m)
697 return 1;
699 /* If there are mapping symbols, consult them. */
700 type = arm_find_mapping_symbol (memaddr, NULL);
701 if (type)
702 return type == 't';
704 /* Thumb functions have a "special" bit set in minimal symbols. */
705 sym = lookup_minimal_symbol_by_pc (memaddr);
706 if (sym.minsym)
707 return (MSYMBOL_IS_SPECIAL (sym.minsym));
709 /* If the user wants to override the fallback mode, let them. */
710 if (strcmp (arm_fallback_mode_string, "arm") == 0)
711 return 0;
712 if (strcmp (arm_fallback_mode_string, "thumb") == 0)
713 return 1;
715 /* If we couldn't find any symbol, but we're talking to a running
716 target, then trust the current value of $cpsr. This lets
717 "display/i $pc" always show the correct mode (though if there is
718 a symbol table we will not reach here, so it still may not be
719 displayed in the mode it will be executed). */
720 if (target_has_registers ())
721 return arm_frame_is_thumb (get_current_frame ());
723 /* Otherwise we're out of luck; we assume ARM. */
724 return 0;
727 /* Determine if the address specified equals any of these magic return
728 values, called EXC_RETURN, defined by the ARM v6-M, v7-M and v8-M
729 architectures.
731 From ARMv6-M Reference Manual B1.5.8
732 Table B1-5 Exception return behavior
734 EXC_RETURN Return To Return Stack
735 0xFFFFFFF1 Handler mode Main
736 0xFFFFFFF9 Thread mode Main
737 0xFFFFFFFD Thread mode Process
739 From ARMv7-M Reference Manual B1.5.8
740 Table B1-8 EXC_RETURN definition of exception return behavior, no FP
742 EXC_RETURN Return To Return Stack
743 0xFFFFFFF1 Handler mode Main
744 0xFFFFFFF9 Thread mode Main
745 0xFFFFFFFD Thread mode Process
747 Table B1-9 EXC_RETURN definition of exception return behavior, with
750 EXC_RETURN Return To Return Stack Frame Type
751 0xFFFFFFE1 Handler mode Main Extended
752 0xFFFFFFE9 Thread mode Main Extended
753 0xFFFFFFED Thread mode Process Extended
754 0xFFFFFFF1 Handler mode Main Basic
755 0xFFFFFFF9 Thread mode Main Basic
756 0xFFFFFFFD Thread mode Process Basic
758 For more details see "B1.5.8 Exception return behavior"
759 in both ARMv6-M and ARMv7-M Architecture Reference Manuals.
761 In the ARMv8-M Architecture Technical Reference also adds
762 for implementations without the Security Extension:
764 EXC_RETURN Condition
765 0xFFFFFFB0 Return to Handler mode.
766 0xFFFFFFB8 Return to Thread mode using the main stack.
767 0xFFFFFFBC Return to Thread mode using the process stack. */
769 static int
770 arm_m_addr_is_magic (struct gdbarch *gdbarch, CORE_ADDR addr)
772 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
773 if (tdep->have_sec_ext)
775 switch ((addr & 0xff000000))
777 case 0xff000000: /* EXC_RETURN pattern. */
778 case 0xfe000000: /* FNC_RETURN pattern. */
779 return 1;
780 default:
781 return 0;
784 else
786 switch (addr)
788 /* Values from ARMv8-M Architecture Technical Reference. */
789 case 0xffffffb0:
790 case 0xffffffb8:
791 case 0xffffffbc:
792 /* Values from Tables in B1.5.8 the EXC_RETURN definitions of
793 the exception return behavior. */
794 case 0xffffffe1:
795 case 0xffffffe9:
796 case 0xffffffed:
797 case 0xfffffff1:
798 case 0xfffffff9:
799 case 0xfffffffd:
800 /* Address is magic. */
801 return 1;
803 default:
804 /* Address is not magic. */
805 return 0;
810 /* Remove useless bits from addresses in a running program. */
811 static CORE_ADDR
812 arm_addr_bits_remove (struct gdbarch *gdbarch, CORE_ADDR val)
814 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
816 /* On M-profile devices, do not strip the low bit from EXC_RETURN
817 (the magic exception return address). */
818 if (tdep->is_m && arm_m_addr_is_magic (gdbarch, val))
819 return val;
821 if (arm_apcs_32)
822 return UNMAKE_THUMB_ADDR (val);
823 else
824 return (val & 0x03fffffc);
827 /* Return 1 if PC is the start of a compiler helper function which
828 can be safely ignored during prologue skipping. IS_THUMB is true
829 if the function is known to be a Thumb function due to the way it
830 is being called. */
831 static int
832 skip_prologue_function (struct gdbarch *gdbarch, CORE_ADDR pc, int is_thumb)
834 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
835 struct bound_minimal_symbol msym;
837 msym = lookup_minimal_symbol_by_pc (pc);
838 if (msym.minsym != NULL
839 && msym.value_address () == pc
840 && msym.minsym->linkage_name () != NULL)
842 const char *name = msym.minsym->linkage_name ();
844 /* The GNU linker's Thumb call stub to foo is named
845 __foo_from_thumb. */
846 if (strstr (name, "_from_thumb") != NULL)
847 name += 2;
849 /* On soft-float targets, __truncdfsf2 is called to convert promoted
850 arguments to their argument types in non-prototyped
851 functions. */
852 if (startswith (name, "__truncdfsf2"))
853 return 1;
854 if (startswith (name, "__aeabi_d2f"))
855 return 1;
857 /* Internal functions related to thread-local storage. */
858 if (startswith (name, "__tls_get_addr"))
859 return 1;
860 if (startswith (name, "__aeabi_read_tp"))
861 return 1;
863 else
865 /* If we run against a stripped glibc, we may be unable to identify
866 special functions by name. Check for one important case,
867 __aeabi_read_tp, by comparing the *code* against the default
868 implementation (this is hand-written ARM assembler in glibc). */
870 if (!is_thumb
871 && read_code_unsigned_integer (pc, 4, byte_order_for_code)
872 == 0xe3e00a0f /* mov r0, #0xffff0fff */
873 && read_code_unsigned_integer (pc + 4, 4, byte_order_for_code)
874 == 0xe240f01f) /* sub pc, r0, #31 */
875 return 1;
878 return 0;
881 /* Extract the immediate from instruction movw/movt of encoding T. INSN1 is
882 the first 16-bit of instruction, and INSN2 is the second 16-bit of
883 instruction. */
884 #define EXTRACT_MOVW_MOVT_IMM_T(insn1, insn2) \
885 ((bits ((insn1), 0, 3) << 12) \
886 | (bits ((insn1), 10, 10) << 11) \
887 | (bits ((insn2), 12, 14) << 8) \
888 | bits ((insn2), 0, 7))
890 /* Extract the immediate from instruction movw/movt of encoding A. INSN is
891 the 32-bit instruction. */
892 #define EXTRACT_MOVW_MOVT_IMM_A(insn) \
893 ((bits ((insn), 16, 19) << 12) \
894 | bits ((insn), 0, 11))
896 /* Decode immediate value; implements ThumbExpandImmediate pseudo-op. */
898 static unsigned int
899 thumb_expand_immediate (unsigned int imm)
901 unsigned int count = imm >> 7;
903 if (count < 8)
904 switch (count / 2)
906 case 0:
907 return imm & 0xff;
908 case 1:
909 return (imm & 0xff) | ((imm & 0xff) << 16);
910 case 2:
911 return ((imm & 0xff) << 8) | ((imm & 0xff) << 24);
912 case 3:
913 return (imm & 0xff) | ((imm & 0xff) << 8)
914 | ((imm & 0xff) << 16) | ((imm & 0xff) << 24);
917 return (0x80 | (imm & 0x7f)) << (32 - count);
920 /* Return 1 if the 16-bit Thumb instruction INSN restores SP in
921 epilogue, 0 otherwise. */
923 static int
924 thumb_instruction_restores_sp (unsigned short insn)
926 return (insn == 0x46bd /* mov sp, r7 */
927 || (insn & 0xff80) == 0xb000 /* add sp, imm */
928 || (insn & 0xfe00) == 0xbc00); /* pop <registers> */
931 /* Analyze a Thumb prologue, looking for a recognizable stack frame
932 and frame pointer. Scan until we encounter a store that could
933 clobber the stack frame unexpectedly, or an unknown instruction.
934 Return the last address which is definitely safe to skip for an
935 initial breakpoint. */
937 static CORE_ADDR
938 thumb_analyze_prologue (struct gdbarch *gdbarch,
939 CORE_ADDR start, CORE_ADDR limit,
940 struct arm_prologue_cache *cache)
942 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
943 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
944 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
945 int i;
946 pv_t regs[16];
947 CORE_ADDR offset;
948 CORE_ADDR unrecognized_pc = 0;
950 for (i = 0; i < 16; i++)
951 regs[i] = pv_register (i, 0);
952 pv_area stack (ARM_SP_REGNUM, gdbarch_addr_bit (gdbarch));
954 while (start < limit)
956 unsigned short insn;
957 gdb::optional<bool> ra_signed_state;
959 insn = read_code_unsigned_integer (start, 2, byte_order_for_code);
961 if ((insn & 0xfe00) == 0xb400) /* push { rlist } */
963 int regno;
964 int mask;
966 if (stack.store_would_trash (regs[ARM_SP_REGNUM]))
967 break;
969 /* Bits 0-7 contain a mask for registers R0-R7. Bit 8 says
970 whether to save LR (R14). */
971 mask = (insn & 0xff) | ((insn & 0x100) << 6);
973 /* Calculate offsets of saved R0-R7 and LR. */
974 for (regno = ARM_LR_REGNUM; regno >= 0; regno--)
975 if (mask & (1 << regno))
977 regs[ARM_SP_REGNUM] = pv_add_constant (regs[ARM_SP_REGNUM],
978 -4);
979 stack.store (regs[ARM_SP_REGNUM], 4, regs[regno]);
982 else if ((insn & 0xff80) == 0xb080) /* sub sp, #imm */
984 offset = (insn & 0x7f) << 2; /* get scaled offset */
985 regs[ARM_SP_REGNUM] = pv_add_constant (regs[ARM_SP_REGNUM],
986 -offset);
988 else if (thumb_instruction_restores_sp (insn))
990 /* Don't scan past the epilogue. */
991 break;
993 else if ((insn & 0xf800) == 0xa800) /* add Rd, sp, #imm */
994 regs[bits (insn, 8, 10)] = pv_add_constant (regs[ARM_SP_REGNUM],
995 (insn & 0xff) << 2);
996 else if ((insn & 0xfe00) == 0x1c00 /* add Rd, Rn, #imm */
997 && pv_is_register (regs[bits (insn, 3, 5)], ARM_SP_REGNUM))
998 regs[bits (insn, 0, 2)] = pv_add_constant (regs[bits (insn, 3, 5)],
999 bits (insn, 6, 8));
1000 else if ((insn & 0xf800) == 0x3000 /* add Rd, #imm */
1001 && pv_is_register (regs[bits (insn, 8, 10)], ARM_SP_REGNUM))
1002 regs[bits (insn, 8, 10)] = pv_add_constant (regs[bits (insn, 8, 10)],
1003 bits (insn, 0, 7));
1004 else if ((insn & 0xfe00) == 0x1800 /* add Rd, Rn, Rm */
1005 && pv_is_register (regs[bits (insn, 6, 8)], ARM_SP_REGNUM)
1006 && pv_is_constant (regs[bits (insn, 3, 5)]))
1007 regs[bits (insn, 0, 2)] = pv_add (regs[bits (insn, 3, 5)],
1008 regs[bits (insn, 6, 8)]);
1009 else if ((insn & 0xff00) == 0x4400 /* add Rd, Rm */
1010 && pv_is_constant (regs[bits (insn, 3, 6)]))
1012 int rd = (bit (insn, 7) << 3) + bits (insn, 0, 2);
1013 int rm = bits (insn, 3, 6);
1014 regs[rd] = pv_add (regs[rd], regs[rm]);
1016 else if ((insn & 0xff00) == 0x4600) /* mov hi, lo or mov lo, hi */
1018 int dst_reg = (insn & 0x7) + ((insn & 0x80) >> 4);
1019 int src_reg = (insn & 0x78) >> 3;
1020 regs[dst_reg] = regs[src_reg];
1022 else if ((insn & 0xf800) == 0x9000) /* str rd, [sp, #off] */
1024 /* Handle stores to the stack. Normally pushes are used,
1025 but with GCC -mtpcs-frame, there may be other stores
1026 in the prologue to create the frame. */
1027 int regno = (insn >> 8) & 0x7;
1028 pv_t addr;
1030 offset = (insn & 0xff) << 2;
1031 addr = pv_add_constant (regs[ARM_SP_REGNUM], offset);
1033 if (stack.store_would_trash (addr))
1034 break;
1036 stack.store (addr, 4, regs[regno]);
1038 else if ((insn & 0xf800) == 0x6000) /* str rd, [rn, #off] */
1040 int rd = bits (insn, 0, 2);
1041 int rn = bits (insn, 3, 5);
1042 pv_t addr;
1044 offset = bits (insn, 6, 10) << 2;
1045 addr = pv_add_constant (regs[rn], offset);
1047 if (stack.store_would_trash (addr))
1048 break;
1050 stack.store (addr, 4, regs[rd]);
1052 else if (((insn & 0xf800) == 0x7000 /* strb Rd, [Rn, #off] */
1053 || (insn & 0xf800) == 0x8000) /* strh Rd, [Rn, #off] */
1054 && pv_is_register (regs[bits (insn, 3, 5)], ARM_SP_REGNUM))
1055 /* Ignore stores of argument registers to the stack. */
1057 else if ((insn & 0xf800) == 0xc800 /* ldmia Rn!, { registers } */
1058 && pv_is_register (regs[bits (insn, 8, 10)], ARM_SP_REGNUM))
1059 /* Ignore block loads from the stack, potentially copying
1060 parameters from memory. */
1062 else if ((insn & 0xf800) == 0x9800 /* ldr Rd, [Rn, #immed] */
1063 || ((insn & 0xf800) == 0x6800 /* ldr Rd, [sp, #immed] */
1064 && pv_is_register (regs[bits (insn, 3, 5)], ARM_SP_REGNUM)))
1065 /* Similarly ignore single loads from the stack. */
1067 else if ((insn & 0xffc0) == 0x0000 /* lsls Rd, Rm, #0 */
1068 || (insn & 0xffc0) == 0x1c00) /* add Rd, Rn, #0 */
1069 /* Skip register copies, i.e. saves to another register
1070 instead of the stack. */
1072 else if ((insn & 0xf800) == 0x2000) /* movs Rd, #imm */
1073 /* Recognize constant loads; even with small stacks these are necessary
1074 on Thumb. */
1075 regs[bits (insn, 8, 10)] = pv_constant (bits (insn, 0, 7));
1076 else if ((insn & 0xf800) == 0x4800) /* ldr Rd, [pc, #imm] */
1078 /* Constant pool loads, for the same reason. */
1079 unsigned int constant;
1080 CORE_ADDR loc;
1082 loc = start + 4 + bits (insn, 0, 7) * 4;
1083 constant = read_memory_unsigned_integer (loc, 4, byte_order);
1084 regs[bits (insn, 8, 10)] = pv_constant (constant);
1086 else if (thumb_insn_size (insn) == 4) /* 32-bit Thumb-2 instructions. */
1088 unsigned short inst2;
1090 inst2 = read_code_unsigned_integer (start + 2, 2,
1091 byte_order_for_code);
1092 uint32_t whole_insn = (insn << 16) | inst2;
1094 if ((insn & 0xf800) == 0xf000 && (inst2 & 0xe800) == 0xe800)
1096 /* BL, BLX. Allow some special function calls when
1097 skipping the prologue; GCC generates these before
1098 storing arguments to the stack. */
1099 CORE_ADDR nextpc;
1100 int j1, j2, imm1, imm2;
1102 imm1 = sbits (insn, 0, 10);
1103 imm2 = bits (inst2, 0, 10);
1104 j1 = bit (inst2, 13);
1105 j2 = bit (inst2, 11);
1107 offset = ((imm1 << 12) + (imm2 << 1));
1108 offset ^= ((!j2) << 22) | ((!j1) << 23);
1110 nextpc = start + 4 + offset;
1111 /* For BLX make sure to clear the low bits. */
1112 if (bit (inst2, 12) == 0)
1113 nextpc = nextpc & 0xfffffffc;
1115 if (!skip_prologue_function (gdbarch, nextpc,
1116 bit (inst2, 12) != 0))
1117 break;
1120 else if ((insn & 0xffd0) == 0xe900 /* stmdb Rn{!},
1121 { registers } */
1122 && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
1124 pv_t addr = regs[bits (insn, 0, 3)];
1125 int regno;
1127 if (stack.store_would_trash (addr))
1128 break;
1130 /* Calculate offsets of saved registers. */
1131 for (regno = ARM_LR_REGNUM; regno >= 0; regno--)
1132 if (inst2 & (1 << regno))
1134 addr = pv_add_constant (addr, -4);
1135 stack.store (addr, 4, regs[regno]);
1138 if (insn & 0x0020)
1139 regs[bits (insn, 0, 3)] = addr;
1142 /* vstmdb Rn{!}, { D-registers } (aka vpush). */
1143 else if ((insn & 0xff20) == 0xed20
1144 && (inst2 & 0x0f00) == 0x0b00
1145 && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
1147 /* Address SP points to. */
1148 pv_t addr = regs[bits (insn, 0, 3)];
1150 /* Number of registers saved. */
1151 unsigned int number = bits (inst2, 0, 7) >> 1;
1153 /* First register to save. */
1154 int vd = bits (inst2, 12, 15) | (bits (insn, 6, 6) << 4);
1156 if (stack.store_would_trash (addr))
1157 break;
1159 /* Calculate offsets of saved registers. */
1160 for (; number > 0; number--)
1162 addr = pv_add_constant (addr, -8);
1163 stack.store (addr, 8, pv_register (ARM_D0_REGNUM
1164 + vd + number, 0));
1167 /* Writeback SP to account for the saved registers. */
1168 regs[bits (insn, 0, 3)] = addr;
1171 else if ((insn & 0xff50) == 0xe940 /* strd Rt, Rt2,
1172 [Rn, #+/-imm]{!} */
1173 && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
1175 int regno1 = bits (inst2, 12, 15);
1176 int regno2 = bits (inst2, 8, 11);
1177 pv_t addr = regs[bits (insn, 0, 3)];
1179 offset = inst2 & 0xff;
1180 if (insn & 0x0080)
1181 addr = pv_add_constant (addr, offset);
1182 else
1183 addr = pv_add_constant (addr, -offset);
1185 if (stack.store_would_trash (addr))
1186 break;
1188 stack.store (addr, 4, regs[regno1]);
1189 stack.store (pv_add_constant (addr, 4),
1190 4, regs[regno2]);
1192 if (insn & 0x0020)
1193 regs[bits (insn, 0, 3)] = addr;
1196 else if ((insn & 0xfff0) == 0xf8c0 /* str Rt,[Rn,+/-#imm]{!} */
1197 && (inst2 & 0x0c00) == 0x0c00
1198 && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
1200 int regno = bits (inst2, 12, 15);
1201 pv_t addr = regs[bits (insn, 0, 3)];
1203 offset = inst2 & 0xff;
1204 if (inst2 & 0x0200)
1205 addr = pv_add_constant (addr, offset);
1206 else
1207 addr = pv_add_constant (addr, -offset);
1209 if (stack.store_would_trash (addr))
1210 break;
1212 stack.store (addr, 4, regs[regno]);
1214 if (inst2 & 0x0100)
1215 regs[bits (insn, 0, 3)] = addr;
1218 else if ((insn & 0xfff0) == 0xf8c0 /* str.w Rt,[Rn,#imm] */
1219 && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
1221 int regno = bits (inst2, 12, 15);
1222 pv_t addr;
1224 offset = inst2 & 0xfff;
1225 addr = pv_add_constant (regs[bits (insn, 0, 3)], offset);
1227 if (stack.store_would_trash (addr))
1228 break;
1230 stack.store (addr, 4, regs[regno]);
1233 else if ((insn & 0xffd0) == 0xf880 /* str{bh}.w Rt,[Rn,#imm] */
1234 && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
1235 /* Ignore stores of argument registers to the stack. */
1238 else if ((insn & 0xffd0) == 0xf800 /* str{bh} Rt,[Rn,#+/-imm] */
1239 && (inst2 & 0x0d00) == 0x0c00
1240 && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
1241 /* Ignore stores of argument registers to the stack. */
1244 else if ((insn & 0xffd0) == 0xe890 /* ldmia Rn[!],
1245 { registers } */
1246 && (inst2 & 0x8000) == 0x0000
1247 && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
1248 /* Ignore block loads from the stack, potentially copying
1249 parameters from memory. */
1252 else if ((insn & 0xff70) == 0xe950 /* ldrd Rt, Rt2,
1253 [Rn, #+/-imm] */
1254 && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
1255 /* Similarly ignore dual loads from the stack. */
1258 else if ((insn & 0xfff0) == 0xf850 /* ldr Rt,[Rn,#+/-imm] */
1259 && (inst2 & 0x0d00) == 0x0c00
1260 && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
1261 /* Similarly ignore single loads from the stack. */
1264 else if ((insn & 0xfff0) == 0xf8d0 /* ldr.w Rt,[Rn,#imm] */
1265 && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
1266 /* Similarly ignore single loads from the stack. */
1269 else if ((insn & 0xfbf0) == 0xf100 /* add.w Rd, Rn, #imm */
1270 && (inst2 & 0x8000) == 0x0000)
1272 unsigned int imm = ((bits (insn, 10, 10) << 11)
1273 | (bits (inst2, 12, 14) << 8)
1274 | bits (inst2, 0, 7));
1276 regs[bits (inst2, 8, 11)]
1277 = pv_add_constant (regs[bits (insn, 0, 3)],
1278 thumb_expand_immediate (imm));
1281 else if ((insn & 0xfbf0) == 0xf200 /* addw Rd, Rn, #imm */
1282 && (inst2 & 0x8000) == 0x0000)
1284 unsigned int imm = ((bits (insn, 10, 10) << 11)
1285 | (bits (inst2, 12, 14) << 8)
1286 | bits (inst2, 0, 7));
1288 regs[bits (inst2, 8, 11)]
1289 = pv_add_constant (regs[bits (insn, 0, 3)], imm);
1292 else if ((insn & 0xfbf0) == 0xf1a0 /* sub.w Rd, Rn, #imm */
1293 && (inst2 & 0x8000) == 0x0000)
1295 unsigned int imm = ((bits (insn, 10, 10) << 11)
1296 | (bits (inst2, 12, 14) << 8)
1297 | bits (inst2, 0, 7));
1299 regs[bits (inst2, 8, 11)]
1300 = pv_add_constant (regs[bits (insn, 0, 3)],
1301 - (CORE_ADDR) thumb_expand_immediate (imm));
1304 else if ((insn & 0xfbf0) == 0xf2a0 /* subw Rd, Rn, #imm */
1305 && (inst2 & 0x8000) == 0x0000)
1307 unsigned int imm = ((bits (insn, 10, 10) << 11)
1308 | (bits (inst2, 12, 14) << 8)
1309 | bits (inst2, 0, 7));
1311 regs[bits (inst2, 8, 11)]
1312 = pv_add_constant (regs[bits (insn, 0, 3)], - (CORE_ADDR) imm);
1315 else if ((insn & 0xfbff) == 0xf04f) /* mov.w Rd, #const */
1317 unsigned int imm = ((bits (insn, 10, 10) << 11)
1318 | (bits (inst2, 12, 14) << 8)
1319 | bits (inst2, 0, 7));
1321 regs[bits (inst2, 8, 11)]
1322 = pv_constant (thumb_expand_immediate (imm));
1325 else if ((insn & 0xfbf0) == 0xf240) /* movw Rd, #const */
1327 unsigned int imm
1328 = EXTRACT_MOVW_MOVT_IMM_T (insn, inst2);
1330 regs[bits (inst2, 8, 11)] = pv_constant (imm);
1333 else if (insn == 0xea5f /* mov.w Rd,Rm */
1334 && (inst2 & 0xf0f0) == 0)
1336 int dst_reg = (inst2 & 0x0f00) >> 8;
1337 int src_reg = inst2 & 0xf;
1338 regs[dst_reg] = regs[src_reg];
1341 else if ((insn & 0xff7f) == 0xf85f) /* ldr.w Rt,<label> */
1343 /* Constant pool loads. */
1344 unsigned int constant;
1345 CORE_ADDR loc;
1347 offset = bits (inst2, 0, 11);
1348 if (insn & 0x0080)
1349 loc = start + 4 + offset;
1350 else
1351 loc = start + 4 - offset;
1353 constant = read_memory_unsigned_integer (loc, 4, byte_order);
1354 regs[bits (inst2, 12, 15)] = pv_constant (constant);
1357 else if ((insn & 0xff7f) == 0xe95f) /* ldrd Rt,Rt2,<label> */
1359 /* Constant pool loads. */
1360 unsigned int constant;
1361 CORE_ADDR loc;
1363 offset = bits (inst2, 0, 7) << 2;
1364 if (insn & 0x0080)
1365 loc = start + 4 + offset;
1366 else
1367 loc = start + 4 - offset;
1369 constant = read_memory_unsigned_integer (loc, 4, byte_order);
1370 regs[bits (inst2, 12, 15)] = pv_constant (constant);
1372 constant = read_memory_unsigned_integer (loc + 4, 4, byte_order);
1373 regs[bits (inst2, 8, 11)] = pv_constant (constant);
1375 /* Start of ARMv8.1-m PACBTI extension instructions. */
1376 else if (IS_PAC (whole_insn))
1378 /* LR and SP are input registers. PAC is in R12. LR is
1379 signed from this point onwards. NOP space. */
1380 ra_signed_state = true;
1382 else if (IS_PACBTI (whole_insn))
1384 /* LR and SP are input registers. PAC is in R12 and PC is a
1385 valid BTI landing pad. LR is signed from this point onwards.
1386 NOP space. */
1387 ra_signed_state = true;
1389 else if (IS_BTI (whole_insn))
1391 /* Valid BTI landing pad. NOP space. */
1393 else if (IS_PACG (whole_insn))
1395 /* Sign Rn using Rm and store the PAC in Rd. Rd is signed from
1396 this point onwards. */
1397 ra_signed_state = true;
1399 else if (IS_AUT (whole_insn) || IS_AUTG (whole_insn))
1401 /* These instructions appear close to the epilogue, when signed
1402 pointers are getting authenticated. */
1403 ra_signed_state = false;
1405 /* End of ARMv8.1-m PACBTI extension instructions */
1406 else if (thumb2_instruction_changes_pc (insn, inst2))
1408 /* Don't scan past anything that might change control flow. */
1409 break;
1411 else
1413 /* The optimizer might shove anything into the prologue,
1414 so we just skip what we don't recognize. */
1415 unrecognized_pc = start;
1418 /* Make sure we are dealing with a target that supports ARMv8.1-m
1419 PACBTI. */
1420 if (cache != nullptr && tdep->have_pacbti
1421 && ra_signed_state.has_value ())
1423 arm_debug_printf ("Found pacbti instruction at %s",
1424 paddress (gdbarch, start));
1425 arm_debug_printf ("RA is %s",
1426 *ra_signed_state? "signed" : "not signed");
1427 cache->ra_signed_state = ra_signed_state;
1430 start += 2;
1432 else if (thumb_instruction_changes_pc (insn))
1434 /* Don't scan past anything that might change control flow. */
1435 break;
1437 else
1439 /* The optimizer might shove anything into the prologue,
1440 so we just skip what we don't recognize. */
1441 unrecognized_pc = start;
1444 start += 2;
1447 arm_debug_printf ("Prologue scan stopped at %s",
1448 paddress (gdbarch, start));
1450 if (unrecognized_pc == 0)
1451 unrecognized_pc = start;
1453 if (cache == NULL)
1454 return unrecognized_pc;
1456 if (pv_is_register (regs[ARM_FP_REGNUM], ARM_SP_REGNUM))
1458 /* Frame pointer is fp. Frame size is constant. */
1459 cache->framereg = ARM_FP_REGNUM;
1460 cache->framesize = -regs[ARM_FP_REGNUM].k;
1462 else if (pv_is_register (regs[THUMB_FP_REGNUM], ARM_SP_REGNUM))
1464 /* Frame pointer is r7. Frame size is constant. */
1465 cache->framereg = THUMB_FP_REGNUM;
1466 cache->framesize = -regs[THUMB_FP_REGNUM].k;
1468 else
1470 /* Try the stack pointer... this is a bit desperate. */
1471 cache->framereg = ARM_SP_REGNUM;
1472 cache->framesize = -regs[ARM_SP_REGNUM].k;
1475 for (i = 0; i < gdbarch_num_regs (gdbarch); i++)
1476 if (stack.find_reg (gdbarch, i, &offset))
1478 cache->saved_regs[i].set_addr (offset);
1479 if (i == ARM_SP_REGNUM)
1480 arm_cache_set_active_sp_value(cache, tdep, offset);
1483 return unrecognized_pc;
1487 /* Try to analyze the instructions starting from PC, which load symbol
1488 __stack_chk_guard. Return the address of instruction after loading this
1489 symbol, set the dest register number to *BASEREG, and set the size of
1490 instructions for loading symbol in OFFSET. Return 0 if instructions are
1491 not recognized. */
1493 static CORE_ADDR
1494 arm_analyze_load_stack_chk_guard(CORE_ADDR pc, struct gdbarch *gdbarch,
1495 unsigned int *destreg, int *offset)
1497 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
1498 int is_thumb = arm_pc_is_thumb (gdbarch, pc);
1499 unsigned int low, high, address;
1501 address = 0;
1502 if (is_thumb)
1504 unsigned short insn1
1505 = read_code_unsigned_integer (pc, 2, byte_order_for_code);
1507 if ((insn1 & 0xf800) == 0x4800) /* ldr Rd, #immed */
1509 *destreg = bits (insn1, 8, 10);
1510 *offset = 2;
1511 address = (pc & 0xfffffffc) + 4 + (bits (insn1, 0, 7) << 2);
1512 address = read_memory_unsigned_integer (address, 4,
1513 byte_order_for_code);
1515 else if ((insn1 & 0xfbf0) == 0xf240) /* movw Rd, #const */
1517 unsigned short insn2
1518 = read_code_unsigned_integer (pc + 2, 2, byte_order_for_code);
1520 low = EXTRACT_MOVW_MOVT_IMM_T (insn1, insn2);
1522 insn1
1523 = read_code_unsigned_integer (pc + 4, 2, byte_order_for_code);
1524 insn2
1525 = read_code_unsigned_integer (pc + 6, 2, byte_order_for_code);
1527 /* movt Rd, #const */
1528 if ((insn1 & 0xfbc0) == 0xf2c0)
1530 high = EXTRACT_MOVW_MOVT_IMM_T (insn1, insn2);
1531 *destreg = bits (insn2, 8, 11);
1532 *offset = 8;
1533 address = (high << 16 | low);
1537 else
1539 unsigned int insn
1540 = read_code_unsigned_integer (pc, 4, byte_order_for_code);
1542 if ((insn & 0x0e5f0000) == 0x041f0000) /* ldr Rd, [PC, #immed] */
1544 address = bits (insn, 0, 11) + pc + 8;
1545 address = read_memory_unsigned_integer (address, 4,
1546 byte_order_for_code);
1548 *destreg = bits (insn, 12, 15);
1549 *offset = 4;
1551 else if ((insn & 0x0ff00000) == 0x03000000) /* movw Rd, #const */
1553 low = EXTRACT_MOVW_MOVT_IMM_A (insn);
1555 insn
1556 = read_code_unsigned_integer (pc + 4, 4, byte_order_for_code);
1558 if ((insn & 0x0ff00000) == 0x03400000) /* movt Rd, #const */
1560 high = EXTRACT_MOVW_MOVT_IMM_A (insn);
1561 *destreg = bits (insn, 12, 15);
1562 *offset = 8;
1563 address = (high << 16 | low);
1568 return address;
1571 /* Try to skip a sequence of instructions used for stack protector. If PC
1572 points to the first instruction of this sequence, return the address of
1573 first instruction after this sequence, otherwise, return original PC.
1575 On arm, this sequence of instructions is composed of mainly three steps,
1576 Step 1: load symbol __stack_chk_guard,
1577 Step 2: load from address of __stack_chk_guard,
1578 Step 3: store it to somewhere else.
1580 Usually, instructions on step 2 and step 3 are the same on various ARM
1581 architectures. On step 2, it is one instruction 'ldr Rx, [Rn, #0]', and
1582 on step 3, it is also one instruction 'str Rx, [r7, #immd]'. However,
1583 instructions in step 1 vary from different ARM architectures. On ARMv7,
1584 they are,
1586 movw Rn, #:lower16:__stack_chk_guard
1587 movt Rn, #:upper16:__stack_chk_guard
1589 On ARMv5t, it is,
1591 ldr Rn, .Label
1592 ....
1593 .Lable:
1594 .word __stack_chk_guard
1596 Since ldr/str is a very popular instruction, we can't use them as
1597 'fingerprint' or 'signature' of stack protector sequence. Here we choose
1598 sequence {movw/movt, ldr}/ldr/str plus symbol __stack_chk_guard, if not
1599 stripped, as the 'fingerprint' of a stack protector cdoe sequence. */
1601 static CORE_ADDR
1602 arm_skip_stack_protector(CORE_ADDR pc, struct gdbarch *gdbarch)
1604 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
1605 unsigned int basereg;
1606 struct bound_minimal_symbol stack_chk_guard;
1607 int offset;
1608 int is_thumb = arm_pc_is_thumb (gdbarch, pc);
1609 CORE_ADDR addr;
1611 /* Try to parse the instructions in Step 1. */
1612 addr = arm_analyze_load_stack_chk_guard (pc, gdbarch,
1613 &basereg, &offset);
1614 if (!addr)
1615 return pc;
1617 stack_chk_guard = lookup_minimal_symbol_by_pc (addr);
1618 /* ADDR must correspond to a symbol whose name is __stack_chk_guard.
1619 Otherwise, this sequence cannot be for stack protector. */
1620 if (stack_chk_guard.minsym == NULL
1621 || !startswith (stack_chk_guard.minsym->linkage_name (), "__stack_chk_guard"))
1622 return pc;
1624 if (is_thumb)
1626 unsigned int destreg;
1627 unsigned short insn
1628 = read_code_unsigned_integer (pc + offset, 2, byte_order_for_code);
1630 /* Step 2: ldr Rd, [Rn, #immed], encoding T1. */
1631 if ((insn & 0xf800) != 0x6800)
1632 return pc;
1633 if (bits (insn, 3, 5) != basereg)
1634 return pc;
1635 destreg = bits (insn, 0, 2);
1637 insn = read_code_unsigned_integer (pc + offset + 2, 2,
1638 byte_order_for_code);
1639 /* Step 3: str Rd, [Rn, #immed], encoding T1. */
1640 if ((insn & 0xf800) != 0x6000)
1641 return pc;
1642 if (destreg != bits (insn, 0, 2))
1643 return pc;
1645 else
1647 unsigned int destreg;
1648 unsigned int insn
1649 = read_code_unsigned_integer (pc + offset, 4, byte_order_for_code);
1651 /* Step 2: ldr Rd, [Rn, #immed], encoding A1. */
1652 if ((insn & 0x0e500000) != 0x04100000)
1653 return pc;
1654 if (bits (insn, 16, 19) != basereg)
1655 return pc;
1656 destreg = bits (insn, 12, 15);
1657 /* Step 3: str Rd, [Rn, #immed], encoding A1. */
1658 insn = read_code_unsigned_integer (pc + offset + 4,
1659 4, byte_order_for_code);
1660 if ((insn & 0x0e500000) != 0x04000000)
1661 return pc;
1662 if (bits (insn, 12, 15) != destreg)
1663 return pc;
1665 /* The size of total two instructions ldr/str is 4 on Thumb-2, while 8
1666 on arm. */
1667 if (is_thumb)
1668 return pc + offset + 4;
1669 else
1670 return pc + offset + 8;
1673 /* Advance the PC across any function entry prologue instructions to
1674 reach some "real" code.
1676 The APCS (ARM Procedure Call Standard) defines the following
1677 prologue:
1679 mov ip, sp
1680 [stmfd sp!, {a1,a2,a3,a4}]
1681 stmfd sp!, {...,fp,ip,lr,pc}
1682 [stfe f7, [sp, #-12]!]
1683 [stfe f6, [sp, #-12]!]
1684 [stfe f5, [sp, #-12]!]
1685 [stfe f4, [sp, #-12]!]
1686 sub fp, ip, #nn @@ nn == 20 or 4 depending on second insn. */
1688 static CORE_ADDR
1689 arm_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR pc)
1691 CORE_ADDR func_addr, limit_pc;
1693 /* See if we can determine the end of the prologue via the symbol table.
1694 If so, then return either PC, or the PC after the prologue, whichever
1695 is greater. */
1696 if (find_pc_partial_function (pc, NULL, &func_addr, NULL))
1698 CORE_ADDR post_prologue_pc
1699 = skip_prologue_using_sal (gdbarch, func_addr);
1700 struct compunit_symtab *cust = find_pc_compunit_symtab (func_addr);
1702 if (post_prologue_pc)
1703 post_prologue_pc
1704 = arm_skip_stack_protector (post_prologue_pc, gdbarch);
1707 /* GCC always emits a line note before the prologue and another
1708 one after, even if the two are at the same address or on the
1709 same line. Take advantage of this so that we do not need to
1710 know every instruction that might appear in the prologue. We
1711 will have producer information for most binaries; if it is
1712 missing (e.g. for -gstabs), assuming the GNU tools. */
1713 if (post_prologue_pc
1714 && (cust == NULL
1715 || cust->producer () == NULL
1716 || startswith (cust->producer (), "GNU ")
1717 || producer_is_llvm (cust->producer ())))
1718 return post_prologue_pc;
1720 if (post_prologue_pc != 0)
1722 CORE_ADDR analyzed_limit;
1724 /* For non-GCC compilers, make sure the entire line is an
1725 acceptable prologue; GDB will round this function's
1726 return value up to the end of the following line so we
1727 can not skip just part of a line (and we do not want to).
1729 RealView does not treat the prologue specially, but does
1730 associate prologue code with the opening brace; so this
1731 lets us skip the first line if we think it is the opening
1732 brace. */
1733 if (arm_pc_is_thumb (gdbarch, func_addr))
1734 analyzed_limit = thumb_analyze_prologue (gdbarch, func_addr,
1735 post_prologue_pc, NULL);
1736 else
1737 analyzed_limit
1738 = arm_analyze_prologue (gdbarch, func_addr, post_prologue_pc,
1739 NULL, target_arm_instruction_reader ());
1741 if (analyzed_limit != post_prologue_pc)
1742 return func_addr;
1744 return post_prologue_pc;
1748 /* Can't determine prologue from the symbol table, need to examine
1749 instructions. */
1751 /* Find an upper limit on the function prologue using the debug
1752 information. If the debug information could not be used to provide
1753 that bound, then use an arbitrary large number as the upper bound. */
1754 /* Like arm_scan_prologue, stop no later than pc + 64. */
1755 limit_pc = skip_prologue_using_sal (gdbarch, pc);
1756 if (limit_pc == 0)
1757 limit_pc = pc + 64; /* Magic. */
1760 /* Check if this is Thumb code. */
1761 if (arm_pc_is_thumb (gdbarch, pc))
1762 return thumb_analyze_prologue (gdbarch, pc, limit_pc, NULL);
1763 else
1764 return arm_analyze_prologue (gdbarch, pc, limit_pc, NULL,
1765 target_arm_instruction_reader ());
1768 /* *INDENT-OFF* */
1769 /* Function: thumb_scan_prologue (helper function for arm_scan_prologue)
1770 This function decodes a Thumb function prologue to determine:
1771 1) the size of the stack frame
1772 2) which registers are saved on it
1773 3) the offsets of saved regs
1774 4) the offset from the stack pointer to the frame pointer
1776 A typical Thumb function prologue would create this stack frame
1777 (offsets relative to FP)
1778 old SP -> 24 stack parameters
1779 20 LR
1780 16 R7
1781 R7 -> 0 local variables (16 bytes)
1782 SP -> -12 additional stack space (12 bytes)
1783 The frame size would thus be 36 bytes, and the frame offset would be
1784 12 bytes. The frame register is R7.
1786 The comments for thumb_skip_prolog() describe the algorithm we use
1787 to detect the end of the prolog. */
1788 /* *INDENT-ON* */
1790 static void
1791 thumb_scan_prologue (struct gdbarch *gdbarch, CORE_ADDR prev_pc,
1792 CORE_ADDR block_addr, struct arm_prologue_cache *cache)
1794 CORE_ADDR prologue_start;
1795 CORE_ADDR prologue_end;
1797 if (find_pc_partial_function (block_addr, NULL, &prologue_start,
1798 &prologue_end))
1800 /* See comment in arm_scan_prologue for an explanation of
1801 this heuristics. */
1802 if (prologue_end > prologue_start + 64)
1804 prologue_end = prologue_start + 64;
1807 else
1808 /* We're in the boondocks: we have no idea where the start of the
1809 function is. */
1810 return;
1812 prologue_end = std::min (prologue_end, prev_pc);
1814 thumb_analyze_prologue (gdbarch, prologue_start, prologue_end, cache);
1817 /* Return 1 if the ARM instruction INSN restores SP in epilogue, 0
1818 otherwise. */
1820 static int
1821 arm_instruction_restores_sp (unsigned int insn)
1823 if (bits (insn, 28, 31) != INST_NV)
1825 if ((insn & 0x0df0f000) == 0x0080d000
1826 /* ADD SP (register or immediate). */
1827 || (insn & 0x0df0f000) == 0x0040d000
1828 /* SUB SP (register or immediate). */
1829 || (insn & 0x0ffffff0) == 0x01a0d000
1830 /* MOV SP. */
1831 || (insn & 0x0fff0000) == 0x08bd0000
1832 /* POP (LDMIA). */
1833 || (insn & 0x0fff0000) == 0x049d0000)
1834 /* POP of a single register. */
1835 return 1;
1838 return 0;
1841 /* Implement immediate value decoding, as described in section A5.2.4
1842 (Modified immediate constants in ARM instructions) of the ARM Architecture
1843 Reference Manual (ARMv7-A and ARMv7-R edition). */
1845 static uint32_t
1846 arm_expand_immediate (uint32_t imm)
1848 /* Immediate values are 12 bits long. */
1849 gdb_assert ((imm & 0xfffff000) == 0);
1851 uint32_t unrotated_value = imm & 0xff;
1852 uint32_t rotate_amount = (imm & 0xf00) >> 7;
1854 if (rotate_amount == 0)
1855 return unrotated_value;
1857 return ((unrotated_value >> rotate_amount)
1858 | (unrotated_value << (32 - rotate_amount)));
1861 /* Analyze an ARM mode prologue starting at PROLOGUE_START and
1862 continuing no further than PROLOGUE_END. If CACHE is non-NULL,
1863 fill it in. Return the first address not recognized as a prologue
1864 instruction.
1866 We recognize all the instructions typically found in ARM prologues,
1867 plus harmless instructions which can be skipped (either for analysis
1868 purposes, or a more restrictive set that can be skipped when finding
1869 the end of the prologue). */
1871 static CORE_ADDR
1872 arm_analyze_prologue (struct gdbarch *gdbarch,
1873 CORE_ADDR prologue_start, CORE_ADDR prologue_end,
1874 struct arm_prologue_cache *cache,
1875 const arm_instruction_reader &insn_reader)
1877 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
1878 int regno;
1879 CORE_ADDR offset, current_pc;
1880 pv_t regs[ARM_FPS_REGNUM];
1881 CORE_ADDR unrecognized_pc = 0;
1882 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
1884 /* Search the prologue looking for instructions that set up the
1885 frame pointer, adjust the stack pointer, and save registers.
1887 Be careful, however, and if it doesn't look like a prologue,
1888 don't try to scan it. If, for instance, a frameless function
1889 begins with stmfd sp!, then we will tell ourselves there is
1890 a frame, which will confuse stack traceback, as well as "finish"
1891 and other operations that rely on a knowledge of the stack
1892 traceback. */
1894 for (regno = 0; regno < ARM_FPS_REGNUM; regno++)
1895 regs[regno] = pv_register (regno, 0);
1896 pv_area stack (ARM_SP_REGNUM, gdbarch_addr_bit (gdbarch));
1898 for (current_pc = prologue_start;
1899 current_pc < prologue_end;
1900 current_pc += 4)
1902 uint32_t insn = insn_reader.read (current_pc, byte_order_for_code);
1904 if (insn == 0xe1a0c00d) /* mov ip, sp */
1906 regs[ARM_IP_REGNUM] = regs[ARM_SP_REGNUM];
1907 continue;
1909 else if ((insn & 0xfff00000) == 0xe2800000 /* add Rd, Rn, #n */
1910 && pv_is_register (regs[bits (insn, 16, 19)], ARM_SP_REGNUM))
1912 uint32_t imm = arm_expand_immediate (insn & 0xfff);
1913 int rd = bits (insn, 12, 15);
1914 regs[rd] = pv_add_constant (regs[bits (insn, 16, 19)], imm);
1915 continue;
1917 else if ((insn & 0xfff00000) == 0xe2400000 /* sub Rd, Rn, #n */
1918 && pv_is_register (regs[bits (insn, 16, 19)], ARM_SP_REGNUM))
1920 uint32_t imm = arm_expand_immediate (insn & 0xfff);
1921 int rd = bits (insn, 12, 15);
1922 regs[rd] = pv_add_constant (regs[bits (insn, 16, 19)], -imm);
1923 continue;
1925 else if ((insn & 0xffff0fff) == 0xe52d0004) /* str Rd,
1926 [sp, #-4]! */
1928 if (stack.store_would_trash (regs[ARM_SP_REGNUM]))
1929 break;
1930 regs[ARM_SP_REGNUM] = pv_add_constant (regs[ARM_SP_REGNUM], -4);
1931 stack.store (regs[ARM_SP_REGNUM], 4,
1932 regs[bits (insn, 12, 15)]);
1933 continue;
1935 else if ((insn & 0xffff0000) == 0xe92d0000)
1936 /* stmfd sp!, {..., fp, ip, lr, pc}
1938 stmfd sp!, {a1, a2, a3, a4} */
1940 int mask = insn & 0xffff;
1942 if (stack.store_would_trash (regs[ARM_SP_REGNUM]))
1943 break;
1945 /* Calculate offsets of saved registers. */
1946 for (regno = ARM_PC_REGNUM; regno >= 0; regno--)
1947 if (mask & (1 << regno))
1949 regs[ARM_SP_REGNUM]
1950 = pv_add_constant (regs[ARM_SP_REGNUM], -4);
1951 stack.store (regs[ARM_SP_REGNUM], 4, regs[regno]);
1954 else if ((insn & 0xffff0000) == 0xe54b0000 /* strb rx,[r11,#-n] */
1955 || (insn & 0xffff00f0) == 0xe14b00b0 /* strh rx,[r11,#-n] */
1956 || (insn & 0xffffc000) == 0xe50b0000) /* str rx,[r11,#-n] */
1958 /* No need to add this to saved_regs -- it's just an arg reg. */
1959 continue;
1961 else if ((insn & 0xffff0000) == 0xe5cd0000 /* strb rx,[sp,#n] */
1962 || (insn & 0xffff00f0) == 0xe1cd00b0 /* strh rx,[sp,#n] */
1963 || (insn & 0xffffc000) == 0xe58d0000) /* str rx,[sp,#n] */
1965 /* No need to add this to saved_regs -- it's just an arg reg. */
1966 continue;
1968 else if ((insn & 0xfff00000) == 0xe8800000 /* stm Rn,
1969 { registers } */
1970 && pv_is_register (regs[bits (insn, 16, 19)], ARM_SP_REGNUM))
1972 /* No need to add this to saved_regs -- it's just arg regs. */
1973 continue;
1975 else if ((insn & 0xfffff000) == 0xe24cb000) /* sub fp, ip #n */
1977 uint32_t imm = arm_expand_immediate (insn & 0xfff);
1978 regs[ARM_FP_REGNUM] = pv_add_constant (regs[ARM_IP_REGNUM], -imm);
1980 else if ((insn & 0xfffff000) == 0xe24dd000) /* sub sp, sp #n */
1982 uint32_t imm = arm_expand_immediate(insn & 0xfff);
1983 regs[ARM_SP_REGNUM] = pv_add_constant (regs[ARM_SP_REGNUM], -imm);
1985 else if ((insn & 0xffff7fff) == 0xed6d0103 /* stfe f?,
1986 [sp, -#c]! */
1987 && tdep->have_fpa_registers)
1989 if (stack.store_would_trash (regs[ARM_SP_REGNUM]))
1990 break;
1992 regs[ARM_SP_REGNUM] = pv_add_constant (regs[ARM_SP_REGNUM], -12);
1993 regno = ARM_F0_REGNUM + ((insn >> 12) & 0x07);
1994 stack.store (regs[ARM_SP_REGNUM], 12, regs[regno]);
1996 else if ((insn & 0xffbf0fff) == 0xec2d0200 /* sfmfd f0, 4,
1997 [sp!] */
1998 && tdep->have_fpa_registers)
2000 int n_saved_fp_regs;
2001 unsigned int fp_start_reg, fp_bound_reg;
2003 if (stack.store_would_trash (regs[ARM_SP_REGNUM]))
2004 break;
2006 if ((insn & 0x800) == 0x800) /* N0 is set */
2008 if ((insn & 0x40000) == 0x40000) /* N1 is set */
2009 n_saved_fp_regs = 3;
2010 else
2011 n_saved_fp_regs = 1;
2013 else
2015 if ((insn & 0x40000) == 0x40000) /* N1 is set */
2016 n_saved_fp_regs = 2;
2017 else
2018 n_saved_fp_regs = 4;
2021 fp_start_reg = ARM_F0_REGNUM + ((insn >> 12) & 0x7);
2022 fp_bound_reg = fp_start_reg + n_saved_fp_regs;
2023 for (; fp_start_reg < fp_bound_reg; fp_start_reg++)
2025 regs[ARM_SP_REGNUM] = pv_add_constant (regs[ARM_SP_REGNUM], -12);
2026 stack.store (regs[ARM_SP_REGNUM], 12,
2027 regs[fp_start_reg++]);
2030 else if ((insn & 0xff000000) == 0xeb000000 && cache == NULL) /* bl */
2032 /* Allow some special function calls when skipping the
2033 prologue; GCC generates these before storing arguments to
2034 the stack. */
2035 CORE_ADDR dest = BranchDest (current_pc, insn);
2037 if (skip_prologue_function (gdbarch, dest, 0))
2038 continue;
2039 else
2040 break;
2042 else if ((insn & 0xf0000000) != 0xe0000000)
2043 break; /* Condition not true, exit early. */
2044 else if (arm_instruction_changes_pc (insn))
2045 /* Don't scan past anything that might change control flow. */
2046 break;
2047 else if (arm_instruction_restores_sp (insn))
2049 /* Don't scan past the epilogue. */
2050 break;
2052 else if ((insn & 0xfe500000) == 0xe8100000 /* ldm */
2053 && pv_is_register (regs[bits (insn, 16, 19)], ARM_SP_REGNUM))
2054 /* Ignore block loads from the stack, potentially copying
2055 parameters from memory. */
2056 continue;
2057 else if ((insn & 0xfc500000) == 0xe4100000
2058 && pv_is_register (regs[bits (insn, 16, 19)], ARM_SP_REGNUM))
2059 /* Similarly ignore single loads from the stack. */
2060 continue;
2061 else if ((insn & 0xffff0ff0) == 0xe1a00000)
2062 /* MOV Rd, Rm. Skip register copies, i.e. saves to another
2063 register instead of the stack. */
2064 continue;
2065 else
2067 /* The optimizer might shove anything into the prologue, if
2068 we build up cache (cache != NULL) from scanning prologue,
2069 we just skip what we don't recognize and scan further to
2070 make cache as complete as possible. However, if we skip
2071 prologue, we'll stop immediately on unrecognized
2072 instruction. */
2073 unrecognized_pc = current_pc;
2074 if (cache != NULL)
2075 continue;
2076 else
2077 break;
2081 if (unrecognized_pc == 0)
2082 unrecognized_pc = current_pc;
2084 if (cache)
2086 int framereg, framesize;
2088 /* The frame size is just the distance from the frame register
2089 to the original stack pointer. */
2090 if (pv_is_register (regs[ARM_FP_REGNUM], ARM_SP_REGNUM))
2092 /* Frame pointer is fp. */
2093 framereg = ARM_FP_REGNUM;
2094 framesize = -regs[ARM_FP_REGNUM].k;
2096 else
2098 /* Try the stack pointer... this is a bit desperate. */
2099 framereg = ARM_SP_REGNUM;
2100 framesize = -regs[ARM_SP_REGNUM].k;
2103 cache->framereg = framereg;
2104 cache->framesize = framesize;
2106 for (regno = 0; regno < ARM_FPS_REGNUM; regno++)
2107 if (stack.find_reg (gdbarch, regno, &offset))
2109 cache->saved_regs[regno].set_addr (offset);
2110 if (regno == ARM_SP_REGNUM)
2111 arm_cache_set_active_sp_value(cache, tdep, offset);
2115 arm_debug_printf ("Prologue scan stopped at %s",
2116 paddress (gdbarch, unrecognized_pc));
2118 return unrecognized_pc;
2121 static void
2122 arm_scan_prologue (struct frame_info *this_frame,
2123 struct arm_prologue_cache *cache)
2125 struct gdbarch *gdbarch = get_frame_arch (this_frame);
2126 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
2127 CORE_ADDR prologue_start, prologue_end;
2128 CORE_ADDR prev_pc = get_frame_pc (this_frame);
2129 CORE_ADDR block_addr = get_frame_address_in_block (this_frame);
2130 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
2132 /* Assume there is no frame until proven otherwise. */
2133 cache->framereg = ARM_SP_REGNUM;
2134 cache->framesize = 0;
2136 /* Check for Thumb prologue. */
2137 if (arm_frame_is_thumb (this_frame))
2139 thumb_scan_prologue (gdbarch, prev_pc, block_addr, cache);
2140 return;
2143 /* Find the function prologue. If we can't find the function in
2144 the symbol table, peek in the stack frame to find the PC. */
2145 if (find_pc_partial_function (block_addr, NULL, &prologue_start,
2146 &prologue_end))
2148 /* One way to find the end of the prologue (which works well
2149 for unoptimized code) is to do the following:
2151 struct symtab_and_line sal = find_pc_line (prologue_start, 0);
2153 if (sal.line == 0)
2154 prologue_end = prev_pc;
2155 else if (sal.end < prologue_end)
2156 prologue_end = sal.end;
2158 This mechanism is very accurate so long as the optimizer
2159 doesn't move any instructions from the function body into the
2160 prologue. If this happens, sal.end will be the last
2161 instruction in the first hunk of prologue code just before
2162 the first instruction that the scheduler has moved from
2163 the body to the prologue.
2165 In order to make sure that we scan all of the prologue
2166 instructions, we use a slightly less accurate mechanism which
2167 may scan more than necessary. To help compensate for this
2168 lack of accuracy, the prologue scanning loop below contains
2169 several clauses which'll cause the loop to terminate early if
2170 an implausible prologue instruction is encountered.
2172 The expression
2174 prologue_start + 64
2176 is a suitable endpoint since it accounts for the largest
2177 possible prologue plus up to five instructions inserted by
2178 the scheduler. */
2180 if (prologue_end > prologue_start + 64)
2182 prologue_end = prologue_start + 64; /* See above. */
2185 else
2187 /* We have no symbol information. Our only option is to assume this
2188 function has a standard stack frame and the normal frame register.
2189 Then, we can find the value of our frame pointer on entrance to
2190 the callee (or at the present moment if this is the innermost frame).
2191 The value stored there should be the address of the stmfd + 8. */
2192 CORE_ADDR frame_loc;
2193 ULONGEST return_value;
2195 /* AAPCS does not use a frame register, so we can abort here. */
2196 if (tdep->arm_abi == ARM_ABI_AAPCS)
2197 return;
2199 frame_loc = get_frame_register_unsigned (this_frame, ARM_FP_REGNUM);
2200 if (!safe_read_memory_unsigned_integer (frame_loc, 4, byte_order,
2201 &return_value))
2202 return;
2203 else
2205 prologue_start = gdbarch_addr_bits_remove
2206 (gdbarch, return_value) - 8;
2207 prologue_end = prologue_start + 64; /* See above. */
2211 if (prev_pc < prologue_end)
2212 prologue_end = prev_pc;
2214 arm_analyze_prologue (gdbarch, prologue_start, prologue_end, cache,
2215 target_arm_instruction_reader ());
2218 static struct arm_prologue_cache *
2219 arm_make_prologue_cache (struct frame_info *this_frame)
2221 int reg;
2222 struct arm_prologue_cache *cache;
2223 CORE_ADDR unwound_fp, prev_sp;
2225 cache = FRAME_OBSTACK_ZALLOC (struct arm_prologue_cache);
2226 arm_cache_init (cache, this_frame);
2228 arm_scan_prologue (this_frame, cache);
2230 unwound_fp = get_frame_register_unsigned (this_frame, cache->framereg);
2231 if (unwound_fp == 0)
2232 return cache;
2234 arm_gdbarch_tdep *tdep =
2235 gdbarch_tdep<arm_gdbarch_tdep> (get_frame_arch (this_frame));
2237 prev_sp = unwound_fp + cache->framesize;
2238 arm_cache_set_active_sp_value (cache, tdep, prev_sp);
2240 /* Calculate actual addresses of saved registers using offsets
2241 determined by arm_scan_prologue. */
2242 for (reg = 0; reg < gdbarch_num_regs (get_frame_arch (this_frame)); reg++)
2243 if (cache->saved_regs[reg].is_addr ())
2244 cache->saved_regs[reg].set_addr (cache->saved_regs[reg].addr () +
2245 prev_sp);
2247 return cache;
2250 /* Implementation of the stop_reason hook for arm_prologue frames. */
2252 static enum unwind_stop_reason
2253 arm_prologue_unwind_stop_reason (struct frame_info *this_frame,
2254 void **this_cache)
2256 struct arm_prologue_cache *cache;
2257 CORE_ADDR pc;
2259 if (*this_cache == NULL)
2260 *this_cache = arm_make_prologue_cache (this_frame);
2261 cache = (struct arm_prologue_cache *) *this_cache;
2263 /* This is meant to halt the backtrace at "_start". */
2264 pc = get_frame_pc (this_frame);
2265 gdbarch *arch = get_frame_arch (this_frame);
2266 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (arch);
2267 if (pc <= tdep->lowest_pc)
2268 return UNWIND_OUTERMOST;
2270 /* If we've hit a wall, stop. */
2271 if (arm_cache_get_prev_sp_value (cache, tdep) == 0)
2272 return UNWIND_OUTERMOST;
2274 return UNWIND_NO_REASON;
2277 /* Our frame ID for a normal frame is the current function's starting PC
2278 and the caller's SP when we were called. */
2280 static void
2281 arm_prologue_this_id (struct frame_info *this_frame,
2282 void **this_cache,
2283 struct frame_id *this_id)
2285 struct arm_prologue_cache *cache;
2286 struct frame_id id;
2287 CORE_ADDR pc, func;
2289 if (*this_cache == NULL)
2290 *this_cache = arm_make_prologue_cache (this_frame);
2291 cache = (struct arm_prologue_cache *) *this_cache;
2293 arm_gdbarch_tdep *tdep
2294 = gdbarch_tdep<arm_gdbarch_tdep> (get_frame_arch (this_frame));
2296 /* Use function start address as part of the frame ID. If we cannot
2297 identify the start address (due to missing symbol information),
2298 fall back to just using the current PC. */
2299 pc = get_frame_pc (this_frame);
2300 func = get_frame_func (this_frame);
2301 if (!func)
2302 func = pc;
2304 id = frame_id_build (arm_cache_get_prev_sp_value (cache, tdep), func);
2305 *this_id = id;
2308 static struct value *
2309 arm_prologue_prev_register (struct frame_info *this_frame,
2310 void **this_cache,
2311 int prev_regnum)
2313 struct gdbarch *gdbarch = get_frame_arch (this_frame);
2314 struct arm_prologue_cache *cache;
2315 CORE_ADDR sp_value;
2317 if (*this_cache == NULL)
2318 *this_cache = arm_make_prologue_cache (this_frame);
2319 cache = (struct arm_prologue_cache *) *this_cache;
2321 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
2323 /* If this frame has signed the return address, mark it as so. */
2324 if (tdep->have_pacbti && cache->ra_signed_state.has_value ()
2325 && *cache->ra_signed_state)
2326 set_frame_previous_pc_masked (this_frame);
2328 /* If we are asked to unwind the PC, then we need to return the LR
2329 instead. The prologue may save PC, but it will point into this
2330 frame's prologue, not the next frame's resume location. Also
2331 strip the saved T bit. A valid LR may have the low bit set, but
2332 a valid PC never does. */
2333 if (prev_regnum == ARM_PC_REGNUM)
2335 CORE_ADDR lr;
2337 lr = frame_unwind_register_unsigned (this_frame, ARM_LR_REGNUM);
2338 return frame_unwind_got_constant (this_frame, prev_regnum,
2339 arm_addr_bits_remove (gdbarch, lr));
2342 /* SP is generally not saved to the stack, but this frame is
2343 identified by the next frame's stack pointer at the time of the call.
2344 The value was already reconstructed into PREV_SP. */
2345 if (prev_regnum == ARM_SP_REGNUM)
2346 return frame_unwind_got_constant (this_frame, prev_regnum,
2347 arm_cache_get_prev_sp_value (cache, tdep));
2349 /* The value might be one of the alternative SP, if so, use the
2350 value already constructed. */
2351 if (arm_is_alternative_sp_register (tdep, prev_regnum))
2353 sp_value = arm_cache_get_sp_register (cache, tdep, prev_regnum);
2354 return frame_unwind_got_constant (this_frame, prev_regnum, sp_value);
2357 /* The CPSR may have been changed by the call instruction and by the
2358 called function. The only bit we can reconstruct is the T bit,
2359 by checking the low bit of LR as of the call. This is a reliable
2360 indicator of Thumb-ness except for some ARM v4T pre-interworking
2361 Thumb code, which could get away with a clear low bit as long as
2362 the called function did not use bx. Guess that all other
2363 bits are unchanged; the condition flags are presumably lost,
2364 but the processor status is likely valid. */
2365 if (prev_regnum == ARM_PS_REGNUM)
2367 ULONGEST cpsr = get_frame_register_unsigned (this_frame, prev_regnum);
2368 CORE_ADDR lr = frame_unwind_register_unsigned (this_frame, ARM_LR_REGNUM);
2370 cpsr = reconstruct_t_bit (gdbarch, lr, cpsr);
2371 return frame_unwind_got_constant (this_frame, prev_regnum, cpsr);
2374 return trad_frame_get_prev_register (this_frame, cache->saved_regs,
2375 prev_regnum);
2378 static frame_unwind arm_prologue_unwind = {
2379 "arm prologue",
2380 NORMAL_FRAME,
2381 arm_prologue_unwind_stop_reason,
2382 arm_prologue_this_id,
2383 arm_prologue_prev_register,
2384 NULL,
2385 default_frame_sniffer
2388 /* Maintain a list of ARM exception table entries per objfile, similar to the
2389 list of mapping symbols. We only cache entries for standard ARM-defined
2390 personality routines; the cache will contain only the frame unwinding
2391 instructions associated with the entry (not the descriptors). */
2393 struct arm_exidx_entry
2395 CORE_ADDR addr;
2396 gdb_byte *entry;
2398 bool operator< (const arm_exidx_entry &other) const
2400 return addr < other.addr;
2404 struct arm_exidx_data
2406 std::vector<std::vector<arm_exidx_entry>> section_maps;
2409 /* Per-BFD key to store exception handling information. */
2410 static const registry<bfd>::key<arm_exidx_data> arm_exidx_data_key;
2412 static struct obj_section *
2413 arm_obj_section_from_vma (struct objfile *objfile, bfd_vma vma)
2415 struct obj_section *osect;
2417 ALL_OBJFILE_OSECTIONS (objfile, osect)
2418 if (bfd_section_flags (osect->the_bfd_section) & SEC_ALLOC)
2420 bfd_vma start, size;
2421 start = bfd_section_vma (osect->the_bfd_section);
2422 size = bfd_section_size (osect->the_bfd_section);
2424 if (start <= vma && vma < start + size)
2425 return osect;
2428 return NULL;
2431 /* Parse contents of exception table and exception index sections
2432 of OBJFILE, and fill in the exception table entry cache.
2434 For each entry that refers to a standard ARM-defined personality
2435 routine, extract the frame unwinding instructions (from either
2436 the index or the table section). The unwinding instructions
2437 are normalized by:
2438 - extracting them from the rest of the table data
2439 - converting to host endianness
2440 - appending the implicit 0xb0 ("Finish") code
2442 The extracted and normalized instructions are stored for later
2443 retrieval by the arm_find_exidx_entry routine. */
2445 static void
2446 arm_exidx_new_objfile (struct objfile *objfile)
2448 struct arm_exidx_data *data;
2449 asection *exidx, *extab;
2450 bfd_vma exidx_vma = 0, extab_vma = 0;
2451 LONGEST i;
2453 /* If we've already touched this file, do nothing. */
2454 if (!objfile || arm_exidx_data_key.get (objfile->obfd.get ()) != NULL)
2455 return;
2457 /* Read contents of exception table and index. */
2458 exidx = bfd_get_section_by_name (objfile->obfd.get (),
2459 ELF_STRING_ARM_unwind);
2460 gdb::byte_vector exidx_data;
2461 if (exidx)
2463 exidx_vma = bfd_section_vma (exidx);
2464 exidx_data.resize (bfd_section_size (exidx));
2466 if (!bfd_get_section_contents (objfile->obfd.get (), exidx,
2467 exidx_data.data (), 0,
2468 exidx_data.size ()))
2469 return;
2472 extab = bfd_get_section_by_name (objfile->obfd.get (), ".ARM.extab");
2473 gdb::byte_vector extab_data;
2474 if (extab)
2476 extab_vma = bfd_section_vma (extab);
2477 extab_data.resize (bfd_section_size (extab));
2479 if (!bfd_get_section_contents (objfile->obfd.get (), extab,
2480 extab_data.data (), 0,
2481 extab_data.size ()))
2482 return;
2485 /* Allocate exception table data structure. */
2486 data = arm_exidx_data_key.emplace (objfile->obfd.get ());
2487 data->section_maps.resize (objfile->obfd->section_count);
2489 /* Fill in exception table. */
2490 for (i = 0; i < exidx_data.size () / 8; i++)
2492 struct arm_exidx_entry new_exidx_entry;
2493 bfd_vma idx = bfd_h_get_32 (objfile->obfd, exidx_data.data () + i * 8);
2494 bfd_vma val = bfd_h_get_32 (objfile->obfd,
2495 exidx_data.data () + i * 8 + 4);
2496 bfd_vma addr = 0, word = 0;
2497 int n_bytes = 0, n_words = 0;
2498 struct obj_section *sec;
2499 gdb_byte *entry = NULL;
2501 /* Extract address of start of function. */
2502 idx = ((idx & 0x7fffffff) ^ 0x40000000) - 0x40000000;
2503 idx += exidx_vma + i * 8;
2505 /* Find section containing function and compute section offset. */
2506 sec = arm_obj_section_from_vma (objfile, idx);
2507 if (sec == NULL)
2508 continue;
2509 idx -= bfd_section_vma (sec->the_bfd_section);
2511 /* Determine address of exception table entry. */
2512 if (val == 1)
2514 /* EXIDX_CANTUNWIND -- no exception table entry present. */
2516 else if ((val & 0xff000000) == 0x80000000)
2518 /* Exception table entry embedded in .ARM.exidx
2519 -- must be short form. */
2520 word = val;
2521 n_bytes = 3;
2523 else if (!(val & 0x80000000))
2525 /* Exception table entry in .ARM.extab. */
2526 addr = ((val & 0x7fffffff) ^ 0x40000000) - 0x40000000;
2527 addr += exidx_vma + i * 8 + 4;
2529 if (addr >= extab_vma && addr + 4 <= extab_vma + extab_data.size ())
2531 word = bfd_h_get_32 (objfile->obfd,
2532 extab_data.data () + addr - extab_vma);
2533 addr += 4;
2535 if ((word & 0xff000000) == 0x80000000)
2537 /* Short form. */
2538 n_bytes = 3;
2540 else if ((word & 0xff000000) == 0x81000000
2541 || (word & 0xff000000) == 0x82000000)
2543 /* Long form. */
2544 n_bytes = 2;
2545 n_words = ((word >> 16) & 0xff);
2547 else if (!(word & 0x80000000))
2549 bfd_vma pers;
2550 struct obj_section *pers_sec;
2551 int gnu_personality = 0;
2553 /* Custom personality routine. */
2554 pers = ((word & 0x7fffffff) ^ 0x40000000) - 0x40000000;
2555 pers = UNMAKE_THUMB_ADDR (pers + addr - 4);
2557 /* Check whether we've got one of the variants of the
2558 GNU personality routines. */
2559 pers_sec = arm_obj_section_from_vma (objfile, pers);
2560 if (pers_sec)
2562 static const char *personality[] =
2564 "__gcc_personality_v0",
2565 "__gxx_personality_v0",
2566 "__gcj_personality_v0",
2567 "__gnu_objc_personality_v0",
2568 NULL
2571 CORE_ADDR pc = pers + pers_sec->offset ();
2572 int k;
2574 for (k = 0; personality[k]; k++)
2575 if (lookup_minimal_symbol_by_pc_name
2576 (pc, personality[k], objfile))
2578 gnu_personality = 1;
2579 break;
2583 /* If so, the next word contains a word count in the high
2584 byte, followed by the same unwind instructions as the
2585 pre-defined forms. */
2586 if (gnu_personality
2587 && addr + 4 <= extab_vma + extab_data.size ())
2589 word = bfd_h_get_32 (objfile->obfd,
2590 (extab_data.data ()
2591 + addr - extab_vma));
2592 addr += 4;
2593 n_bytes = 3;
2594 n_words = ((word >> 24) & 0xff);
2600 /* Sanity check address. */
2601 if (n_words)
2602 if (addr < extab_vma
2603 || addr + 4 * n_words > extab_vma + extab_data.size ())
2604 n_words = n_bytes = 0;
2606 /* The unwind instructions reside in WORD (only the N_BYTES least
2607 significant bytes are valid), followed by N_WORDS words in the
2608 extab section starting at ADDR. */
2609 if (n_bytes || n_words)
2611 gdb_byte *p = entry
2612 = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack,
2613 n_bytes + n_words * 4 + 1);
2615 while (n_bytes--)
2616 *p++ = (gdb_byte) ((word >> (8 * n_bytes)) & 0xff);
2618 while (n_words--)
2620 word = bfd_h_get_32 (objfile->obfd,
2621 extab_data.data () + addr - extab_vma);
2622 addr += 4;
2624 *p++ = (gdb_byte) ((word >> 24) & 0xff);
2625 *p++ = (gdb_byte) ((word >> 16) & 0xff);
2626 *p++ = (gdb_byte) ((word >> 8) & 0xff);
2627 *p++ = (gdb_byte) (word & 0xff);
2630 /* Implied "Finish" to terminate the list. */
2631 *p++ = 0xb0;
2634 /* Push entry onto vector. They are guaranteed to always
2635 appear in order of increasing addresses. */
2636 new_exidx_entry.addr = idx;
2637 new_exidx_entry.entry = entry;
2638 data->section_maps[sec->the_bfd_section->index].push_back
2639 (new_exidx_entry);
2643 /* Search for the exception table entry covering MEMADDR. If one is found,
2644 return a pointer to its data. Otherwise, return 0. If START is non-NULL,
2645 set *START to the start of the region covered by this entry. */
2647 static gdb_byte *
2648 arm_find_exidx_entry (CORE_ADDR memaddr, CORE_ADDR *start)
2650 struct obj_section *sec;
2652 sec = find_pc_section (memaddr);
2653 if (sec != NULL)
2655 struct arm_exidx_data *data;
2656 struct arm_exidx_entry map_key = { memaddr - sec->addr (), 0 };
2658 data = arm_exidx_data_key.get (sec->objfile->obfd.get ());
2659 if (data != NULL)
2661 std::vector<arm_exidx_entry> &map
2662 = data->section_maps[sec->the_bfd_section->index];
2663 if (!map.empty ())
2665 auto idx = std::lower_bound (map.begin (), map.end (), map_key);
2667 /* std::lower_bound finds the earliest ordered insertion
2668 point. If the following symbol starts at this exact
2669 address, we use that; otherwise, the preceding
2670 exception table entry covers this address. */
2671 if (idx < map.end ())
2673 if (idx->addr == map_key.addr)
2675 if (start)
2676 *start = idx->addr + sec->addr ();
2677 return idx->entry;
2681 if (idx > map.begin ())
2683 idx = idx - 1;
2684 if (start)
2685 *start = idx->addr + sec->addr ();
2686 return idx->entry;
2692 return NULL;
2695 /* Given the current frame THIS_FRAME, and its associated frame unwinding
2696 instruction list from the ARM exception table entry ENTRY, allocate and
2697 return a prologue cache structure describing how to unwind this frame.
2699 Return NULL if the unwinding instruction list contains a "spare",
2700 "reserved" or "refuse to unwind" instruction as defined in section
2701 "9.3 Frame unwinding instructions" of the "Exception Handling ABI
2702 for the ARM Architecture" document. */
2704 static struct arm_prologue_cache *
2705 arm_exidx_fill_cache (struct frame_info *this_frame, gdb_byte *entry)
2707 CORE_ADDR vsp = 0;
2708 int vsp_valid = 0;
2710 struct arm_prologue_cache *cache;
2711 cache = FRAME_OBSTACK_ZALLOC (struct arm_prologue_cache);
2712 arm_cache_init (cache, this_frame);
2714 for (;;)
2716 gdb_byte insn;
2718 /* Whenever we reload SP, we actually have to retrieve its
2719 actual value in the current frame. */
2720 if (!vsp_valid)
2722 if (cache->saved_regs[ARM_SP_REGNUM].is_realreg ())
2724 int reg = cache->saved_regs[ARM_SP_REGNUM].realreg ();
2725 vsp = get_frame_register_unsigned (this_frame, reg);
2727 else
2729 CORE_ADDR addr = cache->saved_regs[ARM_SP_REGNUM].addr ();
2730 vsp = get_frame_memory_unsigned (this_frame, addr, 4);
2733 vsp_valid = 1;
2736 /* Decode next unwind instruction. */
2737 insn = *entry++;
2739 if ((insn & 0xc0) == 0)
2741 int offset = insn & 0x3f;
2742 vsp += (offset << 2) + 4;
2744 else if ((insn & 0xc0) == 0x40)
2746 int offset = insn & 0x3f;
2747 vsp -= (offset << 2) + 4;
2749 else if ((insn & 0xf0) == 0x80)
2751 int mask = ((insn & 0xf) << 8) | *entry++;
2752 int i;
2754 /* The special case of an all-zero mask identifies
2755 "Refuse to unwind". We return NULL to fall back
2756 to the prologue analyzer. */
2757 if (mask == 0)
2758 return NULL;
2760 /* Pop registers r4..r15 under mask. */
2761 for (i = 0; i < 12; i++)
2762 if (mask & (1 << i))
2764 cache->saved_regs[4 + i].set_addr (vsp);
2765 vsp += 4;
2768 /* Special-case popping SP -- we need to reload vsp. */
2769 if (mask & (1 << (ARM_SP_REGNUM - 4)))
2770 vsp_valid = 0;
2772 else if ((insn & 0xf0) == 0x90)
2774 int reg = insn & 0xf;
2776 /* Reserved cases. */
2777 if (reg == ARM_SP_REGNUM || reg == ARM_PC_REGNUM)
2778 return NULL;
2780 /* Set SP from another register and mark VSP for reload. */
2781 cache->saved_regs[ARM_SP_REGNUM] = cache->saved_regs[reg];
2782 vsp_valid = 0;
2784 else if ((insn & 0xf0) == 0xa0)
2786 int count = insn & 0x7;
2787 int pop_lr = (insn & 0x8) != 0;
2788 int i;
2790 /* Pop r4..r[4+count]. */
2791 for (i = 0; i <= count; i++)
2793 cache->saved_regs[4 + i].set_addr (vsp);
2794 vsp += 4;
2797 /* If indicated by flag, pop LR as well. */
2798 if (pop_lr)
2800 cache->saved_regs[ARM_LR_REGNUM].set_addr (vsp);
2801 vsp += 4;
2804 else if (insn == 0xb0)
2806 /* We could only have updated PC by popping into it; if so, it
2807 will show up as address. Otherwise, copy LR into PC. */
2808 if (!cache->saved_regs[ARM_PC_REGNUM].is_addr ())
2809 cache->saved_regs[ARM_PC_REGNUM]
2810 = cache->saved_regs[ARM_LR_REGNUM];
2812 /* We're done. */
2813 break;
2815 else if (insn == 0xb1)
2817 int mask = *entry++;
2818 int i;
2820 /* All-zero mask and mask >= 16 is "spare". */
2821 if (mask == 0 || mask >= 16)
2822 return NULL;
2824 /* Pop r0..r3 under mask. */
2825 for (i = 0; i < 4; i++)
2826 if (mask & (1 << i))
2828 cache->saved_regs[i].set_addr (vsp);
2829 vsp += 4;
2832 else if (insn == 0xb2)
2834 ULONGEST offset = 0;
2835 unsigned shift = 0;
2839 offset |= (*entry & 0x7f) << shift;
2840 shift += 7;
2842 while (*entry++ & 0x80);
2844 vsp += 0x204 + (offset << 2);
2846 else if (insn == 0xb3)
2848 int start = *entry >> 4;
2849 int count = (*entry++) & 0xf;
2850 int i;
2852 /* Only registers D0..D15 are valid here. */
2853 if (start + count >= 16)
2854 return NULL;
2856 /* Pop VFP double-precision registers D[start]..D[start+count]. */
2857 for (i = 0; i <= count; i++)
2859 cache->saved_regs[ARM_D0_REGNUM + start + i].set_addr (vsp);
2860 vsp += 8;
2863 /* Add an extra 4 bytes for FSTMFDX-style stack. */
2864 vsp += 4;
2866 else if ((insn & 0xf8) == 0xb8)
2868 int count = insn & 0x7;
2869 int i;
2871 /* Pop VFP double-precision registers D[8]..D[8+count]. */
2872 for (i = 0; i <= count; i++)
2874 cache->saved_regs[ARM_D0_REGNUM + 8 + i].set_addr (vsp);
2875 vsp += 8;
2878 /* Add an extra 4 bytes for FSTMFDX-style stack. */
2879 vsp += 4;
2881 else if (insn == 0xc6)
2883 int start = *entry >> 4;
2884 int count = (*entry++) & 0xf;
2885 int i;
2887 /* Only registers WR0..WR15 are valid. */
2888 if (start + count >= 16)
2889 return NULL;
2891 /* Pop iwmmx registers WR[start]..WR[start+count]. */
2892 for (i = 0; i <= count; i++)
2894 cache->saved_regs[ARM_WR0_REGNUM + start + i].set_addr (vsp);
2895 vsp += 8;
2898 else if (insn == 0xc7)
2900 int mask = *entry++;
2901 int i;
2903 /* All-zero mask and mask >= 16 is "spare". */
2904 if (mask == 0 || mask >= 16)
2905 return NULL;
2907 /* Pop iwmmx general-purpose registers WCGR0..WCGR3 under mask. */
2908 for (i = 0; i < 4; i++)
2909 if (mask & (1 << i))
2911 cache->saved_regs[ARM_WCGR0_REGNUM + i].set_addr (vsp);
2912 vsp += 4;
2915 else if ((insn & 0xf8) == 0xc0)
2917 int count = insn & 0x7;
2918 int i;
2920 /* Pop iwmmx registers WR[10]..WR[10+count]. */
2921 for (i = 0; i <= count; i++)
2923 cache->saved_regs[ARM_WR0_REGNUM + 10 + i].set_addr (vsp);
2924 vsp += 8;
2927 else if (insn == 0xc8)
2929 int start = *entry >> 4;
2930 int count = (*entry++) & 0xf;
2931 int i;
2933 /* Only registers D0..D31 are valid. */
2934 if (start + count >= 16)
2935 return NULL;
2937 /* Pop VFP double-precision registers
2938 D[16+start]..D[16+start+count]. */
2939 for (i = 0; i <= count; i++)
2941 cache->saved_regs[ARM_D0_REGNUM + 16 + start + i].set_addr (vsp);
2942 vsp += 8;
2945 else if (insn == 0xc9)
2947 int start = *entry >> 4;
2948 int count = (*entry++) & 0xf;
2949 int i;
2951 /* Pop VFP double-precision registers D[start]..D[start+count]. */
2952 for (i = 0; i <= count; i++)
2954 cache->saved_regs[ARM_D0_REGNUM + start + i].set_addr (vsp);
2955 vsp += 8;
2958 else if ((insn & 0xf8) == 0xd0)
2960 int count = insn & 0x7;
2961 int i;
2963 /* Pop VFP double-precision registers D[8]..D[8+count]. */
2964 for (i = 0; i <= count; i++)
2966 cache->saved_regs[ARM_D0_REGNUM + 8 + i].set_addr (vsp);
2967 vsp += 8;
2970 else
2972 /* Everything else is "spare". */
2973 return NULL;
2977 /* If we restore SP from a register, assume this was the frame register.
2978 Otherwise just fall back to SP as frame register. */
2979 if (cache->saved_regs[ARM_SP_REGNUM].is_realreg ())
2980 cache->framereg = cache->saved_regs[ARM_SP_REGNUM].realreg ();
2981 else
2982 cache->framereg = ARM_SP_REGNUM;
2984 /* Determine offset to previous frame. */
2985 cache->framesize
2986 = vsp - get_frame_register_unsigned (this_frame, cache->framereg);
2988 /* We already got the previous SP. */
2989 arm_gdbarch_tdep *tdep
2990 = gdbarch_tdep<arm_gdbarch_tdep> (get_frame_arch (this_frame));
2991 arm_cache_set_active_sp_value (cache, tdep, vsp);
2993 return cache;
2996 /* Unwinding via ARM exception table entries. Note that the sniffer
2997 already computes a filled-in prologue cache, which is then used
2998 with the same arm_prologue_this_id and arm_prologue_prev_register
2999 routines also used for prologue-parsing based unwinding. */
3001 static int
3002 arm_exidx_unwind_sniffer (const struct frame_unwind *self,
3003 struct frame_info *this_frame,
3004 void **this_prologue_cache)
3006 struct gdbarch *gdbarch = get_frame_arch (this_frame);
3007 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
3008 CORE_ADDR addr_in_block, exidx_region, func_start;
3009 struct arm_prologue_cache *cache;
3010 gdb_byte *entry;
3012 /* See if we have an ARM exception table entry covering this address. */
3013 addr_in_block = get_frame_address_in_block (this_frame);
3014 entry = arm_find_exidx_entry (addr_in_block, &exidx_region);
3015 if (!entry)
3016 return 0;
3018 /* The ARM exception table does not describe unwind information
3019 for arbitrary PC values, but is guaranteed to be correct only
3020 at call sites. We have to decide here whether we want to use
3021 ARM exception table information for this frame, or fall back
3022 to using prologue parsing. (Note that if we have DWARF CFI,
3023 this sniffer isn't even called -- CFI is always preferred.)
3025 Before we make this decision, however, we check whether we
3026 actually have *symbol* information for the current frame.
3027 If not, prologue parsing would not work anyway, so we might
3028 as well use the exception table and hope for the best. */
3029 if (find_pc_partial_function (addr_in_block, NULL, &func_start, NULL))
3031 int exc_valid = 0;
3033 /* If the next frame is "normal", we are at a call site in this
3034 frame, so exception information is guaranteed to be valid. */
3035 if (get_next_frame (this_frame)
3036 && get_frame_type (get_next_frame (this_frame)) == NORMAL_FRAME)
3037 exc_valid = 1;
3039 /* We also assume exception information is valid if we're currently
3040 blocked in a system call. The system library is supposed to
3041 ensure this, so that e.g. pthread cancellation works. */
3042 if (arm_frame_is_thumb (this_frame))
3044 ULONGEST insn;
3046 if (safe_read_memory_unsigned_integer (get_frame_pc (this_frame) - 2,
3047 2, byte_order_for_code, &insn)
3048 && (insn & 0xff00) == 0xdf00 /* svc */)
3049 exc_valid = 1;
3051 else
3053 ULONGEST insn;
3055 if (safe_read_memory_unsigned_integer (get_frame_pc (this_frame) - 4,
3056 4, byte_order_for_code, &insn)
3057 && (insn & 0x0f000000) == 0x0f000000 /* svc */)
3058 exc_valid = 1;
3061 /* Bail out if we don't know that exception information is valid. */
3062 if (!exc_valid)
3063 return 0;
3065 /* The ARM exception index does not mark the *end* of the region
3066 covered by the entry, and some functions will not have any entry.
3067 To correctly recognize the end of the covered region, the linker
3068 should have inserted dummy records with a CANTUNWIND marker.
3070 Unfortunately, current versions of GNU ld do not reliably do
3071 this, and thus we may have found an incorrect entry above.
3072 As a (temporary) sanity check, we only use the entry if it
3073 lies *within* the bounds of the function. Note that this check
3074 might reject perfectly valid entries that just happen to cover
3075 multiple functions; therefore this check ought to be removed
3076 once the linker is fixed. */
3077 if (func_start > exidx_region)
3078 return 0;
3081 /* Decode the list of unwinding instructions into a prologue cache.
3082 Note that this may fail due to e.g. a "refuse to unwind" code. */
3083 cache = arm_exidx_fill_cache (this_frame, entry);
3084 if (!cache)
3085 return 0;
3087 *this_prologue_cache = cache;
3088 return 1;
3091 struct frame_unwind arm_exidx_unwind = {
3092 "arm exidx",
3093 NORMAL_FRAME,
3094 default_frame_unwind_stop_reason,
3095 arm_prologue_this_id,
3096 arm_prologue_prev_register,
3097 NULL,
3098 arm_exidx_unwind_sniffer
3101 static struct arm_prologue_cache *
3102 arm_make_epilogue_frame_cache (struct frame_info *this_frame)
3104 struct arm_prologue_cache *cache;
3105 int reg;
3107 cache = FRAME_OBSTACK_ZALLOC (struct arm_prologue_cache);
3108 arm_cache_init (cache, this_frame);
3110 /* Still rely on the offset calculated from prologue. */
3111 arm_scan_prologue (this_frame, cache);
3113 /* Since we are in epilogue, the SP has been restored. */
3114 arm_gdbarch_tdep *tdep
3115 = gdbarch_tdep<arm_gdbarch_tdep> (get_frame_arch (this_frame));
3116 arm_cache_set_active_sp_value (cache, tdep,
3117 get_frame_register_unsigned (this_frame,
3118 ARM_SP_REGNUM));
3120 /* Calculate actual addresses of saved registers using offsets
3121 determined by arm_scan_prologue. */
3122 for (reg = 0; reg < gdbarch_num_regs (get_frame_arch (this_frame)); reg++)
3123 if (cache->saved_regs[reg].is_addr ())
3124 cache->saved_regs[reg].set_addr (cache->saved_regs[reg].addr ()
3125 + arm_cache_get_prev_sp_value (cache, tdep));
3127 return cache;
3130 /* Implementation of function hook 'this_id' in
3131 'struct frame_uwnind' for epilogue unwinder. */
3133 static void
3134 arm_epilogue_frame_this_id (struct frame_info *this_frame,
3135 void **this_cache,
3136 struct frame_id *this_id)
3138 struct arm_prologue_cache *cache;
3139 CORE_ADDR pc, func;
3141 if (*this_cache == NULL)
3142 *this_cache = arm_make_epilogue_frame_cache (this_frame);
3143 cache = (struct arm_prologue_cache *) *this_cache;
3145 /* Use function start address as part of the frame ID. If we cannot
3146 identify the start address (due to missing symbol information),
3147 fall back to just using the current PC. */
3148 pc = get_frame_pc (this_frame);
3149 func = get_frame_func (this_frame);
3150 if (func == 0)
3151 func = pc;
3153 arm_gdbarch_tdep *tdep
3154 = gdbarch_tdep<arm_gdbarch_tdep> (get_frame_arch (this_frame));
3155 *this_id = frame_id_build (arm_cache_get_prev_sp_value (cache, tdep), pc);
3158 /* Implementation of function hook 'prev_register' in
3159 'struct frame_uwnind' for epilogue unwinder. */
3161 static struct value *
3162 arm_epilogue_frame_prev_register (struct frame_info *this_frame,
3163 void **this_cache, int regnum)
3165 if (*this_cache == NULL)
3166 *this_cache = arm_make_epilogue_frame_cache (this_frame);
3168 return arm_prologue_prev_register (this_frame, this_cache, regnum);
3171 static int arm_stack_frame_destroyed_p_1 (struct gdbarch *gdbarch,
3172 CORE_ADDR pc);
3173 static int thumb_stack_frame_destroyed_p (struct gdbarch *gdbarch,
3174 CORE_ADDR pc);
3176 /* Implementation of function hook 'sniffer' in
3177 'struct frame_uwnind' for epilogue unwinder. */
3179 static int
3180 arm_epilogue_frame_sniffer (const struct frame_unwind *self,
3181 struct frame_info *this_frame,
3182 void **this_prologue_cache)
3184 if (frame_relative_level (this_frame) == 0)
3186 struct gdbarch *gdbarch = get_frame_arch (this_frame);
3187 CORE_ADDR pc = get_frame_pc (this_frame);
3189 if (arm_frame_is_thumb (this_frame))
3190 return thumb_stack_frame_destroyed_p (gdbarch, pc);
3191 else
3192 return arm_stack_frame_destroyed_p_1 (gdbarch, pc);
3194 else
3195 return 0;
3198 /* Frame unwinder from epilogue. */
3200 static const struct frame_unwind arm_epilogue_frame_unwind =
3202 "arm epilogue",
3203 NORMAL_FRAME,
3204 default_frame_unwind_stop_reason,
3205 arm_epilogue_frame_this_id,
3206 arm_epilogue_frame_prev_register,
3207 NULL,
3208 arm_epilogue_frame_sniffer,
3211 /* Recognize GCC's trampoline for thumb call-indirect. If we are in a
3212 trampoline, return the target PC. Otherwise return 0.
3214 void call0a (char c, short s, int i, long l) {}
3216 int main (void)
3218 (*pointer_to_call0a) (c, s, i, l);
3221 Instead of calling a stub library function _call_via_xx (xx is
3222 the register name), GCC may inline the trampoline in the object
3223 file as below (register r2 has the address of call0a).
3225 .global main
3226 .type main, %function
3228 bl .L1
3230 .size main, .-main
3232 .L1:
3233 bx r2
3235 The trampoline 'bx r2' doesn't belong to main. */
3237 static CORE_ADDR
3238 arm_skip_bx_reg (struct frame_info *frame, CORE_ADDR pc)
3240 /* The heuristics of recognizing such trampoline is that FRAME is
3241 executing in Thumb mode and the instruction on PC is 'bx Rm'. */
3242 if (arm_frame_is_thumb (frame))
3244 gdb_byte buf[2];
3246 if (target_read_memory (pc, buf, 2) == 0)
3248 struct gdbarch *gdbarch = get_frame_arch (frame);
3249 enum bfd_endian byte_order_for_code
3250 = gdbarch_byte_order_for_code (gdbarch);
3251 uint16_t insn
3252 = extract_unsigned_integer (buf, 2, byte_order_for_code);
3254 if ((insn & 0xff80) == 0x4700) /* bx <Rm> */
3256 CORE_ADDR dest
3257 = get_frame_register_unsigned (frame, bits (insn, 3, 6));
3259 /* Clear the LSB so that gdb core sets step-resume
3260 breakpoint at the right address. */
3261 return UNMAKE_THUMB_ADDR (dest);
3266 return 0;
3269 static struct arm_prologue_cache *
3270 arm_make_stub_cache (struct frame_info *this_frame)
3272 struct arm_prologue_cache *cache;
3274 cache = FRAME_OBSTACK_ZALLOC (struct arm_prologue_cache);
3275 arm_cache_init (cache, this_frame);
3277 arm_gdbarch_tdep *tdep
3278 = gdbarch_tdep<arm_gdbarch_tdep> (get_frame_arch (this_frame));
3279 arm_cache_set_active_sp_value (cache, tdep,
3280 get_frame_register_unsigned (this_frame,
3281 ARM_SP_REGNUM));
3283 return cache;
3286 /* Our frame ID for a stub frame is the current SP and LR. */
3288 static void
3289 arm_stub_this_id (struct frame_info *this_frame,
3290 void **this_cache,
3291 struct frame_id *this_id)
3293 struct arm_prologue_cache *cache;
3295 if (*this_cache == NULL)
3296 *this_cache = arm_make_stub_cache (this_frame);
3297 cache = (struct arm_prologue_cache *) *this_cache;
3299 arm_gdbarch_tdep *tdep
3300 = gdbarch_tdep<arm_gdbarch_tdep> (get_frame_arch (this_frame));
3301 *this_id = frame_id_build (arm_cache_get_prev_sp_value (cache, tdep),
3302 get_frame_pc (this_frame));
3305 static int
3306 arm_stub_unwind_sniffer (const struct frame_unwind *self,
3307 struct frame_info *this_frame,
3308 void **this_prologue_cache)
3310 CORE_ADDR addr_in_block;
3311 gdb_byte dummy[4];
3312 CORE_ADDR pc, start_addr;
3313 const char *name;
3315 addr_in_block = get_frame_address_in_block (this_frame);
3316 pc = get_frame_pc (this_frame);
3317 if (in_plt_section (addr_in_block)
3318 /* We also use the stub winder if the target memory is unreadable
3319 to avoid having the prologue unwinder trying to read it. */
3320 || target_read_memory (pc, dummy, 4) != 0)
3321 return 1;
3323 if (find_pc_partial_function (pc, &name, &start_addr, NULL) == 0
3324 && arm_skip_bx_reg (this_frame, pc) != 0)
3325 return 1;
3327 return 0;
3330 struct frame_unwind arm_stub_unwind = {
3331 "arm stub",
3332 NORMAL_FRAME,
3333 default_frame_unwind_stop_reason,
3334 arm_stub_this_id,
3335 arm_prologue_prev_register,
3336 NULL,
3337 arm_stub_unwind_sniffer
3340 /* Put here the code to store, into CACHE->saved_regs, the addresses
3341 of the saved registers of frame described by THIS_FRAME. CACHE is
3342 returned. */
3344 static struct arm_prologue_cache *
3345 arm_m_exception_cache (struct frame_info *this_frame)
3347 struct gdbarch *gdbarch = get_frame_arch (this_frame);
3348 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
3349 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
3350 struct arm_prologue_cache *cache;
3351 CORE_ADDR lr;
3352 CORE_ADDR sp;
3353 CORE_ADDR unwound_sp;
3354 uint32_t sp_r0_offset = 0;
3355 LONGEST xpsr;
3356 uint32_t exc_return;
3357 bool fnc_return;
3358 uint32_t extended_frame_used;
3359 bool secure_stack_used = false;
3360 bool default_callee_register_stacking = false;
3361 bool exception_domain_is_secure = false;
3363 cache = FRAME_OBSTACK_ZALLOC (struct arm_prologue_cache);
3364 arm_cache_init (cache, this_frame);
3366 /* ARMv7-M Architecture Reference "B1.5.6 Exception entry behavior"
3367 describes which bits in LR that define which stack was used prior
3368 to the exception and if FPU is used (causing extended stack frame). */
3370 lr = get_frame_register_unsigned (this_frame, ARM_LR_REGNUM);
3371 sp = get_frame_register_unsigned (this_frame, ARM_SP_REGNUM);
3373 /* ARMv7-M Architecture Reference "A2.3.1 Arm core registers"
3374 states that LR is set to 0xffffffff on reset. ARMv8-M Architecture
3375 Reference "B3.3 Registers" states that LR is set to 0xffffffff on warm
3376 reset if Main Extension is implemented, otherwise the value is unknown. */
3377 if (lr == 0xffffffff)
3379 /* Terminate any further stack unwinding by referring to self. */
3380 arm_cache_set_active_sp_value (cache, tdep, sp);
3381 return cache;
3384 fnc_return = (((lr >> 24) & 0xff) == 0xfe);
3385 if (tdep->have_sec_ext && fnc_return)
3387 if (!arm_unwind_secure_frames)
3389 warning (_("Non-secure to secure stack unwinding disabled."));
3391 /* Terminate any further stack unwinding by referring to self. */
3392 arm_cache_set_active_sp_value (cache, tdep, sp);
3393 return cache;
3396 xpsr = get_frame_register_unsigned (this_frame, ARM_PS_REGNUM);
3397 if ((xpsr & 0xff) != 0)
3398 /* Handler mode: This is the mode that exceptions are handled in. */
3399 arm_cache_switch_prev_sp (cache, tdep, tdep->m_profile_msp_s_regnum);
3400 else
3401 /* Thread mode: This is the normal mode that programs run in. */
3402 arm_cache_switch_prev_sp (cache, tdep, tdep->m_profile_psp_s_regnum);
3404 unwound_sp = arm_cache_get_prev_sp_value (cache, tdep);
3406 /* Stack layout for a function call from Secure to Non-Secure state
3407 (ARMv8-M section B3.16):
3409 SP Offset
3411 +-------------------+
3412 0x08 | |
3413 +-------------------+ <-- Original SP
3414 0x04 | Partial xPSR |
3415 +-------------------+
3416 0x00 | Return Address |
3417 +===================+ <-- New SP */
3419 cache->saved_regs[ARM_PC_REGNUM].set_addr (unwound_sp + 0x00);
3420 cache->saved_regs[ARM_LR_REGNUM].set_addr (unwound_sp + 0x00);
3421 cache->saved_regs[ARM_PS_REGNUM].set_addr (unwound_sp + 0x04);
3423 arm_cache_set_active_sp_value (cache, tdep, unwound_sp + 0x08);
3425 return cache;
3428 /* Check EXC_RETURN indicator bits (24-31). */
3429 exc_return = (((lr >> 24) & 0xff) == 0xff);
3430 if (exc_return)
3432 /* Check EXC_RETURN bit SPSEL if Main or Thread (process) stack used. */
3433 bool process_stack_used = ((lr & (1 << 2)) != 0);
3435 if (tdep->have_sec_ext)
3437 secure_stack_used = ((lr & (1 << 6)) != 0);
3438 default_callee_register_stacking = ((lr & (1 << 5)) != 0);
3439 exception_domain_is_secure = ((lr & (1 << 0)) == 0);
3441 /* Unwinding from non-secure to secure can trip security
3442 measures. In order to avoid the debugger being
3443 intrusive, rely on the user to configure the requested
3444 mode. */
3445 if (secure_stack_used && !exception_domain_is_secure
3446 && !arm_unwind_secure_frames)
3448 warning (_("Non-secure to secure stack unwinding disabled."));
3450 /* Terminate any further stack unwinding by referring to self. */
3451 arm_cache_set_active_sp_value (cache, tdep, sp);
3452 return cache;
3455 if (process_stack_used)
3457 if (secure_stack_used)
3458 /* Secure thread (process) stack used, use PSP_S as SP. */
3459 arm_cache_switch_prev_sp (cache, tdep, tdep->m_profile_psp_s_regnum);
3460 else
3461 /* Non-secure thread (process) stack used, use PSP_NS as SP. */
3462 arm_cache_switch_prev_sp (cache, tdep, tdep->m_profile_psp_ns_regnum);
3464 else
3466 if (secure_stack_used)
3467 /* Secure main stack used, use MSP_S as SP. */
3468 arm_cache_switch_prev_sp (cache, tdep, tdep->m_profile_msp_s_regnum);
3469 else
3470 /* Non-secure main stack used, use MSP_NS as SP. */
3471 arm_cache_switch_prev_sp (cache, tdep, tdep->m_profile_msp_ns_regnum);
3474 else
3476 if (process_stack_used)
3477 /* Thread (process) stack used, use PSP as SP. */
3478 arm_cache_switch_prev_sp (cache, tdep, tdep->m_profile_psp_regnum);
3479 else
3480 /* Main stack used, use MSP as SP. */
3481 arm_cache_switch_prev_sp (cache, tdep, tdep->m_profile_msp_regnum);
3485 /* Fetch the SP to use for this frame. */
3486 unwound_sp = arm_cache_get_prev_sp_value (cache, tdep);
3488 /* Exception entry context stacking are described in ARMv8-M (section B3.19)
3489 and ARMv7-M (sections B1.5.6 and B1.5.7) Architecture Reference Manuals.
3491 The following figure shows the structure of the stack frame when Security
3492 and Floating-point extensions are present.
3494 SP Offsets
3495 Without With
3496 Callee Regs Callee Regs
3497 (Secure -> Non-Secure)
3498 +-------------------+
3499 0xA8 | | 0xD0
3500 +===================+ --+ <-- Original SP
3501 0xA4 | S31 | 0xCC |
3502 +-------------------+ |
3503 ... | Additional FP context
3504 +-------------------+ |
3505 0x68 | S16 | 0x90 |
3506 +===================+ --+
3507 0x64 | Reserved | 0x8C |
3508 +-------------------+ |
3509 0x60 | FPSCR | 0x88 |
3510 +-------------------+ |
3511 0x5C | S15 | 0x84 | FP context
3512 +-------------------+ |
3513 ... |
3514 +-------------------+ |
3515 0x20 | S0 | 0x48 |
3516 +===================+ --+
3517 0x1C | xPSR | 0x44 |
3518 +-------------------+ |
3519 0x18 | Return address | 0x40 |
3520 +-------------------+ |
3521 0x14 | LR(R14) | 0x3C |
3522 +-------------------+ |
3523 0x10 | R12 | 0x38 | State context
3524 +-------------------+ |
3525 0x0C | R3 | 0x34 |
3526 +-------------------+ |
3527 ... |
3528 +-------------------+ |
3529 0x00 | R0 | 0x28 |
3530 +===================+ --+
3531 | R11 | 0x24 |
3532 +-------------------+ |
3533 ... |
3534 +-------------------+ | Additional state context
3535 | R4 | 0x08 | when transitioning from
3536 +-------------------+ | Secure to Non-Secure
3537 | Reserved | 0x04 |
3538 +-------------------+ |
3539 | Magic signature | 0x00 |
3540 +===================+ --+ <-- New SP */
3542 /* With the Security extension, the hardware saves R4..R11 too. */
3543 if (exc_return && tdep->have_sec_ext && secure_stack_used
3544 && (!default_callee_register_stacking || exception_domain_is_secure))
3546 /* Read R4..R11 from the integer callee registers. */
3547 cache->saved_regs[4].set_addr (unwound_sp + 0x08);
3548 cache->saved_regs[5].set_addr (unwound_sp + 0x0C);
3549 cache->saved_regs[6].set_addr (unwound_sp + 0x10);
3550 cache->saved_regs[7].set_addr (unwound_sp + 0x14);
3551 cache->saved_regs[8].set_addr (unwound_sp + 0x18);
3552 cache->saved_regs[9].set_addr (unwound_sp + 0x1C);
3553 cache->saved_regs[10].set_addr (unwound_sp + 0x20);
3554 cache->saved_regs[11].set_addr (unwound_sp + 0x24);
3555 sp_r0_offset = 0x28;
3558 /* The hardware saves eight 32-bit words, comprising xPSR,
3559 ReturnAddress, LR (R14), R12, R3, R2, R1, R0. See details in
3560 "B1.5.6 Exception entry behavior" in
3561 "ARMv7-M Architecture Reference Manual". */
3562 cache->saved_regs[0].set_addr (unwound_sp + sp_r0_offset);
3563 cache->saved_regs[1].set_addr (unwound_sp + sp_r0_offset + 0x04);
3564 cache->saved_regs[2].set_addr (unwound_sp + sp_r0_offset + 0x08);
3565 cache->saved_regs[3].set_addr (unwound_sp + sp_r0_offset + 0x0C);
3566 cache->saved_regs[ARM_IP_REGNUM].set_addr (unwound_sp + sp_r0_offset + 0x10);
3567 cache->saved_regs[ARM_LR_REGNUM].set_addr (unwound_sp + sp_r0_offset + 0x14);
3568 cache->saved_regs[ARM_PC_REGNUM].set_addr (unwound_sp + sp_r0_offset + 0x18);
3569 cache->saved_regs[ARM_PS_REGNUM].set_addr (unwound_sp + sp_r0_offset + 0x1C);
3571 /* Check EXC_RETURN bit FTYPE if extended stack frame (FPU regs stored)
3572 type used. */
3573 extended_frame_used = ((lr & (1 << 4)) == 0);
3574 if (exc_return && extended_frame_used)
3576 int i;
3577 int fpu_regs_stack_offset;
3578 ULONGEST fpccr;
3580 /* Read FPCCR register. */
3581 gdb_assert (safe_read_memory_unsigned_integer (FPCCR,
3582 ARM_INT_REGISTER_SIZE,
3583 byte_order, &fpccr));
3584 bool fpccr_ts = bit (fpccr,26);
3586 /* This code does not take into account the lazy stacking, see "Lazy
3587 context save of FP state", in B1.5.7, also ARM AN298, supported
3588 by Cortex-M4F architecture.
3589 To fully handle this the FPCCR register (Floating-point Context
3590 Control Register) needs to be read out and the bits ASPEN and LSPEN
3591 could be checked to setup correct lazy stacked FP registers.
3592 This register is located at address 0xE000EF34. */
3594 /* Extended stack frame type used. */
3595 fpu_regs_stack_offset = unwound_sp + sp_r0_offset + 0x20;
3596 for (i = 0; i < 8; i++)
3598 cache->saved_regs[ARM_D0_REGNUM + i].set_addr (fpu_regs_stack_offset);
3599 fpu_regs_stack_offset += 8;
3601 cache->saved_regs[ARM_FPSCR_REGNUM].set_addr (unwound_sp + sp_r0_offset + 0x60);
3602 fpu_regs_stack_offset += 4;
3604 if (tdep->have_sec_ext && !default_callee_register_stacking && fpccr_ts)
3606 /* Handle floating-point callee saved registers. */
3607 fpu_regs_stack_offset = unwound_sp + sp_r0_offset + 0x68;
3608 for (i = 8; i < 16; i++)
3610 cache->saved_regs[ARM_D0_REGNUM + i].set_addr (fpu_regs_stack_offset);
3611 fpu_regs_stack_offset += 8;
3614 arm_cache_set_active_sp_value (cache, tdep,
3615 unwound_sp + sp_r0_offset + 0xA8);
3617 else
3619 /* Offset 0x64 is reserved. */
3620 arm_cache_set_active_sp_value (cache, tdep,
3621 unwound_sp + sp_r0_offset + 0x68);
3624 else
3626 /* Standard stack frame type used. */
3627 arm_cache_set_active_sp_value (cache, tdep,
3628 unwound_sp + sp_r0_offset + 0x20);
3631 /* If bit 9 of the saved xPSR is set, then there is a four-byte
3632 aligner between the top of the 32-byte stack frame and the
3633 previous context's stack pointer. */
3634 if (safe_read_memory_integer (unwound_sp + sp_r0_offset + 0x1C, 4,
3635 byte_order, &xpsr)
3636 && (xpsr & (1 << 9)) != 0)
3637 arm_cache_set_active_sp_value (cache, tdep,
3638 arm_cache_get_prev_sp_value (cache, tdep) + 4);
3640 return cache;
3643 /* Implementation of function hook 'this_id' in
3644 'struct frame_uwnind'. */
3646 static void
3647 arm_m_exception_this_id (struct frame_info *this_frame,
3648 void **this_cache,
3649 struct frame_id *this_id)
3651 struct arm_prologue_cache *cache;
3653 if (*this_cache == NULL)
3654 *this_cache = arm_m_exception_cache (this_frame);
3655 cache = (struct arm_prologue_cache *) *this_cache;
3657 /* Our frame ID for a stub frame is the current SP and LR. */
3658 arm_gdbarch_tdep *tdep
3659 = gdbarch_tdep<arm_gdbarch_tdep> (get_frame_arch (this_frame));
3660 *this_id = frame_id_build (arm_cache_get_prev_sp_value (cache, tdep),
3661 get_frame_pc (this_frame));
3664 /* Implementation of function hook 'prev_register' in
3665 'struct frame_uwnind'. */
3667 static struct value *
3668 arm_m_exception_prev_register (struct frame_info *this_frame,
3669 void **this_cache,
3670 int prev_regnum)
3672 struct arm_prologue_cache *cache;
3673 CORE_ADDR sp_value;
3675 if (*this_cache == NULL)
3676 *this_cache = arm_m_exception_cache (this_frame);
3677 cache = (struct arm_prologue_cache *) *this_cache;
3679 /* The value was already reconstructed into PREV_SP. */
3680 arm_gdbarch_tdep *tdep
3681 = gdbarch_tdep<arm_gdbarch_tdep> (get_frame_arch (this_frame));
3682 if (prev_regnum == ARM_SP_REGNUM)
3683 return frame_unwind_got_constant (this_frame, prev_regnum,
3684 arm_cache_get_prev_sp_value (cache, tdep));
3686 /* If we are asked to unwind the PC, strip the saved T bit. */
3687 if (prev_regnum == ARM_PC_REGNUM)
3689 struct value *value = trad_frame_get_prev_register (this_frame,
3690 cache->saved_regs,
3691 prev_regnum);
3692 CORE_ADDR pc = value_as_address (value);
3693 return frame_unwind_got_constant (this_frame, prev_regnum,
3694 UNMAKE_THUMB_ADDR (pc));
3697 /* The value might be one of the alternative SP, if so, use the
3698 value already constructed. */
3699 if (arm_is_alternative_sp_register (tdep, prev_regnum))
3701 sp_value = arm_cache_get_sp_register (cache, tdep, prev_regnum);
3702 return frame_unwind_got_constant (this_frame, prev_regnum, sp_value);
3705 /* If we are asked to unwind the xPSR, set T bit if PC is in thumb mode.
3706 LR register is unreliable as it contains FNC_RETURN or EXC_RETURN
3707 pattern. */
3708 if (prev_regnum == ARM_PS_REGNUM)
3710 struct gdbarch *gdbarch = get_frame_arch (this_frame);
3711 struct value *value = trad_frame_get_prev_register (this_frame,
3712 cache->saved_regs,
3713 ARM_PC_REGNUM);
3714 CORE_ADDR pc = value_as_address (value);
3715 value = trad_frame_get_prev_register (this_frame, cache->saved_regs,
3716 ARM_PS_REGNUM);
3717 ULONGEST xpsr = value_as_long (value);
3719 /* Reconstruct the T bit; see arm_prologue_prev_register for details. */
3720 xpsr = reconstruct_t_bit (gdbarch, pc, xpsr);
3721 return frame_unwind_got_constant (this_frame, ARM_PS_REGNUM, xpsr);
3724 return trad_frame_get_prev_register (this_frame, cache->saved_regs,
3725 prev_regnum);
3728 /* Implementation of function hook 'sniffer' in
3729 'struct frame_uwnind'. */
3731 static int
3732 arm_m_exception_unwind_sniffer (const struct frame_unwind *self,
3733 struct frame_info *this_frame,
3734 void **this_prologue_cache)
3736 struct gdbarch *gdbarch = get_frame_arch (this_frame);
3737 CORE_ADDR this_pc = get_frame_pc (this_frame);
3739 /* No need to check is_m; this sniffer is only registered for
3740 M-profile architectures. */
3742 /* Check if exception frame returns to a magic PC value. */
3743 return arm_m_addr_is_magic (gdbarch, this_pc);
3746 /* Frame unwinder for M-profile exceptions. */
3748 struct frame_unwind arm_m_exception_unwind =
3750 "arm m exception",
3751 SIGTRAMP_FRAME,
3752 default_frame_unwind_stop_reason,
3753 arm_m_exception_this_id,
3754 arm_m_exception_prev_register,
3755 NULL,
3756 arm_m_exception_unwind_sniffer
3759 static CORE_ADDR
3760 arm_normal_frame_base (struct frame_info *this_frame, void **this_cache)
3762 struct arm_prologue_cache *cache;
3764 if (*this_cache == NULL)
3765 *this_cache = arm_make_prologue_cache (this_frame);
3766 cache = (struct arm_prologue_cache *) *this_cache;
3768 arm_gdbarch_tdep *tdep
3769 = gdbarch_tdep<arm_gdbarch_tdep> (get_frame_arch (this_frame));
3770 return arm_cache_get_prev_sp_value (cache, tdep) - cache->framesize;
3773 struct frame_base arm_normal_base = {
3774 &arm_prologue_unwind,
3775 arm_normal_frame_base,
3776 arm_normal_frame_base,
3777 arm_normal_frame_base
3780 static struct value *
3781 arm_dwarf2_prev_register (struct frame_info *this_frame, void **this_cache,
3782 int regnum)
3784 struct gdbarch * gdbarch = get_frame_arch (this_frame);
3785 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
3786 CORE_ADDR lr;
3787 ULONGEST cpsr;
3789 if (regnum == ARM_PC_REGNUM)
3791 /* The PC is normally copied from the return column, which
3792 describes saves of LR. However, that version may have an
3793 extra bit set to indicate Thumb state. The bit is not
3794 part of the PC. */
3796 /* Record in the frame whether the return address was signed. */
3797 if (tdep->have_pacbti)
3799 CORE_ADDR ra_auth_code
3800 = frame_unwind_register_unsigned (this_frame,
3801 tdep->pacbti_pseudo_base);
3803 if (ra_auth_code != 0)
3804 set_frame_previous_pc_masked (this_frame);
3807 lr = frame_unwind_register_unsigned (this_frame, ARM_LR_REGNUM);
3808 return frame_unwind_got_constant (this_frame, regnum,
3809 arm_addr_bits_remove (gdbarch, lr));
3811 else if (regnum == ARM_PS_REGNUM)
3813 /* Reconstruct the T bit; see arm_prologue_prev_register for details. */
3814 cpsr = get_frame_register_unsigned (this_frame, regnum);
3815 lr = frame_unwind_register_unsigned (this_frame, ARM_LR_REGNUM);
3816 cpsr = reconstruct_t_bit (gdbarch, lr, cpsr);
3817 return frame_unwind_got_constant (this_frame, regnum, cpsr);
3819 else if (arm_is_alternative_sp_register (tdep, regnum))
3821 /* Handle the alternative SP registers on Cortex-M. */
3822 bool override_with_sp_value = false;
3823 CORE_ADDR val;
3825 if (tdep->have_sec_ext)
3827 CORE_ADDR sp
3828 = get_frame_register_unsigned (this_frame, ARM_SP_REGNUM);
3829 CORE_ADDR msp_s
3830 = get_frame_register_unsigned (this_frame,
3831 tdep->m_profile_msp_s_regnum);
3832 CORE_ADDR msp_ns
3833 = get_frame_register_unsigned (this_frame,
3834 tdep->m_profile_msp_ns_regnum);
3835 CORE_ADDR psp_s
3836 = get_frame_register_unsigned (this_frame,
3837 tdep->m_profile_psp_s_regnum);
3838 CORE_ADDR psp_ns
3839 = get_frame_register_unsigned (this_frame,
3840 tdep->m_profile_psp_ns_regnum);
3842 bool is_msp = (regnum == tdep->m_profile_msp_regnum)
3843 && (msp_s == sp || msp_ns == sp);
3844 bool is_msp_s = (regnum == tdep->m_profile_msp_s_regnum)
3845 && (msp_s == sp);
3846 bool is_msp_ns = (regnum == tdep->m_profile_msp_ns_regnum)
3847 && (msp_ns == sp);
3848 bool is_psp = (regnum == tdep->m_profile_psp_regnum)
3849 && (psp_s == sp || psp_ns == sp);
3850 bool is_psp_s = (regnum == tdep->m_profile_psp_s_regnum)
3851 && (psp_s == sp);
3852 bool is_psp_ns = (regnum == tdep->m_profile_psp_ns_regnum)
3853 && (psp_ns == sp);
3855 override_with_sp_value = is_msp || is_msp_s || is_msp_ns
3856 || is_psp || is_psp_s || is_psp_ns;
3859 else if (tdep->is_m)
3861 CORE_ADDR sp
3862 = get_frame_register_unsigned (this_frame, ARM_SP_REGNUM);
3863 CORE_ADDR msp
3864 = get_frame_register_unsigned (this_frame,
3865 tdep->m_profile_msp_regnum);
3866 CORE_ADDR psp
3867 = get_frame_register_unsigned (this_frame,
3868 tdep->m_profile_psp_regnum);
3870 bool is_msp = (regnum == tdep->m_profile_msp_regnum) && (sp == msp);
3871 bool is_psp = (regnum == tdep->m_profile_psp_regnum) && (sp == psp);
3873 override_with_sp_value = is_msp || is_psp;
3876 if (override_with_sp_value)
3878 /* Use value of SP from previous frame. */
3879 struct frame_info *prev_frame = get_prev_frame (this_frame);
3880 if (prev_frame)
3881 val = get_frame_register_unsigned (prev_frame, ARM_SP_REGNUM);
3882 else
3883 val = get_frame_base (this_frame);
3885 else
3886 /* Use value for the register from previous frame. */
3887 val = get_frame_register_unsigned (this_frame, regnum);
3889 return frame_unwind_got_constant (this_frame, regnum, val);
3892 internal_error (__FILE__, __LINE__,
3893 _("Unexpected register %d"), regnum);
3896 /* Implement the stack_frame_destroyed_p gdbarch method. */
3898 static int
3899 thumb_stack_frame_destroyed_p (struct gdbarch *gdbarch, CORE_ADDR pc)
3901 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
3902 unsigned int insn, insn2;
3903 int found_return = 0, found_stack_adjust = 0;
3904 CORE_ADDR func_start, func_end;
3905 CORE_ADDR scan_pc;
3906 gdb_byte buf[4];
3908 if (!find_pc_partial_function (pc, NULL, &func_start, &func_end))
3909 return 0;
3911 /* The epilogue is a sequence of instructions along the following lines:
3913 - add stack frame size to SP or FP
3914 - [if frame pointer used] restore SP from FP
3915 - restore registers from SP [may include PC]
3916 - a return-type instruction [if PC wasn't already restored]
3918 In a first pass, we scan forward from the current PC and verify the
3919 instructions we find as compatible with this sequence, ending in a
3920 return instruction.
3922 However, this is not sufficient to distinguish indirect function calls
3923 within a function from indirect tail calls in the epilogue in some cases.
3924 Therefore, if we didn't already find any SP-changing instruction during
3925 forward scan, we add a backward scanning heuristic to ensure we actually
3926 are in the epilogue. */
3928 scan_pc = pc;
3929 while (scan_pc < func_end && !found_return)
3931 if (target_read_memory (scan_pc, buf, 2))
3932 break;
3934 scan_pc += 2;
3935 insn = extract_unsigned_integer (buf, 2, byte_order_for_code);
3937 if ((insn & 0xff80) == 0x4700) /* bx <Rm> */
3938 found_return = 1;
3939 else if (insn == 0x46f7) /* mov pc, lr */
3940 found_return = 1;
3941 else if (thumb_instruction_restores_sp (insn))
3943 if ((insn & 0xff00) == 0xbd00) /* pop <registers, PC> */
3944 found_return = 1;
3946 else if (thumb_insn_size (insn) == 4) /* 32-bit Thumb-2 instruction */
3948 if (target_read_memory (scan_pc, buf, 2))
3949 break;
3951 scan_pc += 2;
3952 insn2 = extract_unsigned_integer (buf, 2, byte_order_for_code);
3954 if (insn == 0xe8bd) /* ldm.w sp!, <registers> */
3956 if (insn2 & 0x8000) /* <registers> include PC. */
3957 found_return = 1;
3959 else if (insn == 0xf85d /* ldr.w <Rt>, [sp], #4 */
3960 && (insn2 & 0x0fff) == 0x0b04)
3962 if ((insn2 & 0xf000) == 0xf000) /* <Rt> is PC. */
3963 found_return = 1;
3965 else if ((insn & 0xffbf) == 0xecbd /* vldm sp!, <list> */
3966 && (insn2 & 0x0e00) == 0x0a00)
3968 else
3969 break;
3971 else
3972 break;
3975 if (!found_return)
3976 return 0;
3978 /* Since any instruction in the epilogue sequence, with the possible
3979 exception of return itself, updates the stack pointer, we need to
3980 scan backwards for at most one instruction. Try either a 16-bit or
3981 a 32-bit instruction. This is just a heuristic, so we do not worry
3982 too much about false positives. */
3984 if (pc - 4 < func_start)
3985 return 0;
3986 if (target_read_memory (pc - 4, buf, 4))
3987 return 0;
3989 insn = extract_unsigned_integer (buf, 2, byte_order_for_code);
3990 insn2 = extract_unsigned_integer (buf + 2, 2, byte_order_for_code);
3992 if (thumb_instruction_restores_sp (insn2))
3993 found_stack_adjust = 1;
3994 else if (insn == 0xe8bd) /* ldm.w sp!, <registers> */
3995 found_stack_adjust = 1;
3996 else if (insn == 0xf85d /* ldr.w <Rt>, [sp], #4 */
3997 && (insn2 & 0x0fff) == 0x0b04)
3998 found_stack_adjust = 1;
3999 else if ((insn & 0xffbf) == 0xecbd /* vldm sp!, <list> */
4000 && (insn2 & 0x0e00) == 0x0a00)
4001 found_stack_adjust = 1;
4003 return found_stack_adjust;
4006 static int
4007 arm_stack_frame_destroyed_p_1 (struct gdbarch *gdbarch, CORE_ADDR pc)
4009 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
4010 unsigned int insn;
4011 int found_return;
4012 CORE_ADDR func_start, func_end;
4014 if (!find_pc_partial_function (pc, NULL, &func_start, &func_end))
4015 return 0;
4017 /* We are in the epilogue if the previous instruction was a stack
4018 adjustment and the next instruction is a possible return (bx, mov
4019 pc, or pop). We could have to scan backwards to find the stack
4020 adjustment, or forwards to find the return, but this is a decent
4021 approximation. First scan forwards. */
4023 found_return = 0;
4024 insn = read_memory_unsigned_integer (pc, 4, byte_order_for_code);
4025 if (bits (insn, 28, 31) != INST_NV)
4027 if ((insn & 0x0ffffff0) == 0x012fff10)
4028 /* BX. */
4029 found_return = 1;
4030 else if ((insn & 0x0ffffff0) == 0x01a0f000)
4031 /* MOV PC. */
4032 found_return = 1;
4033 else if ((insn & 0x0fff0000) == 0x08bd0000
4034 && (insn & 0x0000c000) != 0)
4035 /* POP (LDMIA), including PC or LR. */
4036 found_return = 1;
4039 if (!found_return)
4040 return 0;
4042 /* Scan backwards. This is just a heuristic, so do not worry about
4043 false positives from mode changes. */
4045 if (pc < func_start + 4)
4046 return 0;
4048 insn = read_memory_unsigned_integer (pc - 4, 4, byte_order_for_code);
4049 if (arm_instruction_restores_sp (insn))
4050 return 1;
4052 return 0;
4055 /* Implement the stack_frame_destroyed_p gdbarch method. */
4057 static int
4058 arm_stack_frame_destroyed_p (struct gdbarch *gdbarch, CORE_ADDR pc)
4060 if (arm_pc_is_thumb (gdbarch, pc))
4061 return thumb_stack_frame_destroyed_p (gdbarch, pc);
4062 else
4063 return arm_stack_frame_destroyed_p_1 (gdbarch, pc);
4066 /* When arguments must be pushed onto the stack, they go on in reverse
4067 order. The code below implements a FILO (stack) to do this. */
4069 struct arm_stack_item
4071 int len;
4072 struct arm_stack_item *prev;
4073 gdb_byte *data;
4076 static struct arm_stack_item *
4077 push_stack_item (struct arm_stack_item *prev, const gdb_byte *contents,
4078 int len)
4080 struct arm_stack_item *si;
4081 si = XNEW (struct arm_stack_item);
4082 si->data = (gdb_byte *) xmalloc (len);
4083 si->len = len;
4084 si->prev = prev;
4085 memcpy (si->data, contents, len);
4086 return si;
4089 static struct arm_stack_item *
4090 pop_stack_item (struct arm_stack_item *si)
4092 struct arm_stack_item *dead = si;
4093 si = si->prev;
4094 xfree (dead->data);
4095 xfree (dead);
4096 return si;
4099 /* Implement the gdbarch type alignment method, overrides the generic
4100 alignment algorithm for anything that is arm specific. */
4102 static ULONGEST
4103 arm_type_align (gdbarch *gdbarch, struct type *t)
4105 t = check_typedef (t);
4106 if (t->code () == TYPE_CODE_ARRAY && t->is_vector ())
4108 /* Use the natural alignment for vector types (the same for
4109 scalar type), but the maximum alignment is 64-bit. */
4110 if (TYPE_LENGTH (t) > 8)
4111 return 8;
4112 else
4113 return TYPE_LENGTH (t);
4116 /* Allow the common code to calculate the alignment. */
4117 return 0;
4120 /* Possible base types for a candidate for passing and returning in
4121 VFP registers. */
4123 enum arm_vfp_cprc_base_type
4125 VFP_CPRC_UNKNOWN,
4126 VFP_CPRC_SINGLE,
4127 VFP_CPRC_DOUBLE,
4128 VFP_CPRC_VEC64,
4129 VFP_CPRC_VEC128
4132 /* The length of one element of base type B. */
4134 static unsigned
4135 arm_vfp_cprc_unit_length (enum arm_vfp_cprc_base_type b)
4137 switch (b)
4139 case VFP_CPRC_SINGLE:
4140 return 4;
4141 case VFP_CPRC_DOUBLE:
4142 return 8;
4143 case VFP_CPRC_VEC64:
4144 return 8;
4145 case VFP_CPRC_VEC128:
4146 return 16;
4147 default:
4148 internal_error (__FILE__, __LINE__, _("Invalid VFP CPRC type: %d."),
4149 (int) b);
4153 /* The character ('s', 'd' or 'q') for the type of VFP register used
4154 for passing base type B. */
4156 static int
4157 arm_vfp_cprc_reg_char (enum arm_vfp_cprc_base_type b)
4159 switch (b)
4161 case VFP_CPRC_SINGLE:
4162 return 's';
4163 case VFP_CPRC_DOUBLE:
4164 return 'd';
4165 case VFP_CPRC_VEC64:
4166 return 'd';
4167 case VFP_CPRC_VEC128:
4168 return 'q';
4169 default:
4170 internal_error (__FILE__, __LINE__, _("Invalid VFP CPRC type: %d."),
4171 (int) b);
4175 /* Determine whether T may be part of a candidate for passing and
4176 returning in VFP registers, ignoring the limit on the total number
4177 of components. If *BASE_TYPE is VFP_CPRC_UNKNOWN, set it to the
4178 classification of the first valid component found; if it is not
4179 VFP_CPRC_UNKNOWN, all components must have the same classification
4180 as *BASE_TYPE. If it is found that T contains a type not permitted
4181 for passing and returning in VFP registers, a type differently
4182 classified from *BASE_TYPE, or two types differently classified
4183 from each other, return -1, otherwise return the total number of
4184 base-type elements found (possibly 0 in an empty structure or
4185 array). Vector types are not currently supported, matching the
4186 generic AAPCS support. */
4188 static int
4189 arm_vfp_cprc_sub_candidate (struct type *t,
4190 enum arm_vfp_cprc_base_type *base_type)
4192 t = check_typedef (t);
4193 switch (t->code ())
4195 case TYPE_CODE_FLT:
4196 switch (TYPE_LENGTH (t))
4198 case 4:
4199 if (*base_type == VFP_CPRC_UNKNOWN)
4200 *base_type = VFP_CPRC_SINGLE;
4201 else if (*base_type != VFP_CPRC_SINGLE)
4202 return -1;
4203 return 1;
4205 case 8:
4206 if (*base_type == VFP_CPRC_UNKNOWN)
4207 *base_type = VFP_CPRC_DOUBLE;
4208 else if (*base_type != VFP_CPRC_DOUBLE)
4209 return -1;
4210 return 1;
4212 default:
4213 return -1;
4215 break;
4217 case TYPE_CODE_COMPLEX:
4218 /* Arguments of complex T where T is one of the types float or
4219 double get treated as if they are implemented as:
4221 struct complexT
4223 T real;
4224 T imag;
4228 switch (TYPE_LENGTH (t))
4230 case 8:
4231 if (*base_type == VFP_CPRC_UNKNOWN)
4232 *base_type = VFP_CPRC_SINGLE;
4233 else if (*base_type != VFP_CPRC_SINGLE)
4234 return -1;
4235 return 2;
4237 case 16:
4238 if (*base_type == VFP_CPRC_UNKNOWN)
4239 *base_type = VFP_CPRC_DOUBLE;
4240 else if (*base_type != VFP_CPRC_DOUBLE)
4241 return -1;
4242 return 2;
4244 default:
4245 return -1;
4247 break;
4249 case TYPE_CODE_ARRAY:
4251 if (t->is_vector ())
4253 /* A 64-bit or 128-bit containerized vector type are VFP
4254 CPRCs. */
4255 switch (TYPE_LENGTH (t))
4257 case 8:
4258 if (*base_type == VFP_CPRC_UNKNOWN)
4259 *base_type = VFP_CPRC_VEC64;
4260 return 1;
4261 case 16:
4262 if (*base_type == VFP_CPRC_UNKNOWN)
4263 *base_type = VFP_CPRC_VEC128;
4264 return 1;
4265 default:
4266 return -1;
4269 else
4271 int count;
4272 unsigned unitlen;
4274 count = arm_vfp_cprc_sub_candidate (TYPE_TARGET_TYPE (t),
4275 base_type);
4276 if (count == -1)
4277 return -1;
4278 if (TYPE_LENGTH (t) == 0)
4280 gdb_assert (count == 0);
4281 return 0;
4283 else if (count == 0)
4284 return -1;
4285 unitlen = arm_vfp_cprc_unit_length (*base_type);
4286 gdb_assert ((TYPE_LENGTH (t) % unitlen) == 0);
4287 return TYPE_LENGTH (t) / unitlen;
4290 break;
4292 case TYPE_CODE_STRUCT:
4294 int count = 0;
4295 unsigned unitlen;
4296 int i;
4297 for (i = 0; i < t->num_fields (); i++)
4299 int sub_count = 0;
4301 if (!field_is_static (&t->field (i)))
4302 sub_count = arm_vfp_cprc_sub_candidate (t->field (i).type (),
4303 base_type);
4304 if (sub_count == -1)
4305 return -1;
4306 count += sub_count;
4308 if (TYPE_LENGTH (t) == 0)
4310 gdb_assert (count == 0);
4311 return 0;
4313 else if (count == 0)
4314 return -1;
4315 unitlen = arm_vfp_cprc_unit_length (*base_type);
4316 if (TYPE_LENGTH (t) != unitlen * count)
4317 return -1;
4318 return count;
4321 case TYPE_CODE_UNION:
4323 int count = 0;
4324 unsigned unitlen;
4325 int i;
4326 for (i = 0; i < t->num_fields (); i++)
4328 int sub_count = arm_vfp_cprc_sub_candidate (t->field (i).type (),
4329 base_type);
4330 if (sub_count == -1)
4331 return -1;
4332 count = (count > sub_count ? count : sub_count);
4334 if (TYPE_LENGTH (t) == 0)
4336 gdb_assert (count == 0);
4337 return 0;
4339 else if (count == 0)
4340 return -1;
4341 unitlen = arm_vfp_cprc_unit_length (*base_type);
4342 if (TYPE_LENGTH (t) != unitlen * count)
4343 return -1;
4344 return count;
4347 default:
4348 break;
4351 return -1;
4354 /* Determine whether T is a VFP co-processor register candidate (CPRC)
4355 if passed to or returned from a non-variadic function with the VFP
4356 ABI in effect. Return 1 if it is, 0 otherwise. If it is, set
4357 *BASE_TYPE to the base type for T and *COUNT to the number of
4358 elements of that base type before returning. */
4360 static int
4361 arm_vfp_call_candidate (struct type *t, enum arm_vfp_cprc_base_type *base_type,
4362 int *count)
4364 enum arm_vfp_cprc_base_type b = VFP_CPRC_UNKNOWN;
4365 int c = arm_vfp_cprc_sub_candidate (t, &b);
4366 if (c <= 0 || c > 4)
4367 return 0;
4368 *base_type = b;
4369 *count = c;
4370 return 1;
4373 /* Return 1 if the VFP ABI should be used for passing arguments to and
4374 returning values from a function of type FUNC_TYPE, 0
4375 otherwise. */
4377 static int
4378 arm_vfp_abi_for_function (struct gdbarch *gdbarch, struct type *func_type)
4380 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
4382 /* Variadic functions always use the base ABI. Assume that functions
4383 without debug info are not variadic. */
4384 if (func_type && check_typedef (func_type)->has_varargs ())
4385 return 0;
4387 /* The VFP ABI is only supported as a variant of AAPCS. */
4388 if (tdep->arm_abi != ARM_ABI_AAPCS)
4389 return 0;
4391 return tdep->fp_model == ARM_FLOAT_VFP;
4394 /* We currently only support passing parameters in integer registers, which
4395 conforms with GCC's default model, and VFP argument passing following
4396 the VFP variant of AAPCS. Several other variants exist and
4397 we should probably support some of them based on the selected ABI. */
4399 static CORE_ADDR
4400 arm_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
4401 struct regcache *regcache, CORE_ADDR bp_addr, int nargs,
4402 struct value **args, CORE_ADDR sp,
4403 function_call_return_method return_method,
4404 CORE_ADDR struct_addr)
4406 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
4407 int argnum;
4408 int argreg;
4409 int nstack;
4410 struct arm_stack_item *si = NULL;
4411 int use_vfp_abi;
4412 struct type *ftype;
4413 unsigned vfp_regs_free = (1 << 16) - 1;
4414 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
4416 /* Determine the type of this function and whether the VFP ABI
4417 applies. */
4418 ftype = check_typedef (value_type (function));
4419 if (ftype->code () == TYPE_CODE_PTR)
4420 ftype = check_typedef (TYPE_TARGET_TYPE (ftype));
4421 use_vfp_abi = arm_vfp_abi_for_function (gdbarch, ftype);
4423 /* Set the return address. For the ARM, the return breakpoint is
4424 always at BP_ADDR. */
4425 if (arm_pc_is_thumb (gdbarch, bp_addr))
4426 bp_addr |= 1;
4427 regcache_cooked_write_unsigned (regcache, ARM_LR_REGNUM, bp_addr);
4429 /* Walk through the list of args and determine how large a temporary
4430 stack is required. Need to take care here as structs may be
4431 passed on the stack, and we have to push them. */
4432 nstack = 0;
4434 argreg = ARM_A1_REGNUM;
4435 nstack = 0;
4437 /* The struct_return pointer occupies the first parameter
4438 passing register. */
4439 if (return_method == return_method_struct)
4441 arm_debug_printf ("struct return in %s = %s",
4442 gdbarch_register_name (gdbarch, argreg),
4443 paddress (gdbarch, struct_addr));
4445 regcache_cooked_write_unsigned (regcache, argreg, struct_addr);
4446 argreg++;
4449 for (argnum = 0; argnum < nargs; argnum++)
4451 int len;
4452 struct type *arg_type;
4453 struct type *target_type;
4454 enum type_code typecode;
4455 const bfd_byte *val;
4456 int align;
4457 enum arm_vfp_cprc_base_type vfp_base_type;
4458 int vfp_base_count;
4459 int may_use_core_reg = 1;
4461 arg_type = check_typedef (value_type (args[argnum]));
4462 len = TYPE_LENGTH (arg_type);
4463 target_type = TYPE_TARGET_TYPE (arg_type);
4464 typecode = arg_type->code ();
4465 val = value_contents (args[argnum]).data ();
4467 align = type_align (arg_type);
4468 /* Round alignment up to a whole number of words. */
4469 align = (align + ARM_INT_REGISTER_SIZE - 1)
4470 & ~(ARM_INT_REGISTER_SIZE - 1);
4471 /* Different ABIs have different maximum alignments. */
4472 if (tdep->arm_abi == ARM_ABI_APCS)
4474 /* The APCS ABI only requires word alignment. */
4475 align = ARM_INT_REGISTER_SIZE;
4477 else
4479 /* The AAPCS requires at most doubleword alignment. */
4480 if (align > ARM_INT_REGISTER_SIZE * 2)
4481 align = ARM_INT_REGISTER_SIZE * 2;
4484 if (use_vfp_abi
4485 && arm_vfp_call_candidate (arg_type, &vfp_base_type,
4486 &vfp_base_count))
4488 int regno;
4489 int unit_length;
4490 int shift;
4491 unsigned mask;
4493 /* Because this is a CPRC it cannot go in a core register or
4494 cause a core register to be skipped for alignment.
4495 Either it goes in VFP registers and the rest of this loop
4496 iteration is skipped for this argument, or it goes on the
4497 stack (and the stack alignment code is correct for this
4498 case). */
4499 may_use_core_reg = 0;
4501 unit_length = arm_vfp_cprc_unit_length (vfp_base_type);
4502 shift = unit_length / 4;
4503 mask = (1 << (shift * vfp_base_count)) - 1;
4504 for (regno = 0; regno < 16; regno += shift)
4505 if (((vfp_regs_free >> regno) & mask) == mask)
4506 break;
4508 if (regno < 16)
4510 int reg_char;
4511 int reg_scaled;
4512 int i;
4514 vfp_regs_free &= ~(mask << regno);
4515 reg_scaled = regno / shift;
4516 reg_char = arm_vfp_cprc_reg_char (vfp_base_type);
4517 for (i = 0; i < vfp_base_count; i++)
4519 char name_buf[4];
4520 int regnum;
4521 if (reg_char == 'q')
4522 arm_neon_quad_write (gdbarch, regcache, reg_scaled + i,
4523 val + i * unit_length);
4524 else
4526 xsnprintf (name_buf, sizeof (name_buf), "%c%d",
4527 reg_char, reg_scaled + i);
4528 regnum = user_reg_map_name_to_regnum (gdbarch, name_buf,
4529 strlen (name_buf));
4530 regcache->cooked_write (regnum, val + i * unit_length);
4533 continue;
4535 else
4537 /* This CPRC could not go in VFP registers, so all VFP
4538 registers are now marked as used. */
4539 vfp_regs_free = 0;
4543 /* Push stack padding for doubleword alignment. */
4544 if (nstack & (align - 1))
4546 si = push_stack_item (si, val, ARM_INT_REGISTER_SIZE);
4547 nstack += ARM_INT_REGISTER_SIZE;
4550 /* Doubleword aligned quantities must go in even register pairs. */
4551 if (may_use_core_reg
4552 && argreg <= ARM_LAST_ARG_REGNUM
4553 && align > ARM_INT_REGISTER_SIZE
4554 && argreg & 1)
4555 argreg++;
4557 /* If the argument is a pointer to a function, and it is a
4558 Thumb function, create a LOCAL copy of the value and set
4559 the THUMB bit in it. */
4560 if (TYPE_CODE_PTR == typecode
4561 && target_type != NULL
4562 && TYPE_CODE_FUNC == check_typedef (target_type)->code ())
4564 CORE_ADDR regval = extract_unsigned_integer (val, len, byte_order);
4565 if (arm_pc_is_thumb (gdbarch, regval))
4567 bfd_byte *copy = (bfd_byte *) alloca (len);
4568 store_unsigned_integer (copy, len, byte_order,
4569 MAKE_THUMB_ADDR (regval));
4570 val = copy;
4574 /* Copy the argument to general registers or the stack in
4575 register-sized pieces. Large arguments are split between
4576 registers and stack. */
4577 while (len > 0)
4579 int partial_len = len < ARM_INT_REGISTER_SIZE
4580 ? len : ARM_INT_REGISTER_SIZE;
4581 CORE_ADDR regval
4582 = extract_unsigned_integer (val, partial_len, byte_order);
4584 if (may_use_core_reg && argreg <= ARM_LAST_ARG_REGNUM)
4586 /* The argument is being passed in a general purpose
4587 register. */
4588 if (byte_order == BFD_ENDIAN_BIG)
4589 regval <<= (ARM_INT_REGISTER_SIZE - partial_len) * 8;
4591 arm_debug_printf ("arg %d in %s = 0x%s", argnum,
4592 gdbarch_register_name (gdbarch, argreg),
4593 phex (regval, ARM_INT_REGISTER_SIZE));
4595 regcache_cooked_write_unsigned (regcache, argreg, regval);
4596 argreg++;
4598 else
4600 gdb_byte buf[ARM_INT_REGISTER_SIZE];
4602 memset (buf, 0, sizeof (buf));
4603 store_unsigned_integer (buf, partial_len, byte_order, regval);
4605 /* Push the arguments onto the stack. */
4606 arm_debug_printf ("arg %d @ sp + %d", argnum, nstack);
4607 si = push_stack_item (si, buf, ARM_INT_REGISTER_SIZE);
4608 nstack += ARM_INT_REGISTER_SIZE;
4611 len -= partial_len;
4612 val += partial_len;
4615 /* If we have an odd number of words to push, then decrement the stack
4616 by one word now, so first stack argument will be dword aligned. */
4617 if (nstack & 4)
4618 sp -= 4;
4620 while (si)
4622 sp -= si->len;
4623 write_memory (sp, si->data, si->len);
4624 si = pop_stack_item (si);
4627 /* Finally, update teh SP register. */
4628 regcache_cooked_write_unsigned (regcache, ARM_SP_REGNUM, sp);
4630 return sp;
4634 /* Always align the frame to an 8-byte boundary. This is required on
4635 some platforms and harmless on the rest. */
4637 static CORE_ADDR
4638 arm_frame_align (struct gdbarch *gdbarch, CORE_ADDR sp)
4640 /* Align the stack to eight bytes. */
4641 return sp & ~ (CORE_ADDR) 7;
4644 static void
4645 print_fpu_flags (struct ui_file *file, int flags)
4647 if (flags & (1 << 0))
4648 gdb_puts ("IVO ", file);
4649 if (flags & (1 << 1))
4650 gdb_puts ("DVZ ", file);
4651 if (flags & (1 << 2))
4652 gdb_puts ("OFL ", file);
4653 if (flags & (1 << 3))
4654 gdb_puts ("UFL ", file);
4655 if (flags & (1 << 4))
4656 gdb_puts ("INX ", file);
4657 gdb_putc ('\n', file);
4660 /* Print interesting information about the floating point processor
4661 (if present) or emulator. */
4662 static void
4663 arm_print_float_info (struct gdbarch *gdbarch, struct ui_file *file,
4664 struct frame_info *frame, const char *args)
4666 unsigned long status = get_frame_register_unsigned (frame, ARM_FPS_REGNUM);
4667 int type;
4669 type = (status >> 24) & 127;
4670 if (status & (1 << 31))
4671 gdb_printf (file, _("Hardware FPU type %d\n"), type);
4672 else
4673 gdb_printf (file, _("Software FPU type %d\n"), type);
4674 /* i18n: [floating point unit] mask */
4675 gdb_puts (_("mask: "), file);
4676 print_fpu_flags (file, status >> 16);
4677 /* i18n: [floating point unit] flags */
4678 gdb_puts (_("flags: "), file);
4679 print_fpu_flags (file, status);
4682 /* Construct the ARM extended floating point type. */
4683 static struct type *
4684 arm_ext_type (struct gdbarch *gdbarch)
4686 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
4688 if (!tdep->arm_ext_type)
4689 tdep->arm_ext_type
4690 = arch_float_type (gdbarch, -1, "builtin_type_arm_ext",
4691 floatformats_arm_ext);
4693 return tdep->arm_ext_type;
4696 static struct type *
4697 arm_neon_double_type (struct gdbarch *gdbarch)
4699 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
4701 if (tdep->neon_double_type == NULL)
4703 struct type *t, *elem;
4705 t = arch_composite_type (gdbarch, "__gdb_builtin_type_neon_d",
4706 TYPE_CODE_UNION);
4707 elem = builtin_type (gdbarch)->builtin_uint8;
4708 append_composite_type_field (t, "u8", init_vector_type (elem, 8));
4709 elem = builtin_type (gdbarch)->builtin_uint16;
4710 append_composite_type_field (t, "u16", init_vector_type (elem, 4));
4711 elem = builtin_type (gdbarch)->builtin_uint32;
4712 append_composite_type_field (t, "u32", init_vector_type (elem, 2));
4713 elem = builtin_type (gdbarch)->builtin_uint64;
4714 append_composite_type_field (t, "u64", elem);
4715 elem = builtin_type (gdbarch)->builtin_float;
4716 append_composite_type_field (t, "f32", init_vector_type (elem, 2));
4717 elem = builtin_type (gdbarch)->builtin_double;
4718 append_composite_type_field (t, "f64", elem);
4720 t->set_is_vector (true);
4721 t->set_name ("neon_d");
4722 tdep->neon_double_type = t;
4725 return tdep->neon_double_type;
4728 /* FIXME: The vector types are not correctly ordered on big-endian
4729 targets. Just as s0 is the low bits of d0, d0[0] is also the low
4730 bits of d0 - regardless of what unit size is being held in d0. So
4731 the offset of the first uint8 in d0 is 7, but the offset of the
4732 first float is 4. This code works as-is for little-endian
4733 targets. */
4735 static struct type *
4736 arm_neon_quad_type (struct gdbarch *gdbarch)
4738 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
4740 if (tdep->neon_quad_type == NULL)
4742 struct type *t, *elem;
4744 t = arch_composite_type (gdbarch, "__gdb_builtin_type_neon_q",
4745 TYPE_CODE_UNION);
4746 elem = builtin_type (gdbarch)->builtin_uint8;
4747 append_composite_type_field (t, "u8", init_vector_type (elem, 16));
4748 elem = builtin_type (gdbarch)->builtin_uint16;
4749 append_composite_type_field (t, "u16", init_vector_type (elem, 8));
4750 elem = builtin_type (gdbarch)->builtin_uint32;
4751 append_composite_type_field (t, "u32", init_vector_type (elem, 4));
4752 elem = builtin_type (gdbarch)->builtin_uint64;
4753 append_composite_type_field (t, "u64", init_vector_type (elem, 2));
4754 elem = builtin_type (gdbarch)->builtin_float;
4755 append_composite_type_field (t, "f32", init_vector_type (elem, 4));
4756 elem = builtin_type (gdbarch)->builtin_double;
4757 append_composite_type_field (t, "f64", init_vector_type (elem, 2));
4759 t->set_is_vector (true);
4760 t->set_name ("neon_q");
4761 tdep->neon_quad_type = t;
4764 return tdep->neon_quad_type;
4767 /* Return true if REGNUM is a Q pseudo register. Return false
4768 otherwise.
4770 REGNUM is the raw register number and not a pseudo-relative register
4771 number. */
4773 static bool
4774 is_q_pseudo (struct gdbarch *gdbarch, int regnum)
4776 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
4778 /* Q pseudo registers are available for both NEON (Q0~Q15) and
4779 MVE (Q0~Q7) features. */
4780 if (tdep->have_q_pseudos
4781 && regnum >= tdep->q_pseudo_base
4782 && regnum < (tdep->q_pseudo_base + tdep->q_pseudo_count))
4783 return true;
4785 return false;
4788 /* Return true if REGNUM is a VFP S pseudo register. Return false
4789 otherwise.
4791 REGNUM is the raw register number and not a pseudo-relative register
4792 number. */
4794 static bool
4795 is_s_pseudo (struct gdbarch *gdbarch, int regnum)
4797 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
4799 if (tdep->have_s_pseudos
4800 && regnum >= tdep->s_pseudo_base
4801 && regnum < (tdep->s_pseudo_base + tdep->s_pseudo_count))
4802 return true;
4804 return false;
4807 /* Return true if REGNUM is a MVE pseudo register (P0). Return false
4808 otherwise.
4810 REGNUM is the raw register number and not a pseudo-relative register
4811 number. */
4813 static bool
4814 is_mve_pseudo (struct gdbarch *gdbarch, int regnum)
4816 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
4818 if (tdep->have_mve
4819 && regnum >= tdep->mve_pseudo_base
4820 && regnum < tdep->mve_pseudo_base + tdep->mve_pseudo_count)
4821 return true;
4823 return false;
4826 /* Return true if REGNUM is a PACBTI pseudo register (ra_auth_code). Return
4827 false otherwise.
4829 REGNUM is the raw register number and not a pseudo-relative register
4830 number. */
4832 static bool
4833 is_pacbti_pseudo (struct gdbarch *gdbarch, int regnum)
4835 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
4837 if (tdep->have_pacbti
4838 && regnum >= tdep->pacbti_pseudo_base
4839 && regnum < tdep->pacbti_pseudo_base + tdep->pacbti_pseudo_count)
4840 return true;
4842 return false;
4845 /* Return the GDB type object for the "standard" data type of data in
4846 register N. */
4848 static struct type *
4849 arm_register_type (struct gdbarch *gdbarch, int regnum)
4851 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
4853 if (is_s_pseudo (gdbarch, regnum))
4854 return builtin_type (gdbarch)->builtin_float;
4856 if (is_q_pseudo (gdbarch, regnum))
4857 return arm_neon_quad_type (gdbarch);
4859 if (is_mve_pseudo (gdbarch, regnum))
4860 return builtin_type (gdbarch)->builtin_int16;
4862 if (is_pacbti_pseudo (gdbarch, regnum))
4863 return builtin_type (gdbarch)->builtin_uint32;
4865 /* If the target description has register information, we are only
4866 in this function so that we can override the types of
4867 double-precision registers for NEON. */
4868 if (tdesc_has_registers (gdbarch_target_desc (gdbarch)))
4870 struct type *t = tdesc_register_type (gdbarch, regnum);
4872 if (regnum >= ARM_D0_REGNUM && regnum < ARM_D0_REGNUM + 32
4873 && t->code () == TYPE_CODE_FLT
4874 && tdep->have_neon)
4875 return arm_neon_double_type (gdbarch);
4876 else
4877 return t;
4880 if (regnum >= ARM_F0_REGNUM && regnum < ARM_F0_REGNUM + NUM_FREGS)
4882 if (!tdep->have_fpa_registers)
4883 return builtin_type (gdbarch)->builtin_void;
4885 return arm_ext_type (gdbarch);
4887 else if (regnum == ARM_SP_REGNUM)
4888 return builtin_type (gdbarch)->builtin_data_ptr;
4889 else if (regnum == ARM_PC_REGNUM)
4890 return builtin_type (gdbarch)->builtin_func_ptr;
4891 else if (regnum >= ARRAY_SIZE (arm_register_names))
4892 /* These registers are only supported on targets which supply
4893 an XML description. */
4894 return builtin_type (gdbarch)->builtin_int0;
4895 else
4896 return builtin_type (gdbarch)->builtin_uint32;
4899 /* Map a DWARF register REGNUM onto the appropriate GDB register
4900 number. */
4902 static int
4903 arm_dwarf_reg_to_regnum (struct gdbarch *gdbarch, int reg)
4905 /* Core integer regs. */
4906 if (reg >= 0 && reg <= 15)
4907 return reg;
4909 /* Legacy FPA encoding. These were once used in a way which
4910 overlapped with VFP register numbering, so their use is
4911 discouraged, but GDB doesn't support the ARM toolchain
4912 which used them for VFP. */
4913 if (reg >= 16 && reg <= 23)
4914 return ARM_F0_REGNUM + reg - 16;
4916 /* New assignments for the FPA registers. */
4917 if (reg >= 96 && reg <= 103)
4918 return ARM_F0_REGNUM + reg - 96;
4920 /* WMMX register assignments. */
4921 if (reg >= 104 && reg <= 111)
4922 return ARM_WCGR0_REGNUM + reg - 104;
4924 if (reg >= 112 && reg <= 127)
4925 return ARM_WR0_REGNUM + reg - 112;
4927 /* PACBTI register containing the Pointer Authentication Code. */
4928 if (reg == ARM_DWARF_RA_AUTH_CODE)
4930 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
4932 if (tdep->have_pacbti)
4933 return tdep->pacbti_pseudo_base;
4935 return -1;
4938 if (reg >= 192 && reg <= 199)
4939 return ARM_WC0_REGNUM + reg - 192;
4941 /* VFP v2 registers. A double precision value is actually
4942 in d1 rather than s2, but the ABI only defines numbering
4943 for the single precision registers. This will "just work"
4944 in GDB for little endian targets (we'll read eight bytes,
4945 starting in s0 and then progressing to s1), but will be
4946 reversed on big endian targets with VFP. This won't
4947 be a problem for the new Neon quad registers; you're supposed
4948 to use DW_OP_piece for those. */
4949 if (reg >= 64 && reg <= 95)
4951 char name_buf[4];
4953 xsnprintf (name_buf, sizeof (name_buf), "s%d", reg - 64);
4954 return user_reg_map_name_to_regnum (gdbarch, name_buf,
4955 strlen (name_buf));
4958 /* VFP v3 / Neon registers. This range is also used for VFP v2
4959 registers, except that it now describes d0 instead of s0. */
4960 if (reg >= 256 && reg <= 287)
4962 char name_buf[4];
4964 xsnprintf (name_buf, sizeof (name_buf), "d%d", reg - 256);
4965 return user_reg_map_name_to_regnum (gdbarch, name_buf,
4966 strlen (name_buf));
4969 return -1;
4972 /* Map GDB internal REGNUM onto the Arm simulator register numbers. */
4973 static int
4974 arm_register_sim_regno (struct gdbarch *gdbarch, int regnum)
4976 int reg = regnum;
4977 gdb_assert (reg >= 0 && reg < gdbarch_num_regs (gdbarch));
4979 if (regnum >= ARM_WR0_REGNUM && regnum <= ARM_WR15_REGNUM)
4980 return regnum - ARM_WR0_REGNUM + SIM_ARM_IWMMXT_COP0R0_REGNUM;
4982 if (regnum >= ARM_WC0_REGNUM && regnum <= ARM_WC7_REGNUM)
4983 return regnum - ARM_WC0_REGNUM + SIM_ARM_IWMMXT_COP1R0_REGNUM;
4985 if (regnum >= ARM_WCGR0_REGNUM && regnum <= ARM_WCGR7_REGNUM)
4986 return regnum - ARM_WCGR0_REGNUM + SIM_ARM_IWMMXT_COP1R8_REGNUM;
4988 if (reg < NUM_GREGS)
4989 return SIM_ARM_R0_REGNUM + reg;
4990 reg -= NUM_GREGS;
4992 if (reg < NUM_FREGS)
4993 return SIM_ARM_FP0_REGNUM + reg;
4994 reg -= NUM_FREGS;
4996 if (reg < NUM_SREGS)
4997 return SIM_ARM_FPS_REGNUM + reg;
4998 reg -= NUM_SREGS;
5000 internal_error (__FILE__, __LINE__, _("Bad REGNUM %d"), regnum);
5003 static const unsigned char op_lit0 = DW_OP_lit0;
5005 static void
5006 arm_dwarf2_frame_init_reg (struct gdbarch *gdbarch, int regnum,
5007 struct dwarf2_frame_state_reg *reg,
5008 struct frame_info *this_frame)
5010 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
5012 if (is_pacbti_pseudo (gdbarch, regnum))
5014 /* Initialize RA_AUTH_CODE to zero. */
5015 reg->how = DWARF2_FRAME_REG_SAVED_VAL_EXP;
5016 reg->loc.exp.start = &op_lit0;
5017 reg->loc.exp.len = 1;
5018 return;
5021 if (regnum == ARM_PC_REGNUM || regnum == ARM_PS_REGNUM)
5023 reg->how = DWARF2_FRAME_REG_FN;
5024 reg->loc.fn = arm_dwarf2_prev_register;
5026 else if (regnum == ARM_SP_REGNUM)
5027 reg->how = DWARF2_FRAME_REG_CFA;
5028 else if (arm_is_alternative_sp_register (tdep, regnum))
5030 /* Handle the alternative SP registers on Cortex-M. */
5031 reg->how = DWARF2_FRAME_REG_FN;
5032 reg->loc.fn = arm_dwarf2_prev_register;
5036 /* Given BUF, which is OLD_LEN bytes ending at ENDADDR, expand
5037 the buffer to be NEW_LEN bytes ending at ENDADDR. Return
5038 NULL if an error occurs. BUF is freed. */
5040 static gdb_byte *
5041 extend_buffer_earlier (gdb_byte *buf, CORE_ADDR endaddr,
5042 int old_len, int new_len)
5044 gdb_byte *new_buf;
5045 int bytes_to_read = new_len - old_len;
5047 new_buf = (gdb_byte *) xmalloc (new_len);
5048 memcpy (new_buf + bytes_to_read, buf, old_len);
5049 xfree (buf);
5050 if (target_read_code (endaddr - new_len, new_buf, bytes_to_read) != 0)
5052 xfree (new_buf);
5053 return NULL;
5055 return new_buf;
5058 /* An IT block is at most the 2-byte IT instruction followed by
5059 four 4-byte instructions. The furthest back we must search to
5060 find an IT block that affects the current instruction is thus
5061 2 + 3 * 4 == 14 bytes. */
5062 #define MAX_IT_BLOCK_PREFIX 14
5064 /* Use a quick scan if there are more than this many bytes of
5065 code. */
5066 #define IT_SCAN_THRESHOLD 32
5068 /* Adjust a breakpoint's address to move breakpoints out of IT blocks.
5069 A breakpoint in an IT block may not be hit, depending on the
5070 condition flags. */
5071 static CORE_ADDR
5072 arm_adjust_breakpoint_address (struct gdbarch *gdbarch, CORE_ADDR bpaddr)
5074 gdb_byte *buf;
5075 char map_type;
5076 CORE_ADDR boundary, func_start;
5077 int buf_len;
5078 enum bfd_endian order = gdbarch_byte_order_for_code (gdbarch);
5079 int i, any, last_it, last_it_count;
5080 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
5082 /* If we are using BKPT breakpoints, none of this is necessary. */
5083 if (tdep->thumb2_breakpoint == NULL)
5084 return bpaddr;
5086 /* ARM mode does not have this problem. */
5087 if (!arm_pc_is_thumb (gdbarch, bpaddr))
5088 return bpaddr;
5090 /* We are setting a breakpoint in Thumb code that could potentially
5091 contain an IT block. The first step is to find how much Thumb
5092 code there is; we do not need to read outside of known Thumb
5093 sequences. */
5094 map_type = arm_find_mapping_symbol (bpaddr, &boundary);
5095 if (map_type == 0)
5096 /* Thumb-2 code must have mapping symbols to have a chance. */
5097 return bpaddr;
5099 bpaddr = gdbarch_addr_bits_remove (gdbarch, bpaddr);
5101 if (find_pc_partial_function (bpaddr, NULL, &func_start, NULL)
5102 && func_start > boundary)
5103 boundary = func_start;
5105 /* Search for a candidate IT instruction. We have to do some fancy
5106 footwork to distinguish a real IT instruction from the second
5107 half of a 32-bit instruction, but there is no need for that if
5108 there's no candidate. */
5109 buf_len = std::min (bpaddr - boundary, (CORE_ADDR) MAX_IT_BLOCK_PREFIX);
5110 if (buf_len == 0)
5111 /* No room for an IT instruction. */
5112 return bpaddr;
5114 buf = (gdb_byte *) xmalloc (buf_len);
5115 if (target_read_code (bpaddr - buf_len, buf, buf_len) != 0)
5116 return bpaddr;
5117 any = 0;
5118 for (i = 0; i < buf_len; i += 2)
5120 unsigned short inst1 = extract_unsigned_integer (&buf[i], 2, order);
5121 if ((inst1 & 0xff00) == 0xbf00 && (inst1 & 0x000f) != 0)
5123 any = 1;
5124 break;
5128 if (any == 0)
5130 xfree (buf);
5131 return bpaddr;
5134 /* OK, the code bytes before this instruction contain at least one
5135 halfword which resembles an IT instruction. We know that it's
5136 Thumb code, but there are still two possibilities. Either the
5137 halfword really is an IT instruction, or it is the second half of
5138 a 32-bit Thumb instruction. The only way we can tell is to
5139 scan forwards from a known instruction boundary. */
5140 if (bpaddr - boundary > IT_SCAN_THRESHOLD)
5142 int definite;
5144 /* There's a lot of code before this instruction. Start with an
5145 optimistic search; it's easy to recognize halfwords that can
5146 not be the start of a 32-bit instruction, and use that to
5147 lock on to the instruction boundaries. */
5148 buf = extend_buffer_earlier (buf, bpaddr, buf_len, IT_SCAN_THRESHOLD);
5149 if (buf == NULL)
5150 return bpaddr;
5151 buf_len = IT_SCAN_THRESHOLD;
5153 definite = 0;
5154 for (i = 0; i < buf_len - sizeof (buf) && ! definite; i += 2)
5156 unsigned short inst1 = extract_unsigned_integer (&buf[i], 2, order);
5157 if (thumb_insn_size (inst1) == 2)
5159 definite = 1;
5160 break;
5164 /* At this point, if DEFINITE, BUF[I] is the first place we
5165 are sure that we know the instruction boundaries, and it is far
5166 enough from BPADDR that we could not miss an IT instruction
5167 affecting BPADDR. If ! DEFINITE, give up - start from a
5168 known boundary. */
5169 if (! definite)
5171 buf = extend_buffer_earlier (buf, bpaddr, buf_len,
5172 bpaddr - boundary);
5173 if (buf == NULL)
5174 return bpaddr;
5175 buf_len = bpaddr - boundary;
5176 i = 0;
5179 else
5181 buf = extend_buffer_earlier (buf, bpaddr, buf_len, bpaddr - boundary);
5182 if (buf == NULL)
5183 return bpaddr;
5184 buf_len = bpaddr - boundary;
5185 i = 0;
5188 /* Scan forwards. Find the last IT instruction before BPADDR. */
5189 last_it = -1;
5190 last_it_count = 0;
5191 while (i < buf_len)
5193 unsigned short inst1 = extract_unsigned_integer (&buf[i], 2, order);
5194 last_it_count--;
5195 if ((inst1 & 0xff00) == 0xbf00 && (inst1 & 0x000f) != 0)
5197 last_it = i;
5198 if (inst1 & 0x0001)
5199 last_it_count = 4;
5200 else if (inst1 & 0x0002)
5201 last_it_count = 3;
5202 else if (inst1 & 0x0004)
5203 last_it_count = 2;
5204 else
5205 last_it_count = 1;
5207 i += thumb_insn_size (inst1);
5210 xfree (buf);
5212 if (last_it == -1)
5213 /* There wasn't really an IT instruction after all. */
5214 return bpaddr;
5216 if (last_it_count < 1)
5217 /* It was too far away. */
5218 return bpaddr;
5220 /* This really is a trouble spot. Move the breakpoint to the IT
5221 instruction. */
5222 return bpaddr - buf_len + last_it;
5225 /* ARM displaced stepping support.
5227 Generally ARM displaced stepping works as follows:
5229 1. When an instruction is to be single-stepped, it is first decoded by
5230 arm_process_displaced_insn. Depending on the type of instruction, it is
5231 then copied to a scratch location, possibly in a modified form. The
5232 copy_* set of functions performs such modification, as necessary. A
5233 breakpoint is placed after the modified instruction in the scratch space
5234 to return control to GDB. Note in particular that instructions which
5235 modify the PC will no longer do so after modification.
5237 2. The instruction is single-stepped, by setting the PC to the scratch
5238 location address, and resuming. Control returns to GDB when the
5239 breakpoint is hit.
5241 3. A cleanup function (cleanup_*) is called corresponding to the copy_*
5242 function used for the current instruction. This function's job is to
5243 put the CPU/memory state back to what it would have been if the
5244 instruction had been executed unmodified in its original location. */
5246 /* NOP instruction (mov r0, r0). */
5247 #define ARM_NOP 0xe1a00000
5248 #define THUMB_NOP 0x4600
5250 /* Helper for register reads for displaced stepping. In particular, this
5251 returns the PC as it would be seen by the instruction at its original
5252 location. */
5254 ULONGEST
5255 displaced_read_reg (regcache *regs, arm_displaced_step_copy_insn_closure *dsc,
5256 int regno)
5258 ULONGEST ret;
5259 CORE_ADDR from = dsc->insn_addr;
5261 if (regno == ARM_PC_REGNUM)
5263 /* Compute pipeline offset:
5264 - When executing an ARM instruction, PC reads as the address of the
5265 current instruction plus 8.
5266 - When executing a Thumb instruction, PC reads as the address of the
5267 current instruction plus 4. */
5269 if (!dsc->is_thumb)
5270 from += 8;
5271 else
5272 from += 4;
5274 displaced_debug_printf ("read pc value %.8lx",
5275 (unsigned long) from);
5276 return (ULONGEST) from;
5278 else
5280 regcache_cooked_read_unsigned (regs, regno, &ret);
5282 displaced_debug_printf ("read r%d value %.8lx",
5283 regno, (unsigned long) ret);
5285 return ret;
5289 static int
5290 displaced_in_arm_mode (struct regcache *regs)
5292 ULONGEST ps;
5293 ULONGEST t_bit = arm_psr_thumb_bit (regs->arch ());
5295 regcache_cooked_read_unsigned (regs, ARM_PS_REGNUM, &ps);
5297 return (ps & t_bit) == 0;
5300 /* Write to the PC as from a branch instruction. */
5302 static void
5303 branch_write_pc (regcache *regs, arm_displaced_step_copy_insn_closure *dsc,
5304 ULONGEST val)
5306 if (!dsc->is_thumb)
5307 /* Note: If bits 0/1 are set, this branch would be unpredictable for
5308 architecture versions < 6. */
5309 regcache_cooked_write_unsigned (regs, ARM_PC_REGNUM,
5310 val & ~(ULONGEST) 0x3);
5311 else
5312 regcache_cooked_write_unsigned (regs, ARM_PC_REGNUM,
5313 val & ~(ULONGEST) 0x1);
5316 /* Write to the PC as from a branch-exchange instruction. */
5318 static void
5319 bx_write_pc (struct regcache *regs, ULONGEST val)
5321 ULONGEST ps;
5322 ULONGEST t_bit = arm_psr_thumb_bit (regs->arch ());
5324 regcache_cooked_read_unsigned (regs, ARM_PS_REGNUM, &ps);
5326 if ((val & 1) == 1)
5328 regcache_cooked_write_unsigned (regs, ARM_PS_REGNUM, ps | t_bit);
5329 regcache_cooked_write_unsigned (regs, ARM_PC_REGNUM, val & 0xfffffffe);
5331 else if ((val & 2) == 0)
5333 regcache_cooked_write_unsigned (regs, ARM_PS_REGNUM, ps & ~t_bit);
5334 regcache_cooked_write_unsigned (regs, ARM_PC_REGNUM, val);
5336 else
5338 /* Unpredictable behaviour. Try to do something sensible (switch to ARM
5339 mode, align dest to 4 bytes). */
5340 warning (_("Single-stepping BX to non-word-aligned ARM instruction."));
5341 regcache_cooked_write_unsigned (regs, ARM_PS_REGNUM, ps & ~t_bit);
5342 regcache_cooked_write_unsigned (regs, ARM_PC_REGNUM, val & 0xfffffffc);
5346 /* Write to the PC as if from a load instruction. */
5348 static void
5349 load_write_pc (regcache *regs, arm_displaced_step_copy_insn_closure *dsc,
5350 ULONGEST val)
5352 if (DISPLACED_STEPPING_ARCH_VERSION >= 5)
5353 bx_write_pc (regs, val);
5354 else
5355 branch_write_pc (regs, dsc, val);
5358 /* Write to the PC as if from an ALU instruction. */
5360 static void
5361 alu_write_pc (regcache *regs, arm_displaced_step_copy_insn_closure *dsc,
5362 ULONGEST val)
5364 if (DISPLACED_STEPPING_ARCH_VERSION >= 7 && !dsc->is_thumb)
5365 bx_write_pc (regs, val);
5366 else
5367 branch_write_pc (regs, dsc, val);
5370 /* Helper for writing to registers for displaced stepping. Writing to the PC
5371 has a varying effects depending on the instruction which does the write:
5372 this is controlled by the WRITE_PC argument. */
5374 void
5375 displaced_write_reg (regcache *regs, arm_displaced_step_copy_insn_closure *dsc,
5376 int regno, ULONGEST val, enum pc_write_style write_pc)
5378 if (regno == ARM_PC_REGNUM)
5380 displaced_debug_printf ("writing pc %.8lx", (unsigned long) val);
5382 switch (write_pc)
5384 case BRANCH_WRITE_PC:
5385 branch_write_pc (regs, dsc, val);
5386 break;
5388 case BX_WRITE_PC:
5389 bx_write_pc (regs, val);
5390 break;
5392 case LOAD_WRITE_PC:
5393 load_write_pc (regs, dsc, val);
5394 break;
5396 case ALU_WRITE_PC:
5397 alu_write_pc (regs, dsc, val);
5398 break;
5400 case CANNOT_WRITE_PC:
5401 warning (_("Instruction wrote to PC in an unexpected way when "
5402 "single-stepping"));
5403 break;
5405 default:
5406 internal_error (__FILE__, __LINE__,
5407 _("Invalid argument to displaced_write_reg"));
5410 dsc->wrote_to_pc = 1;
5412 else
5414 displaced_debug_printf ("writing r%d value %.8lx",
5415 regno, (unsigned long) val);
5416 regcache_cooked_write_unsigned (regs, regno, val);
5420 /* This function is used to concisely determine if an instruction INSN
5421 references PC. Register fields of interest in INSN should have the
5422 corresponding fields of BITMASK set to 0b1111. The function
5423 returns return 1 if any of these fields in INSN reference the PC
5424 (also 0b1111, r15), else it returns 0. */
5426 static int
5427 insn_references_pc (uint32_t insn, uint32_t bitmask)
5429 uint32_t lowbit = 1;
5431 while (bitmask != 0)
5433 uint32_t mask;
5435 for (; lowbit && (bitmask & lowbit) == 0; lowbit <<= 1)
5438 if (!lowbit)
5439 break;
5441 mask = lowbit * 0xf;
5443 if ((insn & mask) == mask)
5444 return 1;
5446 bitmask &= ~mask;
5449 return 0;
5452 /* The simplest copy function. Many instructions have the same effect no
5453 matter what address they are executed at: in those cases, use this. */
5455 static int
5456 arm_copy_unmodified (struct gdbarch *gdbarch, uint32_t insn, const char *iname,
5457 arm_displaced_step_copy_insn_closure *dsc)
5459 displaced_debug_printf ("copying insn %.8lx, opcode/class '%s' unmodified",
5460 (unsigned long) insn, iname);
5462 dsc->modinsn[0] = insn;
5464 return 0;
5467 static int
5468 thumb_copy_unmodified_32bit (struct gdbarch *gdbarch, uint16_t insn1,
5469 uint16_t insn2, const char *iname,
5470 arm_displaced_step_copy_insn_closure *dsc)
5472 displaced_debug_printf ("copying insn %.4x %.4x, opcode/class '%s' "
5473 "unmodified", insn1, insn2, iname);
5475 dsc->modinsn[0] = insn1;
5476 dsc->modinsn[1] = insn2;
5477 dsc->numinsns = 2;
5479 return 0;
5482 /* Copy 16-bit Thumb(Thumb and 16-bit Thumb-2) instruction without any
5483 modification. */
5484 static int
5485 thumb_copy_unmodified_16bit (struct gdbarch *gdbarch, uint16_t insn,
5486 const char *iname,
5487 arm_displaced_step_copy_insn_closure *dsc)
5489 displaced_debug_printf ("copying insn %.4x, opcode/class '%s' unmodified",
5490 insn, iname);
5492 dsc->modinsn[0] = insn;
5494 return 0;
5497 /* Preload instructions with immediate offset. */
5499 static void
5500 cleanup_preload (struct gdbarch *gdbarch, regcache *regs,
5501 arm_displaced_step_copy_insn_closure *dsc)
5503 displaced_write_reg (regs, dsc, 0, dsc->tmp[0], CANNOT_WRITE_PC);
5504 if (!dsc->u.preload.immed)
5505 displaced_write_reg (regs, dsc, 1, dsc->tmp[1], CANNOT_WRITE_PC);
5508 static void
5509 install_preload (struct gdbarch *gdbarch, struct regcache *regs,
5510 arm_displaced_step_copy_insn_closure *dsc, unsigned int rn)
5512 ULONGEST rn_val;
5513 /* Preload instructions:
5515 {pli/pld} [rn, #+/-imm]
5517 {pli/pld} [r0, #+/-imm]. */
5519 dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
5520 rn_val = displaced_read_reg (regs, dsc, rn);
5521 displaced_write_reg (regs, dsc, 0, rn_val, CANNOT_WRITE_PC);
5522 dsc->u.preload.immed = 1;
5524 dsc->cleanup = &cleanup_preload;
5527 static int
5528 arm_copy_preload (struct gdbarch *gdbarch, uint32_t insn, struct regcache *regs,
5529 arm_displaced_step_copy_insn_closure *dsc)
5531 unsigned int rn = bits (insn, 16, 19);
5533 if (!insn_references_pc (insn, 0x000f0000ul))
5534 return arm_copy_unmodified (gdbarch, insn, "preload", dsc);
5536 displaced_debug_printf ("copying preload insn %.8lx", (unsigned long) insn);
5538 dsc->modinsn[0] = insn & 0xfff0ffff;
5540 install_preload (gdbarch, regs, dsc, rn);
5542 return 0;
5545 static int
5546 thumb2_copy_preload (struct gdbarch *gdbarch, uint16_t insn1, uint16_t insn2,
5547 regcache *regs, arm_displaced_step_copy_insn_closure *dsc)
5549 unsigned int rn = bits (insn1, 0, 3);
5550 unsigned int u_bit = bit (insn1, 7);
5551 int imm12 = bits (insn2, 0, 11);
5552 ULONGEST pc_val;
5554 if (rn != ARM_PC_REGNUM)
5555 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2, "preload", dsc);
5557 /* PC is only allowed to use in PLI (immediate,literal) Encoding T3, and
5558 PLD (literal) Encoding T1. */
5559 displaced_debug_printf ("copying pld/pli pc (0x%x) %c imm12 %.4x",
5560 (unsigned int) dsc->insn_addr, u_bit ? '+' : '-',
5561 imm12);
5563 if (!u_bit)
5564 imm12 = -1 * imm12;
5566 /* Rewrite instruction {pli/pld} PC imm12 into:
5567 Prepare: tmp[0] <- r0, tmp[1] <- r1, r0 <- pc, r1 <- imm12
5569 {pli/pld} [r0, r1]
5571 Cleanup: r0 <- tmp[0], r1 <- tmp[1]. */
5573 dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
5574 dsc->tmp[1] = displaced_read_reg (regs, dsc, 1);
5576 pc_val = displaced_read_reg (regs, dsc, ARM_PC_REGNUM);
5578 displaced_write_reg (regs, dsc, 0, pc_val, CANNOT_WRITE_PC);
5579 displaced_write_reg (regs, dsc, 1, imm12, CANNOT_WRITE_PC);
5580 dsc->u.preload.immed = 0;
5582 /* {pli/pld} [r0, r1] */
5583 dsc->modinsn[0] = insn1 & 0xfff0;
5584 dsc->modinsn[1] = 0xf001;
5585 dsc->numinsns = 2;
5587 dsc->cleanup = &cleanup_preload;
5588 return 0;
5591 /* Preload instructions with register offset. */
5593 static void
5594 install_preload_reg(struct gdbarch *gdbarch, struct regcache *regs,
5595 arm_displaced_step_copy_insn_closure *dsc, unsigned int rn,
5596 unsigned int rm)
5598 ULONGEST rn_val, rm_val;
5600 /* Preload register-offset instructions:
5602 {pli/pld} [rn, rm {, shift}]
5604 {pli/pld} [r0, r1 {, shift}]. */
5606 dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
5607 dsc->tmp[1] = displaced_read_reg (regs, dsc, 1);
5608 rn_val = displaced_read_reg (regs, dsc, rn);
5609 rm_val = displaced_read_reg (regs, dsc, rm);
5610 displaced_write_reg (regs, dsc, 0, rn_val, CANNOT_WRITE_PC);
5611 displaced_write_reg (regs, dsc, 1, rm_val, CANNOT_WRITE_PC);
5612 dsc->u.preload.immed = 0;
5614 dsc->cleanup = &cleanup_preload;
5617 static int
5618 arm_copy_preload_reg (struct gdbarch *gdbarch, uint32_t insn,
5619 struct regcache *regs,
5620 arm_displaced_step_copy_insn_closure *dsc)
5622 unsigned int rn = bits (insn, 16, 19);
5623 unsigned int rm = bits (insn, 0, 3);
5626 if (!insn_references_pc (insn, 0x000f000ful))
5627 return arm_copy_unmodified (gdbarch, insn, "preload reg", dsc);
5629 displaced_debug_printf ("copying preload insn %.8lx",
5630 (unsigned long) insn);
5632 dsc->modinsn[0] = (insn & 0xfff0fff0) | 0x1;
5634 install_preload_reg (gdbarch, regs, dsc, rn, rm);
5635 return 0;
5638 /* Copy/cleanup coprocessor load and store instructions. */
5640 static void
5641 cleanup_copro_load_store (struct gdbarch *gdbarch,
5642 struct regcache *regs,
5643 arm_displaced_step_copy_insn_closure *dsc)
5645 ULONGEST rn_val = displaced_read_reg (regs, dsc, 0);
5647 displaced_write_reg (regs, dsc, 0, dsc->tmp[0], CANNOT_WRITE_PC);
5649 if (dsc->u.ldst.writeback)
5650 displaced_write_reg (regs, dsc, dsc->u.ldst.rn, rn_val, LOAD_WRITE_PC);
5653 static void
5654 install_copro_load_store (struct gdbarch *gdbarch, struct regcache *regs,
5655 arm_displaced_step_copy_insn_closure *dsc,
5656 int writeback, unsigned int rn)
5658 ULONGEST rn_val;
5660 /* Coprocessor load/store instructions:
5662 {stc/stc2} [<Rn>, #+/-imm] (and other immediate addressing modes)
5664 {stc/stc2} [r0, #+/-imm].
5666 ldc/ldc2 are handled identically. */
5668 dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
5669 rn_val = displaced_read_reg (regs, dsc, rn);
5670 /* PC should be 4-byte aligned. */
5671 rn_val = rn_val & 0xfffffffc;
5672 displaced_write_reg (regs, dsc, 0, rn_val, CANNOT_WRITE_PC);
5674 dsc->u.ldst.writeback = writeback;
5675 dsc->u.ldst.rn = rn;
5677 dsc->cleanup = &cleanup_copro_load_store;
5680 static int
5681 arm_copy_copro_load_store (struct gdbarch *gdbarch, uint32_t insn,
5682 struct regcache *regs,
5683 arm_displaced_step_copy_insn_closure *dsc)
5685 unsigned int rn = bits (insn, 16, 19);
5687 if (!insn_references_pc (insn, 0x000f0000ul))
5688 return arm_copy_unmodified (gdbarch, insn, "copro load/store", dsc);
5690 displaced_debug_printf ("copying coprocessor load/store insn %.8lx",
5691 (unsigned long) insn);
5693 dsc->modinsn[0] = insn & 0xfff0ffff;
5695 install_copro_load_store (gdbarch, regs, dsc, bit (insn, 25), rn);
5697 return 0;
5700 static int
5701 thumb2_copy_copro_load_store (struct gdbarch *gdbarch, uint16_t insn1,
5702 uint16_t insn2, struct regcache *regs,
5703 arm_displaced_step_copy_insn_closure *dsc)
5705 unsigned int rn = bits (insn1, 0, 3);
5707 if (rn != ARM_PC_REGNUM)
5708 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
5709 "copro load/store", dsc);
5711 displaced_debug_printf ("copying coprocessor load/store insn %.4x%.4x",
5712 insn1, insn2);
5714 dsc->modinsn[0] = insn1 & 0xfff0;
5715 dsc->modinsn[1] = insn2;
5716 dsc->numinsns = 2;
5718 /* This function is called for copying instruction LDC/LDC2/VLDR, which
5719 doesn't support writeback, so pass 0. */
5720 install_copro_load_store (gdbarch, regs, dsc, 0, rn);
5722 return 0;
5725 /* Clean up branch instructions (actually perform the branch, by setting
5726 PC). */
5728 static void
5729 cleanup_branch (struct gdbarch *gdbarch, struct regcache *regs,
5730 arm_displaced_step_copy_insn_closure *dsc)
5732 uint32_t status = displaced_read_reg (regs, dsc, ARM_PS_REGNUM);
5733 int branch_taken = condition_true (dsc->u.branch.cond, status);
5734 enum pc_write_style write_pc = dsc->u.branch.exchange
5735 ? BX_WRITE_PC : BRANCH_WRITE_PC;
5737 if (!branch_taken)
5738 return;
5740 if (dsc->u.branch.link)
5742 /* The value of LR should be the next insn of current one. In order
5743 not to confuse logic handling later insn `bx lr', if current insn mode
5744 is Thumb, the bit 0 of LR value should be set to 1. */
5745 ULONGEST next_insn_addr = dsc->insn_addr + dsc->insn_size;
5747 if (dsc->is_thumb)
5748 next_insn_addr |= 0x1;
5750 displaced_write_reg (regs, dsc, ARM_LR_REGNUM, next_insn_addr,
5751 CANNOT_WRITE_PC);
5754 displaced_write_reg (regs, dsc, ARM_PC_REGNUM, dsc->u.branch.dest, write_pc);
5757 /* Copy B/BL/BLX instructions with immediate destinations. */
5759 static void
5760 install_b_bl_blx (struct gdbarch *gdbarch, struct regcache *regs,
5761 arm_displaced_step_copy_insn_closure *dsc,
5762 unsigned int cond, int exchange, int link, long offset)
5764 /* Implement "BL<cond> <label>" as:
5766 Preparation: cond <- instruction condition
5767 Insn: mov r0, r0 (nop)
5768 Cleanup: if (condition true) { r14 <- pc; pc <- label }.
5770 B<cond> similar, but don't set r14 in cleanup. */
5772 dsc->u.branch.cond = cond;
5773 dsc->u.branch.link = link;
5774 dsc->u.branch.exchange = exchange;
5776 dsc->u.branch.dest = dsc->insn_addr;
5777 if (link && exchange)
5778 /* For BLX, offset is computed from the Align (PC, 4). */
5779 dsc->u.branch.dest = dsc->u.branch.dest & 0xfffffffc;
5781 if (dsc->is_thumb)
5782 dsc->u.branch.dest += 4 + offset;
5783 else
5784 dsc->u.branch.dest += 8 + offset;
5786 dsc->cleanup = &cleanup_branch;
5788 static int
5789 arm_copy_b_bl_blx (struct gdbarch *gdbarch, uint32_t insn,
5790 regcache *regs, arm_displaced_step_copy_insn_closure *dsc)
5792 unsigned int cond = bits (insn, 28, 31);
5793 int exchange = (cond == 0xf);
5794 int link = exchange || bit (insn, 24);
5795 long offset;
5797 displaced_debug_printf ("copying %s immediate insn %.8lx",
5798 (exchange) ? "blx" : (link) ? "bl" : "b",
5799 (unsigned long) insn);
5800 if (exchange)
5801 /* For BLX, set bit 0 of the destination. The cleanup_branch function will
5802 then arrange the switch into Thumb mode. */
5803 offset = (bits (insn, 0, 23) << 2) | (bit (insn, 24) << 1) | 1;
5804 else
5805 offset = bits (insn, 0, 23) << 2;
5807 if (bit (offset, 25))
5808 offset = offset | ~0x3ffffff;
5810 dsc->modinsn[0] = ARM_NOP;
5812 install_b_bl_blx (gdbarch, regs, dsc, cond, exchange, link, offset);
5813 return 0;
5816 static int
5817 thumb2_copy_b_bl_blx (struct gdbarch *gdbarch, uint16_t insn1,
5818 uint16_t insn2, struct regcache *regs,
5819 arm_displaced_step_copy_insn_closure *dsc)
5821 int link = bit (insn2, 14);
5822 int exchange = link && !bit (insn2, 12);
5823 int cond = INST_AL;
5824 long offset = 0;
5825 int j1 = bit (insn2, 13);
5826 int j2 = bit (insn2, 11);
5827 int s = sbits (insn1, 10, 10);
5828 int i1 = !(j1 ^ bit (insn1, 10));
5829 int i2 = !(j2 ^ bit (insn1, 10));
5831 if (!link && !exchange) /* B */
5833 offset = (bits (insn2, 0, 10) << 1);
5834 if (bit (insn2, 12)) /* Encoding T4 */
5836 offset |= (bits (insn1, 0, 9) << 12)
5837 | (i2 << 22)
5838 | (i1 << 23)
5839 | (s << 24);
5840 cond = INST_AL;
5842 else /* Encoding T3 */
5844 offset |= (bits (insn1, 0, 5) << 12)
5845 | (j1 << 18)
5846 | (j2 << 19)
5847 | (s << 20);
5848 cond = bits (insn1, 6, 9);
5851 else
5853 offset = (bits (insn1, 0, 9) << 12);
5854 offset |= ((i2 << 22) | (i1 << 23) | (s << 24));
5855 offset |= exchange ?
5856 (bits (insn2, 1, 10) << 2) : (bits (insn2, 0, 10) << 1);
5859 displaced_debug_printf ("copying %s insn %.4x %.4x with offset %.8lx",
5860 link ? (exchange) ? "blx" : "bl" : "b",
5861 insn1, insn2, offset);
5863 dsc->modinsn[0] = THUMB_NOP;
5865 install_b_bl_blx (gdbarch, regs, dsc, cond, exchange, link, offset);
5866 return 0;
5869 /* Copy B Thumb instructions. */
5870 static int
5871 thumb_copy_b (struct gdbarch *gdbarch, uint16_t insn,
5872 arm_displaced_step_copy_insn_closure *dsc)
5874 unsigned int cond = 0;
5875 int offset = 0;
5876 unsigned short bit_12_15 = bits (insn, 12, 15);
5877 CORE_ADDR from = dsc->insn_addr;
5879 if (bit_12_15 == 0xd)
5881 /* offset = SignExtend (imm8:0, 32) */
5882 offset = sbits ((insn << 1), 0, 8);
5883 cond = bits (insn, 8, 11);
5885 else if (bit_12_15 == 0xe) /* Encoding T2 */
5887 offset = sbits ((insn << 1), 0, 11);
5888 cond = INST_AL;
5891 displaced_debug_printf ("copying b immediate insn %.4x with offset %d",
5892 insn, offset);
5894 dsc->u.branch.cond = cond;
5895 dsc->u.branch.link = 0;
5896 dsc->u.branch.exchange = 0;
5897 dsc->u.branch.dest = from + 4 + offset;
5899 dsc->modinsn[0] = THUMB_NOP;
5901 dsc->cleanup = &cleanup_branch;
5903 return 0;
5906 /* Copy BX/BLX with register-specified destinations. */
5908 static void
5909 install_bx_blx_reg (struct gdbarch *gdbarch, struct regcache *regs,
5910 arm_displaced_step_copy_insn_closure *dsc, int link,
5911 unsigned int cond, unsigned int rm)
5913 /* Implement {BX,BLX}<cond> <reg>" as:
5915 Preparation: cond <- instruction condition
5916 Insn: mov r0, r0 (nop)
5917 Cleanup: if (condition true) { r14 <- pc; pc <- dest; }.
5919 Don't set r14 in cleanup for BX. */
5921 dsc->u.branch.dest = displaced_read_reg (regs, dsc, rm);
5923 dsc->u.branch.cond = cond;
5924 dsc->u.branch.link = link;
5926 dsc->u.branch.exchange = 1;
5928 dsc->cleanup = &cleanup_branch;
5931 static int
5932 arm_copy_bx_blx_reg (struct gdbarch *gdbarch, uint32_t insn,
5933 regcache *regs, arm_displaced_step_copy_insn_closure *dsc)
5935 unsigned int cond = bits (insn, 28, 31);
5936 /* BX: x12xxx1x
5937 BLX: x12xxx3x. */
5938 int link = bit (insn, 5);
5939 unsigned int rm = bits (insn, 0, 3);
5941 displaced_debug_printf ("copying insn %.8lx", (unsigned long) insn);
5943 dsc->modinsn[0] = ARM_NOP;
5945 install_bx_blx_reg (gdbarch, regs, dsc, link, cond, rm);
5946 return 0;
5949 static int
5950 thumb_copy_bx_blx_reg (struct gdbarch *gdbarch, uint16_t insn,
5951 struct regcache *regs,
5952 arm_displaced_step_copy_insn_closure *dsc)
5954 int link = bit (insn, 7);
5955 unsigned int rm = bits (insn, 3, 6);
5957 displaced_debug_printf ("copying insn %.4x", (unsigned short) insn);
5959 dsc->modinsn[0] = THUMB_NOP;
5961 install_bx_blx_reg (gdbarch, regs, dsc, link, INST_AL, rm);
5963 return 0;
5967 /* Copy/cleanup arithmetic/logic instruction with immediate RHS. */
5969 static void
5970 cleanup_alu_imm (struct gdbarch *gdbarch,
5971 regcache *regs, arm_displaced_step_copy_insn_closure *dsc)
5973 ULONGEST rd_val = displaced_read_reg (regs, dsc, 0);
5974 displaced_write_reg (regs, dsc, 0, dsc->tmp[0], CANNOT_WRITE_PC);
5975 displaced_write_reg (regs, dsc, 1, dsc->tmp[1], CANNOT_WRITE_PC);
5976 displaced_write_reg (regs, dsc, dsc->rd, rd_val, ALU_WRITE_PC);
5979 static int
5980 arm_copy_alu_imm (struct gdbarch *gdbarch, uint32_t insn, struct regcache *regs,
5981 arm_displaced_step_copy_insn_closure *dsc)
5983 unsigned int rn = bits (insn, 16, 19);
5984 unsigned int rd = bits (insn, 12, 15);
5985 unsigned int op = bits (insn, 21, 24);
5986 int is_mov = (op == 0xd);
5987 ULONGEST rd_val, rn_val;
5989 if (!insn_references_pc (insn, 0x000ff000ul))
5990 return arm_copy_unmodified (gdbarch, insn, "ALU immediate", dsc);
5992 displaced_debug_printf ("copying immediate %s insn %.8lx",
5993 is_mov ? "move" : "ALU",
5994 (unsigned long) insn);
5996 /* Instruction is of form:
5998 <op><cond> rd, [rn,] #imm
6000 Rewrite as:
6002 Preparation: tmp1, tmp2 <- r0, r1;
6003 r0, r1 <- rd, rn
6004 Insn: <op><cond> r0, r1, #imm
6005 Cleanup: rd <- r0; r0 <- tmp1; r1 <- tmp2
6008 dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
6009 dsc->tmp[1] = displaced_read_reg (regs, dsc, 1);
6010 rn_val = displaced_read_reg (regs, dsc, rn);
6011 rd_val = displaced_read_reg (regs, dsc, rd);
6012 displaced_write_reg (regs, dsc, 0, rd_val, CANNOT_WRITE_PC);
6013 displaced_write_reg (regs, dsc, 1, rn_val, CANNOT_WRITE_PC);
6014 dsc->rd = rd;
6016 if (is_mov)
6017 dsc->modinsn[0] = insn & 0xfff00fff;
6018 else
6019 dsc->modinsn[0] = (insn & 0xfff00fff) | 0x10000;
6021 dsc->cleanup = &cleanup_alu_imm;
6023 return 0;
6026 static int
6027 thumb2_copy_alu_imm (struct gdbarch *gdbarch, uint16_t insn1,
6028 uint16_t insn2, struct regcache *regs,
6029 arm_displaced_step_copy_insn_closure *dsc)
6031 unsigned int op = bits (insn1, 5, 8);
6032 unsigned int rn, rm, rd;
6033 ULONGEST rd_val, rn_val;
6035 rn = bits (insn1, 0, 3); /* Rn */
6036 rm = bits (insn2, 0, 3); /* Rm */
6037 rd = bits (insn2, 8, 11); /* Rd */
6039 /* This routine is only called for instruction MOV. */
6040 gdb_assert (op == 0x2 && rn == 0xf);
6042 if (rm != ARM_PC_REGNUM && rd != ARM_PC_REGNUM)
6043 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2, "ALU imm", dsc);
6045 displaced_debug_printf ("copying reg %s insn %.4x%.4x", "ALU", insn1, insn2);
6047 /* Instruction is of form:
6049 <op><cond> rd, [rn,] #imm
6051 Rewrite as:
6053 Preparation: tmp1, tmp2 <- r0, r1;
6054 r0, r1 <- rd, rn
6055 Insn: <op><cond> r0, r1, #imm
6056 Cleanup: rd <- r0; r0 <- tmp1; r1 <- tmp2
6059 dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
6060 dsc->tmp[1] = displaced_read_reg (regs, dsc, 1);
6061 rn_val = displaced_read_reg (regs, dsc, rn);
6062 rd_val = displaced_read_reg (regs, dsc, rd);
6063 displaced_write_reg (regs, dsc, 0, rd_val, CANNOT_WRITE_PC);
6064 displaced_write_reg (regs, dsc, 1, rn_val, CANNOT_WRITE_PC);
6065 dsc->rd = rd;
6067 dsc->modinsn[0] = insn1;
6068 dsc->modinsn[1] = ((insn2 & 0xf0f0) | 0x1);
6069 dsc->numinsns = 2;
6071 dsc->cleanup = &cleanup_alu_imm;
6073 return 0;
6076 /* Copy/cleanup arithmetic/logic insns with register RHS. */
6078 static void
6079 cleanup_alu_reg (struct gdbarch *gdbarch,
6080 regcache *regs, arm_displaced_step_copy_insn_closure *dsc)
6082 ULONGEST rd_val;
6083 int i;
6085 rd_val = displaced_read_reg (regs, dsc, 0);
6087 for (i = 0; i < 3; i++)
6088 displaced_write_reg (regs, dsc, i, dsc->tmp[i], CANNOT_WRITE_PC);
6090 displaced_write_reg (regs, dsc, dsc->rd, rd_val, ALU_WRITE_PC);
6093 static void
6094 install_alu_reg (struct gdbarch *gdbarch, struct regcache *regs,
6095 arm_displaced_step_copy_insn_closure *dsc,
6096 unsigned int rd, unsigned int rn, unsigned int rm)
6098 ULONGEST rd_val, rn_val, rm_val;
6100 /* Instruction is of form:
6102 <op><cond> rd, [rn,] rm [, <shift>]
6104 Rewrite as:
6106 Preparation: tmp1, tmp2, tmp3 <- r0, r1, r2;
6107 r0, r1, r2 <- rd, rn, rm
6108 Insn: <op><cond> r0, [r1,] r2 [, <shift>]
6109 Cleanup: rd <- r0; r0, r1, r2 <- tmp1, tmp2, tmp3
6112 dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
6113 dsc->tmp[1] = displaced_read_reg (regs, dsc, 1);
6114 dsc->tmp[2] = displaced_read_reg (regs, dsc, 2);
6115 rd_val = displaced_read_reg (regs, dsc, rd);
6116 rn_val = displaced_read_reg (regs, dsc, rn);
6117 rm_val = displaced_read_reg (regs, dsc, rm);
6118 displaced_write_reg (regs, dsc, 0, rd_val, CANNOT_WRITE_PC);
6119 displaced_write_reg (regs, dsc, 1, rn_val, CANNOT_WRITE_PC);
6120 displaced_write_reg (regs, dsc, 2, rm_val, CANNOT_WRITE_PC);
6121 dsc->rd = rd;
6123 dsc->cleanup = &cleanup_alu_reg;
6126 static int
6127 arm_copy_alu_reg (struct gdbarch *gdbarch, uint32_t insn, struct regcache *regs,
6128 arm_displaced_step_copy_insn_closure *dsc)
6130 unsigned int op = bits (insn, 21, 24);
6131 int is_mov = (op == 0xd);
6133 if (!insn_references_pc (insn, 0x000ff00ful))
6134 return arm_copy_unmodified (gdbarch, insn, "ALU reg", dsc);
6136 displaced_debug_printf ("copying reg %s insn %.8lx",
6137 is_mov ? "move" : "ALU", (unsigned long) insn);
6139 if (is_mov)
6140 dsc->modinsn[0] = (insn & 0xfff00ff0) | 0x2;
6141 else
6142 dsc->modinsn[0] = (insn & 0xfff00ff0) | 0x10002;
6144 install_alu_reg (gdbarch, regs, dsc, bits (insn, 12, 15), bits (insn, 16, 19),
6145 bits (insn, 0, 3));
6146 return 0;
6149 static int
6150 thumb_copy_alu_reg (struct gdbarch *gdbarch, uint16_t insn,
6151 struct regcache *regs,
6152 arm_displaced_step_copy_insn_closure *dsc)
6154 unsigned rm, rd;
6156 rm = bits (insn, 3, 6);
6157 rd = (bit (insn, 7) << 3) | bits (insn, 0, 2);
6159 if (rd != ARM_PC_REGNUM && rm != ARM_PC_REGNUM)
6160 return thumb_copy_unmodified_16bit (gdbarch, insn, "ALU reg", dsc);
6162 displaced_debug_printf ("copying ALU reg insn %.4x", (unsigned short) insn);
6164 dsc->modinsn[0] = ((insn & 0xff00) | 0x10);
6166 install_alu_reg (gdbarch, regs, dsc, rd, rd, rm);
6168 return 0;
6171 /* Cleanup/copy arithmetic/logic insns with shifted register RHS. */
6173 static void
6174 cleanup_alu_shifted_reg (struct gdbarch *gdbarch,
6175 struct regcache *regs,
6176 arm_displaced_step_copy_insn_closure *dsc)
6178 ULONGEST rd_val = displaced_read_reg (regs, dsc, 0);
6179 int i;
6181 for (i = 0; i < 4; i++)
6182 displaced_write_reg (regs, dsc, i, dsc->tmp[i], CANNOT_WRITE_PC);
6184 displaced_write_reg (regs, dsc, dsc->rd, rd_val, ALU_WRITE_PC);
6187 static void
6188 install_alu_shifted_reg (struct gdbarch *gdbarch, struct regcache *regs,
6189 arm_displaced_step_copy_insn_closure *dsc,
6190 unsigned int rd, unsigned int rn, unsigned int rm,
6191 unsigned rs)
6193 int i;
6194 ULONGEST rd_val, rn_val, rm_val, rs_val;
6196 /* Instruction is of form:
6198 <op><cond> rd, [rn,] rm, <shift> rs
6200 Rewrite as:
6202 Preparation: tmp1, tmp2, tmp3, tmp4 <- r0, r1, r2, r3
6203 r0, r1, r2, r3 <- rd, rn, rm, rs
6204 Insn: <op><cond> r0, r1, r2, <shift> r3
6205 Cleanup: tmp5 <- r0
6206 r0, r1, r2, r3 <- tmp1, tmp2, tmp3, tmp4
6207 rd <- tmp5
6210 for (i = 0; i < 4; i++)
6211 dsc->tmp[i] = displaced_read_reg (regs, dsc, i);
6213 rd_val = displaced_read_reg (regs, dsc, rd);
6214 rn_val = displaced_read_reg (regs, dsc, rn);
6215 rm_val = displaced_read_reg (regs, dsc, rm);
6216 rs_val = displaced_read_reg (regs, dsc, rs);
6217 displaced_write_reg (regs, dsc, 0, rd_val, CANNOT_WRITE_PC);
6218 displaced_write_reg (regs, dsc, 1, rn_val, CANNOT_WRITE_PC);
6219 displaced_write_reg (regs, dsc, 2, rm_val, CANNOT_WRITE_PC);
6220 displaced_write_reg (regs, dsc, 3, rs_val, CANNOT_WRITE_PC);
6221 dsc->rd = rd;
6222 dsc->cleanup = &cleanup_alu_shifted_reg;
6225 static int
6226 arm_copy_alu_shifted_reg (struct gdbarch *gdbarch, uint32_t insn,
6227 struct regcache *regs,
6228 arm_displaced_step_copy_insn_closure *dsc)
6230 unsigned int op = bits (insn, 21, 24);
6231 int is_mov = (op == 0xd);
6232 unsigned int rd, rn, rm, rs;
6234 if (!insn_references_pc (insn, 0x000fff0ful))
6235 return arm_copy_unmodified (gdbarch, insn, "ALU shifted reg", dsc);
6237 displaced_debug_printf ("copying shifted reg %s insn %.8lx",
6238 is_mov ? "move" : "ALU",
6239 (unsigned long) insn);
6241 rn = bits (insn, 16, 19);
6242 rm = bits (insn, 0, 3);
6243 rs = bits (insn, 8, 11);
6244 rd = bits (insn, 12, 15);
6246 if (is_mov)
6247 dsc->modinsn[0] = (insn & 0xfff000f0) | 0x302;
6248 else
6249 dsc->modinsn[0] = (insn & 0xfff000f0) | 0x10302;
6251 install_alu_shifted_reg (gdbarch, regs, dsc, rd, rn, rm, rs);
6253 return 0;
6256 /* Clean up load instructions. */
6258 static void
6259 cleanup_load (struct gdbarch *gdbarch, struct regcache *regs,
6260 arm_displaced_step_copy_insn_closure *dsc)
6262 ULONGEST rt_val, rt_val2 = 0, rn_val;
6264 rt_val = displaced_read_reg (regs, dsc, 0);
6265 if (dsc->u.ldst.xfersize == 8)
6266 rt_val2 = displaced_read_reg (regs, dsc, 1);
6267 rn_val = displaced_read_reg (regs, dsc, 2);
6269 displaced_write_reg (regs, dsc, 0, dsc->tmp[0], CANNOT_WRITE_PC);
6270 if (dsc->u.ldst.xfersize > 4)
6271 displaced_write_reg (regs, dsc, 1, dsc->tmp[1], CANNOT_WRITE_PC);
6272 displaced_write_reg (regs, dsc, 2, dsc->tmp[2], CANNOT_WRITE_PC);
6273 if (!dsc->u.ldst.immed)
6274 displaced_write_reg (regs, dsc, 3, dsc->tmp[3], CANNOT_WRITE_PC);
6276 /* Handle register writeback. */
6277 if (dsc->u.ldst.writeback)
6278 displaced_write_reg (regs, dsc, dsc->u.ldst.rn, rn_val, CANNOT_WRITE_PC);
6279 /* Put result in right place. */
6280 displaced_write_reg (regs, dsc, dsc->rd, rt_val, LOAD_WRITE_PC);
6281 if (dsc->u.ldst.xfersize == 8)
6282 displaced_write_reg (regs, dsc, dsc->rd + 1, rt_val2, LOAD_WRITE_PC);
6285 /* Clean up store instructions. */
6287 static void
6288 cleanup_store (struct gdbarch *gdbarch, struct regcache *regs,
6289 arm_displaced_step_copy_insn_closure *dsc)
6291 ULONGEST rn_val = displaced_read_reg (regs, dsc, 2);
6293 displaced_write_reg (regs, dsc, 0, dsc->tmp[0], CANNOT_WRITE_PC);
6294 if (dsc->u.ldst.xfersize > 4)
6295 displaced_write_reg (regs, dsc, 1, dsc->tmp[1], CANNOT_WRITE_PC);
6296 displaced_write_reg (regs, dsc, 2, dsc->tmp[2], CANNOT_WRITE_PC);
6297 if (!dsc->u.ldst.immed)
6298 displaced_write_reg (regs, dsc, 3, dsc->tmp[3], CANNOT_WRITE_PC);
6299 if (!dsc->u.ldst.restore_r4)
6300 displaced_write_reg (regs, dsc, 4, dsc->tmp[4], CANNOT_WRITE_PC);
6302 /* Writeback. */
6303 if (dsc->u.ldst.writeback)
6304 displaced_write_reg (regs, dsc, dsc->u.ldst.rn, rn_val, CANNOT_WRITE_PC);
6307 /* Copy "extra" load/store instructions. These are halfword/doubleword
6308 transfers, which have a different encoding to byte/word transfers. */
6310 static int
6311 arm_copy_extra_ld_st (struct gdbarch *gdbarch, uint32_t insn, int unprivileged,
6312 regcache *regs, arm_displaced_step_copy_insn_closure *dsc)
6314 unsigned int op1 = bits (insn, 20, 24);
6315 unsigned int op2 = bits (insn, 5, 6);
6316 unsigned int rt = bits (insn, 12, 15);
6317 unsigned int rn = bits (insn, 16, 19);
6318 unsigned int rm = bits (insn, 0, 3);
6319 char load[12] = {0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1};
6320 char bytesize[12] = {2, 2, 2, 2, 8, 1, 8, 1, 8, 2, 8, 2};
6321 int immed = (op1 & 0x4) != 0;
6322 int opcode;
6323 ULONGEST rt_val, rt_val2 = 0, rn_val, rm_val = 0;
6325 if (!insn_references_pc (insn, 0x000ff00ful))
6326 return arm_copy_unmodified (gdbarch, insn, "extra load/store", dsc);
6328 displaced_debug_printf ("copying %sextra load/store insn %.8lx",
6329 unprivileged ? "unprivileged " : "",
6330 (unsigned long) insn);
6332 opcode = ((op2 << 2) | (op1 & 0x1) | ((op1 & 0x4) >> 1)) - 4;
6334 if (opcode < 0)
6335 internal_error (__FILE__, __LINE__,
6336 _("copy_extra_ld_st: instruction decode error"));
6338 dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
6339 dsc->tmp[1] = displaced_read_reg (regs, dsc, 1);
6340 dsc->tmp[2] = displaced_read_reg (regs, dsc, 2);
6341 if (!immed)
6342 dsc->tmp[3] = displaced_read_reg (regs, dsc, 3);
6344 rt_val = displaced_read_reg (regs, dsc, rt);
6345 if (bytesize[opcode] == 8)
6346 rt_val2 = displaced_read_reg (regs, dsc, rt + 1);
6347 rn_val = displaced_read_reg (regs, dsc, rn);
6348 if (!immed)
6349 rm_val = displaced_read_reg (regs, dsc, rm);
6351 displaced_write_reg (regs, dsc, 0, rt_val, CANNOT_WRITE_PC);
6352 if (bytesize[opcode] == 8)
6353 displaced_write_reg (regs, dsc, 1, rt_val2, CANNOT_WRITE_PC);
6354 displaced_write_reg (regs, dsc, 2, rn_val, CANNOT_WRITE_PC);
6355 if (!immed)
6356 displaced_write_reg (regs, dsc, 3, rm_val, CANNOT_WRITE_PC);
6358 dsc->rd = rt;
6359 dsc->u.ldst.xfersize = bytesize[opcode];
6360 dsc->u.ldst.rn = rn;
6361 dsc->u.ldst.immed = immed;
6362 dsc->u.ldst.writeback = bit (insn, 24) == 0 || bit (insn, 21) != 0;
6363 dsc->u.ldst.restore_r4 = 0;
6365 if (immed)
6366 /* {ldr,str}<width><cond> rt, [rt2,] [rn, #imm]
6368 {ldr,str}<width><cond> r0, [r1,] [r2, #imm]. */
6369 dsc->modinsn[0] = (insn & 0xfff00fff) | 0x20000;
6370 else
6371 /* {ldr,str}<width><cond> rt, [rt2,] [rn, +/-rm]
6373 {ldr,str}<width><cond> r0, [r1,] [r2, +/-r3]. */
6374 dsc->modinsn[0] = (insn & 0xfff00ff0) | 0x20003;
6376 dsc->cleanup = load[opcode] ? &cleanup_load : &cleanup_store;
6378 return 0;
6381 /* Copy byte/half word/word loads and stores. */
6383 static void
6384 install_load_store (struct gdbarch *gdbarch, struct regcache *regs,
6385 arm_displaced_step_copy_insn_closure *dsc, int load,
6386 int immed, int writeback, int size, int usermode,
6387 int rt, int rm, int rn)
6389 ULONGEST rt_val, rn_val, rm_val = 0;
6391 dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
6392 dsc->tmp[2] = displaced_read_reg (regs, dsc, 2);
6393 if (!immed)
6394 dsc->tmp[3] = displaced_read_reg (regs, dsc, 3);
6395 if (!load)
6396 dsc->tmp[4] = displaced_read_reg (regs, dsc, 4);
6398 rt_val = displaced_read_reg (regs, dsc, rt);
6399 rn_val = displaced_read_reg (regs, dsc, rn);
6400 if (!immed)
6401 rm_val = displaced_read_reg (regs, dsc, rm);
6403 displaced_write_reg (regs, dsc, 0, rt_val, CANNOT_WRITE_PC);
6404 displaced_write_reg (regs, dsc, 2, rn_val, CANNOT_WRITE_PC);
6405 if (!immed)
6406 displaced_write_reg (regs, dsc, 3, rm_val, CANNOT_WRITE_PC);
6407 dsc->rd = rt;
6408 dsc->u.ldst.xfersize = size;
6409 dsc->u.ldst.rn = rn;
6410 dsc->u.ldst.immed = immed;
6411 dsc->u.ldst.writeback = writeback;
6413 /* To write PC we can do:
6415 Before this sequence of instructions:
6416 r0 is the PC value got from displaced_read_reg, so r0 = from + 8;
6417 r2 is the Rn value got from displaced_read_reg.
6419 Insn1: push {pc} Write address of STR instruction + offset on stack
6420 Insn2: pop {r4} Read it back from stack, r4 = addr(Insn1) + offset
6421 Insn3: sub r4, r4, pc r4 = addr(Insn1) + offset - pc
6422 = addr(Insn1) + offset - addr(Insn3) - 8
6423 = offset - 16
6424 Insn4: add r4, r4, #8 r4 = offset - 8
6425 Insn5: add r0, r0, r4 r0 = from + 8 + offset - 8
6426 = from + offset
6427 Insn6: str r0, [r2, #imm] (or str r0, [r2, r3])
6429 Otherwise we don't know what value to write for PC, since the offset is
6430 architecture-dependent (sometimes PC+8, sometimes PC+12). More details
6431 of this can be found in Section "Saving from r15" in
6432 http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dui0204g/Cihbjifh.html */
6434 dsc->cleanup = load ? &cleanup_load : &cleanup_store;
6438 static int
6439 thumb2_copy_load_literal (struct gdbarch *gdbarch, uint16_t insn1,
6440 uint16_t insn2, struct regcache *regs,
6441 arm_displaced_step_copy_insn_closure *dsc, int size)
6443 unsigned int u_bit = bit (insn1, 7);
6444 unsigned int rt = bits (insn2, 12, 15);
6445 int imm12 = bits (insn2, 0, 11);
6446 ULONGEST pc_val;
6448 displaced_debug_printf ("copying ldr pc (0x%x) R%d %c imm12 %.4x",
6449 (unsigned int) dsc->insn_addr, rt, u_bit ? '+' : '-',
6450 imm12);
6452 if (!u_bit)
6453 imm12 = -1 * imm12;
6455 /* Rewrite instruction LDR Rt imm12 into:
6457 Prepare: tmp[0] <- r0, tmp[1] <- r2, tmp[2] <- r3, r2 <- pc, r3 <- imm12
6459 LDR R0, R2, R3,
6461 Cleanup: rt <- r0, r0 <- tmp[0], r2 <- tmp[1], r3 <- tmp[2]. */
6464 dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
6465 dsc->tmp[2] = displaced_read_reg (regs, dsc, 2);
6466 dsc->tmp[3] = displaced_read_reg (regs, dsc, 3);
6468 pc_val = displaced_read_reg (regs, dsc, ARM_PC_REGNUM);
6470 pc_val = pc_val & 0xfffffffc;
6472 displaced_write_reg (regs, dsc, 2, pc_val, CANNOT_WRITE_PC);
6473 displaced_write_reg (regs, dsc, 3, imm12, CANNOT_WRITE_PC);
6475 dsc->rd = rt;
6477 dsc->u.ldst.xfersize = size;
6478 dsc->u.ldst.immed = 0;
6479 dsc->u.ldst.writeback = 0;
6480 dsc->u.ldst.restore_r4 = 0;
6482 /* LDR R0, R2, R3 */
6483 dsc->modinsn[0] = 0xf852;
6484 dsc->modinsn[1] = 0x3;
6485 dsc->numinsns = 2;
6487 dsc->cleanup = &cleanup_load;
6489 return 0;
6492 static int
6493 thumb2_copy_load_reg_imm (struct gdbarch *gdbarch, uint16_t insn1,
6494 uint16_t insn2, struct regcache *regs,
6495 arm_displaced_step_copy_insn_closure *dsc,
6496 int writeback, int immed)
6498 unsigned int rt = bits (insn2, 12, 15);
6499 unsigned int rn = bits (insn1, 0, 3);
6500 unsigned int rm = bits (insn2, 0, 3); /* Only valid if !immed. */
6501 /* In LDR (register), there is also a register Rm, which is not allowed to
6502 be PC, so we don't have to check it. */
6504 if (rt != ARM_PC_REGNUM && rn != ARM_PC_REGNUM)
6505 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2, "load",
6506 dsc);
6508 displaced_debug_printf ("copying ldr r%d [r%d] insn %.4x%.4x",
6509 rt, rn, insn1, insn2);
6511 install_load_store (gdbarch, regs, dsc, 1, immed, writeback, 4,
6512 0, rt, rm, rn);
6514 dsc->u.ldst.restore_r4 = 0;
6516 if (immed)
6517 /* ldr[b]<cond> rt, [rn, #imm], etc.
6519 ldr[b]<cond> r0, [r2, #imm]. */
6521 dsc->modinsn[0] = (insn1 & 0xfff0) | 0x2;
6522 dsc->modinsn[1] = insn2 & 0x0fff;
6524 else
6525 /* ldr[b]<cond> rt, [rn, rm], etc.
6527 ldr[b]<cond> r0, [r2, r3]. */
6529 dsc->modinsn[0] = (insn1 & 0xfff0) | 0x2;
6530 dsc->modinsn[1] = (insn2 & 0x0ff0) | 0x3;
6533 dsc->numinsns = 2;
6535 return 0;
6539 static int
6540 arm_copy_ldr_str_ldrb_strb (struct gdbarch *gdbarch, uint32_t insn,
6541 struct regcache *regs,
6542 arm_displaced_step_copy_insn_closure *dsc,
6543 int load, int size, int usermode)
6545 int immed = !bit (insn, 25);
6546 int writeback = (bit (insn, 24) == 0 || bit (insn, 21) != 0);
6547 unsigned int rt = bits (insn, 12, 15);
6548 unsigned int rn = bits (insn, 16, 19);
6549 unsigned int rm = bits (insn, 0, 3); /* Only valid if !immed. */
6551 if (!insn_references_pc (insn, 0x000ff00ful))
6552 return arm_copy_unmodified (gdbarch, insn, "load/store", dsc);
6554 displaced_debug_printf ("copying %s%s r%d [r%d] insn %.8lx",
6555 load ? (size == 1 ? "ldrb" : "ldr")
6556 : (size == 1 ? "strb" : "str"),
6557 usermode ? "t" : "",
6558 rt, rn,
6559 (unsigned long) insn);
6561 install_load_store (gdbarch, regs, dsc, load, immed, writeback, size,
6562 usermode, rt, rm, rn);
6564 if (load || rt != ARM_PC_REGNUM)
6566 dsc->u.ldst.restore_r4 = 0;
6568 if (immed)
6569 /* {ldr,str}[b]<cond> rt, [rn, #imm], etc.
6571 {ldr,str}[b]<cond> r0, [r2, #imm]. */
6572 dsc->modinsn[0] = (insn & 0xfff00fff) | 0x20000;
6573 else
6574 /* {ldr,str}[b]<cond> rt, [rn, rm], etc.
6576 {ldr,str}[b]<cond> r0, [r2, r3]. */
6577 dsc->modinsn[0] = (insn & 0xfff00ff0) | 0x20003;
6579 else
6581 /* We need to use r4 as scratch. Make sure it's restored afterwards. */
6582 dsc->u.ldst.restore_r4 = 1;
6583 dsc->modinsn[0] = 0xe92d8000; /* push {pc} */
6584 dsc->modinsn[1] = 0xe8bd0010; /* pop {r4} */
6585 dsc->modinsn[2] = 0xe044400f; /* sub r4, r4, pc. */
6586 dsc->modinsn[3] = 0xe2844008; /* add r4, r4, #8. */
6587 dsc->modinsn[4] = 0xe0800004; /* add r0, r0, r4. */
6589 /* As above. */
6590 if (immed)
6591 dsc->modinsn[5] = (insn & 0xfff00fff) | 0x20000;
6592 else
6593 dsc->modinsn[5] = (insn & 0xfff00ff0) | 0x20003;
6595 dsc->numinsns = 6;
6598 dsc->cleanup = load ? &cleanup_load : &cleanup_store;
6600 return 0;
6603 /* Cleanup LDM instructions with fully-populated register list. This is an
6604 unfortunate corner case: it's impossible to implement correctly by modifying
6605 the instruction. The issue is as follows: we have an instruction,
6607 ldm rN, {r0-r15}
6609 which we must rewrite to avoid loading PC. A possible solution would be to
6610 do the load in two halves, something like (with suitable cleanup
6611 afterwards):
6613 mov r8, rN
6614 ldm[id][ab] r8!, {r0-r7}
6615 str r7, <temp>
6616 ldm[id][ab] r8, {r7-r14}
6617 <bkpt>
6619 but at present there's no suitable place for <temp>, since the scratch space
6620 is overwritten before the cleanup routine is called. For now, we simply
6621 emulate the instruction. */
6623 static void
6624 cleanup_block_load_all (struct gdbarch *gdbarch, struct regcache *regs,
6625 arm_displaced_step_copy_insn_closure *dsc)
6627 int inc = dsc->u.block.increment;
6628 int bump_before = dsc->u.block.before ? (inc ? 4 : -4) : 0;
6629 int bump_after = dsc->u.block.before ? 0 : (inc ? 4 : -4);
6630 uint32_t regmask = dsc->u.block.regmask;
6631 int regno = inc ? 0 : 15;
6632 CORE_ADDR xfer_addr = dsc->u.block.xfer_addr;
6633 int exception_return = dsc->u.block.load && dsc->u.block.user
6634 && (regmask & 0x8000) != 0;
6635 uint32_t status = displaced_read_reg (regs, dsc, ARM_PS_REGNUM);
6636 int do_transfer = condition_true (dsc->u.block.cond, status);
6637 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
6639 if (!do_transfer)
6640 return;
6642 /* If the instruction is ldm rN, {...pc}^, I don't think there's anything
6643 sensible we can do here. Complain loudly. */
6644 if (exception_return)
6645 error (_("Cannot single-step exception return"));
6647 /* We don't handle any stores here for now. */
6648 gdb_assert (dsc->u.block.load != 0);
6650 displaced_debug_printf ("emulating block transfer: %s %s %s",
6651 dsc->u.block.load ? "ldm" : "stm",
6652 dsc->u.block.increment ? "inc" : "dec",
6653 dsc->u.block.before ? "before" : "after");
6655 while (regmask)
6657 uint32_t memword;
6659 if (inc)
6660 while (regno <= ARM_PC_REGNUM && (regmask & (1 << regno)) == 0)
6661 regno++;
6662 else
6663 while (regno >= 0 && (regmask & (1 << regno)) == 0)
6664 regno--;
6666 xfer_addr += bump_before;
6668 memword = read_memory_unsigned_integer (xfer_addr, 4, byte_order);
6669 displaced_write_reg (regs, dsc, regno, memword, LOAD_WRITE_PC);
6671 xfer_addr += bump_after;
6673 regmask &= ~(1 << regno);
6676 if (dsc->u.block.writeback)
6677 displaced_write_reg (regs, dsc, dsc->u.block.rn, xfer_addr,
6678 CANNOT_WRITE_PC);
6681 /* Clean up an STM which included the PC in the register list. */
6683 static void
6684 cleanup_block_store_pc (struct gdbarch *gdbarch, struct regcache *regs,
6685 arm_displaced_step_copy_insn_closure *dsc)
6687 uint32_t status = displaced_read_reg (regs, dsc, ARM_PS_REGNUM);
6688 int store_executed = condition_true (dsc->u.block.cond, status);
6689 CORE_ADDR pc_stored_at, transferred_regs
6690 = count_one_bits (dsc->u.block.regmask);
6691 CORE_ADDR stm_insn_addr;
6692 uint32_t pc_val;
6693 long offset;
6694 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
6696 /* If condition code fails, there's nothing else to do. */
6697 if (!store_executed)
6698 return;
6700 if (dsc->u.block.increment)
6702 pc_stored_at = dsc->u.block.xfer_addr + 4 * transferred_regs;
6704 if (dsc->u.block.before)
6705 pc_stored_at += 4;
6707 else
6709 pc_stored_at = dsc->u.block.xfer_addr;
6711 if (dsc->u.block.before)
6712 pc_stored_at -= 4;
6715 pc_val = read_memory_unsigned_integer (pc_stored_at, 4, byte_order);
6716 stm_insn_addr = dsc->scratch_base;
6717 offset = pc_val - stm_insn_addr;
6719 displaced_debug_printf ("detected PC offset %.8lx for STM instruction",
6720 offset);
6722 /* Rewrite the stored PC to the proper value for the non-displaced original
6723 instruction. */
6724 write_memory_unsigned_integer (pc_stored_at, 4, byte_order,
6725 dsc->insn_addr + offset);
6728 /* Clean up an LDM which includes the PC in the register list. We clumped all
6729 the registers in the transferred list into a contiguous range r0...rX (to
6730 avoid loading PC directly and losing control of the debugged program), so we
6731 must undo that here. */
6733 static void
6734 cleanup_block_load_pc (struct gdbarch *gdbarch,
6735 struct regcache *regs,
6736 arm_displaced_step_copy_insn_closure *dsc)
6738 uint32_t status = displaced_read_reg (regs, dsc, ARM_PS_REGNUM);
6739 int load_executed = condition_true (dsc->u.block.cond, status);
6740 unsigned int mask = dsc->u.block.regmask, write_reg = ARM_PC_REGNUM;
6741 unsigned int regs_loaded = count_one_bits (mask);
6742 unsigned int num_to_shuffle = regs_loaded, clobbered;
6744 /* The method employed here will fail if the register list is fully populated
6745 (we need to avoid loading PC directly). */
6746 gdb_assert (num_to_shuffle < 16);
6748 if (!load_executed)
6749 return;
6751 clobbered = (1 << num_to_shuffle) - 1;
6753 while (num_to_shuffle > 0)
6755 if ((mask & (1 << write_reg)) != 0)
6757 unsigned int read_reg = num_to_shuffle - 1;
6759 if (read_reg != write_reg)
6761 ULONGEST rval = displaced_read_reg (regs, dsc, read_reg);
6762 displaced_write_reg (regs, dsc, write_reg, rval, LOAD_WRITE_PC);
6763 displaced_debug_printf ("LDM: move loaded register r%d to r%d",
6764 read_reg, write_reg);
6766 else
6767 displaced_debug_printf ("LDM: register r%d already in the right "
6768 "place", write_reg);
6770 clobbered &= ~(1 << write_reg);
6772 num_to_shuffle--;
6775 write_reg--;
6778 /* Restore any registers we scribbled over. */
6779 for (write_reg = 0; clobbered != 0; write_reg++)
6781 if ((clobbered & (1 << write_reg)) != 0)
6783 displaced_write_reg (regs, dsc, write_reg, dsc->tmp[write_reg],
6784 CANNOT_WRITE_PC);
6785 displaced_debug_printf ("LDM: restored clobbered register r%d",
6786 write_reg);
6787 clobbered &= ~(1 << write_reg);
6791 /* Perform register writeback manually. */
6792 if (dsc->u.block.writeback)
6794 ULONGEST new_rn_val = dsc->u.block.xfer_addr;
6796 if (dsc->u.block.increment)
6797 new_rn_val += regs_loaded * 4;
6798 else
6799 new_rn_val -= regs_loaded * 4;
6801 displaced_write_reg (regs, dsc, dsc->u.block.rn, new_rn_val,
6802 CANNOT_WRITE_PC);
6806 /* Handle ldm/stm, apart from some tricky cases which are unlikely to occur
6807 in user-level code (in particular exception return, ldm rn, {...pc}^). */
6809 static int
6810 arm_copy_block_xfer (struct gdbarch *gdbarch, uint32_t insn,
6811 struct regcache *regs,
6812 arm_displaced_step_copy_insn_closure *dsc)
6814 int load = bit (insn, 20);
6815 int user = bit (insn, 22);
6816 int increment = bit (insn, 23);
6817 int before = bit (insn, 24);
6818 int writeback = bit (insn, 21);
6819 int rn = bits (insn, 16, 19);
6821 /* Block transfers which don't mention PC can be run directly
6822 out-of-line. */
6823 if (rn != ARM_PC_REGNUM && (insn & 0x8000) == 0)
6824 return arm_copy_unmodified (gdbarch, insn, "ldm/stm", dsc);
6826 if (rn == ARM_PC_REGNUM)
6828 warning (_("displaced: Unpredictable LDM or STM with "
6829 "base register r15"));
6830 return arm_copy_unmodified (gdbarch, insn, "unpredictable ldm/stm", dsc);
6833 displaced_debug_printf ("copying block transfer insn %.8lx",
6834 (unsigned long) insn);
6836 dsc->u.block.xfer_addr = displaced_read_reg (regs, dsc, rn);
6837 dsc->u.block.rn = rn;
6839 dsc->u.block.load = load;
6840 dsc->u.block.user = user;
6841 dsc->u.block.increment = increment;
6842 dsc->u.block.before = before;
6843 dsc->u.block.writeback = writeback;
6844 dsc->u.block.cond = bits (insn, 28, 31);
6846 dsc->u.block.regmask = insn & 0xffff;
6848 if (load)
6850 if ((insn & 0xffff) == 0xffff)
6852 /* LDM with a fully-populated register list. This case is
6853 particularly tricky. Implement for now by fully emulating the
6854 instruction (which might not behave perfectly in all cases, but
6855 these instructions should be rare enough for that not to matter
6856 too much). */
6857 dsc->modinsn[0] = ARM_NOP;
6859 dsc->cleanup = &cleanup_block_load_all;
6861 else
6863 /* LDM of a list of registers which includes PC. Implement by
6864 rewriting the list of registers to be transferred into a
6865 contiguous chunk r0...rX before doing the transfer, then shuffling
6866 registers into the correct places in the cleanup routine. */
6867 unsigned int regmask = insn & 0xffff;
6868 unsigned int num_in_list = count_one_bits (regmask), new_regmask;
6869 unsigned int i;
6871 for (i = 0; i < num_in_list; i++)
6872 dsc->tmp[i] = displaced_read_reg (regs, dsc, i);
6874 /* Writeback makes things complicated. We need to avoid clobbering
6875 the base register with one of the registers in our modified
6876 register list, but just using a different register can't work in
6877 all cases, e.g.:
6879 ldm r14!, {r0-r13,pc}
6881 which would need to be rewritten as:
6883 ldm rN!, {r0-r14}
6885 but that can't work, because there's no free register for N.
6887 Solve this by turning off the writeback bit, and emulating
6888 writeback manually in the cleanup routine. */
6890 if (writeback)
6891 insn &= ~(1 << 21);
6893 new_regmask = (1 << num_in_list) - 1;
6895 displaced_debug_printf ("LDM r%d%s, {..., pc}: original reg list "
6896 "%.4x, modified list %.4x",
6897 rn, writeback ? "!" : "",
6898 (int) insn & 0xffff, new_regmask);
6900 dsc->modinsn[0] = (insn & ~0xffff) | (new_regmask & 0xffff);
6902 dsc->cleanup = &cleanup_block_load_pc;
6905 else
6907 /* STM of a list of registers which includes PC. Run the instruction
6908 as-is, but out of line: this will store the wrong value for the PC,
6909 so we must manually fix up the memory in the cleanup routine.
6910 Doing things this way has the advantage that we can auto-detect
6911 the offset of the PC write (which is architecture-dependent) in
6912 the cleanup routine. */
6913 dsc->modinsn[0] = insn;
6915 dsc->cleanup = &cleanup_block_store_pc;
6918 return 0;
6921 static int
6922 thumb2_copy_block_xfer (struct gdbarch *gdbarch, uint16_t insn1, uint16_t insn2,
6923 struct regcache *regs,
6924 arm_displaced_step_copy_insn_closure *dsc)
6926 int rn = bits (insn1, 0, 3);
6927 int load = bit (insn1, 4);
6928 int writeback = bit (insn1, 5);
6930 /* Block transfers which don't mention PC can be run directly
6931 out-of-line. */
6932 if (rn != ARM_PC_REGNUM && (insn2 & 0x8000) == 0)
6933 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2, "ldm/stm", dsc);
6935 if (rn == ARM_PC_REGNUM)
6937 warning (_("displaced: Unpredictable LDM or STM with "
6938 "base register r15"));
6939 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
6940 "unpredictable ldm/stm", dsc);
6943 displaced_debug_printf ("copying block transfer insn %.4x%.4x",
6944 insn1, insn2);
6946 /* Clear bit 13, since it should be always zero. */
6947 dsc->u.block.regmask = (insn2 & 0xdfff);
6948 dsc->u.block.rn = rn;
6950 dsc->u.block.load = load;
6951 dsc->u.block.user = 0;
6952 dsc->u.block.increment = bit (insn1, 7);
6953 dsc->u.block.before = bit (insn1, 8);
6954 dsc->u.block.writeback = writeback;
6955 dsc->u.block.cond = INST_AL;
6956 dsc->u.block.xfer_addr = displaced_read_reg (regs, dsc, rn);
6958 if (load)
6960 if (dsc->u.block.regmask == 0xffff)
6962 /* This branch is impossible to happen. */
6963 gdb_assert (0);
6965 else
6967 unsigned int regmask = dsc->u.block.regmask;
6968 unsigned int num_in_list = count_one_bits (regmask), new_regmask;
6969 unsigned int i;
6971 for (i = 0; i < num_in_list; i++)
6972 dsc->tmp[i] = displaced_read_reg (regs, dsc, i);
6974 if (writeback)
6975 insn1 &= ~(1 << 5);
6977 new_regmask = (1 << num_in_list) - 1;
6979 displaced_debug_printf ("LDM r%d%s, {..., pc}: original reg list "
6980 "%.4x, modified list %.4x",
6981 rn, writeback ? "!" : "",
6982 (int) dsc->u.block.regmask, new_regmask);
6984 dsc->modinsn[0] = insn1;
6985 dsc->modinsn[1] = (new_regmask & 0xffff);
6986 dsc->numinsns = 2;
6988 dsc->cleanup = &cleanup_block_load_pc;
6991 else
6993 dsc->modinsn[0] = insn1;
6994 dsc->modinsn[1] = insn2;
6995 dsc->numinsns = 2;
6996 dsc->cleanup = &cleanup_block_store_pc;
6998 return 0;
7001 /* Wrapper over read_memory_unsigned_integer for use in arm_get_next_pcs.
7002 This is used to avoid a dependency on BFD's bfd_endian enum. */
7004 ULONGEST
7005 arm_get_next_pcs_read_memory_unsigned_integer (CORE_ADDR memaddr, int len,
7006 int byte_order)
7008 return read_memory_unsigned_integer (memaddr, len,
7009 (enum bfd_endian) byte_order);
7012 /* Wrapper over gdbarch_addr_bits_remove for use in arm_get_next_pcs. */
7014 CORE_ADDR
7015 arm_get_next_pcs_addr_bits_remove (struct arm_get_next_pcs *self,
7016 CORE_ADDR val)
7018 return gdbarch_addr_bits_remove (self->regcache->arch (), val);
7021 /* Wrapper over syscall_next_pc for use in get_next_pcs. */
7023 static CORE_ADDR
7024 arm_get_next_pcs_syscall_next_pc (struct arm_get_next_pcs *self)
7026 return 0;
7029 /* Wrapper over arm_is_thumb for use in arm_get_next_pcs. */
7032 arm_get_next_pcs_is_thumb (struct arm_get_next_pcs *self)
7034 return arm_is_thumb (self->regcache);
7037 /* single_step() is called just before we want to resume the inferior,
7038 if we want to single-step it but there is no hardware or kernel
7039 single-step support. We find the target of the coming instructions
7040 and breakpoint them. */
7042 std::vector<CORE_ADDR>
7043 arm_software_single_step (struct regcache *regcache)
7045 struct gdbarch *gdbarch = regcache->arch ();
7046 struct arm_get_next_pcs next_pcs_ctx;
7048 arm_get_next_pcs_ctor (&next_pcs_ctx,
7049 &arm_get_next_pcs_ops,
7050 gdbarch_byte_order (gdbarch),
7051 gdbarch_byte_order_for_code (gdbarch),
7053 regcache);
7055 std::vector<CORE_ADDR> next_pcs = arm_get_next_pcs (&next_pcs_ctx);
7057 for (CORE_ADDR &pc_ref : next_pcs)
7058 pc_ref = gdbarch_addr_bits_remove (gdbarch, pc_ref);
7060 return next_pcs;
7063 /* Cleanup/copy SVC (SWI) instructions. These two functions are overridden
7064 for Linux, where some SVC instructions must be treated specially. */
7066 static void
7067 cleanup_svc (struct gdbarch *gdbarch, struct regcache *regs,
7068 arm_displaced_step_copy_insn_closure *dsc)
7070 CORE_ADDR resume_addr = dsc->insn_addr + dsc->insn_size;
7072 displaced_debug_printf ("cleanup for svc, resume at %.8lx",
7073 (unsigned long) resume_addr);
7075 displaced_write_reg (regs, dsc, ARM_PC_REGNUM, resume_addr, BRANCH_WRITE_PC);
7079 /* Common copy routine for svc instruction. */
7081 static int
7082 install_svc (struct gdbarch *gdbarch, struct regcache *regs,
7083 arm_displaced_step_copy_insn_closure *dsc)
7085 /* Preparation: none.
7086 Insn: unmodified svc.
7087 Cleanup: pc <- insn_addr + insn_size. */
7089 /* Pretend we wrote to the PC, so cleanup doesn't set PC to the next
7090 instruction. */
7091 dsc->wrote_to_pc = 1;
7093 /* Allow OS-specific code to override SVC handling. */
7094 if (dsc->u.svc.copy_svc_os)
7095 return dsc->u.svc.copy_svc_os (gdbarch, regs, dsc);
7096 else
7098 dsc->cleanup = &cleanup_svc;
7099 return 0;
7103 static int
7104 arm_copy_svc (struct gdbarch *gdbarch, uint32_t insn,
7105 regcache *regs, arm_displaced_step_copy_insn_closure *dsc)
7108 displaced_debug_printf ("copying svc insn %.8lx",
7109 (unsigned long) insn);
7111 dsc->modinsn[0] = insn;
7113 return install_svc (gdbarch, regs, dsc);
7116 static int
7117 thumb_copy_svc (struct gdbarch *gdbarch, uint16_t insn,
7118 regcache *regs, arm_displaced_step_copy_insn_closure *dsc)
7121 displaced_debug_printf ("copying svc insn %.4x", insn);
7123 dsc->modinsn[0] = insn;
7125 return install_svc (gdbarch, regs, dsc);
7128 /* Copy undefined instructions. */
7130 static int
7131 arm_copy_undef (struct gdbarch *gdbarch, uint32_t insn,
7132 arm_displaced_step_copy_insn_closure *dsc)
7134 displaced_debug_printf ("copying undefined insn %.8lx",
7135 (unsigned long) insn);
7137 dsc->modinsn[0] = insn;
7139 return 0;
7142 static int
7143 thumb_32bit_copy_undef (struct gdbarch *gdbarch, uint16_t insn1, uint16_t insn2,
7144 arm_displaced_step_copy_insn_closure *dsc)
7147 displaced_debug_printf ("copying undefined insn %.4x %.4x",
7148 (unsigned short) insn1, (unsigned short) insn2);
7150 dsc->modinsn[0] = insn1;
7151 dsc->modinsn[1] = insn2;
7152 dsc->numinsns = 2;
7154 return 0;
7157 /* Copy unpredictable instructions. */
7159 static int
7160 arm_copy_unpred (struct gdbarch *gdbarch, uint32_t insn,
7161 arm_displaced_step_copy_insn_closure *dsc)
7163 displaced_debug_printf ("copying unpredictable insn %.8lx",
7164 (unsigned long) insn);
7166 dsc->modinsn[0] = insn;
7168 return 0;
7171 /* The decode_* functions are instruction decoding helpers. They mostly follow
7172 the presentation in the ARM ARM. */
7174 static int
7175 arm_decode_misc_memhint_neon (struct gdbarch *gdbarch, uint32_t insn,
7176 struct regcache *regs,
7177 arm_displaced_step_copy_insn_closure *dsc)
7179 unsigned int op1 = bits (insn, 20, 26), op2 = bits (insn, 4, 7);
7180 unsigned int rn = bits (insn, 16, 19);
7182 if (op1 == 0x10 && (op2 & 0x2) == 0x0 && (rn & 0x1) == 0x0)
7183 return arm_copy_unmodified (gdbarch, insn, "cps", dsc);
7184 else if (op1 == 0x10 && op2 == 0x0 && (rn & 0x1) == 0x1)
7185 return arm_copy_unmodified (gdbarch, insn, "setend", dsc);
7186 else if ((op1 & 0x60) == 0x20)
7187 return arm_copy_unmodified (gdbarch, insn, "neon dataproc", dsc);
7188 else if ((op1 & 0x71) == 0x40)
7189 return arm_copy_unmodified (gdbarch, insn, "neon elt/struct load/store",
7190 dsc);
7191 else if ((op1 & 0x77) == 0x41)
7192 return arm_copy_unmodified (gdbarch, insn, "unallocated mem hint", dsc);
7193 else if ((op1 & 0x77) == 0x45)
7194 return arm_copy_preload (gdbarch, insn, regs, dsc); /* pli. */
7195 else if ((op1 & 0x77) == 0x51)
7197 if (rn != 0xf)
7198 return arm_copy_preload (gdbarch, insn, regs, dsc); /* pld/pldw. */
7199 else
7200 return arm_copy_unpred (gdbarch, insn, dsc);
7202 else if ((op1 & 0x77) == 0x55)
7203 return arm_copy_preload (gdbarch, insn, regs, dsc); /* pld/pldw. */
7204 else if (op1 == 0x57)
7205 switch (op2)
7207 case 0x1: return arm_copy_unmodified (gdbarch, insn, "clrex", dsc);
7208 case 0x4: return arm_copy_unmodified (gdbarch, insn, "dsb", dsc);
7209 case 0x5: return arm_copy_unmodified (gdbarch, insn, "dmb", dsc);
7210 case 0x6: return arm_copy_unmodified (gdbarch, insn, "isb", dsc);
7211 default: return arm_copy_unpred (gdbarch, insn, dsc);
7213 else if ((op1 & 0x63) == 0x43)
7214 return arm_copy_unpred (gdbarch, insn, dsc);
7215 else if ((op2 & 0x1) == 0x0)
7216 switch (op1 & ~0x80)
7218 case 0x61:
7219 return arm_copy_unmodified (gdbarch, insn, "unallocated mem hint", dsc);
7220 case 0x65:
7221 return arm_copy_preload_reg (gdbarch, insn, regs, dsc); /* pli reg. */
7222 case 0x71: case 0x75:
7223 /* pld/pldw reg. */
7224 return arm_copy_preload_reg (gdbarch, insn, regs, dsc);
7225 case 0x63: case 0x67: case 0x73: case 0x77:
7226 return arm_copy_unpred (gdbarch, insn, dsc);
7227 default:
7228 return arm_copy_undef (gdbarch, insn, dsc);
7230 else
7231 return arm_copy_undef (gdbarch, insn, dsc); /* Probably unreachable. */
7234 static int
7235 arm_decode_unconditional (struct gdbarch *gdbarch, uint32_t insn,
7236 struct regcache *regs,
7237 arm_displaced_step_copy_insn_closure *dsc)
7239 if (bit (insn, 27) == 0)
7240 return arm_decode_misc_memhint_neon (gdbarch, insn, regs, dsc);
7241 /* Switch on bits: 0bxxxxx321xxx0xxxxxxxxxxxxxxxxxxxx. */
7242 else switch (((insn & 0x7000000) >> 23) | ((insn & 0x100000) >> 20))
7244 case 0x0: case 0x2:
7245 return arm_copy_unmodified (gdbarch, insn, "srs", dsc);
7247 case 0x1: case 0x3:
7248 return arm_copy_unmodified (gdbarch, insn, "rfe", dsc);
7250 case 0x4: case 0x5: case 0x6: case 0x7:
7251 return arm_copy_b_bl_blx (gdbarch, insn, regs, dsc);
7253 case 0x8:
7254 switch ((insn & 0xe00000) >> 21)
7256 case 0x1: case 0x3: case 0x4: case 0x5: case 0x6: case 0x7:
7257 /* stc/stc2. */
7258 return arm_copy_copro_load_store (gdbarch, insn, regs, dsc);
7260 case 0x2:
7261 return arm_copy_unmodified (gdbarch, insn, "mcrr/mcrr2", dsc);
7263 default:
7264 return arm_copy_undef (gdbarch, insn, dsc);
7267 case 0x9:
7269 int rn_f = (bits (insn, 16, 19) == 0xf);
7270 switch ((insn & 0xe00000) >> 21)
7272 case 0x1: case 0x3:
7273 /* ldc/ldc2 imm (undefined for rn == pc). */
7274 return rn_f ? arm_copy_undef (gdbarch, insn, dsc)
7275 : arm_copy_copro_load_store (gdbarch, insn, regs, dsc);
7277 case 0x2:
7278 return arm_copy_unmodified (gdbarch, insn, "mrrc/mrrc2", dsc);
7280 case 0x4: case 0x5: case 0x6: case 0x7:
7281 /* ldc/ldc2 lit (undefined for rn != pc). */
7282 return rn_f ? arm_copy_copro_load_store (gdbarch, insn, regs, dsc)
7283 : arm_copy_undef (gdbarch, insn, dsc);
7285 default:
7286 return arm_copy_undef (gdbarch, insn, dsc);
7290 case 0xa:
7291 return arm_copy_unmodified (gdbarch, insn, "stc/stc2", dsc);
7293 case 0xb:
7294 if (bits (insn, 16, 19) == 0xf)
7295 /* ldc/ldc2 lit. */
7296 return arm_copy_copro_load_store (gdbarch, insn, regs, dsc);
7297 else
7298 return arm_copy_undef (gdbarch, insn, dsc);
7300 case 0xc:
7301 if (bit (insn, 4))
7302 return arm_copy_unmodified (gdbarch, insn, "mcr/mcr2", dsc);
7303 else
7304 return arm_copy_unmodified (gdbarch, insn, "cdp/cdp2", dsc);
7306 case 0xd:
7307 if (bit (insn, 4))
7308 return arm_copy_unmodified (gdbarch, insn, "mrc/mrc2", dsc);
7309 else
7310 return arm_copy_unmodified (gdbarch, insn, "cdp/cdp2", dsc);
7312 default:
7313 return arm_copy_undef (gdbarch, insn, dsc);
7317 /* Decode miscellaneous instructions in dp/misc encoding space. */
7319 static int
7320 arm_decode_miscellaneous (struct gdbarch *gdbarch, uint32_t insn,
7321 struct regcache *regs,
7322 arm_displaced_step_copy_insn_closure *dsc)
7324 unsigned int op2 = bits (insn, 4, 6);
7325 unsigned int op = bits (insn, 21, 22);
7327 switch (op2)
7329 case 0x0:
7330 return arm_copy_unmodified (gdbarch, insn, "mrs/msr", dsc);
7332 case 0x1:
7333 if (op == 0x1) /* bx. */
7334 return arm_copy_bx_blx_reg (gdbarch, insn, regs, dsc);
7335 else if (op == 0x3)
7336 return arm_copy_unmodified (gdbarch, insn, "clz", dsc);
7337 else
7338 return arm_copy_undef (gdbarch, insn, dsc);
7340 case 0x2:
7341 if (op == 0x1)
7342 /* Not really supported. */
7343 return arm_copy_unmodified (gdbarch, insn, "bxj", dsc);
7344 else
7345 return arm_copy_undef (gdbarch, insn, dsc);
7347 case 0x3:
7348 if (op == 0x1)
7349 return arm_copy_bx_blx_reg (gdbarch, insn,
7350 regs, dsc); /* blx register. */
7351 else
7352 return arm_copy_undef (gdbarch, insn, dsc);
7354 case 0x5:
7355 return arm_copy_unmodified (gdbarch, insn, "saturating add/sub", dsc);
7357 case 0x7:
7358 if (op == 0x1)
7359 return arm_copy_unmodified (gdbarch, insn, "bkpt", dsc);
7360 else if (op == 0x3)
7361 /* Not really supported. */
7362 return arm_copy_unmodified (gdbarch, insn, "smc", dsc);
7363 /* Fall through. */
7365 default:
7366 return arm_copy_undef (gdbarch, insn, dsc);
7370 static int
7371 arm_decode_dp_misc (struct gdbarch *gdbarch, uint32_t insn,
7372 struct regcache *regs,
7373 arm_displaced_step_copy_insn_closure *dsc)
7375 if (bit (insn, 25))
7376 switch (bits (insn, 20, 24))
7378 case 0x10:
7379 return arm_copy_unmodified (gdbarch, insn, "movw", dsc);
7381 case 0x14:
7382 return arm_copy_unmodified (gdbarch, insn, "movt", dsc);
7384 case 0x12: case 0x16:
7385 return arm_copy_unmodified (gdbarch, insn, "msr imm", dsc);
7387 default:
7388 return arm_copy_alu_imm (gdbarch, insn, regs, dsc);
7390 else
7392 uint32_t op1 = bits (insn, 20, 24), op2 = bits (insn, 4, 7);
7394 if ((op1 & 0x19) != 0x10 && (op2 & 0x1) == 0x0)
7395 return arm_copy_alu_reg (gdbarch, insn, regs, dsc);
7396 else if ((op1 & 0x19) != 0x10 && (op2 & 0x9) == 0x1)
7397 return arm_copy_alu_shifted_reg (gdbarch, insn, regs, dsc);
7398 else if ((op1 & 0x19) == 0x10 && (op2 & 0x8) == 0x0)
7399 return arm_decode_miscellaneous (gdbarch, insn, regs, dsc);
7400 else if ((op1 & 0x19) == 0x10 && (op2 & 0x9) == 0x8)
7401 return arm_copy_unmodified (gdbarch, insn, "halfword mul/mla", dsc);
7402 else if ((op1 & 0x10) == 0x00 && op2 == 0x9)
7403 return arm_copy_unmodified (gdbarch, insn, "mul/mla", dsc);
7404 else if ((op1 & 0x10) == 0x10 && op2 == 0x9)
7405 return arm_copy_unmodified (gdbarch, insn, "synch", dsc);
7406 else if (op2 == 0xb || (op2 & 0xd) == 0xd)
7407 /* 2nd arg means "unprivileged". */
7408 return arm_copy_extra_ld_st (gdbarch, insn, (op1 & 0x12) == 0x02, regs,
7409 dsc);
7412 /* Should be unreachable. */
7413 return 1;
7416 static int
7417 arm_decode_ld_st_word_ubyte (struct gdbarch *gdbarch, uint32_t insn,
7418 struct regcache *regs,
7419 arm_displaced_step_copy_insn_closure *dsc)
7421 int a = bit (insn, 25), b = bit (insn, 4);
7422 uint32_t op1 = bits (insn, 20, 24);
7424 if ((!a && (op1 & 0x05) == 0x00 && (op1 & 0x17) != 0x02)
7425 || (a && (op1 & 0x05) == 0x00 && (op1 & 0x17) != 0x02 && !b))
7426 return arm_copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 0, 4, 0);
7427 else if ((!a && (op1 & 0x17) == 0x02)
7428 || (a && (op1 & 0x17) == 0x02 && !b))
7429 return arm_copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 0, 4, 1);
7430 else if ((!a && (op1 & 0x05) == 0x01 && (op1 & 0x17) != 0x03)
7431 || (a && (op1 & 0x05) == 0x01 && (op1 & 0x17) != 0x03 && !b))
7432 return arm_copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 1, 4, 0);
7433 else if ((!a && (op1 & 0x17) == 0x03)
7434 || (a && (op1 & 0x17) == 0x03 && !b))
7435 return arm_copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 1, 4, 1);
7436 else if ((!a && (op1 & 0x05) == 0x04 && (op1 & 0x17) != 0x06)
7437 || (a && (op1 & 0x05) == 0x04 && (op1 & 0x17) != 0x06 && !b))
7438 return arm_copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 0, 1, 0);
7439 else if ((!a && (op1 & 0x17) == 0x06)
7440 || (a && (op1 & 0x17) == 0x06 && !b))
7441 return arm_copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 0, 1, 1);
7442 else if ((!a && (op1 & 0x05) == 0x05 && (op1 & 0x17) != 0x07)
7443 || (a && (op1 & 0x05) == 0x05 && (op1 & 0x17) != 0x07 && !b))
7444 return arm_copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 1, 1, 0);
7445 else if ((!a && (op1 & 0x17) == 0x07)
7446 || (a && (op1 & 0x17) == 0x07 && !b))
7447 return arm_copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 1, 1, 1);
7449 /* Should be unreachable. */
7450 return 1;
7453 static int
7454 arm_decode_media (struct gdbarch *gdbarch, uint32_t insn,
7455 arm_displaced_step_copy_insn_closure *dsc)
7457 switch (bits (insn, 20, 24))
7459 case 0x00: case 0x01: case 0x02: case 0x03:
7460 return arm_copy_unmodified (gdbarch, insn, "parallel add/sub signed", dsc);
7462 case 0x04: case 0x05: case 0x06: case 0x07:
7463 return arm_copy_unmodified (gdbarch, insn, "parallel add/sub unsigned", dsc);
7465 case 0x08: case 0x09: case 0x0a: case 0x0b:
7466 case 0x0c: case 0x0d: case 0x0e: case 0x0f:
7467 return arm_copy_unmodified (gdbarch, insn,
7468 "decode/pack/unpack/saturate/reverse", dsc);
7470 case 0x18:
7471 if (bits (insn, 5, 7) == 0) /* op2. */
7473 if (bits (insn, 12, 15) == 0xf)
7474 return arm_copy_unmodified (gdbarch, insn, "usad8", dsc);
7475 else
7476 return arm_copy_unmodified (gdbarch, insn, "usada8", dsc);
7478 else
7479 return arm_copy_undef (gdbarch, insn, dsc);
7481 case 0x1a: case 0x1b:
7482 if (bits (insn, 5, 6) == 0x2) /* op2[1:0]. */
7483 return arm_copy_unmodified (gdbarch, insn, "sbfx", dsc);
7484 else
7485 return arm_copy_undef (gdbarch, insn, dsc);
7487 case 0x1c: case 0x1d:
7488 if (bits (insn, 5, 6) == 0x0) /* op2[1:0]. */
7490 if (bits (insn, 0, 3) == 0xf)
7491 return arm_copy_unmodified (gdbarch, insn, "bfc", dsc);
7492 else
7493 return arm_copy_unmodified (gdbarch, insn, "bfi", dsc);
7495 else
7496 return arm_copy_undef (gdbarch, insn, dsc);
7498 case 0x1e: case 0x1f:
7499 if (bits (insn, 5, 6) == 0x2) /* op2[1:0]. */
7500 return arm_copy_unmodified (gdbarch, insn, "ubfx", dsc);
7501 else
7502 return arm_copy_undef (gdbarch, insn, dsc);
7505 /* Should be unreachable. */
7506 return 1;
7509 static int
7510 arm_decode_b_bl_ldmstm (struct gdbarch *gdbarch, uint32_t insn,
7511 struct regcache *regs,
7512 arm_displaced_step_copy_insn_closure *dsc)
7514 if (bit (insn, 25))
7515 return arm_copy_b_bl_blx (gdbarch, insn, regs, dsc);
7516 else
7517 return arm_copy_block_xfer (gdbarch, insn, regs, dsc);
7520 static int
7521 arm_decode_ext_reg_ld_st (struct gdbarch *gdbarch, uint32_t insn,
7522 struct regcache *regs,
7523 arm_displaced_step_copy_insn_closure *dsc)
7525 unsigned int opcode = bits (insn, 20, 24);
7527 switch (opcode)
7529 case 0x04: case 0x05: /* VFP/Neon mrrc/mcrr. */
7530 return arm_copy_unmodified (gdbarch, insn, "vfp/neon mrrc/mcrr", dsc);
7532 case 0x08: case 0x0a: case 0x0c: case 0x0e:
7533 case 0x12: case 0x16:
7534 return arm_copy_unmodified (gdbarch, insn, "vfp/neon vstm/vpush", dsc);
7536 case 0x09: case 0x0b: case 0x0d: case 0x0f:
7537 case 0x13: case 0x17:
7538 return arm_copy_unmodified (gdbarch, insn, "vfp/neon vldm/vpop", dsc);
7540 case 0x10: case 0x14: case 0x18: case 0x1c: /* vstr. */
7541 case 0x11: case 0x15: case 0x19: case 0x1d: /* vldr. */
7542 /* Note: no writeback for these instructions. Bit 25 will always be
7543 zero though (via caller), so the following works OK. */
7544 return arm_copy_copro_load_store (gdbarch, insn, regs, dsc);
7547 /* Should be unreachable. */
7548 return 1;
7551 /* Decode shifted register instructions. */
7553 static int
7554 thumb2_decode_dp_shift_reg (struct gdbarch *gdbarch, uint16_t insn1,
7555 uint16_t insn2, struct regcache *regs,
7556 arm_displaced_step_copy_insn_closure *dsc)
7558 /* PC is only allowed to be used in instruction MOV. */
7560 unsigned int op = bits (insn1, 5, 8);
7561 unsigned int rn = bits (insn1, 0, 3);
7563 if (op == 0x2 && rn == 0xf) /* MOV */
7564 return thumb2_copy_alu_imm (gdbarch, insn1, insn2, regs, dsc);
7565 else
7566 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
7567 "dp (shift reg)", dsc);
7571 /* Decode extension register load/store. Exactly the same as
7572 arm_decode_ext_reg_ld_st. */
7574 static int
7575 thumb2_decode_ext_reg_ld_st (struct gdbarch *gdbarch, uint16_t insn1,
7576 uint16_t insn2, struct regcache *regs,
7577 arm_displaced_step_copy_insn_closure *dsc)
7579 unsigned int opcode = bits (insn1, 4, 8);
7581 switch (opcode)
7583 case 0x04: case 0x05:
7584 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
7585 "vfp/neon vmov", dsc);
7587 case 0x08: case 0x0c: /* 01x00 */
7588 case 0x0a: case 0x0e: /* 01x10 */
7589 case 0x12: case 0x16: /* 10x10 */
7590 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
7591 "vfp/neon vstm/vpush", dsc);
7593 case 0x09: case 0x0d: /* 01x01 */
7594 case 0x0b: case 0x0f: /* 01x11 */
7595 case 0x13: case 0x17: /* 10x11 */
7596 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
7597 "vfp/neon vldm/vpop", dsc);
7599 case 0x10: case 0x14: case 0x18: case 0x1c: /* vstr. */
7600 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
7601 "vstr", dsc);
7602 case 0x11: case 0x15: case 0x19: case 0x1d: /* vldr. */
7603 return thumb2_copy_copro_load_store (gdbarch, insn1, insn2, regs, dsc);
7606 /* Should be unreachable. */
7607 return 1;
7610 static int
7611 arm_decode_svc_copro (struct gdbarch *gdbarch, uint32_t insn,
7612 regcache *regs, arm_displaced_step_copy_insn_closure *dsc)
7614 unsigned int op1 = bits (insn, 20, 25);
7615 int op = bit (insn, 4);
7616 unsigned int coproc = bits (insn, 8, 11);
7618 if ((op1 & 0x20) == 0x00 && (op1 & 0x3a) != 0x00 && (coproc & 0xe) == 0xa)
7619 return arm_decode_ext_reg_ld_st (gdbarch, insn, regs, dsc);
7620 else if ((op1 & 0x21) == 0x00 && (op1 & 0x3a) != 0x00
7621 && (coproc & 0xe) != 0xa)
7622 /* stc/stc2. */
7623 return arm_copy_copro_load_store (gdbarch, insn, regs, dsc);
7624 else if ((op1 & 0x21) == 0x01 && (op1 & 0x3a) != 0x00
7625 && (coproc & 0xe) != 0xa)
7626 /* ldc/ldc2 imm/lit. */
7627 return arm_copy_copro_load_store (gdbarch, insn, regs, dsc);
7628 else if ((op1 & 0x3e) == 0x00)
7629 return arm_copy_undef (gdbarch, insn, dsc);
7630 else if ((op1 & 0x3e) == 0x04 && (coproc & 0xe) == 0xa)
7631 return arm_copy_unmodified (gdbarch, insn, "neon 64bit xfer", dsc);
7632 else if (op1 == 0x04 && (coproc & 0xe) != 0xa)
7633 return arm_copy_unmodified (gdbarch, insn, "mcrr/mcrr2", dsc);
7634 else if (op1 == 0x05 && (coproc & 0xe) != 0xa)
7635 return arm_copy_unmodified (gdbarch, insn, "mrrc/mrrc2", dsc);
7636 else if ((op1 & 0x30) == 0x20 && !op)
7638 if ((coproc & 0xe) == 0xa)
7639 return arm_copy_unmodified (gdbarch, insn, "vfp dataproc", dsc);
7640 else
7641 return arm_copy_unmodified (gdbarch, insn, "cdp/cdp2", dsc);
7643 else if ((op1 & 0x30) == 0x20 && op)
7644 return arm_copy_unmodified (gdbarch, insn, "neon 8/16/32 bit xfer", dsc);
7645 else if ((op1 & 0x31) == 0x20 && op && (coproc & 0xe) != 0xa)
7646 return arm_copy_unmodified (gdbarch, insn, "mcr/mcr2", dsc);
7647 else if ((op1 & 0x31) == 0x21 && op && (coproc & 0xe) != 0xa)
7648 return arm_copy_unmodified (gdbarch, insn, "mrc/mrc2", dsc);
7649 else if ((op1 & 0x30) == 0x30)
7650 return arm_copy_svc (gdbarch, insn, regs, dsc);
7651 else
7652 return arm_copy_undef (gdbarch, insn, dsc); /* Possibly unreachable. */
7655 static int
7656 thumb2_decode_svc_copro (struct gdbarch *gdbarch, uint16_t insn1,
7657 uint16_t insn2, struct regcache *regs,
7658 arm_displaced_step_copy_insn_closure *dsc)
7660 unsigned int coproc = bits (insn2, 8, 11);
7661 unsigned int bit_5_8 = bits (insn1, 5, 8);
7662 unsigned int bit_9 = bit (insn1, 9);
7663 unsigned int bit_4 = bit (insn1, 4);
7665 if (bit_9 == 0)
7667 if (bit_5_8 == 2)
7668 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
7669 "neon 64bit xfer/mrrc/mrrc2/mcrr/mcrr2",
7670 dsc);
7671 else if (bit_5_8 == 0) /* UNDEFINED. */
7672 return thumb_32bit_copy_undef (gdbarch, insn1, insn2, dsc);
7673 else
7675 /*coproc is 101x. SIMD/VFP, ext registers load/store. */
7676 if ((coproc & 0xe) == 0xa)
7677 return thumb2_decode_ext_reg_ld_st (gdbarch, insn1, insn2, regs,
7678 dsc);
7679 else /* coproc is not 101x. */
7681 if (bit_4 == 0) /* STC/STC2. */
7682 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
7683 "stc/stc2", dsc);
7684 else /* LDC/LDC2 {literal, immediate}. */
7685 return thumb2_copy_copro_load_store (gdbarch, insn1, insn2,
7686 regs, dsc);
7690 else
7691 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2, "coproc", dsc);
7693 return 0;
7696 static void
7697 install_pc_relative (struct gdbarch *gdbarch, struct regcache *regs,
7698 arm_displaced_step_copy_insn_closure *dsc, int rd)
7700 /* ADR Rd, #imm
7702 Rewrite as:
7704 Preparation: Rd <- PC
7705 Insn: ADD Rd, #imm
7706 Cleanup: Null.
7709 /* Rd <- PC */
7710 int val = displaced_read_reg (regs, dsc, ARM_PC_REGNUM);
7711 displaced_write_reg (regs, dsc, rd, val, CANNOT_WRITE_PC);
7714 static int
7715 thumb_copy_pc_relative_16bit (struct gdbarch *gdbarch, struct regcache *regs,
7716 arm_displaced_step_copy_insn_closure *dsc,
7717 int rd, unsigned int imm)
7720 /* Encoding T2: ADDS Rd, #imm */
7721 dsc->modinsn[0] = (0x3000 | (rd << 8) | imm);
7723 install_pc_relative (gdbarch, regs, dsc, rd);
7725 return 0;
7728 static int
7729 thumb_decode_pc_relative_16bit (struct gdbarch *gdbarch, uint16_t insn,
7730 struct regcache *regs,
7731 arm_displaced_step_copy_insn_closure *dsc)
7733 unsigned int rd = bits (insn, 8, 10);
7734 unsigned int imm8 = bits (insn, 0, 7);
7736 displaced_debug_printf ("copying thumb adr r%d, #%d insn %.4x",
7737 rd, imm8, insn);
7739 return thumb_copy_pc_relative_16bit (gdbarch, regs, dsc, rd, imm8);
7742 static int
7743 thumb_copy_pc_relative_32bit (struct gdbarch *gdbarch, uint16_t insn1,
7744 uint16_t insn2, struct regcache *regs,
7745 arm_displaced_step_copy_insn_closure *dsc)
7747 unsigned int rd = bits (insn2, 8, 11);
7748 /* Since immediate has the same encoding in ADR ADD and SUB, so we simply
7749 extract raw immediate encoding rather than computing immediate. When
7750 generating ADD or SUB instruction, we can simply perform OR operation to
7751 set immediate into ADD. */
7752 unsigned int imm_3_8 = insn2 & 0x70ff;
7753 unsigned int imm_i = insn1 & 0x0400; /* Clear all bits except bit 10. */
7755 displaced_debug_printf ("copying thumb adr r%d, #%d:%d insn %.4x%.4x",
7756 rd, imm_i, imm_3_8, insn1, insn2);
7758 if (bit (insn1, 7)) /* Encoding T2 */
7760 /* Encoding T3: SUB Rd, Rd, #imm */
7761 dsc->modinsn[0] = (0xf1a0 | rd | imm_i);
7762 dsc->modinsn[1] = ((rd << 8) | imm_3_8);
7764 else /* Encoding T3 */
7766 /* Encoding T3: ADD Rd, Rd, #imm */
7767 dsc->modinsn[0] = (0xf100 | rd | imm_i);
7768 dsc->modinsn[1] = ((rd << 8) | imm_3_8);
7770 dsc->numinsns = 2;
7772 install_pc_relative (gdbarch, regs, dsc, rd);
7774 return 0;
7777 static int
7778 thumb_copy_16bit_ldr_literal (struct gdbarch *gdbarch, uint16_t insn1,
7779 struct regcache *regs,
7780 arm_displaced_step_copy_insn_closure *dsc)
7782 unsigned int rt = bits (insn1, 8, 10);
7783 unsigned int pc;
7784 int imm8 = (bits (insn1, 0, 7) << 2);
7786 /* LDR Rd, #imm8
7788 Rwrite as:
7790 Preparation: tmp0 <- R0, tmp2 <- R2, tmp3 <- R3, R2 <- PC, R3 <- #imm8;
7792 Insn: LDR R0, [R2, R3];
7793 Cleanup: R2 <- tmp2, R3 <- tmp3, Rd <- R0, R0 <- tmp0 */
7795 displaced_debug_printf ("copying thumb ldr r%d [pc #%d]", rt, imm8);
7797 dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
7798 dsc->tmp[2] = displaced_read_reg (regs, dsc, 2);
7799 dsc->tmp[3] = displaced_read_reg (regs, dsc, 3);
7800 pc = displaced_read_reg (regs, dsc, ARM_PC_REGNUM);
7801 /* The assembler calculates the required value of the offset from the
7802 Align(PC,4) value of this instruction to the label. */
7803 pc = pc & 0xfffffffc;
7805 displaced_write_reg (regs, dsc, 2, pc, CANNOT_WRITE_PC);
7806 displaced_write_reg (regs, dsc, 3, imm8, CANNOT_WRITE_PC);
7808 dsc->rd = rt;
7809 dsc->u.ldst.xfersize = 4;
7810 dsc->u.ldst.rn = 0;
7811 dsc->u.ldst.immed = 0;
7812 dsc->u.ldst.writeback = 0;
7813 dsc->u.ldst.restore_r4 = 0;
7815 dsc->modinsn[0] = 0x58d0; /* ldr r0, [r2, r3]*/
7817 dsc->cleanup = &cleanup_load;
7819 return 0;
7822 /* Copy Thumb cbnz/cbz instruction. */
7824 static int
7825 thumb_copy_cbnz_cbz (struct gdbarch *gdbarch, uint16_t insn1,
7826 struct regcache *regs,
7827 arm_displaced_step_copy_insn_closure *dsc)
7829 int non_zero = bit (insn1, 11);
7830 unsigned int imm5 = (bit (insn1, 9) << 6) | (bits (insn1, 3, 7) << 1);
7831 CORE_ADDR from = dsc->insn_addr;
7832 int rn = bits (insn1, 0, 2);
7833 int rn_val = displaced_read_reg (regs, dsc, rn);
7835 dsc->u.branch.cond = (rn_val && non_zero) || (!rn_val && !non_zero);
7836 /* CBNZ and CBZ do not affect the condition flags. If condition is true,
7837 set it INST_AL, so cleanup_branch will know branch is taken, otherwise,
7838 condition is false, let it be, cleanup_branch will do nothing. */
7839 if (dsc->u.branch.cond)
7841 dsc->u.branch.cond = INST_AL;
7842 dsc->u.branch.dest = from + 4 + imm5;
7844 else
7845 dsc->u.branch.dest = from + 2;
7847 dsc->u.branch.link = 0;
7848 dsc->u.branch.exchange = 0;
7850 displaced_debug_printf ("copying %s [r%d = 0x%x] insn %.4x to %.8lx",
7851 non_zero ? "cbnz" : "cbz",
7852 rn, rn_val, insn1, dsc->u.branch.dest);
7854 dsc->modinsn[0] = THUMB_NOP;
7856 dsc->cleanup = &cleanup_branch;
7857 return 0;
7860 /* Copy Table Branch Byte/Halfword */
7861 static int
7862 thumb2_copy_table_branch (struct gdbarch *gdbarch, uint16_t insn1,
7863 uint16_t insn2, struct regcache *regs,
7864 arm_displaced_step_copy_insn_closure *dsc)
7866 ULONGEST rn_val, rm_val;
7867 int is_tbh = bit (insn2, 4);
7868 CORE_ADDR halfwords = 0;
7869 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
7871 rn_val = displaced_read_reg (regs, dsc, bits (insn1, 0, 3));
7872 rm_val = displaced_read_reg (regs, dsc, bits (insn2, 0, 3));
7874 if (is_tbh)
7876 gdb_byte buf[2];
7878 target_read_memory (rn_val + 2 * rm_val, buf, 2);
7879 halfwords = extract_unsigned_integer (buf, 2, byte_order);
7881 else
7883 gdb_byte buf[1];
7885 target_read_memory (rn_val + rm_val, buf, 1);
7886 halfwords = extract_unsigned_integer (buf, 1, byte_order);
7889 displaced_debug_printf ("%s base 0x%x offset 0x%x offset 0x%x",
7890 is_tbh ? "tbh" : "tbb",
7891 (unsigned int) rn_val, (unsigned int) rm_val,
7892 (unsigned int) halfwords);
7894 dsc->u.branch.cond = INST_AL;
7895 dsc->u.branch.link = 0;
7896 dsc->u.branch.exchange = 0;
7897 dsc->u.branch.dest = dsc->insn_addr + 4 + 2 * halfwords;
7899 dsc->cleanup = &cleanup_branch;
7901 return 0;
7904 static void
7905 cleanup_pop_pc_16bit_all (struct gdbarch *gdbarch, struct regcache *regs,
7906 arm_displaced_step_copy_insn_closure *dsc)
7908 /* PC <- r7 */
7909 int val = displaced_read_reg (regs, dsc, 7);
7910 displaced_write_reg (regs, dsc, ARM_PC_REGNUM, val, BX_WRITE_PC);
7912 /* r7 <- r8 */
7913 val = displaced_read_reg (regs, dsc, 8);
7914 displaced_write_reg (regs, dsc, 7, val, CANNOT_WRITE_PC);
7916 /* r8 <- tmp[0] */
7917 displaced_write_reg (regs, dsc, 8, dsc->tmp[0], CANNOT_WRITE_PC);
7921 static int
7922 thumb_copy_pop_pc_16bit (struct gdbarch *gdbarch, uint16_t insn1,
7923 struct regcache *regs,
7924 arm_displaced_step_copy_insn_closure *dsc)
7926 dsc->u.block.regmask = insn1 & 0x00ff;
7928 /* Rewrite instruction: POP {rX, rY, ...,rZ, PC}
7929 to :
7931 (1) register list is full, that is, r0-r7 are used.
7932 Prepare: tmp[0] <- r8
7934 POP {r0, r1, ...., r6, r7}; remove PC from reglist
7935 MOV r8, r7; Move value of r7 to r8;
7936 POP {r7}; Store PC value into r7.
7938 Cleanup: PC <- r7, r7 <- r8, r8 <-tmp[0]
7940 (2) register list is not full, supposing there are N registers in
7941 register list (except PC, 0 <= N <= 7).
7942 Prepare: for each i, 0 - N, tmp[i] <- ri.
7944 POP {r0, r1, ...., rN};
7946 Cleanup: Set registers in original reglist from r0 - rN. Restore r0 - rN
7947 from tmp[] properly.
7949 displaced_debug_printf ("copying thumb pop {%.8x, pc} insn %.4x",
7950 dsc->u.block.regmask, insn1);
7952 if (dsc->u.block.regmask == 0xff)
7954 dsc->tmp[0] = displaced_read_reg (regs, dsc, 8);
7956 dsc->modinsn[0] = (insn1 & 0xfeff); /* POP {r0,r1,...,r6, r7} */
7957 dsc->modinsn[1] = 0x46b8; /* MOV r8, r7 */
7958 dsc->modinsn[2] = 0xbc80; /* POP {r7} */
7960 dsc->numinsns = 3;
7961 dsc->cleanup = &cleanup_pop_pc_16bit_all;
7963 else
7965 unsigned int num_in_list = count_one_bits (dsc->u.block.regmask);
7966 unsigned int i;
7967 unsigned int new_regmask;
7969 for (i = 0; i < num_in_list + 1; i++)
7970 dsc->tmp[i] = displaced_read_reg (regs, dsc, i);
7972 new_regmask = (1 << (num_in_list + 1)) - 1;
7974 displaced_debug_printf ("POP {..., pc}: original reg list %.4x, "
7975 "modified list %.4x",
7976 (int) dsc->u.block.regmask, new_regmask);
7978 dsc->u.block.regmask |= 0x8000;
7979 dsc->u.block.writeback = 0;
7980 dsc->u.block.cond = INST_AL;
7982 dsc->modinsn[0] = (insn1 & ~0x1ff) | (new_regmask & 0xff);
7984 dsc->cleanup = &cleanup_block_load_pc;
7987 return 0;
7990 static void
7991 thumb_process_displaced_16bit_insn (struct gdbarch *gdbarch, uint16_t insn1,
7992 struct regcache *regs,
7993 arm_displaced_step_copy_insn_closure *dsc)
7995 unsigned short op_bit_12_15 = bits (insn1, 12, 15);
7996 unsigned short op_bit_10_11 = bits (insn1, 10, 11);
7997 int err = 0;
7999 /* 16-bit thumb instructions. */
8000 switch (op_bit_12_15)
8002 /* Shift (imme), add, subtract, move and compare. */
8003 case 0: case 1: case 2: case 3:
8004 err = thumb_copy_unmodified_16bit (gdbarch, insn1,
8005 "shift/add/sub/mov/cmp",
8006 dsc);
8007 break;
8008 case 4:
8009 switch (op_bit_10_11)
8011 case 0: /* Data-processing */
8012 err = thumb_copy_unmodified_16bit (gdbarch, insn1,
8013 "data-processing",
8014 dsc);
8015 break;
8016 case 1: /* Special data instructions and branch and exchange. */
8018 unsigned short op = bits (insn1, 7, 9);
8019 if (op == 6 || op == 7) /* BX or BLX */
8020 err = thumb_copy_bx_blx_reg (gdbarch, insn1, regs, dsc);
8021 else if (bits (insn1, 6, 7) != 0) /* ADD/MOV/CMP high registers. */
8022 err = thumb_copy_alu_reg (gdbarch, insn1, regs, dsc);
8023 else
8024 err = thumb_copy_unmodified_16bit (gdbarch, insn1, "special data",
8025 dsc);
8027 break;
8028 default: /* LDR (literal) */
8029 err = thumb_copy_16bit_ldr_literal (gdbarch, insn1, regs, dsc);
8031 break;
8032 case 5: case 6: case 7: case 8: case 9: /* Load/Store single data item */
8033 err = thumb_copy_unmodified_16bit (gdbarch, insn1, "ldr/str", dsc);
8034 break;
8035 case 10:
8036 if (op_bit_10_11 < 2) /* Generate PC-relative address */
8037 err = thumb_decode_pc_relative_16bit (gdbarch, insn1, regs, dsc);
8038 else /* Generate SP-relative address */
8039 err = thumb_copy_unmodified_16bit (gdbarch, insn1, "sp-relative", dsc);
8040 break;
8041 case 11: /* Misc 16-bit instructions */
8043 switch (bits (insn1, 8, 11))
8045 case 1: case 3: case 9: case 11: /* CBNZ, CBZ */
8046 err = thumb_copy_cbnz_cbz (gdbarch, insn1, regs, dsc);
8047 break;
8048 case 12: case 13: /* POP */
8049 if (bit (insn1, 8)) /* PC is in register list. */
8050 err = thumb_copy_pop_pc_16bit (gdbarch, insn1, regs, dsc);
8051 else
8052 err = thumb_copy_unmodified_16bit (gdbarch, insn1, "pop", dsc);
8053 break;
8054 case 15: /* If-Then, and hints */
8055 if (bits (insn1, 0, 3))
8056 /* If-Then makes up to four following instructions conditional.
8057 IT instruction itself is not conditional, so handle it as a
8058 common unmodified instruction. */
8059 err = thumb_copy_unmodified_16bit (gdbarch, insn1, "If-Then",
8060 dsc);
8061 else
8062 err = thumb_copy_unmodified_16bit (gdbarch, insn1, "hints", dsc);
8063 break;
8064 default:
8065 err = thumb_copy_unmodified_16bit (gdbarch, insn1, "misc", dsc);
8068 break;
8069 case 12:
8070 if (op_bit_10_11 < 2) /* Store multiple registers */
8071 err = thumb_copy_unmodified_16bit (gdbarch, insn1, "stm", dsc);
8072 else /* Load multiple registers */
8073 err = thumb_copy_unmodified_16bit (gdbarch, insn1, "ldm", dsc);
8074 break;
8075 case 13: /* Conditional branch and supervisor call */
8076 if (bits (insn1, 9, 11) != 7) /* conditional branch */
8077 err = thumb_copy_b (gdbarch, insn1, dsc);
8078 else
8079 err = thumb_copy_svc (gdbarch, insn1, regs, dsc);
8080 break;
8081 case 14: /* Unconditional branch */
8082 err = thumb_copy_b (gdbarch, insn1, dsc);
8083 break;
8084 default:
8085 err = 1;
8088 if (err)
8089 internal_error (__FILE__, __LINE__,
8090 _("thumb_process_displaced_16bit_insn: Instruction decode error"));
8093 static int
8094 decode_thumb_32bit_ld_mem_hints (struct gdbarch *gdbarch,
8095 uint16_t insn1, uint16_t insn2,
8096 struct regcache *regs,
8097 arm_displaced_step_copy_insn_closure *dsc)
8099 int rt = bits (insn2, 12, 15);
8100 int rn = bits (insn1, 0, 3);
8101 int op1 = bits (insn1, 7, 8);
8103 switch (bits (insn1, 5, 6))
8105 case 0: /* Load byte and memory hints */
8106 if (rt == 0xf) /* PLD/PLI */
8108 if (rn == 0xf)
8109 /* PLD literal or Encoding T3 of PLI(immediate, literal). */
8110 return thumb2_copy_preload (gdbarch, insn1, insn2, regs, dsc);
8111 else
8112 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8113 "pli/pld", dsc);
8115 else
8117 if (rn == 0xf) /* LDRB/LDRSB (literal) */
8118 return thumb2_copy_load_literal (gdbarch, insn1, insn2, regs, dsc,
8120 else
8121 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8122 "ldrb{reg, immediate}/ldrbt",
8123 dsc);
8126 break;
8127 case 1: /* Load halfword and memory hints. */
8128 if (rt == 0xf) /* PLD{W} and Unalloc memory hint. */
8129 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8130 "pld/unalloc memhint", dsc);
8131 else
8133 if (rn == 0xf)
8134 return thumb2_copy_load_literal (gdbarch, insn1, insn2, regs, dsc,
8136 else
8137 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8138 "ldrh/ldrht", dsc);
8140 break;
8141 case 2: /* Load word */
8143 int insn2_bit_8_11 = bits (insn2, 8, 11);
8145 if (rn == 0xf)
8146 return thumb2_copy_load_literal (gdbarch, insn1, insn2, regs, dsc, 4);
8147 else if (op1 == 0x1) /* Encoding T3 */
8148 return thumb2_copy_load_reg_imm (gdbarch, insn1, insn2, regs, dsc,
8149 0, 1);
8150 else /* op1 == 0x0 */
8152 if (insn2_bit_8_11 == 0xc || (insn2_bit_8_11 & 0x9) == 0x9)
8153 /* LDR (immediate) */
8154 return thumb2_copy_load_reg_imm (gdbarch, insn1, insn2, regs,
8155 dsc, bit (insn2, 8), 1);
8156 else if (insn2_bit_8_11 == 0xe) /* LDRT */
8157 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8158 "ldrt", dsc);
8159 else
8160 /* LDR (register) */
8161 return thumb2_copy_load_reg_imm (gdbarch, insn1, insn2, regs,
8162 dsc, 0, 0);
8164 break;
8166 default:
8167 return thumb_32bit_copy_undef (gdbarch, insn1, insn2, dsc);
8168 break;
8170 return 0;
8173 static void
8174 thumb_process_displaced_32bit_insn (struct gdbarch *gdbarch, uint16_t insn1,
8175 uint16_t insn2, struct regcache *regs,
8176 arm_displaced_step_copy_insn_closure *dsc)
8178 int err = 0;
8179 unsigned short op = bit (insn2, 15);
8180 unsigned int op1 = bits (insn1, 11, 12);
8182 switch (op1)
8184 case 1:
8186 switch (bits (insn1, 9, 10))
8188 case 0:
8189 if (bit (insn1, 6))
8191 /* Load/store {dual, exclusive}, table branch. */
8192 if (bits (insn1, 7, 8) == 1 && bits (insn1, 4, 5) == 1
8193 && bits (insn2, 5, 7) == 0)
8194 err = thumb2_copy_table_branch (gdbarch, insn1, insn2, regs,
8195 dsc);
8196 else
8197 /* PC is not allowed to use in load/store {dual, exclusive}
8198 instructions. */
8199 err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8200 "load/store dual/ex", dsc);
8202 else /* load/store multiple */
8204 switch (bits (insn1, 7, 8))
8206 case 0: case 3: /* SRS, RFE */
8207 err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8208 "srs/rfe", dsc);
8209 break;
8210 case 1: case 2: /* LDM/STM/PUSH/POP */
8211 err = thumb2_copy_block_xfer (gdbarch, insn1, insn2, regs, dsc);
8212 break;
8215 break;
8217 case 1:
8218 /* Data-processing (shift register). */
8219 err = thumb2_decode_dp_shift_reg (gdbarch, insn1, insn2, regs,
8220 dsc);
8221 break;
8222 default: /* Coprocessor instructions. */
8223 err = thumb2_decode_svc_copro (gdbarch, insn1, insn2, regs, dsc);
8224 break;
8226 break;
8228 case 2: /* op1 = 2 */
8229 if (op) /* Branch and misc control. */
8231 if (bit (insn2, 14) /* BLX/BL */
8232 || bit (insn2, 12) /* Unconditional branch */
8233 || (bits (insn1, 7, 9) != 0x7)) /* Conditional branch */
8234 err = thumb2_copy_b_bl_blx (gdbarch, insn1, insn2, regs, dsc);
8235 else
8236 err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8237 "misc ctrl", dsc);
8239 else
8241 if (bit (insn1, 9)) /* Data processing (plain binary imm). */
8243 int dp_op = bits (insn1, 4, 8);
8244 int rn = bits (insn1, 0, 3);
8245 if ((dp_op == 0 || dp_op == 0xa) && rn == 0xf)
8246 err = thumb_copy_pc_relative_32bit (gdbarch, insn1, insn2,
8247 regs, dsc);
8248 else
8249 err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8250 "dp/pb", dsc);
8252 else /* Data processing (modified immediate) */
8253 err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8254 "dp/mi", dsc);
8256 break;
8257 case 3: /* op1 = 3 */
8258 switch (bits (insn1, 9, 10))
8260 case 0:
8261 if (bit (insn1, 4))
8262 err = decode_thumb_32bit_ld_mem_hints (gdbarch, insn1, insn2,
8263 regs, dsc);
8264 else /* NEON Load/Store and Store single data item */
8265 err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8266 "neon elt/struct load/store",
8267 dsc);
8268 break;
8269 case 1: /* op1 = 3, bits (9, 10) == 1 */
8270 switch (bits (insn1, 7, 8))
8272 case 0: case 1: /* Data processing (register) */
8273 err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8274 "dp(reg)", dsc);
8275 break;
8276 case 2: /* Multiply and absolute difference */
8277 err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8278 "mul/mua/diff", dsc);
8279 break;
8280 case 3: /* Long multiply and divide */
8281 err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8282 "lmul/lmua", dsc);
8283 break;
8285 break;
8286 default: /* Coprocessor instructions */
8287 err = thumb2_decode_svc_copro (gdbarch, insn1, insn2, regs, dsc);
8288 break;
8290 break;
8291 default:
8292 err = 1;
8295 if (err)
8296 internal_error (__FILE__, __LINE__,
8297 _("thumb_process_displaced_32bit_insn: Instruction decode error"));
8301 static void
8302 thumb_process_displaced_insn (struct gdbarch *gdbarch, CORE_ADDR from,
8303 struct regcache *regs,
8304 arm_displaced_step_copy_insn_closure *dsc)
8306 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
8307 uint16_t insn1
8308 = read_memory_unsigned_integer (from, 2, byte_order_for_code);
8310 displaced_debug_printf ("process thumb insn %.4x at %.8lx",
8311 insn1, (unsigned long) from);
8313 dsc->is_thumb = 1;
8314 dsc->insn_size = thumb_insn_size (insn1);
8315 if (thumb_insn_size (insn1) == 4)
8317 uint16_t insn2
8318 = read_memory_unsigned_integer (from + 2, 2, byte_order_for_code);
8319 thumb_process_displaced_32bit_insn (gdbarch, insn1, insn2, regs, dsc);
8321 else
8322 thumb_process_displaced_16bit_insn (gdbarch, insn1, regs, dsc);
8325 void
8326 arm_process_displaced_insn (struct gdbarch *gdbarch, CORE_ADDR from,
8327 CORE_ADDR to, struct regcache *regs,
8328 arm_displaced_step_copy_insn_closure *dsc)
8330 int err = 0;
8331 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
8332 uint32_t insn;
8334 /* Most displaced instructions use a 1-instruction scratch space, so set this
8335 here and override below if/when necessary. */
8336 dsc->numinsns = 1;
8337 dsc->insn_addr = from;
8338 dsc->scratch_base = to;
8339 dsc->cleanup = NULL;
8340 dsc->wrote_to_pc = 0;
8342 if (!displaced_in_arm_mode (regs))
8343 return thumb_process_displaced_insn (gdbarch, from, regs, dsc);
8345 dsc->is_thumb = 0;
8346 dsc->insn_size = 4;
8347 insn = read_memory_unsigned_integer (from, 4, byte_order_for_code);
8348 displaced_debug_printf ("stepping insn %.8lx at %.8lx",
8349 (unsigned long) insn, (unsigned long) from);
8351 if ((insn & 0xf0000000) == 0xf0000000)
8352 err = arm_decode_unconditional (gdbarch, insn, regs, dsc);
8353 else switch (((insn & 0x10) >> 4) | ((insn & 0xe000000) >> 24))
8355 case 0x0: case 0x1: case 0x2: case 0x3:
8356 err = arm_decode_dp_misc (gdbarch, insn, regs, dsc);
8357 break;
8359 case 0x4: case 0x5: case 0x6:
8360 err = arm_decode_ld_st_word_ubyte (gdbarch, insn, regs, dsc);
8361 break;
8363 case 0x7:
8364 err = arm_decode_media (gdbarch, insn, dsc);
8365 break;
8367 case 0x8: case 0x9: case 0xa: case 0xb:
8368 err = arm_decode_b_bl_ldmstm (gdbarch, insn, regs, dsc);
8369 break;
8371 case 0xc: case 0xd: case 0xe: case 0xf:
8372 err = arm_decode_svc_copro (gdbarch, insn, regs, dsc);
8373 break;
8376 if (err)
8377 internal_error (__FILE__, __LINE__,
8378 _("arm_process_displaced_insn: Instruction decode error"));
8381 /* Actually set up the scratch space for a displaced instruction. */
8383 void
8384 arm_displaced_init_closure (struct gdbarch *gdbarch, CORE_ADDR from,
8385 CORE_ADDR to,
8386 arm_displaced_step_copy_insn_closure *dsc)
8388 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
8389 unsigned int i, len, offset;
8390 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
8391 int size = dsc->is_thumb? 2 : 4;
8392 const gdb_byte *bkp_insn;
8394 offset = 0;
8395 /* Poke modified instruction(s). */
8396 for (i = 0; i < dsc->numinsns; i++)
8398 if (size == 4)
8399 displaced_debug_printf ("writing insn %.8lx at %.8lx",
8400 dsc->modinsn[i], (unsigned long) to + offset);
8401 else if (size == 2)
8402 displaced_debug_printf ("writing insn %.4x at %.8lx",
8403 (unsigned short) dsc->modinsn[i],
8404 (unsigned long) to + offset);
8406 write_memory_unsigned_integer (to + offset, size,
8407 byte_order_for_code,
8408 dsc->modinsn[i]);
8409 offset += size;
8412 /* Choose the correct breakpoint instruction. */
8413 if (dsc->is_thumb)
8415 bkp_insn = tdep->thumb_breakpoint;
8416 len = tdep->thumb_breakpoint_size;
8418 else
8420 bkp_insn = tdep->arm_breakpoint;
8421 len = tdep->arm_breakpoint_size;
8424 /* Put breakpoint afterwards. */
8425 write_memory (to + offset, bkp_insn, len);
8427 displaced_debug_printf ("copy %s->%s", paddress (gdbarch, from),
8428 paddress (gdbarch, to));
8431 /* Entry point for cleaning things up after a displaced instruction has been
8432 single-stepped. */
8434 void
8435 arm_displaced_step_fixup (struct gdbarch *gdbarch,
8436 struct displaced_step_copy_insn_closure *dsc_,
8437 CORE_ADDR from, CORE_ADDR to,
8438 struct regcache *regs)
8440 arm_displaced_step_copy_insn_closure *dsc
8441 = (arm_displaced_step_copy_insn_closure *) dsc_;
8443 if (dsc->cleanup)
8444 dsc->cleanup (gdbarch, regs, dsc);
8446 if (!dsc->wrote_to_pc)
8447 regcache_cooked_write_unsigned (regs, ARM_PC_REGNUM,
8448 dsc->insn_addr + dsc->insn_size);
8452 #include "bfd-in2.h"
8453 #include "libcoff.h"
8455 static int
8456 gdb_print_insn_arm (bfd_vma memaddr, disassemble_info *info)
8458 gdb_disassemble_info *di
8459 = static_cast<gdb_disassemble_info *> (info->application_data);
8460 struct gdbarch *gdbarch = di->arch ();
8462 if (arm_pc_is_thumb (gdbarch, memaddr))
8464 static asymbol *asym;
8465 static combined_entry_type ce;
8466 static struct coff_symbol_struct csym;
8467 static struct bfd fake_bfd;
8468 static bfd_target fake_target;
8470 if (csym.native == NULL)
8472 /* Create a fake symbol vector containing a Thumb symbol.
8473 This is solely so that the code in print_insn_little_arm()
8474 and print_insn_big_arm() in opcodes/arm-dis.c will detect
8475 the presence of a Thumb symbol and switch to decoding
8476 Thumb instructions. */
8478 fake_target.flavour = bfd_target_coff_flavour;
8479 fake_bfd.xvec = &fake_target;
8480 ce.u.syment.n_sclass = C_THUMBEXTFUNC;
8481 csym.native = &ce;
8482 csym.symbol.the_bfd = &fake_bfd;
8483 csym.symbol.name = "fake";
8484 asym = (asymbol *) & csym;
8487 memaddr = UNMAKE_THUMB_ADDR (memaddr);
8488 info->symbols = &asym;
8490 else
8491 info->symbols = NULL;
8493 /* GDB is able to get bfd_mach from the exe_bfd, info->mach is
8494 accurate, so mark USER_SPECIFIED_MACHINE_TYPE bit. Otherwise,
8495 opcodes/arm-dis.c:print_insn reset info->mach, and it will trigger
8496 the assert on the mismatch of info->mach and
8497 bfd_get_mach (current_program_space->exec_bfd ()) in
8498 default_print_insn. */
8499 if (current_program_space->exec_bfd () != NULL
8500 && (current_program_space->exec_bfd ()->arch_info
8501 == gdbarch_bfd_arch_info (gdbarch)))
8502 info->flags |= USER_SPECIFIED_MACHINE_TYPE;
8504 return default_print_insn (memaddr, info);
8507 /* The following define instruction sequences that will cause ARM
8508 cpu's to take an undefined instruction trap. These are used to
8509 signal a breakpoint to GDB.
8511 The newer ARMv4T cpu's are capable of operating in ARM or Thumb
8512 modes. A different instruction is required for each mode. The ARM
8513 cpu's can also be big or little endian. Thus four different
8514 instructions are needed to support all cases.
8516 Note: ARMv4 defines several new instructions that will take the
8517 undefined instruction trap. ARM7TDMI is nominally ARMv4T, but does
8518 not in fact add the new instructions. The new undefined
8519 instructions in ARMv4 are all instructions that had no defined
8520 behaviour in earlier chips. There is no guarantee that they will
8521 raise an exception, but may be treated as NOP's. In practice, it
8522 may only safe to rely on instructions matching:
8524 3 3 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1
8525 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
8526 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
8528 Even this may only true if the condition predicate is true. The
8529 following use a condition predicate of ALWAYS so it is always TRUE.
8531 There are other ways of forcing a breakpoint. GNU/Linux, RISC iX,
8532 and NetBSD all use a software interrupt rather than an undefined
8533 instruction to force a trap. This can be handled by by the
8534 abi-specific code during establishment of the gdbarch vector. */
8536 #define ARM_LE_BREAKPOINT {0xFE,0xDE,0xFF,0xE7}
8537 #define ARM_BE_BREAKPOINT {0xE7,0xFF,0xDE,0xFE}
8538 #define THUMB_LE_BREAKPOINT {0xbe,0xbe}
8539 #define THUMB_BE_BREAKPOINT {0xbe,0xbe}
8541 static const gdb_byte arm_default_arm_le_breakpoint[] = ARM_LE_BREAKPOINT;
8542 static const gdb_byte arm_default_arm_be_breakpoint[] = ARM_BE_BREAKPOINT;
8543 static const gdb_byte arm_default_thumb_le_breakpoint[] = THUMB_LE_BREAKPOINT;
8544 static const gdb_byte arm_default_thumb_be_breakpoint[] = THUMB_BE_BREAKPOINT;
8546 /* Implement the breakpoint_kind_from_pc gdbarch method. */
8548 static int
8549 arm_breakpoint_kind_from_pc (struct gdbarch *gdbarch, CORE_ADDR *pcptr)
8551 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
8552 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
8554 if (arm_pc_is_thumb (gdbarch, *pcptr))
8556 *pcptr = UNMAKE_THUMB_ADDR (*pcptr);
8558 /* If we have a separate 32-bit breakpoint instruction for Thumb-2,
8559 check whether we are replacing a 32-bit instruction. */
8560 if (tdep->thumb2_breakpoint != NULL)
8562 gdb_byte buf[2];
8564 if (target_read_memory (*pcptr, buf, 2) == 0)
8566 unsigned short inst1;
8568 inst1 = extract_unsigned_integer (buf, 2, byte_order_for_code);
8569 if (thumb_insn_size (inst1) == 4)
8570 return ARM_BP_KIND_THUMB2;
8574 return ARM_BP_KIND_THUMB;
8576 else
8577 return ARM_BP_KIND_ARM;
8581 /* Implement the sw_breakpoint_from_kind gdbarch method. */
8583 static const gdb_byte *
8584 arm_sw_breakpoint_from_kind (struct gdbarch *gdbarch, int kind, int *size)
8586 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
8588 switch (kind)
8590 case ARM_BP_KIND_ARM:
8591 *size = tdep->arm_breakpoint_size;
8592 return tdep->arm_breakpoint;
8593 case ARM_BP_KIND_THUMB:
8594 *size = tdep->thumb_breakpoint_size;
8595 return tdep->thumb_breakpoint;
8596 case ARM_BP_KIND_THUMB2:
8597 *size = tdep->thumb2_breakpoint_size;
8598 return tdep->thumb2_breakpoint;
8599 default:
8600 gdb_assert_not_reached ("unexpected arm breakpoint kind");
8604 /* Implement the breakpoint_kind_from_current_state gdbarch method. */
8606 static int
8607 arm_breakpoint_kind_from_current_state (struct gdbarch *gdbarch,
8608 struct regcache *regcache,
8609 CORE_ADDR *pcptr)
8611 gdb_byte buf[4];
8613 /* Check the memory pointed by PC is readable. */
8614 if (target_read_memory (regcache_read_pc (regcache), buf, 4) == 0)
8616 struct arm_get_next_pcs next_pcs_ctx;
8618 arm_get_next_pcs_ctor (&next_pcs_ctx,
8619 &arm_get_next_pcs_ops,
8620 gdbarch_byte_order (gdbarch),
8621 gdbarch_byte_order_for_code (gdbarch),
8623 regcache);
8625 std::vector<CORE_ADDR> next_pcs = arm_get_next_pcs (&next_pcs_ctx);
8627 /* If MEMADDR is the next instruction of current pc, do the
8628 software single step computation, and get the thumb mode by
8629 the destination address. */
8630 for (CORE_ADDR pc : next_pcs)
8632 if (UNMAKE_THUMB_ADDR (pc) == *pcptr)
8634 if (IS_THUMB_ADDR (pc))
8636 *pcptr = MAKE_THUMB_ADDR (*pcptr);
8637 return arm_breakpoint_kind_from_pc (gdbarch, pcptr);
8639 else
8640 return ARM_BP_KIND_ARM;
8645 return arm_breakpoint_kind_from_pc (gdbarch, pcptr);
8648 /* Extract from an array REGBUF containing the (raw) register state a
8649 function return value of type TYPE, and copy that, in virtual
8650 format, into VALBUF. */
8652 static void
8653 arm_extract_return_value (struct type *type, struct regcache *regs,
8654 gdb_byte *valbuf)
8656 struct gdbarch *gdbarch = regs->arch ();
8657 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
8658 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
8660 if (TYPE_CODE_FLT == type->code ())
8662 switch (tdep->fp_model)
8664 case ARM_FLOAT_FPA:
8666 /* The value is in register F0 in internal format. We need to
8667 extract the raw value and then convert it to the desired
8668 internal type. */
8669 bfd_byte tmpbuf[ARM_FP_REGISTER_SIZE];
8671 regs->cooked_read (ARM_F0_REGNUM, tmpbuf);
8672 target_float_convert (tmpbuf, arm_ext_type (gdbarch),
8673 valbuf, type);
8675 break;
8677 case ARM_FLOAT_SOFT_FPA:
8678 case ARM_FLOAT_SOFT_VFP:
8679 /* ARM_FLOAT_VFP can arise if this is a variadic function so
8680 not using the VFP ABI code. */
8681 case ARM_FLOAT_VFP:
8682 regs->cooked_read (ARM_A1_REGNUM, valbuf);
8683 if (TYPE_LENGTH (type) > 4)
8684 regs->cooked_read (ARM_A1_REGNUM + 1,
8685 valbuf + ARM_INT_REGISTER_SIZE);
8686 break;
8688 default:
8689 internal_error (__FILE__, __LINE__,
8690 _("arm_extract_return_value: "
8691 "Floating point model not supported"));
8692 break;
8695 else if (type->code () == TYPE_CODE_INT
8696 || type->code () == TYPE_CODE_CHAR
8697 || type->code () == TYPE_CODE_BOOL
8698 || type->code () == TYPE_CODE_PTR
8699 || TYPE_IS_REFERENCE (type)
8700 || type->code () == TYPE_CODE_ENUM
8701 || is_fixed_point_type (type))
8703 /* If the type is a plain integer, then the access is
8704 straight-forward. Otherwise we have to play around a bit
8705 more. */
8706 int len = TYPE_LENGTH (type);
8707 int regno = ARM_A1_REGNUM;
8708 ULONGEST tmp;
8710 while (len > 0)
8712 /* By using store_unsigned_integer we avoid having to do
8713 anything special for small big-endian values. */
8714 regcache_cooked_read_unsigned (regs, regno++, &tmp);
8715 store_unsigned_integer (valbuf,
8716 (len > ARM_INT_REGISTER_SIZE
8717 ? ARM_INT_REGISTER_SIZE : len),
8718 byte_order, tmp);
8719 len -= ARM_INT_REGISTER_SIZE;
8720 valbuf += ARM_INT_REGISTER_SIZE;
8723 else
8725 /* For a structure or union the behaviour is as if the value had
8726 been stored to word-aligned memory and then loaded into
8727 registers with 32-bit load instruction(s). */
8728 int len = TYPE_LENGTH (type);
8729 int regno = ARM_A1_REGNUM;
8730 bfd_byte tmpbuf[ARM_INT_REGISTER_SIZE];
8732 while (len > 0)
8734 regs->cooked_read (regno++, tmpbuf);
8735 memcpy (valbuf, tmpbuf,
8736 len > ARM_INT_REGISTER_SIZE ? ARM_INT_REGISTER_SIZE : len);
8737 len -= ARM_INT_REGISTER_SIZE;
8738 valbuf += ARM_INT_REGISTER_SIZE;
8744 /* Will a function return an aggregate type in memory or in a
8745 register? Return 0 if an aggregate type can be returned in a
8746 register, 1 if it must be returned in memory. */
8748 static int
8749 arm_return_in_memory (struct gdbarch *gdbarch, struct type *type)
8751 enum type_code code;
8753 type = check_typedef (type);
8755 /* Simple, non-aggregate types (ie not including vectors and
8756 complex) are always returned in a register (or registers). */
8757 code = type->code ();
8758 if (TYPE_CODE_STRUCT != code && TYPE_CODE_UNION != code
8759 && TYPE_CODE_ARRAY != code && TYPE_CODE_COMPLEX != code)
8760 return 0;
8762 if (TYPE_CODE_ARRAY == code && type->is_vector ())
8764 /* Vector values should be returned using ARM registers if they
8765 are not over 16 bytes. */
8766 return (TYPE_LENGTH (type) > 16);
8769 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
8770 if (tdep->arm_abi != ARM_ABI_APCS)
8772 /* The AAPCS says all aggregates not larger than a word are returned
8773 in a register. */
8774 if (TYPE_LENGTH (type) <= ARM_INT_REGISTER_SIZE
8775 && language_pass_by_reference (type).trivially_copyable)
8776 return 0;
8778 return 1;
8780 else
8782 int nRc;
8784 /* All aggregate types that won't fit in a register must be returned
8785 in memory. */
8786 if (TYPE_LENGTH (type) > ARM_INT_REGISTER_SIZE
8787 || !language_pass_by_reference (type).trivially_copyable)
8788 return 1;
8790 /* In the ARM ABI, "integer" like aggregate types are returned in
8791 registers. For an aggregate type to be integer like, its size
8792 must be less than or equal to ARM_INT_REGISTER_SIZE and the
8793 offset of each addressable subfield must be zero. Note that bit
8794 fields are not addressable, and all addressable subfields of
8795 unions always start at offset zero.
8797 This function is based on the behaviour of GCC 2.95.1.
8798 See: gcc/arm.c: arm_return_in_memory() for details.
8800 Note: All versions of GCC before GCC 2.95.2 do not set up the
8801 parameters correctly for a function returning the following
8802 structure: struct { float f;}; This should be returned in memory,
8803 not a register. Richard Earnshaw sent me a patch, but I do not
8804 know of any way to detect if a function like the above has been
8805 compiled with the correct calling convention. */
8807 /* Assume all other aggregate types can be returned in a register.
8808 Run a check for structures, unions and arrays. */
8809 nRc = 0;
8811 if ((TYPE_CODE_STRUCT == code) || (TYPE_CODE_UNION == code))
8813 int i;
8814 /* Need to check if this struct/union is "integer" like. For
8815 this to be true, its size must be less than or equal to
8816 ARM_INT_REGISTER_SIZE and the offset of each addressable
8817 subfield must be zero. Note that bit fields are not
8818 addressable, and unions always start at offset zero. If any
8819 of the subfields is a floating point type, the struct/union
8820 cannot be an integer type. */
8822 /* For each field in the object, check:
8823 1) Is it FP? --> yes, nRc = 1;
8824 2) Is it addressable (bitpos != 0) and
8825 not packed (bitsize == 0)?
8826 --> yes, nRc = 1
8829 for (i = 0; i < type->num_fields (); i++)
8831 enum type_code field_type_code;
8833 field_type_code
8834 = check_typedef (type->field (i).type ())->code ();
8836 /* Is it a floating point type field? */
8837 if (field_type_code == TYPE_CODE_FLT)
8839 nRc = 1;
8840 break;
8843 /* If bitpos != 0, then we have to care about it. */
8844 if (type->field (i).loc_bitpos () != 0)
8846 /* Bitfields are not addressable. If the field bitsize is
8847 zero, then the field is not packed. Hence it cannot be
8848 a bitfield or any other packed type. */
8849 if (TYPE_FIELD_BITSIZE (type, i) == 0)
8851 nRc = 1;
8852 break;
8858 return nRc;
8862 /* Write into appropriate registers a function return value of type
8863 TYPE, given in virtual format. */
8865 static void
8866 arm_store_return_value (struct type *type, struct regcache *regs,
8867 const gdb_byte *valbuf)
8869 struct gdbarch *gdbarch = regs->arch ();
8870 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
8872 if (type->code () == TYPE_CODE_FLT)
8874 gdb_byte buf[ARM_FP_REGISTER_SIZE];
8875 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
8877 switch (tdep->fp_model)
8879 case ARM_FLOAT_FPA:
8881 target_float_convert (valbuf, type, buf, arm_ext_type (gdbarch));
8882 regs->cooked_write (ARM_F0_REGNUM, buf);
8883 break;
8885 case ARM_FLOAT_SOFT_FPA:
8886 case ARM_FLOAT_SOFT_VFP:
8887 /* ARM_FLOAT_VFP can arise if this is a variadic function so
8888 not using the VFP ABI code. */
8889 case ARM_FLOAT_VFP:
8890 regs->cooked_write (ARM_A1_REGNUM, valbuf);
8891 if (TYPE_LENGTH (type) > 4)
8892 regs->cooked_write (ARM_A1_REGNUM + 1,
8893 valbuf + ARM_INT_REGISTER_SIZE);
8894 break;
8896 default:
8897 internal_error (__FILE__, __LINE__,
8898 _("arm_store_return_value: Floating "
8899 "point model not supported"));
8900 break;
8903 else if (type->code () == TYPE_CODE_INT
8904 || type->code () == TYPE_CODE_CHAR
8905 || type->code () == TYPE_CODE_BOOL
8906 || type->code () == TYPE_CODE_PTR
8907 || TYPE_IS_REFERENCE (type)
8908 || type->code () == TYPE_CODE_ENUM)
8910 if (TYPE_LENGTH (type) <= 4)
8912 /* Values of one word or less are zero/sign-extended and
8913 returned in r0. */
8914 bfd_byte tmpbuf[ARM_INT_REGISTER_SIZE];
8915 LONGEST val = unpack_long (type, valbuf);
8917 store_signed_integer (tmpbuf, ARM_INT_REGISTER_SIZE, byte_order, val);
8918 regs->cooked_write (ARM_A1_REGNUM, tmpbuf);
8920 else
8922 /* Integral values greater than one word are stored in consecutive
8923 registers starting with r0. This will always be a multiple of
8924 the regiser size. */
8925 int len = TYPE_LENGTH (type);
8926 int regno = ARM_A1_REGNUM;
8928 while (len > 0)
8930 regs->cooked_write (regno++, valbuf);
8931 len -= ARM_INT_REGISTER_SIZE;
8932 valbuf += ARM_INT_REGISTER_SIZE;
8936 else
8938 /* For a structure or union the behaviour is as if the value had
8939 been stored to word-aligned memory and then loaded into
8940 registers with 32-bit load instruction(s). */
8941 int len = TYPE_LENGTH (type);
8942 int regno = ARM_A1_REGNUM;
8943 bfd_byte tmpbuf[ARM_INT_REGISTER_SIZE];
8945 while (len > 0)
8947 memcpy (tmpbuf, valbuf,
8948 len > ARM_INT_REGISTER_SIZE ? ARM_INT_REGISTER_SIZE : len);
8949 regs->cooked_write (regno++, tmpbuf);
8950 len -= ARM_INT_REGISTER_SIZE;
8951 valbuf += ARM_INT_REGISTER_SIZE;
8957 /* Handle function return values. */
8959 static enum return_value_convention
8960 arm_return_value (struct gdbarch *gdbarch, struct value *function,
8961 struct type *valtype, struct regcache *regcache,
8962 gdb_byte *readbuf, const gdb_byte *writebuf)
8964 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
8965 struct type *func_type = function ? value_type (function) : NULL;
8966 enum arm_vfp_cprc_base_type vfp_base_type;
8967 int vfp_base_count;
8969 if (arm_vfp_abi_for_function (gdbarch, func_type)
8970 && arm_vfp_call_candidate (valtype, &vfp_base_type, &vfp_base_count))
8972 int reg_char = arm_vfp_cprc_reg_char (vfp_base_type);
8973 int unit_length = arm_vfp_cprc_unit_length (vfp_base_type);
8974 int i;
8975 for (i = 0; i < vfp_base_count; i++)
8977 if (reg_char == 'q')
8979 if (writebuf)
8980 arm_neon_quad_write (gdbarch, regcache, i,
8981 writebuf + i * unit_length);
8983 if (readbuf)
8984 arm_neon_quad_read (gdbarch, regcache, i,
8985 readbuf + i * unit_length);
8987 else
8989 char name_buf[4];
8990 int regnum;
8992 xsnprintf (name_buf, sizeof (name_buf), "%c%d", reg_char, i);
8993 regnum = user_reg_map_name_to_regnum (gdbarch, name_buf,
8994 strlen (name_buf));
8995 if (writebuf)
8996 regcache->cooked_write (regnum, writebuf + i * unit_length);
8997 if (readbuf)
8998 regcache->cooked_read (regnum, readbuf + i * unit_length);
9001 return RETURN_VALUE_REGISTER_CONVENTION;
9004 if (valtype->code () == TYPE_CODE_STRUCT
9005 || valtype->code () == TYPE_CODE_UNION
9006 || valtype->code () == TYPE_CODE_ARRAY)
9008 /* From the AAPCS document:
9010 Result return:
9012 A Composite Type larger than 4 bytes, or whose size cannot be
9013 determined statically by both caller and callee, is stored in memory
9014 at an address passed as an extra argument when the function was
9015 called (Parameter Passing, rule A.4). The memory to be used for the
9016 result may be modified at any point during the function call.
9018 Parameter Passing:
9020 A.4: If the subroutine is a function that returns a result in memory,
9021 then the address for the result is placed in r0 and the NCRN is set
9022 to r1. */
9023 if (tdep->struct_return == pcc_struct_return
9024 || arm_return_in_memory (gdbarch, valtype))
9026 if (readbuf)
9028 CORE_ADDR addr;
9030 regcache->cooked_read (ARM_A1_REGNUM, &addr);
9031 read_memory (addr, readbuf, TYPE_LENGTH (valtype));
9033 return RETURN_VALUE_ABI_RETURNS_ADDRESS;
9036 else if (valtype->code () == TYPE_CODE_COMPLEX)
9038 if (arm_return_in_memory (gdbarch, valtype))
9039 return RETURN_VALUE_STRUCT_CONVENTION;
9042 if (writebuf)
9043 arm_store_return_value (valtype, regcache, writebuf);
9045 if (readbuf)
9046 arm_extract_return_value (valtype, regcache, readbuf);
9048 return RETURN_VALUE_REGISTER_CONVENTION;
9052 static int
9053 arm_get_longjmp_target (struct frame_info *frame, CORE_ADDR *pc)
9055 struct gdbarch *gdbarch = get_frame_arch (frame);
9056 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
9057 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
9058 CORE_ADDR jb_addr;
9059 gdb_byte buf[ARM_INT_REGISTER_SIZE];
9061 jb_addr = get_frame_register_unsigned (frame, ARM_A1_REGNUM);
9063 if (target_read_memory (jb_addr + tdep->jb_pc * tdep->jb_elt_size, buf,
9064 ARM_INT_REGISTER_SIZE))
9065 return 0;
9067 *pc = extract_unsigned_integer (buf, ARM_INT_REGISTER_SIZE, byte_order);
9068 return 1;
9070 /* A call to cmse secure entry function "foo" at "a" is modified by
9071 GNU ld as "b".
9072 a) bl xxxx <foo>
9074 <foo>
9075 xxxx:
9077 b) bl yyyy <__acle_se_foo>
9079 section .gnu.sgstubs:
9080 <foo>
9081 yyyy: sg // secure gateway
9082 b.w xxxx <__acle_se_foo> // original_branch_dest
9084 <__acle_se_foo>
9085 xxxx:
9087 When the control at "b", the pc contains "yyyy" (sg address) which is a
9088 trampoline and does not exist in source code. This function returns the
9089 target pc "xxxx". For more details please refer to section 5.4
9090 (Entry functions) and section 3.4.4 (C level development flow of secure code)
9091 of "armv8-m-security-extensions-requirements-on-development-tools-engineering-specification"
9092 document on www.developer.arm.com. */
9094 static CORE_ADDR
9095 arm_skip_cmse_entry (CORE_ADDR pc, const char *name, struct objfile *objfile)
9097 int target_len = strlen (name) + strlen ("__acle_se_") + 1;
9098 char *target_name = (char *) alloca (target_len);
9099 xsnprintf (target_name, target_len, "%s%s", "__acle_se_", name);
9101 struct bound_minimal_symbol minsym
9102 = lookup_minimal_symbol (target_name, NULL, objfile);
9104 if (minsym.minsym != nullptr)
9105 return minsym.value_address ();
9107 return 0;
9110 /* Return true when SEC points to ".gnu.sgstubs" section. */
9112 static bool
9113 arm_is_sgstubs_section (struct obj_section *sec)
9115 return (sec != nullptr
9116 && sec->the_bfd_section != nullptr
9117 && sec->the_bfd_section->name != nullptr
9118 && streq (sec->the_bfd_section->name, ".gnu.sgstubs"));
9121 /* Recognize GCC and GNU ld's trampolines. If we are in a trampoline,
9122 return the target PC. Otherwise return 0. */
9124 CORE_ADDR
9125 arm_skip_stub (struct frame_info *frame, CORE_ADDR pc)
9127 const char *name;
9128 int namelen;
9129 CORE_ADDR start_addr;
9131 /* Find the starting address and name of the function containing the PC. */
9132 if (find_pc_partial_function (pc, &name, &start_addr, NULL) == 0)
9134 /* Trampoline 'bx reg' doesn't belong to any functions. Do the
9135 check here. */
9136 start_addr = arm_skip_bx_reg (frame, pc);
9137 if (start_addr != 0)
9138 return start_addr;
9140 return 0;
9143 /* If PC is in a Thumb call or return stub, return the address of the
9144 target PC, which is in a register. The thunk functions are called
9145 _call_via_xx, where x is the register name. The possible names
9146 are r0-r9, sl, fp, ip, sp, and lr. ARM RealView has similar
9147 functions, named __ARM_call_via_r[0-7]. */
9148 if (startswith (name, "_call_via_")
9149 || startswith (name, "__ARM_call_via_"))
9151 /* Use the name suffix to determine which register contains the
9152 target PC. */
9153 static const char *table[15] =
9154 {"r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
9155 "r8", "r9", "sl", "fp", "ip", "sp", "lr"
9157 int regno;
9158 int offset = strlen (name) - 2;
9160 for (regno = 0; regno <= 14; regno++)
9161 if (strcmp (&name[offset], table[regno]) == 0)
9162 return get_frame_register_unsigned (frame, regno);
9165 /* GNU ld generates __foo_from_arm or __foo_from_thumb for
9166 non-interworking calls to foo. We could decode the stubs
9167 to find the target but it's easier to use the symbol table. */
9168 namelen = strlen (name);
9169 if (name[0] == '_' && name[1] == '_'
9170 && ((namelen > 2 + strlen ("_from_thumb")
9171 && startswith (name + namelen - strlen ("_from_thumb"), "_from_thumb"))
9172 || (namelen > 2 + strlen ("_from_arm")
9173 && startswith (name + namelen - strlen ("_from_arm"), "_from_arm"))))
9175 char *target_name;
9176 int target_len = namelen - 2;
9177 struct bound_minimal_symbol minsym;
9178 struct objfile *objfile;
9179 struct obj_section *sec;
9181 if (name[namelen - 1] == 'b')
9182 target_len -= strlen ("_from_thumb");
9183 else
9184 target_len -= strlen ("_from_arm");
9186 target_name = (char *) alloca (target_len + 1);
9187 memcpy (target_name, name + 2, target_len);
9188 target_name[target_len] = '\0';
9190 sec = find_pc_section (pc);
9191 objfile = (sec == NULL) ? NULL : sec->objfile;
9192 minsym = lookup_minimal_symbol (target_name, NULL, objfile);
9193 if (minsym.minsym != NULL)
9194 return minsym.value_address ();
9195 else
9196 return 0;
9199 struct obj_section *section = find_pc_section (pc);
9201 /* Check whether SECTION points to the ".gnu.sgstubs" section. */
9202 if (arm_is_sgstubs_section (section))
9203 return arm_skip_cmse_entry (pc, name, section->objfile);
9205 return 0; /* not a stub */
9208 static void
9209 arm_update_current_architecture (void)
9211 /* If the current architecture is not ARM, we have nothing to do. */
9212 if (gdbarch_bfd_arch_info (target_gdbarch ())->arch != bfd_arch_arm)
9213 return;
9215 /* Update the architecture. */
9216 gdbarch_info info;
9217 if (!gdbarch_update_p (info))
9218 internal_error (__FILE__, __LINE__, _("could not update architecture"));
9221 static void
9222 set_fp_model_sfunc (const char *args, int from_tty,
9223 struct cmd_list_element *c)
9225 int fp_model;
9227 for (fp_model = ARM_FLOAT_AUTO; fp_model != ARM_FLOAT_LAST; fp_model++)
9228 if (strcmp (current_fp_model, fp_model_strings[fp_model]) == 0)
9230 arm_fp_model = (enum arm_float_model) fp_model;
9231 break;
9234 if (fp_model == ARM_FLOAT_LAST)
9235 internal_error (__FILE__, __LINE__, _("Invalid fp model accepted: %s."),
9236 current_fp_model);
9238 arm_update_current_architecture ();
9241 static void
9242 show_fp_model (struct ui_file *file, int from_tty,
9243 struct cmd_list_element *c, const char *value)
9245 if (arm_fp_model == ARM_FLOAT_AUTO
9246 && gdbarch_bfd_arch_info (target_gdbarch ())->arch == bfd_arch_arm)
9248 arm_gdbarch_tdep *tdep
9249 = gdbarch_tdep<arm_gdbarch_tdep> (target_gdbarch ());
9251 gdb_printf (file, _("\
9252 The current ARM floating point model is \"auto\" (currently \"%s\").\n"),
9253 fp_model_strings[tdep->fp_model]);
9255 else
9256 gdb_printf (file, _("\
9257 The current ARM floating point model is \"%s\".\n"),
9258 fp_model_strings[arm_fp_model]);
9261 static void
9262 arm_set_abi (const char *args, int from_tty,
9263 struct cmd_list_element *c)
9265 int arm_abi;
9267 for (arm_abi = ARM_ABI_AUTO; arm_abi != ARM_ABI_LAST; arm_abi++)
9268 if (strcmp (arm_abi_string, arm_abi_strings[arm_abi]) == 0)
9270 arm_abi_global = (enum arm_abi_kind) arm_abi;
9271 break;
9274 if (arm_abi == ARM_ABI_LAST)
9275 internal_error (__FILE__, __LINE__, _("Invalid ABI accepted: %s."),
9276 arm_abi_string);
9278 arm_update_current_architecture ();
9281 static void
9282 arm_show_abi (struct ui_file *file, int from_tty,
9283 struct cmd_list_element *c, const char *value)
9285 if (arm_abi_global == ARM_ABI_AUTO
9286 && gdbarch_bfd_arch_info (target_gdbarch ())->arch == bfd_arch_arm)
9288 arm_gdbarch_tdep *tdep
9289 = gdbarch_tdep<arm_gdbarch_tdep> (target_gdbarch ());
9291 gdb_printf (file, _("\
9292 The current ARM ABI is \"auto\" (currently \"%s\").\n"),
9293 arm_abi_strings[tdep->arm_abi]);
9295 else
9296 gdb_printf (file, _("The current ARM ABI is \"%s\".\n"),
9297 arm_abi_string);
9300 static void
9301 arm_show_fallback_mode (struct ui_file *file, int from_tty,
9302 struct cmd_list_element *c, const char *value)
9304 gdb_printf (file,
9305 _("The current execution mode assumed "
9306 "(when symbols are unavailable) is \"%s\".\n"),
9307 arm_fallback_mode_string);
9310 static void
9311 arm_show_force_mode (struct ui_file *file, int from_tty,
9312 struct cmd_list_element *c, const char *value)
9314 gdb_printf (file,
9315 _("The current execution mode assumed "
9316 "(even when symbols are available) is \"%s\".\n"),
9317 arm_force_mode_string);
9320 static void
9321 arm_show_unwind_secure_frames (struct ui_file *file, int from_tty,
9322 struct cmd_list_element *c, const char *value)
9324 gdb_printf (file,
9325 _("Usage of non-secure to secure exception stack unwinding is %s.\n"),
9326 arm_unwind_secure_frames ? "on" : "off");
9329 /* If the user changes the register disassembly style used for info
9330 register and other commands, we have to also switch the style used
9331 in opcodes for disassembly output. This function is run in the "set
9332 arm disassembly" command, and does that. */
9334 static void
9335 set_disassembly_style_sfunc (const char *args, int from_tty,
9336 struct cmd_list_element *c)
9338 /* Convert the short style name into the long style name (eg, reg-names-*)
9339 before calling the generic set_disassembler_options() function. */
9340 std::string long_name = std::string ("reg-names-") + disassembly_style;
9341 set_disassembler_options (&long_name[0]);
9344 static void
9345 show_disassembly_style_sfunc (struct ui_file *file, int from_tty,
9346 struct cmd_list_element *c, const char *value)
9348 struct gdbarch *gdbarch = get_current_arch ();
9349 char *options = get_disassembler_options (gdbarch);
9350 const char *style = "";
9351 int len = 0;
9352 const char *opt;
9354 FOR_EACH_DISASSEMBLER_OPTION (opt, options)
9355 if (startswith (opt, "reg-names-"))
9357 style = &opt[strlen ("reg-names-")];
9358 len = strcspn (style, ",");
9361 gdb_printf (file, "The disassembly style is \"%.*s\".\n", len, style);
9364 /* Return the ARM register name corresponding to register I. */
9365 static const char *
9366 arm_register_name (struct gdbarch *gdbarch, int i)
9368 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
9370 if (is_s_pseudo (gdbarch, i))
9372 static const char *const s_pseudo_names[] = {
9373 "s0", "s1", "s2", "s3", "s4", "s5", "s6", "s7",
9374 "s8", "s9", "s10", "s11", "s12", "s13", "s14", "s15",
9375 "s16", "s17", "s18", "s19", "s20", "s21", "s22", "s23",
9376 "s24", "s25", "s26", "s27", "s28", "s29", "s30", "s31",
9379 return s_pseudo_names[i - tdep->s_pseudo_base];
9382 if (is_q_pseudo (gdbarch, i))
9384 static const char *const q_pseudo_names[] = {
9385 "q0", "q1", "q2", "q3", "q4", "q5", "q6", "q7",
9386 "q8", "q9", "q10", "q11", "q12", "q13", "q14", "q15",
9389 return q_pseudo_names[i - tdep->q_pseudo_base];
9392 if (is_mve_pseudo (gdbarch, i))
9393 return "p0";
9395 /* RA_AUTH_CODE is used for unwinding only. Do not assign it a name. */
9396 if (is_pacbti_pseudo (gdbarch, i))
9397 return "";
9399 if (i >= ARRAY_SIZE (arm_register_names))
9400 /* These registers are only supported on targets which supply
9401 an XML description. */
9402 return "";
9404 /* Non-pseudo registers. */
9405 return arm_register_names[i];
9408 /* Test whether the coff symbol specific value corresponds to a Thumb
9409 function. */
9411 static int
9412 coff_sym_is_thumb (int val)
9414 return (val == C_THUMBEXT
9415 || val == C_THUMBSTAT
9416 || val == C_THUMBEXTFUNC
9417 || val == C_THUMBSTATFUNC
9418 || val == C_THUMBLABEL);
9421 /* arm_coff_make_msymbol_special()
9422 arm_elf_make_msymbol_special()
9424 These functions test whether the COFF or ELF symbol corresponds to
9425 an address in thumb code, and set a "special" bit in a minimal
9426 symbol to indicate that it does. */
9428 static void
9429 arm_elf_make_msymbol_special(asymbol *sym, struct minimal_symbol *msym)
9431 elf_symbol_type *elfsym = (elf_symbol_type *) sym;
9433 if (ARM_GET_SYM_BRANCH_TYPE (elfsym->internal_elf_sym.st_target_internal)
9434 == ST_BRANCH_TO_THUMB)
9435 MSYMBOL_SET_SPECIAL (msym);
9438 static void
9439 arm_coff_make_msymbol_special(int val, struct minimal_symbol *msym)
9441 if (coff_sym_is_thumb (val))
9442 MSYMBOL_SET_SPECIAL (msym);
9445 static void
9446 arm_record_special_symbol (struct gdbarch *gdbarch, struct objfile *objfile,
9447 asymbol *sym)
9449 const char *name = bfd_asymbol_name (sym);
9450 struct arm_per_bfd *data;
9451 struct arm_mapping_symbol new_map_sym;
9453 gdb_assert (name[0] == '$');
9454 if (name[1] != 'a' && name[1] != 't' && name[1] != 'd')
9455 return;
9457 data = arm_bfd_data_key.get (objfile->obfd.get ());
9458 if (data == NULL)
9459 data = arm_bfd_data_key.emplace (objfile->obfd.get (),
9460 objfile->obfd->section_count);
9461 arm_mapping_symbol_vec &map
9462 = data->section_maps[bfd_asymbol_section (sym)->index];
9464 new_map_sym.value = sym->value;
9465 new_map_sym.type = name[1];
9467 /* Insert at the end, the vector will be sorted on first use. */
9468 map.push_back (new_map_sym);
9471 static void
9472 arm_write_pc (struct regcache *regcache, CORE_ADDR pc)
9474 struct gdbarch *gdbarch = regcache->arch ();
9475 regcache_cooked_write_unsigned (regcache, ARM_PC_REGNUM, pc);
9477 /* If necessary, set the T bit. */
9478 if (arm_apcs_32)
9480 ULONGEST val, t_bit;
9481 regcache_cooked_read_unsigned (regcache, ARM_PS_REGNUM, &val);
9482 t_bit = arm_psr_thumb_bit (gdbarch);
9483 if (arm_pc_is_thumb (gdbarch, pc))
9484 regcache_cooked_write_unsigned (regcache, ARM_PS_REGNUM,
9485 val | t_bit);
9486 else
9487 regcache_cooked_write_unsigned (regcache, ARM_PS_REGNUM,
9488 val & ~t_bit);
9492 /* Read the contents of a NEON quad register, by reading from two
9493 double registers. This is used to implement the quad pseudo
9494 registers, and for argument passing in case the quad registers are
9495 missing; vectors are passed in quad registers when using the VFP
9496 ABI, even if a NEON unit is not present. REGNUM is the index of
9497 the quad register, in [0, 15]. */
9499 static enum register_status
9500 arm_neon_quad_read (struct gdbarch *gdbarch, readable_regcache *regcache,
9501 int regnum, gdb_byte *buf)
9503 char name_buf[4];
9504 gdb_byte reg_buf[8];
9505 int offset, double_regnum;
9506 enum register_status status;
9508 xsnprintf (name_buf, sizeof (name_buf), "d%d", regnum << 1);
9509 double_regnum = user_reg_map_name_to_regnum (gdbarch, name_buf,
9510 strlen (name_buf));
9512 /* d0 is always the least significant half of q0. */
9513 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
9514 offset = 8;
9515 else
9516 offset = 0;
9518 status = regcache->raw_read (double_regnum, reg_buf);
9519 if (status != REG_VALID)
9520 return status;
9521 memcpy (buf + offset, reg_buf, 8);
9523 offset = 8 - offset;
9524 status = regcache->raw_read (double_regnum + 1, reg_buf);
9525 if (status != REG_VALID)
9526 return status;
9527 memcpy (buf + offset, reg_buf, 8);
9529 return REG_VALID;
9532 /* Read the contents of the MVE pseudo register REGNUM and store it
9533 in BUF. */
9535 static enum register_status
9536 arm_mve_pseudo_read (struct gdbarch *gdbarch, readable_regcache *regcache,
9537 int regnum, gdb_byte *buf)
9539 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
9541 /* P0 is the first 16 bits of VPR. */
9542 return regcache->raw_read_part (tdep->mve_vpr_regnum, 0, 2, buf);
9545 static enum register_status
9546 arm_pseudo_read (struct gdbarch *gdbarch, readable_regcache *regcache,
9547 int regnum, gdb_byte *buf)
9549 const int num_regs = gdbarch_num_regs (gdbarch);
9550 char name_buf[4];
9551 gdb_byte reg_buf[8];
9552 int offset, double_regnum;
9553 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
9555 gdb_assert (regnum >= num_regs);
9557 if (is_q_pseudo (gdbarch, regnum))
9559 /* Quad-precision register. */
9560 return arm_neon_quad_read (gdbarch, regcache,
9561 regnum - tdep->q_pseudo_base, buf);
9563 else if (is_mve_pseudo (gdbarch, regnum))
9564 return arm_mve_pseudo_read (gdbarch, regcache, regnum, buf);
9565 else
9567 enum register_status status;
9569 regnum -= tdep->s_pseudo_base;
9570 /* Single-precision register. */
9571 gdb_assert (regnum < 32);
9573 /* s0 is always the least significant half of d0. */
9574 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
9575 offset = (regnum & 1) ? 0 : 4;
9576 else
9577 offset = (regnum & 1) ? 4 : 0;
9579 xsnprintf (name_buf, sizeof (name_buf), "d%d", regnum >> 1);
9580 double_regnum = user_reg_map_name_to_regnum (gdbarch, name_buf,
9581 strlen (name_buf));
9583 status = regcache->raw_read (double_regnum, reg_buf);
9584 if (status == REG_VALID)
9585 memcpy (buf, reg_buf + offset, 4);
9586 return status;
9590 /* Store the contents of BUF to a NEON quad register, by writing to
9591 two double registers. This is used to implement the quad pseudo
9592 registers, and for argument passing in case the quad registers are
9593 missing; vectors are passed in quad registers when using the VFP
9594 ABI, even if a NEON unit is not present. REGNUM is the index
9595 of the quad register, in [0, 15]. */
9597 static void
9598 arm_neon_quad_write (struct gdbarch *gdbarch, struct regcache *regcache,
9599 int regnum, const gdb_byte *buf)
9601 char name_buf[4];
9602 int offset, double_regnum;
9604 xsnprintf (name_buf, sizeof (name_buf), "d%d", regnum << 1);
9605 double_regnum = user_reg_map_name_to_regnum (gdbarch, name_buf,
9606 strlen (name_buf));
9608 /* d0 is always the least significant half of q0. */
9609 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
9610 offset = 8;
9611 else
9612 offset = 0;
9614 regcache->raw_write (double_regnum, buf + offset);
9615 offset = 8 - offset;
9616 regcache->raw_write (double_regnum + 1, buf + offset);
9619 /* Store the contents of BUF to the MVE pseudo register REGNUM. */
9621 static void
9622 arm_mve_pseudo_write (struct gdbarch *gdbarch, struct regcache *regcache,
9623 int regnum, const gdb_byte *buf)
9625 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
9627 /* P0 is the first 16 bits of VPR. */
9628 regcache->raw_write_part (tdep->mve_vpr_regnum, 0, 2, buf);
9631 static void
9632 arm_pseudo_write (struct gdbarch *gdbarch, struct regcache *regcache,
9633 int regnum, const gdb_byte *buf)
9635 const int num_regs = gdbarch_num_regs (gdbarch);
9636 char name_buf[4];
9637 gdb_byte reg_buf[8];
9638 int offset, double_regnum;
9639 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
9641 gdb_assert (regnum >= num_regs);
9643 if (is_q_pseudo (gdbarch, regnum))
9645 /* Quad-precision register. */
9646 arm_neon_quad_write (gdbarch, regcache,
9647 regnum - tdep->q_pseudo_base, buf);
9649 else if (is_mve_pseudo (gdbarch, regnum))
9650 arm_mve_pseudo_write (gdbarch, regcache, regnum, buf);
9651 else
9653 regnum -= tdep->s_pseudo_base;
9654 /* Single-precision register. */
9655 gdb_assert (regnum < 32);
9657 /* s0 is always the least significant half of d0. */
9658 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
9659 offset = (regnum & 1) ? 0 : 4;
9660 else
9661 offset = (regnum & 1) ? 4 : 0;
9663 xsnprintf (name_buf, sizeof (name_buf), "d%d", regnum >> 1);
9664 double_regnum = user_reg_map_name_to_regnum (gdbarch, name_buf,
9665 strlen (name_buf));
9667 regcache->raw_read (double_regnum, reg_buf);
9668 memcpy (reg_buf + offset, buf, 4);
9669 regcache->raw_write (double_regnum, reg_buf);
9673 static struct value *
9674 value_of_arm_user_reg (struct frame_info *frame, const void *baton)
9676 const int *reg_p = (const int *) baton;
9677 return value_of_register (*reg_p, frame);
9680 static enum gdb_osabi
9681 arm_elf_osabi_sniffer (bfd *abfd)
9683 unsigned int elfosabi;
9684 enum gdb_osabi osabi = GDB_OSABI_UNKNOWN;
9686 elfosabi = elf_elfheader (abfd)->e_ident[EI_OSABI];
9688 if (elfosabi == ELFOSABI_ARM)
9689 /* GNU tools use this value. Check note sections in this case,
9690 as well. */
9692 for (asection *sect : gdb_bfd_sections (abfd))
9693 generic_elf_osabi_sniff_abi_tag_sections (abfd, sect, &osabi);
9696 /* Anything else will be handled by the generic ELF sniffer. */
9697 return osabi;
9700 static int
9701 arm_register_reggroup_p (struct gdbarch *gdbarch, int regnum,
9702 const struct reggroup *group)
9704 /* FPS register's type is INT, but belongs to float_reggroup. Beside
9705 this, FPS register belongs to save_regroup, restore_reggroup, and
9706 all_reggroup, of course. */
9707 if (regnum == ARM_FPS_REGNUM)
9708 return (group == float_reggroup
9709 || group == save_reggroup
9710 || group == restore_reggroup
9711 || group == all_reggroup);
9712 else
9713 return default_register_reggroup_p (gdbarch, regnum, group);
9716 /* For backward-compatibility we allow two 'g' packet lengths with
9717 the remote protocol depending on whether FPA registers are
9718 supplied. M-profile targets do not have FPA registers, but some
9719 stubs already exist in the wild which use a 'g' packet which
9720 supplies them albeit with dummy values. The packet format which
9721 includes FPA registers should be considered deprecated for
9722 M-profile targets. */
9724 static void
9725 arm_register_g_packet_guesses (struct gdbarch *gdbarch)
9727 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
9729 if (tdep->is_m)
9731 const target_desc *tdesc;
9733 /* If we know from the executable this is an M-profile target,
9734 cater for remote targets whose register set layout is the
9735 same as the FPA layout. */
9736 tdesc = arm_read_mprofile_description (ARM_M_TYPE_WITH_FPA);
9737 register_remote_g_packet_guess (gdbarch,
9738 ARM_CORE_REGS_SIZE + ARM_FP_REGS_SIZE,
9739 tdesc);
9741 /* The regular M-profile layout. */
9742 tdesc = arm_read_mprofile_description (ARM_M_TYPE_M_PROFILE);
9743 register_remote_g_packet_guess (gdbarch, ARM_CORE_REGS_SIZE,
9744 tdesc);
9746 /* M-profile plus M4F VFP. */
9747 tdesc = arm_read_mprofile_description (ARM_M_TYPE_VFP_D16);
9748 register_remote_g_packet_guess (gdbarch,
9749 ARM_CORE_REGS_SIZE + ARM_VFP2_REGS_SIZE,
9750 tdesc);
9751 /* M-profile plus MVE. */
9752 tdesc = arm_read_mprofile_description (ARM_M_TYPE_MVE);
9753 register_remote_g_packet_guess (gdbarch, ARM_CORE_REGS_SIZE
9754 + ARM_VFP2_REGS_SIZE
9755 + ARM_INT_REGISTER_SIZE, tdesc);
9757 /* M-profile system (stack pointers). */
9758 tdesc = arm_read_mprofile_description (ARM_M_TYPE_SYSTEM);
9759 register_remote_g_packet_guess (gdbarch, 2 * ARM_INT_REGISTER_SIZE, tdesc);
9762 /* Otherwise we don't have a useful guess. */
9765 /* Implement the code_of_frame_writable gdbarch method. */
9767 static int
9768 arm_code_of_frame_writable (struct gdbarch *gdbarch, struct frame_info *frame)
9770 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
9772 if (tdep->is_m && get_frame_type (frame) == SIGTRAMP_FRAME)
9774 /* M-profile exception frames return to some magic PCs, where
9775 isn't writable at all. */
9776 return 0;
9778 else
9779 return 1;
9782 /* Implement gdbarch_gnu_triplet_regexp. If the arch name is arm then allow it
9783 to be postfixed by a version (eg armv7hl). */
9785 static const char *
9786 arm_gnu_triplet_regexp (struct gdbarch *gdbarch)
9788 if (strcmp (gdbarch_bfd_arch_info (gdbarch)->arch_name, "arm") == 0)
9789 return "arm(v[^- ]*)?";
9790 return gdbarch_bfd_arch_info (gdbarch)->arch_name;
9793 /* Implement the "get_pc_address_flags" gdbarch method. */
9795 static std::string
9796 arm_get_pc_address_flags (frame_info *frame, CORE_ADDR pc)
9798 if (get_frame_pc_masked (frame))
9799 return "PAC";
9801 return "";
9804 /* Initialize the current architecture based on INFO. If possible,
9805 re-use an architecture from ARCHES, which is a list of
9806 architectures already created during this debugging session.
9808 Called e.g. at program startup, when reading a core file, and when
9809 reading a binary file. */
9811 static struct gdbarch *
9812 arm_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
9814 struct gdbarch *gdbarch;
9815 struct gdbarch_list *best_arch;
9816 enum arm_abi_kind arm_abi = arm_abi_global;
9817 enum arm_float_model fp_model = arm_fp_model;
9818 tdesc_arch_data_up tdesc_data;
9819 int i;
9820 bool is_m = false;
9821 bool have_sec_ext = false;
9822 int vfp_register_count = 0;
9823 bool have_s_pseudos = false, have_q_pseudos = false;
9824 bool have_wmmx_registers = false;
9825 bool have_neon = false;
9826 bool have_fpa_registers = true;
9827 const struct target_desc *tdesc = info.target_desc;
9828 bool have_vfp = false;
9829 bool have_mve = false;
9830 bool have_pacbti = false;
9831 int mve_vpr_regnum = -1;
9832 int register_count = ARM_NUM_REGS;
9833 bool have_m_profile_msp = false;
9834 int m_profile_msp_regnum = -1;
9835 int m_profile_psp_regnum = -1;
9836 int m_profile_msp_ns_regnum = -1;
9837 int m_profile_psp_ns_regnum = -1;
9838 int m_profile_msp_s_regnum = -1;
9839 int m_profile_psp_s_regnum = -1;
9840 int tls_regnum = 0;
9842 /* If we have an object to base this architecture on, try to determine
9843 its ABI. */
9845 if (arm_abi == ARM_ABI_AUTO && info.abfd != NULL)
9847 int ei_osabi, e_flags;
9849 switch (bfd_get_flavour (info.abfd))
9851 case bfd_target_coff_flavour:
9852 /* Assume it's an old APCS-style ABI. */
9853 /* XXX WinCE? */
9854 arm_abi = ARM_ABI_APCS;
9855 break;
9857 case bfd_target_elf_flavour:
9858 ei_osabi = elf_elfheader (info.abfd)->e_ident[EI_OSABI];
9859 e_flags = elf_elfheader (info.abfd)->e_flags;
9861 if (ei_osabi == ELFOSABI_ARM)
9863 /* GNU tools used to use this value, but do not for EABI
9864 objects. There's nowhere to tag an EABI version
9865 anyway, so assume APCS. */
9866 arm_abi = ARM_ABI_APCS;
9868 else if (ei_osabi == ELFOSABI_NONE || ei_osabi == ELFOSABI_GNU)
9870 int eabi_ver = EF_ARM_EABI_VERSION (e_flags);
9872 switch (eabi_ver)
9874 case EF_ARM_EABI_UNKNOWN:
9875 /* Assume GNU tools. */
9876 arm_abi = ARM_ABI_APCS;
9877 break;
9879 case EF_ARM_EABI_VER4:
9880 case EF_ARM_EABI_VER5:
9881 arm_abi = ARM_ABI_AAPCS;
9882 /* EABI binaries default to VFP float ordering.
9883 They may also contain build attributes that can
9884 be used to identify if the VFP argument-passing
9885 ABI is in use. */
9886 if (fp_model == ARM_FLOAT_AUTO)
9888 #ifdef HAVE_ELF
9889 switch (bfd_elf_get_obj_attr_int (info.abfd,
9890 OBJ_ATTR_PROC,
9891 Tag_ABI_VFP_args))
9893 case AEABI_VFP_args_base:
9894 /* "The user intended FP parameter/result
9895 passing to conform to AAPCS, base
9896 variant". */
9897 fp_model = ARM_FLOAT_SOFT_VFP;
9898 break;
9899 case AEABI_VFP_args_vfp:
9900 /* "The user intended FP parameter/result
9901 passing to conform to AAPCS, VFP
9902 variant". */
9903 fp_model = ARM_FLOAT_VFP;
9904 break;
9905 case AEABI_VFP_args_toolchain:
9906 /* "The user intended FP parameter/result
9907 passing to conform to tool chain-specific
9908 conventions" - we don't know any such
9909 conventions, so leave it as "auto". */
9910 break;
9911 case AEABI_VFP_args_compatible:
9912 /* "Code is compatible with both the base
9913 and VFP variants; the user did not permit
9914 non-variadic functions to pass FP
9915 parameters/results" - leave it as
9916 "auto". */
9917 break;
9918 default:
9919 /* Attribute value not mentioned in the
9920 November 2012 ABI, so leave it as
9921 "auto". */
9922 break;
9924 #else
9925 fp_model = ARM_FLOAT_SOFT_VFP;
9926 #endif
9928 break;
9930 default:
9931 /* Leave it as "auto". */
9932 warning (_("unknown ARM EABI version 0x%x"), eabi_ver);
9933 break;
9936 #ifdef HAVE_ELF
9937 /* Detect M-profile programs. This only works if the
9938 executable file includes build attributes; GCC does
9939 copy them to the executable, but e.g. RealView does
9940 not. */
9941 int attr_arch
9942 = bfd_elf_get_obj_attr_int (info.abfd, OBJ_ATTR_PROC,
9943 Tag_CPU_arch);
9944 int attr_profile
9945 = bfd_elf_get_obj_attr_int (info.abfd, OBJ_ATTR_PROC,
9946 Tag_CPU_arch_profile);
9948 /* GCC specifies the profile for v6-M; RealView only
9949 specifies the profile for architectures starting with
9950 V7 (as opposed to architectures with a tag
9951 numerically greater than TAG_CPU_ARCH_V7). */
9952 if (!tdesc_has_registers (tdesc)
9953 && (attr_arch == TAG_CPU_ARCH_V6_M
9954 || attr_arch == TAG_CPU_ARCH_V6S_M
9955 || attr_arch == TAG_CPU_ARCH_V7E_M
9956 || attr_arch == TAG_CPU_ARCH_V8M_BASE
9957 || attr_arch == TAG_CPU_ARCH_V8M_MAIN
9958 || attr_arch == TAG_CPU_ARCH_V8_1M_MAIN
9959 || attr_profile == 'M'))
9960 is_m = true;
9962 /* Look for attributes that indicate support for ARMv8.1-m
9963 PACBTI. */
9964 if (!tdesc_has_registers (tdesc) && is_m)
9966 int attr_pac_extension
9967 = bfd_elf_get_obj_attr_int (info.abfd, OBJ_ATTR_PROC,
9968 Tag_PAC_extension);
9970 int attr_bti_extension
9971 = bfd_elf_get_obj_attr_int (info.abfd, OBJ_ATTR_PROC,
9972 Tag_BTI_extension);
9974 int attr_pacret_use
9975 = bfd_elf_get_obj_attr_int (info.abfd, OBJ_ATTR_PROC,
9976 Tag_PACRET_use);
9978 int attr_bti_use
9979 = bfd_elf_get_obj_attr_int (info.abfd, OBJ_ATTR_PROC,
9980 Tag_BTI_use);
9982 if (attr_pac_extension != 0 || attr_bti_extension != 0
9983 || attr_pacret_use != 0 || attr_bti_use != 0)
9984 have_pacbti = true;
9986 #endif
9989 if (fp_model == ARM_FLOAT_AUTO)
9991 switch (e_flags & (EF_ARM_SOFT_FLOAT | EF_ARM_VFP_FLOAT))
9993 case 0:
9994 /* Leave it as "auto". Strictly speaking this case
9995 means FPA, but almost nobody uses that now, and
9996 many toolchains fail to set the appropriate bits
9997 for the floating-point model they use. */
9998 break;
9999 case EF_ARM_SOFT_FLOAT:
10000 fp_model = ARM_FLOAT_SOFT_FPA;
10001 break;
10002 case EF_ARM_VFP_FLOAT:
10003 fp_model = ARM_FLOAT_VFP;
10004 break;
10005 case EF_ARM_SOFT_FLOAT | EF_ARM_VFP_FLOAT:
10006 fp_model = ARM_FLOAT_SOFT_VFP;
10007 break;
10011 if (e_flags & EF_ARM_BE8)
10012 info.byte_order_for_code = BFD_ENDIAN_LITTLE;
10014 break;
10016 default:
10017 /* Leave it as "auto". */
10018 break;
10022 /* Check any target description for validity. */
10023 if (tdesc_has_registers (tdesc))
10025 /* For most registers we require GDB's default names; but also allow
10026 the numeric names for sp / lr / pc, as a convenience. */
10027 static const char *const arm_sp_names[] = { "r13", "sp", NULL };
10028 static const char *const arm_lr_names[] = { "r14", "lr", NULL };
10029 static const char *const arm_pc_names[] = { "r15", "pc", NULL };
10031 const struct tdesc_feature *feature;
10032 int valid_p;
10034 feature = tdesc_find_feature (tdesc,
10035 "org.gnu.gdb.arm.core");
10036 if (feature == NULL)
10038 feature = tdesc_find_feature (tdesc,
10039 "org.gnu.gdb.arm.m-profile");
10040 if (feature == NULL)
10041 return NULL;
10042 else
10043 is_m = true;
10046 tdesc_data = tdesc_data_alloc ();
10048 valid_p = 1;
10049 for (i = 0; i < ARM_SP_REGNUM; i++)
10050 valid_p &= tdesc_numbered_register (feature, tdesc_data.get (), i,
10051 arm_register_names[i]);
10052 valid_p &= tdesc_numbered_register_choices (feature, tdesc_data.get (),
10053 ARM_SP_REGNUM,
10054 arm_sp_names);
10055 valid_p &= tdesc_numbered_register_choices (feature, tdesc_data.get (),
10056 ARM_LR_REGNUM,
10057 arm_lr_names);
10058 valid_p &= tdesc_numbered_register_choices (feature, tdesc_data.get (),
10059 ARM_PC_REGNUM,
10060 arm_pc_names);
10061 if (is_m)
10062 valid_p &= tdesc_numbered_register (feature, tdesc_data.get (),
10063 ARM_PS_REGNUM, "xpsr");
10064 else
10065 valid_p &= tdesc_numbered_register (feature, tdesc_data.get (),
10066 ARM_PS_REGNUM, "cpsr");
10068 if (!valid_p)
10069 return NULL;
10071 if (is_m)
10073 feature = tdesc_find_feature (tdesc,
10074 "org.gnu.gdb.arm.m-system");
10075 if (feature != nullptr)
10077 /* MSP */
10078 valid_p &= tdesc_numbered_register (feature, tdesc_data.get (),
10079 register_count, "msp");
10080 if (!valid_p)
10082 warning (_("M-profile m-system feature is missing required register msp."));
10083 return nullptr;
10085 have_m_profile_msp = true;
10086 m_profile_msp_regnum = register_count++;
10088 /* PSP */
10089 valid_p &= tdesc_numbered_register (feature, tdesc_data.get (),
10090 register_count, "psp");
10091 if (!valid_p)
10093 warning (_("M-profile m-system feature is missing required register psp."));
10094 return nullptr;
10096 m_profile_psp_regnum = register_count++;
10100 feature = tdesc_find_feature (tdesc,
10101 "org.gnu.gdb.arm.fpa");
10102 if (feature != NULL)
10104 valid_p = 1;
10105 for (i = ARM_F0_REGNUM; i <= ARM_FPS_REGNUM; i++)
10106 valid_p &= tdesc_numbered_register (feature, tdesc_data.get (), i,
10107 arm_register_names[i]);
10108 if (!valid_p)
10109 return NULL;
10111 else
10112 have_fpa_registers = false;
10114 feature = tdesc_find_feature (tdesc,
10115 "org.gnu.gdb.xscale.iwmmxt");
10116 if (feature != NULL)
10118 static const char *const iwmmxt_names[] = {
10119 "wR0", "wR1", "wR2", "wR3", "wR4", "wR5", "wR6", "wR7",
10120 "wR8", "wR9", "wR10", "wR11", "wR12", "wR13", "wR14", "wR15",
10121 "wCID", "wCon", "wCSSF", "wCASF", "", "", "", "",
10122 "wCGR0", "wCGR1", "wCGR2", "wCGR3", "", "", "", "",
10125 valid_p = 1;
10126 for (i = ARM_WR0_REGNUM; i <= ARM_WR15_REGNUM; i++)
10127 valid_p
10128 &= tdesc_numbered_register (feature, tdesc_data.get (), i,
10129 iwmmxt_names[i - ARM_WR0_REGNUM]);
10131 /* Check for the control registers, but do not fail if they
10132 are missing. */
10133 for (i = ARM_WC0_REGNUM; i <= ARM_WCASF_REGNUM; i++)
10134 tdesc_numbered_register (feature, tdesc_data.get (), i,
10135 iwmmxt_names[i - ARM_WR0_REGNUM]);
10137 for (i = ARM_WCGR0_REGNUM; i <= ARM_WCGR3_REGNUM; i++)
10138 valid_p
10139 &= tdesc_numbered_register (feature, tdesc_data.get (), i,
10140 iwmmxt_names[i - ARM_WR0_REGNUM]);
10142 if (!valid_p)
10143 return NULL;
10145 have_wmmx_registers = true;
10148 /* If we have a VFP unit, check whether the single precision registers
10149 are present. If not, then we will synthesize them as pseudo
10150 registers. */
10151 feature = tdesc_find_feature (tdesc,
10152 "org.gnu.gdb.arm.vfp");
10153 if (feature != NULL)
10155 static const char *const vfp_double_names[] = {
10156 "d0", "d1", "d2", "d3", "d4", "d5", "d6", "d7",
10157 "d8", "d9", "d10", "d11", "d12", "d13", "d14", "d15",
10158 "d16", "d17", "d18", "d19", "d20", "d21", "d22", "d23",
10159 "d24", "d25", "d26", "d27", "d28", "d29", "d30", "d31",
10162 /* Require the double precision registers. There must be either
10163 16 or 32. */
10164 valid_p = 1;
10165 for (i = 0; i < 32; i++)
10167 valid_p &= tdesc_numbered_register (feature, tdesc_data.get (),
10168 ARM_D0_REGNUM + i,
10169 vfp_double_names[i]);
10170 if (!valid_p)
10171 break;
10173 if (!valid_p && i == 16)
10174 valid_p = 1;
10176 /* Also require FPSCR. */
10177 valid_p &= tdesc_numbered_register (feature, tdesc_data.get (),
10178 ARM_FPSCR_REGNUM, "fpscr");
10179 if (!valid_p)
10180 return NULL;
10182 have_vfp = true;
10184 if (tdesc_unnumbered_register (feature, "s0") == 0)
10185 have_s_pseudos = true;
10187 vfp_register_count = i;
10189 /* If we have VFP, also check for NEON. The architecture allows
10190 NEON without VFP (integer vector operations only), but GDB
10191 does not support that. */
10192 feature = tdesc_find_feature (tdesc,
10193 "org.gnu.gdb.arm.neon");
10194 if (feature != NULL)
10196 /* NEON requires 32 double-precision registers. */
10197 if (i != 32)
10198 return NULL;
10200 /* If there are quad registers defined by the stub, use
10201 their type; otherwise (normally) provide them with
10202 the default type. */
10203 if (tdesc_unnumbered_register (feature, "q0") == 0)
10204 have_q_pseudos = true;
10208 /* Check for the TLS register feature. */
10209 feature = tdesc_find_feature (tdesc, "org.gnu.gdb.arm.tls");
10210 if (feature != nullptr)
10212 valid_p &= tdesc_numbered_register (feature, tdesc_data.get (),
10213 register_count, "tpidruro");
10214 if (!valid_p)
10215 return nullptr;
10217 tls_regnum = register_count;
10218 register_count++;
10221 /* Check for MVE after all the checks for GPR's, VFP and Neon.
10222 MVE (Helium) is an M-profile extension. */
10223 if (is_m)
10225 /* Do we have the MVE feature? */
10226 feature = tdesc_find_feature (tdesc,"org.gnu.gdb.arm.m-profile-mve");
10228 if (feature != nullptr)
10230 /* If we have MVE, we must always have the VPR register. */
10231 valid_p &= tdesc_numbered_register (feature, tdesc_data.get (),
10232 register_count, "vpr");
10233 if (!valid_p)
10235 warning (_("MVE feature is missing required register vpr."));
10236 return nullptr;
10239 have_mve = true;
10240 mve_vpr_regnum = register_count;
10241 register_count++;
10243 /* We can't have Q pseudo registers available here, as that
10244 would mean we have NEON features, and that is only available
10245 on A and R profiles. */
10246 gdb_assert (!have_q_pseudos);
10248 /* Given we have a M-profile target description, if MVE is
10249 enabled and there are VFP registers, we should have Q
10250 pseudo registers (Q0 ~ Q7). */
10251 if (have_vfp)
10252 have_q_pseudos = true;
10255 /* Do we have the ARMv8.1-m PACBTI feature? */
10256 feature = tdesc_find_feature (tdesc,
10257 "org.gnu.gdb.arm.m-profile-pacbti");
10258 if (feature != nullptr)
10260 /* By advertising this feature, the target acknowledges the
10261 presence of the ARMv8.1-m PACBTI extensions.
10263 We don't care for any particular registers in this group, so
10264 the target is free to include whatever it deems appropriate.
10266 The expectation is for this feature to include the PAC
10267 keys. */
10268 have_pacbti = true;
10271 /* Do we have the Security extension? */
10272 feature = tdesc_find_feature (tdesc,
10273 "org.gnu.gdb.arm.secext");
10274 if (feature != nullptr)
10276 /* Secure/Non-secure stack pointers. */
10277 /* MSP_NS */
10278 valid_p &= tdesc_numbered_register (feature, tdesc_data.get (),
10279 register_count, "msp_ns");
10280 if (!valid_p)
10282 warning (_("M-profile secext feature is missing required register msp_ns."));
10283 return nullptr;
10285 m_profile_msp_ns_regnum = register_count++;
10287 /* PSP_NS */
10288 valid_p &= tdesc_numbered_register (feature, tdesc_data.get (),
10289 register_count, "psp_ns");
10290 if (!valid_p)
10292 warning (_("M-profile secext feature is missing required register psp_ns."));
10293 return nullptr;
10295 m_profile_psp_ns_regnum = register_count++;
10297 /* MSP_S */
10298 valid_p &= tdesc_numbered_register (feature, tdesc_data.get (),
10299 register_count, "msp_s");
10300 if (!valid_p)
10302 warning (_("M-profile secext feature is missing required register msp_s."));
10303 return nullptr;
10305 m_profile_msp_s_regnum = register_count++;
10307 /* PSP_S */
10308 valid_p &= tdesc_numbered_register (feature, tdesc_data.get (),
10309 register_count, "psp_s");
10310 if (!valid_p)
10312 warning (_("M-profile secext feature is missing required register psp_s."));
10313 return nullptr;
10315 m_profile_psp_s_regnum = register_count++;
10317 have_sec_ext = true;
10323 /* If there is already a candidate, use it. */
10324 for (best_arch = gdbarch_list_lookup_by_info (arches, &info);
10325 best_arch != NULL;
10326 best_arch = gdbarch_list_lookup_by_info (best_arch->next, &info))
10328 arm_gdbarch_tdep *tdep
10329 = gdbarch_tdep<arm_gdbarch_tdep> (best_arch->gdbarch);
10331 if (arm_abi != ARM_ABI_AUTO && arm_abi != tdep->arm_abi)
10332 continue;
10334 if (fp_model != ARM_FLOAT_AUTO && fp_model != tdep->fp_model)
10335 continue;
10337 /* There are various other properties in tdep that we do not
10338 need to check here: those derived from a target description,
10339 since gdbarches with a different target description are
10340 automatically disqualified. */
10342 /* Do check is_m, though, since it might come from the binary. */
10343 if (is_m != tdep->is_m)
10344 continue;
10346 /* Also check for ARMv8.1-m PACBTI support, since it might come from
10347 the binary. */
10348 if (have_pacbti != tdep->have_pacbti)
10349 continue;
10351 /* Found a match. */
10352 break;
10355 if (best_arch != NULL)
10356 return best_arch->gdbarch;
10358 arm_gdbarch_tdep *tdep = new arm_gdbarch_tdep;
10359 gdbarch = gdbarch_alloc (&info, tdep);
10361 /* Record additional information about the architecture we are defining.
10362 These are gdbarch discriminators, like the OSABI. */
10363 tdep->arm_abi = arm_abi;
10364 tdep->fp_model = fp_model;
10365 tdep->is_m = is_m;
10366 tdep->have_sec_ext = have_sec_ext;
10367 tdep->have_fpa_registers = have_fpa_registers;
10368 tdep->have_wmmx_registers = have_wmmx_registers;
10369 gdb_assert (vfp_register_count == 0
10370 || vfp_register_count == 16
10371 || vfp_register_count == 32);
10372 tdep->vfp_register_count = vfp_register_count;
10373 tdep->have_s_pseudos = have_s_pseudos;
10374 tdep->have_q_pseudos = have_q_pseudos;
10375 tdep->have_neon = have_neon;
10376 tdep->tls_regnum = tls_regnum;
10378 /* Adjust the MVE feature settings. */
10379 if (have_mve)
10381 tdep->have_mve = true;
10382 tdep->mve_vpr_regnum = mve_vpr_regnum;
10385 /* Adjust the PACBTI feature settings. */
10386 tdep->have_pacbti = have_pacbti;
10388 /* Adjust the M-profile stack pointers settings. */
10389 if (have_m_profile_msp)
10391 tdep->m_profile_msp_regnum = m_profile_msp_regnum;
10392 tdep->m_profile_psp_regnum = m_profile_psp_regnum;
10393 tdep->m_profile_msp_ns_regnum = m_profile_msp_ns_regnum;
10394 tdep->m_profile_psp_ns_regnum = m_profile_psp_ns_regnum;
10395 tdep->m_profile_msp_s_regnum = m_profile_msp_s_regnum;
10396 tdep->m_profile_psp_s_regnum = m_profile_psp_s_regnum;
10399 arm_register_g_packet_guesses (gdbarch);
10401 /* Breakpoints. */
10402 switch (info.byte_order_for_code)
10404 case BFD_ENDIAN_BIG:
10405 tdep->arm_breakpoint = arm_default_arm_be_breakpoint;
10406 tdep->arm_breakpoint_size = sizeof (arm_default_arm_be_breakpoint);
10407 tdep->thumb_breakpoint = arm_default_thumb_be_breakpoint;
10408 tdep->thumb_breakpoint_size = sizeof (arm_default_thumb_be_breakpoint);
10410 break;
10412 case BFD_ENDIAN_LITTLE:
10413 tdep->arm_breakpoint = arm_default_arm_le_breakpoint;
10414 tdep->arm_breakpoint_size = sizeof (arm_default_arm_le_breakpoint);
10415 tdep->thumb_breakpoint = arm_default_thumb_le_breakpoint;
10416 tdep->thumb_breakpoint_size = sizeof (arm_default_thumb_le_breakpoint);
10418 break;
10420 default:
10421 internal_error (__FILE__, __LINE__,
10422 _("arm_gdbarch_init: bad byte order for float format"));
10425 /* On ARM targets char defaults to unsigned. */
10426 set_gdbarch_char_signed (gdbarch, 0);
10428 /* wchar_t is unsigned under the AAPCS. */
10429 if (tdep->arm_abi == ARM_ABI_AAPCS)
10430 set_gdbarch_wchar_signed (gdbarch, 0);
10431 else
10432 set_gdbarch_wchar_signed (gdbarch, 1);
10434 /* Compute type alignment. */
10435 set_gdbarch_type_align (gdbarch, arm_type_align);
10437 /* Note: for displaced stepping, this includes the breakpoint, and one word
10438 of additional scratch space. This setting isn't used for anything beside
10439 displaced stepping at present. */
10440 set_gdbarch_max_insn_length (gdbarch, 4 * ARM_DISPLACED_MODIFIED_INSNS);
10442 /* This should be low enough for everything. */
10443 tdep->lowest_pc = 0x20;
10444 tdep->jb_pc = -1; /* Longjump support not enabled by default. */
10446 /* The default, for both APCS and AAPCS, is to return small
10447 structures in registers. */
10448 tdep->struct_return = reg_struct_return;
10450 set_gdbarch_push_dummy_call (gdbarch, arm_push_dummy_call);
10451 set_gdbarch_frame_align (gdbarch, arm_frame_align);
10453 if (is_m)
10454 set_gdbarch_code_of_frame_writable (gdbarch, arm_code_of_frame_writable);
10456 set_gdbarch_write_pc (gdbarch, arm_write_pc);
10458 frame_base_set_default (gdbarch, &arm_normal_base);
10460 /* Address manipulation. */
10461 set_gdbarch_addr_bits_remove (gdbarch, arm_addr_bits_remove);
10463 /* Advance PC across function entry code. */
10464 set_gdbarch_skip_prologue (gdbarch, arm_skip_prologue);
10466 /* Detect whether PC is at a point where the stack has been destroyed. */
10467 set_gdbarch_stack_frame_destroyed_p (gdbarch, arm_stack_frame_destroyed_p);
10469 /* Skip trampolines. */
10470 set_gdbarch_skip_trampoline_code (gdbarch, arm_skip_stub);
10472 /* The stack grows downward. */
10473 set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
10475 /* Breakpoint manipulation. */
10476 set_gdbarch_breakpoint_kind_from_pc (gdbarch, arm_breakpoint_kind_from_pc);
10477 set_gdbarch_sw_breakpoint_from_kind (gdbarch, arm_sw_breakpoint_from_kind);
10478 set_gdbarch_breakpoint_kind_from_current_state (gdbarch,
10479 arm_breakpoint_kind_from_current_state);
10481 /* Information about registers, etc. */
10482 set_gdbarch_sp_regnum (gdbarch, ARM_SP_REGNUM);
10483 set_gdbarch_pc_regnum (gdbarch, ARM_PC_REGNUM);
10484 set_gdbarch_num_regs (gdbarch, register_count);
10485 set_gdbarch_register_type (gdbarch, arm_register_type);
10486 set_gdbarch_register_reggroup_p (gdbarch, arm_register_reggroup_p);
10488 /* This "info float" is FPA-specific. Use the generic version if we
10489 do not have FPA. */
10490 if (tdep->have_fpa_registers)
10491 set_gdbarch_print_float_info (gdbarch, arm_print_float_info);
10493 /* Internal <-> external register number maps. */
10494 set_gdbarch_dwarf2_reg_to_regnum (gdbarch, arm_dwarf_reg_to_regnum);
10495 set_gdbarch_register_sim_regno (gdbarch, arm_register_sim_regno);
10497 set_gdbarch_register_name (gdbarch, arm_register_name);
10499 /* Returning results. */
10500 set_gdbarch_return_value (gdbarch, arm_return_value);
10502 /* Disassembly. */
10503 set_gdbarch_print_insn (gdbarch, gdb_print_insn_arm);
10505 /* Minsymbol frobbing. */
10506 set_gdbarch_elf_make_msymbol_special (gdbarch, arm_elf_make_msymbol_special);
10507 set_gdbarch_coff_make_msymbol_special (gdbarch,
10508 arm_coff_make_msymbol_special);
10509 set_gdbarch_record_special_symbol (gdbarch, arm_record_special_symbol);
10511 /* Thumb-2 IT block support. */
10512 set_gdbarch_adjust_breakpoint_address (gdbarch,
10513 arm_adjust_breakpoint_address);
10515 /* Virtual tables. */
10516 set_gdbarch_vbit_in_delta (gdbarch, 1);
10518 /* Hook in the ABI-specific overrides, if they have been registered. */
10519 gdbarch_init_osabi (info, gdbarch);
10521 dwarf2_frame_set_init_reg (gdbarch, arm_dwarf2_frame_init_reg);
10523 /* Add some default predicates. */
10524 if (is_m)
10525 frame_unwind_append_unwinder (gdbarch, &arm_m_exception_unwind);
10526 frame_unwind_append_unwinder (gdbarch, &arm_stub_unwind);
10527 dwarf2_append_unwinders (gdbarch);
10528 frame_unwind_append_unwinder (gdbarch, &arm_exidx_unwind);
10529 frame_unwind_append_unwinder (gdbarch, &arm_epilogue_frame_unwind);
10530 frame_unwind_append_unwinder (gdbarch, &arm_prologue_unwind);
10532 /* Now we have tuned the configuration, set a few final things,
10533 based on what the OS ABI has told us. */
10535 /* If the ABI is not otherwise marked, assume the old GNU APCS. EABI
10536 binaries are always marked. */
10537 if (tdep->arm_abi == ARM_ABI_AUTO)
10538 tdep->arm_abi = ARM_ABI_APCS;
10540 /* Watchpoints are not steppable. */
10541 set_gdbarch_have_nonsteppable_watchpoint (gdbarch, 1);
10543 /* We used to default to FPA for generic ARM, but almost nobody
10544 uses that now, and we now provide a way for the user to force
10545 the model. So default to the most useful variant. */
10546 if (tdep->fp_model == ARM_FLOAT_AUTO)
10547 tdep->fp_model = ARM_FLOAT_SOFT_FPA;
10549 if (tdep->jb_pc >= 0)
10550 set_gdbarch_get_longjmp_target (gdbarch, arm_get_longjmp_target);
10552 /* Floating point sizes and format. */
10553 set_gdbarch_float_format (gdbarch, floatformats_ieee_single);
10554 if (tdep->fp_model == ARM_FLOAT_SOFT_FPA || tdep->fp_model == ARM_FLOAT_FPA)
10556 set_gdbarch_double_format
10557 (gdbarch, floatformats_ieee_double_littlebyte_bigword);
10558 set_gdbarch_long_double_format
10559 (gdbarch, floatformats_ieee_double_littlebyte_bigword);
10561 else
10563 set_gdbarch_double_format (gdbarch, floatformats_ieee_double);
10564 set_gdbarch_long_double_format (gdbarch, floatformats_ieee_double);
10567 /* Hook used to decorate frames with signed return addresses, only available
10568 for ARMv8.1-m PACBTI. */
10569 if (is_m && have_pacbti)
10570 set_gdbarch_get_pc_address_flags (gdbarch, arm_get_pc_address_flags);
10572 if (tdesc_data != nullptr)
10574 set_tdesc_pseudo_register_name (gdbarch, arm_register_name);
10576 tdesc_use_registers (gdbarch, tdesc, std::move (tdesc_data));
10577 register_count = gdbarch_num_regs (gdbarch);
10579 /* Override tdesc_register_type to adjust the types of VFP
10580 registers for NEON. */
10581 set_gdbarch_register_type (gdbarch, arm_register_type);
10584 /* Initialize the pseudo register data. */
10585 int num_pseudos = 0;
10586 if (tdep->have_s_pseudos)
10588 /* VFP single precision pseudo registers (S0~S31). */
10589 tdep->s_pseudo_base = register_count;
10590 tdep->s_pseudo_count = 32;
10591 num_pseudos += tdep->s_pseudo_count;
10593 if (tdep->have_q_pseudos)
10595 /* NEON quad precision pseudo registers (Q0~Q15). */
10596 tdep->q_pseudo_base = register_count + num_pseudos;
10598 if (have_neon)
10599 tdep->q_pseudo_count = 16;
10600 else if (have_mve)
10601 tdep->q_pseudo_count = ARM_MVE_NUM_Q_REGS;
10603 num_pseudos += tdep->q_pseudo_count;
10607 /* Do we have any MVE pseudo registers? */
10608 if (have_mve)
10610 tdep->mve_pseudo_base = register_count + num_pseudos;
10611 tdep->mve_pseudo_count = 1;
10612 num_pseudos += tdep->mve_pseudo_count;
10615 /* Do we have any ARMv8.1-m PACBTI pseudo registers. */
10616 if (have_pacbti)
10618 tdep->pacbti_pseudo_base = register_count + num_pseudos;
10619 tdep->pacbti_pseudo_count = 1;
10620 num_pseudos += tdep->pacbti_pseudo_count;
10623 /* Set some pseudo register hooks, if we have pseudo registers. */
10624 if (tdep->have_s_pseudos || have_mve || have_pacbti)
10626 set_gdbarch_num_pseudo_regs (gdbarch, num_pseudos);
10627 set_gdbarch_pseudo_register_read (gdbarch, arm_pseudo_read);
10628 set_gdbarch_pseudo_register_write (gdbarch, arm_pseudo_write);
10631 /* Add standard register aliases. We add aliases even for those
10632 names which are used by the current architecture - it's simpler,
10633 and does no harm, since nothing ever lists user registers. */
10634 for (i = 0; i < ARRAY_SIZE (arm_register_aliases); i++)
10635 user_reg_add (gdbarch, arm_register_aliases[i].name,
10636 value_of_arm_user_reg, &arm_register_aliases[i].regnum);
10638 set_gdbarch_disassembler_options (gdbarch, &arm_disassembler_options);
10639 set_gdbarch_valid_disassembler_options (gdbarch, disassembler_options_arm ());
10641 set_gdbarch_gnu_triplet_regexp (gdbarch, arm_gnu_triplet_regexp);
10643 return gdbarch;
10646 static void
10647 arm_dump_tdep (struct gdbarch *gdbarch, struct ui_file *file)
10649 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
10651 if (tdep == NULL)
10652 return;
10654 gdb_printf (file, _("arm_dump_tdep: fp_model = %i\n"),
10655 (int) tdep->fp_model);
10656 gdb_printf (file, _("arm_dump_tdep: have_fpa_registers = %i\n"),
10657 (int) tdep->have_fpa_registers);
10658 gdb_printf (file, _("arm_dump_tdep: have_wmmx_registers = %i\n"),
10659 (int) tdep->have_wmmx_registers);
10660 gdb_printf (file, _("arm_dump_tdep: vfp_register_count = %i\n"),
10661 (int) tdep->vfp_register_count);
10662 gdb_printf (file, _("arm_dump_tdep: have_s_pseudos = %s\n"),
10663 tdep->have_s_pseudos? "true" : "false");
10664 gdb_printf (file, _("arm_dump_tdep: s_pseudo_base = %i\n"),
10665 (int) tdep->s_pseudo_base);
10666 gdb_printf (file, _("arm_dump_tdep: s_pseudo_count = %i\n"),
10667 (int) tdep->s_pseudo_count);
10668 gdb_printf (file, _("arm_dump_tdep: have_q_pseudos = %s\n"),
10669 tdep->have_q_pseudos? "true" : "false");
10670 gdb_printf (file, _("arm_dump_tdep: q_pseudo_base = %i\n"),
10671 (int) tdep->q_pseudo_base);
10672 gdb_printf (file, _("arm_dump_tdep: q_pseudo_count = %i\n"),
10673 (int) tdep->q_pseudo_count);
10674 gdb_printf (file, _("arm_dump_tdep: have_neon = %i\n"),
10675 (int) tdep->have_neon);
10676 gdb_printf (file, _("arm_dump_tdep: have_mve = %s\n"),
10677 tdep->have_mve? "yes" : "no");
10678 gdb_printf (file, _("arm_dump_tdep: mve_vpr_regnum = %i\n"),
10679 tdep->mve_vpr_regnum);
10680 gdb_printf (file, _("arm_dump_tdep: mve_pseudo_base = %i\n"),
10681 tdep->mve_pseudo_base);
10682 gdb_printf (file, _("arm_dump_tdep: mve_pseudo_count = %i\n"),
10683 tdep->mve_pseudo_count);
10684 gdb_printf (file, _("arm_dump_tdep: m_profile_msp_regnum = %i\n"),
10685 tdep->m_profile_msp_regnum);
10686 gdb_printf (file, _("arm_dump_tdep: m_profile_psp_regnum = %i\n"),
10687 tdep->m_profile_psp_regnum);
10688 gdb_printf (file, _("arm_dump_tdep: m_profile_msp_ns_regnum = %i\n"),
10689 tdep->m_profile_msp_ns_regnum);
10690 gdb_printf (file, _("arm_dump_tdep: m_profile_psp_ns_regnum = %i\n"),
10691 tdep->m_profile_psp_ns_regnum);
10692 gdb_printf (file, _("arm_dump_tdep: m_profile_msp_s_regnum = %i\n"),
10693 tdep->m_profile_msp_s_regnum);
10694 gdb_printf (file, _("arm_dump_tdep: m_profile_psp_s_regnum = %i\n"),
10695 tdep->m_profile_psp_s_regnum);
10696 gdb_printf (file, _("arm_dump_tdep: Lowest pc = 0x%lx\n"),
10697 (unsigned long) tdep->lowest_pc);
10698 gdb_printf (file, _("arm_dump_tdep: have_pacbti = %s\n"),
10699 tdep->have_pacbti? "yes" : "no");
10700 gdb_printf (file, _("arm_dump_tdep: pacbti_pseudo_base = %i\n"),
10701 tdep->pacbti_pseudo_base);
10702 gdb_printf (file, _("arm_dump_tdep: pacbti_pseudo_count = %i\n"),
10703 tdep->pacbti_pseudo_count);
10704 gdb_printf (file, _("arm_dump_tdep: is_m = %s\n"),
10705 tdep->is_m? "yes" : "no");
10708 #if GDB_SELF_TEST
10709 namespace selftests
10711 static void arm_record_test (void);
10712 static void arm_analyze_prologue_test ();
10714 #endif
10716 void _initialize_arm_tdep ();
10717 void
10718 _initialize_arm_tdep ()
10720 long length;
10721 int i, j;
10722 char regdesc[1024], *rdptr = regdesc;
10723 size_t rest = sizeof (regdesc);
10725 gdbarch_register (bfd_arch_arm, arm_gdbarch_init, arm_dump_tdep);
10727 /* Add ourselves to objfile event chain. */
10728 gdb::observers::new_objfile.attach (arm_exidx_new_objfile, "arm-tdep");
10730 /* Register an ELF OS ABI sniffer for ARM binaries. */
10731 gdbarch_register_osabi_sniffer (bfd_arch_arm,
10732 bfd_target_elf_flavour,
10733 arm_elf_osabi_sniffer);
10735 /* Add root prefix command for all "set arm"/"show arm" commands. */
10736 add_setshow_prefix_cmd ("arm", no_class,
10737 _("Various ARM-specific commands."),
10738 _("Various ARM-specific commands."),
10739 &setarmcmdlist, &showarmcmdlist,
10740 &setlist, &showlist);
10742 arm_disassembler_options = xstrdup ("reg-names-std");
10743 const disasm_options_t *disasm_options
10744 = &disassembler_options_arm ()->options;
10745 int num_disassembly_styles = 0;
10746 for (i = 0; disasm_options->name[i] != NULL; i++)
10747 if (startswith (disasm_options->name[i], "reg-names-"))
10748 num_disassembly_styles++;
10750 /* Initialize the array that will be passed to add_setshow_enum_cmd(). */
10751 valid_disassembly_styles = XNEWVEC (const char *,
10752 num_disassembly_styles + 1);
10753 for (i = j = 0; disasm_options->name[i] != NULL; i++)
10754 if (startswith (disasm_options->name[i], "reg-names-"))
10756 size_t offset = strlen ("reg-names-");
10757 const char *style = disasm_options->name[i];
10758 valid_disassembly_styles[j++] = &style[offset];
10759 if (strcmp (&style[offset], "std") == 0)
10760 disassembly_style = &style[offset];
10761 length = snprintf (rdptr, rest, "%s - %s\n", &style[offset],
10762 disasm_options->description[i]);
10763 rdptr += length;
10764 rest -= length;
10766 /* Mark the end of valid options. */
10767 valid_disassembly_styles[num_disassembly_styles] = NULL;
10769 /* Create the help text. */
10770 std::string helptext = string_printf ("%s%s%s",
10771 _("The valid values are:\n"),
10772 regdesc,
10773 _("The default is \"std\"."));
10775 add_setshow_enum_cmd("disassembler", no_class,
10776 valid_disassembly_styles, &disassembly_style,
10777 _("Set the disassembly style."),
10778 _("Show the disassembly style."),
10779 helptext.c_str (),
10780 set_disassembly_style_sfunc,
10781 show_disassembly_style_sfunc,
10782 &setarmcmdlist, &showarmcmdlist);
10784 add_setshow_boolean_cmd ("apcs32", no_class, &arm_apcs_32,
10785 _("Set usage of ARM 32-bit mode."),
10786 _("Show usage of ARM 32-bit mode."),
10787 _("When off, a 26-bit PC will be used."),
10788 NULL,
10789 NULL, /* FIXME: i18n: Usage of ARM 32-bit
10790 mode is %s. */
10791 &setarmcmdlist, &showarmcmdlist);
10793 /* Add a command to allow the user to force the FPU model. */
10794 add_setshow_enum_cmd ("fpu", no_class, fp_model_strings, &current_fp_model,
10795 _("Set the floating point type."),
10796 _("Show the floating point type."),
10797 _("auto - Determine the FP typefrom the OS-ABI.\n\
10798 softfpa - Software FP, mixed-endian doubles on little-endian ARMs.\n\
10799 fpa - FPA co-processor (GCC compiled).\n\
10800 softvfp - Software FP with pure-endian doubles.\n\
10801 vfp - VFP co-processor."),
10802 set_fp_model_sfunc, show_fp_model,
10803 &setarmcmdlist, &showarmcmdlist);
10805 /* Add a command to allow the user to force the ABI. */
10806 add_setshow_enum_cmd ("abi", class_support, arm_abi_strings, &arm_abi_string,
10807 _("Set the ABI."),
10808 _("Show the ABI."),
10809 NULL, arm_set_abi, arm_show_abi,
10810 &setarmcmdlist, &showarmcmdlist);
10812 /* Add two commands to allow the user to force the assumed
10813 execution mode. */
10814 add_setshow_enum_cmd ("fallback-mode", class_support,
10815 arm_mode_strings, &arm_fallback_mode_string,
10816 _("Set the mode assumed when symbols are unavailable."),
10817 _("Show the mode assumed when symbols are unavailable."),
10818 NULL, NULL, arm_show_fallback_mode,
10819 &setarmcmdlist, &showarmcmdlist);
10820 add_setshow_enum_cmd ("force-mode", class_support,
10821 arm_mode_strings, &arm_force_mode_string,
10822 _("Set the mode assumed even when symbols are available."),
10823 _("Show the mode assumed even when symbols are available."),
10824 NULL, NULL, arm_show_force_mode,
10825 &setarmcmdlist, &showarmcmdlist);
10827 /* Add a command to stop triggering security exceptions when
10828 unwinding exception stacks. */
10829 add_setshow_boolean_cmd ("unwind-secure-frames", no_class, &arm_unwind_secure_frames,
10830 _("Set usage of non-secure to secure exception stack unwinding."),
10831 _("Show usage of non-secure to secure exception stack unwinding."),
10832 _("When on, the debugger can trigger memory access traps."),
10833 NULL, arm_show_unwind_secure_frames,
10834 &setarmcmdlist, &showarmcmdlist);
10836 /* Debugging flag. */
10837 add_setshow_boolean_cmd ("arm", class_maintenance, &arm_debug,
10838 _("Set ARM debugging."),
10839 _("Show ARM debugging."),
10840 _("When on, arm-specific debugging is enabled."),
10841 NULL,
10842 NULL, /* FIXME: i18n: "ARM debugging is %s. */
10843 &setdebuglist, &showdebuglist);
10845 #if GDB_SELF_TEST
10846 selftests::register_test ("arm-record", selftests::arm_record_test);
10847 selftests::register_test ("arm_analyze_prologue", selftests::arm_analyze_prologue_test);
10848 #endif
10852 /* ARM-reversible process record data structures. */
10854 #define ARM_INSN_SIZE_BYTES 4
10855 #define THUMB_INSN_SIZE_BYTES 2
10856 #define THUMB2_INSN_SIZE_BYTES 4
10859 /* Position of the bit within a 32-bit ARM instruction
10860 that defines whether the instruction is a load or store. */
10861 #define INSN_S_L_BIT_NUM 20
10863 #define REG_ALLOC(REGS, LENGTH, RECORD_BUF) \
10864 do \
10866 unsigned int reg_len = LENGTH; \
10867 if (reg_len) \
10869 REGS = XNEWVEC (uint32_t, reg_len); \
10870 memcpy(&REGS[0], &RECORD_BUF[0], sizeof(uint32_t)*LENGTH); \
10873 while (0)
10875 #define MEM_ALLOC(MEMS, LENGTH, RECORD_BUF) \
10876 do \
10878 unsigned int mem_len = LENGTH; \
10879 if (mem_len) \
10881 MEMS = XNEWVEC (struct arm_mem_r, mem_len); \
10882 memcpy(&MEMS->len, &RECORD_BUF[0], \
10883 sizeof(struct arm_mem_r) * LENGTH); \
10886 while (0)
10888 /* Checks whether insn is already recorded or yet to be decoded. (boolean expression). */
10889 #define INSN_RECORDED(ARM_RECORD) \
10890 (0 != (ARM_RECORD)->reg_rec_count || 0 != (ARM_RECORD)->mem_rec_count)
10892 /* ARM memory record structure. */
10893 struct arm_mem_r
10895 uint32_t len; /* Record length. */
10896 uint32_t addr; /* Memory address. */
10899 /* ARM instruction record contains opcode of current insn
10900 and execution state (before entry to decode_insn()),
10901 contains list of to-be-modified registers and
10902 memory blocks (on return from decode_insn()). */
10904 struct arm_insn_decode_record
10906 struct gdbarch *gdbarch;
10907 struct regcache *regcache;
10908 CORE_ADDR this_addr; /* Address of the insn being decoded. */
10909 uint32_t arm_insn; /* Should accommodate thumb. */
10910 uint32_t cond; /* Condition code. */
10911 uint32_t opcode; /* Insn opcode. */
10912 uint32_t decode; /* Insn decode bits. */
10913 uint32_t mem_rec_count; /* No of mem records. */
10914 uint32_t reg_rec_count; /* No of reg records. */
10915 uint32_t *arm_regs; /* Registers to be saved for this record. */
10916 struct arm_mem_r *arm_mems; /* Memory to be saved for this record. */
10920 /* Checks ARM SBZ and SBO mandatory fields. */
10922 static int
10923 sbo_sbz (uint32_t insn, uint32_t bit_num, uint32_t len, uint32_t sbo)
10925 uint32_t ones = bits (insn, bit_num - 1, (bit_num -1) + (len - 1));
10927 if (!len)
10928 return 1;
10930 if (!sbo)
10931 ones = ~ones;
10933 while (ones)
10935 if (!(ones & sbo))
10937 return 0;
10939 ones = ones >> 1;
10941 return 1;
10944 enum arm_record_result
10946 ARM_RECORD_SUCCESS = 0,
10947 ARM_RECORD_FAILURE = 1
10950 enum arm_record_strx_t
10952 ARM_RECORD_STRH=1,
10953 ARM_RECORD_STRD
10956 enum record_type_t
10958 ARM_RECORD=1,
10959 THUMB_RECORD,
10960 THUMB2_RECORD
10964 static int
10965 arm_record_strx (arm_insn_decode_record *arm_insn_r, uint32_t *record_buf,
10966 uint32_t *record_buf_mem, arm_record_strx_t str_type)
10969 struct regcache *reg_cache = arm_insn_r->regcache;
10970 ULONGEST u_regval[2]= {0};
10972 uint32_t reg_src1 = 0, reg_src2 = 0;
10973 uint32_t immed_high = 0, immed_low = 0,offset_8 = 0, tgt_mem_addr = 0;
10975 arm_insn_r->opcode = bits (arm_insn_r->arm_insn, 21, 24);
10976 arm_insn_r->decode = bits (arm_insn_r->arm_insn, 4, 7);
10978 if (14 == arm_insn_r->opcode || 10 == arm_insn_r->opcode)
10980 /* 1) Handle misc store, immediate offset. */
10981 immed_low = bits (arm_insn_r->arm_insn, 0, 3);
10982 immed_high = bits (arm_insn_r->arm_insn, 8, 11);
10983 reg_src1 = bits (arm_insn_r->arm_insn, 16, 19);
10984 regcache_raw_read_unsigned (reg_cache, reg_src1,
10985 &u_regval[0]);
10986 if (ARM_PC_REGNUM == reg_src1)
10988 /* If R15 was used as Rn, hence current PC+8. */
10989 u_regval[0] = u_regval[0] + 8;
10991 offset_8 = (immed_high << 4) | immed_low;
10992 /* Calculate target store address. */
10993 if (14 == arm_insn_r->opcode)
10995 tgt_mem_addr = u_regval[0] + offset_8;
10997 else
10999 tgt_mem_addr = u_regval[0] - offset_8;
11001 if (ARM_RECORD_STRH == str_type)
11003 record_buf_mem[0] = 2;
11004 record_buf_mem[1] = tgt_mem_addr;
11005 arm_insn_r->mem_rec_count = 1;
11007 else if (ARM_RECORD_STRD == str_type)
11009 record_buf_mem[0] = 4;
11010 record_buf_mem[1] = tgt_mem_addr;
11011 record_buf_mem[2] = 4;
11012 record_buf_mem[3] = tgt_mem_addr + 4;
11013 arm_insn_r->mem_rec_count = 2;
11016 else if (12 == arm_insn_r->opcode || 8 == arm_insn_r->opcode)
11018 /* 2) Store, register offset. */
11019 /* Get Rm. */
11020 reg_src1 = bits (arm_insn_r->arm_insn, 0, 3);
11021 /* Get Rn. */
11022 reg_src2 = bits (arm_insn_r->arm_insn, 16, 19);
11023 regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval[0]);
11024 regcache_raw_read_unsigned (reg_cache, reg_src2, &u_regval[1]);
11025 if (15 == reg_src2)
11027 /* If R15 was used as Rn, hence current PC+8. */
11028 u_regval[0] = u_regval[0] + 8;
11030 /* Calculate target store address, Rn +/- Rm, register offset. */
11031 if (12 == arm_insn_r->opcode)
11033 tgt_mem_addr = u_regval[0] + u_regval[1];
11035 else
11037 tgt_mem_addr = u_regval[1] - u_regval[0];
11039 if (ARM_RECORD_STRH == str_type)
11041 record_buf_mem[0] = 2;
11042 record_buf_mem[1] = tgt_mem_addr;
11043 arm_insn_r->mem_rec_count = 1;
11045 else if (ARM_RECORD_STRD == str_type)
11047 record_buf_mem[0] = 4;
11048 record_buf_mem[1] = tgt_mem_addr;
11049 record_buf_mem[2] = 4;
11050 record_buf_mem[3] = tgt_mem_addr + 4;
11051 arm_insn_r->mem_rec_count = 2;
11054 else if (11 == arm_insn_r->opcode || 15 == arm_insn_r->opcode
11055 || 2 == arm_insn_r->opcode || 6 == arm_insn_r->opcode)
11057 /* 3) Store, immediate pre-indexed. */
11058 /* 5) Store, immediate post-indexed. */
11059 immed_low = bits (arm_insn_r->arm_insn, 0, 3);
11060 immed_high = bits (arm_insn_r->arm_insn, 8, 11);
11061 offset_8 = (immed_high << 4) | immed_low;
11062 reg_src1 = bits (arm_insn_r->arm_insn, 16, 19);
11063 regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval[0]);
11064 /* Calculate target store address, Rn +/- Rm, register offset. */
11065 if (15 == arm_insn_r->opcode || 6 == arm_insn_r->opcode)
11067 tgt_mem_addr = u_regval[0] + offset_8;
11069 else
11071 tgt_mem_addr = u_regval[0] - offset_8;
11073 if (ARM_RECORD_STRH == str_type)
11075 record_buf_mem[0] = 2;
11076 record_buf_mem[1] = tgt_mem_addr;
11077 arm_insn_r->mem_rec_count = 1;
11079 else if (ARM_RECORD_STRD == str_type)
11081 record_buf_mem[0] = 4;
11082 record_buf_mem[1] = tgt_mem_addr;
11083 record_buf_mem[2] = 4;
11084 record_buf_mem[3] = tgt_mem_addr + 4;
11085 arm_insn_r->mem_rec_count = 2;
11087 /* Record Rn also as it changes. */
11088 *(record_buf) = bits (arm_insn_r->arm_insn, 16, 19);
11089 arm_insn_r->reg_rec_count = 1;
11091 else if (9 == arm_insn_r->opcode || 13 == arm_insn_r->opcode
11092 || 0 == arm_insn_r->opcode || 4 == arm_insn_r->opcode)
11094 /* 4) Store, register pre-indexed. */
11095 /* 6) Store, register post -indexed. */
11096 reg_src1 = bits (arm_insn_r->arm_insn, 0, 3);
11097 reg_src2 = bits (arm_insn_r->arm_insn, 16, 19);
11098 regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval[0]);
11099 regcache_raw_read_unsigned (reg_cache, reg_src2, &u_regval[1]);
11100 /* Calculate target store address, Rn +/- Rm, register offset. */
11101 if (13 == arm_insn_r->opcode || 4 == arm_insn_r->opcode)
11103 tgt_mem_addr = u_regval[0] + u_regval[1];
11105 else
11107 tgt_mem_addr = u_regval[1] - u_regval[0];
11109 if (ARM_RECORD_STRH == str_type)
11111 record_buf_mem[0] = 2;
11112 record_buf_mem[1] = tgt_mem_addr;
11113 arm_insn_r->mem_rec_count = 1;
11115 else if (ARM_RECORD_STRD == str_type)
11117 record_buf_mem[0] = 4;
11118 record_buf_mem[1] = tgt_mem_addr;
11119 record_buf_mem[2] = 4;
11120 record_buf_mem[3] = tgt_mem_addr + 4;
11121 arm_insn_r->mem_rec_count = 2;
11123 /* Record Rn also as it changes. */
11124 *(record_buf) = bits (arm_insn_r->arm_insn, 16, 19);
11125 arm_insn_r->reg_rec_count = 1;
11127 return 0;
11130 /* Handling ARM extension space insns. */
11132 static int
11133 arm_record_extension_space (arm_insn_decode_record *arm_insn_r)
11135 int ret = 0; /* Return value: -1:record failure ; 0:success */
11136 uint32_t opcode1 = 0, opcode2 = 0, insn_op1 = 0;
11137 uint32_t record_buf[8], record_buf_mem[8];
11138 uint32_t reg_src1 = 0;
11139 struct regcache *reg_cache = arm_insn_r->regcache;
11140 ULONGEST u_regval = 0;
11142 gdb_assert (!INSN_RECORDED(arm_insn_r));
11143 /* Handle unconditional insn extension space. */
11145 opcode1 = bits (arm_insn_r->arm_insn, 20, 27);
11146 opcode2 = bits (arm_insn_r->arm_insn, 4, 7);
11147 if (arm_insn_r->cond)
11149 /* PLD has no affect on architectural state, it just affects
11150 the caches. */
11151 if (5 == ((opcode1 & 0xE0) >> 5))
11153 /* BLX(1) */
11154 record_buf[0] = ARM_PS_REGNUM;
11155 record_buf[1] = ARM_LR_REGNUM;
11156 arm_insn_r->reg_rec_count = 2;
11158 /* STC2, LDC2, MCR2, MRC2, CDP2: <TBD>, co-processor insn. */
11162 opcode1 = bits (arm_insn_r->arm_insn, 25, 27);
11163 if (3 == opcode1 && bit (arm_insn_r->arm_insn, 4))
11165 ret = -1;
11166 /* Undefined instruction on ARM V5; need to handle if later
11167 versions define it. */
11170 opcode1 = bits (arm_insn_r->arm_insn, 24, 27);
11171 opcode2 = bits (arm_insn_r->arm_insn, 4, 7);
11172 insn_op1 = bits (arm_insn_r->arm_insn, 20, 23);
11174 /* Handle arithmetic insn extension space. */
11175 if (!opcode1 && 9 == opcode2 && 1 != arm_insn_r->cond
11176 && !INSN_RECORDED(arm_insn_r))
11178 /* Handle MLA(S) and MUL(S). */
11179 if (in_inclusive_range (insn_op1, 0U, 3U))
11181 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11182 record_buf[1] = ARM_PS_REGNUM;
11183 arm_insn_r->reg_rec_count = 2;
11185 else if (in_inclusive_range (insn_op1, 4U, 15U))
11187 /* Handle SMLAL(S), SMULL(S), UMLAL(S), UMULL(S). */
11188 record_buf[0] = bits (arm_insn_r->arm_insn, 16, 19);
11189 record_buf[1] = bits (arm_insn_r->arm_insn, 12, 15);
11190 record_buf[2] = ARM_PS_REGNUM;
11191 arm_insn_r->reg_rec_count = 3;
11195 opcode1 = bits (arm_insn_r->arm_insn, 26, 27);
11196 opcode2 = bits (arm_insn_r->arm_insn, 23, 24);
11197 insn_op1 = bits (arm_insn_r->arm_insn, 21, 22);
11199 /* Handle control insn extension space. */
11201 if (!opcode1 && 2 == opcode2 && !bit (arm_insn_r->arm_insn, 20)
11202 && 1 != arm_insn_r->cond && !INSN_RECORDED(arm_insn_r))
11204 if (!bit (arm_insn_r->arm_insn,25))
11206 if (!bits (arm_insn_r->arm_insn, 4, 7))
11208 if ((0 == insn_op1) || (2 == insn_op1))
11210 /* MRS. */
11211 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11212 arm_insn_r->reg_rec_count = 1;
11214 else if (1 == insn_op1)
11216 /* CSPR is going to be changed. */
11217 record_buf[0] = ARM_PS_REGNUM;
11218 arm_insn_r->reg_rec_count = 1;
11220 else if (3 == insn_op1)
11222 /* SPSR is going to be changed. */
11223 /* We need to get SPSR value, which is yet to be done. */
11224 return -1;
11227 else if (1 == bits (arm_insn_r->arm_insn, 4, 7))
11229 if (1 == insn_op1)
11231 /* BX. */
11232 record_buf[0] = ARM_PS_REGNUM;
11233 arm_insn_r->reg_rec_count = 1;
11235 else if (3 == insn_op1)
11237 /* CLZ. */
11238 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11239 arm_insn_r->reg_rec_count = 1;
11242 else if (3 == bits (arm_insn_r->arm_insn, 4, 7))
11244 /* BLX. */
11245 record_buf[0] = ARM_PS_REGNUM;
11246 record_buf[1] = ARM_LR_REGNUM;
11247 arm_insn_r->reg_rec_count = 2;
11249 else if (5 == bits (arm_insn_r->arm_insn, 4, 7))
11251 /* QADD, QSUB, QDADD, QDSUB */
11252 record_buf[0] = ARM_PS_REGNUM;
11253 record_buf[1] = bits (arm_insn_r->arm_insn, 12, 15);
11254 arm_insn_r->reg_rec_count = 2;
11256 else if (7 == bits (arm_insn_r->arm_insn, 4, 7))
11258 /* BKPT. */
11259 record_buf[0] = ARM_PS_REGNUM;
11260 record_buf[1] = ARM_LR_REGNUM;
11261 arm_insn_r->reg_rec_count = 2;
11263 /* Save SPSR also;how? */
11264 return -1;
11266 else if(8 == bits (arm_insn_r->arm_insn, 4, 7)
11267 || 10 == bits (arm_insn_r->arm_insn, 4, 7)
11268 || 12 == bits (arm_insn_r->arm_insn, 4, 7)
11269 || 14 == bits (arm_insn_r->arm_insn, 4, 7)
11272 if (0 == insn_op1 || 1 == insn_op1)
11274 /* SMLA<x><y>, SMLAW<y>, SMULW<y>. */
11275 /* We dont do optimization for SMULW<y> where we
11276 need only Rd. */
11277 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11278 record_buf[1] = ARM_PS_REGNUM;
11279 arm_insn_r->reg_rec_count = 2;
11281 else if (2 == insn_op1)
11283 /* SMLAL<x><y>. */
11284 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11285 record_buf[1] = bits (arm_insn_r->arm_insn, 16, 19);
11286 arm_insn_r->reg_rec_count = 2;
11288 else if (3 == insn_op1)
11290 /* SMUL<x><y>. */
11291 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11292 arm_insn_r->reg_rec_count = 1;
11296 else
11298 /* MSR : immediate form. */
11299 if (1 == insn_op1)
11301 /* CSPR is going to be changed. */
11302 record_buf[0] = ARM_PS_REGNUM;
11303 arm_insn_r->reg_rec_count = 1;
11305 else if (3 == insn_op1)
11307 /* SPSR is going to be changed. */
11308 /* we need to get SPSR value, which is yet to be done */
11309 return -1;
11314 opcode1 = bits (arm_insn_r->arm_insn, 25, 27);
11315 opcode2 = bits (arm_insn_r->arm_insn, 20, 24);
11316 insn_op1 = bits (arm_insn_r->arm_insn, 5, 6);
11318 /* Handle load/store insn extension space. */
11320 if (!opcode1 && bit (arm_insn_r->arm_insn, 7)
11321 && bit (arm_insn_r->arm_insn, 4) && 1 != arm_insn_r->cond
11322 && !INSN_RECORDED(arm_insn_r))
11324 /* SWP/SWPB. */
11325 if (0 == insn_op1)
11327 /* These insn, changes register and memory as well. */
11328 /* SWP or SWPB insn. */
11329 /* Get memory address given by Rn. */
11330 reg_src1 = bits (arm_insn_r->arm_insn, 16, 19);
11331 regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval);
11332 /* SWP insn ?, swaps word. */
11333 if (8 == arm_insn_r->opcode)
11335 record_buf_mem[0] = 4;
11337 else
11339 /* SWPB insn, swaps only byte. */
11340 record_buf_mem[0] = 1;
11342 record_buf_mem[1] = u_regval;
11343 arm_insn_r->mem_rec_count = 1;
11344 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11345 arm_insn_r->reg_rec_count = 1;
11347 else if (1 == insn_op1 && !bit (arm_insn_r->arm_insn, 20))
11349 /* STRH. */
11350 arm_record_strx(arm_insn_r, &record_buf[0], &record_buf_mem[0],
11351 ARM_RECORD_STRH);
11353 else if (2 == insn_op1 && !bit (arm_insn_r->arm_insn, 20))
11355 /* LDRD. */
11356 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11357 record_buf[1] = record_buf[0] + 1;
11358 arm_insn_r->reg_rec_count = 2;
11360 else if (3 == insn_op1 && !bit (arm_insn_r->arm_insn, 20))
11362 /* STRD. */
11363 arm_record_strx(arm_insn_r, &record_buf[0], &record_buf_mem[0],
11364 ARM_RECORD_STRD);
11366 else if (bit (arm_insn_r->arm_insn, 20) && insn_op1 <= 3)
11368 /* LDRH, LDRSB, LDRSH. */
11369 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11370 arm_insn_r->reg_rec_count = 1;
11375 opcode1 = bits (arm_insn_r->arm_insn, 23, 27);
11376 if (24 == opcode1 && bit (arm_insn_r->arm_insn, 21)
11377 && !INSN_RECORDED(arm_insn_r))
11379 ret = -1;
11380 /* Handle coprocessor insn extension space. */
11383 /* To be done for ARMv5 and later; as of now we return -1. */
11384 if (-1 == ret)
11385 return ret;
11387 REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
11388 MEM_ALLOC (arm_insn_r->arm_mems, arm_insn_r->mem_rec_count, record_buf_mem);
11390 return ret;
11393 /* Handling opcode 000 insns. */
11395 static int
11396 arm_record_data_proc_misc_ld_str (arm_insn_decode_record *arm_insn_r)
11398 struct regcache *reg_cache = arm_insn_r->regcache;
11399 uint32_t record_buf[8], record_buf_mem[8];
11400 ULONGEST u_regval[2] = {0};
11402 uint32_t reg_src1 = 0;
11403 uint32_t opcode1 = 0;
11405 arm_insn_r->opcode = bits (arm_insn_r->arm_insn, 21, 24);
11406 arm_insn_r->decode = bits (arm_insn_r->arm_insn, 4, 7);
11407 opcode1 = bits (arm_insn_r->arm_insn, 20, 24);
11409 if (!((opcode1 & 0x19) == 0x10))
11411 /* Data-processing (register) and Data-processing (register-shifted
11412 register */
11413 /* Out of 11 shifter operands mode, all the insn modifies destination
11414 register, which is specified by 13-16 decode. */
11415 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11416 record_buf[1] = ARM_PS_REGNUM;
11417 arm_insn_r->reg_rec_count = 2;
11419 else if ((arm_insn_r->decode < 8) && ((opcode1 & 0x19) == 0x10))
11421 /* Miscellaneous instructions */
11423 if (3 == arm_insn_r->decode && 0x12 == opcode1
11424 && sbo_sbz (arm_insn_r->arm_insn, 9, 12, 1))
11426 /* Handle BLX, branch and link/exchange. */
11427 if (9 == arm_insn_r->opcode)
11429 /* Branch is chosen by setting T bit of CSPR, bitp[0] of Rm,
11430 and R14 stores the return address. */
11431 record_buf[0] = ARM_PS_REGNUM;
11432 record_buf[1] = ARM_LR_REGNUM;
11433 arm_insn_r->reg_rec_count = 2;
11436 else if (7 == arm_insn_r->decode && 0x12 == opcode1)
11438 /* Handle enhanced software breakpoint insn, BKPT. */
11439 /* CPSR is changed to be executed in ARM state, disabling normal
11440 interrupts, entering abort mode. */
11441 /* According to high vector configuration PC is set. */
11442 /* user hit breakpoint and type reverse, in
11443 that case, we need to go back with previous CPSR and
11444 Program Counter. */
11445 record_buf[0] = ARM_PS_REGNUM;
11446 record_buf[1] = ARM_LR_REGNUM;
11447 arm_insn_r->reg_rec_count = 2;
11449 /* Save SPSR also; how? */
11450 return -1;
11452 else if (1 == arm_insn_r->decode && 0x12 == opcode1
11453 && sbo_sbz (arm_insn_r->arm_insn, 9, 12, 1))
11455 /* Handle BX, branch and link/exchange. */
11456 /* Branch is chosen by setting T bit of CSPR, bitp[0] of Rm. */
11457 record_buf[0] = ARM_PS_REGNUM;
11458 arm_insn_r->reg_rec_count = 1;
11460 else if (1 == arm_insn_r->decode && 0x16 == opcode1
11461 && sbo_sbz (arm_insn_r->arm_insn, 9, 4, 1)
11462 && sbo_sbz (arm_insn_r->arm_insn, 17, 4, 1))
11464 /* Count leading zeros: CLZ. */
11465 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11466 arm_insn_r->reg_rec_count = 1;
11468 else if (!bit (arm_insn_r->arm_insn, INSN_S_L_BIT_NUM)
11469 && (8 == arm_insn_r->opcode || 10 == arm_insn_r->opcode)
11470 && sbo_sbz (arm_insn_r->arm_insn, 17, 4, 1)
11471 && sbo_sbz (arm_insn_r->arm_insn, 1, 12, 0))
11473 /* Handle MRS insn. */
11474 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11475 arm_insn_r->reg_rec_count = 1;
11478 else if (9 == arm_insn_r->decode && opcode1 < 0x10)
11480 /* Multiply and multiply-accumulate */
11482 /* Handle multiply instructions. */
11483 /* MLA, MUL, SMLAL, SMULL, UMLAL, UMULL. */
11484 if (0 == arm_insn_r->opcode || 1 == arm_insn_r->opcode)
11486 /* Handle MLA and MUL. */
11487 record_buf[0] = bits (arm_insn_r->arm_insn, 16, 19);
11488 record_buf[1] = ARM_PS_REGNUM;
11489 arm_insn_r->reg_rec_count = 2;
11491 else if (4 <= arm_insn_r->opcode && 7 >= arm_insn_r->opcode)
11493 /* Handle SMLAL, SMULL, UMLAL, UMULL. */
11494 record_buf[0] = bits (arm_insn_r->arm_insn, 16, 19);
11495 record_buf[1] = bits (arm_insn_r->arm_insn, 12, 15);
11496 record_buf[2] = ARM_PS_REGNUM;
11497 arm_insn_r->reg_rec_count = 3;
11500 else if (9 == arm_insn_r->decode && opcode1 > 0x10)
11502 /* Synchronization primitives */
11504 /* Handling SWP, SWPB. */
11505 /* These insn, changes register and memory as well. */
11506 /* SWP or SWPB insn. */
11508 reg_src1 = bits (arm_insn_r->arm_insn, 16, 19);
11509 regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval[0]);
11510 /* SWP insn ?, swaps word. */
11511 if (8 == arm_insn_r->opcode)
11513 record_buf_mem[0] = 4;
11515 else
11517 /* SWPB insn, swaps only byte. */
11518 record_buf_mem[0] = 1;
11520 record_buf_mem[1] = u_regval[0];
11521 arm_insn_r->mem_rec_count = 1;
11522 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11523 arm_insn_r->reg_rec_count = 1;
11525 else if (11 == arm_insn_r->decode || 13 == arm_insn_r->decode
11526 || 15 == arm_insn_r->decode)
11528 if ((opcode1 & 0x12) == 2)
11530 /* Extra load/store (unprivileged) */
11531 return -1;
11533 else
11535 /* Extra load/store */
11536 switch (bits (arm_insn_r->arm_insn, 5, 6))
11538 case 1:
11539 if ((opcode1 & 0x05) == 0x0 || (opcode1 & 0x05) == 0x4)
11541 /* STRH (register), STRH (immediate) */
11542 arm_record_strx (arm_insn_r, &record_buf[0],
11543 &record_buf_mem[0], ARM_RECORD_STRH);
11545 else if ((opcode1 & 0x05) == 0x1)
11547 /* LDRH (register) */
11548 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11549 arm_insn_r->reg_rec_count = 1;
11551 if (bit (arm_insn_r->arm_insn, 21))
11553 /* Write back to Rn. */
11554 record_buf[arm_insn_r->reg_rec_count++]
11555 = bits (arm_insn_r->arm_insn, 16, 19);
11558 else if ((opcode1 & 0x05) == 0x5)
11560 /* LDRH (immediate), LDRH (literal) */
11561 int rn = bits (arm_insn_r->arm_insn, 16, 19);
11563 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11564 arm_insn_r->reg_rec_count = 1;
11566 if (rn != 15)
11568 /*LDRH (immediate) */
11569 if (bit (arm_insn_r->arm_insn, 21))
11571 /* Write back to Rn. */
11572 record_buf[arm_insn_r->reg_rec_count++] = rn;
11576 else
11577 return -1;
11578 break;
11579 case 2:
11580 if ((opcode1 & 0x05) == 0x0)
11582 /* LDRD (register) */
11583 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11584 record_buf[1] = record_buf[0] + 1;
11585 arm_insn_r->reg_rec_count = 2;
11587 if (bit (arm_insn_r->arm_insn, 21))
11589 /* Write back to Rn. */
11590 record_buf[arm_insn_r->reg_rec_count++]
11591 = bits (arm_insn_r->arm_insn, 16, 19);
11594 else if ((opcode1 & 0x05) == 0x1)
11596 /* LDRSB (register) */
11597 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11598 arm_insn_r->reg_rec_count = 1;
11600 if (bit (arm_insn_r->arm_insn, 21))
11602 /* Write back to Rn. */
11603 record_buf[arm_insn_r->reg_rec_count++]
11604 = bits (arm_insn_r->arm_insn, 16, 19);
11607 else if ((opcode1 & 0x05) == 0x4 || (opcode1 & 0x05) == 0x5)
11609 /* LDRD (immediate), LDRD (literal), LDRSB (immediate),
11610 LDRSB (literal) */
11611 int rn = bits (arm_insn_r->arm_insn, 16, 19);
11613 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11614 arm_insn_r->reg_rec_count = 1;
11616 if (rn != 15)
11618 /*LDRD (immediate), LDRSB (immediate) */
11619 if (bit (arm_insn_r->arm_insn, 21))
11621 /* Write back to Rn. */
11622 record_buf[arm_insn_r->reg_rec_count++] = rn;
11626 else
11627 return -1;
11628 break;
11629 case 3:
11630 if ((opcode1 & 0x05) == 0x0)
11632 /* STRD (register) */
11633 arm_record_strx (arm_insn_r, &record_buf[0],
11634 &record_buf_mem[0], ARM_RECORD_STRD);
11636 else if ((opcode1 & 0x05) == 0x1)
11638 /* LDRSH (register) */
11639 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11640 arm_insn_r->reg_rec_count = 1;
11642 if (bit (arm_insn_r->arm_insn, 21))
11644 /* Write back to Rn. */
11645 record_buf[arm_insn_r->reg_rec_count++]
11646 = bits (arm_insn_r->arm_insn, 16, 19);
11649 else if ((opcode1 & 0x05) == 0x4)
11651 /* STRD (immediate) */
11652 arm_record_strx (arm_insn_r, &record_buf[0],
11653 &record_buf_mem[0], ARM_RECORD_STRD);
11655 else if ((opcode1 & 0x05) == 0x5)
11657 /* LDRSH (immediate), LDRSH (literal) */
11658 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11659 arm_insn_r->reg_rec_count = 1;
11661 if (bit (arm_insn_r->arm_insn, 21))
11663 /* Write back to Rn. */
11664 record_buf[arm_insn_r->reg_rec_count++]
11665 = bits (arm_insn_r->arm_insn, 16, 19);
11668 else
11669 return -1;
11670 break;
11671 default:
11672 return -1;
11676 else
11678 return -1;
11681 REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
11682 MEM_ALLOC (arm_insn_r->arm_mems, arm_insn_r->mem_rec_count, record_buf_mem);
11683 return 0;
11686 /* Handling opcode 001 insns. */
11688 static int
11689 arm_record_data_proc_imm (arm_insn_decode_record *arm_insn_r)
11691 uint32_t record_buf[8], record_buf_mem[8];
11693 arm_insn_r->opcode = bits (arm_insn_r->arm_insn, 21, 24);
11694 arm_insn_r->decode = bits (arm_insn_r->arm_insn, 4, 7);
11696 if ((9 == arm_insn_r->opcode || 11 == arm_insn_r->opcode)
11697 && 2 == bits (arm_insn_r->arm_insn, 20, 21)
11698 && sbo_sbz (arm_insn_r->arm_insn, 13, 4, 1)
11701 /* Handle MSR insn. */
11702 if (9 == arm_insn_r->opcode)
11704 /* CSPR is going to be changed. */
11705 record_buf[0] = ARM_PS_REGNUM;
11706 arm_insn_r->reg_rec_count = 1;
11708 else
11710 /* SPSR is going to be changed. */
11713 else if (arm_insn_r->opcode <= 15)
11715 /* Normal data processing insns. */
11716 /* Out of 11 shifter operands mode, all the insn modifies destination
11717 register, which is specified by 13-16 decode. */
11718 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11719 record_buf[1] = ARM_PS_REGNUM;
11720 arm_insn_r->reg_rec_count = 2;
11722 else
11724 return -1;
11727 REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
11728 MEM_ALLOC (arm_insn_r->arm_mems, arm_insn_r->mem_rec_count, record_buf_mem);
11729 return 0;
11732 static int
11733 arm_record_media (arm_insn_decode_record *arm_insn_r)
11735 uint32_t record_buf[8];
11737 switch (bits (arm_insn_r->arm_insn, 22, 24))
11739 case 0:
11740 /* Parallel addition and subtraction, signed */
11741 case 1:
11742 /* Parallel addition and subtraction, unsigned */
11743 case 2:
11744 case 3:
11745 /* Packing, unpacking, saturation and reversal */
11747 int rd = bits (arm_insn_r->arm_insn, 12, 15);
11749 record_buf[arm_insn_r->reg_rec_count++] = rd;
11751 break;
11753 case 4:
11754 case 5:
11755 /* Signed multiplies */
11757 int rd = bits (arm_insn_r->arm_insn, 16, 19);
11758 unsigned int op1 = bits (arm_insn_r->arm_insn, 20, 22);
11760 record_buf[arm_insn_r->reg_rec_count++] = rd;
11761 if (op1 == 0x0)
11762 record_buf[arm_insn_r->reg_rec_count++] = ARM_PS_REGNUM;
11763 else if (op1 == 0x4)
11764 record_buf[arm_insn_r->reg_rec_count++]
11765 = bits (arm_insn_r->arm_insn, 12, 15);
11767 break;
11769 case 6:
11771 if (bit (arm_insn_r->arm_insn, 21)
11772 && bits (arm_insn_r->arm_insn, 5, 6) == 0x2)
11774 /* SBFX */
11775 record_buf[arm_insn_r->reg_rec_count++]
11776 = bits (arm_insn_r->arm_insn, 12, 15);
11778 else if (bits (arm_insn_r->arm_insn, 20, 21) == 0x0
11779 && bits (arm_insn_r->arm_insn, 5, 7) == 0x0)
11781 /* USAD8 and USADA8 */
11782 record_buf[arm_insn_r->reg_rec_count++]
11783 = bits (arm_insn_r->arm_insn, 16, 19);
11786 break;
11788 case 7:
11790 if (bits (arm_insn_r->arm_insn, 20, 21) == 0x3
11791 && bits (arm_insn_r->arm_insn, 5, 7) == 0x7)
11793 /* Permanently UNDEFINED */
11794 return -1;
11796 else
11798 /* BFC, BFI and UBFX */
11799 record_buf[arm_insn_r->reg_rec_count++]
11800 = bits (arm_insn_r->arm_insn, 12, 15);
11803 break;
11805 default:
11806 return -1;
11809 REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
11811 return 0;
11814 /* Handle ARM mode instructions with opcode 010. */
11816 static int
11817 arm_record_ld_st_imm_offset (arm_insn_decode_record *arm_insn_r)
11819 struct regcache *reg_cache = arm_insn_r->regcache;
11821 uint32_t reg_base , reg_dest;
11822 uint32_t offset_12, tgt_mem_addr;
11823 uint32_t record_buf[8], record_buf_mem[8];
11824 unsigned char wback;
11825 ULONGEST u_regval;
11827 /* Calculate wback. */
11828 wback = (bit (arm_insn_r->arm_insn, 24) == 0)
11829 || (bit (arm_insn_r->arm_insn, 21) == 1);
11831 arm_insn_r->reg_rec_count = 0;
11832 reg_base = bits (arm_insn_r->arm_insn, 16, 19);
11834 if (bit (arm_insn_r->arm_insn, INSN_S_L_BIT_NUM))
11836 /* LDR (immediate), LDR (literal), LDRB (immediate), LDRB (literal), LDRBT
11837 and LDRT. */
11839 reg_dest = bits (arm_insn_r->arm_insn, 12, 15);
11840 record_buf[arm_insn_r->reg_rec_count++] = reg_dest;
11842 /* The LDR instruction is capable of doing branching. If MOV LR, PC
11843 preceeds a LDR instruction having R15 as reg_base, it
11844 emulates a branch and link instruction, and hence we need to save
11845 CPSR and PC as well. */
11846 if (ARM_PC_REGNUM == reg_dest)
11847 record_buf[arm_insn_r->reg_rec_count++] = ARM_PS_REGNUM;
11849 /* If wback is true, also save the base register, which is going to be
11850 written to. */
11851 if (wback)
11852 record_buf[arm_insn_r->reg_rec_count++] = reg_base;
11854 else
11856 /* STR (immediate), STRB (immediate), STRBT and STRT. */
11858 offset_12 = bits (arm_insn_r->arm_insn, 0, 11);
11859 regcache_raw_read_unsigned (reg_cache, reg_base, &u_regval);
11861 /* Handle bit U. */
11862 if (bit (arm_insn_r->arm_insn, 23))
11864 /* U == 1: Add the offset. */
11865 tgt_mem_addr = (uint32_t) u_regval + offset_12;
11867 else
11869 /* U == 0: subtract the offset. */
11870 tgt_mem_addr = (uint32_t) u_regval - offset_12;
11873 /* Bit 22 tells us whether the store instruction writes 1 byte or 4
11874 bytes. */
11875 if (bit (arm_insn_r->arm_insn, 22))
11877 /* STRB and STRBT: 1 byte. */
11878 record_buf_mem[0] = 1;
11880 else
11882 /* STR and STRT: 4 bytes. */
11883 record_buf_mem[0] = 4;
11886 /* Handle bit P. */
11887 if (bit (arm_insn_r->arm_insn, 24))
11888 record_buf_mem[1] = tgt_mem_addr;
11889 else
11890 record_buf_mem[1] = (uint32_t) u_regval;
11892 arm_insn_r->mem_rec_count = 1;
11894 /* If wback is true, also save the base register, which is going to be
11895 written to. */
11896 if (wback)
11897 record_buf[arm_insn_r->reg_rec_count++] = reg_base;
11900 REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
11901 MEM_ALLOC (arm_insn_r->arm_mems, arm_insn_r->mem_rec_count, record_buf_mem);
11902 return 0;
11905 /* Handling opcode 011 insns. */
11907 static int
11908 arm_record_ld_st_reg_offset (arm_insn_decode_record *arm_insn_r)
11910 struct regcache *reg_cache = arm_insn_r->regcache;
11912 uint32_t shift_imm = 0;
11913 uint32_t reg_src1 = 0, reg_src2 = 0, reg_dest = 0;
11914 uint32_t offset_12 = 0, tgt_mem_addr = 0;
11915 uint32_t record_buf[8], record_buf_mem[8];
11917 LONGEST s_word;
11918 ULONGEST u_regval[2];
11920 if (bit (arm_insn_r->arm_insn, 4))
11921 return arm_record_media (arm_insn_r);
11923 arm_insn_r->opcode = bits (arm_insn_r->arm_insn, 21, 24);
11924 arm_insn_r->decode = bits (arm_insn_r->arm_insn, 4, 7);
11926 /* Handle enhanced store insns and LDRD DSP insn,
11927 order begins according to addressing modes for store insns
11928 STRH insn. */
11930 /* LDR or STR? */
11931 if (bit (arm_insn_r->arm_insn, INSN_S_L_BIT_NUM))
11933 reg_dest = bits (arm_insn_r->arm_insn, 12, 15);
11934 /* LDR insn has a capability to do branching, if
11935 MOV LR, PC is preceded by LDR insn having Rn as R15
11936 in that case, it emulates branch and link insn, and hence we
11937 need to save CSPR and PC as well. */
11938 if (15 != reg_dest)
11940 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11941 arm_insn_r->reg_rec_count = 1;
11943 else
11945 record_buf[0] = reg_dest;
11946 record_buf[1] = ARM_PS_REGNUM;
11947 arm_insn_r->reg_rec_count = 2;
11950 else
11952 if (! bits (arm_insn_r->arm_insn, 4, 11))
11954 /* Store insn, register offset and register pre-indexed,
11955 register post-indexed. */
11956 /* Get Rm. */
11957 reg_src1 = bits (arm_insn_r->arm_insn, 0, 3);
11958 /* Get Rn. */
11959 reg_src2 = bits (arm_insn_r->arm_insn, 16, 19);
11960 regcache_raw_read_unsigned (reg_cache, reg_src1
11961 , &u_regval[0]);
11962 regcache_raw_read_unsigned (reg_cache, reg_src2
11963 , &u_regval[1]);
11964 if (15 == reg_src2)
11966 /* If R15 was used as Rn, hence current PC+8. */
11967 /* Pre-indexed mode doesnt reach here ; illegal insn. */
11968 u_regval[0] = u_regval[0] + 8;
11970 /* Calculate target store address, Rn +/- Rm, register offset. */
11971 /* U == 1. */
11972 if (bit (arm_insn_r->arm_insn, 23))
11974 tgt_mem_addr = u_regval[0] + u_regval[1];
11976 else
11978 tgt_mem_addr = u_regval[1] - u_regval[0];
11981 switch (arm_insn_r->opcode)
11983 /* STR. */
11984 case 8:
11985 case 12:
11986 /* STR. */
11987 case 9:
11988 case 13:
11989 /* STRT. */
11990 case 1:
11991 case 5:
11992 /* STR. */
11993 case 0:
11994 case 4:
11995 record_buf_mem[0] = 4;
11996 break;
11998 /* STRB. */
11999 case 10:
12000 case 14:
12001 /* STRB. */
12002 case 11:
12003 case 15:
12004 /* STRBT. */
12005 case 3:
12006 case 7:
12007 /* STRB. */
12008 case 2:
12009 case 6:
12010 record_buf_mem[0] = 1;
12011 break;
12013 default:
12014 gdb_assert_not_reached ("no decoding pattern found");
12015 break;
12017 record_buf_mem[1] = tgt_mem_addr;
12018 arm_insn_r->mem_rec_count = 1;
12020 if (9 == arm_insn_r->opcode || 11 == arm_insn_r->opcode
12021 || 13 == arm_insn_r->opcode || 15 == arm_insn_r->opcode
12022 || 0 == arm_insn_r->opcode || 2 == arm_insn_r->opcode
12023 || 4 == arm_insn_r->opcode || 6 == arm_insn_r->opcode
12024 || 1 == arm_insn_r->opcode || 3 == arm_insn_r->opcode
12025 || 5 == arm_insn_r->opcode || 7 == arm_insn_r->opcode
12028 /* Rn is going to be changed in pre-indexed mode and
12029 post-indexed mode as well. */
12030 record_buf[0] = reg_src2;
12031 arm_insn_r->reg_rec_count = 1;
12034 else
12036 /* Store insn, scaled register offset; scaled pre-indexed. */
12037 offset_12 = bits (arm_insn_r->arm_insn, 5, 6);
12038 /* Get Rm. */
12039 reg_src1 = bits (arm_insn_r->arm_insn, 0, 3);
12040 /* Get Rn. */
12041 reg_src2 = bits (arm_insn_r->arm_insn, 16, 19);
12042 /* Get shift_imm. */
12043 shift_imm = bits (arm_insn_r->arm_insn, 7, 11);
12044 regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval[0]);
12045 regcache_raw_read_signed (reg_cache, reg_src1, &s_word);
12046 regcache_raw_read_unsigned (reg_cache, reg_src2, &u_regval[1]);
12047 /* Offset_12 used as shift. */
12048 switch (offset_12)
12050 case 0:
12051 /* Offset_12 used as index. */
12052 offset_12 = u_regval[0] << shift_imm;
12053 break;
12055 case 1:
12056 offset_12 = (!shift_imm)?0:u_regval[0] >> shift_imm;
12057 break;
12059 case 2:
12060 if (!shift_imm)
12062 if (bit (u_regval[0], 31))
12064 offset_12 = 0xFFFFFFFF;
12066 else
12068 offset_12 = 0;
12071 else
12073 /* This is arithmetic shift. */
12074 offset_12 = s_word >> shift_imm;
12076 break;
12078 case 3:
12079 if (!shift_imm)
12081 regcache_raw_read_unsigned (reg_cache, ARM_PS_REGNUM,
12082 &u_regval[1]);
12083 /* Get C flag value and shift it by 31. */
12084 offset_12 = (((bit (u_regval[1], 29)) << 31) \
12085 | (u_regval[0]) >> 1);
12087 else
12089 offset_12 = (u_regval[0] >> shift_imm) \
12090 | (u_regval[0] <<
12091 (sizeof(uint32_t) - shift_imm));
12093 break;
12095 default:
12096 gdb_assert_not_reached ("no decoding pattern found");
12097 break;
12100 regcache_raw_read_unsigned (reg_cache, reg_src2, &u_regval[1]);
12101 /* bit U set. */
12102 if (bit (arm_insn_r->arm_insn, 23))
12104 tgt_mem_addr = u_regval[1] + offset_12;
12106 else
12108 tgt_mem_addr = u_regval[1] - offset_12;
12111 switch (arm_insn_r->opcode)
12113 /* STR. */
12114 case 8:
12115 case 12:
12116 /* STR. */
12117 case 9:
12118 case 13:
12119 /* STRT. */
12120 case 1:
12121 case 5:
12122 /* STR. */
12123 case 0:
12124 case 4:
12125 record_buf_mem[0] = 4;
12126 break;
12128 /* STRB. */
12129 case 10:
12130 case 14:
12131 /* STRB. */
12132 case 11:
12133 case 15:
12134 /* STRBT. */
12135 case 3:
12136 case 7:
12137 /* STRB. */
12138 case 2:
12139 case 6:
12140 record_buf_mem[0] = 1;
12141 break;
12143 default:
12144 gdb_assert_not_reached ("no decoding pattern found");
12145 break;
12147 record_buf_mem[1] = tgt_mem_addr;
12148 arm_insn_r->mem_rec_count = 1;
12150 if (9 == arm_insn_r->opcode || 11 == arm_insn_r->opcode
12151 || 13 == arm_insn_r->opcode || 15 == arm_insn_r->opcode
12152 || 0 == arm_insn_r->opcode || 2 == arm_insn_r->opcode
12153 || 4 == arm_insn_r->opcode || 6 == arm_insn_r->opcode
12154 || 1 == arm_insn_r->opcode || 3 == arm_insn_r->opcode
12155 || 5 == arm_insn_r->opcode || 7 == arm_insn_r->opcode
12158 /* Rn is going to be changed in register scaled pre-indexed
12159 mode,and scaled post indexed mode. */
12160 record_buf[0] = reg_src2;
12161 arm_insn_r->reg_rec_count = 1;
12166 REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
12167 MEM_ALLOC (arm_insn_r->arm_mems, arm_insn_r->mem_rec_count, record_buf_mem);
12168 return 0;
12171 /* Handle ARM mode instructions with opcode 100. */
12173 static int
12174 arm_record_ld_st_multiple (arm_insn_decode_record *arm_insn_r)
12176 struct regcache *reg_cache = arm_insn_r->regcache;
12177 uint32_t register_count = 0, register_bits;
12178 uint32_t reg_base, addr_mode;
12179 uint32_t record_buf[24], record_buf_mem[48];
12180 uint32_t wback;
12181 ULONGEST u_regval;
12183 /* Fetch the list of registers. */
12184 register_bits = bits (arm_insn_r->arm_insn, 0, 15);
12185 arm_insn_r->reg_rec_count = 0;
12187 /* Fetch the base register that contains the address we are loading data
12188 to. */
12189 reg_base = bits (arm_insn_r->arm_insn, 16, 19);
12191 /* Calculate wback. */
12192 wback = (bit (arm_insn_r->arm_insn, 21) == 1);
12194 if (bit (arm_insn_r->arm_insn, INSN_S_L_BIT_NUM))
12196 /* LDM/LDMIA/LDMFD, LDMDA/LDMFA, LDMDB and LDMIB. */
12198 /* Find out which registers are going to be loaded from memory. */
12199 while (register_bits)
12201 if (register_bits & 0x00000001)
12202 record_buf[arm_insn_r->reg_rec_count++] = register_count;
12203 register_bits = register_bits >> 1;
12204 register_count++;
12208 /* If wback is true, also save the base register, which is going to be
12209 written to. */
12210 if (wback)
12211 record_buf[arm_insn_r->reg_rec_count++] = reg_base;
12213 /* Save the CPSR register. */
12214 record_buf[arm_insn_r->reg_rec_count++] = ARM_PS_REGNUM;
12216 else
12218 /* STM (STMIA, STMEA), STMDA (STMED), STMDB (STMFD) and STMIB (STMFA). */
12220 addr_mode = bits (arm_insn_r->arm_insn, 23, 24);
12222 regcache_raw_read_unsigned (reg_cache, reg_base, &u_regval);
12224 /* Find out how many registers are going to be stored to memory. */
12225 while (register_bits)
12227 if (register_bits & 0x00000001)
12228 register_count++;
12229 register_bits = register_bits >> 1;
12232 switch (addr_mode)
12234 /* STMDA (STMED): Decrement after. */
12235 case 0:
12236 record_buf_mem[1] = (uint32_t) u_regval
12237 - register_count * ARM_INT_REGISTER_SIZE + 4;
12238 break;
12239 /* STM (STMIA, STMEA): Increment after. */
12240 case 1:
12241 record_buf_mem[1] = (uint32_t) u_regval;
12242 break;
12243 /* STMDB (STMFD): Decrement before. */
12244 case 2:
12245 record_buf_mem[1] = (uint32_t) u_regval
12246 - register_count * ARM_INT_REGISTER_SIZE;
12247 break;
12248 /* STMIB (STMFA): Increment before. */
12249 case 3:
12250 record_buf_mem[1] = (uint32_t) u_regval + ARM_INT_REGISTER_SIZE;
12251 break;
12252 default:
12253 gdb_assert_not_reached ("no decoding pattern found");
12254 break;
12257 record_buf_mem[0] = register_count * ARM_INT_REGISTER_SIZE;
12258 arm_insn_r->mem_rec_count = 1;
12260 /* If wback is true, also save the base register, which is going to be
12261 written to. */
12262 if (wback)
12263 record_buf[arm_insn_r->reg_rec_count++] = reg_base;
12266 REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
12267 MEM_ALLOC (arm_insn_r->arm_mems, arm_insn_r->mem_rec_count, record_buf_mem);
12268 return 0;
12271 /* Handling opcode 101 insns. */
12273 static int
12274 arm_record_b_bl (arm_insn_decode_record *arm_insn_r)
12276 uint32_t record_buf[8];
12278 /* Handle B, BL, BLX(1) insns. */
12279 /* B simply branches so we do nothing here. */
12280 /* Note: BLX(1) doesnt fall here but instead it falls into
12281 extension space. */
12282 if (bit (arm_insn_r->arm_insn, 24))
12284 record_buf[0] = ARM_LR_REGNUM;
12285 arm_insn_r->reg_rec_count = 1;
12288 REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
12290 return 0;
12293 static int
12294 arm_record_unsupported_insn (arm_insn_decode_record *arm_insn_r)
12296 gdb_printf (gdb_stderr,
12297 _("Process record does not support instruction "
12298 "0x%0x at address %s.\n"),arm_insn_r->arm_insn,
12299 paddress (arm_insn_r->gdbarch, arm_insn_r->this_addr));
12301 return -1;
12304 /* Record handler for vector data transfer instructions. */
12306 static int
12307 arm_record_vdata_transfer_insn (arm_insn_decode_record *arm_insn_r)
12309 uint32_t bits_a, bit_c, bit_l, reg_t, reg_v;
12310 uint32_t record_buf[4];
12312 reg_t = bits (arm_insn_r->arm_insn, 12, 15);
12313 reg_v = bits (arm_insn_r->arm_insn, 21, 23);
12314 bits_a = bits (arm_insn_r->arm_insn, 21, 23);
12315 bit_l = bit (arm_insn_r->arm_insn, 20);
12316 bit_c = bit (arm_insn_r->arm_insn, 8);
12318 /* Handle VMOV instruction. */
12319 if (bit_l && bit_c)
12321 record_buf[0] = reg_t;
12322 arm_insn_r->reg_rec_count = 1;
12324 else if (bit_l && !bit_c)
12326 /* Handle VMOV instruction. */
12327 if (bits_a == 0x00)
12329 record_buf[0] = reg_t;
12330 arm_insn_r->reg_rec_count = 1;
12332 /* Handle VMRS instruction. */
12333 else if (bits_a == 0x07)
12335 if (reg_t == 15)
12336 reg_t = ARM_PS_REGNUM;
12338 record_buf[0] = reg_t;
12339 arm_insn_r->reg_rec_count = 1;
12342 else if (!bit_l && !bit_c)
12344 /* Handle VMOV instruction. */
12345 if (bits_a == 0x00)
12347 record_buf[0] = ARM_D0_REGNUM + reg_v;
12349 arm_insn_r->reg_rec_count = 1;
12351 /* Handle VMSR instruction. */
12352 else if (bits_a == 0x07)
12354 record_buf[0] = ARM_FPSCR_REGNUM;
12355 arm_insn_r->reg_rec_count = 1;
12358 else if (!bit_l && bit_c)
12360 /* Handle VMOV instruction. */
12361 if (!(bits_a & 0x04))
12363 record_buf[0] = (reg_v | (bit (arm_insn_r->arm_insn, 7) << 4))
12364 + ARM_D0_REGNUM;
12365 arm_insn_r->reg_rec_count = 1;
12367 /* Handle VDUP instruction. */
12368 else
12370 if (bit (arm_insn_r->arm_insn, 21))
12372 reg_v = reg_v | (bit (arm_insn_r->arm_insn, 7) << 4);
12373 record_buf[0] = reg_v + ARM_D0_REGNUM;
12374 record_buf[1] = reg_v + ARM_D0_REGNUM + 1;
12375 arm_insn_r->reg_rec_count = 2;
12377 else
12379 reg_v = reg_v | (bit (arm_insn_r->arm_insn, 7) << 4);
12380 record_buf[0] = reg_v + ARM_D0_REGNUM;
12381 arm_insn_r->reg_rec_count = 1;
12386 REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
12387 return 0;
12390 /* Record handler for extension register load/store instructions. */
12392 static int
12393 arm_record_exreg_ld_st_insn (arm_insn_decode_record *arm_insn_r)
12395 uint32_t opcode, single_reg;
12396 uint8_t op_vldm_vstm;
12397 uint32_t record_buf[8], record_buf_mem[128];
12398 ULONGEST u_regval = 0;
12400 struct regcache *reg_cache = arm_insn_r->regcache;
12402 opcode = bits (arm_insn_r->arm_insn, 20, 24);
12403 single_reg = !bit (arm_insn_r->arm_insn, 8);
12404 op_vldm_vstm = opcode & 0x1b;
12406 /* Handle VMOV instructions. */
12407 if ((opcode & 0x1e) == 0x04)
12409 if (bit (arm_insn_r->arm_insn, 20)) /* to_arm_registers bit 20? */
12411 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
12412 record_buf[1] = bits (arm_insn_r->arm_insn, 16, 19);
12413 arm_insn_r->reg_rec_count = 2;
12415 else
12417 uint8_t reg_m = bits (arm_insn_r->arm_insn, 0, 3);
12418 uint8_t bit_m = bit (arm_insn_r->arm_insn, 5);
12420 if (single_reg)
12422 /* The first S register number m is REG_M:M (M is bit 5),
12423 the corresponding D register number is REG_M:M / 2, which
12424 is REG_M. */
12425 record_buf[arm_insn_r->reg_rec_count++] = ARM_D0_REGNUM + reg_m;
12426 /* The second S register number is REG_M:M + 1, the
12427 corresponding D register number is (REG_M:M + 1) / 2.
12428 IOW, if bit M is 1, the first and second S registers
12429 are mapped to different D registers, otherwise, they are
12430 in the same D register. */
12431 if (bit_m)
12433 record_buf[arm_insn_r->reg_rec_count++]
12434 = ARM_D0_REGNUM + reg_m + 1;
12437 else
12439 record_buf[0] = ((bit_m << 4) + reg_m + ARM_D0_REGNUM);
12440 arm_insn_r->reg_rec_count = 1;
12444 /* Handle VSTM and VPUSH instructions. */
12445 else if (op_vldm_vstm == 0x08 || op_vldm_vstm == 0x0a
12446 || op_vldm_vstm == 0x12)
12448 uint32_t start_address, reg_rn, imm_off32, imm_off8, memory_count;
12449 uint32_t memory_index = 0;
12451 reg_rn = bits (arm_insn_r->arm_insn, 16, 19);
12452 regcache_raw_read_unsigned (reg_cache, reg_rn, &u_regval);
12453 imm_off8 = bits (arm_insn_r->arm_insn, 0, 7);
12454 imm_off32 = imm_off8 << 2;
12455 memory_count = imm_off8;
12457 if (bit (arm_insn_r->arm_insn, 23))
12458 start_address = u_regval;
12459 else
12460 start_address = u_regval - imm_off32;
12462 if (bit (arm_insn_r->arm_insn, 21))
12464 record_buf[0] = reg_rn;
12465 arm_insn_r->reg_rec_count = 1;
12468 while (memory_count > 0)
12470 if (single_reg)
12472 record_buf_mem[memory_index] = 4;
12473 record_buf_mem[memory_index + 1] = start_address;
12474 start_address = start_address + 4;
12475 memory_index = memory_index + 2;
12477 else
12479 record_buf_mem[memory_index] = 4;
12480 record_buf_mem[memory_index + 1] = start_address;
12481 record_buf_mem[memory_index + 2] = 4;
12482 record_buf_mem[memory_index + 3] = start_address + 4;
12483 start_address = start_address + 8;
12484 memory_index = memory_index + 4;
12486 memory_count--;
12488 arm_insn_r->mem_rec_count = (memory_index >> 1);
12490 /* Handle VLDM instructions. */
12491 else if (op_vldm_vstm == 0x09 || op_vldm_vstm == 0x0b
12492 || op_vldm_vstm == 0x13)
12494 uint32_t reg_count, reg_vd;
12495 uint32_t reg_index = 0;
12496 uint32_t bit_d = bit (arm_insn_r->arm_insn, 22);
12498 reg_vd = bits (arm_insn_r->arm_insn, 12, 15);
12499 reg_count = bits (arm_insn_r->arm_insn, 0, 7);
12501 /* REG_VD is the first D register number. If the instruction
12502 loads memory to S registers (SINGLE_REG is TRUE), the register
12503 number is (REG_VD << 1 | bit D), so the corresponding D
12504 register number is (REG_VD << 1 | bit D) / 2 = REG_VD. */
12505 if (!single_reg)
12506 reg_vd = reg_vd | (bit_d << 4);
12508 if (bit (arm_insn_r->arm_insn, 21) /* write back */)
12509 record_buf[reg_index++] = bits (arm_insn_r->arm_insn, 16, 19);
12511 /* If the instruction loads memory to D register, REG_COUNT should
12512 be divided by 2, according to the ARM Architecture Reference
12513 Manual. If the instruction loads memory to S register, divide by
12514 2 as well because two S registers are mapped to D register. */
12515 reg_count = reg_count / 2;
12516 if (single_reg && bit_d)
12518 /* Increase the register count if S register list starts from
12519 an odd number (bit d is one). */
12520 reg_count++;
12523 while (reg_count > 0)
12525 record_buf[reg_index++] = ARM_D0_REGNUM + reg_vd + reg_count - 1;
12526 reg_count--;
12528 arm_insn_r->reg_rec_count = reg_index;
12530 /* VSTR Vector store register. */
12531 else if ((opcode & 0x13) == 0x10)
12533 uint32_t start_address, reg_rn, imm_off32, imm_off8;
12534 uint32_t memory_index = 0;
12536 reg_rn = bits (arm_insn_r->arm_insn, 16, 19);
12537 regcache_raw_read_unsigned (reg_cache, reg_rn, &u_regval);
12538 imm_off8 = bits (arm_insn_r->arm_insn, 0, 7);
12539 imm_off32 = imm_off8 << 2;
12541 if (bit (arm_insn_r->arm_insn, 23))
12542 start_address = u_regval + imm_off32;
12543 else
12544 start_address = u_regval - imm_off32;
12546 if (single_reg)
12548 record_buf_mem[memory_index] = 4;
12549 record_buf_mem[memory_index + 1] = start_address;
12550 arm_insn_r->mem_rec_count = 1;
12552 else
12554 record_buf_mem[memory_index] = 4;
12555 record_buf_mem[memory_index + 1] = start_address;
12556 record_buf_mem[memory_index + 2] = 4;
12557 record_buf_mem[memory_index + 3] = start_address + 4;
12558 arm_insn_r->mem_rec_count = 2;
12561 /* VLDR Vector load register. */
12562 else if ((opcode & 0x13) == 0x11)
12564 uint32_t reg_vd = bits (arm_insn_r->arm_insn, 12, 15);
12566 if (!single_reg)
12568 reg_vd = reg_vd | (bit (arm_insn_r->arm_insn, 22) << 4);
12569 record_buf[0] = ARM_D0_REGNUM + reg_vd;
12571 else
12573 reg_vd = (reg_vd << 1) | bit (arm_insn_r->arm_insn, 22);
12574 /* Record register D rather than pseudo register S. */
12575 record_buf[0] = ARM_D0_REGNUM + reg_vd / 2;
12577 arm_insn_r->reg_rec_count = 1;
12580 REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
12581 MEM_ALLOC (arm_insn_r->arm_mems, arm_insn_r->mem_rec_count, record_buf_mem);
12582 return 0;
12585 /* Record handler for arm/thumb mode VFP data processing instructions. */
12587 static int
12588 arm_record_vfp_data_proc_insn (arm_insn_decode_record *arm_insn_r)
12590 uint32_t opc1, opc2, opc3, dp_op_sz, bit_d, reg_vd;
12591 uint32_t record_buf[4];
12592 enum insn_types {INSN_T0, INSN_T1, INSN_T2, INSN_T3, INSN_INV};
12593 enum insn_types curr_insn_type = INSN_INV;
12595 reg_vd = bits (arm_insn_r->arm_insn, 12, 15);
12596 opc1 = bits (arm_insn_r->arm_insn, 20, 23);
12597 opc2 = bits (arm_insn_r->arm_insn, 16, 19);
12598 opc3 = bits (arm_insn_r->arm_insn, 6, 7);
12599 dp_op_sz = bit (arm_insn_r->arm_insn, 8);
12600 bit_d = bit (arm_insn_r->arm_insn, 22);
12601 /* Mask off the "D" bit. */
12602 opc1 = opc1 & ~0x04;
12604 /* Handle VMLA, VMLS. */
12605 if (opc1 == 0x00)
12607 if (bit (arm_insn_r->arm_insn, 10))
12609 if (bit (arm_insn_r->arm_insn, 6))
12610 curr_insn_type = INSN_T0;
12611 else
12612 curr_insn_type = INSN_T1;
12614 else
12616 if (dp_op_sz)
12617 curr_insn_type = INSN_T1;
12618 else
12619 curr_insn_type = INSN_T2;
12622 /* Handle VNMLA, VNMLS, VNMUL. */
12623 else if (opc1 == 0x01)
12625 if (dp_op_sz)
12626 curr_insn_type = INSN_T1;
12627 else
12628 curr_insn_type = INSN_T2;
12630 /* Handle VMUL. */
12631 else if (opc1 == 0x02 && !(opc3 & 0x01))
12633 if (bit (arm_insn_r->arm_insn, 10))
12635 if (bit (arm_insn_r->arm_insn, 6))
12636 curr_insn_type = INSN_T0;
12637 else
12638 curr_insn_type = INSN_T1;
12640 else
12642 if (dp_op_sz)
12643 curr_insn_type = INSN_T1;
12644 else
12645 curr_insn_type = INSN_T2;
12648 /* Handle VADD, VSUB. */
12649 else if (opc1 == 0x03)
12651 if (!bit (arm_insn_r->arm_insn, 9))
12653 if (bit (arm_insn_r->arm_insn, 6))
12654 curr_insn_type = INSN_T0;
12655 else
12656 curr_insn_type = INSN_T1;
12658 else
12660 if (dp_op_sz)
12661 curr_insn_type = INSN_T1;
12662 else
12663 curr_insn_type = INSN_T2;
12666 /* Handle VDIV. */
12667 else if (opc1 == 0x08)
12669 if (dp_op_sz)
12670 curr_insn_type = INSN_T1;
12671 else
12672 curr_insn_type = INSN_T2;
12674 /* Handle all other vfp data processing instructions. */
12675 else if (opc1 == 0x0b)
12677 /* Handle VMOV. */
12678 if (!(opc3 & 0x01) || (opc2 == 0x00 && opc3 == 0x01))
12680 if (bit (arm_insn_r->arm_insn, 4))
12682 if (bit (arm_insn_r->arm_insn, 6))
12683 curr_insn_type = INSN_T0;
12684 else
12685 curr_insn_type = INSN_T1;
12687 else
12689 if (dp_op_sz)
12690 curr_insn_type = INSN_T1;
12691 else
12692 curr_insn_type = INSN_T2;
12695 /* Handle VNEG and VABS. */
12696 else if ((opc2 == 0x01 && opc3 == 0x01)
12697 || (opc2 == 0x00 && opc3 == 0x03))
12699 if (!bit (arm_insn_r->arm_insn, 11))
12701 if (bit (arm_insn_r->arm_insn, 6))
12702 curr_insn_type = INSN_T0;
12703 else
12704 curr_insn_type = INSN_T1;
12706 else
12708 if (dp_op_sz)
12709 curr_insn_type = INSN_T1;
12710 else
12711 curr_insn_type = INSN_T2;
12714 /* Handle VSQRT. */
12715 else if (opc2 == 0x01 && opc3 == 0x03)
12717 if (dp_op_sz)
12718 curr_insn_type = INSN_T1;
12719 else
12720 curr_insn_type = INSN_T2;
12722 /* Handle VCVT. */
12723 else if (opc2 == 0x07 && opc3 == 0x03)
12725 if (!dp_op_sz)
12726 curr_insn_type = INSN_T1;
12727 else
12728 curr_insn_type = INSN_T2;
12730 else if (opc3 & 0x01)
12732 /* Handle VCVT. */
12733 if ((opc2 == 0x08) || (opc2 & 0x0e) == 0x0c)
12735 if (!bit (arm_insn_r->arm_insn, 18))
12736 curr_insn_type = INSN_T2;
12737 else
12739 if (dp_op_sz)
12740 curr_insn_type = INSN_T1;
12741 else
12742 curr_insn_type = INSN_T2;
12745 /* Handle VCVT. */
12746 else if ((opc2 & 0x0e) == 0x0a || (opc2 & 0x0e) == 0x0e)
12748 if (dp_op_sz)
12749 curr_insn_type = INSN_T1;
12750 else
12751 curr_insn_type = INSN_T2;
12753 /* Handle VCVTB, VCVTT. */
12754 else if ((opc2 & 0x0e) == 0x02)
12755 curr_insn_type = INSN_T2;
12756 /* Handle VCMP, VCMPE. */
12757 else if ((opc2 & 0x0e) == 0x04)
12758 curr_insn_type = INSN_T3;
12762 switch (curr_insn_type)
12764 case INSN_T0:
12765 reg_vd = reg_vd | (bit_d << 4);
12766 record_buf[0] = reg_vd + ARM_D0_REGNUM;
12767 record_buf[1] = reg_vd + ARM_D0_REGNUM + 1;
12768 arm_insn_r->reg_rec_count = 2;
12769 break;
12771 case INSN_T1:
12772 reg_vd = reg_vd | (bit_d << 4);
12773 record_buf[0] = reg_vd + ARM_D0_REGNUM;
12774 arm_insn_r->reg_rec_count = 1;
12775 break;
12777 case INSN_T2:
12778 reg_vd = (reg_vd << 1) | bit_d;
12779 record_buf[0] = reg_vd + ARM_D0_REGNUM;
12780 arm_insn_r->reg_rec_count = 1;
12781 break;
12783 case INSN_T3:
12784 record_buf[0] = ARM_FPSCR_REGNUM;
12785 arm_insn_r->reg_rec_count = 1;
12786 break;
12788 default:
12789 gdb_assert_not_reached ("no decoding pattern found");
12790 break;
12793 REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
12794 return 0;
12797 /* Handling opcode 110 insns. */
12799 static int
12800 arm_record_asimd_vfp_coproc (arm_insn_decode_record *arm_insn_r)
12802 uint32_t op1, op1_ebit, coproc;
12804 coproc = bits (arm_insn_r->arm_insn, 8, 11);
12805 op1 = bits (arm_insn_r->arm_insn, 20, 25);
12806 op1_ebit = bit (arm_insn_r->arm_insn, 20);
12808 if ((coproc & 0x0e) == 0x0a)
12810 /* Handle extension register ld/st instructions. */
12811 if (!(op1 & 0x20))
12812 return arm_record_exreg_ld_st_insn (arm_insn_r);
12814 /* 64-bit transfers between arm core and extension registers. */
12815 if ((op1 & 0x3e) == 0x04)
12816 return arm_record_exreg_ld_st_insn (arm_insn_r);
12818 else
12820 /* Handle coprocessor ld/st instructions. */
12821 if (!(op1 & 0x3a))
12823 /* Store. */
12824 if (!op1_ebit)
12825 return arm_record_unsupported_insn (arm_insn_r);
12826 else
12827 /* Load. */
12828 return arm_record_unsupported_insn (arm_insn_r);
12831 /* Move to coprocessor from two arm core registers. */
12832 if (op1 == 0x4)
12833 return arm_record_unsupported_insn (arm_insn_r);
12835 /* Move to two arm core registers from coprocessor. */
12836 if (op1 == 0x5)
12838 uint32_t reg_t[2];
12840 reg_t[0] = bits (arm_insn_r->arm_insn, 12, 15);
12841 reg_t[1] = bits (arm_insn_r->arm_insn, 16, 19);
12842 arm_insn_r->reg_rec_count = 2;
12844 REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, reg_t);
12845 return 0;
12848 return arm_record_unsupported_insn (arm_insn_r);
12851 /* Handling opcode 111 insns. */
12853 static int
12854 arm_record_coproc_data_proc (arm_insn_decode_record *arm_insn_r)
12856 uint32_t op, op1_ebit, coproc, bits_24_25;
12857 arm_gdbarch_tdep *tdep
12858 = gdbarch_tdep<arm_gdbarch_tdep> (arm_insn_r->gdbarch);
12859 struct regcache *reg_cache = arm_insn_r->regcache;
12861 arm_insn_r->opcode = bits (arm_insn_r->arm_insn, 24, 27);
12862 coproc = bits (arm_insn_r->arm_insn, 8, 11);
12863 op1_ebit = bit (arm_insn_r->arm_insn, 20);
12864 op = bit (arm_insn_r->arm_insn, 4);
12865 bits_24_25 = bits (arm_insn_r->arm_insn, 24, 25);
12867 /* Handle arm SWI/SVC system call instructions. */
12868 if (bits_24_25 == 0x3)
12870 if (tdep->arm_syscall_record != NULL)
12872 ULONGEST svc_operand, svc_number;
12874 svc_operand = (0x00ffffff & arm_insn_r->arm_insn);
12876 if (svc_operand) /* OABI. */
12877 svc_number = svc_operand - 0x900000;
12878 else /* EABI. */
12879 regcache_raw_read_unsigned (reg_cache, 7, &svc_number);
12881 return tdep->arm_syscall_record (reg_cache, svc_number);
12883 else
12885 gdb_printf (gdb_stderr, _("no syscall record support\n"));
12886 return -1;
12889 else if (bits_24_25 == 0x02)
12891 if (op)
12893 if ((coproc & 0x0e) == 0x0a)
12895 /* 8, 16, and 32-bit transfer */
12896 return arm_record_vdata_transfer_insn (arm_insn_r);
12898 else
12900 if (op1_ebit)
12902 /* MRC, MRC2 */
12903 uint32_t record_buf[1];
12905 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
12906 if (record_buf[0] == 15)
12907 record_buf[0] = ARM_PS_REGNUM;
12909 arm_insn_r->reg_rec_count = 1;
12910 REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count,
12911 record_buf);
12912 return 0;
12914 else
12916 /* MCR, MCR2 */
12917 return -1;
12921 else
12923 if ((coproc & 0x0e) == 0x0a)
12925 /* VFP data-processing instructions. */
12926 return arm_record_vfp_data_proc_insn (arm_insn_r);
12928 else
12930 /* CDP, CDP2 */
12931 return -1;
12935 else
12937 unsigned int op1 = bits (arm_insn_r->arm_insn, 20, 25);
12939 if (op1 == 5)
12941 if ((coproc & 0x0e) != 0x0a)
12943 /* MRRC, MRRC2 */
12944 return -1;
12947 else if (op1 == 4 || op1 == 5)
12949 if ((coproc & 0x0e) == 0x0a)
12951 /* 64-bit transfers between ARM core and extension */
12952 return -1;
12954 else if (op1 == 4)
12956 /* MCRR, MCRR2 */
12957 return -1;
12960 else if (op1 == 0 || op1 == 1)
12962 /* UNDEFINED */
12963 return -1;
12965 else
12967 if ((coproc & 0x0e) == 0x0a)
12969 /* Extension register load/store */
12971 else
12973 /* STC, STC2, LDC, LDC2 */
12975 return -1;
12979 return -1;
12982 /* Handling opcode 000 insns. */
12984 static int
12985 thumb_record_shift_add_sub (arm_insn_decode_record *thumb_insn_r)
12987 uint32_t record_buf[8];
12988 uint32_t reg_src1 = 0;
12990 reg_src1 = bits (thumb_insn_r->arm_insn, 0, 2);
12992 record_buf[0] = ARM_PS_REGNUM;
12993 record_buf[1] = reg_src1;
12994 thumb_insn_r->reg_rec_count = 2;
12996 REG_ALLOC (thumb_insn_r->arm_regs, thumb_insn_r->reg_rec_count, record_buf);
12998 return 0;
13002 /* Handling opcode 001 insns. */
13004 static int
13005 thumb_record_add_sub_cmp_mov (arm_insn_decode_record *thumb_insn_r)
13007 uint32_t record_buf[8];
13008 uint32_t reg_src1 = 0;
13010 reg_src1 = bits (thumb_insn_r->arm_insn, 8, 10);
13012 record_buf[0] = ARM_PS_REGNUM;
13013 record_buf[1] = reg_src1;
13014 thumb_insn_r->reg_rec_count = 2;
13016 REG_ALLOC (thumb_insn_r->arm_regs, thumb_insn_r->reg_rec_count, record_buf);
13018 return 0;
13021 /* Handling opcode 010 insns. */
13023 static int
13024 thumb_record_ld_st_reg_offset (arm_insn_decode_record *thumb_insn_r)
13026 struct regcache *reg_cache = thumb_insn_r->regcache;
13027 uint32_t record_buf[8], record_buf_mem[8];
13029 uint32_t reg_src1 = 0, reg_src2 = 0;
13030 uint32_t opcode1 = 0, opcode2 = 0, opcode3 = 0;
13032 ULONGEST u_regval[2] = {0};
13034 opcode1 = bits (thumb_insn_r->arm_insn, 10, 12);
13036 if (bit (thumb_insn_r->arm_insn, 12))
13038 /* Handle load/store register offset. */
13039 uint32_t opB = bits (thumb_insn_r->arm_insn, 9, 11);
13041 if (in_inclusive_range (opB, 4U, 7U))
13043 /* LDR(2), LDRB(2) , LDRH(2), LDRSB, LDRSH. */
13044 reg_src1 = bits (thumb_insn_r->arm_insn,0, 2);
13045 record_buf[0] = reg_src1;
13046 thumb_insn_r->reg_rec_count = 1;
13048 else if (in_inclusive_range (opB, 0U, 2U))
13050 /* STR(2), STRB(2), STRH(2) . */
13051 reg_src1 = bits (thumb_insn_r->arm_insn, 3, 5);
13052 reg_src2 = bits (thumb_insn_r->arm_insn, 6, 8);
13053 regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval[0]);
13054 regcache_raw_read_unsigned (reg_cache, reg_src2, &u_regval[1]);
13055 if (0 == opB)
13056 record_buf_mem[0] = 4; /* STR (2). */
13057 else if (2 == opB)
13058 record_buf_mem[0] = 1; /* STRB (2). */
13059 else if (1 == opB)
13060 record_buf_mem[0] = 2; /* STRH (2). */
13061 record_buf_mem[1] = u_regval[0] + u_regval[1];
13062 thumb_insn_r->mem_rec_count = 1;
13065 else if (bit (thumb_insn_r->arm_insn, 11))
13067 /* Handle load from literal pool. */
13068 /* LDR(3). */
13069 reg_src1 = bits (thumb_insn_r->arm_insn, 8, 10);
13070 record_buf[0] = reg_src1;
13071 thumb_insn_r->reg_rec_count = 1;
13073 else if (opcode1)
13075 /* Special data instructions and branch and exchange */
13076 opcode2 = bits (thumb_insn_r->arm_insn, 8, 9);
13077 opcode3 = bits (thumb_insn_r->arm_insn, 0, 2);
13078 if ((3 == opcode2) && (!opcode3))
13080 /* Branch with exchange. */
13081 record_buf[0] = ARM_PS_REGNUM;
13082 thumb_insn_r->reg_rec_count = 1;
13084 else
13086 /* Format 8; special data processing insns. */
13087 record_buf[0] = ARM_PS_REGNUM;
13088 record_buf[1] = (bit (thumb_insn_r->arm_insn, 7) << 3
13089 | bits (thumb_insn_r->arm_insn, 0, 2));
13090 thumb_insn_r->reg_rec_count = 2;
13093 else
13095 /* Format 5; data processing insns. */
13096 reg_src1 = bits (thumb_insn_r->arm_insn, 0, 2);
13097 if (bit (thumb_insn_r->arm_insn, 7))
13099 reg_src1 = reg_src1 + 8;
13101 record_buf[0] = ARM_PS_REGNUM;
13102 record_buf[1] = reg_src1;
13103 thumb_insn_r->reg_rec_count = 2;
13106 REG_ALLOC (thumb_insn_r->arm_regs, thumb_insn_r->reg_rec_count, record_buf);
13107 MEM_ALLOC (thumb_insn_r->arm_mems, thumb_insn_r->mem_rec_count,
13108 record_buf_mem);
13110 return 0;
13113 /* Handling opcode 001 insns. */
13115 static int
13116 thumb_record_ld_st_imm_offset (arm_insn_decode_record *thumb_insn_r)
13118 struct regcache *reg_cache = thumb_insn_r->regcache;
13119 uint32_t record_buf[8], record_buf_mem[8];
13121 uint32_t reg_src1 = 0;
13122 uint32_t opcode = 0, immed_5 = 0;
13124 ULONGEST u_regval = 0;
13126 opcode = bits (thumb_insn_r->arm_insn, 11, 12);
13128 if (opcode)
13130 /* LDR(1). */
13131 reg_src1 = bits (thumb_insn_r->arm_insn, 0, 2);
13132 record_buf[0] = reg_src1;
13133 thumb_insn_r->reg_rec_count = 1;
13135 else
13137 /* STR(1). */
13138 reg_src1 = bits (thumb_insn_r->arm_insn, 3, 5);
13139 immed_5 = bits (thumb_insn_r->arm_insn, 6, 10);
13140 regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval);
13141 record_buf_mem[0] = 4;
13142 record_buf_mem[1] = u_regval + (immed_5 * 4);
13143 thumb_insn_r->mem_rec_count = 1;
13146 REG_ALLOC (thumb_insn_r->arm_regs, thumb_insn_r->reg_rec_count, record_buf);
13147 MEM_ALLOC (thumb_insn_r->arm_mems, thumb_insn_r->mem_rec_count,
13148 record_buf_mem);
13150 return 0;
13153 /* Handling opcode 100 insns. */
13155 static int
13156 thumb_record_ld_st_stack (arm_insn_decode_record *thumb_insn_r)
13158 struct regcache *reg_cache = thumb_insn_r->regcache;
13159 uint32_t record_buf[8], record_buf_mem[8];
13161 uint32_t reg_src1 = 0;
13162 uint32_t opcode = 0, immed_8 = 0, immed_5 = 0;
13164 ULONGEST u_regval = 0;
13166 opcode = bits (thumb_insn_r->arm_insn, 11, 12);
13168 if (3 == opcode)
13170 /* LDR(4). */
13171 reg_src1 = bits (thumb_insn_r->arm_insn, 8, 10);
13172 record_buf[0] = reg_src1;
13173 thumb_insn_r->reg_rec_count = 1;
13175 else if (1 == opcode)
13177 /* LDRH(1). */
13178 reg_src1 = bits (thumb_insn_r->arm_insn, 0, 2);
13179 record_buf[0] = reg_src1;
13180 thumb_insn_r->reg_rec_count = 1;
13182 else if (2 == opcode)
13184 /* STR(3). */
13185 immed_8 = bits (thumb_insn_r->arm_insn, 0, 7);
13186 regcache_raw_read_unsigned (reg_cache, ARM_SP_REGNUM, &u_regval);
13187 record_buf_mem[0] = 4;
13188 record_buf_mem[1] = u_regval + (immed_8 * 4);
13189 thumb_insn_r->mem_rec_count = 1;
13191 else if (0 == opcode)
13193 /* STRH(1). */
13194 immed_5 = bits (thumb_insn_r->arm_insn, 6, 10);
13195 reg_src1 = bits (thumb_insn_r->arm_insn, 3, 5);
13196 regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval);
13197 record_buf_mem[0] = 2;
13198 record_buf_mem[1] = u_regval + (immed_5 * 2);
13199 thumb_insn_r->mem_rec_count = 1;
13202 REG_ALLOC (thumb_insn_r->arm_regs, thumb_insn_r->reg_rec_count, record_buf);
13203 MEM_ALLOC (thumb_insn_r->arm_mems, thumb_insn_r->mem_rec_count,
13204 record_buf_mem);
13206 return 0;
13209 /* Handling opcode 101 insns. */
13211 static int
13212 thumb_record_misc (arm_insn_decode_record *thumb_insn_r)
13214 struct regcache *reg_cache = thumb_insn_r->regcache;
13216 uint32_t opcode = 0;
13217 uint32_t register_bits = 0, register_count = 0;
13218 uint32_t index = 0, start_address = 0;
13219 uint32_t record_buf[24], record_buf_mem[48];
13220 uint32_t reg_src1;
13222 ULONGEST u_regval = 0;
13224 opcode = bits (thumb_insn_r->arm_insn, 11, 12);
13226 if (opcode == 0 || opcode == 1)
13228 /* ADR and ADD (SP plus immediate) */
13230 reg_src1 = bits (thumb_insn_r->arm_insn, 8, 10);
13231 record_buf[0] = reg_src1;
13232 thumb_insn_r->reg_rec_count = 1;
13234 else
13236 /* Miscellaneous 16-bit instructions */
13237 uint32_t opcode2 = bits (thumb_insn_r->arm_insn, 8, 11);
13239 switch (opcode2)
13241 case 6:
13242 /* SETEND and CPS */
13243 break;
13244 case 0:
13245 /* ADD/SUB (SP plus immediate) */
13246 reg_src1 = bits (thumb_insn_r->arm_insn, 8, 10);
13247 record_buf[0] = ARM_SP_REGNUM;
13248 thumb_insn_r->reg_rec_count = 1;
13249 break;
13250 case 1: /* fall through */
13251 case 3: /* fall through */
13252 case 9: /* fall through */
13253 case 11:
13254 /* CBNZ, CBZ */
13255 break;
13256 case 2:
13257 /* SXTH, SXTB, UXTH, UXTB */
13258 record_buf[0] = bits (thumb_insn_r->arm_insn, 0, 2);
13259 thumb_insn_r->reg_rec_count = 1;
13260 break;
13261 case 4: /* fall through */
13262 case 5:
13263 /* PUSH. */
13264 register_bits = bits (thumb_insn_r->arm_insn, 0, 7);
13265 regcache_raw_read_unsigned (reg_cache, ARM_SP_REGNUM, &u_regval);
13266 while (register_bits)
13268 if (register_bits & 0x00000001)
13269 register_count++;
13270 register_bits = register_bits >> 1;
13272 start_address = u_regval - \
13273 (4 * (bit (thumb_insn_r->arm_insn, 8) + register_count));
13274 thumb_insn_r->mem_rec_count = register_count;
13275 while (register_count)
13277 record_buf_mem[(register_count * 2) - 1] = start_address;
13278 record_buf_mem[(register_count * 2) - 2] = 4;
13279 start_address = start_address + 4;
13280 register_count--;
13282 record_buf[0] = ARM_SP_REGNUM;
13283 thumb_insn_r->reg_rec_count = 1;
13284 break;
13285 case 10:
13286 /* REV, REV16, REVSH */
13287 record_buf[0] = bits (thumb_insn_r->arm_insn, 0, 2);
13288 thumb_insn_r->reg_rec_count = 1;
13289 break;
13290 case 12: /* fall through */
13291 case 13:
13292 /* POP. */
13293 register_bits = bits (thumb_insn_r->arm_insn, 0, 7);
13294 while (register_bits)
13296 if (register_bits & 0x00000001)
13297 record_buf[index++] = register_count;
13298 register_bits = register_bits >> 1;
13299 register_count++;
13301 record_buf[index++] = ARM_PS_REGNUM;
13302 record_buf[index++] = ARM_SP_REGNUM;
13303 thumb_insn_r->reg_rec_count = index;
13304 break;
13305 case 0xe:
13306 /* BKPT insn. */
13307 /* Handle enhanced software breakpoint insn, BKPT. */
13308 /* CPSR is changed to be executed in ARM state, disabling normal
13309 interrupts, entering abort mode. */
13310 /* According to high vector configuration PC is set. */
13311 /* User hits breakpoint and type reverse, in that case, we need to go back with
13312 previous CPSR and Program Counter. */
13313 record_buf[0] = ARM_PS_REGNUM;
13314 record_buf[1] = ARM_LR_REGNUM;
13315 thumb_insn_r->reg_rec_count = 2;
13316 /* We need to save SPSR value, which is not yet done. */
13317 gdb_printf (gdb_stderr,
13318 _("Process record does not support instruction "
13319 "0x%0x at address %s.\n"),
13320 thumb_insn_r->arm_insn,
13321 paddress (thumb_insn_r->gdbarch,
13322 thumb_insn_r->this_addr));
13323 return -1;
13325 case 0xf:
13326 /* If-Then, and hints */
13327 break;
13328 default:
13329 return -1;
13333 REG_ALLOC (thumb_insn_r->arm_regs, thumb_insn_r->reg_rec_count, record_buf);
13334 MEM_ALLOC (thumb_insn_r->arm_mems, thumb_insn_r->mem_rec_count,
13335 record_buf_mem);
13337 return 0;
13340 /* Handling opcode 110 insns. */
13342 static int
13343 thumb_record_ldm_stm_swi (arm_insn_decode_record *thumb_insn_r)
13345 arm_gdbarch_tdep *tdep
13346 = gdbarch_tdep<arm_gdbarch_tdep> (thumb_insn_r->gdbarch);
13347 struct regcache *reg_cache = thumb_insn_r->regcache;
13349 uint32_t ret = 0; /* function return value: -1:record failure ; 0:success */
13350 uint32_t reg_src1 = 0;
13351 uint32_t opcode1 = 0, opcode2 = 0, register_bits = 0, register_count = 0;
13352 uint32_t index = 0, start_address = 0;
13353 uint32_t record_buf[24], record_buf_mem[48];
13355 ULONGEST u_regval = 0;
13357 opcode1 = bits (thumb_insn_r->arm_insn, 8, 12);
13358 opcode2 = bits (thumb_insn_r->arm_insn, 11, 12);
13360 if (1 == opcode2)
13363 /* LDMIA. */
13364 register_bits = bits (thumb_insn_r->arm_insn, 0, 7);
13365 /* Get Rn. */
13366 reg_src1 = bits (thumb_insn_r->arm_insn, 8, 10);
13367 while (register_bits)
13369 if (register_bits & 0x00000001)
13370 record_buf[index++] = register_count;
13371 register_bits = register_bits >> 1;
13372 register_count++;
13374 record_buf[index++] = reg_src1;
13375 thumb_insn_r->reg_rec_count = index;
13377 else if (0 == opcode2)
13379 /* It handles both STMIA. */
13380 register_bits = bits (thumb_insn_r->arm_insn, 0, 7);
13381 /* Get Rn. */
13382 reg_src1 = bits (thumb_insn_r->arm_insn, 8, 10);
13383 regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval);
13384 while (register_bits)
13386 if (register_bits & 0x00000001)
13387 register_count++;
13388 register_bits = register_bits >> 1;
13390 start_address = u_regval;
13391 thumb_insn_r->mem_rec_count = register_count;
13392 while (register_count)
13394 record_buf_mem[(register_count * 2) - 1] = start_address;
13395 record_buf_mem[(register_count * 2) - 2] = 4;
13396 start_address = start_address + 4;
13397 register_count--;
13400 else if (0x1F == opcode1)
13402 /* Handle arm syscall insn. */
13403 if (tdep->arm_syscall_record != NULL)
13405 regcache_raw_read_unsigned (reg_cache, 7, &u_regval);
13406 ret = tdep->arm_syscall_record (reg_cache, u_regval);
13408 else
13410 gdb_printf (gdb_stderr, _("no syscall record support\n"));
13411 return -1;
13415 /* B (1), conditional branch is automatically taken care in process_record,
13416 as PC is saved there. */
13418 REG_ALLOC (thumb_insn_r->arm_regs, thumb_insn_r->reg_rec_count, record_buf);
13419 MEM_ALLOC (thumb_insn_r->arm_mems, thumb_insn_r->mem_rec_count,
13420 record_buf_mem);
13422 return ret;
13425 /* Handling opcode 111 insns. */
13427 static int
13428 thumb_record_branch (arm_insn_decode_record *thumb_insn_r)
13430 uint32_t record_buf[8];
13431 uint32_t bits_h = 0;
13433 bits_h = bits (thumb_insn_r->arm_insn, 11, 12);
13435 if (2 == bits_h || 3 == bits_h)
13437 /* BL */
13438 record_buf[0] = ARM_LR_REGNUM;
13439 thumb_insn_r->reg_rec_count = 1;
13441 else if (1 == bits_h)
13443 /* BLX(1). */
13444 record_buf[0] = ARM_PS_REGNUM;
13445 record_buf[1] = ARM_LR_REGNUM;
13446 thumb_insn_r->reg_rec_count = 2;
13449 /* B(2) is automatically taken care in process_record, as PC is
13450 saved there. */
13452 REG_ALLOC (thumb_insn_r->arm_regs, thumb_insn_r->reg_rec_count, record_buf);
13454 return 0;
13457 /* Handler for thumb2 load/store multiple instructions. */
13459 static int
13460 thumb2_record_ld_st_multiple (arm_insn_decode_record *thumb2_insn_r)
13462 struct regcache *reg_cache = thumb2_insn_r->regcache;
13464 uint32_t reg_rn, op;
13465 uint32_t register_bits = 0, register_count = 0;
13466 uint32_t index = 0, start_address = 0;
13467 uint32_t record_buf[24], record_buf_mem[48];
13469 ULONGEST u_regval = 0;
13471 reg_rn = bits (thumb2_insn_r->arm_insn, 16, 19);
13472 op = bits (thumb2_insn_r->arm_insn, 23, 24);
13474 if (0 == op || 3 == op)
13476 if (bit (thumb2_insn_r->arm_insn, INSN_S_L_BIT_NUM))
13478 /* Handle RFE instruction. */
13479 record_buf[0] = ARM_PS_REGNUM;
13480 thumb2_insn_r->reg_rec_count = 1;
13482 else
13484 /* Handle SRS instruction after reading banked SP. */
13485 return arm_record_unsupported_insn (thumb2_insn_r);
13488 else if (1 == op || 2 == op)
13490 if (bit (thumb2_insn_r->arm_insn, INSN_S_L_BIT_NUM))
13492 /* Handle LDM/LDMIA/LDMFD and LDMDB/LDMEA instructions. */
13493 register_bits = bits (thumb2_insn_r->arm_insn, 0, 15);
13494 while (register_bits)
13496 if (register_bits & 0x00000001)
13497 record_buf[index++] = register_count;
13499 register_count++;
13500 register_bits = register_bits >> 1;
13502 record_buf[index++] = reg_rn;
13503 record_buf[index++] = ARM_PS_REGNUM;
13504 thumb2_insn_r->reg_rec_count = index;
13506 else
13508 /* Handle STM/STMIA/STMEA and STMDB/STMFD. */
13509 register_bits = bits (thumb2_insn_r->arm_insn, 0, 15);
13510 regcache_raw_read_unsigned (reg_cache, reg_rn, &u_regval);
13511 while (register_bits)
13513 if (register_bits & 0x00000001)
13514 register_count++;
13516 register_bits = register_bits >> 1;
13519 if (1 == op)
13521 /* Start address calculation for LDMDB/LDMEA. */
13522 start_address = u_regval;
13524 else if (2 == op)
13526 /* Start address calculation for LDMDB/LDMEA. */
13527 start_address = u_regval - register_count * 4;
13530 thumb2_insn_r->mem_rec_count = register_count;
13531 while (register_count)
13533 record_buf_mem[register_count * 2 - 1] = start_address;
13534 record_buf_mem[register_count * 2 - 2] = 4;
13535 start_address = start_address + 4;
13536 register_count--;
13538 record_buf[0] = reg_rn;
13539 record_buf[1] = ARM_PS_REGNUM;
13540 thumb2_insn_r->reg_rec_count = 2;
13544 MEM_ALLOC (thumb2_insn_r->arm_mems, thumb2_insn_r->mem_rec_count,
13545 record_buf_mem);
13546 REG_ALLOC (thumb2_insn_r->arm_regs, thumb2_insn_r->reg_rec_count,
13547 record_buf);
13548 return ARM_RECORD_SUCCESS;
13551 /* Handler for thumb2 load/store (dual/exclusive) and table branch
13552 instructions. */
13554 static int
13555 thumb2_record_ld_st_dual_ex_tbb (arm_insn_decode_record *thumb2_insn_r)
13557 struct regcache *reg_cache = thumb2_insn_r->regcache;
13559 uint32_t reg_rd, reg_rn, offset_imm;
13560 uint32_t reg_dest1, reg_dest2;
13561 uint32_t address, offset_addr;
13562 uint32_t record_buf[8], record_buf_mem[8];
13563 uint32_t op1, op2, op3;
13565 ULONGEST u_regval[2];
13567 op1 = bits (thumb2_insn_r->arm_insn, 23, 24);
13568 op2 = bits (thumb2_insn_r->arm_insn, 20, 21);
13569 op3 = bits (thumb2_insn_r->arm_insn, 4, 7);
13571 if (bit (thumb2_insn_r->arm_insn, INSN_S_L_BIT_NUM))
13573 if(!(1 == op1 && 1 == op2 && (0 == op3 || 1 == op3)))
13575 reg_dest1 = bits (thumb2_insn_r->arm_insn, 12, 15);
13576 record_buf[0] = reg_dest1;
13577 record_buf[1] = ARM_PS_REGNUM;
13578 thumb2_insn_r->reg_rec_count = 2;
13581 if (3 == op2 || (op1 & 2) || (1 == op1 && 1 == op2 && 7 == op3))
13583 reg_dest2 = bits (thumb2_insn_r->arm_insn, 8, 11);
13584 record_buf[2] = reg_dest2;
13585 thumb2_insn_r->reg_rec_count = 3;
13588 else
13590 reg_rn = bits (thumb2_insn_r->arm_insn, 16, 19);
13591 regcache_raw_read_unsigned (reg_cache, reg_rn, &u_regval[0]);
13593 if (0 == op1 && 0 == op2)
13595 /* Handle STREX. */
13596 offset_imm = bits (thumb2_insn_r->arm_insn, 0, 7);
13597 address = u_regval[0] + (offset_imm * 4);
13598 record_buf_mem[0] = 4;
13599 record_buf_mem[1] = address;
13600 thumb2_insn_r->mem_rec_count = 1;
13601 reg_rd = bits (thumb2_insn_r->arm_insn, 0, 3);
13602 record_buf[0] = reg_rd;
13603 thumb2_insn_r->reg_rec_count = 1;
13605 else if (1 == op1 && 0 == op2)
13607 reg_rd = bits (thumb2_insn_r->arm_insn, 0, 3);
13608 record_buf[0] = reg_rd;
13609 thumb2_insn_r->reg_rec_count = 1;
13610 address = u_regval[0];
13611 record_buf_mem[1] = address;
13613 if (4 == op3)
13615 /* Handle STREXB. */
13616 record_buf_mem[0] = 1;
13617 thumb2_insn_r->mem_rec_count = 1;
13619 else if (5 == op3)
13621 /* Handle STREXH. */
13622 record_buf_mem[0] = 2 ;
13623 thumb2_insn_r->mem_rec_count = 1;
13625 else if (7 == op3)
13627 /* Handle STREXD. */
13628 address = u_regval[0];
13629 record_buf_mem[0] = 4;
13630 record_buf_mem[2] = 4;
13631 record_buf_mem[3] = address + 4;
13632 thumb2_insn_r->mem_rec_count = 2;
13635 else
13637 offset_imm = bits (thumb2_insn_r->arm_insn, 0, 7);
13639 if (bit (thumb2_insn_r->arm_insn, 24))
13641 if (bit (thumb2_insn_r->arm_insn, 23))
13642 offset_addr = u_regval[0] + (offset_imm * 4);
13643 else
13644 offset_addr = u_regval[0] - (offset_imm * 4);
13646 address = offset_addr;
13648 else
13649 address = u_regval[0];
13651 record_buf_mem[0] = 4;
13652 record_buf_mem[1] = address;
13653 record_buf_mem[2] = 4;
13654 record_buf_mem[3] = address + 4;
13655 thumb2_insn_r->mem_rec_count = 2;
13656 record_buf[0] = reg_rn;
13657 thumb2_insn_r->reg_rec_count = 1;
13661 REG_ALLOC (thumb2_insn_r->arm_regs, thumb2_insn_r->reg_rec_count,
13662 record_buf);
13663 MEM_ALLOC (thumb2_insn_r->arm_mems, thumb2_insn_r->mem_rec_count,
13664 record_buf_mem);
13665 return ARM_RECORD_SUCCESS;
13668 /* Handler for thumb2 data processing (shift register and modified immediate)
13669 instructions. */
13671 static int
13672 thumb2_record_data_proc_sreg_mimm (arm_insn_decode_record *thumb2_insn_r)
13674 uint32_t reg_rd, op;
13675 uint32_t record_buf[8];
13677 op = bits (thumb2_insn_r->arm_insn, 21, 24);
13678 reg_rd = bits (thumb2_insn_r->arm_insn, 8, 11);
13680 if ((0 == op || 4 == op || 8 == op || 13 == op) && 15 == reg_rd)
13682 record_buf[0] = ARM_PS_REGNUM;
13683 thumb2_insn_r->reg_rec_count = 1;
13685 else
13687 record_buf[0] = reg_rd;
13688 record_buf[1] = ARM_PS_REGNUM;
13689 thumb2_insn_r->reg_rec_count = 2;
13692 REG_ALLOC (thumb2_insn_r->arm_regs, thumb2_insn_r->reg_rec_count,
13693 record_buf);
13694 return ARM_RECORD_SUCCESS;
13697 /* Generic handler for thumb2 instructions which effect destination and PS
13698 registers. */
13700 static int
13701 thumb2_record_ps_dest_generic (arm_insn_decode_record *thumb2_insn_r)
13703 uint32_t reg_rd;
13704 uint32_t record_buf[8];
13706 reg_rd = bits (thumb2_insn_r->arm_insn, 8, 11);
13708 record_buf[0] = reg_rd;
13709 record_buf[1] = ARM_PS_REGNUM;
13710 thumb2_insn_r->reg_rec_count = 2;
13712 REG_ALLOC (thumb2_insn_r->arm_regs, thumb2_insn_r->reg_rec_count,
13713 record_buf);
13714 return ARM_RECORD_SUCCESS;
13717 /* Handler for thumb2 branch and miscellaneous control instructions. */
13719 static int
13720 thumb2_record_branch_misc_cntrl (arm_insn_decode_record *thumb2_insn_r)
13722 uint32_t op, op1, op2;
13723 uint32_t record_buf[8];
13725 op = bits (thumb2_insn_r->arm_insn, 20, 26);
13726 op1 = bits (thumb2_insn_r->arm_insn, 12, 14);
13727 op2 = bits (thumb2_insn_r->arm_insn, 8, 11);
13729 /* Handle MSR insn. */
13730 if (!(op1 & 0x2) && 0x38 == op)
13732 if (!(op2 & 0x3))
13734 /* CPSR is going to be changed. */
13735 record_buf[0] = ARM_PS_REGNUM;
13736 thumb2_insn_r->reg_rec_count = 1;
13738 else
13740 arm_record_unsupported_insn(thumb2_insn_r);
13741 return -1;
13744 else if (4 == (op1 & 0x5) || 5 == (op1 & 0x5))
13746 /* BLX. */
13747 record_buf[0] = ARM_PS_REGNUM;
13748 record_buf[1] = ARM_LR_REGNUM;
13749 thumb2_insn_r->reg_rec_count = 2;
13752 REG_ALLOC (thumb2_insn_r->arm_regs, thumb2_insn_r->reg_rec_count,
13753 record_buf);
13754 return ARM_RECORD_SUCCESS;
13757 /* Handler for thumb2 store single data item instructions. */
13759 static int
13760 thumb2_record_str_single_data (arm_insn_decode_record *thumb2_insn_r)
13762 struct regcache *reg_cache = thumb2_insn_r->regcache;
13764 uint32_t reg_rn, reg_rm, offset_imm, shift_imm;
13765 uint32_t address, offset_addr;
13766 uint32_t record_buf[8], record_buf_mem[8];
13767 uint32_t op1, op2;
13769 ULONGEST u_regval[2];
13771 op1 = bits (thumb2_insn_r->arm_insn, 21, 23);
13772 op2 = bits (thumb2_insn_r->arm_insn, 6, 11);
13773 reg_rn = bits (thumb2_insn_r->arm_insn, 16, 19);
13774 regcache_raw_read_unsigned (reg_cache, reg_rn, &u_regval[0]);
13776 if (bit (thumb2_insn_r->arm_insn, 23))
13778 /* T2 encoding. */
13779 offset_imm = bits (thumb2_insn_r->arm_insn, 0, 11);
13780 offset_addr = u_regval[0] + offset_imm;
13781 address = offset_addr;
13783 else
13785 /* T3 encoding. */
13786 if ((0 == op1 || 1 == op1 || 2 == op1) && !(op2 & 0x20))
13788 /* Handle STRB (register). */
13789 reg_rm = bits (thumb2_insn_r->arm_insn, 0, 3);
13790 regcache_raw_read_unsigned (reg_cache, reg_rm, &u_regval[1]);
13791 shift_imm = bits (thumb2_insn_r->arm_insn, 4, 5);
13792 offset_addr = u_regval[1] << shift_imm;
13793 address = u_regval[0] + offset_addr;
13795 else
13797 offset_imm = bits (thumb2_insn_r->arm_insn, 0, 7);
13798 if (bit (thumb2_insn_r->arm_insn, 10))
13800 if (bit (thumb2_insn_r->arm_insn, 9))
13801 offset_addr = u_regval[0] + offset_imm;
13802 else
13803 offset_addr = u_regval[0] - offset_imm;
13805 address = offset_addr;
13807 else
13808 address = u_regval[0];
13812 switch (op1)
13814 /* Store byte instructions. */
13815 case 4:
13816 case 0:
13817 record_buf_mem[0] = 1;
13818 break;
13819 /* Store half word instructions. */
13820 case 1:
13821 case 5:
13822 record_buf_mem[0] = 2;
13823 break;
13824 /* Store word instructions. */
13825 case 2:
13826 case 6:
13827 record_buf_mem[0] = 4;
13828 break;
13830 default:
13831 gdb_assert_not_reached ("no decoding pattern found");
13832 break;
13835 record_buf_mem[1] = address;
13836 thumb2_insn_r->mem_rec_count = 1;
13837 record_buf[0] = reg_rn;
13838 thumb2_insn_r->reg_rec_count = 1;
13840 REG_ALLOC (thumb2_insn_r->arm_regs, thumb2_insn_r->reg_rec_count,
13841 record_buf);
13842 MEM_ALLOC (thumb2_insn_r->arm_mems, thumb2_insn_r->mem_rec_count,
13843 record_buf_mem);
13844 return ARM_RECORD_SUCCESS;
13847 /* Handler for thumb2 load memory hints instructions. */
13849 static int
13850 thumb2_record_ld_mem_hints (arm_insn_decode_record *thumb2_insn_r)
13852 uint32_t record_buf[8];
13853 uint32_t reg_rt, reg_rn;
13855 reg_rt = bits (thumb2_insn_r->arm_insn, 12, 15);
13856 reg_rn = bits (thumb2_insn_r->arm_insn, 16, 19);
13858 if (ARM_PC_REGNUM != reg_rt)
13860 record_buf[0] = reg_rt;
13861 record_buf[1] = reg_rn;
13862 record_buf[2] = ARM_PS_REGNUM;
13863 thumb2_insn_r->reg_rec_count = 3;
13865 REG_ALLOC (thumb2_insn_r->arm_regs, thumb2_insn_r->reg_rec_count,
13866 record_buf);
13867 return ARM_RECORD_SUCCESS;
13870 return ARM_RECORD_FAILURE;
13873 /* Handler for thumb2 load word instructions. */
13875 static int
13876 thumb2_record_ld_word (arm_insn_decode_record *thumb2_insn_r)
13878 uint32_t record_buf[8];
13880 record_buf[0] = bits (thumb2_insn_r->arm_insn, 12, 15);
13881 record_buf[1] = ARM_PS_REGNUM;
13882 thumb2_insn_r->reg_rec_count = 2;
13884 REG_ALLOC (thumb2_insn_r->arm_regs, thumb2_insn_r->reg_rec_count,
13885 record_buf);
13886 return ARM_RECORD_SUCCESS;
13889 /* Handler for thumb2 long multiply, long multiply accumulate, and
13890 divide instructions. */
13892 static int
13893 thumb2_record_lmul_lmla_div (arm_insn_decode_record *thumb2_insn_r)
13895 uint32_t opcode1 = 0, opcode2 = 0;
13896 uint32_t record_buf[8];
13898 opcode1 = bits (thumb2_insn_r->arm_insn, 20, 22);
13899 opcode2 = bits (thumb2_insn_r->arm_insn, 4, 7);
13901 if (0 == opcode1 || 2 == opcode1 || (opcode1 >= 4 && opcode1 <= 6))
13903 /* Handle SMULL, UMULL, SMULAL. */
13904 /* Handle SMLAL(S), SMULL(S), UMLAL(S), UMULL(S). */
13905 record_buf[0] = bits (thumb2_insn_r->arm_insn, 16, 19);
13906 record_buf[1] = bits (thumb2_insn_r->arm_insn, 12, 15);
13907 record_buf[2] = ARM_PS_REGNUM;
13908 thumb2_insn_r->reg_rec_count = 3;
13910 else if (1 == opcode1 || 3 == opcode2)
13912 /* Handle SDIV and UDIV. */
13913 record_buf[0] = bits (thumb2_insn_r->arm_insn, 16, 19);
13914 record_buf[1] = bits (thumb2_insn_r->arm_insn, 12, 15);
13915 record_buf[2] = ARM_PS_REGNUM;
13916 thumb2_insn_r->reg_rec_count = 3;
13918 else
13919 return ARM_RECORD_FAILURE;
13921 REG_ALLOC (thumb2_insn_r->arm_regs, thumb2_insn_r->reg_rec_count,
13922 record_buf);
13923 return ARM_RECORD_SUCCESS;
13926 /* Record handler for thumb32 coprocessor instructions. */
13928 static int
13929 thumb2_record_coproc_insn (arm_insn_decode_record *thumb2_insn_r)
13931 if (bit (thumb2_insn_r->arm_insn, 25))
13932 return arm_record_coproc_data_proc (thumb2_insn_r);
13933 else
13934 return arm_record_asimd_vfp_coproc (thumb2_insn_r);
13937 /* Record handler for advance SIMD structure load/store instructions. */
13939 static int
13940 thumb2_record_asimd_struct_ld_st (arm_insn_decode_record *thumb2_insn_r)
13942 struct regcache *reg_cache = thumb2_insn_r->regcache;
13943 uint32_t l_bit, a_bit, b_bits;
13944 uint32_t record_buf[128], record_buf_mem[128];
13945 uint32_t reg_rn, reg_vd, address, f_elem;
13946 uint32_t index_r = 0, index_e = 0, bf_regs = 0, index_m = 0, loop_t = 0;
13947 uint8_t f_ebytes;
13949 l_bit = bit (thumb2_insn_r->arm_insn, 21);
13950 a_bit = bit (thumb2_insn_r->arm_insn, 23);
13951 b_bits = bits (thumb2_insn_r->arm_insn, 8, 11);
13952 reg_rn = bits (thumb2_insn_r->arm_insn, 16, 19);
13953 reg_vd = bits (thumb2_insn_r->arm_insn, 12, 15);
13954 reg_vd = (bit (thumb2_insn_r->arm_insn, 22) << 4) | reg_vd;
13955 f_ebytes = (1 << bits (thumb2_insn_r->arm_insn, 6, 7));
13956 f_elem = 8 / f_ebytes;
13958 if (!l_bit)
13960 ULONGEST u_regval = 0;
13961 regcache_raw_read_unsigned (reg_cache, reg_rn, &u_regval);
13962 address = u_regval;
13964 if (!a_bit)
13966 /* Handle VST1. */
13967 if (b_bits == 0x02 || b_bits == 0x0a || (b_bits & 0x0e) == 0x06)
13969 if (b_bits == 0x07)
13970 bf_regs = 1;
13971 else if (b_bits == 0x0a)
13972 bf_regs = 2;
13973 else if (b_bits == 0x06)
13974 bf_regs = 3;
13975 else if (b_bits == 0x02)
13976 bf_regs = 4;
13977 else
13978 bf_regs = 0;
13980 for (index_r = 0; index_r < bf_regs; index_r++)
13982 for (index_e = 0; index_e < f_elem; index_e++)
13984 record_buf_mem[index_m++] = f_ebytes;
13985 record_buf_mem[index_m++] = address;
13986 address = address + f_ebytes;
13987 thumb2_insn_r->mem_rec_count += 1;
13991 /* Handle VST2. */
13992 else if (b_bits == 0x03 || (b_bits & 0x0e) == 0x08)
13994 if (b_bits == 0x09 || b_bits == 0x08)
13995 bf_regs = 1;
13996 else if (b_bits == 0x03)
13997 bf_regs = 2;
13998 else
13999 bf_regs = 0;
14001 for (index_r = 0; index_r < bf_regs; index_r++)
14002 for (index_e = 0; index_e < f_elem; index_e++)
14004 for (loop_t = 0; loop_t < 2; loop_t++)
14006 record_buf_mem[index_m++] = f_ebytes;
14007 record_buf_mem[index_m++] = address + (loop_t * f_ebytes);
14008 thumb2_insn_r->mem_rec_count += 1;
14010 address = address + (2 * f_ebytes);
14013 /* Handle VST3. */
14014 else if ((b_bits & 0x0e) == 0x04)
14016 for (index_e = 0; index_e < f_elem; index_e++)
14018 for (loop_t = 0; loop_t < 3; loop_t++)
14020 record_buf_mem[index_m++] = f_ebytes;
14021 record_buf_mem[index_m++] = address + (loop_t * f_ebytes);
14022 thumb2_insn_r->mem_rec_count += 1;
14024 address = address + (3 * f_ebytes);
14027 /* Handle VST4. */
14028 else if (!(b_bits & 0x0e))
14030 for (index_e = 0; index_e < f_elem; index_e++)
14032 for (loop_t = 0; loop_t < 4; loop_t++)
14034 record_buf_mem[index_m++] = f_ebytes;
14035 record_buf_mem[index_m++] = address + (loop_t * f_ebytes);
14036 thumb2_insn_r->mem_rec_count += 1;
14038 address = address + (4 * f_ebytes);
14042 else
14044 uint8_t bft_size = bits (thumb2_insn_r->arm_insn, 10, 11);
14046 if (bft_size == 0x00)
14047 f_ebytes = 1;
14048 else if (bft_size == 0x01)
14049 f_ebytes = 2;
14050 else if (bft_size == 0x02)
14051 f_ebytes = 4;
14052 else
14053 f_ebytes = 0;
14055 /* Handle VST1. */
14056 if (!(b_bits & 0x0b) || b_bits == 0x08)
14057 thumb2_insn_r->mem_rec_count = 1;
14058 /* Handle VST2. */
14059 else if ((b_bits & 0x0b) == 0x01 || b_bits == 0x09)
14060 thumb2_insn_r->mem_rec_count = 2;
14061 /* Handle VST3. */
14062 else if ((b_bits & 0x0b) == 0x02 || b_bits == 0x0a)
14063 thumb2_insn_r->mem_rec_count = 3;
14064 /* Handle VST4. */
14065 else if ((b_bits & 0x0b) == 0x03 || b_bits == 0x0b)
14066 thumb2_insn_r->mem_rec_count = 4;
14068 for (index_m = 0; index_m < thumb2_insn_r->mem_rec_count; index_m++)
14070 record_buf_mem[index_m] = f_ebytes;
14071 record_buf_mem[index_m] = address + (index_m * f_ebytes);
14075 else
14077 if (!a_bit)
14079 /* Handle VLD1. */
14080 if (b_bits == 0x02 || b_bits == 0x0a || (b_bits & 0x0e) == 0x06)
14081 thumb2_insn_r->reg_rec_count = 1;
14082 /* Handle VLD2. */
14083 else if (b_bits == 0x03 || (b_bits & 0x0e) == 0x08)
14084 thumb2_insn_r->reg_rec_count = 2;
14085 /* Handle VLD3. */
14086 else if ((b_bits & 0x0e) == 0x04)
14087 thumb2_insn_r->reg_rec_count = 3;
14088 /* Handle VLD4. */
14089 else if (!(b_bits & 0x0e))
14090 thumb2_insn_r->reg_rec_count = 4;
14092 else
14094 /* Handle VLD1. */
14095 if (!(b_bits & 0x0b) || b_bits == 0x08 || b_bits == 0x0c)
14096 thumb2_insn_r->reg_rec_count = 1;
14097 /* Handle VLD2. */
14098 else if ((b_bits & 0x0b) == 0x01 || b_bits == 0x09 || b_bits == 0x0d)
14099 thumb2_insn_r->reg_rec_count = 2;
14100 /* Handle VLD3. */
14101 else if ((b_bits & 0x0b) == 0x02 || b_bits == 0x0a || b_bits == 0x0e)
14102 thumb2_insn_r->reg_rec_count = 3;
14103 /* Handle VLD4. */
14104 else if ((b_bits & 0x0b) == 0x03 || b_bits == 0x0b || b_bits == 0x0f)
14105 thumb2_insn_r->reg_rec_count = 4;
14107 for (index_r = 0; index_r < thumb2_insn_r->reg_rec_count; index_r++)
14108 record_buf[index_r] = reg_vd + ARM_D0_REGNUM + index_r;
14112 if (bits (thumb2_insn_r->arm_insn, 0, 3) != 15)
14114 record_buf[index_r] = reg_rn;
14115 thumb2_insn_r->reg_rec_count += 1;
14118 REG_ALLOC (thumb2_insn_r->arm_regs, thumb2_insn_r->reg_rec_count,
14119 record_buf);
14120 MEM_ALLOC (thumb2_insn_r->arm_mems, thumb2_insn_r->mem_rec_count,
14121 record_buf_mem);
14122 return 0;
14125 /* Decodes thumb2 instruction type and invokes its record handler. */
14127 static unsigned int
14128 thumb2_record_decode_insn_handler (arm_insn_decode_record *thumb2_insn_r)
14130 uint32_t op, op1, op2;
14132 op = bit (thumb2_insn_r->arm_insn, 15);
14133 op1 = bits (thumb2_insn_r->arm_insn, 27, 28);
14134 op2 = bits (thumb2_insn_r->arm_insn, 20, 26);
14136 if (op1 == 0x01)
14138 if (!(op2 & 0x64 ))
14140 /* Load/store multiple instruction. */
14141 return thumb2_record_ld_st_multiple (thumb2_insn_r);
14143 else if ((op2 & 0x64) == 0x4)
14145 /* Load/store (dual/exclusive) and table branch instruction. */
14146 return thumb2_record_ld_st_dual_ex_tbb (thumb2_insn_r);
14148 else if ((op2 & 0x60) == 0x20)
14150 /* Data-processing (shifted register). */
14151 return thumb2_record_data_proc_sreg_mimm (thumb2_insn_r);
14153 else if (op2 & 0x40)
14155 /* Co-processor instructions. */
14156 return thumb2_record_coproc_insn (thumb2_insn_r);
14159 else if (op1 == 0x02)
14161 if (op)
14163 /* Branches and miscellaneous control instructions. */
14164 return thumb2_record_branch_misc_cntrl (thumb2_insn_r);
14166 else if (op2 & 0x20)
14168 /* Data-processing (plain binary immediate) instruction. */
14169 return thumb2_record_ps_dest_generic (thumb2_insn_r);
14171 else
14173 /* Data-processing (modified immediate). */
14174 return thumb2_record_data_proc_sreg_mimm (thumb2_insn_r);
14177 else if (op1 == 0x03)
14179 if (!(op2 & 0x71 ))
14181 /* Store single data item. */
14182 return thumb2_record_str_single_data (thumb2_insn_r);
14184 else if (!((op2 & 0x71) ^ 0x10))
14186 /* Advanced SIMD or structure load/store instructions. */
14187 return thumb2_record_asimd_struct_ld_st (thumb2_insn_r);
14189 else if (!((op2 & 0x67) ^ 0x01))
14191 /* Load byte, memory hints instruction. */
14192 return thumb2_record_ld_mem_hints (thumb2_insn_r);
14194 else if (!((op2 & 0x67) ^ 0x03))
14196 /* Load halfword, memory hints instruction. */
14197 return thumb2_record_ld_mem_hints (thumb2_insn_r);
14199 else if (!((op2 & 0x67) ^ 0x05))
14201 /* Load word instruction. */
14202 return thumb2_record_ld_word (thumb2_insn_r);
14204 else if (!((op2 & 0x70) ^ 0x20))
14206 /* Data-processing (register) instruction. */
14207 return thumb2_record_ps_dest_generic (thumb2_insn_r);
14209 else if (!((op2 & 0x78) ^ 0x30))
14211 /* Multiply, multiply accumulate, abs diff instruction. */
14212 return thumb2_record_ps_dest_generic (thumb2_insn_r);
14214 else if (!((op2 & 0x78) ^ 0x38))
14216 /* Long multiply, long multiply accumulate, and divide. */
14217 return thumb2_record_lmul_lmla_div (thumb2_insn_r);
14219 else if (op2 & 0x40)
14221 /* Co-processor instructions. */
14222 return thumb2_record_coproc_insn (thumb2_insn_r);
14226 return -1;
14229 namespace {
14230 /* Abstract memory reader. */
14232 class abstract_memory_reader
14234 public:
14235 /* Read LEN bytes of target memory at address MEMADDR, placing the
14236 results in GDB's memory at BUF. Return true on success. */
14238 virtual bool read (CORE_ADDR memaddr, gdb_byte *buf, const size_t len) = 0;
14241 /* Instruction reader from real target. */
14243 class instruction_reader : public abstract_memory_reader
14245 public:
14246 bool read (CORE_ADDR memaddr, gdb_byte *buf, const size_t len) override
14248 if (target_read_memory (memaddr, buf, len))
14249 return false;
14250 else
14251 return true;
14255 } // namespace
14257 /* Extracts arm/thumb/thumb2 insn depending on the size, and returns 0 on success
14258 and positive val on failure. */
14260 static int
14261 extract_arm_insn (abstract_memory_reader& reader,
14262 arm_insn_decode_record *insn_record, uint32_t insn_size)
14264 gdb_byte buf[insn_size];
14266 memset (&buf[0], 0, insn_size);
14268 if (!reader.read (insn_record->this_addr, buf, insn_size))
14269 return 1;
14270 insn_record->arm_insn = (uint32_t) extract_unsigned_integer (&buf[0],
14271 insn_size,
14272 gdbarch_byte_order_for_code (insn_record->gdbarch));
14273 return 0;
14276 typedef int (*sti_arm_hdl_fp_t) (arm_insn_decode_record*);
14278 /* Decode arm/thumb insn depending on condition cods and opcodes; and
14279 dispatch it. */
14281 static int
14282 decode_insn (abstract_memory_reader &reader,
14283 arm_insn_decode_record *arm_record,
14284 record_type_t record_type, uint32_t insn_size)
14287 /* (Starting from numerical 0); bits 25, 26, 27 decodes type of arm
14288 instruction. */
14289 static const sti_arm_hdl_fp_t arm_handle_insn[8] =
14291 arm_record_data_proc_misc_ld_str, /* 000. */
14292 arm_record_data_proc_imm, /* 001. */
14293 arm_record_ld_st_imm_offset, /* 010. */
14294 arm_record_ld_st_reg_offset, /* 011. */
14295 arm_record_ld_st_multiple, /* 100. */
14296 arm_record_b_bl, /* 101. */
14297 arm_record_asimd_vfp_coproc, /* 110. */
14298 arm_record_coproc_data_proc /* 111. */
14301 /* (Starting from numerical 0); bits 13,14,15 decodes type of thumb
14302 instruction. */
14303 static const sti_arm_hdl_fp_t thumb_handle_insn[8] =
14305 thumb_record_shift_add_sub, /* 000. */
14306 thumb_record_add_sub_cmp_mov, /* 001. */
14307 thumb_record_ld_st_reg_offset, /* 010. */
14308 thumb_record_ld_st_imm_offset, /* 011. */
14309 thumb_record_ld_st_stack, /* 100. */
14310 thumb_record_misc, /* 101. */
14311 thumb_record_ldm_stm_swi, /* 110. */
14312 thumb_record_branch /* 111. */
14315 uint32_t ret = 0; /* return value: negative:failure 0:success. */
14316 uint32_t insn_id = 0;
14318 if (extract_arm_insn (reader, arm_record, insn_size))
14320 if (record_debug)
14322 gdb_printf (gdb_stdlog,
14323 _("Process record: error reading memory at "
14324 "addr %s len = %d.\n"),
14325 paddress (arm_record->gdbarch,
14326 arm_record->this_addr), insn_size);
14328 return -1;
14330 else if (ARM_RECORD == record_type)
14332 arm_record->cond = bits (arm_record->arm_insn, 28, 31);
14333 insn_id = bits (arm_record->arm_insn, 25, 27);
14335 if (arm_record->cond == 0xf)
14336 ret = arm_record_extension_space (arm_record);
14337 else
14339 /* If this insn has fallen into extension space
14340 then we need not decode it anymore. */
14341 ret = arm_handle_insn[insn_id] (arm_record);
14343 if (ret != ARM_RECORD_SUCCESS)
14345 arm_record_unsupported_insn (arm_record);
14346 ret = -1;
14349 else if (THUMB_RECORD == record_type)
14351 /* As thumb does not have condition codes, we set negative. */
14352 arm_record->cond = -1;
14353 insn_id = bits (arm_record->arm_insn, 13, 15);
14354 ret = thumb_handle_insn[insn_id] (arm_record);
14355 if (ret != ARM_RECORD_SUCCESS)
14357 arm_record_unsupported_insn (arm_record);
14358 ret = -1;
14361 else if (THUMB2_RECORD == record_type)
14363 /* As thumb does not have condition codes, we set negative. */
14364 arm_record->cond = -1;
14366 /* Swap first half of 32bit thumb instruction with second half. */
14367 arm_record->arm_insn
14368 = (arm_record->arm_insn >> 16) | (arm_record->arm_insn << 16);
14370 ret = thumb2_record_decode_insn_handler (arm_record);
14372 if (ret != ARM_RECORD_SUCCESS)
14374 arm_record_unsupported_insn (arm_record);
14375 ret = -1;
14378 else
14380 /* Throw assertion. */
14381 gdb_assert_not_reached ("not a valid instruction, could not decode");
14384 return ret;
14387 #if GDB_SELF_TEST
14388 namespace selftests {
14390 /* Provide both 16-bit and 32-bit thumb instructions. */
14392 class instruction_reader_thumb : public abstract_memory_reader
14394 public:
14395 template<size_t SIZE>
14396 instruction_reader_thumb (enum bfd_endian endian,
14397 const uint16_t (&insns)[SIZE])
14398 : m_endian (endian), m_insns (insns), m_insns_size (SIZE)
14401 bool read (CORE_ADDR memaddr, gdb_byte *buf, const size_t len) override
14403 SELF_CHECK (len == 4 || len == 2);
14404 SELF_CHECK (memaddr % 2 == 0);
14405 SELF_CHECK ((memaddr / 2) < m_insns_size);
14407 store_unsigned_integer (buf, 2, m_endian, m_insns[memaddr / 2]);
14408 if (len == 4)
14410 store_unsigned_integer (&buf[2], 2, m_endian,
14411 m_insns[memaddr / 2 + 1]);
14413 return true;
14416 private:
14417 enum bfd_endian m_endian;
14418 const uint16_t *m_insns;
14419 size_t m_insns_size;
14422 static void
14423 arm_record_test (void)
14425 struct gdbarch_info info;
14426 info.bfd_arch_info = bfd_scan_arch ("arm");
14428 struct gdbarch *gdbarch = gdbarch_find_by_info (info);
14430 SELF_CHECK (gdbarch != NULL);
14432 /* 16-bit Thumb instructions. */
14434 arm_insn_decode_record arm_record;
14436 memset (&arm_record, 0, sizeof (arm_insn_decode_record));
14437 arm_record.gdbarch = gdbarch;
14439 static const uint16_t insns[] = {
14440 /* db b2 uxtb r3, r3 */
14441 0xb2db,
14442 /* cd 58 ldr r5, [r1, r3] */
14443 0x58cd,
14446 enum bfd_endian endian = gdbarch_byte_order_for_code (arm_record.gdbarch);
14447 instruction_reader_thumb reader (endian, insns);
14448 int ret = decode_insn (reader, &arm_record, THUMB_RECORD,
14449 THUMB_INSN_SIZE_BYTES);
14451 SELF_CHECK (ret == 0);
14452 SELF_CHECK (arm_record.mem_rec_count == 0);
14453 SELF_CHECK (arm_record.reg_rec_count == 1);
14454 SELF_CHECK (arm_record.arm_regs[0] == 3);
14456 arm_record.this_addr += 2;
14457 ret = decode_insn (reader, &arm_record, THUMB_RECORD,
14458 THUMB_INSN_SIZE_BYTES);
14460 SELF_CHECK (ret == 0);
14461 SELF_CHECK (arm_record.mem_rec_count == 0);
14462 SELF_CHECK (arm_record.reg_rec_count == 1);
14463 SELF_CHECK (arm_record.arm_regs[0] == 5);
14466 /* 32-bit Thumb-2 instructions. */
14468 arm_insn_decode_record arm_record;
14470 memset (&arm_record, 0, sizeof (arm_insn_decode_record));
14471 arm_record.gdbarch = gdbarch;
14473 static const uint16_t insns[] = {
14474 /* 1d ee 70 7f mrc 15, 0, r7, cr13, cr0, {3} */
14475 0xee1d, 0x7f70,
14478 enum bfd_endian endian = gdbarch_byte_order_for_code (arm_record.gdbarch);
14479 instruction_reader_thumb reader (endian, insns);
14480 int ret = decode_insn (reader, &arm_record, THUMB2_RECORD,
14481 THUMB2_INSN_SIZE_BYTES);
14483 SELF_CHECK (ret == 0);
14484 SELF_CHECK (arm_record.mem_rec_count == 0);
14485 SELF_CHECK (arm_record.reg_rec_count == 1);
14486 SELF_CHECK (arm_record.arm_regs[0] == 7);
14490 /* Instruction reader from manually cooked instruction sequences. */
14492 class test_arm_instruction_reader : public arm_instruction_reader
14494 public:
14495 explicit test_arm_instruction_reader (gdb::array_view<const uint32_t> insns)
14496 : m_insns (insns)
14499 uint32_t read (CORE_ADDR memaddr, enum bfd_endian byte_order) const override
14501 SELF_CHECK (memaddr % 4 == 0);
14502 SELF_CHECK (memaddr / 4 < m_insns.size ());
14504 return m_insns[memaddr / 4];
14507 private:
14508 const gdb::array_view<const uint32_t> m_insns;
14511 static void
14512 arm_analyze_prologue_test ()
14514 for (bfd_endian endianness : {BFD_ENDIAN_LITTLE, BFD_ENDIAN_BIG})
14516 struct gdbarch_info info;
14517 info.byte_order = endianness;
14518 info.byte_order_for_code = endianness;
14519 info.bfd_arch_info = bfd_scan_arch ("arm");
14521 struct gdbarch *gdbarch = gdbarch_find_by_info (info);
14523 SELF_CHECK (gdbarch != NULL);
14525 /* The "sub" instruction contains an immediate value rotate count of 0,
14526 which resulted in a 32-bit shift of a 32-bit value, caught by
14527 UBSan. */
14528 const uint32_t insns[] = {
14529 0xe92d4ff0, /* push {r4, r5, r6, r7, r8, r9, sl, fp, lr} */
14530 0xe1a05000, /* mov r5, r0 */
14531 0xe5903020, /* ldr r3, [r0, #32] */
14532 0xe24dd044, /* sub sp, sp, #68 ; 0x44 */
14535 test_arm_instruction_reader mem_reader (insns);
14536 arm_prologue_cache cache;
14537 arm_cache_init (&cache, gdbarch);
14539 arm_analyze_prologue (gdbarch, 0, sizeof (insns) - 1, &cache, mem_reader);
14543 } // namespace selftests
14544 #endif /* GDB_SELF_TEST */
14546 /* Cleans up local record registers and memory allocations. */
14548 static void
14549 deallocate_reg_mem (arm_insn_decode_record *record)
14551 xfree (record->arm_regs);
14552 xfree (record->arm_mems);
14556 /* Parse the current instruction and record the values of the registers and
14557 memory that will be changed in current instruction to record_arch_list".
14558 Return -1 if something is wrong. */
14561 arm_process_record (struct gdbarch *gdbarch, struct regcache *regcache,
14562 CORE_ADDR insn_addr)
14565 uint32_t no_of_rec = 0;
14566 uint32_t ret = 0; /* return value: -1:record failure ; 0:success */
14567 ULONGEST t_bit = 0, insn_id = 0;
14569 ULONGEST u_regval = 0;
14571 arm_insn_decode_record arm_record;
14573 memset (&arm_record, 0, sizeof (arm_insn_decode_record));
14574 arm_record.regcache = regcache;
14575 arm_record.this_addr = insn_addr;
14576 arm_record.gdbarch = gdbarch;
14579 if (record_debug > 1)
14581 gdb_printf (gdb_stdlog, "Process record: arm_process_record "
14582 "addr = %s\n",
14583 paddress (gdbarch, arm_record.this_addr));
14586 instruction_reader reader;
14587 if (extract_arm_insn (reader, &arm_record, 2))
14589 if (record_debug)
14591 gdb_printf (gdb_stdlog,
14592 _("Process record: error reading memory at "
14593 "addr %s len = %d.\n"),
14594 paddress (arm_record.gdbarch,
14595 arm_record.this_addr), 2);
14597 return -1;
14600 /* Check the insn, whether it is thumb or arm one. */
14602 t_bit = arm_psr_thumb_bit (arm_record.gdbarch);
14603 regcache_raw_read_unsigned (arm_record.regcache, ARM_PS_REGNUM, &u_regval);
14606 if (!(u_regval & t_bit))
14608 /* We are decoding arm insn. */
14609 ret = decode_insn (reader, &arm_record, ARM_RECORD, ARM_INSN_SIZE_BYTES);
14611 else
14613 insn_id = bits (arm_record.arm_insn, 11, 15);
14614 /* is it thumb2 insn? */
14615 if ((0x1D == insn_id) || (0x1E == insn_id) || (0x1F == insn_id))
14617 ret = decode_insn (reader, &arm_record, THUMB2_RECORD,
14618 THUMB2_INSN_SIZE_BYTES);
14620 else
14622 /* We are decoding thumb insn. */
14623 ret = decode_insn (reader, &arm_record, THUMB_RECORD,
14624 THUMB_INSN_SIZE_BYTES);
14628 if (0 == ret)
14630 /* Record registers. */
14631 record_full_arch_list_add_reg (arm_record.regcache, ARM_PC_REGNUM);
14632 if (arm_record.arm_regs)
14634 for (no_of_rec = 0; no_of_rec < arm_record.reg_rec_count; no_of_rec++)
14636 if (record_full_arch_list_add_reg
14637 (arm_record.regcache , arm_record.arm_regs[no_of_rec]))
14638 ret = -1;
14641 /* Record memories. */
14642 if (arm_record.arm_mems)
14644 for (no_of_rec = 0; no_of_rec < arm_record.mem_rec_count; no_of_rec++)
14646 if (record_full_arch_list_add_mem
14647 ((CORE_ADDR)arm_record.arm_mems[no_of_rec].addr,
14648 arm_record.arm_mems[no_of_rec].len))
14649 ret = -1;
14653 if (record_full_arch_list_add_end ())
14654 ret = -1;
14658 deallocate_reg_mem (&arm_record);
14660 return ret;
14663 /* See arm-tdep.h. */
14665 const target_desc *
14666 arm_read_description (arm_fp_type fp_type, bool tls)
14668 struct target_desc *tdesc = tdesc_arm_list[fp_type][tls];
14670 if (tdesc == nullptr)
14672 tdesc = arm_create_target_description (fp_type, tls);
14673 tdesc_arm_list[fp_type][tls] = tdesc;
14676 return tdesc;
14679 /* See arm-tdep.h. */
14681 const target_desc *
14682 arm_read_mprofile_description (arm_m_profile_type m_type)
14684 struct target_desc *tdesc = tdesc_arm_mprofile_list[m_type];
14686 if (tdesc == nullptr)
14688 tdesc = arm_create_mprofile_target_description (m_type);
14689 tdesc_arm_mprofile_list[m_type] = tdesc;
14692 return tdesc;