]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/arm-tdep.c
[gdb/symtab] Factor out have_complaint
[thirdparty/binutils-gdb.git] / gdb / arm-tdep.c
CommitLineData
ed9a39eb 1/* Common target dependent code for GDB on ARM systems.
0fd88904 2
4a94e368 3 Copyright (C) 1988-2022 Free Software Foundation, Inc.
c906108c 4
c5aa993b 5 This file is part of GDB.
c906108c 6
c5aa993b
JM
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
a9762ec7 9 the Free Software Foundation; either version 3 of the License, or
c5aa993b 10 (at your option) any later version.
c906108c 11
c5aa993b
JM
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.
c906108c 16
c5aa993b 17 You should have received a copy of the GNU General Public License
a9762ec7 18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c 19
0baeab03
PA
20#include "defs.h"
21
4de283e4 22#include <ctype.h> /* XXX for isupper (). */
34e8f22d 23
4de283e4
TT
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"
d55e5aa6 35#include "arch-utils.h"
4de283e4
TT
36#include "osabi.h"
37#include "frame-unwind.h"
38#include "frame-base.h"
39#include "trad-frame.h"
40#include "objfiles.h"
a01567f4 41#include "dwarf2.h"
82ca8957 42#include "dwarf2/frame.h"
4de283e4
TT
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"
5f661e03 49#include "count-one-bits.h"
4de283e4 50
d55e5aa6 51#include "arch/arm.h"
4de283e4 52#include "arch/arm-get-next-pcs.h"
34e8f22d 53#include "arm-tdep.h"
4de283e4
TT
54#include "gdb/sim-arm.h"
55
d55e5aa6 56#include "elf-bfd.h"
4de283e4 57#include "coff/internal.h"
d55e5aa6 58#include "elf/arm.h"
4de283e4 59
4de283e4
TT
60#include "record.h"
61#include "record-full.h"
62#include <algorithm>
63
c2fd7fae
AKS
64#include "producer.h"
65
b121eeb9 66#if GDB_SELF_TEST
268a13a5 67#include "gdbsupport/selftest.h"
b121eeb9
YQ
68#endif
69
491144b5 70static bool arm_debug;
6529d2dd 71
7cb6d92a
SM
72/* Print an "arm" debug statement. */
73
74#define arm_debug_printf(fmt, ...) \
75 debug_prefixed_printf_cond (arm_debug, "arm", fmt, ##__VA_ARGS__)
76
082fc60d
RE
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
f594e5e9 79 is used for this purpose.
082fc60d
RE
80
81 MSYMBOL_SET_SPECIAL Actually sets the "special" bit.
f594e5e9 82 MSYMBOL_IS_SPECIAL Tests the "special" bit in a minimal symbol. */
082fc60d 83
0963b4bd 84#define MSYMBOL_SET_SPECIAL(msym) \
e165fcef 85 (msym)->set_target_flag_1 (true)
082fc60d
RE
86
87#define MSYMBOL_IS_SPECIAL(msym) \
e165fcef 88 (msym)->target_flag_1 ()
082fc60d 89
60c5725c
DJ
90struct arm_mapping_symbol
91{
227031b2 92 CORE_ADDR value;
60c5725c 93 char type;
54cc7474
SM
94
95 bool operator< (const arm_mapping_symbol &other) const
96 { return this->value < other.value; }
60c5725c 97};
54cc7474
SM
98
99typedef std::vector<arm_mapping_symbol> arm_mapping_symbol_vec;
60c5725c 100
bd5766ec 101struct arm_per_bfd
60c5725c 102{
bd5766ec 103 explicit arm_per_bfd (size_t num_sections)
4838e44c
SM
104 : section_maps (new arm_mapping_symbol_vec[num_sections]),
105 section_maps_sorted (new bool[num_sections] ())
54cc7474
SM
106 {}
107
bd5766ec 108 DISABLE_COPY_AND_ASSIGN (arm_per_bfd);
54cc7474
SM
109
110 /* Information about mapping symbols ($a, $d, $t) in the objfile.
111
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).
115
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;
4838e44c
SM
119
120 /* For each corresponding element of section_maps above, is this vector
121 sorted. */
122 std::unique_ptr<bool[]> section_maps_sorted;
60c5725c
DJ
123};
124
bd5766ec 125/* Per-bfd data used for mapping symbols. */
08b8a139 126static const registry<bfd>::key<arm_per_bfd> arm_bfd_data_key;
1b7f24cd 127
afd7eef0
RE
128/* The list of available "set arm ..." and "show arm ..." commands. */
129static struct cmd_list_element *setarmcmdlist = NULL;
130static struct cmd_list_element *showarmcmdlist = NULL;
131
fd50bc42
RE
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. */
40478521 134static const char *const fp_model_strings[] =
fd50bc42
RE
135{
136 "auto",
137 "softfpa",
138 "fpa",
139 "softvfp",
28e97307
DJ
140 "vfp",
141 NULL
fd50bc42
RE
142};
143
144/* A variable that can be configured by the user. */
145static enum arm_float_model arm_fp_model = ARM_FLOAT_AUTO;
146static const char *current_fp_model = "auto";
147
28e97307 148/* The ABI to use. Keep this in sync with arm_abi_kind. */
40478521 149static const char *const arm_abi_strings[] =
28e97307
DJ
150{
151 "auto",
152 "APCS",
153 "AAPCS",
154 NULL
155};
156
157/* A variable that can be configured by the user. */
158static enum arm_abi_kind arm_abi_global = ARM_ABI_AUTO;
159static const char *arm_abi_string = "auto";
160
0428b8f5 161/* The execution mode to assume. */
40478521 162static const char *const arm_mode_strings[] =
0428b8f5
DJ
163 {
164 "auto",
165 "arm",
68770265
MGD
166 "thumb",
167 NULL
0428b8f5
DJ
168 };
169
170static const char *arm_fallback_mode_string = "auto";
171static const char *arm_force_mode_string = "auto";
172
f32bf4a4
YQ
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. */
123dc839
DJ
177static const struct
178{
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 },
123dc839 217 { "lr", 14 },
123dc839
DJ
218 /* Names used by GCC (not listed in the ARM EABI). */
219 { "sl", 10 },
123dc839
DJ
220 /* A special name from the older ATPCS. */
221 { "wr", 7 },
222};
bc90b915 223
123dc839 224static const char *const arm_register_names[] =
da59e081
JM
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 */
94c30b78 231 "fps", "cpsr" }; /* 24 25 */
ed9a39eb 232
65b48a81
PB
233/* Holds the current set of options to be passed to the disassembler. */
234static char *arm_disassembler_options;
235
afd7eef0
RE
236/* Valid register name styles. */
237static const char **valid_disassembly_styles;
ed9a39eb 238
afd7eef0
RE
239/* Disassembly style to use. Default to "std" register names. */
240static const char *disassembly_style;
96baa820 241
d105cce5 242/* All possible arm target descriptors. */
92d48a1e 243static struct target_desc *tdesc_arm_list[ARM_FP_TYPE_INVALID][2];
d105cce5
AH
244static struct target_desc *tdesc_arm_mprofile_list[ARM_M_TYPE_INVALID];
245
ed9a39eb 246/* This is used to keep the bfd arch_info in sync with the disassembly
afd7eef0 247 style. */
eb4c3f4a 248static void set_disassembly_style_sfunc (const char *, int,
ed9a39eb 249 struct cmd_list_element *);
65b48a81
PB
250static void show_disassembly_style_sfunc (struct ui_file *, int,
251 struct cmd_list_element *,
252 const char *);
ed9a39eb 253
05d1431c 254static enum register_status arm_neon_quad_read (struct gdbarch *gdbarch,
849d0ba8 255 readable_regcache *regcache,
05d1431c 256 int regnum, gdb_byte *buf);
58d6951d
DJ
257static void arm_neon_quad_write (struct gdbarch *gdbarch,
258 struct regcache *regcache,
259 int regnum, const gdb_byte *buf);
260
e7cf25a8 261static CORE_ADDR
553cb527 262 arm_get_next_pcs_syscall_next_pc (struct arm_get_next_pcs *self);
e7cf25a8
YQ
263
264
d9311bfa
AT
265/* get_next_pcs operations. */
266static 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,
ed443b61
YQ
270 arm_get_next_pcs_is_thumb,
271 NULL,
d9311bfa
AT
272};
273
9b8d791a 274struct arm_prologue_cache
c3b4394c 275{
eb5492fa
DJ
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. */
ae7e2f45
CL
279 CORE_ADDR sp;
280
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;
288
289 /* Active stack pointer. */
290 int active_sp_regnum;
0d12d61b
YR
291 int active_msp_regnum;
292 int active_psp_regnum;
eb5492fa 293
4be43953
DJ
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. */
eb5492fa 297
c3b4394c 298 int framesize;
eb5492fa
DJ
299
300 /* The register used to hold the frame pointer for this frame. */
c3b4394c 301 int framereg;
eb5492fa 302
a01567f4
LM
303 /* True if the return address is signed, false otherwise. */
304 gdb::optional<bool> ra_signed_state;
305
eb5492fa 306 /* Saved register offsets. */
098caef4 307 trad_frame_saved_reg *saved_regs;
0824193f
CL
308
309 arm_prologue_cache() = default;
c3b4394c 310};
ed9a39eb 311
8c9ae6df
YR
312
313/* Reconstruct T bit in program status register from LR value. */
314
315static inline ULONGEST
316reconstruct_t_bit(struct gdbarch *gdbarch, CORE_ADDR lr, ULONGEST psr)
317{
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;
323
324 return psr;
325}
326
ae7e2f45
CL
327/* Initialize stack pointers, and flag the active one. */
328
329static inline void
330arm_cache_init_sp (int regnum, CORE_ADDR* member,
331 struct arm_prologue_cache *cache,
332 struct frame_info *frame)
333{
334 CORE_ADDR val = get_frame_register_unsigned (frame, regnum);
335 if (val == cache->sp)
336 cache->active_sp_regnum = regnum;
337
338 *member = val;
339}
340
0824193f
CL
341/* Initialize CACHE fields for which zero is not adequate (CACHE is
342 expected to have been ZALLOC'ed before calling this function). */
343
344static void
345arm_cache_init (struct arm_prologue_cache *cache, struct gdbarch *gdbarch)
346{
ae7e2f45
CL
347 cache->active_sp_regnum = ARM_SP_REGNUM;
348
0824193f
CL
349 cache->saved_regs = trad_frame_alloc_saved_regs (gdbarch);
350}
351
352/* Similar to the previous function, but extracts GDBARCH from FRAME. */
353
354static void
355arm_cache_init (struct arm_prologue_cache *cache, struct frame_info *frame)
356{
357 struct gdbarch *gdbarch = get_frame_arch (frame);
08106042 358 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
0824193f
CL
359
360 arm_cache_init (cache, gdbarch);
fe642a5b 361 cache->sp = get_frame_register_unsigned (frame, ARM_SP_REGNUM);
ae7e2f45
CL
362
363 if (tdep->have_sec_ext)
364 {
0d12d61b
YR
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);
367
ae7e2f45
CL
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);
372
0d12d61b
YR
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;
381
ae7e2f45
CL
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;
385 }
386 else if (tdep->is_m)
387 {
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);
390 }
391 else
392 arm_cache_init_sp (ARM_SP_REGNUM, &cache->msp_s, cache, frame);
393}
394
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. */
398
399static CORE_ADDR
400arm_cache_get_sp_register (struct arm_prologue_cache *cache,
401 arm_gdbarch_tdep *tdep, int regnum)
402{
ae7e2f45
CL
403 if (tdep->have_sec_ext)
404 {
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;
0d12d61b
YR
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);
b9b66a3a
YR
417 if (regnum == ARM_SP_REGNUM)
418 return arm_cache_get_sp_register (cache, tdep, cache->active_sp_regnum);
ae7e2f45
CL
419 }
420 else if (tdep->is_m)
421 {
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;
b9b66a3a
YR
426 if (regnum == ARM_SP_REGNUM)
427 return arm_cache_get_sp_register (cache, tdep, cache->active_sp_regnum);
ae7e2f45 428 }
b9b66a3a
YR
429 else if (regnum == ARM_SP_REGNUM)
430 return cache->sp;
ae7e2f45
CL
431
432 gdb_assert_not_reached ("Invalid SP selection");
433}
434
435/* Return the previous stack address, depending on which SP register
436 is active. */
437
438static CORE_ADDR
439arm_cache_get_prev_sp_value (struct arm_prologue_cache *cache, arm_gdbarch_tdep *tdep)
440{
441 CORE_ADDR val = arm_cache_get_sp_register (cache, tdep, cache->active_sp_regnum);
442 return val;
443}
444
445/* Set the active stack pointer to VAL. */
446
447static void
448arm_cache_set_active_sp_value (struct arm_prologue_cache *cache,
449 arm_gdbarch_tdep *tdep, CORE_ADDR val)
450{
ae7e2f45
CL
451 if (tdep->have_sec_ext)
452 {
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;
461
462 return;
463 }
464 else if (tdep->is_m)
465 {
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;
470
471 return;
472 }
b9b66a3a
YR
473 else if (cache->active_sp_regnum == ARM_SP_REGNUM)
474 {
475 cache->sp = val;
476 return;
477 }
ae7e2f45
CL
478
479 gdb_assert_not_reached ("Invalid SP selection");
0824193f
CL
480}
481
d65edaa0 482/* Return true if REGNUM is one of the alternative stack pointers. */
ef273377
CL
483
484static bool
d65edaa0 485arm_is_alternative_sp_register (arm_gdbarch_tdep *tdep, int regnum)
ef273377 486{
d65edaa0 487 if ((regnum == tdep->m_profile_msp_regnum)
ef273377
CL
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;
496}
497
498/* Set the active stack pointer to SP_REGNUM. */
499
500static void
501arm_cache_switch_prev_sp (struct arm_prologue_cache *cache,
502 arm_gdbarch_tdep *tdep, int sp_regnum)
503{
d65edaa0 504 gdb_assert (arm_is_alternative_sp_register (tdep, sp_regnum));
ef273377
CL
505
506 if (tdep->have_sec_ext)
507 gdb_assert (sp_regnum != tdep->m_profile_msp_regnum
508 && sp_regnum != tdep->m_profile_psp_regnum);
509
510 cache->active_sp_regnum = sp_regnum;
511}
512
9ecab40c
SM
513namespace {
514
515/* Abstract class to read ARM instructions from memory. */
516
517class arm_instruction_reader
518{
519public:
2c5b1849 520 /* Read a 4 bytes instruction from memory using the BYTE_ORDER endianness. */
9ecab40c
SM
521 virtual uint32_t read (CORE_ADDR memaddr, bfd_endian byte_order) const = 0;
522};
523
524/* Read instructions from target memory. */
525
526class target_arm_instruction_reader : public arm_instruction_reader
527{
528public:
529 uint32_t read (CORE_ADDR memaddr, bfd_endian byte_order) const override
530 {
531 return read_code_unsigned_integer (memaddr, 4, byte_order);
532 }
533};
534
535} /* namespace */
536
537static 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);
0d39a070 540
cca44b1b
JB
541/* Architecture version for displaced stepping. This effects the behaviour of
542 certain instructions, and really should not be hard-wired. */
543
544#define DISPLACED_STEPPING_ARCH_VERSION 5
545
c7ae7675 546/* See arm-tdep.h. */
c906108c 547
491144b5 548bool arm_apcs_32 = true;
ef273377 549bool arm_unwind_secure_frames = true;
c906108c 550
9779414d
DJ
551/* Return the bit mask in ARM_PS_REGNUM that indicates Thumb mode. */
552
478fd957 553int
9779414d
DJ
554arm_psr_thumb_bit (struct gdbarch *gdbarch)
555{
08106042 556 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
345bd07c
SM
557
558 if (tdep->is_m)
9779414d
DJ
559 return XPSR_T;
560 else
561 return CPSR_T;
562}
563
d0e59a68
AT
564/* Determine if the processor is currently executing in Thumb mode. */
565
566int
567arm_is_thumb (struct regcache *regcache)
568{
569 ULONGEST cpsr;
ac7936df 570 ULONGEST t_bit = arm_psr_thumb_bit (regcache->arch ());
d0e59a68
AT
571
572 cpsr = regcache_raw_get_unsigned (regcache, ARM_PS_REGNUM);
573
574 return (cpsr & t_bit) != 0;
575}
576
60270718
AB
577/* Determine if FRAME is executing in Thumb mode. FRAME must be an ARM
578 frame. */
b39cc962 579
25b41d01 580int
b39cc962
DJ
581arm_frame_is_thumb (struct frame_info *frame)
582{
60270718
AB
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);
b39cc962
DJ
586
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. */
60270718 591 CORE_ADDR cpsr = get_frame_register_unsigned (frame, ARM_PS_REGNUM);
b39cc962 592
60270718
AB
593 /* Find and extract the thumb bit. */
594 ULONGEST t_bit = arm_psr_thumb_bit (gdbarch);
9779414d 595 return (cpsr & t_bit) != 0;
b39cc962
DJ
596}
597
f9d67f43
DJ
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. */
c906108c 601
f9d67f43
DJ
602static char
603arm_find_mapping_symbol (CORE_ADDR memaddr, CORE_ADDR *start)
c906108c 604{
60c5725c 605 struct obj_section *sec;
0428b8f5 606
60c5725c
DJ
607 /* If there are mapping symbols, consult them. */
608 sec = find_pc_section (memaddr);
609 if (sec != NULL)
610 {
98badbfd 611 arm_per_bfd *data = arm_bfd_data_key.get (sec->objfile->obfd.get ());
60c5725c
DJ
612 if (data != NULL)
613 {
4838e44c
SM
614 unsigned int section_idx = sec->the_bfd_section->index;
615 arm_mapping_symbol_vec &map
616 = data->section_maps[section_idx];
617
618 /* Sort the vector on first use. */
619 if (!data->section_maps_sorted[section_idx])
620 {
621 std::sort (map.begin (), map.end ());
622 data->section_maps_sorted[section_idx] = true;
623 }
624
0c1bcd23 625 arm_mapping_symbol map_key = { memaddr - sec->addr (), 0 };
54cc7474
SM
626 arm_mapping_symbol_vec::const_iterator it
627 = std::lower_bound (map.begin (), map.end (), map_key);
628
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 ())
60c5725c 634 {
54cc7474 635 if (it->value == map_key.value)
60c5725c 636 {
f9d67f43 637 if (start)
0c1bcd23 638 *start = it->value + sec->addr ();
54cc7474 639 return it->type;
60c5725c
DJ
640 }
641 }
54cc7474
SM
642
643 if (it > map.begin ())
644 {
645 arm_mapping_symbol_vec::const_iterator prev_it
646 = it - 1;
647
648 if (start)
0c1bcd23 649 *start = prev_it->value + sec->addr ();
54cc7474
SM
650 return prev_it->type;
651 }
60c5725c
DJ
652 }
653 }
654
f9d67f43
DJ
655 return 0;
656}
657
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. */
661
e3039479 662int
9779414d 663arm_pc_is_thumb (struct gdbarch *gdbarch, CORE_ADDR memaddr)
f9d67f43 664{
7cbd4a93 665 struct bound_minimal_symbol sym;
f9d67f43 666 char type;
187b041e 667 arm_displaced_step_copy_insn_closure *dsc = nullptr;
08106042 668 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
187b041e
SM
669
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));
a42244db
YQ
674
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)
678 {
136821d9
SM
679 displaced_debug_printf ("check mode of %.8lx instead of %.8lx",
680 (unsigned long) dsc->insn_addr,
681 (unsigned long) memaddr);
a42244db
YQ
682 memaddr = dsc->insn_addr;
683 }
f9d67f43
DJ
684
685 /* If bit 0 of the address is set, assume this is a Thumb address. */
686 if (IS_THUMB_ADDR (memaddr))
687 return 1;
688
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;
694
9779414d 695 /* ARM v6-M and v7-M are always in Thumb mode. */
345bd07c 696 if (tdep->is_m)
9779414d
DJ
697 return 1;
698
f9d67f43
DJ
699 /* If there are mapping symbols, consult them. */
700 type = arm_find_mapping_symbol (memaddr, NULL);
701 if (type)
702 return type == 't';
703
ed9a39eb 704 /* Thumb functions have a "special" bit set in minimal symbols. */
c906108c 705 sym = lookup_minimal_symbol_by_pc (memaddr);
7cbd4a93
TT
706 if (sym.minsym)
707 return (MSYMBOL_IS_SPECIAL (sym.minsym));
0428b8f5
DJ
708
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;
714
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
18819fa6 719 displayed in the mode it will be executed). */
9dccd06e 720 if (target_has_registers ())
18819fa6 721 return arm_frame_is_thumb (get_current_frame ());
0428b8f5
DJ
722
723 /* Otherwise we're out of luck; we assume ARM. */
724 return 0;
c906108c
SS
725}
726
ca90e760 727/* Determine if the address specified equals any of these magic return
55ea94da 728 values, called EXC_RETURN, defined by the ARM v6-M, v7-M and v8-M
ca90e760
FH
729 architectures.
730
731 From ARMv6-M Reference Manual B1.5.8
732 Table B1-5 Exception return behavior
733
734 EXC_RETURN Return To Return Stack
735 0xFFFFFFF1 Handler mode Main
736 0xFFFFFFF9 Thread mode Main
737 0xFFFFFFFD Thread mode Process
738
739 From ARMv7-M Reference Manual B1.5.8
740 Table B1-8 EXC_RETURN definition of exception return behavior, no FP
741
742 EXC_RETURN Return To Return Stack
743 0xFFFFFFF1 Handler mode Main
744 0xFFFFFFF9 Thread mode Main
745 0xFFFFFFFD Thread mode Process
746
747 Table B1-9 EXC_RETURN definition of exception return behavior, with
748 FP
749
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
757
758 For more details see "B1.5.8 Exception return behavior"
55ea94da
FH
759 in both ARMv6-M and ARMv7-M Architecture Reference Manuals.
760
761 In the ARMv8-M Architecture Technical Reference also adds
762 for implementations without the Security Extension:
763
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. */
ca90e760
FH
768
769static int
ef273377
CL
770arm_m_addr_is_magic (struct gdbarch *gdbarch, CORE_ADDR addr)
771{
08106042 772 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
ef273377
CL
773 if (tdep->have_sec_ext)
774 {
775 switch ((addr & 0xff000000))
776 {
777 case 0xff000000: /* EXC_RETURN pattern. */
778 case 0xfe000000: /* FNC_RETURN pattern. */
779 return 1;
780 default:
781 return 0;
782 }
783 }
784 else
785 {
786 switch (addr)
787 {
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;
ca90e760 802
ef273377
CL
803 default:
804 /* Address is not magic. */
805 return 0;
806 }
ca90e760
FH
807 }
808}
809
181c1381 810/* Remove useless bits from addresses in a running program. */
34e8f22d 811static CORE_ADDR
24568a2c 812arm_addr_bits_remove (struct gdbarch *gdbarch, CORE_ADDR val)
c906108c 813{
08106042 814 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
345bd07c 815
2ae28aa9
YQ
816 /* On M-profile devices, do not strip the low bit from EXC_RETURN
817 (the magic exception return address). */
ef273377 818 if (tdep->is_m && arm_m_addr_is_magic (gdbarch, val))
2ae28aa9
YQ
819 return val;
820
a3a2ee65 821 if (arm_apcs_32)
dd6be234 822 return UNMAKE_THUMB_ADDR (val);
c906108c 823 else
a3a2ee65 824 return (val & 0x03fffffc);
c906108c
SS
825}
826
0d39a070 827/* Return 1 if PC is the start of a compiler helper function which
e0634ccf
UW
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. */
0d39a070 831static int
e0634ccf 832skip_prologue_function (struct gdbarch *gdbarch, CORE_ADDR pc, int is_thumb)
0d39a070 833{
e0634ccf 834 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
7cbd4a93 835 struct bound_minimal_symbol msym;
0d39a070
DJ
836
837 msym = lookup_minimal_symbol_by_pc (pc);
7cbd4a93 838 if (msym.minsym != NULL
4aeddc50 839 && msym.value_address () == pc
c9d95fa3 840 && msym.minsym->linkage_name () != NULL)
e0634ccf 841 {
c9d95fa3 842 const char *name = msym.minsym->linkage_name ();
0d39a070 843
e0634ccf
UW
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;
0d39a070 848
e0634ccf
UW
849 /* On soft-float targets, __truncdfsf2 is called to convert promoted
850 arguments to their argument types in non-prototyped
851 functions. */
61012eef 852 if (startswith (name, "__truncdfsf2"))
e0634ccf 853 return 1;
61012eef 854 if (startswith (name, "__aeabi_d2f"))
e0634ccf 855 return 1;
0d39a070 856
e0634ccf 857 /* Internal functions related to thread-local storage. */
61012eef 858 if (startswith (name, "__tls_get_addr"))
e0634ccf 859 return 1;
61012eef 860 if (startswith (name, "__aeabi_read_tp"))
e0634ccf
UW
861 return 1;
862 }
863 else
864 {
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). */
869
870 if (!is_thumb
198cd59d 871 && read_code_unsigned_integer (pc, 4, byte_order_for_code)
e0634ccf 872 == 0xe3e00a0f /* mov r0, #0xffff0fff */
198cd59d 873 && read_code_unsigned_integer (pc + 4, 4, byte_order_for_code)
e0634ccf
UW
874 == 0xe240f01f) /* sub pc, r0, #31 */
875 return 1;
876 }
ec3d575a 877
0d39a070
DJ
878 return 0;
879}
880
621c6d5b
YQ
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))
889
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))
895
ec3d575a
UW
896/* Decode immediate value; implements ThumbExpandImmediate pseudo-op. */
897
898static unsigned int
899thumb_expand_immediate (unsigned int imm)
900{
901 unsigned int count = imm >> 7;
902
903 if (count < 8)
904 switch (count / 2)
905 {
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);
915 }
916
917 return (0x80 | (imm & 0x7f)) << (32 - count);
918}
919
540314bd
YQ
920/* Return 1 if the 16-bit Thumb instruction INSN restores SP in
921 epilogue, 0 otherwise. */
922
923static int
924thumb_instruction_restores_sp (unsigned short insn)
925{
926 return (insn == 0x46bd /* mov sp, r7 */
927 || (insn & 0xff80) == 0xb000 /* add sp, imm */
928 || (insn & 0xfe00) == 0xbc00); /* pop <registers> */
929}
930
29d73ae4
DJ
931/* Analyze a Thumb prologue, looking for a recognizable stack frame
932 and frame pointer. Scan until we encounter a store that could
0d39a070
DJ
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. */
c906108c
SS
936
937static CORE_ADDR
29d73ae4
DJ
938thumb_analyze_prologue (struct gdbarch *gdbarch,
939 CORE_ADDR start, CORE_ADDR limit,
940 struct arm_prologue_cache *cache)
c906108c 941{
08106042 942 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
0d39a070 943 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
e17a4113 944 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
29d73ae4
DJ
945 int i;
946 pv_t regs[16];
29d73ae4 947 CORE_ADDR offset;
ec3d575a 948 CORE_ADDR unrecognized_pc = 0;
da3c6d4a 949
29d73ae4
DJ
950 for (i = 0; i < 16; i++)
951 regs[i] = pv_register (i, 0);
f7b7ed97 952 pv_area stack (ARM_SP_REGNUM, gdbarch_addr_bit (gdbarch));
29d73ae4 953
29d73ae4 954 while (start < limit)
c906108c 955 {
29d73ae4 956 unsigned short insn;
a01567f4 957 gdb::optional<bool> ra_signed_state;
29d73ae4 958
198cd59d 959 insn = read_code_unsigned_integer (start, 2, byte_order_for_code);
9d4fde75 960
94c30b78 961 if ((insn & 0xfe00) == 0xb400) /* push { rlist } */
da59e081 962 {
29d73ae4
DJ
963 int regno;
964 int mask;
4be43953 965
f7b7ed97 966 if (stack.store_would_trash (regs[ARM_SP_REGNUM]))
4be43953 967 break;
29d73ae4
DJ
968
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);
972
973 /* Calculate offsets of saved R0-R7 and LR. */
974 for (regno = ARM_LR_REGNUM; regno >= 0; regno--)
975 if (mask & (1 << regno))
976 {
29d73ae4
DJ
977 regs[ARM_SP_REGNUM] = pv_add_constant (regs[ARM_SP_REGNUM],
978 -4);
f7b7ed97 979 stack.store (regs[ARM_SP_REGNUM], 4, regs[regno]);
29d73ae4 980 }
da59e081 981 }
1db01f22 982 else if ((insn & 0xff80) == 0xb080) /* sub sp, #imm */
da59e081 983 {
29d73ae4 984 offset = (insn & 0x7f) << 2; /* get scaled offset */
1db01f22
YQ
985 regs[ARM_SP_REGNUM] = pv_add_constant (regs[ARM_SP_REGNUM],
986 -offset);
da59e081 987 }
808f7ab1
YQ
988 else if (thumb_instruction_restores_sp (insn))
989 {
990 /* Don't scan past the epilogue. */
991 break;
992 }
0d39a070
DJ
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)]))
1011 {
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]);
1015 }
29d73ae4 1016 else if ((insn & 0xff00) == 0x4600) /* mov hi, lo or mov lo, hi */
da59e081 1017 {
29d73ae4
DJ
1018 int dst_reg = (insn & 0x7) + ((insn & 0x80) >> 4);
1019 int src_reg = (insn & 0x78) >> 3;
1020 regs[dst_reg] = regs[src_reg];
da59e081 1021 }
29d73ae4 1022 else if ((insn & 0xf800) == 0x9000) /* str rd, [sp, #off] */
da59e081 1023 {
29d73ae4
DJ
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;
1029
1030 offset = (insn & 0xff) << 2;
1031 addr = pv_add_constant (regs[ARM_SP_REGNUM], offset);
1032
f7b7ed97 1033 if (stack.store_would_trash (addr))
29d73ae4
DJ
1034 break;
1035
f7b7ed97 1036 stack.store (addr, 4, regs[regno]);
da59e081 1037 }
0d39a070
DJ
1038 else if ((insn & 0xf800) == 0x6000) /* str rd, [rn, #off] */
1039 {
1040 int rd = bits (insn, 0, 2);
1041 int rn = bits (insn, 3, 5);
1042 pv_t addr;
1043
1044 offset = bits (insn, 6, 10) << 2;
1045 addr = pv_add_constant (regs[rn], offset);
1046
f7b7ed97 1047 if (stack.store_would_trash (addr))
0d39a070
DJ
1048 break;
1049
f7b7ed97 1050 stack.store (addr, 4, regs[rd]);
0d39a070
DJ
1051 }
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. */
1056 ;
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. */
1061 ;
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. */
1066 ;
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. */
1071 ;
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] */
1077 {
1078 /* Constant pool loads, for the same reason. */
1079 unsigned int constant;
1080 CORE_ADDR loc;
1081
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);
1085 }
db24da6d 1086 else if (thumb_insn_size (insn) == 4) /* 32-bit Thumb-2 instructions. */
0d39a070 1087 {
0d39a070
DJ
1088 unsigned short inst2;
1089
198cd59d
YQ
1090 inst2 = read_code_unsigned_integer (start + 2, 2,
1091 byte_order_for_code);
a01567f4 1092 uint32_t whole_insn = (insn << 16) | inst2;
0d39a070
DJ
1093
1094 if ((insn & 0xf800) == 0xf000 && (inst2 & 0xe800) == 0xe800)
1095 {
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;
1101
1102 imm1 = sbits (insn, 0, 10);
1103 imm2 = bits (inst2, 0, 10);
1104 j1 = bit (inst2, 13);
1105 j2 = bit (inst2, 11);
1106
1107 offset = ((imm1 << 12) + (imm2 << 1));
1108 offset ^= ((!j2) << 22) | ((!j1) << 23);
1109
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;
1114
e0634ccf
UW
1115 if (!skip_prologue_function (gdbarch, nextpc,
1116 bit (inst2, 12) != 0))
0d39a070
DJ
1117 break;
1118 }
ec3d575a 1119
0963b4bd
MS
1120 else if ((insn & 0xffd0) == 0xe900 /* stmdb Rn{!},
1121 { registers } */
ec3d575a
UW
1122 && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
1123 {
1124 pv_t addr = regs[bits (insn, 0, 3)];
1125 int regno;
1126
f7b7ed97 1127 if (stack.store_would_trash (addr))
ec3d575a
UW
1128 break;
1129
1130 /* Calculate offsets of saved registers. */
1131 for (regno = ARM_LR_REGNUM; regno >= 0; regno--)
1132 if (inst2 & (1 << regno))
1133 {
1134 addr = pv_add_constant (addr, -4);
f7b7ed97 1135 stack.store (addr, 4, regs[regno]);
ec3d575a
UW
1136 }
1137
1138 if (insn & 0x0020)
1139 regs[bits (insn, 0, 3)] = addr;
1140 }
1141
fcaa1071
CL
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))
1146 {
1147 /* Address SP points to. */
1148 pv_t addr = regs[bits (insn, 0, 3)];
1149
1150 /* Number of registers saved. */
1151 unsigned int number = bits (inst2, 0, 7) >> 1;
1152
1153 /* First register to save. */
1154 int vd = bits (inst2, 12, 15) | (bits (insn, 6, 6) << 4);
1155
1156 if (stack.store_would_trash (addr))
1157 break;
1158
1159 /* Calculate offsets of saved registers. */
1160 for (; number > 0; number--)
1161 {
1162 addr = pv_add_constant (addr, -8);
1163 stack.store (addr, 8, pv_register (ARM_D0_REGNUM
1164 + vd + number, 0));
1165 }
1166
1167 /* Writeback SP to account for the saved registers. */
1168 regs[bits (insn, 0, 3)] = addr;
1169 }
1170
0963b4bd
MS
1171 else if ((insn & 0xff50) == 0xe940 /* strd Rt, Rt2,
1172 [Rn, #+/-imm]{!} */
ec3d575a
UW
1173 && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
1174 {
1175 int regno1 = bits (inst2, 12, 15);
1176 int regno2 = bits (inst2, 8, 11);
1177 pv_t addr = regs[bits (insn, 0, 3)];
1178
1179 offset = inst2 & 0xff;
1180 if (insn & 0x0080)
1181 addr = pv_add_constant (addr, offset);
1182 else
1183 addr = pv_add_constant (addr, -offset);
1184
f7b7ed97 1185 if (stack.store_would_trash (addr))
ec3d575a
UW
1186 break;
1187
f7b7ed97
TT
1188 stack.store (addr, 4, regs[regno1]);
1189 stack.store (pv_add_constant (addr, 4),
1190 4, regs[regno2]);
ec3d575a
UW
1191
1192 if (insn & 0x0020)
1193 regs[bits (insn, 0, 3)] = addr;
1194 }
1195
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))
1199 {
1200 int regno = bits (inst2, 12, 15);
1201 pv_t addr = regs[bits (insn, 0, 3)];
1202
1203 offset = inst2 & 0xff;
1204 if (inst2 & 0x0200)
1205 addr = pv_add_constant (addr, offset);
1206 else
1207 addr = pv_add_constant (addr, -offset);
1208
f7b7ed97 1209 if (stack.store_would_trash (addr))
ec3d575a
UW
1210 break;
1211
f7b7ed97 1212 stack.store (addr, 4, regs[regno]);
ec3d575a
UW
1213
1214 if (inst2 & 0x0100)
1215 regs[bits (insn, 0, 3)] = addr;
1216 }
1217
1218 else if ((insn & 0xfff0) == 0xf8c0 /* str.w Rt,[Rn,#imm] */
1219 && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
1220 {
1221 int regno = bits (inst2, 12, 15);
1222 pv_t addr;
1223
1224 offset = inst2 & 0xfff;
1225 addr = pv_add_constant (regs[bits (insn, 0, 3)], offset);
1226
f7b7ed97 1227 if (stack.store_would_trash (addr))
ec3d575a
UW
1228 break;
1229
f7b7ed97 1230 stack.store (addr, 4, regs[regno]);
ec3d575a
UW
1231 }
1232
1233 else if ((insn & 0xffd0) == 0xf880 /* str{bh}.w Rt,[Rn,#imm] */
0d39a070 1234 && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
ec3d575a 1235 /* Ignore stores of argument registers to the stack. */
0d39a070 1236 ;
ec3d575a
UW
1237
1238 else if ((insn & 0xffd0) == 0xf800 /* str{bh} Rt,[Rn,#+/-imm] */
1239 && (inst2 & 0x0d00) == 0x0c00
0d39a070 1240 && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
ec3d575a 1241 /* Ignore stores of argument registers to the stack. */
0d39a070 1242 ;
ec3d575a 1243
0963b4bd
MS
1244 else if ((insn & 0xffd0) == 0xe890 /* ldmia Rn[!],
1245 { registers } */
ec3d575a
UW
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. */
0d39a070 1250 ;
ec3d575a 1251
f8c6d152 1252 else if ((insn & 0xff70) == 0xe950 /* ldrd Rt, Rt2,
0963b4bd 1253 [Rn, #+/-imm] */
0d39a070 1254 && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
ec3d575a 1255 /* Similarly ignore dual loads from the stack. */
0d39a070 1256 ;
ec3d575a
UW
1257
1258 else if ((insn & 0xfff0) == 0xf850 /* ldr Rt,[Rn,#+/-imm] */
1259 && (inst2 & 0x0d00) == 0x0c00
0d39a070 1260 && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
ec3d575a 1261 /* Similarly ignore single loads from the stack. */
0d39a070 1262 ;
ec3d575a
UW
1263
1264 else if ((insn & 0xfff0) == 0xf8d0 /* ldr.w Rt,[Rn,#imm] */
0d39a070 1265 && pv_is_register (regs[bits (insn, 0, 3)], ARM_SP_REGNUM))
ec3d575a 1266 /* Similarly ignore single loads from the stack. */
0d39a070 1267 ;
ec3d575a
UW
1268
1269 else if ((insn & 0xfbf0) == 0xf100 /* add.w Rd, Rn, #imm */
1270 && (inst2 & 0x8000) == 0x0000)
1271 {
1272 unsigned int imm = ((bits (insn, 10, 10) << 11)
1273 | (bits (inst2, 12, 14) << 8)
1274 | bits (inst2, 0, 7));
1275
1276 regs[bits (inst2, 8, 11)]
1277 = pv_add_constant (regs[bits (insn, 0, 3)],
1278 thumb_expand_immediate (imm));
1279 }
1280
1281 else if ((insn & 0xfbf0) == 0xf200 /* addw Rd, Rn, #imm */
1282 && (inst2 & 0x8000) == 0x0000)
0d39a070 1283 {
ec3d575a
UW
1284 unsigned int imm = ((bits (insn, 10, 10) << 11)
1285 | (bits (inst2, 12, 14) << 8)
1286 | bits (inst2, 0, 7));
1287
1288 regs[bits (inst2, 8, 11)]
1289 = pv_add_constant (regs[bits (insn, 0, 3)], imm);
1290 }
1291
1292 else if ((insn & 0xfbf0) == 0xf1a0 /* sub.w Rd, Rn, #imm */
1293 && (inst2 & 0x8000) == 0x0000)
1294 {
1295 unsigned int imm = ((bits (insn, 10, 10) << 11)
1296 | (bits (inst2, 12, 14) << 8)
1297 | bits (inst2, 0, 7));
1298
1299 regs[bits (inst2, 8, 11)]
1300 = pv_add_constant (regs[bits (insn, 0, 3)],
1301 - (CORE_ADDR) thumb_expand_immediate (imm));
1302 }
1303
1304 else if ((insn & 0xfbf0) == 0xf2a0 /* subw Rd, Rn, #imm */
1305 && (inst2 & 0x8000) == 0x0000)
1306 {
1307 unsigned int imm = ((bits (insn, 10, 10) << 11)
1308 | (bits (inst2, 12, 14) << 8)
1309 | bits (inst2, 0, 7));
1310
1311 regs[bits (inst2, 8, 11)]
1312 = pv_add_constant (regs[bits (insn, 0, 3)], - (CORE_ADDR) imm);
1313 }
1314
1315 else if ((insn & 0xfbff) == 0xf04f) /* mov.w Rd, #const */
1316 {
1317 unsigned int imm = ((bits (insn, 10, 10) << 11)
1318 | (bits (inst2, 12, 14) << 8)
1319 | bits (inst2, 0, 7));
1320
1321 regs[bits (inst2, 8, 11)]
1322 = pv_constant (thumb_expand_immediate (imm));
1323 }
1324
1325 else if ((insn & 0xfbf0) == 0xf240) /* movw Rd, #const */
1326 {
621c6d5b
YQ
1327 unsigned int imm
1328 = EXTRACT_MOVW_MOVT_IMM_T (insn, inst2);
ec3d575a
UW
1329
1330 regs[bits (inst2, 8, 11)] = pv_constant (imm);
1331 }
1332
1333 else if (insn == 0xea5f /* mov.w Rd,Rm */
1334 && (inst2 & 0xf0f0) == 0)
1335 {
1336 int dst_reg = (inst2 & 0x0f00) >> 8;
1337 int src_reg = inst2 & 0xf;
1338 regs[dst_reg] = regs[src_reg];
1339 }
1340
1341 else if ((insn & 0xff7f) == 0xf85f) /* ldr.w Rt,<label> */
1342 {
1343 /* Constant pool loads. */
1344 unsigned int constant;
1345 CORE_ADDR loc;
1346
cac395ea 1347 offset = bits (inst2, 0, 11);
ec3d575a
UW
1348 if (insn & 0x0080)
1349 loc = start + 4 + offset;
1350 else
1351 loc = start + 4 - offset;
1352
1353 constant = read_memory_unsigned_integer (loc, 4, byte_order);
1354 regs[bits (inst2, 12, 15)] = pv_constant (constant);
1355 }
1356
1357 else if ((insn & 0xff7f) == 0xe95f) /* ldrd Rt,Rt2,<label> */
1358 {
1359 /* Constant pool loads. */
1360 unsigned int constant;
1361 CORE_ADDR loc;
1362
cac395ea 1363 offset = bits (inst2, 0, 7) << 2;
ec3d575a
UW
1364 if (insn & 0x0080)
1365 loc = start + 4 + offset;
1366 else
1367 loc = start + 4 - offset;
1368
1369 constant = read_memory_unsigned_integer (loc, 4, byte_order);
1370 regs[bits (inst2, 12, 15)] = pv_constant (constant);
1371
1372 constant = read_memory_unsigned_integer (loc + 4, 4, byte_order);
1373 regs[bits (inst2, 8, 11)] = pv_constant (constant);
1374 }
a01567f4
LM
1375 /* Start of ARMv8.1-m PACBTI extension instructions. */
1376 else if (IS_PAC (whole_insn))
1377 {
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;
1381 }
1382 else if (IS_PACBTI (whole_insn))
1383 {
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;
1388 }
1389 else if (IS_BTI (whole_insn))
1390 {
1391 /* Valid BTI landing pad. NOP space. */
1392 }
1393 else if (IS_PACG (whole_insn))
1394 {
1395 /* Sign Rn using Rm and store the PAC in Rd. Rd is signed from
1396 this point onwards. */
1397 ra_signed_state = true;
1398 }
1399 else if (IS_AUT (whole_insn) || IS_AUTG (whole_insn))
1400 {
1401 /* These instructions appear close to the epilogue, when signed
1402 pointers are getting authenticated. */
1403 ra_signed_state = false;
1404 }
1405 /* End of ARMv8.1-m PACBTI extension instructions */
ec3d575a
UW
1406 else if (thumb2_instruction_changes_pc (insn, inst2))
1407 {
1408 /* Don't scan past anything that might change control flow. */
0d39a070
DJ
1409 break;
1410 }
ec3d575a
UW
1411 else
1412 {
1413 /* The optimizer might shove anything into the prologue,
1414 so we just skip what we don't recognize. */
1415 unrecognized_pc = start;
1416 }
0d39a070 1417
a01567f4
LM
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 ())
1422 {
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;
1428 }
1429
0d39a070
DJ
1430 start += 2;
1431 }
ec3d575a 1432 else if (thumb_instruction_changes_pc (insn))
3d74b771 1433 {
ec3d575a 1434 /* Don't scan past anything that might change control flow. */
da3c6d4a 1435 break;
3d74b771 1436 }
ec3d575a
UW
1437 else
1438 {
1439 /* The optimizer might shove anything into the prologue,
1440 so we just skip what we don't recognize. */
1441 unrecognized_pc = start;
1442 }
29d73ae4
DJ
1443
1444 start += 2;
c906108c
SS
1445 }
1446
7cb6d92a
SM
1447 arm_debug_printf ("Prologue scan stopped at %s",
1448 paddress (gdbarch, start));
0d39a070 1449
ec3d575a
UW
1450 if (unrecognized_pc == 0)
1451 unrecognized_pc = start;
1452
29d73ae4 1453 if (cache == NULL)
f7b7ed97 1454 return unrecognized_pc;
29d73ae4 1455
29d73ae4
DJ
1456 if (pv_is_register (regs[ARM_FP_REGNUM], ARM_SP_REGNUM))
1457 {
1458 /* Frame pointer is fp. Frame size is constant. */
1459 cache->framereg = ARM_FP_REGNUM;
1460 cache->framesize = -regs[ARM_FP_REGNUM].k;
1461 }
1462 else if (pv_is_register (regs[THUMB_FP_REGNUM], ARM_SP_REGNUM))
1463 {
1464 /* Frame pointer is r7. Frame size is constant. */
1465 cache->framereg = THUMB_FP_REGNUM;
1466 cache->framesize = -regs[THUMB_FP_REGNUM].k;
1467 }
72a2e3dc 1468 else
29d73ae4
DJ
1469 {
1470 /* Try the stack pointer... this is a bit desperate. */
1471 cache->framereg = ARM_SP_REGNUM;
1472 cache->framesize = -regs[ARM_SP_REGNUM].k;
1473 }
29d73ae4 1474
de76473c 1475 for (i = 0; i < gdbarch_num_regs (gdbarch); i++)
f7b7ed97 1476 if (stack.find_reg (gdbarch, i, &offset))
10245fe8
YR
1477 {
1478 cache->saved_regs[i].set_addr (offset);
1479 if (i == ARM_SP_REGNUM)
1480 arm_cache_set_active_sp_value(cache, tdep, offset);
1481 }
29d73ae4 1482
ec3d575a 1483 return unrecognized_pc;
c906108c
SS
1484}
1485
621c6d5b
YQ
1486
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. */
1492
1493static CORE_ADDR
1494arm_analyze_load_stack_chk_guard(CORE_ADDR pc, struct gdbarch *gdbarch,
1495 unsigned int *destreg, int *offset)
1496{
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;
1500
1501 address = 0;
1502 if (is_thumb)
1503 {
1504 unsigned short insn1
198cd59d 1505 = read_code_unsigned_integer (pc, 2, byte_order_for_code);
621c6d5b
YQ
1506
1507 if ((insn1 & 0xf800) == 0x4800) /* ldr Rd, #immed */
1508 {
1509 *destreg = bits (insn1, 8, 10);
1510 *offset = 2;
6ae274b7
YQ
1511 address = (pc & 0xfffffffc) + 4 + (bits (insn1, 0, 7) << 2);
1512 address = read_memory_unsigned_integer (address, 4,
1513 byte_order_for_code);
621c6d5b
YQ
1514 }
1515 else if ((insn1 & 0xfbf0) == 0xf240) /* movw Rd, #const */
1516 {
1517 unsigned short insn2
198cd59d 1518 = read_code_unsigned_integer (pc + 2, 2, byte_order_for_code);
621c6d5b
YQ
1519
1520 low = EXTRACT_MOVW_MOVT_IMM_T (insn1, insn2);
1521
1522 insn1
198cd59d 1523 = read_code_unsigned_integer (pc + 4, 2, byte_order_for_code);
621c6d5b 1524 insn2
198cd59d 1525 = read_code_unsigned_integer (pc + 6, 2, byte_order_for_code);
621c6d5b
YQ
1526
1527 /* movt Rd, #const */
1528 if ((insn1 & 0xfbc0) == 0xf2c0)
1529 {
1530 high = EXTRACT_MOVW_MOVT_IMM_T (insn1, insn2);
1531 *destreg = bits (insn2, 8, 11);
1532 *offset = 8;
1533 address = (high << 16 | low);
1534 }
1535 }
1536 }
1537 else
1538 {
2e9e421f 1539 unsigned int insn
198cd59d 1540 = read_code_unsigned_integer (pc, 4, byte_order_for_code);
2e9e421f 1541
6ae274b7 1542 if ((insn & 0x0e5f0000) == 0x041f0000) /* ldr Rd, [PC, #immed] */
2e9e421f 1543 {
6ae274b7
YQ
1544 address = bits (insn, 0, 11) + pc + 8;
1545 address = read_memory_unsigned_integer (address, 4,
1546 byte_order_for_code);
1547
2e9e421f
UW
1548 *destreg = bits (insn, 12, 15);
1549 *offset = 4;
1550 }
1551 else if ((insn & 0x0ff00000) == 0x03000000) /* movw Rd, #const */
1552 {
1553 low = EXTRACT_MOVW_MOVT_IMM_A (insn);
1554
1555 insn
198cd59d 1556 = read_code_unsigned_integer (pc + 4, 4, byte_order_for_code);
2e9e421f
UW
1557
1558 if ((insn & 0x0ff00000) == 0x03400000) /* movt Rd, #const */
1559 {
1560 high = EXTRACT_MOVW_MOVT_IMM_A (insn);
1561 *destreg = bits (insn, 12, 15);
1562 *offset = 8;
1563 address = (high << 16 | low);
1564 }
1565 }
621c6d5b
YQ
1566 }
1567
1568 return address;
1569}
1570
1571/* Try to skip a sequence of instructions used for stack protector. If PC
0963b4bd
MS
1572 points to the first instruction of this sequence, return the address of
1573 first instruction after this sequence, otherwise, return original PC.
621c6d5b
YQ
1574
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.
1579
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,
1585
1586 movw Rn, #:lower16:__stack_chk_guard
1587 movt Rn, #:upper16:__stack_chk_guard
1588
1589 On ARMv5t, it is,
1590
1591 ldr Rn, .Label
1592 ....
1593 .Lable:
1594 .word __stack_chk_guard
1595
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. */
1600
1601static CORE_ADDR
1602arm_skip_stack_protector(CORE_ADDR pc, struct gdbarch *gdbarch)
1603{
1604 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
22e048c9 1605 unsigned int basereg;
7cbd4a93 1606 struct bound_minimal_symbol stack_chk_guard;
621c6d5b
YQ
1607 int offset;
1608 int is_thumb = arm_pc_is_thumb (gdbarch, pc);
1609 CORE_ADDR addr;
1610
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;
1616
1617 stack_chk_guard = lookup_minimal_symbol_by_pc (addr);
6041179a
JB
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
c9d95fa3 1621 || !startswith (stack_chk_guard.minsym->linkage_name (), "__stack_chk_guard"))
621c6d5b
YQ
1622 return pc;
1623
1624 if (is_thumb)
1625 {
1626 unsigned int destreg;
1627 unsigned short insn
198cd59d 1628 = read_code_unsigned_integer (pc + offset, 2, byte_order_for_code);
621c6d5b
YQ
1629
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);
1636
198cd59d
YQ
1637 insn = read_code_unsigned_integer (pc + offset + 2, 2,
1638 byte_order_for_code);
621c6d5b
YQ
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;
1644 }
1645 else
1646 {
1647 unsigned int destreg;
1648 unsigned int insn
198cd59d 1649 = read_code_unsigned_integer (pc + offset, 4, byte_order_for_code);
621c6d5b
YQ
1650
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. */
198cd59d 1658 insn = read_code_unsigned_integer (pc + offset + 4,
621c6d5b
YQ
1659 4, byte_order_for_code);
1660 if ((insn & 0x0e500000) != 0x04000000)
1661 return pc;
1662 if (bits (insn, 12, 15) != destreg)
1663 return pc;
1664 }
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;
1671}
1672
da3c6d4a
MS
1673/* Advance the PC across any function entry prologue instructions to
1674 reach some "real" code.
34e8f22d
RE
1675
1676 The APCS (ARM Procedure Call Standard) defines the following
ed9a39eb 1677 prologue:
c906108c 1678
c5aa993b
JM
1679 mov ip, sp
1680 [stmfd sp!, {a1,a2,a3,a4}]
1681 stmfd sp!, {...,fp,ip,lr,pc}
ed9a39eb
JM
1682 [stfe f7, [sp, #-12]!]
1683 [stfe f6, [sp, #-12]!]
1684 [stfe f5, [sp, #-12]!]
1685 [stfe f4, [sp, #-12]!]
0963b4bd 1686 sub fp, ip, #nn @@ nn == 20 or 4 depending on second insn. */
c906108c 1687
34e8f22d 1688static CORE_ADDR
6093d2eb 1689arm_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR pc)
c906108c 1690{
a89fea3c 1691 CORE_ADDR func_addr, limit_pc;
c906108c 1692
a89fea3c
JL
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))
c906108c 1697 {
d80b854b
UW
1698 CORE_ADDR post_prologue_pc
1699 = skip_prologue_using_sal (gdbarch, func_addr);
43f3e411 1700 struct compunit_symtab *cust = find_pc_compunit_symtab (func_addr);
0d39a070 1701
621c6d5b
YQ
1702 if (post_prologue_pc)
1703 post_prologue_pc
1704 = arm_skip_stack_protector (post_prologue_pc, gdbarch);
1705
1706
0d39a070
DJ
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
43f3e411 1714 && (cust == NULL
ab5f850e
SM
1715 || cust->producer () == NULL
1716 || startswith (cust->producer (), "GNU ")
1717 || producer_is_llvm (cust->producer ())))
0d39a070
DJ
1718 return post_prologue_pc;
1719
a89fea3c 1720 if (post_prologue_pc != 0)
0d39a070
DJ
1721 {
1722 CORE_ADDR analyzed_limit;
1723
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).
1728
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. */
9779414d 1733 if (arm_pc_is_thumb (gdbarch, func_addr))
0d39a070
DJ
1734 analyzed_limit = thumb_analyze_prologue (gdbarch, func_addr,
1735 post_prologue_pc, NULL);
1736 else
9ecab40c
SM
1737 analyzed_limit
1738 = arm_analyze_prologue (gdbarch, func_addr, post_prologue_pc,
1739 NULL, target_arm_instruction_reader ());
0d39a070
DJ
1740
1741 if (analyzed_limit != post_prologue_pc)
1742 return func_addr;
1743
1744 return post_prologue_pc;
1745 }
c906108c
SS
1746 }
1747
a89fea3c
JL
1748 /* Can't determine prologue from the symbol table, need to examine
1749 instructions. */
c906108c 1750
a89fea3c
JL
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. */
0963b4bd 1754 /* Like arm_scan_prologue, stop no later than pc + 64. */
d80b854b 1755 limit_pc = skip_prologue_using_sal (gdbarch, pc);
a89fea3c
JL
1756 if (limit_pc == 0)
1757 limit_pc = pc + 64; /* Magic. */
1758
c906108c 1759
29d73ae4 1760 /* Check if this is Thumb code. */
9779414d 1761 if (arm_pc_is_thumb (gdbarch, pc))
a89fea3c 1762 return thumb_analyze_prologue (gdbarch, pc, limit_pc, NULL);
21daaaaf 1763 else
9ecab40c
SM
1764 return arm_analyze_prologue (gdbarch, pc, limit_pc, NULL,
1765 target_arm_instruction_reader ());
c906108c 1766}
94c30b78 1767
c5aa993b 1768/* *INDENT-OFF* */
c906108c
SS
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
c906108c 1775
da59e081
JM
1776 A typical Thumb function prologue would create this stack frame
1777 (offsets relative to FP)
c906108c
SS
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
0963b4bd 1784 12 bytes. The frame register is R7.
da59e081 1785
da3c6d4a
MS
1786 The comments for thumb_skip_prolog() describe the algorithm we use
1787 to detect the end of the prolog. */
c5aa993b
JM
1788/* *INDENT-ON* */
1789
c906108c 1790static void
be8626e0 1791thumb_scan_prologue (struct gdbarch *gdbarch, CORE_ADDR prev_pc,
b39cc962 1792 CORE_ADDR block_addr, struct arm_prologue_cache *cache)
c906108c
SS
1793{
1794 CORE_ADDR prologue_start;
1795 CORE_ADDR prologue_end;
c906108c 1796
b39cc962
DJ
1797 if (find_pc_partial_function (block_addr, NULL, &prologue_start,
1798 &prologue_end))
c906108c 1799 {
ec3d575a
UW
1800 /* See comment in arm_scan_prologue for an explanation of
1801 this heuristics. */
1802 if (prologue_end > prologue_start + 64)
1803 {
1804 prologue_end = prologue_start + 64;
1805 }
c906108c
SS
1806 }
1807 else
f7060f85
DJ
1808 /* We're in the boondocks: we have no idea where the start of the
1809 function is. */
1810 return;
c906108c 1811
325fac50 1812 prologue_end = std::min (prologue_end, prev_pc);
c906108c 1813
be8626e0 1814 thumb_analyze_prologue (gdbarch, prologue_start, prologue_end, cache);
c906108c
SS
1815}
1816
f303bc3e
YQ
1817/* Return 1 if the ARM instruction INSN restores SP in epilogue, 0
1818 otherwise. */
1819
1820static int
1821arm_instruction_restores_sp (unsigned int insn)
1822{
1823 if (bits (insn, 28, 31) != INST_NV)
1824 {
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;
1836 }
1837
1838 return 0;
1839}
1840
9ecab40c
SM
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). */
1844
1845static uint32_t
1846arm_expand_immediate (uint32_t imm)
1847{
1848 /* Immediate values are 12 bits long. */
1849 gdb_assert ((imm & 0xfffff000) == 0);
1850
1851 uint32_t unrotated_value = imm & 0xff;
1852 uint32_t rotate_amount = (imm & 0xf00) >> 7;
1853
1854 if (rotate_amount == 0)
1855 return unrotated_value;
1856
1857 return ((unrotated_value >> rotate_amount)
1858 | (unrotated_value << (32 - rotate_amount)));
1859}
1860
0d39a070
DJ
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.
eb5492fa 1865
0d39a070
DJ
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). */
1870
1871static CORE_ADDR
1872arm_analyze_prologue (struct gdbarch *gdbarch,
1873 CORE_ADDR prologue_start, CORE_ADDR prologue_end,
9ecab40c
SM
1874 struct arm_prologue_cache *cache,
1875 const arm_instruction_reader &insn_reader)
0d39a070 1876{
0d39a070
DJ
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];
0d39a070 1881 CORE_ADDR unrecognized_pc = 0;
08106042 1882 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
0d39a070
DJ
1883
1884 /* Search the prologue looking for instructions that set up the
96baa820 1885 frame pointer, adjust the stack pointer, and save registers.
ed9a39eb 1886
96baa820
JM
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
b8d5e71d 1890 a frame, which will confuse stack traceback, as well as "finish"
96baa820 1891 and other operations that rely on a knowledge of the stack
0d39a070 1892 traceback. */
d4473757 1893
4be43953
DJ
1894 for (regno = 0; regno < ARM_FPS_REGNUM; regno++)
1895 regs[regno] = pv_register (regno, 0);
f7b7ed97 1896 pv_area stack (ARM_SP_REGNUM, gdbarch_addr_bit (gdbarch));
4be43953 1897
94c30b78
MS
1898 for (current_pc = prologue_start;
1899 current_pc < prologue_end;
f43845b3 1900 current_pc += 4)
96baa820 1901 {
9ecab40c 1902 uint32_t insn = insn_reader.read (current_pc, byte_order_for_code);
9d4fde75 1903
94c30b78 1904 if (insn == 0xe1a0c00d) /* mov ip, sp */
f43845b3 1905 {
4be43953 1906 regs[ARM_IP_REGNUM] = regs[ARM_SP_REGNUM];
28cd8767
JG
1907 continue;
1908 }
0d39a070
DJ
1909 else if ((insn & 0xfff00000) == 0xe2800000 /* add Rd, Rn, #n */
1910 && pv_is_register (regs[bits (insn, 16, 19)], ARM_SP_REGNUM))
28cd8767 1911 {
9ecab40c 1912 uint32_t imm = arm_expand_immediate (insn & 0xfff);
0d39a070 1913 int rd = bits (insn, 12, 15);
0d39a070 1914 regs[rd] = pv_add_constant (regs[bits (insn, 16, 19)], imm);
28cd8767
JG
1915 continue;
1916 }
0d39a070
DJ
1917 else if ((insn & 0xfff00000) == 0xe2400000 /* sub Rd, Rn, #n */
1918 && pv_is_register (regs[bits (insn, 16, 19)], ARM_SP_REGNUM))
28cd8767 1919 {
9ecab40c 1920 uint32_t imm = arm_expand_immediate (insn & 0xfff);
0d39a070 1921 int rd = bits (insn, 12, 15);
0d39a070 1922 regs[rd] = pv_add_constant (regs[bits (insn, 16, 19)], -imm);
f43845b3
MS
1923 continue;
1924 }
0963b4bd
MS
1925 else if ((insn & 0xffff0fff) == 0xe52d0004) /* str Rd,
1926 [sp, #-4]! */
f43845b3 1927 {
f7b7ed97 1928 if (stack.store_would_trash (regs[ARM_SP_REGNUM]))
4be43953
DJ
1929 break;
1930 regs[ARM_SP_REGNUM] = pv_add_constant (regs[ARM_SP_REGNUM], -4);
f7b7ed97
TT
1931 stack.store (regs[ARM_SP_REGNUM], 4,
1932 regs[bits (insn, 12, 15)]);
f43845b3
MS
1933 continue;
1934 }
1935 else if ((insn & 0xffff0000) == 0xe92d0000)
d4473757
KB
1936 /* stmfd sp!, {..., fp, ip, lr, pc}
1937 or
1938 stmfd sp!, {a1, a2, a3, a4} */
c906108c 1939 {
d4473757 1940 int mask = insn & 0xffff;
ed9a39eb 1941
f7b7ed97 1942 if (stack.store_would_trash (regs[ARM_SP_REGNUM]))
4be43953
DJ
1943 break;
1944
94c30b78 1945 /* Calculate offsets of saved registers. */
34e8f22d 1946 for (regno = ARM_PC_REGNUM; regno >= 0; regno--)
d4473757
KB
1947 if (mask & (1 << regno))
1948 {
0963b4bd
MS
1949 regs[ARM_SP_REGNUM]
1950 = pv_add_constant (regs[ARM_SP_REGNUM], -4);
f7b7ed97 1951 stack.store (regs[ARM_SP_REGNUM], 4, regs[regno]);
d4473757
KB
1952 }
1953 }
0d39a070
DJ
1954 else if ((insn & 0xffff0000) == 0xe54b0000 /* strb rx,[r11,#-n] */
1955 || (insn & 0xffff00f0) == 0xe14b00b0 /* strh rx,[r11,#-n] */
f8bf5763 1956 || (insn & 0xffffc000) == 0xe50b0000) /* str rx,[r11,#-n] */
b8d5e71d
MS
1957 {
1958 /* No need to add this to saved_regs -- it's just an arg reg. */
1959 continue;
1960 }
0d39a070
DJ
1961 else if ((insn & 0xffff0000) == 0xe5cd0000 /* strb rx,[sp,#n] */
1962 || (insn & 0xffff00f0) == 0xe1cd00b0 /* strh rx,[sp,#n] */
f8bf5763 1963 || (insn & 0xffffc000) == 0xe58d0000) /* str rx,[sp,#n] */
f43845b3
MS
1964 {
1965 /* No need to add this to saved_regs -- it's just an arg reg. */
1966 continue;
1967 }
0963b4bd
MS
1968 else if ((insn & 0xfff00000) == 0xe8800000 /* stm Rn,
1969 { registers } */
0d39a070
DJ
1970 && pv_is_register (regs[bits (insn, 16, 19)], ARM_SP_REGNUM))
1971 {
1972 /* No need to add this to saved_regs -- it's just arg regs. */
1973 continue;
1974 }
d4473757
KB
1975 else if ((insn & 0xfffff000) == 0xe24cb000) /* sub fp, ip #n */
1976 {
9ecab40c 1977 uint32_t imm = arm_expand_immediate (insn & 0xfff);
4be43953 1978 regs[ARM_FP_REGNUM] = pv_add_constant (regs[ARM_IP_REGNUM], -imm);
d4473757
KB
1979 }
1980 else if ((insn & 0xfffff000) == 0xe24dd000) /* sub sp, sp #n */
1981 {
9ecab40c 1982 uint32_t imm = arm_expand_immediate(insn & 0xfff);
4be43953 1983 regs[ARM_SP_REGNUM] = pv_add_constant (regs[ARM_SP_REGNUM], -imm);
d4473757 1984 }
0963b4bd
MS
1985 else if ((insn & 0xffff7fff) == 0xed6d0103 /* stfe f?,
1986 [sp, -#c]! */
345bd07c 1987 && tdep->have_fpa_registers)
d4473757 1988 {
f7b7ed97 1989 if (stack.store_would_trash (regs[ARM_SP_REGNUM]))
4be43953
DJ
1990 break;
1991
1992 regs[ARM_SP_REGNUM] = pv_add_constant (regs[ARM_SP_REGNUM], -12);
34e8f22d 1993 regno = ARM_F0_REGNUM + ((insn >> 12) & 0x07);
f7b7ed97 1994 stack.store (regs[ARM_SP_REGNUM], 12, regs[regno]);
d4473757 1995 }
0963b4bd
MS
1996 else if ((insn & 0xffbf0fff) == 0xec2d0200 /* sfmfd f0, 4,
1997 [sp!] */
345bd07c 1998 && tdep->have_fpa_registers)
d4473757
KB
1999 {
2000 int n_saved_fp_regs;
2001 unsigned int fp_start_reg, fp_bound_reg;
2002
f7b7ed97 2003 if (stack.store_would_trash (regs[ARM_SP_REGNUM]))
4be43953
DJ
2004 break;
2005
94c30b78 2006 if ((insn & 0x800) == 0x800) /* N0 is set */
96baa820 2007 {
d4473757
KB
2008 if ((insn & 0x40000) == 0x40000) /* N1 is set */
2009 n_saved_fp_regs = 3;
2010 else
2011 n_saved_fp_regs = 1;
96baa820 2012 }
d4473757 2013 else
96baa820 2014 {
d4473757
KB
2015 if ((insn & 0x40000) == 0x40000) /* N1 is set */
2016 n_saved_fp_regs = 2;
2017 else
2018 n_saved_fp_regs = 4;
96baa820 2019 }
d4473757 2020
34e8f22d 2021 fp_start_reg = ARM_F0_REGNUM + ((insn >> 12) & 0x7);
d4473757
KB
2022 fp_bound_reg = fp_start_reg + n_saved_fp_regs;
2023 for (; fp_start_reg < fp_bound_reg; fp_start_reg++)
96baa820 2024 {
4be43953 2025 regs[ARM_SP_REGNUM] = pv_add_constant (regs[ARM_SP_REGNUM], -12);
f7b7ed97
TT
2026 stack.store (regs[ARM_SP_REGNUM], 12,
2027 regs[fp_start_reg++]);
96baa820 2028 }
c906108c 2029 }
0d39a070
DJ
2030 else if ((insn & 0xff000000) == 0xeb000000 && cache == NULL) /* bl */
2031 {
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);
2036
e0634ccf 2037 if (skip_prologue_function (gdbarch, dest, 0))
0d39a070
DJ
2038 continue;
2039 else
2040 break;
2041 }
d4473757 2042 else if ((insn & 0xf0000000) != 0xe0000000)
0963b4bd 2043 break; /* Condition not true, exit early. */
0d39a070
DJ
2044 else if (arm_instruction_changes_pc (insn))
2045 /* Don't scan past anything that might change control flow. */
2046 break;
f303bc3e
YQ
2047 else if (arm_instruction_restores_sp (insn))
2048 {
2049 /* Don't scan past the epilogue. */
2050 break;
2051 }
d19f7eee
UW
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;
0d39a070
DJ
2061 else if ((insn & 0xffff0ff0) == 0xe1a00000)
2062 /* MOV Rd, Rm. Skip register copies, i.e. saves to another
2063 register instead of the stack. */
d4473757 2064 continue;
0d39a070
DJ
2065 else
2066 {
21daaaaf
YQ
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. */
0d39a070 2073 unrecognized_pc = current_pc;
21daaaaf
YQ
2074 if (cache != NULL)
2075 continue;
2076 else
2077 break;
0d39a070 2078 }
c906108c
SS
2079 }
2080
0d39a070
DJ
2081 if (unrecognized_pc == 0)
2082 unrecognized_pc = current_pc;
2083
0d39a070
DJ
2084 if (cache)
2085 {
4072f920
YQ
2086 int framereg, framesize;
2087
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))
2091 {
2092 /* Frame pointer is fp. */
2093 framereg = ARM_FP_REGNUM;
2094 framesize = -regs[ARM_FP_REGNUM].k;
2095 }
2096 else
2097 {
2098 /* Try the stack pointer... this is a bit desperate. */
2099 framereg = ARM_SP_REGNUM;
2100 framesize = -regs[ARM_SP_REGNUM].k;
2101 }
2102
0d39a070
DJ
2103 cache->framereg = framereg;
2104 cache->framesize = framesize;
2105
2106 for (regno = 0; regno < ARM_FPS_REGNUM; regno++)
f7b7ed97 2107 if (stack.find_reg (gdbarch, regno, &offset))
10245fe8
YR
2108 {
2109 cache->saved_regs[regno].set_addr (offset);
2110 if (regno == ARM_SP_REGNUM)
2111 arm_cache_set_active_sp_value(cache, tdep, offset);
2112 }
0d39a070
DJ
2113 }
2114
7cb6d92a
SM
2115 arm_debug_printf ("Prologue scan stopped at %s",
2116 paddress (gdbarch, unrecognized_pc));
4be43953 2117
0d39a070
DJ
2118 return unrecognized_pc;
2119}
2120
2121static void
2122arm_scan_prologue (struct frame_info *this_frame,
2123 struct arm_prologue_cache *cache)
2124{
2125 struct gdbarch *gdbarch = get_frame_arch (this_frame);
2126 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
bec2ab5a 2127 CORE_ADDR prologue_start, prologue_end;
0d39a070
DJ
2128 CORE_ADDR prev_pc = get_frame_pc (this_frame);
2129 CORE_ADDR block_addr = get_frame_address_in_block (this_frame);
08106042 2130 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
0d39a070
DJ
2131
2132 /* Assume there is no frame until proven otherwise. */
2133 cache->framereg = ARM_SP_REGNUM;
2134 cache->framesize = 0;
2135
2136 /* Check for Thumb prologue. */
2137 if (arm_frame_is_thumb (this_frame))
2138 {
2139 thumb_scan_prologue (gdbarch, prev_pc, block_addr, cache);
2140 return;
2141 }
2142
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))
2147 {
2148 /* One way to find the end of the prologue (which works well
dda83cd7 2149 for unoptimized code) is to do the following:
0d39a070
DJ
2150
2151 struct symtab_and_line sal = find_pc_line (prologue_start, 0);
2152
2153 if (sal.line == 0)
2154 prologue_end = prev_pc;
2155 else if (sal.end < prologue_end)
2156 prologue_end = sal.end;
2157
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.
2164
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.
2171
2172 The expression
2173
2174 prologue_start + 64
2175
2176 is a suitable endpoint since it accounts for the largest
2177 possible prologue plus up to five instructions inserted by
2178 the scheduler. */
2179
2180 if (prologue_end > prologue_start + 64)
2181 {
2182 prologue_end = prologue_start + 64; /* See above. */
2183 }
2184 }
2185 else
2186 {
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;
7913a64c 2193 ULONGEST return_value;
0d39a070 2194
9e237747 2195 /* AAPCS does not use a frame register, so we can abort here. */
345bd07c 2196 if (tdep->arm_abi == ARM_ABI_AAPCS)
dda83cd7 2197 return;
9e237747 2198
0d39a070 2199 frame_loc = get_frame_register_unsigned (this_frame, ARM_FP_REGNUM);
7913a64c
YQ
2200 if (!safe_read_memory_unsigned_integer (frame_loc, 4, byte_order,
2201 &return_value))
dda83cd7 2202 return;
0d39a070 2203 else
dda83cd7
SM
2204 {
2205 prologue_start = gdbarch_addr_bits_remove
0d39a070 2206 (gdbarch, return_value) - 8;
dda83cd7
SM
2207 prologue_end = prologue_start + 64; /* See above. */
2208 }
0d39a070
DJ
2209 }
2210
2211 if (prev_pc < prologue_end)
2212 prologue_end = prev_pc;
2213
9ecab40c
SM
2214 arm_analyze_prologue (gdbarch, prologue_start, prologue_end, cache,
2215 target_arm_instruction_reader ());
c906108c
SS
2216}
2217
eb5492fa 2218static struct arm_prologue_cache *
a262aec2 2219arm_make_prologue_cache (struct frame_info *this_frame)
c906108c 2220{
eb5492fa
DJ
2221 int reg;
2222 struct arm_prologue_cache *cache;
1ef3351b 2223 CORE_ADDR unwound_fp, prev_sp;
c5aa993b 2224
35d5d4ee 2225 cache = FRAME_OBSTACK_ZALLOC (struct arm_prologue_cache);
0824193f 2226 arm_cache_init (cache, this_frame);
c906108c 2227
a262aec2 2228 arm_scan_prologue (this_frame, cache);
848cfffb 2229
a262aec2 2230 unwound_fp = get_frame_register_unsigned (this_frame, cache->framereg);
eb5492fa
DJ
2231 if (unwound_fp == 0)
2232 return cache;
c906108c 2233
ae7e2f45 2234 arm_gdbarch_tdep *tdep =
08106042 2235 gdbarch_tdep<arm_gdbarch_tdep> (get_frame_arch (this_frame));
ae7e2f45 2236
1ef3351b
YR
2237 prev_sp = unwound_fp + cache->framesize;
2238 arm_cache_set_active_sp_value (cache, tdep, prev_sp);
c906108c 2239
eb5492fa
DJ
2240 /* Calculate actual addresses of saved registers using offsets
2241 determined by arm_scan_prologue. */
a262aec2 2242 for (reg = 0; reg < gdbarch_num_regs (get_frame_arch (this_frame)); reg++)
a9a87d35 2243 if (cache->saved_regs[reg].is_addr ())
1ef3351b
YR
2244 cache->saved_regs[reg].set_addr (cache->saved_regs[reg].addr () +
2245 prev_sp);
eb5492fa
DJ
2246
2247 return cache;
c906108c
SS
2248}
2249
c1ee9414
LM
2250/* Implementation of the stop_reason hook for arm_prologue frames. */
2251
2252static enum unwind_stop_reason
2253arm_prologue_unwind_stop_reason (struct frame_info *this_frame,
2254 void **this_cache)
2255{
2256 struct arm_prologue_cache *cache;
2257 CORE_ADDR pc;
2258
2259 if (*this_cache == NULL)
2260 *this_cache = arm_make_prologue_cache (this_frame);
9a3c8263 2261 cache = (struct arm_prologue_cache *) *this_cache;
c1ee9414
LM
2262
2263 /* This is meant to halt the backtrace at "_start". */
2264 pc = get_frame_pc (this_frame);
345bd07c 2265 gdbarch *arch = get_frame_arch (this_frame);
08106042 2266 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (arch);
345bd07c 2267 if (pc <= tdep->lowest_pc)
c1ee9414
LM
2268 return UNWIND_OUTERMOST;
2269
2270 /* If we've hit a wall, stop. */
ae7e2f45 2271 if (arm_cache_get_prev_sp_value (cache, tdep) == 0)
c1ee9414
LM
2272 return UNWIND_OUTERMOST;
2273
2274 return UNWIND_NO_REASON;
2275}
2276
eb5492fa
DJ
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. */
c906108c 2279
148754e5 2280static void
a262aec2 2281arm_prologue_this_id (struct frame_info *this_frame,
eb5492fa
DJ
2282 void **this_cache,
2283 struct frame_id *this_id)
c906108c 2284{
eb5492fa
DJ
2285 struct arm_prologue_cache *cache;
2286 struct frame_id id;
2c404490 2287 CORE_ADDR pc, func;
f079148d 2288
eb5492fa 2289 if (*this_cache == NULL)
a262aec2 2290 *this_cache = arm_make_prologue_cache (this_frame);
9a3c8263 2291 cache = (struct arm_prologue_cache *) *this_cache;
2a451106 2292
ae7e2f45 2293 arm_gdbarch_tdep *tdep
08106042 2294 = gdbarch_tdep<arm_gdbarch_tdep> (get_frame_arch (this_frame));
ae7e2f45 2295
0e9e9abd
UW
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. */
c1ee9414 2299 pc = get_frame_pc (this_frame);
2c404490 2300 func = get_frame_func (this_frame);
0e9e9abd
UW
2301 if (!func)
2302 func = pc;
2303
ae7e2f45 2304 id = frame_id_build (arm_cache_get_prev_sp_value (cache, tdep), func);
eb5492fa 2305 *this_id = id;
c906108c
SS
2306}
2307
a262aec2
DJ
2308static struct value *
2309arm_prologue_prev_register (struct frame_info *this_frame,
eb5492fa 2310 void **this_cache,
a262aec2 2311 int prev_regnum)
24de872b 2312{
24568a2c 2313 struct gdbarch *gdbarch = get_frame_arch (this_frame);
24de872b 2314 struct arm_prologue_cache *cache;
ef273377 2315 CORE_ADDR sp_value;
24de872b 2316
eb5492fa 2317 if (*this_cache == NULL)
a262aec2 2318 *this_cache = arm_make_prologue_cache (this_frame);
9a3c8263 2319 cache = (struct arm_prologue_cache *) *this_cache;
24de872b 2320
08106042 2321 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
a01567f4
LM
2322
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);
2327
eb5492fa 2328 /* If we are asked to unwind the PC, then we need to return the LR
b39cc962
DJ
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. */
eb5492fa 2333 if (prev_regnum == ARM_PC_REGNUM)
b39cc962
DJ
2334 {
2335 CORE_ADDR lr;
2336
2337 lr = frame_unwind_register_unsigned (this_frame, ARM_LR_REGNUM);
2338 return frame_unwind_got_constant (this_frame, prev_regnum,
24568a2c 2339 arm_addr_bits_remove (gdbarch, lr));
b39cc962 2340 }
24de872b 2341
eb5492fa 2342 /* SP is generally not saved to the stack, but this frame is
a262aec2 2343 identified by the next frame's stack pointer at the time of the call.
eb5492fa
DJ
2344 The value was already reconstructed into PREV_SP. */
2345 if (prev_regnum == ARM_SP_REGNUM)
ae7e2f45
CL
2346 return frame_unwind_got_constant (this_frame, prev_regnum,
2347 arm_cache_get_prev_sp_value (cache, tdep));
eb5492fa 2348
ef273377
CL
2349 /* The value might be one of the alternative SP, if so, use the
2350 value already constructed. */
d65edaa0 2351 if (arm_is_alternative_sp_register (tdep, prev_regnum))
ef273377
CL
2352 {
2353 sp_value = arm_cache_get_sp_register (cache, tdep, prev_regnum);
2354 return frame_unwind_got_constant (this_frame, prev_regnum, sp_value);
2355 }
2356
b39cc962
DJ
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)
2366 {
8c9ae6df
YR
2367 ULONGEST cpsr = get_frame_register_unsigned (this_frame, prev_regnum);
2368 CORE_ADDR lr = frame_unwind_register_unsigned (this_frame, ARM_LR_REGNUM);
b39cc962 2369
8c9ae6df 2370 cpsr = reconstruct_t_bit (gdbarch, lr, cpsr);
b39cc962
DJ
2371 return frame_unwind_got_constant (this_frame, prev_regnum, cpsr);
2372 }
2373
a262aec2
DJ
2374 return trad_frame_get_prev_register (this_frame, cache->saved_regs,
2375 prev_regnum);
eb5492fa
DJ
2376}
2377
6bd434d6 2378static frame_unwind arm_prologue_unwind = {
a154d838 2379 "arm prologue",
eb5492fa 2380 NORMAL_FRAME,
c1ee9414 2381 arm_prologue_unwind_stop_reason,
eb5492fa 2382 arm_prologue_this_id,
a262aec2
DJ
2383 arm_prologue_prev_register,
2384 NULL,
2385 default_frame_sniffer
eb5492fa
DJ
2386};
2387
0e9e9abd
UW
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). */
2392
0e9e9abd
UW
2393struct arm_exidx_entry
2394{
227031b2 2395 CORE_ADDR addr;
0e9e9abd 2396 gdb_byte *entry;
7a5d944b
TT
2397
2398 bool operator< (const arm_exidx_entry &other) const
2399 {
2400 return addr < other.addr;
2401 }
0e9e9abd 2402};
0e9e9abd
UW
2403
2404struct arm_exidx_data
2405{
7a5d944b 2406 std::vector<std::vector<arm_exidx_entry>> section_maps;
0e9e9abd
UW
2407};
2408
a2726d4f 2409/* Per-BFD key to store exception handling information. */
08b8a139 2410static const registry<bfd>::key<arm_exidx_data> arm_exidx_data_key;
0e9e9abd
UW
2411
2412static struct obj_section *
2413arm_obj_section_from_vma (struct objfile *objfile, bfd_vma vma)
2414{
2415 struct obj_section *osect;
2416
2417 ALL_OBJFILE_OSECTIONS (objfile, osect)
fd361982 2418 if (bfd_section_flags (osect->the_bfd_section) & SEC_ALLOC)
0e9e9abd
UW
2419 {
2420 bfd_vma start, size;
fd361982
AM
2421 start = bfd_section_vma (osect->the_bfd_section);
2422 size = bfd_section_size (osect->the_bfd_section);
0e9e9abd
UW
2423
2424 if (start <= vma && vma < start + size)
2425 return osect;
2426 }
2427
2428 return NULL;
2429}
2430
2431/* Parse contents of exception table and exception index sections
2432 of OBJFILE, and fill in the exception table entry cache.
2433
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
2441
2442 The extracted and normalized instructions are stored for later
2443 retrieval by the arm_find_exidx_entry routine. */
2444
2445static void
2446arm_exidx_new_objfile (struct objfile *objfile)
2447{
0e9e9abd
UW
2448 struct arm_exidx_data *data;
2449 asection *exidx, *extab;
2450 bfd_vma exidx_vma = 0, extab_vma = 0;
0e9e9abd
UW
2451 LONGEST i;
2452
2453 /* If we've already touched this file, do nothing. */
98badbfd 2454 if (!objfile || arm_exidx_data_key.get (objfile->obfd.get ()) != NULL)
0e9e9abd
UW
2455 return;
2456
2457 /* Read contents of exception table and index. */
98badbfd
TT
2458 exidx = bfd_get_section_by_name (objfile->obfd.get (),
2459 ELF_STRING_ARM_unwind);
984c7238 2460 gdb::byte_vector exidx_data;
0e9e9abd
UW
2461 if (exidx)
2462 {
fd361982
AM
2463 exidx_vma = bfd_section_vma (exidx);
2464 exidx_data.resize (bfd_section_size (exidx));
0e9e9abd 2465
98badbfd 2466 if (!bfd_get_section_contents (objfile->obfd.get (), exidx,
984c7238
TT
2467 exidx_data.data (), 0,
2468 exidx_data.size ()))
2469 return;
0e9e9abd
UW
2470 }
2471
98badbfd 2472 extab = bfd_get_section_by_name (objfile->obfd.get (), ".ARM.extab");
984c7238 2473 gdb::byte_vector extab_data;
0e9e9abd
UW
2474 if (extab)
2475 {
fd361982
AM
2476 extab_vma = bfd_section_vma (extab);
2477 extab_data.resize (bfd_section_size (extab));
0e9e9abd 2478
98badbfd 2479 if (!bfd_get_section_contents (objfile->obfd.get (), extab,
984c7238
TT
2480 extab_data.data (), 0,
2481 extab_data.size ()))
2482 return;
0e9e9abd
UW
2483 }
2484
2485 /* Allocate exception table data structure. */
98badbfd 2486 data = arm_exidx_data_key.emplace (objfile->obfd.get ());
7a5d944b 2487 data->section_maps.resize (objfile->obfd->section_count);
0e9e9abd
UW
2488
2489 /* Fill in exception table. */
984c7238 2490 for (i = 0; i < exidx_data.size () / 8; i++)
0e9e9abd
UW
2491 {
2492 struct arm_exidx_entry new_exidx_entry;
984c7238
TT
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);
0e9e9abd
UW
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;
2500
2501 /* Extract address of start of function. */
2502 idx = ((idx & 0x7fffffff) ^ 0x40000000) - 0x40000000;
2503 idx += exidx_vma + i * 8;
2504
2505 /* Find section containing function and compute section offset. */
2506 sec = arm_obj_section_from_vma (objfile, idx);
2507 if (sec == NULL)
2508 continue;
fd361982 2509 idx -= bfd_section_vma (sec->the_bfd_section);
0e9e9abd
UW
2510
2511 /* Determine address of exception table entry. */
2512 if (val == 1)
2513 {
2514 /* EXIDX_CANTUNWIND -- no exception table entry present. */
2515 }
2516 else if ((val & 0xff000000) == 0x80000000)
2517 {
2518 /* Exception table entry embedded in .ARM.exidx
2519 -- must be short form. */
2520 word = val;
2521 n_bytes = 3;
2522 }
2523 else if (!(val & 0x80000000))
2524 {
2525 /* Exception table entry in .ARM.extab. */
2526 addr = ((val & 0x7fffffff) ^ 0x40000000) - 0x40000000;
2527 addr += exidx_vma + i * 8 + 4;
2528
984c7238 2529 if (addr >= extab_vma && addr + 4 <= extab_vma + extab_data.size ())
0e9e9abd
UW
2530 {
2531 word = bfd_h_get_32 (objfile->obfd,
984c7238 2532 extab_data.data () + addr - extab_vma);
0e9e9abd
UW
2533 addr += 4;
2534
2535 if ((word & 0xff000000) == 0x80000000)
2536 {
2537 /* Short form. */
2538 n_bytes = 3;
2539 }
2540 else if ((word & 0xff000000) == 0x81000000
2541 || (word & 0xff000000) == 0x82000000)
2542 {
2543 /* Long form. */
2544 n_bytes = 2;
2545 n_words = ((word >> 16) & 0xff);
2546 }
2547 else if (!(word & 0x80000000))
2548 {
2549 bfd_vma pers;
2550 struct obj_section *pers_sec;
2551 int gnu_personality = 0;
2552
2553 /* Custom personality routine. */
2554 pers = ((word & 0x7fffffff) ^ 0x40000000) - 0x40000000;
2555 pers = UNMAKE_THUMB_ADDR (pers + addr - 4);
2556
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)
2561 {
2562 static const char *personality[] =
2563 {
2564 "__gcc_personality_v0",
2565 "__gxx_personality_v0",
2566 "__gcj_personality_v0",
2567 "__gnu_objc_personality_v0",
2568 NULL
2569 };
2570
0c1bcd23 2571 CORE_ADDR pc = pers + pers_sec->offset ();
0e9e9abd
UW
2572 int k;
2573
2574 for (k = 0; personality[k]; k++)
2575 if (lookup_minimal_symbol_by_pc_name
2576 (pc, personality[k], objfile))
2577 {
2578 gnu_personality = 1;
2579 break;
2580 }
2581 }
2582
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
984c7238 2587 && addr + 4 <= extab_vma + extab_data.size ())
0e9e9abd
UW
2588 {
2589 word = bfd_h_get_32 (objfile->obfd,
984c7238
TT
2590 (extab_data.data ()
2591 + addr - extab_vma));
0e9e9abd
UW
2592 addr += 4;
2593 n_bytes = 3;
2594 n_words = ((word >> 24) & 0xff);
2595 }
2596 }
2597 }
2598 }
2599
2600 /* Sanity check address. */
2601 if (n_words)
984c7238
TT
2602 if (addr < extab_vma
2603 || addr + 4 * n_words > extab_vma + extab_data.size ())
0e9e9abd
UW
2604 n_words = n_bytes = 0;
2605
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)
2610 {
224c3ddb
SM
2611 gdb_byte *p = entry
2612 = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack,
2613 n_bytes + n_words * 4 + 1);
0e9e9abd
UW
2614
2615 while (n_bytes--)
2616 *p++ = (gdb_byte) ((word >> (8 * n_bytes)) & 0xff);
2617
2618 while (n_words--)
2619 {
2620 word = bfd_h_get_32 (objfile->obfd,
984c7238 2621 extab_data.data () + addr - extab_vma);
0e9e9abd
UW
2622 addr += 4;
2623
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);
2628 }
2629
2630 /* Implied "Finish" to terminate the list. */
2631 *p++ = 0xb0;
2632 }
2633
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;
7a5d944b
TT
2638 data->section_maps[sec->the_bfd_section->index].push_back
2639 (new_exidx_entry);
0e9e9abd 2640 }
0e9e9abd
UW
2641}
2642
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. */
2646
2647static gdb_byte *
2648arm_find_exidx_entry (CORE_ADDR memaddr, CORE_ADDR *start)
2649{
2650 struct obj_section *sec;
2651
2652 sec = find_pc_section (memaddr);
2653 if (sec != NULL)
2654 {
2655 struct arm_exidx_data *data;
0c1bcd23 2656 struct arm_exidx_entry map_key = { memaddr - sec->addr (), 0 };
0e9e9abd 2657
98badbfd 2658 data = arm_exidx_data_key.get (sec->objfile->obfd.get ());
0e9e9abd
UW
2659 if (data != NULL)
2660 {
7a5d944b
TT
2661 std::vector<arm_exidx_entry> &map
2662 = data->section_maps[sec->the_bfd_section->index];
2663 if (!map.empty ())
0e9e9abd 2664 {
7a5d944b 2665 auto idx = std::lower_bound (map.begin (), map.end (), map_key);
0e9e9abd 2666
7a5d944b 2667 /* std::lower_bound finds the earliest ordered insertion
0e9e9abd
UW
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. */
7a5d944b 2671 if (idx < map.end ())
0e9e9abd 2672 {
7a5d944b 2673 if (idx->addr == map_key.addr)
0e9e9abd
UW
2674 {
2675 if (start)
0c1bcd23 2676 *start = idx->addr + sec->addr ();
7a5d944b 2677 return idx->entry;
0e9e9abd
UW
2678 }
2679 }
2680
7a5d944b 2681 if (idx > map.begin ())
0e9e9abd 2682 {
7a5d944b 2683 idx = idx - 1;
0e9e9abd 2684 if (start)
0c1bcd23 2685 *start = idx->addr + sec->addr ();
7a5d944b 2686 return idx->entry;
0e9e9abd
UW
2687 }
2688 }
2689 }
2690 }
2691
2692 return NULL;
2693}
2694
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.
2698
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. */
2703
2704static struct arm_prologue_cache *
2705arm_exidx_fill_cache (struct frame_info *this_frame, gdb_byte *entry)
2706{
2707 CORE_ADDR vsp = 0;
2708 int vsp_valid = 0;
2709
2710 struct arm_prologue_cache *cache;
2711 cache = FRAME_OBSTACK_ZALLOC (struct arm_prologue_cache);
0824193f 2712 arm_cache_init (cache, this_frame);
0e9e9abd
UW
2713
2714 for (;;)
2715 {
2716 gdb_byte insn;
2717
2718 /* Whenever we reload SP, we actually have to retrieve its
2719 actual value in the current frame. */
2720 if (!vsp_valid)
2721 {
a9a87d35 2722 if (cache->saved_regs[ARM_SP_REGNUM].is_realreg ())
0e9e9abd 2723 {
098caef4 2724 int reg = cache->saved_regs[ARM_SP_REGNUM].realreg ();
0e9e9abd
UW
2725 vsp = get_frame_register_unsigned (this_frame, reg);
2726 }
2727 else
2728 {
098caef4 2729 CORE_ADDR addr = cache->saved_regs[ARM_SP_REGNUM].addr ();
0e9e9abd
UW
2730 vsp = get_frame_memory_unsigned (this_frame, addr, 4);
2731 }
2732
2733 vsp_valid = 1;
2734 }
2735
2736 /* Decode next unwind instruction. */
2737 insn = *entry++;
2738
2739 if ((insn & 0xc0) == 0)
2740 {
2741 int offset = insn & 0x3f;
2742 vsp += (offset << 2) + 4;
2743 }
2744 else if ((insn & 0xc0) == 0x40)
2745 {
2746 int offset = insn & 0x3f;
2747 vsp -= (offset << 2) + 4;
2748 }
2749 else if ((insn & 0xf0) == 0x80)
2750 {
2751 int mask = ((insn & 0xf) << 8) | *entry++;
2752 int i;
2753
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;
2759
2760 /* Pop registers r4..r15 under mask. */
2761 for (i = 0; i < 12; i++)
2762 if (mask & (1 << i))
2763 {
098caef4 2764 cache->saved_regs[4 + i].set_addr (vsp);
0e9e9abd
UW
2765 vsp += 4;
2766 }
2767
2768 /* Special-case popping SP -- we need to reload vsp. */
2769 if (mask & (1 << (ARM_SP_REGNUM - 4)))
2770 vsp_valid = 0;
2771 }
2772 else if ((insn & 0xf0) == 0x90)
2773 {
2774 int reg = insn & 0xf;
2775
2776 /* Reserved cases. */
2777 if (reg == ARM_SP_REGNUM || reg == ARM_PC_REGNUM)
2778 return NULL;
2779
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;
2783 }
2784 else if ((insn & 0xf0) == 0xa0)
2785 {
2786 int count = insn & 0x7;
2787 int pop_lr = (insn & 0x8) != 0;
2788 int i;
2789
2790 /* Pop r4..r[4+count]. */
2791 for (i = 0; i <= count; i++)
2792 {
098caef4 2793 cache->saved_regs[4 + i].set_addr (vsp);
0e9e9abd
UW
2794 vsp += 4;
2795 }
2796
2797 /* If indicated by flag, pop LR as well. */
2798 if (pop_lr)
2799 {
098caef4 2800 cache->saved_regs[ARM_LR_REGNUM].set_addr (vsp);
0e9e9abd
UW
2801 vsp += 4;
2802 }
2803 }
2804 else if (insn == 0xb0)
2805 {
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. */
a9a87d35 2808 if (!cache->saved_regs[ARM_PC_REGNUM].is_addr ())
0e9e9abd
UW
2809 cache->saved_regs[ARM_PC_REGNUM]
2810 = cache->saved_regs[ARM_LR_REGNUM];
2811
2812 /* We're done. */
2813 break;
2814 }
2815 else if (insn == 0xb1)
2816 {
2817 int mask = *entry++;
2818 int i;
2819
2820 /* All-zero mask and mask >= 16 is "spare". */
2821 if (mask == 0 || mask >= 16)
2822 return NULL;
2823
2824 /* Pop r0..r3 under mask. */
2825 for (i = 0; i < 4; i++)
2826 if (mask & (1 << i))
2827 {
098caef4 2828 cache->saved_regs[i].set_addr (vsp);
0e9e9abd
UW
2829 vsp += 4;
2830 }
2831 }
2832 else if (insn == 0xb2)
2833 {
2834 ULONGEST offset = 0;
2835 unsigned shift = 0;
2836
2837 do
2838 {
2839 offset |= (*entry & 0x7f) << shift;
2840 shift += 7;
2841 }
2842 while (*entry++ & 0x80);
2843
2844 vsp += 0x204 + (offset << 2);
2845 }
2846 else if (insn == 0xb3)
2847 {
2848 int start = *entry >> 4;
2849 int count = (*entry++) & 0xf;
2850 int i;
2851
2852 /* Only registers D0..D15 are valid here. */
2853 if (start + count >= 16)
2854 return NULL;
2855
2856 /* Pop VFP double-precision registers D[start]..D[start+count]. */
2857 for (i = 0; i <= count; i++)
2858 {
098caef4 2859 cache->saved_regs[ARM_D0_REGNUM + start + i].set_addr (vsp);
0e9e9abd
UW
2860 vsp += 8;
2861 }
2862
2863 /* Add an extra 4 bytes for FSTMFDX-style stack. */
2864 vsp += 4;
2865 }
2866 else if ((insn & 0xf8) == 0xb8)
2867 {
2868 int count = insn & 0x7;
2869 int i;
2870
2871 /* Pop VFP double-precision registers D[8]..D[8+count]. */
2872 for (i = 0; i <= count; i++)
2873 {
098caef4 2874 cache->saved_regs[ARM_D0_REGNUM + 8 + i].set_addr (vsp);
0e9e9abd
UW
2875 vsp += 8;
2876 }
2877
2878 /* Add an extra 4 bytes for FSTMFDX-style stack. */
2879 vsp += 4;
2880 }
2881 else if (insn == 0xc6)
2882 {
2883 int start = *entry >> 4;
2884 int count = (*entry++) & 0xf;
2885 int i;
2886
2887 /* Only registers WR0..WR15 are valid. */
2888 if (start + count >= 16)
2889 return NULL;
2890
2891 /* Pop iwmmx registers WR[start]..WR[start+count]. */
2892 for (i = 0; i <= count; i++)
2893 {
098caef4 2894 cache->saved_regs[ARM_WR0_REGNUM + start + i].set_addr (vsp);
0e9e9abd
UW
2895 vsp += 8;
2896 }
2897 }
2898 else if (insn == 0xc7)
2899 {
2900 int mask = *entry++;
2901 int i;
2902
2903 /* All-zero mask and mask >= 16 is "spare". */
2904 if (mask == 0 || mask >= 16)
2905 return NULL;
2906
2907 /* Pop iwmmx general-purpose registers WCGR0..WCGR3 under mask. */
2908 for (i = 0; i < 4; i++)
2909 if (mask & (1 << i))
2910 {
098caef4 2911 cache->saved_regs[ARM_WCGR0_REGNUM + i].set_addr (vsp);
0e9e9abd
UW
2912 vsp += 4;
2913 }
2914 }
2915 else if ((insn & 0xf8) == 0xc0)
2916 {
2917 int count = insn & 0x7;
2918 int i;
2919
2920 /* Pop iwmmx registers WR[10]..WR[10+count]. */
2921 for (i = 0; i <= count; i++)
2922 {
098caef4 2923 cache->saved_regs[ARM_WR0_REGNUM + 10 + i].set_addr (vsp);
0e9e9abd
UW
2924 vsp += 8;
2925 }
2926 }
2927 else if (insn == 0xc8)
2928 {
2929 int start = *entry >> 4;
2930 int count = (*entry++) & 0xf;
2931 int i;
2932
2933 /* Only registers D0..D31 are valid. */
2934 if (start + count >= 16)
2935 return NULL;
2936
2937 /* Pop VFP double-precision registers
2938 D[16+start]..D[16+start+count]. */
2939 for (i = 0; i <= count; i++)
2940 {
098caef4 2941 cache->saved_regs[ARM_D0_REGNUM + 16 + start + i].set_addr (vsp);
0e9e9abd
UW
2942 vsp += 8;
2943 }
2944 }
2945 else if (insn == 0xc9)
2946 {
2947 int start = *entry >> 4;
2948 int count = (*entry++) & 0xf;
2949 int i;
2950
2951 /* Pop VFP double-precision registers D[start]..D[start+count]. */
2952 for (i = 0; i <= count; i++)
2953 {
098caef4 2954 cache->saved_regs[ARM_D0_REGNUM + start + i].set_addr (vsp);
0e9e9abd
UW
2955 vsp += 8;
2956 }
2957 }
2958 else if ((insn & 0xf8) == 0xd0)
2959 {
2960 int count = insn & 0x7;
2961 int i;
2962
2963 /* Pop VFP double-precision registers D[8]..D[8+count]. */
2964 for (i = 0; i <= count; i++)
2965 {
098caef4 2966 cache->saved_regs[ARM_D0_REGNUM + 8 + i].set_addr (vsp);
0e9e9abd
UW
2967 vsp += 8;
2968 }
2969 }
2970 else
2971 {
2972 /* Everything else is "spare". */
2973 return NULL;
2974 }
2975 }
2976
2977 /* If we restore SP from a register, assume this was the frame register.
2978 Otherwise just fall back to SP as frame register. */
a9a87d35 2979 if (cache->saved_regs[ARM_SP_REGNUM].is_realreg ())
098caef4 2980 cache->framereg = cache->saved_regs[ARM_SP_REGNUM].realreg ();
0e9e9abd
UW
2981 else
2982 cache->framereg = ARM_SP_REGNUM;
2983
2984 /* Determine offset to previous frame. */
2985 cache->framesize
2986 = vsp - get_frame_register_unsigned (this_frame, cache->framereg);
2987
2988 /* We already got the previous SP. */
ae7e2f45 2989 arm_gdbarch_tdep *tdep
08106042 2990 = gdbarch_tdep<arm_gdbarch_tdep> (get_frame_arch (this_frame));
ae7e2f45 2991 arm_cache_set_active_sp_value (cache, tdep, vsp);
0e9e9abd
UW
2992
2993 return cache;
2994}
2995
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. */
3000
3001static int
3002arm_exidx_unwind_sniffer (const struct frame_unwind *self,
3003 struct frame_info *this_frame,
3004 void **this_prologue_cache)
3005{
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;
3011
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;
3017
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.)
3024
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))
3030 {
3031 int exc_valid = 0;
3032
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;
3038
3039 /* We also assume exception information is valid if we're currently
3040 blocked in a system call. The system library is supposed to
d9311bfa
AT
3041 ensure this, so that e.g. pthread cancellation works. */
3042 if (arm_frame_is_thumb (this_frame))
0e9e9abd 3043 {
7913a64c 3044 ULONGEST insn;
416dc9c6 3045
7913a64c
YQ
3046 if (safe_read_memory_unsigned_integer (get_frame_pc (this_frame) - 2,
3047 2, byte_order_for_code, &insn)
d9311bfa
AT
3048 && (insn & 0xff00) == 0xdf00 /* svc */)
3049 exc_valid = 1;
0e9e9abd 3050 }
d9311bfa
AT
3051 else
3052 {
7913a64c 3053 ULONGEST insn;
416dc9c6 3054
7913a64c
YQ
3055 if (safe_read_memory_unsigned_integer (get_frame_pc (this_frame) - 4,
3056 4, byte_order_for_code, &insn)
d9311bfa
AT
3057 && (insn & 0x0f000000) == 0x0f000000 /* svc */)
3058 exc_valid = 1;
3059 }
3060
0e9e9abd
UW
3061 /* Bail out if we don't know that exception information is valid. */
3062 if (!exc_valid)
3063 return 0;
3064
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.
3069
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;
3079 }
3080
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;
3086
3087 *this_prologue_cache = cache;
3088 return 1;
3089}
3090
3091struct frame_unwind arm_exidx_unwind = {
a154d838 3092 "arm exidx",
0e9e9abd 3093 NORMAL_FRAME,
8fbca658 3094 default_frame_unwind_stop_reason,
0e9e9abd
UW
3095 arm_prologue_this_id,
3096 arm_prologue_prev_register,
3097 NULL,
3098 arm_exidx_unwind_sniffer
3099};
3100
779aa56f
YQ
3101static struct arm_prologue_cache *
3102arm_make_epilogue_frame_cache (struct frame_info *this_frame)
3103{
3104 struct arm_prologue_cache *cache;
779aa56f
YQ
3105 int reg;
3106
3107 cache = FRAME_OBSTACK_ZALLOC (struct arm_prologue_cache);
0824193f 3108 arm_cache_init (cache, this_frame);
779aa56f
YQ
3109
3110 /* Still rely on the offset calculated from prologue. */
3111 arm_scan_prologue (this_frame, cache);
3112
3113 /* Since we are in epilogue, the SP has been restored. */
ae7e2f45 3114 arm_gdbarch_tdep *tdep
08106042 3115 = gdbarch_tdep<arm_gdbarch_tdep> (get_frame_arch (this_frame));
ae7e2f45
CL
3116 arm_cache_set_active_sp_value (cache, tdep,
3117 get_frame_register_unsigned (this_frame,
3118 ARM_SP_REGNUM));
779aa56f
YQ
3119
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++)
a9a87d35 3123 if (cache->saved_regs[reg].is_addr ())
098caef4 3124 cache->saved_regs[reg].set_addr (cache->saved_regs[reg].addr ()
ae7e2f45 3125 + arm_cache_get_prev_sp_value (cache, tdep));
779aa56f
YQ
3126
3127 return cache;
3128}
3129
3130/* Implementation of function hook 'this_id' in
3131 'struct frame_uwnind' for epilogue unwinder. */
3132
3133static void
3134arm_epilogue_frame_this_id (struct frame_info *this_frame,
3135 void **this_cache,
3136 struct frame_id *this_id)
3137{
3138 struct arm_prologue_cache *cache;
3139 CORE_ADDR pc, func;
3140
3141 if (*this_cache == NULL)
3142 *this_cache = arm_make_epilogue_frame_cache (this_frame);
3143 cache = (struct arm_prologue_cache *) *this_cache;
3144
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);
fb3f3d25 3150 if (func == 0)
779aa56f
YQ
3151 func = pc;
3152
ae7e2f45 3153 arm_gdbarch_tdep *tdep
08106042 3154 = gdbarch_tdep<arm_gdbarch_tdep> (get_frame_arch (this_frame));
ae7e2f45 3155 *this_id = frame_id_build (arm_cache_get_prev_sp_value (cache, tdep), pc);
779aa56f
YQ
3156}
3157
3158/* Implementation of function hook 'prev_register' in
3159 'struct frame_uwnind' for epilogue unwinder. */
3160
3161static struct value *
3162arm_epilogue_frame_prev_register (struct frame_info *this_frame,
3163 void **this_cache, int regnum)
3164{
779aa56f
YQ
3165 if (*this_cache == NULL)
3166 *this_cache = arm_make_epilogue_frame_cache (this_frame);
779aa56f
YQ
3167
3168 return arm_prologue_prev_register (this_frame, this_cache, regnum);
3169}
3170
3171static int arm_stack_frame_destroyed_p_1 (struct gdbarch *gdbarch,
3172 CORE_ADDR pc);
3173static int thumb_stack_frame_destroyed_p (struct gdbarch *gdbarch,
3174 CORE_ADDR pc);
3175
3176/* Implementation of function hook 'sniffer' in
3177 'struct frame_uwnind' for epilogue unwinder. */
3178
3179static int
3180arm_epilogue_frame_sniffer (const struct frame_unwind *self,
3181 struct frame_info *this_frame,
3182 void **this_prologue_cache)
3183{
3184 if (frame_relative_level (this_frame) == 0)
3185 {
3186 struct gdbarch *gdbarch = get_frame_arch (this_frame);
3187 CORE_ADDR pc = get_frame_pc (this_frame);
3188
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);
3193 }
3194 else
3195 return 0;
3196}
3197
3198/* Frame unwinder from epilogue. */
3199
3200static const struct frame_unwind arm_epilogue_frame_unwind =
3201{
a154d838 3202 "arm epilogue",
779aa56f
YQ
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,
3209};
3210
80d8d390
YQ
3211/* Recognize GCC's trampoline for thumb call-indirect. If we are in a
3212 trampoline, return the target PC. Otherwise return 0.
3213
3214 void call0a (char c, short s, int i, long l) {}
3215
3216 int main (void)
3217 {
3218 (*pointer_to_call0a) (c, s, i, l);
3219 }
3220
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).
3224
3225 .global main
3226 .type main, %function
3227 ...
3228 bl .L1
3229 ...
3230 .size main, .-main
3231
3232 .L1:
3233 bx r2
3234
3235 The trampoline 'bx r2' doesn't belong to main. */
3236
3237static CORE_ADDR
3238arm_skip_bx_reg (struct frame_info *frame, CORE_ADDR pc)
3239{
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))
3243 {
3244 gdb_byte buf[2];
3245
3246 if (target_read_memory (pc, buf, 2) == 0)
3247 {
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);
3253
3254 if ((insn & 0xff80) == 0x4700) /* bx <Rm> */
3255 {
3256 CORE_ADDR dest
3257 = get_frame_register_unsigned (frame, bits (insn, 3, 6));
3258
3259 /* Clear the LSB so that gdb core sets step-resume
3260 breakpoint at the right address. */
3261 return UNMAKE_THUMB_ADDR (dest);
3262 }
3263 }
3264 }
3265
3266 return 0;
3267}
3268
909cf6ea 3269static struct arm_prologue_cache *
a262aec2 3270arm_make_stub_cache (struct frame_info *this_frame)
909cf6ea 3271{
909cf6ea 3272 struct arm_prologue_cache *cache;
909cf6ea 3273
35d5d4ee 3274 cache = FRAME_OBSTACK_ZALLOC (struct arm_prologue_cache);
0824193f 3275 arm_cache_init (cache, this_frame);
909cf6ea 3276
ae7e2f45 3277 arm_gdbarch_tdep *tdep
08106042 3278 = gdbarch_tdep<arm_gdbarch_tdep> (get_frame_arch (this_frame));
ae7e2f45
CL
3279 arm_cache_set_active_sp_value (cache, tdep,
3280 get_frame_register_unsigned (this_frame,
3281 ARM_SP_REGNUM));
909cf6ea
DJ
3282
3283 return cache;
3284}
3285
3286/* Our frame ID for a stub frame is the current SP and LR. */
3287
3288static void
a262aec2 3289arm_stub_this_id (struct frame_info *this_frame,
909cf6ea
DJ
3290 void **this_cache,
3291 struct frame_id *this_id)
3292{
3293 struct arm_prologue_cache *cache;
3294
3295 if (*this_cache == NULL)
a262aec2 3296 *this_cache = arm_make_stub_cache (this_frame);
9a3c8263 3297 cache = (struct arm_prologue_cache *) *this_cache;
909cf6ea 3298
ae7e2f45 3299 arm_gdbarch_tdep *tdep
08106042 3300 = gdbarch_tdep<arm_gdbarch_tdep> (get_frame_arch (this_frame));
ae7e2f45
CL
3301 *this_id = frame_id_build (arm_cache_get_prev_sp_value (cache, tdep),
3302 get_frame_pc (this_frame));
909cf6ea
DJ
3303}
3304
a262aec2
DJ
3305static int
3306arm_stub_unwind_sniffer (const struct frame_unwind *self,
3307 struct frame_info *this_frame,
3308 void **this_prologue_cache)
909cf6ea 3309{
93d42b30 3310 CORE_ADDR addr_in_block;
948f8e3d 3311 gdb_byte dummy[4];
18d18ac8
YQ
3312 CORE_ADDR pc, start_addr;
3313 const char *name;
909cf6ea 3314
a262aec2 3315 addr_in_block = get_frame_address_in_block (this_frame);
18d18ac8 3316 pc = get_frame_pc (this_frame);
3e5d3a5a 3317 if (in_plt_section (addr_in_block)
fc36e839
DE
3318 /* We also use the stub winder if the target memory is unreadable
3319 to avoid having the prologue unwinder trying to read it. */
18d18ac8
YQ
3320 || target_read_memory (pc, dummy, 4) != 0)
3321 return 1;
3322
3323 if (find_pc_partial_function (pc, &name, &start_addr, NULL) == 0
3324 && arm_skip_bx_reg (this_frame, pc) != 0)
a262aec2 3325 return 1;
909cf6ea 3326
a262aec2 3327 return 0;
909cf6ea
DJ
3328}
3329
a262aec2 3330struct frame_unwind arm_stub_unwind = {
a154d838 3331 "arm stub",
a262aec2 3332 NORMAL_FRAME,
8fbca658 3333 default_frame_unwind_stop_reason,
a262aec2
DJ
3334 arm_stub_this_id,
3335 arm_prologue_prev_register,
3336 NULL,
3337 arm_stub_unwind_sniffer
3338};
3339
2ae28aa9
YQ
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. */
3343
3344static struct arm_prologue_cache *
3345arm_m_exception_cache (struct frame_info *this_frame)
3346{
3347 struct gdbarch *gdbarch = get_frame_arch (this_frame);
08106042 3348 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
2ae28aa9 3349 struct arm_prologue_cache *cache;
2ae28aa9
YQ
3350
3351 cache = FRAME_OBSTACK_ZALLOC (struct arm_prologue_cache);
0824193f 3352 arm_cache_init (cache, this_frame);
2ae28aa9 3353
55ea94da
FH
3354 /* ARMv7-M Architecture Reference "B1.5.6 Exception entry behavior"
3355 describes which bits in LR that define which stack was used prior
3356 to the exception and if FPU is used (causing extended stack frame). */
3357
df4860da
TS
3358 CORE_ADDR lr = get_frame_register_unsigned (this_frame, ARM_LR_REGNUM);
3359 CORE_ADDR sp = get_frame_register_unsigned (this_frame, ARM_SP_REGNUM);
ef273377 3360
148ca9dd
YR
3361 /* ARMv7-M Architecture Reference "A2.3.1 Arm core registers"
3362 states that LR is set to 0xffffffff on reset. ARMv8-M Architecture
3363 Reference "B3.3 Registers" states that LR is set to 0xffffffff on warm
3364 reset if Main Extension is implemented, otherwise the value is unknown. */
3365 if (lr == 0xffffffff)
3366 {
3367 /* Terminate any further stack unwinding by referring to self. */
3368 arm_cache_set_active_sp_value (cache, tdep, sp);
3369 return cache;
3370 }
3371
df4860da
TS
3372 /* Check FNC_RETURN indicator bits (24-31). */
3373 bool fnc_return = (((lr >> 24) & 0xff) == 0xfe);
3374 if (fnc_return)
ef273377 3375 {
df4860da
TS
3376 /* FNC_RETURN is only valid for targets with Security Extension. */
3377 if (!tdep->have_sec_ext)
3378 {
3379 error (_("While unwinding an exception frame, found unexpected Link "
3380 "Register value %s that requires the security extension, "
3381 "but the extension was not found or is disabled. This "
3382 "should not happen and may be caused by corrupt data or a "
3383 "bug in GDB."), phex (lr, ARM_INT_REGISTER_SIZE));
3384 }
3385
8c9ae6df
YR
3386 if (!arm_unwind_secure_frames)
3387 {
3388 warning (_("Non-secure to secure stack unwinding disabled."));
ef273377 3389
8c9ae6df
YR
3390 /* Terminate any further stack unwinding by referring to self. */
3391 arm_cache_set_active_sp_value (cache, tdep, sp);
3392 return cache;
3393 }
3394
df4860da 3395 ULONGEST xpsr = get_frame_register_unsigned (this_frame, ARM_PS_REGNUM);
8c9ae6df
YR
3396 if ((xpsr & 0xff) != 0)
3397 /* Handler mode: This is the mode that exceptions are handled in. */
3398 arm_cache_switch_prev_sp (cache, tdep, tdep->m_profile_msp_s_regnum);
ef273377 3399 else
8c9ae6df
YR
3400 /* Thread mode: This is the normal mode that programs run in. */
3401 arm_cache_switch_prev_sp (cache, tdep, tdep->m_profile_psp_s_regnum);
ef273377 3402
df4860da 3403 CORE_ADDR unwound_sp = arm_cache_get_prev_sp_value (cache, tdep);
55ea94da 3404
8c9ae6df
YR
3405 /* Stack layout for a function call from Secure to Non-Secure state
3406 (ARMv8-M section B3.16):
55ea94da 3407
8c9ae6df
YR
3408 SP Offset
3409
3410 +-------------------+
3411 0x08 | |
3412 +-------------------+ <-- Original SP
3413 0x04 | Partial xPSR |
3414 +-------------------+
3415 0x00 | Return Address |
3416 +===================+ <-- New SP */
3417
3418 cache->saved_regs[ARM_PC_REGNUM].set_addr (unwound_sp + 0x00);
3419 cache->saved_regs[ARM_LR_REGNUM].set_addr (unwound_sp + 0x00);
3420 cache->saved_regs[ARM_PS_REGNUM].set_addr (unwound_sp + 0x04);
3421
3422 arm_cache_set_active_sp_value (cache, tdep, unwound_sp + 0x08);
ef273377
CL
3423
3424 return cache;
3425 }
3426
3427 /* Check EXC_RETURN indicator bits (24-31). */
df4860da 3428 bool exc_return = (((lr >> 24) & 0xff) == 0xff);
ef273377 3429 if (exc_return)
55ea94da 3430 {
df4860da
TS
3431 int sp_regnum;
3432 bool secure_stack_used = false;
3433 bool default_callee_register_stacking = false;
3434 bool exception_domain_is_secure = false;
3435 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
3436
ef273377 3437 /* Check EXC_RETURN bit SPSEL if Main or Thread (process) stack used. */
df4860da 3438 bool process_stack_used = (bit (lr, 2) != 0);
ef273377
CL
3439
3440 if (tdep->have_sec_ext)
3441 {
df4860da
TS
3442 secure_stack_used = (bit (lr, 6) != 0);
3443 default_callee_register_stacking = (bit (lr, 5) != 0);
3444 exception_domain_is_secure = (bit (lr, 0) == 0);
ef273377
CL
3445
3446 /* Unwinding from non-secure to secure can trip security
3447 measures. In order to avoid the debugger being
3448 intrusive, rely on the user to configure the requested
3449 mode. */
3450 if (secure_stack_used && !exception_domain_is_secure
3451 && !arm_unwind_secure_frames)
3452 {
3453 warning (_("Non-secure to secure stack unwinding disabled."));
3454
3455 /* Terminate any further stack unwinding by referring to self. */
3456 arm_cache_set_active_sp_value (cache, tdep, sp);
3457 return cache;
3458 }
3459
3460 if (process_stack_used)
3461 {
3462 if (secure_stack_used)
3463 /* Secure thread (process) stack used, use PSP_S as SP. */
df4860da 3464 sp_regnum = tdep->m_profile_psp_s_regnum;
ef273377
CL
3465 else
3466 /* Non-secure thread (process) stack used, use PSP_NS as SP. */
df4860da 3467 sp_regnum = tdep->m_profile_psp_ns_regnum;
ef273377
CL
3468 }
3469 else
3470 {
3471 if (secure_stack_used)
3472 /* Secure main stack used, use MSP_S as SP. */
df4860da 3473 sp_regnum = tdep->m_profile_msp_s_regnum;
ef273377
CL
3474 else
3475 /* Non-secure main stack used, use MSP_NS as SP. */
df4860da 3476 sp_regnum = tdep->m_profile_msp_ns_regnum;
ef273377
CL
3477 }
3478 }
3479 else
3480 {
3481 if (process_stack_used)
3482 /* Thread (process) stack used, use PSP as SP. */
df4860da 3483 sp_regnum = tdep->m_profile_psp_regnum;
ef273377
CL
3484 else
3485 /* Main stack used, use MSP as SP. */
df4860da
TS
3486 sp_regnum = tdep->m_profile_msp_regnum;
3487 }
3488
3489 /* Set the active SP regnum. */
3490 arm_cache_switch_prev_sp (cache, tdep, sp_regnum);
3491
3492 /* Fetch the SP to use for this frame. */
3493 CORE_ADDR unwound_sp = arm_cache_get_prev_sp_value (cache, tdep);
3494
3495 /* Exception entry context stacking are described in ARMv8-M (section
3496 B3.19) and ARMv7-M (sections B1.5.6 and B1.5.7) Architecture Reference
3497 Manuals.
3498
3499 The following figure shows the structure of the stack frame when
3500 Security and Floating-point extensions are present.
3501
3502 SP Offsets
3503 Without With
3504 Callee Regs Callee Regs
3505 (Secure -> Non-Secure)
3506 +-------------------+
3507 0xA8 | | 0xD0
3508 +===================+ --+ <-- Original SP
3509 0xA4 | S31 | 0xCC |
3510 +-------------------+ |
3511 ... | Additional FP context
3512 +-------------------+ |
3513 0x68 | S16 | 0x90 |
3514 +===================+ --+
3515 0x64 | Reserved | 0x8C |
3516 +-------------------+ |
3517 0x60 | FPSCR | 0x88 |
3518 +-------------------+ |
3519 0x5C | S15 | 0x84 | FP context
3520 +-------------------+ |
3521 ... |
3522 +-------------------+ |
3523 0x20 | S0 | 0x48 |
3524 +===================+ --+
3525 0x1C | xPSR | 0x44 |
3526 +-------------------+ |
3527 0x18 | Return address | 0x40 |
3528 +-------------------+ |
3529 0x14 | LR(R14) | 0x3C |
3530 +-------------------+ |
3531 0x10 | R12 | 0x38 | State context
3532 +-------------------+ |
3533 0x0C | R3 | 0x34 |
3534 +-------------------+ |
3535 ... |
3536 +-------------------+ |
3537 0x00 | R0 | 0x28 |
3538 +===================+ --+
3539 | R11 | 0x24 |
3540 +-------------------+ |
3541 ... |
3542 +-------------------+ | Additional state
3543 | R4 | 0x08 | context when
3544 +-------------------+ | transitioning from
3545 | Reserved | 0x04 | Secure to Non-Secure
3546 +-------------------+ |
3547 | Magic signature | 0x00 |
3548 +===================+ --+ <-- New SP */
3549
3550 uint32_t sp_r0_offset = 0;
3551
3552 /* With the Security extension, the hardware saves R4..R11 too. */
3553 if (tdep->have_sec_ext && secure_stack_used
3554 && (!default_callee_register_stacking || exception_domain_is_secure))
3555 {
3556 /* Read R4..R11 from the integer callee registers. */
3557 cache->saved_regs[4].set_addr (unwound_sp + 0x08);
3558 cache->saved_regs[5].set_addr (unwound_sp + 0x0C);
3559 cache->saved_regs[6].set_addr (unwound_sp + 0x10);
3560 cache->saved_regs[7].set_addr (unwound_sp + 0x14);
3561 cache->saved_regs[8].set_addr (unwound_sp + 0x18);
3562 cache->saved_regs[9].set_addr (unwound_sp + 0x1C);
3563 cache->saved_regs[10].set_addr (unwound_sp + 0x20);
3564 cache->saved_regs[11].set_addr (unwound_sp + 0x24);
3565 sp_r0_offset = 0x28;
3566 }
3567
3568 /* The hardware saves eight 32-bit words, comprising xPSR,
3569 ReturnAddress, LR (R14), R12, R3, R2, R1, R0. See details in
3570 "B1.5.6 Exception entry behavior" in
3571 "ARMv7-M Architecture Reference Manual". */
3572 cache->saved_regs[0].set_addr (unwound_sp + sp_r0_offset);
3573 cache->saved_regs[1].set_addr (unwound_sp + sp_r0_offset + 0x04);
3574 cache->saved_regs[2].set_addr (unwound_sp + sp_r0_offset + 0x08);
3575 cache->saved_regs[3].set_addr (unwound_sp + sp_r0_offset + 0x0C);
3576 cache->saved_regs[ARM_IP_REGNUM].set_addr (unwound_sp + sp_r0_offset
3577 + 0x10);
3578 cache->saved_regs[ARM_LR_REGNUM].set_addr (unwound_sp + sp_r0_offset
3579 + 0x14);
3580 cache->saved_regs[ARM_PC_REGNUM].set_addr (unwound_sp + sp_r0_offset
3581 + 0x18);
3582 cache->saved_regs[ARM_PS_REGNUM].set_addr (unwound_sp + sp_r0_offset
3583 + 0x1C);
3584
3585 /* Check EXC_RETURN bit FTYPE if extended stack frame (FPU regs stored)
3586 type used. */
3587 bool extended_frame_used = (bit (lr, 4) == 0);
3588 if (extended_frame_used)
3589 {
3590 ULONGEST fpccr;
3591
3592 /* Read FPCCR register. */
3593 gdb_assert (safe_read_memory_unsigned_integer (FPCCR,
3594 ARM_INT_REGISTER_SIZE,
3595 byte_order, &fpccr));
3596 bool fpccr_ts = bit (fpccr, 26);
3597
3598 /* This code does not take into account the lazy stacking, see "Lazy
3599 context save of FP state", in B1.5.7, also ARM AN298, supported
3600 by Cortex-M4F architecture.
3601 To fully handle this the FPCCR register (Floating-point Context
3602 Control Register) needs to be read out and the bits ASPEN and
3603 LSPEN could be checked to setup correct lazy stacked FP registers.
3604 This register is located at address 0xE000EF34. */
3605
3606 /* Extended stack frame type used. */
3607 CORE_ADDR addr = unwound_sp + sp_r0_offset + 0x20;
3608 for (int i = 0; i < 8; i++)
3609 {
3610 cache->saved_regs[ARM_D0_REGNUM + i].set_addr (addr);
3611 addr += 8;
3612 }
3613 cache->saved_regs[ARM_FPSCR_REGNUM].set_addr (unwound_sp
3614 + sp_r0_offset + 0x60);
ef273377 3615
df4860da
TS
3616 if (tdep->have_sec_ext && !default_callee_register_stacking
3617 && fpccr_ts)
3618 {
3619 /* Handle floating-point callee saved registers. */
3620 addr = unwound_sp + sp_r0_offset + 0x68;
3621 for (int i = 8; i < 16; i++)
3622 {
3623 cache->saved_regs[ARM_D0_REGNUM + i].set_addr (addr);
3624 addr += 8;
3625 }
2ae28aa9 3626
df4860da
TS
3627 arm_cache_set_active_sp_value (cache, tdep,
3628 unwound_sp + sp_r0_offset + 0xA8);
3629 }
3630 else
ef273377 3631 {
df4860da
TS
3632 /* Offset 0x64 is reserved. */
3633 arm_cache_set_active_sp_value (cache, tdep,
3634 unwound_sp + sp_r0_offset + 0x68);
ef273377 3635 }
ef273377
CL
3636 }
3637 else
3638 {
df4860da 3639 /* Standard stack frame type used. */
2d9cf99d 3640 arm_cache_set_active_sp_value (cache, tdep,
df4860da 3641 unwound_sp + sp_r0_offset + 0x20);
ef273377 3642 }
55ea94da 3643
df4860da
TS
3644 /* If bit 9 of the saved xPSR is set, then there is a four-byte
3645 aligner between the top of the 32-byte stack frame and the
3646 previous context's stack pointer. */
3647 ULONGEST xpsr;
3648 gdb_assert (safe_read_memory_unsigned_integer (cache->saved_regs[
3649 ARM_PS_REGNUM].addr (), 4,
3650 byte_order, &xpsr));
3651 if (bit (xpsr, 9) != 0)
3652 {
3653 CORE_ADDR new_sp = arm_cache_get_prev_sp_value (cache, tdep) + 4;
3654 arm_cache_set_active_sp_value (cache, tdep, new_sp);
3655 }
2ae28aa9 3656
df4860da
TS
3657 return cache;
3658 }
3659
3660 internal_error (__FILE__, __LINE__, _("While unwinding an exception frame, "
3661 "found unexpected Link Register value "
3662 "%s. This should not happen and may "
3663 "be caused by corrupt data or a bug in"
3664 " GDB."),
3665 phex (lr, ARM_INT_REGISTER_SIZE));
2ae28aa9
YQ
3666}
3667
3668/* Implementation of function hook 'this_id' in
3669 'struct frame_uwnind'. */
3670
3671static void
3672arm_m_exception_this_id (struct frame_info *this_frame,
3673 void **this_cache,
3674 struct frame_id *this_id)
3675{
3676 struct arm_prologue_cache *cache;
3677
3678 if (*this_cache == NULL)
3679 *this_cache = arm_m_exception_cache (this_frame);
9a3c8263 3680 cache = (struct arm_prologue_cache *) *this_cache;
2ae28aa9
YQ
3681
3682 /* Our frame ID for a stub frame is the current SP and LR. */
ae7e2f45 3683 arm_gdbarch_tdep *tdep
08106042 3684 = gdbarch_tdep<arm_gdbarch_tdep> (get_frame_arch (this_frame));
ae7e2f45 3685 *this_id = frame_id_build (arm_cache_get_prev_sp_value (cache, tdep),
2ae28aa9
YQ
3686 get_frame_pc (this_frame));
3687}
3688
3689/* Implementation of function hook 'prev_register' in
3690 'struct frame_uwnind'. */
3691
3692static struct value *
3693arm_m_exception_prev_register (struct frame_info *this_frame,
3694 void **this_cache,
3695 int prev_regnum)
3696{
2ae28aa9 3697 struct arm_prologue_cache *cache;
ef273377 3698 CORE_ADDR sp_value;
2ae28aa9
YQ
3699
3700 if (*this_cache == NULL)
3701 *this_cache = arm_m_exception_cache (this_frame);
9a3c8263 3702 cache = (struct arm_prologue_cache *) *this_cache;
2ae28aa9
YQ
3703
3704 /* The value was already reconstructed into PREV_SP. */
ae7e2f45 3705 arm_gdbarch_tdep *tdep
08106042 3706 = gdbarch_tdep<arm_gdbarch_tdep> (get_frame_arch (this_frame));
2ae28aa9
YQ
3707 if (prev_regnum == ARM_SP_REGNUM)
3708 return frame_unwind_got_constant (this_frame, prev_regnum,
ae7e2f45 3709 arm_cache_get_prev_sp_value (cache, tdep));
2ae28aa9 3710
8c9ae6df
YR
3711 /* If we are asked to unwind the PC, strip the saved T bit. */
3712 if (prev_regnum == ARM_PC_REGNUM)
3713 {
3714 struct value *value = trad_frame_get_prev_register (this_frame,
3715 cache->saved_regs,
3716 prev_regnum);
3717 CORE_ADDR pc = value_as_address (value);
3718 return frame_unwind_got_constant (this_frame, prev_regnum,
3719 UNMAKE_THUMB_ADDR (pc));
3720 }
3721
ef273377
CL
3722 /* The value might be one of the alternative SP, if so, use the
3723 value already constructed. */
d65edaa0 3724 if (arm_is_alternative_sp_register (tdep, prev_regnum))
ef273377
CL
3725 {
3726 sp_value = arm_cache_get_sp_register (cache, tdep, prev_regnum);
3727 return frame_unwind_got_constant (this_frame, prev_regnum, sp_value);
3728 }
3729
8c9ae6df
YR
3730 /* If we are asked to unwind the xPSR, set T bit if PC is in thumb mode.
3731 LR register is unreliable as it contains FNC_RETURN or EXC_RETURN
3732 pattern. */
3733 if (prev_regnum == ARM_PS_REGNUM)
3734 {
3735 struct gdbarch *gdbarch = get_frame_arch (this_frame);
3736 struct value *value = trad_frame_get_prev_register (this_frame,
3737 cache->saved_regs,
3738 ARM_PC_REGNUM);
3739 CORE_ADDR pc = value_as_address (value);
3740 value = trad_frame_get_prev_register (this_frame, cache->saved_regs,
3741 ARM_PS_REGNUM);
3742 ULONGEST xpsr = value_as_long (value);
3743
3744 /* Reconstruct the T bit; see arm_prologue_prev_register for details. */
3745 xpsr = reconstruct_t_bit (gdbarch, pc, xpsr);
3746 return frame_unwind_got_constant (this_frame, ARM_PS_REGNUM, xpsr);
3747 }
3748
2ae28aa9
YQ
3749 return trad_frame_get_prev_register (this_frame, cache->saved_regs,
3750 prev_regnum);
3751}
3752
3753/* Implementation of function hook 'sniffer' in
3754 'struct frame_uwnind'. */
3755
3756static int
3757arm_m_exception_unwind_sniffer (const struct frame_unwind *self,
3758 struct frame_info *this_frame,
3759 void **this_prologue_cache)
3760{
ef273377 3761 struct gdbarch *gdbarch = get_frame_arch (this_frame);
2ae28aa9
YQ
3762 CORE_ADDR this_pc = get_frame_pc (this_frame);
3763
3764 /* No need to check is_m; this sniffer is only registered for
3765 M-profile architectures. */
3766
ca90e760 3767 /* Check if exception frame returns to a magic PC value. */
ef273377 3768 return arm_m_addr_is_magic (gdbarch, this_pc);
2ae28aa9
YQ
3769}
3770
3771/* Frame unwinder for M-profile exceptions. */
3772
3773struct frame_unwind arm_m_exception_unwind =
3774{
a154d838 3775 "arm m exception",
2ae28aa9
YQ
3776 SIGTRAMP_FRAME,
3777 default_frame_unwind_stop_reason,
3778 arm_m_exception_this_id,
3779 arm_m_exception_prev_register,
3780 NULL,
3781 arm_m_exception_unwind_sniffer
3782};
3783
24de872b 3784static CORE_ADDR
a262aec2 3785arm_normal_frame_base (struct frame_info *this_frame, void **this_cache)
24de872b
DJ
3786{
3787 struct arm_prologue_cache *cache;
3788
eb5492fa 3789 if (*this_cache == NULL)
a262aec2 3790 *this_cache = arm_make_prologue_cache (this_frame);
9a3c8263 3791 cache = (struct arm_prologue_cache *) *this_cache;
eb5492fa 3792
ae7e2f45 3793 arm_gdbarch_tdep *tdep
08106042 3794 = gdbarch_tdep<arm_gdbarch_tdep> (get_frame_arch (this_frame));
ae7e2f45 3795 return arm_cache_get_prev_sp_value (cache, tdep) - cache->framesize;
24de872b
DJ
3796}
3797
eb5492fa
DJ
3798struct frame_base arm_normal_base = {
3799 &arm_prologue_unwind,
3800 arm_normal_frame_base,
3801 arm_normal_frame_base,
3802 arm_normal_frame_base
3803};
3804
b39cc962
DJ
3805static struct value *
3806arm_dwarf2_prev_register (struct frame_info *this_frame, void **this_cache,
3807 int regnum)
3808{
24568a2c 3809 struct gdbarch * gdbarch = get_frame_arch (this_frame);
08106042 3810 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
8c9ae6df
YR
3811 CORE_ADDR lr;
3812 ULONGEST cpsr;
b39cc962 3813
42e11f36 3814 if (regnum == ARM_PC_REGNUM)
b39cc962 3815 {
b39cc962
DJ
3816 /* The PC is normally copied from the return column, which
3817 describes saves of LR. However, that version may have an
3818 extra bit set to indicate Thumb state. The bit is not
3819 part of the PC. */
a01567f4
LM
3820
3821 /* Record in the frame whether the return address was signed. */
3822 if (tdep->have_pacbti)
3823 {
3824 CORE_ADDR ra_auth_code
3825 = frame_unwind_register_unsigned (this_frame,
3826 tdep->pacbti_pseudo_base);
3827
3828 if (ra_auth_code != 0)
3829 set_frame_previous_pc_masked (this_frame);
3830 }
3831
b39cc962
DJ
3832 lr = frame_unwind_register_unsigned (this_frame, ARM_LR_REGNUM);
3833 return frame_unwind_got_constant (this_frame, regnum,
24568a2c 3834 arm_addr_bits_remove (gdbarch, lr));
42e11f36
TS
3835 }
3836 else if (regnum == ARM_PS_REGNUM)
3837 {
b39cc962 3838 /* Reconstruct the T bit; see arm_prologue_prev_register for details. */
ca38c58e 3839 cpsr = get_frame_register_unsigned (this_frame, regnum);
b39cc962 3840 lr = frame_unwind_register_unsigned (this_frame, ARM_LR_REGNUM);
8c9ae6df 3841 cpsr = reconstruct_t_bit (gdbarch, lr, cpsr);
ca38c58e 3842 return frame_unwind_got_constant (this_frame, regnum, cpsr);
b39cc962 3843 }
a6e4a48c
YR
3844 else if (arm_is_alternative_sp_register (tdep, regnum))
3845 {
3846 /* Handle the alternative SP registers on Cortex-M. */
3847 bool override_with_sp_value = false;
3848 CORE_ADDR val;
3849
3850 if (tdep->have_sec_ext)
3851 {
3852 CORE_ADDR sp
3853 = get_frame_register_unsigned (this_frame, ARM_SP_REGNUM);
3854 CORE_ADDR msp_s
3855 = get_frame_register_unsigned (this_frame,
3856 tdep->m_profile_msp_s_regnum);
3857 CORE_ADDR msp_ns
3858 = get_frame_register_unsigned (this_frame,
3859 tdep->m_profile_msp_ns_regnum);
3860 CORE_ADDR psp_s
3861 = get_frame_register_unsigned (this_frame,
3862 tdep->m_profile_psp_s_regnum);
3863 CORE_ADDR psp_ns
3864 = get_frame_register_unsigned (this_frame,
3865 tdep->m_profile_psp_ns_regnum);
3866
3867 bool is_msp = (regnum == tdep->m_profile_msp_regnum)
3868 && (msp_s == sp || msp_ns == sp);
3869 bool is_msp_s = (regnum == tdep->m_profile_msp_s_regnum)
3870 && (msp_s == sp);
3871 bool is_msp_ns = (regnum == tdep->m_profile_msp_ns_regnum)
3872 && (msp_ns == sp);
3873 bool is_psp = (regnum == tdep->m_profile_psp_regnum)
3874 && (psp_s == sp || psp_ns == sp);
3875 bool is_psp_s = (regnum == tdep->m_profile_psp_s_regnum)
3876 && (psp_s == sp);
3877 bool is_psp_ns = (regnum == tdep->m_profile_psp_ns_regnum)
3878 && (psp_ns == sp);
3879
3880 override_with_sp_value = is_msp || is_msp_s || is_msp_ns
3881 || is_psp || is_psp_s || is_psp_ns;
3882
3883 }
3884 else if (tdep->is_m)
3885 {
3886 CORE_ADDR sp
3887 = get_frame_register_unsigned (this_frame, ARM_SP_REGNUM);
3888 CORE_ADDR msp
3889 = get_frame_register_unsigned (this_frame,
3890 tdep->m_profile_msp_regnum);
3891 CORE_ADDR psp
3892 = get_frame_register_unsigned (this_frame,
3893 tdep->m_profile_psp_regnum);
3894
3895 bool is_msp = (regnum == tdep->m_profile_msp_regnum) && (sp == msp);
3896 bool is_psp = (regnum == tdep->m_profile_psp_regnum) && (sp == psp);
3897
3898 override_with_sp_value = is_msp || is_psp;
3899 }
3900
3901 if (override_with_sp_value)
3902 {
3903 /* Use value of SP from previous frame. */
3904 struct frame_info *prev_frame = get_prev_frame (this_frame);
3905 if (prev_frame)
3906 val = get_frame_register_unsigned (prev_frame, ARM_SP_REGNUM);
3907 else
3908 val = get_frame_base (this_frame);
3909 }
3910 else
3911 /* Use value for the register from previous frame. */
3912 val = get_frame_register_unsigned (this_frame, regnum);
3913
3914 return frame_unwind_got_constant (this_frame, regnum, val);
3915 }
42e11f36
TS
3916
3917 internal_error (__FILE__, __LINE__,
3918 _("Unexpected register %d"), regnum);
b39cc962
DJ
3919}
3920
c9cf6e20 3921/* Implement the stack_frame_destroyed_p gdbarch method. */
4024ca99
UW
3922
3923static int
c9cf6e20 3924thumb_stack_frame_destroyed_p (struct gdbarch *gdbarch, CORE_ADDR pc)
4024ca99
UW
3925{
3926 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
3927 unsigned int insn, insn2;
3928 int found_return = 0, found_stack_adjust = 0;
3929 CORE_ADDR func_start, func_end;
3930 CORE_ADDR scan_pc;
3931 gdb_byte buf[4];
3932
3933 if (!find_pc_partial_function (pc, NULL, &func_start, &func_end))
3934 return 0;
3935
3936 /* The epilogue is a sequence of instructions along the following lines:
3937
3938 - add stack frame size to SP or FP
3939 - [if frame pointer used] restore SP from FP
3940 - restore registers from SP [may include PC]
3941 - a return-type instruction [if PC wasn't already restored]
3942
3943 In a first pass, we scan forward from the current PC and verify the
3944 instructions we find as compatible with this sequence, ending in a
3945 return instruction.
3946
3947 However, this is not sufficient to distinguish indirect function calls
3948 within a function from indirect tail calls in the epilogue in some cases.
3949 Therefore, if we didn't already find any SP-changing instruction during
3950 forward scan, we add a backward scanning heuristic to ensure we actually
3951 are in the epilogue. */
3952
3953 scan_pc = pc;
3954 while (scan_pc < func_end && !found_return)
3955 {
3956 if (target_read_memory (scan_pc, buf, 2))
3957 break;
3958
3959 scan_pc += 2;
3960 insn = extract_unsigned_integer (buf, 2, byte_order_for_code);
3961
3962 if ((insn & 0xff80) == 0x4700) /* bx <Rm> */
3963 found_return = 1;
3964 else if (insn == 0x46f7) /* mov pc, lr */
3965 found_return = 1;
540314bd 3966 else if (thumb_instruction_restores_sp (insn))
4024ca99 3967 {
b7576e5c 3968 if ((insn & 0xff00) == 0xbd00) /* pop <registers, PC> */
4024ca99
UW
3969 found_return = 1;
3970 }
db24da6d 3971 else if (thumb_insn_size (insn) == 4) /* 32-bit Thumb-2 instruction */
4024ca99
UW
3972 {
3973 if (target_read_memory (scan_pc, buf, 2))
3974 break;
3975
3976 scan_pc += 2;
3977 insn2 = extract_unsigned_integer (buf, 2, byte_order_for_code);
3978
3979 if (insn == 0xe8bd) /* ldm.w sp!, <registers> */
3980 {
4024ca99
UW
3981 if (insn2 & 0x8000) /* <registers> include PC. */
3982 found_return = 1;
3983 }
3984 else if (insn == 0xf85d /* ldr.w <Rt>, [sp], #4 */
3985 && (insn2 & 0x0fff) == 0x0b04)
3986 {
4024ca99
UW
3987 if ((insn2 & 0xf000) == 0xf000) /* <Rt> is PC. */
3988 found_return = 1;
3989 }
3990 else if ((insn & 0xffbf) == 0xecbd /* vldm sp!, <list> */
3991 && (insn2 & 0x0e00) == 0x0a00)
6b65d1b6 3992 ;
4024ca99
UW
3993 else
3994 break;
3995 }
3996 else
3997 break;
3998 }
3999
4000 if (!found_return)
4001 return 0;
4002
4003 /* Since any instruction in the epilogue sequence, with the possible
4004 exception of return itself, updates the stack pointer, we need to
4005 scan backwards for at most one instruction. Try either a 16-bit or
4006 a 32-bit instruction. This is just a heuristic, so we do not worry
0963b4bd 4007 too much about false positives. */
4024ca99 4008
6b65d1b6
YQ
4009 if (pc - 4 < func_start)
4010 return 0;
4011 if (target_read_memory (pc - 4, buf, 4))
4012 return 0;
4024ca99 4013
6b65d1b6
YQ
4014 insn = extract_unsigned_integer (buf, 2, byte_order_for_code);
4015 insn2 = extract_unsigned_integer (buf + 2, 2, byte_order_for_code);
4016
4017 if (thumb_instruction_restores_sp (insn2))
4018 found_stack_adjust = 1;
4019 else if (insn == 0xe8bd) /* ldm.w sp!, <registers> */
4020 found_stack_adjust = 1;
4021 else if (insn == 0xf85d /* ldr.w <Rt>, [sp], #4 */
4022 && (insn2 & 0x0fff) == 0x0b04)
4023 found_stack_adjust = 1;
4024 else if ((insn & 0xffbf) == 0xecbd /* vldm sp!, <list> */
4025 && (insn2 & 0x0e00) == 0x0a00)
4026 found_stack_adjust = 1;
4024ca99
UW
4027
4028 return found_stack_adjust;
4029}
4030
4024ca99 4031static int
c58b006a 4032arm_stack_frame_destroyed_p_1 (struct gdbarch *gdbarch, CORE_ADDR pc)
4024ca99
UW
4033{
4034 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
4035 unsigned int insn;
f303bc3e 4036 int found_return;
4024ca99
UW
4037 CORE_ADDR func_start, func_end;
4038
4024ca99
UW
4039 if (!find_pc_partial_function (pc, NULL, &func_start, &func_end))
4040 return 0;
4041
4042 /* We are in the epilogue if the previous instruction was a stack
4043 adjustment and the next instruction is a possible return (bx, mov
4044 pc, or pop). We could have to scan backwards to find the stack
4045 adjustment, or forwards to find the return, but this is a decent
4046 approximation. First scan forwards. */
4047
4048 found_return = 0;
4049 insn = read_memory_unsigned_integer (pc, 4, byte_order_for_code);
4050 if (bits (insn, 28, 31) != INST_NV)
4051 {
4052 if ((insn & 0x0ffffff0) == 0x012fff10)
4053 /* BX. */
4054 found_return = 1;
4055 else if ((insn & 0x0ffffff0) == 0x01a0f000)
4056 /* MOV PC. */
4057 found_return = 1;
4058 else if ((insn & 0x0fff0000) == 0x08bd0000
4059 && (insn & 0x0000c000) != 0)
4060 /* POP (LDMIA), including PC or LR. */
4061 found_return = 1;
4062 }
4063
4064 if (!found_return)
4065 return 0;
4066
4067 /* Scan backwards. This is just a heuristic, so do not worry about
4068 false positives from mode changes. */
4069
4070 if (pc < func_start + 4)
4071 return 0;
4072
4073 insn = read_memory_unsigned_integer (pc - 4, 4, byte_order_for_code);
f303bc3e 4074 if (arm_instruction_restores_sp (insn))
4024ca99
UW
4075 return 1;
4076
4077 return 0;
4078}
4079
c58b006a
YQ
4080/* Implement the stack_frame_destroyed_p gdbarch method. */
4081
4082static int
4083arm_stack_frame_destroyed_p (struct gdbarch *gdbarch, CORE_ADDR pc)
4084{
4085 if (arm_pc_is_thumb (gdbarch, pc))
4086 return thumb_stack_frame_destroyed_p (gdbarch, pc);
4087 else
4088 return arm_stack_frame_destroyed_p_1 (gdbarch, pc);
4089}
4024ca99 4090
2dd604e7
RE
4091/* When arguments must be pushed onto the stack, they go on in reverse
4092 order. The code below implements a FILO (stack) to do this. */
4093
0fecb1a7 4094struct arm_stack_item
2dd604e7
RE
4095{
4096 int len;
0fecb1a7 4097 struct arm_stack_item *prev;
7c543f7b 4098 gdb_byte *data;
2dd604e7
RE
4099};
4100
0fecb1a7
TT
4101static struct arm_stack_item *
4102push_stack_item (struct arm_stack_item *prev, const gdb_byte *contents,
4103 int len)
2dd604e7 4104{
0fecb1a7
TT
4105 struct arm_stack_item *si;
4106 si = XNEW (struct arm_stack_item);
7c543f7b 4107 si->data = (gdb_byte *) xmalloc (len);
2dd604e7
RE
4108 si->len = len;
4109 si->prev = prev;
4110 memcpy (si->data, contents, len);
4111 return si;
4112}
4113
0fecb1a7
TT
4114static struct arm_stack_item *
4115pop_stack_item (struct arm_stack_item *si)
2dd604e7 4116{
0fecb1a7 4117 struct arm_stack_item *dead = si;
2dd604e7
RE
4118 si = si->prev;
4119 xfree (dead->data);
4120 xfree (dead);
4121 return si;
4122}
4123
030197b4
AB
4124/* Implement the gdbarch type alignment method, overrides the generic
4125 alignment algorithm for anything that is arm specific. */
2af48f68 4126
030197b4
AB
4127static ULONGEST
4128arm_type_align (gdbarch *gdbarch, struct type *t)
2af48f68 4129{
2af48f68 4130 t = check_typedef (t);
bd63c870 4131 if (t->code () == TYPE_CODE_ARRAY && t->is_vector ())
2af48f68 4132 {
030197b4
AB
4133 /* Use the natural alignment for vector types (the same for
4134 scalar type), but the maximum alignment is 64-bit. */
df86565b 4135 if (t->length () > 8)
030197b4 4136 return 8;
c4312b19 4137 else
df86565b 4138 return t->length ();
2af48f68 4139 }
030197b4
AB
4140
4141 /* Allow the common code to calculate the alignment. */
4142 return 0;
2af48f68
PB
4143}
4144
90445bd3
DJ
4145/* Possible base types for a candidate for passing and returning in
4146 VFP registers. */
4147
4148enum arm_vfp_cprc_base_type
4149{
4150 VFP_CPRC_UNKNOWN,
4151 VFP_CPRC_SINGLE,
4152 VFP_CPRC_DOUBLE,
4153 VFP_CPRC_VEC64,
4154 VFP_CPRC_VEC128
4155};
4156
4157/* The length of one element of base type B. */
4158
4159static unsigned
4160arm_vfp_cprc_unit_length (enum arm_vfp_cprc_base_type b)
4161{
4162 switch (b)
4163 {
4164 case VFP_CPRC_SINGLE:
4165 return 4;
4166 case VFP_CPRC_DOUBLE:
4167 return 8;
4168 case VFP_CPRC_VEC64:
4169 return 8;
4170 case VFP_CPRC_VEC128:
4171 return 16;
4172 default:
4173 internal_error (__FILE__, __LINE__, _("Invalid VFP CPRC type: %d."),
4174 (int) b);
4175 }
4176}
4177
4178/* The character ('s', 'd' or 'q') for the type of VFP register used
4179 for passing base type B. */
4180
4181static int
4182arm_vfp_cprc_reg_char (enum arm_vfp_cprc_base_type b)
4183{
4184 switch (b)
4185 {
4186 case VFP_CPRC_SINGLE:
4187 return 's';
4188 case VFP_CPRC_DOUBLE:
4189 return 'd';
4190 case VFP_CPRC_VEC64:
4191 return 'd';
4192 case VFP_CPRC_VEC128:
4193 return 'q';
4194 default:
4195 internal_error (__FILE__, __LINE__, _("Invalid VFP CPRC type: %d."),
4196 (int) b);
4197 }
4198}
4199
4200/* Determine whether T may be part of a candidate for passing and
4201 returning in VFP registers, ignoring the limit on the total number
4202 of components. If *BASE_TYPE is VFP_CPRC_UNKNOWN, set it to the
4203 classification of the first valid component found; if it is not
4204 VFP_CPRC_UNKNOWN, all components must have the same classification
4205 as *BASE_TYPE. If it is found that T contains a type not permitted
4206 for passing and returning in VFP registers, a type differently
4207 classified from *BASE_TYPE, or two types differently classified
4208 from each other, return -1, otherwise return the total number of
4209 base-type elements found (possibly 0 in an empty structure or
817e0957
YQ
4210 array). Vector types are not currently supported, matching the
4211 generic AAPCS support. */
90445bd3
DJ
4212
4213static int
4214arm_vfp_cprc_sub_candidate (struct type *t,
4215 enum arm_vfp_cprc_base_type *base_type)
4216{
4217 t = check_typedef (t);
78134374 4218 switch (t->code ())
90445bd3
DJ
4219 {
4220 case TYPE_CODE_FLT:
df86565b 4221 switch (t->length ())
90445bd3
DJ
4222 {
4223 case 4:
4224 if (*base_type == VFP_CPRC_UNKNOWN)
4225 *base_type = VFP_CPRC_SINGLE;
4226 else if (*base_type != VFP_CPRC_SINGLE)
4227 return -1;
4228 return 1;
4229
4230 case 8:
4231 if (*base_type == VFP_CPRC_UNKNOWN)
4232 *base_type = VFP_CPRC_DOUBLE;
4233 else if (*base_type != VFP_CPRC_DOUBLE)
4234 return -1;
4235 return 1;
4236
4237 default:
4238 return -1;
4239 }
4240 break;
4241
817e0957
YQ
4242 case TYPE_CODE_COMPLEX:
4243 /* Arguments of complex T where T is one of the types float or
4244 double get treated as if they are implemented as:
4245
4246 struct complexT
4247 {
4248 T real;
4249 T imag;
5f52445b
YQ
4250 };
4251
4252 */
df86565b 4253 switch (t->length ())
817e0957
YQ
4254 {
4255 case 8:
4256 if (*base_type == VFP_CPRC_UNKNOWN)
4257 *base_type = VFP_CPRC_SINGLE;
4258 else if (*base_type != VFP_CPRC_SINGLE)
4259 return -1;
4260 return 2;
4261
4262 case 16:
4263 if (*base_type == VFP_CPRC_UNKNOWN)
4264 *base_type = VFP_CPRC_DOUBLE;
4265 else if (*base_type != VFP_CPRC_DOUBLE)
4266 return -1;
4267 return 2;
4268
4269 default:
4270 return -1;
4271 }
4272 break;
4273
90445bd3
DJ
4274 case TYPE_CODE_ARRAY:
4275 {
bd63c870 4276 if (t->is_vector ())
90445bd3 4277 {
c4312b19
YQ
4278 /* A 64-bit or 128-bit containerized vector type are VFP
4279 CPRCs. */
df86565b 4280 switch (t->length ())
c4312b19
YQ
4281 {
4282 case 8:
4283 if (*base_type == VFP_CPRC_UNKNOWN)
4284 *base_type = VFP_CPRC_VEC64;
4285 return 1;
4286 case 16:
4287 if (*base_type == VFP_CPRC_UNKNOWN)
4288 *base_type = VFP_CPRC_VEC128;
4289 return 1;
4290 default:
4291 return -1;
4292 }
4293 }
4294 else
4295 {
4296 int count;
4297 unsigned unitlen;
4298
27710edb 4299 count = arm_vfp_cprc_sub_candidate (t->target_type (),
c4312b19
YQ
4300 base_type);
4301 if (count == -1)
4302 return -1;
df86565b 4303 if (t->length () == 0)
c4312b19
YQ
4304 {
4305 gdb_assert (count == 0);
4306 return 0;
4307 }
4308 else if (count == 0)
4309 return -1;
4310 unitlen = arm_vfp_cprc_unit_length (*base_type);
df86565b
SM
4311 gdb_assert ((t->length () % unitlen) == 0);
4312 return t->length () / unitlen;
90445bd3 4313 }
90445bd3
DJ
4314 }
4315 break;
4316
4317 case TYPE_CODE_STRUCT:
4318 {
4319 int count = 0;
4320 unsigned unitlen;
4321 int i;
1f704f76 4322 for (i = 0; i < t->num_fields (); i++)
90445bd3 4323 {
1040b979
YQ
4324 int sub_count = 0;
4325
ceacbf6e 4326 if (!field_is_static (&t->field (i)))
940da03e 4327 sub_count = arm_vfp_cprc_sub_candidate (t->field (i).type (),
1040b979 4328 base_type);
90445bd3
DJ
4329 if (sub_count == -1)
4330 return -1;
4331 count += sub_count;
4332 }
df86565b 4333 if (t->length () == 0)
90445bd3
DJ
4334 {
4335 gdb_assert (count == 0);
4336 return 0;
4337 }
4338 else if (count == 0)
4339 return -1;
4340 unitlen = arm_vfp_cprc_unit_length (*base_type);
df86565b 4341 if (t->length () != unitlen * count)
90445bd3
DJ
4342 return -1;
4343 return count;
4344 }
4345
4346 case TYPE_CODE_UNION:
4347 {
4348 int count = 0;
4349 unsigned unitlen;
4350 int i;
1f704f76 4351 for (i = 0; i < t->num_fields (); i++)
90445bd3 4352 {
940da03e 4353 int sub_count = arm_vfp_cprc_sub_candidate (t->field (i).type (),
90445bd3
DJ
4354 base_type);
4355 if (sub_count == -1)
4356 return -1;
4357 count = (count > sub_count ? count : sub_count);
4358 }
df86565b 4359 if (t->length () == 0)
90445bd3
DJ
4360 {
4361 gdb_assert (count == 0);
4362 return 0;
4363 }
4364 else if (count == 0)
4365 return -1;
4366 unitlen = arm_vfp_cprc_unit_length (*base_type);
df86565b 4367 if (t->length () != unitlen * count)
90445bd3
DJ
4368 return -1;
4369 return count;
4370 }
4371
4372 default:
4373 break;
4374 }
4375
4376 return -1;
4377}
4378
4379/* Determine whether T is a VFP co-processor register candidate (CPRC)
4380 if passed to or returned from a non-variadic function with the VFP
4381 ABI in effect. Return 1 if it is, 0 otherwise. If it is, set
4382 *BASE_TYPE to the base type for T and *COUNT to the number of
4383 elements of that base type before returning. */
4384
4385static int
4386arm_vfp_call_candidate (struct type *t, enum arm_vfp_cprc_base_type *base_type,
4387 int *count)
4388{
4389 enum arm_vfp_cprc_base_type b = VFP_CPRC_UNKNOWN;
4390 int c = arm_vfp_cprc_sub_candidate (t, &b);
4391 if (c <= 0 || c > 4)
4392 return 0;
4393 *base_type = b;
4394 *count = c;
4395 return 1;
4396}
4397
4398/* Return 1 if the VFP ABI should be used for passing arguments to and
4399 returning values from a function of type FUNC_TYPE, 0
4400 otherwise. */
4401
4402static int
4403arm_vfp_abi_for_function (struct gdbarch *gdbarch, struct type *func_type)
4404{
08106042 4405 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
345bd07c 4406
90445bd3
DJ
4407 /* Variadic functions always use the base ABI. Assume that functions
4408 without debug info are not variadic. */
a409645d 4409 if (func_type && check_typedef (func_type)->has_varargs ())
90445bd3 4410 return 0;
345bd07c 4411
90445bd3
DJ
4412 /* The VFP ABI is only supported as a variant of AAPCS. */
4413 if (tdep->arm_abi != ARM_ABI_AAPCS)
4414 return 0;
345bd07c
SM
4415
4416 return tdep->fp_model == ARM_FLOAT_VFP;
90445bd3
DJ
4417}
4418
4419/* We currently only support passing parameters in integer registers, which
4420 conforms with GCC's default model, and VFP argument passing following
4421 the VFP variant of AAPCS. Several other variants exist and
2dd604e7
RE
4422 we should probably support some of them based on the selected ABI. */
4423
4424static CORE_ADDR
7d9b040b 4425arm_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
6a65450a 4426 struct regcache *regcache, CORE_ADDR bp_addr, int nargs,
cf84fa6b
AH
4427 struct value **args, CORE_ADDR sp,
4428 function_call_return_method return_method,
6a65450a 4429 CORE_ADDR struct_addr)
2dd604e7 4430{
e17a4113 4431 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
2dd604e7
RE
4432 int argnum;
4433 int argreg;
4434 int nstack;
0fecb1a7 4435 struct arm_stack_item *si = NULL;
90445bd3
DJ
4436 int use_vfp_abi;
4437 struct type *ftype;
4438 unsigned vfp_regs_free = (1 << 16) - 1;
08106042 4439 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
90445bd3
DJ
4440
4441 /* Determine the type of this function and whether the VFP ABI
4442 applies. */
4443 ftype = check_typedef (value_type (function));
78134374 4444 if (ftype->code () == TYPE_CODE_PTR)
27710edb 4445 ftype = check_typedef (ftype->target_type ());
90445bd3 4446 use_vfp_abi = arm_vfp_abi_for_function (gdbarch, ftype);
2dd604e7 4447
6a65450a
AC
4448 /* Set the return address. For the ARM, the return breakpoint is
4449 always at BP_ADDR. */
9779414d 4450 if (arm_pc_is_thumb (gdbarch, bp_addr))
9dca5578 4451 bp_addr |= 1;
6a65450a 4452 regcache_cooked_write_unsigned (regcache, ARM_LR_REGNUM, bp_addr);
2dd604e7
RE
4453
4454 /* Walk through the list of args and determine how large a temporary
4455 stack is required. Need to take care here as structs may be
7a9dd1b2 4456 passed on the stack, and we have to push them. */
2dd604e7
RE
4457 nstack = 0;
4458
4459 argreg = ARM_A1_REGNUM;
4460 nstack = 0;
4461
2dd604e7
RE
4462 /* The struct_return pointer occupies the first parameter
4463 passing register. */
cf84fa6b 4464 if (return_method == return_method_struct)
2dd604e7 4465 {
7cb6d92a
SM
4466 arm_debug_printf ("struct return in %s = %s",
4467 gdbarch_register_name (gdbarch, argreg),
4468 paddress (gdbarch, struct_addr));
4469
2dd604e7
RE
4470 regcache_cooked_write_unsigned (regcache, argreg, struct_addr);
4471 argreg++;
4472 }
4473
4474 for (argnum = 0; argnum < nargs; argnum++)
4475 {
4476 int len;
4477 struct type *arg_type;
4478 struct type *target_type;
4479 enum type_code typecode;
8c6363cf 4480 const bfd_byte *val;
2af48f68 4481 int align;
90445bd3
DJ
4482 enum arm_vfp_cprc_base_type vfp_base_type;
4483 int vfp_base_count;
4484 int may_use_core_reg = 1;
2dd604e7 4485
df407dfe 4486 arg_type = check_typedef (value_type (args[argnum]));
df86565b 4487 len = arg_type->length ();
27710edb 4488 target_type = arg_type->target_type ();
78134374 4489 typecode = arg_type->code ();
50888e42 4490 val = value_contents (args[argnum]).data ();
2dd604e7 4491
030197b4 4492 align = type_align (arg_type);
2af48f68 4493 /* Round alignment up to a whole number of words. */
f0452268
AH
4494 align = (align + ARM_INT_REGISTER_SIZE - 1)
4495 & ~(ARM_INT_REGISTER_SIZE - 1);
2af48f68 4496 /* Different ABIs have different maximum alignments. */
345bd07c 4497 if (tdep->arm_abi == ARM_ABI_APCS)
2af48f68
PB
4498 {
4499 /* The APCS ABI only requires word alignment. */
f0452268 4500 align = ARM_INT_REGISTER_SIZE;
2af48f68
PB
4501 }
4502 else
4503 {
4504 /* The AAPCS requires at most doubleword alignment. */
f0452268
AH
4505 if (align > ARM_INT_REGISTER_SIZE * 2)
4506 align = ARM_INT_REGISTER_SIZE * 2;
2af48f68
PB
4507 }
4508
90445bd3
DJ
4509 if (use_vfp_abi
4510 && arm_vfp_call_candidate (arg_type, &vfp_base_type,
4511 &vfp_base_count))
4512 {
4513 int regno;
4514 int unit_length;
4515 int shift;
4516 unsigned mask;
4517
4518 /* Because this is a CPRC it cannot go in a core register or
4519 cause a core register to be skipped for alignment.
4520 Either it goes in VFP registers and the rest of this loop
4521 iteration is skipped for this argument, or it goes on the
4522 stack (and the stack alignment code is correct for this
4523 case). */
4524 may_use_core_reg = 0;
4525
4526 unit_length = arm_vfp_cprc_unit_length (vfp_base_type);
4527 shift = unit_length / 4;
4528 mask = (1 << (shift * vfp_base_count)) - 1;
4529 for (regno = 0; regno < 16; regno += shift)
4530 if (((vfp_regs_free >> regno) & mask) == mask)
4531 break;
4532
4533 if (regno < 16)
4534 {
4535 int reg_char;
4536 int reg_scaled;
4537 int i;
4538
4539 vfp_regs_free &= ~(mask << regno);
4540 reg_scaled = regno / shift;
4541 reg_char = arm_vfp_cprc_reg_char (vfp_base_type);
4542 for (i = 0; i < vfp_base_count; i++)
4543 {
4544 char name_buf[4];
4545 int regnum;
58d6951d
DJ
4546 if (reg_char == 'q')
4547 arm_neon_quad_write (gdbarch, regcache, reg_scaled + i,
90445bd3 4548 val + i * unit_length);
58d6951d
DJ
4549 else
4550 {
8c042590
PM
4551 xsnprintf (name_buf, sizeof (name_buf), "%c%d",
4552 reg_char, reg_scaled + i);
58d6951d
DJ
4553 regnum = user_reg_map_name_to_regnum (gdbarch, name_buf,
4554 strlen (name_buf));
b66f5587 4555 regcache->cooked_write (regnum, val + i * unit_length);
58d6951d 4556 }
90445bd3
DJ
4557 }
4558 continue;
4559 }
4560 else
4561 {
4562 /* This CPRC could not go in VFP registers, so all VFP
4563 registers are now marked as used. */
4564 vfp_regs_free = 0;
4565 }
4566 }
4567
85102364 4568 /* Push stack padding for doubleword alignment. */
2af48f68
PB
4569 if (nstack & (align - 1))
4570 {
f0452268
AH
4571 si = push_stack_item (si, val, ARM_INT_REGISTER_SIZE);
4572 nstack += ARM_INT_REGISTER_SIZE;
2af48f68
PB
4573 }
4574
4575 /* Doubleword aligned quantities must go in even register pairs. */
90445bd3
DJ
4576 if (may_use_core_reg
4577 && argreg <= ARM_LAST_ARG_REGNUM
f0452268 4578 && align > ARM_INT_REGISTER_SIZE
2af48f68
PB
4579 && argreg & 1)
4580 argreg++;
4581
2dd604e7
RE
4582 /* If the argument is a pointer to a function, and it is a
4583 Thumb function, create a LOCAL copy of the value and set
4584 the THUMB bit in it. */
4585 if (TYPE_CODE_PTR == typecode
4586 && target_type != NULL
78134374 4587 && TYPE_CODE_FUNC == check_typedef (target_type)->code ())
2dd604e7 4588 {
e17a4113 4589 CORE_ADDR regval = extract_unsigned_integer (val, len, byte_order);
9779414d 4590 if (arm_pc_is_thumb (gdbarch, regval))
2dd604e7 4591 {
224c3ddb 4592 bfd_byte *copy = (bfd_byte *) alloca (len);
8c6363cf 4593 store_unsigned_integer (copy, len, byte_order,
e17a4113 4594 MAKE_THUMB_ADDR (regval));
8c6363cf 4595 val = copy;
2dd604e7
RE
4596 }
4597 }
4598
4599 /* Copy the argument to general registers or the stack in
4600 register-sized pieces. Large arguments are split between
4601 registers and stack. */
4602 while (len > 0)
4603 {
f0452268
AH
4604 int partial_len = len < ARM_INT_REGISTER_SIZE
4605 ? len : ARM_INT_REGISTER_SIZE;
ef9bd0b8
YQ
4606 CORE_ADDR regval
4607 = extract_unsigned_integer (val, partial_len, byte_order);
2dd604e7 4608
90445bd3 4609 if (may_use_core_reg && argreg <= ARM_LAST_ARG_REGNUM)
2dd604e7
RE
4610 {
4611 /* The argument is being passed in a general purpose
4612 register. */
e17a4113 4613 if (byte_order == BFD_ENDIAN_BIG)
f0452268 4614 regval <<= (ARM_INT_REGISTER_SIZE - partial_len) * 8;
7cb6d92a
SM
4615
4616 arm_debug_printf ("arg %d in %s = 0x%s", argnum,
4617 gdbarch_register_name (gdbarch, argreg),
4618 phex (regval, ARM_INT_REGISTER_SIZE));
4619
2dd604e7
RE
4620 regcache_cooked_write_unsigned (regcache, argreg, regval);
4621 argreg++;
4622 }
4623 else
4624 {
f0452268 4625 gdb_byte buf[ARM_INT_REGISTER_SIZE];
ef9bd0b8
YQ
4626
4627 memset (buf, 0, sizeof (buf));
4628 store_unsigned_integer (buf, partial_len, byte_order, regval);
4629
2dd604e7 4630 /* Push the arguments onto the stack. */
7cb6d92a 4631 arm_debug_printf ("arg %d @ sp + %d", argnum, nstack);
f0452268
AH
4632 si = push_stack_item (si, buf, ARM_INT_REGISTER_SIZE);
4633 nstack += ARM_INT_REGISTER_SIZE;
2dd604e7
RE
4634 }
4635
4636 len -= partial_len;
4637 val += partial_len;
4638 }
4639 }
4640 /* If we have an odd number of words to push, then decrement the stack
4641 by one word now, so first stack argument will be dword aligned. */
4642 if (nstack & 4)
4643 sp -= 4;
4644
4645 while (si)
4646 {
4647 sp -= si->len;
4648 write_memory (sp, si->data, si->len);
4649 si = pop_stack_item (si);
4650 }
4651
4652 /* Finally, update teh SP register. */
4653 regcache_cooked_write_unsigned (regcache, ARM_SP_REGNUM, sp);
4654
4655 return sp;
4656}
4657
f53f0d0b
PB
4658
4659/* Always align the frame to an 8-byte boundary. This is required on
4660 some platforms and harmless on the rest. */
4661
4662static CORE_ADDR
4663arm_frame_align (struct gdbarch *gdbarch, CORE_ADDR sp)
4664{
4665 /* Align the stack to eight bytes. */
4666 return sp & ~ (CORE_ADDR) 7;
4667}
4668
c906108c 4669static void
12b27276 4670print_fpu_flags (struct ui_file *file, int flags)
c906108c 4671{
c5aa993b 4672 if (flags & (1 << 0))
0426ad51 4673 gdb_puts ("IVO ", file);
c5aa993b 4674 if (flags & (1 << 1))
0426ad51 4675 gdb_puts ("DVZ ", file);
c5aa993b 4676 if (flags & (1 << 2))
0426ad51 4677 gdb_puts ("OFL ", file);
c5aa993b 4678 if (flags & (1 << 3))
0426ad51 4679 gdb_puts ("UFL ", file);
c5aa993b 4680 if (flags & (1 << 4))
0426ad51 4681 gdb_puts ("INX ", file);
a11ac3b3 4682 gdb_putc ('\n', file);
c906108c
SS
4683}
4684
5e74b15c
RE
4685/* Print interesting information about the floating point processor
4686 (if present) or emulator. */
34e8f22d 4687static void
d855c300 4688arm_print_float_info (struct gdbarch *gdbarch, struct ui_file *file,
23e3a7ac 4689 struct frame_info *frame, const char *args)
c906108c 4690{
9c9acae0 4691 unsigned long status = get_frame_register_unsigned (frame, ARM_FPS_REGNUM);
c5aa993b
JM
4692 int type;
4693
4694 type = (status >> 24) & 127;
edefbb7c 4695 if (status & (1 << 31))
6cb06a8c 4696 gdb_printf (file, _("Hardware FPU type %d\n"), type);
edefbb7c 4697 else
6cb06a8c 4698 gdb_printf (file, _("Software FPU type %d\n"), type);
edefbb7c 4699 /* i18n: [floating point unit] mask */
0426ad51 4700 gdb_puts (_("mask: "), file);
12b27276 4701 print_fpu_flags (file, status >> 16);
edefbb7c 4702 /* i18n: [floating point unit] flags */
0426ad51 4703 gdb_puts (_("flags: "), file);
12b27276 4704 print_fpu_flags (file, status);
c906108c
SS
4705}
4706
27067745
UW
4707/* Construct the ARM extended floating point type. */
4708static struct type *
4709arm_ext_type (struct gdbarch *gdbarch)
4710{
08106042 4711 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
27067745
UW
4712
4713 if (!tdep->arm_ext_type)
4714 tdep->arm_ext_type
e9bb382b 4715 = arch_float_type (gdbarch, -1, "builtin_type_arm_ext",
27067745
UW
4716 floatformats_arm_ext);
4717
4718 return tdep->arm_ext_type;
4719}
4720
58d6951d
DJ
4721static struct type *
4722arm_neon_double_type (struct gdbarch *gdbarch)
4723{
08106042 4724 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
58d6951d
DJ
4725
4726 if (tdep->neon_double_type == NULL)
4727 {
4728 struct type *t, *elem;
4729
4730 t = arch_composite_type (gdbarch, "__gdb_builtin_type_neon_d",
4731 TYPE_CODE_UNION);
4732 elem = builtin_type (gdbarch)->builtin_uint8;
4733 append_composite_type_field (t, "u8", init_vector_type (elem, 8));
4734 elem = builtin_type (gdbarch)->builtin_uint16;
4735 append_composite_type_field (t, "u16", init_vector_type (elem, 4));
4736 elem = builtin_type (gdbarch)->builtin_uint32;
4737 append_composite_type_field (t, "u32", init_vector_type (elem, 2));
4738 elem = builtin_type (gdbarch)->builtin_uint64;
4739 append_composite_type_field (t, "u64", elem);
4740 elem = builtin_type (gdbarch)->builtin_float;
4741 append_composite_type_field (t, "f32", init_vector_type (elem, 2));
4742 elem = builtin_type (gdbarch)->builtin_double;
4743 append_composite_type_field (t, "f64", elem);
4744
2062087b 4745 t->set_is_vector (true);
d0e39ea2 4746 t->set_name ("neon_d");
58d6951d
DJ
4747 tdep->neon_double_type = t;
4748 }
4749
4750 return tdep->neon_double_type;
4751}
4752
4753/* FIXME: The vector types are not correctly ordered on big-endian
4754 targets. Just as s0 is the low bits of d0, d0[0] is also the low
4755 bits of d0 - regardless of what unit size is being held in d0. So
4756 the offset of the first uint8 in d0 is 7, but the offset of the
4757 first float is 4. This code works as-is for little-endian
4758 targets. */
4759
4760static struct type *
4761arm_neon_quad_type (struct gdbarch *gdbarch)
4762{
08106042 4763 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
58d6951d
DJ
4764
4765 if (tdep->neon_quad_type == NULL)
4766 {
4767 struct type *t, *elem;
4768
4769 t = arch_composite_type (gdbarch, "__gdb_builtin_type_neon_q",
4770 TYPE_CODE_UNION);
4771 elem = builtin_type (gdbarch)->builtin_uint8;
4772 append_composite_type_field (t, "u8", init_vector_type (elem, 16));
4773 elem = builtin_type (gdbarch)->builtin_uint16;
4774 append_composite_type_field (t, "u16", init_vector_type (elem, 8));
4775 elem = builtin_type (gdbarch)->builtin_uint32;
4776 append_composite_type_field (t, "u32", init_vector_type (elem, 4));
4777 elem = builtin_type (gdbarch)->builtin_uint64;
4778 append_composite_type_field (t, "u64", init_vector_type (elem, 2));
4779 elem = builtin_type (gdbarch)->builtin_float;
4780 append_composite_type_field (t, "f32", init_vector_type (elem, 4));
4781 elem = builtin_type (gdbarch)->builtin_double;
4782 append_composite_type_field (t, "f64", init_vector_type (elem, 2));
4783
2062087b 4784 t->set_is_vector (true);
d0e39ea2 4785 t->set_name ("neon_q");
58d6951d
DJ
4786 tdep->neon_quad_type = t;
4787 }
4788
4789 return tdep->neon_quad_type;
4790}
4791
ecbf5d4f
LM
4792/* Return true if REGNUM is a Q pseudo register. Return false
4793 otherwise.
4794
4795 REGNUM is the raw register number and not a pseudo-relative register
4796 number. */
4797
4798static bool
4799is_q_pseudo (struct gdbarch *gdbarch, int regnum)
4800{
08106042 4801 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
ecbf5d4f 4802
ae66a8f1
SP
4803 /* Q pseudo registers are available for both NEON (Q0~Q15) and
4804 MVE (Q0~Q7) features. */
ecbf5d4f
LM
4805 if (tdep->have_q_pseudos
4806 && regnum >= tdep->q_pseudo_base
4807 && regnum < (tdep->q_pseudo_base + tdep->q_pseudo_count))
4808 return true;
4809
4810 return false;
4811}
4812
4813/* Return true if REGNUM is a VFP S pseudo register. Return false
4814 otherwise.
4815
4816 REGNUM is the raw register number and not a pseudo-relative register
4817 number. */
4818
4819static bool
4820is_s_pseudo (struct gdbarch *gdbarch, int regnum)
4821{
08106042 4822 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
ecbf5d4f
LM
4823
4824 if (tdep->have_s_pseudos
4825 && regnum >= tdep->s_pseudo_base
4826 && regnum < (tdep->s_pseudo_base + tdep->s_pseudo_count))
4827 return true;
4828
4829 return false;
4830}
4831
ae66a8f1
SP
4832/* Return true if REGNUM is a MVE pseudo register (P0). Return false
4833 otherwise.
4834
4835 REGNUM is the raw register number and not a pseudo-relative register
4836 number. */
4837
4838static bool
4839is_mve_pseudo (struct gdbarch *gdbarch, int regnum)
4840{
08106042 4841 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
ae66a8f1
SP
4842
4843 if (tdep->have_mve
4844 && regnum >= tdep->mve_pseudo_base
4845 && regnum < tdep->mve_pseudo_base + tdep->mve_pseudo_count)
4846 return true;
4847
4848 return false;
4849}
4850
a01567f4
LM
4851/* Return true if REGNUM is a PACBTI pseudo register (ra_auth_code). Return
4852 false otherwise.
4853
4854 REGNUM is the raw register number and not a pseudo-relative register
4855 number. */
4856
4857static bool
4858is_pacbti_pseudo (struct gdbarch *gdbarch, int regnum)
4859{
08106042 4860 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
a01567f4
LM
4861
4862 if (tdep->have_pacbti
4863 && regnum >= tdep->pacbti_pseudo_base
4864 && regnum < tdep->pacbti_pseudo_base + tdep->pacbti_pseudo_count)
4865 return true;
4866
4867 return false;
4868}
4869
34e8f22d
RE
4870/* Return the GDB type object for the "standard" data type of data in
4871 register N. */
4872
4873static struct type *
7a5ea0d4 4874arm_register_type (struct gdbarch *gdbarch, int regnum)
032758dc 4875{
08106042 4876 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
58d6951d 4877
ecbf5d4f 4878 if (is_s_pseudo (gdbarch, regnum))
58d6951d
DJ
4879 return builtin_type (gdbarch)->builtin_float;
4880
ecbf5d4f 4881 if (is_q_pseudo (gdbarch, regnum))
58d6951d
DJ
4882 return arm_neon_quad_type (gdbarch);
4883
ae66a8f1
SP
4884 if (is_mve_pseudo (gdbarch, regnum))
4885 return builtin_type (gdbarch)->builtin_int16;
4886
a01567f4
LM
4887 if (is_pacbti_pseudo (gdbarch, regnum))
4888 return builtin_type (gdbarch)->builtin_uint32;
4889
58d6951d
DJ
4890 /* If the target description has register information, we are only
4891 in this function so that we can override the types of
4892 double-precision registers for NEON. */
4893 if (tdesc_has_registers (gdbarch_target_desc (gdbarch)))
4894 {
4895 struct type *t = tdesc_register_type (gdbarch, regnum);
4896
4897 if (regnum >= ARM_D0_REGNUM && regnum < ARM_D0_REGNUM + 32
78134374 4898 && t->code () == TYPE_CODE_FLT
ecbf5d4f 4899 && tdep->have_neon)
58d6951d
DJ
4900 return arm_neon_double_type (gdbarch);
4901 else
4902 return t;
4903 }
4904
34e8f22d 4905 if (regnum >= ARM_F0_REGNUM && regnum < ARM_F0_REGNUM + NUM_FREGS)
58d6951d 4906 {
ecbf5d4f 4907 if (!tdep->have_fpa_registers)
58d6951d
DJ
4908 return builtin_type (gdbarch)->builtin_void;
4909
4910 return arm_ext_type (gdbarch);
4911 }
e4c16157 4912 else if (regnum == ARM_SP_REGNUM)
0dfff4cb 4913 return builtin_type (gdbarch)->builtin_data_ptr;
e4c16157 4914 else if (regnum == ARM_PC_REGNUM)
0dfff4cb 4915 return builtin_type (gdbarch)->builtin_func_ptr;
ff6f572f
DJ
4916 else if (regnum >= ARRAY_SIZE (arm_register_names))
4917 /* These registers are only supported on targets which supply
4918 an XML description. */
df4df182 4919 return builtin_type (gdbarch)->builtin_int0;
032758dc 4920 else
df4df182 4921 return builtin_type (gdbarch)->builtin_uint32;
032758dc
AC
4922}
4923
ff6f572f
DJ
4924/* Map a DWARF register REGNUM onto the appropriate GDB register
4925 number. */
4926
4927static int
d3f73121 4928arm_dwarf_reg_to_regnum (struct gdbarch *gdbarch, int reg)
ff6f572f
DJ
4929{
4930 /* Core integer regs. */
4931 if (reg >= 0 && reg <= 15)
4932 return reg;
4933
4934 /* Legacy FPA encoding. These were once used in a way which
4935 overlapped with VFP register numbering, so their use is
4936 discouraged, but GDB doesn't support the ARM toolchain
4937 which used them for VFP. */
4938 if (reg >= 16 && reg <= 23)
4939 return ARM_F0_REGNUM + reg - 16;
4940
4941 /* New assignments for the FPA registers. */
4942 if (reg >= 96 && reg <= 103)
4943 return ARM_F0_REGNUM + reg - 96;
4944
4945 /* WMMX register assignments. */
4946 if (reg >= 104 && reg <= 111)
4947 return ARM_WCGR0_REGNUM + reg - 104;
4948
4949 if (reg >= 112 && reg <= 127)
4950 return ARM_WR0_REGNUM + reg - 112;
4951
a01567f4
LM
4952 /* PACBTI register containing the Pointer Authentication Code. */
4953 if (reg == ARM_DWARF_RA_AUTH_CODE)
4954 {
08106042 4955 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
a01567f4
LM
4956
4957 if (tdep->have_pacbti)
4958 return tdep->pacbti_pseudo_base;
4959
4960 return -1;
4961 }
4962
ff6f572f
DJ
4963 if (reg >= 192 && reg <= 199)
4964 return ARM_WC0_REGNUM + reg - 192;
4965
58d6951d
DJ
4966 /* VFP v2 registers. A double precision value is actually
4967 in d1 rather than s2, but the ABI only defines numbering
4968 for the single precision registers. This will "just work"
4969 in GDB for little endian targets (we'll read eight bytes,
4970 starting in s0 and then progressing to s1), but will be
4971 reversed on big endian targets with VFP. This won't
4972 be a problem for the new Neon quad registers; you're supposed
4973 to use DW_OP_piece for those. */
4974 if (reg >= 64 && reg <= 95)
4975 {
4976 char name_buf[4];
4977
8c042590 4978 xsnprintf (name_buf, sizeof (name_buf), "s%d", reg - 64);
58d6951d
DJ
4979 return user_reg_map_name_to_regnum (gdbarch, name_buf,
4980 strlen (name_buf));
4981 }
4982
4983 /* VFP v3 / Neon registers. This range is also used for VFP v2
4984 registers, except that it now describes d0 instead of s0. */
4985 if (reg >= 256 && reg <= 287)
4986 {
4987 char name_buf[4];
4988
8c042590 4989 xsnprintf (name_buf, sizeof (name_buf), "d%d", reg - 256);
58d6951d
DJ
4990 return user_reg_map_name_to_regnum (gdbarch, name_buf,
4991 strlen (name_buf));
4992 }
4993
ff6f572f
DJ
4994 return -1;
4995}
4996
26216b98
AC
4997/* Map GDB internal REGNUM onto the Arm simulator register numbers. */
4998static int
e7faf938 4999arm_register_sim_regno (struct gdbarch *gdbarch, int regnum)
26216b98
AC
5000{
5001 int reg = regnum;
e7faf938 5002 gdb_assert (reg >= 0 && reg < gdbarch_num_regs (gdbarch));
26216b98 5003
ff6f572f
DJ
5004 if (regnum >= ARM_WR0_REGNUM && regnum <= ARM_WR15_REGNUM)
5005 return regnum - ARM_WR0_REGNUM + SIM_ARM_IWMMXT_COP0R0_REGNUM;
5006
5007 if (regnum >= ARM_WC0_REGNUM && regnum <= ARM_WC7_REGNUM)
5008 return regnum - ARM_WC0_REGNUM + SIM_ARM_IWMMXT_COP1R0_REGNUM;
5009
5010 if (regnum >= ARM_WCGR0_REGNUM && regnum <= ARM_WCGR7_REGNUM)
5011 return regnum - ARM_WCGR0_REGNUM + SIM_ARM_IWMMXT_COP1R8_REGNUM;
5012
26216b98
AC
5013 if (reg < NUM_GREGS)
5014 return SIM_ARM_R0_REGNUM + reg;
5015 reg -= NUM_GREGS;
5016
5017 if (reg < NUM_FREGS)
5018 return SIM_ARM_FP0_REGNUM + reg;
5019 reg -= NUM_FREGS;
5020
5021 if (reg < NUM_SREGS)
5022 return SIM_ARM_FPS_REGNUM + reg;
5023 reg -= NUM_SREGS;
5024
edefbb7c 5025 internal_error (__FILE__, __LINE__, _("Bad REGNUM %d"), regnum);
26216b98 5026}
34e8f22d 5027
a01567f4
LM
5028static const unsigned char op_lit0 = DW_OP_lit0;
5029
5030static void
5031arm_dwarf2_frame_init_reg (struct gdbarch *gdbarch, int regnum,
5032 struct dwarf2_frame_state_reg *reg,
5033 struct frame_info *this_frame)
5034{
a6e4a48c
YR
5035 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
5036
a01567f4
LM
5037 if (is_pacbti_pseudo (gdbarch, regnum))
5038 {
5039 /* Initialize RA_AUTH_CODE to zero. */
5040 reg->how = DWARF2_FRAME_REG_SAVED_VAL_EXP;
5041 reg->loc.exp.start = &op_lit0;
5042 reg->loc.exp.len = 1;
5043 return;
5044 }
5045
42e11f36 5046 if (regnum == ARM_PC_REGNUM || regnum == ARM_PS_REGNUM)
a01567f4 5047 {
a01567f4
LM
5048 reg->how = DWARF2_FRAME_REG_FN;
5049 reg->loc.fn = arm_dwarf2_prev_register;
a01567f4 5050 }
42e11f36
TS
5051 else if (regnum == ARM_SP_REGNUM)
5052 reg->how = DWARF2_FRAME_REG_CFA;
a6e4a48c
YR
5053 else if (arm_is_alternative_sp_register (tdep, regnum))
5054 {
5055 /* Handle the alternative SP registers on Cortex-M. */
5056 reg->how = DWARF2_FRAME_REG_FN;
5057 reg->loc.fn = arm_dwarf2_prev_register;
5058 }
a01567f4
LM
5059}
5060
d9311bfa
AT
5061/* Given BUF, which is OLD_LEN bytes ending at ENDADDR, expand
5062 the buffer to be NEW_LEN bytes ending at ENDADDR. Return
5063 NULL if an error occurs. BUF is freed. */
c906108c 5064
d9311bfa
AT
5065static gdb_byte *
5066extend_buffer_earlier (gdb_byte *buf, CORE_ADDR endaddr,
5067 int old_len, int new_len)
5068{
5069 gdb_byte *new_buf;
5070 int bytes_to_read = new_len - old_len;
c906108c 5071
d9311bfa
AT
5072 new_buf = (gdb_byte *) xmalloc (new_len);
5073 memcpy (new_buf + bytes_to_read, buf, old_len);
5074 xfree (buf);
198cd59d 5075 if (target_read_code (endaddr - new_len, new_buf, bytes_to_read) != 0)
d9311bfa
AT
5076 {
5077 xfree (new_buf);
5078 return NULL;
c906108c 5079 }
d9311bfa 5080 return new_buf;
c906108c
SS
5081}
5082
d9311bfa
AT
5083/* An IT block is at most the 2-byte IT instruction followed by
5084 four 4-byte instructions. The furthest back we must search to
5085 find an IT block that affects the current instruction is thus
5086 2 + 3 * 4 == 14 bytes. */
5087#define MAX_IT_BLOCK_PREFIX 14
177321bd 5088
d9311bfa
AT
5089/* Use a quick scan if there are more than this many bytes of
5090 code. */
5091#define IT_SCAN_THRESHOLD 32
177321bd 5092
d9311bfa
AT
5093/* Adjust a breakpoint's address to move breakpoints out of IT blocks.
5094 A breakpoint in an IT block may not be hit, depending on the
5095 condition flags. */
ad527d2e 5096static CORE_ADDR
d9311bfa 5097arm_adjust_breakpoint_address (struct gdbarch *gdbarch, CORE_ADDR bpaddr)
c906108c 5098{
d9311bfa
AT
5099 gdb_byte *buf;
5100 char map_type;
5101 CORE_ADDR boundary, func_start;
5102 int buf_len;
5103 enum bfd_endian order = gdbarch_byte_order_for_code (gdbarch);
5104 int i, any, last_it, last_it_count;
08106042 5105 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
177321bd 5106
d9311bfa 5107 /* If we are using BKPT breakpoints, none of this is necessary. */
345bd07c 5108 if (tdep->thumb2_breakpoint == NULL)
d9311bfa 5109 return bpaddr;
177321bd 5110
d9311bfa
AT
5111 /* ARM mode does not have this problem. */
5112 if (!arm_pc_is_thumb (gdbarch, bpaddr))
5113 return bpaddr;
177321bd 5114
d9311bfa
AT
5115 /* We are setting a breakpoint in Thumb code that could potentially
5116 contain an IT block. The first step is to find how much Thumb
5117 code there is; we do not need to read outside of known Thumb
5118 sequences. */
5119 map_type = arm_find_mapping_symbol (bpaddr, &boundary);
5120 if (map_type == 0)
5121 /* Thumb-2 code must have mapping symbols to have a chance. */
5122 return bpaddr;
9dca5578 5123
d9311bfa 5124 bpaddr = gdbarch_addr_bits_remove (gdbarch, bpaddr);
177321bd 5125
d9311bfa
AT
5126 if (find_pc_partial_function (bpaddr, NULL, &func_start, NULL)
5127 && func_start > boundary)
5128 boundary = func_start;
9dca5578 5129
d9311bfa
AT
5130 /* Search for a candidate IT instruction. We have to do some fancy
5131 footwork to distinguish a real IT instruction from the second
5132 half of a 32-bit instruction, but there is no need for that if
5133 there's no candidate. */
325fac50 5134 buf_len = std::min (bpaddr - boundary, (CORE_ADDR) MAX_IT_BLOCK_PREFIX);
d9311bfa
AT
5135 if (buf_len == 0)
5136 /* No room for an IT instruction. */
5137 return bpaddr;
c906108c 5138
d9311bfa 5139 buf = (gdb_byte *) xmalloc (buf_len);
198cd59d 5140 if (target_read_code (bpaddr - buf_len, buf, buf_len) != 0)
d9311bfa
AT
5141 return bpaddr;
5142 any = 0;
5143 for (i = 0; i < buf_len; i += 2)
c906108c 5144 {
d9311bfa
AT
5145 unsigned short inst1 = extract_unsigned_integer (&buf[i], 2, order);
5146 if ((inst1 & 0xff00) == 0xbf00 && (inst1 & 0x000f) != 0)
25b41d01 5147 {
d9311bfa
AT
5148 any = 1;
5149 break;
25b41d01 5150 }
c906108c 5151 }
d9311bfa
AT
5152
5153 if (any == 0)
c906108c 5154 {
d9311bfa
AT
5155 xfree (buf);
5156 return bpaddr;
f9d67f43
DJ
5157 }
5158
5159 /* OK, the code bytes before this instruction contain at least one
5160 halfword which resembles an IT instruction. We know that it's
5161 Thumb code, but there are still two possibilities. Either the
5162 halfword really is an IT instruction, or it is the second half of
5163 a 32-bit Thumb instruction. The only way we can tell is to
5164 scan forwards from a known instruction boundary. */
5165 if (bpaddr - boundary > IT_SCAN_THRESHOLD)
5166 {
5167 int definite;
5168
5169 /* There's a lot of code before this instruction. Start with an
5170 optimistic search; it's easy to recognize halfwords that can
5171 not be the start of a 32-bit instruction, and use that to
5172 lock on to the instruction boundaries. */
5173 buf = extend_buffer_earlier (buf, bpaddr, buf_len, IT_SCAN_THRESHOLD);
5174 if (buf == NULL)
5175 return bpaddr;
5176 buf_len = IT_SCAN_THRESHOLD;
5177
5178 definite = 0;
5179 for (i = 0; i < buf_len - sizeof (buf) && ! definite; i += 2)
5180 {
5181 unsigned short inst1 = extract_unsigned_integer (&buf[i], 2, order);
5182 if (thumb_insn_size (inst1) == 2)
5183 {
5184 definite = 1;
5185 break;
5186 }
5187 }
5188
5189 /* At this point, if DEFINITE, BUF[I] is the first place we
5190 are sure that we know the instruction boundaries, and it is far
5191 enough from BPADDR that we could not miss an IT instruction
5192 affecting BPADDR. If ! DEFINITE, give up - start from a
5193 known boundary. */
5194 if (! definite)
5195 {
0963b4bd
MS
5196 buf = extend_buffer_earlier (buf, bpaddr, buf_len,
5197 bpaddr - boundary);
f9d67f43
DJ
5198 if (buf == NULL)
5199 return bpaddr;
5200 buf_len = bpaddr - boundary;
5201 i = 0;
5202 }
5203 }
5204 else
5205 {
5206 buf = extend_buffer_earlier (buf, bpaddr, buf_len, bpaddr - boundary);
5207 if (buf == NULL)
5208 return bpaddr;
5209 buf_len = bpaddr - boundary;
5210 i = 0;
5211 }
5212
5213 /* Scan forwards. Find the last IT instruction before BPADDR. */
5214 last_it = -1;
5215 last_it_count = 0;
5216 while (i < buf_len)
5217 {
5218 unsigned short inst1 = extract_unsigned_integer (&buf[i], 2, order);
5219 last_it_count--;
5220 if ((inst1 & 0xff00) == 0xbf00 && (inst1 & 0x000f) != 0)
5221 {
5222 last_it = i;
5223 if (inst1 & 0x0001)
5224 last_it_count = 4;
5225 else if (inst1 & 0x0002)
5226 last_it_count = 3;
5227 else if (inst1 & 0x0004)
5228 last_it_count = 2;
5229 else
5230 last_it_count = 1;
5231 }
5232 i += thumb_insn_size (inst1);
5233 }
5234
5235 xfree (buf);
5236
5237 if (last_it == -1)
5238 /* There wasn't really an IT instruction after all. */
5239 return bpaddr;
5240
5241 if (last_it_count < 1)
5242 /* It was too far away. */
5243 return bpaddr;
5244
5245 /* This really is a trouble spot. Move the breakpoint to the IT
5246 instruction. */
5247 return bpaddr - buf_len + last_it;
5248}
5249
cca44b1b 5250/* ARM displaced stepping support.
c906108c 5251
cca44b1b 5252 Generally ARM displaced stepping works as follows:
c906108c 5253
cca44b1b 5254 1. When an instruction is to be single-stepped, it is first decoded by
2ba163c8
SM
5255 arm_process_displaced_insn. Depending on the type of instruction, it is
5256 then copied to a scratch location, possibly in a modified form. The
5257 copy_* set of functions performs such modification, as necessary. A
5258 breakpoint is placed after the modified instruction in the scratch space
5259 to return control to GDB. Note in particular that instructions which
5260 modify the PC will no longer do so after modification.
c5aa993b 5261
cca44b1b
JB
5262 2. The instruction is single-stepped, by setting the PC to the scratch
5263 location address, and resuming. Control returns to GDB when the
5264 breakpoint is hit.
c5aa993b 5265
cca44b1b
JB
5266 3. A cleanup function (cleanup_*) is called corresponding to the copy_*
5267 function used for the current instruction. This function's job is to
5268 put the CPU/memory state back to what it would have been if the
5269 instruction had been executed unmodified in its original location. */
c5aa993b 5270
cca44b1b
JB
5271/* NOP instruction (mov r0, r0). */
5272#define ARM_NOP 0xe1a00000
34518530 5273#define THUMB_NOP 0x4600
cca44b1b
JB
5274
5275/* Helper for register reads for displaced stepping. In particular, this
5276 returns the PC as it would be seen by the instruction at its original
5277 location. */
5278
5279ULONGEST
1152d984 5280displaced_read_reg (regcache *regs, arm_displaced_step_copy_insn_closure *dsc,
36073a92 5281 int regno)
cca44b1b
JB
5282{
5283 ULONGEST ret;
36073a92 5284 CORE_ADDR from = dsc->insn_addr;
cca44b1b 5285
bf9f652a 5286 if (regno == ARM_PC_REGNUM)
cca44b1b 5287 {
4db71c0b
YQ
5288 /* Compute pipeline offset:
5289 - When executing an ARM instruction, PC reads as the address of the
5290 current instruction plus 8.
5291 - When executing a Thumb instruction, PC reads as the address of the
5292 current instruction plus 4. */
5293
36073a92 5294 if (!dsc->is_thumb)
4db71c0b
YQ
5295 from += 8;
5296 else
5297 from += 4;
5298
136821d9
SM
5299 displaced_debug_printf ("read pc value %.8lx",
5300 (unsigned long) from);
4db71c0b 5301 return (ULONGEST) from;
cca44b1b 5302 }
c906108c 5303 else
cca44b1b
JB
5304 {
5305 regcache_cooked_read_unsigned (regs, regno, &ret);
136821d9
SM
5306
5307 displaced_debug_printf ("read r%d value %.8lx",
5308 regno, (unsigned long) ret);
5309
cca44b1b
JB
5310 return ret;
5311 }
c906108c
SS
5312}
5313
cca44b1b
JB
5314static int
5315displaced_in_arm_mode (struct regcache *regs)
5316{
5317 ULONGEST ps;
ac7936df 5318 ULONGEST t_bit = arm_psr_thumb_bit (regs->arch ());
66e810cd 5319
cca44b1b 5320 regcache_cooked_read_unsigned (regs, ARM_PS_REGNUM, &ps);
66e810cd 5321
9779414d 5322 return (ps & t_bit) == 0;
cca44b1b 5323}
66e810cd 5324
cca44b1b 5325/* Write to the PC as from a branch instruction. */
c906108c 5326
cca44b1b 5327static void
1152d984 5328branch_write_pc (regcache *regs, arm_displaced_step_copy_insn_closure *dsc,
36073a92 5329 ULONGEST val)
c906108c 5330{
36073a92 5331 if (!dsc->is_thumb)
cca44b1b
JB
5332 /* Note: If bits 0/1 are set, this branch would be unpredictable for
5333 architecture versions < 6. */
0963b4bd
MS
5334 regcache_cooked_write_unsigned (regs, ARM_PC_REGNUM,
5335 val & ~(ULONGEST) 0x3);
cca44b1b 5336 else
0963b4bd
MS
5337 regcache_cooked_write_unsigned (regs, ARM_PC_REGNUM,
5338 val & ~(ULONGEST) 0x1);
cca44b1b 5339}
66e810cd 5340
cca44b1b
JB
5341/* Write to the PC as from a branch-exchange instruction. */
5342
5343static void
5344bx_write_pc (struct regcache *regs, ULONGEST val)
5345{
5346 ULONGEST ps;
ac7936df 5347 ULONGEST t_bit = arm_psr_thumb_bit (regs->arch ());
cca44b1b
JB
5348
5349 regcache_cooked_read_unsigned (regs, ARM_PS_REGNUM, &ps);
5350
5351 if ((val & 1) == 1)
c906108c 5352 {
9779414d 5353 regcache_cooked_write_unsigned (regs, ARM_PS_REGNUM, ps | t_bit);
cca44b1b
JB
5354 regcache_cooked_write_unsigned (regs, ARM_PC_REGNUM, val & 0xfffffffe);
5355 }
5356 else if ((val & 2) == 0)
5357 {
9779414d 5358 regcache_cooked_write_unsigned (regs, ARM_PS_REGNUM, ps & ~t_bit);
cca44b1b 5359 regcache_cooked_write_unsigned (regs, ARM_PC_REGNUM, val);
c906108c
SS
5360 }
5361 else
5362 {
cca44b1b
JB
5363 /* Unpredictable behaviour. Try to do something sensible (switch to ARM
5364 mode, align dest to 4 bytes). */
5365 warning (_("Single-stepping BX to non-word-aligned ARM instruction."));
9779414d 5366 regcache_cooked_write_unsigned (regs, ARM_PS_REGNUM, ps & ~t_bit);
cca44b1b 5367 regcache_cooked_write_unsigned (regs, ARM_PC_REGNUM, val & 0xfffffffc);
c906108c
SS
5368 }
5369}
ed9a39eb 5370
cca44b1b 5371/* Write to the PC as if from a load instruction. */
ed9a39eb 5372
34e8f22d 5373static void
1152d984 5374load_write_pc (regcache *regs, arm_displaced_step_copy_insn_closure *dsc,
36073a92 5375 ULONGEST val)
ed9a39eb 5376{
cca44b1b
JB
5377 if (DISPLACED_STEPPING_ARCH_VERSION >= 5)
5378 bx_write_pc (regs, val);
5379 else
36073a92 5380 branch_write_pc (regs, dsc, val);
cca44b1b 5381}
be8626e0 5382
cca44b1b
JB
5383/* Write to the PC as if from an ALU instruction. */
5384
5385static void
1152d984 5386alu_write_pc (regcache *regs, arm_displaced_step_copy_insn_closure *dsc,
36073a92 5387 ULONGEST val)
cca44b1b 5388{
36073a92 5389 if (DISPLACED_STEPPING_ARCH_VERSION >= 7 && !dsc->is_thumb)
cca44b1b
JB
5390 bx_write_pc (regs, val);
5391 else
36073a92 5392 branch_write_pc (regs, dsc, val);
cca44b1b
JB
5393}
5394
5395/* Helper for writing to registers for displaced stepping. Writing to the PC
5396 has a varying effects depending on the instruction which does the write:
5397 this is controlled by the WRITE_PC argument. */
5398
5399void
1152d984 5400displaced_write_reg (regcache *regs, arm_displaced_step_copy_insn_closure *dsc,
cca44b1b
JB
5401 int regno, ULONGEST val, enum pc_write_style write_pc)
5402{
bf9f652a 5403 if (regno == ARM_PC_REGNUM)
08216dd7 5404 {
136821d9
SM
5405 displaced_debug_printf ("writing pc %.8lx", (unsigned long) val);
5406
cca44b1b 5407 switch (write_pc)
08216dd7 5408 {
cca44b1b 5409 case BRANCH_WRITE_PC:
36073a92 5410 branch_write_pc (regs, dsc, val);
08216dd7
RE
5411 break;
5412
cca44b1b
JB
5413 case BX_WRITE_PC:
5414 bx_write_pc (regs, val);
24b21115 5415 break;
cca44b1b
JB
5416
5417 case LOAD_WRITE_PC:
36073a92 5418 load_write_pc (regs, dsc, val);
24b21115 5419 break;
cca44b1b
JB
5420
5421 case ALU_WRITE_PC:
36073a92 5422 alu_write_pc (regs, dsc, val);
24b21115 5423 break;
cca44b1b
JB
5424
5425 case CANNOT_WRITE_PC:
5426 warning (_("Instruction wrote to PC in an unexpected way when "
5427 "single-stepping"));
08216dd7
RE
5428 break;
5429
5430 default:
97b9747c
JB
5431 internal_error (__FILE__, __LINE__,
5432 _("Invalid argument to displaced_write_reg"));
08216dd7 5433 }
b508a996 5434
cca44b1b 5435 dsc->wrote_to_pc = 1;
b508a996 5436 }
ed9a39eb 5437 else
b508a996 5438 {
136821d9
SM
5439 displaced_debug_printf ("writing r%d value %.8lx",
5440 regno, (unsigned long) val);
cca44b1b 5441 regcache_cooked_write_unsigned (regs, regno, val);
b508a996 5442 }
34e8f22d
RE
5443}
5444
cca44b1b
JB
5445/* This function is used to concisely determine if an instruction INSN
5446 references PC. Register fields of interest in INSN should have the
0963b4bd
MS
5447 corresponding fields of BITMASK set to 0b1111. The function
5448 returns return 1 if any of these fields in INSN reference the PC
5449 (also 0b1111, r15), else it returns 0. */
67255d04
RE
5450
5451static int
cca44b1b 5452insn_references_pc (uint32_t insn, uint32_t bitmask)
67255d04 5453{
cca44b1b 5454 uint32_t lowbit = 1;
67255d04 5455
cca44b1b
JB
5456 while (bitmask != 0)
5457 {
5458 uint32_t mask;
44e1a9eb 5459
cca44b1b
JB
5460 for (; lowbit && (bitmask & lowbit) == 0; lowbit <<= 1)
5461 ;
67255d04 5462
cca44b1b
JB
5463 if (!lowbit)
5464 break;
67255d04 5465
cca44b1b 5466 mask = lowbit * 0xf;
67255d04 5467
cca44b1b
JB
5468 if ((insn & mask) == mask)
5469 return 1;
5470
5471 bitmask &= ~mask;
67255d04
RE
5472 }
5473
cca44b1b
JB
5474 return 0;
5475}
2af48f68 5476
cca44b1b
JB
5477/* The simplest copy function. Many instructions have the same effect no
5478 matter what address they are executed at: in those cases, use this. */
67255d04 5479
cca44b1b 5480static int
1152d984
SM
5481arm_copy_unmodified (struct gdbarch *gdbarch, uint32_t insn, const char *iname,
5482 arm_displaced_step_copy_insn_closure *dsc)
cca44b1b 5483{
136821d9
SM
5484 displaced_debug_printf ("copying insn %.8lx, opcode/class '%s' unmodified",
5485 (unsigned long) insn, iname);
67255d04 5486
cca44b1b 5487 dsc->modinsn[0] = insn;
67255d04 5488
cca44b1b
JB
5489 return 0;
5490}
5491
34518530
YQ
5492static int
5493thumb_copy_unmodified_32bit (struct gdbarch *gdbarch, uint16_t insn1,
5494 uint16_t insn2, const char *iname,
1152d984 5495 arm_displaced_step_copy_insn_closure *dsc)
34518530 5496{
136821d9
SM
5497 displaced_debug_printf ("copying insn %.4x %.4x, opcode/class '%s' "
5498 "unmodified", insn1, insn2, iname);
34518530
YQ
5499
5500 dsc->modinsn[0] = insn1;
5501 dsc->modinsn[1] = insn2;
5502 dsc->numinsns = 2;
5503
5504 return 0;
5505}
5506
5507/* Copy 16-bit Thumb(Thumb and 16-bit Thumb-2) instruction without any
5508 modification. */
5509static int
615234c1 5510thumb_copy_unmodified_16bit (struct gdbarch *gdbarch, uint16_t insn,
34518530 5511 const char *iname,
1152d984 5512 arm_displaced_step_copy_insn_closure *dsc)
34518530 5513{
136821d9
SM
5514 displaced_debug_printf ("copying insn %.4x, opcode/class '%s' unmodified",
5515 insn, iname);
34518530
YQ
5516
5517 dsc->modinsn[0] = insn;
5518
5519 return 0;
5520}
5521
cca44b1b
JB
5522/* Preload instructions with immediate offset. */
5523
5524static void
1152d984
SM
5525cleanup_preload (struct gdbarch *gdbarch, regcache *regs,
5526 arm_displaced_step_copy_insn_closure *dsc)
cca44b1b
JB
5527{
5528 displaced_write_reg (regs, dsc, 0, dsc->tmp[0], CANNOT_WRITE_PC);
5529 if (!dsc->u.preload.immed)
5530 displaced_write_reg (regs, dsc, 1, dsc->tmp[1], CANNOT_WRITE_PC);
5531}
5532
7ff120b4
YQ
5533static void
5534install_preload (struct gdbarch *gdbarch, struct regcache *regs,
1152d984 5535 arm_displaced_step_copy_insn_closure *dsc, unsigned int rn)
cca44b1b 5536{
cca44b1b 5537 ULONGEST rn_val;
cca44b1b
JB
5538 /* Preload instructions:
5539
5540 {pli/pld} [rn, #+/-imm]
5541 ->
5542 {pli/pld} [r0, #+/-imm]. */
5543
36073a92
YQ
5544 dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
5545 rn_val = displaced_read_reg (regs, dsc, rn);
cca44b1b 5546 displaced_write_reg (regs, dsc, 0, rn_val, CANNOT_WRITE_PC);
cca44b1b
JB
5547 dsc->u.preload.immed = 1;
5548
cca44b1b 5549 dsc->cleanup = &cleanup_preload;
cca44b1b
JB
5550}
5551
cca44b1b 5552static int
7ff120b4 5553arm_copy_preload (struct gdbarch *gdbarch, uint32_t insn, struct regcache *regs,
1152d984 5554 arm_displaced_step_copy_insn_closure *dsc)
cca44b1b
JB
5555{
5556 unsigned int rn = bits (insn, 16, 19);
cca44b1b 5557
7ff120b4
YQ
5558 if (!insn_references_pc (insn, 0x000f0000ul))
5559 return arm_copy_unmodified (gdbarch, insn, "preload", dsc);
cca44b1b 5560
136821d9 5561 displaced_debug_printf ("copying preload insn %.8lx", (unsigned long) insn);
cca44b1b 5562
7ff120b4
YQ
5563 dsc->modinsn[0] = insn & 0xfff0ffff;
5564
5565 install_preload (gdbarch, regs, dsc, rn);
5566
5567 return 0;
5568}
5569
34518530
YQ
5570static int
5571thumb2_copy_preload (struct gdbarch *gdbarch, uint16_t insn1, uint16_t insn2,
1152d984 5572 regcache *regs, arm_displaced_step_copy_insn_closure *dsc)
34518530
YQ
5573{
5574 unsigned int rn = bits (insn1, 0, 3);
5575 unsigned int u_bit = bit (insn1, 7);
5576 int imm12 = bits (insn2, 0, 11);
5577 ULONGEST pc_val;
5578
5579 if (rn != ARM_PC_REGNUM)
5580 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2, "preload", dsc);
5581
5582 /* PC is only allowed to use in PLI (immediate,literal) Encoding T3, and
5583 PLD (literal) Encoding T1. */
136821d9
SM
5584 displaced_debug_printf ("copying pld/pli pc (0x%x) %c imm12 %.4x",
5585 (unsigned int) dsc->insn_addr, u_bit ? '+' : '-',
5586 imm12);
34518530
YQ
5587
5588 if (!u_bit)
5589 imm12 = -1 * imm12;
5590
5591 /* Rewrite instruction {pli/pld} PC imm12 into:
5592 Prepare: tmp[0] <- r0, tmp[1] <- r1, r0 <- pc, r1 <- imm12
5593
5594 {pli/pld} [r0, r1]
5595
5596 Cleanup: r0 <- tmp[0], r1 <- tmp[1]. */
5597
5598 dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
5599 dsc->tmp[1] = displaced_read_reg (regs, dsc, 1);
5600
5601 pc_val = displaced_read_reg (regs, dsc, ARM_PC_REGNUM);
5602
5603 displaced_write_reg (regs, dsc, 0, pc_val, CANNOT_WRITE_PC);
5604 displaced_write_reg (regs, dsc, 1, imm12, CANNOT_WRITE_PC);
5605 dsc->u.preload.immed = 0;
5606
5607 /* {pli/pld} [r0, r1] */
5608 dsc->modinsn[0] = insn1 & 0xfff0;
5609 dsc->modinsn[1] = 0xf001;
5610 dsc->numinsns = 2;
5611
5612 dsc->cleanup = &cleanup_preload;
5613 return 0;
5614}
5615
7ff120b4
YQ
5616/* Preload instructions with register offset. */
5617
5618static void
5619install_preload_reg(struct gdbarch *gdbarch, struct regcache *regs,
1152d984 5620 arm_displaced_step_copy_insn_closure *dsc, unsigned int rn,
7ff120b4
YQ
5621 unsigned int rm)
5622{
5623 ULONGEST rn_val, rm_val;
5624
cca44b1b
JB
5625 /* Preload register-offset instructions:
5626
5627 {pli/pld} [rn, rm {, shift}]
5628 ->
5629 {pli/pld} [r0, r1 {, shift}]. */
5630
36073a92
YQ
5631 dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
5632 dsc->tmp[1] = displaced_read_reg (regs, dsc, 1);
5633 rn_val = displaced_read_reg (regs, dsc, rn);
5634 rm_val = displaced_read_reg (regs, dsc, rm);
cca44b1b
JB
5635 displaced_write_reg (regs, dsc, 0, rn_val, CANNOT_WRITE_PC);
5636 displaced_write_reg (regs, dsc, 1, rm_val, CANNOT_WRITE_PC);
cca44b1b
JB
5637 dsc->u.preload.immed = 0;
5638
cca44b1b 5639 dsc->cleanup = &cleanup_preload;
7ff120b4
YQ
5640}
5641
5642static int
5643arm_copy_preload_reg (struct gdbarch *gdbarch, uint32_t insn,
5644 struct regcache *regs,
1152d984 5645 arm_displaced_step_copy_insn_closure *dsc)
7ff120b4
YQ
5646{
5647 unsigned int rn = bits (insn, 16, 19);
5648 unsigned int rm = bits (insn, 0, 3);
5649
5650
5651 if (!insn_references_pc (insn, 0x000f000ful))
5652 return arm_copy_unmodified (gdbarch, insn, "preload reg", dsc);
5653
136821d9
SM
5654 displaced_debug_printf ("copying preload insn %.8lx",
5655 (unsigned long) insn);
7ff120b4
YQ
5656
5657 dsc->modinsn[0] = (insn & 0xfff0fff0) | 0x1;
cca44b1b 5658
7ff120b4 5659 install_preload_reg (gdbarch, regs, dsc, rn, rm);
cca44b1b
JB
5660 return 0;
5661}
5662
5663/* Copy/cleanup coprocessor load and store instructions. */
5664
5665static void
6e39997a 5666cleanup_copro_load_store (struct gdbarch *gdbarch,
cca44b1b 5667 struct regcache *regs,
1152d984 5668 arm_displaced_step_copy_insn_closure *dsc)
cca44b1b 5669{
36073a92 5670 ULONGEST rn_val = displaced_read_reg (regs, dsc, 0);
cca44b1b
JB
5671
5672 displaced_write_reg (regs, dsc, 0, dsc->tmp[0], CANNOT_WRITE_PC);
5673
5674 if (dsc->u.ldst.writeback)
5675 displaced_write_reg (regs, dsc, dsc->u.ldst.rn, rn_val, LOAD_WRITE_PC);
5676}
5677
7ff120b4
YQ
5678static void
5679install_copro_load_store (struct gdbarch *gdbarch, struct regcache *regs,
1152d984 5680 arm_displaced_step_copy_insn_closure *dsc,
7ff120b4 5681 int writeback, unsigned int rn)
cca44b1b 5682{
cca44b1b 5683 ULONGEST rn_val;
cca44b1b 5684
cca44b1b
JB
5685 /* Coprocessor load/store instructions:
5686
5687 {stc/stc2} [<Rn>, #+/-imm] (and other immediate addressing modes)
5688 ->
5689 {stc/stc2} [r0, #+/-imm].
5690
5691 ldc/ldc2 are handled identically. */
5692
36073a92
YQ
5693 dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
5694 rn_val = displaced_read_reg (regs, dsc, rn);
2b16b2e3
YQ
5695 /* PC should be 4-byte aligned. */
5696 rn_val = rn_val & 0xfffffffc;
cca44b1b
JB
5697 displaced_write_reg (regs, dsc, 0, rn_val, CANNOT_WRITE_PC);
5698
7ff120b4 5699 dsc->u.ldst.writeback = writeback;
cca44b1b
JB
5700 dsc->u.ldst.rn = rn;
5701
7ff120b4
YQ
5702 dsc->cleanup = &cleanup_copro_load_store;
5703}
5704
5705static int
5706arm_copy_copro_load_store (struct gdbarch *gdbarch, uint32_t insn,
5707 struct regcache *regs,
1152d984 5708 arm_displaced_step_copy_insn_closure *dsc)
7ff120b4
YQ
5709{
5710 unsigned int rn = bits (insn, 16, 19);
5711
5712 if (!insn_references_pc (insn, 0x000f0000ul))
5713 return arm_copy_unmodified (gdbarch, insn, "copro load/store", dsc);
5714
136821d9
SM
5715 displaced_debug_printf ("copying coprocessor load/store insn %.8lx",
5716 (unsigned long) insn);
7ff120b4 5717
cca44b1b
JB
5718 dsc->modinsn[0] = insn & 0xfff0ffff;
5719
7ff120b4 5720 install_copro_load_store (gdbarch, regs, dsc, bit (insn, 25), rn);
cca44b1b
JB
5721
5722 return 0;
5723}
5724
34518530
YQ
5725static int
5726thumb2_copy_copro_load_store (struct gdbarch *gdbarch, uint16_t insn1,
5727 uint16_t insn2, struct regcache *regs,
1152d984 5728 arm_displaced_step_copy_insn_closure *dsc)
34518530
YQ
5729{
5730 unsigned int rn = bits (insn1, 0, 3);
5731
5732 if (rn != ARM_PC_REGNUM)
5733 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
5734 "copro load/store", dsc);
5735
136821d9
SM
5736 displaced_debug_printf ("copying coprocessor load/store insn %.4x%.4x",
5737 insn1, insn2);
34518530
YQ
5738
5739 dsc->modinsn[0] = insn1 & 0xfff0;
5740 dsc->modinsn[1] = insn2;
5741 dsc->numinsns = 2;
5742
5743 /* This function is called for copying instruction LDC/LDC2/VLDR, which
5744 doesn't support writeback, so pass 0. */
5745 install_copro_load_store (gdbarch, regs, dsc, 0, rn);
5746
5747 return 0;
5748}
5749
cca44b1b
JB
5750/* Clean up branch instructions (actually perform the branch, by setting
5751 PC). */
5752
5753static void
6e39997a 5754cleanup_branch (struct gdbarch *gdbarch, struct regcache *regs,
1152d984 5755 arm_displaced_step_copy_insn_closure *dsc)
cca44b1b 5756{
36073a92 5757 uint32_t status = displaced_read_reg (regs, dsc, ARM_PS_REGNUM);
cca44b1b
JB
5758 int branch_taken = condition_true (dsc->u.branch.cond, status);
5759 enum pc_write_style write_pc = dsc->u.branch.exchange
5760 ? BX_WRITE_PC : BRANCH_WRITE_PC;
5761
5762 if (!branch_taken)
5763 return;
5764
5765 if (dsc->u.branch.link)
5766 {
8c8dba6d 5767 /* The value of LR should be the next insn of current one. In order
85102364 5768 not to confuse logic handling later insn `bx lr', if current insn mode
8c8dba6d
YQ
5769 is Thumb, the bit 0 of LR value should be set to 1. */
5770 ULONGEST next_insn_addr = dsc->insn_addr + dsc->insn_size;
5771
5772 if (dsc->is_thumb)
5773 next_insn_addr |= 0x1;
5774
5775 displaced_write_reg (regs, dsc, ARM_LR_REGNUM, next_insn_addr,
5776 CANNOT_WRITE_PC);
cca44b1b
JB
5777 }
5778
bf9f652a 5779 displaced_write_reg (regs, dsc, ARM_PC_REGNUM, dsc->u.branch.dest, write_pc);
cca44b1b
JB
5780}
5781
5782/* Copy B/BL/BLX instructions with immediate destinations. */
5783
7ff120b4
YQ
5784static void
5785install_b_bl_blx (struct gdbarch *gdbarch, struct regcache *regs,
1152d984 5786 arm_displaced_step_copy_insn_closure *dsc,
7ff120b4
YQ
5787 unsigned int cond, int exchange, int link, long offset)
5788{
5789 /* Implement "BL<cond> <label>" as:
5790
5791 Preparation: cond <- instruction condition
5792 Insn: mov r0, r0 (nop)
5793 Cleanup: if (condition true) { r14 <- pc; pc <- label }.
5794
5795 B<cond> similar, but don't set r14 in cleanup. */
5796
5797 dsc->u.branch.cond = cond;
5798 dsc->u.branch.link = link;
5799 dsc->u.branch.exchange = exchange;
5800
2b16b2e3
YQ
5801 dsc->u.branch.dest = dsc->insn_addr;
5802 if (link && exchange)
5803 /* For BLX, offset is computed from the Align (PC, 4). */
5804 dsc->u.branch.dest = dsc->u.branch.dest & 0xfffffffc;
5805
7ff120b4 5806 if (dsc->is_thumb)
2b16b2e3 5807 dsc->u.branch.dest += 4 + offset;
7ff120b4 5808 else
2b16b2e3 5809 dsc->u.branch.dest += 8 + offset;
7ff120b4
YQ
5810
5811 dsc->cleanup = &cleanup_branch;
5812}
cca44b1b 5813static int
7ff120b4 5814arm_copy_b_bl_blx (struct gdbarch *gdbarch, uint32_t insn,
1152d984 5815 regcache *regs, arm_displaced_step_copy_insn_closure *dsc)
cca44b1b
JB
5816{
5817 unsigned int cond = bits (insn, 28, 31);
5818 int exchange = (cond == 0xf);
5819 int link = exchange || bit (insn, 24);
cca44b1b
JB
5820 long offset;
5821
136821d9
SM
5822 displaced_debug_printf ("copying %s immediate insn %.8lx",
5823 (exchange) ? "blx" : (link) ? "bl" : "b",
5824 (unsigned long) insn);
cca44b1b
JB
5825 if (exchange)
5826 /* For BLX, set bit 0 of the destination. The cleanup_branch function will
5827 then arrange the switch into Thumb mode. */
5828 offset = (bits (insn, 0, 23) << 2) | (bit (insn, 24) << 1) | 1;
5829 else
5830 offset = bits (insn, 0, 23) << 2;
5831
5832 if (bit (offset, 25))
5833 offset = offset | ~0x3ffffff;
5834
cca44b1b
JB
5835 dsc->modinsn[0] = ARM_NOP;
5836
7ff120b4 5837 install_b_bl_blx (gdbarch, regs, dsc, cond, exchange, link, offset);
cca44b1b
JB
5838 return 0;
5839}
5840
34518530
YQ
5841static int
5842thumb2_copy_b_bl_blx (struct gdbarch *gdbarch, uint16_t insn1,
5843 uint16_t insn2, struct regcache *regs,
1152d984 5844 arm_displaced_step_copy_insn_closure *dsc)
34518530
YQ
5845{
5846 int link = bit (insn2, 14);
5847 int exchange = link && !bit (insn2, 12);
5848 int cond = INST_AL;
5849 long offset = 0;
5850 int j1 = bit (insn2, 13);
5851 int j2 = bit (insn2, 11);
5852 int s = sbits (insn1, 10, 10);
5853 int i1 = !(j1 ^ bit (insn1, 10));
5854 int i2 = !(j2 ^ bit (insn1, 10));
5855
5856 if (!link && !exchange) /* B */
5857 {
5858 offset = (bits (insn2, 0, 10) << 1);
5859 if (bit (insn2, 12)) /* Encoding T4 */
5860 {
5861 offset |= (bits (insn1, 0, 9) << 12)
5862 | (i2 << 22)
5863 | (i1 << 23)
5864 | (s << 24);
5865 cond = INST_AL;
5866 }
5867 else /* Encoding T3 */
5868 {
5869 offset |= (bits (insn1, 0, 5) << 12)
5870 | (j1 << 18)
5871 | (j2 << 19)
5872 | (s << 20);
5873 cond = bits (insn1, 6, 9);
5874 }
5875 }
5876 else
5877 {
5878 offset = (bits (insn1, 0, 9) << 12);
5879 offset |= ((i2 << 22) | (i1 << 23) | (s << 24));
5880 offset |= exchange ?
5881 (bits (insn2, 1, 10) << 2) : (bits (insn2, 0, 10) << 1);
5882 }
5883
136821d9
SM
5884 displaced_debug_printf ("copying %s insn %.4x %.4x with offset %.8lx",
5885 link ? (exchange) ? "blx" : "bl" : "b",
5886 insn1, insn2, offset);
34518530
YQ
5887
5888 dsc->modinsn[0] = THUMB_NOP;
5889
5890 install_b_bl_blx (gdbarch, regs, dsc, cond, exchange, link, offset);
5891 return 0;
5892}
5893
5894/* Copy B Thumb instructions. */
5895static int
615234c1 5896thumb_copy_b (struct gdbarch *gdbarch, uint16_t insn,
1152d984 5897 arm_displaced_step_copy_insn_closure *dsc)
34518530
YQ
5898{
5899 unsigned int cond = 0;
5900 int offset = 0;
5901 unsigned short bit_12_15 = bits (insn, 12, 15);
5902 CORE_ADDR from = dsc->insn_addr;
5903
5904 if (bit_12_15 == 0xd)
5905 {
5906 /* offset = SignExtend (imm8:0, 32) */
5907 offset = sbits ((insn << 1), 0, 8);
5908 cond = bits (insn, 8, 11);
5909 }
5910 else if (bit_12_15 == 0xe) /* Encoding T2 */
5911 {
5912 offset = sbits ((insn << 1), 0, 11);
5913 cond = INST_AL;
5914 }
5915
136821d9
SM
5916 displaced_debug_printf ("copying b immediate insn %.4x with offset %d",
5917 insn, offset);
34518530
YQ
5918
5919 dsc->u.branch.cond = cond;
5920 dsc->u.branch.link = 0;
5921 dsc->u.branch.exchange = 0;
5922 dsc->u.branch.dest = from + 4 + offset;
5923
5924 dsc->modinsn[0] = THUMB_NOP;
5925
5926 dsc->cleanup = &cleanup_branch;
5927
5928 return 0;
5929}
5930
cca44b1b
JB
5931/* Copy BX/BLX with register-specified destinations. */
5932
7ff120b4
YQ
5933static void
5934install_bx_blx_reg (struct gdbarch *gdbarch, struct regcache *regs,
1152d984 5935 arm_displaced_step_copy_insn_closure *dsc, int link,
7ff120b4 5936 unsigned int cond, unsigned int rm)
cca44b1b 5937{
cca44b1b
JB
5938 /* Implement {BX,BLX}<cond> <reg>" as:
5939
5940 Preparation: cond <- instruction condition
5941 Insn: mov r0, r0 (nop)
5942 Cleanup: if (condition true) { r14 <- pc; pc <- dest; }.
5943
5944 Don't set r14 in cleanup for BX. */
5945
36073a92 5946 dsc->u.branch.dest = displaced_read_reg (regs, dsc, rm);
cca44b1b
JB
5947
5948 dsc->u.branch.cond = cond;
5949 dsc->u.branch.link = link;
cca44b1b 5950
7ff120b4 5951 dsc->u.branch.exchange = 1;
cca44b1b
JB
5952
5953 dsc->cleanup = &cleanup_branch;
7ff120b4 5954}
cca44b1b 5955
7ff120b4
YQ
5956static int
5957arm_copy_bx_blx_reg (struct gdbarch *gdbarch, uint32_t insn,
1152d984 5958 regcache *regs, arm_displaced_step_copy_insn_closure *dsc)
7ff120b4
YQ
5959{
5960 unsigned int cond = bits (insn, 28, 31);
5961 /* BX: x12xxx1x
5962 BLX: x12xxx3x. */
5963 int link = bit (insn, 5);
5964 unsigned int rm = bits (insn, 0, 3);
5965
136821d9 5966 displaced_debug_printf ("copying insn %.8lx", (unsigned long) insn);
7ff120b4
YQ
5967
5968 dsc->modinsn[0] = ARM_NOP;
5969
5970 install_bx_blx_reg (gdbarch, regs, dsc, link, cond, rm);
cca44b1b
JB
5971 return 0;
5972}
5973
34518530
YQ
5974static int
5975thumb_copy_bx_blx_reg (struct gdbarch *gdbarch, uint16_t insn,
5976 struct regcache *regs,
1152d984 5977 arm_displaced_step_copy_insn_closure *dsc)
34518530
YQ
5978{
5979 int link = bit (insn, 7);
5980 unsigned int rm = bits (insn, 3, 6);
5981
136821d9 5982 displaced_debug_printf ("copying insn %.4x", (unsigned short) insn);
34518530
YQ
5983
5984 dsc->modinsn[0] = THUMB_NOP;
5985
5986 install_bx_blx_reg (gdbarch, regs, dsc, link, INST_AL, rm);
5987
5988 return 0;
5989}
5990
5991
0963b4bd 5992/* Copy/cleanup arithmetic/logic instruction with immediate RHS. */
cca44b1b
JB
5993
5994static void
6e39997a 5995cleanup_alu_imm (struct gdbarch *gdbarch,
1152d984 5996 regcache *regs, arm_displaced_step_copy_insn_closure *dsc)
cca44b1b 5997{
36073a92 5998 ULONGEST rd_val = displaced_read_reg (regs, dsc, 0);
cca44b1b
JB
5999 displaced_write_reg (regs, dsc, 0, dsc->tmp[0], CANNOT_WRITE_PC);
6000 displaced_write_reg (regs, dsc, 1, dsc->tmp[1], CANNOT_WRITE_PC);
6001 displaced_write_reg (regs, dsc, dsc->rd, rd_val, ALU_WRITE_PC);
6002}
6003
6004static int
7ff120b4 6005arm_copy_alu_imm (struct gdbarch *gdbarch, uint32_t insn, struct regcache *regs,
1152d984 6006 arm_displaced_step_copy_insn_closure *dsc)
cca44b1b
JB
6007{
6008 unsigned int rn = bits (insn, 16, 19);
6009 unsigned int rd = bits (insn, 12, 15);
6010 unsigned int op = bits (insn, 21, 24);
6011 int is_mov = (op == 0xd);
6012 ULONGEST rd_val, rn_val;
cca44b1b
JB
6013
6014 if (!insn_references_pc (insn, 0x000ff000ul))
7ff120b4 6015 return arm_copy_unmodified (gdbarch, insn, "ALU immediate", dsc);
cca44b1b 6016
136821d9
SM
6017 displaced_debug_printf ("copying immediate %s insn %.8lx",
6018 is_mov ? "move" : "ALU",
6019 (unsigned long) insn);
cca44b1b
JB
6020
6021 /* Instruction is of form:
6022
6023 <op><cond> rd, [rn,] #imm
6024
6025 Rewrite as:
6026
6027 Preparation: tmp1, tmp2 <- r0, r1;
6028 r0, r1 <- rd, rn
6029 Insn: <op><cond> r0, r1, #imm
6030 Cleanup: rd <- r0; r0 <- tmp1; r1 <- tmp2
6031 */
6032
36073a92
YQ
6033 dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
6034 dsc->tmp[1] = displaced_read_reg (regs, dsc, 1);
6035 rn_val = displaced_read_reg (regs, dsc, rn);
6036 rd_val = displaced_read_reg (regs, dsc, rd);
cca44b1b
JB
6037 displaced_write_reg (regs, dsc, 0, rd_val, CANNOT_WRITE_PC);
6038 displaced_write_reg (regs, dsc, 1, rn_val, CANNOT_WRITE_PC);
6039 dsc->rd = rd;
6040
6041 if (is_mov)
6042 dsc->modinsn[0] = insn & 0xfff00fff;
6043 else
6044 dsc->modinsn[0] = (insn & 0xfff00fff) | 0x10000;
6045
6046 dsc->cleanup = &cleanup_alu_imm;
6047
6048 return 0;
6049}
6050
34518530
YQ
6051static int
6052thumb2_copy_alu_imm (struct gdbarch *gdbarch, uint16_t insn1,
6053 uint16_t insn2, struct regcache *regs,
1152d984 6054 arm_displaced_step_copy_insn_closure *dsc)
34518530
YQ
6055{
6056 unsigned int op = bits (insn1, 5, 8);
6057 unsigned int rn, rm, rd;
6058 ULONGEST rd_val, rn_val;
6059
6060 rn = bits (insn1, 0, 3); /* Rn */
6061 rm = bits (insn2, 0, 3); /* Rm */
6062 rd = bits (insn2, 8, 11); /* Rd */
6063
6064 /* This routine is only called for instruction MOV. */
6065 gdb_assert (op == 0x2 && rn == 0xf);
6066
6067 if (rm != ARM_PC_REGNUM && rd != ARM_PC_REGNUM)
6068 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2, "ALU imm", dsc);
6069
136821d9 6070 displaced_debug_printf ("copying reg %s insn %.4x%.4x", "ALU", insn1, insn2);
34518530
YQ
6071
6072 /* Instruction is of form:
6073
6074 <op><cond> rd, [rn,] #imm
6075
6076 Rewrite as:
6077
6078 Preparation: tmp1, tmp2 <- r0, r1;
6079 r0, r1 <- rd, rn
6080 Insn: <op><cond> r0, r1, #imm
6081 Cleanup: rd <- r0; r0 <- tmp1; r1 <- tmp2
6082 */
6083
6084 dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
6085 dsc->tmp[1] = displaced_read_reg (regs, dsc, 1);
6086 rn_val = displaced_read_reg (regs, dsc, rn);
6087 rd_val = displaced_read_reg (regs, dsc, rd);
6088 displaced_write_reg (regs, dsc, 0, rd_val, CANNOT_WRITE_PC);
6089 displaced_write_reg (regs, dsc, 1, rn_val, CANNOT_WRITE_PC);
6090 dsc->rd = rd;
6091
6092 dsc->modinsn[0] = insn1;
6093 dsc->modinsn[1] = ((insn2 & 0xf0f0) | 0x1);
6094 dsc->numinsns = 2;
6095
6096 dsc->cleanup = &cleanup_alu_imm;
6097
6098 return 0;
6099}
6100
cca44b1b
JB
6101/* Copy/cleanup arithmetic/logic insns with register RHS. */
6102
6103static void
6e39997a 6104cleanup_alu_reg (struct gdbarch *gdbarch,
1152d984 6105 regcache *regs, arm_displaced_step_copy_insn_closure *dsc)
cca44b1b
JB
6106{
6107 ULONGEST rd_val;
6108 int i;
6109
36073a92 6110 rd_val = displaced_read_reg (regs, dsc, 0);
cca44b1b
JB
6111
6112 for (i = 0; i < 3; i++)
6113 displaced_write_reg (regs, dsc, i, dsc->tmp[i], CANNOT_WRITE_PC);
6114
6115 displaced_write_reg (regs, dsc, dsc->rd, rd_val, ALU_WRITE_PC);
6116}
6117
7ff120b4
YQ
6118static void
6119install_alu_reg (struct gdbarch *gdbarch, struct regcache *regs,
1152d984 6120 arm_displaced_step_copy_insn_closure *dsc,
7ff120b4 6121 unsigned int rd, unsigned int rn, unsigned int rm)
cca44b1b 6122{
cca44b1b 6123 ULONGEST rd_val, rn_val, rm_val;
cca44b1b 6124
cca44b1b
JB
6125 /* Instruction is of form:
6126
6127 <op><cond> rd, [rn,] rm [, <shift>]
6128
6129 Rewrite as:
6130
6131 Preparation: tmp1, tmp2, tmp3 <- r0, r1, r2;
6132 r0, r1, r2 <- rd, rn, rm
ef713951 6133 Insn: <op><cond> r0, [r1,] r2 [, <shift>]
cca44b1b
JB
6134 Cleanup: rd <- r0; r0, r1, r2 <- tmp1, tmp2, tmp3
6135 */
6136
36073a92
YQ
6137 dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
6138 dsc->tmp[1] = displaced_read_reg (regs, dsc, 1);
6139 dsc->tmp[2] = displaced_read_reg (regs, dsc, 2);
6140 rd_val = displaced_read_reg (regs, dsc, rd);
6141 rn_val = displaced_read_reg (regs, dsc, rn);
6142 rm_val = displaced_read_reg (regs, dsc, rm);
cca44b1b
JB
6143 displaced_write_reg (regs, dsc, 0, rd_val, CANNOT_WRITE_PC);
6144 displaced_write_reg (regs, dsc, 1, rn_val, CANNOT_WRITE_PC);
6145 displaced_write_reg (regs, dsc, 2, rm_val, CANNOT_WRITE_PC);
6146 dsc->rd = rd;
6147
7ff120b4
YQ
6148 dsc->cleanup = &cleanup_alu_reg;
6149}
6150
6151static int
6152arm_copy_alu_reg (struct gdbarch *gdbarch, uint32_t insn, struct regcache *regs,
1152d984 6153 arm_displaced_step_copy_insn_closure *dsc)
7ff120b4
YQ
6154{
6155 unsigned int op = bits (insn, 21, 24);
6156 int is_mov = (op == 0xd);
6157
6158 if (!insn_references_pc (insn, 0x000ff00ful))
6159 return arm_copy_unmodified (gdbarch, insn, "ALU reg", dsc);
6160
136821d9
SM
6161 displaced_debug_printf ("copying reg %s insn %.8lx",
6162 is_mov ? "move" : "ALU", (unsigned long) insn);
7ff120b4 6163
cca44b1b
JB
6164 if (is_mov)
6165 dsc->modinsn[0] = (insn & 0xfff00ff0) | 0x2;
6166 else
6167 dsc->modinsn[0] = (insn & 0xfff00ff0) | 0x10002;
6168
7ff120b4
YQ
6169 install_alu_reg (gdbarch, regs, dsc, bits (insn, 12, 15), bits (insn, 16, 19),
6170 bits (insn, 0, 3));
cca44b1b
JB
6171 return 0;
6172}
6173
34518530
YQ
6174static int
6175thumb_copy_alu_reg (struct gdbarch *gdbarch, uint16_t insn,
6176 struct regcache *regs,
1152d984 6177 arm_displaced_step_copy_insn_closure *dsc)
34518530 6178{
ef713951 6179 unsigned rm, rd;
34518530 6180
ef713951
YQ
6181 rm = bits (insn, 3, 6);
6182 rd = (bit (insn, 7) << 3) | bits (insn, 0, 2);
34518530 6183
ef713951 6184 if (rd != ARM_PC_REGNUM && rm != ARM_PC_REGNUM)
34518530
YQ
6185 return thumb_copy_unmodified_16bit (gdbarch, insn, "ALU reg", dsc);
6186
136821d9 6187 displaced_debug_printf ("copying ALU reg insn %.4x", (unsigned short) insn);
34518530 6188
ef713951 6189 dsc->modinsn[0] = ((insn & 0xff00) | 0x10);
34518530 6190
ef713951 6191 install_alu_reg (gdbarch, regs, dsc, rd, rd, rm);
34518530
YQ
6192
6193 return 0;
6194}
6195
cca44b1b
JB
6196/* Cleanup/copy arithmetic/logic insns with shifted register RHS. */
6197
6198static void
6e39997a 6199cleanup_alu_shifted_reg (struct gdbarch *gdbarch,
cca44b1b 6200 struct regcache *regs,
1152d984 6201 arm_displaced_step_copy_insn_closure *dsc)
cca44b1b 6202{
36073a92 6203 ULONGEST rd_val = displaced_read_reg (regs, dsc, 0);
cca44b1b
JB
6204 int i;
6205
6206 for (i = 0; i < 4; i++)
6207 displaced_write_reg (regs, dsc, i, dsc->tmp[i], CANNOT_WRITE_PC);
6208
6209 displaced_write_reg (regs, dsc, dsc->rd, rd_val, ALU_WRITE_PC);
6210}
6211
7ff120b4
YQ
6212static void
6213install_alu_shifted_reg (struct gdbarch *gdbarch, struct regcache *regs,
1152d984 6214 arm_displaced_step_copy_insn_closure *dsc,
7ff120b4
YQ
6215 unsigned int rd, unsigned int rn, unsigned int rm,
6216 unsigned rs)
cca44b1b 6217{
7ff120b4 6218 int i;
cca44b1b 6219 ULONGEST rd_val, rn_val, rm_val, rs_val;
cca44b1b 6220
cca44b1b
JB
6221 /* Instruction is of form:
6222
6223 <op><cond> rd, [rn,] rm, <shift> rs
6224
6225 Rewrite as:
6226
6227 Preparation: tmp1, tmp2, tmp3, tmp4 <- r0, r1, r2, r3
6228 r0, r1, r2, r3 <- rd, rn, rm, rs
6229 Insn: <op><cond> r0, r1, r2, <shift> r3
6230 Cleanup: tmp5 <- r0
6231 r0, r1, r2, r3 <- tmp1, tmp2, tmp3, tmp4
6232 rd <- tmp5
6233 */
6234
6235 for (i = 0; i < 4; i++)
36073a92 6236 dsc->tmp[i] = displaced_read_reg (regs, dsc, i);
cca44b1b 6237
36073a92
YQ
6238 rd_val = displaced_read_reg (regs, dsc, rd);
6239 rn_val = displaced_read_reg (regs, dsc, rn);
6240 rm_val = displaced_read_reg (regs, dsc, rm);
6241 rs_val = displaced_read_reg (regs, dsc, rs);
cca44b1b
JB
6242 displaced_write_reg (regs, dsc, 0, rd_val, CANNOT_WRITE_PC);
6243 displaced_write_reg (regs, dsc, 1, rn_val, CANNOT_WRITE_PC);
6244 displaced_write_reg (regs, dsc, 2, rm_val, CANNOT_WRITE_PC);
6245 displaced_write_reg (regs, dsc, 3, rs_val, CANNOT_WRITE_PC);
6246 dsc->rd = rd;
7ff120b4
YQ
6247 dsc->cleanup = &cleanup_alu_shifted_reg;
6248}
6249
6250static int
6251arm_copy_alu_shifted_reg (struct gdbarch *gdbarch, uint32_t insn,
6252 struct regcache *regs,
1152d984 6253 arm_displaced_step_copy_insn_closure *dsc)
7ff120b4
YQ
6254{
6255 unsigned int op = bits (insn, 21, 24);
6256 int is_mov = (op == 0xd);
6257 unsigned int rd, rn, rm, rs;
6258
6259 if (!insn_references_pc (insn, 0x000fff0ful))
6260 return arm_copy_unmodified (gdbarch, insn, "ALU shifted reg", dsc);
6261
136821d9
SM
6262 displaced_debug_printf ("copying shifted reg %s insn %.8lx",
6263 is_mov ? "move" : "ALU",
6264 (unsigned long) insn);
7ff120b4
YQ
6265
6266 rn = bits (insn, 16, 19);
6267 rm = bits (insn, 0, 3);
6268 rs = bits (insn, 8, 11);
6269 rd = bits (insn, 12, 15);
cca44b1b
JB
6270
6271 if (is_mov)
6272 dsc->modinsn[0] = (insn & 0xfff000f0) | 0x302;
6273 else
6274 dsc->modinsn[0] = (insn & 0xfff000f0) | 0x10302;
6275
7ff120b4 6276 install_alu_shifted_reg (gdbarch, regs, dsc, rd, rn, rm, rs);
cca44b1b
JB
6277
6278 return 0;
6279}
6280
6281/* Clean up load instructions. */
6282
6283static void
6e39997a 6284cleanup_load (struct gdbarch *gdbarch, struct regcache *regs,
1152d984 6285 arm_displaced_step_copy_insn_closure *dsc)
cca44b1b
JB
6286{
6287 ULONGEST rt_val, rt_val2 = 0, rn_val;
cca44b1b 6288
36073a92 6289 rt_val = displaced_read_reg (regs, dsc, 0);
cca44b1b 6290 if (dsc->u.ldst.xfersize == 8)
36073a92
YQ
6291 rt_val2 = displaced_read_reg (regs, dsc, 1);
6292 rn_val = displaced_read_reg (regs, dsc, 2);
cca44b1b
JB
6293
6294 displaced_write_reg (regs, dsc, 0, dsc->tmp[0], CANNOT_WRITE_PC);
6295 if (dsc->u.ldst.xfersize > 4)
6296 displaced_write_reg (regs, dsc, 1, dsc->tmp[1], CANNOT_WRITE_PC);
6297 displaced_write_reg (regs, dsc, 2, dsc->tmp[2], CANNOT_WRITE_PC);
6298 if (!dsc->u.ldst.immed)
6299 displaced_write_reg (regs, dsc, 3, dsc->tmp[3], CANNOT_WRITE_PC);
6300
6301 /* Handle register writeback. */
6302 if (dsc->u.ldst.writeback)
6303 displaced_write_reg (regs, dsc, dsc->u.ldst.rn, rn_val, CANNOT_WRITE_PC);
6304 /* Put result in right place. */
6305 displaced_write_reg (regs, dsc, dsc->rd, rt_val, LOAD_WRITE_PC);
6306 if (dsc->u.ldst.xfersize == 8)
6307 displaced_write_reg (regs, dsc, dsc->rd + 1, rt_val2, LOAD_WRITE_PC);
6308}
6309
6310/* Clean up store instructions. */
6311
6312static void
6e39997a 6313cleanup_store (struct gdbarch *gdbarch, struct regcache *regs,
1152d984 6314 arm_displaced_step_copy_insn_closure *dsc)
cca44b1b 6315{
36073a92 6316 ULONGEST rn_val = displaced_read_reg (regs, dsc, 2);
cca44b1b
JB
6317
6318 displaced_write_reg (regs, dsc, 0, dsc->tmp[0], CANNOT_WRITE_PC);
6319 if (dsc->u.ldst.xfersize > 4)
6320 displaced_write_reg (regs, dsc, 1, dsc->tmp[1], CANNOT_WRITE_PC);
6321 displaced_write_reg (regs, dsc, 2, dsc->tmp[2], CANNOT_WRITE_PC);
6322 if (!dsc->u.ldst.immed)
6323 displaced_write_reg (regs, dsc, 3, dsc->tmp[3], CANNOT_WRITE_PC);
6324 if (!dsc->u.ldst.restore_r4)
6325 displaced_write_reg (regs, dsc, 4, dsc->tmp[4], CANNOT_WRITE_PC);
6326
6327 /* Writeback. */
6328 if (dsc->u.ldst.writeback)
6329 displaced_write_reg (regs, dsc, dsc->u.ldst.rn, rn_val, CANNOT_WRITE_PC);
6330}
6331
6332/* Copy "extra" load/store instructions. These are halfword/doubleword
6333 transfers, which have a different encoding to byte/word transfers. */
6334
6335static int
550dc4e2 6336arm_copy_extra_ld_st (struct gdbarch *gdbarch, uint32_t insn, int unprivileged,
1152d984 6337 regcache *regs, arm_displaced_step_copy_insn_closure *dsc)
cca44b1b
JB
6338{
6339 unsigned int op1 = bits (insn, 20, 24);
6340 unsigned int op2 = bits (insn, 5, 6);
6341 unsigned int rt = bits (insn, 12, 15);
6342 unsigned int rn = bits (insn, 16, 19);
6343 unsigned int rm = bits (insn, 0, 3);
6344 char load[12] = {0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1};
6345 char bytesize[12] = {2, 2, 2, 2, 8, 1, 8, 1, 8, 2, 8, 2};
6346 int immed = (op1 & 0x4) != 0;
6347 int opcode;
6348 ULONGEST rt_val, rt_val2 = 0, rn_val, rm_val = 0;
cca44b1b
JB
6349
6350 if (!insn_references_pc (insn, 0x000ff00ful))
7ff120b4 6351 return arm_copy_unmodified (gdbarch, insn, "extra load/store", dsc);
cca44b1b 6352
136821d9
SM
6353 displaced_debug_printf ("copying %sextra load/store insn %.8lx",
6354 unprivileged ? "unprivileged " : "",
6355 (unsigned long) insn);
cca44b1b
JB
6356
6357 opcode = ((op2 << 2) | (op1 & 0x1) | ((op1 & 0x4) >> 1)) - 4;
6358
6359 if (opcode < 0)
6360 internal_error (__FILE__, __LINE__,
6361 _("copy_extra_ld_st: instruction decode error"));
6362
36073a92
YQ
6363 dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
6364 dsc->tmp[1] = displaced_read_reg (regs, dsc, 1);
6365 dsc->tmp[2] = displaced_read_reg (regs, dsc, 2);
cca44b1b 6366 if (!immed)
36073a92 6367 dsc->tmp[3] = displaced_read_reg (regs, dsc, 3);
cca44b1b 6368
36073a92 6369 rt_val = displaced_read_reg (regs, dsc, rt);
cca44b1b 6370 if (bytesize[opcode] == 8)
36073a92
YQ
6371 rt_val2 = displaced_read_reg (regs, dsc, rt + 1);
6372 rn_val = displaced_read_reg (regs, dsc, rn);
cca44b1b 6373 if (!immed)
36073a92 6374 rm_val = displaced_read_reg (regs, dsc, rm);
cca44b1b
JB
6375
6376 displaced_write_reg (regs, dsc, 0, rt_val, CANNOT_WRITE_PC);
6377 if (bytesize[opcode] == 8)
6378 displaced_write_reg (regs, dsc, 1, rt_val2, CANNOT_WRITE_PC);
6379 displaced_write_reg (regs, dsc, 2, rn_val, CANNOT_WRITE_PC);
6380 if (!immed)
6381 displaced_write_reg (regs, dsc, 3, rm_val, CANNOT_WRITE_PC);
6382
6383 dsc->rd = rt;
6384 dsc->u.ldst.xfersize = bytesize[opcode];
6385 dsc->u.ldst.rn = rn;
6386 dsc->u.ldst.immed = immed;
6387 dsc->u.ldst.writeback = bit (insn, 24) == 0 || bit (insn, 21) != 0;
6388 dsc->u.ldst.restore_r4 = 0;
6389
6390 if (immed)
6391 /* {ldr,str}<width><cond> rt, [rt2,] [rn, #imm]
6392 ->
6393 {ldr,str}<width><cond> r0, [r1,] [r2, #imm]. */
6394 dsc->modinsn[0] = (insn & 0xfff00fff) | 0x20000;
6395 else
6396 /* {ldr,str}<width><cond> rt, [rt2,] [rn, +/-rm]
6397 ->
6398 {ldr,str}<width><cond> r0, [r1,] [r2, +/-r3]. */
6399 dsc->modinsn[0] = (insn & 0xfff00ff0) | 0x20003;
6400
6401 dsc->cleanup = load[opcode] ? &cleanup_load : &cleanup_store;
6402
6403 return 0;
6404}
6405
0f6f04ba 6406/* Copy byte/half word/word loads and stores. */
cca44b1b 6407
7ff120b4 6408static void
0f6f04ba 6409install_load_store (struct gdbarch *gdbarch, struct regcache *regs,
1152d984 6410 arm_displaced_step_copy_insn_closure *dsc, int load,
0f6f04ba
YQ
6411 int immed, int writeback, int size, int usermode,
6412 int rt, int rm, int rn)
cca44b1b 6413{
cca44b1b 6414 ULONGEST rt_val, rn_val, rm_val = 0;
cca44b1b 6415
36073a92
YQ
6416 dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
6417 dsc->tmp[2] = displaced_read_reg (regs, dsc, 2);
cca44b1b 6418 if (!immed)
36073a92 6419 dsc->tmp[3] = displaced_read_reg (regs, dsc, 3);
cca44b1b 6420 if (!load)
36073a92 6421 dsc->tmp[4] = displaced_read_reg (regs, dsc, 4);
cca44b1b 6422
36073a92
YQ
6423 rt_val = displaced_read_reg (regs, dsc, rt);
6424 rn_val = displaced_read_reg (regs, dsc, rn);
cca44b1b 6425 if (!immed)
36073a92 6426 rm_val = displaced_read_reg (regs, dsc, rm);
cca44b1b
JB
6427
6428 displaced_write_reg (regs, dsc, 0, rt_val, CANNOT_WRITE_PC);
6429 displaced_write_reg (regs, dsc, 2, rn_val, CANNOT_WRITE_PC);
6430 if (!immed)
6431 displaced_write_reg (regs, dsc, 3, rm_val, CANNOT_WRITE_PC);
cca44b1b 6432 dsc->rd = rt;
0f6f04ba 6433 dsc->u.ldst.xfersize = size;
cca44b1b
JB
6434 dsc->u.ldst.rn = rn;
6435 dsc->u.ldst.immed = immed;
7ff120b4 6436 dsc->u.ldst.writeback = writeback;
cca44b1b
JB
6437
6438 /* To write PC we can do:
6439
494e194e
YQ
6440 Before this sequence of instructions:
6441 r0 is the PC value got from displaced_read_reg, so r0 = from + 8;
85102364 6442 r2 is the Rn value got from displaced_read_reg.
494e194e
YQ
6443
6444 Insn1: push {pc} Write address of STR instruction + offset on stack
6445 Insn2: pop {r4} Read it back from stack, r4 = addr(Insn1) + offset
6446 Insn3: sub r4, r4, pc r4 = addr(Insn1) + offset - pc
dda83cd7
SM
6447 = addr(Insn1) + offset - addr(Insn3) - 8
6448 = offset - 16
494e194e
YQ
6449 Insn4: add r4, r4, #8 r4 = offset - 8
6450 Insn5: add r0, r0, r4 r0 = from + 8 + offset - 8
dda83cd7 6451 = from + offset
494e194e 6452 Insn6: str r0, [r2, #imm] (or str r0, [r2, r3])
cca44b1b
JB
6453
6454 Otherwise we don't know what value to write for PC, since the offset is
494e194e
YQ
6455 architecture-dependent (sometimes PC+8, sometimes PC+12). More details
6456 of this can be found in Section "Saving from r15" in
6457 http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dui0204g/Cihbjifh.html */
cca44b1b 6458
7ff120b4
YQ
6459 dsc->cleanup = load ? &cleanup_load : &cleanup_store;
6460}
6461
34518530
YQ
6462
6463static int
6464thumb2_copy_load_literal (struct gdbarch *gdbarch, uint16_t insn1,
6465 uint16_t insn2, struct regcache *regs,
1152d984 6466 arm_displaced_step_copy_insn_closure *dsc, int size)
34518530
YQ
6467{
6468 unsigned int u_bit = bit (insn1, 7);
6469 unsigned int rt = bits (insn2, 12, 15);
6470 int imm12 = bits (insn2, 0, 11);
6471 ULONGEST pc_val;
6472
136821d9
SM
6473 displaced_debug_printf ("copying ldr pc (0x%x) R%d %c imm12 %.4x",
6474 (unsigned int) dsc->insn_addr, rt, u_bit ? '+' : '-',
6475 imm12);
34518530
YQ
6476
6477 if (!u_bit)
6478 imm12 = -1 * imm12;
6479
6480 /* Rewrite instruction LDR Rt imm12 into:
6481
6482 Prepare: tmp[0] <- r0, tmp[1] <- r2, tmp[2] <- r3, r2 <- pc, r3 <- imm12
6483
6484 LDR R0, R2, R3,
6485
6486 Cleanup: rt <- r0, r0 <- tmp[0], r2 <- tmp[1], r3 <- tmp[2]. */
6487
6488
6489 dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
6490 dsc->tmp[2] = displaced_read_reg (regs, dsc, 2);
6491 dsc->tmp[3] = displaced_read_reg (regs, dsc, 3);
6492
6493 pc_val = displaced_read_reg (regs, dsc, ARM_PC_REGNUM);
6494
6495 pc_val = pc_val & 0xfffffffc;
6496
6497 displaced_write_reg (regs, dsc, 2, pc_val, CANNOT_WRITE_PC);
6498 displaced_write_reg (regs, dsc, 3, imm12, CANNOT_WRITE_PC);
6499
6500 dsc->rd = rt;
6501
6502 dsc->u.ldst.xfersize = size;
6503 dsc->u.ldst.immed = 0;
6504 dsc->u.ldst.writeback = 0;
6505 dsc->u.ldst.restore_r4 = 0;
6506
6507 /* LDR R0, R2, R3 */
6508 dsc->modinsn[0] = 0xf852;
6509 dsc->modinsn[1] = 0x3;
6510 dsc->numinsns = 2;
6511
6512 dsc->cleanup = &cleanup_load;
6513
6514 return 0;
6515}
6516
6517static int
6518thumb2_copy_load_reg_imm (struct gdbarch *gdbarch, uint16_t insn1,
6519 uint16_t insn2, struct regcache *regs,
1152d984 6520 arm_displaced_step_copy_insn_closure *dsc,
34518530
YQ
6521 int writeback, int immed)
6522{
6523 unsigned int rt = bits (insn2, 12, 15);
6524 unsigned int rn = bits (insn1, 0, 3);
6525 unsigned int rm = bits (insn2, 0, 3); /* Only valid if !immed. */
6526 /* In LDR (register), there is also a register Rm, which is not allowed to
6527 be PC, so we don't have to check it. */
6528
6529 if (rt != ARM_PC_REGNUM && rn != ARM_PC_REGNUM)
6530 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2, "load",
6531 dsc);
6532
136821d9
SM
6533 displaced_debug_printf ("copying ldr r%d [r%d] insn %.4x%.4x",
6534 rt, rn, insn1, insn2);
34518530
YQ
6535
6536 install_load_store (gdbarch, regs, dsc, 1, immed, writeback, 4,
6537 0, rt, rm, rn);
6538
6539 dsc->u.ldst.restore_r4 = 0;
6540
6541 if (immed)
6542 /* ldr[b]<cond> rt, [rn, #imm], etc.
6543 ->
6544 ldr[b]<cond> r0, [r2, #imm]. */
6545 {
6546 dsc->modinsn[0] = (insn1 & 0xfff0) | 0x2;
6547 dsc->modinsn[1] = insn2 & 0x0fff;
6548 }
6549 else
6550 /* ldr[b]<cond> rt, [rn, rm], etc.
6551 ->
6552 ldr[b]<cond> r0, [r2, r3]. */
6553 {
6554 dsc->modinsn[0] = (insn1 & 0xfff0) | 0x2;
6555 dsc->modinsn[1] = (insn2 & 0x0ff0) | 0x3;
6556 }
6557
6558 dsc->numinsns = 2;
6559
6560 return 0;
6561}
6562
6563
7ff120b4
YQ
6564static int
6565arm_copy_ldr_str_ldrb_strb (struct gdbarch *gdbarch, uint32_t insn,
6566 struct regcache *regs,
1152d984 6567 arm_displaced_step_copy_insn_closure *dsc,
0f6f04ba 6568 int load, int size, int usermode)
7ff120b4
YQ
6569{
6570 int immed = !bit (insn, 25);
6571 int writeback = (bit (insn, 24) == 0 || bit (insn, 21) != 0);
6572 unsigned int rt = bits (insn, 12, 15);
6573 unsigned int rn = bits (insn, 16, 19);
6574 unsigned int rm = bits (insn, 0, 3); /* Only valid if !immed. */
6575
6576 if (!insn_references_pc (insn, 0x000ff00ful))
6577 return arm_copy_unmodified (gdbarch, insn, "load/store", dsc);
6578
136821d9
SM
6579 displaced_debug_printf ("copying %s%s r%d [r%d] insn %.8lx",
6580 load ? (size == 1 ? "ldrb" : "ldr")
6581 : (size == 1 ? "strb" : "str"),
6582 usermode ? "t" : "",
6583 rt, rn,
6584 (unsigned long) insn);
7ff120b4 6585
0f6f04ba
YQ
6586 install_load_store (gdbarch, regs, dsc, load, immed, writeback, size,
6587 usermode, rt, rm, rn);
7ff120b4 6588
bf9f652a 6589 if (load || rt != ARM_PC_REGNUM)
cca44b1b
JB
6590 {
6591 dsc->u.ldst.restore_r4 = 0;
6592
6593 if (immed)
6594 /* {ldr,str}[b]<cond> rt, [rn, #imm], etc.
6595 ->
6596 {ldr,str}[b]<cond> r0, [r2, #imm]. */
6597 dsc->modinsn[0] = (insn & 0xfff00fff) | 0x20000;
6598 else
6599 /* {ldr,str}[b]<cond> rt, [rn, rm], etc.
6600 ->
6601 {ldr,str}[b]<cond> r0, [r2, r3]. */
6602 dsc->modinsn[0] = (insn & 0xfff00ff0) | 0x20003;
6603 }
6604 else
6605 {
6606 /* We need to use r4 as scratch. Make sure it's restored afterwards. */
6607 dsc->u.ldst.restore_r4 = 1;
494e194e
YQ
6608 dsc->modinsn[0] = 0xe92d8000; /* push {pc} */
6609 dsc->modinsn[1] = 0xe8bd0010; /* pop {r4} */
cca44b1b
JB
6610 dsc->modinsn[2] = 0xe044400f; /* sub r4, r4, pc. */
6611 dsc->modinsn[3] = 0xe2844008; /* add r4, r4, #8. */
6612 dsc->modinsn[4] = 0xe0800004; /* add r0, r0, r4. */
6613
6614 /* As above. */
6615 if (immed)
6616 dsc->modinsn[5] = (insn & 0xfff00fff) | 0x20000;
6617 else
6618 dsc->modinsn[5] = (insn & 0xfff00ff0) | 0x20003;
6619
cca44b1b
JB
6620 dsc->numinsns = 6;
6621 }
6622
6623 dsc->cleanup = load ? &cleanup_load : &cleanup_store;
6624
6625 return 0;
6626}
6627
6628/* Cleanup LDM instructions with fully-populated register list. This is an
6629 unfortunate corner case: it's impossible to implement correctly by modifying
6630 the instruction. The issue is as follows: we have an instruction,
6631
6632 ldm rN, {r0-r15}
6633
6634 which we must rewrite to avoid loading PC. A possible solution would be to
6635 do the load in two halves, something like (with suitable cleanup
6636 afterwards):
6637
6638 mov r8, rN
6639 ldm[id][ab] r8!, {r0-r7}
6640 str r7, <temp>
6641 ldm[id][ab] r8, {r7-r14}
6642 <bkpt>
6643
6644 but at present there's no suitable place for <temp>, since the scratch space
6645 is overwritten before the cleanup routine is called. For now, we simply
6646 emulate the instruction. */
6647
6648static void
6649cleanup_block_load_all (struct gdbarch *gdbarch, struct regcache *regs,
1152d984 6650 arm_displaced_step_copy_insn_closure *dsc)
cca44b1b 6651{
cca44b1b
JB
6652 int inc = dsc->u.block.increment;
6653 int bump_before = dsc->u.block.before ? (inc ? 4 : -4) : 0;
6654 int bump_after = dsc->u.block.before ? 0 : (inc ? 4 : -4);
6655 uint32_t regmask = dsc->u.block.regmask;
6656 int regno = inc ? 0 : 15;
6657 CORE_ADDR xfer_addr = dsc->u.block.xfer_addr;
6658 int exception_return = dsc->u.block.load && dsc->u.block.user
6659 && (regmask & 0x8000) != 0;
36073a92 6660 uint32_t status = displaced_read_reg (regs, dsc, ARM_PS_REGNUM);
cca44b1b
JB
6661 int do_transfer = condition_true (dsc->u.block.cond, status);
6662 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
6663
6664 if (!do_transfer)
6665 return;
6666
6667 /* If the instruction is ldm rN, {...pc}^, I don't think there's anything
6668 sensible we can do here. Complain loudly. */
6669 if (exception_return)
6670 error (_("Cannot single-step exception return"));
6671
6672 /* We don't handle any stores here for now. */
6673 gdb_assert (dsc->u.block.load != 0);
6674
136821d9
SM
6675 displaced_debug_printf ("emulating block transfer: %s %s %s",
6676 dsc->u.block.load ? "ldm" : "stm",
6677 dsc->u.block.increment ? "inc" : "dec",
6678 dsc->u.block.before ? "before" : "after");
cca44b1b
JB
6679
6680 while (regmask)
6681 {
6682 uint32_t memword;
6683
6684 if (inc)
bf9f652a 6685 while (regno <= ARM_PC_REGNUM && (regmask & (1 << regno)) == 0)
cca44b1b
JB
6686 regno++;
6687 else
6688 while (regno >= 0 && (regmask & (1 << regno)) == 0)
6689 regno--;
6690
6691 xfer_addr += bump_before;
6692
6693 memword = read_memory_unsigned_integer (xfer_addr, 4, byte_order);
6694 displaced_write_reg (regs, dsc, regno, memword, LOAD_WRITE_PC);
6695
6696 xfer_addr += bump_after;
6697
6698 regmask &= ~(1 << regno);
6699 }
6700
6701 if (dsc->u.block.writeback)
6702 displaced_write_reg (regs, dsc, dsc->u.block.rn, xfer_addr,
6703 CANNOT_WRITE_PC);
6704}
6705
6706/* Clean up an STM which included the PC in the register list. */
6707
6708static void
6709cleanup_block_store_pc (struct gdbarch *gdbarch, struct regcache *regs,
1152d984 6710 arm_displaced_step_copy_insn_closure *dsc)
cca44b1b 6711{
36073a92 6712 uint32_t status = displaced_read_reg (regs, dsc, ARM_PS_REGNUM);
cca44b1b 6713 int store_executed = condition_true (dsc->u.block.cond, status);
5f661e03
SM
6714 CORE_ADDR pc_stored_at, transferred_regs
6715 = count_one_bits (dsc->u.block.regmask);
cca44b1b
JB
6716 CORE_ADDR stm_insn_addr;
6717 uint32_t pc_val;
6718 long offset;
6719 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
6720
6721 /* If condition code fails, there's nothing else to do. */
6722 if (!store_executed)
6723 return;
6724
6725 if (dsc->u.block.increment)
6726 {
6727 pc_stored_at = dsc->u.block.xfer_addr + 4 * transferred_regs;
6728
6729 if (dsc->u.block.before)
6730 pc_stored_at += 4;
6731 }
6732 else
6733 {
6734 pc_stored_at = dsc->u.block.xfer_addr;
6735
6736 if (dsc->u.block.before)
6737 pc_stored_at -= 4;
6738 }
6739
6740 pc_val = read_memory_unsigned_integer (pc_stored_at, 4, byte_order);
6741 stm_insn_addr = dsc->scratch_base;
6742 offset = pc_val - stm_insn_addr;
6743
136821d9
SM
6744 displaced_debug_printf ("detected PC offset %.8lx for STM instruction",
6745 offset);
cca44b1b
JB
6746
6747 /* Rewrite the stored PC to the proper value for the non-displaced original
6748 instruction. */
6749 write_memory_unsigned_integer (pc_stored_at, 4, byte_order,
6750 dsc->insn_addr + offset);
6751}
6752
6753/* Clean up an LDM which includes the PC in the register list. We clumped all
6754 the registers in the transferred list into a contiguous range r0...rX (to
6755 avoid loading PC directly and losing control of the debugged program), so we
6756 must undo that here. */
6757
6758static void
6e39997a 6759cleanup_block_load_pc (struct gdbarch *gdbarch,
cca44b1b 6760 struct regcache *regs,
1152d984 6761 arm_displaced_step_copy_insn_closure *dsc)
cca44b1b 6762{
36073a92 6763 uint32_t status = displaced_read_reg (regs, dsc, ARM_PS_REGNUM);
22e048c9 6764 int load_executed = condition_true (dsc->u.block.cond, status);
bf9f652a 6765 unsigned int mask = dsc->u.block.regmask, write_reg = ARM_PC_REGNUM;
5f661e03 6766 unsigned int regs_loaded = count_one_bits (mask);
cca44b1b
JB
6767 unsigned int num_to_shuffle = regs_loaded, clobbered;
6768
6769 /* The method employed here will fail if the register list is fully populated
6770 (we need to avoid loading PC directly). */
6771 gdb_assert (num_to_shuffle < 16);
6772
6773 if (!load_executed)
6774 return;
6775
6776 clobbered = (1 << num_to_shuffle) - 1;
6777
6778 while (num_to_shuffle > 0)
6779 {
6780 if ((mask & (1 << write_reg)) != 0)
6781 {
6782 unsigned int read_reg = num_to_shuffle - 1;
6783
6784 if (read_reg != write_reg)
6785 {
36073a92 6786 ULONGEST rval = displaced_read_reg (regs, dsc, read_reg);
cca44b1b 6787 displaced_write_reg (regs, dsc, write_reg, rval, LOAD_WRITE_PC);
136821d9
SM
6788 displaced_debug_printf ("LDM: move loaded register r%d to r%d",
6789 read_reg, write_reg);
cca44b1b 6790 }
136821d9
SM
6791 else
6792 displaced_debug_printf ("LDM: register r%d already in the right "
6793 "place", write_reg);
cca44b1b
JB
6794
6795 clobbered &= ~(1 << write_reg);
6796
6797 num_to_shuffle--;
6798 }
6799
6800 write_reg--;
6801 }
6802
6803 /* Restore any registers we scribbled over. */
6804 for (write_reg = 0; clobbered != 0; write_reg++)
6805 {
6806 if ((clobbered & (1 << write_reg)) != 0)
6807 {
6808 displaced_write_reg (regs, dsc, write_reg, dsc->tmp[write_reg],
6809 CANNOT_WRITE_PC);
136821d9
SM
6810 displaced_debug_printf ("LDM: restored clobbered register r%d",
6811 write_reg);
cca44b1b
JB
6812 clobbered &= ~(1 << write_reg);
6813 }
6814 }
6815
6816 /* Perform register writeback manually. */
6817 if (dsc->u.block.writeback)
6818 {
6819 ULONGEST new_rn_val = dsc->u.block.xfer_addr;
6820
6821 if (dsc->u.block.increment)
6822 new_rn_val += regs_loaded * 4;
6823 else
6824 new_rn_val -= regs_loaded * 4;
6825
6826 displaced_write_reg (regs, dsc, dsc->u.block.rn, new_rn_val,
6827 CANNOT_WRITE_PC);
6828 }
6829}
6830
6831/* Handle ldm/stm, apart from some tricky cases which are unlikely to occur
6832 in user-level code (in particular exception return, ldm rn, {...pc}^). */
6833
6834static int
7ff120b4
YQ
6835arm_copy_block_xfer (struct gdbarch *gdbarch, uint32_t insn,
6836 struct regcache *regs,
1152d984 6837 arm_displaced_step_copy_insn_closure *dsc)
cca44b1b
JB
6838{
6839 int load = bit (insn, 20);
6840 int user = bit (insn, 22);
6841 int increment = bit (insn, 23);
6842 int before = bit (insn, 24);
6843 int writeback = bit (insn, 21);
6844 int rn = bits (insn, 16, 19);
cca44b1b 6845
0963b4bd
MS
6846 /* Block transfers which don't mention PC can be run directly
6847 out-of-line. */
bf9f652a 6848 if (rn != ARM_PC_REGNUM && (insn & 0x8000) == 0)
7ff120b4 6849 return arm_copy_unmodified (gdbarch, insn, "ldm/stm", dsc);
cca44b1b 6850
bf9f652a 6851 if (rn == ARM_PC_REGNUM)
cca44b1b 6852 {
0963b4bd
MS
6853 warning (_("displaced: Unpredictable LDM or STM with "
6854 "base register r15"));
7ff120b4 6855 return arm_copy_unmodified (gdbarch, insn, "unpredictable ldm/stm", dsc);
cca44b1b
JB
6856 }
6857
136821d9
SM
6858 displaced_debug_printf ("copying block transfer insn %.8lx",
6859 (unsigned long) insn);
cca44b1b 6860
36073a92 6861 dsc->u.block.xfer_addr = displaced_read_reg (regs, dsc, rn);
cca44b1b
JB
6862 dsc->u.block.rn = rn;
6863
6864 dsc->u.block.load = load;
6865 dsc->u.block.user = user;
6866 dsc->u.block.increment = increment;
6867 dsc->u.block.before = before;
6868 dsc->u.block.writeback = writeback;
6869 dsc->u.block.cond = bits (insn, 28, 31);
6870
6871 dsc->u.block.regmask = insn & 0xffff;
6872
6873 if (load)
6874 {
6875 if ((insn & 0xffff) == 0xffff)
6876 {
6877 /* LDM with a fully-populated register list. This case is
6878 particularly tricky. Implement for now by fully emulating the
6879 instruction (which might not behave perfectly in all cases, but
6880 these instructions should be rare enough for that not to matter
6881 too much). */
6882 dsc->modinsn[0] = ARM_NOP;
6883
6884 dsc->cleanup = &cleanup_block_load_all;
6885 }
6886 else
6887 {
6888 /* LDM of a list of registers which includes PC. Implement by
6889 rewriting the list of registers to be transferred into a
6890 contiguous chunk r0...rX before doing the transfer, then shuffling
6891 registers into the correct places in the cleanup routine. */
6892 unsigned int regmask = insn & 0xffff;
5f661e03 6893 unsigned int num_in_list = count_one_bits (regmask), new_regmask;
bec2ab5a 6894 unsigned int i;
cca44b1b
JB
6895
6896 for (i = 0; i < num_in_list; i++)
36073a92 6897 dsc->tmp[i] = displaced_read_reg (regs, dsc, i);
cca44b1b
JB
6898
6899 /* Writeback makes things complicated. We need to avoid clobbering
6900 the base register with one of the registers in our modified
6901 register list, but just using a different register can't work in
6902 all cases, e.g.:
6903
6904 ldm r14!, {r0-r13,pc}
6905
6906 which would need to be rewritten as:
6907
6908 ldm rN!, {r0-r14}
6909
6910 but that can't work, because there's no free register for N.
6911
6912 Solve this by turning off the writeback bit, and emulating
6913 writeback manually in the cleanup routine. */
6914
6915 if (writeback)
6916 insn &= ~(1 << 21);
6917
6918 new_regmask = (1 << num_in_list) - 1;
6919
136821d9
SM
6920 displaced_debug_printf ("LDM r%d%s, {..., pc}: original reg list "
6921 "%.4x, modified list %.4x",
6922 rn, writeback ? "!" : "",
6923 (int) insn & 0xffff, new_regmask);
cca44b1b
JB
6924
6925 dsc->modinsn[0] = (insn & ~0xffff) | (new_regmask & 0xffff);
6926
6927 dsc->cleanup = &cleanup_block_load_pc;
6928 }
6929 }
6930 else
6931 {
6932 /* STM of a list of registers which includes PC. Run the instruction
6933 as-is, but out of line: this will store the wrong value for the PC,
6934 so we must manually fix up the memory in the cleanup routine.
6935 Doing things this way has the advantage that we can auto-detect
6936 the offset of the PC write (which is architecture-dependent) in
6937 the cleanup routine. */
6938 dsc->modinsn[0] = insn;
6939
6940 dsc->cleanup = &cleanup_block_store_pc;
6941 }
6942
6943 return 0;
6944}
6945
34518530
YQ
6946static int
6947thumb2_copy_block_xfer (struct gdbarch *gdbarch, uint16_t insn1, uint16_t insn2,
6948 struct regcache *regs,
1152d984 6949 arm_displaced_step_copy_insn_closure *dsc)
cca44b1b 6950{
34518530
YQ
6951 int rn = bits (insn1, 0, 3);
6952 int load = bit (insn1, 4);
6953 int writeback = bit (insn1, 5);
cca44b1b 6954
34518530
YQ
6955 /* Block transfers which don't mention PC can be run directly
6956 out-of-line. */
6957 if (rn != ARM_PC_REGNUM && (insn2 & 0x8000) == 0)
6958 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2, "ldm/stm", dsc);
7ff120b4 6959
34518530
YQ
6960 if (rn == ARM_PC_REGNUM)
6961 {
6962 warning (_("displaced: Unpredictable LDM or STM with "
6963 "base register r15"));
6964 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
6965 "unpredictable ldm/stm", dsc);
6966 }
cca44b1b 6967
136821d9
SM
6968 displaced_debug_printf ("copying block transfer insn %.4x%.4x",
6969 insn1, insn2);
cca44b1b 6970
34518530
YQ
6971 /* Clear bit 13, since it should be always zero. */
6972 dsc->u.block.regmask = (insn2 & 0xdfff);
6973 dsc->u.block.rn = rn;
cca44b1b 6974
34518530
YQ
6975 dsc->u.block.load = load;
6976 dsc->u.block.user = 0;
6977 dsc->u.block.increment = bit (insn1, 7);
6978 dsc->u.block.before = bit (insn1, 8);
6979 dsc->u.block.writeback = writeback;
6980 dsc->u.block.cond = INST_AL;
6981 dsc->u.block.xfer_addr = displaced_read_reg (regs, dsc, rn);
cca44b1b 6982
34518530
YQ
6983 if (load)
6984 {
6985 if (dsc->u.block.regmask == 0xffff)
6986 {
6987 /* This branch is impossible to happen. */
6988 gdb_assert (0);
6989 }
6990 else
6991 {
6992 unsigned int regmask = dsc->u.block.regmask;
5f661e03 6993 unsigned int num_in_list = count_one_bits (regmask), new_regmask;
bec2ab5a 6994 unsigned int i;
34518530
YQ
6995
6996 for (i = 0; i < num_in_list; i++)
6997 dsc->tmp[i] = displaced_read_reg (regs, dsc, i);
6998
6999 if (writeback)
7000 insn1 &= ~(1 << 5);
7001
7002 new_regmask = (1 << num_in_list) - 1;
7003
136821d9
SM
7004 displaced_debug_printf ("LDM r%d%s, {..., pc}: original reg list "
7005 "%.4x, modified list %.4x",
7006 rn, writeback ? "!" : "",
7007 (int) dsc->u.block.regmask, new_regmask);
34518530
YQ
7008
7009 dsc->modinsn[0] = insn1;
7010 dsc->modinsn[1] = (new_regmask & 0xffff);
7011 dsc->numinsns = 2;
7012
7013 dsc->cleanup = &cleanup_block_load_pc;
7014 }
7015 }
7016 else
7017 {
7018 dsc->modinsn[0] = insn1;
7019 dsc->modinsn[1] = insn2;
7020 dsc->numinsns = 2;
7021 dsc->cleanup = &cleanup_block_store_pc;
7022 }
7023 return 0;
7024}
7025
d9311bfa
AT
7026/* Wrapper over read_memory_unsigned_integer for use in arm_get_next_pcs.
7027 This is used to avoid a dependency on BFD's bfd_endian enum. */
7028
7029ULONGEST
7030arm_get_next_pcs_read_memory_unsigned_integer (CORE_ADDR memaddr, int len,
7031 int byte_order)
7032{
5f2dfcfd
AT
7033 return read_memory_unsigned_integer (memaddr, len,
7034 (enum bfd_endian) byte_order);
d9311bfa
AT
7035}
7036
7037/* Wrapper over gdbarch_addr_bits_remove for use in arm_get_next_pcs. */
7038
7039CORE_ADDR
7040arm_get_next_pcs_addr_bits_remove (struct arm_get_next_pcs *self,
7041 CORE_ADDR val)
7042{
ac7936df 7043 return gdbarch_addr_bits_remove (self->regcache->arch (), val);
d9311bfa
AT
7044}
7045
7046/* Wrapper over syscall_next_pc for use in get_next_pcs. */
7047
e7cf25a8 7048static CORE_ADDR
553cb527 7049arm_get_next_pcs_syscall_next_pc (struct arm_get_next_pcs *self)
d9311bfa 7050{
d9311bfa
AT
7051 return 0;
7052}
7053
7054/* Wrapper over arm_is_thumb for use in arm_get_next_pcs. */
7055
7056int
7057arm_get_next_pcs_is_thumb (struct arm_get_next_pcs *self)
7058{
7059 return arm_is_thumb (self->regcache);
7060}
7061
7062/* single_step() is called just before we want to resume the inferior,
7063 if we want to single-step it but there is no hardware or kernel
7064 single-step support. We find the target of the coming instructions
7065 and breakpoint them. */
7066
a0ff9e1a 7067std::vector<CORE_ADDR>
f5ea389a 7068arm_software_single_step (struct regcache *regcache)
d9311bfa 7069{
ac7936df 7070 struct gdbarch *gdbarch = regcache->arch ();
d9311bfa 7071 struct arm_get_next_pcs next_pcs_ctx;
d9311bfa
AT
7072
7073 arm_get_next_pcs_ctor (&next_pcs_ctx,
7074 &arm_get_next_pcs_ops,
7075 gdbarch_byte_order (gdbarch),
7076 gdbarch_byte_order_for_code (gdbarch),
1b451dda 7077 0,
d9311bfa
AT
7078 regcache);
7079
a0ff9e1a 7080 std::vector<CORE_ADDR> next_pcs = arm_get_next_pcs (&next_pcs_ctx);
d9311bfa 7081
a0ff9e1a
SM
7082 for (CORE_ADDR &pc_ref : next_pcs)
7083 pc_ref = gdbarch_addr_bits_remove (gdbarch, pc_ref);
d9311bfa 7084
93f9a11f 7085 return next_pcs;
d9311bfa
AT
7086}
7087
34518530
YQ
7088/* Cleanup/copy SVC (SWI) instructions. These two functions are overridden
7089 for Linux, where some SVC instructions must be treated specially. */
7090
7091static void
7092cleanup_svc (struct gdbarch *gdbarch, struct regcache *regs,
1152d984 7093 arm_displaced_step_copy_insn_closure *dsc)
34518530
YQ
7094{
7095 CORE_ADDR resume_addr = dsc->insn_addr + dsc->insn_size;
7096
136821d9
SM
7097 displaced_debug_printf ("cleanup for svc, resume at %.8lx",
7098 (unsigned long) resume_addr);
34518530
YQ
7099
7100 displaced_write_reg (regs, dsc, ARM_PC_REGNUM, resume_addr, BRANCH_WRITE_PC);
7101}
7102
7103
85102364 7104/* Common copy routine for svc instruction. */
34518530
YQ
7105
7106static int
7107install_svc (struct gdbarch *gdbarch, struct regcache *regs,
1152d984 7108 arm_displaced_step_copy_insn_closure *dsc)
34518530
YQ
7109{
7110 /* Preparation: none.
7111 Insn: unmodified svc.
7112 Cleanup: pc <- insn_addr + insn_size. */
7113
7114 /* Pretend we wrote to the PC, so cleanup doesn't set PC to the next
7115 instruction. */
7116 dsc->wrote_to_pc = 1;
7117
7118 /* Allow OS-specific code to override SVC handling. */
bd18283a
YQ
7119 if (dsc->u.svc.copy_svc_os)
7120 return dsc->u.svc.copy_svc_os (gdbarch, regs, dsc);
7121 else
7122 {
7123 dsc->cleanup = &cleanup_svc;
7124 return 0;
7125 }
34518530
YQ
7126}
7127
7128static int
7129arm_copy_svc (struct gdbarch *gdbarch, uint32_t insn,
1152d984 7130 regcache *regs, arm_displaced_step_copy_insn_closure *dsc)
34518530
YQ
7131{
7132
136821d9
SM
7133 displaced_debug_printf ("copying svc insn %.8lx",
7134 (unsigned long) insn);
34518530
YQ
7135
7136 dsc->modinsn[0] = insn;
7137
7138 return install_svc (gdbarch, regs, dsc);
7139}
7140
7141static int
7142thumb_copy_svc (struct gdbarch *gdbarch, uint16_t insn,
1152d984 7143 regcache *regs, arm_displaced_step_copy_insn_closure *dsc)
34518530
YQ
7144{
7145
136821d9 7146 displaced_debug_printf ("copying svc insn %.4x", insn);
bd18283a 7147
34518530
YQ
7148 dsc->modinsn[0] = insn;
7149
7150 return install_svc (gdbarch, regs, dsc);
cca44b1b
JB
7151}
7152
7153/* Copy undefined instructions. */
7154
7155static int
7ff120b4 7156arm_copy_undef (struct gdbarch *gdbarch, uint32_t insn,
1152d984 7157 arm_displaced_step_copy_insn_closure *dsc)
cca44b1b 7158{
136821d9
SM
7159 displaced_debug_printf ("copying undefined insn %.8lx",
7160 (unsigned long) insn);
cca44b1b
JB
7161
7162 dsc->modinsn[0] = insn;
7163
7164 return 0;
7165}
7166
34518530
YQ
7167static int
7168thumb_32bit_copy_undef (struct gdbarch *gdbarch, uint16_t insn1, uint16_t insn2,
1152d984 7169 arm_displaced_step_copy_insn_closure *dsc)
34518530
YQ
7170{
7171
136821d9
SM
7172 displaced_debug_printf ("copying undefined insn %.4x %.4x",
7173 (unsigned short) insn1, (unsigned short) insn2);
34518530
YQ
7174
7175 dsc->modinsn[0] = insn1;
7176 dsc->modinsn[1] = insn2;
7177 dsc->numinsns = 2;
7178
7179 return 0;
7180}
7181
cca44b1b
JB
7182/* Copy unpredictable instructions. */
7183
7184static int
7ff120b4 7185arm_copy_unpred (struct gdbarch *gdbarch, uint32_t insn,
1152d984 7186 arm_displaced_step_copy_insn_closure *dsc)
cca44b1b 7187{
136821d9
SM
7188 displaced_debug_printf ("copying unpredictable insn %.8lx",
7189 (unsigned long) insn);
cca44b1b
JB
7190
7191 dsc->modinsn[0] = insn;
7192
7193 return 0;
7194}
7195
7196/* The decode_* functions are instruction decoding helpers. They mostly follow
7197 the presentation in the ARM ARM. */
7198
7199static int
7ff120b4
YQ
7200arm_decode_misc_memhint_neon (struct gdbarch *gdbarch, uint32_t insn,
7201 struct regcache *regs,
1152d984 7202 arm_displaced_step_copy_insn_closure *dsc)
cca44b1b
JB
7203{
7204 unsigned int op1 = bits (insn, 20, 26), op2 = bits (insn, 4, 7);
7205 unsigned int rn = bits (insn, 16, 19);
7206
2f924de6 7207 if (op1 == 0x10 && (op2 & 0x2) == 0x0 && (rn & 0x1) == 0x0)
7ff120b4 7208 return arm_copy_unmodified (gdbarch, insn, "cps", dsc);
2f924de6 7209 else if (op1 == 0x10 && op2 == 0x0 && (rn & 0x1) == 0x1)
7ff120b4 7210 return arm_copy_unmodified (gdbarch, insn, "setend", dsc);
cca44b1b 7211 else if ((op1 & 0x60) == 0x20)
7ff120b4 7212 return arm_copy_unmodified (gdbarch, insn, "neon dataproc", dsc);
cca44b1b 7213 else if ((op1 & 0x71) == 0x40)
7ff120b4
YQ
7214 return arm_copy_unmodified (gdbarch, insn, "neon elt/struct load/store",
7215 dsc);
cca44b1b 7216 else if ((op1 & 0x77) == 0x41)
7ff120b4 7217 return arm_copy_unmodified (gdbarch, insn, "unallocated mem hint", dsc);
cca44b1b 7218 else if ((op1 & 0x77) == 0x45)
7ff120b4 7219 return arm_copy_preload (gdbarch, insn, regs, dsc); /* pli. */
cca44b1b
JB
7220 else if ((op1 & 0x77) == 0x51)
7221 {
7222 if (rn != 0xf)
7ff120b4 7223 return arm_copy_preload (gdbarch, insn, regs, dsc); /* pld/pldw. */
cca44b1b 7224 else
7ff120b4 7225 return arm_copy_unpred (gdbarch, insn, dsc);
cca44b1b
JB
7226 }
7227 else if ((op1 & 0x77) == 0x55)
7ff120b4 7228 return arm_copy_preload (gdbarch, insn, regs, dsc); /* pld/pldw. */
cca44b1b
JB
7229 else if (op1 == 0x57)
7230 switch (op2)
7231 {
7ff120b4
YQ
7232 case 0x1: return arm_copy_unmodified (gdbarch, insn, "clrex", dsc);
7233 case 0x4: return arm_copy_unmodified (gdbarch, insn, "dsb", dsc);
7234 case 0x5: return arm_copy_unmodified (gdbarch, insn, "dmb", dsc);
7235 case 0x6: return arm_copy_unmodified (gdbarch, insn, "isb", dsc);
7236 default: return arm_copy_unpred (gdbarch, insn, dsc);
cca44b1b
JB
7237 }
7238 else if ((op1 & 0x63) == 0x43)
7ff120b4 7239 return arm_copy_unpred (gdbarch, insn, dsc);
cca44b1b
JB
7240 else if ((op2 & 0x1) == 0x0)
7241 switch (op1 & ~0x80)
7242 {
7243 case 0x61:
7ff120b4 7244 return arm_copy_unmodified (gdbarch, insn, "unallocated mem hint", dsc);
cca44b1b 7245 case 0x65:
7ff120b4 7246 return arm_copy_preload_reg (gdbarch, insn, regs, dsc); /* pli reg. */
cca44b1b 7247 case 0x71: case 0x75:
dda83cd7 7248 /* pld/pldw reg. */
7ff120b4 7249 return arm_copy_preload_reg (gdbarch, insn, regs, dsc);
cca44b1b 7250 case 0x63: case 0x67: case 0x73: case 0x77:
7ff120b4 7251 return arm_copy_unpred (gdbarch, insn, dsc);
cca44b1b 7252 default:
7ff120b4 7253 return arm_copy_undef (gdbarch, insn, dsc);
cca44b1b
JB
7254 }
7255 else
7ff120b4 7256 return arm_copy_undef (gdbarch, insn, dsc); /* Probably unreachable. */
cca44b1b
JB
7257}
7258
7259static int
7ff120b4
YQ
7260arm_decode_unconditional (struct gdbarch *gdbarch, uint32_t insn,
7261 struct regcache *regs,
1152d984 7262 arm_displaced_step_copy_insn_closure *dsc)
cca44b1b
JB
7263{
7264 if (bit (insn, 27) == 0)
7ff120b4 7265 return arm_decode_misc_memhint_neon (gdbarch, insn, regs, dsc);
cca44b1b
JB
7266 /* Switch on bits: 0bxxxxx321xxx0xxxxxxxxxxxxxxxxxxxx. */
7267 else switch (((insn & 0x7000000) >> 23) | ((insn & 0x100000) >> 20))
7268 {
7269 case 0x0: case 0x2:
7ff120b4 7270 return arm_copy_unmodified (gdbarch, insn, "srs", dsc);
cca44b1b
JB
7271
7272 case 0x1: case 0x3:
7ff120b4 7273 return arm_copy_unmodified (gdbarch, insn, "rfe", dsc);
cca44b1b
JB
7274
7275 case 0x4: case 0x5: case 0x6: case 0x7:
7ff120b4 7276 return arm_copy_b_bl_blx (gdbarch, insn, regs, dsc);
cca44b1b
JB
7277
7278 case 0x8:
7279 switch ((insn & 0xe00000) >> 21)
7280 {
7281 case 0x1: case 0x3: case 0x4: case 0x5: case 0x6: case 0x7:
7282 /* stc/stc2. */
7ff120b4 7283 return arm_copy_copro_load_store (gdbarch, insn, regs, dsc);
cca44b1b
JB
7284
7285 case 0x2:
7ff120b4 7286 return arm_copy_unmodified (gdbarch, insn, "mcrr/mcrr2", dsc);
cca44b1b
JB
7287
7288 default:
7ff120b4 7289 return arm_copy_undef (gdbarch, insn, dsc);
cca44b1b
JB
7290 }
7291
7292 case 0x9:
7293 {
7294 int rn_f = (bits (insn, 16, 19) == 0xf);
7295 switch ((insn & 0xe00000) >> 21)
7296 {
7297 case 0x1: case 0x3:
7298 /* ldc/ldc2 imm (undefined for rn == pc). */
7ff120b4
YQ
7299 return rn_f ? arm_copy_undef (gdbarch, insn, dsc)
7300 : arm_copy_copro_load_store (gdbarch, insn, regs, dsc);
cca44b1b
JB
7301
7302 case 0x2:
7ff120b4 7303 return arm_copy_unmodified (gdbarch, insn, "mrrc/mrrc2", dsc);
cca44b1b
JB
7304
7305 case 0x4: case 0x5: case 0x6: case 0x7:
7306 /* ldc/ldc2 lit (undefined for rn != pc). */
7ff120b4
YQ
7307 return rn_f ? arm_copy_copro_load_store (gdbarch, insn, regs, dsc)
7308 : arm_copy_undef (gdbarch, insn, dsc);
cca44b1b
JB
7309
7310 default:
7ff120b4 7311 return arm_copy_undef (gdbarch, insn, dsc);
cca44b1b
JB
7312 }
7313 }
7314
7315 case 0xa:
7ff120b4 7316 return arm_copy_unmodified (gdbarch, insn, "stc/stc2", dsc);
cca44b1b
JB
7317
7318 case 0xb:
7319 if (bits (insn, 16, 19) == 0xf)
dda83cd7 7320 /* ldc/ldc2 lit. */
7ff120b4 7321 return arm_copy_copro_load_store (gdbarch, insn, regs, dsc);
cca44b1b 7322 else
7ff120b4 7323 return arm_copy_undef (gdbarch, insn, dsc);
cca44b1b
JB
7324
7325 case 0xc:
7326 if (bit (insn, 4))
7ff120b4 7327 return arm_copy_unmodified (gdbarch, insn, "mcr/mcr2", dsc);
cca44b1b 7328 else
7ff120b4 7329 return arm_copy_unmodified (gdbarch, insn, "cdp/cdp2", dsc);
cca44b1b
JB
7330
7331 case 0xd:
7332 if (bit (insn, 4))
7ff120b4 7333 return arm_copy_unmodified (gdbarch, insn, "mrc/mrc2", dsc);
cca44b1b 7334 else
7ff120b4 7335 return arm_copy_unmodified (gdbarch, insn, "cdp/cdp2", dsc);
cca44b1b
JB
7336
7337 default:
7ff120b4 7338 return arm_copy_undef (gdbarch, insn, dsc);
cca44b1b
JB
7339 }
7340}
7341
7342/* Decode miscellaneous instructions in dp/misc encoding space. */
7343
7344static int
7ff120b4
YQ
7345arm_decode_miscellaneous (struct gdbarch *gdbarch, uint32_t insn,
7346 struct regcache *regs,
1152d984 7347 arm_displaced_step_copy_insn_closure *dsc)
cca44b1b
JB
7348{
7349 unsigned int op2 = bits (insn, 4, 6);
7350 unsigned int op = bits (insn, 21, 22);
cca44b1b
JB
7351
7352 switch (op2)
7353 {
7354 case 0x0:
7ff120b4 7355 return arm_copy_unmodified (gdbarch, insn, "mrs/msr", dsc);
cca44b1b
JB
7356
7357 case 0x1:
7358 if (op == 0x1) /* bx. */
7ff120b4 7359 return arm_copy_bx_blx_reg (gdbarch, insn, regs, dsc);
cca44b1b 7360 else if (op == 0x3)
7ff120b4 7361 return arm_copy_unmodified (gdbarch, insn, "clz", dsc);
cca44b1b 7362 else
7ff120b4 7363 return arm_copy_undef (gdbarch, insn, dsc);
cca44b1b
JB
7364
7365 case 0x2:
7366 if (op == 0x1)
dda83cd7 7367 /* Not really supported. */
7ff120b4 7368 return arm_copy_unmodified (gdbarch, insn, "bxj", dsc);
cca44b1b 7369 else
7ff120b4 7370 return arm_copy_undef (gdbarch, insn, dsc);
cca44b1b
JB
7371
7372 case 0x3:
7373 if (op == 0x1)
7ff120b4 7374 return arm_copy_bx_blx_reg (gdbarch, insn,
0963b4bd 7375 regs, dsc); /* blx register. */
cca44b1b 7376 else
7ff120b4 7377 return arm_copy_undef (gdbarch, insn, dsc);
cca44b1b
JB
7378
7379 case 0x5:
7ff120b4 7380 return arm_copy_unmodified (gdbarch, insn, "saturating add/sub", dsc);
cca44b1b
JB
7381
7382 case 0x7:
7383 if (op == 0x1)
7ff120b4 7384 return arm_copy_unmodified (gdbarch, insn, "bkpt", dsc);
cca44b1b 7385 else if (op == 0x3)
dda83cd7 7386 /* Not really supported. */
7ff120b4 7387 return arm_copy_unmodified (gdbarch, insn, "smc", dsc);
86a73007 7388 /* Fall through. */
cca44b1b
JB
7389
7390 default:
7ff120b4 7391 return arm_copy_undef (gdbarch, insn, dsc);
cca44b1b
JB
7392 }
7393}
7394
7395static int
7ff120b4
YQ
7396arm_decode_dp_misc (struct gdbarch *gdbarch, uint32_t insn,
7397 struct regcache *regs,
1152d984 7398 arm_displaced_step_copy_insn_closure *dsc)
cca44b1b
JB
7399{
7400 if (bit (insn, 25))
7401 switch (bits (insn, 20, 24))
7402 {
7403 case 0x10:
7ff120b4 7404 return arm_copy_unmodified (gdbarch, insn, "movw", dsc);
cca44b1b
JB
7405
7406 case 0x14:
7ff120b4 7407 return arm_copy_unmodified (gdbarch, insn, "movt", dsc);
cca44b1b
JB
7408
7409 case 0x12: case 0x16:
7ff120b4 7410 return arm_copy_unmodified (gdbarch, insn, "msr imm", dsc);
cca44b1b
JB
7411
7412 default:
7ff120b4 7413 return arm_copy_alu_imm (gdbarch, insn, regs, dsc);
cca44b1b
JB
7414 }
7415 else
7416 {
7417 uint32_t op1 = bits (insn, 20, 24), op2 = bits (insn, 4, 7);
7418
7419 if ((op1 & 0x19) != 0x10 && (op2 & 0x1) == 0x0)
7ff120b4 7420 return arm_copy_alu_reg (gdbarch, insn, regs, dsc);
cca44b1b 7421 else if ((op1 & 0x19) != 0x10 && (op2 & 0x9) == 0x1)
7ff120b4 7422 return arm_copy_alu_shifted_reg (gdbarch, insn, regs, dsc);
cca44b1b 7423 else if ((op1 & 0x19) == 0x10 && (op2 & 0x8) == 0x0)
7ff120b4 7424 return arm_decode_miscellaneous (gdbarch, insn, regs, dsc);
cca44b1b 7425 else if ((op1 & 0x19) == 0x10 && (op2 & 0x9) == 0x8)
7ff120b4 7426 return arm_copy_unmodified (gdbarch, insn, "halfword mul/mla", dsc);
cca44b1b 7427 else if ((op1 & 0x10) == 0x00 && op2 == 0x9)
7ff120b4 7428 return arm_copy_unmodified (gdbarch, insn, "mul/mla", dsc);
cca44b1b 7429 else if ((op1 & 0x10) == 0x10 && op2 == 0x9)
7ff120b4 7430 return arm_copy_unmodified (gdbarch, insn, "synch", dsc);
cca44b1b 7431 else if (op2 == 0xb || (op2 & 0xd) == 0xd)
550dc4e2 7432 /* 2nd arg means "unprivileged". */
7ff120b4
YQ
7433 return arm_copy_extra_ld_st (gdbarch, insn, (op1 & 0x12) == 0x02, regs,
7434 dsc);
cca44b1b
JB
7435 }
7436
7437 /* Should be unreachable. */
7438 return 1;
7439}
7440
7441static int
7ff120b4
YQ
7442arm_decode_ld_st_word_ubyte (struct gdbarch *gdbarch, uint32_t insn,
7443 struct regcache *regs,
1152d984 7444 arm_displaced_step_copy_insn_closure *dsc)
cca44b1b
JB
7445{
7446 int a = bit (insn, 25), b = bit (insn, 4);
7447 uint32_t op1 = bits (insn, 20, 24);
cca44b1b
JB
7448
7449 if ((!a && (op1 & 0x05) == 0x00 && (op1 & 0x17) != 0x02)
7450 || (a && (op1 & 0x05) == 0x00 && (op1 & 0x17) != 0x02 && !b))
0f6f04ba 7451 return arm_copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 0, 4, 0);
cca44b1b
JB
7452 else if ((!a && (op1 & 0x17) == 0x02)
7453 || (a && (op1 & 0x17) == 0x02 && !b))
0f6f04ba 7454 return arm_copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 0, 4, 1);
cca44b1b
JB
7455 else if ((!a && (op1 & 0x05) == 0x01 && (op1 & 0x17) != 0x03)
7456 || (a && (op1 & 0x05) == 0x01 && (op1 & 0x17) != 0x03 && !b))
0f6f04ba 7457 return arm_copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 1, 4, 0);
cca44b1b
JB
7458 else if ((!a && (op1 & 0x17) == 0x03)
7459 || (a && (op1 & 0x17) == 0x03 && !b))
0f6f04ba 7460 return arm_copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 1, 4, 1);
cca44b1b
JB
7461 else if ((!a && (op1 & 0x05) == 0x04 && (op1 & 0x17) != 0x06)
7462 || (a && (op1 & 0x05) == 0x04 && (op1 & 0x17) != 0x06 && !b))
7ff120b4 7463 return arm_copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 0, 1, 0);
cca44b1b
JB
7464 else if ((!a && (op1 & 0x17) == 0x06)
7465 || (a && (op1 & 0x17) == 0x06 && !b))
7ff120b4 7466 return arm_copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 0, 1, 1);
cca44b1b
JB
7467 else if ((!a && (op1 & 0x05) == 0x05 && (op1 & 0x17) != 0x07)
7468 || (a && (op1 & 0x05) == 0x05 && (op1 & 0x17) != 0x07 && !b))
7ff120b4 7469 return arm_copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 1, 1, 0);
cca44b1b
JB
7470 else if ((!a && (op1 & 0x17) == 0x07)
7471 || (a && (op1 & 0x17) == 0x07 && !b))
7ff120b4 7472 return arm_copy_ldr_str_ldrb_strb (gdbarch, insn, regs, dsc, 1, 1, 1);
cca44b1b
JB
7473
7474 /* Should be unreachable. */
7475 return 1;
7476}
7477
7478static int
7ff120b4 7479arm_decode_media (struct gdbarch *gdbarch, uint32_t insn,
1152d984 7480 arm_displaced_step_copy_insn_closure *dsc)
cca44b1b
JB
7481{
7482 switch (bits (insn, 20, 24))
7483 {
7484 case 0x00: case 0x01: case 0x02: case 0x03:
7ff120b4 7485 return arm_copy_unmodified (gdbarch, insn, "parallel add/sub signed", dsc);
cca44b1b
JB
7486
7487 case 0x04: case 0x05: case 0x06: case 0x07:
7ff120b4 7488 return arm_copy_unmodified (gdbarch, insn, "parallel add/sub unsigned", dsc);
cca44b1b
JB
7489
7490 case 0x08: case 0x09: case 0x0a: case 0x0b:
7491 case 0x0c: case 0x0d: case 0x0e: case 0x0f:
7ff120b4 7492 return arm_copy_unmodified (gdbarch, insn,
cca44b1b
JB
7493 "decode/pack/unpack/saturate/reverse", dsc);
7494
7495 case 0x18:
7496 if (bits (insn, 5, 7) == 0) /* op2. */
7497 {
7498 if (bits (insn, 12, 15) == 0xf)
7ff120b4 7499 return arm_copy_unmodified (gdbarch, insn, "usad8", dsc);
cca44b1b 7500 else
7ff120b4 7501 return arm_copy_unmodified (gdbarch, insn, "usada8", dsc);
cca44b1b
JB
7502 }
7503 else
7ff120b4 7504 return arm_copy_undef (gdbarch, insn, dsc);
cca44b1b
JB
7505
7506 case 0x1a: case 0x1b:
7507 if (bits (insn, 5, 6) == 0x2) /* op2[1:0]. */
7ff120b4 7508 return arm_copy_unmodified (gdbarch, insn, "sbfx", dsc);
cca44b1b 7509 else
7ff120b4 7510 return arm_copy_undef (gdbarch, insn, dsc);
cca44b1b
JB
7511
7512 case 0x1c: case 0x1d:
7513 if (bits (insn, 5, 6) == 0x0) /* op2[1:0]. */
7514 {
7515 if (bits (insn, 0, 3) == 0xf)
7ff120b4 7516 return arm_copy_unmodified (gdbarch, insn, "bfc", dsc);
cca44b1b 7517 else
7ff120b4 7518 return arm_copy_unmodified (gdbarch, insn, "bfi", dsc);
cca44b1b
JB
7519 }
7520 else
7ff120b4 7521 return arm_copy_undef (gdbarch, insn, dsc);
cca44b1b
JB
7522
7523 case 0x1e: case 0x1f:
7524 if (bits (insn, 5, 6) == 0x2) /* op2[1:0]. */
7ff120b4 7525 return arm_copy_unmodified (gdbarch, insn, "ubfx", dsc);
cca44b1b 7526 else
7ff120b4 7527 return arm_copy_undef (gdbarch, insn, dsc);
cca44b1b
JB
7528 }
7529
7530 /* Should be unreachable. */
7531 return 1;
7532}
7533
7534static int
615234c1 7535arm_decode_b_bl_ldmstm (struct gdbarch *gdbarch, uint32_t insn,
7ff120b4 7536 struct regcache *regs,
1152d984 7537 arm_displaced_step_copy_insn_closure *dsc)
cca44b1b
JB
7538{
7539 if (bit (insn, 25))
7ff120b4 7540 return arm_copy_b_bl_blx (gdbarch, insn, regs, dsc);
cca44b1b 7541 else
7ff120b4 7542 return arm_copy_block_xfer (gdbarch, insn, regs, dsc);
cca44b1b
JB
7543}
7544
7545static int
7ff120b4
YQ
7546arm_decode_ext_reg_ld_st (struct gdbarch *gdbarch, uint32_t insn,
7547 struct regcache *regs,
1152d984 7548 arm_displaced_step_copy_insn_closure *dsc)
cca44b1b
JB
7549{
7550 unsigned int opcode = bits (insn, 20, 24);
7551
7552 switch (opcode)
7553 {
7554 case 0x04: case 0x05: /* VFP/Neon mrrc/mcrr. */
7ff120b4 7555 return arm_copy_unmodified (gdbarch, insn, "vfp/neon mrrc/mcrr", dsc);
cca44b1b
JB
7556
7557 case 0x08: case 0x0a: case 0x0c: case 0x0e:
7558 case 0x12: case 0x16:
7ff120b4 7559 return arm_copy_unmodified (gdbarch, insn, "vfp/neon vstm/vpush", dsc);
cca44b1b
JB
7560
7561 case 0x09: case 0x0b: case 0x0d: case 0x0f:
7562 case 0x13: case 0x17:
7ff120b4 7563 return arm_copy_unmodified (gdbarch, insn, "vfp/neon vldm/vpop", dsc);
cca44b1b
JB
7564
7565 case 0x10: case 0x14: case 0x18: case 0x1c: /* vstr. */
7566 case 0x11: case 0x15: case 0x19: case 0x1d: /* vldr. */
7567 /* Note: no writeback for these instructions. Bit 25 will always be
7568 zero though (via caller), so the following works OK. */
7ff120b4 7569 return arm_copy_copro_load_store (gdbarch, insn, regs, dsc);
cca44b1b
JB
7570 }
7571
7572 /* Should be unreachable. */
7573 return 1;
7574}
7575
34518530
YQ
7576/* Decode shifted register instructions. */
7577
7578static int
7579thumb2_decode_dp_shift_reg (struct gdbarch *gdbarch, uint16_t insn1,
7580 uint16_t insn2, struct regcache *regs,
1152d984 7581 arm_displaced_step_copy_insn_closure *dsc)
34518530
YQ
7582{
7583 /* PC is only allowed to be used in instruction MOV. */
7584
7585 unsigned int op = bits (insn1, 5, 8);
7586 unsigned int rn = bits (insn1, 0, 3);
7587
7588 if (op == 0x2 && rn == 0xf) /* MOV */
7589 return thumb2_copy_alu_imm (gdbarch, insn1, insn2, regs, dsc);
7590 else
7591 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
7592 "dp (shift reg)", dsc);
7593}
7594
7595
7596/* Decode extension register load/store. Exactly the same as
7597 arm_decode_ext_reg_ld_st. */
7598
7599static int
7600thumb2_decode_ext_reg_ld_st (struct gdbarch *gdbarch, uint16_t insn1,
7601 uint16_t insn2, struct regcache *regs,
1152d984 7602 arm_displaced_step_copy_insn_closure *dsc)
34518530
YQ
7603{
7604 unsigned int opcode = bits (insn1, 4, 8);
7605
7606 switch (opcode)
7607 {
7608 case 0x04: case 0x05:
7609 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
7610 "vfp/neon vmov", dsc);
7611
7612 case 0x08: case 0x0c: /* 01x00 */
7613 case 0x0a: case 0x0e: /* 01x10 */
7614 case 0x12: case 0x16: /* 10x10 */
7615 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
7616 "vfp/neon vstm/vpush", dsc);
7617
7618 case 0x09: case 0x0d: /* 01x01 */
7619 case 0x0b: case 0x0f: /* 01x11 */
7620 case 0x13: case 0x17: /* 10x11 */
7621 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
7622 "vfp/neon vldm/vpop", dsc);
7623
7624 case 0x10: case 0x14: case 0x18: case 0x1c: /* vstr. */
7625 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
7626 "vstr", dsc);
7627 case 0x11: case 0x15: case 0x19: case 0x1d: /* vldr. */
7628 return thumb2_copy_copro_load_store (gdbarch, insn1, insn2, regs, dsc);
7629 }
7630
7631 /* Should be unreachable. */
7632 return 1;
7633}
7634
cca44b1b 7635static int
12545665 7636arm_decode_svc_copro (struct gdbarch *gdbarch, uint32_t insn,
1152d984 7637 regcache *regs, arm_displaced_step_copy_insn_closure *dsc)
cca44b1b
JB
7638{
7639 unsigned int op1 = bits (insn, 20, 25);
7640 int op = bit (insn, 4);
7641 unsigned int coproc = bits (insn, 8, 11);
cca44b1b
JB
7642
7643 if ((op1 & 0x20) == 0x00 && (op1 & 0x3a) != 0x00 && (coproc & 0xe) == 0xa)
7ff120b4 7644 return arm_decode_ext_reg_ld_st (gdbarch, insn, regs, dsc);
cca44b1b
JB
7645 else if ((op1 & 0x21) == 0x00 && (op1 & 0x3a) != 0x00
7646 && (coproc & 0xe) != 0xa)
7647 /* stc/stc2. */
7ff120b4 7648 return arm_copy_copro_load_store (gdbarch, insn, regs, dsc);
cca44b1b
JB
7649 else if ((op1 & 0x21) == 0x01 && (op1 & 0x3a) != 0x00
7650 && (coproc & 0xe) != 0xa)
7651 /* ldc/ldc2 imm/lit. */
7ff120b4 7652 return arm_copy_copro_load_store (gdbarch, insn, regs, dsc);
cca44b1b 7653 else if ((op1 & 0x3e) == 0x00)
7ff120b4 7654 return arm_copy_undef (gdbarch, insn, dsc);
cca44b1b 7655 else if ((op1 & 0x3e) == 0x04 && (coproc & 0xe) == 0xa)
7ff120b4 7656 return arm_copy_unmodified (gdbarch, insn, "neon 64bit xfer", dsc);
cca44b1b 7657 else if (op1 == 0x04 && (coproc & 0xe) != 0xa)
7ff120b4 7658 return arm_copy_unmodified (gdbarch, insn, "mcrr/mcrr2", dsc);
cca44b1b 7659 else if (op1 == 0x05 && (coproc & 0xe) != 0xa)
7ff120b4 7660 return arm_copy_unmodified (gdbarch, insn, "mrrc/mrrc2", dsc);
cca44b1b
JB
7661 else if ((op1 & 0x30) == 0x20 && !op)
7662 {
7663 if ((coproc & 0xe) == 0xa)
7ff120b4 7664 return arm_copy_unmodified (gdbarch, insn, "vfp dataproc", dsc);
cca44b1b 7665 else
7ff120b4 7666 return arm_copy_unmodified (gdbarch, insn, "cdp/cdp2", dsc);
cca44b1b
JB
7667 }
7668 else if ((op1 & 0x30) == 0x20 && op)
7ff120b4 7669 return arm_copy_unmodified (gdbarch, insn, "neon 8/16/32 bit xfer", dsc);
cca44b1b 7670 else if ((op1 & 0x31) == 0x20 && op && (coproc & 0xe) != 0xa)
7ff120b4 7671 return arm_copy_unmodified (gdbarch, insn, "mcr/mcr2", dsc);
cca44b1b 7672 else if ((op1 & 0x31) == 0x21 && op && (coproc & 0xe) != 0xa)
7ff120b4 7673 return arm_copy_unmodified (gdbarch, insn, "mrc/mrc2", dsc);
cca44b1b 7674 else if ((op1 & 0x30) == 0x30)
7ff120b4 7675 return arm_copy_svc (gdbarch, insn, regs, dsc);
cca44b1b 7676 else
7ff120b4 7677 return arm_copy_undef (gdbarch, insn, dsc); /* Possibly unreachable. */
cca44b1b
JB
7678}
7679
34518530
YQ
7680static int
7681thumb2_decode_svc_copro (struct gdbarch *gdbarch, uint16_t insn1,
7682 uint16_t insn2, struct regcache *regs,
1152d984 7683 arm_displaced_step_copy_insn_closure *dsc)
34518530
YQ
7684{
7685 unsigned int coproc = bits (insn2, 8, 11);
34518530
YQ
7686 unsigned int bit_5_8 = bits (insn1, 5, 8);
7687 unsigned int bit_9 = bit (insn1, 9);
7688 unsigned int bit_4 = bit (insn1, 4);
34518530
YQ
7689
7690 if (bit_9 == 0)
7691 {
7692 if (bit_5_8 == 2)
7693 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
7694 "neon 64bit xfer/mrrc/mrrc2/mcrr/mcrr2",
7695 dsc);
7696 else if (bit_5_8 == 0) /* UNDEFINED. */
7697 return thumb_32bit_copy_undef (gdbarch, insn1, insn2, dsc);
7698 else
7699 {
7700 /*coproc is 101x. SIMD/VFP, ext registers load/store. */
7701 if ((coproc & 0xe) == 0xa)
7702 return thumb2_decode_ext_reg_ld_st (gdbarch, insn1, insn2, regs,
7703 dsc);
7704 else /* coproc is not 101x. */
7705 {
7706 if (bit_4 == 0) /* STC/STC2. */
7707 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
7708 "stc/stc2", dsc);
405feb71 7709 else /* LDC/LDC2 {literal, immediate}. */
34518530
YQ
7710 return thumb2_copy_copro_load_store (gdbarch, insn1, insn2,
7711 regs, dsc);
7712 }
7713 }
7714 }
7715 else
7716 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2, "coproc", dsc);
7717
7718 return 0;
7719}
7720
7721static void
7722install_pc_relative (struct gdbarch *gdbarch, struct regcache *regs,
1152d984 7723 arm_displaced_step_copy_insn_closure *dsc, int rd)
34518530
YQ
7724{
7725 /* ADR Rd, #imm
7726
7727 Rewrite as:
7728
7729 Preparation: Rd <- PC
7730 Insn: ADD Rd, #imm
7731 Cleanup: Null.
7732 */
7733
7734 /* Rd <- PC */
7735 int val = displaced_read_reg (regs, dsc, ARM_PC_REGNUM);
7736 displaced_write_reg (regs, dsc, rd, val, CANNOT_WRITE_PC);
7737}
7738
7739static int
7740thumb_copy_pc_relative_16bit (struct gdbarch *gdbarch, struct regcache *regs,
1152d984 7741 arm_displaced_step_copy_insn_closure *dsc,
34518530
YQ
7742 int rd, unsigned int imm)
7743{
7744
7745 /* Encoding T2: ADDS Rd, #imm */
7746 dsc->modinsn[0] = (0x3000 | (rd << 8) | imm);
7747
7748 install_pc_relative (gdbarch, regs, dsc, rd);
7749
7750 return 0;
7751}
7752
7753static int
7754thumb_decode_pc_relative_16bit (struct gdbarch *gdbarch, uint16_t insn,
7755 struct regcache *regs,
1152d984 7756 arm_displaced_step_copy_insn_closure *dsc)
34518530
YQ
7757{
7758 unsigned int rd = bits (insn, 8, 10);
7759 unsigned int imm8 = bits (insn, 0, 7);
7760
136821d9
SM
7761 displaced_debug_printf ("copying thumb adr r%d, #%d insn %.4x",
7762 rd, imm8, insn);
34518530
YQ
7763
7764 return thumb_copy_pc_relative_16bit (gdbarch, regs, dsc, rd, imm8);
7765}
7766
7767static int
7768thumb_copy_pc_relative_32bit (struct gdbarch *gdbarch, uint16_t insn1,
7769 uint16_t insn2, struct regcache *regs,
1152d984 7770 arm_displaced_step_copy_insn_closure *dsc)
34518530
YQ
7771{
7772 unsigned int rd = bits (insn2, 8, 11);
7773 /* Since immediate has the same encoding in ADR ADD and SUB, so we simply
7774 extract raw immediate encoding rather than computing immediate. When
7775 generating ADD or SUB instruction, we can simply perform OR operation to
7776 set immediate into ADD. */
7777 unsigned int imm_3_8 = insn2 & 0x70ff;
7778 unsigned int imm_i = insn1 & 0x0400; /* Clear all bits except bit 10. */
7779
136821d9
SM
7780 displaced_debug_printf ("copying thumb adr r%d, #%d:%d insn %.4x%.4x",
7781 rd, imm_i, imm_3_8, insn1, insn2);
34518530
YQ
7782
7783 if (bit (insn1, 7)) /* Encoding T2 */
7784 {
7785 /* Encoding T3: SUB Rd, Rd, #imm */
7786 dsc->modinsn[0] = (0xf1a0 | rd | imm_i);
7787 dsc->modinsn[1] = ((rd << 8) | imm_3_8);
7788 }
7789 else /* Encoding T3 */
7790 {
7791 /* Encoding T3: ADD Rd, Rd, #imm */
7792 dsc->modinsn[0] = (0xf100 | rd | imm_i);
7793 dsc->modinsn[1] = ((rd << 8) | imm_3_8);
7794 }
7795 dsc->numinsns = 2;
7796
7797 install_pc_relative (gdbarch, regs, dsc, rd);
7798
7799 return 0;
7800}
7801
7802static int
615234c1 7803thumb_copy_16bit_ldr_literal (struct gdbarch *gdbarch, uint16_t insn1,
34518530 7804 struct regcache *regs,
1152d984 7805 arm_displaced_step_copy_insn_closure *dsc)
34518530
YQ
7806{
7807 unsigned int rt = bits (insn1, 8, 10);
7808 unsigned int pc;
7809 int imm8 = (bits (insn1, 0, 7) << 2);
34518530
YQ
7810
7811 /* LDR Rd, #imm8
7812
7813 Rwrite as:
7814
7815 Preparation: tmp0 <- R0, tmp2 <- R2, tmp3 <- R3, R2 <- PC, R3 <- #imm8;
7816
7817 Insn: LDR R0, [R2, R3];
7818 Cleanup: R2 <- tmp2, R3 <- tmp3, Rd <- R0, R0 <- tmp0 */
7819
136821d9 7820 displaced_debug_printf ("copying thumb ldr r%d [pc #%d]", rt, imm8);
34518530
YQ
7821
7822 dsc->tmp[0] = displaced_read_reg (regs, dsc, 0);
7823 dsc->tmp[2] = displaced_read_reg (regs, dsc, 2);
7824 dsc->tmp[3] = displaced_read_reg (regs, dsc, 3);
7825 pc = displaced_read_reg (regs, dsc, ARM_PC_REGNUM);
7826 /* The assembler calculates the required value of the offset from the
7827 Align(PC,4) value of this instruction to the label. */
7828 pc = pc & 0xfffffffc;
7829
7830 displaced_write_reg (regs, dsc, 2, pc, CANNOT_WRITE_PC);
7831 displaced_write_reg (regs, dsc, 3, imm8, CANNOT_WRITE_PC);
7832
7833 dsc->rd = rt;
7834 dsc->u.ldst.xfersize = 4;
7835 dsc->u.ldst.rn = 0;
7836 dsc->u.ldst.immed = 0;
7837 dsc->u.ldst.writeback = 0;
7838 dsc->u.ldst.restore_r4 = 0;
7839
7840 dsc->modinsn[0] = 0x58d0; /* ldr r0, [r2, r3]*/
7841
7842 dsc->cleanup = &cleanup_load;
7843
7844 return 0;
7845}
7846
405feb71 7847/* Copy Thumb cbnz/cbz instruction. */
34518530
YQ
7848
7849static int
7850thumb_copy_cbnz_cbz (struct gdbarch *gdbarch, uint16_t insn1,
7851 struct regcache *regs,
1152d984 7852 arm_displaced_step_copy_insn_closure *dsc)
34518530
YQ
7853{
7854 int non_zero = bit (insn1, 11);
7855 unsigned int imm5 = (bit (insn1, 9) << 6) | (bits (insn1, 3, 7) << 1);
7856 CORE_ADDR from = dsc->insn_addr;
7857 int rn = bits (insn1, 0, 2);
7858 int rn_val = displaced_read_reg (regs, dsc, rn);
7859
7860 dsc->u.branch.cond = (rn_val && non_zero) || (!rn_val && !non_zero);
7861 /* CBNZ and CBZ do not affect the condition flags. If condition is true,
7862 set it INST_AL, so cleanup_branch will know branch is taken, otherwise,
7863 condition is false, let it be, cleanup_branch will do nothing. */
7864 if (dsc->u.branch.cond)
7865 {
7866 dsc->u.branch.cond = INST_AL;
7867 dsc->u.branch.dest = from + 4 + imm5;
7868 }
7869 else
7870 dsc->u.branch.dest = from + 2;
7871
7872 dsc->u.branch.link = 0;
7873 dsc->u.branch.exchange = 0;
7874
136821d9
SM
7875 displaced_debug_printf ("copying %s [r%d = 0x%x] insn %.4x to %.8lx",
7876 non_zero ? "cbnz" : "cbz",
7877 rn, rn_val, insn1, dsc->u.branch.dest);
34518530
YQ
7878
7879 dsc->modinsn[0] = THUMB_NOP;
7880
7881 dsc->cleanup = &cleanup_branch;
7882 return 0;
7883}
7884
7885/* Copy Table Branch Byte/Halfword */
7886static int
7887thumb2_copy_table_branch (struct gdbarch *gdbarch, uint16_t insn1,
7888 uint16_t insn2, struct regcache *regs,
1152d984 7889 arm_displaced_step_copy_insn_closure *dsc)
34518530
YQ
7890{
7891 ULONGEST rn_val, rm_val;
7892 int is_tbh = bit (insn2, 4);
7893 CORE_ADDR halfwords = 0;
7894 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
7895
7896 rn_val = displaced_read_reg (regs, dsc, bits (insn1, 0, 3));
7897 rm_val = displaced_read_reg (regs, dsc, bits (insn2, 0, 3));
7898
7899 if (is_tbh)
7900 {
7901 gdb_byte buf[2];
7902
7903 target_read_memory (rn_val + 2 * rm_val, buf, 2);
7904 halfwords = extract_unsigned_integer (buf, 2, byte_order);
7905 }
7906 else
7907 {
7908 gdb_byte buf[1];
7909
7910 target_read_memory (rn_val + rm_val, buf, 1);
7911 halfwords = extract_unsigned_integer (buf, 1, byte_order);
7912 }
7913
136821d9
SM
7914 displaced_debug_printf ("%s base 0x%x offset 0x%x offset 0x%x",
7915 is_tbh ? "tbh" : "tbb",
7916 (unsigned int) rn_val, (unsigned int) rm_val,
7917 (unsigned int) halfwords);
34518530
YQ
7918
7919 dsc->u.branch.cond = INST_AL;
7920 dsc->u.branch.link = 0;
7921 dsc->u.branch.exchange = 0;
7922 dsc->u.branch.dest = dsc->insn_addr + 4 + 2 * halfwords;
7923
7924 dsc->cleanup = &cleanup_branch;
7925
7926 return 0;
7927}
7928
7929static void
7930cleanup_pop_pc_16bit_all (struct gdbarch *gdbarch, struct regcache *regs,
1152d984 7931 arm_displaced_step_copy_insn_closure *dsc)
34518530
YQ
7932{
7933 /* PC <- r7 */
7934 int val = displaced_read_reg (regs, dsc, 7);
7935 displaced_write_reg (regs, dsc, ARM_PC_REGNUM, val, BX_WRITE_PC);
7936
7937 /* r7 <- r8 */
7938 val = displaced_read_reg (regs, dsc, 8);
7939 displaced_write_reg (regs, dsc, 7, val, CANNOT_WRITE_PC);
7940
7941 /* r8 <- tmp[0] */
7942 displaced_write_reg (regs, dsc, 8, dsc->tmp[0], CANNOT_WRITE_PC);
7943
7944}
7945
7946static int
615234c1 7947thumb_copy_pop_pc_16bit (struct gdbarch *gdbarch, uint16_t insn1,
34518530 7948 struct regcache *regs,
1152d984 7949 arm_displaced_step_copy_insn_closure *dsc)
34518530
YQ
7950{
7951 dsc->u.block.regmask = insn1 & 0x00ff;
7952
7953 /* Rewrite instruction: POP {rX, rY, ...,rZ, PC}
7954 to :
7955
7956 (1) register list is full, that is, r0-r7 are used.
7957 Prepare: tmp[0] <- r8
7958
7959 POP {r0, r1, ...., r6, r7}; remove PC from reglist
7960 MOV r8, r7; Move value of r7 to r8;
7961 POP {r7}; Store PC value into r7.
7962
7963 Cleanup: PC <- r7, r7 <- r8, r8 <-tmp[0]
7964
7965 (2) register list is not full, supposing there are N registers in
7966 register list (except PC, 0 <= N <= 7).
7967 Prepare: for each i, 0 - N, tmp[i] <- ri.
7968
7969 POP {r0, r1, ...., rN};
7970
7971 Cleanup: Set registers in original reglist from r0 - rN. Restore r0 - rN
7972 from tmp[] properly.
7973 */
136821d9
SM
7974 displaced_debug_printf ("copying thumb pop {%.8x, pc} insn %.4x",
7975 dsc->u.block.regmask, insn1);
34518530
YQ
7976
7977 if (dsc->u.block.regmask == 0xff)
7978 {
7979 dsc->tmp[0] = displaced_read_reg (regs, dsc, 8);
7980
7981 dsc->modinsn[0] = (insn1 & 0xfeff); /* POP {r0,r1,...,r6, r7} */
7982 dsc->modinsn[1] = 0x46b8; /* MOV r8, r7 */
7983 dsc->modinsn[2] = 0xbc80; /* POP {r7} */
7984
7985 dsc->numinsns = 3;
7986 dsc->cleanup = &cleanup_pop_pc_16bit_all;
7987 }
7988 else
7989 {
5f661e03 7990 unsigned int num_in_list = count_one_bits (dsc->u.block.regmask);
bec2ab5a
SM
7991 unsigned int i;
7992 unsigned int new_regmask;
34518530
YQ
7993
7994 for (i = 0; i < num_in_list + 1; i++)
7995 dsc->tmp[i] = displaced_read_reg (regs, dsc, i);
7996
7997 new_regmask = (1 << (num_in_list + 1)) - 1;
7998
136821d9
SM
7999 displaced_debug_printf ("POP {..., pc}: original reg list %.4x, "
8000 "modified list %.4x",
8001 (int) dsc->u.block.regmask, new_regmask);
34518530
YQ
8002
8003 dsc->u.block.regmask |= 0x8000;
8004 dsc->u.block.writeback = 0;
8005 dsc->u.block.cond = INST_AL;
8006
8007 dsc->modinsn[0] = (insn1 & ~0x1ff) | (new_regmask & 0xff);
8008
8009 dsc->cleanup = &cleanup_block_load_pc;
8010 }
8011
8012 return 0;
8013}
8014
8015static void
8016thumb_process_displaced_16bit_insn (struct gdbarch *gdbarch, uint16_t insn1,
8017 struct regcache *regs,
1152d984 8018 arm_displaced_step_copy_insn_closure *dsc)
34518530
YQ
8019{
8020 unsigned short op_bit_12_15 = bits (insn1, 12, 15);
8021 unsigned short op_bit_10_11 = bits (insn1, 10, 11);
8022 int err = 0;
8023
8024 /* 16-bit thumb instructions. */
8025 switch (op_bit_12_15)
8026 {
8027 /* Shift (imme), add, subtract, move and compare. */
8028 case 0: case 1: case 2: case 3:
8029 err = thumb_copy_unmodified_16bit (gdbarch, insn1,
8030 "shift/add/sub/mov/cmp",
8031 dsc);
8032 break;
8033 case 4:
8034 switch (op_bit_10_11)
8035 {
8036 case 0: /* Data-processing */
8037 err = thumb_copy_unmodified_16bit (gdbarch, insn1,
8038 "data-processing",
8039 dsc);
8040 break;
8041 case 1: /* Special data instructions and branch and exchange. */
8042 {
8043 unsigned short op = bits (insn1, 7, 9);
8044 if (op == 6 || op == 7) /* BX or BLX */
8045 err = thumb_copy_bx_blx_reg (gdbarch, insn1, regs, dsc);
8046 else if (bits (insn1, 6, 7) != 0) /* ADD/MOV/CMP high registers. */
8047 err = thumb_copy_alu_reg (gdbarch, insn1, regs, dsc);
8048 else
8049 err = thumb_copy_unmodified_16bit (gdbarch, insn1, "special data",
8050 dsc);
8051 }
8052 break;
8053 default: /* LDR (literal) */
8054 err = thumb_copy_16bit_ldr_literal (gdbarch, insn1, regs, dsc);
8055 }
8056 break;
8057 case 5: case 6: case 7: case 8: case 9: /* Load/Store single data item */
8058 err = thumb_copy_unmodified_16bit (gdbarch, insn1, "ldr/str", dsc);
8059 break;
8060 case 10:
8061 if (op_bit_10_11 < 2) /* Generate PC-relative address */
8062 err = thumb_decode_pc_relative_16bit (gdbarch, insn1, regs, dsc);
8063 else /* Generate SP-relative address */
8064 err = thumb_copy_unmodified_16bit (gdbarch, insn1, "sp-relative", dsc);
8065 break;
8066 case 11: /* Misc 16-bit instructions */
8067 {
8068 switch (bits (insn1, 8, 11))
8069 {
8070 case 1: case 3: case 9: case 11: /* CBNZ, CBZ */
8071 err = thumb_copy_cbnz_cbz (gdbarch, insn1, regs, dsc);
8072 break;
8073 case 12: case 13: /* POP */
8074 if (bit (insn1, 8)) /* PC is in register list. */
8075 err = thumb_copy_pop_pc_16bit (gdbarch, insn1, regs, dsc);
8076 else
8077 err = thumb_copy_unmodified_16bit (gdbarch, insn1, "pop", dsc);
8078 break;
8079 case 15: /* If-Then, and hints */
8080 if (bits (insn1, 0, 3))
8081 /* If-Then makes up to four following instructions conditional.
8082 IT instruction itself is not conditional, so handle it as a
8083 common unmodified instruction. */
8084 err = thumb_copy_unmodified_16bit (gdbarch, insn1, "If-Then",
8085 dsc);
8086 else
8087 err = thumb_copy_unmodified_16bit (gdbarch, insn1, "hints", dsc);
8088 break;
8089 default:
8090 err = thumb_copy_unmodified_16bit (gdbarch, insn1, "misc", dsc);
8091 }
8092 }
8093 break;
8094 case 12:
8095 if (op_bit_10_11 < 2) /* Store multiple registers */
8096 err = thumb_copy_unmodified_16bit (gdbarch, insn1, "stm", dsc);
8097 else /* Load multiple registers */
8098 err = thumb_copy_unmodified_16bit (gdbarch, insn1, "ldm", dsc);
8099 break;
8100 case 13: /* Conditional branch and supervisor call */
8101 if (bits (insn1, 9, 11) != 7) /* conditional branch */
8102 err = thumb_copy_b (gdbarch, insn1, dsc);
8103 else
8104 err = thumb_copy_svc (gdbarch, insn1, regs, dsc);
8105 break;
8106 case 14: /* Unconditional branch */
8107 err = thumb_copy_b (gdbarch, insn1, dsc);
8108 break;
8109 default:
8110 err = 1;
8111 }
8112
8113 if (err)
8114 internal_error (__FILE__, __LINE__,
8115 _("thumb_process_displaced_16bit_insn: Instruction decode error"));
8116}
8117
8118static int
8119decode_thumb_32bit_ld_mem_hints (struct gdbarch *gdbarch,
8120 uint16_t insn1, uint16_t insn2,
8121 struct regcache *regs,
1152d984 8122 arm_displaced_step_copy_insn_closure *dsc)
34518530
YQ
8123{
8124 int rt = bits (insn2, 12, 15);
8125 int rn = bits (insn1, 0, 3);
8126 int op1 = bits (insn1, 7, 8);
34518530
YQ
8127
8128 switch (bits (insn1, 5, 6))
8129 {
8130 case 0: /* Load byte and memory hints */
8131 if (rt == 0xf) /* PLD/PLI */
8132 {
8133 if (rn == 0xf)
8134 /* PLD literal or Encoding T3 of PLI(immediate, literal). */
8135 return thumb2_copy_preload (gdbarch, insn1, insn2, regs, dsc);
8136 else
8137 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8138 "pli/pld", dsc);
8139 }
8140 else
8141 {
8142 if (rn == 0xf) /* LDRB/LDRSB (literal) */
8143 return thumb2_copy_load_literal (gdbarch, insn1, insn2, regs, dsc,
8144 1);
8145 else
8146 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8147 "ldrb{reg, immediate}/ldrbt",
8148 dsc);
8149 }
8150
8151 break;
8152 case 1: /* Load halfword and memory hints. */
8153 if (rt == 0xf) /* PLD{W} and Unalloc memory hint. */
8154 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8155 "pld/unalloc memhint", dsc);
8156 else
8157 {
8158 if (rn == 0xf)
8159 return thumb2_copy_load_literal (gdbarch, insn1, insn2, regs, dsc,
8160 2);
8161 else
8162 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8163 "ldrh/ldrht", dsc);
8164 }
8165 break;
8166 case 2: /* Load word */
8167 {
8168 int insn2_bit_8_11 = bits (insn2, 8, 11);
8169
8170 if (rn == 0xf)
8171 return thumb2_copy_load_literal (gdbarch, insn1, insn2, regs, dsc, 4);
8172 else if (op1 == 0x1) /* Encoding T3 */
8173 return thumb2_copy_load_reg_imm (gdbarch, insn1, insn2, regs, dsc,
8174 0, 1);
8175 else /* op1 == 0x0 */
8176 {
8177 if (insn2_bit_8_11 == 0xc || (insn2_bit_8_11 & 0x9) == 0x9)
8178 /* LDR (immediate) */
8179 return thumb2_copy_load_reg_imm (gdbarch, insn1, insn2, regs,
8180 dsc, bit (insn2, 8), 1);
8181 else if (insn2_bit_8_11 == 0xe) /* LDRT */
8182 return thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8183 "ldrt", dsc);
8184 else
8185 /* LDR (register) */
8186 return thumb2_copy_load_reg_imm (gdbarch, insn1, insn2, regs,
8187 dsc, 0, 0);
8188 }
8189 break;
8190 }
8191 default:
8192 return thumb_32bit_copy_undef (gdbarch, insn1, insn2, dsc);
8193 break;
8194 }
8195 return 0;
8196}
8197
8198static void
8199thumb_process_displaced_32bit_insn (struct gdbarch *gdbarch, uint16_t insn1,
8200 uint16_t insn2, struct regcache *regs,
1152d984 8201 arm_displaced_step_copy_insn_closure *dsc)
34518530
YQ
8202{
8203 int err = 0;
8204 unsigned short op = bit (insn2, 15);
8205 unsigned int op1 = bits (insn1, 11, 12);
8206
8207 switch (op1)
8208 {
8209 case 1:
8210 {
8211 switch (bits (insn1, 9, 10))
8212 {
8213 case 0:
8214 if (bit (insn1, 6))
8215 {
405feb71 8216 /* Load/store {dual, exclusive}, table branch. */
34518530
YQ
8217 if (bits (insn1, 7, 8) == 1 && bits (insn1, 4, 5) == 1
8218 && bits (insn2, 5, 7) == 0)
8219 err = thumb2_copy_table_branch (gdbarch, insn1, insn2, regs,
8220 dsc);
8221 else
8222 /* PC is not allowed to use in load/store {dual, exclusive}
8223 instructions. */
8224 err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8225 "load/store dual/ex", dsc);
8226 }
8227 else /* load/store multiple */
8228 {
8229 switch (bits (insn1, 7, 8))
8230 {
8231 case 0: case 3: /* SRS, RFE */
8232 err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8233 "srs/rfe", dsc);
8234 break;
8235 case 1: case 2: /* LDM/STM/PUSH/POP */
8236 err = thumb2_copy_block_xfer (gdbarch, insn1, insn2, regs, dsc);
8237 break;
8238 }
8239 }
8240 break;
8241
8242 case 1:
8243 /* Data-processing (shift register). */
8244 err = thumb2_decode_dp_shift_reg (gdbarch, insn1, insn2, regs,
8245 dsc);
8246 break;
8247 default: /* Coprocessor instructions. */
8248 err = thumb2_decode_svc_copro (gdbarch, insn1, insn2, regs, dsc);
8249 break;
8250 }
8251 break;
8252 }
8253 case 2: /* op1 = 2 */
8254 if (op) /* Branch and misc control. */
8255 {
8256 if (bit (insn2, 14) /* BLX/BL */
8257 || bit (insn2, 12) /* Unconditional branch */
8258 || (bits (insn1, 7, 9) != 0x7)) /* Conditional branch */
8259 err = thumb2_copy_b_bl_blx (gdbarch, insn1, insn2, regs, dsc);
8260 else
8261 err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8262 "misc ctrl", dsc);
8263 }
8264 else
8265 {
8266 if (bit (insn1, 9)) /* Data processing (plain binary imm). */
8267 {
b926417a 8268 int dp_op = bits (insn1, 4, 8);
34518530 8269 int rn = bits (insn1, 0, 3);
b926417a 8270 if ((dp_op == 0 || dp_op == 0xa) && rn == 0xf)
34518530
YQ
8271 err = thumb_copy_pc_relative_32bit (gdbarch, insn1, insn2,
8272 regs, dsc);
8273 else
8274 err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8275 "dp/pb", dsc);
8276 }
405feb71 8277 else /* Data processing (modified immediate) */
34518530
YQ
8278 err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8279 "dp/mi", dsc);
8280 }
8281 break;
8282 case 3: /* op1 = 3 */
8283 switch (bits (insn1, 9, 10))
8284 {
8285 case 0:
8286 if (bit (insn1, 4))
8287 err = decode_thumb_32bit_ld_mem_hints (gdbarch, insn1, insn2,
8288 regs, dsc);
8289 else /* NEON Load/Store and Store single data item */
8290 err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8291 "neon elt/struct load/store",
8292 dsc);
8293 break;
8294 case 1: /* op1 = 3, bits (9, 10) == 1 */
8295 switch (bits (insn1, 7, 8))
8296 {
8297 case 0: case 1: /* Data processing (register) */
8298 err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8299 "dp(reg)", dsc);
8300 break;
8301 case 2: /* Multiply and absolute difference */
8302 err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8303 "mul/mua/diff", dsc);
8304 break;
8305 case 3: /* Long multiply and divide */
8306 err = thumb_copy_unmodified_32bit (gdbarch, insn1, insn2,
8307 "lmul/lmua", dsc);
8308 break;
8309 }
8310 break;
8311 default: /* Coprocessor instructions */
8312 err = thumb2_decode_svc_copro (gdbarch, insn1, insn2, regs, dsc);
8313 break;
8314 }
8315 break;
8316 default:
8317 err = 1;
8318 }
8319
8320 if (err)
8321 internal_error (__FILE__, __LINE__,
8322 _("thumb_process_displaced_32bit_insn: Instruction decode error"));
8323
8324}
8325
b434a28f
YQ
8326static void
8327thumb_process_displaced_insn (struct gdbarch *gdbarch, CORE_ADDR from,
12545665 8328 struct regcache *regs,
1152d984 8329 arm_displaced_step_copy_insn_closure *dsc)
b434a28f 8330{
34518530
YQ
8331 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
8332 uint16_t insn1
8333 = read_memory_unsigned_integer (from, 2, byte_order_for_code);
8334
136821d9
SM
8335 displaced_debug_printf ("process thumb insn %.4x at %.8lx",
8336 insn1, (unsigned long) from);
34518530
YQ
8337
8338 dsc->is_thumb = 1;
8339 dsc->insn_size = thumb_insn_size (insn1);
8340 if (thumb_insn_size (insn1) == 4)
8341 {
8342 uint16_t insn2
8343 = read_memory_unsigned_integer (from + 2, 2, byte_order_for_code);
8344 thumb_process_displaced_32bit_insn (gdbarch, insn1, insn2, regs, dsc);
8345 }
8346 else
8347 thumb_process_displaced_16bit_insn (gdbarch, insn1, regs, dsc);
b434a28f
YQ
8348}
8349
cca44b1b 8350void
b434a28f
YQ
8351arm_process_displaced_insn (struct gdbarch *gdbarch, CORE_ADDR from,
8352 CORE_ADDR to, struct regcache *regs,
1152d984 8353 arm_displaced_step_copy_insn_closure *dsc)
cca44b1b
JB
8354{
8355 int err = 0;
b434a28f
YQ
8356 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
8357 uint32_t insn;
cca44b1b
JB
8358
8359 /* Most displaced instructions use a 1-instruction scratch space, so set this
8360 here and override below if/when necessary. */
8361 dsc->numinsns = 1;
8362 dsc->insn_addr = from;
8363 dsc->scratch_base = to;
8364 dsc->cleanup = NULL;
8365 dsc->wrote_to_pc = 0;
8366
b434a28f 8367 if (!displaced_in_arm_mode (regs))
12545665 8368 return thumb_process_displaced_insn (gdbarch, from, regs, dsc);
b434a28f 8369
4db71c0b
YQ
8370 dsc->is_thumb = 0;
8371 dsc->insn_size = 4;
b434a28f 8372 insn = read_memory_unsigned_integer (from, 4, byte_order_for_code);
136821d9
SM
8373 displaced_debug_printf ("stepping insn %.8lx at %.8lx",
8374 (unsigned long) insn, (unsigned long) from);
b434a28f 8375
cca44b1b 8376 if ((insn & 0xf0000000) == 0xf0000000)
7ff120b4 8377 err = arm_decode_unconditional (gdbarch, insn, regs, dsc);
cca44b1b
JB
8378 else switch (((insn & 0x10) >> 4) | ((insn & 0xe000000) >> 24))
8379 {
8380 case 0x0: case 0x1: case 0x2: case 0x3:
7ff120b4 8381 err = arm_decode_dp_misc (gdbarch, insn, regs, dsc);
cca44b1b
JB
8382 break;
8383
8384 case 0x4: case 0x5: case 0x6:
7ff120b4 8385 err = arm_decode_ld_st_word_ubyte (gdbarch, insn, regs, dsc);
cca44b1b
JB
8386 break;
8387
8388 case 0x7:
7ff120b4 8389 err = arm_decode_media (gdbarch, insn, dsc);
cca44b1b
JB
8390 break;
8391
8392 case 0x8: case 0x9: case 0xa: case 0xb:
7ff120b4 8393 err = arm_decode_b_bl_ldmstm (gdbarch, insn, regs, dsc);
cca44b1b
JB
8394 break;
8395
8396 case 0xc: case 0xd: case 0xe: case 0xf:
12545665 8397 err = arm_decode_svc_copro (gdbarch, insn, regs, dsc);
cca44b1b
JB
8398 break;
8399 }
8400
8401 if (err)
8402 internal_error (__FILE__, __LINE__,
8403 _("arm_process_displaced_insn: Instruction decode error"));
8404}
8405
8406/* Actually set up the scratch space for a displaced instruction. */
8407
8408void
8409arm_displaced_init_closure (struct gdbarch *gdbarch, CORE_ADDR from,
1152d984
SM
8410 CORE_ADDR to,
8411 arm_displaced_step_copy_insn_closure *dsc)
cca44b1b 8412{
08106042 8413 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
4db71c0b 8414 unsigned int i, len, offset;
cca44b1b 8415 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
4db71c0b 8416 int size = dsc->is_thumb? 2 : 4;
948f8e3d 8417 const gdb_byte *bkp_insn;
cca44b1b 8418
4db71c0b 8419 offset = 0;
cca44b1b
JB
8420 /* Poke modified instruction(s). */
8421 for (i = 0; i < dsc->numinsns; i++)
8422 {
136821d9
SM
8423 if (size == 4)
8424 displaced_debug_printf ("writing insn %.8lx at %.8lx",
8425 dsc->modinsn[i], (unsigned long) to + offset);
8426 else if (size == 2)
8427 displaced_debug_printf ("writing insn %.4x at %.8lx",
8428 (unsigned short) dsc->modinsn[i],
8429 (unsigned long) to + offset);
4db71c0b 8430
4db71c0b
YQ
8431 write_memory_unsigned_integer (to + offset, size,
8432 byte_order_for_code,
cca44b1b 8433 dsc->modinsn[i]);
4db71c0b
YQ
8434 offset += size;
8435 }
8436
8437 /* Choose the correct breakpoint instruction. */
8438 if (dsc->is_thumb)
8439 {
8440 bkp_insn = tdep->thumb_breakpoint;
8441 len = tdep->thumb_breakpoint_size;
8442 }
8443 else
8444 {
8445 bkp_insn = tdep->arm_breakpoint;
8446 len = tdep->arm_breakpoint_size;
cca44b1b
JB
8447 }
8448
8449 /* Put breakpoint afterwards. */
4db71c0b 8450 write_memory (to + offset, bkp_insn, len);
cca44b1b 8451
136821d9
SM
8452 displaced_debug_printf ("copy %s->%s", paddress (gdbarch, from),
8453 paddress (gdbarch, to));
cca44b1b
JB
8454}
8455
cca44b1b
JB
8456/* Entry point for cleaning things up after a displaced instruction has been
8457 single-stepped. */
8458
8459void
8460arm_displaced_step_fixup (struct gdbarch *gdbarch,
1152d984 8461 struct displaced_step_copy_insn_closure *dsc_,
cca44b1b
JB
8462 CORE_ADDR from, CORE_ADDR to,
8463 struct regcache *regs)
8464{
1152d984
SM
8465 arm_displaced_step_copy_insn_closure *dsc
8466 = (arm_displaced_step_copy_insn_closure *) dsc_;
cfba9872 8467
cca44b1b
JB
8468 if (dsc->cleanup)
8469 dsc->cleanup (gdbarch, regs, dsc);
8470
8471 if (!dsc->wrote_to_pc)
4db71c0b
YQ
8472 regcache_cooked_write_unsigned (regs, ARM_PC_REGNUM,
8473 dsc->insn_addr + dsc->insn_size);
8474
cca44b1b
JB
8475}
8476
8477#include "bfd-in2.h"
8478#include "libcoff.h"
8479
8480static int
8481gdb_print_insn_arm (bfd_vma memaddr, disassemble_info *info)
8482{
f0c2e3e0
AB
8483 gdb_disassemble_info *di
8484 = static_cast<gdb_disassemble_info *> (info->application_data);
e47ad6c0 8485 struct gdbarch *gdbarch = di->arch ();
9779414d
DJ
8486
8487 if (arm_pc_is_thumb (gdbarch, memaddr))
cca44b1b
JB
8488 {
8489 static asymbol *asym;
8490 static combined_entry_type ce;
8491 static struct coff_symbol_struct csym;
8492 static struct bfd fake_bfd;
8493 static bfd_target fake_target;
8494
8495 if (csym.native == NULL)
8496 {
8497 /* Create a fake symbol vector containing a Thumb symbol.
8498 This is solely so that the code in print_insn_little_arm()
8499 and print_insn_big_arm() in opcodes/arm-dis.c will detect
8500 the presence of a Thumb symbol and switch to decoding
8501 Thumb instructions. */
8502
8503 fake_target.flavour = bfd_target_coff_flavour;
8504 fake_bfd.xvec = &fake_target;
8505 ce.u.syment.n_sclass = C_THUMBEXTFUNC;
8506 csym.native = &ce;
8507 csym.symbol.the_bfd = &fake_bfd;
8508 csym.symbol.name = "fake";
8509 asym = (asymbol *) & csym;
8510 }
8511
8512 memaddr = UNMAKE_THUMB_ADDR (memaddr);
8513 info->symbols = &asym;
8514 }
8515 else
8516 info->symbols = NULL;
8517
e60eb288
YQ
8518 /* GDB is able to get bfd_mach from the exe_bfd, info->mach is
8519 accurate, so mark USER_SPECIFIED_MACHINE_TYPE bit. Otherwise,
8520 opcodes/arm-dis.c:print_insn reset info->mach, and it will trigger
7e10abd1
TT
8521 the assert on the mismatch of info->mach and
8522 bfd_get_mach (current_program_space->exec_bfd ()) in
8523 default_print_insn. */
3047c786
TV
8524 if (current_program_space->exec_bfd () != NULL
8525 && (current_program_space->exec_bfd ()->arch_info
8526 == gdbarch_bfd_arch_info (gdbarch)))
e60eb288
YQ
8527 info->flags |= USER_SPECIFIED_MACHINE_TYPE;
8528
6394c606 8529 return default_print_insn (memaddr, info);
cca44b1b
JB
8530}
8531
8532/* The following define instruction sequences that will cause ARM
8533 cpu's to take an undefined instruction trap. These are used to
8534 signal a breakpoint to GDB.
8535
8536 The newer ARMv4T cpu's are capable of operating in ARM or Thumb
8537 modes. A different instruction is required for each mode. The ARM
8538 cpu's can also be big or little endian. Thus four different
8539 instructions are needed to support all cases.
8540
8541 Note: ARMv4 defines several new instructions that will take the
8542 undefined instruction trap. ARM7TDMI is nominally ARMv4T, but does
8543 not in fact add the new instructions. The new undefined
8544 instructions in ARMv4 are all instructions that had no defined
8545 behaviour in earlier chips. There is no guarantee that they will
8546 raise an exception, but may be treated as NOP's. In practice, it
8547 may only safe to rely on instructions matching:
8548
8549 3 3 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1
8550 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
8551 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
8552
0963b4bd 8553 Even this may only true if the condition predicate is true. The
cca44b1b
JB
8554 following use a condition predicate of ALWAYS so it is always TRUE.
8555
8556 There are other ways of forcing a breakpoint. GNU/Linux, RISC iX,
8557 and NetBSD all use a software interrupt rather than an undefined
8558 instruction to force a trap. This can be handled by by the
8559 abi-specific code during establishment of the gdbarch vector. */
8560
8561#define ARM_LE_BREAKPOINT {0xFE,0xDE,0xFF,0xE7}
8562#define ARM_BE_BREAKPOINT {0xE7,0xFF,0xDE,0xFE}
8563#define THUMB_LE_BREAKPOINT {0xbe,0xbe}
8564#define THUMB_BE_BREAKPOINT {0xbe,0xbe}
8565
948f8e3d
PA
8566static const gdb_byte arm_default_arm_le_breakpoint[] = ARM_LE_BREAKPOINT;
8567static const gdb_byte arm_default_arm_be_breakpoint[] = ARM_BE_BREAKPOINT;
8568static const gdb_byte arm_default_thumb_le_breakpoint[] = THUMB_LE_BREAKPOINT;
8569static const gdb_byte arm_default_thumb_be_breakpoint[] = THUMB_BE_BREAKPOINT;
cca44b1b 8570
cd6c3b4f
YQ
8571/* Implement the breakpoint_kind_from_pc gdbarch method. */
8572
d19280ad
YQ
8573static int
8574arm_breakpoint_kind_from_pc (struct gdbarch *gdbarch, CORE_ADDR *pcptr)
cca44b1b 8575{
08106042 8576 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
177321bd 8577 enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
cca44b1b 8578
9779414d 8579 if (arm_pc_is_thumb (gdbarch, *pcptr))
cca44b1b
JB
8580 {
8581 *pcptr = UNMAKE_THUMB_ADDR (*pcptr);
177321bd
DJ
8582
8583 /* If we have a separate 32-bit breakpoint instruction for Thumb-2,
8584 check whether we are replacing a 32-bit instruction. */
8585 if (tdep->thumb2_breakpoint != NULL)
8586 {
8587 gdb_byte buf[2];
d19280ad 8588
177321bd
DJ
8589 if (target_read_memory (*pcptr, buf, 2) == 0)
8590 {
8591 unsigned short inst1;
d19280ad 8592
177321bd 8593 inst1 = extract_unsigned_integer (buf, 2, byte_order_for_code);
db24da6d 8594 if (thumb_insn_size (inst1) == 4)
d19280ad 8595 return ARM_BP_KIND_THUMB2;
177321bd
DJ
8596 }
8597 }
8598
d19280ad 8599 return ARM_BP_KIND_THUMB;
cca44b1b
JB
8600 }
8601 else
d19280ad
YQ
8602 return ARM_BP_KIND_ARM;
8603
8604}
8605
cd6c3b4f
YQ
8606/* Implement the sw_breakpoint_from_kind gdbarch method. */
8607
d19280ad
YQ
8608static const gdb_byte *
8609arm_sw_breakpoint_from_kind (struct gdbarch *gdbarch, int kind, int *size)
8610{
08106042 8611 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
d19280ad
YQ
8612
8613 switch (kind)
cca44b1b 8614 {
d19280ad
YQ
8615 case ARM_BP_KIND_ARM:
8616 *size = tdep->arm_breakpoint_size;
cca44b1b 8617 return tdep->arm_breakpoint;
d19280ad
YQ
8618 case ARM_BP_KIND_THUMB:
8619 *size = tdep->thumb_breakpoint_size;
8620 return tdep->thumb_breakpoint;
8621 case ARM_BP_KIND_THUMB2:
8622 *size = tdep->thumb2_breakpoint_size;
8623 return tdep->thumb2_breakpoint;
8624 default:
8625 gdb_assert_not_reached ("unexpected arm breakpoint kind");
cca44b1b
JB
8626 }
8627}
8628
833b7ab5
YQ
8629/* Implement the breakpoint_kind_from_current_state gdbarch method. */
8630
8631static int
8632arm_breakpoint_kind_from_current_state (struct gdbarch *gdbarch,
8633 struct regcache *regcache,
8634 CORE_ADDR *pcptr)
8635{
8636 gdb_byte buf[4];
8637
8638 /* Check the memory pointed by PC is readable. */
8639 if (target_read_memory (regcache_read_pc (regcache), buf, 4) == 0)
8640 {
8641 struct arm_get_next_pcs next_pcs_ctx;
833b7ab5
YQ
8642
8643 arm_get_next_pcs_ctor (&next_pcs_ctx,
8644 &arm_get_next_pcs_ops,
8645 gdbarch_byte_order (gdbarch),
8646 gdbarch_byte_order_for_code (gdbarch),
8647 0,
8648 regcache);
8649
a0ff9e1a 8650 std::vector<CORE_ADDR> next_pcs = arm_get_next_pcs (&next_pcs_ctx);
833b7ab5
YQ
8651
8652 /* If MEMADDR is the next instruction of current pc, do the
8653 software single step computation, and get the thumb mode by
8654 the destination address. */
a0ff9e1a 8655 for (CORE_ADDR pc : next_pcs)
833b7ab5
YQ
8656 {
8657 if (UNMAKE_THUMB_ADDR (pc) == *pcptr)
8658 {
833b7ab5
YQ
8659 if (IS_THUMB_ADDR (pc))
8660 {
8661 *pcptr = MAKE_THUMB_ADDR (*pcptr);
8662 return arm_breakpoint_kind_from_pc (gdbarch, pcptr);
8663 }
8664 else
8665 return ARM_BP_KIND_ARM;
8666 }
8667 }
833b7ab5
YQ
8668 }
8669
8670 return arm_breakpoint_kind_from_pc (gdbarch, pcptr);
8671}
8672
cca44b1b
JB
8673/* Extract from an array REGBUF containing the (raw) register state a
8674 function return value of type TYPE, and copy that, in virtual
8675 format, into VALBUF. */
8676
8677static void
8678arm_extract_return_value (struct type *type, struct regcache *regs,
8679 gdb_byte *valbuf)
8680{
ac7936df 8681 struct gdbarch *gdbarch = regs->arch ();
cca44b1b 8682 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
08106042 8683 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
cca44b1b 8684
78134374 8685 if (TYPE_CODE_FLT == type->code ())
cca44b1b 8686 {
345bd07c 8687 switch (tdep->fp_model)
cca44b1b
JB
8688 {
8689 case ARM_FLOAT_FPA:
8690 {
8691 /* The value is in register F0 in internal format. We need to
8692 extract the raw value and then convert it to the desired
8693 internal type. */
f0452268 8694 bfd_byte tmpbuf[ARM_FP_REGISTER_SIZE];
cca44b1b 8695
dca08e1f 8696 regs->cooked_read (ARM_F0_REGNUM, tmpbuf);
3b2ca824
UW
8697 target_float_convert (tmpbuf, arm_ext_type (gdbarch),
8698 valbuf, type);
cca44b1b
JB
8699 }
8700 break;
8701
8702 case ARM_FLOAT_SOFT_FPA:
8703 case ARM_FLOAT_SOFT_VFP:
8704 /* ARM_FLOAT_VFP can arise if this is a variadic function so
8705 not using the VFP ABI code. */
8706 case ARM_FLOAT_VFP:
dca08e1f 8707 regs->cooked_read (ARM_A1_REGNUM, valbuf);
df86565b 8708 if (type->length () > 4)
f0452268
AH
8709 regs->cooked_read (ARM_A1_REGNUM + 1,
8710 valbuf + ARM_INT_REGISTER_SIZE);
cca44b1b
JB
8711 break;
8712
8713 default:
0963b4bd
MS
8714 internal_error (__FILE__, __LINE__,
8715 _("arm_extract_return_value: "
8716 "Floating point model not supported"));
cca44b1b
JB
8717 break;
8718 }
8719 }
78134374
SM
8720 else if (type->code () == TYPE_CODE_INT
8721 || type->code () == TYPE_CODE_CHAR
8722 || type->code () == TYPE_CODE_BOOL
8723 || type->code () == TYPE_CODE_PTR
aa006118 8724 || TYPE_IS_REFERENCE (type)
a6617193
JB
8725 || type->code () == TYPE_CODE_ENUM
8726 || is_fixed_point_type (type))
cca44b1b 8727 {
b021a221
MS
8728 /* If the type is a plain integer, then the access is
8729 straight-forward. Otherwise we have to play around a bit
8730 more. */
df86565b 8731 int len = type->length ();
cca44b1b
JB
8732 int regno = ARM_A1_REGNUM;
8733 ULONGEST tmp;
8734
8735 while (len > 0)
8736 {
8737 /* By using store_unsigned_integer we avoid having to do
8738 anything special for small big-endian values. */
8739 regcache_cooked_read_unsigned (regs, regno++, &tmp);
8740 store_unsigned_integer (valbuf,
f0452268
AH
8741 (len > ARM_INT_REGISTER_SIZE
8742 ? ARM_INT_REGISTER_SIZE : len),
cca44b1b 8743 byte_order, tmp);
f0452268
AH
8744 len -= ARM_INT_REGISTER_SIZE;
8745 valbuf += ARM_INT_REGISTER_SIZE;
cca44b1b
JB
8746 }
8747 }
8748 else
8749 {
8750 /* For a structure or union the behaviour is as if the value had
dda83cd7
SM
8751 been stored to word-aligned memory and then loaded into
8752 registers with 32-bit load instruction(s). */
df86565b 8753 int len = type->length ();
cca44b1b 8754 int regno = ARM_A1_REGNUM;
f0452268 8755 bfd_byte tmpbuf[ARM_INT_REGISTER_SIZE];
cca44b1b
JB
8756
8757 while (len > 0)
8758 {
dca08e1f 8759 regs->cooked_read (regno++, tmpbuf);
cca44b1b 8760 memcpy (valbuf, tmpbuf,
f0452268
AH
8761 len > ARM_INT_REGISTER_SIZE ? ARM_INT_REGISTER_SIZE : len);
8762 len -= ARM_INT_REGISTER_SIZE;
8763 valbuf += ARM_INT_REGISTER_SIZE;
cca44b1b
JB
8764 }
8765 }
8766}
8767
8768
8769/* Will a function return an aggregate type in memory or in a
8770 register? Return 0 if an aggregate type can be returned in a
8771 register, 1 if it must be returned in memory. */
8772
8773static int
8774arm_return_in_memory (struct gdbarch *gdbarch, struct type *type)
8775{
cca44b1b
JB
8776 enum type_code code;
8777
f168693b 8778 type = check_typedef (type);
cca44b1b 8779
b13c8ab2
YQ
8780 /* Simple, non-aggregate types (ie not including vectors and
8781 complex) are always returned in a register (or registers). */
78134374 8782 code = type->code ();
b13c8ab2
YQ
8783 if (TYPE_CODE_STRUCT != code && TYPE_CODE_UNION != code
8784 && TYPE_CODE_ARRAY != code && TYPE_CODE_COMPLEX != code)
8785 return 0;
cca44b1b 8786
bd63c870 8787 if (TYPE_CODE_ARRAY == code && type->is_vector ())
c4312b19
YQ
8788 {
8789 /* Vector values should be returned using ARM registers if they
8790 are not over 16 bytes. */
df86565b 8791 return (type->length () > 16);
c4312b19
YQ
8792 }
8793
08106042 8794 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
345bd07c 8795 if (tdep->arm_abi != ARM_ABI_APCS)
cca44b1b 8796 {
b13c8ab2
YQ
8797 /* The AAPCS says all aggregates not larger than a word are returned
8798 in a register. */
df86565b 8799 if (type->length () <= ARM_INT_REGISTER_SIZE
bab22d06 8800 && language_pass_by_reference (type).trivially_copyable)
b13c8ab2
YQ
8801 return 0;
8802
cca44b1b
JB
8803 return 1;
8804 }
b13c8ab2
YQ
8805 else
8806 {
8807 int nRc;
cca44b1b 8808
b13c8ab2
YQ
8809 /* All aggregate types that won't fit in a register must be returned
8810 in memory. */
df86565b 8811 if (type->length () > ARM_INT_REGISTER_SIZE
bab22d06 8812 || !language_pass_by_reference (type).trivially_copyable)
b13c8ab2 8813 return 1;
cca44b1b 8814
b13c8ab2
YQ
8815 /* In the ARM ABI, "integer" like aggregate types are returned in
8816 registers. For an aggregate type to be integer like, its size
f0452268 8817 must be less than or equal to ARM_INT_REGISTER_SIZE and the
b13c8ab2
YQ
8818 offset of each addressable subfield must be zero. Note that bit
8819 fields are not addressable, and all addressable subfields of
8820 unions always start at offset zero.
cca44b1b 8821
b13c8ab2
YQ
8822 This function is based on the behaviour of GCC 2.95.1.
8823 See: gcc/arm.c: arm_return_in_memory() for details.
cca44b1b 8824
b13c8ab2
YQ
8825 Note: All versions of GCC before GCC 2.95.2 do not set up the
8826 parameters correctly for a function returning the following
8827 structure: struct { float f;}; This should be returned in memory,
8828 not a register. Richard Earnshaw sent me a patch, but I do not
8829 know of any way to detect if a function like the above has been
8830 compiled with the correct calling convention. */
8831
8832 /* Assume all other aggregate types can be returned in a register.
8833 Run a check for structures, unions and arrays. */
8834 nRc = 0;
67255d04 8835
b13c8ab2
YQ
8836 if ((TYPE_CODE_STRUCT == code) || (TYPE_CODE_UNION == code))
8837 {
8838 int i;
8839 /* Need to check if this struct/union is "integer" like. For
8840 this to be true, its size must be less than or equal to
f0452268 8841 ARM_INT_REGISTER_SIZE and the offset of each addressable
b13c8ab2
YQ
8842 subfield must be zero. Note that bit fields are not
8843 addressable, and unions always start at offset zero. If any
8844 of the subfields is a floating point type, the struct/union
8845 cannot be an integer type. */
8846
8847 /* For each field in the object, check:
8848 1) Is it FP? --> yes, nRc = 1;
8849 2) Is it addressable (bitpos != 0) and
8850 not packed (bitsize == 0)?
8851 --> yes, nRc = 1
8852 */
8853
1f704f76 8854 for (i = 0; i < type->num_fields (); i++)
67255d04 8855 {
b13c8ab2
YQ
8856 enum type_code field_type_code;
8857
8858 field_type_code
940da03e 8859 = check_typedef (type->field (i).type ())->code ();
b13c8ab2
YQ
8860
8861 /* Is it a floating point type field? */
8862 if (field_type_code == TYPE_CODE_FLT)
67255d04
RE
8863 {
8864 nRc = 1;
8865 break;
8866 }
b13c8ab2
YQ
8867
8868 /* If bitpos != 0, then we have to care about it. */
b610c045 8869 if (type->field (i).loc_bitpos () != 0)
b13c8ab2
YQ
8870 {
8871 /* Bitfields are not addressable. If the field bitsize is
8872 zero, then the field is not packed. Hence it cannot be
8873 a bitfield or any other packed type. */
8874 if (TYPE_FIELD_BITSIZE (type, i) == 0)
8875 {
8876 nRc = 1;
8877 break;
8878 }
8879 }
67255d04
RE
8880 }
8881 }
67255d04 8882
b13c8ab2
YQ
8883 return nRc;
8884 }
67255d04
RE
8885}
8886
34e8f22d
RE
8887/* Write into appropriate registers a function return value of type
8888 TYPE, given in virtual format. */
8889
8890static void
b508a996 8891arm_store_return_value (struct type *type, struct regcache *regs,
5238cf52 8892 const gdb_byte *valbuf)
34e8f22d 8893{
ac7936df 8894 struct gdbarch *gdbarch = regs->arch ();
e17a4113 8895 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
be8626e0 8896
78134374 8897 if (type->code () == TYPE_CODE_FLT)
34e8f22d 8898 {
f0452268 8899 gdb_byte buf[ARM_FP_REGISTER_SIZE];
08106042 8900 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
34e8f22d 8901
345bd07c 8902 switch (tdep->fp_model)
08216dd7
RE
8903 {
8904 case ARM_FLOAT_FPA:
8905
3b2ca824 8906 target_float_convert (valbuf, type, buf, arm_ext_type (gdbarch));
b66f5587 8907 regs->cooked_write (ARM_F0_REGNUM, buf);
08216dd7
RE
8908 break;
8909
fd50bc42 8910 case ARM_FLOAT_SOFT_FPA:
08216dd7 8911 case ARM_FLOAT_SOFT_VFP:
90445bd3
DJ
8912 /* ARM_FLOAT_VFP can arise if this is a variadic function so
8913 not using the VFP ABI code. */
8914 case ARM_FLOAT_VFP:
b66f5587 8915 regs->cooked_write (ARM_A1_REGNUM, valbuf);
df86565b 8916 if (type->length () > 4)
f0452268
AH
8917 regs->cooked_write (ARM_A1_REGNUM + 1,
8918 valbuf + ARM_INT_REGISTER_SIZE);
08216dd7
RE
8919 break;
8920
8921 default:
9b20d036
MS
8922 internal_error (__FILE__, __LINE__,
8923 _("arm_store_return_value: Floating "
8924 "point model not supported"));
08216dd7
RE
8925 break;
8926 }
34e8f22d 8927 }
78134374
SM
8928 else if (type->code () == TYPE_CODE_INT
8929 || type->code () == TYPE_CODE_CHAR
8930 || type->code () == TYPE_CODE_BOOL
8931 || type->code () == TYPE_CODE_PTR
aa006118 8932 || TYPE_IS_REFERENCE (type)
78134374 8933 || type->code () == TYPE_CODE_ENUM)
b508a996 8934 {
df86565b 8935 if (type->length () <= 4)
b508a996
RE
8936 {
8937 /* Values of one word or less are zero/sign-extended and
8938 returned in r0. */
f0452268 8939 bfd_byte tmpbuf[ARM_INT_REGISTER_SIZE];
b508a996
RE
8940 LONGEST val = unpack_long (type, valbuf);
8941
f0452268 8942 store_signed_integer (tmpbuf, ARM_INT_REGISTER_SIZE, byte_order, val);
b66f5587 8943 regs->cooked_write (ARM_A1_REGNUM, tmpbuf);
b508a996
RE
8944 }
8945 else
8946 {
8947 /* Integral values greater than one word are stored in consecutive
8948 registers starting with r0. This will always be a multiple of
8949 the regiser size. */
df86565b 8950 int len = type->length ();
b508a996
RE
8951 int regno = ARM_A1_REGNUM;
8952
8953 while (len > 0)
8954 {
b66f5587 8955 regs->cooked_write (regno++, valbuf);
f0452268
AH
8956 len -= ARM_INT_REGISTER_SIZE;
8957 valbuf += ARM_INT_REGISTER_SIZE;
b508a996
RE
8958 }
8959 }
8960 }
34e8f22d 8961 else
b508a996
RE
8962 {
8963 /* For a structure or union the behaviour is as if the value had
dda83cd7
SM
8964 been stored to word-aligned memory and then loaded into
8965 registers with 32-bit load instruction(s). */
df86565b 8966 int len = type->length ();
b508a996 8967 int regno = ARM_A1_REGNUM;
f0452268 8968 bfd_byte tmpbuf[ARM_INT_REGISTER_SIZE];
b508a996
RE
8969
8970 while (len > 0)
8971 {
8972 memcpy (tmpbuf, valbuf,
f0452268 8973 len > ARM_INT_REGISTER_SIZE ? ARM_INT_REGISTER_SIZE : len);
b66f5587 8974 regs->cooked_write (regno++, tmpbuf);
f0452268
AH
8975 len -= ARM_INT_REGISTER_SIZE;
8976 valbuf += ARM_INT_REGISTER_SIZE;
b508a996
RE
8977 }
8978 }
34e8f22d
RE
8979}
8980
2af48f68
PB
8981
8982/* Handle function return values. */
8983
8984static enum return_value_convention
6a3a010b 8985arm_return_value (struct gdbarch *gdbarch, struct value *function,
c055b101
CV
8986 struct type *valtype, struct regcache *regcache,
8987 gdb_byte *readbuf, const gdb_byte *writebuf)
2af48f68 8988{
08106042 8989 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
6a3a010b 8990 struct type *func_type = function ? value_type (function) : NULL;
90445bd3
DJ
8991 enum arm_vfp_cprc_base_type vfp_base_type;
8992 int vfp_base_count;
8993
8994 if (arm_vfp_abi_for_function (gdbarch, func_type)
8995 && arm_vfp_call_candidate (valtype, &vfp_base_type, &vfp_base_count))
8996 {
8997 int reg_char = arm_vfp_cprc_reg_char (vfp_base_type);
8998 int unit_length = arm_vfp_cprc_unit_length (vfp_base_type);
8999 int i;
9000 for (i = 0; i < vfp_base_count; i++)
9001 {
58d6951d
DJ
9002 if (reg_char == 'q')
9003 {
9004 if (writebuf)
9005 arm_neon_quad_write (gdbarch, regcache, i,
9006 writebuf + i * unit_length);
9007
9008 if (readbuf)
9009 arm_neon_quad_read (gdbarch, regcache, i,
9010 readbuf + i * unit_length);
9011 }
9012 else
9013 {
9014 char name_buf[4];
9015 int regnum;
9016
8c042590 9017 xsnprintf (name_buf, sizeof (name_buf), "%c%d", reg_char, i);
58d6951d
DJ
9018 regnum = user_reg_map_name_to_regnum (gdbarch, name_buf,
9019 strlen (name_buf));
9020 if (writebuf)
b66f5587 9021 regcache->cooked_write (regnum, writebuf + i * unit_length);
58d6951d 9022 if (readbuf)
dca08e1f 9023 regcache->cooked_read (regnum, readbuf + i * unit_length);
58d6951d 9024 }
90445bd3
DJ
9025 }
9026 return RETURN_VALUE_REGISTER_CONVENTION;
9027 }
7c00367c 9028
78134374
SM
9029 if (valtype->code () == TYPE_CODE_STRUCT
9030 || valtype->code () == TYPE_CODE_UNION
9031 || valtype->code () == TYPE_CODE_ARRAY)
2af48f68 9032 {
bab22d06
LM
9033 /* From the AAPCS document:
9034
9035 Result return:
9036
9037 A Composite Type larger than 4 bytes, or whose size cannot be
9038 determined statically by both caller and callee, is stored in memory
9039 at an address passed as an extra argument when the function was
9040 called (Parameter Passing, rule A.4). The memory to be used for the
9041 result may be modified at any point during the function call.
9042
9043 Parameter Passing:
9044
9045 A.4: If the subroutine is a function that returns a result in memory,
9046 then the address for the result is placed in r0 and the NCRN is set
9047 to r1. */
7c00367c
MK
9048 if (tdep->struct_return == pcc_struct_return
9049 || arm_return_in_memory (gdbarch, valtype))
bab22d06
LM
9050 {
9051 if (readbuf)
9052 {
9053 CORE_ADDR addr;
9054
9055 regcache->cooked_read (ARM_A1_REGNUM, &addr);
df86565b 9056 read_memory (addr, readbuf, valtype->length ());
bab22d06
LM
9057 }
9058 return RETURN_VALUE_ABI_RETURNS_ADDRESS;
9059 }
2af48f68 9060 }
78134374 9061 else if (valtype->code () == TYPE_CODE_COMPLEX)
b13c8ab2
YQ
9062 {
9063 if (arm_return_in_memory (gdbarch, valtype))
9064 return RETURN_VALUE_STRUCT_CONVENTION;
9065 }
7052e42c 9066
2af48f68
PB
9067 if (writebuf)
9068 arm_store_return_value (valtype, regcache, writebuf);
9069
9070 if (readbuf)
9071 arm_extract_return_value (valtype, regcache, readbuf);
9072
9073 return RETURN_VALUE_REGISTER_CONVENTION;
9074}
9075
9076
9df628e0 9077static int
60ade65d 9078arm_get_longjmp_target (struct frame_info *frame, CORE_ADDR *pc)
9df628e0 9079{
e17a4113 9080 struct gdbarch *gdbarch = get_frame_arch (frame);
08106042 9081 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
e17a4113 9082 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
9df628e0 9083 CORE_ADDR jb_addr;
f0452268 9084 gdb_byte buf[ARM_INT_REGISTER_SIZE];
9df628e0 9085
60ade65d 9086 jb_addr = get_frame_register_unsigned (frame, ARM_A1_REGNUM);
9df628e0
RE
9087
9088 if (target_read_memory (jb_addr + tdep->jb_pc * tdep->jb_elt_size, buf,
f0452268 9089 ARM_INT_REGISTER_SIZE))
9df628e0
RE
9090 return 0;
9091
f0452268 9092 *pc = extract_unsigned_integer (buf, ARM_INT_REGISTER_SIZE, byte_order);
9df628e0
RE
9093 return 1;
9094}
40eadf04
SP
9095/* A call to cmse secure entry function "foo" at "a" is modified by
9096 GNU ld as "b".
9097 a) bl xxxx <foo>
9098
9099 <foo>
9100 xxxx:
9101
9102 b) bl yyyy <__acle_se_foo>
9103
9104 section .gnu.sgstubs:
9105 <foo>
9106 yyyy: sg // secure gateway
9107 b.w xxxx <__acle_se_foo> // original_branch_dest
9108
9109 <__acle_se_foo>
9110 xxxx:
9111
9112 When the control at "b", the pc contains "yyyy" (sg address) which is a
9113 trampoline and does not exist in source code. This function returns the
9114 target pc "xxxx". For more details please refer to section 5.4
9115 (Entry functions) and section 3.4.4 (C level development flow of secure code)
9116 of "armv8-m-security-extensions-requirements-on-development-tools-engineering-specification"
9117 document on www.developer.arm.com. */
9118
9119static CORE_ADDR
9120arm_skip_cmse_entry (CORE_ADDR pc, const char *name, struct objfile *objfile)
9121{
9122 int target_len = strlen (name) + strlen ("__acle_se_") + 1;
9123 char *target_name = (char *) alloca (target_len);
9124 xsnprintf (target_name, target_len, "%s%s", "__acle_se_", name);
9125
9126 struct bound_minimal_symbol minsym
9127 = lookup_minimal_symbol (target_name, NULL, objfile);
9128
9129 if (minsym.minsym != nullptr)
4aeddc50 9130 return minsym.value_address ();
40eadf04
SP
9131
9132 return 0;
9133}
9134
9135/* Return true when SEC points to ".gnu.sgstubs" section. */
9136
9137static bool
9138arm_is_sgstubs_section (struct obj_section *sec)
9139{
9140 return (sec != nullptr
9141 && sec->the_bfd_section != nullptr
9142 && sec->the_bfd_section->name != nullptr
9143 && streq (sec->the_bfd_section->name, ".gnu.sgstubs"));
9144}
9df628e0 9145
faa95490
DJ
9146/* Recognize GCC and GNU ld's trampolines. If we are in a trampoline,
9147 return the target PC. Otherwise return 0. */
c906108c
SS
9148
9149CORE_ADDR
52f729a7 9150arm_skip_stub (struct frame_info *frame, CORE_ADDR pc)
c906108c 9151{
2c02bd72 9152 const char *name;
faa95490 9153 int namelen;
c906108c
SS
9154 CORE_ADDR start_addr;
9155
9156 /* Find the starting address and name of the function containing the PC. */
9157 if (find_pc_partial_function (pc, &name, &start_addr, NULL) == 0)
80d8d390
YQ
9158 {
9159 /* Trampoline 'bx reg' doesn't belong to any functions. Do the
9160 check here. */
9161 start_addr = arm_skip_bx_reg (frame, pc);
9162 if (start_addr != 0)
9163 return start_addr;
9164
9165 return 0;
9166 }
c906108c 9167
faa95490
DJ
9168 /* If PC is in a Thumb call or return stub, return the address of the
9169 target PC, which is in a register. The thunk functions are called
9170 _call_via_xx, where x is the register name. The possible names
3d8d5e79
DJ
9171 are r0-r9, sl, fp, ip, sp, and lr. ARM RealView has similar
9172 functions, named __ARM_call_via_r[0-7]. */
61012eef
GB
9173 if (startswith (name, "_call_via_")
9174 || startswith (name, "__ARM_call_via_"))
c906108c 9175 {
ed9a39eb 9176 /* Use the name suffix to determine which register contains the
dda83cd7 9177 target PC. */
a121b7c1 9178 static const char *table[15] =
c5aa993b
JM
9179 {"r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
9180 "r8", "r9", "sl", "fp", "ip", "sp", "lr"
9181 };
c906108c 9182 int regno;
faa95490 9183 int offset = strlen (name) - 2;
c906108c
SS
9184
9185 for (regno = 0; regno <= 14; regno++)
faa95490 9186 if (strcmp (&name[offset], table[regno]) == 0)
52f729a7 9187 return get_frame_register_unsigned (frame, regno);
c906108c 9188 }
ed9a39eb 9189
faa95490
DJ
9190 /* GNU ld generates __foo_from_arm or __foo_from_thumb for
9191 non-interworking calls to foo. We could decode the stubs
9192 to find the target but it's easier to use the symbol table. */
9193 namelen = strlen (name);
9194 if (name[0] == '_' && name[1] == '_'
9195 && ((namelen > 2 + strlen ("_from_thumb")
61012eef 9196 && startswith (name + namelen - strlen ("_from_thumb"), "_from_thumb"))
faa95490 9197 || (namelen > 2 + strlen ("_from_arm")
61012eef 9198 && startswith (name + namelen - strlen ("_from_arm"), "_from_arm"))))
faa95490
DJ
9199 {
9200 char *target_name;
9201 int target_len = namelen - 2;
3b7344d5 9202 struct bound_minimal_symbol minsym;
faa95490
DJ
9203 struct objfile *objfile;
9204 struct obj_section *sec;
9205
9206 if (name[namelen - 1] == 'b')
9207 target_len -= strlen ("_from_thumb");
9208 else
9209 target_len -= strlen ("_from_arm");
9210
224c3ddb 9211 target_name = (char *) alloca (target_len + 1);
faa95490
DJ
9212 memcpy (target_name, name + 2, target_len);
9213 target_name[target_len] = '\0';
9214
9215 sec = find_pc_section (pc);
9216 objfile = (sec == NULL) ? NULL : sec->objfile;
9217 minsym = lookup_minimal_symbol (target_name, NULL, objfile);
3b7344d5 9218 if (minsym.minsym != NULL)
4aeddc50 9219 return minsym.value_address ();
faa95490
DJ
9220 else
9221 return 0;
9222 }
9223
40eadf04
SP
9224 struct obj_section *section = find_pc_section (pc);
9225
9226 /* Check whether SECTION points to the ".gnu.sgstubs" section. */
9227 if (arm_is_sgstubs_section (section))
9228 return arm_skip_cmse_entry (pc, name, section->objfile);
9229
c5aa993b 9230 return 0; /* not a stub */
c906108c
SS
9231}
9232
28e97307
DJ
9233static void
9234arm_update_current_architecture (void)
fd50bc42 9235{
28e97307 9236 /* If the current architecture is not ARM, we have nothing to do. */
f5656ead 9237 if (gdbarch_bfd_arch_info (target_gdbarch ())->arch != bfd_arch_arm)
28e97307 9238 return;
fd50bc42 9239
28e97307 9240 /* Update the architecture. */
b447dd03 9241 gdbarch_info info;
28e97307 9242 if (!gdbarch_update_p (info))
9b20d036 9243 internal_error (__FILE__, __LINE__, _("could not update architecture"));
fd50bc42
RE
9244}
9245
9246static void
eb4c3f4a 9247set_fp_model_sfunc (const char *args, int from_tty,
fd50bc42
RE
9248 struct cmd_list_element *c)
9249{
570dc176 9250 int fp_model;
fd50bc42
RE
9251
9252 for (fp_model = ARM_FLOAT_AUTO; fp_model != ARM_FLOAT_LAST; fp_model++)
9253 if (strcmp (current_fp_model, fp_model_strings[fp_model]) == 0)
9254 {
aead7601 9255 arm_fp_model = (enum arm_float_model) fp_model;
fd50bc42
RE
9256 break;
9257 }
9258
9259 if (fp_model == ARM_FLOAT_LAST)
edefbb7c 9260 internal_error (__FILE__, __LINE__, _("Invalid fp model accepted: %s."),
fd50bc42
RE
9261 current_fp_model);
9262
28e97307 9263 arm_update_current_architecture ();
fd50bc42
RE
9264}
9265
9266static void
08546159
AC
9267show_fp_model (struct ui_file *file, int from_tty,
9268 struct cmd_list_element *c, const char *value)
fd50bc42 9269{
28e97307 9270 if (arm_fp_model == ARM_FLOAT_AUTO
f5656ead 9271 && gdbarch_bfd_arch_info (target_gdbarch ())->arch == bfd_arch_arm)
6dff2a6f
AB
9272 {
9273 arm_gdbarch_tdep *tdep
08106042 9274 = gdbarch_tdep<arm_gdbarch_tdep> (target_gdbarch ());
6dff2a6f
AB
9275
9276 gdb_printf (file, _("\
28e97307 9277The current ARM floating point model is \"auto\" (currently \"%s\").\n"),
6dff2a6f
AB
9278 fp_model_strings[tdep->fp_model]);
9279 }
28e97307 9280 else
6cb06a8c 9281 gdb_printf (file, _("\
28e97307 9282The current ARM floating point model is \"%s\".\n"),
6cb06a8c 9283 fp_model_strings[arm_fp_model]);
28e97307
DJ
9284}
9285
9286static void
eb4c3f4a 9287arm_set_abi (const char *args, int from_tty,
28e97307
DJ
9288 struct cmd_list_element *c)
9289{
570dc176 9290 int arm_abi;
28e97307
DJ
9291
9292 for (arm_abi = ARM_ABI_AUTO; arm_abi != ARM_ABI_LAST; arm_abi++)
9293 if (strcmp (arm_abi_string, arm_abi_strings[arm_abi]) == 0)
9294 {
aead7601 9295 arm_abi_global = (enum arm_abi_kind) arm_abi;
28e97307
DJ
9296 break;
9297 }
9298
9299 if (arm_abi == ARM_ABI_LAST)
9300 internal_error (__FILE__, __LINE__, _("Invalid ABI accepted: %s."),
9301 arm_abi_string);
9302
9303 arm_update_current_architecture ();
9304}
9305
9306static void
9307arm_show_abi (struct ui_file *file, int from_tty,
9308 struct cmd_list_element *c, const char *value)
9309{
28e97307 9310 if (arm_abi_global == ARM_ABI_AUTO
f5656ead 9311 && gdbarch_bfd_arch_info (target_gdbarch ())->arch == bfd_arch_arm)
6dff2a6f
AB
9312 {
9313 arm_gdbarch_tdep *tdep
08106042 9314 = gdbarch_tdep<arm_gdbarch_tdep> (target_gdbarch ());
6dff2a6f
AB
9315
9316 gdb_printf (file, _("\
28e97307 9317The current ARM ABI is \"auto\" (currently \"%s\").\n"),
6dff2a6f
AB
9318 arm_abi_strings[tdep->arm_abi]);
9319 }
28e97307 9320 else
6cb06a8c
TT
9321 gdb_printf (file, _("The current ARM ABI is \"%s\".\n"),
9322 arm_abi_string);
fd50bc42
RE
9323}
9324
0428b8f5
DJ
9325static void
9326arm_show_fallback_mode (struct ui_file *file, int from_tty,
9327 struct cmd_list_element *c, const char *value)
9328{
6cb06a8c
TT
9329 gdb_printf (file,
9330 _("The current execution mode assumed "
9331 "(when symbols are unavailable) is \"%s\".\n"),
9332 arm_fallback_mode_string);
0428b8f5
DJ
9333}
9334
9335static void
9336arm_show_force_mode (struct ui_file *file, int from_tty,
9337 struct cmd_list_element *c, const char *value)
9338{
6cb06a8c
TT
9339 gdb_printf (file,
9340 _("The current execution mode assumed "
9341 "(even when symbols are available) is \"%s\".\n"),
9342 arm_force_mode_string);
0428b8f5
DJ
9343}
9344
ef273377
CL
9345static void
9346arm_show_unwind_secure_frames (struct ui_file *file, int from_tty,
9347 struct cmd_list_element *c, const char *value)
9348{
9349 gdb_printf (file,
9350 _("Usage of non-secure to secure exception stack unwinding is %s.\n"),
9351 arm_unwind_secure_frames ? "on" : "off");
9352}
9353
afd7eef0
RE
9354/* If the user changes the register disassembly style used for info
9355 register and other commands, we have to also switch the style used
9356 in opcodes for disassembly output. This function is run in the "set
9357 arm disassembly" command, and does that. */
bc90b915
FN
9358
9359static void
eb4c3f4a 9360set_disassembly_style_sfunc (const char *args, int from_tty,
65b48a81 9361 struct cmd_list_element *c)
bc90b915 9362{
65b48a81
PB
9363 /* Convert the short style name into the long style name (eg, reg-names-*)
9364 before calling the generic set_disassembler_options() function. */
9365 std::string long_name = std::string ("reg-names-") + disassembly_style;
9366 set_disassembler_options (&long_name[0]);
9367}
9368
9369static void
9370show_disassembly_style_sfunc (struct ui_file *file, int from_tty,
9371 struct cmd_list_element *c, const char *value)
9372{
9373 struct gdbarch *gdbarch = get_current_arch ();
9374 char *options = get_disassembler_options (gdbarch);
9375 const char *style = "";
9376 int len = 0;
f995bbe8 9377 const char *opt;
65b48a81
PB
9378
9379 FOR_EACH_DISASSEMBLER_OPTION (opt, options)
08dedd66 9380 if (startswith (opt, "reg-names-"))
65b48a81
PB
9381 {
9382 style = &opt[strlen ("reg-names-")];
9383 len = strcspn (style, ",");
9384 }
9385
6cb06a8c 9386 gdb_printf (file, "The disassembly style is \"%.*s\".\n", len, style);
bc90b915
FN
9387}
9388\f
966fbf70 9389/* Return the ARM register name corresponding to register I. */
a208b0cb 9390static const char *
d93859e2 9391arm_register_name (struct gdbarch *gdbarch, int i)
966fbf70 9392{
08106042 9393 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
58d6951d 9394
ecbf5d4f 9395 if (is_s_pseudo (gdbarch, i))
58d6951d 9396 {
ecbf5d4f 9397 static const char *const s_pseudo_names[] = {
58d6951d
DJ
9398 "s0", "s1", "s2", "s3", "s4", "s5", "s6", "s7",
9399 "s8", "s9", "s10", "s11", "s12", "s13", "s14", "s15",
9400 "s16", "s17", "s18", "s19", "s20", "s21", "s22", "s23",
9401 "s24", "s25", "s26", "s27", "s28", "s29", "s30", "s31",
9402 };
9403
ecbf5d4f 9404 return s_pseudo_names[i - tdep->s_pseudo_base];
58d6951d
DJ
9405 }
9406
ecbf5d4f 9407 if (is_q_pseudo (gdbarch, i))
58d6951d 9408 {
ecbf5d4f 9409 static const char *const q_pseudo_names[] = {
58d6951d
DJ
9410 "q0", "q1", "q2", "q3", "q4", "q5", "q6", "q7",
9411 "q8", "q9", "q10", "q11", "q12", "q13", "q14", "q15",
9412 };
9413
ecbf5d4f 9414 return q_pseudo_names[i - tdep->q_pseudo_base];
58d6951d
DJ
9415 }
9416
ae66a8f1
SP
9417 if (is_mve_pseudo (gdbarch, i))
9418 return "p0";
9419
a01567f4
LM
9420 /* RA_AUTH_CODE is used for unwinding only. Do not assign it a name. */
9421 if (is_pacbti_pseudo (gdbarch, i))
9422 return "";
9423
ff6f572f
DJ
9424 if (i >= ARRAY_SIZE (arm_register_names))
9425 /* These registers are only supported on targets which supply
9426 an XML description. */
9427 return "";
9428
ecbf5d4f 9429 /* Non-pseudo registers. */
966fbf70
RE
9430 return arm_register_names[i];
9431}
9432
082fc60d
RE
9433/* Test whether the coff symbol specific value corresponds to a Thumb
9434 function. */
9435
9436static int
9437coff_sym_is_thumb (int val)
9438{
f8bf5763
PM
9439 return (val == C_THUMBEXT
9440 || val == C_THUMBSTAT
9441 || val == C_THUMBEXTFUNC
9442 || val == C_THUMBSTATFUNC
9443 || val == C_THUMBLABEL);
082fc60d
RE
9444}
9445
9446/* arm_coff_make_msymbol_special()
9447 arm_elf_make_msymbol_special()
9448
9449 These functions test whether the COFF or ELF symbol corresponds to
9450 an address in thumb code, and set a "special" bit in a minimal
9451 symbol to indicate that it does. */
9452
34e8f22d 9453static void
082fc60d
RE
9454arm_elf_make_msymbol_special(asymbol *sym, struct minimal_symbol *msym)
9455{
39d911fc
TP
9456 elf_symbol_type *elfsym = (elf_symbol_type *) sym;
9457
9458 if (ARM_GET_SYM_BRANCH_TYPE (elfsym->internal_elf_sym.st_target_internal)
467d42c4 9459 == ST_BRANCH_TO_THUMB)
082fc60d
RE
9460 MSYMBOL_SET_SPECIAL (msym);
9461}
9462
34e8f22d 9463static void
082fc60d
RE
9464arm_coff_make_msymbol_special(int val, struct minimal_symbol *msym)
9465{
9466 if (coff_sym_is_thumb (val))
9467 MSYMBOL_SET_SPECIAL (msym);
9468}
9469
60c5725c
DJ
9470static void
9471arm_record_special_symbol (struct gdbarch *gdbarch, struct objfile *objfile,
9472 asymbol *sym)
9473{
9474 const char *name = bfd_asymbol_name (sym);
bd5766ec 9475 struct arm_per_bfd *data;
60c5725c
DJ
9476 struct arm_mapping_symbol new_map_sym;
9477
9478 gdb_assert (name[0] == '$');
9479 if (name[1] != 'a' && name[1] != 't' && name[1] != 'd')
9480 return;
9481
98badbfd 9482 data = arm_bfd_data_key.get (objfile->obfd.get ());
60c5725c 9483 if (data == NULL)
98badbfd 9484 data = arm_bfd_data_key.emplace (objfile->obfd.get (),
bd5766ec 9485 objfile->obfd->section_count);
54cc7474 9486 arm_mapping_symbol_vec &map
e6f7f6d1 9487 = data->section_maps[bfd_asymbol_section (sym)->index];
60c5725c
DJ
9488
9489 new_map_sym.value = sym->value;
9490 new_map_sym.type = name[1];
9491
4838e44c
SM
9492 /* Insert at the end, the vector will be sorted on first use. */
9493 map.push_back (new_map_sym);
60c5725c
DJ
9494}
9495
756fe439 9496static void
61a1198a 9497arm_write_pc (struct regcache *regcache, CORE_ADDR pc)
756fe439 9498{
ac7936df 9499 struct gdbarch *gdbarch = regcache->arch ();
61a1198a 9500 regcache_cooked_write_unsigned (regcache, ARM_PC_REGNUM, pc);
756fe439
DJ
9501
9502 /* If necessary, set the T bit. */
9503 if (arm_apcs_32)
9504 {
9779414d 9505 ULONGEST val, t_bit;
61a1198a 9506 regcache_cooked_read_unsigned (regcache, ARM_PS_REGNUM, &val);
9779414d
DJ
9507 t_bit = arm_psr_thumb_bit (gdbarch);
9508 if (arm_pc_is_thumb (gdbarch, pc))
9509 regcache_cooked_write_unsigned (regcache, ARM_PS_REGNUM,
9510 val | t_bit);
756fe439 9511 else
61a1198a 9512 regcache_cooked_write_unsigned (regcache, ARM_PS_REGNUM,
9779414d 9513 val & ~t_bit);
756fe439
DJ
9514 }
9515}
123dc839 9516
58d6951d
DJ
9517/* Read the contents of a NEON quad register, by reading from two
9518 double registers. This is used to implement the quad pseudo
9519 registers, and for argument passing in case the quad registers are
9520 missing; vectors are passed in quad registers when using the VFP
9521 ABI, even if a NEON unit is not present. REGNUM is the index of
9522 the quad register, in [0, 15]. */
9523
05d1431c 9524static enum register_status
849d0ba8 9525arm_neon_quad_read (struct gdbarch *gdbarch, readable_regcache *regcache,
58d6951d
DJ
9526 int regnum, gdb_byte *buf)
9527{
9528 char name_buf[4];
9529 gdb_byte reg_buf[8];
9530 int offset, double_regnum;
05d1431c 9531 enum register_status status;
58d6951d 9532
8c042590 9533 xsnprintf (name_buf, sizeof (name_buf), "d%d", regnum << 1);
58d6951d
DJ
9534 double_regnum = user_reg_map_name_to_regnum (gdbarch, name_buf,
9535 strlen (name_buf));
9536
9537 /* d0 is always the least significant half of q0. */
9538 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
9539 offset = 8;
9540 else
9541 offset = 0;
9542
03f50fc8 9543 status = regcache->raw_read (double_regnum, reg_buf);
05d1431c
PA
9544 if (status != REG_VALID)
9545 return status;
58d6951d
DJ
9546 memcpy (buf + offset, reg_buf, 8);
9547
9548 offset = 8 - offset;
03f50fc8 9549 status = regcache->raw_read (double_regnum + 1, reg_buf);
05d1431c
PA
9550 if (status != REG_VALID)
9551 return status;
58d6951d 9552 memcpy (buf + offset, reg_buf, 8);
05d1431c
PA
9553
9554 return REG_VALID;
58d6951d
DJ
9555}
9556
ae66a8f1
SP
9557/* Read the contents of the MVE pseudo register REGNUM and store it
9558 in BUF. */
9559
9560static enum register_status
9561arm_mve_pseudo_read (struct gdbarch *gdbarch, readable_regcache *regcache,
9562 int regnum, gdb_byte *buf)
9563{
08106042 9564 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
ae66a8f1
SP
9565
9566 /* P0 is the first 16 bits of VPR. */
9567 return regcache->raw_read_part (tdep->mve_vpr_regnum, 0, 2, buf);
9568}
9569
05d1431c 9570static enum register_status
849d0ba8 9571arm_pseudo_read (struct gdbarch *gdbarch, readable_regcache *regcache,
58d6951d
DJ
9572 int regnum, gdb_byte *buf)
9573{
9574 const int num_regs = gdbarch_num_regs (gdbarch);
9575 char name_buf[4];
9576 gdb_byte reg_buf[8];
9577 int offset, double_regnum;
08106042 9578 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
58d6951d
DJ
9579
9580 gdb_assert (regnum >= num_regs);
58d6951d 9581
ecbf5d4f
LM
9582 if (is_q_pseudo (gdbarch, regnum))
9583 {
9584 /* Quad-precision register. */
9585 return arm_neon_quad_read (gdbarch, regcache,
9586 regnum - tdep->q_pseudo_base, buf);
9587 }
ae66a8f1
SP
9588 else if (is_mve_pseudo (gdbarch, regnum))
9589 return arm_mve_pseudo_read (gdbarch, regcache, regnum, buf);
58d6951d
DJ
9590 else
9591 {
05d1431c
PA
9592 enum register_status status;
9593
ecbf5d4f 9594 regnum -= tdep->s_pseudo_base;
58d6951d
DJ
9595 /* Single-precision register. */
9596 gdb_assert (regnum < 32);
9597
9598 /* s0 is always the least significant half of d0. */
9599 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
9600 offset = (regnum & 1) ? 0 : 4;
9601 else
9602 offset = (regnum & 1) ? 4 : 0;
9603
8c042590 9604 xsnprintf (name_buf, sizeof (name_buf), "d%d", regnum >> 1);
58d6951d
DJ
9605 double_regnum = user_reg_map_name_to_regnum (gdbarch, name_buf,
9606 strlen (name_buf));
9607
03f50fc8 9608 status = regcache->raw_read (double_regnum, reg_buf);
05d1431c
PA
9609 if (status == REG_VALID)
9610 memcpy (buf, reg_buf + offset, 4);
9611 return status;
58d6951d
DJ
9612 }
9613}
9614
9615/* Store the contents of BUF to a NEON quad register, by writing to
9616 two double registers. This is used to implement the quad pseudo
9617 registers, and for argument passing in case the quad registers are
9618 missing; vectors are passed in quad registers when using the VFP
9619 ABI, even if a NEON unit is not present. REGNUM is the index
9620 of the quad register, in [0, 15]. */
9621
9622static void
9623arm_neon_quad_write (struct gdbarch *gdbarch, struct regcache *regcache,
9624 int regnum, const gdb_byte *buf)
9625{
9626 char name_buf[4];
58d6951d
DJ
9627 int offset, double_regnum;
9628
8c042590 9629 xsnprintf (name_buf, sizeof (name_buf), "d%d", regnum << 1);
58d6951d
DJ
9630 double_regnum = user_reg_map_name_to_regnum (gdbarch, name_buf,
9631 strlen (name_buf));
9632
9633 /* d0 is always the least significant half of q0. */
9634 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
9635 offset = 8;
9636 else
9637 offset = 0;
9638
10eaee5f 9639 regcache->raw_write (double_regnum, buf + offset);
58d6951d 9640 offset = 8 - offset;
10eaee5f 9641 regcache->raw_write (double_regnum + 1, buf + offset);
58d6951d
DJ
9642}
9643
ae66a8f1
SP
9644/* Store the contents of BUF to the MVE pseudo register REGNUM. */
9645
9646static void
9647arm_mve_pseudo_write (struct gdbarch *gdbarch, struct regcache *regcache,
9648 int regnum, const gdb_byte *buf)
9649{
08106042 9650 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
ae66a8f1
SP
9651
9652 /* P0 is the first 16 bits of VPR. */
9653 regcache->raw_write_part (tdep->mve_vpr_regnum, 0, 2, buf);
9654}
9655
58d6951d
DJ
9656static void
9657arm_pseudo_write (struct gdbarch *gdbarch, struct regcache *regcache,
9658 int regnum, const gdb_byte *buf)
9659{
9660 const int num_regs = gdbarch_num_regs (gdbarch);
9661 char name_buf[4];
9662 gdb_byte reg_buf[8];
9663 int offset, double_regnum;
08106042 9664 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
58d6951d
DJ
9665
9666 gdb_assert (regnum >= num_regs);
58d6951d 9667
ecbf5d4f
LM
9668 if (is_q_pseudo (gdbarch, regnum))
9669 {
9670 /* Quad-precision register. */
9671 arm_neon_quad_write (gdbarch, regcache,
9672 regnum - tdep->q_pseudo_base, buf);
9673 }
ae66a8f1
SP
9674 else if (is_mve_pseudo (gdbarch, regnum))
9675 arm_mve_pseudo_write (gdbarch, regcache, regnum, buf);
58d6951d
DJ
9676 else
9677 {
ecbf5d4f 9678 regnum -= tdep->s_pseudo_base;
58d6951d
DJ
9679 /* Single-precision register. */
9680 gdb_assert (regnum < 32);
9681
9682 /* s0 is always the least significant half of d0. */
9683 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
9684 offset = (regnum & 1) ? 0 : 4;
9685 else
9686 offset = (regnum & 1) ? 4 : 0;
9687
8c042590 9688 xsnprintf (name_buf, sizeof (name_buf), "d%d", regnum >> 1);
58d6951d
DJ
9689 double_regnum = user_reg_map_name_to_regnum (gdbarch, name_buf,
9690 strlen (name_buf));
9691
0b883586 9692 regcache->raw_read (double_regnum, reg_buf);
58d6951d 9693 memcpy (reg_buf + offset, buf, 4);
10eaee5f 9694 regcache->raw_write (double_regnum, reg_buf);
58d6951d
DJ
9695 }
9696}
9697
123dc839
DJ
9698static struct value *
9699value_of_arm_user_reg (struct frame_info *frame, const void *baton)
9700{
9a3c8263 9701 const int *reg_p = (const int *) baton;
123dc839
DJ
9702 return value_of_register (*reg_p, frame);
9703}
97e03143 9704\f
70f80edf
JT
9705static enum gdb_osabi
9706arm_elf_osabi_sniffer (bfd *abfd)
97e03143 9707{
2af48f68 9708 unsigned int elfosabi;
70f80edf 9709 enum gdb_osabi osabi = GDB_OSABI_UNKNOWN;
97e03143 9710
70f80edf 9711 elfosabi = elf_elfheader (abfd)->e_ident[EI_OSABI];
97e03143 9712
28e97307
DJ
9713 if (elfosabi == ELFOSABI_ARM)
9714 /* GNU tools use this value. Check note sections in this case,
9715 as well. */
b35c1d1c
TT
9716 {
9717 for (asection *sect : gdb_bfd_sections (abfd))
9718 generic_elf_osabi_sniff_abi_tag_sections (abfd, sect, &osabi);
9719 }
97e03143 9720
28e97307 9721 /* Anything else will be handled by the generic ELF sniffer. */
70f80edf 9722 return osabi;
97e03143
RE
9723}
9724
54483882
YQ
9725static int
9726arm_register_reggroup_p (struct gdbarch *gdbarch, int regnum,
dbf5d61b 9727 const struct reggroup *group)
54483882 9728{
2c291032
YQ
9729 /* FPS register's type is INT, but belongs to float_reggroup. Beside
9730 this, FPS register belongs to save_regroup, restore_reggroup, and
9731 all_reggroup, of course. */
54483882 9732 if (regnum == ARM_FPS_REGNUM)
2c291032
YQ
9733 return (group == float_reggroup
9734 || group == save_reggroup
9735 || group == restore_reggroup
9736 || group == all_reggroup);
54483882
YQ
9737 else
9738 return default_register_reggroup_p (gdbarch, regnum, group);
9739}
9740
25f8c692
JL
9741/* For backward-compatibility we allow two 'g' packet lengths with
9742 the remote protocol depending on whether FPA registers are
9743 supplied. M-profile targets do not have FPA registers, but some
9744 stubs already exist in the wild which use a 'g' packet which
9745 supplies them albeit with dummy values. The packet format which
9746 includes FPA registers should be considered deprecated for
9747 M-profile targets. */
9748
9749static void
9750arm_register_g_packet_guesses (struct gdbarch *gdbarch)
9751{
08106042 9752 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
345bd07c
SM
9753
9754 if (tdep->is_m)
25f8c692 9755 {
d105cce5
AH
9756 const target_desc *tdesc;
9757
25f8c692
JL
9758 /* If we know from the executable this is an M-profile target,
9759 cater for remote targets whose register set layout is the
9760 same as the FPA layout. */
d105cce5 9761 tdesc = arm_read_mprofile_description (ARM_M_TYPE_WITH_FPA);
25f8c692 9762 register_remote_g_packet_guess (gdbarch,
350fab54 9763 ARM_CORE_REGS_SIZE + ARM_FP_REGS_SIZE,
d105cce5 9764 tdesc);
25f8c692
JL
9765
9766 /* The regular M-profile layout. */
d105cce5 9767 tdesc = arm_read_mprofile_description (ARM_M_TYPE_M_PROFILE);
350fab54 9768 register_remote_g_packet_guess (gdbarch, ARM_CORE_REGS_SIZE,
d105cce5 9769 tdesc);
3184d3f9
JL
9770
9771 /* M-profile plus M4F VFP. */
d105cce5 9772 tdesc = arm_read_mprofile_description (ARM_M_TYPE_VFP_D16);
3184d3f9 9773 register_remote_g_packet_guess (gdbarch,
350fab54 9774 ARM_CORE_REGS_SIZE + ARM_VFP2_REGS_SIZE,
d105cce5 9775 tdesc);
ae66a8f1
SP
9776 /* M-profile plus MVE. */
9777 tdesc = arm_read_mprofile_description (ARM_M_TYPE_MVE);
9778 register_remote_g_packet_guess (gdbarch, ARM_CORE_REGS_SIZE
9779 + ARM_VFP2_REGS_SIZE
9780 + ARM_INT_REGISTER_SIZE, tdesc);
9074667a
CL
9781
9782 /* M-profile system (stack pointers). */
9783 tdesc = arm_read_mprofile_description (ARM_M_TYPE_SYSTEM);
9784 register_remote_g_packet_guess (gdbarch, 2 * ARM_INT_REGISTER_SIZE, tdesc);
25f8c692
JL
9785 }
9786
9787 /* Otherwise we don't have a useful guess. */
9788}
9789
7eb89530
YQ
9790/* Implement the code_of_frame_writable gdbarch method. */
9791
9792static int
9793arm_code_of_frame_writable (struct gdbarch *gdbarch, struct frame_info *frame)
9794{
08106042 9795 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
345bd07c
SM
9796
9797 if (tdep->is_m && get_frame_type (frame) == SIGTRAMP_FRAME)
7eb89530
YQ
9798 {
9799 /* M-profile exception frames return to some magic PCs, where
9800 isn't writable at all. */
9801 return 0;
9802 }
9803 else
9804 return 1;
9805}
9806
3426ae57
AH
9807/* Implement gdbarch_gnu_triplet_regexp. If the arch name is arm then allow it
9808 to be postfixed by a version (eg armv7hl). */
9809
9810static const char *
9811arm_gnu_triplet_regexp (struct gdbarch *gdbarch)
9812{
9813 if (strcmp (gdbarch_bfd_arch_info (gdbarch)->arch_name, "arm") == 0)
9814 return "arm(v[^- ]*)?";
9815 return gdbarch_bfd_arch_info (gdbarch)->arch_name;
9816}
9817
a01567f4
LM
9818/* Implement the "get_pc_address_flags" gdbarch method. */
9819
9820static std::string
9821arm_get_pc_address_flags (frame_info *frame, CORE_ADDR pc)
9822{
9823 if (get_frame_pc_masked (frame))
9824 return "PAC";
9825
9826 return "";
9827}
9828
da3c6d4a
MS
9829/* Initialize the current architecture based on INFO. If possible,
9830 re-use an architecture from ARCHES, which is a list of
9831 architectures already created during this debugging session.
97e03143 9832
da3c6d4a
MS
9833 Called e.g. at program startup, when reading a core file, and when
9834 reading a binary file. */
97e03143 9835
39bbf761
RE
9836static struct gdbarch *
9837arm_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
9838{
9839 struct gdbarch *gdbarch;
28e97307
DJ
9840 struct gdbarch_list *best_arch;
9841 enum arm_abi_kind arm_abi = arm_abi_global;
9842 enum arm_float_model fp_model = arm_fp_model;
c1e1314d 9843 tdesc_arch_data_up tdesc_data;
7559c217
CB
9844 int i;
9845 bool is_m = false;
ae7e2f45 9846 bool have_sec_ext = false;
7559c217 9847 int vfp_register_count = 0;
ecbf5d4f 9848 bool have_s_pseudos = false, have_q_pseudos = false;
7559c217
CB
9849 bool have_wmmx_registers = false;
9850 bool have_neon = false;
9851 bool have_fpa_registers = true;
9779414d 9852 const struct target_desc *tdesc = info.target_desc;
ae66a8f1
SP
9853 bool have_vfp = false;
9854 bool have_mve = false;
a01567f4 9855 bool have_pacbti = false;
ae66a8f1 9856 int mve_vpr_regnum = -1;
ecbf5d4f 9857 int register_count = ARM_NUM_REGS;
9074667a
CL
9858 bool have_m_profile_msp = false;
9859 int m_profile_msp_regnum = -1;
9860 int m_profile_psp_regnum = -1;
ae7e2f45
CL
9861 int m_profile_msp_ns_regnum = -1;
9862 int m_profile_psp_ns_regnum = -1;
9863 int m_profile_msp_s_regnum = -1;
9864 int m_profile_psp_s_regnum = -1;
92d48a1e 9865 int tls_regnum = 0;
9779414d
DJ
9866
9867 /* If we have an object to base this architecture on, try to determine
9868 its ABI. */
9869
9870 if (arm_abi == ARM_ABI_AUTO && info.abfd != NULL)
9871 {
9872 int ei_osabi, e_flags;
9873
9874 switch (bfd_get_flavour (info.abfd))
9875 {
9779414d
DJ
9876 case bfd_target_coff_flavour:
9877 /* Assume it's an old APCS-style ABI. */
9878 /* XXX WinCE? */
9879 arm_abi = ARM_ABI_APCS;
9880 break;
9881
9882 case bfd_target_elf_flavour:
9883 ei_osabi = elf_elfheader (info.abfd)->e_ident[EI_OSABI];
9884 e_flags = elf_elfheader (info.abfd)->e_flags;
9885
9886 if (ei_osabi == ELFOSABI_ARM)
9887 {
9888 /* GNU tools used to use this value, but do not for EABI
9889 objects. There's nowhere to tag an EABI version
9890 anyway, so assume APCS. */
9891 arm_abi = ARM_ABI_APCS;
9892 }
d403db27 9893 else if (ei_osabi == ELFOSABI_NONE || ei_osabi == ELFOSABI_GNU)
9779414d
DJ
9894 {
9895 int eabi_ver = EF_ARM_EABI_VERSION (e_flags);
9779414d
DJ
9896
9897 switch (eabi_ver)
9898 {
9899 case EF_ARM_EABI_UNKNOWN:
9900 /* Assume GNU tools. */
9901 arm_abi = ARM_ABI_APCS;
9902 break;
9903
9904 case EF_ARM_EABI_VER4:
9905 case EF_ARM_EABI_VER5:
9906 arm_abi = ARM_ABI_AAPCS;
9907 /* EABI binaries default to VFP float ordering.
9908 They may also contain build attributes that can
9909 be used to identify if the VFP argument-passing
9910 ABI is in use. */
9911 if (fp_model == ARM_FLOAT_AUTO)
9912 {
9913#ifdef HAVE_ELF
9914 switch (bfd_elf_get_obj_attr_int (info.abfd,
9915 OBJ_ATTR_PROC,
9916 Tag_ABI_VFP_args))
9917 {
b35b0298 9918 case AEABI_VFP_args_base:
9779414d
DJ
9919 /* "The user intended FP parameter/result
9920 passing to conform to AAPCS, base
9921 variant". */
9922 fp_model = ARM_FLOAT_SOFT_VFP;
9923 break;
b35b0298 9924 case AEABI_VFP_args_vfp:
9779414d
DJ
9925 /* "The user intended FP parameter/result
9926 passing to conform to AAPCS, VFP
9927 variant". */
9928 fp_model = ARM_FLOAT_VFP;
9929 break;
b35b0298 9930 case AEABI_VFP_args_toolchain:
9779414d
DJ
9931 /* "The user intended FP parameter/result
9932 passing to conform to tool chain-specific
9933 conventions" - we don't know any such
9934 conventions, so leave it as "auto". */
9935 break;
b35b0298 9936 case AEABI_VFP_args_compatible:
5c294fee
TG
9937 /* "Code is compatible with both the base
9938 and VFP variants; the user did not permit
9939 non-variadic functions to pass FP
9940 parameters/results" - leave it as
9941 "auto". */
9942 break;
9779414d
DJ
9943 default:
9944 /* Attribute value not mentioned in the
5c294fee 9945 November 2012 ABI, so leave it as
9779414d
DJ
9946 "auto". */
9947 break;
9948 }
9949#else
9950 fp_model = ARM_FLOAT_SOFT_VFP;
9951#endif
9952 }
9953 break;
9954
9955 default:
9956 /* Leave it as "auto". */
9957 warning (_("unknown ARM EABI version 0x%x"), eabi_ver);
9958 break;
9959 }
9960
9961#ifdef HAVE_ELF
9962 /* Detect M-profile programs. This only works if the
9963 executable file includes build attributes; GCC does
9964 copy them to the executable, but e.g. RealView does
9965 not. */
17cbafdb
SM
9966 int attr_arch
9967 = bfd_elf_get_obj_attr_int (info.abfd, OBJ_ATTR_PROC,
9968 Tag_CPU_arch);
9969 int attr_profile
9970 = bfd_elf_get_obj_attr_int (info.abfd, OBJ_ATTR_PROC,
9971 Tag_CPU_arch_profile);
9972
9779414d
DJ
9973 /* GCC specifies the profile for v6-M; RealView only
9974 specifies the profile for architectures starting with
9975 V7 (as opposed to architectures with a tag
9976 numerically greater than TAG_CPU_ARCH_V7). */
9977 if (!tdesc_has_registers (tdesc)
9978 && (attr_arch == TAG_CPU_ARCH_V6_M
9979 || attr_arch == TAG_CPU_ARCH_V6S_M
131a355f
LM
9980 || attr_arch == TAG_CPU_ARCH_V7E_M
9981 || attr_arch == TAG_CPU_ARCH_V8M_BASE
9982 || attr_arch == TAG_CPU_ARCH_V8M_MAIN
ae66a8f1 9983 || attr_arch == TAG_CPU_ARCH_V8_1M_MAIN
9779414d 9984 || attr_profile == 'M'))
7559c217 9985 is_m = true;
a01567f4
LM
9986
9987 /* Look for attributes that indicate support for ARMv8.1-m
9988 PACBTI. */
9989 if (!tdesc_has_registers (tdesc) && is_m)
9990 {
9991 int attr_pac_extension
9992 = bfd_elf_get_obj_attr_int (info.abfd, OBJ_ATTR_PROC,
9993 Tag_PAC_extension);
9994
9995 int attr_bti_extension
9996 = bfd_elf_get_obj_attr_int (info.abfd, OBJ_ATTR_PROC,
9997 Tag_BTI_extension);
9998
9999 int attr_pacret_use
10000 = bfd_elf_get_obj_attr_int (info.abfd, OBJ_ATTR_PROC,
10001 Tag_PACRET_use);
10002
10003 int attr_bti_use
10004 = bfd_elf_get_obj_attr_int (info.abfd, OBJ_ATTR_PROC,
10005 Tag_BTI_use);
10006
10007 if (attr_pac_extension != 0 || attr_bti_extension != 0
10008 || attr_pacret_use != 0 || attr_bti_use != 0)
10009 have_pacbti = true;
10010 }
9779414d
DJ
10011#endif
10012 }
10013
10014 if (fp_model == ARM_FLOAT_AUTO)
10015 {
9779414d
DJ
10016 switch (e_flags & (EF_ARM_SOFT_FLOAT | EF_ARM_VFP_FLOAT))
10017 {
10018 case 0:
10019 /* Leave it as "auto". Strictly speaking this case
10020 means FPA, but almost nobody uses that now, and
10021 many toolchains fail to set the appropriate bits
10022 for the floating-point model they use. */
10023 break;
10024 case EF_ARM_SOFT_FLOAT:
10025 fp_model = ARM_FLOAT_SOFT_FPA;
10026 break;
10027 case EF_ARM_VFP_FLOAT:
10028 fp_model = ARM_FLOAT_VFP;
10029 break;
10030 case EF_ARM_SOFT_FLOAT | EF_ARM_VFP_FLOAT:
10031 fp_model = ARM_FLOAT_SOFT_VFP;
10032 break;
10033 }
10034 }
10035
10036 if (e_flags & EF_ARM_BE8)
10037 info.byte_order_for_code = BFD_ENDIAN_LITTLE;
10038
10039 break;
10040
10041 default:
10042 /* Leave it as "auto". */
10043 break;
10044 }
10045 }
123dc839
DJ
10046
10047 /* Check any target description for validity. */
9779414d 10048 if (tdesc_has_registers (tdesc))
123dc839
DJ
10049 {
10050 /* For most registers we require GDB's default names; but also allow
10051 the numeric names for sp / lr / pc, as a convenience. */
10052 static const char *const arm_sp_names[] = { "r13", "sp", NULL };
10053 static const char *const arm_lr_names[] = { "r14", "lr", NULL };
10054 static const char *const arm_pc_names[] = { "r15", "pc", NULL };
10055
10056 const struct tdesc_feature *feature;
58d6951d 10057 int valid_p;
123dc839 10058
9779414d 10059 feature = tdesc_find_feature (tdesc,
123dc839
DJ
10060 "org.gnu.gdb.arm.core");
10061 if (feature == NULL)
9779414d
DJ
10062 {
10063 feature = tdesc_find_feature (tdesc,
10064 "org.gnu.gdb.arm.m-profile");
10065 if (feature == NULL)
10066 return NULL;
10067 else
7559c217 10068 is_m = true;
9779414d 10069 }
123dc839
DJ
10070
10071 tdesc_data = tdesc_data_alloc ();
10072
10073 valid_p = 1;
10074 for (i = 0; i < ARM_SP_REGNUM; i++)
c1e1314d 10075 valid_p &= tdesc_numbered_register (feature, tdesc_data.get (), i,
123dc839 10076 arm_register_names[i]);
c1e1314d 10077 valid_p &= tdesc_numbered_register_choices (feature, tdesc_data.get (),
123dc839
DJ
10078 ARM_SP_REGNUM,
10079 arm_sp_names);
c1e1314d 10080 valid_p &= tdesc_numbered_register_choices (feature, tdesc_data.get (),
123dc839
DJ
10081 ARM_LR_REGNUM,
10082 arm_lr_names);
c1e1314d 10083 valid_p &= tdesc_numbered_register_choices (feature, tdesc_data.get (),
123dc839
DJ
10084 ARM_PC_REGNUM,
10085 arm_pc_names);
9779414d 10086 if (is_m)
c1e1314d 10087 valid_p &= tdesc_numbered_register (feature, tdesc_data.get (),
9779414d
DJ
10088 ARM_PS_REGNUM, "xpsr");
10089 else
c1e1314d 10090 valid_p &= tdesc_numbered_register (feature, tdesc_data.get (),
9779414d 10091 ARM_PS_REGNUM, "cpsr");
123dc839
DJ
10092
10093 if (!valid_p)
c1e1314d 10094 return NULL;
123dc839 10095
9074667a
CL
10096 if (is_m)
10097 {
10098 feature = tdesc_find_feature (tdesc,
10099 "org.gnu.gdb.arm.m-system");
10100 if (feature != nullptr)
10101 {
10102 /* MSP */
10103 valid_p &= tdesc_numbered_register (feature, tdesc_data.get (),
10104 register_count, "msp");
10105 if (!valid_p)
10106 {
10107 warning (_("M-profile m-system feature is missing required register msp."));
10108 return nullptr;
10109 }
10110 have_m_profile_msp = true;
10111 m_profile_msp_regnum = register_count++;
10112
10113 /* PSP */
10114 valid_p &= tdesc_numbered_register (feature, tdesc_data.get (),
10115 register_count, "psp");
10116 if (!valid_p)
10117 {
10118 warning (_("M-profile m-system feature is missing required register psp."));
10119 return nullptr;
10120 }
10121 m_profile_psp_regnum = register_count++;
10122 }
10123 }
10124
9779414d 10125 feature = tdesc_find_feature (tdesc,
123dc839
DJ
10126 "org.gnu.gdb.arm.fpa");
10127 if (feature != NULL)
10128 {
10129 valid_p = 1;
10130 for (i = ARM_F0_REGNUM; i <= ARM_FPS_REGNUM; i++)
c1e1314d 10131 valid_p &= tdesc_numbered_register (feature, tdesc_data.get (), i,
123dc839
DJ
10132 arm_register_names[i]);
10133 if (!valid_p)
c1e1314d 10134 return NULL;
123dc839 10135 }
ff6f572f 10136 else
7559c217 10137 have_fpa_registers = false;
ff6f572f 10138
9779414d 10139 feature = tdesc_find_feature (tdesc,
ff6f572f
DJ
10140 "org.gnu.gdb.xscale.iwmmxt");
10141 if (feature != NULL)
10142 {
10143 static const char *const iwmmxt_names[] = {
10144 "wR0", "wR1", "wR2", "wR3", "wR4", "wR5", "wR6", "wR7",
10145 "wR8", "wR9", "wR10", "wR11", "wR12", "wR13", "wR14", "wR15",
10146 "wCID", "wCon", "wCSSF", "wCASF", "", "", "", "",
10147 "wCGR0", "wCGR1", "wCGR2", "wCGR3", "", "", "", "",
10148 };
10149
10150 valid_p = 1;
10151 for (i = ARM_WR0_REGNUM; i <= ARM_WR15_REGNUM; i++)
10152 valid_p
c1e1314d 10153 &= tdesc_numbered_register (feature, tdesc_data.get (), i,
ff6f572f
DJ
10154 iwmmxt_names[i - ARM_WR0_REGNUM]);
10155
10156 /* Check for the control registers, but do not fail if they
10157 are missing. */
10158 for (i = ARM_WC0_REGNUM; i <= ARM_WCASF_REGNUM; i++)
c1e1314d 10159 tdesc_numbered_register (feature, tdesc_data.get (), i,
ff6f572f
DJ
10160 iwmmxt_names[i - ARM_WR0_REGNUM]);
10161
10162 for (i = ARM_WCGR0_REGNUM; i <= ARM_WCGR3_REGNUM; i++)
10163 valid_p
c1e1314d 10164 &= tdesc_numbered_register (feature, tdesc_data.get (), i,
ff6f572f
DJ
10165 iwmmxt_names[i - ARM_WR0_REGNUM]);
10166
10167 if (!valid_p)
c1e1314d 10168 return NULL;
a56cc1ce 10169
7559c217 10170 have_wmmx_registers = true;
ff6f572f 10171 }
58d6951d
DJ
10172
10173 /* If we have a VFP unit, check whether the single precision registers
10174 are present. If not, then we will synthesize them as pseudo
10175 registers. */
9779414d 10176 feature = tdesc_find_feature (tdesc,
58d6951d
DJ
10177 "org.gnu.gdb.arm.vfp");
10178 if (feature != NULL)
10179 {
10180 static const char *const vfp_double_names[] = {
10181 "d0", "d1", "d2", "d3", "d4", "d5", "d6", "d7",
10182 "d8", "d9", "d10", "d11", "d12", "d13", "d14", "d15",
10183 "d16", "d17", "d18", "d19", "d20", "d21", "d22", "d23",
10184 "d24", "d25", "d26", "d27", "d28", "d29", "d30", "d31",
10185 };
10186
10187 /* Require the double precision registers. There must be either
10188 16 or 32. */
10189 valid_p = 1;
10190 for (i = 0; i < 32; i++)
10191 {
c1e1314d 10192 valid_p &= tdesc_numbered_register (feature, tdesc_data.get (),
58d6951d
DJ
10193 ARM_D0_REGNUM + i,
10194 vfp_double_names[i]);
10195 if (!valid_p)
10196 break;
10197 }
2b9e5ea6
UW
10198 if (!valid_p && i == 16)
10199 valid_p = 1;
58d6951d 10200
2b9e5ea6 10201 /* Also require FPSCR. */
c1e1314d 10202 valid_p &= tdesc_numbered_register (feature, tdesc_data.get (),
2b9e5ea6
UW
10203 ARM_FPSCR_REGNUM, "fpscr");
10204 if (!valid_p)
c1e1314d 10205 return NULL;
58d6951d 10206
ae66a8f1
SP
10207 have_vfp = true;
10208
58d6951d 10209 if (tdesc_unnumbered_register (feature, "s0") == 0)
ecbf5d4f 10210 have_s_pseudos = true;
58d6951d 10211
330c6ca9 10212 vfp_register_count = i;
58d6951d
DJ
10213
10214 /* If we have VFP, also check for NEON. The architecture allows
10215 NEON without VFP (integer vector operations only), but GDB
10216 does not support that. */
9779414d 10217 feature = tdesc_find_feature (tdesc,
58d6951d
DJ
10218 "org.gnu.gdb.arm.neon");
10219 if (feature != NULL)
10220 {
10221 /* NEON requires 32 double-precision registers. */
10222 if (i != 32)
c1e1314d 10223 return NULL;
58d6951d
DJ
10224
10225 /* If there are quad registers defined by the stub, use
10226 their type; otherwise (normally) provide them with
10227 the default type. */
10228 if (tdesc_unnumbered_register (feature, "q0") == 0)
ecbf5d4f 10229 have_q_pseudos = true;
ae66a8f1
SP
10230 }
10231 }
10232
92d48a1e
JB
10233 /* Check for the TLS register feature. */
10234 feature = tdesc_find_feature (tdesc, "org.gnu.gdb.arm.tls");
10235 if (feature != nullptr)
10236 {
10237 valid_p &= tdesc_numbered_register (feature, tdesc_data.get (),
10238 register_count, "tpidruro");
10239 if (!valid_p)
10240 return nullptr;
10241
10242 tls_regnum = register_count;
10243 register_count++;
10244 }
10245
ae66a8f1
SP
10246 /* Check for MVE after all the checks for GPR's, VFP and Neon.
10247 MVE (Helium) is an M-profile extension. */
10248 if (is_m)
10249 {
10250 /* Do we have the MVE feature? */
10251 feature = tdesc_find_feature (tdesc,"org.gnu.gdb.arm.m-profile-mve");
10252
10253 if (feature != nullptr)
10254 {
10255 /* If we have MVE, we must always have the VPR register. */
10256 valid_p &= tdesc_numbered_register (feature, tdesc_data.get (),
10257 register_count, "vpr");
10258 if (!valid_p)
10259 {
10260 warning (_("MVE feature is missing required register vpr."));
10261 return nullptr;
10262 }
58d6951d 10263
ae66a8f1
SP
10264 have_mve = true;
10265 mve_vpr_regnum = register_count;
10266 register_count++;
10267
10268 /* We can't have Q pseudo registers available here, as that
10269 would mean we have NEON features, and that is only available
10270 on A and R profiles. */
10271 gdb_assert (!have_q_pseudos);
10272
10273 /* Given we have a M-profile target description, if MVE is
10274 enabled and there are VFP registers, we should have Q
10275 pseudo registers (Q0 ~ Q7). */
10276 if (have_vfp)
10277 have_q_pseudos = true;
58d6951d 10278 }
a01567f4
LM
10279
10280 /* Do we have the ARMv8.1-m PACBTI feature? */
10281 feature = tdesc_find_feature (tdesc,
10282 "org.gnu.gdb.arm.m-profile-pacbti");
10283 if (feature != nullptr)
10284 {
10285 /* By advertising this feature, the target acknowledges the
10286 presence of the ARMv8.1-m PACBTI extensions.
10287
10288 We don't care for any particular registers in this group, so
10289 the target is free to include whatever it deems appropriate.
10290
10291 The expectation is for this feature to include the PAC
10292 keys. */
10293 have_pacbti = true;
10294 }
ae7e2f45
CL
10295
10296 /* Do we have the Security extension? */
10297 feature = tdesc_find_feature (tdesc,
10298 "org.gnu.gdb.arm.secext");
10299 if (feature != nullptr)
10300 {
10301 /* Secure/Non-secure stack pointers. */
10302 /* MSP_NS */
10303 valid_p &= tdesc_numbered_register (feature, tdesc_data.get (),
10304 register_count, "msp_ns");
10305 if (!valid_p)
10306 {
10307 warning (_("M-profile secext feature is missing required register msp_ns."));
10308 return nullptr;
10309 }
10310 m_profile_msp_ns_regnum = register_count++;
10311
10312 /* PSP_NS */
10313 valid_p &= tdesc_numbered_register (feature, tdesc_data.get (),
10314 register_count, "psp_ns");
10315 if (!valid_p)
10316 {
10317 warning (_("M-profile secext feature is missing required register psp_ns."));
10318 return nullptr;
10319 }
10320 m_profile_psp_ns_regnum = register_count++;
10321
10322 /* MSP_S */
10323 valid_p &= tdesc_numbered_register (feature, tdesc_data.get (),
10324 register_count, "msp_s");
10325 if (!valid_p)
10326 {
10327 warning (_("M-profile secext feature is missing required register msp_s."));
10328 return nullptr;
10329 }
10330 m_profile_msp_s_regnum = register_count++;
10331
10332 /* PSP_S */
10333 valid_p &= tdesc_numbered_register (feature, tdesc_data.get (),
10334 register_count, "psp_s");
10335 if (!valid_p)
10336 {
10337 warning (_("M-profile secext feature is missing required register psp_s."));
10338 return nullptr;
10339 }
10340 m_profile_psp_s_regnum = register_count++;
10341
10342 have_sec_ext = true;
10343 }
10344
58d6951d 10345 }
123dc839 10346 }
39bbf761 10347
28e97307
DJ
10348 /* If there is already a candidate, use it. */
10349 for (best_arch = gdbarch_list_lookup_by_info (arches, &info);
10350 best_arch != NULL;
10351 best_arch = gdbarch_list_lookup_by_info (best_arch->next, &info))
10352 {
345bd07c 10353 arm_gdbarch_tdep *tdep
08106042 10354 = gdbarch_tdep<arm_gdbarch_tdep> (best_arch->gdbarch);
345bd07c
SM
10355
10356 if (arm_abi != ARM_ABI_AUTO && arm_abi != tdep->arm_abi)
28e97307
DJ
10357 continue;
10358
345bd07c 10359 if (fp_model != ARM_FLOAT_AUTO && fp_model != tdep->fp_model)
28e97307
DJ
10360 continue;
10361
58d6951d
DJ
10362 /* There are various other properties in tdep that we do not
10363 need to check here: those derived from a target description,
10364 since gdbarches with a different target description are
10365 automatically disqualified. */
10366
9779414d 10367 /* Do check is_m, though, since it might come from the binary. */
345bd07c 10368 if (is_m != tdep->is_m)
9779414d
DJ
10369 continue;
10370
a01567f4
LM
10371 /* Also check for ARMv8.1-m PACBTI support, since it might come from
10372 the binary. */
10373 if (have_pacbti != tdep->have_pacbti)
10374 continue;
10375
28e97307
DJ
10376 /* Found a match. */
10377 break;
10378 }
97e03143 10379
28e97307 10380 if (best_arch != NULL)
c1e1314d 10381 return best_arch->gdbarch;
28e97307 10382
345bd07c 10383 arm_gdbarch_tdep *tdep = new arm_gdbarch_tdep;
97e03143
RE
10384 gdbarch = gdbarch_alloc (&info, tdep);
10385
28e97307
DJ
10386 /* Record additional information about the architecture we are defining.
10387 These are gdbarch discriminators, like the OSABI. */
10388 tdep->arm_abi = arm_abi;
10389 tdep->fp_model = fp_model;
9779414d 10390 tdep->is_m = is_m;
ae7e2f45 10391 tdep->have_sec_ext = have_sec_ext;
ff6f572f 10392 tdep->have_fpa_registers = have_fpa_registers;
a56cc1ce 10393 tdep->have_wmmx_registers = have_wmmx_registers;
330c6ca9
YQ
10394 gdb_assert (vfp_register_count == 0
10395 || vfp_register_count == 16
10396 || vfp_register_count == 32);
10397 tdep->vfp_register_count = vfp_register_count;
ecbf5d4f
LM
10398 tdep->have_s_pseudos = have_s_pseudos;
10399 tdep->have_q_pseudos = have_q_pseudos;
58d6951d 10400 tdep->have_neon = have_neon;
92d48a1e 10401 tdep->tls_regnum = tls_regnum;
08216dd7 10402
ae66a8f1
SP
10403 /* Adjust the MVE feature settings. */
10404 if (have_mve)
10405 {
10406 tdep->have_mve = true;
10407 tdep->mve_vpr_regnum = mve_vpr_regnum;
10408 }
10409
a01567f4
LM
10410 /* Adjust the PACBTI feature settings. */
10411 tdep->have_pacbti = have_pacbti;
10412
9074667a
CL
10413 /* Adjust the M-profile stack pointers settings. */
10414 if (have_m_profile_msp)
10415 {
10416 tdep->m_profile_msp_regnum = m_profile_msp_regnum;
10417 tdep->m_profile_psp_regnum = m_profile_psp_regnum;
ae7e2f45
CL
10418 tdep->m_profile_msp_ns_regnum = m_profile_msp_ns_regnum;
10419 tdep->m_profile_psp_ns_regnum = m_profile_psp_ns_regnum;
10420 tdep->m_profile_msp_s_regnum = m_profile_msp_s_regnum;
10421 tdep->m_profile_psp_s_regnum = m_profile_psp_s_regnum;
9074667a
CL
10422 }
10423
25f8c692
JL
10424 arm_register_g_packet_guesses (gdbarch);
10425
08216dd7 10426 /* Breakpoints. */
9d4fde75 10427 switch (info.byte_order_for_code)
67255d04
RE
10428 {
10429 case BFD_ENDIAN_BIG:
66e810cd
RE
10430 tdep->arm_breakpoint = arm_default_arm_be_breakpoint;
10431 tdep->arm_breakpoint_size = sizeof (arm_default_arm_be_breakpoint);
10432 tdep->thumb_breakpoint = arm_default_thumb_be_breakpoint;
10433 tdep->thumb_breakpoint_size = sizeof (arm_default_thumb_be_breakpoint);
10434
67255d04
RE
10435 break;
10436
10437 case BFD_ENDIAN_LITTLE:
66e810cd
RE
10438 tdep->arm_breakpoint = arm_default_arm_le_breakpoint;
10439 tdep->arm_breakpoint_size = sizeof (arm_default_arm_le_breakpoint);
10440 tdep->thumb_breakpoint = arm_default_thumb_le_breakpoint;
10441 tdep->thumb_breakpoint_size = sizeof (arm_default_thumb_le_breakpoint);
10442
67255d04
RE
10443 break;
10444
10445 default:
10446 internal_error (__FILE__, __LINE__,
edefbb7c 10447 _("arm_gdbarch_init: bad byte order for float format"));
67255d04
RE
10448 }
10449
d7b486e7
RE
10450 /* On ARM targets char defaults to unsigned. */
10451 set_gdbarch_char_signed (gdbarch, 0);
10452
53375380
PA
10453 /* wchar_t is unsigned under the AAPCS. */
10454 if (tdep->arm_abi == ARM_ABI_AAPCS)
10455 set_gdbarch_wchar_signed (gdbarch, 0);
10456 else
10457 set_gdbarch_wchar_signed (gdbarch, 1);
53375380 10458
030197b4
AB
10459 /* Compute type alignment. */
10460 set_gdbarch_type_align (gdbarch, arm_type_align);
10461
cca44b1b
JB
10462 /* Note: for displaced stepping, this includes the breakpoint, and one word
10463 of additional scratch space. This setting isn't used for anything beside
10464 displaced stepping at present. */
e935475c 10465 set_gdbarch_max_insn_length (gdbarch, 4 * ARM_DISPLACED_MODIFIED_INSNS);
cca44b1b 10466
9df628e0 10467 /* This should be low enough for everything. */
97e03143 10468 tdep->lowest_pc = 0x20;
94c30b78 10469 tdep->jb_pc = -1; /* Longjump support not enabled by default. */
97e03143 10470
7c00367c
MK
10471 /* The default, for both APCS and AAPCS, is to return small
10472 structures in registers. */
10473 tdep->struct_return = reg_struct_return;
10474
2dd604e7 10475 set_gdbarch_push_dummy_call (gdbarch, arm_push_dummy_call);
f53f0d0b 10476 set_gdbarch_frame_align (gdbarch, arm_frame_align);
39bbf761 10477
7eb89530
YQ
10478 if (is_m)
10479 set_gdbarch_code_of_frame_writable (gdbarch, arm_code_of_frame_writable);
10480
756fe439
DJ
10481 set_gdbarch_write_pc (gdbarch, arm_write_pc);
10482
eb5492fa 10483 frame_base_set_default (gdbarch, &arm_normal_base);
148754e5 10484
34e8f22d 10485 /* Address manipulation. */
34e8f22d
RE
10486 set_gdbarch_addr_bits_remove (gdbarch, arm_addr_bits_remove);
10487
34e8f22d
RE
10488 /* Advance PC across function entry code. */
10489 set_gdbarch_skip_prologue (gdbarch, arm_skip_prologue);
10490
c9cf6e20
MG
10491 /* Detect whether PC is at a point where the stack has been destroyed. */
10492 set_gdbarch_stack_frame_destroyed_p (gdbarch, arm_stack_frame_destroyed_p);
4024ca99 10493
190dce09
UW
10494 /* Skip trampolines. */
10495 set_gdbarch_skip_trampoline_code (gdbarch, arm_skip_stub);
10496
34e8f22d
RE
10497 /* The stack grows downward. */
10498 set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
10499
10500 /* Breakpoint manipulation. */
04180708
YQ
10501 set_gdbarch_breakpoint_kind_from_pc (gdbarch, arm_breakpoint_kind_from_pc);
10502 set_gdbarch_sw_breakpoint_from_kind (gdbarch, arm_sw_breakpoint_from_kind);
833b7ab5
YQ
10503 set_gdbarch_breakpoint_kind_from_current_state (gdbarch,
10504 arm_breakpoint_kind_from_current_state);
34e8f22d
RE
10505
10506 /* Information about registers, etc. */
34e8f22d
RE
10507 set_gdbarch_sp_regnum (gdbarch, ARM_SP_REGNUM);
10508 set_gdbarch_pc_regnum (gdbarch, ARM_PC_REGNUM);
ecbf5d4f 10509 set_gdbarch_num_regs (gdbarch, register_count);
7a5ea0d4 10510 set_gdbarch_register_type (gdbarch, arm_register_type);
54483882 10511 set_gdbarch_register_reggroup_p (gdbarch, arm_register_reggroup_p);
34e8f22d 10512
ff6f572f
DJ
10513 /* This "info float" is FPA-specific. Use the generic version if we
10514 do not have FPA. */
345bd07c 10515 if (tdep->have_fpa_registers)
ff6f572f
DJ
10516 set_gdbarch_print_float_info (gdbarch, arm_print_float_info);
10517
26216b98 10518 /* Internal <-> external register number maps. */
ff6f572f 10519 set_gdbarch_dwarf2_reg_to_regnum (gdbarch, arm_dwarf_reg_to_regnum);
26216b98
AC
10520 set_gdbarch_register_sim_regno (gdbarch, arm_register_sim_regno);
10521
34e8f22d
RE
10522 set_gdbarch_register_name (gdbarch, arm_register_name);
10523
10524 /* Returning results. */
2af48f68 10525 set_gdbarch_return_value (gdbarch, arm_return_value);
34e8f22d 10526
03d48a7d
RE
10527 /* Disassembly. */
10528 set_gdbarch_print_insn (gdbarch, gdb_print_insn_arm);
10529
34e8f22d
RE
10530 /* Minsymbol frobbing. */
10531 set_gdbarch_elf_make_msymbol_special (gdbarch, arm_elf_make_msymbol_special);
10532 set_gdbarch_coff_make_msymbol_special (gdbarch,
10533 arm_coff_make_msymbol_special);
60c5725c 10534 set_gdbarch_record_special_symbol (gdbarch, arm_record_special_symbol);
34e8f22d 10535
f9d67f43
DJ
10536 /* Thumb-2 IT block support. */
10537 set_gdbarch_adjust_breakpoint_address (gdbarch,
10538 arm_adjust_breakpoint_address);
10539
0d5de010
DJ
10540 /* Virtual tables. */
10541 set_gdbarch_vbit_in_delta (gdbarch, 1);
10542
97e03143 10543 /* Hook in the ABI-specific overrides, if they have been registered. */
4be87837 10544 gdbarch_init_osabi (info, gdbarch);
97e03143 10545
b39cc962
DJ
10546 dwarf2_frame_set_init_reg (gdbarch, arm_dwarf2_frame_init_reg);
10547
eb5492fa 10548 /* Add some default predicates. */
2ae28aa9
YQ
10549 if (is_m)
10550 frame_unwind_append_unwinder (gdbarch, &arm_m_exception_unwind);
a262aec2
DJ
10551 frame_unwind_append_unwinder (gdbarch, &arm_stub_unwind);
10552 dwarf2_append_unwinders (gdbarch);
0e9e9abd 10553 frame_unwind_append_unwinder (gdbarch, &arm_exidx_unwind);
779aa56f 10554 frame_unwind_append_unwinder (gdbarch, &arm_epilogue_frame_unwind);
a262aec2 10555 frame_unwind_append_unwinder (gdbarch, &arm_prologue_unwind);
eb5492fa 10556
97e03143
RE
10557 /* Now we have tuned the configuration, set a few final things,
10558 based on what the OS ABI has told us. */
10559
b8926edc
DJ
10560 /* If the ABI is not otherwise marked, assume the old GNU APCS. EABI
10561 binaries are always marked. */
10562 if (tdep->arm_abi == ARM_ABI_AUTO)
10563 tdep->arm_abi = ARM_ABI_APCS;
10564
e3039479
UW
10565 /* Watchpoints are not steppable. */
10566 set_gdbarch_have_nonsteppable_watchpoint (gdbarch, 1);
10567
b8926edc
DJ
10568 /* We used to default to FPA for generic ARM, but almost nobody
10569 uses that now, and we now provide a way for the user to force
10570 the model. So default to the most useful variant. */
10571 if (tdep->fp_model == ARM_FLOAT_AUTO)
10572 tdep->fp_model = ARM_FLOAT_SOFT_FPA;
10573
9df628e0
RE
10574 if (tdep->jb_pc >= 0)
10575 set_gdbarch_get_longjmp_target (gdbarch, arm_get_longjmp_target);
10576
08216dd7 10577 /* Floating point sizes and format. */
8da61cc4 10578 set_gdbarch_float_format (gdbarch, floatformats_ieee_single);
b8926edc 10579 if (tdep->fp_model == ARM_FLOAT_SOFT_FPA || tdep->fp_model == ARM_FLOAT_FPA)
08216dd7 10580 {
8da61cc4
DJ
10581 set_gdbarch_double_format
10582 (gdbarch, floatformats_ieee_double_littlebyte_bigword);
10583 set_gdbarch_long_double_format
10584 (gdbarch, floatformats_ieee_double_littlebyte_bigword);
10585 }
10586 else
10587 {
10588 set_gdbarch_double_format (gdbarch, floatformats_ieee_double);
10589 set_gdbarch_long_double_format (gdbarch, floatformats_ieee_double);
08216dd7
RE
10590 }
10591
a01567f4
LM
10592 /* Hook used to decorate frames with signed return addresses, only available
10593 for ARMv8.1-m PACBTI. */
10594 if (is_m && have_pacbti)
10595 set_gdbarch_get_pc_address_flags (gdbarch, arm_get_pc_address_flags);
10596
dc22c61a
LM
10597 if (tdesc_data != nullptr)
10598 {
10599 set_tdesc_pseudo_register_name (gdbarch, arm_register_name);
10600
10601 tdesc_use_registers (gdbarch, tdesc, std::move (tdesc_data));
ecbf5d4f 10602 register_count = gdbarch_num_regs (gdbarch);
dc22c61a
LM
10603
10604 /* Override tdesc_register_type to adjust the types of VFP
10605 registers for NEON. */
10606 set_gdbarch_register_type (gdbarch, arm_register_type);
10607 }
10608
ecbf5d4f 10609 /* Initialize the pseudo register data. */
ae66a8f1 10610 int num_pseudos = 0;
ecbf5d4f 10611 if (tdep->have_s_pseudos)
58d6951d 10612 {
ecbf5d4f
LM
10613 /* VFP single precision pseudo registers (S0~S31). */
10614 tdep->s_pseudo_base = register_count;
10615 tdep->s_pseudo_count = 32;
ae66a8f1 10616 num_pseudos += tdep->s_pseudo_count;
ecbf5d4f
LM
10617
10618 if (tdep->have_q_pseudos)
10619 {
10620 /* NEON quad precision pseudo registers (Q0~Q15). */
10621 tdep->q_pseudo_base = register_count + num_pseudos;
ae66a8f1
SP
10622
10623 if (have_neon)
10624 tdep->q_pseudo_count = 16;
10625 else if (have_mve)
10626 tdep->q_pseudo_count = ARM_MVE_NUM_Q_REGS;
10627
ecbf5d4f
LM
10628 num_pseudos += tdep->q_pseudo_count;
10629 }
ae66a8f1
SP
10630 }
10631
10632 /* Do we have any MVE pseudo registers? */
10633 if (have_mve)
10634 {
10635 tdep->mve_pseudo_base = register_count + num_pseudos;
10636 tdep->mve_pseudo_count = 1;
10637 num_pseudos += tdep->mve_pseudo_count;
10638 }
58d6951d 10639
a01567f4
LM
10640 /* Do we have any ARMv8.1-m PACBTI pseudo registers. */
10641 if (have_pacbti)
10642 {
10643 tdep->pacbti_pseudo_base = register_count + num_pseudos;
10644 tdep->pacbti_pseudo_count = 1;
10645 num_pseudos += tdep->pacbti_pseudo_count;
10646 }
10647
ae66a8f1 10648 /* Set some pseudo register hooks, if we have pseudo registers. */
a01567f4 10649 if (tdep->have_s_pseudos || have_mve || have_pacbti)
ae66a8f1 10650 {
58d6951d
DJ
10651 set_gdbarch_num_pseudo_regs (gdbarch, num_pseudos);
10652 set_gdbarch_pseudo_register_read (gdbarch, arm_pseudo_read);
10653 set_gdbarch_pseudo_register_write (gdbarch, arm_pseudo_write);
10654 }
10655
123dc839 10656 /* Add standard register aliases. We add aliases even for those
85102364 10657 names which are used by the current architecture - it's simpler,
123dc839
DJ
10658 and does no harm, since nothing ever lists user registers. */
10659 for (i = 0; i < ARRAY_SIZE (arm_register_aliases); i++)
10660 user_reg_add (gdbarch, arm_register_aliases[i].name,
10661 value_of_arm_user_reg, &arm_register_aliases[i].regnum);
10662
65b48a81
PB
10663 set_gdbarch_disassembler_options (gdbarch, &arm_disassembler_options);
10664 set_gdbarch_valid_disassembler_options (gdbarch, disassembler_options_arm ());
10665
3426ae57
AH
10666 set_gdbarch_gnu_triplet_regexp (gdbarch, arm_gnu_triplet_regexp);
10667
39bbf761
RE
10668 return gdbarch;
10669}
10670
97e03143 10671static void
2af46ca0 10672arm_dump_tdep (struct gdbarch *gdbarch, struct ui_file *file)
97e03143 10673{
08106042 10674 arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
97e03143
RE
10675
10676 if (tdep == NULL)
10677 return;
10678
6cb06a8c
TT
10679 gdb_printf (file, _("arm_dump_tdep: fp_model = %i\n"),
10680 (int) tdep->fp_model);
10681 gdb_printf (file, _("arm_dump_tdep: have_fpa_registers = %i\n"),
10682 (int) tdep->have_fpa_registers);
10683 gdb_printf (file, _("arm_dump_tdep: have_wmmx_registers = %i\n"),
10684 (int) tdep->have_wmmx_registers);
10685 gdb_printf (file, _("arm_dump_tdep: vfp_register_count = %i\n"),
10686 (int) tdep->vfp_register_count);
10687 gdb_printf (file, _("arm_dump_tdep: have_s_pseudos = %s\n"),
10688 tdep->have_s_pseudos? "true" : "false");
10689 gdb_printf (file, _("arm_dump_tdep: s_pseudo_base = %i\n"),
10690 (int) tdep->s_pseudo_base);
10691 gdb_printf (file, _("arm_dump_tdep: s_pseudo_count = %i\n"),
10692 (int) tdep->s_pseudo_count);
10693 gdb_printf (file, _("arm_dump_tdep: have_q_pseudos = %s\n"),
10694 tdep->have_q_pseudos? "true" : "false");
10695 gdb_printf (file, _("arm_dump_tdep: q_pseudo_base = %i\n"),
10696 (int) tdep->q_pseudo_base);
10697 gdb_printf (file, _("arm_dump_tdep: q_pseudo_count = %i\n"),
10698 (int) tdep->q_pseudo_count);
10699 gdb_printf (file, _("arm_dump_tdep: have_neon = %i\n"),
10700 (int) tdep->have_neon);
10701 gdb_printf (file, _("arm_dump_tdep: have_mve = %s\n"),
10702 tdep->have_mve? "yes" : "no");
10703 gdb_printf (file, _("arm_dump_tdep: mve_vpr_regnum = %i\n"),
10704 tdep->mve_vpr_regnum);
10705 gdb_printf (file, _("arm_dump_tdep: mve_pseudo_base = %i\n"),
10706 tdep->mve_pseudo_base);
10707 gdb_printf (file, _("arm_dump_tdep: mve_pseudo_count = %i\n"),
10708 tdep->mve_pseudo_count);
9074667a
CL
10709 gdb_printf (file, _("arm_dump_tdep: m_profile_msp_regnum = %i\n"),
10710 tdep->m_profile_msp_regnum);
10711 gdb_printf (file, _("arm_dump_tdep: m_profile_psp_regnum = %i\n"),
10712 tdep->m_profile_psp_regnum);
ae7e2f45
CL
10713 gdb_printf (file, _("arm_dump_tdep: m_profile_msp_ns_regnum = %i\n"),
10714 tdep->m_profile_msp_ns_regnum);
10715 gdb_printf (file, _("arm_dump_tdep: m_profile_psp_ns_regnum = %i\n"),
10716 tdep->m_profile_psp_ns_regnum);
10717 gdb_printf (file, _("arm_dump_tdep: m_profile_msp_s_regnum = %i\n"),
10718 tdep->m_profile_msp_s_regnum);
10719 gdb_printf (file, _("arm_dump_tdep: m_profile_psp_s_regnum = %i\n"),
10720 tdep->m_profile_psp_s_regnum);
6cb06a8c
TT
10721 gdb_printf (file, _("arm_dump_tdep: Lowest pc = 0x%lx\n"),
10722 (unsigned long) tdep->lowest_pc);
a01567f4
LM
10723 gdb_printf (file, _("arm_dump_tdep: have_pacbti = %s\n"),
10724 tdep->have_pacbti? "yes" : "no");
10725 gdb_printf (file, _("arm_dump_tdep: pacbti_pseudo_base = %i\n"),
10726 tdep->pacbti_pseudo_base);
10727 gdb_printf (file, _("arm_dump_tdep: pacbti_pseudo_count = %i\n"),
10728 tdep->pacbti_pseudo_count);
10729 gdb_printf (file, _("arm_dump_tdep: is_m = %s\n"),
10730 tdep->is_m? "yes" : "no");
97e03143
RE
10731}
10732
0d4c07af 10733#if GDB_SELF_TEST
b121eeb9
YQ
10734namespace selftests
10735{
10736static void arm_record_test (void);
9ecab40c 10737static void arm_analyze_prologue_test ();
b121eeb9 10738}
0d4c07af 10739#endif
b121eeb9 10740
6c265988 10741void _initialize_arm_tdep ();
c906108c 10742void
6c265988 10743_initialize_arm_tdep ()
c906108c 10744{
bc90b915 10745 long length;
65b48a81 10746 int i, j;
edefbb7c
AC
10747 char regdesc[1024], *rdptr = regdesc;
10748 size_t rest = sizeof (regdesc);
085dd6e6 10749
42cf1509 10750 gdbarch_register (bfd_arch_arm, arm_gdbarch_init, arm_dump_tdep);
97e03143 10751
0e9e9abd 10752 /* Add ourselves to objfile event chain. */
c90e7d63 10753 gdb::observers::new_objfile.attach (arm_exidx_new_objfile, "arm-tdep");
0e9e9abd 10754
70f80edf
JT
10755 /* Register an ELF OS ABI sniffer for ARM binaries. */
10756 gdbarch_register_osabi_sniffer (bfd_arch_arm,
10757 bfd_target_elf_flavour,
10758 arm_elf_osabi_sniffer);
10759
afd7eef0 10760 /* Add root prefix command for all "set arm"/"show arm" commands. */
f54bdb6d
SM
10761 add_setshow_prefix_cmd ("arm", no_class,
10762 _("Various ARM-specific commands."),
10763 _("Various ARM-specific commands."),
10764 &setarmcmdlist, &showarmcmdlist,
10765 &setlist, &showlist);
c5aa993b 10766
65b48a81 10767 arm_disassembler_options = xstrdup ("reg-names-std");
471b9d15
MR
10768 const disasm_options_t *disasm_options
10769 = &disassembler_options_arm ()->options;
65b48a81
PB
10770 int num_disassembly_styles = 0;
10771 for (i = 0; disasm_options->name[i] != NULL; i++)
08dedd66 10772 if (startswith (disasm_options->name[i], "reg-names-"))
65b48a81
PB
10773 num_disassembly_styles++;
10774
10775 /* Initialize the array that will be passed to add_setshow_enum_cmd(). */
8d749320 10776 valid_disassembly_styles = XNEWVEC (const char *,
65b48a81
PB
10777 num_disassembly_styles + 1);
10778 for (i = j = 0; disasm_options->name[i] != NULL; i++)
08dedd66 10779 if (startswith (disasm_options->name[i], "reg-names-"))
65b48a81
PB
10780 {
10781 size_t offset = strlen ("reg-names-");
10782 const char *style = disasm_options->name[i];
10783 valid_disassembly_styles[j++] = &style[offset];
dedb7102
TT
10784 if (strcmp (&style[offset], "std") == 0)
10785 disassembly_style = &style[offset];
65b48a81
PB
10786 length = snprintf (rdptr, rest, "%s - %s\n", &style[offset],
10787 disasm_options->description[i]);
10788 rdptr += length;
10789 rest -= length;
10790 }
94c30b78 10791 /* Mark the end of valid options. */
65b48a81 10792 valid_disassembly_styles[num_disassembly_styles] = NULL;
c906108c 10793
edefbb7c 10794 /* Create the help text. */
d7e74731
PA
10795 std::string helptext = string_printf ("%s%s%s",
10796 _("The valid values are:\n"),
10797 regdesc,
10798 _("The default is \"std\"."));
ed9a39eb 10799
edefbb7c
AC
10800 add_setshow_enum_cmd("disassembler", no_class,
10801 valid_disassembly_styles, &disassembly_style,
10802 _("Set the disassembly style."),
10803 _("Show the disassembly style."),
09b0e4b0 10804 helptext.c_str (),
2c5b56ce 10805 set_disassembly_style_sfunc,
65b48a81 10806 show_disassembly_style_sfunc,
7376b4c2 10807 &setarmcmdlist, &showarmcmdlist);
edefbb7c
AC
10808
10809 add_setshow_boolean_cmd ("apcs32", no_class, &arm_apcs_32,
10810 _("Set usage of ARM 32-bit mode."),
10811 _("Show usage of ARM 32-bit mode."),
10812 _("When off, a 26-bit PC will be used."),
2c5b56ce 10813 NULL,
0963b4bd
MS
10814 NULL, /* FIXME: i18n: Usage of ARM 32-bit
10815 mode is %s. */
26304000 10816 &setarmcmdlist, &showarmcmdlist);
c906108c 10817
fd50bc42 10818 /* Add a command to allow the user to force the FPU model. */
edefbb7c
AC
10819 add_setshow_enum_cmd ("fpu", no_class, fp_model_strings, &current_fp_model,
10820 _("Set the floating point type."),
10821 _("Show the floating point type."),
10822 _("auto - Determine the FP typefrom the OS-ABI.\n\
10823softfpa - Software FP, mixed-endian doubles on little-endian ARMs.\n\
10824fpa - FPA co-processor (GCC compiled).\n\
10825softvfp - Software FP with pure-endian doubles.\n\
10826vfp - VFP co-processor."),
edefbb7c 10827 set_fp_model_sfunc, show_fp_model,
7376b4c2 10828 &setarmcmdlist, &showarmcmdlist);
fd50bc42 10829
28e97307
DJ
10830 /* Add a command to allow the user to force the ABI. */
10831 add_setshow_enum_cmd ("abi", class_support, arm_abi_strings, &arm_abi_string,
10832 _("Set the ABI."),
10833 _("Show the ABI."),
10834 NULL, arm_set_abi, arm_show_abi,
10835 &setarmcmdlist, &showarmcmdlist);
10836
0428b8f5
DJ
10837 /* Add two commands to allow the user to force the assumed
10838 execution mode. */
10839 add_setshow_enum_cmd ("fallback-mode", class_support,
10840 arm_mode_strings, &arm_fallback_mode_string,
10841 _("Set the mode assumed when symbols are unavailable."),
10842 _("Show the mode assumed when symbols are unavailable."),
10843 NULL, NULL, arm_show_fallback_mode,
10844 &setarmcmdlist, &showarmcmdlist);
10845 add_setshow_enum_cmd ("force-mode", class_support,
10846 arm_mode_strings, &arm_force_mode_string,
10847 _("Set the mode assumed even when symbols are available."),
10848 _("Show the mode assumed even when symbols are available."),
10849 NULL, NULL, arm_show_force_mode,
10850 &setarmcmdlist, &showarmcmdlist);
10851
ef273377
CL
10852 /* Add a command to stop triggering security exceptions when
10853 unwinding exception stacks. */
10854 add_setshow_boolean_cmd ("unwind-secure-frames", no_class, &arm_unwind_secure_frames,
10855 _("Set usage of non-secure to secure exception stack unwinding."),
10856 _("Show usage of non-secure to secure exception stack unwinding."),
10857 _("When on, the debugger can trigger memory access traps."),
10858 NULL, arm_show_unwind_secure_frames,
10859 &setarmcmdlist, &showarmcmdlist);
10860
6529d2dd 10861 /* Debugging flag. */
edefbb7c
AC
10862 add_setshow_boolean_cmd ("arm", class_maintenance, &arm_debug,
10863 _("Set ARM debugging."),
10864 _("Show ARM debugging."),
10865 _("When on, arm-specific debugging is enabled."),
2c5b56ce 10866 NULL,
7915a72c 10867 NULL, /* FIXME: i18n: "ARM debugging is %s. */
26304000 10868 &setdebuglist, &showdebuglist);
b121eeb9
YQ
10869
10870#if GDB_SELF_TEST
1526853e 10871 selftests::register_test ("arm-record", selftests::arm_record_test);
9ecab40c 10872 selftests::register_test ("arm_analyze_prologue", selftests::arm_analyze_prologue_test);
b121eeb9
YQ
10873#endif
10874
c906108c 10875}
72508ac0
PO
10876
10877/* ARM-reversible process record data structures. */
10878
10879#define ARM_INSN_SIZE_BYTES 4
10880#define THUMB_INSN_SIZE_BYTES 2
10881#define THUMB2_INSN_SIZE_BYTES 4
10882
10883
71e396f9
LM
10884/* Position of the bit within a 32-bit ARM instruction
10885 that defines whether the instruction is a load or store. */
72508ac0
PO
10886#define INSN_S_L_BIT_NUM 20
10887
10888#define REG_ALLOC(REGS, LENGTH, RECORD_BUF) \
dda83cd7
SM
10889 do \
10890 { \
10891 unsigned int reg_len = LENGTH; \
10892 if (reg_len) \
10893 { \
10894 REGS = XNEWVEC (uint32_t, reg_len); \
10895 memcpy(&REGS[0], &RECORD_BUF[0], sizeof(uint32_t)*LENGTH); \
10896 } \
10897 } \
10898 while (0)
72508ac0
PO
10899
10900#define MEM_ALLOC(MEMS, LENGTH, RECORD_BUF) \
dda83cd7
SM
10901 do \
10902 { \
10903 unsigned int mem_len = LENGTH; \
10904 if (mem_len) \
01add95b
SM
10905 { \
10906 MEMS = XNEWVEC (struct arm_mem_r, mem_len); \
10907 memcpy(&MEMS->len, &RECORD_BUF[0], \
10908 sizeof(struct arm_mem_r) * LENGTH); \
10909 } \
dda83cd7
SM
10910 } \
10911 while (0)
72508ac0
PO
10912
10913/* Checks whether insn is already recorded or yet to be decoded. (boolean expression). */
10914#define INSN_RECORDED(ARM_RECORD) \
dda83cd7 10915 (0 != (ARM_RECORD)->reg_rec_count || 0 != (ARM_RECORD)->mem_rec_count)
72508ac0
PO
10916
10917/* ARM memory record structure. */
10918struct arm_mem_r
10919{
10920 uint32_t len; /* Record length. */
bfbbec00 10921 uint32_t addr; /* Memory address. */
72508ac0
PO
10922};
10923
10924/* ARM instruction record contains opcode of current insn
10925 and execution state (before entry to decode_insn()),
10926 contains list of to-be-modified registers and
10927 memory blocks (on return from decode_insn()). */
10928
4748a9be 10929struct arm_insn_decode_record
72508ac0
PO
10930{
10931 struct gdbarch *gdbarch;
10932 struct regcache *regcache;
10933 CORE_ADDR this_addr; /* Address of the insn being decoded. */
10934 uint32_t arm_insn; /* Should accommodate thumb. */
10935 uint32_t cond; /* Condition code. */
10936 uint32_t opcode; /* Insn opcode. */
10937 uint32_t decode; /* Insn decode bits. */
10938 uint32_t mem_rec_count; /* No of mem records. */
10939 uint32_t reg_rec_count; /* No of reg records. */
10940 uint32_t *arm_regs; /* Registers to be saved for this record. */
10941 struct arm_mem_r *arm_mems; /* Memory to be saved for this record. */
4748a9be 10942};
72508ac0
PO
10943
10944
10945/* Checks ARM SBZ and SBO mandatory fields. */
10946
10947static int
10948sbo_sbz (uint32_t insn, uint32_t bit_num, uint32_t len, uint32_t sbo)
10949{
10950 uint32_t ones = bits (insn, bit_num - 1, (bit_num -1) + (len - 1));
10951
10952 if (!len)
10953 return 1;
10954
10955 if (!sbo)
10956 ones = ~ones;
10957
10958 while (ones)
10959 {
10960 if (!(ones & sbo))
dda83cd7
SM
10961 {
10962 return 0;
10963 }
72508ac0
PO
10964 ones = ones >> 1;
10965 }
10966 return 1;
10967}
10968
c6ec2b30
OJ
10969enum arm_record_result
10970{
10971 ARM_RECORD_SUCCESS = 0,
10972 ARM_RECORD_FAILURE = 1
10973};
10974
0d1703b8 10975enum arm_record_strx_t
72508ac0
PO
10976{
10977 ARM_RECORD_STRH=1,
10978 ARM_RECORD_STRD
0d1703b8 10979};
72508ac0 10980
0d1703b8 10981enum record_type_t
72508ac0
PO
10982{
10983 ARM_RECORD=1,
10984 THUMB_RECORD,
10985 THUMB2_RECORD
0d1703b8 10986};
72508ac0
PO
10987
10988
10989static int
4748a9be 10990arm_record_strx (arm_insn_decode_record *arm_insn_r, uint32_t *record_buf,
dda83cd7 10991 uint32_t *record_buf_mem, arm_record_strx_t str_type)
72508ac0
PO
10992{
10993
10994 struct regcache *reg_cache = arm_insn_r->regcache;
10995 ULONGEST u_regval[2]= {0};
10996
10997 uint32_t reg_src1 = 0, reg_src2 = 0;
10998 uint32_t immed_high = 0, immed_low = 0,offset_8 = 0, tgt_mem_addr = 0;
72508ac0
PO
10999
11000 arm_insn_r->opcode = bits (arm_insn_r->arm_insn, 21, 24);
11001 arm_insn_r->decode = bits (arm_insn_r->arm_insn, 4, 7);
72508ac0
PO
11002
11003 if (14 == arm_insn_r->opcode || 10 == arm_insn_r->opcode)
11004 {
11005 /* 1) Handle misc store, immediate offset. */
11006 immed_low = bits (arm_insn_r->arm_insn, 0, 3);
11007 immed_high = bits (arm_insn_r->arm_insn, 8, 11);
11008 reg_src1 = bits (arm_insn_r->arm_insn, 16, 19);
11009 regcache_raw_read_unsigned (reg_cache, reg_src1,
dda83cd7 11010 &u_regval[0]);
72508ac0 11011 if (ARM_PC_REGNUM == reg_src1)
dda83cd7
SM
11012 {
11013 /* If R15 was used as Rn, hence current PC+8. */
11014 u_regval[0] = u_regval[0] + 8;
11015 }
72508ac0
PO
11016 offset_8 = (immed_high << 4) | immed_low;
11017 /* Calculate target store address. */
11018 if (14 == arm_insn_r->opcode)
dda83cd7
SM
11019 {
11020 tgt_mem_addr = u_regval[0] + offset_8;
11021 }
72508ac0 11022 else
dda83cd7
SM
11023 {
11024 tgt_mem_addr = u_regval[0] - offset_8;
11025 }
72508ac0 11026 if (ARM_RECORD_STRH == str_type)
dda83cd7
SM
11027 {
11028 record_buf_mem[0] = 2;
11029 record_buf_mem[1] = tgt_mem_addr;
11030 arm_insn_r->mem_rec_count = 1;
11031 }
72508ac0 11032 else if (ARM_RECORD_STRD == str_type)
dda83cd7
SM
11033 {
11034 record_buf_mem[0] = 4;
11035 record_buf_mem[1] = tgt_mem_addr;
11036 record_buf_mem[2] = 4;
11037 record_buf_mem[3] = tgt_mem_addr + 4;
11038 arm_insn_r->mem_rec_count = 2;
11039 }
72508ac0
PO
11040 }
11041 else if (12 == arm_insn_r->opcode || 8 == arm_insn_r->opcode)
11042 {
11043 /* 2) Store, register offset. */
11044 /* Get Rm. */
11045 reg_src1 = bits (arm_insn_r->arm_insn, 0, 3);
11046 /* Get Rn. */
11047 reg_src2 = bits (arm_insn_r->arm_insn, 16, 19);
11048 regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval[0]);
11049 regcache_raw_read_unsigned (reg_cache, reg_src2, &u_regval[1]);
11050 if (15 == reg_src2)
dda83cd7
SM
11051 {
11052 /* If R15 was used as Rn, hence current PC+8. */
11053 u_regval[0] = u_regval[0] + 8;
11054 }
72508ac0
PO
11055 /* Calculate target store address, Rn +/- Rm, register offset. */
11056 if (12 == arm_insn_r->opcode)
dda83cd7
SM
11057 {
11058 tgt_mem_addr = u_regval[0] + u_regval[1];
11059 }
72508ac0 11060 else
dda83cd7
SM
11061 {
11062 tgt_mem_addr = u_regval[1] - u_regval[0];
11063 }
72508ac0 11064 if (ARM_RECORD_STRH == str_type)
dda83cd7
SM
11065 {
11066 record_buf_mem[0] = 2;
11067 record_buf_mem[1] = tgt_mem_addr;
11068 arm_insn_r->mem_rec_count = 1;
11069 }
72508ac0 11070 else if (ARM_RECORD_STRD == str_type)
dda83cd7
SM
11071 {
11072 record_buf_mem[0] = 4;
11073 record_buf_mem[1] = tgt_mem_addr;
11074 record_buf_mem[2] = 4;
11075 record_buf_mem[3] = tgt_mem_addr + 4;
11076 arm_insn_r->mem_rec_count = 2;
11077 }
72508ac0
PO
11078 }
11079 else if (11 == arm_insn_r->opcode || 15 == arm_insn_r->opcode
dda83cd7 11080 || 2 == arm_insn_r->opcode || 6 == arm_insn_r->opcode)
72508ac0
PO
11081 {
11082 /* 3) Store, immediate pre-indexed. */
11083 /* 5) Store, immediate post-indexed. */
11084 immed_low = bits (arm_insn_r->arm_insn, 0, 3);
11085 immed_high = bits (arm_insn_r->arm_insn, 8, 11);
11086 offset_8 = (immed_high << 4) | immed_low;
11087 reg_src1 = bits (arm_insn_r->arm_insn, 16, 19);
11088 regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval[0]);
11089 /* Calculate target store address, Rn +/- Rm, register offset. */
11090 if (15 == arm_insn_r->opcode || 6 == arm_insn_r->opcode)
dda83cd7
SM
11091 {
11092 tgt_mem_addr = u_regval[0] + offset_8;
11093 }
72508ac0 11094 else
dda83cd7
SM
11095 {
11096 tgt_mem_addr = u_regval[0] - offset_8;
11097 }
72508ac0 11098 if (ARM_RECORD_STRH == str_type)
dda83cd7
SM
11099 {
11100 record_buf_mem[0] = 2;
11101 record_buf_mem[1] = tgt_mem_addr;
11102 arm_insn_r->mem_rec_count = 1;
11103 }
72508ac0 11104 else if (ARM_RECORD_STRD == str_type)
dda83cd7
SM
11105 {
11106 record_buf_mem[0] = 4;
11107 record_buf_mem[1] = tgt_mem_addr;
11108 record_buf_mem[2] = 4;
11109 record_buf_mem[3] = tgt_mem_addr + 4;
11110 arm_insn_r->mem_rec_count = 2;
11111 }
72508ac0
PO
11112 /* Record Rn also as it changes. */
11113 *(record_buf) = bits (arm_insn_r->arm_insn, 16, 19);
11114 arm_insn_r->reg_rec_count = 1;
11115 }
11116 else if (9 == arm_insn_r->opcode || 13 == arm_insn_r->opcode
dda83cd7 11117 || 0 == arm_insn_r->opcode || 4 == arm_insn_r->opcode)
72508ac0
PO
11118 {
11119 /* 4) Store, register pre-indexed. */
11120 /* 6) Store, register post -indexed. */
11121 reg_src1 = bits (arm_insn_r->arm_insn, 0, 3);
11122 reg_src2 = bits (arm_insn_r->arm_insn, 16, 19);
11123 regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval[0]);
11124 regcache_raw_read_unsigned (reg_cache, reg_src2, &u_regval[1]);
11125 /* Calculate target store address, Rn +/- Rm, register offset. */
11126 if (13 == arm_insn_r->opcode || 4 == arm_insn_r->opcode)
dda83cd7
SM
11127 {
11128 tgt_mem_addr = u_regval[0] + u_regval[1];
11129 }
72508ac0 11130 else
dda83cd7
SM
11131 {
11132 tgt_mem_addr = u_regval[1] - u_regval[0];
11133 }
72508ac0 11134 if (ARM_RECORD_STRH == str_type)
dda83cd7
SM
11135 {
11136 record_buf_mem[0] = 2;
11137 record_buf_mem[1] = tgt_mem_addr;
11138 arm_insn_r->mem_rec_count = 1;
11139 }
72508ac0 11140 else if (ARM_RECORD_STRD == str_type)
dda83cd7
SM
11141 {
11142 record_buf_mem[0] = 4;
11143 record_buf_mem[1] = tgt_mem_addr;
11144 record_buf_mem[2] = 4;
11145 record_buf_mem[3] = tgt_mem_addr + 4;
11146 arm_insn_r->mem_rec_count = 2;
11147 }
72508ac0
PO
11148 /* Record Rn also as it changes. */
11149 *(record_buf) = bits (arm_insn_r->arm_insn, 16, 19);
11150 arm_insn_r->reg_rec_count = 1;
11151 }
11152 return 0;
11153}
11154
11155/* Handling ARM extension space insns. */
11156
11157static int
4748a9be 11158arm_record_extension_space (arm_insn_decode_record *arm_insn_r)
72508ac0 11159{
df95a9cf 11160 int ret = 0; /* Return value: -1:record failure ; 0:success */
72508ac0
PO
11161 uint32_t opcode1 = 0, opcode2 = 0, insn_op1 = 0;
11162 uint32_t record_buf[8], record_buf_mem[8];
11163 uint32_t reg_src1 = 0;
72508ac0
PO
11164 struct regcache *reg_cache = arm_insn_r->regcache;
11165 ULONGEST u_regval = 0;
11166
11167 gdb_assert (!INSN_RECORDED(arm_insn_r));
11168 /* Handle unconditional insn extension space. */
11169
11170 opcode1 = bits (arm_insn_r->arm_insn, 20, 27);
11171 opcode2 = bits (arm_insn_r->arm_insn, 4, 7);
11172 if (arm_insn_r->cond)
11173 {
11174 /* PLD has no affect on architectural state, it just affects
dda83cd7 11175 the caches. */
72508ac0 11176 if (5 == ((opcode1 & 0xE0) >> 5))
dda83cd7
SM
11177 {
11178 /* BLX(1) */
11179 record_buf[0] = ARM_PS_REGNUM;
11180 record_buf[1] = ARM_LR_REGNUM;
11181 arm_insn_r->reg_rec_count = 2;
11182 }
72508ac0
PO
11183 /* STC2, LDC2, MCR2, MRC2, CDP2: <TBD>, co-processor insn. */
11184 }
11185
11186
11187 opcode1 = bits (arm_insn_r->arm_insn, 25, 27);
11188 if (3 == opcode1 && bit (arm_insn_r->arm_insn, 4))
11189 {
11190 ret = -1;
11191 /* Undefined instruction on ARM V5; need to handle if later
dda83cd7 11192 versions define it. */
72508ac0
PO
11193 }
11194
11195 opcode1 = bits (arm_insn_r->arm_insn, 24, 27);
11196 opcode2 = bits (arm_insn_r->arm_insn, 4, 7);
11197 insn_op1 = bits (arm_insn_r->arm_insn, 20, 23);
11198
11199 /* Handle arithmetic insn extension space. */
11200 if (!opcode1 && 9 == opcode2 && 1 != arm_insn_r->cond
11201 && !INSN_RECORDED(arm_insn_r))
11202 {
11203 /* Handle MLA(S) and MUL(S). */
b020ff80 11204 if (in_inclusive_range (insn_op1, 0U, 3U))
01add95b
SM
11205 {
11206 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11207 record_buf[1] = ARM_PS_REGNUM;
11208 arm_insn_r->reg_rec_count = 2;
11209 }
b020ff80 11210 else if (in_inclusive_range (insn_op1, 4U, 15U))
01add95b
SM
11211 {
11212 /* Handle SMLAL(S), SMULL(S), UMLAL(S), UMULL(S). */
11213 record_buf[0] = bits (arm_insn_r->arm_insn, 16, 19);
11214 record_buf[1] = bits (arm_insn_r->arm_insn, 12, 15);
11215 record_buf[2] = ARM_PS_REGNUM;
11216 arm_insn_r->reg_rec_count = 3;
11217 }
72508ac0
PO
11218 }
11219
11220 opcode1 = bits (arm_insn_r->arm_insn, 26, 27);
11221 opcode2 = bits (arm_insn_r->arm_insn, 23, 24);
11222 insn_op1 = bits (arm_insn_r->arm_insn, 21, 22);
11223
11224 /* Handle control insn extension space. */
11225
11226 if (!opcode1 && 2 == opcode2 && !bit (arm_insn_r->arm_insn, 20)
11227 && 1 != arm_insn_r->cond && !INSN_RECORDED(arm_insn_r))
11228 {
11229 if (!bit (arm_insn_r->arm_insn,25))
dda83cd7
SM
11230 {
11231 if (!bits (arm_insn_r->arm_insn, 4, 7))
11232 {
11233 if ((0 == insn_op1) || (2 == insn_op1))
11234 {
11235 /* MRS. */
11236 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11237 arm_insn_r->reg_rec_count = 1;
11238 }
11239 else if (1 == insn_op1)
11240 {
11241 /* CSPR is going to be changed. */
11242 record_buf[0] = ARM_PS_REGNUM;
11243 arm_insn_r->reg_rec_count = 1;
11244 }
11245 else if (3 == insn_op1)
11246 {
11247 /* SPSR is going to be changed. */
11248 /* We need to get SPSR value, which is yet to be done. */
11249 return -1;
11250 }
11251 }
11252 else if (1 == bits (arm_insn_r->arm_insn, 4, 7))
11253 {
11254 if (1 == insn_op1)
11255 {
11256 /* BX. */
11257 record_buf[0] = ARM_PS_REGNUM;
11258 arm_insn_r->reg_rec_count = 1;
11259 }
11260 else if (3 == insn_op1)
11261 {
11262 /* CLZ. */
11263 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11264 arm_insn_r->reg_rec_count = 1;
11265 }
11266 }
11267 else if (3 == bits (arm_insn_r->arm_insn, 4, 7))
11268 {
11269 /* BLX. */
11270 record_buf[0] = ARM_PS_REGNUM;
11271 record_buf[1] = ARM_LR_REGNUM;
11272 arm_insn_r->reg_rec_count = 2;
11273 }
11274 else if (5 == bits (arm_insn_r->arm_insn, 4, 7))
11275 {
11276 /* QADD, QSUB, QDADD, QDSUB */
11277 record_buf[0] = ARM_PS_REGNUM;
11278 record_buf[1] = bits (arm_insn_r->arm_insn, 12, 15);
11279 arm_insn_r->reg_rec_count = 2;
11280 }
11281 else if (7 == bits (arm_insn_r->arm_insn, 4, 7))
11282 {
11283 /* BKPT. */
11284 record_buf[0] = ARM_PS_REGNUM;
11285 record_buf[1] = ARM_LR_REGNUM;
11286 arm_insn_r->reg_rec_count = 2;
11287
11288 /* Save SPSR also;how? */
11289 return -1;
11290 }
11291 else if(8 == bits (arm_insn_r->arm_insn, 4, 7)
11292 || 10 == bits (arm_insn_r->arm_insn, 4, 7)
11293 || 12 == bits (arm_insn_r->arm_insn, 4, 7)
11294 || 14 == bits (arm_insn_r->arm_insn, 4, 7)
11295 )
11296 {
11297 if (0 == insn_op1 || 1 == insn_op1)
11298 {
11299 /* SMLA<x><y>, SMLAW<y>, SMULW<y>. */
11300 /* We dont do optimization for SMULW<y> where we
11301 need only Rd. */
11302 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11303 record_buf[1] = ARM_PS_REGNUM;
11304 arm_insn_r->reg_rec_count = 2;
11305 }
11306 else if (2 == insn_op1)
11307 {
11308 /* SMLAL<x><y>. */
11309 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11310 record_buf[1] = bits (arm_insn_r->arm_insn, 16, 19);
11311 arm_insn_r->reg_rec_count = 2;
11312 }
11313 else if (3 == insn_op1)
11314 {
11315 /* SMUL<x><y>. */
11316 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11317 arm_insn_r->reg_rec_count = 1;
11318 }
11319 }
11320 }
72508ac0 11321 else
dda83cd7
SM
11322 {
11323 /* MSR : immediate form. */
11324 if (1 == insn_op1)
11325 {
11326 /* CSPR is going to be changed. */
11327 record_buf[0] = ARM_PS_REGNUM;
11328 arm_insn_r->reg_rec_count = 1;
11329 }
11330 else if (3 == insn_op1)
11331 {
11332 /* SPSR is going to be changed. */
11333 /* we need to get SPSR value, which is yet to be done */
11334 return -1;
11335 }
11336 }
72508ac0
PO
11337 }
11338
11339 opcode1 = bits (arm_insn_r->arm_insn, 25, 27);
11340 opcode2 = bits (arm_insn_r->arm_insn, 20, 24);
11341 insn_op1 = bits (arm_insn_r->arm_insn, 5, 6);
11342
11343 /* Handle load/store insn extension space. */
11344
11345 if (!opcode1 && bit (arm_insn_r->arm_insn, 7)
11346 && bit (arm_insn_r->arm_insn, 4) && 1 != arm_insn_r->cond
11347 && !INSN_RECORDED(arm_insn_r))
11348 {
11349 /* SWP/SWPB. */
11350 if (0 == insn_op1)
dda83cd7
SM
11351 {
11352 /* These insn, changes register and memory as well. */
11353 /* SWP or SWPB insn. */
11354 /* Get memory address given by Rn. */
11355 reg_src1 = bits (arm_insn_r->arm_insn, 16, 19);
11356 regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval);
11357 /* SWP insn ?, swaps word. */
11358 if (8 == arm_insn_r->opcode)
11359 {
11360 record_buf_mem[0] = 4;
11361 }
11362 else
11363 {
11364 /* SWPB insn, swaps only byte. */
11365 record_buf_mem[0] = 1;
11366 }
11367 record_buf_mem[1] = u_regval;
11368 arm_insn_r->mem_rec_count = 1;
11369 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11370 arm_insn_r->reg_rec_count = 1;
11371 }
72508ac0 11372 else if (1 == insn_op1 && !bit (arm_insn_r->arm_insn, 20))
dda83cd7
SM
11373 {
11374 /* STRH. */
11375 arm_record_strx(arm_insn_r, &record_buf[0], &record_buf_mem[0],
11376 ARM_RECORD_STRH);
11377 }
72508ac0 11378 else if (2 == insn_op1 && !bit (arm_insn_r->arm_insn, 20))
dda83cd7
SM
11379 {
11380 /* LDRD. */
11381 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11382 record_buf[1] = record_buf[0] + 1;
11383 arm_insn_r->reg_rec_count = 2;
11384 }
72508ac0 11385 else if (3 == insn_op1 && !bit (arm_insn_r->arm_insn, 20))
dda83cd7
SM
11386 {
11387 /* STRD. */
11388 arm_record_strx(arm_insn_r, &record_buf[0], &record_buf_mem[0],
11389 ARM_RECORD_STRD);
11390 }
72508ac0 11391 else if (bit (arm_insn_r->arm_insn, 20) && insn_op1 <= 3)
dda83cd7
SM
11392 {
11393 /* LDRH, LDRSB, LDRSH. */
11394 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11395 arm_insn_r->reg_rec_count = 1;
11396 }
72508ac0
PO
11397
11398 }
11399
11400 opcode1 = bits (arm_insn_r->arm_insn, 23, 27);
11401 if (24 == opcode1 && bit (arm_insn_r->arm_insn, 21)
11402 && !INSN_RECORDED(arm_insn_r))
11403 {
11404 ret = -1;
11405 /* Handle coprocessor insn extension space. */
11406 }
11407
11408 /* To be done for ARMv5 and later; as of now we return -1. */
11409 if (-1 == ret)
ca92db2d 11410 return ret;
72508ac0
PO
11411
11412 REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
11413 MEM_ALLOC (arm_insn_r->arm_mems, arm_insn_r->mem_rec_count, record_buf_mem);
11414
11415 return ret;
11416}
11417
11418/* Handling opcode 000 insns. */
11419
11420static int
4748a9be 11421arm_record_data_proc_misc_ld_str (arm_insn_decode_record *arm_insn_r)
72508ac0
PO
11422{
11423 struct regcache *reg_cache = arm_insn_r->regcache;
11424 uint32_t record_buf[8], record_buf_mem[8];
11425 ULONGEST u_regval[2] = {0};
11426
8d49165d 11427 uint32_t reg_src1 = 0;
72508ac0
PO
11428 uint32_t opcode1 = 0;
11429
11430 arm_insn_r->opcode = bits (arm_insn_r->arm_insn, 21, 24);
11431 arm_insn_r->decode = bits (arm_insn_r->arm_insn, 4, 7);
11432 opcode1 = bits (arm_insn_r->arm_insn, 20, 24);
11433
2d9e6acb 11434 if (!((opcode1 & 0x19) == 0x10))
72508ac0 11435 {
2d9e6acb
YQ
11436 /* Data-processing (register) and Data-processing (register-shifted
11437 register */
11438 /* Out of 11 shifter operands mode, all the insn modifies destination
11439 register, which is specified by 13-16 decode. */
11440 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11441 record_buf[1] = ARM_PS_REGNUM;
11442 arm_insn_r->reg_rec_count = 2;
72508ac0 11443 }
2d9e6acb 11444 else if ((arm_insn_r->decode < 8) && ((opcode1 & 0x19) == 0x10))
72508ac0 11445 {
2d9e6acb
YQ
11446 /* Miscellaneous instructions */
11447
11448 if (3 == arm_insn_r->decode && 0x12 == opcode1
11449 && sbo_sbz (arm_insn_r->arm_insn, 9, 12, 1))
11450 {
11451 /* Handle BLX, branch and link/exchange. */
11452 if (9 == arm_insn_r->opcode)
11453 {
11454 /* Branch is chosen by setting T bit of CSPR, bitp[0] of Rm,
11455 and R14 stores the return address. */
11456 record_buf[0] = ARM_PS_REGNUM;
11457 record_buf[1] = ARM_LR_REGNUM;
11458 arm_insn_r->reg_rec_count = 2;
11459 }
11460 }
11461 else if (7 == arm_insn_r->decode && 0x12 == opcode1)
11462 {
11463 /* Handle enhanced software breakpoint insn, BKPT. */
11464 /* CPSR is changed to be executed in ARM state, disabling normal
11465 interrupts, entering abort mode. */
11466 /* According to high vector configuration PC is set. */
11467 /* user hit breakpoint and type reverse, in
11468 that case, we need to go back with previous CPSR and
11469 Program Counter. */
11470 record_buf[0] = ARM_PS_REGNUM;
11471 record_buf[1] = ARM_LR_REGNUM;
11472 arm_insn_r->reg_rec_count = 2;
11473
11474 /* Save SPSR also; how? */
11475 return -1;
11476 }
11477 else if (1 == arm_insn_r->decode && 0x12 == opcode1
11478 && sbo_sbz (arm_insn_r->arm_insn, 9, 12, 1))
11479 {
11480 /* Handle BX, branch and link/exchange. */
11481 /* Branch is chosen by setting T bit of CSPR, bitp[0] of Rm. */
11482 record_buf[0] = ARM_PS_REGNUM;
11483 arm_insn_r->reg_rec_count = 1;
11484 }
11485 else if (1 == arm_insn_r->decode && 0x16 == opcode1
11486 && sbo_sbz (arm_insn_r->arm_insn, 9, 4, 1)
11487 && sbo_sbz (arm_insn_r->arm_insn, 17, 4, 1))
11488 {
11489 /* Count leading zeros: CLZ. */
11490 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11491 arm_insn_r->reg_rec_count = 1;
11492 }
11493 else if (!bit (arm_insn_r->arm_insn, INSN_S_L_BIT_NUM)
11494 && (8 == arm_insn_r->opcode || 10 == arm_insn_r->opcode)
11495 && sbo_sbz (arm_insn_r->arm_insn, 17, 4, 1)
11496 && sbo_sbz (arm_insn_r->arm_insn, 1, 12, 0))
11497 {
11498 /* Handle MRS insn. */
11499 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11500 arm_insn_r->reg_rec_count = 1;
11501 }
72508ac0 11502 }
2d9e6acb 11503 else if (9 == arm_insn_r->decode && opcode1 < 0x10)
72508ac0 11504 {
2d9e6acb
YQ
11505 /* Multiply and multiply-accumulate */
11506
11507 /* Handle multiply instructions. */
11508 /* MLA, MUL, SMLAL, SMULL, UMLAL, UMULL. */
11509 if (0 == arm_insn_r->opcode || 1 == arm_insn_r->opcode)
f2a883a8
SM
11510 {
11511 /* Handle MLA and MUL. */
11512 record_buf[0] = bits (arm_insn_r->arm_insn, 16, 19);
11513 record_buf[1] = ARM_PS_REGNUM;
11514 arm_insn_r->reg_rec_count = 2;
11515 }
11516 else if (4 <= arm_insn_r->opcode && 7 >= arm_insn_r->opcode)
11517 {
11518 /* Handle SMLAL, SMULL, UMLAL, UMULL. */
11519 record_buf[0] = bits (arm_insn_r->arm_insn, 16, 19);
11520 record_buf[1] = bits (arm_insn_r->arm_insn, 12, 15);
11521 record_buf[2] = ARM_PS_REGNUM;
11522 arm_insn_r->reg_rec_count = 3;
11523 }
2d9e6acb
YQ
11524 }
11525 else if (9 == arm_insn_r->decode && opcode1 > 0x10)
11526 {
11527 /* Synchronization primitives */
11528
72508ac0
PO
11529 /* Handling SWP, SWPB. */
11530 /* These insn, changes register and memory as well. */
11531 /* SWP or SWPB insn. */
11532
11533 reg_src1 = bits (arm_insn_r->arm_insn, 16, 19);
11534 regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval[0]);
11535 /* SWP insn ?, swaps word. */
11536 if (8 == arm_insn_r->opcode)
2d9e6acb
YQ
11537 {
11538 record_buf_mem[0] = 4;
11539 }
11540 else
11541 {
11542 /* SWPB insn, swaps only byte. */
11543 record_buf_mem[0] = 1;
11544 }
72508ac0
PO
11545 record_buf_mem[1] = u_regval[0];
11546 arm_insn_r->mem_rec_count = 1;
11547 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11548 arm_insn_r->reg_rec_count = 1;
11549 }
2d9e6acb
YQ
11550 else if (11 == arm_insn_r->decode || 13 == arm_insn_r->decode
11551 || 15 == arm_insn_r->decode)
72508ac0 11552 {
2d9e6acb
YQ
11553 if ((opcode1 & 0x12) == 2)
11554 {
11555 /* Extra load/store (unprivileged) */
11556 return -1;
11557 }
11558 else
11559 {
11560 /* Extra load/store */
11561 switch (bits (arm_insn_r->arm_insn, 5, 6))
11562 {
11563 case 1:
11564 if ((opcode1 & 0x05) == 0x0 || (opcode1 & 0x05) == 0x4)
11565 {
11566 /* STRH (register), STRH (immediate) */
11567 arm_record_strx (arm_insn_r, &record_buf[0],
11568 &record_buf_mem[0], ARM_RECORD_STRH);
11569 }
11570 else if ((opcode1 & 0x05) == 0x1)
11571 {
11572 /* LDRH (register) */
11573 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11574 arm_insn_r->reg_rec_count = 1;
72508ac0 11575
2d9e6acb
YQ
11576 if (bit (arm_insn_r->arm_insn, 21))
11577 {
11578 /* Write back to Rn. */
11579 record_buf[arm_insn_r->reg_rec_count++]
11580 = bits (arm_insn_r->arm_insn, 16, 19);
11581 }
11582 }
11583 else if ((opcode1 & 0x05) == 0x5)
11584 {
11585 /* LDRH (immediate), LDRH (literal) */
11586 int rn = bits (arm_insn_r->arm_insn, 16, 19);
72508ac0 11587
2d9e6acb
YQ
11588 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11589 arm_insn_r->reg_rec_count = 1;
11590
11591 if (rn != 15)
11592 {
11593 /*LDRH (immediate) */
11594 if (bit (arm_insn_r->arm_insn, 21))
11595 {
11596 /* Write back to Rn. */
11597 record_buf[arm_insn_r->reg_rec_count++] = rn;
11598 }
11599 }
11600 }
11601 else
11602 return -1;
11603 break;
11604 case 2:
11605 if ((opcode1 & 0x05) == 0x0)
11606 {
11607 /* LDRD (register) */
11608 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11609 record_buf[1] = record_buf[0] + 1;
11610 arm_insn_r->reg_rec_count = 2;
11611
11612 if (bit (arm_insn_r->arm_insn, 21))
11613 {
11614 /* Write back to Rn. */
11615 record_buf[arm_insn_r->reg_rec_count++]
11616 = bits (arm_insn_r->arm_insn, 16, 19);
11617 }
11618 }
11619 else if ((opcode1 & 0x05) == 0x1)
11620 {
11621 /* LDRSB (register) */
11622 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11623 arm_insn_r->reg_rec_count = 1;
11624
11625 if (bit (arm_insn_r->arm_insn, 21))
11626 {
11627 /* Write back to Rn. */
11628 record_buf[arm_insn_r->reg_rec_count++]
11629 = bits (arm_insn_r->arm_insn, 16, 19);
11630 }
11631 }
11632 else if ((opcode1 & 0x05) == 0x4 || (opcode1 & 0x05) == 0x5)
11633 {
11634 /* LDRD (immediate), LDRD (literal), LDRSB (immediate),
11635 LDRSB (literal) */
11636 int rn = bits (arm_insn_r->arm_insn, 16, 19);
11637
11638 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11639 arm_insn_r->reg_rec_count = 1;
11640
11641 if (rn != 15)
11642 {
11643 /*LDRD (immediate), LDRSB (immediate) */
11644 if (bit (arm_insn_r->arm_insn, 21))
11645 {
11646 /* Write back to Rn. */
11647 record_buf[arm_insn_r->reg_rec_count++] = rn;
11648 }
11649 }
11650 }
11651 else
11652 return -1;
11653 break;
11654 case 3:
11655 if ((opcode1 & 0x05) == 0x0)
11656 {
11657 /* STRD (register) */
11658 arm_record_strx (arm_insn_r, &record_buf[0],
11659 &record_buf_mem[0], ARM_RECORD_STRD);
11660 }
11661 else if ((opcode1 & 0x05) == 0x1)
11662 {
11663 /* LDRSH (register) */
11664 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11665 arm_insn_r->reg_rec_count = 1;
11666
11667 if (bit (arm_insn_r->arm_insn, 21))
11668 {
11669 /* Write back to Rn. */
11670 record_buf[arm_insn_r->reg_rec_count++]
11671 = bits (arm_insn_r->arm_insn, 16, 19);
11672 }
11673 }
11674 else if ((opcode1 & 0x05) == 0x4)
11675 {
11676 /* STRD (immediate) */
11677 arm_record_strx (arm_insn_r, &record_buf[0],
11678 &record_buf_mem[0], ARM_RECORD_STRD);
11679 }
11680 else if ((opcode1 & 0x05) == 0x5)
11681 {
11682 /* LDRSH (immediate), LDRSH (literal) */
11683 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11684 arm_insn_r->reg_rec_count = 1;
11685
11686 if (bit (arm_insn_r->arm_insn, 21))
11687 {
11688 /* Write back to Rn. */
11689 record_buf[arm_insn_r->reg_rec_count++]
11690 = bits (arm_insn_r->arm_insn, 16, 19);
11691 }
11692 }
11693 else
11694 return -1;
11695 break;
11696 default:
11697 return -1;
11698 }
11699 }
72508ac0
PO
11700 }
11701 else
11702 {
11703 return -1;
11704 }
11705
11706 REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
11707 MEM_ALLOC (arm_insn_r->arm_mems, arm_insn_r->mem_rec_count, record_buf_mem);
11708 return 0;
11709}
11710
11711/* Handling opcode 001 insns. */
11712
11713static int
4748a9be 11714arm_record_data_proc_imm (arm_insn_decode_record *arm_insn_r)
72508ac0
PO
11715{
11716 uint32_t record_buf[8], record_buf_mem[8];
11717
11718 arm_insn_r->opcode = bits (arm_insn_r->arm_insn, 21, 24);
11719 arm_insn_r->decode = bits (arm_insn_r->arm_insn, 4, 7);
11720
11721 if ((9 == arm_insn_r->opcode || 11 == arm_insn_r->opcode)
11722 && 2 == bits (arm_insn_r->arm_insn, 20, 21)
11723 && sbo_sbz (arm_insn_r->arm_insn, 13, 4, 1)
11724 )
11725 {
11726 /* Handle MSR insn. */
11727 if (9 == arm_insn_r->opcode)
dda83cd7
SM
11728 {
11729 /* CSPR is going to be changed. */
11730 record_buf[0] = ARM_PS_REGNUM;
11731 arm_insn_r->reg_rec_count = 1;
11732 }
72508ac0 11733 else
dda83cd7
SM
11734 {
11735 /* SPSR is going to be changed. */
11736 }
72508ac0
PO
11737 }
11738 else if (arm_insn_r->opcode <= 15)
11739 {
11740 /* Normal data processing insns. */
11741 /* Out of 11 shifter operands mode, all the insn modifies destination
dda83cd7 11742 register, which is specified by 13-16 decode. */
72508ac0
PO
11743 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11744 record_buf[1] = ARM_PS_REGNUM;
11745 arm_insn_r->reg_rec_count = 2;
11746 }
11747 else
11748 {
11749 return -1;
11750 }
11751
11752 REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
11753 MEM_ALLOC (arm_insn_r->arm_mems, arm_insn_r->mem_rec_count, record_buf_mem);
11754 return 0;
11755}
11756
c55978a6 11757static int
4748a9be 11758arm_record_media (arm_insn_decode_record *arm_insn_r)
c55978a6
YQ
11759{
11760 uint32_t record_buf[8];
11761
11762 switch (bits (arm_insn_r->arm_insn, 22, 24))
11763 {
11764 case 0:
11765 /* Parallel addition and subtraction, signed */
11766 case 1:
11767 /* Parallel addition and subtraction, unsigned */
11768 case 2:
11769 case 3:
11770 /* Packing, unpacking, saturation and reversal */
11771 {
11772 int rd = bits (arm_insn_r->arm_insn, 12, 15);
11773
11774 record_buf[arm_insn_r->reg_rec_count++] = rd;
11775 }
11776 break;
11777
11778 case 4:
11779 case 5:
11780 /* Signed multiplies */
11781 {
11782 int rd = bits (arm_insn_r->arm_insn, 16, 19);
11783 unsigned int op1 = bits (arm_insn_r->arm_insn, 20, 22);
11784
11785 record_buf[arm_insn_r->reg_rec_count++] = rd;
11786 if (op1 == 0x0)
11787 record_buf[arm_insn_r->reg_rec_count++] = ARM_PS_REGNUM;
11788 else if (op1 == 0x4)
11789 record_buf[arm_insn_r->reg_rec_count++]
11790 = bits (arm_insn_r->arm_insn, 12, 15);
11791 }
11792 break;
11793
11794 case 6:
11795 {
11796 if (bit (arm_insn_r->arm_insn, 21)
11797 && bits (arm_insn_r->arm_insn, 5, 6) == 0x2)
11798 {
11799 /* SBFX */
11800 record_buf[arm_insn_r->reg_rec_count++]
11801 = bits (arm_insn_r->arm_insn, 12, 15);
11802 }
11803 else if (bits (arm_insn_r->arm_insn, 20, 21) == 0x0
11804 && bits (arm_insn_r->arm_insn, 5, 7) == 0x0)
11805 {
11806 /* USAD8 and USADA8 */
11807 record_buf[arm_insn_r->reg_rec_count++]
11808 = bits (arm_insn_r->arm_insn, 16, 19);
11809 }
11810 }
11811 break;
11812
11813 case 7:
11814 {
11815 if (bits (arm_insn_r->arm_insn, 20, 21) == 0x3
11816 && bits (arm_insn_r->arm_insn, 5, 7) == 0x7)
11817 {
11818 /* Permanently UNDEFINED */
11819 return -1;
11820 }
11821 else
11822 {
11823 /* BFC, BFI and UBFX */
11824 record_buf[arm_insn_r->reg_rec_count++]
11825 = bits (arm_insn_r->arm_insn, 12, 15);
11826 }
11827 }
11828 break;
11829
11830 default:
11831 return -1;
11832 }
11833
11834 REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
11835
11836 return 0;
11837}
11838
71e396f9 11839/* Handle ARM mode instructions with opcode 010. */
72508ac0
PO
11840
11841static int
4748a9be 11842arm_record_ld_st_imm_offset (arm_insn_decode_record *arm_insn_r)
72508ac0
PO
11843{
11844 struct regcache *reg_cache = arm_insn_r->regcache;
11845
71e396f9
LM
11846 uint32_t reg_base , reg_dest;
11847 uint32_t offset_12, tgt_mem_addr;
72508ac0 11848 uint32_t record_buf[8], record_buf_mem[8];
71e396f9
LM
11849 unsigned char wback;
11850 ULONGEST u_regval;
72508ac0 11851
71e396f9
LM
11852 /* Calculate wback. */
11853 wback = (bit (arm_insn_r->arm_insn, 24) == 0)
11854 || (bit (arm_insn_r->arm_insn, 21) == 1);
72508ac0 11855
71e396f9
LM
11856 arm_insn_r->reg_rec_count = 0;
11857 reg_base = bits (arm_insn_r->arm_insn, 16, 19);
72508ac0
PO
11858
11859 if (bit (arm_insn_r->arm_insn, INSN_S_L_BIT_NUM))
11860 {
71e396f9
LM
11861 /* LDR (immediate), LDR (literal), LDRB (immediate), LDRB (literal), LDRBT
11862 and LDRT. */
11863
72508ac0 11864 reg_dest = bits (arm_insn_r->arm_insn, 12, 15);
71e396f9
LM
11865 record_buf[arm_insn_r->reg_rec_count++] = reg_dest;
11866
11867 /* The LDR instruction is capable of doing branching. If MOV LR, PC
11868 preceeds a LDR instruction having R15 as reg_base, it
11869 emulates a branch and link instruction, and hence we need to save
11870 CPSR and PC as well. */
11871 if (ARM_PC_REGNUM == reg_dest)
11872 record_buf[arm_insn_r->reg_rec_count++] = ARM_PS_REGNUM;
11873
11874 /* If wback is true, also save the base register, which is going to be
11875 written to. */
11876 if (wback)
11877 record_buf[arm_insn_r->reg_rec_count++] = reg_base;
72508ac0
PO
11878 }
11879 else
11880 {
71e396f9
LM
11881 /* STR (immediate), STRB (immediate), STRBT and STRT. */
11882
72508ac0 11883 offset_12 = bits (arm_insn_r->arm_insn, 0, 11);
71e396f9
LM
11884 regcache_raw_read_unsigned (reg_cache, reg_base, &u_regval);
11885
11886 /* Handle bit U. */
72508ac0 11887 if (bit (arm_insn_r->arm_insn, 23))
71e396f9
LM
11888 {
11889 /* U == 1: Add the offset. */
11890 tgt_mem_addr = (uint32_t) u_regval + offset_12;
11891 }
72508ac0 11892 else
71e396f9
LM
11893 {
11894 /* U == 0: subtract the offset. */
11895 tgt_mem_addr = (uint32_t) u_regval - offset_12;
11896 }
11897
11898 /* Bit 22 tells us whether the store instruction writes 1 byte or 4
11899 bytes. */
11900 if (bit (arm_insn_r->arm_insn, 22))
11901 {
11902 /* STRB and STRBT: 1 byte. */
11903 record_buf_mem[0] = 1;
11904 }
11905 else
11906 {
11907 /* STR and STRT: 4 bytes. */
11908 record_buf_mem[0] = 4;
11909 }
11910
11911 /* Handle bit P. */
11912 if (bit (arm_insn_r->arm_insn, 24))
11913 record_buf_mem[1] = tgt_mem_addr;
11914 else
11915 record_buf_mem[1] = (uint32_t) u_regval;
72508ac0 11916
72508ac0
PO
11917 arm_insn_r->mem_rec_count = 1;
11918
71e396f9
LM
11919 /* If wback is true, also save the base register, which is going to be
11920 written to. */
11921 if (wback)
11922 record_buf[arm_insn_r->reg_rec_count++] = reg_base;
72508ac0
PO
11923 }
11924
11925 REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
11926 MEM_ALLOC (arm_insn_r->arm_mems, arm_insn_r->mem_rec_count, record_buf_mem);
11927 return 0;
11928}
11929
11930/* Handling opcode 011 insns. */
11931
11932static int
4748a9be 11933arm_record_ld_st_reg_offset (arm_insn_decode_record *arm_insn_r)
72508ac0
PO
11934{
11935 struct regcache *reg_cache = arm_insn_r->regcache;
11936
11937 uint32_t shift_imm = 0;
11938 uint32_t reg_src1 = 0, reg_src2 = 0, reg_dest = 0;
11939 uint32_t offset_12 = 0, tgt_mem_addr = 0;
11940 uint32_t record_buf[8], record_buf_mem[8];
11941
11942 LONGEST s_word;
11943 ULONGEST u_regval[2];
11944
c55978a6
YQ
11945 if (bit (arm_insn_r->arm_insn, 4))
11946 return arm_record_media (arm_insn_r);
11947
72508ac0
PO
11948 arm_insn_r->opcode = bits (arm_insn_r->arm_insn, 21, 24);
11949 arm_insn_r->decode = bits (arm_insn_r->arm_insn, 4, 7);
11950
11951 /* Handle enhanced store insns and LDRD DSP insn,
11952 order begins according to addressing modes for store insns
11953 STRH insn. */
11954
11955 /* LDR or STR? */
11956 if (bit (arm_insn_r->arm_insn, INSN_S_L_BIT_NUM))
11957 {
11958 reg_dest = bits (arm_insn_r->arm_insn, 12, 15);
11959 /* LDR insn has a capability to do branching, if
dda83cd7
SM
11960 MOV LR, PC is preceded by LDR insn having Rn as R15
11961 in that case, it emulates branch and link insn, and hence we
11962 need to save CSPR and PC as well. */
72508ac0 11963 if (15 != reg_dest)
dda83cd7
SM
11964 {
11965 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
11966 arm_insn_r->reg_rec_count = 1;
11967 }
72508ac0 11968 else
dda83cd7
SM
11969 {
11970 record_buf[0] = reg_dest;
11971 record_buf[1] = ARM_PS_REGNUM;
11972 arm_insn_r->reg_rec_count = 2;
11973 }
72508ac0
PO
11974 }
11975 else
11976 {
11977 if (! bits (arm_insn_r->arm_insn, 4, 11))
dda83cd7
SM
11978 {
11979 /* Store insn, register offset and register pre-indexed,
11980 register post-indexed. */
11981 /* Get Rm. */
11982 reg_src1 = bits (arm_insn_r->arm_insn, 0, 3);
11983 /* Get Rn. */
11984 reg_src2 = bits (arm_insn_r->arm_insn, 16, 19);
11985 regcache_raw_read_unsigned (reg_cache, reg_src1
11986 , &u_regval[0]);
11987 regcache_raw_read_unsigned (reg_cache, reg_src2
11988 , &u_regval[1]);
11989 if (15 == reg_src2)
11990 {
11991 /* If R15 was used as Rn, hence current PC+8. */
11992 /* Pre-indexed mode doesnt reach here ; illegal insn. */
11993 u_regval[0] = u_regval[0] + 8;
11994 }
11995 /* Calculate target store address, Rn +/- Rm, register offset. */
11996 /* U == 1. */
11997 if (bit (arm_insn_r->arm_insn, 23))
11998 {
11999 tgt_mem_addr = u_regval[0] + u_regval[1];
12000 }
12001 else
12002 {
12003 tgt_mem_addr = u_regval[1] - u_regval[0];
12004 }
12005
12006 switch (arm_insn_r->opcode)
12007 {
12008 /* STR. */
12009 case 8:
12010 case 12:
12011 /* STR. */
12012 case 9:
12013 case 13:
12014 /* STRT. */
12015 case 1:
12016 case 5:
12017 /* STR. */
12018 case 0:
12019 case 4:
12020 record_buf_mem[0] = 4;
12021 break;
12022
12023 /* STRB. */
12024 case 10:
12025 case 14:
12026 /* STRB. */
12027 case 11:
12028 case 15:
12029 /* STRBT. */
12030 case 3:
12031 case 7:
12032 /* STRB. */
12033 case 2:
12034 case 6:
12035 record_buf_mem[0] = 1;
12036 break;
12037
12038 default:
12039 gdb_assert_not_reached ("no decoding pattern found");
12040 break;
12041 }
12042 record_buf_mem[1] = tgt_mem_addr;
12043 arm_insn_r->mem_rec_count = 1;
12044
12045 if (9 == arm_insn_r->opcode || 11 == arm_insn_r->opcode
12046 || 13 == arm_insn_r->opcode || 15 == arm_insn_r->opcode
12047 || 0 == arm_insn_r->opcode || 2 == arm_insn_r->opcode
12048 || 4 == arm_insn_r->opcode || 6 == arm_insn_r->opcode
12049 || 1 == arm_insn_r->opcode || 3 == arm_insn_r->opcode
12050 || 5 == arm_insn_r->opcode || 7 == arm_insn_r->opcode
12051 )
12052 {
12053 /* Rn is going to be changed in pre-indexed mode and
12054 post-indexed mode as well. */
12055 record_buf[0] = reg_src2;
12056 arm_insn_r->reg_rec_count = 1;
12057 }
12058 }
72508ac0 12059 else
dda83cd7
SM
12060 {
12061 /* Store insn, scaled register offset; scaled pre-indexed. */
12062 offset_12 = bits (arm_insn_r->arm_insn, 5, 6);
12063 /* Get Rm. */
12064 reg_src1 = bits (arm_insn_r->arm_insn, 0, 3);
12065 /* Get Rn. */
12066 reg_src2 = bits (arm_insn_r->arm_insn, 16, 19);
12067 /* Get shift_imm. */
12068 shift_imm = bits (arm_insn_r->arm_insn, 7, 11);
12069 regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval[0]);
12070 regcache_raw_read_signed (reg_cache, reg_src1, &s_word);
12071 regcache_raw_read_unsigned (reg_cache, reg_src2, &u_regval[1]);
12072 /* Offset_12 used as shift. */
12073 switch (offset_12)
12074 {
12075 case 0:
12076 /* Offset_12 used as index. */
12077 offset_12 = u_regval[0] << shift_imm;
12078 break;
12079
12080 case 1:
12081 offset_12 = (!shift_imm)?0:u_regval[0] >> shift_imm;
12082 break;
12083
12084 case 2:
12085 if (!shift_imm)
12086 {
12087 if (bit (u_regval[0], 31))
12088 {
12089 offset_12 = 0xFFFFFFFF;
12090 }
12091 else
12092 {
12093 offset_12 = 0;
12094 }
12095 }
12096 else
12097 {
12098 /* This is arithmetic shift. */
12099 offset_12 = s_word >> shift_imm;
12100 }
12101 break;
12102
12103 case 3:
12104 if (!shift_imm)
12105 {
12106 regcache_raw_read_unsigned (reg_cache, ARM_PS_REGNUM,
12107 &u_regval[1]);
12108 /* Get C flag value and shift it by 31. */
12109 offset_12 = (((bit (u_regval[1], 29)) << 31) \
12110 | (u_regval[0]) >> 1);
12111 }
12112 else
12113 {
12114 offset_12 = (u_regval[0] >> shift_imm) \
12115 | (u_regval[0] <<
12116 (sizeof(uint32_t) - shift_imm));
12117 }
12118 break;
12119
12120 default:
12121 gdb_assert_not_reached ("no decoding pattern found");
12122 break;
12123 }
12124
12125 regcache_raw_read_unsigned (reg_cache, reg_src2, &u_regval[1]);
12126 /* bit U set. */
12127 if (bit (arm_insn_r->arm_insn, 23))
12128 {
12129 tgt_mem_addr = u_regval[1] + offset_12;
12130 }
12131 else
12132 {
12133 tgt_mem_addr = u_regval[1] - offset_12;
12134 }
12135
12136 switch (arm_insn_r->opcode)
12137 {
12138 /* STR. */
12139 case 8:
12140 case 12:
12141 /* STR. */
12142 case 9:
12143 case 13:
12144 /* STRT. */
12145 case 1:
12146 case 5:
12147 /* STR. */
12148 case 0:
12149 case 4:
12150 record_buf_mem[0] = 4;
12151 break;
12152
12153 /* STRB. */
12154 case 10:
12155 case 14:
12156 /* STRB. */
12157 case 11:
12158 case 15:
12159 /* STRBT. */
12160 case 3:
12161 case 7:
12162 /* STRB. */
12163 case 2:
12164 case 6:
12165 record_buf_mem[0] = 1;
12166 break;
12167
12168 default:
12169 gdb_assert_not_reached ("no decoding pattern found");
12170 break;
12171 }
12172 record_buf_mem[1] = tgt_mem_addr;
12173 arm_insn_r->mem_rec_count = 1;
12174
12175 if (9 == arm_insn_r->opcode || 11 == arm_insn_r->opcode
12176 || 13 == arm_insn_r->opcode || 15 == arm_insn_r->opcode
12177 || 0 == arm_insn_r->opcode || 2 == arm_insn_r->opcode
12178 || 4 == arm_insn_r->opcode || 6 == arm_insn_r->opcode
12179 || 1 == arm_insn_r->opcode || 3 == arm_insn_r->opcode
12180 || 5 == arm_insn_r->opcode || 7 == arm_insn_r->opcode
12181 )
12182 {
12183 /* Rn is going to be changed in register scaled pre-indexed
12184 mode,and scaled post indexed mode. */
12185 record_buf[0] = reg_src2;
12186 arm_insn_r->reg_rec_count = 1;
12187 }
12188 }
72508ac0
PO
12189 }
12190
12191 REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
12192 MEM_ALLOC (arm_insn_r->arm_mems, arm_insn_r->mem_rec_count, record_buf_mem);
12193 return 0;
12194}
12195
71e396f9 12196/* Handle ARM mode instructions with opcode 100. */
72508ac0
PO
12197
12198static int
4748a9be 12199arm_record_ld_st_multiple (arm_insn_decode_record *arm_insn_r)
72508ac0
PO
12200{
12201 struct regcache *reg_cache = arm_insn_r->regcache;
71e396f9
LM
12202 uint32_t register_count = 0, register_bits;
12203 uint32_t reg_base, addr_mode;
72508ac0 12204 uint32_t record_buf[24], record_buf_mem[48];
71e396f9
LM
12205 uint32_t wback;
12206 ULONGEST u_regval;
72508ac0 12207
71e396f9
LM
12208 /* Fetch the list of registers. */
12209 register_bits = bits (arm_insn_r->arm_insn, 0, 15);
12210 arm_insn_r->reg_rec_count = 0;
12211
12212 /* Fetch the base register that contains the address we are loading data
12213 to. */
12214 reg_base = bits (arm_insn_r->arm_insn, 16, 19);
72508ac0 12215
71e396f9
LM
12216 /* Calculate wback. */
12217 wback = (bit (arm_insn_r->arm_insn, 21) == 1);
72508ac0
PO
12218
12219 if (bit (arm_insn_r->arm_insn, INSN_S_L_BIT_NUM))
12220 {
71e396f9 12221 /* LDM/LDMIA/LDMFD, LDMDA/LDMFA, LDMDB and LDMIB. */
72508ac0 12222
71e396f9 12223 /* Find out which registers are going to be loaded from memory. */
72508ac0 12224 while (register_bits)
71e396f9
LM
12225 {
12226 if (register_bits & 0x00000001)
12227 record_buf[arm_insn_r->reg_rec_count++] = register_count;
12228 register_bits = register_bits >> 1;
12229 register_count++;
12230 }
72508ac0 12231
71e396f9
LM
12232
12233 /* If wback is true, also save the base register, which is going to be
12234 written to. */
12235 if (wback)
12236 record_buf[arm_insn_r->reg_rec_count++] = reg_base;
12237
12238 /* Save the CPSR register. */
12239 record_buf[arm_insn_r->reg_rec_count++] = ARM_PS_REGNUM;
72508ac0
PO
12240 }
12241 else
12242 {
71e396f9 12243 /* STM (STMIA, STMEA), STMDA (STMED), STMDB (STMFD) and STMIB (STMFA). */
72508ac0 12244
71e396f9
LM
12245 addr_mode = bits (arm_insn_r->arm_insn, 23, 24);
12246
12247 regcache_raw_read_unsigned (reg_cache, reg_base, &u_regval);
12248
12249 /* Find out how many registers are going to be stored to memory. */
72508ac0 12250 while (register_bits)
71e396f9
LM
12251 {
12252 if (register_bits & 0x00000001)
12253 register_count++;
12254 register_bits = register_bits >> 1;
12255 }
72508ac0
PO
12256
12257 switch (addr_mode)
71e396f9
LM
12258 {
12259 /* STMDA (STMED): Decrement after. */
12260 case 0:
12261 record_buf_mem[1] = (uint32_t) u_regval
f0452268 12262 - register_count * ARM_INT_REGISTER_SIZE + 4;
71e396f9
LM
12263 break;
12264 /* STM (STMIA, STMEA): Increment after. */
12265 case 1:
12266 record_buf_mem[1] = (uint32_t) u_regval;
12267 break;
12268 /* STMDB (STMFD): Decrement before. */
12269 case 2:
12270 record_buf_mem[1] = (uint32_t) u_regval
f0452268 12271 - register_count * ARM_INT_REGISTER_SIZE;
71e396f9
LM
12272 break;
12273 /* STMIB (STMFA): Increment before. */
12274 case 3:
f0452268 12275 record_buf_mem[1] = (uint32_t) u_regval + ARM_INT_REGISTER_SIZE;
71e396f9
LM
12276 break;
12277 default:
12278 gdb_assert_not_reached ("no decoding pattern found");
12279 break;
12280 }
72508ac0 12281
f0452268 12282 record_buf_mem[0] = register_count * ARM_INT_REGISTER_SIZE;
71e396f9
LM
12283 arm_insn_r->mem_rec_count = 1;
12284
12285 /* If wback is true, also save the base register, which is going to be
12286 written to. */
12287 if (wback)
12288 record_buf[arm_insn_r->reg_rec_count++] = reg_base;
72508ac0
PO
12289 }
12290
12291 REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
12292 MEM_ALLOC (arm_insn_r->arm_mems, arm_insn_r->mem_rec_count, record_buf_mem);
12293 return 0;
12294}
12295
12296/* Handling opcode 101 insns. */
12297
12298static int
4748a9be 12299arm_record_b_bl (arm_insn_decode_record *arm_insn_r)
72508ac0
PO
12300{
12301 uint32_t record_buf[8];
12302
12303 /* Handle B, BL, BLX(1) insns. */
12304 /* B simply branches so we do nothing here. */
12305 /* Note: BLX(1) doesnt fall here but instead it falls into
12306 extension space. */
12307 if (bit (arm_insn_r->arm_insn, 24))
01add95b
SM
12308 {
12309 record_buf[0] = ARM_LR_REGNUM;
12310 arm_insn_r->reg_rec_count = 1;
12311 }
72508ac0
PO
12312
12313 REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
12314
12315 return 0;
12316}
12317
72508ac0 12318static int
4748a9be 12319arm_record_unsupported_insn (arm_insn_decode_record *arm_insn_r)
72508ac0 12320{
6cb06a8c
TT
12321 gdb_printf (gdb_stderr,
12322 _("Process record does not support instruction "
12323 "0x%0x at address %s.\n"),arm_insn_r->arm_insn,
12324 paddress (arm_insn_r->gdbarch, arm_insn_r->this_addr));
72508ac0
PO
12325
12326 return -1;
12327}
12328
5a578da5
OJ
12329/* Record handler for vector data transfer instructions. */
12330
12331static int
4748a9be 12332arm_record_vdata_transfer_insn (arm_insn_decode_record *arm_insn_r)
5a578da5
OJ
12333{
12334 uint32_t bits_a, bit_c, bit_l, reg_t, reg_v;
12335 uint32_t record_buf[4];
12336
5a578da5
OJ
12337 reg_t = bits (arm_insn_r->arm_insn, 12, 15);
12338 reg_v = bits (arm_insn_r->arm_insn, 21, 23);
12339 bits_a = bits (arm_insn_r->arm_insn, 21, 23);
12340 bit_l = bit (arm_insn_r->arm_insn, 20);
12341 bit_c = bit (arm_insn_r->arm_insn, 8);
12342
12343 /* Handle VMOV instruction. */
12344 if (bit_l && bit_c)
12345 {
12346 record_buf[0] = reg_t;
12347 arm_insn_r->reg_rec_count = 1;
12348 }
12349 else if (bit_l && !bit_c)
12350 {
12351 /* Handle VMOV instruction. */
12352 if (bits_a == 0x00)
dda83cd7 12353 {
f1771dce 12354 record_buf[0] = reg_t;
dda83cd7
SM
12355 arm_insn_r->reg_rec_count = 1;
12356 }
5a578da5
OJ
12357 /* Handle VMRS instruction. */
12358 else if (bits_a == 0x07)
dda83cd7
SM
12359 {
12360 if (reg_t == 15)
12361 reg_t = ARM_PS_REGNUM;
5a578da5 12362
dda83cd7
SM
12363 record_buf[0] = reg_t;
12364 arm_insn_r->reg_rec_count = 1;
12365 }
5a578da5
OJ
12366 }
12367 else if (!bit_l && !bit_c)
12368 {
12369 /* Handle VMOV instruction. */
12370 if (bits_a == 0x00)
dda83cd7 12371 {
f1771dce 12372 record_buf[0] = ARM_D0_REGNUM + reg_v;
5a578da5 12373
dda83cd7
SM
12374 arm_insn_r->reg_rec_count = 1;
12375 }
5a578da5
OJ
12376 /* Handle VMSR instruction. */
12377 else if (bits_a == 0x07)
dda83cd7
SM
12378 {
12379 record_buf[0] = ARM_FPSCR_REGNUM;
12380 arm_insn_r->reg_rec_count = 1;
12381 }
5a578da5
OJ
12382 }
12383 else if (!bit_l && bit_c)
12384 {
12385 /* Handle VMOV instruction. */
12386 if (!(bits_a & 0x04))
dda83cd7
SM
12387 {
12388 record_buf[0] = (reg_v | (bit (arm_insn_r->arm_insn, 7) << 4))
12389 + ARM_D0_REGNUM;
12390 arm_insn_r->reg_rec_count = 1;
12391 }
5a578da5
OJ
12392 /* Handle VDUP instruction. */
12393 else
dda83cd7
SM
12394 {
12395 if (bit (arm_insn_r->arm_insn, 21))
12396 {
12397 reg_v = reg_v | (bit (arm_insn_r->arm_insn, 7) << 4);
12398 record_buf[0] = reg_v + ARM_D0_REGNUM;
12399 record_buf[1] = reg_v + ARM_D0_REGNUM + 1;
12400 arm_insn_r->reg_rec_count = 2;
12401 }
12402 else
12403 {
12404 reg_v = reg_v | (bit (arm_insn_r->arm_insn, 7) << 4);
12405 record_buf[0] = reg_v + ARM_D0_REGNUM;
12406 arm_insn_r->reg_rec_count = 1;
12407 }
12408 }
12409 }
12410
12411 REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
12412 return 0;
12413}
12414
f20f80dd
OJ
12415/* Record handler for extension register load/store instructions. */
12416
12417static int
4748a9be 12418arm_record_exreg_ld_st_insn (arm_insn_decode_record *arm_insn_r)
f20f80dd
OJ
12419{
12420 uint32_t opcode, single_reg;
12421 uint8_t op_vldm_vstm;
12422 uint32_t record_buf[8], record_buf_mem[128];
12423 ULONGEST u_regval = 0;
12424
12425 struct regcache *reg_cache = arm_insn_r->regcache;
f20f80dd
OJ
12426
12427 opcode = bits (arm_insn_r->arm_insn, 20, 24);
9fde51ed 12428 single_reg = !bit (arm_insn_r->arm_insn, 8);
f20f80dd
OJ
12429 op_vldm_vstm = opcode & 0x1b;
12430
12431 /* Handle VMOV instructions. */
12432 if ((opcode & 0x1e) == 0x04)
12433 {
9fde51ed 12434 if (bit (arm_insn_r->arm_insn, 20)) /* to_arm_registers bit 20? */
01e57735
YQ
12435 {
12436 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
12437 record_buf[1] = bits (arm_insn_r->arm_insn, 16, 19);
12438 arm_insn_r->reg_rec_count = 2;
12439 }
f20f80dd 12440 else
01e57735 12441 {
9fde51ed
YQ
12442 uint8_t reg_m = bits (arm_insn_r->arm_insn, 0, 3);
12443 uint8_t bit_m = bit (arm_insn_r->arm_insn, 5);
f20f80dd 12444
9fde51ed 12445 if (single_reg)
01e57735 12446 {
9fde51ed
YQ
12447 /* The first S register number m is REG_M:M (M is bit 5),
12448 the corresponding D register number is REG_M:M / 2, which
12449 is REG_M. */
12450 record_buf[arm_insn_r->reg_rec_count++] = ARM_D0_REGNUM + reg_m;
12451 /* The second S register number is REG_M:M + 1, the
12452 corresponding D register number is (REG_M:M + 1) / 2.
12453 IOW, if bit M is 1, the first and second S registers
12454 are mapped to different D registers, otherwise, they are
12455 in the same D register. */
12456 if (bit_m)
12457 {
12458 record_buf[arm_insn_r->reg_rec_count++]
12459 = ARM_D0_REGNUM + reg_m + 1;
12460 }
01e57735
YQ
12461 }
12462 else
12463 {
9fde51ed 12464 record_buf[0] = ((bit_m << 4) + reg_m + ARM_D0_REGNUM);
01e57735
YQ
12465 arm_insn_r->reg_rec_count = 1;
12466 }
12467 }
f20f80dd
OJ
12468 }
12469 /* Handle VSTM and VPUSH instructions. */
12470 else if (op_vldm_vstm == 0x08 || op_vldm_vstm == 0x0a
01e57735 12471 || op_vldm_vstm == 0x12)
f20f80dd
OJ
12472 {
12473 uint32_t start_address, reg_rn, imm_off32, imm_off8, memory_count;
12474 uint32_t memory_index = 0;
12475
12476 reg_rn = bits (arm_insn_r->arm_insn, 16, 19);
12477 regcache_raw_read_unsigned (reg_cache, reg_rn, &u_regval);
12478 imm_off8 = bits (arm_insn_r->arm_insn, 0, 7);
9fde51ed 12479 imm_off32 = imm_off8 << 2;
f20f80dd
OJ
12480 memory_count = imm_off8;
12481
12482 if (bit (arm_insn_r->arm_insn, 23))
01e57735 12483 start_address = u_regval;
f20f80dd 12484 else
01e57735 12485 start_address = u_regval - imm_off32;
f20f80dd
OJ
12486
12487 if (bit (arm_insn_r->arm_insn, 21))
01e57735
YQ
12488 {
12489 record_buf[0] = reg_rn;
12490 arm_insn_r->reg_rec_count = 1;
12491 }
f20f80dd
OJ
12492
12493 while (memory_count > 0)
01e57735 12494 {
9fde51ed 12495 if (single_reg)
01e57735 12496 {
9fde51ed
YQ
12497 record_buf_mem[memory_index] = 4;
12498 record_buf_mem[memory_index + 1] = start_address;
01e57735
YQ
12499 start_address = start_address + 4;
12500 memory_index = memory_index + 2;
12501 }
12502 else
12503 {
9fde51ed
YQ
12504 record_buf_mem[memory_index] = 4;
12505 record_buf_mem[memory_index + 1] = start_address;
12506 record_buf_mem[memory_index + 2] = 4;
12507 record_buf_mem[memory_index + 3] = start_address + 4;
01e57735
YQ
12508 start_address = start_address + 8;
12509 memory_index = memory_index + 4;
12510 }
12511 memory_count--;
12512 }
f20f80dd
OJ
12513 arm_insn_r->mem_rec_count = (memory_index >> 1);
12514 }
12515 /* Handle VLDM instructions. */
12516 else if (op_vldm_vstm == 0x09 || op_vldm_vstm == 0x0b
01e57735 12517 || op_vldm_vstm == 0x13)
f20f80dd
OJ
12518 {
12519 uint32_t reg_count, reg_vd;
12520 uint32_t reg_index = 0;
9fde51ed 12521 uint32_t bit_d = bit (arm_insn_r->arm_insn, 22);
f20f80dd
OJ
12522
12523 reg_vd = bits (arm_insn_r->arm_insn, 12, 15);
12524 reg_count = bits (arm_insn_r->arm_insn, 0, 7);
12525
9fde51ed
YQ
12526 /* REG_VD is the first D register number. If the instruction
12527 loads memory to S registers (SINGLE_REG is TRUE), the register
12528 number is (REG_VD << 1 | bit D), so the corresponding D
12529 register number is (REG_VD << 1 | bit D) / 2 = REG_VD. */
12530 if (!single_reg)
12531 reg_vd = reg_vd | (bit_d << 4);
f20f80dd 12532
9fde51ed 12533 if (bit (arm_insn_r->arm_insn, 21) /* write back */)
01e57735 12534 record_buf[reg_index++] = bits (arm_insn_r->arm_insn, 16, 19);
f20f80dd 12535
9fde51ed
YQ
12536 /* If the instruction loads memory to D register, REG_COUNT should
12537 be divided by 2, according to the ARM Architecture Reference
12538 Manual. If the instruction loads memory to S register, divide by
12539 2 as well because two S registers are mapped to D register. */
12540 reg_count = reg_count / 2;
12541 if (single_reg && bit_d)
01e57735 12542 {
9fde51ed
YQ
12543 /* Increase the register count if S register list starts from
12544 an odd number (bit d is one). */
12545 reg_count++;
12546 }
f20f80dd 12547
9fde51ed
YQ
12548 while (reg_count > 0)
12549 {
12550 record_buf[reg_index++] = ARM_D0_REGNUM + reg_vd + reg_count - 1;
01e57735
YQ
12551 reg_count--;
12552 }
f20f80dd
OJ
12553 arm_insn_r->reg_rec_count = reg_index;
12554 }
12555 /* VSTR Vector store register. */
12556 else if ((opcode & 0x13) == 0x10)
12557 {
bec2ab5a 12558 uint32_t start_address, reg_rn, imm_off32, imm_off8;
f20f80dd
OJ
12559 uint32_t memory_index = 0;
12560
12561 reg_rn = bits (arm_insn_r->arm_insn, 16, 19);
12562 regcache_raw_read_unsigned (reg_cache, reg_rn, &u_regval);
12563 imm_off8 = bits (arm_insn_r->arm_insn, 0, 7);
9fde51ed 12564 imm_off32 = imm_off8 << 2;
f20f80dd
OJ
12565
12566 if (bit (arm_insn_r->arm_insn, 23))
01e57735 12567 start_address = u_regval + imm_off32;
f20f80dd 12568 else
01e57735 12569 start_address = u_regval - imm_off32;
f20f80dd
OJ
12570
12571 if (single_reg)
01e57735 12572 {
9fde51ed
YQ
12573 record_buf_mem[memory_index] = 4;
12574 record_buf_mem[memory_index + 1] = start_address;
01e57735
YQ
12575 arm_insn_r->mem_rec_count = 1;
12576 }
f20f80dd 12577 else
01e57735 12578 {
9fde51ed
YQ
12579 record_buf_mem[memory_index] = 4;
12580 record_buf_mem[memory_index + 1] = start_address;
12581 record_buf_mem[memory_index + 2] = 4;
12582 record_buf_mem[memory_index + 3] = start_address + 4;
01e57735
YQ
12583 arm_insn_r->mem_rec_count = 2;
12584 }
f20f80dd
OJ
12585 }
12586 /* VLDR Vector load register. */
12587 else if ((opcode & 0x13) == 0x11)
12588 {
12589 uint32_t reg_vd = bits (arm_insn_r->arm_insn, 12, 15);
12590
12591 if (!single_reg)
01e57735
YQ
12592 {
12593 reg_vd = reg_vd | (bit (arm_insn_r->arm_insn, 22) << 4);
12594 record_buf[0] = ARM_D0_REGNUM + reg_vd;
12595 }
f20f80dd 12596 else
01e57735
YQ
12597 {
12598 reg_vd = (reg_vd << 1) | bit (arm_insn_r->arm_insn, 22);
9fde51ed
YQ
12599 /* Record register D rather than pseudo register S. */
12600 record_buf[0] = ARM_D0_REGNUM + reg_vd / 2;
01e57735 12601 }
f20f80dd
OJ
12602 arm_insn_r->reg_rec_count = 1;
12603 }
12604
12605 REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
12606 MEM_ALLOC (arm_insn_r->arm_mems, arm_insn_r->mem_rec_count, record_buf_mem);
12607 return 0;
12608}
12609
851f26ae
OJ
12610/* Record handler for arm/thumb mode VFP data processing instructions. */
12611
12612static int
4748a9be 12613arm_record_vfp_data_proc_insn (arm_insn_decode_record *arm_insn_r)
851f26ae
OJ
12614{
12615 uint32_t opc1, opc2, opc3, dp_op_sz, bit_d, reg_vd;
12616 uint32_t record_buf[4];
12617 enum insn_types {INSN_T0, INSN_T1, INSN_T2, INSN_T3, INSN_INV};
12618 enum insn_types curr_insn_type = INSN_INV;
12619
12620 reg_vd = bits (arm_insn_r->arm_insn, 12, 15);
12621 opc1 = bits (arm_insn_r->arm_insn, 20, 23);
12622 opc2 = bits (arm_insn_r->arm_insn, 16, 19);
12623 opc3 = bits (arm_insn_r->arm_insn, 6, 7);
12624 dp_op_sz = bit (arm_insn_r->arm_insn, 8);
12625 bit_d = bit (arm_insn_r->arm_insn, 22);
ce887586
TT
12626 /* Mask off the "D" bit. */
12627 opc1 = opc1 & ~0x04;
851f26ae
OJ
12628
12629 /* Handle VMLA, VMLS. */
12630 if (opc1 == 0x00)
12631 {
12632 if (bit (arm_insn_r->arm_insn, 10))
dda83cd7
SM
12633 {
12634 if (bit (arm_insn_r->arm_insn, 6))
12635 curr_insn_type = INSN_T0;
12636 else
12637 curr_insn_type = INSN_T1;
12638 }
851f26ae 12639 else
dda83cd7
SM
12640 {
12641 if (dp_op_sz)
12642 curr_insn_type = INSN_T1;
12643 else
12644 curr_insn_type = INSN_T2;
12645 }
851f26ae
OJ
12646 }
12647 /* Handle VNMLA, VNMLS, VNMUL. */
12648 else if (opc1 == 0x01)
12649 {
12650 if (dp_op_sz)
dda83cd7 12651 curr_insn_type = INSN_T1;
851f26ae 12652 else
dda83cd7 12653 curr_insn_type = INSN_T2;
851f26ae
OJ
12654 }
12655 /* Handle VMUL. */
12656 else if (opc1 == 0x02 && !(opc3 & 0x01))
12657 {
12658 if (bit (arm_insn_r->arm_insn, 10))
dda83cd7
SM
12659 {
12660 if (bit (arm_insn_r->arm_insn, 6))
12661 curr_insn_type = INSN_T0;
12662 else
12663 curr_insn_type = INSN_T1;
12664 }
851f26ae 12665 else
dda83cd7
SM
12666 {
12667 if (dp_op_sz)
12668 curr_insn_type = INSN_T1;
12669 else
12670 curr_insn_type = INSN_T2;
12671 }
851f26ae
OJ
12672 }
12673 /* Handle VADD, VSUB. */
12674 else if (opc1 == 0x03)
12675 {
12676 if (!bit (arm_insn_r->arm_insn, 9))
dda83cd7
SM
12677 {
12678 if (bit (arm_insn_r->arm_insn, 6))
12679 curr_insn_type = INSN_T0;
12680 else
12681 curr_insn_type = INSN_T1;
12682 }
851f26ae 12683 else
dda83cd7
SM
12684 {
12685 if (dp_op_sz)
12686 curr_insn_type = INSN_T1;
12687 else
12688 curr_insn_type = INSN_T2;
12689 }
851f26ae
OJ
12690 }
12691 /* Handle VDIV. */
ce887586 12692 else if (opc1 == 0x08)
851f26ae
OJ
12693 {
12694 if (dp_op_sz)
dda83cd7 12695 curr_insn_type = INSN_T1;
851f26ae 12696 else
dda83cd7 12697 curr_insn_type = INSN_T2;
851f26ae
OJ
12698 }
12699 /* Handle all other vfp data processing instructions. */
12700 else if (opc1 == 0x0b)
12701 {
12702 /* Handle VMOV. */
12703 if (!(opc3 & 0x01) || (opc2 == 0x00 && opc3 == 0x01))
dda83cd7
SM
12704 {
12705 if (bit (arm_insn_r->arm_insn, 4))
12706 {
12707 if (bit (arm_insn_r->arm_insn, 6))
12708 curr_insn_type = INSN_T0;
12709 else
12710 curr_insn_type = INSN_T1;
12711 }
12712 else
12713 {
12714 if (dp_op_sz)
12715 curr_insn_type = INSN_T1;
12716 else
12717 curr_insn_type = INSN_T2;
12718 }
12719 }
851f26ae
OJ
12720 /* Handle VNEG and VABS. */
12721 else if ((opc2 == 0x01 && opc3 == 0x01)
dda83cd7
SM
12722 || (opc2 == 0x00 && opc3 == 0x03))
12723 {
12724 if (!bit (arm_insn_r->arm_insn, 11))
12725 {
12726 if (bit (arm_insn_r->arm_insn, 6))
12727 curr_insn_type = INSN_T0;
12728 else
12729 curr_insn_type = INSN_T1;
12730 }
12731 else
12732 {
12733 if (dp_op_sz)
12734 curr_insn_type = INSN_T1;
12735 else
12736 curr_insn_type = INSN_T2;
12737 }
12738 }
851f26ae
OJ
12739 /* Handle VSQRT. */
12740 else if (opc2 == 0x01 && opc3 == 0x03)
dda83cd7
SM
12741 {
12742 if (dp_op_sz)
12743 curr_insn_type = INSN_T1;
12744 else
12745 curr_insn_type = INSN_T2;
12746 }
851f26ae
OJ
12747 /* Handle VCVT. */
12748 else if (opc2 == 0x07 && opc3 == 0x03)
dda83cd7
SM
12749 {
12750 if (!dp_op_sz)
12751 curr_insn_type = INSN_T1;
12752 else
12753 curr_insn_type = INSN_T2;
12754 }
851f26ae 12755 else if (opc3 & 0x01)
dda83cd7
SM
12756 {
12757 /* Handle VCVT. */
12758 if ((opc2 == 0x08) || (opc2 & 0x0e) == 0x0c)
12759 {
12760 if (!bit (arm_insn_r->arm_insn, 18))
12761 curr_insn_type = INSN_T2;
12762 else
12763 {
12764 if (dp_op_sz)
12765 curr_insn_type = INSN_T1;
12766 else
12767 curr_insn_type = INSN_T2;
12768 }
12769 }
12770 /* Handle VCVT. */
12771 else if ((opc2 & 0x0e) == 0x0a || (opc2 & 0x0e) == 0x0e)
12772 {
12773 if (dp_op_sz)
12774 curr_insn_type = INSN_T1;
12775 else
12776 curr_insn_type = INSN_T2;
12777 }
12778 /* Handle VCVTB, VCVTT. */
12779 else if ((opc2 & 0x0e) == 0x02)
12780 curr_insn_type = INSN_T2;
12781 /* Handle VCMP, VCMPE. */
12782 else if ((opc2 & 0x0e) == 0x04)
12783 curr_insn_type = INSN_T3;
12784 }
851f26ae
OJ
12785 }
12786
12787 switch (curr_insn_type)
12788 {
12789 case INSN_T0:
dda83cd7
SM
12790 reg_vd = reg_vd | (bit_d << 4);
12791 record_buf[0] = reg_vd + ARM_D0_REGNUM;
12792 record_buf[1] = reg_vd + ARM_D0_REGNUM + 1;
12793 arm_insn_r->reg_rec_count = 2;
12794 break;
851f26ae
OJ
12795
12796 case INSN_T1:
dda83cd7
SM
12797 reg_vd = reg_vd | (bit_d << 4);
12798 record_buf[0] = reg_vd + ARM_D0_REGNUM;
12799 arm_insn_r->reg_rec_count = 1;
12800 break;
851f26ae
OJ
12801
12802 case INSN_T2:
dda83cd7
SM
12803 reg_vd = (reg_vd << 1) | bit_d;
12804 record_buf[0] = reg_vd + ARM_D0_REGNUM;
12805 arm_insn_r->reg_rec_count = 1;
12806 break;
851f26ae
OJ
12807
12808 case INSN_T3:
dda83cd7
SM
12809 record_buf[0] = ARM_FPSCR_REGNUM;
12810 arm_insn_r->reg_rec_count = 1;
12811 break;
851f26ae
OJ
12812
12813 default:
dda83cd7
SM
12814 gdb_assert_not_reached ("no decoding pattern found");
12815 break;
851f26ae
OJ
12816 }
12817
12818 REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, record_buf);
12819 return 0;
12820}
12821
60cc5e93
OJ
12822/* Handling opcode 110 insns. */
12823
12824static int
4748a9be 12825arm_record_asimd_vfp_coproc (arm_insn_decode_record *arm_insn_r)
60cc5e93 12826{
bec2ab5a 12827 uint32_t op1, op1_ebit, coproc;
60cc5e93
OJ
12828
12829 coproc = bits (arm_insn_r->arm_insn, 8, 11);
12830 op1 = bits (arm_insn_r->arm_insn, 20, 25);
12831 op1_ebit = bit (arm_insn_r->arm_insn, 20);
12832
12833 if ((coproc & 0x0e) == 0x0a)
12834 {
12835 /* Handle extension register ld/st instructions. */
12836 if (!(op1 & 0x20))
dda83cd7 12837 return arm_record_exreg_ld_st_insn (arm_insn_r);
60cc5e93
OJ
12838
12839 /* 64-bit transfers between arm core and extension registers. */
12840 if ((op1 & 0x3e) == 0x04)
dda83cd7 12841 return arm_record_exreg_ld_st_insn (arm_insn_r);
60cc5e93
OJ
12842 }
12843 else
12844 {
12845 /* Handle coprocessor ld/st instructions. */
12846 if (!(op1 & 0x3a))
dda83cd7
SM
12847 {
12848 /* Store. */
12849 if (!op1_ebit)
12850 return arm_record_unsupported_insn (arm_insn_r);
12851 else
12852 /* Load. */
12853 return arm_record_unsupported_insn (arm_insn_r);
12854 }
60cc5e93
OJ
12855
12856 /* Move to coprocessor from two arm core registers. */
12857 if (op1 == 0x4)
dda83cd7 12858 return arm_record_unsupported_insn (arm_insn_r);
60cc5e93
OJ
12859
12860 /* Move to two arm core registers from coprocessor. */
12861 if (op1 == 0x5)
dda83cd7
SM
12862 {
12863 uint32_t reg_t[2];
60cc5e93 12864
dda83cd7
SM
12865 reg_t[0] = bits (arm_insn_r->arm_insn, 12, 15);
12866 reg_t[1] = bits (arm_insn_r->arm_insn, 16, 19);
12867 arm_insn_r->reg_rec_count = 2;
60cc5e93 12868
dda83cd7
SM
12869 REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count, reg_t);
12870 return 0;
60cc5e93
OJ
12871 }
12872 }
12873 return arm_record_unsupported_insn (arm_insn_r);
12874}
12875
72508ac0
PO
12876/* Handling opcode 111 insns. */
12877
12878static int
4748a9be 12879arm_record_coproc_data_proc (arm_insn_decode_record *arm_insn_r)
72508ac0 12880{
2d9e6acb 12881 uint32_t op, op1_ebit, coproc, bits_24_25;
345bd07c 12882 arm_gdbarch_tdep *tdep
08106042 12883 = gdbarch_tdep<arm_gdbarch_tdep> (arm_insn_r->gdbarch);
72508ac0 12884 struct regcache *reg_cache = arm_insn_r->regcache;
72508ac0
PO
12885
12886 arm_insn_r->opcode = bits (arm_insn_r->arm_insn, 24, 27);
60cc5e93 12887 coproc = bits (arm_insn_r->arm_insn, 8, 11);
60cc5e93
OJ
12888 op1_ebit = bit (arm_insn_r->arm_insn, 20);
12889 op = bit (arm_insn_r->arm_insn, 4);
2d9e6acb 12890 bits_24_25 = bits (arm_insn_r->arm_insn, 24, 25);
97dfe206
OJ
12891
12892 /* Handle arm SWI/SVC system call instructions. */
2d9e6acb 12893 if (bits_24_25 == 0x3)
97dfe206
OJ
12894 {
12895 if (tdep->arm_syscall_record != NULL)
dda83cd7
SM
12896 {
12897 ULONGEST svc_operand, svc_number;
97dfe206 12898
dda83cd7 12899 svc_operand = (0x00ffffff & arm_insn_r->arm_insn);
97dfe206 12900
dda83cd7
SM
12901 if (svc_operand) /* OABI. */
12902 svc_number = svc_operand - 0x900000;
12903 else /* EABI. */
12904 regcache_raw_read_unsigned (reg_cache, 7, &svc_number);
97dfe206 12905
dda83cd7
SM
12906 return tdep->arm_syscall_record (reg_cache, svc_number);
12907 }
97dfe206 12908 else
dda83cd7 12909 {
6cb06a8c 12910 gdb_printf (gdb_stderr, _("no syscall record support\n"));
dda83cd7
SM
12911 return -1;
12912 }
97dfe206 12913 }
2d9e6acb 12914 else if (bits_24_25 == 0x02)
60cc5e93 12915 {
2d9e6acb
YQ
12916 if (op)
12917 {
12918 if ((coproc & 0x0e) == 0x0a)
12919 {
12920 /* 8, 16, and 32-bit transfer */
12921 return arm_record_vdata_transfer_insn (arm_insn_r);
12922 }
12923 else
12924 {
12925 if (op1_ebit)
12926 {
12927 /* MRC, MRC2 */
12928 uint32_t record_buf[1];
12929
12930 record_buf[0] = bits (arm_insn_r->arm_insn, 12, 15);
12931 if (record_buf[0] == 15)
12932 record_buf[0] = ARM_PS_REGNUM;
60cc5e93 12933
2d9e6acb
YQ
12934 arm_insn_r->reg_rec_count = 1;
12935 REG_ALLOC (arm_insn_r->arm_regs, arm_insn_r->reg_rec_count,
12936 record_buf);
12937 return 0;
12938 }
12939 else
12940 {
12941 /* MCR, MCR2 */
12942 return -1;
12943 }
12944 }
12945 }
12946 else
12947 {
12948 if ((coproc & 0x0e) == 0x0a)
12949 {
12950 /* VFP data-processing instructions. */
12951 return arm_record_vfp_data_proc_insn (arm_insn_r);
12952 }
12953 else
12954 {
12955 /* CDP, CDP2 */
12956 return -1;
12957 }
12958 }
60cc5e93 12959 }
97dfe206
OJ
12960 else
12961 {
2d9e6acb 12962 unsigned int op1 = bits (arm_insn_r->arm_insn, 20, 25);
60cc5e93 12963
2d9e6acb
YQ
12964 if (op1 == 5)
12965 {
12966 if ((coproc & 0x0e) != 0x0a)
12967 {
12968 /* MRRC, MRRC2 */
12969 return -1;
12970 }
12971 }
12972 else if (op1 == 4 || op1 == 5)
12973 {
12974 if ((coproc & 0x0e) == 0x0a)
12975 {
12976 /* 64-bit transfers between ARM core and extension */
12977 return -1;
12978 }
12979 else if (op1 == 4)
12980 {
12981 /* MCRR, MCRR2 */
12982 return -1;
12983 }
12984 }
12985 else if (op1 == 0 || op1 == 1)
12986 {
12987 /* UNDEFINED */
12988 return -1;
12989 }
12990 else
12991 {
12992 if ((coproc & 0x0e) == 0x0a)
12993 {
12994 /* Extension register load/store */
12995 }
12996 else
12997 {
12998 /* STC, STC2, LDC, LDC2 */
12999 }
13000 return -1;
13001 }
97dfe206 13002 }
72508ac0 13003
2d9e6acb 13004 return -1;
72508ac0
PO
13005}
13006
13007/* Handling opcode 000 insns. */
13008
13009static int
4748a9be 13010thumb_record_shift_add_sub (arm_insn_decode_record *thumb_insn_r)
72508ac0
PO
13011{
13012 uint32_t record_buf[8];
13013 uint32_t reg_src1 = 0;
13014
13015 reg_src1 = bits (thumb_insn_r->arm_insn, 0, 2);
13016
13017 record_buf[0] = ARM_PS_REGNUM;
13018 record_buf[1] = reg_src1;
13019 thumb_insn_r->reg_rec_count = 2;
13020
13021 REG_ALLOC (thumb_insn_r->arm_regs, thumb_insn_r->reg_rec_count, record_buf);
13022
13023 return 0;
13024}
13025
13026
13027/* Handling opcode 001 insns. */
13028
13029static int
4748a9be 13030thumb_record_add_sub_cmp_mov (arm_insn_decode_record *thumb_insn_r)
72508ac0
PO
13031{
13032 uint32_t record_buf[8];
13033 uint32_t reg_src1 = 0;
13034
13035 reg_src1 = bits (thumb_insn_r->arm_insn, 8, 10);
13036
13037 record_buf[0] = ARM_PS_REGNUM;
13038 record_buf[1] = reg_src1;
13039 thumb_insn_r->reg_rec_count = 2;
13040
13041 REG_ALLOC (thumb_insn_r->arm_regs, thumb_insn_r->reg_rec_count, record_buf);
13042
13043 return 0;
13044}
13045
13046/* Handling opcode 010 insns. */
13047
13048static int
4748a9be 13049thumb_record_ld_st_reg_offset (arm_insn_decode_record *thumb_insn_r)
72508ac0
PO
13050{
13051 struct regcache *reg_cache = thumb_insn_r->regcache;
13052 uint32_t record_buf[8], record_buf_mem[8];
13053
13054 uint32_t reg_src1 = 0, reg_src2 = 0;
13055 uint32_t opcode1 = 0, opcode2 = 0, opcode3 = 0;
13056
13057 ULONGEST u_regval[2] = {0};
13058
13059 opcode1 = bits (thumb_insn_r->arm_insn, 10, 12);
13060
13061 if (bit (thumb_insn_r->arm_insn, 12))
13062 {
13063 /* Handle load/store register offset. */
b121eeb9
YQ
13064 uint32_t opB = bits (thumb_insn_r->arm_insn, 9, 11);
13065
b020ff80 13066 if (in_inclusive_range (opB, 4U, 7U))
dda83cd7
SM
13067 {
13068 /* LDR(2), LDRB(2) , LDRH(2), LDRSB, LDRSH. */
13069 reg_src1 = bits (thumb_insn_r->arm_insn,0, 2);
13070 record_buf[0] = reg_src1;
13071 thumb_insn_r->reg_rec_count = 1;
13072 }
b020ff80 13073 else if (in_inclusive_range (opB, 0U, 2U))
dda83cd7
SM
13074 {
13075 /* STR(2), STRB(2), STRH(2) . */
13076 reg_src1 = bits (thumb_insn_r->arm_insn, 3, 5);
13077 reg_src2 = bits (thumb_insn_r->arm_insn, 6, 8);
13078 regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval[0]);
13079 regcache_raw_read_unsigned (reg_cache, reg_src2, &u_regval[1]);
13080 if (0 == opB)
13081 record_buf_mem[0] = 4; /* STR (2). */
13082 else if (2 == opB)
13083 record_buf_mem[0] = 1; /* STRB (2). */
13084 else if (1 == opB)
13085 record_buf_mem[0] = 2; /* STRH (2). */
13086 record_buf_mem[1] = u_regval[0] + u_regval[1];
13087 thumb_insn_r->mem_rec_count = 1;
13088 }
72508ac0
PO
13089 }
13090 else if (bit (thumb_insn_r->arm_insn, 11))
13091 {
13092 /* Handle load from literal pool. */
13093 /* LDR(3). */
13094 reg_src1 = bits (thumb_insn_r->arm_insn, 8, 10);
13095 record_buf[0] = reg_src1;
13096 thumb_insn_r->reg_rec_count = 1;
13097 }
13098 else if (opcode1)
13099 {
b121eeb9 13100 /* Special data instructions and branch and exchange */
72508ac0
PO
13101 opcode2 = bits (thumb_insn_r->arm_insn, 8, 9);
13102 opcode3 = bits (thumb_insn_r->arm_insn, 0, 2);
13103 if ((3 == opcode2) && (!opcode3))
dda83cd7
SM
13104 {
13105 /* Branch with exchange. */
13106 record_buf[0] = ARM_PS_REGNUM;
13107 thumb_insn_r->reg_rec_count = 1;
13108 }
72508ac0 13109 else
dda83cd7 13110 {
1f33efec
YQ
13111 /* Format 8; special data processing insns. */
13112 record_buf[0] = ARM_PS_REGNUM;
13113 record_buf[1] = (bit (thumb_insn_r->arm_insn, 7) << 3
13114 | bits (thumb_insn_r->arm_insn, 0, 2));
dda83cd7
SM
13115 thumb_insn_r->reg_rec_count = 2;
13116 }
72508ac0
PO
13117 }
13118 else
13119 {
13120 /* Format 5; data processing insns. */
13121 reg_src1 = bits (thumb_insn_r->arm_insn, 0, 2);
13122 if (bit (thumb_insn_r->arm_insn, 7))
dda83cd7
SM
13123 {
13124 reg_src1 = reg_src1 + 8;
13125 }
72508ac0
PO
13126 record_buf[0] = ARM_PS_REGNUM;
13127 record_buf[1] = reg_src1;
13128 thumb_insn_r->reg_rec_count = 2;
13129 }
13130
13131 REG_ALLOC (thumb_insn_r->arm_regs, thumb_insn_r->reg_rec_count, record_buf);
13132 MEM_ALLOC (thumb_insn_r->arm_mems, thumb_insn_r->mem_rec_count,
dda83cd7 13133 record_buf_mem);
72508ac0
PO
13134
13135 return 0;
13136}
13137
13138/* Handling opcode 001 insns. */
13139
13140static int
4748a9be 13141thumb_record_ld_st_imm_offset (arm_insn_decode_record *thumb_insn_r)
72508ac0
PO
13142{
13143 struct regcache *reg_cache = thumb_insn_r->regcache;
13144 uint32_t record_buf[8], record_buf_mem[8];
13145
13146 uint32_t reg_src1 = 0;
13147 uint32_t opcode = 0, immed_5 = 0;
13148
13149 ULONGEST u_regval = 0;
13150
13151 opcode = bits (thumb_insn_r->arm_insn, 11, 12);
13152
13153 if (opcode)
13154 {
13155 /* LDR(1). */
13156 reg_src1 = bits (thumb_insn_r->arm_insn, 0, 2);
13157 record_buf[0] = reg_src1;
13158 thumb_insn_r->reg_rec_count = 1;
13159 }
13160 else
13161 {
13162 /* STR(1). */
13163 reg_src1 = bits (thumb_insn_r->arm_insn, 3, 5);
13164 immed_5 = bits (thumb_insn_r->arm_insn, 6, 10);
13165 regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval);
13166 record_buf_mem[0] = 4;
13167 record_buf_mem[1] = u_regval + (immed_5 * 4);
13168 thumb_insn_r->mem_rec_count = 1;
13169 }
13170
13171 REG_ALLOC (thumb_insn_r->arm_regs, thumb_insn_r->reg_rec_count, record_buf);
13172 MEM_ALLOC (thumb_insn_r->arm_mems, thumb_insn_r->mem_rec_count,
dda83cd7 13173 record_buf_mem);
72508ac0
PO
13174
13175 return 0;
13176}
13177
13178/* Handling opcode 100 insns. */
13179
13180static int
4748a9be 13181thumb_record_ld_st_stack (arm_insn_decode_record *thumb_insn_r)
72508ac0
PO
13182{
13183 struct regcache *reg_cache = thumb_insn_r->regcache;
13184 uint32_t record_buf[8], record_buf_mem[8];
13185
13186 uint32_t reg_src1 = 0;
13187 uint32_t opcode = 0, immed_8 = 0, immed_5 = 0;
13188
13189 ULONGEST u_regval = 0;
13190
13191 opcode = bits (thumb_insn_r->arm_insn, 11, 12);
13192
13193 if (3 == opcode)
13194 {
13195 /* LDR(4). */
13196 reg_src1 = bits (thumb_insn_r->arm_insn, 8, 10);
13197 record_buf[0] = reg_src1;
13198 thumb_insn_r->reg_rec_count = 1;
13199 }
13200 else if (1 == opcode)
13201 {
13202 /* LDRH(1). */
13203 reg_src1 = bits (thumb_insn_r->arm_insn, 0, 2);
13204 record_buf[0] = reg_src1;
13205 thumb_insn_r->reg_rec_count = 1;
13206 }
13207 else if (2 == opcode)
13208 {
13209 /* STR(3). */
13210 immed_8 = bits (thumb_insn_r->arm_insn, 0, 7);
13211 regcache_raw_read_unsigned (reg_cache, ARM_SP_REGNUM, &u_regval);
13212 record_buf_mem[0] = 4;
13213 record_buf_mem[1] = u_regval + (immed_8 * 4);
13214 thumb_insn_r->mem_rec_count = 1;
13215 }
13216 else if (0 == opcode)
13217 {
13218 /* STRH(1). */
13219 immed_5 = bits (thumb_insn_r->arm_insn, 6, 10);
13220 reg_src1 = bits (thumb_insn_r->arm_insn, 3, 5);
13221 regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval);
13222 record_buf_mem[0] = 2;
13223 record_buf_mem[1] = u_regval + (immed_5 * 2);
13224 thumb_insn_r->mem_rec_count = 1;
13225 }
13226
13227 REG_ALLOC (thumb_insn_r->arm_regs, thumb_insn_r->reg_rec_count, record_buf);
13228 MEM_ALLOC (thumb_insn_r->arm_mems, thumb_insn_r->mem_rec_count,
dda83cd7 13229 record_buf_mem);
72508ac0
PO
13230
13231 return 0;
13232}
13233
13234/* Handling opcode 101 insns. */
13235
13236static int
4748a9be 13237thumb_record_misc (arm_insn_decode_record *thumb_insn_r)
72508ac0
PO
13238{
13239 struct regcache *reg_cache = thumb_insn_r->regcache;
13240
b121eeb9 13241 uint32_t opcode = 0;
72508ac0 13242 uint32_t register_bits = 0, register_count = 0;
bec2ab5a 13243 uint32_t index = 0, start_address = 0;
72508ac0
PO
13244 uint32_t record_buf[24], record_buf_mem[48];
13245 uint32_t reg_src1;
13246
13247 ULONGEST u_regval = 0;
13248
13249 opcode = bits (thumb_insn_r->arm_insn, 11, 12);
72508ac0 13250
b121eeb9 13251 if (opcode == 0 || opcode == 1)
72508ac0 13252 {
b121eeb9
YQ
13253 /* ADR and ADD (SP plus immediate) */
13254
72508ac0
PO
13255 reg_src1 = bits (thumb_insn_r->arm_insn, 8, 10);
13256 record_buf[0] = reg_src1;
13257 thumb_insn_r->reg_rec_count = 1;
13258 }
b121eeb9 13259 else
72508ac0 13260 {
b121eeb9
YQ
13261 /* Miscellaneous 16-bit instructions */
13262 uint32_t opcode2 = bits (thumb_insn_r->arm_insn, 8, 11);
13263
13264 switch (opcode2)
13265 {
13266 case 6:
13267 /* SETEND and CPS */
13268 break;
13269 case 0:
13270 /* ADD/SUB (SP plus immediate) */
13271 reg_src1 = bits (thumb_insn_r->arm_insn, 8, 10);
13272 record_buf[0] = ARM_SP_REGNUM;
13273 thumb_insn_r->reg_rec_count = 1;
13274 break;
13275 case 1: /* fall through */
13276 case 3: /* fall through */
13277 case 9: /* fall through */
13278 case 11:
13279 /* CBNZ, CBZ */
b121eeb9
YQ
13280 break;
13281 case 2:
13282 /* SXTH, SXTB, UXTH, UXTB */
13283 record_buf[0] = bits (thumb_insn_r->arm_insn, 0, 2);
13284 thumb_insn_r->reg_rec_count = 1;
13285 break;
13286 case 4: /* fall through */
13287 case 5:
13288 /* PUSH. */
13289 register_bits = bits (thumb_insn_r->arm_insn, 0, 7);
13290 regcache_raw_read_unsigned (reg_cache, ARM_SP_REGNUM, &u_regval);
13291 while (register_bits)
13292 {
13293 if (register_bits & 0x00000001)
13294 register_count++;
13295 register_bits = register_bits >> 1;
13296 }
13297 start_address = u_regval - \
13298 (4 * (bit (thumb_insn_r->arm_insn, 8) + register_count));
13299 thumb_insn_r->mem_rec_count = register_count;
13300 while (register_count)
13301 {
13302 record_buf_mem[(register_count * 2) - 1] = start_address;
13303 record_buf_mem[(register_count * 2) - 2] = 4;
13304 start_address = start_address + 4;
13305 register_count--;
13306 }
13307 record_buf[0] = ARM_SP_REGNUM;
13308 thumb_insn_r->reg_rec_count = 1;
13309 break;
13310 case 10:
13311 /* REV, REV16, REVSH */
ba14f379
YQ
13312 record_buf[0] = bits (thumb_insn_r->arm_insn, 0, 2);
13313 thumb_insn_r->reg_rec_count = 1;
b121eeb9
YQ
13314 break;
13315 case 12: /* fall through */
13316 case 13:
13317 /* POP. */
13318 register_bits = bits (thumb_insn_r->arm_insn, 0, 7);
13319 while (register_bits)
13320 {
13321 if (register_bits & 0x00000001)
13322 record_buf[index++] = register_count;
13323 register_bits = register_bits >> 1;
13324 register_count++;
13325 }
13326 record_buf[index++] = ARM_PS_REGNUM;
13327 record_buf[index++] = ARM_SP_REGNUM;
13328 thumb_insn_r->reg_rec_count = index;
13329 break;
13330 case 0xe:
13331 /* BKPT insn. */
13332 /* Handle enhanced software breakpoint insn, BKPT. */
13333 /* CPSR is changed to be executed in ARM state, disabling normal
13334 interrupts, entering abort mode. */
13335 /* According to high vector configuration PC is set. */
13336 /* User hits breakpoint and type reverse, in that case, we need to go back with
13337 previous CPSR and Program Counter. */
13338 record_buf[0] = ARM_PS_REGNUM;
13339 record_buf[1] = ARM_LR_REGNUM;
13340 thumb_insn_r->reg_rec_count = 2;
13341 /* We need to save SPSR value, which is not yet done. */
6cb06a8c
TT
13342 gdb_printf (gdb_stderr,
13343 _("Process record does not support instruction "
13344 "0x%0x at address %s.\n"),
13345 thumb_insn_r->arm_insn,
13346 paddress (thumb_insn_r->gdbarch,
13347 thumb_insn_r->this_addr));
b121eeb9
YQ
13348 return -1;
13349
13350 case 0xf:
13351 /* If-Then, and hints */
13352 break;
13353 default:
13354 return -1;
13355 };
72508ac0
PO
13356 }
13357
13358 REG_ALLOC (thumb_insn_r->arm_regs, thumb_insn_r->reg_rec_count, record_buf);
13359 MEM_ALLOC (thumb_insn_r->arm_mems, thumb_insn_r->mem_rec_count,
dda83cd7 13360 record_buf_mem);
72508ac0
PO
13361
13362 return 0;
13363}
13364
13365/* Handling opcode 110 insns. */
13366
13367static int
4748a9be 13368thumb_record_ldm_stm_swi (arm_insn_decode_record *thumb_insn_r)
72508ac0 13369{
345bd07c 13370 arm_gdbarch_tdep *tdep
08106042 13371 = gdbarch_tdep<arm_gdbarch_tdep> (thumb_insn_r->gdbarch);
72508ac0
PO
13372 struct regcache *reg_cache = thumb_insn_r->regcache;
13373
13374 uint32_t ret = 0; /* function return value: -1:record failure ; 0:success */
13375 uint32_t reg_src1 = 0;
13376 uint32_t opcode1 = 0, opcode2 = 0, register_bits = 0, register_count = 0;
bec2ab5a 13377 uint32_t index = 0, start_address = 0;
72508ac0
PO
13378 uint32_t record_buf[24], record_buf_mem[48];
13379
13380 ULONGEST u_regval = 0;
13381
13382 opcode1 = bits (thumb_insn_r->arm_insn, 8, 12);
13383 opcode2 = bits (thumb_insn_r->arm_insn, 11, 12);
13384
13385 if (1 == opcode2)
13386 {
13387
13388 /* LDMIA. */
13389 register_bits = bits (thumb_insn_r->arm_insn, 0, 7);
13390 /* Get Rn. */
13391 reg_src1 = bits (thumb_insn_r->arm_insn, 8, 10);
13392 while (register_bits)
dda83cd7
SM
13393 {
13394 if (register_bits & 0x00000001)
13395 record_buf[index++] = register_count;
13396 register_bits = register_bits >> 1;
13397 register_count++;
13398 }
f969241e
OJ
13399 record_buf[index++] = reg_src1;
13400 thumb_insn_r->reg_rec_count = index;
72508ac0
PO
13401 }
13402 else if (0 == opcode2)
13403 {
13404 /* It handles both STMIA. */
13405 register_bits = bits (thumb_insn_r->arm_insn, 0, 7);
13406 /* Get Rn. */
13407 reg_src1 = bits (thumb_insn_r->arm_insn, 8, 10);
13408 regcache_raw_read_unsigned (reg_cache, reg_src1, &u_regval);
13409 while (register_bits)
dda83cd7
SM
13410 {
13411 if (register_bits & 0x00000001)
13412 register_count++;
13413 register_bits = register_bits >> 1;
13414 }
72508ac0
PO
13415 start_address = u_regval;
13416 thumb_insn_r->mem_rec_count = register_count;
13417 while (register_count)
dda83cd7
SM
13418 {
13419 record_buf_mem[(register_count * 2) - 1] = start_address;
13420 record_buf_mem[(register_count * 2) - 2] = 4;
13421 start_address = start_address + 4;
13422 register_count--;
13423 }
72508ac0
PO
13424 }
13425 else if (0x1F == opcode1)
13426 {
dda83cd7
SM
13427 /* Handle arm syscall insn. */
13428 if (tdep->arm_syscall_record != NULL)
13429 {
13430 regcache_raw_read_unsigned (reg_cache, 7, &u_regval);
13431 ret = tdep->arm_syscall_record (reg_cache, u_regval);
13432 }
13433 else
13434 {
6cb06a8c 13435 gdb_printf (gdb_stderr, _("no syscall record support\n"));
dda83cd7
SM
13436 return -1;
13437 }
72508ac0
PO
13438 }
13439
13440 /* B (1), conditional branch is automatically taken care in process_record,
13441 as PC is saved there. */
13442
13443 REG_ALLOC (thumb_insn_r->arm_regs, thumb_insn_r->reg_rec_count, record_buf);
13444 MEM_ALLOC (thumb_insn_r->arm_mems, thumb_insn_r->mem_rec_count,
dda83cd7 13445 record_buf_mem);
72508ac0
PO
13446
13447 return ret;
13448}
13449
13450/* Handling opcode 111 insns. */
13451
13452static int
4748a9be 13453thumb_record_branch (arm_insn_decode_record *thumb_insn_r)
72508ac0
PO
13454{
13455 uint32_t record_buf[8];
13456 uint32_t bits_h = 0;
13457
13458 bits_h = bits (thumb_insn_r->arm_insn, 11, 12);
13459
13460 if (2 == bits_h || 3 == bits_h)
13461 {
13462 /* BL */
13463 record_buf[0] = ARM_LR_REGNUM;
13464 thumb_insn_r->reg_rec_count = 1;
13465 }
13466 else if (1 == bits_h)
13467 {
13468 /* BLX(1). */
13469 record_buf[0] = ARM_PS_REGNUM;
13470 record_buf[1] = ARM_LR_REGNUM;
13471 thumb_insn_r->reg_rec_count = 2;
13472 }
13473
13474 /* B(2) is automatically taken care in process_record, as PC is
13475 saved there. */
13476
13477 REG_ALLOC (thumb_insn_r->arm_regs, thumb_insn_r->reg_rec_count, record_buf);
13478
13479 return 0;
13480}
13481
c6ec2b30
OJ
13482/* Handler for thumb2 load/store multiple instructions. */
13483
13484static int
4748a9be 13485thumb2_record_ld_st_multiple (arm_insn_decode_record *thumb2_insn_r)
c6ec2b30
OJ
13486{
13487 struct regcache *reg_cache = thumb2_insn_r->regcache;
13488
13489 uint32_t reg_rn, op;
13490 uint32_t register_bits = 0, register_count = 0;
13491 uint32_t index = 0, start_address = 0;
13492 uint32_t record_buf[24], record_buf_mem[48];
13493
13494 ULONGEST u_regval = 0;
13495
13496 reg_rn = bits (thumb2_insn_r->arm_insn, 16, 19);
13497 op = bits (thumb2_insn_r->arm_insn, 23, 24);
13498
13499 if (0 == op || 3 == op)
13500 {
13501 if (bit (thumb2_insn_r->arm_insn, INSN_S_L_BIT_NUM))
dda83cd7
SM
13502 {
13503 /* Handle RFE instruction. */
13504 record_buf[0] = ARM_PS_REGNUM;
13505 thumb2_insn_r->reg_rec_count = 1;
13506 }
c6ec2b30 13507 else
dda83cd7
SM
13508 {
13509 /* Handle SRS instruction after reading banked SP. */
13510 return arm_record_unsupported_insn (thumb2_insn_r);
13511 }
c6ec2b30
OJ
13512 }
13513 else if (1 == op || 2 == op)
13514 {
13515 if (bit (thumb2_insn_r->arm_insn, INSN_S_L_BIT_NUM))
dda83cd7
SM
13516 {
13517 /* Handle LDM/LDMIA/LDMFD and LDMDB/LDMEA instructions. */
13518 register_bits = bits (thumb2_insn_r->arm_insn, 0, 15);
13519 while (register_bits)
13520 {
13521 if (register_bits & 0x00000001)
13522 record_buf[index++] = register_count;
13523
13524 register_count++;
13525 register_bits = register_bits >> 1;
13526 }
13527 record_buf[index++] = reg_rn;
13528 record_buf[index++] = ARM_PS_REGNUM;
13529 thumb2_insn_r->reg_rec_count = index;
13530 }
c6ec2b30 13531 else
dda83cd7
SM
13532 {
13533 /* Handle STM/STMIA/STMEA and STMDB/STMFD. */
13534 register_bits = bits (thumb2_insn_r->arm_insn, 0, 15);
13535 regcache_raw_read_unsigned (reg_cache, reg_rn, &u_regval);
13536 while (register_bits)
13537 {
13538 if (register_bits & 0x00000001)
13539 register_count++;
13540
13541 register_bits = register_bits >> 1;
13542 }
13543
13544 if (1 == op)
13545 {
13546 /* Start address calculation for LDMDB/LDMEA. */
13547 start_address = u_regval;
13548 }
13549 else if (2 == op)
13550 {
13551 /* Start address calculation for LDMDB/LDMEA. */
13552 start_address = u_regval - register_count * 4;
13553 }
13554
13555 thumb2_insn_r->mem_rec_count = register_count;
13556 while (register_count)
13557 {
13558 record_buf_mem[register_count * 2 - 1] = start_address;
13559 record_buf_mem[register_count * 2 - 2] = 4;
13560 start_address = start_address + 4;
13561 register_count--;
13562 }
13563 record_buf[0] = reg_rn;
13564 record_buf[1] = ARM_PS_REGNUM;
13565 thumb2_insn_r->reg_rec_count = 2;
13566 }
c6ec2b30
OJ
13567 }
13568
13569 MEM_ALLOC (thumb2_insn_r->arm_mems, thumb2_insn_r->mem_rec_count,
dda83cd7 13570 record_buf_mem);
c6ec2b30 13571 REG_ALLOC (thumb2_insn_r->arm_regs, thumb2_insn_r->reg_rec_count,
dda83cd7 13572 record_buf);
c6ec2b30
OJ
13573 return ARM_RECORD_SUCCESS;
13574}
13575
13576/* Handler for thumb2 load/store (dual/exclusive) and table branch
13577 instructions. */
13578
13579static int
4748a9be 13580thumb2_record_ld_st_dual_ex_tbb (arm_insn_decode_record *thumb2_insn_r)
c6ec2b30
OJ
13581{
13582 struct regcache *reg_cache = thumb2_insn_r->regcache;
13583
13584 uint32_t reg_rd, reg_rn, offset_imm;
13585 uint32_t reg_dest1, reg_dest2;
13586 uint32_t address, offset_addr;
13587 uint32_t record_buf[8], record_buf_mem[8];
13588 uint32_t op1, op2, op3;
c6ec2b30
OJ
13589
13590 ULONGEST u_regval[2];
13591
13592 op1 = bits (thumb2_insn_r->arm_insn, 23, 24);
13593 op2 = bits (thumb2_insn_r->arm_insn, 20, 21);
13594 op3 = bits (thumb2_insn_r->arm_insn, 4, 7);
13595
13596 if (bit (thumb2_insn_r->arm_insn, INSN_S_L_BIT_NUM))
13597 {
13598 if(!(1 == op1 && 1 == op2 && (0 == op3 || 1 == op3)))
dda83cd7
SM
13599 {
13600 reg_dest1 = bits (thumb2_insn_r->arm_insn, 12, 15);
13601 record_buf[0] = reg_dest1;
13602 record_buf[1] = ARM_PS_REGNUM;
13603 thumb2_insn_r->reg_rec_count = 2;
13604 }
c6ec2b30
OJ
13605
13606 if (3 == op2 || (op1 & 2) || (1 == op1 && 1 == op2 && 7 == op3))
dda83cd7
SM
13607 {
13608 reg_dest2 = bits (thumb2_insn_r->arm_insn, 8, 11);
13609 record_buf[2] = reg_dest2;
13610 thumb2_insn_r->reg_rec_count = 3;
13611 }
c6ec2b30
OJ
13612 }
13613 else
13614 {
13615 reg_rn = bits (thumb2_insn_r->arm_insn, 16, 19);
13616 regcache_raw_read_unsigned (reg_cache, reg_rn, &u_regval[0]);
13617
13618 if (0 == op1 && 0 == op2)
dda83cd7
SM
13619 {
13620 /* Handle STREX. */
13621 offset_imm = bits (thumb2_insn_r->arm_insn, 0, 7);
13622 address = u_regval[0] + (offset_imm * 4);
13623 record_buf_mem[0] = 4;
13624 record_buf_mem[1] = address;
13625 thumb2_insn_r->mem_rec_count = 1;
13626 reg_rd = bits (thumb2_insn_r->arm_insn, 0, 3);
13627 record_buf[0] = reg_rd;
13628 thumb2_insn_r->reg_rec_count = 1;
13629 }
c6ec2b30 13630 else if (1 == op1 && 0 == op2)
dda83cd7
SM
13631 {
13632 reg_rd = bits (thumb2_insn_r->arm_insn, 0, 3);
13633 record_buf[0] = reg_rd;
13634 thumb2_insn_r->reg_rec_count = 1;
13635 address = u_regval[0];
13636 record_buf_mem[1] = address;
13637
13638 if (4 == op3)
13639 {
13640 /* Handle STREXB. */
13641 record_buf_mem[0] = 1;
13642 thumb2_insn_r->mem_rec_count = 1;
13643 }
13644 else if (5 == op3)
13645 {
13646 /* Handle STREXH. */
13647 record_buf_mem[0] = 2 ;
13648 thumb2_insn_r->mem_rec_count = 1;
13649 }
13650 else if (7 == op3)
13651 {
13652 /* Handle STREXD. */
13653 address = u_regval[0];
13654 record_buf_mem[0] = 4;
13655 record_buf_mem[2] = 4;
13656 record_buf_mem[3] = address + 4;
13657 thumb2_insn_r->mem_rec_count = 2;
13658 }
13659 }
c6ec2b30 13660 else
dda83cd7
SM
13661 {
13662 offset_imm = bits (thumb2_insn_r->arm_insn, 0, 7);
13663
13664 if (bit (thumb2_insn_r->arm_insn, 24))
13665 {
13666 if (bit (thumb2_insn_r->arm_insn, 23))
13667 offset_addr = u_regval[0] + (offset_imm * 4);
13668 else
13669 offset_addr = u_regval[0] - (offset_imm * 4);
13670
13671 address = offset_addr;
13672 }
13673 else
13674 address = u_regval[0];
13675
13676 record_buf_mem[0] = 4;
13677 record_buf_mem[1] = address;
13678 record_buf_mem[2] = 4;
13679 record_buf_mem[3] = address + 4;
13680 thumb2_insn_r->mem_rec_count = 2;
13681 record_buf[0] = reg_rn;
13682 thumb2_insn_r->reg_rec_count = 1;
13683 }
c6ec2b30
OJ
13684 }
13685
13686 REG_ALLOC (thumb2_insn_r->arm_regs, thumb2_insn_r->reg_rec_count,
dda83cd7 13687 record_buf);
c6ec2b30 13688 MEM_ALLOC (thumb2_insn_r->arm_mems, thumb2_insn_r->mem_rec_count,
dda83cd7 13689 record_buf_mem);
c6ec2b30
OJ
13690 return ARM_RECORD_SUCCESS;
13691}
13692
13693/* Handler for thumb2 data processing (shift register and modified immediate)
13694 instructions. */
13695
13696static int
4748a9be 13697thumb2_record_data_proc_sreg_mimm (arm_insn_decode_record *thumb2_insn_r)
c6ec2b30
OJ
13698{
13699 uint32_t reg_rd, op;
13700 uint32_t record_buf[8];
13701
13702 op = bits (thumb2_insn_r->arm_insn, 21, 24);
13703 reg_rd = bits (thumb2_insn_r->arm_insn, 8, 11);
13704
13705 if ((0 == op || 4 == op || 8 == op || 13 == op) && 15 == reg_rd)
13706 {
13707 record_buf[0] = ARM_PS_REGNUM;
13708 thumb2_insn_r->reg_rec_count = 1;
13709 }
13710 else
13711 {
13712 record_buf[0] = reg_rd;
13713 record_buf[1] = ARM_PS_REGNUM;
13714 thumb2_insn_r->reg_rec_count = 2;
13715 }
13716
13717 REG_ALLOC (thumb2_insn_r->arm_regs, thumb2_insn_r->reg_rec_count,
dda83cd7 13718 record_buf);
c6ec2b30
OJ
13719 return ARM_RECORD_SUCCESS;
13720}
13721
13722/* Generic handler for thumb2 instructions which effect destination and PS
13723 registers. */
13724
13725static int
4748a9be 13726thumb2_record_ps_dest_generic (arm_insn_decode_record *thumb2_insn_r)
c6ec2b30
OJ
13727{
13728 uint32_t reg_rd;
13729 uint32_t record_buf[8];
13730
13731 reg_rd = bits (thumb2_insn_r->arm_insn, 8, 11);
13732
13733 record_buf[0] = reg_rd;
13734 record_buf[1] = ARM_PS_REGNUM;
13735 thumb2_insn_r->reg_rec_count = 2;
13736
13737 REG_ALLOC (thumb2_insn_r->arm_regs, thumb2_insn_r->reg_rec_count,
dda83cd7 13738 record_buf);
c6ec2b30
OJ
13739 return ARM_RECORD_SUCCESS;
13740}
13741
13742/* Handler for thumb2 branch and miscellaneous control instructions. */
13743
13744static int
4748a9be 13745thumb2_record_branch_misc_cntrl (arm_insn_decode_record *thumb2_insn_r)
c6ec2b30
OJ
13746{
13747 uint32_t op, op1, op2;
13748 uint32_t record_buf[8];
13749
13750 op = bits (thumb2_insn_r->arm_insn, 20, 26);
13751 op1 = bits (thumb2_insn_r->arm_insn, 12, 14);
13752 op2 = bits (thumb2_insn_r->arm_insn, 8, 11);
13753
13754 /* Handle MSR insn. */
13755 if (!(op1 & 0x2) && 0x38 == op)
13756 {
13757 if (!(op2 & 0x3))
dda83cd7
SM
13758 {
13759 /* CPSR is going to be changed. */
13760 record_buf[0] = ARM_PS_REGNUM;
13761 thumb2_insn_r->reg_rec_count = 1;
13762 }
c6ec2b30 13763 else
dda83cd7
SM
13764 {
13765 arm_record_unsupported_insn(thumb2_insn_r);
13766 return -1;
13767 }
c6ec2b30
OJ
13768 }
13769 else if (4 == (op1 & 0x5) || 5 == (op1 & 0x5))
13770 {
13771 /* BLX. */
13772 record_buf[0] = ARM_PS_REGNUM;
13773 record_buf[1] = ARM_LR_REGNUM;
13774 thumb2_insn_r->reg_rec_count = 2;
13775 }
13776
13777 REG_ALLOC (thumb2_insn_r->arm_regs, thumb2_insn_r->reg_rec_count,
dda83cd7 13778 record_buf);
c6ec2b30
OJ
13779 return ARM_RECORD_SUCCESS;
13780}
13781
13782/* Handler for thumb2 store single data item instructions. */
13783
13784static int
4748a9be 13785thumb2_record_str_single_data (arm_insn_decode_record *thumb2_insn_r)
c6ec2b30
OJ
13786{
13787 struct regcache *reg_cache = thumb2_insn_r->regcache;
13788
13789 uint32_t reg_rn, reg_rm, offset_imm, shift_imm;
13790 uint32_t address, offset_addr;
13791 uint32_t record_buf[8], record_buf_mem[8];
13792 uint32_t op1, op2;
13793
13794 ULONGEST u_regval[2];
13795
13796 op1 = bits (thumb2_insn_r->arm_insn, 21, 23);
13797 op2 = bits (thumb2_insn_r->arm_insn, 6, 11);
13798 reg_rn = bits (thumb2_insn_r->arm_insn, 16, 19);
13799 regcache_raw_read_unsigned (reg_cache, reg_rn, &u_regval[0]);
13800
13801 if (bit (thumb2_insn_r->arm_insn, 23))
13802 {
13803 /* T2 encoding. */
13804 offset_imm = bits (thumb2_insn_r->arm_insn, 0, 11);
13805 offset_addr = u_regval[0] + offset_imm;
13806 address = offset_addr;
13807 }
13808 else
13809 {
13810 /* T3 encoding. */
13811 if ((0 == op1 || 1 == op1 || 2 == op1) && !(op2 & 0x20))
dda83cd7
SM
13812 {
13813 /* Handle STRB (register). */
13814 reg_rm = bits (thumb2_insn_r->arm_insn, 0, 3);
13815 regcache_raw_read_unsigned (reg_cache, reg_rm, &u_regval[1]);
13816 shift_imm = bits (thumb2_insn_r->arm_insn, 4, 5);
13817 offset_addr = u_regval[1] << shift_imm;
13818 address = u_regval[0] + offset_addr;
13819 }
c6ec2b30 13820 else
dda83cd7
SM
13821 {
13822 offset_imm = bits (thumb2_insn_r->arm_insn, 0, 7);
13823 if (bit (thumb2_insn_r->arm_insn, 10))
13824 {
13825 if (bit (thumb2_insn_r->arm_insn, 9))
13826 offset_addr = u_regval[0] + offset_imm;
13827 else
13828 offset_addr = u_regval[0] - offset_imm;
13829
13830 address = offset_addr;
13831 }
13832 else
13833 address = u_regval[0];
13834 }
c6ec2b30
OJ
13835 }
13836
13837 switch (op1)
13838 {
13839 /* Store byte instructions. */
13840 case 4:
13841 case 0:
dda83cd7
SM
13842 record_buf_mem[0] = 1;
13843 break;
c6ec2b30
OJ
13844 /* Store half word instructions. */
13845 case 1:
13846 case 5:
dda83cd7
SM
13847 record_buf_mem[0] = 2;
13848 break;
c6ec2b30
OJ
13849 /* Store word instructions. */
13850 case 2:
13851 case 6:
dda83cd7
SM
13852 record_buf_mem[0] = 4;
13853 break;
c6ec2b30
OJ
13854
13855 default:
dda83cd7
SM
13856 gdb_assert_not_reached ("no decoding pattern found");
13857 break;
c6ec2b30
OJ
13858 }
13859
13860 record_buf_mem[1] = address;
13861 thumb2_insn_r->mem_rec_count = 1;
13862 record_buf[0] = reg_rn;
13863 thumb2_insn_r->reg_rec_count = 1;
13864
13865 REG_ALLOC (thumb2_insn_r->arm_regs, thumb2_insn_r->reg_rec_count,
dda83cd7 13866 record_buf);
c6ec2b30 13867 MEM_ALLOC (thumb2_insn_r->arm_mems, thumb2_insn_r->mem_rec_count,
dda83cd7 13868 record_buf_mem);
c6ec2b30
OJ
13869 return ARM_RECORD_SUCCESS;
13870}
13871
13872/* Handler for thumb2 load memory hints instructions. */
13873
13874static int
4748a9be 13875thumb2_record_ld_mem_hints (arm_insn_decode_record *thumb2_insn_r)
c6ec2b30
OJ
13876{
13877 uint32_t record_buf[8];
13878 uint32_t reg_rt, reg_rn;
13879
13880 reg_rt = bits (thumb2_insn_r->arm_insn, 12, 15);
13881 reg_rn = bits (thumb2_insn_r->arm_insn, 16, 19);
13882
13883 if (ARM_PC_REGNUM != reg_rt)
13884 {
13885 record_buf[0] = reg_rt;
13886 record_buf[1] = reg_rn;
13887 record_buf[2] = ARM_PS_REGNUM;
13888 thumb2_insn_r->reg_rec_count = 3;
13889
13890 REG_ALLOC (thumb2_insn_r->arm_regs, thumb2_insn_r->reg_rec_count,
dda83cd7 13891 record_buf);
c6ec2b30
OJ
13892 return ARM_RECORD_SUCCESS;
13893 }
13894
13895 return ARM_RECORD_FAILURE;
13896}
13897
13898/* Handler for thumb2 load word instructions. */
13899
13900static int
4748a9be 13901thumb2_record_ld_word (arm_insn_decode_record *thumb2_insn_r)
c6ec2b30 13902{
c6ec2b30
OJ
13903 uint32_t record_buf[8];
13904
13905 record_buf[0] = bits (thumb2_insn_r->arm_insn, 12, 15);
13906 record_buf[1] = ARM_PS_REGNUM;
13907 thumb2_insn_r->reg_rec_count = 2;
13908
13909 REG_ALLOC (thumb2_insn_r->arm_regs, thumb2_insn_r->reg_rec_count,
dda83cd7 13910 record_buf);
c6ec2b30
OJ
13911 return ARM_RECORD_SUCCESS;
13912}
13913
13914/* Handler for thumb2 long multiply, long multiply accumulate, and
13915 divide instructions. */
13916
13917static int
4748a9be 13918thumb2_record_lmul_lmla_div (arm_insn_decode_record *thumb2_insn_r)
c6ec2b30
OJ
13919{
13920 uint32_t opcode1 = 0, opcode2 = 0;
13921 uint32_t record_buf[8];
c6ec2b30
OJ
13922
13923 opcode1 = bits (thumb2_insn_r->arm_insn, 20, 22);
13924 opcode2 = bits (thumb2_insn_r->arm_insn, 4, 7);
13925
13926 if (0 == opcode1 || 2 == opcode1 || (opcode1 >= 4 && opcode1 <= 6))
13927 {
13928 /* Handle SMULL, UMULL, SMULAL. */
13929 /* Handle SMLAL(S), SMULL(S), UMLAL(S), UMULL(S). */
13930 record_buf[0] = bits (thumb2_insn_r->arm_insn, 16, 19);
13931 record_buf[1] = bits (thumb2_insn_r->arm_insn, 12, 15);
13932 record_buf[2] = ARM_PS_REGNUM;
13933 thumb2_insn_r->reg_rec_count = 3;
13934 }
13935 else if (1 == opcode1 || 3 == opcode2)
13936 {
13937 /* Handle SDIV and UDIV. */
13938 record_buf[0] = bits (thumb2_insn_r->arm_insn, 16, 19);
13939 record_buf[1] = bits (thumb2_insn_r->arm_insn, 12, 15);
13940 record_buf[2] = ARM_PS_REGNUM;
13941 thumb2_insn_r->reg_rec_count = 3;
13942 }
13943 else
13944 return ARM_RECORD_FAILURE;
13945
13946 REG_ALLOC (thumb2_insn_r->arm_regs, thumb2_insn_r->reg_rec_count,
dda83cd7 13947 record_buf);
c6ec2b30
OJ
13948 return ARM_RECORD_SUCCESS;
13949}
13950
60cc5e93
OJ
13951/* Record handler for thumb32 coprocessor instructions. */
13952
13953static int
4748a9be 13954thumb2_record_coproc_insn (arm_insn_decode_record *thumb2_insn_r)
60cc5e93
OJ
13955{
13956 if (bit (thumb2_insn_r->arm_insn, 25))
13957 return arm_record_coproc_data_proc (thumb2_insn_r);
13958 else
13959 return arm_record_asimd_vfp_coproc (thumb2_insn_r);
13960}
13961
1e1b6563
OJ
13962/* Record handler for advance SIMD structure load/store instructions. */
13963
13964static int
4748a9be 13965thumb2_record_asimd_struct_ld_st (arm_insn_decode_record *thumb2_insn_r)
1e1b6563
OJ
13966{
13967 struct regcache *reg_cache = thumb2_insn_r->regcache;
13968 uint32_t l_bit, a_bit, b_bits;
13969 uint32_t record_buf[128], record_buf_mem[128];
bec2ab5a 13970 uint32_t reg_rn, reg_vd, address, f_elem;
1e1b6563
OJ
13971 uint32_t index_r = 0, index_e = 0, bf_regs = 0, index_m = 0, loop_t = 0;
13972 uint8_t f_ebytes;
13973
13974 l_bit = bit (thumb2_insn_r->arm_insn, 21);
13975 a_bit = bit (thumb2_insn_r->arm_insn, 23);
13976 b_bits = bits (thumb2_insn_r->arm_insn, 8, 11);
13977 reg_rn = bits (thumb2_insn_r->arm_insn, 16, 19);
13978 reg_vd = bits (thumb2_insn_r->arm_insn, 12, 15);
13979 reg_vd = (bit (thumb2_insn_r->arm_insn, 22) << 4) | reg_vd;
13980 f_ebytes = (1 << bits (thumb2_insn_r->arm_insn, 6, 7));
1e1b6563
OJ
13981 f_elem = 8 / f_ebytes;
13982
13983 if (!l_bit)
13984 {
13985 ULONGEST u_regval = 0;
13986 regcache_raw_read_unsigned (reg_cache, reg_rn, &u_regval);
13987 address = u_regval;
13988
13989 if (!a_bit)
dda83cd7
SM
13990 {
13991 /* Handle VST1. */
13992 if (b_bits == 0x02 || b_bits == 0x0a || (b_bits & 0x0e) == 0x06)
13993 {
13994 if (b_bits == 0x07)
13995 bf_regs = 1;
13996 else if (b_bits == 0x0a)
13997 bf_regs = 2;
13998 else if (b_bits == 0x06)
13999 bf_regs = 3;
14000 else if (b_bits == 0x02)
14001 bf_regs = 4;
14002 else
14003 bf_regs = 0;
14004
14005 for (index_r = 0; index_r < bf_regs; index_r++)
14006 {
14007 for (index_e = 0; index_e < f_elem; index_e++)
14008 {
14009 record_buf_mem[index_m++] = f_ebytes;
14010 record_buf_mem[index_m++] = address;
14011 address = address + f_ebytes;
14012 thumb2_insn_r->mem_rec_count += 1;
14013 }
14014 }
14015 }
14016 /* Handle VST2. */
14017 else if (b_bits == 0x03 || (b_bits & 0x0e) == 0x08)
14018 {
14019 if (b_bits == 0x09 || b_bits == 0x08)
14020 bf_regs = 1;
14021 else if (b_bits == 0x03)
14022 bf_regs = 2;
14023 else
14024 bf_regs = 0;
14025
14026 for (index_r = 0; index_r < bf_regs; index_r++)
14027 for (index_e = 0; index_e < f_elem; index_e++)
14028 {
14029 for (loop_t = 0; loop_t < 2; loop_t++)
14030 {
14031 record_buf_mem[index_m++] = f_ebytes;
14032 record_buf_mem[index_m++] = address + (loop_t * f_ebytes);
14033 thumb2_insn_r->mem_rec_count += 1;
14034 }
14035 address = address + (2 * f_ebytes);
14036 }
14037 }
14038 /* Handle VST3. */
14039 else if ((b_bits & 0x0e) == 0x04)
14040 {
14041 for (index_e = 0; index_e < f_elem; index_e++)
14042 {
14043 for (loop_t = 0; loop_t < 3; loop_t++)
14044 {
14045 record_buf_mem[index_m++] = f_ebytes;
14046 record_buf_mem[index_m++] = address + (loop_t * f_ebytes);
14047 thumb2_insn_r->mem_rec_count += 1;
14048 }
14049 address = address + (3 * f_ebytes);
14050 }
14051 }
14052 /* Handle VST4. */
14053 else if (!(b_bits & 0x0e))
14054 {
14055 for (index_e = 0; index_e < f_elem; index_e++)
14056 {
14057 for (loop_t = 0; loop_t < 4; loop_t++)
14058 {
14059 record_buf_mem[index_m++] = f_ebytes;
14060 record_buf_mem[index_m++] = address + (loop_t * f_ebytes);
14061 thumb2_insn_r->mem_rec_count += 1;
14062 }
14063 address = address + (4 * f_ebytes);
14064 }
14065 }
14066 }
1e1b6563 14067 else
dda83cd7
SM
14068 {
14069 uint8_t bft_size = bits (thumb2_insn_r->arm_insn, 10, 11);
14070
14071 if (bft_size == 0x00)
14072 f_ebytes = 1;
14073 else if (bft_size == 0x01)
14074 f_ebytes = 2;
14075 else if (bft_size == 0x02)
14076 f_ebytes = 4;
14077 else
14078 f_ebytes = 0;
14079
14080 /* Handle VST1. */
14081 if (!(b_bits & 0x0b) || b_bits == 0x08)
14082 thumb2_insn_r->mem_rec_count = 1;
14083 /* Handle VST2. */
14084 else if ((b_bits & 0x0b) == 0x01 || b_bits == 0x09)
14085 thumb2_insn_r->mem_rec_count = 2;
14086 /* Handle VST3. */
14087 else if ((b_bits & 0x0b) == 0x02 || b_bits == 0x0a)
14088 thumb2_insn_r->mem_rec_count = 3;
14089 /* Handle VST4. */
14090 else if ((b_bits & 0x0b) == 0x03 || b_bits == 0x0b)
14091 thumb2_insn_r->mem_rec_count = 4;
14092
14093 for (index_m = 0; index_m < thumb2_insn_r->mem_rec_count; index_m++)
14094 {
14095 record_buf_mem[index_m] = f_ebytes;
14096 record_buf_mem[index_m] = address + (index_m * f_ebytes);
14097 }
14098 }
1e1b6563
OJ
14099 }
14100 else
14101 {
14102 if (!a_bit)
dda83cd7
SM
14103 {
14104 /* Handle VLD1. */
14105 if (b_bits == 0x02 || b_bits == 0x0a || (b_bits & 0x0e) == 0x06)
14106 thumb2_insn_r->reg_rec_count = 1;
14107 /* Handle VLD2. */
14108 else if (b_bits == 0x03 || (b_bits & 0x0e) == 0x08)
14109 thumb2_insn_r->reg_rec_count = 2;
14110 /* Handle VLD3. */
14111 else if ((b_bits & 0x0e) == 0x04)
14112 thumb2_insn_r->reg_rec_count = 3;
14113 /* Handle VLD4. */
14114 else if (!(b_bits & 0x0e))
14115 thumb2_insn_r->reg_rec_count = 4;
14116 }
1e1b6563 14117 else
dda83cd7
SM
14118 {
14119 /* Handle VLD1. */
14120 if (!(b_bits & 0x0b) || b_bits == 0x08 || b_bits == 0x0c)
14121 thumb2_insn_r->reg_rec_count = 1;
14122 /* Handle VLD2. */
14123 else if ((b_bits & 0x0b) == 0x01 || b_bits == 0x09 || b_bits == 0x0d)
14124 thumb2_insn_r->reg_rec_count = 2;
14125 /* Handle VLD3. */
14126 else if ((b_bits & 0x0b) == 0x02 || b_bits == 0x0a || b_bits == 0x0e)
14127 thumb2_insn_r->reg_rec_count = 3;
14128 /* Handle VLD4. */
14129 else if ((b_bits & 0x0b) == 0x03 || b_bits == 0x0b || b_bits == 0x0f)
14130 thumb2_insn_r->reg_rec_count = 4;
14131
14132 for (index_r = 0; index_r < thumb2_insn_r->reg_rec_count; index_r++)
14133 record_buf[index_r] = reg_vd + ARM_D0_REGNUM + index_r;
14134 }
1e1b6563
OJ
14135 }
14136
14137 if (bits (thumb2_insn_r->arm_insn, 0, 3) != 15)
14138 {
14139 record_buf[index_r] = reg_rn;
14140 thumb2_insn_r->reg_rec_count += 1;
14141 }
14142
14143 REG_ALLOC (thumb2_insn_r->arm_regs, thumb2_insn_r->reg_rec_count,
dda83cd7 14144 record_buf);
1e1b6563 14145 MEM_ALLOC (thumb2_insn_r->arm_mems, thumb2_insn_r->mem_rec_count,
dda83cd7 14146 record_buf_mem);
1e1b6563
OJ
14147 return 0;
14148}
14149
c6ec2b30
OJ
14150/* Decodes thumb2 instruction type and invokes its record handler. */
14151
14152static unsigned int
4748a9be 14153thumb2_record_decode_insn_handler (arm_insn_decode_record *thumb2_insn_r)
c6ec2b30
OJ
14154{
14155 uint32_t op, op1, op2;
14156
14157 op = bit (thumb2_insn_r->arm_insn, 15);
14158 op1 = bits (thumb2_insn_r->arm_insn, 27, 28);
14159 op2 = bits (thumb2_insn_r->arm_insn, 20, 26);
14160
14161 if (op1 == 0x01)
14162 {
14163 if (!(op2 & 0x64 ))
dda83cd7
SM
14164 {
14165 /* Load/store multiple instruction. */
14166 return thumb2_record_ld_st_multiple (thumb2_insn_r);
14167 }
b121eeb9 14168 else if ((op2 & 0x64) == 0x4)
dda83cd7
SM
14169 {
14170 /* Load/store (dual/exclusive) and table branch instruction. */
14171 return thumb2_record_ld_st_dual_ex_tbb (thumb2_insn_r);
14172 }
b121eeb9 14173 else if ((op2 & 0x60) == 0x20)
dda83cd7
SM
14174 {
14175 /* Data-processing (shifted register). */
14176 return thumb2_record_data_proc_sreg_mimm (thumb2_insn_r);
14177 }
c6ec2b30 14178 else if (op2 & 0x40)
dda83cd7
SM
14179 {
14180 /* Co-processor instructions. */
14181 return thumb2_record_coproc_insn (thumb2_insn_r);
14182 }
c6ec2b30
OJ
14183 }
14184 else if (op1 == 0x02)
14185 {
14186 if (op)
dda83cd7
SM
14187 {
14188 /* Branches and miscellaneous control instructions. */
14189 return thumb2_record_branch_misc_cntrl (thumb2_insn_r);
14190 }
c6ec2b30 14191 else if (op2 & 0x20)
dda83cd7
SM
14192 {
14193 /* Data-processing (plain binary immediate) instruction. */
14194 return thumb2_record_ps_dest_generic (thumb2_insn_r);
14195 }
c6ec2b30 14196 else
dda83cd7
SM
14197 {
14198 /* Data-processing (modified immediate). */
14199 return thumb2_record_data_proc_sreg_mimm (thumb2_insn_r);
14200 }
c6ec2b30
OJ
14201 }
14202 else if (op1 == 0x03)
14203 {
14204 if (!(op2 & 0x71 ))
dda83cd7
SM
14205 {
14206 /* Store single data item. */
14207 return thumb2_record_str_single_data (thumb2_insn_r);
14208 }
c6ec2b30 14209 else if (!((op2 & 0x71) ^ 0x10))
dda83cd7
SM
14210 {
14211 /* Advanced SIMD or structure load/store instructions. */
14212 return thumb2_record_asimd_struct_ld_st (thumb2_insn_r);
14213 }
c6ec2b30 14214 else if (!((op2 & 0x67) ^ 0x01))
dda83cd7
SM
14215 {
14216 /* Load byte, memory hints instruction. */
14217 return thumb2_record_ld_mem_hints (thumb2_insn_r);
14218 }
c6ec2b30 14219 else if (!((op2 & 0x67) ^ 0x03))
dda83cd7
SM
14220 {
14221 /* Load halfword, memory hints instruction. */
14222 return thumb2_record_ld_mem_hints (thumb2_insn_r);
14223 }
c6ec2b30 14224 else if (!((op2 & 0x67) ^ 0x05))
dda83cd7
SM
14225 {
14226 /* Load word instruction. */
14227 return thumb2_record_ld_word (thumb2_insn_r);
14228 }
c6ec2b30 14229 else if (!((op2 & 0x70) ^ 0x20))
dda83cd7
SM
14230 {
14231 /* Data-processing (register) instruction. */
14232 return thumb2_record_ps_dest_generic (thumb2_insn_r);
14233 }
c6ec2b30 14234 else if (!((op2 & 0x78) ^ 0x30))
dda83cd7
SM
14235 {
14236 /* Multiply, multiply accumulate, abs diff instruction. */
14237 return thumb2_record_ps_dest_generic (thumb2_insn_r);
14238 }
c6ec2b30 14239 else if (!((op2 & 0x78) ^ 0x38))
dda83cd7
SM
14240 {
14241 /* Long multiply, long multiply accumulate, and divide. */
14242 return thumb2_record_lmul_lmla_div (thumb2_insn_r);
14243 }
c6ec2b30 14244 else if (op2 & 0x40)
dda83cd7
SM
14245 {
14246 /* Co-processor instructions. */
14247 return thumb2_record_coproc_insn (thumb2_insn_r);
14248 }
c6ec2b30
OJ
14249 }
14250
14251 return -1;
14252}
72508ac0 14253
ffdbe864 14254namespace {
0833fb8f 14255/* Abstract instruction reader. */
728a7913 14256
0833fb8f 14257class abstract_instruction_reader
728a7913
YQ
14258{
14259public:
0833fb8f
LM
14260 /* Read one instruction of size LEN from address MEMADDR and using
14261 BYTE_ORDER endianness. */
728a7913 14262
0833fb8f
LM
14263 virtual ULONGEST read (CORE_ADDR memaddr, const size_t len,
14264 enum bfd_endian byte_order) = 0;
728a7913
YQ
14265};
14266
14267/* Instruction reader from real target. */
14268
0833fb8f 14269class instruction_reader : public abstract_instruction_reader
728a7913
YQ
14270{
14271 public:
0833fb8f
LM
14272 ULONGEST read (CORE_ADDR memaddr, const size_t len,
14273 enum bfd_endian byte_order) override
728a7913 14274 {
0833fb8f 14275 return read_code_unsigned_integer (memaddr, len, byte_order);
728a7913
YQ
14276 }
14277};
14278
ffdbe864
YQ
14279} // namespace
14280
4748a9be 14281typedef int (*sti_arm_hdl_fp_t) (arm_insn_decode_record*);
72508ac0
PO
14282
14283/* Decode arm/thumb insn depending on condition cods and opcodes; and
14284 dispatch it. */
14285
14286static int
0833fb8f 14287decode_insn (abstract_instruction_reader &reader,
4748a9be 14288 arm_insn_decode_record *arm_record,
728a7913 14289 record_type_t record_type, uint32_t insn_size)
72508ac0
PO
14290{
14291
01e57735
YQ
14292 /* (Starting from numerical 0); bits 25, 26, 27 decodes type of arm
14293 instruction. */
0fa9c223 14294 static const sti_arm_hdl_fp_t arm_handle_insn[8] =
72508ac0
PO
14295 {
14296 arm_record_data_proc_misc_ld_str, /* 000. */
14297 arm_record_data_proc_imm, /* 001. */
14298 arm_record_ld_st_imm_offset, /* 010. */
14299 arm_record_ld_st_reg_offset, /* 011. */
14300 arm_record_ld_st_multiple, /* 100. */
14301 arm_record_b_bl, /* 101. */
60cc5e93 14302 arm_record_asimd_vfp_coproc, /* 110. */
72508ac0
PO
14303 arm_record_coproc_data_proc /* 111. */
14304 };
14305
01e57735
YQ
14306 /* (Starting from numerical 0); bits 13,14,15 decodes type of thumb
14307 instruction. */
0fa9c223 14308 static const sti_arm_hdl_fp_t thumb_handle_insn[8] =
72508ac0
PO
14309 { \
14310 thumb_record_shift_add_sub, /* 000. */
14311 thumb_record_add_sub_cmp_mov, /* 001. */
14312 thumb_record_ld_st_reg_offset, /* 010. */
14313 thumb_record_ld_st_imm_offset, /* 011. */
14314 thumb_record_ld_st_stack, /* 100. */
14315 thumb_record_misc, /* 101. */
14316 thumb_record_ldm_stm_swi, /* 110. */
14317 thumb_record_branch /* 111. */
14318 };
14319
14320 uint32_t ret = 0; /* return value: negative:failure 0:success. */
14321 uint32_t insn_id = 0;
0833fb8f
LM
14322 enum bfd_endian code_endian
14323 = gdbarch_byte_order_for_code (arm_record->gdbarch);
14324 arm_record->arm_insn
14325 = reader.read (arm_record->this_addr, insn_size, code_endian);
72508ac0 14326
0833fb8f 14327 if (ARM_RECORD == record_type)
72508ac0
PO
14328 {
14329 arm_record->cond = bits (arm_record->arm_insn, 28, 31);
14330 insn_id = bits (arm_record->arm_insn, 25, 27);
ca92db2d
YQ
14331
14332 if (arm_record->cond == 0xf)
14333 ret = arm_record_extension_space (arm_record);
14334 else
01e57735 14335 {
ca92db2d
YQ
14336 /* If this insn has fallen into extension space
14337 then we need not decode it anymore. */
01e57735
YQ
14338 ret = arm_handle_insn[insn_id] (arm_record);
14339 }
ca92db2d
YQ
14340 if (ret != ARM_RECORD_SUCCESS)
14341 {
14342 arm_record_unsupported_insn (arm_record);
14343 ret = -1;
14344 }
72508ac0
PO
14345 }
14346 else if (THUMB_RECORD == record_type)
14347 {
14348 /* As thumb does not have condition codes, we set negative. */
14349 arm_record->cond = -1;
14350 insn_id = bits (arm_record->arm_insn, 13, 15);
14351 ret = thumb_handle_insn[insn_id] (arm_record);
ca92db2d
YQ
14352 if (ret != ARM_RECORD_SUCCESS)
14353 {
14354 arm_record_unsupported_insn (arm_record);
14355 ret = -1;
14356 }
72508ac0
PO
14357 }
14358 else if (THUMB2_RECORD == record_type)
14359 {
c6ec2b30
OJ
14360 /* As thumb does not have condition codes, we set negative. */
14361 arm_record->cond = -1;
14362
14363 /* Swap first half of 32bit thumb instruction with second half. */
14364 arm_record->arm_insn
01e57735 14365 = (arm_record->arm_insn >> 16) | (arm_record->arm_insn << 16);
c6ec2b30 14366
ca92db2d 14367 ret = thumb2_record_decode_insn_handler (arm_record);
c6ec2b30 14368
ca92db2d 14369 if (ret != ARM_RECORD_SUCCESS)
01e57735
YQ
14370 {
14371 arm_record_unsupported_insn (arm_record);
14372 ret = -1;
14373 }
72508ac0
PO
14374 }
14375 else
14376 {
14377 /* Throw assertion. */
14378 gdb_assert_not_reached ("not a valid instruction, could not decode");
14379 }
14380
14381 return ret;
14382}
14383
b121eeb9
YQ
14384#if GDB_SELF_TEST
14385namespace selftests {
14386
0833fb8f
LM
14387/* Instruction reader class for selftests.
14388
14389 For 16-bit Thumb instructions, an array of uint16_t should be used.
b121eeb9 14390
0833fb8f
LM
14391 For 32-bit Thumb instructions and regular 32-bit Arm instructions, an array
14392 of uint32_t should be used. */
14393
14394template<typename T>
14395class instruction_reader_selftest : public abstract_instruction_reader
b121eeb9
YQ
14396{
14397public:
14398 template<size_t SIZE>
0833fb8f
LM
14399 instruction_reader_selftest (const T (&insns)[SIZE])
14400 : m_insns (insns), m_insns_size (SIZE)
b121eeb9
YQ
14401 {}
14402
0833fb8f
LM
14403 ULONGEST read (CORE_ADDR memaddr, const size_t length,
14404 enum bfd_endian byte_order) override
b121eeb9 14405 {
0833fb8f
LM
14406 SELF_CHECK (length == sizeof (T));
14407 SELF_CHECK (memaddr % sizeof (T) == 0);
14408 SELF_CHECK ((memaddr / sizeof (T)) < m_insns_size);
b121eeb9 14409
0833fb8f 14410 return m_insns[memaddr / sizeof (T)];
b121eeb9
YQ
14411 }
14412
14413private:
0833fb8f
LM
14414 const T *m_insns;
14415 const size_t m_insns_size;
b121eeb9
YQ
14416};
14417
14418static void
14419arm_record_test (void)
14420{
14421 struct gdbarch_info info;
b121eeb9
YQ
14422 info.bfd_arch_info = bfd_scan_arch ("arm");
14423
14424 struct gdbarch *gdbarch = gdbarch_find_by_info (info);
14425
14426 SELF_CHECK (gdbarch != NULL);
14427
14428 /* 16-bit Thumb instructions. */
14429 {
4748a9be 14430 arm_insn_decode_record arm_record;
b121eeb9 14431
4748a9be 14432 memset (&arm_record, 0, sizeof (arm_insn_decode_record));
b121eeb9
YQ
14433 arm_record.gdbarch = gdbarch;
14434
0833fb8f
LM
14435 /* Use the endian-free representation of the instructions here. The test
14436 will handle endianness conversions. */
b121eeb9
YQ
14437 static const uint16_t insns[] = {
14438 /* db b2 uxtb r3, r3 */
14439 0xb2db,
14440 /* cd 58 ldr r5, [r1, r3] */
14441 0x58cd,
14442 };
14443
0833fb8f 14444 instruction_reader_selftest<uint16_t> reader (insns);
b121eeb9
YQ
14445 int ret = decode_insn (reader, &arm_record, THUMB_RECORD,
14446 THUMB_INSN_SIZE_BYTES);
14447
14448 SELF_CHECK (ret == 0);
14449 SELF_CHECK (arm_record.mem_rec_count == 0);
14450 SELF_CHECK (arm_record.reg_rec_count == 1);
14451 SELF_CHECK (arm_record.arm_regs[0] == 3);
14452
14453 arm_record.this_addr += 2;
14454 ret = decode_insn (reader, &arm_record, THUMB_RECORD,
14455 THUMB_INSN_SIZE_BYTES);
14456
14457 SELF_CHECK (ret == 0);
14458 SELF_CHECK (arm_record.mem_rec_count == 0);
14459 SELF_CHECK (arm_record.reg_rec_count == 1);
14460 SELF_CHECK (arm_record.arm_regs[0] == 5);
14461 }
14462
14463 /* 32-bit Thumb-2 instructions. */
14464 {
4748a9be 14465 arm_insn_decode_record arm_record;
b121eeb9 14466
4748a9be 14467 memset (&arm_record, 0, sizeof (arm_insn_decode_record));
b121eeb9
YQ
14468 arm_record.gdbarch = gdbarch;
14469
0833fb8f
LM
14470 /* Use the endian-free representation of the instruction here. The test
14471 will handle endianness conversions. */
14472 static const uint32_t insns[] = {
14473 /* mrc 15, 0, r7, cr13, cr0, {3} */
14474 0x7f70ee1d,
b121eeb9
YQ
14475 };
14476
0833fb8f 14477 instruction_reader_selftest<uint32_t> reader (insns);
b121eeb9
YQ
14478 int ret = decode_insn (reader, &arm_record, THUMB2_RECORD,
14479 THUMB2_INSN_SIZE_BYTES);
14480
14481 SELF_CHECK (ret == 0);
14482 SELF_CHECK (arm_record.mem_rec_count == 0);
14483 SELF_CHECK (arm_record.reg_rec_count == 1);
14484 SELF_CHECK (arm_record.arm_regs[0] == 7);
14485 }
0833fb8f
LM
14486
14487 /* 32-bit instructions. */
14488 {
14489 arm_insn_decode_record arm_record;
14490
14491 memset (&arm_record, 0, sizeof (arm_insn_decode_record));
14492 arm_record.gdbarch = gdbarch;
14493
14494 /* Use the endian-free representation of the instruction here. The test
14495 will handle endianness conversions. */
14496 static const uint32_t insns[] = {
14497 /* mov r5, r0 */
14498 0xe1a05000,
14499 };
14500
14501 instruction_reader_selftest<uint32_t> reader (insns);
14502 int ret = decode_insn (reader, &arm_record, ARM_RECORD,
14503 ARM_INSN_SIZE_BYTES);
14504
14505 SELF_CHECK (ret == 0);
14506 }
b121eeb9 14507}
9ecab40c
SM
14508
14509/* Instruction reader from manually cooked instruction sequences. */
14510
14511class test_arm_instruction_reader : public arm_instruction_reader
14512{
14513public:
14514 explicit test_arm_instruction_reader (gdb::array_view<const uint32_t> insns)
14515 : m_insns (insns)
14516 {}
14517
14518 uint32_t read (CORE_ADDR memaddr, enum bfd_endian byte_order) const override
14519 {
14520 SELF_CHECK (memaddr % 4 == 0);
14521 SELF_CHECK (memaddr / 4 < m_insns.size ());
14522
14523 return m_insns[memaddr / 4];
14524 }
14525
14526private:
14527 const gdb::array_view<const uint32_t> m_insns;
14528};
14529
14530static void
14531arm_analyze_prologue_test ()
14532{
14533 for (bfd_endian endianness : {BFD_ENDIAN_LITTLE, BFD_ENDIAN_BIG})
14534 {
14535 struct gdbarch_info info;
9ecab40c
SM
14536 info.byte_order = endianness;
14537 info.byte_order_for_code = endianness;
14538 info.bfd_arch_info = bfd_scan_arch ("arm");
14539
14540 struct gdbarch *gdbarch = gdbarch_find_by_info (info);
14541
14542 SELF_CHECK (gdbarch != NULL);
14543
14544 /* The "sub" instruction contains an immediate value rotate count of 0,
14545 which resulted in a 32-bit shift of a 32-bit value, caught by
14546 UBSan. */
14547 const uint32_t insns[] = {
14548 0xe92d4ff0, /* push {r4, r5, r6, r7, r8, r9, sl, fp, lr} */
14549 0xe1a05000, /* mov r5, r0 */
14550 0xe5903020, /* ldr r3, [r0, #32] */
14551 0xe24dd044, /* sub sp, sp, #68 ; 0x44 */
14552 };
14553
14554 test_arm_instruction_reader mem_reader (insns);
14555 arm_prologue_cache cache;
0824193f 14556 arm_cache_init (&cache, gdbarch);
9ecab40c
SM
14557
14558 arm_analyze_prologue (gdbarch, 0, sizeof (insns) - 1, &cache, mem_reader);
14559 }
14560}
14561
b121eeb9
YQ
14562} // namespace selftests
14563#endif /* GDB_SELF_TEST */
72508ac0
PO
14564
14565/* Cleans up local record registers and memory allocations. */
14566
14567static void
4748a9be 14568deallocate_reg_mem (arm_insn_decode_record *record)
72508ac0
PO
14569{
14570 xfree (record->arm_regs);
14571 xfree (record->arm_mems);
14572}
14573
14574
01e57735 14575/* Parse the current instruction and record the values of the registers and
72508ac0
PO
14576 memory that will be changed in current instruction to record_arch_list".
14577 Return -1 if something is wrong. */
14578
14579int
01e57735
YQ
14580arm_process_record (struct gdbarch *gdbarch, struct regcache *regcache,
14581 CORE_ADDR insn_addr)
72508ac0
PO
14582{
14583
72508ac0
PO
14584 uint32_t no_of_rec = 0;
14585 uint32_t ret = 0; /* return value: -1:record failure ; 0:success */
14586 ULONGEST t_bit = 0, insn_id = 0;
14587
14588 ULONGEST u_regval = 0;
14589
4748a9be 14590 arm_insn_decode_record arm_record;
72508ac0 14591
4748a9be 14592 memset (&arm_record, 0, sizeof (arm_insn_decode_record));
72508ac0
PO
14593 arm_record.regcache = regcache;
14594 arm_record.this_addr = insn_addr;
14595 arm_record.gdbarch = gdbarch;
14596
14597
14598 if (record_debug > 1)
14599 {
6cb06a8c
TT
14600 gdb_printf (gdb_stdlog, "Process record: arm_process_record "
14601 "addr = %s\n",
14602 paddress (gdbarch, arm_record.this_addr));
72508ac0
PO
14603 }
14604
728a7913 14605 instruction_reader reader;
0833fb8f
LM
14606 enum bfd_endian code_endian
14607 = gdbarch_byte_order_for_code (arm_record.gdbarch);
14608 arm_record.arm_insn
14609 = reader.read (arm_record.this_addr, 2, code_endian);
72508ac0
PO
14610
14611 /* Check the insn, whether it is thumb or arm one. */
14612
14613 t_bit = arm_psr_thumb_bit (arm_record.gdbarch);
14614 regcache_raw_read_unsigned (arm_record.regcache, ARM_PS_REGNUM, &u_regval);
14615
14616
14617 if (!(u_regval & t_bit))
14618 {
14619 /* We are decoding arm insn. */
728a7913 14620 ret = decode_insn (reader, &arm_record, ARM_RECORD, ARM_INSN_SIZE_BYTES);
72508ac0
PO
14621 }
14622 else
14623 {
14624 insn_id = bits (arm_record.arm_insn, 11, 15);
14625 /* is it thumb2 insn? */
14626 if ((0x1D == insn_id) || (0x1E == insn_id) || (0x1F == insn_id))
01e57735 14627 {
728a7913 14628 ret = decode_insn (reader, &arm_record, THUMB2_RECORD,
01e57735
YQ
14629 THUMB2_INSN_SIZE_BYTES);
14630 }
72508ac0 14631 else
01e57735
YQ
14632 {
14633 /* We are decoding thumb insn. */
728a7913
YQ
14634 ret = decode_insn (reader, &arm_record, THUMB_RECORD,
14635 THUMB_INSN_SIZE_BYTES);
01e57735 14636 }
72508ac0
PO
14637 }
14638
14639 if (0 == ret)
14640 {
14641 /* Record registers. */
25ea693b 14642 record_full_arch_list_add_reg (arm_record.regcache, ARM_PC_REGNUM);
72508ac0 14643 if (arm_record.arm_regs)
01e57735
YQ
14644 {
14645 for (no_of_rec = 0; no_of_rec < arm_record.reg_rec_count; no_of_rec++)
14646 {
14647 if (record_full_arch_list_add_reg
25ea693b 14648 (arm_record.regcache , arm_record.arm_regs[no_of_rec]))
01e57735
YQ
14649 ret = -1;
14650 }
14651 }
72508ac0
PO
14652 /* Record memories. */
14653 if (arm_record.arm_mems)
01e57735
YQ
14654 {
14655 for (no_of_rec = 0; no_of_rec < arm_record.mem_rec_count; no_of_rec++)
14656 {
14657 if (record_full_arch_list_add_mem
14658 ((CORE_ADDR)arm_record.arm_mems[no_of_rec].addr,
25ea693b 14659 arm_record.arm_mems[no_of_rec].len))
01e57735
YQ
14660 ret = -1;
14661 }
14662 }
72508ac0 14663
25ea693b 14664 if (record_full_arch_list_add_end ())
01e57735 14665 ret = -1;
72508ac0
PO
14666 }
14667
14668
14669 deallocate_reg_mem (&arm_record);
14670
14671 return ret;
14672}
d105cce5
AH
14673
14674/* See arm-tdep.h. */
14675
14676const target_desc *
92d48a1e 14677arm_read_description (arm_fp_type fp_type, bool tls)
d105cce5 14678{
92d48a1e 14679 struct target_desc *tdesc = tdesc_arm_list[fp_type][tls];
d105cce5
AH
14680
14681 if (tdesc == nullptr)
14682 {
92d48a1e
JB
14683 tdesc = arm_create_target_description (fp_type, tls);
14684 tdesc_arm_list[fp_type][tls] = tdesc;
d105cce5
AH
14685 }
14686
14687 return tdesc;
14688}
14689
14690/* See arm-tdep.h. */
14691
14692const target_desc *
14693arm_read_mprofile_description (arm_m_profile_type m_type)
14694{
14695 struct target_desc *tdesc = tdesc_arm_mprofile_list[m_type];
14696
14697 if (tdesc == nullptr)
14698 {
14699 tdesc = arm_create_mprofile_target_description (m_type);
14700 tdesc_arm_mprofile_list[m_type] = tdesc;
14701 }
14702
14703 return tdesc;
14704}